vserver 1.9.3
[linux-2.6.git] / sound / core / sound.c
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>
35 #include <linux/device.h>
36
37 #define SNDRV_OS_MINORS 256
38
39 static int major = CONFIG_SND_MAJOR;
40 int snd_major;
41 static int cards_limit = 1;
42 static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
43
44 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
45 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
46 MODULE_LICENSE("GPL");
47 module_param(major, int, 0444);
48 MODULE_PARM_DESC(major, "Major # for sound driver.");
49 module_param(cards_limit, int, 0444);
50 MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
51 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
52 #ifdef CONFIG_DEVFS_FS
53 module_param(device_mode, int, 0444);
54 MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
55 #endif
56 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
57
58 /* this one holds the actual max. card number currently available.
59  * as default, it's identical with cards_limit option.  when more
60  * modules are loaded manually, this limit number increases, too.
61  */
62 int snd_ecards_limit;
63
64 static struct list_head snd_minors_hash[SNDRV_CARDS];
65
66 static DECLARE_MUTEX(sound_mutex);
67
68 extern struct class_simple *sound_class;
69
70
71 #ifdef CONFIG_KMOD
72
73 /**
74  * snd_request_card - try to load the card module
75  * @card: the card number
76  *
77  * Tries to load the module "snd-card-X" for the given card number
78  * via KMOD.  Returns immediately if already loaded.
79  */
80 void snd_request_card(int card)
81 {
82         int locked;
83
84         if (! current->fs->root)
85                 return;
86         read_lock(&snd_card_rwlock);
87         locked = snd_cards_lock & (1 << card);
88         read_unlock(&snd_card_rwlock);
89         if (locked)
90                 return;
91         if (card < 0 || card >= cards_limit)
92                 return;
93         request_module("snd-card-%i", card);
94 }
95
96 static void snd_request_other(int minor)
97 {
98         char *str;
99
100         if (! current->fs->root)
101                 return;
102         switch (minor) {
103         case SNDRV_MINOR_SEQUENCER:     str = "snd-seq";        break;
104         case SNDRV_MINOR_TIMER:         str = "snd-timer";      break;
105         default:                        return;
106         }
107         request_module(str);
108 }
109
110 #endif                          /* request_module support */
111
112 static snd_minor_t *snd_minor_search(int minor)
113 {
114         struct list_head *list;
115         snd_minor_t *mptr;
116
117         list_for_each(list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]) {
118                 mptr = list_entry(list, snd_minor_t, list);
119                 if (mptr->number == minor)
120                         return mptr;
121         }
122         return NULL;
123 }
124
125 static int snd_open(struct inode *inode, struct file *file)
126 {
127         int minor = iminor(inode);
128         int card = SNDRV_MINOR_CARD(minor);
129         int dev = SNDRV_MINOR_DEVICE(minor);
130         snd_minor_t *mptr = NULL;
131         struct file_operations *old_fops;
132         int err = 0;
133
134         if (dev != SNDRV_MINOR_SEQUENCER && dev != SNDRV_MINOR_TIMER) {
135                 if (snd_cards[card] == NULL) {
136 #ifdef CONFIG_KMOD
137                         snd_request_card(card);
138                         if (snd_cards[card] == NULL)
139 #endif
140                                 return -ENODEV;
141                 }
142         } else {
143 #ifdef CONFIG_KMOD
144                 if ((mptr = snd_minor_search(minor)) == NULL)
145                         snd_request_other(minor);
146 #endif
147         }
148         if (mptr == NULL && (mptr = snd_minor_search(minor)) == NULL)
149                 return -ENODEV;
150         old_fops = file->f_op;
151         file->f_op = fops_get(mptr->f_ops);
152         if (file->f_op->open)
153                 err = file->f_op->open(inode, file);
154         if (err) {
155                 fops_put(file->f_op);
156                 file->f_op = fops_get(old_fops);
157         }
158         fops_put(old_fops);
159         return err;
160 }
161
162 struct file_operations snd_fops =
163 {
164         .owner =        THIS_MODULE,
165         .open =         snd_open
166 };
167
168 static int snd_kernel_minor(int type, snd_card_t * card, int dev)
169 {
170         int minor;
171
172         switch (type) {
173         case SNDRV_DEVICE_TYPE_SEQUENCER:
174         case SNDRV_DEVICE_TYPE_TIMER:
175                 minor = type;
176                 break;
177         case SNDRV_DEVICE_TYPE_CONTROL:
178                 snd_assert(card != NULL, return -EINVAL);
179                 minor = SNDRV_MINOR(card->number, type);
180                 break;
181         case SNDRV_DEVICE_TYPE_HWDEP:
182         case SNDRV_DEVICE_TYPE_RAWMIDI:
183         case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
184         case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
185                 snd_assert(card != NULL, return -EINVAL);
186                 minor = SNDRV_MINOR(card->number, type + dev);
187                 break;
188         default:
189                 return -EINVAL;
190         }
191         snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
192         return minor;
193 }
194
195 /**
196  * snd_register_device - Register the ALSA device file for the card
197  * @type: the device type, SNDRV_DEVICE_TYPE_XXX
198  * @card: the card instance
199  * @dev: the device index
200  * @reg: the snd_minor_t record
201  * @name: the device file name
202  *
203  * Registers an ALSA device file for the given card.
204  * The operators have to be set in reg parameter.
205  *
206  * Retrurns zero if successful, or a negative error code on failure.
207  */
208 int snd_register_device(int type, snd_card_t * card, int dev, snd_minor_t * reg, const char *name)
209 {
210         int minor = snd_kernel_minor(type, card, dev);
211         snd_minor_t *preg;
212         struct device *device = NULL;
213
214         if (minor < 0)
215                 return minor;
216         snd_assert(name, return -EINVAL);
217         preg = (snd_minor_t *)kmalloc(sizeof(snd_minor_t) + strlen(name) + 1, GFP_KERNEL);
218         if (preg == NULL)
219                 return -ENOMEM;
220         *preg = *reg;
221         preg->number = minor;
222         preg->device = dev;
223         strcpy(preg->name, name);
224         down(&sound_mutex);
225         if (snd_minor_search(minor)) {
226                 up(&sound_mutex);
227                 kfree(preg);
228                 return -EBUSY;
229         }
230         list_add_tail(&preg->list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]);
231         if (strncmp(name, "controlC", 8) || card->number >= cards_limit) {
232                 devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
233                 if (card)
234                         device = card->dev;
235                 class_simple_device_add(sound_class, MKDEV(major, minor), device, name);
236         }
237
238         up(&sound_mutex);
239         return 0;
240 }
241
242 /**
243  * snd_unregister_device - unregister the device on the given card
244  * @type: the device type, SNDRV_DEVICE_TYPE_XXX
245  * @card: the card instance
246  * @dev: the device index
247  *
248  * Unregisters the device file already registered via
249  * snd_register_device().
250  *
251  * Returns zero if sucecessful, or a negative error code on failure
252  */
253 int snd_unregister_device(int type, snd_card_t * card, int dev)
254 {
255         int minor = snd_kernel_minor(type, card, dev);
256         snd_minor_t *mptr;
257
258         if (minor < 0)
259                 return minor;
260         down(&sound_mutex);
261         if ((mptr = snd_minor_search(minor)) == NULL) {
262                 up(&sound_mutex);
263                 return -EINVAL;
264         }
265
266         if (strncmp(mptr->name, "controlC", 8) || card->number >= cards_limit) { /* created in sound.c */
267                 devfs_remove("snd/%s", mptr->name);
268                 class_simple_device_remove(MKDEV(major, minor));
269         }
270
271         list_del(&mptr->list);
272         up(&sound_mutex);
273         kfree(mptr);
274         return 0;
275 }
276
277 /*
278  *  INFO PART
279  */
280
281 static snd_info_entry_t *snd_minor_info_entry = NULL;
282
283 static void snd_minor_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
284 {
285         int card, device;
286         struct list_head *list;
287         snd_minor_t *mptr;
288
289         down(&sound_mutex);
290         for (card = 0; card < SNDRV_CARDS; card++) {
291                 list_for_each(list, &snd_minors_hash[card]) {
292                         mptr = list_entry(list, snd_minor_t, list);
293                         if (SNDRV_MINOR_DEVICE(mptr->number) != SNDRV_MINOR_SEQUENCER) {
294                                 if ((device = mptr->device) >= 0)
295                                         snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, device, mptr->comment);
296                                 else
297                                         snd_iprintf(buffer, "%3i: [%i]   : %s\n", mptr->number, card, mptr->comment);
298                         } else {
299                                 snd_iprintf(buffer, "%3i:       : %s\n", mptr->number, mptr->comment);
300                         }
301                 }
302         }
303         up(&sound_mutex);
304 }
305
306 int __init snd_minor_info_init(void)
307 {
308         snd_info_entry_t *entry;
309
310         entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
311         if (entry) {
312                 entry->c.text.read_size = PAGE_SIZE;
313                 entry->c.text.read = snd_minor_info_read;
314                 if (snd_info_register(entry) < 0) {
315                         snd_info_free_entry(entry);
316                         entry = NULL;
317                 }
318         }
319         snd_minor_info_entry = entry;
320         return 0;
321 }
322
323 int __exit snd_minor_info_done(void)
324 {
325         if (snd_minor_info_entry)
326                 snd_info_unregister(snd_minor_info_entry);
327         return 0;
328 }
329
330 /*
331  *  INIT PART
332  */
333
334 static int __init alsa_sound_init(void)
335 {
336         short controlnum;
337         int err;
338         int card;
339
340         snd_major = major;
341         snd_ecards_limit = cards_limit;
342         for (card = 0; card < SNDRV_CARDS; card++)
343                 INIT_LIST_HEAD(&snd_minors_hash[card]);
344         if ((err = snd_oss_init_module()) < 0)
345                 return err;
346         devfs_mk_dir("snd");
347         if (register_chrdev(major, "alsa", &snd_fops)) {
348                 snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
349                 devfs_remove("snd");
350                 return -EIO;
351         }
352         snd_memory_init();
353         if (snd_info_init() < 0) {
354                 snd_memory_done();
355                 unregister_chrdev(major, "alsa");
356                 devfs_remove("snd");
357                 return -ENOMEM;
358         }
359         snd_info_minor_register();
360         for (controlnum = 0; controlnum < cards_limit; controlnum++) {
361                 devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
362                 class_simple_device_add(sound_class, MKDEV(major, controlnum<<5), NULL, "controlC%d", controlnum);
363         }
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
370 static 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                 class_simple_device_remove(MKDEV(major, controlnum<<5));
377         }
378
379         snd_info_minor_unregister();
380         snd_info_done();
381         snd_memory_done();
382         if (unregister_chrdev(major, "alsa") != 0)
383                 snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
384         devfs_remove("snd");
385 }
386
387 module_init(alsa_sound_init)
388 module_exit(alsa_sound_exit)
389
390   /* sound.c */
391 EXPORT_SYMBOL(snd_major);
392 EXPORT_SYMBOL(snd_ecards_limit);
393 #if defined(CONFIG_KMOD)
394 EXPORT_SYMBOL(snd_request_card);
395 #endif
396 EXPORT_SYMBOL(snd_register_device);
397 EXPORT_SYMBOL(snd_unregister_device);
398 #if defined(CONFIG_SND_OSSEMUL)
399 EXPORT_SYMBOL(snd_register_oss_device);
400 EXPORT_SYMBOL(snd_unregister_oss_device);
401 #endif
402   /* memory.c */
403 #ifdef CONFIG_SND_DEBUG_MEMORY
404 EXPORT_SYMBOL(snd_hidden_kmalloc);
405 EXPORT_SYMBOL(snd_hidden_kcalloc);
406 EXPORT_SYMBOL(snd_hidden_kfree);
407 EXPORT_SYMBOL(snd_hidden_vmalloc);
408 EXPORT_SYMBOL(snd_hidden_vfree);
409 #endif
410 EXPORT_SYMBOL(snd_kmalloc_strdup);
411 EXPORT_SYMBOL(copy_to_user_fromio);
412 EXPORT_SYMBOL(copy_from_user_toio);
413   /* init.c */
414 EXPORT_SYMBOL(snd_cards_count);
415 EXPORT_SYMBOL(snd_cards);
416 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
417 EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
418 #endif
419 EXPORT_SYMBOL(snd_card_new);
420 EXPORT_SYMBOL(snd_card_disconnect);
421 EXPORT_SYMBOL(snd_card_free);
422 EXPORT_SYMBOL(snd_card_free_in_thread);
423 EXPORT_SYMBOL(snd_card_register);
424 EXPORT_SYMBOL(snd_component_add);
425 EXPORT_SYMBOL(snd_card_file_add);
426 EXPORT_SYMBOL(snd_card_file_remove);
427 #ifdef CONFIG_PM
428 EXPORT_SYMBOL(snd_power_wait);
429 EXPORT_SYMBOL(snd_card_set_pm_callback);
430 EXPORT_SYMBOL(snd_card_set_dev_pm_callback);
431 #ifdef CONFIG_PCI
432 EXPORT_SYMBOL(snd_card_pci_suspend);
433 EXPORT_SYMBOL(snd_card_pci_resume);
434 #endif
435 #endif
436   /* device.c */
437 EXPORT_SYMBOL(snd_device_new);
438 EXPORT_SYMBOL(snd_device_register);
439 EXPORT_SYMBOL(snd_device_free);
440 EXPORT_SYMBOL(snd_device_free_all);
441   /* isadma.c */
442 #ifdef CONFIG_ISA
443 EXPORT_SYMBOL(snd_dma_program);
444 EXPORT_SYMBOL(snd_dma_disable);
445 EXPORT_SYMBOL(snd_dma_pointer);
446 #endif
447   /* info.c */
448 #ifdef CONFIG_PROC_FS
449 EXPORT_SYMBOL(snd_seq_root);
450 EXPORT_SYMBOL(snd_create_proc_entry);
451 EXPORT_SYMBOL(snd_remove_proc_entry);
452 EXPORT_SYMBOL(snd_iprintf);
453 EXPORT_SYMBOL(snd_info_get_line);
454 EXPORT_SYMBOL(snd_info_get_str);
455 EXPORT_SYMBOL(snd_info_create_module_entry);
456 EXPORT_SYMBOL(snd_info_create_card_entry);
457 EXPORT_SYMBOL(snd_info_free_entry);
458 EXPORT_SYMBOL(snd_info_register);
459 EXPORT_SYMBOL(snd_info_unregister);
460 EXPORT_SYMBOL(snd_card_proc_new);
461 #endif
462   /* info_oss.c */
463 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
464 EXPORT_SYMBOL(snd_oss_info_register);
465 #endif
466   /* control.c */
467 EXPORT_SYMBOL(snd_ctl_new);
468 EXPORT_SYMBOL(snd_ctl_new1);
469 EXPORT_SYMBOL(snd_ctl_free_one);
470 EXPORT_SYMBOL(snd_ctl_add);
471 EXPORT_SYMBOL(snd_ctl_remove);
472 EXPORT_SYMBOL(snd_ctl_remove_id);
473 EXPORT_SYMBOL(snd_ctl_rename_id);
474 EXPORT_SYMBOL(snd_ctl_find_numid);
475 EXPORT_SYMBOL(snd_ctl_find_id);
476 EXPORT_SYMBOL(snd_ctl_notify);
477 EXPORT_SYMBOL(snd_ctl_register_ioctl);
478 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
479   /* misc.c */
480 EXPORT_SYMBOL(snd_task_name);
481 #ifdef CONFIG_SND_VERBOSE_PRINTK
482 EXPORT_SYMBOL(snd_verbose_printk);
483 #endif
484 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
485 EXPORT_SYMBOL(snd_verbose_printd);
486 #endif
487   /* wrappers */
488 #ifdef CONFIG_SND_DEBUG_MEMORY
489 EXPORT_SYMBOL(snd_wrapper_kmalloc);
490 EXPORT_SYMBOL(snd_wrapper_kfree);
491 EXPORT_SYMBOL(snd_wrapper_vmalloc);
492 EXPORT_SYMBOL(snd_wrapper_vfree);
493 #endif