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 struct ioctl_call
*find_ioctl_call( struct device
*device
, struct thread
*thread
,
275 struct ioctl_call
*ioctl
;
277 LIST_FOR_EACH_ENTRY( ioctl
, &device
->requests
, struct ioctl_call
, dev_entry
)
278 if (ioctl
->thread
== thread
&& ioctl
->user_arg
== user_arg
) return ioctl
;
280 set_error( STATUS_INVALID_PARAMETER
);
284 static obj_handle_t
device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
285 const void *data
, data_size_t size
)
287 struct device
*device
= get_fd_user( fd
);
288 struct ioctl_call
*ioctl
;
291 if (!device
->manager
) /* it has been deleted */
293 set_error( STATUS_FILE_DELETED
);
297 if (!(ioctl
= create_ioctl( device
, code
, data
, size
, get_reply_max_size() )))
300 ioctl
->thread
= (struct thread
*)grab_object( current
);
301 ioctl
->user_arg
= async_data
->arg
;
303 if (!(handle
= alloc_handle( current
->process
, ioctl
, SYNCHRONIZE
, 0 )))
305 release_object( ioctl
);
309 if (!(ioctl
->async
= fd_queue_async( device
->fd
, async_data
, ASYNC_TYPE_WAIT
, 0 )))
311 close_handle( current
->process
, handle
);
312 release_object( ioctl
);
315 list_add_tail( &device
->requests
, &ioctl
->dev_entry
);
316 list_add_tail( &device
->manager
->requests
, &ioctl
->mgr_entry
);
317 if (list_head( &device
->manager
->requests
) == &ioctl
->mgr_entry
) /* first one */
318 wake_up( &device
->manager
->obj
, 0 );
319 /* don't release ioctl since it is now queued in the device */
320 set_error( STATUS_PENDING
);
324 static struct device
*create_device( struct directory
*root
, const struct unicode_str
*name
,
325 struct device_manager
*manager
, unsigned int attr
)
327 struct device
*device
;
329 if ((device
= create_named_object_dir( root
, name
, attr
, &device_ops
)))
331 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
333 /* initialize it if it didn't already exist */
334 device
->manager
= manager
;
335 list_add_tail( &manager
->devices
, &device
->entry
);
336 list_init( &device
->requests
);
337 if (!(device
->fd
= alloc_pseudo_fd( &device_fd_ops
, &device
->obj
, 0 )))
339 release_object( device
);
347 static void delete_device( struct device
*device
)
349 struct ioctl_call
*ioctl
;
351 if (!device
->manager
) return; /* already deleted */
353 /* terminate all pending requests */
354 LIST_FOR_EACH_ENTRY( ioctl
, &device
->requests
, struct ioctl_call
, dev_entry
)
356 list_remove( &ioctl
->mgr_entry
);
357 set_ioctl_result( ioctl
, STATUS_FILE_DELETED
, NULL
, 0 );
359 unlink_named_object( &device
->obj
);
360 list_remove( &device
->entry
);
361 device
->manager
= NULL
;
365 static void device_manager_dump( struct object
*obj
, int verbose
)
367 fprintf( stderr
, "Device manager\n" );
370 static int device_manager_signaled( struct object
*obj
, struct thread
*thread
)
372 struct device_manager
*manager
= (struct device_manager
*)obj
;
374 return !list_empty( &manager
->requests
);
377 static void device_manager_destroy( struct object
*obj
)
379 struct device_manager
*manager
= (struct device_manager
*)obj
;
382 while ((ptr
= list_head( &manager
->devices
)))
384 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
385 delete_device( device
);
389 static struct device_manager
*create_device_manager(void)
391 struct device_manager
*manager
;
393 if ((manager
= alloc_object( &device_manager_ops
)))
395 list_init( &manager
->devices
);
396 list_init( &manager
->requests
);
402 /* create a device manager */
403 DECL_HANDLER(create_device_manager
)
405 struct device_manager
*manager
= create_device_manager();
409 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
410 release_object( manager
);
415 /* create a device */
416 DECL_HANDLER(create_device
)
418 struct device
*device
;
419 struct unicode_str name
;
420 struct device_manager
*manager
;
421 struct directory
*root
= NULL
;
423 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
424 0, &device_manager_ops
)))
427 get_req_unicode_str( &name
);
428 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
430 release_object( manager
);
434 if ((device
= create_device( root
, &name
, manager
, req
->attributes
)))
436 device
->user_ptr
= req
->user_ptr
;
437 reply
->handle
= alloc_handle( current
->process
, device
, req
->access
, req
->attributes
);
438 release_object( device
);
441 if (root
) release_object( root
);
442 release_object( manager
);
446 /* delete a device */
447 DECL_HANDLER(delete_device
)
449 struct device
*device
;
451 if ((device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
453 delete_device( device
);
454 release_object( device
);
459 /* retrieve the next pending device ioctl request */
460 DECL_HANDLER(get_next_device_request
)
462 struct ioctl_call
*ioctl
;
463 struct device_manager
*manager
;
466 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
467 0, &device_manager_ops
)))
472 if ((ioctl
= (struct ioctl_call
*)get_handle_obj( current
->process
, req
->prev
,
473 0, &ioctl_call_ops
)))
475 set_ioctl_result( ioctl
, req
->status
, get_req_data(), get_req_data_size() );
476 close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
477 release_object( ioctl
);
482 if ((ptr
= list_head( &manager
->requests
)))
484 ioctl
= LIST_ENTRY( ptr
, struct ioctl_call
, mgr_entry
);
485 reply
->code
= ioctl
->code
;
486 reply
->user_ptr
= ioctl
->device
->user_ptr
;
487 reply
->in_size
= ioctl
->in_size
;
488 reply
->out_size
= ioctl
->out_size
;
489 if (ioctl
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
490 else if ((reply
->next
= alloc_handle( current
->process
, ioctl
, 0, 0 )))
492 set_reply_data_ptr( ioctl
->in_data
, ioctl
->in_size
);
493 ioctl
->in_data
= NULL
;
495 list_remove( &ioctl
->mgr_entry
);
496 list_init( &ioctl
->mgr_entry
);
499 else set_error( STATUS_PENDING
);
501 release_object( manager
);
505 /* retrieve results of an async ioctl */
506 DECL_HANDLER(get_ioctl_result
)
508 struct device
*device
;
509 struct ioctl_call
*ioctl
;
511 if (!(device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
514 if ((ioctl
= find_ioctl_call( device
, current
, req
->user_arg
)))
518 data_size_t size
= min( ioctl
->out_size
, get_reply_max_size() );
521 set_reply_data_ptr( ioctl
->out_data
, size
);
522 ioctl
->out_data
= NULL
;
525 set_error( ioctl
->status
);
526 list_remove( &ioctl
->dev_entry
);
527 release_object( ioctl
); /* no longer on the device queue */
529 release_object( device
);