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