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