server: Add a common structure to store irp parameters.
[wine.git] / server / device.c
blob2707faff55cc61fc02a90f29bc6d536b7b7ac492
1 /*
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
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "ddk/wdm.h"
32 #include "object.h"
33 #include "file.h"
34 #include "handle.h"
35 #include "request.h"
36 #include "process.h"
38 struct irp_call
40 struct object obj; /* object header */
41 struct list dev_entry; /* entry in device queue */
42 struct list mgr_entry; /* entry in manager queue */
43 struct device *device; /* device containing this irp */
44 struct thread *thread; /* thread that queued the irp */
45 client_ptr_t user_arg; /* user arg used to identify the request */
46 struct async *async; /* pending async op */
47 unsigned int status; /* resulting status (or STATUS_PENDING) */
48 irp_params_t params; /* irp parameters */
49 data_size_t result; /* size of result (input or output depending on the type) */
50 data_size_t in_size; /* size of input data */
51 void *in_data; /* input data */
52 data_size_t out_size; /* size of output data */
53 void *out_data; /* output data */
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_open_file, /* open_file */
76 no_close_handle, /* close_handle */
77 irp_call_destroy /* destroy */
81 struct device_manager
83 struct object obj; /* object header */
84 struct list devices; /* list of devices */
85 struct list requests; /* list of pending irps across all devices */
88 static void device_manager_dump( struct object *obj, int verbose );
89 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
90 static void device_manager_destroy( struct object *obj );
92 static const struct object_ops device_manager_ops =
94 sizeof(struct device_manager), /* size */
95 device_manager_dump, /* dump */
96 no_get_type, /* get_type */
97 add_queue, /* add_queue */
98 remove_queue, /* remove_queue */
99 device_manager_signaled, /* signaled */
100 no_satisfied, /* satisfied */
101 no_signal, /* signal */
102 no_get_fd, /* get_fd */
103 no_map_access, /* map_access */
104 default_get_sd, /* get_sd */
105 default_set_sd, /* set_sd */
106 no_lookup_name, /* lookup_name */
107 no_open_file, /* open_file */
108 no_close_handle, /* close_handle */
109 device_manager_destroy /* destroy */
113 struct device
115 struct object obj; /* object header */
116 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
117 struct fd *fd; /* file descriptor for irp */
118 client_ptr_t user_ptr; /* opaque ptr for client side */
119 struct list entry; /* entry in device manager list */
120 struct list requests; /* list of pending irp requests */
123 static void device_dump( struct object *obj, int verbose );
124 static struct object_type *device_get_type( struct object *obj );
125 static struct fd *device_get_fd( struct object *obj );
126 static void device_destroy( struct object *obj );
127 static struct object *device_open_file( struct object *obj, unsigned int access,
128 unsigned int sharing, unsigned int options );
129 static enum server_fd_type device_get_fd_type( struct fd *fd );
130 static obj_handle_t device_read( struct fd *fd, const async_data_t *async_data, int blocking,
131 file_pos_t pos );
132 static obj_handle_t device_write( struct fd *fd, const async_data_t *async_data, int blocking,
133 file_pos_t pos, data_size_t *written );
134 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
135 int blocking );
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 */
144 NULL, /* signaled */
145 no_satisfied, /* satisfied */
146 no_signal, /* signal */
147 device_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 device_open_file, /* open_file */
153 no_close_handle, /* close_handle */
154 device_destroy /* destroy */
157 static const struct fd_ops device_fd_ops =
159 default_fd_get_poll_events, /* get_poll_events */
160 default_poll_event, /* poll_event */
161 device_get_fd_type, /* get_fd_type */
162 device_read, /* read */
163 device_write, /* write */
164 no_fd_flush, /* flush */
165 device_ioctl, /* ioctl */
166 default_fd_queue_async, /* queue_async */
167 default_fd_reselect_async, /* reselect_async */
168 default_fd_cancel_async /* cancel_async */
172 static void irp_call_dump( struct object *obj, int verbose )
174 struct irp_call *irp = (struct irp_call *)obj;
175 fprintf( stderr, "IRP call device=%p\n", irp->device );
178 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
180 struct irp_call *irp = (struct irp_call *)obj;
182 return !irp->device; /* device is cleared once the irp has completed */
185 static void irp_call_destroy( struct object *obj )
187 struct irp_call *irp = (struct irp_call *)obj;
189 free( irp->in_data );
190 free( irp->out_data );
191 if (irp->async)
193 async_terminate( irp->async, STATUS_CANCELLED );
194 release_object( irp->async );
196 if (irp->device) release_object( irp->device );
197 release_object( irp->thread );
200 static struct irp_call *create_irp( struct device *device, const irp_params_t *params,
201 const void *in_data, data_size_t in_size, data_size_t out_size )
203 struct irp_call *irp;
205 if (!device->manager) /* it has been deleted */
207 set_error( STATUS_FILE_DELETED );
208 return NULL;
211 if ((irp = alloc_object( &irp_call_ops )))
213 irp->device = (struct device *)grab_object( device );
214 irp->async = NULL;
215 irp->params = *params;
216 irp->status = STATUS_PENDING;
217 irp->result = 0;
218 irp->in_size = in_size;
219 irp->in_data = NULL;
220 irp->out_size = out_size;
221 irp->out_data = NULL;
223 if (irp->in_size && !(irp->in_data = memdup( in_data, in_size )))
225 release_object( irp );
226 irp = NULL;
229 return irp;
232 static void set_irp_result( struct irp_call *irp, unsigned int status,
233 const void *out_data, data_size_t out_size, data_size_t result )
235 struct device *device = irp->device;
237 if (!device) return; /* already finished */
239 /* FIXME: handle the STATUS_PENDING case */
240 irp->status = status;
241 irp->result = result;
242 irp->out_size = min( irp->out_size, out_size );
243 if (irp->out_size && !(irp->out_data = memdup( out_data, irp->out_size )))
244 irp->out_size = 0;
245 release_object( device );
246 irp->device = NULL;
247 if (irp->async)
249 if (result) status = STATUS_ALERTED;
250 async_terminate( irp->async, status );
251 release_object( irp->async );
252 irp->async = NULL;
254 wake_up( &irp->obj, 0 );
256 if (status != STATUS_ALERTED)
258 /* remove it from the device queue */
259 /* (for STATUS_ALERTED this will be done in get_irp_result) */
260 list_remove( &irp->dev_entry );
261 release_object( irp ); /* no longer on the device queue */
266 static void device_dump( struct object *obj, int verbose )
268 struct device *device = (struct device *)obj;
270 fprintf( stderr, "Device " );
271 dump_object_name( &device->obj );
272 fputc( '\n', stderr );
275 static struct object_type *device_get_type( struct object *obj )
277 static const WCHAR name[] = {'D','e','v','i','c','e'};
278 static const struct unicode_str str = { name, sizeof(name) };
279 return get_object_type( &str );
282 static struct fd *device_get_fd( struct object *obj )
284 struct device *device = (struct device *)obj;
286 return (struct fd *)grab_object( device->fd );
289 static void device_destroy( struct object *obj )
291 struct device *device = (struct device *)obj;
292 struct irp_call *irp, *next;
294 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &device->requests, struct irp_call, dev_entry )
296 list_remove( &irp->dev_entry );
297 release_object( irp ); /* no longer on the device queue */
299 if (device->fd) release_object( device->fd );
300 if (device->manager) list_remove( &device->entry );
303 static struct object *device_open_file( struct object *obj, unsigned int access,
304 unsigned int sharing, unsigned int options )
306 return grab_object( obj );
309 static enum server_fd_type device_get_fd_type( struct fd *fd )
311 return FD_TYPE_DEVICE;
314 static struct irp_call *find_irp_call( struct device *device, struct thread *thread,
315 client_ptr_t user_arg )
317 struct irp_call *irp;
319 LIST_FOR_EACH_ENTRY( irp, &device->requests, struct irp_call, dev_entry )
320 if (irp->thread == thread && irp->user_arg == user_arg) return irp;
322 set_error( STATUS_INVALID_PARAMETER );
323 return NULL;
326 /* queue an irp to the device */
327 static obj_handle_t queue_irp( struct device *device, struct irp_call *irp,
328 const async_data_t *async_data, int blocking )
330 obj_handle_t handle = 0;
332 if (blocking && !(handle = alloc_handle( current->process, irp, SYNCHRONIZE, 0 ))) return 0;
334 if (!(irp->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT )))
336 if (handle) close_handle( current->process, handle );
337 return 0;
339 irp->thread = (struct thread *)grab_object( current );
340 irp->user_arg = async_data->arg;
341 grab_object( irp ); /* grab reference for queued irp */
343 list_add_tail( &device->requests, &irp->dev_entry );
344 list_add_tail( &device->manager->requests, &irp->mgr_entry );
345 if (list_head( &device->manager->requests ) == &irp->mgr_entry) /* first one */
346 wake_up( &device->manager->obj, 0 );
347 set_error( STATUS_PENDING );
348 return handle;
351 static obj_handle_t device_read( struct fd *fd, const async_data_t *async_data, int blocking,
352 file_pos_t pos )
354 struct device *device = get_fd_user( fd );
355 struct irp_call *irp;
356 obj_handle_t handle;
357 irp_params_t params;
359 params.major = IRP_MJ_READ;
360 params.read.key = 0;
361 params.read.pos = pos;
363 irp = create_irp( device, &params, NULL, 0, get_reply_max_size() );
364 if (!irp) return 0;
366 handle = queue_irp( device, irp, async_data, blocking );
367 release_object( irp );
368 return handle;
371 static obj_handle_t device_write( struct fd *fd, const async_data_t *async_data, int blocking,
372 file_pos_t pos, data_size_t *written )
374 struct device *device = get_fd_user( fd );
375 struct irp_call *irp;
376 obj_handle_t handle;
377 irp_params_t params;
379 params.major = IRP_MJ_WRITE;
380 params.write.key = 0;
381 params.write.pos = pos;
383 irp = create_irp( device, &params, get_req_data(), get_req_data_size(), 0 );
384 if (!irp) return 0;
386 handle = queue_irp( device, irp, async_data, blocking );
387 release_object( irp );
388 return handle;
391 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
392 int blocking )
394 struct device *device = get_fd_user( fd );
395 struct irp_call *irp;
396 obj_handle_t handle;
397 irp_params_t params;
399 params.major = IRP_MJ_DEVICE_CONTROL;
400 params.ioctl.code = code;
402 irp = create_irp( device, &params, get_req_data(), get_req_data_size(),
403 get_reply_max_size() );
404 if (!irp) return 0;
406 handle = queue_irp( device, irp, async_data, blocking );
407 release_object( irp );
408 return handle;
411 static struct device *create_device( struct directory *root, const struct unicode_str *name,
412 struct device_manager *manager, unsigned int attr )
414 struct device *device;
416 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
418 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
420 /* initialize it if it didn't already exist */
421 device->manager = manager;
422 list_add_tail( &manager->devices, &device->entry );
423 list_init( &device->requests );
424 if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
426 release_object( device );
427 device = NULL;
431 return device;
434 static void delete_device( struct device *device )
436 struct irp_call *irp, *next;
438 if (!device->manager) return; /* already deleted */
440 /* terminate all pending requests */
441 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &device->requests, struct irp_call, dev_entry )
443 list_remove( &irp->mgr_entry );
444 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
446 unlink_named_object( &device->obj );
447 list_remove( &device->entry );
448 device->manager = NULL;
452 static void device_manager_dump( struct object *obj, int verbose )
454 fprintf( stderr, "Device manager\n" );
457 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
459 struct device_manager *manager = (struct device_manager *)obj;
461 return !list_empty( &manager->requests );
464 static void device_manager_destroy( struct object *obj )
466 struct device_manager *manager = (struct device_manager *)obj;
467 struct list *ptr;
469 while ((ptr = list_head( &manager->devices )))
471 struct device *device = LIST_ENTRY( ptr, struct device, entry );
472 delete_device( device );
476 static struct device_manager *create_device_manager(void)
478 struct device_manager *manager;
480 if ((manager = alloc_object( &device_manager_ops )))
482 list_init( &manager->devices );
483 list_init( &manager->requests );
485 return manager;
489 /* create a device manager */
490 DECL_HANDLER(create_device_manager)
492 struct device_manager *manager = create_device_manager();
494 if (manager)
496 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
497 release_object( manager );
502 /* create a device */
503 DECL_HANDLER(create_device)
505 struct device *device;
506 struct unicode_str name;
507 struct device_manager *manager;
508 struct directory *root = NULL;
510 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
511 0, &device_manager_ops )))
512 return;
514 get_req_unicode_str( &name );
515 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
517 release_object( manager );
518 return;
521 if ((device = create_device( root, &name, manager, req->attributes )))
523 device->user_ptr = req->user_ptr;
524 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
525 release_object( device );
528 if (root) release_object( root );
529 release_object( manager );
533 /* delete a device */
534 DECL_HANDLER(delete_device)
536 struct device *device;
538 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
540 delete_device( device );
541 release_object( device );
546 /* retrieve the next pending device irp request */
547 DECL_HANDLER(get_next_device_request)
549 struct irp_call *irp;
550 struct device_manager *manager;
551 struct list *ptr;
553 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
554 0, &device_manager_ops )))
555 return;
557 if (req->prev)
559 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev,
560 0, &irp_call_ops )))
562 set_irp_result( irp, req->status, NULL, 0, 0 );
563 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
564 release_object( irp );
566 clear_error();
569 if ((ptr = list_head( &manager->requests )))
571 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
572 reply->params = irp->params;
573 reply->user_ptr = irp->device->user_ptr;
574 reply->client_pid = get_process_id( irp->thread->process );
575 reply->client_tid = get_thread_id( irp->thread );
576 reply->in_size = irp->in_size;
577 reply->out_size = irp->out_size;
578 if (irp->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
579 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
581 set_reply_data_ptr( irp->in_data, irp->in_size );
582 irp->in_data = NULL;
583 irp->in_size = 0;
584 list_remove( &irp->mgr_entry );
585 list_init( &irp->mgr_entry );
588 else set_error( STATUS_PENDING );
590 release_object( manager );
594 /* store results of an async irp */
595 DECL_HANDLER(set_irp_result)
597 struct irp_call *irp;
598 struct device_manager *manager;
600 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
601 0, &device_manager_ops )))
602 return;
604 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
606 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
607 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
608 release_object( irp );
610 release_object( manager );
614 /* retrieve results of an async irp */
615 DECL_HANDLER(get_irp_result)
617 struct device *device;
618 struct irp_call *irp;
620 if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
621 return;
623 if ((irp = find_irp_call( device, current, req->user_arg )))
625 if (irp->out_data)
627 data_size_t size = min( irp->out_size, get_reply_max_size() );
628 if (size)
630 set_reply_data_ptr( irp->out_data, size );
631 irp->out_data = NULL;
634 reply->size = irp->result;
635 set_error( irp->status );
636 list_remove( &irp->dev_entry );
637 release_object( irp ); /* no longer on the device queue */
639 release_object( device );