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