emuxki.c revision 1.24 1 /* $NetBSD: emuxki.c,v 1.24 2003/03/17 12:29:09 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.24 2003/03/17 12:29:09 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 } else if (voice->use != use)
1304 emuxki_voice_dataloc_destroy(voice);
1305 else
1306 goto skip_initialize;
1307
1308 voice->sc = sc;
1309 voice->state = !EMU_VOICE_STATE_STARTED;
1310 voice->stereo = EMU_VOICE_STEREO_NOTSET;
1311 voice->b16 = 0;
1312 voice->sample_rate = 0;
1313 if (use & EMU_VOICE_USE_PLAY)
1314 voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL;
1315 else
1316 voice->dataloc.source = EMU_RECSRC_NOTSET;
1317 voice->buffer = NULL;
1318 voice->blksize = 0;
1319 voice->trigblk = 0;
1320 voice->blkmod = 0;
1321 voice->inth = NULL;
1322 voice->inthparam = NULL;
1323 voice->use = use;
1324
1325 skip_initialize:
1326 s = splaudio();
1327 LIST_INSERT_HEAD((&sc->voices), voice, next);
1328 splx(s);
1329
1330 return (voice);
1331 }
1332
1333 static void
1334 emuxki_voice_delete(struct emuxki_voice *voice)
1335 {
1336 struct emuxki_softc *sc = voice->sc;
1337 struct emuxki_voice *lvoice;
1338 int s;
1339
1340 if (voice->state & EMU_VOICE_STATE_STARTED)
1341 emuxki_voice_halt(voice);
1342
1343 s = splaudio();
1344 LIST_REMOVE(voice, next);
1345 lvoice = sc->lvoice;
1346 sc->lvoice = voice;
1347 splx(s);
1348
1349 if (lvoice) {
1350 emuxki_voice_dataloc_destroy(lvoice);
1351 free(lvoice, M_DEVBUF);
1352 }
1353 }
1354
1355 static int
1356 emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo)
1357 {
1358 int error;
1359 emuxki_recsrc_t source;
1360 struct emuxki_chanparms_fxsend fxsend;
1361
1362 if (! (voice->use & EMU_VOICE_USE_PLAY))
1363 source = voice->dataloc.source;
1364 emuxki_voice_dataloc_destroy(voice);
1365 if (! (voice->use & EMU_VOICE_USE_PLAY))
1366 voice->dataloc.source = source;
1367 voice->stereo = stereo;
1368 if ((error = emuxki_voice_dataloc_create(voice)))
1369 return (error);
1370 if (voice->use & EMU_VOICE_USE_PLAY) {
1371 fxsend.a.dest = 0x0;
1372 fxsend.b.dest = 0x1;
1373 fxsend.c.dest = 0x2;
1374 fxsend.d.dest = 0x3;
1375 if (voice->stereo) {
1376 fxsend.a.level = fxsend.c.level = 0xc0;
1377 fxsend.b.level = fxsend.d.level = 0x00;
1378 emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1379 &fxsend);
1380 fxsend.a.level = fxsend.c.level = 0x00;
1381 fxsend.b.level = fxsend.d.level = 0xc0;
1382 emuxki_channel_set_fxsend(voice->dataloc.chan[1],
1383 &fxsend);
1384 } /* No else : default is good for mono */
1385 }
1386 return (0);
1387 }
1388
1389 static int
1390 emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate)
1391 {
1392 if (voice->use & EMU_VOICE_USE_PLAY) {
1393 if ((srate < 4000) || (srate > 48000))
1394 return (EINVAL);
1395 voice->sample_rate = srate;
1396 emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1397 if (voice->stereo)
1398 emuxki_channel_set_srate(voice->dataloc.chan[1],
1399 srate);
1400 } else {
1401 if (emuxki_recsrc_rate_to_index(srate) < 0)
1402 return (EINVAL);
1403 voice->sample_rate = srate;
1404 }
1405 return (0);
1406 }
1407
1408 static int
1409 emuxki_voice_set_audioparms(struct emuxki_voice *voice, u_int8_t stereo,
1410 u_int8_t b16, u_int32_t srate)
1411 {
1412 int error;
1413
1414 if (voice->stereo == stereo && voice->b16 == b16 &&
1415 voice->sample_rate == srate)
1416 return (0);
1417
1418 #ifdef EMUXKI_DEBUG
1419 printf("Setting %s voice params : %s, %u bits, %u Hz\n",
1420 (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record",
1421 stereo ? "stereo" : "mono", (b16 + 1) * 8, srate);
1422 #endif
1423
1424 if (voice->stereo != stereo) {
1425 if ((error = emuxki_voice_set_stereo(voice, stereo)))
1426 return (error);
1427 }
1428 voice->b16 = b16;
1429 if (voice->sample_rate != srate)
1430 error = emuxki_voice_set_srate(voice, srate);
1431 return error;
1432 }
1433
1434 /* voice audio parms (see just before) must be set prior to this */
1435 static int
1436 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr,
1437 u_int32_t bufsize, u_int16_t blksize)
1438 {
1439 struct emuxki_mem *mem;
1440 struct emuxki_channel **chan;
1441 u_int32_t start, end;
1442 u_int8_t sample_size;
1443 int idx;
1444 int error = EFAULT;
1445
1446 LIST_FOREACH(mem, &voice->sc->mem, next) {
1447 if (KERNADDR(mem->dmamem) != ptr)
1448 continue;
1449
1450 voice->buffer = mem;
1451 sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1452 voice->trigblk = 0; /* This shouldn't be needed */
1453 voice->blkmod = bufsize / blksize;
1454 if (bufsize % blksize) /* This should not happen */
1455 voice->blkmod++;
1456 error = 0;
1457
1458 if (voice->use & EMU_VOICE_USE_PLAY) {
1459 voice->blksize = blksize / sample_size;
1460 chan = voice->dataloc.chan;
1461 start = mem->ptbidx << 12;
1462 end = start + bufsize / sample_size;
1463 emuxki_channel_set_bufparms(chan[0],
1464 start, end);
1465 if (voice->stereo)
1466 emuxki_channel_set_bufparms(chan[1],
1467 start, end);
1468 voice->timerate = (u_int32_t) 48000 *
1469 voice->blksize / voice->sample_rate;
1470 if (voice->timerate < 5)
1471 error = EINVAL;
1472 } else {
1473 voice->blksize = blksize;
1474 for(idx = sizeof(emuxki_recbuf_sz) /
1475 sizeof(emuxki_recbuf_sz[0]); --idx >= 0;)
1476 if (emuxki_recbuf_sz[idx] == bufsize)
1477 break;
1478 if (idx < 0) {
1479 #ifdef EMUXKI_DEBUG
1480 printf("Invalid bufsize: %d\n", bufsize);
1481 #endif
1482 return (EINVAL);
1483 }
1484 emuxki_write(voice->sc, 0,
1485 emuxki_recsrc_szreg[voice->dataloc.source], idx);
1486 emuxki_write(voice->sc, 0,
1487 emuxki_recsrc_bufaddrreg[voice->dataloc.source],
1488 DMAADDR(mem->dmamem));
1489
1490 /* Use timer to emulate DMA completion interrupt */
1491 voice->timerate = (u_int32_t) 48000 * blksize /
1492 (voice->sample_rate * sample_size);
1493 if (voice->timerate < 5) {
1494 #ifdef EMUXKI_DEBUG
1495 printf("Invalid timerate: %d, blksize %d\n",
1496 voice->timerate, blksize);
1497 #endif
1498 error = EINVAL;
1499 }
1500 }
1501
1502 break;
1503 }
1504
1505 return (error);
1506 }
1507
1508 static void
1509 emuxki_voice_commit_parms(struct emuxki_voice *voice)
1510 {
1511 if (voice->use & EMU_VOICE_USE_PLAY) {
1512 emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1513 if (voice->stereo)
1514 emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1515 }
1516 }
1517
1518 static u_int32_t
1519 emuxki_voice_curaddr(struct emuxki_voice *voice)
1520 {
1521
1522 /* XXX different semantics in these cases */
1523 if (voice->use & EMU_VOICE_USE_PLAY)
1524 /* returns number of samples (an l/r pair counts 1) */
1525 return (emuxki_read(voice->sc,
1526 voice->dataloc.chan[0]->num,
1527 EMU_CHAN_CCCA_CURRADDR) -
1528 voice->dataloc.chan[0]->loop.start);
1529 else
1530 /* returns number of bytes */
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 /*
1847 * Always use slinear_le for recording, as how to set otherwise
1848 * isn't known.
1849 */
1850 if (mode == AUMODE_PLAY)
1851 b16 = (p->precision == 16);
1852 else {
1853 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
1854 p->hw_precision = 16;
1855 b16 = 1;
1856 if (p->precision == 8)
1857 p->factor *= 2;
1858 }
1859
1860 switch (p->encoding) {
1861 case AUDIO_ENCODING_ULAW:
1862 if (mode == AUMODE_PLAY) {
1863 p->factor = 2;
1864 p->sw_code = mulaw_to_slinear16_le;
1865 b16 = 1;
1866 } else
1867 p->sw_code = slinear16_to_mulaw_le;
1868 break;
1869
1870 case AUDIO_ENCODING_ALAW:
1871 if (mode == AUMODE_PLAY) {
1872 p->factor = 2;
1873 p->sw_code = alaw_to_slinear16_le;
1874 b16 = 1;
1875 } else
1876 p->sw_code = slinear16_to_alaw_le;
1877 break;
1878
1879 case AUDIO_ENCODING_SLINEAR_LE:
1880 if (p->precision == 8) {
1881 if (mode == AUMODE_PLAY)
1882 p->sw_code = change_sign8;
1883 else
1884 p->sw_code = linear16_to_linear8_le;
1885 }
1886 break;
1887
1888 case AUDIO_ENCODING_ULINEAR_LE:
1889 if (p->precision == 16)
1890 p->sw_code = change_sign16_le;
1891 else if (mode == AUMODE_RECORD)
1892 p->sw_code = slinear16_to_ulinear8_le;
1893 break;
1894
1895 case AUDIO_ENCODING_SLINEAR_BE:
1896 if (p->precision == 16)
1897 p->sw_code = swap_bytes;
1898 else {
1899 if (mode == AUMODE_PLAY)
1900 p->sw_code = change_sign8;
1901 else
1902 p->sw_code = linear16_to_linear8_le;
1903 }
1904 break;
1905
1906 case AUDIO_ENCODING_ULINEAR_BE:
1907 if (p->precision == 16) {
1908 if (mode == AUMODE_PLAY)
1909 p->sw_code = swap_bytes_change_sign16_le;
1910 else
1911 p->sw_code = change_sign16_swap_bytes_le;
1912 } else if (mode == AUMODE_RECORD)
1913 p->sw_code = slinear16_to_ulinear8_le;
1914 break;
1915
1916 default:
1917 return (EINVAL);
1918 }
1919
1920 return (emuxki_voice_set_audioparms(voice, p->channels == 2,
1921 b16, p->sample_rate));
1922 }
1923
1924 static int
1925 emuxki_set_params(void *addr, int setmode, int usemode,
1926 struct audio_params *play, struct audio_params *rec)
1927 {
1928 struct emuxki_softc *sc = addr;
1929 int mode, error;
1930 struct audio_params *p;
1931 struct emuxki_voice *v;
1932
1933 for (mode = AUMODE_RECORD; mode != -1;
1934 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1935 if ((usemode & setmode & mode) == 0)
1936 continue;
1937
1938 if (mode == AUMODE_PLAY) {
1939 p = play;
1940 v = sc->pvoice;
1941 } else {
1942 p = rec;
1943 v = sc->rvoice;
1944 }
1945
1946 if (v == NULL) {
1947 continue;
1948 }
1949
1950 /* No multiple voice support for now */
1951 if ((error = emuxki_set_vparms(v, p)))
1952 return (error);
1953 }
1954
1955 return (0);
1956 }
1957
1958 static int
1959 emuxki_halt_output(void *addr)
1960 {
1961 struct emuxki_softc *sc = addr;
1962
1963 /* No multiple voice support for now */
1964 if (sc->pvoice == NULL)
1965 return (ENXIO);
1966
1967 emuxki_voice_halt(sc->pvoice);
1968 return (0);
1969 }
1970
1971 static int
1972 emuxki_halt_input(void *addr)
1973 {
1974 struct emuxki_softc *sc = addr;
1975
1976 #ifdef EMUXKI_DEBUG
1977 printf("%s: emuxki_halt_input called\n", sc->sc_dev.dv_xname);
1978 #endif
1979
1980 /* No multiple voice support for now */
1981 if (sc->rvoice == NULL)
1982 return (ENXIO);
1983 emuxki_voice_halt(sc->rvoice);
1984 return (0);
1985 }
1986
1987 static int
1988 emuxki_getdev(void *addr, struct audio_device *dev)
1989 {
1990 strncpy(dev->name, "Creative EMU10k1", sizeof(dev->name));
1991 strcpy(dev->version, "");
1992 strncpy(dev->config, "emuxki", sizeof(dev->config));
1993
1994 return (0);
1995 }
1996
1997 static int
1998 emuxki_set_port(void *addr, mixer_ctrl_t *mctl)
1999 {
2000 struct emuxki_softc *sc = addr;
2001
2002 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
2003 }
2004
2005 static int
2006 emuxki_get_port(void *addr, mixer_ctrl_t *mctl)
2007 {
2008 struct emuxki_softc *sc = addr;
2009
2010 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
2011 }
2012
2013 static int
2014 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo)
2015 {
2016 struct emuxki_softc *sc = addr;
2017
2018 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
2019 }
2020
2021 static void *
2022 emuxki_allocm(void *addr, int direction, size_t size,
2023 struct malloc_type *type, int flags)
2024 {
2025 struct emuxki_softc *sc = addr;
2026
2027 if (direction == AUMODE_PLAY)
2028 return emuxki_pmem_alloc(sc, size, type, flags);
2029 else
2030 return emuxki_rmem_alloc(sc, size, type, flags);
2031 }
2032
2033 static void
2034 emuxki_freem(void *addr, void *ptr, struct malloc_type *type)
2035 {
2036 struct emuxki_softc *sc = addr;
2037 int i, s;
2038 struct emuxki_mem *mem;
2039 size_t numblocks;
2040 u_int32_t *ptb, silentpage;
2041
2042 ptb = KERNADDR(sc->ptb);
2043 silentpage = DMAADDR(sc->silentpage) << 1;
2044 LIST_FOREACH(mem, &sc->mem, next) {
2045 if (KERNADDR(mem->dmamem) != ptr)
2046 continue;
2047
2048 s = splaudio();
2049 if (mem->ptbidx != EMU_RMEM) {
2050 numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE;
2051 if (DMASIZE(mem->dmamem) % EMU_PTESIZE)
2052 numblocks++;
2053 for (i = 0; i < numblocks; i++)
2054 ptb[mem->ptbidx + i] =
2055 silentpage | (mem->ptbidx + i);
2056 }
2057 LIST_REMOVE(mem, next);
2058 splx(s);
2059
2060 emuxki_mem_delete(mem, type);
2061 break;
2062 }
2063 }
2064
2065 /* blocksize should be a divisor of allowable buffersize */
2066 /* XXX probably this could be done better */
2067 static int
2068 emuxki_round_blocksize(void *addr, int blksize)
2069 {
2070 #if 0
2071 struct emuxki_softc *sc = addr;
2072 struct audio_softc *au;
2073 #endif
2074 int bufsize;
2075 #if 0
2076 if (sc == NULL)
2077 return blksize;
2078
2079 au = (void *)sc->sc_audev;
2080 if (au == NULL)
2081 return blksize;
2082
2083 bufsize = emuxki_round_buffersize(sc, AUMODE_RECORD,
2084 au->sc_rr.bufsize);
2085 #else
2086 bufsize = 65536;
2087 #endif
2088
2089 while (bufsize > blksize)
2090 bufsize /= 2;
2091
2092 return bufsize;
2093 }
2094
2095 static size_t
2096 emuxki_round_buffersize(void *addr, int direction, size_t bsize)
2097 {
2098
2099 if (direction == AUMODE_PLAY) {
2100 if (bsize < EMU_PTESIZE)
2101 bsize = EMU_PTESIZE;
2102 else if (bsize > (EMU_PTESIZE * EMU_MAXPTE))
2103 bsize = EMU_PTESIZE * EMU_MAXPTE;
2104 /* Would be better if set to max available */
2105 else if (bsize % EMU_PTESIZE)
2106 bsize = bsize -
2107 (bsize % EMU_PTESIZE) +
2108 EMU_PTESIZE;
2109 } else {
2110 int idx;
2111
2112 /* find nearest lower recbuf size */
2113 for(idx = sizeof(emuxki_recbuf_sz) /
2114 sizeof(emuxki_recbuf_sz[0]); --idx >= 0; ) {
2115 if (bsize >= emuxki_recbuf_sz[idx]) {
2116 bsize = emuxki_recbuf_sz[idx];
2117 break;
2118 }
2119 }
2120
2121 if (bsize == 0)
2122 bsize = 384;
2123 }
2124
2125 return (bsize);
2126 }
2127
2128 static paddr_t
2129 emuxki_mappage(void *addr, void *ptr, off_t off, int prot)
2130 {
2131 struct emuxki_softc *sc = addr;
2132 struct emuxki_mem *mem;
2133 u_int32_t *ptb;
2134
2135 ptb = KERNADDR(sc->ptb);
2136 LIST_FOREACH(mem, &sc->mem, next) {
2137 if (KERNADDR(mem->dmamem) == ptr) {
2138 struct dmamem *dm = mem->dmamem;
2139
2140 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs,
2141 off, prot, BUS_DMA_WAITOK);
2142 }
2143 }
2144
2145 return (-1);
2146 }
2147
2148 static int
2149 emuxki_get_props(void *addr)
2150 {
2151 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
2152 AUDIO_PROP_FULLDUPLEX);
2153 }
2154
2155 static int
2156 emuxki_trigger_output(void *addr, void *start, void *end, int blksize,
2157 void (*inth) (void *), void *inthparam,
2158 struct audio_params *params)
2159 {
2160 struct emuxki_softc *sc = addr;
2161 /* No multiple voice support for now */
2162 struct emuxki_voice *voice = sc->pvoice;
2163 int error;
2164
2165 if (voice == NULL)
2166 return (ENXIO);
2167 if ((error = emuxki_set_vparms(voice, params)))
2168 return (error);
2169 if ((error = emuxki_voice_set_bufparms(voice, start,
2170 (caddr_t)end - (caddr_t)start, blksize)))
2171 return (error);
2172 emuxki_voice_commit_parms(voice);
2173 emuxki_voice_start(voice, inth, inthparam);
2174
2175 return (0);
2176 }
2177
2178 static int
2179 emuxki_trigger_input(void *addr, void *start, void *end, int blksize,
2180 void (*inth) (void *), void *inthparam,
2181 struct audio_params *params)
2182 {
2183 struct emuxki_softc *sc = addr;
2184 /* No multiple voice support for now */
2185 struct emuxki_voice *voice = sc->rvoice;
2186 int error;
2187
2188 if (voice == NULL)
2189 return (ENXIO);
2190 if ((error = emuxki_set_vparms(voice, params)))
2191 return (error);
2192 if ((error = emuxki_voice_set_bufparms(voice, start,
2193 (caddr_t)end - (caddr_t)start,
2194 blksize)))
2195 return (error);
2196 emuxki_voice_start(voice, inth, inthparam);
2197
2198 return (0);
2199 }
2200
2201
2202 /*
2203 * AC97 callbacks
2204 */
2205
2206 static int
2207 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif)
2208 {
2209 struct emuxki_softc *sc = arg;
2210
2211 sc->codecif = codecif;
2212 return (0);
2213 }
2214
2215 static int
2216 emuxki_ac97_read(void *arg, u_int8_t reg, u_int16_t *val)
2217 {
2218 struct emuxki_softc *sc = arg;
2219 int s;
2220
2221 s = splaudio();
2222 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2223 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA);
2224 splx(s);
2225
2226 return (0);
2227 }
2228
2229 static int
2230 emuxki_ac97_write(void *arg, u_int8_t reg, u_int16_t val)
2231 {
2232 struct emuxki_softc *sc = arg;
2233 int s;
2234
2235 s = splaudio();
2236 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2237 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val);
2238 splx(s);
2239
2240 return (0);
2241 }
2242
2243 static void
2244 emuxki_ac97_reset(void *arg)
2245 {
2246 }
2247
2248 enum ac97_host_flags
2249 emuxki_ac97_flags(void *arg)
2250 {
2251 return (AC97_HOST_SWAPPED_CHANNELS);
2252 }
2253