server: Implement read and write requests for device files.
[wine.git] / server / device.c
blob353b9a53d778180d5fc116b32ff2f4a2bc6a68d5
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 type; /* request type (IRP_MJ_*) */
48 unsigned int status; /* resulting status (or STATUS_PENDING) */
49 ioctl_code_t code; /* ioctl code */
50 data_size_t result; /* size of result (input or output depending on the type) */
51 data_size_t in_size; /* size of input data */
52 void *in_data; /* input data */
53 data_size_t out_size; /* size of output data */
54 void *out_data; /* output data */
57 static void irp_call_dump( struct object *obj, int verbose );
58 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
59 static void irp_call_destroy( struct object *obj );
61 static const struct object_ops irp_call_ops =
63 sizeof(struct irp_call), /* size */
64 irp_call_dump, /* dump */
65 no_get_type, /* get_type */
66 add_queue, /* add_queue */
67 remove_queue, /* remove_queue */
68 irp_call_signaled, /* signaled */
69 no_satisfied, /* satisfied */
70 no_signal, /* signal */
71 no_get_fd, /* get_fd */
72 no_map_access, /* map_access */
73 default_get_sd, /* get_sd */
74 default_set_sd, /* set_sd */
75 no_lookup_name, /* lookup_name */
76 no_open_file, /* open_file */
77 no_close_handle, /* close_handle */
78 irp_call_destroy /* destroy */
82 struct device_manager
84 struct object obj; /* object header */
85 struct list devices; /* list of devices */
86 struct list requests; /* list of pending irps across all devices */
89 static void device_manager_dump( struct object *obj, int verbose );
90 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
91 static void device_manager_destroy( struct object *obj );
93 static const struct object_ops device_manager_ops =
95 sizeof(struct device_manager), /* size */
96 device_manager_dump, /* dump */
97 no_get_type, /* get_type */
98 add_queue, /* add_queue */
99 remove_queue, /* remove_queue */
100 device_manager_signaled, /* signaled */
101 no_satisfied, /* satisfied */
102 no_signal, /* signal */
103 no_get_fd, /* get_fd */
104 no_map_access, /* map_access */
105 default_get_sd, /* get_sd */
106 default_set_sd, /* set_sd */
107 no_lookup_name, /* lookup_name */
108 no_open_file, /* open_file */
109 no_close_handle, /* close_handle */
110 device_manager_destroy /* destroy */
114 struct device
116 struct object obj; /* object header */
117 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
118 struct fd *fd; /* file descriptor for irp */
119 client_ptr_t user_ptr; /* opaque ptr for client side */
120 struct list entry; /* entry in device manager list */
121 struct list requests; /* list of pending irp requests */
124 static void device_dump( struct object *obj, int verbose );
125 static struct object_type *device_get_type( struct object *obj );
126 static struct fd *device_get_fd( struct object *obj );
127 static void device_destroy( struct object *obj );
128 static struct object *device_open_file( struct object *obj, unsigned int access,
129 unsigned int sharing, unsigned int options );
130 static enum server_fd_type device_get_fd_type( struct fd *fd );
131 static obj_handle_t device_read( struct fd *fd, const async_data_t *async_data, int blocking,
132 file_pos_t pos );
133 static obj_handle_t device_write( struct fd *fd, const async_data_t *async_data, int blocking,
134 file_pos_t pos, data_size_t *written );
135 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
136 int blocking );
138 static const struct object_ops device_ops =
140 sizeof(struct device), /* size */
141 device_dump, /* dump */
142 device_get_type, /* get_type */
143 no_add_queue, /* add_queue */
144 NULL, /* remove_queue */
145 NULL, /* signaled */
146 no_satisfied, /* satisfied */
147 no_signal, /* signal */
148 device_get_fd, /* get_fd */
149 default_fd_map_access, /* map_access */
150 default_get_sd, /* get_sd */
151 default_set_sd, /* set_sd */
152 no_lookup_name, /* lookup_name */
153 device_open_file, /* open_file */
154 no_close_handle, /* close_handle */
155 device_destroy /* destroy */
158 static const struct fd_ops device_fd_ops =
160 default_fd_get_poll_events, /* get_poll_events */
161 default_poll_event, /* poll_event */
162 device_get_fd_type, /* get_fd_type */
163 device_read, /* read */
164 device_write, /* write */
165 no_fd_flush, /* flush */
166 device_ioctl, /* ioctl */
167 default_fd_queue_async, /* queue_async */
168 default_fd_reselect_async, /* reselect_async */
169 default_fd_cancel_async /* cancel_async */
173 static void irp_call_dump( struct object *obj, int verbose )
175 struct irp_call *irp = (struct irp_call *)obj;
176 fprintf( stderr, "IRP call device=%p\n", irp->device );
179 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
181 struct irp_call *irp = (struct irp_call *)obj;
183 return !irp->device; /* device is cleared once the irp has completed */
186 static void irp_call_destroy( struct object *obj )
188 struct irp_call *irp = (struct irp_call *)obj;
190 free( irp->in_data );
191 free( irp->out_data );
192 if (irp->async)
194 async_terminate( irp->async, STATUS_CANCELLED );
195 release_object( irp->async );
197 if (irp->device) release_object( irp->device );
198 release_object( irp->thread );
201 static struct irp_call *create_irp( struct device *device, unsigned int type, const void *in_data,
202 data_size_t in_size, data_size_t out_size )
204 struct irp_call *irp;
206 if (!device->manager) /* it has been deleted */
208 set_error( STATUS_FILE_DELETED );
209 return NULL;
212 if ((irp = alloc_object( &irp_call_ops )))
214 irp->device = (struct device *)grab_object( device );
215 irp->async = NULL;
216 irp->type = type;
217 irp->code = 0;
218 irp->status = STATUS_PENDING;
219 irp->result = 0;
220 irp->in_size = in_size;
221 irp->in_data = NULL;
222 irp->out_size = out_size;
223 irp->out_data = NULL;
225 if (irp->in_size && !(irp->in_data = memdup( in_data, in_size )))
227 release_object( irp );
228 irp = NULL;
231 return irp;
234 static void set_irp_result( struct irp_call *irp, unsigned int status,
235 const void *out_data, data_size_t out_size, data_size_t result )
237 struct device *device = irp->device;
239 if (!device) return; /* already finished */
241 /* FIXME: handle the STATUS_PENDING case */
242 irp->status = status;
243 irp->result = result;
244 irp->out_size = min( irp->out_size, out_size );
245 if (irp->out_size && !(irp->out_data = memdup( out_data, irp->out_size )))
246 irp->out_size = 0;
247 release_object( device );
248 irp->device = NULL;
249 if (irp->async)
251 if (result) status = STATUS_ALERTED;
252 async_terminate( irp->async, status );
253 release_object( irp->async );
254 irp->async = NULL;
256 wake_up( &irp->obj, 0 );
258 if (status != STATUS_ALERTED)
260 /* remove it from the device queue */
261 /* (for STATUS_ALERTED this will be done in get_irp_result) */
262 list_remove( &irp->dev_entry );
263 release_object( irp ); /* no longer on the device queue */
268 static void device_dump( struct object *obj, int verbose )
270 struct device *device = (struct device *)obj;
272 fprintf( stderr, "Device " );
273 dump_object_name( &device->obj );
274 fputc( '\n', stderr );
277 static struct object_type *device_get_type( struct object *obj )
279 static const WCHAR name[] = {'D','e','v','i','c','e'};
280 static const struct unicode_str str = { name, sizeof(name) };
281 return get_object_type( &str );
284 static struct fd *device_get_fd( struct object *obj )
286 struct device *device = (struct device *)obj;
288 return (struct fd *)grab_object( device->fd );
291 static void device_destroy( struct object *obj )
293 struct device *device = (struct device *)obj;
294 struct irp_call *irp, *next;
296 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &device->requests, struct irp_call, dev_entry )
298 list_remove( &irp->dev_entry );
299 release_object( irp ); /* no longer on the device queue */
301 if (device->fd) release_object( device->fd );
302 if (device->manager) list_remove( &device->entry );
305 static struct object *device_open_file( struct object *obj, unsigned int access,
306 unsigned int sharing, unsigned int options )
308 return grab_object( obj );
311 static enum server_fd_type device_get_fd_type( struct fd *fd )
313 return FD_TYPE_DEVICE;
316 static struct irp_call *find_irp_call( struct device *device, struct thread *thread,
317 client_ptr_t user_arg )
319 struct irp_call *irp;
321 LIST_FOR_EACH_ENTRY( irp, &device->requests, struct irp_call, dev_entry )
322 if (irp->thread == thread && irp->user_arg == user_arg) return irp;
324 set_error( STATUS_INVALID_PARAMETER );
325 return NULL;
328 /* queue an irp to the device */
329 static obj_handle_t queue_irp( struct device *device, struct irp_call *irp,
330 const async_data_t *async_data, int blocking )
332 obj_handle_t handle = 0;
334 if (blocking && !(handle = alloc_handle( current->process, irp, SYNCHRONIZE, 0 ))) return 0;
336 if (!(irp->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT )))
338 if (handle) close_handle( current->process, handle );
339 return 0;
341 irp->thread = (struct thread *)grab_object( current );
342 irp->user_arg = async_data->arg;
343 grab_object( irp ); /* grab reference for queued irp */
345 list_add_tail( &device->requests, &irp->dev_entry );
346 list_add_tail( &device->manager->requests, &irp->mgr_entry );
347 if (list_head( &device->manager->requests ) == &irp->mgr_entry) /* first one */
348 wake_up( &device->manager->obj, 0 );
349 set_error( STATUS_PENDING );
350 return handle;
353 static obj_handle_t device_read( struct fd *fd, const async_data_t *async_data, int blocking,
354 file_pos_t pos )
356 struct device *device = get_fd_user( fd );
357 struct irp_call *irp;
358 obj_handle_t handle;
360 irp = create_irp( device, IRP_MJ_READ, NULL, 0, get_reply_max_size() );
361 if (!irp) return 0;
363 handle = queue_irp( device, irp, async_data, blocking );
364 release_object( irp );
365 return handle;
368 static obj_handle_t device_write( struct fd *fd, const async_data_t *async_data, int blocking,
369 file_pos_t pos, data_size_t *written )
371 struct device *device = get_fd_user( fd );
372 struct irp_call *irp;
373 obj_handle_t handle;
375 irp = create_irp( device, IRP_MJ_WRITE, get_req_data(), get_req_data_size(), 0 );
376 if (!irp) return 0;
378 handle = queue_irp( device, irp, async_data, blocking );
379 release_object( irp );
380 return handle;
383 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
384 int blocking )
386 struct device *device = get_fd_user( fd );
387 struct irp_call *irp;
388 obj_handle_t handle;
390 irp = create_irp( device, IRP_MJ_DEVICE_CONTROL, get_req_data(), get_req_data_size(),
391 get_reply_max_size() );
392 if (!irp) return 0;
394 irp->code = code;
396 handle = queue_irp( device, irp, async_data, blocking );
397 release_object( irp );
398 return handle;
401 static struct device *create_device( struct directory *root, const struct unicode_str *name,
402 struct device_manager *manager, unsigned int attr )
404 struct device *device;
406 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
408 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
410 /* initialize it if it didn't already exist */
411 device->manager = manager;
412 list_add_tail( &manager->devices, &device->entry );
413 list_init( &device->requests );
414 if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
416 release_object( device );
417 device = NULL;
421 return device;
424 static void delete_device( struct device *device )
426 struct irp_call *irp, *next;
428 if (!device->manager) return; /* already deleted */
430 /* terminate all pending requests */
431 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &device->requests, struct irp_call, dev_entry )
433 list_remove( &irp->mgr_entry );
434 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
436 unlink_named_object( &device->obj );
437 list_remove( &device->entry );
438 device->manager = NULL;
442 static void device_manager_dump( struct object *obj, int verbose )
444 fprintf( stderr, "Device manager\n" );
447 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
449 struct device_manager *manager = (struct device_manager *)obj;
451 return !list_empty( &manager->requests );
454 static void device_manager_destroy( struct object *obj )
456 struct device_manager *manager = (struct device_manager *)obj;
457 struct list *ptr;
459 while ((ptr = list_head( &manager->devices )))
461 struct device *device = LIST_ENTRY( ptr, struct device, entry );
462 delete_device( device );
466 static struct device_manager *create_device_manager(void)
468 struct device_manager *manager;
470 if ((manager = alloc_object( &device_manager_ops )))
472 list_init( &manager->devices );
473 list_init( &manager->requests );
475 return manager;
479 /* create a device manager */
480 DECL_HANDLER(create_device_manager)
482 struct device_manager *manager = create_device_manager();
484 if (manager)
486 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
487 release_object( manager );
492 /* create a device */
493 DECL_HANDLER(create_device)
495 struct device *device;
496 struct unicode_str name;
497 struct device_manager *manager;
498 struct directory *root = NULL;
500 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
501 0, &device_manager_ops )))
502 return;
504 get_req_unicode_str( &name );
505 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
507 release_object( manager );
508 return;
511 if ((device = create_device( root, &name, manager, req->attributes )))
513 device->user_ptr = req->user_ptr;
514 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
515 release_object( device );
518 if (root) release_object( root );
519 release_object( manager );
523 /* delete a device */
524 DECL_HANDLER(delete_device)
526 struct device *device;
528 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
530 delete_device( device );
531 release_object( device );
536 /* retrieve the next pending device irp request */
537 DECL_HANDLER(get_next_device_request)
539 struct irp_call *irp;
540 struct device_manager *manager;
541 struct list *ptr;
543 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
544 0, &device_manager_ops )))
545 return;
547 if (req->prev)
549 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev,
550 0, &irp_call_ops )))
552 set_irp_result( irp, req->status, NULL, 0, 0 );
553 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
554 release_object( irp );
556 clear_error();
559 if ((ptr = list_head( &manager->requests )))
561 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
562 reply->type = irp->type;
563 reply->code = irp->code;
564 reply->user_ptr = irp->device->user_ptr;
565 reply->client_pid = get_process_id( irp->thread->process );
566 reply->client_tid = get_thread_id( irp->thread );
567 reply->in_size = irp->in_size;
568 reply->out_size = irp->out_size;
569 if (irp->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
570 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
572 set_reply_data_ptr( irp->in_data, irp->in_size );
573 irp->in_data = NULL;
574 irp->in_size = 0;
575 list_remove( &irp->mgr_entry );
576 list_init( &irp->mgr_entry );
579 else set_error( STATUS_PENDING );
581 release_object( manager );
585 /* store results of an async irp */
586 DECL_HANDLER(set_irp_result)
588 struct irp_call *irp;
589 struct device_manager *manager;
591 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
592 0, &device_manager_ops )))
593 return;
595 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
597 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
598 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
599 release_object( irp );
601 release_object( manager );
605 /* retrieve results of an async irp */
606 DECL_HANDLER(get_irp_result)
608 struct device *device;
609 struct irp_call *irp;
611 if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
612 return;
614 if ((irp = find_irp_call( device, current, req->user_arg )))
616 if (irp->out_data)
618 data_size_t size = min( irp->out_size, get_reply_max_size() );
619 if (size)
621 set_reply_data_ptr( irp->out_data, size );
622 irp->out_data = NULL;
625 reply->size = irp->result;
626 set_error( irp->status );
627 list_remove( &irp->dev_entry );
628 release_object( irp ); /* no longer on the device queue */
630 release_object( device );