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