sb16.c revision 274035
1131722Sobrien/*- 2218822Sdim * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 3131722Sobrien * Copyright (c) 1997,1998 Luigi Rizzo 4131722Sobrien * 5131722Sobrien * Derived from files in the Voxware 3.5 distribution, 6131722Sobrien * Copyright by Hannu Savolainen 1994, under the same copyright 7131722Sobrien * conditions. 8131722Sobrien * All rights reserved. 9131722Sobrien * 10131722Sobrien * Redistribution and use in source and binary forms, with or without 11131722Sobrien * modification, are permitted provided that the following conditions 12131722Sobrien * are met: 13131722Sobrien * 1. Redistributions of source code must retain the above copyright 14131722Sobrien * notice, this list of conditions and the following disclaimer. 15131722Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16131722Sobrien * notice, this list of conditions and the following disclaimer in the 17131722Sobrien * documentation and/or other materials provided with the distribution. 18131722Sobrien * 19218822Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20131722Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21131722Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22131722Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23131722Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24131722Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25131722Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26131722Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27131722Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28131722Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29131722Sobrien * SUCH DAMAGE. 30131722Sobrien */ 31131722Sobrien 32131722Sobrien#ifdef HAVE_KERNEL_OPTION_HEADERS 33131722Sobrien#include "opt_snd.h" 34131722Sobrien#endif 35131722Sobrien 36131722Sobrien#include <dev/sound/pcm/sound.h> 37131722Sobrien 38131722Sobrien#include <dev/sound/isa/sb.h> 39131722Sobrien#include <dev/sound/chip.h> 40131722Sobrien 41131722Sobrien#include <isa/isavar.h> 42218822Sdim 43218822Sdim#include "mixer_if.h" 44218822Sdim 45218822SdimSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/isa/sb16.c 274035 2014-11-03 11:11:45Z bapt $"); 46218822Sdim 47218822Sdim#define SB16_BUFFSIZE 4096 48131722Sobrien#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16) 49131722Sobrien 50131722Sobrienstatic u_int32_t sb16_fmt8[] = { 51131722Sobrien SND_FORMAT(AFMT_U8, 1, 0), 52131722Sobrien SND_FORMAT(AFMT_U8, 2, 0), 53131722Sobrien 0 54131722Sobrien}; 55131722Sobrienstatic struct pcmchan_caps sb16_caps8 = {5000, 45000, sb16_fmt8, 0}; 56131722Sobrien 57131722Sobrienstatic u_int32_t sb16_fmt16[] = { 58131722Sobrien SND_FORMAT(AFMT_S16_LE, 1, 0), 59131722Sobrien SND_FORMAT(AFMT_S16_LE, 2, 0), 60131722Sobrien 0 61131722Sobrien}; 62131722Sobrienstatic struct pcmchan_caps sb16_caps16 = {5000, 45000, sb16_fmt16, 0}; 63131722Sobrien 64131722Sobrienstatic u_int32_t sb16x_fmt[] = { 65131722Sobrien SND_FORMAT(AFMT_U8, 1, 0), 66218822Sdim SND_FORMAT(AFMT_U8, 2, 0), 67218822Sdim SND_FORMAT(AFMT_S16_LE, 1, 0), 68131722Sobrien SND_FORMAT(AFMT_S16_LE, 2, 0), 69131722Sobrien 0 70218822Sdim}; 71218822Sdimstatic struct pcmchan_caps sb16x_caps = {5000, 49000, sb16x_fmt, 0}; 72131722Sobrien 73131722Sobrienstruct sb_info; 74131722Sobrien 75131722Sobrienstruct sb_chinfo { 76218822Sdim struct sb_info *parent; 77131722Sobrien struct pcm_channel *channel; 78131722Sobrien struct snd_dbuf *buffer; 79131722Sobrien int dir, run, dch; 80131722Sobrien u_int32_t fmt, spd, blksz; 81218822Sdim}; 82218822Sdim 83131722Sobrienstruct sb_info { 84131722Sobrien struct resource *io_base; /* I/O address for the board */ 85131722Sobrien struct resource *irq; 86131722Sobrien struct resource *drq1; 87218822Sdim struct resource *drq2; 88218822Sdim void *ih; 89131722Sobrien bus_dma_tag_t parent_dmat; 90131722Sobrien 91131722Sobrien unsigned int bufsize; 92131722Sobrien int bd_id; 93131722Sobrien u_long bd_flags; /* board-specific flags */ 94131722Sobrien int prio, prio16; 95131722Sobrien struct sb_chinfo pch, rch; 96131722Sobrien device_t parent_dev; 97131722Sobrien}; 98131722Sobrien 99131722Sobrien#if 0 100218822Sdimstatic void sb_lock(struct sb_info *sb); 101131722Sobrienstatic void sb_unlock(struct sb_info *sb); 102131722Sobrienstatic int sb_rd(struct sb_info *sb, int reg); 103131722Sobrienstatic void sb_wr(struct sb_info *sb, int reg, u_int8_t val); 104131722Sobrienstatic int sb_cmd(struct sb_info *sb, u_char val); 105131722Sobrien/* static int sb_cmd1(struct sb_info *sb, u_char cmd, int val); */ 106131722Sobrienstatic int sb_cmd2(struct sb_info *sb, u_char cmd, int val); 107131722Sobrienstatic u_int sb_get_byte(struct sb_info *sb); 108131722Sobrienstatic void sb_setmixer(struct sb_info *sb, u_int port, u_int value); 109131722Sobrienstatic int sb_getmixer(struct sb_info *sb, u_int port); 110131722Sobrienstatic int sb_reset_dsp(struct sb_info *sb); 111131722Sobrien 112131722Sobrienstatic void sb_intr(void *arg); 113218822Sdim#endif 114131722Sobrien 115131722Sobrien/* 116131722Sobrien * Common code for the midi and pcm functions 117131722Sobrien * 118131722Sobrien * sb_cmd write a single byte to the CMD port. 119131722Sobrien * sb_cmd1 write a CMD + 1 byte arg 120131722Sobrien * sb_cmd2 write a CMD + 2 byte arg 121131722Sobrien * sb_get_byte returns a single byte from the DSP data port 122131722Sobrien */ 123131722Sobrien 124218822Sdimstatic void 125218822Sdimsb_lock(struct sb_info *sb) { 126218822Sdim 127131722Sobrien sbc_lock(device_get_softc(sb->parent_dev)); 128131722Sobrien} 129218822Sdim 130131722Sobrienstatic void 131131722Sobriensb_lockassert(struct sb_info *sb) { 132218822Sdim 133131722Sobrien sbc_lockassert(device_get_softc(sb->parent_dev)); 134131722Sobrien} 135218822Sdim 136131722Sobrienstatic void 137131722Sobriensb_unlock(struct sb_info *sb) { 138218822Sdim 139131722Sobrien sbc_unlock(device_get_softc(sb->parent_dev)); 140131722Sobrien} 141218822Sdim 142131722Sobrienstatic int 143131722Sobrienport_rd(struct resource *port, int off) 144218822Sdim{ 145131722Sobrien return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off); 146218822Sdim} 147218822Sdim 148218822Sdimstatic void 149131722Sobrienport_wr(struct resource *port, int off, u_int8_t data) 150218822Sdim{ 151131722Sobrien bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data); 152218822Sdim} 153131722Sobrien 154218822Sdimstatic int 155131722Sobriensb_rd(struct sb_info *sb, int reg) 156131722Sobrien{ 157218822Sdim return port_rd(sb->io_base, reg); 158131722Sobrien} 159131722Sobrien 160218822Sdimstatic void 161131722Sobriensb_wr(struct sb_info *sb, int reg, u_int8_t val) 162131722Sobrien{ 163131722Sobrien port_wr(sb->io_base, reg, val); 164131722Sobrien} 165131722Sobrien 166131722Sobrienstatic int 167218822Sdimsb_dspwr(struct sb_info *sb, u_char val) 168131722Sobrien{ 169131722Sobrien int i; 170218822Sdim 171131722Sobrien for (i = 0; i < 1000; i++) { 172131722Sobrien if ((sb_rd(sb, SBDSP_STATUS) & 0x80)) 173218822Sdim DELAY((i > 100)? 1000 : 10); 174131722Sobrien else { 175131722Sobrien sb_wr(sb, SBDSP_CMD, val); 176218822Sdim return 1; 177131722Sobrien } 178131722Sobrien } 179218822Sdim if (curthread->td_intr_nesting_level == 0) 180131722Sobrien printf("sb_dspwr(0x%02x) timed out.\n", val); 181131722Sobrien return 0; 182218822Sdim} 183131722Sobrien 184218822Sdimstatic int 185218822Sdimsb_cmd(struct sb_info *sb, u_char val) 186131722Sobrien{ 187131722Sobrien#if 0 188131722Sobrien printf("sb_cmd: %x\n", val); 189131722Sobrien#endif 190218822Sdim return sb_dspwr(sb, val); 191218822Sdim} 192131722Sobrien 193131722Sobrien/* 194218822Sdimstatic int 195131722Sobriensb_cmd1(struct sb_info *sb, u_char cmd, int val) 196218822Sdim{ 197218822Sdim#if 0 198131722Sobrien printf("sb_cmd1: %x, %x\n", cmd, val); 199131722Sobrien#endif 200131722Sobrien if (sb_dspwr(sb, cmd)) { 201131722Sobrien return sb_dspwr(sb, val & 0xff); 202131722Sobrien } else return 0; 203131722Sobrien} 204131722Sobrien*/ 205131722Sobrien 206131722Sobrienstatic int 207131722Sobriensb_cmd2(struct sb_info *sb, u_char cmd, int val) 208131722Sobrien{ 209131722Sobrien int r; 210131722Sobrien 211131722Sobrien#if 0 212131722Sobrien printf("sb_cmd2: %x, %x\n", cmd, val); 213131722Sobrien#endif 214131722Sobrien sb_lockassert(sb); 215131722Sobrien r = 0; 216131722Sobrien if (sb_dspwr(sb, cmd)) { 217131722Sobrien if (sb_dspwr(sb, val & 0xff)) { 218131722Sobrien if (sb_dspwr(sb, (val >> 8) & 0xff)) { 219131722Sobrien r = 1; 220131722Sobrien } 221218822Sdim } 222131722Sobrien } 223218822Sdim 224131722Sobrien return r; 225131722Sobrien} 226218822Sdim 227218822Sdim/* 228218822Sdim * in the SB, there is a set of indirect "mixer" registers with 229218822Sdim * address at offset 4, data at offset 5 230218822Sdim */ 231218822Sdimstatic void 232218822Sdimsb_setmixer(struct sb_info *sb, u_int port, u_int value) 233218822Sdim{ 234131722Sobrien sb_lock(sb); 235131722Sobrien sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 236 DELAY(10); 237 sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff)); 238 DELAY(10); 239 sb_unlock(sb); 240} 241 242static int 243sb_getmixer(struct sb_info *sb, u_int port) 244{ 245 int val; 246 247 sb_lockassert(sb); 248 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 249 DELAY(10); 250 val = sb_rd(sb, SB_MIX_DATA); 251 DELAY(10); 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 u_int32_t 372sb16mix_setrecsrc(struct snd_mixer *m, u_int32_t src) 373{ 374 struct sb_info *sb = mix_getdevinfo(m); 375 u_char recdev_l, recdev_r; 376 377 recdev_l = 0; 378 recdev_r = 0; 379 if (src & SOUND_MASK_MIC) { 380 recdev_l |= 0x01; /* mono mic */ 381 recdev_r |= 0x01; 382 } 383 384 if (src & SOUND_MASK_CD) { 385 recdev_l |= 0x04; /* l cd */ 386 recdev_r |= 0x02; /* r cd */ 387 } 388 389 if (src & SOUND_MASK_LINE) { 390 recdev_l |= 0x10; /* l line */ 391 recdev_r |= 0x08; /* r line */ 392 } 393 394 if (src & SOUND_MASK_SYNTH) { 395 recdev_l |= 0x40; /* l midi */ 396 recdev_r |= 0x20; /* r midi */ 397 } 398 399 sb_setmixer(sb, SB16_IMASK_L, recdev_l); 400 sb_setmixer(sb, SB16_IMASK_R, recdev_r); 401 402 /* Switch on/off FM tuner source */ 403 if (src & SOUND_MASK_LINE1) 404 sb_setmixer(sb, 0x4a, 0x0c); 405 else 406 sb_setmixer(sb, 0x4a, 0x00); 407 408 /* 409 * since the same volume controls apply to the input and 410 * output sections, the best approach to have a consistent 411 * behaviour among cards would be to disable the output path 412 * on devices which are used to record. 413 * However, since users like to have feedback, we only disable 414 * the mic -- permanently. 415 */ 416 sb_setmixer(sb, SB16_OMASK, 0x1f & ~1); 417 418 return src; 419} 420 421static kobj_method_t sb16mix_mixer_methods[] = { 422 KOBJMETHOD(mixer_init, sb16mix_init), 423 KOBJMETHOD(mixer_set, sb16mix_set), 424 KOBJMETHOD(mixer_setrecsrc, sb16mix_setrecsrc), 425 KOBJMETHOD_END 426}; 427MIXER_DECLARE(sb16mix_mixer); 428 429/************************************************************/ 430 431static void 432sb16_release_resources(struct sb_info *sb, device_t dev) 433{ 434 if (sb->irq) { 435 if (sb->ih) 436 bus_teardown_intr(dev, sb->irq, sb->ih); 437 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq); 438 sb->irq = 0; 439 } 440 if (sb->drq2) { 441 if (sb->drq2 != sb->drq1) { 442 isa_dma_release(rman_get_start(sb->drq2)); 443 bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2); 444 } 445 sb->drq2 = 0; 446 } 447 if (sb->drq1) { 448 isa_dma_release(rman_get_start(sb->drq1)); 449 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1); 450 sb->drq1 = 0; 451 } 452 if (sb->io_base) { 453 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base); 454 sb->io_base = 0; 455 } 456 if (sb->parent_dmat) { 457 bus_dma_tag_destroy(sb->parent_dmat); 458 sb->parent_dmat = 0; 459 } 460 free(sb, M_DEVBUF); 461} 462 463static int 464sb16_alloc_resources(struct sb_info *sb, device_t dev) 465{ 466 int rid; 467 468 rid = 0; 469 if (!sb->io_base) 470 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 471 &rid, RF_ACTIVE); 472 473 rid = 0; 474 if (!sb->irq) 475 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 476 RF_ACTIVE); 477 478 rid = 0; 479 if (!sb->drq1) 480 sb->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid, 481 RF_ACTIVE); 482 483 rid = 1; 484 if (!sb->drq2) 485 sb->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid, 486 RF_ACTIVE); 487 488 if (sb->io_base && sb->drq1 && sb->irq) { 489 isa_dma_acquire(rman_get_start(sb->drq1)); 490 isa_dmainit(rman_get_start(sb->drq1), sb->bufsize); 491 492 if (sb->drq2) { 493 isa_dma_acquire(rman_get_start(sb->drq2)); 494 isa_dmainit(rman_get_start(sb->drq2), sb->bufsize); 495 } else { 496 sb->drq2 = sb->drq1; 497 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 498 } 499 return 0; 500 } else return ENXIO; 501} 502 503/* sbc does locking for us */ 504static void 505sb_intr(void *arg) 506{ 507 struct sb_info *sb = (struct sb_info *)arg; 508 int reason, c; 509 510 /* 511 * The Vibra16X has separate flags for 8 and 16 bit transfers, but 512 * I have no idea how to tell capture from playback interrupts... 513 */ 514 515 reason = 0; 516 sb_lock(sb); 517 c = sb_getmixer(sb, IRQ_STAT); 518 if (c & 1) 519 sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */ 520 521 if (c & 2) 522 sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */ 523 sb_unlock(sb); 524 525 /* 526 * this tells us if the source is 8-bit or 16-bit dma. We 527 * have to check the io channel to map it to read or write... 528 */ 529 530 if (sb->bd_flags & BD_F_SB16X) { 531 if (c & 1) { /* 8-bit format */ 532 if (sb->pch.fmt & AFMT_8BIT) 533 reason |= 1; 534 if (sb->rch.fmt & AFMT_8BIT) 535 reason |= 2; 536 } 537 if (c & 2) { /* 16-bit format */ 538 if (sb->pch.fmt & AFMT_16BIT) 539 reason |= 1; 540 if (sb->rch.fmt & AFMT_16BIT) 541 reason |= 2; 542 } 543 } else { 544 if (c & 1) { /* 8-bit dma */ 545 if (sb->pch.dch == 1) 546 reason |= 1; 547 if (sb->rch.dch == 1) 548 reason |= 2; 549 } 550 if (c & 2) { /* 16-bit dma */ 551 if (sb->pch.dch == 2) 552 reason |= 1; 553 if (sb->rch.dch == 2) 554 reason |= 2; 555 } 556 } 557#if 0 558 printf("sb_intr: reason=%d c=0x%x\n", reason, c); 559#endif 560 if ((reason & 1) && (sb->pch.run)) 561 chn_intr(sb->pch.channel); 562 563 if ((reason & 2) && (sb->rch.run)) 564 chn_intr(sb->rch.channel); 565} 566 567static int 568sb_setup(struct sb_info *sb) 569{ 570 struct sb_chinfo *ch; 571 u_int8_t v; 572 int l, pprio; 573 574 sb_lock(sb); 575 if (sb->bd_flags & BD_F_DMARUN) 576 sndbuf_dma(sb->pch.buffer, PCMTRIG_STOP); 577 if (sb->bd_flags & BD_F_DMARUN2) 578 sndbuf_dma(sb->rch.buffer, PCMTRIG_STOP); 579 sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2); 580 581 sb_reset_dsp(sb); 582 583 if (sb->bd_flags & BD_F_SB16X) { 584 /* full-duplex doesn't work! */ 585 pprio = sb->pch.run? 1 : 0; 586 sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq1 : sb->drq2); 587 sb->pch.dch = pprio? 1 : 0; 588 sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq2 : sb->drq1); 589 sb->rch.dch = pprio? 2 : 1; 590 } else { 591 if (sb->pch.run && sb->rch.run) { 592 pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1; 593 sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq2 : sb->drq1); 594 sb->pch.dch = pprio? 2 : 1; 595 sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq1 : sb->drq2); 596 sb->rch.dch = pprio? 1 : 2; 597 } else { 598 if (sb->pch.run) { 599 sndbuf_dmasetup(sb->pch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1); 600 sb->pch.dch = (sb->pch.fmt & AFMT_16BIT)? 2 : 1; 601 sndbuf_dmasetup(sb->rch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2); 602 sb->rch.dch = (sb->pch.fmt & AFMT_16BIT)? 1 : 2; 603 } else if (sb->rch.run) { 604 sndbuf_dmasetup(sb->pch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2); 605 sb->pch.dch = (sb->rch.fmt & AFMT_16BIT)? 1 : 2; 606 sndbuf_dmasetup(sb->rch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1); 607 sb->rch.dch = (sb->rch.fmt & AFMT_16BIT)? 2 : 1; 608 } 609 } 610 } 611 612 sndbuf_dmasetdir(sb->pch.buffer, PCMDIR_PLAY); 613 sndbuf_dmasetdir(sb->rch.buffer, PCMDIR_REC); 614 615 /* 616 printf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n", 617 sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run); 618 */ 619 620 ch = &sb->pch; 621 if (ch->run) { 622 l = ch->blksz; 623 if (ch->fmt & AFMT_16BIT) 624 l >>= 1; 625 l--; 626 627 /* play speed */ 628 RANGE(ch->spd, 5000, 45000); 629 sb_cmd(sb, DSP_CMD_OUT16); 630 sb_cmd(sb, ch->spd >> 8); 631 sb_cmd(sb, ch->spd & 0xff); 632 633 /* play format, length */ 634 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC; 635 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8; 636 sb_cmd(sb, v); 637 638 v = (AFMT_CHANNEL(ch->fmt) > 1)? DSP_F16_STEREO : 0; 639 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0; 640 sb_cmd2(sb, v, l); 641 sndbuf_dma(ch->buffer, PCMTRIG_START); 642 sb->bd_flags |= BD_F_DMARUN; 643 } 644 645 ch = &sb->rch; 646 if (ch->run) { 647 l = ch->blksz; 648 if (ch->fmt & AFMT_16BIT) 649 l >>= 1; 650 l--; 651 652 /* record speed */ 653 RANGE(ch->spd, 5000, 45000); 654 sb_cmd(sb, DSP_CMD_IN16); 655 sb_cmd(sb, ch->spd >> 8); 656 sb_cmd(sb, ch->spd & 0xff); 657 658 /* record format, length */ 659 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC; 660 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8; 661 sb_cmd(sb, v); 662 663 v = (AFMT_CHANNEL(ch->fmt) > 1)? DSP_F16_STEREO : 0; 664 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0; 665 sb_cmd2(sb, v, l); 666 sndbuf_dma(ch->buffer, PCMTRIG_START); 667 sb->bd_flags |= BD_F_DMARUN2; 668 } 669 sb_unlock(sb); 670 671 return 0; 672} 673 674/* channel interface */ 675static void * 676sb16chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 677{ 678 struct sb_info *sb = devinfo; 679 struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch; 680 681 ch->parent = sb; 682 ch->channel = c; 683 ch->buffer = b; 684 ch->dir = dir; 685 686 if (sndbuf_alloc(ch->buffer, sb->parent_dmat, 0, sb->bufsize) != 0) 687 return NULL; 688 689 return ch; 690} 691 692static int 693sb16chan_setformat(kobj_t obj, void *data, u_int32_t format) 694{ 695 struct sb_chinfo *ch = data; 696 struct sb_info *sb = ch->parent; 697 698 ch->fmt = format; 699 sb->prio = ch->dir; 700 sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0; 701 702 return 0; 703} 704 705static u_int32_t 706sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed) 707{ 708 struct sb_chinfo *ch = data; 709 710 ch->spd = speed; 711 return speed; 712} 713 714static u_int32_t 715sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 716{ 717 struct sb_chinfo *ch = data; 718 719 ch->blksz = blocksize; 720 return ch->blksz; 721} 722 723static int 724sb16chan_trigger(kobj_t obj, void *data, int go) 725{ 726 struct sb_chinfo *ch = data; 727 struct sb_info *sb = ch->parent; 728 729 if (!PCMTRIG_COMMON(go)) 730 return 0; 731 732 if (go == PCMTRIG_START) 733 ch->run = 1; 734 else 735 ch->run = 0; 736 737 sb_setup(sb); 738 739 return 0; 740} 741 742static u_int32_t 743sb16chan_getptr(kobj_t obj, void *data) 744{ 745 struct sb_chinfo *ch = data; 746 747 return sndbuf_dmaptr(ch->buffer); 748} 749 750static struct pcmchan_caps * 751sb16chan_getcaps(kobj_t obj, void *data) 752{ 753 struct sb_chinfo *ch = data; 754 struct sb_info *sb = ch->parent; 755 756 if ((sb->prio == 0) || (sb->prio == ch->dir)) 757 return &sb16x_caps; 758 else 759 return sb->prio16? &sb16_caps8 : &sb16_caps16; 760} 761 762static int 763sb16chan_resetdone(kobj_t obj, void *data) 764{ 765 struct sb_chinfo *ch = data; 766 struct sb_info *sb = ch->parent; 767 768 sb->prio = 0; 769 770 return 0; 771} 772 773static kobj_method_t sb16chan_methods[] = { 774 KOBJMETHOD(channel_init, sb16chan_init), 775 KOBJMETHOD(channel_resetdone, sb16chan_resetdone), 776 KOBJMETHOD(channel_setformat, sb16chan_setformat), 777 KOBJMETHOD(channel_setspeed, sb16chan_setspeed), 778 KOBJMETHOD(channel_setblocksize, sb16chan_setblocksize), 779 KOBJMETHOD(channel_trigger, sb16chan_trigger), 780 KOBJMETHOD(channel_getptr, sb16chan_getptr), 781 KOBJMETHOD(channel_getcaps, sb16chan_getcaps), 782 KOBJMETHOD_END 783}; 784CHANNEL_DECLARE(sb16chan); 785 786/************************************************************/ 787 788static int 789sb16_probe(device_t dev) 790{ 791 char buf[64]; 792 uintptr_t func, ver, r, f; 793 794 /* The parent device has already been probed. */ 795 r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func); 796 if (func != SCF_PCM) 797 return (ENXIO); 798 799 r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver); 800 f = (ver & 0xffff0000) >> 16; 801 ver &= 0x0000ffff; 802 if (f & BD_F_SB16) { 803 snprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff, 804 (f & BD_F_SB16X)? " (ViBRA16X)" : ""); 805 device_set_desc_copy(dev, buf); 806 return 0; 807 } else 808 return (ENXIO); 809} 810 811static int 812sb16_attach(device_t dev) 813{ 814 struct sb_info *sb; 815 uintptr_t ver; 816 char status[SND_STATUSLEN], status2[SND_STATUSLEN]; 817 818 sb = malloc(sizeof(*sb), M_DEVBUF, M_WAITOK | M_ZERO); 819 sb->parent_dev = device_get_parent(dev); 820 BUS_READ_IVAR(sb->parent_dev, dev, 1, &ver); 821 sb->bd_id = ver & 0x0000ffff; 822 sb->bd_flags = (ver & 0xffff0000) >> 16; 823 sb->bufsize = pcm_getbuffersize(dev, 4096, SB16_BUFFSIZE, 65536); 824 825 if (sb16_alloc_resources(sb, dev)) 826 goto no; 827 sb_lock(sb); 828 if (sb_reset_dsp(sb)) { 829 sb_unlock(sb); 830 goto no; 831 } 832 sb_unlock(sb); 833 if (mixer_init(dev, &sb16mix_mixer_class, sb)) 834 goto no; 835 if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih)) 836 goto no; 837 838 if (sb->bd_flags & BD_F_SB16X) 839 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 840 841 sb->prio = 0; 842 843 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, 844 /*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