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