wmp: Add seeking and duration.
[wine.git] / server / device.c
blobdcc29461f57257509f7930d64c5711cfe3eba239
1 /*
2 * Server-side device support
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "object.h"
37 #include "file.h"
38 #include "handle.h"
39 #include "request.h"
40 #include "process.h"
42 /* IRP object */
44 struct irp_call
46 struct object obj; /* object header */
47 struct list dev_entry; /* entry in device queue */
48 struct list mgr_entry; /* entry in manager queue */
49 struct device_file *file; /* file containing this irp */
50 struct thread *thread; /* thread that queued the irp */
51 struct async *async; /* pending async op */
52 irp_params_t params; /* irp parameters */
53 struct iosb *iosb; /* I/O status block */
56 static void irp_call_dump( struct object *obj, int verbose );
57 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
58 static void irp_call_destroy( struct object *obj );
60 static const struct object_ops irp_call_ops =
62 sizeof(struct irp_call), /* size */
63 irp_call_dump, /* dump */
64 no_get_type, /* get_type */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 irp_call_signaled, /* signaled */
68 no_satisfied, /* satisfied */
69 no_signal, /* signal */
70 no_get_fd, /* get_fd */
71 no_map_access, /* map_access */
72 default_get_sd, /* get_sd */
73 default_set_sd, /* set_sd */
74 no_lookup_name, /* lookup_name */
75 no_link_name, /* link_name */
76 NULL, /* unlink_name */
77 no_open_file, /* open_file */
78 no_close_handle, /* close_handle */
79 irp_call_destroy /* destroy */
83 /* device manager (a list of devices managed by the same client process) */
85 struct device_manager
87 struct object obj; /* object header */
88 struct list devices; /* list of devices */
89 struct list requests; /* list of pending irps across all devices */
92 static void device_manager_dump( struct object *obj, int verbose );
93 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
94 static void device_manager_destroy( struct object *obj );
96 static const struct object_ops device_manager_ops =
98 sizeof(struct device_manager), /* size */
99 device_manager_dump, /* dump */
100 no_get_type, /* get_type */
101 add_queue, /* add_queue */
102 remove_queue, /* remove_queue */
103 device_manager_signaled, /* signaled */
104 no_satisfied, /* satisfied */
105 no_signal, /* signal */
106 no_get_fd, /* get_fd */
107 no_map_access, /* map_access */
108 default_get_sd, /* get_sd */
109 default_set_sd, /* set_sd */
110 no_lookup_name, /* lookup_name */
111 no_link_name, /* link_name */
112 NULL, /* unlink_name */
113 no_open_file, /* open_file */
114 no_close_handle, /* close_handle */
115 device_manager_destroy /* destroy */
119 /* device (a single device object) */
121 struct device
123 struct object obj; /* object header */
124 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
125 char *unix_path; /* path to unix device if any */
126 client_ptr_t user_ptr; /* opaque ptr for client side */
127 struct list entry; /* entry in device manager list */
128 struct list files; /* list of open files */
131 static void device_dump( struct object *obj, int verbose );
132 static struct object_type *device_get_type( struct object *obj );
133 static void device_destroy( struct object *obj );
134 static struct object *device_open_file( struct object *obj, unsigned int access,
135 unsigned int sharing, unsigned int options );
137 static const struct object_ops device_ops =
139 sizeof(struct device), /* size */
140 device_dump, /* dump */
141 device_get_type, /* get_type */
142 no_add_queue, /* add_queue */
143 NULL, /* remove_queue */
144 NULL, /* signaled */
145 no_satisfied, /* satisfied */
146 no_signal, /* signal */
147 no_get_fd, /* get_fd */
148 default_fd_map_access, /* map_access */
149 default_get_sd, /* get_sd */
150 default_set_sd, /* set_sd */
151 no_lookup_name, /* lookup_name */
152 directory_link_name, /* link_name */
153 default_unlink_name, /* unlink_name */
154 device_open_file, /* open_file */
155 no_close_handle, /* close_handle */
156 device_destroy /* destroy */
160 /* device file (an open file handle to a device) */
162 struct device_file
164 struct object obj; /* object header */
165 struct device *device; /* device for this file */
166 struct fd *fd; /* file descriptor for irp */
167 client_ptr_t user_ptr; /* opaque ptr for client side */
168 struct list entry; /* entry in device list */
169 struct list requests; /* list of pending irp requests */
172 static void device_file_dump( struct object *obj, int verbose );
173 static struct fd *device_file_get_fd( struct object *obj );
174 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
175 static void device_file_destroy( struct object *obj );
176 static enum server_fd_type device_file_get_fd_type( struct fd *fd );
177 static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos );
178 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos );
179 static int device_file_flush( struct fd *fd, struct async *async );
180 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
182 static const struct object_ops device_file_ops =
184 sizeof(struct device_file), /* size */
185 device_file_dump, /* dump */
186 no_get_type, /* get_type */
187 add_queue, /* add_queue */
188 remove_queue, /* remove_queue */
189 default_fd_signaled, /* signaled */
190 no_satisfied, /* satisfied */
191 no_signal, /* signal */
192 device_file_get_fd, /* get_fd */
193 default_fd_map_access, /* map_access */
194 default_get_sd, /* get_sd */
195 default_set_sd, /* set_sd */
196 no_lookup_name, /* lookup_name */
197 no_link_name, /* link_name */
198 NULL, /* unlink_name */
199 no_open_file, /* open_file */
200 device_file_close_handle, /* close_handle */
201 device_file_destroy /* destroy */
204 static const struct fd_ops device_file_fd_ops =
206 default_fd_get_poll_events, /* get_poll_events */
207 default_poll_event, /* poll_event */
208 device_file_get_fd_type, /* get_fd_type */
209 device_file_read, /* read */
210 device_file_write, /* write */
211 device_file_flush, /* flush */
212 no_fd_get_file_info, /* get_file_info */
213 no_fd_get_volume_info, /* get_volume_info */
214 device_file_ioctl, /* ioctl */
215 default_fd_queue_async, /* queue_async */
216 default_fd_reselect_async /* reselect_async */
220 static void irp_call_dump( struct object *obj, int verbose )
222 struct irp_call *irp = (struct irp_call *)obj;
223 fprintf( stderr, "IRP call file=%p\n", irp->file );
226 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
228 struct irp_call *irp = (struct irp_call *)obj;
230 return !irp->file; /* file is cleared once the irp has completed */
233 static void irp_call_destroy( struct object *obj )
235 struct irp_call *irp = (struct irp_call *)obj;
237 if (irp->async)
239 async_terminate( irp->async, STATUS_CANCELLED );
240 release_object( irp->async );
242 if (irp->iosb) release_object( irp->iosb );
243 if (irp->file) release_object( irp->file );
244 if (irp->thread) release_object( irp->thread );
247 static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params, struct async *async )
249 struct irp_call *irp;
251 if (!file->device->manager) /* it has been deleted */
253 set_error( STATUS_FILE_DELETED );
254 return NULL;
257 if ((irp = alloc_object( &irp_call_ops )))
259 irp->file = (struct device_file *)grab_object( file );
260 irp->thread = NULL;
261 irp->async = NULL;
262 irp->params = *params;
263 irp->iosb = NULL;
265 if (async) irp->iosb = async_get_iosb( async );
266 if (!irp->iosb && !(irp->iosb = create_iosb( NULL, 0, 0 )))
268 release_object( irp );
269 irp = NULL;
272 return irp;
275 static void set_irp_result( struct irp_call *irp, unsigned int status,
276 const void *out_data, data_size_t out_size, data_size_t result )
278 struct device_file *file = irp->file;
279 struct iosb *iosb = irp->iosb;
281 if (!file) return; /* already finished */
283 /* FIXME: handle the STATUS_PENDING case */
284 iosb->status = status;
285 iosb->result = result;
286 iosb->out_size = min( iosb->out_size, out_size );
287 if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size )))
288 iosb->out_size = 0;
289 irp->file = NULL;
290 if (irp->async)
292 if (result) status = STATUS_ALERTED;
293 async_terminate( irp->async, status );
294 release_object( irp->async );
295 irp->async = NULL;
297 wake_up( &irp->obj, 0 );
299 /* remove it from the device queue */
300 list_remove( &irp->dev_entry );
301 release_object( irp ); /* no longer on the device queue */
302 release_object( file );
306 static void device_dump( struct object *obj, int verbose )
308 fputs( "Device\n", stderr );
311 static struct object_type *device_get_type( struct object *obj )
313 static const WCHAR name[] = {'D','e','v','i','c','e'};
314 static const struct unicode_str str = { name, sizeof(name) };
315 return get_object_type( &str );
318 static void device_destroy( struct object *obj )
320 struct device *device = (struct device *)obj;
322 assert( list_empty( &device->files ));
324 free( device->unix_path );
325 if (device->manager) list_remove( &device->entry );
328 static void add_irp_to_queue( struct device_file *file, struct irp_call *irp, struct thread *thread )
330 struct device_manager *manager = file->device->manager;
332 assert( manager );
334 grab_object( irp ); /* grab reference for queued irp */
335 irp->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
336 list_add_tail( &file->requests, &irp->dev_entry );
337 list_add_tail( &manager->requests, &irp->mgr_entry );
338 if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 ); /* first one */
341 static struct object *device_open_file( struct object *obj, unsigned int access,
342 unsigned int sharing, unsigned int options )
344 struct device *device = (struct device *)obj;
345 struct device_file *file;
347 if (!(file = alloc_object( &device_file_ops ))) return NULL;
349 file->device = (struct device *)grab_object( device );
350 file->user_ptr = 0;
351 list_init( &file->requests );
352 list_add_tail( &device->files, &file->entry );
353 if (device->unix_path)
355 mode_t mode = 0666;
356 access = file->obj.ops->map_access( &file->obj, access );
357 file->fd = open_fd( NULL, device->unix_path, O_NONBLOCK | O_LARGEFILE,
358 &mode, access, sharing, options );
359 if (file->fd) set_fd_user( file->fd, &device_file_fd_ops, &file->obj );
361 else file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, 0 );
363 if (!file->fd)
365 release_object( file );
366 return NULL;
369 allow_fd_caching( file->fd );
371 if (device->manager)
373 struct irp_call *irp;
374 irp_params_t params;
376 memset( &params, 0, sizeof(params) );
377 params.create.major = IRP_MJ_CREATE;
378 params.create.access = access;
379 params.create.sharing = sharing;
380 params.create.options = options;
381 params.create.device = file->device->user_ptr;
383 if ((irp = create_irp( file, &params, NULL )))
385 add_irp_to_queue( file, irp, NULL );
386 release_object( irp );
389 return &file->obj;
392 static void device_file_dump( struct object *obj, int verbose )
394 struct device_file *file = (struct device_file *)obj;
396 fprintf( stderr, "File on device %p\n", file->device );
399 static struct fd *device_file_get_fd( struct object *obj )
401 struct device_file *file = (struct device_file *)obj;
403 return (struct fd *)grab_object( file->fd );
406 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
408 struct device_file *file = (struct device_file *)obj;
410 if (file->device->manager && obj->handle_count == 1) /* last handle */
412 struct irp_call *irp;
413 irp_params_t params;
415 memset( &params, 0, sizeof(params) );
416 params.close.major = IRP_MJ_CLOSE;
417 params.close.file = file->user_ptr;
419 if ((irp = create_irp( file, &params, NULL )))
421 add_irp_to_queue( file, irp, NULL );
422 release_object( irp );
425 return 1;
428 static void device_file_destroy( struct object *obj )
430 struct device_file *file = (struct device_file *)obj;
431 struct irp_call *irp, *next;
433 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
435 list_remove( &irp->dev_entry );
436 release_object( irp ); /* no longer on the device queue */
438 if (file->fd) release_object( file->fd );
439 list_remove( &file->entry );
440 release_object( file->device );
443 static void set_file_user_ptr( struct device_file *file, client_ptr_t ptr )
445 struct irp_call *irp;
447 if (file->user_ptr == ptr) return; /* nothing to do */
449 file->user_ptr = ptr;
451 /* update already queued irps */
453 LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
455 switch (irp->params.major)
457 case IRP_MJ_CLOSE: irp->params.close.file = ptr; break;
458 case IRP_MJ_READ: irp->params.read.file = ptr; break;
459 case IRP_MJ_WRITE: irp->params.write.file = ptr; break;
460 case IRP_MJ_FLUSH_BUFFERS: irp->params.flush.file = ptr; break;
461 case IRP_MJ_DEVICE_CONTROL: irp->params.ioctl.file = ptr; break;
466 /* queue an irp to the device */
467 static int queue_irp( struct device_file *file, const irp_params_t *params, struct async *async )
469 struct irp_call *irp = create_irp( file, params, async );
470 if (!irp) return 0;
472 fd_queue_async( file->fd, async, ASYNC_TYPE_WAIT );
473 irp->async = (struct async *)grab_object( async );
474 add_irp_to_queue( file, irp, current );
475 release_object( irp );
476 set_error( STATUS_PENDING );
477 return 1;
480 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
482 return FD_TYPE_DEVICE;
485 static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos )
487 struct device_file *file = get_fd_user( fd );
488 irp_params_t params;
490 memset( &params, 0, sizeof(params) );
491 params.read.major = IRP_MJ_READ;
492 params.read.key = 0;
493 params.read.pos = pos;
494 params.read.file = file->user_ptr;
495 return queue_irp( file, &params, async );
498 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos )
500 struct device_file *file = get_fd_user( fd );
501 irp_params_t params;
503 memset( &params, 0, sizeof(params) );
504 params.write.major = IRP_MJ_WRITE;
505 params.write.key = 0;
506 params.write.pos = pos;
507 params.write.file = file->user_ptr;
508 return queue_irp( file, &params, async );
511 static int device_file_flush( struct fd *fd, struct async *async )
513 struct device_file *file = get_fd_user( fd );
514 irp_params_t params;
516 memset( &params, 0, sizeof(params) );
517 params.flush.major = IRP_MJ_FLUSH_BUFFERS;
518 params.flush.file = file->user_ptr;
519 return queue_irp( file, &params, async );
522 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
524 struct device_file *file = get_fd_user( fd );
525 irp_params_t params;
527 memset( &params, 0, sizeof(params) );
528 params.ioctl.major = IRP_MJ_DEVICE_CONTROL;
529 params.ioctl.code = code;
530 params.ioctl.file = file->user_ptr;
531 return queue_irp( file, &params, async );
534 static struct device *create_device( struct object *root, const struct unicode_str *name,
535 struct device_manager *manager, unsigned int attr )
537 struct device *device;
539 if ((device = create_named_object( root, &device_ops, name, attr, NULL )))
541 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
543 /* initialize it if it didn't already exist */
544 device->unix_path = NULL;
545 device->manager = manager;
546 list_add_tail( &manager->devices, &device->entry );
547 list_init( &device->files );
550 return device;
553 struct object *create_unix_device( struct object *root, const struct unicode_str *name,
554 const char *unix_path )
556 struct device *device;
558 if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
560 device->unix_path = strdup( unix_path );
561 device->manager = NULL; /* no manager, requests go straight to the Unix device */
562 list_init( &device->files );
564 return &device->obj;
568 /* terminate requests when the underlying device is deleted */
569 static void delete_file( struct device_file *file )
571 struct irp_call *irp, *next;
573 /* terminate all pending requests */
574 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
576 list_remove( &irp->mgr_entry );
577 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
581 static void delete_device( struct device *device )
583 struct device_file *file, *next;
585 if (!device->manager) return; /* already deleted */
587 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
588 delete_file( file );
590 unlink_named_object( &device->obj );
591 list_remove( &device->entry );
592 device->manager = NULL;
596 static void device_manager_dump( struct object *obj, int verbose )
598 fprintf( stderr, "Device manager\n" );
601 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
603 struct device_manager *manager = (struct device_manager *)obj;
605 return !list_empty( &manager->requests );
608 static void device_manager_destroy( struct object *obj )
610 struct device_manager *manager = (struct device_manager *)obj;
611 struct list *ptr;
613 while ((ptr = list_head( &manager->devices )))
615 struct device *device = LIST_ENTRY( ptr, struct device, entry );
616 delete_device( device );
620 static struct device_manager *create_device_manager(void)
622 struct device_manager *manager;
624 if ((manager = alloc_object( &device_manager_ops )))
626 list_init( &manager->devices );
627 list_init( &manager->requests );
629 return manager;
633 /* create a device manager */
634 DECL_HANDLER(create_device_manager)
636 struct device_manager *manager = create_device_manager();
638 if (manager)
640 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
641 release_object( manager );
646 /* create a device */
647 DECL_HANDLER(create_device)
649 struct device *device;
650 struct unicode_str name = get_req_unicode_str();
651 struct device_manager *manager;
652 struct object *root = NULL;
654 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
655 0, &device_manager_ops )))
656 return;
658 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
660 release_object( manager );
661 return;
664 if ((device = create_device( root, &name, manager, req->attributes )))
666 device->user_ptr = req->user_ptr;
667 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
668 release_object( device );
671 if (root) release_object( root );
672 release_object( manager );
676 /* delete a device */
677 DECL_HANDLER(delete_device)
679 struct device *device;
681 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
683 delete_device( device );
684 release_object( device );
689 /* retrieve the next pending device irp request */
690 DECL_HANDLER(get_next_device_request)
692 struct irp_call *irp;
693 struct device_manager *manager;
694 struct list *ptr;
695 struct iosb *iosb;
697 reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1;
699 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
700 0, &device_manager_ops )))
701 return;
703 if (req->prev)
705 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
707 set_irp_result( irp, req->status, NULL, 0, 0 );
708 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
709 release_object( irp );
711 clear_error();
714 if ((ptr = list_head( &manager->requests )))
716 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
717 if (irp->thread)
719 reply->client_pid = get_process_id( irp->thread->process );
720 reply->client_tid = get_thread_id( irp->thread );
722 reply->params = irp->params;
723 iosb = irp->iosb;
724 reply->in_size = iosb->in_size;
725 reply->out_size = iosb->out_size;
726 if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
727 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
729 set_reply_data_ptr( iosb->in_data, iosb->in_size );
730 iosb->in_data = NULL;
731 iosb->in_size = 0;
732 list_remove( &irp->mgr_entry );
733 list_init( &irp->mgr_entry );
736 else set_error( STATUS_PENDING );
738 release_object( manager );
742 /* store results of an async irp */
743 DECL_HANDLER(set_irp_result)
745 struct irp_call *irp;
747 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
749 if (irp->file) set_file_user_ptr( irp->file, req->file_ptr );
750 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
751 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
752 release_object( irp );