2 * Sound core. This file is composed of two parts. sound_class
3 * which is common to both OSS and ALSA and OSS sound core which
4 * is used OSS or emulation of it.
8 * First, the common part.
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/kdev_t.h>
14 #include <linux/major.h>
15 #include <sound/core.h>
17 #ifdef CONFIG_SOUND_OSS_CORE
18 static int __init
init_oss_soundcore(void);
19 static void cleanup_oss_soundcore(void);
21 static inline int init_oss_soundcore(void) { return 0; }
22 static inline void cleanup_oss_soundcore(void) { }
25 struct class *sound_class
;
26 EXPORT_SYMBOL(sound_class
);
28 MODULE_DESCRIPTION("Core sound module");
29 MODULE_AUTHOR("Alan Cox");
30 MODULE_LICENSE("GPL");
32 static char *sound_devnode(struct device
*dev
, mode_t
*mode
)
34 if (MAJOR(dev
->devt
) == SOUND_MAJOR
)
36 return kasprintf(GFP_KERNEL
, "snd/%s", dev_name(dev
));
39 static int __init
init_soundcore(void)
43 rc
= init_oss_soundcore();
47 sound_class
= class_create(THIS_MODULE
, "sound");
48 if (IS_ERR(sound_class
)) {
49 cleanup_oss_soundcore();
50 return PTR_ERR(sound_class
);
53 sound_class
->devnode
= sound_devnode
;
58 static void __exit
cleanup_soundcore(void)
60 cleanup_oss_soundcore();
61 class_destroy(sound_class
);
64 subsys_initcall(init_soundcore
);
65 module_exit(cleanup_soundcore
);
68 #ifdef CONFIG_SOUND_OSS_CORE
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/smp_lock.h>
73 #include <linux/types.h>
74 #include <linux/kernel.h>
75 #include <linux/sound.h>
76 #include <linux/kmod.h>
83 const struct file_operations
*unit_fops
;
84 struct sound_unit
*next
;
88 #ifdef CONFIG_SOUND_MSNDCLAS
89 extern int msnd_classic_init(void);
91 #ifdef CONFIG_SOUND_MSNDPIN
92 extern int msnd_pinnacle_init(void);
96 * By default, OSS sound_core claims full legacy minor range (0-255)
97 * of SOUND_MAJOR to trap open attempts to any sound minor and
98 * requests modules using custom sound-slot/service-* module aliases.
99 * The only benefit of doing this is allowing use of custom module
100 * aliases instead of the standard char-major-* ones. This behavior
101 * prevents alternative OSS implementation and is scheduled to be
104 * CONFIG_SOUND_OSS_CORE_PRECLAIM and soundcore.preclaim_oss kernel
105 * parameter are added to allow distros and developers to try and
106 * switch to alternative implementations without needing to rebuild
107 * the kernel in the meantime. If preclaim_oss is non-zero, the
108 * kernel will behave the same as before. All SOUND_MAJOR minors are
109 * preclaimed and the custom module aliases along with standard chrdev
110 * ones are emitted if a missing device is opened. If preclaim_oss is
111 * zero, sound_core only grabs what's actually in use and for missing
112 * devices only the standard chrdev aliases are requested.
114 * All these clutters are scheduled to be removed along with
115 * sound-slot/service-* module aliases. Please take a look at
116 * feature-removal-schedule.txt for details.
118 #ifdef CONFIG_SOUND_OSS_CORE_PRECLAIM
119 static int preclaim_oss
= 1;
121 static int preclaim_oss
= 0;
124 module_param(preclaim_oss
, int, 0444);
126 static int soundcore_open(struct inode
*, struct file
*);
128 static const struct file_operations soundcore_fops
=
130 /* We must have an owner or the module locking fails */
131 .owner
= THIS_MODULE
,
132 .open
= soundcore_open
,
136 * Low level list operator. Scan the ordered list, find a hole and
137 * join into it. Called with the lock asserted
140 static int __sound_insert_unit(struct sound_unit
* s
, struct sound_unit
**list
, const struct file_operations
*fops
, int index
, int low
, int top
)
144 if (index
< 0) { /* first free */
146 while (*list
&& (*list
)->unit_minor
<n
)
147 list
=&((*list
)->next
);
152 if(*list
==NULL
|| (*list
)->unit_minor
>n
)
154 list
=&((*list
)->next
);
163 if ((*list
)->unit_minor
==n
)
165 if ((*list
)->unit_minor
>n
)
167 list
=&((*list
)->next
);
190 * Remove a node from the chain. Called with the lock asserted
193 static struct sound_unit
*__sound_remove_unit(struct sound_unit
**list
, int unit
)
197 struct sound_unit
*p
=*list
;
198 if(p
->unit_minor
==unit
)
205 printk(KERN_ERR
"Sound device %d went missing!\n", unit
);
210 * This lock guards the sound loader list.
213 static DEFINE_SPINLOCK(sound_loader_lock
);
216 * Allocate the controlling structure and add it to the sound driver
217 * list. Acquires locks as needed
220 static int sound_insert_unit(struct sound_unit
**list
, const struct file_operations
*fops
, int index
, int low
, int top
, const char *name
, umode_t mode
, struct device
*dev
)
222 struct sound_unit
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
228 spin_lock(&sound_loader_lock
);
230 r
= __sound_insert_unit(s
, list
, fops
, index
, low
, top
);
231 spin_unlock(&sound_loader_lock
);
235 else if (r
< SOUND_STEP
)
236 sprintf(s
->name
, "sound/%s", name
);
238 sprintf(s
->name
, "sound/%s%d", name
, r
/ SOUND_STEP
);
242 * Something else might have grabbed the minor. If
243 * first free slot is requested, rescan with @low set
244 * to the next unit; otherwise, -EBUSY.
246 r
= __register_chrdev(SOUND_MAJOR
, s
->unit_minor
, 1, s
->name
,
249 spin_lock(&sound_loader_lock
);
250 __sound_remove_unit(list
, s
->unit_minor
);
252 low
= s
->unit_minor
+ SOUND_STEP
;
255 spin_unlock(&sound_loader_lock
);
260 device_create(sound_class
, dev
, MKDEV(SOUND_MAJOR
, s
->unit_minor
),
262 return s
->unit_minor
;
270 * Remove a unit. Acquires locks as needed. The drivers MUST have
271 * completed the removal before their file operations become
275 static void sound_remove_unit(struct sound_unit
**list
, int unit
)
277 struct sound_unit
*p
;
279 spin_lock(&sound_loader_lock
);
280 p
= __sound_remove_unit(list
, unit
);
281 spin_unlock(&sound_loader_lock
);
284 __unregister_chrdev(SOUND_MAJOR
, p
->unit_minor
, 1,
286 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, p
->unit_minor
));
300 * 6 -- sndstat (obsolete)
302 * 8 -- alternate sequencer (see above)
303 * 9 *16 raw synthesizer access
312 static struct sound_unit
*chains
[SOUND_STEP
];
315 * register_sound_special_device - register a special sound node
316 * @fops: File operations for the driver
317 * @unit: Unit number to allocate
318 * @dev: device pointer
320 * Allocate a special sound device by minor number from the sound
321 * subsystem. The allocated number is returned on success. On failure
322 * a negative error code is returned.
325 int register_sound_special_device(const struct file_operations
*fops
, int unit
,
328 const int chain
= unit
% SOUND_STEP
;
329 int max_unit
= 128 + chain
;
339 if (unit
>= SOUND_STEP
)
354 if (unit
>= SOUND_STEP
)
376 sprintf(_name
, "unknown%d", chain
);
377 if (unit
>= SOUND_STEP
)
383 return sound_insert_unit(&chains
[chain
], fops
, -1, unit
, max_unit
,
384 name
, S_IRUSR
| S_IWUSR
, dev
);
387 EXPORT_SYMBOL(register_sound_special_device
);
389 int register_sound_special(const struct file_operations
*fops
, int unit
)
391 return register_sound_special_device(fops
, unit
, NULL
);
394 EXPORT_SYMBOL(register_sound_special
);
397 * register_sound_mixer - register a mixer device
398 * @fops: File operations for the driver
399 * @dev: Unit number to allocate
401 * Allocate a mixer device. Unit is the number of the mixer requested.
402 * Pass -1 to request the next free mixer unit. On success the allocated
403 * number is returned, on failure a negative error code is returned.
406 int register_sound_mixer(const struct file_operations
*fops
, int dev
)
408 return sound_insert_unit(&chains
[0], fops
, dev
, 0, 128,
409 "mixer", S_IRUSR
| S_IWUSR
, NULL
);
412 EXPORT_SYMBOL(register_sound_mixer
);
415 * register_sound_midi - register a midi device
416 * @fops: File operations for the driver
417 * @dev: Unit number to allocate
419 * Allocate a midi device. Unit is the number of the midi device requested.
420 * Pass -1 to request the next free midi unit. On success the allocated
421 * number is returned, on failure a negative error code is returned.
424 int register_sound_midi(const struct file_operations
*fops
, int dev
)
426 return sound_insert_unit(&chains
[2], fops
, dev
, 2, 130,
427 "midi", S_IRUSR
| S_IWUSR
, NULL
);
430 EXPORT_SYMBOL(register_sound_midi
);
433 * DSP's are registered as a triple. Register only one and cheat
434 * in open - see below.
438 * register_sound_dsp - register a DSP device
439 * @fops: File operations for the driver
440 * @dev: Unit number to allocate
442 * Allocate a DSP device. Unit is the number of the DSP requested.
443 * Pass -1 to request the next free DSP unit. On success the allocated
444 * number is returned, on failure a negative error code is returned.
446 * This function allocates both the audio and dsp device entries together
447 * and will always allocate them as a matching pair - eg dsp3/audio3
450 int register_sound_dsp(const struct file_operations
*fops
, int dev
)
452 return sound_insert_unit(&chains
[3], fops
, dev
, 3, 131,
453 "dsp", S_IWUSR
| S_IRUSR
, NULL
);
456 EXPORT_SYMBOL(register_sound_dsp
);
459 * unregister_sound_special - unregister a special sound device
460 * @unit: unit number to allocate
462 * Release a sound device that was allocated with
463 * register_sound_special(). The unit passed is the return value from
464 * the register function.
468 void unregister_sound_special(int unit
)
470 sound_remove_unit(&chains
[unit
% SOUND_STEP
], unit
);
473 EXPORT_SYMBOL(unregister_sound_special
);
476 * unregister_sound_mixer - unregister a mixer
477 * @unit: unit number to allocate
479 * Release a sound device that was allocated with register_sound_mixer().
480 * The unit passed is the return value from the register function.
483 void unregister_sound_mixer(int unit
)
485 sound_remove_unit(&chains
[0], unit
);
488 EXPORT_SYMBOL(unregister_sound_mixer
);
491 * unregister_sound_midi - unregister a midi device
492 * @unit: unit number to allocate
494 * Release a sound device that was allocated with register_sound_midi().
495 * The unit passed is the return value from the register function.
498 void unregister_sound_midi(int unit
)
500 sound_remove_unit(&chains
[2], unit
);
503 EXPORT_SYMBOL(unregister_sound_midi
);
506 * unregister_sound_dsp - unregister a DSP device
507 * @unit: unit number to allocate
509 * Release a sound device that was allocated with register_sound_dsp().
510 * The unit passed is the return value from the register function.
512 * Both of the allocated units are released together automatically.
515 void unregister_sound_dsp(int unit
)
517 sound_remove_unit(&chains
[3], unit
);
521 EXPORT_SYMBOL(unregister_sound_dsp
);
523 static struct sound_unit
*__look_for_unit(int chain
, int unit
)
525 struct sound_unit
*s
;
528 while(s
&& s
->unit_minor
<= unit
)
530 if(s
->unit_minor
==unit
)
537 static int soundcore_open(struct inode
*inode
, struct file
*file
)
540 int unit
= iminor(inode
);
541 struct sound_unit
*s
;
542 const struct file_operations
*new_fops
= NULL
;
545 if(chain
==4 || chain
==5) /* dsp/audio/dsp16 */
552 spin_lock(&sound_loader_lock
);
553 s
= __look_for_unit(chain
, unit
);
555 new_fops
= fops_get(s
->unit_fops
);
556 if (preclaim_oss
&& !new_fops
) {
557 spin_unlock(&sound_loader_lock
);
560 * Please, don't change this order or code.
561 * For ALSA slot means soundcard and OSS emulation code
562 * comes as add-on modules which aren't depend on
563 * ALSA toplevel modules for soundcards, thus we need
564 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
566 request_module("sound-slot-%i", unit
>>4);
567 request_module("sound-service-%i-%i", unit
>>4, chain
);
570 * sound-slot/service-* module aliases are scheduled
571 * for removal in favor of the standard char-major-*
572 * module aliases. For the time being, generate both
573 * the legacy and standard module aliases to ease
576 if (request_module("char-major-%d-%d", SOUND_MAJOR
, unit
) > 0)
577 request_module("char-major-%d", SOUND_MAJOR
);
579 spin_lock(&sound_loader_lock
);
580 s
= __look_for_unit(chain
, unit
);
582 new_fops
= fops_get(s
->unit_fops
);
586 * We rely upon the fact that we can't be unloaded while the
587 * subdriver is there, so if ->open() is successful we can
588 * safely drop the reference counter and if it is not we can
589 * revert to old ->f_op. Ugly, indeed, but that's the cost of
590 * switching ->f_op in the first place.
593 const struct file_operations
*old_fops
= file
->f_op
;
594 file
->f_op
= new_fops
;
595 spin_unlock(&sound_loader_lock
);
597 if (file
->f_op
->open
)
598 err
= file
->f_op
->open(inode
,file
);
601 fops_put(file
->f_op
);
602 file
->f_op
= fops_get(old_fops
);
608 spin_unlock(&sound_loader_lock
);
612 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR
);
614 static void cleanup_oss_soundcore(void)
616 /* We have nothing to really do here - we know the lists must be
618 unregister_chrdev(SOUND_MAJOR
, "sound");
621 static int __init
init_oss_soundcore(void)
624 register_chrdev(SOUND_MAJOR
, "sound", &soundcore_fops
) == -1) {
625 printk(KERN_ERR
"soundcore: sound device already in use.\n");
632 #endif /* CONFIG_SOUND_OSS_CORE */