eso.c revision 1.39 1 /* $NetBSD: eso.c,v 1.39 2005/01/15 15:19:52 kent Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000, 2004 Klaus J. Klein
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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * ESS Technology Inc. Solo-1 PCI AudioDrive (ES1938/1946) device driver.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: eso.c,v 1.39 2005/01/15 15:19:52 kent Exp $");
37
38 #include "mpu.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46
47 #include <dev/pci/pcidevs.h>
48 #include <dev/pci/pcivar.h>
49
50 #include <sys/audioio.h>
51 #include <dev/audio_if.h>
52 #include <dev/midi_if.h>
53
54 #include <dev/mulaw.h>
55 #include <dev/auconv.h>
56
57 #include <dev/ic/mpuvar.h>
58 #include <dev/ic/i8237reg.h>
59 #include <dev/pci/esoreg.h>
60 #include <dev/pci/esovar.h>
61
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64
65 /*
66 * XXX Work around the 24-bit implementation limit of the Audio 1 DMA
67 * XXX engine by allocating through the ISA DMA tag.
68 */
69 #if defined(amd64) || defined(i386)
70 #include "isa.h"
71 #if NISA > 0
72 #include <dev/isa/isavar.h>
73 #endif
74 #endif
75
76 #if defined(AUDIO_DEBUG) || defined(DEBUG)
77 #define DPRINTF(x) printf x
78 #else
79 #define DPRINTF(x)
80 #endif
81
82 struct eso_dma {
83 bus_dma_tag_t ed_dmat;
84 bus_dmamap_t ed_map;
85 caddr_t ed_addr;
86 bus_dma_segment_t ed_segs[1];
87 int ed_nsegs;
88 size_t ed_size;
89 struct eso_dma * ed_next;
90 };
91
92 #define KVADDR(dma) ((void *)(dma)->ed_addr)
93 #define DMAADDR(dma) ((dma)->ed_map->dm_segs[0].ds_addr)
94
95 /* Autoconfiguration interface */
96 static int eso_match(struct device *, struct cfdata *, void *);
97 static void eso_attach(struct device *, struct device *, void *);
98 static void eso_defer(struct device *);
99 static int eso_print(void *, const char *);
100
101 CFATTACH_DECL(eso, sizeof (struct eso_softc),
102 eso_match, eso_attach, NULL, NULL);
103
104 /* PCI interface */
105 static int eso_intr(void *);
106
107 /* MI audio layer interface */
108 static int eso_query_encoding(void *, struct audio_encoding *);
109 static int eso_set_params(void *, int, int, audio_params_t *,
110 audio_params_t *, stream_filter_list_t *,
111 stream_filter_list_t *);
112 static int eso_round_blocksize(void *, int, int, const audio_params_t *);
113 static int eso_halt_output(void *);
114 static int eso_halt_input(void *);
115 static int eso_getdev(void *, struct audio_device *);
116 static int eso_set_port(void *, mixer_ctrl_t *);
117 static int eso_get_port(void *, mixer_ctrl_t *);
118 static int eso_query_devinfo(void *, mixer_devinfo_t *);
119 static void * eso_allocm(void *, int, size_t, struct malloc_type *, int);
120 static void eso_freem(void *, void *, struct malloc_type *);
121 static size_t eso_round_buffersize(void *, int, size_t);
122 static paddr_t eso_mappage(void *, void *, off_t, int);
123 static int eso_get_props(void *);
124 static int eso_trigger_output(void *, void *, void *, int,
125 void (*)(void *), void *, const audio_params_t *);
126 static int eso_trigger_input(void *, void *, void *, int,
127 void (*)(void *), void *, const audio_params_t *);
128
129 static const struct audio_hw_if eso_hw_if = {
130 NULL, /* open */
131 NULL, /* close */
132 NULL, /* drain */
133 eso_query_encoding,
134 eso_set_params,
135 eso_round_blocksize,
136 NULL, /* commit_settings */
137 NULL, /* init_output */
138 NULL, /* init_input */
139 NULL, /* start_output */
140 NULL, /* start_input */
141 eso_halt_output,
142 eso_halt_input,
143 NULL, /* speaker_ctl */
144 eso_getdev,
145 NULL, /* setfd */
146 eso_set_port,
147 eso_get_port,
148 eso_query_devinfo,
149 eso_allocm,
150 eso_freem,
151 eso_round_buffersize,
152 eso_mappage,
153 eso_get_props,
154 eso_trigger_output,
155 eso_trigger_input,
156 NULL, /* dev_ioctl */
157 };
158
159 static const char * const eso_rev2model[] = {
160 "ES1938",
161 "ES1946",
162 "ES1946 Revision E"
163 };
164
165 #define ESO_NFORMATS 12
166 static const struct audio_format eso_formats[ESO_NFORMATS] = {
167 {NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
168 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
169 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
170 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
171 {NULL, AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
172 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
173 {NULL, AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_BE, 16, 16,
174 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
175 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
176 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
177 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
178 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
179 {NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
180 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
181 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
182 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
183 {NULL, AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
184 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
185 {NULL, AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_BE, 16, 16,
186 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
187 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
188 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
189 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
190 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
191 };
192
193
194 /*
195 * Utility routines
196 */
197 /* Register access etc. */
198 static uint8_t eso_read_ctlreg(struct eso_softc *, uint8_t);
199 static uint8_t eso_read_mixreg(struct eso_softc *, uint8_t);
200 static uint8_t eso_read_rdr(struct eso_softc *);
201 static void eso_reload_master_vol(struct eso_softc *);
202 static int eso_reset(struct eso_softc *);
203 static void eso_set_gain(struct eso_softc *, unsigned int);
204 static int eso_set_recsrc(struct eso_softc *, unsigned int);
205 static int eso_set_monooutsrc(struct eso_softc *, unsigned int);
206 static int eso_set_monoinbypass(struct eso_softc *, unsigned int);
207 static int eso_set_preamp(struct eso_softc *, unsigned int);
208 static void eso_write_cmd(struct eso_softc *, uint8_t);
209 static void eso_write_ctlreg(struct eso_softc *, uint8_t, uint8_t);
210 static void eso_write_mixreg(struct eso_softc *, uint8_t, uint8_t);
211 /* DMA memory allocation */
212 static int eso_allocmem(struct eso_softc *, size_t, size_t, size_t,
213 int, int, struct eso_dma *);
214 static void eso_freemem(struct eso_dma *);
215
216
217 static int
218 eso_match(struct device *parent, struct cfdata *match, void *aux)
219 {
220 struct pci_attach_args *pa;
221
222 pa = aux;
223 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ESSTECH &&
224 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ESSTECH_SOLO1)
225 return 1;
226
227 return 0;
228 }
229
230 static void
231 eso_attach(struct device *parent, struct device *self, void *aux)
232 {
233 struct eso_softc *sc;
234 struct pci_attach_args *pa;
235 struct audio_attach_args aa;
236 pci_intr_handle_t ih;
237 bus_addr_t vcbase;
238 const char *intrstring;
239 int idx;
240 uint8_t a2mode, mvctl;
241
242 sc = (struct eso_softc *)self;
243 pa = aux;
244 aprint_naive(": Audio controller\n");
245
246 sc->sc_revision = PCI_REVISION(pa->pa_class);
247
248 aprint_normal(": ESS Solo-1 PCI AudioDrive ");
249 if (sc->sc_revision <
250 sizeof (eso_rev2model) / sizeof (eso_rev2model[0]))
251 aprint_normal("%s\n", eso_rev2model[sc->sc_revision]);
252 else
253 aprint_normal("(unknown rev. 0x%02x)\n", sc->sc_revision);
254
255 /* Map I/O registers. */
256 if (pci_mapreg_map(pa, ESO_PCI_BAR_IO, PCI_MAPREG_TYPE_IO, 0,
257 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
258 aprint_error("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
259 return;
260 }
261 if (pci_mapreg_map(pa, ESO_PCI_BAR_SB, PCI_MAPREG_TYPE_IO, 0,
262 &sc->sc_sb_iot, &sc->sc_sb_ioh, NULL, NULL)) {
263 aprint_error("%s: can't map SB I/O space\n",
264 sc->sc_dev.dv_xname);
265 return;
266 }
267 if (pci_mapreg_map(pa, ESO_PCI_BAR_VC, PCI_MAPREG_TYPE_IO, 0,
268 &sc->sc_dmac_iot, &sc->sc_dmac_ioh, &vcbase, &sc->sc_vcsize)) {
269 aprint_error("%s: can't map VC I/O space\n",
270 sc->sc_dev.dv_xname);
271 /* Don't bail out yet: we can map it later, see below. */
272 vcbase = 0;
273 sc->sc_vcsize = 0x10; /* From the data sheet. */
274 }
275 if (pci_mapreg_map(pa, ESO_PCI_BAR_MPU, PCI_MAPREG_TYPE_IO, 0,
276 &sc->sc_mpu_iot, &sc->sc_mpu_ioh, NULL, NULL)) {
277 aprint_error("%s: can't map MPU I/O space\n",
278 sc->sc_dev.dv_xname);
279 return;
280 }
281 if (pci_mapreg_map(pa, ESO_PCI_BAR_GAME, PCI_MAPREG_TYPE_IO, 0,
282 &sc->sc_game_iot, &sc->sc_game_ioh, NULL, NULL)) {
283 aprint_error("%s: can't map Game I/O space\n",
284 sc->sc_dev.dv_xname);
285 return;
286 }
287
288 sc->sc_dmat = pa->pa_dmat;
289 sc->sc_dmas = NULL;
290 sc->sc_dmac_configured = 0;
291
292 /* Enable bus mastering. */
293 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
294 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
295 PCI_COMMAND_MASTER_ENABLE);
296
297 /* Reset the device; bail out upon failure. */
298 if (eso_reset(sc) != 0) {
299 aprint_error("%s: can't reset\n", sc->sc_dev.dv_xname);
300 return;
301 }
302
303 /* Select the DMA/IRQ policy: DDMA, ISA IRQ emulation disabled. */
304 pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_S1C,
305 pci_conf_read(pa->pa_pc, pa->pa_tag, ESO_PCI_S1C) &
306 ~(ESO_PCI_S1C_IRQP_MASK | ESO_PCI_S1C_DMAP_MASK));
307
308 /* Enable the relevant (DMA) interrupts. */
309 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL,
310 ESO_IO_IRQCTL_A1IRQ | ESO_IO_IRQCTL_A2IRQ | ESO_IO_IRQCTL_HVIRQ |
311 ESO_IO_IRQCTL_MPUIRQ);
312
313 /* Set up A1's sample rate generator for new-style parameters. */
314 a2mode = eso_read_mixreg(sc, ESO_MIXREG_A2MODE);
315 a2mode |= ESO_MIXREG_A2MODE_NEWA1 | ESO_MIXREG_A2MODE_ASYNC;
316 eso_write_mixreg(sc, ESO_MIXREG_A2MODE, a2mode);
317
318 /* Slave Master Volume to Hardware Volume Control Counter, unmask IRQ.*/
319 mvctl = eso_read_mixreg(sc, ESO_MIXREG_MVCTL);
320 mvctl &= ~ESO_MIXREG_MVCTL_SPLIT;
321 mvctl |= ESO_MIXREG_MVCTL_HVIRQM;
322 eso_write_mixreg(sc, ESO_MIXREG_MVCTL, mvctl);
323
324 /* Set mixer regs to something reasonable, needs work. */
325 sc->sc_recmon = sc->sc_spatializer = sc->sc_mvmute = 0;
326 eso_set_monooutsrc(sc, ESO_MIXREG_MPM_MOMUTE);
327 eso_set_monoinbypass(sc, 0);
328 eso_set_preamp(sc, 1);
329 for (idx = 0; idx < ESO_NGAINDEVS; idx++) {
330 int v;
331
332 switch (idx) {
333 case ESO_MIC_PLAY_VOL:
334 case ESO_LINE_PLAY_VOL:
335 case ESO_CD_PLAY_VOL:
336 case ESO_MONO_PLAY_VOL:
337 case ESO_AUXB_PLAY_VOL:
338 case ESO_DAC_REC_VOL:
339 case ESO_LINE_REC_VOL:
340 case ESO_SYNTH_REC_VOL:
341 case ESO_CD_REC_VOL:
342 case ESO_MONO_REC_VOL:
343 case ESO_AUXB_REC_VOL:
344 case ESO_SPATIALIZER:
345 v = 0;
346 break;
347 case ESO_MASTER_VOL:
348 v = ESO_GAIN_TO_6BIT(AUDIO_MAX_GAIN / 2);
349 break;
350 default:
351 v = ESO_GAIN_TO_4BIT(AUDIO_MAX_GAIN / 2);
352 break;
353 }
354 sc->sc_gain[idx][ESO_LEFT] = sc->sc_gain[idx][ESO_RIGHT] = v;
355 eso_set_gain(sc, idx);
356 }
357 eso_set_recsrc(sc, ESO_MIXREG_ERS_MIC);
358
359 /* Map and establish the interrupt. */
360 if (pci_intr_map(pa, &ih)) {
361 aprint_error("%s: couldn't map interrupt\n",
362 sc->sc_dev.dv_xname);
363 return;
364 }
365 intrstring = pci_intr_string(pa->pa_pc, ih);
366 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, eso_intr, sc);
367 if (sc->sc_ih == NULL) {
368 aprint_error("%s: couldn't establish interrupt",
369 sc->sc_dev.dv_xname);
370 if (intrstring != NULL)
371 aprint_normal(" at %s", intrstring);
372 aprint_normal("\n");
373 return;
374 }
375 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
376 intrstring);
377
378 /*
379 * Set up the DDMA Control register; a suitable I/O region has been
380 * supposedly mapped in the VC base address register.
381 *
382 * The Solo-1 has an ... interesting silicon bug that causes it to
383 * not respond to I/O space accesses to the Audio 1 DMA controller
384 * if the latter's mapping base address is aligned on a 1K boundary.
385 * As a consequence, it is quite possible for the mapping provided
386 * in the VC BAR to be useless. To work around this, we defer this
387 * part until all autoconfiguration on our parent bus is completed
388 * and then try to map it ourselves in fulfillment of the constraint.
389 *
390 * According to the register map we may write to the low 16 bits
391 * only, but experimenting has shown we're safe.
392 * -kjk
393 */
394 if (ESO_VALID_DDMAC_BASE(vcbase)) {
395 pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_DDMAC,
396 vcbase | ESO_PCI_DDMAC_DE);
397 sc->sc_dmac_configured = 1;
398
399 aprint_normal(
400 "%s: mapping Audio 1 DMA using VC I/O space at 0x%lx\n",
401 sc->sc_dev.dv_xname, (unsigned long)vcbase);
402 } else {
403 DPRINTF(("%s: VC I/O space at 0x%lx not suitable, deferring\n",
404 sc->sc_dev.dv_xname, (unsigned long)vcbase));
405 sc->sc_pa = *pa;
406 config_defer(self, eso_defer);
407 }
408
409 audio_attach_mi(&eso_hw_if, sc, &sc->sc_dev);
410
411 aa.type = AUDIODEV_TYPE_OPL;
412 aa.hwif = NULL;
413 aa.hdl = NULL;
414 (void)config_found(&sc->sc_dev, &aa, audioprint);
415
416 aa.type = AUDIODEV_TYPE_MPU;
417 aa.hwif = NULL;
418 aa.hdl = NULL;
419 sc->sc_mpudev = config_found(&sc->sc_dev, &aa, audioprint);
420 if (sc->sc_mpudev != NULL) {
421 /* Unmask the MPU irq. */
422 mvctl = eso_read_mixreg(sc, ESO_MIXREG_MVCTL);
423 mvctl |= ESO_MIXREG_MVCTL_MPUIRQM;
424 eso_write_mixreg(sc, ESO_MIXREG_MVCTL, mvctl);
425 }
426
427 aa.type = AUDIODEV_TYPE_AUX;
428 aa.hwif = NULL;
429 aa.hdl = NULL;
430 (void)config_found(&sc->sc_dev, &aa, eso_print);
431 }
432
433 static void
434 eso_defer(struct device *self)
435 {
436 struct eso_softc *sc;
437 struct pci_attach_args *pa;
438 bus_addr_t addr, start;
439
440 sc = (struct eso_softc *)self;
441 pa = &sc->sc_pa;
442 aprint_normal("%s: ", sc->sc_dev.dv_xname);
443
444 /*
445 * This is outright ugly, but since we must not make assumptions
446 * on the underlying allocator's behaviour it's the most straight-
447 * forward way to implement it. Note that we skip over the first
448 * 1K region, which is typically occupied by an attached ISA bus.
449 */
450 for (start = 0x0400; start < 0xffff; start += 0x0400) {
451 if (bus_space_alloc(sc->sc_iot,
452 start + sc->sc_vcsize, start + 0x0400 - 1,
453 sc->sc_vcsize, sc->sc_vcsize, 0, 0, &addr,
454 &sc->sc_dmac_ioh) != 0)
455 continue;
456
457 pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_DDMAC,
458 addr | ESO_PCI_DDMAC_DE);
459 sc->sc_dmac_iot = sc->sc_iot;
460 sc->sc_dmac_configured = 1;
461 aprint_normal("mapping Audio 1 DMA using I/O space at 0x%lx\n",
462 (unsigned long)addr);
463
464 return;
465 }
466
467 aprint_error("can't map Audio 1 DMA into I/O space\n");
468 }
469
470 /* ARGSUSED */
471 static int
472 eso_print(void *aux, const char *pnp)
473 {
474
475 /* Only joys can attach via this; easy. */
476 if (pnp)
477 aprint_normal("joy at %s:", pnp);
478
479 return UNCONF;
480 }
481
482 static void
483 eso_write_cmd(struct eso_softc *sc, uint8_t cmd)
484 {
485 int i;
486
487 /* Poll for busy indicator to become clear. */
488 for (i = 0; i < ESO_WDR_TIMEOUT; i++) {
489 if ((bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RSR)
490 & ESO_SB_RSR_BUSY) == 0) {
491 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh,
492 ESO_SB_WDR, cmd);
493 return;
494 } else {
495 delay(10);
496 }
497 }
498
499 printf("%s: WDR timeout\n", sc->sc_dev.dv_xname);
500 return;
501 }
502
503 /* Write to a controller register */
504 static void
505 eso_write_ctlreg(struct eso_softc *sc, uint8_t reg, uint8_t val)
506 {
507
508 /* DPRINTF(("ctlreg 0x%02x = 0x%02x\n", reg, val)); */
509
510 eso_write_cmd(sc, reg);
511 eso_write_cmd(sc, val);
512 }
513
514 /* Read out the Read Data Register */
515 static uint8_t
516 eso_read_rdr(struct eso_softc *sc)
517 {
518 int i;
519
520 for (i = 0; i < ESO_RDR_TIMEOUT; i++) {
521 if (bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
522 ESO_SB_RBSR) & ESO_SB_RBSR_RDAV) {
523 return (bus_space_read_1(sc->sc_sb_iot,
524 sc->sc_sb_ioh, ESO_SB_RDR));
525 } else {
526 delay(10);
527 }
528 }
529
530 printf("%s: RDR timeout\n", sc->sc_dev.dv_xname);
531 return (-1);
532 }
533
534 static uint8_t
535 eso_read_ctlreg(struct eso_softc *sc, uint8_t reg)
536 {
537
538 eso_write_cmd(sc, ESO_CMD_RCR);
539 eso_write_cmd(sc, reg);
540 return eso_read_rdr(sc);
541 }
542
543 static void
544 eso_write_mixreg(struct eso_softc *sc, uint8_t reg, uint8_t val)
545 {
546 int s;
547
548 /* DPRINTF(("mixreg 0x%02x = 0x%02x\n", reg, val)); */
549
550 s = splaudio();
551 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
552 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA, val);
553 splx(s);
554 }
555
556 static uint8_t
557 eso_read_mixreg(struct eso_softc *sc, uint8_t reg)
558 {
559 int s;
560 uint8_t val;
561
562 s = splaudio();
563 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
564 val = bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA);
565 splx(s);
566
567 return val;
568 }
569
570 static int
571 eso_intr(void *hdl)
572 {
573 struct eso_softc *sc;
574 uint8_t irqctl;
575
576 sc = hdl;
577 irqctl = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL);
578
579 /* If it wasn't ours, that's all she wrote. */
580 if ((irqctl & (ESO_IO_IRQCTL_A1IRQ | ESO_IO_IRQCTL_A2IRQ |
581 ESO_IO_IRQCTL_HVIRQ | ESO_IO_IRQCTL_MPUIRQ)) == 0)
582 return 0;
583
584 if (irqctl & ESO_IO_IRQCTL_A1IRQ) {
585 /* Clear interrupt. */
586 (void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
587 ESO_SB_RBSR);
588
589 if (sc->sc_rintr)
590 sc->sc_rintr(sc->sc_rarg);
591 else
592 wakeup(&sc->sc_rintr);
593 }
594
595 if (irqctl & ESO_IO_IRQCTL_A2IRQ) {
596 /*
597 * Clear the A2 IRQ latch: the cached value reflects the
598 * current DAC settings with the IRQ latch bit not set.
599 */
600 eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
601
602 if (sc->sc_pintr)
603 sc->sc_pintr(sc->sc_parg);
604 else
605 wakeup(&sc->sc_pintr);
606 }
607
608 if (irqctl & ESO_IO_IRQCTL_HVIRQ) {
609 /* Clear interrupt. */
610 eso_write_mixreg(sc, ESO_MIXREG_CHVIR, ESO_MIXREG_CHVIR_CHVIR);
611
612 /*
613 * Raise a flag to cause a lazy update of the in-softc gain
614 * values the next time the software mixer is read to keep
615 * interrupt service cost low. ~0 cannot occur otherwise
616 * as the master volume has a precision of 6 bits only.
617 */
618 sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] = (uint8_t)~0;
619 }
620
621 #if NMPU > 0
622 if ((irqctl & ESO_IO_IRQCTL_MPUIRQ) && sc->sc_mpudev != NULL)
623 mpu_intr(sc->sc_mpudev);
624 #endif
625
626 return 1;
627 }
628
629 /* Perform a software reset, including DMA FIFOs. */
630 static int
631 eso_reset(struct eso_softc *sc)
632 {
633 int i;
634
635 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET,
636 ESO_SB_RESET_SW | ESO_SB_RESET_FIFO);
637 /* `Delay' suggested in the data sheet. */
638 (void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_STATUS);
639 bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET, 0);
640
641 /* Wait for reset to take effect. */
642 for (i = 0; i < ESO_RESET_TIMEOUT; i++) {
643 /* Poll for data to become available. */
644 if ((bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
645 ESO_SB_RBSR) & ESO_SB_RBSR_RDAV) != 0 &&
646 bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
647 ESO_SB_RDR) == ESO_SB_RDR_RESETMAGIC) {
648
649 /* Activate Solo-1 extension commands. */
650 eso_write_cmd(sc, ESO_CMD_EXTENB);
651 /* Reset mixer registers. */
652 eso_write_mixreg(sc, ESO_MIXREG_RESET,
653 ESO_MIXREG_RESET_RESET);
654
655 return 0;
656 } else {
657 delay(1000);
658 }
659 }
660
661 printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
662 return -1;
663 }
664
665 static int
666 eso_query_encoding(void *hdl, struct audio_encoding *fp)
667 {
668
669 switch (fp->index) {
670 case 0:
671 strcpy(fp->name, AudioEulinear);
672 fp->encoding = AUDIO_ENCODING_ULINEAR;
673 fp->precision = 8;
674 fp->flags = 0;
675 break;
676 case 1:
677 strcpy(fp->name, AudioEmulaw);
678 fp->encoding = AUDIO_ENCODING_ULAW;
679 fp->precision = 8;
680 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
681 break;
682 case 2:
683 strcpy(fp->name, AudioEalaw);
684 fp->encoding = AUDIO_ENCODING_ALAW;
685 fp->precision = 8;
686 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
687 break;
688 case 3:
689 strcpy(fp->name, AudioEslinear);
690 fp->encoding = AUDIO_ENCODING_SLINEAR;
691 fp->precision = 8;
692 fp->flags = 0;
693 break;
694 case 4:
695 strcpy(fp->name, AudioEslinear_le);
696 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
697 fp->precision = 16;
698 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
699 break;
700 case 5:
701 strcpy(fp->name, AudioEulinear_le);
702 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
703 fp->precision = 16;
704 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
705 break;
706 case 6:
707 strcpy(fp->name, AudioEslinear_be);
708 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
709 fp->precision = 16;
710 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
711 break;
712 case 7:
713 strcpy(fp->name, AudioEulinear_be);
714 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
715 fp->precision = 16;
716 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
717 break;
718 default:
719 return EINVAL;
720 }
721
722 return 0;
723 }
724
725 static int
726 eso_set_params(void *hdl, int setmode, int usemode, audio_params_t *play,
727 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
728 {
729 struct eso_softc *sc;
730 struct audio_params *p;
731 stream_filter_list_t *fil;
732 int mode, r[2], rd[2], clk;
733 unsigned int srg, fltdiv;
734 int i;
735
736 sc = hdl;
737 for (mode = AUMODE_RECORD; mode != -1;
738 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
739 if ((setmode & mode) == 0)
740 continue;
741
742 p = (mode == AUMODE_PLAY) ? play : rec;
743
744 if (p->sample_rate < ESO_MINRATE ||
745 p->sample_rate > ESO_MAXRATE ||
746 (p->precision != 8 && p->precision != 16) ||
747 (p->channels != 1 && p->channels != 2))
748 return EINVAL;
749
750 /*
751 * We'll compute both possible sample rate dividers and pick
752 * the one with the least error.
753 */
754 #define ABS(x) ((x) < 0 ? -(x) : (x))
755 r[0] = ESO_CLK0 /
756 (128 - (rd[0] = 128 - ESO_CLK0 / p->sample_rate));
757 r[1] = ESO_CLK1 /
758 (128 - (rd[1] = 128 - ESO_CLK1 / p->sample_rate));
759
760 clk = ABS(p->sample_rate - r[0]) > ABS(p->sample_rate - r[1]);
761 srg = rd[clk] | (clk == 1 ? ESO_CLK1_SELECT : 0x00);
762
763 /* Roll-off frequency of 87%, as in the ES1888 driver. */
764 fltdiv = 256 - 200279L / r[clk];
765
766 /* Update to reflect the possibly inexact rate. */
767 p->sample_rate = r[clk];
768
769 fil = (mode == AUMODE_PLAY) ? pfil : rfil;
770 i = auconv_set_converter(eso_formats, ESO_NFORMATS,
771 mode, p, FALSE, fil);
772 if (i < 0)
773 return EINVAL;
774 if (mode == AUMODE_RECORD) {
775 /* Audio 1 */
776 DPRINTF(("A1 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
777 eso_write_ctlreg(sc, ESO_CTLREG_SRG, srg);
778 eso_write_ctlreg(sc, ESO_CTLREG_FLTDIV, fltdiv);
779 } else {
780 /* Audio 2 */
781 DPRINTF(("A2 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
782 eso_write_mixreg(sc, ESO_MIXREG_A2SRG, srg);
783 eso_write_mixreg(sc, ESO_MIXREG_A2FLTDIV, fltdiv);
784 }
785 #undef ABS
786
787 }
788
789 return 0;
790 }
791
792 static int
793 eso_round_blocksize(void *hdl, int blk, int mode, const audio_params_t *param)
794 {
795
796 return blk & -32; /* keep good alignment; at least 16 req'd */
797 }
798
799 static int
800 eso_halt_output(void *hdl)
801 {
802 struct eso_softc *sc;
803 int error, s;
804
805 sc = hdl;
806 DPRINTF(("%s: halt_output\n", sc->sc_dev.dv_xname));
807
808 /*
809 * Disable auto-initialize DMA, allowing the FIFO to drain and then
810 * stop. The interrupt callback pointer is cleared at this
811 * point so that an outstanding FIFO interrupt for the remaining data
812 * will be acknowledged without further processing.
813 *
814 * This does not immediately `abort' an operation in progress (c.f.
815 * audio(9)) but is the method to leave the FIFO behind in a clean
816 * state with the least hair. (Besides, that item needs to be
817 * rephrased for trigger_*()-based DMA environments.)
818 */
819 s = splaudio();
820 eso_write_mixreg(sc, ESO_MIXREG_A2C1,
821 ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB);
822 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
823 ESO_IO_A2DMAM_DMAENB);
824
825 sc->sc_pintr = NULL;
826 error = tsleep(&sc->sc_pintr, PCATCH | PWAIT, "esoho", sc->sc_pdrain);
827 splx(s);
828
829 /* Shut down DMA completely. */
830 eso_write_mixreg(sc, ESO_MIXREG_A2C1, 0);
831 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
832
833 return error == EWOULDBLOCK ? 0 : error;
834 }
835
836 static int
837 eso_halt_input(void *hdl)
838 {
839 struct eso_softc *sc;
840 int error, s;
841
842 sc = hdl;
843 DPRINTF(("%s: halt_input\n", sc->sc_dev.dv_xname));
844
845 /* Just like eso_halt_output(), but for Audio 1. */
846 s = splaudio();
847 eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
848 ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC |
849 ESO_CTLREG_A1C2_DMAENB);
850 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
851 DMA37MD_WRITE | DMA37MD_DEMAND);
852
853 sc->sc_rintr = NULL;
854 error = tsleep(&sc->sc_rintr, PCATCH | PWAIT, "esohi", sc->sc_rdrain);
855 splx(s);
856
857 /* Shut down DMA completely. */
858 eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
859 ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC);
860 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
861 ESO_DMAC_MASK_MASK);
862
863 return error == EWOULDBLOCK ? 0 : error;
864 }
865
866 static int
867 eso_getdev(void *hdl, struct audio_device *retp)
868 {
869 struct eso_softc *sc;
870
871 sc = hdl;
872 strncpy(retp->name, "ESS Solo-1", sizeof (retp->name));
873 snprintf(retp->version, sizeof (retp->version), "0x%02x",
874 sc->sc_revision);
875 if (sc->sc_revision <
876 sizeof (eso_rev2model) / sizeof (eso_rev2model[0]))
877 strncpy(retp->config, eso_rev2model[sc->sc_revision],
878 sizeof (retp->config));
879 else
880 strncpy(retp->config, "unknown", sizeof (retp->config));
881
882 return 0;
883 }
884
885 static int
886 eso_set_port(void *hdl, mixer_ctrl_t *cp)
887 {
888 struct eso_softc *sc;
889 unsigned int lgain, rgain;
890 uint8_t tmp;
891
892 sc = hdl;
893 switch (cp->dev) {
894 case ESO_DAC_PLAY_VOL:
895 case ESO_MIC_PLAY_VOL:
896 case ESO_LINE_PLAY_VOL:
897 case ESO_SYNTH_PLAY_VOL:
898 case ESO_CD_PLAY_VOL:
899 case ESO_AUXB_PLAY_VOL:
900 case ESO_RECORD_VOL:
901 case ESO_DAC_REC_VOL:
902 case ESO_MIC_REC_VOL:
903 case ESO_LINE_REC_VOL:
904 case ESO_SYNTH_REC_VOL:
905 case ESO_CD_REC_VOL:
906 case ESO_AUXB_REC_VOL:
907 if (cp->type != AUDIO_MIXER_VALUE)
908 return EINVAL;
909
910 /*
911 * Stereo-capable mixer ports: if we get a single-channel
912 * gain value passed in, then we duplicate it to both left
913 * and right channels.
914 */
915 switch (cp->un.value.num_channels) {
916 case 1:
917 lgain = rgain = ESO_GAIN_TO_4BIT(
918 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
919 break;
920 case 2:
921 lgain = ESO_GAIN_TO_4BIT(
922 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
923 rgain = ESO_GAIN_TO_4BIT(
924 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
925 break;
926 default:
927 return EINVAL;
928 }
929
930 sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
931 sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
932 eso_set_gain(sc, cp->dev);
933 break;
934
935 case ESO_MASTER_VOL:
936 if (cp->type != AUDIO_MIXER_VALUE)
937 return EINVAL;
938
939 /* Like above, but a precision of 6 bits. */
940 switch (cp->un.value.num_channels) {
941 case 1:
942 lgain = rgain = ESO_GAIN_TO_6BIT(
943 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
944 break;
945 case 2:
946 lgain = ESO_GAIN_TO_6BIT(
947 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
948 rgain = ESO_GAIN_TO_6BIT(
949 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
950 break;
951 default:
952 return EINVAL;
953 }
954
955 sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
956 sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
957 eso_set_gain(sc, cp->dev);
958 break;
959
960 case ESO_SPATIALIZER:
961 if (cp->type != AUDIO_MIXER_VALUE ||
962 cp->un.value.num_channels != 1)
963 return EINVAL;
964
965 sc->sc_gain[cp->dev][ESO_LEFT] =
966 sc->sc_gain[cp->dev][ESO_RIGHT] =
967 ESO_GAIN_TO_6BIT(
968 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
969 eso_set_gain(sc, cp->dev);
970 break;
971
972 case ESO_MONO_PLAY_VOL:
973 case ESO_MONO_REC_VOL:
974 if (cp->type != AUDIO_MIXER_VALUE ||
975 cp->un.value.num_channels != 1)
976 return EINVAL;
977
978 sc->sc_gain[cp->dev][ESO_LEFT] =
979 sc->sc_gain[cp->dev][ESO_RIGHT] =
980 ESO_GAIN_TO_4BIT(
981 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
982 eso_set_gain(sc, cp->dev);
983 break;
984
985 case ESO_PCSPEAKER_VOL:
986 if (cp->type != AUDIO_MIXER_VALUE ||
987 cp->un.value.num_channels != 1)
988 return EINVAL;
989
990 sc->sc_gain[cp->dev][ESO_LEFT] =
991 sc->sc_gain[cp->dev][ESO_RIGHT] =
992 ESO_GAIN_TO_3BIT(
993 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
994 eso_set_gain(sc, cp->dev);
995 break;
996
997 case ESO_SPATIALIZER_ENABLE:
998 if (cp->type != AUDIO_MIXER_ENUM)
999 return EINVAL;
1000
1001 sc->sc_spatializer = (cp->un.ord != 0);
1002
1003 tmp = eso_read_mixreg(sc, ESO_MIXREG_SPAT);
1004 if (sc->sc_spatializer)
1005 tmp |= ESO_MIXREG_SPAT_ENB;
1006 else
1007 tmp &= ~ESO_MIXREG_SPAT_ENB;
1008 eso_write_mixreg(sc, ESO_MIXREG_SPAT,
1009 tmp | ESO_MIXREG_SPAT_RSTREL);
1010 break;
1011
1012 case ESO_MASTER_MUTE:
1013 if (cp->type != AUDIO_MIXER_ENUM)
1014 return EINVAL;
1015
1016 sc->sc_mvmute = (cp->un.ord != 0);
1017
1018 if (sc->sc_mvmute) {
1019 eso_write_mixreg(sc, ESO_MIXREG_LMVM,
1020 eso_read_mixreg(sc, ESO_MIXREG_LMVM) |
1021 ESO_MIXREG_LMVM_MUTE);
1022 eso_write_mixreg(sc, ESO_MIXREG_RMVM,
1023 eso_read_mixreg(sc, ESO_MIXREG_RMVM) |
1024 ESO_MIXREG_RMVM_MUTE);
1025 } else {
1026 eso_write_mixreg(sc, ESO_MIXREG_LMVM,
1027 eso_read_mixreg(sc, ESO_MIXREG_LMVM) &
1028 ~ESO_MIXREG_LMVM_MUTE);
1029 eso_write_mixreg(sc, ESO_MIXREG_RMVM,
1030 eso_read_mixreg(sc, ESO_MIXREG_RMVM) &
1031 ~ESO_MIXREG_RMVM_MUTE);
1032 }
1033 break;
1034
1035 case ESO_MONOOUT_SOURCE:
1036 if (cp->type != AUDIO_MIXER_ENUM)
1037 return EINVAL;
1038
1039 return eso_set_monooutsrc(sc, cp->un.ord);
1040
1041 case ESO_MONOIN_BYPASS:
1042 if (cp->type != AUDIO_MIXER_ENUM)
1043 return EINVAL;
1044
1045 return (eso_set_monoinbypass(sc, cp->un.ord));
1046
1047 case ESO_RECORD_MONITOR:
1048 if (cp->type != AUDIO_MIXER_ENUM)
1049 return EINVAL;
1050
1051 sc->sc_recmon = (cp->un.ord != 0);
1052
1053 tmp = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
1054 if (sc->sc_recmon)
1055 tmp |= ESO_CTLREG_ACTL_RECMON;
1056 else
1057 tmp &= ~ESO_CTLREG_ACTL_RECMON;
1058 eso_write_ctlreg(sc, ESO_CTLREG_ACTL, tmp);
1059 break;
1060
1061 case ESO_RECORD_SOURCE:
1062 if (cp->type != AUDIO_MIXER_ENUM)
1063 return EINVAL;
1064
1065 return eso_set_recsrc(sc, cp->un.ord);
1066
1067 case ESO_MIC_PREAMP:
1068 if (cp->type != AUDIO_MIXER_ENUM)
1069 return EINVAL;
1070
1071 return eso_set_preamp(sc, cp->un.ord);
1072
1073 default:
1074 return EINVAL;
1075 }
1076
1077 return 0;
1078 }
1079
1080 static int
1081 eso_get_port(void *hdl, mixer_ctrl_t *cp)
1082 {
1083 struct eso_softc *sc;
1084
1085 sc = hdl;
1086 switch (cp->dev) {
1087 case ESO_MASTER_VOL:
1088 /* Reload from mixer after hardware volume control use. */
1089 if (sc->sc_gain[cp->dev][ESO_LEFT] == (uint8_t)~0)
1090 eso_reload_master_vol(sc);
1091 /* FALLTHROUGH */
1092 case ESO_DAC_PLAY_VOL:
1093 case ESO_MIC_PLAY_VOL:
1094 case ESO_LINE_PLAY_VOL:
1095 case ESO_SYNTH_PLAY_VOL:
1096 case ESO_CD_PLAY_VOL:
1097 case ESO_AUXB_PLAY_VOL:
1098 case ESO_RECORD_VOL:
1099 case ESO_DAC_REC_VOL:
1100 case ESO_MIC_REC_VOL:
1101 case ESO_LINE_REC_VOL:
1102 case ESO_SYNTH_REC_VOL:
1103 case ESO_CD_REC_VOL:
1104 case ESO_AUXB_REC_VOL:
1105 /*
1106 * Stereo-capable ports: if a single-channel query is made,
1107 * just return the left channel's value (since single-channel
1108 * settings themselves are applied to both channels).
1109 */
1110 switch (cp->un.value.num_channels) {
1111 case 1:
1112 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1113 sc->sc_gain[cp->dev][ESO_LEFT];
1114 break;
1115 case 2:
1116 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1117 sc->sc_gain[cp->dev][ESO_LEFT];
1118 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1119 sc->sc_gain[cp->dev][ESO_RIGHT];
1120 break;
1121 default:
1122 return EINVAL;
1123 }
1124 break;
1125
1126 case ESO_MONO_PLAY_VOL:
1127 case ESO_PCSPEAKER_VOL:
1128 case ESO_MONO_REC_VOL:
1129 case ESO_SPATIALIZER:
1130 if (cp->un.value.num_channels != 1)
1131 return EINVAL;
1132 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1133 sc->sc_gain[cp->dev][ESO_LEFT];
1134 break;
1135
1136 case ESO_RECORD_MONITOR:
1137 cp->un.ord = sc->sc_recmon;
1138 break;
1139
1140 case ESO_RECORD_SOURCE:
1141 cp->un.ord = sc->sc_recsrc;
1142 break;
1143
1144 case ESO_MONOOUT_SOURCE:
1145 cp->un.ord = sc->sc_monooutsrc;
1146 break;
1147
1148 case ESO_MONOIN_BYPASS:
1149 cp->un.ord = sc->sc_monoinbypass;
1150 break;
1151
1152 case ESO_SPATIALIZER_ENABLE:
1153 cp->un.ord = sc->sc_spatializer;
1154 break;
1155
1156 case ESO_MIC_PREAMP:
1157 cp->un.ord = sc->sc_preamp;
1158 break;
1159
1160 case ESO_MASTER_MUTE:
1161 /* Reload from mixer after hardware volume control use. */
1162 if (sc->sc_gain[cp->dev][ESO_LEFT] == (uint8_t)~0)
1163 eso_reload_master_vol(sc);
1164 cp->un.ord = sc->sc_mvmute;
1165 break;
1166
1167 default:
1168 return EINVAL;
1169 }
1170
1171 return 0;
1172 }
1173
1174 static int
1175 eso_query_devinfo(void *hdl, mixer_devinfo_t *dip)
1176 {
1177
1178 switch (dip->index) {
1179 case ESO_DAC_PLAY_VOL:
1180 dip->mixer_class = ESO_INPUT_CLASS;
1181 dip->next = dip->prev = AUDIO_MIXER_LAST;
1182 strcpy(dip->label.name, AudioNdac);
1183 dip->type = AUDIO_MIXER_VALUE;
1184 dip->un.v.num_channels = 2;
1185 strcpy(dip->un.v.units.name, AudioNvolume);
1186 break;
1187 case ESO_MIC_PLAY_VOL:
1188 dip->mixer_class = ESO_INPUT_CLASS;
1189 dip->next = dip->prev = AUDIO_MIXER_LAST;
1190 strcpy(dip->label.name, AudioNmicrophone);
1191 dip->type = AUDIO_MIXER_VALUE;
1192 dip->un.v.num_channels = 2;
1193 strcpy(dip->un.v.units.name, AudioNvolume);
1194 break;
1195 case ESO_LINE_PLAY_VOL:
1196 dip->mixer_class = ESO_INPUT_CLASS;
1197 dip->next = dip->prev = AUDIO_MIXER_LAST;
1198 strcpy(dip->label.name, AudioNline);
1199 dip->type = AUDIO_MIXER_VALUE;
1200 dip->un.v.num_channels = 2;
1201 strcpy(dip->un.v.units.name, AudioNvolume);
1202 break;
1203 case ESO_SYNTH_PLAY_VOL:
1204 dip->mixer_class = ESO_INPUT_CLASS;
1205 dip->next = dip->prev = AUDIO_MIXER_LAST;
1206 strcpy(dip->label.name, AudioNfmsynth);
1207 dip->type = AUDIO_MIXER_VALUE;
1208 dip->un.v.num_channels = 2;
1209 strcpy(dip->un.v.units.name, AudioNvolume);
1210 break;
1211 case ESO_MONO_PLAY_VOL:
1212 dip->mixer_class = ESO_INPUT_CLASS;
1213 dip->next = dip->prev = AUDIO_MIXER_LAST;
1214 strcpy(dip->label.name, "mono_in");
1215 dip->type = AUDIO_MIXER_VALUE;
1216 dip->un.v.num_channels = 1;
1217 strcpy(dip->un.v.units.name, AudioNvolume);
1218 break;
1219 case ESO_CD_PLAY_VOL:
1220 dip->mixer_class = ESO_INPUT_CLASS;
1221 dip->next = dip->prev = AUDIO_MIXER_LAST;
1222 strcpy(dip->label.name, AudioNcd);
1223 dip->type = AUDIO_MIXER_VALUE;
1224 dip->un.v.num_channels = 2;
1225 strcpy(dip->un.v.units.name, AudioNvolume);
1226 break;
1227 case ESO_AUXB_PLAY_VOL:
1228 dip->mixer_class = ESO_INPUT_CLASS;
1229 dip->next = dip->prev = AUDIO_MIXER_LAST;
1230 strcpy(dip->label.name, "auxb");
1231 dip->type = AUDIO_MIXER_VALUE;
1232 dip->un.v.num_channels = 2;
1233 strcpy(dip->un.v.units.name, AudioNvolume);
1234 break;
1235
1236 case ESO_MIC_PREAMP:
1237 dip->mixer_class = ESO_MICROPHONE_CLASS;
1238 dip->next = dip->prev = AUDIO_MIXER_LAST;
1239 strcpy(dip->label.name, AudioNpreamp);
1240 dip->type = AUDIO_MIXER_ENUM;
1241 dip->un.e.num_mem = 2;
1242 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1243 dip->un.e.member[0].ord = 0;
1244 strcpy(dip->un.e.member[1].label.name, AudioNon);
1245 dip->un.e.member[1].ord = 1;
1246 break;
1247 case ESO_MICROPHONE_CLASS:
1248 dip->mixer_class = ESO_MICROPHONE_CLASS;
1249 dip->next = dip->prev = AUDIO_MIXER_LAST;
1250 strcpy(dip->label.name, AudioNmicrophone);
1251 dip->type = AUDIO_MIXER_CLASS;
1252 break;
1253
1254 case ESO_INPUT_CLASS:
1255 dip->mixer_class = ESO_INPUT_CLASS;
1256 dip->next = dip->prev = AUDIO_MIXER_LAST;
1257 strcpy(dip->label.name, AudioCinputs);
1258 dip->type = AUDIO_MIXER_CLASS;
1259 break;
1260
1261 case ESO_MASTER_VOL:
1262 dip->mixer_class = ESO_OUTPUT_CLASS;
1263 dip->prev = AUDIO_MIXER_LAST;
1264 dip->next = ESO_MASTER_MUTE;
1265 strcpy(dip->label.name, AudioNmaster);
1266 dip->type = AUDIO_MIXER_VALUE;
1267 dip->un.v.num_channels = 2;
1268 strcpy(dip->un.v.units.name, AudioNvolume);
1269 break;
1270 case ESO_MASTER_MUTE:
1271 dip->mixer_class = ESO_OUTPUT_CLASS;
1272 dip->prev = ESO_MASTER_VOL;
1273 dip->next = AUDIO_MIXER_LAST;
1274 strcpy(dip->label.name, AudioNmute);
1275 dip->type = AUDIO_MIXER_ENUM;
1276 dip->un.e.num_mem = 2;
1277 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1278 dip->un.e.member[0].ord = 0;
1279 strcpy(dip->un.e.member[1].label.name, AudioNon);
1280 dip->un.e.member[1].ord = 1;
1281 break;
1282
1283 case ESO_PCSPEAKER_VOL:
1284 dip->mixer_class = ESO_OUTPUT_CLASS;
1285 dip->next = dip->prev = AUDIO_MIXER_LAST;
1286 strcpy(dip->label.name, "pc_speaker");
1287 dip->type = AUDIO_MIXER_VALUE;
1288 dip->un.v.num_channels = 1;
1289 strcpy(dip->un.v.units.name, AudioNvolume);
1290 break;
1291 case ESO_MONOOUT_SOURCE:
1292 dip->mixer_class = ESO_OUTPUT_CLASS;
1293 dip->next = dip->prev = AUDIO_MIXER_LAST;
1294 strcpy(dip->label.name, "mono_out");
1295 dip->type = AUDIO_MIXER_ENUM;
1296 dip->un.e.num_mem = 3;
1297 strcpy(dip->un.e.member[0].label.name, AudioNmute);
1298 dip->un.e.member[0].ord = ESO_MIXREG_MPM_MOMUTE;
1299 strcpy(dip->un.e.member[1].label.name, AudioNdac);
1300 dip->un.e.member[1].ord = ESO_MIXREG_MPM_MOA2R;
1301 strcpy(dip->un.e.member[2].label.name, AudioNmixerout);
1302 dip->un.e.member[2].ord = ESO_MIXREG_MPM_MOREC;
1303 break;
1304
1305 case ESO_MONOIN_BYPASS:
1306 dip->mixer_class = ESO_MONOIN_CLASS;
1307 dip->next = dip->prev = AUDIO_MIXER_LAST;
1308 strcpy(dip->label.name, "bypass");
1309 dip->type = AUDIO_MIXER_ENUM;
1310 dip->un.e.num_mem = 2;
1311 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1312 dip->un.e.member[0].ord = 0;
1313 strcpy(dip->un.e.member[1].label.name, AudioNon);
1314 dip->un.e.member[1].ord = 1;
1315 break;
1316 case ESO_MONOIN_CLASS:
1317 dip->mixer_class = ESO_MONOIN_CLASS;
1318 dip->next = dip->prev = AUDIO_MIXER_LAST;
1319 strcpy(dip->label.name, "mono_in");
1320 dip->type = AUDIO_MIXER_CLASS;
1321 break;
1322
1323 case ESO_SPATIALIZER:
1324 dip->mixer_class = ESO_OUTPUT_CLASS;
1325 dip->prev = AUDIO_MIXER_LAST;
1326 dip->next = ESO_SPATIALIZER_ENABLE;
1327 strcpy(dip->label.name, AudioNspatial);
1328 dip->type = AUDIO_MIXER_VALUE;
1329 dip->un.v.num_channels = 1;
1330 strcpy(dip->un.v.units.name, "level");
1331 break;
1332 case ESO_SPATIALIZER_ENABLE:
1333 dip->mixer_class = ESO_OUTPUT_CLASS;
1334 dip->prev = ESO_SPATIALIZER;
1335 dip->next = AUDIO_MIXER_LAST;
1336 strcpy(dip->label.name, "enable");
1337 dip->type = AUDIO_MIXER_ENUM;
1338 dip->un.e.num_mem = 2;
1339 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1340 dip->un.e.member[0].ord = 0;
1341 strcpy(dip->un.e.member[1].label.name, AudioNon);
1342 dip->un.e.member[1].ord = 1;
1343 break;
1344
1345 case ESO_OUTPUT_CLASS:
1346 dip->mixer_class = ESO_OUTPUT_CLASS;
1347 dip->next = dip->prev = AUDIO_MIXER_LAST;
1348 strcpy(dip->label.name, AudioCoutputs);
1349 dip->type = AUDIO_MIXER_CLASS;
1350 break;
1351
1352 case ESO_RECORD_MONITOR:
1353 dip->mixer_class = ESO_MONITOR_CLASS;
1354 dip->next = dip->prev = AUDIO_MIXER_LAST;
1355 strcpy(dip->label.name, AudioNmute);
1356 dip->type = AUDIO_MIXER_ENUM;
1357 dip->un.e.num_mem = 2;
1358 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1359 dip->un.e.member[0].ord = 0;
1360 strcpy(dip->un.e.member[1].label.name, AudioNon);
1361 dip->un.e.member[1].ord = 1;
1362 break;
1363 case ESO_MONITOR_CLASS:
1364 dip->mixer_class = ESO_MONITOR_CLASS;
1365 dip->next = dip->prev = AUDIO_MIXER_LAST;
1366 strcpy(dip->label.name, AudioCmonitor);
1367 dip->type = AUDIO_MIXER_CLASS;
1368 break;
1369
1370 case ESO_RECORD_VOL:
1371 dip->mixer_class = ESO_RECORD_CLASS;
1372 dip->next = dip->prev = AUDIO_MIXER_LAST;
1373 strcpy(dip->label.name, AudioNrecord);
1374 dip->type = AUDIO_MIXER_VALUE;
1375 strcpy(dip->un.v.units.name, AudioNvolume);
1376 break;
1377 case ESO_RECORD_SOURCE:
1378 dip->mixer_class = ESO_RECORD_CLASS;
1379 dip->next = dip->prev = AUDIO_MIXER_LAST;
1380 strcpy(dip->label.name, AudioNsource);
1381 dip->type = AUDIO_MIXER_ENUM;
1382 dip->un.e.num_mem = 4;
1383 strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
1384 dip->un.e.member[0].ord = ESO_MIXREG_ERS_MIC;
1385 strcpy(dip->un.e.member[1].label.name, AudioNline);
1386 dip->un.e.member[1].ord = ESO_MIXREG_ERS_LINE;
1387 strcpy(dip->un.e.member[2].label.name, AudioNcd);
1388 dip->un.e.member[2].ord = ESO_MIXREG_ERS_CD;
1389 strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
1390 dip->un.e.member[3].ord = ESO_MIXREG_ERS_MIXER;
1391 break;
1392 case ESO_DAC_REC_VOL:
1393 dip->mixer_class = ESO_RECORD_CLASS;
1394 dip->next = dip->prev = AUDIO_MIXER_LAST;
1395 strcpy(dip->label.name, AudioNdac);
1396 dip->type = AUDIO_MIXER_VALUE;
1397 dip->un.v.num_channels = 2;
1398 strcpy(dip->un.v.units.name, AudioNvolume);
1399 break;
1400 case ESO_MIC_REC_VOL:
1401 dip->mixer_class = ESO_RECORD_CLASS;
1402 dip->next = dip->prev = AUDIO_MIXER_LAST;
1403 strcpy(dip->label.name, AudioNmicrophone);
1404 dip->type = AUDIO_MIXER_VALUE;
1405 dip->un.v.num_channels = 2;
1406 strcpy(dip->un.v.units.name, AudioNvolume);
1407 break;
1408 case ESO_LINE_REC_VOL:
1409 dip->mixer_class = ESO_RECORD_CLASS;
1410 dip->next = dip->prev = AUDIO_MIXER_LAST;
1411 strcpy(dip->label.name, AudioNline);
1412 dip->type = AUDIO_MIXER_VALUE;
1413 dip->un.v.num_channels = 2;
1414 strcpy(dip->un.v.units.name, AudioNvolume);
1415 break;
1416 case ESO_SYNTH_REC_VOL:
1417 dip->mixer_class = ESO_RECORD_CLASS;
1418 dip->next = dip->prev = AUDIO_MIXER_LAST;
1419 strcpy(dip->label.name, AudioNfmsynth);
1420 dip->type = AUDIO_MIXER_VALUE;
1421 dip->un.v.num_channels = 2;
1422 strcpy(dip->un.v.units.name, AudioNvolume);
1423 break;
1424 case ESO_MONO_REC_VOL:
1425 dip->mixer_class = ESO_RECORD_CLASS;
1426 dip->next = dip->prev = AUDIO_MIXER_LAST;
1427 strcpy(dip->label.name, "mono_in");
1428 dip->type = AUDIO_MIXER_VALUE;
1429 dip->un.v.num_channels = 1; /* No lies */
1430 strcpy(dip->un.v.units.name, AudioNvolume);
1431 break;
1432 case ESO_CD_REC_VOL:
1433 dip->mixer_class = ESO_RECORD_CLASS;
1434 dip->next = dip->prev = AUDIO_MIXER_LAST;
1435 strcpy(dip->label.name, AudioNcd);
1436 dip->type = AUDIO_MIXER_VALUE;
1437 dip->un.v.num_channels = 2;
1438 strcpy(dip->un.v.units.name, AudioNvolume);
1439 break;
1440 case ESO_AUXB_REC_VOL:
1441 dip->mixer_class = ESO_RECORD_CLASS;
1442 dip->next = dip->prev = AUDIO_MIXER_LAST;
1443 strcpy(dip->label.name, "auxb");
1444 dip->type = AUDIO_MIXER_VALUE;
1445 dip->un.v.num_channels = 2;
1446 strcpy(dip->un.v.units.name, AudioNvolume);
1447 break;
1448 case ESO_RECORD_CLASS:
1449 dip->mixer_class = ESO_RECORD_CLASS;
1450 dip->next = dip->prev = AUDIO_MIXER_LAST;
1451 strcpy(dip->label.name, AudioCrecord);
1452 dip->type = AUDIO_MIXER_CLASS;
1453 break;
1454
1455 default:
1456 return ENXIO;
1457 }
1458
1459 return 0;
1460 }
1461
1462 static int
1463 eso_allocmem(struct eso_softc *sc, size_t size, size_t align, size_t boundary,
1464 int flags, int direction, struct eso_dma *ed)
1465 {
1466 int error, wait;
1467
1468 wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
1469 ed->ed_size = size;
1470
1471 error = bus_dmamem_alloc(ed->ed_dmat, ed->ed_size, align, boundary,
1472 ed->ed_segs, sizeof (ed->ed_segs) / sizeof (ed->ed_segs[0]),
1473 &ed->ed_nsegs, wait);
1474 if (error)
1475 goto out;
1476
1477 error = bus_dmamem_map(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
1478 ed->ed_size, &ed->ed_addr, wait | BUS_DMA_COHERENT);
1479 if (error)
1480 goto free;
1481
1482 error = bus_dmamap_create(ed->ed_dmat, ed->ed_size, 1, ed->ed_size, 0,
1483 wait, &ed->ed_map);
1484 if (error)
1485 goto unmap;
1486
1487 error = bus_dmamap_load(ed->ed_dmat, ed->ed_map, ed->ed_addr,
1488 ed->ed_size, NULL, wait |
1489 (direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE);
1490 if (error)
1491 goto destroy;
1492
1493 return 0;
1494
1495 destroy:
1496 bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1497 unmap:
1498 bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1499 free:
1500 bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1501 out:
1502 return error;
1503 }
1504
1505 static void
1506 eso_freemem(struct eso_dma *ed)
1507 {
1508
1509 bus_dmamap_unload(ed->ed_dmat, ed->ed_map);
1510 bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1511 bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1512 bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1513 }
1514
1515 static void *
1516 eso_allocm(void *hdl, int direction, size_t size, struct malloc_type *type,
1517 int flags)
1518 {
1519 struct eso_softc *sc;
1520 struct eso_dma *ed;
1521 size_t boundary;
1522 int error;
1523
1524 sc = hdl;
1525 if ((ed = malloc(sizeof (*ed), type, flags)) == NULL)
1526 return NULL;
1527
1528 /*
1529 * Apparently the Audio 1 DMA controller's current address
1530 * register can't roll over a 64K address boundary, so we have to
1531 * take care of that ourselves. Similarly, the Audio 2 DMA
1532 * controller needs a 1M address boundary.
1533 */
1534 if (direction == AUMODE_RECORD)
1535 boundary = 0x10000;
1536 else
1537 boundary = 0x100000;
1538
1539 /*
1540 * XXX Work around allocation problems for Audio 1, which
1541 * XXX implements the 24 low address bits only, with
1542 * XXX machine-specific DMA tag use.
1543 */
1544 #ifdef alpha
1545 /*
1546 * XXX Force allocation through the (ISA) SGMAP.
1547 */
1548 if (direction == AUMODE_RECORD)
1549 ed->ed_dmat = alphabus_dma_get_tag(sc->sc_dmat, ALPHA_BUS_ISA);
1550 else
1551 #elif defined(amd64) || defined(i386)
1552 /*
1553 * XXX Force allocation through the ISA DMA tag.
1554 */
1555 if (direction == AUMODE_RECORD)
1556 ed->ed_dmat = &isa_bus_dma_tag;
1557 else
1558 #endif
1559 ed->ed_dmat = sc->sc_dmat;
1560
1561 error = eso_allocmem(sc, size, 32, boundary, flags, direction, ed);
1562 if (error) {
1563 free(ed, type);
1564 return NULL;
1565 }
1566 ed->ed_next = sc->sc_dmas;
1567 sc->sc_dmas = ed;
1568
1569 return KVADDR(ed);
1570 }
1571
1572 static void
1573 eso_freem(void *hdl, void *addr, struct malloc_type *type)
1574 {
1575 struct eso_softc *sc;
1576 struct eso_dma *p, **pp;
1577
1578 sc = hdl;
1579 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->ed_next) {
1580 if (KVADDR(p) == addr) {
1581 eso_freemem(p);
1582 *pp = p->ed_next;
1583 free(p, type);
1584 return;
1585 }
1586 }
1587 }
1588
1589 static size_t
1590 eso_round_buffersize(void *hdl, int direction, size_t bufsize)
1591 {
1592 size_t maxsize;
1593
1594 /*
1595 * The playback DMA buffer size on the Solo-1 is limited to 0xfff0
1596 * bytes. This is because IO_A2DMAC is a two byte value
1597 * indicating the literal byte count, and the 4 least significant
1598 * bits are read-only. Zero is not used as a special case for
1599 * 0x10000.
1600 *
1601 * For recording, DMAC_DMAC is the byte count - 1, so 0x10000 can
1602 * be represented.
1603 */
1604 maxsize = (direction == AUMODE_PLAY) ? 0xfff0 : 0x10000;
1605
1606 if (bufsize > maxsize)
1607 bufsize = maxsize;
1608
1609 return bufsize;
1610 }
1611
1612 static paddr_t
1613 eso_mappage(void *hdl, void *addr, off_t offs, int prot)
1614 {
1615 struct eso_softc *sc;
1616 struct eso_dma *ed;
1617
1618 sc = hdl;
1619 if (offs < 0)
1620 return -1;
1621 for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != addr;
1622 ed = ed->ed_next)
1623 continue;
1624 if (ed == NULL)
1625 return -1;
1626
1627 return bus_dmamem_mmap(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
1628 offs, prot, BUS_DMA_WAITOK);
1629 }
1630
1631 /* ARGSUSED */
1632 static int
1633 eso_get_props(void *hdl)
1634 {
1635
1636 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1637 AUDIO_PROP_FULLDUPLEX;
1638 }
1639
1640 static int
1641 eso_trigger_output(void *hdl, void *start, void *end, int blksize,
1642 void (*intr)(void *), void *arg, const audio_params_t *param)
1643 {
1644 struct eso_softc *sc;
1645 struct eso_dma *ed;
1646 uint8_t a2c1;
1647
1648 sc = hdl;
1649 DPRINTF((
1650 "%s: trigger_output: start %p, end %p, blksize %d, intr %p(%p)\n",
1651 sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1652 DPRINTF(("%s: param: rate %u, encoding %u, precision %u, channels %u\n",
1653 sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1654 param->precision, param->channels));
1655
1656 /* Find DMA buffer. */
1657 for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1658 ed = ed->ed_next)
1659 continue;
1660 if (ed == NULL) {
1661 printf("%s: trigger_output: bad addr %p\n",
1662 sc->sc_dev.dv_xname, start);
1663 return EINVAL;
1664 }
1665 DPRINTF(("%s: dmaaddr %lx\n",
1666 sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1667
1668 sc->sc_pintr = intr;
1669 sc->sc_parg = arg;
1670
1671 /* Compute drain timeout. */
1672 sc->sc_pdrain = (blksize * NBBY * hz) /
1673 (param->sample_rate * param->channels *
1674 param->precision) + 2; /* slop */
1675
1676 /* DMA transfer count (in `words'!) reload using 2's complement. */
1677 blksize = -(blksize >> 1);
1678 eso_write_mixreg(sc, ESO_MIXREG_A2TCRLO, blksize & 0xff);
1679 eso_write_mixreg(sc, ESO_MIXREG_A2TCRHI, blksize >> 8);
1680
1681 /* Update DAC to reflect DMA count and audio parameters. */
1682 /* Note: we cache A2C2 in order to avoid r/m/w at interrupt time. */
1683 if (param->precision == 16)
1684 sc->sc_a2c2 |= ESO_MIXREG_A2C2_16BIT;
1685 else
1686 sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_16BIT;
1687 if (param->channels == 2)
1688 sc->sc_a2c2 |= ESO_MIXREG_A2C2_STEREO;
1689 else
1690 sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_STEREO;
1691 if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1692 param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1693 sc->sc_a2c2 |= ESO_MIXREG_A2C2_SIGNED;
1694 else
1695 sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_SIGNED;
1696 /* Unmask IRQ. */
1697 sc->sc_a2c2 |= ESO_MIXREG_A2C2_IRQM;
1698 eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
1699
1700 /* Set up DMA controller. */
1701 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAA,
1702 DMAADDR(ed));
1703 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAC,
1704 (uint8_t *)end - (uint8_t *)start);
1705 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
1706 ESO_IO_A2DMAM_DMAENB | ESO_IO_A2DMAM_AUTO);
1707
1708 /* Start DMA. */
1709 a2c1 = eso_read_mixreg(sc, ESO_MIXREG_A2C1);
1710 a2c1 &= ~ESO_MIXREG_A2C1_RESV0; /* Paranoia? XXX bit 5 */
1711 a2c1 |= ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB |
1712 ESO_MIXREG_A2C1_AUTO;
1713 eso_write_mixreg(sc, ESO_MIXREG_A2C1, a2c1);
1714
1715 return 0;
1716 }
1717
1718 static int
1719 eso_trigger_input(void *hdl, void *start, void *end, int blksize,
1720 void (*intr)(void *), void *arg, const audio_params_t *param)
1721 {
1722 struct eso_softc *sc;
1723 struct eso_dma *ed;
1724 uint8_t actl, a1c1;
1725
1726 sc = hdl;
1727 DPRINTF((
1728 "%s: trigger_input: start %p, end %p, blksize %d, intr %p(%p)\n",
1729 sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1730 DPRINTF(("%s: param: rate %u, encoding %u, precision %u, channels %u\n",
1731 sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1732 param->precision, param->channels));
1733
1734 /*
1735 * If we failed to configure the Audio 1 DMA controller, bail here
1736 * while retaining availability of the DAC direction (in Audio 2).
1737 */
1738 if (!sc->sc_dmac_configured)
1739 return EIO;
1740
1741 /* Find DMA buffer. */
1742 for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1743 ed = ed->ed_next)
1744 continue;
1745 if (ed == NULL) {
1746 printf("%s: trigger_output: bad addr %p\n",
1747 sc->sc_dev.dv_xname, start);
1748 return EINVAL;
1749 }
1750 DPRINTF(("%s: dmaaddr %lx\n",
1751 sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1752
1753 sc->sc_rintr = intr;
1754 sc->sc_rarg = arg;
1755
1756 /* Compute drain timeout. */
1757 sc->sc_rdrain = (blksize * NBBY * hz) /
1758 (param->sample_rate * param->channels *
1759 param->precision) + 2; /* slop */
1760
1761 /* Set up ADC DMA converter parameters. */
1762 actl = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
1763 if (param->channels == 2) {
1764 actl &= ~ESO_CTLREG_ACTL_MONO;
1765 actl |= ESO_CTLREG_ACTL_STEREO;
1766 } else {
1767 actl &= ~ESO_CTLREG_ACTL_STEREO;
1768 actl |= ESO_CTLREG_ACTL_MONO;
1769 }
1770 eso_write_ctlreg(sc, ESO_CTLREG_ACTL, actl);
1771
1772 /* Set up Transfer Type: maybe move to attach time? */
1773 eso_write_ctlreg(sc, ESO_CTLREG_A1TT, ESO_CTLREG_A1TT_DEMAND4);
1774
1775 /* DMA transfer count reload using 2's complement. */
1776 blksize = -blksize;
1777 eso_write_ctlreg(sc, ESO_CTLREG_A1TCRLO, blksize & 0xff);
1778 eso_write_ctlreg(sc, ESO_CTLREG_A1TCRHI, blksize >> 8);
1779
1780 /* Set up and enable Audio 1 DMA FIFO. */
1781 a1c1 = ESO_CTLREG_A1C1_RESV1 | ESO_CTLREG_A1C1_FIFOENB;
1782 if (param->precision == 16)
1783 a1c1 |= ESO_CTLREG_A1C1_16BIT;
1784 if (param->channels == 2)
1785 a1c1 |= ESO_CTLREG_A1C1_STEREO;
1786 else
1787 a1c1 |= ESO_CTLREG_A1C1_MONO;
1788 if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1789 param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1790 a1c1 |= ESO_CTLREG_A1C1_SIGNED;
1791 eso_write_ctlreg(sc, ESO_CTLREG_A1C1, a1c1);
1792
1793 /* Set up ADC IRQ/DRQ parameters. */
1794 eso_write_ctlreg(sc, ESO_CTLREG_LAIC,
1795 ESO_CTLREG_LAIC_PINENB | ESO_CTLREG_LAIC_EXTENB);
1796 eso_write_ctlreg(sc, ESO_CTLREG_DRQCTL,
1797 ESO_CTLREG_DRQCTL_ENB1 | ESO_CTLREG_DRQCTL_EXTENB);
1798
1799 /* Set up and enable DMA controller. */
1800 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_CLEAR, 0);
1801 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
1802 ESO_DMAC_MASK_MASK);
1803 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
1804 DMA37MD_WRITE | DMA37MD_LOOP | DMA37MD_DEMAND);
1805 bus_space_write_4(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAA,
1806 DMAADDR(ed));
1807 bus_space_write_2(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAC,
1808 (uint8_t *)end - (uint8_t *)start - 1);
1809 bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK, 0);
1810
1811 /* Start DMA. */
1812 eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
1813 ESO_CTLREG_A1C2_DMAENB | ESO_CTLREG_A1C2_READ |
1814 ESO_CTLREG_A1C2_AUTO | ESO_CTLREG_A1C2_ADC);
1815
1816 return 0;
1817 }
1818
1819 /*
1820 * Mixer utility functions.
1821 */
1822 static int
1823 eso_set_recsrc(struct eso_softc *sc, unsigned int recsrc)
1824 {
1825 mixer_devinfo_t di;
1826 int i;
1827
1828 di.index = ESO_RECORD_SOURCE;
1829 if (eso_query_devinfo(sc, &di) != 0)
1830 panic("eso_set_recsrc: eso_query_devinfo failed");
1831
1832 for (i = 0; i < di.un.e.num_mem; i++) {
1833 if (recsrc == di.un.e.member[i].ord) {
1834 eso_write_mixreg(sc, ESO_MIXREG_ERS, recsrc);
1835 sc->sc_recsrc = recsrc;
1836 return 0;
1837 }
1838 }
1839
1840 return EINVAL;
1841 }
1842
1843 static int
1844 eso_set_monooutsrc(struct eso_softc *sc, unsigned int monooutsrc)
1845 {
1846 mixer_devinfo_t di;
1847 int i;
1848 uint8_t mpm;
1849
1850 di.index = ESO_MONOOUT_SOURCE;
1851 if (eso_query_devinfo(sc, &di) != 0)
1852 panic("eso_set_monooutsrc: eso_query_devinfo failed");
1853
1854 for (i = 0; i < di.un.e.num_mem; i++) {
1855 if (monooutsrc == di.un.e.member[i].ord) {
1856 mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1857 mpm &= ~ESO_MIXREG_MPM_MOMASK;
1858 mpm |= monooutsrc;
1859 eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1860 sc->sc_monooutsrc = monooutsrc;
1861 return 0;
1862 }
1863 }
1864
1865 return EINVAL;
1866 }
1867
1868 static int
1869 eso_set_monoinbypass(struct eso_softc *sc, unsigned int monoinbypass)
1870 {
1871 mixer_devinfo_t di;
1872 int i;
1873 uint8_t mpm;
1874
1875 di.index = ESO_MONOIN_BYPASS;
1876 if (eso_query_devinfo(sc, &di) != 0)
1877 panic("eso_set_monoinbypass: eso_query_devinfo failed");
1878
1879 for (i = 0; i < di.un.e.num_mem; i++) {
1880 if (monoinbypass == di.un.e.member[i].ord) {
1881 mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1882 mpm &= ~(ESO_MIXREG_MPM_MOMASK | ESO_MIXREG_MPM_RESV0);
1883 mpm |= (monoinbypass ? ESO_MIXREG_MPM_MIBYPASS : 0);
1884 eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1885 sc->sc_monoinbypass = monoinbypass;
1886 return 0;
1887 }
1888 }
1889
1890 return EINVAL;
1891 }
1892
1893 static int
1894 eso_set_preamp(struct eso_softc *sc, unsigned int preamp)
1895 {
1896 mixer_devinfo_t di;
1897 int i;
1898 uint8_t mpm;
1899
1900 di.index = ESO_MIC_PREAMP;
1901 if (eso_query_devinfo(sc, &di) != 0)
1902 panic("eso_set_preamp: eso_query_devinfo failed");
1903
1904 for (i = 0; i < di.un.e.num_mem; i++) {
1905 if (preamp == di.un.e.member[i].ord) {
1906 mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1907 mpm &= ~(ESO_MIXREG_MPM_PREAMP | ESO_MIXREG_MPM_RESV0);
1908 mpm |= (preamp ? ESO_MIXREG_MPM_PREAMP : 0);
1909 eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1910 sc->sc_preamp = preamp;
1911 return 0;
1912 }
1913 }
1914
1915 return EINVAL;
1916 }
1917
1918 /*
1919 * Reload Master Volume and Mute values in softc from mixer; used when
1920 * those have previously been invalidated by use of hardware volume controls.
1921 */
1922 static void
1923 eso_reload_master_vol(struct eso_softc *sc)
1924 {
1925 uint8_t mv;
1926
1927 mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
1928 sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] =
1929 (mv & ~ESO_MIXREG_LMVM_MUTE) << 2;
1930 mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
1931 sc->sc_gain[ESO_MASTER_VOL][ESO_RIGHT] =
1932 (mv & ~ESO_MIXREG_RMVM_MUTE) << 2;
1933 /* Currently both channels are muted simultaneously; either is OK. */
1934 sc->sc_mvmute = (mv & ESO_MIXREG_RMVM_MUTE) != 0;
1935 }
1936
1937 static void
1938 eso_set_gain(struct eso_softc *sc, unsigned int port)
1939 {
1940 uint8_t mixreg, tmp;
1941
1942 switch (port) {
1943 case ESO_DAC_PLAY_VOL:
1944 mixreg = ESO_MIXREG_PVR_A2;
1945 break;
1946 case ESO_MIC_PLAY_VOL:
1947 mixreg = ESO_MIXREG_PVR_MIC;
1948 break;
1949 case ESO_LINE_PLAY_VOL:
1950 mixreg = ESO_MIXREG_PVR_LINE;
1951 break;
1952 case ESO_SYNTH_PLAY_VOL:
1953 mixreg = ESO_MIXREG_PVR_SYNTH;
1954 break;
1955 case ESO_CD_PLAY_VOL:
1956 mixreg = ESO_MIXREG_PVR_CD;
1957 break;
1958 case ESO_AUXB_PLAY_VOL:
1959 mixreg = ESO_MIXREG_PVR_AUXB;
1960 break;
1961
1962 case ESO_DAC_REC_VOL:
1963 mixreg = ESO_MIXREG_RVR_A2;
1964 break;
1965 case ESO_MIC_REC_VOL:
1966 mixreg = ESO_MIXREG_RVR_MIC;
1967 break;
1968 case ESO_LINE_REC_VOL:
1969 mixreg = ESO_MIXREG_RVR_LINE;
1970 break;
1971 case ESO_SYNTH_REC_VOL:
1972 mixreg = ESO_MIXREG_RVR_SYNTH;
1973 break;
1974 case ESO_CD_REC_VOL:
1975 mixreg = ESO_MIXREG_RVR_CD;
1976 break;
1977 case ESO_AUXB_REC_VOL:
1978 mixreg = ESO_MIXREG_RVR_AUXB;
1979 break;
1980 case ESO_MONO_PLAY_VOL:
1981 mixreg = ESO_MIXREG_PVR_MONO;
1982 break;
1983 case ESO_MONO_REC_VOL:
1984 mixreg = ESO_MIXREG_RVR_MONO;
1985 break;
1986
1987 case ESO_PCSPEAKER_VOL:
1988 /* Special case - only 3-bit, mono, and reserved bits. */
1989 tmp = eso_read_mixreg(sc, ESO_MIXREG_PCSVR);
1990 tmp &= ESO_MIXREG_PCSVR_RESV;
1991 /* Map bits 7:5 -> 2:0. */
1992 tmp |= (sc->sc_gain[port][ESO_LEFT] >> 5);
1993 eso_write_mixreg(sc, ESO_MIXREG_PCSVR, tmp);
1994 return;
1995
1996 case ESO_MASTER_VOL:
1997 /* Special case - separate regs, and 6-bit precision. */
1998 /* Map bits 7:2 -> 5:0, reflect mute settings. */
1999 eso_write_mixreg(sc, ESO_MIXREG_LMVM,
2000 (sc->sc_gain[port][ESO_LEFT] >> 2) |
2001 (sc->sc_mvmute ? ESO_MIXREG_LMVM_MUTE : 0x00));
2002 eso_write_mixreg(sc, ESO_MIXREG_RMVM,
2003 (sc->sc_gain[port][ESO_RIGHT] >> 2) |
2004 (sc->sc_mvmute ? ESO_MIXREG_RMVM_MUTE : 0x00));
2005 return;
2006
2007 case ESO_SPATIALIZER:
2008 /* Special case - only `mono', and higher precision. */
2009 eso_write_mixreg(sc, ESO_MIXREG_SPATLVL,
2010 sc->sc_gain[port][ESO_LEFT]);
2011 return;
2012
2013 case ESO_RECORD_VOL:
2014 /* Very Special case, controller register. */
2015 eso_write_ctlreg(sc, ESO_CTLREG_RECLVL,ESO_4BIT_GAIN_TO_STEREO(
2016 sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
2017 return;
2018
2019 default:
2020 #ifdef DIAGNOSTIC
2021 panic("eso_set_gain: bad port %u", port);
2022 /* NOTREACHED */
2023 #else
2024 return;
2025 #endif
2026 }
2027
2028 eso_write_mixreg(sc, mixreg, ESO_4BIT_GAIN_TO_STEREO(
2029 sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
2030 }
2031