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