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
22 #include "wine/port.h"
23 #include "wine/rbtree.h"
32 #define WIN32_NO_STATUS
47 struct object obj
; /* object header */
48 struct list dev_entry
; /* entry in device queue */
49 struct list mgr_entry
; /* entry in manager queue */
50 struct device_file
*file
; /* file containing this irp */
51 struct thread
*thread
; /* thread that queued the irp */
52 struct async
*async
; /* pending async op */
53 irp_params_t params
; /* irp parameters */
54 struct iosb
*iosb
; /* I/O status block */
55 int canceled
; /* the call was canceled */
56 client_ptr_t user_ptr
; /* client side pointer */
59 static void irp_call_dump( struct object
*obj
, int verbose
);
60 static int irp_call_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
61 static void irp_call_destroy( struct object
*obj
);
63 static const struct object_ops irp_call_ops
=
65 sizeof(struct irp_call
), /* size */
66 irp_call_dump
, /* dump */
67 no_get_type
, /* get_type */
68 add_queue
, /* add_queue */
69 remove_queue
, /* remove_queue */
70 irp_call_signaled
, /* signaled */
71 no_satisfied
, /* satisfied */
72 no_signal
, /* signal */
73 no_get_fd
, /* get_fd */
74 no_map_access
, /* map_access */
75 default_get_sd
, /* get_sd */
76 default_set_sd
, /* set_sd */
77 no_get_full_name
, /* get_full_name */
78 no_lookup_name
, /* lookup_name */
79 no_link_name
, /* link_name */
80 NULL
, /* unlink_name */
81 no_open_file
, /* open_file */
82 no_kernel_obj_list
, /* get_kernel_obj_list */
83 no_close_handle
, /* close_handle */
84 irp_call_destroy
/* destroy */
88 /* device manager (a list of devices managed by the same client process) */
92 struct object obj
; /* object header */
93 struct list devices
; /* list of devices */
94 struct list requests
; /* list of pending irps across all devices */
95 struct irp_call
*current_call
; /* call currently executed on client side */
96 struct wine_rb_tree kernel_objects
; /* map of objects that have client side pointer associated */
99 static void device_manager_dump( struct object
*obj
, int verbose
);
100 static int device_manager_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
101 static void device_manager_destroy( struct object
*obj
);
103 static const struct object_ops device_manager_ops
=
105 sizeof(struct device_manager
), /* size */
106 device_manager_dump
, /* dump */
107 no_get_type
, /* get_type */
108 add_queue
, /* add_queue */
109 remove_queue
, /* remove_queue */
110 device_manager_signaled
, /* signaled */
111 no_satisfied
, /* satisfied */
112 no_signal
, /* signal */
113 no_get_fd
, /* get_fd */
114 no_map_access
, /* map_access */
115 default_get_sd
, /* get_sd */
116 default_set_sd
, /* set_sd */
117 no_get_full_name
, /* get_full_name */
118 no_lookup_name
, /* lookup_name */
119 no_link_name
, /* link_name */
120 NULL
, /* unlink_name */
121 no_open_file
, /* open_file */
122 no_kernel_obj_list
, /* get_kernel_obj_list */
123 no_close_handle
, /* close_handle */
124 device_manager_destroy
/* destroy */
128 /* device (a single device object) */
132 struct object obj
; /* object header */
133 struct device_manager
*manager
; /* manager for this device (or NULL if deleted) */
134 char *unix_path
; /* path to unix device if any */
135 struct list kernel_object
; /* list of kernel object pointers */
136 struct list entry
; /* entry in device manager list */
137 struct list files
; /* list of open files */
140 static void device_dump( struct object
*obj
, int verbose
);
141 static struct object_type
*device_get_type( struct object
*obj
);
142 static void device_destroy( struct object
*obj
);
143 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
144 unsigned int sharing
, unsigned int options
);
145 static struct list
*device_get_kernel_obj_list( struct object
*obj
);
147 static const struct object_ops device_ops
=
149 sizeof(struct device
), /* size */
150 device_dump
, /* dump */
151 device_get_type
, /* get_type */
152 no_add_queue
, /* add_queue */
153 NULL
, /* remove_queue */
155 no_satisfied
, /* satisfied */
156 no_signal
, /* signal */
157 no_get_fd
, /* get_fd */
158 default_fd_map_access
, /* map_access */
159 default_get_sd
, /* get_sd */
160 default_set_sd
, /* set_sd */
161 default_get_full_name
, /* get_full_name */
162 no_lookup_name
, /* lookup_name */
163 directory_link_name
, /* link_name */
164 default_unlink_name
, /* unlink_name */
165 device_open_file
, /* open_file */
166 device_get_kernel_obj_list
, /* get_kernel_obj_list */
167 no_close_handle
, /* close_handle */
168 device_destroy
/* destroy */
172 /* device file (an open file handle to a device) */
176 struct object obj
; /* object header */
177 struct device
*device
; /* device for this file */
178 struct fd
*fd
; /* file descriptor for irp */
179 struct list kernel_object
; /* list of kernel object pointers */
180 int closed
; /* closed file flag */
181 struct list entry
; /* entry in device list */
182 struct list requests
; /* list of pending irp requests */
185 static void device_file_dump( struct object
*obj
, int verbose
);
186 static struct fd
*device_file_get_fd( struct object
*obj
);
187 static WCHAR
*device_file_get_full_name( struct object
*obj
, data_size_t
*len
);
188 static struct list
*device_file_get_kernel_obj_list( struct object
*obj
);
189 static int device_file_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
190 static void device_file_destroy( struct object
*obj
);
191 static enum server_fd_type
device_file_get_fd_type( struct fd
*fd
);
192 static int device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
193 static int device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
194 static int device_file_flush( struct fd
*fd
, struct async
*async
);
195 static int device_file_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
196 static void device_file_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
198 static const struct object_ops device_file_ops
=
200 sizeof(struct device_file
), /* size */
201 device_file_dump
, /* dump */
202 file_get_type
, /* get_type */
203 add_queue
, /* add_queue */
204 remove_queue
, /* remove_queue */
205 default_fd_signaled
, /* signaled */
206 no_satisfied
, /* satisfied */
207 no_signal
, /* signal */
208 device_file_get_fd
, /* get_fd */
209 default_fd_map_access
, /* map_access */
210 default_get_sd
, /* get_sd */
211 default_set_sd
, /* set_sd */
212 device_file_get_full_name
, /* get_full_name */
213 no_lookup_name
, /* lookup_name */
214 no_link_name
, /* link_name */
215 NULL
, /* unlink_name */
216 no_open_file
, /* open_file */
217 device_file_get_kernel_obj_list
, /* get_kernel_obj_list */
218 device_file_close_handle
, /* close_handle */
219 device_file_destroy
/* destroy */
222 static const struct fd_ops device_file_fd_ops
=
224 default_fd_get_poll_events
, /* get_poll_events */
225 default_poll_event
, /* poll_event */
226 device_file_get_fd_type
, /* get_fd_type */
227 device_file_read
, /* read */
228 device_file_write
, /* write */
229 device_file_flush
, /* flush */
230 default_fd_get_file_info
, /* get_file_info */
231 no_fd_get_volume_info
, /* get_volume_info */
232 device_file_ioctl
, /* ioctl */
233 default_fd_queue_async
, /* queue_async */
234 device_file_reselect_async
/* reselect_async */
238 struct list
*no_kernel_obj_list( struct object
*obj
)
245 struct device_manager
*manager
;
246 client_ptr_t user_ptr
;
247 struct object
*object
;
249 struct list list_entry
;
250 struct wine_rb_entry rb_entry
;
253 static int compare_kernel_object( const void *k
, const struct wine_rb_entry
*entry
)
255 struct kernel_object
*ptr
= WINE_RB_ENTRY_VALUE( entry
, struct kernel_object
, rb_entry
);
256 return memcmp( k
, &ptr
->user_ptr
, sizeof(client_ptr_t
) );
259 static struct kernel_object
*kernel_object_from_obj( struct device_manager
*manager
, struct object
*obj
)
261 struct kernel_object
*kernel_object
;
264 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return NULL
;
265 LIST_FOR_EACH_ENTRY( kernel_object
, list
, struct kernel_object
, list_entry
)
267 if (kernel_object
->manager
!= manager
) continue;
268 return kernel_object
;
273 static client_ptr_t
get_kernel_object_ptr( struct device_manager
*manager
, struct object
*obj
)
275 struct kernel_object
*kernel_object
= kernel_object_from_obj( manager
, obj
);
276 return kernel_object
? kernel_object
->user_ptr
: 0;
279 static struct kernel_object
*set_kernel_object( struct device_manager
*manager
, struct object
*obj
, client_ptr_t user_ptr
)
281 struct kernel_object
*kernel_object
;
284 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return NULL
;
286 if (!(kernel_object
= malloc( sizeof(*kernel_object
) ))) return NULL
;
287 kernel_object
->manager
= manager
;
288 kernel_object
->user_ptr
= user_ptr
;
289 kernel_object
->object
= obj
;
290 kernel_object
->owned
= 0;
292 if (wine_rb_put( &manager
->kernel_objects
, &user_ptr
, &kernel_object
->rb_entry
))
294 /* kernel_object pointer already set */
295 free( kernel_object
);
299 list_add_head( list
, &kernel_object
->list_entry
);
300 return kernel_object
;
303 static struct kernel_object
*kernel_object_from_ptr( struct device_manager
*manager
, client_ptr_t client_ptr
)
305 struct wine_rb_entry
*entry
= wine_rb_get( &manager
->kernel_objects
, &client_ptr
);
306 return entry
? WINE_RB_ENTRY_VALUE( entry
, struct kernel_object
, rb_entry
) : NULL
;
309 static void grab_kernel_object( struct kernel_object
*ptr
)
313 grab_object( ptr
->object
);
318 static void irp_call_dump( struct object
*obj
, int verbose
)
320 struct irp_call
*irp
= (struct irp_call
*)obj
;
321 fprintf( stderr
, "IRP call file=%p\n", irp
->file
);
324 static int irp_call_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
326 struct irp_call
*irp
= (struct irp_call
*)obj
;
328 return !irp
->file
; /* file is cleared once the irp has completed */
331 static void irp_call_destroy( struct object
*obj
)
333 struct irp_call
*irp
= (struct irp_call
*)obj
;
337 async_terminate( irp
->async
, STATUS_CANCELLED
);
338 release_object( irp
->async
);
340 if (irp
->iosb
) release_object( irp
->iosb
);
341 if (irp
->file
) release_object( irp
->file
);
342 if (irp
->thread
) release_object( irp
->thread
);
345 static struct irp_call
*create_irp( struct device_file
*file
, const irp_params_t
*params
, struct async
*async
)
347 struct irp_call
*irp
;
349 if (file
&& !file
->device
->manager
) /* it has been deleted */
351 set_error( STATUS_FILE_DELETED
);
355 if ((irp
= alloc_object( &irp_call_ops
)))
357 irp
->file
= file
? (struct device_file
*)grab_object( file
) : NULL
;
360 irp
->params
= *params
;
365 if (async
) irp
->iosb
= async_get_iosb( async
);
366 if (!irp
->iosb
&& !(irp
->iosb
= create_iosb( NULL
, 0, 0 )))
368 release_object( irp
);
375 static void set_irp_result( struct irp_call
*irp
, unsigned int status
,
376 const void *out_data
, data_size_t out_size
, data_size_t result
)
378 struct device_file
*file
= irp
->file
;
379 struct iosb
*iosb
= irp
->iosb
;
381 if (!file
) return; /* already finished */
383 /* FIXME: handle the STATUS_PENDING case */
384 iosb
->status
= status
;
385 iosb
->result
= result
;
386 iosb
->out_size
= min( iosb
->out_size
, out_size
);
387 if (iosb
->out_size
&& !(iosb
->out_data
= memdup( out_data
, iosb
->out_size
)))
390 /* remove it from the device queue */
391 list_remove( &irp
->dev_entry
);
395 if (result
) status
= STATUS_ALERTED
;
396 async_terminate( irp
->async
, status
);
397 release_object( irp
->async
);
400 wake_up( &irp
->obj
, 0 );
402 release_object( irp
); /* no longer on the device queue */
403 release_object( file
);
407 static void device_dump( struct object
*obj
, int verbose
)
409 fputs( "Device\n", stderr
);
412 static struct object_type
*device_get_type( struct object
*obj
)
414 static const WCHAR name
[] = {'D','e','v','i','c','e'};
415 static const struct unicode_str str
= { name
, sizeof(name
) };
416 return get_object_type( &str
);
419 static void device_destroy( struct object
*obj
)
421 struct device
*device
= (struct device
*)obj
;
423 assert( list_empty( &device
->files
));
425 free( device
->unix_path
);
426 if (device
->manager
) list_remove( &device
->entry
);
429 static void add_irp_to_queue( struct device_manager
*manager
, struct irp_call
*irp
, struct thread
*thread
)
431 grab_object( irp
); /* grab reference for queued irp */
432 irp
->thread
= thread
? (struct thread
*)grab_object( thread
) : NULL
;
433 if (irp
->file
) list_add_tail( &irp
->file
->requests
, &irp
->dev_entry
);
434 list_add_tail( &manager
->requests
, &irp
->mgr_entry
);
435 if (list_head( &manager
->requests
) == &irp
->mgr_entry
) wake_up( &manager
->obj
, 0 ); /* first one */
438 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
439 unsigned int sharing
, unsigned int options
)
441 struct device
*device
= (struct device
*)obj
;
442 struct device_file
*file
;
444 if (!(file
= alloc_object( &device_file_ops
))) return NULL
;
446 file
->device
= (struct device
*)grab_object( device
);
448 list_init( &file
->kernel_object
);
449 list_init( &file
->requests
);
450 list_add_tail( &device
->files
, &file
->entry
);
451 if (device
->unix_path
)
454 access
= file
->obj
.ops
->map_access( &file
->obj
, access
);
455 file
->fd
= open_fd( NULL
, device
->unix_path
, O_NONBLOCK
| O_LARGEFILE
,
456 &mode
, access
, sharing
, options
);
457 if (file
->fd
) set_fd_user( file
->fd
, &device_file_fd_ops
, &file
->obj
);
459 else file
->fd
= alloc_pseudo_fd( &device_file_fd_ops
, &file
->obj
, options
);
463 release_object( file
);
467 allow_fd_caching( file
->fd
);
471 struct irp_call
*irp
;
474 memset( ¶ms
, 0, sizeof(params
) );
475 params
.create
.type
= IRP_CALL_CREATE
;
476 params
.create
.access
= access
;
477 params
.create
.sharing
= sharing
;
478 params
.create
.options
= options
;
479 params
.create
.device
= get_kernel_object_ptr( device
->manager
, &device
->obj
);
481 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
483 add_irp_to_queue( device
->manager
, irp
, current
);
484 release_object( irp
);
490 static struct list
*device_get_kernel_obj_list( struct object
*obj
)
492 struct device
*device
= (struct device
*)obj
;
493 return &device
->kernel_object
;
496 static void device_file_dump( struct object
*obj
, int verbose
)
498 struct device_file
*file
= (struct device_file
*)obj
;
500 fprintf( stderr
, "File on device %p\n", file
->device
);
503 static struct fd
*device_file_get_fd( struct object
*obj
)
505 struct device_file
*file
= (struct device_file
*)obj
;
507 return (struct fd
*)grab_object( file
->fd
);
510 static WCHAR
*device_file_get_full_name( struct object
*obj
, data_size_t
*len
)
512 struct device_file
*file
= (struct device_file
*)obj
;
513 return file
->device
->obj
.ops
->get_full_name( &file
->device
->obj
, len
);
516 static struct list
*device_file_get_kernel_obj_list( struct object
*obj
)
518 struct device_file
*file
= (struct device_file
*)obj
;
519 return &file
->kernel_object
;
522 static int device_file_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
524 struct device_file
*file
= (struct device_file
*)obj
;
526 if (!file
->closed
&& file
->device
->manager
&& obj
->handle_count
== 1) /* last handle */
528 struct irp_call
*irp
;
532 memset( ¶ms
, 0, sizeof(params
) );
533 params
.close
.type
= IRP_CALL_CLOSE
;
535 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
537 add_irp_to_queue( file
->device
->manager
, irp
, current
);
538 release_object( irp
);
544 static void device_file_destroy( struct object
*obj
)
546 struct device_file
*file
= (struct device_file
*)obj
;
547 struct irp_call
*irp
, *next
;
549 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
551 list_remove( &irp
->dev_entry
);
552 release_object( irp
); /* no longer on the device queue */
554 if (file
->fd
) release_object( file
->fd
);
555 list_remove( &file
->entry
);
556 release_object( file
->device
);
559 static int fill_irp_params( struct device_manager
*manager
, struct irp_call
*irp
, irp_params_t
*params
)
561 switch (irp
->params
.type
)
565 case IRP_CALL_CANCEL
:
567 case IRP_CALL_CREATE
:
568 irp
->params
.create
.file
= alloc_handle( current
->process
, irp
->file
,
569 irp
->params
.create
.access
, 0 );
570 if (!irp
->params
.create
.file
) return 0;
573 irp
->params
.close
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
576 irp
->params
.read
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
577 irp
->params
.read
.out_size
= irp
->iosb
->out_size
;
580 irp
->params
.write
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
583 irp
->params
.flush
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
586 irp
->params
.ioctl
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
587 irp
->params
.ioctl
.out_size
= irp
->iosb
->out_size
;
591 *params
= irp
->params
;
595 static void free_irp_params( struct irp_call
*irp
)
597 switch (irp
->params
.type
)
599 case IRP_CALL_CREATE
:
600 close_handle( current
->process
, irp
->params
.create
.file
);
607 /* queue an irp to the device */
608 static int queue_irp( struct device_file
*file
, const irp_params_t
*params
, struct async
*async
)
610 struct irp_call
*irp
= create_irp( file
, params
, async
);
613 fd_queue_async( file
->fd
, async
, ASYNC_TYPE_WAIT
);
614 irp
->async
= (struct async
*)grab_object( async
);
615 add_irp_to_queue( file
->device
->manager
, irp
, current
);
616 release_object( irp
);
617 set_error( STATUS_PENDING
);
621 static enum server_fd_type
device_file_get_fd_type( struct fd
*fd
)
623 return FD_TYPE_DEVICE
;
626 static int device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
628 struct device_file
*file
= get_fd_user( fd
);
631 memset( ¶ms
, 0, sizeof(params
) );
632 params
.read
.type
= IRP_CALL_READ
;
634 params
.read
.pos
= pos
;
635 return queue_irp( file
, ¶ms
, async
);
638 static int device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
640 struct device_file
*file
= get_fd_user( fd
);
643 memset( ¶ms
, 0, sizeof(params
) );
644 params
.write
.type
= IRP_CALL_WRITE
;
645 params
.write
.key
= 0;
646 params
.write
.pos
= pos
;
647 return queue_irp( file
, ¶ms
, async
);
650 static int device_file_flush( struct fd
*fd
, struct async
*async
)
652 struct device_file
*file
= get_fd_user( fd
);
655 memset( ¶ms
, 0, sizeof(params
) );
656 params
.flush
.type
= IRP_CALL_FLUSH
;
657 return queue_irp( file
, ¶ms
, async
);
660 static int device_file_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
662 struct device_file
*file
= get_fd_user( fd
);
665 memset( ¶ms
, 0, sizeof(params
) );
666 params
.ioctl
.type
= IRP_CALL_IOCTL
;
667 params
.ioctl
.code
= code
;
668 return queue_irp( file
, ¶ms
, async
);
671 static void cancel_irp_call( struct irp_call
*irp
)
673 struct irp_call
*cancel_irp
;
677 if (!irp
->user_ptr
|| !irp
->file
|| !irp
->file
->device
->manager
) return;
679 memset( ¶ms
, 0, sizeof(params
) );
680 params
.cancel
.type
= IRP_CALL_CANCEL
;
681 params
.cancel
.irp
= irp
->user_ptr
;
683 if ((cancel_irp
= create_irp( NULL
, ¶ms
, NULL
)))
685 add_irp_to_queue( irp
->file
->device
->manager
, cancel_irp
, NULL
);
686 release_object( cancel_irp
);
689 set_irp_result( irp
, STATUS_CANCELLED
, NULL
, 0, 0 );
692 static void device_file_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
694 struct device_file
*file
= get_fd_user( fd
);
695 struct irp_call
*irp
;
697 LIST_FOR_EACH_ENTRY( irp
, &file
->requests
, struct irp_call
, dev_entry
)
698 if (irp
->iosb
->status
!= STATUS_PENDING
)
700 cancel_irp_call( irp
);
705 static struct device
*create_device( struct object
*root
, const struct unicode_str
*name
,
706 struct device_manager
*manager
)
708 struct device
*device
;
710 if ((device
= create_named_object( root
, &device_ops
, name
, 0, NULL
)))
712 device
->unix_path
= NULL
;
713 device
->manager
= manager
;
714 grab_object( device
);
715 list_add_tail( &manager
->devices
, &device
->entry
);
716 list_init( &device
->kernel_object
);
717 list_init( &device
->files
);
722 struct object
*create_unix_device( struct object
*root
, const struct unicode_str
*name
,
723 unsigned int attr
, const struct security_descriptor
*sd
,
724 const char *unix_path
)
726 struct device
*device
;
728 if ((device
= create_named_object( root
, &device_ops
, name
, attr
, sd
)))
730 device
->unix_path
= strdup( unix_path
);
731 device
->manager
= NULL
; /* no manager, requests go straight to the Unix device */
732 list_init( &device
->kernel_object
);
733 list_init( &device
->files
);
739 /* terminate requests when the underlying device is deleted */
740 static void delete_file( struct device_file
*file
)
742 struct irp_call
*irp
, *next
;
744 /* the pending requests may be the only thing holding a reference to the file */
747 /* terminate all pending requests */
748 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
750 list_remove( &irp
->mgr_entry
);
751 set_irp_result( irp
, STATUS_FILE_DELETED
, NULL
, 0, 0 );
754 release_object( file
);
757 static void delete_device( struct device
*device
)
759 struct device_file
*file
, *next
;
761 if (!device
->manager
) return; /* already deleted */
763 LIST_FOR_EACH_ENTRY_SAFE( file
, next
, &device
->files
, struct device_file
, entry
)
766 unlink_named_object( &device
->obj
);
767 list_remove( &device
->entry
);
768 device
->manager
= NULL
;
769 release_object( device
);
773 static void device_manager_dump( struct object
*obj
, int verbose
)
775 fprintf( stderr
, "Device manager\n" );
778 static int device_manager_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
780 struct device_manager
*manager
= (struct device_manager
*)obj
;
782 return !list_empty( &manager
->requests
);
785 static void device_manager_destroy( struct object
*obj
)
787 struct device_manager
*manager
= (struct device_manager
*)obj
;
788 struct kernel_object
*kernel_object
;
791 if (manager
->current_call
)
793 release_object( manager
->current_call
);
794 manager
->current_call
= NULL
;
797 while (manager
->kernel_objects
.root
)
799 kernel_object
= WINE_RB_ENTRY_VALUE( manager
->kernel_objects
.root
, struct kernel_object
, rb_entry
);
800 wine_rb_remove( &manager
->kernel_objects
, &kernel_object
->rb_entry
);
801 list_remove( &kernel_object
->list_entry
);
802 if (kernel_object
->owned
) release_object( kernel_object
->object
);
803 free( kernel_object
);
806 while ((ptr
= list_head( &manager
->devices
)))
808 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
809 delete_device( device
);
812 while ((ptr
= list_head( &manager
->requests
)))
814 struct irp_call
*irp
= LIST_ENTRY( ptr
, struct irp_call
, mgr_entry
);
815 list_remove( &irp
->mgr_entry
);
816 assert( !irp
->file
&& !irp
->async
);
817 release_object( irp
);
821 static struct device_manager
*create_device_manager(void)
823 struct device_manager
*manager
;
825 if ((manager
= alloc_object( &device_manager_ops
)))
827 manager
->current_call
= NULL
;
828 list_init( &manager
->devices
);
829 list_init( &manager
->requests
);
830 wine_rb_init( &manager
->kernel_objects
, compare_kernel_object
);
835 void free_kernel_objects( struct object
*obj
)
837 struct list
*ptr
, *list
;
839 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return;
841 while ((ptr
= list_head( list
)))
843 struct kernel_object
*kernel_object
= LIST_ENTRY( ptr
, struct kernel_object
, list_entry
);
844 struct irp_call
*irp
;
847 assert( !kernel_object
->owned
);
849 memset( ¶ms
, 0, sizeof(params
) );
850 params
.free
.type
= IRP_CALL_FREE
;
851 params
.free
.obj
= kernel_object
->user_ptr
;
853 if ((irp
= create_irp( NULL
, ¶ms
, NULL
)))
855 add_irp_to_queue( kernel_object
->manager
, irp
, NULL
);
856 release_object( irp
);
859 list_remove( &kernel_object
->list_entry
);
860 wine_rb_remove( &kernel_object
->manager
->kernel_objects
, &kernel_object
->rb_entry
);
861 free( kernel_object
);
866 /* create a device manager */
867 DECL_HANDLER(create_device_manager
)
869 struct device_manager
*manager
= create_device_manager();
873 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
874 release_object( manager
);
879 /* create a device */
880 DECL_HANDLER(create_device
)
882 struct device
*device
;
883 struct unicode_str name
= get_req_unicode_str();
884 struct device_manager
*manager
;
885 struct object
*root
= NULL
;
887 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
888 0, &device_manager_ops
)))
891 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
)))
893 release_object( manager
);
897 if ((device
= create_device( root
, &name
, manager
)))
899 struct kernel_object
*ptr
= set_kernel_object( manager
, &device
->obj
, req
->user_ptr
);
901 grab_kernel_object( ptr
);
903 set_error( STATUS_NO_MEMORY
);
904 release_object( device
);
907 if (root
) release_object( root
);
908 release_object( manager
);
912 /* delete a device */
913 DECL_HANDLER(delete_device
)
915 struct device_manager
*manager
;
916 struct kernel_object
*ref
;
917 struct device
*device
;
919 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
920 0, &device_manager_ops
)))
923 if ((ref
= kernel_object_from_ptr( manager
, req
->device
)) && ref
->object
->ops
== &device_ops
)
925 device
= (struct device
*)grab_object( ref
->object
);
926 delete_device( device
);
927 release_object( device
);
929 else set_error( STATUS_INVALID_HANDLE
);
931 release_object( manager
);
935 /* retrieve the next pending device irp request */
936 DECL_HANDLER(get_next_device_request
)
938 struct irp_call
*irp
;
939 struct device_manager
*manager
;
943 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
944 0, &device_manager_ops
)))
947 if (req
->prev
) close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
949 /* process result of previous call */
950 if (manager
->current_call
)
952 irp
= manager
->current_call
;
953 irp
->user_ptr
= req
->user_ptr
;
956 set_irp_result( irp
, req
->status
, NULL
, 0, 0 );
958 /* if it was canceled during dispatch, we couldn't queue cancel call without client pointer,
959 * so we need to do it now */
960 cancel_irp_call( irp
);
962 set_async_pending( irp
->async
, irp
->file
&& is_fd_overlapped( irp
->file
->fd
) );
964 free_irp_params( irp
);
965 release_object( irp
);
966 manager
->current_call
= NULL
;
971 if ((ptr
= list_head( &manager
->requests
)))
973 struct thread
*thread
;
975 irp
= LIST_ENTRY( ptr
, struct irp_call
, mgr_entry
);
977 thread
= irp
->thread
? irp
->thread
: current
;
978 reply
->client_thread
= get_kernel_object_ptr( manager
, &thread
->obj
);
979 reply
->client_tid
= get_thread_id( thread
);
982 reply
->in_size
= iosb
->in_size
;
983 if (iosb
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
984 else if (!irp
->file
|| (reply
->next
= alloc_handle( current
->process
, irp
, 0, 0 )))
986 if (fill_irp_params( manager
, irp
, &reply
->params
))
988 set_reply_data_ptr( iosb
->in_data
, iosb
->in_size
);
989 iosb
->in_data
= NULL
;
991 list_remove( &irp
->mgr_entry
);
992 list_init( &irp
->mgr_entry
);
993 /* we already own the object if it's only on manager queue */
994 if (irp
->file
) grab_object( irp
);
995 manager
->current_call
= irp
;
997 else close_handle( current
->process
, reply
->next
);
1000 else set_error( STATUS_PENDING
);
1002 release_object( manager
);
1006 /* store results of an async irp */
1007 DECL_HANDLER(set_irp_result
)
1009 struct irp_call
*irp
;
1011 if ((irp
= (struct irp_call
*)get_handle_obj( current
->process
, req
->handle
, 0, &irp_call_ops
)))
1014 set_irp_result( irp
, req
->status
, get_req_data(), get_req_data_size(), req
->size
);
1015 else if(irp
->user_ptr
) /* cancel already queued */
1016 set_error( STATUS_MORE_PROCESSING_REQUIRED
);
1017 else /* we may be still dispatching the IRP. don't bother queuing cancel if it's already complete */
1019 close_handle( current
->process
, req
->handle
); /* avoid an extra round-trip for close */
1020 release_object( irp
);
1025 /* get kernel pointer from server object */
1026 DECL_HANDLER(get_kernel_object_ptr
)
1028 struct device_manager
*manager
;
1029 struct object
*object
= NULL
;
1031 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1032 0, &device_manager_ops
)))
1035 if ((object
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
1037 reply
->user_ptr
= get_kernel_object_ptr( manager
, object
);
1038 release_object( object
);
1041 release_object( manager
);
1045 /* associate kernel pointer with server object */
1046 DECL_HANDLER(set_kernel_object_ptr
)
1048 struct device_manager
*manager
;
1049 struct object
*object
= NULL
;
1051 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1052 0, &device_manager_ops
)))
1055 if (!(object
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
1057 release_object( manager
);
1061 if (!set_kernel_object( manager
, object
, req
->user_ptr
))
1062 set_error( STATUS_INVALID_HANDLE
);
1064 release_object( object
);
1065 release_object( manager
);
1069 /* grab server object reference from kernel object pointer */
1070 DECL_HANDLER(grab_kernel_object
)
1072 struct device_manager
*manager
;
1073 struct kernel_object
*ref
;
1075 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1076 0, &device_manager_ops
)))
1079 if ((ref
= kernel_object_from_ptr( manager
, req
->user_ptr
)) && !ref
->owned
)
1080 grab_kernel_object( ref
);
1082 set_error( STATUS_INVALID_HANDLE
);
1084 release_object( manager
);
1088 /* release server object reference from kernel object pointer */
1089 DECL_HANDLER(release_kernel_object
)
1091 struct device_manager
*manager
;
1092 struct kernel_object
*ref
;
1094 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1095 0, &device_manager_ops
)))
1098 if ((ref
= kernel_object_from_ptr( manager
, req
->user_ptr
)) && ref
->owned
)
1101 release_object( ref
->object
);
1103 else set_error( STATUS_INVALID_HANDLE
);
1105 release_object( manager
);
1109 /* get handle from kernel object pointer */
1110 DECL_HANDLER(get_kernel_object_handle
)
1112 struct device_manager
*manager
;
1113 struct kernel_object
*ref
;
1115 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1116 0, &device_manager_ops
)))
1119 if ((ref
= kernel_object_from_ptr( manager
, req
->user_ptr
)))
1120 reply
->handle
= alloc_handle( current
->process
, ref
->object
, req
->access
, 0 );
1122 set_error( STATUS_INVALID_HANDLE
);
1124 release_object( manager
);