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