emuxki.c revision 1.17 1 /* $NetBSD: emuxki.c,v 1.17 2003/02/18 18:47:06 wiz 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.17 2003/02/18 18:47:06 wiz 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, struct malloc_type *, int);
90 static void dmamem_free(struct dmamem *, struct malloc_type *);
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,
98 struct malloc_type *,int);
99 static void *emuxki_rmem_alloc(struct emuxki_softc *, size_t,
100 struct malloc_type *,int);
101
102 /*
103 * Emu10k1 channels funcs : There is no direct access to channels, everything
104 * is done through voices I will at least provide channel based fx params
105 * modification, later...
106 */
107
108 /* Emu10k1 voice mgmt */
109 static struct emuxki_voice *emuxki_voice_new(struct emuxki_softc *,
110 u_int8_t);
111 static void emuxki_voice_delete(struct emuxki_voice *);
112 static int emuxki_voice_set_audioparms(struct emuxki_voice *, u_int8_t,
113 u_int8_t, u_int32_t);
114 /* emuxki_voice_set_fxparms will come later, it'll need channel distinction */
115 static int emuxki_voice_set_bufparms(struct emuxki_voice *,
116 void *, u_int32_t, u_int16_t);
117 static void emuxki_voice_commit_parms(struct emuxki_voice *);
118 static u_int32_t emuxki_voice_curaddr(struct emuxki_voice *);
119 static void emuxki_voice_start(struct emuxki_voice *,
120 void (*) (void *), void *);
121 static void emuxki_voice_halt(struct emuxki_voice *);
122
123 /*
124 * Emu10k1 stream mgmt : not done yet
125 */
126 #if 0
127 static struct emuxki_stream *emuxki_stream_new(struct emu10k1 *);
128 static void emuxki_stream_delete(struct emuxki_stream *);
129 static int emuxki_stream_set_audio_params(struct emuxki_stream *, u_int8_t,
130 u_int8_t, u_int8_t, u_int16_t);
131 static void emuxki_stream_start(struct emuxki_stream *);
132 static void emuxki_stream_halt(struct emuxki_stream *);
133 #endif
134
135 /* audio interface callbacks */
136
137 static int emuxki_open(void *, int);
138 static void emuxki_close(void *);
139
140 static int emuxki_query_encoding(void *, struct audio_encoding *);
141 static int emuxki_set_params(void *, int, int,
142 struct audio_params *,
143 struct audio_params *);
144
145 static size_t emuxki_round_buffersize(void *, int, size_t);
146
147 static int emuxki_trigger_output(void *, void *, void *, int,
148 void (*)(void *), void *,
149 struct audio_params *);
150 static int emuxki_trigger_input(void *, void *, void *, int,
151 void (*) (void *), void *,
152 struct audio_params *);
153 static int emuxki_halt_output(void *);
154 static int emuxki_halt_input(void *);
155
156 static int emuxki_getdev(void *, struct audio_device *);
157 static int emuxki_set_port(void *, mixer_ctrl_t *);
158 static int emuxki_get_port(void *, mixer_ctrl_t *);
159 static int emuxki_query_devinfo(void *, mixer_devinfo_t *);
160
161 static void *emuxki_allocm(void *, int, size_t, struct malloc_type *, int);
162 static void emuxki_freem(void *, void *, struct malloc_type *);
163
164 static paddr_t emuxki_mappage(void *, void *, off_t, int);
165 static int emuxki_get_props(void *);
166
167 /* Interrupt handler */
168 static int emuxki_intr(void *);
169
170 /* Emu10k1 AC97 interface callbacks */
171 static int emuxki_ac97_attach(void *, struct ac97_codec_if *);
172 static int emuxki_ac97_read(void *, u_int8_t, u_int16_t *);
173 static int emuxki_ac97_write(void *, u_int8_t, u_int16_t);
174 static void emuxki_ac97_reset(void *);
175 static enum ac97_host_flags emuxki_ac97_flags(void *);
176
177 /*
178 * Autoconfig goo.
179 */
180 CFATTACH_DECL(emuxki, sizeof(struct emuxki_softc),
181 emuxki_match, emuxki_attach, emuxki_detach, NULL);
182
183 static struct audio_hw_if emuxki_hw_if = {
184 emuxki_open,
185 emuxki_close,
186 NULL, /* drain */
187 emuxki_query_encoding,
188 emuxki_set_params,
189 NULL, /* round blocksize */
190 NULL, /* commit settings */
191 NULL, /* init_output */
192 NULL, /* init_input */
193 NULL, /* start_output */
194 NULL, /* start_input */
195 emuxki_halt_output,
196 emuxki_halt_input,
197 NULL, /* speaker_ctl */
198 emuxki_getdev,
199 NULL, /* setfd */
200 emuxki_set_port,
201 emuxki_get_port,
202 emuxki_query_devinfo,
203 emuxki_allocm,
204 emuxki_freem,
205 emuxki_round_buffersize,
206 emuxki_mappage,
207 emuxki_get_props,
208 emuxki_trigger_output,
209 emuxki_trigger_input,
210 NULL, /* dev_ioctl */
211 };
212
213 /*
214 * Dma memory mgmt
215 */
216
217 static void
218 dmamem_delete(struct dmamem *mem, struct malloc_type *type)
219 {
220 free(mem->segs, type);
221 free(mem, type);
222 }
223
224 static struct dmamem *
225 dmamem_alloc(bus_dma_tag_t dmat, size_t size, bus_size_t align,
226 int nsegs, struct malloc_type *type, int flags)
227 {
228 struct dmamem *mem;
229 int bus_dma_flags;
230
231 /* Allocate memory for structure */
232 if ((mem = malloc(sizeof(*mem), type, flags)) == NULL)
233 return (NULL);
234 mem->dmat = dmat;
235 mem->size = size;
236 mem->align = align;
237 mem->nsegs = nsegs;
238 mem->bound = 0;
239
240 mem->segs = malloc(mem->nsegs * sizeof(*(mem->segs)), type, flags);
241 if (mem->segs == NULL) {
242 free(mem, type);
243 return (NULL);
244 }
245
246 bus_dma_flags = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
247 if (bus_dmamem_alloc(dmat, mem->size, mem->align, mem->bound,
248 mem->segs, mem->nsegs, &(mem->rsegs),
249 bus_dma_flags)) {
250 dmamem_delete(mem, type);
251 return (NULL);
252 }
253
254 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, mem->size,
255 &(mem->kaddr), bus_dma_flags | BUS_DMA_COHERENT)) {
256 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
257 dmamem_delete(mem, type);
258 return (NULL);
259 }
260
261 if (bus_dmamap_create(dmat, mem->size, mem->nsegs, mem->size,
262 mem->bound, bus_dma_flags, &(mem->map))) {
263 bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
264 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
265 dmamem_delete(mem, type);
266 return (NULL);
267 }
268
269 if (bus_dmamap_load(dmat, mem->map, mem->kaddr,
270 mem->size, NULL, bus_dma_flags)) {
271 bus_dmamap_destroy(dmat, mem->map);
272 bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
273 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
274 dmamem_delete(mem, type);
275 return (NULL);
276 }
277
278 return (mem);
279 }
280
281 static void
282 dmamem_free(struct dmamem *mem, struct malloc_type *type)
283 {
284 bus_dmamap_unload(mem->dmat, mem->map);
285 bus_dmamap_destroy(mem->dmat, mem->map);
286 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
287 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
288 dmamem_delete(mem, type);
289 }
290
291
292 /*
293 * Autoconf device callbacks : attach and detach
294 */
295
296 static void
297 emuxki_pci_shutdown(struct emuxki_softc *sc)
298 {
299 if (sc->sc_ih != NULL)
300 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
301 if (sc->sc_ios)
302 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
303 }
304
305 static int
306 emuxki_scinit(struct emuxki_softc *sc)
307 {
308 int err;
309
310 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
311 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK |
312 EMU_HCFG_MUTEBUTTONENABLE);
313 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
314 EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE);
315
316 if ((err = emuxki_init(sc)))
317 return (err);
318
319 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
320 EMU_HCFG_AUDIOENABLE | EMU_HCFG_JOYENABLE |
321 EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_AUTOMUTE);
322 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
323 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
324 EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE |
325 EMU_INTE_MUTEENABLE);
326
327 /* No multiple voice support for now */
328 sc->pvoice = sc->rvoice = NULL;
329
330 return (0);
331 }
332
333 static int
334 emuxki_ac97_init(struct emuxki_softc *sc)
335 {
336 sc->hostif.arg = sc;
337 sc->hostif.attach = emuxki_ac97_attach;
338 sc->hostif.read = emuxki_ac97_read;
339 sc->hostif.write = emuxki_ac97_write;
340 sc->hostif.reset = emuxki_ac97_reset;
341 sc->hostif.flags = emuxki_ac97_flags;
342 return (ac97_attach(&(sc->hostif)));
343 }
344
345 static int
346 emuxki_match(struct device *parent, struct cfdata *match, void *aux)
347 {
348 struct pci_attach_args *pa = aux;
349
350 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CREATIVELABS &&
351 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBLIVE)
352 return (1);
353
354 return (0);
355 }
356
357 static void
358 emuxki_attach(struct device *parent, struct device *self, void *aux)
359 {
360 struct emuxki_softc *sc = (struct emuxki_softc *) self;
361 struct pci_attach_args *pa = aux;
362 char devinfo[256];
363 pci_intr_handle_t ih;
364 const char *intrstr;
365
366 aprint_naive(": Audio controller\n");
367
368 if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
369 &(sc->sc_iot), &(sc->sc_ioh), &(sc->sc_iob),
370 &(sc->sc_ios))) {
371 aprint_error(": can't map iospace\n");
372 return;
373 }
374 pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
375 aprint_normal(": %s\n", devinfo);
376
377 sc->sc_pc = pa->pa_pc;
378 sc->sc_dmat = pa->pa_dmat;
379 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
380 pci_conf_read(pa->pa_pc, pa->pa_tag,
381 (PCI_COMMAND_STATUS_REG) | PCI_COMMAND_MASTER_ENABLE));
382
383 if (pci_intr_map(pa, &ih)) {
384 aprint_error("%s: couldn't map interrupt\n",
385 sc->sc_dev.dv_xname);
386 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
387 return;
388 }
389
390 intrstr = pci_intr_string(pa->pa_pc, ih);
391 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, emuxki_intr,
392 sc);
393 if (sc->sc_ih == NULL) {
394 aprint_error("%s: couldn't establish interrupt",
395 sc->sc_dev.dv_xname);
396 if (intrstr != NULL)
397 aprint_normal(" at %s", intrstr);
398 aprint_normal("\n");
399 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
400 return;
401 }
402 aprint_normal("%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, struct malloc_type *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, struct malloc_type *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,
811 struct malloc_type *type, int flags)
812 {
813 int i, j, s;
814 size_t numblocks;
815 struct emuxki_mem *mem;
816 u_int32_t *ptb, silentpage;
817
818 ptb = KERNADDR(sc->ptb);
819 silentpage = DMAADDR(sc->silentpage) << 1;
820 numblocks = size / EMU_PTESIZE;
821 if (size % EMU_PTESIZE)
822 numblocks++;
823
824 for (i = 0; i < EMU_MAXPTE; i++)
825 if ((ptb[i] & EMU_CHAN_MAP_PTE_MASK) == silentpage) {
826 /* We look for a free PTE */
827 s = splaudio();
828 for (j = 0; j < numblocks; j++)
829 if ((ptb[i + j] & EMU_CHAN_MAP_PTE_MASK)
830 != silentpage)
831 break;
832 if (j == numblocks) {
833 if ((mem = emuxki_mem_new(sc, i,
834 size, type, flags)) == NULL) {
835 splx(s);
836 return (NULL);
837 }
838 for (j = 0; j < numblocks; j++)
839 ptb[i + j] =
840 (((DMAADDR(mem->dmamem) +
841 j * EMU_PTESIZE)) << 1)
842 | (i + j);
843 LIST_INSERT_HEAD(&(sc->mem), mem, next);
844 splx(s);
845 return (KERNADDR(mem->dmamem));
846 } else
847 i += j;
848 splx(s);
849 }
850 return (NULL);
851 }
852
853 static void *
854 emuxki_rmem_alloc(struct emuxki_softc *sc, size_t size,
855 struct malloc_type *type, int flags)
856 {
857 struct emuxki_mem *mem;
858 int s;
859
860 mem = emuxki_mem_new(sc, EMU_RMEM, size, type, flags);
861 if (mem == NULL)
862 return (NULL);
863
864 s = splaudio();
865 LIST_INSERT_HEAD(&(sc->mem), mem, next);
866 splx(s);
867
868 return (KERNADDR(mem->dmamem));
869 }
870
871 /*
872 * emuxki_channel_* : Channel managment functions
873 * emuxki_chanparms_* : Channel parameters modification functions
874 */
875
876 /*
877 * is splaudio necessary here, can the same voice be manipulated by two
878 * different threads at a time ?
879 */
880 static void
881 emuxki_chanparms_set_defaults(struct emuxki_channel *chan)
882 {
883 chan->fxsend.a.level = chan->fxsend.b.level =
884 chan->fxsend.c.level = chan->fxsend.d.level = 0xc0; /* not max */
885 chan->fxsend.a.dest = 0x0;
886 chan->fxsend.b.dest = 0x1;
887 chan->fxsend.c.dest = 0x2;
888 chan->fxsend.d.dest = 0x3;
889
890 chan->pitch.initial = 0x0000; /* shouldn't it be 0xE000 ? */
891 chan->pitch.current = 0x0000; /* should it be 0x0400 */
892 chan->pitch.target = 0x0000; /* the unity pitch shift ? */
893 chan->pitch.envelope_amount = 0x00; /* none */
894
895 chan->initial_attenuation = 0x00; /* no attenuation */
896 chan->volume.current = 0x0000; /* no volume */
897 chan->volume.target = 0xffff;
898 chan->volume.envelope.current_state = 0x8000; /* 0 msec delay */
899 chan->volume.envelope.hold_time = 0x7f; /* 0 msec */
900 chan->volume.envelope.attack_time = 0x7F; /* 5.5msec */
901 chan->volume.envelope.sustain_level = 0x7F; /* full */
902 chan->volume.envelope.decay_time = 0x7F; /* 22msec */
903
904 chan->filter.initial_cutoff_frequency = 0xff; /* no filter */
905 chan->filter.current_cutoff_frequency = 0xffff; /* no filtering */
906 chan->filter.target_cutoff_frequency = 0xffff; /* no filtering */
907 chan->filter.lowpass_resonance_height = 0x0;
908 chan->filter.interpolation_ROM = 0x1; /* full band */
909 chan->filter.envelope_amount = 0x7f; /* none */
910 chan->filter.LFO_modulation_depth = 0x00; /* none */
911
912 chan->loop.start = 0x000000;
913 chan->loop.end = 0x000010; /* Why ? */
914
915 chan->modulation.envelope.current_state = 0x8000;
916 chan->modulation.envelope.hold_time = 0x00; /* 127 better ? */
917 chan->modulation.envelope.attack_time = 0x00; /* infinite */
918 chan->modulation.envelope.sustain_level = 0x00; /* off */
919 chan->modulation.envelope.decay_time = 0x7f; /* 22 msec */
920 chan->modulation.LFO_state = 0x8000;
921
922 chan->vibrato_LFO.state = 0x8000;
923 chan->vibrato_LFO.modulation_depth = 0x00; /* none */
924 chan->vibrato_LFO.vibrato_depth = 0x00;
925 chan->vibrato_LFO.frequency = 0x00; /* Why set to 24 when
926 * initialized ? */
927
928 chan->tremolo_depth = 0x00;
929 }
930
931 /* only call it at splaudio */
932 static struct emuxki_channel *
933 emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num)
934 {
935 struct emuxki_channel *chan;
936
937 chan = malloc(sizeof(struct emuxki_channel), M_DEVBUF, M_WAITOK);
938 if (chan == NULL)
939 return (NULL);
940
941 chan->voice = voice;
942 chan->num = num;
943 emuxki_chanparms_set_defaults(chan);
944 chan->voice->sc->channel[num] = chan;
945 return (chan);
946 }
947
948 /* only call it at splaudio */
949 static void
950 emuxki_channel_delete(struct emuxki_channel *chan)
951 {
952 chan->voice->sc->channel[chan->num] = NULL;
953 free(chan, M_DEVBUF);
954 }
955
956 static void
957 emuxki_channel_set_fxsend(struct emuxki_channel *chan,
958 struct emuxki_chanparms_fxsend *fxsend)
959 {
960 /* Could do a memcpy ...*/
961 chan->fxsend.a.level = fxsend->a.level;
962 chan->fxsend.b.level = fxsend->b.level;
963 chan->fxsend.c.level = fxsend->c.level;
964 chan->fxsend.d.level = fxsend->d.level;
965 chan->fxsend.a.dest = fxsend->a.dest;
966 chan->fxsend.b.dest = fxsend->b.dest;
967 chan->fxsend.c.dest = fxsend->c.dest;
968 chan->fxsend.d.dest = fxsend->d.dest;
969 }
970
971 static void
972 emuxki_channel_set_srate(struct emuxki_channel *chan, u_int32_t srate)
973 {
974 chan->pitch.target = (srate << 8) / 375;
975 chan->pitch.target = (chan->pitch.target >> 1) +
976 (chan->pitch.target & 1);
977 chan->pitch.target &= 0xffff;
978 chan->pitch.current = chan->pitch.target;
979 chan->pitch.initial =
980 (emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK;
981 }
982
983 /* voice params must be set before calling this */
984 static void
985 emuxki_channel_set_bufparms(struct emuxki_channel *chan,
986 u_int32_t start, u_int32_t end)
987 {
988 u_int8_t shift;
989 struct emuxki_voice *voice = chan->voice;
990
991 shift = voice->stereo + voice->b16;
992 chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK;
993 chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK;
994 }
995
996 static void
997 emuxki_channel_commit_parms(struct emuxki_channel *chan)
998 {
999 struct emuxki_voice *voice = chan->voice;
1000 struct emuxki_softc *sc = voice->sc;
1001 u_int32_t start, mapval;
1002 u_int8_t chano = chan->num;
1003 int s;
1004
1005 start = chan->loop.start +
1006 (voice->stereo ? 28 : 30) * (voice->b16 + 1);
1007 mapval = DMAADDR(sc->silentpage) << 1 | EMU_CHAN_MAP_PTI_MASK;
1008
1009 s = splaudio();
1010 emuxki_write(sc, chano, EMU_CHAN_CPF_STEREO, voice->stereo);
1011 emuxki_write(sc, chano, EMU_CHAN_FXRT,
1012 (chan->fxsend.d.dest << 28) | (chan->fxsend.c.dest << 24) |
1013 (chan->fxsend.b.dest << 20) | (chan->fxsend.a.dest << 16));
1014 emuxki_write(sc, chano, 0x10000000 | EMU_CHAN_PTRX,
1015 (chan->fxsend.a.level << 8) | chan->fxsend.b.level);
1016 emuxki_write(sc, chano, EMU_CHAN_DSL,
1017 (chan->fxsend.d.level << 24) | chan->loop.end);
1018 emuxki_write(sc, chano, EMU_CHAN_PSST,
1019 (chan->fxsend.c.level << 24) | chan->loop.start);
1020 emuxki_write(sc, chano, EMU_CHAN_CCCA,
1021 (chan->filter.lowpass_resonance_height << 28) |
1022 (chan->filter.interpolation_ROM << 25) |
1023 (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start);
1024 emuxki_write(sc, chano, EMU_CHAN_Z1, 0);
1025 emuxki_write(sc, chano, EMU_CHAN_Z2, 0);
1026 emuxki_write(sc, chano, EMU_CHAN_MAPA, mapval);
1027 emuxki_write(sc, chano, EMU_CHAN_MAPB, mapval);
1028 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRFILTER,
1029 chan->filter.current_cutoff_frequency);
1030 emuxki_write(sc, chano, EMU_CHAN_VTFT_FILTERTARGET,
1031 chan->filter.target_cutoff_frequency);
1032 emuxki_write(sc, chano, EMU_CHAN_ATKHLDM,
1033 (chan->modulation.envelope.hold_time << 8) |
1034 chan->modulation.envelope.attack_time);
1035 emuxki_write(sc, chano, EMU_CHAN_DCYSUSM,
1036 (chan->modulation.envelope.sustain_level << 8) |
1037 chan->modulation.envelope.decay_time);
1038 emuxki_write(sc, chano, EMU_CHAN_LFOVAL1,
1039 chan->modulation.LFO_state);
1040 emuxki_write(sc, chano, EMU_CHAN_LFOVAL2,
1041 chan->vibrato_LFO.state);
1042 emuxki_write(sc, chano, EMU_CHAN_FMMOD,
1043 (chan->vibrato_LFO.modulation_depth << 8) |
1044 chan->filter.LFO_modulation_depth);
1045 emuxki_write(sc, chano, EMU_CHAN_TREMFRQ,
1046 (chan->tremolo_depth << 8));
1047 emuxki_write(sc, chano, EMU_CHAN_FM2FRQ2,
1048 (chan->vibrato_LFO.vibrato_depth << 8) |
1049 chan->vibrato_LFO.frequency);
1050 emuxki_write(sc, chano, EMU_CHAN_ENVVAL,
1051 chan->modulation.envelope.current_state);
1052 emuxki_write(sc, chano, EMU_CHAN_ATKHLDV,
1053 (chan->volume.envelope.hold_time << 8) |
1054 chan->volume.envelope.attack_time);
1055 emuxki_write(sc, chano, EMU_CHAN_ENVVOL,
1056 chan->volume.envelope.current_state);
1057 emuxki_write(sc, chano, EMU_CHAN_PEFE,
1058 (chan->pitch.envelope_amount << 8) |
1059 chan->filter.envelope_amount);
1060 splx(s);
1061 }
1062
1063 static void
1064 emuxki_channel_start(struct emuxki_channel *chan)
1065 {
1066 struct emuxki_voice *voice = chan->voice;
1067 struct emuxki_softc *sc = voice->sc;
1068 u_int8_t cache_sample, cache_invalid_size, chano = chan->num;
1069 u_int32_t sample;
1070 int s;
1071
1072 cache_sample = voice->stereo ? 4 : 2;
1073 sample = voice->b16 ? 0x00000000 : 0x80808080;
1074 cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1);
1075
1076 s = splaudio();
1077 while (cache_sample--) {
1078 emuxki_write(sc, chano, EMU_CHAN_CD0 + cache_sample,
1079 sample);
1080 }
1081 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
1082 emuxki_write(sc, chano, EMU_CHAN_CCR_READADDRESS, 64);
1083 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE,
1084 cache_invalid_size);
1085 emuxki_write(sc, chano, EMU_CHAN_IFATN,
1086 (chan->filter.target_cutoff_frequency << 8) |
1087 chan->initial_attenuation);
1088 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET,
1089 chan->volume.target);
1090 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL,
1091 chan->volume.current);
1092 emuxki_write(sc, 0,
1093 EMU_MKSUBREG(1, chano, EMU_SOLEL + (chano >> 5)),
1094 0); /* Clear stop on loop */
1095 emuxki_write(sc, 0,
1096 EMU_MKSUBREG(1, chano, EMU_CLIEL + (chano >> 5)),
1097 0); /* Clear loop interrupt */
1098 emuxki_write(sc, chano, EMU_CHAN_DCYSUSV,
1099 (chan->volume.envelope.sustain_level << 8) |
1100 chan->volume.envelope.decay_time);
1101 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET,
1102 chan->pitch.target);
1103 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH,
1104 chan->pitch.current);
1105 emuxki_write(sc, chano, EMU_CHAN_IP, chan->pitch.initial);
1106
1107 splx(s);
1108 }
1109
1110 static void
1111 emuxki_channel_stop(struct emuxki_channel *chan)
1112 {
1113 int s;
1114 u_int8_t chano = chan->num;
1115 struct emuxki_softc *sc = chan->voice->sc;
1116
1117 s = splaudio();
1118 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 0);
1119 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 0);
1120 emuxki_write(sc, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff);
1121 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0);
1122 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 0);
1123 emuxki_write(sc, chano, EMU_CHAN_IP, 0);
1124 splx(s);
1125 }
1126
1127 /*
1128 * Voices managment
1129 * emuxki_voice_dataloc : use(play or rec) independant dataloc union helpers
1130 * emuxki_voice_channel_* : play part of dataloc union helpers
1131 * emuxki_voice_recsrc_* : rec part of dataloc union helpers
1132 */
1133
1134 /* Allocate channels for voice in case of play voice */
1135 static int
1136 emuxki_voice_channel_create(struct emuxki_voice *voice)
1137 {
1138 struct emuxki_channel **channel = voice->sc->channel;
1139 u_int8_t i, stereo = voice->stereo;
1140 int s;
1141
1142 for (i = 0; i < EMU_NUMCHAN; i += stereo + 1) {
1143 if ((stereo && (channel[i + 1] != NULL)) ||
1144 (channel[i] != NULL)) /* Looking for free channels */
1145 continue;
1146 s = splaudio();
1147 if (stereo) {
1148 voice->dataloc.chan[1] =
1149 emuxki_channel_new(voice, i + 1);
1150 if (voice->dataloc.chan[1] == NULL) {
1151 splx(s);
1152 return (ENOMEM);
1153 }
1154 }
1155 voice->dataloc.chan[0] = emuxki_channel_new(voice, i);
1156 if (voice->dataloc.chan[0] == NULL) {
1157 if (stereo) {
1158 emuxki_channel_delete(voice->dataloc.chan[1]);
1159 voice->dataloc.chan[1] = NULL;
1160 }
1161 splx(s);
1162 return (ENOMEM);
1163 }
1164 splx(s);
1165 return (0);
1166 }
1167 return (EAGAIN);
1168 }
1169
1170 /* When calling this function we assume no one can access the voice */
1171 static void
1172 emuxki_voice_channel_destroy(struct emuxki_voice *voice)
1173 {
1174 emuxki_channel_delete(voice->dataloc.chan[0]);
1175 voice->dataloc.chan[0] = NULL;
1176 if (voice->stereo)
1177 emuxki_channel_delete(voice->dataloc.chan[1]);
1178 voice->dataloc.chan[1] = NULL;
1179 }
1180
1181 /*
1182 * Will come back when used in voice_dataloc_create
1183 */
1184 #if 0
1185 static int
1186 emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source)
1187 {
1188 if (voice->emu->recsrc[source] != NULL)
1189 return (EBUSY);
1190 voice->emu->recsrc[source] = voice;
1191 return (0);
1192 }
1193 #endif
1194
1195 /* When calling this function we assume the voice is stopped */
1196 static void
1197 emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source)
1198 {
1199 sc->recsrc[source] = NULL;
1200 }
1201
1202 static int
1203 emuxki_voice_dataloc_create(struct emuxki_voice *voice)
1204 {
1205 int error;
1206
1207 if (voice->use & EMU_VOICE_USE_PLAY) {
1208 if ((error = emuxki_voice_channel_create(voice)))
1209 return (error);
1210 } else {
1211 /*
1212 * Commented out because i don't know how to get the selected
1213 * recording source
1214 */
1215 #if 0
1216 if (emuxki_recsrc_reserve(voice, recsrc))
1217 return (EBUSY);
1218 printf("Which rec src do i have to create!!!\n");
1219 #endif
1220 return (EBUSY); /* just return an error, no real meaning */
1221 }
1222 return (0);
1223 }
1224
1225 static void
1226 emuxki_voice_dataloc_destroy(struct emuxki_voice *voice)
1227 {
1228 if (voice->use & EMU_VOICE_USE_PLAY) {
1229 if (voice->dataloc.chan[0] != NULL)
1230 emuxki_voice_channel_destroy(voice);
1231 } else {
1232 if (voice->dataloc.source != EMU_RECSRC_NOTSET) {
1233 emuxki_voice_recsrc_release(voice->sc,
1234 voice->dataloc.source);
1235 voice->dataloc.source = EMU_RECSRC_NOTSET;
1236 }
1237 }
1238 }
1239
1240 static struct emuxki_voice *
1241 emuxki_voice_new(struct emuxki_softc *sc, u_int8_t use)
1242 {
1243 struct emuxki_voice *voice;
1244 int s;
1245
1246 s = splaudio();
1247 voice = sc->lvoice;
1248 sc->lvoice = NULL;
1249 splx(s);
1250
1251 if (!voice) {
1252 if (!(voice = malloc(sizeof(*voice), M_DEVBUF, M_WAITOK)))
1253 return (NULL);
1254 voice->sc = sc;
1255 voice->state = !EMU_VOICE_STATE_STARTED;
1256 voice->stereo = EMU_VOICE_STEREO_NOTSET;
1257 voice->b16 = 0;
1258 voice->sample_rate = 0;
1259 if (use & EMU_VOICE_USE_PLAY)
1260 voice->dataloc.chan[0] = voice->dataloc.chan[0] = NULL;
1261 else
1262 voice->dataloc.source = EMU_RECSRC_NOTSET;
1263 voice->buffer = NULL;
1264 voice->blksize = 0;
1265 voice->trigblk = 0;
1266 voice->blkmod = 0;
1267 voice->inth = NULL;
1268 voice->inthparam = NULL;
1269 }
1270 voice->use = use;
1271
1272 s = splaudio();
1273 LIST_INSERT_HEAD((&sc->voices), voice, next);
1274 splx(s);
1275
1276 return (voice);
1277 }
1278
1279 static void
1280 emuxki_voice_delete(struct emuxki_voice *voice)
1281 {
1282 struct emuxki_softc *sc = voice->sc;
1283 struct emuxki_voice *lvoice;
1284 int s;
1285
1286 if (voice->state & EMU_VOICE_STATE_STARTED)
1287 emuxki_voice_halt(voice);
1288
1289 s = splaudio();
1290 LIST_REMOVE(voice, next);
1291 lvoice = sc->lvoice;
1292 sc->lvoice = voice;
1293 splx(s);
1294
1295 if (lvoice) {
1296 emuxki_voice_dataloc_destroy(lvoice);
1297 free(lvoice, M_DEVBUF);
1298 }
1299 }
1300
1301 static int
1302 emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo)
1303 {
1304 int error;
1305 struct emuxki_chanparms_fxsend fxsend;
1306
1307 emuxki_voice_dataloc_destroy(voice);
1308 voice->stereo = stereo;
1309 if ((error = emuxki_voice_dataloc_create(voice)))
1310 return (error);
1311 if (voice->use & EMU_VOICE_USE_PLAY) {
1312 fxsend.a.dest = 0x0;
1313 fxsend.b.dest = 0x1;
1314 fxsend.c.dest = 0x2;
1315 fxsend.d.dest = 0x3;
1316 if (voice->stereo) {
1317 fxsend.a.level = fxsend.c.level = 0xc0;
1318 fxsend.b.level = fxsend.d.level = 0x00;
1319 emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1320 &fxsend);
1321 fxsend.a.level = fxsend.c.level = 0x00;
1322 fxsend.b.level = fxsend.d.level = 0xc0;
1323 emuxki_channel_set_fxsend(voice->dataloc.chan[1],
1324 &fxsend);
1325 } /* No else : default is good for mono */
1326 }
1327 return (0);
1328 }
1329
1330 static int
1331 emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate)
1332 {
1333 if (voice->use & EMU_VOICE_USE_PLAY) {
1334 if ((srate < 4000) || (srate > 48000))
1335 return (EINVAL);
1336 voice->sample_rate = srate;
1337 emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1338 if (voice->stereo)
1339 emuxki_channel_set_srate(voice->dataloc.chan[1],
1340 srate);
1341 } else {
1342 #ifdef EMUXKI_DEBUG
1343 printf("Recording voice set_srate not implemented\n");
1344 #endif
1345 return (EINVAL);
1346 }
1347 return (0);
1348 }
1349
1350 static int
1351 emuxki_voice_set_audioparms(struct emuxki_voice *voice, u_int8_t stereo,
1352 u_int8_t b16, u_int32_t srate)
1353 {
1354 int error;
1355
1356 if (voice->stereo == stereo && voice->b16 == b16 &&
1357 voice->sample_rate == srate)
1358 return (0);
1359
1360 #ifdef EMUXKI_DEBUG
1361 printf("Setting %s voice params : %s, %u bits, %u hz\n",
1362 (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record",
1363 stereo ? "stereo" : "mono", (b16 + 1) * 8, srate);
1364 #endif
1365
1366 if (voice->stereo != stereo) {
1367 if ((error = emuxki_voice_set_stereo(voice, stereo)))
1368 return (error);
1369 }
1370 voice->b16 = b16;
1371 if (voice->sample_rate != srate)
1372 emuxki_voice_set_srate(voice, srate);
1373 return (0);
1374 }
1375
1376 /* voice audio parms (see just before) must be set prior to this */
1377 static int
1378 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr,
1379 u_int32_t bufsize, u_int16_t blksize)
1380 {
1381 struct emuxki_mem *mem;
1382 struct emuxki_channel **chan;
1383 u_int32_t start, end;
1384 u_int8_t sample_size;
1385 int error = EFAULT;
1386
1387 LIST_FOREACH(mem, &voice->sc->mem, next) {
1388 if (KERNADDR(mem->dmamem) != ptr)
1389 continue;
1390
1391 voice->buffer = mem;
1392 sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1393 voice->blksize = blksize / sample_size;
1394 voice->trigblk = 0; /* This shouldn't be needed */
1395 voice->blkmod = bufsize / blksize;
1396 if (bufsize % blksize) /* This should not happen */
1397 voice->blkmod++;
1398 error = 0;
1399
1400 if (voice->use & EMU_VOICE_USE_PLAY) {
1401 chan = voice->dataloc.chan;
1402 start = mem->ptbidx << 12;
1403 end = start + bufsize / sample_size;
1404 emuxki_channel_set_bufparms(chan[0],
1405 start, end);
1406 if (voice->stereo)
1407 emuxki_channel_set_bufparms(chan[1],
1408 start, end);
1409 voice->timerate = (u_int32_t) 48000 *
1410 voice->blksize / voice->sample_rate;
1411 if (voice->timerate < 5)
1412 error = EINVAL;
1413 } else {
1414 #ifdef EMUXKI_DEBUG
1415 printf("Rec voice set bufparms not implemented\n");
1416 #endif
1417 error = ENODEV;
1418 }
1419
1420 break;
1421 }
1422
1423 return (error);
1424 }
1425
1426 static void
1427 emuxki_voice_commit_parms(struct emuxki_voice *voice)
1428 {
1429 if (voice->use & EMU_VOICE_USE_PLAY) {
1430 emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1431 if (voice->stereo)
1432 emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1433 }
1434 }
1435
1436 static u_int32_t
1437 emuxki_voice_curaddr(struct emuxki_voice *voice)
1438 {
1439 if (voice->use & EMU_VOICE_USE_PLAY)
1440 return (emuxki_read(voice->sc,
1441 voice->dataloc.chan[0]->num,
1442 EMU_CHAN_CCCA_CURRADDR) -
1443 voice->dataloc.chan[0]->loop.start);
1444 return (0);
1445 }
1446
1447 static void
1448 emuxki_resched_timer(struct emuxki_softc *sc)
1449 {
1450 struct emuxki_voice *voice;
1451 u_int16_t timerate = 1024;
1452 u_int8_t active = 0;
1453 int s;
1454
1455 s = splaudio();
1456 LIST_FOREACH(voice, &sc->voices, next) {
1457 if ((voice->use & EMU_VOICE_USE_PLAY) == 0 ||
1458 (voice->state & EMU_VOICE_STATE_STARTED) == 0)
1459 continue;
1460 active = 1;
1461 if (voice->timerate < timerate)
1462 timerate = voice->timerate;
1463 }
1464
1465 if (timerate & ~EMU_TIMER_RATE_MASK)
1466 timerate = 0;
1467 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_TIMER, timerate);
1468 if (!active && (sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1469 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1470 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) &
1471 ~EMU_INTE_INTERTIMERENB);
1472 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED;
1473 } else if (active && !(sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1474 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1475 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
1476 EMU_INTE_INTERTIMERENB);
1477 sc->timerstate |= EMU_TIMER_STATE_ENABLED;
1478 }
1479 splx(s);
1480 }
1481
1482 static void
1483 emuxki_voice_start(struct emuxki_voice *voice,
1484 void (*inth) (void *), void *inthparam)
1485 {
1486 voice->inth = inth;
1487 voice->inthparam = inthparam;
1488 if (voice->use & EMU_VOICE_USE_PLAY) {
1489 voice->trigblk = 1;
1490 emuxki_channel_start(voice->dataloc.chan[0]);
1491 if (voice->stereo)
1492 emuxki_channel_start(voice->dataloc.chan[1]);
1493 }
1494 #ifdef EMUXKI_DEBUG
1495 else
1496 printf("Recording voice start not implemented\n");
1497 #endif
1498 voice->state |= EMU_VOICE_STATE_STARTED;
1499 if (voice->use & EMU_VOICE_USE_PLAY)
1500 emuxki_resched_timer(voice->sc);
1501 }
1502
1503 static void
1504 emuxki_voice_halt(struct emuxki_voice *voice)
1505 {
1506 if (voice->use & EMU_VOICE_USE_PLAY) {
1507 emuxki_channel_stop(voice->dataloc.chan[0]);
1508 if (voice->stereo)
1509 emuxki_channel_stop(voice->dataloc.chan[1]);
1510 }
1511 #ifdef EMUXKI_DEBUG
1512 else
1513 printf("Recording voice halt not implemented\n");
1514 #endif
1515 voice->state &= ~EMU_VOICE_STATE_STARTED;
1516 if (voice->use & EMU_VOICE_USE_PLAY)
1517 emuxki_resched_timer(voice->sc);
1518 }
1519
1520 /*
1521 * The interrupt handler
1522 */
1523 static int
1524 emuxki_intr(void *arg)
1525 {
1526 struct emuxki_softc *sc = arg;
1527 u_int32_t ipr, curblk;
1528 struct emuxki_voice *voice;
1529 int claim = 0;
1530
1531 while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) {
1532 if (ipr & EMU_IPR_INTERVALTIMER) {
1533 LIST_FOREACH(voice, &sc->voices, next) {
1534 if ((voice->use & EMU_VOICE_USE_PLAY)==0 ||
1535 (voice->state &
1536 EMU_VOICE_STATE_STARTED) == 0)
1537 continue;
1538
1539 curblk = emuxki_voice_curaddr(voice) /
1540 voice->blksize;
1541 if (curblk == voice->trigblk) {
1542 voice->inth(voice->inthparam);
1543 voice->trigblk++;
1544 voice->trigblk %= voice->blkmod;
1545 }
1546 }
1547 }
1548
1549 /* Got interrupt */
1550 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr);
1551
1552 claim = 1;
1553 }
1554
1555 return (claim);
1556 }
1557
1558
1559 /*
1560 * Audio Architecture callbacks
1561 */
1562
1563 static int
1564 emuxki_open(void *addr, int flags)
1565 {
1566 struct emuxki_softc *sc = addr;
1567
1568 #ifdef EMUXKI_DEBUG
1569 printf("%s: emuxki_open called\n", sc->sc_dev.dv_xname);
1570 #endif
1571
1572 /*
1573 * Multiple voice support would be added as soon as I find a way to
1574 * trick the audio arch into supporting multiple voices.
1575 * Or I might integrate a modified audio arch supporting
1576 * multiple voices.
1577 */
1578
1579 /*
1580 * I did this because i have problems identifying the selected
1581 * recording source(s) which is necessary when setting recording
1582 * params This will be adressed very soon
1583 */
1584 if (flags & AUOPEN_READ)
1585 return (EOPNOTSUPP);
1586
1587 if (flags & AUOPEN_WRITE) {
1588 sc->pvoice = emuxki_voice_new(sc, EMU_VOICE_USE_PLAY);
1589 if (sc->pvoice == NULL) {
1590 if (sc->rvoice) {
1591 emuxki_voice_delete(sc->rvoice);
1592 sc->rvoice = NULL;
1593 }
1594 return (EBUSY);
1595 }
1596 }
1597
1598 return (0);
1599 }
1600
1601 static void
1602 emuxki_close(void *addr)
1603 {
1604 struct emuxki_softc *sc = addr;
1605
1606 #ifdef EMUXKI_DEBUG
1607 printf("%s: emu10K1_close called\n", sc->sc_dev.dv_xname);
1608 #endif
1609
1610 /* No multiple voice support for now */
1611 if (sc->rvoice != NULL) {
1612 emuxki_voice_delete(sc->rvoice);
1613 sc->rvoice = NULL;
1614 }
1615 if (sc->pvoice != NULL) {
1616 emuxki_voice_delete(sc->pvoice);
1617 sc->pvoice = NULL;
1618 }
1619 }
1620
1621 static int
1622 emuxki_query_encoding(void *addr, struct audio_encoding *fp)
1623 {
1624 #ifdef EMUXKI_DEBUG
1625 struct emuxki_softc *sc = addr;
1626
1627 printf("%s: emuxki_query_encoding called\n", sc->sc_dev.dv_xname);
1628 #endif
1629
1630 switch (fp->index) {
1631 case 0:
1632 strcpy(fp->name, AudioEulinear);
1633 fp->encoding = AUDIO_ENCODING_ULINEAR;
1634 fp->precision = 8;
1635 fp->flags = 0;
1636 break;
1637 case 1:
1638 strcpy(fp->name, AudioEmulaw);
1639 fp->encoding = AUDIO_ENCODING_ULAW;
1640 fp->precision = 8;
1641 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1642 break;
1643 case 2:
1644 strcpy(fp->name, AudioEalaw);
1645 fp->encoding = AUDIO_ENCODING_ALAW;
1646 fp->precision = 8;
1647 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1648 break;
1649 case 3:
1650 strcpy(fp->name, AudioEslinear);
1651 fp->encoding = AUDIO_ENCODING_SLINEAR;
1652 fp->precision = 8;
1653 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1654 break;
1655 case 4:
1656 strcpy(fp->name, AudioEslinear_le);
1657 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
1658 fp->precision = 16;
1659 fp->flags = 0;
1660 break;
1661 case 5:
1662 strcpy(fp->name, AudioEulinear_le);
1663 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
1664 fp->precision = 16;
1665 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1666 break;
1667 case 6:
1668 strcpy(fp->name, AudioEslinear_be);
1669 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1670 fp->precision = 16;
1671 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1672 break;
1673 case 7:
1674 strcpy(fp->name, AudioEulinear_be);
1675 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1676 fp->precision = 16;
1677 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1678 break;
1679 default:
1680 return (EINVAL);
1681 }
1682 return (0);
1683 }
1684
1685 static int
1686 emuxki_set_vparms(struct emuxki_voice *voice, struct audio_params *p)
1687 {
1688 u_int8_t b16, mode;
1689
1690 mode = (voice->use & EMU_VOICE_USE_PLAY) ?
1691 AUMODE_PLAY : AUMODE_RECORD;
1692 p->factor = 1;
1693 p->sw_code = NULL;
1694 if (p->channels != 1 && p->channels != 2)
1695 return (EINVAL);/* Will change when streams come in use */
1696
1697 switch (p->encoding) {
1698 case AUDIO_ENCODING_ULAW:
1699 if (mode == AUMODE_PLAY) {
1700 p->factor = 2;
1701 p->sw_code = mulaw_to_slinear16_le;
1702 b16 = 1;
1703 } else {
1704 p->sw_code = ulinear8_to_mulaw;
1705 b16 = 0;
1706 }
1707 break;
1708
1709 case AUDIO_ENCODING_ALAW:
1710 if (mode == AUMODE_PLAY) {
1711 p->factor = 2;
1712 p->sw_code = alaw_to_slinear16_le;
1713 b16 = 1;
1714 } else {
1715 p->sw_code = ulinear8_to_alaw;
1716 b16 = 0;
1717 }
1718 break;
1719
1720 case AUDIO_ENCODING_SLINEAR_LE:
1721 if (p->precision == 8)
1722 p->sw_code = change_sign8;
1723 b16 = (p->precision == 16);
1724 break;
1725
1726 case AUDIO_ENCODING_ULINEAR_LE:
1727 if (p->precision == 16)
1728 p->sw_code = change_sign16_le;
1729 b16 = (p->precision == 16);
1730 break;
1731
1732 case AUDIO_ENCODING_SLINEAR_BE:
1733 if (p->precision == 16)
1734 p->sw_code = swap_bytes;
1735 else
1736 p->sw_code = change_sign8;
1737 b16 = (p->precision == 16);
1738 break;
1739
1740 case AUDIO_ENCODING_ULINEAR_BE:
1741 if (p->precision == 16) {
1742 if (mode == AUMODE_PLAY)
1743 p->sw_code = swap_bytes_change_sign16_le;
1744 else
1745 p->sw_code = change_sign16_swap_bytes_le;
1746 }
1747 b16 = (p->precision == 16);
1748 break;
1749
1750 default:
1751 return (EINVAL);
1752 }
1753
1754 return (emuxki_voice_set_audioparms(voice, p->channels == 2,
1755 b16, p->sample_rate));
1756 }
1757
1758 static int
1759 emuxki_set_params(void *addr, int setmode, int usemode,
1760 struct audio_params *play, struct audio_params *rec)
1761 {
1762 struct emuxki_softc *sc = addr;
1763 int mode, error;
1764 struct audio_params *p;
1765 struct emuxki_voice *v;
1766
1767 for (mode = AUMODE_RECORD; mode != -1;
1768 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1769 if ((usemode & setmode & mode) == 0)
1770 continue;
1771
1772 if (mode == AUMODE_PLAY) {
1773 p = play;
1774 v = sc->pvoice;
1775 } else {
1776 p = rec;
1777 v = sc->rvoice;
1778 }
1779
1780 if (v == NULL) {
1781 continue;
1782 }
1783
1784 /* No multiple voice support for now */
1785 if ((error = emuxki_set_vparms(v, p)))
1786 return (error);
1787 }
1788
1789 return (0);
1790 }
1791
1792 static int
1793 emuxki_halt_output(void *addr)
1794 {
1795 struct emuxki_softc *sc = addr;
1796
1797 /* No multiple voice support for now */
1798 if (sc->pvoice == NULL)
1799 return (ENXIO);
1800
1801 emuxki_voice_halt(sc->pvoice);
1802 return (0);
1803 }
1804
1805 static int
1806 emuxki_halt_input(void *addr)
1807 {
1808 struct emuxki_softc *sc = addr;
1809
1810 #ifdef EMUXKI_DEBUG
1811 printf("%s: emuxki_halt_input called\n", sc->sc_dev.dv_xname);
1812 #endif
1813
1814 /* No multiple voice support for now */
1815 if (sc->rvoice == NULL)
1816 return (ENXIO);
1817 emuxki_voice_halt(sc->rvoice);
1818 return (0);
1819 }
1820
1821 static int
1822 emuxki_getdev(void *addr, struct audio_device *dev)
1823 {
1824 strncpy(dev->name, "Creative EMU10k1", sizeof(dev->name));
1825 strcpy(dev->version, "");
1826 strncpy(dev->config, "emuxki", sizeof(dev->config));
1827
1828 return (0);
1829 }
1830
1831 static int
1832 emuxki_set_port(void *addr, mixer_ctrl_t *mctl)
1833 {
1834 struct emuxki_softc *sc = addr;
1835
1836 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
1837 }
1838
1839 static int
1840 emuxki_get_port(void *addr, mixer_ctrl_t *mctl)
1841 {
1842 struct emuxki_softc *sc = addr;
1843
1844 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
1845 }
1846
1847 static int
1848 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo)
1849 {
1850 struct emuxki_softc *sc = addr;
1851
1852 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
1853 }
1854
1855 static void *
1856 emuxki_allocm(void *addr, int direction, size_t size,
1857 struct malloc_type *type, int flags)
1858 {
1859 struct emuxki_softc *sc = addr;
1860
1861 if (direction == AUMODE_PLAY)
1862 return emuxki_pmem_alloc(sc, size, type, flags);
1863 else
1864 return emuxki_rmem_alloc(sc, size, type, flags);
1865 }
1866
1867 static void
1868 emuxki_freem(void *addr, void *ptr, struct malloc_type *type)
1869 {
1870 struct emuxki_softc *sc = addr;
1871 int i, s;
1872 struct emuxki_mem *mem;
1873 size_t numblocks;
1874 u_int32_t *ptb, silentpage;
1875
1876 ptb = KERNADDR(sc->ptb);
1877 silentpage = DMAADDR(sc->silentpage) << 1;
1878 LIST_FOREACH(mem, &sc->mem, next) {
1879 if (KERNADDR(mem->dmamem) != ptr)
1880 continue;
1881
1882 s = splaudio();
1883 if (mem->ptbidx != EMU_RMEM) {
1884 numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE;
1885 if (DMASIZE(mem->dmamem) % EMU_PTESIZE)
1886 numblocks++;
1887 for (i = 0; i < numblocks; i++)
1888 ptb[mem->ptbidx + i] =
1889 silentpage | (mem->ptbidx + i);
1890 }
1891 LIST_REMOVE(mem, next);
1892 splx(s);
1893
1894 emuxki_mem_delete(mem, type);
1895 break;
1896 }
1897 }
1898
1899 static size_t
1900 emuxki_round_buffersize(void *addr, int direction, size_t bsize)
1901 {
1902 static const int recbuf_sz[] = {
1903 0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792,
1904 2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240,
1905 12288, 14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152,
1906 57344, 65536
1907 };
1908
1909 if (direction == AUMODE_PLAY) {
1910 if (bsize < EMU_PTESIZE)
1911 bsize = EMU_PTESIZE;
1912 else if (bsize > (EMU_PTESIZE * EMU_MAXPTE))
1913 bsize = EMU_PTESIZE * EMU_MAXPTE;
1914 /* Would be better if set to max available */
1915 else if (bsize % EMU_PTESIZE)
1916 bsize = bsize -
1917 (bsize % EMU_PTESIZE) +
1918 EMU_PTESIZE;
1919 } else {
1920 int idx;
1921
1922 /* find nearest lower recbuf size */
1923 for(idx=32; --idx >= 0; ) {
1924 if (bsize >= recbuf_sz[idx]) {
1925 bsize = recbuf_sz[idx];
1926 break;
1927 }
1928 }
1929
1930 if (bsize == 0)
1931 bsize = 384;
1932 }
1933
1934 return (bsize);
1935 }
1936
1937 static paddr_t
1938 emuxki_mappage(void *addr, void *ptr, off_t off, int prot)
1939 {
1940 struct emuxki_softc *sc = addr;
1941 struct emuxki_mem *mem;
1942 u_int32_t *ptb;
1943
1944 ptb = KERNADDR(sc->ptb);
1945 LIST_FOREACH(mem, &sc->mem, next) {
1946 if (KERNADDR(mem->dmamem) == ptr) {
1947 struct dmamem *dm = mem->dmamem;
1948
1949 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs,
1950 off, prot, BUS_DMA_WAITOK);
1951 }
1952 }
1953
1954 return (-1);
1955 }
1956
1957 static int
1958 emuxki_get_props(void *addr)
1959 {
1960 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1961 AUDIO_PROP_FULLDUPLEX);
1962 }
1963
1964 static int
1965 emuxki_trigger_output(void *addr, void *start, void *end, int blksize,
1966 void (*inth) (void *), void *inthparam,
1967 struct audio_params *params)
1968 {
1969 struct emuxki_softc *sc = addr;
1970 /* No multiple voice support for now */
1971 struct emuxki_voice *voice = sc->pvoice;
1972 int error;
1973
1974 if (voice == NULL)
1975 return (ENXIO);
1976 if ((error = emuxki_set_vparms(voice, params)))
1977 return (error);
1978 if ((error = emuxki_voice_set_bufparms(voice, start,
1979 (caddr_t)end - (caddr_t)start, blksize)))
1980 return (error);
1981 emuxki_voice_commit_parms(voice);
1982 emuxki_voice_start(voice, inth, inthparam);
1983
1984 return (0);
1985 }
1986
1987 static int
1988 emuxki_trigger_input(void *addr, void *start, void *end, int blksize,
1989 void (*inth) (void *), void *inthparam,
1990 struct audio_params *params)
1991 {
1992 struct emuxki_softc *sc = addr;
1993 /* No multiple voice support for now */
1994 struct emuxki_voice *voice = sc->rvoice;
1995 int error;
1996
1997 if (voice == NULL)
1998 return (ENXIO);
1999 if ((error = emuxki_set_vparms(voice, params)))
2000 return (error);
2001 if ((error = emuxki_voice_set_bufparms(voice, start,
2002 (caddr_t)end - (caddr_t)start,
2003 blksize)))
2004 return (error);
2005 emuxki_voice_commit_parms(voice); /* Useless for record ? */
2006 emuxki_voice_start(voice, inth, inthparam);
2007
2008 return (0);
2009 }
2010
2011
2012 /*
2013 * AC97 callbacks
2014 */
2015
2016 static int
2017 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif)
2018 {
2019 struct emuxki_softc *sc = arg;
2020
2021 sc->codecif = codecif;
2022 return (0);
2023 }
2024
2025 static int
2026 emuxki_ac97_read(void *arg, u_int8_t reg, u_int16_t *val)
2027 {
2028 struct emuxki_softc *sc = arg;
2029 int s;
2030
2031 s = splaudio();
2032 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2033 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA);
2034 splx(s);
2035
2036 return (0);
2037 }
2038
2039 static int
2040 emuxki_ac97_write(void *arg, u_int8_t reg, u_int16_t val)
2041 {
2042 struct emuxki_softc *sc = arg;
2043 int s;
2044
2045 s = splaudio();
2046 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2047 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val);
2048 splx(s);
2049
2050 return (0);
2051 }
2052
2053 static void
2054 emuxki_ac97_reset(void *arg)
2055 {
2056 }
2057
2058 enum ac97_host_flags
2059 emuxki_ac97_flags(void *arg)
2060 {
2061 return (AC97_HOST_SWAPPED_CHANNELS);
2062 }
2063