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