1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifdef HAVE_KERNEL_OPTION_HEADERS 29 #include "opt_snd.h" 30 #endif 31 32 #include <dev/sound/pcm/sound.h> 33 34 #include <dev/pci/pcireg.h> 35 #include <dev/pci/pcivar.h> 36 37 #include <dev/sound/isa/sb.h> 38 #include <dev/sound/chip.h> 39 40 #include "mixer_if.h" 41 42 SND_DECLARE_FILE("$FreeBSD$"); 43 44 #define SOLO_DEFAULT_BUFSZ 16384 45 #define ABS(x) (((x) < 0)? -(x) : (x)) 46 47 /* if defined, playback always uses the 2nd channel and full duplex works */ 48 #define ESS18XX_DUPLEX 1 49 50 /* more accurate clocks and split audio1/audio2 rates */ 51 #define ESS18XX_NEWSPEED 52 53 /* 1 = INTR_MPSAFE, 0 = GIANT */ 54 #define ESS18XX_MPSAFE 1 55 56 static u_int32_t ess_playfmt[] = { 57 SND_FORMAT(AFMT_U8, 1, 0), 58 SND_FORMAT(AFMT_U8, 2, 0), 59 SND_FORMAT(AFMT_S8, 1, 0), 60 SND_FORMAT(AFMT_S8, 2, 0), 61 SND_FORMAT(AFMT_S16_LE, 1, 0), 62 SND_FORMAT(AFMT_S16_LE, 2, 0), 63 SND_FORMAT(AFMT_U16_LE, 1, 0), 64 SND_FORMAT(AFMT_U16_LE, 2, 0), 65 0 66 }; 67 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0}; 68 69 /* 70 * Recording output is byte-swapped 71 */ 72 static u_int32_t ess_recfmt[] = { 73 SND_FORMAT(AFMT_U8, 1, 0), 74 SND_FORMAT(AFMT_U8, 2, 0), 75 SND_FORMAT(AFMT_S8, 1, 0), 76 SND_FORMAT(AFMT_S8, 2, 0), 77 SND_FORMAT(AFMT_S16_BE, 1, 0), 78 SND_FORMAT(AFMT_S16_BE, 2, 0), 79 SND_FORMAT(AFMT_U16_BE, 1, 0), 80 SND_FORMAT(AFMT_U16_BE, 2, 0), 81 0 82 }; 83 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0}; 84 85 struct ess_info; 86 87 struct ess_chinfo { 88 struct ess_info *parent; 89 struct pcm_channel *channel; 90 struct snd_dbuf *buffer; 91 int dir, hwch, stopping; 92 u_int32_t fmt, spd, blksz; 93 }; 94 95 struct ess_info { 96 struct resource *io, *sb, *vc, *mpu, *gp; /* I/O address for the board */ 97 struct resource *irq; 98 void *ih; 99 bus_dma_tag_t parent_dmat; 100 101 int simplex_dir, type, dmasz[2]; 102 unsigned int duplex:1, newspeed:1; 103 unsigned int bufsz; 104 105 struct ess_chinfo pch, rch; 106 #if ESS18XX_MPSAFE == 1 107 struct mtx *lock; 108 #endif 109 }; 110 111 #if ESS18XX_MPSAFE == 1 112 #define ess_lock(_ess) snd_mtxlock((_ess)->lock) 113 #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock) 114 #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock) 115 #else 116 #define ess_lock(_ess) 117 #define ess_unlock(_ess) 118 #define ess_lock_assert(_ess) 119 #endif 120 121 static int ess_rd(struct ess_info *sc, int reg); 122 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val); 123 static int ess_dspready(struct ess_info *sc); 124 static int ess_cmd(struct ess_info *sc, u_char val); 125 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val); 126 static int ess_get_byte(struct ess_info *sc); 127 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value); 128 static int ess_getmixer(struct ess_info *sc, u_int port); 129 static int ess_reset_dsp(struct ess_info *sc); 130 131 static int ess_write(struct ess_info *sc, u_char reg, int val); 132 static int ess_read(struct ess_info *sc, u_char reg); 133 134 static void ess_intr(void *arg); 135 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len); 136 static int ess_start(struct ess_chinfo *ch); 137 static int ess_stop(struct ess_chinfo *ch); 138 139 static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir); 140 static int ess_dmapos(struct ess_info *sc, int ch); 141 static int ess_dmatrigger(struct ess_info *sc, int ch, int go); 142 143 /* 144 * Common code for the midi and pcm functions 145 * 146 * ess_cmd write a single byte to the CMD port. 147 * ess_cmd1 write a CMD + 1 byte arg 148 * ess_cmd2 write a CMD + 2 byte arg 149 * ess_get_byte returns a single byte from the DSP data port 150 * 151 * ess_write is actually ess_cmd1 152 * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte 153 */ 154 155 static int 156 port_rd(struct resource *port, int regno, int size) 157 { 158 bus_space_tag_t st = rman_get_bustag(port); 159 bus_space_handle_t sh = rman_get_bushandle(port); 160 161 switch (size) { 162 case 1: 163 return bus_space_read_1(st, sh, regno); 164 case 2: 165 return bus_space_read_2(st, sh, regno); 166 case 4: 167 return bus_space_read_4(st, sh, regno); 168 default: 169 return 0xffffffff; 170 } 171 } 172 173 static void 174 port_wr(struct resource *port, int regno, u_int32_t data, int size) 175 { 176 bus_space_tag_t st = rman_get_bustag(port); 177 bus_space_handle_t sh = rman_get_bushandle(port); 178 179 switch (size) { 180 case 1: 181 bus_space_write_1(st, sh, regno, data); 182 break; 183 case 2: 184 bus_space_write_2(st, sh, regno, data); 185 break; 186 case 4: 187 bus_space_write_4(st, sh, regno, data); 188 break; 189 } 190 } 191 192 static int 193 ess_rd(struct ess_info *sc, int reg) 194 { 195 return port_rd(sc->sb, reg, 1); 196 } 197 198 static void 199 ess_wr(struct ess_info *sc, int reg, u_int8_t val) 200 { 201 port_wr(sc->sb, reg, val, 1); 202 } 203 204 static int 205 ess_dspready(struct ess_info *sc) 206 { 207 return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0); 208 } 209 210 static int 211 ess_dspwr(struct ess_info *sc, u_char val) 212 { 213 int i; 214 215 for (i = 0; i < 1000; i++) { 216 if (ess_dspready(sc)) { 217 ess_wr(sc, SBDSP_CMD, val); 218 return 1; 219 } 220 if (i > 10) DELAY((i > 100)? 1000 : 10); 221 } 222 printf("ess_dspwr(0x%02x) timed out.\n", val); 223 return 0; 224 } 225 226 static int 227 ess_cmd(struct ess_info *sc, u_char val) 228 { 229 DEB(printf("ess_cmd: %x\n", val)); 230 return ess_dspwr(sc, val); 231 } 232 233 static int 234 ess_cmd1(struct ess_info *sc, u_char cmd, int val) 235 { 236 DEB(printf("ess_cmd1: %x, %x\n", cmd, val)); 237 if (ess_dspwr(sc, cmd)) { 238 return ess_dspwr(sc, val & 0xff); 239 } else return 0; 240 } 241 242 static void 243 ess_setmixer(struct ess_info *sc, u_int port, u_int value) 244 { 245 DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);) 246 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 247 DELAY(10); 248 ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff)); 249 DELAY(10); 250 } 251 252 static int 253 ess_getmixer(struct ess_info *sc, u_int port) 254 { 255 int val; 256 257 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 258 DELAY(10); 259 val = ess_rd(sc, SB_MIX_DATA); 260 DELAY(10); 261 262 return val; 263 } 264 265 static int 266 ess_get_byte(struct ess_info *sc) 267 { 268 int i; 269 270 for (i = 1000; i > 0; i--) { 271 if (ess_rd(sc, 0xc) & 0x40) 272 return ess_rd(sc, DSP_READ); 273 else 274 DELAY(20); 275 } 276 return -1; 277 } 278 279 static int 280 ess_write(struct ess_info *sc, u_char reg, int val) 281 { 282 return ess_cmd1(sc, reg, val); 283 } 284 285 static int 286 ess_read(struct ess_info *sc, u_char reg) 287 { 288 return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1; 289 } 290 291 static int 292 ess_reset_dsp(struct ess_info *sc) 293 { 294 DEB(printf("ess_reset_dsp\n")); 295 ess_wr(sc, SBDSP_RST, 3); 296 DELAY(100); 297 ess_wr(sc, SBDSP_RST, 0); 298 if (ess_get_byte(sc) != 0xAA) { 299 DEB(printf("ess_reset_dsp failed\n")); 300 /* 301 rman_get_start(d->io_base))); 302 */ 303 return ENXIO; /* Sorry */ 304 } 305 ess_cmd(sc, 0xc6); 306 return 0; 307 } 308 309 static void 310 ess_intr(void *arg) 311 { 312 struct ess_info *sc = (struct ess_info *)arg; 313 int src, pirq = 0, rirq = 0; 314 315 ess_lock(sc); 316 src = 0; 317 if (ess_getmixer(sc, 0x7a) & 0x80) 318 src |= 2; 319 if (ess_rd(sc, 0x0c) & 0x01) 320 src |= 1; 321 322 if (src == 0) { 323 ess_unlock(sc); 324 return; 325 } 326 327 if (sc->duplex) { 328 pirq = (src & sc->pch.hwch)? 1 : 0; 329 rirq = (src & sc->rch.hwch)? 1 : 0; 330 } else { 331 if (sc->simplex_dir == PCMDIR_PLAY) 332 pirq = 1; 333 if (sc->simplex_dir == PCMDIR_REC) 334 rirq = 1; 335 if (!pirq && !rirq) 336 printf("solo: IRQ neither playback nor rec!\n"); 337 } 338 339 DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq)); 340 341 if (pirq) { 342 if (sc->pch.stopping) { 343 ess_dmatrigger(sc, sc->pch.hwch, 0); 344 sc->pch.stopping = 0; 345 if (sc->pch.hwch == 1) 346 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 347 else 348 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03); 349 } 350 ess_unlock(sc); 351 chn_intr(sc->pch.channel); 352 ess_lock(sc); 353 } 354 355 if (rirq) { 356 if (sc->rch.stopping) { 357 ess_dmatrigger(sc, sc->rch.hwch, 0); 358 sc->rch.stopping = 0; 359 /* XXX: will this stop audio2? */ 360 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 361 } 362 ess_unlock(sc); 363 chn_intr(sc->rch.channel); 364 ess_lock(sc); 365 } 366 367 if (src & 2) 368 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80); 369 if (src & 1) 370 ess_rd(sc, DSP_DATA_AVAIL); 371 372 ess_unlock(sc); 373 } 374 375 /* utility functions for ESS */ 376 static u_int8_t 377 ess_calcspeed8(int *spd) 378 { 379 int speed = *spd; 380 u_int32_t t; 381 382 if (speed > 22000) { 383 t = (795500 + speed / 2) / speed; 384 speed = (795500 + t / 2) / t; 385 t = (256 - t) | 0x80; 386 } else { 387 t = (397700 + speed / 2) / speed; 388 speed = (397700 + t / 2) / t; 389 t = 128 - t; 390 } 391 *spd = speed; 392 return t & 0x000000ff; 393 } 394 395 static u_int8_t 396 ess_calcspeed9(int *spd) 397 { 398 int speed, s0, s1, use0; 399 u_int8_t t0, t1; 400 401 /* rate = source / (256 - divisor) */ 402 /* divisor = 256 - (source / rate) */ 403 speed = *spd; 404 t0 = 128 - (793800 / speed); 405 s0 = 793800 / (128 - t0); 406 407 t1 = 128 - (768000 / speed); 408 s1 = 768000 / (128 - t1); 409 t1 |= 0x80; 410 411 use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0; 412 413 *spd = use0? s0 : s1; 414 return use0? t0 : t1; 415 } 416 417 static u_int8_t 418 ess_calcfilter(int spd) 419 { 420 int cutoff; 421 422 /* cutoff = 7160000 / (256 - divisor) */ 423 /* divisor = 256 - (7160000 / cutoff) */ 424 cutoff = (spd * 9 * 82) / 20; 425 return (256 - (7160000 / cutoff)); 426 } 427 428 static int 429 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len) 430 { 431 int play = (dir == PCMDIR_PLAY)? 1 : 0; 432 int b16 = (fmt & AFMT_16BIT)? 1 : 0; 433 int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0; 434 int unsign = (!(fmt & AFMT_SIGNED))? 1 : 0; 435 u_int8_t spdval, fmtval; 436 437 DEB(printf("ess_setupch\n")); 438 spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd); 439 440 sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ; 441 442 if (ch == 1) { 443 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad")); 444 len = -len; 445 /* transfer length low */ 446 ess_write(sc, 0xa4, len & 0x00ff); 447 /* transfer length high */ 448 ess_write(sc, 0xa5, (len & 0xff00) >> 8); 449 /* autoinit, dma dir */ 450 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a)); 451 /* mono/stereo */ 452 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02)); 453 /* demand mode, 4 bytes/xfer */ 454 ess_write(sc, 0xb9, 0x02); 455 /* sample rate */ 456 ess_write(sc, 0xa1, spdval); 457 /* filter cutoff */ 458 ess_write(sc, 0xa2, ess_calcfilter(spd)); 459 /* setup dac/adc */ 460 /* 461 if (play) 462 ess_write(sc, 0xb6, unsign? 0x80 : 0x00); 463 */ 464 /* mono, b16: signed, load signal */ 465 /* 466 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20)); 467 */ 468 /* setup fifo */ 469 ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) | 470 (b16? 0x04 : 0x00) | 471 (stereo? 0x08 : 0x40)); 472 /* irq control */ 473 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50); 474 /* drq control */ 475 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50); 476 } else if (ch == 2) { 477 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad")); 478 len >>= 1; 479 len = -len; 480 /* transfer length low */ 481 ess_setmixer(sc, 0x74, len & 0x00ff); 482 /* transfer length high */ 483 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8); 484 /* autoinit, 4 bytes/req */ 485 ess_setmixer(sc, 0x78, 0x10); 486 fmtval = b16 | (stereo << 1) | ((!unsign) << 2); 487 /* enable irq, set format */ 488 ess_setmixer(sc, 0x7a, 0x40 | fmtval); 489 if (sc->newspeed) { 490 /* sample rate */ 491 ess_setmixer(sc, 0x70, spdval); 492 /* filter cutoff */ 493 ess_setmixer(sc, 0x72, ess_calcfilter(spd)); 494 } 495 } 496 return 0; 497 } 498 static int 499 ess_start(struct ess_chinfo *ch) 500 { 501 struct ess_info *sc = ch->parent; 502 503 DEB(printf("ess_start\n");); 504 ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz); 505 ch->stopping = 0; 506 if (ch->hwch == 1) { 507 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01); 508 if (ch->dir == PCMDIR_PLAY) { 509 #if 0 510 DELAY(100000); /* 100 ms */ 511 #endif 512 ess_cmd(sc, 0xd1); 513 } 514 } else 515 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03); 516 return 0; 517 } 518 519 static int 520 ess_stop(struct ess_chinfo *ch) 521 { 522 struct ess_info *sc = ch->parent; 523 524 DEB(printf("ess_stop\n")); 525 ch->stopping = 1; 526 if (ch->hwch == 1) 527 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04); 528 else 529 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10); 530 DEB(printf("done with stop\n")); 531 return 0; 532 } 533 534 /* -------------------------------------------------------------------- */ 535 /* channel interface for ESS18xx */ 536 static void * 537 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 538 { 539 struct ess_info *sc = devinfo; 540 struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch; 541 542 DEB(printf("esschan_init\n")); 543 ch->parent = sc; 544 ch->channel = c; 545 ch->buffer = b; 546 ch->dir = dir; 547 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0) 548 return NULL; 549 ch->hwch = 1; 550 if ((dir == PCMDIR_PLAY) && (sc->duplex)) 551 ch->hwch = 2; 552 return ch; 553 } 554 555 static int 556 esschan_setformat(kobj_t obj, void *data, u_int32_t format) 557 { 558 struct ess_chinfo *ch = data; 559 560 ch->fmt = format; 561 return 0; 562 } 563 564 static u_int32_t 565 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed) 566 { 567 struct ess_chinfo *ch = data; 568 struct ess_info *sc = ch->parent; 569 570 ch->spd = speed; 571 if (sc->newspeed) 572 ess_calcspeed9(&ch->spd); 573 else 574 ess_calcspeed8(&ch->spd); 575 return ch->spd; 576 } 577 578 static u_int32_t 579 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 580 { 581 struct ess_chinfo *ch = data; 582 583 ch->blksz = blocksize; 584 return ch->blksz; 585 } 586 587 static int 588 esschan_trigger(kobj_t obj, void *data, int go) 589 { 590 struct ess_chinfo *ch = data; 591 struct ess_info *sc = ch->parent; 592 593 if (!PCMTRIG_COMMON(go)) 594 return 0; 595 596 DEB(printf("esschan_trigger: %d\n",go)); 597 598 ess_lock(sc); 599 switch (go) { 600 case PCMTRIG_START: 601 ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir); 602 ess_dmatrigger(sc, ch->hwch, 1); 603 ess_start(ch); 604 break; 605 606 case PCMTRIG_STOP: 607 case PCMTRIG_ABORT: 608 default: 609 ess_stop(ch); 610 break; 611 } 612 ess_unlock(sc); 613 return 0; 614 } 615 616 static u_int32_t 617 esschan_getptr(kobj_t obj, void *data) 618 { 619 struct ess_chinfo *ch = data; 620 struct ess_info *sc = ch->parent; 621 u_int32_t ret; 622 623 ess_lock(sc); 624 ret = ess_dmapos(sc, ch->hwch); 625 ess_unlock(sc); 626 return ret; 627 } 628 629 static struct pcmchan_caps * 630 esschan_getcaps(kobj_t obj, void *data) 631 { 632 struct ess_chinfo *ch = data; 633 634 return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps; 635 } 636 637 static kobj_method_t esschan_methods[] = { 638 KOBJMETHOD(channel_init, esschan_init), 639 KOBJMETHOD(channel_setformat, esschan_setformat), 640 KOBJMETHOD(channel_setspeed, esschan_setspeed), 641 KOBJMETHOD(channel_setblocksize, esschan_setblocksize), 642 KOBJMETHOD(channel_trigger, esschan_trigger), 643 KOBJMETHOD(channel_getptr, esschan_getptr), 644 KOBJMETHOD(channel_getcaps, esschan_getcaps), 645 KOBJMETHOD_END 646 }; 647 CHANNEL_DECLARE(esschan); 648 649 /************************************************************/ 650 651 static int 652 essmix_init(struct snd_mixer *m) 653 { 654 struct ess_info *sc = mix_getdevinfo(m); 655 656 mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE | 657 SOUND_MASK_IMIX); 658 659 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE | 660 SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME | 661 SOUND_MASK_LINE1); 662 663 ess_setmixer(sc, 0, 0); /* reset */ 664 665 return 0; 666 } 667 668 static int 669 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 670 { 671 struct ess_info *sc = mix_getdevinfo(m); 672 int preg = 0, rreg = 0, l, r; 673 674 l = (left * 15) / 100; 675 r = (right * 15) / 100; 676 switch (dev) { 677 case SOUND_MIXER_SYNTH: 678 preg = 0x36; 679 rreg = 0x6b; 680 break; 681 682 case SOUND_MIXER_PCM: 683 preg = 0x14; 684 rreg = 0x7c; 685 break; 686 687 case SOUND_MIXER_LINE: 688 preg = 0x3e; 689 rreg = 0x6e; 690 break; 691 692 case SOUND_MIXER_MIC: 693 preg = 0x1a; 694 rreg = 0x68; 695 break; 696 697 case SOUND_MIXER_LINE1: 698 preg = 0x3a; 699 rreg = 0x6c; 700 break; 701 702 case SOUND_MIXER_CD: 703 preg = 0x38; 704 rreg = 0x6a; 705 break; 706 707 case SOUND_MIXER_VOLUME: 708 l = left? (left * 63) / 100 : 64; 709 r = right? (right * 63) / 100 : 64; 710 ess_setmixer(sc, 0x60, l); 711 ess_setmixer(sc, 0x62, r); 712 left = (l == 64)? 0 : (l * 100) / 63; 713 right = (r == 64)? 0 : (r * 100) / 63; 714 return left | (right << 8); 715 } 716 717 if (preg) 718 ess_setmixer(sc, preg, (l << 4) | r); 719 if (rreg) 720 ess_setmixer(sc, rreg, (l << 4) | r); 721 722 left = (l * 100) / 15; 723 right = (r * 100) / 15; 724 725 return left | (right << 8); 726 } 727 728 static u_int32_t 729 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src) 730 { 731 struct ess_info *sc = mix_getdevinfo(m); 732 u_char recdev; 733 734 switch (src) { 735 case SOUND_MASK_CD: 736 recdev = 0x02; 737 break; 738 739 case SOUND_MASK_LINE: 740 recdev = 0x06; 741 break; 742 743 case SOUND_MASK_IMIX: 744 recdev = 0x05; 745 break; 746 747 case SOUND_MASK_MIC: 748 default: 749 recdev = 0x00; 750 src = SOUND_MASK_MIC; 751 break; 752 } 753 754 ess_setmixer(sc, 0x1c, recdev); 755 756 return src; 757 } 758 759 static kobj_method_t solomixer_methods[] = { 760 KOBJMETHOD(mixer_init, essmix_init), 761 KOBJMETHOD(mixer_set, essmix_set), 762 KOBJMETHOD(mixer_setrecsrc, essmix_setrecsrc), 763 KOBJMETHOD_END 764 }; 765 MIXER_DECLARE(solomixer); 766 767 /************************************************************/ 768 769 static int 770 ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir) 771 { 772 KASSERT(ch == 1 || ch == 2, ("bad ch")); 773 sc->dmasz[ch - 1] = cnt; 774 if (ch == 1) { 775 port_wr(sc->vc, 0x8, 0xc4, 1); /* command */ 776 port_wr(sc->vc, 0xd, 0xff, 1); /* reset */ 777 port_wr(sc->vc, 0xf, 0x01, 1); /* mask */ 778 port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */ 779 port_wr(sc->vc, 0x0, base, 4); 780 port_wr(sc->vc, 0x4, cnt - 1, 2); 781 782 } else if (ch == 2) { 783 port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */ 784 port_wr(sc->io, 0x0, base, 4); 785 port_wr(sc->io, 0x4, cnt, 2); 786 } 787 return 0; 788 } 789 790 static int 791 ess_dmapos(struct ess_info *sc, int ch) 792 { 793 int p = 0, i = 0, j = 0; 794 795 KASSERT(ch == 1 || ch == 2, ("bad ch")); 796 if (ch == 1) { 797 /* 798 * During recording, this register is known to give back 799 * garbage if it's not quiescent while being read. That's 800 * why we spl, stop the DMA, and try over and over until 801 * adjacent reads are "close", in the right order and not 802 * bigger than is otherwise possible. 803 */ 804 ess_dmatrigger(sc, ch, 0); 805 DELAY(20); 806 do { 807 DELAY(10); 808 if (j > 1) 809 printf("DMA count reg bogus: %04x & %04x\n", 810 i, p); 811 i = port_rd(sc->vc, 0x4, 2) + 1; 812 p = port_rd(sc->vc, 0x4, 2) + 1; 813 } while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000); 814 ess_dmatrigger(sc, ch, 1); 815 } 816 else if (ch == 2) 817 p = port_rd(sc->io, 0x4, 2); 818 return sc->dmasz[ch - 1] - p; 819 } 820 821 static int 822 ess_dmatrigger(struct ess_info *sc, int ch, int go) 823 { 824 KASSERT(ch == 1 || ch == 2, ("bad ch")); 825 if (ch == 1) 826 port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */ 827 else if (ch == 2) 828 port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */ 829 return 0; 830 } 831 832 static void 833 ess_release_resources(struct ess_info *sc, device_t dev) 834 { 835 if (sc->irq) { 836 if (sc->ih) 837 bus_teardown_intr(dev, sc->irq, sc->ih); 838 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 839 sc->irq = NULL; 840 } 841 if (sc->io) { 842 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io); 843 sc->io = NULL; 844 } 845 846 if (sc->sb) { 847 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb); 848 sc->sb = NULL; 849 } 850 851 if (sc->vc) { 852 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc); 853 sc->vc = NULL; 854 } 855 856 if (sc->mpu) { 857 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu); 858 sc->mpu = NULL; 859 } 860 861 if (sc->gp) { 862 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp); 863 sc->gp = NULL; 864 } 865 866 if (sc->parent_dmat) { 867 bus_dma_tag_destroy(sc->parent_dmat); 868 sc->parent_dmat = 0; 869 } 870 871 #if ESS18XX_MPSAFE == 1 872 if (sc->lock) { 873 snd_mtxfree(sc->lock); 874 sc->lock = NULL; 875 } 876 #endif 877 878 free(sc, M_DEVBUF); 879 } 880 881 static int 882 ess_alloc_resources(struct ess_info *sc, device_t dev) 883 { 884 int rid; 885 886 rid = PCIR_BAR(0); 887 sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 888 889 rid = PCIR_BAR(1); 890 sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 891 892 rid = PCIR_BAR(2); 893 sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 894 895 rid = PCIR_BAR(3); 896 sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 897 898 rid = PCIR_BAR(4); 899 sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 900 901 rid = 0; 902 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 903 RF_ACTIVE | RF_SHAREABLE); 904 905 #if ESS18XX_MPSAFE == 1 906 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_solo softc"); 907 908 return (sc->irq && sc->io && sc->sb && sc->vc && 909 sc->mpu && sc->gp && sc->lock)? 0 : ENXIO; 910 #else 911 return (sc->irq && sc->io && sc->sb && sc->vc && sc->mpu && sc->gp)? 0 : ENXIO; 912 #endif 913 } 914 915 static int 916 ess_probe(device_t dev) 917 { 918 char *s = NULL; 919 u_int32_t subdev; 920 921 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 922 switch (pci_get_devid(dev)) { 923 case 0x1969125d: 924 if (subdev == 0x8888125d) 925 s = "ESS Solo-1E"; 926 else if (subdev == 0x1818125d) 927 s = "ESS Solo-1"; 928 else 929 s = "ESS Solo-1 (unknown vendor)"; 930 break; 931 } 932 933 if (s) 934 device_set_desc(dev, s); 935 return s ? BUS_PROBE_DEFAULT : ENXIO; 936 } 937 938 #define ESS_PCI_LEGACYCONTROL 0x40 939 #define ESS_PCI_CONFIG 0x50 940 #define ESS_PCI_DDMACONTROL 0x60 941 942 static int 943 ess_suspend(device_t dev) 944 { 945 return 0; 946 } 947 948 static int 949 ess_resume(device_t dev) 950 { 951 uint16_t ddma; 952 struct ess_info *sc = pcm_getdevinfo(dev); 953 954 ess_lock(sc); 955 ddma = rman_get_start(sc->vc) | 1; 956 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 957 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 958 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 959 960 if (ess_reset_dsp(sc)) { 961 ess_unlock(sc); 962 goto no; 963 } 964 ess_unlock(sc); 965 if (mixer_reinit(dev)) 966 goto no; 967 ess_lock(sc); 968 if (sc->newspeed) 969 ess_setmixer(sc, 0x71, 0x2a); 970 971 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 972 ess_unlock(sc); 973 974 return 0; 975 no: 976 return EIO; 977 } 978 979 static int 980 ess_attach(device_t dev) 981 { 982 struct ess_info *sc; 983 char status[SND_STATUSLEN]; 984 u_int16_t ddma; 985 986 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 987 pci_enable_busmaster(dev); 988 989 if (ess_alloc_resources(sc, dev)) 990 goto no; 991 992 sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536); 993 994 ddma = rman_get_start(sc->vc) | 1; 995 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 996 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 997 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 998 999 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 1000 #ifdef ESS18XX_DUPLEX 1001 sc->duplex = 1; 1002 #else 1003 sc->duplex = 0; 1004 #endif 1005 1006 #ifdef ESS18XX_NEWSPEED 1007 sc->newspeed = 1; 1008 #else 1009 sc->newspeed = 0; 1010 #endif 1011 if (snd_setup_intr(dev, sc->irq, 1012 #if ESS18XX_MPSAFE == 1 1013 INTR_MPSAFE 1014 #else 1015 0 1016 #endif 1017 , ess_intr, sc, &sc->ih)) { 1018 device_printf(dev, "unable to map interrupt\n"); 1019 goto no; 1020 } 1021 1022 if (!sc->duplex) 1023 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 1024 1025 #if 0 1026 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/65536, /*boundary*/0, 1027 #endif 1028 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, /*boundary*/0, 1029 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 1030 /*highaddr*/BUS_SPACE_MAXADDR, 1031 /*filter*/NULL, /*filterarg*/NULL, 1032 /*maxsize*/sc->bufsz, /*nsegments*/1, 1033 /*maxsegz*/0x3ffff, 1034 /*flags*/0, 1035 #if ESS18XX_MPSAFE == 1 1036 /*lockfunc*/NULL, /*lockarg*/NULL, 1037 #else 1038 /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, 1039 #endif 1040 &sc->parent_dmat) != 0) { 1041 device_printf(dev, "unable to create dma tag\n"); 1042 goto no; 1043 } 1044 1045 if (ess_reset_dsp(sc)) 1046 goto no; 1047 1048 if (sc->newspeed) 1049 ess_setmixer(sc, 0x71, 0x2a); 1050 1051 if (mixer_init(dev, &solomixer_class, sc)) 1052 goto no; 1053 1054 snprintf(status, SND_STATUSLEN, "at io 0x%jx,0x%jx,0x%jx irq %jd %s", 1055 rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc), 1056 rman_get_start(sc->irq),PCM_KLDSTRING(snd_solo)); 1057 1058 if (pcm_register(dev, sc, 1, 1)) 1059 goto no; 1060 pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc); 1061 pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc); 1062 pcm_setstatus(dev, status); 1063 1064 return 0; 1065 1066 no: 1067 ess_release_resources(sc, dev); 1068 return ENXIO; 1069 } 1070 1071 static int 1072 ess_detach(device_t dev) 1073 { 1074 int r; 1075 struct ess_info *sc; 1076 1077 r = pcm_unregister(dev); 1078 if (r) 1079 return r; 1080 1081 sc = pcm_getdevinfo(dev); 1082 ess_release_resources(sc, dev); 1083 return 0; 1084 } 1085 1086 static device_method_t ess_methods[] = { 1087 /* Device interface */ 1088 DEVMETHOD(device_probe, ess_probe), 1089 DEVMETHOD(device_attach, ess_attach), 1090 DEVMETHOD(device_detach, ess_detach), 1091 DEVMETHOD(device_resume, ess_resume), 1092 DEVMETHOD(device_suspend, ess_suspend), 1093 { 0, 0 } 1094 }; 1095 1096 static driver_t ess_driver = { 1097 "pcm", 1098 ess_methods, 1099 PCM_SOFTC_SIZE, 1100 }; 1101 1102 DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, 0, 0); 1103 MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1104 MODULE_VERSION(snd_solo, 1); 1105