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