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