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