xref: /netbsd-src/sys/dev/pci/gcscaudio.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: gcscaudio.c,v 1.17 2019/05/08 13:40:19 isaki Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 SHIMIZU Ryo <ryo@nerv.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: gcscaudio.c,v 1.17 2019/05/08 13:40:19 isaki Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 #include <sys/device.h>
36 #include <sys/queue.h>
37 
38 #include <dev/pci/pcidevs.h>
39 #include <dev/pci/pcivar.h>
40 
41 #include <sys/audioio.h>
42 #include <dev/audio/audio_if.h>
43 
44 #include <dev/ic/ac97reg.h>
45 #include <dev/ic/ac97var.h>
46 
47 #include <dev/pci/gcscaudioreg.h>
48 
49 
50 #define	GCSCAUDIO_NPRDTABLE	256	/* including a JMP-PRD for loop */
51 #define	GCSCAUDIO_PRD_SIZE_MAX	65532	/* limited by CS5536 Controller */
52 #define	GCSCAUDIO_BUFSIZE_MAX	(GCSCAUDIO_PRD_SIZE_MAX * (GCSCAUDIO_NPRDTABLE - 1))
53 
54 struct gcscaudio_prd {
55 	/* PRD table for play/rec */
56 	struct gcscaudio_prdtables {
57 #define	PRD_TABLE_FRONT		0
58 #define	PRD_TABLE_SURR		1
59 #define	PRD_TABLE_CENTER	2
60 #define	PRD_TABLE_LFE		3
61 #define	PRD_TABLE_REC		4
62 #define	PRD_TABLE_MAX		5
63 		struct acc_prd prdtbl[PRD_TABLE_MAX][GCSCAUDIO_NPRDTABLE];
64 	} *p_prdtables;
65 	bus_dmamap_t p_prdmap;
66 	bus_dma_segment_t p_prdsegs[1];
67 	int p_prdnseg;
68 };
69 
70 struct gcscaudio_dma {
71 	LIST_ENTRY(gcscaudio_dma) list;
72 	bus_dmamap_t map;
73 	void *addr;
74 	size_t size;
75 	bus_dma_segment_t segs[1];
76 	int nseg;
77 };
78 
79 struct gcscaudio_softc_ch {
80 	void (*ch_intr)(void *);
81 	void *ch_intr_arg;
82 	struct audio_params ch_params;
83 };
84 
85 struct gcscaudio_softc {
86 	device_t sc_dev;
87 	kmutex_t sc_lock;
88 	kmutex_t sc_intr_lock;
89 	pci_chipset_tag_t sc_pc;
90 	pcitag_t sc_pt;
91 	void *sc_ih;
92 	bus_space_tag_t sc_iot;
93 	bus_space_handle_t sc_ioh;
94 	bus_size_t sc_ios;
95 	bus_dma_tag_t sc_dmat;
96 
97 	/* allocated DMA buffer list */
98 	LIST_HEAD(, gcscaudio_dma) sc_dmalist;
99 
100 #define GCSCAUDIO_MAXFORMATS	4
101 	struct audio_format sc_formats[GCSCAUDIO_MAXFORMATS];
102 	int sc_nformats;
103 
104 	/* AC97 codec */
105 	struct ac97_host_if host_if;
106 	struct ac97_codec_if *codec_if;
107 
108 	/* input, output channels */
109 	struct gcscaudio_softc_ch sc_play;
110 	struct gcscaudio_softc_ch sc_rec;
111 	struct gcscaudio_prd sc_prd;
112 
113 	/* multi channel splitter work; {4,6}ch stream to {2,4} DMA buffers */
114 	void *sc_mch_split_buf;
115 	void *sc_mch_split_start;
116 	int sc_mch_split_off;
117 	int sc_mch_split_size;
118 	int sc_mch_split_blksize;
119 	void (*sc_mch_splitter)(void *, void *, int, int);
120 	bool sc_spdif;
121 };
122 
123 /* for cfattach */
124 static int gcscaudio_match(device_t, cfdata_t, void *);
125 static void gcscaudio_attach(device_t, device_t, void *);
126 
127 /* for audio_hw_if */
128 static int gcscaudio_open(void *, int);
129 static void gcscaudio_close(void *);
130 static int gcscaudio_query_format(void *, audio_format_query_t *);
131 static int gcscaudio_set_format(void *, int,
132                                 const audio_params_t *, const audio_params_t *,
133                                 audio_filter_reg_t *, audio_filter_reg_t *);
134 static int gcscaudio_round_blocksize(void *, int, int, const audio_params_t *);
135 static int gcscaudio_halt_output(void *);
136 static int gcscaudio_halt_input(void *);
137 static int gcscaudio_getdev(void *, struct audio_device *);
138 static int gcscaudio_set_port(void *, mixer_ctrl_t *);
139 static int gcscaudio_get_port(void *, mixer_ctrl_t *);
140 static int gcscaudio_query_devinfo(void *, mixer_devinfo_t *);
141 static void *gcscaudio_malloc(void *, int, size_t);
142 static void gcscaudio_free(void *, void *, size_t);
143 static size_t gcscaudio_round_buffersize(void *, int, size_t);
144 static int gcscaudio_get_props(void *);
145 static int gcscaudio_trigger_output(void *, void *, void *, int,
146                                     void (*)(void *), void *,
147                                     const audio_params_t *);
148 static int gcscaudio_trigger_input(void *, void *, void *, int,
149                                    void (*)(void *), void *,
150                                    const audio_params_t *);
151 static void gcscaudio_get_locks(void *, kmutex_t **, kmutex_t **);
152 static bool gcscaudio_resume(device_t, const pmf_qual_t *);
153 static int gcscaudio_intr(void *);
154 
155 /* for codec_if */
156 static int gcscaudio_attach_codec(void *, struct ac97_codec_if *);
157 static int gcscaudio_write_codec(void *, uint8_t, uint16_t);
158 static int gcscaudio_read_codec(void *, uint8_t, uint16_t *);
159 static int gcscaudio_reset_codec(void *);
160 static void gcscaudio_spdif_event_codec(void *, bool);
161 
162 /* misc */
163 static int gcscaudio_append_formats(struct gcscaudio_softc *,
164                                     const struct audio_format *);
165 static int gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *);
166 static int gcscaudio_allocate_dma(struct gcscaudio_softc *, size_t, void **,
167                                   bus_dma_segment_t *, int, int *,
168                                   bus_dmamap_t *);
169 
170 
171 CFATTACH_DECL_NEW(gcscaudio, sizeof (struct gcscaudio_softc),
172     gcscaudio_match, gcscaudio_attach, NULL, NULL);
173 
174 
175 static struct audio_device gcscaudio_device = {
176 	"AMD Geode CS5536",
177 	"",
178 	"gcscaudio"
179 };
180 
181 static const struct audio_hw_if gcscaudio_hw_if = {
182 	.open			= gcscaudio_open,
183 	.close			= gcscaudio_close,
184 	.query_format		= gcscaudio_query_format,
185 	.set_format		= gcscaudio_set_format,
186 	.round_blocksize	= gcscaudio_round_blocksize,
187 	.commit_settings	= NULL,
188 	.init_output		= NULL,
189 	.init_input		= NULL,
190 	.start_output		= NULL,
191 	.start_input		= NULL,
192 	.halt_output		= gcscaudio_halt_output,
193 	.halt_input		= gcscaudio_halt_input,
194 	.speaker_ctl		= NULL,
195 	.getdev			= gcscaudio_getdev,
196 	.set_port		= gcscaudio_set_port,
197 	.get_port		= gcscaudio_get_port,
198 	.query_devinfo		= gcscaudio_query_devinfo,
199 	.allocm			= gcscaudio_malloc,
200 	.freem			= gcscaudio_free,
201 	.round_buffersize	= gcscaudio_round_buffersize,
202 	.get_props		= gcscaudio_get_props,
203 	.trigger_output		= gcscaudio_trigger_output,
204 	.trigger_input		= gcscaudio_trigger_input,
205 	.dev_ioctl		= NULL,
206 	.get_locks		= gcscaudio_get_locks,
207 };
208 
209 #define GCSCAUDIO_FORMAT(aumode, ch, chmask) \
210 	{ \
211 		.mode		= (aumode), \
212 		.encoding	= AUDIO_ENCODING_SLINEAR_LE, \
213 		.validbits	= 16, \
214 		.precision	= 16, \
215 		.channels	= (ch), \
216 		.channel_mask	= (chmask), \
217 		.frequency_type	= 0, \
218 		.frequency	= { 8000, 48000 }, \
219 	}
220 static const struct audio_format gcscaudio_formats_2ch =
221 	GCSCAUDIO_FORMAT(AUMODE_PLAY | AUMODE_RECORD, 2, AUFMT_STEREO);
222 
223 static const struct audio_format gcscaudio_formats_4ch =
224 	GCSCAUDIO_FORMAT(AUMODE_PLAY                , 4, AUFMT_SURROUND4);
225 
226 static const struct audio_format gcscaudio_formats_6ch =
227 	GCSCAUDIO_FORMAT(AUMODE_PLAY                , 6, AUFMT_DOLBY_5_1);
228 
229 static int
230 gcscaudio_match(device_t parent, cfdata_t match, void *aux)
231 {
232 	struct pci_attach_args *pa;
233 
234 	pa = (struct pci_attach_args *)aux;
235 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) &&
236 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_CS5536_AUDIO))
237 		return 1;
238 
239 	return 0;
240 }
241 
242 static int
243 gcscaudio_append_formats(struct gcscaudio_softc *sc,
244                          const struct audio_format *format)
245 {
246 	if (sc->sc_nformats >= GCSCAUDIO_MAXFORMATS) {
247 		aprint_error_dev(sc->sc_dev, "too many formats\n");
248 		return EINVAL;
249 	}
250 	sc->sc_formats[sc->sc_nformats++] = *format;
251 	return 0;
252 }
253 
254 static void
255 gcscaudio_attach(device_t parent, device_t self, void *aux)
256 {
257 	struct gcscaudio_softc *sc;
258 	struct pci_attach_args *pa;
259 	const char *intrstr;
260 	pci_intr_handle_t ih;
261 	int rc, i;
262 	char intrbuf[PCI_INTRSTR_LEN];
263 
264 	sc = device_private(self);
265 
266 	sc->sc_dev = self;
267 
268 	aprint_naive(": Audio controller\n");
269 
270 	pa = aux;
271 	sc->sc_pc = pa->pa_pc;
272 	sc->sc_pt = pa->pa_tag;
273 	sc->sc_dmat = pa->pa_dmat;
274 	LIST_INIT(&sc->sc_dmalist);
275 	sc->sc_mch_split_buf = NULL;
276 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
277 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
278 
279 	aprint_normal(": AMD Geode CS5536 Audio\n");
280 
281 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
282 	    &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)) {
283 		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
284 		return;
285 	}
286 
287 	if (pci_intr_map(pa, &ih)) {
288 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
289 		goto attach_failure_unmap;
290 	}
291 	intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, sizeof(intrbuf));
292 
293 	sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, ih, IPL_AUDIO,
294 	    gcscaudio_intr, sc, device_xname(self));
295 	if (sc->sc_ih == NULL) {
296 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
297 		if (intrstr != NULL)
298 			aprint_error(" at %s", intrstr);
299 		aprint_error("\n");
300 		goto attach_failure_unmap;
301 	}
302 
303 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
304 
305 
306 	if (gcscaudio_allocate_dma(sc, sizeof(*sc->sc_prd.p_prdtables),
307 	    (void **)&(sc->sc_prd.p_prdtables), sc->sc_prd.p_prdsegs, 1,
308 	    &(sc->sc_prd.p_prdnseg), &(sc->sc_prd.p_prdmap)) != 0)
309 		goto attach_failure_intr;
310 
311 	sc->host_if.arg = sc;
312 	sc->host_if.attach = gcscaudio_attach_codec;
313 	sc->host_if.read = gcscaudio_read_codec;
314 	sc->host_if.write = gcscaudio_write_codec;
315 	sc->host_if.reset = gcscaudio_reset_codec;
316 	sc->host_if.spdif_event = gcscaudio_spdif_event_codec;
317 
318 	if ((rc = ac97_attach(&sc->host_if, self, &sc->sc_lock)) != 0) {
319 		aprint_error_dev(sc->sc_dev,
320 		    "can't attach codec (error=%d)\n", rc);
321 		goto attach_failure_intr;
322 	}
323 
324 	if (!pmf_device_register(self, NULL, gcscaudio_resume))
325 		aprint_error_dev(self, "couldn't establish power handler\n");
326 
327 
328 	sc->sc_nformats = 0;
329 	gcscaudio_append_formats(sc, &gcscaudio_formats_2ch);
330 
331 	mutex_enter(&sc->sc_lock);
332 	if (AC97_IS_4CH(sc->codec_if))
333 		gcscaudio_append_formats(sc, &gcscaudio_formats_4ch);
334 	if (AC97_IS_6CH(sc->codec_if))
335 		gcscaudio_append_formats(sc, &gcscaudio_formats_6ch);
336 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
337 		for (i = 0; i < sc->sc_nformats; i++) {
338 			sc->sc_formats[i].frequency_type = 1;
339 			sc->sc_formats[i].frequency[0] = 48000;
340 		}
341 	}
342 	mutex_exit(&sc->sc_lock);
343 
344 	audio_attach_mi(&gcscaudio_hw_if, sc, sc->sc_dev);
345 	return;
346 
347 attach_failure_intr:
348 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
349 attach_failure_unmap:
350 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
351 	return;
352 }
353 
354 static int
355 gcscaudio_attach_codec(void *arg, struct ac97_codec_if *codec_if)
356 {
357 	struct gcscaudio_softc *sc;
358 
359 	sc = (struct gcscaudio_softc *)arg;
360 	sc->codec_if = codec_if;
361 	return 0;
362 }
363 
364 static int
365 gcscaudio_reset_codec(void *arg)
366 {
367 	struct gcscaudio_softc *sc;
368 	sc = (struct gcscaudio_softc *)arg;
369 
370 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
371 	    ACC_CODEC_CNTL_LNK_WRM_RST |
372 	    ACC_CODEC_CNTL_CMD_NEW);
373 
374 	if (gcscaudio_wait_ready_codec(sc, "reset timeout\n"))
375 		return 1;
376 
377 	return 0;
378 }
379 
380 static void
381 gcscaudio_spdif_event_codec(void *arg, bool flag)
382 {
383 	struct gcscaudio_softc *sc;
384 
385 	sc = (struct gcscaudio_softc *)arg;
386 	sc->sc_spdif = flag;
387 }
388 
389 static int
390 gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *timeout_msg)
391 {
392 	int i;
393 
394 #define GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT	500
395 	for (i = GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT; (i >= 0) &&
396 	    (bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL) &
397 	    ACC_CODEC_CNTL_CMD_NEW); i--)
398 		delay(1);
399 
400 	if (i < 0) {
401 		aprint_error_dev(sc->sc_dev, "%s", timeout_msg);
402 		return 1;
403 	}
404 
405 	return 0;
406 }
407 
408 static int
409 gcscaudio_write_codec(void *arg, uint8_t reg, uint16_t val)
410 {
411 	struct gcscaudio_softc *sc;
412 
413 	sc = (struct gcscaudio_softc *)arg;
414 
415 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
416 	    ACC_CODEC_CNTL_WRITE_CMD |
417 	    ACC_CODEC_CNTL_CMD_NEW |
418 	    ACC_CODEC_REG2ADDR(reg) |
419 	    (val & ACC_CODEC_CNTL_CMD_DATA_MASK));
420 
421 	if (gcscaudio_wait_ready_codec(sc, "codec write timeout\n"))
422 		return 1;
423 
424 #ifdef GCSCAUDIO_CODEC_DEBUG
425 	aprint_error_dev(sc->sc_dev, "codec write: reg=0x%02x, val=0x%04x\n",
426 	    reg, val);
427 #endif
428 
429 	return 0;
430 }
431 
432 static int
433 gcscaudio_read_codec(void *arg, uint8_t reg, uint16_t *val)
434 {
435 	struct gcscaudio_softc *sc;
436 	uint32_t v;
437 	int i;
438 
439 	sc = (struct gcscaudio_softc *)arg;
440 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
441 	    ACC_CODEC_CNTL_READ_CMD | ACC_CODEC_CNTL_CMD_NEW |
442 	    ACC_CODEC_REG2ADDR(reg));
443 
444 	if (gcscaudio_wait_ready_codec(sc, "codec write timeout for reading"))
445 		return 1;
446 
447 #define GCSCAUDIO_READ_CODEC_TIMEOUT	50
448 	for (i = GCSCAUDIO_READ_CODEC_TIMEOUT; i >= 0; i--) {
449 		v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_STATUS);
450 		if ((v & ACC_CODEC_STATUS_STS_NEW) &&
451 		    (ACC_CODEC_ADDR2REG(v) == reg))
452 			break;
453 
454 		delay(10);
455 	}
456 
457 	if (i < 0) {
458 		aprint_error_dev(sc->sc_dev, "codec read timeout\n");
459 		return 1;
460 	}
461 
462 #ifdef GCSCAUDIO_CODEC_DEBUG
463 	aprint_error_dev(sc->sc_dev, "codec read: reg=0x%02x, val=0x%04x\n",
464 	    reg, v & ACC_CODEC_STATUS_STS_DATA_MASK);
465 #endif
466 
467 	*val = v;
468 	return 0;
469 }
470 
471 static int
472 gcscaudio_open(void *arg, int flags)
473 {
474 	struct gcscaudio_softc *sc;
475 
476 	sc = (struct gcscaudio_softc *)arg;
477 	sc->codec_if->vtbl->lock(sc->codec_if);
478 	return 0;
479 }
480 
481 static void
482 gcscaudio_close(void *arg)
483 {
484 	struct gcscaudio_softc *sc;
485 
486 	sc = (struct gcscaudio_softc *)arg;
487 	sc->codec_if->vtbl->unlock(sc->codec_if);
488 }
489 
490 static int
491 gcscaudio_query_format(void *arg, audio_format_query_t *afp)
492 {
493 	struct gcscaudio_softc *sc;
494 
495 	sc = (struct gcscaudio_softc *)arg;
496 	return audio_query_format(sc->sc_formats, sc->sc_nformats, afp);
497 }
498 
499 static int
500 gcscaudio_set_format(void *arg, int setmode,
501                      const audio_params_t *play, const audio_params_t *rec,
502                      audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
503 {
504 	struct gcscaudio_softc *sc;
505 	int rate;
506 	int error;
507 
508 	sc = (struct gcscaudio_softc *)arg;
509 
510 	if (setmode & AUMODE_PLAY) {
511 		if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
512 			/* setup rate of DAC */
513 			rate = play->sample_rate;
514 			if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
515 			    AC97_REG_PCM_FRONT_DAC_RATE, &rate)) != 0)
516 				return error;
517 
518 			/* additional rate of DAC for Surround */
519 			rate = play->sample_rate;
520 			if ((play->channels >= 4) &&
521 			    (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
522 			    AC97_REG_PCM_SURR_DAC_RATE, &rate)) != 0)
523 				return error;
524 
525 			/* additional rate of DAC for LowFrequencyEffect */
526 			rate = play->sample_rate;
527 			if ((play->channels == 6) &&
528 			    (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
529 			    AC97_REG_PCM_LFE_DAC_RATE, &rate)) != 0)
530 				return error;
531 		}
532 		sc->sc_play.ch_params = *rec;
533 	}
534 	if (setmode & AUMODE_RECORD) {
535 		if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
536 			/* setup rate of ADC */
537 			rate = rec->sample_rate;
538 			if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
539 			    AC97_REG_PCM_LR_ADC_RATE, &rate)) != 0)
540 				return error;
541 		}
542 		sc->sc_rec.ch_params = *rec;
543 	}
544 
545 	return 0;
546 }
547 
548 static int
549 gcscaudio_round_blocksize(void *arg, int blk, int mode,
550                           const audio_params_t *param)
551 {
552 	blk &= -4;
553 	if (blk > GCSCAUDIO_PRD_SIZE_MAX)
554 		blk = GCSCAUDIO_PRD_SIZE_MAX;
555 
556 	return blk;
557 }
558 
559 static int
560 gcscaudio_halt_output(void *arg)
561 {
562 	struct gcscaudio_softc *sc;
563 
564 	sc = (struct gcscaudio_softc *)arg;
565 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
566 	    ACC_BMx_CMD_BM_CTL_DISABLE);
567 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
568 	    ACC_BMx_CMD_BM_CTL_DISABLE);
569 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
570 	    ACC_BMx_CMD_BM_CTL_DISABLE);
571 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
572 	    ACC_BMx_CMD_BM_CTL_DISABLE);
573 	sc->sc_play.ch_intr = NULL;
574 
575 	/* channel splitter */
576 	sc->sc_mch_splitter = NULL;
577 	if (sc->sc_mch_split_buf)
578 		gcscaudio_free(sc, sc->sc_mch_split_buf, sc->sc_mch_split_size);
579 	sc->sc_mch_split_buf = NULL;
580 
581 	return 0;
582 }
583 
584 static int
585 gcscaudio_halt_input(void *arg)
586 {
587 	struct gcscaudio_softc *sc;
588 
589 	sc = (struct gcscaudio_softc *)arg;
590 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
591 	    ACC_BMx_CMD_BM_CTL_DISABLE);
592 	sc->sc_rec.ch_intr = NULL;
593 	return 0;
594 }
595 
596 static int
597 gcscaudio_getdev(void *addr, struct audio_device *retp)
598 {
599 	*retp = gcscaudio_device;
600 	return 0;
601 }
602 
603 static int
604 gcscaudio_set_port(void *addr, mixer_ctrl_t *cp)
605 {
606 	struct gcscaudio_softc *sc;
607 
608 	sc = addr;
609 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
610 }
611 
612 static int
613 gcscaudio_get_port(void *addr, mixer_ctrl_t *cp)
614 {
615 	struct gcscaudio_softc *sc;
616 
617 	sc = addr;
618 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
619 }
620 
621 static int
622 gcscaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
623 {
624 	struct gcscaudio_softc *sc;
625 
626 	sc = addr;
627 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
628 }
629 
630 static void *
631 gcscaudio_malloc(void *arg, int direction, size_t size)
632 {
633 	struct gcscaudio_softc *sc;
634 	struct gcscaudio_dma *p;
635 	int error;
636 
637 	sc = (struct gcscaudio_softc *)arg;
638 
639 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
640 	p->size = size;
641 
642 	error = gcscaudio_allocate_dma(sc, size, &p->addr,
643 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]), &p->nseg, &p->map);
644 	if (error) {
645 		kmem_free(p, sizeof(*p));
646 		return NULL;
647 	}
648 
649 	LIST_INSERT_HEAD(&sc->sc_dmalist, p, list);
650 	return p->addr;
651 }
652 
653 static void
654 gcscaudio_free(void *arg, void *ptr, size_t size)
655 {
656 	struct gcscaudio_softc *sc;
657 	struct gcscaudio_dma *p;
658 
659 	sc = (struct gcscaudio_softc *)arg;
660 
661 	LIST_FOREACH(p, &sc->sc_dmalist, list) {
662 		if (p->addr == ptr) {
663 			bus_dmamap_unload(sc->sc_dmat, p->map);
664 			bus_dmamap_destroy(sc->sc_dmat, p->map);
665 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
666 			bus_dmamem_free(sc->sc_dmat, p->segs, p->nseg);
667 
668 			LIST_REMOVE(p, list);
669 			kmem_free(p, sizeof(*p));
670 			break;
671 		}
672 	}
673 }
674 
675 static size_t
676 gcscaudio_round_buffersize(void *addr, int direction, size_t size)
677 {
678 	if (size > GCSCAUDIO_BUFSIZE_MAX)
679 		size = GCSCAUDIO_BUFSIZE_MAX;
680 
681 	return size;
682 }
683 
684 static int
685 gcscaudio_get_props(void *addr)
686 {
687 	struct gcscaudio_softc *sc;
688 	int props;
689 
690 	sc = (struct gcscaudio_softc *)addr;
691 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
692 	/*
693 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
694 	 * rate because of aurateconv.  Applications can't know what rate the
695 	 * device can process in the case of mmap().
696 	 */
697 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
698 		props |= AUDIO_PROP_MMAP;
699 	return props;
700 }
701 
702 static int
703 build_prdtables(struct gcscaudio_softc *sc, int prdidx,
704                 void *addr, size_t size, int blksize, int blklen, int blkoff)
705 {
706 	struct gcscaudio_dma *p;
707 	struct acc_prd *prdp;
708 	bus_addr_t paddr;
709 	int i;
710 
711 	/* get physical address of start */
712 	paddr = (bus_addr_t)0;
713 	LIST_FOREACH(p, &sc->sc_dmalist, list) {
714 		if (p->addr == addr) {
715 			paddr = p->map->dm_segs[0].ds_addr;
716 			break;
717 		}
718 	}
719 	if (!paddr) {
720 		aprint_error_dev(sc->sc_dev, "bad addr %p\n", addr);
721 		return EINVAL;
722 	}
723 
724 #define PRDADDR(prdidx,idx) \
725 	(sc->sc_prd.p_prdmap->dm_segs[0].ds_addr) + sizeof(struct acc_prd) * \
726 	(((prdidx) * GCSCAUDIO_NPRDTABLE) + (idx))
727 
728 	/*
729 	 * build PRD table
730 	 *   prdtbl[] = <PRD0>, <PRD1>, <PRD2>, ..., <PRDn>, <jmp to PRD0>
731 	 */
732 	prdp = sc->sc_prd.p_prdtables->prdtbl[prdidx];
733 	for (i = 0; size > 0; size -= blksize, i++) {
734 		prdp[i].address = paddr + blksize * i + blkoff;
735 		prdp[i].ctrlsize =
736 		    (size < blklen ? size : blklen) | ACC_BMx_PRD_CTRL_EOP;
737 	}
738 	prdp[i].address = PRDADDR(prdidx, 0);
739 	prdp[i].ctrlsize = ACC_BMx_PRD_CTRL_JMP;
740 
741 	bus_dmamap_sync(sc->sc_dmat, sc->sc_prd.p_prdmap, 0,
742 	    sizeof(struct acc_prd) * i, BUS_DMASYNC_PREWRITE);
743 
744 	return 0;
745 }
746 
747 static void
748 split_buffer_4ch(void *dst, void *src, int size, int blksize)
749 {
750 	int left, i;
751 	uint16_t *s, *d;
752 
753 	/*
754 	 * src[blk0]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
755 	 * src[blk1]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
756 	 * src[blk2]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
757 	 *     :
758 	 *
759 	 *   rearrange to
760 	 *
761 	 * src[blk0]: L,R,L,R,L,R,L,R,..
762 	 * src[blk1]: L,R,L,R,L,R,L,R,..
763 	 * src[blk2]: L,R,L,R,L,R,L,R,..
764 	 *     :
765 	 * dst[blk0]: SL,SR,SL,SR,SL,SR,SL,SR,..
766 	 * dst[blk1]: SL,SR,SL,SR,SL,SR,SL,SR,..
767 	 * dst[blk2]: SL,SR,SL,SR,SL,SR,SL,SR,..
768 	 *     :
769 	 */
770 	for (left = size; left > 0; left -= blksize) {
771 		s = (uint16_t *)src;
772 		d = (uint16_t *)dst;
773 		for (i = 0; i < blksize / sizeof(uint16_t) / 4; i++) {
774 			/* L,R,SL,SR -> SL,SR */
775 			s++;
776 			s++;
777 			*d++ = *s++;
778 			*d++ = *s++;
779 		}
780 
781 		s = (uint16_t *)src;
782 		d = (uint16_t *)src;
783 		for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
784 			/* L,R,SL,SR -> L,R */
785 			*d++ = *s++;
786 			*d++ = *s++;
787 			s++;
788 			s++;
789 		}
790 
791 		src = (char *)src + blksize;
792 		dst = (char *)dst + blksize;
793 	}
794 }
795 
796 static void
797 split_buffer_6ch(void *dst, void *src, int size, int blksize)
798 {
799 	int left, i;
800 	uint16_t *s, *d, *dc, *dl;
801 
802 	/*
803 	 * by default, treat as WAV style 5.1ch order
804 	 *   5.1ch(WAV): L R C LFE SL SR
805 	 *   5.1ch(AAC): C L R SL SR LFE
806 	 *        :
807 	 */
808 
809 	/*
810 	 * src[blk0]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
811 	 * src[blk1]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
812 	 * src[blk2]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
813 	 *     :
814 	 * src[N-1] : L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
815 	 *
816 	 *   rearrange to
817 	 *
818 	 * src[blk0]: L,R,L,R,..
819 	 * src[blk1]: L,R,L,R,..
820 	 * src[blk2]: L,R,L,R,..
821 	 *     :
822 	 *
823 	 * dst[blk0]: SL,SR,SL,SR,..
824 	 * dst[blk1]: SL,SR,SL,SR,..
825 	 * dst[blk2]: SL,SR,SL,SR,..
826 	 *     :
827 	 *
828 	 * dst[N/2+0]: C,C,C,..
829 	 * dst[N/2+1]: C,C,C,..
830 	 *     :
831 	 *
832 	 * dst[N/2+N/4+0]: LFE,LFE,LFE,..
833 	 * dst[N/2+N/4+1]: LFE,LFE,LFE,..
834 	 *     :
835 	 */
836 
837 	for (left = size; left > 0; left -= blksize) {
838 		s = (uint16_t *)src;
839 		d = (uint16_t *)dst;
840 		dc = (uint16_t *)((char *)dst + blksize / 2);
841 		dl = (uint16_t *)((char *)dst + blksize / 2 + blksize / 4);
842 		for (i = 0; i < blksize / sizeof(uint16_t) / 6; i++) {
843 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
844 			/*
845 			 * AAC: [C,L,R,SL,SR,LFE]
846 			 *  => [SL,SR]
847 			 *  => [C]
848 			 *  => [LFE]
849 			 */
850 			*dc++ = s[0];	/* C */
851 			*dl++ = s[5];	/* LFE */
852 			*d++ = s[3];	/* SL */
853 			*d++ = s[4];	/* SR */
854 #else
855 			/*
856 			 * WAV: [L,R,C,LFE,SL,SR]
857 			 *  => [SL,SR]
858 			 *  => [C]
859 			 *  => [LFE]
860 			 */
861 			*dc++ = s[2];	/* C */
862 			*dl++ = s[3];	/* LFE */
863 			*d++ = s[4];	/* SL */
864 			*d++ = s[5];	/* SR */
865 #endif
866 			s += 6;
867 		}
868 
869 		s = (uint16_t *)src;
870 		d = (uint16_t *)src;
871 		for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
872 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
873 			/* AAC: [C,L,R,SL,SR,LFE] => [L,R] */
874 			*d++ = s[1];
875 			*d++ = s[2];
876 #else
877 			/* WAV: [L,R,C,LFE,SL,SR] => [L,R] */
878 			*d++ = s[0];
879 			*d++ = s[1];
880 #endif
881 			s += 6;
882 		}
883 
884 		src = (char *)src + blksize;
885 		dst = (char *)dst + blksize;
886 	}
887 }
888 
889 static void
890 channel_splitter(struct gcscaudio_softc *sc)
891 {
892 	int splitsize, left;
893 	void *src, *dst;
894 
895 	if (sc->sc_mch_splitter == NULL)
896 		return;
897 
898 	left = sc->sc_mch_split_size - sc->sc_mch_split_off;
899 	splitsize = sc->sc_mch_split_blksize;
900 	if (left < splitsize)
901 		splitsize = left;
902 
903 	src = (char *)sc->sc_mch_split_start + sc->sc_mch_split_off;
904 	dst = (char *)sc->sc_mch_split_buf + sc->sc_mch_split_off;
905 
906 	sc->sc_mch_splitter(dst, src, splitsize, sc->sc_mch_split_blksize);
907 
908 	sc->sc_mch_split_off += sc->sc_mch_split_blksize;
909 	if (sc->sc_mch_split_off >= sc->sc_mch_split_size)
910 		sc->sc_mch_split_off = 0;
911 }
912 
913 static int
914 gcscaudio_trigger_output(void *addr, void *start, void *end, int blksize,
915                          void (*intr)(void *), void *arg,
916                          const audio_params_t *param)
917 {
918 	struct gcscaudio_softc *sc;
919 	size_t size;
920 
921 	sc = (struct gcscaudio_softc *)addr;
922 	sc->sc_play.ch_intr = intr;
923 	sc->sc_play.ch_intr_arg = arg;
924 	size = (char *)end - (char *)start;
925 
926 	switch (sc->sc_play.ch_params.channels) {
927 	case 2:
928 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
929 		    blksize, 0))
930 			return EINVAL;
931 
932 		if (!AC97_IS_4CH(sc->codec_if)) {
933 			/*
934 			 * output 2ch PCM to FRONT.LR(BM0)
935 			 *
936 			 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
937 			 *
938 			 */
939 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
940 			    PRDADDR(PRD_TABLE_FRONT, 0));
941 
942 			/* start DMA transfer */
943 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
944 			    ACC_BMx_CMD_WRITE |
945 			    ACC_BMx_CMD_BYTE_ORD_EL |
946 			    ACC_BMx_CMD_BM_CTL_ENABLE);
947 		} else {
948 			/*
949 			 * output same PCM to FRONT.LR(BM0) and SURROUND.LR(BM6).
950 			 * CENTER(BM4) and LFE(BM7) doesn't sound.
951 			 *
952 			 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
953 			 *                             BM6: (same of BM0)
954 			 *                             BM4: none
955 			 *                             BM7: none
956 			 */
957 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
958 			    PRDADDR(PRD_TABLE_FRONT, 0));
959 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
960 			    PRDADDR(PRD_TABLE_FRONT, 0));
961 
962 			/* start DMA transfer */
963 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
964 			    ACC_BMx_CMD_WRITE |
965 			    ACC_BMx_CMD_BYTE_ORD_EL |
966 			    ACC_BMx_CMD_BM_CTL_ENABLE);
967 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
968 			    ACC_BMx_CMD_WRITE |
969 			    ACC_BMx_CMD_BYTE_ORD_EL |
970 			    ACC_BMx_CMD_BM_CTL_ENABLE);
971 		}
972 		break;
973 	case 4:
974 		/*
975 		 * output 4ch PCM split to FRONT.LR(BM0) and SURROUND.LR(BM6).
976 		 * CENTER(BM4) and LFE(BM7) doesn't sound.
977 		 *
978 		 * rearrange ordered channel to continuous per channel
979 		 *
980 		 *   4ch: L,R,SL,SR,L,R,SL,SR,... => BM0: L,R,L,R,...
981 		 *                                   BM6: SL,SR,SL,SR,...
982 		 *                                   BM4: none
983 		 *                                   BM7: none
984 		 */
985 		if (sc->sc_mch_split_buf)
986 			gcscaudio_free(sc, sc->sc_mch_split_buf,
987 			    sc->sc_mch_split_size);
988 
989 		if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
990 		    size)) == NULL)
991 			return ENOMEM;
992 
993 		/*
994 		 * 1st and 2nd blocks are split immediately.
995 		 * Other blocks will be split synchronous with intr.
996 		 */
997 		split_buffer_4ch(sc->sc_mch_split_buf, start, blksize * 2,
998 		    blksize);
999 
1000 		sc->sc_mch_split_start = start;
1001 		sc->sc_mch_split_size = size;
1002 		sc->sc_mch_split_blksize = blksize;
1003 		sc->sc_mch_split_off = (blksize * 2) % size;
1004 		sc->sc_mch_splitter = split_buffer_4ch;	/* split function */
1005 
1006 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1007 		    blksize / 2, 0))
1008 			return EINVAL;
1009 		if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1010 		    size, blksize, blksize / 2, 0))
1011 			return EINVAL;
1012 
1013 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1014 		    PRDADDR(PRD_TABLE_FRONT, 0));
1015 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1016 		    PRDADDR(PRD_TABLE_SURR, 0));
1017 
1018 		/* start DMA transfer */
1019 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1020 		    ACC_BMx_CMD_WRITE |
1021 		    ACC_BMx_CMD_BYTE_ORD_EL |
1022 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1023 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1024 		    ACC_BMx_CMD_WRITE |
1025 		    ACC_BMx_CMD_BYTE_ORD_EL |
1026 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1027 		break;
1028 	case 6:
1029 		/*
1030 		 * output 6ch PCM split to
1031 		 * FRONT.LR(BM0), SURROUND.LR(BM6), CENTER(BM4) and LFE(BM7)
1032 		 *
1033 		 * rearrange ordered channel to continuous per channel
1034 		 *
1035 		 *   5.1ch: L,R,C,LFE,SL,SR,... => BM0: L,R,...
1036 		 *                                 BM4: C,...
1037 		 *                                 BM6: SL,SR,...
1038 		 *                                 BM7: LFE,...
1039 		 *
1040 		 */
1041 		if (sc->sc_mch_split_buf)
1042 			gcscaudio_free(sc, sc->sc_mch_split_buf,
1043 			    sc->sc_mch_split_size);
1044 
1045 		if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
1046 		    size)) == NULL)
1047 			return ENOMEM;
1048 
1049 		/*
1050 		 * 1st and 2nd blocks are split immediately.
1051 		 * Other block will be split synchronous with intr.
1052 		 */
1053 		split_buffer_6ch(sc->sc_mch_split_buf, start, blksize * 2,
1054 		    blksize);
1055 
1056 		sc->sc_mch_split_start = start;
1057 		sc->sc_mch_split_size = size;
1058 		sc->sc_mch_split_blksize = blksize;
1059 		sc->sc_mch_split_off = (blksize * 2) % size;
1060 		sc->sc_mch_splitter = split_buffer_6ch;	/* split function */
1061 
1062 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1063 		    blksize / 3, 0))
1064 			return EINVAL;
1065 		if (build_prdtables(sc, PRD_TABLE_CENTER, sc->sc_mch_split_buf,
1066 		    size, blksize, blksize / 3, blksize / 2))
1067 			return EINVAL;
1068 		if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1069 		    size, blksize, blksize / 3, 0))
1070 			return EINVAL;
1071 		if (build_prdtables(sc, PRD_TABLE_LFE, sc->sc_mch_split_buf,
1072 		    size, blksize, blksize / 3, blksize / 2 + blksize / 4))
1073 			return EINVAL;
1074 
1075 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1076 		    PRDADDR(PRD_TABLE_FRONT, 0));
1077 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_PRD,
1078 		    PRDADDR(PRD_TABLE_CENTER, 0));
1079 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1080 		    PRDADDR(PRD_TABLE_SURR, 0));
1081 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_PRD,
1082 		    PRDADDR(PRD_TABLE_LFE, 0));
1083 
1084 		/* start DMA transfer */
1085 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1086 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1087 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1088 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
1089 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1090 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1091 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1092 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1093 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1094 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
1095 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1096 		    ACC_BMx_CMD_BM_CTL_ENABLE);
1097 		break;
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 static int
1104 gcscaudio_trigger_input(void *addr, void *start, void *end, int blksize,
1105                         void (*intr)(void *), void *arg,
1106                         const audio_params_t *param)
1107 {
1108 	struct gcscaudio_softc *sc;
1109 	size_t size;
1110 
1111 	sc = (struct gcscaudio_softc *)addr;
1112 	sc->sc_rec.ch_intr = intr;
1113 	sc->sc_rec.ch_intr_arg = arg;
1114 	size = (char *)end - (char *)start;
1115 
1116 	if (build_prdtables(sc, PRD_TABLE_REC, start, size, blksize, blksize, 0))
1117 		return EINVAL;
1118 
1119 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_PRD,
1120 	    PRDADDR(PRD_TABLE_REC, 0));
1121 
1122 	/* start transfer */
1123 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
1124 	    ACC_BMx_CMD_READ |
1125 	    ACC_BMx_CMD_BYTE_ORD_EL |
1126 	    ACC_BMx_CMD_BM_CTL_ENABLE);
1127 
1128 	return 0;
1129 }
1130 
1131 static void
1132 gcscaudio_get_locks(void *arg, kmutex_t **intr, kmutex_t **thread)
1133 {
1134 	struct gcscaudio_softc *sc;
1135 
1136 	sc = (struct gcscaudio_softc *)arg;
1137 
1138 	*intr = &sc->sc_intr_lock;
1139 	*thread = &sc->sc_lock;
1140 }
1141 
1142 static int
1143 gcscaudio_intr(void *arg)
1144 {
1145 	struct gcscaudio_softc *sc;
1146 	uint16_t intr;
1147 	uint8_t bmstat;
1148 	int nintr;
1149 
1150 	nintr = 0;
1151 	sc = (struct gcscaudio_softc *)arg;
1152 
1153 	mutex_spin_enter(&sc->sc_intr_lock);
1154 
1155 	intr = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ACC_IRQ_STATUS);
1156 	if (intr == 0)
1157 		goto done;
1158 
1159 	/* Front output */
1160 	if (intr & ACC_IRQ_STATUS_BM0_IRQ_STS) {
1161 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_STATUS);
1162 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1163 			aprint_normal_dev(sc->sc_dev, "BM0: Bus Master Error\n");
1164 		if (!(bmstat & ACC_BMx_STATUS_EOP))
1165 			aprint_normal_dev(sc->sc_dev, "BM0: NO End of Page?\n");
1166 
1167 		if (sc->sc_play.ch_intr) {
1168 			sc->sc_play.ch_intr(sc->sc_play.ch_intr_arg);
1169 			channel_splitter(sc);
1170 		}
1171 		nintr++;
1172 	}
1173 
1174 	/* Center output */
1175 	if (intr & ACC_IRQ_STATUS_BM4_IRQ_STS) {
1176 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_STATUS);
1177 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1178 			aprint_normal_dev(sc->sc_dev, "BM4: Bus Master Error\n");
1179 		if (!(bmstat & ACC_BMx_STATUS_EOP))
1180 			aprint_normal_dev(sc->sc_dev, "BM4: NO End of Page?\n");
1181 
1182 		nintr++;
1183 	}
1184 
1185 	/* Surround output */
1186 	if (intr & ACC_IRQ_STATUS_BM6_IRQ_STS) {
1187 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_STATUS);
1188 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1189 			aprint_normal_dev(sc->sc_dev, "BM6: Bus Master Error\n");
1190 		if (!(bmstat & ACC_BMx_STATUS_EOP))
1191 			aprint_normal_dev(sc->sc_dev, "BM6: NO End of Page?\n");
1192 
1193 		nintr++;
1194 	}
1195 
1196 	/* LowFrequencyEffect output */
1197 	if (intr & ACC_IRQ_STATUS_BM7_IRQ_STS) {
1198 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_STATUS);
1199 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1200 			aprint_normal_dev(sc->sc_dev, "BM7: Bus Master Error\n");
1201 		if (!(bmstat & ACC_BMx_STATUS_EOP))
1202 			aprint_normal_dev(sc->sc_dev, "BM7: NO End of Page?\n");
1203 
1204 		nintr++;
1205 	}
1206 
1207 	/* record */
1208 	if (intr & ACC_IRQ_STATUS_BM1_IRQ_STS) {
1209 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_STATUS);
1210 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1211 			aprint_normal_dev(sc->sc_dev, "BM1: Bus Master Error\n");
1212 		if (!(bmstat & ACC_BMx_STATUS_EOP))
1213 			aprint_normal_dev(sc->sc_dev, "BM1: NO End of Page?\n");
1214 
1215 		if (sc->sc_rec.ch_intr) {
1216 			sc->sc_rec.ch_intr(sc->sc_rec.ch_intr_arg);
1217 		}
1218 		nintr++;
1219 	}
1220 
1221 #ifdef GCSCAUDIO_DEBUG
1222 	if (intr & ACC_IRQ_STATUS_IRQ_STS)
1223 		aprint_normal_dev(sc->sc_dev, "Codec GPIO IRQ Status\n");
1224 	if (intr & ACC_IRQ_STATUS_WU_IRQ_STS)
1225 		aprint_normal_dev(sc->sc_dev, "Codec GPIO Wakeup IRQ Status\n");
1226 	if (intr & ACC_IRQ_STATUS_BM2_IRQ_STS)
1227 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 2 IRQ Status\n");
1228 	if (intr & ACC_IRQ_STATUS_BM3_IRQ_STS)
1229 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 3 IRQ Status\n");
1230 	if (intr & ACC_IRQ_STATUS_BM5_IRQ_STS)
1231 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 5 IRQ Status\n");
1232 #endif
1233 
1234 done:
1235 	mutex_spin_exit(&sc->sc_intr_lock);
1236 
1237 	return nintr ? 1 : 0;
1238 }
1239 
1240 static bool
1241 gcscaudio_resume(device_t dv, const pmf_qual_t *qual)
1242 {
1243 	struct gcscaudio_softc *sc = device_private(dv);
1244 
1245 	gcscaudio_reset_codec(sc);
1246 	DELAY(1000);
1247 	(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1248 
1249 	return true;
1250 }
1251 
1252 static int
1253 gcscaudio_allocate_dma(struct gcscaudio_softc *sc, size_t size, void **addrp,
1254                        bus_dma_segment_t *seglist, int nseg, int *rsegp,
1255                        bus_dmamap_t *mapp)
1256 {
1257 	int error;
1258 
1259 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seglist,
1260 	    nseg, rsegp, BUS_DMA_WAITOK)) != 0) {
1261 		aprint_error_dev(sc->sc_dev,
1262 		    "unable to allocate DMA buffer, error=%d\n", error);
1263 		goto fail_alloc;
1264 	}
1265 
1266 	if ((error = bus_dmamem_map(sc->sc_dmat, seglist, nseg, size, addrp,
1267 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
1268 		aprint_error_dev(sc->sc_dev,
1269 		    "unable to map DMA buffer, error=%d\n",
1270 		    error);
1271 		goto fail_map;
1272 	}
1273 
1274 	if ((error = bus_dmamap_create(sc->sc_dmat, size, nseg, size, 0,
1275 	    BUS_DMA_WAITOK, mapp)) != 0) {
1276 		aprint_error_dev(sc->sc_dev,
1277 		    "unable to create DMA map, error=%d\n", error);
1278 		goto fail_create;
1279 	}
1280 
1281 	if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
1282 	    BUS_DMA_WAITOK)) != 0) {
1283 		aprint_error_dev(sc->sc_dev,
1284 		    "unable to load DMA map, error=%d\n", error);
1285 		goto fail_load;
1286 	}
1287 
1288 	return 0;
1289 
1290 fail_load:
1291 	bus_dmamap_destroy(sc->sc_dmat, *mapp);
1292 fail_create:
1293 	bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
1294 fail_map:
1295 	bus_dmamem_free(sc->sc_dmat, seglist, nseg);
1296 fail_alloc:
1297 	return error;
1298 }
1299