hdaudio.c revision 1.1 1 /* $NetBSD: hdaudio.c,v 1.1 2015/03/28 14:09:59 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.1 2015/03/28 14:09:59 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 err = hdaudio_dma_alloc(sc, &dma, BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
1032 if (err)
1033 return NULL;
1034
1035 mutex_enter(&sc->sc_stream_mtx);
1036 for (i = 0; i < HDAUDIO_MAX_STREAMS; i++) {
1037 st = &sc->sc_stream[i];
1038 if (st->st_enable == false)
1039 break;
1040 if (st->st_type != type)
1041 continue;
1042 if (sc->sc_stream_mask & (1 << i))
1043 continue;
1044
1045 /* Allocate stream */
1046 st->st_bdl = dma;
1047 st->st_intr = intr;
1048 st->st_cookie = cookie;
1049 sc->sc_stream_mask |= (1 << i);
1050 mutex_exit(&sc->sc_stream_mtx);
1051 return st;
1052 }
1053 mutex_exit(&sc->sc_stream_mtx);
1054
1055 /* No streams of requested type available */
1056 hdaudio_dma_free(sc, &dma);
1057 return NULL;
1058 }
1059
1060 void
1061 hdaudio_stream_disestablish(struct hdaudio_stream *st)
1062 {
1063 struct hdaudio_softc *sc = st->st_host;
1064 struct hdaudio_dma dma;
1065
1066 KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
1067
1068 mutex_enter(&sc->sc_stream_mtx);
1069 sc->sc_stream_mask &= ~(1 << st->st_shift);
1070 st->st_intr = NULL;
1071 st->st_cookie = NULL;
1072 dma = st->st_bdl;
1073 st->st_bdl.dma_valid = false;
1074 mutex_exit(&sc->sc_stream_mtx);
1075
1076 /* Can't bus_dmamem_unmap while holding a mutex. */
1077 hdaudio_dma_free(sc, &dma);
1078 }
1079
1080 /*
1081 * Convert most of audio_params_t to stream fmt descriptor; noticably missing
1082 * is the # channels bits, as this is encoded differently in codec and
1083 * stream descriptors.
1084 *
1085 * TODO: validate that the stream and selected codecs can handle the fmt
1086 */
1087 uint16_t
1088 hdaudio_stream_param(struct hdaudio_stream *st, const audio_params_t *param)
1089 {
1090 uint16_t fmt = 0;
1091
1092 switch (param->encoding) {
1093 case AUDIO_ENCODING_AC3:
1094 fmt |= HDAUDIO_FMT_TYPE_NONPCM;
1095 break;
1096 default:
1097 fmt |= HDAUDIO_FMT_TYPE_PCM;
1098 break;
1099 }
1100
1101 switch (param->sample_rate) {
1102 case 8000:
1103 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
1104 HDAUDIO_FMT_DIV(6);
1105 break;
1106 case 11025:
1107 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
1108 HDAUDIO_FMT_DIV(4);
1109 break;
1110 case 16000:
1111 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
1112 HDAUDIO_FMT_DIV(3);
1113 break;
1114 case 22050:
1115 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
1116 HDAUDIO_FMT_DIV(2);
1117 break;
1118 case 32000:
1119 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2) |
1120 HDAUDIO_FMT_DIV(3);
1121 break;
1122 case 44100:
1123 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1);
1124 break;
1125 case 48000:
1126 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1);
1127 break;
1128 case 88200:
1129 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(2);
1130 break;
1131 case 96000:
1132 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2);
1133 break;
1134 case 176400:
1135 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(4);
1136 break;
1137 case 192000:
1138 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(4);
1139 break;
1140 default:
1141 return 0;
1142 }
1143
1144 if (param->precision == 16 && param->validbits == 8)
1145 fmt |= HDAUDIO_FMT_BITS_8_16;
1146 else if (param->precision == 16 && param->validbits == 16)
1147 fmt |= HDAUDIO_FMT_BITS_16_16;
1148 else if (param->precision == 32 && param->validbits == 20)
1149 fmt |= HDAUDIO_FMT_BITS_20_32;
1150 else if (param->precision == 32 && param->validbits == 24)
1151 fmt |= HDAUDIO_FMT_BITS_24_32;
1152 else if (param->precision == 32 && param->validbits == 32)
1153 fmt |= HDAUDIO_FMT_BITS_32_32;
1154 else
1155 return 0;
1156
1157 return fmt;
1158 }
1159
1160 void
1161 hdaudio_stream_reset(struct hdaudio_stream *st)
1162 {
1163 struct hdaudio_softc *sc = st->st_host;
1164 int snum = st->st_shift;
1165 int retry;
1166 uint8_t ctl0;
1167
1168 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1169 ctl0 |= HDAUDIO_CTL_SRST;
1170 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1171
1172 retry = HDAUDIO_RESET_TIMEOUT;
1173 do {
1174 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1175 if (ctl0 & HDAUDIO_CTL_SRST)
1176 break;
1177 hda_delay(10);
1178 } while (--retry > 0);
1179 if (retry == 0) {
1180 hda_error(sc, "timeout entering stream reset state\n");
1181 return;
1182 }
1183
1184 ctl0 &= ~HDAUDIO_CTL_SRST;
1185 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1186
1187 retry = HDAUDIO_RESET_TIMEOUT;
1188 do {
1189 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1190 if (!(ctl0 & HDAUDIO_CTL_SRST))
1191 break;
1192 hda_delay(10);
1193 } while (--retry > 0);
1194 if (retry == 0) {
1195 hda_error(sc, "timeout leaving stream reset state\n");
1196 return;
1197 }
1198 }
1199
1200 void
1201 hdaudio_stream_start(struct hdaudio_stream *st, int blksize,
1202 bus_size_t dmasize, const audio_params_t *params)
1203 {
1204 struct hdaudio_softc *sc = st->st_host;
1205 struct hdaudio_bdl_entry *bdl;
1206 uint64_t dmaaddr;
1207 uint32_t intctl;
1208 uint16_t fmt;
1209 uint8_t ctl0, ctl2;
1210 int cnt, snum = st->st_shift;
1211
1212 KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
1213 KASSERT(st->st_data.dma_valid == true);
1214 KASSERT(st->st_bdl.dma_valid == true);
1215
1216 hdaudio_stream_stop(st);
1217 hdaudio_stream_reset(st);
1218
1219 /*
1220 * Configure buffer descriptor list
1221 */
1222 dmaaddr = DMA_DMAADDR(&st->st_data);
1223 bdl = DMA_KERNADDR(&st->st_bdl);
1224 for (cnt = 0; cnt < HDAUDIO_BDL_MAX; cnt++) {
1225 bdl[cnt].address_lo = (uint32_t)dmaaddr;
1226 bdl[cnt].address_hi = dmaaddr >> 32;
1227 bdl[cnt].length = blksize;
1228 bdl[cnt].flags = HDAUDIO_BDL_ENTRY_IOC;
1229 dmaaddr += blksize;
1230 if (dmaaddr >= DMA_DMAADDR(&st->st_data) + dmasize) {
1231 cnt++;
1232 break;
1233 }
1234 }
1235
1236 /*
1237 * Program buffer descriptor list
1238 */
1239 dmaaddr = DMA_DMAADDR(&st->st_bdl);
1240 hda_write4(sc, HDAUDIO_SD_BDPL(snum), (uint32_t)dmaaddr);
1241 hda_write4(sc, HDAUDIO_SD_BDPU(snum), (uint32_t)(dmaaddr >> 32));
1242 hda_write2(sc, HDAUDIO_SD_LVI(snum), (cnt - 1) & 0xff);
1243
1244 /*
1245 * Program cyclic buffer length
1246 */
1247 hda_write4(sc, HDAUDIO_SD_CBL(snum), dmasize);
1248
1249 /*
1250 * Program stream number (tag). Although controller hardware is
1251 * capable of transmitting any stream number (0-15), by convention
1252 * stream 0 is reserved as unused by software, so that converters
1253 * whose stream numbers have been reset to 0 do not unintentionally
1254 * decode data not intended for them.
1255 */
1256 ctl2 = hda_read1(sc, HDAUDIO_SD_CTL2(snum));
1257 ctl2 &= ~0xf0;
1258 ctl2 |= hdaudio_stream_tag(st) << 4;
1259 hda_write1(sc, HDAUDIO_SD_CTL2(snum), ctl2);
1260
1261 /*
1262 * Program stream format
1263 */
1264 fmt = hdaudio_stream_param(st, params) |
1265 HDAUDIO_FMT_CHAN(params->channels);
1266 hda_write2(sc, HDAUDIO_SD_FMT(snum), fmt);
1267
1268 /*
1269 * Switch on interrupts for this stream
1270 */
1271 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
1272 intctl |= (1 << st->st_shift);
1273 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
1274
1275 /*
1276 * Start running the stream
1277 */
1278 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1279 ctl0 |= HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
1280 HDAUDIO_CTL_RUN;
1281 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1282 }
1283
1284 void
1285 hdaudio_stream_stop(struct hdaudio_stream *st)
1286 {
1287 struct hdaudio_softc *sc = st->st_host;
1288 uint32_t intctl;
1289 uint8_t ctl0;
1290 int snum = st->st_shift;
1291
1292 /*
1293 * Stop running the stream
1294 */
1295 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1296 ctl0 &= ~(HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
1297 HDAUDIO_CTL_RUN);
1298 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1299
1300 /*
1301 * Switch off interrupts for this stream
1302 */
1303 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
1304 intctl &= ~(1 << st->st_shift);
1305 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
1306 }
1307
1308 /*
1309 * /dev/hdaudioN interface
1310 */
1311
1312 static const char *
1313 hdaudioioctl_fgrp_to_cstr(enum function_group_type type)
1314 {
1315 switch (type) {
1316 case HDAUDIO_GROUP_TYPE_AFG:
1317 return "afg";
1318 case HDAUDIO_GROUP_TYPE_VSM_FG:
1319 return "vsmfg";
1320 default:
1321 return "unknown";
1322 }
1323 }
1324
1325 static struct hdaudio_function_group *
1326 hdaudioioctl_fgrp_lookup(struct hdaudio_softc *sc, int codecid, int nid)
1327 {
1328 struct hdaudio_codec *co;
1329 struct hdaudio_function_group *fg = NULL;
1330 int i;
1331
1332 if (codecid < 0 || codecid >= HDAUDIO_MAX_CODECS)
1333 return NULL;
1334 co = &sc->sc_codec[codecid];
1335 if (co->co_valid == false)
1336 return NULL;
1337
1338 for (i = 0; i < co->co_nfg; i++)
1339 if (co->co_fg[i].fg_nid == nid) {
1340 fg = &co->co_fg[i];
1341 break;
1342 }
1343
1344 return fg;
1345 }
1346
1347 static int
1348 hdaudioioctl_fgrp_info(struct hdaudio_softc *sc, prop_dictionary_t request,
1349 prop_dictionary_t response)
1350 {
1351 struct hdaudio_codec *co;
1352 struct hdaudio_function_group *fg;
1353 prop_array_t array;
1354 prop_dictionary_t dict;
1355 int codecid, fgid;
1356
1357 array = prop_array_create();
1358 if (array == NULL)
1359 return ENOMEM;
1360
1361 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) {
1362 co = &sc->sc_codec[codecid];
1363 if (co->co_valid == false)
1364 continue;
1365 for (fgid = 0; fgid < co->co_nfg; fgid++) {
1366 fg = &co->co_fg[fgid];
1367 dict = prop_dictionary_create();
1368 if (dict == NULL)
1369 return ENOMEM;
1370 prop_dictionary_set_cstring_nocopy(dict,
1371 "type", hdaudioioctl_fgrp_to_cstr(fg->fg_type));
1372 prop_dictionary_set_int16(dict, "nid", fg->fg_nid);
1373 prop_dictionary_set_int16(dict, "codecid", codecid);
1374 prop_dictionary_set_uint16(dict, "vendor-id",
1375 fg->fg_vendor);
1376 prop_dictionary_set_uint16(dict, "product-id",
1377 fg->fg_product);
1378 prop_dictionary_set_uint32(dict, "subsystem-id",
1379 sc->sc_subsystem);
1380 if (fg->fg_device)
1381 prop_dictionary_set_cstring(dict, "device",
1382 device_xname(fg->fg_device));
1383 else
1384 prop_dictionary_set_cstring_nocopy(dict,
1385 "device", "<none>");
1386 prop_array_add(array, dict);
1387 }
1388 }
1389
1390 prop_dictionary_set(response, "function-group-info", array);
1391 return 0;
1392 }
1393
1394 static int
1395 hdaudioioctl_fgrp_getconfig(struct hdaudio_softc *sc,
1396 prop_dictionary_t request, prop_dictionary_t response)
1397 {
1398 struct hdaudio_function_group *fg;
1399 prop_dictionary_t dict;
1400 prop_array_t array;
1401 uint32_t nodecnt, wcap, config;
1402 int16_t codecid, nid, i;
1403 int startnode, endnode;
1404
1405 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1406 !prop_dictionary_get_int16(request, "nid", &nid))
1407 return EINVAL;
1408
1409 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1410 if (fg == NULL)
1411 return ENODEV;
1412
1413 array = prop_array_create();
1414 if (array == NULL)
1415 return ENOMEM;
1416
1417 nodecnt = hdaudio_command(fg->fg_codec, fg->fg_nid,
1418 CORB_GET_PARAMETER, COP_SUBORDINATE_NODE_COUNT);
1419 startnode = COP_NODECNT_STARTNODE(nodecnt);
1420 endnode = startnode + COP_NODECNT_NUMNODES(nodecnt);
1421
1422 for (i = startnode; i < endnode; i++) {
1423 wcap = hdaudio_command(fg->fg_codec, i,
1424 CORB_GET_PARAMETER, COP_AUDIO_WIDGET_CAPABILITIES);
1425 if (COP_AWCAP_TYPE(wcap) != COP_AWCAP_TYPE_PIN_COMPLEX)
1426 continue;
1427 config = hdaudio_command(fg->fg_codec, i,
1428 CORB_GET_CONFIGURATION_DEFAULT, 0);
1429 dict = prop_dictionary_create();
1430 if (dict == NULL)
1431 return ENOMEM;
1432 prop_dictionary_set_int16(dict, "nid", i);
1433 prop_dictionary_set_uint32(dict, "config", config);
1434 prop_array_add(array, dict);
1435 }
1436
1437 prop_dictionary_set(response, "pin-config", array);
1438
1439 return 0;
1440 }
1441
1442 static int
1443 hdaudioioctl_fgrp_setconfig(struct hdaudio_softc *sc,
1444 prop_dictionary_t request, prop_dictionary_t response)
1445 {
1446 struct hdaudio_function_group *fg;
1447 prop_array_t config;
1448 int16_t codecid, nid;
1449 int err;
1450
1451 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1452 !prop_dictionary_get_int16(request, "nid", &nid))
1453 return EINVAL;
1454
1455 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1456 if (fg == NULL)
1457 return ENODEV;
1458
1459 if (fg->fg_device) {
1460 err = config_detach(fg->fg_device, 0);
1461 if (err)
1462 return err;
1463 fg->fg_device = NULL;
1464 }
1465
1466 /* "pin-config" may be NULL, this means "use BIOS configuration" */
1467 config = prop_dictionary_get(request, "pin-config");
1468 if (config && prop_object_type(config) != PROP_TYPE_ARRAY) {
1469 prop_object_release(config);
1470 return EINVAL;
1471 }
1472 hdaudio_attach_fg(fg, config);
1473 if (config)
1474 prop_object_release(config);
1475
1476 return 0;
1477 }
1478
1479 static int
1480 hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc *sc, u_long cmd,
1481 prop_dictionary_t request, prop_dictionary_t response)
1482 {
1483 struct hdaudio_function_group *fg;
1484 int (*infocb)(void *, prop_dictionary_t, prop_dictionary_t);
1485 prop_dictionary_t fgrp_dict;
1486 uint64_t info_fn;
1487 int16_t codecid, nid;
1488 void *fgrp_sc;
1489 bool rv;
1490 int err;
1491
1492 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1493 !prop_dictionary_get_int16(request, "nid", &nid))
1494 return EINVAL;
1495
1496 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1497 if (fg == NULL)
1498 return ENODEV;
1499 if (fg->fg_device == NULL)
1500 return ENXIO;
1501 fgrp_sc = device_private(fg->fg_device);
1502 fgrp_dict = device_properties(fg->fg_device);
1503
1504 switch (fg->fg_type) {
1505 case HDAUDIO_GROUP_TYPE_AFG:
1506 switch (cmd) {
1507 case HDAUDIO_FGRP_CODEC_INFO:
1508 rv = prop_dictionary_get_uint64(fgrp_dict,
1509 "codecinfo-callback", &info_fn);
1510 if (!rv)
1511 return ENXIO;
1512 infocb = (void *)(uintptr_t)info_fn;
1513 err = infocb(fgrp_sc, request, response);
1514 break;
1515 case HDAUDIO_FGRP_WIDGET_INFO:
1516 rv = prop_dictionary_get_uint64(fgrp_dict,
1517 "widgetinfo-callback", &info_fn);
1518 if (!rv)
1519 return ENXIO;
1520 infocb = (void *)(uintptr_t)info_fn;
1521 err = infocb(fgrp_sc, request, response);
1522 break;
1523 default:
1524 err = EINVAL;
1525 break;
1526 }
1527 break;
1528
1529 default:
1530 err = EINVAL;
1531 break;
1532 }
1533 return err;
1534 }
1535
1536 int
1537 hdaudioopen(dev_t dev, int flag, int mode, struct lwp *l)
1538 {
1539 device_t self;
1540
1541 self = device_lookup(&hdaudio_cd, HDAUDIOUNIT(dev));
1542 if (self == NULL)
1543 return ENXIO;
1544
1545 return 0;
1546 }
1547
1548 int
1549 hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
1550 {
1551 return 0;
1552 }
1553
1554 int
1555 hdaudioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1556 {
1557 struct hdaudio_softc *sc;
1558 struct plistref *pref = addr;
1559 prop_dictionary_t request, response;
1560 int err;
1561
1562 sc = device_lookup_private(&hdaudio_cd, HDAUDIOUNIT(dev));
1563 if (sc == NULL)
1564 return ENXIO;
1565
1566 response = prop_dictionary_create();
1567 if (response == NULL)
1568 return ENOMEM;
1569
1570 err = prop_dictionary_copyin_ioctl(pref, cmd, &request);
1571 if (err) {
1572 prop_object_release(response);
1573 return err;
1574 }
1575
1576 switch (cmd) {
1577 case HDAUDIO_FGRP_INFO:
1578 err = hdaudioioctl_fgrp_info(sc, request, response);
1579 break;
1580 case HDAUDIO_FGRP_GETCONFIG:
1581 err = hdaudioioctl_fgrp_getconfig(sc, request, response);
1582 break;
1583 case HDAUDIO_FGRP_SETCONFIG:
1584 err = hdaudioioctl_fgrp_setconfig(sc, request, response);
1585 break;
1586 case HDAUDIO_FGRP_CODEC_INFO:
1587 case HDAUDIO_FGRP_WIDGET_INFO:
1588 err = hdaudio_dispatch_fgrp_ioctl(sc, cmd, request, response);
1589 break;
1590 default:
1591 err = EINVAL;
1592 break;
1593 }
1594
1595 if (!err)
1596 err = prop_dictionary_copyout_ioctl(pref, cmd, response);
1597
1598 if (response)
1599 prop_object_release(response);
1600 prop_object_release(request);
1601 return err;
1602 }
1603
1604 MODULE(MODULE_CLASS_DRIVER, hdaudio, NULL);
1605
1606 static int
1607 hdaudio_modcmd(modcmd_t cmd, void *opaque)
1608 {
1609 int error = 0;
1610 #ifdef _MODULE
1611 int bmaj = -1, cmaj = -1;
1612 #endif
1613
1614 switch (cmd) {
1615 case MODULE_CMD_INIT:
1616 #ifdef _MODULE
1617 error = devsw_attach("hdaudio", NULL, &bmaj,
1618 &hdaudio_cdevsw, &cmaj);
1619 #endif
1620 return error;
1621 case MODULE_CMD_FINI:
1622 #ifdef _MODULE
1623 devsw_detach(NULL, &hdaudio_cdevsw);
1624 #endif
1625 return 0;
1626 default:
1627 return ENOTTY;
1628 }
1629 }
1630
1631 DEV_VERBOSE_DEFINE(hdaudio);
1632