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