[PATCH] cond_resched() fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / sound_core.c
blob7535ec821dcf8dfb53a55046853ad86b0cbff54d
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/device.h>
49 #define SOUND_STEP 16
52 struct sound_unit
54 int unit_minor;
55 const 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
67 struct class *sound_class;
68 EXPORT_SYMBOL(sound_class);
71 * Low level list operator. Scan the ordered list, find a hole and
72 * join into it. Called with the lock asserted
75 static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
77 int n=low;
79 if (index < 0) { /* first free */
81 while (*list && (*list)->unit_minor<n)
82 list=&((*list)->next);
84 while(n<top)
86 /* Found a hole ? */
87 if(*list==NULL || (*list)->unit_minor>n)
88 break;
89 list=&((*list)->next);
90 n+=SOUND_STEP;
93 if(n>=top)
94 return -ENOENT;
95 } else {
96 n = low+(index*16);
97 while (*list) {
98 if ((*list)->unit_minor==n)
99 return -EBUSY;
100 if ((*list)->unit_minor>n)
101 break;
102 list=&((*list)->next);
107 * Fill it in
110 s->unit_minor=n;
111 s->unit_fops=fops;
114 * Link it
117 s->next=*list;
118 *list=s;
121 return n;
125 * Remove a node from the chain. Called with the lock asserted
128 static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
130 while(*list)
132 struct sound_unit *p=*list;
133 if(p->unit_minor==unit)
135 *list=p->next;
136 return p;
138 list=&(p->next);
140 printk(KERN_ERR "Sound device %d went missing!\n", unit);
141 return NULL;
145 * This lock guards the sound loader list.
148 static DEFINE_SPINLOCK(sound_loader_lock);
151 * Allocate the controlling structure and add it to the sound driver
152 * list. Acquires locks as needed
155 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)
157 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
158 int r;
160 if (!s)
161 return -ENOMEM;
163 spin_lock(&sound_loader_lock);
164 r = __sound_insert_unit(s, list, fops, index, low, top);
165 spin_unlock(&sound_loader_lock);
167 if (r < 0)
168 goto fail;
169 else if (r < SOUND_STEP)
170 sprintf(s->name, "sound/%s", name);
171 else
172 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
174 class_device_create(sound_class, NULL, MKDEV(SOUND_MAJOR, s->unit_minor),
175 dev, s->name+6);
176 return r;
178 fail:
179 kfree(s);
180 return r;
184 * Remove a unit. Acquires locks as needed. The drivers MUST have
185 * completed the removal before their file operations become
186 * invalid.
189 static void sound_remove_unit(struct sound_unit **list, int unit)
191 struct sound_unit *p;
193 spin_lock(&sound_loader_lock);
194 p = __sound_remove_unit(list, unit);
195 spin_unlock(&sound_loader_lock);
196 if (p) {
197 class_device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
198 kfree(p);
203 * Allocations
205 * 0 *16 Mixers
206 * 1 *8 Sequencers
207 * 2 *16 Midi
208 * 3 *16 DSP
209 * 4 *16 SunDSP
210 * 5 *16 DSP16
211 * 6 -- sndstat (obsolete)
212 * 7 *16 unused
213 * 8 -- alternate sequencer (see above)
214 * 9 *16 raw synthesizer access
215 * 10 *16 unused
216 * 11 *16 unused
217 * 12 *16 unused
218 * 13 *16 unused
219 * 14 *16 unused
220 * 15 *16 unused
223 static struct sound_unit *chains[SOUND_STEP];
226 * register_sound_special_device - register a special sound node
227 * @fops: File operations for the driver
228 * @unit: Unit number to allocate
229 * @dev: device pointer
231 * Allocate a special sound device by minor number from the sound
232 * subsystem. The allocated number is returned on succes. On failure
233 * a negative error code is returned.
236 int register_sound_special_device(const struct file_operations *fops, int unit,
237 struct device *dev)
239 const int chain = unit % SOUND_STEP;
240 int max_unit = 128 + chain;
241 const char *name;
242 char _name[16];
244 switch (chain) {
245 case 0:
246 name = "mixer";
247 break;
248 case 1:
249 name = "sequencer";
250 if (unit >= SOUND_STEP)
251 goto __unknown;
252 max_unit = unit + 1;
253 break;
254 case 2:
255 name = "midi";
256 break;
257 case 3:
258 name = "dsp";
259 break;
260 case 4:
261 name = "audio";
262 break;
263 case 8:
264 name = "sequencer2";
265 if (unit >= SOUND_STEP)
266 goto __unknown;
267 max_unit = unit + 1;
268 break;
269 case 9:
270 name = "dmmidi";
271 break;
272 case 10:
273 name = "dmfm";
274 break;
275 case 12:
276 name = "adsp";
277 break;
278 case 13:
279 name = "amidi";
280 break;
281 case 14:
282 name = "admmidi";
283 break;
284 default:
286 __unknown:
287 sprintf(_name, "unknown%d", chain);
288 if (unit >= SOUND_STEP)
289 strcat(_name, "-");
290 name = _name;
292 break;
294 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
295 name, S_IRUSR | S_IWUSR, dev);
298 EXPORT_SYMBOL(register_sound_special_device);
300 int register_sound_special(const struct file_operations *fops, int unit)
302 return register_sound_special_device(fops, unit, NULL);
305 EXPORT_SYMBOL(register_sound_special);
308 * register_sound_mixer - register a mixer device
309 * @fops: File operations for the driver
310 * @dev: Unit number to allocate
312 * Allocate a mixer device. Unit is the number of the mixer requested.
313 * Pass -1 to request the next free mixer unit. On success the allocated
314 * number is returned, on failure a negative error code is returned.
317 int register_sound_mixer(const struct file_operations *fops, int dev)
319 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
320 "mixer", S_IRUSR | S_IWUSR, NULL);
323 EXPORT_SYMBOL(register_sound_mixer);
326 * register_sound_midi - register a midi device
327 * @fops: File operations for the driver
328 * @dev: Unit number to allocate
330 * Allocate a midi device. Unit is the number of the midi device requested.
331 * Pass -1 to request the next free midi unit. On success the allocated
332 * number is returned, on failure a negative error code is returned.
335 int register_sound_midi(const struct file_operations *fops, int dev)
337 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
338 "midi", S_IRUSR | S_IWUSR, NULL);
341 EXPORT_SYMBOL(register_sound_midi);
344 * DSP's are registered as a triple. Register only one and cheat
345 * in open - see below.
349 * register_sound_dsp - register a DSP device
350 * @fops: File operations for the driver
351 * @dev: Unit number to allocate
353 * Allocate a DSP device. Unit is the number of the DSP requested.
354 * Pass -1 to request the next free DSP unit. On success the allocated
355 * number is returned, on failure a negative error code is returned.
357 * This function allocates both the audio and dsp device entries together
358 * and will always allocate them as a matching pair - eg dsp3/audio3
361 int register_sound_dsp(const struct file_operations *fops, int dev)
363 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
364 "dsp", S_IWUSR | S_IRUSR, NULL);
367 EXPORT_SYMBOL(register_sound_dsp);
370 * register_sound_synth - register a synth device
371 * @fops: File operations for the driver
372 * @dev: Unit number to allocate
374 * Allocate a synth device. Unit is the number of the synth device requested.
375 * Pass -1 to request the next free synth unit. On success the allocated
376 * number is returned, on failure a negative error code is returned.
380 int register_sound_synth(const struct file_operations *fops, int dev)
382 return sound_insert_unit(&chains[9], fops, dev, 9, 137,
383 "synth", S_IRUSR | S_IWUSR, NULL);
386 EXPORT_SYMBOL(register_sound_synth);
389 * unregister_sound_special - unregister a special sound device
390 * @unit: unit number to allocate
392 * Release a sound device that was allocated with
393 * register_sound_special(). The unit passed is the return value from
394 * the register function.
398 void unregister_sound_special(int unit)
400 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
403 EXPORT_SYMBOL(unregister_sound_special);
406 * unregister_sound_mixer - unregister a mixer
407 * @unit: unit number to allocate
409 * Release a sound device that was allocated with register_sound_mixer().
410 * The unit passed is the return value from the register function.
413 void unregister_sound_mixer(int unit)
415 sound_remove_unit(&chains[0], unit);
418 EXPORT_SYMBOL(unregister_sound_mixer);
421 * unregister_sound_midi - unregister a midi device
422 * @unit: unit number to allocate
424 * Release a sound device that was allocated with register_sound_midi().
425 * The unit passed is the return value from the register function.
428 void unregister_sound_midi(int unit)
430 return sound_remove_unit(&chains[2], unit);
433 EXPORT_SYMBOL(unregister_sound_midi);
436 * unregister_sound_dsp - unregister a DSP device
437 * @unit: unit number to allocate
439 * Release a sound device that was allocated with register_sound_dsp().
440 * The unit passed is the return value from the register function.
442 * Both of the allocated units are released together automatically.
445 void unregister_sound_dsp(int unit)
447 return sound_remove_unit(&chains[3], unit);
451 EXPORT_SYMBOL(unregister_sound_dsp);
454 * unregister_sound_synth - unregister a synth device
455 * @unit: unit number to allocate
457 * Release a sound device that was allocated with register_sound_synth().
458 * The unit passed is the return value from the register function.
461 void unregister_sound_synth(int unit)
463 return sound_remove_unit(&chains[9], unit);
466 EXPORT_SYMBOL(unregister_sound_synth);
469 * Now our file operations
472 static int soundcore_open(struct inode *, struct file *);
474 static struct file_operations soundcore_fops=
476 /* We must have an owner or the module locking fails */
477 .owner = THIS_MODULE,
478 .open = soundcore_open,
481 static struct sound_unit *__look_for_unit(int chain, int unit)
483 struct sound_unit *s;
485 s=chains[chain];
486 while(s && s->unit_minor <= unit)
488 if(s->unit_minor==unit)
489 return s;
490 s=s->next;
492 return NULL;
495 int soundcore_open(struct inode *inode, struct file *file)
497 int chain;
498 int unit = iminor(inode);
499 struct sound_unit *s;
500 const struct file_operations *new_fops = NULL;
502 chain=unit&0x0F;
503 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
505 unit&=0xF0;
506 unit|=3;
507 chain=3;
510 spin_lock(&sound_loader_lock);
511 s = __look_for_unit(chain, unit);
512 if (s)
513 new_fops = fops_get(s->unit_fops);
514 if (!new_fops) {
515 spin_unlock(&sound_loader_lock);
517 * Please, don't change this order or code.
518 * For ALSA slot means soundcard and OSS emulation code
519 * comes as add-on modules which aren't depend on
520 * ALSA toplevel modules for soundcards, thus we need
521 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
523 request_module("sound-slot-%i", unit>>4);
524 request_module("sound-service-%i-%i", unit>>4, chain);
525 spin_lock(&sound_loader_lock);
526 s = __look_for_unit(chain, unit);
527 if (s)
528 new_fops = fops_get(s->unit_fops);
530 if (new_fops) {
532 * We rely upon the fact that we can't be unloaded while the
533 * subdriver is there, so if ->open() is successful we can
534 * safely drop the reference counter and if it is not we can
535 * revert to old ->f_op. Ugly, indeed, but that's the cost of
536 * switching ->f_op in the first place.
538 int err = 0;
539 const struct file_operations *old_fops = file->f_op;
540 file->f_op = new_fops;
541 spin_unlock(&sound_loader_lock);
542 if(file->f_op->open)
543 err = file->f_op->open(inode,file);
544 if (err) {
545 fops_put(file->f_op);
546 file->f_op = fops_get(old_fops);
548 fops_put(old_fops);
549 return err;
551 spin_unlock(&sound_loader_lock);
552 return -ENODEV;
555 extern int mod_firmware_load(const char *, char **);
556 EXPORT_SYMBOL(mod_firmware_load);
559 MODULE_DESCRIPTION("Core sound module");
560 MODULE_AUTHOR("Alan Cox");
561 MODULE_LICENSE("GPL");
562 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
564 static void __exit cleanup_soundcore(void)
566 /* We have nothing to really do here - we know the lists must be
567 empty */
568 unregister_chrdev(SOUND_MAJOR, "sound");
569 class_destroy(sound_class);
572 static int __init init_soundcore(void)
574 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
575 printk(KERN_ERR "soundcore: sound device already in use.\n");
576 return -EBUSY;
578 sound_class = class_create(THIS_MODULE, "sound");
579 if (IS_ERR(sound_class))
580 return PTR_ERR(sound_class);
582 return 0;
585 module_init(init_soundcore);
586 module_exit(cleanup_soundcore);