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