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 <sound/core.h>
15 #ifdef CONFIG_SOUND_OSS_CORE
16 static int __init
init_oss_soundcore(void);
17 static void cleanup_oss_soundcore(void);
19 static inline int init_oss_soundcore(void) { return 0; }
20 static inline void cleanup_oss_soundcore(void) { }
23 struct class *sound_class
;
24 EXPORT_SYMBOL(sound_class
);
26 MODULE_DESCRIPTION("Core sound module");
27 MODULE_AUTHOR("Alan Cox");
28 MODULE_LICENSE("GPL");
30 static int __init
init_soundcore(void)
34 rc
= init_oss_soundcore();
38 sound_class
= class_create(THIS_MODULE
, "sound");
39 if (IS_ERR(sound_class
)) {
40 cleanup_oss_soundcore();
41 return PTR_ERR(sound_class
);
47 static void __exit
cleanup_soundcore(void)
49 cleanup_oss_soundcore();
50 class_destroy(sound_class
);
53 module_init(init_soundcore
);
54 module_exit(cleanup_soundcore
);
57 #ifdef CONFIG_SOUND_OSS_CORE
59 * OSS sound core handling. Breaks out sound functions to submodules
61 * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
66 * This program is free software; you can redistribute it and/or
67 * modify it under the terms of the GNU General Public License
68 * as published by the Free Software Foundation; either version
69 * 2 of the License, or (at your option) any later version.
71 * --------------------
73 * Top level handler for the sound subsystem. Various devices can
74 * plug into this. The fact they don't all go via OSS doesn't mean
75 * they don't have to implement the OSS API. There is a lot of logic
76 * to keeping much of the OSS weight out of the code in a compatibility
77 * module, but it's up to the driver to rember to load it...
79 * The code provides a set of functions for registration of devices
80 * by type. This is done rather than providing a single call so that
81 * we can hide any future changes in the internals (eg when we go to
82 * 32bit dev_t) from the modules and their interface.
84 * Secondly we need to allocate the dsp, dsp16 and audio devices as
85 * one. Thus we misuse the chains a bit to simplify this.
87 * Thirdly to make it more fun and for 2.3.x and above we do all
88 * of this using fine grained locking.
90 * FIXME: we have to resolve modules and fine grained load/unload
91 * locking at some point in 2.3.x.
94 #include <linux/init.h>
95 #include <linux/slab.h>
96 #include <linux/smp_lock.h>
97 #include <linux/types.h>
98 #include <linux/kernel.h>
99 #include <linux/sound.h>
100 #include <linux/major.h>
101 #include <linux/kmod.h>
103 #define SOUND_STEP 16
108 const struct file_operations
*unit_fops
;
109 struct sound_unit
*next
;
113 #ifdef CONFIG_SOUND_MSNDCLAS
114 extern int msnd_classic_init(void);
116 #ifdef CONFIG_SOUND_MSNDPIN
117 extern int msnd_pinnacle_init(void);
121 * Low level list operator. Scan the ordered list, find a hole and
122 * join into it. Called with the lock asserted
125 static int __sound_insert_unit(struct sound_unit
* s
, struct sound_unit
**list
, const struct file_operations
*fops
, int index
, int low
, int top
)
129 if (index
< 0) { /* first free */
131 while (*list
&& (*list
)->unit_minor
<n
)
132 list
=&((*list
)->next
);
137 if(*list
==NULL
|| (*list
)->unit_minor
>n
)
139 list
=&((*list
)->next
);
148 if ((*list
)->unit_minor
==n
)
150 if ((*list
)->unit_minor
>n
)
152 list
=&((*list
)->next
);
175 * Remove a node from the chain. Called with the lock asserted
178 static struct sound_unit
*__sound_remove_unit(struct sound_unit
**list
, int unit
)
182 struct sound_unit
*p
=*list
;
183 if(p
->unit_minor
==unit
)
190 printk(KERN_ERR
"Sound device %d went missing!\n", unit
);
195 * This lock guards the sound loader list.
198 static DEFINE_SPINLOCK(sound_loader_lock
);
201 * Allocate the controlling structure and add it to the sound driver
202 * list. Acquires locks as needed
205 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
)
207 struct sound_unit
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
213 spin_lock(&sound_loader_lock
);
214 r
= __sound_insert_unit(s
, list
, fops
, index
, low
, top
);
215 spin_unlock(&sound_loader_lock
);
219 else if (r
< SOUND_STEP
)
220 sprintf(s
->name
, "sound/%s", name
);
222 sprintf(s
->name
, "sound/%s%d", name
, r
/ SOUND_STEP
);
224 device_create(sound_class
, dev
, MKDEV(SOUND_MAJOR
, s
->unit_minor
),
234 * Remove a unit. Acquires locks as needed. The drivers MUST have
235 * completed the removal before their file operations become
239 static void sound_remove_unit(struct sound_unit
**list
, int unit
)
241 struct sound_unit
*p
;
243 spin_lock(&sound_loader_lock
);
244 p
= __sound_remove_unit(list
, unit
);
245 spin_unlock(&sound_loader_lock
);
247 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, p
->unit_minor
));
261 * 6 -- sndstat (obsolete)
263 * 8 -- alternate sequencer (see above)
264 * 9 *16 raw synthesizer access
273 static struct sound_unit
*chains
[SOUND_STEP
];
276 * register_sound_special_device - register a special sound node
277 * @fops: File operations for the driver
278 * @unit: Unit number to allocate
279 * @dev: device pointer
281 * Allocate a special sound device by minor number from the sound
282 * subsystem. The allocated number is returned on succes. On failure
283 * a negative error code is returned.
286 int register_sound_special_device(const struct file_operations
*fops
, int unit
,
289 const int chain
= unit
% SOUND_STEP
;
290 int max_unit
= 128 + chain
;
300 if (unit
>= SOUND_STEP
)
315 if (unit
>= SOUND_STEP
)
337 sprintf(_name
, "unknown%d", chain
);
338 if (unit
>= SOUND_STEP
)
344 return sound_insert_unit(&chains
[chain
], fops
, -1, unit
, max_unit
,
345 name
, S_IRUSR
| S_IWUSR
, dev
);
348 EXPORT_SYMBOL(register_sound_special_device
);
350 int register_sound_special(const struct file_operations
*fops
, int unit
)
352 return register_sound_special_device(fops
, unit
, NULL
);
355 EXPORT_SYMBOL(register_sound_special
);
358 * register_sound_mixer - register a mixer device
359 * @fops: File operations for the driver
360 * @dev: Unit number to allocate
362 * Allocate a mixer device. Unit is the number of the mixer requested.
363 * Pass -1 to request the next free mixer unit. On success the allocated
364 * number is returned, on failure a negative error code is returned.
367 int register_sound_mixer(const struct file_operations
*fops
, int dev
)
369 return sound_insert_unit(&chains
[0], fops
, dev
, 0, 128,
370 "mixer", S_IRUSR
| S_IWUSR
, NULL
);
373 EXPORT_SYMBOL(register_sound_mixer
);
376 * register_sound_midi - register a midi device
377 * @fops: File operations for the driver
378 * @dev: Unit number to allocate
380 * Allocate a midi device. Unit is the number of the midi device requested.
381 * Pass -1 to request the next free midi unit. On success the allocated
382 * number is returned, on failure a negative error code is returned.
385 int register_sound_midi(const struct file_operations
*fops
, int dev
)
387 return sound_insert_unit(&chains
[2], fops
, dev
, 2, 130,
388 "midi", S_IRUSR
| S_IWUSR
, NULL
);
391 EXPORT_SYMBOL(register_sound_midi
);
394 * DSP's are registered as a triple. Register only one and cheat
395 * in open - see below.
399 * register_sound_dsp - register a DSP device
400 * @fops: File operations for the driver
401 * @dev: Unit number to allocate
403 * Allocate a DSP device. Unit is the number of the DSP requested.
404 * Pass -1 to request the next free DSP unit. On success the allocated
405 * number is returned, on failure a negative error code is returned.
407 * This function allocates both the audio and dsp device entries together
408 * and will always allocate them as a matching pair - eg dsp3/audio3
411 int register_sound_dsp(const struct file_operations
*fops
, int dev
)
413 return sound_insert_unit(&chains
[3], fops
, dev
, 3, 131,
414 "dsp", S_IWUSR
| S_IRUSR
, NULL
);
417 EXPORT_SYMBOL(register_sound_dsp
);
420 * unregister_sound_special - unregister a special sound device
421 * @unit: unit number to allocate
423 * Release a sound device that was allocated with
424 * register_sound_special(). The unit passed is the return value from
425 * the register function.
429 void unregister_sound_special(int unit
)
431 sound_remove_unit(&chains
[unit
% SOUND_STEP
], unit
);
434 EXPORT_SYMBOL(unregister_sound_special
);
437 * unregister_sound_mixer - unregister a mixer
438 * @unit: unit number to allocate
440 * Release a sound device that was allocated with register_sound_mixer().
441 * The unit passed is the return value from the register function.
444 void unregister_sound_mixer(int unit
)
446 sound_remove_unit(&chains
[0], unit
);
449 EXPORT_SYMBOL(unregister_sound_mixer
);
452 * unregister_sound_midi - unregister a midi device
453 * @unit: unit number to allocate
455 * Release a sound device that was allocated with register_sound_midi().
456 * The unit passed is the return value from the register function.
459 void unregister_sound_midi(int unit
)
461 sound_remove_unit(&chains
[2], unit
);
464 EXPORT_SYMBOL(unregister_sound_midi
);
467 * unregister_sound_dsp - unregister a DSP device
468 * @unit: unit number to allocate
470 * Release a sound device that was allocated with register_sound_dsp().
471 * The unit passed is the return value from the register function.
473 * Both of the allocated units are released together automatically.
476 void unregister_sound_dsp(int unit
)
478 sound_remove_unit(&chains
[3], unit
);
482 EXPORT_SYMBOL(unregister_sound_dsp
);
485 * Now our file operations
488 static int soundcore_open(struct inode
*, struct file
*);
490 static const struct file_operations soundcore_fops
=
492 /* We must have an owner or the module locking fails */
493 .owner
= THIS_MODULE
,
494 .open
= soundcore_open
,
497 static struct sound_unit
*__look_for_unit(int chain
, int unit
)
499 struct sound_unit
*s
;
502 while(s
&& s
->unit_minor
<= unit
)
504 if(s
->unit_minor
==unit
)
511 static int soundcore_open(struct inode
*inode
, struct file
*file
)
514 int unit
= iminor(inode
);
515 struct sound_unit
*s
;
516 const struct file_operations
*new_fops
= NULL
;
521 if(chain
==4 || chain
==5) /* dsp/audio/dsp16 */
528 spin_lock(&sound_loader_lock
);
529 s
= __look_for_unit(chain
, unit
);
531 new_fops
= fops_get(s
->unit_fops
);
533 spin_unlock(&sound_loader_lock
);
535 * Please, don't change this order or code.
536 * For ALSA slot means soundcard and OSS emulation code
537 * comes as add-on modules which aren't depend on
538 * ALSA toplevel modules for soundcards, thus we need
539 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
541 request_module("sound-slot-%i", unit
>>4);
542 request_module("sound-service-%i-%i", unit
>>4, chain
);
543 spin_lock(&sound_loader_lock
);
544 s
= __look_for_unit(chain
, unit
);
546 new_fops
= fops_get(s
->unit_fops
);
550 * We rely upon the fact that we can't be unloaded while the
551 * subdriver is there, so if ->open() is successful we can
552 * safely drop the reference counter and if it is not we can
553 * revert to old ->f_op. Ugly, indeed, but that's the cost of
554 * switching ->f_op in the first place.
557 const struct file_operations
*old_fops
= file
->f_op
;
558 file
->f_op
= new_fops
;
559 spin_unlock(&sound_loader_lock
);
561 err
= file
->f_op
->open(inode
,file
);
563 fops_put(file
->f_op
);
564 file
->f_op
= fops_get(old_fops
);
570 spin_unlock(&sound_loader_lock
);
575 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR
);
577 static void cleanup_oss_soundcore(void)
579 /* We have nothing to really do here - we know the lists must be
581 unregister_chrdev(SOUND_MAJOR
, "sound");
584 static int __init
init_oss_soundcore(void)
586 if (register_chrdev(SOUND_MAJOR
, "sound", &soundcore_fops
)==-1) {
587 printk(KERN_ERR
"soundcore: sound device already in use.\n");
594 #endif /* CONFIG_SOUND_OSS_CORE */