sb16.c revision 152418
1/*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 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 32#include <dev/sound/pcm/sound.h> 33 34#include <dev/sound/isa/sb.h> 35#include <dev/sound/chip.h> 36 37#include <isa/isavar.h> 38 39#include "mixer_if.h" 40 41SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/isa/sb16.c 152418 2005-11-14 18:16:59Z ariff $"); 42 43#define SB16_BUFFSIZE 4096 44#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16) 45 46static u_int32_t sb16_fmt8[] = { 47 AFMT_U8, 48 AFMT_STEREO | AFMT_U8, 49 0 50}; 51static struct pcmchan_caps sb16_caps8 = {5000, 45000, sb16_fmt8, 0}; 52 53static u_int32_t sb16_fmt16[] = { 54 AFMT_S16_LE, 55 AFMT_STEREO | AFMT_S16_LE, 56 0 57}; 58static struct pcmchan_caps sb16_caps16 = {5000, 45000, sb16_fmt16, 0}; 59 60static u_int32_t sb16x_fmt[] = { 61 AFMT_U8, 62 AFMT_STEREO | AFMT_U8, 63 AFMT_S16_LE, 64 AFMT_STEREO | AFMT_S16_LE, 65 0 66}; 67static struct pcmchan_caps sb16x_caps = {5000, 49000, sb16x_fmt, 0}; 68 69struct sb_info; 70 71struct sb_chinfo { 72 struct sb_info *parent; 73 struct pcm_channel *channel; 74 struct snd_dbuf *buffer; 75 int dir, run, dch; 76 u_int32_t fmt, spd, blksz; 77}; 78 79struct sb_info { 80 struct resource *io_base; /* I/O address for the board */ 81 struct resource *irq; 82 struct resource *drq1; 83 struct resource *drq2; 84 void *ih; 85 bus_dma_tag_t parent_dmat; 86 87 unsigned int bufsize; 88 int bd_id; 89 u_long bd_flags; /* board-specific flags */ 90 int prio, prio16; 91 struct sb_chinfo pch, rch; 92 device_t parent_dev; 93}; 94 95#if 0 96static void sb_lock(struct sb_info *sb); 97static void sb_unlock(struct sb_info *sb); 98static int sb_rd(struct sb_info *sb, int reg); 99static void sb_wr(struct sb_info *sb, int reg, u_int8_t val); 100static int sb_cmd(struct sb_info *sb, u_char val); 101/* static int sb_cmd1(struct sb_info *sb, u_char cmd, int val); */ 102static int sb_cmd2(struct sb_info *sb, u_char cmd, int val); 103static u_int sb_get_byte(struct sb_info *sb); 104static void sb_setmixer(struct sb_info *sb, u_int port, u_int value); 105static int sb_getmixer(struct sb_info *sb, u_int port); 106static int sb_reset_dsp(struct sb_info *sb); 107 108static void sb_intr(void *arg); 109#endif 110 111/* 112 * Common code for the midi and pcm functions 113 * 114 * sb_cmd write a single byte to the CMD port. 115 * sb_cmd1 write a CMD + 1 byte arg 116 * sb_cmd2 write a CMD + 2 byte arg 117 * sb_get_byte returns a single byte from the DSP data port 118 */ 119 120static void 121sb_lock(struct sb_info *sb) { 122 123 sbc_lock(device_get_softc(sb->parent_dev)); 124} 125 126static void 127sb_lockassert(struct sb_info *sb) { 128 129 sbc_lockassert(device_get_softc(sb->parent_dev)); 130} 131 132static void 133sb_unlock(struct sb_info *sb) { 134 135 sbc_unlock(device_get_softc(sb->parent_dev)); 136} 137 138static int 139port_rd(struct resource *port, int off) 140{ 141 return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off); 142} 143 144static void 145port_wr(struct resource *port, int off, u_int8_t data) 146{ 147 bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data); 148} 149 150static int 151sb_rd(struct sb_info *sb, int reg) 152{ 153 return port_rd(sb->io_base, reg); 154} 155 156static void 157sb_wr(struct sb_info *sb, int reg, u_int8_t val) 158{ 159 port_wr(sb->io_base, reg, val); 160} 161 162static int 163sb_dspwr(struct sb_info *sb, u_char val) 164{ 165 int i; 166 167 for (i = 0; i < 1000; i++) { 168 if ((sb_rd(sb, SBDSP_STATUS) & 0x80)) 169 DELAY((i > 100)? 1000 : 10); 170 else { 171 sb_wr(sb, SBDSP_CMD, val); 172 return 1; 173 } 174 } 175#if __FreeBSD_version > 500000 176 if (curthread->td_intr_nesting_level == 0) 177 printf("sb_dspwr(0x%02x) timed out.\n", val); 178#endif 179 return 0; 180} 181 182static int 183sb_cmd(struct sb_info *sb, u_char val) 184{ 185#if 0 186 printf("sb_cmd: %x\n", val); 187#endif 188 return sb_dspwr(sb, val); 189} 190 191/* 192static int 193sb_cmd1(struct sb_info *sb, u_char cmd, int val) 194{ 195#if 0 196 printf("sb_cmd1: %x, %x\n", cmd, val); 197#endif 198 if (sb_dspwr(sb, cmd)) { 199 return sb_dspwr(sb, val & 0xff); 200 } else return 0; 201} 202*/ 203 204static int 205sb_cmd2(struct sb_info *sb, u_char cmd, int val) 206{ 207 int r; 208 209#if 0 210 printf("sb_cmd2: %x, %x\n", cmd, val); 211#endif 212 sb_lockassert(sb); 213 r = 0; 214 if (sb_dspwr(sb, cmd)) { 215 if (sb_dspwr(sb, val & 0xff)) { 216 if (sb_dspwr(sb, (val >> 8) & 0xff)) { 217 r = 1; 218 } 219 } 220 } 221 222 return r; 223} 224 225/* 226 * in the SB, there is a set of indirect "mixer" registers with 227 * address at offset 4, data at offset 5 228 */ 229static void 230sb_setmixer(struct sb_info *sb, u_int port, u_int value) 231{ 232 sb_lock(sb); 233 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 234 DELAY(10); 235 sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff)); 236 DELAY(10); 237 sb_unlock(sb); 238} 239 240static int 241sb_getmixer(struct sb_info *sb, u_int port) 242{ 243 int val; 244 245 sb_lockassert(sb); 246 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 247 DELAY(10); 248 val = sb_rd(sb, SB_MIX_DATA); 249 DELAY(10); 250 251 return val; 252} 253 254static u_int 255sb_get_byte(struct sb_info *sb) 256{ 257 int i; 258 259 for (i = 1000; i > 0; i--) { 260 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80) 261 return sb_rd(sb, DSP_READ); 262 else 263 DELAY(20); 264 } 265 return 0xffff; 266} 267 268static int 269sb_reset_dsp(struct sb_info *sb) 270{ 271 u_char b; 272 273 sb_lockassert(sb); 274 sb_wr(sb, SBDSP_RST, 3); 275 DELAY(100); 276 sb_wr(sb, SBDSP_RST, 0); 277 b = sb_get_byte(sb); 278 if (b != 0xAA) { 279 DEB(printf("sb_reset_dsp 0x%lx failed\n", 280 rman_get_start(sb->io_base))); 281 return ENXIO; /* Sorry */ 282 } 283 return 0; 284} 285 286/************************************************************/ 287 288struct sb16_mixent { 289 int reg; 290 int bits; 291 int ofs; 292 int stereo; 293}; 294 295static const struct sb16_mixent sb16_mixtab[32] = { 296 [SOUND_MIXER_VOLUME] = { 0x30, 5, 3, 1 }, 297 [SOUND_MIXER_PCM] = { 0x32, 5, 3, 1 }, 298 [SOUND_MIXER_SYNTH] = { 0x34, 5, 3, 1 }, 299 [SOUND_MIXER_CD] = { 0x36, 5, 3, 1 }, 300 [SOUND_MIXER_LINE] = { 0x38, 5, 3, 1 }, 301 [SOUND_MIXER_MIC] = { 0x3a, 5, 3, 0 }, 302 [SOUND_MIXER_SPEAKER] = { 0x3b, 5, 3, 0 }, 303 [SOUND_MIXER_IGAIN] = { 0x3f, 2, 6, 1 }, 304 [SOUND_MIXER_OGAIN] = { 0x41, 2, 6, 1 }, 305 [SOUND_MIXER_TREBLE] = { 0x44, 4, 4, 1 }, 306 [SOUND_MIXER_BASS] = { 0x46, 4, 4, 1 }, 307 [SOUND_MIXER_LINE1] = { 0x52, 5, 3, 1 } 308}; 309 310static int 311sb16mix_init(struct snd_mixer *m) 312{ 313 struct sb_info *sb = mix_getdevinfo(m); 314 315 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER | 316 SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD | 317 SOUND_MASK_IGAIN | SOUND_MASK_OGAIN | SOUND_MASK_LINE1 | 318 SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE); 319 320 mix_setrecdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_LINE | 321 SOUND_MASK_LINE1 | SOUND_MASK_MIC | SOUND_MASK_CD); 322 323 sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */ 324 325 sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */ 326 sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */ 327 328 return 0; 329} 330 331static int 332rel2abs_volume(int x, int max) 333{ 334 int temp; 335 336 temp = ((x * max) + 50) / 100; 337 if (temp > max) 338 temp = max; 339 else if (temp < 0) 340 temp = 0; 341 return (temp); 342} 343 344static int 345sb16mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 346{ 347 struct sb_info *sb = mix_getdevinfo(m); 348 const struct sb16_mixent *e; 349 int max; 350 351 e = &sb16_mixtab[dev]; 352 max = (1 << e->bits) - 1; 353 354 left = rel2abs_volume(left, max); 355 right = rel2abs_volume(right, max); 356 357 sb_setmixer(sb, e->reg, left << e->ofs); 358 if (e->stereo) 359 sb_setmixer(sb, e->reg + 1, right << e->ofs); 360 else 361 right = left; 362 363 left = (left * 100) / max; 364 right = (right * 100) / max; 365 366 return left | (right << 8); 367} 368 369static int 370sb16mix_setrecsrc(struct snd_mixer *m, u_int32_t src) 371{ 372 struct sb_info *sb = mix_getdevinfo(m); 373 u_char recdev_l, recdev_r; 374 375 recdev_l = 0; 376 recdev_r = 0; 377 if (src & SOUND_MASK_MIC) { 378 recdev_l |= 0x01; /* mono mic */ 379 recdev_r |= 0x01; 380 } 381 382 if (src & SOUND_MASK_CD) { 383 recdev_l |= 0x04; /* l cd */ 384 recdev_r |= 0x02; /* r cd */ 385 } 386 387 if (src & SOUND_MASK_LINE) { 388 recdev_l |= 0x10; /* l line */ 389 recdev_r |= 0x08; /* r line */ 390 } 391 392 if (src & SOUND_MASK_SYNTH) { 393 recdev_l |= 0x40; /* l midi */ 394 recdev_r |= 0x20; /* r midi */ 395 } 396 397 sb_setmixer(sb, SB16_IMASK_L, recdev_l); 398 sb_setmixer(sb, SB16_IMASK_R, recdev_r); 399 400 /* Switch on/off FM tuner source */ 401 if (src & SOUND_MASK_LINE1) 402 sb_setmixer(sb, 0x4a, 0x0c); 403 else 404 sb_setmixer(sb, 0x4a, 0x00); 405 406 /* 407 * since the same volume controls apply to the input and 408 * output sections, the best approach to have a consistent 409 * behaviour among cards would be to disable the output path 410 * on devices which are used to record. 411 * However, since users like to have feedback, we only disable 412 * the mic -- permanently. 413 */ 414 sb_setmixer(sb, SB16_OMASK, 0x1f & ~1); 415 416 return src; 417} 418 419static kobj_method_t sb16mix_mixer_methods[] = { 420 KOBJMETHOD(mixer_init, sb16mix_init), 421 KOBJMETHOD(mixer_set, sb16mix_set), 422 KOBJMETHOD(mixer_setrecsrc, sb16mix_setrecsrc), 423 { 0, 0 } 424}; 425MIXER_DECLARE(sb16mix_mixer); 426 427/************************************************************/ 428 429static void 430sb16_release_resources(struct sb_info *sb, device_t dev) 431{ 432 if (sb->irq) { 433 if (sb->ih) 434 bus_teardown_intr(dev, sb->irq, sb->ih); 435 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq); 436 sb->irq = 0; 437 } 438 if (sb->drq2) { 439 if (sb->drq2 != sb->drq1) { 440 isa_dma_release(rman_get_start(sb->drq2)); 441 bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2); 442 } 443 sb->drq2 = 0; 444 } 445 if (sb->drq1) { 446 isa_dma_release(rman_get_start(sb->drq1)); 447 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1); 448 sb->drq1 = 0; 449 } 450 if (sb->io_base) { 451 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base); 452 sb->io_base = 0; 453 } 454 if (sb->parent_dmat) { 455 bus_dma_tag_destroy(sb->parent_dmat); 456 sb->parent_dmat = 0; 457 } 458 free(sb, M_DEVBUF); 459} 460 461static int 462sb16_alloc_resources(struct sb_info *sb, device_t dev) 463{ 464 int rid; 465 466 rid = 0; 467 if (!sb->io_base) 468 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 469 &rid, RF_ACTIVE); 470 471 rid = 0; 472 if (!sb->irq) 473 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 474 RF_ACTIVE); 475 476 rid = 0; 477 if (!sb->drq1) 478 sb->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid, 479 RF_ACTIVE); 480 481 rid = 1; 482 if (!sb->drq2) 483 sb->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid, 484 RF_ACTIVE); 485 486 if (sb->io_base && sb->drq1 && sb->irq) { 487 isa_dma_acquire(rman_get_start(sb->drq1)); 488 isa_dmainit(rman_get_start(sb->drq1), sb->bufsize); 489 490 if (sb->drq2) { 491 isa_dma_acquire(rman_get_start(sb->drq2)); 492 isa_dmainit(rman_get_start(sb->drq2), sb->bufsize); 493 } else { 494 sb->drq2 = sb->drq1; 495 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 496 } 497 return 0; 498 } else return ENXIO; 499} 500 501/* sbc does locking for us */ 502static void 503sb_intr(void *arg) 504{ 505 struct sb_info *sb = (struct sb_info *)arg; 506 int reason, c; 507 508 /* 509 * The Vibra16X has separate flags for 8 and 16 bit transfers, but 510 * I have no idea how to tell capture from playback interrupts... 511 */ 512 513 reason = 0; 514 sb_lock(sb); 515 c = sb_getmixer(sb, IRQ_STAT); 516 if (c & 1) 517 sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */ 518 519 if (c & 2) 520 sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */ 521 sb_unlock(sb); 522 523 /* 524 * this tells us if the source is 8-bit or 16-bit dma. We 525 * have to check the io channel to map it to read or write... 526 */ 527 528 if (sb->bd_flags & BD_F_SB16X) { 529 if (c & 1) { /* 8-bit format */ 530 if (sb->pch.fmt & AFMT_8BIT) 531 reason |= 1; 532 if (sb->rch.fmt & AFMT_8BIT) 533 reason |= 2; 534 } 535 if (c & 2) { /* 16-bit format */ 536 if (sb->pch.fmt & AFMT_16BIT) 537 reason |= 1; 538 if (sb->rch.fmt & AFMT_16BIT) 539 reason |= 2; 540 } 541 } else { 542 if (c & 1) { /* 8-bit dma */ 543 if (sb->pch.dch == 1) 544 reason |= 1; 545 if (sb->rch.dch == 1) 546 reason |= 2; 547 } 548 if (c & 2) { /* 16-bit dma */ 549 if (sb->pch.dch == 2) 550 reason |= 1; 551 if (sb->rch.dch == 2) 552 reason |= 2; 553 } 554 } 555#if 0 556 printf("sb_intr: reason=%d c=0x%x\n", reason, c); 557#endif 558 if ((reason & 1) && (sb->pch.run)) 559 chn_intr(sb->pch.channel); 560 561 if ((reason & 2) && (sb->rch.run)) 562 chn_intr(sb->rch.channel); 563} 564 565static int 566sb_setup(struct sb_info *sb) 567{ 568 struct sb_chinfo *ch; 569 u_int8_t v; 570 int l, pprio; 571 572 sb_lock(sb); 573 if (sb->bd_flags & BD_F_DMARUN) 574 sndbuf_dma(sb->pch.buffer, PCMTRIG_STOP); 575 if (sb->bd_flags & BD_F_DMARUN2) 576 sndbuf_dma(sb->rch.buffer, PCMTRIG_STOP); 577 sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2); 578 579 sb_reset_dsp(sb); 580 581 if (sb->bd_flags & BD_F_SB16X) { 582 /* full-duplex doesn't work! */ 583 pprio = sb->pch.run? 1 : 0; 584 sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq1 : sb->drq2); 585 sb->pch.dch = pprio? 1 : 0; 586 sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq2 : sb->drq1); 587 sb->rch.dch = pprio? 2 : 1; 588 } else { 589 if (sb->pch.run && sb->rch.run) { 590 pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1; 591 sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq2 : sb->drq1); 592 sb->pch.dch = pprio? 2 : 1; 593 sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq1 : sb->drq2); 594 sb->rch.dch = pprio? 1 : 2; 595 } else { 596 if (sb->pch.run) { 597 sndbuf_dmasetup(sb->pch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1); 598 sb->pch.dch = (sb->pch.fmt & AFMT_16BIT)? 2 : 1; 599 sndbuf_dmasetup(sb->rch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2); 600 sb->rch.dch = (sb->pch.fmt & AFMT_16BIT)? 1 : 2; 601 } else if (sb->rch.run) { 602 sndbuf_dmasetup(sb->pch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2); 603 sb->pch.dch = (sb->rch.fmt & AFMT_16BIT)? 1 : 2; 604 sndbuf_dmasetup(sb->rch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1); 605 sb->rch.dch = (sb->rch.fmt & AFMT_16BIT)? 2 : 1; 606 } 607 } 608 } 609 610 sndbuf_dmasetdir(sb->pch.buffer, PCMDIR_PLAY); 611 sndbuf_dmasetdir(sb->rch.buffer, PCMDIR_REC); 612 613 /* 614 printf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n", 615 sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run); 616 */ 617 618 ch = &sb->pch; 619 if (ch->run) { 620 l = ch->blksz; 621 if (ch->fmt & AFMT_16BIT) 622 l >>= 1; 623 l--; 624 625 /* play speed */ 626 RANGE(ch->spd, 5000, 45000); 627 sb_cmd(sb, DSP_CMD_OUT16); 628 sb_cmd(sb, ch->spd >> 8); 629 sb_cmd(sb, ch->spd & 0xff); 630 631 /* play format, length */ 632 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC; 633 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8; 634 sb_cmd(sb, v); 635 636 v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0; 637 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0; 638 sb_cmd2(sb, v, l); 639 sndbuf_dma(ch->buffer, PCMTRIG_START); 640 sb->bd_flags |= BD_F_DMARUN; 641 } 642 643 ch = &sb->rch; 644 if (ch->run) { 645 l = ch->blksz; 646 if (ch->fmt & AFMT_16BIT) 647 l >>= 1; 648 l--; 649 650 /* record speed */ 651 RANGE(ch->spd, 5000, 45000); 652 sb_cmd(sb, DSP_CMD_IN16); 653 sb_cmd(sb, ch->spd >> 8); 654 sb_cmd(sb, ch->spd & 0xff); 655 656 /* record format, length */ 657 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC; 658 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8; 659 sb_cmd(sb, v); 660 661 v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0; 662 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0; 663 sb_cmd2(sb, v, l); 664 sndbuf_dma(ch->buffer, PCMTRIG_START); 665 sb->bd_flags |= BD_F_DMARUN2; 666 } 667 sb_unlock(sb); 668 669 return 0; 670} 671 672/* channel interface */ 673static void * 674sb16chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 675{ 676 struct sb_info *sb = devinfo; 677 struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch; 678 679 ch->parent = sb; 680 ch->channel = c; 681 ch->buffer = b; 682 ch->dir = dir; 683 684 if (sndbuf_alloc(ch->buffer, sb->parent_dmat, sb->bufsize) != 0) 685 return NULL; 686 687 return ch; 688} 689 690static int 691sb16chan_setformat(kobj_t obj, void *data, u_int32_t format) 692{ 693 struct sb_chinfo *ch = data; 694 struct sb_info *sb = ch->parent; 695 696 ch->fmt = format; 697 sb->prio = ch->dir; 698 sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0; 699 700 return 0; 701} 702 703static int 704sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed) 705{ 706 struct sb_chinfo *ch = data; 707 708 ch->spd = speed; 709 return speed; 710} 711 712static int 713sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 714{ 715 struct sb_chinfo *ch = data; 716 717 ch->blksz = blocksize; 718 return ch->blksz; 719} 720 721static int 722sb16chan_trigger(kobj_t obj, void *data, int go) 723{ 724 struct sb_chinfo *ch = data; 725 struct sb_info *sb = ch->parent; 726 727 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) 728 return 0; 729 730 if (go == PCMTRIG_START) 731 ch->run = 1; 732 else 733 ch->run = 0; 734 735 sb_setup(sb); 736 737 return 0; 738} 739 740static int 741sb16chan_getptr(kobj_t obj, void *data) 742{ 743 struct sb_chinfo *ch = data; 744 745 return sndbuf_dmaptr(ch->buffer); 746} 747 748static struct pcmchan_caps * 749sb16chan_getcaps(kobj_t obj, void *data) 750{ 751 struct sb_chinfo *ch = data; 752 struct sb_info *sb = ch->parent; 753 754 if ((sb->prio == 0) || (sb->prio == ch->dir)) 755 return &sb16x_caps; 756 else 757 return sb->prio16? &sb16_caps8 : &sb16_caps16; 758} 759 760static int 761sb16chan_resetdone(kobj_t obj, void *data) 762{ 763 struct sb_chinfo *ch = data; 764 struct sb_info *sb = ch->parent; 765 766 sb->prio = 0; 767 768 return 0; 769} 770 771static kobj_method_t sb16chan_methods[] = { 772 KOBJMETHOD(channel_init, sb16chan_init), 773 KOBJMETHOD(channel_resetdone, sb16chan_resetdone), 774 KOBJMETHOD(channel_setformat, sb16chan_setformat), 775 KOBJMETHOD(channel_setspeed, sb16chan_setspeed), 776 KOBJMETHOD(channel_setblocksize, sb16chan_setblocksize), 777 KOBJMETHOD(channel_trigger, sb16chan_trigger), 778 KOBJMETHOD(channel_getptr, sb16chan_getptr), 779 KOBJMETHOD(channel_getcaps, sb16chan_getcaps), 780 { 0, 0 } 781}; 782CHANNEL_DECLARE(sb16chan); 783 784/************************************************************/ 785 786static int 787sb16_probe(device_t dev) 788{ 789 char buf[64]; 790 uintptr_t func, ver, r, f; 791 792 /* The parent device has already been probed. */ 793 r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func); 794 if (func != SCF_PCM) 795 return (ENXIO); 796 797 r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver); 798 f = (ver & 0xffff0000) >> 16; 799 ver &= 0x0000ffff; 800 if (f & BD_F_SB16) { 801 snprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff, 802 (f & BD_F_SB16X)? " (ViBRA16X)" : ""); 803 device_set_desc_copy(dev, buf); 804 return 0; 805 } else 806 return (ENXIO); 807} 808 809static int 810sb16_attach(device_t dev) 811{ 812 struct sb_info *sb; 813 uintptr_t ver; 814 char status[SND_STATUSLEN], status2[SND_STATUSLEN]; 815 816 sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT | M_ZERO); 817 if (!sb) 818 return ENXIO; 819 820 sb->parent_dev = device_get_parent(dev); 821 BUS_READ_IVAR(sb->parent_dev, dev, 1, &ver); 822 sb->bd_id = ver & 0x0000ffff; 823 sb->bd_flags = (ver & 0xffff0000) >> 16; 824 sb->bufsize = pcm_getbuffersize(dev, 4096, SB16_BUFFSIZE, 65536); 825 826 if (sb16_alloc_resources(sb, dev)) 827 goto no; 828 sb_lock(sb); 829 if (sb_reset_dsp(sb)) { 830 sb_unlock(sb); 831 goto no; 832 } 833 sb_unlock(sb); 834 if (mixer_init(dev, &sb16mix_mixer_class, sb)) 835 goto no; 836 if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih)) 837 goto no; 838 839 if (sb->bd_flags & BD_F_SB16X) 840 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 841 842 sb->prio = 0; 843 844 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 845 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 846 /*highaddr*/BUS_SPACE_MAXADDR, 847 /*filter*/NULL, /*filterarg*/NULL, 848 /*maxsize*/sb->bufsize, /*nsegments*/1, 849 /*maxsegz*/0x3ffff, /*flags*/0, 850 /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, 851 &sb->parent_dmat) != 0) { 852 device_printf(dev, "unable to create dma tag\n"); 853 goto no; 854 } 855 856 if (!(pcm_getflags(dev) & SD_F_SIMPLEX)) 857 snprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(sb->drq2)); 858 else 859 status2[0] = '\0'; 860 861 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s", 862 rman_get_start(sb->io_base), rman_get_start(sb->irq), 863 rman_get_start(sb->drq1), status2, sb->bufsize, 864 PCM_KLDSTRING(snd_sb16)); 865 866 if (pcm_register(dev, sb, 1, 1)) 867 goto no; 868 pcm_addchan(dev, PCMDIR_REC, &sb16chan_class, sb); 869 pcm_addchan(dev, PCMDIR_PLAY, &sb16chan_class, sb); 870 871 pcm_setstatus(dev, status); 872 873 return 0; 874 875no: 876 sb16_release_resources(sb, dev); 877 return ENXIO; 878} 879 880static int 881sb16_detach(device_t dev) 882{ 883 int r; 884 struct sb_info *sb; 885 886 r = pcm_unregister(dev); 887 if (r) 888 return r; 889 890 sb = pcm_getdevinfo(dev); 891 sb16_release_resources(sb, dev); 892 return 0; 893} 894 895static device_method_t sb16_methods[] = { 896 /* Device interface */ 897 DEVMETHOD(device_probe, sb16_probe), 898 DEVMETHOD(device_attach, sb16_attach), 899 DEVMETHOD(device_detach, sb16_detach), 900 901 { 0, 0 } 902}; 903 904static driver_t sb16_driver = { 905 "pcm", 906 sb16_methods, 907 PCM_SOFTC_SIZE, 908}; 909 910DRIVER_MODULE(snd_sb16, sbc, sb16_driver, pcm_devclass, 0, 0); 911MODULE_DEPEND(snd_sb16, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 912MODULE_DEPEND(snd_sb16, snd_sbc, 1, 1, 1); 913MODULE_VERSION(snd_sb16, 1); 914