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