xref: /openbsd/sys/dev/pci/emuxki.c (revision 5a8d990a)
1 /*	$OpenBSD: emuxki.c,v 1.60 2022/10/19 19:14:16 kn Exp $	*/
2 /*	$NetBSD: emuxki.c,v 1.1 2001/10/17 18:39:41 jdolecek Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Yannick Montulet.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Driver for Creative Labs SBLive! series and probably PCI512.
35  *
36  * Known bugs:
37  * - inversed stereo at ac97 codec level
38  *   (XXX jdolecek - don't see the problem? maybe because auvia(4) has
39  *    it swapped too?)
40  * - bass disappear when you plug rear jack-in on Cambridge FPS2000 speakers
41  *   (and presumably all speakers that support front and rear jack-in)
42  *
43  * TODO:
44  * - Digital Outputs
45  * - (midi/mpu),joystick support
46  * - Multiple voices play (problem with /dev/audio architecture)
47  * - Multiple sources recording (Pb with audio(4))
48  * - Independent modification of each channel's parameters (via mixer ?)
49  * - DSP FX patches (to make fx like chipmunk)
50  */
51 
52 #include <sys/param.h>
53 #include <sys/device.h>
54 #include <sys/errno.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
57 #include <sys/systm.h>
58 #include <sys/audioio.h>
59 
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcidevs.h>
63 
64 #include <dev/audio_if.h>
65 #include <dev/ic/ac97.h>
66 
67 #include <dev/pci/emuxkireg.h>
68 #include <dev/pci/emuxkivar.h>
69 
70 /* autoconf goo */
71 int  emuxki_match(struct device *, void *, void *);
72 void emuxki_attach(struct device *, struct device *, void *);
73 int  emuxki_detach(struct device *, int);
74 int  emuxki_activate(struct device *, int);
75 int  emuxki_scinit(struct emuxki_softc *sc, int);
76 void emuxki_pci_shutdown(struct emuxki_softc *sc);
77 
78 /* dma mem mgmt */
79 struct dmamem *emuxki_dmamem_alloc(bus_dma_tag_t, size_t, bus_size_t,
80 				 int, int, int);
81 void	emuxki_dmamem_free(struct dmamem *, int);
82 void	emuxki_dmamem_delete(struct dmamem *mem, int type);
83 
84 struct emuxki_mem *emuxki_mem_new(struct emuxki_softc *sc, int ptbidx,
85 	size_t size, int type, int flags);
86 void emuxki_mem_delete(struct emuxki_mem *mem, int type);
87 
88 /* Emu10k1 init & shutdown */
89 int  emuxki_init(struct emuxki_softc *, int);
90 void emuxki_shutdown(struct emuxki_softc *);
91 
92 /* Emu10k1 mem mgmt */
93 void   *emuxki_pmem_alloc(struct emuxki_softc *, size_t,int,int);
94 void   *emuxki_rmem_alloc(struct emuxki_softc *, size_t,int,int);
95 
96 /*
97  * Emu10k1 channels funcs : There is no direct access to channels, everything
98  * is done through voices I will at least provide channel based fx params
99  * modification, later...
100  */
101 
102 /* Emu10k1 voice mgmt */
103 struct emuxki_voice *emuxki_voice_new(struct emuxki_softc *, u_int8_t);
104 void   emuxki_voice_delete(struct emuxki_voice *);
105 int    emuxki_voice_set_audioparms(struct emuxki_voice *, u_int8_t, u_int8_t, u_int32_t);
106 /* emuxki_voice_set_fxparms will come later, it'll need channel distinction */
107 int emuxki_voice_set_bufparms(struct emuxki_voice *, void *, u_int32_t, u_int16_t);
108 int emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo);
109 int emuxki_voice_dataloc_create(struct emuxki_voice *voice);
110 void emuxki_voice_dataloc_destroy(struct emuxki_voice *voice);
111 void emuxki_voice_commit_parms(struct emuxki_voice *);
112 void emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source);
113 int emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source);
114 int emuxki_voice_adc_rate(struct emuxki_voice *);
115 u_int32_t emuxki_voice_curaddr(struct emuxki_voice *);
116 int emuxki_set_vparms(struct emuxki_voice *voice, struct audio_params *p);
117 int emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate);
118 void emuxki_voice_start(struct emuxki_voice *, void (*) (void *), void *);
119 void emuxki_voice_halt(struct emuxki_voice *);
120 int emuxki_voice_channel_create(struct emuxki_voice *voice);
121 void emuxki_voice_channel_destroy(struct emuxki_voice *voice);
122 
123 struct emuxki_channel *emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num);
124 void emuxki_channel_delete(struct emuxki_channel *chan);
125 void emuxki_channel_start(struct emuxki_channel *chan);
126 void emuxki_channel_stop(struct emuxki_channel *chan);
127 void emuxki_channel_commit_fx(struct emuxki_channel *chan);
128 void emuxki_channel_commit_parms(struct emuxki_channel *chan);
129 void emuxki_channel_set_bufparms(struct emuxki_channel *chan, u_int32_t start, u_int32_t end);
130 void emuxki_channel_set_srate(struct emuxki_channel *chan, u_int32_t srate);
131 void emuxki_channel_set_fxsend(struct emuxki_channel *chan,
132 	struct emuxki_chanparms_fxsend *fxsend);
133 void emuxki_chanparms_set_defaults(struct emuxki_channel *chan);
134 
135 void emuxki_resched_timer(struct emuxki_softc *sc);
136 
137 /*
138  * Emu10k1 stream mgmt : not done yet
139  */
140 #if 0
141 struct emuxki_stream *emuxki_stream_new(struct emu10k1 *);
142 void   emuxki_stream_delete(struct emuxki_stream *);
143 int    emuxki_stream_set_audio_params(struct emuxki_stream *, u_int8_t,
144 					    u_int8_t, u_int8_t, u_int16_t);
145 void   emuxki_stream_start(struct emuxki_stream *);
146 void   emuxki_stream_halt(struct emuxki_stream *);
147 #endif
148 
149 /* fx interface */
150 void emuxki_initfx(struct emuxki_softc *sc);
151 void emuxki_dsp_addop(struct emuxki_softc *sc, u_int16_t *pc, u_int8_t op,
152 	u_int16_t r, u_int16_t a, u_int16_t x, u_int16_t y);
153 void emuxki_write_micro(struct emuxki_softc *sc, u_int32_t pc, u_int32_t data);
154 
155 /* audio interface callbacks */
156 
157 int	emuxki_open(void *, int);
158 void	emuxki_close(void *);
159 
160 int	emuxki_set_params(void *, int, int,
161 				      struct audio_params *,
162 				      struct audio_params *);
163 
164 int	emuxki_round_blocksize(void *, int);
165 size_t	emuxki_round_buffersize(void *, int, size_t);
166 
167 int	emuxki_trigger_output(void *, void *, void *, int, void (*)(void *),
168 	    void *, struct audio_params *);
169 int	emuxki_trigger_input(void *, void *, void *, int, void (*) (void *),
170 	    void *, struct audio_params *);
171 int	emuxki_halt_output(void *);
172 int	emuxki_halt_input(void *);
173 
174 int	emuxki_set_port(void *, mixer_ctrl_t *);
175 int	emuxki_get_port(void *, mixer_ctrl_t *);
176 int	emuxki_query_devinfo(void *, mixer_devinfo_t *);
177 
178 void   *emuxki_allocm(void *, int, size_t, int, int);
179 void	emuxki_freem(void *, void *, int);
180 
181 int	emuxki_get_props(void *);
182 
183 /* Interrupt handler */
184 int  emuxki_intr(void *);
185 
186 /* Emu10k1 AC97 interface callbacks */
187 int  emuxki_ac97_init(struct emuxki_softc *sc);
188 int  emuxki_ac97_attach(void *, struct ac97_codec_if *);
189 int  emuxki_ac97_read(void *, u_int8_t, u_int16_t *);
190 int  emuxki_ac97_write(void *, u_int8_t, u_int16_t);
191 void emuxki_ac97_reset(void *);
192 
193 const struct pci_matchid emuxki_devices[] = {
194 	{ PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_SBLIVE },
195 	{ PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_AUDIGY },
196 	{ PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_AUDIGY2 },
197 };
198 
199 /*
200  * Autoconfig goo.
201  */
202 struct cfdriver emu_cd = {
203 	NULL, "emu", DV_DULL
204 };
205 
206 const struct cfattach emu_ca = {
207         sizeof(struct emuxki_softc),
208         emuxki_match,
209         emuxki_attach,
210 	emuxki_detach,
211 	emuxki_activate
212 };
213 
214 const struct audio_hw_if emuxki_hw_if = {
215 	.open = emuxki_open,
216 	.close = emuxki_close,
217 	.set_params = emuxki_set_params,
218 	.round_blocksize = emuxki_round_blocksize,
219 	.halt_output = emuxki_halt_output,
220 	.halt_input = emuxki_halt_input,
221 	.set_port = emuxki_set_port,
222 	.get_port = emuxki_get_port,
223 	.query_devinfo = emuxki_query_devinfo,
224 	.allocm = emuxki_allocm,
225 	.freem = emuxki_freem,
226 	.round_buffersize = emuxki_round_buffersize,
227 	.get_props = emuxki_get_props,
228 	.trigger_output = emuxki_trigger_output,
229 	.trigger_input = emuxki_trigger_input,
230 };
231 
232 #if 0
233 static const int emuxki_recsrc_intrmasks[EMU_NUMRECSRCS] =
234     { EMU_INTE_MICBUFENABLE, EMU_INTE_ADCBUFENABLE, EMU_INTE_EFXBUFENABLE };
235 #endif
236 static const u_int32_t emuxki_recsrc_bufaddrreg[EMU_NUMRECSRCS] =
237     { EMU_MICBA, EMU_ADCBA, EMU_FXBA };
238 static const u_int32_t emuxki_recsrc_szreg[EMU_NUMRECSRCS] =
239     { EMU_MICBS, EMU_ADCBS, EMU_FXBS };
240 static const int emuxki_recbuf_sz[] = {
241 	0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792,
242 	2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240,
243 	12288, 14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152,
244 	57344, 65536
245 };
246 
247 /*
248  * DMA memory mgmt
249  */
250 
251 void
252 emuxki_dmamem_delete(struct dmamem *mem, int type)
253 {
254 	free(mem->segs, type, 0);
255 	free(mem, type, 0);
256 }
257 
258 struct dmamem *
259 emuxki_dmamem_alloc(bus_dma_tag_t dmat, size_t size, bus_size_t align,
260 	     int nsegs, int type, int flags)
261 {
262 	struct dmamem	*mem;
263 	int		bus_dma_flags;
264 
265 	/* Allocate memory for structure */
266 	if ((mem = malloc(sizeof(*mem), type, flags)) == NULL)
267 		return (NULL);
268 	mem->dmat = dmat;
269 	mem->size = size;
270 	mem->align = align;
271 	mem->nsegs = nsegs;
272 	mem->bound = 0;
273 
274 	mem->segs = mallocarray(mem->nsegs, sizeof(*(mem->segs)), type, flags);
275 	if (mem->segs == NULL) {
276 		free(mem, type, 0);
277 		return (NULL);
278 	}
279 
280 	bus_dma_flags = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
281 	if (bus_dmamem_alloc(dmat, mem->size, mem->align, mem->bound,
282 			     mem->segs, mem->nsegs, &(mem->rsegs),
283 			     bus_dma_flags)) {
284 		emuxki_dmamem_delete(mem, type);
285 		return (NULL);
286 	}
287 
288 	if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, mem->size,
289 			   &(mem->kaddr), bus_dma_flags | BUS_DMA_COHERENT)) {
290 		bus_dmamem_free(dmat, mem->segs, mem->nsegs);
291 		emuxki_dmamem_delete(mem, type);
292 		return (NULL);
293 	}
294 
295 	if (bus_dmamap_create(dmat, mem->size, mem->nsegs, mem->size,
296 			      mem->bound, bus_dma_flags, &(mem->map))) {
297 		bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
298 		bus_dmamem_free(dmat, mem->segs, mem->nsegs);
299 		emuxki_dmamem_delete(mem, type);
300 		return (NULL);
301 	}
302 
303 	if (bus_dmamap_load(dmat, mem->map, mem->kaddr,
304 			    mem->size, NULL, bus_dma_flags)) {
305 		bus_dmamap_destroy(dmat, mem->map);
306 		bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
307 		bus_dmamem_free(dmat, mem->segs, mem->nsegs);
308 		emuxki_dmamem_delete(mem, type);
309 		return (NULL);
310 	}
311 
312 	return (mem);
313 }
314 
315 void
316 emuxki_dmamem_free(struct dmamem *mem, int type)
317 {
318 	bus_dmamap_unload(mem->dmat, mem->map);
319 	bus_dmamap_destroy(mem->dmat, mem->map);
320 	bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
321 	bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
322 	emuxki_dmamem_delete(mem, type);
323 }
324 
325 
326 /*
327  * Autoconf device callbacks : attach and detach
328  */
329 
330 void
331 emuxki_pci_shutdown(struct emuxki_softc *sc)
332 {
333 	if (sc->sc_ih != NULL)
334 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
335 	if (sc->sc_ios)
336 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
337 }
338 
339 int
340 emuxki_scinit(struct emuxki_softc *sc, int resuming)
341 {
342 	int             err;
343 
344 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
345 		/* enable spdif(?) output on non-APS */
346 		(sc->sc_flags & EMUXKI_APS? 0 : EMU_HCFG_GPOUTPUT0) |
347 		EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK |
348 		EMU_HCFG_MUTEBUTTONENABLE);
349 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
350 		EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE);
351 
352 	if ((err = emuxki_init(sc, resuming)))
353 		return (err);
354 
355 	if (sc->sc_flags & EMUXKI_AUDIGY2) {
356 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
357 			EMU_HCFG_AUDIOENABLE | EMU_HCFG_AC3ENABLE_CDSPDIF |
358 			EMU_HCFG_AC3ENABLE_GPSPDIF | EMU_HCFG_AUTOMUTE);
359 	} else if (sc->sc_flags & EMUXKI_AUDIGY) {
360 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
361 			EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE);
362 	} else {
363 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
364 			EMU_HCFG_AUDIOENABLE | EMU_HCFG_JOYENABLE |
365 			EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_AUTOMUTE);
366 	}
367 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
368 		bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
369 		EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE |
370 		EMU_INTE_MUTEENABLE);
371 
372 	if (sc->sc_flags & EMUXKI_AUDIGY2) {
373 		if (sc->sc_flags & EMUXKI_CA0108) {
374 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG,
375 			    0x0060 | bus_space_read_4(sc->sc_iot, sc->sc_ioh,
376 			    EMU_A_IOCFG));
377 		} else {
378 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG,
379 			    EMU_A_IOCFG_GPOUT0 | bus_space_read_4(sc->sc_iot,
380 			    sc->sc_ioh, EMU_A_IOCFG));
381 		}
382 	}
383 
384 	if (!resuming) {
385 		/* No multiple voice support for now */
386 		sc->pvoice = sc->rvoice = NULL;
387 	}
388 
389 	return (0);
390 }
391 
392 int
393 emuxki_ac97_init(struct emuxki_softc *sc)
394 {
395 	sc->hostif.arg = sc;
396 	sc->hostif.attach = emuxki_ac97_attach;
397 	sc->hostif.read = emuxki_ac97_read;
398 	sc->hostif.write = emuxki_ac97_write;
399 	sc->hostif.reset = emuxki_ac97_reset;
400 	sc->hostif.flags = NULL;
401 	return (ac97_attach(&(sc->hostif)));
402 }
403 
404 int
405 emuxki_match(struct device *parent, void *match, void *aux)
406 {
407 	return (pci_matchbyid((struct pci_attach_args *)aux, emuxki_devices,
408 	    nitems(emuxki_devices)));
409 }
410 
411 void
412 emuxki_attach(struct device *parent, struct device *self, void *aux)
413 {
414 	struct emuxki_softc *sc = (struct emuxki_softc *) self;
415 	struct pci_attach_args *pa = aux;
416 	pci_intr_handle_t ih;
417 	const char     *intrstr;
418 
419 	if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
420 	    &(sc->sc_iot), &(sc->sc_ioh), &(sc->sc_iob), &(sc->sc_ios), 0)) {
421 		printf(": can't map i/o space\n");
422 		return;
423 	}
424 
425 	sc->sc_pc   = pa->pa_pc;
426 	sc->sc_dmat = pa->pa_dmat;
427 
428 	if (pci_intr_map(pa, &ih)) {
429 		printf(": can't map interrupt\n");
430 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
431 		return;
432 	}
433 
434 	intrstr = pci_intr_string(pa->pa_pc, ih);
435 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO | IPL_MPSAFE,
436 	    emuxki_intr, sc, sc->sc_dev.dv_xname);
437 	if (sc->sc_ih == NULL) {
438 		printf(": can't establish interrupt");
439 		if (intrstr != NULL)
440 			printf(" at %s", intrstr);
441 		printf("\n");
442 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
443 		return;
444 	}
445 	printf(": %s\n", intrstr);
446 
447 	/* XXX it's unknown whether APS is made from Audigy as well */
448 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY) {
449 		sc->sc_flags |= EMUXKI_AUDIGY;
450                 if (PCI_REVISION(pa->pa_class) == 0x04 ||
451 		    PCI_REVISION(pa->pa_class) == 0x08) {
452 			sc->sc_flags |= EMUXKI_AUDIGY2;
453 		}
454 	} else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY2) {
455 		sc->sc_flags |= EMUXKI_AUDIGY | EMUXKI_AUDIGY2;
456 		if (pci_conf_read(pa->pa_pc, pa->pa_tag,
457 		    PCI_SUBSYS_ID_REG) == 0x10011102) {
458 			sc->sc_flags |= EMUXKI_CA0108;
459 		}
460 	} else if (pci_conf_read(pa->pa_pc, pa->pa_tag,
461 	    PCI_SUBSYS_ID_REG) == EMU_SUBSYS_APS) {
462 		sc->sc_flags |= EMUXKI_APS;
463 	} else {
464 		sc->sc_flags |= EMUXKI_SBLIVE;
465 	}
466 
467 	if (emuxki_scinit(sc, 0) ||
468 	    /* APS has no ac97 XXX */
469 	    (sc->sc_flags & EMUXKI_APS || emuxki_ac97_init(sc)) ||
470 	    (sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, NULL, self)) == NULL) {
471 		emuxki_pci_shutdown(sc);
472 		return;
473 	}
474 }
475 
476 int
477 emuxki_detach(struct device *self, int flags)
478 {
479 	struct emuxki_softc *sc = (struct emuxki_softc *) self;
480 
481         if (sc->sc_audev != NULL) /* Test in case audio didn't attach */
482 		config_detach(sc->sc_audev, 0);
483 
484 	/* All voices should be stopped now but add some code here if not */
485 
486 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
487 		EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK |
488 		EMU_HCFG_MUTEBUTTONENABLE);
489 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 0);
490 
491 	emuxki_shutdown(sc);
492 
493 	emuxki_pci_shutdown(sc);
494 
495 	return (0);
496 }
497 
498 int
499 emuxki_activate(struct device *self, int act)
500 {
501 	struct emuxki_softc *sc = (struct emuxki_softc *)self;
502 
503 	switch (act) {
504 	case DVACT_RESUME:
505 		emuxki_scinit(sc, 1);
506 		ac97_resume(&sc->hostif, sc->codecif);
507 		break;
508 	default:
509 		break;
510 	}
511 	return (config_activate_children(self, act));
512 }
513 
514 /* Misc stuff relative to emu10k1 */
515 
516 static u_int32_t
517 emuxki_rate_to_pitch(u_int32_t rate)
518 {
519 	static const u_int32_t logMagTable[128] = {
520 		0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3,
521 		0x13aa2, 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a,
522 		0x2655d, 0x28ed5, 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb,
523 		0x381b6, 0x3a93d, 0x3d081, 0x3f782, 0x41e42, 0x444c1, 0x46b01,
524 		0x49101, 0x4b6c4, 0x4dc49, 0x50191, 0x5269e, 0x54b6f, 0x57006,
525 		0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 0x646ee, 0x66a00,
526 		0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 0x759d4,
527 		0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
528 		0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20,
529 		0x93d26, 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec,
530 		0xa11d8, 0xa2f9d, 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241,
531 		0xadf26, 0xafbe7, 0xb1885, 0xb3500, 0xb5157, 0xb6d8c, 0xb899f,
532 		0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 0xc1404, 0xc2f50, 0xc4a7b,
533 		0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 0xceaec, 0xd053f,
534 		0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 0xdba4a,
535 		0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
536 		0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a,
537 		0xf2c83, 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57,
538 		0xfd1a7, 0xfe8df
539 	};
540 	static const u_int8_t logSlopeTable[128] = {
541 		0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
542 		0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
543 		0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
544 		0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
545 		0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
546 		0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
547 		0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
548 		0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
549 		0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
550 		0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
551 		0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
552 		0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
553 		0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
554 		0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
555 		0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
556 		0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
557 	};
558 	int8_t          i;
559 
560 	if (rate == 0)
561 		return 0;	/* Bail out if no leading "1" */
562 	rate *= 11185;		/* Scale 48000 to 0x20002380 */
563 	for (i = 31; i > 0; i--) {
564 		if (rate & 0x80000000) {	/* Detect leading "1" */
565 			return (((u_int32_t) (i - 15) << 20) +
566 				logMagTable[0x7f & (rate >> 24)] +
567 				(0x7f & (rate >> 17)) *
568 				logSlopeTable[0x7f & (rate >> 24)]);
569 		}
570 		rate <<= 1;
571 	}
572 
573 	return 0;		/* Should never reach this point */
574 }
575 
576 /* Emu10k1 Low level */
577 
578 static u_int32_t
579 emuxki_read(struct emuxki_softc *sc, u_int16_t chano, u_int32_t reg)
580 {
581 	u_int32_t       ptr, mask = 0xffffffff;
582 	u_int8_t        size, offset = 0;
583 
584 	ptr = ((((u_int32_t) reg) << 16) &
585 		(sc->sc_flags & EMUXKI_AUDIGY ?
586 			EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) |
587 		(chano & EMU_PTR_CHNO_MASK);
588 	if (reg & 0xff000000) {
589 		size = (reg >> 24) & 0x3f;
590 		offset = (reg >> 16) & 0x1f;
591 		mask = ((1 << size) - 1) << offset;
592 	}
593 
594 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr);
595 	ptr = (bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_DATA) & mask)
596 		>> offset;
597 	return (ptr);
598 }
599 
600 static void
601 emuxki_write(struct emuxki_softc *sc, u_int16_t chano,
602 	      u_int32_t reg, u_int32_t data)
603 {
604 	u_int32_t       ptr, mask;
605 	u_int8_t        size, offset;
606 
607 	ptr = ((((u_int32_t) reg) << 16) &
608 		(sc->sc_flags & EMUXKI_AUDIGY ?
609 			EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) |
610 		(chano & EMU_PTR_CHNO_MASK);
611 
612 	/* BE CAREFUL WITH THAT AXE, EUGENE */
613 	if (ptr == 0x52 || ptr == 0x53)
614 		return;
615 
616 	if (reg & 0xff000000) {
617 		size = (reg >> 24) & 0x3f;
618 		offset = (reg >> 16) & 0x1f;
619 		mask = ((1 << size) - 1) << offset;
620 		data = ((data << offset) & mask) |
621 			(emuxki_read(sc, chano, reg & 0xffff) & ~mask);
622 	}
623 
624 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr);
625 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_DATA, data);
626 }
627 
628 /* Microcode should this go in /sys/dev/microcode ? */
629 
630 void
631 emuxki_write_micro(struct emuxki_softc *sc, u_int32_t pc, u_int32_t data)
632 {
633 	emuxki_write(sc, 0,
634 		(sc->sc_flags & EMUXKI_AUDIGY ?
635 			EMU_A_MICROCODEBASE : EMU_MICROCODEBASE) + pc,
636 		 data);
637 }
638 
639 void
640 emuxki_dsp_addop(struct emuxki_softc *sc, u_int16_t *pc, u_int8_t op,
641 		  u_int16_t r, u_int16_t a, u_int16_t x, u_int16_t y)
642 {
643 	if (sc->sc_flags & EMUXKI_AUDIGY) {
644 		emuxki_write_micro(sc, *pc << 1,
645 			((x << 12) & EMU_A_DSP_LOWORD_OPX_MASK) |
646 			(y & EMU_A_DSP_LOWORD_OPY_MASK));
647 		emuxki_write_micro(sc, (*pc << 1) + 1,
648 			((op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK) |
649 			((r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK) |
650 			(a & EMU_A_DSP_HIWORD_OPA_MASK));
651 	} else {
652 		emuxki_write_micro(sc, *pc << 1,
653 			((x << 10) & EMU_DSP_LOWORD_OPX_MASK) |
654 			(y & EMU_DSP_LOWORD_OPY_MASK));
655 		emuxki_write_micro(sc, (*pc << 1) + 1,
656 			((op << 20) & EMU_DSP_HIWORD_OPCODE_MASK) |
657 			((r << 10) & EMU_DSP_HIWORD_RESULT_MASK) |
658 			(a & EMU_DSP_HIWORD_OPA_MASK));
659 	}
660 	(*pc)++;
661 }
662 
663 /* init and shutdown */
664 
665 void
666 emuxki_initfx(struct emuxki_softc *sc)
667 {
668 	u_int16_t       pc;
669 
670 	/* Set all GPRs to 0 */
671 	for (pc = 0; pc < 256; pc++)
672 		emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
673 	for (pc = 0; pc < 160; pc++) {
674 		emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
675 		emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
676 	}
677 	pc = 0;
678 
679 	if (sc->sc_flags & EMUXKI_AUDIGY) {
680 		/* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */
681 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
682 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT),
683 				  EMU_A_DSP_CST(0),
684 				  EMU_DSP_FX(0), EMU_A_DSP_CST(4));
685 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
686 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT),
687 				  EMU_A_DSP_CST(0),
688 				  EMU_DSP_FX(1), EMU_A_DSP_CST(4));
689 
690 		/* Rear channel OUT (l/r) = FX[2/3] * 4 */
691 #if 0
692 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
693 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_REAR),
694 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT),
695 				  EMU_DSP_FX(0), EMU_A_DSP_CST(4));
696 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
697 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_REAR),
698 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT),
699 				  EMU_DSP_FX(1), EMU_A_DSP_CST(4));
700 #endif
701 		/* ADC recording (l/r) = AC97 In (l/r) */
702 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
703 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_ADC),
704 				  EMU_A_DSP_INL(EMU_DSP_IN_AC97),
705 				  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
706 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
707 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_ADC),
708 				  EMU_A_DSP_INR(EMU_DSP_IN_AC97),
709 				  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
710 
711 		/* zero out the rest of the microcode */
712 		while (pc < 512)
713 			emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
714 					  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0),
715 					  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
716 
717 		emuxki_write(sc, 0, EMU_A_DBG, 0);	/* Is it really necessary ? */
718 	} else {
719 		/* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */
720 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
721 				  EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT),
722 				  EMU_DSP_CST(0),
723 				  EMU_DSP_FX(0), EMU_DSP_CST(4));
724 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
725 				  EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT),
726 				  EMU_DSP_CST(0),
727 				  EMU_DSP_FX(1), EMU_DSP_CST(4));
728 
729 		/* Rear channel OUT (l/r) = FX[2/3] * 4 */
730 #if 0
731 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
732 				  EMU_DSP_OUTL(EMU_DSP_OUT_AD_REAR),
733 				  EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT),
734 				  EMU_DSP_FX(0), EMU_DSP_CST(4));
735 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
736 				  EMU_DSP_OUTR(EMU_DSP_OUT_AD_REAR),
737 				  EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT),
738 				  EMU_DSP_FX(1), EMU_DSP_CST(4));
739 #endif
740 		/* ADC recording (l/r) = AC97 In (l/r) */
741 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
742 				  EMU_DSP_OUTL(EMU_DSP_OUT_ADC),
743 				  EMU_DSP_INL(EMU_DSP_IN_AC97),
744 				  EMU_DSP_CST(0), EMU_DSP_CST(0));
745 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
746 				  EMU_DSP_OUTR(EMU_DSP_OUT_ADC),
747 				  EMU_DSP_INR(EMU_DSP_IN_AC97),
748 				  EMU_DSP_CST(0), EMU_DSP_CST(0));
749 
750 		/* zero out the rest of the microcode */
751 		while (pc < 512)
752 			emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
753 					  EMU_DSP_CST(0), EMU_DSP_CST(0),
754 					  EMU_DSP_CST(0), EMU_DSP_CST(0));
755 
756 		emuxki_write(sc, 0, EMU_DBG, 0);	/* Is it really necessary ? */
757 	}
758 }
759 
760 int
761 emuxki_init(struct emuxki_softc *sc, int resuming)
762 {
763 	u_int16_t       i;
764 	u_int32_t       spcs, *ptb;
765 	bus_addr_t      silentpage;
766 
767 	/* disable any channel interrupt */
768 	emuxki_write(sc, 0, EMU_CLIEL, 0);
769 	emuxki_write(sc, 0, EMU_CLIEH, 0);
770 	emuxki_write(sc, 0, EMU_SOLEL, 0);
771 	emuxki_write(sc, 0, EMU_SOLEH, 0);
772 
773 	/* Set recording buffers sizes to zero */
774 	emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
775 	emuxki_write(sc, 0, EMU_MICBA, 0);
776 	emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
777 	emuxki_write(sc, 0, EMU_FXBA, 0);
778 	emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
779 	emuxki_write(sc, 0, EMU_ADCBA, 0);
780 
781         if (sc->sc_flags & EMUXKI_AUDIGY) {
782                 emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
783                 emuxki_write(sc, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
784         }
785 
786 	/* Initialize all channels to stopped and no effects */
787 	for (i = 0; i < EMU_NUMCHAN; i++) {
788 		emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0);
789 		emuxki_write(sc, i, EMU_CHAN_IP, 0);
790 		emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
791 		emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
792 		emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
793 		emuxki_write(sc, i, EMU_CHAN_CPF, 0);
794 		emuxki_write(sc, i, EMU_CHAN_CCR, 0);
795 		emuxki_write(sc, i, EMU_CHAN_PSST, 0);
796 		emuxki_write(sc, i, EMU_CHAN_DSL, 0x10);	/* Why 16 ? */
797 		emuxki_write(sc, i, EMU_CHAN_CCCA, 0);
798 		emuxki_write(sc, i, EMU_CHAN_Z1, 0);
799 		emuxki_write(sc, i, EMU_CHAN_Z2, 0);
800 		emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
801 		emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
802 		emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
803 		emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
804 		emuxki_write(sc, i, EMU_CHAN_PEFE, 0);
805 		emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
806 		emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 24);
807 		emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 24);
808 		emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
809 
810 		/* these are last so OFF prevents writing */
811 		emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0);
812 		emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0);
813 		emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0);
814 		emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
815 		emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0);
816 	}
817 
818 	/* set digital outputs format */
819 	spcs = (EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 |
820 	      EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC |
821 		EMU_SPCS_GENERATIONSTATUS | 0x00001200 /* Cat code. */ |
822 		0x00000000 /* IEC-958 Mode */ | EMU_SPCS_EMPHASIS_NONE |
823 		EMU_SPCS_COPYRIGHT);
824 	emuxki_write(sc, 0, EMU_SPCS0, spcs);
825 	emuxki_write(sc, 0, EMU_SPCS1, spcs);
826 	emuxki_write(sc, 0, EMU_SPCS2, spcs);
827 
828 	if (sc->sc_flags & EMUXKI_CA0108) {
829 		u_int32_t tmp;
830 
831 		/* Setup SRCMulti_I2S SamplingRate */
832 		tmp = emuxki_read(sc, 0, EMU_A_SPDIF_SAMPLERATE) & 0xfffff1ff;
833 		emuxki_write(sc, 0, EMU_A_SPDIF_SAMPLERATE, tmp | 0x400);
834 
835 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
836 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL);
837 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA,
838 		EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
839 
840 		/* Setup SRCMulti Input Audio Enable */
841 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, 0x7b0000);
842 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 0xff000000);
843 
844 		/* Setup SPDIF Out Audio Enable
845 		 * The Audigy 2 Value has a separate SPDIF out,
846 		 * so no need for a mixer switch */
847 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, 0x7a0000);
848 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 0xff000000);
849 		tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG) & ~0x8; /* Clear bit 3 */
850 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG, tmp);
851 	} else if(sc->sc_flags & EMUXKI_AUDIGY2) {
852                 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE, EMU_A2_SPDIF_UNKNOWN);
853 
854                 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL);
855                 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA,
856                         EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
857 
858                 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCMULTI);
859                 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, EMU_A2_SRCMULTI_ENABLE_INPUT);
860         }
861 
862 
863 	/* Let's play with sound processor */
864 	emuxki_initfx(sc);
865 
866 	if (!resuming) {
867 		/* Here is our Page Table */
868 		if ((sc->ptb = emuxki_dmamem_alloc(sc->sc_dmat,
869 		    EMU_MAXPTE * sizeof(u_int32_t),
870 		    EMU_DMA_ALIGN, EMU_DMAMEM_NSEG,
871 		    M_DEVBUF, M_WAITOK)) == NULL)
872 			return (ENOMEM);
873 
874 		/* This is necessary unless you like Metallic noise... */
875 		if ((sc->silentpage = emuxki_dmamem_alloc(sc->sc_dmat, EMU_PTESIZE,
876 		    EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, M_DEVBUF, M_WAITOK))==NULL){
877 			emuxki_dmamem_free(sc->ptb, M_DEVBUF);
878 			return (ENOMEM);
879 		}
880 
881 		/* Zero out the silent page */
882 		/* This might not be always true, it might be 128 for 8bit channels */
883 		memset(KERNADDR(sc->silentpage), 0, DMASIZE(sc->silentpage));
884 	}
885 
886 	/*
887 	 * Set all the PTB Entries to the silent page We shift the physical
888 	 * address by one and OR it with the page number. I don't know what
889 	 * the ORed index is for, might be a very useful unused feature...
890 	 */
891 	silentpage = DMAADDR(sc->silentpage) << 1;
892 	ptb = KERNADDR(sc->ptb);
893 	for (i = 0; i < EMU_MAXPTE; i++)
894 		ptb[i] = htole32(silentpage | i);
895 
896 	/* Write PTB address and set TCB to none */
897 	emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
898 	emuxki_write(sc, 0, EMU_TCBS, 0);	/* This means 16K TCB */
899 	emuxki_write(sc, 0, EMU_TCB, 0);	/* No TCB use for now */
900 
901 	/*
902 	 * Set channels MAPs to the silent page.
903 	 * I don't know what MAPs are for.
904 	 */
905 	silentpage |= EMU_CHAN_MAP_PTI_MASK;
906 	for (i = 0; i < EMU_NUMCHAN; i++) {
907 		emuxki_write(sc, i, EMU_CHAN_MAPA, silentpage);
908 		emuxki_write(sc, i, EMU_CHAN_MAPB, silentpage);
909 		sc->channel[i] = NULL;
910 	}
911 
912 	if (!resuming) {
913 		/* Init voices list */
914 		LIST_INIT(&(sc->voices));
915 	}
916 
917 	/* Timer is stopped */
918 	sc->timerstate &= ~EMU_TIMER_STATE_ENABLED;
919 	return (0);
920 }
921 
922 void
923 emuxki_shutdown(struct emuxki_softc *sc)
924 {
925 	u_int32_t       i;
926 
927 	/* Disable any Channels interrupts */
928 	emuxki_write(sc, 0, EMU_CLIEL, 0);
929 	emuxki_write(sc, 0, EMU_CLIEH, 0);
930 	emuxki_write(sc, 0, EMU_SOLEL, 0);
931 	emuxki_write(sc, 0, EMU_SOLEH, 0);
932 
933 	/*
934 	 * Should do some voice(stream) stopping stuff here, that's what will
935 	 * stop and deallocate all channels.
936 	 */
937 
938 	/* Stop all channels */
939 	/* XXX This shouldn't be necessary, I'll remove once everything works */
940 	for (i = 0; i < EMU_NUMCHAN; i++)
941 		emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0);
942 	for (i = 0; i < EMU_NUMCHAN; i++) {
943 		emuxki_write(sc, i, EMU_CHAN_VTFT, 0);
944 		emuxki_write(sc, i, EMU_CHAN_CVCF, 0);
945 		emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
946 		emuxki_write(sc, i, EMU_CHAN_CPF, 0);
947 	}
948 
949 	/*
950 	 * Deallocate Emu10k1 caches and recording buffers. Again it will be
951 	 * removed because it will be done in voice shutdown.
952 	 */
953 	emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
954 	emuxki_write(sc, 0, EMU_MICBA, 0);
955 	emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
956 	emuxki_write(sc, 0, EMU_FXBA, 0);
957 	if (sc->sc_flags & EMUXKI_AUDIGY) {
958 		emuxki_write(sc, 0, EMU_A_FXWC1, 0);
959 		emuxki_write(sc, 0, EMU_A_FXWC2, 0);
960 	} else
961 		emuxki_write(sc, 0, EMU_FXWC, 0);
962 	emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
963 	emuxki_write(sc, 0, EMU_ADCBA, 0);
964 
965 	/*
966 	 * XXX I don't know yet how I will handle tank cache buffer,
967 	 * I don't even clearly  know what it is for.
968 	 */
969 	emuxki_write(sc, 0, EMU_TCB, 0);	/* 16K again */
970 	emuxki_write(sc, 0, EMU_TCBS, 0);
971 
972 	emuxki_write(sc, 0, EMU_DBG, 0x8000);	/* necessary ? */
973 
974 	emuxki_dmamem_free(sc->silentpage, M_DEVBUF);
975 	emuxki_dmamem_free(sc->ptb, M_DEVBUF);
976 }
977 
978 /* Emu10k1 Memory management */
979 
980 struct emuxki_mem *
981 emuxki_mem_new(struct emuxki_softc *sc, int ptbidx,
982 		size_t size, int type, int flags)
983 {
984 	struct emuxki_mem *mem;
985 
986 	if ((mem = malloc(sizeof(*mem), type, flags)) == NULL)
987 		return (NULL);
988 
989 	mem->ptbidx = ptbidx;
990 	if ((mem->dmamem = emuxki_dmamem_alloc(sc->sc_dmat, size,
991 	    EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, type, flags)) == NULL) {
992 		free(mem, type, 0);
993 		return (NULL);
994 	}
995 	return (mem);
996 }
997 
998 void
999 emuxki_mem_delete(struct emuxki_mem *mem, int type)
1000 {
1001 	emuxki_dmamem_free(mem->dmamem, type);
1002 	free(mem, type, 0);
1003 }
1004 
1005 void *
1006 emuxki_pmem_alloc(struct emuxki_softc *sc, size_t size, int type, int flags)
1007 {
1008 	int             i, j;
1009 	size_t          numblocks;
1010 	struct emuxki_mem *mem;
1011 	u_int32_t      *ptb, silentpage;
1012 
1013 	ptb = KERNADDR(sc->ptb);
1014 	silentpage = DMAADDR(sc->silentpage) << 1;
1015 	numblocks = size / EMU_PTESIZE;
1016 	if (size % EMU_PTESIZE)
1017 		numblocks++;
1018 
1019 	for (i = 0; i < EMU_MAXPTE; i++)
1020 		if ((letoh32(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) {
1021 			/* We look for a free PTE */
1022 			for (j = 0; j < numblocks; j++)
1023 				if ((letoh32(ptb[i + j])
1024 				    & EMU_CHAN_MAP_PTE_MASK)
1025 				    != silentpage)
1026 					break;
1027 			if (j == numblocks) {
1028 				if ((mem = emuxki_mem_new(sc, i,
1029 						size, type, flags)) == NULL) {
1030 					return (NULL);
1031 				}
1032 				for (j = 0; j < numblocks; j++)
1033 					ptb[i + j] =
1034 					    htole32((((DMAADDR(mem->dmamem) +
1035 					    j * EMU_PTESIZE)) << 1) | (i + j));
1036 				mtx_enter(&audio_lock);
1037 				LIST_INSERT_HEAD(&(sc->mem), mem, next);
1038 				mtx_leave(&audio_lock);
1039 				return (KERNADDR(mem->dmamem));
1040 			} else
1041 				i += j;
1042 		}
1043 	return (NULL);
1044 }
1045 
1046 void *
1047 emuxki_rmem_alloc(struct emuxki_softc *sc, size_t size, int type, int flags)
1048 {
1049 	struct emuxki_mem *mem;
1050 
1051 	mem = emuxki_mem_new(sc, EMU_RMEM, size, type, flags);
1052 	if (mem == NULL)
1053 		return (NULL);
1054 
1055 	mtx_enter(&audio_lock);
1056 	LIST_INSERT_HEAD(&(sc->mem), mem, next);
1057 	mtx_leave(&audio_lock);
1058 
1059 	return (KERNADDR(mem->dmamem));
1060 }
1061 
1062 /*
1063  * emuxki_channel_* : Channel management functions
1064  * emuxki_chanparms_* : Channel parameters modification functions
1065  */
1066 
1067 /*
1068  * is splaudio necessary here, can the same voice be manipulated by two
1069  * different threads at a time ?
1070  */
1071 void
1072 emuxki_chanparms_set_defaults(struct emuxki_channel *chan)
1073 {
1074 	chan->fxsend.a.level = chan->fxsend.b.level =
1075 	chan->fxsend.c.level = chan->fxsend.d.level =
1076 	/* for audigy */
1077 	chan->fxsend.e.level = chan->fxsend.f.level =
1078 	chan->fxsend.g.level = chan->fxsend.h.level =
1079 		chan->voice->sc->sc_flags & EMUXKI_AUDIGY ?
1080 			0xc0 : 0xff;	/* not max */
1081 
1082 	chan->fxsend.a.dest = 0x0;
1083 	chan->fxsend.b.dest = 0x1;
1084 	chan->fxsend.c.dest = 0x2;
1085 	chan->fxsend.d.dest = 0x3;
1086 	/* for audigy */
1087 	chan->fxsend.e.dest = 0x4;
1088 	chan->fxsend.f.dest = 0x5;
1089 	chan->fxsend.g.dest = 0x6;
1090 	chan->fxsend.h.dest = 0x7;
1091 
1092 	chan->pitch.initial = 0x0000;	/* shouldn't it be 0xE000 ? */
1093 	chan->pitch.current = 0x0000;	/* should it be 0x0400 */
1094 	chan->pitch.target = 0x0000;	/* the unity pitch shift ? */
1095 	chan->pitch.envelope_amount = 0x00;	/* none */
1096 
1097 	chan->initial_attenuation = 0x00;	/* no attenuation */
1098 	chan->volume.current = 0x0000;	/* no volume */
1099 	chan->volume.target = 0xffff;
1100 	chan->volume.envelope.current_state = 0x8000;	/* 0 msec delay */
1101 	chan->volume.envelope.hold_time = 0x7f;	/* 0 msec */
1102 	chan->volume.envelope.attack_time = 0x7F;	/* 5.5msec */
1103 	chan->volume.envelope.sustain_level = 0x7F;	/* full  */
1104 	chan->volume.envelope.decay_time = 0x7F;	/* 22msec  */
1105 
1106 	chan->filter.initial_cutoff_frequency = 0xff;	/* no filter */
1107 	chan->filter.current_cutoff_frequency = 0xffff;	/* no filtering */
1108 	chan->filter.target_cutoff_frequency = 0xffff;	/* no filtering */
1109 	chan->filter.lowpass_resonance_height = 0x0;
1110 	chan->filter.interpolation_ROM = 0x1;	/* full band */
1111 	chan->filter.envelope_amount = 0x7f;	/* none */
1112 	chan->filter.LFO_modulation_depth = 0x00;	/* none */
1113 
1114 	chan->loop.start = 0x000000;
1115 	chan->loop.end = 0x000010;	/* Why ? */
1116 
1117 	chan->modulation.envelope.current_state = 0x8000;
1118 	chan->modulation.envelope.hold_time = 0x00;	/* 127 better ? */
1119 	chan->modulation.envelope.attack_time = 0x00;	/* infinite */
1120 	chan->modulation.envelope.sustain_level = 0x00;	/* off */
1121 	chan->modulation.envelope.decay_time = 0x7f;	/* 22 msec */
1122 	chan->modulation.LFO_state = 0x8000;
1123 
1124 	chan->vibrato_LFO.state = 0x8000;
1125 	chan->vibrato_LFO.modulation_depth = 0x00;	/* none */
1126 	chan->vibrato_LFO.vibrato_depth = 0x00;
1127 	chan->vibrato_LFO.frequency = 0x00;	/* Why set to 24 when
1128 						 * initialized ? */
1129 
1130 	chan->tremolo_depth = 0x00;
1131 }
1132 
1133 /* only call it at splaudio */
1134 struct emuxki_channel *
1135 emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num)
1136 {
1137 	struct emuxki_channel *chan;
1138 
1139 	chan = malloc(sizeof(struct emuxki_channel), M_DEVBUF,
1140 	    M_WAITOK | M_CANFAIL);
1141 	if (chan == NULL)
1142 		return (NULL);
1143 
1144 	chan->voice = voice;
1145 	chan->num = num;
1146 	emuxki_chanparms_set_defaults(chan);
1147 	chan->voice->sc->channel[num] = chan;
1148 	return (chan);
1149 }
1150 
1151 /* only call it at splaudio */
1152 void
1153 emuxki_channel_delete(struct emuxki_channel *chan)
1154 {
1155 	chan->voice->sc->channel[chan->num] = NULL;
1156 	free(chan, M_DEVBUF, 0);
1157 }
1158 
1159 void
1160 emuxki_channel_set_fxsend(struct emuxki_channel *chan,
1161 			   struct emuxki_chanparms_fxsend *fxsend)
1162 {
1163 	/* Could do a memcpy ...*/
1164 	chan->fxsend.a.level = fxsend->a.level;
1165 	chan->fxsend.b.level = fxsend->b.level;
1166 	chan->fxsend.c.level = fxsend->c.level;
1167 	chan->fxsend.d.level = fxsend->d.level;
1168 	chan->fxsend.a.dest = fxsend->a.dest;
1169 	chan->fxsend.b.dest = fxsend->b.dest;
1170 	chan->fxsend.c.dest = fxsend->c.dest;
1171 	chan->fxsend.d.dest = fxsend->d.dest;
1172 
1173 	/* for audigy */
1174 	chan->fxsend.e.level = fxsend->e.level;
1175 	chan->fxsend.f.level = fxsend->f.level;
1176 	chan->fxsend.g.level = fxsend->g.level;
1177 	chan->fxsend.h.level = fxsend->h.level;
1178 	chan->fxsend.e.dest = fxsend->e.dest;
1179 	chan->fxsend.f.dest = fxsend->f.dest;
1180 	chan->fxsend.g.dest = fxsend->g.dest;
1181 	chan->fxsend.h.dest = fxsend->h.dest;
1182 }
1183 
1184 void
1185 emuxki_channel_set_srate(struct emuxki_channel *chan, u_int32_t srate)
1186 {
1187 	chan->pitch.target = (srate << 8) / 375;
1188 	chan->pitch.target = (chan->pitch.target >> 1) +
1189 		(chan->pitch.target & 1);
1190 	chan->pitch.target &= 0xffff;
1191 	chan->pitch.current = chan->pitch.target;
1192 	chan->pitch.initial =
1193 		(emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK;
1194 }
1195 
1196 /* voice params must be set before calling this */
1197 void
1198 emuxki_channel_set_bufparms(struct emuxki_channel *chan,
1199 			     u_int32_t start, u_int32_t end)
1200 {
1201 	chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK;
1202 	chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK;
1203 }
1204 
1205 void
1206 emuxki_channel_commit_fx(struct emuxki_channel *chan)
1207 {
1208 	struct emuxki_softc *sc = chan->voice->sc;
1209         u_int8_t	chano = chan->num;
1210 
1211         if (sc->sc_flags & EMUXKI_AUDIGY) {
1212                 emuxki_write(sc, chano, EMU_A_CHAN_FXRT1,
1213                               (chan->fxsend.d.dest << 24) |
1214                               (chan->fxsend.c.dest << 16) |
1215                               (chan->fxsend.b.dest << 8) |
1216                               (chan->fxsend.a.dest));
1217                 emuxki_write(sc, chano, EMU_A_CHAN_FXRT2,
1218                               (chan->fxsend.h.dest << 24) |
1219                               (chan->fxsend.g.dest << 16) |
1220                               (chan->fxsend.f.dest << 8) |
1221                               (chan->fxsend.e.dest));
1222                 emuxki_write(sc, chano, EMU_A_CHAN_SENDAMOUNTS,
1223                               (chan->fxsend.e.level << 24) |
1224                               (chan->fxsend.f.level << 16) |
1225                               (chan->fxsend.g.level << 8) |
1226                               (chan->fxsend.h.level));
1227         } else {
1228                 emuxki_write(sc, chano, EMU_CHAN_FXRT,
1229                               (chan->fxsend.d.dest << 28) |
1230                               (chan->fxsend.c.dest << 24) |
1231                               (chan->fxsend.b.dest << 20) |
1232                               (chan->fxsend.a.dest << 16));
1233         }
1234 
1235         emuxki_write(sc, chano, 0x10000000 | EMU_CHAN_PTRX,
1236                       (chan->fxsend.a.level << 8) | chan->fxsend.b.level);
1237         emuxki_write(sc, chano, EMU_CHAN_DSL,
1238                       (chan->fxsend.d.level << 24) | chan->loop.end);
1239         emuxki_write(sc, chano, EMU_CHAN_PSST,
1240                       (chan->fxsend.c.level << 24) | chan->loop.start);
1241 }
1242 
1243 void
1244 emuxki_channel_commit_parms(struct emuxki_channel *chan)
1245 {
1246 	struct emuxki_voice *voice = chan->voice;
1247 	struct emuxki_softc *sc = voice->sc;
1248 	u_int32_t start, mapval;
1249 	u_int8_t chano = chan->num;
1250 
1251 	start = chan->loop.start +
1252 		(voice->stereo ? 28 : 30) * (voice->b16 + 1);
1253 	mapval = DMAADDR(sc->silentpage) << 1 | EMU_CHAN_MAP_PTI_MASK;
1254 
1255 	mtx_enter(&audio_lock);
1256 	emuxki_write(sc, chano, EMU_CHAN_CPF_STEREO, voice->stereo);
1257 
1258 	emuxki_channel_commit_fx(chan);
1259 
1260 	emuxki_write(sc, chano, EMU_CHAN_CCCA,
1261 		(chan->filter.lowpass_resonance_height << 28) |
1262 		(chan->filter.interpolation_ROM << 25) |
1263 		(voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start);
1264 	emuxki_write(sc, chano, EMU_CHAN_Z1, 0);
1265 	emuxki_write(sc, chano, EMU_CHAN_Z2, 0);
1266 	emuxki_write(sc, chano, EMU_CHAN_MAPA, mapval);
1267 	emuxki_write(sc, chano, EMU_CHAN_MAPB, mapval);
1268 	emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRFILTER,
1269 		chan->filter.current_cutoff_frequency);
1270 	emuxki_write(sc, chano, EMU_CHAN_VTFT_FILTERTARGET,
1271 		chan->filter.target_cutoff_frequency);
1272 	emuxki_write(sc, chano, EMU_CHAN_ATKHLDM,
1273 		(chan->modulation.envelope.hold_time << 8) |
1274 		chan->modulation.envelope.attack_time);
1275 	emuxki_write(sc, chano, EMU_CHAN_DCYSUSM,
1276 		(chan->modulation.envelope.sustain_level << 8) |
1277 		chan->modulation.envelope.decay_time);
1278 	emuxki_write(sc, chano, EMU_CHAN_LFOVAL1,
1279 		chan->modulation.LFO_state);
1280 	emuxki_write(sc, chano, EMU_CHAN_LFOVAL2,
1281 		chan->vibrato_LFO.state);
1282 	emuxki_write(sc, chano, EMU_CHAN_FMMOD,
1283 		(chan->vibrato_LFO.modulation_depth << 8) |
1284 		chan->filter.LFO_modulation_depth);
1285 	emuxki_write(sc, chano, EMU_CHAN_TREMFRQ,
1286 		(chan->tremolo_depth << 8));
1287 	emuxki_write(sc, chano, EMU_CHAN_FM2FRQ2,
1288 		(chan->vibrato_LFO.vibrato_depth << 8) |
1289 		chan->vibrato_LFO.frequency);
1290 	emuxki_write(sc, chano, EMU_CHAN_ENVVAL,
1291 		chan->modulation.envelope.current_state);
1292 	emuxki_write(sc, chano, EMU_CHAN_ATKHLDV,
1293 		(chan->volume.envelope.hold_time << 8) |
1294 		chan->volume.envelope.attack_time);
1295 	emuxki_write(sc, chano, EMU_CHAN_ENVVOL,
1296 		chan->volume.envelope.current_state);
1297 	emuxki_write(sc, chano, EMU_CHAN_PEFE,
1298 		(chan->pitch.envelope_amount << 8) |
1299 		chan->filter.envelope_amount);
1300 	mtx_leave(&audio_lock);
1301 }
1302 
1303 void
1304 emuxki_channel_start(struct emuxki_channel *chan)
1305 {
1306 	struct emuxki_voice *voice = chan->voice;
1307 	struct emuxki_softc *sc = voice->sc;
1308 	u_int8_t        cache_sample, cache_invalid_size, chano = chan->num;
1309 	u_int32_t       sample;
1310 
1311 	cache_sample = voice->stereo ? 4 : 2;
1312 	sample = voice->b16 ? 0x00000000 : 0x80808080;
1313 	cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1);
1314 
1315 	while (cache_sample--) {
1316 		emuxki_write(sc, chano, EMU_CHAN_CD0 + cache_sample,
1317 			sample);
1318 	}
1319 	emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
1320 	emuxki_write(sc, chano, EMU_CHAN_CCR_READADDRESS, 64);
1321 	emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE,
1322 		cache_invalid_size);
1323 	emuxki_write(sc, chano, EMU_CHAN_IFATN,
1324 		(chan->filter.target_cutoff_frequency << 8) |
1325 		chan->initial_attenuation);
1326 	emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET,
1327 		chan->volume.target);
1328 	emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL,
1329 		chan->volume.current);
1330 	emuxki_write(sc, 0,
1331 		EMU_MKSUBREG(1, chano, EMU_SOLEL + (chano >> 5)),
1332 		0);	/* Clear stop on loop */
1333 	emuxki_write(sc, 0,
1334 		EMU_MKSUBREG(1, chano, EMU_CLIEL + (chano >> 5)),
1335 		0);	/* Clear loop interrupt */
1336 	emuxki_write(sc, chano, EMU_CHAN_DCYSUSV,
1337 		(chan->volume.envelope.sustain_level << 8) |
1338 		chan->volume.envelope.decay_time);
1339 	emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET,
1340 		chan->pitch.target);
1341 	emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH,
1342 		chan->pitch.current);
1343 	emuxki_write(sc, chano, EMU_CHAN_IP, chan->pitch.initial);
1344 }
1345 
1346 void
1347 emuxki_channel_stop(struct emuxki_channel *chan)
1348 {
1349 	u_int8_t chano = chan->num;
1350 	struct emuxki_softc *sc = chan->voice->sc;
1351 
1352 	emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 0);
1353 	emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 0);
1354 	emuxki_write(sc, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff);
1355 	emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0);
1356 	emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 0);
1357 	emuxki_write(sc, chano, EMU_CHAN_IP, 0);
1358 }
1359 
1360 /*
1361  * Voices management
1362  * emuxki_voice_dataloc : use(play or rec) independent dataloc union helpers
1363  * emuxki_voice_channel_* : play part of dataloc union helpers
1364  * emuxki_voice_recsrc_* : rec part of dataloc union helpers
1365  */
1366 
1367 /* Allocate channels for voice in case of play voice */
1368 int
1369 emuxki_voice_channel_create(struct emuxki_voice *voice)
1370 {
1371 	struct emuxki_channel **channel = voice->sc->channel;
1372 	u_int8_t i, stereo = voice->stereo;
1373 
1374 	for (i = 0; i < EMU_NUMCHAN; i += stereo + 1) {
1375 		if ((stereo && (channel[i + 1] != NULL)) ||
1376 		    (channel[i] != NULL))	/* Looking for free channels */
1377 			continue;
1378 		if (stereo) {
1379 			voice->dataloc.chan[1] =
1380 				emuxki_channel_new(voice, i + 1);
1381 			if (voice->dataloc.chan[1] == NULL) {
1382 				return (ENOMEM);
1383 			}
1384 		}
1385 		voice->dataloc.chan[0] = emuxki_channel_new(voice, i);
1386 		if (voice->dataloc.chan[0] == NULL) {
1387 			if (stereo) {
1388 				emuxki_channel_delete(voice->dataloc.chan[1]);
1389 				voice->dataloc.chan[1] = NULL;
1390 			}
1391 			return (ENOMEM);
1392 		}
1393 		return (0);
1394 	}
1395 	return (EAGAIN);
1396 }
1397 
1398 /* When calling this function we assume no one can access the voice */
1399 void
1400 emuxki_voice_channel_destroy(struct emuxki_voice *voice)
1401 {
1402 	emuxki_channel_delete(voice->dataloc.chan[0]);
1403 	voice->dataloc.chan[0] = NULL;
1404 	if (voice->stereo)
1405 		emuxki_channel_delete(voice->dataloc.chan[1]);
1406 	voice->dataloc.chan[1] = NULL;
1407 }
1408 
1409 /*
1410  * Will come back when used in voice_dataloc_create
1411  */
1412 int
1413 emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source)
1414 {
1415 	if (source >= EMU_NUMRECSRCS) {
1416 #ifdef EMUXKI_DEBUG
1417 		printf("Tried to reserve invalid source: %d\n", source);
1418 #endif
1419 		return (EINVAL);
1420 	}
1421 	if (voice->sc->recsrc[source] == voice)
1422 		return (0);			/* XXX */
1423 	if (voice->sc->recsrc[source] != NULL)
1424 		return (EBUSY);
1425 	voice->sc->recsrc[source] = voice;
1426 	return (0);
1427 }
1428 
1429 /* When calling this function we assume the voice is stopped */
1430 void
1431 emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source)
1432 {
1433 	sc->recsrc[source] = NULL;
1434 }
1435 
1436 int
1437 emuxki_voice_dataloc_create(struct emuxki_voice *voice)
1438 {
1439 	int             error;
1440 
1441 	if (voice->use & EMU_VOICE_USE_PLAY) {
1442 		if ((error = emuxki_voice_channel_create(voice)))
1443 			return (error);
1444 	} else {
1445 		if ((error =
1446 		    emuxki_recsrc_reserve(voice, voice->dataloc.source)))
1447 			return (error);
1448 	}
1449 	return (0);
1450 }
1451 
1452 void
1453 emuxki_voice_dataloc_destroy(struct emuxki_voice *voice)
1454 {
1455 	if (voice->use & EMU_VOICE_USE_PLAY) {
1456 		if (voice->dataloc.chan[0] != NULL)
1457 			emuxki_voice_channel_destroy(voice);
1458 	} else {
1459 		if (voice->dataloc.source != EMU_RECSRC_NOTSET) {
1460 			emuxki_voice_recsrc_release(voice->sc,
1461 						     voice->dataloc.source);
1462 			voice->dataloc.source = EMU_RECSRC_NOTSET;
1463 		}
1464 	}
1465 }
1466 
1467 struct emuxki_voice *
1468 emuxki_voice_new(struct emuxki_softc *sc, u_int8_t use)
1469 {
1470 	struct emuxki_voice *voice;
1471 
1472 	mtx_enter(&audio_lock);
1473 	voice = sc->lvoice;
1474 	sc->lvoice = NULL;
1475 	mtx_leave(&audio_lock);
1476 
1477 	if (!voice) {
1478 		if (!(voice = malloc(sizeof(*voice), M_DEVBUF, M_WAITOK)))
1479 			return (NULL);
1480 	} else if (voice->use != use)
1481 		emuxki_voice_dataloc_destroy(voice);
1482 	else
1483 		goto skip_initialize;
1484 
1485 	voice->sc = sc;
1486 	voice->state = 0;
1487 	voice->stereo = EMU_VOICE_STEREO_NOTSET;
1488 	voice->b16 = 0;
1489 	voice->sample_rate = 0;
1490 	if (use & EMU_VOICE_USE_PLAY)
1491 		voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL;
1492 	else
1493 		voice->dataloc.source = EMU_RECSRC_NOTSET;
1494 	voice->buffer = NULL;
1495 	voice->blksize = 0;
1496 	voice->trigblk = 0;
1497 	voice->blkmod = 0;
1498 	voice->inth = NULL;
1499 	voice->inthparam = NULL;
1500 	voice->use = use;
1501 
1502 skip_initialize:
1503 	mtx_enter(&audio_lock);
1504 	LIST_INSERT_HEAD((&sc->voices), voice, next);
1505 	mtx_leave(&audio_lock);
1506 
1507 	return (voice);
1508 }
1509 
1510 void
1511 emuxki_voice_delete(struct emuxki_voice *voice)
1512 {
1513 	struct emuxki_softc *sc = voice->sc;
1514 	struct emuxki_voice *lvoice;
1515 
1516 	if (voice->state & EMU_VOICE_STATE_STARTED)
1517 		emuxki_voice_halt(voice);
1518 
1519 	mtx_enter(&audio_lock);
1520 	LIST_REMOVE(voice, next);
1521 	lvoice = sc->lvoice;
1522 	sc->lvoice = voice;
1523 	mtx_leave(&audio_lock);
1524 
1525 	if (lvoice) {
1526 		emuxki_voice_dataloc_destroy(lvoice);
1527 		free(lvoice, M_DEVBUF, 0);
1528 	}
1529 }
1530 
1531 int
1532 emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo)
1533 {
1534 	int	error;
1535 	emuxki_recsrc_t source = 0; /* XXX: gcc */
1536 	struct emuxki_chanparms_fxsend fxsend;
1537 
1538 	if (! (voice->use & EMU_VOICE_USE_PLAY))
1539 		source = voice->dataloc.source;
1540 	emuxki_voice_dataloc_destroy(voice);
1541 	if (! (voice->use & EMU_VOICE_USE_PLAY))
1542 		voice->dataloc.source = source;
1543 	voice->stereo = stereo;
1544 	if ((error = emuxki_voice_dataloc_create(voice)))
1545 	  return (error);
1546 	if (voice->use & EMU_VOICE_USE_PLAY) {
1547 		fxsend.a.dest = 0x0;
1548 		fxsend.b.dest = 0x1;
1549 		fxsend.c.dest = 0x2;
1550 		fxsend.d.dest = 0x3;
1551 		/* for audigy */
1552 		fxsend.e.dest = 0x4;
1553 		fxsend.f.dest = 0x5;
1554 		fxsend.g.dest = 0x6;
1555 		fxsend.h.dest = 0x7;
1556 		if (voice->stereo) {
1557 			fxsend.a.level = fxsend.c.level = 0xc0;
1558 			fxsend.b.level = fxsend.d.level = 0x00;
1559 			fxsend.e.level = fxsend.g.level = 0xc0;
1560 			fxsend.f.level = fxsend.h.level = 0x00;
1561 			emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1562 						   &fxsend);
1563 			fxsend.a.level = fxsend.c.level = 0x00;
1564 			fxsend.b.level = fxsend.d.level = 0xc0;
1565 			fxsend.e.level = fxsend.g.level = 0x00;
1566 			fxsend.f.level = fxsend.h.level = 0xc0;
1567 			emuxki_channel_set_fxsend(voice->dataloc.chan[1],
1568 						   &fxsend);
1569 		} /* No else : default is good for mono */
1570 	}
1571 	return (0);
1572 }
1573 
1574 int
1575 emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate)
1576 {
1577 	if (voice->use & EMU_VOICE_USE_PLAY) {
1578 		if (srate < 4000)
1579 			srate = 4000;
1580 		if (srate > 48000)
1581 			srate = 48000;
1582 		voice->sample_rate = srate;
1583 		emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1584 		if (voice->stereo)
1585 			emuxki_channel_set_srate(voice->dataloc.chan[1],
1586 						  srate);
1587 	} else {
1588 		if (srate < 8000)
1589 			srate = 8000;
1590 		if (srate > 48000)
1591 			srate = 48000;
1592 		voice->sample_rate = srate;
1593 		if (emuxki_voice_adc_rate(voice) < 0) {
1594 			voice->sample_rate = 0;
1595 			return (EINVAL);
1596 		}
1597 	}
1598 	return (0);
1599 }
1600 
1601 int
1602 emuxki_voice_set_audioparms(struct emuxki_voice *voice, u_int8_t stereo,
1603 			     u_int8_t b16, u_int32_t srate)
1604 {
1605 	int             error = 0;
1606 
1607 	/*
1608 	 * Audio driver tried to set recording AND playing params even if
1609 	 * device opened in play or record only mode ==>
1610 	 * modified emuxki_set_params.
1611 	 * Stays here for now just in case ...
1612 	 */
1613 	if (voice == NULL) {
1614 #ifdef EMUXKI_DEBUG
1615 		printf("warning: tried to set unallocated voice params !!\n");
1616 #endif
1617 		return (0);
1618 	}
1619 
1620 	if (voice->stereo == stereo && voice->b16 == b16 &&
1621 	    voice->sample_rate == srate)
1622 		return (0);
1623 
1624 #ifdef EMUXKI_DEBUG
1625 	printf("Setting %s voice params : %s, %u bits, %u hz\n",
1626 	       (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record",
1627 	       stereo ? "stereo" : "mono", (b16 + 1) * 8, srate);
1628 #endif
1629 
1630 	voice->b16 = b16;
1631 
1632 	/* sample rate must be set after any channel number changes */
1633 	if ((voice->stereo != stereo) || (voice->sample_rate != srate)) {
1634 		if (voice->stereo != stereo) {
1635 			if ((error = emuxki_voice_set_stereo(voice, stereo)))
1636 				return (error);
1637 		}
1638 		error = emuxki_voice_set_srate(voice, srate);
1639 	}
1640 	return error;
1641 }
1642 
1643 /* voice audio parms (see just before) must be set prior to this */
1644 int
1645 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr,
1646 			   u_int32_t bufsize, u_int16_t blksize)
1647 {
1648 	struct emuxki_mem *mem;
1649 	struct emuxki_channel **chan;
1650 	u_int32_t start, end;
1651 	u_int8_t sample_size;
1652 	int idx;
1653 	int error = EFAULT;
1654 
1655 	LIST_FOREACH(mem, &voice->sc->mem, next) {
1656 		if (KERNADDR(mem->dmamem) != ptr)
1657 			continue;
1658 
1659 		voice->buffer = mem;
1660 		sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1661 		voice->trigblk = 0;	/* This shouldn't be needed */
1662 		voice->blkmod = bufsize / blksize;
1663 		if (bufsize % blksize) 	  /* This should not happen */
1664 			voice->blkmod++;
1665 		error = 0;
1666 
1667 		if (voice->use & EMU_VOICE_USE_PLAY) {
1668 			voice->blksize = blksize / sample_size;
1669 			chan = voice->dataloc.chan;
1670 			start = (mem->ptbidx << 12) / sample_size;
1671 			end = start + bufsize / sample_size;
1672 			emuxki_channel_set_bufparms(chan[0],
1673 						     start, end);
1674 			if (voice->stereo)
1675 				emuxki_channel_set_bufparms(chan[1],
1676 				     start, end);
1677 			voice->timerate = (u_int32_t) 48000 *
1678 			                voice->blksize / voice->sample_rate;
1679 			if (voice->timerate < 5)
1680 				error = EINVAL;
1681 		} else {
1682 			voice->blksize = blksize;
1683 			for(idx = sizeof(emuxki_recbuf_sz) /
1684 			    sizeof(emuxki_recbuf_sz[0]); --idx >= 0;)
1685 				if (emuxki_recbuf_sz[idx] == bufsize)
1686 					break;
1687 			if (idx < 0) {
1688 #ifdef EMUXKI_DEBUG
1689 				printf("Invalid bufsize: %d\n", bufsize);
1690 #endif
1691 				return (EINVAL);
1692 			}
1693 			mtx_enter(&audio_lock);
1694 			emuxki_write(voice->sc, 0,
1695 			    emuxki_recsrc_szreg[voice->dataloc.source], idx);
1696 			emuxki_write(voice->sc, 0,
1697 			    emuxki_recsrc_bufaddrreg[voice->dataloc.source],
1698 			    DMAADDR(mem->dmamem));
1699 			mtx_leave(&audio_lock);
1700 			/* Use timer to emulate DMA completion interrupt */
1701 			voice->timerate = (u_int32_t) 48000 * blksize /
1702 			    (voice->sample_rate * sample_size);
1703 			if (voice->timerate < 5) {
1704 #ifdef EMUXKI_DEBUG
1705 				printf("Invalid timerate: %d, blksize %d\n",
1706 				    voice->timerate, blksize);
1707 #endif
1708 				error = EINVAL;
1709 			}
1710 		}
1711 
1712 		break;
1713 	}
1714 
1715 	return (error);
1716 }
1717 
1718 void
1719 emuxki_voice_commit_parms(struct emuxki_voice *voice)
1720 {
1721 	if (voice->use & EMU_VOICE_USE_PLAY) {
1722 		emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1723 		if (voice->stereo)
1724 			emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1725 	}
1726 }
1727 
1728 u_int32_t
1729 emuxki_voice_curaddr(struct emuxki_voice *voice)
1730 {
1731 	int idxreg = 0;
1732 
1733 	/* XXX different semantics in these cases */
1734 	if (voice->use & EMU_VOICE_USE_PLAY) {
1735 		/* returns number of samples (an l/r pair counts 1) */
1736 		return (emuxki_read(voice->sc,
1737 				     voice->dataloc.chan[0]->num,
1738 				     EMU_CHAN_CCCA_CURRADDR) -
1739 			voice->dataloc.chan[0]->loop.start);
1740 	} else {
1741 		/* returns number of bytes */
1742 		switch (voice->dataloc.source) {
1743 			case EMU_RECSRC_MIC:
1744 				idxreg = (voice->sc->sc_flags & EMUXKI_AUDIGY) ?
1745 					EMU_A_MICIDX : EMU_MICIDX;
1746 				break;
1747 			case EMU_RECSRC_ADC:
1748 				idxreg = (voice->sc->sc_flags & EMUXKI_AUDIGY) ?
1749 					EMU_A_ADCIDX : EMU_ADCIDX;
1750 				break;
1751 			case EMU_RECSRC_FX:
1752 				idxreg = EMU_FXIDX;
1753 				break;
1754 			default:
1755 #ifdef EMUXKI_DEBUG
1756 				printf("emu: bad recording source!\n");
1757 #endif
1758 				break;
1759 		}
1760 		return (emuxki_read(voice->sc, 0, EMU_RECIDX(idxreg))
1761 				& EMU_RECIDX_MASK);
1762 	}
1763 	return (0);
1764 }
1765 
1766 void
1767 emuxki_resched_timer(struct emuxki_softc *sc)
1768 {
1769 	struct emuxki_voice *voice;
1770 	u_int16_t       timerate = 1024;
1771 	u_int8_t	active = 0;
1772 
1773 	LIST_FOREACH(voice, &sc->voices, next) {
1774 		if ((voice->state & EMU_VOICE_STATE_STARTED) == 0)
1775 			continue;
1776 		active = 1;
1777 		if (voice->timerate < timerate)
1778 			timerate = voice->timerate;
1779 	}
1780 
1781 	if (timerate & ~EMU_TIMER_RATE_MASK)
1782 		timerate = 0;
1783 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_TIMER, timerate);
1784 	if (!active && (sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1785 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1786 			bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) &
1787 			~EMU_INTE_INTERTIMERENB);
1788 		sc->timerstate &= ~EMU_TIMER_STATE_ENABLED;
1789 	} else if (active && !(sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1790 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1791 			bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
1792 			EMU_INTE_INTERTIMERENB);
1793 		sc->timerstate |= EMU_TIMER_STATE_ENABLED;
1794 	}
1795 }
1796 
1797 int
1798 emuxki_voice_adc_rate(struct emuxki_voice *voice)
1799 {
1800 	switch(voice->sample_rate) {
1801 		case 48000:
1802 			return EMU_ADCCR_SAMPLERATE_48;
1803 			break;
1804 		case 44100:
1805 			return EMU_ADCCR_SAMPLERATE_44;
1806 			break;
1807 		case 32000:
1808 			return EMU_ADCCR_SAMPLERATE_32;
1809 			break;
1810 		case 24000:
1811 			return EMU_ADCCR_SAMPLERATE_24;
1812 			break;
1813 		case 22050:
1814 			return EMU_ADCCR_SAMPLERATE_22;
1815 			break;
1816 		case 16000:
1817 			return EMU_ADCCR_SAMPLERATE_16;
1818 			break;
1819 		case 12000:
1820 			if (voice->sc->sc_flags & EMUXKI_AUDIGY)
1821 				return EMU_A_ADCCR_SAMPLERATE_12;
1822 			else {
1823 #ifdef EMUXKI_DEBUG
1824 				printf("recording sample_rate not supported : %u\n", voice->sample_rate);
1825 #endif
1826 				return (-1);
1827 			}
1828 			break;
1829 		case 11000:
1830 			if (voice->sc->sc_flags & EMUXKI_AUDIGY)
1831 				return EMU_A_ADCCR_SAMPLERATE_11;
1832 			else
1833 				return EMU_ADCCR_SAMPLERATE_11;
1834 			break;
1835 		case 8000:
1836 			if (voice->sc->sc_flags & EMUXKI_AUDIGY)
1837 				return EMU_A_ADCCR_SAMPLERATE_8;
1838 			else
1839 				return EMU_ADCCR_SAMPLERATE_8;
1840 			break;
1841 		default:
1842 #ifdef EMUXKI_DEBUG
1843 				printf("recording sample_rate not supported : %u\n", voice->sample_rate);
1844 #endif
1845 				return (-1);
1846 	}
1847 	return (-1);  /* shouldn't get here */
1848 }
1849 
1850 void
1851 emuxki_voice_start(struct emuxki_voice *voice,
1852 		    void (*inth) (void *), void *inthparam)
1853 {
1854 	u_int32_t val;
1855 
1856 	mtx_enter(&audio_lock);
1857 	voice->inth = inth;
1858 	voice->inthparam = inthparam;
1859 	if (voice->use & EMU_VOICE_USE_PLAY) {
1860 		voice->trigblk = 1;
1861 		emuxki_channel_start(voice->dataloc.chan[0]);
1862 		if (voice->stereo)
1863 			emuxki_channel_start(voice->dataloc.chan[1]);
1864 	} else {
1865 		voice->trigblk = 1;
1866 		switch (voice->dataloc.source) {
1867 		case EMU_RECSRC_ADC:
1868 			/* XXX need to program DSP to output L+R
1869 			 * XXX in monaural case? */
1870 			if (voice->sc->sc_flags & EMUXKI_AUDIGY) {
1871 				val = EMU_A_ADCCR_LCHANENABLE;
1872 				if (voice->stereo)
1873 					val |= EMU_A_ADCCR_RCHANENABLE;
1874 			} else {
1875 				val = EMU_ADCCR_LCHANENABLE;
1876 				if (voice->stereo)
1877 					val |= EMU_ADCCR_RCHANENABLE;
1878 			}
1879 			val |= emuxki_voice_adc_rate(voice);
1880 			emuxki_write(voice->sc, 0, EMU_ADCCR, 0);
1881 			emuxki_write(voice->sc, 0, EMU_ADCCR, val);
1882 			break;
1883 		case EMU_RECSRC_MIC:
1884 		case EMU_RECSRC_FX:
1885 			printf("unimplemented\n");
1886 			break;
1887 		case EMU_RECSRC_NOTSET:
1888 		default:
1889 			break;
1890 		}
1891 #if 0
1892 		/* DMA completion interrupt is useless; use timer */
1893 		val = emu_rd(sc, INTE, 4);
1894 		val |= emuxki_recsrc_intrmasks[voice->dataloc.source];
1895 		emu_wr(sc, INTE, val, 4);
1896 #endif
1897 	}
1898 	voice->state |= EMU_VOICE_STATE_STARTED;
1899 	emuxki_resched_timer(voice->sc);
1900 	mtx_leave(&audio_lock);
1901 }
1902 
1903 void
1904 emuxki_voice_halt(struct emuxki_voice *voice)
1905 {
1906 	mtx_enter(&audio_lock);
1907 	if (voice->use & EMU_VOICE_USE_PLAY) {
1908 		emuxki_channel_stop(voice->dataloc.chan[0]);
1909 		if (voice->stereo)
1910 			emuxki_channel_stop(voice->dataloc.chan[1]);
1911 	} else {
1912 		switch (voice->dataloc.source) {
1913 		case EMU_RECSRC_ADC:
1914 			emuxki_write(voice->sc, 0, EMU_ADCCR, 0);
1915 			break;
1916 		case EMU_RECSRC_FX:
1917 		case EMU_RECSRC_MIC:
1918 			printf("unimplemented\n");
1919 			break;
1920 		case EMU_RECSRC_NOTSET:
1921 			printf("Bad dataloc.source\n");
1922 		}
1923 		/* This should reset buffer pointer */
1924 		emuxki_write(voice->sc, 0,
1925 		    emuxki_recsrc_szreg[voice->dataloc.source],
1926 		    EMU_RECBS_BUFSIZE_NONE);
1927 #if 0
1928 		val = emu_rd(sc, INTE, 4);
1929 		val &= ~emuxki_recsrc_intrmasks[voice->dataloc.source];
1930 		emu_wr(sc, INTE, val, 4);
1931 #endif
1932 	}
1933 	voice->state &= ~EMU_VOICE_STATE_STARTED;
1934 	emuxki_resched_timer(voice->sc);
1935 	mtx_leave(&audio_lock);
1936 }
1937 
1938 /*
1939  * The interrupt handler
1940  */
1941 int
1942 emuxki_intr(void *arg)
1943 {
1944 	struct emuxki_softc *sc = arg;
1945 	u_int32_t       ipr, curblk, us = 0;
1946 	struct emuxki_voice *voice;
1947 
1948 	mtx_enter(&audio_lock);
1949 	while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) {
1950 		if (ipr & EMU_IPR_INTERVALTIMER) {
1951 			LIST_FOREACH(voice, &sc->voices, next) {
1952 				if ((voice->state &
1953 				      EMU_VOICE_STATE_STARTED) == 0)
1954 					continue;
1955 
1956 				curblk = emuxki_voice_curaddr(voice) /
1957 				       voice->blksize;
1958 #if 0
1959 				if (curblk == voice->trigblk) {
1960 					voice->inth(voice->inthparam);
1961 					voice->trigblk++;
1962 					voice->trigblk %= voice->blkmod;
1963 				}
1964 #else
1965 				while ((curblk >= voice->trigblk &&
1966 				    curblk < (voice->trigblk + voice->blkmod / 2)) ||
1967 				    ((int)voice->trigblk - (int)curblk) >
1968 				    (voice->blkmod / 2 + 1)) {
1969 					voice->inth(voice->inthparam);
1970 					voice->trigblk++;
1971 					voice->trigblk %= voice->blkmod;
1972 				}
1973 #endif
1974 			}
1975 			us = 1;
1976 		}
1977 
1978 		/* Got interrupt */
1979 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr);
1980 	}
1981 	mtx_leave(&audio_lock);
1982 	return (us);
1983 }
1984 
1985 
1986 /*
1987  * Audio Architecture callbacks
1988  */
1989 
1990 int
1991 emuxki_open(void *addr, int flags)
1992 {
1993 	struct emuxki_softc *sc = addr;
1994 
1995 #ifdef EMUXKI_DEBUG
1996 	printf("%s: emuxki_open called\n", sc->sc_dev.dv_xname);
1997 #endif
1998 
1999 	/*
2000 	 * Multiple voice support would be added as soon as I find a way to
2001 	 * trick the audio arch into supporting multiple voices.
2002 	 * Or I might integrate a modified audio arch supporting
2003 	 * multiple voices.
2004 	 */
2005 
2006 	/*
2007 	 * I did this because i have problems identifying the selected
2008 	 * recording source(s) which is necessary when setting recording
2009 	 * params. This will be addressed very soon.
2010 	 */
2011 	if (flags & FREAD) {
2012 		sc->rvoice = emuxki_voice_new(sc, 0 /* EMU_VOICE_USE_RECORD */);
2013 		if (sc->rvoice == NULL)
2014 			return (EBUSY);
2015 
2016 		/* XXX Hardcode RECSRC_ADC for now */
2017 		sc->rvoice->dataloc.source = EMU_RECSRC_ADC;
2018 	}
2019 
2020 	if (flags & FWRITE) {
2021 		sc->pvoice = emuxki_voice_new(sc, EMU_VOICE_USE_PLAY);
2022 		if (sc->pvoice == NULL) {
2023 			if (flags & FREAD)
2024 				emuxki_voice_delete(sc->rvoice);
2025 			return (EBUSY);
2026 		}
2027 	}
2028 
2029 	return (0);
2030 }
2031 
2032 void
2033 emuxki_close(void *addr)
2034 {
2035 	struct emuxki_softc *sc = addr;
2036 
2037 #ifdef EMUXKI_DEBUG
2038 	printf("%s: emu10K1_close called\n", sc->sc_dev.dv_xname);
2039 #endif
2040 
2041 	/* No multiple voice support for now */
2042 	if (sc->rvoice != NULL)
2043 		emuxki_voice_delete(sc->rvoice);
2044 	sc->rvoice = NULL;
2045 	if (sc->pvoice != NULL)
2046 		emuxki_voice_delete(sc->pvoice);
2047 	sc->pvoice = NULL;
2048 }
2049 
2050 int
2051 emuxki_set_vparms(struct emuxki_voice *voice, struct audio_params *p)
2052 {
2053 	u_int8_t	b16, mode;
2054 
2055 	mode = (voice->use & EMU_VOICE_USE_PLAY) ?
2056 		AUMODE_PLAY : AUMODE_RECORD;
2057 	if (p->channels > 2)
2058 		p->channels = 2;
2059 	if (p->precision > 16)
2060 		p->precision = 16;
2061 	/* Will change when streams come in use */
2062 
2063 	/*
2064 	 * Always use slinear_le for recording, as how to set otherwise
2065 	 * isn't known.
2066 	 */
2067 	if (mode == AUMODE_PLAY)
2068 		b16 = (p->precision == 16);
2069 	else {
2070 		b16 = 1;
2071 		p->precision = 16;
2072 	}
2073 
2074 	switch (p->encoding) {
2075 	case AUDIO_ENCODING_SLINEAR_LE:
2076 		if (p->precision != 16)
2077 			return EINVAL;
2078 		break;
2079 
2080 	case AUDIO_ENCODING_ULINEAR_LE:
2081 	case AUDIO_ENCODING_ULINEAR_BE:
2082 		if (p->precision != 8)
2083 			return EINVAL;
2084 		break;
2085 
2086 	default:
2087 		return (EINVAL);
2088 	}
2089 	p->bps = AUDIO_BPS(p->precision);
2090 	p->msb = 1;
2091 
2092 	return (emuxki_voice_set_audioparms(voice, p->channels == 2,
2093 				     b16, p->sample_rate));
2094 }
2095 
2096 int
2097 emuxki_set_params(void *addr, int setmode, int usemode,
2098 		   struct audio_params *play, struct audio_params *rec)
2099 {
2100 	struct emuxki_softc *sc = addr;
2101 	int	     mode, error;
2102 	struct audio_params *p;
2103 
2104 	for (mode = AUMODE_RECORD; mode != -1;
2105 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
2106 		if ((usemode & setmode & mode) == 0)
2107 			continue;
2108 
2109 		p = (mode == AUMODE_PLAY) ? play : rec;
2110 
2111 		/* No multiple voice support for now */
2112 		if ((error = emuxki_set_vparms((mode == AUMODE_PLAY) ?
2113 						sc->pvoice : sc->rvoice, p)))
2114 			return (error);
2115 	}
2116 
2117 	return (0);
2118 }
2119 
2120 int
2121 emuxki_halt_output(void *addr)
2122 {
2123 	struct emuxki_softc *sc = addr;
2124 
2125 	/* No multiple voice support for now */
2126 	if (sc->pvoice == NULL)
2127 		return (ENXIO);
2128 
2129 	emuxki_voice_halt(sc->pvoice);
2130 	return (0);
2131 }
2132 
2133 int
2134 emuxki_halt_input(void *addr)
2135 {
2136 	struct emuxki_softc *sc = addr;
2137 
2138 #ifdef EMUXKI_DEBUG
2139 	printf("%s: emuxki_halt_input called\n", sc->sc_dev.dv_xname);
2140 #endif
2141 
2142 	/* No multiple voice support for now */
2143 	if (sc->rvoice == NULL)
2144 		return (ENXIO);
2145 	emuxki_voice_halt(sc->rvoice);
2146 	return (0);
2147 }
2148 
2149 int
2150 emuxki_set_port(void *addr, mixer_ctrl_t *mctl)
2151 {
2152 	struct emuxki_softc *sc = addr;
2153 
2154 	return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
2155 }
2156 
2157 int
2158 emuxki_get_port(void *addr, mixer_ctrl_t *mctl)
2159 {
2160 	struct emuxki_softc *sc = addr;
2161 
2162 	return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
2163 }
2164 
2165 int
2166 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo)
2167 {
2168 	struct emuxki_softc *sc = addr;
2169 
2170 	return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
2171 }
2172 
2173 void *
2174 emuxki_allocm(void *addr, int direction, size_t size, int type, int flags)
2175 {
2176 	struct emuxki_softc *sc = addr;
2177 
2178 	if (direction == AUMODE_PLAY)
2179 		return emuxki_pmem_alloc(sc, size, type, flags);
2180 	else
2181 		return emuxki_rmem_alloc(sc, size, type, flags);
2182 }
2183 
2184 void
2185 emuxki_freem(void *addr, void *ptr, int type)
2186 {
2187 	struct emuxki_softc *sc = addr;
2188 	int	     i;
2189 	struct emuxki_mem *mem;
2190 	size_t	  numblocks;
2191 	u_int32_t      *ptb, silentpage;
2192 
2193 	ptb = KERNADDR(sc->ptb);
2194 	silentpage = DMAADDR(sc->silentpage) << 1;
2195 	LIST_FOREACH(mem, &sc->mem, next) {
2196 		if (KERNADDR(mem->dmamem) != ptr)
2197 			continue;
2198 
2199 		mtx_enter(&audio_lock);
2200 		if (mem->ptbidx != EMU_RMEM) {
2201 			numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE;
2202 			if (DMASIZE(mem->dmamem) % EMU_PTESIZE)
2203 				numblocks++;
2204 			for (i = 0; i < numblocks; i++)
2205 				ptb[mem->ptbidx + i] =
2206 				    htole32(silentpage | (mem->ptbidx + i));
2207 		}
2208 		LIST_REMOVE(mem, next);
2209 		mtx_leave(&audio_lock);
2210 
2211 		emuxki_mem_delete(mem, type);
2212 		break;
2213 	}
2214 }
2215 
2216 /* blocksize should be a divisor of allowable buffersize */
2217 /* XXX probably this could be done better */
2218 int
2219 emuxki_round_blocksize(void *addr, int blksize)
2220 {
2221 	int bufsize = 65536;
2222 
2223 	while (bufsize > blksize)
2224 		bufsize /= 2;
2225 
2226 	return bufsize;
2227 }
2228 
2229 size_t
2230 emuxki_round_buffersize(void *addr, int direction, size_t bsize)
2231 {
2232 
2233 	if (direction == AUMODE_PLAY) {
2234 		if (bsize < EMU_PTESIZE)
2235 			bsize = EMU_PTESIZE;
2236 		else if (bsize > (EMU_PTESIZE * EMU_MAXPTE))
2237 			bsize = EMU_PTESIZE * EMU_MAXPTE;
2238 		/* Would be better if set to max available */
2239 		else if (bsize % EMU_PTESIZE)
2240 			bsize = bsize -
2241 				(bsize % EMU_PTESIZE) +
2242 				EMU_PTESIZE;
2243 	} else {
2244 		int idx;
2245 
2246 		/* find nearest lower recbuf size */
2247 		for(idx = sizeof(emuxki_recbuf_sz) /
2248 		    sizeof(emuxki_recbuf_sz[0]); --idx >= 0; ) {
2249 			if (bsize >= emuxki_recbuf_sz[idx]) {
2250 				bsize = emuxki_recbuf_sz[idx];
2251 				break;
2252 			}
2253 		}
2254 
2255 		if (bsize == 0)
2256 			bsize = 384;
2257 	}
2258 
2259 	return (bsize);
2260 }
2261 
2262 int
2263 emuxki_get_props(void *addr)
2264 {
2265 	return (AUDIO_PROP_FULLDUPLEX);
2266 }
2267 
2268 int
2269 emuxki_trigger_output(void *addr, void *start, void *end, int blksize,
2270 		       void (*inth) (void *), void *inthparam,
2271 		       struct audio_params *params)
2272 {
2273 	struct emuxki_softc *sc = addr;
2274 	/* No multiple voice support for now */
2275 	struct emuxki_voice *voice = sc->pvoice;
2276 	int	     error;
2277 
2278 	if (voice == NULL)
2279 		return (ENXIO);
2280 	if ((error = emuxki_set_vparms(voice, params)))
2281 		return (error);
2282 	if ((error = emuxki_voice_set_bufparms(voice, start,
2283 				(caddr_t)end - (caddr_t)start, blksize)))
2284 		return (error);
2285 	emuxki_voice_commit_parms(voice);
2286 	emuxki_voice_start(voice, inth, inthparam);
2287 	return (0);
2288 }
2289 
2290 int
2291 emuxki_trigger_input(void *addr, void *start, void *end, int blksize,
2292 		      void (*inth) (void *), void *inthparam,
2293 		      struct audio_params *params)
2294 {
2295 	struct emuxki_softc *sc = addr;
2296 	/* No multiple voice support for now */
2297 	struct emuxki_voice *voice = sc->rvoice;
2298 	int	error;
2299 
2300 	if (voice == NULL)
2301 		return (ENXIO);
2302 	if ((error = emuxki_set_vparms(voice, params)))
2303 		return (error);
2304 	if ((error = emuxki_voice_set_bufparms(voice, start,
2305 						(caddr_t)end - (caddr_t)start,
2306 						blksize)))
2307 		return (error);
2308 	emuxki_voice_start(voice, inth, inthparam);
2309 	return (0);
2310 }
2311 
2312 
2313 /*
2314  * AC97 callbacks
2315  */
2316 
2317 int
2318 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif)
2319 {
2320 	struct emuxki_softc *sc = arg;
2321 
2322 	sc->codecif = codecif;
2323 	return (0);
2324 }
2325 
2326 int
2327 emuxki_ac97_read(void *arg, u_int8_t reg, u_int16_t *val)
2328 {
2329 	struct emuxki_softc *sc = arg;
2330 
2331 	mtx_enter(&audio_lock);
2332 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2333 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA);
2334 	mtx_leave(&audio_lock);
2335 
2336 	return (0);
2337 }
2338 
2339 int
2340 emuxki_ac97_write(void *arg, u_int8_t reg, u_int16_t val)
2341 {
2342 	struct emuxki_softc *sc = arg;
2343 
2344 	mtx_enter(&audio_lock);
2345 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2346 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val);
2347 	mtx_leave(&audio_lock);
2348 
2349 	return (0);
2350 }
2351 
2352 void
2353 emuxki_ac97_reset(void *arg)
2354 {
2355 }
2356