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