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>
14 #ifdef CONFIG_SOUND_OSS_CORE
15 static int __init
init_oss_soundcore(void);
16 static void cleanup_oss_soundcore(void);
18 static inline int init_oss_soundcore(void) { return 0; }
19 static inline void cleanup_oss_soundcore(void) { }
22 struct class *sound_class
;
23 EXPORT_SYMBOL(sound_class
);
25 MODULE_DESCRIPTION("Core sound module");
26 MODULE_AUTHOR("Alan Cox");
27 MODULE_LICENSE("GPL");
29 static int __init
init_soundcore(void)
33 rc
= init_oss_soundcore();
37 sound_class
= class_create(THIS_MODULE
, "sound");
38 if (IS_ERR(sound_class
)) {
39 cleanup_oss_soundcore();
40 return PTR_ERR(sound_class
);
46 static void __exit
cleanup_soundcore(void)
48 cleanup_oss_soundcore();
49 class_destroy(sound_class
);
52 module_init(init_soundcore
);
53 module_exit(cleanup_soundcore
);
56 #ifdef CONFIG_SOUND_OSS_CORE
58 * OSS sound core handling. Breaks out sound functions to submodules
60 * Author: Alan Cox <alan.cox@linux.org>
65 * This program is free software; you can redistribute it and/or
66 * modify it under the terms of the GNU General Public License
67 * as published by the Free Software Foundation; either version
68 * 2 of the License, or (at your option) any later version.
70 * --------------------
72 * Top level handler for the sound subsystem. Various devices can
73 * plug into this. The fact they don't all go via OSS doesn't mean
74 * they don't have to implement the OSS API. There is a lot of logic
75 * to keeping much of the OSS weight out of the code in a compatibility
76 * module, but it's up to the driver to rember to load it...
78 * The code provides a set of functions for registration of devices
79 * by type. This is done rather than providing a single call so that
80 * we can hide any future changes in the internals (eg when we go to
81 * 32bit dev_t) from the modules and their interface.
83 * Secondly we need to allocate the dsp, dsp16 and audio devices as
84 * one. Thus we misuse the chains a bit to simplify this.
86 * Thirdly to make it more fun and for 2.3.x and above we do all
87 * of this using fine grained locking.
89 * FIXME: we have to resolve modules and fine grained load/unload
90 * locking at some point in 2.3.x.
93 #include <linux/init.h>
94 #include <linux/slab.h>
95 #include <linux/smp_lock.h>
96 #include <linux/types.h>
97 #include <linux/kernel.h>
98 #include <linux/sound.h>
99 #include <linux/major.h>
100 #include <linux/kmod.h>
102 #define SOUND_STEP 16
107 const struct file_operations
*unit_fops
;
108 struct sound_unit
*next
;
112 #ifdef CONFIG_SOUND_MSNDCLAS
113 extern int msnd_classic_init(void);
115 #ifdef CONFIG_SOUND_MSNDPIN
116 extern int msnd_pinnacle_init(void);
120 * Low level list operator. Scan the ordered list, find a hole and
121 * join into it. Called with the lock asserted
124 static int __sound_insert_unit(struct sound_unit
* s
, struct sound_unit
**list
, const struct file_operations
*fops
, int index
, int low
, int top
)
128 if (index
< 0) { /* first free */
130 while (*list
&& (*list
)->unit_minor
<n
)
131 list
=&((*list
)->next
);
136 if(*list
==NULL
|| (*list
)->unit_minor
>n
)
138 list
=&((*list
)->next
);
147 if ((*list
)->unit_minor
==n
)
149 if ((*list
)->unit_minor
>n
)
151 list
=&((*list
)->next
);
174 * Remove a node from the chain. Called with the lock asserted
177 static struct sound_unit
*__sound_remove_unit(struct sound_unit
**list
, int unit
)
181 struct sound_unit
*p
=*list
;
182 if(p
->unit_minor
==unit
)
189 printk(KERN_ERR
"Sound device %d went missing!\n", unit
);
194 * This lock guards the sound loader list.
197 static DEFINE_SPINLOCK(sound_loader_lock
);
200 * Allocate the controlling structure and add it to the sound driver
201 * list. Acquires locks as needed
204 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
)
206 struct sound_unit
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
212 spin_lock(&sound_loader_lock
);
213 r
= __sound_insert_unit(s
, list
, fops
, index
, low
, top
);
214 spin_unlock(&sound_loader_lock
);
218 else if (r
< SOUND_STEP
)
219 sprintf(s
->name
, "sound/%s", name
);
221 sprintf(s
->name
, "sound/%s%d", name
, r
/ SOUND_STEP
);
223 device_create(sound_class
, dev
, MKDEV(SOUND_MAJOR
, s
->unit_minor
),
233 * Remove a unit. Acquires locks as needed. The drivers MUST have
234 * completed the removal before their file operations become
238 static void sound_remove_unit(struct sound_unit
**list
, int unit
)
240 struct sound_unit
*p
;
242 spin_lock(&sound_loader_lock
);
243 p
= __sound_remove_unit(list
, unit
);
244 spin_unlock(&sound_loader_lock
);
246 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, p
->unit_minor
));
260 * 6 -- sndstat (obsolete)
262 * 8 -- alternate sequencer (see above)
263 * 9 *16 raw synthesizer access
272 static struct sound_unit
*chains
[SOUND_STEP
];
275 * register_sound_special_device - register a special sound node
276 * @fops: File operations for the driver
277 * @unit: Unit number to allocate
278 * @dev: device pointer
280 * Allocate a special sound device by minor number from the sound
281 * subsystem. The allocated number is returned on succes. On failure
282 * a negative error code is returned.
285 int register_sound_special_device(const struct file_operations
*fops
, int unit
,
288 const int chain
= unit
% SOUND_STEP
;
289 int max_unit
= 128 + chain
;
299 if (unit
>= SOUND_STEP
)
314 if (unit
>= SOUND_STEP
)
336 sprintf(_name
, "unknown%d", chain
);
337 if (unit
>= SOUND_STEP
)
343 return sound_insert_unit(&chains
[chain
], fops
, -1, unit
, max_unit
,
344 name
, S_IRUSR
| S_IWUSR
, dev
);
347 EXPORT_SYMBOL(register_sound_special_device
);
349 int register_sound_special(const struct file_operations
*fops
, int unit
)
351 return register_sound_special_device(fops
, unit
, NULL
);
354 EXPORT_SYMBOL(register_sound_special
);
357 * register_sound_mixer - register a mixer device
358 * @fops: File operations for the driver
359 * @dev: Unit number to allocate
361 * Allocate a mixer device. Unit is the number of the mixer requested.
362 * Pass -1 to request the next free mixer unit. On success the allocated
363 * number is returned, on failure a negative error code is returned.
366 int register_sound_mixer(const struct file_operations
*fops
, int dev
)
368 return sound_insert_unit(&chains
[0], fops
, dev
, 0, 128,
369 "mixer", S_IRUSR
| S_IWUSR
, NULL
);
372 EXPORT_SYMBOL(register_sound_mixer
);
375 * register_sound_midi - register a midi device
376 * @fops: File operations for the driver
377 * @dev: Unit number to allocate
379 * Allocate a midi device. Unit is the number of the midi device requested.
380 * Pass -1 to request the next free midi unit. On success the allocated
381 * number is returned, on failure a negative error code is returned.
384 int register_sound_midi(const struct file_operations
*fops
, int dev
)
386 return sound_insert_unit(&chains
[2], fops
, dev
, 2, 130,
387 "midi", S_IRUSR
| S_IWUSR
, NULL
);
390 EXPORT_SYMBOL(register_sound_midi
);
393 * DSP's are registered as a triple. Register only one and cheat
394 * in open - see below.
398 * register_sound_dsp - register a DSP device
399 * @fops: File operations for the driver
400 * @dev: Unit number to allocate
402 * Allocate a DSP device. Unit is the number of the DSP requested.
403 * Pass -1 to request the next free DSP unit. On success the allocated
404 * number is returned, on failure a negative error code is returned.
406 * This function allocates both the audio and dsp device entries together
407 * and will always allocate them as a matching pair - eg dsp3/audio3
410 int register_sound_dsp(const struct file_operations
*fops
, int dev
)
412 return sound_insert_unit(&chains
[3], fops
, dev
, 3, 131,
413 "dsp", S_IWUSR
| S_IRUSR
, NULL
);
416 EXPORT_SYMBOL(register_sound_dsp
);
419 * unregister_sound_special - unregister a special sound device
420 * @unit: unit number to allocate
422 * Release a sound device that was allocated with
423 * register_sound_special(). The unit passed is the return value from
424 * the register function.
428 void unregister_sound_special(int unit
)
430 sound_remove_unit(&chains
[unit
% SOUND_STEP
], unit
);
433 EXPORT_SYMBOL(unregister_sound_special
);
436 * unregister_sound_mixer - unregister a mixer
437 * @unit: unit number to allocate
439 * Release a sound device that was allocated with register_sound_mixer().
440 * The unit passed is the return value from the register function.
443 void unregister_sound_mixer(int unit
)
445 sound_remove_unit(&chains
[0], unit
);
448 EXPORT_SYMBOL(unregister_sound_mixer
);
451 * unregister_sound_midi - unregister a midi device
452 * @unit: unit number to allocate
454 * Release a sound device that was allocated with register_sound_midi().
455 * The unit passed is the return value from the register function.
458 void unregister_sound_midi(int unit
)
460 return sound_remove_unit(&chains
[2], unit
);
463 EXPORT_SYMBOL(unregister_sound_midi
);
466 * unregister_sound_dsp - unregister a DSP device
467 * @unit: unit number to allocate
469 * Release a sound device that was allocated with register_sound_dsp().
470 * The unit passed is the return value from the register function.
472 * Both of the allocated units are released together automatically.
475 void unregister_sound_dsp(int unit
)
477 return sound_remove_unit(&chains
[3], unit
);
481 EXPORT_SYMBOL(unregister_sound_dsp
);
484 * Now our file operations
487 static int soundcore_open(struct inode
*, struct file
*);
489 static const struct file_operations soundcore_fops
=
491 /* We must have an owner or the module locking fails */
492 .owner
= THIS_MODULE
,
493 .open
= soundcore_open
,
496 static struct sound_unit
*__look_for_unit(int chain
, int unit
)
498 struct sound_unit
*s
;
501 while(s
&& s
->unit_minor
<= unit
)
503 if(s
->unit_minor
==unit
)
510 int soundcore_open(struct inode
*inode
, struct file
*file
)
513 int unit
= iminor(inode
);
514 struct sound_unit
*s
;
515 const struct file_operations
*new_fops
= NULL
;
520 if(chain
==4 || chain
==5) /* dsp/audio/dsp16 */
527 spin_lock(&sound_loader_lock
);
528 s
= __look_for_unit(chain
, unit
);
530 new_fops
= fops_get(s
->unit_fops
);
532 spin_unlock(&sound_loader_lock
);
534 * Please, don't change this order or code.
535 * For ALSA slot means soundcard and OSS emulation code
536 * comes as add-on modules which aren't depend on
537 * ALSA toplevel modules for soundcards, thus we need
538 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
540 request_module("sound-slot-%i", unit
>>4);
541 request_module("sound-service-%i-%i", unit
>>4, chain
);
542 spin_lock(&sound_loader_lock
);
543 s
= __look_for_unit(chain
, unit
);
545 new_fops
= fops_get(s
->unit_fops
);
549 * We rely upon the fact that we can't be unloaded while the
550 * subdriver is there, so if ->open() is successful we can
551 * safely drop the reference counter and if it is not we can
552 * revert to old ->f_op. Ugly, indeed, but that's the cost of
553 * switching ->f_op in the first place.
556 const struct file_operations
*old_fops
= file
->f_op
;
557 file
->f_op
= new_fops
;
558 spin_unlock(&sound_loader_lock
);
560 err
= file
->f_op
->open(inode
,file
);
562 fops_put(file
->f_op
);
563 file
->f_op
= fops_get(old_fops
);
569 spin_unlock(&sound_loader_lock
);
574 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR
);
576 static void cleanup_oss_soundcore(void)
578 /* We have nothing to really do here - we know the lists must be
580 unregister_chrdev(SOUND_MAJOR
, "sound");
583 static int __init
init_oss_soundcore(void)
585 if (register_chrdev(SOUND_MAJOR
, "sound", &soundcore_fops
)==-1) {
586 printk(KERN_ERR
"soundcore: sound device already in use.\n");
593 #endif /* CONFIG_SOUND_OSS_CORE */