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