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