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
);
93 static int snd_seq_device_dev_unregister(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 list_head
*head
;
112 mutex_lock(&ops_mutex
);
113 list_for_each(head
, &opslist
) {
114 struct ops_list
*ops
= list_entry(head
, struct ops_list
, list
);
115 snd_iprintf(buffer
, "snd-%s%s%s%s,%d\n",
117 ops
->driver
& DRIVER_LOADED
? ",loaded" : (ops
->driver
== DRIVER_EMPTY
? ",empty" : ""),
118 ops
->driver
& DRIVER_REQUESTED
? ",requested" : "",
119 ops
->driver
& DRIVER_LOCKED
? ",locked" : "",
122 mutex_unlock(&ops_mutex
);
127 * load all registered drivers (called from seq_clientmgr.c)
131 /* avoid auto-loading during module_init() */
132 static int snd_seq_in_init
;
133 void snd_seq_autoload_lock(void)
138 void snd_seq_autoload_unlock(void)
144 void snd_seq_device_load_drivers(void)
147 struct list_head
*head
;
149 /* Calling request_module during module_init()
150 * may cause blocking.
155 if (! current
->fs
->root
)
158 mutex_lock(&ops_mutex
);
159 list_for_each(head
, &opslist
) {
160 struct ops_list
*ops
= list_entry(head
, struct ops_list
, list
);
161 if (! (ops
->driver
& DRIVER_LOADED
) &&
162 ! (ops
->driver
& DRIVER_REQUESTED
)) {
164 mutex_unlock(&ops_mutex
);
165 ops
->driver
|= DRIVER_REQUESTED
;
166 request_module("snd-%s", ops
->id
);
167 mutex_lock(&ops_mutex
);
171 mutex_unlock(&ops_mutex
);
176 * register a sequencer device
177 * card = card info (NULL allowed)
178 * device = device number (if any)
180 * result = return pointer (NULL allowed if unnecessary)
182 int snd_seq_device_new(struct snd_card
*card
, int device
, char *id
, int argsize
,
183 struct snd_seq_device
**result
)
185 struct snd_seq_device
*dev
;
186 struct ops_list
*ops
;
188 static struct snd_device_ops dops
= {
189 .dev_free
= snd_seq_device_dev_free
,
190 .dev_register
= snd_seq_device_dev_register
,
191 .dev_disconnect
= snd_seq_device_dev_disconnect
,
192 .dev_unregister
= snd_seq_device_dev_unregister
198 snd_assert(id
!= NULL
, return -EINVAL
);
200 ops
= find_driver(id
, 1);
204 dev
= kzalloc(sizeof(*dev
)*2 + argsize
, GFP_KERNEL
);
210 /* set up device info */
212 dev
->device
= device
;
213 strlcpy(dev
->id
, id
, sizeof(dev
->id
));
214 dev
->argsize
= argsize
;
215 dev
->status
= SNDRV_SEQ_DEVICE_FREE
;
217 /* add this device to the list */
218 mutex_lock(&ops
->reg_mutex
);
219 list_add_tail(&dev
->list
, &ops
->dev_list
);
221 mutex_unlock(&ops
->reg_mutex
);
225 if ((err
= snd_device_new(card
, SNDRV_DEV_SEQUENCER
, dev
, &dops
)) < 0) {
226 snd_seq_device_free(dev
);
237 * free the existing device
239 static int snd_seq_device_free(struct snd_seq_device
*dev
)
241 struct ops_list
*ops
;
243 snd_assert(dev
!= NULL
, return -EINVAL
);
245 ops
= find_driver(dev
->id
, 0);
249 /* remove the device from the list */
250 mutex_lock(&ops
->reg_mutex
);
251 list_del(&dev
->list
);
253 mutex_unlock(&ops
->reg_mutex
);
255 free_device(dev
, ops
);
256 if (dev
->private_free
)
257 dev
->private_free(dev
);
265 static int snd_seq_device_dev_free(struct snd_device
*device
)
267 struct snd_seq_device
*dev
= device
->device_data
;
268 return snd_seq_device_free(dev
);
272 * register the device
274 static int snd_seq_device_dev_register(struct snd_device
*device
)
276 struct snd_seq_device
*dev
= device
->device_data
;
277 struct ops_list
*ops
;
279 ops
= find_driver(dev
->id
, 0);
283 /* initialize this device if the corresponding driver was
286 if (ops
->driver
& DRIVER_LOADED
)
287 init_device(dev
, ops
);
294 * disconnect the device
296 static int snd_seq_device_dev_disconnect(struct snd_device
*device
)
298 struct snd_seq_device
*dev
= device
->device_data
;
299 struct ops_list
*ops
;
301 ops
= find_driver(dev
->id
, 0);
305 free_device(dev
, ops
);
312 * unregister the existing device
314 static int snd_seq_device_dev_unregister(struct snd_device
*device
)
316 struct snd_seq_device
*dev
= device
->device_data
;
317 return snd_seq_device_free(dev
);
321 * register device driver
323 * entry = driver operators - duplicated to each instance
325 int snd_seq_device_register_driver(char *id
, struct snd_seq_dev_ops
*entry
,
328 struct list_head
*head
;
329 struct ops_list
*ops
;
331 if (id
== NULL
|| entry
== NULL
||
332 entry
->init_device
== NULL
|| entry
->free_device
== NULL
)
335 snd_seq_autoload_lock();
336 ops
= find_driver(id
, 1);
338 snd_seq_autoload_unlock();
341 if (ops
->driver
& DRIVER_LOADED
) {
342 snd_printk(KERN_WARNING
"driver_register: driver '%s' already exists\n", id
);
344 snd_seq_autoload_unlock();
348 mutex_lock(&ops
->reg_mutex
);
349 /* copy driver operators */
351 ops
->driver
|= DRIVER_LOADED
;
352 ops
->argsize
= argsize
;
354 /* initialize existing devices if necessary */
355 list_for_each(head
, &ops
->dev_list
) {
356 struct snd_seq_device
*dev
= list_entry(head
, struct snd_seq_device
, list
);
357 init_device(dev
, ops
);
359 mutex_unlock(&ops
->reg_mutex
);
362 snd_seq_autoload_unlock();
369 * create driver record
371 static struct ops_list
* create_driver(char *id
)
373 struct ops_list
*ops
;
375 ops
= kmalloc(sizeof(*ops
), GFP_KERNEL
);
378 memset(ops
, 0, sizeof(*ops
));
380 /* set up driver entry */
381 strlcpy(ops
->id
, id
, sizeof(ops
->id
));
382 mutex_init(&ops
->reg_mutex
);
384 * The ->reg_mutex locking rules are per-driver, so we create
385 * separate per-driver lock classes:
387 lockdep_set_class(&ops
->reg_mutex
, (struct lock_class_key
*)id
);
389 ops
->driver
= DRIVER_EMPTY
;
390 INIT_LIST_HEAD(&ops
->dev_list
);
391 /* lock this instance */
394 /* register driver entry */
395 mutex_lock(&ops_mutex
);
396 list_add_tail(&ops
->list
, &opslist
);
398 mutex_unlock(&ops_mutex
);
405 * unregister the specified driver
407 int snd_seq_device_unregister_driver(char *id
)
409 struct list_head
*head
;
410 struct ops_list
*ops
;
412 ops
= find_driver(id
, 0);
415 if (! (ops
->driver
& DRIVER_LOADED
) ||
416 (ops
->driver
& DRIVER_LOCKED
)) {
417 snd_printk(KERN_ERR
"driver_unregister: cannot unload driver '%s': status=%x\n",
423 /* close and release all devices associated with this driver */
424 mutex_lock(&ops
->reg_mutex
);
425 ops
->driver
|= DRIVER_LOCKED
; /* do not remove this driver recursively */
426 list_for_each(head
, &ops
->dev_list
) {
427 struct snd_seq_device
*dev
= list_entry(head
, struct snd_seq_device
, list
);
428 free_device(dev
, ops
);
432 if (ops
->num_init_devices
> 0)
433 snd_printk(KERN_ERR
"free_driver: init_devices > 0!! (%d)\n",
434 ops
->num_init_devices
);
435 mutex_unlock(&ops
->reg_mutex
);
439 /* remove empty driver entries */
447 * remove empty driver entries
449 static void remove_drivers(void)
451 struct list_head
*head
;
453 mutex_lock(&ops_mutex
);
455 while (head
!= &opslist
) {
456 struct ops_list
*ops
= list_entry(head
, struct ops_list
, list
);
457 if (! (ops
->driver
& DRIVER_LOADED
) &&
458 ops
->used
== 0 && ops
->num_devices
== 0) {
460 list_del(&ops
->list
);
466 mutex_unlock(&ops_mutex
);
470 * initialize the device - call init_device operator
472 static int init_device(struct snd_seq_device
*dev
, struct ops_list
*ops
)
474 if (! (ops
->driver
& DRIVER_LOADED
))
475 return 0; /* driver is not loaded yet */
476 if (dev
->status
!= SNDRV_SEQ_DEVICE_FREE
)
477 return 0; /* already initialized */
478 if (ops
->argsize
!= dev
->argsize
) {
479 snd_printk(KERN_ERR
"incompatible device '%s' for plug-in '%s' (%d %d)\n",
480 dev
->name
, ops
->id
, ops
->argsize
, dev
->argsize
);
483 if (ops
->ops
.init_device(dev
) >= 0) {
484 dev
->status
= SNDRV_SEQ_DEVICE_REGISTERED
;
485 ops
->num_init_devices
++;
487 snd_printk(KERN_ERR
"init_device failed: %s: %s\n",
495 * release the device - call free_device operator
497 static int free_device(struct snd_seq_device
*dev
, struct ops_list
*ops
)
501 if (! (ops
->driver
& DRIVER_LOADED
))
502 return 0; /* driver is not loaded yet */
503 if (dev
->status
!= SNDRV_SEQ_DEVICE_REGISTERED
)
504 return 0; /* not registered */
505 if (ops
->argsize
!= dev
->argsize
) {
506 snd_printk(KERN_ERR
"incompatible device '%s' for plug-in '%s' (%d %d)\n",
507 dev
->name
, ops
->id
, ops
->argsize
, dev
->argsize
);
510 if ((result
= ops
->ops
.free_device(dev
)) >= 0 || result
== -ENXIO
) {
511 dev
->status
= SNDRV_SEQ_DEVICE_FREE
;
512 dev
->driver_data
= NULL
;
513 ops
->num_init_devices
--;
515 snd_printk(KERN_ERR
"free_device failed: %s: %s\n",
523 * find the matching driver with given id
525 static struct ops_list
* find_driver(char *id
, int create_if_empty
)
527 struct list_head
*head
;
529 mutex_lock(&ops_mutex
);
530 list_for_each(head
, &opslist
) {
531 struct ops_list
*ops
= list_entry(head
, struct ops_list
, list
);
532 if (strcmp(ops
->id
, id
) == 0) {
534 mutex_unlock(&ops_mutex
);
538 mutex_unlock(&ops_mutex
);
540 return create_driver(id
);
544 static void unlock_driver(struct ops_list
*ops
)
546 mutex_lock(&ops_mutex
);
548 mutex_unlock(&ops_mutex
);
556 static int __init
alsa_seq_device_init(void)
558 #ifdef CONFIG_PROC_FS
559 info_entry
= snd_info_create_module_entry(THIS_MODULE
, "drivers",
561 if (info_entry
== NULL
)
563 info_entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
564 info_entry
->c
.text
.read
= snd_seq_device_info
;
565 if (snd_info_register(info_entry
) < 0) {
566 snd_info_free_entry(info_entry
);
573 static void __exit
alsa_seq_device_exit(void)
576 #ifdef CONFIG_PROC_FS
577 snd_info_unregister(info_entry
);
580 snd_printk(KERN_ERR
"drivers not released (%d)\n", num_ops
);
583 module_init(alsa_seq_device_init
)
584 module_exit(alsa_seq_device_exit
)
586 EXPORT_SYMBOL(snd_seq_device_load_drivers
);
587 EXPORT_SYMBOL(snd_seq_device_new
);
588 EXPORT_SYMBOL(snd_seq_device_register_driver
);
589 EXPORT_SYMBOL(snd_seq_device_unregister_driver
);
591 EXPORT_SYMBOL(snd_seq_autoload_lock
);
592 EXPORT_SYMBOL(snd_seq_autoload_unlock
);