1 /*        $NetBSD: sb.c,v 1.93 2021/08/07 16:19:12 thorpej Exp $      */
2 
3 /*
4  * Copyright (c) 1991-1993 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the Computer Systems
18  *        Engineering Group at Lawrence Berkeley Laboratory.
19  * 4. Neither the name of the University nor of the Laboratory may be used
20  *    to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: sb.c,v 1.93 2021/08/07 16:19:12 thorpej Exp $");
39 
40 #include "midi.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
47 #include <sys/device.h>
48 #include <sys/proc.h>
49 
50 #include <sys/cpu.h>
51 #include <sys/intr.h>
52 #include <sys/bus.h>
53 
54 #include <sys/audioio.h>
55 #include <dev/audio/audio_if.h>
56 #include <dev/midi_if.h>
57 
58 #include <dev/isa/isavar.h>
59 #include <dev/isa/isadmavar.h>
60 
61 #include <dev/isa/sbreg.h>
62 #include <dev/isa/sbvar.h>
63 #include <dev/isa/sbdspvar.h>
64 
65 #if NMPU > 0
66 const struct midi_hw_if sb_midi_hw_if = {
67           sbdsp_midi_open,
68           sbdsp_midi_close,
69           sbdsp_midi_output,
70           sbdsp_midi_getinfo,
71           0,                            /* ioctl */
72           sbdsp_get_locks,
73 };
74 #endif
75 
76 int       sb_getdev(void *, struct audio_device *);
77 
78 /*
79  * Define our interface to the higher level audio driver.
80  */
81 
82 const struct audio_hw_if sb_hw_if = {
83           .open                         = sbdsp_open,
84           .close                        = sbdsp_close,
85           .query_format                 = sbdsp_query_format,
86           .set_format                   = sbdsp_set_format,
87           .round_blocksize    = sbdsp_round_blocksize,
88           .halt_output                  = sbdsp_halt_output,
89           .halt_input                   = sbdsp_halt_input,
90           .speaker_ctl                  = sbdsp_speaker_ctl,
91           .getdev                       = sb_getdev,
92           .set_port           = sbdsp_mixer_set_port,
93           .get_port           = sbdsp_mixer_get_port,
94           .query_devinfo                = sbdsp_mixer_query_devinfo,
95           .allocm                       = sb_malloc,
96           .freem                        = sb_free,
97           .round_buffersize   = sb_round_buffersize,
98           .get_props                    = sbdsp_get_props,
99           .trigger_output               = sbdsp_trigger_output,
100           .trigger_input                = sbdsp_trigger_input,
101           .get_locks                    = sbdsp_get_locks,
102 };
103 
104 /*
105  * Probe / attach routines.
106  */
107 
108 
109 int
sbmatch(struct sbdsp_softc * sc,int probing,cfdata_t match)110 sbmatch(struct sbdsp_softc *sc, int probing, cfdata_t match)
111 {
112           static const u_char drq_conf[8] = {
113                     0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
114           };
115 
116           static const u_char irq_conf[11] = {
117                     -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
118           };
119 
120           if (sbdsp_probe(sc, match) == 0)
121                     return 0;
122 
123           /*
124            * Cannot auto-discover DMA channel.
125            */
126           if (ISSBPROCLASS(sc)) {
127                     if (!SBP_DRQ_VALID(sc->sc_drq8)) {
128                               aprint_error("%s: configured DMA chan %d invalid\n",
129                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
130                                   sc->sc_drq8);
131                               return 0;
132                     }
133           } else {
134                     if (!SB_DRQ_VALID(sc->sc_drq8)) {
135                               aprint_error("%s: configured DMA chan %d invalid\n",
136                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
137                                   sc->sc_drq8);
138                               return 0;
139                     }
140           }
141 
142           if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
143                     /*
144                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
145                  * channels.  I've no clue how to use them, so ignore
146                  * one of them for now.  -- augustss@NetBSD.org
147                  */
148                     sc->sc_drq16 = -1;
149 
150           if (ISSB16CLASS(sc)) {
151                     if (sc->sc_drq16 == -1)
152                               sc->sc_drq16 = sc->sc_drq8;
153                     if (!SB16_DRQ_VALID(sc->sc_drq16)) {
154                               aprint_error("%s: configured DMA chan %d invalid\n",
155                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
156                                   sc->sc_drq16);
157                               return 0;
158                     }
159           } else
160                     sc->sc_drq16 = sc->sc_drq8;
161 
162           if (ISSBPROCLASS(sc)) {
163                     if (!SBP_IRQ_VALID(sc->sc_irq)) {
164                               aprint_error("%s: configured irq %d invalid\n",
165                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
166                                   sc->sc_irq);
167                               return 0;
168                     }
169           } else {
170                     if (!SB_IRQ_VALID(sc->sc_irq)) {
171                               aprint_error("%s: configured irq %d invalid\n",
172                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
173                                   sc->sc_irq);
174                               return 0;
175                     }
176           }
177 
178           if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) {
179                     int w, r;
180                     if (sc->sc_irq >= __arraycount(irq_conf)) {
181                               aprint_error("%s: Cannot handle irq %d\n",
182                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
183                                   sc->sc_irq);
184                               return 0;
185                     }
186                     if (sc->sc_drq16 >= __arraycount(drq_conf)) {
187                               aprint_error("%s: Cannot handle drq16 %d\n",
188                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
189                                   sc->sc_drq16);
190                               return 0;
191                     }
192                     if (sc->sc_drq8 >= __arraycount(drq_conf)) {
193                               aprint_error("%s: Cannot handle drq8 %d\n",
194                                   probing ? "sbmatch" : device_xname(sc->sc_dev),
195                                   sc->sc_drq8);
196                               return 0;
197                     }
198 #if 0
199                     printf("%s: old drq conf %02x\n", device_xname(sc->sc_dev),
200                         sbdsp_mix_read(sc, SBP_SET_DRQ));
201                     printf("%s: try drq conf %02x\n", device_xname(sc->sc_dev),
202                         drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
203 #endif
204                     w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
205                     sbdsp_mix_write(sc, SBP_SET_DRQ, w);
206                     r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
207                     if (r != w) {
208                               aprint_error("%s: setting drq mask %02x failed, "
209                                   "got %02x\n",
210                                   probing ? "sbmatch" : device_xname(sc->sc_dev), w,
211                                   r);
212                               return 0;
213                     }
214 #if 0
215                     printf("%s: new drq conf %02x\n", device_xname(sc->sc_dev),
216                         sbdsp_mix_read(sc, SBP_SET_DRQ));
217 #endif
218 
219 #if 0
220                     printf("%s: old irq conf %02x\n", device_xname(sc->sc_dev),
221                         sbdsp_mix_read(sc, SBP_SET_IRQ));
222                     printf("%s: try irq conf %02x\n", device_xname(sc->sc_dev),
223                         irq_conf[sc->sc_irq]);
224 #endif
225                     w = irq_conf[sc->sc_irq];
226                     sbdsp_mix_write(sc, SBP_SET_IRQ, w);
227                     r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
228                     if (r != w) {
229                               aprint_error("%s: setting irq mask %02x failed, "
230                                   "got %02x\n",
231                                   probing ? "sbmatch" : device_xname(sc->sc_dev), w,
232                                   r);
233                               return 0;
234                     }
235 #if 0
236                     printf("%s: new irq conf %02x\n", device_xname(sc->sc_dev),
237                         sbdsp_mix_read(sc, SBP_SET_IRQ));
238 #endif
239           }
240 
241           return 1;
242 }
243 
244 
245 void
sbattach(struct sbdsp_softc * sc)246 sbattach(struct sbdsp_softc *sc)
247 {
248           struct audio_attach_args arg;
249 
250           sbdsp_attach(sc);
251 
252           audio_attach_mi(&sb_hw_if, sc, sc->sc_dev);
253 
254 #if NMPU > 0
255           switch(sc->sc_hasmpu) {
256           default:
257           case SBMPU_NONE:    /* no mpu */
258                     break;
259           case SBMPU_INTERNAL:          /* try to attach midi directly */
260                     midi_attach_mi(&sb_midi_hw_if, sc, sc->sc_dev);
261                     break;
262           case SBMPU_EXTERNAL:          /* search for mpu */
263                     arg.type = AUDIODEV_TYPE_MPU;
264                     arg.hwif = 0;
265                     arg.hdl = 0;
266                     sc->sc_mpudev = config_found(sc->sc_dev, &arg, audioprint,
267                         CFARGS(.iattr = "sbdsp"));
268                     break;
269           }
270 #endif
271           if (sc->sc_model >= SB_20) {
272                     arg.type = AUDIODEV_TYPE_OPL;
273                     arg.hwif = 0;
274                     arg.hdl = 0;
275                     config_found(sc->sc_dev, &arg, audioprint,
276                         CFARGS(.iattr = "sbdsp"));
277           }
278 }
279 
280 /*
281  * Various routines to interface to higher level audio driver
282  */
283 
284 int
sb_getdev(void * addr,struct audio_device * retp)285 sb_getdev(void *addr, struct audio_device *retp)
286 {
287           static const char * const names[] = SB_NAMES;
288           struct sbdsp_softc *sc;
289           const char *config;
290 
291           sc = addr;
292           if (sc->sc_model == SB_JAZZ)
293                     strlcpy(retp->name, "MV Jazz16", sizeof(retp->name));
294           else
295                     strlcpy(retp->name, "SoundBlaster", sizeof(retp->name));
296           snprintf(retp->version, sizeof(retp->version), "%d.%02d",
297               SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version));
298           if (sc->sc_model < sizeof names / sizeof names[0])
299                     config = names[sc->sc_model];
300           else
301                     config = "??";
302           strlcpy(retp->config, config, sizeof(retp->config));
303 
304           return 0;
305 }
306