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