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.
8 * First, the common part.
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);
19 static inline int init_oss_soundcore(void) { return 0; }
20 static inline void cleanup_oss_soundcore(void) { }
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)
39 rc
= init_oss_soundcore();
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
;
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>
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
115 const struct file_operations
*unit_fops
;
116 struct sound_unit
*next
;
120 #ifdef CONFIG_SOUND_MSNDCLAS
121 extern int msnd_classic_init(void);
123 #ifdef CONFIG_SOUND_MSNDPIN
124 extern int msnd_pinnacle_init(void);
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
)
136 if (index
< 0) { /* first free */
138 while (*list
&& (*list
)->unit_minor
<n
)
139 list
=&((*list
)->next
);
144 if(*list
==NULL
|| (*list
)->unit_minor
>n
)
146 list
=&((*list
)->next
);
155 if ((*list
)->unit_minor
==n
)
157 if ((*list
)->unit_minor
>n
)
159 list
=&((*list
)->next
);
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
)
189 struct sound_unit
*p
=*list
;
190 if(p
->unit_minor
==unit
)
197 printk(KERN_ERR
"Sound device %d went missing!\n", unit
);
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
);
220 spin_lock(&sound_loader_lock
);
221 r
= __sound_insert_unit(s
, list
, fops
, index
, low
, top
);
222 spin_unlock(&sound_loader_lock
);
226 else if (r
< SOUND_STEP
)
227 sprintf(s
->name
, "sound/%s", name
);
229 sprintf(s
->name
, "sound/%s%d", name
, r
/ SOUND_STEP
);
231 device_create(sound_class
, dev
, MKDEV(SOUND_MAJOR
, s
->unit_minor
),
241 * Remove a unit. Acquires locks as needed. The drivers MUST have
242 * completed the removal before their file operations become
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
);
254 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, p
->unit_minor
));
268 * 6 -- sndstat (obsolete)
270 * 8 -- alternate sequencer (see above)
271 * 9 *16 raw synthesizer access
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
,
296 const int chain
= unit
% SOUND_STEP
;
297 int max_unit
= 128 + chain
;
307 if (unit
>= SOUND_STEP
)
322 if (unit
>= SOUND_STEP
)
344 sprintf(_name
, "unknown%d", chain
);
345 if (unit
>= SOUND_STEP
)
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
;
509 while(s
&& s
->unit_minor
<= unit
)
511 if(s
->unit_minor
==unit
)
518 static int soundcore_open(struct inode
*inode
, struct file
*file
)
521 int unit
= iminor(inode
);
522 struct sound_unit
*s
;
523 const struct file_operations
*new_fops
= NULL
;
528 if(chain
==4 || chain
==5) /* dsp/audio/dsp16 */
535 spin_lock(&sound_loader_lock
);
536 s
= __look_for_unit(chain
, unit
);
538 new_fops
= fops_get(s
->unit_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
);
553 new_fops
= fops_get(s
->unit_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.
564 const struct file_operations
*old_fops
= file
->f_op
;
565 file
->f_op
= new_fops
;
566 spin_unlock(&sound_loader_lock
);
568 err
= file
->f_op
->open(inode
,file
);
570 fops_put(file
->f_op
);
571 file
->f_op
= fops_get(old_fops
);
577 spin_unlock(&sound_loader_lock
);
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
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");
601 #endif /* CONFIG_SOUND_OSS_CORE */