blob: ee66d43f848c549c7f93a62dbe46325430456128 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heod886e872008-08-28 16:42:51 +02002 * 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
14#ifdef CONFIG_SOUND_OSS_CORE
15static int __init init_oss_soundcore(void);
Krzysztof Heltde30d362008-09-07 17:24:33 +020016static void cleanup_oss_soundcore(void);
Tejun Heod886e872008-08-28 16:42:51 +020017#else
18static inline int init_oss_soundcore(void) { return 0; }
19static inline void cleanup_oss_soundcore(void) { }
20#endif
21
22struct class *sound_class;
23EXPORT_SYMBOL(sound_class);
24
25MODULE_DESCRIPTION("Core sound module");
26MODULE_AUTHOR("Alan Cox");
27MODULE_LICENSE("GPL");
28
29static int __init init_soundcore(void)
30{
31 int rc;
32
33 rc = init_oss_soundcore();
34 if (rc)
35 return rc;
36
37 sound_class = class_create(THIS_MODULE, "sound");
38 if (IS_ERR(sound_class)) {
39 cleanup_oss_soundcore();
40 return PTR_ERR(sound_class);
41 }
42
43 return 0;
44}
45
46static void __exit cleanup_soundcore(void)
47{
48 cleanup_oss_soundcore();
49 class_destroy(sound_class);
50}
51
52module_init(init_soundcore);
53module_exit(cleanup_soundcore);
54
55
56#ifdef CONFIG_SOUND_OSS_CORE
57/*
58 * OSS sound core handling. Breaks out sound functions to submodules
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
Alan Cox2f1e5932008-10-27 15:21:19 +000060 * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Fixes:
63 *
64 *
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.
69 *
70 * --------------------
71 *
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...
77 *
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.
82 *
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.
85 *
86 * Thirdly to make it more fun and for 2.3.x and above we do all
87 * of this using fine grained locking.
88 *
89 * FIXME: we have to resolve modules and fine grained load/unload
90 * locking at some point in 2.3.x.
91 */
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/init.h>
94#include <linux/slab.h>
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -060095#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/types.h>
97#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/sound.h>
99#include <linux/major.h>
100#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#define SOUND_STEP 16
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104struct sound_unit
105{
106 int unit_minor;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800107 const struct file_operations *unit_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct sound_unit *next;
109 char name[32];
110};
111
112#ifdef CONFIG_SOUND_MSNDCLAS
113extern int msnd_classic_init(void);
114#endif
115#ifdef CONFIG_SOUND_MSNDPIN
116extern int msnd_pinnacle_init(void);
117#endif
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Low level list operator. Scan the ordered list, find a hole and
121 * join into it. Called with the lock asserted
122 */
123
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800124static 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 -0700125{
126 int n=low;
127
128 if (index < 0) { /* first free */
129
130 while (*list && (*list)->unit_minor<n)
131 list=&((*list)->next);
132
133 while(n<top)
134 {
135 /* Found a hole ? */
136 if(*list==NULL || (*list)->unit_minor>n)
137 break;
138 list=&((*list)->next);
139 n+=SOUND_STEP;
140 }
141
142 if(n>=top)
143 return -ENOENT;
144 } else {
145 n = low+(index*16);
146 while (*list) {
147 if ((*list)->unit_minor==n)
148 return -EBUSY;
149 if ((*list)->unit_minor>n)
150 break;
151 list=&((*list)->next);
152 }
153 }
154
155 /*
156 * Fill it in
157 */
158
159 s->unit_minor=n;
160 s->unit_fops=fops;
161
162 /*
163 * Link it
164 */
165
166 s->next=*list;
167 *list=s;
168
169
170 return n;
171}
172
173/*
174 * Remove a node from the chain. Called with the lock asserted
175 */
176
177static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
178{
179 while(*list)
180 {
181 struct sound_unit *p=*list;
182 if(p->unit_minor==unit)
183 {
184 *list=p->next;
185 return p;
186 }
187 list=&(p->next);
188 }
189 printk(KERN_ERR "Sound device %d went missing!\n", unit);
190 return NULL;
191}
192
193/*
194 * This lock guards the sound loader list.
195 */
196
197static DEFINE_SPINLOCK(sound_loader_lock);
198
199/*
200 * Allocate the controlling structure and add it to the sound driver
201 * list. Acquires locks as needed
202 */
203
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800204static 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 -0700205{
206 struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
207 int r;
208
209 if (!s)
210 return -ENOMEM;
211
212 spin_lock(&sound_loader_lock);
213 r = __sound_insert_unit(s, list, fops, index, low, top);
214 spin_unlock(&sound_loader_lock);
215
216 if (r < 0)
217 goto fail;
218 else if (r < SOUND_STEP)
219 sprintf(s->name, "sound/%s", name);
220 else
221 sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
222
Greg Kroah-Hartman89409212008-05-21 12:52:33 -0700223 device_create_drvdata(sound_class, dev,
224 MKDEV(SOUND_MAJOR, s->unit_minor),
225 NULL, s->name+6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return r;
227
228 fail:
229 kfree(s);
230 return r;
231}
232
233/*
234 * Remove a unit. Acquires locks as needed. The drivers MUST have
235 * completed the removal before their file operations become
236 * invalid.
237 */
238
239static void sound_remove_unit(struct sound_unit **list, int unit)
240{
241 struct sound_unit *p;
242
243 spin_lock(&sound_loader_lock);
244 p = __sound_remove_unit(list, unit);
245 spin_unlock(&sound_loader_lock);
246 if (p) {
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700247 device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 kfree(p);
249 }
250}
251
252/*
253 * Allocations
254 *
255 * 0 *16 Mixers
256 * 1 *8 Sequencers
257 * 2 *16 Midi
258 * 3 *16 DSP
259 * 4 *16 SunDSP
260 * 5 *16 DSP16
261 * 6 -- sndstat (obsolete)
262 * 7 *16 unused
263 * 8 -- alternate sequencer (see above)
264 * 9 *16 raw synthesizer access
265 * 10 *16 unused
266 * 11 *16 unused
267 * 12 *16 unused
268 * 13 *16 unused
269 * 14 *16 unused
270 * 15 *16 unused
271 */
272
273static struct sound_unit *chains[SOUND_STEP];
274
275/**
Takashi Iwaid5681212005-08-30 08:58:37 +0200276 * register_sound_special_device - register a special sound node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * @fops: File operations for the driver
278 * @unit: Unit number to allocate
Takashi Iwaid5681212005-08-30 08:58:37 +0200279 * @dev: device pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Allocate a special sound device by minor number from the sound
282 * subsystem. The allocated number is returned on succes. On failure
283 * a negative error code is returned.
284 */
285
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800286int register_sound_special_device(const struct file_operations *fops, int unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200287 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 const int chain = unit % SOUND_STEP;
290 int max_unit = 128 + chain;
291 const char *name;
292 char _name[16];
293
294 switch (chain) {
295 case 0:
296 name = "mixer";
297 break;
298 case 1:
299 name = "sequencer";
300 if (unit >= SOUND_STEP)
301 goto __unknown;
302 max_unit = unit + 1;
303 break;
304 case 2:
305 name = "midi";
306 break;
307 case 3:
308 name = "dsp";
309 break;
310 case 4:
311 name = "audio";
312 break;
313 case 8:
314 name = "sequencer2";
315 if (unit >= SOUND_STEP)
316 goto __unknown;
317 max_unit = unit + 1;
318 break;
319 case 9:
320 name = "dmmidi";
321 break;
322 case 10:
323 name = "dmfm";
324 break;
325 case 12:
326 name = "adsp";
327 break;
328 case 13:
329 name = "amidi";
330 break;
331 case 14:
332 name = "admmidi";
333 break;
334 default:
335 {
336 __unknown:
337 sprintf(_name, "unknown%d", chain);
338 if (unit >= SOUND_STEP)
339 strcat(_name, "-");
340 name = _name;
341 }
342 break;
343 }
344 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200345 name, S_IRUSR | S_IWUSR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Takashi Iwaid5681212005-08-30 08:58:37 +0200348EXPORT_SYMBOL(register_sound_special_device);
349
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800350int register_sound_special(const struct file_operations *fops, int unit)
Takashi Iwaid5681212005-08-30 08:58:37 +0200351{
352 return register_sound_special_device(fops, unit, NULL);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355EXPORT_SYMBOL(register_sound_special);
356
357/**
358 * register_sound_mixer - register a mixer device
359 * @fops: File operations for the driver
360 * @dev: Unit number to allocate
361 *
362 * Allocate a mixer device. Unit is the number of the mixer requested.
363 * Pass -1 to request the next free mixer unit. On success the allocated
364 * number is returned, on failure a negative error code is returned.
365 */
366
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800367int register_sound_mixer(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
Takashi Iwaid5681212005-08-30 08:58:37 +0200370 "mixer", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373EXPORT_SYMBOL(register_sound_mixer);
374
375/**
376 * register_sound_midi - register a midi device
377 * @fops: File operations for the driver
378 * @dev: Unit number to allocate
379 *
380 * Allocate a midi device. Unit is the number of the midi device requested.
381 * Pass -1 to request the next free midi unit. On success the allocated
382 * number is returned, on failure a negative error code is returned.
383 */
384
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800385int register_sound_midi(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
Takashi Iwaid5681212005-08-30 08:58:37 +0200388 "midi", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391EXPORT_SYMBOL(register_sound_midi);
392
393/*
394 * DSP's are registered as a triple. Register only one and cheat
395 * in open - see below.
396 */
397
398/**
399 * register_sound_dsp - register a DSP device
400 * @fops: File operations for the driver
401 * @dev: Unit number to allocate
402 *
403 * Allocate a DSP device. Unit is the number of the DSP requested.
404 * Pass -1 to request the next free DSP unit. On success the allocated
405 * number is returned, on failure a negative error code is returned.
406 *
407 * This function allocates both the audio and dsp device entries together
408 * and will always allocate them as a matching pair - eg dsp3/audio3
409 */
410
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800411int register_sound_dsp(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
Takashi Iwaid5681212005-08-30 08:58:37 +0200414 "dsp", S_IWUSR | S_IRUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417EXPORT_SYMBOL(register_sound_dsp);
418
419/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * unregister_sound_special - unregister a special sound device
421 * @unit: unit number to allocate
422 *
423 * Release a sound device that was allocated with
424 * register_sound_special(). The unit passed is the return value from
425 * the register function.
426 */
427
428
429void unregister_sound_special(int unit)
430{
431 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
432}
433
434EXPORT_SYMBOL(unregister_sound_special);
435
436/**
437 * unregister_sound_mixer - unregister a mixer
438 * @unit: unit number to allocate
439 *
440 * Release a sound device that was allocated with register_sound_mixer().
441 * The unit passed is the return value from the register function.
442 */
443
444void unregister_sound_mixer(int unit)
445{
446 sound_remove_unit(&chains[0], unit);
447}
448
449EXPORT_SYMBOL(unregister_sound_mixer);
450
451/**
452 * unregister_sound_midi - unregister a midi device
453 * @unit: unit number to allocate
454 *
455 * Release a sound device that was allocated with register_sound_midi().
456 * The unit passed is the return value from the register function.
457 */
458
459void unregister_sound_midi(int unit)
460{
461 return sound_remove_unit(&chains[2], unit);
462}
463
464EXPORT_SYMBOL(unregister_sound_midi);
465
466/**
467 * unregister_sound_dsp - unregister a DSP device
468 * @unit: unit number to allocate
469 *
470 * Release a sound device that was allocated with register_sound_dsp().
471 * The unit passed is the return value from the register function.
472 *
473 * Both of the allocated units are released together automatically.
474 */
475
476void unregister_sound_dsp(int unit)
477{
478 return sound_remove_unit(&chains[3], unit);
479}
480
481
482EXPORT_SYMBOL(unregister_sound_dsp);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * Now our file operations
486 */
487
488static int soundcore_open(struct inode *, struct file *);
489
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800490static const struct file_operations soundcore_fops=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 /* We must have an owner or the module locking fails */
493 .owner = THIS_MODULE,
494 .open = soundcore_open,
495};
496
497static struct sound_unit *__look_for_unit(int chain, int unit)
498{
499 struct sound_unit *s;
500
501 s=chains[chain];
502 while(s && s->unit_minor <= unit)
503 {
504 if(s->unit_minor==unit)
505 return s;
506 s=s->next;
507 }
508 return NULL;
509}
510
511int soundcore_open(struct inode *inode, struct file *file)
512{
513 int chain;
514 int unit = iminor(inode);
515 struct sound_unit *s;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800516 const struct file_operations *new_fops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600518 lock_kernel ();
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 chain=unit&0x0F;
521 if(chain==4 || chain==5) /* dsp/audio/dsp16 */
522 {
523 unit&=0xF0;
524 unit|=3;
525 chain=3;
526 }
527
528 spin_lock(&sound_loader_lock);
529 s = __look_for_unit(chain, unit);
530 if (s)
531 new_fops = fops_get(s->unit_fops);
532 if (!new_fops) {
533 spin_unlock(&sound_loader_lock);
534 /*
535 * Please, don't change this order or code.
536 * For ALSA slot means soundcard and OSS emulation code
537 * comes as add-on modules which aren't depend on
538 * ALSA toplevel modules for soundcards, thus we need
539 * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
540 */
541 request_module("sound-slot-%i", unit>>4);
542 request_module("sound-service-%i-%i", unit>>4, chain);
543 spin_lock(&sound_loader_lock);
544 s = __look_for_unit(chain, unit);
545 if (s)
546 new_fops = fops_get(s->unit_fops);
547 }
548 if (new_fops) {
549 /*
550 * We rely upon the fact that we can't be unloaded while the
551 * subdriver is there, so if ->open() is successful we can
552 * safely drop the reference counter and if it is not we can
553 * revert to old ->f_op. Ugly, indeed, but that's the cost of
554 * switching ->f_op in the first place.
555 */
556 int err = 0;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800557 const struct file_operations *old_fops = file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 file->f_op = new_fops;
559 spin_unlock(&sound_loader_lock);
560 if(file->f_op->open)
561 err = file->f_op->open(inode,file);
562 if (err) {
563 fops_put(file->f_op);
564 file->f_op = fops_get(old_fops);
565 }
566 fops_put(old_fops);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600567 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return err;
569 }
570 spin_unlock(&sound_loader_lock);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600571 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return -ENODEV;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
576
Krzysztof Heltde30d362008-09-07 17:24:33 +0200577static void cleanup_oss_soundcore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 /* We have nothing to really do here - we know the lists must be
580 empty */
581 unregister_chrdev(SOUND_MAJOR, "sound");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Tejun Heod886e872008-08-28 16:42:51 +0200584static int __init init_oss_soundcore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
587 printk(KERN_ERR "soundcore: sound device already in use.\n");
588 return -EBUSY;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return 0;
592}
593
Tejun Heod886e872008-08-28 16:42:51 +0200594#endif /* CONFIG_SOUND_OSS_CORE */