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