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