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