eeepc-laptop: Register as a pci-hotplug device
[linux-2.6/linux-acpi-2.6.git] / sound / sound_core.c
blob12522e6913d92d068676f4b3916571b03dae2d87
1 /*
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.
5 */
7 /*
8 * First, the common part.
9 */
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);
18 #else
19 static inline int init_oss_soundcore(void) { return 0; }
20 static inline void cleanup_oss_soundcore(void) { }
21 #endif
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 char *sound_nodename(struct device *dev)
32 return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
35 static int __init init_soundcore(void)
37 int rc;
39 rc = init_oss_soundcore();
40 if (rc)
41 return rc;
43 sound_class = class_create(THIS_MODULE, "sound");
44 if (IS_ERR(sound_class)) {
45 cleanup_oss_soundcore();
46 return PTR_ERR(sound_class);
49 sound_class->nodename = sound_nodename;
51 return 0;
54 static void __exit cleanup_soundcore(void)
56 cleanup_oss_soundcore();
57 class_destroy(sound_class);
60 module_init(init_soundcore);
61 module_exit(cleanup_soundcore);
64 #ifdef CONFIG_SOUND_OSS_CORE
66 * OSS sound core handling. Breaks out sound functions to submodules
68 * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
70 * Fixes:
73 * This program is free software; you can redistribute it and/or
74 * modify it under the terms of the GNU General Public License
75 * as published by the Free Software Foundation; either version
76 * 2 of the License, or (at your option) any later version.
78 * --------------------
80 * Top level handler for the sound subsystem. Various devices can
81 * plug into this. The fact they don't all go via OSS doesn't mean
82 * they don't have to implement the OSS API. There is a lot of logic
83 * to keeping much of the OSS weight out of the code in a compatibility
84 * module, but it's up to the driver to rember to load it...
86 * The code provides a set of functions for registration of devices
87 * by type. This is done rather than providing a single call so that
88 * we can hide any future changes in the internals (eg when we go to
89 * 32bit dev_t) from the modules and their interface.
91 * Secondly we need to allocate the dsp, dsp16 and audio devices as
92 * one. Thus we misuse the chains a bit to simplify this.
94 * Thirdly to make it more fun and for 2.3.x and above we do all
95 * of this using fine grained locking.
97 * FIXME: we have to resolve modules and fine grained load/unload
98 * locking at some point in 2.3.x.
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/smp_lock.h>
104 #include <linux/types.h>
105 #include <linux/kernel.h>
106 #include <linux/sound.h>
107 #include <linux/major.h>
108 #include <linux/kmod.h>
110 #define SOUND_STEP 16
112 struct sound_unit
114 int unit_minor;
115 const struct file_operations *unit_fops;
116 struct sound_unit *next;
117 char name[32];
120 #ifdef CONFIG_SOUND_MSNDCLAS
121 extern int msnd_classic_init(void);
122 #endif
123 #ifdef CONFIG_SOUND_MSNDPIN
124 extern int msnd_pinnacle_init(void);
125 #endif
128 * Low level list operator. Scan the ordered list, find a hole and
129 * join into it. Called with the lock asserted
132 static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
134 int n=low;
136 if (index < 0) { /* first free */
138 while (*list && (*list)->unit_minor<n)
139 list=&((*list)->next);
141 while(n<top)
143 /* Found a hole ? */
144 if(*list==NULL || (*list)->unit_minor>n)
145 break;
146 list=&((*list)->next);
147 n+=SOUND_STEP;
150 if(n>=top)
151 return -ENOENT;
152 } else {
153 n = low+(index*16);
154 while (*list) {
155 if ((*list)->unit_minor==n)
156 return -EBUSY;
157 if ((*list)->unit_minor>n)
158 break;
159 list=&((*list)->next);
164 * Fill it in
167 s->unit_minor=n;
168 s->unit_fops=fops;
171 * Link it
174 s->next=*list;
175 *list=s;
178 return n;
182 * Remove a node from the chain. Called with the lock asserted
185 static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
187 while(*list)
189 struct sound_unit *p=*list;
190 if(p->unit_minor==unit)
192 *list=p->next;
193 return p;
195 list=&(p->next);
197 printk(KERN_ERR "Sound device %d went missing!\n", unit);
198 return NULL;
202 * This lock guards the sound loader list.
205 static DEFINE_SPINLOCK(sound_loader_lock);
208 * Allocate the controlling structure and add it to the sound driver
209 * list. Acquires locks as needed
212 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)
214 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
215 int r;
217 if (!s)
218 return -ENOMEM;
220 spin_lock(&sound_loader_lock);
221 r = __sound_insert_unit(s, list, fops, index, low, top);
222 spin_unlock(&sound_loader_lock);
224 if (r < 0)
225 goto fail;
226 else if (r < SOUND_STEP)
227 sprintf(s->name, "sound/%s", name);
228 else
229 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
231 device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
232 NULL, s->name+6);
233 return r;
235 fail:
236 kfree(s);
237 return r;
241 * Remove a unit. Acquires locks as needed. The drivers MUST have
242 * completed the removal before their file operations become
243 * invalid.
246 static void sound_remove_unit(struct sound_unit **list, int unit)
248 struct sound_unit *p;
250 spin_lock(&sound_loader_lock);
251 p = __sound_remove_unit(list, unit);
252 spin_unlock(&sound_loader_lock);
253 if (p) {
254 device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
255 kfree(p);
260 * Allocations
262 * 0 *16 Mixers
263 * 1 *8 Sequencers
264 * 2 *16 Midi
265 * 3 *16 DSP
266 * 4 *16 SunDSP
267 * 5 *16 DSP16
268 * 6 -- sndstat (obsolete)
269 * 7 *16 unused
270 * 8 -- alternate sequencer (see above)
271 * 9 *16 raw synthesizer access
272 * 10 *16 unused
273 * 11 *16 unused
274 * 12 *16 unused
275 * 13 *16 unused
276 * 14 *16 unused
277 * 15 *16 unused
280 static struct sound_unit *chains[SOUND_STEP];
283 * register_sound_special_device - register a special sound node
284 * @fops: File operations for the driver
285 * @unit: Unit number to allocate
286 * @dev: device pointer
288 * Allocate a special sound device by minor number from the sound
289 * subsystem. The allocated number is returned on succes. On failure
290 * a negative error code is returned.
293 int register_sound_special_device(const struct file_operations *fops, int unit,
294 struct device *dev)
296 const int chain = unit % SOUND_STEP;
297 int max_unit = 128 + chain;
298 const char *name;
299 char _name[16];
301 switch (chain) {
302 case 0:
303 name = "mixer";
304 break;
305 case 1:
306 name = "sequencer";
307 if (unit >= SOUND_STEP)
308 goto __unknown;
309 max_unit = unit + 1;
310 break;
311 case 2:
312 name = "midi";
313 break;
314 case 3:
315 name = "dsp";
316 break;
317 case 4:
318 name = "audio";
319 break;
320 case 8:
321 name = "sequencer2";
322 if (unit >= SOUND_STEP)
323 goto __unknown;
324 max_unit = unit + 1;
325 break;
326 case 9:
327 name = "dmmidi";
328 break;
329 case 10:
330 name = "dmfm";
331 break;
332 case 12:
333 name = "adsp";
334 break;
335 case 13:
336 name = "amidi";
337 break;
338 case 14:
339 name = "admmidi";
340 break;
341 default:
343 __unknown:
344 sprintf(_name, "unknown%d", chain);
345 if (unit >= SOUND_STEP)
346 strcat(_name, "-");
347 name = _name;
349 break;
351 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
352 name, S_IRUSR | S_IWUSR, dev);
355 EXPORT_SYMBOL(register_sound_special_device);
357 int register_sound_special(const struct file_operations *fops, int unit)
359 return register_sound_special_device(fops, unit, NULL);
362 EXPORT_SYMBOL(register_sound_special);
365 * register_sound_mixer - register a mixer device
366 * @fops: File operations for the driver
367 * @dev: Unit number to allocate
369 * Allocate a mixer device. Unit is the number of the mixer requested.
370 * Pass -1 to request the next free mixer unit. On success the allocated
371 * number is returned, on failure a negative error code is returned.
374 int register_sound_mixer(const struct file_operations *fops, int dev)
376 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
377 "mixer", S_IRUSR | S_IWUSR, NULL);
380 EXPORT_SYMBOL(register_sound_mixer);
383 * register_sound_midi - register a midi device
384 * @fops: File operations for the driver
385 * @dev: Unit number to allocate
387 * Allocate a midi device. Unit is the number of the midi device requested.
388 * Pass -1 to request the next free midi unit. On success the allocated
389 * number is returned, on failure a negative error code is returned.
392 int register_sound_midi(const struct file_operations *fops, int dev)
394 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
395 "midi", S_IRUSR | S_IWUSR, NULL);
398 EXPORT_SYMBOL(register_sound_midi);
401 * DSP's are registered as a triple. Register only one and cheat
402 * in open - see below.
406 * register_sound_dsp - register a DSP device
407 * @fops: File operations for the driver
408 * @dev: Unit number to allocate
410 * Allocate a DSP device. Unit is the number of the DSP requested.
411 * Pass -1 to request the next free DSP unit. On success the allocated
412 * number is returned, on failure a negative error code is returned.
414 * This function allocates both the audio and dsp device entries together
415 * and will always allocate them as a matching pair - eg dsp3/audio3
418 int register_sound_dsp(const struct file_operations *fops, int dev)
420 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
421 "dsp", S_IWUSR | S_IRUSR, NULL);
424 EXPORT_SYMBOL(register_sound_dsp);
427 * unregister_sound_special - unregister a special sound device
428 * @unit: unit number to allocate
430 * Release a sound device that was allocated with
431 * register_sound_special(). The unit passed is the return value from
432 * the register function.
436 void unregister_sound_special(int unit)
438 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
441 EXPORT_SYMBOL(unregister_sound_special);
444 * unregister_sound_mixer - unregister a mixer
445 * @unit: unit number to allocate
447 * Release a sound device that was allocated with register_sound_mixer().
448 * The unit passed is the return value from the register function.
451 void unregister_sound_mixer(int unit)
453 sound_remove_unit(&chains[0], unit);
456 EXPORT_SYMBOL(unregister_sound_mixer);
459 * unregister_sound_midi - unregister a midi device
460 * @unit: unit number to allocate
462 * Release a sound device that was allocated with register_sound_midi().
463 * The unit passed is the return value from the register function.
466 void unregister_sound_midi(int unit)
468 sound_remove_unit(&chains[2], unit);
471 EXPORT_SYMBOL(unregister_sound_midi);
474 * unregister_sound_dsp - unregister a DSP device
475 * @unit: unit number to allocate
477 * Release a sound device that was allocated with register_sound_dsp().
478 * The unit passed is the return value from the register function.
480 * Both of the allocated units are released together automatically.
483 void unregister_sound_dsp(int unit)
485 sound_remove_unit(&chains[3], unit);
489 EXPORT_SYMBOL(unregister_sound_dsp);
492 * Now our file operations
495 static int soundcore_open(struct inode *, struct file *);
497 static const struct file_operations soundcore_fops=
499 /* We must have an owner or the module locking fails */
500 .owner = THIS_MODULE,
501 .open = soundcore_open,
504 static struct sound_unit *__look_for_unit(int chain, int unit)
506 struct sound_unit *s;
508 s=chains[chain];
509 while(s && s->unit_minor <= unit)
511 if(s->unit_minor==unit)
512 return s;
513 s=s->next;
515 return NULL;
518 static int soundcore_open(struct inode *inode, struct file *file)
520 int chain;
521 int unit = iminor(inode);
522 struct sound_unit *s;
523 const struct file_operations *new_fops = NULL;
525 lock_kernel ();
527 chain=unit&0x0F;
528 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
530 unit&=0xF0;
531 unit|=3;
532 chain=3;
535 spin_lock(&sound_loader_lock);
536 s = __look_for_unit(chain, unit);
537 if (s)
538 new_fops = fops_get(s->unit_fops);
539 if (!new_fops) {
540 spin_unlock(&sound_loader_lock);
542 * Please, don't change this order or code.
543 * For ALSA slot means soundcard and OSS emulation code
544 * comes as add-on modules which aren't depend on
545 * ALSA toplevel modules for soundcards, thus we need
546 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
548 request_module("sound-slot-%i", unit>>4);
549 request_module("sound-service-%i-%i", unit>>4, chain);
550 spin_lock(&sound_loader_lock);
551 s = __look_for_unit(chain, unit);
552 if (s)
553 new_fops = fops_get(s->unit_fops);
555 if (new_fops) {
557 * We rely upon the fact that we can't be unloaded while the
558 * subdriver is there, so if ->open() is successful we can
559 * safely drop the reference counter and if it is not we can
560 * revert to old ->f_op. Ugly, indeed, but that's the cost of
561 * switching ->f_op in the first place.
563 int err = 0;
564 const struct file_operations *old_fops = file->f_op;
565 file->f_op = new_fops;
566 spin_unlock(&sound_loader_lock);
567 if(file->f_op->open)
568 err = file->f_op->open(inode,file);
569 if (err) {
570 fops_put(file->f_op);
571 file->f_op = fops_get(old_fops);
573 fops_put(old_fops);
574 unlock_kernel();
575 return err;
577 spin_unlock(&sound_loader_lock);
578 unlock_kernel();
579 return -ENODEV;
582 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
584 static void cleanup_oss_soundcore(void)
586 /* We have nothing to really do here - we know the lists must be
587 empty */
588 unregister_chrdev(SOUND_MAJOR, "sound");
591 static int __init init_oss_soundcore(void)
593 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
594 printk(KERN_ERR "soundcore: sound device already in use.\n");
595 return -EBUSY;
598 return 0;
601 #endif /* CONFIG_SOUND_OSS_CORE */