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