1 /* $NetBSD: ess.c,v 1.70 2006/08/30 01:24:40 christos Exp $ */ 2 3 /* 4 * Copyright 1997 5 * Digital Equipment Corporation. All rights reserved. 6 * 7 * This software is furnished under license and may be used and 8 * copied only in accordance with the following terms and conditions. 9 * Subject to these conditions, you may download, copy, install, 10 * use, modify and distribute this software in source and/or binary 11 * form. No title or ownership is transferred hereby. 12 * 13 * 1) Any source code used, modified or distributed must reproduce 14 * and retain this copyright notice and list of conditions as 15 * they appear in the source file. 16 * 17 * 2) No right is granted to use any trade name, trademark, or logo of 18 * Digital Equipment Corporation. Neither the "Digital Equipment 19 * Corporation" name nor any trademark or logo of Digital Equipment 20 * Corporation may be used to endorse or promote products derived 21 * from this software without the prior written permission of 22 * Digital Equipment Corporation. 23 * 24 * 3) This software is provided "AS-IS" and any express or implied 25 * warranties, including but not limited to, any implied warranties 26 * of merchantability, fitness for a particular purpose, or 27 * non-infringement are disclaimed. In no event shall DIGITAL be 28 * liable for any damages whatsoever, and in particular, DIGITAL 29 * shall not be liable for special, indirect, consequential, or 30 * incidental damages or damages for lost profits, loss of 31 * revenue or loss of use, whether such damages arise in contract, 32 * negligence, tort, under statute, in equity, at law or otherwise, 33 * even if advised of the possibility of such damage. 34 */ 35 36 /* 37 **++ 38 ** 39 ** ess.c 40 ** 41 ** FACILITY: 42 ** 43 ** DIGITAL Network Appliance Reference Design (DNARD) 44 ** 45 ** MODULE DESCRIPTION: 46 ** 47 ** This module contains the device driver for the ESS 48 ** Technologies 1888/1887/888 sound chip. The code in sbdsp.c was 49 ** used as a reference point when implementing this driver. 50 ** 51 ** AUTHORS: 52 ** 53 ** Blair Fidler Software Engineering Australia 54 ** Gold Coast, Australia. 55 ** 56 ** CREATION DATE: 57 ** 58 ** March 10, 1997. 59 ** 60 ** MODIFICATION HISTORY: 61 ** 62 ** Heavily modified by Lennart Augustsson and Charles M. Hannum for 63 ** bus_dma, changes to audio interface, and many bug fixes. 64 ** ESS1788 support by Nathan J. Williams and Charles M. Hannum. 65 **-- 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: ess.c,v 1.70 2006/08/30 01:24:40 christos Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/errno.h> 74 #include <sys/ioctl.h> 75 #include <sys/syslog.h> 76 #include <sys/device.h> 77 #include <sys/proc.h> 78 #include <sys/kernel.h> 79 80 #include <machine/cpu.h> 81 #include <machine/intr.h> 82 #include <machine/bus.h> 83 84 #include <sys/audioio.h> 85 #include <dev/audio_if.h> 86 #include <dev/auconv.h> 87 #include <dev/mulaw.h> 88 89 #include <dev/isa/isavar.h> 90 #include <dev/isa/isadmavar.h> 91 92 #include <dev/isa/essvar.h> 93 #include <dev/isa/essreg.h> 94 95 #include "joy_ess.h" 96 97 #ifdef AUDIO_DEBUG 98 #define DPRINTF(x) if (essdebug) printf x 99 #define DPRINTFN(n,x) if (essdebug>(n)) printf x 100 int essdebug = 0; 101 #else 102 #define DPRINTF(x) 103 #define DPRINTFN(n,x) 104 #endif 105 106 #if 0 107 unsigned uuu; 108 #define EREAD1(t, h, a) (uuu=bus_space_read_1(t, h, a),printf("EREAD %02x=%02x\n", ((int)h&0xfff)+a, uuu),uuu) 109 #define EWRITE1(t, h, a, d) (printf("EWRITE %02x=%02x\n", ((int)h & 0xfff)+a, d), bus_space_write_1(t, h, a, d)) 110 #else 111 #define EREAD1(t, h, a) bus_space_read_1(t, h, a) 112 #define EWRITE1(t, h, a, d) bus_space_write_1(t, h, a, d) 113 #endif 114 115 116 int ess_setup_sc(struct ess_softc *, int); 117 118 int ess_open(void *, int); 119 void ess_close(void *); 120 int ess_getdev(void *, struct audio_device *); 121 int ess_drain(void *); 122 123 int ess_query_encoding(void *, struct audio_encoding *); 124 125 int ess_set_params(void *, int, int, audio_params_t *, 126 audio_params_t *, stream_filter_list_t *, stream_filter_list_t *); 127 128 int ess_round_blocksize(void *, int, int, const audio_params_t *); 129 130 int ess_audio1_trigger_output(void *, void *, void *, int, 131 void (*)(void *), void *, const audio_params_t *); 132 int ess_audio2_trigger_output(void *, void *, void *, int, 133 void (*)(void *), void *, const audio_params_t *); 134 int ess_audio1_trigger_input(void *, void *, void *, int, 135 void (*)(void *), void *, const audio_params_t *); 136 int ess_audio1_halt(void *); 137 int ess_audio2_halt(void *); 138 int ess_audio1_intr(void *); 139 int ess_audio2_intr(void *); 140 void ess_audio1_poll(void *); 141 void ess_audio2_poll(void *); 142 143 int ess_speaker_ctl(void *, int); 144 145 int ess_getdev(void *, struct audio_device *); 146 147 int ess_set_port(void *, mixer_ctrl_t *); 148 int ess_get_port(void *, mixer_ctrl_t *); 149 150 void *ess_malloc(void *, int, size_t, struct malloc_type *, int); 151 void ess_free(void *, void *, struct malloc_type *); 152 size_t ess_round_buffersize(void *, int, size_t); 153 paddr_t ess_mappage(void *, void *, off_t, int); 154 155 156 int ess_query_devinfo(void *, mixer_devinfo_t *); 157 int ess_1788_get_props(void *); 158 int ess_1888_get_props(void *); 159 160 void ess_speaker_on(struct ess_softc *); 161 void ess_speaker_off(struct ess_softc *); 162 163 void ess_config_irq(struct ess_softc *); 164 void ess_config_drq(struct ess_softc *); 165 void ess_setup(struct ess_softc *); 166 int ess_identify(struct ess_softc *); 167 168 int ess_reset(struct ess_softc *); 169 void ess_set_gain(struct ess_softc *, int, int); 170 int ess_set_in_port(struct ess_softc *, int); 171 int ess_set_in_ports(struct ess_softc *, int); 172 u_int ess_srtotc(u_int); 173 u_int ess_srtofc(u_int); 174 u_char ess_get_dsp_status(struct ess_softc *); 175 u_char ess_dsp_read_ready(struct ess_softc *); 176 u_char ess_dsp_write_ready(struct ess_softc *); 177 int ess_rdsp(struct ess_softc *); 178 int ess_wdsp(struct ess_softc *, u_char); 179 u_char ess_read_x_reg(struct ess_softc *, u_char); 180 int ess_write_x_reg(struct ess_softc *, u_char, u_char); 181 void ess_clear_xreg_bits(struct ess_softc *, u_char, u_char); 182 void ess_set_xreg_bits(struct ess_softc *, u_char, u_char); 183 u_char ess_read_mix_reg(struct ess_softc *, u_char); 184 void ess_write_mix_reg(struct ess_softc *, u_char, u_char); 185 void ess_clear_mreg_bits(struct ess_softc *, u_char, u_char); 186 void ess_set_mreg_bits(struct ess_softc *, u_char, u_char); 187 void ess_read_multi_mix_reg(struct ess_softc *, u_char, u_int8_t *, bus_size_t); 188 189 static const char *essmodel[] = { 190 "unsupported", 191 192 "688", 193 "1688", 194 "1788", 195 "1868", 196 "1869", 197 "1878", 198 "1879", 199 200 "888", 201 "1887", 202 "1888", 203 }; 204 205 struct audio_device ess_device = { 206 "ESS Technology", 207 "x", 208 "ess" 209 }; 210 211 /* 212 * Define our interface to the higher level audio driver. 213 */ 214 215 const struct audio_hw_if ess_1788_hw_if = { 216 ess_open, 217 ess_close, 218 ess_drain, 219 ess_query_encoding, 220 ess_set_params, 221 ess_round_blocksize, 222 NULL, 223 NULL, 224 NULL, 225 NULL, 226 NULL, 227 ess_audio1_halt, 228 ess_audio1_halt, 229 ess_speaker_ctl, 230 ess_getdev, 231 NULL, 232 ess_set_port, 233 ess_get_port, 234 ess_query_devinfo, 235 ess_malloc, 236 ess_free, 237 ess_round_buffersize, 238 ess_mappage, 239 ess_1788_get_props, 240 ess_audio1_trigger_output, 241 ess_audio1_trigger_input, 242 NULL, 243 NULL, 244 }; 245 246 const struct audio_hw_if ess_1888_hw_if = { 247 ess_open, 248 ess_close, 249 ess_drain, 250 ess_query_encoding, 251 ess_set_params, 252 ess_round_blocksize, 253 NULL, 254 NULL, 255 NULL, 256 NULL, 257 NULL, 258 ess_audio2_halt, 259 ess_audio1_halt, 260 ess_speaker_ctl, 261 ess_getdev, 262 NULL, 263 ess_set_port, 264 ess_get_port, 265 ess_query_devinfo, 266 ess_malloc, 267 ess_free, 268 ess_round_buffersize, 269 ess_mappage, 270 ess_1888_get_props, 271 ess_audio2_trigger_output, 272 ess_audio1_trigger_input, 273 NULL, 274 NULL, 275 }; 276 277 #define ESS_NFORMATS 8 278 static const struct audio_format ess_formats[ESS_NFORMATS] = { 279 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, 280 2, AUFMT_STEREO, 0, {ESS_MINRATE, ESS_MAXRATE}}, 281 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, 282 1, AUFMT_MONAURAL, 0, {ESS_MINRATE, ESS_MAXRATE}}, 283 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16, 284 2, AUFMT_STEREO, 0, {ESS_MINRATE, ESS_MAXRATE}}, 285 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16, 286 1, AUFMT_MONAURAL, 0, {ESS_MINRATE, ESS_MAXRATE}}, 287 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, 288 2, AUFMT_STEREO, 0, {ESS_MINRATE, ESS_MAXRATE}}, 289 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, 290 1, AUFMT_MONAURAL, 0, {ESS_MINRATE, ESS_MAXRATE}}, 291 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8, 292 2, AUFMT_STEREO, 0, {ESS_MINRATE, ESS_MAXRATE}}, 293 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8, 294 1, AUFMT_MONAURAL, 0, {ESS_MINRATE, ESS_MAXRATE}}, 295 }; 296 297 #ifdef AUDIO_DEBUG 298 void ess_printsc(struct ess_softc *); 299 void ess_dump_mixer(struct ess_softc *); 300 301 void 302 ess_printsc(struct ess_softc *sc) 303 { 304 int i; 305 306 printf("iobase 0x%x outport %u inport %u speaker %s\n", 307 sc->sc_iobase, sc->out_port, 308 sc->in_port, sc->spkr_state ? "on" : "off"); 309 310 printf("audio1: DMA chan %d irq %d nintr %lu intr %p arg %p\n", 311 sc->sc_audio1.drq, sc->sc_audio1.irq, sc->sc_audio1.nintr, 312 sc->sc_audio1.intr, sc->sc_audio1.arg); 313 314 if (!ESS_USE_AUDIO1(sc->sc_model)) { 315 printf("audio2: DMA chan %d irq %d nintr %lu intr %p arg %p\n", 316 sc->sc_audio2.drq, sc->sc_audio2.irq, sc->sc_audio2.nintr, 317 sc->sc_audio2.intr, sc->sc_audio2.arg); 318 } 319 320 printf("gain:"); 321 for (i = 0; i < sc->ndevs; i++) 322 printf(" %u,%u", sc->gain[i][ESS_LEFT], sc->gain[i][ESS_RIGHT]); 323 printf("\n"); 324 } 325 326 void 327 ess_dump_mixer(struct ess_softc *sc) 328 { 329 330 printf("ESS_DAC_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 331 0x7C, ess_read_mix_reg(sc, 0x7C)); 332 printf("ESS_MIC_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 333 0x1A, ess_read_mix_reg(sc, 0x1A)); 334 printf("ESS_LINE_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 335 0x3E, ess_read_mix_reg(sc, 0x3E)); 336 printf("ESS_SYNTH_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 337 0x36, ess_read_mix_reg(sc, 0x36)); 338 printf("ESS_CD_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 339 0x38, ess_read_mix_reg(sc, 0x38)); 340 printf("ESS_AUXB_PLAY_VOL: mix reg 0x%02x=0x%02x\n", 341 0x3A, ess_read_mix_reg(sc, 0x3A)); 342 printf("ESS_MASTER_VOL: mix reg 0x%02x=0x%02x\n", 343 0x32, ess_read_mix_reg(sc, 0x32)); 344 printf("ESS_PCSPEAKER_VOL: mix reg 0x%02x=0x%02x\n", 345 0x3C, ess_read_mix_reg(sc, 0x3C)); 346 printf("ESS_DAC_REC_VOL: mix reg 0x%02x=0x%02x\n", 347 0x69, ess_read_mix_reg(sc, 0x69)); 348 printf("ESS_MIC_REC_VOL: mix reg 0x%02x=0x%02x\n", 349 0x68, ess_read_mix_reg(sc, 0x68)); 350 printf("ESS_LINE_REC_VOL: mix reg 0x%02x=0x%02x\n", 351 0x6E, ess_read_mix_reg(sc, 0x6E)); 352 printf("ESS_SYNTH_REC_VOL: mix reg 0x%02x=0x%02x\n", 353 0x6B, ess_read_mix_reg(sc, 0x6B)); 354 printf("ESS_CD_REC_VOL: mix reg 0x%02x=0x%02x\n", 355 0x6A, ess_read_mix_reg(sc, 0x6A)); 356 printf("ESS_AUXB_REC_VOL: mix reg 0x%02x=0x%02x\n", 357 0x6C, ess_read_mix_reg(sc, 0x6C)); 358 printf("ESS_RECORD_VOL: x reg 0x%02x=0x%02x\n", 359 0xB4, ess_read_x_reg(sc, 0xB4)); 360 printf("Audio 1 play vol (unused): mix reg 0x%02x=0x%02x\n", 361 0x14, ess_read_mix_reg(sc, 0x14)); 362 363 printf("ESS_MIC_PREAMP: x reg 0x%02x=0x%02x\n", 364 ESS_XCMD_PREAMP_CTRL, ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL)); 365 printf("ESS_RECORD_MONITOR: x reg 0x%02x=0x%02x\n", 366 ESS_XCMD_AUDIO_CTRL, ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL)); 367 printf("Record source: mix reg 0x%02x=0x%02x, 0x%02x=0x%02x\n", 368 ESS_MREG_ADC_SOURCE, ess_read_mix_reg(sc, ESS_MREG_ADC_SOURCE), 369 ESS_MREG_AUDIO2_CTRL2, ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2)); 370 } 371 372 #endif 373 374 /* 375 * Configure the ESS chip for the desired audio base address. 376 */ 377 int 378 ess_config_addr(struct ess_softc *sc) 379 { 380 int iobase; 381 bus_space_tag_t iot; 382 /* 383 * Configure using the System Control Register method. This 384 * method is used when the AMODE line is tied high, which is 385 * the case for the Shark, but not for the evaluation board. 386 */ 387 bus_space_handle_t scr_access_ioh; 388 bus_space_handle_t scr_ioh; 389 u_short scr_value; 390 391 iobase = sc->sc_iobase; 392 iot = sc->sc_iot; 393 /* 394 * Set the SCR bit to enable audio. 395 */ 396 scr_value = ESS_SCR_AUDIO_ENABLE; 397 398 /* 399 * Set the SCR bits necessary to select the specified audio 400 * base address. 401 */ 402 switch(iobase) { 403 case 0x220: 404 scr_value |= ESS_SCR_AUDIO_220; 405 break; 406 case 0x230: 407 scr_value |= ESS_SCR_AUDIO_230; 408 break; 409 case 0x240: 410 scr_value |= ESS_SCR_AUDIO_240; 411 break; 412 case 0x250: 413 scr_value |= ESS_SCR_AUDIO_250; 414 break; 415 default: 416 printf("ess: configured iobase 0x%x invalid\n", iobase); 417 return 1; 418 break; 419 } 420 421 /* 422 * Get a mapping for the System Control Register (SCR) access 423 * registers and the SCR data registers. 424 */ 425 if (bus_space_map(iot, ESS_SCR_ACCESS_BASE, ESS_SCR_ACCESS_PORTS, 426 0, &scr_access_ioh)) { 427 printf("ess: can't map SCR access registers\n"); 428 return 1; 429 } 430 if (bus_space_map(iot, ESS_SCR_BASE, ESS_SCR_PORTS, 431 0, &scr_ioh)) { 432 printf("ess: can't map SCR registers\n"); 433 bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS); 434 return 1; 435 } 436 437 /* Unlock the SCR. */ 438 EWRITE1(iot, scr_access_ioh, ESS_SCR_UNLOCK, 0); 439 440 /* Write the base address information into SCR[0]. */ 441 EWRITE1(iot, scr_ioh, ESS_SCR_INDEX, 0); 442 EWRITE1(iot, scr_ioh, ESS_SCR_DATA, scr_value); 443 444 /* Lock the SCR. */ 445 EWRITE1(iot, scr_access_ioh, ESS_SCR_LOCK, 0); 446 447 /* Unmap the SCR access ports and the SCR data ports. */ 448 bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS); 449 bus_space_unmap(iot, scr_ioh, ESS_SCR_PORTS); 450 451 return 0; 452 } 453 454 455 /* 456 * Configure the ESS chip for the desired IRQ and DMA channels. 457 * ESS ISA 458 * -------- 459 * IRQA irq9 460 * IRQB irq5 461 * IRQC irq7 462 * IRQD irq10 463 * IRQE irq15 464 * 465 * DRQA drq0 466 * DRQB drq1 467 * DRQC drq3 468 * DRQD drq5 469 */ 470 void 471 ess_config_irq(struct ess_softc *sc) 472 { 473 int v; 474 475 DPRINTFN(2,("ess_config_irq\n")); 476 477 if (sc->sc_model == ESS_1887 && 478 sc->sc_audio1.irq == sc->sc_audio2.irq && 479 sc->sc_audio1.irq != -1) { 480 /* Use new method, both interrupts are the same. */ 481 v = ESS_IS_SELECT_IRQ; /* enable intrs */ 482 switch (sc->sc_audio1.irq) { 483 case 5: 484 v |= ESS_IS_INTRB; 485 break; 486 case 7: 487 v |= ESS_IS_INTRC; 488 break; 489 case 9: 490 v |= ESS_IS_INTRA; 491 break; 492 case 10: 493 v |= ESS_IS_INTRD; 494 break; 495 case 15: 496 v |= ESS_IS_INTRE; 497 break; 498 #ifdef DIAGNOSTIC 499 default: 500 printf("ess_config_irq: configured irq %d not supported for Audio 1\n", 501 sc->sc_audio1.irq); 502 return; 503 #endif 504 } 505 /* Set the IRQ */ 506 ess_write_mix_reg(sc, ESS_MREG_INTR_ST, v); 507 return; 508 } 509 510 if (sc->sc_model == ESS_1887) { 511 /* Tell the 1887 to use the old interrupt method. */ 512 ess_write_mix_reg(sc, ESS_MREG_INTR_ST, ESS_IS_ES1888); 513 } 514 515 if (sc->sc_audio1.polled) { 516 /* Turn off Audio1 interrupts. */ 517 v = 0; 518 } else { 519 /* Configure Audio 1 for the appropriate IRQ line. */ 520 v = ESS_IRQ_CTRL_MASK | ESS_IRQ_CTRL_EXT; /* All intrs on */ 521 switch (sc->sc_audio1.irq) { 522 case 5: 523 v |= ESS_IRQ_CTRL_INTRB; 524 break; 525 case 7: 526 v |= ESS_IRQ_CTRL_INTRC; 527 break; 528 case 9: 529 v |= ESS_IRQ_CTRL_INTRA; 530 break; 531 case 10: 532 v |= ESS_IRQ_CTRL_INTRD; 533 break; 534 #ifdef DIAGNOSTIC 535 default: 536 printf("ess: configured irq %d not supported for Audio 1\n", 537 sc->sc_audio1.irq); 538 return; 539 #endif 540 } 541 } 542 ess_write_x_reg(sc, ESS_XCMD_IRQ_CTRL, v); 543 544 if (ESS_USE_AUDIO1(sc->sc_model)) 545 return; 546 547 if (sc->sc_audio2.polled) { 548 /* Turn off Audio2 interrupts. */ 549 ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 550 ESS_AUDIO2_CTRL2_IRQ2_ENABLE); 551 } else { 552 /* Audio2 is hardwired to INTRE in this mode. */ 553 ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 554 ESS_AUDIO2_CTRL2_IRQ2_ENABLE); 555 } 556 } 557 558 559 void 560 ess_config_drq(struct ess_softc *sc) 561 { 562 int v; 563 564 DPRINTFN(2,("ess_config_drq\n")); 565 566 /* Configure Audio 1 (record) for DMA on the appropriate channel. */ 567 v = ESS_DRQ_CTRL_PU | ESS_DRQ_CTRL_EXT; 568 switch (sc->sc_audio1.drq) { 569 case 0: 570 v |= ESS_DRQ_CTRL_DRQA; 571 break; 572 case 1: 573 v |= ESS_DRQ_CTRL_DRQB; 574 break; 575 case 3: 576 v |= ESS_DRQ_CTRL_DRQC; 577 break; 578 #ifdef DIAGNOSTIC 579 default: 580 printf("ess_config_drq: configured DMA chan %d not supported for Audio 1\n", 581 sc->sc_audio1.drq); 582 return; 583 #endif 584 } 585 /* Set DRQ1 */ 586 ess_write_x_reg(sc, ESS_XCMD_DRQ_CTRL, v); 587 588 if (ESS_USE_AUDIO1(sc->sc_model)) 589 return; 590 591 /* Configure DRQ2 */ 592 v = ESS_AUDIO2_CTRL3_DRQ_PD; 593 switch (sc->sc_audio2.drq) { 594 case 0: 595 v |= ESS_AUDIO2_CTRL3_DRQA; 596 break; 597 case 1: 598 v |= ESS_AUDIO2_CTRL3_DRQB; 599 break; 600 case 3: 601 v |= ESS_AUDIO2_CTRL3_DRQC; 602 break; 603 case 5: 604 v |= ESS_AUDIO2_CTRL3_DRQD; 605 break; 606 #ifdef DIAGNOSTIC 607 default: 608 printf("ess_config_drq: configured DMA chan %d not supported for Audio 2\n", 609 sc->sc_audio2.drq); 610 return; 611 #endif 612 } 613 ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL3, v); 614 /* Enable DMA 2 */ 615 ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 616 ESS_AUDIO2_CTRL2_DMA_ENABLE); 617 } 618 619 /* 620 * Set up registers after a reset. 621 */ 622 void 623 ess_setup(struct ess_softc *sc) 624 { 625 626 ess_config_irq(sc); 627 ess_config_drq(sc); 628 629 DPRINTFN(2,("ess_setup: done\n")); 630 } 631 632 /* 633 * Determine the model of ESS chip we are talking to. Currently we 634 * only support ES1888, ES1887 and ES888. The method of determining 635 * the chip is based on the information on page 27 of the ES1887 data 636 * sheet. 637 * 638 * This routine sets the values of sc->sc_model and sc->sc_version. 639 */ 640 int 641 ess_identify(struct ess_softc *sc) 642 { 643 u_char reg1; 644 u_char reg2; 645 u_char reg3; 646 u_int8_t ident[4]; 647 648 sc->sc_model = ESS_UNSUPPORTED; 649 sc->sc_version = 0; 650 651 memset(ident, 0, sizeof(ident)); 652 653 /* 654 * 1. Check legacy ID bytes. These should be 0x68 0x8n, where 655 * n >= 8 for an ES1887 or an ES888. Other values indicate 656 * earlier (unsupported) chips. 657 */ 658 ess_wdsp(sc, ESS_ACMD_LEGACY_ID); 659 660 if ((reg1 = ess_rdsp(sc)) != 0x68) { 661 printf("ess: First ID byte wrong (0x%02x)\n", reg1); 662 return 1; 663 } 664 665 reg2 = ess_rdsp(sc); 666 if (((reg2 & 0xf0) != 0x80) || 667 ((reg2 & 0x0f) < 8)) { 668 sc->sc_model = ESS_688; 669 return 0; 670 } 671 672 /* 673 * Store the ID bytes as the version. 674 */ 675 sc->sc_version = (reg1 << 8) + reg2; 676 677 678 /* 679 * 2. Verify we can change bit 2 in mixer register 0x64. This 680 * should be possible on all supported chips. 681 */ 682 reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL); 683 reg2 = reg1 ^ 0x04; /* toggle bit 2 */ 684 685 ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2); 686 687 if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) != reg2) { 688 switch (sc->sc_version) { 689 case 0x688b: 690 sc->sc_model = ESS_1688; 691 break; 692 default: 693 printf("ess: Hardware error (unable to toggle bit 2 of mixer register 0x64)\n"); 694 return 1; 695 } 696 return 0; 697 } 698 699 /* 700 * Restore the original value of mixer register 0x64. 701 */ 702 ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1); 703 704 705 /* 706 * 3. Verify we can change the value of mixer register 707 * ESS_MREG_SAMPLE_RATE. 708 * This is possible on the 1888/1887/888, but not on the 1788. 709 * It is not necessary to restore the value of this mixer register. 710 */ 711 reg1 = ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE); 712 reg2 = reg1 ^ 0xff; /* toggle all bits */ 713 714 ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, reg2); 715 716 if (ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE) != reg2) { 717 /* If we got this far before failing, it's a 1788. */ 718 sc->sc_model = ESS_1788; 719 720 /* 721 * Identify ESS model for ES18[67]8. 722 */ 723 ess_read_multi_mix_reg(sc, 0x40, ident, sizeof(ident)); 724 if(ident[0] == 0x18) { 725 switch(ident[1]) { 726 case 0x68: 727 sc->sc_model = ESS_1868; 728 break; 729 case 0x78: 730 sc->sc_model = ESS_1878; 731 break; 732 } 733 } 734 735 return 0; 736 } 737 738 /* 739 * 4. Determine if we can change bit 5 in mixer register 0x64. 740 * This determines whether we have an ES1887: 741 * 742 * - can change indicates ES1887 743 * - can't change indicates ES1888 or ES888 744 */ 745 reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL); 746 reg2 = reg1 ^ 0x20; /* toggle bit 5 */ 747 748 ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2); 749 750 if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) == reg2) { 751 sc->sc_model = ESS_1887; 752 753 /* 754 * Restore the original value of mixer register 0x64. 755 */ 756 ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1); 757 758 /* 759 * Identify ESS model for ES18[67]9. 760 */ 761 ess_read_multi_mix_reg(sc, 0x40, ident, sizeof(ident)); 762 if(ident[0] == 0x18) { 763 switch(ident[1]) { 764 case 0x69: 765 sc->sc_model = ESS_1869; 766 break; 767 case 0x79: 768 sc->sc_model = ESS_1879; 769 break; 770 } 771 } 772 773 return 0; 774 } 775 776 /* 777 * 5. Determine if we can change the value of mixer 778 * register 0x69 independently of mixer register 779 * 0x68. This determines which chip we have: 780 * 781 * - can modify idependently indicates ES888 782 * - register 0x69 is an alias of 0x68 indicates ES1888 783 */ 784 reg1 = ess_read_mix_reg(sc, 0x68); 785 reg2 = ess_read_mix_reg(sc, 0x69); 786 reg3 = reg2 ^ 0xff; /* toggle all bits */ 787 788 /* 789 * Write different values to each register. 790 */ 791 ess_write_mix_reg(sc, 0x68, reg2); 792 ess_write_mix_reg(sc, 0x69, reg3); 793 794 if (ess_read_mix_reg(sc, 0x68) == reg2 && 795 ess_read_mix_reg(sc, 0x69) == reg3) 796 sc->sc_model = ESS_888; 797 else 798 sc->sc_model = ESS_1888; 799 800 /* 801 * Restore the original value of the registers. 802 */ 803 ess_write_mix_reg(sc, 0x68, reg1); 804 ess_write_mix_reg(sc, 0x69, reg2); 805 806 return 0; 807 } 808 809 810 int 811 ess_setup_sc(struct ess_softc *sc, int doinit) 812 { 813 814 callout_init(&sc->sc_poll1_ch); 815 callout_init(&sc->sc_poll2_ch); 816 817 /* Reset the chip. */ 818 if (ess_reset(sc) != 0) { 819 DPRINTF(("ess_setup_sc: couldn't reset chip\n")); 820 return 1; 821 } 822 823 /* Identify the ESS chip, and check that it is supported. */ 824 if (ess_identify(sc)) { 825 DPRINTF(("ess_setup_sc: couldn't identify\n")); 826 return 1; 827 } 828 829 return 0; 830 } 831 832 /* 833 * Probe for the ESS hardware. 834 */ 835 int 836 essmatch(struct ess_softc *sc) 837 { 838 if (!ESS_BASE_VALID(sc->sc_iobase)) { 839 printf("ess: configured iobase 0x%x invalid\n", sc->sc_iobase); 840 return 0; 841 } 842 843 if (ess_setup_sc(sc, 1)) 844 return 0; 845 846 if (sc->sc_model == ESS_UNSUPPORTED) { 847 DPRINTF(("ess: Unsupported model\n")); 848 return 0; 849 } 850 851 /* Check that requested DMA channels are valid and different. */ 852 if (!ESS_DRQ1_VALID(sc->sc_audio1.drq)) { 853 printf("ess: record drq %d invalid\n", sc->sc_audio1.drq); 854 return 0; 855 } 856 if (!isa_drq_isfree(sc->sc_ic, sc->sc_audio1.drq)) 857 return 0; 858 if (!ESS_USE_AUDIO1(sc->sc_model)) { 859 if (!ESS_DRQ2_VALID(sc->sc_audio2.drq)) { 860 printf("ess: play drq %d invalid\n", sc->sc_audio2.drq); 861 return 0; 862 } 863 if (sc->sc_audio1.drq == sc->sc_audio2.drq) { 864 printf("ess: play and record drq both %d\n", 865 sc->sc_audio1.drq); 866 return 0; 867 } 868 if (!isa_drq_isfree(sc->sc_ic, sc->sc_audio2.drq)) 869 return 0; 870 } 871 872 /* 873 * The 1887 has an additional IRQ mode where both channels are mapped 874 * to the same IRQ. 875 */ 876 if (sc->sc_model == ESS_1887 && 877 sc->sc_audio1.irq == sc->sc_audio2.irq && 878 sc->sc_audio1.irq != -1 && 879 ESS_IRQ12_VALID(sc->sc_audio1.irq)) 880 goto irq_not1888; 881 882 /* Check that requested IRQ lines are valid and different. */ 883 if (sc->sc_audio1.irq != -1 && 884 !ESS_IRQ1_VALID(sc->sc_audio1.irq)) { 885 printf("ess: record irq %d invalid\n", sc->sc_audio1.irq); 886 return 0; 887 } 888 if (!ESS_USE_AUDIO1(sc->sc_model)) { 889 if (sc->sc_audio2.irq != -1 && 890 !ESS_IRQ2_VALID(sc->sc_audio2.irq)) { 891 printf("ess: play irq %d invalid\n", sc->sc_audio2.irq); 892 return 0; 893 } 894 if (sc->sc_audio1.irq == sc->sc_audio2.irq && 895 sc->sc_audio1.irq != -1) { 896 printf("ess: play and record irq both %d\n", 897 sc->sc_audio1.irq); 898 return 0; 899 } 900 } 901 902 irq_not1888: 903 /* XXX should we check IRQs as well? */ 904 905 return 2; /* beat "sb" */ 906 } 907 908 909 /* 910 * Attach hardware to driver, attach hardware driver to audio 911 * pseudo-device driver. 912 */ 913 void 914 essattach(struct ess_softc *sc, int enablejoy) 915 { 916 struct audio_attach_args arg; 917 int i; 918 u_int v; 919 920 if (ess_setup_sc(sc, 0)) { 921 printf(": setup failed\n"); 922 return; 923 } 924 925 printf(": ESS Technology ES%s [version 0x%04x]\n", 926 essmodel[sc->sc_model], sc->sc_version); 927 928 sc->sc_audio1.polled = sc->sc_audio1.irq == -1; 929 if (!sc->sc_audio1.polled) { 930 sc->sc_audio1.ih = isa_intr_establish(sc->sc_ic, 931 sc->sc_audio1.irq, sc->sc_audio1.ist, IPL_AUDIO, 932 ess_audio1_intr, sc); 933 printf("%s: audio1 interrupting at irq %d\n", 934 sc->sc_dev.dv_xname, sc->sc_audio1.irq); 935 } else 936 printf("%s: audio1 polled\n", sc->sc_dev.dv_xname); 937 sc->sc_audio1.maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_audio1.drq); 938 939 if (isa_drq_alloc(sc->sc_ic, sc->sc_audio1.drq) != 0) { 940 printf("%s: can't reserve drq %d\n", 941 sc->sc_dev.dv_xname, sc->sc_audio1.drq); 942 return; 943 } 944 945 if (isa_dmamap_create(sc->sc_ic, sc->sc_audio1.drq, 946 sc->sc_audio1.maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) { 947 printf("%s: can't create map for drq %d\n", 948 sc->sc_dev.dv_xname, sc->sc_audio1.drq); 949 return; 950 } 951 952 if (!ESS_USE_AUDIO1(sc->sc_model)) { 953 sc->sc_audio2.polled = sc->sc_audio2.irq == -1; 954 if (!sc->sc_audio2.polled) { 955 sc->sc_audio2.ih = isa_intr_establish(sc->sc_ic, 956 sc->sc_audio2.irq, sc->sc_audio2.ist, IPL_AUDIO, 957 ess_audio2_intr, sc); 958 printf("%s: audio2 interrupting at irq %d\n", 959 sc->sc_dev.dv_xname, sc->sc_audio2.irq); 960 } else 961 printf("%s: audio2 polled\n", sc->sc_dev.dv_xname); 962 sc->sc_audio2.maxsize = isa_dmamaxsize(sc->sc_ic, 963 sc->sc_audio2.drq); 964 965 if (isa_drq_alloc(sc->sc_ic, sc->sc_audio2.drq) != 0) { 966 printf("%s: can't reserve drq %d\n", 967 sc->sc_dev.dv_xname, sc->sc_audio2.drq); 968 return; 969 } 970 971 if (isa_dmamap_create(sc->sc_ic, sc->sc_audio2.drq, 972 sc->sc_audio2.maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) { 973 printf("%s: can't create map for drq %d\n", 974 sc->sc_dev.dv_xname, sc->sc_audio2.drq); 975 return; 976 } 977 } 978 979 /* Do a hardware reset on the mixer. */ 980 ess_write_mix_reg(sc, ESS_MIX_RESET, ESS_MIX_RESET); 981 982 /* 983 * Set volume of Audio 1 to zero and disable Audio 1 DAC input 984 * to playback mixer, since playback is always through Audio 2. 985 */ 986 if (!ESS_USE_AUDIO1(sc->sc_model)) 987 ess_write_mix_reg(sc, ESS_MREG_VOLUME_VOICE, 0); 988 ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR); 989 990 if (ESS_USE_AUDIO1(sc->sc_model)) { 991 ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIC); 992 sc->in_port = ESS_SOURCE_MIC; 993 sc->ndevs = ESS_1788_NDEVS; 994 } else { 995 /* 996 * Set hardware record source to use output of the record 997 * mixer. We do the selection of record source in software by 998 * setting the gain of the unused sources to zero. (See 999 * ess_set_in_ports.) 1000 */ 1001 ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIXER); 1002 sc->in_mask = 1 << ESS_MIC_REC_VOL; 1003 sc->ndevs = ESS_1888_NDEVS; 1004 ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x10); 1005 ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x08); 1006 } 1007 1008 /* 1009 * Set gain on each mixer device to a sensible value. 1010 * Devices not normally used are turned off, and other devices 1011 * are set to 50% volume. 1012 */ 1013 for (i = 0; i < sc->ndevs; i++) { 1014 switch (i) { 1015 case ESS_MIC_PLAY_VOL: 1016 case ESS_LINE_PLAY_VOL: 1017 case ESS_CD_PLAY_VOL: 1018 case ESS_AUXB_PLAY_VOL: 1019 case ESS_DAC_REC_VOL: 1020 case ESS_LINE_REC_VOL: 1021 case ESS_SYNTH_REC_VOL: 1022 case ESS_CD_REC_VOL: 1023 case ESS_AUXB_REC_VOL: 1024 v = 0; 1025 break; 1026 default: 1027 v = ESS_4BIT_GAIN(AUDIO_MAX_GAIN / 2); 1028 break; 1029 } 1030 sc->gain[i][ESS_LEFT] = sc->gain[i][ESS_RIGHT] = v; 1031 ess_set_gain(sc, i, 1); 1032 } 1033 1034 ess_setup(sc); 1035 1036 /* Disable the speaker until the device is opened. */ 1037 ess_speaker_off(sc); 1038 sc->spkr_state = SPKR_OFF; 1039 1040 snprintf(ess_device.name, sizeof(ess_device.name), "ES%s", 1041 essmodel[sc->sc_model]); 1042 snprintf(ess_device.version, sizeof(ess_device.version), "0x%04x", 1043 sc->sc_version); 1044 1045 if (ESS_USE_AUDIO1(sc->sc_model)) 1046 audio_attach_mi(&ess_1788_hw_if, sc, &sc->sc_dev); 1047 else 1048 audio_attach_mi(&ess_1888_hw_if, sc, &sc->sc_dev); 1049 1050 arg.type = AUDIODEV_TYPE_OPL; 1051 arg.hwif = 0; 1052 arg.hdl = 0; 1053 (void)config_found(&sc->sc_dev, &arg, audioprint); 1054 1055 #if NJOY_ESS > 0 1056 if (sc->sc_model == ESS_1888 && enablejoy) { 1057 unsigned char m40; 1058 1059 m40 = ess_read_mix_reg(sc, 0x40); 1060 m40 |= 2; 1061 ess_write_mix_reg(sc, 0x40, m40); 1062 1063 arg.type = AUDIODEV_TYPE_AUX; 1064 (void)config_found(&sc->sc_dev, &arg, audioprint); 1065 } 1066 #endif 1067 1068 #ifdef AUDIO_DEBUG 1069 if (essdebug > 0) 1070 ess_printsc(sc); 1071 #endif 1072 } 1073 1074 /* 1075 * Various routines to interface to higher level audio driver 1076 */ 1077 1078 int 1079 ess_open(void *addr, int flags) 1080 { 1081 return 0; 1082 } 1083 1084 void 1085 ess_close(void *addr) 1086 { 1087 struct ess_softc *sc; 1088 1089 sc = addr; 1090 DPRINTF(("ess_close: sc=%p\n", sc)); 1091 1092 ess_speaker_off(sc); 1093 sc->spkr_state = SPKR_OFF; 1094 1095 DPRINTF(("ess_close: closed\n")); 1096 } 1097 1098 /* 1099 * Wait for FIFO to drain, and analog section to settle. 1100 * XXX should check FIFO empty bit. 1101 */ 1102 int 1103 ess_drain(void *addr) 1104 { 1105 1106 tsleep(addr, PWAIT | PCATCH, "essdr", hz/20); /* XXX */ 1107 return 0; 1108 } 1109 1110 /* XXX should use reference count */ 1111 int 1112 ess_speaker_ctl(void *addr, int newstate) 1113 { 1114 struct ess_softc *sc; 1115 1116 sc = addr; 1117 if ((newstate == SPKR_ON) && (sc->spkr_state == SPKR_OFF)) { 1118 ess_speaker_on(sc); 1119 sc->spkr_state = SPKR_ON; 1120 } 1121 if ((newstate == SPKR_OFF) && (sc->spkr_state == SPKR_ON)) { 1122 ess_speaker_off(sc); 1123 sc->spkr_state = SPKR_OFF; 1124 } 1125 return 0; 1126 } 1127 1128 int 1129 ess_getdev(void *addr, struct audio_device *retp) 1130 { 1131 1132 *retp = ess_device; 1133 return 0; 1134 } 1135 1136 int 1137 ess_query_encoding(void *addr, struct audio_encoding *fp) 1138 { 1139 /*struct ess_softc *sc = addr;*/ 1140 1141 switch (fp->index) { 1142 case 0: 1143 strcpy(fp->name, AudioEulinear); 1144 fp->encoding = AUDIO_ENCODING_ULINEAR; 1145 fp->precision = 8; 1146 fp->flags = 0; 1147 return 0; 1148 case 1: 1149 strcpy(fp->name, AudioEmulaw); 1150 fp->encoding = AUDIO_ENCODING_ULAW; 1151 fp->precision = 8; 1152 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 1153 return 0; 1154 case 2: 1155 strcpy(fp->name, AudioEalaw); 1156 fp->encoding = AUDIO_ENCODING_ALAW; 1157 fp->precision = 8; 1158 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 1159 return 0; 1160 case 3: 1161 strcpy(fp->name, AudioEslinear); 1162 fp->encoding = AUDIO_ENCODING_SLINEAR; 1163 fp->precision = 8; 1164 fp->flags = 0; 1165 return 0; 1166 case 4: 1167 strcpy(fp->name, AudioEslinear_le); 1168 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 1169 fp->precision = 16; 1170 fp->flags = 0; 1171 return 0; 1172 case 5: 1173 strcpy(fp->name, AudioEulinear_le); 1174 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 1175 fp->precision = 16; 1176 fp->flags = 0; 1177 return 0; 1178 case 6: 1179 strcpy(fp->name, AudioEslinear_be); 1180 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 1181 fp->precision = 16; 1182 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 1183 return 0; 1184 case 7: 1185 strcpy(fp->name, AudioEulinear_be); 1186 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 1187 fp->precision = 16; 1188 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 1189 return 0; 1190 default: 1191 return EINVAL; 1192 } 1193 return 0; 1194 } 1195 1196 int 1197 ess_set_params( 1198 void *addr, 1199 int setmode, int usemode, 1200 audio_params_t *play, audio_params_t *rec, 1201 stream_filter_list_t *pfil, stream_filter_list_t *rfil) 1202 { 1203 struct ess_softc *sc; 1204 int rate; 1205 1206 DPRINTF(("ess_set_params: set=%d use=%d\n", setmode, usemode)); 1207 sc = addr; 1208 /* 1209 * The ES1887 manual (page 39, `Full-Duplex DMA Mode') claims that in 1210 * full-duplex operation the sample rates must be the same for both 1211 * channels. This appears to be false; the only bit in common is the 1212 * clock source selection. However, we'll be conservative here. 1213 * - mycroft 1214 */ 1215 if (play->sample_rate != rec->sample_rate && 1216 usemode == (AUMODE_PLAY | AUMODE_RECORD)) { 1217 if (setmode == AUMODE_PLAY) { 1218 rec->sample_rate = play->sample_rate; 1219 setmode |= AUMODE_RECORD; 1220 } else if (setmode == AUMODE_RECORD) { 1221 play->sample_rate = rec->sample_rate; 1222 setmode |= AUMODE_PLAY; 1223 } else 1224 return EINVAL; 1225 } 1226 1227 if (setmode & AUMODE_RECORD) { 1228 if (auconv_set_converter(ess_formats, ESS_NFORMATS, 1229 AUMODE_RECORD, rec, FALSE, rfil) < 0) 1230 return EINVAL; 1231 } 1232 if (setmode & AUMODE_PLAY) { 1233 if (auconv_set_converter(ess_formats, ESS_NFORMATS, 1234 AUMODE_PLAY, play, FALSE, pfil) < 0) 1235 return EINVAL; 1236 } 1237 1238 if (usemode == AUMODE_RECORD) 1239 rate = rec->sample_rate; 1240 else 1241 rate = play->sample_rate; 1242 1243 ess_write_x_reg(sc, ESS_XCMD_SAMPLE_RATE, ess_srtotc(rate)); 1244 ess_write_x_reg(sc, ESS_XCMD_FILTER_CLOCK, ess_srtofc(rate)); 1245 1246 if (!ESS_USE_AUDIO1(sc->sc_model)) { 1247 ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, ess_srtotc(rate)); 1248 ess_write_mix_reg(sc, ESS_MREG_FILTER_CLOCK, ess_srtofc(rate)); 1249 } 1250 1251 return 0; 1252 } 1253 1254 int 1255 ess_audio1_trigger_output( 1256 void *addr, 1257 void *start, void *end, 1258 int blksize, 1259 void (*intr)(void *), 1260 void *arg, 1261 const audio_params_t *param) 1262 { 1263 struct ess_softc *sc; 1264 u_int8_t reg; 1265 1266 sc = addr; 1267 DPRINTFN(1, ("ess_audio1_trigger_output: sc=%p start=%p end=%p " 1268 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg)); 1269 1270 if (sc->sc_audio1.active) 1271 panic("ess_audio1_trigger_output: already running"); 1272 1273 sc->sc_audio1.active = 1; 1274 sc->sc_audio1.intr = intr; 1275 sc->sc_audio1.arg = arg; 1276 if (sc->sc_audio1.polled) { 1277 sc->sc_audio1.dmapos = 0; 1278 sc->sc_audio1.buffersize = (char *)end - (char *)start; 1279 sc->sc_audio1.dmacount = 0; 1280 sc->sc_audio1.blksize = blksize; 1281 callout_reset(&sc->sc_poll1_ch, hz / 30, 1282 ess_audio1_poll, sc); 1283 } 1284 1285 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL); 1286 if (param->channels == 2) { 1287 reg &= ~ESS_AUDIO_CTRL_MONO; 1288 reg |= ESS_AUDIO_CTRL_STEREO; 1289 } else { 1290 reg |= ESS_AUDIO_CTRL_MONO; 1291 reg &= ~ESS_AUDIO_CTRL_STEREO; 1292 } 1293 ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg); 1294 1295 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1); 1296 if (param->precision == 16) 1297 reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE; 1298 else 1299 reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE; 1300 if (param->channels == 2) 1301 reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO; 1302 else 1303 reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO; 1304 if (param->encoding == AUDIO_ENCODING_SLINEAR_BE || 1305 param->encoding == AUDIO_ENCODING_SLINEAR_LE) 1306 reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED; 1307 else 1308 reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED; 1309 reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT; 1310 ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg); 1311 1312 isa_dmastart(sc->sc_ic, sc->sc_audio1.drq, start, 1313 (char *)end - (char *)start, NULL, 1314 DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT); 1315 1316 /* Program transfer count registers with 2's complement of count. */ 1317 blksize = -blksize; 1318 ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize); 1319 ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8); 1320 1321 /* Use 4 bytes per output DMA. */ 1322 ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4); 1323 1324 /* Start auto-init DMA */ 1325 ess_wdsp(sc, ESS_ACMD_ENABLE_SPKR); 1326 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2); 1327 reg &= ~(ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE); 1328 reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT; 1329 ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg); 1330 1331 return 0; 1332 } 1333 1334 int 1335 ess_audio2_trigger_output( 1336 void *addr, 1337 void *start, void *end, 1338 int blksize, 1339 void (*intr)(void *), 1340 void *arg, 1341 const audio_params_t *param) 1342 { 1343 struct ess_softc *sc; 1344 u_int8_t reg; 1345 1346 sc = addr; 1347 DPRINTFN(1, ("ess_audio2_trigger_output: sc=%p start=%p end=%p " 1348 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg)); 1349 1350 if (sc->sc_audio2.active) 1351 panic("ess_audio2_trigger_output: already running"); 1352 1353 sc->sc_audio2.active = 1; 1354 sc->sc_audio2.intr = intr; 1355 sc->sc_audio2.arg = arg; 1356 if (sc->sc_audio2.polled) { 1357 sc->sc_audio2.dmapos = 0; 1358 sc->sc_audio2.buffersize = (char *)end - (char *)start; 1359 sc->sc_audio2.dmacount = 0; 1360 sc->sc_audio2.blksize = blksize; 1361 callout_reset(&sc->sc_poll2_ch, hz / 30, 1362 ess_audio2_poll, sc); 1363 } 1364 1365 reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2); 1366 if (param->precision == 16) 1367 reg |= ESS_AUDIO2_CTRL2_FIFO_SIZE; 1368 else 1369 reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIZE; 1370 if (param->channels == 2) 1371 reg |= ESS_AUDIO2_CTRL2_CHANNELS; 1372 else 1373 reg &= ~ESS_AUDIO2_CTRL2_CHANNELS; 1374 if (param->encoding == AUDIO_ENCODING_SLINEAR_BE || 1375 param->encoding == AUDIO_ENCODING_SLINEAR_LE) 1376 reg |= ESS_AUDIO2_CTRL2_FIFO_SIGNED; 1377 else 1378 reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIGNED; 1379 ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg); 1380 1381 isa_dmastart(sc->sc_ic, sc->sc_audio2.drq, start, 1382 (char *)end - (char *)start, NULL, 1383 DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT); 1384 1385 if (IS16BITDRQ(sc->sc_audio2.drq)) 1386 blksize >>= 1; /* use word count for 16 bit DMA */ 1387 /* Program transfer count registers with 2's complement of count. */ 1388 blksize = -blksize; 1389 ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTLO, blksize); 1390 ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTHI, blksize >> 8); 1391 1392 reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1); 1393 if (IS16BITDRQ(sc->sc_audio2.drq)) 1394 reg |= ESS_AUDIO2_CTRL1_XFER_SIZE; 1395 else 1396 reg &= ~ESS_AUDIO2_CTRL1_XFER_SIZE; 1397 reg |= ESS_AUDIO2_CTRL1_DEMAND_8; 1398 reg |= ESS_AUDIO2_CTRL1_DAC_ENABLE | ESS_AUDIO2_CTRL1_FIFO_ENABLE | 1399 ESS_AUDIO2_CTRL1_AUTO_INIT; 1400 ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1, reg); 1401 1402 return (0); 1403 } 1404 1405 int 1406 ess_audio1_trigger_input( 1407 void *addr, 1408 void *start, void *end, 1409 int blksize, 1410 void (*intr)(void *), 1411 void *arg, 1412 const audio_params_t *param) 1413 { 1414 struct ess_softc *sc; 1415 u_int8_t reg; 1416 1417 sc = addr; 1418 DPRINTFN(1, ("ess_audio1_trigger_input: sc=%p start=%p end=%p " 1419 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg)); 1420 1421 if (sc->sc_audio1.active) 1422 panic("ess_audio1_trigger_input: already running"); 1423 1424 sc->sc_audio1.active = 1; 1425 sc->sc_audio1.intr = intr; 1426 sc->sc_audio1.arg = arg; 1427 if (sc->sc_audio1.polled) { 1428 sc->sc_audio1.dmapos = 0; 1429 sc->sc_audio1.buffersize = (char *)end - (char *)start; 1430 sc->sc_audio1.dmacount = 0; 1431 sc->sc_audio1.blksize = blksize; 1432 callout_reset(&sc->sc_poll1_ch, hz / 30, 1433 ess_audio1_poll, sc); 1434 } 1435 1436 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL); 1437 if (param->channels == 2) { 1438 reg &= ~ESS_AUDIO_CTRL_MONO; 1439 reg |= ESS_AUDIO_CTRL_STEREO; 1440 } else { 1441 reg |= ESS_AUDIO_CTRL_MONO; 1442 reg &= ~ESS_AUDIO_CTRL_STEREO; 1443 } 1444 ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg); 1445 1446 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1); 1447 if (param->precision == 16) 1448 reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE; 1449 else 1450 reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE; 1451 if (param->channels == 2) 1452 reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO; 1453 else 1454 reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO; 1455 if (param->encoding == AUDIO_ENCODING_SLINEAR_BE || 1456 param->encoding == AUDIO_ENCODING_SLINEAR_LE) 1457 reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED; 1458 else 1459 reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED; 1460 reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT; 1461 ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg); 1462 1463 isa_dmastart(sc->sc_ic, sc->sc_audio1.drq, start, 1464 (char *)end - (char *)start, NULL, 1465 DMAMODE_READ | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT); 1466 1467 /* Program transfer count registers with 2's complement of count. */ 1468 blksize = -blksize; 1469 ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize); 1470 ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8); 1471 1472 /* Use 4 bytes per input DMA. */ 1473 ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4); 1474 1475 /* Start auto-init DMA */ 1476 ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR); 1477 reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2); 1478 reg |= ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE; 1479 reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT; 1480 ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg); 1481 1482 return 0; 1483 } 1484 1485 int 1486 ess_audio1_halt(void *addr) 1487 { 1488 struct ess_softc *sc; 1489 1490 sc = addr; 1491 DPRINTF(("ess_audio1_halt: sc=%p\n", sc)); 1492 1493 if (sc->sc_audio1.active) { 1494 ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2, 1495 ESS_AUDIO1_CTRL2_FIFO_ENABLE); 1496 isa_dmaabort(sc->sc_ic, sc->sc_audio1.drq); 1497 if (sc->sc_audio1.polled) 1498 callout_stop(&sc->sc_poll1_ch); 1499 sc->sc_audio1.active = 0; 1500 } 1501 1502 return 0; 1503 } 1504 1505 int 1506 ess_audio2_halt(void *addr) 1507 { 1508 struct ess_softc *sc; 1509 1510 sc = addr; 1511 DPRINTF(("ess_audio2_halt: sc=%p\n", sc)); 1512 1513 if (sc->sc_audio2.active) { 1514 ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1, 1515 ESS_AUDIO2_CTRL1_DAC_ENABLE | 1516 ESS_AUDIO2_CTRL1_FIFO_ENABLE); 1517 isa_dmaabort(sc->sc_ic, sc->sc_audio2.drq); 1518 if (sc->sc_audio2.polled) 1519 callout_stop(&sc->sc_poll2_ch); 1520 sc->sc_audio2.active = 0; 1521 } 1522 1523 return 0; 1524 } 1525 1526 int 1527 ess_audio1_intr(void *arg) 1528 { 1529 struct ess_softc *sc; 1530 uint8_t reg; 1531 1532 sc = arg; 1533 DPRINTFN(1,("ess_audio1_intr: intr=%p\n", sc->sc_audio1.intr)); 1534 1535 /* Check and clear interrupt on Audio1. */ 1536 reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS); 1537 if ((reg & ESS_DSP_READ_OFLOW) == 0) 1538 return 0; 1539 reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_CLEAR_INTR); 1540 1541 sc->sc_audio1.nintr++; 1542 1543 if (sc->sc_audio1.active) { 1544 (*sc->sc_audio1.intr)(sc->sc_audio1.arg); 1545 return 1; 1546 } else 1547 return 0; 1548 } 1549 1550 int 1551 ess_audio2_intr(void *arg) 1552 { 1553 struct ess_softc *sc; 1554 uint8_t reg; 1555 1556 sc = arg; 1557 DPRINTFN(1,("ess_audio2_intr: intr=%p\n", sc->sc_audio2.intr)); 1558 1559 /* Check and clear interrupt on Audio2. */ 1560 reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2); 1561 if ((reg & ESS_AUDIO2_CTRL2_IRQ_LATCH) == 0) 1562 return 0; 1563 reg &= ~ESS_AUDIO2_CTRL2_IRQ_LATCH; 1564 ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg); 1565 1566 sc->sc_audio2.nintr++; 1567 1568 if (sc->sc_audio2.active) { 1569 (*sc->sc_audio2.intr)(sc->sc_audio2.arg); 1570 return 1; 1571 } else 1572 return 0; 1573 } 1574 1575 void 1576 ess_audio1_poll(void *addr) 1577 { 1578 struct ess_softc *sc; 1579 int dmapos, dmacount; 1580 1581 sc = addr; 1582 if (!sc->sc_audio1.active) 1583 return; 1584 1585 sc->sc_audio1.nintr++; 1586 1587 dmapos = isa_dmacount(sc->sc_ic, sc->sc_audio1.drq); 1588 dmacount = sc->sc_audio1.dmapos - dmapos; 1589 if (dmacount < 0) 1590 dmacount += sc->sc_audio1.buffersize; 1591 sc->sc_audio1.dmapos = dmapos; 1592 #if 1 1593 dmacount += sc->sc_audio1.dmacount; 1594 while (dmacount > sc->sc_audio1.blksize) { 1595 dmacount -= sc->sc_audio1.blksize; 1596 (*sc->sc_audio1.intr)(sc->sc_audio1.arg); 1597 } 1598 sc->sc_audio1.dmacount = dmacount; 1599 #else 1600 (*sc->sc_audio1.intr)(sc->sc_audio1.arg, dmacount); 1601 #endif 1602 1603 callout_reset(&sc->sc_poll1_ch, hz / 30, ess_audio1_poll, sc); 1604 } 1605 1606 void 1607 ess_audio2_poll(void *addr) 1608 { 1609 struct ess_softc *sc; 1610 int dmapos, dmacount; 1611 1612 sc = addr; 1613 if (!sc->sc_audio2.active) 1614 return; 1615 1616 sc->sc_audio2.nintr++; 1617 1618 dmapos = isa_dmacount(sc->sc_ic, sc->sc_audio2.drq); 1619 dmacount = sc->sc_audio2.dmapos - dmapos; 1620 if (dmacount < 0) 1621 dmacount += sc->sc_audio2.buffersize; 1622 sc->sc_audio2.dmapos = dmapos; 1623 #if 1 1624 dmacount += sc->sc_audio2.dmacount; 1625 while (dmacount > sc->sc_audio2.blksize) { 1626 dmacount -= sc->sc_audio2.blksize; 1627 (*sc->sc_audio2.intr)(sc->sc_audio2.arg); 1628 } 1629 sc->sc_audio2.dmacount = dmacount; 1630 #else 1631 (*sc->sc_audio2.intr)(sc->sc_audio2.arg, dmacount); 1632 #endif 1633 1634 callout_reset(&sc->sc_poll2_ch, hz / 30, ess_audio2_poll, sc); 1635 } 1636 1637 int 1638 ess_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param) 1639 { 1640 1641 return blk & -8; /* round for max DMA size */ 1642 } 1643 1644 int 1645 ess_set_port(void *addr, mixer_ctrl_t *cp) 1646 { 1647 struct ess_softc *sc; 1648 int lgain, rgain; 1649 1650 sc = addr; 1651 DPRINTFN(5,("ess_set_port: port=%d num_channels=%d\n", 1652 cp->dev, cp->un.value.num_channels)); 1653 1654 switch (cp->dev) { 1655 /* 1656 * The following mixer ports are all stereo. If we get a 1657 * single-channel gain value passed in, then we duplicate it 1658 * to both left and right channels. 1659 */ 1660 case ESS_MASTER_VOL: 1661 case ESS_DAC_PLAY_VOL: 1662 case ESS_MIC_PLAY_VOL: 1663 case ESS_LINE_PLAY_VOL: 1664 case ESS_SYNTH_PLAY_VOL: 1665 case ESS_CD_PLAY_VOL: 1666 case ESS_AUXB_PLAY_VOL: 1667 case ESS_RECORD_VOL: 1668 if (cp->type != AUDIO_MIXER_VALUE) 1669 return EINVAL; 1670 1671 switch (cp->un.value.num_channels) { 1672 case 1: 1673 lgain = rgain = ESS_4BIT_GAIN( 1674 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]); 1675 break; 1676 case 2: 1677 lgain = ESS_4BIT_GAIN( 1678 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]); 1679 rgain = ESS_4BIT_GAIN( 1680 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]); 1681 break; 1682 default: 1683 return EINVAL; 1684 } 1685 1686 sc->gain[cp->dev][ESS_LEFT] = lgain; 1687 sc->gain[cp->dev][ESS_RIGHT] = rgain; 1688 ess_set_gain(sc, cp->dev, 1); 1689 return 0; 1690 1691 /* 1692 * The PC speaker port is mono. If we get a stereo gain value 1693 * passed in, then we return EINVAL. 1694 */ 1695 case ESS_PCSPEAKER_VOL: 1696 if (cp->un.value.num_channels != 1) 1697 return EINVAL; 1698 1699 sc->gain[cp->dev][ESS_LEFT] = sc->gain[cp->dev][ESS_RIGHT] = 1700 ESS_3BIT_GAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]); 1701 ess_set_gain(sc, cp->dev, 1); 1702 return 0; 1703 1704 case ESS_RECORD_SOURCE: 1705 if (ESS_USE_AUDIO1(sc->sc_model)) { 1706 if (cp->type == AUDIO_MIXER_ENUM) 1707 return ess_set_in_port(sc, cp->un.ord); 1708 else 1709 return EINVAL; 1710 } else { 1711 if (cp->type == AUDIO_MIXER_SET) 1712 return ess_set_in_ports(sc, cp->un.mask); 1713 else 1714 return EINVAL; 1715 } 1716 return 0; 1717 1718 case ESS_RECORD_MONITOR: 1719 if (cp->type != AUDIO_MIXER_ENUM) 1720 return EINVAL; 1721 1722 if (cp->un.ord) 1723 /* Enable monitor */ 1724 ess_set_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL, 1725 ESS_AUDIO_CTRL_MONITOR); 1726 else 1727 /* Disable monitor */ 1728 ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL, 1729 ESS_AUDIO_CTRL_MONITOR); 1730 return 0; 1731 } 1732 1733 if (ESS_USE_AUDIO1(sc->sc_model)) 1734 return EINVAL; 1735 1736 switch (cp->dev) { 1737 case ESS_DAC_REC_VOL: 1738 case ESS_MIC_REC_VOL: 1739 case ESS_LINE_REC_VOL: 1740 case ESS_SYNTH_REC_VOL: 1741 case ESS_CD_REC_VOL: 1742 case ESS_AUXB_REC_VOL: 1743 if (cp->type != AUDIO_MIXER_VALUE) 1744 return EINVAL; 1745 1746 switch (cp->un.value.num_channels) { 1747 case 1: 1748 lgain = rgain = ESS_4BIT_GAIN( 1749 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]); 1750 break; 1751 case 2: 1752 lgain = ESS_4BIT_GAIN( 1753 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]); 1754 rgain = ESS_4BIT_GAIN( 1755 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]); 1756 break; 1757 default: 1758 return EINVAL; 1759 } 1760 1761 sc->gain[cp->dev][ESS_LEFT] = lgain; 1762 sc->gain[cp->dev][ESS_RIGHT] = rgain; 1763 ess_set_gain(sc, cp->dev, 1); 1764 return 0; 1765 1766 case ESS_MIC_PREAMP: 1767 if (cp->type != AUDIO_MIXER_ENUM) 1768 return EINVAL; 1769 1770 if (cp->un.ord) 1771 /* Enable microphone preamp */ 1772 ess_set_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL, 1773 ESS_PREAMP_CTRL_ENABLE); 1774 else 1775 /* Disable microphone preamp */ 1776 ess_clear_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL, 1777 ESS_PREAMP_CTRL_ENABLE); 1778 return 0; 1779 } 1780 1781 return EINVAL; 1782 } 1783 1784 int 1785 ess_get_port(void *addr, mixer_ctrl_t *cp) 1786 { 1787 struct ess_softc *sc; 1788 1789 sc = addr; 1790 DPRINTFN(5,("ess_get_port: port=%d\n", cp->dev)); 1791 1792 switch (cp->dev) { 1793 case ESS_MASTER_VOL: 1794 case ESS_DAC_PLAY_VOL: 1795 case ESS_MIC_PLAY_VOL: 1796 case ESS_LINE_PLAY_VOL: 1797 case ESS_SYNTH_PLAY_VOL: 1798 case ESS_CD_PLAY_VOL: 1799 case ESS_AUXB_PLAY_VOL: 1800 case ESS_RECORD_VOL: 1801 switch (cp->un.value.num_channels) { 1802 case 1: 1803 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 1804 sc->gain[cp->dev][ESS_LEFT]; 1805 break; 1806 case 2: 1807 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 1808 sc->gain[cp->dev][ESS_LEFT]; 1809 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 1810 sc->gain[cp->dev][ESS_RIGHT]; 1811 break; 1812 default: 1813 return EINVAL; 1814 } 1815 return 0; 1816 1817 case ESS_PCSPEAKER_VOL: 1818 if (cp->un.value.num_channels != 1) 1819 return EINVAL; 1820 1821 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 1822 sc->gain[cp->dev][ESS_LEFT]; 1823 return 0; 1824 1825 case ESS_RECORD_SOURCE: 1826 if (ESS_USE_AUDIO1(sc->sc_model)) 1827 cp->un.ord = sc->in_port; 1828 else 1829 cp->un.mask = sc->in_mask; 1830 return 0; 1831 1832 case ESS_RECORD_MONITOR: 1833 cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) & 1834 ESS_AUDIO_CTRL_MONITOR) ? 1 : 0; 1835 return 0; 1836 } 1837 1838 if (ESS_USE_AUDIO1(sc->sc_model)) 1839 return EINVAL; 1840 1841 switch (cp->dev) { 1842 case ESS_DAC_REC_VOL: 1843 case ESS_MIC_REC_VOL: 1844 case ESS_LINE_REC_VOL: 1845 case ESS_SYNTH_REC_VOL: 1846 case ESS_CD_REC_VOL: 1847 case ESS_AUXB_REC_VOL: 1848 switch (cp->un.value.num_channels) { 1849 case 1: 1850 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 1851 sc->gain[cp->dev][ESS_LEFT]; 1852 break; 1853 case 2: 1854 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 1855 sc->gain[cp->dev][ESS_LEFT]; 1856 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 1857 sc->gain[cp->dev][ESS_RIGHT]; 1858 break; 1859 default: 1860 return EINVAL; 1861 } 1862 return 0; 1863 1864 case ESS_MIC_PREAMP: 1865 cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL) & 1866 ESS_PREAMP_CTRL_ENABLE) ? 1 : 0; 1867 return 0; 1868 } 1869 1870 return EINVAL; 1871 } 1872 1873 int 1874 ess_query_devinfo(void *addr, mixer_devinfo_t *dip) 1875 { 1876 struct ess_softc *sc; 1877 1878 sc = addr; 1879 DPRINTFN(5,("ess_query_devinfo: model=%d index=%d\n", 1880 sc->sc_model, dip->index)); 1881 1882 /* 1883 * REVISIT: There are some slight differences between the 1884 * mixers on the different ESS chips, which can 1885 * be sorted out using the chip model rather than a 1886 * separate mixer model. 1887 * This is currently coded assuming an ES1887; we 1888 * need to work out which bits are not applicable to 1889 * the other models (1888 and 888). 1890 */ 1891 switch (dip->index) { 1892 case ESS_DAC_PLAY_VOL: 1893 dip->mixer_class = ESS_INPUT_CLASS; 1894 dip->next = dip->prev = AUDIO_MIXER_LAST; 1895 strcpy(dip->label.name, AudioNdac); 1896 dip->type = AUDIO_MIXER_VALUE; 1897 dip->un.v.num_channels = 2; 1898 strcpy(dip->un.v.units.name, AudioNvolume); 1899 return 0; 1900 1901 case ESS_MIC_PLAY_VOL: 1902 dip->mixer_class = ESS_INPUT_CLASS; 1903 dip->prev = AUDIO_MIXER_LAST; 1904 if (ESS_USE_AUDIO1(sc->sc_model)) 1905 dip->next = AUDIO_MIXER_LAST; 1906 else 1907 dip->next = ESS_MIC_PREAMP; 1908 strcpy(dip->label.name, AudioNmicrophone); 1909 dip->type = AUDIO_MIXER_VALUE; 1910 dip->un.v.num_channels = 2; 1911 strcpy(dip->un.v.units.name, AudioNvolume); 1912 return 0; 1913 1914 case ESS_LINE_PLAY_VOL: 1915 dip->mixer_class = ESS_INPUT_CLASS; 1916 dip->next = dip->prev = AUDIO_MIXER_LAST; 1917 strcpy(dip->label.name, AudioNline); 1918 dip->type = AUDIO_MIXER_VALUE; 1919 dip->un.v.num_channels = 2; 1920 strcpy(dip->un.v.units.name, AudioNvolume); 1921 return 0; 1922 1923 case ESS_SYNTH_PLAY_VOL: 1924 dip->mixer_class = ESS_INPUT_CLASS; 1925 dip->next = dip->prev = AUDIO_MIXER_LAST; 1926 strcpy(dip->label.name, AudioNfmsynth); 1927 dip->type = AUDIO_MIXER_VALUE; 1928 dip->un.v.num_channels = 2; 1929 strcpy(dip->un.v.units.name, AudioNvolume); 1930 return 0; 1931 1932 case ESS_CD_PLAY_VOL: 1933 dip->mixer_class = ESS_INPUT_CLASS; 1934 dip->next = dip->prev = AUDIO_MIXER_LAST; 1935 strcpy(dip->label.name, AudioNcd); 1936 dip->type = AUDIO_MIXER_VALUE; 1937 dip->un.v.num_channels = 2; 1938 strcpy(dip->un.v.units.name, AudioNvolume); 1939 return 0; 1940 1941 case ESS_AUXB_PLAY_VOL: 1942 dip->mixer_class = ESS_INPUT_CLASS; 1943 dip->next = dip->prev = AUDIO_MIXER_LAST; 1944 strcpy(dip->label.name, "auxb"); 1945 dip->type = AUDIO_MIXER_VALUE; 1946 dip->un.v.num_channels = 2; 1947 strcpy(dip->un.v.units.name, AudioNvolume); 1948 return 0; 1949 1950 case ESS_INPUT_CLASS: 1951 dip->mixer_class = ESS_INPUT_CLASS; 1952 dip->next = dip->prev = AUDIO_MIXER_LAST; 1953 strcpy(dip->label.name, AudioCinputs); 1954 dip->type = AUDIO_MIXER_CLASS; 1955 return 0; 1956 1957 case ESS_MASTER_VOL: 1958 dip->mixer_class = ESS_OUTPUT_CLASS; 1959 dip->next = dip->prev = AUDIO_MIXER_LAST; 1960 strcpy(dip->label.name, AudioNmaster); 1961 dip->type = AUDIO_MIXER_VALUE; 1962 dip->un.v.num_channels = 2; 1963 strcpy(dip->un.v.units.name, AudioNvolume); 1964 return 0; 1965 1966 case ESS_PCSPEAKER_VOL: 1967 dip->mixer_class = ESS_OUTPUT_CLASS; 1968 dip->next = dip->prev = AUDIO_MIXER_LAST; 1969 strcpy(dip->label.name, "pc_speaker"); 1970 dip->type = AUDIO_MIXER_VALUE; 1971 dip->un.v.num_channels = 1; 1972 strcpy(dip->un.v.units.name, AudioNvolume); 1973 return 0; 1974 1975 case ESS_OUTPUT_CLASS: 1976 dip->mixer_class = ESS_OUTPUT_CLASS; 1977 dip->next = dip->prev = AUDIO_MIXER_LAST; 1978 strcpy(dip->label.name, AudioCoutputs); 1979 dip->type = AUDIO_MIXER_CLASS; 1980 return 0; 1981 1982 case ESS_RECORD_VOL: 1983 dip->mixer_class = ESS_RECORD_CLASS; 1984 dip->next = dip->prev = AUDIO_MIXER_LAST; 1985 strcpy(dip->label.name, AudioNrecord); 1986 dip->type = AUDIO_MIXER_VALUE; 1987 dip->un.v.num_channels = 2; 1988 strcpy(dip->un.v.units.name, AudioNvolume); 1989 return 0; 1990 1991 case ESS_RECORD_SOURCE: 1992 dip->mixer_class = ESS_RECORD_CLASS; 1993 dip->next = dip->prev = AUDIO_MIXER_LAST; 1994 strcpy(dip->label.name, AudioNsource); 1995 if (ESS_USE_AUDIO1(sc->sc_model)) { 1996 /* 1997 * The 1788 doesn't use the input mixer control that 1998 * the 1888 uses, because it's a pain when you only 1999 * have one mixer. 2000 * Perhaps it could be emulated by keeping both sets of 2001 * gain values, and doing a `context switch' of the 2002 * mixer registers when shifting from playing to 2003 * recording. 2004 */ 2005 dip->type = AUDIO_MIXER_ENUM; 2006 dip->un.e.num_mem = 4; 2007 strcpy(dip->un.e.member[0].label.name, AudioNmicrophone); 2008 dip->un.e.member[0].ord = ESS_SOURCE_MIC; 2009 strcpy(dip->un.e.member[1].label.name, AudioNline); 2010 dip->un.e.member[1].ord = ESS_SOURCE_LINE; 2011 strcpy(dip->un.e.member[2].label.name, AudioNcd); 2012 dip->un.e.member[2].ord = ESS_SOURCE_CD; 2013 strcpy(dip->un.e.member[3].label.name, AudioNmixerout); 2014 dip->un.e.member[3].ord = ESS_SOURCE_MIXER; 2015 } else { 2016 dip->type = AUDIO_MIXER_SET; 2017 dip->un.s.num_mem = 6; 2018 strcpy(dip->un.s.member[0].label.name, AudioNdac); 2019 dip->un.s.member[0].mask = 1 << ESS_DAC_REC_VOL; 2020 strcpy(dip->un.s.member[1].label.name, AudioNmicrophone); 2021 dip->un.s.member[1].mask = 1 << ESS_MIC_REC_VOL; 2022 strcpy(dip->un.s.member[2].label.name, AudioNline); 2023 dip->un.s.member[2].mask = 1 << ESS_LINE_REC_VOL; 2024 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth); 2025 dip->un.s.member[3].mask = 1 << ESS_SYNTH_REC_VOL; 2026 strcpy(dip->un.s.member[4].label.name, AudioNcd); 2027 dip->un.s.member[4].mask = 1 << ESS_CD_REC_VOL; 2028 strcpy(dip->un.s.member[5].label.name, "auxb"); 2029 dip->un.s.member[5].mask = 1 << ESS_AUXB_REC_VOL; 2030 } 2031 return 0; 2032 2033 case ESS_RECORD_CLASS: 2034 dip->mixer_class = ESS_RECORD_CLASS; 2035 dip->next = dip->prev = AUDIO_MIXER_LAST; 2036 strcpy(dip->label.name, AudioCrecord); 2037 dip->type = AUDIO_MIXER_CLASS; 2038 return 0; 2039 2040 case ESS_RECORD_MONITOR: 2041 dip->prev = dip->next = AUDIO_MIXER_LAST; 2042 strcpy(dip->label.name, AudioNmute); 2043 dip->type = AUDIO_MIXER_ENUM; 2044 dip->mixer_class = ESS_MONITOR_CLASS; 2045 dip->un.e.num_mem = 2; 2046 strcpy(dip->un.e.member[0].label.name, AudioNoff); 2047 dip->un.e.member[0].ord = 0; 2048 strcpy(dip->un.e.member[1].label.name, AudioNon); 2049 dip->un.e.member[1].ord = 1; 2050 return 0; 2051 2052 case ESS_MONITOR_CLASS: 2053 dip->mixer_class = ESS_MONITOR_CLASS; 2054 dip->next = dip->prev = AUDIO_MIXER_LAST; 2055 strcpy(dip->label.name, AudioCmonitor); 2056 dip->type = AUDIO_MIXER_CLASS; 2057 return 0; 2058 } 2059 2060 if (ESS_USE_AUDIO1(sc->sc_model)) 2061 return ENXIO; 2062 2063 switch (dip->index) { 2064 case ESS_DAC_REC_VOL: 2065 dip->mixer_class = ESS_RECORD_CLASS; 2066 dip->next = dip->prev = AUDIO_MIXER_LAST; 2067 strcpy(dip->label.name, AudioNdac); 2068 dip->type = AUDIO_MIXER_VALUE; 2069 dip->un.v.num_channels = 2; 2070 strcpy(dip->un.v.units.name, AudioNvolume); 2071 return 0; 2072 2073 case ESS_MIC_REC_VOL: 2074 dip->mixer_class = ESS_RECORD_CLASS; 2075 dip->next = dip->prev = AUDIO_MIXER_LAST; 2076 strcpy(dip->label.name, AudioNmicrophone); 2077 dip->type = AUDIO_MIXER_VALUE; 2078 dip->un.v.num_channels = 2; 2079 strcpy(dip->un.v.units.name, AudioNvolume); 2080 return 0; 2081 2082 case ESS_LINE_REC_VOL: 2083 dip->mixer_class = ESS_RECORD_CLASS; 2084 dip->next = dip->prev = AUDIO_MIXER_LAST; 2085 strcpy(dip->label.name, AudioNline); 2086 dip->type = AUDIO_MIXER_VALUE; 2087 dip->un.v.num_channels = 2; 2088 strcpy(dip->un.v.units.name, AudioNvolume); 2089 return 0; 2090 2091 case ESS_SYNTH_REC_VOL: 2092 dip->mixer_class = ESS_RECORD_CLASS; 2093 dip->next = dip->prev = AUDIO_MIXER_LAST; 2094 strcpy(dip->label.name, AudioNfmsynth); 2095 dip->type = AUDIO_MIXER_VALUE; 2096 dip->un.v.num_channels = 2; 2097 strcpy(dip->un.v.units.name, AudioNvolume); 2098 return 0; 2099 2100 case ESS_CD_REC_VOL: 2101 dip->mixer_class = ESS_RECORD_CLASS; 2102 dip->next = dip->prev = AUDIO_MIXER_LAST; 2103 strcpy(dip->label.name, AudioNcd); 2104 dip->type = AUDIO_MIXER_VALUE; 2105 dip->un.v.num_channels = 2; 2106 strcpy(dip->un.v.units.name, AudioNvolume); 2107 return 0; 2108 2109 case ESS_AUXB_REC_VOL: 2110 dip->mixer_class = ESS_RECORD_CLASS; 2111 dip->next = dip->prev = AUDIO_MIXER_LAST; 2112 strcpy(dip->label.name, "auxb"); 2113 dip->type = AUDIO_MIXER_VALUE; 2114 dip->un.v.num_channels = 2; 2115 strcpy(dip->un.v.units.name, AudioNvolume); 2116 return 0; 2117 2118 case ESS_MIC_PREAMP: 2119 dip->mixer_class = ESS_INPUT_CLASS; 2120 dip->prev = ESS_MIC_PLAY_VOL; 2121 dip->next = AUDIO_MIXER_LAST; 2122 strcpy(dip->label.name, AudioNpreamp); 2123 dip->type = AUDIO_MIXER_ENUM; 2124 dip->un.e.num_mem = 2; 2125 strcpy(dip->un.e.member[0].label.name, AudioNoff); 2126 dip->un.e.member[0].ord = 0; 2127 strcpy(dip->un.e.member[1].label.name, AudioNon); 2128 dip->un.e.member[1].ord = 1; 2129 return 0; 2130 } 2131 2132 return ENXIO; 2133 } 2134 2135 void * 2136 ess_malloc(void *addr, int direction, size_t size, 2137 struct malloc_type *pool, int flags) 2138 { 2139 struct ess_softc *sc; 2140 int drq; 2141 2142 sc = addr; 2143 if ((!ESS_USE_AUDIO1(sc->sc_model)) && direction == AUMODE_PLAY) 2144 drq = sc->sc_audio2.drq; 2145 else 2146 drq = sc->sc_audio1.drq; 2147 return (isa_malloc(sc->sc_ic, drq, size, pool, flags)); 2148 } 2149 2150 void 2151 ess_free(void *addr, void *ptr, struct malloc_type *pool) 2152 { 2153 2154 isa_free(ptr, pool); 2155 } 2156 2157 size_t 2158 ess_round_buffersize(void *addr, int direction, size_t size) 2159 { 2160 struct ess_softc *sc; 2161 bus_size_t maxsize; 2162 2163 sc = addr; 2164 if ((!ESS_USE_AUDIO1(sc->sc_model)) && direction == AUMODE_PLAY) 2165 maxsize = sc->sc_audio2.maxsize; 2166 else 2167 maxsize = sc->sc_audio1.maxsize; 2168 2169 if (size > maxsize) 2170 size = maxsize; 2171 return size; 2172 } 2173 2174 paddr_t 2175 ess_mappage(void *addr, void *mem, off_t off, int prot) 2176 { 2177 2178 return isa_mappage(mem, off, prot); 2179 } 2180 2181 int 2182 ess_1788_get_props(void *addr) 2183 { 2184 2185 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT; 2186 } 2187 2188 int 2189 ess_1888_get_props(void *addr) 2190 { 2191 2192 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 2193 } 2194 2195 /* ============================================ 2196 * Generic functions for ess, not used by audio h/w i/f 2197 * ============================================= 2198 */ 2199 2200 /* 2201 * Reset the chip. 2202 * Return non-zero if the chip isn't detected. 2203 */ 2204 int 2205 ess_reset(struct ess_softc *sc) 2206 { 2207 bus_space_tag_t iot; 2208 bus_space_handle_t ioh; 2209 2210 iot = sc->sc_iot; 2211 ioh = sc->sc_ioh; 2212 sc->sc_audio1.active = 0; 2213 sc->sc_audio2.active = 0; 2214 2215 EWRITE1(iot, ioh, ESS_DSP_RESET, ESS_RESET_EXT); 2216 delay(10000); /* XXX shouldn't delay so long */ 2217 EWRITE1(iot, ioh, ESS_DSP_RESET, 0); 2218 if (ess_rdsp(sc) != ESS_MAGIC) 2219 return 1; 2220 2221 /* Enable access to the ESS extension commands. */ 2222 ess_wdsp(sc, ESS_ACMD_ENABLE_EXT); 2223 2224 return 0; 2225 } 2226 2227 void 2228 ess_set_gain(struct ess_softc *sc, int port, int on) 2229 { 2230 int gain, left, right; 2231 int mix; 2232 int src; 2233 int stereo; 2234 2235 /* 2236 * Most gain controls are found in the mixer registers and 2237 * are stereo. Any that are not, must set mix and stereo as 2238 * required. 2239 */ 2240 mix = 1; 2241 stereo = 1; 2242 2243 switch (port) { 2244 case ESS_MASTER_VOL: 2245 src = ESS_MREG_VOLUME_MASTER; 2246 break; 2247 case ESS_DAC_PLAY_VOL: 2248 if (ESS_USE_AUDIO1(sc->sc_model)) 2249 src = ESS_MREG_VOLUME_VOICE; 2250 else 2251 src = 0x7C; 2252 break; 2253 case ESS_MIC_PLAY_VOL: 2254 src = ESS_MREG_VOLUME_MIC; 2255 break; 2256 case ESS_LINE_PLAY_VOL: 2257 src = ESS_MREG_VOLUME_LINE; 2258 break; 2259 case ESS_SYNTH_PLAY_VOL: 2260 src = ESS_MREG_VOLUME_SYNTH; 2261 break; 2262 case ESS_CD_PLAY_VOL: 2263 src = ESS_MREG_VOLUME_CD; 2264 break; 2265 case ESS_AUXB_PLAY_VOL: 2266 src = ESS_MREG_VOLUME_AUXB; 2267 break; 2268 case ESS_PCSPEAKER_VOL: 2269 src = ESS_MREG_VOLUME_PCSPKR; 2270 stereo = 0; 2271 break; 2272 case ESS_DAC_REC_VOL: 2273 src = 0x69; 2274 break; 2275 case ESS_MIC_REC_VOL: 2276 src = 0x68; 2277 break; 2278 case ESS_LINE_REC_VOL: 2279 src = 0x6E; 2280 break; 2281 case ESS_SYNTH_REC_VOL: 2282 src = 0x6B; 2283 break; 2284 case ESS_CD_REC_VOL: 2285 src = 0x6A; 2286 break; 2287 case ESS_AUXB_REC_VOL: 2288 src = 0x6C; 2289 break; 2290 case ESS_RECORD_VOL: 2291 src = ESS_XCMD_VOLIN_CTRL; 2292 mix = 0; 2293 break; 2294 default: 2295 return; 2296 } 2297 2298 /* 1788 doesn't have a separate recording mixer */ 2299 if (ESS_USE_AUDIO1(sc->sc_model) && mix && src > 0x62) 2300 return; 2301 2302 if (on) { 2303 left = sc->gain[port][ESS_LEFT]; 2304 right = sc->gain[port][ESS_RIGHT]; 2305 } else { 2306 left = right = 0; 2307 } 2308 2309 if (stereo) 2310 gain = ESS_STEREO_GAIN(left, right); 2311 else 2312 gain = ESS_MONO_GAIN(left); 2313 2314 if (mix) 2315 ess_write_mix_reg(sc, src, gain); 2316 else 2317 ess_write_x_reg(sc, src, gain); 2318 } 2319 2320 /* Set the input device on devices without an input mixer. */ 2321 int 2322 ess_set_in_port(struct ess_softc *sc, int ord) 2323 { 2324 mixer_devinfo_t di; 2325 int i; 2326 2327 DPRINTF(("ess_set_in_port: ord=0x%x\n", ord)); 2328 2329 /* 2330 * Get the device info for the record source control, 2331 * including the list of available sources. 2332 */ 2333 di.index = ESS_RECORD_SOURCE; 2334 if (ess_query_devinfo(sc, &di)) 2335 return EINVAL; 2336 2337 /* See if the given ord value was anywhere in the list. */ 2338 for (i = 0; i < di.un.e.num_mem; i++) { 2339 if (ord == di.un.e.member[i].ord) 2340 break; 2341 } 2342 if (i == di.un.e.num_mem) 2343 return EINVAL; 2344 2345 ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ord); 2346 2347 sc->in_port = ord; 2348 return 0; 2349 } 2350 2351 /* Set the input device levels on input-mixer-enabled devices. */ 2352 int 2353 ess_set_in_ports(struct ess_softc *sc, int mask) 2354 { 2355 mixer_devinfo_t di; 2356 int i, port; 2357 2358 DPRINTF(("ess_set_in_ports: mask=0x%x\n", mask)); 2359 2360 /* 2361 * Get the device info for the record source control, 2362 * including the list of available sources. 2363 */ 2364 di.index = ESS_RECORD_SOURCE; 2365 if (ess_query_devinfo(sc, &di)) 2366 return EINVAL; 2367 2368 /* 2369 * Set or disable the record volume control for each of the 2370 * possible sources. 2371 */ 2372 for (i = 0; i < di.un.s.num_mem; i++) { 2373 /* 2374 * Calculate the source port number from its mask. 2375 */ 2376 port = ffs(di.un.s.member[i].mask); 2377 2378 /* 2379 * Set the source gain: 2380 * to the current value if source is enabled 2381 * to zero if source is disabled 2382 */ 2383 ess_set_gain(sc, port, mask & di.un.s.member[i].mask); 2384 } 2385 2386 sc->in_mask = mask; 2387 return 0; 2388 } 2389 2390 void 2391 ess_speaker_on(struct ess_softc *sc) 2392 { 2393 2394 /* Unmute the DAC. */ 2395 ess_set_gain(sc, ESS_DAC_PLAY_VOL, 1); 2396 } 2397 2398 void 2399 ess_speaker_off(struct ess_softc *sc) 2400 { 2401 2402 /* Mute the DAC. */ 2403 ess_set_gain(sc, ESS_DAC_PLAY_VOL, 0); 2404 } 2405 2406 /* 2407 * Calculate the time constant for the requested sampling rate. 2408 */ 2409 u_int 2410 ess_srtotc(u_int rate) 2411 { 2412 u_int tc; 2413 2414 /* The following formulae are from the ESS data sheet. */ 2415 if (rate <= 22050) 2416 tc = 128 - 397700L / rate; 2417 else 2418 tc = 256 - 795500L / rate; 2419 2420 return tc; 2421 } 2422 2423 2424 /* 2425 * Calculate the filter constant for the reuqested sampling rate. 2426 */ 2427 u_int 2428 ess_srtofc(u_int rate) 2429 { 2430 /* 2431 * The following formula is derived from the information in 2432 * the ES1887 data sheet, based on a roll-off frequency of 2433 * 87%. 2434 */ 2435 return 256 - 200279L / rate; 2436 } 2437 2438 2439 /* 2440 * Return the status of the DSP. 2441 */ 2442 u_char 2443 ess_get_dsp_status(struct ess_softc *sc) 2444 { 2445 return EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS); 2446 } 2447 2448 2449 /* 2450 * Return the read status of the DSP: 1 -> DSP ready for reading 2451 * 0 -> DSP not ready for reading 2452 */ 2453 u_char 2454 ess_dsp_read_ready(struct ess_softc *sc) 2455 { 2456 2457 return (ess_get_dsp_status(sc) & ESS_DSP_READ_READY) ? 1 : 0; 2458 } 2459 2460 2461 /* 2462 * Return the write status of the DSP: 1 -> DSP ready for writing 2463 * 0 -> DSP not ready for writing 2464 */ 2465 u_char 2466 ess_dsp_write_ready(struct ess_softc *sc) 2467 { 2468 return (ess_get_dsp_status(sc) & ESS_DSP_WRITE_BUSY) ? 0 : 1; 2469 } 2470 2471 2472 /* 2473 * Read a byte from the DSP. 2474 */ 2475 int 2476 ess_rdsp(struct ess_softc *sc) 2477 { 2478 bus_space_tag_t iot; 2479 bus_space_handle_t ioh; 2480 int i; 2481 2482 iot = sc->sc_iot; 2483 ioh = sc->sc_ioh; 2484 for (i = ESS_READ_TIMEOUT; i > 0; --i) { 2485 if (ess_dsp_read_ready(sc)) { 2486 i = EREAD1(iot, ioh, ESS_DSP_READ); 2487 DPRINTFN(8,("ess_rdsp() = 0x%02x\n", i)); 2488 return i; 2489 } else 2490 delay(10); 2491 } 2492 2493 DPRINTF(("ess_rdsp: timed out\n")); 2494 return -1; 2495 } 2496 2497 /* 2498 * Write a byte to the DSP. 2499 */ 2500 int 2501 ess_wdsp(struct ess_softc *sc, u_char v) 2502 { 2503 bus_space_tag_t iot; 2504 bus_space_handle_t ioh; 2505 int i; 2506 2507 DPRINTFN(8,("ess_wdsp(0x%02x)\n", v)); 2508 2509 iot = sc->sc_iot; 2510 ioh = sc->sc_ioh; 2511 for (i = ESS_WRITE_TIMEOUT; i > 0; --i) { 2512 if (ess_dsp_write_ready(sc)) { 2513 EWRITE1(iot, ioh, ESS_DSP_WRITE, v); 2514 return 0; 2515 } else 2516 delay(10); 2517 } 2518 2519 DPRINTF(("ess_wdsp(0x%02x): timed out\n", v)); 2520 return -1; 2521 } 2522 2523 /* 2524 * Write a value to one of the ESS extended registers. 2525 */ 2526 int 2527 ess_write_x_reg(struct ess_softc *sc, u_char reg, u_char val) 2528 { 2529 int error; 2530 2531 DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val)); 2532 if ((error = ess_wdsp(sc, reg)) == 0) 2533 error = ess_wdsp(sc, val); 2534 2535 return error; 2536 } 2537 2538 /* 2539 * Read the value of one of the ESS extended registers. 2540 */ 2541 u_char 2542 ess_read_x_reg(struct ess_softc *sc, u_char reg) 2543 { 2544 int error; 2545 int val; 2546 2547 if ((error = ess_wdsp(sc, 0xC0)) == 0) 2548 error = ess_wdsp(sc, reg); 2549 if (error) 2550 DPRINTF(("Error reading extended register 0x%02x\n", reg)); 2551 /* REVISIT: what if an error is returned above? */ 2552 val = ess_rdsp(sc); 2553 DPRINTFN(2,("ess_read_x_reg: %02x=%02x\n", reg, val)); 2554 return val; 2555 } 2556 2557 void 2558 ess_clear_xreg_bits(struct ess_softc *sc, u_char reg, u_char mask) 2559 { 2560 if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) & ~mask) == -1) 2561 DPRINTF(("Error clearing bits in extended register 0x%02x\n", 2562 reg)); 2563 } 2564 2565 void 2566 ess_set_xreg_bits(struct ess_softc *sc, u_char reg, u_char mask) 2567 { 2568 if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) | mask) == -1) 2569 DPRINTF(("Error setting bits in extended register 0x%02x\n", 2570 reg)); 2571 } 2572 2573 2574 /* 2575 * Write a value to one of the ESS mixer registers. 2576 */ 2577 void 2578 ess_write_mix_reg(struct ess_softc *sc, u_char reg, u_char val) 2579 { 2580 bus_space_tag_t iot; 2581 bus_space_handle_t ioh; 2582 int s; 2583 2584 DPRINTFN(2,("ess_write_mix_reg: %x=%x\n", reg, val)); 2585 2586 iot = sc->sc_iot; 2587 ioh = sc->sc_ioh; 2588 s = splaudio(); 2589 EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg); 2590 EWRITE1(iot, ioh, ESS_MIX_REG_DATA, val); 2591 splx(s); 2592 } 2593 2594 /* 2595 * Read the value of one of the ESS mixer registers. 2596 */ 2597 u_char 2598 ess_read_mix_reg(struct ess_softc *sc, u_char reg) 2599 { 2600 bus_space_tag_t iot; 2601 bus_space_handle_t ioh; 2602 int s; 2603 u_char val; 2604 2605 iot = sc->sc_iot; 2606 ioh = sc->sc_ioh; 2607 s = splaudio(); 2608 EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg); 2609 val = EREAD1(iot, ioh, ESS_MIX_REG_DATA); 2610 splx(s); 2611 2612 DPRINTFN(2,("ess_read_mix_reg: %x=%x\n", reg, val)); 2613 return val; 2614 } 2615 2616 void 2617 ess_clear_mreg_bits(struct ess_softc *sc, u_char reg, u_char mask) 2618 { 2619 2620 ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) & ~mask); 2621 } 2622 2623 void 2624 ess_set_mreg_bits(struct ess_softc *sc, u_char reg, u_char mask) 2625 { 2626 2627 ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) | mask); 2628 } 2629 2630 void 2631 ess_read_multi_mix_reg(struct ess_softc *sc, u_char reg, 2632 uint8_t *datap, bus_size_t count) 2633 { 2634 bus_space_tag_t iot; 2635 bus_space_handle_t ioh; 2636 int s; 2637 2638 iot = sc->sc_iot; 2639 ioh = sc->sc_ioh; 2640 s = splaudio(); 2641 EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg); 2642 bus_space_read_multi_1(iot, ioh, ESS_MIX_REG_DATA, datap, count); 2643 splx(s); 2644 } 2645