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