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/module.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40 #include <linux/smp_lock.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/device.h>
55 const 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);
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)
79 if (index < 0) { /* first free */
81 while (*list && (*list)->unit_minor<n)
82 list=&((*list)->next);
87 if(*list==NULL || (*list)->unit_minor>n)
89 list=&((*list)->next);
98 if ((*list)->unit_minor==n)
100 if ((*list)->unit_minor>n)
102 list=&((*list)->next);
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)
132 struct sound_unit *p=*list;
133 if(p->unit_minor==unit)
140 printk(KERN_ERR "Sound device %d went missing!\n", unit);
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);
163 spin_lock(&sound_loader_lock);
164 r = __sound_insert_unit(s, list, fops, index, low, top);
165 spin_unlock(&sound_loader_lock);
169 else if (r < SOUND_STEP)
170 sprintf(s->name, "sound/%s", name);
172 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
174 device_create_drvdata(sound_class, dev,
175 MKDEV(SOUND_MAJOR, s->unit_minor),
185 * Remove a unit. Acquires locks as needed. The drivers MUST have
186 * completed the removal before their file operations become
190 static void sound_remove_unit(struct sound_unit **list, int unit)
192 struct sound_unit *p;
194 spin_lock(&sound_loader_lock);
195 p = __sound_remove_unit(list, unit);
196 spin_unlock(&sound_loader_lock);
198 device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
212 * 6 -- sndstat (obsolete)
214 * 8 -- alternate sequencer (see above)
215 * 9 *16 raw synthesizer access
224 static struct sound_unit *chains[SOUND_STEP];
227 * register_sound_special_device - register a special sound node
228 * @fops: File operations for the driver
229 * @unit: Unit number to allocate
230 * @dev: device pointer
232 * Allocate a special sound device by minor number from the sound
233 * subsystem. The allocated number is returned on succes. On failure
234 * a negative error code is returned.
237 int register_sound_special_device(const struct file_operations *fops, int unit,
240 const int chain = unit % SOUND_STEP;
241 int max_unit = 128 + chain;
251 if (unit >= SOUND_STEP)
266 if (unit >= SOUND_STEP)
288 sprintf(_name, "unknown%d", chain);
289 if (unit >= SOUND_STEP)
295 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
296 name, S_IRUSR | S_IWUSR, dev);
299 EXPORT_SYMBOL(register_sound_special_device);
301 int register_sound_special(const struct file_operations *fops, int unit)
303 return register_sound_special_device(fops, unit, NULL);
306 EXPORT_SYMBOL(register_sound_special);
309 * register_sound_mixer - register a mixer device
310 * @fops: File operations for the driver
311 * @dev: Unit number to allocate
313 * Allocate a mixer device. Unit is the number of the mixer requested.
314 * Pass -1 to request the next free mixer unit. On success the allocated
315 * number is returned, on failure a negative error code is returned.
318 int register_sound_mixer(const struct file_operations *fops, int dev)
320 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
321 "mixer", S_IRUSR | S_IWUSR, NULL);
324 EXPORT_SYMBOL(register_sound_mixer);
327 * register_sound_midi - register a midi device
328 * @fops: File operations for the driver
329 * @dev: Unit number to allocate
331 * Allocate a midi device. Unit is the number of the midi device requested.
332 * Pass -1 to request the next free midi unit. On success the allocated
333 * number is returned, on failure a negative error code is returned.
336 int register_sound_midi(const struct file_operations *fops, int dev)
338 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
339 "midi", S_IRUSR | S_IWUSR, NULL);
342 EXPORT_SYMBOL(register_sound_midi);
345 * DSP's are registered as a triple. Register only one and cheat
346 * in open - see below.
350 * register_sound_dsp - register a DSP device
351 * @fops: File operations for the driver
352 * @dev: Unit number to allocate
354 * Allocate a DSP device. Unit is the number of the DSP requested.
355 * Pass -1 to request the next free DSP unit. On success the allocated
356 * number is returned, on failure a negative error code is returned.
358 * This function allocates both the audio and dsp device entries together
359 * and will always allocate them as a matching pair - eg dsp3/audio3
362 int register_sound_dsp(const struct file_operations *fops, int dev)
364 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
365 "dsp", S_IWUSR | S_IRUSR, NULL);
368 EXPORT_SYMBOL(register_sound_dsp);
371 * unregister_sound_special - unregister a special sound device
372 * @unit: unit number to allocate
374 * Release a sound device that was allocated with
375 * register_sound_special(). The unit passed is the return value from
376 * the register function.
380 void unregister_sound_special(int unit)
382 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
385 EXPORT_SYMBOL(unregister_sound_special);
388 * unregister_sound_mixer - unregister a mixer
389 * @unit: unit number to allocate
391 * Release a sound device that was allocated with register_sound_mixer().
392 * The unit passed is the return value from the register function.
395 void unregister_sound_mixer(int unit)
397 sound_remove_unit(&chains[0], unit);
400 EXPORT_SYMBOL(unregister_sound_mixer);
403 * unregister_sound_midi - unregister a midi device
404 * @unit: unit number to allocate
406 * Release a sound device that was allocated with register_sound_midi().
407 * The unit passed is the return value from the register function.
410 void unregister_sound_midi(int unit)
412 return sound_remove_unit(&chains[2], unit);
415 EXPORT_SYMBOL(unregister_sound_midi);
418 * unregister_sound_dsp - unregister a DSP device
419 * @unit: unit number to allocate
421 * Release a sound device that was allocated with register_sound_dsp().
422 * The unit passed is the return value from the register function.
424 * Both of the allocated units are released together automatically.
427 void unregister_sound_dsp(int unit)
429 return sound_remove_unit(&chains[3], unit);
433 EXPORT_SYMBOL(unregister_sound_dsp);
436 * Now our file operations
439 static int soundcore_open(struct inode *, struct file *);
441 static const struct file_operations soundcore_fops=
443 /* We must have an owner or the module locking fails */
444 .owner = THIS_MODULE,
445 .open = soundcore_open,
448 static struct sound_unit *__look_for_unit(int chain, int unit)
450 struct sound_unit *s;
453 while(s && s->unit_minor <= unit)
455 if(s->unit_minor==unit)
462 int soundcore_open(struct inode *inode, struct file *file)
465 int unit = iminor(inode);
466 struct sound_unit *s;
467 const struct file_operations *new_fops = NULL;
472 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
479 spin_lock(&sound_loader_lock);
480 s = __look_for_unit(chain, unit);
482 new_fops = fops_get(s->unit_fops);
484 spin_unlock(&sound_loader_lock);
486 * Please, don't change this order or code.
487 * For ALSA slot means soundcard and OSS emulation code
488 * comes as add-on modules which aren't depend on
489 * ALSA toplevel modules for soundcards, thus we need
490 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
492 request_module("sound-slot-%i", unit>>4);
493 request_module("sound-service-%i-%i", unit>>4, chain);
494 spin_lock(&sound_loader_lock);
495 s = __look_for_unit(chain, unit);
497 new_fops = fops_get(s->unit_fops);
501 * We rely upon the fact that we can't be unloaded while the
502 * subdriver is there, so if ->open() is successful we can
503 * safely drop the reference counter and if it is not we can
504 * revert to old ->f_op. Ugly, indeed, but that's the cost of
505 * switching ->f_op in the first place.
508 const struct file_operations *old_fops = file->f_op;
509 file->f_op = new_fops;
510 spin_unlock(&sound_loader_lock);
512 err = file->f_op->open(inode,file);
514 fops_put(file->f_op);
515 file->f_op = fops_get(old_fops);
521 spin_unlock(&sound_loader_lock);
526 MODULE_DESCRIPTION("Core sound module");
527 MODULE_AUTHOR("Alan Cox");
528 MODULE_LICENSE("GPL");
529 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
531 static void __exit cleanup_soundcore(void)
533 /* We have nothing to really do here - we know the lists must be
535 unregister_chrdev(SOUND_MAJOR, "sound");
536 class_destroy(sound_class);
539 static int __init init_soundcore(void)
541 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
542 printk(KERN_ERR "soundcore: sound device already in use.\n");
545 sound_class = class_create(THIS_MODULE, "sound");
546 if (IS_ERR(sound_class))
547 return PTR_ERR(sound_class);
552 module_init(init_soundcore);
553 module_exit(cleanup_soundcore);