blob: dcfc1d5ce63173e5c636cc344d391889a1236611 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/slab.h>
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -060040#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/device.h>
48
49#define SOUND_STEP 16
50
51
52struct sound_unit
53{
54 int unit_minor;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080055 const struct file_operations *unit_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct sound_unit *next;
57 char name[32];
58};
59
60#ifdef CONFIG_SOUND_MSNDCLAS
61extern int msnd_classic_init(void);
62#endif
63#ifdef CONFIG_SOUND_MSNDPIN
64extern int msnd_pinnacle_init(void);
65#endif
66
gregkh@suse.de619e6662005-03-23 09:51:41 -080067struct class *sound_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(sound_class);
69
70/*
71 * Low level list operator. Scan the ordered list, find a hole and
72 * join into it. Called with the lock asserted
73 */
74
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080075static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int n=low;
78
79 if (index < 0) { /* first free */
80
81 while (*list && (*list)->unit_minor<n)
82 list=&((*list)->next);
83
84 while(n<top)
85 {
86 /* Found a hole ? */
87 if(*list==NULL || (*list)->unit_minor>n)
88 break;
89 list=&((*list)->next);
90 n+=SOUND_STEP;
91 }
92
93 if(n>=top)
94 return -ENOENT;
95 } else {
96 n = low+(index*16);
97 while (*list) {
98 if ((*list)->unit_minor==n)
99 return -EBUSY;
100 if ((*list)->unit_minor>n)
101 break;
102 list=&((*list)->next);
103 }
104 }
105
106 /*
107 * Fill it in
108 */
109
110 s->unit_minor=n;
111 s->unit_fops=fops;
112
113 /*
114 * Link it
115 */
116
117 s->next=*list;
118 *list=s;
119
120
121 return n;
122}
123
124/*
125 * Remove a node from the chain. Called with the lock asserted
126 */
127
128static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
129{
130 while(*list)
131 {
132 struct sound_unit *p=*list;
133 if(p->unit_minor==unit)
134 {
135 *list=p->next;
136 return p;
137 }
138 list=&(p->next);
139 }
140 printk(KERN_ERR "Sound device %d went missing!\n", unit);
141 return NULL;
142}
143
144/*
145 * This lock guards the sound loader list.
146 */
147
148static DEFINE_SPINLOCK(sound_loader_lock);
149
150/*
151 * Allocate the controlling structure and add it to the sound driver
152 * list. Acquires locks as needed
153 */
154
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800155static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
158 int r;
159
160 if (!s)
161 return -ENOMEM;
162
163 spin_lock(&sound_loader_lock);
164 r = __sound_insert_unit(s, list, fops, index, low, top);
165 spin_unlock(&sound_loader_lock);
166
167 if (r < 0)
168 goto fail;
169 else if (r < SOUND_STEP)
170 sprintf(s->name, "sound/%s", name);
171 else
172 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
173
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700174 device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
175 s->name+6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return r;
177
178 fail:
179 kfree(s);
180 return r;
181}
182
183/*
184 * Remove a unit. Acquires locks as needed. The drivers MUST have
185 * completed the removal before their file operations become
186 * invalid.
187 */
188
189static void sound_remove_unit(struct sound_unit **list, int unit)
190{
191 struct sound_unit *p;
192
193 spin_lock(&sound_loader_lock);
194 p = __sound_remove_unit(list, unit);
195 spin_unlock(&sound_loader_lock);
196 if (p) {
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700197 device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(p);
199 }
200}
201
202/*
203 * Allocations
204 *
205 * 0 *16 Mixers
206 * 1 *8 Sequencers
207 * 2 *16 Midi
208 * 3 *16 DSP
209 * 4 *16 SunDSP
210 * 5 *16 DSP16
211 * 6 -- sndstat (obsolete)
212 * 7 *16 unused
213 * 8 -- alternate sequencer (see above)
214 * 9 *16 raw synthesizer access
215 * 10 *16 unused
216 * 11 *16 unused
217 * 12 *16 unused
218 * 13 *16 unused
219 * 14 *16 unused
220 * 15 *16 unused
221 */
222
223static struct sound_unit *chains[SOUND_STEP];
224
225/**
Takashi Iwaid5681212005-08-30 08:58:37 +0200226 * register_sound_special_device - register a special sound node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * @fops: File operations for the driver
228 * @unit: Unit number to allocate
Takashi Iwaid5681212005-08-30 08:58:37 +0200229 * @dev: device pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
231 * Allocate a special sound device by minor number from the sound
232 * subsystem. The allocated number is returned on succes. On failure
233 * a negative error code is returned.
234 */
235
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800236int register_sound_special_device(const struct file_operations *fops, int unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200237 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 const int chain = unit % SOUND_STEP;
240 int max_unit = 128 + chain;
241 const char *name;
242 char _name[16];
243
244 switch (chain) {
245 case 0:
246 name = "mixer";
247 break;
248 case 1:
249 name = "sequencer";
250 if (unit >= SOUND_STEP)
251 goto __unknown;
252 max_unit = unit + 1;
253 break;
254 case 2:
255 name = "midi";
256 break;
257 case 3:
258 name = "dsp";
259 break;
260 case 4:
261 name = "audio";
262 break;
263 case 8:
264 name = "sequencer2";
265 if (unit >= SOUND_STEP)
266 goto __unknown;
267 max_unit = unit + 1;
268 break;
269 case 9:
270 name = "dmmidi";
271 break;
272 case 10:
273 name = "dmfm";
274 break;
275 case 12:
276 name = "adsp";
277 break;
278 case 13:
279 name = "amidi";
280 break;
281 case 14:
282 name = "admmidi";
283 break;
284 default:
285 {
286 __unknown:
287 sprintf(_name, "unknown%d", chain);
288 if (unit >= SOUND_STEP)
289 strcat(_name, "-");
290 name = _name;
291 }
292 break;
293 }
294 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200295 name, S_IRUSR | S_IWUSR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Takashi Iwaid5681212005-08-30 08:58:37 +0200298EXPORT_SYMBOL(register_sound_special_device);
299
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800300int register_sound_special(const struct file_operations *fops, int unit)
Takashi Iwaid5681212005-08-30 08:58:37 +0200301{
302 return register_sound_special_device(fops, unit, NULL);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305EXPORT_SYMBOL(register_sound_special);
306
307/**
308 * register_sound_mixer - register a mixer device
309 * @fops: File operations for the driver
310 * @dev: Unit number to allocate
311 *
312 * Allocate a mixer device. Unit is the number of the mixer requested.
313 * Pass -1 to request the next free mixer unit. On success the allocated
314 * number is returned, on failure a negative error code is returned.
315 */
316
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800317int register_sound_mixer(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
Takashi Iwaid5681212005-08-30 08:58:37 +0200320 "mixer", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323EXPORT_SYMBOL(register_sound_mixer);
324
325/**
326 * register_sound_midi - register a midi device
327 * @fops: File operations for the driver
328 * @dev: Unit number to allocate
329 *
330 * Allocate a midi device. Unit is the number of the midi device requested.
331 * Pass -1 to request the next free midi unit. On success the allocated
332 * number is returned, on failure a negative error code is returned.
333 */
334
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800335int register_sound_midi(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
Takashi Iwaid5681212005-08-30 08:58:37 +0200338 "midi", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341EXPORT_SYMBOL(register_sound_midi);
342
343/*
344 * DSP's are registered as a triple. Register only one and cheat
345 * in open - see below.
346 */
347
348/**
349 * register_sound_dsp - register a DSP device
350 * @fops: File operations for the driver
351 * @dev: Unit number to allocate
352 *
353 * Allocate a DSP device. Unit is the number of the DSP requested.
354 * Pass -1 to request the next free DSP unit. On success the allocated
355 * number is returned, on failure a negative error code is returned.
356 *
357 * This function allocates both the audio and dsp device entries together
358 * and will always allocate them as a matching pair - eg dsp3/audio3
359 */
360
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800361int register_sound_dsp(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
Takashi Iwaid5681212005-08-30 08:58:37 +0200364 "dsp", S_IWUSR | S_IRUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367EXPORT_SYMBOL(register_sound_dsp);
368
369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * unregister_sound_special - unregister a special sound device
371 * @unit: unit number to allocate
372 *
373 * Release a sound device that was allocated with
374 * register_sound_special(). The unit passed is the return value from
375 * the register function.
376 */
377
378
379void unregister_sound_special(int unit)
380{
381 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
382}
383
384EXPORT_SYMBOL(unregister_sound_special);
385
386/**
387 * unregister_sound_mixer - unregister a mixer
388 * @unit: unit number to allocate
389 *
390 * Release a sound device that was allocated with register_sound_mixer().
391 * The unit passed is the return value from the register function.
392 */
393
394void unregister_sound_mixer(int unit)
395{
396 sound_remove_unit(&chains[0], unit);
397}
398
399EXPORT_SYMBOL(unregister_sound_mixer);
400
401/**
402 * unregister_sound_midi - unregister a midi device
403 * @unit: unit number to allocate
404 *
405 * Release a sound device that was allocated with register_sound_midi().
406 * The unit passed is the return value from the register function.
407 */
408
409void unregister_sound_midi(int unit)
410{
411 return sound_remove_unit(&chains[2], unit);
412}
413
414EXPORT_SYMBOL(unregister_sound_midi);
415
416/**
417 * unregister_sound_dsp - unregister a DSP device
418 * @unit: unit number to allocate
419 *
420 * Release a sound device that was allocated with register_sound_dsp().
421 * The unit passed is the return value from the register function.
422 *
423 * Both of the allocated units are released together automatically.
424 */
425
426void unregister_sound_dsp(int unit)
427{
428 return sound_remove_unit(&chains[3], unit);
429}
430
431
432EXPORT_SYMBOL(unregister_sound_dsp);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Now our file operations
436 */
437
438static int soundcore_open(struct inode *, struct file *);
439
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800440static const struct file_operations soundcore_fops=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 /* We must have an owner or the module locking fails */
443 .owner = THIS_MODULE,
444 .open = soundcore_open,
445};
446
447static struct sound_unit *__look_for_unit(int chain, int unit)
448{
449 struct sound_unit *s;
450
451 s=chains[chain];
452 while(s && s->unit_minor <= unit)
453 {
454 if(s->unit_minor==unit)
455 return s;
456 s=s->next;
457 }
458 return NULL;
459}
460
461int soundcore_open(struct inode *inode, struct file *file)
462{
463 int chain;
464 int unit = iminor(inode);
465 struct sound_unit *s;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800466 const struct file_operations *new_fops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600468 lock_kernel ();
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 chain=unit&0x0F;
471 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
472 {
473 unit&=0xF0;
474 unit|=3;
475 chain=3;
476 }
477
478 spin_lock(&sound_loader_lock);
479 s = __look_for_unit(chain, unit);
480 if (s)
481 new_fops = fops_get(s->unit_fops);
482 if (!new_fops) {
483 spin_unlock(&sound_loader_lock);
484 /*
485 * Please, don't change this order or code.
486 * For ALSA slot means soundcard and OSS emulation code
487 * comes as add-on modules which aren't depend on
488 * ALSA toplevel modules for soundcards, thus we need
489 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
490 */
491 request_module("sound-slot-%i", unit>>4);
492 request_module("sound-service-%i-%i", unit>>4, chain);
493 spin_lock(&sound_loader_lock);
494 s = __look_for_unit(chain, unit);
495 if (s)
496 new_fops = fops_get(s->unit_fops);
497 }
498 if (new_fops) {
499 /*
500 * We rely upon the fact that we can't be unloaded while the
501 * subdriver is there, so if ->open() is successful we can
502 * safely drop the reference counter and if it is not we can
503 * revert to old ->f_op. Ugly, indeed, but that's the cost of
504 * switching ->f_op in the first place.
505 */
506 int err = 0;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800507 const struct file_operations *old_fops = file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 file->f_op = new_fops;
509 spin_unlock(&sound_loader_lock);
510 if(file->f_op->open)
511 err = file->f_op->open(inode,file);
512 if (err) {
513 fops_put(file->f_op);
514 file->f_op = fops_get(old_fops);
515 }
516 fops_put(old_fops);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600517 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return err;
519 }
520 spin_unlock(&sound_loader_lock);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600521 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -ENODEV;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525MODULE_DESCRIPTION("Core sound module");
526MODULE_AUTHOR("Alan Cox");
527MODULE_LICENSE("GPL");
528MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
529
530static void __exit cleanup_soundcore(void)
531{
532 /* We have nothing to really do here - we know the lists must be
533 empty */
534 unregister_chrdev(SOUND_MAJOR, "sound");
gregkh@suse.de619e6662005-03-23 09:51:41 -0800535 class_destroy(sound_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538static int __init init_soundcore(void)
539{
540 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
541 printk(KERN_ERR "soundcore: sound device already in use.\n");
542 return -EBUSY;
543 }
gregkh@suse.de619e6662005-03-23 09:51:41 -0800544 sound_class = class_create(THIS_MODULE, "sound");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (IS_ERR(sound_class))
546 return PTR_ERR(sound_class);
547
548 return 0;
549}
550
551module_init(init_soundcore);
552module_exit(cleanup_soundcore);