xref: /openbsd-src/sys/dev/pci/autri.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: autri.c,v 1.21 2008/04/21 00:32:42 jakemsr 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 memory 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 
1016 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
1017 		    (p->precision != 8 && p->precision != 16) ||
1018 		    (p->channels != 1 && p->channels != 2))
1019 			return (EINVAL);
1020 
1021 		p->factor = 1;
1022 		p->sw_code = 0;
1023 		switch (p->encoding) {
1024 		case AUDIO_ENCODING_SLINEAR_BE:
1025 		case AUDIO_ENCODING_ULINEAR_BE:
1026 			if (p->precision == 16)
1027 				p->sw_code = swap_bytes;
1028 			break;
1029 		case AUDIO_ENCODING_SLINEAR_LE:
1030 		case AUDIO_ENCODING_ULINEAR_LE:
1031 			break;
1032 		case AUDIO_ENCODING_ULAW:
1033 			if (mode == AUMODE_PLAY)
1034 				p->sw_code = mulaw_to_ulinear8;
1035 			else
1036 				p->sw_code = ulinear8_to_mulaw;
1037 
1038 			break;
1039 		case AUDIO_ENCODING_ALAW:
1040 			if (mode == AUMODE_PLAY)
1041 				p->sw_code = alaw_to_ulinear8;
1042 			else
1043 				p->sw_code = ulinear8_to_alaw;
1044 
1045 			break;
1046 		default:
1047 			return (EINVAL);
1048 		}
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 int
1055 autri_round_blocksize(addr, block)
1056 	void *addr;
1057 	int block;
1058 {
1059 	return ((block + 3) & -4);
1060 }
1061 
1062 int
1063 autri_halt_output(addr)
1064 	void *addr;
1065 {
1066 	struct autri_softc *sc = addr;
1067 
1068 	DPRINTF(("autri_halt_output()\n"));
1069 
1070 	sc->sc_play.intr = NULL;
1071 	autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1072 	autri_disable_interrupt(sc, sc->sc_play.ch_intr);
1073 
1074 	return 0;
1075 }
1076 
1077 int
1078 autri_halt_input(addr)
1079 	void *addr;
1080 {
1081 	struct autri_softc *sc = addr;
1082 
1083 	DPRINTF(("autri_halt_input()\n"));
1084 
1085 	sc->sc_rec.intr = NULL;
1086 	autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1087 	autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
1088 
1089 	return 0;
1090 }
1091 
1092 int
1093 autri_getdev(addr, retp)
1094 	void *addr;
1095 	struct audio_device *retp;
1096 {
1097 	struct autri_softc *sc = addr;
1098 
1099 	DPRINTF(("autri_getdev().\n"));
1100 
1101 	strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
1102 	snprintf(retp->version, sizeof(retp->version), "0x%02x",
1103 	    PCI_REVISION(sc->sc_class));
1104 
1105 	switch (sc->sc_devid) {
1106 	case AUTRI_DEVICE_ID_4DWAVE_DX:
1107 		strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
1108 		break;
1109 	case AUTRI_DEVICE_ID_4DWAVE_NX:
1110 		strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
1111 		break;
1112 	case AUTRI_DEVICE_ID_SIS_7018:
1113 		strncpy(retp->config, "SiS 7018", sizeof(retp->config));
1114 		break;
1115 	case AUTRI_DEVICE_ID_ALI_M5451:
1116 		strncpy(retp->config, "ALi M5451", sizeof(retp->config));
1117 		break;
1118 	default:
1119 		strncpy(retp->config, "unknown", sizeof(retp->config));
1120 	}
1121 
1122 	return 0;
1123 }
1124 
1125 int
1126 autri_mixer_set_port(addr, cp)
1127 	void *addr;
1128 	mixer_ctrl_t *cp;
1129 {
1130 	struct autri_softc *sc = addr;
1131 
1132 	return (sc->sc_codec.codec_if->vtbl->mixer_set_port(
1133 	    sc->sc_codec.codec_if, cp));
1134 }
1135 
1136 int
1137 autri_mixer_get_port(addr, cp)
1138 	void *addr;
1139 	mixer_ctrl_t *cp;
1140 {
1141 	struct autri_softc *sc = addr;
1142 
1143 	return (sc->sc_codec.codec_if->vtbl->mixer_get_port(
1144 	    sc->sc_codec.codec_if, cp));
1145 }
1146 
1147 int
1148 autri_query_devinfo(addr, dip)
1149 	void *addr;
1150 	mixer_devinfo_t *dip;
1151 {
1152 	struct autri_softc *sc = addr;
1153 
1154 	return (sc->sc_codec.codec_if->vtbl->query_devinfo(
1155 	    sc->sc_codec.codec_if, dip));
1156 }
1157 
1158 int
1159 autri_get_portnum_by_name(sc, class, device, qualifier)
1160 	struct autri_softc *sc;
1161 	char *class, *device, *qualifier;
1162 {
1163 	return (sc->sc_codec.codec_if->vtbl->get_portnum_by_name(
1164 	    sc->sc_codec.codec_if, class, device, qualifier));
1165 }
1166 
1167 void *
1168 autri_malloc(addr, direction, size, pool, flags)
1169 	void *addr;
1170 	int direction;
1171 	size_t size;
1172 	int pool, flags;
1173 {
1174 	struct autri_softc *sc = addr;
1175 	struct autri_dma *p;
1176 	int error;
1177 
1178 	p = malloc(sizeof(*p), pool, flags);
1179 	if (!p)
1180 		return NULL;
1181 
1182 #if 0
1183 	error = autri_allocmem(sc, size, 16, p);
1184 #endif
1185 	error = autri_allocmem(sc, size, 0x10000, p);
1186 	if (error) {
1187 		free(p, pool);
1188 		return NULL;
1189 	}
1190 
1191 	p->next = sc->sc_dmas;
1192 	sc->sc_dmas = p;
1193 	return KERNADDR(p);
1194 }
1195 
1196 void
1197 autri_free(addr, ptr, pool)
1198 	void *addr;
1199 	void *ptr;
1200 	int pool;
1201 {
1202 	struct autri_softc *sc = addr;
1203 	struct autri_dma **pp, *p;
1204 
1205 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1206 		if (KERNADDR(p) == ptr) {
1207 			autri_freemem(sc, p);
1208 			*pp = p->next;
1209 			free(p, pool);
1210 			return;
1211 		}
1212 	}
1213 }
1214 
1215 struct autri_dma *
1216 autri_find_dma(sc, addr)
1217 	struct autri_softc *sc;
1218 	void *addr;
1219 {
1220 	struct autri_dma *p;
1221 
1222 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1223 		;
1224 
1225 	return p;
1226 }
1227 
1228 paddr_t
1229 autri_mappage(addr, mem, off, prot)
1230 	void *addr;
1231 	void *mem;
1232 	off_t off;
1233 	int prot;
1234 {
1235 	struct autri_softc *sc = addr;
1236 	struct autri_dma *p;
1237 
1238 	if (off < 0)
1239 		return (-1);
1240 
1241 	p = autri_find_dma(sc, mem);
1242 	if (!p)
1243 		return (-1);
1244 
1245 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1246 	    off, prot, BUS_DMA_WAITOK));
1247 }
1248 
1249 int
1250 autri_get_props(addr)
1251 	void *addr;
1252 {
1253 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1254 		AUDIO_PROP_FULLDUPLEX);
1255 }
1256 
1257 void
1258 autri_setup_channel(sc, mode, param)
1259 	struct autri_softc *sc;
1260 	int mode;
1261 	struct audio_params *param;
1262 {
1263 	int i, ch, channel;
1264 	u_int32_t reg, cr[5];
1265 	u_int32_t cso, eso;
1266 	u_int32_t delta, dch[2], ctrl;
1267 	u_int32_t alpha_fms, fm_vol, attribute;
1268 
1269 	u_int32_t dmaaddr, dmalen;
1270 	int factor, rvol, cvol;
1271 	struct autri_chstatus *chst;
1272 
1273 	ctrl = AUTRI_CTRL_LOOPMODE;
1274 	switch (param->encoding) {
1275 	case AUDIO_ENCODING_SLINEAR_BE:
1276 	case AUDIO_ENCODING_SLINEAR_LE:
1277 		ctrl |= AUTRI_CTRL_SIGNED;
1278 		break;
1279 	}
1280 
1281 	factor = 0;
1282 	if (param->precision == 16) {
1283 		ctrl |= AUTRI_CTRL_16BIT;
1284 		factor++;
1285 	}
1286 
1287 	if (param->channels == 2) {
1288 		ctrl |= AUTRI_CTRL_STEREO;
1289 		factor++;
1290 	}
1291 
1292 	delta = (u_int32_t)param->sample_rate;
1293 	if (delta < 4000)
1294 		delta = 4000;
1295 	if (delta > 48000)
1296 		delta = 48000;
1297 
1298 	attribute = 0;
1299 
1300 	dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
1301 	if (mode == AUMODE_PLAY) {
1302 		chst = &sc->sc_play;
1303 		dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
1304 		ctrl |= AUTRI_CTRL_WAVEVOL;
1305 /*
1306 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451)
1307 			ctrl |= 0x80000000;
1308 */
1309 	} else {
1310 		chst = &sc->sc_rec;
1311 		dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
1312 		if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018) {
1313 			ctrl |= AUTRI_CTRL_MUTE_SIS;
1314 			attribute = AUTRI_ATTR_PCMREC_SIS;
1315 			if (delta != 48000)
1316 				attribute |= AUTRI_ATTR_ENASRC_SIS;
1317 		}
1318 		ctrl |= AUTRI_CTRL_MUTE;
1319 	}
1320 
1321 	dmaaddr = DMAADDR(chst->dma);
1322 	cso = alpha_fms = 0;
1323 	rvol = cvol = 0x7f;
1324 	fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
1325 
1326 	for (ch=0; ch<2; ch++) {
1327 
1328 		if (ch == 0)
1329 			dmalen = (chst->length >> factor);
1330 		else {
1331 			/* channel for interrupt */
1332 			dmalen = (chst->blksize >> factor);
1333 			if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
1334 				ctrl |= AUTRI_CTRL_MUTE_SIS;
1335 			else
1336 				ctrl |= AUTRI_CTRL_MUTE;
1337 			attribute = 0;
1338 		}
1339 
1340 		eso = dmalen - 1;
1341 
1342 		switch (sc->sc_devid) {
1343 		case AUTRI_DEVICE_ID_4DWAVE_DX:
1344 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1345 			cr[1] = dmaaddr;
1346 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1347 			cr[3] = fm_vol;
1348 			cr[4] = ctrl;
1349 			break;
1350 		case AUTRI_DEVICE_ID_4DWAVE_NX:
1351 			cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
1352 			cr[1] = dmaaddr;
1353 			cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
1354 			cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
1355 			cr[4] = ctrl;
1356 			break;
1357 		case AUTRI_DEVICE_ID_SIS_7018:
1358 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1359 			cr[1] = dmaaddr;
1360 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1361 			cr[3] = attribute;
1362 			cr[4] = ctrl;
1363 			break;
1364 		case AUTRI_DEVICE_ID_ALI_M5451:
1365 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
1366 			cr[1] = dmaaddr;
1367 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
1368 			cr[3] = 0;
1369 			cr[4] = ctrl;
1370 			break;
1371 		}
1372 
1373 		/* write channel data */
1374 		channel = (ch == 0) ? chst->ch : chst->ch_intr;
1375 
1376 		reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
1377 		TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
1378 
1379 		for (i=0; i<5; i++) {
1380 			TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
1381 			DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
1382 		}
1383 
1384 		/* Bank A only */
1385 		if (channel < 0x20) {
1386 			TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
1387 			TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
1388 		}
1389 	}
1390 
1391 }
1392 
1393 int
1394 autri_trigger_output(addr, start, end, blksize, intr, arg, param)
1395 	void *addr;
1396 	void *start, *end;
1397 	int blksize;
1398 	void (*intr)(void *);
1399 	void *arg;
1400 	struct audio_params *param;
1401 {
1402 	struct autri_softc *sc = addr;
1403 	struct autri_dma *p;
1404 
1405 	DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
1406 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1407 
1408 	sc->sc_play.intr = intr;
1409 	sc->sc_play.intr_arg = arg;
1410 	sc->sc_play.offset = 0;
1411 	sc->sc_play.blksize = blksize;
1412 	sc->sc_play.length = (char *)end - (char *)start;
1413 
1414 	p = autri_find_dma(sc, start);
1415 	if (!p) {
1416 		printf("autri_trigger_output: bad addr %p\n", start);
1417 		return (EINVAL);
1418 	}
1419 
1420 	sc->sc_play.dma = p;
1421 
1422 	/* */
1423 	autri_setup_channel(sc, AUMODE_PLAY, param);
1424 
1425 	/* volume set to no attenuation */
1426 	TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
1427 
1428 	/* enable interrupt */
1429 	autri_enable_interrupt(sc, sc->sc_play.ch_intr);
1430 
1431 	/* start channel */
1432 	autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
1433 
1434 	return 0;
1435 }
1436 
1437 int
1438 autri_trigger_input(addr, start, end, blksize, intr, arg, param)
1439 	void *addr;
1440 	void *start, *end;
1441 	int blksize;
1442 	void (*intr)(void *);
1443 	void *arg;
1444 	struct audio_params *param;
1445 {
1446 	struct autri_softc *sc = addr;
1447 	struct autri_dma *p;
1448 
1449 	DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
1450 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1451 
1452 	sc->sc_rec.intr = intr;
1453 	sc->sc_rec.intr_arg = arg;
1454 	sc->sc_rec.offset = 0;
1455 	sc->sc_rec.blksize = blksize;
1456 	sc->sc_rec.length = (char *)end - (char *)start;
1457 
1458 	/* */
1459 	p = autri_find_dma(sc, start);
1460 	if (!p) {
1461 		printf("autri_trigger_input: bad addr %p\n", start);
1462 		return (EINVAL);
1463 	}
1464 
1465 	sc->sc_rec.dma = p;
1466 
1467 	/* */
1468 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
1469 		autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
1470 		TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
1471 	}
1472 
1473 #if 0
1474 	/* 4DWAVE only allows capturing at a 48KHz rate */
1475 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
1476 	    sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
1477 		param->sample_rate = 48000;
1478 #endif
1479 
1480 	autri_setup_channel(sc, AUMODE_RECORD, param);
1481 
1482 	/* enable interrupt */
1483 	autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
1484 
1485 	/* start channel */
1486 	autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
1487 
1488 	return 0;
1489 }
1490 
1491 #if 0
1492 int
1493 autri_halt(sc)
1494 	struct autri_softc *sc;
1495 {
1496 	DPRINTF(("autri_halt().\n"));
1497 	/*autri_stopch(sc);*/
1498 	autri_disable_interrupt(sc, sc->sc_play.channel);
1499 	autri_disable_interrupt(sc, sc->sc_rec.channel);
1500 	return 0;
1501 }
1502 #endif
1503 
1504 void
1505 autri_enable_interrupt(sc, ch)
1506 	struct autri_softc *sc;
1507 	int ch;
1508 {
1509 	int reg;
1510 
1511 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1512 	ch &= 0x1f;
1513 
1514 	autri_reg_set_4(sc, reg, 1 << ch);
1515 }
1516 
1517 void
1518 autri_disable_interrupt(sc, ch)
1519 	struct autri_softc *sc;
1520 	int ch;
1521 {
1522 	int reg;
1523 
1524 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
1525 	ch &= 0x1f;
1526 
1527 	autri_reg_clear_4(sc, reg, 1 << ch);
1528 }
1529 
1530 void
1531 autri_startch(sc, ch, ch_intr)
1532 	struct autri_softc *sc;
1533 	int ch, ch_intr;
1534 {
1535 	int reg;
1536 	u_int32_t chmask;
1537 
1538 	reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
1539 	ch &= 0x1f;
1540 	chmask = (1 << ch) | (1 << ch_intr);
1541 
1542 	autri_reg_set_4(sc, reg, chmask);
1543 }
1544 
1545 void
1546 autri_stopch(sc, ch, ch_intr)
1547 	struct autri_softc *sc;
1548 	int ch, ch_intr;
1549 {
1550 	int reg;
1551 	u_int32_t chmask;
1552 
1553 	reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
1554 	ch &= 0x1f;
1555 	chmask = (1 << ch) | (1 << ch_intr);
1556 
1557 	autri_reg_set_4(sc, reg, chmask);
1558 }
1559 
1560 #if NMIDI > 0
1561 int
1562 autri_midi_open(void *addr, int flags,
1563 	void (*iintr)(void *, int),
1564 	void (*ointr)(void *),
1565 	void *arg)
1566 {
1567 	struct autri_softc *sc = addr;
1568 
1569 	DPRINTF(("autri_midi_open()\n"));
1570 
1571 	DPRINTFN(5,("MPUR1 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR1)));
1572 	DPRINTFN(5,("MPUR2 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR2)));
1573 
1574 	sc->sc_iintr = iintr;
1575 	sc->sc_ointr = ointr;
1576 	sc->sc_arg = arg;
1577 
1578 	if (flags & FREAD)
1579 		autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
1580 
1581 	if (flags & FWRITE)
1582 		autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
1583 
1584 	return (0);
1585 }
1586 
1587 void
1588 autri_midi_close(void *addr)
1589 {
1590 	struct autri_softc *sc = addr;
1591 
1592 	DPRINTF(("autri_midi_close()\n"));
1593 
1594 	tsleep(sc, PWAIT, "autri", hz/10); /* give uart a chance to drain */
1595 
1596 	sc->sc_iintr = NULL;
1597 	sc->sc_ointr = NULL;
1598 }
1599 
1600 int
1601 autri_midi_output(void *addr, int d)
1602 {
1603 	struct autri_softc *sc = addr;
1604 	int x;
1605 
1606 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1607 		if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
1608 			TWRITE1(sc, AUTRI_MPUR0, d);
1609 			return (0);
1610 		}
1611 		delay(MIDI_BUSY_DELAY);
1612 	}
1613 	return (EIO);
1614 }
1615 
1616 void
1617 autri_midi_getinfo(void *addr, struct midi_info *mi)
1618 {
1619 	mi->name = "4DWAVE MIDI UART";
1620 	mi->props = MIDI_PROP_CAN_INPUT;
1621 }
1622 
1623 #endif
1624