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 ops_list *ops;
111 mutex_lock(&ops_mutex);
112 list_for_each_entry(ops, &opslist, list) {
113 snd_iprintf(buffer, "snd-%s%s%s%s,%d\n",
115 ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""),
116 ops->driver & DRIVER_REQUESTED ? ",requested" : "",
117 ops->driver & DRIVER_LOCKED ? ",locked" : "",
120 mutex_unlock(&ops_mutex);
125 * load all registered drivers (called from seq_clientmgr.c)
129 /* avoid auto-loading during module_init() */
130 static int snd_seq_in_init;
131 void snd_seq_autoload_lock(void)
136 void snd_seq_autoload_unlock(void)
142 void snd_seq_device_load_drivers(void)
145 struct ops_list *ops;
147 /* Calling request_module during module_init()
148 * may cause blocking.
153 if (! current->fs->root)
156 mutex_lock(&ops_mutex);
157 list_for_each_entry(ops, &opslist, list) {
158 if (! (ops->driver & DRIVER_LOADED) &&
159 ! (ops->driver & DRIVER_REQUESTED)) {
161 mutex_unlock(&ops_mutex);
162 ops->driver |= DRIVER_REQUESTED;
163 request_module("snd-%s", ops->id);
164 mutex_lock(&ops_mutex);
168 mutex_unlock(&ops_mutex);
173 * register a sequencer device
174 * card = card info (NULL allowed)
175 * device = device number (if any)
177 * result = return pointer (NULL allowed if unnecessary)
179 int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
180 struct snd_seq_device **result)
182 struct snd_seq_device *dev;
183 struct ops_list *ops;
185 static struct snd_device_ops dops = {
186 .dev_free = snd_seq_device_dev_free,
187 .dev_register = snd_seq_device_dev_register,
188 .dev_disconnect = snd_seq_device_dev_disconnect,
194 snd_assert(id != NULL, return -EINVAL);
196 ops = find_driver(id, 1);
200 dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
206 /* set up device info */
208 dev->device = device;
209 strlcpy(dev->id, id, sizeof(dev->id));
210 dev->argsize = argsize;
211 dev->status = SNDRV_SEQ_DEVICE_FREE;
213 /* add this device to the list */
214 mutex_lock(&ops->reg_mutex);
215 list_add_tail(&dev->list, &ops->dev_list);
217 mutex_unlock(&ops->reg_mutex);
221 if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
222 snd_seq_device_free(dev);
233 * free the existing device
235 static int snd_seq_device_free(struct snd_seq_device *dev)
237 struct ops_list *ops;
239 snd_assert(dev != NULL, return -EINVAL);
241 ops = find_driver(dev->id, 0);
245 /* remove the device from the list */
246 mutex_lock(&ops->reg_mutex);
247 list_del(&dev->list);
249 mutex_unlock(&ops->reg_mutex);
251 free_device(dev, ops);
252 if (dev->private_free)
253 dev->private_free(dev);
261 static int snd_seq_device_dev_free(struct snd_device *device)
263 struct snd_seq_device *dev = device->device_data;
264 return snd_seq_device_free(dev);
268 * register the device
270 static int snd_seq_device_dev_register(struct snd_device *device)
272 struct snd_seq_device *dev = device->device_data;
273 struct ops_list *ops;
275 ops = find_driver(dev->id, 0);
279 /* initialize this device if the corresponding driver was
282 if (ops->driver & DRIVER_LOADED)
283 init_device(dev, ops);
290 * disconnect the device
292 static int snd_seq_device_dev_disconnect(struct snd_device *device)
294 struct snd_seq_device *dev = device->device_data;
295 struct ops_list *ops;
297 ops = find_driver(dev->id, 0);
301 free_device(dev, ops);
308 * register device driver
310 * entry = driver operators - duplicated to each instance
312 int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
315 struct ops_list *ops;
316 struct snd_seq_device *dev;
318 if (id == NULL || entry == NULL ||
319 entry->init_device == NULL || entry->free_device == NULL)
322 snd_seq_autoload_lock();
323 ops = find_driver(id, 1);
325 snd_seq_autoload_unlock();
328 if (ops->driver & DRIVER_LOADED) {
329 snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
331 snd_seq_autoload_unlock();
335 mutex_lock(&ops->reg_mutex);
336 /* copy driver operators */
338 ops->driver |= DRIVER_LOADED;
339 ops->argsize = argsize;
341 /* initialize existing devices if necessary */
342 list_for_each_entry(dev, &ops->dev_list, list) {
343 init_device(dev, ops);
345 mutex_unlock(&ops->reg_mutex);
348 snd_seq_autoload_unlock();
355 * create driver record
357 static struct ops_list * create_driver(char *id)
359 struct ops_list *ops;
361 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
365 /* set up driver entry */
366 strlcpy(ops->id, id, sizeof(ops->id));
367 mutex_init(&ops->reg_mutex);
369 * The ->reg_mutex locking rules are per-driver, so we create
370 * separate per-driver lock classes:
372 lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
374 ops->driver = DRIVER_EMPTY;
375 INIT_LIST_HEAD(&ops->dev_list);
376 /* lock this instance */
379 /* register driver entry */
380 mutex_lock(&ops_mutex);
381 list_add_tail(&ops->list, &opslist);
383 mutex_unlock(&ops_mutex);
390 * unregister the specified driver
392 int snd_seq_device_unregister_driver(char *id)
394 struct ops_list *ops;
395 struct snd_seq_device *dev;
397 ops = find_driver(id, 0);
400 if (! (ops->driver & DRIVER_LOADED) ||
401 (ops->driver & DRIVER_LOCKED)) {
402 snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
408 /* close and release all devices associated with this driver */
409 mutex_lock(&ops->reg_mutex);
410 ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
411 list_for_each_entry(dev, &ops->dev_list, list) {
412 free_device(dev, ops);
416 if (ops->num_init_devices > 0)
417 snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
418 ops->num_init_devices);
419 mutex_unlock(&ops->reg_mutex);
423 /* remove empty driver entries */
431 * remove empty driver entries
433 static void remove_drivers(void)
435 struct list_head *head;
437 mutex_lock(&ops_mutex);
439 while (head != &opslist) {
440 struct ops_list *ops = list_entry(head, struct ops_list, list);
441 if (! (ops->driver & DRIVER_LOADED) &&
442 ops->used == 0 && ops->num_devices == 0) {
444 list_del(&ops->list);
450 mutex_unlock(&ops_mutex);
454 * initialize the device - call init_device operator
456 static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
458 if (! (ops->driver & DRIVER_LOADED))
459 return 0; /* driver is not loaded yet */
460 if (dev->status != SNDRV_SEQ_DEVICE_FREE)
461 return 0; /* already initialized */
462 if (ops->argsize != dev->argsize) {
463 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
464 dev->name, ops->id, ops->argsize, dev->argsize);
467 if (ops->ops.init_device(dev) >= 0) {
468 dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
469 ops->num_init_devices++;
471 snd_printk(KERN_ERR "init_device failed: %s: %s\n",
479 * release the device - call free_device operator
481 static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
485 if (! (ops->driver & DRIVER_LOADED))
486 return 0; /* driver is not loaded yet */
487 if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
488 return 0; /* not registered */
489 if (ops->argsize != dev->argsize) {
490 snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
491 dev->name, ops->id, ops->argsize, dev->argsize);
494 if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
495 dev->status = SNDRV_SEQ_DEVICE_FREE;
496 dev->driver_data = NULL;
497 ops->num_init_devices--;
499 snd_printk(KERN_ERR "free_device failed: %s: %s\n",
507 * find the matching driver with given id
509 static struct ops_list * find_driver(char *id, int create_if_empty)
511 struct ops_list *ops;
513 mutex_lock(&ops_mutex);
514 list_for_each_entry(ops, &opslist, list) {
515 if (strcmp(ops->id, id) == 0) {
517 mutex_unlock(&ops_mutex);
521 mutex_unlock(&ops_mutex);
523 return create_driver(id);
527 static void unlock_driver(struct ops_list *ops)
529 mutex_lock(&ops_mutex);
531 mutex_unlock(&ops_mutex);
539 static int __init alsa_seq_device_init(void)
541 #ifdef CONFIG_PROC_FS
542 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
544 if (info_entry == NULL)
546 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
547 info_entry->c.text.read = snd_seq_device_info;
548 if (snd_info_register(info_entry) < 0) {
549 snd_info_free_entry(info_entry);
556 static void __exit alsa_seq_device_exit(void)
559 #ifdef CONFIG_PROC_FS
560 snd_info_free_entry(info_entry);
563 snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
566 module_init(alsa_seq_device_init)
567 module_exit(alsa_seq_device_exit)
569 EXPORT_SYMBOL(snd_seq_device_load_drivers);
570 EXPORT_SYMBOL(snd_seq_device_new);
571 EXPORT_SYMBOL(snd_seq_device_register_driver);
572 EXPORT_SYMBOL(snd_seq_device_unregister_driver);
574 EXPORT_SYMBOL(snd_seq_autoload_lock);
575 EXPORT_SYMBOL(snd_seq_autoload_unlock);