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