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