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/device.h>
27 #include <linux/moduleparam.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <sound/version.h>
32 #include <sound/control.h>
33 #include <sound/initval.h>
34 #include <linux/kmod.h>
35 #include <linux/devfs_fs_kernel.h>
36 #include <linux/mutex.h>
38 #define SNDRV_OS_MINORS 256
40 static int major = CONFIG_SND_MAJOR;
42 EXPORT_SYMBOL(snd_major);
44 static int cards_limit = 1;
45 static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
48 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
49 MODULE_LICENSE("GPL");
50 module_param(major, int, 0444);
51 MODULE_PARM_DESC(major, "Major # for sound driver.");
52 module_param(cards_limit, int, 0444);
53 MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
54 #ifdef CONFIG_DEVFS_FS
55 module_param(device_mode, int, 0444);
56 MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
58 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
60 /* this one holds the actual max. card number currently available.
61 * as default, it's identical with cards_limit option. when more
62 * modules are loaded manually, this limit number increases, too.
65 EXPORT_SYMBOL(snd_ecards_limit);
67 static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
68 static DEFINE_MUTEX(sound_mutex);
70 extern struct class *sound_class;
76 * snd_request_card - try to load the card module
77 * @card: the card number
79 * Tries to load the module "snd-card-X" for the given card number
80 * via KMOD. Returns immediately if already loaded.
82 void snd_request_card(int card)
84 if (! current->fs->root)
86 if (snd_card_locked(card))
88 if (card < 0 || card >= cards_limit)
90 request_module("snd-card-%i", card);
93 EXPORT_SYMBOL(snd_request_card);
95 static void snd_request_other(int minor)
99 if (! current->fs->root)
102 case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
103 case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
109 #endif /* request_module support */
112 * snd_lookup_minor_data - get user data of a registered device
113 * @minor: the minor number
114 * @type: device type (SNDRV_DEVICE_TYPE_XXX)
116 * Checks that a minor device with the specified type is registered, and returns
117 * its user data pointer.
119 void *snd_lookup_minor_data(unsigned int minor, int type)
121 struct snd_minor *mreg;
124 if (minor >= ARRAY_SIZE(snd_minors))
126 mutex_lock(&sound_mutex);
127 mreg = snd_minors[minor];
128 if (mreg && mreg->type == type)
129 private_data = mreg->private_data;
132 mutex_unlock(&sound_mutex);
136 EXPORT_SYMBOL(snd_lookup_minor_data);
138 static int snd_open(struct inode *inode, struct file *file)
140 unsigned int minor = iminor(inode);
141 struct snd_minor *mptr = NULL;
142 const struct file_operations *old_fops;
145 if (minor >= ARRAY_SIZE(snd_minors))
147 mptr = snd_minors[minor];
150 int dev = SNDRV_MINOR_DEVICE(minor);
151 if (dev == SNDRV_MINOR_CONTROL) {
153 int card = SNDRV_MINOR_CARD(minor);
154 if (snd_cards[card] == NULL)
155 snd_request_card(card);
156 } else if (dev == SNDRV_MINOR_GLOBAL) {
158 snd_request_other(minor);
160 #ifndef CONFIG_SND_DYNAMIC_MINORS
161 /* /dev/snd/{controlC?,seq} */
162 mptr = snd_minors[minor];
168 old_fops = file->f_op;
169 file->f_op = fops_get(mptr->f_ops);
170 if (file->f_op->open)
171 err = file->f_op->open(inode, file);
173 fops_put(file->f_op);
174 file->f_op = fops_get(old_fops);
180 static struct file_operations snd_fops =
182 .owner = THIS_MODULE,
186 #ifdef CONFIG_SND_DYNAMIC_MINORS
187 static int snd_find_free_minor(void)
191 for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
192 /* skip minors still used statically for autoloading devices */
193 if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
194 minor == SNDRV_MINOR_SEQUENCER)
196 if (!snd_minors[minor])
202 static int snd_kernel_minor(int type, struct snd_card *card, int dev)
207 case SNDRV_DEVICE_TYPE_SEQUENCER:
208 case SNDRV_DEVICE_TYPE_TIMER:
211 case SNDRV_DEVICE_TYPE_CONTROL:
212 snd_assert(card != NULL, return -EINVAL);
213 minor = SNDRV_MINOR(card->number, type);
215 case SNDRV_DEVICE_TYPE_HWDEP:
216 case SNDRV_DEVICE_TYPE_RAWMIDI:
217 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
218 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
219 snd_assert(card != NULL, return -EINVAL);
220 minor = SNDRV_MINOR(card->number, type + dev);
225 snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
231 * snd_register_device - Register the ALSA device file for the card
232 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
233 * @card: the card instance
234 * @dev: the device index
235 * @f_ops: the file operations
236 * @private_data: user pointer for f_ops->open()
237 * @name: the device file name
239 * Registers an ALSA device file for the given card.
240 * The operators have to be set in reg parameter.
242 * Retrurns zero if successful, or a negative error code on failure.
244 int snd_register_device(int type, struct snd_card *card, int dev,
245 const struct file_operations *f_ops, void *private_data,
249 struct snd_minor *preg;
250 struct device *device = NULL;
252 snd_assert(name, return -EINVAL);
253 preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
257 preg->card = card ? card->number : -1;
260 preg->private_data = private_data;
261 strcpy(preg->name, name);
262 mutex_lock(&sound_mutex);
263 #ifdef CONFIG_SND_DYNAMIC_MINORS
264 minor = snd_find_free_minor();
266 minor = snd_kernel_minor(type, card, dev);
267 if (minor >= 0 && snd_minors[minor])
271 mutex_unlock(&sound_mutex);
275 snd_minors[minor] = preg;
276 if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
277 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
280 class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
282 mutex_unlock(&sound_mutex);
286 EXPORT_SYMBOL(snd_register_device);
289 * snd_unregister_device - unregister the device on the given card
290 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
291 * @card: the card instance
292 * @dev: the device index
294 * Unregisters the device file already registered via
295 * snd_register_device().
297 * Returns zero if sucecessful, or a negative error code on failure
299 int snd_unregister_device(int type, struct snd_card *card, int dev)
302 struct snd_minor *mptr;
304 cardnum = card ? card->number : -1;
305 mutex_lock(&sound_mutex);
306 for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
307 if ((mptr = snd_minors[minor]) != NULL &&
308 mptr->type == type &&
309 mptr->card == cardnum &&
312 if (minor == ARRAY_SIZE(snd_minors)) {
313 mutex_unlock(&sound_mutex);
317 if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
318 mptr->card >= cards_limit) /* created in sound.c */
319 devfs_remove("snd/%s", mptr->name);
320 class_device_destroy(sound_class, MKDEV(major, minor));
322 snd_minors[minor] = NULL;
323 mutex_unlock(&sound_mutex);
328 EXPORT_SYMBOL(snd_unregister_device);
330 #ifdef CONFIG_PROC_FS
335 static struct snd_info_entry *snd_minor_info_entry;
337 static const char *snd_device_type_name(int type)
340 case SNDRV_DEVICE_TYPE_CONTROL:
342 case SNDRV_DEVICE_TYPE_HWDEP:
343 return "hardware dependent";
344 case SNDRV_DEVICE_TYPE_RAWMIDI:
346 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
347 return "digital audio playback";
348 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
349 return "digital audio capture";
350 case SNDRV_DEVICE_TYPE_SEQUENCER:
352 case SNDRV_DEVICE_TYPE_TIMER:
359 static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
362 struct snd_minor *mptr;
364 mutex_lock(&sound_mutex);
365 for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
366 if (!(mptr = snd_minors[minor]))
368 if (mptr->card >= 0) {
369 if (mptr->device >= 0)
370 snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
371 minor, mptr->card, mptr->device,
372 snd_device_type_name(mptr->type));
374 snd_iprintf(buffer, "%3i: [%2i] : %s\n",
376 snd_device_type_name(mptr->type));
378 snd_iprintf(buffer, "%3i: : %s\n", minor,
379 snd_device_type_name(mptr->type));
381 mutex_unlock(&sound_mutex);
384 int __init snd_minor_info_init(void)
386 struct snd_info_entry *entry;
388 entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
390 entry->c.text.read = snd_minor_info_read;
391 if (snd_info_register(entry) < 0) {
392 snd_info_free_entry(entry);
396 snd_minor_info_entry = entry;
400 int __exit snd_minor_info_done(void)
402 if (snd_minor_info_entry)
403 snd_info_unregister(snd_minor_info_entry);
406 #endif /* CONFIG_PROC_FS */
412 static int __init alsa_sound_init(void)
417 snd_ecards_limit = cards_limit;
419 if (register_chrdev(major, "alsa", &snd_fops)) {
420 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
424 if (snd_info_init() < 0) {
425 unregister_chrdev(major, "alsa");
429 snd_info_minor_register();
430 for (controlnum = 0; controlnum < cards_limit; controlnum++)
431 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
433 printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
438 static void __exit alsa_sound_exit(void)
442 for (controlnum = 0; controlnum < cards_limit; controlnum++)
443 devfs_remove("snd/controlC%d", controlnum);
445 snd_info_minor_unregister();
447 if (unregister_chrdev(major, "alsa") != 0)
448 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
452 module_init(alsa_sound_init)
453 module_exit(alsa_sound_exit)