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