xref: /openbsd-src/sys/dev/pci/esa.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: esa.c,v 1.23 2011/04/03 15:36:02 jasper 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;
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 	}
905 
906 	if (status & ESA_ASSP_INT_PENDING) {
907 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
908 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
909 			ctl = bus_space_read_1(iot, ioh,
910 					       ESA_ASSP_HOST_INT_STATUS);
911 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
912 				bus_space_write_1(iot, ioh,
913 				    ESA_ASSP_HOST_INT_STATUS,
914 				    ESA_DSP2HOST_REQ_TIMER);
915 				for (i = 0; i < ESA_NUM_VOICES; i++) {
916 					vc = &sc->voice[i];
917 					if (vc->play.active) {
918 						play_blksize = vc->play.blksize;
919 						play_bufsize = vc->play.bufsize;
920 						pos = esa_get_pointer(sc, &vc->play)
921 						    % play_bufsize;
922 						diff = (play_bufsize + pos - vc->play.pos)
923 						    % play_bufsize;
924 						vc->play.pos = pos;
925 						vc->play.count += diff;
926 						while(vc->play.count >= play_blksize) {
927 							vc->play.count -= play_blksize;
928 							(*vc->play.intr)(vc->play.arg);
929 						}
930 					}
931 					if (vc->rec.active) {
932 						rec_blksize = vc->rec.blksize;
933 						rec_bufsize = vc->rec.bufsize;
934 						pos = esa_get_pointer(sc, &vc->rec)
935 						    % rec_bufsize;
936 						diff = (rec_bufsize + pos - vc->rec.pos)
937 						    % rec_bufsize;
938 						vc->rec.pos = pos;
939 						vc->rec.count += diff;
940 						while(vc->rec.count >= rec_blksize) {
941 							vc->rec.count -= rec_blksize;
942 							(*vc->rec.intr)(vc->rec.arg);
943 						}
944 					}
945 				}
946 			}
947 		}
948 	}
949 
950 	return (1);
951 }
952 
953 int
954 esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
955 		struct esa_dma *p)
956 {
957 	int error;
958 
959 	p->size = size;
960 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
961 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
962 				 &p->nsegs, BUS_DMA_NOWAIT);
963 	if (error)
964 		return (error);
965 
966 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
967 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
968 	if (error)
969 		goto free;
970 
971 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
972 				  BUS_DMA_NOWAIT, &p->map);
973 	if (error)
974 		goto unmap;
975 
976 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
977 				BUS_DMA_NOWAIT);
978 	if (error)
979 		goto destroy;
980 
981 	return (0);
982 
983 destroy:
984 	bus_dmamap_destroy(sc->sc_dmat, p->map);
985 unmap:
986 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
987 free:
988 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
989 
990 	return (error);
991 }
992 
993 int
994 esa_freemem(struct esa_softc *sc, struct esa_dma *p)
995 {
996 
997 	bus_dmamap_unload(sc->sc_dmat, p->map);
998 	bus_dmamap_destroy(sc->sc_dmat, p->map);
999 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
1000 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1001 
1002 	return (0);
1003 }
1004 
1005 /*
1006  * Supporting Subroutines
1007  */
1008 const struct pci_matchid esa_devices[] = {
1009 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989 },
1010 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3 },
1011 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2 },
1012 };
1013 
1014 int
1015 esa_match(struct device *dev, void *match, void *aux)
1016 {
1017 	return (pci_matchbyid((struct pci_attach_args *)aux, esa_devices,
1018 	    nitems(esa_devices)));
1019 }
1020 
1021 void
1022 esa_attach(struct device *parent, struct device *self, void *aux)
1023 {
1024 	struct esa_softc *sc = (struct esa_softc *)self;
1025 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1026 	pcitag_t tag = pa->pa_tag;
1027 	pci_chipset_tag_t pc = pa->pa_pc;
1028 	pci_intr_handle_t ih;
1029 	struct esa_card_type *card;
1030 	const char *intrstr;
1031 	int i, len;
1032 
1033 	for (card = esa_card_types; card->pci_vendor_id; card++)
1034 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
1035 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
1036 			sc->type = card->type;
1037 			sc->delay1 = card->delay1;
1038 			sc->delay2 = card->delay2;
1039 			break;
1040 		}
1041 
1042 	/* Map I/O register */
1043 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
1044 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) {
1045 		printf(": can't map i/o space\n");
1046 		return;
1047 	}
1048 
1049 	/* Initialize softc */
1050 	sc->sc_tag = tag;
1051 	sc->sc_pct = pc;
1052 	sc->sc_dmat = pa->pa_dmat;
1053 
1054 	/* Map and establish an interrupt */
1055 	if (pci_intr_map(pa, &ih)) {
1056 		printf(": can't map interrupt\n");
1057 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1058 		return;
1059 	}
1060 	intrstr = pci_intr_string(pc, ih);
1061 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, esa_intr, self,
1062             sc->sc_dev.dv_xname);
1063 	if (sc->sc_ih == NULL) {
1064 		printf(": can't establish interrupt");
1065 		if (intrstr != NULL)
1066 			printf(" at %s", intrstr);
1067 		printf("\n");
1068 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1069 		return;
1070 	}
1071 	printf(": %s\n", intrstr);
1072 
1073 	/* Power up chip */
1074 	pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0);
1075 
1076 	/* Init chip */
1077 	if (esa_init(sc) == -1) {
1078 		printf("%s: esa_attach: unable to initialize the card\n",
1079 		    sc->sc_dev.dv_xname);
1080 		pci_intr_disestablish(pc, sc->sc_ih);
1081 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1082 		return;
1083 	}
1084 
1085 	/* create suspend save area */
1086 	len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1087 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1088 	sc->savemem = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1089 	if (sc->savemem == NULL) {
1090 		printf("%s: unable to allocate suspend buffer\n",
1091 		    sc->sc_dev.dv_xname);
1092 		pci_intr_disestablish(pc, sc->sc_ih);
1093 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1094 		return;
1095 	}
1096 
1097 	/*
1098 	 * Every card I've seen has had their channels swapped with respect
1099 	 * to the mixer. Ie:
1100 	 *  $ mixerctl -w outputs.master=0,191
1101 	 * Would result in the _right_ speaker being turned off.
1102 	 *
1103 	 * So, we will swap the left and right mixer channels to compensate
1104 	 * for this.
1105 	 */
1106 	sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS;
1107 	sc->codec_flags |= AC97_HOST_DONT_READ;
1108 
1109 	/* Attach AC97 host interface */
1110 	sc->host_if.arg = self;
1111 	sc->host_if.attach = esa_attach_codec;
1112 	sc->host_if.read = esa_read_codec;
1113 	sc->host_if.write = esa_write_codec;
1114 	sc->host_if.reset = esa_reset_codec;
1115 	sc->host_if.flags = esa_flags_codec;
1116 
1117 	if (ac97_attach(&sc->host_if) != 0) {
1118 		pci_intr_disestablish(pc, sc->sc_ih);
1119 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1120 		free(sc->savemem, M_DEVBUF);
1121 		return;
1122 	}
1123 
1124 	/* initialize list management structures */
1125 	sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0;
1126 	sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS;
1127 	sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0;
1128 	sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS;
1129 	sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0;
1130 	sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS;
1131 	sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC;
1132 	sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC;
1133 
1134 	/* initialize index maps */
1135 	for (i = 0; i < ESA_NUM_VOICES * 2; i++) {
1136 		sc->mixer_list.indexmap[i] = -1;
1137 		sc->msrc_list.indexmap[i] = -1;
1138 		sc->dma_list.indexmap[i] = -1;
1139 		sc->adc1_list.indexmap[i] = -1;
1140 	}
1141 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1142 		sc->voice[i].parent = (struct device *)sc;
1143 		sc->voice[i].index = i;
1144 		sc->sc_audiodev[i] =
1145 		    audio_attach_mi(&esa_hw_if, &sc->voice[i], &sc->sc_dev);
1146 	}
1147 }
1148 
1149 int
1150 esa_detach(struct device *self, int flags)
1151 {
1152 	struct esa_softc *sc = (struct esa_softc *)self;
1153 	int i;
1154 
1155 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1156 		if (sc->sc_audiodev[i] != NULL)
1157 			config_detach(sc->sc_audiodev[i], flags);
1158 	}
1159 
1160 	if (sc->sc_ih != NULL)
1161 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1162 	if (sc->sc_ios)
1163 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1164 
1165 	free(sc->savemem, M_DEVBUF);
1166 
1167 	return (0);
1168 }
1169 
1170 u_int16_t
1171 esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
1172 {
1173 	u_int16_t data;
1174 	bus_space_tag_t iot = sc->sc_iot;
1175 	bus_space_handle_t ioh = sc->sc_ioh;
1176 
1177 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1178 	    region & ESA_MEMTYPE_MASK);
1179 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1180 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
1181 
1182 	return (data);
1183 }
1184 
1185 void
1186 esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
1187 		u_int16_t data)
1188 {
1189 	bus_space_tag_t iot = sc->sc_iot;
1190 	bus_space_handle_t ioh = sc->sc_ioh;
1191 
1192 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1193 	    region & ESA_MEMTYPE_MASK);
1194 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1195 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
1196 
1197 	return;
1198 }
1199 
1200 int
1201 esa_init_codec(struct esa_softc *sc)
1202 {
1203 	bus_space_tag_t iot = sc->sc_iot;
1204 	bus_space_handle_t ioh = sc->sc_ioh;
1205 	u_int32_t data;
1206 
1207 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
1208 
1209 	return ((data & 0x1) ? 0 : 1);
1210 }
1211 
1212 int
1213 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1214 {
1215 	struct esa_softc *sc = aux;
1216 
1217 	sc->codec_if = codec_if;
1218 
1219 	return (0);
1220 }
1221 
1222 int
1223 esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
1224 {
1225 	struct esa_softc *sc = aux;
1226 	bus_space_tag_t iot = sc->sc_iot;
1227 	bus_space_handle_t ioh = sc->sc_ioh;
1228 
1229 	if (esa_wait(sc))
1230 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1231 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
1232 	delay(50);
1233 	if (esa_wait(sc))
1234 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1235 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
1236 
1237 	return (0);
1238 }
1239 
1240 int
1241 esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
1242 {
1243 	struct esa_softc *sc = aux;
1244 	bus_space_tag_t iot = sc->sc_iot;
1245 	bus_space_handle_t ioh = sc->sc_ioh;
1246 
1247 	if (esa_wait(sc)) {
1248 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
1249 		return (-1);
1250 	}
1251 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
1252 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
1253 	delay(50);
1254 
1255 	return (0);
1256 }
1257 
1258 void
1259 esa_reset_codec(void *aux)
1260 {
1261 
1262 	return;
1263 }
1264 
1265 enum ac97_host_flags
1266 esa_flags_codec(void *aux)
1267 {
1268 	struct esa_softc *sc = aux;
1269 
1270 	return (sc->codec_flags);
1271 }
1272 
1273 int
1274 esa_wait(struct esa_softc *sc)
1275 {
1276 	int i, val;
1277 	bus_space_tag_t iot = sc->sc_iot;
1278 	bus_space_handle_t ioh = sc->sc_ioh;
1279 
1280 	for (i = 0; i < 20; i++) {
1281 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
1282 		if ((val & 1) == 0)
1283 			return (0);
1284 		delay(2);
1285 	}
1286 
1287 	return (-1);
1288 }
1289 
1290 int
1291 esa_init(struct esa_softc *sc)
1292 {
1293 	struct esa_voice *vc;
1294 	bus_space_tag_t iot = sc->sc_iot;
1295 	bus_space_handle_t ioh = sc->sc_ioh;
1296 	pcitag_t tag = sc->sc_tag;
1297 	pci_chipset_tag_t pc = sc->sc_pct;
1298 	u_int32_t data, i, size;
1299 	u_int8_t reset_state;
1300 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
1301 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
1302 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
1303 			   &~ 255;
1304 
1305 	/* Disable legacy emulation */
1306 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
1307 	data |= DISABLE_LEGACY;
1308 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
1309 
1310 	esa_config(sc);
1311 
1312 	reset_state = esa_assp_halt(sc);
1313 
1314 	esa_init_codec(sc);
1315 	esa_codec_reset(sc);
1316 
1317 	/* Zero kernel and mixer data */
1318 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
1319 	for (i = 0; i < size / 2; i++) {
1320 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1321 		    ESA_KDATA_BASE_ADDR + i, 0);
1322 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1323 		    ESA_KDATA_BASE_ADDR2 + i, 0);
1324 	}
1325 
1326 	/* Init DMA pointer */
1327 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
1328 	    ESA_KDATA_DMA_XFER0);
1329 
1330 	/* Write kernel code into memory */
1331 	size = nitems(esa_assp_kernel_image);
1332 	for (i = 0; i < size; i++)
1333 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1334 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
1335 
1336 	size = nitems(esa_assp_minisrc_image);
1337 	for (i = 0; i < size; i++)
1338 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
1339 		    esa_assp_minisrc_image[i]);
1340 
1341 	/* Write the coefficients for the low pass filter */
1342 	size = nitems(esa_minisrc_lpf_image);
1343 	for (i = 0; i < size; i++)
1344 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1345 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
1346 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1347 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
1348 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
1349 	/* Init the mixer number */
1350 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1351              ESA_KDATA_MIXER_TASK_NUMBER, 0);
1352 	/* Extreme kernel master volume */
1353 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1354 	    ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME);
1355 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1356             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
1357 
1358 	if (esa_amp_enable(sc))
1359 		return (-1);
1360 
1361 	/* Zero entire DAC/ADC area */
1362 	for (i = 0x1100; i < 0x1c00; i++)
1363 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
1364 
1365 	/* set some sane defaults */
1366 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1367 		vc = &sc->voice[i];
1368 		vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i);
1369 		vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2);
1370 	}
1371 
1372 	esa_enable_interrupts(sc);
1373 
1374 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1375 	    reset_state | ESA_REGB_ENABLE_RESET);
1376 
1377 	return (0);
1378 }
1379 
1380 void
1381 esa_config(struct esa_softc *sc)
1382 {
1383 	bus_space_tag_t iot = sc->sc_iot;
1384 	bus_space_handle_t ioh = sc->sc_ioh;
1385 	pcitag_t tag = sc->sc_tag;
1386 	pci_chipset_tag_t pc = sc->sc_pct;
1387 	u_int32_t data;
1388 
1389 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1390 	data &= ESA_REDUCED_DEBOUNCE;
1391 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
1392 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1393 
1394 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
1395 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1396 	data &= ~ESA_INT_CLK_SELECT;
1397 	if (sc->type == ESS_MAESTRO3) {
1398 		data &= ~ESA_INT_CLK_MULT_ENABLE;
1399 		data |= ESA_INT_CLK_SRC_NOT_PCI;
1400 	}
1401 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
1402 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1403 
1404 	if (sc->type == ESS_ALLEGRO1) {
1405 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
1406 		data |= ESA_IN_CLK_12MHZ_SELECT;
1407 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
1408 	}
1409 
1410 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
1411 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
1412 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
1413 	data |= ESA_ASSP_0_WS_ENABLE;
1414 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
1415 
1416 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
1417 
1418 	return;
1419 }
1420 
1421 u_int8_t
1422 esa_assp_halt(struct esa_softc *sc)
1423 {
1424 	bus_space_tag_t iot = sc->sc_iot;
1425 	bus_space_handle_t ioh = sc->sc_ioh;
1426 	u_int8_t data, reset_state;
1427 
1428 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
1429 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
1430 	delay(10000);		/* XXX use tsleep */
1431 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1432 			reset_state & ~ESA_REGB_ENABLE_RESET);
1433 	delay(10000);		/* XXX use tsleep */
1434 
1435 	return (reset_state);
1436 }
1437 
1438 void
1439 esa_codec_reset(struct esa_softc *sc)
1440 {
1441 	bus_space_tag_t iot = sc->sc_iot;
1442 	bus_space_handle_t ioh = sc->sc_ioh;
1443 	u_int16_t data, dir;
1444 	int retry = 0;
1445 
1446 	do {
1447 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1448 		dir = data | 0x10; /* assuming pci bus master? */
1449 
1450 		/* remote codec config */
1451 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
1452 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
1453 		    data & ~ESA_SECOND_CODEC_ID_MASK);
1454 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
1455 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
1456 		    data & ~ESA_COMMAND_ADDR_OUT);
1457 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
1458 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
1459 		    data & ~ESA_STATUS_ADDR_IN);
1460 
1461 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1462 				  ESA_IO_SRAM_ENABLE);
1463 		delay(20);
1464 
1465 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1466 		    dir & ~ESA_GPO_PRIMARY_AC97);
1467 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
1468 				  ~ESA_GPO_PRIMARY_AC97);
1469 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
1470 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1471 		    dir | ESA_GPO_PRIMARY_AC97);
1472 		delay(sc->delay1 * 1000);
1473 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
1474 				  ESA_GPO_PRIMARY_AC97);
1475 		delay(5);
1476 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1477 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
1478 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1479 		delay(sc->delay2 * 1000);
1480 
1481 		esa_read_codec(sc, 0x7c, &data);
1482 		if ((data == 0) || (data == 0xffff)) {
1483 			retry++;
1484 			if (retry > 3) {
1485 				printf("%s: esa_codec_reset: failed\n",
1486 				    sc->sc_dev.dv_xname);
1487 				break;
1488 			}
1489 			printf("%s: esa_codec_reset: retrying\n",
1490 			    sc->sc_dev.dv_xname);
1491 		} else
1492 			retry = 0;
1493 	} while (retry);
1494 
1495 	return;
1496 }
1497 
1498 int
1499 esa_amp_enable(struct esa_softc *sc)
1500 {
1501 	bus_space_tag_t iot = sc->sc_iot;
1502 	bus_space_handle_t ioh = sc->sc_ioh;
1503 	u_int32_t gpo, polarity_port, polarity;
1504 	u_int16_t data;
1505 
1506 	switch (sc->type) {
1507 	case ESS_ALLEGRO1:
1508 		polarity_port = 0x1800;
1509 		break;
1510 	case ESS_MAESTRO3:
1511 		polarity_port = 0x1100;
1512 		break;
1513 	default:
1514 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
1515 		    sc->sc_dev.dv_xname);
1516 		return (1);
1517 	}
1518 
1519 	gpo = (polarity_port >> 8) & 0x0f;
1520 	polarity = polarity_port >> 12;
1521 	polarity = !polarity;	/* Enable */
1522 	polarity = polarity << gpo;
1523 	gpo = 1 << gpo;
1524 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
1525 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1526 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
1527 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
1528 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
1529 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1530 
1531 	return (0);
1532 }
1533 
1534 void
1535 esa_enable_interrupts(struct esa_softc *sc)
1536 {
1537 	bus_space_tag_t iot = sc->sc_iot;
1538 	bus_space_handle_t ioh = sc->sc_ioh;
1539 	u_int8_t data;
1540 
1541 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
1542 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
1543 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
1544 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
1545 	    data | ESA_ASSP_HOST_INT_ENABLE);
1546 }
1547 
1548 /*
1549  * List management
1550  */
1551 int
1552 esa_add_list(struct esa_voice *vc, struct esa_list *el,
1553 	     u_int16_t val, int index)
1554 {
1555 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1556 
1557 	el->indexmap[index] = el->currlen;
1558 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1559 		       el->mem_addr + el->currlen,
1560 		       val);
1561 
1562 	return (el->currlen++);
1563 }
1564 
1565 void
1566 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index)
1567 {
1568 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1569 	u_int16_t val;
1570 	int lastindex = el->currlen - 1;
1571 	int vindex = el->indexmap[index];
1572 	int i;
1573 
1574 	/* reset our virtual index */
1575 	el->indexmap[index] = -1;
1576 
1577 	if (vindex != lastindex) {
1578 		val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1579 				    el->mem_addr + lastindex);
1580 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1581 			       el->mem_addr + vindex,
1582 			       val);
1583 		for (i = 0; i < ESA_NUM_VOICES * 2; i++)
1584 			if (el->indexmap[i] == lastindex)
1585 				break;
1586 		if (i >= ESA_NUM_VOICES * 2)
1587 			printf("%s: esa_remove_list: invalid task index\n",
1588 			       sc->sc_dev.dv_xname);
1589 		else
1590 			el->indexmap[i] = vindex;
1591 	}
1592 
1593 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1594 		       el->mem_addr + lastindex, 0);
1595 	el->currlen--;
1596 
1597 	return;
1598 }
1599 
1600 int
1601 esa_activate(struct device *self, int act)
1602 {
1603 	struct esa_softc *sc = (struct esa_softc *)self;
1604 
1605 	switch (act) {
1606 	case DVACT_SUSPEND:
1607 		esa_suspend(sc);
1608 		break;
1609 	case DVACT_RESUME:
1610 		esa_resume(sc);
1611 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1612 		break;
1613 	}
1614 	return 0;
1615 }
1616 
1617 int
1618 esa_suspend(struct esa_softc *sc)
1619 {
1620 	bus_space_tag_t iot = sc->sc_iot;
1621 	bus_space_handle_t ioh = sc->sc_ioh;
1622 	int i, index;
1623 
1624 	index = 0;
1625 
1626 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
1627 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
1628 
1629 	esa_assp_halt(sc);
1630 
1631 	/* Save ASSP state */
1632 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1633 	    i++)
1634 		sc->savemem[index++] = esa_read_assp(sc,
1635 		    ESA_MEMTYPE_INTERNAL_CODE, i);
1636 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1637 	    i++)
1638 		sc->savemem[index++] = esa_read_assp(sc,
1639 		    ESA_MEMTYPE_INTERNAL_DATA, i);
1640 
1641 	pci_set_powerstate(sc->sc_pct, sc->sc_tag, PCI_PMCSR_STATE_D3);
1642 
1643 	return (0);
1644 }
1645 
1646 int
1647 esa_resume(struct esa_softc *sc) {
1648 	bus_space_tag_t iot = sc->sc_iot;
1649 	bus_space_handle_t ioh = sc->sc_ioh;
1650 	int i, index;
1651 	u_int8_t reset_state;
1652 
1653 	index = 0;
1654 
1655 	pci_set_powerstate(sc->sc_pct, sc->sc_tag, PCI_PMCSR_STATE_D0);
1656 	delay(10000);
1657 
1658 	esa_config(sc);
1659 
1660 	reset_state = esa_assp_halt(sc);
1661 
1662 	esa_codec_reset(sc);
1663 
1664 	/* restore ASSP */
1665 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1666 	    i++)
1667 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
1668 		    sc->savemem[index++]);
1669 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1670 	    i++)
1671 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
1672 		    sc->savemem[index++]);
1673 
1674 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
1675 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1676 	    reset_state | ESA_REGB_ENABLE_RESET);
1677 
1678 	esa_enable_interrupts(sc);
1679 	esa_amp_enable(sc);
1680 
1681 	return (0);
1682 }
1683 
1684 u_int32_t
1685 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
1686 {
1687 	u_int16_t hi = 0, lo = 0;
1688 	u_int32_t addr;
1689 	int data_offset = ch->data_offset;
1690 
1691 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1692 	    ESA_CDATA_HOST_SRC_CURRENTH);
1693 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1694 	    ESA_CDATA_HOST_SRC_CURRENTL);
1695 
1696 	addr = lo | ((u_int32_t)hi << 16);
1697 	return (addr - ch->start);
1698 }
1699 
1700 paddr_t
1701 esa_mappage(void *addr, void *mem, off_t off, int prot)
1702 {
1703 	struct esa_voice *vc = addr;
1704 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1705 	struct esa_dma *p;
1706 
1707 	if (off < 0)
1708 		return (-1);
1709 	for (p = vc->dma; p && KERNADDR(p) != mem; p = p->next)
1710 		;
1711 	if (!p)
1712 		return (-1);
1713 	return (bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
1714 				off, prot, BUS_DMA_WAITOK));
1715 }
1716