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