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