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 void *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 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 ioctl_call_signaled, /* signaled */
64 no_satisfied, /* satisfied */
65 no_signal, /* signal */
66 no_get_fd, /* get_fd */
67 no_map_access, /* map_access */
68 no_lookup_name, /* lookup_name */
69 no_open_file, /* open_file */
70 no_close_handle, /* close_handle */
71 ioctl_call_destroy /* destroy */
77 struct object obj; /* object header */
78 struct list devices; /* list of devices */
79 struct list requests; /* list of pending ioctls across all devices */
82 static void device_manager_dump( struct object *obj, int verbose );
83 static int device_manager_signaled( struct object *obj, struct thread *thread );
84 static void device_manager_destroy( struct object *obj );
86 static const struct object_ops device_manager_ops =
88 sizeof(struct device_manager), /* size */
89 device_manager_dump, /* dump */
90 add_queue, /* add_queue */
91 remove_queue, /* remove_queue */
92 device_manager_signaled, /* signaled */
93 no_satisfied, /* satisfied */
94 no_signal, /* signal */
95 no_get_fd, /* get_fd */
96 no_map_access, /* map_access */
97 no_lookup_name, /* lookup_name */
98 no_open_file, /* open_file */
99 no_close_handle, /* close_handle */
100 device_manager_destroy /* destroy */
106 struct object obj; /* object header */
107 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
108 struct fd *fd; /* file descriptor for ioctl */
109 void *user_ptr; /* opaque ptr for client side */
110 struct list entry; /* entry in device manager list */
111 struct list requests; /* list of pending ioctl requests */
114 static void device_dump( struct object *obj, int verbose );
115 static struct fd *device_get_fd( struct object *obj );
116 static void device_destroy( struct object *obj );
117 static struct object *device_open_file( struct object *obj, unsigned int access,
118 unsigned int sharing, unsigned int options );
119 static enum server_fd_type device_get_fd_type( struct fd *fd );
120 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
121 const void *data, data_size_t size );
123 static const struct object_ops device_ops =
125 sizeof(struct device), /* size */
126 device_dump, /* dump */
127 no_add_queue, /* add_queue */
128 NULL, /* remove_queue */
130 no_satisfied, /* satisfied */
131 no_signal, /* signal */
132 device_get_fd, /* get_fd */
133 no_map_access, /* map_access */
134 no_lookup_name, /* lookup_name */
135 device_open_file, /* open_file */
136 no_close_handle, /* close_handle */
137 device_destroy /* destroy */
140 static const struct fd_ops device_fd_ops =
142 default_fd_get_poll_events, /* get_poll_events */
143 default_poll_event, /* poll_event */
144 no_flush, /* flush */
145 device_get_fd_type, /* get_fd_type */
146 device_ioctl, /* ioctl */
147 default_fd_queue_async, /* queue_async */
148 default_fd_reselect_async, /* reselect_async */
149 default_fd_cancel_async /* cancel_async */
153 static void ioctl_call_dump( struct object *obj, int verbose )
155 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
156 fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
159 static int ioctl_call_signaled( struct object *obj, struct thread *thread )
161 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
163 return !ioctl->device; /* device is cleared once the ioctl has completed */
166 static void ioctl_call_destroy( struct object *obj )
168 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
170 free( ioctl->in_data );
171 free( ioctl->out_data );
174 async_terminate( ioctl->async, STATUS_CANCELLED );
175 release_object( ioctl->async );
177 if (ioctl->device) release_object( ioctl->device );
178 release_object( ioctl->thread );
181 static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
182 const void *in_data, data_size_t in_size,
183 data_size_t out_size )
185 struct ioctl_call *ioctl;
187 if ((ioctl = alloc_object( &ioctl_call_ops )))
189 ioctl->device = (struct device *)grab_object( device );
192 ioctl->status = STATUS_PENDING;
193 ioctl->in_size = in_size;
194 ioctl->in_data = NULL;
195 ioctl->out_size = out_size;
196 ioctl->out_data = NULL;
198 if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
200 release_object( ioctl );
207 static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
208 const void *out_data, data_size_t out_size )
210 struct device *device = ioctl->device;
212 if (!device) return; /* already finished */
214 /* FIXME: handle the STATUS_PENDING case */
215 ioctl->status = status;
216 ioctl->out_size = min( ioctl->out_size, out_size );
217 if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
219 release_object( device );
220 ioctl->device = NULL;
223 async_terminate( ioctl->async, ioctl->out_size ? STATUS_ALERTED : status );
224 release_object( ioctl->async );
227 wake_up( &ioctl->obj, 0 );
231 static void device_dump( struct object *obj, int verbose )
233 struct device *device = (struct device *)obj;
235 fprintf( stderr, "Device " );
236 dump_object_name( &device->obj );
237 fputc( '\n', stderr );
240 static struct fd *device_get_fd( struct object *obj )
242 struct device *device = (struct device *)obj;
244 return (struct fd *)grab_object( device->fd );
247 static void device_destroy( struct object *obj )
249 struct device *device = (struct device *)obj;
250 struct ioctl_call *ioctl, *next;
252 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
254 list_remove( &ioctl->dev_entry );
255 release_object( ioctl ); /* no longer on the device queue */
257 if (device->fd) release_object( device->fd );
258 if (device->manager) list_remove( &device->entry );
261 static struct object *device_open_file( struct object *obj, unsigned int access,
262 unsigned int sharing, unsigned int options )
264 return grab_object( obj );
267 static enum server_fd_type device_get_fd_type( struct fd *fd )
269 return FD_TYPE_DEVICE;
272 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
273 const void *data, data_size_t size )
275 struct device *device = get_fd_user( fd );
276 struct ioctl_call *ioctl;
279 if (!device->manager) /* it has been deleted */
281 set_error( STATUS_FILE_DELETED );
285 if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
288 ioctl->thread = (struct thread *)grab_object( current );
289 ioctl->user_arg = async_data->arg;
291 if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
293 release_object( ioctl );
297 if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
299 close_handle( current->process, handle );
300 release_object( ioctl );
303 list_add_tail( &device->requests, &ioctl->dev_entry );
304 list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
305 if (list_head( &device->manager->requests ) == &ioctl->mgr_entry) /* first one */
306 wake_up( &device->manager->obj, 0 );
307 /* don't release ioctl since it is now queued in the device */
308 set_error( STATUS_PENDING );
312 static struct device *create_device( struct directory *root, const struct unicode_str *name,
313 struct device_manager *manager, unsigned int attr )
315 struct device *device;
317 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
319 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
321 /* initialize it if it didn't already exist */
322 device->manager = manager;
323 list_add_tail( &manager->devices, &device->entry );
324 list_init( &device->requests );
325 if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
327 release_object( device );
335 static void delete_device( struct device *device )
337 struct ioctl_call *ioctl;
339 if (!device->manager) return; /* already deleted */
341 /* terminate all pending requests */
342 LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
344 list_remove( &ioctl->mgr_entry );
345 set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
347 unlink_named_object( &device->obj );
348 list_remove( &device->entry );
349 device->manager = NULL;
353 static void device_manager_dump( struct object *obj, int verbose )
355 fprintf( stderr, "Device manager\n" );
358 static int device_manager_signaled( struct object *obj, struct thread *thread )
360 struct device_manager *manager = (struct device_manager *)obj;
362 return !list_empty( &manager->requests );
365 static void device_manager_destroy( struct object *obj )
367 struct device_manager *manager = (struct device_manager *)obj;
370 while ((ptr = list_head( &manager->devices )))
372 struct device *device = LIST_ENTRY( ptr, struct device, entry );
373 delete_device( device );
377 static struct device_manager *create_device_manager(void)
379 struct device_manager *manager;
381 if ((manager = alloc_object( &device_manager_ops )))
383 list_init( &manager->devices );
384 list_init( &manager->requests );
390 /* create a device manager */
391 DECL_HANDLER(create_device_manager)
393 struct device_manager *manager = create_device_manager();
397 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
398 release_object( manager );
403 /* create a device */
404 DECL_HANDLER(create_device)
406 struct device *device;
407 struct unicode_str name;
408 struct device_manager *manager;
409 struct directory *root = NULL;
411 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
412 0, &device_manager_ops )))
415 get_req_unicode_str( &name );
416 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
418 release_object( manager );
422 if ((device = create_device( root, &name, manager, req->attributes )))
424 device->user_ptr = req->user_ptr;
425 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
426 release_object( device );
429 if (root) release_object( root );
430 release_object( manager );
434 /* delete a device */
435 DECL_HANDLER(delete_device)
437 struct device *device;
439 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
441 delete_device( device );
442 release_object( device );
447 /* retrieve the next pending device ioctl request */
448 DECL_HANDLER(get_next_device_request)
450 struct ioctl_call *ioctl;
451 struct device_manager *manager;
454 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
455 0, &device_manager_ops )))
460 if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
461 0, &ioctl_call_ops )))
463 set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
464 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
465 release_object( ioctl );
470 if ((ptr = list_head( &manager->requests )))
472 ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
473 reply->code = ioctl->code;
474 reply->user_ptr = ioctl->device->user_ptr;
475 reply->in_size = ioctl->in_size;
476 reply->out_size = ioctl->out_size;
477 if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
478 else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
480 set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
481 ioctl->in_data = NULL;
483 list_remove( &ioctl->mgr_entry );
484 list_init( &ioctl->mgr_entry );
487 else set_error( STATUS_PENDING );
489 release_object( manager );