xref: /openbsd-src/sys/dev/isa/ess.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: ess.c,v 1.16 2010/07/15 03:43:11 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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
1187 	default:
1188 		return EINVAL;
1189 	}
1190 	fp->bps = AUDIO_BPS(fp->precision);
1191 	fp->msb = 1;
1192 
1193 	return (0);
1194 }
1195 
1196 int
1197 ess_set_params(addr, setmode, usemode, play, rec)
1198 	void *addr;
1199 	int setmode, usemode;
1200 	struct audio_params *play, *rec;
1201 {
1202 	struct ess_softc *sc = addr;
1203 	struct audio_params *p;
1204 	int mode;
1205 	int rate;
1206 
1207 	DPRINTF(("ess_set_params: set=%d use=%d\n", setmode, usemode));
1208 
1209 	/*
1210 	 * The ES1887 manual (page 39, `Full-Duplex DMA Mode') claims that in
1211 	 * full-duplex operation the sample rates must be the same for both
1212 	 * channels.  This appears to be false; the only bit in common is the
1213 	 * clock source selection.  However, we'll be conservative here.
1214 	 * - mycroft
1215 	 */
1216 	if (play->sample_rate != rec->sample_rate &&
1217 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1218 		if (setmode == AUMODE_PLAY) {
1219 			rec->sample_rate = play->sample_rate;
1220 			setmode |= AUMODE_RECORD;
1221 		} else if (setmode == AUMODE_RECORD) {
1222 			play->sample_rate = rec->sample_rate;
1223 			setmode |= AUMODE_PLAY;
1224 		} else
1225 			return (EINVAL);
1226 	}
1227 
1228 	for (mode = AUMODE_RECORD; mode != -1;
1229 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1230 		if ((setmode & mode) == 0)
1231 			continue;
1232 
1233 		p = mode == AUMODE_PLAY ? play : rec;
1234 
1235 		if (p->sample_rate < ESS_MINRATE)
1236 			p->sample_rate = ESS_MINRATE;
1237 		if (p->sample_rate > ESS_MAXRATE)
1238 			p->sample_rate = ESS_MAXRATE;
1239 		if (p->precision > 16)
1240 			p->precision = 16;
1241 		if (p->channels > 2)
1242 			p->channels = 2;
1243 
1244 		p->factor = 1;
1245 		p->sw_code = 0;
1246 		switch (p->encoding) {
1247 		case AUDIO_ENCODING_SLINEAR_BE:
1248 		case AUDIO_ENCODING_ULINEAR_BE:
1249 			if (p->precision == 16)
1250 				p->sw_code = swap_bytes;
1251 			break;
1252 		case AUDIO_ENCODING_SLINEAR_LE:
1253 		case AUDIO_ENCODING_ULINEAR_LE:
1254 			break;
1255 		case AUDIO_ENCODING_ULAW:
1256 			if (mode == AUMODE_PLAY) {
1257 				p->factor = 2;
1258 				p->sw_code = mulaw_to_ulinear16_le;
1259 			} else
1260 				p->sw_code = ulinear8_to_mulaw;
1261 			break;
1262 		case AUDIO_ENCODING_ALAW:
1263 			if (mode == AUMODE_PLAY) {
1264 				p->factor = 2;
1265 				p->sw_code = alaw_to_ulinear16_le;
1266 			} else
1267 				p->sw_code = ulinear8_to_alaw;
1268 			break;
1269 		default:
1270 			return (EINVAL);
1271 		}
1272 		p->bps = AUDIO_BPS(p->precision);
1273 		p->msb = 1;
1274 	}
1275 
1276 	if (usemode == AUMODE_RECORD)
1277 		rate = rec->sample_rate;
1278 	else
1279 		rate = play->sample_rate;
1280 
1281 	ess_write_x_reg(sc, ESS_XCMD_SAMPLE_RATE, ess_srtotc(rate));
1282 	ess_write_x_reg(sc, ESS_XCMD_FILTER_CLOCK, ess_srtofc(rate));
1283 
1284 	if (!ESS_USE_AUDIO1(sc->sc_model)) {
1285 		ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, ess_srtotc(rate));
1286 		ess_write_mix_reg(sc, ESS_MREG_FILTER_CLOCK, ess_srtofc(rate));
1287 	}
1288 
1289 	return (0);
1290 }
1291 
1292 int
1293 ess_audio1_trigger_output(addr, start, end, blksize, intr, arg, param)
1294 	void *addr;
1295 	void *start, *end;
1296 	int blksize;
1297 	void (*intr)(void *);
1298 	void *arg;
1299 	struct audio_params *param;
1300 {
1301 	struct ess_softc *sc = addr;
1302 	u_int8_t reg;
1303 
1304 	DPRINTFN(1, ("ess_audio1_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1305 	    addr, start, end, blksize, intr, arg));
1306 
1307 	if (sc->sc_audio1.active)
1308 		panic("ess_audio1_trigger_output: already running");
1309 
1310 	sc->sc_audio1.active = 1;
1311 	sc->sc_audio1.intr = intr;
1312 	sc->sc_audio1.arg = arg;
1313 	if (sc->sc_audio1.polled) {
1314 		sc->sc_audio1.dmapos = 0;
1315 		sc->sc_audio1.buffersize = (char *)end - (char *)start;
1316 		sc->sc_audio1.dmacount = 0;
1317 		sc->sc_audio1.blksize = blksize;
1318 		timeout_add_msec(&sc->sc_tmo1, 1000/30);
1319 	}
1320 
1321 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
1322 	if (param->channels == 2) {
1323 		reg &= ~ESS_AUDIO_CTRL_MONO;
1324 		reg |= ESS_AUDIO_CTRL_STEREO;
1325 	} else {
1326 		reg |= ESS_AUDIO_CTRL_MONO;
1327 		reg &= ~ESS_AUDIO_CTRL_STEREO;
1328 	}
1329 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
1330 
1331 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
1332 	if (param->precision * param->factor == 16)
1333 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
1334 	else
1335 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
1336 	if (param->channels == 2)
1337 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
1338 	else
1339 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
1340 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1341 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1342 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1343 	else
1344 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1345 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
1346 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
1347 
1348 	isa_dmastart(sc->sc_isa, sc->sc_audio1.drq, start,
1349 		     (char *)end - (char *)start, NULL,
1350 	    DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1351 
1352 	/* Program transfer count registers with 2's complement of count. */
1353 	blksize = -blksize;
1354 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
1355 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
1356 
1357 	/* Use 4 bytes per output DMA. */
1358 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
1359 
1360 	/* Start auto-init DMA */
1361 	ess_wdsp(sc, ESS_ACMD_ENABLE_SPKR);
1362 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
1363 	reg &= ~(ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE);
1364 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
1365 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
1366 
1367 	return (0);
1368 }
1369 
1370 int
1371 ess_audio2_trigger_output(addr, start, end, blksize, intr, arg, param)
1372 	void *addr;
1373 	void *start, *end;
1374 	int blksize;
1375 	void (*intr)(void *);
1376 	void *arg;
1377 	struct audio_params *param;
1378 {
1379 	struct ess_softc *sc = addr;
1380 	u_int8_t reg;
1381 
1382 	DPRINTFN(1, ("ess_audio2_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1383 	    addr, start, end, blksize, intr, arg));
1384 
1385 	if (sc->sc_audio2.active)
1386 		panic("ess_audio2_trigger_output: already running");
1387 
1388 	sc->sc_audio2.active = 1;
1389 	sc->sc_audio2.intr = intr;
1390 	sc->sc_audio2.arg = arg;
1391 	if (sc->sc_audio2.polled) {
1392 		sc->sc_audio2.dmapos = 0;
1393 		sc->sc_audio2.buffersize = (char *)end - (char *)start;
1394 		sc->sc_audio2.dmacount = 0;
1395 		sc->sc_audio2.blksize = blksize;
1396 		timeout_add_msec(&sc->sc_tmo2, 1000/30);
1397 	}
1398 
1399 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
1400 	if (param->precision * param->factor == 16)
1401 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIZE;
1402 	else
1403 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIZE;
1404 	if (param->channels == 2)
1405 		reg |= ESS_AUDIO2_CTRL2_CHANNELS;
1406 	else
1407 		reg &= ~ESS_AUDIO2_CTRL2_CHANNELS;
1408 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1409 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1410 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIGNED;
1411 	else
1412 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIGNED;
1413 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
1414 
1415 	isa_dmastart(sc->sc_isa, sc->sc_audio2.drq, start,
1416 		     (char *)end - (char *)start, NULL,
1417 	    DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1418 
1419 	if (IS16BITDRQ(sc->sc_audio2.drq))
1420 		blksize >>= 1;	/* use word count for 16 bit DMA */
1421 	/* Program transfer count registers with 2's complement of count. */
1422 	blksize = -blksize;
1423 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTLO, blksize);
1424 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTHI, blksize >> 8);
1425 
1426 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1);
1427 	if (IS16BITDRQ(sc->sc_audio2.drq))
1428 		reg |= ESS_AUDIO2_CTRL1_XFER_SIZE;
1429 	else
1430 		reg &= ~ESS_AUDIO2_CTRL1_XFER_SIZE;
1431 	reg |= ESS_AUDIO2_CTRL1_DEMAND_8;
1432 	reg |= ESS_AUDIO2_CTRL1_DAC_ENABLE | ESS_AUDIO2_CTRL1_FIFO_ENABLE |
1433 	       ESS_AUDIO2_CTRL1_AUTO_INIT;
1434 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1, reg);
1435 
1436 	return (0);
1437 }
1438 
1439 int
1440 ess_audio1_trigger_input(addr, start, end, blksize, intr, arg, param)
1441 	void *addr;
1442 	void *start, *end;
1443 	int blksize;
1444 	void (*intr)(void *);
1445 	void *arg;
1446 	struct audio_params *param;
1447 {
1448 	struct ess_softc *sc = addr;
1449 	u_int8_t reg;
1450 
1451 	DPRINTFN(1, ("ess_audio1_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1452 	    addr, start, end, blksize, intr, arg));
1453 
1454 	if (sc->sc_audio1.active)
1455 		panic("ess_audio1_trigger_input: already running");
1456 
1457 	sc->sc_audio1.active = 1;
1458 	sc->sc_audio1.intr = intr;
1459 	sc->sc_audio1.arg = arg;
1460 	if (sc->sc_audio1.polled) {
1461 		sc->sc_audio1.dmapos = 0;
1462 		sc->sc_audio1.buffersize = (char *)end - (char *)start;
1463 		sc->sc_audio1.dmacount = 0;
1464 		sc->sc_audio1.blksize = blksize;
1465 		timeout_add_msec(&sc->sc_tmo1, 1000/30);
1466 	}
1467 
1468 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
1469 	if (param->channels == 2) {
1470 		reg &= ~ESS_AUDIO_CTRL_MONO;
1471 		reg |= ESS_AUDIO_CTRL_STEREO;
1472 	} else {
1473 		reg |= ESS_AUDIO_CTRL_MONO;
1474 		reg &= ~ESS_AUDIO_CTRL_STEREO;
1475 	}
1476 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
1477 
1478 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
1479 	if (param->precision * param->factor == 16)
1480 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
1481 	else
1482 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
1483 	if (param->channels == 2)
1484 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
1485 	else
1486 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
1487 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1488 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1489 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1490 	else
1491 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
1492 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
1493 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
1494 
1495 	isa_dmastart(sc->sc_isa, sc->sc_audio1.drq, start,
1496 		     (char *)end - (char *)start, NULL,
1497 	    DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1498 
1499 	/* Program transfer count registers with 2's complement of count. */
1500 	blksize = -blksize;
1501 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
1502 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
1503 
1504 	/* Use 4 bytes per input DMA. */
1505 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
1506 
1507 	/* Start auto-init DMA */
1508 	ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR);
1509 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
1510 	reg |= ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE;
1511 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
1512 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
1513 
1514 	return (0);
1515 }
1516 
1517 int
1518 ess_audio1_halt(addr)
1519 	void *addr;
1520 {
1521 	struct ess_softc *sc = addr;
1522 
1523 	DPRINTF(("ess_audio1_halt: sc=%p\n", sc));
1524 
1525 	if (sc->sc_audio1.active) {
1526 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2,
1527 		    ESS_AUDIO1_CTRL2_FIFO_ENABLE);
1528 		isa_dmaabort(sc->sc_isa, sc->sc_audio1.drq);
1529 		if (sc->sc_audio1.polled)
1530 			timeout_del(&sc->sc_tmo1);
1531 		sc->sc_audio1.active = 0;
1532 	}
1533 
1534 	return (0);
1535 }
1536 
1537 int
1538 ess_audio2_halt(addr)
1539 	void *addr;
1540 {
1541 	struct ess_softc *sc = addr;
1542 
1543 	DPRINTF(("ess_audio2_halt: sc=%p\n", sc));
1544 
1545 	if (sc->sc_audio2.active) {
1546 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
1547 		    ESS_AUDIO2_CTRL1_DAC_ENABLE |
1548 		    ESS_AUDIO2_CTRL1_FIFO_ENABLE);
1549 		isa_dmaabort(sc->sc_isa, sc->sc_audio2.drq);
1550 		if (sc->sc_audio2.polled)
1551 			timeout_del(&sc->sc_tmo2);
1552 		sc->sc_audio2.active = 0;
1553 	}
1554 
1555 	return (0);
1556 }
1557 
1558 int
1559 ess_audio1_intr(arg)
1560 	void *arg;
1561 {
1562 	struct ess_softc *sc = arg;
1563 	u_int8_t reg;
1564 
1565 	DPRINTFN(1,("ess_audio1_intr: intr=%p\n", sc->sc_audio1.intr));
1566 
1567 	/* Check and clear interrupt on Audio1. */
1568 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS);
1569 	if ((reg & ESS_DSP_READ_OFLOW) == 0)
1570 		return (0);
1571 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_CLEAR_INTR);
1572 
1573 	sc->sc_audio1.nintr++;
1574 
1575 	if (sc->sc_audio1.active) {
1576 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
1577 		return (1);
1578 	} else
1579 		return (0);
1580 }
1581 
1582 int
1583 ess_audio2_intr(arg)
1584 	void *arg;
1585 {
1586 	struct ess_softc *sc = arg;
1587 	u_int8_t reg;
1588 
1589 	DPRINTFN(1,("ess_audio2_intr: intr=%p\n", sc->sc_audio2.intr));
1590 
1591 	/* Check and clear interrupt on Audio2. */
1592 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
1593 	if ((reg & ESS_AUDIO2_CTRL2_IRQ_LATCH) == 0)
1594 		return (0);
1595 	reg &= ~ESS_AUDIO2_CTRL2_IRQ_LATCH;
1596 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
1597 
1598 	sc->sc_audio2.nintr++;
1599 
1600 	if (sc->sc_audio2.active) {
1601 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
1602 		return (1);
1603 	} else
1604 		return (0);
1605 }
1606 
1607 void
1608 ess_audio1_poll(addr)
1609 	void *addr;
1610 {
1611 	struct ess_softc *sc = addr;
1612 	int dmapos, dmacount;
1613 
1614 	if (!sc->sc_audio1.active)
1615 		return;
1616 
1617 	sc->sc_audio1.nintr++;
1618 
1619 	dmapos = isa_dmacount(sc->sc_isa, sc->sc_audio1.drq);
1620 	dmacount = sc->sc_audio1.dmapos - dmapos;
1621 	if (dmacount < 0)
1622 		dmacount += sc->sc_audio1.buffersize;
1623 	sc->sc_audio1.dmapos = dmapos;
1624 #if 1
1625 	dmacount += sc->sc_audio1.dmacount;
1626 	while (dmacount > sc->sc_audio1.blksize) {
1627 		dmacount -= sc->sc_audio1.blksize;
1628 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
1629 	}
1630 	sc->sc_audio1.dmacount = dmacount;
1631 #else
1632 	(*sc->sc_audio1.intr)(sc->sc_audio1.arg, dmacount);
1633 #endif
1634 
1635 	timeout_add_msec(&sc->sc_tmo1, 1000/30);
1636 }
1637 
1638 void
1639 ess_audio2_poll(addr)
1640 	void *addr;
1641 {
1642 	struct ess_softc *sc = addr;
1643 	int dmapos, dmacount;
1644 
1645 	if (!sc->sc_audio2.active)
1646 		return;
1647 
1648 	sc->sc_audio2.nintr++;
1649 
1650 	dmapos = isa_dmacount(sc->sc_isa, sc->sc_audio2.drq);
1651 	dmacount = sc->sc_audio2.dmapos - dmapos;
1652 	if (dmacount < 0)
1653 		dmacount += sc->sc_audio2.buffersize;
1654 	sc->sc_audio2.dmapos = dmapos;
1655 #if 1
1656 	dmacount += sc->sc_audio2.dmacount;
1657 	while (dmacount > sc->sc_audio2.blksize) {
1658 		dmacount -= sc->sc_audio2.blksize;
1659 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
1660 	}
1661 	sc->sc_audio2.dmacount = dmacount;
1662 #else
1663 	(*sc->sc_audio2.intr)(sc->sc_audio2.arg, dmacount);
1664 #endif
1665 
1666 	timeout_add_msec(&sc->sc_tmo2, 1000/30);
1667 }
1668 
1669 int
1670 ess_round_blocksize(addr, blk)
1671 	void *addr;
1672 	int blk;
1673 {
1674 	return ((blk + 7) & -8);	/* round for max DMA size */
1675 }
1676 
1677 int
1678 ess_set_port(addr, cp)
1679 	void *addr;
1680 	mixer_ctrl_t *cp;
1681 {
1682 	struct ess_softc *sc = addr;
1683 	int lgain, rgain;
1684 
1685 	DPRINTFN(5,("ess_set_port: port=%d num_channels=%d\n",
1686 		    cp->dev, cp->un.value.num_channels));
1687 
1688 	switch (cp->dev) {
1689 	/*
1690 	 * The following mixer ports are all stereo. If we get a
1691 	 * single-channel gain value passed in, then we duplicate it
1692 	 * to both left and right channels.
1693 	 */
1694 	case ESS_MASTER_VOL:
1695 	case ESS_DAC_PLAY_VOL:
1696 	case ESS_MIC_PLAY_VOL:
1697 	case ESS_LINE_PLAY_VOL:
1698 	case ESS_SYNTH_PLAY_VOL:
1699 	case ESS_CD_PLAY_VOL:
1700 	case ESS_AUXB_PLAY_VOL:
1701 	case ESS_RECORD_VOL:
1702 		if (cp->type != AUDIO_MIXER_VALUE)
1703 			return EINVAL;
1704 
1705 		switch (cp->un.value.num_channels) {
1706 		case 1:
1707 			lgain = rgain = ESS_4BIT_GAIN(
1708 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1709 			break;
1710 		case 2:
1711 			lgain = ESS_4BIT_GAIN(
1712 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1713 			rgain = ESS_4BIT_GAIN(
1714 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1715 			break;
1716 		default:
1717 			return EINVAL;
1718 		}
1719 
1720 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
1721 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
1722 		ess_set_gain(sc, cp->dev, 1);
1723 		return (0);
1724 
1725 	/*
1726 	 * The PC speaker port is mono. If we get a stereo gain value
1727 	 * passed in, then we return EINVAL.
1728 	 */
1729 	case ESS_PCSPEAKER_VOL:
1730 		if (cp->un.value.num_channels != 1)
1731 			return EINVAL;
1732 
1733 		sc->gain[cp->dev][ESS_LEFT] = sc->gain[cp->dev][ESS_RIGHT] =
1734 		  ESS_3BIT_GAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1735 		ess_set_gain(sc, cp->dev, 1);
1736 		return (0);
1737 
1738 	case ESS_RECORD_SOURCE:
1739 		if (ESS_USE_AUDIO1(sc->sc_model)) {
1740 			if (cp->type == AUDIO_MIXER_ENUM)
1741 				return (ess_set_in_port(sc, cp->un.ord));
1742 			else
1743 				return (EINVAL);
1744 		} else {
1745 			if (cp->type == AUDIO_MIXER_SET)
1746 				return (ess_set_in_ports(sc, cp->un.mask));
1747 			else
1748 				return (EINVAL);
1749 		}
1750 		return (0);
1751 
1752 	case ESS_RECORD_MONITOR:
1753 		if (cp->type != AUDIO_MIXER_ENUM)
1754 			return EINVAL;
1755 
1756 		if (cp->un.ord)
1757 			/* Enable monitor */
1758 			ess_set_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
1759 					  ESS_AUDIO_CTRL_MONITOR);
1760 		else
1761 			/* Disable monitor */
1762 			ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
1763 					    ESS_AUDIO_CTRL_MONITOR);
1764 		return (0);
1765 	}
1766 
1767 	if (ESS_USE_AUDIO1(sc->sc_model))
1768 		return (EINVAL);
1769 
1770 	switch (cp->dev) {
1771 	case ESS_DAC_REC_VOL:
1772 	case ESS_MIC_REC_VOL:
1773 	case ESS_LINE_REC_VOL:
1774 	case ESS_SYNTH_REC_VOL:
1775 	case ESS_CD_REC_VOL:
1776 	case ESS_AUXB_REC_VOL:
1777 		if (cp->type != AUDIO_MIXER_VALUE)
1778 			return EINVAL;
1779 
1780 		switch (cp->un.value.num_channels) {
1781 		case 1:
1782 			lgain = rgain = ESS_4BIT_GAIN(
1783 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1784 			break;
1785 		case 2:
1786 			lgain = ESS_4BIT_GAIN(
1787 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1788 			rgain = ESS_4BIT_GAIN(
1789 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1790 			break;
1791 		default:
1792 			return EINVAL;
1793 		}
1794 
1795 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
1796 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
1797 		ess_set_gain(sc, cp->dev, 1);
1798 		return (0);
1799 
1800 	case ESS_MIC_PREAMP:
1801 		if (cp->type != AUDIO_MIXER_ENUM)
1802 			return EINVAL;
1803 
1804 		if (cp->un.ord)
1805 			/* Enable microphone preamp */
1806 			ess_set_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
1807 					  ESS_PREAMP_CTRL_ENABLE);
1808 		else
1809 			/* Disable microphone preamp */
1810 			ess_clear_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
1811 					  ESS_PREAMP_CTRL_ENABLE);
1812 		return (0);
1813 	}
1814 
1815 	return (EINVAL);
1816 }
1817 
1818 int
1819 ess_get_port(addr, cp)
1820 	void *addr;
1821 	mixer_ctrl_t *cp;
1822 {
1823 	struct ess_softc *sc = addr;
1824 
1825 	DPRINTFN(5,("ess_get_port: port=%d\n", cp->dev));
1826 
1827 	switch (cp->dev) {
1828 	case ESS_MASTER_VOL:
1829 	case ESS_DAC_PLAY_VOL:
1830 	case ESS_MIC_PLAY_VOL:
1831 	case ESS_LINE_PLAY_VOL:
1832 	case ESS_SYNTH_PLAY_VOL:
1833 	case ESS_CD_PLAY_VOL:
1834 	case ESS_AUXB_PLAY_VOL:
1835 	case ESS_RECORD_VOL:
1836 		switch (cp->un.value.num_channels) {
1837 		case 1:
1838 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1839 				sc->gain[cp->dev][ESS_LEFT];
1840 			break;
1841 		case 2:
1842 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1843 				sc->gain[cp->dev][ESS_LEFT];
1844 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1845 				sc->gain[cp->dev][ESS_RIGHT];
1846 			break;
1847 		default:
1848 			return EINVAL;
1849 		}
1850 		return (0);
1851 
1852 	case ESS_PCSPEAKER_VOL:
1853 		if (cp->un.value.num_channels != 1)
1854 			return EINVAL;
1855 
1856 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1857 			sc->gain[cp->dev][ESS_LEFT];
1858 		return (0);
1859 
1860 	case ESS_RECORD_SOURCE:
1861 		if (ESS_USE_AUDIO1(sc->sc_model))
1862 			cp->un.ord = sc->in_port;
1863 		else
1864 			cp->un.mask = sc->in_mask;
1865 		return (0);
1866 
1867 	case ESS_RECORD_MONITOR:
1868 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) &
1869 			      ESS_AUDIO_CTRL_MONITOR) ? 1 : 0;
1870 		return (0);
1871 	}
1872 
1873 	if (ESS_USE_AUDIO1(sc->sc_model))
1874 		return (EINVAL);
1875 
1876 	switch (cp->dev) {
1877 	case ESS_DAC_REC_VOL:
1878 	case ESS_MIC_REC_VOL:
1879 	case ESS_LINE_REC_VOL:
1880 	case ESS_SYNTH_REC_VOL:
1881 	case ESS_CD_REC_VOL:
1882 	case ESS_AUXB_REC_VOL:
1883 		switch (cp->un.value.num_channels) {
1884 		case 1:
1885 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1886 				sc->gain[cp->dev][ESS_LEFT];
1887 			break;
1888 		case 2:
1889 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1890 				sc->gain[cp->dev][ESS_LEFT];
1891 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1892 				sc->gain[cp->dev][ESS_RIGHT];
1893 			break;
1894 		default:
1895 			return EINVAL;
1896 		}
1897 		return (0);
1898 
1899 	case ESS_MIC_PREAMP:
1900 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL) &
1901 			      ESS_PREAMP_CTRL_ENABLE) ? 1 : 0;
1902 		return (0);
1903 	}
1904 
1905 	return (EINVAL);
1906 }
1907 
1908 int
1909 ess_query_devinfo(addr, dip)
1910 	void *addr;
1911 	mixer_devinfo_t *dip;
1912 {
1913 	struct ess_softc *sc = addr;
1914 
1915 	DPRINTFN(5,("ess_query_devinfo: model=%d index=%d\n",
1916 		    sc->sc_model, dip->index));
1917 
1918 	/*
1919 	 * REVISIT: There are some slight differences between the
1920 	 *          mixers on the different ESS chips, which can
1921 	 *          be sorted out using the chip model rather than a
1922 	 *          separate mixer model.
1923 	 *          This is currently coded assuming an ES1887; we
1924 	 *          need to work out which bits are not applicable to
1925 	 *          the other models (1888 and 888).
1926 	 */
1927 	switch (dip->index) {
1928 	case ESS_DAC_PLAY_VOL:
1929 		dip->mixer_class = ESS_INPUT_CLASS;
1930 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1931 		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1932 		dip->type = AUDIO_MIXER_VALUE;
1933 		dip->un.v.num_channels = 2;
1934 		strlcpy(dip->un.v.units.name, AudioNvolume,
1935 		    sizeof dip->un.v.units.name);
1936 		return (0);
1937 
1938 	case ESS_MIC_PLAY_VOL:
1939 		dip->mixer_class = ESS_INPUT_CLASS;
1940 		dip->prev = AUDIO_MIXER_LAST;
1941 		if (ESS_USE_AUDIO1(sc->sc_model))
1942 			dip->next = AUDIO_MIXER_LAST;
1943 		else
1944 			dip->next = ESS_MIC_PREAMP;
1945 		strlcpy(dip->label.name, AudioNmicrophone,
1946 		    sizeof dip->label.name);
1947 		dip->type = AUDIO_MIXER_VALUE;
1948 		dip->un.v.num_channels = 2;
1949 		strlcpy(dip->un.v.units.name, AudioNvolume,
1950 		    sizeof dip->un.v.units.name);
1951 		return (0);
1952 
1953 	case ESS_LINE_PLAY_VOL:
1954 		dip->mixer_class = ESS_INPUT_CLASS;
1955 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1956 		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1957 		dip->type = AUDIO_MIXER_VALUE;
1958 		dip->un.v.num_channels = 2;
1959 		strlcpy(dip->un.v.units.name, AudioNvolume,
1960 		    sizeof dip->un.v.units.name);
1961 		return (0);
1962 
1963 	case ESS_SYNTH_PLAY_VOL:
1964 		dip->mixer_class = ESS_INPUT_CLASS;
1965 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1966 		strlcpy(dip->label.name, AudioNfmsynth,
1967 		    sizeof dip->label.name);
1968 		dip->type = AUDIO_MIXER_VALUE;
1969 		dip->un.v.num_channels = 2;
1970 		strlcpy(dip->un.v.units.name, AudioNvolume,
1971 		    sizeof dip->un.v.units.name);
1972 		return (0);
1973 
1974 	case ESS_CD_PLAY_VOL:
1975 		dip->mixer_class = ESS_INPUT_CLASS;
1976 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1977 		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1978 		dip->type = AUDIO_MIXER_VALUE;
1979 		dip->un.v.num_channels = 2;
1980 		strlcpy(dip->un.v.units.name, AudioNvolume,
1981 		    sizeof dip->un.v.units.name);
1982 		return (0);
1983 
1984 	case ESS_AUXB_PLAY_VOL:
1985 		dip->mixer_class = ESS_INPUT_CLASS;
1986 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1987 		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
1988 		dip->type = AUDIO_MIXER_VALUE;
1989 		dip->un.v.num_channels = 2;
1990 		strlcpy(dip->un.v.units.name, AudioNvolume,
1991 		    sizeof dip->un.v.units.name);
1992 		return (0);
1993 
1994 	case ESS_INPUT_CLASS:
1995 		dip->mixer_class = ESS_INPUT_CLASS;
1996 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1997 		strlcpy(dip->label.name, AudioCinputs, sizeof dip->label.name);
1998 		dip->type = AUDIO_MIXER_CLASS;
1999 		return (0);
2000 
2001 	case ESS_MASTER_VOL:
2002 		dip->mixer_class = ESS_OUTPUT_CLASS;
2003 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2004 		strlcpy(dip->label.name, AudioNmaster, sizeof dip->label.name);
2005 		dip->type = AUDIO_MIXER_VALUE;
2006 		dip->un.v.num_channels = 2;
2007 		strlcpy(dip->un.v.units.name, AudioNvolume,
2008 		    sizeof dip->un.v.units.name);
2009 		return (0);
2010 
2011 	case ESS_PCSPEAKER_VOL:
2012 		dip->mixer_class = ESS_OUTPUT_CLASS;
2013 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2014 		strlcpy(dip->label.name, "pc_speaker", sizeof dip->label.name);
2015 		dip->type = AUDIO_MIXER_VALUE;
2016 		dip->un.v.num_channels = 1;
2017 		strlcpy(dip->un.v.units.name, AudioNvolume,
2018 		    sizeof dip->un.v.units.name);
2019 		return (0);
2020 
2021 	case ESS_OUTPUT_CLASS:
2022 		dip->mixer_class = ESS_OUTPUT_CLASS;
2023 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2024 		strlcpy(dip->label.name, AudioCoutputs, sizeof dip->label.name);
2025 		dip->type = AUDIO_MIXER_CLASS;
2026 		return (0);
2027 
2028 	case ESS_RECORD_VOL:
2029 		dip->mixer_class = ESS_RECORD_CLASS;
2030 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2031 		strlcpy(dip->label.name, AudioNrecord, sizeof dip->label.name);
2032 		dip->type = AUDIO_MIXER_VALUE;
2033 		dip->un.v.num_channels = 2;
2034 		strlcpy(dip->un.v.units.name, AudioNvolume,
2035 		    sizeof dip->un.v.units.name);
2036 		return (0);
2037 
2038 	case ESS_RECORD_SOURCE:
2039 		dip->mixer_class = ESS_RECORD_CLASS;
2040 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2041 		strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
2042 		if (ESS_USE_AUDIO1(sc->sc_model)) {
2043 			/*
2044 			 * The 1788 doesn't use the input mixer control that
2045 			 * the 1888 uses, because it's a pain when you only
2046 			 * have one mixer.
2047 			 * Perhaps it could be emulated by keeping both sets of
2048 			 * gain values, and doing a `context switch' of the
2049 			 * mixer registers when shifting from playing to
2050 			 * recording.
2051 			 */
2052 			dip->type = AUDIO_MIXER_ENUM;
2053 			dip->un.e.num_mem = 4;
2054 			strlcpy(dip->un.e.member[0].label.name,
2055 			    AudioNmicrophone,
2056 			    sizeof dip->un.e.member[0].label.name);
2057 			dip->un.e.member[0].ord = ESS_SOURCE_MIC;
2058 			strlcpy(dip->un.e.member[1].label.name, AudioNline,
2059 			    sizeof dip->un.e.member[1].label.name);
2060 			dip->un.e.member[1].ord = ESS_SOURCE_LINE;
2061 			strlcpy(dip->un.e.member[2].label.name, AudioNcd,
2062 			    sizeof dip->un.e.member[2].label.name);
2063 			dip->un.e.member[2].ord = ESS_SOURCE_CD;
2064 			strlcpy(dip->un.e.member[3].label.name, AudioNmixerout,
2065 			    sizeof dip->un.e.member[3].label.name);
2066 			dip->un.e.member[3].ord = ESS_SOURCE_MIXER;
2067 		} else {
2068 			dip->type = AUDIO_MIXER_SET;
2069 			dip->un.s.num_mem = 6;
2070 			strlcpy(dip->un.s.member[0].label.name, AudioNdac,
2071 			    sizeof dip->un.e.member[0].label.name);
2072 			dip->un.s.member[0].mask = 1 << ESS_DAC_REC_VOL;
2073 			strlcpy(dip->un.s.member[1].label.name,
2074 			    AudioNmicrophone,
2075 			    sizeof dip->un.e.member[1].label.name);
2076 			dip->un.s.member[1].mask = 1 << ESS_MIC_REC_VOL;
2077 			strlcpy(dip->un.s.member[2].label.name, AudioNline,
2078 			    sizeof dip->un.e.member[2].label.name);
2079 			dip->un.s.member[2].mask = 1 << ESS_LINE_REC_VOL;
2080 			strlcpy(dip->un.s.member[3].label.name, AudioNfmsynth,
2081 			    sizeof dip->un.e.member[3].label.name);
2082 			dip->un.s.member[3].mask = 1 << ESS_SYNTH_REC_VOL;
2083 			strlcpy(dip->un.s.member[4].label.name, AudioNcd,
2084 			    sizeof dip->un.e.member[4].label.name);
2085 			dip->un.s.member[4].mask = 1 << ESS_CD_REC_VOL;
2086 			strlcpy(dip->un.s.member[5].label.name, "auxb",
2087 			    sizeof dip->un.e.member[5].label.name);
2088 			dip->un.s.member[5].mask = 1 << ESS_AUXB_REC_VOL;
2089 		}
2090 		return (0);
2091 
2092 	case ESS_RECORD_CLASS:
2093 		dip->mixer_class = ESS_RECORD_CLASS;
2094 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2095 		strlcpy(dip->label.name, AudioCrecord, sizeof dip->label.name);
2096 		dip->type = AUDIO_MIXER_CLASS;
2097 		return (0);
2098 
2099 	case ESS_RECORD_MONITOR:
2100 		dip->prev = dip->next = AUDIO_MIXER_LAST;
2101 		strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
2102 		dip->type = AUDIO_MIXER_ENUM;
2103 		dip->mixer_class = ESS_MONITOR_CLASS;
2104 		dip->un.e.num_mem = 2;
2105 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
2106 		    sizeof dip->un.e.member[0].label.name);
2107 		dip->un.e.member[0].ord = 0;
2108 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
2109 		    sizeof dip->un.e.member[1].label.name);
2110 		dip->un.e.member[1].ord = 1;
2111 		return (0);
2112 
2113 	case ESS_MONITOR_CLASS:
2114 		dip->mixer_class = ESS_MONITOR_CLASS;
2115 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2116 		strlcpy(dip->label.name, AudioCmonitor,
2117 		    sizeof dip->label.name);
2118 		dip->type = AUDIO_MIXER_CLASS;
2119 		return (0);
2120 	}
2121 
2122 	if (ESS_USE_AUDIO1(sc->sc_model))
2123 		return (ENXIO);
2124 
2125 	switch (dip->index) {
2126 	case ESS_DAC_REC_VOL:
2127 		dip->mixer_class = ESS_RECORD_CLASS;
2128 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2129 		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
2130 		dip->type = AUDIO_MIXER_VALUE;
2131 		dip->un.v.num_channels = 2;
2132 		strlcpy(dip->un.v.units.name, AudioNvolume,
2133 		    sizeof dip->un.v.units.name);
2134 		return (0);
2135 
2136 	case ESS_MIC_REC_VOL:
2137 		dip->mixer_class = ESS_RECORD_CLASS;
2138 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2139 		strlcpy(dip->label.name, AudioNmicrophone,
2140 		    sizeof dip->label.name);
2141 		dip->type = AUDIO_MIXER_VALUE;
2142 		dip->un.v.num_channels = 2;
2143 		strlcpy(dip->un.v.units.name, AudioNvolume,
2144 		    sizeof dip->un.v.units.name);
2145 		return (0);
2146 
2147 	case ESS_LINE_REC_VOL:
2148 		dip->mixer_class = ESS_RECORD_CLASS;
2149 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2150 		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
2151 		dip->type = AUDIO_MIXER_VALUE;
2152 		dip->un.v.num_channels = 2;
2153 		strlcpy(dip->un.v.units.name, AudioNvolume,
2154 		    sizeof dip->un.v.units.name);
2155 		return (0);
2156 
2157 	case ESS_SYNTH_REC_VOL:
2158 		dip->mixer_class = ESS_RECORD_CLASS;
2159 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2160 		strlcpy(dip->label.name, AudioNfmsynth,
2161 		    sizeof dip->label.name);
2162 		dip->type = AUDIO_MIXER_VALUE;
2163 		dip->un.v.num_channels = 2;
2164 		strlcpy(dip->un.v.units.name, AudioNvolume,
2165 		    sizeof dip->un.v.units.name);
2166 		return (0);
2167 
2168 	case ESS_CD_REC_VOL:
2169 		dip->mixer_class = ESS_RECORD_CLASS;
2170 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2171 		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
2172 		dip->type = AUDIO_MIXER_VALUE;
2173 		dip->un.v.num_channels = 2;
2174 		strlcpy(dip->un.v.units.name, AudioNvolume,
2175 		    sizeof dip->un.v.units.name);
2176 		return (0);
2177 
2178 	case ESS_AUXB_REC_VOL:
2179 		dip->mixer_class = ESS_RECORD_CLASS;
2180 		dip->next = dip->prev = AUDIO_MIXER_LAST;
2181 		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
2182 		dip->type = AUDIO_MIXER_VALUE;
2183 		dip->un.v.num_channels = 2;
2184 		strlcpy(dip->un.v.units.name, AudioNvolume,
2185 		    sizeof dip->un.v.units.name);
2186 		return (0);
2187 
2188 	case ESS_MIC_PREAMP:
2189 		dip->mixer_class = ESS_INPUT_CLASS;
2190 		dip->prev = ESS_MIC_PLAY_VOL;
2191 		dip->next = AUDIO_MIXER_LAST;
2192 		strlcpy(dip->label.name, AudioNpreamp, sizeof dip->label.name);
2193 		dip->type = AUDIO_MIXER_ENUM;
2194 		dip->un.e.num_mem = 2;
2195 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
2196 		    sizeof dip->un.e.member[0].label.name);
2197 		dip->un.e.member[0].ord = 0;
2198 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
2199 		    sizeof dip->un.e.member[1].label.name);
2200 		dip->un.e.member[1].ord = 1;
2201 		return (0);
2202 	}
2203 
2204 	return (ENXIO);
2205 }
2206 
2207 void *
2208 ess_malloc(addr, direction, size, pool, flags)
2209 	void *addr;
2210 	int direction;
2211 	size_t size;
2212 	int pool, flags;
2213 {
2214 	struct ess_softc *sc = addr;
2215 	int drq;
2216 
2217 	if (!ESS_USE_AUDIO1(sc->sc_model))
2218 		drq = sc->sc_audio2.drq;
2219 	else
2220 		drq = sc->sc_audio1.drq;
2221 	return (isa_malloc(sc->sc_isa, drq, size, pool, flags));
2222 }
2223 
2224 void
2225 ess_free(addr, ptr, pool)
2226 	void *addr;
2227 	void *ptr;
2228 	int pool;
2229 {
2230 	isa_free(ptr, pool);
2231 }
2232 
2233 size_t
2234 ess_round_buffersize(addr, direction, size)
2235 	void *addr;
2236 	int direction;
2237 	size_t size;
2238 {
2239 	if (size > MAX_ISADMA)
2240 		size = MAX_ISADMA;
2241 	return (size);
2242 }
2243 
2244 paddr_t
2245 ess_mappage(addr, mem, off, prot)
2246 	void *addr;
2247 	void *mem;
2248 	off_t off;
2249 	int prot;
2250 {
2251 	return (isa_mappage(mem, off, prot));
2252 }
2253 
2254 int
2255 ess_1788_get_props(addr)
2256 	void *addr;
2257 {
2258 
2259 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT);
2260 }
2261 
2262 int
2263 ess_1888_get_props(addr)
2264 	void *addr;
2265 {
2266 
2267 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
2268 }
2269 
2270 /* ============================================
2271  * Generic functions for ess, not used by audio h/w i/f
2272  * =============================================
2273  */
2274 
2275 /*
2276  * Reset the chip.
2277  * Return non-zero if the chip isn't detected.
2278  */
2279 int
2280 ess_reset(sc)
2281 	struct ess_softc *sc;
2282 {
2283 	bus_space_tag_t iot = sc->sc_iot;
2284 	bus_space_handle_t ioh = sc->sc_ioh;
2285 
2286 	sc->sc_audio1.active = 0;
2287 	sc->sc_audio2.active = 0;
2288 
2289 	EWRITE1(iot, ioh, ESS_DSP_RESET, ESS_RESET_EXT);
2290 	delay(10000);
2291 	EWRITE1(iot, ioh, ESS_DSP_RESET, 0);
2292 	if (ess_rdsp(sc) != ESS_MAGIC)
2293 		return (1);
2294 
2295 	/* Enable access to the ESS extension commands. */
2296 	ess_wdsp(sc, ESS_ACMD_ENABLE_EXT);
2297 
2298 	return (0);
2299 }
2300 
2301 void
2302 ess_set_gain(sc, port, on)
2303 	struct ess_softc *sc;
2304 	int port;
2305 	int on;
2306 {
2307 	int gain, left, right;
2308 	int mix;
2309 	int src;
2310 	int stereo;
2311 
2312 	/*
2313 	 * Most gain controls are found in the mixer registers and
2314 	 * are stereo. Any that are not, must set mix and stereo as
2315 	 * required.
2316 	 */
2317 	mix = 1;
2318 	stereo = 1;
2319 
2320 	switch (port) {
2321 	case ESS_MASTER_VOL:
2322 		src = ESS_MREG_VOLUME_MASTER;
2323 		break;
2324 	case ESS_DAC_PLAY_VOL:
2325 		if (ESS_USE_AUDIO1(sc->sc_model))
2326 			src = ESS_MREG_VOLUME_VOICE;
2327 		else
2328 			src = 0x7C;
2329 		break;
2330 	case ESS_MIC_PLAY_VOL:
2331 		src = ESS_MREG_VOLUME_MIC;
2332 		break;
2333 	case ESS_LINE_PLAY_VOL:
2334 		src = ESS_MREG_VOLUME_LINE;
2335 		break;
2336 	case ESS_SYNTH_PLAY_VOL:
2337 		src = ESS_MREG_VOLUME_SYNTH;
2338 		break;
2339 	case ESS_CD_PLAY_VOL:
2340 		src = ESS_MREG_VOLUME_CD;
2341 		break;
2342 	case ESS_AUXB_PLAY_VOL:
2343 		src = ESS_MREG_VOLUME_AUXB;
2344 		break;
2345 	case ESS_PCSPEAKER_VOL:
2346 		src = ESS_MREG_VOLUME_PCSPKR;
2347 		stereo = 0;
2348 		break;
2349 	case ESS_DAC_REC_VOL:
2350 		src = 0x69;
2351 		break;
2352 	case ESS_MIC_REC_VOL:
2353 		src = 0x68;
2354 		break;
2355 	case ESS_LINE_REC_VOL:
2356 		src = 0x6E;
2357 		break;
2358 	case ESS_SYNTH_REC_VOL:
2359 		src = 0x6B;
2360 		break;
2361 	case ESS_CD_REC_VOL:
2362 		src = 0x6A;
2363 		break;
2364 	case ESS_AUXB_REC_VOL:
2365 		src = 0x6C;
2366 		break;
2367 	case ESS_RECORD_VOL:
2368 		src = ESS_XCMD_VOLIN_CTRL;
2369 		mix = 0;
2370 		break;
2371 	default:
2372 		return;
2373 	}
2374 
2375 	/* 1788 doesn't have a separate recording mixer */
2376 	if (ESS_USE_AUDIO1(sc->sc_model) && mix && src > 0x62)
2377 		return;
2378 
2379 	if (on) {
2380 		left = sc->gain[port][ESS_LEFT];
2381 		right = sc->gain[port][ESS_RIGHT];
2382 	} else {
2383 		left = right = 0;
2384 	}
2385 
2386 	if (stereo)
2387 		gain = ESS_STEREO_GAIN(left, right);
2388 	else
2389 		gain = ESS_MONO_GAIN(left);
2390 
2391 	if (mix)
2392 		ess_write_mix_reg(sc, src, gain);
2393 	else
2394 		ess_write_x_reg(sc, src, gain);
2395 }
2396 
2397 /* Set the input device on devices without an input mixer. */
2398 int
2399 ess_set_in_port(sc, ord)
2400 	struct ess_softc *sc;
2401 	int ord;
2402 {
2403 	mixer_devinfo_t di;
2404 	int i;
2405 
2406 	DPRINTF(("ess_set_in_port: ord=0x%x\n", ord));
2407 
2408 	/*
2409 	 * Get the device info for the record source control,
2410 	 * including the list of available sources.
2411 	 */
2412 	di.index = ESS_RECORD_SOURCE;
2413 	if (ess_query_devinfo(sc, &di))
2414 		return EINVAL;
2415 
2416 	/* See if the given ord value was anywhere in the list. */
2417 	for (i = 0; i < di.un.e.num_mem; i++) {
2418 		if (ord == di.un.e.member[i].ord)
2419 			break;
2420 	}
2421 	if (i == di.un.e.num_mem)
2422 		return EINVAL;
2423 
2424 	ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ord);
2425 
2426 	sc->in_port = ord;
2427 	return (0);
2428 }
2429 
2430 /* Set the input device levels on input-mixer-enabled devices. */
2431 int
2432 ess_set_in_ports(sc, mask)
2433 	struct ess_softc *sc;
2434 	int mask;
2435 {
2436 	mixer_devinfo_t di;
2437 	int i, port;
2438 
2439 	DPRINTF(("ess_set_in_ports: mask=0x%x\n", mask));
2440 
2441 	/*
2442 	 * Get the device info for the record source control,
2443 	 * including the list of available sources.
2444 	 */
2445 	di.index = ESS_RECORD_SOURCE;
2446 	if (ess_query_devinfo(sc, &di))
2447 		return EINVAL;
2448 
2449 	/*
2450 	 * Set or disable the record volume control for each of the
2451 	 * possible sources.
2452 	 */
2453 	for (i = 0; i < di.un.s.num_mem; i++) {
2454 		/*
2455 		 * Calculate the source port number from its mask.
2456 		 */
2457 		port = ffs(di.un.s.member[i].mask);
2458 
2459 		/*
2460 		 * Set the source gain:
2461 		 *	to the current value if source is enabled
2462 		 *	to zero if source is disabled
2463 		 */
2464 		ess_set_gain(sc, port, mask & di.un.s.member[i].mask);
2465 	}
2466 
2467 	sc->in_mask = mask;
2468 	return (0);
2469 }
2470 
2471 void
2472 ess_speaker_on(sc)
2473 	struct ess_softc *sc;
2474 {
2475 	/* Unmute the DAC. */
2476 	ess_set_gain(sc, ESS_DAC_PLAY_VOL, 1);
2477 }
2478 
2479 void
2480 ess_speaker_off(sc)
2481 	struct ess_softc *sc;
2482 {
2483 	/* Mute the DAC. */
2484 	ess_set_gain(sc, ESS_DAC_PLAY_VOL, 0);
2485 }
2486 
2487 /*
2488  * Calculate the time constant for the requested sampling rate.
2489  */
2490 u_int
2491 ess_srtotc(rate)
2492 	u_int rate;
2493 {
2494 	u_int tc;
2495 
2496 	/* The following formulae are from the ESS data sheet. */
2497 	if (rate <= 22050)
2498 		tc = 128 - 397700L / rate;
2499 	else
2500 		tc = 256 - 795500L / rate;
2501 
2502 	return (tc);
2503 }
2504 
2505 
2506 /*
2507  * Calculate the filter constant for the reuqested sampling rate.
2508  */
2509 u_int
2510 ess_srtofc(rate)
2511 	u_int rate;
2512 {
2513 	/*
2514 	 * The following formula is derived from the information in
2515 	 * the ES1887 data sheet, based on a roll-off frequency of
2516 	 * 87%.
2517 	 */
2518 	return (256 - 200279L / rate);
2519 }
2520 
2521 
2522 /*
2523  * Return the status of the DSP.
2524  */
2525 u_char
2526 ess_get_dsp_status(sc)
2527 	struct ess_softc *sc;
2528 {
2529 	return (EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS));
2530 }
2531 
2532 
2533 /*
2534  * Return the read status of the DSP:	1 -> DSP ready for reading
2535  *					0 -> DSP not ready for reading
2536  */
2537 u_char
2538 ess_dsp_read_ready(sc)
2539 	struct ess_softc *sc;
2540 {
2541 	return ((ess_get_dsp_status(sc) & ESS_DSP_READ_READY) ? 1 : 0);
2542 }
2543 
2544 
2545 /*
2546  * Return the write status of the DSP:	1 -> DSP ready for writing
2547  *					0 -> DSP not ready for writing
2548  */
2549 u_char
2550 ess_dsp_write_ready(sc)
2551 	struct ess_softc *sc;
2552 {
2553 	return ((ess_get_dsp_status(sc) & ESS_DSP_WRITE_BUSY) ? 0 : 1);
2554 }
2555 
2556 
2557 /*
2558  * Read a byte from the DSP.
2559  */
2560 int
2561 ess_rdsp(sc)
2562 	struct ess_softc *sc;
2563 {
2564 	bus_space_tag_t iot = sc->sc_iot;
2565 	bus_space_handle_t ioh = sc->sc_ioh;
2566 	int i;
2567 
2568 	for (i = ESS_READ_TIMEOUT; i > 0; --i) {
2569 		if (ess_dsp_read_ready(sc)) {
2570 			i = EREAD1(iot, ioh, ESS_DSP_READ);
2571 			DPRINTFN(8,("ess_rdsp() = 0x%02x\n", i));
2572 			return i;
2573 		} else
2574 			delay(10);
2575 	}
2576 
2577 	DPRINTF(("ess_rdsp: timed out\n"));
2578 	return (-1);
2579 }
2580 
2581 /*
2582  * Write a byte to the DSP.
2583  */
2584 int
2585 ess_wdsp(sc, v)
2586 	struct ess_softc *sc;
2587 	u_char v;
2588 {
2589 	bus_space_tag_t iot = sc->sc_iot;
2590 	bus_space_handle_t ioh = sc->sc_ioh;
2591 	int i;
2592 
2593 	DPRINTFN(8,("ess_wdsp(0x%02x)\n", v));
2594 
2595 	for (i = ESS_WRITE_TIMEOUT; i > 0; --i) {
2596 		if (ess_dsp_write_ready(sc)) {
2597 			EWRITE1(iot, ioh, ESS_DSP_WRITE, v);
2598 			return (0);
2599 		} else
2600 			delay(10);
2601 	}
2602 
2603 	DPRINTF(("ess_wdsp(0x%02x): timed out\n", v));
2604 	return (-1);
2605 }
2606 
2607 /*
2608  * Write a value to one of the ESS extended registers.
2609  */
2610 int
2611 ess_write_x_reg(sc, reg, val)
2612 	struct ess_softc *sc;
2613 	u_char reg;
2614 	u_char val;
2615 {
2616 	int error;
2617 
2618 	DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val));
2619 	if ((error = ess_wdsp(sc, reg)) == 0)
2620 		error = ess_wdsp(sc, val);
2621 
2622 	return error;
2623 }
2624 
2625 /*
2626  * Read the value of one of the ESS extended registers.
2627  */
2628 u_char
2629 ess_read_x_reg(sc, reg)
2630 	struct ess_softc *sc;
2631 	u_char reg;
2632 {
2633 	int error;
2634 	int val;
2635 
2636 	if ((error = ess_wdsp(sc, 0xC0)) == 0)
2637 		error = ess_wdsp(sc, reg);
2638 	if (error)
2639 		DPRINTF(("Error reading extended register 0x%02x\n", reg));
2640 /* REVISIT: what if an error is returned above? */
2641 	val = ess_rdsp(sc);
2642 	DPRINTFN(2,("ess_read_x_reg: %02x=%02x\n", reg, val));
2643 	return val;
2644 }
2645 
2646 void
2647 ess_clear_xreg_bits(sc, reg, mask)
2648 	struct ess_softc *sc;
2649 	u_char reg;
2650 	u_char mask;
2651 {
2652 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) & ~mask) == -1)
2653 		DPRINTF(("Error clearing bits in extended register 0x%02x\n",
2654 			 reg));
2655 }
2656 
2657 void
2658 ess_set_xreg_bits(sc, reg, mask)
2659 	struct ess_softc *sc;
2660 	u_char reg;
2661 	u_char mask;
2662 {
2663 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) | mask) == -1)
2664 		DPRINTF(("Error setting bits in extended register 0x%02x\n",
2665 			 reg));
2666 }
2667 
2668 
2669 /*
2670  * Write a value to one of the ESS mixer registers.
2671  */
2672 void
2673 ess_write_mix_reg(sc, reg, val)
2674 	struct ess_softc *sc;
2675 	u_char reg;
2676 	u_char val;
2677 {
2678 	bus_space_tag_t iot = sc->sc_iot;
2679 	bus_space_handle_t ioh = sc->sc_ioh;
2680 	int s;
2681 
2682 	DPRINTFN(2,("ess_write_mix_reg: %x=%x\n", reg, val));
2683 
2684 	s = splaudio();
2685 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2686 	EWRITE1(iot, ioh, ESS_MIX_REG_DATA, val);
2687 	splx(s);
2688 }
2689 
2690 /*
2691  * Read the value of one of the ESS mixer registers.
2692  */
2693 u_char
2694 ess_read_mix_reg(sc, reg)
2695 	struct ess_softc *sc;
2696 	u_char reg;
2697 {
2698 	bus_space_tag_t iot = sc->sc_iot;
2699 	bus_space_handle_t ioh = sc->sc_ioh;
2700 	int s;
2701 	u_char val;
2702 
2703 	s = splaudio();
2704 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2705 	val = EREAD1(iot, ioh, ESS_MIX_REG_DATA);
2706 	splx(s);
2707 
2708 	DPRINTFN(2,("ess_read_mix_reg: %x=%x\n", reg, val));
2709 	return val;
2710 }
2711 
2712 void
2713 ess_clear_mreg_bits(sc, reg, mask)
2714 	struct ess_softc *sc;
2715 	u_char reg;
2716 	u_char mask;
2717 {
2718 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) & ~mask);
2719 }
2720 
2721 void
2722 ess_set_mreg_bits(sc, reg, mask)
2723 	struct ess_softc *sc;
2724 	u_char reg;
2725 	u_char mask;
2726 {
2727 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) | mask);
2728 }
2729 
2730 void
2731 ess_read_multi_mix_reg(sc, reg, datap, count)
2732 	struct ess_softc *sc;
2733 	u_char reg;
2734 	u_int8_t *datap;
2735 	bus_size_t count;
2736 {
2737 	bus_space_tag_t iot = sc->sc_iot;
2738 	bus_space_handle_t ioh = sc->sc_ioh;
2739 	int s;
2740 
2741 	s = splaudio();
2742 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
2743 	bus_space_read_multi_1(iot, ioh, ESS_MIX_REG_DATA, datap, count);
2744 	splx(s);
2745 }
2746