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