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