xref: /openbsd-src/sys/dev/pci/esa.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: esa.c,v 1.28 2014/07/12 18:48:51 tedu 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 	mtx_enter(&audio_lock);
437 	vc->play.active = 0;
438 
439 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
440 		       ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0);
441 
442 	sc->sc_ntimers--;
443 	if (sc->sc_ntimers == 0) {
444 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
445 			       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
446 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
447 			       ESA_KDATA_TIMER_COUNT_CURRENT, 0);
448 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
449 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
450 		    data & ~ESA_CLKRUN_GEN_ENABLE);
451 	}
452 
453 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
454 		       ESA_KDATA_MIXER_TASK_NUMBER,
455 		       sc->mixer_list.indexmap[vc->index]);
456 	/* remove ourselves from the packed lists */
457 	esa_remove_list(vc, &sc->mixer_list, vc->index);
458 	esa_remove_list(vc, &sc->dma_list, vc->index);
459 	esa_remove_list(vc, &sc->msrc_list, vc->index);
460 	mtx_leave(&audio_lock);
461 	return (0);
462 }
463 
464 int
465 esa_halt_input(void *hdl)
466 {
467 	struct esa_voice *vc = hdl;
468 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
469 	bus_space_tag_t iot = sc->sc_iot;
470 	bus_space_handle_t ioh = sc->sc_ioh;
471 	u_int32_t data;
472 
473 	if (vc->rec.active == 0)
474 		return (0);
475 
476 	mtx_enter(&audio_lock);
477 	vc->rec.active = 0;
478 
479 	sc->sc_ntimers--;
480 	if (sc->sc_ntimers == 0) {
481 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
482 			       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
483 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
484 			       ESA_KDATA_TIMER_COUNT_CURRENT, 0);
485 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
486 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
487 				  data & ~ESA_CLKRUN_GEN_ENABLE);
488 	}
489 
490 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset +
491 		       ESA_CDATA_INSTANCE_READY, 0);
492 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
493 		       0);
494 
495 	/* remove ourselves from the packed lists */
496 	esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES);
497 	esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES);
498 	esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES);
499 	mtx_leave(&audio_lock);
500 	return (0);
501 }
502 
503 void *
504 esa_malloc(void *hdl, int direction, size_t size, int type, int flags)
505 {
506 	struct esa_voice *vc = hdl;
507 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
508 	struct esa_dma *p;
509 	int error;
510 
511 	p = malloc(sizeof(*p), type, flags);
512 	if (!p)
513 		return (0);
514 	error = esa_allocmem(sc, size, 16, p);
515 	if (error) {
516 		free(p, type, 0);
517 		printf("%s: esa_malloc: not enough memory\n",
518 		    sc->sc_dev.dv_xname);
519 		return (0);
520 	}
521 	p->next = vc->dma;
522 	vc->dma = p;
523 
524 	return (KERNADDR(p));
525 }
526 
527 void
528 esa_free(void *hdl, void *addr, int type)
529 {
530 	struct esa_voice *vc = hdl;
531 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
532 	struct esa_dma *p;
533 	struct esa_dma **pp;
534 
535 	for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next)
536 		if (KERNADDR(p) == addr) {
537 			esa_freemem(sc, p);
538 			*pp = p->next;
539 			free(p, type, 0);
540 			return;
541 		}
542 }
543 
544 int
545 esa_getdev(void *hdl, struct audio_device *ret)
546 {
547 
548 	*ret = esa_device;
549 
550 	return (0);
551 }
552 
553 int
554 esa_set_port(void *hdl, mixer_ctrl_t *mc)
555 {
556 	struct esa_voice *vc = hdl;
557 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
558 
559 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc));
560 }
561 
562 int
563 esa_get_port(void *hdl, mixer_ctrl_t *mc)
564 {
565 	struct esa_voice *vc = hdl;
566 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
567 
568 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc));
569 }
570 
571 int
572 esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
573 {
574 	struct esa_voice *vc = hdl;
575 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
576 
577 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di));
578 }
579 
580 size_t
581 esa_round_buffersize(void *hdl, int direction, size_t bufsize)
582 {
583 	struct esa_voice *vc = hdl;
584 
585 	/*
586 	 * We must be able to do better than this...
587 	 */
588 	vc->play.bufsize = vc->rec.bufsize = 65536;
589 
590 	return (vc->play.bufsize);
591 }
592 
593 int
594 esa_get_props(void *hdl)
595 {
596 
597 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
598 }
599 
600 int
601 esa_trigger_output(void *hdl, void *start, void *end, int blksize,
602 			void (*intr)(void *), void *intrarg,
603 			struct audio_params *param)
604 {
605 	struct esa_voice *vc = hdl;
606 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
607 	struct esa_dma *p;
608 	bus_space_tag_t iot = sc->sc_iot;
609 	bus_space_handle_t ioh = sc->sc_ioh;
610 	u_int32_t data;
611 	u_int32_t bufaddr;
612 	u_int32_t i;
613 	size_t size;
614 
615 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
616 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
617 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
618 			   &~ 255;
619 	int dac_data = ESA_DAC_DATA + (data_bytes * vc->index);
620 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
621 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
622 	int dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
623 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
624 
625 	if (vc->play.active)
626 		return (EINVAL);
627 
628 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
629 		;
630 	if (!p) {
631 		printf("%s: esa_trigger_output: bad addr %p\n",
632 		    sc->sc_dev.dv_xname, start);
633 		return (EINVAL);
634 	}
635 
636 	vc->play.active = 1;
637 	vc->play.intr = intr;
638 	vc->play.arg = intrarg;
639 	vc->play.pos = 0;
640 	vc->play.count = 0;
641 	vc->play.buf = start;
642 	size = (size_t)(((caddr_t)end - (caddr_t)start));
643 	bufaddr = DMAADDR(p);
644 	vc->play.start = bufaddr;
645 
646 #define LO(x) ((x) & 0x0000ffff)
647 #define HI(x) ((x) >> 16)
648 
649 	mtx_enter(&audio_lock);
650 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
651 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
652 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
653 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
654 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
655 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
656 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
657 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
658 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
659 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
660 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
661 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
662 
663 	/* DSP buffers */
664 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
665 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
666 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
667 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
668 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
669 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
670 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
671 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
672 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
673 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
674 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
675 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
676 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
677 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
678 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
679 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
680 
681 	/* Some per-client initializers */
682 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
683 	    ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8);
684 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
685 	    ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
686 	/* Enable or disable low-pass filter? (0xff if rate > 45000) */
687 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
688 	    ESA_SRC3_DIRECTION_OFFSET + 22,
689 	    vc->play.mode.sample_rate > 45000 ? 0xff : 0);
690 	/* Tell it which way DMA is going */
691 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
692 	    ESA_CDATA_DMA_CONTROL,
693 	    ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
694 	    ESA_DMAC_BLOCKF_SELECTOR);
695 
696 	/* Set an armload of static initializers */
697 	for (i = 0; i < nitems(esa_playvals); i++)
698 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
699 		    esa_playvals[i].addr, esa_playvals[i].val);
700 
701 	/* Put us in the packed task lists */
702 	esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT,
703 		     vc->index);
704 	esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT,
705 		     vc->index);
706 	esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT,
707 		     vc->index);
708 #undef LO
709 #undef HI
710 
711 	/* XXX */
712 	//esa_commit_settings(vc);
713 
714 	sc->sc_ntimers++;
715 
716 	if (sc->sc_ntimers == 1) {
717 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
718 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
719 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
720 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
721 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
722 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
723 		    data | ESA_CLKRUN_GEN_ENABLE);
724 	}
725 
726 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
727 	    ESA_CDATA_INSTANCE_READY, 1);
728 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
729 	    ESA_KDATA_MIXER_TASK_NUMBER,
730 	    sc->mixer_list.indexmap[vc->index]);
731 	mtx_leave(&audio_lock);
732 	return (0);
733 }
734 
735 int
736 esa_trigger_input(void *hdl, void *start, void *end, int blksize,
737 			void (*intr)(void *), void *intrarg,
738 			struct audio_params *param)
739 {
740 	struct esa_voice *vc = hdl;
741 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
742 	struct esa_dma *p;
743 	bus_space_tag_t iot = sc->sc_iot;
744 	bus_space_handle_t ioh = sc->sc_ioh;
745 	u_int32_t data;
746 	u_int32_t bufaddr;
747 	u_int32_t i;
748 	size_t size;
749 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
750 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
751 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
752 			   &~ 255;
753 	int adc_data = ESA_DAC_DATA + (data_bytes * vc->index) +
754 		       (data_bytes / 2);
755 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
756 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
757 	int dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
758 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
759 	vc->rec.data_offset = adc_data;
760 
761 	/* We only support 1 recording channel */
762 	if (vc->index > 0)
763 		return (ENODEV);
764 
765 	if (vc->rec.active)
766 		return (EINVAL);
767 
768 	for (p = vc->dma; p && KERNADDR(p) != start; p = p->next)
769 		;
770 	if (!p) {
771 		printf("%s: esa_trigger_input: bad addr %p\n",
772 		    sc->sc_dev.dv_xname, start);
773 		return (EINVAL);
774 	}
775 
776 	vc->rec.active = 1;
777 	vc->rec.intr = intr;
778 	vc->rec.arg = intrarg;
779 	vc->rec.pos = 0;
780 	vc->rec.count = 0;
781 	vc->rec.buf = start;
782 	size = (size_t)(((caddr_t)end - (caddr_t)start));
783 	bufaddr = DMAADDR(p);
784 	vc->rec.start = bufaddr;
785 
786 #define LO(x) ((x) & 0x0000ffff)
787 #define HI(x) ((x) >> 16)
788 	mtx_enter(&audio_lock);
789 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
790 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
791 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
792 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
793 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
794 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
795 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
796 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
797 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
798 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
799 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
800 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
801 
802 	/* DSP buffers */
803 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
804 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
805 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
806 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
807 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
808 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
809 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
810 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
811 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
812 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
813 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
814 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
815 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
816 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
817 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
818 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
819 
820 	/* Some per-client initializers */
821 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
822 	    ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8);
823 	/* Tell it which way DMA is going */
824 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
825 	    ESA_CDATA_DMA_CONTROL,
826 	    ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT +
827 	    ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR);
828 
829 	/* Set an armload of static initializers */
830 	for (i = 0; i < nitems(esa_recvals); i++)
831 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
832 		    esa_recvals[i].addr, esa_recvals[i].val);
833 
834 	/* Put us in the packed task lists */
835 	esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT,
836 		     vc->index + ESA_NUM_VOICES);
837 	esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT,
838 		     vc->index + ESA_NUM_VOICES);
839 	esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT,
840 		     vc->index + ESA_NUM_VOICES);
841 #undef LO
842 #undef HI
843 
844 	/* XXX */
845 	//esa_commit_settings(vc);
846 
847 	sc->sc_ntimers++;
848 	if (sc->sc_ntimers == 1) {
849 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
850 		    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
851 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
852 		    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
853 		data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
854 		bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
855 		    data | ESA_CLKRUN_GEN_ENABLE);
856 	}
857 
858 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
859 	    ESA_CDATA_INSTANCE_READY, 1);
860 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST,
861 	    1);
862 	mtx_leave(&audio_lock);
863 	return (0);
864 }
865 
866 /* Interrupt handler */
867 
868 int
869 esa_intr(void *hdl)
870 {
871 	struct esa_softc *sc = hdl;
872 	struct esa_voice *vc;
873 	bus_space_tag_t iot = sc->sc_iot;
874 	bus_space_handle_t ioh = sc->sc_ioh;
875 	u_int8_t status, ctl;
876 	u_int32_t pos;
877 	u_int32_t diff;
878 	u_int32_t play_blksize, play_bufsize;
879 	u_int32_t rec_blksize, rec_bufsize;
880 	int i, claimed = 0;
881 
882 	mtx_enter(&audio_lock);
883 	status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
884 	if (status == 0xff) {
885 		mtx_leave(&audio_lock);
886 		return (0);
887 	}
888 
889 	/* ack the interrupt */
890 	bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status);
891 
892 	if (status & ESA_HV_INT_PENDING) {
893 		u_int8_t event;
894 
895 		printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname);
896 		event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
897 		switch(event) {
898 		case 0x99:
899 		case 0xaa:
900 		case 0x66:
901 		case 0x88:
902 			printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname);
903 			break;
904 		default:
905 			printf("%s: unknown hwvol event 0x%02x\n",
906 			    sc->sc_dev.dv_xname, event);
907 			break;
908 		}
909 		bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
910 		claimed = 1;
911 	}
912 
913 	if (status & ESA_ASSP_INT_PENDING) {
914 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
915 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
916 			ctl = bus_space_read_1(iot, ioh,
917 					       ESA_ASSP_HOST_INT_STATUS);
918 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
919 				bus_space_write_1(iot, ioh,
920 				    ESA_ASSP_HOST_INT_STATUS,
921 				    ESA_DSP2HOST_REQ_TIMER);
922 				for (i = 0; i < ESA_NUM_VOICES; i++) {
923 					vc = &sc->voice[i];
924 					if (vc->play.active) {
925 						play_blksize = vc->play.blksize;
926 						play_bufsize = vc->play.bufsize;
927 						pos = esa_get_pointer(sc, &vc->play)
928 						    % play_bufsize;
929 						diff = (play_bufsize + pos - vc->play.pos)
930 						    % play_bufsize;
931 						vc->play.pos = pos;
932 						vc->play.count += diff;
933 						while(vc->play.count >= play_blksize) {
934 							vc->play.count -= play_blksize;
935 							(*vc->play.intr)(vc->play.arg);
936 						}
937 					}
938 					if (vc->rec.active) {
939 						rec_blksize = vc->rec.blksize;
940 						rec_bufsize = vc->rec.bufsize;
941 						pos = esa_get_pointer(sc, &vc->rec)
942 						    % rec_bufsize;
943 						diff = (rec_bufsize + pos - vc->rec.pos)
944 						    % rec_bufsize;
945 						vc->rec.pos = pos;
946 						vc->rec.count += diff;
947 						while(vc->rec.count >= rec_blksize) {
948 							vc->rec.count -= rec_blksize;
949 							(*vc->rec.intr)(vc->rec.arg);
950 						}
951 					}
952 				}
953 			}
954 		}
955 		claimed = 1;
956 	}
957 	mtx_leave(&audio_lock);
958 	return (claimed);
959 }
960 
961 int
962 esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
963 		struct esa_dma *p)
964 {
965 	int error;
966 
967 	p->size = size;
968 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
969 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
970 				 &p->nsegs, BUS_DMA_NOWAIT);
971 	if (error)
972 		return (error);
973 
974 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
975 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
976 	if (error)
977 		goto free;
978 
979 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
980 				  BUS_DMA_NOWAIT, &p->map);
981 	if (error)
982 		goto unmap;
983 
984 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
985 				BUS_DMA_NOWAIT);
986 	if (error)
987 		goto destroy;
988 
989 	return (0);
990 
991 destroy:
992 	bus_dmamap_destroy(sc->sc_dmat, p->map);
993 unmap:
994 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
995 free:
996 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
997 
998 	return (error);
999 }
1000 
1001 int
1002 esa_freemem(struct esa_softc *sc, struct esa_dma *p)
1003 {
1004 
1005 	bus_dmamap_unload(sc->sc_dmat, p->map);
1006 	bus_dmamap_destroy(sc->sc_dmat, p->map);
1007 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
1008 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1009 
1010 	return (0);
1011 }
1012 
1013 /*
1014  * Supporting Subroutines
1015  */
1016 const struct pci_matchid esa_devices[] = {
1017 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989 },
1018 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3 },
1019 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2 },
1020 };
1021 
1022 int
1023 esa_match(struct device *dev, void *match, void *aux)
1024 {
1025 	return (pci_matchbyid((struct pci_attach_args *)aux, esa_devices,
1026 	    nitems(esa_devices)));
1027 }
1028 
1029 void
1030 esa_attach(struct device *parent, struct device *self, void *aux)
1031 {
1032 	struct esa_softc *sc = (struct esa_softc *)self;
1033 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1034 	pcitag_t tag = pa->pa_tag;
1035 	pci_chipset_tag_t pc = pa->pa_pc;
1036 	pci_intr_handle_t ih;
1037 	struct esa_card_type *card;
1038 	const char *intrstr;
1039 	int i, len;
1040 
1041 	for (card = esa_card_types; card->pci_vendor_id; card++)
1042 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
1043 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
1044 			sc->type = card->type;
1045 			sc->delay1 = card->delay1;
1046 			sc->delay2 = card->delay2;
1047 			break;
1048 		}
1049 
1050 	/* Map I/O register */
1051 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
1052 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) {
1053 		printf(": can't map i/o space\n");
1054 		return;
1055 	}
1056 
1057 	/* Initialize softc */
1058 	sc->sc_tag = tag;
1059 	sc->sc_pct = pc;
1060 	sc->sc_dmat = pa->pa_dmat;
1061 
1062 	/* Map and establish an interrupt */
1063 	if (pci_intr_map(pa, &ih)) {
1064 		printf(": can't map interrupt\n");
1065 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1066 		return;
1067 	}
1068 	intrstr = pci_intr_string(pc, ih);
1069 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE,
1070 	    esa_intr, self, sc->sc_dev.dv_xname);
1071 	if (sc->sc_ih == NULL) {
1072 		printf(": can't establish interrupt");
1073 		if (intrstr != NULL)
1074 			printf(" at %s", intrstr);
1075 		printf("\n");
1076 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1077 		return;
1078 	}
1079 	printf(": %s\n", intrstr);
1080 
1081 	/* Power up chip */
1082 	pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0);
1083 
1084 	/* Init chip */
1085 	if (esa_init(sc) == -1) {
1086 		printf("%s: esa_attach: unable to initialize the card\n",
1087 		    sc->sc_dev.dv_xname);
1088 		pci_intr_disestablish(pc, sc->sc_ih);
1089 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1090 		return;
1091 	}
1092 
1093 	/* create suspend save area */
1094 	len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
1095 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
1096 	sc->savemem = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1097 	if (sc->savemem == NULL) {
1098 		printf("%s: unable to allocate suspend buffer\n",
1099 		    sc->sc_dev.dv_xname);
1100 		pci_intr_disestablish(pc, sc->sc_ih);
1101 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1102 		return;
1103 	}
1104 
1105 	/*
1106 	 * Every card I've seen has had their channels swapped with respect
1107 	 * to the mixer. Ie:
1108 	 *  $ mixerctl -w outputs.master=0,191
1109 	 * Would result in the _right_ speaker being turned off.
1110 	 *
1111 	 * So, we will swap the left and right mixer channels to compensate
1112 	 * for this.
1113 	 */
1114 	sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS;
1115 	sc->codec_flags |= AC97_HOST_DONT_READ;
1116 
1117 	/* Attach AC97 host interface */
1118 	sc->host_if.arg = self;
1119 	sc->host_if.attach = esa_attach_codec;
1120 	sc->host_if.read = esa_read_codec;
1121 	sc->host_if.write = esa_write_codec;
1122 	sc->host_if.reset = esa_reset_codec;
1123 	sc->host_if.flags = esa_flags_codec;
1124 
1125 	if (ac97_attach(&sc->host_if) != 0) {
1126 		pci_intr_disestablish(pc, sc->sc_ih);
1127 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1128 		free(sc->savemem, M_DEVBUF, 0);
1129 		return;
1130 	}
1131 
1132 	/* initialize list management structures */
1133 	sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0;
1134 	sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS;
1135 	sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0;
1136 	sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS;
1137 	sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0;
1138 	sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS;
1139 	sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC;
1140 	sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC;
1141 
1142 	/* initialize index maps */
1143 	for (i = 0; i < ESA_NUM_VOICES * 2; i++) {
1144 		sc->mixer_list.indexmap[i] = -1;
1145 		sc->msrc_list.indexmap[i] = -1;
1146 		sc->dma_list.indexmap[i] = -1;
1147 		sc->adc1_list.indexmap[i] = -1;
1148 	}
1149 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1150 		sc->voice[i].parent = (struct device *)sc;
1151 		sc->voice[i].index = i;
1152 		sc->sc_audiodev[i] =
1153 		    audio_attach_mi(&esa_hw_if, &sc->voice[i], &sc->sc_dev);
1154 	}
1155 }
1156 
1157 int
1158 esa_detach(struct device *self, int flags)
1159 {
1160 	struct esa_softc *sc = (struct esa_softc *)self;
1161 	int i;
1162 
1163 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1164 		if (sc->sc_audiodev[i] != NULL)
1165 			config_detach(sc->sc_audiodev[i], flags);
1166 	}
1167 
1168 	if (sc->sc_ih != NULL)
1169 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
1170 	if (sc->sc_ios)
1171 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
1172 
1173 	free(sc->savemem, M_DEVBUF, 0);
1174 
1175 	return (0);
1176 }
1177 
1178 u_int16_t
1179 esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
1180 {
1181 	u_int16_t data;
1182 	bus_space_tag_t iot = sc->sc_iot;
1183 	bus_space_handle_t ioh = sc->sc_ioh;
1184 
1185 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1186 	    region & ESA_MEMTYPE_MASK);
1187 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1188 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
1189 
1190 	return (data);
1191 }
1192 
1193 void
1194 esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
1195 		u_int16_t data)
1196 {
1197 	bus_space_tag_t iot = sc->sc_iot;
1198 	bus_space_handle_t ioh = sc->sc_ioh;
1199 
1200 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
1201 	    region & ESA_MEMTYPE_MASK);
1202 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
1203 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
1204 
1205 	return;
1206 }
1207 
1208 int
1209 esa_init_codec(struct esa_softc *sc)
1210 {
1211 	bus_space_tag_t iot = sc->sc_iot;
1212 	bus_space_handle_t ioh = sc->sc_ioh;
1213 	u_int32_t data;
1214 
1215 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
1216 
1217 	return ((data & 0x1) ? 0 : 1);
1218 }
1219 
1220 int
1221 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
1222 {
1223 	struct esa_softc *sc = aux;
1224 
1225 	sc->codec_if = codec_if;
1226 
1227 	return (0);
1228 }
1229 
1230 int
1231 esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
1232 {
1233 	struct esa_softc *sc = aux;
1234 	bus_space_tag_t iot = sc->sc_iot;
1235 	bus_space_handle_t ioh = sc->sc_ioh;
1236 
1237 	if (esa_wait(sc))
1238 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1239 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
1240 	delay(50);
1241 	if (esa_wait(sc))
1242 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
1243 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
1244 
1245 	return (0);
1246 }
1247 
1248 int
1249 esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
1250 {
1251 	struct esa_softc *sc = aux;
1252 	bus_space_tag_t iot = sc->sc_iot;
1253 	bus_space_handle_t ioh = sc->sc_ioh;
1254 
1255 	if (esa_wait(sc)) {
1256 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
1257 		return (-1);
1258 	}
1259 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
1260 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
1261 	delay(50);
1262 
1263 	return (0);
1264 }
1265 
1266 void
1267 esa_reset_codec(void *aux)
1268 {
1269 
1270 	return;
1271 }
1272 
1273 enum ac97_host_flags
1274 esa_flags_codec(void *aux)
1275 {
1276 	struct esa_softc *sc = aux;
1277 
1278 	return (sc->codec_flags);
1279 }
1280 
1281 int
1282 esa_wait(struct esa_softc *sc)
1283 {
1284 	int i, val;
1285 	bus_space_tag_t iot = sc->sc_iot;
1286 	bus_space_handle_t ioh = sc->sc_ioh;
1287 
1288 	for (i = 0; i < 20; i++) {
1289 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
1290 		if ((val & 1) == 0)
1291 			return (0);
1292 		delay(2);
1293 	}
1294 
1295 	return (-1);
1296 }
1297 
1298 int
1299 esa_init(struct esa_softc *sc)
1300 {
1301 	struct esa_voice *vc;
1302 	bus_space_tag_t iot = sc->sc_iot;
1303 	bus_space_handle_t ioh = sc->sc_ioh;
1304 	pcitag_t tag = sc->sc_tag;
1305 	pci_chipset_tag_t pc = sc->sc_pct;
1306 	u_int32_t data, i, size;
1307 	u_int8_t reset_state;
1308 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
1309 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
1310 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
1311 			   &~ 255;
1312 
1313 	/* Disable legacy emulation */
1314 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
1315 	data |= DISABLE_LEGACY;
1316 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
1317 
1318 	esa_config(sc);
1319 
1320 	reset_state = esa_assp_halt(sc);
1321 
1322 	esa_init_codec(sc);
1323 	esa_codec_reset(sc);
1324 
1325 	/* Zero kernel and mixer data */
1326 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
1327 	for (i = 0; i < size / 2; i++) {
1328 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1329 		    ESA_KDATA_BASE_ADDR + i, 0);
1330 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1331 		    ESA_KDATA_BASE_ADDR2 + i, 0);
1332 	}
1333 
1334 	/* Init DMA pointer */
1335 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
1336 	    ESA_KDATA_DMA_XFER0);
1337 
1338 	/* Write kernel code into memory */
1339 	size = nitems(esa_assp_kernel_image);
1340 	for (i = 0; i < size; i++)
1341 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1342 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
1343 
1344 	size = nitems(esa_assp_minisrc_image);
1345 	for (i = 0; i < size; i++)
1346 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
1347 		    esa_assp_minisrc_image[i]);
1348 
1349 	/* Write the coefficients for the low pass filter */
1350 	size = nitems(esa_minisrc_lpf_image);
1351 	for (i = 0; i < size; i++)
1352 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1353 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
1354 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
1355 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
1356 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
1357 	/* Init the mixer number */
1358 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1359              ESA_KDATA_MIXER_TASK_NUMBER, 0);
1360 	/* Extreme kernel master volume */
1361 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1362 	    ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME);
1363 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1364             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
1365 
1366 	if (esa_amp_enable(sc))
1367 		return (-1);
1368 
1369 	/* Zero entire DAC/ADC area */
1370 	for (i = 0x1100; i < 0x1c00; i++)
1371 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
1372 
1373 	/* set some sane defaults */
1374 	for (i = 0; i < ESA_NUM_VOICES; i++) {
1375 		vc = &sc->voice[i];
1376 		vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i);
1377 		vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2);
1378 	}
1379 
1380 	esa_enable_interrupts(sc);
1381 
1382 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1383 	    reset_state | ESA_REGB_ENABLE_RESET);
1384 
1385 	return (0);
1386 }
1387 
1388 void
1389 esa_config(struct esa_softc *sc)
1390 {
1391 	bus_space_tag_t iot = sc->sc_iot;
1392 	bus_space_handle_t ioh = sc->sc_ioh;
1393 	pcitag_t tag = sc->sc_tag;
1394 	pci_chipset_tag_t pc = sc->sc_pct;
1395 	u_int32_t data;
1396 
1397 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1398 	data &= ESA_REDUCED_DEBOUNCE;
1399 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
1400 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1401 
1402 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
1403 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
1404 	data &= ~ESA_INT_CLK_SELECT;
1405 	if (sc->type == ESS_MAESTRO3) {
1406 		data &= ~ESA_INT_CLK_MULT_ENABLE;
1407 		data |= ESA_INT_CLK_SRC_NOT_PCI;
1408 	}
1409 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
1410 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
1411 
1412 	if (sc->type == ESS_ALLEGRO1) {
1413 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
1414 		data |= ESA_IN_CLK_12MHZ_SELECT;
1415 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
1416 	}
1417 
1418 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
1419 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
1420 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
1421 	data |= ESA_ASSP_0_WS_ENABLE;
1422 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
1423 
1424 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
1425 
1426 	return;
1427 }
1428 
1429 u_int8_t
1430 esa_assp_halt(struct esa_softc *sc)
1431 {
1432 	bus_space_tag_t iot = sc->sc_iot;
1433 	bus_space_handle_t ioh = sc->sc_ioh;
1434 	u_int8_t data, reset_state;
1435 
1436 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
1437 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
1438 	delay(10000);		/* XXX use tsleep */
1439 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1440 			reset_state & ~ESA_REGB_ENABLE_RESET);
1441 	delay(10000);		/* XXX use tsleep */
1442 
1443 	return (reset_state);
1444 }
1445 
1446 void
1447 esa_codec_reset(struct esa_softc *sc)
1448 {
1449 	bus_space_tag_t iot = sc->sc_iot;
1450 	bus_space_handle_t ioh = sc->sc_ioh;
1451 	u_int16_t data, dir;
1452 	int retry = 0;
1453 
1454 	do {
1455 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1456 		dir = data | 0x10; /* assuming pci bus master? */
1457 
1458 		/* remote codec config */
1459 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
1460 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
1461 		    data & ~ESA_SECOND_CODEC_ID_MASK);
1462 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
1463 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
1464 		    data & ~ESA_COMMAND_ADDR_OUT);
1465 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
1466 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
1467 		    data & ~ESA_STATUS_ADDR_IN);
1468 
1469 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1470 				  ESA_IO_SRAM_ENABLE);
1471 		delay(20);
1472 
1473 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1474 		    dir & ~ESA_GPO_PRIMARY_AC97);
1475 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
1476 				  ~ESA_GPO_PRIMARY_AC97);
1477 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
1478 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
1479 		    dir | ESA_GPO_PRIMARY_AC97);
1480 		delay(sc->delay1 * 1000);
1481 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
1482 				  ESA_GPO_PRIMARY_AC97);
1483 		delay(5);
1484 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
1485 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
1486 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1487 		delay(sc->delay2 * 1000);
1488 
1489 		esa_read_codec(sc, 0x7c, &data);
1490 		if ((data == 0) || (data == 0xffff)) {
1491 			retry++;
1492 			if (retry > 3) {
1493 				printf("%s: esa_codec_reset: failed\n",
1494 				    sc->sc_dev.dv_xname);
1495 				break;
1496 			}
1497 			printf("%s: esa_codec_reset: retrying\n",
1498 			    sc->sc_dev.dv_xname);
1499 		} else
1500 			retry = 0;
1501 	} while (retry);
1502 
1503 	return;
1504 }
1505 
1506 int
1507 esa_amp_enable(struct esa_softc *sc)
1508 {
1509 	bus_space_tag_t iot = sc->sc_iot;
1510 	bus_space_handle_t ioh = sc->sc_ioh;
1511 	u_int32_t gpo, polarity_port, polarity;
1512 	u_int16_t data;
1513 
1514 	switch (sc->type) {
1515 	case ESS_ALLEGRO1:
1516 		polarity_port = 0x1800;
1517 		break;
1518 	case ESS_MAESTRO3:
1519 		polarity_port = 0x1100;
1520 		break;
1521 	default:
1522 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
1523 		    sc->sc_dev.dv_xname);
1524 		return (1);
1525 	}
1526 
1527 	gpo = (polarity_port >> 8) & 0x0f;
1528 	polarity = polarity_port >> 12;
1529 	polarity = !polarity;	/* Enable */
1530 	polarity = polarity << gpo;
1531 	gpo = 1 << gpo;
1532 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
1533 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
1534 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
1535 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
1536 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
1537 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
1538 
1539 	return (0);
1540 }
1541 
1542 void
1543 esa_enable_interrupts(struct esa_softc *sc)
1544 {
1545 	bus_space_tag_t iot = sc->sc_iot;
1546 	bus_space_handle_t ioh = sc->sc_ioh;
1547 	u_int8_t data;
1548 
1549 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
1550 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
1551 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
1552 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
1553 	    data | ESA_ASSP_HOST_INT_ENABLE);
1554 }
1555 
1556 /*
1557  * List management
1558  */
1559 int
1560 esa_add_list(struct esa_voice *vc, struct esa_list *el,
1561 	     u_int16_t val, int index)
1562 {
1563 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1564 
1565 	el->indexmap[index] = el->currlen;
1566 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1567 		       el->mem_addr + el->currlen,
1568 		       val);
1569 
1570 	return (el->currlen++);
1571 }
1572 
1573 void
1574 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index)
1575 {
1576 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1577 	u_int16_t val;
1578 	int lastindex = el->currlen - 1;
1579 	int vindex = el->indexmap[index];
1580 	int i;
1581 
1582 	/* reset our virtual index */
1583 	el->indexmap[index] = -1;
1584 
1585 	if (vindex != lastindex) {
1586 		val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1587 				    el->mem_addr + lastindex);
1588 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1589 			       el->mem_addr + vindex,
1590 			       val);
1591 		for (i = 0; i < ESA_NUM_VOICES * 2; i++)
1592 			if (el->indexmap[i] == lastindex)
1593 				break;
1594 		if (i >= ESA_NUM_VOICES * 2)
1595 			printf("%s: esa_remove_list: invalid task index\n",
1596 			       sc->sc_dev.dv_xname);
1597 		else
1598 			el->indexmap[i] = vindex;
1599 	}
1600 
1601 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
1602 		       el->mem_addr + lastindex, 0);
1603 	el->currlen--;
1604 
1605 	return;
1606 }
1607 
1608 int
1609 esa_activate(struct device *self, int act)
1610 {
1611 	struct esa_softc *sc = (struct esa_softc *)self;
1612 
1613 	switch (act) {
1614 	case DVACT_SUSPEND:
1615 		esa_suspend(sc);
1616 		break;
1617 	case DVACT_RESUME:
1618 		esa_resume(sc);
1619 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1620 		break;
1621 	}
1622 	return 0;
1623 }
1624 
1625 int
1626 esa_suspend(struct esa_softc *sc)
1627 {
1628 	bus_space_tag_t iot = sc->sc_iot;
1629 	bus_space_handle_t ioh = sc->sc_ioh;
1630 	int i, index;
1631 
1632 	index = 0;
1633 
1634 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
1635 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
1636 
1637 	esa_assp_halt(sc);
1638 
1639 	/* Save ASSP state */
1640 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1641 	    i++)
1642 		sc->savemem[index++] = esa_read_assp(sc,
1643 		    ESA_MEMTYPE_INTERNAL_CODE, i);
1644 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1645 	    i++)
1646 		sc->savemem[index++] = esa_read_assp(sc,
1647 		    ESA_MEMTYPE_INTERNAL_DATA, i);
1648 
1649 	return (0);
1650 }
1651 
1652 int
1653 esa_resume(struct esa_softc *sc) {
1654 	bus_space_tag_t iot = sc->sc_iot;
1655 	bus_space_handle_t ioh = sc->sc_ioh;
1656 	int i, index;
1657 	u_int8_t reset_state;
1658 
1659 	index = 0;
1660 
1661 	esa_config(sc);
1662 
1663 	reset_state = esa_assp_halt(sc);
1664 
1665 	esa_codec_reset(sc);
1666 
1667 	/* restore ASSP */
1668 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
1669 	    i++)
1670 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
1671 		    sc->savemem[index++]);
1672 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
1673 	    i++)
1674 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
1675 		    sc->savemem[index++]);
1676 
1677 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
1678 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
1679 	    reset_state | ESA_REGB_ENABLE_RESET);
1680 
1681 	esa_enable_interrupts(sc);
1682 	esa_amp_enable(sc);
1683 
1684 	return (0);
1685 }
1686 
1687 u_int32_t
1688 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
1689 {
1690 	u_int16_t hi = 0, lo = 0;
1691 	u_int32_t addr;
1692 	int data_offset = ch->data_offset;
1693 
1694 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1695 	    ESA_CDATA_HOST_SRC_CURRENTH);
1696 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
1697 	    ESA_CDATA_HOST_SRC_CURRENTL);
1698 
1699 	addr = lo | ((u_int32_t)hi << 16);
1700 	return (addr - ch->start);
1701 }
1702 
1703 paddr_t
1704 esa_mappage(void *addr, void *mem, off_t off, int prot)
1705 {
1706 	struct esa_voice *vc = addr;
1707 	struct esa_softc *sc = (struct esa_softc *)vc->parent;
1708 	struct esa_dma *p;
1709 
1710 	if (off < 0)
1711 		return (-1);
1712 	for (p = vc->dma; p && KERNADDR(p) != mem; p = p->next)
1713 		;
1714 	if (!p)
1715 		return (-1);
1716 	return (bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
1717 				off, prot, BUS_DMA_WAITOK));
1718 }
1719