blob: a509f49fa0b4ce47bb50c51bbe480aa85484ca7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Advanced Linux Sound Architecture
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define SNDRV_OS_MINORS 256
37
38static int major = CONFIG_SND_MAJOR;
39int snd_major;
40static int cards_limit = 1;
41static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
42
43MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
44MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
45MODULE_LICENSE("GPL");
46module_param(major, int, 0444);
47MODULE_PARM_DESC(major, "Major # for sound driver.");
48module_param(cards_limit, int, 0444);
49MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
50#ifdef CONFIG_DEVFS_FS
51module_param(device_mode, int, 0444);
52MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
53#endif
54MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
55
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.
59 */
60int snd_ecards_limit;
61
Clemens Ladisch6983b722005-11-20 14:05:49 +010062static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static DECLARE_MUTEX(sound_mutex);
65
gregkh@suse.de619e6662005-03-23 09:51:41 -080066extern struct class *sound_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
69#ifdef CONFIG_KMOD
70
71/**
72 * snd_request_card - try to load the card module
73 * @card: the card number
74 *
75 * Tries to load the module "snd-card-X" for the given card number
76 * via KMOD. Returns immediately if already loaded.
77 */
78void snd_request_card(int card)
79{
80 int locked;
81
82 if (! current->fs->root)
83 return;
84 read_lock(&snd_card_rwlock);
85 locked = snd_cards_lock & (1 << card);
86 read_unlock(&snd_card_rwlock);
87 if (locked)
88 return;
89 if (card < 0 || card >= cards_limit)
90 return;
91 request_module("snd-card-%i", card);
92}
93
94static void snd_request_other(int minor)
95{
96 char *str;
97
98 if (! current->fs->root)
99 return;
100 switch (minor) {
101 case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
102 case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
103 default: return;
104 }
105 request_module(str);
106}
107
108#endif /* request_module support */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int snd_open(struct inode *inode, struct file *file)
111{
112 int minor = iminor(inode);
113 int card = SNDRV_MINOR_CARD(minor);
114 int dev = SNDRV_MINOR_DEVICE(minor);
Takashi Iwai512bbd62005-11-17 13:51:18 +0100115 struct snd_minor *mptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct file_operations *old_fops;
117 int err = 0;
118
Clemens Ladisch3939e712005-10-24 17:02:03 +0200119 if (dev != SNDRV_MINOR_GLOBAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (snd_cards[card] == NULL) {
121#ifdef CONFIG_KMOD
122 snd_request_card(card);
123 if (snd_cards[card] == NULL)
124#endif
125 return -ENODEV;
126 }
127 } else {
128#ifdef CONFIG_KMOD
Clemens Ladisch6983b722005-11-20 14:05:49 +0100129 if ((mptr = snd_minors[minor]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 snd_request_other(minor);
131#endif
132 }
Clemens Ladisch6983b722005-11-20 14:05:49 +0100133 if (mptr == NULL && (mptr = snd_minors[minor]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENODEV;
135 old_fops = file->f_op;
136 file->f_op = fops_get(mptr->f_ops);
137 if (file->f_op->open)
138 err = file->f_op->open(inode, file);
139 if (err) {
140 fops_put(file->f_op);
141 file->f_op = fops_get(old_fops);
142 }
143 fops_put(old_fops);
144 return err;
145}
146
147static struct file_operations snd_fops =
148{
149 .owner = THIS_MODULE,
150 .open = snd_open
151};
152
Takashi Iwai512bbd62005-11-17 13:51:18 +0100153static int snd_kernel_minor(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int minor;
156
157 switch (type) {
158 case SNDRV_DEVICE_TYPE_SEQUENCER:
159 case SNDRV_DEVICE_TYPE_TIMER:
160 minor = type;
161 break;
162 case SNDRV_DEVICE_TYPE_CONTROL:
163 snd_assert(card != NULL, return -EINVAL);
164 minor = SNDRV_MINOR(card->number, type);
165 break;
166 case SNDRV_DEVICE_TYPE_HWDEP:
167 case SNDRV_DEVICE_TYPE_RAWMIDI:
168 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
169 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
170 snd_assert(card != NULL, return -EINVAL);
171 minor = SNDRV_MINOR(card->number, type + dev);
172 break;
173 default:
174 return -EINVAL;
175 }
176 snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
177 return minor;
178}
179
180/**
181 * snd_register_device - Register the ALSA device file for the card
182 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
183 * @card: the card instance
184 * @dev: the device index
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100185 * @f_ops: the file operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * @name: the device file name
187 *
188 * Registers an ALSA device file for the given card.
189 * The operators have to be set in reg parameter.
190 *
191 * Retrurns zero if successful, or a negative error code on failure.
192 */
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100193int snd_register_device(int type, struct snd_card *card, int dev,
194 struct file_operations *f_ops, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int minor = snd_kernel_minor(type, card, dev);
Takashi Iwai512bbd62005-11-17 13:51:18 +0100197 struct snd_minor *preg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct device *device = NULL;
199
200 if (minor < 0)
201 return minor;
202 snd_assert(name, return -EINVAL);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100203 preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (preg == NULL)
205 return -ENOMEM;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100206 preg->type = type;
Clemens Ladisch6983b722005-11-20 14:05:49 +0100207 preg->card = card ? card->number : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 preg->device = dev;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100209 preg->f_ops = f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 strcpy(preg->name, name);
211 down(&sound_mutex);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100212 if (snd_minors[minor]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 up(&sound_mutex);
214 kfree(preg);
215 return -EBUSY;
216 }
Clemens Ladisch6983b722005-11-20 14:05:49 +0100217 snd_minors[minor] = preg;
218 if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
220 if (card)
221 device = card->dev;
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700222 class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 up(&sound_mutex);
225 return 0;
226}
227
228/**
229 * snd_unregister_device - unregister the device on the given card
230 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
231 * @card: the card instance
232 * @dev: the device index
233 *
234 * Unregisters the device file already registered via
235 * snd_register_device().
236 *
237 * Returns zero if sucecessful, or a negative error code on failure
238 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100239int snd_unregister_device(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int minor = snd_kernel_minor(type, card, dev);
Takashi Iwai512bbd62005-11-17 13:51:18 +0100242 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (minor < 0)
245 return minor;
246 down(&sound_mutex);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100247 if ((mptr = snd_minors[minor]) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 up(&sound_mutex);
249 return -EINVAL;
250 }
251
Clemens Ladisch6983b722005-11-20 14:05:49 +0100252 if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
253 mptr->card >= cards_limit) /* created in sound.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 devfs_remove("snd/%s", mptr->name);
gregkh@suse.de619e6662005-03-23 09:51:41 -0800255 class_device_destroy(sound_class, MKDEV(major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Clemens Ladisch6983b722005-11-20 14:05:49 +0100257 snd_minors[minor] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 up(&sound_mutex);
259 kfree(mptr);
260 return 0;
261}
262
263/*
264 * INFO PART
265 */
266
Takashi Iwai512bbd62005-11-17 13:51:18 +0100267static struct snd_info_entry *snd_minor_info_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100269static const char *snd_device_type_name(int type)
270{
271 switch (type) {
272 case SNDRV_DEVICE_TYPE_CONTROL:
273 return "control";
274 case SNDRV_DEVICE_TYPE_HWDEP:
275 return "hardware dependent";
276 case SNDRV_DEVICE_TYPE_RAWMIDI:
277 return "raw midi";
278 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
279 return "digital audio playback";
280 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
281 return "digital audio capture";
282 case SNDRV_DEVICE_TYPE_SEQUENCER:
283 return "sequencer";
284 case SNDRV_DEVICE_TYPE_TIMER:
285 return "timer";
286 default:
287 return "?";
288 }
289}
290
Takashi Iwai512bbd62005-11-17 13:51:18 +0100291static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Clemens Ladisch6983b722005-11-20 14:05:49 +0100293 int minor;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100294 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 down(&sound_mutex);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100297 for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
298 if (!(mptr = snd_minors[minor]))
299 continue;
300 if (mptr->card >= 0) {
301 if (mptr->device >= 0)
302 snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n",
303 minor, mptr->card, mptr->device,
304 snd_device_type_name(mptr->type));
305 else
306 snd_iprintf(buffer, "%3i: [%i] : %s\n",
307 minor, mptr->card,
308 snd_device_type_name(mptr->type));
309 } else
310 snd_iprintf(buffer, "%3i: : %s\n", minor,
311 snd_device_type_name(mptr->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 up(&sound_mutex);
314}
315
316int __init snd_minor_info_init(void)
317{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100318 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
321 if (entry) {
322 entry->c.text.read_size = PAGE_SIZE;
323 entry->c.text.read = snd_minor_info_read;
324 if (snd_info_register(entry) < 0) {
325 snd_info_free_entry(entry);
326 entry = NULL;
327 }
328 }
329 snd_minor_info_entry = entry;
330 return 0;
331}
332
333int __exit snd_minor_info_done(void)
334{
335 if (snd_minor_info_entry)
336 snd_info_unregister(snd_minor_info_entry);
337 return 0;
338}
339
340/*
341 * INIT PART
342 */
343
344static int __init alsa_sound_init(void)
345{
346 short controlnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 snd_major = major;
349 snd_ecards_limit = cards_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 devfs_mk_dir("snd");
351 if (register_chrdev(major, "alsa", &snd_fops)) {
352 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
353 devfs_remove("snd");
354 return -EIO;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (snd_info_init() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unregister_chrdev(major, "alsa");
358 devfs_remove("snd");
359 return -ENOMEM;
360 }
361 snd_info_minor_register();
362 for (controlnum = 0; controlnum < cards_limit; controlnum++)
363 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
364#ifndef MODULE
365 printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
366#endif
367 return 0;
368}
369
370static void __exit alsa_sound_exit(void)
371{
372 short controlnum;
373
374 for (controlnum = 0; controlnum < cards_limit; controlnum++)
375 devfs_remove("snd/controlC%d", controlnum);
376
377 snd_info_minor_unregister();
378 snd_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (unregister_chrdev(major, "alsa") != 0)
380 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
381 devfs_remove("snd");
382}
383
384module_init(alsa_sound_init)
385module_exit(alsa_sound_exit)
386
387 /* sound.c */
388EXPORT_SYMBOL(snd_major);
389EXPORT_SYMBOL(snd_ecards_limit);
390#if defined(CONFIG_KMOD)
391EXPORT_SYMBOL(snd_request_card);
392#endif
393EXPORT_SYMBOL(snd_register_device);
394EXPORT_SYMBOL(snd_unregister_device);
395#if defined(CONFIG_SND_OSSEMUL)
396EXPORT_SYMBOL(snd_register_oss_device);
397EXPORT_SYMBOL(snd_unregister_oss_device);
398#endif
399 /* memory.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400EXPORT_SYMBOL(copy_to_user_fromio);
401EXPORT_SYMBOL(copy_from_user_toio);
402 /* init.c */
403EXPORT_SYMBOL(snd_cards);
404#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
405EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
406#endif
407EXPORT_SYMBOL(snd_card_new);
408EXPORT_SYMBOL(snd_card_disconnect);
409EXPORT_SYMBOL(snd_card_free);
410EXPORT_SYMBOL(snd_card_free_in_thread);
411EXPORT_SYMBOL(snd_card_register);
412EXPORT_SYMBOL(snd_component_add);
413EXPORT_SYMBOL(snd_card_file_add);
414EXPORT_SYMBOL(snd_card_file_remove);
415#ifdef CONFIG_PM
416EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif
418 /* device.c */
419EXPORT_SYMBOL(snd_device_new);
420EXPORT_SYMBOL(snd_device_register);
421EXPORT_SYMBOL(snd_device_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* isadma.c */
Al Viro276bd312005-08-23 22:45:06 +0100423#ifdef CONFIG_ISA_DMA_API
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424EXPORT_SYMBOL(snd_dma_program);
425EXPORT_SYMBOL(snd_dma_disable);
426EXPORT_SYMBOL(snd_dma_pointer);
427#endif
428 /* info.c */
429#ifdef CONFIG_PROC_FS
430EXPORT_SYMBOL(snd_seq_root);
431EXPORT_SYMBOL(snd_iprintf);
432EXPORT_SYMBOL(snd_info_get_line);
433EXPORT_SYMBOL(snd_info_get_str);
434EXPORT_SYMBOL(snd_info_create_module_entry);
435EXPORT_SYMBOL(snd_info_create_card_entry);
436EXPORT_SYMBOL(snd_info_free_entry);
437EXPORT_SYMBOL(snd_info_register);
438EXPORT_SYMBOL(snd_info_unregister);
439EXPORT_SYMBOL(snd_card_proc_new);
440#endif
441 /* info_oss.c */
442#if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
443EXPORT_SYMBOL(snd_oss_info_register);
444#endif
445 /* control.c */
446EXPORT_SYMBOL(snd_ctl_new);
447EXPORT_SYMBOL(snd_ctl_new1);
448EXPORT_SYMBOL(snd_ctl_free_one);
449EXPORT_SYMBOL(snd_ctl_add);
450EXPORT_SYMBOL(snd_ctl_remove);
451EXPORT_SYMBOL(snd_ctl_remove_id);
452EXPORT_SYMBOL(snd_ctl_rename_id);
453EXPORT_SYMBOL(snd_ctl_find_numid);
454EXPORT_SYMBOL(snd_ctl_find_id);
455EXPORT_SYMBOL(snd_ctl_notify);
456EXPORT_SYMBOL(snd_ctl_register_ioctl);
457EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
458#ifdef CONFIG_COMPAT
459EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
460EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
461#endif
462EXPORT_SYMBOL(snd_ctl_elem_read);
463EXPORT_SYMBOL(snd_ctl_elem_write);
464 /* misc.c */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200465EXPORT_SYMBOL(release_and_free_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#ifdef CONFIG_SND_VERBOSE_PRINTK
467EXPORT_SYMBOL(snd_verbose_printk);
468#endif
469#if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
470EXPORT_SYMBOL(snd_verbose_printd);
471#endif