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