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