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
22 #include "wine/port.h"
31 #define WIN32_NO_STATUS
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) */
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) */
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 */
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) */
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 obj_handle_t
device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
178 static obj_handle_t
device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
179 static obj_handle_t
device_file_flush( struct fd
*fd
, struct async
*async
);
180 static obj_handle_t
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 device_file_ioctl
, /* ioctl */
213 default_fd_queue_async
, /* queue_async */
214 default_fd_reselect_async
/* reselect_async */
218 static void irp_call_dump( struct object
*obj
, int verbose
)
220 struct irp_call
*irp
= (struct irp_call
*)obj
;
221 fprintf( stderr
, "IRP call file=%p\n", irp
->file
);
224 static int irp_call_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
226 struct irp_call
*irp
= (struct irp_call
*)obj
;
228 return !irp
->file
; /* file is cleared once the irp has completed */
231 static void irp_call_destroy( struct object
*obj
)
233 struct irp_call
*irp
= (struct irp_call
*)obj
;
237 async_terminate( irp
->async
, STATUS_CANCELLED
);
238 release_object( irp
->async
);
240 if (irp
->iosb
) release_object( irp
->iosb
);
241 if (irp
->file
) release_object( irp
->file
);
242 if (irp
->thread
) release_object( irp
->thread
);
245 static struct irp_call
*create_irp( struct device_file
*file
, const irp_params_t
*params
, struct async
*async
)
247 struct irp_call
*irp
;
249 if (!file
->device
->manager
) /* it has been deleted */
251 set_error( STATUS_FILE_DELETED
);
255 if ((irp
= alloc_object( &irp_call_ops
)))
257 irp
->file
= (struct device_file
*)grab_object( file
);
260 irp
->params
= *params
;
263 if (async
) irp
->iosb
= async_get_iosb( async
);
264 if (!irp
->iosb
&& !(irp
->iosb
= create_iosb( NULL
, 0, 0 )))
266 release_object( irp
);
273 static void set_irp_result( struct irp_call
*irp
, unsigned int status
,
274 const void *out_data
, data_size_t out_size
, data_size_t result
)
276 struct device_file
*file
= irp
->file
;
277 struct iosb
*iosb
= irp
->iosb
;
279 if (!file
) return; /* already finished */
281 /* FIXME: handle the STATUS_PENDING case */
282 iosb
->status
= status
;
283 iosb
->result
= result
;
284 iosb
->out_size
= min( iosb
->out_size
, out_size
);
285 if (iosb
->out_size
&& !(iosb
->out_data
= memdup( out_data
, iosb
->out_size
)))
290 if (result
) status
= STATUS_ALERTED
;
291 async_terminate( irp
->async
, status
);
292 release_object( irp
->async
);
295 wake_up( &irp
->obj
, 0 );
297 /* remove it from the device queue */
298 list_remove( &irp
->dev_entry
);
299 release_object( irp
); /* no longer on the device queue */
300 release_object( file
);
304 static void device_dump( struct object
*obj
, int verbose
)
306 fputs( "Device\n", stderr
);
309 static struct object_type
*device_get_type( struct object
*obj
)
311 static const WCHAR name
[] = {'D','e','v','i','c','e'};
312 static const struct unicode_str str
= { name
, sizeof(name
) };
313 return get_object_type( &str
);
316 static void device_destroy( struct object
*obj
)
318 struct device
*device
= (struct device
*)obj
;
320 assert( list_empty( &device
->files
));
322 free( device
->unix_path
);
323 if (device
->manager
) list_remove( &device
->entry
);
326 static void add_irp_to_queue( struct device_file
*file
, struct irp_call
*irp
, struct thread
*thread
)
328 struct device_manager
*manager
= file
->device
->manager
;
332 grab_object( irp
); /* grab reference for queued irp */
333 irp
->thread
= thread
? (struct thread
*)grab_object( thread
) : NULL
;
334 list_add_tail( &file
->requests
, &irp
->dev_entry
);
335 list_add_tail( &manager
->requests
, &irp
->mgr_entry
);
336 if (list_head( &manager
->requests
) == &irp
->mgr_entry
) wake_up( &manager
->obj
, 0 ); /* first one */
339 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
340 unsigned int sharing
, unsigned int options
)
342 struct device
*device
= (struct device
*)obj
;
343 struct device_file
*file
;
345 if (!(file
= alloc_object( &device_file_ops
))) return NULL
;
347 file
->device
= (struct device
*)grab_object( device
);
349 list_init( &file
->requests
);
350 list_add_tail( &device
->files
, &file
->entry
);
351 if (device
->unix_path
)
354 access
= file
->obj
.ops
->map_access( &file
->obj
, access
);
355 file
->fd
= open_fd( NULL
, device
->unix_path
, O_NONBLOCK
| O_LARGEFILE
,
356 &mode
, access
, sharing
, options
);
357 if (file
->fd
) set_fd_user( file
->fd
, &device_file_fd_ops
, &file
->obj
);
359 else file
->fd
= alloc_pseudo_fd( &device_file_fd_ops
, &file
->obj
, 0 );
363 release_object( file
);
367 allow_fd_caching( file
->fd
);
371 struct irp_call
*irp
;
374 memset( ¶ms
, 0, sizeof(params
) );
375 params
.create
.major
= IRP_MJ_CREATE
;
376 params
.create
.access
= access
;
377 params
.create
.sharing
= sharing
;
378 params
.create
.options
= options
;
379 params
.create
.device
= file
->device
->user_ptr
;
381 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
383 add_irp_to_queue( file
, irp
, NULL
);
384 release_object( irp
);
390 static void device_file_dump( struct object
*obj
, int verbose
)
392 struct device_file
*file
= (struct device_file
*)obj
;
394 fprintf( stderr
, "File on device %p\n", file
->device
);
397 static struct fd
*device_file_get_fd( struct object
*obj
)
399 struct device_file
*file
= (struct device_file
*)obj
;
401 return (struct fd
*)grab_object( file
->fd
);
404 static int device_file_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
406 struct device_file
*file
= (struct device_file
*)obj
;
408 if (file
->device
->manager
&& obj
->handle_count
== 1) /* last handle */
410 struct irp_call
*irp
;
413 memset( ¶ms
, 0, sizeof(params
) );
414 params
.close
.major
= IRP_MJ_CLOSE
;
415 params
.close
.file
= file
->user_ptr
;
417 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
419 add_irp_to_queue( file
, irp
, NULL
);
420 release_object( irp
);
426 static void device_file_destroy( struct object
*obj
)
428 struct device_file
*file
= (struct device_file
*)obj
;
429 struct irp_call
*irp
, *next
;
431 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
433 list_remove( &irp
->dev_entry
);
434 release_object( irp
); /* no longer on the device queue */
436 if (file
->fd
) release_object( file
->fd
);
437 list_remove( &file
->entry
);
438 release_object( file
->device
);
441 static void set_file_user_ptr( struct device_file
*file
, client_ptr_t ptr
)
443 struct irp_call
*irp
;
445 if (file
->user_ptr
== ptr
) return; /* nothing to do */
447 file
->user_ptr
= ptr
;
449 /* update already queued irps */
451 LIST_FOR_EACH_ENTRY( irp
, &file
->requests
, struct irp_call
, dev_entry
)
453 switch (irp
->params
.major
)
455 case IRP_MJ_CLOSE
: irp
->params
.close
.file
= ptr
; break;
456 case IRP_MJ_READ
: irp
->params
.read
.file
= ptr
; break;
457 case IRP_MJ_WRITE
: irp
->params
.write
.file
= ptr
; break;
458 case IRP_MJ_FLUSH_BUFFERS
: irp
->params
.flush
.file
= ptr
; break;
459 case IRP_MJ_DEVICE_CONTROL
: irp
->params
.ioctl
.file
= ptr
; break;
464 /* queue an irp to the device */
465 static obj_handle_t
queue_irp( struct device_file
*file
, struct irp_call
*irp
, struct async
*async
)
467 obj_handle_t handle
= 0;
469 if (async_is_blocking( async
) && !(handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 ))) return 0;
471 if (!fd_queue_async( file
->fd
, async
, ASYNC_TYPE_WAIT
))
473 if (handle
) close_handle( current
->process
, handle
);
476 irp
->async
= (struct async
*)grab_object( async
);
477 add_irp_to_queue( file
, irp
, current
);
478 set_error( STATUS_PENDING
);
482 static enum server_fd_type
device_file_get_fd_type( struct fd
*fd
)
484 return FD_TYPE_DEVICE
;
487 static obj_handle_t
device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
489 struct device_file
*file
= get_fd_user( fd
);
490 struct irp_call
*irp
;
494 memset( ¶ms
, 0, sizeof(params
) );
495 params
.read
.major
= IRP_MJ_READ
;
497 params
.read
.pos
= pos
;
498 params
.read
.file
= file
->user_ptr
;
500 irp
= create_irp( file
, ¶ms
, async
);
503 handle
= queue_irp( file
, irp
, async
);
504 release_object( irp
);
508 static obj_handle_t
device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
510 struct device_file
*file
= get_fd_user( fd
);
511 struct irp_call
*irp
;
515 memset( ¶ms
, 0, sizeof(params
) );
516 params
.write
.major
= IRP_MJ_WRITE
;
517 params
.write
.key
= 0;
518 params
.write
.pos
= pos
;
519 params
.write
.file
= file
->user_ptr
;
521 irp
= create_irp( file
, ¶ms
, async
);
524 handle
= queue_irp( file
, irp
, async
);
525 release_object( irp
);
529 static obj_handle_t
device_file_flush( struct fd
*fd
, struct async
*async
)
531 struct device_file
*file
= get_fd_user( fd
);
532 struct irp_call
*irp
;
536 memset( ¶ms
, 0, sizeof(params
) );
537 params
.flush
.major
= IRP_MJ_FLUSH_BUFFERS
;
538 params
.flush
.file
= file
->user_ptr
;
540 irp
= create_irp( file
, ¶ms
, NULL
);
543 handle
= queue_irp( file
, irp
, async
);
544 release_object( irp
);
548 static obj_handle_t
device_file_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
550 struct device_file
*file
= get_fd_user( fd
);
551 struct irp_call
*irp
;
555 memset( ¶ms
, 0, sizeof(params
) );
556 params
.ioctl
.major
= IRP_MJ_DEVICE_CONTROL
;
557 params
.ioctl
.code
= code
;
558 params
.ioctl
.file
= file
->user_ptr
;
560 irp
= create_irp( file
, ¶ms
, async
);
563 handle
= queue_irp( file
, irp
, async
);
564 release_object( irp
);
568 static struct device
*create_device( struct object
*root
, const struct unicode_str
*name
,
569 struct device_manager
*manager
, unsigned int attr
)
571 struct device
*device
;
573 if ((device
= create_named_object( root
, &device_ops
, name
, attr
, NULL
)))
575 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
577 /* initialize it if it didn't already exist */
578 device
->unix_path
= NULL
;
579 device
->manager
= manager
;
580 list_add_tail( &manager
->devices
, &device
->entry
);
581 list_init( &device
->files
);
587 struct object
*create_unix_device( struct object
*root
, const struct unicode_str
*name
,
588 const char *unix_path
)
590 struct device
*device
;
592 if ((device
= create_named_object( root
, &device_ops
, name
, 0, NULL
)))
594 device
->unix_path
= strdup( unix_path
);
595 device
->manager
= NULL
; /* no manager, requests go straight to the Unix device */
596 list_init( &device
->files
);
602 /* terminate requests when the underlying device is deleted */
603 static void delete_file( struct device_file
*file
)
605 struct irp_call
*irp
, *next
;
607 /* terminate all pending requests */
608 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
610 list_remove( &irp
->mgr_entry
);
611 set_irp_result( irp
, STATUS_FILE_DELETED
, NULL
, 0, 0 );
615 static void delete_device( struct device
*device
)
617 struct device_file
*file
, *next
;
619 if (!device
->manager
) return; /* already deleted */
621 LIST_FOR_EACH_ENTRY_SAFE( file
, next
, &device
->files
, struct device_file
, entry
)
624 unlink_named_object( &device
->obj
);
625 list_remove( &device
->entry
);
626 device
->manager
= NULL
;
630 static void device_manager_dump( struct object
*obj
, int verbose
)
632 fprintf( stderr
, "Device manager\n" );
635 static int device_manager_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
637 struct device_manager
*manager
= (struct device_manager
*)obj
;
639 return !list_empty( &manager
->requests
);
642 static void device_manager_destroy( struct object
*obj
)
644 struct device_manager
*manager
= (struct device_manager
*)obj
;
647 while ((ptr
= list_head( &manager
->devices
)))
649 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
650 delete_device( device
);
654 static struct device_manager
*create_device_manager(void)
656 struct device_manager
*manager
;
658 if ((manager
= alloc_object( &device_manager_ops
)))
660 list_init( &manager
->devices
);
661 list_init( &manager
->requests
);
667 /* create a device manager */
668 DECL_HANDLER(create_device_manager
)
670 struct device_manager
*manager
= create_device_manager();
674 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
675 release_object( manager
);
680 /* create a device */
681 DECL_HANDLER(create_device
)
683 struct device
*device
;
684 struct unicode_str name
= get_req_unicode_str();
685 struct device_manager
*manager
;
686 struct object
*root
= NULL
;
688 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
689 0, &device_manager_ops
)))
692 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
)))
694 release_object( manager
);
698 if ((device
= create_device( root
, &name
, manager
, req
->attributes
)))
700 device
->user_ptr
= req
->user_ptr
;
701 reply
->handle
= alloc_handle( current
->process
, device
, req
->access
, req
->attributes
);
702 release_object( device
);
705 if (root
) release_object( root
);
706 release_object( manager
);
710 /* delete a device */
711 DECL_HANDLER(delete_device
)
713 struct device
*device
;
715 if ((device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
717 delete_device( device
);
718 release_object( device
);
723 /* retrieve the next pending device irp request */
724 DECL_HANDLER(get_next_device_request
)
726 struct irp_call
*irp
;
727 struct device_manager
*manager
;
731 reply
->params
.major
= IRP_MJ_MAXIMUM_FUNCTION
+ 1;
733 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
734 0, &device_manager_ops
)))
739 if ((irp
= (struct irp_call
*)get_handle_obj( current
->process
, req
->prev
, 0, &irp_call_ops
)))
741 set_irp_result( irp
, req
->status
, NULL
, 0, 0 );
742 close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
743 release_object( irp
);
748 if ((ptr
= list_head( &manager
->requests
)))
750 irp
= LIST_ENTRY( ptr
, struct irp_call
, mgr_entry
);
753 reply
->client_pid
= get_process_id( irp
->thread
->process
);
754 reply
->client_tid
= get_thread_id( irp
->thread
);
756 reply
->params
= irp
->params
;
758 reply
->in_size
= iosb
->in_size
;
759 reply
->out_size
= iosb
->out_size
;
760 if (iosb
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
761 else if ((reply
->next
= alloc_handle( current
->process
, irp
, 0, 0 )))
763 set_reply_data_ptr( iosb
->in_data
, iosb
->in_size
);
764 iosb
->in_data
= NULL
;
766 list_remove( &irp
->mgr_entry
);
767 list_init( &irp
->mgr_entry
);
770 else set_error( STATUS_PENDING
);
772 release_object( manager
);
776 /* store results of an async irp */
777 DECL_HANDLER(set_irp_result
)
779 struct irp_call
*irp
;
781 if ((irp
= (struct irp_call
*)get_handle_obj( current
->process
, req
->handle
, 0, &irp_call_ops
)))
783 if (irp
->file
) set_file_user_ptr( irp
->file
, req
->file_ptr
);
784 set_irp_result( irp
, req
->status
, get_req_data(), get_req_data_size(), req
->size
);
785 close_handle( current
->process
, req
->handle
); /* avoid an extra round-trip for close */
786 release_object( irp
);