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