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