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