xref: /openbsd/sys/dev/isa/ess.c (revision 133306f0)
1 /*	$OpenBSD: ess.c,v 1.2 1999/09/30 22:13:52 kstailey Exp $	*/
2 /*	$NetBSD: ess.c,v 1.44.4.1 1999/06/21 01:18:00 thorpej Exp $	*/
3 
4 /*
5  * Copyright 1997
6  * Digital Equipment Corporation. All rights reserved.
7  *
8  * This software is furnished under license and may be used and
9  * copied only in accordance with the following terms and conditions.
10  * Subject to these conditions, you may download, copy, install,
11  * use, modify and distribute this software in source and/or binary
12  * form. No title or ownership is transferred hereby.
13  *
14  * 1) Any source code used, modified or distributed must reproduce
15  *    and retain this copyright notice and list of conditions as
16  *    they appear in the source file.
17  *
18  * 2) No right is granted to use any trade name, trademark, or logo of
19  *    Digital Equipment Corporation. Neither the "Digital Equipment
20  *    Corporation" name nor any trademark or logo of Digital Equipment
21  *    Corporation may be used to endorse or promote products derived
22  *    from this software without the prior written permission of
23  *    Digital Equipment Corporation.
24  *
25  * 3) This software is provided "AS-IS" and any express or implied
26  *    warranties, including but not limited to, any implied warranties
27  *    of merchantability, fitness for a particular purpose, or
28  *    non-infringement are disclaimed. In no event shall DIGITAL be
29  *    liable for any damages whatsoever, and in particular, DIGITAL
30  *    shall not be liable for special, indirect, consequential, or
31  *    incidental damages or damages for lost profits, loss of
32  *    revenue or loss of use, whether such damages arise in contract,
33  *    negligence, tort, under statute, in equity, at law or otherwise,
34  *    even if advised of the possibility of such damage.
35  */
36 
37 /*
38 **++
39 **
40 **  ess.c
41 **
42 **  FACILITY:
43 **
44 **	DIGITAL Network Appliance Reference Design (DNARD)
45 **
46 **  MODULE DESCRIPTION:
47 **
48 **      This module contains the device driver for the ESS
49 **      Technologies 1888/1887/888 sound chip. The code in sbdsp.c was
50 **	used as a reference point when implementing this driver.
51 **
52 **  AUTHORS:
53 **
54 **	Blair Fidler	Software Engineering Australia
55 **			Gold Coast, Australia.
56 **
57 **  CREATION DATE:
58 **
59 **	March 10, 1997.
60 **
61 **  MODIFICATION HISTORY:
62 **
63 **	Heavily modified by Lennart Augustsson and Charles M. Hannum for
64 **	bus_dma, changes to audio interface, and many bug fixes.
65 **	ESS1788 support by Nathan J. Williams and Charles M. Hannum.
66 **--
67 */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/errno.h>
72 #include <sys/ioctl.h>
73 #include <sys/syslog.h>
74 #include <sys/device.h>
75 #include <sys/proc.h>
76 #include <sys/kernel.h>
77 
78 #include <machine/cpu.h>
79 #include <machine/intr.h>
80 #include <machine/bus.h>
81 
82 #include <sys/audioio.h>
83 #include <dev/audio_if.h>
84 #include <dev/auconv.h>
85 #include <dev/mulaw.h>
86 
87 #include <dev/isa/isavar.h>
88 #include <dev/isa/isadmavar.h>
89 
90 #include <dev/isa/essvar.h>
91 #include <dev/isa/essreg.h>
92 
93 #ifdef AUDIO_DEBUG
94 #define DPRINTF(x)	if (essdebug) printf x
95 #define DPRINTFN(n,x)	if (essdebug>(n)) printf x
96 int	essdebug = 0;
97 #else
98 #define DPRINTF(x)
99 #define DPRINTFN(n,x)
100 #endif
101 
102 #if 0
103 unsigned uuu;
104 #define EREAD1(t, h, a) (uuu=bus_space_read_1(t, h, a),printf("EREAD  %02x=%02x\n", ((int)h&0xfff)+a, uuu),uuu)
105 #define EWRITE1(t, h, a, d) (printf("EWRITE %02x=%02x\n", ((int)h & 0xfff)+a, d), bus_space_write_1(t, h, a, d))
106 #else
107 #define EREAD1(t, h, a) bus_space_read_1(t, h, a)
108 #define EWRITE1(t, h, a, d) bus_space_write_1(t, h, a, d)
109 #endif
110 
111 struct cfdriver ess_cd = {
112 	NULL, "ess", DV_DULL
113 };
114 
115 int	ess_setup_sc __P((struct ess_softc *, int));
116 
117 int	ess_open __P((void *, int));
118 void	ess_1788_close __P((void *));
119 void	ess_1888_close __P((void *));
120 int	ess_getdev __P((void *, struct audio_device *));
121 int	ess_drain __P((void *));
122 
123 int	ess_query_encoding __P((void *, struct audio_encoding *));
124 
125 int	ess_set_params __P((void *, int, int, struct audio_params *,
126 	    struct audio_params *));
127 
128 int	ess_round_blocksize __P((void *, int));
129 
130 int	ess_audio1_trigger_output __P((void *, void *, void *, int,
131 	    void (*)(void *), void *, struct audio_params *));
132 int	ess_audio2_trigger_output __P((void *, void *, void *, int,
133 	    void (*)(void *), void *, struct audio_params *));
134 int	ess_audio1_trigger_input __P((void *, void *, void *, int,
135 	    void (*)(void *), void *, struct audio_params *));
136 int	ess_audio1_halt __P((void *));
137 int	ess_audio2_halt __P((void *));
138 int	ess_audio1_intr __P((void *));
139 int	ess_audio2_intr __P((void *));
140 void	ess_audio1_poll __P((void *));
141 void	ess_audio2_poll __P((void *));
142 
143 int	ess_speaker_ctl __P((void *, int));
144 
145 int	ess_getdev __P((void *, struct audio_device *));
146 
147 int	ess_set_port __P((void *, mixer_ctrl_t *));
148 int	ess_get_port __P((void *, mixer_ctrl_t *));
149 
150 void   *ess_malloc __P((void *, unsigned long, int, int));
151 void	ess_free __P((void *, void *, int));
152 unsigned long ess_round_buffersize __P((void *, unsigned long));
153 int	ess_mappage __P((void *, void *, int, int));
154 
155 
156 int	ess_query_devinfo __P((void *, mixer_devinfo_t *));
157 int	ess_1788_get_props __P((void *));
158 int	ess_1888_get_props __P((void *));
159 
160 void	ess_speaker_on __P((struct ess_softc *));
161 void	ess_speaker_off __P((struct ess_softc *));
162 
163 int	ess_config_addr __P((struct ess_softc *));
164 void	ess_config_irq __P((struct ess_softc *));
165 void	ess_config_drq __P((struct ess_softc *));
166 void	ess_setup __P((struct ess_softc *));
167 int	ess_identify __P((struct ess_softc *));
168 
169 int	ess_reset __P((struct ess_softc *));
170 void	ess_set_gain __P((struct ess_softc *, int, int));
171 int	ess_set_in_port __P((struct ess_softc *, int));
172 int	ess_set_in_ports __P((struct ess_softc *, int));
173 u_int	ess_srtotc __P((u_int));
174 u_int	ess_srtofc __P((u_int));
175 u_char	ess_get_dsp_status __P((struct ess_softc *));
176 u_char	ess_dsp_read_ready __P((struct ess_softc *));
177 u_char	ess_dsp_write_ready __P((struct ess_softc *));
178 int	ess_rdsp __P((struct ess_softc *));
179 int	ess_wdsp __P((struct ess_softc *, u_char));
180 u_char	ess_read_x_reg __P((struct ess_softc *, u_char));
181 int	ess_write_x_reg __P((struct ess_softc *, u_char, u_char));
182 void	ess_clear_xreg_bits __P((struct ess_softc *, u_char, u_char));
183 void	ess_set_xreg_bits __P((struct ess_softc *, u_char, u_char));
184 u_char	ess_read_mix_reg __P((struct ess_softc *, u_char));
185 void	ess_write_mix_reg __P((struct ess_softc *, u_char, u_char));
186 void	ess_clear_mreg_bits __P((struct ess_softc *, u_char, u_char));
187 void	ess_set_mreg_bits __P((struct ess_softc *, u_char, u_char));
188 void	ess_read_multi_mix_reg __P((struct ess_softc *, u_char, u_int8_t *, bus_size_t));
189 
190 static char *essmodel[] = {
191 	"unsupported",
192 	"1888",
193 	"1887",
194 	"888",
195 	"1788",
196 	"1869",
197 	"1879",
198 	"1868",
199 	"1878",
200 };
201 
202 struct audio_device ess_device = {
203 	"ESS Technology",
204 	"x",
205 	"ess"
206 };
207 
208 /*
209  * Define our interface to the higher level audio driver.
210  */
211 
212 struct audio_hw_if ess_1788_hw_if = {
213 	ess_open,
214 	ess_1788_close,
215 	ess_drain,
216 	ess_query_encoding,
217 	ess_set_params,
218 	ess_round_blocksize,
219 	NULL,
220 	NULL,
221 	NULL,
222 	NULL,
223 	NULL,
224 	ess_audio1_halt,
225 	ess_audio1_halt,
226 	ess_speaker_ctl,
227 	ess_getdev,
228 	NULL,
229 	ess_set_port,
230 	ess_get_port,
231 	ess_query_devinfo,
232 	ess_malloc,
233 	ess_free,
234 	ess_round_buffersize,
235 	ess_mappage,
236 	ess_1788_get_props,
237 	ess_audio1_trigger_output,
238 	ess_audio1_trigger_input,
239 };
240 
241 struct audio_hw_if ess_1888_hw_if = {
242 	ess_open,
243 	ess_1888_close,
244 	ess_drain,
245 	ess_query_encoding,
246 	ess_set_params,
247 	ess_round_blocksize,
248 	NULL,
249 	NULL,
250 	NULL,
251 	NULL,
252 	NULL,
253 	ess_audio2_halt,
254 	ess_audio1_halt,
255 	ess_speaker_ctl,
256 	ess_getdev,
257 	NULL,
258 	ess_set_port,
259 	ess_get_port,
260 	ess_query_devinfo,
261 	ess_malloc,
262 	ess_free,
263 	ess_round_buffersize,
264 	ess_mappage,
265 	ess_1888_get_props,
266 	ess_audio2_trigger_output,
267 	ess_audio1_trigger_input,
268 };
269 
270 #ifdef AUDIO_DEBUG
271 void ess_printsc __P((struct ess_softc *));
272 void ess_dump_mixer __P((struct ess_softc *));
273 
274 void
275 ess_printsc(sc)
276 	struct ess_softc *sc;
277 {
278 	int i;
279 
280 	printf("open %d iobase 0x%x outport %u inport %u speaker %s\n",
281 	       (int)sc->sc_open, sc->sc_iobase, sc->out_port,
282 	       sc->in_port, sc->spkr_state ? "on" : "off");
283 
284 	printf("audio1: dmachan %d irq %d nintr %lu intr %p arg %p\n",
285 	       sc->sc_audio1.drq, sc->sc_audio1.irq, sc->sc_audio1.nintr,
286 	       sc->sc_audio1.intr, sc->sc_audio1.arg);
287 
288 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
289 		printf("audio2: dmachan %d irq %d nintr %lu intr %p arg %p\n",
290 		       sc->sc_audio2.drq, sc->sc_audio2.irq, sc->sc_audio2.nintr,
291 		       sc->sc_audio2.intr, sc->sc_audio2.arg);
292 	}
293 
294 	printf("gain:");
295 	for (i = 0; i < sc->ndevs; i++)
296 		printf(" %u,%u", sc->gain[i][ESS_LEFT], sc->gain[i][ESS_RIGHT]);
297 	printf("\n");
298 }
299 
300 void
301 ess_dump_mixer(sc)
302 	struct ess_softc *sc;
303 {
304 	printf("ESS_DAC_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
305 	       0x7C, ess_read_mix_reg(sc, 0x7C));
306 	printf("ESS_MIC_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
307 	       0x1A, ess_read_mix_reg(sc, 0x1A));
308 	printf("ESS_LINE_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
309 	       0x3E, ess_read_mix_reg(sc, 0x3E));
310 	printf("ESS_SYNTH_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
311 	       0x36, ess_read_mix_reg(sc, 0x36));
312 	printf("ESS_CD_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
313 	       0x38, ess_read_mix_reg(sc, 0x38));
314 	printf("ESS_AUXB_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
315 	       0x3A, ess_read_mix_reg(sc, 0x3A));
316 	printf("ESS_MASTER_VOL: mix reg 0x%02x=0x%02x\n",
317 	       0x32, ess_read_mix_reg(sc, 0x32));
318 	printf("ESS_PCSPEAKER_VOL: mix reg 0x%02x=0x%02x\n",
319 	       0x3C, ess_read_mix_reg(sc, 0x3C));
320 	printf("ESS_DAC_REC_VOL: mix reg 0x%02x=0x%02x\n",
321 	       0x69, ess_read_mix_reg(sc, 0x69));
322 	printf("ESS_MIC_REC_VOL: mix reg 0x%02x=0x%02x\n",
323 	       0x68, ess_read_mix_reg(sc, 0x68));
324 	printf("ESS_LINE_REC_VOL: mix reg 0x%02x=0x%02x\n",
325 	       0x6E, ess_read_mix_reg(sc, 0x6E));
326 	printf("ESS_SYNTH_REC_VOL: mix reg 0x%02x=0x%02x\n",
327 	       0x6B, ess_read_mix_reg(sc, 0x6B));
328 	printf("ESS_CD_REC_VOL: mix reg 0x%02x=0x%02x\n",
329 	       0x6A, ess_read_mix_reg(sc, 0x6A));
330 	printf("ESS_AUXB_REC_VOL: mix reg 0x%02x=0x%02x\n",
331 	       0x6C, ess_read_mix_reg(sc, 0x6C));
332 	printf("ESS_RECORD_VOL: x reg 0x%02x=0x%02x\n",
333 	       0xB4, ess_read_x_reg(sc, 0xB4));
334 	printf("Audio 1 play vol (unused): mix reg 0x%02x=0x%02x\n",
335 	       0x14, ess_read_mix_reg(sc, 0x14));
336 
337 	printf("ESS_MIC_PREAMP: x reg 0x%02x=0x%02x\n",
338 	       ESS_XCMD_PREAMP_CTRL, ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL));
339 	printf("ESS_RECORD_MONITOR: x reg 0x%02x=0x%02x\n",
340 	       ESS_XCMD_AUDIO_CTRL, ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL));
341 	printf("Record source: mix reg 0x%02x=0x%02x, 0x%02x=0x%02x\n",
342 	       ESS_MREG_ADC_SOURCE, ess_read_mix_reg(sc, ESS_MREG_ADC_SOURCE),
343 	       ESS_MREG_AUDIO2_CTRL2, ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2));
344 }
345 
346 #endif
347 
348 /*
349  * Configure the ESS chip for the desired audio base address.
350  */
351 int
352 ess_config_addr(sc)
353 	struct ess_softc *sc;
354 {
355 	int iobase = sc->sc_iobase;
356 	bus_space_tag_t iot = sc->sc_iot;
357 
358 	/*
359 	 * Configure using the System Control Register method.  This
360 	 * method is used when the AMODE line is tied high, which is
361 	 * the case for the Shark, but not for the evaluation board.
362 	 */
363 
364 	bus_space_handle_t scr_access_ioh;
365 	bus_space_handle_t scr_ioh;
366 	u_short scr_value;
367 
368 	/*
369 	 * Set the SCR bit to enable audio.
370 	 */
371 	scr_value = ESS_SCR_AUDIO_ENABLE;
372 
373 	/*
374 	 * Set the SCR bits necessary to select the specified audio
375 	 * base address.
376 	 */
377 	switch(iobase) {
378 	case 0x220:
379 		scr_value |= ESS_SCR_AUDIO_220;
380 		break;
381 	case 0x230:
382 		scr_value |= ESS_SCR_AUDIO_230;
383 		break;
384 	case 0x240:
385 		scr_value |= ESS_SCR_AUDIO_240;
386 		break;
387 	case 0x250:
388 		scr_value |= ESS_SCR_AUDIO_250;
389 		break;
390 	default:
391 		printf("ess: configured iobase 0x%x invalid\n", iobase);
392 		return (1);
393 		break;
394 	}
395 
396 	/*
397 	 * Get a mapping for the System Control Register (SCR) access
398 	 * registers and the SCR data registers.
399 	 */
400 	if (bus_space_map(iot, ESS_SCR_ACCESS_BASE, ESS_SCR_ACCESS_PORTS,
401 			  0, &scr_access_ioh)) {
402 		printf("ess: can't map SCR access registers\n");
403 		return (1);
404 	}
405 	if (bus_space_map(iot, ESS_SCR_BASE, ESS_SCR_PORTS,
406 			  0, &scr_ioh)) {
407 		printf("ess: can't map SCR registers\n");
408 		bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS);
409 		return (1);
410 	}
411 
412 	/* Unlock the SCR. */
413 	EWRITE1(iot, scr_access_ioh, ESS_SCR_UNLOCK, 0);
414 
415 	/* Write the base address information into SCR[0]. */
416 	EWRITE1(iot, scr_ioh, ESS_SCR_INDEX, 0);
417 	EWRITE1(iot, scr_ioh, ESS_SCR_DATA, scr_value);
418 
419 	/* Lock the SCR. */
420 	EWRITE1(iot, scr_access_ioh, ESS_SCR_LOCK, 0);
421 
422 	/* Unmap the SCR access ports and the SCR data ports. */
423 	bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS);
424 	bus_space_unmap(iot, scr_ioh, ESS_SCR_PORTS);
425 
426 	return 0;
427 }
428 
429 
430 /*
431  * Configure the ESS chip for the desired IRQ and DMA channels.
432  * ESS  ISA
433  * --------
434  * IRQA irq9
435  * IRQB irq5
436  * IRQC irq7
437  * IRQD irq10
438  * IRQE irq15
439  *
440  * DRQA drq0
441  * DRQB drq1
442  * DRQC drq3
443  * DRQD drq5
444  */
445 void
446 ess_config_irq(sc)
447 	struct ess_softc *sc;
448 {
449 	int v;
450 
451 	DPRINTFN(2,("ess_config_irq\n"));
452 
453 	if (sc->sc_model == ESS_1887 &&
454 	    sc->sc_audio1.irq == sc->sc_audio2.irq &&
455 	    sc->sc_audio1.irq != -1) {
456 		/* Use new method, both interrupts are the same. */
457 		v = ESS_IS_SELECT_IRQ;	/* enable intrs */
458 		switch (sc->sc_audio1.irq) {
459 		case 5:
460 			v |= ESS_IS_INTRB;
461 			break;
462 		case 7:
463 			v |= ESS_IS_INTRC;
464 			break;
465 		case 9:
466 			v |= ESS_IS_INTRA;
467 			break;
468 		case 10:
469 			v |= ESS_IS_INTRD;
470 			break;
471 		case 15:
472 			v |= ESS_IS_INTRE;
473 			break;
474 #ifdef DIAGNOSTIC
475 		default:
476 			printf("ess_config_irq: configured irq %d not supported for Audio 1\n",
477 			       sc->sc_audio1.irq);
478 			return;
479 #endif
480 		}
481 		/* Set the IRQ */
482 		ess_write_mix_reg(sc, ESS_MREG_INTR_ST, v);
483 		return;
484 	}
485 
486 	if (sc->sc_model == ESS_1887) {
487 		/* Tell the 1887 to use the old interrupt method. */
488 		ess_write_mix_reg(sc, ESS_MREG_INTR_ST, ESS_IS_ES1888);
489 	}
490 
491 	if (sc->sc_audio1.polled) {
492 		/* Turn off Audio1 interrupts. */
493 		v = 0;
494 	} else {
495 		/* Configure Audio 1 for the appropriate IRQ line. */
496 		v = ESS_IRQ_CTRL_MASK | ESS_IRQ_CTRL_EXT; /* All intrs on */
497 		switch (sc->sc_audio1.irq) {
498 		case 5:
499 			v |= ESS_IRQ_CTRL_INTRB;
500 			break;
501 		case 7:
502 			v |= ESS_IRQ_CTRL_INTRC;
503 			break;
504 		case 9:
505 			v |= ESS_IRQ_CTRL_INTRA;
506 			break;
507 		case 10:
508 			v |= ESS_IRQ_CTRL_INTRD;
509 			break;
510 #ifdef DIAGNOSTIC
511 		default:
512 			printf("ess: configured irq %d not supported for Audio 1\n",
513 			       sc->sc_audio1.irq);
514 			return;
515 #endif
516 		}
517 	}
518 	ess_write_x_reg(sc, ESS_XCMD_IRQ_CTRL, v);
519 
520 	if (ESS_USE_AUDIO1(sc->sc_model))
521 		return;
522 
523 	if (sc->sc_audio2.polled) {
524 		/* Turn off Audio2 interrupts. */
525 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
526 				    ESS_AUDIO2_CTRL2_IRQ2_ENABLE);
527 	} else {
528 		/* Audio2 is hardwired to INTRE in this mode. */
529 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
530 				  ESS_AUDIO2_CTRL2_IRQ2_ENABLE);
531 	}
532 }
533 
534 
535 void
536 ess_config_drq(sc)
537 	struct ess_softc *sc;
538 {
539 	int v;
540 
541 	DPRINTFN(2,("ess_config_drq\n"));
542 
543 	/* Configure Audio 1 (record) for DMA on the appropriate channel. */
544 	v = ESS_DRQ_CTRL_PU | ESS_DRQ_CTRL_EXT;
545 	switch (sc->sc_audio1.drq) {
546 	case 0:
547 		v |= ESS_DRQ_CTRL_DRQA;
548 		break;
549 	case 1:
550 		v |= ESS_DRQ_CTRL_DRQB;
551 		break;
552 	case 3:
553 		v |= ESS_DRQ_CTRL_DRQC;
554 		break;
555 #ifdef DIAGNOSTIC
556 	default:
557 		printf("ess_config_drq: configured dma chan %d not supported for Audio 1\n",
558 		       sc->sc_audio1.drq);
559 		return;
560 #endif
561 	}
562 	/* Set DRQ1 */
563 	ess_write_x_reg(sc, ESS_XCMD_DRQ_CTRL, v);
564 
565 	if (ESS_USE_AUDIO1(sc->sc_model))
566 		return;
567 
568 	/* Configure DRQ2 */
569 	v = ESS_AUDIO2_CTRL3_DRQ_PD;
570 	switch (sc->sc_audio2.drq) {
571 	case 0:
572 		v |= ESS_AUDIO2_CTRL3_DRQA;
573 		break;
574 	case 1:
575 		v |= ESS_AUDIO2_CTRL3_DRQB;
576 		break;
577 	case 3:
578 		v |= ESS_AUDIO2_CTRL3_DRQC;
579 		break;
580 	case 5:
581 		v |= ESS_AUDIO2_CTRL3_DRQD;
582 		break;
583 #ifdef DIAGNOSTIC
584 	default:
585 		printf("ess_config_drq: configured dma chan %d not supported for Audio 2\n",
586 		       sc->sc_audio2.drq);
587 		return;
588 #endif
589 	}
590 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL3, v);
591 	/* Enable DMA 2 */
592 	ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
593 			  ESS_AUDIO2_CTRL2_DMA_ENABLE);
594 }
595 
596 /*
597  * Set up registers after a reset.
598  */
599 void
600 ess_setup(sc)
601 	struct ess_softc *sc;
602 {
603 
604 	ess_config_irq(sc);
605 	ess_config_drq(sc);
606 
607 	DPRINTFN(2,("ess_setup: done\n"));
608 }
609 
610 /*
611  * Determine the model of ESS chip we are talking to.  Currently we
612  * only support ES1888, ES1887 and ES888.  The method of determining
613  * the chip is based on the information on page 27 of the ES1887 data
614  * sheet.
615  *
616  * This routine sets the values of sc->sc_model and sc->sc_version.
617  */
618 int
619 ess_identify(sc)
620 	struct ess_softc *sc;
621 {
622 	u_char reg1;
623 	u_char reg2;
624 	u_char reg3;
625 	u_int8_t ident[4];
626 
627 	sc->sc_model = ESS_UNSUPPORTED;
628 	sc->sc_version = 0;
629 
630 	memset(ident, 0, sizeof(ident));
631 
632 	/*
633 	 * 1. Check legacy ID bytes.  These should be 0x68 0x8n, where
634 	 *    n >= 8 for an ES1887 or an ES888.  Other values indicate
635 	 *    earlier (unsupported) chips.
636 	 */
637 	ess_wdsp(sc, ESS_ACMD_LEGACY_ID);
638 
639 	if ((reg1 = ess_rdsp(sc)) != 0x68) {
640 		printf("ess: First ID byte wrong (0x%02x)\n", reg1);
641 		return 1;
642 	}
643 
644 	reg2 = ess_rdsp(sc);
645 	if (((reg2 & 0xf0) != 0x80) ||
646 	    ((reg2 & 0x0f) < 8)) {
647 		printf("ess: Second ID byte wrong (0x%02x)\n", reg2);
648 		return 1;
649 	}
650 
651 	/*
652 	 * Store the ID bytes as the version.
653 	 */
654 	sc->sc_version = (reg1 << 8) + reg2;
655 
656 
657 	/*
658 	 * 2. Verify we can change bit 2 in mixer register 0x64.  This
659 	 *    should be possible on all supported chips.
660 	 */
661 	reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL);
662 	reg2 = reg1 ^ 0x04;  /* toggle bit 2 */
663 
664 	ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2);
665 
666 	if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) != reg2) {
667 		printf("ess: Hardware error (unable to toggle bit 2 of mixer register 0x64)\n");
668 		return 1;
669 	}
670 
671 	/*
672 	 * Restore the original value of mixer register 0x64.
673 	 */
674 	ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1);
675 
676 
677 	/*
678 	 * 3. Verify we can change the value of mixer register
679 	 *    ESS_MREG_SAMPLE_RATE.
680 	 *    This is possible on the 1888/1887/888, but not on the 1788.
681 	 *    It is not necessary to restore the value of this mixer register.
682 	 */
683 	reg1 = ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE);
684 	reg2 = reg1 ^ 0xff;  /* toggle all bits */
685 
686 	ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, reg2);
687 
688 	if (ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE) != reg2) {
689 		/* If we got this far before failing, it's a 1788. */
690 		sc->sc_model = ESS_1788;
691 
692 		/*
693 		 * Identify ESS model for ES18[67]8.
694 		 */
695 		ess_read_multi_mix_reg(sc, 0x40, ident, sizeof(ident));
696 		if(ident[0] == 0x18) {
697 			switch(ident[1]) {
698 			case 0x68:
699 				sc->sc_model = ESS_1868;
700 				break;
701 			case 0x78:
702 				sc->sc_model = ESS_1878;
703 				break;
704 			}
705 		}
706 	} else {
707 		/*
708 		 * 4. Determine if we can change bit 5 in mixer register 0x64.
709 		 *    This determines whether we have an ES1887:
710 		 *
711 		 *    - can change indicates ES1887
712 		 *    - can't change indicates ES1888 or ES888
713 		 */
714 		reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL);
715 		reg2 = reg1 ^ 0x20;  /* toggle bit 5 */
716 
717 		ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2);
718 
719 		if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) == reg2) {
720 			sc->sc_model = ESS_1887;
721 
722 			/*
723 			 * Restore the original value of mixer register 0x64.
724 			 */
725 			ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1);
726 
727 			/*
728 			 * Identify ESS model for ES18[67]9.
729 			 */
730 			ess_read_multi_mix_reg(sc, 0x40, ident, sizeof(ident));
731 			if(ident[0] == 0x18) {
732 				switch(ident[1]) {
733 				case 0x69:
734 					sc->sc_model = ESS_1869;
735 					break;
736 				case 0x79:
737 					sc->sc_model = ESS_1879;
738 					break;
739 				}
740 			}
741 		} else {
742 			/*
743 			 * 5. Determine if we can change the value of mixer
744 			 *    register 0x69 independently of mixer register
745 			 *    0x68. This determines which chip we have:
746 			 *
747 			 *    - can modify idependently indicates ES888
748 			 *    - register 0x69 is an alias of 0x68 indicates ES1888
749 			 */
750 			reg1 = ess_read_mix_reg(sc, 0x68);
751 			reg2 = ess_read_mix_reg(sc, 0x69);
752 			reg3 = reg2 ^ 0xff;  /* toggle all bits */
753 
754 			/*
755 			 * Write different values to each register.
756 			 */
757 			ess_write_mix_reg(sc, 0x68, reg2);
758 			ess_write_mix_reg(sc, 0x69, reg3);
759 
760 			if (ess_read_mix_reg(sc, 0x68) == reg2 &&
761 			    ess_read_mix_reg(sc, 0x69) == reg3)
762 				sc->sc_model = ESS_888;
763 			else
764 				sc->sc_model = ESS_1888;
765 
766 			/*
767 			 * Restore the original value of the registers.
768 			 */
769 			ess_write_mix_reg(sc, 0x68, reg1);
770 			ess_write_mix_reg(sc, 0x69, reg2);
771 		}
772 	}
773 
774 	return 0;
775 }
776 
777 
778 int
779 ess_setup_sc(sc, doinit)
780 	struct ess_softc *sc;
781 	int doinit;
782 {
783 	/* Reset the chip. */
784 	if (ess_reset(sc) != 0) {
785 		DPRINTF(("ess_setup_sc: couldn't reset chip\n"));
786 		return (1);
787 	}
788 
789 	/* Identify the ESS chip, and check that it is supported. */
790 	if (ess_identify(sc)) {
791 		DPRINTF(("ess_setup_sc: couldn't identify\n"));
792 		return (1);
793 	}
794 
795 	return (0);
796 }
797 
798 /*
799  * Probe for the ESS hardware.
800  */
801 int
802 essmatch(sc)
803 	struct ess_softc *sc;
804 {
805 	if (!ESS_BASE_VALID(sc->sc_iobase)) {
806 		printf("ess: configured iobase 0x%x invalid\n", sc->sc_iobase);
807 		return (0);
808 	}
809 
810 	/* Configure the ESS chip for the desired audio base address. */
811 	if (ess_config_addr(sc))
812 		return (0);
813 
814 	if (ess_setup_sc(sc, 1))
815 		return (0);
816 
817 	if (sc->sc_model == ESS_UNSUPPORTED) {
818 		DPRINTF(("ess: Unsupported model\n"));
819 		return (0);
820 	}
821 
822 	/* Check that requested DMA channels are valid and different. */
823 	if (!ESS_DRQ1_VALID(sc->sc_audio1.drq)) {
824 		printf("ess: record drq %d invalid\n", sc->sc_audio1.drq);
825 		return (0);
826 	}
827 	if (!isa_drq_isfree(sc->sc_isa, sc->sc_audio1.drq))
828 		return (0);
829 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
830 		if (!ESS_DRQ2_VALID(sc->sc_audio2.drq)) {
831 			printf("ess: play drq %d invalid\n", sc->sc_audio2.drq);
832 			return (0);
833 		}
834 		if (sc->sc_audio1.drq == sc->sc_audio2.drq) {
835 			printf("ess: play and record drq both %d\n",
836 			       sc->sc_audio1.drq);
837 			return (0);
838 		}
839 		if (!isa_drq_isfree(sc->sc_isa, sc->sc_audio2.drq))
840 			return (0);
841 	}
842 
843 	/*
844 	 * The 1887 has an additional IRQ mode where both channels are mapped
845 	 * to the same IRQ.
846 	 */
847 	if (sc->sc_model == ESS_1887 &&
848 	    sc->sc_audio1.irq == sc->sc_audio2.irq &&
849 	    sc->sc_audio1.irq != -1 &&
850 	    ESS_IRQ12_VALID(sc->sc_audio1.irq))
851 		goto irq_not1888;
852 
853 	/* Check that requested IRQ lines are valid and different. */
854 	if (sc->sc_audio1.irq != -1 &&
855 	    !ESS_IRQ1_VALID(sc->sc_audio1.irq)) {
856 		printf("ess: record irq %d invalid\n", sc->sc_audio1.irq);
857 		return (0);
858 	}
859 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
860 		if (sc->sc_audio2.irq != -1 &&
861 		    !ESS_IRQ2_VALID(sc->sc_audio2.irq)) {
862 			printf("ess: play irq %d invalid\n", sc->sc_audio2.irq);
863 			return (0);
864 		}
865 		if (sc->sc_audio1.irq == sc->sc_audio2.irq &&
866 		    sc->sc_audio1.irq != -1) {
867 			printf("ess: play and record irq both %d\n",
868 			       sc->sc_audio1.irq);
869 			return (0);
870 		}
871 	}
872 
873 irq_not1888:
874 	/* XXX should we check IRQs as well? */
875 
876 	return (1);
877 }
878 
879 
880 /*
881  * Attach hardware to driver, attach hardware driver to audio
882  * pseudo-device driver.
883  */
884 void
885 essattach(sc)
886 	struct ess_softc *sc;
887 {
888 	struct audio_attach_args arg;
889 	struct audio_params pparams, rparams;
890 	int i;
891 	u_int v;
892 
893 	if (ess_setup_sc(sc, 0)) {
894 		printf(": setup failed\n");
895 		return;
896 	}
897 
898 	printf(": ESS Technology ES%s [version 0x%04x]\n",
899 	       essmodel[sc->sc_model], sc->sc_version);
900 
901 	sc->sc_audio1.polled = sc->sc_audio1.irq == -1;
902 	if (!sc->sc_audio1.polled) {
903 		sc->sc_audio1.ih = isa_intr_establish(sc->sc_ic,
904 		    sc->sc_audio1.irq, sc->sc_audio1.ist, IPL_AUDIO,
905 		    ess_audio1_intr, sc, sc->sc_dev.dv_xname);
906 		printf("%s: audio1 interrupting at irq %d\n",
907 		    sc->sc_dev.dv_xname, sc->sc_audio1.irq);
908 	} else
909 		printf("%s: audio1 polled\n", sc->sc_dev.dv_xname);
910 	if (isa_dmamap_create(sc->sc_isa, sc->sc_audio1.drq,
911 	    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
912 		printf("%s: can't create map for drq %d\n",
913 		       sc->sc_dev.dv_xname, sc->sc_audio1.drq);
914 		return;
915 	}
916 
917 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
918 		sc->sc_audio2.polled = sc->sc_audio2.irq == -1;
919 		if (!sc->sc_audio2.polled) {
920 			sc->sc_audio2.ih = isa_intr_establish(sc->sc_ic,
921 			    sc->sc_audio2.irq, sc->sc_audio2.ist, IPL_AUDIO,
922 			    ess_audio2_intr, sc, sc->sc_dev.dv_xname);
923 			printf("%s: audio2 interrupting at irq %d\n",
924 			    sc->sc_dev.dv_xname, sc->sc_audio2.irq);
925 		} else
926 			printf("%s: audio2 polled\n", sc->sc_dev.dv_xname);
927 		if (isa_dmamap_create(sc->sc_isa, sc->sc_audio2.drq,
928 		    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
929 			printf("%s: can't create map for drq %d\n",
930 			       sc->sc_dev.dv_xname, sc->sc_audio2.drq);
931 			return;
932 		}
933 	}
934 
935 	/*
936 	 * Set record and play parameters to default values defined in
937 	 * generic audio driver.
938 	 */
939 	pparams = audio_default;
940 	rparams = audio_default;
941 	ess_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
942 
943 	/* Do a hardware reset on the mixer. */
944 	ess_write_mix_reg(sc, ESS_MIX_RESET, ESS_MIX_RESET);
945 
946 	/*
947 	 * Set volume of Audio 1 to zero and disable Audio 1 DAC input
948 	 * to playback mixer, since playback is always through Audio 2.
949 	 */
950 	if (!ESS_USE_AUDIO1(sc->sc_model))
951 		ess_write_mix_reg(sc, ESS_MREG_VOLUME_VOICE, 0);
952 	ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR);
953 
954 	if (ESS_USE_AUDIO1(sc->sc_model)) {
955 		ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIC);
956 		sc->in_port = ESS_SOURCE_MIC;
957 		sc->ndevs = ESS_1788_NDEVS;
958 	} else {
959 		/*
960 		 * Set hardware record source to use output of the record
961 		 * mixer. We do the selection of record source in software by
962 		 * setting the gain of the unused sources to zero. (See
963 		 * ess_set_in_ports.)
964 		 */
965 		ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIXER);
966 		sc->in_mask = 1 << ESS_MIC_REC_VOL;
967 		sc->ndevs = ESS_1888_NDEVS;
968 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x10);
969 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x08);
970 	}
971 
972 	/*
973 	 * Set gain on each mixer device to a sensible value.
974 	 * Devices not normally used are turned off, and other devices
975 	 * are set to 50% volume.
976 	 */
977 	for (i = 0; i < sc->ndevs; i++) {
978 		switch (i) {
979 		case ESS_MIC_PLAY_VOL:
980 		case ESS_LINE_PLAY_VOL:
981 		case ESS_CD_PLAY_VOL:
982 		case ESS_AUXB_PLAY_VOL:
983 		case ESS_DAC_REC_VOL:
984 		case ESS_LINE_REC_VOL:
985 		case ESS_SYNTH_REC_VOL:
986 		case ESS_CD_REC_VOL:
987 		case ESS_AUXB_REC_VOL:
988 			v = 0;
989 			break;
990 		default:
991 			v = ESS_4BIT_GAIN(AUDIO_MAX_GAIN / 2);
992 			break;
993 		}
994 		sc->gain[i][ESS_LEFT] = sc->gain[i][ESS_RIGHT] = v;
995 		ess_set_gain(sc, i, 1);
996 	}
997 
998 	ess_setup(sc);
999 
1000 	/* Disable the speaker until the device is opened.  */
1001 	ess_speaker_off(sc);
1002 	sc->spkr_state = SPKR_OFF;
1003 
1004 	sprintf(ess_device.name, "ES%s", essmodel[sc->sc_model]);
1005 	sprintf(ess_device.version, "0x%04x", sc->sc_version);
1006 
1007 	if (ESS_USE_AUDIO1(sc->sc_model))
1008 		audio_attach_mi(&ess_1788_hw_if, sc, &sc->sc_dev);
1009 	else
1010 		audio_attach_mi(&ess_1888_hw_if, sc, &sc->sc_dev);
1011 
1012 	arg.type = AUDIODEV_TYPE_OPL;
1013 	arg.hwif = 0;
1014 	arg.hdl = 0;
1015 	(void)config_found(&sc->sc_dev, &arg, audioprint);
1016 
1017 #ifdef AUDIO_DEBUG
1018 	if (essdebug > 0)
1019 		ess_printsc(sc);
1020 #endif
1021 }
1022 
1023 /*
1024  * Various routines to interface to higher level audio driver
1025  */
1026 
1027 int
1028 ess_open(addr, flags)
1029 	void *addr;
1030 	int flags;
1031 {
1032 	struct ess_softc *sc = addr;
1033 
1034 	DPRINTF(("ess_open: sc=%p\n", sc));
1035 
1036 	if (sc->sc_open != 0 || ess_reset(sc) != 0)
1037 		return ENXIO;
1038 
1039 	ess_setup(sc);		/* because we did a reset */
1040 
1041 	sc->sc_open = 1;
1042 
1043 	DPRINTF(("ess_open: opened\n"));
1044 
1045 	return (0);
1046 }
1047 
1048 void
1049 ess_1788_close(addr)
1050 	void *addr;
1051 {
1052 	struct ess_softc *sc = addr;
1053 
1054 	DPRINTF(("ess_1788_close: sc=%p\n", sc));
1055 
1056 	ess_speaker_off(sc);
1057 	sc->spkr_state = SPKR_OFF;
1058 
1059 	ess_audio1_halt(sc);
1060 
1061 	sc->sc_open = 0;
1062 	DPRINTF(("ess_1788_close: closed\n"));
1063 }
1064 
1065 void
1066 ess_1888_close(addr)
1067 	void *addr;
1068 {
1069 	struct ess_softc *sc = addr;
1070 
1071 	DPRINTF(("ess_1888_close: sc=%p\n", sc));
1072 
1073 	ess_speaker_off(sc);
1074 	sc->spkr_state = SPKR_OFF;
1075 
1076 	ess_audio1_halt(sc);
1077 	ess_audio2_halt(sc);
1078 
1079 	sc->sc_open = 0;
1080 	DPRINTF(("ess_1888_close: closed\n"));
1081 }
1082 
1083 /*
1084  * Wait for FIFO to drain, and analog section to settle.
1085  * XXX should check FIFO empty bit.
1086  */
1087 int
1088 ess_drain(addr)
1089 	void *addr;
1090 {
1091 	tsleep(addr, PWAIT | PCATCH, "essdr", hz/20); /* XXX */
1092 	return (0);
1093 }
1094 
1095 /* XXX should use reference count */
1096 int
1097 ess_speaker_ctl(addr, newstate)
1098 	void *addr;
1099 	int newstate;
1100 {
1101 	struct ess_softc *sc = addr;
1102 
1103 	if ((newstate == SPKR_ON) && (sc->spkr_state == SPKR_OFF)) {
1104 		ess_speaker_on(sc);
1105 		sc->spkr_state = SPKR_ON;
1106 	}
1107 	if ((newstate == SPKR_OFF) && (sc->spkr_state == SPKR_ON)) {
1108 		ess_speaker_off(sc);
1109 		sc->spkr_state = SPKR_OFF;
1110 	}
1111 	return (0);
1112 }
1113 
1114 int
1115 ess_getdev(addr, retp)
1116 	void *addr;
1117 	struct audio_device *retp;
1118 {
1119 	*retp = ess_device;
1120 	return (0);
1121 }
1122 
1123 int
1124 ess_query_encoding(addr, fp)
1125 	void *addr;
1126 	struct audio_encoding *fp;
1127 {
1128 	/*struct ess_softc *sc = addr;*/
1129 
1130 	switch (fp->index) {
1131 	case 0:
1132 		strcpy(fp->name, AudioEulinear);
1133 		fp->encoding = AUDIO_ENCODING_ULINEAR;
1134 		fp->precision = 8;
1135 		fp->flags = 0;
1136 		return (0);
1137 	case 1:
1138 		strcpy(fp->name, AudioEmulaw);
1139 		fp->encoding = AUDIO_ENCODING_ULAW;
1140 		fp->precision = 8;
1141 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1142 		return (0);
1143 	case 2:
1144 		strcpy(fp->name, AudioEalaw);
1145 		fp->encoding = AUDIO_ENCODING_ALAW;
1146 		fp->precision = 8;
1147 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1148 		return (0);
1149 	case 3:
1150 		strcpy(fp->name, AudioEslinear);
1151 		fp->encoding = AUDIO_ENCODING_SLINEAR;
1152 		fp->precision = 8;
1153 		fp->flags = 0;
1154 		return (0);
1155 	case 4:
1156 		strcpy(fp->name, AudioEslinear_le);
1157 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
1158 		fp->precision = 16;
1159 		fp->flags = 0;
1160 		return (0);
1161 	case 5:
1162 		strcpy(fp->name, AudioEulinear_le);
1163 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
1164 		fp->precision = 16;
1165 		fp->flags = 0;
1166 		return (0);
1167 	case 6:
1168 		strcpy(fp->name, AudioEslinear_be);
1169 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1170 		fp->precision = 16;
1171 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1172 		return (0);
1173 	case 7:
1174 		strcpy(fp->name, AudioEulinear_be);
1175 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1176 		fp->precision = 16;
1177 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1178 		return (0);
1179 	default:
1180 		return EINVAL;
1181 	}
1182 	return (0);
1183 }
1184 
1185 int
1186 ess_set_params(addr, setmode, usemode, play, rec)
1187 	void *addr;
1188 	int setmode, usemode;
1189 	struct audio_params *play, *rec;
1190 {
1191 	struct ess_softc *sc = addr;
1192 	struct audio_params *p;
1193 	int mode;
1194 	int rate;
1195 
1196 	DPRINTF(("ess_set_params: set=%d use=%d\n", setmode, usemode));
1197 
1198 	/*
1199 	 * The ES1887 manual (page 39, `Full-Duplex DMA Mode') claims that in
1200 	 * full-duplex operation the sample rates must be the same for both
1201 	 * channels.  This appears to be false; the only bit in common is the
1202 	 * clock source selection.  However, we'll be conservative here.
1203 	 * - mycroft
1204 	 */
1205 	if (play->sample_rate != rec->sample_rate &&
1206 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1207 		if (setmode == AUMODE_PLAY) {
1208 			rec->sample_rate = play->sample_rate;
1209 			setmode |= AUMODE_RECORD;
1210 		} else if (setmode == AUMODE_RECORD) {
1211 			play->sample_rate = rec->sample_rate;
1212 			setmode |= AUMODE_PLAY;
1213 		} else
1214 			return (EINVAL);
1215 	}
1216 
1217 	for (mode = AUMODE_RECORD; mode != -1;
1218 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1219 		if ((setmode & mode) == 0)
1220 			continue;
1221 
1222 		p = mode == AUMODE_PLAY ? play : rec;
1223 
1224 		if (p->sample_rate < ESS_MINRATE ||
1225 		    p->sample_rate > ESS_MAXRATE ||
1226 		    (p->precision != 8 && p->precision != 16) ||
1227 		    (p->channels != 1 && p->channels != 2))
1228 			return (EINVAL);
1229 
1230 		p->factor = 1;
1231 		p->sw_code = 0;
1232 		switch (p->encoding) {
1233 		case AUDIO_ENCODING_SLINEAR_BE:
1234 		case AUDIO_ENCODING_ULINEAR_BE:
1235 			if (p->precision == 16)
1236 				p->sw_code = swap_bytes;
1237 			break;
1238 		case AUDIO_ENCODING_SLINEAR_LE:
1239 		case AUDIO_ENCODING_ULINEAR_LE:
1240 			break;
1241 		case AUDIO_ENCODING_ULAW:
1242 			if (mode == AUMODE_PLAY) {
1243 				p->factor = 2;
1244 				p->sw_code = mulaw_to_ulinear16;
1245 			} else
1246 				p->sw_code = ulinear8_to_mulaw;
1247 			break;
1248 		case AUDIO_ENCODING_ALAW:
1249 			if (mode == AUMODE_PLAY) {
1250 				p->factor = 2;
1251 				p->sw_code = alaw_to_ulinear16;
1252 			} else
1253 				p->sw_code = ulinear8_to_alaw;
1254 			break;
1255 		default:
1256 			return (EINVAL);
1257 		}
1258 	}
1259 
1260 	if (usemode == AUMODE_RECORD)
1261 		rate = rec->sample_rate;
1262 	else
1263 		rate = play->sample_rate;
1264 
1265 	ess_write_x_reg(sc, ESS_XCMD_SAMPLE_RATE, ess_srtotc(rate));
1266 	ess_write_x_reg(sc, ESS_XCMD_FILTER_CLOCK, ess_srtofc(rate));
1267 
1268 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
1269 		ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, ess_srtotc(rate));
1270 		ess_write_mix_reg(sc, ESS_MREG_FILTER_CLOCK, ess_srtofc(rate));
1271 	}
1272 
1273 	return (0);
1274 }
1275 
1276 int
1277 ess_audio1_trigger_output(addr, start, end, blksize, intr, arg, param)
1278 	void *addr;
1279 	void *start, *end;
1280 	int blksize;
1281 	void (*intr) __P((void *));
1282 	void *arg;
1283 	struct audio_params *param;
1284 {
1285 	struct ess_softc *sc = addr;
1286 	u_int8_t reg;
1287 
1288 	DPRINTFN(1, ("ess_audio1_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1289 	    addr, start, end, blksize, intr, arg));
1290 
1291 	if (sc->sc_audio1.active)
1292 		panic("ess_audio1_trigger_output: already running");
1293 
1294 	sc->sc_audio1.active = 1;
1295 	sc->sc_audio1.intr = intr;
1296 	sc->sc_audio1.arg = arg;
1297 	if (sc->sc_audio1.polled) {
1298 		sc->sc_audio1.dmapos = 0;
1299 		sc->sc_audio1.buffersize = (char *)end - (char *)start;
1300 		sc->sc_audio1.dmacount = 0;
1301 		sc->sc_audio1.blksize = blksize;
1302 		timeout(ess_audio1_poll, sc, hz/30);
1303 	}
1304 
1305 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
1306 	if (param->channels == 2) {
1307 		reg &= ~ESS_AUDIO_CTRL_MONO;
1308 		reg |= ESS_AUDIO_CTRL_STEREO;
1309 	} else {
1310 		reg |= ESS_AUDIO_CTRL_MONO;
1311 		reg &= ~ESS_AUDIO_CTRL_STEREO;
1312 	}
1313 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
1314 
1315 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
1316 	if (param->precision * param->factor == 16)
1317 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
1318 	else
1319 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
1320 	if (param->channels == 2)
1321 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
1322 	else
1323 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
1324 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1325 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1326 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1327 	else
1328 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1329 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
1330 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
1331 
1332 	isa_dmastart(sc->sc_isa, sc->sc_audio1.drq, start,
1333 		     (char *)end - (char *)start, NULL,
1334 	    DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1335 
1336 	/* Program transfer count registers with 2's complement of count. */
1337 	blksize = -blksize;
1338 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
1339 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
1340 
1341 	/* Use 4 bytes per output DMA. */
1342 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
1343 
1344 	/* Start auto-init DMA */
1345   	ess_wdsp(sc, ESS_ACMD_ENABLE_SPKR);
1346 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
1347 	reg &= ~(ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE);
1348 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
1349 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
1350 
1351 	return (0);
1352 }
1353 
1354 int
1355 ess_audio2_trigger_output(addr, start, end, blksize, intr, arg, param)
1356 	void *addr;
1357 	void *start, *end;
1358 	int blksize;
1359 	void (*intr) __P((void *));
1360 	void *arg;
1361 	struct audio_params *param;
1362 {
1363 	struct ess_softc *sc = addr;
1364 	u_int8_t reg;
1365 
1366 	DPRINTFN(1, ("ess_audio2_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1367 	    addr, start, end, blksize, intr, arg));
1368 
1369 	if (sc->sc_audio2.active)
1370 		panic("ess_audio2_trigger_output: already running");
1371 
1372 	sc->sc_audio2.active = 1;
1373 	sc->sc_audio2.intr = intr;
1374 	sc->sc_audio2.arg = arg;
1375 	if (sc->sc_audio2.polled) {
1376 		sc->sc_audio2.dmapos = 0;
1377 		sc->sc_audio2.buffersize = (char *)end - (char *)start;
1378 		sc->sc_audio2.dmacount = 0;
1379 		sc->sc_audio2.blksize = blksize;
1380 		timeout(ess_audio2_poll, sc, hz/30);
1381 	}
1382 
1383 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
1384 	if (param->precision * param->factor == 16)
1385 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIZE;
1386 	else
1387 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIZE;
1388 	if (param->channels == 2)
1389 		reg |= ESS_AUDIO2_CTRL2_CHANNELS;
1390 	else
1391 		reg &= ~ESS_AUDIO2_CTRL2_CHANNELS;
1392 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1393 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1394 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIGNED;
1395 	else
1396 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIGNED;
1397 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
1398 
1399 	isa_dmastart(sc->sc_isa, sc->sc_audio2.drq, start,
1400 		     (char *)end - (char *)start, NULL,
1401 	    DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1402 
1403 	if (IS16BITDRQ(sc->sc_audio2.drq))
1404 		blksize >>= 1;	/* use word count for 16 bit DMA */
1405 	/* Program transfer count registers with 2's complement of count. */
1406 	blksize = -blksize;
1407 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTLO, blksize);
1408 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTHI, blksize >> 8);
1409 
1410 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1);
1411 	if (IS16BITDRQ(sc->sc_audio2.drq))
1412 		reg |= ESS_AUDIO2_CTRL1_XFER_SIZE;
1413 	else
1414 		reg &= ~ESS_AUDIO2_CTRL1_XFER_SIZE;
1415 	reg |= ESS_AUDIO2_CTRL1_DEMAND_8;
1416 	reg |= ESS_AUDIO2_CTRL1_DAC_ENABLE | ESS_AUDIO2_CTRL1_FIFO_ENABLE |
1417 	       ESS_AUDIO2_CTRL1_AUTO_INIT;
1418 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1, reg);
1419 
1420 	return (0);
1421 }
1422 
1423 int
1424 ess_audio1_trigger_input(addr, start, end, blksize, intr, arg, param)
1425 	void *addr;
1426 	void *start, *end;
1427 	int blksize;
1428 	void (*intr) __P((void *));
1429 	void *arg;
1430 	struct audio_params *param;
1431 {
1432 	struct ess_softc *sc = addr;
1433 	u_int8_t reg;
1434 
1435 	DPRINTFN(1, ("ess_audio1_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1436 	    addr, start, end, blksize, intr, arg));
1437 
1438 	if (sc->sc_audio1.active)
1439 		panic("ess_audio1_trigger_input: already running");
1440 
1441 	sc->sc_audio1.active = 1;
1442 	sc->sc_audio1.intr = intr;
1443 	sc->sc_audio1.arg = arg;
1444 	if (sc->sc_audio1.polled) {
1445 		sc->sc_audio1.dmapos = 0;
1446 		sc->sc_audio1.buffersize = (char *)end - (char *)start;
1447 		sc->sc_audio1.dmacount = 0;
1448 		sc->sc_audio1.blksize = blksize;
1449 		timeout(ess_audio1_poll, sc, hz/30);
1450 	}
1451 
1452 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
1453 	if (param->channels == 2) {
1454 		reg &= ~ESS_AUDIO_CTRL_MONO;
1455 		reg |= ESS_AUDIO_CTRL_STEREO;
1456 	} else {
1457 		reg |= ESS_AUDIO_CTRL_MONO;
1458 		reg &= ~ESS_AUDIO_CTRL_STEREO;
1459 	}
1460 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
1461 
1462 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
1463 	if (param->precision * param->factor == 16)
1464 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
1465 	else
1466 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
1467 	if (param->channels == 2)
1468 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
1469 	else
1470 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
1471 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1472 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1473 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1474 	else
1475 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1476 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
1477 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
1478 
1479 	isa_dmastart(sc->sc_isa, sc->sc_audio1.drq, start,
1480 		     (char *)end - (char *)start, NULL,
1481 	    DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1482 
1483 	/* Program transfer count registers with 2's complement of count. */
1484 	blksize = -blksize;
1485 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
1486 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
1487 
1488 	/* Use 4 bytes per input DMA. */
1489 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
1490 
1491 	/* Start auto-init DMA */
1492   	ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR);
1493 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
1494 	reg |= ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE;
1495 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
1496 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
1497 
1498 	return (0);
1499 }
1500 
1501 int
1502 ess_audio1_halt(addr)
1503 	void *addr;
1504 {
1505 	struct ess_softc *sc = addr;
1506 
1507 	DPRINTF(("ess_audio1_halt: sc=%p\n", sc));
1508 
1509 	if (sc->sc_audio1.active) {
1510 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2,
1511 		    ESS_AUDIO1_CTRL2_FIFO_ENABLE);
1512 		isa_dmaabort(sc->sc_isa, sc->sc_audio1.drq);
1513 		if (sc->sc_audio1.polled)
1514 			untimeout(ess_audio1_poll, sc);
1515 		sc->sc_audio1.active = 0;
1516 	}
1517 
1518 	return (0);
1519 }
1520 
1521 int
1522 ess_audio2_halt(addr)
1523 	void *addr;
1524 {
1525 	struct ess_softc *sc = addr;
1526 
1527 	DPRINTF(("ess_audio2_halt: sc=%p\n", sc));
1528 
1529 	if (sc->sc_audio2.active) {
1530 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
1531 		    ESS_AUDIO2_CTRL1_DAC_ENABLE |
1532 		    ESS_AUDIO2_CTRL1_FIFO_ENABLE);
1533 		isa_dmaabort(sc->sc_isa, sc->sc_audio2.drq);
1534 		if (sc->sc_audio2.polled)
1535 			untimeout(ess_audio2_poll, sc);
1536 		sc->sc_audio2.active = 0;
1537 	}
1538 
1539 	return (0);
1540 }
1541 
1542 int
1543 ess_audio1_intr(arg)
1544 	void *arg;
1545 {
1546 	struct ess_softc *sc = arg;
1547 	u_int8_t reg;
1548 
1549 	DPRINTFN(1,("ess_audio1_intr: intr=%p\n", sc->sc_audio1.intr));
1550 
1551 	/* Check and clear interrupt on Audio1. */
1552 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS);
1553 	if ((reg & ESS_DSP_READ_OFLOW) == 0)
1554 		return (0);
1555 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_CLEAR_INTR);
1556 
1557 	sc->sc_audio1.nintr++;
1558 
1559 	if (sc->sc_audio1.active) {
1560 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
1561 		return (1);
1562 	} else
1563 		return (0);
1564 }
1565 
1566 int
1567 ess_audio2_intr(arg)
1568 	void *arg;
1569 {
1570 	struct ess_softc *sc = arg;
1571 	u_int8_t reg;
1572 
1573 	DPRINTFN(1,("ess_audio2_intr: intr=%p\n", sc->sc_audio2.intr));
1574 
1575 	/* Check and clear interrupt on Audio2. */
1576 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
1577 	if ((reg & ESS_AUDIO2_CTRL2_IRQ_LATCH) == 0)
1578 		return (0);
1579 	reg &= ~ESS_AUDIO2_CTRL2_IRQ_LATCH;
1580 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
1581 
1582 	sc->sc_audio2.nintr++;
1583 
1584 	if (sc->sc_audio2.active) {
1585 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
1586 		return (1);
1587 	} else
1588 		return (0);
1589 }
1590 
1591 void
1592 ess_audio1_poll(addr)
1593 	void *addr;
1594 {
1595 	struct ess_softc *sc = addr;
1596 	int dmapos, dmacount;
1597 
1598 	if (!sc->sc_audio1.active)
1599 		return;
1600 
1601 	sc->sc_audio1.nintr++;
1602 
1603 	dmapos = isa_dmacount(sc->sc_isa, sc->sc_audio1.drq);
1604 	dmacount = sc->sc_audio1.dmapos - dmapos;
1605 	if (dmacount < 0)
1606 		dmacount += sc->sc_audio1.buffersize;
1607 	sc->sc_audio1.dmapos = dmapos;
1608 #if 1
1609 	dmacount += sc->sc_audio1.dmacount;
1610 	while (dmacount > sc->sc_audio1.blksize) {
1611 		dmacount -= sc->sc_audio1.blksize;
1612 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
1613 	}
1614 	sc->sc_audio1.dmacount = dmacount;
1615 #else
1616 	(*sc->sc_audio1.intr)(sc->sc_audio1.arg, dmacount);
1617 #endif
1618 
1619 	timeout(ess_audio1_poll, sc, hz/30);
1620 }
1621 
1622 void
1623 ess_audio2_poll(addr)
1624 	void *addr;
1625 {
1626 	struct ess_softc *sc = addr;
1627 	int dmapos, dmacount;
1628 
1629 	if (!sc->sc_audio2.active)
1630 		return;
1631 
1632 	sc->sc_audio2.nintr++;
1633 
1634 	dmapos = isa_dmacount(sc->sc_isa, sc->sc_audio2.drq);
1635 	dmacount = sc->sc_audio2.dmapos - dmapos;
1636 	if (dmacount < 0)
1637 		dmacount += sc->sc_audio2.buffersize;
1638 	sc->sc_audio2.dmapos = dmapos;
1639 #if 1
1640 	dmacount += sc->sc_audio2.dmacount;
1641 	while (dmacount > sc->sc_audio2.blksize) {
1642 		dmacount -= sc->sc_audio2.blksize;
1643 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
1644 	}
1645 	sc->sc_audio2.dmacount = dmacount;
1646 #else
1647 	(*sc->sc_audio2.intr)(sc->sc_audio2.arg, dmacount);
1648 #endif
1649 
1650 	timeout(ess_audio2_poll, sc, hz/30);
1651 }
1652 
1653 int
1654 ess_round_blocksize(addr, blk)
1655 	void *addr;
1656 	int blk;
1657 {
1658 	return (blk & -8);	/* round for max DMA size */
1659 }
1660 
1661 int
1662 ess_set_port(addr, cp)
1663 	void *addr;
1664 	mixer_ctrl_t *cp;
1665 {
1666 	struct ess_softc *sc = addr;
1667 	int lgain, rgain;
1668 
1669 	DPRINTFN(5,("ess_set_port: port=%d num_channels=%d\n",
1670 		    cp->dev, cp->un.value.num_channels));
1671 
1672 	switch (cp->dev) {
1673 	/*
1674 	 * The following mixer ports are all stereo. If we get a
1675 	 * single-channel gain value passed in, then we duplicate it
1676 	 * to both left and right channels.
1677 	 */
1678 	case ESS_MASTER_VOL:
1679 	case ESS_DAC_PLAY_VOL:
1680 	case ESS_MIC_PLAY_VOL:
1681 	case ESS_LINE_PLAY_VOL:
1682 	case ESS_SYNTH_PLAY_VOL:
1683 	case ESS_CD_PLAY_VOL:
1684 	case ESS_AUXB_PLAY_VOL:
1685 	case ESS_RECORD_VOL:
1686 		if (cp->type != AUDIO_MIXER_VALUE)
1687 			return EINVAL;
1688 
1689 		switch (cp->un.value.num_channels) {
1690 		case 1:
1691 			lgain = rgain = ESS_4BIT_GAIN(
1692 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1693 			break;
1694 		case 2:
1695 			lgain = ESS_4BIT_GAIN(
1696 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1697 			rgain = ESS_4BIT_GAIN(
1698 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1699 			break;
1700 		default:
1701 			return EINVAL;
1702 		}
1703 
1704 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
1705 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
1706 		ess_set_gain(sc, cp->dev, 1);
1707 		return (0);
1708 
1709 	/*
1710 	 * The PC speaker port is mono. If we get a stereo gain value
1711 	 * passed in, then we return EINVAL.
1712 	 */
1713 	case ESS_PCSPEAKER_VOL:
1714 		if (cp->un.value.num_channels != 1)
1715 			return EINVAL;
1716 
1717 		sc->gain[cp->dev][ESS_LEFT] = sc->gain[cp->dev][ESS_RIGHT] =
1718 		  ESS_3BIT_GAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1719 		ess_set_gain(sc, cp->dev, 1);
1720 		return (0);
1721 
1722 	case ESS_RECORD_SOURCE:
1723 		if (ESS_USE_AUDIO1(sc->sc_model)) {
1724 			if (cp->type == AUDIO_MIXER_ENUM)
1725 				return (ess_set_in_port(sc, cp->un.ord));
1726 			else
1727 				return (EINVAL);
1728 		} else {
1729 			if (cp->type == AUDIO_MIXER_SET)
1730 				return (ess_set_in_ports(sc, cp->un.mask));
1731 			else
1732 				return (EINVAL);
1733 		}
1734 		return (0);
1735 
1736 	case ESS_RECORD_MONITOR:
1737 		if (cp->type != AUDIO_MIXER_ENUM)
1738 			return EINVAL;
1739 
1740 		if (cp->un.ord)
1741 			/* Enable monitor */
1742 			ess_set_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
1743 					  ESS_AUDIO_CTRL_MONITOR);
1744 		else
1745 			/* Disable monitor */
1746 			ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
1747 					    ESS_AUDIO_CTRL_MONITOR);
1748 		return (0);
1749 	}
1750 
1751 	if (ESS_USE_AUDIO1(sc->sc_model))
1752 		return (EINVAL);
1753 
1754 	switch (cp->dev) {
1755 	case ESS_DAC_REC_VOL:
1756 	case ESS_MIC_REC_VOL:
1757 	case ESS_LINE_REC_VOL:
1758 	case ESS_SYNTH_REC_VOL:
1759 	case ESS_CD_REC_VOL:
1760 	case ESS_AUXB_REC_VOL:
1761 		if (cp->type != AUDIO_MIXER_VALUE)
1762 			return EINVAL;
1763 
1764 		switch (cp->un.value.num_channels) {
1765 		case 1:
1766 			lgain = rgain = ESS_4BIT_GAIN(
1767 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1768 			break;
1769 		case 2:
1770 			lgain = ESS_4BIT_GAIN(
1771 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1772 			rgain = ESS_4BIT_GAIN(
1773 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1774 			break;
1775 		default:
1776 			return EINVAL;
1777 		}
1778 
1779 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
1780 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
1781 		ess_set_gain(sc, cp->dev, 1);
1782 		return (0);
1783 
1784 	case ESS_MIC_PREAMP:
1785 		if (cp->type != AUDIO_MIXER_ENUM)
1786 			return EINVAL;
1787 
1788 		if (cp->un.ord)
1789 			/* Enable microphone preamp */
1790 			ess_set_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
1791 					  ESS_PREAMP_CTRL_ENABLE);
1792 		else
1793 			/* Disable microphone preamp */
1794 			ess_clear_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
1795 					  ESS_PREAMP_CTRL_ENABLE);
1796 		return (0);
1797 	}
1798 
1799 	return (EINVAL);
1800 }
1801 
1802 int
1803 ess_get_port(addr, cp)
1804 	void *addr;
1805 	mixer_ctrl_t *cp;
1806 {
1807 	struct ess_softc *sc = addr;
1808 
1809 	DPRINTFN(5,("ess_get_port: port=%d\n", cp->dev));
1810 
1811 	switch (cp->dev) {
1812 	case ESS_MASTER_VOL:
1813 	case ESS_DAC_PLAY_VOL:
1814 	case ESS_MIC_PLAY_VOL:
1815 	case ESS_LINE_PLAY_VOL:
1816 	case ESS_SYNTH_PLAY_VOL:
1817 	case ESS_CD_PLAY_VOL:
1818 	case ESS_AUXB_PLAY_VOL:
1819 	case ESS_RECORD_VOL:
1820 		switch (cp->un.value.num_channels) {
1821 		case 1:
1822 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1823 				sc->gain[cp->dev][ESS_LEFT];
1824 			break;
1825 		case 2:
1826 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1827 				sc->gain[cp->dev][ESS_LEFT];
1828 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1829 				sc->gain[cp->dev][ESS_RIGHT];
1830 			break;
1831 		default:
1832 			return EINVAL;
1833 		}
1834 		return (0);
1835 
1836 	case ESS_PCSPEAKER_VOL:
1837 		if (cp->un.value.num_channels != 1)
1838 			return EINVAL;
1839 
1840 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1841 			sc->gain[cp->dev][ESS_LEFT];
1842 		return (0);
1843 
1844 	case ESS_RECORD_SOURCE:
1845 		if (ESS_USE_AUDIO1(sc->sc_model))
1846 			cp->un.ord = sc->in_port;
1847 		else
1848 			cp->un.mask = sc->in_mask;
1849 		return (0);
1850 
1851 	case ESS_RECORD_MONITOR:
1852 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) &
1853 			      ESS_AUDIO_CTRL_MONITOR) ? 1 : 0;
1854 		return (0);
1855 	}
1856 
1857 	if (ESS_USE_AUDIO1(sc->sc_model))
1858 		return (EINVAL);
1859 
1860 	switch (cp->dev) {
1861 	case ESS_DAC_REC_VOL:
1862 	case ESS_MIC_REC_VOL:
1863 	case ESS_LINE_REC_VOL:
1864 	case ESS_SYNTH_REC_VOL:
1865 	case ESS_CD_REC_VOL:
1866 	case ESS_AUXB_REC_VOL:
1867 		switch (cp->un.value.num_channels) {
1868 		case 1:
1869 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1870 				sc->gain[cp->dev][ESS_LEFT];
1871 			break;
1872 		case 2:
1873 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1874 				sc->gain[cp->dev][ESS_LEFT];
1875 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1876 				sc->gain[cp->dev][ESS_RIGHT];
1877 			break;
1878 		default:
1879 			return EINVAL;
1880 		}
1881 		return (0);
1882 
1883 	case ESS_MIC_PREAMP:
1884 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL) &
1885 			      ESS_PREAMP_CTRL_ENABLE) ? 1 : 0;
1886 		return (0);
1887 	}
1888 
1889 	return (EINVAL);
1890 }
1891 
1892 int
1893 ess_query_devinfo(addr, dip)
1894 	void *addr;
1895 	mixer_devinfo_t *dip;
1896 {
1897 	struct ess_softc *sc = addr;
1898 
1899 	DPRINTFN(5,("ess_query_devinfo: model=%d index=%d\n",
1900 		    sc->sc_model, dip->index));
1901 
1902 	/*
1903 	 * REVISIT: There are some slight differences between the
1904 	 *          mixers on the different ESS chips, which can
1905 	 *          be sorted out using the chip model rather than a
1906 	 *          separate mixer model.
1907 	 *          This is currently coded assuming an ES1887; we
1908 	 *          need to work out which bits are not applicable to
1909 	 *          the other models (1888 and 888).
1910 	 */
1911 	switch (dip->index) {
1912 	case ESS_DAC_PLAY_VOL:
1913 		dip->mixer_class = ESS_INPUT_CLASS;
1914 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1915 		strcpy(dip->label.name, AudioNdac);
1916 		dip->type = AUDIO_MIXER_VALUE;
1917 		dip->un.v.num_channels = 2;
1918 		strcpy(dip->un.v.units.name, AudioNvolume);
1919 		return (0);
1920 
1921 	case ESS_MIC_PLAY_VOL:
1922 		dip->mixer_class = ESS_INPUT_CLASS;
1923 		dip->prev = AUDIO_MIXER_LAST;
1924 		if (ESS_USE_AUDIO1(sc->sc_model))
1925 			dip->next = AUDIO_MIXER_LAST;
1926 		else
1927 			dip->next = ESS_MIC_PREAMP;
1928 		strcpy(dip->label.name, AudioNmicrophone);
1929 		dip->type = AUDIO_MIXER_VALUE;
1930 		dip->un.v.num_channels = 2;
1931 		strcpy(dip->un.v.units.name, AudioNvolume);
1932 		return (0);
1933 
1934 	case ESS_LINE_PLAY_VOL:
1935 		dip->mixer_class = ESS_INPUT_CLASS;
1936 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1937 		strcpy(dip->label.name, AudioNline);
1938 		dip->type = AUDIO_MIXER_VALUE;
1939 		dip->un.v.num_channels = 2;
1940 		strcpy(dip->un.v.units.name, AudioNvolume);
1941 		return (0);
1942 
1943 	case ESS_SYNTH_PLAY_VOL:
1944 		dip->mixer_class = ESS_INPUT_CLASS;
1945 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1946 		strcpy(dip->label.name, AudioNfmsynth);
1947 		dip->type = AUDIO_MIXER_VALUE;
1948 		dip->un.v.num_channels = 2;
1949 		strcpy(dip->un.v.units.name, AudioNvolume);
1950 		return (0);
1951 
1952 	case ESS_CD_PLAY_VOL:
1953 		dip->mixer_class = ESS_INPUT_CLASS;
1954 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1955 		strcpy(dip->label.name, AudioNcd);
1956 		dip->type = AUDIO_MIXER_VALUE;
1957 		dip->un.v.num_channels = 2;
1958 		strcpy(dip->un.v.units.name, AudioNvolume);
1959 		return (0);
1960 
1961 	case ESS_AUXB_PLAY_VOL:
1962 		dip->mixer_class = ESS_INPUT_CLASS;
1963 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1964 		strcpy(dip->label.name, "auxb");
1965 		dip->type = AUDIO_MIXER_VALUE;
1966 		dip->un.v.num_channels = 2;
1967 		strcpy(dip->un.v.units.name, AudioNvolume);
1968 		return (0);
1969 
1970 	case ESS_INPUT_CLASS:
1971 		dip->mixer_class = ESS_INPUT_CLASS;
1972 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1973 		strcpy(dip->label.name, AudioCinputs);
1974 		dip->type = AUDIO_MIXER_CLASS;
1975 		return (0);
1976 
1977 	case ESS_MASTER_VOL:
1978 		dip->mixer_class = ESS_OUTPUT_CLASS;
1979 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1980 		strcpy(dip->label.name, AudioNmaster);
1981 		dip->type = AUDIO_MIXER_VALUE;
1982 		dip->un.v.num_channels = 2;
1983 		strcpy(dip->un.v.units.name, AudioNvolume);
1984 		return (0);
1985 
1986 	case ESS_PCSPEAKER_VOL:
1987 		dip->mixer_class = ESS_OUTPUT_CLASS;
1988 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1989 		strcpy(dip->label.name, "pc_speaker");
1990 		dip->type = AUDIO_MIXER_VALUE;
1991 		dip->un.v.num_channels = 1;
1992 		strcpy(dip->un.v.units.name, AudioNvolume);
1993 		return (0);
1994 
1995 	case ESS_OUTPUT_CLASS:
1996 		dip->mixer_class = ESS_OUTPUT_CLASS;
1997 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1998 		strcpy(dip->label.name, AudioCoutputs);
1999 		dip->type = AUDIO_MIXER_CLASS;
2000 		return (0);
2001 
2002 	case ESS_RECORD_VOL:
2003 		dip->mixer_class = ESS_RECORD_CLASS;
2004 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2005 		strcpy(dip->label.name, AudioNrecord);
2006 		dip->type = AUDIO_MIXER_VALUE;
2007 		dip->un.v.num_channels = 2;
2008 		strcpy(dip->un.v.units.name, AudioNvolume);
2009 		return (0);
2010 
2011 	case ESS_RECORD_SOURCE:
2012 		dip->mixer_class = ESS_RECORD_CLASS;
2013 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2014 		strcpy(dip->label.name, AudioNsource);
2015 		if (ESS_USE_AUDIO1(sc->sc_model)) {
2016 			/*
2017 			 * The 1788 doesn't use the input mixer control that
2018 			 * the 1888 uses, because it's a pain when you only
2019 			 * have one mixer.
2020 			 * Perhaps it could be emulated by keeping both sets of
2021 			 * gain values, and doing a `context switch' of the
2022 			 * mixer registers when shifting from playing to
2023 			 * recording.
2024 			 */
2025 			dip->type = AUDIO_MIXER_ENUM;
2026 			dip->un.e.num_mem = 4;
2027 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
2028 			dip->un.e.member[0].ord = ESS_SOURCE_MIC;
2029 			strcpy(dip->un.e.member[1].label.name, AudioNline);
2030 			dip->un.e.member[1].ord = ESS_SOURCE_LINE;
2031 			strcpy(dip->un.e.member[2].label.name, AudioNcd);
2032 			dip->un.e.member[2].ord = ESS_SOURCE_CD;
2033 			strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
2034 			dip->un.e.member[3].ord = ESS_SOURCE_MIXER;
2035 		} else {
2036 			dip->type = AUDIO_MIXER_SET;
2037 			dip->un.s.num_mem = 6;
2038 			strcpy(dip->un.s.member[0].label.name, AudioNdac);
2039 			dip->un.s.member[0].mask = 1 << ESS_DAC_REC_VOL;
2040 			strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
2041 			dip->un.s.member[1].mask = 1 << ESS_MIC_REC_VOL;
2042 			strcpy(dip->un.s.member[2].label.name, AudioNline);
2043 			dip->un.s.member[2].mask = 1 << ESS_LINE_REC_VOL;
2044 			strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
2045 			dip->un.s.member[3].mask = 1 << ESS_SYNTH_REC_VOL;
2046 			strcpy(dip->un.s.member[4].label.name, AudioNcd);
2047 			dip->un.s.member[4].mask = 1 << ESS_CD_REC_VOL;
2048 			strcpy(dip->un.s.member[5].label.name, "auxb");
2049 			dip->un.s.member[5].mask = 1 << ESS_AUXB_REC_VOL;
2050 		}
2051 		return (0);
2052 
2053 	case ESS_RECORD_CLASS:
2054 		dip->mixer_class = ESS_RECORD_CLASS;
2055 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2056 		strcpy(dip->label.name, AudioCrecord);
2057 		dip->type = AUDIO_MIXER_CLASS;
2058 		return (0);
2059 
2060 	case ESS_RECORD_MONITOR:
2061 		dip->prev = dip->next = AUDIO_MIXER_LAST;
2062 		strcpy(dip->label.name, AudioNmute);
2063 		dip->type = AUDIO_MIXER_ENUM;
2064 		dip->mixer_class = ESS_MONITOR_CLASS;
2065 		dip->un.e.num_mem = 2;
2066 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
2067 		dip->un.e.member[0].ord = 0;
2068 		strcpy(dip->un.e.member[1].label.name, AudioNon);
2069 		dip->un.e.member[1].ord = 1;
2070 		return (0);
2071 
2072 	case ESS_MONITOR_CLASS:
2073 		dip->mixer_class = ESS_MONITOR_CLASS;
2074 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2075 		strcpy(dip->label.name, AudioCmonitor);
2076 		dip->type = AUDIO_MIXER_CLASS;
2077 		return (0);
2078 	}
2079 
2080 	if (ESS_USE_AUDIO1(sc->sc_model))
2081 		return (ENXIO);
2082 
2083 	switch (dip->index) {
2084 	case ESS_DAC_REC_VOL:
2085 		dip->mixer_class = ESS_RECORD_CLASS;
2086 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2087 		strcpy(dip->label.name, AudioNdac);
2088 		dip->type = AUDIO_MIXER_VALUE;
2089 		dip->un.v.num_channels = 2;
2090 		strcpy(dip->un.v.units.name, AudioNvolume);
2091 		return (0);
2092 
2093 	case ESS_MIC_REC_VOL:
2094 		dip->mixer_class = ESS_RECORD_CLASS;
2095 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2096 		strcpy(dip->label.name, AudioNmicrophone);
2097 		dip->type = AUDIO_MIXER_VALUE;
2098 		dip->un.v.num_channels = 2;
2099 		strcpy(dip->un.v.units.name, AudioNvolume);
2100 		return (0);
2101 
2102 	case ESS_LINE_REC_VOL:
2103 		dip->mixer_class = ESS_RECORD_CLASS;
2104 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2105 		strcpy(dip->label.name, AudioNline);
2106 		dip->type = AUDIO_MIXER_VALUE;
2107 		dip->un.v.num_channels = 2;
2108 		strcpy(dip->un.v.units.name, AudioNvolume);
2109 		return (0);
2110 
2111 	case ESS_SYNTH_REC_VOL:
2112 		dip->mixer_class = ESS_RECORD_CLASS;
2113 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2114 		strcpy(dip->label.name, AudioNfmsynth);
2115 		dip->type = AUDIO_MIXER_VALUE;
2116 		dip->un.v.num_channels = 2;
2117 		strcpy(dip->un.v.units.name, AudioNvolume);
2118 		return (0);
2119 
2120 	case ESS_CD_REC_VOL:
2121 		dip->mixer_class = ESS_RECORD_CLASS;
2122 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2123 		strcpy(dip->label.name, AudioNcd);
2124 		dip->type = AUDIO_MIXER_VALUE;
2125 		dip->un.v.num_channels = 2;
2126 		strcpy(dip->un.v.units.name, AudioNvolume);
2127 		return (0);
2128 
2129 	case ESS_AUXB_REC_VOL:
2130 		dip->mixer_class = ESS_RECORD_CLASS;
2131 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2132 		strcpy(dip->label.name, "auxb");
2133 		dip->type = AUDIO_MIXER_VALUE;
2134 		dip->un.v.num_channels = 2;
2135 		strcpy(dip->un.v.units.name, AudioNvolume);
2136 		return (0);
2137 
2138 	case ESS_MIC_PREAMP:
2139 		dip->mixer_class = ESS_INPUT_CLASS;
2140 		dip->prev = ESS_MIC_PLAY_VOL;
2141 		dip->next = AUDIO_MIXER_LAST;
2142 		strcpy(dip->label.name, AudioNpreamp);
2143 		dip->type = AUDIO_MIXER_ENUM;
2144 		dip->un.e.num_mem = 2;
2145 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
2146 		dip->un.e.member[0].ord = 0;
2147 		strcpy(dip->un.e.member[1].label.name, AudioNon);
2148 		dip->un.e.member[1].ord = 1;
2149 		return (0);
2150 	}
2151 
2152 	return (ENXIO);
2153 }
2154 
2155 void *
2156 ess_malloc(addr, size, pool, flags)
2157 	void *addr;
2158 	unsigned long size;
2159 	int pool, flags;
2160 {
2161 	struct ess_softc *sc = addr;
2162 	int drq;
2163 
2164 	if (!ESS_USE_AUDIO1(sc->sc_model))
2165 		drq = sc->sc_audio2.drq;
2166 	else
2167 		drq = sc->sc_audio1.drq;
2168 	return (isa_malloc(sc->sc_isa, drq, size, pool, flags));
2169 }
2170 
2171 void
2172 ess_free(addr, ptr, pool)
2173 	void *addr;
2174 	void *ptr;
2175 	int pool;
2176 {
2177 	isa_free(ptr, pool);
2178 }
2179 
2180 unsigned long
2181 ess_round_buffersize(addr, size)
2182 	void *addr;
2183 	unsigned long size;
2184 {
2185 	if (size > MAX_ISADMA)
2186 		size = MAX_ISADMA;
2187 	return (size);
2188 }
2189 
2190 int
2191 ess_mappage(addr, mem, off, prot)
2192 	void *addr;
2193 	void *mem;
2194 	int off;
2195 	int prot;
2196 {
2197 	return (isa_mappage(mem, off, prot));
2198 }
2199 
2200 int
2201 ess_1788_get_props(addr)
2202 	void *addr;
2203 {
2204 
2205 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT);
2206 }
2207 
2208 int
2209 ess_1888_get_props(addr)
2210 	void *addr;
2211 {
2212 
2213 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
2214 }
2215 
2216 /* ============================================
2217  * Generic functions for ess, not used by audio h/w i/f
2218  * =============================================
2219  */
2220 
2221 /*
2222  * Reset the chip.
2223  * Return non-zero if the chip isn't detected.
2224  */
2225 int
2226 ess_reset(sc)
2227 	struct ess_softc *sc;
2228 {
2229 	bus_space_tag_t iot = sc->sc_iot;
2230 	bus_space_handle_t ioh = sc->sc_ioh;
2231 
2232 	sc->sc_audio1.active = 0;
2233 	sc->sc_audio2.active = 0;
2234 
2235 	EWRITE1(iot, ioh, ESS_DSP_RESET, ESS_RESET_EXT);
2236 	delay(10000);
2237 	EWRITE1(iot, ioh, ESS_DSP_RESET, 0);
2238 	if (ess_rdsp(sc) != ESS_MAGIC)
2239 		return (1);
2240 
2241 	/* Enable access to the ESS extension commands. */
2242 	ess_wdsp(sc, ESS_ACMD_ENABLE_EXT);
2243 
2244 	return (0);
2245 }
2246 
2247 void
2248 ess_set_gain(sc, port, on)
2249 	struct ess_softc *sc;
2250 	int port;
2251 	int on;
2252 {
2253 	int gain, left, right;
2254 	int mix;
2255 	int src;
2256 	int stereo;
2257 
2258 	/*
2259 	 * Most gain controls are found in the mixer registers and
2260 	 * are stereo. Any that are not, must set mix and stereo as
2261 	 * required.
2262 	 */
2263 	mix = 1;
2264 	stereo = 1;
2265 
2266 	switch (port) {
2267 	case ESS_MASTER_VOL:
2268 		src = ESS_MREG_VOLUME_MASTER;
2269 		break;
2270 	case ESS_DAC_PLAY_VOL:
2271 		if (ESS_USE_AUDIO1(sc->sc_model))
2272 			src = ESS_MREG_VOLUME_VOICE;
2273 		else
2274 			src = 0x7C;
2275 		break;
2276 	case ESS_MIC_PLAY_VOL:
2277 		src = ESS_MREG_VOLUME_MIC;
2278 		break;
2279 	case ESS_LINE_PLAY_VOL:
2280 		src = ESS_MREG_VOLUME_LINE;
2281 		break;
2282 	case ESS_SYNTH_PLAY_VOL:
2283 		src = ESS_MREG_VOLUME_SYNTH;
2284 		break;
2285 	case ESS_CD_PLAY_VOL:
2286 		src = ESS_MREG_VOLUME_CD;
2287 		break;
2288 	case ESS_AUXB_PLAY_VOL:
2289 		src = ESS_MREG_VOLUME_AUXB;
2290 		break;
2291 	case ESS_PCSPEAKER_VOL:
2292 		src = ESS_MREG_VOLUME_PCSPKR;
2293 		stereo = 0;
2294 		break;
2295 	case ESS_DAC_REC_VOL:
2296 		src = 0x69;
2297 		break;
2298 	case ESS_MIC_REC_VOL:
2299 		src = 0x68;
2300 		break;
2301 	case ESS_LINE_REC_VOL:
2302 		src = 0x6E;
2303 		break;
2304 	case ESS_SYNTH_REC_VOL:
2305 		src = 0x6B;
2306 		break;
2307 	case ESS_CD_REC_VOL:
2308 		src = 0x6A;
2309 		break;
2310 	case ESS_AUXB_REC_VOL:
2311 		src = 0x6C;
2312 		break;
2313 	case ESS_RECORD_VOL:
2314 		src = ESS_XCMD_VOLIN_CTRL;
2315 		mix = 0;
2316 		break;
2317 	default:
2318 		return;
2319 	}
2320 
2321 	/* 1788 doesn't have a separate recording mixer */
2322 	if (ESS_USE_AUDIO1(sc->sc_model) && mix && src > 0x62)
2323 		return;
2324 
2325 	if (on) {
2326 		left = sc->gain[port][ESS_LEFT];
2327 		right = sc->gain[port][ESS_RIGHT];
2328 	} else {
2329 		left = right = 0;
2330 	}
2331 
2332 	if (stereo)
2333 		gain = ESS_STEREO_GAIN(left, right);
2334 	else
2335 		gain = ESS_MONO_GAIN(left);
2336 
2337 	if (mix)
2338 		ess_write_mix_reg(sc, src, gain);
2339 	else
2340 		ess_write_x_reg(sc, src, gain);
2341 }
2342 
2343 /* Set the input device on devices without an input mixer. */
2344 int
2345 ess_set_in_port(sc, ord)
2346 	struct ess_softc *sc;
2347 	int ord;
2348 {
2349 	mixer_devinfo_t di;
2350 	int i;
2351 
2352 	DPRINTF(("ess_set_in_port: ord=0x%x\n", ord));
2353 
2354 	/*
2355 	 * Get the device info for the record source control,
2356 	 * including the list of available sources.
2357 	 */
2358 	di.index = ESS_RECORD_SOURCE;
2359 	if (ess_query_devinfo(sc, &di))
2360 		return EINVAL;
2361 
2362 	/* See if the given ord value was anywhere in the list. */
2363 	for (i = 0; i < di.un.e.num_mem; i++) {
2364 		if (ord == di.un.e.member[i].ord)
2365 			break;
2366 	}
2367 	if (i == di.un.e.num_mem)
2368 		return EINVAL;
2369 
2370 	ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ord);
2371 
2372 	sc->in_port = ord;
2373 	return (0);
2374 }
2375 
2376 /* Set the input device levels on input-mixer-enabled devices. */
2377 int
2378 ess_set_in_ports(sc, mask)
2379 	struct ess_softc *sc;
2380 	int mask;
2381 {
2382 	mixer_devinfo_t di;
2383 	int i, port;
2384 
2385 	DPRINTF(("ess_set_in_ports: mask=0x%x\n", mask));
2386 
2387 	/*
2388 	 * Get the device info for the record source control,
2389 	 * including the list of available sources.
2390 	 */
2391 	di.index = ESS_RECORD_SOURCE;
2392 	if (ess_query_devinfo(sc, &di))
2393 		return EINVAL;
2394 
2395 	/*
2396 	 * Set or disable the record volume control for each of the
2397 	 * possible sources.
2398 	 */
2399 	for (i = 0; i < di.un.s.num_mem; i++) {
2400 		/*
2401 		 * Calculate the source port number from its mask.
2402 		 */
2403 		port = ffs(di.un.s.member[i].mask);
2404 
2405 		/*
2406 		 * Set the source gain:
2407 		 *	to the current value if source is enabled
2408 		 *	to zero if source is disabled
2409 		 */
2410 		ess_set_gain(sc, port, mask & di.un.s.member[i].mask);
2411 	}
2412 
2413 	sc->in_mask = mask;
2414 	return (0);
2415 }
2416 
2417 void
2418 ess_speaker_on(sc)
2419 	struct ess_softc *sc;
2420 {
2421 	/* Disable mute on left- and right-master volume. */
2422 	ess_clear_mreg_bits(sc, ESS_MREG_VOLUME_LEFT, ESS_VOLUME_MUTE);
2423 	ess_clear_mreg_bits(sc, ESS_MREG_VOLUME_RIGHT, ESS_VOLUME_MUTE);
2424 }
2425 
2426 void
2427 ess_speaker_off(sc)
2428 	struct ess_softc *sc;
2429 {
2430 	/* Enable mute on left- and right-master volume. */
2431 	ess_set_mreg_bits(sc, ESS_MREG_VOLUME_LEFT, ESS_VOLUME_MUTE);
2432 	ess_set_mreg_bits(sc, ESS_MREG_VOLUME_RIGHT, ESS_VOLUME_MUTE);
2433 }
2434 
2435 /*
2436  * Calculate the time constant for the requested sampling rate.
2437  */
2438 u_int
2439 ess_srtotc(rate)
2440 	u_int rate;
2441 {
2442 	u_int tc;
2443 
2444 	/* The following formulae are from the ESS data sheet. */
2445 	if (rate <= 22050)
2446 		tc = 128 - 397700L / rate;
2447 	else
2448 		tc = 256 - 795500L / rate;
2449 
2450 	return (tc);
2451 }
2452 
2453 
2454 /*
2455  * Calculate the filter constant for the reuqested sampling rate.
2456  */
2457 u_int
2458 ess_srtofc(rate)
2459 	u_int rate;
2460 {
2461 	/*
2462 	 * The following formula is derived from the information in
2463 	 * the ES1887 data sheet, based on a roll-off frequency of
2464 	 * 87%.
2465 	 */
2466 	return (256 - 200279L / rate);
2467 }
2468 
2469 
2470 /*
2471  * Return the status of the DSP.
2472  */
2473 u_char
2474 ess_get_dsp_status(sc)
2475 	struct ess_softc *sc;
2476 {
2477 	return (EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS));
2478 }
2479 
2480 
2481 /*
2482  * Return the read status of the DSP:	1 -> DSP ready for reading
2483  *					0 -> DSP not ready for reading
2484  */
2485 u_char
2486 ess_dsp_read_ready(sc)
2487 	struct ess_softc *sc;
2488 {
2489 	return ((ess_get_dsp_status(sc) & ESS_DSP_READ_READY) ? 1 : 0);
2490 }
2491 
2492 
2493 /*
2494  * Return the write status of the DSP:	1 -> DSP ready for writing
2495  *					0 -> DSP not ready for writing
2496  */
2497 u_char
2498 ess_dsp_write_ready(sc)
2499 	struct ess_softc *sc;
2500 {
2501 	return ((ess_get_dsp_status(sc) & ESS_DSP_WRITE_BUSY) ? 0 : 1);
2502 }
2503 
2504 
2505 /*
2506  * Read a byte from the DSP.
2507  */
2508 int
2509 ess_rdsp(sc)
2510 	struct ess_softc *sc;
2511 {
2512 	bus_space_tag_t iot = sc->sc_iot;
2513 	bus_space_handle_t ioh = sc->sc_ioh;
2514 	int i;
2515 
2516 	for (i = ESS_READ_TIMEOUT; i > 0; --i) {
2517 		if (ess_dsp_read_ready(sc)) {
2518 			i = EREAD1(iot, ioh, ESS_DSP_READ);
2519 			DPRINTFN(8,("ess_rdsp() = 0x%02x\n", i));
2520 			return i;
2521 		} else
2522 			delay(10);
2523 	}
2524 
2525 	DPRINTF(("ess_rdsp: timed out\n"));
2526 	return (-1);
2527 }
2528 
2529 /*
2530  * Write a byte to the DSP.
2531  */
2532 int
2533 ess_wdsp(sc, v)
2534 	struct ess_softc *sc;
2535 	u_char v;
2536 {
2537 	bus_space_tag_t iot = sc->sc_iot;
2538 	bus_space_handle_t ioh = sc->sc_ioh;
2539 	int i;
2540 
2541 	DPRINTFN(8,("ess_wdsp(0x%02x)\n", v));
2542 
2543 	for (i = ESS_WRITE_TIMEOUT; i > 0; --i) {
2544 		if (ess_dsp_write_ready(sc)) {
2545 			EWRITE1(iot, ioh, ESS_DSP_WRITE, v);
2546 			return (0);
2547 		} else
2548 			delay(10);
2549 	}
2550 
2551 	DPRINTF(("ess_wdsp(0x%02x): timed out\n", v));
2552 	return (-1);
2553 }
2554 
2555 /*
2556  * Write a value to one of the ESS extended registers.
2557  */
2558 int
2559 ess_write_x_reg(sc, reg, val)
2560 	struct ess_softc *sc;
2561 	u_char reg;
2562 	u_char val;
2563 {
2564 	int error;
2565 
2566 	DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val));
2567 	if ((error = ess_wdsp(sc, reg)) == 0)
2568 		error = ess_wdsp(sc, val);
2569 
2570 	return error;
2571 }
2572 
2573 /*
2574  * Read the value of one of the ESS extended registers.
2575  */
2576 u_char
2577 ess_read_x_reg(sc, reg)
2578 	struct ess_softc *sc;
2579 	u_char reg;
2580 {
2581 	int error;
2582 	int val;
2583 
2584 	if ((error = ess_wdsp(sc, 0xC0)) == 0)
2585 		error = ess_wdsp(sc, reg);
2586 	if (error)
2587 		DPRINTF(("Error reading extended register 0x%02x\n", reg));
2588 /* REVISIT: what if an error is returned above? */
2589 	val = ess_rdsp(sc);
2590 	DPRINTFN(2,("ess_read_x_reg: %02x=%02x\n", reg, val));
2591 	return val;
2592 }
2593 
2594 void
2595 ess_clear_xreg_bits(sc, reg, mask)
2596 	struct ess_softc *sc;
2597 	u_char reg;
2598 	u_char mask;
2599 {
2600 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) & ~mask) == -1)
2601 		DPRINTF(("Error clearing bits in extended register 0x%02x\n",
2602 			 reg));
2603 }
2604 
2605 void
2606 ess_set_xreg_bits(sc, reg, mask)
2607 	struct ess_softc *sc;
2608 	u_char reg;
2609 	u_char mask;
2610 {
2611 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) | mask) == -1)
2612 		DPRINTF(("Error setting bits in extended register 0x%02x\n",
2613 			 reg));
2614 }
2615 
2616 
2617 /*
2618  * Write a value to one of the ESS mixer registers.
2619  */
2620 void
2621 ess_write_mix_reg(sc, reg, val)
2622 	struct ess_softc *sc;
2623 	u_char reg;
2624 	u_char val;
2625 {
2626 	bus_space_tag_t iot = sc->sc_iot;
2627 	bus_space_handle_t ioh = sc->sc_ioh;
2628 	int s;
2629 
2630 	DPRINTFN(2,("ess_write_mix_reg: %x=%x\n", reg, val));
2631 
2632 	s = splaudio();
2633 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2634 	EWRITE1(iot, ioh, ESS_MIX_REG_DATA, val);
2635 	splx(s);
2636 }
2637 
2638 /*
2639  * Read the value of one of the ESS mixer registers.
2640  */
2641 u_char
2642 ess_read_mix_reg(sc, reg)
2643 	struct ess_softc *sc;
2644 	u_char reg;
2645 {
2646 	bus_space_tag_t iot = sc->sc_iot;
2647 	bus_space_handle_t ioh = sc->sc_ioh;
2648 	int s;
2649 	u_char val;
2650 
2651 	s = splaudio();
2652 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2653 	val = EREAD1(iot, ioh, ESS_MIX_REG_DATA);
2654 	splx(s);
2655 
2656 	DPRINTFN(2,("ess_read_mix_reg: %x=%x\n", reg, val));
2657 	return val;
2658 }
2659 
2660 void
2661 ess_clear_mreg_bits(sc, reg, mask)
2662 	struct ess_softc *sc;
2663 	u_char reg;
2664 	u_char mask;
2665 {
2666 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) & ~mask);
2667 }
2668 
2669 void
2670 ess_set_mreg_bits(sc, reg, mask)
2671 	struct ess_softc *sc;
2672 	u_char reg;
2673 	u_char mask;
2674 {
2675 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) | mask);
2676 }
2677 
2678 void
2679 ess_read_multi_mix_reg(sc, reg, datap, count)
2680 	struct ess_softc *sc;
2681 	u_char reg;
2682 	u_int8_t *datap;
2683 	bus_size_t count;
2684 {
2685 	bus_space_tag_t iot = sc->sc_iot;
2686 	bus_space_handle_t ioh = sc->sc_ioh;
2687 	int s;
2688 
2689 	s = splaudio();
2690 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2691 	bus_space_read_multi_1(iot, ioh, ESS_MIX_REG_DATA, datap, count);
2692 	splx(s);
2693 }
2694