Import 2.1.81
[davej-history.git] / drivers / sound / dev_table.c
blob63dacee8e5d9d8b973fc168dd6870d0c018fa1e4
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 sb_be_quiet = 0;
22 int softoss_dev = 0;
23 int sound_started = 0;
24 int sndtable_get_cardcount(void);
26 int snd_find_driver(int type)
28 int i, n = num_sound_drivers;
30 for (i = 0; i < n; i++)
31 if (sound_drivers[i].card_type == type)
32 return i;
34 return -1;
37 static void start_services(void)
39 #ifdef FIXME
40 int soundcards_installed;
42 if (!(soundcards_installed = sndtable_get_cardcount()))
43 return; /* No cards detected */
44 #endif
46 #ifdef CONFIG_AUDIO
47 if (num_audiodevs) /* Audio devices present */
49 int dev;
50 for (dev = 0; dev < num_audiodevs; dev++)
53 audio_init_devices();
55 #endif
57 return;
60 static void
61 start_cards(void)
63 int i, n = num_sound_cards;
64 int drv;
66 sound_started = 1;
67 if (trace_init)
68 printk(KERN_DEBUG "Sound initialization started\n");
70 #ifdef CONFIG_LOWLEVEL_SOUND
72 extern void sound_preinit_lowlevel_drivers(void);
73 sound_preinit_lowlevel_drivers();
75 #endif
78 * Check the number of cards actually defined in the table
81 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
82 num_sound_cards = i + 1;
84 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
86 if (snd_installed_cards[i].enabled)
88 snd_installed_cards[i].for_driver_use = NULL;
90 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) == -1)
92 snd_installed_cards[i].enabled = 0; /*
93 * Mark as not detected
95 continue;
97 snd_installed_cards[i].config.card_subtype =
98 sound_drivers[drv].card_subtype;
100 if (sound_drivers[drv].probe(&snd_installed_cards[i].config))
101 sound_drivers[drv].attach(&snd_installed_cards[i].config);
102 else
103 snd_installed_cards[i].enabled = 0; /*
104 * Mark as not detected
108 #ifdef CONFIG_LOWLEVEL_SOUND
110 extern void sound_init_lowlevel_drivers(void);
111 sound_init_lowlevel_drivers();
113 #endif
114 if (trace_init)
115 printk(KERN_DEBUG "Sound initialization complete\n");
118 void sndtable_init(void)
120 start_cards();
124 void sound_unload_drivers(void)
126 int i, n = num_sound_cards;
127 int drv;
129 if (!sound_started)
130 return;
132 if (trace_init)
133 printk(KERN_DEBUG "Sound unload started\n");
136 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
138 if (snd_installed_cards[i].enabled)
140 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) != -1)
142 if (sound_drivers[drv].unload)
144 sound_drivers[drv].unload(&snd_installed_cards[i].config);
145 snd_installed_cards[i].enabled = 0;
151 for (i=0;i<num_audiodevs;i++)
152 DMAbuf_deinit(i);
154 if (trace_init)
155 printk(KERN_DEBUG "Sound unload complete\n");
158 void sound_unload_driver(int type)
160 int i, drv = -1, n = num_sound_cards;
163 DEB(printk("unload driver %d: ", type));
165 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
167 if (snd_installed_cards[i].card_type == type)
169 if (snd_installed_cards[i].enabled)
171 if ((drv = snd_find_driver(type)) != -1)
173 DEB(printk(" card %d", i));
174 if (sound_drivers[drv].unload)
176 sound_drivers[drv].unload(&snd_installed_cards[i].config);
177 snd_installed_cards[i].enabled = 0;
183 DEB(printk("\n"));
187 int sndtable_probe(int unit, struct address_info *hw_config)
189 int sel = -1;
191 DEB(printk(KERN_DEBUG "sndtable_probe(%d)\n", unit));
193 if (!unit)
194 return 1;
197 if (sel == -1 && num_sound_cards < max_sound_cards)
199 int i;
200 i = sel = (num_sound_cards++);
201 snd_installed_cards[sel].card_type = unit;
202 snd_installed_cards[sel].enabled = 1;
204 if (sel != -1)
206 int drv;
208 snd_installed_cards[sel].for_driver_use = NULL;
209 snd_installed_cards[sel].config.io_base = hw_config->io_base;
210 snd_installed_cards[sel].config.irq = hw_config->irq;
211 snd_installed_cards[sel].config.dma = hw_config->dma;
212 snd_installed_cards[sel].config.dma2 = hw_config->dma2;
213 snd_installed_cards[sel].config.name = hw_config->name;
214 snd_installed_cards[sel].config.always_detect = hw_config->always_detect;
215 snd_installed_cards[sel].config.driver_use_1 = hw_config->driver_use_1;
216 snd_installed_cards[sel].config.driver_use_2 = hw_config->driver_use_2;
217 snd_installed_cards[sel].config.card_subtype = hw_config->card_subtype;
219 if ((drv = snd_find_driver(snd_installed_cards[sel].card_type)) == -1)
221 snd_installed_cards[sel].enabled = 0;
222 DEB(printk(KERN_DEBUG "Failed to find driver\n"));
223 return 0;
225 DEB(printk(KERN_DEBUG "Driver name '%s'\n", sound_drivers[drv].name));
227 hw_config->card_subtype = snd_installed_cards[sel].config.card_subtype = sound_drivers[drv].card_subtype;
229 if (sound_drivers[drv].probe(hw_config))
231 DEB(printk(KERN_DEBUG "Hardware probed OK\n"));
232 return 1;
234 DEB(printk("Failed to find hardware\n"));
235 snd_installed_cards[sel].enabled = 0; /*
236 * Mark as not detected
238 return 0;
240 return 0;
244 int sndtable_init_card(int unit, struct address_info *hw_config)
246 int i, n = num_sound_cards;
248 DEB(printk("sndtable_init_card(%d) entered\n", unit));
250 if (!unit)
252 sndtable_init();
253 return 1;
255 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
257 if (snd_installed_cards[i].card_type == unit)
259 int drv;
261 snd_installed_cards[i].config.io_base = hw_config->io_base;
262 snd_installed_cards[i].config.irq = hw_config->irq;
263 snd_installed_cards[i].config.dma = hw_config->dma;
264 snd_installed_cards[i].config.dma2 = hw_config->dma2;
265 snd_installed_cards[i].config.name = hw_config->name;
266 snd_installed_cards[i].config.always_detect = hw_config->always_detect;
267 snd_installed_cards[i].config.driver_use_1 = hw_config->driver_use_1;
268 snd_installed_cards[i].config.driver_use_2 = hw_config->driver_use_2;
269 snd_installed_cards[i].config.card_subtype = hw_config->card_subtype;
271 if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) == -1)
272 snd_installed_cards[i].enabled = 0; /*
273 * Mark as not detected
275 else
277 DEB(printk(KERN_DEBUG "Located card - calling attach routine\n"));
278 sound_drivers[drv].attach(hw_config);
280 DEB(printk("attach routine finished\n"));
282 start_services();
283 return 1;
286 DEB(printk("sndtable_init_card: No card defined with type=%d, num cards: %d\n", unit, num_sound_cards));
287 return 0;
290 int sndtable_get_cardcount(void)
292 return num_audiodevs + num_mixers + num_synths + num_midis;
295 int sndtable_identify_card(char *name)
297 int i, n = num_sound_drivers;
299 if (name == NULL)
300 return 0;
302 for (i = 0; i < n; i++)
304 if (sound_drivers[i].driver_id != NULL)
306 char *id = sound_drivers[i].driver_id;
307 int j;
309 for (j = 0; j < 80 && name[j] == id[j]; j++)
310 if (id[j] == 0 && name[j] == 0) /* Match */
311 return sound_drivers[i].card_type;
314 return 0;
317 void sound_setup(char *str, int *ints)
319 int i, n = num_sound_cards;
322 * First disable all drivers
325 for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
326 snd_installed_cards[i].enabled = 0;
328 if (ints[0] == 0 || ints[1] == 0)
329 return;
331 * Then enable them one by time
334 for (i = 1; i <= ints[0]; i++)
336 int card_type, ioaddr, irq, dma, dma2, ptr, j;
337 unsigned int val;
339 val = (unsigned int) ints[i];
340 card_type = (val & 0x0ff00000) >> 20;
342 if (card_type > 127)
345 * Add any future extensions here
347 return;
349 ioaddr = (val & 0x000fff00) >> 8;
350 irq = (val & 0x000000f0) >> 4;
351 dma = (val & 0x0000000f);
352 dma2 = (val & 0xf0000000) >> 28;
354 ptr = -1;
355 for (j = 0; j < n && ptr == -1; j++)
357 if (snd_installed_cards[j].card_type == card_type &&
358 !snd_installed_cards[j].enabled)/*
359 * Not already found
361 ptr = j;
364 if (ptr == -1)
365 printk(KERN_ERR "Sound: Invalid setup parameter 0x%08x\n", val);
366 else
368 snd_installed_cards[ptr].enabled = 1;
369 snd_installed_cards[ptr].config.io_base = ioaddr;
370 snd_installed_cards[ptr].config.irq = irq;
371 snd_installed_cards[ptr].config.dma = dma;
372 snd_installed_cards[ptr].config.dma2 = dma2;
373 snd_installed_cards[ptr].config.name = NULL;
374 snd_installed_cards[ptr].config.always_detect = 0;
375 snd_installed_cards[ptr].config.driver_use_1 = 0;
376 snd_installed_cards[ptr].config.driver_use_2 = 0;
377 snd_installed_cards[ptr].config.card_subtype = 0;
383 struct address_info * sound_getconf(int card_type)
385 int j, ptr;
386 int n = num_sound_cards;
388 ptr = -1;
389 for (j = 0; j < n && ptr == -1 && snd_installed_cards[j].card_type; j++)
391 if (snd_installed_cards[j].card_type == card_type)
392 ptr = j;
394 if (ptr == -1)
395 return (struct address_info *) NULL;
397 return &snd_installed_cards[ptr].config;
402 int sound_install_audiodrv(int vers, char *name, struct audio_driver *driver,
403 int driver_size, int flags, unsigned int format_mask,
404 void *devc, int dma1, int dma2)
406 #ifdef CONFIG_AUDIO
407 struct audio_driver *d;
408 struct audio_operations *op;
409 int l, num;
411 if (vers != AUDIO_DRIVER_VERSION || driver_size > sizeof(struct audio_driver))
413 printk(KERN_ERR "Sound: Incompatible audio driver for %s\n", name);
414 return -(EINVAL);
416 num = sound_alloc_audiodev();
418 if (num == -1)
420 printk(KERN_ERR "sound: Too many audio drivers\n");
421 return -(EBUSY);
423 d = (struct audio_driver *) (sound_mem_blocks[sound_nblocks] = vmalloc(sizeof(struct audio_driver)));
424 sound_mem_sizes[sound_nblocks] = sizeof(struct audio_driver);
426 if (sound_nblocks < 1024)
427 sound_nblocks++;
429 op = (struct audio_operations *) (sound_mem_blocks[sound_nblocks] = vmalloc(sizeof(struct audio_operations)));
430 sound_mem_sizes[sound_nblocks] = sizeof(struct audio_operations);
432 if (sound_nblocks < 1024)
433 sound_nblocks++;
434 if (d == NULL || op == NULL)
436 printk(KERN_ERR "Sound: Can't allocate driver for (%s)\n", name);
437 sound_unload_audiodev(num);
438 return -(ENOMEM);
440 memset((char *) op, 0, sizeof(struct audio_operations));
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)));
494 sound_mem_sizes[sound_nblocks] = sizeof(struct mixer_operations);
496 if (sound_nblocks < 1024)
497 sound_nblocks++;
498 if (op == NULL)
500 printk(KERN_ERR "Sound: Can't allocate mixer driver for (%s)\n", name);
501 return -ENOMEM;
503 memset((char *) op, 0, sizeof(struct mixer_operations));
504 memcpy((char *) op, (char *) driver, driver_size);
506 l = strlen(name) + 1;
507 if (l > sizeof(op->name))
508 l = sizeof(op->name);
509 strncpy(op->name, name, l);
510 op->name[l - 1] = 0;
511 op->devc = devc;
513 mixer_devs[n] = op;
514 return n;
517 void sound_unload_audiodev(int dev)
519 if (dev != -1)
520 audio_devs[dev] = NULL;
523 int sound_alloc_audiodev(void)
525 int i;
527 for (i = 0; i < MAX_AUDIO_DEV; i++)
529 if (audio_devs[i] == NULL)
531 if (i >= num_audiodevs)
532 num_audiodevs = i + 1;
533 return i;
536 return -1;
539 int sound_alloc_mididev(void)
541 int i;
543 for (i = 0; i < MAX_MIDI_DEV; i++)
545 if (midi_devs[i] == NULL)
547 if (i >= num_midis)
548 num_midis++;
549 return i;
552 return -1;
555 int sound_alloc_synthdev(void)
557 int i;
559 for (i = 0; i < MAX_SYNTH_DEV; i++)
561 if (synth_devs[i] == NULL)
563 if (i >= num_synths)
564 num_synths++;
565 return i;
568 return -1;
571 int sound_alloc_mixerdev(void)
573 int i;
575 for (i = 0; i < MAX_MIXER_DEV; i++)
577 if (mixer_devs[i] == NULL)
579 if (i >= num_mixers)
580 num_mixers++;
581 return i;
584 return -1;
587 int sound_alloc_timerdev(void)
589 int i;
591 for (i = 0; i < MAX_TIMER_DEV; i++)
593 if (sound_timer_devs[i] == NULL)
595 if (i >= num_sound_timers)
596 num_sound_timers++;
597 return i;
600 return -1;
603 void sound_unload_mixerdev(int dev)
605 if (dev != -1)
606 mixer_devs[dev] = NULL;
609 void sound_unload_mididev(int dev)
611 #ifdef CONFIG_MIDI
612 if (dev != -1)
613 midi_devs[dev] = NULL;
614 #endif
617 void sound_unload_synthdev(int dev)
619 if (dev != -1)
620 synth_devs[dev] = NULL;
623 void sound_unload_timerdev(int dev)
625 if (dev != -1)
626 sound_timer_devs[dev] = NULL;