1 /* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <dev/sound/pcm/sound.h> 30 #include <dev/sound/pcm/ac97.h> 31 #include <gnu/dev/sound/pci/emu10k1.h> 32 33 #include <pci/pcireg.h> 34 #include <pci/pcivar.h> 35 #include <sys/queue.h> 36 37 /* -------------------------------------------------------------------- */ 38 39 #define EMU10K1_PCI_ID 0x00021102 40 #define EMU_BUFFSIZE 4096 41 #define EMU_CHANS 4 42 #undef EMUDEBUG 43 44 struct emu_memblk { 45 SLIST_ENTRY(emu_memblk) link; 46 void *buf; 47 u_int32_t pte_start, pte_size; 48 }; 49 50 struct emu_mem { 51 u_int8_t bmap[MAXPAGES / 8]; 52 u_int32_t *ptb_pages; 53 void *silent_page; 54 SLIST_HEAD(, emu_memblk) blocks; 55 }; 56 57 struct emu_voice { 58 int vnum; 59 int b16:1, stereo:1, busy:1, ismaster:1, istracker:1; 60 int speed; 61 int start, end, vol; 62 u_int32_t buf; 63 struct emu_voice *slave, *tracker; 64 pcm_channel *channel; 65 }; 66 67 struct sc_info; 68 69 /* channel registers */ 70 struct sc_pchinfo { 71 int spd, fmt, run; 72 struct emu_voice *master, *slave, *tracker; 73 snd_dbuf *buffer; 74 pcm_channel *channel; 75 struct sc_info *parent; 76 }; 77 78 struct sc_rchinfo { 79 int spd, fmt, run, num; 80 u_int32_t idxreg, basereg, sizereg, setupreg, irqmask; 81 snd_dbuf *buffer; 82 pcm_channel *channel; 83 struct sc_info *parent; 84 }; 85 86 /* device private data */ 87 struct sc_info { 88 device_t dev; 89 u_int32_t type, rev; 90 u_int32_t tos_link:1, APS:1; 91 92 bus_space_tag_t st; 93 bus_space_handle_t sh; 94 bus_dma_tag_t parent_dmat; 95 96 struct resource *reg, *irq; 97 int regtype, regid, irqid; 98 void *ih; 99 100 int pnum, rnum; 101 struct emu_mem mem; 102 struct emu_voice voice[64]; 103 struct sc_pchinfo pch[EMU_CHANS]; 104 struct sc_rchinfo rch[2]; 105 }; 106 107 /* -------------------------------------------------------------------- */ 108 109 /* 110 * prototypes 111 */ 112 113 /* channel interface */ 114 static void *emupchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 115 static int emupchan_setdir(void *data, int dir); 116 static int emupchan_setformat(void *data, u_int32_t format); 117 static int emupchan_setspeed(void *data, u_int32_t speed); 118 static int emupchan_setblocksize(void *data, u_int32_t blocksize); 119 static int emupchan_trigger(void *data, int go); 120 static int emupchan_getptr(void *data); 121 static pcmchan_caps *emupchan_getcaps(void *data); 122 123 /* channel interface */ 124 static void *emurchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 125 static int emurchan_setdir(void *data, int dir); 126 static int emurchan_setformat(void *data, u_int32_t format); 127 static int emurchan_setspeed(void *data, u_int32_t speed); 128 static int emurchan_setblocksize(void *data, u_int32_t blocksize); 129 static int emurchan_trigger(void *data, int go); 130 static int emurchan_getptr(void *data); 131 static pcmchan_caps *emurchan_getcaps(void *data); 132 133 /* talk to the codec - called from ac97.c */ 134 static u_int32_t emu_rdcd(void *, int); 135 static void emu_wrcd(void *, int, u_int32_t); 136 137 /* stuff */ 138 static int emu_init(struct sc_info *); 139 static void emu_intr(void *); 140 static void *emu_malloc(struct sc_info *sc, u_int32_t sz); 141 static void *emu_memalloc(struct sc_info *sc, u_int32_t sz); 142 #ifdef notyet 143 static int emu_memfree(struct sc_info *sc, void *buf); 144 #endif 145 static int emu_memstart(struct sc_info *sc, void *buf); 146 #ifdef EMUDEBUG 147 static void emu_vdump(struct sc_info *sc, struct emu_voice *v); 148 #endif 149 150 /* talk to the card */ 151 static u_int32_t emu_rd(struct sc_info *, int, int); 152 static void emu_wr(struct sc_info *, int, u_int32_t, int); 153 154 /* -------------------------------------------------------------------- */ 155 156 static pcmchan_caps emu_reccaps[3] = { 157 {8000, 48000, AFMT_STEREO | AFMT_S16_LE, AFMT_STEREO | AFMT_S16_LE}, 158 {8000, 8000, AFMT_U8, AFMT_U8}, 159 {48000, 48000, AFMT_STEREO | AFMT_S16_LE, AFMT_STEREO | AFMT_S16_LE}, 160 }; 161 162 static pcmchan_caps emu_playcaps = { 163 4000, 48000, 164 AFMT_STEREO | AFMT_U8 | AFMT_S16_LE, 165 AFMT_STEREO | AFMT_S16_LE 166 }; 167 168 static pcm_channel emu_chantemplate = { 169 emupchan_init, 170 emupchan_setdir, 171 emupchan_setformat, 172 emupchan_setspeed, 173 emupchan_setblocksize, 174 emupchan_trigger, 175 emupchan_getptr, 176 emupchan_getcaps, 177 }; 178 179 static pcm_channel emur_chantemplate = { 180 emurchan_init, 181 emurchan_setdir, 182 emurchan_setformat, 183 emurchan_setspeed, 184 emurchan_setblocksize, 185 emurchan_trigger, 186 emurchan_getptr, 187 emurchan_getcaps, 188 }; 189 190 static int adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000}; 191 192 /* -------------------------------------------------------------------- */ 193 /* Hardware */ 194 static u_int32_t 195 emu_rd(struct sc_info *sc, int regno, int size) 196 { 197 switch (size) { 198 case 1: 199 return bus_space_read_1(sc->st, sc->sh, regno); 200 case 2: 201 return bus_space_read_2(sc->st, sc->sh, regno); 202 case 4: 203 return bus_space_read_4(sc->st, sc->sh, regno); 204 default: 205 return 0xffffffff; 206 } 207 } 208 209 static void 210 emu_wr(struct sc_info *sc, int regno, u_int32_t data, int size) 211 { 212 switch (size) { 213 case 1: 214 bus_space_write_1(sc->st, sc->sh, regno, data); 215 break; 216 case 2: 217 bus_space_write_2(sc->st, sc->sh, regno, data); 218 break; 219 case 4: 220 bus_space_write_4(sc->st, sc->sh, regno, data); 221 break; 222 } 223 } 224 225 static u_int32_t 226 emu_rdptr(struct sc_info *sc, int chn, int reg) 227 { 228 u_int32_t ptr, val, mask, size, offset; 229 230 ptr = ((reg << 16) & PTR_ADDRESS_MASK) | (chn & PTR_CHANNELNUM_MASK); 231 emu_wr(sc, PTR, ptr, 4); 232 val = emu_rd(sc, DATA, 4); 233 if (reg & 0xff000000) { 234 size = (reg >> 24) & 0x3f; 235 offset = (reg >> 16) & 0x1f; 236 mask = ((1 << size) - 1) << offset; 237 val &= mask; 238 val >>= offset; 239 } 240 return val; 241 } 242 243 static void 244 emu_wrptr(struct sc_info *sc, int chn, int reg, u_int32_t data) 245 { 246 u_int32_t ptr, mask, size, offset; 247 248 ptr = ((reg << 16) & PTR_ADDRESS_MASK) | (chn & PTR_CHANNELNUM_MASK); 249 emu_wr(sc, PTR, ptr, 4); 250 if (reg & 0xff000000) { 251 size = (reg >> 24) & 0x3f; 252 offset = (reg >> 16) & 0x1f; 253 mask = ((1 << size) - 1) << offset; 254 data <<= offset; 255 data &= mask; 256 data |= emu_rd(sc, DATA, 4) & ~mask; 257 } 258 emu_wr(sc, DATA, data, 4); 259 } 260 261 static void 262 emu_wrefx(struct sc_info *sc, unsigned int pc, unsigned int data) 263 { 264 emu_wrptr(sc, 0, MICROCODEBASE + pc, data); 265 } 266 267 /* playback channel interrupts */ 268 static u_int32_t 269 emu_testint(struct sc_info *sc, char channel) 270 { 271 int reg = (channel & 0x20)? CLIPH : CLIPL; 272 channel &= 0x1f; 273 reg |= 1 << 24; 274 reg |= channel << 16; 275 return emu_rdptr(sc, 0, reg); 276 } 277 278 static void 279 emu_clrint(struct sc_info *sc, char channel) 280 { 281 int reg = (channel & 0x20)? CLIPH : CLIPL; 282 channel &= 0x1f; 283 reg |= 1 << 24; 284 reg |= channel << 16; 285 emu_wrptr(sc, 0, reg, 1); 286 } 287 288 static void 289 emu_enaint(struct sc_info *sc, char channel, int enable) 290 { 291 int reg = (channel & 0x20)? CLIEH : CLIEL; 292 channel &= 0x1f; 293 reg |= 1 << 24; 294 reg |= channel << 16; 295 emu_wrptr(sc, 0, reg, enable); 296 } 297 298 static void 299 emu_enastop(struct sc_info *sc, char channel, int enable) 300 { 301 int reg = (channel & 0x20)? SOLEH : SOLEL; 302 channel &= 0x1f; 303 reg |= 1 << 24; 304 reg |= channel << 16; 305 emu_wrptr(sc, 0, reg, enable); 306 } 307 308 static int 309 emu_recval(int speed) { 310 int val; 311 312 val = 0; 313 while (val < 7 && speed < adcspeed[val]) 314 val++; 315 return val; 316 } 317 318 /* ac97 codec */ 319 static u_int32_t 320 emu_rdcd(void *devinfo, int regno) 321 { 322 struct sc_info *sc = (struct sc_info *)devinfo; 323 324 emu_wr(sc, AC97ADDRESS, regno, 1); 325 return emu_rd(sc, AC97DATA, 2); 326 } 327 328 static void 329 emu_wrcd(void *devinfo, int regno, u_int32_t data) 330 { 331 struct sc_info *sc = (struct sc_info *)devinfo; 332 333 emu_wr(sc, AC97ADDRESS, regno, 1); 334 emu_wr(sc, AC97DATA, data, 2); 335 } 336 337 static u_int32_t 338 emu_rate_to_pitch(u_int32_t rate) 339 { 340 static u_int32_t logMagTable[128] = { 341 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2, 342 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5, 343 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081, 344 0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191, 345 0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 346 0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 347 0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e, 348 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26, 349 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d, 350 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885, 351 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 352 0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 353 0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 354 0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3, 355 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83, 356 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df 357 }; 358 static char logSlopeTable[128] = { 359 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58, 360 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53, 361 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f, 362 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b, 363 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47, 364 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44, 365 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41, 366 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e, 367 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c, 368 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39, 369 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37, 370 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35, 371 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34, 372 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32, 373 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 374 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f 375 }; 376 int i; 377 378 if (rate == 0) 379 return 0; /* Bail out if no leading "1" */ 380 rate *= 11185; /* Scale 48000 to 0x20002380 */ 381 for (i = 31; i > 0; i--) { 382 if (rate & 0x80000000) { /* Detect leading "1" */ 383 return (((u_int32_t) (i - 15) << 20) + 384 logMagTable[0x7f & (rate >> 24)] + 385 (0x7f & (rate >> 17)) * 386 logSlopeTable[0x7f & (rate >> 24)]); 387 } 388 rate <<= 1; 389 } 390 391 return 0; /* Should never reach this point */ 392 } 393 394 static struct emu_voice * 395 emu_valloc(struct sc_info *sc) 396 { 397 struct emu_voice *v; 398 int i; 399 400 v = NULL; 401 for (i = 0; i < 64 && sc->voice[i].busy; i++); 402 if (i < 64) { 403 v = &sc->voice[i]; 404 v->busy = 1; 405 } 406 return v; 407 } 408 409 static int 410 emu_vinit(struct sc_info *sc, struct emu_voice *m, struct emu_voice *s, struct emu_voice *t, 411 u_int32_t sz, pcm_channel *c) 412 { 413 void *buf; 414 415 buf = emu_memalloc(sc, sz); 416 if (buf == NULL) 417 return -1; 418 if (c != NULL) { 419 c->buffer.buf = buf; 420 c->buffer.bufsize = sz; 421 } 422 m->start = emu_memstart(sc, buf) * EMUPAGESIZE; 423 m->end = m->start + sz; 424 m->channel = NULL; 425 m->speed = 0; 426 m->b16 = 0; 427 m->stereo = 0; 428 m->ismaster = 1; 429 m->istracker = 0; 430 m->vol = 0xff; 431 m->buf = vtophys(buf); 432 m->slave = s; 433 m->tracker = t; 434 if (s != NULL) { 435 s->start = m->start; 436 s->end = m->end; 437 s->channel = NULL; 438 s->speed = 0; 439 s->b16 = 0; 440 s->stereo = 0; 441 s->ismaster = 0; 442 s->istracker = 0; 443 s->vol = m->vol; 444 s->buf = m->buf; 445 s->slave = NULL; 446 s->tracker = NULL; 447 } 448 if (t != NULL) { 449 t->start = m->start; 450 t->end = t->start + sz / 2; 451 t->channel = c; 452 t->speed = 0; 453 t->b16 = 0; 454 t->stereo = 0; 455 t->ismaster = 0; 456 t->istracker = 1; 457 t->vol = 0; 458 t->buf = m->buf; 459 t->slave = NULL; 460 t->tracker = NULL; 461 } 462 return 0; 463 } 464 465 static void 466 emu_vsetup(struct sc_pchinfo *ch) 467 { 468 struct emu_voice *v = ch->master; 469 470 if (ch->fmt) { 471 v->b16 = (ch->fmt & AFMT_16BIT)? 1 : 0; 472 v->stereo = (ch->fmt & AFMT_STEREO)? 1 : 0; 473 if (v->slave != NULL) { 474 v->slave->b16 = v->b16; 475 v->slave->stereo = v->stereo; 476 } 477 if (v->tracker != NULL) { 478 v->tracker->b16 = v->b16; 479 v->tracker->stereo = v->stereo; 480 } 481 } 482 if (ch->spd) { 483 v->speed = ch->spd; 484 if (v->slave != NULL) 485 v->slave->speed = v->speed; 486 if (v->tracker != NULL) 487 v->tracker->speed = v->speed; 488 } 489 } 490 491 static void 492 emu_vwrite(struct sc_info *sc, struct emu_voice *v) 493 { 494 int s, l, r, p, x; 495 u_int32_t sa, ea, start = 0, val = 0, v2 = 0, sample, silent_page, i; 496 497 s = (v->stereo? 1 : 0) + (v->b16? 1 : 0); 498 sa = v->start >> s; 499 ea = v->end >> s; 500 l = r = x = v->vol; 501 if (v->stereo) { 502 l = v->ismaster? l : 0; 503 r = v->ismaster? 0 : r; 504 } 505 p = emu_rate_to_pitch(v->speed) >> 8; 506 sample = v->b16? 0 : 0x80808080; 507 508 emu_wrptr(sc, v->vnum, DCYSUSV, ENV_OFF); 509 emu_wrptr(sc, v->vnum, VTFT, VTFT_FILTERTARGET_MASK); 510 emu_wrptr(sc, v->vnum, CVCF, CVCF_CURRENTFILTER_MASK); 511 emu_wrptr(sc, v->vnum, FXRT, 0xd01c0000); 512 513 emu_wrptr(sc, v->vnum, PTRX, (x << 8) | r); 514 if (v->ismaster) { 515 val = 0x20; 516 if (v->stereo) { 517 val <<= 1; 518 emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK); 519 emu_wrptr(sc, v->slave->vnum, CPF, CPF_STEREO_MASK); 520 } else 521 emu_wrptr(sc, v->vnum, CPF, 0); 522 sample = 0x80808080; 523 if (!v->b16) 524 val <<= 1; 525 val -= 4; 526 /* 527 * mono 8bit: val = 0x3c 528 * stereo 8bit: val = 0x7c 529 * mono 16bit: val = 0x1c 530 * stereo 16bit: val = 0x3c 531 */ 532 if (v->stereo) { 533 v2 = 0x3c << 16; 534 emu_wrptr(sc, v->vnum, CCR, v2); 535 emu_wrptr(sc, v->slave->vnum, CCR, val << 16); 536 emu_wrptr(sc, v->slave->vnum, CDE, sample); 537 emu_wrptr(sc, v->slave->vnum, CDF, sample); 538 start = sa + val / 2; 539 } else { 540 v2 = 0x1c << 16; 541 emu_wrptr(sc, v->vnum, CCR, v2); 542 emu_wrptr(sc, v->vnum, CDE, sample); 543 emu_wrptr(sc, v->vnum, CDF, sample); 544 start = sa + val; 545 } 546 val <<= 25; 547 val |= v2; 548 /* 549 * mono 8bit: val = 0x781c0000 550 * stereo 8bit: val = 0xf83c0000 551 * mono 16bit: val = 0x381c0000 552 * stereo 16bit: val = 0x783c0000 553 */ 554 start |= CCCA_INTERPROM_0; 555 } 556 emu_wrptr(sc, v->vnum, DSL, ea); 557 emu_wrptr(sc, v->vnum, PSST, sa | (l << 24)); 558 emu_wrptr(sc, v->vnum, CCCA, start | (v->b16? 0 : CCCA_8BITSELECT)); 559 560 emu_wrptr(sc, v->vnum, Z1, 0); 561 emu_wrptr(sc, v->vnum, Z2, 0); 562 563 silent_page = ((u_int32_t)v->buf << 1) | (v->start / EMUPAGESIZE); 564 /* silent_page = ((u_int32_t)vtophys(sc->mem.silent_page) << 1) | MAP_PTI_MASK; */ 565 emu_wrptr(sc, v->vnum, MAPA, silent_page); 566 emu_wrptr(sc, v->vnum, MAPB, silent_page); 567 568 if (v->ismaster) 569 emu_wrptr(sc, v->vnum, CCR, val); 570 571 for (i = CD0; i < CDF; i++) 572 emu_wrptr(sc, v->vnum, i, sample); 573 574 emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK); 575 emu_wrptr(sc, v->vnum, LFOVAL1, 0x8000); 576 emu_wrptr(sc, v->vnum, ATKHLDM, 0); 577 emu_wrptr(sc, v->vnum, DCYSUSM, DCYSUSM_DECAYTIME_MASK); 578 emu_wrptr(sc, v->vnum, LFOVAL2, 0x8000); 579 emu_wrptr(sc, v->vnum, IP, p); 580 emu_wrptr(sc, v->vnum, PEFE, 0x7f); 581 emu_wrptr(sc, v->vnum, FMMOD, 0); 582 emu_wrptr(sc, v->vnum, TREMFRQ, 0); 583 emu_wrptr(sc, v->vnum, FM2FRQ2, 0); 584 emu_wrptr(sc, v->vnum, ENVVAL, 0xbfff); 585 emu_wrptr(sc, v->vnum, ENVVOL, 0xbfff); 586 emu_wrptr(sc, v->vnum, IFATN, IFATN_FILTERCUTOFF_MASK); 587 588 if (v->slave != NULL) 589 emu_vwrite(sc, v->slave); 590 if (v->tracker != NULL) 591 emu_vwrite(sc, v->tracker); 592 } 593 594 #define IP_TO_CP(ip) ((ip == 0) ? 0 : (((0x00001000uL | (ip & 0x00000FFFL)) << (((ip >> 12) & 0x000FL) + 4)) & 0xFFFF0000uL)) 595 static void 596 emu_vtrigger(struct sc_info *sc, struct emu_voice *v, int go) 597 { 598 u_int32_t pitch_target; 599 if (go) { 600 pitch_target = IP_TO_CP((emu_rate_to_pitch(v->speed) >> 8)) >> 16; 601 emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, pitch_target); 602 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, pitch_target); 603 emu_wrptr(sc, v->vnum, VTFT, 0xffff); 604 emu_wrptr(sc, v->vnum, CVCF, 0xffff); 605 emu_enastop(sc, v->vnum, 0); 606 emu_enaint(sc, v->vnum, v->istracker); 607 emu_wrptr(sc, v->vnum, DCYSUSV, ENV_ON | 0x00007f7f); 608 } else { 609 emu_wrptr(sc, v->vnum, IFATN, 0xffff); 610 emu_wrptr(sc, v->vnum, IP, 0); 611 emu_wrptr(sc, v->vnum, VTFT, 0xffff); 612 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, 0); 613 emu_enaint(sc, v->vnum, 0); 614 } 615 if (v->slave != NULL) 616 emu_vtrigger(sc, v->slave, go); 617 if (v->tracker != NULL) 618 emu_vtrigger(sc, v->tracker, go); 619 } 620 621 static int 622 emu_vpos(struct sc_info *sc, struct emu_voice *v) 623 { 624 int s, ptr; 625 626 s = (v->b16? 1 : 0) + (v->stereo? 1 : 0); 627 ptr = (emu_rdptr(sc, v->vnum, CCCA_CURRADDR) << s) - v->start; 628 return ptr; 629 } 630 631 #ifdef EMUDEBUG 632 static void 633 emu_vdump(struct sc_info *sc, struct emu_voice *v) 634 { 635 char *regname[] = { "cpf", "ptrx", "cvcf", "vtft", "z2", "z1", "psst", "dsl", 636 "ccca", "ccr", "clp", "fxrt", "mapa", "mapb", NULL, NULL, 637 "envvol", "atkhldv", "dcysusv", "lfoval1", 638 "envval", "atkhldm", "dcysusm", "lfoval2", 639 "ip", "ifatn", "pefe", "fmmod", "tremfrq", "fmfrq2", 640 "tempenv" }; 641 int i, x; 642 643 printf("voice number %d\n", v->vnum); 644 for (i = 0, x = 0; i <= 0x1e; i++) { 645 if (regname[i] == NULL) 646 continue; 647 printf("%s\t[%08x]", regname[i], emu_rdptr(sc, v->vnum, i)); 648 printf("%s", (x == 2)? "\n" : "\t"); 649 x++; 650 if (x > 2) 651 x = 0; 652 } 653 printf("\n\n"); 654 } 655 #endif 656 657 /* channel interface */ 658 void * 659 emupchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 660 { 661 struct sc_info *sc = devinfo; 662 struct sc_pchinfo *ch; 663 664 KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction")); 665 ch = &sc->pch[sc->pnum++]; 666 ch->buffer = b; 667 ch->parent = sc; 668 ch->channel = c; 669 ch->master = emu_valloc(sc); 670 ch->slave = emu_valloc(sc); 671 ch->tracker = emu_valloc(sc); 672 if (emu_vinit(sc, ch->master, ch->slave, ch->tracker, EMU_BUFFSIZE, ch->channel)) 673 return NULL; 674 else 675 return ch; 676 } 677 678 static int 679 emupchan_setdir(void *data, int dir) 680 { 681 return 0; 682 } 683 684 static int 685 emupchan_setformat(void *data, u_int32_t format) 686 { 687 struct sc_pchinfo *ch = data; 688 689 ch->fmt = format; 690 return 0; 691 } 692 693 static int 694 emupchan_setspeed(void *data, u_int32_t speed) 695 { 696 struct sc_pchinfo *ch = data; 697 698 ch->spd = speed; 699 return ch->spd; 700 } 701 702 static int 703 emupchan_setblocksize(void *data, u_int32_t blocksize) 704 { 705 return blocksize; 706 } 707 708 static int 709 emupchan_trigger(void *data, int go) 710 { 711 struct sc_pchinfo *ch = data; 712 struct sc_info *sc = ch->parent; 713 714 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) 715 return 0; 716 717 if (go == PCMTRIG_START) { 718 emu_vsetup(ch); 719 emu_vwrite(sc, ch->master); 720 #ifdef EMUDEBUG 721 printf("start [%d bit, %s, %d hz]\n", 722 ch->master->b16? 16 : 8, 723 ch->master->stereo? "stereo" : "mono", 724 ch->master->speed); 725 emu_vdump(sc, ch->master); 726 emu_vdump(sc, ch->slave); 727 #endif 728 } 729 ch->run = (go == PCMTRIG_START)? 1 : 0; 730 emu_vtrigger(sc, ch->master, ch->run); 731 return 0; 732 } 733 734 static int 735 emupchan_getptr(void *data) 736 { 737 struct sc_pchinfo *ch = data; 738 struct sc_info *sc = ch->parent; 739 740 return emu_vpos(sc, ch->master); 741 } 742 743 static pcmchan_caps * 744 emupchan_getcaps(void *data) 745 { 746 return &emu_playcaps; 747 } 748 749 /* channel interface */ 750 static void * 751 emurchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 752 { 753 struct sc_info *sc = devinfo; 754 struct sc_rchinfo *ch; 755 756 KASSERT(dir == PCMDIR_REC, ("emupchan_init: bad direction")); 757 ch = &sc->rch[sc->rnum]; 758 ch->buffer = b; 759 ch->buffer->bufsize = EMU_BUFFSIZE; 760 ch->parent = sc; 761 ch->channel = c; 762 ch->fmt = AFMT_U8; 763 ch->spd = 8000; 764 ch->num = sc->rnum; 765 switch(sc->rnum) { 766 case 0: 767 ch->idxreg = ADCIDX; 768 ch->basereg = ADCBA; 769 ch->sizereg = ADCBS; 770 ch->setupreg = ADCCR; 771 ch->irqmask = INTE_ADCBUFENABLE; 772 break; 773 774 case 1: 775 ch->idxreg = MICIDX; 776 ch->basereg = MICBA; 777 ch->sizereg = MICBS; 778 ch->setupreg = 0; 779 ch->irqmask = INTE_MICBUFENABLE; 780 break; 781 782 case 2: 783 ch->idxreg = FXIDX; 784 ch->basereg = FXBA; 785 ch->sizereg = FXBS; 786 ch->setupreg = FXWC; 787 ch->irqmask = INTE_EFXBUFENABLE; 788 break; 789 } 790 sc->rnum++; 791 if (chn_allocbuf(ch->buffer, sc->parent_dmat) == -1) 792 return NULL; 793 else { 794 emu_wrptr(sc, 0, ch->basereg, vtophys(ch->buffer->buf)); 795 emu_wrptr(sc, 0, ch->sizereg, 0); /* off */ 796 return ch; 797 } 798 } 799 800 static int 801 emurchan_setdir(void *data, int dir) 802 { 803 return 0; 804 } 805 806 static int 807 emurchan_setformat(void *data, u_int32_t format) 808 { 809 struct sc_rchinfo *ch = data; 810 811 ch->fmt = format; 812 return 0; 813 } 814 815 static int 816 emurchan_setspeed(void *data, u_int32_t speed) 817 { 818 struct sc_rchinfo *ch = data; 819 820 if (ch->num == 0) 821 speed = adcspeed[emu_recval(speed)]; 822 if (ch->num == 1) 823 speed = 8000; 824 if (ch->num == 2) 825 speed = 48000; 826 ch->spd = speed; 827 return ch->spd; 828 } 829 830 static int 831 emurchan_setblocksize(void *data, u_int32_t blocksize) 832 { 833 return blocksize; 834 } 835 836 /* semantic note: must start at beginning of buffer */ 837 static int 838 emurchan_trigger(void *data, int go) 839 { 840 struct sc_rchinfo *ch = data; 841 struct sc_info *sc = ch->parent; 842 u_int32_t val; 843 844 switch(go) { 845 case PCMTRIG_START: 846 ch->run = 1; 847 emu_wrptr(sc, 0, ch->sizereg, ADCBS_BUFSIZE_4096); 848 if (ch->num == 0) { 849 val = ADCCR_LCHANENABLE; 850 if (ch->fmt & AFMT_STEREO) 851 val |= ADCCR_RCHANENABLE; 852 val |= emu_recval(ch->spd); 853 emu_wrptr(sc, 0, ch->setupreg, val); 854 } 855 val = emu_rd(sc, INTE, 4); 856 val |= ch->irqmask; 857 emu_wr(sc, INTE, val, 4); 858 break; 859 860 case PCMTRIG_STOP: 861 case PCMTRIG_ABORT: 862 ch->run = 0; 863 emu_wrptr(sc, 0, ch->sizereg, 0); 864 if (ch->setupreg) 865 emu_wrptr(sc, 0, ch->setupreg, 0); 866 val = emu_rd(sc, INTE, 4); 867 val &= ~ch->irqmask; 868 emu_wr(sc, INTE, val, 4); 869 break; 870 871 case PCMTRIG_EMLDMAWR: 872 case PCMTRIG_EMLDMARD: 873 default: 874 break; 875 } 876 877 return 0; 878 } 879 880 static int 881 emurchan_getptr(void *data) 882 { 883 struct sc_rchinfo *ch = data; 884 struct sc_info *sc = ch->parent; 885 886 return emu_rdptr(sc, 0, ch->idxreg) & 0x0000ffff; 887 } 888 889 static pcmchan_caps * 890 emurchan_getcaps(void *data) 891 { 892 struct sc_rchinfo *ch = data; 893 894 return &emu_reccaps[ch->num]; 895 } 896 897 /* The interrupt handler */ 898 static void 899 emu_intr(void *p) 900 { 901 struct sc_info *sc = (struct sc_info *)p; 902 u_int32_t stat, ack, i; 903 904 while (1) { 905 stat = emu_rd(sc, IPR, 4); 906 if (stat == 0) 907 break; 908 ack = 0; 909 910 /* process irq */ 911 if (stat & IPR_CHANNELLOOP) { 912 ack |= IPR_CHANNELLOOP | (stat & IPR_CHANNELNUMBERMASK); 913 for (i = 0; i < 64; i++) { 914 if (emu_testint(sc, i)) { 915 if (sc->voice[i].channel) 916 chn_intr(sc->voice[i].channel); 917 else { 918 device_printf(sc->dev, "bad irq voice %d\n", i); 919 emu_enaint(sc, i, 0); 920 } 921 emu_clrint(sc, i); 922 } 923 } 924 } 925 926 if (stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL)) { 927 ack |= stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL); 928 if (sc->rch[0].channel) 929 chn_intr(sc->rch[0].channel); 930 } 931 if (stat & (IPR_MICBUFFULL | IPR_MICBUFHALFFULL)) { 932 ack |= stat & (IPR_MICBUFFULL | IPR_MICBUFHALFFULL); 933 if (sc->rch[1].channel) 934 chn_intr(sc->rch[1].channel); 935 } 936 if (stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL)) { 937 ack |= stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL); 938 if (sc->rch[2].channel) 939 chn_intr(sc->rch[2].channel); 940 } 941 if (stat & IPR_PCIERROR) { 942 ack |= IPR_PCIERROR; 943 device_printf(sc->dev, "pci error\n"); 944 } 945 946 if (stat & ~ack) 947 device_printf(sc->dev, "dodgy irq: %x\n", stat & ~ack); 948 949 emu_wr(sc, IPR, stat, 4); 950 } 951 } 952 953 /* -------------------------------------------------------------------- */ 954 955 static void 956 emu_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) 957 { 958 void **phys = arg; 959 960 *phys = error? 0 : (void *)segs->ds_addr; 961 962 if (bootverbose) { 963 printf("emu: setmap (%lx, %lx), nseg=%d, error=%d\n", 964 (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len, 965 nseg, error); 966 } 967 } 968 969 static void * 970 emu_malloc(struct sc_info *sc, u_int32_t sz) 971 { 972 void *buf, *phys = 0; 973 bus_dmamap_t map; 974 975 if (bus_dmamem_alloc(sc->parent_dmat, &buf, BUS_DMA_NOWAIT, &map)) 976 return NULL; 977 if (bus_dmamap_load(sc->parent_dmat, map, buf, sz, emu_setmap, &phys, 0) 978 || !phys) 979 return NULL; 980 return buf; 981 } 982 983 static void 984 emu_free(struct sc_info *sc, void *buf) 985 { 986 bus_dmamem_free(sc->parent_dmat, buf, NULL); 987 } 988 989 static void * 990 emu_memalloc(struct sc_info *sc, u_int32_t sz) 991 { 992 u_int32_t blksz, start, idx, ofs, tmp, found; 993 struct emu_mem *mem = &sc->mem; 994 struct emu_memblk *blk; 995 void *buf; 996 997 blksz = sz / EMUPAGESIZE; 998 if (sz > (blksz * EMUPAGESIZE)) 999 blksz++; 1000 /* find a free block in the bitmap */ 1001 found = 0; 1002 start = 0; 1003 while (!found && start + blksz < MAXPAGES) { 1004 found = 1; 1005 for (idx = start; idx < start + blksz; idx++) 1006 if (mem->bmap[idx >> 3] & (1 << (idx & 7))) 1007 found = 0; 1008 if (!found) 1009 start++; 1010 } 1011 if (!found) 1012 return NULL; 1013 blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT); 1014 if (blk == NULL) 1015 return NULL; 1016 buf = emu_malloc(sc, sz); 1017 if (buf == NULL) { 1018 free(blk, M_DEVBUF); 1019 return NULL; 1020 } 1021 blk->buf = buf; 1022 blk->pte_start = start; 1023 blk->pte_size = blksz; 1024 /* printf("buf %p, pte_start %d, pte_size %d\n", blk->buf, blk->pte_start, blk->pte_size); */ 1025 ofs = 0; 1026 for (idx = start; idx < start + blksz; idx++) { 1027 mem->bmap[idx >> 3] |= 1 << (idx & 7); 1028 tmp = (u_int32_t)vtophys((u_int8_t *)buf + ofs); 1029 /* printf("pte[%d] -> %x phys, %x virt\n", idx, tmp, ((u_int32_t)buf) + ofs); */ 1030 mem->ptb_pages[idx] = (tmp << 1) | idx; 1031 ofs += EMUPAGESIZE; 1032 } 1033 SLIST_INSERT_HEAD(&mem->blocks, blk, link); 1034 return buf; 1035 } 1036 1037 #ifdef notyet 1038 static int 1039 emu_memfree(struct sc_info *sc, void *buf) 1040 { 1041 u_int32_t idx, tmp; 1042 struct emu_mem *mem = &sc->mem; 1043 struct emu_memblk *blk, *i; 1044 1045 blk = NULL; 1046 SLIST_FOREACH(i, &mem->blocks, link) { 1047 if (i->buf == buf) 1048 blk = i; 1049 } 1050 if (blk == NULL) 1051 return EINVAL; 1052 SLIST_REMOVE(&mem->blocks, blk, emu_memblk, link); 1053 emu_free(sc, buf); 1054 tmp = (u_int32_t)vtophys(sc->mem.silent_page) << 1; 1055 for (idx = blk->pte_start; idx < blk->pte_start + blk->pte_size; idx++) { 1056 mem->bmap[idx >> 3] &= ~(1 << (idx & 7)); 1057 mem->ptb_pages[idx] = tmp | idx; 1058 } 1059 free(blk, M_DEVBUF); 1060 return 0; 1061 } 1062 #endif 1063 1064 static int 1065 emu_memstart(struct sc_info *sc, void *buf) 1066 { 1067 struct emu_mem *mem = &sc->mem; 1068 struct emu_memblk *blk, *i; 1069 1070 blk = NULL; 1071 SLIST_FOREACH(i, &mem->blocks, link) { 1072 if (i->buf == buf) 1073 blk = i; 1074 } 1075 if (blk == NULL) 1076 return -EINVAL; 1077 return blk->pte_start; 1078 } 1079 1080 static void 1081 emu_addefxop(struct sc_info *sc, int op, int z, int w, int x, int y, u_int32_t *pc) 1082 { 1083 emu_wrefx(sc, (*pc) * 2, (x << 10) | y); 1084 emu_wrefx(sc, (*pc) * 2 + 1, (op << 20) | (z << 10) | w); 1085 (*pc)++; 1086 } 1087 1088 static void 1089 emu_initefx(struct sc_info *sc) 1090 { 1091 int i; 1092 u_int32_t pc = 16; 1093 1094 for (i = 0; i < 512; i++) { 1095 emu_wrefx(sc, i * 2, 0x10040); 1096 emu_wrefx(sc, i * 2 + 1, 0x610040); 1097 } 1098 1099 for (i = 0; i < 256; i++) 1100 emu_wrptr(sc, 0, FXGPREGBASE + i, 0); 1101 1102 /* FX-8010 DSP Registers: 1103 FX Bus 1104 0x000-0x00f : 16 registers 1105 Input 1106 0x010/0x011 : AC97 Codec (l/r) 1107 0x012/0x013 : ADC, S/PDIF (l/r) 1108 0x014/0x015 : Mic(left), Zoom (l/r) 1109 0x016/0x017 : APS S/PDIF?? (l/r) 1110 Output 1111 0x020/0x021 : AC97 Output (l/r) 1112 0x022/0x023 : TOS link out (l/r) 1113 0x024/0x025 : ??? (l/r) 1114 0x026/0x027 : LiveDrive Headphone (l/r) 1115 0x028/0x029 : Rear Channel (l/r) 1116 0x02a/0x02b : ADC Recording Buffer (l/r) 1117 Constants 1118 0x040 - 0x044 = 0 - 4 1119 0x045 = 0x8, 0x046 = 0x10, 0x047 = 0x20 1120 0x048 = 0x100, 0x049 = 0x10000, 0x04a = 0x80000 1121 0x04b = 0x10000000, 0x04c = 0x20000000, 0x04d = 0x40000000 1122 0x04e = 0x80000000, 0x04f = 0x7fffffff 1123 Temporary Values 1124 0x056 : Accumulator 1125 0x058 : Noise source? 1126 0x059 : Noise source? 1127 General Purpose Registers 1128 0x100 - 0x1ff 1129 Tank Memory Data Registers 1130 0x200 - 0x2ff 1131 Tank Memory Address Registers 1132 0x300 - 0x3ff 1133 */ 1134 1135 /* Operators: 1136 0 : z := w + (x * y >> 31) 1137 4 : z := w + x * y 1138 6 : z := w + x + y 1139 */ 1140 1141 /* Routing - this will be configurable in later version */ 1142 1143 /* GPR[0/1] = FX * 4 + SPDIF-in */ 1144 emu_addefxop(sc, 4, 0x100, 0x12, 0, 0x44, &pc); 1145 emu_addefxop(sc, 4, 0x101, 0x13, 1, 0x44, &pc); 1146 /* GPR[0/1] += APS-input */ 1147 emu_addefxop(sc, 6, 0x100, 0x100, 0x40, sc->APS ? 0x16 : 0x40, &pc); 1148 emu_addefxop(sc, 6, 0x101, 0x101, 0x40, sc->APS ? 0x17 : 0x40, &pc); 1149 /* FrontOut (AC97) = GPR[0/1] */ 1150 emu_addefxop(sc, 6, 0x20, 0x40, 0x40, 0x100, &pc); 1151 emu_addefxop(sc, 6, 0x21, 0x40, 0x41, 0x101, &pc); 1152 /* RearOut = (GPR[0/1] * RearVolume) >> 31 */ 1153 /* RearVolume = GRP[0x10/0x11] */ 1154 emu_addefxop(sc, 0, 0x28, 0x40, 0x110, 0x100, &pc); 1155 emu_addefxop(sc, 0, 0x29, 0x40, 0x111, 0x101, &pc); 1156 /* TOS out = GPR[0/1] */ 1157 emu_addefxop(sc, 6, 0x22, 0x40, 0x40, 0x100, &pc); 1158 emu_addefxop(sc, 6, 0x23, 0x40, 0x40, 0x101, &pc); 1159 /* Mute Out2 */ 1160 emu_addefxop(sc, 6, 0x24, 0x40, 0x40, 0x40, &pc); 1161 emu_addefxop(sc, 6, 0x25, 0x40, 0x40, 0x40, &pc); 1162 /* Mute Out3 */ 1163 emu_addefxop(sc, 6, 0x26, 0x40, 0x40, 0x40, &pc); 1164 emu_addefxop(sc, 6, 0x27, 0x40, 0x40, 0x40, &pc); 1165 /* Input0 (AC97) -> Record */ 1166 emu_addefxop(sc, 6, 0x2a, 0x40, 0x40, 0x10, &pc); 1167 emu_addefxop(sc, 6, 0x2b, 0x40, 0x40, 0x11, &pc); 1168 1169 emu_wrptr(sc, 0, DBG, 0); 1170 } 1171 1172 /* Probe and attach the card */ 1173 static int 1174 emu_init(struct sc_info *sc) 1175 { 1176 u_int32_t spcs, ch, tmp, i; 1177 1178 /* disable audio and lock cache */ 1179 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE | HCFG_MUTEBUTTONENABLE, 4); 1180 1181 /* reset recording buffers */ 1182 emu_wrptr(sc, 0, MICBS, 0); 1183 emu_wrptr(sc, 0, MICBA, 0); 1184 emu_wrptr(sc, 0, FXBS, 0); 1185 emu_wrptr(sc, 0, FXBA, 0); 1186 emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE); 1187 emu_wrptr(sc, 0, ADCBA, 0); 1188 1189 /* disable channel interrupt */ 1190 emu_wr(sc, INTE, INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE, 4); 1191 emu_wrptr(sc, 0, CLIEL, 0); 1192 emu_wrptr(sc, 0, CLIEH, 0); 1193 emu_wrptr(sc, 0, SOLEL, 0); 1194 emu_wrptr(sc, 0, SOLEH, 0); 1195 1196 /* init envelope engine */ 1197 for (ch = 0; ch < NUM_G; ch++) { 1198 emu_wrptr(sc, ch, DCYSUSV, ENV_OFF); 1199 emu_wrptr(sc, ch, IP, 0); 1200 emu_wrptr(sc, ch, VTFT, 0xffff); 1201 emu_wrptr(sc, ch, CVCF, 0xffff); 1202 emu_wrptr(sc, ch, PTRX, 0); 1203 emu_wrptr(sc, ch, CPF, 0); 1204 emu_wrptr(sc, ch, CCR, 0); 1205 1206 emu_wrptr(sc, ch, PSST, 0); 1207 emu_wrptr(sc, ch, DSL, 0x10); 1208 emu_wrptr(sc, ch, CCCA, 0); 1209 emu_wrptr(sc, ch, Z1, 0); 1210 emu_wrptr(sc, ch, Z2, 0); 1211 emu_wrptr(sc, ch, FXRT, 0xd01c0000); 1212 1213 emu_wrptr(sc, ch, ATKHLDM, 0); 1214 emu_wrptr(sc, ch, DCYSUSM, 0); 1215 emu_wrptr(sc, ch, IFATN, 0xffff); 1216 emu_wrptr(sc, ch, PEFE, 0); 1217 emu_wrptr(sc, ch, FMMOD, 0); 1218 emu_wrptr(sc, ch, TREMFRQ, 24); /* 1 Hz */ 1219 emu_wrptr(sc, ch, FM2FRQ2, 24); /* 1 Hz */ 1220 emu_wrptr(sc, ch, TEMPENV, 0); 1221 1222 /*** these are last so OFF prevents writing ***/ 1223 emu_wrptr(sc, ch, LFOVAL2, 0); 1224 emu_wrptr(sc, ch, LFOVAL1, 0); 1225 emu_wrptr(sc, ch, ATKHLDV, 0); 1226 emu_wrptr(sc, ch, ENVVOL, 0); 1227 emu_wrptr(sc, ch, ENVVAL, 0); 1228 1229 sc->voice[ch].vnum = ch; 1230 sc->voice[ch].slave = NULL; 1231 sc->voice[ch].tracker = NULL; 1232 sc->voice[ch].busy = 0; 1233 sc->voice[ch].ismaster = 0; 1234 sc->voice[ch].istracker = 0; 1235 sc->voice[ch].b16 = 0; 1236 sc->voice[ch].stereo = 0; 1237 sc->voice[ch].speed = 0; 1238 sc->voice[ch].start = 0; 1239 sc->voice[ch].end = 0; 1240 sc->voice[ch].channel = NULL; 1241 } 1242 sc->pnum = sc->rnum = 0; 1243 1244 /* 1245 * Init to 0x02109204 : 1246 * Clock accuracy = 0 (1000ppm) 1247 * Sample Rate = 2 (48kHz) 1248 * Audio Channel = 1 (Left of 2) 1249 * Source Number = 0 (Unspecified) 1250 * Generation Status = 1 (Original for Cat Code 12) 1251 * Cat Code = 12 (Digital Signal Mixer) 1252 * Mode = 0 (Mode 0) 1253 * Emphasis = 0 (None) 1254 * CP = 1 (Copyright unasserted) 1255 * AN = 0 (Audio data) 1256 * P = 0 (Consumer) 1257 */ 1258 spcs = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 | 1259 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | 1260 SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 | 1261 SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT; 1262 emu_wrptr(sc, 0, SPCS0, spcs); 1263 emu_wrptr(sc, 0, SPCS1, spcs); 1264 emu_wrptr(sc, 0, SPCS2, spcs); 1265 1266 emu_initefx(sc); 1267 1268 SLIST_INIT(&sc->mem.blocks); 1269 sc->mem.ptb_pages = emu_malloc(sc, MAXPAGES * sizeof(u_int32_t)); 1270 if (sc->mem.ptb_pages == NULL) 1271 return -1; 1272 1273 sc->mem.silent_page = emu_malloc(sc, EMUPAGESIZE); 1274 if (sc->mem.silent_page == NULL) { 1275 emu_free(sc, sc->mem.ptb_pages); 1276 return -1; 1277 } 1278 /* Clear page with silence & setup all pointers to this page */ 1279 bzero(sc->mem.silent_page, EMUPAGESIZE); 1280 tmp = (u_int32_t)vtophys(sc->mem.silent_page) << 1; 1281 for (i = 0; i < MAXPAGES; i++) 1282 sc->mem.ptb_pages[i] = tmp | i; 1283 1284 emu_wrptr(sc, 0, PTB, vtophys(sc->mem.ptb_pages)); 1285 emu_wrptr(sc, 0, TCB, 0); /* taken from original driver */ 1286 emu_wrptr(sc, 0, TCBS, 4); /* taken from original driver */ 1287 1288 for (ch = 0; ch < NUM_G; ch++) { 1289 emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK); 1290 emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK); 1291 } 1292 1293 /* emu_memalloc(sc, EMUPAGESIZE); */ 1294 /* 1295 * Hokay, now enable the AUD bit 1296 * Enable Audio = 1 1297 * Mute Disable Audio = 0 1298 * Lock Tank Memory = 1 1299 * Lock Sound Memory = 0 1300 * Auto Mute = 1 1301 */ 1302 tmp = HCFG_AUDIOENABLE | HCFG_LOCKTANKCACHE | HCFG_AUTOMUTE; 1303 if (sc->rev >= 6) 1304 tmp |= HCFG_JOYENABLE; 1305 emu_wr(sc, HCFG, tmp, 4); 1306 1307 /* TOSLink detection */ 1308 sc->tos_link = 0; 1309 tmp = emu_rd(sc, HCFG, 4); 1310 if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) { 1311 emu_wr(sc, HCFG, tmp | 0x800, 4); 1312 DELAY(50); 1313 if (tmp != (emu_rd(sc, HCFG, 4) & ~0x800)) { 1314 sc->tos_link = 1; 1315 emu_wr(sc, HCFG, tmp, 4); 1316 } 1317 } 1318 1319 return 0; 1320 } 1321 1322 static int 1323 emu_pci_probe(device_t dev) 1324 { 1325 char *s = NULL; 1326 1327 switch (pci_get_devid(dev)) { 1328 case EMU10K1_PCI_ID: 1329 s = "Creative EMU10K1"; 1330 break; 1331 } 1332 1333 if (s) device_set_desc(dev, s); 1334 return s? 0 : ENXIO; 1335 } 1336 1337 static int 1338 emu_pci_attach(device_t dev) 1339 { 1340 snddev_info *d; 1341 u_int32_t data; 1342 struct sc_info *sc; 1343 struct ac97_info *codec; 1344 int i, mapped; 1345 char status[SND_STATUSLEN]; 1346 1347 d = device_get_softc(dev); 1348 if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT)) == NULL) { 1349 device_printf(dev, "cannot allocate softc\n"); 1350 return ENXIO; 1351 } 1352 1353 bzero(sc, sizeof(*sc)); 1354 sc->dev = dev; 1355 sc->type = pci_get_devid(dev); 1356 sc->rev = pci_get_revid(dev); 1357 1358 data = pci_read_config(dev, PCIR_COMMAND, 2); 1359 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 1360 pci_write_config(dev, PCIR_COMMAND, data, 2); 1361 data = pci_read_config(dev, PCIR_COMMAND, 2); 1362 1363 mapped = 0; 1364 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) { 1365 sc->regid = PCIR_MAPS + i*4; 1366 sc->regtype = SYS_RES_MEMORY; 1367 sc->reg = bus_alloc_resource(dev, sc->regtype, &sc->regid, 1368 0, ~0, 1, RF_ACTIVE); 1369 if (!sc->reg) { 1370 sc->regtype = SYS_RES_IOPORT; 1371 sc->reg = bus_alloc_resource(dev, sc->regtype, 1372 &sc->regid, 0, ~0, 1, 1373 RF_ACTIVE); 1374 } 1375 if (sc->reg) { 1376 sc->st = rman_get_bustag(sc->reg); 1377 sc->sh = rman_get_bushandle(sc->reg); 1378 mapped++; 1379 } 1380 } 1381 1382 if (mapped == 0) { 1383 device_printf(dev, "unable to map register space\n"); 1384 goto bad; 1385 } 1386 1387 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 1388 /*lowaddr*/1 << 31, /* can only access 0-2gb */ 1389 /*highaddr*/BUS_SPACE_MAXADDR, 1390 /*filter*/NULL, /*filterarg*/NULL, 1391 /*maxsize*/262144, /*nsegments*/1, /*maxsegz*/0x3ffff, 1392 /*flags*/0, &sc->parent_dmat) != 0) { 1393 device_printf(dev, "unable to create dma tag\n"); 1394 goto bad; 1395 } 1396 1397 if (emu_init(sc) == -1) { 1398 device_printf(dev, "unable to initialize the card\n"); 1399 goto bad; 1400 } 1401 1402 codec = ac97_create(dev, sc, NULL, emu_rdcd, emu_wrcd); 1403 if (codec == NULL) goto bad; 1404 if (mixer_init(d, &ac97_mixer, codec) == -1) goto bad; 1405 1406 sc->irqid = 0; 1407 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid, 1408 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 1409 if (!sc->irq || 1410 bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY, emu_intr, sc, &sc->ih)) { 1411 device_printf(dev, "unable to map interrupt\n"); 1412 goto bad; 1413 } 1414 1415 snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld", 1416 (sc->regtype == SYS_RES_IOPORT)? "io" : "memory", 1417 rman_get_start(sc->reg), rman_get_start(sc->irq)); 1418 1419 if (pcm_register(dev, sc, EMU_CHANS, 3)) goto bad; 1420 for (i = 0; i < EMU_CHANS; i++) 1421 pcm_addchan(dev, PCMDIR_PLAY, &emu_chantemplate, sc); 1422 for (i = 0; i < 3; i++) 1423 pcm_addchan(dev, PCMDIR_REC, &emur_chantemplate, sc); 1424 1425 pcm_setstatus(dev, status); 1426 1427 return 0; 1428 1429 bad: 1430 if (sc->reg) bus_release_resource(dev, sc->regtype, sc->regid, sc->reg); 1431 if (sc->ih) bus_teardown_intr(dev, sc->irq, sc->ih); 1432 if (sc->irq) bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq); 1433 free(sc, M_DEVBUF); 1434 return ENXIO; 1435 } 1436 1437 static device_method_t emu_methods[] = { 1438 /* Device interface */ 1439 DEVMETHOD(device_probe, emu_pci_probe), 1440 DEVMETHOD(device_attach, emu_pci_attach), 1441 1442 { 0, 0 } 1443 }; 1444 1445 static driver_t emu_driver = { 1446 "pcm", 1447 emu_methods, 1448 sizeof(snddev_info), 1449 }; 1450 1451 static devclass_t pcm_devclass; 1452 1453 DRIVER_MODULE(snd_emu10k1, pci, emu_driver, pcm_devclass, 0, 0); 1454 MODULE_DEPEND(snd_emu10k1, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER); 1455 MODULE_VERSION(snd_emu10k1, 1); 1456 1457 static int 1458 emujoy_pci_probe(device_t dev) 1459 { 1460 char *s = NULL; 1461 1462 switch (pci_get_devid(dev)) { 1463 case 0x70021102: 1464 s = "Creative EMU10K1 Joystick"; 1465 device_quiet(dev); 1466 break; 1467 } 1468 1469 if (s) device_set_desc(dev, s); 1470 return s? 0 : ENXIO; 1471 } 1472 1473 static int 1474 emujoy_pci_attach(device_t dev) 1475 { 1476 return 0; 1477 } 1478 1479 static device_method_t emujoy_methods[] = { 1480 DEVMETHOD(device_probe, emujoy_pci_probe), 1481 DEVMETHOD(device_attach, emujoy_pci_attach), 1482 1483 { 0, 0 } 1484 }; 1485 1486 static driver_t emujoy_driver = { 1487 "emujoy", 1488 emujoy_methods, 1489 8, 1490 }; 1491 1492 static devclass_t emujoy_devclass; 1493 1494 DRIVER_MODULE(emujoy, pci, emujoy_driver, emujoy_devclass, 0, 0); 1495 1496