emuxki.c revision 1.47 1 /* $NetBSD: emuxki.c,v 1.47 2006/09/23 01:19:18 jmcneill 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 disappear 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 * - Multiple voices play (problem with /dev/audio architecture)
53 * - Multiple sources recording (Pb with audio(4))
54 * - Independent modification of each channel's parameters (via mixer ?)
55 * - DSP FX patches (to make fx like chipmunk)
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: emuxki.c,v 1.47 2006/09/23 01:19:18 jmcneill Exp $");
60
61 #include <sys/param.h>
62 #include <sys/device.h>
63 #include <sys/errno.h>
64 #include <sys/malloc.h>
65 #include <sys/systm.h>
66 #include <sys/audioio.h>
67 #include <sys/select.h>
68 #include <dev/pci/pcireg.h>
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcidevs.h>
71 #include <dev/audio_if.h>
72 #include <dev/audiovar.h>
73 #include <dev/auconv.h>
74 #include <dev/mulaw.h>
75 #include <dev/ic/ac97reg.h>
76 #include <dev/ic/ac97var.h>
77
78 #include <dev/pci/emuxkireg.h>
79 #include <dev/pci/emuxkivar.h>
80
81 /* autoconf goo */
82 static int emuxki_match(struct device *, struct cfdata *, void *);
83 static void emuxki_attach(struct device *, struct device *, void *);
84 static int emuxki_detach(struct device *, int);
85
86 /* DMA mem mgmt */
87 static struct dmamem *dmamem_alloc(bus_dma_tag_t, size_t, bus_size_t,
88 int, struct malloc_type *, int);
89 static void dmamem_free(struct dmamem *, struct malloc_type *);
90
91 /* Emu10k1 init & shutdown */
92 static int emuxki_init(struct emuxki_softc *);
93 static void emuxki_shutdown(struct emuxki_softc *);
94
95 /* Emu10k1 mem mgmt */
96 static void *emuxki_pmem_alloc(struct emuxki_softc *, size_t,
97 struct malloc_type *,int);
98 static void *emuxki_rmem_alloc(struct emuxki_softc *, size_t,
99 struct malloc_type *,int);
100
101 /*
102 * Emu10k1 channels funcs : There is no direct access to channels, everything
103 * is done through voices I will at least provide channel based fx params
104 * modification, later...
105 */
106
107 /* Emu10k1 voice mgmt */
108 static struct emuxki_voice *emuxki_voice_new(struct emuxki_softc *,
109 uint8_t);
110 static void emuxki_voice_delete(struct emuxki_voice *);
111 static int emuxki_voice_set_audioparms(struct emuxki_voice *, uint8_t,
112 uint8_t, uint32_t);
113 /* emuxki_voice_set_fxparms will come later, it'll need channel distinction */
114 static int emuxki_voice_set_bufparms(struct emuxki_voice *,
115 void *, uint32_t, uint16_t);
116 static void emuxki_voice_commit_parms(struct emuxki_voice *);
117 static int emuxki_voice_adc_rate(struct emuxki_voice *);
118 static uint32_t emuxki_voice_curaddr(struct emuxki_voice *);
119 static void emuxki_voice_start(struct emuxki_voice *,
120 void (*) (void *), void *);
121 static void emuxki_voice_halt(struct emuxki_voice *);
122
123 /*
124 * Emu10k1 stream mgmt : not done yet
125 */
126 #if 0
127 static struct emuxki_stream *emuxki_stream_new(struct emu10k1 *);
128 static void emuxki_stream_delete(struct emuxki_stream *);
129 static int emuxki_stream_set_audio_params(struct emuxki_stream *,
130 uint8_t, uint8_t, uint8_t, uint16_t);
131 static void emuxki_stream_start(struct emuxki_stream *);
132 static void emuxki_stream_halt(struct emuxki_stream *);
133 #endif
134
135 /* audio interface callbacks */
136
137 static int emuxki_open(void *, int);
138 static void emuxki_close(void *);
139
140 static int emuxki_query_encoding(void *, struct audio_encoding *);
141 static int emuxki_set_params(void *, int, int, audio_params_t *,
142 audio_params_t *, stream_filter_list_t *,
143 stream_filter_list_t *);
144
145 static int emuxki_round_blocksize(void *, int, int, const audio_params_t *);
146 static size_t emuxki_round_buffersize(void *, int, size_t);
147
148 static int emuxki_trigger_output(void *, void *, void *, int,
149 void (*)(void *), void *, const audio_params_t *);
150 static int emuxki_trigger_input(void *, void *, void *, int,
151 void (*) (void *), void *, const audio_params_t *);
152 static int emuxki_halt_output(void *);
153 static int emuxki_halt_input(void *);
154
155 static int emuxki_getdev(void *, struct audio_device *);
156 static int emuxki_set_port(void *, mixer_ctrl_t *);
157 static int emuxki_get_port(void *, mixer_ctrl_t *);
158 static int emuxki_query_devinfo(void *, mixer_devinfo_t *);
159
160 static void *emuxki_allocm(void *, int, size_t, struct malloc_type *, int);
161 static void emuxki_freem(void *, void *, struct malloc_type *);
162
163 static paddr_t emuxki_mappage(void *, void *, off_t, int);
164 static int emuxki_get_props(void *);
165
166 /* Interrupt handler */
167 static int emuxki_intr(void *);
168
169 /* Emu10k1 AC97 interface callbacks */
170 static int emuxki_ac97_attach(void *, struct ac97_codec_if *);
171 static int emuxki_ac97_read(void *, uint8_t, uint16_t *);
172 static int emuxki_ac97_write(void *, uint8_t, uint16_t);
173 static int emuxki_ac97_reset(void *);
174 static enum ac97_host_flags emuxki_ac97_flags(void *);
175
176 /*
177 * Autoconfig goo.
178 */
179 CFATTACH_DECL(emuxki, sizeof(struct emuxki_softc),
180 emuxki_match, emuxki_attach, emuxki_detach, NULL);
181
182 static const struct audio_hw_if emuxki_hw_if = {
183 emuxki_open,
184 emuxki_close,
185 NULL, /* drain */
186 emuxki_query_encoding,
187 emuxki_set_params,
188 emuxki_round_blocksize,
189 NULL, /* commit settings */
190 NULL, /* init_output */
191 NULL, /* init_input */
192 NULL, /* start_output */
193 NULL, /* start_input */
194 emuxki_halt_output,
195 emuxki_halt_input,
196 NULL, /* speaker_ctl */
197 emuxki_getdev,
198 NULL, /* setfd */
199 emuxki_set_port,
200 emuxki_get_port,
201 emuxki_query_devinfo,
202 emuxki_allocm,
203 emuxki_freem,
204 emuxki_round_buffersize,
205 emuxki_mappage,
206 emuxki_get_props,
207 emuxki_trigger_output,
208 emuxki_trigger_input,
209 NULL, /* dev_ioctl */
210 NULL, /* powerstate */
211 };
212
213 #if 0
214 static const int emuxki_recsrc_intrmasks[EMU_NUMRECSRCS] =
215 { EMU_INTE_MICBUFENABLE, EMU_INTE_ADCBUFENABLE, EMU_INTE_EFXBUFENABLE };
216 #endif
217 static const uint32_t emuxki_recsrc_bufaddrreg[EMU_NUMRECSRCS] =
218 { EMU_MICBA, EMU_ADCBA, EMU_FXBA };
219 static const uint32_t emuxki_recsrc_szreg[EMU_NUMRECSRCS] =
220 { EMU_MICBS, EMU_ADCBS, EMU_FXBS };
221 static const int emuxki_recbuf_sz[] = {
222 0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792,
223 2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240,
224 12288, 14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152,
225 57344, 65536
226 };
227
228 #define EMUXKI_NFORMATS 4
229 static const struct audio_format emuxki_formats[EMUXKI_NFORMATS] = {
230 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
231 2, AUFMT_STEREO, 0, {4000, 48000}},
232 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
233 1, AUFMT_MONAURAL, 0, {4000, 48000}},
234 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
235 2, AUFMT_STEREO, 0, {4000, 48000}},
236 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
237 1, AUFMT_MONAURAL, 0, {4000, 48000}},
238 };
239
240 /*
241 * DMA memory mgmt
242 */
243
244 static void
245 dmamem_delete(struct dmamem *mem, struct malloc_type *type)
246 {
247
248 free(mem->segs, type);
249 free(mem, type);
250 }
251
252 static struct dmamem *
253 dmamem_alloc(bus_dma_tag_t dmat, size_t size, bus_size_t align,
254 int nsegs, struct malloc_type *type, int flags)
255 {
256 struct dmamem *mem;
257 int bus_dma_flags;
258
259 /* Allocate memory for structure */
260 if ((mem = malloc(sizeof(*mem), type, flags)) == NULL)
261 return NULL;
262 mem->dmat = dmat;
263 mem->size = size;
264 mem->align = align;
265 mem->nsegs = nsegs;
266 mem->bound = 0;
267
268 mem->segs = malloc(mem->nsegs * sizeof(*(mem->segs)), type, flags);
269 if (mem->segs == NULL) {
270 free(mem, type);
271 return NULL;
272 }
273
274 bus_dma_flags = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
275 if (bus_dmamem_alloc(dmat, mem->size, mem->align, mem->bound,
276 mem->segs, mem->nsegs, &(mem->rsegs),
277 bus_dma_flags)) {
278 dmamem_delete(mem, type);
279 return NULL;
280 }
281
282 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, mem->size,
283 &(mem->kaddr), bus_dma_flags | BUS_DMA_COHERENT)) {
284 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
285 dmamem_delete(mem, type);
286 return NULL;
287 }
288
289 if (bus_dmamap_create(dmat, mem->size, mem->nsegs, mem->size,
290 mem->bound, bus_dma_flags, &(mem->map))) {
291 bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
292 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
293 dmamem_delete(mem, type);
294 return NULL;
295 }
296
297 if (bus_dmamap_load(dmat, mem->map, mem->kaddr,
298 mem->size, NULL, bus_dma_flags)) {
299 bus_dmamap_destroy(dmat, mem->map);
300 bus_dmamem_unmap(dmat, mem->kaddr, mem->size);
301 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
302 dmamem_delete(mem, type);
303 return NULL;
304 }
305
306 return mem;
307 }
308
309 static void
310 dmamem_free(struct dmamem *mem, struct malloc_type *type)
311 {
312
313 bus_dmamap_unload(mem->dmat, mem->map);
314 bus_dmamap_destroy(mem->dmat, mem->map);
315 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
316 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
317 dmamem_delete(mem, type);
318 }
319
320
321 /*
322 * Autoconf device callbacks : attach and detach
323 */
324
325 static void
326 emuxki_pci_shutdown(struct emuxki_softc *sc)
327 {
328
329 if (sc->sc_ih != NULL)
330 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
331 if (sc->sc_ios)
332 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
333 }
334
335 static int
336 emuxki_scinit(struct emuxki_softc *sc)
337 {
338 int err;
339
340 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
341 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK |
342 EMU_HCFG_MUTEBUTTONENABLE);
343 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
344 EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE);
345
346 if ((err = emuxki_init(sc)))
347 return err;
348
349 if (sc->sc_type & EMUXKI_AUDIGY2) {
350 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
351 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AC3ENABLE_CDSPDIF |
352 EMU_HCFG_AC3ENABLE_GPSPDIF | EMU_HCFG_AUTOMUTE);
353 } else if (sc->sc_type & EMUXKI_AUDIGY) {
354 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
355 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE);
356 } else {
357 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
358 EMU_HCFG_AUDIOENABLE | EMU_HCFG_JOYENABLE |
359 EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_AUTOMUTE);
360 }
361 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
362 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
363 EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE |
364 EMU_INTE_MUTEENABLE);
365 if (sc->sc_type & EMUXKI_AUDIGY2) {
366 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG,
367 EMU_A_IOCFG_GPOUT0 |
368 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG));
369 }
370
371 /* No multiple voice support for now */
372 sc->pvoice = sc->rvoice = NULL;
373
374 return 0;
375 }
376
377 static int
378 emuxki_ac97_init(struct emuxki_softc *sc)
379 {
380 sc->hostif.arg = sc;
381 sc->hostif.attach = emuxki_ac97_attach;
382 sc->hostif.read = emuxki_ac97_read;
383 sc->hostif.write = emuxki_ac97_write;
384 sc->hostif.reset = emuxki_ac97_reset;
385 sc->hostif.flags = emuxki_ac97_flags;
386 return ac97_attach(&sc->hostif, &sc->sc_dev);
387 }
388
389 static int
390 emuxki_match(struct device *parent, struct cfdata *match, void *aux)
391 {
392 struct pci_attach_args *pa;
393
394 pa = aux;
395 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CREATIVELABS)
396 return 0;
397
398 switch (PCI_PRODUCT(pa->pa_id)) {
399 case PCI_PRODUCT_CREATIVELABS_SBLIVE:
400 case PCI_PRODUCT_CREATIVELABS_SBLIVE2:
401 case PCI_PRODUCT_CREATIVELABS_AUDIGY:
402 return 1;
403 default:
404 return 0;
405 }
406 }
407
408 static void
409 emuxki_attach(struct device *parent, struct device *self, void *aux)
410 {
411 struct emuxki_softc *sc;
412 struct pci_attach_args *pa;
413 char devinfo[256];
414 pci_intr_handle_t ih;
415 const char *intrstr;
416
417 sc = (struct emuxki_softc *) self;
418 pa = aux;
419 aprint_naive(": Audio controller\n");
420
421 if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
422 &(sc->sc_iot), &(sc->sc_ioh), &(sc->sc_iob),
423 &(sc->sc_ios))) {
424 aprint_error(": can't map iospace\n");
425 return;
426 }
427 pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
428 aprint_normal(": %s\n", devinfo);
429
430 sc->sc_pc = pa->pa_pc;
431 sc->sc_dmat = pa->pa_dmat;
432 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
433 pci_conf_read(pa->pa_pc, pa->pa_tag,
434 (PCI_COMMAND_STATUS_REG) | PCI_COMMAND_MASTER_ENABLE));
435
436 if (pci_intr_map(pa, &ih)) {
437 aprint_error("%s: couldn't map interrupt\n",
438 sc->sc_dev.dv_xname);
439 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
440 return;
441 }
442
443 intrstr = pci_intr_string(pa->pa_pc, ih);
444 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, emuxki_intr,
445 sc);
446 if (sc->sc_ih == NULL) {
447 aprint_error("%s: couldn't establish interrupt",
448 sc->sc_dev.dv_xname);
449 if (intrstr != NULL)
450 aprint_normal(" at %s", intrstr);
451 aprint_normal("\n");
452 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
453 return;
454 }
455 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
456
457 /* XXX it's unknown whether APS is made from Audigy as well */
458 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY) {
459 sc->sc_type = EMUXKI_AUDIGY;
460 if (PCI_REVISION(pa->pa_class) == 0x04) {
461 sc->sc_type |= EMUXKI_AUDIGY2;
462 strlcpy(sc->sc_audv.name, "Audigy2", sizeof sc->sc_audv.name);
463 } else {
464 strlcpy(sc->sc_audv.name, "Audigy", sizeof sc->sc_audv.name);
465 }
466 } else if (pci_conf_read(pa->pa_pc, pa->pa_tag,
467 PCI_SUBSYS_ID_REG) == EMU_SUBSYS_APS) {
468 sc->sc_type = EMUXKI_APS;
469 strlcpy(sc->sc_audv.name, "E-mu APS", sizeof sc->sc_audv.name);
470 } else {
471 sc->sc_type = EMUXKI_SBLIVE;
472 strlcpy(sc->sc_audv.name, "SB Live!", sizeof sc->sc_audv.name);
473 }
474 snprintf(sc->sc_audv.version, sizeof sc->sc_audv.version, "0x%02x",
475 PCI_REVISION(pa->pa_class));
476 strlcpy(sc->sc_audv.config, "emuxki", sizeof sc->sc_audv.config);
477
478 if (emuxki_scinit(sc) || emuxki_ac97_init(sc) ||
479 (sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self)) == NULL) {
480 emuxki_pci_shutdown(sc);
481 return;
482 }
483 #if 0
484 sc->rsourcectl.dev =
485 sc->codecif->vtbl->get_portnum_by_name(sc->codec_if, AudioCrecord,
486 AudioNsource, NULL);
487 sc->rsourcectl.cp = AUDIO_MIXER_ENUM;
488 #endif
489 }
490
491 static int
492 emuxki_detach(struct device *self, int flags)
493 {
494 struct emuxki_softc *sc;
495
496 sc = (struct emuxki_softc *)self;
497 if (sc->sc_audev != NULL) /* Test in case audio didn't attach */
498 config_detach(sc->sc_audev, 0);
499
500 /* All voices should be stopped now but add some code here if not */
501
502 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG,
503 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK |
504 EMU_HCFG_MUTEBUTTONENABLE);
505 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 0);
506
507 emuxki_shutdown(sc);
508
509 emuxki_pci_shutdown(sc);
510
511 return 0;
512 }
513
514
515 /* Misc stuff relative to emu10k1 */
516
517 static uint32_t
518 emuxki_rate_to_pitch(uint32_t rate)
519 {
520 static const uint32_t logMagTable[128] = {
521 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3,
522 0x13aa2, 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a,
523 0x2655d, 0x28ed5, 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb,
524 0x381b6, 0x3a93d, 0x3d081, 0x3f782, 0x41e42, 0x444c1, 0x46b01,
525 0x49101, 0x4b6c4, 0x4dc49, 0x50191, 0x5269e, 0x54b6f, 0x57006,
526 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 0x646ee, 0x66a00,
527 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 0x759d4,
528 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
529 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20,
530 0x93d26, 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec,
531 0xa11d8, 0xa2f9d, 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241,
532 0xadf26, 0xafbe7, 0xb1885, 0xb3500, 0xb5157, 0xb6d8c, 0xb899f,
533 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 0xc1404, 0xc2f50, 0xc4a7b,
534 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 0xceaec, 0xd053f,
535 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 0xdba4a,
536 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
537 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a,
538 0xf2c83, 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57,
539 0xfd1a7, 0xfe8df
540 };
541 static const uint8_t logSlopeTable[128] = {
542 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
543 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
544 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
545 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
546 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
547 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
548 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
549 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
550 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
551 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
552 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
553 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
554 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
555 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
556 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
557 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
558 };
559 int8_t i;
560
561 if (rate == 0)
562 return 0; /* Bail out if no leading "1" */
563 rate *= 11185; /* Scale 48000 to 0x20002380 */
564 for (i = 31; i > 0; i--) {
565 if (rate & 0x80000000) { /* Detect leading "1" */
566 return (((uint32_t) (i - 15) << 20) +
567 logMagTable[0x7f & (rate >> 24)] +
568 (0x7f & (rate >> 17)) *
569 logSlopeTable[0x7f & (rate >> 24)]);
570 }
571 rate <<= 1;
572 }
573
574 return 0; /* Should never reach this point */
575 }
576
577 /* Emu10k1 Low level */
578
579 static uint32_t
580 emuxki_read(struct emuxki_softc *sc, uint16_t chano, uint32_t reg)
581 {
582 uint32_t ptr, mask;
583 uint8_t size, offset;
584 int s;
585
586 mask = 0xffffffff;
587 offset = 0;
588 ptr = ((((u_int32_t) reg) << 16) &
589 (sc->sc_type & EMUXKI_AUDIGY ?
590 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) |
591 (chano & EMU_PTR_CHNO_MASK);
592 if (reg & 0xff000000) {
593 size = (reg >> 24) & 0x3f;
594 offset = (reg >> 16) & 0x1f;
595 mask = ((1 << size) - 1) << offset;
596 }
597
598 s = splaudio();
599 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr);
600 ptr = (bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_DATA) & mask)
601 >> offset;
602 splx(s);
603
604 return ptr;
605 }
606
607 static void
608 emuxki_write(struct emuxki_softc *sc, uint16_t chano,
609 uint32_t reg, uint32_t data)
610 {
611 uint32_t ptr, mask;
612 uint8_t size, offset;
613 int s;
614
615 ptr = ((((u_int32_t) reg) << 16) &
616 (sc->sc_type & EMUXKI_AUDIGY ?
617 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) |
618 (chano & EMU_PTR_CHNO_MASK);
619 if (reg & 0xff000000) {
620 size = (reg >> 24) & 0x3f;
621 offset = (reg >> 16) & 0x1f;
622 mask = ((1 << size) - 1) << offset;
623 data = ((data << offset) & mask) |
624 (emuxki_read(sc, chano, reg & 0xffff) & ~mask);
625 }
626
627 s = splaudio();
628 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr);
629 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_DATA, data);
630 splx(s);
631 }
632
633 /* Microcode should this go in /sys/dev/microcode ? */
634
635 static void
636 emuxki_write_micro(struct emuxki_softc *sc, uint32_t pc, uint32_t data)
637 {
638
639 emuxki_write(sc, 0,
640 (sc->sc_type & EMUXKI_AUDIGY ?
641 EMU_A_MICROCODEBASE : EMU_MICROCODEBASE) + pc,
642 data);
643 }
644
645 static void
646 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op,
647 uint16_t r, uint16_t a, uint16_t x, uint16_t y)
648 {
649
650 if (sc->sc_type & EMUXKI_AUDIGY) {
651 emuxki_write_micro(sc, *pc << 1,
652 ((x << 12) & EMU_A_DSP_LOWORD_OPX_MASK) |
653 (y & EMU_A_DSP_LOWORD_OPY_MASK));
654 emuxki_write_micro(sc, (*pc << 1) + 1,
655 ((op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK) |
656 ((r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK) |
657 (a & EMU_A_DSP_HIWORD_OPA_MASK));
658 } else {
659 emuxki_write_micro(sc, *pc << 1,
660 ((x << 10) & EMU_DSP_LOWORD_OPX_MASK) |
661 (y & EMU_DSP_LOWORD_OPY_MASK));
662 emuxki_write_micro(sc, (*pc << 1) + 1,
663 ((op << 20) & EMU_DSP_HIWORD_OPCODE_MASK) |
664 ((r << 10) & EMU_DSP_HIWORD_RESULT_MASK) |
665 (a & EMU_DSP_HIWORD_OPA_MASK));
666 }
667 (*pc)++;
668 }
669
670 /* init and shutdown */
671
672 static void
673 emuxki_initfx(struct emuxki_softc *sc)
674 {
675 uint16_t pc;
676
677 /* Set all GPRs to 0 */
678 for (pc = 0; pc < 256; pc++)
679 emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
680 for (pc = 0; pc < 160; pc++) {
681 emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
682 emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
683 }
684 pc = 0;
685
686 if (sc->sc_type & EMUXKI_AUDIGY) {
687 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */
688 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
689 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT),
690 EMU_A_DSP_CST(0),
691 EMU_DSP_FX(0), EMU_A_DSP_CST(4));
692 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
693 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT),
694 EMU_A_DSP_CST(0),
695 EMU_DSP_FX(1), EMU_A_DSP_CST(4));
696
697 /* Rear channel OUT (l/r) = FX[2/3] * 4 */
698 #if 0
699 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
700 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_REAR),
701 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT),
702 EMU_DSP_FX(0), EMU_A_DSP_CST(4));
703 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
704 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_REAR),
705 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT),
706 EMU_DSP_FX(1), EMU_A_DSP_CST(4));
707 #endif
708 /* ADC recording (l/r) = AC97 In (l/r) */
709 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
710 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_ADC),
711 EMU_A_DSP_INL(EMU_DSP_IN_AC97),
712 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
713 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
714 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_ADC),
715 EMU_A_DSP_INR(EMU_DSP_IN_AC97),
716 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
717
718 /* zero out the rest of the microcode */
719 while (pc < 512)
720 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
721 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0),
722 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
723
724 emuxki_write(sc, 0, EMU_A_DBG, 0); /* Is it really necessary ? */
725 } else {
726 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */
727 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
728 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT),
729 EMU_DSP_CST(0),
730 EMU_DSP_FX(0), EMU_DSP_CST(4));
731 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
732 EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT),
733 EMU_DSP_CST(0),
734 EMU_DSP_FX(1), EMU_DSP_CST(4));
735
736 /* Rear channel OUT (l/r) = FX[2/3] * 4 */
737 #if 0
738 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
739 EMU_DSP_OUTL(EMU_DSP_OUT_AD_REAR),
740 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT),
741 EMU_DSP_FX(0), EMU_DSP_CST(4));
742 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
743 EMU_DSP_OUTR(EMU_DSP_OUT_AD_REAR),
744 EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT),
745 EMU_DSP_FX(1), EMU_DSP_CST(4));
746 #endif
747 /* ADC recording (l/r) = AC97 In (l/r) */
748 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
749 EMU_DSP_OUTL(EMU_DSP_OUT_ADC),
750 EMU_DSP_INL(EMU_DSP_IN_AC97),
751 EMU_DSP_CST(0), EMU_DSP_CST(0));
752 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
753 EMU_DSP_OUTR(EMU_DSP_OUT_ADC),
754 EMU_DSP_INR(EMU_DSP_IN_AC97),
755 EMU_DSP_CST(0), EMU_DSP_CST(0));
756
757 /* zero out the rest of the microcode */
758 while (pc < 512)
759 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
760 EMU_DSP_CST(0), EMU_DSP_CST(0),
761 EMU_DSP_CST(0), EMU_DSP_CST(0));
762
763 emuxki_write(sc, 0, EMU_DBG, 0); /* Is it really necessary ? */
764 }
765 }
766
767 static int
768 emuxki_init(struct emuxki_softc *sc)
769 {
770 uint16_t i;
771 uint32_t spcs, *ptb;
772 bus_addr_t silentpage;
773
774 /* disable any channel interrupt */
775 emuxki_write(sc, 0, EMU_CLIEL, 0);
776 emuxki_write(sc, 0, EMU_CLIEH, 0);
777 emuxki_write(sc, 0, EMU_SOLEL, 0);
778 emuxki_write(sc, 0, EMU_SOLEH, 0);
779
780 /* Set recording buffers sizes to zero */
781 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
782 emuxki_write(sc, 0, EMU_MICBA, 0);
783 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
784 emuxki_write(sc, 0, EMU_FXBA, 0);
785 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
786 emuxki_write(sc, 0, EMU_ADCBA, 0);
787
788 if(sc->sc_type & EMUXKI_AUDIGY) {
789 emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
790 emuxki_write(sc, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
791 }
792
793 /* Initialize all channels to stopped and no effects */
794 for (i = 0; i < EMU_NUMCHAN; i++) {
795 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0);
796 emuxki_write(sc, i, EMU_CHAN_IP, 0);
797 emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
798 emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
799 emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
800 emuxki_write(sc, i, EMU_CHAN_CPF, 0);
801 emuxki_write(sc, i, EMU_CHAN_CCR, 0);
802 emuxki_write(sc, i, EMU_CHAN_PSST, 0);
803 emuxki_write(sc, i, EMU_CHAN_DSL, 0x10); /* Why 16 ? */
804 emuxki_write(sc, i, EMU_CHAN_CCCA, 0);
805 emuxki_write(sc, i, EMU_CHAN_Z1, 0);
806 emuxki_write(sc, i, EMU_CHAN_Z2, 0);
807 emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
808 emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
809 emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
810 emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
811 emuxki_write(sc, i, EMU_CHAN_PEFE, 0);
812 emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
813 emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 24);
814 emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 24);
815 emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
816
817 /* these are last so OFF prevents writing */
818 emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0);
819 emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0);
820 emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0);
821 emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
822 emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0);
823 }
824
825 /* set digital outputs format */
826 spcs = (EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 |
827 EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC |
828 EMU_SPCS_GENERATIONSTATUS | 0x00001200 /* Cat code. */ |
829 0x00000000 /* IEC-958 Mode */ | EMU_SPCS_EMPHASIS_NONE |
830 EMU_SPCS_COPYRIGHT);
831 emuxki_write(sc, 0, EMU_SPCS0, spcs);
832 emuxki_write(sc, 0, EMU_SPCS1, spcs);
833 emuxki_write(sc, 0, EMU_SPCS2, spcs);
834
835 if(sc->sc_type & EMUXKI_AUDIGY2) {
836 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE, EMU_A2_SPDIF_UNKNOWN);
837
838 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL);
839 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA,
840 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
841
842 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCMULTI);
843 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, EMU_A2_SRCMULTI_ENABLE_INPUT);
844 }
845
846
847 /* Let's play with sound processor */
848 emuxki_initfx(sc);
849
850 /* Here is our Page Table */
851 if ((sc->ptb = dmamem_alloc(sc->sc_dmat,
852 EMU_MAXPTE * sizeof(u_int32_t),
853 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG,
854 M_DEVBUF, M_WAITOK)) == NULL)
855 return ENOMEM;
856
857 /* This is necessary unless you like Metallic noise... */
858 if ((sc->silentpage = dmamem_alloc(sc->sc_dmat, EMU_PTESIZE,
859 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, M_DEVBUF, M_WAITOK))==NULL){
860 dmamem_free(sc->ptb, M_DEVBUF);
861 return ENOMEM;
862 }
863
864 /* Zero out the silent page */
865 /* This might not be always true, it might be 128 for 8bit channels */
866 memset(KERNADDR(sc->silentpage), 0, DMASIZE(sc->silentpage));
867
868 /*
869 * Set all the PTB Entries to the silent page We shift the physical
870 * address by one and OR it with the page number. I don't know what
871 * the ORed index is for, might be a very useful unused feature...
872 */
873 silentpage = DMAADDR(sc->silentpage) << 1;
874 ptb = KERNADDR(sc->ptb);
875 for (i = 0; i < EMU_MAXPTE; i++)
876 ptb[i] = htole32(silentpage | i);
877
878 /* Write PTB address and set TCB to none */
879 emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
880 emuxki_write(sc, 0, EMU_TCBS, 0); /* This means 16K TCB */
881 emuxki_write(sc, 0, EMU_TCB, 0); /* No TCB use for now */
882
883 /*
884 * Set channels MAPs to the silent page.
885 * I don't know what MAPs are for.
886 */
887 silentpage |= EMU_CHAN_MAP_PTI_MASK;
888 for (i = 0; i < EMU_NUMCHAN; i++) {
889 emuxki_write(sc, i, EMU_CHAN_MAPA, silentpage);
890 emuxki_write(sc, i, EMU_CHAN_MAPB, silentpage);
891 sc->channel[i] = NULL;
892 }
893
894 /* Init voices list */
895 LIST_INIT(&(sc->voices));
896
897 /* Timer is stopped */
898 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED;
899 return 0;
900 }
901
902 static void
903 emuxki_shutdown(struct emuxki_softc *sc)
904 {
905 uint32_t i;
906
907 /* Disable any Channels interrupts */
908 emuxki_write(sc, 0, EMU_CLIEL, 0);
909 emuxki_write(sc, 0, EMU_CLIEH, 0);
910 emuxki_write(sc, 0, EMU_SOLEL, 0);
911 emuxki_write(sc, 0, EMU_SOLEH, 0);
912
913 /*
914 * Should do some voice(stream) stopping stuff here, that's what will
915 * stop and deallocate all channels.
916 */
917
918 /* Stop all channels */
919 /* XXX This shouldn't be necessary, I'll remove once everything works */
920 for (i = 0; i < EMU_NUMCHAN; i++)
921 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0);
922 for (i = 0; i < EMU_NUMCHAN; i++) {
923 emuxki_write(sc, i, EMU_CHAN_VTFT, 0);
924 emuxki_write(sc, i, EMU_CHAN_CVCF, 0);
925 emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
926 emuxki_write(sc, i, EMU_CHAN_CPF, 0);
927 }
928
929 /*
930 * Deallocate Emu10k1 caches and recording buffers. Again it will be
931 * removed because it will be done in voice shutdown.
932 */
933 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
934 emuxki_write(sc, 0, EMU_MICBA, 0);
935 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
936 emuxki_write(sc, 0, EMU_FXBA, 0);
937 if(sc->sc_type & EMUXKI_AUDIGY) {
938 emuxki_write(sc, 0, EMU_A_FXWC1, 0);
939 emuxki_write(sc, 0, EMU_A_FXWC2, 0);
940 } else {
941 emuxki_write(sc, 0, EMU_FXWC, 0);
942 }
943 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
944 emuxki_write(sc, 0, EMU_ADCBA, 0);
945
946 /*
947 * XXX I don't know yet how I will handle tank cache buffer,
948 * I don't even clearly know what it is for.
949 */
950 emuxki_write(sc, 0, EMU_TCB, 0); /* 16K again */
951 emuxki_write(sc, 0, EMU_TCBS, 0);
952
953 emuxki_write(sc, 0, EMU_DBG, 0x8000); /* necessary ? */
954
955 dmamem_free(sc->silentpage, M_DEVBUF);
956 dmamem_free(sc->ptb, M_DEVBUF);
957 }
958
959 /* Emu10k1 Memory management */
960
961 static struct emuxki_mem *
962 emuxki_mem_new(struct emuxki_softc *sc, int ptbidx,
963 size_t size, struct malloc_type *type, int flags)
964 {
965 struct emuxki_mem *mem;
966
967 if ((mem = malloc(sizeof(*mem), type, flags)) == NULL)
968 return NULL;
969
970 mem->ptbidx = ptbidx;
971 if ((mem->dmamem = dmamem_alloc(sc->sc_dmat, size, EMU_DMA_ALIGN,
972 EMU_DMAMEM_NSEG, type, flags)) == NULL) {
973 free(mem, type);
974 return NULL;
975 }
976 return mem;
977 }
978
979 static void
980 emuxki_mem_delete(struct emuxki_mem *mem, struct malloc_type *type)
981 {
982
983 dmamem_free(mem->dmamem, type);
984 free(mem, type);
985 }
986
987 static void *
988 emuxki_pmem_alloc(struct emuxki_softc *sc, size_t size,
989 struct malloc_type *type, int flags)
990 {
991 int i, j, s;
992 size_t numblocks;
993 struct emuxki_mem *mem;
994 uint32_t *ptb, silentpage;
995
996 ptb = KERNADDR(sc->ptb);
997 silentpage = DMAADDR(sc->silentpage) << 1;
998 numblocks = size / EMU_PTESIZE;
999 if (size % EMU_PTESIZE)
1000 numblocks++;
1001
1002 for (i = 0; i < EMU_MAXPTE; i++)
1003 if ((le32toh(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) {
1004 /* We look for a free PTE */
1005 s = splaudio();
1006 for (j = 0; j < numblocks; j++)
1007 if ((le32toh(ptb[i + j])
1008 & EMU_CHAN_MAP_PTE_MASK) != silentpage)
1009 break;
1010 if (j == numblocks) {
1011 if ((mem = emuxki_mem_new(sc, i,
1012 size, type, flags)) == NULL) {
1013 splx(s);
1014 return NULL;
1015 }
1016 for (j = 0; j < numblocks; j++)
1017 ptb[i + j] =
1018 htole32((((DMAADDR(mem->dmamem) +
1019 j * EMU_PTESIZE)) << 1) | (i + j));
1020 LIST_INSERT_HEAD(&(sc->mem), mem, next);
1021 splx(s);
1022 return (KERNADDR(mem->dmamem));
1023 } else
1024 i += j;
1025 splx(s);
1026 }
1027 return NULL;
1028 }
1029
1030 static void *
1031 emuxki_rmem_alloc(struct emuxki_softc *sc, size_t size,
1032 struct malloc_type *type, int flags)
1033 {
1034 struct emuxki_mem *mem;
1035 int s;
1036
1037 mem = emuxki_mem_new(sc, EMU_RMEM, size, type, flags);
1038 if (mem == NULL)
1039 return NULL;
1040
1041 s = splaudio();
1042 LIST_INSERT_HEAD(&(sc->mem), mem, next);
1043 splx(s);
1044
1045 return KERNADDR(mem->dmamem);
1046 }
1047
1048 /*
1049 * emuxki_channel_* : Channel management functions
1050 * emuxki_chanparms_* : Channel parameters modification functions
1051 */
1052
1053 /*
1054 * is splaudio necessary here, can the same voice be manipulated by two
1055 * different threads at a time ?
1056 */
1057 static void
1058 emuxki_chanparms_set_defaults(struct emuxki_channel *chan)
1059 {
1060
1061 chan->fxsend.a.level = chan->fxsend.b.level =
1062 chan->fxsend.c.level = chan->fxsend.d.level =
1063 /* for audigy */
1064 chan->fxsend.e.level = chan->fxsend.f.level =
1065 chan->fxsend.g.level = chan->fxsend.h.level =
1066 chan->voice->sc->sc_type & EMUXKI_AUDIGY ?
1067 0xc0 : 0xff; /* not max */
1068
1069 chan->fxsend.a.dest = 0x0;
1070 chan->fxsend.b.dest = 0x1;
1071 chan->fxsend.c.dest = 0x2;
1072 chan->fxsend.d.dest = 0x3;
1073 /* for audigy */
1074 chan->fxsend.e.dest = 0x4;
1075 chan->fxsend.f.dest = 0x5;
1076 chan->fxsend.g.dest = 0x6;
1077 chan->fxsend.h.dest = 0x7;
1078
1079 chan->pitch.initial = 0x0000; /* shouldn't it be 0xE000 ? */
1080 chan->pitch.current = 0x0000; /* should it be 0x0400 */
1081 chan->pitch.target = 0x0000; /* the unity pitch shift ? */
1082 chan->pitch.envelope_amount = 0x00; /* none */
1083
1084 chan->initial_attenuation = 0x00; /* no attenuation */
1085 chan->volume.current = 0x0000; /* no volume */
1086 chan->volume.target = 0xffff;
1087 chan->volume.envelope.current_state = 0x8000; /* 0 msec delay */
1088 chan->volume.envelope.hold_time = 0x7f; /* 0 msec */
1089 chan->volume.envelope.attack_time = 0x7F; /* 5.5msec */
1090 chan->volume.envelope.sustain_level = 0x7F; /* full */
1091 chan->volume.envelope.decay_time = 0x7F; /* 22msec */
1092
1093 chan->filter.initial_cutoff_frequency = 0xff; /* no filter */
1094 chan->filter.current_cutoff_frequency = 0xffff; /* no filtering */
1095 chan->filter.target_cutoff_frequency = 0xffff; /* no filtering */
1096 chan->filter.lowpass_resonance_height = 0x0;
1097 chan->filter.interpolation_ROM = 0x1; /* full band */
1098 chan->filter.envelope_amount = 0x7f; /* none */
1099 chan->filter.LFO_modulation_depth = 0x00; /* none */
1100
1101 chan->loop.start = 0x000000;
1102 chan->loop.end = 0x000010; /* Why ? */
1103
1104 chan->modulation.envelope.current_state = 0x8000;
1105 chan->modulation.envelope.hold_time = 0x00; /* 127 better ? */
1106 chan->modulation.envelope.attack_time = 0x00; /* infinite */
1107 chan->modulation.envelope.sustain_level = 0x00; /* off */
1108 chan->modulation.envelope.decay_time = 0x7f; /* 22 msec */
1109 chan->modulation.LFO_state = 0x8000;
1110
1111 chan->vibrato_LFO.state = 0x8000;
1112 chan->vibrato_LFO.modulation_depth = 0x00; /* none */
1113 chan->vibrato_LFO.vibrato_depth = 0x00;
1114 chan->vibrato_LFO.frequency = 0x00; /* Why set to 24 when
1115 * initialized ? */
1116
1117 chan->tremolo_depth = 0x00;
1118 }
1119
1120 /* only call it at splaudio */
1121 static struct emuxki_channel *
1122 emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num)
1123 {
1124 struct emuxki_channel *chan;
1125
1126 chan = malloc(sizeof(struct emuxki_channel), M_DEVBUF, M_WAITOK);
1127 if (chan == NULL)
1128 return NULL;
1129
1130 chan->voice = voice;
1131 chan->num = num;
1132 emuxki_chanparms_set_defaults(chan);
1133 chan->voice->sc->channel[num] = chan;
1134 return chan;
1135 }
1136
1137 /* only call it at splaudio */
1138 static void
1139 emuxki_channel_delete(struct emuxki_channel *chan)
1140 {
1141
1142 chan->voice->sc->channel[chan->num] = NULL;
1143 free(chan, M_DEVBUF);
1144 }
1145
1146 static void
1147 emuxki_channel_set_fxsend(struct emuxki_channel *chan,
1148 struct emuxki_chanparms_fxsend *fxsend)
1149 {
1150 /* Could do a memcpy ...*/
1151 chan->fxsend.a.level = fxsend->a.level;
1152 chan->fxsend.b.level = fxsend->b.level;
1153 chan->fxsend.c.level = fxsend->c.level;
1154 chan->fxsend.d.level = fxsend->d.level;
1155 chan->fxsend.a.dest = fxsend->a.dest;
1156 chan->fxsend.b.dest = fxsend->b.dest;
1157 chan->fxsend.c.dest = fxsend->c.dest;
1158 chan->fxsend.d.dest = fxsend->d.dest;
1159
1160 /* for audigy */
1161 chan->fxsend.e.level = fxsend->e.level;
1162 chan->fxsend.f.level = fxsend->f.level;
1163 chan->fxsend.g.level = fxsend->g.level;
1164 chan->fxsend.h.level = fxsend->h.level;
1165 chan->fxsend.e.dest = fxsend->e.dest;
1166 chan->fxsend.f.dest = fxsend->f.dest;
1167 chan->fxsend.g.dest = fxsend->g.dest;
1168 chan->fxsend.h.dest = fxsend->h.dest;
1169 }
1170
1171 static void
1172 emuxki_channel_set_srate(struct emuxki_channel *chan, uint32_t srate)
1173 {
1174
1175 chan->pitch.target = (srate << 8) / 375;
1176 chan->pitch.target = (chan->pitch.target >> 1) +
1177 (chan->pitch.target & 1);
1178 chan->pitch.target &= 0xffff;
1179 chan->pitch.current = chan->pitch.target;
1180 chan->pitch.initial =
1181 (emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK;
1182 }
1183
1184 /* voice params must be set before calling this */
1185 static void
1186 emuxki_channel_set_bufparms(struct emuxki_channel *chan,
1187 uint32_t start, uint32_t end)
1188 {
1189
1190 chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK;
1191 chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK;
1192 }
1193
1194 static void
1195 emuxki_channel_commit_fx(struct emuxki_channel *chan)
1196 {
1197 struct emuxki_softc *sc;
1198 u_int8_t chano;
1199
1200 sc = chan->voice->sc;
1201 chano = chan->num;
1202 if(sc->sc_type & EMUXKI_AUDIGY) {
1203 emuxki_write(sc, chano, EMU_A_CHAN_FXRT1,
1204 (chan->fxsend.d.dest << 24) |
1205 (chan->fxsend.c.dest << 16) |
1206 (chan->fxsend.b.dest << 8) |
1207 (chan->fxsend.a.dest));
1208 emuxki_write(sc, chano, EMU_A_CHAN_FXRT2,
1209 (chan->fxsend.h.dest << 24) |
1210 (chan->fxsend.g.dest << 16) |
1211 (chan->fxsend.f.dest << 8) |
1212 (chan->fxsend.e.dest));
1213 emuxki_write(sc, chano, EMU_A_CHAN_SENDAMOUNTS,
1214 (chan->fxsend.e.level << 24) |
1215 (chan->fxsend.f.level << 16) |
1216 (chan->fxsend.g.level << 8) |
1217 (chan->fxsend.h.level));
1218 } else {
1219 emuxki_write(sc, chano, EMU_CHAN_FXRT,
1220 (chan->fxsend.d.dest << 28) |
1221 (chan->fxsend.c.dest << 24) |
1222 (chan->fxsend.b.dest << 20) |
1223 (chan->fxsend.a.dest << 16));
1224 }
1225
1226 emuxki_write(sc, chano, 0x10000000 | EMU_CHAN_PTRX,
1227 (chan->fxsend.a.level << 8) | chan->fxsend.b.level);
1228 emuxki_write(sc, chano, EMU_CHAN_DSL,
1229 (chan->fxsend.d.level << 24) | chan->loop.end);
1230 emuxki_write(sc, chano, EMU_CHAN_PSST,
1231 (chan->fxsend.c.level << 24) | chan->loop.start);
1232 }
1233
1234 static void
1235 emuxki_channel_commit_parms(struct emuxki_channel *chan)
1236 {
1237 struct emuxki_voice *voice;
1238 struct emuxki_softc *sc;
1239 uint32_t start, mapval;
1240 uint8_t chano;
1241 int s;
1242
1243 voice = chan->voice;
1244 sc = voice->sc;
1245 chano = chan->num;
1246 start = chan->loop.start +
1247 (voice->stereo ? 28 : 30) * (voice->b16 + 1);
1248 mapval = DMAADDR(sc->silentpage) << 1 | EMU_CHAN_MAP_PTI_MASK;
1249
1250 s = splaudio();
1251 emuxki_write(sc, chano, EMU_CHAN_CPF_STEREO, voice->stereo);
1252
1253 emuxki_channel_commit_fx(chan);
1254
1255 emuxki_write(sc, chano, EMU_CHAN_CCCA,
1256 (chan->filter.lowpass_resonance_height << 28) |
1257 (chan->filter.interpolation_ROM << 25) |
1258 (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start);
1259 emuxki_write(sc, chano, EMU_CHAN_Z1, 0);
1260 emuxki_write(sc, chano, EMU_CHAN_Z2, 0);
1261 emuxki_write(sc, chano, EMU_CHAN_MAPA, mapval);
1262 emuxki_write(sc, chano, EMU_CHAN_MAPB, mapval);
1263 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRFILTER,
1264 chan->filter.current_cutoff_frequency);
1265 emuxki_write(sc, chano, EMU_CHAN_VTFT_FILTERTARGET,
1266 chan->filter.target_cutoff_frequency);
1267 emuxki_write(sc, chano, EMU_CHAN_ATKHLDM,
1268 (chan->modulation.envelope.hold_time << 8) |
1269 chan->modulation.envelope.attack_time);
1270 emuxki_write(sc, chano, EMU_CHAN_DCYSUSM,
1271 (chan->modulation.envelope.sustain_level << 8) |
1272 chan->modulation.envelope.decay_time);
1273 emuxki_write(sc, chano, EMU_CHAN_LFOVAL1,
1274 chan->modulation.LFO_state);
1275 emuxki_write(sc, chano, EMU_CHAN_LFOVAL2,
1276 chan->vibrato_LFO.state);
1277 emuxki_write(sc, chano, EMU_CHAN_FMMOD,
1278 (chan->vibrato_LFO.modulation_depth << 8) |
1279 chan->filter.LFO_modulation_depth);
1280 emuxki_write(sc, chano, EMU_CHAN_TREMFRQ,
1281 (chan->tremolo_depth << 8));
1282 emuxki_write(sc, chano, EMU_CHAN_FM2FRQ2,
1283 (chan->vibrato_LFO.vibrato_depth << 8) |
1284 chan->vibrato_LFO.frequency);
1285 emuxki_write(sc, chano, EMU_CHAN_ENVVAL,
1286 chan->modulation.envelope.current_state);
1287 emuxki_write(sc, chano, EMU_CHAN_ATKHLDV,
1288 (chan->volume.envelope.hold_time << 8) |
1289 chan->volume.envelope.attack_time);
1290 emuxki_write(sc, chano, EMU_CHAN_ENVVOL,
1291 chan->volume.envelope.current_state);
1292 emuxki_write(sc, chano, EMU_CHAN_PEFE,
1293 (chan->pitch.envelope_amount << 8) |
1294 chan->filter.envelope_amount);
1295 splx(s);
1296 }
1297
1298 static void
1299 emuxki_channel_start(struct emuxki_channel *chan)
1300 {
1301 struct emuxki_voice *voice;
1302 struct emuxki_softc *sc;
1303 u_int8_t cache_sample, cache_invalid_size, chano;
1304 u_int32_t sample;
1305 int s;
1306
1307 voice = chan->voice;
1308 sc = voice->sc;
1309 chano = chan->num;
1310 cache_sample = voice->stereo ? 4 : 2;
1311 sample = voice->b16 ? 0x00000000 : 0x80808080;
1312 cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1);
1313
1314 s = splaudio();
1315 while (cache_sample--) {
1316 emuxki_write(sc, chano, EMU_CHAN_CD0 + cache_sample,
1317 sample);
1318 }
1319 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
1320 emuxki_write(sc, chano, EMU_CHAN_CCR_READADDRESS, 64);
1321 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE,
1322 cache_invalid_size);
1323 emuxki_write(sc, chano, EMU_CHAN_IFATN,
1324 (chan->filter.target_cutoff_frequency << 8) |
1325 chan->initial_attenuation);
1326 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET,
1327 chan->volume.target);
1328 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL,
1329 chan->volume.current);
1330 emuxki_write(sc, 0,
1331 EMU_MKSUBREG(1, chano, EMU_SOLEL + (chano >> 5)),
1332 0); /* Clear stop on loop */
1333 emuxki_write(sc, 0,
1334 EMU_MKSUBREG(1, chano, EMU_CLIEL + (chano >> 5)),
1335 0); /* Clear loop interrupt */
1336 emuxki_write(sc, chano, EMU_CHAN_DCYSUSV,
1337 (chan->volume.envelope.sustain_level << 8) |
1338 chan->volume.envelope.decay_time);
1339 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET,
1340 chan->pitch.target);
1341 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH,
1342 chan->pitch.current);
1343 emuxki_write(sc, chano, EMU_CHAN_IP, chan->pitch.initial);
1344
1345 splx(s);
1346 }
1347
1348 static void
1349 emuxki_channel_stop(struct emuxki_channel *chan)
1350 {
1351 struct emuxki_softc *sc;
1352 int s;
1353 u_int8_t chano;
1354
1355 sc = chan->voice->sc;
1356 chano = chan->num;
1357 s = splaudio();
1358 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 0);
1359 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 0);
1360 emuxki_write(sc, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff);
1361 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0);
1362 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 0);
1363 emuxki_write(sc, chano, EMU_CHAN_IP, 0);
1364 splx(s);
1365 }
1366
1367 /*
1368 * Voices management
1369 * emuxki_voice_dataloc : use(play or rec) independent dataloc union helpers
1370 * emuxki_voice_channel_* : play part of dataloc union helpers
1371 * emuxki_voice_recsrc_* : rec part of dataloc union helpers
1372 */
1373
1374 /* Allocate channels for voice in case of play voice */
1375 static int
1376 emuxki_voice_channel_create(struct emuxki_voice *voice)
1377 {
1378 struct emuxki_channel **channel;
1379 int s;
1380 uint8_t i, stereo;
1381
1382 channel = voice->sc->channel;
1383 stereo = voice->stereo;
1384 for (i = 0; i < EMU_NUMCHAN - stereo; i += stereo + 1) {
1385 if ((stereo && (channel[i + 1] != NULL)) ||
1386 (channel[i] != NULL)) /* Looking for free channels */
1387 continue;
1388 s = splaudio();
1389 if (stereo) {
1390 voice->dataloc.chan[1] =
1391 emuxki_channel_new(voice, i + 1);
1392 if (voice->dataloc.chan[1] == NULL) {
1393 splx(s);
1394 return ENOMEM;
1395 }
1396 }
1397 voice->dataloc.chan[0] = emuxki_channel_new(voice, i);
1398 if (voice->dataloc.chan[0] == NULL) {
1399 if (stereo) {
1400 emuxki_channel_delete(voice->dataloc.chan[1]);
1401 voice->dataloc.chan[1] = NULL;
1402 }
1403 splx(s);
1404 return ENOMEM;
1405 }
1406 splx(s);
1407 return 0;
1408 }
1409 return EAGAIN;
1410 }
1411
1412 /* When calling this function we assume no one can access the voice */
1413 static void
1414 emuxki_voice_channel_destroy(struct emuxki_voice *voice)
1415 {
1416
1417 emuxki_channel_delete(voice->dataloc.chan[0]);
1418 voice->dataloc.chan[0] = NULL;
1419 if (voice->stereo)
1420 emuxki_channel_delete(voice->dataloc.chan[1]);
1421 voice->dataloc.chan[1] = NULL;
1422 }
1423
1424 /*
1425 * Will come back when used in voice_dataloc_create
1426 */
1427 static int
1428 emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source)
1429 {
1430
1431 if (source >= EMU_NUMRECSRCS) {
1432 #ifdef EMUXKI_DEBUG
1433 printf("Tried to reserve invalid source: %d\n", source);
1434 #endif
1435 return EINVAL;
1436 }
1437 if (voice->sc->recsrc[source] == voice)
1438 return 0; /* XXX */
1439 if (voice->sc->recsrc[source] != NULL)
1440 return EBUSY;
1441 voice->sc->recsrc[source] = voice;
1442 return 0;
1443 }
1444
1445 /* When calling this function we assume the voice is stopped */
1446 static void
1447 emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source)
1448 {
1449
1450 sc->recsrc[source] = NULL;
1451 }
1452
1453 static int
1454 emuxki_voice_dataloc_create(struct emuxki_voice *voice)
1455 {
1456 int error;
1457
1458 if (voice->use & EMU_VOICE_USE_PLAY) {
1459 if ((error = emuxki_voice_channel_create(voice)))
1460 return error;
1461 } else {
1462 if ((error =
1463 emuxki_recsrc_reserve(voice, voice->dataloc.source)))
1464 return error;
1465 }
1466 return 0;
1467 }
1468
1469 static void
1470 emuxki_voice_dataloc_destroy(struct emuxki_voice *voice)
1471 {
1472
1473 if (voice->use & EMU_VOICE_USE_PLAY) {
1474 if (voice->dataloc.chan[0] != NULL)
1475 emuxki_voice_channel_destroy(voice);
1476 } else {
1477 if (voice->dataloc.source != EMU_RECSRC_NOTSET) {
1478 emuxki_voice_recsrc_release(voice->sc,
1479 voice->dataloc.source);
1480 voice->dataloc.source = EMU_RECSRC_NOTSET;
1481 }
1482 }
1483 }
1484
1485 static struct emuxki_voice *
1486 emuxki_voice_new(struct emuxki_softc *sc, uint8_t use)
1487 {
1488 struct emuxki_voice *voice;
1489 int s;
1490
1491 s = splaudio();
1492 voice = sc->lvoice;
1493 sc->lvoice = NULL;
1494 splx(s);
1495
1496 if (!voice) {
1497 if (!(voice = malloc(sizeof(*voice), M_DEVBUF, M_WAITOK)))
1498 return NULL;
1499 } else if (voice->use != use)
1500 emuxki_voice_dataloc_destroy(voice);
1501 else
1502 goto skip_initialize;
1503
1504 voice->sc = sc;
1505 voice->state = !EMU_VOICE_STATE_STARTED;
1506 voice->stereo = EMU_VOICE_STEREO_NOTSET;
1507 voice->b16 = 0;
1508 voice->sample_rate = 0;
1509 if (use & EMU_VOICE_USE_PLAY)
1510 voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL;
1511 else
1512 voice->dataloc.source = EMU_RECSRC_NOTSET;
1513 voice->buffer = NULL;
1514 voice->blksize = 0;
1515 voice->trigblk = 0;
1516 voice->blkmod = 0;
1517 voice->inth = NULL;
1518 voice->inthparam = NULL;
1519 voice->use = use;
1520
1521 skip_initialize:
1522 s = splaudio();
1523 LIST_INSERT_HEAD((&sc->voices), voice, next);
1524 splx(s);
1525
1526 return voice;
1527 }
1528
1529 static void
1530 emuxki_voice_delete(struct emuxki_voice *voice)
1531 {
1532 struct emuxki_softc *sc;
1533 struct emuxki_voice *lvoice;
1534 int s;
1535
1536 sc = voice->sc;
1537 if (voice->state & EMU_VOICE_STATE_STARTED)
1538 emuxki_voice_halt(voice);
1539
1540 s = splaudio();
1541 LIST_REMOVE(voice, next);
1542 lvoice = sc->lvoice;
1543 sc->lvoice = voice;
1544 splx(s);
1545
1546 if (lvoice) {
1547 emuxki_voice_dataloc_destroy(lvoice);
1548 free(lvoice, M_DEVBUF);
1549 }
1550 }
1551
1552 static int
1553 emuxki_voice_set_stereo(struct emuxki_voice *voice, uint8_t stereo)
1554 {
1555 int error;
1556 emuxki_recsrc_t source;
1557 struct emuxki_chanparms_fxsend fxsend;
1558
1559 source = 0; /* XXX: gcc */
1560 if (! (voice->use & EMU_VOICE_USE_PLAY))
1561 source = voice->dataloc.source;
1562 emuxki_voice_dataloc_destroy(voice);
1563 if (! (voice->use & EMU_VOICE_USE_PLAY))
1564 voice->dataloc.source = source;
1565 voice->stereo = stereo;
1566 if ((error = emuxki_voice_dataloc_create(voice)))
1567 return error;
1568 if (voice->use & EMU_VOICE_USE_PLAY) {
1569 fxsend.a.dest = 0x0;
1570 fxsend.b.dest = 0x1;
1571 fxsend.c.dest = 0x2;
1572 fxsend.d.dest = 0x3;
1573 /* for audigy */
1574 fxsend.e.dest = 0x4;
1575 fxsend.f.dest = 0x5;
1576 fxsend.g.dest = 0x6;
1577 fxsend.h.dest = 0x7;
1578 if (voice->stereo) {
1579 fxsend.a.level = fxsend.c.level = 0xc0;
1580 fxsend.b.level = fxsend.d.level = 0x00;
1581 fxsend.e.level = fxsend.g.level = 0xc0;
1582 fxsend.f.level = fxsend.h.level = 0x00;
1583 emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1584 &fxsend);
1585 fxsend.a.level = fxsend.c.level = 0x00;
1586 fxsend.b.level = fxsend.d.level = 0xc0;
1587 fxsend.e.level = fxsend.g.level = 0x00;
1588 fxsend.f.level = fxsend.h.level = 0xc0;
1589 emuxki_channel_set_fxsend(voice->dataloc.chan[1],
1590 &fxsend);
1591 } /* No else : default is good for mono */
1592 }
1593 return 0;
1594 }
1595
1596 static int
1597 emuxki_voice_set_srate(struct emuxki_voice *voice, uint32_t srate)
1598 {
1599
1600 if (voice->use & EMU_VOICE_USE_PLAY) {
1601 if ((srate < 4000) || (srate > 48000))
1602 return EINVAL;
1603 voice->sample_rate = srate;
1604 emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1605 if (voice->stereo)
1606 emuxki_channel_set_srate(voice->dataloc.chan[1],
1607 srate);
1608 } else {
1609 if ((srate < 8000) || (srate > 48000))
1610 return EINVAL;
1611 voice->sample_rate = srate;
1612 if (emuxki_voice_adc_rate(voice) < 0) {
1613 voice->sample_rate = 0;
1614 return EINVAL;
1615 }
1616 }
1617 return 0;
1618 }
1619
1620 static int
1621 emuxki_voice_set_audioparms(struct emuxki_voice *voice, uint8_t stereo,
1622 uint8_t b16, uint32_t srate)
1623 {
1624 int error;
1625
1626 if (voice->stereo == stereo && voice->b16 == b16 &&
1627 voice->sample_rate == srate)
1628 return 0;
1629
1630 #ifdef EMUXKI_DEBUG
1631 printf("Setting %s voice params : %s, %u bits, %u Hz\n",
1632 (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record",
1633 stereo ? "stereo" : "mono", (b16 + 1) * 8, srate);
1634 #endif
1635 error = 0;
1636 if (voice->stereo != stereo) {
1637 if ((error = emuxki_voice_set_stereo(voice, stereo)))
1638 return error;
1639 }
1640 voice->b16 = b16;
1641 if (voice->sample_rate != srate)
1642 error = emuxki_voice_set_srate(voice, srate);
1643 return error;
1644 }
1645
1646 /* voice audio parms (see just before) must be set prior to this */
1647 static int
1648 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr,
1649 uint32_t bufsize, uint16_t blksize)
1650 {
1651 struct emuxki_mem *mem;
1652 struct emuxki_channel **chan;
1653 uint32_t start, end;
1654 uint8_t sample_size;
1655 int idx;
1656 int error;
1657
1658 error = EFAULT;
1659 LIST_FOREACH(mem, &voice->sc->mem, next) {
1660 if (KERNADDR(mem->dmamem) != ptr)
1661 continue;
1662
1663 voice->buffer = mem;
1664 sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1665 voice->trigblk = 0; /* This shouldn't be needed */
1666 voice->blkmod = bufsize / blksize;
1667 if (bufsize % blksize) /* This should not happen */
1668 voice->blkmod++;
1669 error = 0;
1670
1671 if (voice->use & EMU_VOICE_USE_PLAY) {
1672 voice->blksize = blksize / sample_size;
1673 chan = voice->dataloc.chan;
1674 start = mem->ptbidx << 12;
1675 end = start + bufsize / sample_size;
1676 emuxki_channel_set_bufparms(chan[0],
1677 start, end);
1678 if (voice->stereo)
1679 emuxki_channel_set_bufparms(chan[1],
1680 start, end);
1681 voice->timerate = (uint32_t) 48000 *
1682 voice->blksize / voice->sample_rate;
1683 if (voice->timerate < 5)
1684 error = EINVAL;
1685 } else {
1686 voice->blksize = blksize;
1687 for(idx = sizeof(emuxki_recbuf_sz) /
1688 sizeof(emuxki_recbuf_sz[0]); --idx >= 0;)
1689 if (emuxki_recbuf_sz[idx] == bufsize)
1690 break;
1691 if (idx < 0) {
1692 #ifdef EMUXKI_DEBUG
1693 printf("Invalid bufsize: %d\n", bufsize);
1694 #endif
1695 return EINVAL;
1696 }
1697 emuxki_write(voice->sc, 0,
1698 emuxki_recsrc_szreg[voice->dataloc.source], idx);
1699 emuxki_write(voice->sc, 0,
1700 emuxki_recsrc_bufaddrreg[voice->dataloc.source],
1701 DMAADDR(mem->dmamem));
1702
1703 /* Use timer to emulate DMA completion interrupt */
1704 voice->timerate = (u_int32_t) 48000 * blksize /
1705 (voice->sample_rate * sample_size);
1706 if (voice->timerate < 5) {
1707 #ifdef EMUXKI_DEBUG
1708 printf("Invalid timerate: %d, blksize %d\n",
1709 voice->timerate, blksize);
1710 #endif
1711 error = EINVAL;
1712 }
1713 }
1714
1715 break;
1716 }
1717
1718 return error;
1719 }
1720
1721 static void
1722 emuxki_voice_commit_parms(struct emuxki_voice *voice)
1723 {
1724 if (voice->use & EMU_VOICE_USE_PLAY) {
1725 emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1726 if (voice->stereo)
1727 emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1728 }
1729 }
1730
1731 static uint32_t
1732 emuxki_voice_curaddr(struct emuxki_voice *voice)
1733 {
1734 int idxreg;
1735
1736 /* XXX different semantics in these cases */
1737 if (voice->use & EMU_VOICE_USE_PLAY) {
1738 /* returns number of samples (an l/r pair counts 1) */
1739 return emuxki_read(voice->sc,
1740 voice->dataloc.chan[0]->num, EMU_CHAN_CCCA_CURRADDR) -
1741 voice->dataloc.chan[0]->loop.start;
1742 } else {
1743 idxreg = 0;
1744 /* returns number of bytes */
1745 switch (voice->dataloc.source) {
1746 case EMU_RECSRC_MIC:
1747 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ?
1748 EMU_A_MICIDX : EMU_MICIDX;
1749 break;
1750 case EMU_RECSRC_ADC:
1751 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ?
1752 EMU_A_ADCIDX : EMU_ADCIDX;
1753 break;
1754 case EMU_RECSRC_FX:
1755 idxreg = EMU_FXIDX;
1756 break;
1757 default:
1758 #ifdef EMUXKI_DEBUG
1759 printf("emu: bad recording source!\n");
1760 #endif
1761 break;
1762 }
1763 return emuxki_read(voice->sc, 0, EMU_RECIDX(idxreg)
1764 & EMU_RECIDX_MASK);
1765 }
1766 return 0;
1767 }
1768
1769 static void
1770 emuxki_resched_timer(struct emuxki_softc *sc)
1771 {
1772 struct emuxki_voice *voice;
1773 uint16_t timerate;
1774 uint8_t active;
1775 int s;
1776
1777 timerate = 1024;
1778 active = 0;
1779 s = splaudio();
1780 LIST_FOREACH(voice, &sc->voices, next) {
1781 if ((voice->state & EMU_VOICE_STATE_STARTED) == 0)
1782 continue;
1783 active = 1;
1784 if (voice->timerate < timerate)
1785 timerate = voice->timerate;
1786 }
1787
1788 if (timerate & ~EMU_TIMER_RATE_MASK)
1789 timerate = 0;
1790 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_TIMER, timerate);
1791 if (!active && (sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1792 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1793 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) &
1794 ~EMU_INTE_INTERTIMERENB);
1795 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED;
1796 } else if (active && !(sc->timerstate & EMU_TIMER_STATE_ENABLED)) {
1797 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE,
1798 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) |
1799 EMU_INTE_INTERTIMERENB);
1800 sc->timerstate |= EMU_TIMER_STATE_ENABLED;
1801 }
1802 splx(s);
1803 }
1804
1805 static int
1806 emuxki_voice_adc_rate(struct emuxki_voice *voice)
1807 {
1808
1809 switch(voice->sample_rate) {
1810 case 48000:
1811 return EMU_ADCCR_SAMPLERATE_48;
1812 break;
1813 case 44100:
1814 return EMU_ADCCR_SAMPLERATE_44;
1815 break;
1816 case 32000:
1817 return EMU_ADCCR_SAMPLERATE_32;
1818 break;
1819 case 24000:
1820 return EMU_ADCCR_SAMPLERATE_24;
1821 break;
1822 case 22050:
1823 return EMU_ADCCR_SAMPLERATE_22;
1824 break;
1825 case 16000:
1826 return EMU_ADCCR_SAMPLERATE_16;
1827 break;
1828 case 12000:
1829 if(voice->sc->sc_type & EMUXKI_AUDIGY)
1830 return EMU_A_ADCCR_SAMPLERATE_12;
1831 else {
1832 #ifdef EMUXKI_DEBUG
1833 printf("recording sample_rate not supported : %u\n", voice->sample_rate);
1834 #endif
1835 return -1;
1836 }
1837 break;
1838 case 11000:
1839 if(voice->sc->sc_type & EMUXKI_AUDIGY)
1840 return EMU_A_ADCCR_SAMPLERATE_11;
1841 else
1842 return EMU_ADCCR_SAMPLERATE_11;
1843 break;
1844 case 8000:
1845 if(voice->sc->sc_type & EMUXKI_AUDIGY)
1846 return EMU_A_ADCCR_SAMPLERATE_8;
1847 else
1848 return EMU_ADCCR_SAMPLERATE_8;
1849 break;
1850 default:
1851 #ifdef EMUXKI_DEBUG
1852 printf("recording sample_rate not supported : %u\n", voice->sample_rate);
1853 #endif
1854 return -1;
1855 }
1856 return -1; /* shouldn't get here */
1857 }
1858
1859
1860 static void
1861 emuxki_voice_start(struct emuxki_voice *voice,
1862 void (*inth) (void *), void *inthparam)
1863 {
1864 uint32_t val;
1865
1866 voice->inth = inth;
1867 voice->inthparam = inthparam;
1868 if (voice->use & EMU_VOICE_USE_PLAY) {
1869 voice->trigblk = 1;
1870 emuxki_channel_start(voice->dataloc.chan[0]);
1871 if (voice->stereo)
1872 emuxki_channel_start(voice->dataloc.chan[1]);
1873 } else {
1874 voice->trigblk = 1;
1875 switch (voice->dataloc.source) {
1876 case EMU_RECSRC_ADC:
1877 /* XXX need to program DSP to output L+R
1878 * XXX in monaural case? */
1879 if (voice->sc->sc_type & EMUXKI_AUDIGY) {
1880 val = EMU_A_ADCCR_LCHANENABLE;
1881 if (voice->stereo)
1882 val |= EMU_A_ADCCR_RCHANENABLE;
1883 } else {
1884 val = EMU_ADCCR_LCHANENABLE;
1885 if (voice->stereo)
1886 val |= EMU_ADCCR_RCHANENABLE;
1887 }
1888 val |= emuxki_voice_adc_rate(voice);
1889 emuxki_write(voice->sc, 0, EMU_ADCCR, 0);
1890 emuxki_write(voice->sc, 0, EMU_ADCCR, val);
1891 break;
1892 case EMU_RECSRC_MIC:
1893 case EMU_RECSRC_FX:
1894 printf("unimplemented\n");
1895 break;
1896 case EMU_RECSRC_NOTSET:
1897 default:
1898 printf("Bad dataloc.source %d\n",
1899 voice->dataloc.source);
1900 break;
1901 }
1902 #if 0
1903 switch (voice->dataloc.source) {
1904 case EMU_RECSRC_ADC:
1905 case EMU_RECSRC_FX:
1906 case EMU_RECSRC_MIC:
1907 /* DMA completion interrupt is useless; use timer */
1908 int s;
1909 s = splaudio();
1910 val = emu_rd(sc, INTE, 4);
1911 val |= emuxki_recsrc_intrmasks[voice->dataloc.source];
1912 emu_wr(sc, INTE, val, 4);
1913 splx(s);
1914 break;
1915 default:
1916 break;
1917 }
1918 #endif
1919 }
1920 voice->state |= EMU_VOICE_STATE_STARTED;
1921 emuxki_resched_timer(voice->sc);
1922 }
1923
1924 static void
1925 emuxki_voice_halt(struct emuxki_voice *voice)
1926 {
1927
1928 if (voice->use & EMU_VOICE_USE_PLAY) {
1929 emuxki_channel_stop(voice->dataloc.chan[0]);
1930 if (voice->stereo)
1931 emuxki_channel_stop(voice->dataloc.chan[1]);
1932 } else {
1933 switch (voice->dataloc.source) {
1934 case EMU_RECSRC_ADC:
1935 emuxki_write(voice->sc, 0, EMU_ADCCR, 0);
1936 break;
1937 case EMU_RECSRC_FX:
1938 case EMU_RECSRC_MIC:
1939 printf("unimplemented\n");
1940 break;
1941 default:
1942 case EMU_RECSRC_NOTSET:
1943 printf("Bad dataloc.source %d\n",
1944 voice->dataloc.source);
1945 break;
1946 }
1947
1948 switch (voice->dataloc.source) {
1949 case EMU_RECSRC_ADC:
1950 case EMU_RECSRC_FX:
1951 case EMU_RECSRC_MIC:
1952 /* This should reset buffer pointer */
1953 emuxki_write(voice->sc, 0,
1954 emuxki_recsrc_szreg[voice->dataloc.source],
1955 EMU_RECBS_BUFSIZE_NONE);
1956 #if 0
1957 int s;
1958 s = splaudio();
1959 val = emu_rd(sc, INTE, 4);
1960 val &= ~emuxki_recsrc_intrmasks[voice->dataloc.source];
1961 emu_wr(sc, INTE, val, 4);
1962 splx(s);
1963 #endif
1964 break;
1965 default:
1966 break;
1967 }
1968 }
1969 voice->state &= ~EMU_VOICE_STATE_STARTED;
1970 emuxki_resched_timer(voice->sc);
1971 }
1972
1973 /*
1974 * The interrupt handler
1975 */
1976 static int
1977 emuxki_intr(void *arg)
1978 {
1979 struct emuxki_softc *sc;
1980 struct emuxki_voice *voice;
1981 uint32_t ipr, curblk;
1982 int claim;
1983
1984 sc = arg;
1985 claim = 0;
1986 while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) {
1987 if (ipr & EMU_IPR_INTERVALTIMER) {
1988 LIST_FOREACH(voice, &sc->voices, next) {
1989 if ((voice->state &
1990 EMU_VOICE_STATE_STARTED) == 0)
1991 continue;
1992
1993 curblk = emuxki_voice_curaddr(voice) /
1994 voice->blksize;
1995 #if 0
1996 if (curblk == voice->trigblk) {
1997 voice->inth(voice->inthparam);
1998 voice->trigblk++;
1999 voice->trigblk %= voice->blkmod;
2000 }
2001 #else
2002 while ((curblk >= voice->trigblk &&
2003 curblk < (voice->trigblk + voice->blkmod / 2)) ||
2004 ((int)voice->trigblk - (int)curblk) >
2005 (voice->blkmod / 2 + 1)) {
2006 voice->inth(voice->inthparam);
2007 voice->trigblk++;
2008 voice->trigblk %= voice->blkmod;
2009 }
2010 #endif
2011 }
2012 }
2013
2014 /* Got interrupt */
2015 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr);
2016
2017 claim = 1;
2018 }
2019
2020 return claim;
2021 }
2022
2023
2024 /*
2025 * Audio Architecture callbacks
2026 */
2027
2028 static int
2029 emuxki_open(void *addr, int flags)
2030 {
2031 struct emuxki_softc *sc;
2032
2033 sc = addr;
2034 #ifdef EMUXKI_DEBUG
2035 printf("%s: emuxki_open called\n", sc->sc_dev.dv_xname);
2036 #endif
2037
2038 /*
2039 * Multiple voice support would be added as soon as I find a way to
2040 * trick the audio arch into supporting multiple voices.
2041 * Or I might integrate a modified audio arch supporting
2042 * multiple voices.
2043 */
2044
2045 /*
2046 * I did this because i have problems identifying the selected
2047 * recording source(s) which is necessary when setting recording
2048 * params This will be addressed very soon
2049 */
2050 if (flags & AUOPEN_READ) {
2051 sc->rvoice = emuxki_voice_new(sc, 0 /* EMU_VOICE_USE_RECORD */);
2052 if (sc->rvoice == NULL)
2053 return EBUSY;
2054
2055 /* XXX Hardcode RECSRC_ADC for now */
2056 sc->rvoice->dataloc.source = EMU_RECSRC_ADC;
2057 }
2058
2059 if (flags & AUOPEN_WRITE) {
2060 sc->pvoice = emuxki_voice_new(sc, EMU_VOICE_USE_PLAY);
2061 if (sc->pvoice == NULL) {
2062 if (sc->rvoice) {
2063 emuxki_voice_delete(sc->rvoice);
2064 sc->rvoice = NULL;
2065 }
2066 return EBUSY;
2067 }
2068 }
2069
2070 return 0;
2071 }
2072
2073 static void
2074 emuxki_close(void *addr)
2075 {
2076 struct emuxki_softc *sc;
2077
2078 sc = addr;
2079 #ifdef EMUXKI_DEBUG
2080 printf("%s: emu10K1_close called\n", sc->sc_dev.dv_xname);
2081 #endif
2082
2083 /* No multiple voice support for now */
2084 if (sc->rvoice != NULL) {
2085 emuxki_voice_delete(sc->rvoice);
2086 sc->rvoice = NULL;
2087 }
2088 if (sc->pvoice != NULL) {
2089 emuxki_voice_delete(sc->pvoice);
2090 sc->pvoice = NULL;
2091 }
2092 }
2093
2094 static int
2095 emuxki_query_encoding(void *addr, struct audio_encoding *fp)
2096 {
2097 #ifdef EMUXKI_DEBUG
2098 struct emuxki_softc *sc;
2099
2100 sc = addr;
2101 printf("%s: emuxki_query_encoding called\n", sc->sc_dev.dv_xname);
2102 #endif
2103
2104 switch (fp->index) {
2105 case 0:
2106 strcpy(fp->name, AudioEulinear);
2107 fp->encoding = AUDIO_ENCODING_ULINEAR;
2108 fp->precision = 8;
2109 fp->flags = 0;
2110 break;
2111 case 1:
2112 strcpy(fp->name, AudioEmulaw);
2113 fp->encoding = AUDIO_ENCODING_ULAW;
2114 fp->precision = 8;
2115 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2116 break;
2117 case 2:
2118 strcpy(fp->name, AudioEalaw);
2119 fp->encoding = AUDIO_ENCODING_ALAW;
2120 fp->precision = 8;
2121 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2122 break;
2123 case 3:
2124 strcpy(fp->name, AudioEslinear);
2125 fp->encoding = AUDIO_ENCODING_SLINEAR;
2126 fp->precision = 8;
2127 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2128 break;
2129 case 4:
2130 strcpy(fp->name, AudioEslinear_le);
2131 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
2132 fp->precision = 16;
2133 fp->flags = 0;
2134 break;
2135 case 5:
2136 strcpy(fp->name, AudioEulinear_le);
2137 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
2138 fp->precision = 16;
2139 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2140 break;
2141 case 6:
2142 strcpy(fp->name, AudioEslinear_be);
2143 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
2144 fp->precision = 16;
2145 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2146 break;
2147 case 7:
2148 strcpy(fp->name, AudioEulinear_be);
2149 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
2150 fp->precision = 16;
2151 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
2152 break;
2153 default:
2154 return EINVAL;
2155 }
2156 return 0;
2157 }
2158
2159 static int
2160 emuxki_set_vparms(struct emuxki_voice *voice, const audio_params_t *p,
2161 stream_filter_list_t *fil)
2162 {
2163 int mode, i;
2164
2165 mode = (voice->use & EMU_VOICE_USE_PLAY) ?
2166 AUMODE_PLAY : AUMODE_RECORD;
2167 i = auconv_set_converter(emuxki_formats, EMUXKI_NFORMATS,
2168 mode, p, FALSE, fil);
2169 if (i < 0)
2170 return EINVAL;
2171 if (fil->req_size > 0)
2172 p = &fil->filters[0].param;
2173 return emuxki_voice_set_audioparms
2174 (voice, p->channels == 2, p->precision == 16, p->sample_rate);
2175 }
2176
2177 static int
2178 emuxki_set_params(void *addr, int setmode, int usemode, audio_params_t *play,
2179 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
2180 {
2181 struct emuxki_softc *sc;
2182 struct audio_params *p;
2183 struct emuxki_voice *v;
2184 stream_filter_list_t *fil;
2185 int mode, error;
2186
2187 sc = addr;
2188 for (mode = AUMODE_RECORD; mode != -1;
2189 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
2190 if ((usemode & setmode & mode) == 0)
2191 continue;
2192
2193 if (mode == AUMODE_PLAY) {
2194 p = play;
2195 fil = pfil;
2196 v = sc->pvoice;
2197 } else {
2198 p = rec;
2199 fil = rfil;
2200 v = sc->rvoice;
2201 }
2202
2203 if (v == NULL) {
2204 continue;
2205 }
2206
2207 /* No multiple voice support for now */
2208 if ((error = emuxki_set_vparms(v, p, fil)))
2209 return error;
2210 }
2211
2212 return 0;
2213 }
2214
2215 static int
2216 emuxki_halt_output(void *addr)
2217 {
2218 struct emuxki_softc *sc;
2219
2220 sc = addr;
2221 /* No multiple voice support for now */
2222 if (sc->pvoice == NULL)
2223 return ENXIO;
2224
2225 emuxki_voice_halt(sc->pvoice);
2226 return 0;
2227 }
2228
2229 static int
2230 emuxki_halt_input(void *addr)
2231 {
2232 struct emuxki_softc *sc;
2233
2234 sc = addr;
2235 #ifdef EMUXKI_DEBUG
2236 printf("%s: emuxki_halt_input called\n", sc->sc_dev.dv_xname);
2237 #endif
2238
2239 /* No multiple voice support for now */
2240 if (sc->rvoice == NULL)
2241 return ENXIO;
2242 emuxki_voice_halt(sc->rvoice);
2243 return 0;
2244 }
2245
2246 static int
2247 emuxki_getdev(void *addr, struct audio_device *dev)
2248 {
2249 struct emuxki_softc *sc;
2250
2251 sc = addr;
2252 *dev = sc->sc_audv;
2253 return 0;
2254 }
2255
2256 static int
2257 emuxki_set_port(void *addr, mixer_ctrl_t *mctl)
2258 {
2259 struct emuxki_softc *sc;
2260
2261 sc = addr;
2262 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
2263 }
2264
2265 static int
2266 emuxki_get_port(void *addr, mixer_ctrl_t *mctl)
2267 {
2268 struct emuxki_softc *sc;
2269
2270 sc = addr;
2271 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
2272 }
2273
2274 static int
2275 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo)
2276 {
2277 struct emuxki_softc *sc;
2278
2279 sc = addr;
2280 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
2281 }
2282
2283 static void *
2284 emuxki_allocm(void *addr, int direction, size_t size,
2285 struct malloc_type *type, int flags)
2286 {
2287 if (direction == AUMODE_PLAY)
2288 return emuxki_pmem_alloc(addr, size, type, flags);
2289 else
2290 return emuxki_rmem_alloc(addr, size, type, flags);
2291 }
2292
2293 static void
2294 emuxki_freem(void *addr, void *ptr, struct malloc_type *type)
2295 {
2296 struct emuxki_softc *sc;
2297 struct emuxki_mem *mem;
2298 uint32_t *ptb, silentpage;
2299 size_t numblocks;
2300 int i, s;
2301
2302 sc = addr;
2303 ptb = KERNADDR(sc->ptb);
2304 silentpage = DMAADDR(sc->silentpage) << 1;
2305 LIST_FOREACH(mem, &sc->mem, next) {
2306 if (KERNADDR(mem->dmamem) != ptr)
2307 continue;
2308
2309 s = splaudio();
2310 if (mem->ptbidx != EMU_RMEM) {
2311 numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE;
2312 if (DMASIZE(mem->dmamem) % EMU_PTESIZE)
2313 numblocks++;
2314 for (i = 0; i < numblocks; i++)
2315 ptb[mem->ptbidx + i] =
2316 htole32(silentpage | (mem->ptbidx + i));
2317 }
2318 LIST_REMOVE(mem, next);
2319 splx(s);
2320
2321 emuxki_mem_delete(mem, type);
2322 break;
2323 }
2324 }
2325
2326 /* blocksize should be a divisor of allowable buffersize */
2327 /* XXX probably this could be done better */
2328 static int
2329 emuxki_round_blocksize(void *addr, int blksize,
2330 int mode, const audio_params_t* param)
2331 {
2332 #if 0
2333 struct emuxki_softc *sc;
2334 struct audio_softc *au;
2335 #endif
2336 int bufsize;
2337 #if 0
2338 sc = addr;
2339 if (sc == NULL)
2340 return blksize;
2341
2342 au = (void *)sc->sc_audev;
2343 if (au == NULL)
2344 return blksize;
2345
2346 bufsize = emuxki_round_buffersize(sc, AUMODE_RECORD,
2347 au->sc_rr.bufsize);
2348 #else
2349 bufsize = 65536;
2350 #endif
2351
2352 while (bufsize > blksize)
2353 bufsize /= 2;
2354
2355 return bufsize;
2356 }
2357
2358 static size_t
2359 emuxki_round_buffersize(void *addr, int direction, size_t bsize)
2360 {
2361
2362 if (direction == AUMODE_PLAY) {
2363 if (bsize < EMU_PTESIZE)
2364 bsize = EMU_PTESIZE;
2365 else if (bsize > (EMU_PTESIZE * EMU_MAXPTE))
2366 bsize = EMU_PTESIZE * EMU_MAXPTE;
2367 /* Would be better if set to max available */
2368 else if (bsize % EMU_PTESIZE)
2369 bsize = bsize -
2370 (bsize % EMU_PTESIZE) +
2371 EMU_PTESIZE;
2372 } else {
2373 int idx;
2374
2375 /* find nearest lower recbuf size */
2376 for(idx = sizeof(emuxki_recbuf_sz) /
2377 sizeof(emuxki_recbuf_sz[0]); --idx >= 0; ) {
2378 if (bsize >= emuxki_recbuf_sz[idx]) {
2379 bsize = emuxki_recbuf_sz[idx];
2380 break;
2381 }
2382 }
2383
2384 if (bsize == 0)
2385 bsize = 384;
2386 }
2387
2388 return bsize;
2389 }
2390
2391 static paddr_t
2392 emuxki_mappage(void *addr, void *ptr, off_t off, int prot)
2393 {
2394 struct emuxki_softc *sc;
2395 struct emuxki_mem *mem;
2396
2397 sc = addr;
2398 LIST_FOREACH(mem, &sc->mem, next) {
2399 if (KERNADDR(mem->dmamem) == ptr) {
2400 struct dmamem *dm = mem->dmamem;
2401
2402 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs,
2403 off, prot, BUS_DMA_WAITOK);
2404 }
2405 }
2406
2407 return -1;
2408 }
2409
2410 static int
2411 emuxki_get_props(void *addr)
2412 {
2413 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
2414 AUDIO_PROP_FULLDUPLEX;
2415 }
2416
2417 static int
2418 emuxki_trigger_output(void *addr, void *start, void *end, int blksize,
2419 void (*inth) (void *), void *inthparam, const audio_params_t *params)
2420 {
2421 struct emuxki_softc *sc;
2422 /* No multiple voice support for now */
2423 struct emuxki_voice *voice;
2424 int error;
2425
2426 sc = addr;
2427 voice = sc->pvoice;
2428 if (voice == NULL)
2429 return ENXIO;
2430 if ((error = emuxki_voice_set_audioparms(voice, params->channels == 2,
2431 params->precision == 16, params->sample_rate)))
2432 return error;
2433 if ((error = emuxki_voice_set_bufparms(voice, start,
2434 (caddr_t)end - (caddr_t)start, blksize)))
2435 return error;
2436 emuxki_voice_commit_parms(voice);
2437 emuxki_voice_start(voice, inth, inthparam);
2438
2439 return 0;
2440 }
2441
2442 static int
2443 emuxki_trigger_input(void *addr, void *start, void *end, int blksize,
2444 void (*inth) (void *), void *inthparam, const audio_params_t *params)
2445 {
2446 struct emuxki_softc *sc;
2447 /* No multiple voice support for now */
2448 struct emuxki_voice *voice;
2449 int error;
2450
2451 sc = addr;
2452 voice = sc->rvoice;
2453 if (voice == NULL)
2454 return ENXIO;
2455 if ((error = emuxki_voice_set_audioparms(voice, params->channels == 2,
2456 params->precision == 16, params->sample_rate)))
2457 return error;
2458 if ((error = emuxki_voice_set_bufparms(voice, start,
2459 (caddr_t)end - (caddr_t)start, blksize)))
2460 return error;
2461 emuxki_voice_start(voice, inth, inthparam);
2462
2463 return 0;
2464 }
2465
2466 /*
2467 * AC97 callbacks
2468 */
2469
2470 static int
2471 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif)
2472 {
2473 struct emuxki_softc *sc;
2474
2475 sc = arg;
2476 sc->codecif = codecif;
2477 return 0;
2478 }
2479
2480 static int
2481 emuxki_ac97_read(void *arg, uint8_t reg, uint16_t *val)
2482 {
2483 struct emuxki_softc *sc;
2484 int s;
2485
2486 sc = arg;
2487 s = splaudio();
2488 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2489 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA);
2490 splx(s);
2491
2492 return 0;
2493 }
2494
2495 static int
2496 emuxki_ac97_write(void *arg, uint8_t reg, uint16_t val)
2497 {
2498 struct emuxki_softc *sc;
2499 int s;
2500
2501 sc = arg;
2502 s = splaudio();
2503 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg);
2504 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val);
2505 splx(s);
2506
2507 return 0;
2508 }
2509
2510 static int
2511 emuxki_ac97_reset(void *arg)
2512 {
2513
2514 return 0;
2515 }
2516
2517 enum ac97_host_flags
2518 emuxki_ac97_flags(void *arg)
2519 {
2520
2521 return AC97_HOST_SWAPPED_CHANNELS;
2522 }
2523