Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / sound / isa / i386 / dev_table.c
1 /*
2  * sound/dev_table.c
3  * 
4  * Device call tables.
5  * 
6  * Copyright by Hannu Savolainen 1993
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer. 2.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  * 
28  * $FreeBSD: src/sys/i386/isa/sound/dev_table.c,v 1.18 1999/12/20 18:04:59 eivind Exp $
29  */
30
31 #define _DEV_TABLE_C_
32 #include <i386/isa/sound/sound_config.h>
33
34 #if NSND > 0
35
36 int             sound_started = 0;
37
38 int             sndtable_get_cardcount(void);
39 int             snd_find_driver(int type);
40 static void     sndtable_init(void);
41 int             sndtable_probe(int unit, struct address_info * hw_config);
42 int             sndtable_init_card(int unit, struct address_info * hw_config);
43 static void     start_services(void);
44 static void     start_cards(void);
45 struct address_info *sound_getconf(int card_type);
46
47 int
48 snd_find_driver(int type)
49 {
50     int   i, n = num_sound_drivers;
51
52     for (i = 0; i < n; i++)
53         if (sound_drivers[i].card_type == type)
54             return i;
55
56     return -1;
57 }
58
59 static void
60 start_services()
61 {
62     int   soundcards_installed;
63
64     if (!(soundcards_installed = sndtable_get_cardcount()))
65         return ;        /* No cards detected */
66
67 #ifdef CONFIG_AUDIO
68     if (num_audiodevs)  /* Audio devices present */
69         DMAbuf_init();
70 #endif
71
72 #ifdef CONFIG_MIDI
73     if (num_midis)
74         /* MIDIbuf_init(0) */;
75 #endif
76
77 #ifdef CONFIG_SEQUENCER
78     if (num_midis + num_synths)
79         sequencer_init();
80 #endif
81 }
82
83 static void
84 start_cards()
85 {
86     int  drv, i, n = num_sound_cards;
87     struct card_info *ci = snd_installed_cards ;
88
89     sound_started = 1;
90     if (trace_init)
91         printf("Sound initialization started\n");
92
93     /*
94      * Check the number of cards actually defined in the table
95      */
96
97     for (i = 0; i < n && snd_installed_cards[i].card_type; i++)
98         num_sound_cards = i + 1;
99
100     for (i = 0; i < n && ci->card_type; ci++, i++)
101         if (ci->enabled) {
102             if ((drv = snd_find_driver(ci->card_type)) == -1) {
103                 ci->enabled = 0;        /* Mark as not detected */
104                 continue;
105             }
106             ci->config.card_subtype = sound_drivers[drv].card_subtype;
107
108             if (sound_drivers[drv].probe(&(ci->config)))
109                 sound_drivers[drv].attach(&(ci->config));
110             else
111                 ci->enabled = 0;        /* Mark as not detected */
112         }
113     if (trace_init)
114         printf("Sound initialization complete\n");
115 }
116
117 static void
118 sndtable_init()
119 {
120     start_cards();
121 }
122
123 /*
124  * sndtable_probe probes a specific device. unit is the voxware unit number.
125  */
126
127 int
128 sndtable_probe(int unit, struct address_info * hw_config)
129 {
130     int  i, sel = -1, n = num_sound_cards;
131     struct card_info *ci = snd_installed_cards ;
132
133     DDB(printf("-- sndtable_probe(%d)\n", unit));
134
135
136     /*
137      * for some reason unit 0 always succeeds ?
138      */
139     if (!unit)
140         return TRUE;
141
142     sound_started = 1;
143
144     for (i=0; i<n && sel== -1 && ci->card_type; ci++, i++)
145         if ( (ci->enabled) && (ci->card_type == unit) ) {
146             /* DDB(printf("-- found card %d\n", i) ); */
147             sel = i; /* and break */
148         }
149
150     /*
151      * not found. Creates a new entry in the table for this unit.
152      */
153     if (sel == -1 && num_sound_cards < max_sound_cards) {
154         int   i;
155
156         i = sel = (num_sound_cards++);
157         DDB(printf("-- installing card %d\n", i) );
158
159         ci = &snd_installed_cards[sel] ;
160         ci->card_type = unit;
161         ci->enabled = 1;
162     }
163     /* DDB(printf("-- installed card %d\n", sel) ); */
164     if (sel != -1) {
165         int   drv;
166
167         ci->config.io_base = hw_config->io_base;
168         ci->config.irq = hw_config->irq;
169         ci->config.dma = hw_config->dma;
170         ci->config.dma2 = hw_config->dma2;
171         ci->config.name = hw_config->name;
172         ci->config.always_detect = hw_config->always_detect;
173         ci->config.card_subtype = hw_config->card_subtype;
174         ci->config.osp = hw_config->osp;
175
176         if ((drv = snd_find_driver(ci->card_type)) == -1) {
177             ci->enabled = 0;
178             DDB(printf("Failed to find driver\n"));
179             return FALSE;
180         }
181         DDB(printf("-- Driver name '%s' probe 0x%08x\n",
182                 sound_drivers[drv].name, sound_drivers[drv].probe));
183
184         hw_config->card_subtype =
185         ci->config.card_subtype = sound_drivers[drv].card_subtype;
186
187         if (sound_drivers[drv].probe(hw_config)) {
188             DDB(printf("-- Hardware probed OK\n"));
189             return TRUE;
190         }
191         DDB(printf("-- Failed to find hardware\n"));
192         ci->enabled = 0;        /* mark as not detected */
193         return FALSE;
194     }
195     return FALSE;
196 }
197
198 int
199 sndtable_init_card(int unit, struct address_info * hw_config)
200 {
201     int     i, n = num_sound_cards;
202     struct card_info *ci = snd_installed_cards ;
203
204     DDB(printf("sndtable_init_card(%d) entered\n", unit));
205
206     if (!unit) {
207         sndtable_init() ;
208         return TRUE;
209     }
210     for (i = 0; i < n && ci->card_type; ci++, i++)
211         if (ci->card_type == unit) {
212             int             drv;
213
214             ci->config.io_base = hw_config->io_base;
215             ci->config.irq = hw_config->irq;
216             ci->config.dma = hw_config->dma;
217             ci->config.dma2 = hw_config->dma2;
218             ci->config.name = hw_config->name;
219             ci->config.always_detect = hw_config->always_detect;
220             ci->config.card_subtype = hw_config->card_subtype;
221             ci->config.osp = hw_config->osp;
222
223             if ((drv = snd_find_driver(ci->card_type)) == -1)
224                 ci->enabled = 0; /* Mark not fnd */
225             else {
226                 DDB(printf("Located card - calling attach routine\n"));
227                 sound_drivers[drv].attach(hw_config) ;
228                 DDB(printf("attach routine finished\n"));
229             }
230             start_services();
231             return TRUE;
232         }
233         DDB(printf("sndtable_init_card: No card defined with type=%d, num cards: %d\n",
234                unit, num_sound_cards));
235     return FALSE;
236 }
237
238 int
239 sndtable_get_cardcount(void)
240 {
241     return num_audiodevs + num_mixers + num_synths + num_midis;
242 }
243
244 struct address_info *
245 sound_getconf(int card_type)
246 {
247     int    j, ptr = -1, n = num_sound_cards;
248
249     for (j = 0; j < n && ptr == -1 && snd_installed_cards[j].card_type; j++)
250         if (snd_installed_cards[j].card_type == card_type)
251             ptr = j;
252
253     if (ptr == -1)
254         return (struct address_info *) NULL;
255
256     return &snd_installed_cards[ptr].config;
257 }
258
259 #endif