[ALSA] ice1712 - Fix Front Digital Input of Terratec DMX 6Fire
[deliverable/linux.git] / sound / core / sound.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
35
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
6983b724 62static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
1da177e4
LT
63static DECLARE_MUTEX(sound_mutex);
64
619e666b 65extern struct class *sound_class;
1da177e4
LT
66
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
f87135f5
CL
109/**
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
1da177e4
LT
134static int snd_open(struct inode *inode, struct file *file)
135{
332682b1 136 unsigned int minor = iminor(inode);
512bbd6a 137 struct snd_minor *mptr = NULL;
1da177e4
LT
138 struct file_operations *old_fops;
139 int err = 0;
140
332682b1
CL
141 if (minor > ARRAY_SIZE(snd_minors))
142 return -ENODEV;
143 mptr = snd_minors[minor];
144 if (mptr == NULL) {
1da177e4 145#ifdef CONFIG_KMOD
332682b1
CL
146 int dev = SNDRV_MINOR_DEVICE(minor);
147 if (dev == SNDRV_MINOR_CONTROL) {
148 /* /dev/aloadC? */
149 int card = SNDRV_MINOR_CARD(minor);
1da177e4 150 if (snd_cards[card] == NULL)
332682b1
CL
151 snd_request_card(card);
152 } else if (dev == SNDRV_MINOR_GLOBAL) {
153 /* /dev/aloadSEQ */
1da177e4 154 snd_request_other(minor);
332682b1
CL
155 }
156#ifndef CONFIG_SND_DYNAMIC_MINORS
157 /* /dev/snd/{controlC?,seq} */
158 mptr = snd_minors[minor];
159 if (mptr == NULL)
160#endif
1da177e4 161#endif
332682b1 162 return -ENODEV;
1da177e4 163 }
1da177e4
LT
164 old_fops = file->f_op;
165 file->f_op = fops_get(mptr->f_ops);
166 if (file->f_op->open)
167 err = file->f_op->open(inode, file);
168 if (err) {
169 fops_put(file->f_op);
170 file->f_op = fops_get(old_fops);
171 }
172 fops_put(old_fops);
173 return err;
174}
175
176static struct file_operations snd_fops =
177{
178 .owner = THIS_MODULE,
179 .open = snd_open
180};
181
332682b1
CL
182#ifdef CONFIG_SND_DYNAMIC_MINORS
183static int snd_find_free_minor(void)
184{
185 int minor;
186
187 for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
188 /* skip minors still used statically for autoloading devices */
189 if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
190 minor == SNDRV_MINOR_SEQUENCER)
191 continue;
192 if (!snd_minors[minor])
193 return minor;
194 }
195 return -EBUSY;
196}
197#else
512bbd6a 198static int snd_kernel_minor(int type, struct snd_card *card, int dev)
1da177e4
LT
199{
200 int minor;
201
202 switch (type) {
203 case SNDRV_DEVICE_TYPE_SEQUENCER:
204 case SNDRV_DEVICE_TYPE_TIMER:
205 minor = type;
206 break;
207 case SNDRV_DEVICE_TYPE_CONTROL:
208 snd_assert(card != NULL, return -EINVAL);
209 minor = SNDRV_MINOR(card->number, type);
210 break;
211 case SNDRV_DEVICE_TYPE_HWDEP:
212 case SNDRV_DEVICE_TYPE_RAWMIDI:
213 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
214 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
215 snd_assert(card != NULL, return -EINVAL);
216 minor = SNDRV_MINOR(card->number, type + dev);
217 break;
218 default:
219 return -EINVAL;
220 }
221 snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
222 return minor;
223}
332682b1 224#endif
1da177e4
LT
225
226/**
227 * snd_register_device - Register the ALSA device file for the card
228 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
229 * @card: the card instance
230 * @dev: the device index
2af677fc 231 * @f_ops: the file operations
f87135f5 232 * @private_data: user pointer for f_ops->open()
1da177e4
LT
233 * @name: the device file name
234 *
235 * Registers an ALSA device file for the given card.
236 * The operators have to be set in reg parameter.
237 *
238 * Retrurns zero if successful, or a negative error code on failure.
239 */
2af677fc 240int snd_register_device(int type, struct snd_card *card, int dev,
f87135f5
CL
241 struct file_operations *f_ops, void *private_data,
242 const char *name)
1da177e4 243{
332682b1 244 int minor;
512bbd6a 245 struct snd_minor *preg;
1da177e4
LT
246 struct device *device = NULL;
247
1da177e4 248 snd_assert(name, return -EINVAL);
6983b724 249 preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
1da177e4
LT
250 if (preg == NULL)
251 return -ENOMEM;
2af677fc 252 preg->type = type;
6983b724 253 preg->card = card ? card->number : -1;
1da177e4 254 preg->device = dev;
2af677fc 255 preg->f_ops = f_ops;
f87135f5 256 preg->private_data = private_data;
1da177e4
LT
257 strcpy(preg->name, name);
258 down(&sound_mutex);
332682b1
CL
259#ifdef CONFIG_SND_DYNAMIC_MINORS
260 minor = snd_find_free_minor();
261#else
262 minor = snd_kernel_minor(type, card, dev);
263 if (minor >= 0 && snd_minors[minor])
264 minor = -EBUSY;
265#endif
266 if (minor < 0) {
1da177e4
LT
267 up(&sound_mutex);
268 kfree(preg);
332682b1 269 return minor;
1da177e4 270 }
6983b724
CL
271 snd_minors[minor] = preg;
272 if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
1da177e4
LT
273 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
274 if (card)
275 device = card->dev;
53f46542 276 class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
1da177e4
LT
277
278 up(&sound_mutex);
279 return 0;
280}
281
282/**
283 * snd_unregister_device - unregister the device on the given card
284 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
285 * @card: the card instance
286 * @dev: the device index
287 *
288 * Unregisters the device file already registered via
289 * snd_register_device().
290 *
291 * Returns zero if sucecessful, or a negative error code on failure
292 */
512bbd6a 293int snd_unregister_device(int type, struct snd_card *card, int dev)
1da177e4 294{
f87135f5 295 int cardnum, minor;
512bbd6a 296 struct snd_minor *mptr;
1da177e4 297
f87135f5 298 cardnum = card ? card->number : -1;
1da177e4 299 down(&sound_mutex);
f87135f5
CL
300 for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
301 if ((mptr = snd_minors[minor]) != NULL &&
302 mptr->type == type &&
303 mptr->card == cardnum &&
304 mptr->device == dev)
305 break;
306 if (minor == ARRAY_SIZE(snd_minors)) {
1da177e4
LT
307 up(&sound_mutex);
308 return -EINVAL;
309 }
310
6983b724
CL
311 if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
312 mptr->card >= cards_limit) /* created in sound.c */
1da177e4 313 devfs_remove("snd/%s", mptr->name);
619e666b 314 class_device_destroy(sound_class, MKDEV(major, minor));
1da177e4 315
6983b724 316 snd_minors[minor] = NULL;
1da177e4
LT
317 up(&sound_mutex);
318 kfree(mptr);
319 return 0;
320}
321
322/*
323 * INFO PART
324 */
325
512bbd6a 326static struct snd_info_entry *snd_minor_info_entry = NULL;
1da177e4 327
2af677fc
CL
328static const char *snd_device_type_name(int type)
329{
330 switch (type) {
331 case SNDRV_DEVICE_TYPE_CONTROL:
332 return "control";
333 case SNDRV_DEVICE_TYPE_HWDEP:
334 return "hardware dependent";
335 case SNDRV_DEVICE_TYPE_RAWMIDI:
336 return "raw midi";
337 case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
338 return "digital audio playback";
339 case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
340 return "digital audio capture";
341 case SNDRV_DEVICE_TYPE_SEQUENCER:
342 return "sequencer";
343 case SNDRV_DEVICE_TYPE_TIMER:
344 return "timer";
345 default:
346 return "?";
347 }
348}
349
512bbd6a 350static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1da177e4 351{
6983b724 352 int minor;
512bbd6a 353 struct snd_minor *mptr;
1da177e4
LT
354
355 down(&sound_mutex);
6983b724
CL
356 for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
357 if (!(mptr = snd_minors[minor]))
358 continue;
359 if (mptr->card >= 0) {
360 if (mptr->device >= 0)
d001544d 361 snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
6983b724
CL
362 minor, mptr->card, mptr->device,
363 snd_device_type_name(mptr->type));
364 else
d001544d 365 snd_iprintf(buffer, "%3i: [%2i] : %s\n",
6983b724
CL
366 minor, mptr->card,
367 snd_device_type_name(mptr->type));
368 } else
d001544d 369 snd_iprintf(buffer, "%3i: : %s\n", minor,
6983b724 370 snd_device_type_name(mptr->type));
1da177e4
LT
371 }
372 up(&sound_mutex);
373}
374
375int __init snd_minor_info_init(void)
376{
512bbd6a 377 struct snd_info_entry *entry;
1da177e4
LT
378
379 entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
380 if (entry) {
381 entry->c.text.read_size = PAGE_SIZE;
382 entry->c.text.read = snd_minor_info_read;
383 if (snd_info_register(entry) < 0) {
384 snd_info_free_entry(entry);
385 entry = NULL;
386 }
387 }
388 snd_minor_info_entry = entry;
389 return 0;
390}
391
392int __exit snd_minor_info_done(void)
393{
394 if (snd_minor_info_entry)
395 snd_info_unregister(snd_minor_info_entry);
396 return 0;
397}
398
399/*
400 * INIT PART
401 */
402
403static int __init alsa_sound_init(void)
404{
405 short controlnum;
1da177e4
LT
406
407 snd_major = major;
408 snd_ecards_limit = cards_limit;
1da177e4
LT
409 devfs_mk_dir("snd");
410 if (register_chrdev(major, "alsa", &snd_fops)) {
411 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
412 devfs_remove("snd");
413 return -EIO;
414 }
1da177e4 415 if (snd_info_init() < 0) {
1da177e4
LT
416 unregister_chrdev(major, "alsa");
417 devfs_remove("snd");
418 return -ENOMEM;
419 }
420 snd_info_minor_register();
421 for (controlnum = 0; controlnum < cards_limit; controlnum++)
422 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
423#ifndef MODULE
424 printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
425#endif
426 return 0;
427}
428
429static void __exit alsa_sound_exit(void)
430{
431 short controlnum;
432
433 for (controlnum = 0; controlnum < cards_limit; controlnum++)
434 devfs_remove("snd/controlC%d", controlnum);
435
436 snd_info_minor_unregister();
437 snd_info_done();
1da177e4
LT
438 if (unregister_chrdev(major, "alsa") != 0)
439 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
440 devfs_remove("snd");
441}
442
443module_init(alsa_sound_init)
444module_exit(alsa_sound_exit)
445
446 /* sound.c */
447EXPORT_SYMBOL(snd_major);
448EXPORT_SYMBOL(snd_ecards_limit);
449#if defined(CONFIG_KMOD)
450EXPORT_SYMBOL(snd_request_card);
451#endif
452EXPORT_SYMBOL(snd_register_device);
453EXPORT_SYMBOL(snd_unregister_device);
f87135f5 454EXPORT_SYMBOL(snd_lookup_minor_data);
1da177e4
LT
455#if defined(CONFIG_SND_OSSEMUL)
456EXPORT_SYMBOL(snd_register_oss_device);
457EXPORT_SYMBOL(snd_unregister_oss_device);
f87135f5 458EXPORT_SYMBOL(snd_lookup_oss_minor_data);
1da177e4
LT
459#endif
460 /* memory.c */
1da177e4
LT
461EXPORT_SYMBOL(copy_to_user_fromio);
462EXPORT_SYMBOL(copy_from_user_toio);
463 /* init.c */
464EXPORT_SYMBOL(snd_cards);
465#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
466EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
467#endif
468EXPORT_SYMBOL(snd_card_new);
469EXPORT_SYMBOL(snd_card_disconnect);
470EXPORT_SYMBOL(snd_card_free);
471EXPORT_SYMBOL(snd_card_free_in_thread);
472EXPORT_SYMBOL(snd_card_register);
473EXPORT_SYMBOL(snd_component_add);
474EXPORT_SYMBOL(snd_card_file_add);
475EXPORT_SYMBOL(snd_card_file_remove);
476#ifdef CONFIG_PM
477EXPORT_SYMBOL(snd_power_wait);
1da177e4
LT
478#endif
479 /* device.c */
480EXPORT_SYMBOL(snd_device_new);
481EXPORT_SYMBOL(snd_device_register);
482EXPORT_SYMBOL(snd_device_free);
1da177e4 483 /* isadma.c */
276bd31c 484#ifdef CONFIG_ISA_DMA_API
1da177e4
LT
485EXPORT_SYMBOL(snd_dma_program);
486EXPORT_SYMBOL(snd_dma_disable);
487EXPORT_SYMBOL(snd_dma_pointer);
488#endif
489 /* info.c */
490#ifdef CONFIG_PROC_FS
491EXPORT_SYMBOL(snd_seq_root);
492EXPORT_SYMBOL(snd_iprintf);
493EXPORT_SYMBOL(snd_info_get_line);
494EXPORT_SYMBOL(snd_info_get_str);
495EXPORT_SYMBOL(snd_info_create_module_entry);
496EXPORT_SYMBOL(snd_info_create_card_entry);
497EXPORT_SYMBOL(snd_info_free_entry);
498EXPORT_SYMBOL(snd_info_register);
499EXPORT_SYMBOL(snd_info_unregister);
500EXPORT_SYMBOL(snd_card_proc_new);
501#endif
502 /* info_oss.c */
503#if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
504EXPORT_SYMBOL(snd_oss_info_register);
505#endif
506 /* control.c */
507EXPORT_SYMBOL(snd_ctl_new);
508EXPORT_SYMBOL(snd_ctl_new1);
509EXPORT_SYMBOL(snd_ctl_free_one);
510EXPORT_SYMBOL(snd_ctl_add);
511EXPORT_SYMBOL(snd_ctl_remove);
512EXPORT_SYMBOL(snd_ctl_remove_id);
513EXPORT_SYMBOL(snd_ctl_rename_id);
514EXPORT_SYMBOL(snd_ctl_find_numid);
515EXPORT_SYMBOL(snd_ctl_find_id);
516EXPORT_SYMBOL(snd_ctl_notify);
517EXPORT_SYMBOL(snd_ctl_register_ioctl);
518EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
519#ifdef CONFIG_COMPAT
520EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
521EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
522#endif
523EXPORT_SYMBOL(snd_ctl_elem_read);
524EXPORT_SYMBOL(snd_ctl_elem_write);
525 /* misc.c */
b1d5776d 526EXPORT_SYMBOL(release_and_free_resource);
1da177e4
LT
527#ifdef CONFIG_SND_VERBOSE_PRINTK
528EXPORT_SYMBOL(snd_verbose_printk);
529#endif
530#if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
531EXPORT_SYMBOL(snd_verbose_printd);
1da177e4 532#endif
This page took 0.10313 seconds and 5 git commands to generate.