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