Home | History | Annotate | Line # | Download | only in pci
auich.c revision 1.2.2.2
      1  1.2.2.2  bouyer /*	$NetBSD: auich.c,v 1.2.2.2 2000/12/08 09:12:30 bouyer Exp $	*/
      2  1.2.2.2  bouyer 
      3  1.2.2.2  bouyer /*-
      4  1.2.2.2  bouyer  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.2.2.2  bouyer  * All rights reserved.
      6  1.2.2.2  bouyer  *
      7  1.2.2.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  bouyer  * by Jason R. Thorpe.
      9  1.2.2.2  bouyer  *
     10  1.2.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.2.2.2  bouyer  * are met:
     13  1.2.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.2.2.2  bouyer  *    must display the following acknowledgement:
     20  1.2.2.2  bouyer  *	This product includes software developed by the NetBSD
     21  1.2.2.2  bouyer  *	Foundation, Inc. and its contributors.
     22  1.2.2.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.2.2  bouyer  *    contributors may be used to endorse or promote products derived
     24  1.2.2.2  bouyer  *    from this software without specific prior written permission.
     25  1.2.2.2  bouyer  *
     26  1.2.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.2.2  bouyer  */
     38  1.2.2.2  bouyer 
     39  1.2.2.2  bouyer /*
     40  1.2.2.2  bouyer  * Copyright (c) 2000 Michael Shalayeff
     41  1.2.2.2  bouyer  * All rights reserved.
     42  1.2.2.2  bouyer  *
     43  1.2.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     44  1.2.2.2  bouyer  * modification, are permitted provided that the following conditions
     45  1.2.2.2  bouyer  * are met:
     46  1.2.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     47  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     48  1.2.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     50  1.2.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     51  1.2.2.2  bouyer  * 3. The name of the author may not be used to endorse or promote products
     52  1.2.2.2  bouyer  *    derived from this software without specific prior written permission.
     53  1.2.2.2  bouyer  *
     54  1.2.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  1.2.2.2  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  1.2.2.2  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  1.2.2.2  bouyer  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     58  1.2.2.2  bouyer  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     59  1.2.2.2  bouyer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     60  1.2.2.2  bouyer  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  1.2.2.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     62  1.2.2.2  bouyer  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     63  1.2.2.2  bouyer  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     64  1.2.2.2  bouyer  * THE POSSIBILITY OF SUCH DAMAGE.
     65  1.2.2.2  bouyer  *
     66  1.2.2.2  bouyer  *	from OpenBSD: ich.c,v 1.3 2000/08/11 06:17:18 mickey Exp
     67  1.2.2.2  bouyer  */
     68  1.2.2.2  bouyer 
     69  1.2.2.2  bouyer /* #define	ICH_DEBUG */
     70  1.2.2.2  bouyer /*
     71  1.2.2.2  bouyer  * AC'97 audio found on Intel 810/820/440MX chipsets.
     72  1.2.2.2  bouyer  *	http://developer.intel.com/design/chipsets/datashts/290655.htm
     73  1.2.2.2  bouyer  *	http://developer.intel.com/design/chipsets/manuals/298028.htm
     74  1.2.2.2  bouyer  *
     75  1.2.2.2  bouyer  * TODO:
     76  1.2.2.2  bouyer  *
     77  1.2.2.2  bouyer  *	- Probe codecs for supported sample rates.
     78  1.2.2.2  bouyer  *
     79  1.2.2.2  bouyer  *	- Add support for the microphone input.
     80  1.2.2.2  bouyer  */
     81  1.2.2.2  bouyer 
     82  1.2.2.2  bouyer #include <sys/param.h>
     83  1.2.2.2  bouyer #include <sys/systm.h>
     84  1.2.2.2  bouyer #include <sys/kernel.h>
     85  1.2.2.2  bouyer #include <sys/malloc.h>
     86  1.2.2.2  bouyer #include <sys/device.h>
     87  1.2.2.2  bouyer #include <sys/fcntl.h>
     88  1.2.2.2  bouyer #include <sys/proc.h>
     89  1.2.2.2  bouyer 
     90  1.2.2.2  bouyer #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
     91  1.2.2.2  bouyer 
     92  1.2.2.2  bouyer #include <dev/pci/pcidevs.h>
     93  1.2.2.2  bouyer #include <dev/pci/pcivar.h>
     94  1.2.2.2  bouyer #include <dev/pci/auichreg.h>
     95  1.2.2.2  bouyer 
     96  1.2.2.2  bouyer #include <sys/audioio.h>
     97  1.2.2.2  bouyer #include <dev/audio_if.h>
     98  1.2.2.2  bouyer #include <dev/mulaw.h>
     99  1.2.2.2  bouyer #include <dev/auconv.h>
    100  1.2.2.2  bouyer 
    101  1.2.2.2  bouyer #include <machine/bus.h>
    102  1.2.2.2  bouyer 
    103  1.2.2.2  bouyer #include <dev/ic/ac97reg.h>
    104  1.2.2.2  bouyer #include <dev/ic/ac97var.h>
    105  1.2.2.2  bouyer 
    106  1.2.2.2  bouyer struct auich_dma {
    107  1.2.2.2  bouyer 	bus_dmamap_t map;
    108  1.2.2.2  bouyer 	caddr_t addr;
    109  1.2.2.2  bouyer 	bus_dma_segment_t segs[1];
    110  1.2.2.2  bouyer 	int nsegs;
    111  1.2.2.2  bouyer 	size_t size;
    112  1.2.2.2  bouyer 	struct auich_dma *next;
    113  1.2.2.2  bouyer };
    114  1.2.2.2  bouyer 
    115  1.2.2.2  bouyer #define	DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
    116  1.2.2.2  bouyer #define	KERNADDR(p)	((void *)((p)->addr))
    117  1.2.2.2  bouyer 
    118  1.2.2.2  bouyer struct auich_cdata {
    119  1.2.2.2  bouyer 	struct auich_dmalist ic_dmalist_pcmo[ICH_DMALIST_MAX];
    120  1.2.2.2  bouyer 	struct auich_dmalist ic_dmalist_pcmi[ICH_DMALIST_MAX];
    121  1.2.2.2  bouyer 	struct auich_dmalist ic_dmalist_mici[ICH_DMALIST_MAX];
    122  1.2.2.2  bouyer };
    123  1.2.2.2  bouyer 
    124  1.2.2.2  bouyer #define	ICH_CDOFF(x)		offsetof(struct auich_cdata, x)
    125  1.2.2.2  bouyer #define	ICH_PCMO_OFF(x)		ICH_CDOFF(ic_dmalist_pcmo[(x)])
    126  1.2.2.2  bouyer #define	ICH_PCMI_OFF(x)		ICH_CDOFF(ic_dmalist_pcmi[(x)])
    127  1.2.2.2  bouyer #define	ICH_MICI_OFF(x)		ICH_CDOFF(ic_dmalist_mici[(x)])
    128  1.2.2.2  bouyer 
    129  1.2.2.2  bouyer struct auich_softc {
    130  1.2.2.2  bouyer 	struct device sc_dev;
    131  1.2.2.2  bouyer 	void *sc_ih;
    132  1.2.2.2  bouyer 
    133  1.2.2.2  bouyer 	audio_device_t sc_audev;
    134  1.2.2.2  bouyer 
    135  1.2.2.2  bouyer 	bus_space_tag_t iot;
    136  1.2.2.2  bouyer 	bus_space_handle_t mix_ioh;
    137  1.2.2.2  bouyer 	bus_space_handle_t aud_ioh;
    138  1.2.2.2  bouyer 	bus_dma_tag_t dmat;
    139  1.2.2.2  bouyer 
    140  1.2.2.2  bouyer 	struct ac97_codec_if *codec_if;
    141  1.2.2.2  bouyer 	struct ac97_host_if host_if;
    142  1.2.2.2  bouyer 
    143  1.2.2.2  bouyer 	/* DMA scatter-gather lists. */
    144  1.2.2.2  bouyer 	bus_dmamap_t sc_cddmamap;
    145  1.2.2.2  bouyer #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    146  1.2.2.2  bouyer 
    147  1.2.2.2  bouyer 	struct auich_cdata *sc_cdata;
    148  1.2.2.2  bouyer #define	dmalist_pcmo	sc_cdata->ic_dmalist_pcmo
    149  1.2.2.2  bouyer #define	dmalist_pcmi	sc_cdata->ic_dmalist_pcmi
    150  1.2.2.2  bouyer #define	dmalist_mici	sc_cdata->ic_dmalist_mici
    151  1.2.2.2  bouyer 
    152  1.2.2.2  bouyer 	int	ptr_pcmo,
    153  1.2.2.2  bouyer 		ptr_pcmi,
    154  1.2.2.2  bouyer 		ptr_mici;
    155  1.2.2.2  bouyer 
    156  1.2.2.2  bouyer 	/* i/o buffer pointers */
    157  1.2.2.2  bouyer 	u_int32_t pcmo_start, pcmo_p, pcmo_end;
    158  1.2.2.2  bouyer 	int pcmo_blksize, pcmo_fifoe;
    159  1.2.2.2  bouyer 
    160  1.2.2.2  bouyer 	u_int32_t pcmi_start, pcmi_p, pcmi_end;
    161  1.2.2.2  bouyer 	int pcmi_blksize, pcmi_fifoe;
    162  1.2.2.2  bouyer 
    163  1.2.2.2  bouyer 	u_int32_t mici_start, mici_p, mici_end;
    164  1.2.2.2  bouyer 	int mici_blksize, mici_fifoe;
    165  1.2.2.2  bouyer 
    166  1.2.2.2  bouyer 	struct auich_dma *sc_dmas;
    167  1.2.2.2  bouyer 
    168  1.2.2.2  bouyer 	void (*sc_pintr)(void *);
    169  1.2.2.2  bouyer 	void *sc_parg;
    170  1.2.2.2  bouyer 
    171  1.2.2.2  bouyer 	void (*sc_rintr)(void *);
    172  1.2.2.2  bouyer 	void *sc_rarg;
    173  1.2.2.2  bouyer };
    174  1.2.2.2  bouyer 
    175  1.2.2.2  bouyer /* Debug */
    176  1.2.2.2  bouyer #ifdef AUDIO_DEBUG
    177  1.2.2.2  bouyer #define	DPRINTF(l,x)	do { if (auich_debug & (l)) printf x; } while(0)
    178  1.2.2.2  bouyer int auich_debug = 0xfffe;
    179  1.2.2.2  bouyer #define	ICH_DEBUG_CODECIO	0x0001
    180  1.2.2.2  bouyer #define	ICH_DEBUG_DMA		0x0002
    181  1.2.2.2  bouyer #define	ICH_DEBUG_PARAM		0x0004
    182  1.2.2.2  bouyer #else
    183  1.2.2.2  bouyer #define	DPRINTF(x,y)	/* nothing */
    184  1.2.2.2  bouyer #endif
    185  1.2.2.2  bouyer 
    186  1.2.2.2  bouyer int	auich_match(struct device *, struct cfdata *, void *);
    187  1.2.2.2  bouyer void	auich_attach(struct device *, struct device *, void *);
    188  1.2.2.2  bouyer int	auich_intr(void *);
    189  1.2.2.2  bouyer 
    190  1.2.2.2  bouyer struct cfattach auich_ca = {
    191  1.2.2.2  bouyer 	sizeof(struct auich_softc), auich_match, auich_attach
    192  1.2.2.2  bouyer };
    193  1.2.2.2  bouyer 
    194  1.2.2.2  bouyer int	auich_open(void *, int);
    195  1.2.2.2  bouyer void	auich_close(void *);
    196  1.2.2.2  bouyer int	auich_query_encoding(void *, struct audio_encoding *);
    197  1.2.2.2  bouyer int	auich_set_params(void *, int, int, struct audio_params *,
    198  1.2.2.2  bouyer 	    struct audio_params *);
    199  1.2.2.2  bouyer int	auich_round_blocksize(void *, int);
    200  1.2.2.2  bouyer int	auich_halt_output(void *);
    201  1.2.2.2  bouyer int	auich_halt_input(void *);
    202  1.2.2.2  bouyer int	auich_getdev(void *, struct audio_device *);
    203  1.2.2.2  bouyer int	auich_set_port(void *, mixer_ctrl_t *);
    204  1.2.2.2  bouyer int	auich_get_port(void *, mixer_ctrl_t *);
    205  1.2.2.2  bouyer int	auich_query_devinfo(void *, mixer_devinfo_t *);
    206  1.2.2.2  bouyer void	*auich_allocm(void *, int, size_t, int, int);
    207  1.2.2.2  bouyer void	auich_freem(void *, void *, int);
    208  1.2.2.2  bouyer size_t	auich_round_buffersize(void *, int, size_t);
    209  1.2.2.2  bouyer paddr_t	auich_mappage(void *, void *, off_t, int);
    210  1.2.2.2  bouyer int	auich_get_props(void *);
    211  1.2.2.2  bouyer int	auich_trigger_output(void *, void *, void *, int, void (*)(void *),
    212  1.2.2.2  bouyer 	    void *, struct audio_params *);
    213  1.2.2.2  bouyer int	auich_trigger_input(void *, void *, void *, int, void (*)(void *),
    214  1.2.2.2  bouyer 	    void *, struct audio_params *);
    215  1.2.2.2  bouyer 
    216  1.2.2.2  bouyer int	auich_alloc_cdata(struct auich_softc *);
    217  1.2.2.2  bouyer 
    218  1.2.2.2  bouyer int	auich_allocmem(struct auich_softc *, size_t, size_t,
    219  1.2.2.2  bouyer 	    struct auich_dma *);
    220  1.2.2.2  bouyer int	auich_freemem(struct auich_softc *, struct auich_dma *);
    221  1.2.2.2  bouyer 
    222  1.2.2.2  bouyer struct audio_hw_if auich_hw_if = {
    223  1.2.2.2  bouyer 	auich_open,
    224  1.2.2.2  bouyer 	auich_close,
    225  1.2.2.2  bouyer 	NULL,			/* drain */
    226  1.2.2.2  bouyer 	auich_query_encoding,
    227  1.2.2.2  bouyer 	auich_set_params,
    228  1.2.2.2  bouyer 	auich_round_blocksize,
    229  1.2.2.2  bouyer 	NULL,			/* commit_setting */
    230  1.2.2.2  bouyer 	NULL,			/* init_output */
    231  1.2.2.2  bouyer 	NULL,			/* init_input */
    232  1.2.2.2  bouyer 	NULL,			/* start_output */
    233  1.2.2.2  bouyer 	NULL,			/* start_input */
    234  1.2.2.2  bouyer 	auich_halt_output,
    235  1.2.2.2  bouyer 	auich_halt_input,
    236  1.2.2.2  bouyer 	NULL,			/* speaker_ctl */
    237  1.2.2.2  bouyer 	auich_getdev,
    238  1.2.2.2  bouyer 	NULL,			/* getfd */
    239  1.2.2.2  bouyer 	auich_set_port,
    240  1.2.2.2  bouyer 	auich_get_port,
    241  1.2.2.2  bouyer 	auich_query_devinfo,
    242  1.2.2.2  bouyer 	auich_allocm,
    243  1.2.2.2  bouyer 	auich_freem,
    244  1.2.2.2  bouyer 	auich_round_buffersize,
    245  1.2.2.2  bouyer 	auich_mappage,
    246  1.2.2.2  bouyer 	auich_get_props,
    247  1.2.2.2  bouyer 	auich_trigger_output,
    248  1.2.2.2  bouyer 	auich_trigger_input,
    249  1.2.2.2  bouyer };
    250  1.2.2.2  bouyer 
    251  1.2.2.2  bouyer int	auich_attach_codec(void *, struct ac97_codec_if *);
    252  1.2.2.2  bouyer int	auich_read_codec(void *, u_int8_t, u_int16_t *);
    253  1.2.2.2  bouyer int	auich_write_codec(void *, u_int8_t, u_int16_t);
    254  1.2.2.2  bouyer void	auich_reset_codec(void *);
    255  1.2.2.2  bouyer 
    256  1.2.2.2  bouyer static const struct auich_devtype {
    257  1.2.2.2  bouyer 	int	product;
    258  1.2.2.2  bouyer 	const char *name;
    259  1.2.2.2  bouyer 	const char *shortname;
    260  1.2.2.2  bouyer } auich_devices[] = {
    261  1.2.2.2  bouyer 	{ PCI_PRODUCT_INTEL_82801AA_ACA,
    262  1.2.2.2  bouyer 	    "i82801AA (ICH) AC-97 Audio",	"ICH" },
    263  1.2.2.2  bouyer 	{ PCI_PRODUCT_INTEL_82801AB_ACA,
    264  1.2.2.2  bouyer 	    "i82801AB (ICH0) AC-97 Audio",	"ICH0" },
    265  1.2.2.2  bouyer 	{ PCI_PRODUCT_INTEL_82801BA_ACA,
    266  1.2.2.2  bouyer 	    "i82801BA (ICH2) AC-97 Audio",	"ICH2" },
    267  1.2.2.2  bouyer 	{ PCI_PRODUCT_INTEL_82440MX_ACA,
    268  1.2.2.2  bouyer 	    "i82440MX AC-97 Audio",		"440MX" },
    269  1.2.2.2  bouyer 
    270  1.2.2.2  bouyer 	{ 0,
    271  1.2.2.2  bouyer 	    NULL,			NULL },
    272  1.2.2.2  bouyer };
    273  1.2.2.2  bouyer 
    274  1.2.2.2  bouyer static const struct auich_devtype *
    275  1.2.2.2  bouyer auich_lookup(struct pci_attach_args *pa)
    276  1.2.2.2  bouyer {
    277  1.2.2.2  bouyer 	const struct auich_devtype *d;
    278  1.2.2.2  bouyer 
    279  1.2.2.2  bouyer 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    280  1.2.2.2  bouyer 		return (NULL);
    281  1.2.2.2  bouyer 
    282  1.2.2.2  bouyer 	for (d = auich_devices; d->name != NULL; d++) {
    283  1.2.2.2  bouyer 		if (PCI_PRODUCT(pa->pa_id) == d->product)
    284  1.2.2.2  bouyer 			return (d);
    285  1.2.2.2  bouyer 	}
    286  1.2.2.2  bouyer 
    287  1.2.2.2  bouyer 	return (NULL);
    288  1.2.2.2  bouyer }
    289  1.2.2.2  bouyer 
    290  1.2.2.2  bouyer int
    291  1.2.2.2  bouyer auich_match(struct device *parent, struct cfdata *match, void *aux)
    292  1.2.2.2  bouyer {
    293  1.2.2.2  bouyer 	struct pci_attach_args *pa = aux;
    294  1.2.2.2  bouyer 
    295  1.2.2.2  bouyer 	if (auich_lookup(pa) != NULL)
    296  1.2.2.2  bouyer 		return (1);
    297  1.2.2.2  bouyer 
    298  1.2.2.2  bouyer 	return (0);
    299  1.2.2.2  bouyer }
    300  1.2.2.2  bouyer 
    301  1.2.2.2  bouyer void
    302  1.2.2.2  bouyer auich_attach(struct device *parent, struct device *self, void *aux)
    303  1.2.2.2  bouyer {
    304  1.2.2.2  bouyer 	struct auich_softc *sc = (struct auich_softc *)self;
    305  1.2.2.2  bouyer 	struct pci_attach_args *pa = aux;
    306  1.2.2.2  bouyer 	pci_intr_handle_t ih;
    307  1.2.2.2  bouyer 	bus_size_t mix_size, aud_size;
    308  1.2.2.2  bouyer 	pcireg_t csr;
    309  1.2.2.2  bouyer 	const char *intrstr;
    310  1.2.2.2  bouyer 	const struct auich_devtype *d;
    311  1.2.2.2  bouyer 
    312  1.2.2.2  bouyer 	d = auich_lookup(pa);
    313  1.2.2.2  bouyer 	if (d == NULL)
    314  1.2.2.2  bouyer 		panic("auich_attach: impossible");
    315  1.2.2.2  bouyer 
    316  1.2.2.2  bouyer 	printf(": %s\n", d->name);
    317  1.2.2.2  bouyer 
    318  1.2.2.2  bouyer 	if (pci_mapreg_map(pa, ICH_NAMBAR, PCI_MAPREG_TYPE_IO, 0,
    319  1.2.2.2  bouyer 			   &sc->iot, &sc->mix_ioh, NULL, &mix_size)) {
    320  1.2.2.2  bouyer 		printf("%s: can't map codec i/o space\n",
    321  1.2.2.2  bouyer 		    sc->sc_dev.dv_xname);
    322  1.2.2.2  bouyer 		return;
    323  1.2.2.2  bouyer 	}
    324  1.2.2.2  bouyer 	if (pci_mapreg_map(pa, ICH_NABMBAR, PCI_MAPREG_TYPE_IO, 0,
    325  1.2.2.2  bouyer 			   &sc->iot, &sc->aud_ioh, NULL, &aud_size)) {
    326  1.2.2.2  bouyer 		printf("%s: can't map device i/o space\n",
    327  1.2.2.2  bouyer 		    sc->sc_dev.dv_xname);
    328  1.2.2.2  bouyer 		return;
    329  1.2.2.2  bouyer 	}
    330  1.2.2.2  bouyer 	sc->dmat = pa->pa_dmat;
    331  1.2.2.2  bouyer 
    332  1.2.2.2  bouyer 	/* enable bus mastering */
    333  1.2.2.2  bouyer 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    334  1.2.2.2  bouyer 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    335  1.2.2.2  bouyer 	    csr | PCI_COMMAND_MASTER_ENABLE);
    336  1.2.2.2  bouyer 
    337  1.2.2.2  bouyer 	/* Map and establish the interrupt. */
    338  1.2.2.2  bouyer 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    339  1.2.2.2  bouyer 			 pa->pa_intrline, &ih)) {
    340  1.2.2.2  bouyer 		printf("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
    341  1.2.2.2  bouyer 		return;
    342  1.2.2.2  bouyer 	}
    343  1.2.2.2  bouyer 	intrstr = pci_intr_string(pa->pa_pc, ih);
    344  1.2.2.2  bouyer 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO,
    345  1.2.2.2  bouyer 	    auich_intr, sc);
    346  1.2.2.2  bouyer 	if (sc->sc_ih == NULL) {
    347  1.2.2.2  bouyer 		printf("%s: can't establish interrupt", sc->sc_dev.dv_xname);
    348  1.2.2.2  bouyer 		if (intrstr != NULL)
    349  1.2.2.2  bouyer 			printf(" at %s", intrstr);
    350  1.2.2.2  bouyer 		printf("\n");
    351  1.2.2.2  bouyer 		return;
    352  1.2.2.2  bouyer 	}
    353  1.2.2.2  bouyer 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    354  1.2.2.2  bouyer 
    355  1.2.2.2  bouyer 	sprintf(sc->sc_audev.name, "%s AC97", d->shortname);
    356  1.2.2.2  bouyer 	sprintf(sc->sc_audev.version, "0x%02x", PCI_REVISION(pa->pa_class));
    357  1.2.2.2  bouyer 	strcpy(sc->sc_audev.config, sc->sc_dev.dv_xname);
    358  1.2.2.2  bouyer 
    359  1.2.2.2  bouyer 	/* Set up DMA lists. */
    360  1.2.2.2  bouyer 	sc->ptr_pcmo = sc->ptr_pcmi = sc->ptr_mici = 0;
    361  1.2.2.2  bouyer 	auich_alloc_cdata(sc);
    362  1.2.2.2  bouyer 
    363  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA, ("auich_attach: lists %p %p %p\n",
    364  1.2.2.2  bouyer 	    sc->dmalist_pcmo, sc->dmalist_pcmi, sc->dmalist_mici));
    365  1.2.2.2  bouyer 
    366  1.2.2.2  bouyer 	/* Reset codec and AC'97 */
    367  1.2.2.2  bouyer 	auich_reset_codec(sc);
    368  1.2.2.2  bouyer 
    369  1.2.2.2  bouyer 	sc->host_if.arg = sc;
    370  1.2.2.2  bouyer 	sc->host_if.attach = auich_attach_codec;
    371  1.2.2.2  bouyer 	sc->host_if.read = auich_read_codec;
    372  1.2.2.2  bouyer 	sc->host_if.write = auich_write_codec;
    373  1.2.2.2  bouyer 	sc->host_if.reset = auich_reset_codec;
    374  1.2.2.2  bouyer 
    375  1.2.2.2  bouyer 	if (ac97_attach(&sc->host_if) != 0)
    376  1.2.2.2  bouyer 		return;
    377  1.2.2.2  bouyer 
    378  1.2.2.2  bouyer 	audio_attach_mi(&auich_hw_if, sc, &sc->sc_dev);
    379  1.2.2.2  bouyer }
    380  1.2.2.2  bouyer 
    381  1.2.2.2  bouyer int
    382  1.2.2.2  bouyer auich_read_codec(void *v, u_int8_t reg, u_int16_t *val)
    383  1.2.2.2  bouyer {
    384  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    385  1.2.2.2  bouyer 	int i;
    386  1.2.2.2  bouyer 
    387  1.2.2.2  bouyer 	/* wait for an access semaphore */
    388  1.2.2.2  bouyer 	for (i = ICH_SEMATIMO; i-- &&
    389  1.2.2.2  bouyer 	    bus_space_read_1(sc->iot, sc->aud_ioh, ICH_CAS) & 1; DELAY(1));
    390  1.2.2.2  bouyer 
    391  1.2.2.2  bouyer 	if (i > 0) {
    392  1.2.2.2  bouyer 		*val = bus_space_read_2(sc->iot, sc->mix_ioh, reg);
    393  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_CODECIO,
    394  1.2.2.2  bouyer 		    ("auich_read_codec(%x, %x)\n", reg, *val));
    395  1.2.2.2  bouyer 
    396  1.2.2.2  bouyer 		return 0;
    397  1.2.2.2  bouyer 	} else {
    398  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_CODECIO,
    399  1.2.2.2  bouyer 		    ("%s: read_codec timeout\n", sc->sc_dev.dv_xname));
    400  1.2.2.2  bouyer 		return -1;
    401  1.2.2.2  bouyer 	}
    402  1.2.2.2  bouyer }
    403  1.2.2.2  bouyer 
    404  1.2.2.2  bouyer int
    405  1.2.2.2  bouyer auich_write_codec(void *v, u_int8_t reg, u_int16_t val)
    406  1.2.2.2  bouyer {
    407  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    408  1.2.2.2  bouyer 	int i;
    409  1.2.2.2  bouyer 
    410  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_CODECIO, ("auich_write_codec(%x, %x)\n", reg, val));
    411  1.2.2.2  bouyer 
    412  1.2.2.2  bouyer 	/* wait for an access semaphore */
    413  1.2.2.2  bouyer 	for (i = ICH_SEMATIMO; i-- &&
    414  1.2.2.2  bouyer 	    bus_space_read_1(sc->iot, sc->aud_ioh, ICH_CAS) & 1; DELAY(1));
    415  1.2.2.2  bouyer 
    416  1.2.2.2  bouyer 	if (i > 0) {
    417  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->mix_ioh, reg, val);
    418  1.2.2.2  bouyer 		return 0;
    419  1.2.2.2  bouyer 	} else {
    420  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_CODECIO,
    421  1.2.2.2  bouyer 		    ("%s: write_codec timeout\n", sc->sc_dev.dv_xname));
    422  1.2.2.2  bouyer 		return -1;
    423  1.2.2.2  bouyer 	}
    424  1.2.2.2  bouyer }
    425  1.2.2.2  bouyer 
    426  1.2.2.2  bouyer int
    427  1.2.2.2  bouyer auich_attach_codec(void *v, struct ac97_codec_if *cif)
    428  1.2.2.2  bouyer {
    429  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    430  1.2.2.2  bouyer 
    431  1.2.2.2  bouyer 	sc->codec_if = cif;
    432  1.2.2.2  bouyer 	return 0;
    433  1.2.2.2  bouyer }
    434  1.2.2.2  bouyer 
    435  1.2.2.2  bouyer void
    436  1.2.2.2  bouyer auich_reset_codec(void *v)
    437  1.2.2.2  bouyer {
    438  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    439  1.2.2.2  bouyer 
    440  1.2.2.2  bouyer 	bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GCTRL, 0);
    441  1.2.2.2  bouyer 	DELAY(10);
    442  1.2.2.2  bouyer 	bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GCTRL, ICH_CRESET);
    443  1.2.2.2  bouyer }
    444  1.2.2.2  bouyer 
    445  1.2.2.2  bouyer int
    446  1.2.2.2  bouyer auich_open(void *v, int flags)
    447  1.2.2.2  bouyer {
    448  1.2.2.2  bouyer 
    449  1.2.2.2  bouyer 	return 0;
    450  1.2.2.2  bouyer }
    451  1.2.2.2  bouyer 
    452  1.2.2.2  bouyer void
    453  1.2.2.2  bouyer auich_close(void *v)
    454  1.2.2.2  bouyer {
    455  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    456  1.2.2.2  bouyer 
    457  1.2.2.2  bouyer 	auich_halt_output(sc);
    458  1.2.2.2  bouyer 	auich_halt_input(sc);
    459  1.2.2.2  bouyer 
    460  1.2.2.2  bouyer 	sc->sc_pintr = NULL;
    461  1.2.2.2  bouyer 	sc->sc_rintr = NULL;
    462  1.2.2.2  bouyer }
    463  1.2.2.2  bouyer 
    464  1.2.2.2  bouyer int
    465  1.2.2.2  bouyer auich_query_encoding(void *v, struct audio_encoding *aep)
    466  1.2.2.2  bouyer {
    467  1.2.2.2  bouyer 	switch (aep->index) {
    468  1.2.2.2  bouyer #if 0 /* XXX Not until we emulate it. */
    469  1.2.2.2  bouyer 	case 0:
    470  1.2.2.2  bouyer 		strcpy(aep->name, AudioEulinear);
    471  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_ULINEAR;
    472  1.2.2.2  bouyer 		aep->precision = 8;
    473  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    474  1.2.2.2  bouyer 		return (0);
    475  1.2.2.2  bouyer #endif
    476  1.2.2.2  bouyer 	case 1:
    477  1.2.2.2  bouyer 		strcpy(aep->name, AudioEmulaw);
    478  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_ULAW;
    479  1.2.2.2  bouyer 		aep->precision = 8;
    480  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    481  1.2.2.2  bouyer 		return (0);
    482  1.2.2.2  bouyer 	case 2:
    483  1.2.2.2  bouyer 		strcpy(aep->name, AudioEalaw);
    484  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_ALAW;
    485  1.2.2.2  bouyer 		aep->precision = 8;
    486  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    487  1.2.2.2  bouyer 		return (0);
    488  1.2.2.2  bouyer 	case 3:
    489  1.2.2.2  bouyer 		strcpy(aep->name, AudioEslinear);
    490  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_SLINEAR;
    491  1.2.2.2  bouyer 		aep->precision = 8;
    492  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    493  1.2.2.2  bouyer 		return (0);
    494  1.2.2.2  bouyer 	case 4:
    495  1.2.2.2  bouyer 		strcpy(aep->name, AudioEslinear_le);
    496  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_SLINEAR_LE;
    497  1.2.2.2  bouyer 		aep->precision = 16;
    498  1.2.2.2  bouyer 		aep->flags = 0;
    499  1.2.2.2  bouyer 		return (0);
    500  1.2.2.2  bouyer 	case 5:
    501  1.2.2.2  bouyer 		strcpy(aep->name, AudioEulinear_le);
    502  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_ULINEAR_LE;
    503  1.2.2.2  bouyer 		aep->precision = 16;
    504  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    505  1.2.2.2  bouyer 		return (0);
    506  1.2.2.2  bouyer 	case 6:
    507  1.2.2.2  bouyer 		strcpy(aep->name, AudioEslinear_be);
    508  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_SLINEAR_BE;
    509  1.2.2.2  bouyer 		aep->precision = 16;
    510  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    511  1.2.2.2  bouyer 		return (0);
    512  1.2.2.2  bouyer 	case 7:
    513  1.2.2.2  bouyer 		strcpy(aep->name, AudioEulinear_be);
    514  1.2.2.2  bouyer 		aep->encoding = AUDIO_ENCODING_ULINEAR_BE;
    515  1.2.2.2  bouyer 		aep->precision = 16;
    516  1.2.2.2  bouyer 		aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
    517  1.2.2.2  bouyer 		return (0);
    518  1.2.2.2  bouyer 	default:
    519  1.2.2.2  bouyer 		return (EINVAL);
    520  1.2.2.2  bouyer 	}
    521  1.2.2.2  bouyer }
    522  1.2.2.2  bouyer 
    523  1.2.2.2  bouyer int
    524  1.2.2.2  bouyer auich_set_params(void *v, int setmode, int usemode, struct audio_params *play,
    525  1.2.2.2  bouyer     struct audio_params *rec)
    526  1.2.2.2  bouyer {
    527  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    528  1.2.2.2  bouyer 	struct audio_params *p;
    529  1.2.2.2  bouyer 	int mode;
    530  1.2.2.2  bouyer 	u_int16_t val, rate, inout;
    531  1.2.2.2  bouyer 
    532  1.2.2.2  bouyer 	for (mode = AUMODE_RECORD; mode != -1;
    533  1.2.2.2  bouyer 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    534  1.2.2.2  bouyer 		if ((setmode & mode) == 0)
    535  1.2.2.2  bouyer 			continue;
    536  1.2.2.2  bouyer 
    537  1.2.2.2  bouyer 		p = mode == AUMODE_PLAY ? play : rec;
    538  1.2.2.2  bouyer 		if (p == NULL)
    539  1.2.2.2  bouyer 			continue;
    540  1.2.2.2  bouyer 
    541  1.2.2.2  bouyer 		inout = mode == AUMODE_PLAY ? ICH_PM_PCMO : ICH_PM_PCMI;
    542  1.2.2.2  bouyer 
    543  1.2.2.2  bouyer 		/*
    544  1.2.2.2  bouyer 		 * XXX NEED TO DETERMINE WHICH RATES THE CODEC SUPPORTS!
    545  1.2.2.2  bouyer 		 */
    546  1.2.2.2  bouyer 		if (p->sample_rate != 48000)
    547  1.2.2.2  bouyer 			return (EINVAL);
    548  1.2.2.2  bouyer 
    549  1.2.2.2  bouyer 		p->factor = 1;
    550  1.2.2.2  bouyer 		p->sw_code = NULL;
    551  1.2.2.2  bouyer 		switch (p->encoding) {
    552  1.2.2.2  bouyer 		case AUDIO_ENCODING_SLINEAR_BE:
    553  1.2.2.2  bouyer 			if (p->precision == 16)
    554  1.2.2.2  bouyer 				p->sw_code = swap_bytes;
    555  1.2.2.2  bouyer 			else {
    556  1.2.2.2  bouyer 				if (mode == AUMODE_PLAY)
    557  1.2.2.2  bouyer 					p->sw_code = linear8_to_linear16_le;
    558  1.2.2.2  bouyer 				else
    559  1.2.2.2  bouyer 					p->sw_code = linear16_to_linear8_le;
    560  1.2.2.2  bouyer 			}
    561  1.2.2.2  bouyer 			break;
    562  1.2.2.2  bouyer 
    563  1.2.2.2  bouyer 		case AUDIO_ENCODING_SLINEAR_LE:
    564  1.2.2.2  bouyer 			if (p->precision != 16) {
    565  1.2.2.2  bouyer 				if (mode == AUMODE_PLAY)
    566  1.2.2.2  bouyer 					p->sw_code = linear8_to_linear16_le;
    567  1.2.2.2  bouyer 				else
    568  1.2.2.2  bouyer 					p->sw_code = linear16_to_linear8_le;
    569  1.2.2.2  bouyer 			}
    570  1.2.2.2  bouyer 			break;
    571  1.2.2.2  bouyer 
    572  1.2.2.2  bouyer 		case AUDIO_ENCODING_ULINEAR_BE:
    573  1.2.2.2  bouyer 			if (p->precision == 16) {
    574  1.2.2.2  bouyer 				if (mode == AUMODE_PLAY)
    575  1.2.2.2  bouyer 					p->sw_code =
    576  1.2.2.2  bouyer 					    swap_bytes_change_sign16_le;
    577  1.2.2.2  bouyer 				else
    578  1.2.2.2  bouyer 					p->sw_code =
    579  1.2.2.2  bouyer 					    change_sign16_swap_bytes_le;
    580  1.2.2.2  bouyer 			} else {
    581  1.2.2.2  bouyer 				/*
    582  1.2.2.2  bouyer 				 * XXX ulinear8_to_slinear16_le
    583  1.2.2.2  bouyer 				 */
    584  1.2.2.2  bouyer 				return (EINVAL);
    585  1.2.2.2  bouyer 			}
    586  1.2.2.2  bouyer 			break;
    587  1.2.2.2  bouyer 
    588  1.2.2.2  bouyer 		case AUDIO_ENCODING_ULINEAR_LE:
    589  1.2.2.2  bouyer 			if (p->precision == 16)
    590  1.2.2.2  bouyer 				p->sw_code = change_sign16_le;
    591  1.2.2.2  bouyer 			else {
    592  1.2.2.2  bouyer 				/*
    593  1.2.2.2  bouyer 				 * XXX ulinear8_to_slinear16_le
    594  1.2.2.2  bouyer 				 */
    595  1.2.2.2  bouyer 				return (EINVAL);
    596  1.2.2.2  bouyer 			}
    597  1.2.2.2  bouyer 			break;
    598  1.2.2.2  bouyer 
    599  1.2.2.2  bouyer 		case AUDIO_ENCODING_ULAW:
    600  1.2.2.2  bouyer 			if (mode == AUMODE_PLAY) {
    601  1.2.2.2  bouyer 				p->factor = 2;
    602  1.2.2.2  bouyer 				p->sw_code = mulaw_to_slinear16_le;
    603  1.2.2.2  bouyer 			} else {
    604  1.2.2.2  bouyer 				/*
    605  1.2.2.2  bouyer 				 * XXX slinear16_le_to_mulaw
    606  1.2.2.2  bouyer 				 */
    607  1.2.2.2  bouyer 				return (EINVAL);
    608  1.2.2.2  bouyer 			}
    609  1.2.2.2  bouyer 			break;
    610  1.2.2.2  bouyer 
    611  1.2.2.2  bouyer 		case AUDIO_ENCODING_ALAW:
    612  1.2.2.2  bouyer 			if (mode == AUMODE_PLAY) {
    613  1.2.2.2  bouyer 				p->factor = 2;
    614  1.2.2.2  bouyer 				p->sw_code = alaw_to_slinear16_le;
    615  1.2.2.2  bouyer 			} else {
    616  1.2.2.2  bouyer 				/*
    617  1.2.2.2  bouyer 				 * XXX slinear16_le_to_alaw
    618  1.2.2.2  bouyer 				 */
    619  1.2.2.2  bouyer 				return (EINVAL);
    620  1.2.2.2  bouyer 			}
    621  1.2.2.2  bouyer 			break;
    622  1.2.2.2  bouyer 
    623  1.2.2.2  bouyer 		default:
    624  1.2.2.2  bouyer 			return (EINVAL);
    625  1.2.2.2  bouyer 		}
    626  1.2.2.2  bouyer 
    627  1.2.2.2  bouyer 		auich_read_codec(sc, AC97_REG_POWER, &val);
    628  1.2.2.2  bouyer 		auich_write_codec(sc, AC97_REG_POWER, val | inout);
    629  1.2.2.2  bouyer 
    630  1.2.2.2  bouyer 		auich_write_codec(sc, AC97_REG_PCM_FRONT_DAC_RATE,
    631  1.2.2.2  bouyer 		    p->sample_rate);
    632  1.2.2.2  bouyer 		auich_read_codec(sc, AC97_REG_PCM_FRONT_DAC_RATE, &rate);
    633  1.2.2.2  bouyer 		p->sample_rate = rate;
    634  1.2.2.2  bouyer 
    635  1.2.2.2  bouyer 		auich_write_codec(sc, AC97_REG_POWER, val);
    636  1.2.2.2  bouyer 	}
    637  1.2.2.2  bouyer 
    638  1.2.2.2  bouyer 	return (0);
    639  1.2.2.2  bouyer }
    640  1.2.2.2  bouyer 
    641  1.2.2.2  bouyer int
    642  1.2.2.2  bouyer auich_round_blocksize(void *v, int blk)
    643  1.2.2.2  bouyer {
    644  1.2.2.2  bouyer 
    645  1.2.2.2  bouyer 	return (blk & ~0x3f);		/* keep good alignment */
    646  1.2.2.2  bouyer }
    647  1.2.2.2  bouyer 
    648  1.2.2.2  bouyer int
    649  1.2.2.2  bouyer auich_halt_output(void *v)
    650  1.2.2.2  bouyer {
    651  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    652  1.2.2.2  bouyer 
    653  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA, ("%s: halt_output\n", sc->sc_dev.dv_xname));
    654  1.2.2.2  bouyer 
    655  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CTRL, ICH_RR);
    656  1.2.2.2  bouyer 
    657  1.2.2.2  bouyer 	return (0);
    658  1.2.2.2  bouyer }
    659  1.2.2.2  bouyer 
    660  1.2.2.2  bouyer int
    661  1.2.2.2  bouyer auich_halt_input(void *v)
    662  1.2.2.2  bouyer {
    663  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    664  1.2.2.2  bouyer 
    665  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA,
    666  1.2.2.2  bouyer 	    ("%s: halt_input\n", sc->sc_dev.dv_xname));
    667  1.2.2.2  bouyer 
    668  1.2.2.2  bouyer 	/* XXX halt both unless known otherwise */
    669  1.2.2.2  bouyer 
    670  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL, ICH_RR);
    671  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_MICI + ICH_CTRL, ICH_RR);
    672  1.2.2.2  bouyer 
    673  1.2.2.2  bouyer 	return (0);
    674  1.2.2.2  bouyer }
    675  1.2.2.2  bouyer 
    676  1.2.2.2  bouyer int
    677  1.2.2.2  bouyer auich_getdev(void *v, struct audio_device *adp)
    678  1.2.2.2  bouyer {
    679  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    680  1.2.2.2  bouyer 
    681  1.2.2.2  bouyer 	*adp = sc->sc_audev;
    682  1.2.2.2  bouyer 	return (0);
    683  1.2.2.2  bouyer }
    684  1.2.2.2  bouyer 
    685  1.2.2.2  bouyer int
    686  1.2.2.2  bouyer auich_set_port(void *v, mixer_ctrl_t *cp)
    687  1.2.2.2  bouyer {
    688  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    689  1.2.2.2  bouyer 
    690  1.2.2.2  bouyer 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
    691  1.2.2.2  bouyer }
    692  1.2.2.2  bouyer 
    693  1.2.2.2  bouyer int
    694  1.2.2.2  bouyer auich_get_port(void *v, mixer_ctrl_t *cp)
    695  1.2.2.2  bouyer {
    696  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    697  1.2.2.2  bouyer 
    698  1.2.2.2  bouyer 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    699  1.2.2.2  bouyer }
    700  1.2.2.2  bouyer 
    701  1.2.2.2  bouyer int
    702  1.2.2.2  bouyer auich_query_devinfo(void *v, mixer_devinfo_t *dp)
    703  1.2.2.2  bouyer {
    704  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    705  1.2.2.2  bouyer 
    706  1.2.2.2  bouyer 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dp));
    707  1.2.2.2  bouyer }
    708  1.2.2.2  bouyer 
    709  1.2.2.2  bouyer void *
    710  1.2.2.2  bouyer auich_allocm(void *v, int direction, size_t size, int pool, int flags)
    711  1.2.2.2  bouyer {
    712  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    713  1.2.2.2  bouyer 	struct auich_dma *p;
    714  1.2.2.2  bouyer 	int error;
    715  1.2.2.2  bouyer 
    716  1.2.2.2  bouyer 	if (size > (ICH_DMALIST_MAX * ICH_DMASEG_MAX))
    717  1.2.2.2  bouyer 		return (NULL);
    718  1.2.2.2  bouyer 
    719  1.2.2.2  bouyer 	p = malloc(sizeof(*p), pool, flags);
    720  1.2.2.2  bouyer 	if (p == NULL)
    721  1.2.2.2  bouyer 		return (NULL);
    722  1.2.2.2  bouyer 	memset(p, 0, sizeof(*p));
    723  1.2.2.2  bouyer 
    724  1.2.2.2  bouyer 	error = auich_allocmem(sc, size, 0, p);
    725  1.2.2.2  bouyer 	if (error) {
    726  1.2.2.2  bouyer 		free(p, pool);
    727  1.2.2.2  bouyer 		return (NULL);
    728  1.2.2.2  bouyer 	}
    729  1.2.2.2  bouyer 
    730  1.2.2.2  bouyer 	p->next = sc->sc_dmas;
    731  1.2.2.2  bouyer 	sc->sc_dmas = p;
    732  1.2.2.2  bouyer 
    733  1.2.2.2  bouyer 	return (KERNADDR(p));
    734  1.2.2.2  bouyer }
    735  1.2.2.2  bouyer 
    736  1.2.2.2  bouyer void
    737  1.2.2.2  bouyer auich_freem(void *v, void *ptr, int pool)
    738  1.2.2.2  bouyer {
    739  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    740  1.2.2.2  bouyer 	struct auich_dma *p, **pp;
    741  1.2.2.2  bouyer 
    742  1.2.2.2  bouyer 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
    743  1.2.2.2  bouyer 		if (KERNADDR(p) == ptr) {
    744  1.2.2.2  bouyer 			auich_freemem(sc, p);
    745  1.2.2.2  bouyer 			*pp = p->next;
    746  1.2.2.2  bouyer 			free(p, pool);
    747  1.2.2.2  bouyer 			return;
    748  1.2.2.2  bouyer 		}
    749  1.2.2.2  bouyer 	}
    750  1.2.2.2  bouyer }
    751  1.2.2.2  bouyer 
    752  1.2.2.2  bouyer size_t
    753  1.2.2.2  bouyer auich_round_buffersize(void *v, int direction, size_t size)
    754  1.2.2.2  bouyer {
    755  1.2.2.2  bouyer 
    756  1.2.2.2  bouyer 	if (size > (ICH_DMALIST_MAX * ICH_DMASEG_MAX))
    757  1.2.2.2  bouyer 		size = ICH_DMALIST_MAX * ICH_DMASEG_MAX;
    758  1.2.2.2  bouyer 
    759  1.2.2.2  bouyer 	return size;
    760  1.2.2.2  bouyer }
    761  1.2.2.2  bouyer 
    762  1.2.2.2  bouyer paddr_t
    763  1.2.2.2  bouyer auich_mappage(void *v, void *mem, off_t off, int prot)
    764  1.2.2.2  bouyer {
    765  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    766  1.2.2.2  bouyer 	struct auich_dma *p;
    767  1.2.2.2  bouyer 
    768  1.2.2.2  bouyer 	if (off < 0)
    769  1.2.2.2  bouyer 		return (-1);
    770  1.2.2.2  bouyer 
    771  1.2.2.2  bouyer 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
    772  1.2.2.2  bouyer 		;
    773  1.2.2.2  bouyer 	if (!p)
    774  1.2.2.2  bouyer 		return (-1);
    775  1.2.2.2  bouyer 	return (bus_dmamem_mmap(sc->dmat, p->segs, p->nsegs,
    776  1.2.2.2  bouyer 	    off, prot, BUS_DMA_WAITOK));
    777  1.2.2.2  bouyer }
    778  1.2.2.2  bouyer 
    779  1.2.2.2  bouyer int
    780  1.2.2.2  bouyer auich_get_props(void *v)
    781  1.2.2.2  bouyer {
    782  1.2.2.2  bouyer 
    783  1.2.2.2  bouyer 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
    784  1.2.2.2  bouyer 		AUDIO_PROP_FULLDUPLEX);
    785  1.2.2.2  bouyer }
    786  1.2.2.2  bouyer 
    787  1.2.2.2  bouyer int
    788  1.2.2.2  bouyer auich_intr(void *v)
    789  1.2.2.2  bouyer {
    790  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    791  1.2.2.2  bouyer 	int ret = 0, sts, gsts, i, qptr;
    792  1.2.2.2  bouyer 
    793  1.2.2.2  bouyer 	gsts = bus_space_read_2(sc->iot, sc->aud_ioh, ICH_GSTS);
    794  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA, ("auich_intr: gsts=0x%x\n", gsts));
    795  1.2.2.2  bouyer 
    796  1.2.2.2  bouyer 	if (gsts & ICH_POINT) {
    797  1.2.2.2  bouyer 		sts = bus_space_read_2(sc->iot, sc->aud_ioh, ICH_PCMO+ICH_STS);
    798  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_DMA,
    799  1.2.2.2  bouyer 		    ("auich_intr: osts=0x%x\n", sts));
    800  1.2.2.2  bouyer 
    801  1.2.2.2  bouyer 		if (sts & ICH_FIFOE) {
    802  1.2.2.2  bouyer 			printf("%s: fifo underrun # %u\n",
    803  1.2.2.2  bouyer 			    sc->sc_dev.dv_xname, ++sc->pcmo_fifoe);
    804  1.2.2.2  bouyer 		}
    805  1.2.2.2  bouyer 
    806  1.2.2.2  bouyer 		i = bus_space_read_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CIV);
    807  1.2.2.2  bouyer 		if (sts & (ICH_LVBCI | ICH_CELV)) {
    808  1.2.2.2  bouyer 			struct auich_dmalist *q;
    809  1.2.2.2  bouyer 
    810  1.2.2.2  bouyer 			qptr = sc->ptr_pcmo;
    811  1.2.2.2  bouyer 
    812  1.2.2.2  bouyer 			while (qptr != i) {
    813  1.2.2.2  bouyer 				q = &sc->dmalist_pcmo[qptr];
    814  1.2.2.2  bouyer 
    815  1.2.2.2  bouyer 				q->base = sc->pcmo_p;
    816  1.2.2.2  bouyer 				q->len = (sc->pcmo_blksize / 2) | ICH_DMAF_IOC;
    817  1.2.2.2  bouyer 				DPRINTF(ICH_DEBUG_DMA,
    818  1.2.2.2  bouyer 				    ("auich_intr: %p, %p = %x @ 0x%x\n",
    819  1.2.2.2  bouyer 				    &sc->dmalist_pcmo[i], q,
    820  1.2.2.2  bouyer 				    sc->pcmo_blksize / 2, sc->pcmo_p));
    821  1.2.2.2  bouyer 
    822  1.2.2.2  bouyer 				sc->pcmo_p += sc->pcmo_blksize;
    823  1.2.2.2  bouyer 				if (sc->pcmo_p >= sc->pcmo_end)
    824  1.2.2.2  bouyer 					sc->pcmo_p = sc->pcmo_start;
    825  1.2.2.2  bouyer 
    826  1.2.2.2  bouyer 				if (++qptr == ICH_DMALIST_MAX)
    827  1.2.2.2  bouyer 					qptr = 0;
    828  1.2.2.2  bouyer 			}
    829  1.2.2.2  bouyer 
    830  1.2.2.2  bouyer 			sc->ptr_pcmo = qptr;
    831  1.2.2.2  bouyer 			bus_space_write_1(sc->iot, sc->aud_ioh,
    832  1.2.2.2  bouyer 			    ICH_PCMO + ICH_LVI,
    833  1.2.2.2  bouyer 			    (sc->ptr_pcmo - 1) & ICH_LVI_MASK);
    834  1.2.2.2  bouyer 		}
    835  1.2.2.2  bouyer 
    836  1.2.2.2  bouyer 		if (sts & ICH_BCIS && sc->sc_pintr)
    837  1.2.2.2  bouyer 			sc->sc_pintr(sc->sc_parg);
    838  1.2.2.2  bouyer 
    839  1.2.2.2  bouyer 		/* int ack */
    840  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_STS,
    841  1.2.2.2  bouyer 		    sts & (ICH_LVBCI | ICH_CELV | ICH_BCIS | ICH_FIFOE));
    842  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_POINT);
    843  1.2.2.2  bouyer 		ret++;
    844  1.2.2.2  bouyer 	}
    845  1.2.2.2  bouyer 
    846  1.2.2.2  bouyer 	if (gsts & ICH_PIINT) {
    847  1.2.2.2  bouyer 		sts = bus_space_read_2(sc->iot, sc->aud_ioh, ICH_PCMI+ICH_STS);
    848  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_DMA,
    849  1.2.2.2  bouyer 		    ("auich_intr: ists=0x%x\n", sts));
    850  1.2.2.2  bouyer 
    851  1.2.2.2  bouyer 		if (sts & ICH_FIFOE) {
    852  1.2.2.2  bouyer 			printf("%s: fifo overrun # %u\n",
    853  1.2.2.2  bouyer 			    sc->sc_dev.dv_xname, ++sc->pcmi_fifoe);
    854  1.2.2.2  bouyer 		}
    855  1.2.2.2  bouyer 
    856  1.2.2.2  bouyer 		i = bus_space_read_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CIV);
    857  1.2.2.2  bouyer 		if (sts & (ICH_LVBCI | ICH_CELV)) {
    858  1.2.2.2  bouyer 			struct auich_dmalist *q;
    859  1.2.2.2  bouyer 
    860  1.2.2.2  bouyer 			qptr = sc->ptr_pcmi;
    861  1.2.2.2  bouyer 
    862  1.2.2.2  bouyer 			while (qptr != i) {
    863  1.2.2.2  bouyer 				q = &sc->dmalist_pcmi[qptr];
    864  1.2.2.2  bouyer 
    865  1.2.2.2  bouyer 				q->base = sc->pcmi_p;
    866  1.2.2.2  bouyer 				q->len = (sc->pcmi_blksize / 2) | ICH_DMAF_IOC;
    867  1.2.2.2  bouyer 				DPRINTF(ICH_DEBUG_DMA,
    868  1.2.2.2  bouyer 				    ("auich_intr: %p, %p = %x @ 0x%x\n",
    869  1.2.2.2  bouyer 				    &sc->dmalist_pcmi[i], q,
    870  1.2.2.2  bouyer 				    sc->pcmi_blksize / 2, sc->pcmi_p));
    871  1.2.2.2  bouyer 
    872  1.2.2.2  bouyer 				sc->pcmi_p += sc->pcmi_blksize;
    873  1.2.2.2  bouyer 				if (sc->pcmi_p >= sc->pcmi_end)
    874  1.2.2.2  bouyer 					sc->pcmi_p = sc->pcmi_start;
    875  1.2.2.2  bouyer 
    876  1.2.2.2  bouyer 				if (++qptr == ICH_DMALIST_MAX)
    877  1.2.2.2  bouyer 					qptr = 0;
    878  1.2.2.2  bouyer 			}
    879  1.2.2.2  bouyer 
    880  1.2.2.2  bouyer 			sc->ptr_pcmi = qptr;
    881  1.2.2.2  bouyer 			bus_space_write_1(sc->iot, sc->aud_ioh,
    882  1.2.2.2  bouyer 			    ICH_PCMI + ICH_LVI,
    883  1.2.2.2  bouyer 			    (sc->ptr_pcmi - 1) & ICH_LVI_MASK);
    884  1.2.2.2  bouyer 		}
    885  1.2.2.2  bouyer 
    886  1.2.2.2  bouyer 		if (sts & ICH_BCIS && sc->sc_rintr)
    887  1.2.2.2  bouyer 			sc->sc_rintr(sc->sc_rarg);
    888  1.2.2.2  bouyer 
    889  1.2.2.2  bouyer 		/* int ack */
    890  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_STS,
    891  1.2.2.2  bouyer 		    sts & (ICH_LVBCI | ICH_CELV | ICH_BCIS | ICH_FIFOE));
    892  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_POINT);
    893  1.2.2.2  bouyer 		ret++;
    894  1.2.2.2  bouyer 	}
    895  1.2.2.2  bouyer 
    896  1.2.2.2  bouyer 	if (gsts & ICH_MIINT) {
    897  1.2.2.2  bouyer 		sts = bus_space_read_2(sc->iot, sc->aud_ioh, ICH_MICI+ICH_STS);
    898  1.2.2.2  bouyer 		DPRINTF(ICH_DEBUG_DMA,
    899  1.2.2.2  bouyer 		    ("auich_intr: ists=0x%x\n", sts));
    900  1.2.2.2  bouyer 		if (sts & ICH_FIFOE)
    901  1.2.2.2  bouyer 			printf("%s: fifo overrun\n", sc->sc_dev.dv_xname);
    902  1.2.2.2  bouyer 
    903  1.2.2.2  bouyer 		/* TODO mic input dma */
    904  1.2.2.2  bouyer 
    905  1.2.2.2  bouyer 		bus_space_write_2(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_MIINT);
    906  1.2.2.2  bouyer 	}
    907  1.2.2.2  bouyer 
    908  1.2.2.2  bouyer 	return ret;
    909  1.2.2.2  bouyer }
    910  1.2.2.2  bouyer 
    911  1.2.2.2  bouyer int
    912  1.2.2.2  bouyer auich_trigger_output(void *v, void *start, void *end, int blksize,
    913  1.2.2.2  bouyer     void (*intr)(void *), void *arg, struct audio_params *param)
    914  1.2.2.2  bouyer {
    915  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    916  1.2.2.2  bouyer 	struct auich_dmalist *q;
    917  1.2.2.2  bouyer 	struct auich_dma *p;
    918  1.2.2.2  bouyer 	size_t size;
    919  1.2.2.2  bouyer 
    920  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA,
    921  1.2.2.2  bouyer 	    ("auich_trigger_output(%p, %p, %d, %p, %p, %p)\n",
    922  1.2.2.2  bouyer 	    start, end, blksize, intr, arg, param));
    923  1.2.2.2  bouyer 
    924  1.2.2.2  bouyer 	sc->sc_pintr = intr;
    925  1.2.2.2  bouyer 	sc->sc_parg = arg;
    926  1.2.2.2  bouyer 
    927  1.2.2.2  bouyer 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    928  1.2.2.2  bouyer 		;
    929  1.2.2.2  bouyer 	if (!p) {
    930  1.2.2.2  bouyer 		printf("auich_trigger_output: bad addr %p\n", start);
    931  1.2.2.2  bouyer 		return (EINVAL);
    932  1.2.2.2  bouyer 	}
    933  1.2.2.2  bouyer 
    934  1.2.2.2  bouyer 	size = (size_t)((caddr_t)end - (caddr_t)start);
    935  1.2.2.2  bouyer 
    936  1.2.2.2  bouyer 	/*
    937  1.2.2.2  bouyer 	 * The logic behind this is:
    938  1.2.2.2  bouyer 	 * setup one buffer to play, then LVI dump out the rest
    939  1.2.2.2  bouyer 	 * to the scatter-gather chain.
    940  1.2.2.2  bouyer 	 */
    941  1.2.2.2  bouyer 	sc->pcmo_start = DMAADDR(p);
    942  1.2.2.2  bouyer 	sc->pcmo_p = sc->pcmo_start + blksize;
    943  1.2.2.2  bouyer 	sc->pcmo_end = sc->pcmo_start + size;
    944  1.2.2.2  bouyer 	sc->pcmo_blksize = blksize;
    945  1.2.2.2  bouyer 
    946  1.2.2.2  bouyer 	sc->ptr_pcmo = 0;
    947  1.2.2.2  bouyer 	q = &sc->dmalist_pcmo[sc->ptr_pcmo];
    948  1.2.2.2  bouyer 	q->base = sc->pcmo_start;
    949  1.2.2.2  bouyer 	q->len = (blksize / 2) | ICH_DMAF_IOC;
    950  1.2.2.2  bouyer 	if (++sc->ptr_pcmo == ICH_DMALIST_MAX)
    951  1.2.2.2  bouyer 		sc->ptr_pcmo = 0;
    952  1.2.2.2  bouyer 
    953  1.2.2.2  bouyer 	bus_space_write_4(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_BDBAR,
    954  1.2.2.2  bouyer 	    sc->sc_cddma + ICH_PCMO_OFF(0));
    955  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CTRL,
    956  1.2.2.2  bouyer 	    ICH_IOCE | ICH_FEIE | ICH_LVBIE | ICH_RPBM);
    957  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_LVI,
    958  1.2.2.2  bouyer 	    (sc->ptr_pcmo - 1) & ICH_LVI_MASK);
    959  1.2.2.2  bouyer 
    960  1.2.2.2  bouyer 	return (0);
    961  1.2.2.2  bouyer }
    962  1.2.2.2  bouyer 
    963  1.2.2.2  bouyer int
    964  1.2.2.2  bouyer auich_trigger_input(v, start, end, blksize, intr, arg, param)
    965  1.2.2.2  bouyer 	void *v;
    966  1.2.2.2  bouyer 	void *start, *end;
    967  1.2.2.2  bouyer 	int blksize;
    968  1.2.2.2  bouyer 	void (*intr)(void *);
    969  1.2.2.2  bouyer 	void *arg;
    970  1.2.2.2  bouyer 	struct audio_params *param;
    971  1.2.2.2  bouyer {
    972  1.2.2.2  bouyer 	struct auich_softc *sc = v;
    973  1.2.2.2  bouyer 	struct auich_dmalist *q;
    974  1.2.2.2  bouyer 	struct auich_dma *p;
    975  1.2.2.2  bouyer 	size_t size;
    976  1.2.2.2  bouyer 
    977  1.2.2.2  bouyer 	DPRINTF(ICH_DEBUG_DMA,
    978  1.2.2.2  bouyer 	    ("auich_trigger_input(%p, %p, %d, %p, %p, %p)\n",
    979  1.2.2.2  bouyer 	    start, end, blksize, intr, arg, param));
    980  1.2.2.2  bouyer 
    981  1.2.2.2  bouyer 	sc->sc_rintr = intr;
    982  1.2.2.2  bouyer 	sc->sc_rarg = arg;
    983  1.2.2.2  bouyer 
    984  1.2.2.2  bouyer 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    985  1.2.2.2  bouyer 		;
    986  1.2.2.2  bouyer 	if (!p) {
    987  1.2.2.2  bouyer 		printf("auich_trigger_input: bad addr %p\n", start);
    988  1.2.2.2  bouyer 		return (EINVAL);
    989  1.2.2.2  bouyer 	}
    990  1.2.2.2  bouyer 
    991  1.2.2.2  bouyer 	size = (size_t)((caddr_t)end - (caddr_t)start);
    992  1.2.2.2  bouyer 
    993  1.2.2.2  bouyer 	/*
    994  1.2.2.2  bouyer 	 * The logic behind this is:
    995  1.2.2.2  bouyer 	 * setup one buffer to play, then LVI dump out the rest
    996  1.2.2.2  bouyer 	 * to the scatter-gather chain.
    997  1.2.2.2  bouyer 	 */
    998  1.2.2.2  bouyer 	sc->pcmi_start = DMAADDR(p);
    999  1.2.2.2  bouyer 	sc->pcmi_p = sc->pcmi_start + blksize;
   1000  1.2.2.2  bouyer 	sc->pcmi_end = sc->pcmi_start + size;
   1001  1.2.2.2  bouyer 	sc->pcmi_blksize = blksize;
   1002  1.2.2.2  bouyer 
   1003  1.2.2.2  bouyer 	sc->ptr_pcmi = 0;
   1004  1.2.2.2  bouyer 	q = &sc->dmalist_pcmi[sc->ptr_pcmi];
   1005  1.2.2.2  bouyer 	q->base = sc->pcmi_start;
   1006  1.2.2.2  bouyer 	q->len = (blksize / 2) | ICH_DMAF_IOC;
   1007  1.2.2.2  bouyer 	if (++sc->ptr_pcmi == ICH_DMALIST_MAX)
   1008  1.2.2.2  bouyer 		sc->ptr_pcmi = 0;
   1009  1.2.2.2  bouyer 
   1010  1.2.2.2  bouyer 	bus_space_write_4(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_BDBAR,
   1011  1.2.2.2  bouyer 	    sc->sc_cddma + ICH_PCMI_OFF(0));
   1012  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL,
   1013  1.2.2.2  bouyer 	    ICH_IOCE | ICH_FEIE | ICH_LVBIE | ICH_RPBM);
   1014  1.2.2.2  bouyer 	bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_LVI,
   1015  1.2.2.2  bouyer 	    (sc->ptr_pcmi - 1) & ICH_LVI_MASK);
   1016  1.2.2.2  bouyer 
   1017  1.2.2.2  bouyer 	return (0);
   1018  1.2.2.2  bouyer }
   1019  1.2.2.2  bouyer 
   1020  1.2.2.2  bouyer int
   1021  1.2.2.2  bouyer auich_allocmem(struct auich_softc *sc, size_t size, size_t align,
   1022  1.2.2.2  bouyer     struct auich_dma *p)
   1023  1.2.2.2  bouyer {
   1024  1.2.2.2  bouyer 	int error;
   1025  1.2.2.2  bouyer 
   1026  1.2.2.2  bouyer 	p->size = size;
   1027  1.2.2.2  bouyer 	error = bus_dmamem_alloc(sc->dmat, p->size, align, 0,
   1028  1.2.2.2  bouyer 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
   1029  1.2.2.2  bouyer 				 &p->nsegs, BUS_DMA_NOWAIT);
   1030  1.2.2.2  bouyer 	if (error)
   1031  1.2.2.2  bouyer 		return (error);
   1032  1.2.2.2  bouyer 
   1033  1.2.2.2  bouyer 	error = bus_dmamem_map(sc->dmat, p->segs, p->nsegs, p->size,
   1034  1.2.2.2  bouyer 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1035  1.2.2.2  bouyer 	if (error)
   1036  1.2.2.2  bouyer 		goto free;
   1037  1.2.2.2  bouyer 
   1038  1.2.2.2  bouyer 	error = bus_dmamap_create(sc->dmat, p->size, 1, p->size,
   1039  1.2.2.2  bouyer 				  0, BUS_DMA_NOWAIT, &p->map);
   1040  1.2.2.2  bouyer 	if (error)
   1041  1.2.2.2  bouyer 		goto unmap;
   1042  1.2.2.2  bouyer 
   1043  1.2.2.2  bouyer 	error = bus_dmamap_load(sc->dmat, p->map, p->addr, p->size, NULL,
   1044  1.2.2.2  bouyer 				BUS_DMA_NOWAIT);
   1045  1.2.2.2  bouyer 	if (error)
   1046  1.2.2.2  bouyer 		goto destroy;
   1047  1.2.2.2  bouyer 	return (0);
   1048  1.2.2.2  bouyer 
   1049  1.2.2.2  bouyer  destroy:
   1050  1.2.2.2  bouyer 	bus_dmamap_destroy(sc->dmat, p->map);
   1051  1.2.2.2  bouyer  unmap:
   1052  1.2.2.2  bouyer 	bus_dmamem_unmap(sc->dmat, p->addr, p->size);
   1053  1.2.2.2  bouyer  free:
   1054  1.2.2.2  bouyer 	bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
   1055  1.2.2.2  bouyer 	return (error);
   1056  1.2.2.2  bouyer }
   1057  1.2.2.2  bouyer 
   1058  1.2.2.2  bouyer int
   1059  1.2.2.2  bouyer auich_freemem(struct auich_softc *sc, struct auich_dma *p)
   1060  1.2.2.2  bouyer {
   1061  1.2.2.2  bouyer 
   1062  1.2.2.2  bouyer 	bus_dmamap_unload(sc->dmat, p->map);
   1063  1.2.2.2  bouyer 	bus_dmamap_destroy(sc->dmat, p->map);
   1064  1.2.2.2  bouyer 	bus_dmamem_unmap(sc->dmat, p->addr, p->size);
   1065  1.2.2.2  bouyer 	bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
   1066  1.2.2.2  bouyer 	return (0);
   1067  1.2.2.2  bouyer }
   1068  1.2.2.2  bouyer 
   1069  1.2.2.2  bouyer int
   1070  1.2.2.2  bouyer auich_alloc_cdata(struct auich_softc *sc)
   1071  1.2.2.2  bouyer {
   1072  1.2.2.2  bouyer 	bus_dma_segment_t seg;
   1073  1.2.2.2  bouyer 	int error, rseg;
   1074  1.2.2.2  bouyer 
   1075  1.2.2.2  bouyer 	/*
   1076  1.2.2.2  bouyer 	 * Allocate the control data structure, and create and load the
   1077  1.2.2.2  bouyer 	 * DMA map for it.
   1078  1.2.2.2  bouyer 	 */
   1079  1.2.2.2  bouyer 	if ((error = bus_dmamem_alloc(sc->dmat,
   1080  1.2.2.2  bouyer 				      sizeof(struct auich_cdata),
   1081  1.2.2.2  bouyer 				      PAGE_SIZE, 0, &seg, 1, &rseg, 0)) != 0) {
   1082  1.2.2.2  bouyer 		printf("%s: unable to allocate control data, error = %d\n",
   1083  1.2.2.2  bouyer 		    sc->sc_dev.dv_xname, error);
   1084  1.2.2.2  bouyer 		goto fail_0;
   1085  1.2.2.2  bouyer 	}
   1086  1.2.2.2  bouyer 
   1087  1.2.2.2  bouyer 	if ((error = bus_dmamem_map(sc->dmat, &seg, rseg,
   1088  1.2.2.2  bouyer 				    sizeof(struct auich_cdata),
   1089  1.2.2.2  bouyer 				    (caddr_t *) &sc->sc_cdata,
   1090  1.2.2.2  bouyer 				    BUS_DMA_COHERENT)) != 0) {
   1091  1.2.2.2  bouyer 		printf("%s: unable to map control data, error = %d\n",
   1092  1.2.2.2  bouyer 		    sc->sc_dev.dv_xname, error);
   1093  1.2.2.2  bouyer 		goto fail_1;
   1094  1.2.2.2  bouyer 	}
   1095  1.2.2.2  bouyer 
   1096  1.2.2.2  bouyer 	if ((error = bus_dmamap_create(sc->dmat, sizeof(struct auich_cdata), 1,
   1097  1.2.2.2  bouyer 				       sizeof(struct auich_cdata), 0, 0,
   1098  1.2.2.2  bouyer 				       &sc->sc_cddmamap)) != 0) {
   1099  1.2.2.2  bouyer 		printf("%s: unable to create control data DMA map, "
   1100  1.2.2.2  bouyer 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1101  1.2.2.2  bouyer 		goto fail_2;
   1102  1.2.2.2  bouyer 	}
   1103  1.2.2.2  bouyer 
   1104  1.2.2.2  bouyer 	if ((error = bus_dmamap_load(sc->dmat, sc->sc_cddmamap,
   1105  1.2.2.2  bouyer 				     sc->sc_cdata, sizeof(struct auich_cdata),
   1106  1.2.2.2  bouyer 				     NULL, 0)) != 0) {
   1107  1.2.2.2  bouyer 		printf("%s: unable tp load control data DMA map, "
   1108  1.2.2.2  bouyer 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1109  1.2.2.2  bouyer 		goto fail_3;
   1110  1.2.2.2  bouyer 	}
   1111  1.2.2.2  bouyer 
   1112  1.2.2.2  bouyer 	return (0);
   1113  1.2.2.2  bouyer 
   1114  1.2.2.2  bouyer  fail_3:
   1115  1.2.2.2  bouyer 	bus_dmamap_destroy(sc->dmat, sc->sc_cddmamap);
   1116  1.2.2.2  bouyer  fail_2:
   1117  1.2.2.2  bouyer 	bus_dmamem_unmap(sc->dmat, (caddr_t) sc->sc_cdata,
   1118  1.2.2.2  bouyer 	    sizeof(struct auich_cdata));
   1119  1.2.2.2  bouyer  fail_1:
   1120  1.2.2.2  bouyer 	bus_dmamem_free(sc->dmat, &seg, rseg);
   1121  1.2.2.2  bouyer  fail_0:
   1122  1.2.2.2  bouyer 	return (error);
   1123  1.2.2.2  bouyer }
   1124