2 * ALSA sequencer device management
3 * Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
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.
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.
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
20 *----------------------------------------------------------------
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
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.
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 #include <linux/mutex.h>
50 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA sequencer device management");
52 MODULE_LICENSE("GPL");
55 #define DRIVER_EMPTY 0
56 #define DRIVER_LOADED (1<<0)
57 #define DRIVER_REQUESTED (1<<1)
58 #define DRIVER_LOCKED (1<<2)
61 char id[ID_LEN]; /* driver id */
62 int driver; /* driver state */
63 int used; /* reference counter */
64 int argsize; /* argument size */
67 struct snd_seq_dev_ops ops;
69 /* registred devices */
70 struct list_head dev_list; /* list of devices */
71 int num_devices; /* number of associated devices */
72 int num_init_devices; /* number of initialized devices */
73 struct mutex reg_mutex;
75 struct list_head list; /* next driver */
79 static LIST_HEAD(opslist);
81 static DEFINE_MUTEX(ops_mutex);
83 static struct snd_info_entry *info_entry;
89 static int snd_seq_device_free(struct snd_seq_device *dev);
90 static int snd_seq_device_dev_free(struct snd_device *device);
91 static int snd_seq_device_dev_register(struct snd_device *device);
92 static int snd_seq_device_dev_disconnect(struct snd_device *device);
94 static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
95 static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
96 static struct ops_list *find_driver(char *id, int create_if_empty);
97 static struct ops_list *create_driver(char *id);
98 static void unlock_driver(struct ops_list *ops);
99 static void remove_drivers(void);
102 * show all drivers and their status
105 #ifdef CONFIG_PROC_FS
106 static void snd_seq_device_info(struct snd_info_entry *entry,
107 struct snd_info_buffer *buffer)
109 struct list_head *head;
111 mutex_lock(&ops_mutex);
112 list_for_each(head, &opslist) {
113 struct ops_list *ops = list_entry(head, struct ops_list, list);
114 snd_iprintf(buffer, "snd-%s%s%s%s,%d\n",
116 ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""),
117 ops->driver & DRIVER_REQUESTED ? ",requested" : "",
118 ops->driver & DRIVER_LOCKED ? ",locked" : "",
121 mutex_unlock(&ops_mutex);
126 * load all registered drivers (called from seq_clientmgr.c)
130 /* avoid auto-loading during module_init() */
131 static int snd_seq_in_init;
132 void snd_seq_autoload_lock(void)
137 void snd_seq_autoload_unlock(void)
143 void snd_seq_device_load_drivers(void)
146 struct list_head *head;
148 /* Calling request_module during module_init()
149 * may cause blocking.
154 if (! current->fs->root)
157 mutex_lock(&ops_mutex);
158 list_for_each(head, &opslist) {
159 struct ops_list *ops = list_entry(head, struct ops_list, list);
160 if (! (ops->driver & DRIVER_LOADED) &&
161 ! (ops->driver & DRIVER_REQUESTED)) {
163 mutex_unlock(&ops_mutex);
164 ops->driver |= DRIVER_REQUESTED;
165 request_module("snd-%s", ops->id);
166 mutex_lock(&ops_mutex);
170 mutex_unlock(&ops_mutex);
175 * register a sequencer device
176 * card = card info (NULL allowed)
177 * device = device number (if any)
179 * result = return pointer (NULL allowed if unnecessary)
181 int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
182 struct snd_seq_device **result)
184 struct snd_seq_device *dev;
185 struct ops_list *ops;
187 static struct snd_device_ops dops = {
188 .dev_free = snd_seq_device_dev_free,
189 .dev_register = snd_seq_device_dev_register,
190 .dev_disconnect = snd_seq_device_dev_disconnect,
196 snd_assert(id != NULL, return -EINVAL);
198 ops = find_driver(id, 1);
202 dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
208 /* set up device info */
210 dev->device = device;
211 strlcpy(dev->id, id, sizeof(dev->id));
212 dev->argsize = argsize;
213 dev->status = SNDRV_SEQ_DEVICE_FREE;
215 /* add this device to the list */
216 mutex_lock(&ops->reg_mutex);
217 list_add_tail(&dev->list, &ops->dev_list);
219 mutex_unlock(&ops->reg_mutex);
223 if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
224 snd_seq_device_free(dev);
235 * free the existing device
237 static int snd_seq_device_free(struct snd_seq_device *dev)
239 struct ops_list *ops;
241 snd_assert(dev != NULL, return -EINVAL);
243 ops = find_driver(dev->id, 0);
247 /* remove the device from the list */
248 mutex_lock(&ops->reg_mutex);
249 list_del(&dev->list);
251 mutex_unlock(&ops->reg_mutex);
253 free_device(dev, ops);
254 if (dev->private_free)
255 dev->private_free(dev);
263 static int snd_seq_device_dev_free(struct snd_device *device)
265 struct snd_seq_device *dev = device->device_data;
266 return snd_seq_device_free(dev);
270 * register the device
272 static int snd_seq_device_dev_register(struct snd_device *device)
274 struct snd_seq_device *dev = device->device_data;
275 struct ops_list *ops;
277 ops = find_driver(dev->id, 0);
281 /* initialize this device if the corresponding driver was
284 if (ops->driver & DRIVER_LOADED)
285 init_device(dev, ops);
292 * disconnect the device
294 static int snd_seq_device_dev_disconnect(struct snd_device *device)
296 struct snd_seq_device *dev = device->device_data;
297 struct ops_list *ops;
299 ops = find_driver(dev->id, 0);
303 free_device(dev, ops);
310 * register device driver
312 * entry = driver operators - duplicated to each instance
314 int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
317 struct list_head *head;
318 struct ops_list *ops;
320 if (id == NULL || entry == NULL ||
321 entry->init_device == NULL || entry->free_device == NULL)
324 snd_seq_autoload_lock();
325 ops = find_driver(id, 1);
327 snd_seq_autoload_unlock();
330 if (ops->driver & DRIVER_LOADED) {
331 snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
333 snd_seq_autoload_unlock();
337 mutex_lock(&ops->reg_mutex);
338 /* copy driver operators */
340 ops->driver |= DRIVER_LOADED;
341 ops->argsize = argsize;
343 /* initialize existing devices if necessary */
344 list_for_each(head, &ops->dev_list) {
345 struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
346 init_device(dev, ops);
348 mutex_unlock(&ops->reg_mutex);
351 snd_seq_autoload_unlock();
358 * create driver record
360 static struct ops_list * create_driver(char *id)
362 struct ops_list *ops;
364 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
368 /* set up driver entry */
369 strlcpy(ops->id, id, sizeof(ops->id));
370 mutex_init(&ops->reg_mutex);
372 * The ->reg_mutex locking rules are per-driver, so we create
373 * separate per-driver lock classes:
375 lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
377 ops->driver = DRIVER_EMPTY;
378 INIT_LIST_HEAD(&ops->dev_list);
379 /* lock this instance */
382 /* register driver entry */
383 mutex_lock(&ops_mutex);
384 list_add_tail(&ops->list, &opslist);
386 mutex_unlock(&ops_mutex);
393 * unregister the specified driver
395 int snd_seq_device_unregister_driver(char *id)
397 struct list_head *head;
398 struct ops_list *ops;
400 ops = find_driver(id, 0);
403 if (! (ops->driver & DRIVER_LOADED) ||
404 (ops->driver & DRIVER_LOCKED)) {
405 snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
411 /* close and release all devices associated with this driver */
412 mutex_lock(&ops->reg_mutex);
413 ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
414 list_for_each(head, &ops->dev_list) {
415 struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
416 free_device(dev, ops);
420 if (ops->num_init_devices > 0)
421 snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
422 ops->num_init_devices);
423 mutex_unlock(&ops->reg_mutex);
427 /* remove empty driver entries */
435 * remove empty driver entries
437 static void remove_drivers(void)
439 struct list_head *head;
441 mutex_lock(&ops_mutex);
443 while (head != &opslist) {
444 struct ops_list *ops = list_entry(head, struct ops_list, list);
445 if (! (ops->driver & DRIVER_LOADED) &&
446 ops->used == 0 && ops->num_devices == 0) {
448 list_del(&ops->list);
454 mutex_unlock(&ops_mutex);
458 * initialize the device - call init_device operator
460 static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
462 if (! (ops->driver & DRIVER_LOADED))
463 return 0; /* driver is not loaded yet */
464 if (dev->status != SNDRV_SEQ_DEVICE_FREE)
465 return 0; /* already initialized */
466 if (ops->argsize != dev->argsize) {
467 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
468 dev->name, ops->id, ops->argsize, dev->argsize);
471 if (ops->ops.init_device(dev) >= 0) {
472 dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
473 ops->num_init_devices++;
475 snd_printk(KERN_ERR "init_device failed: %s: %s\n",
483 * release the device - call free_device operator
485 static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
489 if (! (ops->driver & DRIVER_LOADED))
490 return 0; /* driver is not loaded yet */
491 if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
492 return 0; /* not registered */
493 if (ops->argsize != dev->argsize) {
494 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
495 dev->name, ops->id, ops->argsize, dev->argsize);
498 if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
499 dev->status = SNDRV_SEQ_DEVICE_FREE;
500 dev->driver_data = NULL;
501 ops->num_init_devices--;
503 snd_printk(KERN_ERR "free_device failed: %s: %s\n",
511 * find the matching driver with given id
513 static struct ops_list * find_driver(char *id, int create_if_empty)
515 struct list_head *head;
517 mutex_lock(&ops_mutex);
518 list_for_each(head, &opslist) {
519 struct ops_list *ops = list_entry(head, struct ops_list, list);
520 if (strcmp(ops->id, id) == 0) {
522 mutex_unlock(&ops_mutex);
526 mutex_unlock(&ops_mutex);
528 return create_driver(id);
532 static void unlock_driver(struct ops_list *ops)
534 mutex_lock(&ops_mutex);
536 mutex_unlock(&ops_mutex);
544 static int __init alsa_seq_device_init(void)
546 #ifdef CONFIG_PROC_FS
547 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
549 if (info_entry == NULL)
551 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
552 info_entry->c.text.read = snd_seq_device_info;
553 if (snd_info_register(info_entry) < 0) {
554 snd_info_free_entry(info_entry);
561 static void __exit alsa_seq_device_exit(void)
564 #ifdef CONFIG_PROC_FS
565 snd_info_free_entry(info_entry);
568 snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
571 module_init(alsa_seq_device_init)
572 module_exit(alsa_seq_device_exit)
574 EXPORT_SYMBOL(snd_seq_device_load_drivers);
575 EXPORT_SYMBOL(snd_seq_device_new);
576 EXPORT_SYMBOL(snd_seq_device_register_driver);
577 EXPORT_SYMBOL(snd_seq_device_unregister_driver);
579 EXPORT_SYMBOL(snd_seq_autoload_lock);
580 EXPORT_SYMBOL(snd_seq_autoload_unlock);