2 * Advanced Linux Sound Architecture
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/moduleparam.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/info.h>
30 #include <sound/version.h>
31 #include <sound/control.h>
32 #include <sound/initval.h>
33 #include <linux/kmod.h>
34 #include <linux/devfs_fs_kernel.h>
36 #define SNDRV_OS_MINORS 256
38 static int major = CONFIG_SND_MAJOR;
40 static int cards_limit = 1;
41 static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
43 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
44 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
45 MODULE_LICENSE("GPL");
46 module_param(major, int, 0444);
47 MODULE_PARM_DESC(major, "Major # for sound driver.");
48 module_param(cards_limit, int, 0444);
49 MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
50 #ifdef CONFIG_DEVFS_FS
51 module_param(device_mode, int, 0444);
52 MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
54 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
56 /* this one holds the actual max. card number currently available.
57 * as default, it's identical with cards_limit option. when more
58 * modules are loaded manually, this limit number increases, too.
62 static struct list_head snd_minors_hash[SNDRV_CARDS];
64 static DECLARE_MUTEX(sound_mutex);
66 extern struct class *sound_class;
72 * snd_request_card - try to load the card module
73 * @card: the card number
75 * Tries to load the module "snd-card-X" for the given card number
76 * via KMOD. Returns immediately if already loaded.
78 void snd_request_card(int card)
82 if (! current->fs->root)
84 read_lock(&snd_card_rwlock);
85 locked = snd_cards_lock & (1 << card);
86 read_unlock(&snd_card_rwlock);
89 if (card < 0 || card >= cards_limit)
91 request_module("snd-card-%i", card);
94 static void snd_request_other(int minor)
98 if (! current->fs->root)
101 case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
102 case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
108 #endif /* request_module support */
110 static struct snd_minor *snd_minor_search(int minor)
112 struct list_head *list;
113 struct snd_minor *mptr;
115 list_for_each(list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]) {
116 mptr = list_entry(list, struct snd_minor, list);
117 if (mptr->number == minor)
123 static int snd_open(struct inode *inode, struct file *file)
125 int minor = iminor(inode);
126 int card = SNDRV_MINOR_CARD(minor);
127 int dev = SNDRV_MINOR_DEVICE(minor);
128 struct snd_minor *mptr = NULL;
129 struct file_operations *old_fops;
132 if (dev != SNDRV_MINOR_GLOBAL) {
133 if (snd_cards[card] == NULL) {
135 snd_request_card(card);
136 if (snd_cards[card] == NULL)
142 if ((mptr = snd_minor_search(minor)) == NULL)
143 snd_request_other(minor);
146 if (mptr == NULL && (mptr = snd_minor_search(minor)) == NULL)
148 old_fops = file->f_op;
149 file->f_op = fops_get(mptr->f_ops);
150 if (file->f_op->open)
151 err = file->f_op->open(inode, file);
153 fops_put(file->f_op);
154 file->f_op = fops_get(old_fops);
160 static struct file_operations snd_fops =
162 .owner = THIS_MODULE,
166 static int snd_kernel_minor(int type, struct snd_card *card, int dev)
171 case SNDRV_DEVICE_TYPE_SEQUENCER:
172 case SNDRV_DEVICE_TYPE_TIMER:
175 case SNDRV_DEVICE_TYPE_CONTROL:
176 snd_assert(card != NULL, return -EINVAL);
177 minor = SNDRV_MINOR(card->number, type);
179 case SNDRV_DEVICE_TYPE_HWDEP:
180 case SNDRV_DEVICE_TYPE_RAWMIDI:
181 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
182 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
183 snd_assert(card != NULL, return -EINVAL);
184 minor = SNDRV_MINOR(card->number, type + dev);
189 snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
194 * snd_register_device - Register the ALSA device file for the card
195 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
196 * @card: the card instance
197 * @dev: the device index
198 * @reg: the struct snd_minor record
199 * @name: the device file name
201 * Registers an ALSA device file for the given card.
202 * The operators have to be set in reg parameter.
204 * Retrurns zero if successful, or a negative error code on failure.
206 int snd_register_device(int type, struct snd_card *card, int dev, struct snd_minor * reg, const char *name)
208 int minor = snd_kernel_minor(type, card, dev);
209 struct snd_minor *preg;
210 struct device *device = NULL;
214 snd_assert(name, return -EINVAL);
215 preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
219 preg->number = minor;
221 strcpy(preg->name, name);
223 if (snd_minor_search(minor)) {
228 list_add_tail(&preg->list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]);
229 if (strncmp(name, "controlC", 8) || card->number >= cards_limit)
230 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
233 class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
240 * snd_unregister_device - unregister the device on the given card
241 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
242 * @card: the card instance
243 * @dev: the device index
245 * Unregisters the device file already registered via
246 * snd_register_device().
248 * Returns zero if sucecessful, or a negative error code on failure
250 int snd_unregister_device(int type, struct snd_card *card, int dev)
252 int minor = snd_kernel_minor(type, card, dev);
253 struct snd_minor *mptr;
258 if ((mptr = snd_minor_search(minor)) == NULL) {
263 if (strncmp(mptr->name, "controlC", 8) || card->number >= cards_limit) /* created in sound.c */
264 devfs_remove("snd/%s", mptr->name);
265 class_device_destroy(sound_class, MKDEV(major, minor));
267 list_del(&mptr->list);
277 static struct snd_info_entry *snd_minor_info_entry = NULL;
279 static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
282 struct list_head *list;
283 struct snd_minor *mptr;
286 for (card = 0; card < SNDRV_CARDS; card++) {
287 list_for_each(list, &snd_minors_hash[card]) {
288 mptr = list_entry(list, struct snd_minor, list);
289 if (SNDRV_MINOR_DEVICE(mptr->number) != SNDRV_MINOR_GLOBAL) {
290 if ((device = mptr->device) >= 0)
291 snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, device, mptr->comment);
293 snd_iprintf(buffer, "%3i: [%i] : %s\n", mptr->number, card, mptr->comment);
295 snd_iprintf(buffer, "%3i: : %s\n", mptr->number, mptr->comment);
302 int __init snd_minor_info_init(void)
304 struct snd_info_entry *entry;
306 entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
308 entry->c.text.read_size = PAGE_SIZE;
309 entry->c.text.read = snd_minor_info_read;
310 if (snd_info_register(entry) < 0) {
311 snd_info_free_entry(entry);
315 snd_minor_info_entry = entry;
319 int __exit snd_minor_info_done(void)
321 if (snd_minor_info_entry)
322 snd_info_unregister(snd_minor_info_entry);
330 static int __init alsa_sound_init(void)
337 snd_ecards_limit = cards_limit;
338 for (card = 0; card < SNDRV_CARDS; card++)
339 INIT_LIST_HEAD(&snd_minors_hash[card]);
340 if ((err = snd_oss_init_module()) < 0)
343 if (register_chrdev(major, "alsa", &snd_fops)) {
344 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
348 if (snd_info_init() < 0) {
349 unregister_chrdev(major, "alsa");
353 snd_info_minor_register();
354 for (controlnum = 0; controlnum < cards_limit; controlnum++)
355 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
357 printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
362 static void __exit alsa_sound_exit(void)
366 for (controlnum = 0; controlnum < cards_limit; controlnum++)
367 devfs_remove("snd/controlC%d", controlnum);
369 snd_info_minor_unregister();
371 if (unregister_chrdev(major, "alsa") != 0)
372 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
376 module_init(alsa_sound_init)
377 module_exit(alsa_sound_exit)
380 EXPORT_SYMBOL(snd_major);
381 EXPORT_SYMBOL(snd_ecards_limit);
382 #if defined(CONFIG_KMOD)
383 EXPORT_SYMBOL(snd_request_card);
385 EXPORT_SYMBOL(snd_register_device);
386 EXPORT_SYMBOL(snd_unregister_device);
387 #if defined(CONFIG_SND_OSSEMUL)
388 EXPORT_SYMBOL(snd_register_oss_device);
389 EXPORT_SYMBOL(snd_unregister_oss_device);
392 EXPORT_SYMBOL(copy_to_user_fromio);
393 EXPORT_SYMBOL(copy_from_user_toio);
395 EXPORT_SYMBOL(snd_cards);
396 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
397 EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
399 EXPORT_SYMBOL(snd_card_new);
400 EXPORT_SYMBOL(snd_card_disconnect);
401 EXPORT_SYMBOL(snd_card_free);
402 EXPORT_SYMBOL(snd_card_free_in_thread);
403 EXPORT_SYMBOL(snd_card_register);
404 EXPORT_SYMBOL(snd_component_add);
405 EXPORT_SYMBOL(snd_card_file_add);
406 EXPORT_SYMBOL(snd_card_file_remove);
408 EXPORT_SYMBOL(snd_power_wait);
411 EXPORT_SYMBOL(snd_device_new);
412 EXPORT_SYMBOL(snd_device_register);
413 EXPORT_SYMBOL(snd_device_free);
415 #ifdef CONFIG_ISA_DMA_API
416 EXPORT_SYMBOL(snd_dma_program);
417 EXPORT_SYMBOL(snd_dma_disable);
418 EXPORT_SYMBOL(snd_dma_pointer);
421 #ifdef CONFIG_PROC_FS
422 EXPORT_SYMBOL(snd_seq_root);
423 EXPORT_SYMBOL(snd_iprintf);
424 EXPORT_SYMBOL(snd_info_get_line);
425 EXPORT_SYMBOL(snd_info_get_str);
426 EXPORT_SYMBOL(snd_info_create_module_entry);
427 EXPORT_SYMBOL(snd_info_create_card_entry);
428 EXPORT_SYMBOL(snd_info_free_entry);
429 EXPORT_SYMBOL(snd_info_register);
430 EXPORT_SYMBOL(snd_info_unregister);
431 EXPORT_SYMBOL(snd_card_proc_new);
434 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
435 EXPORT_SYMBOL(snd_oss_info_register);
438 EXPORT_SYMBOL(snd_ctl_new);
439 EXPORT_SYMBOL(snd_ctl_new1);
440 EXPORT_SYMBOL(snd_ctl_free_one);
441 EXPORT_SYMBOL(snd_ctl_add);
442 EXPORT_SYMBOL(snd_ctl_remove);
443 EXPORT_SYMBOL(snd_ctl_remove_id);
444 EXPORT_SYMBOL(snd_ctl_rename_id);
445 EXPORT_SYMBOL(snd_ctl_find_numid);
446 EXPORT_SYMBOL(snd_ctl_find_id);
447 EXPORT_SYMBOL(snd_ctl_notify);
448 EXPORT_SYMBOL(snd_ctl_register_ioctl);
449 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
451 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
452 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
454 EXPORT_SYMBOL(snd_ctl_elem_read);
455 EXPORT_SYMBOL(snd_ctl_elem_write);
457 EXPORT_SYMBOL(release_and_free_resource);
458 #ifdef CONFIG_SND_VERBOSE_PRINTK
459 EXPORT_SYMBOL(snd_verbose_printk);
461 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
462 EXPORT_SYMBOL(snd_verbose_printd);