1 /* $NetBSD: hdaudio.c,v 1.4 2015/12/23 12:45:06 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk> 5 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca> 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Precedence Technologies Ltd 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: hdaudio.c,v 1.4 2015/12/23 12:45:06 jmcneill Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/device.h> 39 #include <sys/conf.h> 40 #include <sys/bus.h> 41 #include <sys/kmem.h> 42 #include <sys/module.h> 43 44 #include "hdaudiovar.h" 45 #include "hdaudioreg.h" 46 #include "hdaudioio.h" 47 #include "hdaudio_verbose.h" 48 49 /* #define HDAUDIO_DEBUG */ 50 51 #define HDAUDIO_RESET_TIMEOUT 5000 52 #define HDAUDIO_CORB_TIMEOUT 1000 53 #define HDAUDIO_RIRB_TIMEOUT 5000 54 55 #define HDAUDIO_CODEC_DELAY 1000 /* spec calls for 250 */ 56 57 dev_type_open(hdaudioopen); 58 dev_type_close(hdaudioclose); 59 dev_type_ioctl(hdaudioioctl); 60 61 const struct cdevsw hdaudio_cdevsw = { 62 .d_open = hdaudioopen, 63 .d_close = hdaudioclose, 64 .d_read = noread, 65 .d_write = nowrite, 66 .d_ioctl = hdaudioioctl, 67 .d_stop = nostop, 68 .d_tty = notty, 69 .d_poll = nopoll, 70 .d_mmap = nommap, 71 .d_kqfilter = nokqfilter, 72 .d_discard = nodiscard, 73 .d_flag = D_OTHER 74 }; 75 76 extern struct cfdriver hdaudio_cd; 77 78 #define HDAUDIOUNIT(x) minor((x)) 79 80 static void 81 hdaudio_stream_init(struct hdaudio_softc *sc, int nis, int nos, int nbidir) 82 { 83 int i, cnt = 0; 84 85 for (i = 0; i < nis && cnt < HDAUDIO_MAX_STREAMS; i++) { 86 sc->sc_stream[cnt].st_host = sc; 87 sc->sc_stream[cnt].st_enable = true; 88 sc->sc_stream[cnt].st_shift = cnt; 89 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_ISS; 90 } 91 for (i = 0; i < nos && cnt < HDAUDIO_MAX_STREAMS; i++) { 92 sc->sc_stream[cnt].st_host = sc; 93 sc->sc_stream[cnt].st_enable = true; 94 sc->sc_stream[cnt].st_shift = cnt; 95 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_OSS; 96 } 97 for (i = 0; i < nbidir && cnt < HDAUDIO_MAX_STREAMS; i++) { 98 sc->sc_stream[cnt].st_host = sc; 99 sc->sc_stream[cnt].st_enable = true; 100 sc->sc_stream[cnt].st_shift = cnt; 101 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_BSS; 102 } 103 104 for (i = 0; i < cnt; i++) 105 hdaudio_stream_stop(&sc->sc_stream[i]); 106 107 sc->sc_stream_mask = 0; 108 } 109 110 static void 111 hdaudio_codec_init(struct hdaudio_softc *sc) 112 { 113 int i; 114 115 for (i = 0; i < HDAUDIO_MAX_CODECS; i++) { 116 sc->sc_codec[i].co_addr = i; 117 sc->sc_codec[i].co_host = sc; 118 } 119 } 120 121 static void 122 hdaudio_init(struct hdaudio_softc *sc) 123 { 124 uint16_t gcap; 125 int nos, nis, nbidir; 126 #if defined(HDAUDIO_DEBUG) 127 uint8_t vmin, vmaj; 128 int nsdo, addr64; 129 #endif 130 131 #if defined(HDAUDIO_DEBUG) 132 vmaj = hda_read1(sc, HDAUDIO_MMIO_VMAJ); 133 vmin = hda_read1(sc, HDAUDIO_MMIO_VMIN); 134 135 hda_print(sc, "High Definition Audio version %d.%d\n", vmaj, vmin); 136 #endif 137 138 gcap = hda_read2(sc, HDAUDIO_MMIO_GCAP); 139 nis = HDAUDIO_GCAP_ISS(gcap); 140 nos = HDAUDIO_GCAP_OSS(gcap); 141 nbidir = HDAUDIO_GCAP_BSS(gcap); 142 143 /* Initialize codecs and streams */ 144 hdaudio_codec_init(sc); 145 hdaudio_stream_init(sc, nis, nos, nbidir); 146 147 #if defined(HDAUDIO_DEBUG) 148 nsdo = HDAUDIO_GCAP_NSDO(gcap); 149 addr64 = HDAUDIO_GCAP_64OK(gcap); 150 151 hda_print(sc, "OSS %d ISS %d BSS %d SDO %d%s\n", 152 nos, nis, nbidir, nsdo, addr64 ? " 64-bit" : ""); 153 #endif 154 } 155 156 static int 157 hdaudio_codec_probe(struct hdaudio_softc *sc) 158 { 159 uint16_t statests; 160 int codecid; 161 162 statests = hda_read2(sc, HDAUDIO_MMIO_STATESTS); 163 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) 164 if (statests & (1 << codecid)) 165 sc->sc_codec[codecid].co_valid = true; 166 hda_write2(sc, HDAUDIO_MMIO_STATESTS, statests); 167 168 return statests; 169 } 170 171 int 172 hdaudio_dma_alloc(struct hdaudio_softc *sc, struct hdaudio_dma *dma, 173 int flags) 174 { 175 int err; 176 177 KASSERT(dma->dma_size > 0); 178 179 err = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, 128, 0, 180 dma->dma_segs, sizeof(dma->dma_segs) / sizeof(dma->dma_segs[0]), 181 &dma->dma_nsegs, BUS_DMA_WAITOK); 182 if (err) 183 return err; 184 err = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs, 185 dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | flags); 186 if (err) 187 goto free; 188 err = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs, 189 dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map); 190 if (err) 191 goto unmap; 192 err = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr, 193 dma->dma_size, NULL, BUS_DMA_WAITOK | flags); 194 if (err) 195 goto destroy; 196 197 dma->dma_valid = true; 198 return 0; 199 200 destroy: 201 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 202 unmap: 203 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 204 free: 205 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 206 207 dma->dma_valid = false; 208 return err; 209 } 210 211 void 212 hdaudio_dma_free(struct hdaudio_softc *sc, struct hdaudio_dma *dma) 213 { 214 if (dma->dma_valid == false) 215 return; 216 bus_dmamap_unload(sc->sc_dmat, dma->dma_map); 217 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 218 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 219 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 220 dma->dma_valid = false; 221 } 222 223 static void 224 hdaudio_corb_enqueue(struct hdaudio_softc *sc, int addr, int nid, 225 uint32_t control, uint32_t param) 226 { 227 uint32_t *corb = DMA_KERNADDR(&sc->sc_corb); 228 uint32_t verb; 229 uint16_t corbrp; 230 int wp; 231 232 /* Build command */ 233 verb = (addr << 28) | (nid << 20) | (control << 8) | param; 234 235 /* Fetch and update write pointer */ 236 corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBWP); 237 wp = (corbrp & 0xff) + 1; 238 if (wp >= (sc->sc_corb.dma_size / sizeof(*corb))) 239 wp = 0; 240 241 /* Enqueue command */ 242 bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0, 243 sc->sc_corb.dma_size, BUS_DMASYNC_POSTWRITE); 244 corb[wp] = verb; 245 bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0, 246 sc->sc_corb.dma_size, BUS_DMASYNC_PREWRITE); 247 248 /* Commit updated write pointer */ 249 hda_write2(sc, HDAUDIO_MMIO_CORBWP, wp); 250 } 251 252 static void 253 hdaudio_rirb_unsol(struct hdaudio_softc *sc, struct rirb_entry *entry) 254 { 255 struct hdaudio_codec *co; 256 struct hdaudio_function_group *fg; 257 uint8_t codecid = RIRB_CODEC_ID(entry); 258 unsigned int i; 259 260 if (codecid >= HDAUDIO_MAX_CODECS) { 261 hda_error(sc, "unsol: codec id 0x%02x out of range\n", codecid); 262 return; 263 } 264 co = &sc->sc_codec[codecid]; 265 if (sc->sc_codec[codecid].co_valid == false) { 266 hda_error(sc, "unsol: codec id 0x%02x not valid\n", codecid); 267 return; 268 } 269 270 for (i = 0; i < co->co_nfg; i++) { 271 fg = &co->co_fg[i]; 272 if (fg->fg_device && fg->fg_unsol) 273 fg->fg_unsol(fg->fg_device, entry->resp); 274 } 275 } 276 277 static uint32_t 278 hdaudio_rirb_dequeue(struct hdaudio_softc *sc, bool unsol) 279 { 280 uint16_t rirbwp; 281 uint64_t *rirb = DMA_KERNADDR(&sc->sc_rirb); 282 struct rirb_entry entry; 283 int retry; 284 285 for (;;) { 286 retry = HDAUDIO_RIRB_TIMEOUT; 287 288 rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP); 289 while (--retry > 0 && (rirbwp & 0xff) == sc->sc_rirbrp) { 290 if (unsol) { 291 /* don't wait for more unsol events */ 292 hda_trace(sc, "unsol: rirb empty\n"); 293 return 0xffffffff; 294 } 295 hda_delay(10); 296 rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP); 297 } 298 if (retry == 0) { 299 hda_error(sc, "RIRB timeout\n"); 300 return 0xffffffff; 301 } 302 303 sc->sc_rirbrp++; 304 if (sc->sc_rirbrp >= (sc->sc_rirb.dma_size / sizeof(*rirb))) 305 sc->sc_rirbrp = 0; 306 307 bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0, 308 sc->sc_rirb.dma_size, BUS_DMASYNC_POSTREAD); 309 entry = *(struct rirb_entry *)&rirb[sc->sc_rirbrp]; 310 bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0, 311 sc->sc_rirb.dma_size, BUS_DMASYNC_PREREAD); 312 313 hda_trace(sc, "%s: response %08X %08X\n", 314 unsol ? "unsol" : "cmd ", 315 entry.resp, entry.resp_ex); 316 317 if (RIRB_UNSOL(&entry)) { 318 hdaudio_rirb_unsol(sc, &entry); 319 continue; 320 } 321 322 return entry.resp; 323 } 324 } 325 326 uint32_t 327 hdaudio_command(struct hdaudio_codec *co, int nid, uint32_t control, 328 uint32_t param) 329 { 330 uint32_t result; 331 struct hdaudio_softc *sc = co->co_host; 332 mutex_enter(&sc->sc_corb_mtx); 333 result = hdaudio_command_unlocked(co, nid, control, param); 334 mutex_exit(&sc->sc_corb_mtx); 335 return result; 336 } 337 338 uint32_t 339 hdaudio_command_unlocked(struct hdaudio_codec *co, int nid, uint32_t control, 340 uint32_t param) 341 { 342 struct hdaudio_softc *sc = co->co_host; 343 uint32_t result; 344 345 hda_trace(sc, "cmd : request %08X %08X (%02X)\n", 346 control, param, nid); 347 hdaudio_corb_enqueue(sc, co->co_addr, nid, control, param); 348 result = hdaudio_rirb_dequeue(sc, false); 349 350 return result; 351 } 352 353 static int 354 hdaudio_corb_setsize(struct hdaudio_softc *sc) 355 { 356 uint8_t corbsize; 357 bus_size_t bufsize = 0; 358 359 /* 360 * The size of the CORB is programmable to 2, 16, or 256 entries 361 * by using the CORBSIZE register. Choose a size based on the 362 * controller capabilities, preferring a larger size when possible. 363 */ 364 corbsize = hda_read1(sc, HDAUDIO_MMIO_CORBSIZE); 365 corbsize &= ~0x3; 366 if ((corbsize >> 4) & 0x4) { 367 corbsize |= 0x2; 368 bufsize = 1024; 369 } else if ((corbsize >> 4) & 0x2) { 370 corbsize |= 0x1; 371 bufsize = 64; 372 } else if ((corbsize >> 4) & 0x1) { 373 corbsize |= 0x0; 374 bufsize = 8; 375 } else { 376 hda_error(sc, "couldn't configure CORB size\n"); 377 return ENXIO; 378 } 379 380 #if defined(HDAUDIO_DEBUG) 381 hda_print(sc, "using %d byte CORB (cap %X)\n", 382 (int)bufsize, corbsize >> 4); 383 #endif 384 385 sc->sc_corb.dma_size = bufsize; 386 sc->sc_corb.dma_sizereg = corbsize; 387 388 return 0; 389 } 390 391 static int 392 hdaudio_corb_config(struct hdaudio_softc *sc) 393 { 394 uint32_t corbubase, corblbase; 395 uint16_t corbrp; 396 int retry = HDAUDIO_CORB_TIMEOUT; 397 398 /* Program command buffer base address and size */ 399 corblbase = (uint32_t)DMA_DMAADDR(&sc->sc_corb); 400 corbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_corb)) >> 32); 401 hda_write4(sc, HDAUDIO_MMIO_CORBLBASE, corblbase); 402 hda_write4(sc, HDAUDIO_MMIO_CORBUBASE, corbubase); 403 hda_write1(sc, HDAUDIO_MMIO_CORBSIZE, sc->sc_corb.dma_sizereg); 404 405 /* Clear the read and write pointers */ 406 hda_write2(sc, HDAUDIO_MMIO_CORBRP, HDAUDIO_CORBRP_RP_RESET); 407 hda_write2(sc, HDAUDIO_MMIO_CORBRP, 0); 408 do { 409 hda_delay(10); 410 corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBRP); 411 } while (--retry > 0 && (corbrp & HDAUDIO_CORBRP_RP_RESET) != 0); 412 if (retry == 0) { 413 hda_error(sc, "timeout resetting CORB\n"); 414 return ETIME; 415 } 416 hda_write2(sc, HDAUDIO_MMIO_CORBWP, 0); 417 418 return 0; 419 } 420 421 static int 422 hdaudio_corb_stop(struct hdaudio_softc *sc) 423 { 424 uint8_t corbctl; 425 int retry = HDAUDIO_CORB_TIMEOUT; 426 427 /* Stop the CORB if necessary */ 428 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL); 429 if (corbctl & HDAUDIO_CORBCTL_RUN) { 430 corbctl &= ~HDAUDIO_CORBCTL_RUN; 431 hda_write4(sc, HDAUDIO_MMIO_CORBCTL, corbctl); 432 do { 433 hda_delay(10); 434 corbctl = hda_read4(sc, HDAUDIO_MMIO_CORBCTL); 435 } while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) != 0); 436 if (retry == 0) { 437 hda_error(sc, "timeout stopping CORB\n"); 438 return ETIME; 439 } 440 } 441 442 return 0; 443 } 444 445 static int 446 hdaudio_corb_start(struct hdaudio_softc *sc) 447 { 448 uint8_t corbctl; 449 int retry = HDAUDIO_CORB_TIMEOUT; 450 451 /* Start the CORB if necessary */ 452 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL); 453 if ((corbctl & HDAUDIO_CORBCTL_RUN) == 0) { 454 corbctl |= HDAUDIO_CORBCTL_RUN; 455 hda_write4(sc, HDAUDIO_MMIO_CORBCTL, corbctl); 456 do { 457 hda_delay(10); 458 corbctl = hda_read4(sc, HDAUDIO_MMIO_CORBCTL); 459 } while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) == 0); 460 if (retry == 0) { 461 hda_error(sc, "timeout starting CORB\n"); 462 return ETIME; 463 } 464 } 465 466 return 0; 467 } 468 469 static int 470 hdaudio_rirb_stop(struct hdaudio_softc *sc) 471 { 472 uint8_t rirbctl; 473 int retry = HDAUDIO_RIRB_TIMEOUT; 474 475 /* Stop the RIRB if necessary */ 476 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL); 477 if (rirbctl & (HDAUDIO_RIRBCTL_RUN|HDAUDIO_RIRBCTL_ROI_EN)) { 478 rirbctl &= ~HDAUDIO_RIRBCTL_RUN; 479 rirbctl &= ~HDAUDIO_RIRBCTL_ROI_EN; 480 hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl); 481 do { 482 hda_delay(10); 483 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL); 484 } while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) != 0); 485 if (retry == 0) { 486 hda_error(sc, "timeout stopping RIRB\n"); 487 return ETIME; 488 } 489 } 490 491 return 0; 492 } 493 494 static int 495 hdaudio_rirb_start(struct hdaudio_softc *sc) 496 { 497 uint8_t rirbctl; 498 int retry = HDAUDIO_RIRB_TIMEOUT; 499 500 /* Start the RIRB if necessary */ 501 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL); 502 if ((rirbctl & (HDAUDIO_RIRBCTL_RUN|HDAUDIO_RIRBCTL_INT_EN)) == 0) { 503 rirbctl |= HDAUDIO_RIRBCTL_RUN; 504 rirbctl |= HDAUDIO_RIRBCTL_INT_EN; 505 hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl); 506 do { 507 hda_delay(10); 508 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL); 509 } while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) == 0); 510 if (retry == 0) { 511 hda_error(sc, "timeout starting RIRB\n"); 512 return ETIME; 513 } 514 } 515 516 return 0; 517 } 518 519 static int 520 hdaudio_rirb_setsize(struct hdaudio_softc *sc) 521 { 522 uint8_t rirbsize; 523 bus_size_t bufsize = 0; 524 525 /* 526 * The size of the RIRB is programmable to 2, 16, or 256 entries 527 * by using the RIRBSIZE register. Choose a size based on the 528 * controller capabilities, preferring a larger size when possible. 529 */ 530 rirbsize = hda_read1(sc, HDAUDIO_MMIO_RIRBSIZE); 531 rirbsize &= ~0x3; 532 if ((rirbsize >> 4) & 0x4) { 533 rirbsize |= 0x2; 534 bufsize = 2048; 535 } else if ((rirbsize >> 4) & 0x2) { 536 rirbsize |= 0x1; 537 bufsize = 128; 538 } else if ((rirbsize >> 4) & 0x1) { 539 rirbsize |= 0x0; 540 bufsize = 16; 541 } else { 542 hda_error(sc, "couldn't configure RIRB size\n"); 543 return ENXIO; 544 } 545 546 #if defined(HDAUDIO_DEBUG) 547 hda_print(sc, "using %d byte RIRB (cap %X)\n", 548 (int)bufsize, rirbsize >> 4); 549 #endif 550 551 sc->sc_rirb.dma_size = bufsize; 552 sc->sc_rirb.dma_sizereg = rirbsize; 553 554 return 0; 555 } 556 557 static int 558 hdaudio_rirb_config(struct hdaudio_softc *sc) 559 { 560 uint32_t rirbubase, rirblbase; 561 uint32_t rirbwp; 562 int retry = HDAUDIO_RIRB_TIMEOUT; 563 564 /* Program command buffer base address and size */ 565 rirblbase = (uint32_t)DMA_DMAADDR(&sc->sc_rirb); 566 rirbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_rirb)) >> 32); 567 hda_write4(sc, HDAUDIO_MMIO_RIRBLBASE, rirblbase); 568 hda_write4(sc, HDAUDIO_MMIO_RIRBUBASE, rirbubase); 569 hda_write1(sc, HDAUDIO_MMIO_RIRBSIZE, sc->sc_rirb.dma_sizereg); 570 571 /* Clear the write pointer */ 572 hda_write2(sc, HDAUDIO_MMIO_RIRBWP, HDAUDIO_RIRBWP_WP_RESET); 573 hda_write2(sc, HDAUDIO_MMIO_RIRBWP, 0); 574 do { 575 hda_delay(10); 576 rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP); 577 } while (--retry > 0 && (rirbwp & HDAUDIO_RIRBWP_WP_RESET) != 0); 578 if (retry == 0) { 579 hda_error(sc, "timeout resetting RIRB\n"); 580 return ETIME; 581 } 582 sc->sc_rirbrp = 0; 583 584 return 0; 585 } 586 587 static int 588 hdaudio_reset(struct hdaudio_softc *sc) 589 { 590 int retry = HDAUDIO_RESET_TIMEOUT; 591 uint32_t gctl; 592 int err; 593 594 if ((err = hdaudio_rirb_stop(sc)) != 0) { 595 hda_error(sc, "couldn't reset because RIRB is busy\n"); 596 return err; 597 } 598 if ((err = hdaudio_corb_stop(sc)) != 0) { 599 hda_error(sc, "couldn't reset because CORB is busy\n"); 600 return err; 601 } 602 603 /* Disable wake events */ 604 hda_write2(sc, HDAUDIO_MMIO_WAKEEN, 0); 605 606 /* Disable interrupts */ 607 hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0); 608 609 /* Clear state change status register */ 610 hda_write2(sc, HDAUDIO_MMIO_STATESTS, 611 hda_read2(sc, HDAUDIO_MMIO_STATESTS)); 612 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, 613 hda_read1(sc, HDAUDIO_MMIO_RIRBSTS)); 614 615 /* If the controller isn't in reset state, initiate the transition */ 616 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL); 617 if (gctl & HDAUDIO_GCTL_CRST) { 618 gctl &= ~HDAUDIO_GCTL_CRST; 619 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl); 620 do { 621 hda_delay(10); 622 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL); 623 } while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) != 0); 624 if (retry == 0) { 625 hda_error(sc, "timeout entering reset state\n"); 626 return ETIME; 627 } 628 } 629 630 /* Now the controller is in reset state, so bring it out */ 631 retry = HDAUDIO_RESET_TIMEOUT; 632 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_CRST); 633 do { 634 hda_delay(10); 635 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL); 636 } while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) == 0); 637 if (retry == 0) { 638 hda_error(sc, "timeout leaving reset state\n"); 639 return ETIME; 640 } 641 642 /* Accept unsolicited responses */ 643 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_UNSOL_EN); 644 645 return 0; 646 } 647 648 static void 649 hdaudio_intr_enable(struct hdaudio_softc *sc) 650 { 651 hda_write4(sc, HDAUDIO_MMIO_INTSTS, 652 hda_read4(sc, HDAUDIO_MMIO_INTSTS)); 653 hda_write4(sc, HDAUDIO_MMIO_INTCTL, 654 HDAUDIO_INTCTL_GIE | HDAUDIO_INTCTL_CIE); 655 } 656 657 static void 658 hdaudio_intr_disable(struct hdaudio_softc *sc) 659 { 660 hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0); 661 } 662 663 static int 664 hdaudio_config_print(void *opaque, const char *pnp) 665 { 666 prop_dictionary_t dict = opaque; 667 uint8_t fgtype, nid; 668 uint16_t vendor, product; 669 const char *type = "unknown"; 670 671 prop_dictionary_get_uint8(dict, "function-group-type", &fgtype); 672 prop_dictionary_get_uint8(dict, "node-id", &nid); 673 prop_dictionary_get_uint16(dict, "vendor-id", &vendor); 674 prop_dictionary_get_uint16(dict, "product-id", &product); 675 if (pnp) { 676 if (fgtype == HDAUDIO_GROUP_TYPE_AFG) 677 type = "hdafg"; 678 else if (fgtype == HDAUDIO_GROUP_TYPE_VSM_FG) 679 type = "hdvsmfg"; 680 681 aprint_normal("%s at %s", type, pnp); 682 } 683 aprint_debug(" vendor 0x%04X product 0x%04X nid 0x%02X", 684 vendor, product, nid); 685 686 return UNCONF; 687 } 688 689 static void 690 hdaudio_attach_fg(struct hdaudio_function_group *fg, prop_array_t config) 691 { 692 struct hdaudio_codec *co = fg->fg_codec; 693 struct hdaudio_softc *sc = co->co_host; 694 prop_dictionary_t args = prop_dictionary_create(); 695 uint64_t fgptr = (vaddr_t)fg; 696 int locs[1]; 697 698 prop_dictionary_set_uint8(args, "function-group-type", fg->fg_type); 699 prop_dictionary_set_uint64(args, "function-group", fgptr); 700 prop_dictionary_set_uint8(args, "node-id", fg->fg_nid); 701 prop_dictionary_set_uint16(args, "vendor-id", fg->fg_vendor); 702 prop_dictionary_set_uint16(args, "product-id", fg->fg_product); 703 if (config) 704 prop_dictionary_set(args, "pin-config", config); 705 706 locs[0] = fg->fg_nid; 707 708 fg->fg_device = config_found_sm_loc(sc->sc_dev, "hdaudiobus", 709 locs, args, hdaudio_config_print, config_stdsubmatch); 710 711 prop_object_release(args); 712 } 713 714 static void 715 hdaudio_codec_attach(struct hdaudio_codec *co) 716 { 717 struct hdaudio_function_group *fg; 718 uint32_t vid, snc, fgrp; 719 int starting_node, num_nodes, nid; 720 721 if (co->co_valid == false) 722 return; 723 724 vid = hdaudio_command(co, 0, CORB_GET_PARAMETER, COP_VENDOR_ID); 725 snc = hdaudio_command(co, 0, CORB_GET_PARAMETER, 726 COP_SUBORDINATE_NODE_COUNT); 727 728 /* make sure the vendor and product IDs are valid */ 729 if (vid == 0xffffffff || vid == 0x00000000) 730 return; 731 732 #ifdef HDAUDIO_DEBUG 733 struct hdaudio_softc *sc = co->co_host; 734 uint32_t rid = hdaudio_command(co, 0, CORB_GET_PARAMETER, 735 COP_REVISION_ID); 736 hda_print(sc, "Codec%02X: %04X:%04X HDA %d.%d rev %d stepping %d\n", 737 co->co_addr, vid >> 16, vid & 0xffff, 738 (rid >> 20) & 0xf, (rid >> 16) & 0xf, 739 (rid >> 8) & 0xff, rid & 0xff); 740 #endif 741 starting_node = (snc >> 16) & 0xff; 742 num_nodes = snc & 0xff; 743 744 co->co_nfg = num_nodes; 745 co->co_fg = kmem_zalloc(co->co_nfg * sizeof(*co->co_fg), KM_SLEEP); 746 747 for (nid = starting_node; nid < starting_node + num_nodes; nid++) { 748 fg = &co->co_fg[nid - starting_node]; 749 fg->fg_codec = co; 750 fg->fg_nid = nid; 751 fg->fg_vendor = vid >> 16; 752 fg->fg_product = vid & 0xffff; 753 754 fgrp = hdaudio_command(co, nid, CORB_GET_PARAMETER, 755 COP_FUNCTION_GROUP_TYPE); 756 switch (fgrp & 0xff) { 757 case 0x01: /* Audio Function Group */ 758 fg->fg_type = HDAUDIO_GROUP_TYPE_AFG; 759 break; 760 case 0x02: /* Vendor Specific Modem Function Group */ 761 fg->fg_type = HDAUDIO_GROUP_TYPE_VSM_FG; 762 break; 763 default: 764 /* Function group type not supported */ 765 fg->fg_type = HDAUDIO_GROUP_TYPE_UNKNOWN; 766 break; 767 } 768 hdaudio_attach_fg(fg, NULL); 769 } 770 } 771 772 int 773 hdaudio_stream_tag(struct hdaudio_stream *st) 774 { 775 int ret = 0; 776 777 switch (st->st_type) { 778 case HDAUDIO_STREAM_ISS: 779 ret = 1; 780 break; 781 case HDAUDIO_STREAM_OSS: 782 ret = 2; 783 break; 784 case HDAUDIO_STREAM_BSS: 785 ret = 3; 786 break; 787 } 788 789 return ret; 790 } 791 792 int 793 hdaudio_attach(device_t dev, struct hdaudio_softc *sc) 794 { 795 int err, i; 796 797 KASSERT(sc->sc_memvalid == true); 798 799 sc->sc_dev = dev; 800 mutex_init(&sc->sc_corb_mtx, MUTEX_DEFAULT, IPL_AUDIO); 801 mutex_init(&sc->sc_stream_mtx, MUTEX_DEFAULT, IPL_AUDIO); 802 803 hdaudio_init(sc); 804 805 /* 806 * Put the controller into a known state by entering and leaving 807 * CRST as necessary. 808 */ 809 if ((err = hdaudio_reset(sc)) != 0) 810 goto fail; 811 812 /* 813 * From the spec: 814 * 815 * Must wait 250us after reading CRST as a 1 before assuming that 816 * codecs have all made status change requests and have been 817 * registered by the controller. 818 * 819 * In reality, we need to wait longer than this. 820 */ 821 hda_delay(HDAUDIO_CODEC_DELAY); 822 if (hdaudio_codec_probe(sc) == 0) { 823 hda_error(sc, "no codecs found\n"); 824 err = ENODEV; 825 goto fail; 826 } 827 828 /* 829 * Ensure that the device is in a known state 830 */ 831 hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE); 832 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, 833 HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL); 834 hda_write4(sc, HDAUDIO_MMIO_INTSTS, 835 hda_read4(sc, HDAUDIO_MMIO_INTSTS)); 836 hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0); 837 hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0); 838 839 /* 840 * Initialize the CORB. First negotiate a command buffer size, 841 * then allocate and configure it. 842 */ 843 if ((err = hdaudio_corb_setsize(sc)) != 0) 844 goto fail; 845 if ((err = hdaudio_dma_alloc(sc, &sc->sc_corb, BUS_DMA_WRITE)) != 0) 846 goto fail; 847 if ((err = hdaudio_corb_config(sc)) != 0) 848 goto fail; 849 850 /* 851 * Initialize the RIRB. 852 */ 853 if ((err = hdaudio_rirb_setsize(sc)) != 0) 854 goto fail; 855 if ((err = hdaudio_dma_alloc(sc, &sc->sc_rirb, BUS_DMA_READ)) != 0) 856 goto fail; 857 if ((err = hdaudio_rirb_config(sc)) != 0) 858 goto fail; 859 860 /* 861 * Start the CORB and RIRB 862 */ 863 if ((err = hdaudio_corb_start(sc)) != 0) 864 goto fail; 865 if ((err = hdaudio_rirb_start(sc)) != 0) 866 goto fail; 867 868 /* 869 * Identify and attach discovered codecs 870 */ 871 for (i = 0; i < HDAUDIO_MAX_CODECS; i++) 872 hdaudio_codec_attach(&sc->sc_codec[i]); 873 874 /* 875 * Enable interrupts 876 */ 877 hdaudio_intr_enable(sc); 878 879 fail: 880 if (err) 881 hda_error(sc, "device driver failed to attach\n"); 882 return err; 883 } 884 885 int 886 hdaudio_detach(struct hdaudio_softc *sc, int flags) 887 { 888 int error; 889 890 /* Disable interrupts */ 891 hdaudio_intr_disable(sc); 892 893 error = config_detach_children(sc->sc_dev, flags); 894 if (error != 0) { 895 hdaudio_intr_enable(sc); 896 return error; 897 } 898 899 mutex_destroy(&sc->sc_corb_mtx); 900 mutex_destroy(&sc->sc_stream_mtx); 901 902 hdaudio_dma_free(sc, &sc->sc_corb); 903 hdaudio_dma_free(sc, &sc->sc_rirb); 904 905 return 0; 906 } 907 908 bool 909 hdaudio_resume(struct hdaudio_softc *sc) 910 { 911 if (hdaudio_reset(sc) != 0) 912 return false; 913 914 hda_delay(HDAUDIO_CODEC_DELAY); 915 916 /* 917 * Ensure that the device is in a known state 918 */ 919 hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE); 920 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, 921 HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL); 922 hda_write4(sc, HDAUDIO_MMIO_INTSTS, 923 hda_read4(sc, HDAUDIO_MMIO_INTSTS)); 924 hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0); 925 hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0); 926 927 if (hdaudio_corb_config(sc) != 0) 928 return false; 929 if (hdaudio_rirb_config(sc) != 0) 930 return false; 931 if (hdaudio_corb_start(sc) != 0) 932 return false; 933 if (hdaudio_rirb_start(sc) != 0) 934 return false; 935 936 hdaudio_intr_enable(sc); 937 938 return true; 939 } 940 941 int 942 hdaudio_rescan(struct hdaudio_softc *sc, const char *ifattr, const int *locs) 943 { 944 struct hdaudio_codec *co; 945 struct hdaudio_function_group *fg; 946 unsigned int codec; 947 948 if (!ifattr_match(ifattr, "hdaudiobus")) 949 return 0; 950 951 for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) { 952 co = &sc->sc_codec[codec]; 953 fg = co->co_fg; 954 if (!co->co_valid || fg == NULL) 955 continue; 956 if (fg->fg_device) 957 continue; 958 hdaudio_attach_fg(fg, NULL); 959 } 960 961 return 0; 962 } 963 964 void 965 hdaudio_childdet(struct hdaudio_softc *sc, device_t child) 966 { 967 struct hdaudio_codec *co; 968 struct hdaudio_function_group *fg; 969 unsigned int codec; 970 971 for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) { 972 co = &sc->sc_codec[codec]; 973 fg = co->co_fg; 974 if (!co->co_valid || fg == NULL) 975 continue; 976 if (fg->fg_device == child) 977 fg->fg_device = NULL; 978 } 979 } 980 981 int 982 hdaudio_intr(struct hdaudio_softc *sc) 983 { 984 struct hdaudio_stream *st; 985 uint32_t intsts, stream_mask; 986 int streamid = 0; 987 uint8_t rirbsts; 988 989 intsts = hda_read4(sc, HDAUDIO_MMIO_INTSTS); 990 if (!(intsts & HDAUDIO_INTSTS_GIS)) 991 return 0; 992 993 if (intsts & HDAUDIO_INTSTS_CIS) { 994 rirbsts = hda_read1(sc, HDAUDIO_MMIO_RIRBSTS); 995 if (rirbsts & HDAUDIO_RIRBSTS_RINTFL) { 996 mutex_enter(&sc->sc_corb_mtx); 997 hdaudio_rirb_dequeue(sc, true); 998 mutex_exit(&sc->sc_corb_mtx); 999 } 1000 if (rirbsts & (HDAUDIO_RIRBSTS_RIRBOIS|HDAUDIO_RIRBSTS_RINTFL)) 1001 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, rirbsts); 1002 hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_CIS); 1003 } 1004 if (intsts & HDAUDIO_INTSTS_SIS_MASK) { 1005 mutex_enter(&sc->sc_stream_mtx); 1006 stream_mask = intsts & sc->sc_stream_mask; 1007 while (streamid < HDAUDIO_MAX_STREAMS && stream_mask != 0) { 1008 st = &sc->sc_stream[streamid++]; 1009 if ((stream_mask & 1) != 0 && st->st_intr) { 1010 st->st_intr(st); 1011 } 1012 stream_mask >>= 1; 1013 } 1014 mutex_exit(&sc->sc_stream_mtx); 1015 hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_SIS_MASK); 1016 } 1017 1018 return 1; 1019 } 1020 1021 struct hdaudio_stream * 1022 hdaudio_stream_establish(struct hdaudio_softc *sc, 1023 enum hdaudio_stream_type type, int (*intr)(struct hdaudio_stream *), 1024 void *cookie) 1025 { 1026 struct hdaudio_stream *st; 1027 struct hdaudio_dma dma; 1028 int i, err; 1029 1030 dma.dma_size = sizeof(struct hdaudio_bdl_entry) * HDAUDIO_BDL_MAX; 1031 dma.dma_sizereg = 0; 1032 err = hdaudio_dma_alloc(sc, &dma, BUS_DMA_COHERENT | BUS_DMA_NOCACHE); 1033 if (err) 1034 return NULL; 1035 1036 mutex_enter(&sc->sc_stream_mtx); 1037 for (i = 0; i < HDAUDIO_MAX_STREAMS; i++) { 1038 st = &sc->sc_stream[i]; 1039 if (st->st_enable == false) 1040 break; 1041 if (st->st_type != type) 1042 continue; 1043 if (sc->sc_stream_mask & (1 << i)) 1044 continue; 1045 1046 /* Allocate stream */ 1047 st->st_bdl = dma; 1048 st->st_intr = intr; 1049 st->st_cookie = cookie; 1050 sc->sc_stream_mask |= (1 << i); 1051 mutex_exit(&sc->sc_stream_mtx); 1052 return st; 1053 } 1054 mutex_exit(&sc->sc_stream_mtx); 1055 1056 /* No streams of requested type available */ 1057 hdaudio_dma_free(sc, &dma); 1058 return NULL; 1059 } 1060 1061 void 1062 hdaudio_stream_disestablish(struct hdaudio_stream *st) 1063 { 1064 struct hdaudio_softc *sc = st->st_host; 1065 struct hdaudio_dma dma; 1066 1067 KASSERT(sc->sc_stream_mask & (1 << st->st_shift)); 1068 1069 mutex_enter(&sc->sc_stream_mtx); 1070 sc->sc_stream_mask &= ~(1 << st->st_shift); 1071 st->st_intr = NULL; 1072 st->st_cookie = NULL; 1073 dma = st->st_bdl; 1074 st->st_bdl.dma_valid = false; 1075 mutex_exit(&sc->sc_stream_mtx); 1076 1077 /* Can't bus_dmamem_unmap while holding a mutex. */ 1078 hdaudio_dma_free(sc, &dma); 1079 } 1080 1081 /* 1082 * Convert most of audio_params_t to stream fmt descriptor; noticably missing 1083 * is the # channels bits, as this is encoded differently in codec and 1084 * stream descriptors. 1085 * 1086 * TODO: validate that the stream and selected codecs can handle the fmt 1087 */ 1088 uint16_t 1089 hdaudio_stream_param(struct hdaudio_stream *st, const audio_params_t *param) 1090 { 1091 uint16_t fmt = 0; 1092 1093 switch (param->encoding) { 1094 case AUDIO_ENCODING_AC3: 1095 fmt |= HDAUDIO_FMT_TYPE_NONPCM; 1096 break; 1097 default: 1098 fmt |= HDAUDIO_FMT_TYPE_PCM; 1099 break; 1100 } 1101 1102 switch (param->sample_rate) { 1103 case 8000: 1104 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) | 1105 HDAUDIO_FMT_DIV(6); 1106 break; 1107 case 11025: 1108 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) | 1109 HDAUDIO_FMT_DIV(4); 1110 break; 1111 case 16000: 1112 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) | 1113 HDAUDIO_FMT_DIV(3); 1114 break; 1115 case 22050: 1116 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) | 1117 HDAUDIO_FMT_DIV(2); 1118 break; 1119 case 32000: 1120 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2) | 1121 HDAUDIO_FMT_DIV(3); 1122 break; 1123 case 44100: 1124 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1); 1125 break; 1126 case 48000: 1127 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1); 1128 break; 1129 case 88200: 1130 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(2); 1131 break; 1132 case 96000: 1133 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2); 1134 break; 1135 case 176400: 1136 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(4); 1137 break; 1138 case 192000: 1139 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(4); 1140 break; 1141 default: 1142 return 0; 1143 } 1144 1145 if (param->precision == 16 && param->validbits == 8) 1146 fmt |= HDAUDIO_FMT_BITS_8_16; 1147 else if (param->precision == 16 && param->validbits == 16) 1148 fmt |= HDAUDIO_FMT_BITS_16_16; 1149 else if (param->precision == 32 && param->validbits == 20) 1150 fmt |= HDAUDIO_FMT_BITS_20_32; 1151 else if (param->precision == 32 && param->validbits == 24) 1152 fmt |= HDAUDIO_FMT_BITS_24_32; 1153 else if (param->precision == 32 && param->validbits == 32) 1154 fmt |= HDAUDIO_FMT_BITS_32_32; 1155 else 1156 return 0; 1157 1158 return fmt; 1159 } 1160 1161 void 1162 hdaudio_stream_reset(struct hdaudio_stream *st) 1163 { 1164 struct hdaudio_softc *sc = st->st_host; 1165 int snum = st->st_shift; 1166 int retry; 1167 uint8_t ctl0; 1168 1169 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1170 ctl0 |= HDAUDIO_CTL_SRST; 1171 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1172 1173 retry = HDAUDIO_RESET_TIMEOUT; 1174 do { 1175 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1176 if (ctl0 & HDAUDIO_CTL_SRST) 1177 break; 1178 hda_delay(10); 1179 } while (--retry > 0); 1180 if (retry == 0) { 1181 hda_error(sc, "timeout entering stream reset state\n"); 1182 return; 1183 } 1184 1185 ctl0 &= ~HDAUDIO_CTL_SRST; 1186 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1187 1188 retry = HDAUDIO_RESET_TIMEOUT; 1189 do { 1190 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1191 if (!(ctl0 & HDAUDIO_CTL_SRST)) 1192 break; 1193 hda_delay(10); 1194 } while (--retry > 0); 1195 if (retry == 0) { 1196 hda_error(sc, "timeout leaving stream reset state\n"); 1197 return; 1198 } 1199 } 1200 1201 void 1202 hdaudio_stream_start(struct hdaudio_stream *st, int blksize, 1203 bus_size_t dmasize, const audio_params_t *params) 1204 { 1205 struct hdaudio_softc *sc = st->st_host; 1206 struct hdaudio_bdl_entry *bdl; 1207 uint64_t dmaaddr; 1208 uint32_t intctl; 1209 uint16_t fmt; 1210 uint8_t ctl0, ctl2; 1211 int cnt, snum = st->st_shift; 1212 1213 KASSERT(sc->sc_stream_mask & (1 << st->st_shift)); 1214 KASSERT(st->st_data.dma_valid == true); 1215 KASSERT(st->st_bdl.dma_valid == true); 1216 1217 hdaudio_stream_stop(st); 1218 hdaudio_stream_reset(st); 1219 1220 /* 1221 * Configure buffer descriptor list 1222 */ 1223 dmaaddr = DMA_DMAADDR(&st->st_data); 1224 bdl = DMA_KERNADDR(&st->st_bdl); 1225 for (cnt = 0; cnt < HDAUDIO_BDL_MAX; cnt++) { 1226 bdl[cnt].address_lo = (uint32_t)dmaaddr; 1227 bdl[cnt].address_hi = dmaaddr >> 32; 1228 bdl[cnt].length = blksize; 1229 bdl[cnt].flags = HDAUDIO_BDL_ENTRY_IOC; 1230 dmaaddr += blksize; 1231 if (dmaaddr >= DMA_DMAADDR(&st->st_data) + dmasize) { 1232 cnt++; 1233 break; 1234 } 1235 } 1236 1237 /* 1238 * Program buffer descriptor list 1239 */ 1240 dmaaddr = DMA_DMAADDR(&st->st_bdl); 1241 hda_write4(sc, HDAUDIO_SD_BDPL(snum), (uint32_t)dmaaddr); 1242 hda_write4(sc, HDAUDIO_SD_BDPU(snum), (uint32_t)(dmaaddr >> 32)); 1243 hda_write2(sc, HDAUDIO_SD_LVI(snum), (cnt - 1) & 0xff); 1244 1245 /* 1246 * Program cyclic buffer length 1247 */ 1248 hda_write4(sc, HDAUDIO_SD_CBL(snum), dmasize); 1249 1250 /* 1251 * Program stream number (tag). Although controller hardware is 1252 * capable of transmitting any stream number (0-15), by convention 1253 * stream 0 is reserved as unused by software, so that converters 1254 * whose stream numbers have been reset to 0 do not unintentionally 1255 * decode data not intended for them. 1256 */ 1257 ctl2 = hda_read1(sc, HDAUDIO_SD_CTL2(snum)); 1258 ctl2 &= ~0xf0; 1259 ctl2 |= hdaudio_stream_tag(st) << 4; 1260 hda_write1(sc, HDAUDIO_SD_CTL2(snum), ctl2); 1261 1262 /* 1263 * Program stream format 1264 */ 1265 fmt = hdaudio_stream_param(st, params) | 1266 HDAUDIO_FMT_CHAN(params->channels); 1267 hda_write2(sc, HDAUDIO_SD_FMT(snum), fmt); 1268 1269 /* 1270 * Switch on interrupts for this stream 1271 */ 1272 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL); 1273 intctl |= (1 << st->st_shift); 1274 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl); 1275 1276 /* 1277 * Start running the stream 1278 */ 1279 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1280 ctl0 |= HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE | 1281 HDAUDIO_CTL_RUN; 1282 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1283 } 1284 1285 void 1286 hdaudio_stream_stop(struct hdaudio_stream *st) 1287 { 1288 struct hdaudio_softc *sc = st->st_host; 1289 uint32_t intctl; 1290 uint8_t ctl0; 1291 int snum = st->st_shift; 1292 1293 /* 1294 * Stop running the stream 1295 */ 1296 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1297 ctl0 &= ~(HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE | 1298 HDAUDIO_CTL_RUN); 1299 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1300 1301 /* 1302 * Switch off interrupts for this stream 1303 */ 1304 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL); 1305 intctl &= ~(1 << st->st_shift); 1306 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl); 1307 } 1308 1309 /* 1310 * /dev/hdaudioN interface 1311 */ 1312 1313 static const char * 1314 hdaudioioctl_fgrp_to_cstr(enum function_group_type type) 1315 { 1316 switch (type) { 1317 case HDAUDIO_GROUP_TYPE_AFG: 1318 return "afg"; 1319 case HDAUDIO_GROUP_TYPE_VSM_FG: 1320 return "vsmfg"; 1321 default: 1322 return "unknown"; 1323 } 1324 } 1325 1326 static struct hdaudio_function_group * 1327 hdaudioioctl_fgrp_lookup(struct hdaudio_softc *sc, int codecid, int nid) 1328 { 1329 struct hdaudio_codec *co; 1330 struct hdaudio_function_group *fg = NULL; 1331 int i; 1332 1333 if (codecid < 0 || codecid >= HDAUDIO_MAX_CODECS) 1334 return NULL; 1335 co = &sc->sc_codec[codecid]; 1336 if (co->co_valid == false) 1337 return NULL; 1338 1339 for (i = 0; i < co->co_nfg; i++) 1340 if (co->co_fg[i].fg_nid == nid) { 1341 fg = &co->co_fg[i]; 1342 break; 1343 } 1344 1345 return fg; 1346 } 1347 1348 static int 1349 hdaudioioctl_fgrp_info(struct hdaudio_softc *sc, prop_dictionary_t request, 1350 prop_dictionary_t response) 1351 { 1352 struct hdaudio_codec *co; 1353 struct hdaudio_function_group *fg; 1354 prop_array_t array; 1355 prop_dictionary_t dict; 1356 int codecid, fgid; 1357 1358 array = prop_array_create(); 1359 if (array == NULL) 1360 return ENOMEM; 1361 1362 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) { 1363 co = &sc->sc_codec[codecid]; 1364 if (co->co_valid == false) 1365 continue; 1366 for (fgid = 0; fgid < co->co_nfg; fgid++) { 1367 fg = &co->co_fg[fgid]; 1368 dict = prop_dictionary_create(); 1369 if (dict == NULL) 1370 return ENOMEM; 1371 prop_dictionary_set_cstring_nocopy(dict, 1372 "type", hdaudioioctl_fgrp_to_cstr(fg->fg_type)); 1373 prop_dictionary_set_int16(dict, "nid", fg->fg_nid); 1374 prop_dictionary_set_int16(dict, "codecid", codecid); 1375 prop_dictionary_set_uint16(dict, "vendor-id", 1376 fg->fg_vendor); 1377 prop_dictionary_set_uint16(dict, "product-id", 1378 fg->fg_product); 1379 prop_dictionary_set_uint32(dict, "subsystem-id", 1380 sc->sc_subsystem); 1381 if (fg->fg_device) 1382 prop_dictionary_set_cstring(dict, "device", 1383 device_xname(fg->fg_device)); 1384 else 1385 prop_dictionary_set_cstring_nocopy(dict, 1386 "device", "<none>"); 1387 prop_array_add(array, dict); 1388 } 1389 } 1390 1391 prop_dictionary_set(response, "function-group-info", array); 1392 return 0; 1393 } 1394 1395 static int 1396 hdaudioioctl_fgrp_getconfig(struct hdaudio_softc *sc, 1397 prop_dictionary_t request, prop_dictionary_t response) 1398 { 1399 struct hdaudio_function_group *fg; 1400 prop_dictionary_t dict; 1401 prop_array_t array; 1402 uint32_t nodecnt, wcap, config; 1403 int16_t codecid, nid, i; 1404 int startnode, endnode; 1405 1406 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1407 !prop_dictionary_get_int16(request, "nid", &nid)) 1408 return EINVAL; 1409 1410 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1411 if (fg == NULL) 1412 return ENODEV; 1413 1414 array = prop_array_create(); 1415 if (array == NULL) 1416 return ENOMEM; 1417 1418 nodecnt = hdaudio_command(fg->fg_codec, fg->fg_nid, 1419 CORB_GET_PARAMETER, COP_SUBORDINATE_NODE_COUNT); 1420 startnode = COP_NODECNT_STARTNODE(nodecnt); 1421 endnode = startnode + COP_NODECNT_NUMNODES(nodecnt); 1422 1423 for (i = startnode; i < endnode; i++) { 1424 wcap = hdaudio_command(fg->fg_codec, i, 1425 CORB_GET_PARAMETER, COP_AUDIO_WIDGET_CAPABILITIES); 1426 if (COP_AWCAP_TYPE(wcap) != COP_AWCAP_TYPE_PIN_COMPLEX) 1427 continue; 1428 config = hdaudio_command(fg->fg_codec, i, 1429 CORB_GET_CONFIGURATION_DEFAULT, 0); 1430 dict = prop_dictionary_create(); 1431 if (dict == NULL) 1432 return ENOMEM; 1433 prop_dictionary_set_int16(dict, "nid", i); 1434 prop_dictionary_set_uint32(dict, "config", config); 1435 prop_array_add(array, dict); 1436 } 1437 1438 prop_dictionary_set(response, "pin-config", array); 1439 1440 return 0; 1441 } 1442 1443 static int 1444 hdaudioioctl_fgrp_setconfig(struct hdaudio_softc *sc, 1445 prop_dictionary_t request, prop_dictionary_t response) 1446 { 1447 struct hdaudio_function_group *fg; 1448 prop_array_t config; 1449 int16_t codecid, nid; 1450 int err; 1451 1452 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1453 !prop_dictionary_get_int16(request, "nid", &nid)) 1454 return EINVAL; 1455 1456 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1457 if (fg == NULL) 1458 return ENODEV; 1459 1460 if (fg->fg_device) { 1461 err = config_detach(fg->fg_device, 0); 1462 if (err) 1463 return err; 1464 fg->fg_device = NULL; 1465 } 1466 1467 /* "pin-config" may be NULL, this means "use BIOS configuration" */ 1468 config = prop_dictionary_get(request, "pin-config"); 1469 if (config && prop_object_type(config) != PROP_TYPE_ARRAY) { 1470 prop_object_release(config); 1471 return EINVAL; 1472 } 1473 hdaudio_attach_fg(fg, config); 1474 if (config) 1475 prop_object_release(config); 1476 1477 return 0; 1478 } 1479 1480 static int 1481 hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc *sc, u_long cmd, 1482 prop_dictionary_t request, prop_dictionary_t response) 1483 { 1484 struct hdaudio_function_group *fg; 1485 int (*infocb)(void *, prop_dictionary_t, prop_dictionary_t); 1486 prop_dictionary_t fgrp_dict; 1487 uint64_t info_fn; 1488 int16_t codecid, nid; 1489 void *fgrp_sc; 1490 bool rv; 1491 int err; 1492 1493 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1494 !prop_dictionary_get_int16(request, "nid", &nid)) 1495 return EINVAL; 1496 1497 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1498 if (fg == NULL) 1499 return ENODEV; 1500 if (fg->fg_device == NULL) 1501 return ENXIO; 1502 fgrp_sc = device_private(fg->fg_device); 1503 fgrp_dict = device_properties(fg->fg_device); 1504 1505 switch (fg->fg_type) { 1506 case HDAUDIO_GROUP_TYPE_AFG: 1507 switch (cmd) { 1508 case HDAUDIO_FGRP_CODEC_INFO: 1509 rv = prop_dictionary_get_uint64(fgrp_dict, 1510 "codecinfo-callback", &info_fn); 1511 if (!rv) 1512 return ENXIO; 1513 infocb = (void *)(uintptr_t)info_fn; 1514 err = infocb(fgrp_sc, request, response); 1515 break; 1516 case HDAUDIO_FGRP_WIDGET_INFO: 1517 rv = prop_dictionary_get_uint64(fgrp_dict, 1518 "widgetinfo-callback", &info_fn); 1519 if (!rv) 1520 return ENXIO; 1521 infocb = (void *)(uintptr_t)info_fn; 1522 err = infocb(fgrp_sc, request, response); 1523 break; 1524 default: 1525 err = EINVAL; 1526 break; 1527 } 1528 break; 1529 1530 default: 1531 err = EINVAL; 1532 break; 1533 } 1534 return err; 1535 } 1536 1537 int 1538 hdaudioopen(dev_t dev, int flag, int mode, struct lwp *l) 1539 { 1540 device_t self; 1541 1542 self = device_lookup(&hdaudio_cd, HDAUDIOUNIT(dev)); 1543 if (self == NULL) 1544 return ENXIO; 1545 1546 return 0; 1547 } 1548 1549 int 1550 hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l) 1551 { 1552 return 0; 1553 } 1554 1555 int 1556 hdaudioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1557 { 1558 struct hdaudio_softc *sc; 1559 struct plistref *pref = addr; 1560 prop_dictionary_t request, response; 1561 int err; 1562 1563 sc = device_lookup_private(&hdaudio_cd, HDAUDIOUNIT(dev)); 1564 if (sc == NULL) 1565 return ENXIO; 1566 1567 response = prop_dictionary_create(); 1568 if (response == NULL) 1569 return ENOMEM; 1570 1571 err = prop_dictionary_copyin_ioctl(pref, cmd, &request); 1572 if (err) { 1573 prop_object_release(response); 1574 return err; 1575 } 1576 1577 switch (cmd) { 1578 case HDAUDIO_FGRP_INFO: 1579 err = hdaudioioctl_fgrp_info(sc, request, response); 1580 break; 1581 case HDAUDIO_FGRP_GETCONFIG: 1582 err = hdaudioioctl_fgrp_getconfig(sc, request, response); 1583 break; 1584 case HDAUDIO_FGRP_SETCONFIG: 1585 err = hdaudioioctl_fgrp_setconfig(sc, request, response); 1586 break; 1587 case HDAUDIO_FGRP_CODEC_INFO: 1588 case HDAUDIO_FGRP_WIDGET_INFO: 1589 err = hdaudio_dispatch_fgrp_ioctl(sc, cmd, request, response); 1590 break; 1591 default: 1592 err = EINVAL; 1593 break; 1594 } 1595 1596 if (!err) 1597 err = prop_dictionary_copyout_ioctl(pref, cmd, response); 1598 1599 if (response) 1600 prop_object_release(response); 1601 prop_object_release(request); 1602 return err; 1603 } 1604 1605 MODULE(MODULE_CLASS_DRIVER, hdaudio, NULL); 1606 1607 static int 1608 hdaudio_modcmd(modcmd_t cmd, void *opaque) 1609 { 1610 int error = 0; 1611 #ifdef _MODULE 1612 int bmaj = -1, cmaj = -1; 1613 #endif 1614 1615 switch (cmd) { 1616 case MODULE_CMD_INIT: 1617 #ifdef _MODULE 1618 error = devsw_attach("hdaudio", NULL, &bmaj, 1619 &hdaudio_cdevsw, &cmaj); 1620 #endif 1621 return error; 1622 case MODULE_CMD_FINI: 1623 #ifdef _MODULE 1624 devsw_detach(NULL, &hdaudio_cdevsw); 1625 #endif 1626 return 0; 1627 default: 1628 return ENOTTY; 1629 } 1630 } 1631 1632 DEV_VERBOSE_DEFINE(hdaudio); 1633