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
27 #define WIN32_NO_STATUS
38 struct object obj
; /* object header */
39 struct list dev_entry
; /* entry in device queue */
40 struct list mgr_entry
; /* entry in manager queue */
41 struct device
*device
; /* device containing this ioctl */
42 struct thread
*thread
; /* thread that queued the ioctl */
43 client_ptr_t user_arg
; /* user arg used to identify the request */
44 struct async
*async
; /* pending async op */
45 ioctl_code_t code
; /* ioctl code */
46 unsigned int status
; /* resulting status (or STATUS_PENDING) */
47 data_size_t in_size
; /* size of input data */
48 void *in_data
; /* input data */
49 data_size_t out_size
; /* size of output data */
50 void *out_data
; /* output data */
53 static void ioctl_call_dump( struct object
*obj
, int verbose
);
54 static int ioctl_call_signaled( struct object
*obj
, struct thread
*thread
);
55 static void ioctl_call_destroy( struct object
*obj
);
57 static const struct object_ops ioctl_call_ops
=
59 sizeof(struct ioctl_call
), /* size */
60 ioctl_call_dump
, /* dump */
61 no_get_type
, /* get_type */
62 add_queue
, /* add_queue */
63 remove_queue
, /* remove_queue */
64 ioctl_call_signaled
, /* signaled */
65 no_satisfied
, /* satisfied */
66 no_signal
, /* signal */
67 no_get_fd
, /* get_fd */
68 no_map_access
, /* map_access */
69 default_get_sd
, /* get_sd */
70 default_set_sd
, /* set_sd */
71 no_lookup_name
, /* lookup_name */
72 no_open_file
, /* open_file */
73 no_close_handle
, /* close_handle */
74 ioctl_call_destroy
/* destroy */
80 struct object obj
; /* object header */
81 struct list devices
; /* list of devices */
82 struct list requests
; /* list of pending ioctls across all devices */
85 static void device_manager_dump( struct object
*obj
, int verbose
);
86 static int device_manager_signaled( struct object
*obj
, struct thread
*thread
);
87 static void device_manager_destroy( struct object
*obj
);
89 static const struct object_ops device_manager_ops
=
91 sizeof(struct device_manager
), /* size */
92 device_manager_dump
, /* dump */
93 no_get_type
, /* get_type */
94 add_queue
, /* add_queue */
95 remove_queue
, /* remove_queue */
96 device_manager_signaled
, /* signaled */
97 no_satisfied
, /* satisfied */
98 no_signal
, /* signal */
99 no_get_fd
, /* get_fd */
100 no_map_access
, /* map_access */
101 default_get_sd
, /* get_sd */
102 default_set_sd
, /* set_sd */
103 no_lookup_name
, /* lookup_name */
104 no_open_file
, /* open_file */
105 no_close_handle
, /* close_handle */
106 device_manager_destroy
/* destroy */
112 struct object obj
; /* object header */
113 struct device_manager
*manager
; /* manager for this device (or NULL if deleted) */
114 struct fd
*fd
; /* file descriptor for ioctl */
115 client_ptr_t user_ptr
; /* opaque ptr for client side */
116 struct list entry
; /* entry in device manager list */
117 struct list requests
; /* list of pending ioctl requests */
120 static void device_dump( struct object
*obj
, int verbose
);
121 static struct object_type
*device_get_type( struct object
*obj
);
122 static struct fd
*device_get_fd( struct object
*obj
);
123 static void device_destroy( struct object
*obj
);
124 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
125 unsigned int sharing
, unsigned int options
);
126 static enum server_fd_type
device_get_fd_type( struct fd
*fd
);
127 static obj_handle_t
device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
128 int blocking
, const void *data
, data_size_t size
);
130 static const struct object_ops device_ops
=
132 sizeof(struct device
), /* size */
133 device_dump
, /* dump */
134 device_get_type
, /* get_type */
135 no_add_queue
, /* add_queue */
136 NULL
, /* remove_queue */
138 no_satisfied
, /* satisfied */
139 no_signal
, /* signal */
140 device_get_fd
, /* get_fd */
141 default_fd_map_access
, /* map_access */
142 default_get_sd
, /* get_sd */
143 default_set_sd
, /* set_sd */
144 no_lookup_name
, /* lookup_name */
145 device_open_file
, /* open_file */
146 no_close_handle
, /* close_handle */
147 device_destroy
/* destroy */
150 static const struct fd_ops device_fd_ops
=
152 default_fd_get_poll_events
, /* get_poll_events */
153 default_poll_event
, /* poll_event */
154 no_flush
, /* flush */
155 device_get_fd_type
, /* get_fd_type */
156 default_fd_removable
, /* removable */
157 device_ioctl
, /* ioctl */
158 default_fd_queue_async
, /* queue_async */
159 default_fd_async_event
, /* async_event */
160 default_fd_async_terminated
, /* async_terminated */
161 default_fd_cancel_async
/* cancel_async */
165 static void ioctl_call_dump( struct object
*obj
, int verbose
)
167 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
168 fprintf( stderr
, "Ioctl call code=%08x device=%p\n", ioctl
->code
, ioctl
->device
);
171 static int ioctl_call_signaled( struct object
*obj
, struct thread
*thread
)
173 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
175 return !ioctl
->device
; /* device is cleared once the ioctl has completed */
178 static void ioctl_call_destroy( struct object
*obj
)
180 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
182 free( ioctl
->in_data
);
183 free( ioctl
->out_data
);
186 async_terminate( ioctl
->async
, STATUS_CANCELLED
);
187 release_object( ioctl
->async
);
189 if (ioctl
->device
) release_object( ioctl
->device
);
190 release_object( ioctl
->thread
);
193 static struct ioctl_call
*create_ioctl( struct device
*device
, ioctl_code_t code
,
194 const void *in_data
, data_size_t in_size
,
195 data_size_t out_size
)
197 struct ioctl_call
*ioctl
;
199 if ((ioctl
= alloc_object( &ioctl_call_ops
)))
201 ioctl
->device
= (struct device
*)grab_object( device
);
204 ioctl
->status
= STATUS_PENDING
;
205 ioctl
->in_size
= in_size
;
206 ioctl
->in_data
= NULL
;
207 ioctl
->out_size
= out_size
;
208 ioctl
->out_data
= NULL
;
210 if (ioctl
->in_size
&& !(ioctl
->in_data
= memdup( in_data
, in_size
)))
212 release_object( ioctl
);
219 static void set_ioctl_result( struct ioctl_call
*ioctl
, unsigned int status
,
220 const void *out_data
, data_size_t out_size
)
222 struct device
*device
= ioctl
->device
;
224 if (!device
) return; /* already finished */
226 /* FIXME: handle the STATUS_PENDING case */
227 ioctl
->status
= status
;
228 ioctl
->out_size
= min( ioctl
->out_size
, out_size
);
229 if (ioctl
->out_size
&& !(ioctl
->out_data
= memdup( out_data
, ioctl
->out_size
)))
231 release_object( device
);
232 ioctl
->device
= NULL
;
235 if (ioctl
->out_size
) status
= STATUS_ALERTED
;
236 async_terminate( ioctl
->async
, status
);
237 release_object( ioctl
->async
);
240 wake_up( &ioctl
->obj
, 0 );
242 if (status
!= STATUS_ALERTED
)
244 /* remove it from the device queue */
245 /* (for STATUS_ALERTED this will be done in get_ioctl_result) */
246 list_remove( &ioctl
->dev_entry
);
247 release_object( ioctl
); /* no longer on the device queue */
252 static void device_dump( struct object
*obj
, int verbose
)
254 struct device
*device
= (struct device
*)obj
;
256 fprintf( stderr
, "Device " );
257 dump_object_name( &device
->obj
);
258 fputc( '\n', stderr
);
261 static struct object_type
*device_get_type( struct object
*obj
)
263 static const WCHAR name
[] = {'D','e','v','i','c','e'};
264 static const struct unicode_str str
= { name
, sizeof(name
) };
265 return get_object_type( &str
);
268 static struct fd
*device_get_fd( struct object
*obj
)
270 struct device
*device
= (struct device
*)obj
;
272 return (struct fd
*)grab_object( device
->fd
);
275 static void device_destroy( struct object
*obj
)
277 struct device
*device
= (struct device
*)obj
;
278 struct ioctl_call
*ioctl
, *next
;
280 LIST_FOR_EACH_ENTRY_SAFE( ioctl
, next
, &device
->requests
, struct ioctl_call
, dev_entry
)
282 list_remove( &ioctl
->dev_entry
);
283 release_object( ioctl
); /* no longer on the device queue */
285 if (device
->fd
) release_object( device
->fd
);
286 if (device
->manager
) list_remove( &device
->entry
);
289 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
290 unsigned int sharing
, unsigned int options
)
292 return grab_object( obj
);
295 static enum server_fd_type
device_get_fd_type( struct fd
*fd
)
297 return FD_TYPE_DEVICE
;
300 static struct ioctl_call
*find_ioctl_call( struct device
*device
, struct thread
*thread
,
301 client_ptr_t user_arg
)
303 struct ioctl_call
*ioctl
;
305 LIST_FOR_EACH_ENTRY( ioctl
, &device
->requests
, struct ioctl_call
, dev_entry
)
306 if (ioctl
->thread
== thread
&& ioctl
->user_arg
== user_arg
) return ioctl
;
308 set_error( STATUS_INVALID_PARAMETER
);
312 static obj_handle_t
device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
313 int blocking
, const void *data
, data_size_t size
)
315 struct device
*device
= get_fd_user( fd
);
316 struct ioctl_call
*ioctl
;
319 if (!device
->manager
) /* it has been deleted */
321 set_error( STATUS_FILE_DELETED
);
325 if (!(ioctl
= create_ioctl( device
, code
, data
, size
, get_reply_max_size() )))
328 ioctl
->thread
= (struct thread
*)grab_object( current
);
329 ioctl
->user_arg
= async_data
->arg
;
331 if (!(handle
= alloc_handle( current
->process
, ioctl
, SYNCHRONIZE
, 0 )))
333 release_object( ioctl
);
337 if (!(ioctl
->async
= fd_queue_async( device
->fd
, async_data
, ASYNC_TYPE_WAIT
)))
339 close_handle( current
->process
, handle
);
340 release_object( ioctl
);
343 list_add_tail( &device
->requests
, &ioctl
->dev_entry
);
344 list_add_tail( &device
->manager
->requests
, &ioctl
->mgr_entry
);
345 if (list_head( &device
->manager
->requests
) == &ioctl
->mgr_entry
) /* first one */
346 wake_up( &device
->manager
->obj
, 0 );
347 /* don't release ioctl since it is now queued in the device */
348 set_error( STATUS_PENDING
);
352 static struct device
*create_device( struct directory
*root
, const struct unicode_str
*name
,
353 struct device_manager
*manager
, unsigned int attr
)
355 struct device
*device
;
357 if ((device
= create_named_object_dir( root
, name
, attr
, &device_ops
)))
359 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
361 /* initialize it if it didn't already exist */
362 device
->manager
= manager
;
363 list_add_tail( &manager
->devices
, &device
->entry
);
364 list_init( &device
->requests
);
365 if (!(device
->fd
= alloc_pseudo_fd( &device_fd_ops
, &device
->obj
, 0 )))
367 release_object( device
);
375 static void delete_device( struct device
*device
)
377 struct ioctl_call
*ioctl
, *next
;
379 if (!device
->manager
) return; /* already deleted */
381 /* terminate all pending requests */
382 LIST_FOR_EACH_ENTRY_SAFE( ioctl
, next
, &device
->requests
, struct ioctl_call
, dev_entry
)
384 list_remove( &ioctl
->mgr_entry
);
385 set_ioctl_result( ioctl
, STATUS_FILE_DELETED
, NULL
, 0 );
387 unlink_named_object( &device
->obj
);
388 list_remove( &device
->entry
);
389 device
->manager
= NULL
;
393 static void device_manager_dump( struct object
*obj
, int verbose
)
395 fprintf( stderr
, "Device manager\n" );
398 static int device_manager_signaled( struct object
*obj
, struct thread
*thread
)
400 struct device_manager
*manager
= (struct device_manager
*)obj
;
402 return !list_empty( &manager
->requests
);
405 static void device_manager_destroy( struct object
*obj
)
407 struct device_manager
*manager
= (struct device_manager
*)obj
;
410 while ((ptr
= list_head( &manager
->devices
)))
412 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
413 delete_device( device
);
417 static struct device_manager
*create_device_manager(void)
419 struct device_manager
*manager
;
421 if ((manager
= alloc_object( &device_manager_ops
)))
423 list_init( &manager
->devices
);
424 list_init( &manager
->requests
);
430 /* create a device manager */
431 DECL_HANDLER(create_device_manager
)
433 struct device_manager
*manager
= create_device_manager();
437 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
438 release_object( manager
);
443 /* create a device */
444 DECL_HANDLER(create_device
)
446 struct device
*device
;
447 struct unicode_str name
;
448 struct device_manager
*manager
;
449 struct directory
*root
= NULL
;
451 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
452 0, &device_manager_ops
)))
455 get_req_unicode_str( &name
);
456 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
458 release_object( manager
);
462 if ((device
= create_device( root
, &name
, manager
, req
->attributes
)))
464 device
->user_ptr
= req
->user_ptr
;
465 reply
->handle
= alloc_handle( current
->process
, device
, req
->access
, req
->attributes
);
466 release_object( device
);
469 if (root
) release_object( root
);
470 release_object( manager
);
474 /* delete a device */
475 DECL_HANDLER(delete_device
)
477 struct device
*device
;
479 if ((device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
481 delete_device( device
);
482 release_object( device
);
487 /* retrieve the next pending device ioctl request */
488 DECL_HANDLER(get_next_device_request
)
490 struct ioctl_call
*ioctl
;
491 struct device_manager
*manager
;
494 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
495 0, &device_manager_ops
)))
500 if ((ioctl
= (struct ioctl_call
*)get_handle_obj( current
->process
, req
->prev
,
501 0, &ioctl_call_ops
)))
503 set_ioctl_result( ioctl
, req
->status
, get_req_data(), get_req_data_size() );
504 close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
505 release_object( ioctl
);
510 if ((ptr
= list_head( &manager
->requests
)))
512 ioctl
= LIST_ENTRY( ptr
, struct ioctl_call
, mgr_entry
);
513 reply
->code
= ioctl
->code
;
514 reply
->user_ptr
= ioctl
->device
->user_ptr
;
515 reply
->in_size
= ioctl
->in_size
;
516 reply
->out_size
= ioctl
->out_size
;
517 if (ioctl
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
518 else if ((reply
->next
= alloc_handle( current
->process
, ioctl
, 0, 0 )))
520 set_reply_data_ptr( ioctl
->in_data
, ioctl
->in_size
);
521 ioctl
->in_data
= NULL
;
523 list_remove( &ioctl
->mgr_entry
);
524 list_init( &ioctl
->mgr_entry
);
527 else set_error( STATUS_PENDING
);
529 release_object( manager
);
533 /* retrieve results of an async ioctl */
534 DECL_HANDLER(get_ioctl_result
)
536 struct device
*device
;
537 struct ioctl_call
*ioctl
;
539 if (!(device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
542 if ((ioctl
= find_ioctl_call( device
, current
, req
->user_arg
)))
546 data_size_t size
= min( ioctl
->out_size
, get_reply_max_size() );
549 set_reply_data_ptr( ioctl
->out_data
, size
);
550 ioctl
->out_data
= NULL
;
553 set_error( ioctl
->status
);
554 list_remove( &ioctl
->dev_entry
);
555 release_object( ioctl
); /* no longer on the device queue */
557 release_object( device
);