Home | History | Annotate | Line # | Download | only in pci
gcscaudio.c revision 1.16.2.2
      1 /*	$NetBSD: gcscaudio.c,v 1.16.2.2 2019/04/28 07:01:45 isaki 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.16.2.2 2019/04/28 07:01:45 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_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 	.drain			= NULL,
    185 	.query_format		= gcscaudio_query_format,
    186 	.set_format		= gcscaudio_set_format,
    187 	.round_blocksize	= gcscaudio_round_blocksize,
    188 	.commit_settings	= NULL,
    189 	.init_output		= NULL,
    190 	.init_input		= NULL,
    191 	.start_output		= NULL,
    192 	.start_input		= NULL,
    193 	.halt_output		= gcscaudio_halt_output,
    194 	.halt_input		= gcscaudio_halt_input,
    195 	.speaker_ctl		= NULL,
    196 	.getdev			= gcscaudio_getdev,
    197 	.setfd			= NULL,
    198 	.set_port		= gcscaudio_set_port,
    199 	.get_port		= gcscaudio_get_port,
    200 	.query_devinfo		= gcscaudio_query_devinfo,
    201 	.allocm			= gcscaudio_malloc,
    202 	.freem			= gcscaudio_free,
    203 	.round_buffersize	= gcscaudio_round_buffersize,
    204 	.get_props		= gcscaudio_get_props,
    205 	.trigger_output		= gcscaudio_trigger_output,
    206 	.trigger_input		= gcscaudio_trigger_input,
    207 	.dev_ioctl		= NULL,
    208 	.get_locks		= gcscaudio_get_locks,
    209 };
    210 
    211 #define GCSCAUDIO_FORMAT(aumode, ch, chmask) \
    212 	{ \
    213 		.mode		= (aumode), \
    214 		.encoding	= AUDIO_ENCODING_SLINEAR_LE, \
    215 		.validbits	= 16, \
    216 		.precision	= 16, \
    217 		.channels	= (ch), \
    218 		.channel_mask	= (chmask), \
    219 		.frequency_type	= 0, \
    220 		.frequency	= { 8000, 48000 }, \
    221 	}
    222 static const struct audio_format gcscaudio_formats_2ch =
    223 	GCSCAUDIO_FORMAT(AUMODE_PLAY | AUMODE_RECORD, 2, AUFMT_STEREO);
    224 
    225 static const struct audio_format gcscaudio_formats_4ch =
    226 	GCSCAUDIO_FORMAT(AUMODE_PLAY                , 4, AUFMT_SURROUND4);
    227 
    228 static const struct audio_format gcscaudio_formats_6ch =
    229 	GCSCAUDIO_FORMAT(AUMODE_PLAY                , 6, AUFMT_DOLBY_5_1);
    230 
    231 static int
    232 gcscaudio_match(device_t parent, cfdata_t match, void *aux)
    233 {
    234 	struct pci_attach_args *pa;
    235 
    236 	pa = (struct pci_attach_args *)aux;
    237 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) &&
    238 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_CS5536_AUDIO))
    239 		return 1;
    240 
    241 	return 0;
    242 }
    243 
    244 static int
    245 gcscaudio_append_formats(struct gcscaudio_softc *sc,
    246                          const struct audio_format *format)
    247 {
    248 	if (sc->sc_nformats >= GCSCAUDIO_MAXFORMATS) {
    249 		aprint_error_dev(sc->sc_dev, "too many formats\n");
    250 		return EINVAL;
    251 	}
    252 	sc->sc_formats[sc->sc_nformats++] = *format;
    253 	return 0;
    254 }
    255 
    256 static void
    257 gcscaudio_attach(device_t parent, device_t self, void *aux)
    258 {
    259 	struct gcscaudio_softc *sc;
    260 	struct pci_attach_args *pa;
    261 	const char *intrstr;
    262 	pci_intr_handle_t ih;
    263 	int rc, i;
    264 	char intrbuf[PCI_INTRSTR_LEN];
    265 
    266 	sc = device_private(self);
    267 
    268 	sc->sc_dev = 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_AUDIO);
    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, intrbuf, sizeof(intrbuf));
    294 
    295 	sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, ih, IPL_AUDIO,
    296 	    gcscaudio_intr, sc, device_xname(self));
    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 
    333 	mutex_enter(&sc->sc_lock);
    334 	if (AC97_IS_4CH(sc->codec_if))
    335 		gcscaudio_append_formats(sc, &gcscaudio_formats_4ch);
    336 	if (AC97_IS_6CH(sc->codec_if))
    337 		gcscaudio_append_formats(sc, &gcscaudio_formats_6ch);
    338 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
    339 		for (i = 0; i < sc->sc_nformats; i++) {
    340 			sc->sc_formats[i].frequency_type = 1;
    341 			sc->sc_formats[i].frequency[0] = 48000;
    342 		}
    343 	}
    344 	mutex_exit(&sc->sc_lock);
    345 
    346 	audio_attach_mi(&gcscaudio_hw_if, sc, sc->sc_dev);
    347 	return;
    348 
    349 attach_failure_intr:
    350 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    351 attach_failure_unmap:
    352 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    353 	return;
    354 }
    355 
    356 static int
    357 gcscaudio_attach_codec(void *arg, struct ac97_codec_if *codec_if)
    358 {
    359 	struct gcscaudio_softc *sc;
    360 
    361 	sc = (struct gcscaudio_softc *)arg;
    362 	sc->codec_if = codec_if;
    363 	return 0;
    364 }
    365 
    366 static int
    367 gcscaudio_reset_codec(void *arg)
    368 {
    369 	struct gcscaudio_softc *sc;
    370 	sc = (struct gcscaudio_softc *)arg;
    371 
    372 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
    373 	    ACC_CODEC_CNTL_LNK_WRM_RST |
    374 	    ACC_CODEC_CNTL_CMD_NEW);
    375 
    376 	if (gcscaudio_wait_ready_codec(sc, "reset timeout\n"))
    377 		return 1;
    378 
    379 	return 0;
    380 }
    381 
    382 static void
    383 gcscaudio_spdif_event_codec(void *arg, bool flag)
    384 {
    385 	struct gcscaudio_softc *sc;
    386 
    387 	sc = (struct gcscaudio_softc *)arg;
    388 	sc->sc_spdif = flag;
    389 }
    390 
    391 static int
    392 gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *timeout_msg)
    393 {
    394 	int i;
    395 
    396 #define GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT	500
    397 	for (i = GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT; (i >= 0) &&
    398 	    (bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL) &
    399 	    ACC_CODEC_CNTL_CMD_NEW); i--)
    400 		delay(1);
    401 
    402 	if (i < 0) {
    403 		aprint_error_dev(sc->sc_dev, "%s", timeout_msg);
    404 		return 1;
    405 	}
    406 
    407 	return 0;
    408 }
    409 
    410 static int
    411 gcscaudio_write_codec(void *arg, uint8_t reg, uint16_t val)
    412 {
    413 	struct gcscaudio_softc *sc;
    414 
    415 	sc = (struct gcscaudio_softc *)arg;
    416 
    417 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
    418 	    ACC_CODEC_CNTL_WRITE_CMD |
    419 	    ACC_CODEC_CNTL_CMD_NEW |
    420 	    ACC_CODEC_REG2ADDR(reg) |
    421 	    (val & ACC_CODEC_CNTL_CMD_DATA_MASK));
    422 
    423 	if (gcscaudio_wait_ready_codec(sc, "codec write timeout\n"))
    424 		return 1;
    425 
    426 #ifdef GCSCAUDIO_CODEC_DEBUG
    427 	aprint_error_dev(sc->sc_dev, "codec write: reg=0x%02x, val=0x%04x\n",
    428 	    reg, val);
    429 #endif
    430 
    431 	return 0;
    432 }
    433 
    434 static int
    435 gcscaudio_read_codec(void *arg, uint8_t reg, uint16_t *val)
    436 {
    437 	struct gcscaudio_softc *sc;
    438 	uint32_t v;
    439 	int i;
    440 
    441 	sc = (struct gcscaudio_softc *)arg;
    442 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
    443 	    ACC_CODEC_CNTL_READ_CMD | ACC_CODEC_CNTL_CMD_NEW |
    444 	    ACC_CODEC_REG2ADDR(reg));
    445 
    446 	if (gcscaudio_wait_ready_codec(sc, "codec write timeout for reading"))
    447 		return 1;
    448 
    449 #define GCSCAUDIO_READ_CODEC_TIMEOUT	50
    450 	for (i = GCSCAUDIO_READ_CODEC_TIMEOUT; i >= 0; i--) {
    451 		v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_STATUS);
    452 		if ((v & ACC_CODEC_STATUS_STS_NEW) &&
    453 		    (ACC_CODEC_ADDR2REG(v) == reg))
    454 			break;
    455 
    456 		delay(10);
    457 	}
    458 
    459 	if (i < 0) {
    460 		aprint_error_dev(sc->sc_dev, "codec read timeout\n");
    461 		return 1;
    462 	}
    463 
    464 #ifdef GCSCAUDIO_CODEC_DEBUG
    465 	aprint_error_dev(sc->sc_dev, "codec read: reg=0x%02x, val=0x%04x\n",
    466 	    reg, v & ACC_CODEC_STATUS_STS_DATA_MASK);
    467 #endif
    468 
    469 	*val = v;
    470 	return 0;
    471 }
    472 
    473 static int
    474 gcscaudio_open(void *arg, int flags)
    475 {
    476 	struct gcscaudio_softc *sc;
    477 
    478 	sc = (struct gcscaudio_softc *)arg;
    479 	sc->codec_if->vtbl->lock(sc->codec_if);
    480 	return 0;
    481 }
    482 
    483 static void
    484 gcscaudio_close(void *arg)
    485 {
    486 	struct gcscaudio_softc *sc;
    487 
    488 	sc = (struct gcscaudio_softc *)arg;
    489 	sc->codec_if->vtbl->unlock(sc->codec_if);
    490 }
    491 
    492 static int
    493 gcscaudio_query_format(void *arg, audio_format_query_t *afp)
    494 {
    495 	struct gcscaudio_softc *sc;
    496 
    497 	sc = (struct gcscaudio_softc *)arg;
    498 	return audio_query_format(sc->sc_formats, sc->sc_nformats, afp);
    499 }
    500 
    501 static int
    502 gcscaudio_set_format(void *arg, int setmode,
    503                      const audio_params_t *play, const audio_params_t *rec,
    504                      audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    505 {
    506 	struct gcscaudio_softc *sc;
    507 	int rate;
    508 	int error;
    509 
    510 	sc = (struct gcscaudio_softc *)arg;
    511 
    512 	if (setmode & AUMODE_PLAY) {
    513 		if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
    514 			/* setup rate of DAC */
    515 			rate = play->sample_rate;
    516 			if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
    517 			    AC97_REG_PCM_FRONT_DAC_RATE, &rate)) != 0)
    518 				return error;
    519 
    520 			/* additional rate of DAC for Surround */
    521 			rate = play->sample_rate;
    522 			if ((play->channels >= 4) &&
    523 			    (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
    524 			    AC97_REG_PCM_SURR_DAC_RATE, &rate)) != 0)
    525 				return error;
    526 
    527 			/* additional rate of DAC for LowFrequencyEffect */
    528 			rate = play->sample_rate;
    529 			if ((play->channels == 6) &&
    530 			    (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
    531 			    AC97_REG_PCM_LFE_DAC_RATE, &rate)) != 0)
    532 				return error;
    533 		}
    534 		sc->sc_play.ch_params = *rec;
    535 	}
    536 	if (setmode & AUMODE_RECORD) {
    537 		if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
    538 			/* setup rate of ADC */
    539 			rate = rec->sample_rate;
    540 			if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
    541 			    AC97_REG_PCM_LR_ADC_RATE, &rate)) != 0)
    542 				return error;
    543 		}
    544 		sc->sc_rec.ch_params = *rec;
    545 	}
    546 
    547 	return 0;
    548 }
    549 
    550 static int
    551 gcscaudio_round_blocksize(void *arg, int blk, int mode,
    552                           const audio_params_t *param)
    553 {
    554 	blk &= -4;
    555 	if (blk > GCSCAUDIO_PRD_SIZE_MAX)
    556 		blk = GCSCAUDIO_PRD_SIZE_MAX;
    557 
    558 	return blk;
    559 }
    560 
    561 static int
    562 gcscaudio_halt_output(void *arg)
    563 {
    564 	struct gcscaudio_softc *sc;
    565 
    566 	sc = (struct gcscaudio_softc *)arg;
    567 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
    568 	    ACC_BMx_CMD_BM_CTL_DISABLE);
    569 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
    570 	    ACC_BMx_CMD_BM_CTL_DISABLE);
    571 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
    572 	    ACC_BMx_CMD_BM_CTL_DISABLE);
    573 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
    574 	    ACC_BMx_CMD_BM_CTL_DISABLE);
    575 	sc->sc_play.ch_intr = NULL;
    576 
    577 	/* channel splitter */
    578 	sc->sc_mch_splitter = NULL;
    579 	if (sc->sc_mch_split_buf)
    580 		gcscaudio_free(sc, sc->sc_mch_split_buf, sc->sc_mch_split_size);
    581 	sc->sc_mch_split_buf = NULL;
    582 
    583 	return 0;
    584 }
    585 
    586 static int
    587 gcscaudio_halt_input(void *arg)
    588 {
    589 	struct gcscaudio_softc *sc;
    590 
    591 	sc = (struct gcscaudio_softc *)arg;
    592 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
    593 	    ACC_BMx_CMD_BM_CTL_DISABLE);
    594 	sc->sc_rec.ch_intr = NULL;
    595 	return 0;
    596 }
    597 
    598 static int
    599 gcscaudio_getdev(void *addr, struct audio_device *retp)
    600 {
    601 	*retp = gcscaudio_device;
    602 	return 0;
    603 }
    604 
    605 static int
    606 gcscaudio_set_port(void *addr, mixer_ctrl_t *cp)
    607 {
    608 	struct gcscaudio_softc *sc;
    609 
    610 	sc = addr;
    611 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    612 }
    613 
    614 static int
    615 gcscaudio_get_port(void *addr, mixer_ctrl_t *cp)
    616 {
    617 	struct gcscaudio_softc *sc;
    618 
    619 	sc = addr;
    620 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    621 }
    622 
    623 static int
    624 gcscaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    625 {
    626 	struct gcscaudio_softc *sc;
    627 
    628 	sc = addr;
    629 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    630 }
    631 
    632 static void *
    633 gcscaudio_malloc(void *arg, int direction, size_t size)
    634 {
    635 	struct gcscaudio_softc *sc;
    636 	struct gcscaudio_dma *p;
    637 	int error;
    638 
    639 	sc = (struct gcscaudio_softc *)arg;
    640 
    641 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
    642 	p->size = size;
    643 
    644 	error = gcscaudio_allocate_dma(sc, size, &p->addr,
    645 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]), &p->nseg, &p->map);
    646 	if (error) {
    647 		kmem_free(p, sizeof(*p));
    648 		return NULL;
    649 	}
    650 
    651 	LIST_INSERT_HEAD(&sc->sc_dmalist, p, list);
    652 	return p->addr;
    653 }
    654 
    655 static void
    656 gcscaudio_free(void *arg, void *ptr, size_t size)
    657 {
    658 	struct gcscaudio_softc *sc;
    659 	struct gcscaudio_dma *p;
    660 
    661 	sc = (struct gcscaudio_softc *)arg;
    662 
    663 	LIST_FOREACH(p, &sc->sc_dmalist, list) {
    664 		if (p->addr == ptr) {
    665 			bus_dmamap_unload(sc->sc_dmat, p->map);
    666 			bus_dmamap_destroy(sc->sc_dmat, p->map);
    667 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    668 			bus_dmamem_free(sc->sc_dmat, p->segs, p->nseg);
    669 
    670 			LIST_REMOVE(p, list);
    671 			kmem_free(p, sizeof(*p));
    672 			break;
    673 		}
    674 	}
    675 }
    676 
    677 static size_t
    678 gcscaudio_round_buffersize(void *addr, int direction, size_t size)
    679 {
    680 	if (size > GCSCAUDIO_BUFSIZE_MAX)
    681 		size = GCSCAUDIO_BUFSIZE_MAX;
    682 
    683 	return size;
    684 }
    685 
    686 static int
    687 gcscaudio_get_props(void *addr)
    688 {
    689 	struct gcscaudio_softc *sc;
    690 	int props;
    691 
    692 	sc = (struct gcscaudio_softc *)addr;
    693 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    694 	/*
    695 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
    696 	 * rate because of aurateconv.  Applications can't know what rate the
    697 	 * device can process in the case of mmap().
    698 	 */
    699 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
    700 		props |= AUDIO_PROP_MMAP;
    701 	return props;
    702 }
    703 
    704 static int
    705 build_prdtables(struct gcscaudio_softc *sc, int prdidx,
    706                 void *addr, size_t size, int blksize, int blklen, int blkoff)
    707 {
    708 	struct gcscaudio_dma *p;
    709 	struct acc_prd *prdp;
    710 	bus_addr_t paddr;
    711 	int i;
    712 
    713 	/* get physical address of start */
    714 	paddr = (bus_addr_t)0;
    715 	LIST_FOREACH(p, &sc->sc_dmalist, list) {
    716 		if (p->addr == addr) {
    717 			paddr = p->map->dm_segs[0].ds_addr;
    718 			break;
    719 		}
    720 	}
    721 	if (!paddr) {
    722 		aprint_error_dev(sc->sc_dev, "bad addr %p\n", addr);
    723 		return EINVAL;
    724 	}
    725 
    726 #define PRDADDR(prdidx,idx) \
    727 	(sc->sc_prd.p_prdmap->dm_segs[0].ds_addr) + sizeof(struct acc_prd) * \
    728 	(((prdidx) * GCSCAUDIO_NPRDTABLE) + (idx))
    729 
    730 	/*
    731 	 * build PRD table
    732 	 *   prdtbl[] = <PRD0>, <PRD1>, <PRD2>, ..., <PRDn>, <jmp to PRD0>
    733 	 */
    734 	prdp = sc->sc_prd.p_prdtables->prdtbl[prdidx];
    735 	for (i = 0; size > 0; size -= blksize, i++) {
    736 		prdp[i].address = paddr + blksize * i + blkoff;
    737 		prdp[i].ctrlsize =
    738 		    (size < blklen ? size : blklen) | ACC_BMx_PRD_CTRL_EOP;
    739 	}
    740 	prdp[i].address = PRDADDR(prdidx, 0);
    741 	prdp[i].ctrlsize = ACC_BMx_PRD_CTRL_JMP;
    742 
    743 	bus_dmamap_sync(sc->sc_dmat, sc->sc_prd.p_prdmap, 0,
    744 	    sizeof(struct acc_prd) * i, BUS_DMASYNC_PREWRITE);
    745 
    746 	return 0;
    747 }
    748 
    749 static void
    750 split_buffer_4ch(void *dst, void *src, int size, int blksize)
    751 {
    752 	int left, i;
    753 	uint16_t *s, *d;
    754 
    755 	/*
    756 	 * src[blk0]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
    757 	 * src[blk1]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
    758 	 * src[blk2]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
    759 	 *     :
    760 	 *
    761 	 *   rearrange to
    762 	 *
    763 	 * src[blk0]: L,R,L,R,L,R,L,R,..
    764 	 * src[blk1]: L,R,L,R,L,R,L,R,..
    765 	 * src[blk2]: L,R,L,R,L,R,L,R,..
    766 	 *     :
    767 	 * dst[blk0]: SL,SR,SL,SR,SL,SR,SL,SR,..
    768 	 * dst[blk1]: SL,SR,SL,SR,SL,SR,SL,SR,..
    769 	 * dst[blk2]: SL,SR,SL,SR,SL,SR,SL,SR,..
    770 	 *     :
    771 	 */
    772 	for (left = size; left > 0; left -= blksize) {
    773 		s = (uint16_t *)src;
    774 		d = (uint16_t *)dst;
    775 		for (i = 0; i < blksize / sizeof(uint16_t) / 4; i++) {
    776 			/* L,R,SL,SR -> SL,SR */
    777 			s++;
    778 			s++;
    779 			*d++ = *s++;
    780 			*d++ = *s++;
    781 		}
    782 
    783 		s = (uint16_t *)src;
    784 		d = (uint16_t *)src;
    785 		for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
    786 			/* L,R,SL,SR -> L,R */
    787 			*d++ = *s++;
    788 			*d++ = *s++;
    789 			s++;
    790 			s++;
    791 		}
    792 
    793 		src = (char *)src + blksize;
    794 		dst = (char *)dst + blksize;
    795 	}
    796 }
    797 
    798 static void
    799 split_buffer_6ch(void *dst, void *src, int size, int blksize)
    800 {
    801 	int left, i;
    802 	uint16_t *s, *d, *dc, *dl;
    803 
    804 	/*
    805 	 * by default, treat as WAV style 5.1ch order
    806 	 *   5.1ch(WAV): L R C LFE SL SR
    807 	 *   5.1ch(AAC): C L R SL SR LFE
    808 	 *        :
    809 	 */
    810 
    811 	/*
    812 	 * src[blk0]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
    813 	 * src[blk1]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
    814 	 * src[blk2]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
    815 	 *     :
    816 	 * src[N-1] : L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
    817 	 *
    818 	 *   rearrange to
    819 	 *
    820 	 * src[blk0]: L,R,L,R,..
    821 	 * src[blk1]: L,R,L,R,..
    822 	 * src[blk2]: L,R,L,R,..
    823 	 *     :
    824 	 *
    825 	 * dst[blk0]: SL,SR,SL,SR,..
    826 	 * dst[blk1]: SL,SR,SL,SR,..
    827 	 * dst[blk2]: SL,SR,SL,SR,..
    828 	 *     :
    829 	 *
    830 	 * dst[N/2+0]: C,C,C,..
    831 	 * dst[N/2+1]: C,C,C,..
    832 	 *     :
    833 	 *
    834 	 * dst[N/2+N/4+0]: LFE,LFE,LFE,..
    835 	 * dst[N/2+N/4+1]: LFE,LFE,LFE,..
    836 	 *     :
    837 	 */
    838 
    839 	for (left = size; left > 0; left -= blksize) {
    840 		s = (uint16_t *)src;
    841 		d = (uint16_t *)dst;
    842 		dc = (uint16_t *)((char *)dst + blksize / 2);
    843 		dl = (uint16_t *)((char *)dst + blksize / 2 + blksize / 4);
    844 		for (i = 0; i < blksize / sizeof(uint16_t) / 6; i++) {
    845 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
    846 			/*
    847 			 * AAC: [C,L,R,SL,SR,LFE]
    848 			 *  => [SL,SR]
    849 			 *  => [C]
    850 			 *  => [LFE]
    851 			 */
    852 			*dc++ = s[0];	/* C */
    853 			*dl++ = s[5];	/* LFE */
    854 			*d++ = s[3];	/* SL */
    855 			*d++ = s[4];	/* SR */
    856 #else
    857 			/*
    858 			 * WAV: [L,R,C,LFE,SL,SR]
    859 			 *  => [SL,SR]
    860 			 *  => [C]
    861 			 *  => [LFE]
    862 			 */
    863 			*dc++ = s[2];	/* C */
    864 			*dl++ = s[3];	/* LFE */
    865 			*d++ = s[4];	/* SL */
    866 			*d++ = s[5];	/* SR */
    867 #endif
    868 			s += 6;
    869 		}
    870 
    871 		s = (uint16_t *)src;
    872 		d = (uint16_t *)src;
    873 		for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
    874 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
    875 			/* AAC: [C,L,R,SL,SR,LFE] => [L,R] */
    876 			*d++ = s[1];
    877 			*d++ = s[2];
    878 #else
    879 			/* WAV: [L,R,C,LFE,SL,SR] => [L,R] */
    880 			*d++ = s[0];
    881 			*d++ = s[1];
    882 #endif
    883 			s += 6;
    884 		}
    885 
    886 		src = (char *)src + blksize;
    887 		dst = (char *)dst + blksize;
    888 	}
    889 }
    890 
    891 static void
    892 channel_splitter(struct gcscaudio_softc *sc)
    893 {
    894 	int splitsize, left;
    895 	void *src, *dst;
    896 
    897 	if (sc->sc_mch_splitter == NULL)
    898 		return;
    899 
    900 	left = sc->sc_mch_split_size - sc->sc_mch_split_off;
    901 	splitsize = sc->sc_mch_split_blksize;
    902 	if (left < splitsize)
    903 		splitsize = left;
    904 
    905 	src = (char *)sc->sc_mch_split_start + sc->sc_mch_split_off;
    906 	dst = (char *)sc->sc_mch_split_buf + sc->sc_mch_split_off;
    907 
    908 	sc->sc_mch_splitter(dst, src, splitsize, sc->sc_mch_split_blksize);
    909 
    910 	sc->sc_mch_split_off += sc->sc_mch_split_blksize;
    911 	if (sc->sc_mch_split_off >= sc->sc_mch_split_size)
    912 		sc->sc_mch_split_off = 0;
    913 }
    914 
    915 static int
    916 gcscaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    917                          void (*intr)(void *), void *arg,
    918                          const audio_params_t *param)
    919 {
    920 	struct gcscaudio_softc *sc;
    921 	size_t size;
    922 
    923 	sc = (struct gcscaudio_softc *)addr;
    924 	sc->sc_play.ch_intr = intr;
    925 	sc->sc_play.ch_intr_arg = arg;
    926 	size = (char *)end - (char *)start;
    927 
    928 	switch (sc->sc_play.ch_params.channels) {
    929 	case 2:
    930 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
    931 		    blksize, 0))
    932 			return EINVAL;
    933 
    934 		if (!AC97_IS_4CH(sc->codec_if)) {
    935 			/*
    936 			 * output 2ch PCM to FRONT.LR(BM0)
    937 			 *
    938 			 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
    939 			 *
    940 			 */
    941 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
    942 			    PRDADDR(PRD_TABLE_FRONT, 0));
    943 
    944 			/* start DMA transfer */
    945 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
    946 			    ACC_BMx_CMD_WRITE |
    947 			    ACC_BMx_CMD_BYTE_ORD_EL |
    948 			    ACC_BMx_CMD_BM_CTL_ENABLE);
    949 		} else {
    950 			/*
    951 			 * output same PCM to FRONT.LR(BM0) and SURROUND.LR(BM6).
    952 			 * CENTER(BM4) and LFE(BM7) doesn't sound.
    953 			 *
    954 			 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
    955 			 *                             BM6: (same of BM0)
    956 			 *                             BM4: none
    957 			 *                             BM7: none
    958 			 */
    959 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
    960 			    PRDADDR(PRD_TABLE_FRONT, 0));
    961 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
    962 			    PRDADDR(PRD_TABLE_FRONT, 0));
    963 
    964 			/* start DMA transfer */
    965 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
    966 			    ACC_BMx_CMD_WRITE |
    967 			    ACC_BMx_CMD_BYTE_ORD_EL |
    968 			    ACC_BMx_CMD_BM_CTL_ENABLE);
    969 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
    970 			    ACC_BMx_CMD_WRITE |
    971 			    ACC_BMx_CMD_BYTE_ORD_EL |
    972 			    ACC_BMx_CMD_BM_CTL_ENABLE);
    973 		}
    974 		break;
    975 	case 4:
    976 		/*
    977 		 * output 4ch PCM split to FRONT.LR(BM0) and SURROUND.LR(BM6).
    978 		 * CENTER(BM4) and LFE(BM7) doesn't sound.
    979 		 *
    980 		 * rearrange ordered channel to continuous per channel
    981 		 *
    982 		 *   4ch: L,R,SL,SR,L,R,SL,SR,... => BM0: L,R,L,R,...
    983 		 *                                   BM6: SL,SR,SL,SR,...
    984 		 *                                   BM4: none
    985 		 *                                   BM7: none
    986 		 */
    987 		if (sc->sc_mch_split_buf)
    988 			gcscaudio_free(sc, sc->sc_mch_split_buf,
    989 			    sc->sc_mch_split_size);
    990 
    991 		if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
    992 		    size)) == NULL)
    993 			return ENOMEM;
    994 
    995 		/*
    996 		 * 1st and 2nd blocks are split immediately.
    997 		 * Other blocks will be split synchronous with intr.
    998 		 */
    999 		split_buffer_4ch(sc->sc_mch_split_buf, start, blksize * 2,
   1000 		    blksize);
   1001 
   1002 		sc->sc_mch_split_start = start;
   1003 		sc->sc_mch_split_size = size;
   1004 		sc->sc_mch_split_blksize = blksize;
   1005 		sc->sc_mch_split_off = (blksize * 2) % size;
   1006 		sc->sc_mch_splitter = split_buffer_4ch;	/* split function */
   1007 
   1008 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
   1009 		    blksize / 2, 0))
   1010 			return EINVAL;
   1011 		if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
   1012 		    size, blksize, blksize / 2, 0))
   1013 			return EINVAL;
   1014 
   1015 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
   1016 		    PRDADDR(PRD_TABLE_FRONT, 0));
   1017 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
   1018 		    PRDADDR(PRD_TABLE_SURR, 0));
   1019 
   1020 		/* start DMA transfer */
   1021 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
   1022 		    ACC_BMx_CMD_WRITE |
   1023 		    ACC_BMx_CMD_BYTE_ORD_EL |
   1024 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1025 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
   1026 		    ACC_BMx_CMD_WRITE |
   1027 		    ACC_BMx_CMD_BYTE_ORD_EL |
   1028 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1029 		break;
   1030 	case 6:
   1031 		/*
   1032 		 * output 6ch PCM split to
   1033 		 * FRONT.LR(BM0), SURROUND.LR(BM6), CENTER(BM4) and LFE(BM7)
   1034 		 *
   1035 		 * rearrange ordered channel to continuous per channel
   1036 		 *
   1037 		 *   5.1ch: L,R,C,LFE,SL,SR,... => BM0: L,R,...
   1038 		 *                                 BM4: C,...
   1039 		 *                                 BM6: SL,SR,...
   1040 		 *                                 BM7: LFE,...
   1041 		 *
   1042 		 */
   1043 		if (sc->sc_mch_split_buf)
   1044 			gcscaudio_free(sc, sc->sc_mch_split_buf,
   1045 			    sc->sc_mch_split_size);
   1046 
   1047 		if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
   1048 		    size)) == NULL)
   1049 			return ENOMEM;
   1050 
   1051 		/*
   1052 		 * 1st and 2nd blocks are split immediately.
   1053 		 * Other block will be split synchronous with intr.
   1054 		 */
   1055 		split_buffer_6ch(sc->sc_mch_split_buf, start, blksize * 2,
   1056 		    blksize);
   1057 
   1058 		sc->sc_mch_split_start = start;
   1059 		sc->sc_mch_split_size = size;
   1060 		sc->sc_mch_split_blksize = blksize;
   1061 		sc->sc_mch_split_off = (blksize * 2) % size;
   1062 		sc->sc_mch_splitter = split_buffer_6ch;	/* split function */
   1063 
   1064 		if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
   1065 		    blksize / 3, 0))
   1066 			return EINVAL;
   1067 		if (build_prdtables(sc, PRD_TABLE_CENTER, sc->sc_mch_split_buf,
   1068 		    size, blksize, blksize / 3, blksize / 2))
   1069 			return EINVAL;
   1070 		if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
   1071 		    size, blksize, blksize / 3, 0))
   1072 			return EINVAL;
   1073 		if (build_prdtables(sc, PRD_TABLE_LFE, sc->sc_mch_split_buf,
   1074 		    size, blksize, blksize / 3, blksize / 2 + blksize / 4))
   1075 			return EINVAL;
   1076 
   1077 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
   1078 		    PRDADDR(PRD_TABLE_FRONT, 0));
   1079 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_PRD,
   1080 		    PRDADDR(PRD_TABLE_CENTER, 0));
   1081 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
   1082 		    PRDADDR(PRD_TABLE_SURR, 0));
   1083 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_PRD,
   1084 		    PRDADDR(PRD_TABLE_LFE, 0));
   1085 
   1086 		/* start DMA transfer */
   1087 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
   1088 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
   1089 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1090 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
   1091 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
   1092 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1093 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
   1094 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
   1095 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1096 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
   1097 		    ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
   1098 		    ACC_BMx_CMD_BM_CTL_ENABLE);
   1099 		break;
   1100 	}
   1101 
   1102 	return 0;
   1103 }
   1104 
   1105 static int
   1106 gcscaudio_trigger_input(void *addr, void *start, void *end, int blksize,
   1107                         void (*intr)(void *), void *arg,
   1108                         const audio_params_t *param)
   1109 {
   1110 	struct gcscaudio_softc *sc;
   1111 	size_t size;
   1112 
   1113 	sc = (struct gcscaudio_softc *)addr;
   1114 	sc->sc_rec.ch_intr = intr;
   1115 	sc->sc_rec.ch_intr_arg = arg;
   1116 	size = (char *)end - (char *)start;
   1117 
   1118 	if (build_prdtables(sc, PRD_TABLE_REC, start, size, blksize, blksize, 0))
   1119 		return EINVAL;
   1120 
   1121 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_PRD,
   1122 	    PRDADDR(PRD_TABLE_REC, 0));
   1123 
   1124 	/* start transfer */
   1125 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
   1126 	    ACC_BMx_CMD_READ |
   1127 	    ACC_BMx_CMD_BYTE_ORD_EL |
   1128 	    ACC_BMx_CMD_BM_CTL_ENABLE);
   1129 
   1130 	return 0;
   1131 }
   1132 
   1133 static void
   1134 gcscaudio_get_locks(void *arg, kmutex_t **intr, kmutex_t **thread)
   1135 {
   1136 	struct gcscaudio_softc *sc;
   1137 
   1138 	sc = (struct gcscaudio_softc *)arg;
   1139 
   1140 	*intr = &sc->sc_intr_lock;
   1141 	*thread = &sc->sc_lock;
   1142 }
   1143 
   1144 static int
   1145 gcscaudio_intr(void *arg)
   1146 {
   1147 	struct gcscaudio_softc *sc;
   1148 	uint16_t intr;
   1149 	uint8_t bmstat;
   1150 	int nintr;
   1151 
   1152 	nintr = 0;
   1153 	sc = (struct gcscaudio_softc *)arg;
   1154 
   1155 	mutex_spin_enter(&sc->sc_intr_lock);
   1156 
   1157 	intr = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ACC_IRQ_STATUS);
   1158 	if (intr == 0)
   1159 		goto done;
   1160 
   1161 	/* Front output */
   1162 	if (intr & ACC_IRQ_STATUS_BM0_IRQ_STS) {
   1163 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_STATUS);
   1164 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
   1165 			aprint_normal_dev(sc->sc_dev, "BM0: Bus Master Error\n");
   1166 		if (!(bmstat & ACC_BMx_STATUS_EOP))
   1167 			aprint_normal_dev(sc->sc_dev, "BM0: NO End of Page?\n");
   1168 
   1169 		if (sc->sc_play.ch_intr) {
   1170 			sc->sc_play.ch_intr(sc->sc_play.ch_intr_arg);
   1171 			channel_splitter(sc);
   1172 		}
   1173 		nintr++;
   1174 	}
   1175 
   1176 	/* Center output */
   1177 	if (intr & ACC_IRQ_STATUS_BM4_IRQ_STS) {
   1178 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_STATUS);
   1179 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
   1180 			aprint_normal_dev(sc->sc_dev, "BM4: Bus Master Error\n");
   1181 		if (!(bmstat & ACC_BMx_STATUS_EOP))
   1182 			aprint_normal_dev(sc->sc_dev, "BM4: NO End of Page?\n");
   1183 
   1184 		nintr++;
   1185 	}
   1186 
   1187 	/* Surround output */
   1188 	if (intr & ACC_IRQ_STATUS_BM6_IRQ_STS) {
   1189 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_STATUS);
   1190 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
   1191 			aprint_normal_dev(sc->sc_dev, "BM6: Bus Master Error\n");
   1192 		if (!(bmstat & ACC_BMx_STATUS_EOP))
   1193 			aprint_normal_dev(sc->sc_dev, "BM6: NO End of Page?\n");
   1194 
   1195 		nintr++;
   1196 	}
   1197 
   1198 	/* LowFrequencyEffect output */
   1199 	if (intr & ACC_IRQ_STATUS_BM7_IRQ_STS) {
   1200 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_STATUS);
   1201 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
   1202 			aprint_normal_dev(sc->sc_dev, "BM7: Bus Master Error\n");
   1203 		if (!(bmstat & ACC_BMx_STATUS_EOP))
   1204 			aprint_normal_dev(sc->sc_dev, "BM7: NO End of Page?\n");
   1205 
   1206 		nintr++;
   1207 	}
   1208 
   1209 	/* record */
   1210 	if (intr & ACC_IRQ_STATUS_BM1_IRQ_STS) {
   1211 		bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_STATUS);
   1212 		if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
   1213 			aprint_normal_dev(sc->sc_dev, "BM1: Bus Master Error\n");
   1214 		if (!(bmstat & ACC_BMx_STATUS_EOP))
   1215 			aprint_normal_dev(sc->sc_dev, "BM1: NO End of Page?\n");
   1216 
   1217 		if (sc->sc_rec.ch_intr) {
   1218 			sc->sc_rec.ch_intr(sc->sc_rec.ch_intr_arg);
   1219 		}
   1220 		nintr++;
   1221 	}
   1222 
   1223 #ifdef GCSCAUDIO_DEBUG
   1224 	if (intr & ACC_IRQ_STATUS_IRQ_STS)
   1225 		aprint_normal_dev(sc->sc_dev, "Codec GPIO IRQ Status\n");
   1226 	if (intr & ACC_IRQ_STATUS_WU_IRQ_STS)
   1227 		aprint_normal_dev(sc->sc_dev, "Codec GPIO Wakeup IRQ Status\n");
   1228 	if (intr & ACC_IRQ_STATUS_BM2_IRQ_STS)
   1229 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 2 IRQ Status\n");
   1230 	if (intr & ACC_IRQ_STATUS_BM3_IRQ_STS)
   1231 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 3 IRQ Status\n");
   1232 	if (intr & ACC_IRQ_STATUS_BM5_IRQ_STS)
   1233 		aprint_normal_dev(sc->sc_dev, "Audio Bus Master 5 IRQ Status\n");
   1234 #endif
   1235 
   1236 done:
   1237 	mutex_spin_exit(&sc->sc_intr_lock);
   1238 
   1239 	return nintr ? 1 : 0;
   1240 }
   1241 
   1242 static bool
   1243 gcscaudio_resume(device_t dv, const pmf_qual_t *qual)
   1244 {
   1245 	struct gcscaudio_softc *sc = device_private(dv);
   1246 
   1247 	gcscaudio_reset_codec(sc);
   1248 	DELAY(1000);
   1249 	(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1250 
   1251 	return true;
   1252 }
   1253 
   1254 static int
   1255 gcscaudio_allocate_dma(struct gcscaudio_softc *sc, size_t size, void **addrp,
   1256                        bus_dma_segment_t *seglist, int nseg, int *rsegp,
   1257                        bus_dmamap_t *mapp)
   1258 {
   1259 	int error;
   1260 
   1261 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seglist,
   1262 	    nseg, rsegp, BUS_DMA_WAITOK)) != 0) {
   1263 		aprint_error_dev(sc->sc_dev,
   1264 		    "unable to allocate DMA buffer, error=%d\n", error);
   1265 		goto fail_alloc;
   1266 	}
   1267 
   1268 	if ((error = bus_dmamem_map(sc->sc_dmat, seglist, nseg, size, addrp,
   1269 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
   1270 		aprint_error_dev(sc->sc_dev,
   1271 		    "unable to map DMA buffer, error=%d\n",
   1272 		    error);
   1273 		goto fail_map;
   1274 	}
   1275 
   1276 	if ((error = bus_dmamap_create(sc->sc_dmat, size, nseg, size, 0,
   1277 	    BUS_DMA_WAITOK, mapp)) != 0) {
   1278 		aprint_error_dev(sc->sc_dev,
   1279 		    "unable to create DMA map, error=%d\n", error);
   1280 		goto fail_create;
   1281 	}
   1282 
   1283 	if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
   1284 	    BUS_DMA_WAITOK)) != 0) {
   1285 		aprint_error_dev(sc->sc_dev,
   1286 		    "unable to load DMA map, error=%d\n", error);
   1287 		goto fail_load;
   1288 	}
   1289 
   1290 	return 0;
   1291 
   1292 fail_load:
   1293 	bus_dmamap_destroy(sc->sc_dmat, *mapp);
   1294 fail_create:
   1295 	bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
   1296 fail_map:
   1297 	bus_dmamem_free(sc->sc_dmat, seglist, nseg);
   1298 fail_alloc:
   1299 	return error;
   1300 }
   1301