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