Home | History | Annotate | Line # | Download | only in hdaudio
hdaudio.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: hdaudio.c,v 1.1 2015/03/28 14:09:59 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: hdaudio.c,v 1.1 2015/03/28 14:09:59 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.1  jmcneill 		hda_write4(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
    432  1.1  jmcneill 		do {
    433  1.1  jmcneill 			hda_delay(10);
    434  1.1  jmcneill 			corbctl = hda_read4(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.1  jmcneill 		hda_write4(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
    456  1.1  jmcneill 		do {
    457  1.1  jmcneill 			hda_delay(10);
    458  1.1  jmcneill 			corbctl = hda_read4(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.1  jmcneill 	/* If the controller isn't in reset state, initiate the transition */
    616  1.1  jmcneill 	gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
    617  1.1  jmcneill 	if (gctl & HDAUDIO_GCTL_CRST) {
    618  1.1  jmcneill 		gctl &= ~HDAUDIO_GCTL_CRST;
    619  1.1  jmcneill 		hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl);
    620  1.1  jmcneill 		do {
    621  1.1  jmcneill 			hda_delay(10);
    622  1.1  jmcneill 			gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
    623  1.1  jmcneill 		} while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) != 0);
    624  1.1  jmcneill 		if (retry == 0) {
    625  1.1  jmcneill 			hda_error(sc, "timeout entering reset state\n");
    626  1.1  jmcneill 			return ETIME;
    627  1.1  jmcneill 		}
    628  1.1  jmcneill 	}
    629  1.1  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.1  jmcneill 	/* Accept unsolicited responses */
    643  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_UNSOL_EN);
    644  1.1  jmcneill 
    645  1.1  jmcneill 	return 0;
    646  1.1  jmcneill }
    647  1.1  jmcneill 
    648  1.1  jmcneill static void
    649  1.1  jmcneill hdaudio_intr_enable(struct hdaudio_softc *sc)
    650  1.1  jmcneill {
    651  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTSTS,
    652  1.1  jmcneill 	    hda_read4(sc, HDAUDIO_MMIO_INTSTS));
    653  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTCTL,
    654  1.1  jmcneill 	    HDAUDIO_INTCTL_GIE | HDAUDIO_INTCTL_CIE);
    655  1.1  jmcneill }
    656  1.1  jmcneill 
    657  1.1  jmcneill static void
    658  1.1  jmcneill hdaudio_intr_disable(struct hdaudio_softc *sc)
    659  1.1  jmcneill {
    660  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0);
    661  1.1  jmcneill }
    662  1.1  jmcneill 
    663  1.1  jmcneill static int
    664  1.1  jmcneill hdaudio_config_print(void *opaque, const char *pnp)
    665  1.1  jmcneill {
    666  1.1  jmcneill 	prop_dictionary_t dict = opaque;
    667  1.1  jmcneill 	uint8_t fgtype, nid;
    668  1.1  jmcneill 	uint16_t vendor, product;
    669  1.1  jmcneill 	const char *type = "unknown";
    670  1.1  jmcneill 
    671  1.1  jmcneill 	prop_dictionary_get_uint8(dict, "function-group-type", &fgtype);
    672  1.1  jmcneill 	prop_dictionary_get_uint8(dict, "node-id", &nid);
    673  1.1  jmcneill 	prop_dictionary_get_uint16(dict, "vendor-id", &vendor);
    674  1.1  jmcneill 	prop_dictionary_get_uint16(dict, "product-id", &product);
    675  1.1  jmcneill 	if (pnp) {
    676  1.1  jmcneill 		if (fgtype == HDAUDIO_GROUP_TYPE_AFG)
    677  1.1  jmcneill 			type = "hdafg";
    678  1.1  jmcneill 		else if (fgtype == HDAUDIO_GROUP_TYPE_VSM_FG)
    679  1.1  jmcneill 			type = "hdvsmfg";
    680  1.1  jmcneill 
    681  1.1  jmcneill 		aprint_normal("%s at %s", type, pnp);
    682  1.1  jmcneill 	}
    683  1.1  jmcneill 	aprint_debug(" vendor 0x%04X product 0x%04X nid 0x%02X",
    684  1.1  jmcneill 	    vendor, product, nid);
    685  1.1  jmcneill 
    686  1.1  jmcneill 	return UNCONF;
    687  1.1  jmcneill }
    688  1.1  jmcneill 
    689  1.1  jmcneill static void
    690  1.1  jmcneill hdaudio_attach_fg(struct hdaudio_function_group *fg, prop_array_t config)
    691  1.1  jmcneill {
    692  1.1  jmcneill 	struct hdaudio_codec *co = fg->fg_codec;
    693  1.1  jmcneill 	struct hdaudio_softc *sc = co->co_host;
    694  1.1  jmcneill 	prop_dictionary_t args = prop_dictionary_create();
    695  1.1  jmcneill 	uint64_t fgptr = (vaddr_t)fg;
    696  1.1  jmcneill 	int locs[1];
    697  1.1  jmcneill 
    698  1.1  jmcneill 	prop_dictionary_set_uint8(args, "function-group-type", fg->fg_type);
    699  1.1  jmcneill 	prop_dictionary_set_uint64(args, "function-group", fgptr);
    700  1.1  jmcneill 	prop_dictionary_set_uint8(args, "node-id", fg->fg_nid);
    701  1.1  jmcneill 	prop_dictionary_set_uint16(args, "vendor-id", fg->fg_vendor);
    702  1.1  jmcneill 	prop_dictionary_set_uint16(args, "product-id", fg->fg_product);
    703  1.1  jmcneill 	if (config)
    704  1.1  jmcneill 		prop_dictionary_set(args, "pin-config", config);
    705  1.1  jmcneill 
    706  1.1  jmcneill 	locs[0] = fg->fg_nid;
    707  1.1  jmcneill 
    708  1.1  jmcneill 	fg->fg_device = config_found_sm_loc(sc->sc_dev, "hdaudiobus",
    709  1.1  jmcneill 	    locs, args, hdaudio_config_print, config_stdsubmatch);
    710  1.1  jmcneill 
    711  1.1  jmcneill 	prop_object_release(args);
    712  1.1  jmcneill }
    713  1.1  jmcneill 
    714  1.1  jmcneill static void
    715  1.1  jmcneill hdaudio_codec_attach(struct hdaudio_codec *co)
    716  1.1  jmcneill {
    717  1.1  jmcneill 	struct hdaudio_function_group *fg;
    718  1.1  jmcneill 	uint32_t vid, snc, fgrp;
    719  1.1  jmcneill 	int starting_node, num_nodes, nid;
    720  1.1  jmcneill 
    721  1.1  jmcneill 	if (co->co_valid == false)
    722  1.1  jmcneill 		return;
    723  1.1  jmcneill 
    724  1.1  jmcneill 	vid = hdaudio_command(co, 0, CORB_GET_PARAMETER, COP_VENDOR_ID);
    725  1.1  jmcneill 	snc = hdaudio_command(co, 0, CORB_GET_PARAMETER,
    726  1.1  jmcneill 	    COP_SUBORDINATE_NODE_COUNT);
    727  1.1  jmcneill 
    728  1.1  jmcneill 	/* make sure the vendor and product IDs are valid */
    729  1.1  jmcneill 	if (vid == 0xffffffff || vid == 0x00000000)
    730  1.1  jmcneill 		return;
    731  1.1  jmcneill 
    732  1.1  jmcneill #ifdef HDAUDIO_DEBUG
    733  1.1  jmcneill 	struct hdaudio_softc *sc = co->co_host;
    734  1.1  jmcneill 	uint32_t rid = hdaudio_command(co, 0, CORB_GET_PARAMETER,
    735  1.1  jmcneill 	    COP_REVISION_ID);
    736  1.1  jmcneill 	hda_print(sc, "Codec%02X: %04X:%04X HDA %d.%d rev %d stepping %d\n",
    737  1.1  jmcneill 	    co->co_addr, vid >> 16, vid & 0xffff,
    738  1.1  jmcneill 	    (rid >> 20) & 0xf, (rid >> 16) & 0xf,
    739  1.1  jmcneill 	    (rid >> 8) & 0xff, rid & 0xff);
    740  1.1  jmcneill #endif
    741  1.1  jmcneill 	starting_node = (snc >> 16) & 0xff;
    742  1.1  jmcneill 	num_nodes = snc & 0xff;
    743  1.1  jmcneill 
    744  1.1  jmcneill 	co->co_nfg = num_nodes;
    745  1.1  jmcneill 	co->co_fg = kmem_zalloc(co->co_nfg * sizeof(*co->co_fg), KM_SLEEP);
    746  1.1  jmcneill 
    747  1.1  jmcneill 	for (nid = starting_node; nid < starting_node + num_nodes; nid++) {
    748  1.1  jmcneill 		fg = &co->co_fg[nid - starting_node];
    749  1.1  jmcneill 		fg->fg_codec = co;
    750  1.1  jmcneill 		fg->fg_nid = nid;
    751  1.1  jmcneill 		fg->fg_vendor = vid >> 16;
    752  1.1  jmcneill 		fg->fg_product = vid & 0xffff;
    753  1.1  jmcneill 
    754  1.1  jmcneill 		fgrp = hdaudio_command(co, nid, CORB_GET_PARAMETER,
    755  1.1  jmcneill 		    COP_FUNCTION_GROUP_TYPE);
    756  1.1  jmcneill 		switch (fgrp & 0xff) {
    757  1.1  jmcneill 		case 0x01:	/* Audio Function Group */
    758  1.1  jmcneill 			fg->fg_type = HDAUDIO_GROUP_TYPE_AFG;
    759  1.1  jmcneill 			break;
    760  1.1  jmcneill 		case 0x02:	/* Vendor Specific Modem Function Group */
    761  1.1  jmcneill 			fg->fg_type = HDAUDIO_GROUP_TYPE_VSM_FG;
    762  1.1  jmcneill 			break;
    763  1.1  jmcneill 		default:
    764  1.1  jmcneill 			/* Function group type not supported */
    765  1.1  jmcneill 			fg->fg_type = HDAUDIO_GROUP_TYPE_UNKNOWN;
    766  1.1  jmcneill 			break;
    767  1.1  jmcneill 		}
    768  1.1  jmcneill 		hdaudio_attach_fg(fg, NULL);
    769  1.1  jmcneill 	}
    770  1.1  jmcneill }
    771  1.1  jmcneill 
    772  1.1  jmcneill int
    773  1.1  jmcneill hdaudio_stream_tag(struct hdaudio_stream *st)
    774  1.1  jmcneill {
    775  1.1  jmcneill 	int ret = 0;
    776  1.1  jmcneill 
    777  1.1  jmcneill 	switch (st->st_type) {
    778  1.1  jmcneill 	case HDAUDIO_STREAM_ISS:
    779  1.1  jmcneill 		ret = 1;
    780  1.1  jmcneill 		break;
    781  1.1  jmcneill 	case HDAUDIO_STREAM_OSS:
    782  1.1  jmcneill 		ret = 2;
    783  1.1  jmcneill 		break;
    784  1.1  jmcneill 	case HDAUDIO_STREAM_BSS:
    785  1.1  jmcneill 		ret = 3;
    786  1.1  jmcneill 		break;
    787  1.1  jmcneill 	}
    788  1.1  jmcneill 
    789  1.1  jmcneill 	return ret;
    790  1.1  jmcneill }
    791  1.1  jmcneill 
    792  1.1  jmcneill int
    793  1.1  jmcneill hdaudio_attach(device_t dev, struct hdaudio_softc *sc)
    794  1.1  jmcneill {
    795  1.1  jmcneill 	int err, i;
    796  1.1  jmcneill 
    797  1.1  jmcneill 	KASSERT(sc->sc_memvalid == true);
    798  1.1  jmcneill 
    799  1.1  jmcneill 	sc->sc_dev = dev;
    800  1.1  jmcneill 	mutex_init(&sc->sc_corb_mtx, MUTEX_DEFAULT, IPL_AUDIO);
    801  1.1  jmcneill 	mutex_init(&sc->sc_stream_mtx, MUTEX_DEFAULT, IPL_AUDIO);
    802  1.1  jmcneill 
    803  1.1  jmcneill 	hdaudio_init(sc);
    804  1.1  jmcneill 
    805  1.1  jmcneill 	/*
    806  1.1  jmcneill 	 * Put the controller into a known state by entering and leaving
    807  1.1  jmcneill 	 * CRST as necessary.
    808  1.1  jmcneill 	 */
    809  1.1  jmcneill 	if ((err = hdaudio_reset(sc)) != 0)
    810  1.1  jmcneill 		goto fail;
    811  1.1  jmcneill 
    812  1.1  jmcneill 	/*
    813  1.1  jmcneill 	 * From the spec:
    814  1.1  jmcneill 	 *
    815  1.1  jmcneill 	 * Must wait 250us after reading CRST as a 1 before assuming that
    816  1.1  jmcneill 	 * codecs have all made status change requests and have been
    817  1.1  jmcneill 	 * registered by the controller.
    818  1.1  jmcneill 	 *
    819  1.1  jmcneill 	 * In reality, we need to wait longer than this.
    820  1.1  jmcneill 	 */
    821  1.1  jmcneill 	hda_delay(HDAUDIO_CODEC_DELAY);
    822  1.1  jmcneill 	if (hdaudio_codec_probe(sc) == 0) {
    823  1.1  jmcneill 		hda_error(sc, "no codecs found\n");
    824  1.1  jmcneill 		err = ENODEV;
    825  1.1  jmcneill 		goto fail;
    826  1.1  jmcneill 	}
    827  1.1  jmcneill 
    828  1.1  jmcneill 	/*
    829  1.1  jmcneill 	 * Ensure that the device is in a known state
    830  1.1  jmcneill 	 */
    831  1.1  jmcneill 	hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE);
    832  1.1  jmcneill 	hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
    833  1.1  jmcneill 	    HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL);
    834  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTSTS,
    835  1.1  jmcneill 	    hda_read4(sc, HDAUDIO_MMIO_INTSTS));
    836  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0);
    837  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0);
    838  1.1  jmcneill 
    839  1.1  jmcneill 	/*
    840  1.1  jmcneill 	 * Initialize the CORB. First negotiate a command buffer size,
    841  1.1  jmcneill 	 * then allocate and configure it.
    842  1.1  jmcneill 	 */
    843  1.1  jmcneill 	if ((err = hdaudio_corb_setsize(sc)) != 0)
    844  1.1  jmcneill 		goto fail;
    845  1.1  jmcneill 	if ((err = hdaudio_dma_alloc(sc, &sc->sc_corb, BUS_DMA_WRITE)) != 0)
    846  1.1  jmcneill 		goto fail;
    847  1.1  jmcneill 	if ((err = hdaudio_corb_config(sc)) != 0)
    848  1.1  jmcneill 		goto fail;
    849  1.1  jmcneill 
    850  1.1  jmcneill 	/*
    851  1.1  jmcneill 	 * Initialize the RIRB.
    852  1.1  jmcneill 	 */
    853  1.1  jmcneill 	if ((err = hdaudio_rirb_setsize(sc)) != 0)
    854  1.1  jmcneill 		goto fail;
    855  1.1  jmcneill 	if ((err = hdaudio_dma_alloc(sc, &sc->sc_rirb, BUS_DMA_READ)) != 0)
    856  1.1  jmcneill 		goto fail;
    857  1.1  jmcneill 	if ((err = hdaudio_rirb_config(sc)) != 0)
    858  1.1  jmcneill 		goto fail;
    859  1.1  jmcneill 
    860  1.1  jmcneill 	/*
    861  1.1  jmcneill 	 * Start the CORB and RIRB
    862  1.1  jmcneill 	 */
    863  1.1  jmcneill 	if ((err = hdaudio_corb_start(sc)) != 0)
    864  1.1  jmcneill 		goto fail;
    865  1.1  jmcneill 	if ((err = hdaudio_rirb_start(sc)) != 0)
    866  1.1  jmcneill 		goto fail;
    867  1.1  jmcneill 
    868  1.1  jmcneill 	/*
    869  1.1  jmcneill 	 * Identify and attach discovered codecs
    870  1.1  jmcneill 	 */
    871  1.1  jmcneill 	for (i = 0; i < HDAUDIO_MAX_CODECS; i++)
    872  1.1  jmcneill 		hdaudio_codec_attach(&sc->sc_codec[i]);
    873  1.1  jmcneill 
    874  1.1  jmcneill 	/*
    875  1.1  jmcneill 	 * Enable interrupts
    876  1.1  jmcneill 	 */
    877  1.1  jmcneill 	hdaudio_intr_enable(sc);
    878  1.1  jmcneill 
    879  1.1  jmcneill fail:
    880  1.1  jmcneill 	if (err)
    881  1.1  jmcneill 		hda_error(sc, "device driver failed to attach\n");
    882  1.1  jmcneill 	return err;
    883  1.1  jmcneill }
    884  1.1  jmcneill 
    885  1.1  jmcneill int
    886  1.1  jmcneill hdaudio_detach(struct hdaudio_softc *sc, int flags)
    887  1.1  jmcneill {
    888  1.1  jmcneill 	int error;
    889  1.1  jmcneill 
    890  1.1  jmcneill 	/* Disable interrupts */
    891  1.1  jmcneill 	hdaudio_intr_disable(sc);
    892  1.1  jmcneill 
    893  1.1  jmcneill 	error = config_detach_children(sc->sc_dev, flags);
    894  1.1  jmcneill 	if (error != 0) {
    895  1.1  jmcneill 		hdaudio_intr_enable(sc);
    896  1.1  jmcneill 		return error;
    897  1.1  jmcneill 	}
    898  1.1  jmcneill 
    899  1.1  jmcneill 	mutex_destroy(&sc->sc_corb_mtx);
    900  1.1  jmcneill 	mutex_destroy(&sc->sc_stream_mtx);
    901  1.1  jmcneill 
    902  1.1  jmcneill 	hdaudio_dma_free(sc, &sc->sc_corb);
    903  1.1  jmcneill 	hdaudio_dma_free(sc, &sc->sc_rirb);
    904  1.1  jmcneill 
    905  1.1  jmcneill 	return 0;
    906  1.1  jmcneill }
    907  1.1  jmcneill 
    908  1.1  jmcneill bool
    909  1.1  jmcneill hdaudio_resume(struct hdaudio_softc *sc)
    910  1.1  jmcneill {
    911  1.1  jmcneill 	if (hdaudio_reset(sc) != 0)
    912  1.1  jmcneill 		return false;
    913  1.1  jmcneill 
    914  1.1  jmcneill 	hda_delay(HDAUDIO_CODEC_DELAY);
    915  1.1  jmcneill 
    916  1.1  jmcneill 	/*
    917  1.1  jmcneill 	 * Ensure that the device is in a known state
    918  1.1  jmcneill 	 */
    919  1.1  jmcneill 	hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE);
    920  1.1  jmcneill 	hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
    921  1.1  jmcneill 	    HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL);
    922  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTSTS,
    923  1.1  jmcneill 	    hda_read4(sc, HDAUDIO_MMIO_INTSTS));
    924  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0);
    925  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0);
    926  1.1  jmcneill 
    927  1.1  jmcneill 	if (hdaudio_corb_config(sc) != 0)
    928  1.1  jmcneill 		return false;
    929  1.1  jmcneill 	if (hdaudio_rirb_config(sc) != 0)
    930  1.1  jmcneill 		return false;
    931  1.1  jmcneill 	if (hdaudio_corb_start(sc) != 0)
    932  1.1  jmcneill 		return false;
    933  1.1  jmcneill 	if (hdaudio_rirb_start(sc) != 0)
    934  1.1  jmcneill 		return false;
    935  1.1  jmcneill 
    936  1.1  jmcneill 	hdaudio_intr_enable(sc);
    937  1.1  jmcneill 
    938  1.1  jmcneill 	return true;
    939  1.1  jmcneill }
    940  1.1  jmcneill 
    941  1.1  jmcneill int
    942  1.1  jmcneill hdaudio_rescan(struct hdaudio_softc *sc, const char *ifattr, const int *locs)
    943  1.1  jmcneill {
    944  1.1  jmcneill 	struct hdaudio_codec *co;
    945  1.1  jmcneill 	struct hdaudio_function_group *fg;
    946  1.1  jmcneill 	unsigned int codec;
    947  1.1  jmcneill 
    948  1.1  jmcneill 	if (!ifattr_match(ifattr, "hdaudiobus"))
    949  1.1  jmcneill 		return 0;
    950  1.1  jmcneill 
    951  1.1  jmcneill 	for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) {
    952  1.1  jmcneill 		co = &sc->sc_codec[codec];
    953  1.1  jmcneill 		fg = co->co_fg;
    954  1.1  jmcneill 		if (!co->co_valid || fg == NULL)
    955  1.1  jmcneill 			continue;
    956  1.1  jmcneill 		if (fg->fg_device)
    957  1.1  jmcneill 			continue;
    958  1.1  jmcneill 		hdaudio_attach_fg(fg, NULL);
    959  1.1  jmcneill 	}
    960  1.1  jmcneill 
    961  1.1  jmcneill 	return 0;
    962  1.1  jmcneill }
    963  1.1  jmcneill 
    964  1.1  jmcneill void
    965  1.1  jmcneill hdaudio_childdet(struct hdaudio_softc *sc, device_t child)
    966  1.1  jmcneill {
    967  1.1  jmcneill 	struct hdaudio_codec *co;
    968  1.1  jmcneill 	struct hdaudio_function_group *fg;
    969  1.1  jmcneill 	unsigned int codec;
    970  1.1  jmcneill 
    971  1.1  jmcneill 	for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) {
    972  1.1  jmcneill 		co = &sc->sc_codec[codec];
    973  1.1  jmcneill 		fg = co->co_fg;
    974  1.1  jmcneill 		if (!co->co_valid || fg == NULL)
    975  1.1  jmcneill 			continue;
    976  1.1  jmcneill 		if (fg->fg_device == child)
    977  1.1  jmcneill 			fg->fg_device = NULL;
    978  1.1  jmcneill 	}
    979  1.1  jmcneill }
    980  1.1  jmcneill 
    981  1.1  jmcneill int
    982  1.1  jmcneill hdaudio_intr(struct hdaudio_softc *sc)
    983  1.1  jmcneill {
    984  1.1  jmcneill 	struct hdaudio_stream *st;
    985  1.1  jmcneill 	uint32_t intsts, stream_mask;
    986  1.1  jmcneill 	int streamid = 0;
    987  1.1  jmcneill 	uint8_t rirbsts;
    988  1.1  jmcneill 
    989  1.1  jmcneill 	intsts = hda_read4(sc, HDAUDIO_MMIO_INTSTS);
    990  1.1  jmcneill 	if (!(intsts & HDAUDIO_INTSTS_GIS))
    991  1.1  jmcneill 		return 0;
    992  1.1  jmcneill 
    993  1.1  jmcneill 	if (intsts & HDAUDIO_INTSTS_CIS) {
    994  1.1  jmcneill 		rirbsts = hda_read1(sc, HDAUDIO_MMIO_RIRBSTS);
    995  1.1  jmcneill 		if (rirbsts & HDAUDIO_RIRBSTS_RINTFL) {
    996  1.1  jmcneill 			mutex_enter(&sc->sc_corb_mtx);
    997  1.1  jmcneill 			hdaudio_rirb_dequeue(sc, true);
    998  1.1  jmcneill 			mutex_exit(&sc->sc_corb_mtx);
    999  1.1  jmcneill 		}
   1000  1.1  jmcneill 		if (rirbsts & (HDAUDIO_RIRBSTS_RIRBOIS|HDAUDIO_RIRBSTS_RINTFL))
   1001  1.1  jmcneill 			hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, rirbsts);
   1002  1.1  jmcneill 		hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_CIS);
   1003  1.1  jmcneill 	}
   1004  1.1  jmcneill 	if (intsts & HDAUDIO_INTSTS_SIS_MASK) {
   1005  1.1  jmcneill 		mutex_enter(&sc->sc_stream_mtx);
   1006  1.1  jmcneill 		stream_mask = intsts & sc->sc_stream_mask;
   1007  1.1  jmcneill 		while (streamid < HDAUDIO_MAX_STREAMS && stream_mask != 0) {
   1008  1.1  jmcneill 			st = &sc->sc_stream[streamid++];
   1009  1.1  jmcneill 			if ((stream_mask & 1) != 0 && st->st_intr) {
   1010  1.1  jmcneill 				st->st_intr(st);
   1011  1.1  jmcneill 			}
   1012  1.1  jmcneill 			stream_mask >>= 1;
   1013  1.1  jmcneill 		}
   1014  1.1  jmcneill 		mutex_exit(&sc->sc_stream_mtx);
   1015  1.1  jmcneill 		hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_SIS_MASK);
   1016  1.1  jmcneill 	}
   1017  1.1  jmcneill 
   1018  1.1  jmcneill 	return 1;
   1019  1.1  jmcneill }
   1020  1.1  jmcneill 
   1021  1.1  jmcneill struct hdaudio_stream *
   1022  1.1  jmcneill hdaudio_stream_establish(struct hdaudio_softc *sc,
   1023  1.1  jmcneill     enum hdaudio_stream_type type, int (*intr)(struct hdaudio_stream *),
   1024  1.1  jmcneill     void *cookie)
   1025  1.1  jmcneill {
   1026  1.1  jmcneill 	struct hdaudio_stream *st;
   1027  1.1  jmcneill 	struct hdaudio_dma dma;
   1028  1.1  jmcneill 	int i, err;
   1029  1.1  jmcneill 
   1030  1.1  jmcneill 	dma.dma_size = sizeof(struct hdaudio_bdl_entry) * HDAUDIO_BDL_MAX;
   1031  1.1  jmcneill 	err = hdaudio_dma_alloc(sc, &dma, BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
   1032  1.1  jmcneill 	if (err)
   1033  1.1  jmcneill 		return NULL;
   1034  1.1  jmcneill 
   1035  1.1  jmcneill 	mutex_enter(&sc->sc_stream_mtx);
   1036  1.1  jmcneill 	for (i = 0; i < HDAUDIO_MAX_STREAMS; i++) {
   1037  1.1  jmcneill 		st = &sc->sc_stream[i];
   1038  1.1  jmcneill 		if (st->st_enable == false)
   1039  1.1  jmcneill 			break;
   1040  1.1  jmcneill 		if (st->st_type != type)
   1041  1.1  jmcneill 			continue;
   1042  1.1  jmcneill 		if (sc->sc_stream_mask & (1 << i))
   1043  1.1  jmcneill 			continue;
   1044  1.1  jmcneill 
   1045  1.1  jmcneill 		/* Allocate stream */
   1046  1.1  jmcneill 		st->st_bdl = dma;
   1047  1.1  jmcneill 		st->st_intr = intr;
   1048  1.1  jmcneill 		st->st_cookie = cookie;
   1049  1.1  jmcneill 		sc->sc_stream_mask |= (1 << i);
   1050  1.1  jmcneill 		mutex_exit(&sc->sc_stream_mtx);
   1051  1.1  jmcneill 		return st;
   1052  1.1  jmcneill 	}
   1053  1.1  jmcneill 	mutex_exit(&sc->sc_stream_mtx);
   1054  1.1  jmcneill 
   1055  1.1  jmcneill 	/* No streams of requested type available */
   1056  1.1  jmcneill 	hdaudio_dma_free(sc, &dma);
   1057  1.1  jmcneill 	return NULL;
   1058  1.1  jmcneill }
   1059  1.1  jmcneill 
   1060  1.1  jmcneill void
   1061  1.1  jmcneill hdaudio_stream_disestablish(struct hdaudio_stream *st)
   1062  1.1  jmcneill {
   1063  1.1  jmcneill 	struct hdaudio_softc *sc = st->st_host;
   1064  1.1  jmcneill 	struct hdaudio_dma dma;
   1065  1.1  jmcneill 
   1066  1.1  jmcneill 	KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
   1067  1.1  jmcneill 
   1068  1.1  jmcneill 	mutex_enter(&sc->sc_stream_mtx);
   1069  1.1  jmcneill 	sc->sc_stream_mask &= ~(1 << st->st_shift);
   1070  1.1  jmcneill 	st->st_intr = NULL;
   1071  1.1  jmcneill 	st->st_cookie = NULL;
   1072  1.1  jmcneill 	dma = st->st_bdl;
   1073  1.1  jmcneill 	st->st_bdl.dma_valid = false;
   1074  1.1  jmcneill 	mutex_exit(&sc->sc_stream_mtx);
   1075  1.1  jmcneill 
   1076  1.1  jmcneill 	/* Can't bus_dmamem_unmap while holding a mutex.  */
   1077  1.1  jmcneill 	hdaudio_dma_free(sc, &dma);
   1078  1.1  jmcneill }
   1079  1.1  jmcneill 
   1080  1.1  jmcneill /*
   1081  1.1  jmcneill  * Convert most of audio_params_t to stream fmt descriptor; noticably missing
   1082  1.1  jmcneill  * is the # channels bits, as this is encoded differently in codec and
   1083  1.1  jmcneill  * stream descriptors.
   1084  1.1  jmcneill  *
   1085  1.1  jmcneill  * TODO: validate that the stream and selected codecs can handle the fmt
   1086  1.1  jmcneill  */
   1087  1.1  jmcneill uint16_t
   1088  1.1  jmcneill hdaudio_stream_param(struct hdaudio_stream *st, const audio_params_t *param)
   1089  1.1  jmcneill {
   1090  1.1  jmcneill 	uint16_t fmt = 0;
   1091  1.1  jmcneill 
   1092  1.1  jmcneill 	switch (param->encoding) {
   1093  1.1  jmcneill 	case AUDIO_ENCODING_AC3:
   1094  1.1  jmcneill 		fmt |= HDAUDIO_FMT_TYPE_NONPCM;
   1095  1.1  jmcneill 		break;
   1096  1.1  jmcneill 	default:
   1097  1.1  jmcneill 		fmt |= HDAUDIO_FMT_TYPE_PCM;
   1098  1.1  jmcneill 		break;
   1099  1.1  jmcneill 	}
   1100  1.1  jmcneill 
   1101  1.1  jmcneill 	switch (param->sample_rate) {
   1102  1.1  jmcneill 	case 8000:
   1103  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
   1104  1.1  jmcneill 		    HDAUDIO_FMT_DIV(6);
   1105  1.1  jmcneill 		break;
   1106  1.1  jmcneill 	case 11025:
   1107  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
   1108  1.1  jmcneill 		    HDAUDIO_FMT_DIV(4);
   1109  1.1  jmcneill 		break;
   1110  1.1  jmcneill 	case 16000:
   1111  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
   1112  1.1  jmcneill 		    HDAUDIO_FMT_DIV(3);
   1113  1.1  jmcneill 		break;
   1114  1.1  jmcneill 	case 22050:
   1115  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
   1116  1.1  jmcneill 		    HDAUDIO_FMT_DIV(2);
   1117  1.1  jmcneill 		break;
   1118  1.1  jmcneill 	case 32000:
   1119  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2) |
   1120  1.1  jmcneill 		    HDAUDIO_FMT_DIV(3);
   1121  1.1  jmcneill 		break;
   1122  1.1  jmcneill 	case 44100:
   1123  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1);
   1124  1.1  jmcneill 		break;
   1125  1.1  jmcneill 	case 48000:
   1126  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1);
   1127  1.1  jmcneill 		break;
   1128  1.1  jmcneill 	case 88200:
   1129  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(2);
   1130  1.1  jmcneill 		break;
   1131  1.1  jmcneill 	case 96000:
   1132  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2);
   1133  1.1  jmcneill 		break;
   1134  1.1  jmcneill 	case 176400:
   1135  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(4);
   1136  1.1  jmcneill 		break;
   1137  1.1  jmcneill 	case 192000:
   1138  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(4);
   1139  1.1  jmcneill 		break;
   1140  1.1  jmcneill 	default:
   1141  1.1  jmcneill 		return 0;
   1142  1.1  jmcneill 	}
   1143  1.1  jmcneill 
   1144  1.1  jmcneill 	if (param->precision == 16 && param->validbits == 8)
   1145  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BITS_8_16;
   1146  1.1  jmcneill 	else if (param->precision == 16 && param->validbits == 16)
   1147  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BITS_16_16;
   1148  1.1  jmcneill 	else if (param->precision == 32 && param->validbits == 20)
   1149  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BITS_20_32;
   1150  1.1  jmcneill 	else if (param->precision == 32 && param->validbits == 24)
   1151  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BITS_24_32;
   1152  1.1  jmcneill 	else if (param->precision == 32 && param->validbits == 32)
   1153  1.1  jmcneill 		fmt |= HDAUDIO_FMT_BITS_32_32;
   1154  1.1  jmcneill 	else
   1155  1.1  jmcneill 		return 0;
   1156  1.1  jmcneill 
   1157  1.1  jmcneill 	return fmt;
   1158  1.1  jmcneill }
   1159  1.1  jmcneill 
   1160  1.1  jmcneill void
   1161  1.1  jmcneill hdaudio_stream_reset(struct hdaudio_stream *st)
   1162  1.1  jmcneill {
   1163  1.1  jmcneill 	struct hdaudio_softc *sc = st->st_host;
   1164  1.1  jmcneill 	int snum = st->st_shift;
   1165  1.1  jmcneill 	int retry;
   1166  1.1  jmcneill 	uint8_t ctl0;
   1167  1.1  jmcneill 
   1168  1.1  jmcneill 	ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
   1169  1.1  jmcneill 	ctl0 |= HDAUDIO_CTL_SRST;
   1170  1.1  jmcneill 	hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
   1171  1.1  jmcneill 
   1172  1.1  jmcneill 	retry = HDAUDIO_RESET_TIMEOUT;
   1173  1.1  jmcneill 	do {
   1174  1.1  jmcneill 		ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
   1175  1.1  jmcneill 		if (ctl0 & HDAUDIO_CTL_SRST)
   1176  1.1  jmcneill 			break;
   1177  1.1  jmcneill 		hda_delay(10);
   1178  1.1  jmcneill 	} while (--retry > 0);
   1179  1.1  jmcneill 	if (retry == 0) {
   1180  1.1  jmcneill 		hda_error(sc, "timeout entering stream reset state\n");
   1181  1.1  jmcneill 		return;
   1182  1.1  jmcneill 	}
   1183  1.1  jmcneill 
   1184  1.1  jmcneill 	ctl0 &= ~HDAUDIO_CTL_SRST;
   1185  1.1  jmcneill 	hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
   1186  1.1  jmcneill 
   1187  1.1  jmcneill 	retry = HDAUDIO_RESET_TIMEOUT;
   1188  1.1  jmcneill 	do {
   1189  1.1  jmcneill 		ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
   1190  1.1  jmcneill 		if (!(ctl0 & HDAUDIO_CTL_SRST))
   1191  1.1  jmcneill 			break;
   1192  1.1  jmcneill 		hda_delay(10);
   1193  1.1  jmcneill 	} while (--retry > 0);
   1194  1.1  jmcneill 	if (retry == 0) {
   1195  1.1  jmcneill 		hda_error(sc, "timeout leaving stream reset state\n");
   1196  1.1  jmcneill 		return;
   1197  1.1  jmcneill 	}
   1198  1.1  jmcneill }
   1199  1.1  jmcneill 
   1200  1.1  jmcneill void
   1201  1.1  jmcneill hdaudio_stream_start(struct hdaudio_stream *st, int blksize,
   1202  1.1  jmcneill     bus_size_t dmasize, const audio_params_t *params)
   1203  1.1  jmcneill {
   1204  1.1  jmcneill 	struct hdaudio_softc *sc = st->st_host;
   1205  1.1  jmcneill 	struct hdaudio_bdl_entry *bdl;
   1206  1.1  jmcneill 	uint64_t dmaaddr;
   1207  1.1  jmcneill 	uint32_t intctl;
   1208  1.1  jmcneill 	uint16_t fmt;
   1209  1.1  jmcneill 	uint8_t ctl0, ctl2;
   1210  1.1  jmcneill 	int cnt, snum = st->st_shift;
   1211  1.1  jmcneill 
   1212  1.1  jmcneill 	KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
   1213  1.1  jmcneill 	KASSERT(st->st_data.dma_valid == true);
   1214  1.1  jmcneill 	KASSERT(st->st_bdl.dma_valid == true);
   1215  1.1  jmcneill 
   1216  1.1  jmcneill 	hdaudio_stream_stop(st);
   1217  1.1  jmcneill 	hdaudio_stream_reset(st);
   1218  1.1  jmcneill 
   1219  1.1  jmcneill 	/*
   1220  1.1  jmcneill 	 * Configure buffer descriptor list
   1221  1.1  jmcneill 	 */
   1222  1.1  jmcneill 	dmaaddr = DMA_DMAADDR(&st->st_data);
   1223  1.1  jmcneill 	bdl = DMA_KERNADDR(&st->st_bdl);
   1224  1.1  jmcneill 	for (cnt = 0; cnt < HDAUDIO_BDL_MAX; cnt++) {
   1225  1.1  jmcneill 		bdl[cnt].address_lo = (uint32_t)dmaaddr;
   1226  1.1  jmcneill 		bdl[cnt].address_hi = dmaaddr >> 32;
   1227  1.1  jmcneill 		bdl[cnt].length = blksize;
   1228  1.1  jmcneill 		bdl[cnt].flags = HDAUDIO_BDL_ENTRY_IOC;
   1229  1.1  jmcneill 		dmaaddr += blksize;
   1230  1.1  jmcneill 		if (dmaaddr >= DMA_DMAADDR(&st->st_data) + dmasize) {
   1231  1.1  jmcneill 			cnt++;
   1232  1.1  jmcneill 			break;
   1233  1.1  jmcneill 		}
   1234  1.1  jmcneill 	}
   1235  1.1  jmcneill 
   1236  1.1  jmcneill 	/*
   1237  1.1  jmcneill 	 * Program buffer descriptor list
   1238  1.1  jmcneill 	 */
   1239  1.1  jmcneill 	dmaaddr = DMA_DMAADDR(&st->st_bdl);
   1240  1.1  jmcneill 	hda_write4(sc, HDAUDIO_SD_BDPL(snum), (uint32_t)dmaaddr);
   1241  1.1  jmcneill 	hda_write4(sc, HDAUDIO_SD_BDPU(snum), (uint32_t)(dmaaddr >> 32));
   1242  1.1  jmcneill 	hda_write2(sc, HDAUDIO_SD_LVI(snum), (cnt - 1) & 0xff);
   1243  1.1  jmcneill 
   1244  1.1  jmcneill 	/*
   1245  1.1  jmcneill 	 * Program cyclic buffer length
   1246  1.1  jmcneill 	 */
   1247  1.1  jmcneill 	hda_write4(sc, HDAUDIO_SD_CBL(snum), dmasize);
   1248  1.1  jmcneill 
   1249  1.1  jmcneill 	/*
   1250  1.1  jmcneill 	 * Program stream number (tag). Although controller hardware is
   1251  1.1  jmcneill 	 * capable of transmitting any stream number (0-15), by convention
   1252  1.1  jmcneill 	 * stream 0 is reserved as unused by software, so that converters
   1253  1.1  jmcneill 	 * whose stream numbers have been reset to 0 do not unintentionally
   1254  1.1  jmcneill 	 * decode data not intended for them.
   1255  1.1  jmcneill 	 */
   1256  1.1  jmcneill 	ctl2 = hda_read1(sc, HDAUDIO_SD_CTL2(snum));
   1257  1.1  jmcneill 	ctl2 &= ~0xf0;
   1258  1.1  jmcneill 	ctl2 |= hdaudio_stream_tag(st) << 4;
   1259  1.1  jmcneill 	hda_write1(sc, HDAUDIO_SD_CTL2(snum), ctl2);
   1260  1.1  jmcneill 
   1261  1.1  jmcneill 	/*
   1262  1.1  jmcneill 	 * Program stream format
   1263  1.1  jmcneill 	 */
   1264  1.1  jmcneill 	fmt = hdaudio_stream_param(st, params) |
   1265  1.1  jmcneill 	    HDAUDIO_FMT_CHAN(params->channels);
   1266  1.1  jmcneill 	hda_write2(sc, HDAUDIO_SD_FMT(snum), fmt);
   1267  1.1  jmcneill 
   1268  1.1  jmcneill 	/*
   1269  1.1  jmcneill 	 * Switch on interrupts for this stream
   1270  1.1  jmcneill 	 */
   1271  1.1  jmcneill 	intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
   1272  1.1  jmcneill 	intctl |= (1 << st->st_shift);
   1273  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
   1274  1.1  jmcneill 
   1275  1.1  jmcneill 	/*
   1276  1.1  jmcneill 	 * Start running the stream
   1277  1.1  jmcneill 	 */
   1278  1.1  jmcneill 	ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
   1279  1.1  jmcneill 	ctl0 |= HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
   1280  1.1  jmcneill 	    HDAUDIO_CTL_RUN;
   1281  1.1  jmcneill 	hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
   1282  1.1  jmcneill }
   1283  1.1  jmcneill 
   1284  1.1  jmcneill void
   1285  1.1  jmcneill hdaudio_stream_stop(struct hdaudio_stream *st)
   1286  1.1  jmcneill {
   1287  1.1  jmcneill 	struct hdaudio_softc *sc = st->st_host;
   1288  1.1  jmcneill 	uint32_t intctl;
   1289  1.1  jmcneill 	uint8_t ctl0;
   1290  1.1  jmcneill 	int snum = st->st_shift;
   1291  1.1  jmcneill 
   1292  1.1  jmcneill 	/*
   1293  1.1  jmcneill 	 * Stop running the stream
   1294  1.1  jmcneill 	 */
   1295  1.1  jmcneill 	ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
   1296  1.1  jmcneill 	ctl0 &= ~(HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
   1297  1.1  jmcneill 	    HDAUDIO_CTL_RUN);
   1298  1.1  jmcneill 	hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
   1299  1.1  jmcneill 
   1300  1.1  jmcneill 	/*
   1301  1.1  jmcneill 	 * Switch off interrupts for this stream
   1302  1.1  jmcneill 	 */
   1303  1.1  jmcneill 	intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
   1304  1.1  jmcneill 	intctl &= ~(1 << st->st_shift);
   1305  1.1  jmcneill 	hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
   1306  1.1  jmcneill }
   1307  1.1  jmcneill 
   1308  1.1  jmcneill /*
   1309  1.1  jmcneill  * /dev/hdaudioN interface
   1310  1.1  jmcneill  */
   1311  1.1  jmcneill 
   1312  1.1  jmcneill static const char *
   1313  1.1  jmcneill hdaudioioctl_fgrp_to_cstr(enum function_group_type type)
   1314  1.1  jmcneill {
   1315  1.1  jmcneill 	switch (type) {
   1316  1.1  jmcneill 	case HDAUDIO_GROUP_TYPE_AFG:
   1317  1.1  jmcneill 		return "afg";
   1318  1.1  jmcneill 	case HDAUDIO_GROUP_TYPE_VSM_FG:
   1319  1.1  jmcneill 		return "vsmfg";
   1320  1.1  jmcneill 	default:
   1321  1.1  jmcneill 		return "unknown";
   1322  1.1  jmcneill 	}
   1323  1.1  jmcneill }
   1324  1.1  jmcneill 
   1325  1.1  jmcneill static struct hdaudio_function_group *
   1326  1.1  jmcneill hdaudioioctl_fgrp_lookup(struct hdaudio_softc *sc, int codecid, int nid)
   1327  1.1  jmcneill {
   1328  1.1  jmcneill 	struct hdaudio_codec *co;
   1329  1.1  jmcneill 	struct hdaudio_function_group *fg = NULL;
   1330  1.1  jmcneill 	int i;
   1331  1.1  jmcneill 
   1332  1.1  jmcneill 	if (codecid < 0 || codecid >= HDAUDIO_MAX_CODECS)
   1333  1.1  jmcneill 		return NULL;
   1334  1.1  jmcneill 	co = &sc->sc_codec[codecid];
   1335  1.1  jmcneill 	if (co->co_valid == false)
   1336  1.1  jmcneill 		return NULL;
   1337  1.1  jmcneill 
   1338  1.1  jmcneill 	for (i = 0; i < co->co_nfg; i++)
   1339  1.1  jmcneill 		if (co->co_fg[i].fg_nid == nid) {
   1340  1.1  jmcneill 			fg = &co->co_fg[i];
   1341  1.1  jmcneill 			break;
   1342  1.1  jmcneill 		}
   1343  1.1  jmcneill 
   1344  1.1  jmcneill 	return fg;
   1345  1.1  jmcneill }
   1346  1.1  jmcneill 
   1347  1.1  jmcneill static int
   1348  1.1  jmcneill hdaudioioctl_fgrp_info(struct hdaudio_softc *sc, prop_dictionary_t request,
   1349  1.1  jmcneill     prop_dictionary_t response)
   1350  1.1  jmcneill {
   1351  1.1  jmcneill 	struct hdaudio_codec *co;
   1352  1.1  jmcneill 	struct hdaudio_function_group *fg;
   1353  1.1  jmcneill 	prop_array_t array;
   1354  1.1  jmcneill 	prop_dictionary_t dict;
   1355  1.1  jmcneill 	int codecid, fgid;
   1356  1.1  jmcneill 
   1357  1.1  jmcneill 	array = prop_array_create();
   1358  1.1  jmcneill 	if (array == NULL)
   1359  1.1  jmcneill 		return ENOMEM;
   1360  1.1  jmcneill 
   1361  1.1  jmcneill 	for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) {
   1362  1.1  jmcneill 		co = &sc->sc_codec[codecid];
   1363  1.1  jmcneill 		if (co->co_valid == false)
   1364  1.1  jmcneill 			continue;
   1365  1.1  jmcneill 		for (fgid = 0; fgid < co->co_nfg; fgid++) {
   1366  1.1  jmcneill 			fg = &co->co_fg[fgid];
   1367  1.1  jmcneill 			dict = prop_dictionary_create();
   1368  1.1  jmcneill 			if (dict == NULL)
   1369  1.1  jmcneill 				return ENOMEM;
   1370  1.1  jmcneill 			prop_dictionary_set_cstring_nocopy(dict,
   1371  1.1  jmcneill 			    "type", hdaudioioctl_fgrp_to_cstr(fg->fg_type));
   1372  1.1  jmcneill 			prop_dictionary_set_int16(dict, "nid", fg->fg_nid);
   1373  1.1  jmcneill 			prop_dictionary_set_int16(dict, "codecid", codecid);
   1374  1.1  jmcneill 			prop_dictionary_set_uint16(dict, "vendor-id",
   1375  1.1  jmcneill 			    fg->fg_vendor);
   1376  1.1  jmcneill 			prop_dictionary_set_uint16(dict, "product-id",
   1377  1.1  jmcneill 			    fg->fg_product);
   1378  1.1  jmcneill 			prop_dictionary_set_uint32(dict, "subsystem-id",
   1379  1.1  jmcneill 			    sc->sc_subsystem);
   1380  1.1  jmcneill 			if (fg->fg_device)
   1381  1.1  jmcneill 				prop_dictionary_set_cstring(dict, "device",
   1382  1.1  jmcneill 				    device_xname(fg->fg_device));
   1383  1.1  jmcneill 			else
   1384  1.1  jmcneill 				prop_dictionary_set_cstring_nocopy(dict,
   1385  1.1  jmcneill 				    "device", "<none>");
   1386  1.1  jmcneill 			prop_array_add(array, dict);
   1387  1.1  jmcneill 		}
   1388  1.1  jmcneill 	}
   1389  1.1  jmcneill 
   1390  1.1  jmcneill 	prop_dictionary_set(response, "function-group-info", array);
   1391  1.1  jmcneill 	return 0;
   1392  1.1  jmcneill }
   1393  1.1  jmcneill 
   1394  1.1  jmcneill static int
   1395  1.1  jmcneill hdaudioioctl_fgrp_getconfig(struct hdaudio_softc *sc,
   1396  1.1  jmcneill     prop_dictionary_t request, prop_dictionary_t response)
   1397  1.1  jmcneill {
   1398  1.1  jmcneill 	struct hdaudio_function_group *fg;
   1399  1.1  jmcneill 	prop_dictionary_t dict;
   1400  1.1  jmcneill 	prop_array_t array;
   1401  1.1  jmcneill 	uint32_t nodecnt, wcap, config;
   1402  1.1  jmcneill 	int16_t codecid, nid, i;
   1403  1.1  jmcneill 	int startnode, endnode;
   1404  1.1  jmcneill 
   1405  1.1  jmcneill 	if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
   1406  1.1  jmcneill 	    !prop_dictionary_get_int16(request, "nid", &nid))
   1407  1.1  jmcneill 		return EINVAL;
   1408  1.1  jmcneill 
   1409  1.1  jmcneill 	fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
   1410  1.1  jmcneill 	if (fg == NULL)
   1411  1.1  jmcneill 		return ENODEV;
   1412  1.1  jmcneill 
   1413  1.1  jmcneill 	array = prop_array_create();
   1414  1.1  jmcneill 	if (array == NULL)
   1415  1.1  jmcneill 		return ENOMEM;
   1416  1.1  jmcneill 
   1417  1.1  jmcneill 	nodecnt = hdaudio_command(fg->fg_codec, fg->fg_nid,
   1418  1.1  jmcneill 	    CORB_GET_PARAMETER, COP_SUBORDINATE_NODE_COUNT);
   1419  1.1  jmcneill 	startnode = COP_NODECNT_STARTNODE(nodecnt);
   1420  1.1  jmcneill 	endnode = startnode + COP_NODECNT_NUMNODES(nodecnt);
   1421  1.1  jmcneill 
   1422  1.1  jmcneill 	for (i = startnode; i < endnode; i++) {
   1423  1.1  jmcneill 		wcap = hdaudio_command(fg->fg_codec, i,
   1424  1.1  jmcneill 		    CORB_GET_PARAMETER, COP_AUDIO_WIDGET_CAPABILITIES);
   1425  1.1  jmcneill 		if (COP_AWCAP_TYPE(wcap) != COP_AWCAP_TYPE_PIN_COMPLEX)
   1426  1.1  jmcneill 			continue;
   1427  1.1  jmcneill 		config = hdaudio_command(fg->fg_codec, i,
   1428  1.1  jmcneill 		    CORB_GET_CONFIGURATION_DEFAULT, 0);
   1429  1.1  jmcneill 		dict = prop_dictionary_create();
   1430  1.1  jmcneill 		if (dict == NULL)
   1431  1.1  jmcneill 			return ENOMEM;
   1432  1.1  jmcneill 		prop_dictionary_set_int16(dict, "nid", i);
   1433  1.1  jmcneill 		prop_dictionary_set_uint32(dict, "config", config);
   1434  1.1  jmcneill 		prop_array_add(array, dict);
   1435  1.1  jmcneill 	}
   1436  1.1  jmcneill 
   1437  1.1  jmcneill 	prop_dictionary_set(response, "pin-config", array);
   1438  1.1  jmcneill 
   1439  1.1  jmcneill 	return 0;
   1440  1.1  jmcneill }
   1441  1.1  jmcneill 
   1442  1.1  jmcneill static int
   1443  1.1  jmcneill hdaudioioctl_fgrp_setconfig(struct hdaudio_softc *sc,
   1444  1.1  jmcneill     prop_dictionary_t request, prop_dictionary_t response)
   1445  1.1  jmcneill {
   1446  1.1  jmcneill 	struct hdaudio_function_group *fg;
   1447  1.1  jmcneill 	prop_array_t config;
   1448  1.1  jmcneill 	int16_t codecid, nid;
   1449  1.1  jmcneill 	int err;
   1450  1.1  jmcneill 
   1451  1.1  jmcneill 	if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
   1452  1.1  jmcneill 	    !prop_dictionary_get_int16(request, "nid", &nid))
   1453  1.1  jmcneill 		return EINVAL;
   1454  1.1  jmcneill 
   1455  1.1  jmcneill 	fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
   1456  1.1  jmcneill 	if (fg == NULL)
   1457  1.1  jmcneill 		return ENODEV;
   1458  1.1  jmcneill 
   1459  1.1  jmcneill 	if (fg->fg_device) {
   1460  1.1  jmcneill 		err = config_detach(fg->fg_device, 0);
   1461  1.1  jmcneill 		if (err)
   1462  1.1  jmcneill 			return err;
   1463  1.1  jmcneill 		fg->fg_device = NULL;
   1464  1.1  jmcneill 	}
   1465  1.1  jmcneill 
   1466  1.1  jmcneill 	/* "pin-config" may be NULL, this means "use BIOS configuration" */
   1467  1.1  jmcneill 	config = prop_dictionary_get(request, "pin-config");
   1468  1.1  jmcneill 	if (config && prop_object_type(config) != PROP_TYPE_ARRAY) {
   1469  1.1  jmcneill 		prop_object_release(config);
   1470  1.1  jmcneill 		return EINVAL;
   1471  1.1  jmcneill 	}
   1472  1.1  jmcneill 	hdaudio_attach_fg(fg, config);
   1473  1.1  jmcneill 	if (config)
   1474  1.1  jmcneill 		prop_object_release(config);
   1475  1.1  jmcneill 
   1476  1.1  jmcneill 	return 0;
   1477  1.1  jmcneill }
   1478  1.1  jmcneill 
   1479  1.1  jmcneill static int
   1480  1.1  jmcneill hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc *sc, u_long cmd,
   1481  1.1  jmcneill     prop_dictionary_t request, prop_dictionary_t response)
   1482  1.1  jmcneill {
   1483  1.1  jmcneill 	struct hdaudio_function_group *fg;
   1484  1.1  jmcneill 	int (*infocb)(void *, prop_dictionary_t, prop_dictionary_t);
   1485  1.1  jmcneill 	prop_dictionary_t fgrp_dict;
   1486  1.1  jmcneill 	uint64_t info_fn;
   1487  1.1  jmcneill 	int16_t codecid, nid;
   1488  1.1  jmcneill 	void *fgrp_sc;
   1489  1.1  jmcneill 	bool rv;
   1490  1.1  jmcneill 	int err;
   1491  1.1  jmcneill 
   1492  1.1  jmcneill 	if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
   1493  1.1  jmcneill 	    !prop_dictionary_get_int16(request, "nid", &nid))
   1494  1.1  jmcneill 		return EINVAL;
   1495  1.1  jmcneill 
   1496  1.1  jmcneill 	fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
   1497  1.1  jmcneill 	if (fg == NULL)
   1498  1.1  jmcneill 		return ENODEV;
   1499  1.1  jmcneill 	if (fg->fg_device == NULL)
   1500  1.1  jmcneill 		return ENXIO;
   1501  1.1  jmcneill 	fgrp_sc = device_private(fg->fg_device);
   1502  1.1  jmcneill 	fgrp_dict = device_properties(fg->fg_device);
   1503  1.1  jmcneill 
   1504  1.1  jmcneill 	switch (fg->fg_type) {
   1505  1.1  jmcneill 	case HDAUDIO_GROUP_TYPE_AFG:
   1506  1.1  jmcneill 		switch (cmd) {
   1507  1.1  jmcneill 		case HDAUDIO_FGRP_CODEC_INFO:
   1508  1.1  jmcneill 			rv = prop_dictionary_get_uint64(fgrp_dict,
   1509  1.1  jmcneill 			    "codecinfo-callback", &info_fn);
   1510  1.1  jmcneill 			if (!rv)
   1511  1.1  jmcneill 				return ENXIO;
   1512  1.1  jmcneill 			infocb = (void *)(uintptr_t)info_fn;
   1513  1.1  jmcneill 			err = infocb(fgrp_sc, request, response);
   1514  1.1  jmcneill 			break;
   1515  1.1  jmcneill 		case HDAUDIO_FGRP_WIDGET_INFO:
   1516  1.1  jmcneill 			rv = prop_dictionary_get_uint64(fgrp_dict,
   1517  1.1  jmcneill 			    "widgetinfo-callback", &info_fn);
   1518  1.1  jmcneill 			if (!rv)
   1519  1.1  jmcneill 				return ENXIO;
   1520  1.1  jmcneill 			infocb = (void *)(uintptr_t)info_fn;
   1521  1.1  jmcneill 			err = infocb(fgrp_sc, request, response);
   1522  1.1  jmcneill 			break;
   1523  1.1  jmcneill 		default:
   1524  1.1  jmcneill 			err = EINVAL;
   1525  1.1  jmcneill 			break;
   1526  1.1  jmcneill 		}
   1527  1.1  jmcneill 		break;
   1528  1.1  jmcneill 
   1529  1.1  jmcneill 	default:
   1530  1.1  jmcneill 		err = EINVAL;
   1531  1.1  jmcneill 		break;
   1532  1.1  jmcneill 	}
   1533  1.1  jmcneill 	return err;
   1534  1.1  jmcneill }
   1535  1.1  jmcneill 
   1536  1.1  jmcneill int
   1537  1.1  jmcneill hdaudioopen(dev_t dev, int flag, int mode, struct lwp *l)
   1538  1.1  jmcneill {
   1539  1.1  jmcneill 	device_t self;
   1540  1.1  jmcneill 
   1541  1.1  jmcneill 	self = device_lookup(&hdaudio_cd, HDAUDIOUNIT(dev));
   1542  1.1  jmcneill 	if (self == NULL)
   1543  1.1  jmcneill 		return ENXIO;
   1544  1.1  jmcneill 
   1545  1.1  jmcneill 	return 0;
   1546  1.1  jmcneill }
   1547  1.1  jmcneill 
   1548  1.1  jmcneill int
   1549  1.1  jmcneill hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
   1550  1.1  jmcneill {
   1551  1.1  jmcneill 	return 0;
   1552  1.1  jmcneill }
   1553  1.1  jmcneill 
   1554  1.1  jmcneill int
   1555  1.1  jmcneill hdaudioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1556  1.1  jmcneill {
   1557  1.1  jmcneill 	struct hdaudio_softc *sc;
   1558  1.1  jmcneill 	struct plistref *pref = addr;
   1559  1.1  jmcneill 	prop_dictionary_t request, response;
   1560  1.1  jmcneill 	int err;
   1561  1.1  jmcneill 
   1562  1.1  jmcneill 	sc = device_lookup_private(&hdaudio_cd, HDAUDIOUNIT(dev));
   1563  1.1  jmcneill 	if (sc == NULL)
   1564  1.1  jmcneill 		return ENXIO;
   1565  1.1  jmcneill 
   1566  1.1  jmcneill 	response = prop_dictionary_create();
   1567  1.1  jmcneill 	if (response == NULL)
   1568  1.1  jmcneill 		return ENOMEM;
   1569  1.1  jmcneill 
   1570  1.1  jmcneill 	err = prop_dictionary_copyin_ioctl(pref, cmd, &request);
   1571  1.1  jmcneill 	if (err) {
   1572  1.1  jmcneill 		prop_object_release(response);
   1573  1.1  jmcneill 		return err;
   1574  1.1  jmcneill 	}
   1575  1.1  jmcneill 
   1576  1.1  jmcneill 	switch (cmd) {
   1577  1.1  jmcneill 	case HDAUDIO_FGRP_INFO:
   1578  1.1  jmcneill 		err = hdaudioioctl_fgrp_info(sc, request, response);
   1579  1.1  jmcneill 		break;
   1580  1.1  jmcneill 	case HDAUDIO_FGRP_GETCONFIG:
   1581  1.1  jmcneill 		err = hdaudioioctl_fgrp_getconfig(sc, request, response);
   1582  1.1  jmcneill 		break;
   1583  1.1  jmcneill 	case HDAUDIO_FGRP_SETCONFIG:
   1584  1.1  jmcneill 		err = hdaudioioctl_fgrp_setconfig(sc, request, response);
   1585  1.1  jmcneill 		break;
   1586  1.1  jmcneill 	case HDAUDIO_FGRP_CODEC_INFO:
   1587  1.1  jmcneill 	case HDAUDIO_FGRP_WIDGET_INFO:
   1588  1.1  jmcneill 		err = hdaudio_dispatch_fgrp_ioctl(sc, cmd, request, response);
   1589  1.1  jmcneill 		break;
   1590  1.1  jmcneill 	default:
   1591  1.1  jmcneill 		err = EINVAL;
   1592  1.1  jmcneill 		break;
   1593  1.1  jmcneill 	}
   1594  1.1  jmcneill 
   1595  1.1  jmcneill 	if (!err)
   1596  1.1  jmcneill 		err = prop_dictionary_copyout_ioctl(pref, cmd, response);
   1597  1.1  jmcneill 
   1598  1.1  jmcneill 	if (response)
   1599  1.1  jmcneill 		prop_object_release(response);
   1600  1.1  jmcneill 	prop_object_release(request);
   1601  1.1  jmcneill 	return err;
   1602  1.1  jmcneill }
   1603  1.1  jmcneill 
   1604  1.1  jmcneill MODULE(MODULE_CLASS_DRIVER, hdaudio, NULL);
   1605  1.1  jmcneill 
   1606  1.1  jmcneill static int
   1607  1.1  jmcneill hdaudio_modcmd(modcmd_t cmd, void *opaque)
   1608  1.1  jmcneill {
   1609  1.1  jmcneill 	int error = 0;
   1610  1.1  jmcneill #ifdef _MODULE
   1611  1.1  jmcneill 	int bmaj = -1, cmaj = -1;
   1612  1.1  jmcneill #endif
   1613  1.1  jmcneill 
   1614  1.1  jmcneill 	switch (cmd) {
   1615  1.1  jmcneill 	case MODULE_CMD_INIT:
   1616  1.1  jmcneill #ifdef _MODULE
   1617  1.1  jmcneill 		error = devsw_attach("hdaudio", NULL, &bmaj,
   1618  1.1  jmcneill 		    &hdaudio_cdevsw, &cmaj);
   1619  1.1  jmcneill #endif
   1620  1.1  jmcneill 		return error;
   1621  1.1  jmcneill 	case MODULE_CMD_FINI:
   1622  1.1  jmcneill #ifdef _MODULE
   1623  1.1  jmcneill 		devsw_detach(NULL, &hdaudio_cdevsw);
   1624  1.1  jmcneill #endif
   1625  1.1  jmcneill 		return 0;
   1626  1.1  jmcneill 	default:
   1627  1.1  jmcneill 		return ENOTTY;
   1628  1.1  jmcneill 	}
   1629  1.1  jmcneill }
   1630  1.1  jmcneill 
   1631  1.1  jmcneill DEV_VERBOSE_DEFINE(hdaudio);
   1632