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