wined3d: Make pixel shader input an array.
[wine/multimedia.git] / server / device.c
blobd5e5e44222ecd6dac38800fcd1a4306047724f4e
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"
31 #include "object.h"
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
36 struct ioctl_call
38 struct object obj; /* object header */
39 struct list dev_entry; /* entry in device queue */
40 struct list mgr_entry; /* entry in manager queue */
41 struct device *device; /* device containing this ioctl */
42 struct thread *thread; /* thread that queued the ioctl */
43 void *user_arg; /* user arg used to identify the request */
44 struct async *async; /* pending async op */
45 ioctl_code_t code; /* ioctl code */
46 unsigned int status; /* resulting status (or STATUS_PENDING) */
47 data_size_t in_size; /* size of input data */
48 void *in_data; /* input data */
49 data_size_t out_size; /* size of output data */
50 void *out_data; /* output data */
53 static void ioctl_call_dump( struct object *obj, int verbose );
54 static int ioctl_call_signaled( struct object *obj, struct thread *thread );
55 static void ioctl_call_destroy( struct object *obj );
57 static const struct object_ops ioctl_call_ops =
59 sizeof(struct ioctl_call), /* size */
60 ioctl_call_dump, /* dump */
61 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 ioctl_call_signaled, /* signaled */
64 no_satisfied, /* satisfied */
65 no_signal, /* signal */
66 no_get_fd, /* get_fd */
67 no_map_access, /* map_access */
68 default_get_sd, /* get_sd */
69 default_set_sd, /* set_sd */
70 no_lookup_name, /* lookup_name */
71 no_open_file, /* open_file */
72 no_close_handle, /* close_handle */
73 ioctl_call_destroy /* destroy */
77 struct device_manager
79 struct object obj; /* object header */
80 struct list devices; /* list of devices */
81 struct list requests; /* list of pending ioctls across all devices */
84 static void device_manager_dump( struct object *obj, int verbose );
85 static int device_manager_signaled( struct object *obj, struct thread *thread );
86 static void device_manager_destroy( struct object *obj );
88 static const struct object_ops device_manager_ops =
90 sizeof(struct device_manager), /* size */
91 device_manager_dump, /* dump */
92 add_queue, /* add_queue */
93 remove_queue, /* remove_queue */
94 device_manager_signaled, /* signaled */
95 no_satisfied, /* satisfied */
96 no_signal, /* signal */
97 no_get_fd, /* get_fd */
98 no_map_access, /* map_access */
99 default_get_sd, /* get_sd */
100 default_set_sd, /* set_sd */
101 no_lookup_name, /* lookup_name */
102 no_open_file, /* open_file */
103 no_close_handle, /* close_handle */
104 device_manager_destroy /* destroy */
108 struct device
110 struct object obj; /* object header */
111 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
112 struct fd *fd; /* file descriptor for ioctl */
113 void *user_ptr; /* opaque ptr for client side */
114 struct list entry; /* entry in device manager list */
115 struct list requests; /* list of pending ioctl requests */
118 static void device_dump( struct object *obj, int verbose );
119 static struct fd *device_get_fd( struct object *obj );
120 static void device_destroy( struct object *obj );
121 static struct object *device_open_file( struct object *obj, unsigned int access,
122 unsigned int sharing, unsigned int options );
123 static enum server_fd_type device_get_fd_type( struct fd *fd );
124 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
125 const void *data, data_size_t size );
127 static const struct object_ops device_ops =
129 sizeof(struct device), /* size */
130 device_dump, /* dump */
131 no_add_queue, /* add_queue */
132 NULL, /* remove_queue */
133 NULL, /* signaled */
134 no_satisfied, /* satisfied */
135 no_signal, /* signal */
136 device_get_fd, /* get_fd */
137 default_fd_map_access, /* map_access */
138 default_get_sd, /* get_sd */
139 default_set_sd, /* set_sd */
140 no_lookup_name, /* lookup_name */
141 device_open_file, /* open_file */
142 no_close_handle, /* close_handle */
143 device_destroy /* destroy */
146 static const struct fd_ops device_fd_ops =
148 default_fd_get_poll_events, /* get_poll_events */
149 default_poll_event, /* poll_event */
150 no_flush, /* flush */
151 device_get_fd_type, /* get_fd_type */
152 device_ioctl, /* ioctl */
153 default_fd_queue_async, /* queue_async */
154 default_fd_reselect_async, /* reselect_async */
155 default_fd_cancel_async /* cancel_async */
159 static void ioctl_call_dump( struct object *obj, int verbose )
161 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
162 fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
165 static int ioctl_call_signaled( struct object *obj, struct thread *thread )
167 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
169 return !ioctl->device; /* device is cleared once the ioctl has completed */
172 static void ioctl_call_destroy( struct object *obj )
174 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
176 free( ioctl->in_data );
177 free( ioctl->out_data );
178 if (ioctl->async)
180 async_terminate( ioctl->async, STATUS_CANCELLED );
181 release_object( ioctl->async );
183 if (ioctl->device) release_object( ioctl->device );
184 release_object( ioctl->thread );
187 static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
188 const void *in_data, data_size_t in_size,
189 data_size_t out_size )
191 struct ioctl_call *ioctl;
193 if ((ioctl = alloc_object( &ioctl_call_ops )))
195 ioctl->device = (struct device *)grab_object( device );
196 ioctl->code = code;
197 ioctl->async = NULL;
198 ioctl->status = STATUS_PENDING;
199 ioctl->in_size = in_size;
200 ioctl->in_data = NULL;
201 ioctl->out_size = out_size;
202 ioctl->out_data = NULL;
204 if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
206 release_object( ioctl );
207 ioctl = NULL;
210 return ioctl;
213 static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
214 const void *out_data, data_size_t out_size )
216 struct device *device = ioctl->device;
218 if (!device) return; /* already finished */
220 /* FIXME: handle the STATUS_PENDING case */
221 ioctl->status = status;
222 ioctl->out_size = min( ioctl->out_size, out_size );
223 if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
224 ioctl->out_size = 0;
225 release_object( device );
226 ioctl->device = NULL;
227 if (ioctl->async)
229 if (ioctl->out_size) status = STATUS_ALERTED;
230 async_terminate( ioctl->async, status );
231 release_object( ioctl->async );
232 ioctl->async = NULL;
234 wake_up( &ioctl->obj, 0 );
236 if (status != STATUS_ALERTED)
238 /* remove it from the device queue */
239 /* (for STATUS_ALERTED this will be done in get_ioctl_result) */
240 list_remove( &ioctl->dev_entry );
241 release_object( ioctl ); /* no longer on the device queue */
246 static void device_dump( struct object *obj, int verbose )
248 struct device *device = (struct device *)obj;
250 fprintf( stderr, "Device " );
251 dump_object_name( &device->obj );
252 fputc( '\n', stderr );
255 static struct fd *device_get_fd( struct object *obj )
257 struct device *device = (struct device *)obj;
259 return (struct fd *)grab_object( device->fd );
262 static void device_destroy( struct object *obj )
264 struct device *device = (struct device *)obj;
265 struct ioctl_call *ioctl, *next;
267 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
269 list_remove( &ioctl->dev_entry );
270 release_object( ioctl ); /* no longer on the device queue */
272 if (device->fd) release_object( device->fd );
273 if (device->manager) list_remove( &device->entry );
276 static struct object *device_open_file( struct object *obj, unsigned int access,
277 unsigned int sharing, unsigned int options )
279 return grab_object( obj );
282 static enum server_fd_type device_get_fd_type( struct fd *fd )
284 return FD_TYPE_DEVICE;
287 static struct ioctl_call *find_ioctl_call( struct device *device, struct thread *thread,
288 void *user_arg )
290 struct ioctl_call *ioctl;
292 LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
293 if (ioctl->thread == thread && ioctl->user_arg == user_arg) return ioctl;
295 set_error( STATUS_INVALID_PARAMETER );
296 return NULL;
299 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
300 const void *data, data_size_t size )
302 struct device *device = get_fd_user( fd );
303 struct ioctl_call *ioctl;
304 obj_handle_t handle;
306 if (!device->manager) /* it has been deleted */
308 set_error( STATUS_FILE_DELETED );
309 return 0;
312 if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
313 return 0;
315 ioctl->thread = (struct thread *)grab_object( current );
316 ioctl->user_arg = async_data->arg;
318 if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
320 release_object( ioctl );
321 return 0;
324 if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
326 close_handle( current->process, handle );
327 release_object( ioctl );
328 return 0;
330 list_add_tail( &device->requests, &ioctl->dev_entry );
331 list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
332 if (list_head( &device->manager->requests ) == &ioctl->mgr_entry) /* first one */
333 wake_up( &device->manager->obj, 0 );
334 /* don't release ioctl since it is now queued in the device */
335 set_error( STATUS_PENDING );
336 return handle;
339 static struct device *create_device( struct directory *root, const struct unicode_str *name,
340 struct device_manager *manager, unsigned int attr )
342 struct device *device;
344 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
346 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
348 /* initialize it if it didn't already exist */
349 device->manager = manager;
350 list_add_tail( &manager->devices, &device->entry );
351 list_init( &device->requests );
352 if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
354 release_object( device );
355 device = NULL;
359 return device;
362 static void delete_device( struct device *device )
364 struct ioctl_call *ioctl, *next;
366 if (!device->manager) return; /* already deleted */
368 /* terminate all pending requests */
369 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
371 list_remove( &ioctl->mgr_entry );
372 set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
374 unlink_named_object( &device->obj );
375 list_remove( &device->entry );
376 device->manager = NULL;
380 static void device_manager_dump( struct object *obj, int verbose )
382 fprintf( stderr, "Device manager\n" );
385 static int device_manager_signaled( struct object *obj, struct thread *thread )
387 struct device_manager *manager = (struct device_manager *)obj;
389 return !list_empty( &manager->requests );
392 static void device_manager_destroy( struct object *obj )
394 struct device_manager *manager = (struct device_manager *)obj;
395 struct list *ptr;
397 while ((ptr = list_head( &manager->devices )))
399 struct device *device = LIST_ENTRY( ptr, struct device, entry );
400 delete_device( device );
404 static struct device_manager *create_device_manager(void)
406 struct device_manager *manager;
408 if ((manager = alloc_object( &device_manager_ops )))
410 list_init( &manager->devices );
411 list_init( &manager->requests );
413 return manager;
417 /* create a device manager */
418 DECL_HANDLER(create_device_manager)
420 struct device_manager *manager = create_device_manager();
422 if (manager)
424 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
425 release_object( manager );
430 /* create a device */
431 DECL_HANDLER(create_device)
433 struct device *device;
434 struct unicode_str name;
435 struct device_manager *manager;
436 struct directory *root = NULL;
438 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
439 0, &device_manager_ops )))
440 return;
442 get_req_unicode_str( &name );
443 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
445 release_object( manager );
446 return;
449 if ((device = create_device( root, &name, manager, req->attributes )))
451 device->user_ptr = req->user_ptr;
452 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
453 release_object( device );
456 if (root) release_object( root );
457 release_object( manager );
461 /* delete a device */
462 DECL_HANDLER(delete_device)
464 struct device *device;
466 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
468 delete_device( device );
469 release_object( device );
474 /* retrieve the next pending device ioctl request */
475 DECL_HANDLER(get_next_device_request)
477 struct ioctl_call *ioctl;
478 struct device_manager *manager;
479 struct list *ptr;
481 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
482 0, &device_manager_ops )))
483 return;
485 if (req->prev)
487 if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
488 0, &ioctl_call_ops )))
490 set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
491 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
492 release_object( ioctl );
494 clear_error();
497 if ((ptr = list_head( &manager->requests )))
499 ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
500 reply->code = ioctl->code;
501 reply->user_ptr = ioctl->device->user_ptr;
502 reply->in_size = ioctl->in_size;
503 reply->out_size = ioctl->out_size;
504 if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
505 else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
507 set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
508 ioctl->in_data = NULL;
509 ioctl->in_size = 0;
510 list_remove( &ioctl->mgr_entry );
511 list_init( &ioctl->mgr_entry );
514 else set_error( STATUS_PENDING );
516 release_object( manager );
520 /* retrieve results of an async ioctl */
521 DECL_HANDLER(get_ioctl_result)
523 struct device *device;
524 struct ioctl_call *ioctl;
526 if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
527 return;
529 if ((ioctl = find_ioctl_call( device, current, req->user_arg )))
531 if (ioctl->out_data)
533 data_size_t size = min( ioctl->out_size, get_reply_max_size() );
534 if (size)
536 set_reply_data_ptr( ioctl->out_data, size );
537 ioctl->out_data = NULL;
540 set_error( ioctl->status );
541 list_remove( &ioctl->dev_entry );
542 release_object( ioctl ); /* no longer on the device queue */
544 release_object( device );