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