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