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_lookup_name
, /* lookup_name */
78 no_link_name
, /* link_name */
79 NULL
, /* unlink_name */
80 no_open_file
, /* open_file */
81 no_kernel_obj_list
, /* get_kernel_obj_list */
82 no_close_handle
, /* close_handle */
83 irp_call_destroy
/* destroy */
87 /* device manager (a list of devices managed by the same client process) */
91 struct object obj
; /* object header */
92 struct list devices
; /* list of devices */
93 struct list requests
; /* list of pending irps across all devices */
94 struct irp_call
*current_call
; /* call currently executed on client side */
95 struct wine_rb_tree kernel_objects
; /* map of objects that have client side pointer associated */
98 static void device_manager_dump( struct object
*obj
, int verbose
);
99 static int device_manager_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
100 static void device_manager_destroy( struct object
*obj
);
102 static const struct object_ops device_manager_ops
=
104 sizeof(struct device_manager
), /* size */
105 device_manager_dump
, /* dump */
106 no_get_type
, /* get_type */
107 add_queue
, /* add_queue */
108 remove_queue
, /* remove_queue */
109 device_manager_signaled
, /* signaled */
110 no_satisfied
, /* satisfied */
111 no_signal
, /* signal */
112 no_get_fd
, /* get_fd */
113 no_map_access
, /* map_access */
114 default_get_sd
, /* get_sd */
115 default_set_sd
, /* set_sd */
116 no_lookup_name
, /* lookup_name */
117 no_link_name
, /* link_name */
118 NULL
, /* unlink_name */
119 no_open_file
, /* open_file */
120 no_kernel_obj_list
, /* get_kernel_obj_list */
121 no_close_handle
, /* close_handle */
122 device_manager_destroy
/* destroy */
126 /* device (a single device object) */
130 struct object obj
; /* object header */
131 struct device_manager
*manager
; /* manager for this device (or NULL if deleted) */
132 char *unix_path
; /* path to unix device if any */
133 struct list kernel_object
; /* list of kernel object pointers */
134 struct list entry
; /* entry in device manager list */
135 struct list files
; /* list of open files */
138 static void device_dump( struct object
*obj
, int verbose
);
139 static struct object_type
*device_get_type( struct object
*obj
);
140 static void device_destroy( struct object
*obj
);
141 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
142 unsigned int sharing
, unsigned int options
);
143 static struct list
*device_get_kernel_obj_list( struct object
*obj
);
145 static const struct object_ops device_ops
=
147 sizeof(struct device
), /* size */
148 device_dump
, /* dump */
149 device_get_type
, /* get_type */
150 no_add_queue
, /* add_queue */
151 NULL
, /* remove_queue */
153 no_satisfied
, /* satisfied */
154 no_signal
, /* signal */
155 no_get_fd
, /* get_fd */
156 default_fd_map_access
, /* map_access */
157 default_get_sd
, /* get_sd */
158 default_set_sd
, /* set_sd */
159 no_lookup_name
, /* lookup_name */
160 directory_link_name
, /* link_name */
161 default_unlink_name
, /* unlink_name */
162 device_open_file
, /* open_file */
163 device_get_kernel_obj_list
, /* get_kernel_obj_list */
164 no_close_handle
, /* close_handle */
165 device_destroy
/* destroy */
169 /* device file (an open file handle to a device) */
173 struct object obj
; /* object header */
174 struct device
*device
; /* device for this file */
175 struct fd
*fd
; /* file descriptor for irp */
176 struct list kernel_object
; /* list of kernel object pointers */
177 int closed
; /* closed file flag */
178 struct list entry
; /* entry in device list */
179 struct list requests
; /* list of pending irp requests */
182 static void device_file_dump( struct object
*obj
, int verbose
);
183 static struct fd
*device_file_get_fd( struct object
*obj
);
184 static struct list
*device_file_get_kernel_obj_list( struct object
*obj
);
185 static int device_file_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
186 static void device_file_destroy( struct object
*obj
);
187 static enum server_fd_type
device_file_get_fd_type( struct fd
*fd
);
188 static int device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
189 static int device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
190 static int device_file_flush( struct fd
*fd
, struct async
*async
);
191 static int device_file_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
192 static void device_file_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
194 static const struct object_ops device_file_ops
=
196 sizeof(struct device_file
), /* size */
197 device_file_dump
, /* dump */
198 file_get_type
, /* get_type */
199 add_queue
, /* add_queue */
200 remove_queue
, /* remove_queue */
201 default_fd_signaled
, /* signaled */
202 no_satisfied
, /* satisfied */
203 no_signal
, /* signal */
204 device_file_get_fd
, /* get_fd */
205 default_fd_map_access
, /* map_access */
206 default_get_sd
, /* get_sd */
207 default_set_sd
, /* set_sd */
208 no_lookup_name
, /* lookup_name */
209 no_link_name
, /* link_name */
210 NULL
, /* unlink_name */
211 no_open_file
, /* open_file */
212 device_file_get_kernel_obj_list
, /* get_kernel_obj_list */
213 device_file_close_handle
, /* close_handle */
214 device_file_destroy
/* destroy */
217 static const struct fd_ops device_file_fd_ops
=
219 default_fd_get_poll_events
, /* get_poll_events */
220 default_poll_event
, /* poll_event */
221 device_file_get_fd_type
, /* get_fd_type */
222 device_file_read
, /* read */
223 device_file_write
, /* write */
224 device_file_flush
, /* flush */
225 default_fd_get_file_info
, /* get_file_info */
226 no_fd_get_volume_info
, /* get_volume_info */
227 device_file_ioctl
, /* ioctl */
228 default_fd_queue_async
, /* queue_async */
229 device_file_reselect_async
/* reselect_async */
233 struct list
*no_kernel_obj_list( struct object
*obj
)
240 struct device_manager
*manager
;
241 client_ptr_t user_ptr
;
242 struct object
*object
;
244 struct list list_entry
;
245 struct wine_rb_entry rb_entry
;
248 static int compare_kernel_object( const void *k
, const struct wine_rb_entry
*entry
)
250 struct kernel_object
*ptr
= WINE_RB_ENTRY_VALUE( entry
, struct kernel_object
, rb_entry
);
251 return memcmp( k
, &ptr
->user_ptr
, sizeof(client_ptr_t
) );
254 static struct kernel_object
*kernel_object_from_obj( struct device_manager
*manager
, struct object
*obj
)
256 struct kernel_object
*kernel_object
;
259 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return NULL
;
260 LIST_FOR_EACH_ENTRY( kernel_object
, list
, struct kernel_object
, list_entry
)
262 if (kernel_object
->manager
!= manager
) continue;
263 return kernel_object
;
268 static client_ptr_t
get_kernel_object_ptr( struct device_manager
*manager
, struct object
*obj
)
270 struct kernel_object
*kernel_object
= kernel_object_from_obj( manager
, obj
);
271 return kernel_object
? kernel_object
->user_ptr
: 0;
274 static struct kernel_object
*set_kernel_object( struct device_manager
*manager
, struct object
*obj
, client_ptr_t user_ptr
)
276 struct kernel_object
*kernel_object
;
279 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return NULL
;
281 if (!(kernel_object
= malloc( sizeof(*kernel_object
) ))) return NULL
;
282 kernel_object
->manager
= manager
;
283 kernel_object
->user_ptr
= user_ptr
;
284 kernel_object
->object
= obj
;
285 kernel_object
->owned
= 0;
287 if (wine_rb_put( &manager
->kernel_objects
, &user_ptr
, &kernel_object
->rb_entry
))
289 /* kernel_object pointer already set */
290 free( kernel_object
);
294 list_add_head( list
, &kernel_object
->list_entry
);
295 return kernel_object
;
298 static struct kernel_object
*kernel_object_from_ptr( struct device_manager
*manager
, client_ptr_t client_ptr
)
300 struct wine_rb_entry
*entry
= wine_rb_get( &manager
->kernel_objects
, &client_ptr
);
301 return entry
? WINE_RB_ENTRY_VALUE( entry
, struct kernel_object
, rb_entry
) : NULL
;
304 static void grab_kernel_object( struct kernel_object
*ptr
)
308 grab_object( ptr
->object
);
313 static void irp_call_dump( struct object
*obj
, int verbose
)
315 struct irp_call
*irp
= (struct irp_call
*)obj
;
316 fprintf( stderr
, "IRP call file=%p\n", irp
->file
);
319 static int irp_call_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
321 struct irp_call
*irp
= (struct irp_call
*)obj
;
323 return !irp
->file
; /* file is cleared once the irp has completed */
326 static void irp_call_destroy( struct object
*obj
)
328 struct irp_call
*irp
= (struct irp_call
*)obj
;
332 async_terminate( irp
->async
, STATUS_CANCELLED
);
333 release_object( irp
->async
);
335 if (irp
->iosb
) release_object( irp
->iosb
);
336 if (irp
->file
) release_object( irp
->file
);
337 if (irp
->thread
) release_object( irp
->thread
);
340 static struct irp_call
*create_irp( struct device_file
*file
, const irp_params_t
*params
, struct async
*async
)
342 struct irp_call
*irp
;
344 if (file
&& !file
->device
->manager
) /* it has been deleted */
346 set_error( STATUS_FILE_DELETED
);
350 if ((irp
= alloc_object( &irp_call_ops
)))
352 irp
->file
= file
? (struct device_file
*)grab_object( file
) : NULL
;
355 irp
->params
= *params
;
360 if (async
) irp
->iosb
= async_get_iosb( async
);
361 if (!irp
->iosb
&& !(irp
->iosb
= create_iosb( NULL
, 0, 0 )))
363 release_object( irp
);
370 static void set_irp_result( struct irp_call
*irp
, unsigned int status
,
371 const void *out_data
, data_size_t out_size
, data_size_t result
)
373 struct device_file
*file
= irp
->file
;
374 struct iosb
*iosb
= irp
->iosb
;
376 if (!file
) return; /* already finished */
378 /* FIXME: handle the STATUS_PENDING case */
379 iosb
->status
= status
;
380 iosb
->result
= result
;
381 iosb
->out_size
= min( iosb
->out_size
, out_size
);
382 if (iosb
->out_size
&& !(iosb
->out_data
= memdup( out_data
, iosb
->out_size
)))
385 /* remove it from the device queue */
386 list_remove( &irp
->dev_entry
);
390 if (result
) status
= STATUS_ALERTED
;
391 async_terminate( irp
->async
, status
);
392 release_object( irp
->async
);
395 wake_up( &irp
->obj
, 0 );
397 release_object( irp
); /* no longer on the device queue */
398 release_object( file
);
402 static void device_dump( struct object
*obj
, int verbose
)
404 fputs( "Device\n", stderr
);
407 static struct object_type
*device_get_type( struct object
*obj
)
409 static const WCHAR name
[] = {'D','e','v','i','c','e'};
410 static const struct unicode_str str
= { name
, sizeof(name
) };
411 return get_object_type( &str
);
414 static void device_destroy( struct object
*obj
)
416 struct device
*device
= (struct device
*)obj
;
418 assert( list_empty( &device
->files
));
420 free( device
->unix_path
);
421 if (device
->manager
) list_remove( &device
->entry
);
424 static void add_irp_to_queue( struct device_manager
*manager
, struct irp_call
*irp
, struct thread
*thread
)
426 grab_object( irp
); /* grab reference for queued irp */
427 irp
->thread
= thread
? (struct thread
*)grab_object( thread
) : NULL
;
428 if (irp
->file
) list_add_tail( &irp
->file
->requests
, &irp
->dev_entry
);
429 list_add_tail( &manager
->requests
, &irp
->mgr_entry
);
430 if (list_head( &manager
->requests
) == &irp
->mgr_entry
) wake_up( &manager
->obj
, 0 ); /* first one */
433 static struct object
*device_open_file( struct object
*obj
, unsigned int access
,
434 unsigned int sharing
, unsigned int options
)
436 struct device
*device
= (struct device
*)obj
;
437 struct device_file
*file
;
439 if (!(file
= alloc_object( &device_file_ops
))) return NULL
;
441 file
->device
= (struct device
*)grab_object( device
);
443 list_init( &file
->kernel_object
);
444 list_init( &file
->requests
);
445 list_add_tail( &device
->files
, &file
->entry
);
446 if (device
->unix_path
)
449 access
= file
->obj
.ops
->map_access( &file
->obj
, access
);
450 file
->fd
= open_fd( NULL
, device
->unix_path
, O_NONBLOCK
| O_LARGEFILE
,
451 &mode
, access
, sharing
, options
);
452 if (file
->fd
) set_fd_user( file
->fd
, &device_file_fd_ops
, &file
->obj
);
454 else file
->fd
= alloc_pseudo_fd( &device_file_fd_ops
, &file
->obj
, options
);
458 release_object( file
);
462 allow_fd_caching( file
->fd
);
466 struct irp_call
*irp
;
469 memset( ¶ms
, 0, sizeof(params
) );
470 params
.create
.type
= IRP_CALL_CREATE
;
471 params
.create
.access
= access
;
472 params
.create
.sharing
= sharing
;
473 params
.create
.options
= options
;
474 params
.create
.device
= get_kernel_object_ptr( device
->manager
, &device
->obj
);
476 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
478 add_irp_to_queue( device
->manager
, irp
, current
);
479 release_object( irp
);
485 static struct list
*device_get_kernel_obj_list( struct object
*obj
)
487 struct device
*device
= (struct device
*)obj
;
488 return &device
->kernel_object
;
491 static void device_file_dump( struct object
*obj
, int verbose
)
493 struct device_file
*file
= (struct device_file
*)obj
;
495 fprintf( stderr
, "File on device %p\n", file
->device
);
498 static struct fd
*device_file_get_fd( struct object
*obj
)
500 struct device_file
*file
= (struct device_file
*)obj
;
502 return (struct fd
*)grab_object( file
->fd
);
505 static struct list
*device_file_get_kernel_obj_list( struct object
*obj
)
507 struct device_file
*file
= (struct device_file
*)obj
;
508 return &file
->kernel_object
;
511 static int device_file_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
513 struct device_file
*file
= (struct device_file
*)obj
;
515 if (!file
->closed
&& file
->device
->manager
&& obj
->handle_count
== 1) /* last handle */
517 struct irp_call
*irp
;
521 memset( ¶ms
, 0, sizeof(params
) );
522 params
.close
.type
= IRP_CALL_CLOSE
;
524 if ((irp
= create_irp( file
, ¶ms
, NULL
)))
526 add_irp_to_queue( file
->device
->manager
, irp
, current
);
527 release_object( irp
);
533 static void device_file_destroy( struct object
*obj
)
535 struct device_file
*file
= (struct device_file
*)obj
;
536 struct irp_call
*irp
, *next
;
538 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
540 list_remove( &irp
->dev_entry
);
541 release_object( irp
); /* no longer on the device queue */
543 if (file
->fd
) release_object( file
->fd
);
544 list_remove( &file
->entry
);
545 release_object( file
->device
);
548 static int fill_irp_params( struct device_manager
*manager
, struct irp_call
*irp
, irp_params_t
*params
)
550 switch (irp
->params
.type
)
554 case IRP_CALL_CANCEL
:
556 case IRP_CALL_CREATE
:
557 irp
->params
.create
.file
= alloc_handle( current
->process
, irp
->file
,
558 irp
->params
.create
.access
, 0 );
559 if (!irp
->params
.create
.file
) return 0;
562 irp
->params
.close
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
565 irp
->params
.read
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
566 irp
->params
.read
.out_size
= irp
->iosb
->out_size
;
569 irp
->params
.write
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
572 irp
->params
.flush
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
575 irp
->params
.ioctl
.file
= get_kernel_object_ptr( manager
, &irp
->file
->obj
);
576 irp
->params
.ioctl
.out_size
= irp
->iosb
->out_size
;
580 *params
= irp
->params
;
584 static void free_irp_params( struct irp_call
*irp
)
586 switch (irp
->params
.type
)
588 case IRP_CALL_CREATE
:
589 close_handle( current
->process
, irp
->params
.create
.file
);
596 /* queue an irp to the device */
597 static int queue_irp( struct device_file
*file
, const irp_params_t
*params
, struct async
*async
)
599 struct irp_call
*irp
= create_irp( file
, params
, async
);
602 fd_queue_async( file
->fd
, async
, ASYNC_TYPE_WAIT
);
603 irp
->async
= (struct async
*)grab_object( async
);
604 add_irp_to_queue( file
->device
->manager
, irp
, current
);
605 release_object( irp
);
606 set_error( STATUS_PENDING
);
610 static enum server_fd_type
device_file_get_fd_type( struct fd
*fd
)
612 return FD_TYPE_DEVICE
;
615 static int device_file_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
617 struct device_file
*file
= get_fd_user( fd
);
620 memset( ¶ms
, 0, sizeof(params
) );
621 params
.read
.type
= IRP_CALL_READ
;
623 params
.read
.pos
= pos
;
624 return queue_irp( file
, ¶ms
, async
);
627 static int device_file_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
629 struct device_file
*file
= get_fd_user( fd
);
632 memset( ¶ms
, 0, sizeof(params
) );
633 params
.write
.type
= IRP_CALL_WRITE
;
634 params
.write
.key
= 0;
635 params
.write
.pos
= pos
;
636 return queue_irp( file
, ¶ms
, async
);
639 static int device_file_flush( struct fd
*fd
, struct async
*async
)
641 struct device_file
*file
= get_fd_user( fd
);
644 memset( ¶ms
, 0, sizeof(params
) );
645 params
.flush
.type
= IRP_CALL_FLUSH
;
646 return queue_irp( file
, ¶ms
, async
);
649 static int device_file_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
651 struct device_file
*file
= get_fd_user( fd
);
654 memset( ¶ms
, 0, sizeof(params
) );
655 params
.ioctl
.type
= IRP_CALL_IOCTL
;
656 params
.ioctl
.code
= code
;
657 return queue_irp( file
, ¶ms
, async
);
660 static void cancel_irp_call( struct irp_call
*irp
)
662 struct irp_call
*cancel_irp
;
666 if (!irp
->user_ptr
|| !irp
->file
|| !irp
->file
->device
->manager
) return;
668 memset( ¶ms
, 0, sizeof(params
) );
669 params
.cancel
.type
= IRP_CALL_CANCEL
;
670 params
.cancel
.irp
= irp
->user_ptr
;
672 if ((cancel_irp
= create_irp( NULL
, ¶ms
, NULL
)))
674 add_irp_to_queue( irp
->file
->device
->manager
, cancel_irp
, NULL
);
675 release_object( cancel_irp
);
678 set_irp_result( irp
, STATUS_CANCELLED
, NULL
, 0, 0 );
681 static void device_file_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
683 struct device_file
*file
= get_fd_user( fd
);
684 struct irp_call
*irp
;
686 LIST_FOR_EACH_ENTRY( irp
, &file
->requests
, struct irp_call
, dev_entry
)
687 if (irp
->iosb
->status
!= STATUS_PENDING
)
689 cancel_irp_call( irp
);
694 static struct device
*create_device( struct object
*root
, const struct unicode_str
*name
,
695 struct device_manager
*manager
)
697 struct device
*device
;
699 if ((device
= create_named_object( root
, &device_ops
, name
, 0, NULL
)))
701 device
->unix_path
= NULL
;
702 device
->manager
= manager
;
703 list_add_tail( &manager
->devices
, &device
->entry
);
704 list_init( &device
->kernel_object
);
705 list_init( &device
->files
);
710 struct object
*create_unix_device( struct object
*root
, const struct unicode_str
*name
,
711 const char *unix_path
)
713 struct device
*device
;
715 if ((device
= create_named_object( root
, &device_ops
, name
, 0, NULL
)))
717 device
->unix_path
= strdup( unix_path
);
718 device
->manager
= NULL
; /* no manager, requests go straight to the Unix device */
719 list_init( &device
->kernel_object
);
720 list_init( &device
->files
);
726 /* terminate requests when the underlying device is deleted */
727 static void delete_file( struct device_file
*file
)
729 struct irp_call
*irp
, *next
;
731 /* terminate all pending requests */
732 LIST_FOR_EACH_ENTRY_SAFE( irp
, next
, &file
->requests
, struct irp_call
, dev_entry
)
734 list_remove( &irp
->mgr_entry
);
735 set_irp_result( irp
, STATUS_FILE_DELETED
, NULL
, 0, 0 );
739 static void delete_device( struct device
*device
)
741 struct device_file
*file
, *next
;
743 if (!device
->manager
) return; /* already deleted */
745 LIST_FOR_EACH_ENTRY_SAFE( file
, next
, &device
->files
, struct device_file
, entry
)
748 unlink_named_object( &device
->obj
);
749 list_remove( &device
->entry
);
750 device
->manager
= NULL
;
754 static void device_manager_dump( struct object
*obj
, int verbose
)
756 fprintf( stderr
, "Device manager\n" );
759 static int device_manager_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
761 struct device_manager
*manager
= (struct device_manager
*)obj
;
763 return !list_empty( &manager
->requests
);
766 static void device_manager_destroy( struct object
*obj
)
768 struct device_manager
*manager
= (struct device_manager
*)obj
;
769 struct kernel_object
*kernel_object
;
772 if (manager
->current_call
)
774 release_object( manager
->current_call
);
775 manager
->current_call
= NULL
;
778 while (manager
->kernel_objects
.root
)
780 kernel_object
= WINE_RB_ENTRY_VALUE( manager
->kernel_objects
.root
, struct kernel_object
, rb_entry
);
781 wine_rb_remove( &manager
->kernel_objects
, &kernel_object
->rb_entry
);
782 list_remove( &kernel_object
->list_entry
);
783 if (kernel_object
->owned
) release_object( kernel_object
->object
);
784 free( kernel_object
);
787 while ((ptr
= list_head( &manager
->devices
)))
789 struct device
*device
= LIST_ENTRY( ptr
, struct device
, entry
);
790 delete_device( device
);
793 while ((ptr
= list_head( &manager
->requests
)))
795 struct irp_call
*irp
= LIST_ENTRY( ptr
, struct irp_call
, mgr_entry
);
796 list_remove( &irp
->mgr_entry
);
797 assert( !irp
->file
&& !irp
->async
);
798 release_object( irp
);
802 static struct device_manager
*create_device_manager(void)
804 struct device_manager
*manager
;
806 if ((manager
= alloc_object( &device_manager_ops
)))
808 manager
->current_call
= NULL
;
809 list_init( &manager
->devices
);
810 list_init( &manager
->requests
);
811 wine_rb_init( &manager
->kernel_objects
, compare_kernel_object
);
816 void free_kernel_objects( struct object
*obj
)
818 struct list
*ptr
, *list
;
820 if (!(list
= obj
->ops
->get_kernel_obj_list( obj
))) return;
822 while ((ptr
= list_head( list
)))
824 struct kernel_object
*kernel_object
= LIST_ENTRY( ptr
, struct kernel_object
, list_entry
);
825 struct irp_call
*irp
;
828 assert( !kernel_object
->owned
);
830 memset( ¶ms
, 0, sizeof(params
) );
831 params
.free
.type
= IRP_CALL_FREE
;
832 params
.free
.obj
= kernel_object
->user_ptr
;
834 if ((irp
= create_irp( NULL
, ¶ms
, NULL
)))
836 add_irp_to_queue( kernel_object
->manager
, irp
, NULL
);
837 release_object( irp
);
840 list_remove( &kernel_object
->list_entry
);
841 wine_rb_remove( &kernel_object
->manager
->kernel_objects
, &kernel_object
->rb_entry
);
842 free( kernel_object
);
847 /* create a device manager */
848 DECL_HANDLER(create_device_manager
)
850 struct device_manager
*manager
= create_device_manager();
854 reply
->handle
= alloc_handle( current
->process
, manager
, req
->access
, req
->attributes
);
855 release_object( manager
);
860 /* create a device */
861 DECL_HANDLER(create_device
)
863 struct device
*device
;
864 struct unicode_str name
= get_req_unicode_str();
865 struct device_manager
*manager
;
866 struct object
*root
= NULL
;
868 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
869 0, &device_manager_ops
)))
872 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
)))
874 release_object( manager
);
878 if ((device
= create_device( root
, &name
, manager
)))
880 struct kernel_object
*ptr
= set_kernel_object( manager
, &device
->obj
, req
->user_ptr
);
882 grab_kernel_object( ptr
);
884 set_error( STATUS_NO_MEMORY
);
885 release_object( device
);
888 if (root
) release_object( root
);
889 release_object( manager
);
893 /* delete a device */
894 DECL_HANDLER(delete_device
)
896 struct device_manager
*manager
;
897 struct kernel_object
*ref
;
898 struct device
*device
;
900 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
901 0, &device_manager_ops
)))
904 if ((ref
= kernel_object_from_ptr( manager
, req
->device
)) && ref
->object
->ops
== &device_ops
)
906 device
= (struct device
*)grab_object( ref
->object
);
907 delete_device( device
);
908 release_object( device
);
910 else set_error( STATUS_INVALID_HANDLE
);
912 release_object( manager
);
916 /* retrieve the next pending device irp request */
917 DECL_HANDLER(get_next_device_request
)
919 struct irp_call
*irp
;
920 struct device_manager
*manager
;
924 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
925 0, &device_manager_ops
)))
928 if (req
->prev
) close_handle( current
->process
, req
->prev
); /* avoid an extra round-trip for close */
930 /* process result of previous call */
931 if (manager
->current_call
)
933 irp
= manager
->current_call
;
934 irp
->user_ptr
= req
->user_ptr
;
937 set_irp_result( irp
, req
->status
, NULL
, 0, 0 );
939 /* if it was canceled during dispatch, we couldn't queue cancel call without client pointer,
940 * so we need to do it now */
941 cancel_irp_call( irp
);
943 set_async_pending( irp
->async
, irp
->file
&& is_fd_overlapped( irp
->file
->fd
) );
945 free_irp_params( irp
);
946 release_object( irp
);
947 manager
->current_call
= NULL
;
952 if ((ptr
= list_head( &manager
->requests
)))
954 struct thread
*thread
;
956 irp
= LIST_ENTRY( ptr
, struct irp_call
, mgr_entry
);
958 thread
= irp
->thread
? irp
->thread
: current
;
959 reply
->client_thread
= get_kernel_object_ptr( manager
, &thread
->obj
);
960 reply
->client_tid
= get_thread_id( thread
);
963 reply
->in_size
= iosb
->in_size
;
964 if (iosb
->in_size
> get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW
);
965 else if (!irp
->file
|| (reply
->next
= alloc_handle( current
->process
, irp
, 0, 0 )))
967 if (fill_irp_params( manager
, irp
, &reply
->params
))
969 set_reply_data_ptr( iosb
->in_data
, iosb
->in_size
);
970 iosb
->in_data
= NULL
;
972 list_remove( &irp
->mgr_entry
);
973 list_init( &irp
->mgr_entry
);
974 /* we already own the object if it's only on manager queue */
975 if (irp
->file
) grab_object( irp
);
976 manager
->current_call
= irp
;
978 else close_handle( current
->process
, reply
->next
);
981 else set_error( STATUS_PENDING
);
983 release_object( manager
);
987 /* store results of an async irp */
988 DECL_HANDLER(set_irp_result
)
990 struct irp_call
*irp
;
992 if ((irp
= (struct irp_call
*)get_handle_obj( current
->process
, req
->handle
, 0, &irp_call_ops
)))
995 set_irp_result( irp
, req
->status
, get_req_data(), get_req_data_size(), req
->size
);
996 else if(irp
->user_ptr
) /* cancel already queued */
997 set_error( STATUS_MORE_PROCESSING_REQUIRED
);
998 else /* we may be still dispatching the IRP. don't bother queuing cancel if it's already complete */
1000 close_handle( current
->process
, req
->handle
); /* avoid an extra round-trip for close */
1001 release_object( irp
);
1006 /* get kernel pointer from server object */
1007 DECL_HANDLER(get_kernel_object_ptr
)
1009 struct device_manager
*manager
;
1010 struct object
*object
= NULL
;
1012 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1013 0, &device_manager_ops
)))
1016 if ((object
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
1018 reply
->user_ptr
= get_kernel_object_ptr( manager
, object
);
1019 release_object( object
);
1022 release_object( manager
);
1026 /* associate kernel pointer with server object */
1027 DECL_HANDLER(set_kernel_object_ptr
)
1029 struct device_manager
*manager
;
1030 struct object
*object
= NULL
;
1032 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1033 0, &device_manager_ops
)))
1036 if (!(object
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
1038 release_object( manager
);
1042 if (!set_kernel_object( manager
, object
, req
->user_ptr
))
1043 set_error( STATUS_INVALID_HANDLE
);
1045 release_object( object
);
1046 release_object( manager
);
1050 /* grab server object reference from kernel object pointer */
1051 DECL_HANDLER(grab_kernel_object
)
1053 struct device_manager
*manager
;
1054 struct kernel_object
*ref
;
1056 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1057 0, &device_manager_ops
)))
1060 if ((ref
= kernel_object_from_ptr( manager
, req
->user_ptr
)) && !ref
->owned
)
1061 grab_kernel_object( ref
);
1063 set_error( STATUS_INVALID_HANDLE
);
1065 release_object( manager
);
1069 /* release server object reference from kernel object pointer */
1070 DECL_HANDLER(release_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
)
1082 release_object( ref
->object
);
1084 else set_error( STATUS_INVALID_HANDLE
);
1086 release_object( manager
);
1090 /* get handle from kernel object pointer */
1091 DECL_HANDLER(get_kernel_object_handle
)
1093 struct device_manager
*manager
;
1094 struct kernel_object
*ref
;
1096 if (!(manager
= (struct device_manager
*)get_handle_obj( current
->process
, req
->manager
,
1097 0, &device_manager_ops
)))
1100 if ((ref
= kernel_object_from_ptr( manager
, req
->user_ptr
)))
1101 reply
->handle
= alloc_handle( current
->process
, ref
->object
, req
->access
, 0 );
1103 set_error( STATUS_INVALID_HANDLE
);
1105 release_object( manager
);