1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
68struct class_simple *sound_class;
69EXPORT_SYMBOL(sound_class);
70
71
72
73
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) {
81
82 while (*list && (*list)->unit_minor<n)
83 list=&((*list)->next);
84
85 while(n<top)
86 {
87
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
109
110
111 s->unit_minor=n;
112 s->unit_fops=fops;
113
114
115
116
117
118 s->next=*list;
119 *list=s;
120
121
122 return n;
123}
124
125
126
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
147
148
149static spinlock_t sound_loader_lock = SPIN_LOCK_UNLOCKED;
150
151
152
153
154
155
156static int sound_insert_unit(struct sound_unit **list, struct file_operations *fops, int index, int low, int top, const char *name, umode_t mode)
157{
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);
177 class_simple_device_add(sound_class, MKDEV(SOUND_MAJOR, s->unit_minor),
178 NULL, s->name+6);
179 return r;
180
181 fail:
182 kfree(s);
183 return r;
184}
185
186
187
188
189
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);
201 class_simple_device_remove(MKDEV(SOUND_MAJOR, p->unit_minor));
202 kfree(p);
203 }
204}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227static struct sound_unit *chains[SOUND_STEP];
228
229
230
231
232
233
234
235
236
237
238
239int register_sound_special(struct file_operations *fops, int unit)
240{
241 const int chain = unit % SOUND_STEP;
242 int max_unit = 128 + chain;
243 const char *name;
244 char _name[16];
245
246 switch (chain) {
247 case 0:
248 name = "mixer";
249 break;
250 case 1:
251 name = "sequencer";
252 if (unit >= SOUND_STEP)
253 goto __unknown;
254 max_unit = unit + 1;
255 break;
256 case 2:
257 name = "midi";
258 break;
259 case 3:
260 name = "dsp";
261 break;
262 case 4:
263 name = "audio";
264 break;
265 case 8:
266 name = "sequencer2";
267 if (unit >= SOUND_STEP)
268 goto __unknown;
269 max_unit = unit + 1;
270 break;
271 case 9:
272 name = "dmmidi";
273 break;
274 case 10:
275 name = "dmfm";
276 break;
277 case 12:
278 name = "adsp";
279 break;
280 case 13:
281 name = "amidi";
282 break;
283 case 14:
284 name = "admmidi";
285 break;
286 default:
287 {
288 __unknown:
289 sprintf(_name, "unknown%d", chain);
290 if (unit >= SOUND_STEP)
291 strcat(_name, "-");
292 name = _name;
293 }
294 break;
295 }
296 return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
297 name, S_IRUSR | S_IWUSR);
298}
299
300EXPORT_SYMBOL(register_sound_special);
301
302
303
304
305
306
307
308
309
310
311
312int register_sound_mixer(struct file_operations *fops, int dev)
313{
314 return sound_insert_unit(&chains[0], fops, dev, 0, 128,
315 "mixer", S_IRUSR | S_IWUSR);
316}
317
318EXPORT_SYMBOL(register_sound_mixer);
319
320
321
322
323
324
325
326
327
328
329
330int register_sound_midi(struct file_operations *fops, int dev)
331{
332 return sound_insert_unit(&chains[2], fops, dev, 2, 130,
333 "midi", S_IRUSR | S_IWUSR);
334}
335
336EXPORT_SYMBOL(register_sound_midi);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356int register_sound_dsp(struct file_operations *fops, int dev)
357{
358 return sound_insert_unit(&chains[3], fops, dev, 3, 131,
359 "dsp", S_IWUSR | S_IRUSR);
360}
361
362EXPORT_SYMBOL(register_sound_dsp);
363
364
365
366
367
368
369
370
371
372
373
374
375int register_sound_synth(struct file_operations *fops, int dev)
376{
377 return sound_insert_unit(&chains[9], fops, dev, 9, 137,
378 "synth", S_IRUSR | S_IWUSR);
379}
380
381EXPORT_SYMBOL(register_sound_synth);
382
383
384
385
386
387
388
389
390
391
392
393void unregister_sound_special(int unit)
394{
395 sound_remove_unit(&chains[unit % SOUND_STEP], unit);
396}
397
398EXPORT_SYMBOL(unregister_sound_special);
399
400
401
402
403
404
405
406
407
408void unregister_sound_mixer(int unit)
409{
410 sound_remove_unit(&chains[0], unit);
411}
412
413EXPORT_SYMBOL(unregister_sound_mixer);
414
415
416
417
418
419
420
421
422
423void unregister_sound_midi(int unit)
424{
425 return sound_remove_unit(&chains[2], unit);
426}
427
428EXPORT_SYMBOL(unregister_sound_midi);
429
430
431
432
433
434
435
436
437
438
439
440void unregister_sound_dsp(int unit)
441{
442 return sound_remove_unit(&chains[3], unit);
443}
444
445
446EXPORT_SYMBOL(unregister_sound_dsp);
447
448
449
450
451
452
453
454
455
456void unregister_sound_synth(int unit)
457{
458 return sound_remove_unit(&chains[9], unit);
459}
460
461EXPORT_SYMBOL(unregister_sound_synth);
462
463
464
465
466
467static int soundcore_open(struct inode *, struct file *);
468
469static struct file_operations soundcore_fops=
470{
471
472 .owner = THIS_MODULE,
473 .open = soundcore_open,
474};
475
476static struct sound_unit *__look_for_unit(int chain, int unit)
477{
478 struct sound_unit *s;
479
480 s=chains[chain];
481 while(s && s->unit_minor <= unit)
482 {
483 if(s->unit_minor==unit)
484 return s;
485 s=s->next;
486 }
487 return NULL;
488}
489
490int soundcore_open(struct inode *inode, struct file *file)
491{
492 int chain;
493 int unit = iminor(inode);
494 struct sound_unit *s;
495 struct file_operations *new_fops = NULL;
496
497 chain=unit&0x0F;
498 if(chain==4 || chain==5)
499 {
500 unit&=0xF0;
501 unit|=3;
502 chain=3;
503 }
504
505 spin_lock(&sound_loader_lock);
506 s = __look_for_unit(chain, unit);
507 if (s)
508 new_fops = fops_get(s->unit_fops);
509 if (!new_fops) {
510 spin_unlock(&sound_loader_lock);
511
512
513
514
515
516
517
518 request_module("sound-slot-%i", unit>>4);
519 request_module("sound-service-%i-%i", unit>>4, chain);
520 spin_lock(&sound_loader_lock);
521 s = __look_for_unit(chain, unit);
522 if (s)
523 new_fops = fops_get(s->unit_fops);
524 }
525 if (new_fops) {
526
527
528
529
530
531
532
533 int err = 0;
534 struct file_operations *old_fops = file->f_op;
535 file->f_op = new_fops;
536 spin_unlock(&sound_loader_lock);
537 if(file->f_op->open)
538 err = file->f_op->open(inode,file);
539 if (err) {
540 fops_put(file->f_op);
541 file->f_op = fops_get(old_fops);
542 }
543 fops_put(old_fops);
544 return err;
545 }
546 spin_unlock(&sound_loader_lock);
547 return -ENODEV;
548}
549
550extern int mod_firmware_load(const char *, char **);
551EXPORT_SYMBOL(mod_firmware_load);
552
553
554MODULE_DESCRIPTION("Core sound module");
555MODULE_AUTHOR("Alan Cox");
556MODULE_LICENSE("GPL");
557MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
558
559static void __exit cleanup_soundcore(void)
560{
561
562
563 unregister_chrdev(SOUND_MAJOR, "sound");
564 devfs_remove("sound");
565 class_simple_destroy(sound_class);
566}
567
568static int __init init_soundcore(void)
569{
570 if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
571 printk(KERN_ERR "soundcore: sound device already in use.\n");
572 return -EBUSY;
573 }
574 devfs_mk_dir ("sound");
575 sound_class = class_simple_create(THIS_MODULE, "sound");
576 if (IS_ERR(sound_class))
577 return PTR_ERR(sound_class);
578
579 return 0;
580}
581
582module_init(init_soundcore);
583module_exit(cleanup_soundcore);
584