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