2 * Advanced Linux Sound Architecture
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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>
36 #define SNDRV_OS_MINORS 256
38 static int major
= CONFIG_SND_MAJOR
;
40 static int cards_limit
= 1;
41 static int device_mode
= S_IFCHR
| S_IRUGO
| S_IWUGO
;
43 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
44 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
45 MODULE_LICENSE("GPL");
46 module_param(major
, int, 0444);
47 MODULE_PARM_DESC(major
, "Major # for sound driver.");
48 module_param(cards_limit
, int, 0444);
49 MODULE_PARM_DESC(cards_limit
, "Count of auto-loadable soundcards.");
50 #ifdef CONFIG_DEVFS_FS
51 module_param(device_mode
, int, 0444);
52 MODULE_PARM_DESC(device_mode
, "Device file permission mask for devfs.");
54 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR
);
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.
62 static struct snd_minor
*snd_minors
[SNDRV_OS_MINORS
];
63 static DECLARE_MUTEX(sound_mutex
);
65 extern struct class *sound_class
;
71 * snd_request_card - try to load the card module
72 * @card: the card number
74 * Tries to load the module "snd-card-X" for the given card number
75 * via KMOD. Returns immediately if already loaded.
77 void snd_request_card(int card
)
81 if (! current
->fs
->root
)
83 read_lock(&snd_card_rwlock
);
84 locked
= snd_cards_lock
& (1 << card
);
85 read_unlock(&snd_card_rwlock
);
88 if (card
< 0 || card
>= cards_limit
)
90 request_module("snd-card-%i", card
);
93 static void snd_request_other(int minor
)
97 if (! current
->fs
->root
)
100 case SNDRV_MINOR_SEQUENCER
: str
= "snd-seq"; break;
101 case SNDRV_MINOR_TIMER
: str
= "snd-timer"; break;
107 #endif /* request_module support */
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)
114 * Checks that a minor device with the specified type is registered, and returns
115 * its user data pointer.
117 void *snd_lookup_minor_data(unsigned int minor
, int type
)
119 struct snd_minor
*mreg
;
122 if (minor
> ARRAY_SIZE(snd_minors
))
125 mreg
= snd_minors
[minor
];
126 if (mreg
&& mreg
->type
== type
)
127 private_data
= mreg
->private_data
;
134 static int snd_open(struct inode
*inode
, struct file
*file
)
136 int minor
= iminor(inode
);
137 int card
= SNDRV_MINOR_CARD(minor
);
138 int dev
= SNDRV_MINOR_DEVICE(minor
);
139 struct snd_minor
*mptr
= NULL
;
140 struct file_operations
*old_fops
;
143 if (dev
!= SNDRV_MINOR_GLOBAL
) {
144 if (snd_cards
[card
] == NULL
) {
146 snd_request_card(card
);
147 if (snd_cards
[card
] == NULL
)
153 if ((mptr
= snd_minors
[minor
]) == NULL
)
154 snd_request_other(minor
);
157 if (mptr
== NULL
&& (mptr
= snd_minors
[minor
]) == NULL
)
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
);
164 fops_put(file
->f_op
);
165 file
->f_op
= fops_get(old_fops
);
171 static struct file_operations snd_fops
=
173 .owner
= THIS_MODULE
,
177 static int snd_kernel_minor(int type
, struct snd_card
*card
, int dev
)
182 case SNDRV_DEVICE_TYPE_SEQUENCER
:
183 case SNDRV_DEVICE_TYPE_TIMER
:
186 case SNDRV_DEVICE_TYPE_CONTROL
:
187 snd_assert(card
!= NULL
, return -EINVAL
);
188 minor
= SNDRV_MINOR(card
->number
, type
);
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
);
200 snd_assert(minor
>= 0 && minor
< SNDRV_OS_MINORS
, return -EINVAL
);
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
209 * @f_ops: the file operations
210 * @private_data: user pointer for f_ops->open()
211 * @name: the device file name
213 * Registers an ALSA device file for the given card.
214 * The operators have to be set in reg parameter.
216 * Retrurns zero if successful, or a negative error code on failure.
218 int snd_register_device(int type
, struct snd_card
*card
, int dev
,
219 struct file_operations
*f_ops
, void *private_data
,
222 int minor
= snd_kernel_minor(type
, card
, dev
);
223 struct snd_minor
*preg
;
224 struct device
*device
= NULL
;
228 snd_assert(name
, return -EINVAL
);
229 preg
= kmalloc(sizeof(struct snd_minor
) + strlen(name
) + 1, GFP_KERNEL
);
233 preg
->card
= card
? card
->number
: -1;
236 preg
->private_data
= private_data
;
237 strcpy(preg
->name
, name
);
239 if (snd_minors
[minor
]) {
244 snd_minors
[minor
] = preg
;
245 if (type
!= SNDRV_DEVICE_TYPE_CONTROL
|| preg
->card
>= cards_limit
)
246 devfs_mk_cdev(MKDEV(major
, minor
), S_IFCHR
| device_mode
, "snd/%s", name
);
249 class_device_create(sound_class
, NULL
, MKDEV(major
, minor
), device
, "%s", name
);
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
261 * Unregisters the device file already registered via
262 * snd_register_device().
264 * Returns zero if sucecessful, or a negative error code on failure
266 int snd_unregister_device(int type
, struct snd_card
*card
, int dev
)
269 struct snd_minor
*mptr
;
271 cardnum
= card
? card
->number
: -1;
273 for (minor
= 0; minor
< ARRAY_SIZE(snd_minors
); ++minor
)
274 if ((mptr
= snd_minors
[minor
]) != NULL
&&
275 mptr
->type
== type
&&
276 mptr
->card
== cardnum
&&
279 if (minor
== ARRAY_SIZE(snd_minors
)) {
284 if (mptr
->type
!= SNDRV_DEVICE_TYPE_CONTROL
||
285 mptr
->card
>= cards_limit
) /* created in sound.c */
286 devfs_remove("snd/%s", mptr
->name
);
287 class_device_destroy(sound_class
, MKDEV(major
, minor
));
289 snd_minors
[minor
] = NULL
;
299 static struct snd_info_entry
*snd_minor_info_entry
= NULL
;
301 static const char *snd_device_type_name(int type
)
304 case SNDRV_DEVICE_TYPE_CONTROL
:
306 case SNDRV_DEVICE_TYPE_HWDEP
:
307 return "hardware dependent";
308 case SNDRV_DEVICE_TYPE_RAWMIDI
:
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
:
316 case SNDRV_DEVICE_TYPE_TIMER
:
323 static void snd_minor_info_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
326 struct snd_minor
*mptr
;
329 for (minor
= 0; minor
< SNDRV_OS_MINORS
; ++minor
) {
330 if (!(mptr
= snd_minors
[minor
]))
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
));
338 snd_iprintf(buffer
, "%3i: [%i] : %s\n",
340 snd_device_type_name(mptr
->type
));
342 snd_iprintf(buffer
, "%3i: : %s\n", minor
,
343 snd_device_type_name(mptr
->type
));
348 int __init
snd_minor_info_init(void)
350 struct snd_info_entry
*entry
;
352 entry
= snd_info_create_module_entry(THIS_MODULE
, "devices", NULL
);
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
);
361 snd_minor_info_entry
= entry
;
365 int __exit
snd_minor_info_done(void)
367 if (snd_minor_info_entry
)
368 snd_info_unregister(snd_minor_info_entry
);
376 static int __init
alsa_sound_init(void)
381 snd_ecards_limit
= cards_limit
;
383 if (register_chrdev(major
, "alsa", &snd_fops
)) {
384 snd_printk(KERN_ERR
"unable to register native major device number %d\n", major
);
388 if (snd_info_init() < 0) {
389 unregister_chrdev(major
, "alsa");
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
);
397 printk(KERN_INFO
"Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE
".\n");
402 static void __exit
alsa_sound_exit(void)
406 for (controlnum
= 0; controlnum
< cards_limit
; controlnum
++)
407 devfs_remove("snd/controlC%d", controlnum
);
409 snd_info_minor_unregister();
411 if (unregister_chrdev(major
, "alsa") != 0)
412 snd_printk(KERN_ERR
"unable to unregister major device number %d\n", major
);
416 module_init(alsa_sound_init
)
417 module_exit(alsa_sound_exit
)
420 EXPORT_SYMBOL(snd_major
);
421 EXPORT_SYMBOL(snd_ecards_limit
);
422 #if defined(CONFIG_KMOD)
423 EXPORT_SYMBOL(snd_request_card
);
425 EXPORT_SYMBOL(snd_register_device
);
426 EXPORT_SYMBOL(snd_unregister_device
);
427 EXPORT_SYMBOL(snd_lookup_minor_data
);
428 #if defined(CONFIG_SND_OSSEMUL)
429 EXPORT_SYMBOL(snd_register_oss_device
);
430 EXPORT_SYMBOL(snd_unregister_oss_device
);
431 EXPORT_SYMBOL(snd_lookup_oss_minor_data
);
434 EXPORT_SYMBOL(copy_to_user_fromio
);
435 EXPORT_SYMBOL(copy_from_user_toio
);
437 EXPORT_SYMBOL(snd_cards
);
438 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
439 EXPORT_SYMBOL(snd_mixer_oss_notify_callback
);
441 EXPORT_SYMBOL(snd_card_new
);
442 EXPORT_SYMBOL(snd_card_disconnect
);
443 EXPORT_SYMBOL(snd_card_free
);
444 EXPORT_SYMBOL(snd_card_free_in_thread
);
445 EXPORT_SYMBOL(snd_card_register
);
446 EXPORT_SYMBOL(snd_component_add
);
447 EXPORT_SYMBOL(snd_card_file_add
);
448 EXPORT_SYMBOL(snd_card_file_remove
);
450 EXPORT_SYMBOL(snd_power_wait
);
453 EXPORT_SYMBOL(snd_device_new
);
454 EXPORT_SYMBOL(snd_device_register
);
455 EXPORT_SYMBOL(snd_device_free
);
457 #ifdef CONFIG_ISA_DMA_API
458 EXPORT_SYMBOL(snd_dma_program
);
459 EXPORT_SYMBOL(snd_dma_disable
);
460 EXPORT_SYMBOL(snd_dma_pointer
);
463 #ifdef CONFIG_PROC_FS
464 EXPORT_SYMBOL(snd_seq_root
);
465 EXPORT_SYMBOL(snd_iprintf
);
466 EXPORT_SYMBOL(snd_info_get_line
);
467 EXPORT_SYMBOL(snd_info_get_str
);
468 EXPORT_SYMBOL(snd_info_create_module_entry
);
469 EXPORT_SYMBOL(snd_info_create_card_entry
);
470 EXPORT_SYMBOL(snd_info_free_entry
);
471 EXPORT_SYMBOL(snd_info_register
);
472 EXPORT_SYMBOL(snd_info_unregister
);
473 EXPORT_SYMBOL(snd_card_proc_new
);
476 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
477 EXPORT_SYMBOL(snd_oss_info_register
);
480 EXPORT_SYMBOL(snd_ctl_new
);
481 EXPORT_SYMBOL(snd_ctl_new1
);
482 EXPORT_SYMBOL(snd_ctl_free_one
);
483 EXPORT_SYMBOL(snd_ctl_add
);
484 EXPORT_SYMBOL(snd_ctl_remove
);
485 EXPORT_SYMBOL(snd_ctl_remove_id
);
486 EXPORT_SYMBOL(snd_ctl_rename_id
);
487 EXPORT_SYMBOL(snd_ctl_find_numid
);
488 EXPORT_SYMBOL(snd_ctl_find_id
);
489 EXPORT_SYMBOL(snd_ctl_notify
);
490 EXPORT_SYMBOL(snd_ctl_register_ioctl
);
491 EXPORT_SYMBOL(snd_ctl_unregister_ioctl
);
493 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat
);
494 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat
);
496 EXPORT_SYMBOL(snd_ctl_elem_read
);
497 EXPORT_SYMBOL(snd_ctl_elem_write
);
499 EXPORT_SYMBOL(release_and_free_resource
);
500 #ifdef CONFIG_SND_VERBOSE_PRINTK
501 EXPORT_SYMBOL(snd_verbose_printk
);
503 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
504 EXPORT_SYMBOL(snd_verbose_printd
);