[ALSA] Remove xxx_t typedefs: Sequencer
[powerpc.git] / sound / core / seq / seq_device.c
1 /*
2  *  ALSA sequencer device management
3  *  Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  *
20  *----------------------------------------------------------------
21  *
22  * This device handler separates the card driver module from sequencer
23  * stuff (sequencer core, synth drivers, etc), so that user can avoid
24  * to spend unnecessary resources e.g. if he needs only listening to
25  * MP3s.
26  *
27  * The card (or lowlevel) driver creates a sequencer device entry
28  * via snd_seq_device_new().  This is an entry pointer to communicate
29  * with the sequencer device "driver", which is involved with the
30  * actual part to communicate with the sequencer core.
31  * Each sequencer device entry has an id string and the corresponding
32  * driver with the same id is loaded when required.  For example,
33  * lowlevel codes to access emu8000 chip on sbawe card are included in
34  * emu8000-synth module.  To activate this module, the hardware
35  * resources like i/o port are passed via snd_seq_device argument.
36  *
37  */
38
39 #include <sound/driver.h>
40 #include <linux/init.h>
41 #include <sound/core.h>
42 #include <sound/info.h>
43 #include <sound/seq_device.h>
44 #include <sound/seq_kernel.h>
45 #include <sound/initval.h>
46 #include <linux/kmod.h>
47 #include <linux/slab.h>
48
49 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA sequencer device management");
51 MODULE_LICENSE("GPL");
52
53 /* driver state */
54 #define DRIVER_EMPTY            0
55 #define DRIVER_LOADED           (1<<0)
56 #define DRIVER_REQUESTED        (1<<1)
57 #define DRIVER_LOCKED           (1<<2)
58
59 struct ops_list {
60         char id[ID_LEN];        /* driver id */
61         int driver;             /* driver state */
62         int used;               /* reference counter */
63         int argsize;            /* argument size */
64
65         /* operators */
66         struct snd_seq_dev_ops ops;
67
68         /* registred devices */
69         struct list_head dev_list;      /* list of devices */
70         int num_devices;        /* number of associated devices */
71         int num_init_devices;   /* number of initialized devices */
72         struct semaphore reg_mutex;
73
74         struct list_head list;  /* next driver */
75 };
76
77
78 static LIST_HEAD(opslist);
79 static int num_ops;
80 static DECLARE_MUTEX(ops_mutex);
81 static struct snd_info_entry *info_entry = NULL;
82
83 /*
84  * prototypes
85  */
86 static int snd_seq_device_free(struct snd_seq_device *dev);
87 static int snd_seq_device_dev_free(struct snd_device *device);
88 static int snd_seq_device_dev_register(struct snd_device *device);
89 static int snd_seq_device_dev_disconnect(struct snd_device *device);
90 static int snd_seq_device_dev_unregister(struct snd_device *device);
91
92 static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
93 static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
94 static struct ops_list *find_driver(char *id, int create_if_empty);
95 static struct ops_list *create_driver(char *id);
96 static void unlock_driver(struct ops_list *ops);
97 static void remove_drivers(void);
98
99 /*
100  * show all drivers and their status
101  */
102
103 static void snd_seq_device_info(struct snd_info_entry *entry,
104                                 struct snd_info_buffer *buffer)
105 {
106         struct list_head *head;
107
108         down(&ops_mutex);
109         list_for_each(head, &opslist) {
110                 struct ops_list *ops = list_entry(head, struct ops_list, list);
111                 snd_iprintf(buffer, "snd-%s%s%s%s,%d\n",
112                                 ops->id,
113                                 ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""),
114                                 ops->driver & DRIVER_REQUESTED ? ",requested" : "",
115                                 ops->driver & DRIVER_LOCKED ? ",locked" : "",
116                                 ops->num_devices);
117         }
118         up(&ops_mutex); 
119 }
120  
121 /*
122  * load all registered drivers (called from seq_clientmgr.c)
123  */
124
125 #ifdef CONFIG_KMOD
126 /* avoid auto-loading during module_init() */
127 static int snd_seq_in_init;
128 void snd_seq_autoload_lock(void)
129 {
130         snd_seq_in_init++;
131 }
132
133 void snd_seq_autoload_unlock(void)
134 {
135         snd_seq_in_init--;
136 }
137 #endif
138
139 void snd_seq_device_load_drivers(void)
140 {
141 #ifdef CONFIG_KMOD
142         struct list_head *head;
143
144         /* Calling request_module during module_init()
145          * may cause blocking.
146          */
147         if (snd_seq_in_init)
148                 return;
149
150         if (! current->fs->root)
151                 return;
152
153         down(&ops_mutex);
154         list_for_each(head, &opslist) {
155                 struct ops_list *ops = list_entry(head, struct ops_list, list);
156                 if (! (ops->driver & DRIVER_LOADED) &&
157                     ! (ops->driver & DRIVER_REQUESTED)) {
158                         ops->used++;
159                         up(&ops_mutex);
160                         ops->driver |= DRIVER_REQUESTED;
161                         request_module("snd-%s", ops->id);
162                         down(&ops_mutex);
163                         ops->used--;
164                 }
165         }
166         up(&ops_mutex);
167 #endif
168 }
169
170 /*
171  * register a sequencer device
172  * card = card info (NULL allowed)
173  * device = device number (if any)
174  * id = id of driver
175  * result = return pointer (NULL allowed if unnecessary)
176  */
177 int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
178                        struct snd_seq_device **result)
179 {
180         struct snd_seq_device *dev;
181         struct ops_list *ops;
182         int err;
183         static struct snd_device_ops dops = {
184                 .dev_free = snd_seq_device_dev_free,
185                 .dev_register = snd_seq_device_dev_register,
186                 .dev_disconnect = snd_seq_device_dev_disconnect,
187                 .dev_unregister = snd_seq_device_dev_unregister
188         };
189
190         if (result)
191                 *result = NULL;
192
193         snd_assert(id != NULL, return -EINVAL);
194
195         ops = find_driver(id, 1);
196         if (ops == NULL)
197                 return -ENOMEM;
198
199         dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
200         if (dev == NULL) {
201                 unlock_driver(ops);
202                 return -ENOMEM;
203         }
204
205         /* set up device info */
206         dev->card = card;
207         dev->device = device;
208         strlcpy(dev->id, id, sizeof(dev->id));
209         dev->argsize = argsize;
210         dev->status = SNDRV_SEQ_DEVICE_FREE;
211
212         /* add this device to the list */
213         down(&ops->reg_mutex);
214         list_add_tail(&dev->list, &ops->dev_list);
215         ops->num_devices++;
216         up(&ops->reg_mutex);
217
218         unlock_driver(ops);
219         
220         if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
221                 snd_seq_device_free(dev);
222                 return err;
223         }
224         
225         if (result)
226                 *result = dev;
227
228         return 0;
229 }
230
231 /*
232  * free the existing device
233  */
234 static int snd_seq_device_free(struct snd_seq_device *dev)
235 {
236         struct ops_list *ops;
237
238         snd_assert(dev != NULL, return -EINVAL);
239
240         ops = find_driver(dev->id, 0);
241         if (ops == NULL)
242                 return -ENXIO;
243
244         /* remove the device from the list */
245         down(&ops->reg_mutex);
246         list_del(&dev->list);
247         ops->num_devices--;
248         up(&ops->reg_mutex);
249
250         free_device(dev, ops);
251         if (dev->private_free)
252                 dev->private_free(dev);
253         kfree(dev);
254
255         unlock_driver(ops);
256
257         return 0;
258 }
259
260 static int snd_seq_device_dev_free(struct snd_device *device)
261 {
262         struct snd_seq_device *dev = device->device_data;
263         return snd_seq_device_free(dev);
264 }
265
266 /*
267  * register the device
268  */
269 static int snd_seq_device_dev_register(struct snd_device *device)
270 {
271         struct snd_seq_device *dev = device->device_data;
272         struct ops_list *ops;
273
274         ops = find_driver(dev->id, 0);
275         if (ops == NULL)
276                 return -ENOENT;
277
278         /* initialize this device if the corresponding driver was
279          * already loaded
280          */
281         if (ops->driver & DRIVER_LOADED)
282                 init_device(dev, ops);
283
284         unlock_driver(ops);
285         return 0;
286 }
287
288 /*
289  * disconnect the device
290  */
291 static int snd_seq_device_dev_disconnect(struct snd_device *device)
292 {
293         struct snd_seq_device *dev = device->device_data;
294         struct ops_list *ops;
295
296         ops = find_driver(dev->id, 0);
297         if (ops == NULL)
298                 return -ENOENT;
299
300         free_device(dev, ops);
301
302         unlock_driver(ops);
303         return 0;
304 }
305
306 /*
307  * unregister the existing device
308  */
309 static int snd_seq_device_dev_unregister(struct snd_device *device)
310 {
311         struct snd_seq_device *dev = device->device_data;
312         return snd_seq_device_free(dev);
313 }
314
315 /*
316  * register device driver
317  * id = driver id
318  * entry = driver operators - duplicated to each instance
319  */
320 int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
321                                    int argsize)
322 {
323         struct list_head *head;
324         struct ops_list *ops;
325
326         if (id == NULL || entry == NULL ||
327             entry->init_device == NULL || entry->free_device == NULL)
328                 return -EINVAL;
329
330         snd_seq_autoload_lock();
331         ops = find_driver(id, 1);
332         if (ops == NULL) {
333                 snd_seq_autoload_unlock();
334                 return -ENOMEM;
335         }
336         if (ops->driver & DRIVER_LOADED) {
337                 snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
338                 unlock_driver(ops);
339                 snd_seq_autoload_unlock();
340                 return -EBUSY;
341         }
342
343         down(&ops->reg_mutex);
344         /* copy driver operators */
345         ops->ops = *entry;
346         ops->driver |= DRIVER_LOADED;
347         ops->argsize = argsize;
348
349         /* initialize existing devices if necessary */
350         list_for_each(head, &ops->dev_list) {
351                 struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
352                 init_device(dev, ops);
353         }
354         up(&ops->reg_mutex);
355
356         unlock_driver(ops);
357         snd_seq_autoload_unlock();
358
359         return 0;
360 }
361
362
363 /*
364  * create driver record
365  */
366 static struct ops_list * create_driver(char *id)
367 {
368         struct ops_list *ops;
369
370         ops = kmalloc(sizeof(*ops), GFP_KERNEL);
371         if (ops == NULL)
372                 return ops;
373         memset(ops, 0, sizeof(*ops));
374
375         /* set up driver entry */
376         strlcpy(ops->id, id, sizeof(ops->id));
377         init_MUTEX(&ops->reg_mutex);
378         ops->driver = DRIVER_EMPTY;
379         INIT_LIST_HEAD(&ops->dev_list);
380         /* lock this instance */
381         ops->used = 1;
382
383         /* register driver entry */
384         down(&ops_mutex);
385         list_add_tail(&ops->list, &opslist);
386         num_ops++;
387         up(&ops_mutex);
388
389         return ops;
390 }
391
392
393 /*
394  * unregister the specified driver
395  */
396 int snd_seq_device_unregister_driver(char *id)
397 {
398         struct list_head *head;
399         struct ops_list *ops;
400
401         ops = find_driver(id, 0);
402         if (ops == NULL)
403                 return -ENXIO;
404         if (! (ops->driver & DRIVER_LOADED) ||
405             (ops->driver & DRIVER_LOCKED)) {
406                 snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
407                            id, ops->driver);
408                 unlock_driver(ops);
409                 return -EBUSY;
410         }
411
412         /* close and release all devices associated with this driver */
413         down(&ops->reg_mutex);
414         ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
415         list_for_each(head, &ops->dev_list) {
416                 struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
417                 free_device(dev, ops);
418         }
419
420         ops->driver = 0;
421         if (ops->num_init_devices > 0)
422                 snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
423                            ops->num_init_devices);
424         up(&ops->reg_mutex);
425
426         unlock_driver(ops);
427
428         /* remove empty driver entries */
429         remove_drivers();
430
431         return 0;
432 }
433
434
435 /*
436  * remove empty driver entries
437  */
438 static void remove_drivers(void)
439 {
440         struct list_head *head;
441
442         down(&ops_mutex);
443         head = opslist.next;
444         while (head != &opslist) {
445                 struct ops_list *ops = list_entry(head, struct ops_list, list);
446                 if (! (ops->driver & DRIVER_LOADED) &&
447                     ops->used == 0 && ops->num_devices == 0) {
448                         head = head->next;
449                         list_del(&ops->list);
450                         kfree(ops);
451                         num_ops--;
452                 } else
453                         head = head->next;
454         }
455         up(&ops_mutex);
456 }
457
458 /*
459  * initialize the device - call init_device operator
460  */
461 static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
462 {
463         if (! (ops->driver & DRIVER_LOADED))
464                 return 0; /* driver is not loaded yet */
465         if (dev->status != SNDRV_SEQ_DEVICE_FREE)
466                 return 0; /* already initialized */
467         if (ops->argsize != dev->argsize) {
468                 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
469                            dev->name, ops->id, ops->argsize, dev->argsize);
470                 return -EINVAL;
471         }
472         if (ops->ops.init_device(dev) >= 0) {
473                 dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
474                 ops->num_init_devices++;
475         } else {
476                 snd_printk(KERN_ERR "init_device failed: %s: %s\n",
477                            dev->name, dev->id);
478         }
479
480         return 0;
481 }
482
483 /*
484  * release the device - call free_device operator
485  */
486 static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
487 {
488         int result;
489
490         if (! (ops->driver & DRIVER_LOADED))
491                 return 0; /* driver is not loaded yet */
492         if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
493                 return 0; /* not registered */
494         if (ops->argsize != dev->argsize) {
495                 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
496                            dev->name, ops->id, ops->argsize, dev->argsize);
497                 return -EINVAL;
498         }
499         if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
500                 dev->status = SNDRV_SEQ_DEVICE_FREE;
501                 dev->driver_data = NULL;
502                 ops->num_init_devices--;
503         } else {
504                 snd_printk(KERN_ERR "free_device failed: %s: %s\n",
505                            dev->name, dev->id);
506         }
507
508         return 0;
509 }
510
511 /*
512  * find the matching driver with given id
513  */
514 static struct ops_list * find_driver(char *id, int create_if_empty)
515 {
516         struct list_head *head;
517
518         down(&ops_mutex);
519         list_for_each(head, &opslist) {
520                 struct ops_list *ops = list_entry(head, struct ops_list, list);
521                 if (strcmp(ops->id, id) == 0) {
522                         ops->used++;
523                         up(&ops_mutex);
524                         return ops;
525                 }
526         }
527         up(&ops_mutex);
528         if (create_if_empty)
529                 return create_driver(id);
530         return NULL;
531 }
532
533 static void unlock_driver(struct ops_list *ops)
534 {
535         down(&ops_mutex);
536         ops->used--;
537         up(&ops_mutex);
538 }
539
540
541 /*
542  * module part
543  */
544
545 static int __init alsa_seq_device_init(void)
546 {
547         info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
548                                                   snd_seq_root);
549         if (info_entry == NULL)
550                 return -ENOMEM;
551         info_entry->content = SNDRV_INFO_CONTENT_TEXT;
552         info_entry->c.text.read_size = 2048;
553         info_entry->c.text.read = snd_seq_device_info;
554         if (snd_info_register(info_entry) < 0) {
555                 snd_info_free_entry(info_entry);
556                 return -ENOMEM;
557         }
558         return 0;
559 }
560
561 static void __exit alsa_seq_device_exit(void)
562 {
563         remove_drivers();
564         snd_info_unregister(info_entry);
565         if (num_ops)
566                 snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
567 }
568
569 module_init(alsa_seq_device_init)
570 module_exit(alsa_seq_device_exit)
571
572 EXPORT_SYMBOL(snd_seq_device_load_drivers);
573 EXPORT_SYMBOL(snd_seq_device_new);
574 EXPORT_SYMBOL(snd_seq_device_register_driver);
575 EXPORT_SYMBOL(snd_seq_device_unregister_driver);
576 #ifdef CONFIG_KMOD
577 EXPORT_SYMBOL(snd_seq_autoload_lock);
578 EXPORT_SYMBOL(snd_seq_autoload_unlock);
579 #endif