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