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