blob: 954f994592ab3d7ff6b2f29a84816fbb38d1a039 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Sound core handling. Breaks out sound functions to submodules
3 *
4 * Author: Alan Cox <alan.cox@linux.org>
5 *
6 * Fixes:
7 *
8 *
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.
13 *
14 * --------------------
15 *
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...
21 *
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.
26 *
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.
29 *
30 * Thirdly to make it more fun and for 2.3.x and above we do all
31 * of this using fine grained locking.
32 *
33 * FIXME: we have to resolve modules and fine grained load/unload
34 * locking at some point in 2.3.x.
35 */
36
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/devfs_fs_kernel.h>
48#include <linux/device.h>
49
50#define SOUND_STEP 16
51
52
53struct sound_unit
54{
55 int unit_minor;
56 struct file_operations *unit_fops;
57 struct sound_unit *next;
58 char name[32];
59};
60
61#ifdef CONFIG_SOUND_MSNDCLAS
62extern int msnd_classic_init(void);
63#endif
64#ifdef CONFIG_SOUND_MSNDPIN
65extern int msnd_pinnacle_init(void);
66#endif
67
gregkh@suse.de619e6662005-03-23 09:51:41 -080068struct class *sound_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069EXPORT_SYMBOL(sound_class);
70
71/*
72 * Low level list operator. Scan the ordered list, find a hole and
73 * join into it. Called with the lock asserted
74 */
75
76static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, struct file_operations *fops, int index, int low, int top)
77{
78 int n=low;
79
80 if (index < 0) { /* first free */
81
82 while (*list && (*list)->unit_minor<n)
83 list=&((*list)->next);
84
85 while(n<top)
86 {
87 /* Found a hole ? */
88 if(*list==NULL || (*list)->unit_minor>n)
89 break;
90 list=&((*list)->next);
91 n+=SOUND_STEP;
92 }
93
94 if(n>=top)
95 return -ENOENT;
96 } else {
97 n = low+(index*16);
98 while (*list) {
99 if ((*list)->unit_minor==n)
100 return -EBUSY;
101 if ((*list)->unit_minor>n)
102 break;
103 list=&((*list)->next);
104 }
105 }
106
107 /*
108 * Fill it in
109 */
110
111 s->unit_minor=n;
112 s->unit_fops=fops;
113
114 /*
115 * Link it
116 */
117
118 s->next=*list;
119 *list=s;
120
121
122 return n;
123}
124
125/*
126 * Remove a node from the chain. Called with the lock asserted
127 */
128
129static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
130{
131 while(*list)
132 {
133 struct sound_unit *p=*list;
134 if(p->unit_minor==unit)
135 {
136 *list=p->next;
137 return p;
138 }
139 list=&(p->next);
140 }
141 printk(KERN_ERR "Sound device %d went missing!\n", unit);
142 return NULL;
143}
144
145/*
146 * This lock guards the sound loader list.
147 */
148
149static DEFINE_SPINLOCK(sound_loader_lock);
150
151/*
152 * Allocate the controlling structure and add it to the sound driver
153 * list. Acquires locks as needed
154 */
155
Takashi Iwaid5681212005-08-30 08:58:37 +0200156static int sound_insert_unit(struct sound_unit **list, struct file_operations *fops, int index, int low, int top, const char *name, umode_t mode, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
159 int r;
160
161 if (!s)
162 return -ENOMEM;
163
164 spin_lock(&sound_loader_lock);
165 r = __sound_insert_unit(s, list, fops, index, low, top);
166 spin_unlock(&sound_loader_lock);
167
168 if (r < 0)
169 goto fail;
170 else if (r < SOUND_STEP)
171 sprintf(s->name, "sound/%s", name);
172 else
173 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
174
175 devfs_mk_cdev(MKDEV(SOUND_MAJOR, s->unit_minor),
176 S_IFCHR | mode, s->name);
gregkh@suse.de619e6662005-03-23 09:51:41 -0800177 class_device_create(sound_class, MKDEV(SOUND_MAJOR, s->unit_minor),
Takashi Iwaid5681212005-08-30 08:58:37 +0200178 dev, s->name+6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return r;
180
181 fail:
182 kfree(s);
183 return r;
184}
185
186/*
187 * Remove a unit. Acquires locks as needed. The drivers MUST have
188 * completed the removal before their file operations become
189 * invalid.
190 */
191
192static void sound_remove_unit(struct sound_unit **list, int unit)
193{
194 struct sound_unit *p;
195
196 spin_lock(&sound_loader_lock);
197 p = __sound_remove_unit(list, unit);
198 spin_unlock(&sound_loader_lock);
199 if (p) {
200 devfs_remove(p->name);
gregkh@suse.de619e6662005-03-23 09:51:41 -0800201 class_device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 kfree(p);
203 }
204}
205
206/*
207 * Allocations
208 *
209 * 0 *16 Mixers
210 * 1 *8 Sequencers
211 * 2 *16 Midi
212 * 3 *16 DSP
213 * 4 *16 SunDSP
214 * 5 *16 DSP16
215 * 6 -- sndstat (obsolete)
216 * 7 *16 unused
217 * 8 -- alternate sequencer (see above)
218 * 9 *16 raw synthesizer access
219 * 10 *16 unused
220 * 11 *16 unused
221 * 12 *16 unused
222 * 13 *16 unused
223 * 14 *16 unused
224 * 15 *16 unused
225 */
226
227static struct sound_unit *chains[SOUND_STEP];
228
229/**
Takashi Iwaid5681212005-08-30 08:58:37 +0200230 * register_sound_special_device - register a special sound node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * @fops: File operations for the driver
232 * @unit: Unit number to allocate
Takashi Iwaid5681212005-08-30 08:58:37 +0200233 * @dev: device pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
235 * Allocate a special sound device by minor number from the sound
236 * subsystem. The allocated number is returned on succes. On failure
237 * a negative error code is returned.
238 */
239
Takashi Iwaid5681212005-08-30 08:58:37 +0200240int register_sound_special_device(struct file_operations *fops, int unit,
241 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 const int chain = unit % SOUND_STEP;
244 int max_unit = 128 + chain;
245 const char *name;
246 char _name[16];
247
248 switch (chain) {
249 case 0:
250 name = "mixer";
251 break;
252 case 1:
253 name = "sequencer";
254 if (unit >= SOUND_STEP)
255 goto __unknown;
256 max_unit = unit + 1;
257 break;
258 case 2:
259 name = "midi";
260 break;
261 case 3:
262 name = "dsp";
263 break;
264 case 4:
265 name = "audio";
266 break;
267 case 8:
268 name = "sequencer2";
269 if (unit >= SOUND_STEP)
270 goto __unknown;
271 max_unit = unit + 1;
272 break;
273 case 9:
274 name = "dmmidi";
275 break;
276 case 10:
277 name = "dmfm";
278 break;
279 case 12:
280 name = "adsp";
281 break;
282 case 13:
283 name = "amidi";
284 break;
285 case 14:
286 name = "admmidi";
287 break;
288 default:
289 {
290 __unknown:
291 sprintf(_name, "unknown%d", chain);
292 if (unit >= SOUND_STEP)
293 strcat(_name, "-");
294 name = _name;
295 }
296 break;
297 }
298 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200299 name, S_IRUSR | S_IWUSR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Takashi Iwaid5681212005-08-30 08:58:37 +0200302EXPORT_SYMBOL(register_sound_special_device);
303
304int register_sound_special(struct file_operations *fops, int unit)
305{
306 return register_sound_special_device(fops, unit, NULL);
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309EXPORT_SYMBOL(register_sound_special);
310
311/**
312 * register_sound_mixer - register a mixer device
313 * @fops: File operations for the driver
314 * @dev: Unit number to allocate
315 *
316 * Allocate a mixer device. Unit is the number of the mixer requested.
317 * Pass -1 to request the next free mixer unit. On success the allocated
318 * number is returned, on failure a negative error code is returned.
319 */
320
321int register_sound_mixer(struct file_operations *fops, int dev)
322{
323 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
Takashi Iwaid5681212005-08-30 08:58:37 +0200324 "mixer", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327EXPORT_SYMBOL(register_sound_mixer);
328
329/**
330 * register_sound_midi - register a midi device
331 * @fops: File operations for the driver
332 * @dev: Unit number to allocate
333 *
334 * Allocate a midi device. Unit is the number of the midi device requested.
335 * Pass -1 to request the next free midi unit. On success the allocated
336 * number is returned, on failure a negative error code is returned.
337 */
338
339int register_sound_midi(struct file_operations *fops, int dev)
340{
341 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
Takashi Iwaid5681212005-08-30 08:58:37 +0200342 "midi", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345EXPORT_SYMBOL(register_sound_midi);
346
347/*
348 * DSP's are registered as a triple. Register only one and cheat
349 * in open - see below.
350 */
351
352/**
353 * register_sound_dsp - register a DSP device
354 * @fops: File operations for the driver
355 * @dev: Unit number to allocate
356 *
357 * Allocate a DSP device. Unit is the number of the DSP requested.
358 * Pass -1 to request the next free DSP unit. On success the allocated
359 * number is returned, on failure a negative error code is returned.
360 *
361 * This function allocates both the audio and dsp device entries together
362 * and will always allocate them as a matching pair - eg dsp3/audio3
363 */
364
365int register_sound_dsp(struct file_operations *fops, int dev)
366{
367 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
Takashi Iwaid5681212005-08-30 08:58:37 +0200368 "dsp", S_IWUSR | S_IRUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371EXPORT_SYMBOL(register_sound_dsp);
372
373/**
374 * register_sound_synth - register a synth device
375 * @fops: File operations for the driver
376 * @dev: Unit number to allocate
377 *
378 * Allocate a synth device. Unit is the number of the synth device requested.
379 * Pass -1 to request the next free synth unit. On success the allocated
380 * number is returned, on failure a negative error code is returned.
381 */
382
383
384int register_sound_synth(struct file_operations *fops, int dev)
385{
386 return sound_insert_unit(&chains[9], fops, dev, 9, 137,
Takashi Iwaid5681212005-08-30 08:58:37 +0200387 "synth", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390EXPORT_SYMBOL(register_sound_synth);
391
392/**
393 * unregister_sound_special - unregister a special sound device
394 * @unit: unit number to allocate
395 *
396 * Release a sound device that was allocated with
397 * register_sound_special(). The unit passed is the return value from
398 * the register function.
399 */
400
401
402void unregister_sound_special(int unit)
403{
404 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
405}
406
407EXPORT_SYMBOL(unregister_sound_special);
408
409/**
410 * unregister_sound_mixer - unregister a mixer
411 * @unit: unit number to allocate
412 *
413 * Release a sound device that was allocated with register_sound_mixer().
414 * The unit passed is the return value from the register function.
415 */
416
417void unregister_sound_mixer(int unit)
418{
419 sound_remove_unit(&chains[0], unit);
420}
421
422EXPORT_SYMBOL(unregister_sound_mixer);
423
424/**
425 * unregister_sound_midi - unregister a midi device
426 * @unit: unit number to allocate
427 *
428 * Release a sound device that was allocated with register_sound_midi().
429 * The unit passed is the return value from the register function.
430 */
431
432void unregister_sound_midi(int unit)
433{
434 return sound_remove_unit(&chains[2], unit);
435}
436
437EXPORT_SYMBOL(unregister_sound_midi);
438
439/**
440 * unregister_sound_dsp - unregister a DSP device
441 * @unit: unit number to allocate
442 *
443 * Release a sound device that was allocated with register_sound_dsp().
444 * The unit passed is the return value from the register function.
445 *
446 * Both of the allocated units are released together automatically.
447 */
448
449void unregister_sound_dsp(int unit)
450{
451 return sound_remove_unit(&chains[3], unit);
452}
453
454
455EXPORT_SYMBOL(unregister_sound_dsp);
456
457/**
458 * unregister_sound_synth - unregister a synth device
459 * @unit: unit number to allocate
460 *
461 * Release a sound device that was allocated with register_sound_synth().
462 * The unit passed is the return value from the register function.
463 */
464
465void unregister_sound_synth(int unit)
466{
467 return sound_remove_unit(&chains[9], unit);
468}
469
470EXPORT_SYMBOL(unregister_sound_synth);
471
472/*
473 * Now our file operations
474 */
475
476static int soundcore_open(struct inode *, struct file *);
477
478static struct file_operations soundcore_fops=
479{
480 /* We must have an owner or the module locking fails */
481 .owner = THIS_MODULE,
482 .open = soundcore_open,
483};
484
485static struct sound_unit *__look_for_unit(int chain, int unit)
486{
487 struct sound_unit *s;
488
489 s=chains[chain];
490 while(s && s->unit_minor <= unit)
491 {
492 if(s->unit_minor==unit)
493 return s;
494 s=s->next;
495 }
496 return NULL;
497}
498
499int soundcore_open(struct inode *inode, struct file *file)
500{
501 int chain;
502 int unit = iminor(inode);
503 struct sound_unit *s;
504 struct file_operations *new_fops = NULL;
505
506 chain=unit&0x0F;
507 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
508 {
509 unit&=0xF0;
510 unit|=3;
511 chain=3;
512 }
513
514 spin_lock(&sound_loader_lock);
515 s = __look_for_unit(chain, unit);
516 if (s)
517 new_fops = fops_get(s->unit_fops);
518 if (!new_fops) {
519 spin_unlock(&sound_loader_lock);
520 /*
521 * Please, don't change this order or code.
522 * For ALSA slot means soundcard and OSS emulation code
523 * comes as add-on modules which aren't depend on
524 * ALSA toplevel modules for soundcards, thus we need
525 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
526 */
527 request_module("sound-slot-%i", unit>>4);
528 request_module("sound-service-%i-%i", unit>>4, chain);
529 spin_lock(&sound_loader_lock);
530 s = __look_for_unit(chain, unit);
531 if (s)
532 new_fops = fops_get(s->unit_fops);
533 }
534 if (new_fops) {
535 /*
536 * We rely upon the fact that we can't be unloaded while the
537 * subdriver is there, so if ->open() is successful we can
538 * safely drop the reference counter and if it is not we can
539 * revert to old ->f_op. Ugly, indeed, but that's the cost of
540 * switching ->f_op in the first place.
541 */
542 int err = 0;
543 struct file_operations *old_fops = file->f_op;
544 file->f_op = new_fops;
545 spin_unlock(&sound_loader_lock);
546 if(file->f_op->open)
547 err = file->f_op->open(inode,file);
548 if (err) {
549 fops_put(file->f_op);
550 file->f_op = fops_get(old_fops);
551 }
552 fops_put(old_fops);
553 return err;
554 }
555 spin_unlock(&sound_loader_lock);
556 return -ENODEV;
557}
558
559extern int mod_firmware_load(const char *, char **);
560EXPORT_SYMBOL(mod_firmware_load);
561
562
563MODULE_DESCRIPTION("Core sound module");
564MODULE_AUTHOR("Alan Cox");
565MODULE_LICENSE("GPL");
566MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
567
568static void __exit cleanup_soundcore(void)
569{
570 /* We have nothing to really do here - we know the lists must be
571 empty */
572 unregister_chrdev(SOUND_MAJOR, "sound");
573 devfs_remove("sound");
gregkh@suse.de619e6662005-03-23 09:51:41 -0800574 class_destroy(sound_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577static int __init init_soundcore(void)
578{
579 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
580 printk(KERN_ERR "soundcore: sound device already in use.\n");
581 return -EBUSY;
582 }
583 devfs_mk_dir ("sound");
gregkh@suse.de619e6662005-03-23 09:51:41 -0800584 sound_class = class_create(THIS_MODULE, "sound");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (IS_ERR(sound_class))
586 return PTR_ERR(sound_class);
587
588 return 0;
589}
590
591module_init(init_soundcore);
592module_exit(cleanup_soundcore);