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