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