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