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 default_get_sd
, /* get_sd */
69 default_set_sd
, /* set_sd */
70 no_lookup_name
, /* lookup_name */
71 no_open_file
, /* open_file */
72 no_close_handle
, /* close_handle */
73 ioctl_call_destroy
/* destroy */
79 struct object obj
; /* object header */
80 struct list devices
; /* list of devices */
81 struct list requests
; /* list of pending ioctls across all devices */
84 static void device_manager_dump( struct object
*obj
, int verbose
);
85 static int device_manager_signaled( struct object
*obj
, struct thread
*thread
);
86 static void device_manager_destroy( struct object
*obj
);
88 static const struct object_ops device_manager_ops
=
90 sizeof(struct device_manager
), /* size */
91 device_manager_dump
, /* dump */
92 add_queue
, /* add_queue */
93 remove_queue
, /* remove_queue */
94 device_manager_signaled
, /* signaled */
95 no_satisfied
, /* satisfied */
96 no_signal
, /* signal */
97 no_get_fd
, /* get_fd */
98 no_map_access
, /* map_access */
99 default_get_sd
, /* get_sd */
100 default_set_sd
, /* set_sd */
101 no_lookup_name
, /* lookup_name */
102 no_open_file
, /* open_file */
103 no_close_handle
, /* close_handle */
104 device_manager_destroy
/* destroy */
110 struct object obj
; /* object header */
111 struct device_manager
*manager
; /* manager for this device (or NULL if deleted) */
112 struct fd
*fd
; /* file descriptor for ioctl */
113 void *user_ptr
; /* opaque ptr for client side */
114 struct list entry
; /* entry in device manager list */
115 struct list requests
; /* list of pending ioctl requests */
118 static void device_dump( struct object
*obj
, int verbose
);
119 static struct fd
*device_get_fd( struct object
*obj
);
120 static void device_destroy( struct object
*obj
);
121 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
122 unsigned int sharing
, unsigned int options
);
123 static enum server_fd_type
device_get_fd_type( struct fd
*fd
);
124 static obj_handle_t
device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
125 const void *data
, data_size_t size
);
127 static const struct object_ops device_ops
=
129 sizeof(struct device
), /* size */
130 device_dump
, /* dump */
131 no_add_queue
, /* add_queue */
132 NULL
, /* remove_queue */
134 no_satisfied
, /* satisfied */
135 no_signal
, /* signal */
136 device_get_fd
, /* get_fd */
137 default_fd_map_access
, /* map_access */
138 default_get_sd
, /* get_sd */
139 default_set_sd
, /* set_sd */
140 no_lookup_name
, /* lookup_name */
141 device_open_file
, /* open_file */
142 no_close_handle
, /* close_handle */
143 device_destroy
/* destroy */
146 static const struct fd_ops device_fd_ops
=
148 default_fd_get_poll_events
, /* get_poll_events */
149 default_poll_event
, /* poll_event */
150 no_flush
, /* flush */
151 device_get_fd_type
, /* get_fd_type */
152 device_ioctl
, /* ioctl */
153 default_fd_queue_async
, /* queue_async */
154 default_fd_reselect_async
, /* reselect_async */
155 default_fd_cancel_async
/* cancel_async */
159 static void ioctl_call_dump( struct object
*obj
, int verbose
)
161 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
162 fprintf( stderr
, "Ioctl call code=%08x device=%p\n", ioctl
->code
, ioctl
->device
);
165 static int ioctl_call_signaled( struct object
*obj
, struct thread
*thread
)
167 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
169 return !ioctl
->device
; /* device is cleared once the ioctl has completed */
172 static void ioctl_call_destroy( struct object
*obj
)
174 struct ioctl_call
*ioctl
= (struct ioctl_call
*)obj
;
176 free( ioctl
->in_data
);
177 free( ioctl
->out_data
);
180 async_terminate( ioctl
->async
, STATUS_CANCELLED
);
181 release_object( ioctl
->async
);
183 if (ioctl
->device
) release_object( ioctl
->device
);
184 release_object( ioctl
->thread
);
187 static struct ioctl_call
*create_ioctl( struct device
*device
, ioctl_code_t code
,
188 const void *in_data
, data_size_t in_size
,
189 data_size_t out_size
)
191 struct ioctl_call
*ioctl
;
193 if ((ioctl
= alloc_object( &ioctl_call_ops
)))
195 ioctl
->device
= (struct device
*)grab_object( device
);
198 ioctl
->status
= STATUS_PENDING
;
199 ioctl
->in_size
= in_size
;
200 ioctl
->in_data
= NULL
;
201 ioctl
->out_size
= out_size
;
202 ioctl
->out_data
= NULL
;
204 if (ioctl
->in_size
&& !(ioctl
->in_data
= memdup( in_data
, in_size
)))
206 release_object( ioctl
);
213 static void set_ioctl_result( struct ioctl_call
*ioctl
, unsigned int status
,
214 const void *out_data
, data_size_t out_size
)
216 struct device
*device
= ioctl
->device
;
218 if (!device
) return; /* already finished */
220 /* FIXME: handle the STATUS_PENDING case */
221 ioctl
->status
= status
;
222 ioctl
->out_size
= min( ioctl
->out_size
, out_size
);
223 if (ioctl
->out_size
&& !(ioctl
->out_data
= memdup( out_data
, ioctl
->out_size
)))
225 release_object( device
);
226 ioctl
->device
= NULL
;
229 async_terminate( ioctl
->async
, ioctl
->out_size
? STATUS_ALERTED
: status
);
230 release_object( ioctl
->async
);
233 wake_up( &ioctl
->obj
, 0 );
237 static void device_dump( struct object
*obj
, int verbose
)
239 struct device
*device
= (struct device
*)obj
;
241 fprintf( stderr
, "Device " );
242 dump_object_name( &device
->obj
);
243 fputc( '\n', stderr
);
246 static struct fd
*device_get_fd( struct object
*obj
)
248 struct device
*device
= (struct device
*)obj
;
250 return (struct fd
*)grab_object( device
->fd
);
253 static void device_destroy( struct object
*obj
)
255 struct device
*device
= (struct device
*)obj
;
256 struct ioctl_call
*ioctl
, *next
;
258 LIST_FOR_EACH_ENTRY_SAFE( ioctl
, next
, &device
->requests
, struct ioctl_call
, dev_entry
)
260 list_remove( &ioctl
->dev_entry
);
261 release_object( ioctl
); /* no longer on the device queue */
263 if (device
->fd
) release_object( device
->fd
);
264 if (device
->manager
) list_remove( &device
->entry
);
267 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
268 unsigned int sharing
, unsigned int options
)
270 return grab_object( obj
);
273 static enum server_fd_type
device_get_fd_type( struct fd
*fd
)
275 return FD_TYPE_DEVICE
;
278 static struct ioctl_call
*find_ioctl_call( struct device
*device
, struct thread
*thread
,
281 struct ioctl_call
*ioctl
;
283 LIST_FOR_EACH_ENTRY( ioctl
, &device
->requests
, struct ioctl_call
, dev_entry
)
284 if (ioctl
->thread
== thread
&& ioctl
->user_arg
== user_arg
) return ioctl
;
286 set_error( STATUS_INVALID_PARAMETER
);
290 static obj_handle_t
device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
291 const void *data
, data_size_t size
)
293 struct device
*device
= get_fd_user( fd
);
294 struct ioctl_call
*ioctl
;
297 if (!device
->manager
) /* it has been deleted */
299 set_error( STATUS_FILE_DELETED
);
303 if (!(ioctl
= create_ioctl( device
, code
, data
, size
, get_reply_max_size() )))
306 ioctl
->thread
= (struct thread
*)grab_object( current
);
307 ioctl
->user_arg
= async_data
->arg
;
309 if (!(handle
= alloc_handle( current
->process
, ioctl
, SYNCHRONIZE
, 0 )))
311 release_object( ioctl
);
315 if (!(ioctl
->async
= fd_queue_async( device
->fd
, async_data
, ASYNC_TYPE_WAIT
, 0 )))
317 close_handle( current
->process
, handle
);
318 release_object( ioctl
);
321 list_add_tail( &device
->requests
, &ioctl
->dev_entry
);
322 list_add_tail( &device
->manager
->requests
, &ioctl
->mgr_entry
);
323 if (list_head( &device
->manager
->requests
) == &ioctl
->mgr_entry
) /* first one */
324 wake_up( &device
->manager
->obj
, 0 );
325 /* don't release ioctl since it is now queued in the device */
326 set_error( STATUS_PENDING
);
330 static struct device
*create_device( struct directory
*root
, const struct unicode_str
*name
,
331 struct device_manager
*manager
, unsigned int attr
)
333 struct device
*device
;
335 if ((device
= create_named_object_dir( root
, name
, attr
, &device_ops
)))
337 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
339 /* initialize it if it didn't already exist */
340 device
->manager
= manager
;
341 list_add_tail( &manager
->devices
, &device
->entry
);
342 list_init( &device
->requests
);
343 if (!(device
->fd
= alloc_pseudo_fd( &device_fd_ops
, &device
->obj
, 0 )))
345 release_object( device
);
353 static void delete_device( struct device
*device
)
355 struct ioctl_call
*ioctl
;
357 if (!device
->manager
) return; /* already deleted */
359 /* terminate all pending requests */
360 LIST_FOR_EACH_ENTRY( ioctl
, &device
->requests
, struct ioctl_call
, dev_entry
)
362 list_remove( &ioctl
->mgr_entry
);
363 set_ioctl_result( ioctl
, STATUS_FILE_DELETED
, NULL
, 0 );
365 unlink_named_object( &device
->obj
);
366 list_remove( &device
->entry
);
367 device
->manager
= NULL
;
371 static void device_manager_dump( struct object
*obj
, int verbose
)
373 fprintf( stderr
, "Device manager\n" );
376 static int device_manager_signaled( struct object
*obj
, struct thread
*thread
)
378 struct device_manager
*manager
= (struct device_manager
*)obj
;
380 return !list_empty( &manager
->requests
);
383 static void device_manager_destroy( struct object
*obj
)
385 struct device_manager
*manager
= (struct device_manager
*)obj
;
388 while ((ptr
= list_head( &manager
->devices
)))
390 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
391 delete_device( device
);
395 static struct device_manager
*create_device_manager(void)
397 struct device_manager
*manager
;
399 if ((manager
= alloc_object( &device_manager_ops
)))
401 list_init( &manager
->devices
);
402 list_init( &manager
->requests
);
408 /* create a device manager */
409 DECL_HANDLER(create_device_manager
)
411 struct device_manager
*manager
= create_device_manager();
415 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
416 release_object( manager
);
421 /* create a device */
422 DECL_HANDLER(create_device
)
424 struct device
*device
;
425 struct unicode_str name
;
426 struct device_manager
*manager
;
427 struct directory
*root
= NULL
;
429 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
430 0, &device_manager_ops
)))
433 get_req_unicode_str( &name
);
434 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
436 release_object( manager
);
440 if ((device
= create_device( root
, &name
, manager
, req
->attributes
)))
442 device
->user_ptr
= req
->user_ptr
;
443 reply
->handle
= alloc_handle( current
->process
, device
, req
->access
, req
->attributes
);
444 release_object( device
);
447 if (root
) release_object( root
);
448 release_object( manager
);
452 /* delete a device */
453 DECL_HANDLER(delete_device
)
455 struct device
*device
;
457 if ((device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
459 delete_device( device
);
460 release_object( device
);
465 /* retrieve the next pending device ioctl request */
466 DECL_HANDLER(get_next_device_request
)
468 struct ioctl_call
*ioctl
;
469 struct device_manager
*manager
;
472 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
473 0, &device_manager_ops
)))
478 if ((ioctl
= (struct ioctl_call
*)get_handle_obj( current
->process
, req
->prev
,
479 0, &ioctl_call_ops
)))
481 set_ioctl_result( ioctl
, req
->status
, get_req_data(), get_req_data_size() );
482 close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
483 release_object( ioctl
);
488 if ((ptr
= list_head( &manager
->requests
)))
490 ioctl
= LIST_ENTRY( ptr
, struct ioctl_call
, mgr_entry
);
491 reply
->code
= ioctl
->code
;
492 reply
->user_ptr
= ioctl
->device
->user_ptr
;
493 reply
->in_size
= ioctl
->in_size
;
494 reply
->out_size
= ioctl
->out_size
;
495 if (ioctl
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
496 else if ((reply
->next
= alloc_handle( current
->process
, ioctl
, 0, 0 )))
498 set_reply_data_ptr( ioctl
->in_data
, ioctl
->in_size
);
499 ioctl
->in_data
= NULL
;
501 list_remove( &ioctl
->mgr_entry
);
502 list_init( &ioctl
->mgr_entry
);
505 else set_error( STATUS_PENDING
);
507 release_object( manager
);
511 /* retrieve results of an async ioctl */
512 DECL_HANDLER(get_ioctl_result
)
514 struct device
*device
;
515 struct ioctl_call
*ioctl
;
517 if (!(device
= (struct device
*)get_handle_obj( current
->process
, req
->handle
, 0, &device_ops
)))
520 if ((ioctl
= find_ioctl_call( device
, current
, req
->user_arg
)))
524 data_size_t size
= min( ioctl
->out_size
, get_reply_max_size() );
527 set_reply_data_ptr( ioctl
->out_data
, size
);
528 ioctl
->out_data
= NULL
;
531 set_error( ioctl
->status
);
532 list_remove( &ioctl
->dev_entry
);
533 release_object( ioctl
); /* no longer on the device queue */
535 release_object( device
);