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