]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - sound/sound_core.c
ASoC: Provide API for reordering channels
[mirror_ubuntu-artful-kernel.git] / sound / sound_core.c
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 */
6
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>
14
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
22
23 struct class *sound_class;
24 EXPORT_SYMBOL(sound_class);
25
26 MODULE_DESCRIPTION("Core sound module");
27 MODULE_AUTHOR("Alan Cox");
28 MODULE_LICENSE("GPL");
29
30 static char *sound_nodename(struct device *dev)
31 {
32 return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
33 }
34
35 static int __init init_soundcore(void)
36 {
37 int rc;
38
39 rc = init_oss_soundcore();
40 if (rc)
41 return rc;
42
43 sound_class = class_create(THIS_MODULE, "sound");
44 if (IS_ERR(sound_class)) {
45 cleanup_oss_soundcore();
46 return PTR_ERR(sound_class);
47 }
48
49 sound_class->nodename = sound_nodename;
50
51 return 0;
52 }
53
54 static void __exit cleanup_soundcore(void)
55 {
56 cleanup_oss_soundcore();
57 class_destroy(sound_class);
58 }
59
60 module_init(init_soundcore);
61 module_exit(cleanup_soundcore);
62
63
64 #ifdef CONFIG_SOUND_OSS_CORE
65 /*
66 * OSS sound core handling. Breaks out sound functions to submodules
67 *
68 * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
69 *
70 * Fixes:
71 *
72 *
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.
77 *
78 * --------------------
79 *
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...
85 *
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.
90 *
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.
93 *
94 * Thirdly to make it more fun and for 2.3.x and above we do all
95 * of this using fine grained locking.
96 *
97 * FIXME: we have to resolve modules and fine grained load/unload
98 * locking at some point in 2.3.x.
99 */
100
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>
109
110 #define SOUND_STEP 16
111
112 struct sound_unit
113 {
114 int unit_minor;
115 const struct file_operations *unit_fops;
116 struct sound_unit *next;
117 char name[32];
118 };
119
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
126
127 /*
128 * Low level list operator. Scan the ordered list, find a hole and
129 * join into it. Called with the lock asserted
130 */
131
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)
133 {
134 int n=low;
135
136 if (index < 0) { /* first free */
137
138 while (*list && (*list)->unit_minor<n)
139 list=&((*list)->next);
140
141 while(n<top)
142 {
143 /* Found a hole ? */
144 if(*list==NULL || (*list)->unit_minor>n)
145 break;
146 list=&((*list)->next);
147 n+=SOUND_STEP;
148 }
149
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);
160 }
161 }
162
163 /*
164 * Fill it in
165 */
166
167 s->unit_minor=n;
168 s->unit_fops=fops;
169
170 /*
171 * Link it
172 */
173
174 s->next=*list;
175 *list=s;
176
177
178 return n;
179 }
180
181 /*
182 * Remove a node from the chain. Called with the lock asserted
183 */
184
185 static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
186 {
187 while(*list)
188 {
189 struct sound_unit *p=*list;
190 if(p->unit_minor==unit)
191 {
192 *list=p->next;
193 return p;
194 }
195 list=&(p->next);
196 }
197 printk(KERN_ERR "Sound device %d went missing!\n", unit);
198 return NULL;
199 }
200
201 /*
202 * This lock guards the sound loader list.
203 */
204
205 static DEFINE_SPINLOCK(sound_loader_lock);
206
207 /*
208 * Allocate the controlling structure and add it to the sound driver
209 * list. Acquires locks as needed
210 */
211
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)
213 {
214 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
215 int r;
216
217 if (!s)
218 return -ENOMEM;
219
220 spin_lock(&sound_loader_lock);
221 r = __sound_insert_unit(s, list, fops, index, low, top);
222 spin_unlock(&sound_loader_lock);
223
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);
230
231 device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
232 NULL, s->name+6);
233 return r;
234
235 fail:
236 kfree(s);
237 return r;
238 }
239
240 /*
241 * Remove a unit. Acquires locks as needed. The drivers MUST have
242 * completed the removal before their file operations become
243 * invalid.
244 */
245
246 static void sound_remove_unit(struct sound_unit **list, int unit)
247 {
248 struct sound_unit *p;
249
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);
256 }
257 }
258
259 /*
260 * Allocations
261 *
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
278 */
279
280 static struct sound_unit *chains[SOUND_STEP];
281
282 /**
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
287 *
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.
291 */
292
293 int register_sound_special_device(const struct file_operations *fops, int unit,
294 struct device *dev)
295 {
296 const int chain = unit % SOUND_STEP;
297 int max_unit = 128 + chain;
298 const char *name;
299 char _name[16];
300
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:
342 {
343 __unknown:
344 sprintf(_name, "unknown%d", chain);
345 if (unit >= SOUND_STEP)
346 strcat(_name, "-");
347 name = _name;
348 }
349 break;
350 }
351 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
352 name, S_IRUSR | S_IWUSR, dev);
353 }
354
355 EXPORT_SYMBOL(register_sound_special_device);
356
357 int register_sound_special(const struct file_operations *fops, int unit)
358 {
359 return register_sound_special_device(fops, unit, NULL);
360 }
361
362 EXPORT_SYMBOL(register_sound_special);
363
364 /**
365 * register_sound_mixer - register a mixer device
366 * @fops: File operations for the driver
367 * @dev: Unit number to allocate
368 *
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.
372 */
373
374 int register_sound_mixer(const struct file_operations *fops, int dev)
375 {
376 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
377 "mixer", S_IRUSR | S_IWUSR, NULL);
378 }
379
380 EXPORT_SYMBOL(register_sound_mixer);
381
382 /**
383 * register_sound_midi - register a midi device
384 * @fops: File operations for the driver
385 * @dev: Unit number to allocate
386 *
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.
390 */
391
392 int register_sound_midi(const struct file_operations *fops, int dev)
393 {
394 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
395 "midi", S_IRUSR | S_IWUSR, NULL);
396 }
397
398 EXPORT_SYMBOL(register_sound_midi);
399
400 /*
401 * DSP's are registered as a triple. Register only one and cheat
402 * in open - see below.
403 */
404
405 /**
406 * register_sound_dsp - register a DSP device
407 * @fops: File operations for the driver
408 * @dev: Unit number to allocate
409 *
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.
413 *
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
416 */
417
418 int register_sound_dsp(const struct file_operations *fops, int dev)
419 {
420 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
421 "dsp", S_IWUSR | S_IRUSR, NULL);
422 }
423
424 EXPORT_SYMBOL(register_sound_dsp);
425
426 /**
427 * unregister_sound_special - unregister a special sound device
428 * @unit: unit number to allocate
429 *
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.
433 */
434
435
436 void unregister_sound_special(int unit)
437 {
438 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
439 }
440
441 EXPORT_SYMBOL(unregister_sound_special);
442
443 /**
444 * unregister_sound_mixer - unregister a mixer
445 * @unit: unit number to allocate
446 *
447 * Release a sound device that was allocated with register_sound_mixer().
448 * The unit passed is the return value from the register function.
449 */
450
451 void unregister_sound_mixer(int unit)
452 {
453 sound_remove_unit(&chains[0], unit);
454 }
455
456 EXPORT_SYMBOL(unregister_sound_mixer);
457
458 /**
459 * unregister_sound_midi - unregister a midi device
460 * @unit: unit number to allocate
461 *
462 * Release a sound device that was allocated with register_sound_midi().
463 * The unit passed is the return value from the register function.
464 */
465
466 void unregister_sound_midi(int unit)
467 {
468 sound_remove_unit(&chains[2], unit);
469 }
470
471 EXPORT_SYMBOL(unregister_sound_midi);
472
473 /**
474 * unregister_sound_dsp - unregister a DSP device
475 * @unit: unit number to allocate
476 *
477 * Release a sound device that was allocated with register_sound_dsp().
478 * The unit passed is the return value from the register function.
479 *
480 * Both of the allocated units are released together automatically.
481 */
482
483 void unregister_sound_dsp(int unit)
484 {
485 sound_remove_unit(&chains[3], unit);
486 }
487
488
489 EXPORT_SYMBOL(unregister_sound_dsp);
490
491 /*
492 * Now our file operations
493 */
494
495 static int soundcore_open(struct inode *, struct file *);
496
497 static const struct file_operations soundcore_fops=
498 {
499 /* We must have an owner or the module locking fails */
500 .owner = THIS_MODULE,
501 .open = soundcore_open,
502 };
503
504 static struct sound_unit *__look_for_unit(int chain, int unit)
505 {
506 struct sound_unit *s;
507
508 s=chains[chain];
509 while(s && s->unit_minor <= unit)
510 {
511 if(s->unit_minor==unit)
512 return s;
513 s=s->next;
514 }
515 return NULL;
516 }
517
518 static int soundcore_open(struct inode *inode, struct file *file)
519 {
520 int chain;
521 int unit = iminor(inode);
522 struct sound_unit *s;
523 const struct file_operations *new_fops = NULL;
524
525 lock_kernel ();
526
527 chain=unit&0x0F;
528 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
529 {
530 unit&=0xF0;
531 unit|=3;
532 chain=3;
533 }
534
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);
541 /*
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>]
547 */
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);
554 }
555 if (new_fops) {
556 /*
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.
562 */
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);
572 }
573 fops_put(old_fops);
574 unlock_kernel();
575 return err;
576 }
577 spin_unlock(&sound_loader_lock);
578 unlock_kernel();
579 return -ENODEV;
580 }
581
582 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
583
584 static void cleanup_oss_soundcore(void)
585 {
586 /* We have nothing to really do here - we know the lists must be
587 empty */
588 unregister_chrdev(SOUND_MAJOR, "sound");
589 }
590
591 static int __init init_oss_soundcore(void)
592 {
593 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
594 printk(KERN_ERR "soundcore: sound device already in use.\n");
595 return -EBUSY;
596 }
597
598 return 0;
599 }
600
601 #endif /* CONFIG_SOUND_OSS_CORE */