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