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