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