sb16.c revision 50723
1/* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * Copyright 1997,1998 Luigi Rizzo. 4 * 5 * Derived from files in the Voxware 3.5 distribution, 6 * Copyright by Hannu Savolainen 1994, under the same copyright 7 * conditions. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $Id$ 32 */ 33 34#include <dev/pcm/sound.h> 35#if NPCM > 0 36 37#define __SB_MIXER_C__ /* XXX warning... */ 38#include <dev/pcm/isa/sb.h> 39 40/* channel interface */ 41static void *sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 42static int sbchan_setdir(void *data, int dir); 43static int sbchan_setformat(void *data, u_int32_t format); 44static int sbchan_setspeed(void *data, u_int32_t speed); 45static int sbchan_setblocksize(void *data, u_int32_t blocksize); 46static int sbchan_trigger(void *data, int go); 47static int sbchan_getptr(void *data); 48static pcmchan_caps *sbchan_getcaps(void *data); 49 50static pcmchan_caps sb_playcaps = { 51 4000, 22050, 52 AFMT_U8, 53 AFMT_U8 54}; 55 56static pcmchan_caps sb_reccaps = { 57 4000, 13000, 58 AFMT_U8, 59 AFMT_U8 60}; 61 62static pcmchan_caps sbpro_playcaps = { 63 4000, 45000, 64 AFMT_STEREO | AFMT_U8, 65 AFMT_STEREO | AFMT_U8 66}; 67 68static pcmchan_caps sbpro_reccaps = { 69 4000, 15000, 70 AFMT_STEREO | AFMT_U8, 71 AFMT_STEREO | AFMT_U8 72}; 73 74static pcmchan_caps sb16_playcaps = { 75 5000, 45000, 76 AFMT_STEREO | AFMT_S16_LE, 77 AFMT_STEREO | AFMT_S16_LE 78}; 79 80static pcmchan_caps sb16_reccaps = { 81 5000, 45000, 82 AFMT_STEREO | AFMT_U8, 83 AFMT_STEREO | AFMT_U8 84}; 85 86static pcmchan_caps ess_playcaps = { 87 5000, 49000, 88 AFMT_STEREO | AFMT_U8 | AFMT_S16_LE, 89 AFMT_STEREO | AFMT_S16_LE 90}; 91 92static pcmchan_caps ess_reccaps = { 93 5000, 49000, 94 AFMT_STEREO | AFMT_U8 | AFMT_S16_LE, 95 AFMT_STEREO | AFMT_S16_LE 96}; 97 98static pcm_channel sb_chantemplate = { 99 sbchan_init, 100 sbchan_setdir, 101 sbchan_setformat, 102 sbchan_setspeed, 103 sbchan_setblocksize, 104 sbchan_trigger, 105 sbchan_getptr, 106 sbchan_getcaps, 107}; 108 109#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16) 110 111struct sb_info; 112 113struct sb_chinfo { 114 struct sb_info *parent; 115 pcm_channel *channel; 116 snd_dbuf *buffer; 117 int dir; 118 u_int32_t fmt; 119}; 120 121struct sb_info { 122 struct resource *io_base; /* I/O address for the board */ 123 int io_rid; 124 struct resource *irq; 125 int irq_rid; 126 struct resource *drq1; /* play */ 127 int drq1_rid; 128 struct resource *drq2; /* rec */ 129 int drq2_rid; 130 bus_dma_tag_t parent_dmat; 131 132 int dma16, dma8; 133 int bd_id; 134 u_long bd_flags; /* board-specific flags */ 135 struct sb_chinfo pch, rch; 136}; 137 138static int sb_rd(struct sb_info *sb, int reg); 139static void sb_wr(struct sb_info *sb, int reg, u_int8_t val); 140static int sb_dspready(struct sb_info *sb); 141static int sb_cmd(struct sb_info *sb, u_char val); 142static int sb_cmd1(struct sb_info *sb, u_char cmd, int val); 143static int sb_cmd2(struct sb_info *sb, u_char cmd, int val); 144static u_int sb_get_byte(struct sb_info *sb); 145static int ess_write(struct sb_info *sb, u_char reg, int val); 146static int ess_read(struct sb_info *sb, u_char reg); 147 148/* 149 * in the SB, there is a set of indirect "mixer" registers with 150 * address at offset 4, data at offset 5 151 */ 152static void sb_setmixer(struct sb_info *sb, u_int port, u_int value); 153static int sb_getmixer(struct sb_info *sb, u_int port); 154 155static void sb_intr(void *arg); 156static int sb_init(device_t dev, struct sb_info *sb); 157static int sb_reset_dsp(struct sb_info *sb); 158 159static int sb_format(struct sb_chinfo *ch, u_int32_t format); 160static int sb_speed(struct sb_chinfo *ch, int speed); 161static int sb_start(struct sb_chinfo *ch); 162static int sb_stop(struct sb_chinfo *ch); 163 164static int sbmix_init(snd_mixer *m); 165static int sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right); 166static int sbmix_setrecsrc(snd_mixer *m, u_int32_t src); 167 168static snd_mixer sb_mixer = { 169 "SoundBlaster mixer", 170 sbmix_init, 171 sbmix_set, 172 sbmix_setrecsrc, 173}; 174 175static devclass_t pcm_devclass; 176 177/* 178 * Common code for the midi and pcm functions 179 * 180 * sb_cmd write a single byte to the CMD port. 181 * sb_cmd1 write a CMD + 1 byte arg 182 * sb_cmd2 write a CMD + 2 byte arg 183 * sb_get_byte returns a single byte from the DSP data port 184 * 185 * ess_write is actually sb_cmd1 186 * ess_read access ext. regs via sb_cmd(0xc0, reg) followed by sb_get_byte 187 */ 188 189static int 190port_rd(struct resource *port, int off) 191{ 192 return bus_space_read_1(rman_get_bustag(port), 193 rman_get_bushandle(port), 194 off); 195} 196 197static void 198port_wr(struct resource *port, int off, u_int8_t data) 199{ 200 return bus_space_write_1(rman_get_bustag(port), 201 rman_get_bushandle(port), 202 off, data); 203} 204 205static int 206sb_rd(struct sb_info *sb, int reg) 207{ 208 return port_rd(sb->io_base, reg); 209} 210 211static void 212sb_wr(struct sb_info *sb, int reg, u_int8_t val) 213{ 214 port_wr(sb->io_base, reg, val); 215} 216 217static int 218sb_dspready(struct sb_info *sb) 219{ 220 return ((sb_rd(sb, SBDSP_STATUS) & 0x80) == 0); 221} 222 223static int 224sb_dspwr(struct sb_info *sb, u_char val) 225{ 226 int i; 227 228 for (i = 0; i < 1000; i++) { 229 if (sb_dspready(sb)) { 230 sb_wr(sb, SBDSP_CMD, val); 231 return 1; 232 } 233 if (i > 10) DELAY((i > 100)? 1000 : 10); 234 } 235 printf("sb_dspwr(0x%02x) timed out.\n", val); 236 return 0; 237} 238 239static int 240sb_cmd(struct sb_info *sb, u_char val) 241{ 242#if 0 243 printf("sb_cmd: %x\n", val); 244#endif 245 return sb_dspwr(sb, val); 246} 247 248static int 249sb_cmd1(struct sb_info *sb, u_char cmd, int val) 250{ 251#if 0 252 printf("sb_cmd1: %x, %x\n", cmd, val); 253#endif 254 if (sb_dspwr(sb, cmd)) { 255 return sb_dspwr(sb, val & 0xff); 256 } else return 0; 257} 258 259static int 260sb_cmd2(struct sb_info *sb, u_char cmd, int val) 261{ 262#if 0 263 printf("sb_cmd2: %x, %x\n", cmd, val); 264#endif 265 if (sb_dspwr(sb, cmd)) { 266 return sb_dspwr(sb, val & 0xff) && 267 sb_dspwr(sb, (val >> 8) & 0xff); 268 } else return 0; 269} 270 271/* 272 * in the SB, there is a set of indirect "mixer" registers with 273 * address at offset 4, data at offset 5 274 */ 275static void 276sb_setmixer(struct sb_info *sb, u_int port, u_int value) 277{ 278 u_long flags; 279 280 flags = spltty(); 281 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 282 DELAY(10); 283 sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff)); 284 DELAY(10); 285 splx(flags); 286} 287 288static int 289sb_getmixer(struct sb_info *sb, u_int port) 290{ 291 int val; 292 u_long flags; 293 294 flags = spltty(); 295 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 296 DELAY(10); 297 val = sb_rd(sb, SB_MIX_DATA); 298 DELAY(10); 299 splx(flags); 300 301 return val; 302} 303 304static u_int 305sb_get_byte(struct sb_info *sb) 306{ 307 int i; 308 309 for (i = 1000; i > 0; i--) { 310 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80) 311 return sb_rd(sb, DSP_READ); 312 else 313 DELAY(20); 314 } 315 return 0xffff; 316} 317 318static int 319ess_write(struct sb_info *sb, u_char reg, int val) 320{ 321 return sb_cmd1(sb, reg, val); 322} 323 324static int 325ess_read(struct sb_info *sb, u_char reg) 326{ 327 return (sb_cmd(sb, 0xc0) && sb_cmd(sb, reg))? sb_get_byte(sb) : 0xffff; 328} 329 330static int 331sb_reset_dsp(struct sb_info *sb) 332{ 333 sb_wr(sb, SBDSP_RST, 3); 334 DELAY(100); 335 sb_wr(sb, SBDSP_RST, 0); 336 if (sb_get_byte(sb) != 0xAA) { 337 DEB(printf("sb_reset_dsp 0x%lx failed\n", 338 rman_get_start(d->io_base))); 339 return ENXIO; /* Sorry */ 340 } 341 if (sb->bd_flags & BD_F_ESS) sb_cmd(sb, 0xc6); 342 return 0; 343} 344 345static void 346sb_release_resources(struct sb_info *sb, device_t dev) 347{ 348 /* should we bus_teardown_intr here? */ 349 if (sb->irq) { 350 bus_release_resource(dev, SYS_RES_IRQ, sb->irq_rid, sb->irq); 351 sb->irq = 0; 352 } 353 if (sb->drq1) { 354 bus_release_resource(dev, SYS_RES_DRQ, sb->drq1_rid, sb->drq1); 355 sb->drq1 = 0; 356 } 357 if (sb->drq2) { 358 bus_release_resource(dev, SYS_RES_DRQ, sb->drq2_rid, sb->drq2); 359 sb->drq2 = 0; 360 } 361 if (sb->io_base) { 362 bus_release_resource(dev, SYS_RES_IOPORT, sb->io_rid, 363 sb->io_base); 364 sb->io_base = 0; 365 } 366 free(sb, M_DEVBUF); 367} 368 369static int 370sb_alloc_resources(struct sb_info *sb, device_t dev) 371{ 372 if (!sb->io_base) 373 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, 374 &sb->io_rid, 0, ~0, 1, 375 RF_ACTIVE); 376 if (!sb->irq) 377 sb->irq = bus_alloc_resource(dev, SYS_RES_IRQ, 378 &sb->irq_rid, 0, ~0, 1, 379 RF_ACTIVE); 380 if (!sb->drq1) 381 sb->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, 382 &sb->drq1_rid, 0, ~0, 1, 383 RF_ACTIVE); 384 if (!sb->drq2 && sb->drq2_rid > 0) 385 sb->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, 386 &sb->drq2_rid, 0, ~0, 1, 387 RF_ACTIVE); 388 389 if (sb->io_base && sb->drq1 && sb->irq) { 390 sb->dma8 = rman_get_start(sb->drq1); 391 isa_dma_acquire(sb->dma8); 392 isa_dmainit(sb->dma8, DSP_BUFFSIZE); 393 394 if (sb->drq2) { 395 sb->dma16 = rman_get_start(sb->drq2); 396 isa_dma_acquire(sb->dma16); 397 isa_dmainit(sb->dma16, DSP_BUFFSIZE); 398 } else sb->dma16 = sb->dma8; 399 400 if (sb->dma8 > sb->dma16) { 401 int tmp = sb->dma16; 402 sb->dma16 = sb->dma8; 403 sb->dma8 = tmp; 404 } 405 return 0; 406 } else return ENXIO; 407} 408 409static int 410sb_identify_board(device_t dev, struct sb_info *sb) 411{ 412 char *fmt = NULL; 413 static char buf[64]; 414 415 sb_cmd(sb, DSP_CMD_GETVER); /* Get version */ 416 sb->bd_id = (sb_get_byte(sb) << 8) | sb_get_byte(sb); 417 418 switch (sb->bd_id >> 8) { 419 case 1: /* old sound blaster has nothing... */ 420 case 2: 421 fmt = "SoundBlaster %d.%d" ; /* default */ 422 break; 423 424 case 3: 425 fmt = "SoundBlaster Pro %d.%d"; 426 if (sb->bd_id == 0x301) { 427 int essver, rev; 428 429 /* Try to detect ESS chips. */ 430 sb_cmd(sb, DSP_CMD_GETID); /* Return ident. bytes. */ 431 essver = (sb_get_byte(sb) << 8) | sb_get_byte(sb); 432 rev = essver & 0x000f; 433 essver &= 0xfff0; 434 if (essver == 0x4880) { 435 /* the ESS488 can be treated as an SBPRO */ 436 fmt = "SoundBlaster Pro (ESS488 rev %d)"; 437 } else if (essver == 0x6880) { 438 if (rev < 8) fmt = "SoundBlaster Pro (ESS688 rev %d)"; 439 else fmt = "SoundBlaster Pro (ESS1868 rev %d)"; 440 sb->bd_flags |= BD_F_ESS; 441 } else return ENXIO; 442 sb->bd_id &= 0xff00; 443 sb->bd_id |= ((essver & 0xf000) >> 8) | rev; 444 } 445 break; 446 447 case 4: 448 sb->bd_flags |= BD_F_SB16; 449 fmt = "SoundBlaster 16 %d.%d"; 450 break; 451 452 default: 453 device_printf(dev, "failed to get SB version (%x)\n", 454 sb->bd_id); 455 return ENXIO; 456 457 } 458 if ((sb->bd_id >> 8) <= 4) snprintf(buf, sizeof buf, fmt, 459 sb->bd_id >> 8, sb->bd_id & 0xff); 460 else snprintf(buf, sizeof buf, fmt, sb->bd_id & 0x000f); 461 device_set_desc_copy(dev, buf); 462 return sb_reset_dsp(sb); 463} 464 465static int 466sb_init(device_t dev, struct sb_info *sb) 467{ 468 int x, irq; 469 470 sb->bd_flags &= ~BD_F_MIX_MASK; 471 /* do various initializations depending on board id. */ 472 switch (sb->bd_id >> 8) { 473 case 1: /* old sound blaster has nothing... */ 474 break; 475 476 case 2: 477 sb->bd_flags |= BD_F_DUP_MIDI; 478 if (sb->bd_id > 0x200) sb->bd_flags |= BD_F_MIX_CT1335; 479 break; 480 481 case 3: 482 sb->bd_flags |= BD_F_DUP_MIDI | BD_F_MIX_CT1345; 483 break; 484 485 case 4: 486 sb->bd_flags |= BD_F_SB16 | BD_F_MIX_CT1745; 487 if (sb->dma16 != sb->dma8) sb->bd_flags |= BD_F_DUPLEX; 488 489 /* soft irq/dma configuration */ 490 x = -1; 491 irq = rman_get_start(sb->irq); 492 if (irq == 5) x = 2; 493 else if (irq == 7) x = 4; 494 else if (irq == 9) x = 1; 495 else if (irq == 10) x = 8; 496 if (x == -1) device_printf(dev, 497 "bad irq %d (5/7/9/10 valid)\n", 498 irq); 499 else sb_setmixer(sb, IRQ_NR, x); 500 sb_setmixer(sb, DMA_NR, (1 << sb->dma16) | (1 << sb->dma8)); 501 break; 502 } 503 return 0; 504} 505 506static int 507sb_probe(device_t dev) 508{ 509 snddev_info *d = device_get_softc(dev); 510 struct sb_info *sb; 511 int allocated, i; 512 int error; 513 514 if (isa_get_vendorid(dev)) return ENXIO; /* not yet */ 515 516 device_set_desc(dev, "SoundBlaster"); 517 bzero(d, sizeof *d); 518 sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT); 519 if (!sb) return ENXIO; 520 bzero(sb, sizeof *sb); 521 522 allocated = 0; 523 sb->io_rid = 0; 524 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &sb->io_rid, 525 0, ~0, 16, RF_ACTIVE); 526 if (!sb->io_base) { 527 BVDDB(printf("sb_probe: no addr, trying (0x220, 0x240)\n")); 528 allocated = 1; 529 sb->io_rid = 0; 530 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, 531 &sb->io_rid, 0x220, 0x22f, 532 16, RF_ACTIVE); 533 if (!sb->io_base) { 534 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, 535 &sb->io_rid, 0x240, 536 0x24f, 16, RF_ACTIVE); 537 } 538 } 539 if (!sb->io_base) return ENXIO; 540 541 error = sb_reset_dsp(sb); 542 if (error) goto no; 543 error = sb_identify_board(dev, sb); 544 if (error) goto no; 545no: 546 i = sb->io_rid; 547 sb_release_resources(sb, dev); 548 if (allocated) ISA_DELETE_RESOURCE(device_get_parent(dev), dev, 549 SYS_RES_IOPORT, i); 550 return error; 551} 552 553static int 554sb_doattach(device_t dev, struct sb_info *sb) 555{ 556 snddev_info *d = device_get_softc(dev); 557 void *ih; 558 int error; 559 char status[SND_STATUSLEN]; 560 561 sb->irq_rid = 0; 562 sb->drq1_rid = 0; 563 sb->drq2_rid = 1; 564 if (sb_alloc_resources(sb, dev)) goto no; 565 error = sb_reset_dsp(sb); 566 if (error) goto no; 567 error = sb_identify_board(dev, sb); 568 if (error) goto no; 569 570 sb_init(dev, sb); 571 mixer_init(d, &sb_mixer, sb); 572 bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, sb_intr, sb, &ih); 573 574 if (sb->bd_flags & BD_F_SB16) 575 pcm_setflags(dev, pcm_getflags(dev) | SD_F_EVILSB16); 576 if (sb->dma16 == sb->dma8) 577 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 578 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 579 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 580 /*highaddr*/BUS_SPACE_MAXADDR, 581 /*filter*/NULL, /*filterarg*/NULL, 582 /*maxsize*/DSP_BUFFSIZE, /*nsegments*/1, 583 /*maxsegz*/0x3ffff, 584 /*flags*/0, &sb->parent_dmat) != 0) { 585 device_printf(dev, "unable to create dma tag\n"); 586 goto no; 587 } 588 589 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %d", 590 rman_get_start(sb->io_base), rman_get_start(sb->irq), 591 sb->dma8); 592 if (sb->dma16 != sb->dma8) snprintf(status + strlen(status), 593 SND_STATUSLEN - strlen(status), ":%d", sb->dma16); 594 595 if (pcm_register(dev, sb, 1, 1)) goto no; 596 pcm_addchan(dev, PCMDIR_REC, &sb_chantemplate, sb); 597 pcm_addchan(dev, PCMDIR_PLAY, &sb_chantemplate, sb); 598 pcm_setstatus(dev, status); 599 600 return 0; 601 602no: 603 sb_release_resources(sb, dev); 604 return ENXIO; 605} 606 607static int 608sb_attach(device_t dev) 609{ 610 struct sb_info *sb; 611 int flags = isa_get_flags(dev); 612 613 if (flags & DV_F_DUAL_DMA) { 614 ISA_SET_RESOURCE(device_get_parent(dev), dev, SYS_RES_DRQ, 1, 615 flags & DV_F_DRQ_MASK, 1); 616 } 617 sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT); 618 if (!sb) return ENXIO; 619 bzero(sb, sizeof *sb); 620 621 /* XXX in probe should set io resource to right val instead of this */ 622 sb->io_rid = 0; 623 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &sb->io_rid, 624 0, ~0, 16, RF_ACTIVE); 625 if (!sb->io_base) { 626 BVDDB(printf("sb_probe: no addr, trying (0x220, 0x240)\n")); 627 sb->io_rid = 0; 628 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, 629 &sb->io_rid, 0x220, 0x22f, 630 16, RF_ACTIVE); 631 if (!sb->io_base) { 632 sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, 633 &sb->io_rid, 0x240, 634 0x24f, 16, RF_ACTIVE); 635 } 636 } 637 if (!sb->io_base) return ENXIO; 638 639 return sb_doattach(dev, sb); 640} 641 642static device_method_t sb_methods[] = { 643 /* Device interface */ 644 DEVMETHOD(device_probe, sb_probe), 645 DEVMETHOD(device_attach, sb_attach), 646 647 { 0, 0 } 648}; 649 650static driver_t sb_driver = { 651 "pcm", 652 sb_methods, 653 sizeof(snddev_info), 654}; 655 656DRIVER_MODULE(sb, isa, sb_driver, pcm_devclass, 0, 0); 657 658static void 659sb_intr(void *arg) 660{ 661 struct sb_info *sb = (struct sb_info *)arg; 662 int reason = 3, c; 663 664 /* 665 * SB < 4.0 is half duplex and has only 1 bit for int source, 666 * so we fake it. SB 4.x (SB16) has the int source in a separate 667 * register. 668 * The Vibra16X has separate flags for 8 and 16 bit transfers, but 669 * I have no idea how to tell capture from playback interrupts... 670 */ 671 if (sb->bd_flags & BD_F_SB16) { 672 c = sb_getmixer(sb, IRQ_STAT); 673 /* this tells us if the source is 8-bit or 16-bit dma. We 674 * have to check the io channel to map it to read or write... 675 */ 676 reason = 0; 677 if (c & 1) { /* 8-bit dma */ 678 if (sb->pch.fmt & AFMT_U8) reason |= 1; 679 if (sb->rch.fmt & AFMT_U8) reason |= 2; 680 } 681 if (c & 2) { /* 16-bit dma */ 682 if (sb->pch.fmt & AFMT_S16_LE) reason |= 1; 683 if (sb->rch.fmt & AFMT_S16_LE) reason |= 2; 684 } 685 } else c = 1; 686#if 0 687 printf("sb_intr: reason=%d c=0x%x\n", reason, c); 688#endif 689 if ((reason & 1) && (sb->pch.buffer->dl > 0)) 690 chn_intr(sb->pch.channel); 691 if ((reason & 2) && (sb->rch.buffer->dl > 0)) 692 chn_intr(sb->rch.channel); 693 if (c & 1) sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */ 694 if (c & 2) sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */ 695} 696 697static int 698sb_format(struct sb_chinfo *ch, u_int32_t format) 699{ 700 struct sb_info *sb = ch->parent; 701 ch->fmt = format; 702 if (sb->bd_flags & BD_F_ESS) { 703 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0; 704 int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0; 705 int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0; 706 707 /* autoinit DMA mode */ 708 ess_write(sb, 0xb8, 0x04 | play? 0x00 : 0x0a); 709 /* mono/stereo */ 710 ess_write(sb, 0xa8, 711 (ess_read(sb, 0xa8) & ~0x03) | stereo? 0x01 : 0x02); 712 /* demand mode, 4 bytes/xfer */ 713 ess_write(sb, 0xb9, 2); 714 /* setup dac/adc */ 715 if (play) ess_write(sb, 0xb6, b16? 0x00 : 0x80); 716 ess_write(sb, 0xb7, 0x51 | (b16? 0x20 : 0x00)); 717 ess_write(sb, 0xb7, 718 0x98 + (b16? 0x24 : 0x00) + (stereo? 0x00 : 0x38)); 719 /* irq/drq control */ 720 ess_write(sb, 0xb1, ess_read(sb, 0xb1) | 0x50); 721 ess_write(sb, 0xb2, ess_read(sb, 0xb1) | 0x50); 722 } 723 return 0; 724} 725 726static int 727sb_speed(struct sb_chinfo *ch, int speed) 728{ 729 struct sb_info *sb = ch->parent; 730 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0; 731 int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0; 732 733 if (sb->bd_flags & BD_F_SB16) { 734 RANGE(speed, 5000, 45000); 735 sb_cmd(sb, 0x42 - play); 736 sb_cmd(sb, speed >> 8); 737 sb_cmd(sb, speed & 0xff); 738 } else if (sb->bd_flags & BD_F_ESS) { 739 int t; 740 RANGE(speed, 5000, 49000); 741 if (speed > 22000) { 742 t = (795500 + speed / 2) / speed; 743 speed = (795500 + t / 2) / t; 744 t = (256 - t) | 0x80; 745 } else { 746 t = (397700 + speed / 2) / speed; 747 speed = (397700 + t / 2) / t; 748 t = 128 - t; 749 } 750 ess_write(sb, 0xa1, t); /* set time constant */ 751 t = 256 - 7160000 / (((speed * 9) / 20) * 82); 752 ess_write(sb, 0xa2, t); 753 } else { 754 u_char tconst; 755 int max_speed = 45000, tmp; 756 u_long flags; 757 758 /* here enforce speed limitations - max 22050 on sb 1.x*/ 759 if (sb->bd_id <= 0x200) max_speed = 22050; 760 761 /* 762 * SB models earlier than SB Pro have low limit for the 763 * input rate. Note that this is only for input, but since 764 * we do not support separate values for rec & play.... 765 */ 766 if (!play) { 767 if (sb->bd_id <= 0x200) max_speed = 13000; 768 else if (sb->bd_id < 0x300) max_speed = 15000; 769 } 770 RANGE(speed, 4000, max_speed); 771 if (stereo) speed <<= 1; 772 773 /* 774 * Now the speed should be valid. Compute the value to be 775 * programmed into the board. 776 */ 777 if (speed > 22050) { /* High speed mode on 2.01/3.xx */ 778 tconst = (u_char) 779 ((65536 - ((256000000 + speed / 2) / speed)) 780 >> 8); 781 sb->bd_flags |= BD_F_HISPEED; 782 tmp = 65536 - (tconst << 8); 783 speed = (256000000 + tmp / 2) / tmp; 784 } else { 785 sb->bd_flags &= ~BD_F_HISPEED; 786 tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff; 787 tmp = 256 - tconst; 788 speed = (1000000 + tmp / 2) / tmp; 789 } 790 flags = spltty(); 791 sb_cmd1(sb, 0x40, tconst); /* set time constant */ 792 splx(flags); 793 if (stereo) speed >>= 1; 794 } 795 return speed; 796} 797 798static int 799sb_start(struct sb_chinfo *ch) 800{ 801 struct sb_info *sb = ch->parent; 802 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0; 803 int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0; 804 int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0; 805 int l = ch->buffer->dl; 806 u_char i1, i2 = 0; 807 808 if (b16) l >>= 1; 809 l--; 810 if (play) sb_cmd(sb, DSP_CMD_SPKON); 811 if (sb->bd_flags & BD_F_SB16) { 812 i1 = DSP_F16_AUTO | DSP_F16_FIFO_ON | 813 (play? DSP_F16_DAC : DSP_F16_ADC); 814 i1 |= (b16 && (sb->bd_flags & BD_F_DUPLEX))? DSP_DMA16 : DSP_DMA8; 815 i2 = (stereo? DSP_F16_STEREO : 0) | (b16? DSP_F16_SIGNED : 0); 816 sb_cmd(sb, i1); 817 sb_cmd2(sb, i2, l); 818 } else if (sb->bd_flags & BD_F_ESS) { 819 ess_write(sb, 0xa4, l); 820 ess_write(sb, 0xa5, l >> 8); 821 ess_write(sb, 0xb8, ess_read(sb, 0xb8) | (play? 0x05 : 0x0f)); 822 } else { 823 if (sb->bd_flags & BD_F_HISPEED) i1 = play? 0x90 : 0x98; 824 else i1 = play? 0x1c : 0x2c; 825 sb_setmixer(sb, 0x0e, stereo? 2 : 0); 826 /* an ESS extension -- they can do 16 bits */ 827 if (b16) i1 |= 1; 828 sb_cmd2(sb, 0x48, l); 829 sb_cmd(sb, i1); 830 } 831 sb->bd_flags |= BD_F_DMARUN << b16; 832 return 0; 833} 834 835static int 836sb_stop(struct sb_chinfo *ch) 837{ 838 struct sb_info *sb = ch->parent; 839 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0; 840 int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0; 841 842 if (sb->bd_flags & BD_F_HISPEED) sb_reset_dsp(sb); 843 else { 844 sb_cmd(sb, b16? DSP_CMD_DMAPAUSE_16 : DSP_CMD_DMAPAUSE_8); 845 /* 846 * The above seems to have the undocumented side effect of 847 * blocking the other side as well. If the other 848 * channel was active (SB16) I have to re-enable it :( 849 */ 850 if (sb->bd_flags & (BD_F_DMARUN << (1 - b16))) 851 sb_cmd(sb, b16? 0xd4 : 0xd6 ); 852 } 853 if (play) sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */ 854 sb->bd_flags &= ~(BD_F_DMARUN << b16); 855 return 0; 856} 857 858/* channel interface */ 859static void * 860sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 861{ 862 struct sb_info *sb = devinfo; 863 struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch; 864 865 ch->parent = sb; 866 ch->channel = c; 867 ch->buffer = b; 868 ch->buffer->bufsize = DSP_BUFFSIZE; 869 if (chn_allocbuf(ch->buffer, sb->parent_dmat) == -1) return NULL; 870 ch->buffer->chan = (dir == PCMDIR_PLAY)? sb->dma16 : sb->dma8; 871 return ch; 872} 873 874static int 875sbchan_setdir(void *data, int dir) 876{ 877 struct sb_chinfo *ch = data; 878 ch->dir = dir; 879 return 0; 880} 881 882static int 883sbchan_setformat(void *data, u_int32_t format) 884{ 885 struct sb_chinfo *ch = data; 886 sb_format(ch, format); 887 return 0; 888} 889 890static int 891sbchan_setspeed(void *data, u_int32_t speed) 892{ 893 struct sb_chinfo *ch = data; 894 return sb_speed(ch, speed); 895} 896 897static int 898sbchan_setblocksize(void *data, u_int32_t blocksize) 899{ 900 return blocksize; 901} 902 903static int 904sbchan_trigger(void *data, int go) 905{ 906 struct sb_chinfo *ch = data; 907 buf_isadma(ch->buffer, go); 908 if (go == PCMTRIG_START) sb_start(ch); else sb_stop(ch); 909 return 0; 910} 911 912static int 913sbchan_getptr(void *data) 914{ 915 struct sb_chinfo *ch = data; 916 return buf_isadmaptr(ch->buffer); 917} 918 919static pcmchan_caps * 920sbchan_getcaps(void *data) 921{ 922 struct sb_chinfo *ch = data; 923 int p = (ch->dir == PCMDIR_PLAY)? 1 : 0; 924 if (ch->parent->bd_flags & BD_F_ESS) 925 return p? &ess_playcaps : &ess_reccaps; 926 else if (ch->parent->bd_id <= 0x200) 927 return p? &sb_playcaps : &sb_reccaps; 928 else if (ch->parent->bd_id >= 0x400) 929 return p? &sb16_playcaps : &sb16_reccaps; 930 else 931 return p? &sbpro_playcaps : &sbpro_reccaps; 932} 933 934/************************************************************/ 935 936static int 937sbmix_init(snd_mixer *m) 938{ 939 struct sb_info *sb = mix_getdevinfo(m); 940 941 switch (sb->bd_flags & BD_F_MIX_MASK) { 942 case BD_F_MIX_CT1345: /* SB 3.0 has 1345 mixer */ 943 mix_setdevs(m, SBPRO_MIXER_DEVICES); 944 mix_setrecdevs(m, SBPRO_RECORDING_DEVICES); 945 sb_setmixer(sb, 0, 1); /* reset mixer */ 946 sb_setmixer(sb, MIC_VOL, 0x6); /* mic volume max */ 947 sb_setmixer(sb, RECORD_SRC, 0x0); /* mic source */ 948 sb_setmixer(sb, FM_VOL, 0x0); /* no midi */ 949 break; 950 951 case BD_F_MIX_CT1745: /* SB16 mixer ... */ 952 mix_setdevs(m, SB16_MIXER_DEVICES); 953 mix_setrecdevs(m, SB16_RECORDING_DEVICES); 954 sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */ 955 sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */ 956 sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */ 957 } 958 return 0; 959} 960 961static int 962sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right) 963{ 964 struct sb_info *sb = mix_getdevinfo(m); 965 int regoffs; 966 u_char val; 967 mixer_tab *iomap; 968 969 switch (sb->bd_flags & BD_F_MIX_MASK) { 970 case BD_F_MIX_CT1345: 971 iomap = &sbpro_mix; 972 break; 973 974 case BD_F_MIX_CT1745: 975 iomap = &sb16_mix; 976 break; 977 978 default: 979 return -1; 980 /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */ 981 } 982 regoffs = (*iomap)[dev][LEFT_CHN].regno; 983 if (regoffs == 0) return -1; 984 val = sb_getmixer(sb, regoffs); 985 change_bits(iomap, &val, dev, LEFT_CHN, left); 986 sb_setmixer(sb, regoffs, val); 987 if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) { /* Change register */ 988 regoffs = (*iomap)[dev][RIGHT_CHN].regno; 989 if (regoffs != 0) { 990 val = sb_getmixer(sb, regoffs); /* Read the new one */ 991 change_bits(iomap, &val, dev, RIGHT_CHN, right); 992 sb_setmixer(sb, regoffs, val); 993 } else right = left; 994 } else right = left; 995 return left | (right << 8); 996} 997 998static int 999sbmix_setrecsrc(snd_mixer *m, u_int32_t src) 1000{ 1001 struct sb_info *sb = mix_getdevinfo(m); 1002 u_char recdev; 1003 1004 switch (sb->bd_flags & BD_F_MIX_MASK) { 1005 case BD_F_MIX_CT1345: 1006 if (src == SOUND_MASK_LINE) recdev = 0x06; 1007 else if (src == SOUND_MASK_CD) recdev = 0x02; 1008 else { /* default: mic */ 1009 src = SOUND_MASK_MIC; 1010 recdev = 0; 1011 } 1012 sb_setmixer(sb, RECORD_SRC, recdev | 1013 (sb_getmixer(sb, RECORD_SRC) & ~0x07)); 1014 break; 1015 1016 case BD_F_MIX_CT1745: /* sb16 */ 1017 recdev = 0; 1018 if (src & SOUND_MASK_MIC) recdev |= 0x01; /* mono mic */ 1019 if (src & SOUND_MASK_CD) recdev |= 0x06; /* l+r cd */ 1020 if (src & SOUND_MASK_LINE) recdev |= 0x18; /* l+r line */ 1021 if (src & SOUND_MASK_SYNTH) recdev |= 0x60; /* l+r midi */ 1022 sb_setmixer(sb, SB16_IMASK_L, recdev); 1023 sb_setmixer(sb, SB16_IMASK_R, recdev); 1024 /* 1025 * since the same volume controls apply to the input and 1026 * output sections, the best approach to have a consistent 1027 * behaviour among cards would be to disable the output path 1028 * on devices which are used to record. 1029 * However, since users like to have feedback, we only disable 1030 * the mic -- permanently. 1031 */ 1032 sb_setmixer(sb, SB16_OMASK, 0x1f & ~1); 1033 break; 1034 } 1035 return src; 1036} 1037 1038#if NPNP > 0 1039static int 1040sbpnp_probe(device_t dev) 1041{ 1042 char *s = NULL; 1043 u_int32_t logical_id = isa_get_logicalid(dev); 1044 1045 switch(logical_id) { 1046 case 0x43008c0e: /* CTL0043 */ 1047 case 0x01008c0e: /* CTL0001 */ 1048 s = "Vibra16X"; 1049 break; 1050 1051 case 0x31008c0e: /* CTL0031 */ 1052 case 0x41008c0e: /* CTL0041 */ 1053 case 0x42008c0e: /* CTL0042 */ 1054 case 0x45008c0e: /* CTL0045 */ 1055 s = "SB16 PnP"; 1056 break; 1057 1058 case 0x01100000: /* @@@1001 */ 1059 s = "Avance Asound 110"; 1060 break; 1061 1062 case 0x01200000: /* @@@2001 */ 1063 s = "Avance Logic ALS120"; 1064 break; 1065 1066 case 0x68187316: /* ESS1868 */ 1067 s = "ESS1868"; 1068 break; 1069 } 1070 if (s) { 1071 device_set_desc(dev, s); 1072 return 0; 1073 } 1074 return ENXIO; 1075} 1076 1077static int 1078sbpnp_attach(device_t dev) 1079{ 1080 struct sb_info *sb; 1081 u_int32_t vend_id = isa_get_vendorid(dev); 1082 1083 sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT); 1084 if (!sb) return ENXIO; 1085 bzero(sb, sizeof *sb); 1086 1087 switch(vend_id) { 1088 case 0xf0008c0e: 1089 case 0x10019305: 1090 case 0x20019305: 1091 /* XXX add here the vend_id for other vibra16X cards... */ 1092 sb->bd_flags = BD_F_SB16X; 1093 } 1094 return sb_doattach(dev, sb); 1095} 1096 1097static device_method_t sbpnp_methods[] = { 1098 /* Device interface */ 1099 DEVMETHOD(device_probe, sbpnp_probe), 1100 DEVMETHOD(device_attach, sbpnp_attach), 1101 1102 { 0, 0 } 1103}; 1104 1105static driver_t sbpnp_driver = { 1106 "pcm", 1107 sbpnp_methods, 1108 sizeof(snddev_info), 1109}; 1110 1111DRIVER_MODULE(sbpnp, isa, sbpnp_driver, pcm_devclass, 0, 0); 1112 1113#endif /* NPNP > 0 */ 1114 1115#endif /* NPCM > 0 */ 1116 1117 1118