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