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 <linux/init.h>
40 #include <linux/module.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)
59 #define DRIVER_REQUESTING (1<<3)
62 char id[ID_LEN]; /* driver id */
63 int driver; /* driver state */
64 int used; /* reference counter */
65 int argsize; /* argument size */
68 struct snd_seq_dev_ops ops;
70 /* registered devices */
71 struct list_head dev_list; /* list of devices */
72 int num_devices; /* number of associated devices */
73 int num_init_devices; /* number of initialized devices */
74 struct mutex reg_mutex;
76 struct list_head list; /* next driver */
80 static LIST_HEAD(opslist);
82 static DEFINE_MUTEX(ops_mutex);
84 static struct snd_info_entry *info_entry;
90 static int snd_seq_device_free(struct snd_seq_device *dev);
91 static int snd_seq_device_dev_free(struct snd_device *device);
92 static int snd_seq_device_dev_register(struct snd_device *device);
93 static int snd_seq_device_dev_disconnect(struct snd_device *device);
95 static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
96 static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
97 static struct ops_list *find_driver(char *id, int create_if_empty);
98 static struct ops_list *create_driver(char *id);
99 static void unlock_driver(struct ops_list *ops);
100 static void remove_drivers(void);
103 * show all drivers and their status
106 #ifdef CONFIG_PROC_FS
107 static void snd_seq_device_info(struct snd_info_entry *entry,
108 struct snd_info_buffer *buffer)
110 struct ops_list *ops;
112 mutex_lock(&ops_mutex);
113 list_for_each_entry(ops, &opslist, 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)
129 #ifdef CONFIG_MODULES
130 /* avoid auto-loading during module_init() */
131 static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
132 void snd_seq_autoload_lock(void)
134 atomic_inc(&snd_seq_in_init);
137 void snd_seq_autoload_unlock(void)
139 atomic_dec(&snd_seq_in_init);
142 static void autoload_drivers(void)
144 /* avoid reentrance */
145 if (atomic_inc_return(&snd_seq_in_init) == 1) {
146 struct ops_list *ops;
148 mutex_lock(&ops_mutex);
149 list_for_each_entry(ops, &opslist, list) {
150 if ((ops->driver & DRIVER_REQUESTING) &&
151 !(ops->driver & DRIVER_REQUESTED)) {
153 mutex_unlock(&ops_mutex);
154 ops->driver |= DRIVER_REQUESTED;
155 request_module("snd-%s", ops->id);
156 mutex_lock(&ops_mutex);
160 mutex_unlock(&ops_mutex);
162 atomic_dec(&snd_seq_in_init);
165 static void call_autoload(struct work_struct *work)
170 static DECLARE_WORK(autoload_work, call_autoload);
172 static void try_autoload(struct ops_list *ops)
175 ops->driver |= DRIVER_REQUESTING;
176 schedule_work(&autoload_work);
180 static void queue_autoload_drivers(void)
182 struct ops_list *ops;
184 mutex_lock(&ops_mutex);
185 list_for_each_entry(ops, &opslist, list)
187 mutex_unlock(&ops_mutex);
190 void snd_seq_autoload_init(void)
192 atomic_dec(&snd_seq_in_init);
193 #ifdef CONFIG_SND_SEQUENCER_MODULE
194 /* initial autoload only when snd-seq is a module */
195 queue_autoload_drivers();
199 #define try_autoload(ops) /* NOP */
202 void snd_seq_device_load_drivers(void)
204 #ifdef CONFIG_MODULES
205 queue_autoload_drivers();
206 flush_work(&autoload_work);
211 * register a sequencer device
213 * device = device number (if any)
215 * result = return pointer (NULL allowed if unnecessary)
217 int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
218 struct snd_seq_device **result)
220 struct snd_seq_device *dev;
221 struct ops_list *ops;
223 static struct snd_device_ops dops = {
224 .dev_free = snd_seq_device_dev_free,
225 .dev_register = snd_seq_device_dev_register,
226 .dev_disconnect = snd_seq_device_dev_disconnect,
235 ops = find_driver(id, 1);
239 dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
245 /* set up device info */
247 dev->device = device;
248 strlcpy(dev->id, id, sizeof(dev->id));
249 dev->argsize = argsize;
250 dev->status = SNDRV_SEQ_DEVICE_FREE;
252 /* add this device to the list */
253 mutex_lock(&ops->reg_mutex);
254 list_add_tail(&dev->list, &ops->dev_list);
256 mutex_unlock(&ops->reg_mutex);
258 if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
259 snd_seq_device_free(dev);
273 * free the existing device
275 static int snd_seq_device_free(struct snd_seq_device *dev)
277 struct ops_list *ops;
279 if (snd_BUG_ON(!dev))
282 ops = find_driver(dev->id, 0);
286 /* remove the device from the list */
287 mutex_lock(&ops->reg_mutex);
288 list_del(&dev->list);
290 mutex_unlock(&ops->reg_mutex);
292 free_device(dev, ops);
293 if (dev->private_free)
294 dev->private_free(dev);
302 static int snd_seq_device_dev_free(struct snd_device *device)
304 struct snd_seq_device *dev = device->device_data;
305 return snd_seq_device_free(dev);
309 * register the device
311 static int snd_seq_device_dev_register(struct snd_device *device)
313 struct snd_seq_device *dev = device->device_data;
314 struct ops_list *ops;
316 ops = find_driver(dev->id, 0);
320 /* initialize this device if the corresponding driver was
323 if (ops->driver & DRIVER_LOADED)
324 init_device(dev, ops);
331 * disconnect the device
333 static int snd_seq_device_dev_disconnect(struct snd_device *device)
335 struct snd_seq_device *dev = device->device_data;
336 struct ops_list *ops;
338 ops = find_driver(dev->id, 0);
342 free_device(dev, ops);
349 * register device driver
351 * entry = driver operators - duplicated to each instance
353 int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
356 struct ops_list *ops;
357 struct snd_seq_device *dev;
359 if (id == NULL || entry == NULL ||
360 entry->init_device == NULL || entry->free_device == NULL)
363 ops = find_driver(id, 1);
366 if (ops->driver & DRIVER_LOADED) {
367 pr_warn("ALSA: seq: driver_register: driver '%s' already exists\n", id);
372 mutex_lock(&ops->reg_mutex);
373 /* copy driver operators */
375 ops->driver |= DRIVER_LOADED;
376 ops->argsize = argsize;
378 /* initialize existing devices if necessary */
379 list_for_each_entry(dev, &ops->dev_list, list) {
380 init_device(dev, ops);
382 mutex_unlock(&ops->reg_mutex);
391 * create driver record
393 static struct ops_list * create_driver(char *id)
395 struct ops_list *ops;
397 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
401 /* set up driver entry */
402 strlcpy(ops->id, id, sizeof(ops->id));
403 mutex_init(&ops->reg_mutex);
405 * The ->reg_mutex locking rules are per-driver, so we create
406 * separate per-driver lock classes:
408 lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
410 ops->driver = DRIVER_EMPTY;
411 INIT_LIST_HEAD(&ops->dev_list);
412 /* lock this instance */
415 /* register driver entry */
416 mutex_lock(&ops_mutex);
417 list_add_tail(&ops->list, &opslist);
419 mutex_unlock(&ops_mutex);
426 * unregister the specified driver
428 int snd_seq_device_unregister_driver(char *id)
430 struct ops_list *ops;
431 struct snd_seq_device *dev;
433 ops = find_driver(id, 0);
436 if (! (ops->driver & DRIVER_LOADED) ||
437 (ops->driver & DRIVER_LOCKED)) {
438 pr_err("ALSA: seq: driver_unregister: cannot unload driver '%s': status=%x\n",
444 /* close and release all devices associated with this driver */
445 mutex_lock(&ops->reg_mutex);
446 ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
447 list_for_each_entry(dev, &ops->dev_list, list) {
448 free_device(dev, ops);
452 if (ops->num_init_devices > 0)
453 pr_err("ALSA: seq: free_driver: init_devices > 0!! (%d)\n",
454 ops->num_init_devices);
455 mutex_unlock(&ops->reg_mutex);
459 /* remove empty driver entries */
467 * remove empty driver entries
469 static void remove_drivers(void)
471 struct list_head *head;
473 mutex_lock(&ops_mutex);
475 while (head != &opslist) {
476 struct ops_list *ops = list_entry(head, struct ops_list, list);
477 if (! (ops->driver & DRIVER_LOADED) &&
478 ops->used == 0 && ops->num_devices == 0) {
480 list_del(&ops->list);
486 mutex_unlock(&ops_mutex);
490 * initialize the device - call init_device operator
492 static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
494 if (! (ops->driver & DRIVER_LOADED))
495 return 0; /* driver is not loaded yet */
496 if (dev->status != SNDRV_SEQ_DEVICE_FREE)
497 return 0; /* already initialized */
498 if (ops->argsize != dev->argsize) {
499 pr_err("ALSA: seq: incompatible device '%s' for plug-in '%s' (%d %d)\n",
500 dev->name, ops->id, ops->argsize, dev->argsize);
503 if (ops->ops.init_device(dev) >= 0) {
504 dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
505 ops->num_init_devices++;
507 pr_err("ALSA: seq: init_device failed: %s: %s\n",
515 * release the device - call free_device operator
517 static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
521 if (! (ops->driver & DRIVER_LOADED))
522 return 0; /* driver is not loaded yet */
523 if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
524 return 0; /* not registered */
525 if (ops->argsize != dev->argsize) {
526 pr_err("ALSA: seq: incompatible device '%s' for plug-in '%s' (%d %d)\n",
527 dev->name, ops->id, ops->argsize, dev->argsize);
530 if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
531 dev->status = SNDRV_SEQ_DEVICE_FREE;
532 dev->driver_data = NULL;
533 ops->num_init_devices--;
535 pr_err("ALSA: seq: free_device failed: %s: %s\n",
543 * find the matching driver with given id
545 static struct ops_list * find_driver(char *id, int create_if_empty)
547 struct ops_list *ops;
549 mutex_lock(&ops_mutex);
550 list_for_each_entry(ops, &opslist, list) {
551 if (strcmp(ops->id, id) == 0) {
553 mutex_unlock(&ops_mutex);
557 mutex_unlock(&ops_mutex);
559 return create_driver(id);
563 static void unlock_driver(struct ops_list *ops)
565 mutex_lock(&ops_mutex);
567 mutex_unlock(&ops_mutex);
575 static int __init alsa_seq_device_init(void)
577 #ifdef CONFIG_PROC_FS
578 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
580 if (info_entry == NULL)
582 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
583 info_entry->c.text.read = snd_seq_device_info;
584 if (snd_info_register(info_entry) < 0) {
585 snd_info_free_entry(info_entry);
592 static void __exit alsa_seq_device_exit(void)
594 #ifdef CONFIG_MODULES
595 cancel_work_sync(&autoload_work);
598 #ifdef CONFIG_PROC_FS
599 snd_info_free_entry(info_entry);
602 pr_err("ALSA: seq: drivers not released (%d)\n", num_ops);
605 module_init(alsa_seq_device_init)
606 module_exit(alsa_seq_device_exit)
608 EXPORT_SYMBOL(snd_seq_device_load_drivers);
609 EXPORT_SYMBOL(snd_seq_device_new);
610 EXPORT_SYMBOL(snd_seq_device_register_driver);
611 EXPORT_SYMBOL(snd_seq_device_unregister_driver);
612 #ifdef CONFIG_MODULES
613 EXPORT_SYMBOL(snd_seq_autoload_init);
614 EXPORT_SYMBOL(snd_seq_autoload_lock);
615 EXPORT_SYMBOL(snd_seq_autoload_unlock);