xref: /netbsd-src/sys/dev/ic/interwave.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: interwave.c,v 1.31 2007/10/19 11:59:54 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Author: Kari Mettinen
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: interwave.c,v 1.31 2007/10/19 11:59:54 ad Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/ioctl.h>
45 #include <sys/syslog.h>
46 #include <sys/device.h>
47 #include <sys/proc.h>
48 #include <sys/buf.h>
49 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 
53 #include <sys/cpu.h>
54 #include <sys/intr.h>
55 #include <machine/pio.h>
56 #include <sys/audioio.h>
57 #include <dev/audio_if.h>
58 #include <dev/mulaw.h>
59 
60 #include <dev/isa/isavar.h>
61 #include <dev/isa/isadmavar.h>
62 
63 #include <dev/ic/interwavereg.h>
64 #include <dev/ic/interwavevar.h>
65 
66 
67 static void iwreset(struct iw_softc *, int);
68 
69 static int iw_set_speed(struct iw_softc *, u_long, char);
70 static u_long iw_set_format(struct iw_softc *, u_long, int);
71 static void iw_mixer_line_level(struct iw_softc *, int, int, int);
72 static void iw_trigger_dma(struct iw_softc *, u_char);
73 static void iw_stop_dma(struct iw_softc *, u_char, u_char);
74 static void iw_dma_count(struct iw_softc *, u_short, int);
75 static int iwintr(void *);
76 static void iw_meminit(struct iw_softc *);
77 static void iw_mempoke(struct iw_softc *, u_long, u_char);
78 static u_char iw_mempeek(struct iw_softc *, u_long);
79 
80 #ifdef USE_WAVETABLE
81 static void iw_set_voice_place(struct iw_softc *, u_char, u_long);
82 static void iw_voice_pan(struct iw_softc *, u_char, u_short, u_short);
83 static void iw_voice_freq(struct iw_softc *, u_char, u_long);
84 static void iw_set_loopmode(struct iw_softc *, u_char, u_char, u_char);
85 static void iw_set_voice_pos(struct iw_softc *, u_short, u_long, u_long);
86 static void iw_start_voice(struct iw_softc *, u_char);
87 static void iw_play_voice(struct iw_softc *, u_long, u_long, u_short);
88 static void iw_stop_voice(struct iw_softc *, u_char);
89 static void iw_move_voice_end(struct iw_softc *, u_short, u_long);
90 static void iw_initvoices(struct iw_softc *);
91 #endif
92 
93 struct audio_device iw_device = {
94 	"Am78C201",
95 	"0.1",
96 	"guspnp"
97 };
98 
99 #ifdef AUDIO_DEBUG
100 int iw_debug;
101 #define DPRINTF(p)       if (iw_debug) printf p
102 #else
103 #define DPRINTF(p)
104 #endif
105 
106 static int      iw_cc = 1;
107 #ifdef DIAGNOSTIC
108 static int      outputs = 0;
109 static int      iw_ints = 0;
110 static int      inputs = 0;
111 static int      iw_inints = 0;
112 #endif
113 
114 int
115 iwintr(void *arg)
116 {
117 	struct	iw_softc *sc;
118 	int	val;
119 	u_char	intrs;
120 
121 	sc = arg;
122 	val = 0;
123 	intrs = 0;
124 	IW_READ_DIRECT_1(6, sc->p2xr_h, intrs);	/* UISR */
125 
126 	/* codec ints */
127 
128 	/*
129 	 * The proper order to do this seems to be to read CSR3 to get the
130 	 * int cause and fifo over underrrun status, then deal with the ints
131 	 * (new DMA set up), and to clear ints by writing the respective bit
132 	 * to 0.
133 	 */
134 
135 	/* read what ints happened */
136 
137 	IW_READ_CODEC_1(CSR3I, intrs);
138 
139 	/* clear them */
140 
141 	IW_WRITE_DIRECT_1(2, sc->codec_index_h, 0x00);
142 
143 	/* and process them */
144 
145 	if (intrs & 0x20) {
146 #ifdef DIAGNOSTIC
147 		iw_inints++;
148 #endif
149 		if (sc->sc_recintr != 0)
150 			sc->sc_recintr(sc->sc_recarg);
151 		val = 1;
152 	}
153 	if (intrs & 0x10) {
154 #ifdef DIAGNOSTIC
155 		iw_ints++;
156 #endif
157 		if (sc->sc_playintr != 0)
158 			sc->sc_playintr(sc->sc_playarg);
159 		val = 1;
160 	}
161 	return val;
162 
163 }
164 
165 void
166 iwattach(struct iw_softc *sc)
167 {
168 	int	got_irq;
169 
170 	DPRINTF(("iwattach sc %p\n", sc));
171 	got_irq = 0;
172 
173 	sc->cdatap = 1;		/* relative offsets in region */
174 	sc->csr1r = 2;
175 	sc->cxdr = 3;		/* CPDR or CRDR */
176 
177 	sc->gmxr = 0;		/* sc->p3xr */
178 	sc->gmxdr = 1;		/* GMTDR or GMRDR */
179 	sc->svsr = 2;
180 	sc->igidxr = 3;
181 	sc->i16dp = 4;
182 	sc->i8dp = 5;
183 	sc->lmbdr = 7;
184 
185 	sc->rec_precision = sc->play_precision = 8;
186 	sc->rec_channels = sc->play_channels = 1;
187 	sc->rec_encoding = sc->play_encoding = AUDIO_ENCODING_ULAW;
188 	sc->sc_irate = 8000;
189 	sc->sc_orate = 8000;
190 
191 	sc->sc_fullduplex = 1;
192 
193 	sc->sc_dma_flags = 0;
194 
195 	/*
196 	 * We can only use a few selected irqs, see if we got one from pnp
197 	 * code that suits us.
198 	 */
199 
200 	if (sc->sc_irq > 0) {
201 		sc->sc_ih = isa_intr_establish(sc->sc_p2xr_ic,
202 		    sc->sc_irq, IST_EDGE, IPL_AUDIO, iwintr, sc);
203 		got_irq = 1;
204 	}
205 	if (!got_irq) {
206 		printf("\niwattach: couldn't get a suitable irq\n");
207 		return;
208 	}
209 	printf("\n");
210 	iwreset(sc, 0);
211 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 0);
212 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 1);
213 	printf("%s: interwave version %s\n",
214 	    sc->sc_dev.dv_xname, iw_device.version);
215 	audio_attach_mi(sc->iw_hw_if, sc, &sc->sc_dev);
216 }
217 
218 int
219 iwopen(struct iw_softc *sc, int flags)
220 {
221 
222 	DPRINTF(("iwopen: sc %p\n", sc));
223 
224 #ifdef DIAGNOSTIC
225 	outputs = 0;
226 	iw_ints = 0;
227 	inputs = 0;
228 	iw_inints = 0;
229 #endif
230 
231 	iwreset(sc, 1);
232 
233 	return 0;
234 }
235 
236 void
237 iwclose(void *addr)
238 {
239 
240 	DPRINTF(("iwclose sc %p\n", addr));
241 #ifdef DIAGNOSTIC
242 	DPRINTF(("iwclose: outputs %d ints %d inputs %d in_ints %d\n",
243 		outputs, iw_ints, inputs, iw_inints));
244 #endif
245 }
246 
247 #define RAM_STEP	64*1024
248 
249 static void
250 iw_mempoke(struct iw_softc *sc, u_long addy, u_char val)
251 {
252 
253 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
254 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
255 
256 	/* Write byte to LMBDR */
257 	IW_WRITE_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, val);
258 }
259 
260 static u_char
261 iw_mempeek(struct iw_softc *sc, u_long addy)
262 {
263 	u_char	ret;
264 
265 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
266 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
267 
268 	IW_READ_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, ret);
269 	return ret;		/* return byte from LMBDR */
270 }
271 
272 static void
273 iw_meminit(struct iw_softc *sc)
274 {
275 	u_long	bank[4] = {0L, 0L, 0L, 0L};
276 	u_long	addr, base, cnt;
277 	u_char	i, ram /* ,memval=0 */ ;
278 	u_short	lmcfi;
279 	u_long	temppi;
280 	u_long	*lpbanks;
281 
282 	addr = 0L;
283 	base = 0L;
284 	cnt = 0L;
285 	ram = 0;
286 	lpbanks = &temppi;
287 
288 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
289 
290 	IW_READ_GENERAL_2(LMCFI, lmcfi);	/* 0x52 */
291 	lmcfi |= 0x0A0C;
292 	IW_WRITE_GENERAL_2(LMCFI, lmcfi);	/* max addr span */
293 	IW_WRITE_GENERAL_1(LMCI, 0x00);
294 
295 	/* fifo addresses */
296 
297 	IW_WRITE_GENERAL_2(LMRFAI, ((4 * 1024 * 1024) >> 8));
298 	IW_WRITE_GENERAL_2(LMPFAI, ((4 * 1024 * 1024 + 16 * 1024) >> 8));
299 
300 	IW_WRITE_GENERAL_2(LMFSI, 0x000);
301 
302 	IW_WRITE_GENERAL_2(LDICI, 0x0000);
303 
304 	while (addr < (16 * 1024 * 1024)) {
305 		iw_mempoke(sc, addr, 0x00);
306 		addr += RAM_STEP;
307 	}
308 
309 	printf("%s:", sc->sc_dev.dv_xname);
310 
311 	for (i = 0; i < 4; i++) {
312 		iw_mempoke(sc, base, 0xAA);	/* mark start of bank */
313 		iw_mempoke(sc, base + 1L, 0x55);
314 		if (iw_mempeek(sc, base) == 0xAA  &&
315 		    iw_mempeek(sc, base + 1L) == 0x55)
316 			ram = 1;
317 		if (ram) {
318 			while (cnt < (4 * 1024 * 1024)) {
319 				bank[i] += RAM_STEP;
320 				cnt += RAM_STEP;
321 				addr = base + cnt;
322 				if (iw_mempeek(sc, addr) == 0xAA)
323 					break;
324 			}
325 		}
326 		if (lpbanks != NULL) {
327 			*lpbanks = bank[i];
328 			lpbanks++;
329 		}
330 		bank[i] = bank[i] >> 10;
331 		printf("%s bank[%d]: %ldK", i ? "," : "", i, bank[i]);
332 		base += 4 * 1024 * 1024;
333 		cnt = 0L;
334 		ram = 0;
335 	}
336 
337 	printf("\n");
338 
339 	/*
340 	 * this is not really useful since GUS PnP supports memory
341 	 * configurations that aren't really supported by Interwave...beware
342 	 * of holes! Also, we don't use the memory for anything in this
343 	 * version of the driver.
344 	 *
345 	 * we've configured for 4M-4M-4M-4M
346 	 */
347 }
348 
349 static void
350 iwreset(struct iw_softc *sc, int warm)
351 {
352 	u_char	reg, cmode, val, mixer_image;
353 
354 	val = 0;
355 	mixer_image = 0;
356 	reg = 0;		/* XXX gcc -Wall */
357 
358 	cmode = 0x6c;		/* enhanced codec mode (full duplex) */
359 
360 	/* reset */
361 
362 	IW_WRITE_GENERAL_1(URSTI, 0x00);
363 	delay(10);
364 	IW_WRITE_GENERAL_1(URSTI, 0x07);
365 	IW_WRITE_GENERAL_1(ICMPTI, 0x1f);	/* disable DSP and uici and
366 						 * udci writes */
367 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* enable ints to ISA and
368 						 * codec access */
369 	IW_READ_GENERAL_1(IVERI, reg);
370 	IW_WRITE_GENERAL_1(IVERI, reg | 0x01);	/* hidden reg lock disable */
371 	IW_WRITE_GENERAL_1(UASBCI, 0x00);
372 
373 	/* synth enhanced mode (default), 0 active voices, disable ints */
374 
375 	IW_WRITE_GENERAL_1(SGMI_WR, 0x01);	/* enhanced mode, LFOs
376 						 * disabled */
377 	for (val = 0; val < 32; val++) {
378 		/* set each synth sound volume to 0 */
379 		IW_WRITE_DIRECT_1(sc->p3xr + 2, sc->p3xr_h, val);
380 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
381 		IW_WRITE_GENERAL_2(SASLI_WR, 0x0000);
382 		IW_WRITE_GENERAL_2(SASHI_WR, 0x0000);
383 		IW_WRITE_GENERAL_2(SAELI_WR, 0x0000);
384 		IW_WRITE_GENERAL_2(SAEHI_WR, 0x0000);
385 		IW_WRITE_GENERAL_2(SFCI_WR, 0x0000);
386 		IW_WRITE_GENERAL_1(SACI_WR, 0x02);
387 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
388 		IW_WRITE_GENERAL_1(SVEI_WR, 0x00);
389 		IW_WRITE_GENERAL_2(SVLI_WR, 0x0000);
390 		IW_WRITE_GENERAL_1(SVCI_WR, 0x02);
391 		IW_WRITE_GENERAL_1(SMSI_WR, 0x02);
392 	}
393 
394 	IW_WRITE_GENERAL_1(SAVI_WR, 0x00);
395 
396 	/* codec mode/init */
397 
398 	/* first change mode to 1 */
399 
400 	IW_WRITE_CODEC_1(CMODEI, 0x00);
401 
402 	/* and mode 3 */
403 
404 	IW_WRITE_CODEC_1(CMODEI, cmode);
405 
406 	IW_READ_CODEC_1(CMODEI, reg);
407 
408 	DPRINTF(("cmode %x\n", reg));
409 
410 	sc->revision = ((reg & 0x80) >> 3) | (reg & 0x0f);
411 
412 	IW_WRITE_DIRECT_1(sc->codec_index + 2, sc->p2xr_h, 0x00);
413 
414 	IW_WRITE_CODEC_1(CFIG1I | IW_MCE, 0x00);	/* DMA 2 chan access */
415 	IW_WRITE_CODEC_1(CEXTI, 0x00);	/* disable ints for now */
416 
417 
418 	IW_WRITE_CODEC_1(CLPCTI, 0x00);	/* reset playback sample counters */
419 	IW_WRITE_CODEC_1(CUPCTI, 0x00);	/* always upper byte last */
420 	IW_WRITE_CODEC_1(CFIG2I, 0x80);	/* full voltage range, enable record
421 					 * and playback sample counters, and
422 					 * don't center output in case or
423 					 * FIFO underrun */
424 	IW_WRITE_CODEC_1(CFIG3I, 0xc0);	/* enable record/playback irq (still
425 					 * turned off from CEXTI), max DMA
426 					 * rate */
427 	IW_WRITE_CODEC_1(CSR3I, 0x00);	/* clear status 3 reg */
428 
429 
430 	IW_WRITE_CODEC_1(CLRCTI, 0x00);	/* reset record sample counters */
431 	IW_WRITE_CODEC_1(CURCTI, 0x00);	/* always upper byte last */
432 
433 
434 	IW_READ_GENERAL_1(IVERI, reg);
435 
436 	sc->vers = reg >> 4;
437 	if (!warm)
438 		snprintf(iw_device.version, sizeof(iw_device.version), "%d.%d",
439 		    sc->vers, sc->revision);
440 
441 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* irqs and codec decode
442 						 * enable */
443 
444 
445 	/* ports */
446 
447 	if (!warm) {
448 		iw_mixer_line_level(sc, IW_LINE_OUT, 255, 255);
449 		iw_mixer_line_level(sc, IW_LINE_IN, 0, 0);
450 		iw_mixer_line_level(sc, IW_AUX1, 0, 0);
451 		iw_mixer_line_level(sc, IW_AUX2, 200, 200); /* CD */
452 		sc->sc_dac.off = 0;
453 		iw_mixer_line_level(sc, IW_DAC, 200, 200);
454 
455 		iw_mixer_line_level(sc, IW_MIC_IN, 0, 0);
456 		iw_mixer_line_level(sc, IW_REC, 0, 0);
457 		iw_mixer_line_level(sc, IW_LOOPBACK, 0, 0);
458 		iw_mixer_line_level(sc, IW_MONO_IN, 0, 0);
459 
460 		/* mem stuff */
461 		iw_meminit(sc);
462 
463 	}
464 	IW_WRITE_CODEC_1(CEXTI, 0x02);	/* codec int enable */
465 
466 	/* clear _LDMACI */
467 
468 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
469 
470 	/* enable mixer paths */
471 	mixer_image = 0x0c;
472 	IW_WRITE_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
473 	/*
474 	 * enable output, line in. disable mic in bit 0 = 0 -> line in on
475 	 * (from codec?) bit 1 = 0 -> output on bit 2 = 1 -> mic in on bit 3
476 	 * = 1 -> irq&drq pin enable bit 4 = 1 -> channel interrupts to chan
477 	 * 1 bit 5 = 1 -> enable midi loop back bit 6 = 0 -> irq latches
478 	 * URCR[2:0] bit 6 = 1 -> DMA latches URCR[2:0]
479 	 */
480 
481 
482 	IW_READ_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
483 #ifdef AUDIO_DEBUG
484 	if (!warm)
485 		DPRINTF(("mix image %x \n", mixer_image));
486 #endif
487 }
488 
489 struct iw_codec_freq {
490 	u_long	freq;
491 	u_char	bits;
492 };
493 
494 int
495 iw_set_speed(struct iw_softc *sc, u_long freq, char in)
496 {
497 	u_char	var, cfig3, reg;
498 
499 	static struct iw_codec_freq iw_cf[17] = {
500 #define FREQ_1 24576000
501 #define FREQ_2 16934400
502 #define XTAL1 0
503 #define XTAL2 1
504 		{5510, 0x00 | XTAL2}, {6620, 0x0E | XTAL2},
505 		{8000, 0x00 | XTAL1}, {9600, 0x0E | XTAL1},
506 		{11025, 0x02 | XTAL2}, {16000, 0x02 | XTAL1},
507 		{18900, 0x04 | XTAL2}, {22050, 0x06 | XTAL2},
508 		{27420, 0x04 | XTAL1}, {32000, 0x06 | XTAL1},
509 		{33075, 0x0C | XTAL2}, {37800, 0x08 | XTAL2},
510 		{38400, 0x0A | XTAL1}, {44100, 0x0A | XTAL2},
511 		{44800, 0x08 | XTAL1}, {48000, 0x0C | XTAL1},
512 		{48000, 0x0C | XTAL1}	/* really a dummy for indexing later */
513 #undef XTAL1
514 #undef XTAL2
515 	};
516 
517 	cfig3 = 0;		/* XXX gcc -Wall */
518 
519 	/*
520 	 * if the frequency is between 3493 Hz and 32 kHz we can use a more
521 	 * accurate frequency than the ones listed above base on the formula
522 	 * FREQ/((16*(48+x))) where FREQ is either FREQ_1 (24576000Hz) or
523 	 * FREQ_2 (16934400Hz) and x is the value to be written to either
524 	 * CPVFI or CRVFI. To enable this option, bit 2 in CFIG3 needs to be
525 	 * set high
526 	 *
527 	 * NOT IMPLEMENTED!
528 	 *
529 	 * Note that if you have a 'bad' XTAL_1 (higher than 18.5 MHz), 44.8 kHz
530 	 * and 38.4 kHz modes will provide wrong frequencies to output.
531 	 */
532 
533 
534 	if (freq > 48000)
535 		freq = 48000;
536 	if (freq < 5510)
537 		freq = 5510;
538 
539 	/* reset CFIG3[2] */
540 
541 	IW_READ_CODEC_1(CFIG3I, cfig3);
542 
543 	cfig3 |= 0xc0;		/* not full fifo treshhold */
544 
545 	DPRINTF(("cfig3i = %x -> ", cfig3));
546 
547 	cfig3 &= ~0x04;
548 	IW_WRITE_CODEC_1(CFIG3I, cfig3);
549 	IW_READ_CODEC_1(CFIG3I, cfig3);
550 
551 	DPRINTF(("%x\n", cfig3));
552 
553 	for (var = 0; var < 16; var++)	/* select closest frequency */
554 		if (freq <= iw_cf[var].freq)
555 			break;
556 	if (var != 16)
557 		if (abs(freq - iw_cf[var].freq) > abs(iw_cf[var + 1].freq - freq))
558 			var++;
559 
560 	if (in)
561 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, sc->recfmtbits | iw_cf[var].bits);
562 	else
563 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, sc->playfmtbits | iw_cf[var].bits);
564 	freq = iw_cf[var].freq;
565 	DPRINTF(("setting %s frequency to %d bits %x \n",
566 	       in ? "in" : "out", (int) freq, iw_cf[var].bits));
567 
568 	IW_READ_CODEC_1(CPDFI, reg);
569 
570 	DPRINTF((" CPDFI %x ", reg));
571 
572 	IW_READ_CODEC_1(CRDFI, reg);
573 
574 	DPRINTF((" CRDFI %x ", reg));
575 
576 	return freq;
577 }
578 
579 /* Encoding. */
580 int
581 iw_query_encoding(void *addr, audio_encoding_t *fp)
582 {
583 	/*
584 	 * LINEAR, ALAW, ULAW, ADPCM in HW, we'll use linear unsigned
585 	 * hardware mode for all 8-bit modes due to buggy (?) codec.
586 	 */
587 
588 	/*
589 	 * except in wavetable synth. there we have only mu-law and 8 and 16
590 	 * bit linear data
591 	 */
592 
593 	switch (fp->index) {
594 	case 0:
595 		strcpy(fp->name, AudioEulinear);
596 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
597 		fp->precision = 8;
598 		fp->flags = 0;
599 		break;
600 	case 1:
601 		strcpy(fp->name, AudioEmulaw);
602 		fp->encoding = AUDIO_ENCODING_ULAW;
603 		fp->precision = 8;
604 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
605 		break;
606 	case 2:
607 		strcpy(fp->name, AudioEalaw);
608 		fp->encoding = AUDIO_ENCODING_ALAW;
609 		fp->precision = 8;
610 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
611 		break;
612 	case 3:
613 		strcpy(fp->name, AudioEadpcm);
614 		fp->encoding = AUDIO_ENCODING_ADPCM;
615 		fp->precision = 8;	/* really 4 bit */
616 		fp->flags = 0;
617 		break;
618 	case 4:
619 		strcpy(fp->name, AudioEslinear_le);
620 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
621 		fp->precision = 16;
622 		fp->flags = 0;
623 		break;
624 	case 5:
625 		strcpy(fp->name, AudioEslinear_be);
626 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
627 		fp->precision = 16;
628 		fp->flags = 0;
629 		break;
630 	default:
631 		return EINVAL;
632 		/* NOTREACHED */
633 	}
634 	return 0;
635 }
636 
637 u_long
638 iw_set_format(struct iw_softc *sc, u_long precision, int in)
639 {
640 	u_char	data;
641 	int	encoding, channels;
642 
643 	encoding = in ? sc->rec_encoding : sc->play_encoding;
644 	channels = in ? sc->rec_channels : sc->play_channels;
645 
646 	DPRINTF(("iw_set_format\n"));
647 
648 	switch (encoding) {
649 	case AUDIO_ENCODING_ULAW:
650 		data = 0x00;
651 		break;
652 
653 	case AUDIO_ENCODING_ALAW:
654 		data = 0x00;
655 		break;
656 
657 	case AUDIO_ENCODING_SLINEAR_LE:
658 		if (precision == 16)
659 			data = 0x40;	/* little endian. 0xc0 is big endian */
660 		else
661 			data = 0x00;
662 		break;
663 
664 	case AUDIO_ENCODING_SLINEAR_BE:
665 		if (precision == 16)
666 			data = 0xc0;
667 		else
668 			data = 0x00;
669 		break;
670 
671 	case AUDIO_ENCODING_ADPCM:
672 		data = 0xa0;
673 		break;
674 
675 	default:
676 		return -1;
677 	}
678 
679 	if (channels == 2)
680 		data |= 0x10;	/* stereo */
681 
682 	if (in) {
683 		/* in */
684 		sc->recfmtbits = data;
685 		/* This will zero the normal codec frequency,
686 		 * iw_set_speed should always be called afterwards.
687 		 */
688 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, data);
689 	} else {
690 		/* out */
691 		sc->playfmtbits = data;
692 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, data);
693 	}
694 
695 	DPRINTF(("formatbits %s %x", in ? "in" : "out", data));
696 
697 	return encoding;
698 }
699 
700 int
701 iw_set_params(void *addr, int setmode, int usemode, audio_params_t *p,
702     audio_params_t *q, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
703 {
704 	audio_params_t phw, rhw;
705 	struct iw_softc *sc;
706 	stream_filter_factory_t *swcode;
707 
708 	DPRINTF(("iw_setparams: code %u, prec %u, rate %u, chan %u\n",
709 	    p->encoding, p->precision, p->sample_rate, p->channels));
710 	sc = addr;
711 	swcode = NULL;
712 	phw = *p;
713 	rhw = *q;
714 	switch (p->encoding) {
715 	case AUDIO_ENCODING_ULAW:
716 		if (p->precision != 8)
717 			return EINVAL;
718 		phw.encoding = AUDIO_ENCODING_ULINEAR_LE;
719 		rhw.encoding = AUDIO_ENCODING_ULINEAR_LE;
720 		swcode = setmode & AUMODE_PLAY ? mulaw_to_linear8 : linear8_to_mulaw;
721 		break;
722 	case AUDIO_ENCODING_ALAW:
723 		if (p->precision != 8)
724 			return EINVAL;
725 		phw.encoding = AUDIO_ENCODING_ULINEAR_LE;
726 		rhw.encoding = AUDIO_ENCODING_ULINEAR_LE;
727 		swcode = setmode & AUMODE_PLAY ? alaw_to_linear8 : linear8_to_alaw;
728 		break;
729 	case AUDIO_ENCODING_ADPCM:
730 		if (p->precision != 8)
731 			return EINVAL;
732 		else
733 			break;
734 
735 	case AUDIO_ENCODING_SLINEAR_LE:
736 	case AUDIO_ENCODING_SLINEAR_BE:
737 		if (p->precision != 8 && p->precision != 16)
738 			return EINVAL;
739 		else
740 			break;
741 
742 	default:
743 		return EINVAL;
744 
745 	}
746 
747 	if (setmode & AUMODE_PLAY) {
748 		sc->play_channels = p->channels;
749 		sc->play_encoding = p->encoding;
750 		sc->play_precision = p->precision;
751 		iw_set_format(sc, p->precision, 0);
752 		q->sample_rate = p->sample_rate = sc->sc_orate =
753 			iw_set_speed(sc, p->sample_rate, 0);
754 		if (swcode != NULL) {
755 			phw.sample_rate = p->sample_rate;
756 			pfil->append(pfil, swcode, &phw);
757 		}
758 	} else {
759 #if 0
760 		q->channels = sc->rec_channels = p->channels;
761 		q->encoding = sc->rec_encoding = p->encoding;
762 		q->precision = sc->rec_precision = p->precision;
763 #endif
764 		sc->rec_channels = q->channels;
765 		sc->rec_encoding = q->encoding;
766 		sc->rec_precision = q->precision;
767 
768 		iw_set_format(sc, p->precision, 1);
769 		q->sample_rate = sc->sc_irate =
770 			iw_set_speed(sc, q->sample_rate, 1);
771 		if (swcode != NULL) {
772 			rhw.sample_rate = q->sample_rate;
773 			rfil->append(rfil, swcode, &rhw);
774 		}
775 	}
776 	return 0;
777 }
778 
779 
780 int
781 iw_round_blocksize(void *addr, int blk, int mode,
782     const audio_params_t *param)
783 {
784 
785 	/* Round to a multiple of the biggest sample size. */
786 	return blk &= -4;
787 }
788 
789 void
790 iw_mixer_line_level(struct iw_softc *sc, int line, int levl, int levr)
791 {
792 	u_char	gainl, gainr, attenl, attenr;
793 
794 	switch (line) {
795 	case IW_REC:
796 		gainl = sc->sc_recsrcbits | (levl >> 4);
797 		gainr = sc->sc_recsrcbits | (levr >> 4);
798 		DPRINTF(("recording with %x", gainl));
799 		IW_WRITE_CODEC_1(CLICI, gainl);
800 		IW_WRITE_CODEC_1(CRICI, gainr);
801 		sc->sc_rec.voll = levl & 0xf0;
802 		sc->sc_rec.volr = levr & 0xf0;
803 		break;
804 
805 	case IW_AUX1:
806 
807 		gainl = (255 - levl) >> 3;
808 		gainr = (255 - levr) >> 3;
809 
810 		/* mute if 0 level */
811 		if (levl == 0)
812 			gainl |= 0x80;
813 		if (levr == 0)
814 			gainr |= 0x80;
815 
816 		IW_WRITE_CODEC_1(IW_LEFT_AUX1_PORT, gainl);
817 		IW_WRITE_CODEC_1(IW_RIGHT_AUX1_PORT, gainr);
818 		sc->sc_aux1.voll = levl & 0xf8;
819 		sc->sc_aux1.volr = levr & 0xf8;
820 
821 		break;
822 
823 	case IW_AUX2:
824 
825 		gainl = (255 - levl) >> 3;
826 		gainr = (255 - levr) >> 3;
827 
828 		/* mute if 0 level */
829 		if (levl == 0)
830 			gainl |= 0x80;
831 		if (levr == 0)
832 			gainr |= 0x80;
833 
834 		IW_WRITE_CODEC_1(IW_LEFT_AUX2_PORT, gainl);
835 		IW_WRITE_CODEC_1(IW_RIGHT_AUX2_PORT, gainr);
836 		sc->sc_aux2.voll = levl & 0xf8;
837 		sc->sc_aux2.volr = levr & 0xf8;
838 		break;
839 	case IW_DAC:
840 		attenl = ((255 - levl) >> 2) | ((levl && !sc->sc_dac.off) ? 0 : 0x80);
841 		attenr = ((255 - levr) >> 2) | ((levr && !sc->sc_dac.off) ? 0 : 0x80);
842 		IW_WRITE_CODEC_1(CLDACI, attenl);
843 		IW_WRITE_CODEC_1(CRDACI, attenr);
844 		sc->sc_dac.voll = levl & 0xfc;
845 		sc->sc_dac.volr = levr & 0xfc;
846 		break;
847 	case IW_LOOPBACK:
848 		attenl = ((255 - levl) & 0xfc) | (levl ? 0x01 : 0);
849 		IW_WRITE_CODEC_1(CLCI, attenl);
850 		sc->sc_loopback.voll = levl & 0xfc;
851 		break;
852 	case IW_LINE_IN:
853 		gainl = (levl >> 3) | (levl ? 0 : 0x80);
854 		gainr = (levr >> 3) | (levr ? 0 : 0x80);
855 		IW_WRITE_CODEC_1(CLLICI, gainl);
856 		IW_WRITE_CODEC_1(CRLICI, gainr);
857 		sc->sc_linein.voll = levl & 0xf8;
858 		sc->sc_linein.volr = levr & 0xf8;
859 		break;
860 	case IW_MIC_IN:
861 		gainl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
862 		gainr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
863 		IW_WRITE_CODEC_1(CLMICI, gainl);
864 		IW_WRITE_CODEC_1(CRMICI, gainr);
865 		sc->sc_mic.voll = levl & 0xf8;
866 		sc->sc_mic.volr = levr & 0xf8;
867 		break;
868 	case IW_LINE_OUT:
869 		attenl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
870 		attenr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
871 		IW_WRITE_CODEC_1(CLOAI, attenl);
872 		IW_WRITE_CODEC_1(CROAI, attenr);
873 		sc->sc_lineout.voll = levl & 0xf8;
874 		sc->sc_lineout.volr = levr & 0xf8;
875 		break;
876 	case IW_MONO_IN:
877 		attenl = ((255 - levl) >> 4) | (levl ? 0 : 0xc0);	/* in/out mute */
878 		IW_WRITE_CODEC_1(CMONOI, attenl);
879 		sc->sc_monoin.voll = levl & 0xf0;
880 		break;
881 	}
882 }
883 
884 int
885 iw_commit_settings(void *addr)
886 {
887 
888 	return 0;
889 }
890 
891 void
892 iw_trigger_dma(struct iw_softc *sc, u_char io)
893 {
894 	u_char	reg;
895 	int	s;
896 
897 	s = splaudio();
898 
899 	IW_READ_CODEC_1(CSR3I, reg);
900 	IW_WRITE_CODEC_1(CSR3I, reg & ~(io == IW_DMA_PLAYBACK ? 0x10 : 0x20));
901 
902 	IW_READ_CODEC_1(CFIG1I, reg);
903 
904 	IW_WRITE_CODEC_1(CFIG1I, reg | io);
905 
906 	/* let the counter run */
907 	IW_READ_CODEC_1(CFIG2I, reg);
908 	IW_WRITE_CODEC_1(CFIG2I, reg & ~(io << 4));
909 
910 	splx(s);
911 }
912 
913 void
914 iw_stop_dma(struct iw_softc *sc, u_char io, u_char hard)
915 {
916 	u_char	reg;
917 
918 	/* just stop the counter, no need to flush the fifo */
919 	IW_READ_CODEC_1(CFIG2I, reg);
920 	IW_WRITE_CODEC_1(CFIG2I, (reg | (io << 4)));
921 
922 	if (hard) {
923 		/* unless we're closing the device */
924 		IW_READ_CODEC_1(CFIG1I, reg);
925 		IW_WRITE_CODEC_1(CFIG1I, reg & ~io);
926 	}
927 }
928 
929 void
930 iw_dma_count(struct iw_softc *sc, u_short count, int io)
931 {
932 
933 	if (io == IW_DMA_PLAYBACK) {
934 		IW_WRITE_CODEC_1(CLPCTI, (u_char) (count & 0x00ff));
935 		IW_WRITE_CODEC_1(CUPCTI, (u_char) ((count >> 8) & 0x00ff));
936 	} else {
937 		IW_WRITE_CODEC_1(CLRCTI, (u_char) (count & 0x00ff));
938 		IW_WRITE_CODEC_1(CURCTI, (u_char) ((count >> 8) & 0x00ff));
939 	}
940 }
941 
942 int
943 iw_init_output(addr, sbuf, cc)
944 	void	*addr;
945 	void	*sbuf;
946 	int	cc;
947 {
948 	struct iw_softc *sc = (struct iw_softc *) addr;
949 
950 	DPRINTF(("iw_init_output\n"));
951 
952 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sbuf,
953 		     cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
954 	return 0;
955 }
956 
957 int
958 iw_init_input(void *addr, void *sbuf, int cc)
959 {
960 	struct	iw_softc *sc;
961 
962 	DPRINTF(("iw_init_input\n"));
963 	sc = (struct iw_softc *) addr;
964 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sbuf,
965 		     cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
966 	return 0;
967 }
968 
969 
970 int
971 iw_start_output(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
972 {
973 	struct	iw_softc *sc;
974 
975 #ifdef DIAGNOSTIC
976 	if (!intr) {
977 		printf("iw_start_output: no callback!\n");
978 		return 1;
979 	}
980 #endif
981 	sc = addr;
982 	sc->sc_playintr = intr;
983 	sc->sc_playarg = arg;
984 	sc->sc_dma_flags |= DMAMODE_WRITE;
985 	sc->sc_playdma_bp = p;
986 
987 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sc->sc_playdma_bp,
988 	    cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);
989 
990 
991 	if (sc->play_encoding == AUDIO_ENCODING_ADPCM)
992 		cc >>= 2;
993 	if (sc->play_precision == 16)
994 		cc >>= 1;
995 
996 	if (sc->play_channels == 2 && sc->play_encoding != AUDIO_ENCODING_ADPCM)
997 		cc >>= 1;
998 
999 	cc -= iw_cc;
1000 
1001 	/* iw_dma_access(sc,1); */
1002 	if (cc != sc->sc_playdma_cnt) {
1003 		iw_dma_count(sc, (u_short) cc, IW_DMA_PLAYBACK);
1004 		sc->sc_playdma_cnt = cc;
1005 
1006 		iw_trigger_dma(sc, IW_DMA_PLAYBACK);
1007 	}
1008 
1009 #ifdef DIAGNOSTIC
1010 	if (outputs != iw_ints)
1011 		printf("iw_start_output: out %d, int %d\n", outputs, iw_ints);
1012 	outputs++;
1013 #endif
1014 
1015 	return 0;
1016 }
1017 
1018 
1019 int
1020 iw_start_input(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
1021 {
1022 	struct	iw_softc *sc;
1023 
1024 #ifdef DIAGNOSTIC
1025 	if (!intr) {
1026 		printf("iw_start_input: no callback!\n");
1027 		return 1;
1028 	}
1029 #endif
1030 	sc = addr;
1031 	sc->sc_recintr = intr;
1032 	sc->sc_recarg = arg;
1033 	sc->sc_dma_flags |= DMAMODE_READ;
1034 	sc->sc_recdma_bp = p;
1035 
1036 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sc->sc_recdma_bp,
1037 	    cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);
1038 
1039 
1040 	if (sc->rec_encoding == AUDIO_ENCODING_ADPCM)
1041 		cc >>= 2;
1042 	if (sc->rec_precision == 16)
1043 		cc >>= 1;
1044 
1045 	if (sc->rec_channels == 2 && sc->rec_encoding != AUDIO_ENCODING_ADPCM)
1046 		cc >>= 1;
1047 
1048 	cc -= iw_cc;
1049 
1050 	/* iw_dma_access(sc,0); */
1051 	if (sc->sc_recdma_cnt != cc) {
1052 		iw_dma_count(sc, (u_short) cc, IW_DMA_RECORD);
1053 		sc->sc_recdma_cnt = cc;
1054 		/* iw_dma_ctrl(sc, IW_DMA_RECORD); */
1055 		iw_trigger_dma(sc, IW_DMA_RECORD);
1056 	}
1057 
1058 #ifdef DIAGNOSTIC
1059 	if ((inputs != iw_inints))
1060 		printf("iw_start_input: in %d, inints %d\n", inputs, iw_inints);
1061 	inputs++;
1062 #endif
1063 
1064 	return 0;
1065 }
1066 
1067 
1068 int
1069 iw_halt_output(void *addr)
1070 {
1071 	struct	iw_softc *sc;
1072 
1073 	sc = addr;
1074 	iw_stop_dma(sc, IW_DMA_PLAYBACK, 0);
1075 	return 0;
1076 }
1077 
1078 
1079 int
1080 iw_halt_input(void *addr)
1081 {
1082 	struct	iw_softc *sc;
1083 
1084 	sc = addr;
1085 	iw_stop_dma(sc, IW_DMA_RECORD, 0);
1086 	return 0;
1087 }
1088 
1089 int
1090 iw_speaker_ctl(void *addr, int newstate)
1091 {
1092 	struct iw_softc *sc;
1093 	u_char reg;
1094 
1095 	sc = addr;
1096 	if (newstate == SPKR_ON) {
1097 		sc->sc_dac.off = 0;
1098 		IW_READ_CODEC_1(CLDACI, reg);
1099 		IW_WRITE_CODEC_1(CLDACI, reg & 0x7f);
1100 		IW_READ_CODEC_1(CRDACI, reg);
1101 		IW_WRITE_CODEC_1(CRDACI, reg & 0x7f);
1102 	} else {
1103 		/* SPKR_OFF */
1104 		sc->sc_dac.off = 1;
1105 		IW_READ_CODEC_1(CLDACI, reg);
1106 		IW_WRITE_CODEC_1(CLDACI, reg | 0x80);
1107 		IW_READ_CODEC_1(CRDACI, reg);
1108 		IW_WRITE_CODEC_1(CRDACI, reg | 0x80);
1109 	}
1110 	return 0;
1111 }
1112 
1113 int
1114 iw_getdev(void *addr, struct audio_device *retp)
1115 {
1116 
1117 	*retp = iw_device;
1118 	return 0;
1119 }
1120 
1121 int
1122 iw_setfd(void *addr, int flag)
1123 {
1124 
1125 	return 0;
1126 }
1127 
1128 /* Mixer (in/out ports) */
1129 int
1130 iw_set_port(void *addr, mixer_ctrl_t *cp)
1131 {
1132 	struct iw_softc *sc;
1133 	u_char vall, valr;
1134 	int error;
1135 
1136 	sc = addr;
1137 	vall = 0;
1138 	valr = 0;
1139 	error = EINVAL;
1140 	switch (cp->dev) {
1141 	case IW_MIC_IN_LVL:
1142 		if (cp->type == AUDIO_MIXER_VALUE) {
1143 			error = 0;
1144 			if (cp->un.value.num_channels == 1) {
1145 				vall = valr = cp->un.value.level[0];
1146 			} else {
1147 				vall = cp->un.value.level[0];
1148 				valr = cp->un.value.level[1];
1149 			}
1150 			sc->sc_mic.voll = vall;
1151 			sc->sc_mic.volr = valr;
1152 			iw_mixer_line_level(sc, IW_MIC_IN, vall, valr);
1153 		}
1154 		break;
1155 	case IW_AUX1_LVL:
1156 		if (cp->type == AUDIO_MIXER_VALUE) {
1157 			error = 0;
1158 			if (cp->un.value.num_channels == 1) {
1159 				vall = valr = cp->un.value.level[0];
1160 			} else {
1161 				vall = cp->un.value.level[0];
1162 				valr = cp->un.value.level[1];
1163 			}
1164 			sc->sc_aux1.voll = vall;
1165 			sc->sc_aux1.volr = valr;
1166 			iw_mixer_line_level(sc, IW_AUX1, vall, valr);
1167 		}
1168 		break;
1169 	case IW_AUX2_LVL:
1170 		if (cp->type == AUDIO_MIXER_VALUE) {
1171 			error = 0;
1172 			if (cp->un.value.num_channels == 1) {
1173 				vall = valr = cp->un.value.level[0];
1174 			} else {
1175 				vall = cp->un.value.level[0];
1176 				valr = cp->un.value.level[1];
1177 			}
1178 			sc->sc_aux2.voll = vall;
1179 			sc->sc_aux2.volr = valr;
1180 			iw_mixer_line_level(sc, IW_AUX2, vall, valr);
1181 		}
1182 		break;
1183 	case IW_LINE_IN_LVL:
1184 		if (cp->type == AUDIO_MIXER_VALUE) {
1185 			error = 0;
1186 			if (cp->un.value.num_channels == 1) {
1187 				vall = valr = cp->un.value.level[0];
1188 			} else {
1189 				vall = cp->un.value.level[0];
1190 				valr = cp->un.value.level[1];
1191 			}
1192 			sc->sc_linein.voll = vall;
1193 			sc->sc_linein.volr = valr;
1194 			iw_mixer_line_level(sc, IW_LINE_IN, vall, valr);
1195 		}
1196 		break;
1197 	case IW_LINE_OUT_LVL:
1198 		if (cp->type == AUDIO_MIXER_VALUE) {
1199 			error = 0;
1200 			if (cp->un.value.num_channels == 1) {
1201 				vall = valr = cp->un.value.level[0];
1202 			} else {
1203 				vall = cp->un.value.level[0];
1204 				valr = cp->un.value.level[1];
1205 			}
1206 			sc->sc_lineout.voll = vall;
1207 			sc->sc_lineout.volr = valr;
1208 			iw_mixer_line_level(sc, IW_LINE_OUT, vall, valr);
1209 		}
1210 		break;
1211 	case IW_REC_LVL:
1212 		if (cp->type == AUDIO_MIXER_VALUE) {
1213 			error = 0;
1214 			if (cp->un.value.num_channels == 1) {
1215 				vall = valr = cp->un.value.level[0];
1216 			} else {
1217 				vall = cp->un.value.level[0];
1218 				valr = cp->un.value.level[1];
1219 			}
1220 			sc->sc_rec.voll = vall;
1221 			sc->sc_rec.volr = valr;
1222 			iw_mixer_line_level(sc, IW_REC, vall, valr);
1223 		}
1224 		break;
1225 
1226 	case IW_DAC_LVL:
1227 		if (cp->type == AUDIO_MIXER_VALUE) {
1228 			error = 0;
1229 			if (cp->un.value.num_channels == 1) {
1230 				vall = valr = cp->un.value.level[0];
1231 			} else {
1232 				vall = cp->un.value.level[0];
1233 				valr = cp->un.value.level[1];
1234 			}
1235 			sc->sc_dac.voll = vall;
1236 			sc->sc_dac.volr = valr;
1237 			iw_mixer_line_level(sc, IW_DAC, vall, valr);
1238 		}
1239 		break;
1240 
1241 	case IW_LOOPBACK_LVL:
1242 		if (cp->type == AUDIO_MIXER_VALUE) {
1243 			error = 0;
1244 			if (cp->un.value.num_channels != 1) {
1245 				return EINVAL;
1246 			} else {
1247 				valr = vall = cp->un.value.level[0];
1248 			}
1249 			sc->sc_loopback.voll = vall;
1250 			sc->sc_loopback.volr = valr;
1251 			iw_mixer_line_level(sc, IW_LOOPBACK, vall, valr);
1252 		}
1253 		break;
1254 
1255 	case IW_MONO_IN_LVL:
1256 		if (cp->type == AUDIO_MIXER_VALUE) {
1257 			error = 0;
1258 			if (cp->un.value.num_channels != 1) {
1259 				return EINVAL;
1260 			} else {
1261 				valr = vall = cp->un.value.level[0];
1262 			}
1263 			sc->sc_monoin.voll = vall;
1264 			sc->sc_monoin.volr = valr;
1265 			iw_mixer_line_level(sc, IW_MONO_IN, vall, valr);
1266 		}
1267 		break;
1268 	case IW_RECORD_SOURCE:
1269 		error = 0;
1270 		sc->sc_recsrcbits = cp->un.ord << 6;
1271 		DPRINTF(("record source %d bits %x\n", cp->un.ord, sc->sc_recsrcbits));
1272 		iw_mixer_line_level(sc, IW_REC, sc->sc_rec.voll, sc->sc_rec.volr);
1273 		break;
1274 	}
1275 
1276 	return error;
1277 }
1278 
1279 
1280 int
1281 iw_get_port(void *addr, mixer_ctrl_t *cp)
1282 {
1283 	struct iw_softc *sc;
1284 	int error;
1285 
1286 	sc = addr;
1287 	error = EINVAL;
1288 	switch (cp->dev) {
1289 	case IW_MIC_IN_LVL:
1290 		if (cp->type == AUDIO_MIXER_VALUE) {
1291 			cp->un.value.num_channels = 2;
1292 			cp->un.value.level[0] = sc->sc_mic.voll;
1293 			cp->un.value.level[1] = sc->sc_mic.volr;
1294 			error = 0;
1295 		}
1296 		break;
1297 	case IW_AUX1_LVL:
1298 		if (cp->type == AUDIO_MIXER_VALUE) {
1299 			cp->un.value.num_channels = 2;
1300 			cp->un.value.level[0] = sc->sc_aux1.voll;
1301 			cp->un.value.level[1] = sc->sc_aux1.volr;
1302 			error = 0;
1303 		}
1304 		break;
1305 	case IW_AUX2_LVL:
1306 		if (cp->type == AUDIO_MIXER_VALUE) {
1307 			cp->un.value.num_channels = 2;
1308 			cp->un.value.level[0] = sc->sc_aux2.voll;
1309 			cp->un.value.level[1] = sc->sc_aux2.volr;
1310 			error = 0;
1311 		}
1312 		break;
1313 	case IW_LINE_OUT_LVL:
1314 		if (cp->type == AUDIO_MIXER_VALUE) {
1315 			cp->un.value.num_channels = 2;
1316 			cp->un.value.level[0] = sc->sc_lineout.voll;
1317 			cp->un.value.level[1] = sc->sc_lineout.volr;
1318 			error = 0;
1319 		}
1320 		break;
1321 	case IW_LINE_IN_LVL:
1322 		if (cp->type == AUDIO_MIXER_VALUE) {
1323 			cp->un.value.num_channels = 2;
1324 			cp->un.value.level[0] = sc->sc_linein.voll;
1325 			cp->un.value.level[1] = sc->sc_linein.volr;
1326 			error = 0;
1327 		}
1328 	case IW_REC_LVL:
1329 		if (cp->type == AUDIO_MIXER_VALUE) {
1330 			cp->un.value.num_channels = 2;
1331 			cp->un.value.level[0] = sc->sc_rec.voll;
1332 			cp->un.value.level[1] = sc->sc_rec.volr;
1333 			error = 0;
1334 		}
1335 		break;
1336 
1337 	case IW_DAC_LVL:
1338 		if (cp->type == AUDIO_MIXER_VALUE) {
1339 			cp->un.value.num_channels = 2;
1340 			cp->un.value.level[0] = sc->sc_dac.voll;
1341 			cp->un.value.level[1] = sc->sc_dac.volr;
1342 			error = 0;
1343 		}
1344 		break;
1345 
1346 	case IW_LOOPBACK_LVL:
1347 		if (cp->type == AUDIO_MIXER_VALUE) {
1348 			cp->un.value.num_channels = 1;
1349 			cp->un.value.level[0] = sc->sc_loopback.voll;
1350 			error = 0;
1351 		}
1352 		break;
1353 
1354 	case IW_MONO_IN_LVL:
1355 		if (cp->type == AUDIO_MIXER_VALUE) {
1356 			cp->un.value.num_channels = 1;
1357 			cp->un.value.level[0] = sc->sc_monoin.voll;
1358 			error = 0;
1359 		}
1360 		break;
1361 	case IW_RECORD_SOURCE:
1362 		cp->un.ord = sc->sc_recsrcbits >> 6;
1363 		error = 0;
1364 		break;
1365 	}
1366 
1367 	return error;
1368 }
1369 
1370 
1371 
1372 int
1373 iw_query_devinfo(void *addr, mixer_devinfo_t *dip)
1374 {
1375 
1376 	switch (dip->index) {
1377 	case IW_MIC_IN_LVL:	/* Microphone */
1378 		dip->type = AUDIO_MIXER_VALUE;
1379 		dip->mixer_class = IW_INPUT_CLASS;
1380 		dip->prev = AUDIO_MIXER_LAST;
1381 		dip->next = AUDIO_MIXER_LAST;
1382 		strcpy(dip->label.name, AudioNmicrophone);
1383 		dip->un.v.num_channels = 2;
1384 		strcpy(dip->un.v.units.name, AudioNvolume);
1385 		break;
1386 	case IW_AUX1_LVL:
1387 		dip->type = AUDIO_MIXER_VALUE;
1388 		dip->mixer_class = IW_INPUT_CLASS;
1389 		dip->prev = AUDIO_MIXER_LAST;
1390 		dip->next = AUDIO_MIXER_LAST;
1391 		strcpy(dip->label.name, AudioNline);
1392 		dip->un.v.num_channels = 2;
1393 		strcpy(dip->un.v.units.name, AudioNvolume);
1394 		break;
1395 	case IW_AUX2_LVL:
1396 		dip->type = AUDIO_MIXER_VALUE;
1397 		dip->mixer_class = IW_INPUT_CLASS;
1398 		dip->prev = AUDIO_MIXER_LAST;
1399 		dip->next = AUDIO_MIXER_LAST;
1400 		strcpy(dip->label.name, AudioNcd);
1401 		dip->un.v.num_channels = 2;
1402 		strcpy(dip->un.v.units.name, AudioNvolume);
1403 		break;
1404 	case IW_LINE_OUT_LVL:
1405 		dip->type = AUDIO_MIXER_VALUE;
1406 		dip->mixer_class = IW_OUTPUT_CLASS;
1407 		dip->prev = AUDIO_MIXER_LAST;
1408 		dip->next = AUDIO_MIXER_LAST;
1409 		strcpy(dip->label.name, AudioNline);
1410 		dip->un.v.num_channels = 2;
1411 		strcpy(dip->un.v.units.name, AudioNvolume);
1412 		break;
1413 	case IW_DAC_LVL:
1414 		dip->type = AUDIO_MIXER_VALUE;
1415 		dip->mixer_class = IW_OUTPUT_CLASS;
1416 		dip->prev = AUDIO_MIXER_LAST;
1417 		dip->next = AUDIO_MIXER_LAST;
1418 		strcpy(dip->label.name, AudioNdac);
1419 		dip->un.v.num_channels = 2;
1420 		strcpy(dip->un.v.units.name, AudioNvolume);
1421 		break;
1422 	case IW_LINE_IN_LVL:
1423 		dip->type = AUDIO_MIXER_VALUE;
1424 		dip->mixer_class = IW_INPUT_CLASS;
1425 		dip->prev = AUDIO_MIXER_LAST;
1426 		dip->next = AUDIO_MIXER_LAST;
1427 		strcpy(dip->label.name, AudioNinput);
1428 		dip->un.v.num_channels = 2;
1429 		strcpy(dip->un.v.units.name, AudioNvolume);
1430 		break;
1431 	case IW_MONO_IN_LVL:
1432 		dip->type = AUDIO_MIXER_VALUE;
1433 		dip->mixer_class = IW_INPUT_CLASS;
1434 		dip->prev = AUDIO_MIXER_LAST;
1435 		dip->next = AUDIO_MIXER_LAST;
1436 		strcpy(dip->label.name, AudioNmono);
1437 		dip->un.v.num_channels = 1;
1438 		strcpy(dip->un.v.units.name, AudioNvolume);
1439 		break;
1440 
1441 	case IW_REC_LVL:	/* record level */
1442 		dip->type = AUDIO_MIXER_VALUE;
1443 		dip->mixer_class = IW_RECORD_CLASS;
1444 		dip->prev = AUDIO_MIXER_LAST;
1445 		dip->next = AUDIO_MIXER_LAST;
1446 		strcpy(dip->label.name, AudioNrecord);
1447 		dip->un.v.num_channels = 2;
1448 		strcpy(dip->un.v.units.name, AudioNvolume);
1449 		break;
1450 
1451 	case IW_LOOPBACK_LVL:
1452 		dip->type = AUDIO_MIXER_VALUE;
1453 		dip->mixer_class = IW_RECORD_CLASS;
1454 		dip->prev = AUDIO_MIXER_LAST;
1455 		dip->next = AUDIO_MIXER_LAST;
1456 		strcpy(dip->label.name, "filter");
1457 		dip->un.v.num_channels = 1;
1458 		strcpy(dip->un.v.units.name, AudioNvolume);
1459 		break;
1460 
1461 	case IW_RECORD_SOURCE:
1462 		dip->mixer_class = IW_RECORD_CLASS;
1463 		dip->type = AUDIO_MIXER_ENUM;
1464 		dip->prev = AUDIO_MIXER_LAST;
1465 		dip->next = AUDIO_MIXER_LAST;
1466 		strcpy(dip->label.name, AudioNsource);
1467 		dip->un.e.num_mem = 4;
1468 		strcpy(dip->un.e.member[0].label.name, AudioNline);
1469 		dip->un.e.member[0].ord = IW_LINE_IN_SRC;
1470 		strcpy(dip->un.e.member[1].label.name, "aux1");
1471 		dip->un.e.member[1].ord = IW_AUX1_SRC;
1472 		strcpy(dip->un.e.member[2].label.name, AudioNmicrophone);
1473 		dip->un.e.member[2].ord = IW_MIC_IN_SRC;
1474 		strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
1475 		dip->un.e.member[3].ord = IW_MIX_OUT_SRC;
1476 		break;
1477 	case IW_INPUT_CLASS:
1478 		dip->type = AUDIO_MIXER_CLASS;
1479 		dip->mixer_class = IW_INPUT_CLASS;
1480 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1481 		strcpy(dip->label.name, AudioCinputs);
1482 		break;
1483 	case IW_OUTPUT_CLASS:
1484 		dip->type = AUDIO_MIXER_CLASS;
1485 		dip->mixer_class = IW_OUTPUT_CLASS;
1486 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1487 		strcpy(dip->label.name, AudioCoutputs);
1488 		break;
1489 	case IW_RECORD_CLASS:	/* record source class */
1490 		dip->type = AUDIO_MIXER_CLASS;
1491 		dip->mixer_class = IW_RECORD_CLASS;
1492 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1493 		strcpy(dip->label.name, AudioCrecord);
1494 		return 0;
1495 	default:
1496 		return ENXIO;
1497 	}
1498 	return 0;
1499 }
1500 
1501 
1502 void *
1503 iw_malloc(void *addr, int direction, size_t size,
1504     struct malloc_type *pool, int flags)
1505 {
1506 	struct iw_softc *sc;
1507 	int drq;
1508 
1509 	sc = addr;
1510 	if (direction == AUMODE_PLAY)
1511 		drq = sc->sc_playdrq;
1512 	else
1513 		drq = sc->sc_recdrq;
1514 	return isa_malloc(sc->sc_ic, drq, size, pool, flags);
1515 }
1516 
1517 void
1518 iw_free(void *addr, void *ptr, struct malloc_type *pool)
1519 {
1520 	isa_free(ptr, pool);
1521 }
1522 
1523 size_t
1524 iw_round_buffersize(void *addr, int direction, size_t size)
1525 {
1526 	struct iw_softc *sc;
1527 	bus_size_t maxsize;
1528 
1529 	sc = addr;
1530 	if (direction == AUMODE_PLAY)
1531 		maxsize = sc->sc_play_maxsize;
1532 	else
1533 		maxsize = sc->sc_rec_maxsize;
1534 
1535 	if (size > maxsize)
1536 		size = maxsize;
1537 	return size;
1538 }
1539 
1540 paddr_t
1541 iw_mappage(void *addr, void *mem, off_t off, int prot)
1542 {
1543 
1544 	return isa_mappage(mem, off, prot);
1545 }
1546 
1547 int
1548 iw_get_props(void *addr)
1549 {
1550 	struct iw_softc *sc;
1551 
1552 	sc = addr;
1553 	return AUDIO_PROP_MMAP |
1554 		(sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
1555 }
1556