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