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