winealsa.drv: Exclude unused headers.
[wine] / server / device.c
1 /*
2  * Server-side device support
3  *
4  * Copyright (C) 2007 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30
31 #include "object.h"
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
35
36 struct ioctl_call
37 {
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 */
51 };
52
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 );
56
57 static const struct object_ops ioctl_call_ops =
58 {
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     no_lookup_name,                   /* lookup_name */
69     no_open_file,                     /* open_file */
70     no_close_handle,                  /* close_handle */
71     ioctl_call_destroy                /* destroy */
72 };
73
74
75 struct device_manager
76 {
77     struct object          obj;           /* object header */
78     struct list            devices;       /* list of devices */
79     struct list            requests;      /* list of pending ioctls across all devices */
80 };
81
82 static void device_manager_dump( struct object *obj, int verbose );
83 static int device_manager_signaled( struct object *obj, struct thread *thread );
84 static void device_manager_destroy( struct object *obj );
85
86 static const struct object_ops device_manager_ops =
87 {
88     sizeof(struct device_manager),    /* size */
89     device_manager_dump,              /* dump */
90     add_queue,                        /* add_queue */
91     remove_queue,                     /* remove_queue */
92     device_manager_signaled,          /* signaled */
93     no_satisfied,                     /* satisfied */
94     no_signal,                        /* signal */
95     no_get_fd,                        /* get_fd */
96     no_map_access,                    /* map_access */
97     no_lookup_name,                   /* lookup_name */
98     no_open_file,                     /* open_file */
99     no_close_handle,                  /* close_handle */
100     device_manager_destroy            /* destroy */
101 };
102
103
104 struct device
105 {
106     struct object          obj;           /* object header */
107     struct device_manager *manager;       /* manager for this device (or NULL if deleted) */
108     struct fd             *fd;            /* file descriptor for ioctl */
109     void                  *user_ptr;      /* opaque ptr for client side */
110     struct list            entry;         /* entry in device manager list */
111     struct list            requests;      /* list of pending ioctl requests */
112 };
113
114 static void device_dump( struct object *obj, int verbose );
115 static struct fd *device_get_fd( struct object *obj );
116 static void device_destroy( struct object *obj );
117 static struct object *device_open_file( struct object *obj, unsigned int access,
118                                         unsigned int sharing, unsigned int options );
119 static enum server_fd_type device_get_fd_type( struct fd *fd );
120 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
121                                   const void *data, data_size_t size );
122
123 static const struct object_ops device_ops =
124 {
125     sizeof(struct device),            /* size */
126     device_dump,                      /* dump */
127     no_add_queue,                     /* add_queue */
128     NULL,                             /* remove_queue */
129     NULL,                             /* signaled */
130     no_satisfied,                     /* satisfied */
131     no_signal,                        /* signal */
132     device_get_fd,                    /* get_fd */
133     no_map_access,                    /* map_access */
134     no_lookup_name,                   /* lookup_name */
135     device_open_file,                 /* open_file */
136     no_close_handle,                  /* close_handle */
137     device_destroy                    /* destroy */
138 };
139
140 static const struct fd_ops device_fd_ops =
141 {
142     default_fd_get_poll_events,       /* get_poll_events */
143     default_poll_event,               /* poll_event */
144     no_flush,                         /* flush */
145     device_get_fd_type,               /* get_fd_type */
146     device_ioctl,                     /* ioctl */
147     default_fd_queue_async,           /* queue_async */
148     default_fd_reselect_async,        /* reselect_async */
149     default_fd_cancel_async           /* cancel_async */
150 };
151
152
153 static void ioctl_call_dump( struct object *obj, int verbose )
154 {
155     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
156     fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
157 }
158
159 static int ioctl_call_signaled( struct object *obj, struct thread *thread )
160 {
161     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
162
163     return !ioctl->device;  /* device is cleared once the ioctl has completed */
164 }
165
166 static void ioctl_call_destroy( struct object *obj )
167 {
168     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
169
170     free( ioctl->in_data );
171     free( ioctl->out_data );
172     if (ioctl->async)
173     {
174         async_terminate( ioctl->async, STATUS_CANCELLED );
175         release_object( ioctl->async );
176     }
177     if (ioctl->device) release_object( ioctl->device );
178     release_object( ioctl->thread );
179 }
180
181 static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
182                                         const void *in_data, data_size_t in_size,
183                                         data_size_t out_size )
184 {
185     struct ioctl_call *ioctl;
186
187     if ((ioctl = alloc_object( &ioctl_call_ops )))
188     {
189         ioctl->device   = (struct device *)grab_object( device );
190         ioctl->code     = code;
191         ioctl->async    = NULL;
192         ioctl->status   = STATUS_PENDING;
193         ioctl->in_size  = in_size;
194         ioctl->in_data  = NULL;
195         ioctl->out_size = out_size;
196         ioctl->out_data = NULL;
197
198         if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
199         {
200             release_object( ioctl );
201             ioctl = NULL;
202         }
203     }
204     return ioctl;
205 }
206
207 static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
208                               const void *out_data, data_size_t out_size )
209 {
210     struct device *device = ioctl->device;
211
212     if (!device) return;  /* already finished */
213
214     /* FIXME: handle the STATUS_PENDING case */
215     ioctl->status = status;
216     ioctl->out_size = min( ioctl->out_size, out_size );
217     if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
218         ioctl->out_size = 0;
219     release_object( device );
220     ioctl->device = NULL;
221     if (ioctl->async)
222     {
223         async_terminate( ioctl->async, ioctl->out_size ? STATUS_ALERTED : status );
224         release_object( ioctl->async );
225         ioctl->async = NULL;
226     }
227     wake_up( &ioctl->obj, 0 );
228 }
229
230
231 static void device_dump( struct object *obj, int verbose )
232 {
233     struct device *device = (struct device *)obj;
234
235     fprintf( stderr, "Device " );
236     dump_object_name( &device->obj );
237     fputc( '\n', stderr );
238 }
239
240 static struct fd *device_get_fd( struct object *obj )
241 {
242     struct device *device = (struct device *)obj;
243
244     return (struct fd *)grab_object( device->fd );
245 }
246
247 static void device_destroy( struct object *obj )
248 {
249     struct device *device = (struct device *)obj;
250     struct ioctl_call *ioctl, *next;
251
252     LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
253     {
254         list_remove( &ioctl->dev_entry );
255         release_object( ioctl );  /* no longer on the device queue */
256     }
257     if (device->fd) release_object( device->fd );
258     if (device->manager) list_remove( &device->entry );
259 }
260
261 static struct object *device_open_file( struct object *obj, unsigned int access,
262                                         unsigned int sharing, unsigned int options )
263 {
264     return grab_object( obj );
265 }
266
267 static enum server_fd_type device_get_fd_type( struct fd *fd )
268 {
269     return FD_TYPE_DEVICE;
270 }
271
272 static struct ioctl_call *find_ioctl_call( struct device *device, struct thread *thread,
273                                            void *user_arg )
274 {
275     struct ioctl_call *ioctl;
276
277     LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
278         if (ioctl->thread == thread && ioctl->user_arg == user_arg) return ioctl;
279
280     set_error( STATUS_INVALID_PARAMETER );
281     return NULL;
282 }
283
284 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
285                                   const void *data, data_size_t size )
286 {
287     struct device *device = get_fd_user( fd );
288     struct ioctl_call *ioctl;
289     obj_handle_t handle;
290
291     if (!device->manager)  /* it has been deleted */
292     {
293         set_error( STATUS_FILE_DELETED );
294         return 0;
295     }
296
297     if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
298         return 0;
299
300     ioctl->thread   = (struct thread *)grab_object( current );
301     ioctl->user_arg = async_data->arg;
302
303     if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
304     {
305         release_object( ioctl );
306         return 0;
307     }
308
309     if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
310     {
311         close_handle( current->process, handle );
312         release_object( ioctl );
313         return 0;
314     }
315     list_add_tail( &device->requests, &ioctl->dev_entry );
316     list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
317     if (list_head( &device->manager->requests ) == &ioctl->mgr_entry)  /* first one */
318         wake_up( &device->manager->obj, 0 );
319     /* don't release ioctl since it is now queued in the device */
320     set_error( STATUS_PENDING );
321     return handle;
322 }
323
324 static struct device *create_device( struct directory *root, const struct unicode_str *name,
325                                      struct device_manager *manager, unsigned int attr )
326 {
327     struct device *device;
328
329     if ((device = create_named_object_dir( root, name, attr, &device_ops )))
330     {
331         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
332         {
333             /* initialize it if it didn't already exist */
334             device->manager = manager;
335             list_add_tail( &manager->devices, &device->entry );
336             list_init( &device->requests );
337             if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
338             {
339                 release_object( device );
340                 device = NULL;
341             }
342         }
343     }
344     return device;
345 }
346
347 static void delete_device( struct device *device )
348 {
349     struct ioctl_call *ioctl;
350
351     if (!device->manager) return;  /* already deleted */
352
353     /* terminate all pending requests */
354     LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
355     {
356         list_remove( &ioctl->mgr_entry );
357         set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
358     }
359     unlink_named_object( &device->obj );
360     list_remove( &device->entry );
361     device->manager = NULL;
362 }
363
364
365 static void device_manager_dump( struct object *obj, int verbose )
366 {
367     fprintf( stderr, "Device manager\n" );
368 }
369
370 static int device_manager_signaled( struct object *obj, struct thread *thread )
371 {
372     struct device_manager *manager = (struct device_manager *)obj;
373
374     return !list_empty( &manager->requests );
375 }
376
377 static void device_manager_destroy( struct object *obj )
378 {
379     struct device_manager *manager = (struct device_manager *)obj;
380     struct list *ptr;
381
382     while ((ptr = list_head( &manager->devices )))
383     {
384         struct device *device = LIST_ENTRY( ptr, struct device, entry );
385         delete_device( device );
386     }
387 }
388
389 static struct device_manager *create_device_manager(void)
390 {
391     struct device_manager *manager;
392
393     if ((manager = alloc_object( &device_manager_ops )))
394     {
395         list_init( &manager->devices );
396         list_init( &manager->requests );
397     }
398     return manager;
399 }
400
401
402 /* create a device manager */
403 DECL_HANDLER(create_device_manager)
404 {
405     struct device_manager *manager = create_device_manager();
406
407     if (manager)
408     {
409         reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
410         release_object( manager );
411     }
412 }
413
414
415 /* create a device */
416 DECL_HANDLER(create_device)
417 {
418     struct device *device;
419     struct unicode_str name;
420     struct device_manager *manager;
421     struct directory *root = NULL;
422
423     if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
424                                                              0, &device_manager_ops )))
425         return;
426
427     get_req_unicode_str( &name );
428     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
429     {
430         release_object( manager );
431         return;
432     }
433
434     if ((device = create_device( root, &name, manager, req->attributes )))
435     {
436         device->user_ptr = req->user_ptr;
437         reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
438         release_object( device );
439     }
440
441     if (root) release_object( root );
442     release_object( manager );
443 }
444
445
446 /* delete a device */
447 DECL_HANDLER(delete_device)
448 {
449     struct device *device;
450
451     if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
452     {
453         delete_device( device );
454         release_object( device );
455     }
456 }
457
458
459 /* retrieve the next pending device ioctl request */
460 DECL_HANDLER(get_next_device_request)
461 {
462     struct ioctl_call *ioctl;
463     struct device_manager *manager;
464     struct list *ptr;
465
466     if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
467                                                              0, &device_manager_ops )))
468         return;
469
470     if (req->prev)
471     {
472         if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
473                                                           0, &ioctl_call_ops )))
474         {
475             set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
476             close_handle( current->process, req->prev );  /* avoid an extra round-trip for close */
477             release_object( ioctl );
478         }
479         clear_error();
480     }
481
482     if ((ptr = list_head( &manager->requests )))
483     {
484         ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
485         reply->code = ioctl->code;
486         reply->user_ptr = ioctl->device->user_ptr;
487         reply->in_size = ioctl->in_size;
488         reply->out_size = ioctl->out_size;
489         if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
490         else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
491         {
492             set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
493             ioctl->in_data = NULL;
494             ioctl->in_size = 0;
495             list_remove( &ioctl->mgr_entry );
496             list_init( &ioctl->mgr_entry );
497         }
498     }
499     else set_error( STATUS_PENDING );
500
501     release_object( manager );
502 }
503
504
505 /* retrieve results of an async ioctl */
506 DECL_HANDLER(get_ioctl_result)
507 {
508     struct device *device;
509     struct ioctl_call *ioctl;
510
511     if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
512         return;
513
514     if ((ioctl = find_ioctl_call( device, current, req->user_arg )))
515     {
516         if (ioctl->out_data)
517         {
518             data_size_t size = min( ioctl->out_size, get_reply_max_size() );
519             if (size)
520             {
521                 set_reply_data_ptr( ioctl->out_data, size );
522                 ioctl->out_data = NULL;
523             }
524         }
525         set_error( ioctl->status );
526         list_remove( &ioctl->dev_entry );
527         release_object( ioctl );  /* no longer on the device queue */
528     }
529     release_object( device );
530 }