xref: /netbsd/sys/dev/pci/autri.c (revision bf9ec67e)
1 /*	$NetBSD: autri.c,v 1.4 2002/02/05 01:51:51 eeh 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 
103 static void autri_powerhook(int why,void *addr);
104 static int  autri_init(void *sc);
105 static struct autri_dma *autri_find_dma(struct autri_softc *, void *);
106 static void autri_setup_channel(struct autri_softc *sc,int mode,
107 				struct audio_params *param);
108 static void autri_enable_interrupt(struct autri_softc *sc, int ch);
109 static void autri_disable_interrupt(struct autri_softc *sc, int ch);
110 static void autri_startch(struct autri_softc *sc, int ch, int ch_intr);
111 static void autri_stopch(struct autri_softc *sc, int ch, int ch_intr);
112 static void autri_enable_loop_interrupt(void *sc);
113 #if 0
114 static void autri_disable_loop_interrupt(void *sc);
115 #endif
116 
117 struct cfattach autri_ca = {
118 	sizeof(struct autri_softc), autri_match, autri_attach
119 };
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, int, int);
137 void	autri_free(void *, void *, int);
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 		/* warm reset AC'97 codec */
402 		autri_reg_set_4(sc, AUTRI_SIS_SCTRL, 1);
403 		delay(100);
404 		/* release reset (warm & cold) */
405 		autri_reg_clear_4(sc, AUTRI_SIS_SCTRL, 3);
406 		delay(100);
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 /*
438  *
439  */
440 
441 int
442 autri_match(struct device *parent, struct cfdata *match, void *aux)
443 {
444 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
445 
446 	switch (PCI_VENDOR(pa->pa_id)) {
447 	case PCI_VENDOR_TRIDENT:
448 		switch (PCI_PRODUCT(pa->pa_id)) {
449 		case PCI_PRODUCT_TRIDENT_4DWAVE_DX:
450 		case PCI_PRODUCT_TRIDENT_4DWAVE_NX:
451 			return 1;
452 		}
453 		break;
454 	case PCI_VENDOR_SIS:
455 		switch (PCI_PRODUCT(pa->pa_id)) {
456 		case PCI_PRODUCT_SIS_7018:
457 			return 1;
458 		}
459 		break;
460 	case PCI_VENDOR_ALI:
461 		switch (PCI_PRODUCT(pa->pa_id)) {
462 		case PCI_PRODUCT_ALI_M5451:
463 			return 1;
464 		}
465 		break;
466 	}
467 
468 	return 0;
469 }
470 
471 void
472 autri_attach(struct device *parent, struct device *self, void *aux)
473 {
474 	struct autri_softc *sc = (struct autri_softc *)self;
475 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
476 	pci_chipset_tag_t pc = pa->pa_pc;
477 	struct autri_codec_softc *codec;
478 	pci_intr_handle_t ih;
479 	char const *intrstr;
480 	char devinfo[256];
481 	mixer_ctrl_t ctl;
482 	int i, r;
483 	u_int32_t reg;
484 
485 	sc->sc_devid = pa->pa_id;
486 	sc->sc_class = pa->pa_class;
487 
488 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
489 	sc->sc_revision = PCI_REVISION(pa->pa_class);
490 	printf(": %s (rev. 0x%02x)\n", devinfo, sc->sc_revision);
491 
492 	/* map register to memory */
493 	if (pci_mapreg_map(pa, AUTRI_PCI_MEMORY_BASE,
494 	    PCI_MAPREG_TYPE_MEM, 0, &sc->memt, &sc->memh, NULL, NULL)) {
495 		printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
496 		return;
497 	}
498 
499 	/* map and establish the interrupt */
500 	if (pci_intr_map(pa, &ih)) {
501 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
502 		return;
503 	}
504 	intrstr = pci_intr_string(pc, ih);
505 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, autri_intr, sc);
506 	if (sc->sc_ih == NULL) {
507 		printf("%s: couldn't establish interrupt",
508 		    sc->sc_dev.dv_xname);
509 		if (intrstr != NULL)
510 			printf(" at %s", intrstr);
511 		printf("\n");
512 		return;
513 	}
514 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
515 
516 	sc->sc_dmatag = pa->pa_dmat;
517 	sc->sc_pc = pc;
518 	sc->sc_pt = pa->pa_tag;
519 
520 	/* enable the device */
521 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
522 	reg |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
523 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
524 
525 	/* initialize the device */
526 	autri_init(sc);
527 
528 	/* attach AC'97 codec */
529 	codec = &sc->sc_codec;
530 	memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
531 	codec->sc = sc;
532 
533 	codec->host_if.arg = codec;
534 	codec->host_if.attach = autri_attach_codec;
535 	codec->host_if.reset = autri_reset_codec;
536 	codec->host_if.read = autri_read_codec;
537 	codec->host_if.write = autri_write_codec;
538 
539 	if ((r = ac97_attach(&codec->host_if)) != 0) {
540 		printf("%s: can't attach codec (error 0x%X)\n",
541 		    sc->sc_dev.dv_xname, r);
542 		return;
543 	}
544 
545 	/* disable mutes */
546 	for (i = 0; i < 4; i++) {
547 		static struct {
548 			char *class, *device;
549 		} d[] = {
550 			{ AudioCoutputs, AudioNmaster},
551 			{ AudioCinputs, AudioNdac},
552 			{ AudioCinputs, AudioNcd},
553 			{ AudioCrecord, AudioNvolume},
554 		};
555 
556 		ctl.type = AUDIO_MIXER_ENUM;
557 		ctl.un.ord = 0;
558 
559 #if 0
560 		ctl.dev = sc->sc_codec.codec_if->vtbl->get_portnum_by_name(sc->sc_codec.codec_if,
561 		    d[i].class, d[i].device, AudioNmute);
562 #endif
563 		ctl.dev = autri_get_portnum_by_name(sc,d[i].class,
564 						   d[i].device, AudioNmute);
565 		autri_mixer_set_port(sc, &ctl);
566 	}
567 
568 	/* set a reasonable default volume */
569 	ctl.type = AUDIO_MIXER_VALUE;
570 	ctl.un.value.num_channels = 2;
571 	ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
572 	ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 127;
573 
574 	ctl.dev = autri_get_portnum_by_name(sc,AudioCoutputs,AudioNmaster,NULL);
575 	autri_mixer_set_port(sc, &ctl);
576 
577 	audio_attach_mi(&autri_hw_if, sc, &sc->sc_dev);
578 
579 #if NMIDI > 0
580 	midi_attach_mi(&autri_midi_hw_if, sc, &sc->sc_dev);
581 #endif
582 
583 	sc->sc_old_power = PWR_RESUME;
584 	powerhook_establish(autri_powerhook, sc);
585 }
586 
587 static void
588 autri_powerhook(int why, void *addr)
589 {
590 	struct autri_softc *sc = addr;
591 
592 	if (why == PWR_RESUME && sc->sc_old_power == PWR_SUSPEND) {
593 		DPRINTF(("PWR_RESUME\n"));
594 		autri_init(sc);
595 		/*autri_reset_codec(&sc->sc_codec);*/
596 		(sc->sc_codec.codec_if->vtbl->restore_ports)(sc->sc_codec.codec_if);
597 	}
598 	sc->sc_old_power = why;
599 }
600 
601 int
602 autri_init(void *sc_)
603 {
604 	struct autri_softc *sc = sc_;
605 	u_int32_t reg;
606 
607 	pci_chipset_tag_t pc = sc->sc_pc;
608 	pcitag_t pt = sc->sc_pt;
609 
610 	DPRINTF(("in autri_init()\n"));
611 	DPRINTFN(5,("pci_conf_read(0x40) : 0x%X\n",pci_conf_read(pc,pt,0x40)));
612 	DPRINTFN(5,("pci_conf_read(0x44) : 0x%X\n",pci_conf_read(pc,pt,0x44)));
613 
614 	switch (sc->sc_devid) {
615 	case AUTRI_DEVICE_ID_4DWAVE_DX:
616 		/* disable Legacy Control */
617 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
618 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
619 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
620 		delay(100);
621 		/* audio engine reset */
622 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
623 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00040000);
624 		delay(100);
625 		/* release reset */
626 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
627 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
628 		delay(100);
629 		/* DAC on */
630 		autri_reg_set_4(sc,AUTRI_DX_ACR2,0x02);
631 		break;
632 	case AUTRI_DEVICE_ID_4DWAVE_NX:
633 		/* disable Legacy Control */
634 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
635 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
636 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
637 		delay(100);
638 		/* audio engine reset */
639 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
640 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00010000);
641 		delay(100);
642 		/* release reset */
643 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
644 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00010000);
645 		delay(100);
646 		/* DAC on */
647 		autri_reg_set_4(sc,AUTRI_NX_ACR0,0x02);
648 		break;
649 	case AUTRI_DEVICE_ID_SIS_7018:
650 		/* disable Legacy Control */
651 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
652 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
653 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
654 		delay(100);
655 		/* reset Digital Controller */
656 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
657 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
658 		delay(100);
659 		/* release reset */
660 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
661 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
662 		delay(100);
663 		/* disable AC97 GPIO interrupt */
664 		TWRITE1(sc, AUTRI_SIS_ACGPIO, 0);
665 		/* enable 64 channel mode */
666 		autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, BANK_B_EN);
667 		break;
668 	case AUTRI_DEVICE_ID_ALI_M5451:
669 		/* disable Legacy Control */
670 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
671 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
672 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
673 		delay(100);
674 		/* reset Digital Controller */
675 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
676 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
677 		delay(100);
678 		/* release reset */
679 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
680 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
681 		delay(100);
682 		/* enable PCM input */
683 		autri_reg_set_4(sc, AUTRI_ALI_GCONTROL, AUTRI_ALI_GCONTROL_PCM_IN);
684 		break;
685 	}
686 
687 	if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
688 		sc->sc_play.ch      = 0;
689 		sc->sc_play.ch_intr = 1;
690 		sc->sc_rec.ch       = 31;
691 		sc->sc_rec.ch_intr  = 2;
692 	} else {
693 		sc->sc_play.ch      = 0x20;
694 		sc->sc_play.ch_intr = 0x21;
695 		sc->sc_rec.ch       = 0x22;
696 		sc->sc_rec.ch_intr  = 0x23;
697 	}
698 
699 	/* clear channel status */
700 	TWRITE4(sc, AUTRI_STOP_A, 0xffffffff);
701 	TWRITE4(sc, AUTRI_STOP_B, 0xffffffff);
702 
703 	/* disable channel interrupt */
704 	TWRITE4(sc, AUTRI_AINTEN_A, 0);
705 	TWRITE4(sc, AUTRI_AINTEN_B, 0);
706 
707 #if 0
708 	/* TLB */
709 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
710 		TWRITE4(sc,AUTRI_NX_TLBC,0);
711 	}
712 #endif
713 
714 	autri_enable_loop_interrupt(sc);
715 
716 	DPRINTF(("out autri_init()\n"));
717 	return 0;
718 }
719 
720 static void
721 autri_enable_loop_interrupt(void *sc_)
722 {
723 	struct autri_softc *sc = sc_;
724 	u_int32_t reg;
725 
726 	/*reg = (ENDLP_IE | MIDLP_IE);*/
727 	reg = ENDLP_IE;
728 #if 0
729 	if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
730 		reg |= BANK_B_EN;
731 #endif
732 	autri_reg_set_4(sc,AUTRI_LFO_GC_CIR,reg);
733 }
734 
735 #if 0
736 static void
737 autri_disable_loop_interrupt(void *sc_)
738 {
739 	struct autri_softc *sc = sc_;
740 	u_int32_t reg;
741 
742 	reg = (ENDLP_IE | MIDLP_IE);
743 	autri_reg_clear_4(sc,AUTRI_LFO_GC_CIR,reg);
744 }
745 #endif
746 
747 int
748 autri_intr(void *p)
749 {
750 	struct autri_softc *sc = p;
751 	u_int32_t intsrc;
752 	u_int32_t mask, active[2];
753 	int ch, endch;
754 /*
755 	u_int32_t reg;
756 	u_int32_t cso,eso;
757 */
758 
759 	intsrc = TREAD4(sc,AUTRI_MISCINT);
760 	if ((intsrc & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
761 		return 0;
762 
763 	if (intsrc & ADDRESS_IRQ) {
764 
765 		active[0] = TREAD4(sc,AUTRI_AIN_A);
766 		active[1] = TREAD4(sc,AUTRI_AIN_B);
767 
768 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
769 			endch = 32;
770 		} else {
771 			endch = 64;
772 		}
773 
774 		for (ch=0; ch<endch; ch++) {
775 			mask = 1 << (ch & 0x1f);
776 			if (active[(ch & 0x20) ? 1 : 0] & mask) {
777 
778 				/* clear interupt */
779 				TWRITE4(sc, (ch & 0x20) ? AUTRI_AIN_B : AUTRI_AIN_A, mask);
780 				/* disable interupt */
781 				autri_reg_clear_4(sc,(ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
782 #if 0
783 				reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
784 				TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | ch);
785 
786 				if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
787 				  cso = TREAD4(sc, 0xe0) & 0x00ffffff;
788 				  eso = TREAD4(sc, 0xe8) & 0x00ffffff;
789 				} else {
790 				  cso = (TREAD4(sc, 0xe0) >> 16) & 0x0000ffff;
791 				  eso = (TREAD4(sc, 0xe8) >> 16) & 0x0000ffff;
792 				}
793 				/*printf("cso=%d, eso=%d\n",cso,eso);*/
794 #endif
795 				if (ch == sc->sc_play.ch_intr) {
796 					if (sc->sc_play.intr)
797 						sc->sc_play.intr(sc->sc_play.intr_arg);
798 				}
799 
800 				if (ch == sc->sc_rec.ch_intr) {
801 					if (sc->sc_rec.intr)
802 						sc->sc_rec.intr(sc->sc_rec.intr_arg);
803 				}
804 
805 				/* enable interrupt */
806 				autri_reg_set_4(sc, (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
807 			}
808 		}
809 	}
810 
811 	if (intsrc & MPU401_IRQ) {
812 		/* XXX */
813 	}
814 
815 	autri_reg_set_4(sc,AUTRI_MISCINT,
816 		ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW);
817 
818 	return 1;
819 }
820 
821 /*
822  *
823  */
824 
825 int
826 autri_allocmem(struct autri_softc *sc, size_t size, size_t align,
827 	       struct autri_dma *p)
828 {
829 	int error;
830 
831 	p->size = size;
832 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
833 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
834 	    &p->nsegs, BUS_DMA_NOWAIT);
835 	if (error)
836 		return (error);
837 
838 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
839 	    &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
840 	if (error)
841 		goto free;
842 
843 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
844 	    0, BUS_DMA_NOWAIT, &p->map);
845 	if (error)
846 		goto unmap;
847 
848 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
849 	    BUS_DMA_NOWAIT);
850 	if (error)
851 		goto destroy;
852 	return (0);
853 
854 destroy:
855 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
856 unmap:
857 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
858 free:
859 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
860 	return (error);
861 }
862 
863 int
864 autri_freemem(struct autri_softc *sc, struct autri_dma *p)
865 {
866 	bus_dmamap_unload(sc->sc_dmatag, p->map);
867 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
868 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
869 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
870 	return 0;
871 }
872 
873 int
874 autri_open(void *addr, int flags)
875 {
876 	DPRINTF(("autri_open()\n"));
877 	DPRINTFN(5,("MISCINT    : 0x%08X\n",
878 		    TREAD4((struct autri_softc *)addr, AUTRI_MISCINT)));
879 	DPRINTFN(5,("LFO_GC_CIR : 0x%08X\n",
880 		    TREAD4((struct autri_softc *)addr, AUTRI_LFO_GC_CIR)));
881         return 0;
882 }
883 
884 void
885 autri_close(void *addr)
886 {
887 	DPRINTF(("autri_close()\n"));
888 }
889 
890 int
891 autri_query_encoding(void *addr, struct audio_encoding *fp)
892 {
893 	switch (fp->index) {
894 	case 0:
895 		strcpy(fp->name, AudioEulinear);
896 		fp->encoding = AUDIO_ENCODING_ULINEAR;
897 		fp->precision = 8;
898 		fp->flags = 0;
899 		break;
900 	case 1:
901 		strcpy(fp->name, AudioEmulaw);
902 		fp->encoding = AUDIO_ENCODING_ULAW;
903 		fp->precision = 8;
904 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
905 		break;
906 	case 2:
907 		strcpy(fp->name, AudioEalaw);
908 		fp->encoding = AUDIO_ENCODING_ALAW;
909 		fp->precision = 8;
910 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
911 		break;
912 	case 3:
913 		strcpy(fp->name, AudioEslinear);
914 		fp->encoding = AUDIO_ENCODING_SLINEAR;
915 		fp->precision = 8;
916 		fp->flags = 0;
917 		break;
918 	case 4:
919 		strcpy(fp->name, AudioEslinear_le);
920 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
921 		fp->precision = 16;
922 		fp->flags = 0;
923 		break;
924 	case 5:
925 		strcpy(fp->name, AudioEulinear_le);
926 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
927 		fp->precision = 16;
928 		fp->flags = 0;
929 		break;
930 	case 6:
931 		strcpy(fp->name, AudioEslinear_be);
932 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
933 		fp->precision = 16;
934 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
935 		break;
936 	case 7:
937 		strcpy(fp->name, AudioEulinear_be);
938 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
939 		fp->precision = 16;
940 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
941 		break;
942 	default:
943 		return (EINVAL);
944 	}
945 
946 	return 0;
947 }
948 
949 int
950 autri_set_params(void *addr, int setmode, int usemode,
951 		 struct audio_params *play, struct audio_params *rec)
952 {
953 	struct audio_params *p;
954 	int mode;
955 
956 	for (mode = AUMODE_RECORD; mode != -1;
957 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
958 		if ((setmode & mode) == 0)
959 			continue;
960 
961 		p = mode == AUMODE_PLAY ? play : rec;
962 
963 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
964 		    (p->precision != 8 && p->precision != 16) ||
965 		    (p->channels != 1 && p->channels != 2))
966 			return (EINVAL);
967 
968 		p->factor = 1;
969 		p->sw_code = 0;
970 		switch (p->encoding) {
971 		case AUDIO_ENCODING_SLINEAR_BE:
972 		case AUDIO_ENCODING_ULINEAR_BE:
973 			if (p->precision == 16)
974 				p->sw_code = swap_bytes;
975 			break;
976 		case AUDIO_ENCODING_SLINEAR_LE:
977 		case AUDIO_ENCODING_ULINEAR_LE:
978 			break;
979 		case AUDIO_ENCODING_ULAW:
980 			if (mode == AUMODE_PLAY)
981 				p->sw_code = mulaw_to_ulinear8;
982 			else
983 				p->sw_code = ulinear8_to_mulaw;
984 
985 			break;
986 		case AUDIO_ENCODING_ALAW:
987 			if (mode == AUMODE_PLAY)
988 				p->sw_code = alaw_to_ulinear8;
989 			else
990 				p->sw_code = ulinear8_to_alaw;
991 
992 			break;
993 		default:
994 			return (EINVAL);
995 		}
996 	}
997 
998 	return 0;
999 }
1000 
1001 int
1002 autri_round_blocksize(void *addr, int block)
1003 {
1004 	return (block & -4);
1005 }
1006 
1007 int
1008 autri_halt_output(void *addr)
1009 {
1010 	struct autri_softc *sc = addr;
1011 
1012 	DPRINTF(("autri_halt_output()\n"));
1013 
1014 	sc->sc_play.intr = NULL;
1015 	autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1016 	autri_disable_interrupt(sc, sc->sc_play.ch_intr);
1017 
1018 	return 0;
1019 }
1020 
1021 int
1022 autri_halt_input(void *addr)
1023 {
1024 	struct autri_softc *sc = addr;
1025 
1026 	DPRINTF(("autri_halt_input()\n"));
1027 
1028 	sc->sc_rec.intr = NULL;
1029 	autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1030 	autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
1031 
1032 	return 0;
1033 }
1034 
1035 int
1036 autri_getdev(void *addr, struct audio_device *retp)
1037 {
1038 	struct autri_softc *sc = addr;
1039 
1040 	DPRINTF(("autri_getdev().\n"));
1041 
1042 	strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
1043 	snprintf(retp->version, sizeof(retp->version), "0x%02x",
1044 	     PCI_REVISION(sc->sc_class));
1045 
1046 	switch (sc->sc_devid) {
1047 	case AUTRI_DEVICE_ID_4DWAVE_DX:
1048 		strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
1049 		break;
1050 	case AUTRI_DEVICE_ID_4DWAVE_NX:
1051 		strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
1052 		break;
1053 	case AUTRI_DEVICE_ID_SIS_7018:
1054 		strncpy(retp->config, "SiS 7018", sizeof(retp->config));
1055 		break;
1056 	case AUTRI_DEVICE_ID_ALI_M5451:
1057 		strncpy(retp->config, "ALi M5451", sizeof(retp->config));
1058 		break;
1059 	default:
1060 		strncpy(retp->config, "unknown", sizeof(retp->config));
1061 	}
1062 
1063 	return 0;
1064 }
1065 
1066 int
1067 autri_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1068 {
1069 	struct autri_softc *sc = addr;
1070 
1071 	return (sc->sc_codec.codec_if->vtbl->mixer_set_port(
1072 	    sc->sc_codec.codec_if, cp));
1073 }
1074 
1075 int
1076 autri_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1077 {
1078 	struct autri_softc *sc = addr;
1079 
1080 	return (sc->sc_codec.codec_if->vtbl->mixer_get_port(
1081 	    sc->sc_codec.codec_if, cp));
1082 }
1083 
1084 int
1085 autri_query_devinfo(void *addr, mixer_devinfo_t *dip)
1086 {
1087 	struct autri_softc *sc = addr;
1088 
1089 	return (sc->sc_codec.codec_if->vtbl->query_devinfo(
1090 	    sc->sc_codec.codec_if, dip));
1091 }
1092 
1093 int
1094 autri_get_portnum_by_name(struct autri_softc *sc, char *class,
1095 			  char *device, char *qualifier)
1096 {
1097 	return (sc->sc_codec.codec_if->vtbl->get_portnum_by_name(
1098 	    sc->sc_codec.codec_if, class, device, qualifier));
1099 }
1100 
1101 void *
1102 autri_malloc(void *addr, int direction, size_t size, int pool, int flags)
1103 {
1104 	struct autri_softc *sc = addr;
1105 	struct autri_dma *p;
1106 	int error;
1107 
1108 	p = malloc(sizeof(*p), pool, flags);
1109 	if (!p)
1110 		return NULL;
1111 
1112 #if 0
1113 	error = autri_allocmem(sc, size, 16, p);
1114 #endif
1115 	error = autri_allocmem(sc, size, 0x10000, p);
1116 	if (error) {
1117 		free(p, pool);
1118 		return NULL;
1119 	}
1120 
1121 	p->next = sc->sc_dmas;
1122 	sc->sc_dmas = p;
1123 	return KERNADDR(p);
1124 }
1125 
1126 void
1127 autri_free(void *addr, void *ptr, int pool)
1128 {
1129 	struct autri_softc *sc = addr;
1130 	struct autri_dma **pp, *p;
1131 
1132 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1133 		if (KERNADDR(p) == ptr) {
1134 			autri_freemem(sc, p);
1135 			*pp = p->next;
1136 			free(p, pool);
1137 			return;
1138 		}
1139 	}
1140 }
1141 
1142 static struct autri_dma *
1143 autri_find_dma(struct autri_softc *sc, void *addr)
1144 {
1145 	struct autri_dma *p;
1146 
1147 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1148 		;
1149 
1150 	return p;
1151 }
1152 
1153 size_t
1154 autri_round_buffersize(void *addr, int direction, size_t size)
1155 {
1156 	return size;
1157 }
1158 
1159 paddr_t
1160 autri_mappage(void *addr, void *mem, off_t off, int prot)
1161 {
1162 	struct autri_softc *sc = addr;
1163 	struct autri_dma *p;
1164 
1165 	if (off < 0)
1166 		return (-1);
1167 
1168 	p = autri_find_dma(sc, mem);
1169 	if (!p)
1170 		return (-1);
1171 
1172 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1173 	    off, prot, BUS_DMA_WAITOK));
1174 }
1175 
1176 int
1177 autri_get_props(void *addr)
1178 {
1179 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1180 		AUDIO_PROP_FULLDUPLEX);
1181 }
1182 
1183 static void
1184 autri_setup_channel(struct autri_softc *sc, int mode,
1185 		    struct audio_params *param)
1186 {
1187 	int i, ch, channel;
1188 	u_int32_t reg, cr[5];
1189 	u_int32_t cso, eso;
1190 	u_int32_t delta, dch[2], ctrl;
1191 	u_int32_t alpha_fms, fm_vol, attribute;
1192 
1193 	u_int32_t dmaaddr, dmalen;
1194 	int factor, rvol, cvol;
1195 	struct autri_chstatus *chst;
1196 
1197 	ctrl = AUTRI_CTRL_LOOPMODE;
1198 	switch (param->encoding) {
1199 	case AUDIO_ENCODING_SLINEAR_BE:
1200 	case AUDIO_ENCODING_SLINEAR_LE:
1201 		ctrl |= AUTRI_CTRL_SIGNED;
1202 		break;
1203 	}
1204 
1205 	factor = 0;
1206 	if (param->precision == 16) {
1207 		ctrl |= AUTRI_CTRL_16BIT;
1208 		factor++;
1209 	}
1210 
1211 	if (param->channels == 2) {
1212 		ctrl |= AUTRI_CTRL_STEREO;
1213 		factor++;
1214 	}
1215 
1216 	delta = (u_int32_t)param->sample_rate;
1217 	if (delta < 4000)
1218 		delta = 4000;
1219 	if (delta > 48000)
1220 		delta = 48000;
1221 
1222 	dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
1223 	if (mode == AUMODE_PLAY) {
1224 		chst = &sc->sc_play;
1225 		dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
1226 		if (sc->sc_devid != AUTRI_DEVICE_ID_SIS_7018)
1227 			ctrl |= AUTRI_CTRL_WAVEVOL;
1228 /*
1229 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451)
1230 			ctrl |= 0x80000000;
1231 */
1232 	} else {
1233 		chst = &sc->sc_rec;
1234 		dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
1235 		ctrl |= AUTRI_CTRL_MUTE;
1236 	}
1237 
1238 	dmaaddr = DMAADDR(chst->dma);
1239 	cso = alpha_fms = 0;
1240 	rvol = cvol = 0x7f;
1241 	fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
1242 	attribute = 0;
1243 
1244 	for (ch=0; ch<2; ch++) {
1245 
1246 		if (ch == 0)
1247 			dmalen = (chst->length >> factor);
1248 		else {
1249 			/* channel for interrupt */
1250 			dmalen = (chst->blksize >> factor);
1251 			ctrl |= AUTRI_CTRL_MUTE;
1252 		}
1253 
1254 		eso = dmalen - 1;
1255 
1256 		switch (sc->sc_devid) {
1257 		case AUTRI_DEVICE_ID_4DWAVE_DX:
1258 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1259 			cr[1] = dmaaddr;
1260 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1261 			cr[3] = fm_vol;
1262 			cr[4] = ctrl;
1263 			break;
1264 		case AUTRI_DEVICE_ID_4DWAVE_NX:
1265 			cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
1266 			cr[1] = dmaaddr;
1267 			cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
1268 			cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
1269 			cr[4] = ctrl;
1270 			break;
1271 		case AUTRI_DEVICE_ID_SIS_7018:
1272 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1273 			cr[1] = dmaaddr;
1274 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1275 			cr[3] = (attribute << 16) | (fm_vol & 0x0000ffff);
1276 			cr[4] = ctrl;
1277 			break;
1278 		case AUTRI_DEVICE_ID_ALI_M5451:
1279 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1280 			cr[1] = dmaaddr;
1281 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1282 			cr[3] = 0;
1283 			cr[4] = ctrl;
1284 			break;
1285 		}
1286 
1287 		/* write channel data */
1288 		channel = (ch == 0) ? chst->ch : chst->ch_intr;
1289 
1290 		reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
1291 		TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
1292 
1293 		for (i=0; i<5; i++) {
1294 			TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
1295 			DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
1296 		}
1297 
1298 		/* Bank A only */
1299 		if (channel < 0x20) {
1300 			TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
1301 			TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
1302 		}
1303 	}
1304 
1305 }
1306 
1307 int
1308 autri_trigger_output(void *addr, void *start, void *end, int blksize,
1309 		     void (*intr)(void *), void *arg,
1310 		     struct audio_params *param)
1311 {
1312 	struct autri_softc *sc = addr;
1313         struct autri_dma *p;
1314 
1315 	DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
1316 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1317 
1318 	sc->sc_play.intr = intr;
1319 	sc->sc_play.intr_arg = arg;
1320 	sc->sc_play.offset = 0;
1321 	sc->sc_play.blksize = blksize;
1322 	sc->sc_play.length = (char *)end - (char *)start;
1323 
1324 	p = autri_find_dma(sc, start);
1325 	if (!p) {
1326 		printf("autri_trigger_output: bad addr %p\n", start);
1327 		return (EINVAL);
1328 	}
1329 
1330 	sc->sc_play.dma = p;
1331 
1332 	/* */
1333 	autri_setup_channel(sc, AUMODE_PLAY, param);
1334 
1335 	/* volume set to no attenuation */
1336 	TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
1337 
1338 	/* enable interrupt */
1339 	autri_enable_interrupt(sc, sc->sc_play.ch_intr);
1340 
1341 	/* start channel */
1342 	autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1343 
1344 	return 0;
1345 }
1346 
1347 int
1348 autri_trigger_input(void *addr, void *start, void *end, int blksize,
1349 		    void (*intr)(void *), void *arg,
1350 		    struct audio_params *param)
1351 {
1352 	struct autri_softc *sc = addr;
1353         struct autri_dma *p;
1354 
1355 	DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
1356 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1357 
1358 	sc->sc_rec.intr = intr;
1359 	sc->sc_rec.intr_arg = arg;
1360 	sc->sc_rec.offset = 0;
1361 	sc->sc_rec.blksize = blksize;
1362 	sc->sc_rec.length = (char *)end - (char *)start;
1363 
1364 	/* */
1365 	p = autri_find_dma(sc, start);
1366 	if (!p) {
1367 		printf("autri_trigger_input: bad addr %p\n", start);
1368 		return (EINVAL);
1369 	}
1370 
1371 	sc->sc_rec.dma = p;
1372 
1373 	/* */
1374 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
1375 		autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
1376 		TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
1377 	}
1378 
1379 #if 0
1380 	/* 4DWAVE only allows capturing at a 48KHz rate */
1381 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
1382 	    sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
1383 		param->sample_rate = 48000;
1384 #endif
1385 
1386 	autri_setup_channel(sc, AUMODE_RECORD, param);
1387 
1388 	/* enable interrupt */
1389 	autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
1390 
1391 	/* start channel */
1392 	autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1393 
1394 	return 0;
1395 }
1396 
1397 #if 0
1398 static int
1399 autri_halt(struct autri_softc *sc)
1400 {
1401 	DPRINTF(("autri_halt().\n"));
1402 	/*autri_stopch(sc);*/
1403 	autri_disable_interrupt(sc, sc->sc_play.channel);
1404 	autri_disable_interrupt(sc, sc->sc_rec.channel);
1405 	return 0;
1406 }
1407 #endif
1408 
1409 static void
1410 autri_enable_interrupt(struct autri_softc *sc, int ch)
1411 {
1412 	int reg;
1413 
1414 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1415 	ch &= 0x1f;
1416 
1417 	autri_reg_set_4(sc, reg, 1 << ch);
1418 }
1419 
1420 static void
1421 autri_disable_interrupt(struct autri_softc *sc, int ch)
1422 {
1423 	int reg;
1424 
1425 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1426 	ch &= 0x1f;
1427 
1428 	autri_reg_clear_4(sc, reg, 1 << ch);
1429 }
1430 
1431 static void
1432 autri_startch(struct autri_softc *sc, int ch, int ch_intr)
1433 {
1434 	int reg;
1435 	u_int32_t chmask;
1436 
1437 	reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
1438 	ch &= 0x1f;
1439 	chmask = (1 << ch) | (1 << ch_intr);
1440 
1441 	autri_reg_set_4(sc, reg, chmask);
1442 }
1443 
1444 static void
1445 autri_stopch(struct autri_softc *sc, int ch, int ch_intr)
1446 {
1447 	int reg;
1448 	u_int32_t chmask;
1449 
1450 	reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
1451 	ch &= 0x1f;
1452 	chmask = (1 << ch) | (1 << ch_intr);
1453 
1454 	autri_reg_set_4(sc, reg, chmask);
1455 }
1456 
1457 #if NMIDI > 0
1458 int
1459 autri_midi_open(void *addr, int flags, void (*iintr)(void *, int),
1460 		void (*ointr)(void *), void *arg)
1461 {
1462 	struct autri_softc *sc = addr;
1463 
1464 	DPRINTF(("autri_midi_open()\n"));
1465 
1466 	DPRINTFN(5,("MPUR1 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR1)));
1467 	DPRINTFN(5,("MPUR2 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR2)));
1468 
1469 	sc->sc_iintr = iintr;
1470 	sc->sc_ointr = ointr;
1471 	sc->sc_arg = arg;
1472 
1473 	if (flags & FREAD)
1474 		autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
1475 
1476 	if (flags & FWRITE)
1477 		autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
1478 
1479 	return (0);
1480 }
1481 
1482 void
1483 autri_midi_close(void *addr)
1484 {
1485 	struct autri_softc *sc = addr;
1486 
1487 	DPRINTF(("autri_midi_close()\n"));
1488 
1489 	tsleep(sc, PWAIT, "autri", hz/10); /* give uart a chance to drain */
1490 
1491 	sc->sc_iintr = NULL;
1492 	sc->sc_ointr = NULL;
1493 }
1494 
1495 int
1496 autri_midi_output(void *addr, int d)
1497 {
1498 	struct autri_softc *sc = addr;
1499 	int x;
1500 
1501 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1502 		if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
1503 			TWRITE1(sc, AUTRI_MPUR0, d);
1504 			return (0);
1505 		}
1506 		delay(MIDI_BUSY_DELAY);
1507 	}
1508 	return (EIO);
1509 }
1510 
1511 void
1512 autri_midi_getinfo(void *addr, struct midi_info *mi)
1513 {
1514 	mi->name = "4DWAVE MIDI UART";
1515 	mi->props = MIDI_PROP_CAN_INPUT;
1516 }
1517 
1518 #endif
1519