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