Get rid of arch/mips64/kernel. 9116 lines of code gone.
[linux-2.6/linux-mips.git] / sound / sound_core.c
blob192f7ea9c26058c304dd6712c57f46921b9709e0
1 /*
2 * Sound core handling. Breaks out sound functions to submodules
3 *
4 * Author: Alan Cox <alan.cox@linux.org>
6 * Fixes:
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>
43 #include <linux/fs.h>
44 #include <linux/sound.h>
45 #include <linux/major.h>
46 #include <linux/kmod.h>
47 #include <linux/devfs_fs_kernel.h>
49 #define SOUND_STEP 16
52 struct sound_unit
54 int unit_minor;
55 struct file_operations *unit_fops;
56 struct sound_unit *next;
57 char name[32];
60 #ifdef CONFIG_SOUND_MSNDCLAS
61 extern int msnd_classic_init(void);
62 #endif
63 #ifdef CONFIG_SOUND_MSNDPIN
64 extern int msnd_pinnacle_init(void);
65 #endif
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)
74 int n=low;
76 if (index < 0) { /* first free */
78 while (*list && (*list)->unit_minor<n)
79 list=&((*list)->next);
81 while(n<top)
83 /* Found a hole ? */
84 if(*list==NULL || (*list)->unit_minor>n)
85 break;
86 list=&((*list)->next);
87 n+=SOUND_STEP;
90 if(n>=top)
91 return -ENOENT;
92 } else {
93 n = low+(index*16);
94 while (*list) {
95 if ((*list)->unit_minor==n)
96 return -EBUSY;
97 if ((*list)->unit_minor>n)
98 break;
99 list=&((*list)->next);
104 * Fill it in
107 s->unit_minor=n;
108 s->unit_fops=fops;
111 * Link it
114 s->next=*list;
115 *list=s;
118 return n;
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)
127 while(*list)
129 struct sound_unit *p=*list;
130 if(p->unit_minor==unit)
132 *list=p->next;
133 return p;
135 list=&(p->next);
137 printk(KERN_ERR "Sound device %d went missing!\n", unit);
138 return NULL;
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);
155 int r;
157 if (!s)
158 return -ENOMEM;
160 spin_lock(&sound_loader_lock);
161 r = __sound_insert_unit(s, list, fops, index, low, top);
162 spin_unlock(&sound_loader_lock);
164 if (r < 0)
165 goto fail;
166 else if (r < SOUND_STEP)
167 sprintf(s->name, "sound/%s", name);
168 else
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);
173 return r;
175 fail:
176 kfree(s);
177 return r;
181 * Remove a unit. Acquires locks as needed. The drivers MUST have
182 * completed the removal before their file operations become
183 * invalid.
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);
193 if (p) {
194 devfs_remove(p->name);
195 kfree(p);
200 * Allocations
202 * 0 *16 Mixers
203 * 1 *8 Sequencers
204 * 2 *16 Midi
205 * 3 *16 DSP
206 * 4 *16 SunDSP
207 * 5 *16 DSP16
208 * 6 -- sndstat (obsolete)
209 * 7 *16 unused
210 * 8 -- alternate sequencer (see above)
211 * 9 *16 raw synthesizer access
212 * 10 *16 unused
213 * 11 *16 unused
214 * 12 *16 unused
215 * 13 *16 unused
216 * 14 *16 unused
217 * 15 *16 unused
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;
236 const char *name;
237 char _name[16];
239 switch (chain) {
240 case 0:
241 name = "mixer";
242 break;
243 case 1:
244 name = "sequencer";
245 if (unit >= SOUND_STEP)
246 goto __unknown;
247 max_unit = unit + 1;
248 break;
249 case 2:
250 name = "midi";
251 break;
252 case 3:
253 name = "dsp";
254 break;
255 case 4:
256 name = "audio";
257 break;
258 case 8:
259 name = "sequencer2";
260 if (unit >= SOUND_STEP)
261 goto __unknown;
262 max_unit = unit + 1;
263 break;
264 case 9:
265 name = "dmmidi";
266 break;
267 case 10:
268 name = "dmfm";
269 break;
270 case 12:
271 name = "adsp";
272 break;
273 case 13:
274 name = "amidi";
275 break;
276 case 14:
277 name = "admmidi";
278 break;
279 default:
281 __unknown:
282 sprintf(_name, "unknown%d", chain);
283 if (unit >= SOUND_STEP)
284 strcat(_name, "-");
285 name = _name;
287 break;
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;
473 s=chains[chain];
474 while(s && s->unit_minor <= unit)
476 if(s->unit_minor==unit)
477 return s;
478 s=s->next;
480 return NULL;
483 int soundcore_open(struct inode *inode, struct file *file)
485 int chain;
486 int unit = minor(inode->i_rdev);
487 struct sound_unit *s;
488 struct file_operations *new_fops = NULL;
490 chain=unit&0x0F;
491 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
493 unit&=0xF0;
494 unit|=3;
495 chain=3;
498 spin_lock(&sound_loader_lock);
499 s = __look_for_unit(chain, unit);
500 if (s)
501 new_fops = fops_get(s->unit_fops);
502 if (!new_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);
515 if (s)
516 new_fops = fops_get(s->unit_fops);
518 if (new_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.
526 int err = 0;
527 struct file_operations *old_fops = file->f_op;
528 file->f_op = new_fops;
529 spin_unlock(&sound_loader_lock);
530 if(file->f_op->open)
531 err = file->f_op->open(inode,file);
532 if (err) {
533 fops_put(file->f_op);
534 file->f_op = fops_get(old_fops);
536 fops_put(old_fops);
537 return err;
539 spin_unlock(&sound_loader_lock);
540 return -ENODEV;
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
554 empty */
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");
563 return -EBUSY;
565 devfs_mk_dir ("sound");
567 return 0;
570 module_init(init_soundcore);
571 module_exit(cleanup_soundcore);