eap.c revision 1.31 1 /* $NetBSD: eap.c,v 1.31 1999/11/02 05:40:59 soren 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 * http://www.ensoniq.com/multimedia/semi_html/html/es1370.zip
54 * ftp://ftp.alsa-project.org/pub/manuals/asahi_kasei/4531.pdf
55 * http://www.ensoniq.com/multimedia/semi_html/html/es1371.zip
56 * ftp://download.intel.com/pc-supp/platform/ac97/ac97r21.pdf
57 */
58
59
60 #include "midi.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/fcntl.h>
66 #include <sys/malloc.h>
67 #include <sys/device.h>
68
69 #include <dev/pci/pcidevs.h>
70 #include <dev/pci/pcivar.h>
71
72 #include <sys/audioio.h>
73 #include <dev/audio_if.h>
74 #include <dev/midi_if.h>
75 #include <dev/mulaw.h>
76 #include <dev/auconv.h>
77 #include <dev/ic/ac97.h>
78
79 #include <machine/bus.h>
80
81 #define PCI_CBIO 0x10
82
83 #define EAP_ICSC 0x00 /* interrupt / chip select control */
84 #define EAP_SERR_DISABLE 0x00000001
85 #define EAP_CDC_EN 0x00000002
86 #define EAP_JYSTK_EN 0x00000004
87 #define EAP_UART_EN 0x00000008
88 #define EAP_ADC_EN 0x00000010
89 #define EAP_DAC2_EN 0x00000020
90 #define EAP_DAC1_EN 0x00000040
91 #define EAP_BREQ 0x00000080
92 #define EAP_XTCL0 0x00000100
93 #define EAP_M_CB 0x00000200
94 #define EAP_CCB_INTRM 0x00000400
95 #define EAP_DAC_SYNC 0x00000800
96 #define EAP_WTSRSEL 0x00003000
97 #define EAP_WTSRSEL_5 0x00000000
98 #define EAP_WTSRSEL_11 0x00001000
99 #define EAP_WTSRSEL_22 0x00002000
100 #define EAP_WTSRSEL_44 0x00003000
101 #define EAP_M_SBB 0x00004000
102 #define EAP_MSFMTSEL 0x00008000
103 #define EAP_SET_PCLKDIV(n) (((n)&0x1fff)<<16)
104 #define EAP_GET_PCLKDIV(n) (((n)>>16)&0x1fff)
105 #define EAP_PCLKBITS 0x1fff0000
106 #define EAP_XTCL1 0x40000000
107 #define EAP_ADC_STOP 0x80000000
108 #define E1371_SYNC_RES (1<<14)
109
110 #define EAP_ICSS 0x04 /* interrupt / chip select status */
111 #define EAP_I_ADC 0x00000001
112 #define EAP_I_DAC2 0x00000002
113 #define EAP_I_DAC1 0x00000004
114 #define EAP_I_UART 0x00000008
115 #define EAP_I_MCCB 0x00000010
116 #define EAP_VC 0x00000060
117 #define EAP_CWRIP 0x00000100
118 #define EAP_CBUSY 0x00000200
119 #define EAP_CSTAT 0x00000400
120 #define EAP_INTR 0x80000000
121
122 #define EAP_UART_DATA 0x08
123 #define EAP_UART_STATUS 0x09
124 #define EAP_US_RXRDY 0x01
125 #define EAP_US_TXRDY 0x02
126 #define EAP_US_TXINT 0x04
127 #define EAP_US_RXINT 0x80
128 #define EAP_UART_CONTROL 0x09
129 #define EAP_UC_CNTRL 0x03
130 #define EAP_UC_TXINTEN 0x20
131 #define EAP_UC_RXINTEN 0x80
132 #define EAP_MEMPAGE 0x0c
133 #define EAP_CODEC 0x10
134 #define EAP_SET_CODEC(a,d) (((a)<<8) | (d))
135
136 /* ES1371 Registers */
137 #define E1371_CODEC 0x14
138 #define E1371_CODEC_WIP (1<<30)
139 #define E1371_CODEC_VALID (1<<31)
140 #define E1371_CODEC_READ (1<<23)
141 #define E1371_SET_CODEC(a,d) (((a)<<16) | (d))
142 #define E1371_SRC 0x10
143 #define E1371_SRC_RAMWE (1<<24)
144 #define E1371_SRC_RBUSY (1<<23)
145 #define E1371_SRC_DISABLE (1<<22)
146 #define E1371_SRC_DISP1 (1<<21)
147 #define E1371_SRC_DISP2 (1<<20)
148 #define E1371_SRC_DISREC (1<<19)
149 #define E1371_SRC_ADDR(a) ((a)<<25)
150 #define E1371_SRC_DATA(d) (d)
151 #define E1371_SRC_DATAMASK 0xffff
152 #define E1371_LEGACY 0x18
153
154 /* ES1371 Sample rate converter registers */
155 #define ESRC_ADC 0x78
156 #define ESRC_DAC1 0x74
157 #define ESRC_DAC2 0x70
158 #define ESRC_ADC_VOLL 0x6c
159 #define ESRC_ADC_VOLR 0x6d
160 #define ESRC_DAC1_VOLL 0x7c
161 #define ESRC_DAC1_VOLR 0x7d
162 #define ESRC_DAC2_VOLL 0x7e
163 #define ESRC_DAC2_VOLR 0x7f
164 #define ESRC_TRUNC_N 0x00
165 #define ESRC_IREGS 0x01
166 #define ESRC_ACF 0x02
167 #define ESRC_VFF 0x03
168 #define ESRC_SET_TRUNC(n) ((n)<<9)
169 #define ESRC_SET_N(n) ((n)<<4)
170 #define ESRC_SMF 0x8000
171 #define ESRC_SET_VFI(n) ((n)<<10)
172 #define ESRC_SET_ACI(n) (n)
173 #define ESRC_SET_ADC_VOL(n) ((n)<<8)
174 #define ESRC_SET_DAC_VOLI(n) ((n)<<12)
175 #define ESRC_SET_DAC_VOLF(n) (n)
176 #define SRC_MAGIC ((1<15)|(1<<13)|(1<<11)|(1<<9))
177
178
179 #define EAP_SIC 0x20
180 #define EAP_P1_S_MB 0x00000001
181 #define EAP_P1_S_EB 0x00000002
182 #define EAP_P2_S_MB 0x00000004
183 #define EAP_P2_S_EB 0x00000008
184 #define EAP_R1_S_MB 0x00000010
185 #define EAP_R1_S_EB 0x00000020
186 #define EAP_P2_DAC_SEN 0x00000040
187 #define EAP_P1_SCT_RLD 0x00000080
188 #define EAP_P1_INTR_EN 0x00000100
189 #define EAP_P2_INTR_EN 0x00000200
190 #define EAP_R1_INTR_EN 0x00000400
191 #define EAP_P1_PAUSE 0x00000800
192 #define EAP_P2_PAUSE 0x00001000
193 #define EAP_P1_LOOP_SEL 0x00002000
194 #define EAP_P2_LOOP_SEL 0x00004000
195 #define EAP_R1_LOOP_SEL 0x00008000
196 #define EAP_SET_P2_ST_INC(i) ((i) << 16)
197 #define EAP_SET_P2_END_INC(i) ((i) << 19)
198 #define EAP_INC_BITS 0x003f0000
199
200 #define EAP_DAC1_CSR 0x24
201 #define EAP_DAC2_CSR 0x28
202 #define EAP_ADC_CSR 0x2c
203 #define EAP_GET_CURRSAMP(r) ((r) >> 16)
204
205 #define EAP_DAC_PAGE 0xc
206 #define EAP_ADC_PAGE 0xd
207 #define EAP_UART_PAGE1 0xe
208 #define EAP_UART_PAGE2 0xf
209
210 #define EAP_DAC1_ADDR 0x30
211 #define EAP_DAC1_SIZE 0x34
212 #define EAP_DAC2_ADDR 0x38
213 #define EAP_DAC2_SIZE 0x3c
214 #define EAP_ADC_ADDR 0x30
215 #define EAP_ADC_SIZE 0x34
216 #define EAP_SET_SIZE(c,s) (((c)<<16) | (s))
217
218 #define EAP_READ_TIMEOUT 5000000
219 #define EAP_WRITE_TIMEOUT 5000000
220
221
222 #define EAP_XTAL_FREQ 1411200 /* 22.5792 / 16 MHz */
223
224 /* AK4531 registers */
225 #define AK_MASTER_L 0x00
226 #define AK_MASTER_R 0x01
227 #define AK_VOICE_L 0x02
228 #define AK_VOICE_R 0x03
229 #define AK_FM_L 0x04
230 #define AK_FM_R 0x05
231 #define AK_CD_L 0x06
232 #define AK_CD_R 0x07
233 #define AK_LINE_L 0x08
234 #define AK_LINE_R 0x09
235 #define AK_AUX_L 0x0a
236 #define AK_AUX_R 0x0b
237 #define AK_MONO1 0x0c
238 #define AK_MONO2 0x0d
239 #define AK_MIC 0x0e
240 #define AK_MONO 0x0f
241 #define AK_OUT_MIXER1 0x10
242 #define AK_M_FM_L 0x40
243 #define AK_M_FM_R 0x20
244 #define AK_M_LINE_L 0x10
245 #define AK_M_LINE_R 0x08
246 #define AK_M_CD_L 0x04
247 #define AK_M_CD_R 0x02
248 #define AK_M_MIC 0x01
249 #define AK_OUT_MIXER2 0x11
250 #define AK_M_AUX_L 0x20
251 #define AK_M_AUX_R 0x10
252 #define AK_M_VOICE_L 0x08
253 #define AK_M_VOICE_R 0x04
254 #define AK_M_MONO2 0x02
255 #define AK_M_MONO1 0x01
256 #define AK_IN_MIXER1_L 0x12
257 #define AK_IN_MIXER1_R 0x13
258 #define AK_IN_MIXER2_L 0x14
259 #define AK_IN_MIXER2_R 0x15
260 #define AK_M_TMIC 0x80
261 #define AK_M_TMONO1 0x40
262 #define AK_M_TMONO2 0x20
263 #define AK_M2_AUX_L 0x10
264 #define AK_M2_AUX_R 0x08
265 #define AK_M_VOICE 0x04
266 #define AK_M2_MONO2 0x02
267 #define AK_M2_MONO1 0x01
268 #define AK_RESET 0x16
269 #define AK_PD 0x02
270 #define AK_NRST 0x01
271 #define AK_CS 0x17
272 #define AK_ADSEL 0x18
273 #define AK_MGAIN 0x19
274 #define AK_NPORTS 0x20
275
276 #define MAX_NPORTS AK_NPORTS
277
278 /* Not sensical for AC97? */
279 #define VOL_TO_ATT5(v) (0x1f - ((v) >> 3))
280 #define VOL_TO_GAIN5(v) VOL_TO_ATT5(v)
281 #define ATT5_TO_VOL(v) ((0x1f - (v)) << 3)
282 #define GAIN5_TO_VOL(v) ATT5_TO_VOL(v)
283 #define VOL_0DB 200
284
285 /* Futzable parms */
286 #define EAP_MASTER_VOL 0
287 #define EAP_VOICE_VOL 1
288 #define EAP_FM_VOL 2
289 #define EAP_VIDEO_VOL 2 /* ES1371 */
290 #define EAP_CD_VOL 3
291 #define EAP_LINE_VOL 4
292 #define EAP_AUX_VOL 5
293 #define EAP_MIC_VOL 6
294 #define EAP_RECORD_SOURCE 7
295 #define EAP_OUTPUT_SELECT 8
296 #define EAP_MIC_PREAMP 9
297 #define EAP_OUTPUT_CLASS 10
298 #define EAP_RECORD_CLASS 11
299 #define EAP_INPUT_CLASS 12
300
301 #define MIDI_BUSY_WAIT 100
302 #define MIDI_BUSY_DELAY 100 /* Delay when UART is busy */
303
304 /* Debug */
305 #ifdef AUDIO_DEBUG
306 #define DPRINTF(x) if (eapdebug) printf x
307 #define DPRINTFN(n,x) if (eapdebug>(n)) printf x
308 int eapdebug = 0;
309 #else
310 #define DPRINTF(x)
311 #define DPRINTFN(n,x)
312 #endif
313
314 int eap_match __P((struct device *, struct cfdata *, void *));
315 void eap_attach __P((struct device *, struct device *, void *));
316 int eap_intr __P((void *));
317
318 struct eap_dma {
319 bus_dmamap_t map;
320 caddr_t addr;
321 bus_dma_segment_t segs[1];
322 int nsegs;
323 size_t size;
324 struct eap_dma *next;
325 };
326 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
327 #define KERNADDR(p) ((void *)((p)->addr))
328
329 struct eap_softc {
330 struct device sc_dev; /* base device */
331 void *sc_ih; /* interrupt vectoring */
332 bus_space_tag_t iot;
333 bus_space_handle_t ioh;
334 bus_dma_tag_t sc_dmatag; /* DMA tag */
335
336 struct eap_dma *sc_dmas;
337
338 void (*sc_pintr)(void *); /* dma completion intr handler */
339 void *sc_parg; /* arg for sc_intr() */
340 #ifdef DIAGNOSTIC
341 char sc_prun;
342 #endif
343
344 void (*sc_rintr)(void *); /* dma completion intr handler */
345 void *sc_rarg; /* arg for sc_intr() */
346 #ifdef DIAGNOSTIC
347 char sc_rrun;
348 #endif
349
350 #if NMIDI > 0
351 void (*sc_iintr)(void *, int); /* midi input ready handler */
352 void (*sc_ointr)(void *); /* midi output ready handler */
353 void *sc_arg;
354 #endif
355
356 u_short sc_port[MAX_NPORTS]; /* mirror of the hardware setting */
357 u_int sc_record_source; /* recording source mask */
358 u_int sc_output_source; /* output source mask */
359 u_int sc_mic_preamp;
360 char sc_1371; /* Using ES1371/AC97 codec */
361
362 struct ac97_codec_if *codec_if;
363 struct ac97_host_if host_if;
364 };
365
366 int eap_allocmem __P((struct eap_softc *, size_t, size_t, struct eap_dma *));
367 int eap_freemem __P((struct eap_softc *, struct eap_dma *));
368
369 #define EWRITE1(sc, r, x) bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x))
370 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
371 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
372 #define EREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
373 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
374 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
375
376 struct cfattach eap_ca = {
377 sizeof(struct eap_softc), eap_match, eap_attach
378 };
379
380 int eap_open __P((void *, int));
381 void eap_close __P((void *));
382 int eap_query_encoding __P((void *, struct audio_encoding *));
383 int eap_set_params __P((void *, int, int, struct audio_params *, struct audio_params *));
384 int eap_round_blocksize __P((void *, int));
385 int eap_trigger_output __P((void *, void *, void *, int, void (*)(void *),
386 void *, struct audio_params *));
387 int eap_trigger_input __P((void *, void *, void *, int, void (*)(void *),
388 void *, struct audio_params *));
389 int eap_halt_output __P((void *));
390 int eap_halt_input __P((void *));
391 void eap_write_codec __P((struct eap_softc *, int, int));
392 int eap_getdev __P((void *, struct audio_device *));
393 int eap_mixer_set_port __P((void *, mixer_ctrl_t *));
394 int eap_mixer_get_port __P((void *, mixer_ctrl_t *));
395 int eap1371_mixer_set_port __P((void *, mixer_ctrl_t *));
396 int eap1371_mixer_get_port __P((void *, mixer_ctrl_t *));
397 int eap_query_devinfo __P((void *, mixer_devinfo_t *));
398 void *eap_malloc __P((void *, int, size_t, int, int));
399 void eap_free __P((void *, void *, int));
400 size_t eap_round_buffersize __P((void *, int, size_t));
401 int eap_mappage __P((void *, void *, int, int));
402 int eap_get_props __P((void *));
403 void eap_set_mixer __P((struct eap_softc *sc, int a, int d));
404 void eap1371_src_wait __P((struct eap_softc *sc));
405 void eap1371_set_adc_rate __P((struct eap_softc *sc, int rate));
406 void eap1371_set_dac_rate __P((struct eap_softc *sc, int rate, int which));
407 int eap1371_src_read __P((struct eap_softc *sc, int a));
408 void eap1371_src_write __P((struct eap_softc *sc, int a, int d));
409 int eap1371_query_devinfo __P((void *addr, mixer_devinfo_t *dip));
410
411 int eap1371_attach_codec __P((void *sc, struct ac97_codec_if *));
412 int eap1371_read_codec __P((void *sc, u_int8_t a, u_int16_t *d));
413 int eap1371_write_codec __P((void *sc, u_int8_t a, u_int16_t d));
414 void eap1371_reset_codec __P((void *sc));
415 int eap1371_get_portnum_by_name __P((struct eap_softc *, char *, char *,
416 char *));
417 #if NMIDI > 0
418 void eap_midi_close __P((void *));
419 void eap_midi_getinfo __P((void *, struct midi_info *));
420 int eap_midi_open __P((void *, int, void (*)(void *, int),
421 void (*)(void *), void *));
422 int eap_midi_output __P((void *, int));
423 #endif
424
425 struct audio_hw_if eap1370_hw_if = {
426 eap_open,
427 eap_close,
428 NULL,
429 eap_query_encoding,
430 eap_set_params,
431 eap_round_blocksize,
432 NULL,
433 NULL,
434 NULL,
435 NULL,
436 NULL,
437 eap_halt_output,
438 eap_halt_input,
439 NULL,
440 eap_getdev,
441 NULL,
442 eap_mixer_set_port,
443 eap_mixer_get_port,
444 eap_query_devinfo,
445 eap_malloc,
446 eap_free,
447 eap_round_buffersize,
448 eap_mappage,
449 eap_get_props,
450 eap_trigger_output,
451 eap_trigger_input,
452 };
453
454 struct audio_hw_if eap1371_hw_if = {
455 eap_open,
456 eap_close,
457 NULL,
458 eap_query_encoding,
459 eap_set_params,
460 eap_round_blocksize,
461 NULL,
462 NULL,
463 NULL,
464 NULL,
465 NULL,
466 eap_halt_output,
467 eap_halt_input,
468 NULL,
469 eap_getdev,
470 NULL,
471 eap1371_mixer_set_port,
472 eap1371_mixer_get_port,
473 eap1371_query_devinfo,
474 eap_malloc,
475 eap_free,
476 eap_round_buffersize,
477 eap_mappage,
478 eap_get_props,
479 eap_trigger_output,
480 eap_trigger_input,
481 };
482
483 #if NMIDI > 0
484 struct midi_hw_if eap_midi_hw_if = {
485 eap_midi_open,
486 eap_midi_close,
487 eap_midi_output,
488 eap_midi_getinfo,
489 0, /* ioctl */
490 };
491 #endif
492
493 struct audio_device eap_device = {
494 "Ensoniq AudioPCI",
495 "",
496 "eap"
497 };
498
499 int
500 eap_match(parent, match, aux)
501 struct device *parent;
502 struct cfdata *match;
503 void *aux;
504 {
505 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
506
507 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ENSONIQ)
508 return (0);
509 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI ||
510 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI97) {
511 return (1);
512 }
513
514 return (0);
515 }
516
517 void
518 eap_write_codec(sc, a, d)
519 struct eap_softc *sc;
520 int a, d;
521 {
522 int icss, to;
523
524 to = EAP_WRITE_TIMEOUT;
525 do {
526 icss = EREAD4(sc, EAP_ICSS);
527 DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
528 if (!to--) {
529 printf("eap: timeout writing to codec\n");
530 return;
531 }
532 } while(icss & EAP_CWRIP); /* XXX could use CSTAT here */
533 EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
534 }
535
536 int
537 eap1371_read_codec(sc_, a, d)
538 void *sc_;
539 u_int8_t a;
540 u_int16_t *d;
541 {
542 struct eap_softc *sc = sc_;
543 int to;
544 int cdc;
545
546 to = EAP_WRITE_TIMEOUT;
547 do {
548 cdc = EREAD4(sc, E1371_CODEC);
549 if (!to--) {
550 printf("eap: timeout writing to codec\n");
551 return 1;
552 }
553 } while (cdc & E1371_CODEC_WIP);
554
555 /* just do it */
556 eap1371_src_wait(sc);
557 EWRITE4(sc, E1371_CODEC, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
558
559 for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
560 if ((cdc = EREAD4(sc, E1371_CODEC)) & E1371_CODEC_VALID)
561 break;
562 }
563
564 if (to == EAP_WRITE_TIMEOUT) {
565 DPRINTF(("eap1371: read codec timeout\n"));
566 }
567
568 *d = cdc & 0xffff;
569
570 DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
571
572 return (0);
573 }
574
575 int
576 eap1371_write_codec(sc_, a, d)
577 void *sc_;
578 u_int8_t a;
579 u_int16_t d;
580 {
581 struct eap_softc *sc = sc_;
582 int to;
583 int cdc;
584
585 to = EAP_WRITE_TIMEOUT;
586 do {
587 cdc = EREAD4(sc, E1371_CODEC);
588 if (!to--) {
589 printf("eap: timeout writing to codec\n");
590 return (1);
591 }
592 } while (cdc & E1371_CODEC_WIP);
593
594 /* just do it */
595 eap1371_src_wait(sc);
596 EWRITE4(sc, E1371_CODEC, E1371_SET_CODEC(a, d));
597 DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
598
599 return (0);
600 }
601
602 void
603 eap1371_src_wait(sc)
604 struct eap_softc *sc;
605 {
606 int to;
607 int src;
608
609 to = EAP_READ_TIMEOUT;
610 do {
611 src = EREAD4(sc, E1371_SRC);
612 if (!to--) {
613 printf("eap: timeout waiting for sample rate"
614 "converter\n");
615 return;
616 }
617 } while (src & E1371_SRC_RBUSY);
618 }
619
620 int
621 eap1371_src_read(sc, a)
622 struct eap_softc *sc;
623 int a;
624 {
625 int r;
626
627 eap1371_src_wait(sc);
628 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE | E1371_SRC_DISP1 |
629 E1371_SRC_DISP2 | E1371_SRC_DISREC);
630 r |= E1371_SRC_ADDR(a);
631 EWRITE4(sc, E1371_SRC, r);
632 r = EREAD4(sc, E1371_SRC) & E1371_SRC_DATAMASK;
633 return r;
634 }
635
636 void
637 eap1371_src_write(sc, a, d)
638 struct eap_softc *sc;
639 int a,d;
640 {
641 int r;
642
643 eap1371_src_wait(sc);
644 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE | E1371_SRC_DISP1 |
645 E1371_SRC_DISP2 | E1371_SRC_DISREC);
646 r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
647 EWRITE4(sc, E1371_SRC, r);
648 }
649
650 void
651 eap1371_set_adc_rate(sc, rate)
652 struct eap_softc *sc;
653 int rate;
654 {
655 int freq, n, truncm;
656 int out;
657
658 /* Whatever, it works, so I'll leave it :) */
659
660 if (rate > 48000)
661 rate = 48000;
662 if (rate < 4000)
663 rate = 4000;
664 n = rate / 3000;
665 if ((1 << n) & SRC_MAGIC)
666 n--;
667 truncm = ((21 * n) - 1) | 1;
668 freq = ((48000 << 15) / rate) * n;
669 if (rate >= 24000) {
670 if (truncm > 239)
671 truncm = 239;
672 out = ESRC_SET_TRUNC((239 - truncm) / 2);
673 } else {
674 if (truncm > 119)
675 truncm = 119;
676 out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
677 }
678 out |= ESRC_SET_N(n);
679 eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
680
681
682 out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
683 eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
684 ESRC_SET_VFI(freq >> 15));
685 eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
686 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
687 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
688 }
689
690 void
691 eap1371_set_dac_rate(sc, rate, which)
692 struct eap_softc *sc;
693 int rate;
694 int which;
695 {
696 int dac = (which == 1) ? ESRC_DAC1 : ESRC_DAC2;
697 int freq, r;
698
699 /* Whatever, it works, so I'll leave it :) */
700
701 if (rate > 48000)
702 rate = 48000;
703 if (rate < 4000)
704 rate = 4000;
705 freq = (rate << 15) / 3000;
706
707 eap1371_src_wait(sc);
708 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
709 E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
710 r |= (which == 1) ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
711 EWRITE4(sc, E1371_SRC, r);
712 r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
713 eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
714 eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
715 r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
716 E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
717 r &= ~((which == 1) ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
718 EWRITE4(sc, E1371_SRC, r);
719 }
720
721 void
722 eap_attach(parent, self, aux)
723 struct device *parent;
724 struct device *self;
725 void *aux;
726 {
727 struct eap_softc *sc = (struct eap_softc *)self;
728 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
729 pci_chipset_tag_t pc = pa->pa_pc;
730 struct audio_hw_if *eap_hw_if;
731 char const *intrstr;
732 pci_intr_handle_t ih;
733 pcireg_t csr;
734 char devinfo[256];
735 mixer_ctrl_t ctl;
736 int i;
737
738 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
739 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
740
741 /* Flag if we're "creative" */
742 sc->sc_1371 = PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI97;
743
744 /* Map I/O register */
745 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
746 &sc->iot, &sc->ioh, NULL, NULL)) {
747 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
748 return;
749 }
750
751 sc->sc_dmatag = pa->pa_dmat;
752
753 /* Enable the device. */
754 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
755 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
756 csr | PCI_COMMAND_MASTER_ENABLE);
757
758 /* Map and establish the interrupt. */
759 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
760 pa->pa_intrline, &ih)) {
761 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
762 return;
763 }
764 intrstr = pci_intr_string(pc, ih);
765 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
766 if (sc->sc_ih == NULL) {
767 printf("%s: couldn't establish interrupt",
768 sc->sc_dev.dv_xname);
769 if (intrstr != NULL)
770 printf(" at %s", intrstr);
771 printf("\n");
772 return;
773 }
774 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
775
776 if (!sc->sc_1371) {
777 /* Enable interrupts and looping mode. */
778 /* enable the parts we need */
779 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
780 EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
781
782 /* reset codec */
783 /* normal operation */
784 /* select codec clocks */
785 eap_write_codec(sc, AK_RESET, AK_PD);
786 eap_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
787 eap_write_codec(sc, AK_CS, 0x0);
788
789 eap_hw_if = &eap1370_hw_if;
790
791 /* Enable all relevant mixer switches. */
792 ctl.dev = EAP_OUTPUT_SELECT;
793 ctl.type = AUDIO_MIXER_SET;
794 ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
795 1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
796 1 << EAP_MIC_VOL;
797 eap_hw_if->set_port(sc, &ctl);
798
799 ctl.type = AUDIO_MIXER_VALUE;
800 ctl.un.value.num_channels = 1;
801 for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
802 ctl.dev++) {
803 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
804 eap_hw_if->set_port(sc, &ctl);
805 }
806 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
807 eap_hw_if->set_port(sc, &ctl);
808 ctl.dev = EAP_MIC_PREAMP;
809 ctl.type = AUDIO_MIXER_ENUM;
810 ctl.un.ord = 0;
811 eap_hw_if->set_port(sc, &ctl);
812 ctl.dev = EAP_RECORD_SOURCE;
813 ctl.type = AUDIO_MIXER_SET;
814 ctl.un.mask = 1 << EAP_MIC_VOL;
815 eap_hw_if->set_port(sc, &ctl);
816 } else {
817 /* clean slate */
818 EWRITE4(sc, EAP_SIC, 0);
819 EWRITE4(sc, EAP_ICSC, 0);
820 EWRITE4(sc, E1371_LEGACY, 0);
821
822 /* Reset from es1371's perspective */
823 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
824 delay(20);
825 EWRITE4(sc, EAP_ICSC, 0);
826
827 /* must properly reprogram sample rate converter,
828 * or it locks up. Set some defaults for the life of the
829 * machine, and set up a sb default sample rate.
830 */
831 EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
832 for (i=0; i<0x80; i++)
833 eap1371_src_write(sc, i, 0);
834 eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
835 eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
836 eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
837 eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
838 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
839 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
840 eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
841 eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
842 eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
843 eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
844 eap1371_set_adc_rate(sc, 22050);
845 eap1371_set_dac_rate(sc, 22050, 1);
846 eap1371_set_dac_rate(sc, 22050, 2);
847
848 EWRITE4(sc, E1371_SRC, 0);
849
850 /* Reset codec */
851
852 /* Interrupt enable */
853 sc->host_if.arg = sc;
854 sc->host_if.attach = eap1371_attach_codec;
855 sc->host_if.read = eap1371_read_codec;
856 sc->host_if.write = eap1371_write_codec;
857 sc->host_if.reset = eap1371_reset_codec;
858
859 if (ac97_attach(&sc->host_if) == 0) {
860 /* Interrupt enable */
861 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
862 } else
863 return;
864
865 eap_hw_if = &eap1371_hw_if;
866
867 /* Just enable the DAC and master volumes by default */
868 ctl.type = AUDIO_MIXER_ENUM;
869 ctl.un.ord = 0; /* off */
870 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
871 AudioNmaster, AudioNmute);
872 eap1371_mixer_set_port(sc, &ctl);
873 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
874 AudioNdac, AudioNmute);
875 eap1371_mixer_set_port(sc, &ctl);
876 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
877 AudioNvolume, AudioNmute);
878 eap1371_mixer_set_port(sc, &ctl);
879
880
881 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
882 AudioNsource, NULL);
883 ctl.type = AUDIO_MIXER_ENUM;
884 ctl.un.ord = 0;
885 eap1371_mixer_set_port(sc, &ctl);
886
887 }
888
889 audio_attach_mi(eap_hw_if, sc, &sc->sc_dev);
890
891 #if NMIDI > 0
892 midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
893 #endif
894 }
895
896 int
897 eap1371_attach_codec(sc_, codec_if)
898 void *sc_;
899 struct ac97_codec_if *codec_if;
900 {
901 struct eap_softc *sc = sc_;
902
903 sc->codec_if = codec_if;
904 return (0);
905 }
906
907 void
908 eap1371_reset_codec(sc_)
909 void *sc_;
910 {
911 struct eap_softc *sc = sc_;
912 u_int32_t icsc = EREAD4(sc, EAP_ICSC);
913
914 EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
915 delay(100);
916 EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
917
918 return;
919 }
920
921 int
922 eap_intr(p)
923 void *p;
924 {
925 struct eap_softc *sc = p;
926 u_int32_t intr, sic;
927
928 intr = EREAD4(sc, EAP_ICSS);
929 if (!(intr & EAP_INTR))
930 return (0);
931 sic = EREAD4(sc, EAP_SIC);
932 DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
933 if (intr & EAP_I_ADC) {
934 /*
935 * XXX This is a hack!
936 * The EAP chip sometimes generates the recording interrupt
937 * while it is still transferring the data. To make sure
938 * it has all arrived we busy wait until the count is right.
939 * The transfer we are waiting for is 8 longwords.
940 */
941 int s, nw, n;
942 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
943 s = EREAD4(sc, EAP_ADC_CSR);
944 nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
945 n = 0;
946 while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
947 delay(10);
948 if (++n > 100) {
949 printf("eapintr: dma fix timeout");
950 break;
951 }
952 }
953 /* Continue with normal interrupt handling. */
954 EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
955 EWRITE4(sc, EAP_SIC, sic);
956 if (sc->sc_rintr)
957 sc->sc_rintr(sc->sc_rarg);
958 }
959 if (intr & EAP_I_DAC2) {
960 EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
961 EWRITE4(sc, EAP_SIC, sic);
962 if (sc->sc_pintr)
963 sc->sc_pintr(sc->sc_parg);
964 }
965 #if NMIDI > 0
966 if (intr & EAP_I_UART) {
967 u_int32_t data;
968
969 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
970 while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
971 data = EREAD1(sc, EAP_UART_DATA);
972 if (sc->sc_iintr)
973 sc->sc_iintr(sc->sc_arg, data);
974 }
975 }
976 }
977 #endif
978 return (1);
979 }
980
981 int
982 eap_allocmem(sc, size, align, p)
983 struct eap_softc *sc;
984 size_t size;
985 size_t align;
986 struct eap_dma *p;
987 {
988 int error;
989
990 p->size = size;
991 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
992 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
993 &p->nsegs, BUS_DMA_NOWAIT);
994 if (error)
995 return (error);
996
997 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
998 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
999 if (error)
1000 goto free;
1001
1002 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1003 0, BUS_DMA_NOWAIT, &p->map);
1004 if (error)
1005 goto unmap;
1006
1007 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1008 BUS_DMA_NOWAIT);
1009 if (error)
1010 goto destroy;
1011 return (0);
1012
1013 destroy:
1014 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1015 unmap:
1016 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1017 free:
1018 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1019 return (error);
1020 }
1021
1022 int
1023 eap_freemem(sc, p)
1024 struct eap_softc *sc;
1025 struct eap_dma *p;
1026 {
1027 bus_dmamap_unload(sc->sc_dmatag, p->map);
1028 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1029 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1030 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1031 return (0);
1032 }
1033
1034 int
1035 eap_open(addr, flags)
1036 void *addr;
1037 int flags;
1038 {
1039 return (0);
1040 }
1041
1042 /*
1043 * Close function is called at splaudio().
1044 */
1045 void
1046 eap_close(addr)
1047 void *addr;
1048 {
1049 struct eap_softc *sc = addr;
1050
1051 eap_halt_output(sc);
1052 eap_halt_input(sc);
1053
1054 sc->sc_pintr = 0;
1055 sc->sc_rintr = 0;
1056 }
1057
1058 int
1059 eap_query_encoding(addr, fp)
1060 void *addr;
1061 struct audio_encoding *fp;
1062 {
1063 switch (fp->index) {
1064 case 0:
1065 strcpy(fp->name, AudioEulinear);
1066 fp->encoding = AUDIO_ENCODING_ULINEAR;
1067 fp->precision = 8;
1068 fp->flags = 0;
1069 return (0);
1070 case 1:
1071 strcpy(fp->name, AudioEmulaw);
1072 fp->encoding = AUDIO_ENCODING_ULAW;
1073 fp->precision = 8;
1074 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1075 return (0);
1076 case 2:
1077 strcpy(fp->name, AudioEalaw);
1078 fp->encoding = AUDIO_ENCODING_ALAW;
1079 fp->precision = 8;
1080 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1081 return (0);
1082 case 3:
1083 strcpy(fp->name, AudioEslinear);
1084 fp->encoding = AUDIO_ENCODING_SLINEAR;
1085 fp->precision = 8;
1086 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1087 return (0);
1088 case 4:
1089 strcpy(fp->name, AudioEslinear_le);
1090 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
1091 fp->precision = 16;
1092 fp->flags = 0;
1093 return (0);
1094 case 5:
1095 strcpy(fp->name, AudioEulinear_le);
1096 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
1097 fp->precision = 16;
1098 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1099 return (0);
1100 case 6:
1101 strcpy(fp->name, AudioEslinear_be);
1102 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1103 fp->precision = 16;
1104 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1105 return (0);
1106 case 7:
1107 strcpy(fp->name, AudioEulinear_be);
1108 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1109 fp->precision = 16;
1110 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1111 return (0);
1112 default:
1113 return (EINVAL);
1114 }
1115 }
1116
1117 int
1118 eap_set_params(addr, setmode, usemode, play, rec)
1119 void *addr;
1120 int setmode, usemode;
1121 struct audio_params *play, *rec;
1122 {
1123 struct eap_softc *sc = addr;
1124 struct audio_params *p;
1125 int mode;
1126 u_int32_t div;
1127
1128 /*
1129 * The es1370 only has one clock, so make the sample rates match.
1130 */
1131 if (!sc->sc_1371) {
1132 if (play->sample_rate != rec->sample_rate &&
1133 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1134 if (setmode == AUMODE_PLAY) {
1135 rec->sample_rate = play->sample_rate;
1136 setmode |= AUMODE_RECORD;
1137 } else if (setmode == AUMODE_RECORD) {
1138 play->sample_rate = rec->sample_rate;
1139 setmode |= AUMODE_PLAY;
1140 } else
1141 return (EINVAL);
1142 }
1143 }
1144
1145 for (mode = AUMODE_RECORD; mode != -1;
1146 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1147 if ((setmode & mode) == 0)
1148 continue;
1149
1150 p = mode == AUMODE_PLAY ? play : rec;
1151
1152 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
1153 (p->precision != 8 && p->precision != 16) ||
1154 (p->channels != 1 && p->channels != 2))
1155 return (EINVAL);
1156
1157 p->factor = 1;
1158 p->sw_code = 0;
1159 switch (p->encoding) {
1160 case AUDIO_ENCODING_SLINEAR_BE:
1161 if (p->precision == 16)
1162 p->sw_code = swap_bytes;
1163 else
1164 p->sw_code = change_sign8;
1165 break;
1166 case AUDIO_ENCODING_SLINEAR_LE:
1167 if (p->precision != 16)
1168 p->sw_code = change_sign8;
1169 break;
1170 case AUDIO_ENCODING_ULINEAR_BE:
1171 if (p->precision == 16) {
1172 if (mode == AUMODE_PLAY)
1173 p->sw_code = swap_bytes_change_sign16_le;
1174 else
1175 p->sw_code = change_sign16_swap_bytes_le;
1176 }
1177 break;
1178 case AUDIO_ENCODING_ULINEAR_LE:
1179 if (p->precision == 16)
1180 p->sw_code = change_sign16_le;
1181 break;
1182 case AUDIO_ENCODING_ULAW:
1183 if (mode == AUMODE_PLAY) {
1184 p->factor = 2;
1185 p->sw_code = mulaw_to_slinear16_le;
1186 } else
1187 p->sw_code = ulinear8_to_mulaw;
1188 break;
1189 case AUDIO_ENCODING_ALAW:
1190 if (mode == AUMODE_PLAY) {
1191 p->factor = 2;
1192 p->sw_code = alaw_to_slinear16_le;
1193 } else
1194 p->sw_code = ulinear8_to_alaw;
1195 break;
1196 default:
1197 return (EINVAL);
1198 }
1199 }
1200
1201 if (sc->sc_1371) {
1202 eap1371_set_dac_rate(sc, play->sample_rate, 1);
1203 eap1371_set_dac_rate(sc, play->sample_rate, 2);
1204 eap1371_set_adc_rate(sc, rec->sample_rate);
1205 } else {
1206 /* Set the speed */
1207 DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1208 EREAD4(sc, EAP_ICSC)));
1209 div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1210 /*
1211 * XXX
1212 * The -2 isn't documented, but seemed to make the wall
1213 * time match
1214 * what I expect. - mycroft
1215 */
1216 if (usemode == AUMODE_RECORD)
1217 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1218 rec->sample_rate - 2);
1219 else
1220 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1221 play->sample_rate - 2);
1222 div |= EAP_CCB_INTRM;
1223 EWRITE4(sc, EAP_ICSC, div);
1224 DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1225 }
1226
1227 return (0);
1228 }
1229
1230 int
1231 eap_round_blocksize(addr, blk)
1232 void *addr;
1233 int blk;
1234 {
1235 return (blk & -32); /* keep good alignment */
1236 }
1237
1238 int
1239 eap_trigger_output(addr, start, end, blksize, intr, arg, param)
1240 void *addr;
1241 void *start, *end;
1242 int blksize;
1243 void (*intr) __P((void *));
1244 void *arg;
1245 struct audio_params *param;
1246 {
1247 struct eap_softc *sc = addr;
1248 struct eap_dma *p;
1249 u_int32_t icsc, sic;
1250 int sampshift;
1251
1252 #ifdef DIAGNOSTIC
1253 if (sc->sc_prun)
1254 panic("eap_trigger_output: already running");
1255 sc->sc_prun = 1;
1256 #endif
1257
1258 DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1259 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1260 sc->sc_pintr = intr;
1261 sc->sc_parg = arg;
1262
1263 icsc = EREAD4(sc, EAP_ICSC);
1264 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1265
1266 sic = EREAD4(sc, EAP_SIC);
1267 sic &= ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
1268 sic |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
1269 sampshift = 0;
1270 if (param->precision * param->factor == 16) {
1271 sic |= EAP_P2_S_EB;
1272 sampshift++;
1273 }
1274 if (param->channels == 2) {
1275 sic |= EAP_P2_S_MB;
1276 sampshift++;
1277 }
1278 EWRITE4(sc, EAP_SIC, sic);
1279
1280 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1281 ;
1282 if (!p) {
1283 printf("eap_trigger_output: bad addr %p\n", start);
1284 return (EINVAL);
1285 }
1286
1287 DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1288 (int)DMAADDR(p),
1289 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1290 EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1291 EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1292 EWRITE4(sc, EAP_DAC2_SIZE,
1293 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1294
1295 EWRITE2(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1296
1297 EWRITE4(sc, EAP_ICSC, icsc | EAP_DAC2_EN);
1298
1299 DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1300
1301 return (0);
1302 }
1303
1304 int
1305 eap_trigger_input(addr, start, end, blksize, intr, arg, param)
1306 void *addr;
1307 void *start, *end;
1308 int blksize;
1309 void (*intr) __P((void *));
1310 void *arg;
1311 struct audio_params *param;
1312 {
1313 struct eap_softc *sc = addr;
1314 struct eap_dma *p;
1315 u_int32_t icsc, sic;
1316 int sampshift;
1317
1318 #ifdef DIAGNOSTIC
1319 if (sc->sc_rrun)
1320 panic("eap_trigger_input: already running");
1321 sc->sc_rrun = 1;
1322 #endif
1323
1324 DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1325 addr, start, end, blksize, intr, arg));
1326 sc->sc_rintr = intr;
1327 sc->sc_rarg = arg;
1328
1329 icsc = EREAD4(sc, EAP_ICSC);
1330 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1331
1332 sic = EREAD4(sc, EAP_SIC);
1333 sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1334 sampshift = 0;
1335 if (param->precision * param->factor == 16) {
1336 sic |= EAP_R1_S_EB;
1337 sampshift++;
1338 }
1339 if (param->channels == 2) {
1340 sic |= EAP_R1_S_MB;
1341 sampshift++;
1342 }
1343 EWRITE4(sc, EAP_SIC, sic);
1344
1345 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1346 ;
1347 if (!p) {
1348 printf("eap_trigger_input: bad addr %p\n", start);
1349 return (EINVAL);
1350 }
1351
1352 DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1353 (int)DMAADDR(p),
1354 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1355 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1356 EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1357 EWRITE4(sc, EAP_ADC_SIZE,
1358 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1359
1360 EWRITE2(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1361
1362 EWRITE4(sc, EAP_ICSC, icsc | EAP_ADC_EN);
1363
1364 DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1365
1366 return (0);
1367 }
1368
1369 int
1370 eap_halt_output(addr)
1371 void *addr;
1372 {
1373 struct eap_softc *sc = addr;
1374 u_int32_t icsc;
1375
1376 DPRINTF(("eap: eap_halt_output\n"));
1377 icsc = EREAD4(sc, EAP_ICSC);
1378 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1379 #ifdef DIAGNOSTIC
1380 sc->sc_prun = 0;
1381 #endif
1382 return (0);
1383 }
1384
1385 int
1386 eap_halt_input(addr)
1387 void *addr;
1388 {
1389 struct eap_softc *sc = addr;
1390 u_int32_t icsc;
1391
1392 DPRINTF(("eap: eap_halt_input\n"));
1393 icsc = EREAD4(sc, EAP_ICSC);
1394 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1395 #ifdef DIAGNOSTIC
1396 sc->sc_rrun = 0;
1397 #endif
1398 return (0);
1399 }
1400
1401 int
1402 eap_getdev(addr, retp)
1403 void *addr;
1404 struct audio_device *retp;
1405 {
1406 *retp = eap_device;
1407 return (0);
1408 }
1409
1410 int
1411 eap1371_mixer_set_port(addr, cp)
1412 void *addr;
1413 mixer_ctrl_t *cp;
1414 {
1415 struct eap_softc *sc = addr;
1416
1417 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1418 }
1419
1420 int
1421 eap1371_mixer_get_port(addr, cp)
1422 void *addr;
1423 mixer_ctrl_t *cp;
1424 {
1425 struct eap_softc *sc = addr;
1426
1427 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1428 }
1429
1430 int
1431 eap1371_query_devinfo(addr, dip)
1432 void *addr;
1433 mixer_devinfo_t *dip;
1434 {
1435 struct eap_softc *sc = addr;
1436
1437 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1438 }
1439
1440 int
1441 eap1371_get_portnum_by_name(sc, class, device, qualifier)
1442 struct eap_softc *sc;
1443 char *class, *device, *qualifier;
1444 {
1445 return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1446 device, qualifier));
1447 }
1448
1449 void
1450 eap_set_mixer(sc, a, d)
1451 struct eap_softc *sc;
1452 int a, d;
1453 {
1454 eap_write_codec(sc, a, d);
1455
1456 sc->sc_port[a] = d;
1457 DPRINTFN(1, ("eap_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1458 }
1459
1460 int
1461 eap_mixer_set_port(addr, cp)
1462 void *addr;
1463 mixer_ctrl_t *cp;
1464 {
1465 struct eap_softc *sc = addr;
1466 int lval, rval, l, r, la, ra;
1467 int l1, r1, l2, r2, m, o1, o2;
1468
1469 if (cp->dev == EAP_RECORD_SOURCE) {
1470 if (cp->type != AUDIO_MIXER_SET)
1471 return (EINVAL);
1472 m = sc->sc_record_source = cp->un.mask;
1473 l1 = l2 = r1 = r2 = 0;
1474 if (m & (1 << EAP_VOICE_VOL))
1475 l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1476 if (m & (1 << EAP_FM_VOL))
1477 l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1478 if (m & (1 << EAP_CD_VOL))
1479 l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1480 if (m & (1 << EAP_LINE_VOL))
1481 l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1482 if (m & (1 << EAP_AUX_VOL))
1483 l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1484 if (m & (1 << EAP_MIC_VOL))
1485 l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1486 eap_set_mixer(sc, AK_IN_MIXER1_L, l1);
1487 eap_set_mixer(sc, AK_IN_MIXER1_R, r1);
1488 eap_set_mixer(sc, AK_IN_MIXER2_L, l2);
1489 eap_set_mixer(sc, AK_IN_MIXER2_R, r2);
1490 return (0);
1491 }
1492 if (cp->dev == EAP_OUTPUT_SELECT) {
1493 if (cp->type != AUDIO_MIXER_SET)
1494 return (EINVAL);
1495 m = sc->sc_output_source = cp->un.mask;
1496 o1 = o2 = 0;
1497 if (m & (1 << EAP_VOICE_VOL))
1498 o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1499 if (m & (1 << EAP_FM_VOL))
1500 o1 |= AK_M_FM_L | AK_M_FM_R;
1501 if (m & (1 << EAP_CD_VOL))
1502 o1 |= AK_M_CD_L | AK_M_CD_R;
1503 if (m & (1 << EAP_LINE_VOL))
1504 o1 |= AK_M_LINE_L | AK_M_LINE_R;
1505 if (m & (1 << EAP_AUX_VOL))
1506 o2 |= AK_M_AUX_L | AK_M_AUX_R;
1507 if (m & (1 << EAP_MIC_VOL))
1508 o1 |= AK_M_MIC;
1509 eap_set_mixer(sc, AK_OUT_MIXER1, o1);
1510 eap_set_mixer(sc, AK_OUT_MIXER2, o2);
1511 return (0);
1512 }
1513 if (cp->dev == EAP_MIC_PREAMP) {
1514 if (cp->type != AUDIO_MIXER_ENUM)
1515 return (EINVAL);
1516 if (cp->un.ord != 0 && cp->un.ord != 1)
1517 return (EINVAL);
1518 sc->sc_mic_preamp = cp->un.ord;
1519 eap_set_mixer(sc, AK_MGAIN, cp->un.ord);
1520 return (0);
1521 }
1522 if (cp->type != AUDIO_MIXER_VALUE)
1523 return (EINVAL);
1524 if (cp->un.value.num_channels == 1)
1525 lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1526 else if (cp->un.value.num_channels == 2) {
1527 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1528 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1529 } else
1530 return (EINVAL);
1531 ra = -1;
1532 switch (cp->dev) {
1533 case EAP_MASTER_VOL:
1534 l = VOL_TO_ATT5(lval);
1535 r = VOL_TO_ATT5(rval);
1536 la = AK_MASTER_L;
1537 ra = AK_MASTER_R;
1538 break;
1539 case EAP_MIC_VOL:
1540 if (cp->un.value.num_channels != 1)
1541 return (EINVAL);
1542 la = AK_MIC;
1543 goto lr;
1544 case EAP_VOICE_VOL:
1545 la = AK_VOICE_L;
1546 ra = AK_VOICE_R;
1547 goto lr;
1548 case EAP_FM_VOL:
1549 la = AK_FM_L;
1550 ra = AK_FM_R;
1551 goto lr;
1552 case EAP_CD_VOL:
1553 la = AK_CD_L;
1554 ra = AK_CD_R;
1555 goto lr;
1556 case EAP_LINE_VOL:
1557 la = AK_LINE_L;
1558 ra = AK_LINE_R;
1559 goto lr;
1560 case EAP_AUX_VOL:
1561 la = AK_AUX_L;
1562 ra = AK_AUX_R;
1563 lr:
1564 l = VOL_TO_GAIN5(lval);
1565 r = VOL_TO_GAIN5(rval);
1566 break;
1567 default:
1568 return (EINVAL);
1569 }
1570 eap_set_mixer(sc, la, l);
1571 if (ra >= 0) {
1572 eap_set_mixer(sc, ra, r);
1573 }
1574 return (0);
1575 }
1576
1577 int
1578 eap_mixer_get_port(addr, cp)
1579 void *addr;
1580 mixer_ctrl_t *cp;
1581 {
1582 struct eap_softc *sc = addr;
1583 int la, ra, l, r;
1584
1585 switch (cp->dev) {
1586 case EAP_RECORD_SOURCE:
1587 if (cp->type != AUDIO_MIXER_SET)
1588 return (EINVAL);
1589 cp->un.mask = sc->sc_record_source;
1590 return (0);
1591 case EAP_OUTPUT_SELECT:
1592 if (cp->type != AUDIO_MIXER_SET)
1593 return (EINVAL);
1594 cp->un.mask = sc->sc_output_source;
1595 return (0);
1596 case EAP_MIC_PREAMP:
1597 if (cp->type != AUDIO_MIXER_ENUM)
1598 return (EINVAL);
1599 cp->un.ord = sc->sc_mic_preamp;
1600 return (0);
1601 case EAP_MASTER_VOL:
1602 l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1603 r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1604 break;
1605 case EAP_MIC_VOL:
1606 if (cp->un.value.num_channels != 1)
1607 return (EINVAL);
1608 la = ra = AK_MIC;
1609 goto lr;
1610 case EAP_VOICE_VOL:
1611 la = AK_VOICE_L;
1612 ra = AK_VOICE_R;
1613 goto lr;
1614 case EAP_FM_VOL:
1615 la = AK_FM_L;
1616 ra = AK_FM_R;
1617 goto lr;
1618 case EAP_CD_VOL:
1619 la = AK_CD_L;
1620 ra = AK_CD_R;
1621 goto lr;
1622 case EAP_LINE_VOL:
1623 la = AK_LINE_L;
1624 ra = AK_LINE_R;
1625 goto lr;
1626 case EAP_AUX_VOL:
1627 la = AK_AUX_L;
1628 ra = AK_AUX_R;
1629 lr:
1630 l = GAIN5_TO_VOL(sc->sc_port[la]);
1631 r = GAIN5_TO_VOL(sc->sc_port[ra]);
1632 break;
1633 default:
1634 return (EINVAL);
1635 }
1636 if (cp->un.value.num_channels == 1)
1637 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1638 else if (cp->un.value.num_channels == 2) {
1639 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1640 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1641 } else
1642 return (EINVAL);
1643 return (0);
1644 }
1645
1646 int
1647 eap_query_devinfo(addr, dip)
1648 void *addr;
1649 mixer_devinfo_t *dip;
1650 {
1651 switch (dip->index) {
1652 case EAP_MASTER_VOL:
1653 dip->type = AUDIO_MIXER_VALUE;
1654 dip->mixer_class = EAP_OUTPUT_CLASS;
1655 dip->prev = dip->next = AUDIO_MIXER_LAST;
1656 strcpy(dip->label.name, AudioNmaster);
1657 dip->un.v.num_channels = 2;
1658 strcpy(dip->un.v.units.name, AudioNvolume);
1659 return (0);
1660 case EAP_VOICE_VOL:
1661 dip->type = AUDIO_MIXER_VALUE;
1662 dip->mixer_class = EAP_INPUT_CLASS;
1663 dip->prev = AUDIO_MIXER_LAST;
1664 dip->next = AUDIO_MIXER_LAST;
1665 strcpy(dip->label.name, AudioNdac);
1666 dip->un.v.num_channels = 2;
1667 strcpy(dip->un.v.units.name, AudioNvolume);
1668 return (0);
1669 case EAP_FM_VOL:
1670 dip->type = AUDIO_MIXER_VALUE;
1671 dip->mixer_class = EAP_INPUT_CLASS;
1672 dip->prev = AUDIO_MIXER_LAST;
1673 dip->next = AUDIO_MIXER_LAST;
1674 strcpy(dip->label.name, AudioNfmsynth);
1675 dip->un.v.num_channels = 2;
1676 strcpy(dip->un.v.units.name, AudioNvolume);
1677 return (0);
1678 case EAP_CD_VOL:
1679 dip->type = AUDIO_MIXER_VALUE;
1680 dip->mixer_class = EAP_INPUT_CLASS;
1681 dip->prev = AUDIO_MIXER_LAST;
1682 dip->next = AUDIO_MIXER_LAST;
1683 strcpy(dip->label.name, AudioNcd);
1684 dip->un.v.num_channels = 2;
1685 strcpy(dip->un.v.units.name, AudioNvolume);
1686 return (0);
1687 case EAP_LINE_VOL:
1688 dip->type = AUDIO_MIXER_VALUE;
1689 dip->mixer_class = EAP_INPUT_CLASS;
1690 dip->prev = AUDIO_MIXER_LAST;
1691 dip->next = AUDIO_MIXER_LAST;
1692 strcpy(dip->label.name, AudioNline);
1693 dip->un.v.num_channels = 2;
1694 strcpy(dip->un.v.units.name, AudioNvolume);
1695 return (0);
1696 case EAP_AUX_VOL:
1697 dip->type = AUDIO_MIXER_VALUE;
1698 dip->mixer_class = EAP_INPUT_CLASS;
1699 dip->prev = AUDIO_MIXER_LAST;
1700 dip->next = AUDIO_MIXER_LAST;
1701 strcpy(dip->label.name, AudioNaux);
1702 dip->un.v.num_channels = 2;
1703 strcpy(dip->un.v.units.name, AudioNvolume);
1704 return (0);
1705 case EAP_MIC_VOL:
1706 dip->type = AUDIO_MIXER_VALUE;
1707 dip->mixer_class = EAP_INPUT_CLASS;
1708 dip->prev = AUDIO_MIXER_LAST;
1709 dip->next = EAP_MIC_PREAMP;
1710 strcpy(dip->label.name, AudioNmicrophone);
1711 dip->un.v.num_channels = 1;
1712 strcpy(dip->un.v.units.name, AudioNvolume);
1713 return (0);
1714 case EAP_RECORD_SOURCE:
1715 dip->mixer_class = EAP_RECORD_CLASS;
1716 dip->prev = dip->next = AUDIO_MIXER_LAST;
1717 strcpy(dip->label.name, AudioNsource);
1718 dip->type = AUDIO_MIXER_SET;
1719 dip->un.s.num_mem = 6;
1720 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1721 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1722 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1723 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1724 strcpy(dip->un.s.member[2].label.name, AudioNline);
1725 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1726 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1727 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1728 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1729 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1730 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1731 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1732 return (0);
1733 case EAP_OUTPUT_SELECT:
1734 dip->mixer_class = EAP_OUTPUT_CLASS;
1735 dip->prev = dip->next = AUDIO_MIXER_LAST;
1736 strcpy(dip->label.name, AudioNselect);
1737 dip->type = AUDIO_MIXER_SET;
1738 dip->un.s.num_mem = 6;
1739 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1740 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1741 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1742 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1743 strcpy(dip->un.s.member[2].label.name, AudioNline);
1744 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1745 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1746 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1747 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1748 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1749 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1750 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1751 return (0);
1752 case EAP_MIC_PREAMP:
1753 dip->type = AUDIO_MIXER_ENUM;
1754 dip->mixer_class = EAP_INPUT_CLASS;
1755 dip->prev = EAP_MIC_VOL;
1756 dip->next = AUDIO_MIXER_LAST;
1757 strcpy(dip->label.name, AudioNpreamp);
1758 dip->un.e.num_mem = 2;
1759 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1760 dip->un.e.member[0].ord = 0;
1761 strcpy(dip->un.e.member[1].label.name, AudioNon);
1762 dip->un.e.member[1].ord = 1;
1763 return (0);
1764 case EAP_OUTPUT_CLASS:
1765 dip->type = AUDIO_MIXER_CLASS;
1766 dip->mixer_class = EAP_OUTPUT_CLASS;
1767 dip->next = dip->prev = AUDIO_MIXER_LAST;
1768 strcpy(dip->label.name, AudioCoutputs);
1769 return (0);
1770 case EAP_RECORD_CLASS:
1771 dip->type = AUDIO_MIXER_CLASS;
1772 dip->mixer_class = EAP_RECORD_CLASS;
1773 dip->next = dip->prev = AUDIO_MIXER_LAST;
1774 strcpy(dip->label.name, AudioCrecord);
1775 return (0);
1776 case EAP_INPUT_CLASS:
1777 dip->type = AUDIO_MIXER_CLASS;
1778 dip->mixer_class = EAP_INPUT_CLASS;
1779 dip->next = dip->prev = AUDIO_MIXER_LAST;
1780 strcpy(dip->label.name, AudioCinputs);
1781 return (0);
1782 }
1783 return (ENXIO);
1784 }
1785
1786 void *
1787 eap_malloc(addr, direction, size, pool, flags)
1788 void *addr;
1789 int direction;
1790 size_t size;
1791 int pool, flags;
1792 {
1793 struct eap_softc *sc = addr;
1794 struct eap_dma *p;
1795 int error;
1796
1797 p = malloc(sizeof(*p), pool, flags);
1798 if (!p)
1799 return (0);
1800 error = eap_allocmem(sc, size, 16, p);
1801 if (error) {
1802 free(p, pool);
1803 return (0);
1804 }
1805 p->next = sc->sc_dmas;
1806 sc->sc_dmas = p;
1807 return (KERNADDR(p));
1808 }
1809
1810 void
1811 eap_free(addr, ptr, pool)
1812 void *addr;
1813 void *ptr;
1814 int pool;
1815 {
1816 struct eap_softc *sc = addr;
1817 struct eap_dma **pp, *p;
1818
1819 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1820 if (KERNADDR(p) == ptr) {
1821 eap_freemem(sc, p);
1822 *pp = p->next;
1823 free(p, pool);
1824 return;
1825 }
1826 }
1827 }
1828
1829 size_t
1830 eap_round_buffersize(addr, direction, size)
1831 void *addr;
1832 int direction;
1833 size_t size;
1834 {
1835 return (size);
1836 }
1837
1838 int
1839 eap_mappage(addr, mem, off, prot)
1840 void *addr;
1841 void *mem;
1842 int off;
1843 int prot;
1844 {
1845 struct eap_softc *sc = addr;
1846 struct eap_dma *p;
1847
1848 if (off < 0)
1849 return (-1);
1850 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1851 ;
1852 if (!p)
1853 return (-1);
1854 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1855 off, prot, BUS_DMA_WAITOK));
1856 }
1857
1858 int
1859 eap_get_props(addr)
1860 void *addr;
1861 {
1862 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1863 AUDIO_PROP_FULLDUPLEX);
1864 }
1865
1866 #if NMIDI > 0
1867 int
1868 eap_midi_open(addr, flags, iintr, ointr, arg)
1869 void *addr;
1870 int flags;
1871 void (*iintr)__P((void *, int));
1872 void (*ointr)__P((void *));
1873 void *arg;
1874 {
1875 struct eap_softc *sc = addr;
1876 u_int32_t uctrl;
1877
1878 sc->sc_iintr = iintr;
1879 sc->sc_ointr = ointr;
1880 sc->sc_arg = arg;
1881
1882 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1883 uctrl = 0;
1884 if (flags & FREAD)
1885 uctrl |= EAP_UC_RXINTEN;
1886 #if 0
1887 /* I don't understand ../midi.c well enough to use output interrupts */
1888 if (flags & FWRITE)
1889 uctrl |= EAP_UC_TXINTEN; */
1890 #endif
1891 EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1892
1893 return (0);
1894 }
1895
1896 void
1897 eap_midi_close(addr)
1898 void *addr;
1899 {
1900 struct eap_softc *sc = addr;
1901
1902 EWRITE1(sc, EAP_UART_CONTROL, 0);
1903 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1904
1905 sc->sc_iintr = 0;
1906 sc->sc_ointr = 0;
1907 }
1908
1909 int
1910 eap_midi_output(addr, d)
1911 void *addr;
1912 int d;
1913 {
1914 struct eap_softc *sc = addr;
1915 int x;
1916
1917 for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1918 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
1919 EWRITE1(sc, EAP_UART_DATA, d);
1920 return (0);
1921 }
1922 delay(MIDI_BUSY_DELAY);
1923 }
1924 return (EIO);
1925 }
1926
1927 void
1928 eap_midi_getinfo(addr, mi)
1929 void *addr;
1930 struct midi_info *mi;
1931 {
1932 mi->name = "AudioPCI MIDI UART";
1933 mi->props = MIDI_PROP_CAN_INPUT;
1934 }
1935
1936 #endif
1937