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