blob: 12522e6913d92d068676f4b3916571b03dae2d87 [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>
Mark Brown3d1ee372008-12-15 15:43:43 +000013#include <sound/core.h>
Tejun Heod886e872008-08-28 16:42:51 +020014
15#ifdef CONFIG_SOUND_OSS_CORE
16static int __init init_oss_soundcore(void);
Krzysztof Heltde30d362008-09-07 17:24:33 +020017static void cleanup_oss_soundcore(void);
Tejun Heod886e872008-08-28 16:42:51 +020018#else
19static inline int init_oss_soundcore(void) { return 0; }
20static inline void cleanup_oss_soundcore(void) { }
21#endif
22
23struct class *sound_class;
24EXPORT_SYMBOL(sound_class);
25
26MODULE_DESCRIPTION("Core sound module");
27MODULE_AUTHOR("Alan Cox");
28MODULE_LICENSE("GPL");
29
Kay Sievers7a9d56f2009-04-30 15:23:42 +020030static char *sound_nodename(struct device *dev)
31{
32 return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
33}
34
Tejun Heod886e872008-08-28 16:42:51 +020035static 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
Kay Sievers7a9d56f2009-04-30 15:23:42 +020049 sound_class->nodename = sound_nodename;
50
Tejun Heod886e872008-08-28 16:42:51 +020051 return 0;
52}
53
54static void __exit cleanup_soundcore(void)
55{
56 cleanup_oss_soundcore();
57 class_destroy(sound_class);
58}
59
60module_init(init_soundcore);
61module_exit(cleanup_soundcore);
62
63
64#ifdef CONFIG_SOUND_OSS_CORE
65/*
66 * OSS sound core handling. Breaks out sound functions to submodules
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
Alan Cox2f1e5932008-10-27 15:21:19 +000068 * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <linux/init.h>
102#include <linux/slab.h>
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600103#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <linux/types.h>
105#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <linux/sound.h>
107#include <linux/major.h>
108#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#define SOUND_STEP 16
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112struct sound_unit
113{
114 int unit_minor;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800115 const struct file_operations *unit_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct sound_unit *next;
117 char name[32];
118};
119
120#ifdef CONFIG_SOUND_MSNDCLAS
121extern int msnd_classic_init(void);
122#endif
123#ifdef CONFIG_SOUND_MSNDPIN
124extern int msnd_pinnacle_init(void);
125#endif
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * Low level list operator. Scan the ordered list, find a hole and
129 * join into it. Called with the lock asserted
130 */
131
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800132static 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 -0700133{
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
185static 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
205static 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
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800212static 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 -0700213{
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
Greg Kroah-Hartmanabe9ab82008-07-21 20:03:34 -0700231 device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
232 NULL, s->name+6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 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
246static 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) {
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700254 device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 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
280static struct sound_unit *chains[SOUND_STEP];
281
282/**
Takashi Iwaid5681212005-08-30 08:58:37 +0200283 * register_sound_special_device - register a special sound node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * @fops: File operations for the driver
285 * @unit: Unit number to allocate
Takashi Iwaid5681212005-08-30 08:58:37 +0200286 * @dev: device pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
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
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800293int register_sound_special_device(const struct file_operations *fops, int unit,
Takashi Iwaid5681212005-08-30 08:58:37 +0200294 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
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,
Takashi Iwaid5681212005-08-30 08:58:37 +0200352 name, S_IRUSR | S_IWUSR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Takashi Iwaid5681212005-08-30 08:58:37 +0200355EXPORT_SYMBOL(register_sound_special_device);
356
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800357int register_sound_special(const struct file_operations *fops, int unit)
Takashi Iwaid5681212005-08-30 08:58:37 +0200358{
359 return register_sound_special_device(fops, unit, NULL);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362EXPORT_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
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800374int register_sound_mixer(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
Takashi Iwaid5681212005-08-30 08:58:37 +0200377 "mixer", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380EXPORT_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
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800392int register_sound_midi(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
Takashi Iwaid5681212005-08-30 08:58:37 +0200395 "midi", S_IRUSR | S_IWUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398EXPORT_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
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800418int register_sound_dsp(const struct file_operations *fops, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
Takashi Iwaid5681212005-08-30 08:58:37 +0200421 "dsp", S_IWUSR | S_IRUSR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424EXPORT_SYMBOL(register_sound_dsp);
425
426/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * 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
436void unregister_sound_special(int unit)
437{
438 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
439}
440
441EXPORT_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
451void unregister_sound_mixer(int unit)
452{
453 sound_remove_unit(&chains[0], unit);
454}
455
456EXPORT_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
466void unregister_sound_midi(int unit)
467{
Hannes Edera39c4ad2008-11-20 21:25:25 +0100468 sound_remove_unit(&chains[2], unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471EXPORT_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
483void unregister_sound_dsp(int unit)
484{
Hannes Edera39c4ad2008-11-20 21:25:25 +0100485 sound_remove_unit(&chains[3], unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488
489EXPORT_SYMBOL(unregister_sound_dsp);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * Now our file operations
493 */
494
495static int soundcore_open(struct inode *, struct file *);
496
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800497static const struct file_operations soundcore_fops=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 /* We must have an owner or the module locking fails */
500 .owner = THIS_MODULE,
501 .open = soundcore_open,
502};
503
504static 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
Hannes Edera39c4ad2008-11-20 21:25:25 +0100518static int soundcore_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int chain;
521 int unit = iminor(inode);
522 struct sound_unit *s;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800523 const struct file_operations *new_fops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600525 lock_kernel ();
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 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;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800564 const struct file_operations *old_fops = file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600574 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return err;
576 }
577 spin_unlock(&sound_loader_lock);
Jonathan Corbet78a3c3d2008-05-15 16:39:03 -0600578 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -ENODEV;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
583
Krzysztof Heltde30d362008-09-07 17:24:33 +0200584static void cleanup_oss_soundcore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 /* We have nothing to really do here - we know the lists must be
587 empty */
588 unregister_chrdev(SOUND_MAJOR, "sound");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Tejun Heod886e872008-08-28 16:42:51 +0200591static int __init init_oss_soundcore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 return 0;
599}
600
Tejun Heod886e872008-08-28 16:42:51 +0200601#endif /* CONFIG_SOUND_OSS_CORE */