Home | History | Annotate | Line # | Download | only in pci
autri.c revision 1.56
      1 /*	$NetBSD: autri.c,v 1.56 2019/03/16 12:09:58 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 SOMEYA Yoshihiko and KUROSAWA Takahiro.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Trident 4DWAVE-DX/NX, SiS 7018, ALi M5451 Sound Driver
     30  *
     31  * The register information is taken from the ALSA driver.
     32  *
     33  * Documentation links:
     34  * - ftp://ftp.alsa-project.org/pub/manuals/trident/
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: autri.c,v 1.56 2019/03/16 12:09:58 isaki Exp $");
     39 
     40 #include "midi.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/kmem.h>
     47 #include <sys/device.h>
     48 #include <sys/proc.h>
     49 #include <sys/audioio.h>
     50 #include <sys/bus.h>
     51 #include <sys/intr.h>
     52 
     53 #include <dev/audio_if.h>
     54 #include <dev/midi_if.h>
     55 #include <dev/mulaw.h>
     56 #include <dev/auconv.h>
     57 
     58 #include <dev/ic/ac97reg.h>
     59 #include <dev/ic/ac97var.h>
     60 #include <dev/ic/mpuvar.h>
     61 
     62 #include <dev/pci/pcidevs.h>
     63 #include <dev/pci/pcireg.h>
     64 #include <dev/pci/pcivar.h>
     65 #include <dev/pci/autrireg.h>
     66 #include <dev/pci/autrivar.h>
     67 
     68 #ifdef AUDIO_DEBUG
     69 # define DPRINTF(x)	if (autridebug) printf x
     70 # define DPRINTFN(n,x)	if (autridebug > (n)) printf x
     71 int autridebug = 0;
     72 #else
     73 # define DPRINTF(x)
     74 # define DPRINTFN(n,x)
     75 #endif
     76 
     77 static int	autri_intr(void *);
     78 
     79 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
     80 #define KERNADDR(p) ((void *)((p)->addr))
     81 
     82 static int     autri_allocmem(struct autri_softc *, size_t,
     83 			      size_t, struct autri_dma *);
     84 static int     autri_freemem(struct autri_softc *, struct autri_dma *);
     85 
     86 #define TWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
     87 #define TWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
     88 #define TWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
     89 #define TREAD1(sc, r) bus_space_read_1((sc)->memt, (sc)->memh, (r))
     90 #define TREAD2(sc, r) bus_space_read_2((sc)->memt, (sc)->memh, (r))
     91 #define TREAD4(sc, r) bus_space_read_4((sc)->memt, (sc)->memh, (r))
     92 
     93 static int	autri_attach_codec(void *, struct ac97_codec_if *);
     94 static int	autri_read_codec(void *, uint8_t, uint16_t *);
     95 static int	autri_write_codec(void *, uint8_t, uint16_t);
     96 static int	autri_reset_codec(void *);
     97 static enum ac97_host_flags	autri_flags_codec(void *);
     98 
     99 static bool autri_resume(device_t, const pmf_qual_t *);
    100 static int  autri_init(void *);
    101 static struct autri_dma *autri_find_dma(struct autri_softc *, void *);
    102 static void autri_setup_channel(struct autri_softc *, int,
    103 				const audio_params_t *param);
    104 static void autri_enable_interrupt(struct autri_softc *, int);
    105 static void autri_disable_interrupt(struct autri_softc *, int);
    106 static void autri_startch(struct autri_softc *, int, int);
    107 static void autri_stopch(struct autri_softc *, int, int);
    108 static void autri_enable_loop_interrupt(void *);
    109 #if 0
    110 static void autri_disable_loop_interrupt(void *);
    111 #endif
    112 
    113 static int	autri_open(void *, int);
    114 static int	autri_query_encoding(void *, struct audio_encoding *);
    115 static int	autri_set_params(void *, int, int, audio_params_t *,
    116 				 audio_params_t *, stream_filter_list_t *,
    117 				 stream_filter_list_t *);
    118 static int	autri_round_blocksize(void *, int, int, const audio_params_t *);
    119 static int	autri_trigger_output(void *, void *, void *, int,
    120 				     void (*)(void *), void *,
    121 				     const audio_params_t *);
    122 static int	autri_trigger_input(void *, void *, void *, int,
    123 				    void (*)(void *), void *,
    124 				    const audio_params_t *);
    125 static int	autri_halt_output(void *);
    126 static int	autri_halt_input(void *);
    127 static int	autri_getdev(void *, struct audio_device *);
    128 static int	autri_mixer_set_port(void *, mixer_ctrl_t *);
    129 static int	autri_mixer_get_port(void *, mixer_ctrl_t *);
    130 static void*	autri_malloc(void *, int, size_t);
    131 static void	autri_free(void *, void *, size_t);
    132 static size_t	autri_round_buffersize(void *, int, size_t);
    133 static paddr_t autri_mappage(void *, void *, off_t, int);
    134 static int	autri_get_props(void *);
    135 static int	autri_query_devinfo(void *, mixer_devinfo_t *);
    136 static void	autri_get_locks(void *, kmutex_t **, kmutex_t **);
    137 
    138 static const struct audio_hw_if autri_hw_if = {
    139 	.open			= autri_open,
    140 	.query_encoding		= autri_query_encoding,
    141 	.set_params		= autri_set_params,
    142 	.round_blocksize	= autri_round_blocksize,
    143 	.halt_output		= autri_halt_output,
    144 	.halt_input		= autri_halt_input,
    145 	.getdev			= autri_getdev,
    146 	.set_port		= autri_mixer_set_port,
    147 	.get_port		= autri_mixer_get_port,
    148 	.query_devinfo		= autri_query_devinfo,
    149 	.allocm			= autri_malloc,
    150 	.freem			= autri_free,
    151 	.round_buffersize	= autri_round_buffersize,
    152 	.mappage		= autri_mappage,
    153 	.get_props		= autri_get_props,
    154 	.trigger_output		= autri_trigger_output,
    155 	.trigger_input		= autri_trigger_input,
    156 	.get_locks		= autri_get_locks,
    157 };
    158 
    159 #if NMIDI > 0
    160 static void	autri_midi_close(void *);
    161 static void	autri_midi_getinfo(void *, struct midi_info *);
    162 static int	autri_midi_open(void *, int, void (*)(void *, int),
    163 			   void (*)(void *), void *);
    164 static int	autri_midi_output(void *, int);
    165 
    166 static const struct midi_hw_if autri_midi_hw_if = {
    167 	autri_midi_open,
    168 	autri_midi_close,
    169 	autri_midi_output,
    170 	autri_midi_getinfo,
    171 	NULL,			/* ioctl */
    172 	autri_get_locks,
    173 };
    174 #endif
    175 
    176 #define AUTRI_NFORMATS	8
    177 static const struct audio_format autri_formats[AUTRI_NFORMATS] = {
    178 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    179 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    180 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    181 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    182 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    183 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    184 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    185 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    186 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    187 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    188 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    189 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    190 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    191 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    192 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    193 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    194 };
    195 
    196 /*
    197  * register set/clear bit
    198  */
    199 #if NMIDI > 0
    200 static inline void
    201 autri_reg_set_1(struct autri_softc *sc, int no, uint8_t mask)
    202 {
    203 	bus_space_write_1(sc->memt, sc->memh, no,
    204 	    (bus_space_read_1(sc->memt, sc->memh, no) | mask));
    205 }
    206 
    207 static inline void
    208 autri_reg_clear_1(struct autri_softc *sc, int no, uint8_t mask)
    209 {
    210 	bus_space_write_1(sc->memt, sc->memh, no,
    211 	    (bus_space_read_1(sc->memt, sc->memh, no) & ~mask));
    212 }
    213 #endif
    214 
    215 static inline void
    216 autri_reg_set_4(struct autri_softc *sc, int no, uint32_t mask)
    217 {
    218 	bus_space_write_4(sc->memt, sc->memh, no,
    219 	    (bus_space_read_4(sc->memt, sc->memh, no) | mask));
    220 }
    221 
    222 static inline void
    223 autri_reg_clear_4(struct autri_softc *sc, int no, uint32_t mask)
    224 {
    225 	bus_space_write_4(sc->memt, sc->memh, no,
    226 	    (bus_space_read_4(sc->memt, sc->memh, no) & ~mask));
    227 }
    228 
    229 /*
    230  * AC'97 codec
    231  */
    232 static int
    233 autri_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    234 {
    235 	struct autri_codec_softc *sc;
    236 
    237 	DPRINTF(("autri_attach_codec()\n"));
    238 	sc = sc_;
    239 	sc->codec_if = codec_if;
    240 	return 0;
    241 }
    242 
    243 static int
    244 autri_read_codec(void *sc_, uint8_t index, uint16_t *data)
    245 {
    246 	struct autri_codec_softc *codec;
    247 	struct autri_softc *sc;
    248 	uint32_t status, addr, cmd, busy;
    249 	uint16_t count;
    250 
    251 	codec = sc_;
    252 	sc = codec->sc;
    253 	/*DPRINTF(("sc->sc->type : 0x%X",sc->sc->type));*/
    254 
    255 	switch (sc->sc_devid) {
    256 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    257 		addr = AUTRI_DX_ACR1;
    258 		cmd  = AUTRI_DX_ACR1_CMD_READ;
    259 		busy = AUTRI_DX_ACR1_BUSY_READ;
    260 		break;
    261 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    262 		addr = AUTRI_NX_ACR2;
    263 		cmd  = AUTRI_NX_ACR2_CMD_READ;
    264 		busy = AUTRI_NX_ACR2_BUSY_READ | AUTRI_NX_ACR2_RECV_WAIT;
    265 		break;
    266 	case AUTRI_DEVICE_ID_SIS_7018:
    267 		addr = AUTRI_SIS_ACRD;
    268 		cmd  = AUTRI_SIS_ACRD_CMD_READ;
    269 		busy = AUTRI_SIS_ACRD_BUSY_READ | AUTRI_SIS_ACRD_AUDIO_BUSY;
    270 		break;
    271 	case AUTRI_DEVICE_ID_ALI_M5451:
    272 		if (sc->sc_revision > 0x01)
    273 			addr = AUTRI_ALI_ACWR;
    274 		else
    275 			addr = AUTRI_ALI_ACRD;
    276 		cmd  = AUTRI_ALI_ACRD_CMD_READ;
    277 		busy = AUTRI_ALI_ACRD_BUSY_READ;
    278 		break;
    279 	default:
    280 		printf("%s: autri_read_codec : unknown device\n",
    281 		       device_xname(sc->sc_dev));
    282 		return -1;
    283 	}
    284 
    285 	/* wait for 'Ready to Read' */
    286 	for (count=0; count<0xffff; count++) {
    287 		if ((TREAD4(sc, addr) & busy) == 0)
    288 			break;
    289 	}
    290 
    291 	if (count == 0xffff) {
    292 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    293 		       device_xname(sc->sc_dev));
    294 		return -1;
    295 	}
    296 
    297 	/* send Read Command to AC'97 */
    298 	TWRITE4(sc, addr, (index & 0x7f) | cmd);
    299 
    300 	/* wait for 'Returned data is avalable' */
    301 	for (count=0; count<0xffff; count++) {
    302 		status = TREAD4(sc, addr);
    303 		if ((status & busy) == 0)
    304 			break;
    305 	}
    306 
    307 	if (count == 0xffff) {
    308 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    309 		       device_xname(sc->sc_dev));
    310 		return -1;
    311 	}
    312 
    313 	*data =  (status >> 16) & 0x0000ffff;
    314 	/*DPRINTF(("autri_read_codec(0x%X) return 0x%X\n",reg,*data));*/
    315 	return 0;
    316 }
    317 
    318 static int
    319 autri_write_codec(void *sc_, uint8_t index, uint16_t data)
    320 {
    321 	struct autri_codec_softc *codec;
    322 	struct autri_softc *sc;
    323 	uint32_t addr, cmd, busy;
    324 	uint16_t count;
    325 
    326 	codec = sc_;
    327 	sc = codec->sc;
    328 	/*DPRINTF(("autri_write_codec(0x%X,0x%X)\n",index,data));*/
    329 
    330 	switch (sc->sc_devid) {
    331 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    332 		addr = AUTRI_DX_ACR0;
    333 		cmd  = AUTRI_DX_ACR0_CMD_WRITE;
    334 		busy = AUTRI_DX_ACR0_BUSY_WRITE;
    335 		break;
    336 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    337 		addr = AUTRI_NX_ACR1;
    338 		cmd  = AUTRI_NX_ACR1_CMD_WRITE;
    339 		busy = AUTRI_NX_ACR1_BUSY_WRITE;
    340 		break;
    341 	case AUTRI_DEVICE_ID_SIS_7018:
    342 		addr = AUTRI_SIS_ACWR;
    343 		cmd  = AUTRI_SIS_ACWR_CMD_WRITE;
    344 		busy = AUTRI_SIS_ACWR_BUSY_WRITE | AUTRI_SIS_ACWR_AUDIO_BUSY;
    345 		break;
    346 	case AUTRI_DEVICE_ID_ALI_M5451:
    347 		addr = AUTRI_ALI_ACWR;
    348 		cmd  = AUTRI_ALI_ACWR_CMD_WRITE;
    349 		if (sc->sc_revision > 0x01)
    350 			cmd  |= 0x0100;
    351 		busy = AUTRI_ALI_ACWR_BUSY_WRITE;
    352 		break;
    353 	default:
    354 		printf("%s: autri_write_codec : unknown device.\n",
    355 		       device_xname(sc->sc_dev));
    356 		return -1;
    357 	}
    358 
    359 	/* wait for 'Ready to Write' */
    360 	for (count=0; count<0xffff; count++) {
    361 		if ((TREAD4(sc, addr) & busy) == 0)
    362 			break;
    363 	}
    364 
    365 	if (count == 0xffff) {
    366 		printf("%s: Codec timeout. Busy writing AC'97 codec\n",
    367 		       device_xname(sc->sc_dev));
    368 		return -1;
    369 	}
    370 
    371 	/* send Write Command to AC'97 */
    372 	TWRITE4(sc, addr, (data << 16) | (index & 0x7f) | cmd);
    373 
    374 	return 0;
    375 }
    376 
    377 static int
    378 autri_reset_codec(void *sc_)
    379 {
    380 	struct autri_codec_softc *codec;
    381 	struct autri_softc *sc;
    382 	uint32_t reg, ready;
    383 	int addr, count;
    384 
    385 	codec = sc_;
    386 	sc = codec->sc;
    387 	count = 200;
    388 	DPRINTF(("autri_reset_codec(codec=%p,sc=%p)\n", codec, sc));
    389 	DPRINTF(("sc->sc_devid=%X\n", sc->sc_devid));
    390 
    391 	switch (sc->sc_devid) {
    392 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    393 		/* warm reset AC'97 codec */
    394 		autri_reg_set_4(sc, AUTRI_DX_ACR2, 1);
    395 		delay(100);
    396 		/* release reset */
    397 		autri_reg_clear_4(sc, AUTRI_DX_ACR2, 1);
    398 		delay(100);
    399 
    400 		addr = AUTRI_DX_ACR2;
    401 		ready = AUTRI_DX_ACR2_CODEC_READY;
    402 		break;
    403 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    404 		/* warm reset AC'97 codec */
    405 		autri_reg_set_4(sc, AUTRI_NX_ACR0, 1);
    406 		delay(100);
    407 		/* release reset */
    408 		autri_reg_clear_4(sc, AUTRI_NX_ACR0, 1);
    409 		delay(100);
    410 
    411 		addr = AUTRI_NX_ACR0;
    412 		ready = AUTRI_NX_ACR0_CODEC_READY;
    413 		break;
    414 	case AUTRI_DEVICE_ID_SIS_7018:
    415 		/* cold reset AC'97 codec */
    416 		autri_reg_set_4(sc, AUTRI_SIS_SCTRL, 2);
    417 		delay(1000);
    418 		/* release reset (warm & cold) */
    419 		autri_reg_clear_4(sc, AUTRI_SIS_SCTRL, 3);
    420 		delay(2000);
    421 
    422 		addr = AUTRI_SIS_SCTRL;
    423 		ready = AUTRI_SIS_SCTRL_CODEC_READY;
    424 		break;
    425 	case AUTRI_DEVICE_ID_ALI_M5451:
    426 		/* warm reset AC'97 codec */
    427 		autri_reg_set_4(sc, AUTRI_ALI_SCTRL, 1);
    428 		delay(100);
    429 		/* release reset (warm & cold) */
    430 		autri_reg_clear_4(sc, AUTRI_ALI_SCTRL, 3);
    431 		delay(100);
    432 
    433 		addr = AUTRI_ALI_SCTRL;
    434 		ready = AUTRI_ALI_SCTRL_CODEC_READY;
    435 		break;
    436 	default:
    437 		printf("%s: autri_reset_codec : unknown device\n",
    438 		       device_xname(sc->sc_dev));
    439 		return EOPNOTSUPP;
    440 	}
    441 
    442 	/* wait for 'Codec Ready' */
    443 	while (count--) {
    444 		reg = TREAD4(sc, addr);
    445 		if (reg & ready)
    446 			break;
    447 		delay(1000);
    448 	}
    449 
    450 	if (count == 0) {
    451 		printf("%s: Codec timeout. AC'97 is not ready for operation.\n",
    452 		       device_xname(sc->sc_dev));
    453 		return ETIMEDOUT;
    454 	}
    455 	return 0;
    456 }
    457 
    458 static enum ac97_host_flags
    459 autri_flags_codec(void *sc)
    460 {
    461 	return AC97_HOST_DONT_READ;
    462 }
    463 
    464 /*
    465  *
    466  */
    467 
    468 static int
    469 autri_match(device_t parent, cfdata_t match, void *aux)
    470 {
    471 	struct pci_attach_args *pa;
    472 
    473 	pa = (struct pci_attach_args *)aux;
    474 	switch (PCI_VENDOR(pa->pa_id)) {
    475 	case PCI_VENDOR_TRIDENT:
    476 		switch (PCI_PRODUCT(pa->pa_id)) {
    477 		case PCI_PRODUCT_TRIDENT_4DWAVE_DX:
    478 			/*
    479 			 * IBM makes a pcn network card and improperly
    480 			 * sets the vendor and product ID's.  Avoid matching.
    481 			 */
    482 			if (PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK)
    483 				return 0;
    484 		/* FALLTHROUGH */
    485 		case PCI_PRODUCT_TRIDENT_4DWAVE_NX:
    486 			return 1;
    487 		}
    488 		break;
    489 	case PCI_VENDOR_SIS:
    490 		switch (PCI_PRODUCT(pa->pa_id)) {
    491 		case PCI_PRODUCT_SIS_7018:
    492 			return 1;
    493 		}
    494 		break;
    495 	case PCI_VENDOR_ALI:
    496 		switch (PCI_PRODUCT(pa->pa_id)) {
    497 		case PCI_PRODUCT_ALI_M5451:
    498 			return 1;
    499 		}
    500 		break;
    501 	}
    502 
    503 	return 0;
    504 }
    505 
    506 static void
    507 autri_attach(device_t parent, device_t self, void *aux)
    508 {
    509 	struct autri_softc *sc;
    510 	struct pci_attach_args *pa;
    511 	pci_chipset_tag_t pc;
    512 	struct autri_codec_softc *codec;
    513 	pci_intr_handle_t ih;
    514 	char const *intrstr;
    515 	int r;
    516 	uint32_t reg;
    517 	char intrbuf[PCI_INTRSTR_LEN];
    518 
    519 	sc = device_private(self);
    520 	sc->sc_dev = self;
    521 	pa = (struct pci_attach_args *)aux;
    522 	pc = pa->pa_pc;
    523 
    524 	sc->sc_devid = pa->pa_id;
    525 	sc->sc_class = pa->pa_class;
    526 
    527 	pci_aprint_devinfo(pa, "Audio controller");
    528 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    529 
    530 	/* map register to memory */
    531 	if (pci_mapreg_map(pa, AUTRI_PCI_MEMORY_BASE,
    532 	    PCI_MAPREG_TYPE_MEM, 0, &sc->memt, &sc->memh, NULL, NULL)) {
    533 		aprint_error_dev(sc->sc_dev, "can't map memory space\n");
    534 		return;
    535 	}
    536 
    537 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    538 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    539 
    540 	/* map and establish the interrupt */
    541 	if (pci_intr_map(pa, &ih)) {
    542 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
    543 		return;
    544 	}
    545 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    546 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, autri_intr,
    547 	    sc, device_xname(self));
    548 	if (sc->sc_ih == NULL) {
    549 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    550 		if (intrstr != NULL)
    551 			aprint_error(" at %s", intrstr);
    552 		aprint_error("\n");
    553 		return;
    554 	}
    555 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    556 
    557 	sc->sc_dmatag = pa->pa_dmat;
    558 	sc->sc_pc = pc;
    559 	sc->sc_pt = pa->pa_tag;
    560 
    561 	/* enable the device */
    562 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    563 	reg |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    564 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    565 
    566 	/* initialize the device */
    567 	autri_init(sc);
    568 
    569 	/* attach AC'97 codec */
    570 	codec = &sc->sc_codec;
    571 	codec->sc = sc;
    572 
    573 	codec->host_if.arg = codec;
    574 	codec->host_if.attach = autri_attach_codec;
    575 	codec->host_if.reset = autri_reset_codec;
    576 	codec->host_if.read = autri_read_codec;
    577 	codec->host_if.write = autri_write_codec;
    578 	codec->host_if.flags = autri_flags_codec;
    579 
    580 	r = ac97_attach(&codec->host_if, self, &sc->sc_lock);
    581 	if (r != 0) {
    582 		aprint_error_dev(self, "can't attach codec (error 0x%X)\n", r);
    583 		return;
    584 	}
    585 
    586 	if (!pmf_device_register(self, NULL, autri_resume))
    587 		aprint_error_dev(self, "couldn't establish power handler\n");
    588 
    589 	audio_attach_mi(&autri_hw_if, sc, sc->sc_dev);
    590 
    591 #if NMIDI > 0
    592 	midi_attach_mi(&autri_midi_hw_if, sc, sc->sc_dev);
    593 #endif
    594 }
    595 
    596 CFATTACH_DECL_NEW(autri, sizeof(struct autri_softc),
    597     autri_match, autri_attach, NULL, NULL);
    598 
    599 static bool
    600 autri_resume(device_t dv, const pmf_qual_t *qual)
    601 {
    602 	struct autri_softc *sc = device_private(dv);
    603 
    604 	mutex_enter(&sc->sc_lock);
    605 	mutex_spin_enter(&sc->sc_intr_lock);
    606 	autri_init(sc);
    607 	mutex_spin_exit(&sc->sc_intr_lock);
    608 	(sc->sc_codec.codec_if->vtbl->restore_ports)(sc->sc_codec.codec_if);
    609 	mutex_exit(&sc->sc_lock);
    610 
    611 	return true;
    612 }
    613 
    614 static int
    615 autri_init(void *sc_)
    616 {
    617 	struct autri_softc *sc;
    618 	uint32_t reg;
    619 	pci_chipset_tag_t pc;
    620 	pcitag_t pt;
    621 
    622 	sc = sc_;
    623 	pc = sc->sc_pc;
    624 	pt = sc->sc_pt;
    625 	DPRINTF(("in autri_init()\n"));
    626 	DPRINTFN(5,("pci_conf_read(0x40) : 0x%X\n",pci_conf_read(pc,pt,0x40)));
    627 	DPRINTFN(5,("pci_conf_read(0x44) : 0x%X\n",pci_conf_read(pc,pt,0x44)));
    628 
    629 	switch (sc->sc_devid) {
    630 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    631 		/* disable Legacy Control */
    632 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    633 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    634 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    635 		delay(100);
    636 		/* audio engine reset */
    637 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    638 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00040000);
    639 		delay(100);
    640 		/* release reset */
    641 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    642 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    643 		delay(100);
    644 		/* DAC on */
    645 		autri_reg_set_4(sc,AUTRI_DX_ACR2,0x02);
    646 		break;
    647 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    648 		/* disable Legacy Control */
    649 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    650 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    651 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    652 		delay(100);
    653 		/* audio engine reset */
    654 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    655 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00010000);
    656 		delay(100);
    657 		/* release reset */
    658 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    659 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00010000);
    660 		delay(100);
    661 		/* DAC on */
    662 		autri_reg_set_4(sc,AUTRI_NX_ACR0,0x02);
    663 		break;
    664 	case AUTRI_DEVICE_ID_SIS_7018:
    665 		/* disable Legacy Control */
    666 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    667 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    668 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    669 		delay(100);
    670 		/* reset Digital Controller */
    671 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    672 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    673 		delay(100);
    674 		/* release reset */
    675 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    676 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    677 		delay(100);
    678 		/* disable AC97 GPIO interrupt */
    679 		TWRITE1(sc, AUTRI_SIS_ACGPIO, 0);
    680 		/* enable 64 channel mode */
    681 		autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, BANK_B_EN);
    682 		break;
    683 	case AUTRI_DEVICE_ID_ALI_M5451:
    684 		/* disable Legacy Control */
    685 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    686 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    687 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    688 		delay(100);
    689 		/* reset Digital Controller */
    690 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    691 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    692 		delay(100);
    693 		/* release reset */
    694 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    695 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    696 		delay(100);
    697 		/* enable PCM input */
    698 		autri_reg_set_4(sc, AUTRI_ALI_GCONTROL, AUTRI_ALI_GCONTROL_PCM_IN);
    699 		break;
    700 	}
    701 
    702 	if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    703 		sc->sc_play.ch      = 0;
    704 		sc->sc_play.ch_intr = 1;
    705 		sc->sc_rec.ch       = 31;
    706 		sc->sc_rec.ch_intr  = 2;
    707 	} else {
    708 		sc->sc_play.ch      = 0x20;
    709 		sc->sc_play.ch_intr = 0x21;
    710 		sc->sc_rec.ch       = 0x22;
    711 		sc->sc_rec.ch_intr  = 0x23;
    712 	}
    713 
    714 	/* clear channel status */
    715 	TWRITE4(sc, AUTRI_STOP_A, 0xffffffff);
    716 	TWRITE4(sc, AUTRI_STOP_B, 0xffffffff);
    717 
    718 	/* disable channel interrupt */
    719 	TWRITE4(sc, AUTRI_AINTEN_A, 0);
    720 	TWRITE4(sc, AUTRI_AINTEN_B, 0);
    721 
    722 #if 0
    723 	/* TLB */
    724 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    725 		TWRITE4(sc,AUTRI_NX_TLBC,0);
    726 	}
    727 #endif
    728 
    729 	autri_enable_loop_interrupt(sc);
    730 
    731 	DPRINTF(("out autri_init()\n"));
    732 	return 0;
    733 }
    734 
    735 static void
    736 autri_enable_loop_interrupt(void *sc_)
    737 {
    738 	struct autri_softc *sc;
    739 	uint32_t reg;
    740 
    741 	/*reg = (ENDLP_IE | MIDLP_IE);*/
    742 	reg = ENDLP_IE;
    743 	sc = sc_;
    744 	if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
    745 		reg |= BANK_B_EN;
    746 
    747 	autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, reg);
    748 }
    749 
    750 #if 0
    751 static void
    752 autri_disable_loop_interrupt(void *sc_)
    753 {
    754 	struct autri_softc *sc;
    755 	uint32_t reg;
    756 
    757 	reg = (ENDLP_IE | MIDLP_IE);
    758 	sc = sc_;
    759 	autri_reg_clear_4(sc, AUTRI_LFO_GC_CIR, reg);
    760 }
    761 #endif
    762 
    763 static int
    764 autri_intr(void *p)
    765 {
    766 	struct autri_softc *sc;
    767 	uint32_t intsrc;
    768 	uint32_t mask, active[2];
    769 	int ch, endch;
    770 /*
    771 	u_int32_t reg;
    772 	u_int32_t cso,eso;
    773 */
    774 	sc = p;
    775 	mutex_spin_enter(&sc->sc_intr_lock);
    776 
    777 	intsrc = TREAD4(sc, AUTRI_MISCINT);
    778 	if ((intsrc & (ADDRESS_IRQ | MPU401_IRQ)) == 0) {
    779 		mutex_spin_exit(&sc->sc_intr_lock);
    780 		return 0;
    781 	}
    782 
    783 	if (intsrc & ADDRESS_IRQ) {
    784 
    785 		active[0] = TREAD4(sc,AUTRI_AIN_A);
    786 		active[1] = TREAD4(sc,AUTRI_AIN_B);
    787 
    788 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    789 			endch = 32;
    790 		} else {
    791 			endch = 64;
    792 		}
    793 
    794 		for (ch = 0; ch < endch; ch++) {
    795 			mask = 1 << (ch & 0x1f);
    796 			if (active[(ch & 0x20) ? 1 : 0] & mask) {
    797 
    798 				/* clear interrupt */
    799 				TWRITE4(sc, (ch & 0x20) ? AUTRI_AIN_B : AUTRI_AIN_A, mask);
    800 				/* disable interrupt */
    801 				autri_reg_clear_4(sc,(ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    802 #if 0
    803 				reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
    804 				TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | ch);
    805 
    806 				if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    807 					cso = TREAD4(sc, 0xe0) & 0x00ffffff;
    808 					eso = TREAD4(sc, 0xe8) & 0x00ffffff;
    809 				} else {
    810 					cso = (TREAD4(sc, 0xe0) >> 16) & 0x0000ffff;
    811 					eso = (TREAD4(sc, 0xe8) >> 16) & 0x0000ffff;
    812 				}
    813 				/*printf("cso=%d, eso=%d\n",cso,eso);*/
    814 #endif
    815 				if (ch == sc->sc_play.ch_intr) {
    816 					if (sc->sc_play.intr)
    817 						sc->sc_play.intr(sc->sc_play.intr_arg);
    818 				}
    819 
    820 				if (ch == sc->sc_rec.ch_intr) {
    821 					if (sc->sc_rec.intr)
    822 						sc->sc_rec.intr(sc->sc_rec.intr_arg);
    823 				}
    824 
    825 				/* enable interrupt */
    826 				autri_reg_set_4(sc, (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    827 			}
    828 		}
    829 	}
    830 
    831 	if (intsrc & MPU401_IRQ) {
    832 		/* XXX */
    833 	}
    834 
    835 	autri_reg_set_4(sc,AUTRI_MISCINT,
    836 		ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW);
    837 
    838 	mutex_spin_exit(&sc->sc_intr_lock);
    839 	return 1;
    840 }
    841 
    842 /*
    843  *
    844  */
    845 
    846 static int
    847 autri_allocmem(struct autri_softc *sc, size_t size, size_t align,
    848 	       struct autri_dma *p)
    849 {
    850 	int error;
    851 
    852 	p->size = size;
    853 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    854 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    855 	    &p->nsegs, BUS_DMA_WAITOK);
    856 	if (error)
    857 		return error;
    858 
    859 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    860 	    &p->addr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    861 	if (error)
    862 		goto free;
    863 
    864 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    865 	    0, BUS_DMA_WAITOK, &p->map);
    866 	if (error)
    867 		goto unmap;
    868 
    869 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    870 	    BUS_DMA_WAITOK);
    871 	if (error)
    872 		goto destroy;
    873 	return (0);
    874 
    875 destroy:
    876 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    877 unmap:
    878 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    879 free:
    880 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    881 	return error;
    882 }
    883 
    884 static int
    885 autri_freemem(struct autri_softc *sc, struct autri_dma *p)
    886 {
    887 
    888 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    889 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    890 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    891 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    892 	return 0;
    893 }
    894 
    895 static int
    896 autri_open(void *addr, int flags)
    897 {
    898 	DPRINTF(("autri_open()\n"));
    899 	DPRINTFN(5,("MISCINT    : 0x%08X\n",
    900 		    TREAD4((struct autri_softc *)addr, AUTRI_MISCINT)));
    901 	DPRINTFN(5,("LFO_GC_CIR : 0x%08X\n",
    902 		    TREAD4((struct autri_softc *)addr, AUTRI_LFO_GC_CIR)));
    903 	return 0;
    904 }
    905 
    906 static int
    907 autri_query_encoding(void *addr, struct audio_encoding *fp)
    908 {
    909 
    910 	switch (fp->index) {
    911 	case 0:
    912 		strcpy(fp->name, AudioEulinear);
    913 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    914 		fp->precision = 8;
    915 		fp->flags = 0;
    916 		break;
    917 	case 1:
    918 		strcpy(fp->name, AudioEmulaw);
    919 		fp->encoding = AUDIO_ENCODING_ULAW;
    920 		fp->precision = 8;
    921 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    922 		break;
    923 	case 2:
    924 		strcpy(fp->name, AudioEalaw);
    925 		fp->encoding = AUDIO_ENCODING_ALAW;
    926 		fp->precision = 8;
    927 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    928 		break;
    929 	case 3:
    930 		strcpy(fp->name, AudioEslinear);
    931 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    932 		fp->precision = 8;
    933 		fp->flags = 0;
    934 		break;
    935 	case 4:
    936 		strcpy(fp->name, AudioEslinear_le);
    937 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    938 		fp->precision = 16;
    939 		fp->flags = 0;
    940 		break;
    941 	case 5:
    942 		strcpy(fp->name, AudioEulinear_le);
    943 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    944 		fp->precision = 16;
    945 		fp->flags = 0;
    946 		break;
    947 	case 6:
    948 		strcpy(fp->name, AudioEslinear_be);
    949 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    950 		fp->precision = 16;
    951 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    952 		break;
    953 	case 7:
    954 		strcpy(fp->name, AudioEulinear_be);
    955 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    956 		fp->precision = 16;
    957 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    958 		break;
    959 	default:
    960 		return EINVAL;
    961 	}
    962 
    963 	return 0;
    964 }
    965 
    966 static int
    967 autri_set_params(void *addr, int setmode, int usemode,
    968     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    969     stream_filter_list_t *rfil)
    970 {
    971 	if (setmode & AUMODE_RECORD) {
    972 		if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
    973 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
    974 			return EINVAL;
    975 	}
    976 	if (setmode & AUMODE_PLAY) {
    977 		if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
    978 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
    979 			return EINVAL;
    980 	}
    981 	return 0;
    982 }
    983 
    984 static int
    985 autri_round_blocksize(void *addr, int block,
    986     int mode, const audio_params_t *param)
    987 {
    988 	return block & -4;
    989 }
    990 
    991 static int
    992 autri_halt_output(void *addr)
    993 {
    994 	struct autri_softc *sc;
    995 
    996 	DPRINTF(("autri_halt_output()\n"));
    997 	sc = addr;
    998 	sc->sc_play.intr = NULL;
    999 	autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
   1000 	autri_disable_interrupt(sc, sc->sc_play.ch_intr);
   1001 
   1002 	return 0;
   1003 }
   1004 
   1005 static int
   1006 autri_halt_input(void *addr)
   1007 {
   1008 	struct autri_softc *sc;
   1009 
   1010 	DPRINTF(("autri_halt_input()\n"));
   1011 	sc = addr;
   1012 	sc->sc_rec.intr = NULL;
   1013 	autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1014 	autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
   1015 
   1016 	return 0;
   1017 }
   1018 
   1019 static int
   1020 autri_getdev(void *addr, struct audio_device *retp)
   1021 {
   1022 	struct autri_softc *sc;
   1023 
   1024 	DPRINTF(("autri_getdev().\n"));
   1025 	sc = addr;
   1026 	strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
   1027 	snprintf(retp->version, sizeof(retp->version), "0x%02x",
   1028 	     PCI_REVISION(sc->sc_class));
   1029 
   1030 	switch (sc->sc_devid) {
   1031 	case AUTRI_DEVICE_ID_4DWAVE_DX:
   1032 		strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
   1033 		break;
   1034 	case AUTRI_DEVICE_ID_4DWAVE_NX:
   1035 		strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
   1036 		break;
   1037 	case AUTRI_DEVICE_ID_SIS_7018:
   1038 		strncpy(retp->config, "SiS 7018", sizeof(retp->config));
   1039 		break;
   1040 	case AUTRI_DEVICE_ID_ALI_M5451:
   1041 		strncpy(retp->config, "ALi M5451", sizeof(retp->config));
   1042 		break;
   1043 	default:
   1044 		strncpy(retp->config, "unknown", sizeof(retp->config));
   1045 	}
   1046 
   1047 	return 0;
   1048 }
   1049 
   1050 static int
   1051 autri_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1052 {
   1053 	struct autri_softc *sc;
   1054 
   1055 	sc = addr;
   1056 	return sc->sc_codec.codec_if->vtbl->mixer_set_port(
   1057 	    sc->sc_codec.codec_if, cp);
   1058 }
   1059 
   1060 static int
   1061 autri_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1062 {
   1063 	struct autri_softc *sc;
   1064 
   1065 	sc = addr;
   1066 	return sc->sc_codec.codec_if->vtbl->mixer_get_port(
   1067 	    sc->sc_codec.codec_if, cp);
   1068 }
   1069 
   1070 static int
   1071 autri_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1072 {
   1073 	struct autri_softc *sc;
   1074 
   1075 	sc = addr;
   1076 	return sc->sc_codec.codec_if->vtbl->query_devinfo(
   1077 	    sc->sc_codec.codec_if, dip);
   1078 }
   1079 
   1080 static void *
   1081 autri_malloc(void *addr, int direction, size_t size)
   1082 {
   1083 	struct autri_softc *sc;
   1084 	struct autri_dma *p;
   1085 	int error;
   1086 
   1087 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
   1088 	sc = addr;
   1089 #if 0
   1090 	error = autri_allocmem(sc, size, 16, p);
   1091 #endif
   1092 	error = autri_allocmem(sc, size, 0x10000, p);
   1093 	if (error) {
   1094 		kmem_free(p, sizeof(*p));
   1095 		return NULL;
   1096 	}
   1097 
   1098 	p->next = sc->sc_dmas;
   1099 	sc->sc_dmas = p;
   1100 	return KERNADDR(p);
   1101 }
   1102 
   1103 static void
   1104 autri_free(void *addr, void *ptr, size_t size)
   1105 {
   1106 	struct autri_softc *sc;
   1107 	struct autri_dma **pp, *p;
   1108 
   1109 	sc = addr;
   1110 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1111 		if (KERNADDR(p) == ptr) {
   1112 			autri_freemem(sc, p);
   1113 			*pp = p->next;
   1114 			kmem_free(p, sizeof(*p));
   1115 			return;
   1116 		}
   1117 	}
   1118 }
   1119 
   1120 static struct autri_dma *
   1121 autri_find_dma(struct autri_softc *sc, void *addr)
   1122 {
   1123 	struct autri_dma *p;
   1124 
   1125 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1126 		continue;
   1127 
   1128 	return p;
   1129 }
   1130 
   1131 static size_t
   1132 autri_round_buffersize(void *addr, int direction, size_t size)
   1133 {
   1134 
   1135 	return size;
   1136 }
   1137 
   1138 static paddr_t
   1139 autri_mappage(void *addr, void *mem, off_t off, int prot)
   1140 {
   1141 	struct autri_softc *sc;
   1142 	struct autri_dma *p;
   1143 
   1144 	if (off < 0)
   1145 		return -1;
   1146 	sc = addr;
   1147 	p = autri_find_dma(sc, mem);
   1148 	if (!p)
   1149 		return -1;
   1150 
   1151 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1152 	    off, prot, BUS_DMA_WAITOK);
   1153 }
   1154 
   1155 static int
   1156 autri_get_props(void *addr)
   1157 {
   1158 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1159 	    AUDIO_PROP_FULLDUPLEX;
   1160 }
   1161 
   1162 static void
   1163 autri_setup_channel(struct autri_softc *sc, int mode,
   1164 		    const audio_params_t *param)
   1165 {
   1166 	int i, ch, channel;
   1167 	uint32_t reg, cr[5];
   1168 	uint32_t cso, eso;
   1169 	uint32_t delta, dch[2], ctrl;
   1170 	uint32_t alpha_fms, fm_vol, attribute;
   1171 
   1172 	uint32_t dmaaddr, dmalen;
   1173 	int factor, rvol, cvol;
   1174 	struct autri_chstatus *chst;
   1175 
   1176 	ctrl = AUTRI_CTRL_LOOPMODE;
   1177 	switch (param->encoding) {
   1178 	case AUDIO_ENCODING_SLINEAR_BE:
   1179 	case AUDIO_ENCODING_SLINEAR_LE:
   1180 		ctrl |= AUTRI_CTRL_SIGNED;
   1181 		break;
   1182 	}
   1183 
   1184 	factor = 0;
   1185 	if (param->precision == 16) {
   1186 		ctrl |= AUTRI_CTRL_16BIT;
   1187 		factor++;
   1188 	}
   1189 
   1190 	if (param->channels == 2) {
   1191 		ctrl |= AUTRI_CTRL_STEREO;
   1192 		factor++;
   1193 	}
   1194 
   1195 	delta = param->sample_rate;
   1196 	if (delta < 4000)
   1197 		delta = 4000;
   1198 	if (delta > 48000)
   1199 		delta = 48000;
   1200 
   1201 	attribute = 0;
   1202 
   1203 	dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
   1204 	if (mode == AUMODE_PLAY) {
   1205 		chst = &sc->sc_play;
   1206 		dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
   1207 		ctrl |= AUTRI_CTRL_WAVEVOL;
   1208 	} else {
   1209 		chst = &sc->sc_rec;
   1210 		dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
   1211 		if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018) {
   1212 			ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1213 			attribute = AUTRI_ATTR_PCMREC_SIS;
   1214 			if (delta != 48000)
   1215 				attribute |= AUTRI_ATTR_ENASRC_SIS;
   1216 		} else
   1217 			ctrl |= AUTRI_CTRL_MUTEVOL;
   1218 	}
   1219 
   1220 	dmaaddr = DMAADDR(chst->dma);
   1221 	cso = alpha_fms = 0;
   1222 	rvol = cvol = 0x7f;
   1223 	fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
   1224 
   1225 	for (ch = 0; ch < 2; ch++) {
   1226 
   1227 		if (ch == 0)
   1228 			dmalen = (chst->length >> factor);
   1229 		else {
   1230 			/* channel for interrupt */
   1231 			dmalen = (chst->blksize >> factor);
   1232 			if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
   1233 				ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1234 			else
   1235 				ctrl |= AUTRI_CTRL_MUTEVOL;
   1236 			attribute = 0;
   1237 			cso = dmalen - 1;
   1238 		}
   1239 
   1240 		eso = dmalen - 1;
   1241 
   1242 		switch (sc->sc_devid) {
   1243 		case AUTRI_DEVICE_ID_4DWAVE_DX:
   1244 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1245 			cr[1] = dmaaddr;
   1246 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1247 			cr[3] = fm_vol;
   1248 			cr[4] = ctrl;
   1249 			break;
   1250 		case AUTRI_DEVICE_ID_4DWAVE_NX:
   1251 			cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
   1252 			cr[1] = dmaaddr;
   1253 			cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
   1254 			cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
   1255 			cr[4] = ctrl;
   1256 			break;
   1257 		case AUTRI_DEVICE_ID_SIS_7018:
   1258 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1259 			cr[1] = dmaaddr;
   1260 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1261 			cr[3] = attribute;
   1262 			cr[4] = ctrl;
   1263 			break;
   1264 		case AUTRI_DEVICE_ID_ALI_M5451:
   1265 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1266 			cr[1] = dmaaddr;
   1267 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1268 			cr[3] = 0;
   1269 			cr[4] = ctrl;
   1270 			break;
   1271 		}
   1272 
   1273 		/* write channel data */
   1274 		channel = (ch == 0) ? chst->ch : chst->ch_intr;
   1275 
   1276 		reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
   1277 		TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
   1278 
   1279 		for (i = 0; i < 5; i++) {
   1280 			TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
   1281 			DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
   1282 		}
   1283 
   1284 		/* Bank A only */
   1285 		if (channel < 0x20) {
   1286 			TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
   1287 			TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
   1288 		}
   1289 	}
   1290 
   1291 }
   1292 
   1293 static int
   1294 autri_trigger_output(void *addr, void *start, void *end, int blksize,
   1295 		     void (*intr)(void *), void *arg,
   1296 		     const audio_params_t *param)
   1297 {
   1298 	struct autri_softc *sc;
   1299 	struct autri_dma *p;
   1300 
   1301 	DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
   1302 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1303 	sc = addr;
   1304 	sc->sc_play.intr = intr;
   1305 	sc->sc_play.intr_arg = arg;
   1306 	sc->sc_play.offset = 0;
   1307 	sc->sc_play.blksize = blksize;
   1308 	sc->sc_play.length = (char *)end - (char *)start;
   1309 
   1310 	p = autri_find_dma(sc, start);
   1311 	if (!p) {
   1312 		printf("autri_trigger_output: bad addr %p\n", start);
   1313 		return (EINVAL);
   1314 	}
   1315 
   1316 	sc->sc_play.dma = p;
   1317 
   1318 	/* */
   1319 	autri_setup_channel(sc, AUMODE_PLAY, param);
   1320 
   1321 	/* volume set to no attenuation */
   1322 	TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
   1323 
   1324 	/* enable interrupt */
   1325 	autri_enable_interrupt(sc, sc->sc_play.ch_intr);
   1326 
   1327 	/* start channel */
   1328 	autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
   1329 
   1330 	return 0;
   1331 }
   1332 
   1333 static int
   1334 autri_trigger_input(void *addr, void *start, void *end, int blksize,
   1335 		    void (*intr)(void *), void *arg,
   1336 		    const audio_params_t *param)
   1337 {
   1338 	struct autri_softc *sc;
   1339 	struct autri_dma *p;
   1340 
   1341 	DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
   1342 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1343 	sc = addr;
   1344 	sc->sc_rec.intr = intr;
   1345 	sc->sc_rec.intr_arg = arg;
   1346 	sc->sc_rec.offset = 0;
   1347 	sc->sc_rec.blksize = blksize;
   1348 	sc->sc_rec.length = (char *)end - (char *)start;
   1349 
   1350 	/* */
   1351 	p = autri_find_dma(sc, start);
   1352 	if (!p) {
   1353 		printf("autri_trigger_input: bad addr %p\n", start);
   1354 		return (EINVAL);
   1355 	}
   1356 
   1357 	sc->sc_rec.dma = p;
   1358 
   1359 	/* */
   1360 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
   1361 		autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
   1362 		TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
   1363 	}
   1364 
   1365 #if 0
   1366 	/* 4DWAVE only allows capturing at a 48 kHz rate */
   1367 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
   1368 	    sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
   1369 		param->sample_rate = 48000;
   1370 #endif
   1371 
   1372 	autri_setup_channel(sc, AUMODE_RECORD, param);
   1373 
   1374 	/* enable interrupt */
   1375 	autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
   1376 
   1377 	/* start channel */
   1378 	autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1379 
   1380 	return 0;
   1381 }
   1382 
   1383 
   1384 static void
   1385 autri_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
   1386 {
   1387 	struct autri_softc *sc;
   1388 
   1389 	sc = addr;
   1390 	*intr = &sc->sc_intr_lock;
   1391 	*proc = &sc->sc_lock;
   1392 }
   1393 
   1394 #if 0
   1395 static int
   1396 autri_halt(struct autri_softc *sc)
   1397 {
   1398 
   1399 	DPRINTF(("autri_halt().\n"));
   1400 	/*autri_stopch(sc);*/
   1401 	autri_disable_interrupt(sc, sc->sc_play.channel);
   1402 	autri_disable_interrupt(sc, sc->sc_rec.channel);
   1403 	return 0;
   1404 }
   1405 #endif
   1406 
   1407 static void
   1408 autri_enable_interrupt(struct autri_softc *sc, int ch)
   1409 {
   1410 	int reg;
   1411 
   1412 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1413 	ch &= 0x1f;
   1414 
   1415 	autri_reg_set_4(sc, reg, 1 << ch);
   1416 }
   1417 
   1418 static void
   1419 autri_disable_interrupt(struct autri_softc *sc, int ch)
   1420 {
   1421 	int reg;
   1422 
   1423 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1424 	ch &= 0x1f;
   1425 
   1426 	autri_reg_clear_4(sc, reg, 1 << ch);
   1427 }
   1428 
   1429 static void
   1430 autri_startch(struct autri_softc *sc, int ch, int ch_intr)
   1431 {
   1432 	int reg;
   1433 	uint32_t chmask;
   1434 
   1435 	reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
   1436 	ch &= 0x1f;
   1437 	ch_intr &= 0x1f;
   1438 	chmask = (1 << ch) | (1 << ch_intr);
   1439 
   1440 	autri_reg_set_4(sc, reg, chmask);
   1441 }
   1442 
   1443 static void
   1444 autri_stopch(struct autri_softc *sc, int ch, int ch_intr)
   1445 {
   1446 	int reg;
   1447 	uint32_t chmask;
   1448 
   1449 	reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
   1450 	ch &= 0x1f;
   1451 	ch_intr &= 0x1f;
   1452 	chmask = (1 << ch) | (1 << ch_intr);
   1453 
   1454 	autri_reg_set_4(sc, reg, chmask);
   1455 }
   1456 
   1457 #if NMIDI > 0
   1458 static int
   1459 autri_midi_open(void *addr, int flags, void (*iintr)(void *, int),
   1460 		void (*ointr)(void *), void *arg)
   1461 {
   1462 	struct autri_softc *sc;
   1463 
   1464 	DPRINTF(("autri_midi_open()\n"));
   1465 	sc = addr;
   1466 	DPRINTFN(5,("MPUR1 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR1)));
   1467 	DPRINTFN(5,("MPUR2 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR2)));
   1468 
   1469 	sc->sc_iintr = iintr;
   1470 	sc->sc_ointr = ointr;
   1471 	sc->sc_arg = arg;
   1472 
   1473 	if (flags & FREAD)
   1474 		autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
   1475 
   1476 	if (flags & FWRITE)
   1477 		autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
   1478 
   1479 	return 0;
   1480 }
   1481 
   1482 static void
   1483 autri_midi_close(void *addr)
   1484 {
   1485 	struct autri_softc *sc;
   1486 
   1487 	DPRINTF(("autri_midi_close()\n"));
   1488 	sc = addr;
   1489 	kpause("autri", FALSE, hz/10, &sc->sc_lock); /* give uart a chance to drain */
   1490 
   1491 	sc->sc_iintr = NULL;
   1492 	sc->sc_ointr = NULL;
   1493 }
   1494 
   1495 static int
   1496 autri_midi_output(void *addr, int d)
   1497 {
   1498 	struct autri_softc *sc;
   1499 	int x;
   1500 
   1501 	sc = addr;
   1502 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1503 		if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
   1504 			TWRITE1(sc, AUTRI_MPUR0, d);
   1505 			return 0;
   1506 		}
   1507 		delay(MIDI_BUSY_DELAY);
   1508 	}
   1509 	return EIO;
   1510 }
   1511 
   1512 static void
   1513 autri_midi_getinfo(void *addr, struct midi_info *mi)
   1514 {
   1515 
   1516 	mi->name = "4DWAVE MIDI UART";
   1517 	mi->props = MIDI_PROP_CAN_INPUT;
   1518 }
   1519 
   1520 #endif
   1521