Import 2.3.1pre2
[davej-history.git] / drivers / sound / dev_table.c
blob71d820b9efe06d88b2fb1cffcb537f6ca8c693f9
1 /*
2 * sound/dev_table.c
4 * Device call tables.
5 */
7 /*
8 * Copyright (C) by Hannu Savolainen 1993-1997
10 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
11 * Version 2 (June 1991). See the "COPYING" file distributed with this software
12 * for more info.
15 #include <linux/config.h>
18 #define _DEV_TABLE_C_
19 #include "sound_config.h"
21 int softoss_dev = 0;
22 int sound_started = 0;
23 int sndtable_get_cardcount(void);
25 int snd_find_driver(int type)
27 int i, n = num_sound_drivers;
29 for (i = 0; i < n; i++)
30 if (sound_drivers[i].card_type == type)
31 return i;
33 return -1;
36 static void start_services(void)
38 #ifdef FIXME
39 int soundcards_installed;
41 if (!(soundcards_installed = sndtable_get_cardcount()))
42 return; /* No cards detected */
43 #endif
45 #ifdef CONFIG_AUDIO
46 if (num_audiodevs) /* Audio devices present */
48 int dev;
49 for (dev = 0; dev < num_audiodevs; dev++)
52 audio_init_devices();
54 #endif
56 return;
59 static void
60 start_cards(void)
62 int i, n = num_sound_cards;
63 int drv;
65 sound_started = 1;
66 if (trace_init)
67 printk(KERN_DEBUG "Sound initialization started\n");
69 #ifdef CONFIG_LOWLEVEL_SOUND
71 extern void sound_preinit_lowlevel_drivers(void);
72 sound_preinit_lowlevel_drivers();
74 #endif
77 * Check the number of cards actually defined in the table
80 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
81 num_sound_cards = i + 1;
83 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
85 if (snd_installed_cards[i].enabled)
87 snd_installed_cards[i].for_driver_use = NULL;
89 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) == -1)
91 snd_installed_cards[i].enabled = 0; /*
92 * Mark as not detected
94 continue;
96 snd_installed_cards[i].config.card_subtype =
97 sound_drivers[drv].card_subtype;
99 if (sound_drivers[drv].probe(&snd_installed_cards[i].config))
100 sound_drivers[drv].attach(&snd_installed_cards[i].config);
101 else
102 snd_installed_cards[i].enabled = 0; /*
103 * Mark as not detected
107 #ifdef CONFIG_LOWLEVEL_SOUND
109 extern void sound_init_lowlevel_drivers(void);
110 sound_init_lowlevel_drivers();
112 #endif
113 if (trace_init)
114 printk(KERN_DEBUG "Sound initialization complete\n");
117 void sndtable_init(void)
119 start_cards();
123 void sound_unload_drivers(void)
125 int i, n = num_sound_cards;
126 int drv;
128 if (!sound_started)
129 return;
131 if (trace_init)
132 printk(KERN_DEBUG "Sound unload started\n");
135 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
137 if (snd_installed_cards[i].enabled)
139 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) != -1)
141 if (sound_drivers[drv].unload)
143 sound_drivers[drv].unload(&snd_installed_cards[i].config);
144 snd_installed_cards[i].enabled = 0;
150 for (i=0;i<num_audiodevs;i++)
151 DMAbuf_deinit(i);
153 if (trace_init)
154 printk(KERN_DEBUG "Sound unload complete\n");
157 void sound_unload_driver(int type)
159 int i, drv = -1, n = num_sound_cards;
162 DEB(printk("unload driver %d: ", type));
164 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
166 if (snd_installed_cards[i].card_type == type)
168 if (snd_installed_cards[i].enabled)
170 if ((drv = snd_find_driver(type)) != -1)
172 DEB(printk(" card %d", i));
173 if (sound_drivers[drv].unload)
175 sound_drivers[drv].unload(&snd_installed_cards[i].config);
176 snd_installed_cards[i].enabled = 0;
182 DEB(printk("\n"));
186 int sndtable_probe(int unit, struct address_info *hw_config)
188 int sel = -1;
190 DEB(printk(KERN_DEBUG "sndtable_probe(%d)\n", unit));
192 if (!unit)
193 return 1;
196 if (sel == -1 && num_sound_cards < max_sound_cards)
198 int i;
199 i = sel = (num_sound_cards++);
200 snd_installed_cards[sel].card_type = unit;
201 snd_installed_cards[sel].enabled = 1;
203 if (sel != -1)
205 int drv;
207 snd_installed_cards[sel].for_driver_use = NULL;
208 snd_installed_cards[sel].config.io_base = hw_config->io_base;
209 snd_installed_cards[sel].config.irq = hw_config->irq;
210 snd_installed_cards[sel].config.dma = hw_config->dma;
211 snd_installed_cards[sel].config.dma2 = hw_config->dma2;
212 snd_installed_cards[sel].config.name = hw_config->name;
213 snd_installed_cards[sel].config.always_detect = hw_config->always_detect;
214 snd_installed_cards[sel].config.driver_use_1 = hw_config->driver_use_1;
215 snd_installed_cards[sel].config.driver_use_2 = hw_config->driver_use_2;
216 snd_installed_cards[sel].config.card_subtype = hw_config->card_subtype;
218 if ((drv = snd_find_driver(snd_installed_cards[sel].card_type)) == -1)
220 snd_installed_cards[sel].enabled = 0;
221 DEB(printk(KERN_DEBUG "Failed to find driver\n"));
222 return 0;
224 DEB(printk(KERN_DEBUG "Driver name '%s'\n", sound_drivers[drv].name));
226 hw_config->card_subtype = snd_installed_cards[sel].config.card_subtype = sound_drivers[drv].card_subtype;
228 if (sound_drivers[drv].probe(hw_config))
230 DEB(printk(KERN_DEBUG "Hardware probed OK\n"));
231 return 1;
233 DEB(printk("Failed to find hardware\n"));
234 snd_installed_cards[sel].enabled = 0; /*
235 * Mark as not detected
237 return 0;
239 return 0;
243 int sndtable_init_card(int unit, struct address_info *hw_config)
245 int i, n = num_sound_cards;
247 DEB(printk("sndtable_init_card(%d) entered\n", unit));
249 if (!unit)
251 sndtable_init();
252 return 1;
254 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
256 if (snd_installed_cards[i].card_type == unit)
258 int drv;
260 snd_installed_cards[i].config.io_base = hw_config->io_base;
261 snd_installed_cards[i].config.irq = hw_config->irq;
262 snd_installed_cards[i].config.dma = hw_config->dma;
263 snd_installed_cards[i].config.dma2 = hw_config->dma2;
264 snd_installed_cards[i].config.name = hw_config->name;
265 snd_installed_cards[i].config.always_detect = hw_config->always_detect;
266 snd_installed_cards[i].config.driver_use_1 = hw_config->driver_use_1;
267 snd_installed_cards[i].config.driver_use_2 = hw_config->driver_use_2;
268 snd_installed_cards[i].config.card_subtype = hw_config->card_subtype;
270 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) == -1)
271 snd_installed_cards[i].enabled = 0; /*
272 * Mark as not detected
274 else
276 DEB(printk(KERN_DEBUG "Located card - calling attach routine\n"));
277 sound_drivers[drv].attach(hw_config);
279 DEB(printk("attach routine finished\n"));
281 start_services();
282 return 1;
285 DEB(printk("sndtable_init_card: No card defined with type=%d, num cards: %d\n", unit, num_sound_cards));
286 return 0;
289 int sndtable_get_cardcount(void)
291 return num_audiodevs + num_mixers + num_synths + num_midis;
294 int sndtable_identify_card(char *name)
296 int i, n = num_sound_drivers;
298 if (name == NULL)
299 return 0;
301 for (i = 0; i < n; i++)
303 if (sound_drivers[i].driver_id != NULL)
305 char *id = sound_drivers[i].driver_id;
306 int j;
308 for (j = 0; j < 80 && name[j] == id[j]; j++)
309 if (id[j] == 0 && name[j] == 0) /* Match */
310 return sound_drivers[i].card_type;
313 return 0;
316 void sound_setup(char *str, int *ints)
318 int i, n = num_sound_cards;
321 * First disable all drivers
324 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
325 snd_installed_cards[i].enabled = 0;
327 if (ints[0] == 0 || ints[1] == 0)
328 return;
330 * Then enable them one by time
333 for (i = 1; i <= ints[0]; i++)
335 int card_type, ioaddr, irq, dma, dma2, ptr, j;
336 unsigned int val;
338 val = (unsigned int) ints[i];
339 card_type = (val & 0x0ff00000) >> 20;
341 if (card_type > 127)
344 * Add any future extensions here
346 return;
348 ioaddr = (val & 0x000fff00) >> 8;
349 irq = (val & 0x000000f0) >> 4;
350 dma = (val & 0x0000000f);
351 dma2 = (val & 0xf0000000) >> 28;
353 ptr = -1;
354 for (j = 0; j < n && ptr == -1; j++)
356 if (snd_installed_cards[j].card_type == card_type &&
357 !snd_installed_cards[j].enabled)/*
358 * Not already found
360 ptr = j;
363 if (ptr == -1)
364 printk(KERN_ERR "Sound: Invalid setup parameter 0x%08x\n", val);
365 else
367 snd_installed_cards[ptr].enabled = 1;
368 snd_installed_cards[ptr].config.io_base = ioaddr;
369 snd_installed_cards[ptr].config.irq = irq;
370 snd_installed_cards[ptr].config.dma = dma;
371 snd_installed_cards[ptr].config.dma2 = dma2;
372 snd_installed_cards[ptr].config.name = NULL;
373 snd_installed_cards[ptr].config.always_detect = 0;
374 snd_installed_cards[ptr].config.driver_use_1 = 0;
375 snd_installed_cards[ptr].config.driver_use_2 = 0;
376 snd_installed_cards[ptr].config.card_subtype = 0;
382 struct address_info * sound_getconf(int card_type)
384 int j, ptr;
385 int n = num_sound_cards;
387 ptr = -1;
388 for (j = 0; j < n && ptr == -1 && snd_installed_cards[j].card_type; j++)
390 if (snd_installed_cards[j].card_type == card_type)
391 ptr = j;
393 if (ptr == -1)
394 return (struct address_info *) NULL;
396 return &snd_installed_cards[ptr].config;
401 int sound_install_audiodrv(int vers, char *name, struct audio_driver *driver,
402 int driver_size, int flags, unsigned int format_mask,
403 void *devc, int dma1, int dma2)
405 #ifdef CONFIG_AUDIO
406 struct audio_driver *d;
407 struct audio_operations *op;
408 int l, num;
410 if (vers != AUDIO_DRIVER_VERSION || driver_size > sizeof(struct audio_driver))
412 printk(KERN_ERR "Sound: Incompatible audio driver for %s\n", name);
413 return -(EINVAL);
415 num = sound_alloc_audiodev();
417 if (num == -1)
419 printk(KERN_ERR "sound: Too many audio drivers\n");
420 return -(EBUSY);
422 d = (struct audio_driver *) (sound_mem_blocks[sound_nblocks] = vmalloc(sizeof(struct audio_driver)));
424 if (sound_nblocks < 1024)
425 sound_nblocks++;
427 op = (struct audio_operations *) (sound_mem_blocks[sound_nblocks] = vmalloc(sizeof(struct audio_operations)));
429 if (sound_nblocks < 1024)
430 sound_nblocks++;
431 if (d == NULL || op == NULL)
433 printk(KERN_ERR "Sound: Can't allocate driver for (%s)\n", name);
434 sound_unload_audiodev(num);
435 return -(ENOMEM);
437 memset((char *) op, 0, sizeof(struct audio_operations));
438 init_waitqueue_head(&op->in_sleeper);
439 init_waitqueue_head(&op->out_sleeper);
440 init_waitqueue_head(&op->poll_sleeper);
441 if (driver_size < sizeof(struct audio_driver))
442 memset((char *) d, 0, sizeof(struct audio_driver));
444 memcpy((char *) d, (char *) driver, driver_size);
446 op->d = d;
447 l = strlen(name) + 1;
448 if (l > sizeof(op->name))
449 l = sizeof(op->name);
450 strncpy(op->name, name, l);
451 op->name[l - 1] = 0;
452 op->flags = flags;
453 op->format_mask = format_mask;
454 op->devc = devc;
457 * Hardcoded defaults
459 audio_devs[num] = op;
461 DMAbuf_init(num, dma1, dma2);
463 audio_init_devices();
464 return num;
465 #else
466 return -EINVAL;
467 #endif
470 int sound_install_mixer(int vers, char *name, struct mixer_operations *driver,
471 int driver_size, void *devc)
473 struct mixer_operations *op;
474 int l;
476 int n = sound_alloc_mixerdev();
478 if (n == -1)
480 printk(KERN_ERR "Sound: Too many mixer drivers\n");
481 return -EBUSY;
483 if (vers != MIXER_DRIVER_VERSION ||
484 driver_size > sizeof(struct mixer_operations))
486 printk(KERN_ERR "Sound: Incompatible mixer driver for %s\n", name);
487 return -EINVAL;
490 /* FIXME: This leaks a mixer_operations struct every time its called
491 until you unload sound! */
493 op = (struct mixer_operations *) (sound_mem_blocks[sound_nblocks] = vmalloc(sizeof(struct mixer_operations)));
495 if (sound_nblocks < 1024)
496 sound_nblocks++;
497 if (op == NULL)
499 printk(KERN_ERR "Sound: Can't allocate mixer driver for (%s)\n", name);
500 return -ENOMEM;
502 memset((char *) op, 0, sizeof(struct mixer_operations));
503 memcpy((char *) op, (char *) driver, driver_size);
505 l = strlen(name) + 1;
506 if (l > sizeof(op->name))
507 l = sizeof(op->name);
508 strncpy(op->name, name, l);
509 op->name[l - 1] = 0;
510 op->devc = devc;
512 mixer_devs[n] = op;
513 return n;
516 void sound_unload_audiodev(int dev)
518 if (dev != -1)
520 DMAbuf_deinit(dev);
521 audio_devs[dev] = NULL;
522 unregister_sound_dsp((dev<<4)+3);
526 int sound_alloc_audiodev(void)
528 int i = register_sound_dsp(&oss_sound_fops, -1);
529 if(i==-1)
530 return i;
531 i>>=4;
532 if(i>=num_audiodevs)
533 num_audiodevs = i + 1;
534 return i;
537 int sound_alloc_mididev(void)
539 #ifdef CONFIG_MIDI
540 int i = register_sound_midi(&oss_sound_fops, -1);
541 if(i==-1)
542 return i;
543 i>>=4;
544 if(i>=num_midis)
545 num_midis = i + 1;
546 return i;
547 #else
548 return (-1);
549 #endif
552 int sound_alloc_synthdev(void)
554 int i;
556 for (i = 0; i < MAX_SYNTH_DEV; i++)
558 if (synth_devs[i] == NULL)
560 if (i >= num_synths)
561 num_synths++;
562 return i;
565 return -1;
568 int sound_alloc_mixerdev(void)
570 int i = register_sound_mixer(&oss_sound_fops, -1);
571 if(i==-1)
572 return -1;
573 i>>=4;
574 if(i>=num_mixers)
575 num_mixers = i + 1;
576 return i;
579 int sound_alloc_timerdev(void)
581 int i;
583 for (i = 0; i < MAX_TIMER_DEV; i++)
585 if (sound_timer_devs[i] == NULL)
587 if (i >= num_sound_timers)
588 num_sound_timers++;
589 return i;
592 return -1;
595 void sound_unload_mixerdev(int dev)
597 if (dev != -1)
599 mixer_devs[dev] = NULL;
600 unregister_sound_mixer(dev<<4);
604 void sound_unload_mididev(int dev)
606 #ifdef CONFIG_MIDI
607 if (dev != -1)
609 midi_devs[dev] = NULL;
610 unregister_sound_midi((dev<<4)+2);
612 #endif
615 void sound_unload_synthdev(int dev)
617 if (dev != -1)
618 synth_devs[dev] = NULL;
621 void sound_unload_timerdev(int dev)
623 if (dev != -1)
624 sound_timer_devs[dev] = NULL;