blob: 1e5eca54692507951ade4e5eacaee5199e056c42 [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 -070063static DECLARE_MUTEX(sound_mutex);
64
gregkh@suse.de619e6662005-03-23 09:51:41 -080065extern struct class *sound_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67
68#ifdef CONFIG_KMOD
69
70/**
71 * snd_request_card - try to load the card module
72 * @card: the card number
73 *
74 * Tries to load the module "snd-card-X" for the given card number
75 * via KMOD. Returns immediately if already loaded.
76 */
77void snd_request_card(int card)
78{
79 int locked;
80
81 if (! current->fs->root)
82 return;
83 read_lock(&snd_card_rwlock);
84 locked = snd_cards_lock & (1 << card);
85 read_unlock(&snd_card_rwlock);
86 if (locked)
87 return;
88 if (card < 0 || card >= cards_limit)
89 return;
90 request_module("snd-card-%i", card);
91}
92
93static void snd_request_other(int minor)
94{
95 char *str;
96
97 if (! current->fs->root)
98 return;
99 switch (minor) {
100 case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
101 case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
102 default: return;
103 }
104 request_module(str);
105}
106
107#endif /* request_module support */
108
Clemens Ladischf87135f2005-11-20 14:06:59 +0100109/**
110 * snd_lookup_minor_data - get user data of a registered device
111 * @minor: the minor number
112 * @type: device type (SNDRV_DEVICE_TYPE_XXX)
113 *
114 * Checks that a minor device with the specified type is registered, and returns
115 * its user data pointer.
116 */
117void *snd_lookup_minor_data(unsigned int minor, int type)
118{
119 struct snd_minor *mreg;
120 void *private_data;
121
122 if (minor > ARRAY_SIZE(snd_minors))
123 return NULL;
124 down(&sound_mutex);
125 mreg = snd_minors[minor];
126 if (mreg && mreg->type == type)
127 private_data = mreg->private_data;
128 else
129 private_data = NULL;
130 up(&sound_mutex);
131 return private_data;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int snd_open(struct inode *inode, struct file *file)
135{
136 int minor = iminor(inode);
137 int card = SNDRV_MINOR_CARD(minor);
138 int dev = SNDRV_MINOR_DEVICE(minor);
Takashi Iwai512bbd62005-11-17 13:51:18 +0100139 struct snd_minor *mptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct file_operations *old_fops;
141 int err = 0;
142
Clemens Ladisch3939e712005-10-24 17:02:03 +0200143 if (dev != SNDRV_MINOR_GLOBAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (snd_cards[card] == NULL) {
145#ifdef CONFIG_KMOD
146 snd_request_card(card);
147 if (snd_cards[card] == NULL)
148#endif
149 return -ENODEV;
150 }
151 } else {
152#ifdef CONFIG_KMOD
Clemens Ladisch6983b722005-11-20 14:05:49 +0100153 if ((mptr = snd_minors[minor]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 snd_request_other(minor);
155#endif
156 }
Clemens Ladisch6983b722005-11-20 14:05:49 +0100157 if (mptr == NULL && (mptr = snd_minors[minor]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -ENODEV;
159 old_fops = file->f_op;
160 file->f_op = fops_get(mptr->f_ops);
161 if (file->f_op->open)
162 err = file->f_op->open(inode, file);
163 if (err) {
164 fops_put(file->f_op);
165 file->f_op = fops_get(old_fops);
166 }
167 fops_put(old_fops);
168 return err;
169}
170
171static struct file_operations snd_fops =
172{
173 .owner = THIS_MODULE,
174 .open = snd_open
175};
176
Takashi Iwai512bbd62005-11-17 13:51:18 +0100177static int snd_kernel_minor(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int minor;
180
181 switch (type) {
182 case SNDRV_DEVICE_TYPE_SEQUENCER:
183 case SNDRV_DEVICE_TYPE_TIMER:
184 minor = type;
185 break;
186 case SNDRV_DEVICE_TYPE_CONTROL:
187 snd_assert(card != NULL, return -EINVAL);
188 minor = SNDRV_MINOR(card->number, type);
189 break;
190 case SNDRV_DEVICE_TYPE_HWDEP:
191 case SNDRV_DEVICE_TYPE_RAWMIDI:
192 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
193 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
194 snd_assert(card != NULL, return -EINVAL);
195 minor = SNDRV_MINOR(card->number, type + dev);
196 break;
197 default:
198 return -EINVAL;
199 }
200 snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
201 return minor;
202}
203
204/**
205 * snd_register_device - Register the ALSA device file for the card
206 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
207 * @card: the card instance
208 * @dev: the device index
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100209 * @f_ops: the file operations
Clemens Ladischf87135f2005-11-20 14:06:59 +0100210 * @private_data: user pointer for f_ops->open()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * @name: the device file name
212 *
213 * Registers an ALSA device file for the given card.
214 * The operators have to be set in reg parameter.
215 *
216 * Retrurns zero if successful, or a negative error code on failure.
217 */
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100218int snd_register_device(int type, struct snd_card *card, int dev,
Clemens Ladischf87135f2005-11-20 14:06:59 +0100219 struct file_operations *f_ops, void *private_data,
220 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int minor = snd_kernel_minor(type, card, dev);
Takashi Iwai512bbd62005-11-17 13:51:18 +0100223 struct snd_minor *preg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct device *device = NULL;
225
226 if (minor < 0)
227 return minor;
228 snd_assert(name, return -EINVAL);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100229 preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (preg == NULL)
231 return -ENOMEM;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100232 preg->type = type;
Clemens Ladisch6983b722005-11-20 14:05:49 +0100233 preg->card = card ? card->number : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 preg->device = dev;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100235 preg->f_ops = f_ops;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100236 preg->private_data = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 strcpy(preg->name, name);
238 down(&sound_mutex);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100239 if (snd_minors[minor]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 up(&sound_mutex);
241 kfree(preg);
242 return -EBUSY;
243 }
Clemens Ladisch6983b722005-11-20 14:05:49 +0100244 snd_minors[minor] = preg;
245 if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
247 if (card)
248 device = card->dev;
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700249 class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 up(&sound_mutex);
252 return 0;
253}
254
255/**
256 * snd_unregister_device - unregister the device on the given card
257 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
258 * @card: the card instance
259 * @dev: the device index
260 *
261 * Unregisters the device file already registered via
262 * snd_register_device().
263 *
264 * Returns zero if sucecessful, or a negative error code on failure
265 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100266int snd_unregister_device(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100268 int cardnum, minor;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100269 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Clemens Ladischf87135f2005-11-20 14:06:59 +0100271 cardnum = card ? card->number : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 down(&sound_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100273 for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
274 if ((mptr = snd_minors[minor]) != NULL &&
275 mptr->type == type &&
276 mptr->card == cardnum &&
277 mptr->device == dev)
278 break;
279 if (minor == ARRAY_SIZE(snd_minors)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 up(&sound_mutex);
281 return -EINVAL;
282 }
283
Clemens Ladisch6983b722005-11-20 14:05:49 +0100284 if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
285 mptr->card >= cards_limit) /* created in sound.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 devfs_remove("snd/%s", mptr->name);
gregkh@suse.de619e6662005-03-23 09:51:41 -0800287 class_device_destroy(sound_class, MKDEV(major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Clemens Ladisch6983b722005-11-20 14:05:49 +0100289 snd_minors[minor] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 up(&sound_mutex);
291 kfree(mptr);
292 return 0;
293}
294
295/*
296 * INFO PART
297 */
298
Takashi Iwai512bbd62005-11-17 13:51:18 +0100299static struct snd_info_entry *snd_minor_info_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100301static const char *snd_device_type_name(int type)
302{
303 switch (type) {
304 case SNDRV_DEVICE_TYPE_CONTROL:
305 return "control";
306 case SNDRV_DEVICE_TYPE_HWDEP:
307 return "hardware dependent";
308 case SNDRV_DEVICE_TYPE_RAWMIDI:
309 return "raw midi";
310 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
311 return "digital audio playback";
312 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
313 return "digital audio capture";
314 case SNDRV_DEVICE_TYPE_SEQUENCER:
315 return "sequencer";
316 case SNDRV_DEVICE_TYPE_TIMER:
317 return "timer";
318 default:
319 return "?";
320 }
321}
322
Takashi Iwai512bbd62005-11-17 13:51:18 +0100323static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Clemens Ladisch6983b722005-11-20 14:05:49 +0100325 int minor;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100326 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 down(&sound_mutex);
Clemens Ladisch6983b722005-11-20 14:05:49 +0100329 for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
330 if (!(mptr = snd_minors[minor]))
331 continue;
332 if (mptr->card >= 0) {
333 if (mptr->device >= 0)
334 snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n",
335 minor, mptr->card, mptr->device,
336 snd_device_type_name(mptr->type));
337 else
338 snd_iprintf(buffer, "%3i: [%i] : %s\n",
339 minor, mptr->card,
340 snd_device_type_name(mptr->type));
341 } else
342 snd_iprintf(buffer, "%3i: : %s\n", minor,
343 snd_device_type_name(mptr->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 up(&sound_mutex);
346}
347
348int __init snd_minor_info_init(void)
349{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100350 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
353 if (entry) {
354 entry->c.text.read_size = PAGE_SIZE;
355 entry->c.text.read = snd_minor_info_read;
356 if (snd_info_register(entry) < 0) {
357 snd_info_free_entry(entry);
358 entry = NULL;
359 }
360 }
361 snd_minor_info_entry = entry;
362 return 0;
363}
364
365int __exit snd_minor_info_done(void)
366{
367 if (snd_minor_info_entry)
368 snd_info_unregister(snd_minor_info_entry);
369 return 0;
370}
371
372/*
373 * INIT PART
374 */
375
376static int __init alsa_sound_init(void)
377{
378 short controlnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 snd_major = major;
381 snd_ecards_limit = cards_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 devfs_mk_dir("snd");
383 if (register_chrdev(major, "alsa", &snd_fops)) {
384 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
385 devfs_remove("snd");
386 return -EIO;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (snd_info_init() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unregister_chrdev(major, "alsa");
390 devfs_remove("snd");
391 return -ENOMEM;
392 }
393 snd_info_minor_register();
394 for (controlnum = 0; controlnum < cards_limit; controlnum++)
395 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
396#ifndef MODULE
397 printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
398#endif
399 return 0;
400}
401
402static void __exit alsa_sound_exit(void)
403{
404 short controlnum;
405
406 for (controlnum = 0; controlnum < cards_limit; controlnum++)
407 devfs_remove("snd/controlC%d", controlnum);
408
409 snd_info_minor_unregister();
410 snd_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (unregister_chrdev(major, "alsa") != 0)
412 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
413 devfs_remove("snd");
414}
415
416module_init(alsa_sound_init)
417module_exit(alsa_sound_exit)
418
419 /* sound.c */
420EXPORT_SYMBOL(snd_major);
421EXPORT_SYMBOL(snd_ecards_limit);
422#if defined(CONFIG_KMOD)
423EXPORT_SYMBOL(snd_request_card);
424#endif
425EXPORT_SYMBOL(snd_register_device);
426EXPORT_SYMBOL(snd_unregister_device);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100427EXPORT_SYMBOL(snd_lookup_minor_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#if defined(CONFIG_SND_OSSEMUL)
429EXPORT_SYMBOL(snd_register_oss_device);
430EXPORT_SYMBOL(snd_unregister_oss_device);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100431EXPORT_SYMBOL(snd_lookup_oss_minor_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#endif
433 /* memory.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL(copy_to_user_fromio);
435EXPORT_SYMBOL(copy_from_user_toio);
436 /* init.c */
437EXPORT_SYMBOL(snd_cards);
438#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
439EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
440#endif
441EXPORT_SYMBOL(snd_card_new);
442EXPORT_SYMBOL(snd_card_disconnect);
443EXPORT_SYMBOL(snd_card_free);
444EXPORT_SYMBOL(snd_card_free_in_thread);
445EXPORT_SYMBOL(snd_card_register);
446EXPORT_SYMBOL(snd_component_add);
447EXPORT_SYMBOL(snd_card_file_add);
448EXPORT_SYMBOL(snd_card_file_remove);
449#ifdef CONFIG_PM
450EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#endif
452 /* device.c */
453EXPORT_SYMBOL(snd_device_new);
454EXPORT_SYMBOL(snd_device_register);
455EXPORT_SYMBOL(snd_device_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* isadma.c */
Al Viro276bd312005-08-23 22:45:06 +0100457#ifdef CONFIG_ISA_DMA_API
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458EXPORT_SYMBOL(snd_dma_program);
459EXPORT_SYMBOL(snd_dma_disable);
460EXPORT_SYMBOL(snd_dma_pointer);
461#endif
462 /* info.c */
463#ifdef CONFIG_PROC_FS
464EXPORT_SYMBOL(snd_seq_root);
465EXPORT_SYMBOL(snd_iprintf);
466EXPORT_SYMBOL(snd_info_get_line);
467EXPORT_SYMBOL(snd_info_get_str);
468EXPORT_SYMBOL(snd_info_create_module_entry);
469EXPORT_SYMBOL(snd_info_create_card_entry);
470EXPORT_SYMBOL(snd_info_free_entry);
471EXPORT_SYMBOL(snd_info_register);
472EXPORT_SYMBOL(snd_info_unregister);
473EXPORT_SYMBOL(snd_card_proc_new);
474#endif
475 /* info_oss.c */
476#if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
477EXPORT_SYMBOL(snd_oss_info_register);
478#endif
479 /* control.c */
480EXPORT_SYMBOL(snd_ctl_new);
481EXPORT_SYMBOL(snd_ctl_new1);
482EXPORT_SYMBOL(snd_ctl_free_one);
483EXPORT_SYMBOL(snd_ctl_add);
484EXPORT_SYMBOL(snd_ctl_remove);
485EXPORT_SYMBOL(snd_ctl_remove_id);
486EXPORT_SYMBOL(snd_ctl_rename_id);
487EXPORT_SYMBOL(snd_ctl_find_numid);
488EXPORT_SYMBOL(snd_ctl_find_id);
489EXPORT_SYMBOL(snd_ctl_notify);
490EXPORT_SYMBOL(snd_ctl_register_ioctl);
491EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
492#ifdef CONFIG_COMPAT
493EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
494EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
495#endif
496EXPORT_SYMBOL(snd_ctl_elem_read);
497EXPORT_SYMBOL(snd_ctl_elem_write);
498 /* misc.c */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200499EXPORT_SYMBOL(release_and_free_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#ifdef CONFIG_SND_VERBOSE_PRINTK
501EXPORT_SYMBOL(snd_verbose_printk);
502#endif
503#if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
504EXPORT_SYMBOL(snd_verbose_printd);
505#endif