eap.c revision 1.47 1 /* $NetBSD: eap.c,v 1.47 2001/10/03 00:04:52 augustss Exp $ */
2 /* $OpenBSD: eap.c,v 1.6 1999/10/05 19:24:42 csapuntz Exp $ */
3
4 /*
5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson <augustss (at) netbsd.org> and Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Debugging: Andreas Gustafsson <gson (at) araneus.fi>
42 * Testing: Chuck Cranor <chuck (at) maria.wustl.edu>
43 * Phil Nelson <phil (at) cs.wwu.edu>
44 *
45 * ES1371/AC97: Ezra Story <ezy (at) panix.com>
46 */
47
48 /*
49 * Ensoniq ES1370 + AK4531 and ES1371/ES1373 + AC97
50 *
51 * Documentation links:
52 *
53 * ftp://ftp.alsa-project.org/pub/manuals/ensoniq/
54 * ftp://ftp.alsa-project.org/pub/manuals/asahi_kasei/4531.pdf
55 * ftp://download.intel.com/ial/scalableplatforms/audio/ac97r21.pdf
56 */
57
58 #include "midi.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/fcntl.h>
64 #include <sys/malloc.h>
65 #include <sys/device.h>
66 #include <sys/proc.h>
67
68 #include <dev/pci/pcidevs.h>
69 #include <dev/pci/pcivar.h>
70
71 #include <sys/audioio.h>
72 #include <dev/audio_if.h>
73 #include <dev/midi_if.h>
74 #include <dev/mulaw.h>
75 #include <dev/auconv.h>
76 #include <dev/ic/ac97var.h>
77
78 #include <machine/bus.h>
79
80 #include <dev/pci/eapreg.h>
81
82 #define PCI_CBIO 0x10
83
84 /* Debug */
85 #ifdef AUDIO_DEBUG
86 #define DPRINTF(x) if (eapdebug) printf x
87 #define DPRINTFN(n,x) if (eapdebug>(n)) printf x
88 int eapdebug = 0;
89 #else
90 #define DPRINTF(x)
91 #define DPRINTFN(n,x)
92 #endif
93
94 int eap_match(struct device *, struct cfdata *, void *);
95 void eap_attach(struct device *, struct device *, void *);
96 int eap_intr(void *);
97
98 struct eap_dma {
99 bus_dmamap_t map;
100 caddr_t addr;
101 bus_dma_segment_t segs[1];
102 int nsegs;
103 size_t size;
104 struct eap_dma *next;
105 };
106
107 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
108 #define KERNADDR(p) ((void *)((p)->addr))
109
110 struct eap_softc {
111 struct device sc_dev; /* base device */
112 void *sc_ih; /* interrupt vectoring */
113 bus_space_tag_t iot;
114 bus_space_handle_t ioh;
115 bus_dma_tag_t sc_dmatag; /* DMA tag */
116
117 struct eap_dma *sc_dmas;
118
119 void (*sc_pintr)(void *); /* dma completion intr handler */
120 void *sc_parg; /* arg for sc_intr() */
121 #ifdef DIAGNOSTIC
122 char sc_prun;
123 #endif
124
125 void (*sc_rintr)(void *); /* dma completion intr handler */
126 void *sc_rarg; /* arg for sc_intr() */
127 #ifdef DIAGNOSTIC
128 char sc_rrun;
129 #endif
130
131 #if NMIDI > 0
132 void (*sc_iintr)(void *, int); /* midi input ready handler */
133 void (*sc_ointr)(void *); /* midi output ready handler */
134 void *sc_arg;
135 #endif
136
137 u_short sc_port[AK_NPORTS]; /* mirror of the hardware setting */
138 u_int sc_record_source; /* recording source mask */
139 u_int sc_output_source; /* output source mask */
140 u_int sc_mic_preamp;
141 char sc_1371; /* Using ES1371/AC97 codec */
142
143 struct ac97_codec_if *codec_if;
144 struct ac97_host_if host_if;
145 };
146
147 int eap_allocmem(struct eap_softc *, size_t, size_t, struct eap_dma *);
148 int eap_freemem(struct eap_softc *, struct eap_dma *);
149
150 #define EWRITE1(sc, r, x) bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x))
151 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
152 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
153 #define EREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
154 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
155 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
156
157 struct cfattach eap_ca = {
158 sizeof(struct eap_softc), eap_match, eap_attach
159 };
160
161 int eap_open(void *, int);
162 void eap_close(void *);
163 int eap_query_encoding(void *, struct audio_encoding *);
164 int eap_set_params(void *, int, int, struct audio_params *, struct audio_params *);
165 int eap_round_blocksize(void *, int);
166 int eap_trigger_output(void *, void *, void *, int, void (*)(void *),
167 void *, struct audio_params *);
168 int eap_trigger_input(void *, void *, void *, int, void (*)(void *),
169 void *, struct audio_params *);
170 int eap_halt_output(void *);
171 int eap_halt_input(void *);
172 void eap1370_write_codec(struct eap_softc *, int, int);
173 int eap_getdev(void *, struct audio_device *);
174 int eap1370_mixer_set_port(void *, mixer_ctrl_t *);
175 int eap1370_mixer_get_port(void *, mixer_ctrl_t *);
176 int eap1371_mixer_set_port(void *, mixer_ctrl_t *);
177 int eap1371_mixer_get_port(void *, mixer_ctrl_t *);
178 int eap1370_query_devinfo(void *, mixer_devinfo_t *);
179 void *eap_malloc(void *, int, size_t, int, int);
180 void eap_free(void *, void *, int);
181 size_t eap_round_buffersize(void *, int, size_t);
182 paddr_t eap_mappage(void *, void *, off_t, int);
183 int eap_get_props(void *);
184 void eap1370_set_mixer(struct eap_softc *sc, int a, int d);
185 u_int32_t eap1371_src_wait(struct eap_softc *sc);
186 void eap1371_set_adc_rate(struct eap_softc *sc, int rate);
187 void eap1371_set_dac_rate(struct eap_softc *sc, int rate, int which);
188 int eap1371_src_read(struct eap_softc *sc, int a);
189 void eap1371_src_write(struct eap_softc *sc, int a, int d);
190 int eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip);
191
192 int eap1371_attach_codec(void *sc, struct ac97_codec_if *);
193 int eap1371_read_codec(void *sc, u_int8_t a, u_int16_t *d);
194 int eap1371_write_codec(void *sc, u_int8_t a, u_int16_t d);
195 void eap1371_reset_codec(void *sc);
196 int eap1371_get_portnum_by_name(struct eap_softc *, char *, char *,
197 char *);
198 #if NMIDI > 0
199 void eap_midi_close(void *);
200 void eap_midi_getinfo(void *, struct midi_info *);
201 int eap_midi_open(void *, int, void (*)(void *, int),
202 void (*)(void *), void *);
203 int eap_midi_output(void *, int);
204 #endif
205
206 struct audio_hw_if eap1370_hw_if = {
207 eap_open,
208 eap_close,
209 NULL,
210 eap_query_encoding,
211 eap_set_params,
212 eap_round_blocksize,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 NULL,
218 eap_halt_output,
219 eap_halt_input,
220 NULL,
221 eap_getdev,
222 NULL,
223 eap1370_mixer_set_port,
224 eap1370_mixer_get_port,
225 eap1370_query_devinfo,
226 eap_malloc,
227 eap_free,
228 eap_round_buffersize,
229 eap_mappage,
230 eap_get_props,
231 eap_trigger_output,
232 eap_trigger_input,
233 NULL,
234 };
235
236 struct audio_hw_if eap1371_hw_if = {
237 eap_open,
238 eap_close,
239 NULL,
240 eap_query_encoding,
241 eap_set_params,
242 eap_round_blocksize,
243 NULL,
244 NULL,
245 NULL,
246 NULL,
247 NULL,
248 eap_halt_output,
249 eap_halt_input,
250 NULL,
251 eap_getdev,
252 NULL,
253 eap1371_mixer_set_port,
254 eap1371_mixer_get_port,
255 eap1371_query_devinfo,
256 eap_malloc,
257 eap_free,
258 eap_round_buffersize,
259 eap_mappage,
260 eap_get_props,
261 eap_trigger_output,
262 eap_trigger_input,
263 NULL,
264 };
265
266 #if NMIDI > 0
267 struct midi_hw_if eap_midi_hw_if = {
268 eap_midi_open,
269 eap_midi_close,
270 eap_midi_output,
271 eap_midi_getinfo,
272 0, /* ioctl */
273 };
274 #endif
275
276 struct audio_device eap_device = {
277 "Ensoniq AudioPCI",
278 "",
279 "eap"
280 };
281
282 int
283 eap_match(struct device *parent, struct cfdata *match, void *aux)
284 {
285 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
286
287 switch (PCI_VENDOR(pa->pa_id)) {
288 case PCI_VENDOR_CREATIVELABS:
289 switch (PCI_PRODUCT(pa->pa_id)) {
290 case PCI_PRODUCT_CREATIVELABS_EV1938:
291 return (1);
292 }
293 break;
294 case PCI_VENDOR_ENSONIQ:
295 switch (PCI_PRODUCT(pa->pa_id)) {
296 case PCI_PRODUCT_ENSONIQ_AUDIOPCI:
297 case PCI_PRODUCT_ENSONIQ_AUDIOPCI97:
298 case PCI_PRODUCT_ENSONIQ_CT5880:
299 return (1);
300 }
301 break;
302 }
303
304 return (0);
305 }
306
307 void
308 eap1370_write_codec(struct eap_softc *sc, int a, int d)
309 {
310 int icss, to;
311
312 to = EAP_WRITE_TIMEOUT;
313 do {
314 icss = EREAD4(sc, EAP_ICSS);
315 DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
316 if (!to--) {
317 printf("eap: timeout writing to codec\n");
318 return;
319 }
320 } while(icss & EAP_CWRIP); /* XXX could use CSTAT here */
321 EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
322 }
323
324 /*
325 * Reading and writing the CODEC is very convoluted. This mimics the
326 * FreeBSD and Linux drivers.
327 */
328
329 static __inline void
330 eap1371_ready_codec(struct eap_softc *sc, u_int8_t a, u_int32_t wd)
331 {
332 int to, s;
333 u_int32_t src, t;
334
335 for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
336 if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
337 break;
338 delay(1);
339 }
340 if (to >= EAP_WRITE_TIMEOUT)
341 printf("%s: eap1371_ready_codec timeout 1\n",
342 sc->sc_dev.dv_xname);
343
344 s = splaudio();
345 src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
346 EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
347
348 for (to = 0; to < EAP_READ_TIMEOUT; to++) {
349 t = EREAD4(sc, E1371_SRC);
350 if ((t & E1371_SRC_STATE_MASK) == 0)
351 break;
352 delay(1);
353 }
354 if (to >= EAP_READ_TIMEOUT)
355 printf("%s: eap1371_ready_codec timeout 2\n",
356 sc->sc_dev.dv_xname);
357
358 for (to = 0; to < EAP_READ_TIMEOUT; to++) {
359 t = EREAD4(sc, E1371_SRC);
360 if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
361 break;
362 delay(1);
363 }
364 if (to >= EAP_READ_TIMEOUT)
365 printf("%s: eap1371_ready_codec timeout 3\n",
366 sc->sc_dev.dv_xname);
367
368 EWRITE4(sc, E1371_CODEC, wd);
369
370 eap1371_src_wait(sc);
371 EWRITE4(sc, E1371_SRC, src);
372
373 splx(s);
374 }
375
376 int
377 eap1371_read_codec(void *sc_, u_int8_t a, u_int16_t *d)
378 {
379 struct eap_softc *sc = sc_;
380 int to;
381 u_int32_t t;
382
383 eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
384
385 for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
386 if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
387 break;
388 }
389 if (to > EAP_WRITE_TIMEOUT)
390 printf("%s: eap1371_read_codec timeout 1\n",
391 sc->sc_dev.dv_xname);
392
393 for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
394 t = EREAD4(sc, E1371_CODEC);
395 if (t & E1371_CODEC_VALID)
396 break;
397 }
398 if (to > EAP_WRITE_TIMEOUT)
399 printf("%s: eap1371_read_codec timeout 2\n",
400 sc->sc_dev.dv_xname);
401
402 *d = (u_int16_t)t;
403
404 DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
405
406 return (0);
407 }
408
409 int
410 eap1371_write_codec(void *sc_, u_int8_t a, u_int16_t d)
411 {
412 struct eap_softc *sc = sc_;
413
414 eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
415
416 DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
417
418 return (0);
419 }
420
421 u_int32_t
422 eap1371_src_wait(struct eap_softc *sc)
423 {
424 int to;
425 u_int32_t src;
426
427 for (to = 0; to < EAP_READ_TIMEOUT; to++) {
428 src = EREAD4(sc, E1371_SRC);
429 if (!(src & E1371_SRC_RBUSY))
430 return (src);
431 delay(1);
432 }
433 printf("%s: eap1371_src_wait timeout\n", sc->sc_dev.dv_xname);
434 return (src);
435 }
436
437 int
438 eap1371_src_read(struct eap_softc *sc, int a)
439 {
440 int to;
441 u_int32_t src, t;
442
443 src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
444 src |= E1371_SRC_ADDR(a);
445 EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
446
447 if ((eap1371_src_wait(sc) & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
448 for (to = 0; to < EAP_READ_TIMEOUT; to++) {
449 t = EREAD4(sc, E1371_SRC);
450 if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
451 break;
452 delay(1);
453 }
454 }
455
456 EWRITE4(sc, E1371_SRC, src);
457
458 return t & E1371_SRC_DATAMASK;
459 }
460
461 void
462 eap1371_src_write(struct eap_softc *sc, int a, int d)
463 {
464 u_int32_t r;
465
466 r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
467 r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
468 EWRITE4(sc, E1371_SRC, r);
469 }
470
471 void
472 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
473 {
474 int freq, n, truncm;
475 int out;
476 int s;
477
478 /* Whatever, it works, so I'll leave it :) */
479
480 if (rate > 48000)
481 rate = 48000;
482 if (rate < 4000)
483 rate = 4000;
484 n = rate / 3000;
485 if ((1 << n) & SRC_MAGIC)
486 n--;
487 truncm = ((21 * n) - 1) | 1;
488 freq = ((48000 << 15) / rate) * n;
489 if (rate >= 24000) {
490 if (truncm > 239)
491 truncm = 239;
492 out = ESRC_SET_TRUNC((239 - truncm) / 2);
493 } else {
494 if (truncm > 119)
495 truncm = 119;
496 out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
497 }
498 out |= ESRC_SET_N(n);
499 s = splaudio();
500 eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
501
502
503 out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
504 eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
505 ESRC_SET_VFI(freq >> 15));
506 eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
507 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
508 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
509 splx(s);
510 }
511
512 void
513 eap1371_set_dac_rate(struct eap_softc *sc, int rate, int which)
514 {
515 int dac = which == 1 ? ESRC_DAC1 : ESRC_DAC2;
516 int freq, r;
517 int s;
518
519 /* Whatever, it works, so I'll leave it :) */
520
521 if (rate > 48000)
522 rate = 48000;
523 if (rate < 4000)
524 rate = 4000;
525 freq = ((rate << 15) + 1500) / 3000;
526
527 s = splaudio();
528 eap1371_src_wait(sc);
529 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
530 E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
531 r |= (which == 1) ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
532 EWRITE4(sc, E1371_SRC, r);
533 r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
534 eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
535 eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
536 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
537 E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
538 r &= ~(which == 1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
539 EWRITE4(sc, E1371_SRC, r);
540 splx(s);
541 }
542
543 void
544 eap_attach(struct device *parent, struct device *self, void *aux)
545 {
546 struct eap_softc *sc = (struct eap_softc *)self;
547 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
548 pci_chipset_tag_t pc = pa->pa_pc;
549 struct audio_hw_if *eap_hw_if;
550 char const *intrstr;
551 pci_intr_handle_t ih;
552 pcireg_t csr;
553 char devinfo[256];
554 mixer_ctrl_t ctl;
555 int i;
556 int revision, ct5880;
557 const char *revstr = "";
558
559 /* Flag if we're "creative" */
560 sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
561 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
562
563 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
564 revision = PCI_REVISION(pa->pa_class);
565 if (sc->sc_1371) {
566 ct5880 = 0;
567 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
568 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880)
569 ct5880 = 1;
570 switch (revision) {
571 case EAP_EV1938_A: revstr = "EV1938A "; break;
572 case EAP_CT5880_C: revstr = "CT5880C "; ct5880 = 1; break;
573 case EAP_ES1373_A: revstr = "ES1373A "; break;
574 case EAP_ES1373_B: revstr = "ES1373B "; break;
575 case EAP_CT5880_A: revstr = "CT5880A "; ct5880 = 1; break;
576 case EAP_ES1371_B: revstr = "ES1371B "; break;
577 }
578 }
579 printf(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
580
581 /* Map I/O register */
582 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
583 &sc->iot, &sc->ioh, NULL, NULL)) {
584 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
585 return;
586 }
587
588 sc->sc_dmatag = pa->pa_dmat;
589
590 /* Enable the device. */
591 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
592 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
593 csr | PCI_COMMAND_MASTER_ENABLE);
594
595 /* Map and establish the interrupt. */
596 if (pci_intr_map(pa, &ih)) {
597 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
598 return;
599 }
600 intrstr = pci_intr_string(pc, ih);
601 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
602 if (sc->sc_ih == NULL) {
603 printf("%s: couldn't establish interrupt",
604 sc->sc_dev.dv_xname);
605 if (intrstr != NULL)
606 printf(" at %s", intrstr);
607 printf("\n");
608 return;
609 }
610 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
611
612 if (!sc->sc_1371) {
613 /* Enable interrupts and looping mode. */
614 /* enable the parts we need */
615 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
616 EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
617
618 /* reset codec */
619 /* normal operation */
620 /* select codec clocks */
621 eap1370_write_codec(sc, AK_RESET, AK_PD);
622 eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
623 eap1370_write_codec(sc, AK_CS, 0x0);
624
625 eap_hw_if = &eap1370_hw_if;
626
627 /* Enable all relevant mixer switches. */
628 ctl.dev = EAP_OUTPUT_SELECT;
629 ctl.type = AUDIO_MIXER_SET;
630 ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
631 1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
632 1 << EAP_MIC_VOL;
633 eap_hw_if->set_port(sc, &ctl);
634
635 ctl.type = AUDIO_MIXER_VALUE;
636 ctl.un.value.num_channels = 1;
637 for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
638 ctl.dev++) {
639 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
640 eap_hw_if->set_port(sc, &ctl);
641 }
642 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
643 eap_hw_if->set_port(sc, &ctl);
644 ctl.dev = EAP_MIC_PREAMP;
645 ctl.type = AUDIO_MIXER_ENUM;
646 ctl.un.ord = 0;
647 eap_hw_if->set_port(sc, &ctl);
648 ctl.dev = EAP_RECORD_SOURCE;
649 ctl.type = AUDIO_MIXER_SET;
650 ctl.un.mask = 1 << EAP_MIC_VOL;
651 eap_hw_if->set_port(sc, &ctl);
652 } else {
653 /* clean slate */
654
655 EWRITE4(sc, EAP_SIC, 0);
656 EWRITE4(sc, EAP_ICSC, 0);
657 EWRITE4(sc, E1371_LEGACY, 0);
658
659 if (ct5880) {
660 EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
661 /* Let codec wake up */
662 tsleep(sc, PRIBIO, "eapcdc", hz / 20);
663 }
664
665 /* Reset from es1371's perspective */
666 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
667 delay(20);
668 EWRITE4(sc, EAP_ICSC, 0);
669
670 /*
671 * Must properly reprogram sample rate converter,
672 * or it locks up. Set some defaults for the life of the
673 * machine, and set up a sb default sample rate.
674 */
675 EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
676 for (i = 0; i < 0x80; i++)
677 eap1371_src_write(sc, i, 0);
678 eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
679 eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
680 eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
681 eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
682 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
683 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
684 eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
685 eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
686 eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
687 eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
688 eap1371_set_adc_rate(sc, 22050);
689 eap1371_set_dac_rate(sc, 22050, 1);
690 eap1371_set_dac_rate(sc, 22050, 2);
691
692 EWRITE4(sc, E1371_SRC, 0);
693
694 /* Reset codec */
695
696 /* Interrupt enable */
697 sc->host_if.arg = sc;
698 sc->host_if.attach = eap1371_attach_codec;
699 sc->host_if.read = eap1371_read_codec;
700 sc->host_if.write = eap1371_write_codec;
701 sc->host_if.reset = eap1371_reset_codec;
702
703 if (ac97_attach(&sc->host_if) == 0) {
704 /* Interrupt enable */
705 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
706 } else
707 return;
708
709 eap_hw_if = &eap1371_hw_if;
710
711 /* Just enable the DAC and master volumes by default */
712 ctl.type = AUDIO_MIXER_ENUM;
713 ctl.un.ord = 0; /* off */
714 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
715 AudioNmaster, AudioNmute);
716 eap1371_mixer_set_port(sc, &ctl);
717 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
718 AudioNdac, AudioNmute);
719 eap1371_mixer_set_port(sc, &ctl);
720 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
721 AudioNvolume, AudioNmute);
722 eap1371_mixer_set_port(sc, &ctl);
723
724 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
725 AudioNsource, NULL);
726 ctl.type = AUDIO_MIXER_ENUM;
727 ctl.un.ord = 0;
728 eap1371_mixer_set_port(sc, &ctl);
729
730 }
731
732 audio_attach_mi(eap_hw_if, sc, &sc->sc_dev);
733
734 #if NMIDI > 0
735 midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
736 #endif
737 }
738
739 int
740 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
741 {
742 struct eap_softc *sc = sc_;
743
744 sc->codec_if = codec_if;
745 return (0);
746 }
747
748 void
749 eap1371_reset_codec(void *sc_)
750 {
751 struct eap_softc *sc = sc_;
752 u_int32_t icsc;
753 int s;
754
755 s = splaudio();
756 icsc = EREAD4(sc, EAP_ICSC);
757 EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
758 delay(20);
759 EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
760 delay(1);
761 splx(s);
762
763 return;
764 }
765
766 int
767 eap_intr(void *p)
768 {
769 struct eap_softc *sc = p;
770 u_int32_t intr, sic;
771
772 intr = EREAD4(sc, EAP_ICSS);
773 if (!(intr & EAP_INTR))
774 return (0);
775 sic = EREAD4(sc, EAP_SIC);
776 DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
777 if (intr & EAP_I_ADC) {
778 #if 0
779 /*
780 * XXX This is a hack!
781 * The EAP chip sometimes generates the recording interrupt
782 * while it is still transferring the data. To make sure
783 * it has all arrived we busy wait until the count is right.
784 * The transfer we are waiting for is 8 longwords.
785 */
786 int s, nw, n;
787 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
788 s = EREAD4(sc, EAP_ADC_CSR);
789 nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
790 n = 0;
791 while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
792 delay(10);
793 if (++n > 100) {
794 printf("eapintr: dma fix timeout");
795 break;
796 }
797 }
798 /* Continue with normal interrupt handling. */
799 #endif
800 EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
801 EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
802 if (sc->sc_rintr)
803 sc->sc_rintr(sc->sc_rarg);
804 }
805 if (intr & EAP_I_DAC2) {
806 EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
807 EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
808 if (sc->sc_pintr)
809 sc->sc_pintr(sc->sc_parg);
810 }
811 #if NMIDI > 0
812 if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
813 u_int32_t data;
814
815 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
816 while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
817 data = EREAD1(sc, EAP_UART_DATA);
818 sc->sc_iintr(sc->sc_arg, data);
819 }
820 }
821 }
822 #endif
823 return (1);
824 }
825
826 int
827 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
828 {
829 int error;
830
831 p->size = size;
832 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
833 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
834 &p->nsegs, BUS_DMA_NOWAIT);
835 if (error)
836 return (error);
837
838 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
839 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
840 if (error)
841 goto free;
842
843 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
844 0, BUS_DMA_NOWAIT, &p->map);
845 if (error)
846 goto unmap;
847
848 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
849 BUS_DMA_NOWAIT);
850 if (error)
851 goto destroy;
852 return (0);
853
854 destroy:
855 bus_dmamap_destroy(sc->sc_dmatag, p->map);
856 unmap:
857 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
858 free:
859 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
860 return (error);
861 }
862
863 int
864 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
865 {
866 bus_dmamap_unload(sc->sc_dmatag, p->map);
867 bus_dmamap_destroy(sc->sc_dmatag, p->map);
868 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
869 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
870 return (0);
871 }
872
873 int
874 eap_open(void *addr, int flags)
875 {
876 return (0);
877 }
878
879 /*
880 * Close function is called at splaudio().
881 */
882 void
883 eap_close(void *addr)
884 {
885 struct eap_softc *sc = addr;
886
887 eap_halt_output(sc);
888 eap_halt_input(sc);
889
890 sc->sc_pintr = 0;
891 sc->sc_rintr = 0;
892 }
893
894 int
895 eap_query_encoding(void *addr, struct audio_encoding *fp)
896 {
897 switch (fp->index) {
898 case 0:
899 strcpy(fp->name, AudioEulinear);
900 fp->encoding = AUDIO_ENCODING_ULINEAR;
901 fp->precision = 8;
902 fp->flags = 0;
903 return (0);
904 case 1:
905 strcpy(fp->name, AudioEmulaw);
906 fp->encoding = AUDIO_ENCODING_ULAW;
907 fp->precision = 8;
908 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
909 return (0);
910 case 2:
911 strcpy(fp->name, AudioEalaw);
912 fp->encoding = AUDIO_ENCODING_ALAW;
913 fp->precision = 8;
914 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
915 return (0);
916 case 3:
917 strcpy(fp->name, AudioEslinear);
918 fp->encoding = AUDIO_ENCODING_SLINEAR;
919 fp->precision = 8;
920 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
921 return (0);
922 case 4:
923 strcpy(fp->name, AudioEslinear_le);
924 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
925 fp->precision = 16;
926 fp->flags = 0;
927 return (0);
928 case 5:
929 strcpy(fp->name, AudioEulinear_le);
930 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
931 fp->precision = 16;
932 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
933 return (0);
934 case 6:
935 strcpy(fp->name, AudioEslinear_be);
936 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
937 fp->precision = 16;
938 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
939 return (0);
940 case 7:
941 strcpy(fp->name, AudioEulinear_be);
942 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
943 fp->precision = 16;
944 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
945 return (0);
946 default:
947 return (EINVAL);
948 }
949 }
950
951 int
952 eap_set_params(void *addr, int setmode, int usemode,
953 struct audio_params *play, struct audio_params *rec)
954 {
955 struct eap_softc *sc = addr;
956 struct audio_params *p;
957 int mode;
958 u_int32_t div;
959
960 /*
961 * The es1370 only has one clock, so make the sample rates match.
962 */
963 if (!sc->sc_1371) {
964 if (play->sample_rate != rec->sample_rate &&
965 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
966 if (setmode == AUMODE_PLAY) {
967 rec->sample_rate = play->sample_rate;
968 setmode |= AUMODE_RECORD;
969 } else if (setmode == AUMODE_RECORD) {
970 play->sample_rate = rec->sample_rate;
971 setmode |= AUMODE_PLAY;
972 } else
973 return (EINVAL);
974 }
975 }
976
977 for (mode = AUMODE_RECORD; mode != -1;
978 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
979 if ((setmode & mode) == 0)
980 continue;
981
982 p = mode == AUMODE_PLAY ? play : rec;
983
984 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
985 (p->precision != 8 && p->precision != 16) ||
986 (p->channels != 1 && p->channels != 2))
987 return (EINVAL);
988
989 p->factor = 1;
990 p->sw_code = 0;
991 switch (p->encoding) {
992 case AUDIO_ENCODING_SLINEAR_BE:
993 if (p->precision == 16)
994 p->sw_code = swap_bytes;
995 else
996 p->sw_code = change_sign8;
997 break;
998 case AUDIO_ENCODING_SLINEAR_LE:
999 if (p->precision != 16)
1000 p->sw_code = change_sign8;
1001 break;
1002 case AUDIO_ENCODING_ULINEAR_BE:
1003 if (p->precision == 16) {
1004 if (mode == AUMODE_PLAY)
1005 p->sw_code = swap_bytes_change_sign16_le;
1006 else
1007 p->sw_code = change_sign16_swap_bytes_le;
1008 }
1009 break;
1010 case AUDIO_ENCODING_ULINEAR_LE:
1011 if (p->precision == 16)
1012 p->sw_code = change_sign16_le;
1013 break;
1014 case AUDIO_ENCODING_ULAW:
1015 if (mode == AUMODE_PLAY) {
1016 p->factor = 2;
1017 p->sw_code = mulaw_to_slinear16_le;
1018 } else
1019 p->sw_code = ulinear8_to_mulaw;
1020 break;
1021 case AUDIO_ENCODING_ALAW:
1022 if (mode == AUMODE_PLAY) {
1023 p->factor = 2;
1024 p->sw_code = alaw_to_slinear16_le;
1025 } else
1026 p->sw_code = ulinear8_to_alaw;
1027 break;
1028 default:
1029 return (EINVAL);
1030 }
1031 }
1032
1033 if (sc->sc_1371) {
1034 eap1371_set_dac_rate(sc, play->sample_rate, 1);
1035 eap1371_set_dac_rate(sc, play->sample_rate, 2);
1036 eap1371_set_adc_rate(sc, rec->sample_rate);
1037 } else {
1038 /* Set the speed */
1039 DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1040 EREAD4(sc, EAP_ICSC)));
1041 div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1042 /*
1043 * XXX
1044 * The -2 isn't documented, but seemed to make the wall
1045 * time match
1046 * what I expect. - mycroft
1047 */
1048 if (usemode == AUMODE_RECORD)
1049 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1050 rec->sample_rate - 2);
1051 else
1052 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1053 play->sample_rate - 2);
1054 div |= EAP_CCB_INTRM;
1055 EWRITE4(sc, EAP_ICSC, div);
1056 DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1057 }
1058
1059 return (0);
1060 }
1061
1062 int
1063 eap_round_blocksize(void *addr, int blk)
1064 {
1065 return (blk & -32); /* keep good alignment */
1066 }
1067
1068 int
1069 eap_trigger_output(
1070 void *addr,
1071 void *start,
1072 void *end,
1073 int blksize,
1074 void (*intr)(void *),
1075 void *arg,
1076 struct audio_params *param)
1077 {
1078 struct eap_softc *sc = addr;
1079 struct eap_dma *p;
1080 u_int32_t icsc, sic;
1081 int sampshift;
1082
1083 #ifdef DIAGNOSTIC
1084 if (sc->sc_prun)
1085 panic("eap_trigger_output: already running");
1086 sc->sc_prun = 1;
1087 #endif
1088
1089 DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1090 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1091 sc->sc_pintr = intr;
1092 sc->sc_parg = arg;
1093
1094 sic = EREAD4(sc, EAP_SIC);
1095 sic &= ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
1096 sic |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
1097 sampshift = 0;
1098 if (param->precision * param->factor == 16) {
1099 sic |= EAP_P2_S_EB;
1100 sampshift++;
1101 }
1102 if (param->channels == 2) {
1103 sic |= EAP_P2_S_MB;
1104 sampshift++;
1105 }
1106 EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
1107 EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
1108
1109 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1110 ;
1111 if (!p) {
1112 printf("eap_trigger_output: bad addr %p\n", start);
1113 return (EINVAL);
1114 }
1115
1116 DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1117 (int)DMAADDR(p),
1118 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1119 EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1120 EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1121 EWRITE4(sc, EAP_DAC2_SIZE,
1122 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1123
1124 EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1125
1126 if (sc->sc_1371)
1127 EWRITE4(sc, E1371_SRC, 0);
1128
1129 icsc = EREAD4(sc, EAP_ICSC);
1130 EWRITE4(sc, EAP_ICSC, icsc | EAP_DAC2_EN);
1131
1132 DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1133
1134 return (0);
1135 }
1136
1137 int
1138 eap_trigger_input(
1139 void *addr,
1140 void *start,
1141 void *end,
1142 int blksize,
1143 void (*intr)(void *),
1144 void *arg,
1145 struct audio_params *param)
1146 {
1147 struct eap_softc *sc = addr;
1148 struct eap_dma *p;
1149 u_int32_t icsc, sic;
1150 int sampshift;
1151
1152 #ifdef DIAGNOSTIC
1153 if (sc->sc_rrun)
1154 panic("eap_trigger_input: already running");
1155 sc->sc_rrun = 1;
1156 #endif
1157
1158 DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1159 addr, start, end, blksize, intr, arg));
1160 sc->sc_rintr = intr;
1161 sc->sc_rarg = arg;
1162
1163 sic = EREAD4(sc, EAP_SIC);
1164 sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1165 sampshift = 0;
1166 if (param->precision * param->factor == 16) {
1167 sic |= EAP_R1_S_EB;
1168 sampshift++;
1169 }
1170 if (param->channels == 2) {
1171 sic |= EAP_R1_S_MB;
1172 sampshift++;
1173 }
1174 EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
1175 EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
1176
1177 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1178 ;
1179 if (!p) {
1180 printf("eap_trigger_input: bad addr %p\n", start);
1181 return (EINVAL);
1182 }
1183
1184 DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1185 (int)DMAADDR(p),
1186 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1187 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1188 EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1189 EWRITE4(sc, EAP_ADC_SIZE,
1190 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1191
1192 EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1193
1194 if (sc->sc_1371)
1195 EWRITE4(sc, E1371_SRC, 0);
1196
1197 icsc = EREAD4(sc, EAP_ICSC);
1198 EWRITE4(sc, EAP_ICSC, icsc | EAP_ADC_EN);
1199
1200 DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1201
1202 return (0);
1203 }
1204
1205 int
1206 eap_halt_output(void *addr)
1207 {
1208 struct eap_softc *sc = addr;
1209 u_int32_t icsc;
1210
1211 DPRINTF(("eap: eap_halt_output\n"));
1212 icsc = EREAD4(sc, EAP_ICSC);
1213 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1214 #ifdef DIAGNOSTIC
1215 sc->sc_prun = 0;
1216 #endif
1217 return (0);
1218 }
1219
1220 int
1221 eap_halt_input(void *addr)
1222 {
1223 struct eap_softc *sc = addr;
1224 u_int32_t icsc;
1225
1226 DPRINTF(("eap: eap_halt_input\n"));
1227 icsc = EREAD4(sc, EAP_ICSC);
1228 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1229 #ifdef DIAGNOSTIC
1230 sc->sc_rrun = 0;
1231 #endif
1232 return (0);
1233 }
1234
1235 int
1236 eap_getdev(void *addr, struct audio_device *retp)
1237 {
1238 *retp = eap_device;
1239 return (0);
1240 }
1241
1242 int
1243 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1244 {
1245 struct eap_softc *sc = addr;
1246
1247 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1248 }
1249
1250 int
1251 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1252 {
1253 struct eap_softc *sc = addr;
1254
1255 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1256 }
1257
1258 int
1259 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
1260 {
1261 struct eap_softc *sc = addr;
1262
1263 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1264 }
1265
1266 int
1267 eap1371_get_portnum_by_name(struct eap_softc *sc,
1268 char *class, char *device, char *qualifier)
1269 {
1270 return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1271 device, qualifier));
1272 }
1273
1274 void
1275 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
1276 {
1277 eap1370_write_codec(sc, a, d);
1278
1279 sc->sc_port[a] = d;
1280 DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1281 }
1282
1283 int
1284 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1285 {
1286 struct eap_softc *sc = addr;
1287 int lval, rval, l, r, la, ra;
1288 int l1, r1, l2, r2, m, o1, o2;
1289
1290 if (cp->dev == EAP_RECORD_SOURCE) {
1291 if (cp->type != AUDIO_MIXER_SET)
1292 return (EINVAL);
1293 m = sc->sc_record_source = cp->un.mask;
1294 l1 = l2 = r1 = r2 = 0;
1295 if (m & (1 << EAP_VOICE_VOL))
1296 l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1297 if (m & (1 << EAP_FM_VOL))
1298 l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1299 if (m & (1 << EAP_CD_VOL))
1300 l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1301 if (m & (1 << EAP_LINE_VOL))
1302 l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1303 if (m & (1 << EAP_AUX_VOL))
1304 l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1305 if (m & (1 << EAP_MIC_VOL))
1306 l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1307 eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
1308 eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
1309 eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
1310 eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
1311 return (0);
1312 }
1313 if (cp->dev == EAP_OUTPUT_SELECT) {
1314 if (cp->type != AUDIO_MIXER_SET)
1315 return (EINVAL);
1316 m = sc->sc_output_source = cp->un.mask;
1317 o1 = o2 = 0;
1318 if (m & (1 << EAP_VOICE_VOL))
1319 o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1320 if (m & (1 << EAP_FM_VOL))
1321 o1 |= AK_M_FM_L | AK_M_FM_R;
1322 if (m & (1 << EAP_CD_VOL))
1323 o1 |= AK_M_CD_L | AK_M_CD_R;
1324 if (m & (1 << EAP_LINE_VOL))
1325 o1 |= AK_M_LINE_L | AK_M_LINE_R;
1326 if (m & (1 << EAP_AUX_VOL))
1327 o2 |= AK_M_AUX_L | AK_M_AUX_R;
1328 if (m & (1 << EAP_MIC_VOL))
1329 o1 |= AK_M_MIC;
1330 eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
1331 eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
1332 return (0);
1333 }
1334 if (cp->dev == EAP_MIC_PREAMP) {
1335 if (cp->type != AUDIO_MIXER_ENUM)
1336 return (EINVAL);
1337 if (cp->un.ord != 0 && cp->un.ord != 1)
1338 return (EINVAL);
1339 sc->sc_mic_preamp = cp->un.ord;
1340 eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
1341 return (0);
1342 }
1343 if (cp->type != AUDIO_MIXER_VALUE)
1344 return (EINVAL);
1345 if (cp->un.value.num_channels == 1)
1346 lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1347 else if (cp->un.value.num_channels == 2) {
1348 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1349 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1350 } else
1351 return (EINVAL);
1352 ra = -1;
1353 switch (cp->dev) {
1354 case EAP_MASTER_VOL:
1355 l = VOL_TO_ATT5(lval);
1356 r = VOL_TO_ATT5(rval);
1357 la = AK_MASTER_L;
1358 ra = AK_MASTER_R;
1359 break;
1360 case EAP_MIC_VOL:
1361 if (cp->un.value.num_channels != 1)
1362 return (EINVAL);
1363 la = AK_MIC;
1364 goto lr;
1365 case EAP_VOICE_VOL:
1366 la = AK_VOICE_L;
1367 ra = AK_VOICE_R;
1368 goto lr;
1369 case EAP_FM_VOL:
1370 la = AK_FM_L;
1371 ra = AK_FM_R;
1372 goto lr;
1373 case EAP_CD_VOL:
1374 la = AK_CD_L;
1375 ra = AK_CD_R;
1376 goto lr;
1377 case EAP_LINE_VOL:
1378 la = AK_LINE_L;
1379 ra = AK_LINE_R;
1380 goto lr;
1381 case EAP_AUX_VOL:
1382 la = AK_AUX_L;
1383 ra = AK_AUX_R;
1384 lr:
1385 l = VOL_TO_GAIN5(lval);
1386 r = VOL_TO_GAIN5(rval);
1387 break;
1388 default:
1389 return (EINVAL);
1390 }
1391 eap1370_set_mixer(sc, la, l);
1392 if (ra >= 0) {
1393 eap1370_set_mixer(sc, ra, r);
1394 }
1395 return (0);
1396 }
1397
1398 int
1399 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1400 {
1401 struct eap_softc *sc = addr;
1402 int la, ra, l, r;
1403
1404 switch (cp->dev) {
1405 case EAP_RECORD_SOURCE:
1406 if (cp->type != AUDIO_MIXER_SET)
1407 return (EINVAL);
1408 cp->un.mask = sc->sc_record_source;
1409 return (0);
1410 case EAP_OUTPUT_SELECT:
1411 if (cp->type != AUDIO_MIXER_SET)
1412 return (EINVAL);
1413 cp->un.mask = sc->sc_output_source;
1414 return (0);
1415 case EAP_MIC_PREAMP:
1416 if (cp->type != AUDIO_MIXER_ENUM)
1417 return (EINVAL);
1418 cp->un.ord = sc->sc_mic_preamp;
1419 return (0);
1420 case EAP_MASTER_VOL:
1421 l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1422 r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1423 break;
1424 case EAP_MIC_VOL:
1425 if (cp->un.value.num_channels != 1)
1426 return (EINVAL);
1427 la = ra = AK_MIC;
1428 goto lr;
1429 case EAP_VOICE_VOL:
1430 la = AK_VOICE_L;
1431 ra = AK_VOICE_R;
1432 goto lr;
1433 case EAP_FM_VOL:
1434 la = AK_FM_L;
1435 ra = AK_FM_R;
1436 goto lr;
1437 case EAP_CD_VOL:
1438 la = AK_CD_L;
1439 ra = AK_CD_R;
1440 goto lr;
1441 case EAP_LINE_VOL:
1442 la = AK_LINE_L;
1443 ra = AK_LINE_R;
1444 goto lr;
1445 case EAP_AUX_VOL:
1446 la = AK_AUX_L;
1447 ra = AK_AUX_R;
1448 lr:
1449 l = GAIN5_TO_VOL(sc->sc_port[la]);
1450 r = GAIN5_TO_VOL(sc->sc_port[ra]);
1451 break;
1452 default:
1453 return (EINVAL);
1454 }
1455 if (cp->un.value.num_channels == 1)
1456 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1457 else if (cp->un.value.num_channels == 2) {
1458 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1459 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1460 } else
1461 return (EINVAL);
1462 return (0);
1463 }
1464
1465 int
1466 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
1467 {
1468 switch (dip->index) {
1469 case EAP_MASTER_VOL:
1470 dip->type = AUDIO_MIXER_VALUE;
1471 dip->mixer_class = EAP_OUTPUT_CLASS;
1472 dip->prev = dip->next = AUDIO_MIXER_LAST;
1473 strcpy(dip->label.name, AudioNmaster);
1474 dip->un.v.num_channels = 2;
1475 strcpy(dip->un.v.units.name, AudioNvolume);
1476 return (0);
1477 case EAP_VOICE_VOL:
1478 dip->type = AUDIO_MIXER_VALUE;
1479 dip->mixer_class = EAP_INPUT_CLASS;
1480 dip->prev = AUDIO_MIXER_LAST;
1481 dip->next = AUDIO_MIXER_LAST;
1482 strcpy(dip->label.name, AudioNdac);
1483 dip->un.v.num_channels = 2;
1484 strcpy(dip->un.v.units.name, AudioNvolume);
1485 return (0);
1486 case EAP_FM_VOL:
1487 dip->type = AUDIO_MIXER_VALUE;
1488 dip->mixer_class = EAP_INPUT_CLASS;
1489 dip->prev = AUDIO_MIXER_LAST;
1490 dip->next = AUDIO_MIXER_LAST;
1491 strcpy(dip->label.name, AudioNfmsynth);
1492 dip->un.v.num_channels = 2;
1493 strcpy(dip->un.v.units.name, AudioNvolume);
1494 return (0);
1495 case EAP_CD_VOL:
1496 dip->type = AUDIO_MIXER_VALUE;
1497 dip->mixer_class = EAP_INPUT_CLASS;
1498 dip->prev = AUDIO_MIXER_LAST;
1499 dip->next = AUDIO_MIXER_LAST;
1500 strcpy(dip->label.name, AudioNcd);
1501 dip->un.v.num_channels = 2;
1502 strcpy(dip->un.v.units.name, AudioNvolume);
1503 return (0);
1504 case EAP_LINE_VOL:
1505 dip->type = AUDIO_MIXER_VALUE;
1506 dip->mixer_class = EAP_INPUT_CLASS;
1507 dip->prev = AUDIO_MIXER_LAST;
1508 dip->next = AUDIO_MIXER_LAST;
1509 strcpy(dip->label.name, AudioNline);
1510 dip->un.v.num_channels = 2;
1511 strcpy(dip->un.v.units.name, AudioNvolume);
1512 return (0);
1513 case EAP_AUX_VOL:
1514 dip->type = AUDIO_MIXER_VALUE;
1515 dip->mixer_class = EAP_INPUT_CLASS;
1516 dip->prev = AUDIO_MIXER_LAST;
1517 dip->next = AUDIO_MIXER_LAST;
1518 strcpy(dip->label.name, AudioNaux);
1519 dip->un.v.num_channels = 2;
1520 strcpy(dip->un.v.units.name, AudioNvolume);
1521 return (0);
1522 case EAP_MIC_VOL:
1523 dip->type = AUDIO_MIXER_VALUE;
1524 dip->mixer_class = EAP_INPUT_CLASS;
1525 dip->prev = AUDIO_MIXER_LAST;
1526 dip->next = EAP_MIC_PREAMP;
1527 strcpy(dip->label.name, AudioNmicrophone);
1528 dip->un.v.num_channels = 1;
1529 strcpy(dip->un.v.units.name, AudioNvolume);
1530 return (0);
1531 case EAP_RECORD_SOURCE:
1532 dip->mixer_class = EAP_RECORD_CLASS;
1533 dip->prev = dip->next = AUDIO_MIXER_LAST;
1534 strcpy(dip->label.name, AudioNsource);
1535 dip->type = AUDIO_MIXER_SET;
1536 dip->un.s.num_mem = 6;
1537 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1538 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1539 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1540 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1541 strcpy(dip->un.s.member[2].label.name, AudioNline);
1542 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1543 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1544 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1545 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1546 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1547 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1548 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1549 return (0);
1550 case EAP_OUTPUT_SELECT:
1551 dip->mixer_class = EAP_OUTPUT_CLASS;
1552 dip->prev = dip->next = AUDIO_MIXER_LAST;
1553 strcpy(dip->label.name, AudioNselect);
1554 dip->type = AUDIO_MIXER_SET;
1555 dip->un.s.num_mem = 6;
1556 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1557 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1558 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1559 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1560 strcpy(dip->un.s.member[2].label.name, AudioNline);
1561 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1562 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1563 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1564 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1565 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1566 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1567 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1568 return (0);
1569 case EAP_MIC_PREAMP:
1570 dip->type = AUDIO_MIXER_ENUM;
1571 dip->mixer_class = EAP_INPUT_CLASS;
1572 dip->prev = EAP_MIC_VOL;
1573 dip->next = AUDIO_MIXER_LAST;
1574 strcpy(dip->label.name, AudioNpreamp);
1575 dip->un.e.num_mem = 2;
1576 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1577 dip->un.e.member[0].ord = 0;
1578 strcpy(dip->un.e.member[1].label.name, AudioNon);
1579 dip->un.e.member[1].ord = 1;
1580 return (0);
1581 case EAP_OUTPUT_CLASS:
1582 dip->type = AUDIO_MIXER_CLASS;
1583 dip->mixer_class = EAP_OUTPUT_CLASS;
1584 dip->next = dip->prev = AUDIO_MIXER_LAST;
1585 strcpy(dip->label.name, AudioCoutputs);
1586 return (0);
1587 case EAP_RECORD_CLASS:
1588 dip->type = AUDIO_MIXER_CLASS;
1589 dip->mixer_class = EAP_RECORD_CLASS;
1590 dip->next = dip->prev = AUDIO_MIXER_LAST;
1591 strcpy(dip->label.name, AudioCrecord);
1592 return (0);
1593 case EAP_INPUT_CLASS:
1594 dip->type = AUDIO_MIXER_CLASS;
1595 dip->mixer_class = EAP_INPUT_CLASS;
1596 dip->next = dip->prev = AUDIO_MIXER_LAST;
1597 strcpy(dip->label.name, AudioCinputs);
1598 return (0);
1599 }
1600 return (ENXIO);
1601 }
1602
1603 void *
1604 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
1605 {
1606 struct eap_softc *sc = addr;
1607 struct eap_dma *p;
1608 int error;
1609
1610 p = malloc(sizeof(*p), pool, flags);
1611 if (!p)
1612 return (0);
1613 error = eap_allocmem(sc, size, 16, p);
1614 if (error) {
1615 free(p, pool);
1616 return (0);
1617 }
1618 p->next = sc->sc_dmas;
1619 sc->sc_dmas = p;
1620 return (KERNADDR(p));
1621 }
1622
1623 void
1624 eap_free(void *addr, void *ptr, int pool)
1625 {
1626 struct eap_softc *sc = addr;
1627 struct eap_dma **pp, *p;
1628
1629 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1630 if (KERNADDR(p) == ptr) {
1631 eap_freemem(sc, p);
1632 *pp = p->next;
1633 free(p, pool);
1634 return;
1635 }
1636 }
1637 }
1638
1639 size_t
1640 eap_round_buffersize(void *addr, int direction, size_t size)
1641 {
1642 return (size);
1643 }
1644
1645 paddr_t
1646 eap_mappage(void *addr, void *mem, off_t off, int prot)
1647 {
1648 struct eap_softc *sc = addr;
1649 struct eap_dma *p;
1650
1651 if (off < 0)
1652 return (-1);
1653 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1654 ;
1655 if (!p)
1656 return (-1);
1657 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1658 off, prot, BUS_DMA_WAITOK));
1659 }
1660
1661 int
1662 eap_get_props(void *addr)
1663 {
1664 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1665 AUDIO_PROP_FULLDUPLEX);
1666 }
1667
1668 #if NMIDI > 0
1669 int
1670 eap_midi_open(void *addr, int flags,
1671 void (*iintr)(void *, int),
1672 void (*ointr)(void *),
1673 void *arg)
1674 {
1675 struct eap_softc *sc = addr;
1676 u_int32_t uctrl;
1677
1678 sc->sc_iintr = iintr;
1679 sc->sc_ointr = ointr;
1680 sc->sc_arg = arg;
1681
1682 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1683 uctrl = 0;
1684 if (flags & FREAD)
1685 uctrl |= EAP_UC_RXINTEN;
1686 #if 0
1687 /* I don't understand ../midi.c well enough to use output interrupts */
1688 if (flags & FWRITE)
1689 uctrl |= EAP_UC_TXINTEN; */
1690 #endif
1691 EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1692
1693 return (0);
1694 }
1695
1696 void
1697 eap_midi_close(void *addr)
1698 {
1699 struct eap_softc *sc = addr;
1700
1701 tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
1702 EWRITE1(sc, EAP_UART_CONTROL, 0);
1703 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1704
1705 sc->sc_iintr = 0;
1706 sc->sc_ointr = 0;
1707 }
1708
1709 int
1710 eap_midi_output(void *addr, int d)
1711 {
1712 struct eap_softc *sc = addr;
1713 int x;
1714
1715 for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1716 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
1717 EWRITE1(sc, EAP_UART_DATA, d);
1718 return (0);
1719 }
1720 delay(MIDI_BUSY_DELAY);
1721 }
1722 return (EIO);
1723 }
1724
1725 void
1726 eap_midi_getinfo(void *addr, struct midi_info *mi)
1727 {
1728 mi->name = "AudioPCI MIDI UART";
1729 mi->props = MIDI_PROP_CAN_INPUT;
1730 }
1731
1732 #endif
1733