2 * Sound core handling. Breaks out sound functions to submodules
4 * Author: Alan Cox <alan.cox@linux.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * --------------------
16 * Top level handler for the sound subsystem. Various devices can
17 * plug into this. The fact they don't all go via OSS doesn't mean
18 * they don't have to implement the OSS API. There is a lot of logic
19 * to keeping much of the OSS weight out of the code in a compatibility
20 * module, but it's up to the driver to rember to load it...
22 * The code provides a set of functions for registration of devices
23 * by type. This is done rather than providing a single call so that
24 * we can hide any future changes in the internals (eg when we go to
25 * 32bit dev_t) from the modules and their interface.
27 * Secondly we need to allocate the dsp, dsp16 and audio devices as
28 * one. Thus we misuse the chains a bit to simplify this.
30 * Thirdly to make it more fun and for 2.3.x and above we do all
31 * of this using fine grained locking.
33 * FIXME: we have to resolve modules and fine grained load/unload
34 * locking at some point in 2.3.x.
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
44 #include <linux/sound.h>
45 #include <linux/major.h>
46 #include <linux/kmod.h>
47 #include <linux/devfs_fs_kernel.h>
55 struct file_operations
*unit_fops
;
56 struct sound_unit
*next
;
60 #ifdef CONFIG_SOUND_MSNDCLAS
61 extern int msnd_classic_init(void);
63 #ifdef CONFIG_SOUND_MSNDPIN
64 extern int msnd_pinnacle_init(void);
68 * Low level list operator. Scan the ordered list, find a hole and
69 * join into it. Called with the lock asserted
72 static int __sound_insert_unit(struct sound_unit
* s
, struct sound_unit
**list
, struct file_operations
*fops
, int index
, int low
, int top
)
76 if (index
< 0) { /* first free */
78 while (*list
&& (*list
)->unit_minor
<n
)
79 list
=&((*list
)->next
);
84 if(*list
==NULL
|| (*list
)->unit_minor
>n
)
86 list
=&((*list
)->next
);
95 if ((*list
)->unit_minor
==n
)
97 if ((*list
)->unit_minor
>n
)
99 list
=&((*list
)->next
);
122 * Remove a node from the chain. Called with the lock asserted
125 static struct sound_unit
*__sound_remove_unit(struct sound_unit
**list
, int unit
)
129 struct sound_unit
*p
=*list
;
130 if(p
->unit_minor
==unit
)
137 printk(KERN_ERR
"Sound device %d went missing!\n", unit
);
142 * This lock guards the sound loader list.
145 static spinlock_t sound_loader_lock
= SPIN_LOCK_UNLOCKED
;
148 * Allocate the controlling structure and add it to the sound driver
149 * list. Acquires locks as needed
152 static int sound_insert_unit(struct sound_unit
**list
, struct file_operations
*fops
, int index
, int low
, int top
, const char *name
, umode_t mode
)
154 struct sound_unit
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
160 spin_lock(&sound_loader_lock
);
161 r
= __sound_insert_unit(s
, list
, fops
, index
, low
, top
);
162 spin_unlock(&sound_loader_lock
);
166 else if (r
< SOUND_STEP
)
167 sprintf(s
->name
, "sound/%s", name
);
169 sprintf(s
->name
, "sound/%s%d", name
, r
/ SOUND_STEP
);
171 devfs_mk_cdev(MKDEV(SOUND_MAJOR
, s
->unit_minor
),
172 S_IFCHR
| mode
, s
->name
);
181 * Remove a unit. Acquires locks as needed. The drivers MUST have
182 * completed the removal before their file operations become
186 static void sound_remove_unit(struct sound_unit
**list
, int unit
)
188 struct sound_unit
*p
;
190 spin_lock(&sound_loader_lock
);
191 p
= __sound_remove_unit(list
, unit
);
192 spin_unlock(&sound_loader_lock
);
194 devfs_remove(p
->name
);
208 * 6 -- sndstat (obsolete)
210 * 8 -- alternate sequencer (see above)
211 * 9 *16 raw synthesizer access
220 static struct sound_unit
*chains
[SOUND_STEP
];
223 * register_sound_special - register a special sound node
224 * @fops: File operations for the driver
225 * @unit: Unit number to allocate
227 * Allocate a special sound device by minor number from the sound
228 * subsystem. The allocated number is returned on succes. On failure
229 * a negative error code is returned.
232 int register_sound_special(struct file_operations
*fops
, int unit
)
234 const int chain
= unit
% SOUND_STEP
;
235 int max_unit
= 128 + chain
;
245 if (unit
>= SOUND_STEP
)
260 if (unit
>= SOUND_STEP
)
282 sprintf(_name
, "unknown%d", chain
);
283 if (unit
>= SOUND_STEP
)
289 return sound_insert_unit(&chains
[chain
], fops
, -1, unit
, max_unit
,
290 name
, S_IRUSR
| S_IWUSR
);
293 EXPORT_SYMBOL(register_sound_special
);
296 * register_sound_mixer - register a mixer device
297 * @fops: File operations for the driver
298 * @dev: Unit number to allocate
300 * Allocate a mixer device. Unit is the number of the mixer requested.
301 * Pass -1 to request the next free mixer unit. On success the allocated
302 * number is returned, on failure a negative error code is returned.
305 int register_sound_mixer(struct file_operations
*fops
, int dev
)
307 return sound_insert_unit(&chains
[0], fops
, dev
, 0, 128,
308 "mixer", S_IRUSR
| S_IWUSR
);
311 EXPORT_SYMBOL(register_sound_mixer
);
314 * register_sound_midi - register a midi device
315 * @fops: File operations for the driver
316 * @dev: Unit number to allocate
318 * Allocate a midi device. Unit is the number of the midi device requested.
319 * Pass -1 to request the next free midi unit. On success the allocated
320 * number is returned, on failure a negative error code is returned.
323 int register_sound_midi(struct file_operations
*fops
, int dev
)
325 return sound_insert_unit(&chains
[2], fops
, dev
, 2, 130,
326 "midi", S_IRUSR
| S_IWUSR
);
329 EXPORT_SYMBOL(register_sound_midi
);
332 * DSP's are registered as a triple. Register only one and cheat
333 * in open - see below.
337 * register_sound_dsp - register a DSP device
338 * @fops: File operations for the driver
339 * @dev: Unit number to allocate
341 * Allocate a DSP device. Unit is the number of the DSP requested.
342 * Pass -1 to request the next free DSP unit. On success the allocated
343 * number is returned, on failure a negative error code is returned.
345 * This function allocates both the audio and dsp device entries together
346 * and will always allocate them as a matching pair - eg dsp3/audio3
349 int register_sound_dsp(struct file_operations
*fops
, int dev
)
351 return sound_insert_unit(&chains
[3], fops
, dev
, 3, 131,
352 "dsp", S_IWUSR
| S_IRUSR
);
355 EXPORT_SYMBOL(register_sound_dsp
);
358 * register_sound_synth - register a synth device
359 * @fops: File operations for the driver
360 * @dev: Unit number to allocate
362 * Allocate a synth device. Unit is the number of the synth device requested.
363 * Pass -1 to request the next free synth unit. On success the allocated
364 * number is returned, on failure a negative error code is returned.
368 int register_sound_synth(struct file_operations
*fops
, int dev
)
370 return sound_insert_unit(&chains
[9], fops
, dev
, 9, 137,
371 "synth", S_IRUSR
| S_IWUSR
);
374 EXPORT_SYMBOL(register_sound_synth
);
377 * unregister_sound_special - unregister a special sound device
378 * @unit: unit number to allocate
380 * Release a sound device that was allocated with
381 * register_sound_special(). The unit passed is the return value from
382 * the register function.
386 void unregister_sound_special(int unit
)
388 sound_remove_unit(&chains
[unit
% SOUND_STEP
], unit
);
391 EXPORT_SYMBOL(unregister_sound_special
);
394 * unregister_sound_mixer - unregister a mixer
395 * @unit: unit number to allocate
397 * Release a sound device that was allocated with register_sound_mixer().
398 * The unit passed is the return value from the register function.
401 void unregister_sound_mixer(int unit
)
403 sound_remove_unit(&chains
[0], unit
);
406 EXPORT_SYMBOL(unregister_sound_mixer
);
409 * unregister_sound_midi - unregister a midi device
410 * @unit: unit number to allocate
412 * Release a sound device that was allocated with register_sound_midi().
413 * The unit passed is the return value from the register function.
416 void unregister_sound_midi(int unit
)
418 return sound_remove_unit(&chains
[2], unit
);
421 EXPORT_SYMBOL(unregister_sound_midi
);
424 * unregister_sound_dsp - unregister a DSP device
425 * @unit: unit number to allocate
427 * Release a sound device that was allocated with register_sound_dsp().
428 * The unit passed is the return value from the register function.
430 * Both of the allocated units are released together automatically.
433 void unregister_sound_dsp(int unit
)
435 return sound_remove_unit(&chains
[3], unit
);
439 EXPORT_SYMBOL(unregister_sound_dsp
);
442 * unregister_sound_synth - unregister a synth device
443 * @unit: unit number to allocate
445 * Release a sound device that was allocated with register_sound_synth().
446 * The unit passed is the return value from the register function.
449 void unregister_sound_synth(int unit
)
451 return sound_remove_unit(&chains
[9], unit
);
454 EXPORT_SYMBOL(unregister_sound_synth
);
457 * Now our file operations
460 static int soundcore_open(struct inode
*, struct file
*);
462 static struct file_operations soundcore_fops
=
464 /* We must have an owner or the module locking fails */
465 .owner
= THIS_MODULE
,
466 .open
= soundcore_open
,
469 static struct sound_unit
*__look_for_unit(int chain
, int unit
)
471 struct sound_unit
*s
;
474 while(s
&& s
->unit_minor
<= unit
)
476 if(s
->unit_minor
==unit
)
483 int soundcore_open(struct inode
*inode
, struct file
*file
)
486 int unit
= minor(inode
->i_rdev
);
487 struct sound_unit
*s
;
488 struct file_operations
*new_fops
= NULL
;
491 if(chain
==4 || chain
==5) /* dsp/audio/dsp16 */
498 spin_lock(&sound_loader_lock
);
499 s
= __look_for_unit(chain
, unit
);
501 new_fops
= fops_get(s
->unit_fops
);
503 spin_unlock(&sound_loader_lock
);
505 * Please, don't change this order or code.
506 * For ALSA slot means soundcard and OSS emulation code
507 * comes as add-on modules which aren't depend on
508 * ALSA toplevel modules for soundcards, thus we need
509 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
511 request_module("sound-slot-%i", unit
>>4);
512 request_module("sound-service-%i-%i", unit
>>4, chain
);
513 spin_lock(&sound_loader_lock
);
514 s
= __look_for_unit(chain
, unit
);
516 new_fops
= fops_get(s
->unit_fops
);
520 * We rely upon the fact that we can't be unloaded while the
521 * subdriver is there, so if ->open() is successful we can
522 * safely drop the reference counter and if it is not we can
523 * revert to old ->f_op. Ugly, indeed, but that's the cost of
524 * switching ->f_op in the first place.
527 struct file_operations
*old_fops
= file
->f_op
;
528 file
->f_op
= new_fops
;
529 spin_unlock(&sound_loader_lock
);
531 err
= file
->f_op
->open(inode
,file
);
533 fops_put(file
->f_op
);
534 file
->f_op
= fops_get(old_fops
);
539 spin_unlock(&sound_loader_lock
);
543 extern int mod_firmware_load(const char *, char **);
544 EXPORT_SYMBOL(mod_firmware_load
);
547 MODULE_DESCRIPTION("Core sound module");
548 MODULE_AUTHOR("Alan Cox");
549 MODULE_LICENSE("GPL");
551 static void __exit
cleanup_soundcore(void)
553 /* We have nothing to really do here - we know the lists must be
555 unregister_chrdev(SOUND_MAJOR
, "sound");
556 devfs_remove("sound");
559 static int __init
init_soundcore(void)
561 if (register_chrdev(SOUND_MAJOR
, "sound", &soundcore_fops
)==-1) {
562 printk(KERN_ERR
"soundcore: sound device already in use.\n");
565 devfs_mk_dir ("sound");
570 module_init(init_soundcore
);
571 module_exit(cleanup_soundcore
);