1 /* $NetBSD: hdaudio.c,v 1.3 2015/07/26 17:54:33 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.3 2015/07/26 17:54:33 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 if ((sc->sc_flags & HDAUDIO_FLAG_NO_STREAM_RESET) == 0) 1219 hdaudio_stream_reset(st); 1220 1221 /* 1222 * Configure buffer descriptor list 1223 */ 1224 dmaaddr = DMA_DMAADDR(&st->st_data); 1225 bdl = DMA_KERNADDR(&st->st_bdl); 1226 for (cnt = 0; cnt < HDAUDIO_BDL_MAX; cnt++) { 1227 bdl[cnt].address_lo = (uint32_t)dmaaddr; 1228 bdl[cnt].address_hi = dmaaddr >> 32; 1229 bdl[cnt].length = blksize; 1230 bdl[cnt].flags = HDAUDIO_BDL_ENTRY_IOC; 1231 dmaaddr += blksize; 1232 if (dmaaddr >= DMA_DMAADDR(&st->st_data) + dmasize) { 1233 cnt++; 1234 break; 1235 } 1236 } 1237 1238 /* 1239 * Program buffer descriptor list 1240 */ 1241 dmaaddr = DMA_DMAADDR(&st->st_bdl); 1242 hda_write4(sc, HDAUDIO_SD_BDPL(snum), (uint32_t)dmaaddr); 1243 hda_write4(sc, HDAUDIO_SD_BDPU(snum), (uint32_t)(dmaaddr >> 32)); 1244 hda_write2(sc, HDAUDIO_SD_LVI(snum), (cnt - 1) & 0xff); 1245 1246 /* 1247 * Program cyclic buffer length 1248 */ 1249 hda_write4(sc, HDAUDIO_SD_CBL(snum), dmasize); 1250 1251 /* 1252 * Program stream number (tag). Although controller hardware is 1253 * capable of transmitting any stream number (0-15), by convention 1254 * stream 0 is reserved as unused by software, so that converters 1255 * whose stream numbers have been reset to 0 do not unintentionally 1256 * decode data not intended for them. 1257 */ 1258 ctl2 = hda_read1(sc, HDAUDIO_SD_CTL2(snum)); 1259 ctl2 &= ~0xf0; 1260 ctl2 |= hdaudio_stream_tag(st) << 4; 1261 hda_write1(sc, HDAUDIO_SD_CTL2(snum), ctl2); 1262 1263 /* 1264 * Program stream format 1265 */ 1266 fmt = hdaudio_stream_param(st, params) | 1267 HDAUDIO_FMT_CHAN(params->channels); 1268 hda_write2(sc, HDAUDIO_SD_FMT(snum), fmt); 1269 1270 /* 1271 * Switch on interrupts for this stream 1272 */ 1273 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL); 1274 intctl |= (1 << st->st_shift); 1275 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl); 1276 1277 /* 1278 * Start running the stream 1279 */ 1280 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1281 ctl0 |= HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE | 1282 HDAUDIO_CTL_RUN; 1283 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1284 } 1285 1286 void 1287 hdaudio_stream_stop(struct hdaudio_stream *st) 1288 { 1289 struct hdaudio_softc *sc = st->st_host; 1290 uint32_t intctl; 1291 uint8_t ctl0; 1292 int snum = st->st_shift; 1293 1294 /* 1295 * Stop running the stream 1296 */ 1297 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum)); 1298 ctl0 &= ~(HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE | 1299 HDAUDIO_CTL_RUN); 1300 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0); 1301 1302 /* 1303 * Switch off interrupts for this stream 1304 */ 1305 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL); 1306 intctl &= ~(1 << st->st_shift); 1307 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl); 1308 } 1309 1310 /* 1311 * /dev/hdaudioN interface 1312 */ 1313 1314 static const char * 1315 hdaudioioctl_fgrp_to_cstr(enum function_group_type type) 1316 { 1317 switch (type) { 1318 case HDAUDIO_GROUP_TYPE_AFG: 1319 return "afg"; 1320 case HDAUDIO_GROUP_TYPE_VSM_FG: 1321 return "vsmfg"; 1322 default: 1323 return "unknown"; 1324 } 1325 } 1326 1327 static struct hdaudio_function_group * 1328 hdaudioioctl_fgrp_lookup(struct hdaudio_softc *sc, int codecid, int nid) 1329 { 1330 struct hdaudio_codec *co; 1331 struct hdaudio_function_group *fg = NULL; 1332 int i; 1333 1334 if (codecid < 0 || codecid >= HDAUDIO_MAX_CODECS) 1335 return NULL; 1336 co = &sc->sc_codec[codecid]; 1337 if (co->co_valid == false) 1338 return NULL; 1339 1340 for (i = 0; i < co->co_nfg; i++) 1341 if (co->co_fg[i].fg_nid == nid) { 1342 fg = &co->co_fg[i]; 1343 break; 1344 } 1345 1346 return fg; 1347 } 1348 1349 static int 1350 hdaudioioctl_fgrp_info(struct hdaudio_softc *sc, prop_dictionary_t request, 1351 prop_dictionary_t response) 1352 { 1353 struct hdaudio_codec *co; 1354 struct hdaudio_function_group *fg; 1355 prop_array_t array; 1356 prop_dictionary_t dict; 1357 int codecid, fgid; 1358 1359 array = prop_array_create(); 1360 if (array == NULL) 1361 return ENOMEM; 1362 1363 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) { 1364 co = &sc->sc_codec[codecid]; 1365 if (co->co_valid == false) 1366 continue; 1367 for (fgid = 0; fgid < co->co_nfg; fgid++) { 1368 fg = &co->co_fg[fgid]; 1369 dict = prop_dictionary_create(); 1370 if (dict == NULL) 1371 return ENOMEM; 1372 prop_dictionary_set_cstring_nocopy(dict, 1373 "type", hdaudioioctl_fgrp_to_cstr(fg->fg_type)); 1374 prop_dictionary_set_int16(dict, "nid", fg->fg_nid); 1375 prop_dictionary_set_int16(dict, "codecid", codecid); 1376 prop_dictionary_set_uint16(dict, "vendor-id", 1377 fg->fg_vendor); 1378 prop_dictionary_set_uint16(dict, "product-id", 1379 fg->fg_product); 1380 prop_dictionary_set_uint32(dict, "subsystem-id", 1381 sc->sc_subsystem); 1382 if (fg->fg_device) 1383 prop_dictionary_set_cstring(dict, "device", 1384 device_xname(fg->fg_device)); 1385 else 1386 prop_dictionary_set_cstring_nocopy(dict, 1387 "device", "<none>"); 1388 prop_array_add(array, dict); 1389 } 1390 } 1391 1392 prop_dictionary_set(response, "function-group-info", array); 1393 return 0; 1394 } 1395 1396 static int 1397 hdaudioioctl_fgrp_getconfig(struct hdaudio_softc *sc, 1398 prop_dictionary_t request, prop_dictionary_t response) 1399 { 1400 struct hdaudio_function_group *fg; 1401 prop_dictionary_t dict; 1402 prop_array_t array; 1403 uint32_t nodecnt, wcap, config; 1404 int16_t codecid, nid, i; 1405 int startnode, endnode; 1406 1407 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1408 !prop_dictionary_get_int16(request, "nid", &nid)) 1409 return EINVAL; 1410 1411 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1412 if (fg == NULL) 1413 return ENODEV; 1414 1415 array = prop_array_create(); 1416 if (array == NULL) 1417 return ENOMEM; 1418 1419 nodecnt = hdaudio_command(fg->fg_codec, fg->fg_nid, 1420 CORB_GET_PARAMETER, COP_SUBORDINATE_NODE_COUNT); 1421 startnode = COP_NODECNT_STARTNODE(nodecnt); 1422 endnode = startnode + COP_NODECNT_NUMNODES(nodecnt); 1423 1424 for (i = startnode; i < endnode; i++) { 1425 wcap = hdaudio_command(fg->fg_codec, i, 1426 CORB_GET_PARAMETER, COP_AUDIO_WIDGET_CAPABILITIES); 1427 if (COP_AWCAP_TYPE(wcap) != COP_AWCAP_TYPE_PIN_COMPLEX) 1428 continue; 1429 config = hdaudio_command(fg->fg_codec, i, 1430 CORB_GET_CONFIGURATION_DEFAULT, 0); 1431 dict = prop_dictionary_create(); 1432 if (dict == NULL) 1433 return ENOMEM; 1434 prop_dictionary_set_int16(dict, "nid", i); 1435 prop_dictionary_set_uint32(dict, "config", config); 1436 prop_array_add(array, dict); 1437 } 1438 1439 prop_dictionary_set(response, "pin-config", array); 1440 1441 return 0; 1442 } 1443 1444 static int 1445 hdaudioioctl_fgrp_setconfig(struct hdaudio_softc *sc, 1446 prop_dictionary_t request, prop_dictionary_t response) 1447 { 1448 struct hdaudio_function_group *fg; 1449 prop_array_t config; 1450 int16_t codecid, nid; 1451 int err; 1452 1453 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1454 !prop_dictionary_get_int16(request, "nid", &nid)) 1455 return EINVAL; 1456 1457 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1458 if (fg == NULL) 1459 return ENODEV; 1460 1461 if (fg->fg_device) { 1462 err = config_detach(fg->fg_device, 0); 1463 if (err) 1464 return err; 1465 fg->fg_device = NULL; 1466 } 1467 1468 /* "pin-config" may be NULL, this means "use BIOS configuration" */ 1469 config = prop_dictionary_get(request, "pin-config"); 1470 if (config && prop_object_type(config) != PROP_TYPE_ARRAY) { 1471 prop_object_release(config); 1472 return EINVAL; 1473 } 1474 hdaudio_attach_fg(fg, config); 1475 if (config) 1476 prop_object_release(config); 1477 1478 return 0; 1479 } 1480 1481 static int 1482 hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc *sc, u_long cmd, 1483 prop_dictionary_t request, prop_dictionary_t response) 1484 { 1485 struct hdaudio_function_group *fg; 1486 int (*infocb)(void *, prop_dictionary_t, prop_dictionary_t); 1487 prop_dictionary_t fgrp_dict; 1488 uint64_t info_fn; 1489 int16_t codecid, nid; 1490 void *fgrp_sc; 1491 bool rv; 1492 int err; 1493 1494 if (!prop_dictionary_get_int16(request, "codecid", &codecid) || 1495 !prop_dictionary_get_int16(request, "nid", &nid)) 1496 return EINVAL; 1497 1498 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid); 1499 if (fg == NULL) 1500 return ENODEV; 1501 if (fg->fg_device == NULL) 1502 return ENXIO; 1503 fgrp_sc = device_private(fg->fg_device); 1504 fgrp_dict = device_properties(fg->fg_device); 1505 1506 switch (fg->fg_type) { 1507 case HDAUDIO_GROUP_TYPE_AFG: 1508 switch (cmd) { 1509 case HDAUDIO_FGRP_CODEC_INFO: 1510 rv = prop_dictionary_get_uint64(fgrp_dict, 1511 "codecinfo-callback", &info_fn); 1512 if (!rv) 1513 return ENXIO; 1514 infocb = (void *)(uintptr_t)info_fn; 1515 err = infocb(fgrp_sc, request, response); 1516 break; 1517 case HDAUDIO_FGRP_WIDGET_INFO: 1518 rv = prop_dictionary_get_uint64(fgrp_dict, 1519 "widgetinfo-callback", &info_fn); 1520 if (!rv) 1521 return ENXIO; 1522 infocb = (void *)(uintptr_t)info_fn; 1523 err = infocb(fgrp_sc, request, response); 1524 break; 1525 default: 1526 err = EINVAL; 1527 break; 1528 } 1529 break; 1530 1531 default: 1532 err = EINVAL; 1533 break; 1534 } 1535 return err; 1536 } 1537 1538 int 1539 hdaudioopen(dev_t dev, int flag, int mode, struct lwp *l) 1540 { 1541 device_t self; 1542 1543 self = device_lookup(&hdaudio_cd, HDAUDIOUNIT(dev)); 1544 if (self == NULL) 1545 return ENXIO; 1546 1547 return 0; 1548 } 1549 1550 int 1551 hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l) 1552 { 1553 return 0; 1554 } 1555 1556 int 1557 hdaudioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1558 { 1559 struct hdaudio_softc *sc; 1560 struct plistref *pref = addr; 1561 prop_dictionary_t request, response; 1562 int err; 1563 1564 sc = device_lookup_private(&hdaudio_cd, HDAUDIOUNIT(dev)); 1565 if (sc == NULL) 1566 return ENXIO; 1567 1568 response = prop_dictionary_create(); 1569 if (response == NULL) 1570 return ENOMEM; 1571 1572 err = prop_dictionary_copyin_ioctl(pref, cmd, &request); 1573 if (err) { 1574 prop_object_release(response); 1575 return err; 1576 } 1577 1578 switch (cmd) { 1579 case HDAUDIO_FGRP_INFO: 1580 err = hdaudioioctl_fgrp_info(sc, request, response); 1581 break; 1582 case HDAUDIO_FGRP_GETCONFIG: 1583 err = hdaudioioctl_fgrp_getconfig(sc, request, response); 1584 break; 1585 case HDAUDIO_FGRP_SETCONFIG: 1586 err = hdaudioioctl_fgrp_setconfig(sc, request, response); 1587 break; 1588 case HDAUDIO_FGRP_CODEC_INFO: 1589 case HDAUDIO_FGRP_WIDGET_INFO: 1590 err = hdaudio_dispatch_fgrp_ioctl(sc, cmd, request, response); 1591 break; 1592 default: 1593 err = EINVAL; 1594 break; 1595 } 1596 1597 if (!err) 1598 err = prop_dictionary_copyout_ioctl(pref, cmd, response); 1599 1600 if (response) 1601 prop_object_release(response); 1602 prop_object_release(request); 1603 return err; 1604 } 1605 1606 MODULE(MODULE_CLASS_DRIVER, hdaudio, NULL); 1607 1608 static int 1609 hdaudio_modcmd(modcmd_t cmd, void *opaque) 1610 { 1611 int error = 0; 1612 #ifdef _MODULE 1613 int bmaj = -1, cmaj = -1; 1614 #endif 1615 1616 switch (cmd) { 1617 case MODULE_CMD_INIT: 1618 #ifdef _MODULE 1619 error = devsw_attach("hdaudio", NULL, &bmaj, 1620 &hdaudio_cdevsw, &cmaj); 1621 #endif 1622 return error; 1623 case MODULE_CMD_FINI: 1624 #ifdef _MODULE 1625 devsw_detach(NULL, &hdaudio_cdevsw); 1626 #endif 1627 return 0; 1628 default: 1629 return ENOTTY; 1630 } 1631 } 1632 1633 DEV_VERBOSE_DEFINE(hdaudio); 1634