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