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