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