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 <sound/core.h>
41 #include <sound/info.h>
42 #include <sound/seq_device.h>
43 #include <sound/seq_kernel.h>
44 #include <sound/initval.h>
45 #include <linux/kmod.h>
46 #include <linux/slab.h>
47 #include <linux/mutex.h>
49 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA sequencer device management");
51 MODULE_LICENSE("GPL");
54 #define DRIVER_EMPTY 0
55 #define DRIVER_LOADED (1<<0)
56 #define DRIVER_REQUESTED (1<<1)
57 #define DRIVER_LOCKED (1<<2)
60 char id[ID_LEN]; /* driver id */
61 int driver; /* driver state */
62 int used; /* reference counter */
63 int argsize; /* argument size */
66 struct snd_seq_dev_ops ops;
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 mutex reg_mutex;
74 struct list_head list; /* next driver */
78 static LIST_HEAD(opslist);
80 static DEFINE_MUTEX(ops_mutex);
82 static struct snd_info_entry *info_entry;
88 static int snd_seq_device_free(struct snd_seq_device *dev);
89 static int snd_seq_device_dev_free(struct snd_device *device);
90 static int snd_seq_device_dev_register(struct snd_device *device);
91 static int snd_seq_device_dev_disconnect(struct snd_device *device);
93 static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
94 static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
95 static struct ops_list *find_driver(char *id, int create_if_empty);
96 static struct ops_list *create_driver(char *id);
97 static void unlock_driver(struct ops_list *ops);
98 static void remove_drivers(void);
101 * show all drivers and their status
104 #ifdef CONFIG_PROC_FS
105 static void snd_seq_device_info(struct snd_info_entry *entry,
106 struct snd_info_buffer *buffer)
108 struct ops_list *ops;
110 mutex_lock(&ops_mutex);
111 list_for_each_entry(ops, &opslist, list) {
112 snd_iprintf(buffer, "snd-%s%s%s%s,%d\n",
114 ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""),
115 ops->driver & DRIVER_REQUESTED ? ",requested" : "",
116 ops->driver & DRIVER_LOCKED ? ",locked" : "",
119 mutex_unlock(&ops_mutex);
124 * load all registered drivers (called from seq_clientmgr.c)
128 /* avoid auto-loading during module_init() */
129 static int snd_seq_in_init;
130 void snd_seq_autoload_lock(void)
135 void snd_seq_autoload_unlock(void)
141 void snd_seq_device_load_drivers(void)
144 struct ops_list *ops;
146 /* Calling request_module during module_init()
147 * may cause blocking.
152 if (! current->fs->root)
155 mutex_lock(&ops_mutex);
156 list_for_each_entry(ops, &opslist, list) {
157 if (! (ops->driver & DRIVER_LOADED) &&
158 ! (ops->driver & DRIVER_REQUESTED)) {
160 mutex_unlock(&ops_mutex);
161 ops->driver |= DRIVER_REQUESTED;
162 request_module("snd-%s", ops->id);
163 mutex_lock(&ops_mutex);
167 mutex_unlock(&ops_mutex);
172 * register a sequencer device
173 * card = card info (NULL allowed)
174 * device = device number (if any)
176 * result = return pointer (NULL allowed if unnecessary)
178 int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
179 struct snd_seq_device **result)
181 struct snd_seq_device *dev;
182 struct ops_list *ops;
184 static struct snd_device_ops dops = {
185 .dev_free = snd_seq_device_dev_free,
186 .dev_register = snd_seq_device_dev_register,
187 .dev_disconnect = snd_seq_device_dev_disconnect,
193 snd_assert(id != NULL, return -EINVAL);
195 ops = find_driver(id, 1);
199 dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
205 /* set up device info */
207 dev->device = device;
208 strlcpy(dev->id, id, sizeof(dev->id));
209 dev->argsize = argsize;
210 dev->status = SNDRV_SEQ_DEVICE_FREE;
212 /* add this device to the list */
213 mutex_lock(&ops->reg_mutex);
214 list_add_tail(&dev->list, &ops->dev_list);
216 mutex_unlock(&ops->reg_mutex);
220 if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
221 snd_seq_device_free(dev);
232 * free the existing device
234 static int snd_seq_device_free(struct snd_seq_device *dev)
236 struct ops_list *ops;
238 snd_assert(dev != NULL, return -EINVAL);
240 ops = find_driver(dev->id, 0);
244 /* remove the device from the list */
245 mutex_lock(&ops->reg_mutex);
246 list_del(&dev->list);
248 mutex_unlock(&ops->reg_mutex);
250 free_device(dev, ops);
251 if (dev->private_free)
252 dev->private_free(dev);
260 static int snd_seq_device_dev_free(struct snd_device *device)
262 struct snd_seq_device *dev = device->device_data;
263 return snd_seq_device_free(dev);
267 * register the device
269 static int snd_seq_device_dev_register(struct snd_device *device)
271 struct snd_seq_device *dev = device->device_data;
272 struct ops_list *ops;
274 ops = find_driver(dev->id, 0);
278 /* initialize this device if the corresponding driver was
281 if (ops->driver & DRIVER_LOADED)
282 init_device(dev, ops);
289 * disconnect the device
291 static int snd_seq_device_dev_disconnect(struct snd_device *device)
293 struct snd_seq_device *dev = device->device_data;
294 struct ops_list *ops;
296 ops = find_driver(dev->id, 0);
300 free_device(dev, ops);
307 * register device driver
309 * entry = driver operators - duplicated to each instance
311 int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
314 struct ops_list *ops;
315 struct snd_seq_device *dev;
317 if (id == NULL || entry == NULL ||
318 entry->init_device == NULL || entry->free_device == NULL)
321 snd_seq_autoload_lock();
322 ops = find_driver(id, 1);
324 snd_seq_autoload_unlock();
327 if (ops->driver & DRIVER_LOADED) {
328 snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
330 snd_seq_autoload_unlock();
334 mutex_lock(&ops->reg_mutex);
335 /* copy driver operators */
337 ops->driver |= DRIVER_LOADED;
338 ops->argsize = argsize;
340 /* initialize existing devices if necessary */
341 list_for_each_entry(dev, &ops->dev_list, list) {
342 init_device(dev, ops);
344 mutex_unlock(&ops->reg_mutex);
347 snd_seq_autoload_unlock();
354 * create driver record
356 static struct ops_list * create_driver(char *id)
358 struct ops_list *ops;
360 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
364 /* set up driver entry */
365 strlcpy(ops->id, id, sizeof(ops->id));
366 mutex_init(&ops->reg_mutex);
368 * The ->reg_mutex locking rules are per-driver, so we create
369 * separate per-driver lock classes:
371 lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
373 ops->driver = DRIVER_EMPTY;
374 INIT_LIST_HEAD(&ops->dev_list);
375 /* lock this instance */
378 /* register driver entry */
379 mutex_lock(&ops_mutex);
380 list_add_tail(&ops->list, &opslist);
382 mutex_unlock(&ops_mutex);
389 * unregister the specified driver
391 int snd_seq_device_unregister_driver(char *id)
393 struct ops_list *ops;
394 struct snd_seq_device *dev;
396 ops = find_driver(id, 0);
399 if (! (ops->driver & DRIVER_LOADED) ||
400 (ops->driver & DRIVER_LOCKED)) {
401 snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
407 /* close and release all devices associated with this driver */
408 mutex_lock(&ops->reg_mutex);
409 ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
410 list_for_each_entry(dev, &ops->dev_list, list) {
411 free_device(dev, ops);
415 if (ops->num_init_devices > 0)
416 snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
417 ops->num_init_devices);
418 mutex_unlock(&ops->reg_mutex);
422 /* remove empty driver entries */
430 * remove empty driver entries
432 static void remove_drivers(void)
434 struct list_head *head;
436 mutex_lock(&ops_mutex);
438 while (head != &opslist) {
439 struct ops_list *ops = list_entry(head, struct ops_list, list);
440 if (! (ops->driver & DRIVER_LOADED) &&
441 ops->used == 0 && ops->num_devices == 0) {
443 list_del(&ops->list);
449 mutex_unlock(&ops_mutex);
453 * initialize the device - call init_device operator
455 static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
457 if (! (ops->driver & DRIVER_LOADED))
458 return 0; /* driver is not loaded yet */
459 if (dev->status != SNDRV_SEQ_DEVICE_FREE)
460 return 0; /* already initialized */
461 if (ops->argsize != dev->argsize) {
462 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
463 dev->name, ops->id, ops->argsize, dev->argsize);
466 if (ops->ops.init_device(dev) >= 0) {
467 dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
468 ops->num_init_devices++;
470 snd_printk(KERN_ERR "init_device failed: %s: %s\n",
478 * release the device - call free_device operator
480 static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
484 if (! (ops->driver & DRIVER_LOADED))
485 return 0; /* driver is not loaded yet */
486 if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
487 return 0; /* not registered */
488 if (ops->argsize != dev->argsize) {
489 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
490 dev->name, ops->id, ops->argsize, dev->argsize);
493 if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
494 dev->status = SNDRV_SEQ_DEVICE_FREE;
495 dev->driver_data = NULL;
496 ops->num_init_devices--;
498 snd_printk(KERN_ERR "free_device failed: %s: %s\n",
506 * find the matching driver with given id
508 static struct ops_list * find_driver(char *id, int create_if_empty)
510 struct ops_list *ops;
512 mutex_lock(&ops_mutex);
513 list_for_each_entry(ops, &opslist, list) {
514 if (strcmp(ops->id, id) == 0) {
516 mutex_unlock(&ops_mutex);
520 mutex_unlock(&ops_mutex);
522 return create_driver(id);
526 static void unlock_driver(struct ops_list *ops)
528 mutex_lock(&ops_mutex);
530 mutex_unlock(&ops_mutex);
538 static int __init alsa_seq_device_init(void)
540 #ifdef CONFIG_PROC_FS
541 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
543 if (info_entry == NULL)
545 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
546 info_entry->c.text.read = snd_seq_device_info;
547 if (snd_info_register(info_entry) < 0) {
548 snd_info_free_entry(info_entry);
555 static void __exit alsa_seq_device_exit(void)
558 #ifdef CONFIG_PROC_FS
559 snd_info_free_entry(info_entry);
562 snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
565 module_init(alsa_seq_device_init)
566 module_exit(alsa_seq_device_exit)
568 EXPORT_SYMBOL(snd_seq_device_load_drivers);
569 EXPORT_SYMBOL(snd_seq_device_new);
570 EXPORT_SYMBOL(snd_seq_device_register_driver);
571 EXPORT_SYMBOL(snd_seq_device_unregister_driver);
573 EXPORT_SYMBOL(snd_seq_autoload_lock);
574 EXPORT_SYMBOL(snd_seq_autoload_unlock);