xref: /openbsd-src/sys/dev/pci/esa.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: esa.c,v 1.42 2023/09/11 08:41:26 mvs Exp $	*/
2 /* $NetBSD: esa.c,v 1.12 2002/03/24 14:17:35 jmcneill Exp $ */
3 
4 /*
5  * Copyright (c) 2001, 2002 Jared D. McNeill <jmcneill@invisible.ca>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Shamelessly stolen from NetBSD who based it on FreeBSD's who in turn
31  * based it on Linux's driver.  What a wonderful world.
32  *
33  *
34  * ESS Allegro-1 / Maestro3 Audio Driver
35  *
36  * Based on the FreeBSD maestro3 driver and the NetBSD eap driver.
37  * Original driver by Don Kim.
38  *
39  * The list management code could possibly be written better, but what
40  * we have right now does the job nicely. Thanks to Zach Brown <zab@zabbo.net>
41  * and Andrew MacDonald <amac@epsilon.yi.org> for helping me debug the
42  * problems with the original list management code present in the Linux
43  * driver.
44  */
45 
46 #include <sys/errno.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/conf.h>
52 #include <sys/exec.h>
53 #include <sys/audioio.h>
54 
55 #include <machine/bus.h>
56 #include <machine/intr.h>
57 
58 #include <dev/pci/pcidevs.h>
59 #include <dev/pci/pcivar.h>
60 
61 #include <dev/audio_if.h>
62 #include <dev/ic/ac97.h>
63 
64 #include <dev/pci/esareg.h>
65 #include <dev/pci/esavar.h>
66 #include <dev/microcode/esa/esadsp.h>
67 
68 #define PCI_CBIO	0x10
69 
70 #define ESA_DAC_DATA	0x1100
71 
72 enum {
73 	ESS_ALLEGRO1,
74 	ESS_MAESTRO3
75 };
76 
77 static struct esa_card_type {
78 	u_int16_t pci_vendor_id;
79 	u_int16_t pci_product_id;
80 	int type;
81 	int delay1, delay2;
82 } esa_card_types[] = {
83 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989,
84 	  ESS_ALLEGRO1, 50, 800 },
85 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3,
86 	  ESS_MAESTRO3, 20, 500 },
87 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2,
88 	  ESS_MAESTRO3, 20, 500 },
89 	{ 0, 0, 0, 0, 0 }
90 };
91 
92 int		esa_match(struct device *, void *, void *);
93 void		esa_attach(struct device *, struct device *, void *);
94 int		esa_detach(struct device *, int);
95 int		esa_activate(struct device *, int);
96 
97 /* audio(9) functions */
98 int		esa_open(void *, int);
99 void		esa_close(void *);
100 int		esa_set_params(void *, int, int, struct audio_params *,
101 			       struct audio_params *);
102 int		esa_round_blocksize(void *, int);
103 int		esa_commit_settings(void *);
104 int		esa_halt_output(void *);
105 int		esa_halt_input(void *);
106 int		esa_set_port(void *, mixer_ctrl_t *);
107 int		esa_get_port(void *, mixer_ctrl_t *);
108 int		esa_query_devinfo(void *, mixer_devinfo_t *);
109 void *		esa_malloc(void *, int, size_t, int, int);
110 void		esa_free(void *, void *, int);
111 size_t		esa_round_buffersize(void *, int, size_t);
112 int		esa_trigger_output(void *, void *, void *, int,
113 				   void (*)(void *), void *,
114 				   struct audio_params *);
115 int		esa_trigger_input(void *, void *, void *, int,
116 				  void (*)(void *), void *,
117 				  struct audio_params *);
118 
119 int		esa_intr(void *);
120 int		esa_allocmem(struct esa_softc *, size_t, size_t,
121 			     struct esa_dma *);
122 int		esa_freemem(struct esa_softc *, struct esa_dma *);
123 
124 /* Supporting subroutines */
125 u_int16_t	esa_read_assp(struct esa_softc *, u_int16_t, u_int16_t);
126 void		esa_write_assp(struct esa_softc *, u_int16_t, u_int16_t,
127 			       u_int16_t);
128 int		esa_init_codec(struct esa_softc *);
129 int		esa_attach_codec(void *, struct ac97_codec_if *);
130 int		esa_read_codec(void *, u_int8_t, u_int16_t *);
131 int		esa_write_codec(void *, u_int8_t, u_int16_t);
132 void		esa_reset_codec(void *);
133 enum ac97_host_flags	esa_flags_codec(void *);
134 int		esa_wait(struct esa_softc *);
135 int		esa_init(struct esa_softc *);
136 void		esa_config(struct esa_softc *);
137 u_int8_t	esa_assp_halt(struct esa_softc *);
138 void		esa_codec_reset(struct esa_softc *);
139 int		esa_amp_enable(struct esa_softc *);
140 void		esa_enable_interrupts(struct esa_softc *);
141 u_int32_t	esa_get_pointer(struct esa_softc *, struct esa_channel *);
142 
143 /* list management */
144 int		esa_add_list(struct esa_voice *, struct esa_list *, u_int16_t,
145 			     int);
146 void		esa_remove_list(struct esa_voice *, struct esa_list *, int);
147 
148 /* power management */
149 void		esa_suspend(struct esa_softc *);
150 void		esa_resume(struct esa_softc *);
151 
152 const struct audio_hw_if esa_hw_if = {
153 	.open = esa_open,
154 	.close = esa_close,
155 	.set_params = esa_set_params,
156 	.round_blocksize = esa_round_blocksize,
157 	.commit_settings = esa_commit_settings,
158 	.halt_output = esa_halt_output,
159 	.halt_input = esa_halt_input,
160 	.set_port = esa_set_port,
161 	.get_port = esa_get_port,
162 	.query_devinfo = esa_query_devinfo,
163 	.allocm = esa_malloc,
164 	.freem = esa_free,
165 	.round_buffersize = esa_round_buffersize,
166 	.trigger_output = esa_trigger_output,
167 	.trigger_input = esa_trigger_input,
168 };
169 
170 struct cfdriver esa_cd = {
171 	NULL, "esa", DV_DULL
172 };
173 
174 const struct cfattach esa_ca = {
175 	sizeof(struct esa_softc), esa_match, esa_attach,
176 	esa_detach, esa_activate
177 };
178 
179 /*
180  * audio(9) functions
181  */
182 
183 int
184 esa_open(void *hdl, int flags)
185 {
186 
187 	return (0);
188 }
189 
190 void
191 esa_close(void *hdl)
192 {
193 
194 	return;
195 }
196 
197 int
198 esa_set_params(void *hdl, int setmode, int usemode, struct audio_params *play,
199 	       struct audio_params *rec)
200 {
201 	struct esa_voice *vc = hdl;
202 	struct esa_channel *ch;
203 	struct audio_params *p;
204 	int mode;
205 
206 	for (mode = AUMODE_RECORD; mode != -1;
207 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
208 		if ((setmode & mode) == 0)
209 			continue;
210 
211 		switch (mode) {
212 		case AUMODE_PLAY:
213 			p = play;
214 			ch = &vc->play;
215 			break;
216 		case AUMODE_RECORD:
217 			p = rec;
218 			ch = &vc->rec;
219 			break;
220 		}
221 
222 		if (p->sample_rate < ESA_MINRATE)
223 			p->sample_rate = ESA_MINRATE;
224 		if (p->sample_rate > ESA_MAXRATE)
225 			p->sample_rate = ESA_MAXRATE;
226 		if (p->precision > 16)
227 			p->precision = 16;
228 		if (p->channels > 2)
229 			p->channels = 2;
230 
231 		switch(p->encoding) {
232 		case AUDIO_ENCODING_SLINEAR_LE:
233 			if (p->precision != 16)
234 				return EINVAL;
235 			break;
236 		case AUDIO_ENCODING_ULINEAR_LE:
237 		case AUDIO_ENCODING_ULINEAR_BE:
238 			if (p->precision != 8)
239 				return EINVAL;
240 			break;
241 		default:
242 			return (EINVAL);
243 		}
244 		p->bps = AUDIO_BPS(p->precision);
245 		p->msb = 1;
246 
247 		ch->mode = *p;
248 	}
249 
250 	return (0);
251 }
252 
253 int
254 esa_commit_settings(void *hdl)
255 {
256 	struct esa_voice *vc = hdl;
257 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
258 	struct audio_params *p = &vc->play.mode;
259 	struct audio_params *r = &vc->rec.mode;
260 	u_int32_t data;
261 	u_int32_t freq;
262 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
263 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
264 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
265 			   &~ 255;
266 
267 	/* playback */
268 	vc->play.data_offset = ESA_DAC_DATA + (data_bytes * vc->index);
269 	if (p->channels == 1)
270 		data = 1;
271 	else
272 		data = 0;
273 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
274 		       vc->play.data_offset + ESA_SRC3_MODE_OFFSET,
275 		       data);
276 	if (p->precision == 8)
277 		data = 1;
278 	else
279 		data = 0;
280 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
281 		       vc->play.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
282 		       data);
283 	if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) {
284 		freq--;
285 	}
286 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
287 		       vc->play.data_offset + ESA_CDATA_FREQUENCY, freq);
288 
289 	/* recording */
290 	vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * vc->index) +
291 			      (data_bytes / 2);
292 	if (r->channels == 1)
293 		data = 1;
294 	else
295 		data = 0;
296 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
297 		       vc->rec.data_offset + ESA_SRC3_MODE_OFFSET,
298 		       data);
299 	if (r->precision == 8)
300 		data = 1;
301 	else
302 		data = 0;
303 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
304 		       vc->rec.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
305 		       data);
306 	if ((freq = ((r->sample_rate << 15) + 24000) / 48000) != 0) {
307 		freq--;
308 	}
309 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
310 		       vc->rec.data_offset + ESA_CDATA_FREQUENCY, freq);
311 
312 	return (0);
313 };
314 
315 int
316 esa_round_blocksize(void *hdl, int bs)
317 {
318 	struct esa_voice *vc = hdl;
319 
320 	/*
321 	 * Surely there has to be a better solution...
322 	 */
323 	vc->play.blksize = vc->rec.blksize = 4096;
324 
325 	return (vc->play.blksize);
326 }
327 
328 int
329 esa_halt_output(void *hdl)
330 {
331 	struct esa_voice *vc = hdl;
332 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
333 	bus_space_tag_t iot = sc->sc_iot;
334 	bus_space_handle_t ioh = sc->sc_ioh;
335 	u_int16_t data;
336 
337 	if (vc->play.active == 0)
338 		return (0);
339 
340 	mtx_enter(&audio_lock);
341 	vc->play.active = 0;
342 
343 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
344 		       ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0);
345 
346 	sc->sc_ntimers--;
347 	if (sc->sc_ntimers == 0) {
348 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
349 			       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
350 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
351 			       ESA_KDATA_TIMER_COUNT_CURRENT, 0);
352 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
353 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
354 		    data & ~ESA_CLKRUN_GEN_ENABLE);
355 	}
356 
357 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
358 		       ESA_KDATA_MIXER_TASK_NUMBER,
359 		       sc->mixer_list.indexmap[vc->index]);
360 	/* remove ourselves from the packed lists */
361 	esa_remove_list(vc, &sc->mixer_list, vc->index);
362 	esa_remove_list(vc, &sc->dma_list, vc->index);
363 	esa_remove_list(vc, &sc->msrc_list, vc->index);
364 	mtx_leave(&audio_lock);
365 	return (0);
366 }
367 
368 int
369 esa_halt_input(void *hdl)
370 {
371 	struct esa_voice *vc = hdl;
372 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
373 	bus_space_tag_t iot = sc->sc_iot;
374 	bus_space_handle_t ioh = sc->sc_ioh;
375 	u_int32_t data;
376 
377 	if (vc->rec.active == 0)
378 		return (0);
379 
380 	mtx_enter(&audio_lock);
381 	vc->rec.active = 0;
382 
383 	sc->sc_ntimers--;
384 	if (sc->sc_ntimers == 0) {
385 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
386 			       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
387 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
388 			       ESA_KDATA_TIMER_COUNT_CURRENT, 0);
389 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
390 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
391 				  data & ~ESA_CLKRUN_GEN_ENABLE);
392 	}
393 
394 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset +
395 		       ESA_CDATA_INSTANCE_READY, 0);
396 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
397 		       0);
398 
399 	/* remove ourselves from the packed lists */
400 	esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES);
401 	esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES);
402 	esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES);
403 	mtx_leave(&audio_lock);
404 	return (0);
405 }
406 
407 void *
408 esa_malloc(void *hdl, int direction, size_t size, int type, int flags)
409 {
410 	struct esa_voice *vc = hdl;
411 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
412 	struct esa_dma *p;
413 	int error;
414 
415 	p = malloc(sizeof(*p), type, flags);
416 	if (!p)
417 		return (0);
418 	error = esa_allocmem(sc, size, 16, p);
419 	if (error) {
420 		free(p, type, 0);
421 		printf("%s: esa_malloc: not enough memory\n",
422 		    sc->sc_dev.dv_xname);
423 		return (0);
424 	}
425 	p->next = vc->dma;
426 	vc->dma = p;
427 
428 	return (KERNADDR(p));
429 }
430 
431 void
432 esa_free(void *hdl, void *addr, int type)
433 {
434 	struct esa_voice *vc = hdl;
435 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
436 	struct esa_dma *p;
437 	struct esa_dma **pp;
438 
439 	for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next)
440 		if (KERNADDR(p) == addr) {
441 			esa_freemem(sc, p);
442 			*pp = p->next;
443 			free(p, type, 0);
444 			return;
445 		}
446 }
447 
448 int
449 esa_set_port(void *hdl, mixer_ctrl_t *mc)
450 {
451 	struct esa_voice *vc = hdl;
452 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
453 
454 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc));
455 }
456 
457 int
458 esa_get_port(void *hdl, mixer_ctrl_t *mc)
459 {
460 	struct esa_voice *vc = hdl;
461 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
462 
463 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc));
464 }
465 
466 int
467 esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
468 {
469 	struct esa_voice *vc = hdl;
470 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
471 
472 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di));
473 }
474 
475 size_t
476 esa_round_buffersize(void *hdl, int direction, size_t bufsize)
477 {
478 	struct esa_voice *vc = hdl;
479 
480 	/*
481 	 * We must be able to do better than this...
482 	 */
483 	vc->play.bufsize = vc->rec.bufsize = 65536;
484 
485 	return (vc->play.bufsize);
486 }
487 
488 int
489 esa_trigger_output(void *hdl, void *start, void *end, int blksize,
490 			void (*intr)(void *), void *intrarg,
491 			struct audio_params *param)
492 {
493 	struct esa_voice *vc = hdl;
494 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
495 	struct esa_dma *p;
496 	bus_space_tag_t iot = sc->sc_iot;
497 	bus_space_handle_t ioh = sc->sc_ioh;
498 	u_int32_t data;
499 	u_int32_t bufaddr;
500 	u_int32_t i;
501 	size_t size;
502 
503 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
504 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
505 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
506 			   &~ 255;
507 	int dac_data = ESA_DAC_DATA + (data_bytes * vc->index);
508 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
509 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
510 	int dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
511 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
512 
513 	if (vc->play.active)
514 		return (EINVAL);
515 
516 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
517 		;
518 	if (!p) {
519 		printf("%s: esa_trigger_output: bad addr %p\n",
520 		    sc->sc_dev.dv_xname, start);
521 		return (EINVAL);
522 	}
523 
524 	vc->play.active = 1;
525 	vc->play.intr = intr;
526 	vc->play.arg = intrarg;
527 	vc->play.pos = 0;
528 	vc->play.count = 0;
529 	vc->play.buf = start;
530 	size = (size_t)(((caddr_t)end - (caddr_t)start));
531 	bufaddr = DMAADDR(p);
532 	vc->play.start = bufaddr;
533 
534 #define LO(x) ((x) & 0x0000ffff)
535 #define HI(x) ((x) >> 16)
536 
537 	mtx_enter(&audio_lock);
538 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
539 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
540 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
541 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
542 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
543 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
544 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
545 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
546 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
547 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
548 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
549 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
550 
551 	/* DSP buffers */
552 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
553 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
554 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
555 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
556 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
557 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
558 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
559 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
560 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
561 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
562 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
563 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
564 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
565 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
566 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
567 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
568 
569 	/* Some per-client initializers */
570 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
571 	    ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8);
572 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
573 	    ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
574 	/* Enable or disable low-pass filter? (0xff if rate > 45000) */
575 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
576 	    ESA_SRC3_DIRECTION_OFFSET + 22,
577 	    vc->play.mode.sample_rate > 45000 ? 0xff : 0);
578 	/* Tell it which way DMA is going */
579 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
580 	    ESA_CDATA_DMA_CONTROL,
581 	    ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
582 	    ESA_DMAC_BLOCKF_SELECTOR);
583 
584 	/* Set an armload of static initializers */
585 	for (i = 0; i < nitems(esa_playvals); i++)
586 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
587 		    esa_playvals[i].addr, esa_playvals[i].val);
588 
589 	/* Put us in the packed task lists */
590 	esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT,
591 		     vc->index);
592 	esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT,
593 		     vc->index);
594 	esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT,
595 		     vc->index);
596 #undef LO
597 #undef HI
598 
599 	sc->sc_ntimers++;
600 
601 	if (sc->sc_ntimers == 1) {
602 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
603 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
604 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
605 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
606 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
607 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
608 		    data | ESA_CLKRUN_GEN_ENABLE);
609 	}
610 
611 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
612 	    ESA_CDATA_INSTANCE_READY, 1);
613 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
614 	    ESA_KDATA_MIXER_TASK_NUMBER,
615 	    sc->mixer_list.indexmap[vc->index]);
616 	mtx_leave(&audio_lock);
617 	return (0);
618 }
619 
620 int
621 esa_trigger_input(void *hdl, void *start, void *end, int blksize,
622 			void (*intr)(void *), void *intrarg,
623 			struct audio_params *param)
624 {
625 	struct esa_voice *vc = hdl;
626 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
627 	struct esa_dma *p;
628 	bus_space_tag_t iot = sc->sc_iot;
629 	bus_space_handle_t ioh = sc->sc_ioh;
630 	u_int32_t data;
631 	u_int32_t bufaddr;
632 	u_int32_t i;
633 	size_t size;
634 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
635 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
636 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
637 			   &~ 255;
638 	int adc_data = ESA_DAC_DATA + (data_bytes * vc->index) +
639 		       (data_bytes / 2);
640 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
641 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
642 	int dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
643 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
644 	vc->rec.data_offset = adc_data;
645 
646 	/* We only support 1 recording channel */
647 	if (vc->index > 0)
648 		return (ENODEV);
649 
650 	if (vc->rec.active)
651 		return (EINVAL);
652 
653 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
654 		;
655 	if (!p) {
656 		printf("%s: esa_trigger_input: bad addr %p\n",
657 		    sc->sc_dev.dv_xname, start);
658 		return (EINVAL);
659 	}
660 
661 	vc->rec.active = 1;
662 	vc->rec.intr = intr;
663 	vc->rec.arg = intrarg;
664 	vc->rec.pos = 0;
665 	vc->rec.count = 0;
666 	vc->rec.buf = start;
667 	size = (size_t)(((caddr_t)end - (caddr_t)start));
668 	bufaddr = DMAADDR(p);
669 	vc->rec.start = bufaddr;
670 
671 #define LO(x) ((x) & 0x0000ffff)
672 #define HI(x) ((x) >> 16)
673 	mtx_enter(&audio_lock);
674 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
675 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
676 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
677 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
678 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
679 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
680 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
681 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
682 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
683 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
684 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
685 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
686 
687 	/* DSP buffers */
688 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
689 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
690 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
691 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
692 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
693 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
694 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
695 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
696 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
697 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
698 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
699 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
700 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
701 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
702 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
703 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
704 
705 	/* Some per-client initializers */
706 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
707 	    ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8);
708 	/* Tell it which way DMA is going */
709 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
710 	    ESA_CDATA_DMA_CONTROL,
711 	    ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT +
712 	    ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR);
713 
714 	/* Set an armload of static initializers */
715 	for (i = 0; i < nitems(esa_recvals); i++)
716 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
717 		    esa_recvals[i].addr, esa_recvals[i].val);
718 
719 	/* Put us in the packed task lists */
720 	esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT,
721 		     vc->index + ESA_NUM_VOICES);
722 	esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT,
723 		     vc->index + ESA_NUM_VOICES);
724 	esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT,
725 		     vc->index + ESA_NUM_VOICES);
726 #undef LO
727 #undef HI
728 
729 	sc->sc_ntimers++;
730 	if (sc->sc_ntimers == 1) {
731 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
732 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
733 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
734 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
735 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
736 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
737 		    data | ESA_CLKRUN_GEN_ENABLE);
738 	}
739 
740 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
741 	    ESA_CDATA_INSTANCE_READY, 1);
742 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
743 	    1);
744 	mtx_leave(&audio_lock);
745 	return (0);
746 }
747 
748 /* Interrupt handler */
749 
750 int
751 esa_intr(void *hdl)
752 {
753 	struct esa_softc *sc = hdl;
754 	struct esa_voice *vc;
755 	bus_space_tag_t iot = sc->sc_iot;
756 	bus_space_handle_t ioh = sc->sc_ioh;
757 	u_int8_t status, ctl;
758 	u_int32_t pos;
759 	u_int32_t diff;
760 	u_int32_t play_blksize, play_bufsize;
761 	u_int32_t rec_blksize, rec_bufsize;
762 	int i, claimed = 0;
763 
764 	mtx_enter(&audio_lock);
765 	status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
766 	if (status == 0xff) {
767 		mtx_leave(&audio_lock);
768 		return (0);
769 	}
770 
771 	/* ack the interrupt */
772 	bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status);
773 
774 	if (status & ESA_HV_INT_PENDING) {
775 		u_int8_t event;
776 
777 		printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname);
778 		event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
779 		switch(event) {
780 		case 0x99:
781 		case 0xaa:
782 		case 0x66:
783 		case 0x88:
784 			printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname);
785 			break;
786 		default:
787 			printf("%s: unknown hwvol event 0x%02x\n",
788 			    sc->sc_dev.dv_xname, event);
789 			break;
790 		}
791 		bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
792 		claimed = 1;
793 	}
794 
795 	if (status & ESA_ASSP_INT_PENDING) {
796 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
797 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
798 			ctl = bus_space_read_1(iot, ioh,
799 					       ESA_ASSP_HOST_INT_STATUS);
800 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
801 				bus_space_write_1(iot, ioh,
802 				    ESA_ASSP_HOST_INT_STATUS,
803 				    ESA_DSP2HOST_REQ_TIMER);
804 				for (i = 0; i < ESA_NUM_VOICES; i++) {
805 					vc = &sc->voice[i];
806 					if (vc->play.active) {
807 						play_blksize = vc->play.blksize;
808 						play_bufsize = vc->play.bufsize;
809 						pos = esa_get_pointer(sc, &vc->play)
810 						    % play_bufsize;
811 						diff = (play_bufsize + pos - vc->play.pos)
812 						    % play_bufsize;
813 						vc->play.pos = pos;
814 						vc->play.count += diff;
815 						while(vc->play.count >= play_blksize) {
816 							vc->play.count -= play_blksize;
817 							(*vc->play.intr)(vc->play.arg);
818 						}
819 					}
820 					if (vc->rec.active) {
821 						rec_blksize = vc->rec.blksize;
822 						rec_bufsize = vc->rec.bufsize;
823 						pos = esa_get_pointer(sc, &vc->rec)
824 						    % rec_bufsize;
825 						diff = (rec_bufsize + pos - vc->rec.pos)
826 						    % rec_bufsize;
827 						vc->rec.pos = pos;
828 						vc->rec.count += diff;
829 						while(vc->rec.count >= rec_blksize) {
830 							vc->rec.count -= rec_blksize;
831 							(*vc->rec.intr)(vc->rec.arg);
832 						}
833 					}
834 				}
835 			}
836 		}
837 		claimed = 1;
838 	}
839 	mtx_leave(&audio_lock);
840 	return (claimed);
841 }
842 
843 int
844 esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
845 		struct esa_dma *p)
846 {
847 	int error;
848 
849 	p->size = size;
850 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
851 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
852 				 &p->nsegs, BUS_DMA_NOWAIT);
853 	if (error)
854 		return (error);
855 
856 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
857 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
858 	if (error)
859 		goto free;
860 
861 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
862 				  BUS_DMA_NOWAIT, &p->map);
863 	if (error)
864 		goto unmap;
865 
866 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
867 				BUS_DMA_NOWAIT);
868 	if (error)
869 		goto destroy;
870 
871 	return (0);
872 
873 destroy:
874 	bus_dmamap_destroy(sc->sc_dmat, p->map);
875 unmap:
876 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
877 free:
878 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
879 
880 	return (error);
881 }
882 
883 int
884 esa_freemem(struct esa_softc *sc, struct esa_dma *p)
885 {
886 
887 	bus_dmamap_unload(sc->sc_dmat, p->map);
888 	bus_dmamap_destroy(sc->sc_dmat, p->map);
889 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
890 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
891 
892 	return (0);
893 }
894 
895 /*
896  * Supporting Subroutines
897  */
898 const struct pci_matchid esa_devices[] = {
899 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989 },
900 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3 },
901 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2 },
902 };
903 
904 int
905 esa_match(struct device *dev, void *match, void *aux)
906 {
907 	return (pci_matchbyid((struct pci_attach_args *)aux, esa_devices,
908 	    nitems(esa_devices)));
909 }
910 
911 void
912 esa_attach(struct device *parent, struct device *self, void *aux)
913 {
914 	struct esa_softc *sc = (struct esa_softc *)self;
915 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
916 	pcitag_t tag = pa->pa_tag;
917 	pci_chipset_tag_t pc = pa->pa_pc;
918 	pci_intr_handle_t ih;
919 	struct esa_card_type *card;
920 	const char *intrstr;
921 	int i, len;
922 
923 	for (card = esa_card_types; card->pci_vendor_id; card++)
924 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
925 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
926 			sc->type = card->type;
927 			sc->delay1 = card->delay1;
928 			sc->delay2 = card->delay2;
929 			break;
930 		}
931 
932 	/* Map I/O register */
933 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
934 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) {
935 		printf(": can't map i/o space\n");
936 		return;
937 	}
938 
939 	/* Initialize softc */
940 	sc->sc_tag = tag;
941 	sc->sc_pct = pc;
942 	sc->sc_dmat = pa->pa_dmat;
943 
944 	/* Map and establish an interrupt */
945 	if (pci_intr_map(pa, &ih)) {
946 		printf(": can't map interrupt\n");
947 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
948 		return;
949 	}
950 	intrstr = pci_intr_string(pc, ih);
951 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE,
952 	    esa_intr, self, sc->sc_dev.dv_xname);
953 	if (sc->sc_ih == NULL) {
954 		printf(": can't establish interrupt");
955 		if (intrstr != NULL)
956 			printf(" at %s", intrstr);
957 		printf("\n");
958 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
959 		return;
960 	}
961 	printf(": %s\n", intrstr);
962 
963 	/* Power up chip */
964 	pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0);
965 
966 	/* Init chip */
967 	if (esa_init(sc) == -1) {
968 		printf("%s: esa_attach: unable to initialize the card\n",
969 		    sc->sc_dev.dv_xname);
970 		pci_intr_disestablish(pc, sc->sc_ih);
971 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
972 		return;
973 	}
974 
975 	/* create suspend save area */
976 	len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
977 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
978 	sc->savemem = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
979 	if (sc->savemem == NULL) {
980 		printf("%s: unable to allocate suspend buffer\n",
981 		    sc->sc_dev.dv_xname);
982 		pci_intr_disestablish(pc, sc->sc_ih);
983 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
984 		return;
985 	}
986 
987 	/*
988 	 * Every card I've seen has had their channels swapped with respect
989 	 * to the mixer. Ie:
990 	 *  $ mixerctl -w outputs.master=0,191
991 	 * Would result in the _right_ speaker being turned off.
992 	 *
993 	 * So, we will swap the left and right mixer channels to compensate
994 	 * for this.
995 	 */
996 	sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS;
997 	sc->codec_flags |= AC97_HOST_DONT_READ;
998 
999 	/* Attach AC97 host interface */
1000 	sc->host_if.arg = self;
1001 	sc->host_if.attach = esa_attach_codec;
1002 	sc->host_if.read = esa_read_codec;
1003 	sc->host_if.write = esa_write_codec;
1004 	sc->host_if.reset = esa_reset_codec;
1005 	sc->host_if.flags = esa_flags_codec;
1006 
1007 	if (ac97_attach(&sc->host_if) != 0) {
1008 		pci_intr_disestablish(pc, sc->sc_ih);
1009 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1010 		free(sc->savemem, M_DEVBUF, 0);
1011 		return;
1012 	}
1013 
1014 	/* initialize list management structures */
1015 	sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0;
1016 	sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS;
1017 	sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0;
1018 	sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS;
1019 	sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0;
1020 	sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS;
1021 	sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC;
1022 	sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC;
1023 
1024 	/* initialize index maps */
1025 	for (i = 0; i < ESA_NUM_VOICES * 2; i++) {
1026 		sc->mixer_list.indexmap[i] = -1;
1027 		sc->msrc_list.indexmap[i] = -1;
1028 		sc->dma_list.indexmap[i] = -1;
1029 		sc->adc1_list.indexmap[i] = -1;
1030 	}
1031 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1032 		sc->voice[i].parent = (struct device *)sc;
1033 		sc->voice[i].index = i;
1034 		sc->sc_audiodev[i] =
1035 		    audio_attach_mi(&esa_hw_if, &sc->voice[i], NULL, &sc->sc_dev);
1036 	}
1037 }
1038 
1039 int
1040 esa_detach(struct device *self, int flags)
1041 {
1042 	struct esa_softc *sc = (struct esa_softc *)self;
1043 	int i;
1044 
1045 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1046 		if (sc->sc_audiodev[i] != NULL)
1047 			config_detach(sc->sc_audiodev[i], flags);
1048 	}
1049 
1050 	if (sc->sc_ih != NULL)
1051 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1052 	if (sc->sc_ios)
1053 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1054 
1055 	free(sc->savemem, M_DEVBUF, 0);
1056 
1057 	return (0);
1058 }
1059 
1060 u_int16_t
1061 esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
1062 {
1063 	u_int16_t data;
1064 	bus_space_tag_t iot = sc->sc_iot;
1065 	bus_space_handle_t ioh = sc->sc_ioh;
1066 
1067 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1068 	    region & ESA_MEMTYPE_MASK);
1069 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1070 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
1071 
1072 	return (data);
1073 }
1074 
1075 void
1076 esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
1077 		u_int16_t data)
1078 {
1079 	bus_space_tag_t iot = sc->sc_iot;
1080 	bus_space_handle_t ioh = sc->sc_ioh;
1081 
1082 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1083 	    region & ESA_MEMTYPE_MASK);
1084 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1085 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
1086 
1087 	return;
1088 }
1089 
1090 int
1091 esa_init_codec(struct esa_softc *sc)
1092 {
1093 	bus_space_tag_t iot = sc->sc_iot;
1094 	bus_space_handle_t ioh = sc->sc_ioh;
1095 	u_int32_t data;
1096 
1097 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
1098 
1099 	return ((data & 0x1) ? 0 : 1);
1100 }
1101 
1102 int
1103 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1104 {
1105 	struct esa_softc *sc = aux;
1106 
1107 	sc->codec_if = codec_if;
1108 
1109 	return (0);
1110 }
1111 
1112 int
1113 esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
1114 {
1115 	struct esa_softc *sc = aux;
1116 	bus_space_tag_t iot = sc->sc_iot;
1117 	bus_space_handle_t ioh = sc->sc_ioh;
1118 
1119 	if (esa_wait(sc))
1120 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1121 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
1122 	delay(50);
1123 	if (esa_wait(sc))
1124 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1125 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
1126 
1127 	return (0);
1128 }
1129 
1130 int
1131 esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
1132 {
1133 	struct esa_softc *sc = aux;
1134 	bus_space_tag_t iot = sc->sc_iot;
1135 	bus_space_handle_t ioh = sc->sc_ioh;
1136 
1137 	if (esa_wait(sc)) {
1138 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
1139 		return (-1);
1140 	}
1141 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
1142 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
1143 	delay(50);
1144 
1145 	return (0);
1146 }
1147 
1148 void
1149 esa_reset_codec(void *aux)
1150 {
1151 
1152 	return;
1153 }
1154 
1155 enum ac97_host_flags
1156 esa_flags_codec(void *aux)
1157 {
1158 	struct esa_softc *sc = aux;
1159 
1160 	return (sc->codec_flags);
1161 }
1162 
1163 int
1164 esa_wait(struct esa_softc *sc)
1165 {
1166 	int i, val;
1167 	bus_space_tag_t iot = sc->sc_iot;
1168 	bus_space_handle_t ioh = sc->sc_ioh;
1169 
1170 	for (i = 0; i < 20; i++) {
1171 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
1172 		if ((val & 1) == 0)
1173 			return (0);
1174 		delay(2);
1175 	}
1176 
1177 	return (-1);
1178 }
1179 
1180 int
1181 esa_init(struct esa_softc *sc)
1182 {
1183 	struct esa_voice *vc;
1184 	bus_space_tag_t iot = sc->sc_iot;
1185 	bus_space_handle_t ioh = sc->sc_ioh;
1186 	pcitag_t tag = sc->sc_tag;
1187 	pci_chipset_tag_t pc = sc->sc_pct;
1188 	u_int32_t data, i, size;
1189 	u_int8_t reset_state;
1190 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
1191 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
1192 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
1193 			   &~ 255;
1194 
1195 	/* Disable legacy emulation */
1196 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
1197 	data |= DISABLE_LEGACY;
1198 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
1199 
1200 	esa_config(sc);
1201 
1202 	reset_state = esa_assp_halt(sc);
1203 
1204 	esa_init_codec(sc);
1205 	esa_codec_reset(sc);
1206 
1207 	/* Zero kernel and mixer data */
1208 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
1209 	for (i = 0; i < size / 2; i++) {
1210 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1211 		    ESA_KDATA_BASE_ADDR + i, 0);
1212 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1213 		    ESA_KDATA_BASE_ADDR2 + i, 0);
1214 	}
1215 
1216 	/* Init DMA pointer */
1217 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
1218 	    ESA_KDATA_DMA_XFER0);
1219 
1220 	/* Write kernel code into memory */
1221 	size = nitems(esa_assp_kernel_image);
1222 	for (i = 0; i < size; i++)
1223 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1224 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
1225 
1226 	size = nitems(esa_assp_minisrc_image);
1227 	for (i = 0; i < size; i++)
1228 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
1229 		    esa_assp_minisrc_image[i]);
1230 
1231 	/* Write the coefficients for the low pass filter */
1232 	size = nitems(esa_minisrc_lpf_image);
1233 	for (i = 0; i < size; i++)
1234 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1235 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
1236 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1237 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
1238 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
1239 	/* Init the mixer number */
1240 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1241              ESA_KDATA_MIXER_TASK_NUMBER, 0);
1242 	/* Extreme kernel master volume */
1243 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1244 	    ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME);
1245 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1246             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
1247 
1248 	if (esa_amp_enable(sc))
1249 		return (-1);
1250 
1251 	/* Zero entire DAC/ADC area */
1252 	for (i = 0x1100; i < 0x1c00; i++)
1253 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
1254 
1255 	/* set some sane defaults */
1256 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1257 		vc = &sc->voice[i];
1258 		vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i);
1259 		vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2);
1260 	}
1261 
1262 	esa_enable_interrupts(sc);
1263 
1264 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1265 	    reset_state | ESA_REGB_ENABLE_RESET);
1266 
1267 	return (0);
1268 }
1269 
1270 void
1271 esa_config(struct esa_softc *sc)
1272 {
1273 	bus_space_tag_t iot = sc->sc_iot;
1274 	bus_space_handle_t ioh = sc->sc_ioh;
1275 	pcitag_t tag = sc->sc_tag;
1276 	pci_chipset_tag_t pc = sc->sc_pct;
1277 	u_int32_t data;
1278 
1279 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1280 	data &= ESA_REDUCED_DEBOUNCE;
1281 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
1282 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1283 
1284 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
1285 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1286 	data &= ~ESA_INT_CLK_SELECT;
1287 	if (sc->type == ESS_MAESTRO3) {
1288 		data &= ~ESA_INT_CLK_MULT_ENABLE;
1289 		data |= ESA_INT_CLK_SRC_NOT_PCI;
1290 	}
1291 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
1292 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1293 
1294 	if (sc->type == ESS_ALLEGRO1) {
1295 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
1296 		data |= ESA_IN_CLK_12MHZ_SELECT;
1297 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
1298 	}
1299 
1300 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
1301 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
1302 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
1303 	data |= ESA_ASSP_0_WS_ENABLE;
1304 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
1305 
1306 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
1307 
1308 	return;
1309 }
1310 
1311 u_int8_t
1312 esa_assp_halt(struct esa_softc *sc)
1313 {
1314 	bus_space_tag_t iot = sc->sc_iot;
1315 	bus_space_handle_t ioh = sc->sc_ioh;
1316 	u_int8_t data, reset_state;
1317 
1318 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
1319 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
1320 	delay(10000);		/* XXX use tsleep */
1321 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1322 			reset_state & ~ESA_REGB_ENABLE_RESET);
1323 	delay(10000);		/* XXX use tsleep */
1324 
1325 	return (reset_state);
1326 }
1327 
1328 void
1329 esa_codec_reset(struct esa_softc *sc)
1330 {
1331 	bus_space_tag_t iot = sc->sc_iot;
1332 	bus_space_handle_t ioh = sc->sc_ioh;
1333 	u_int16_t data, dir;
1334 	int retry = 0;
1335 
1336 	do {
1337 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1338 		dir = data | 0x10; /* assuming pci bus master? */
1339 
1340 		/* remote codec config */
1341 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
1342 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
1343 		    data & ~ESA_SECOND_CODEC_ID_MASK);
1344 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
1345 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
1346 		    data & ~ESA_COMMAND_ADDR_OUT);
1347 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
1348 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
1349 		    data & ~ESA_STATUS_ADDR_IN);
1350 
1351 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1352 				  ESA_IO_SRAM_ENABLE);
1353 		delay(20);
1354 
1355 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1356 		    dir & ~ESA_GPO_PRIMARY_AC97);
1357 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
1358 				  ~ESA_GPO_PRIMARY_AC97);
1359 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
1360 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1361 		    dir | ESA_GPO_PRIMARY_AC97);
1362 		delay(sc->delay1 * 1000);
1363 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
1364 				  ESA_GPO_PRIMARY_AC97);
1365 		delay(5);
1366 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1367 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
1368 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1369 		delay(sc->delay2 * 1000);
1370 
1371 		esa_read_codec(sc, 0x7c, &data);
1372 		if ((data == 0) || (data == 0xffff)) {
1373 			retry++;
1374 			if (retry > 3) {
1375 				printf("%s: esa_codec_reset: failed\n",
1376 				    sc->sc_dev.dv_xname);
1377 				break;
1378 			}
1379 			printf("%s: esa_codec_reset: retrying\n",
1380 			    sc->sc_dev.dv_xname);
1381 		} else
1382 			retry = 0;
1383 	} while (retry);
1384 
1385 	return;
1386 }
1387 
1388 int
1389 esa_amp_enable(struct esa_softc *sc)
1390 {
1391 	bus_space_tag_t iot = sc->sc_iot;
1392 	bus_space_handle_t ioh = sc->sc_ioh;
1393 	u_int32_t gpo, polarity_port, polarity;
1394 	u_int16_t data;
1395 
1396 	switch (sc->type) {
1397 	case ESS_ALLEGRO1:
1398 		polarity_port = 0x1800;
1399 		break;
1400 	case ESS_MAESTRO3:
1401 		polarity_port = 0x1100;
1402 		break;
1403 	default:
1404 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
1405 		    sc->sc_dev.dv_xname);
1406 		return (1);
1407 	}
1408 
1409 	gpo = (polarity_port >> 8) & 0x0f;
1410 	polarity = polarity_port >> 12;
1411 	polarity = !polarity;	/* Enable */
1412 	polarity = polarity << gpo;
1413 	gpo = 1 << gpo;
1414 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
1415 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1416 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
1417 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
1418 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
1419 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1420 
1421 	return (0);
1422 }
1423 
1424 void
1425 esa_enable_interrupts(struct esa_softc *sc)
1426 {
1427 	bus_space_tag_t iot = sc->sc_iot;
1428 	bus_space_handle_t ioh = sc->sc_ioh;
1429 	u_int8_t data;
1430 
1431 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
1432 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
1433 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
1434 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
1435 	    data | ESA_ASSP_HOST_INT_ENABLE);
1436 }
1437 
1438 /*
1439  * List management
1440  */
1441 int
1442 esa_add_list(struct esa_voice *vc, struct esa_list *el,
1443 	     u_int16_t val, int index)
1444 {
1445 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1446 
1447 	el->indexmap[index] = el->currlen;
1448 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1449 		       el->mem_addr + el->currlen,
1450 		       val);
1451 
1452 	return (el->currlen++);
1453 }
1454 
1455 void
1456 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index)
1457 {
1458 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1459 	u_int16_t val;
1460 	int lastindex = el->currlen - 1;
1461 	int vindex = el->indexmap[index];
1462 	int i;
1463 
1464 	/* reset our virtual index */
1465 	el->indexmap[index] = -1;
1466 
1467 	if (vindex != lastindex) {
1468 		val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1469 				    el->mem_addr + lastindex);
1470 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1471 			       el->mem_addr + vindex,
1472 			       val);
1473 		for (i = 0; i < ESA_NUM_VOICES * 2; i++)
1474 			if (el->indexmap[i] == lastindex)
1475 				break;
1476 		if (i >= ESA_NUM_VOICES * 2)
1477 			printf("%s: esa_remove_list: invalid task index\n",
1478 			       sc->sc_dev.dv_xname);
1479 		else
1480 			el->indexmap[i] = vindex;
1481 	}
1482 
1483 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1484 		       el->mem_addr + lastindex, 0);
1485 	el->currlen--;
1486 
1487 	return;
1488 }
1489 
1490 int
1491 esa_activate(struct device *self, int act)
1492 {
1493 	struct esa_softc *sc = (struct esa_softc *)self;
1494 
1495 	switch (act) {
1496 	case DVACT_SUSPEND:
1497 		esa_suspend(sc);
1498 		break;
1499 	case DVACT_RESUME:
1500 		esa_resume(sc);
1501 		break;
1502 	}
1503 	return 0;
1504 }
1505 
1506 void
1507 esa_suspend(struct esa_softc *sc)
1508 {
1509 	bus_space_tag_t iot = sc->sc_iot;
1510 	bus_space_handle_t ioh = sc->sc_ioh;
1511 	int i, index;
1512 
1513 	index = 0;
1514 
1515 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
1516 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
1517 
1518 	esa_assp_halt(sc);
1519 
1520 	/* Save ASSP state */
1521 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1522 	    i++)
1523 		sc->savemem[index++] = esa_read_assp(sc,
1524 		    ESA_MEMTYPE_INTERNAL_CODE, i);
1525 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1526 	    i++)
1527 		sc->savemem[index++] = esa_read_assp(sc,
1528 		    ESA_MEMTYPE_INTERNAL_DATA, i);
1529 }
1530 
1531 void
1532 esa_resume(struct esa_softc *sc)
1533 {
1534 	bus_space_tag_t iot = sc->sc_iot;
1535 	bus_space_handle_t ioh = sc->sc_ioh;
1536 	int i, index;
1537 	u_int8_t reset_state;
1538 
1539 	index = 0;
1540 
1541 	esa_config(sc);
1542 
1543 	reset_state = esa_assp_halt(sc);
1544 
1545 	esa_codec_reset(sc);
1546 
1547 	/* restore ASSP */
1548 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1549 	    i++)
1550 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
1551 		    sc->savemem[index++]);
1552 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1553 	    i++)
1554 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
1555 		    sc->savemem[index++]);
1556 
1557 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
1558 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1559 	    reset_state | ESA_REGB_ENABLE_RESET);
1560 
1561 	esa_enable_interrupts(sc);
1562 	esa_amp_enable(sc);
1563 }
1564 
1565 u_int32_t
1566 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
1567 {
1568 	u_int16_t hi = 0, lo = 0;
1569 	u_int32_t addr;
1570 	int data_offset = ch->data_offset;
1571 
1572 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1573 	    ESA_CDATA_HOST_SRC_CURRENTH);
1574 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1575 	    ESA_CDATA_HOST_SRC_CURRENTL);
1576 
1577 	addr = lo | ((u_int32_t)hi << 16);
1578 	return (addr - ch->start);
1579 }
1580