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