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