]>
Commit | Line | Data |
---|---|---|
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> | |
9a1a2a1d | 26 | #include <linux/device.h> |
1da177e4 LT |
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> | |
1a60d4c5 | 36 | #include <linux/mutex.h> |
1da177e4 LT |
37 | |
38 | #define SNDRV_OS_MINORS 256 | |
39 | ||
40 | static int major = CONFIG_SND_MAJOR; | |
41 | int snd_major; | |
c0d3fb39 TI |
42 | EXPORT_SYMBOL(snd_major); |
43 | ||
1da177e4 LT |
44 | static int cards_limit = 1; |
45 | static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO; | |
46 | ||
47 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); | |
48 | MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards."); | |
49 | MODULE_LICENSE("GPL"); | |
50 | module_param(major, int, 0444); | |
51 | MODULE_PARM_DESC(major, "Major # for sound driver."); | |
52 | module_param(cards_limit, int, 0444); | |
53 | MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards."); | |
54 | #ifdef CONFIG_DEVFS_FS | |
55 | module_param(device_mode, int, 0444); | |
56 | MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs."); | |
57 | #endif | |
58 | MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR); | |
59 | ||
60 | /* this one holds the actual max. card number currently available. | |
61 | * as default, it's identical with cards_limit option. when more | |
62 | * modules are loaded manually, this limit number increases, too. | |
63 | */ | |
64 | int snd_ecards_limit; | |
c0d3fb39 | 65 | EXPORT_SYMBOL(snd_ecards_limit); |
1da177e4 | 66 | |
6983b724 | 67 | static struct snd_minor *snd_minors[SNDRV_OS_MINORS]; |
1a60d4c5 | 68 | static DEFINE_MUTEX(sound_mutex); |
1da177e4 | 69 | |
619e666b | 70 | extern struct class *sound_class; |
1da177e4 LT |
71 | |
72 | ||
73 | #ifdef CONFIG_KMOD | |
74 | ||
75 | /** | |
76 | * snd_request_card - try to load the card module | |
77 | * @card: the card number | |
78 | * | |
79 | * Tries to load the module "snd-card-X" for the given card number | |
80 | * via KMOD. Returns immediately if already loaded. | |
81 | */ | |
82 | void snd_request_card(int card) | |
83 | { | |
84 | int locked; | |
85 | ||
86 | if (! current->fs->root) | |
87 | return; | |
88 | read_lock(&snd_card_rwlock); | |
89 | locked = snd_cards_lock & (1 << card); | |
90 | read_unlock(&snd_card_rwlock); | |
91 | if (locked) | |
92 | return; | |
93 | if (card < 0 || card >= cards_limit) | |
94 | return; | |
95 | request_module("snd-card-%i", card); | |
96 | } | |
97 | ||
c0d3fb39 TI |
98 | EXPORT_SYMBOL(snd_request_card); |
99 | ||
1da177e4 LT |
100 | static void snd_request_other(int minor) |
101 | { | |
102 | char *str; | |
103 | ||
104 | if (! current->fs->root) | |
105 | return; | |
106 | switch (minor) { | |
107 | case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break; | |
108 | case SNDRV_MINOR_TIMER: str = "snd-timer"; break; | |
109 | default: return; | |
110 | } | |
111 | request_module(str); | |
112 | } | |
113 | ||
114 | #endif /* request_module support */ | |
115 | ||
f87135f5 CL |
116 | /** |
117 | * snd_lookup_minor_data - get user data of a registered device | |
118 | * @minor: the minor number | |
119 | * @type: device type (SNDRV_DEVICE_TYPE_XXX) | |
120 | * | |
121 | * Checks that a minor device with the specified type is registered, and returns | |
122 | * its user data pointer. | |
123 | */ | |
124 | void *snd_lookup_minor_data(unsigned int minor, int type) | |
125 | { | |
126 | struct snd_minor *mreg; | |
127 | void *private_data; | |
128 | ||
3a63e444 | 129 | if (minor >= ARRAY_SIZE(snd_minors)) |
f87135f5 | 130 | return NULL; |
1a60d4c5 | 131 | mutex_lock(&sound_mutex); |
f87135f5 CL |
132 | mreg = snd_minors[minor]; |
133 | if (mreg && mreg->type == type) | |
134 | private_data = mreg->private_data; | |
135 | else | |
136 | private_data = NULL; | |
1a60d4c5 | 137 | mutex_unlock(&sound_mutex); |
f87135f5 CL |
138 | return private_data; |
139 | } | |
140 | ||
c0d3fb39 TI |
141 | EXPORT_SYMBOL(snd_lookup_minor_data); |
142 | ||
1da177e4 LT |
143 | static int snd_open(struct inode *inode, struct file *file) |
144 | { | |
332682b1 | 145 | unsigned int minor = iminor(inode); |
512bbd6a | 146 | struct snd_minor *mptr = NULL; |
99ac48f5 | 147 | const struct file_operations *old_fops; |
1da177e4 LT |
148 | int err = 0; |
149 | ||
3a63e444 | 150 | if (minor >= ARRAY_SIZE(snd_minors)) |
332682b1 CL |
151 | return -ENODEV; |
152 | mptr = snd_minors[minor]; | |
153 | if (mptr == NULL) { | |
1da177e4 | 154 | #ifdef CONFIG_KMOD |
332682b1 CL |
155 | int dev = SNDRV_MINOR_DEVICE(minor); |
156 | if (dev == SNDRV_MINOR_CONTROL) { | |
157 | /* /dev/aloadC? */ | |
158 | int card = SNDRV_MINOR_CARD(minor); | |
1da177e4 | 159 | if (snd_cards[card] == NULL) |
332682b1 CL |
160 | snd_request_card(card); |
161 | } else if (dev == SNDRV_MINOR_GLOBAL) { | |
162 | /* /dev/aloadSEQ */ | |
1da177e4 | 163 | snd_request_other(minor); |
332682b1 CL |
164 | } |
165 | #ifndef CONFIG_SND_DYNAMIC_MINORS | |
166 | /* /dev/snd/{controlC?,seq} */ | |
167 | mptr = snd_minors[minor]; | |
168 | if (mptr == NULL) | |
169 | #endif | |
1da177e4 | 170 | #endif |
332682b1 | 171 | return -ENODEV; |
1da177e4 | 172 | } |
1da177e4 LT |
173 | old_fops = file->f_op; |
174 | file->f_op = fops_get(mptr->f_ops); | |
175 | if (file->f_op->open) | |
176 | err = file->f_op->open(inode, file); | |
177 | if (err) { | |
178 | fops_put(file->f_op); | |
179 | file->f_op = fops_get(old_fops); | |
180 | } | |
181 | fops_put(old_fops); | |
182 | return err; | |
183 | } | |
184 | ||
185 | static struct file_operations snd_fops = | |
186 | { | |
187 | .owner = THIS_MODULE, | |
188 | .open = snd_open | |
189 | }; | |
190 | ||
332682b1 CL |
191 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
192 | static int snd_find_free_minor(void) | |
193 | { | |
194 | int minor; | |
195 | ||
196 | for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) { | |
197 | /* skip minors still used statically for autoloading devices */ | |
198 | if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL || | |
199 | minor == SNDRV_MINOR_SEQUENCER) | |
200 | continue; | |
201 | if (!snd_minors[minor]) | |
202 | return minor; | |
203 | } | |
204 | return -EBUSY; | |
205 | } | |
206 | #else | |
512bbd6a | 207 | static int snd_kernel_minor(int type, struct snd_card *card, int dev) |
1da177e4 LT |
208 | { |
209 | int minor; | |
210 | ||
211 | switch (type) { | |
212 | case SNDRV_DEVICE_TYPE_SEQUENCER: | |
213 | case SNDRV_DEVICE_TYPE_TIMER: | |
214 | minor = type; | |
215 | break; | |
216 | case SNDRV_DEVICE_TYPE_CONTROL: | |
217 | snd_assert(card != NULL, return -EINVAL); | |
218 | minor = SNDRV_MINOR(card->number, type); | |
219 | break; | |
220 | case SNDRV_DEVICE_TYPE_HWDEP: | |
221 | case SNDRV_DEVICE_TYPE_RAWMIDI: | |
222 | case SNDRV_DEVICE_TYPE_PCM_PLAYBACK: | |
223 | case SNDRV_DEVICE_TYPE_PCM_CAPTURE: | |
224 | snd_assert(card != NULL, return -EINVAL); | |
225 | minor = SNDRV_MINOR(card->number, type + dev); | |
226 | break; | |
227 | default: | |
228 | return -EINVAL; | |
229 | } | |
230 | snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL); | |
231 | return minor; | |
232 | } | |
332682b1 | 233 | #endif |
1da177e4 LT |
234 | |
235 | /** | |
236 | * snd_register_device - Register the ALSA device file for the card | |
237 | * @type: the device type, SNDRV_DEVICE_TYPE_XXX | |
238 | * @card: the card instance | |
239 | * @dev: the device index | |
2af677fc | 240 | * @f_ops: the file operations |
f87135f5 | 241 | * @private_data: user pointer for f_ops->open() |
1da177e4 LT |
242 | * @name: the device file name |
243 | * | |
244 | * Registers an ALSA device file for the given card. | |
245 | * The operators have to be set in reg parameter. | |
246 | * | |
247 | * Retrurns zero if successful, or a negative error code on failure. | |
248 | */ | |
2af677fc | 249 | int snd_register_device(int type, struct snd_card *card, int dev, |
99ac48f5 | 250 | const struct file_operations *f_ops, void *private_data, |
f87135f5 | 251 | const char *name) |
1da177e4 | 252 | { |
332682b1 | 253 | int minor; |
512bbd6a | 254 | struct snd_minor *preg; |
1da177e4 LT |
255 | struct device *device = NULL; |
256 | ||
1da177e4 | 257 | snd_assert(name, return -EINVAL); |
6983b724 | 258 | preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL); |
1da177e4 LT |
259 | if (preg == NULL) |
260 | return -ENOMEM; | |
2af677fc | 261 | preg->type = type; |
6983b724 | 262 | preg->card = card ? card->number : -1; |
1da177e4 | 263 | preg->device = dev; |
2af677fc | 264 | preg->f_ops = f_ops; |
f87135f5 | 265 | preg->private_data = private_data; |
1da177e4 | 266 | strcpy(preg->name, name); |
1a60d4c5 | 267 | mutex_lock(&sound_mutex); |
332682b1 CL |
268 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
269 | minor = snd_find_free_minor(); | |
270 | #else | |
271 | minor = snd_kernel_minor(type, card, dev); | |
272 | if (minor >= 0 && snd_minors[minor]) | |
273 | minor = -EBUSY; | |
274 | #endif | |
275 | if (minor < 0) { | |
1a60d4c5 | 276 | mutex_unlock(&sound_mutex); |
1da177e4 | 277 | kfree(preg); |
332682b1 | 278 | return minor; |
1da177e4 | 279 | } |
6983b724 CL |
280 | snd_minors[minor] = preg; |
281 | if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit) | |
1da177e4 | 282 | devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name); |
3e23c658 | 283 | if (card) |
1da177e4 | 284 | device = card->dev; |
3e23c658 | 285 | class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name); |
1da177e4 | 286 | |
1a60d4c5 | 287 | mutex_unlock(&sound_mutex); |
1da177e4 LT |
288 | return 0; |
289 | } | |
290 | ||
c0d3fb39 TI |
291 | EXPORT_SYMBOL(snd_register_device); |
292 | ||
1da177e4 LT |
293 | /** |
294 | * snd_unregister_device - unregister the device on the given card | |
295 | * @type: the device type, SNDRV_DEVICE_TYPE_XXX | |
296 | * @card: the card instance | |
297 | * @dev: the device index | |
298 | * | |
299 | * Unregisters the device file already registered via | |
300 | * snd_register_device(). | |
301 | * | |
302 | * Returns zero if sucecessful, or a negative error code on failure | |
303 | */ | |
512bbd6a | 304 | int snd_unregister_device(int type, struct snd_card *card, int dev) |
1da177e4 | 305 | { |
f87135f5 | 306 | int cardnum, minor; |
512bbd6a | 307 | struct snd_minor *mptr; |
1da177e4 | 308 | |
f87135f5 | 309 | cardnum = card ? card->number : -1; |
1a60d4c5 | 310 | mutex_lock(&sound_mutex); |
f87135f5 CL |
311 | for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) |
312 | if ((mptr = snd_minors[minor]) != NULL && | |
313 | mptr->type == type && | |
314 | mptr->card == cardnum && | |
315 | mptr->device == dev) | |
316 | break; | |
317 | if (minor == ARRAY_SIZE(snd_minors)) { | |
1a60d4c5 | 318 | mutex_unlock(&sound_mutex); |
1da177e4 LT |
319 | return -EINVAL; |
320 | } | |
321 | ||
6983b724 CL |
322 | if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL || |
323 | mptr->card >= cards_limit) /* created in sound.c */ | |
1da177e4 | 324 | devfs_remove("snd/%s", mptr->name); |
619e666b | 325 | class_device_destroy(sound_class, MKDEV(major, minor)); |
1da177e4 | 326 | |
6983b724 | 327 | snd_minors[minor] = NULL; |
1a60d4c5 | 328 | mutex_unlock(&sound_mutex); |
1da177e4 LT |
329 | kfree(mptr); |
330 | return 0; | |
331 | } | |
332 | ||
c0d3fb39 TI |
333 | EXPORT_SYMBOL(snd_unregister_device); |
334 | ||
e28563cc | 335 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
336 | /* |
337 | * INFO PART | |
338 | */ | |
339 | ||
512bbd6a | 340 | static struct snd_info_entry *snd_minor_info_entry = NULL; |
1da177e4 | 341 | |
2af677fc CL |
342 | static const char *snd_device_type_name(int type) |
343 | { | |
344 | switch (type) { | |
345 | case SNDRV_DEVICE_TYPE_CONTROL: | |
346 | return "control"; | |
347 | case SNDRV_DEVICE_TYPE_HWDEP: | |
348 | return "hardware dependent"; | |
349 | case SNDRV_DEVICE_TYPE_RAWMIDI: | |
350 | return "raw midi"; | |
351 | case SNDRV_DEVICE_TYPE_PCM_PLAYBACK: | |
352 | return "digital audio playback"; | |
353 | case SNDRV_DEVICE_TYPE_PCM_CAPTURE: | |
354 | return "digital audio capture"; | |
355 | case SNDRV_DEVICE_TYPE_SEQUENCER: | |
356 | return "sequencer"; | |
357 | case SNDRV_DEVICE_TYPE_TIMER: | |
358 | return "timer"; | |
359 | default: | |
360 | return "?"; | |
361 | } | |
362 | } | |
363 | ||
512bbd6a | 364 | static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
1da177e4 | 365 | { |
6983b724 | 366 | int minor; |
512bbd6a | 367 | struct snd_minor *mptr; |
1da177e4 | 368 | |
1a60d4c5 | 369 | mutex_lock(&sound_mutex); |
6983b724 CL |
370 | for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) { |
371 | if (!(mptr = snd_minors[minor])) | |
372 | continue; | |
373 | if (mptr->card >= 0) { | |
374 | if (mptr->device >= 0) | |
d001544d | 375 | snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n", |
6983b724 CL |
376 | minor, mptr->card, mptr->device, |
377 | snd_device_type_name(mptr->type)); | |
378 | else | |
d001544d | 379 | snd_iprintf(buffer, "%3i: [%2i] : %s\n", |
6983b724 CL |
380 | minor, mptr->card, |
381 | snd_device_type_name(mptr->type)); | |
382 | } else | |
d001544d | 383 | snd_iprintf(buffer, "%3i: : %s\n", minor, |
6983b724 | 384 | snd_device_type_name(mptr->type)); |
1da177e4 | 385 | } |
1a60d4c5 | 386 | mutex_unlock(&sound_mutex); |
1da177e4 LT |
387 | } |
388 | ||
389 | int __init snd_minor_info_init(void) | |
390 | { | |
512bbd6a | 391 | struct snd_info_entry *entry; |
1da177e4 LT |
392 | |
393 | entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL); | |
394 | if (entry) { | |
1da177e4 LT |
395 | entry->c.text.read = snd_minor_info_read; |
396 | if (snd_info_register(entry) < 0) { | |
397 | snd_info_free_entry(entry); | |
398 | entry = NULL; | |
399 | } | |
400 | } | |
401 | snd_minor_info_entry = entry; | |
402 | return 0; | |
403 | } | |
404 | ||
405 | int __exit snd_minor_info_done(void) | |
406 | { | |
407 | if (snd_minor_info_entry) | |
408 | snd_info_unregister(snd_minor_info_entry); | |
409 | return 0; | |
410 | } | |
e28563cc | 411 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
412 | |
413 | /* | |
414 | * INIT PART | |
415 | */ | |
416 | ||
417 | static int __init alsa_sound_init(void) | |
418 | { | |
419 | short controlnum; | |
1da177e4 LT |
420 | |
421 | snd_major = major; | |
422 | snd_ecards_limit = cards_limit; | |
1da177e4 LT |
423 | devfs_mk_dir("snd"); |
424 | if (register_chrdev(major, "alsa", &snd_fops)) { | |
425 | snd_printk(KERN_ERR "unable to register native major device number %d\n", major); | |
426 | devfs_remove("snd"); | |
427 | return -EIO; | |
428 | } | |
1da177e4 | 429 | if (snd_info_init() < 0) { |
1da177e4 LT |
430 | unregister_chrdev(major, "alsa"); |
431 | devfs_remove("snd"); | |
432 | return -ENOMEM; | |
433 | } | |
434 | snd_info_minor_register(); | |
435 | for (controlnum = 0; controlnum < cards_limit; controlnum++) | |
436 | devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum); | |
437 | #ifndef MODULE | |
438 | printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"); | |
439 | #endif | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static void __exit alsa_sound_exit(void) | |
444 | { | |
445 | short controlnum; | |
446 | ||
447 | for (controlnum = 0; controlnum < cards_limit; controlnum++) | |
448 | devfs_remove("snd/controlC%d", controlnum); | |
449 | ||
450 | snd_info_minor_unregister(); | |
451 | snd_info_done(); | |
1da177e4 LT |
452 | if (unregister_chrdev(major, "alsa") != 0) |
453 | snd_printk(KERN_ERR "unable to unregister major device number %d\n", major); | |
454 | devfs_remove("snd"); | |
455 | } | |
456 | ||
457 | module_init(alsa_sound_init) | |
458 | module_exit(alsa_sound_exit) |