sv.c revision 1.29 1 /* $NetBSD: sv.c,v 1.29 2005/04/30 15:24:51 hannken Exp $ */
2 /* $OpenBSD: sv.c,v 1.2 1998/07/13 01:50:15 csapuntz Exp $ */
3
4 /*
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1998 Constantine Paul Sapuntzakis
42 * All rights reserved
43 *
44 * Author: Constantine Paul Sapuntzakis (csapuntz (at) cvs.openbsd.org)
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. The author's name or those of the contributors may be used to
55 * endorse or promote products derived from this software without
56 * specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * S3 SonicVibes driver
73 * Heavily based on the eap driver by Lennart Augustsson
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: sv.c,v 1.29 2005/04/30 15:24:51 hannken Exp $");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/device.h>
84
85 #include <dev/pci/pcireg.h>
86 #include <dev/pci/pcivar.h>
87 #include <dev/pci/pcidevs.h>
88
89 #include <sys/audioio.h>
90 #include <dev/audio_if.h>
91 #include <dev/mulaw.h>
92 #include <dev/auconv.h>
93
94 #include <dev/ic/i8237reg.h>
95 #include <dev/pci/svreg.h>
96 #include <dev/pci/svvar.h>
97
98 #include <machine/bus.h>
99
100 /* XXX
101 * The SonicVibes DMA is broken and only works on 24-bit addresses.
102 * As long as bus_dmamem_alloc_range() is missing we use the ISA
103 * DMA tag on i386.
104 */
105 #if defined(i386)
106 #include "isa.h"
107 #if NISA > 0
108 #include <dev/isa/isavar.h>
109 #endif
110 #endif
111
112 #ifdef AUDIO_DEBUG
113 #define DPRINTF(x) if (svdebug) printf x
114 #define DPRINTFN(n,x) if (svdebug>(n)) printf x
115 int svdebug = 0;
116 #else
117 #define DPRINTF(x)
118 #define DPRINTFN(n,x)
119 #endif
120
121 int sv_match(struct device *, struct cfdata *, void *);
122 void sv_attach(struct device *, struct device *, void *);
123 int sv_intr(void *);
124
125 struct sv_dma {
126 bus_dmamap_t map;
127 caddr_t addr;
128 bus_dma_segment_t segs[1];
129 int nsegs;
130 size_t size;
131 struct sv_dma *next;
132 };
133 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
134 #define KERNADDR(p) ((void *)((p)->addr))
135
136 CFATTACH_DECL(sv, sizeof(struct sv_softc),
137 sv_match, sv_attach, NULL, NULL);
138
139 struct audio_device sv_device = {
140 "S3 SonicVibes",
141 "",
142 "sv"
143 };
144
145 #define ARRAY_SIZE(foo) ((sizeof(foo)) / sizeof(foo[0]))
146
147 int sv_allocmem(struct sv_softc *, size_t, size_t, int, struct sv_dma *);
148 int sv_freemem(struct sv_softc *, struct sv_dma *);
149
150 int sv_open(void *, int);
151 int sv_query_encoding(void *, struct audio_encoding *);
152 int sv_set_params(void *, int, int, audio_params_t *, audio_params_t *,
153 stream_filter_list_t *, stream_filter_list_t *);
154 int sv_round_blocksize(void *, int, int, const audio_params_t *);
155 int sv_trigger_output(void *, void *, void *, int, void (*)(void *),
156 void *, const audio_params_t *);
157 int sv_trigger_input(void *, void *, void *, int, void (*)(void *),
158 void *, const audio_params_t *);
159 int sv_halt_output(void *);
160 int sv_halt_input(void *);
161 int sv_getdev(void *, struct audio_device *);
162 int sv_mixer_set_port(void *, mixer_ctrl_t *);
163 int sv_mixer_get_port(void *, mixer_ctrl_t *);
164 int sv_query_devinfo(void *, mixer_devinfo_t *);
165 void *sv_malloc(void *, int, size_t, struct malloc_type *, int);
166 void sv_free(void *, void *, struct malloc_type *);
167 size_t sv_round_buffersize(void *, int, size_t);
168 paddr_t sv_mappage(void *, void *, off_t, int);
169 int sv_get_props(void *);
170
171 #ifdef AUDIO_DEBUG
172 void sv_dumpregs(struct sv_softc *sc);
173 #endif
174
175 const struct audio_hw_if sv_hw_if = {
176 sv_open,
177 NULL, /* close */
178 NULL,
179 sv_query_encoding,
180 sv_set_params,
181 sv_round_blocksize,
182 NULL,
183 NULL,
184 NULL,
185 NULL,
186 NULL,
187 sv_halt_output,
188 sv_halt_input,
189 NULL,
190 sv_getdev,
191 NULL,
192 sv_mixer_set_port,
193 sv_mixer_get_port,
194 sv_query_devinfo,
195 sv_malloc,
196 sv_free,
197 sv_round_buffersize,
198 sv_mappage,
199 sv_get_props,
200 sv_trigger_output,
201 sv_trigger_input,
202 NULL,
203 };
204
205 #define SV_NFORMATS 4
206 static const struct audio_format sv_formats[SV_NFORMATS] = {
207 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
208 2, AUFMT_STEREO, 0, {2000, 48000}},
209 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
210 1, AUFMT_MONAURAL, 0, {2000, 48000}},
211 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
212 2, AUFMT_STEREO, 0, {2000, 48000}},
213 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
214 1, AUFMT_MONAURAL, 0, {2000, 48000}},
215 };
216
217
218 static uint8_t sv_read(struct sv_softc *, uint8_t);
219 static uint8_t sv_read_indirect(struct sv_softc *, uint8_t);
220 static void sv_write(struct sv_softc *, uint8_t, uint8_t);
221 static void sv_write_indirect(struct sv_softc *, uint8_t, uint8_t);
222 static void sv_init_mixer(struct sv_softc *);
223
224 static void sv_defer(struct device *self);
225
226 static void
227 sv_write(struct sv_softc *sc, uint8_t reg, uint8_t val)
228 {
229
230 DPRINTFN(8,("sv_write(0x%x, 0x%x)\n", reg, val));
231 bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val);
232 }
233
234 static uint8_t
235 sv_read(struct sv_softc *sc, uint8_t reg)
236 {
237 uint8_t val;
238
239 val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg);
240 DPRINTFN(8,("sv_read(0x%x) = 0x%x\n", reg, val));
241 return val;
242 }
243
244 static uint8_t
245 sv_read_indirect(struct sv_softc *sc, uint8_t reg)
246 {
247 uint8_t val;
248 int s;
249
250 s = splaudio();
251 sv_write(sc, SV_CODEC_IADDR, reg & SV_IADDR_MASK);
252 val = sv_read(sc, SV_CODEC_IDATA);
253 splx(s);
254 return val;
255 }
256
257 static void
258 sv_write_indirect(struct sv_softc *sc, uint8_t reg, uint8_t val)
259 {
260 uint8_t iaddr;
261 int s;
262
263 iaddr = reg & SV_IADDR_MASK;
264 s = splaudio();
265 if (reg == SV_DMA_DATA_FORMAT)
266 iaddr |= SV_IADDR_MCE;
267
268 sv_write(sc, SV_CODEC_IADDR, iaddr);
269 sv_write(sc, SV_CODEC_IDATA, val);
270 splx(s);
271 }
272
273 int
274 sv_match(struct device *parent, struct cfdata *match, void *aux)
275 {
276 struct pci_attach_args *pa;
277
278 pa = aux;
279 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_S3 &&
280 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_S3_SONICVIBES)
281 return 1;
282
283 return 0;
284 }
285
286 int pci_alloc_io(pci_chipset_tag_t, pcitag_t, int,
287 bus_space_tag_t, bus_size_t,
288 bus_size_t, bus_size_t, int,
289 bus_space_handle_t *);
290
291 static pcireg_t pci_io_alloc_low, pci_io_alloc_high;
292
293 int
294 pci_alloc_io(pci_chipset_tag_t pc, pcitag_t pt, int pcioffs,
295 bus_space_tag_t iot, bus_size_t size, bus_size_t align,
296 bus_size_t bound, int flags, bus_space_handle_t *ioh)
297 {
298 bus_addr_t addr;
299 int error;
300
301 error = bus_space_alloc(iot, pci_io_alloc_low, pci_io_alloc_high,
302 size, align, bound, flags, &addr, ioh);
303 if (error)
304 return error;
305
306 pci_conf_write(pc, pt, pcioffs, addr);
307 return 0;
308 }
309
310 /*
311 * Allocate IO addresses when all other configuration is done.
312 */
313 void
314 sv_defer(struct device *self)
315 {
316 struct sv_softc *sc;
317 pci_chipset_tag_t pc;
318 pcitag_t pt;
319 pcireg_t dmaio;
320
321 sc = (struct sv_softc *)self;
322 pc = sc->sc_pa.pa_pc;
323 pt = sc->sc_pa.pa_tag;
324 DPRINTF(("sv_defer: %p\n", sc));
325
326 /* XXX
327 * Get a reasonable default for the I/O range.
328 * Assume the range around SB_PORTBASE is valid on this PCI bus.
329 */
330 pci_io_alloc_low = pci_conf_read(pc, pt, SV_SB_PORTBASE_SLOT);
331 pci_io_alloc_high = pci_io_alloc_low + 0x1000;
332
333 if (pci_alloc_io(pc, pt, SV_DMAA_CONFIG_OFF,
334 sc->sc_iot, SV_DMAA_SIZE, SV_DMAA_ALIGN, 0,
335 0, &sc->sc_dmaa_ioh)) {
336 printf("sv_attach: cannot allocate DMA A range\n");
337 return;
338 }
339 dmaio = pci_conf_read(pc, pt, SV_DMAA_CONFIG_OFF);
340 DPRINTF(("sv_attach: addr a dmaio=0x%lx\n", (u_long)dmaio));
341 pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF,
342 dmaio | SV_DMA_CHANNEL_ENABLE | SV_DMAA_EXTENDED_ADDR);
343
344 if (pci_alloc_io(pc, pt, SV_DMAC_CONFIG_OFF,
345 sc->sc_iot, SV_DMAC_SIZE, SV_DMAC_ALIGN, 0,
346 0, &sc->sc_dmac_ioh)) {
347 printf("sv_attach: cannot allocate DMA C range\n");
348 return;
349 }
350 dmaio = pci_conf_read(pc, pt, SV_DMAC_CONFIG_OFF);
351 DPRINTF(("sv_attach: addr c dmaio=0x%lx\n", (u_long)dmaio));
352 pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF,
353 dmaio | SV_DMA_CHANNEL_ENABLE);
354
355 sc->sc_dmaset = 1;
356 }
357
358 void
359 sv_attach(struct device *parent, struct device *self, void *aux)
360 {
361 struct sv_softc *sc;
362 struct pci_attach_args *pa;
363 pci_chipset_tag_t pc;
364 pcitag_t pt;
365 pci_intr_handle_t ih;
366 pcireg_t csr;
367 char const *intrstr;
368 uint8_t reg;
369 struct audio_attach_args arg;
370
371 sc = (struct sv_softc *)self;
372 pa = aux;
373 pc = pa->pa_pc;
374 pt = pa->pa_tag;
375 printf ("\n");
376
377 /* Map I/O registers */
378 if (pci_mapreg_map(pa, SV_ENHANCED_PORTBASE_SLOT,
379 PCI_MAPREG_TYPE_IO, 0,
380 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
381 printf("%s: can't map enhanced i/o space\n",
382 sc->sc_dev.dv_xname);
383 return;
384 }
385 if (pci_mapreg_map(pa, SV_FM_PORTBASE_SLOT,
386 PCI_MAPREG_TYPE_IO, 0,
387 &sc->sc_opliot, &sc->sc_oplioh, NULL, NULL)) {
388 printf("%s: can't map FM i/o space\n", sc->sc_dev.dv_xname);
389 return;
390 }
391 if (pci_mapreg_map(pa, SV_MIDI_PORTBASE_SLOT,
392 PCI_MAPREG_TYPE_IO, 0,
393 &sc->sc_midiiot, &sc->sc_midiioh, NULL, NULL)) {
394 printf("%s: can't map MIDI i/o space\n", sc->sc_dev.dv_xname);
395 return;
396 }
397 DPRINTF(("sv: IO ports: enhanced=0x%x, OPL=0x%x, MIDI=0x%x\n",
398 (int)sc->sc_ioh, (int)sc->sc_oplioh, (int)sc->sc_midiioh));
399
400 #if defined(alpha)
401 /* XXX Force allocation through the SGMAP. */
402 sc->sc_dmatag = alphabus_dma_get_tag(pa->pa_dmat, ALPHA_BUS_ISA);
403 #elif defined(i386) && NISA > 0
404 /* XXX
405 * The SonicVibes DMA is broken and only works on 24-bit addresses.
406 * As long as bus_dmamem_alloc_range() is missing we use the ISA
407 * DMA tag on i386.
408 */
409 sc->sc_dmatag = &isa_bus_dma_tag;
410 #else
411 sc->sc_dmatag = pa->pa_dmat;
412 #endif
413
414 pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF, SV_DMAA_EXTENDED_ADDR);
415 pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF, 0);
416
417 /* Enable the device. */
418 csr = pci_conf_read(pc, pt, PCI_COMMAND_STATUS_REG);
419 pci_conf_write(pc, pt, PCI_COMMAND_STATUS_REG,
420 csr | PCI_COMMAND_MASTER_ENABLE);
421
422 sv_write_indirect(sc, SV_ANALOG_POWER_DOWN_CONTROL, 0);
423 sv_write_indirect(sc, SV_DIGITAL_POWER_DOWN_CONTROL, 0);
424
425 /* initialize codec registers */
426 reg = sv_read(sc, SV_CODEC_CONTROL);
427 reg |= SV_CTL_RESET;
428 sv_write(sc, SV_CODEC_CONTROL, reg);
429 delay(50);
430
431 reg = sv_read(sc, SV_CODEC_CONTROL);
432 reg &= ~SV_CTL_RESET;
433 reg |= SV_CTL_INTA | SV_CTL_ENHANCED;
434
435 /* This write clears the reset */
436 sv_write(sc, SV_CODEC_CONTROL, reg);
437 delay(50);
438
439 /* This write actually shoves the new values in */
440 sv_write(sc, SV_CODEC_CONTROL, reg);
441
442 DPRINTF(("sv_attach: control=0x%x\n", sv_read(sc, SV_CODEC_CONTROL)));
443
444 /* Enable DMA interrupts */
445 reg = sv_read(sc, SV_CODEC_INTMASK);
446 reg &= ~(SV_INTMASK_DMAA | SV_INTMASK_DMAC);
447 reg |= SV_INTMASK_UD | SV_INTMASK_SINT | SV_INTMASK_MIDI;
448 sv_write(sc, SV_CODEC_INTMASK, reg);
449
450 sv_read(sc, SV_CODEC_STATUS);
451
452 /* Map and establish the interrupt. */
453 if (pci_intr_map(pa, &ih)) {
454 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
455 return;
456 }
457 intrstr = pci_intr_string(pc, ih);
458 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, sv_intr, sc);
459 if (sc->sc_ih == NULL) {
460 printf("%s: couldn't establish interrupt",
461 sc->sc_dev.dv_xname);
462 if (intrstr != NULL)
463 printf(" at %s", intrstr);
464 printf("\n");
465 return;
466 }
467 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
468 printf("%s: rev %d", sc->sc_dev.dv_xname,
469 sv_read_indirect(sc, SV_REVISION_LEVEL));
470 if (sv_read(sc, SV_CODEC_CONTROL) & SV_CTL_MD1)
471 printf(", reverb SRAM present");
472 if (!(sv_read_indirect(sc, SV_WAVETABLE_SOURCE_SELECT) & SV_WSS_WT0))
473 printf(", wavetable ROM present");
474 printf("\n");
475
476 sv_init_mixer(sc);
477
478 audio_attach_mi(&sv_hw_if, sc, &sc->sc_dev);
479
480 arg.type = AUDIODEV_TYPE_OPL;
481 arg.hwif = 0;
482 arg.hdl = 0;
483 (void)config_found(&sc->sc_dev, &arg, audioprint);
484
485 sc->sc_pa = *pa; /* for deferred setup */
486 config_defer(self, sv_defer);
487 }
488
489 #ifdef AUDIO_DEBUG
490 void
491 sv_dumpregs(struct sv_softc *sc)
492 {
493 int idx;
494
495 #if 0
496 for (idx = 0; idx < 0x50; idx += 4)
497 printf ("%02x = %x\n", idx,
498 pci_conf_read(pa->pa_pc, pa->pa_tag, idx));
499 #endif
500
501 for (idx = 0; idx < 6; idx++)
502 printf ("REG %02x = %02x\n", idx, sv_read(sc, idx));
503
504 for (idx = 0; idx < 0x32; idx++)
505 printf ("IREG %02x = %02x\n", idx, sv_read_indirect(sc, idx));
506
507 for (idx = 0; idx < 0x10; idx++)
508 printf ("DMA %02x = %02x\n", idx,
509 bus_space_read_1(sc->sc_iot, sc->sc_dmaa_ioh, idx));
510 }
511 #endif
512
513 int
514 sv_intr(void *p)
515 {
516 struct sv_softc *sc;
517 uint8_t intr;
518
519 sc = p;
520 intr = sv_read(sc, SV_CODEC_STATUS);
521 DPRINTFN(5,("sv_intr: intr=0x%x\n", intr));
522
523 if (!(intr & (SV_INTSTATUS_DMAA | SV_INTSTATUS_DMAC)))
524 return 0;
525
526 if (intr & SV_INTSTATUS_DMAA) {
527 if (sc->sc_pintr)
528 sc->sc_pintr(sc->sc_parg);
529 }
530
531 if (intr & SV_INTSTATUS_DMAC) {
532 if (sc->sc_rintr)
533 sc->sc_rintr(sc->sc_rarg);
534 }
535
536 return 1;
537 }
538
539 int
540 sv_allocmem(struct sv_softc *sc, size_t size, size_t align,
541 int direction, struct sv_dma *p)
542 {
543 int error;
544
545 p->size = size;
546 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
547 p->segs, ARRAY_SIZE(p->segs), &p->nsegs, BUS_DMA_NOWAIT);
548 if (error)
549 return error;
550
551 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
552 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
553 if (error)
554 goto free;
555
556 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
557 0, BUS_DMA_NOWAIT, &p->map);
558 if (error)
559 goto unmap;
560
561 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
562 BUS_DMA_NOWAIT | (direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE);
563 if (error)
564 goto destroy;
565 DPRINTF(("sv_allocmem: pa=%lx va=%lx pba=%lx\n",
566 (long)p->segs[0].ds_addr, (long)KERNADDR(p), (long)DMAADDR(p)));
567 return 0;
568
569 destroy:
570 bus_dmamap_destroy(sc->sc_dmatag, p->map);
571 unmap:
572 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
573 free:
574 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
575 return error;
576 }
577
578 int
579 sv_freemem(struct sv_softc *sc, struct sv_dma *p)
580 {
581
582 bus_dmamap_unload(sc->sc_dmatag, p->map);
583 bus_dmamap_destroy(sc->sc_dmatag, p->map);
584 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
585 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
586 return 0;
587 }
588
589 int
590 sv_open(void *addr, int flags)
591 {
592 struct sv_softc *sc;
593
594 sc = addr;
595 DPRINTF(("sv_open\n"));
596 if (!sc->sc_dmaset)
597 return ENXIO;
598
599 return 0;
600 }
601
602 int
603 sv_query_encoding(void *addr, struct audio_encoding *fp)
604 {
605
606 switch (fp->index) {
607 case 0:
608 strcpy(fp->name, AudioEulinear);
609 fp->encoding = AUDIO_ENCODING_ULINEAR;
610 fp->precision = 8;
611 fp->flags = 0;
612 return 0;
613 case 1:
614 strcpy(fp->name, AudioEmulaw);
615 fp->encoding = AUDIO_ENCODING_ULAW;
616 fp->precision = 8;
617 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
618 return 0;
619 case 2:
620 strcpy(fp->name, AudioEalaw);
621 fp->encoding = AUDIO_ENCODING_ALAW;
622 fp->precision = 8;
623 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
624 return 0;
625 case 3:
626 strcpy(fp->name, AudioEslinear);
627 fp->encoding = AUDIO_ENCODING_SLINEAR;
628 fp->precision = 8;
629 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
630 return 0;
631 case 4:
632 strcpy(fp->name, AudioEslinear_le);
633 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
634 fp->precision = 16;
635 fp->flags = 0;
636 return 0;
637 case 5:
638 strcpy(fp->name, AudioEulinear_le);
639 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
640 fp->precision = 16;
641 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
642 return 0;
643 case 6:
644 strcpy(fp->name, AudioEslinear_be);
645 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
646 fp->precision = 16;
647 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
648 return 0;
649 case 7:
650 strcpy(fp->name, AudioEulinear_be);
651 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
652 fp->precision = 16;
653 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
654 return 0;
655 default:
656 return EINVAL;
657 }
658 }
659
660 int
661 sv_set_params(void *addr, int setmode, int usemode, audio_params_t *play,
662 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
663 {
664 struct sv_softc *sc;
665 audio_params_t *p;
666 uint32_t val;
667
668 sc = addr;
669 p = NULL;
670 /*
671 * This device only has one clock, so make the sample rates match.
672 */
673 if (play->sample_rate != rec->sample_rate &&
674 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
675 if (setmode == AUMODE_PLAY) {
676 rec->sample_rate = play->sample_rate;
677 setmode |= AUMODE_RECORD;
678 } else if (setmode == AUMODE_RECORD) {
679 play->sample_rate = rec->sample_rate;
680 setmode |= AUMODE_PLAY;
681 } else
682 return EINVAL;
683 }
684
685 if (setmode & AUMODE_RECORD) {
686 p = rec;
687 if (auconv_set_converter(sv_formats, SV_NFORMATS,
688 AUMODE_RECORD, rec, FALSE, rfil) < 0)
689 return EINVAL;
690 }
691 if (setmode & AUMODE_PLAY) {
692 p = play;
693 if (auconv_set_converter(sv_formats, SV_NFORMATS,
694 AUMODE_PLAY, play, FALSE, pfil) < 0)
695 return EINVAL;
696 }
697
698 if (p == NULL)
699 return 0;
700
701 val = p->sample_rate * 65536 / 48000;
702 /*
703 * If the sample rate is exactly 48KHz, the fraction would overflow the
704 * register, so we have to bias it. This causes a little clock drift.
705 * The drift is below normal crystal tolerance (.0001%), so although
706 * this seems a little silly, we can pretty much ignore it.
707 * (I tested the output speed with values of 1-20, just to be sure this
708 * register isn't *supposed* to have a bias. It isn't.)
709 * - mycroft
710 */
711 if (val > 65535)
712 val = 65535;
713
714 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_0, val & 0xff);
715 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_1, val >> 8);
716
717 #define F_REF 24576000
718
719 #define ABS(x) (((x) < 0) ? (-x) : (x))
720
721 if (setmode & AUMODE_RECORD) {
722 /* The ADC reference frequency (f_out) is 512 * sample rate */
723
724 /* f_out is dervied from the 24.576MHz crystal by three values:
725 M & N & R. The equation is as follows:
726
727 f_out = (m + 2) * f_ref / ((n + 2) * (2 ^ a))
728
729 with the constraint that:
730
731 80 MHz < (m + 2) / (n + 2) * f_ref <= 150MHz
732 and n, m >= 1
733 */
734
735 int goal_f_out;
736 int a, n, m, best_n, best_m, best_error;
737 int pll_sample;
738 int error;
739
740 goal_f_out = 512 * rec->sample_rate;
741 best_n = 0;
742 best_m = 0;
743 best_error = 10000000;
744 for (a = 0; a < 8; a++) {
745 if ((goal_f_out * (1 << a)) >= 80000000)
746 break;
747 }
748
749 /* a != 8 because sample_rate >= 2000 */
750
751 for (n = 33; n > 2; n--) {
752 m = (goal_f_out * n * (1 << a)) / F_REF;
753 if ((m > 257) || (m < 3))
754 continue;
755
756 pll_sample = (m * F_REF) / (n * (1 << a));
757 pll_sample /= 512;
758
759 /* Threshold might be good here */
760 error = pll_sample - rec->sample_rate;
761 error = ABS(error);
762
763 if (error < best_error) {
764 best_error = error;
765 best_n = n;
766 best_m = m;
767 if (error == 0) break;
768 }
769 }
770
771 best_n -= 2;
772 best_m -= 2;
773
774 sv_write_indirect(sc, SV_ADC_PLL_M, best_m);
775 sv_write_indirect(sc, SV_ADC_PLL_N,
776 best_n | (a << SV_PLL_R_SHIFT));
777 }
778
779 return 0;
780 }
781
782 int
783 sv_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
784 {
785
786 return blk & -32; /* keep good alignment */
787 }
788
789 int
790 sv_trigger_output(void *addr, void *start, void *end, int blksize,
791 void (*intr)(void *), void *arg, const audio_params_t *param)
792 {
793 struct sv_softc *sc;
794 struct sv_dma *p;
795 uint8_t mode;
796 int dma_count;
797
798 DPRINTFN(1, ("sv_trigger_output: sc=%p start=%p end=%p blksize=%d "
799 "intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
800 sc = addr;
801 sc->sc_pintr = intr;
802 sc->sc_parg = arg;
803
804 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
805 mode &= ~(SV_DMAA_FORMAT16 | SV_DMAA_STEREO);
806 if (param->precision == 16)
807 mode |= SV_DMAA_FORMAT16;
808 if (param->channels == 2)
809 mode |= SV_DMAA_STEREO;
810 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
811
812 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
813 continue;
814 if (p == NULL) {
815 printf("sv_trigger_output: bad addr %p\n", start);
816 return EINVAL;
817 }
818
819 dma_count = ((char *)end - (char *)start) - 1;
820 DPRINTF(("sv_trigger_output: DMA start loop input addr=%x cc=%d\n",
821 (int)DMAADDR(p), dma_count));
822
823 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0,
824 DMAADDR(p));
825 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_COUNT0,
826 dma_count);
827 bus_space_write_1(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_MODE,
828 DMA37MD_READ | DMA37MD_LOOP);
829
830 DPRINTF(("sv_trigger_output: current addr=%x\n",
831 bus_space_read_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0)));
832
833 dma_count = blksize - 1;
834
835 sv_write_indirect(sc, SV_DMAA_COUNT1, dma_count >> 8);
836 sv_write_indirect(sc, SV_DMAA_COUNT0, dma_count & 0xFF);
837
838 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
839 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_PLAY_ENABLE);
840
841 return 0;
842 }
843
844 int
845 sv_trigger_input(void *addr, void *start, void *end, int blksize,
846 void (*intr)(void *), void *arg, const audio_params_t *param)
847 {
848 struct sv_softc *sc;
849 struct sv_dma *p;
850 uint8_t mode;
851 int dma_count;
852
853 DPRINTFN(1, ("sv_trigger_input: sc=%p start=%p end=%p blksize=%d "
854 "intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
855 sc = addr;
856 sc->sc_rintr = intr;
857 sc->sc_rarg = arg;
858
859 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
860 mode &= ~(SV_DMAC_FORMAT16 | SV_DMAC_STEREO);
861 if (param->precision == 16)
862 mode |= SV_DMAC_FORMAT16;
863 if (param->channels == 2)
864 mode |= SV_DMAC_STEREO;
865 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
866
867 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
868 continue;
869 if (!p) {
870 printf("sv_trigger_input: bad addr %p\n", start);
871 return EINVAL;
872 }
873
874 dma_count = (((char *)end - (char *)start) >> 1) - 1;
875 DPRINTF(("sv_trigger_input: DMA start loop input addr=%x cc=%d\n",
876 (int)DMAADDR(p), dma_count));
877
878 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0,
879 DMAADDR(p));
880 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_COUNT0,
881 dma_count);
882 bus_space_write_1(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_MODE,
883 DMA37MD_WRITE | DMA37MD_LOOP);
884
885 DPRINTF(("sv_trigger_input: current addr=%x\n",
886 bus_space_read_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0)));
887
888 dma_count = (blksize >> 1) - 1;
889
890 sv_write_indirect(sc, SV_DMAC_COUNT1, dma_count >> 8);
891 sv_write_indirect(sc, SV_DMAC_COUNT0, dma_count & 0xFF);
892
893 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
894 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_RECORD_ENABLE);
895
896 return 0;
897 }
898
899 int
900 sv_halt_output(void *addr)
901 {
902 struct sv_softc *sc;
903 uint8_t mode;
904
905 DPRINTF(("sv: sv_halt_output\n"));
906 sc = addr;
907 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
908 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_PLAY_ENABLE);
909 sc->sc_pintr = 0;
910
911 return 0;
912 }
913
914 int
915 sv_halt_input(void *addr)
916 {
917 struct sv_softc *sc;
918 uint8_t mode;
919
920 DPRINTF(("sv: sv_halt_input\n"));
921 sc = addr;
922 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
923 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_RECORD_ENABLE);
924 sc->sc_rintr = 0;
925
926 return 0;
927 }
928
929 int
930 sv_getdev(void *addr, struct audio_device *retp)
931 {
932
933 *retp = sv_device;
934 return 0;
935 }
936
937
938 /*
939 * Mixer related code is here
940 *
941 */
942
943 #define SV_INPUT_CLASS 0
944 #define SV_OUTPUT_CLASS 1
945 #define SV_RECORD_CLASS 2
946
947 #define SV_LAST_CLASS 2
948
949 static const char *mixer_classes[] =
950 { AudioCinputs, AudioCoutputs, AudioCrecord };
951
952 static const struct {
953 uint8_t l_port;
954 uint8_t r_port;
955 uint8_t mask;
956 uint8_t class;
957 const char *audio;
958 } ports[] = {
959 { SV_LEFT_AUX1_INPUT_CONTROL, SV_RIGHT_AUX1_INPUT_CONTROL, SV_AUX1_MASK,
960 SV_INPUT_CLASS, "aux1" },
961 { SV_LEFT_CD_INPUT_CONTROL, SV_RIGHT_CD_INPUT_CONTROL, SV_CD_MASK,
962 SV_INPUT_CLASS, AudioNcd },
963 { SV_LEFT_LINE_IN_INPUT_CONTROL, SV_RIGHT_LINE_IN_INPUT_CONTROL, SV_LINE_IN_MASK,
964 SV_INPUT_CLASS, AudioNline },
965 { SV_MIC_INPUT_CONTROL, 0, SV_MIC_MASK, SV_INPUT_CLASS, AudioNmicrophone },
966 { SV_LEFT_SYNTH_INPUT_CONTROL, SV_RIGHT_SYNTH_INPUT_CONTROL,
967 SV_SYNTH_MASK, SV_INPUT_CLASS, AudioNfmsynth },
968 { SV_LEFT_AUX2_INPUT_CONTROL, SV_RIGHT_AUX2_INPUT_CONTROL, SV_AUX2_MASK,
969 SV_INPUT_CLASS, "aux2" },
970 { SV_LEFT_PCM_INPUT_CONTROL, SV_RIGHT_PCM_INPUT_CONTROL, SV_PCM_MASK,
971 SV_INPUT_CLASS, AudioNdac },
972 { SV_LEFT_MIXER_OUTPUT_CONTROL, SV_RIGHT_MIXER_OUTPUT_CONTROL,
973 SV_MIXER_OUT_MASK, SV_OUTPUT_CLASS, AudioNmaster }
974 };
975
976
977 static const struct {
978 int idx;
979 const char *name;
980 } record_sources[] = {
981 { SV_REC_CD, AudioNcd },
982 { SV_REC_DAC, AudioNdac },
983 { SV_REC_AUX2, "aux2" },
984 { SV_REC_LINE, AudioNline },
985 { SV_REC_AUX1, "aux1" },
986 { SV_REC_MIC, AudioNmicrophone },
987 { SV_REC_MIXER, AudioNmixerout }
988 };
989
990
991 #define SV_DEVICES_PER_PORT 2
992 #define SV_FIRST_MIXER (SV_LAST_CLASS + 1)
993 #define SV_LAST_MIXER (SV_DEVICES_PER_PORT * (ARRAY_SIZE(ports)) + SV_LAST_CLASS)
994 #define SV_RECORD_SOURCE (SV_LAST_MIXER + 1)
995 #define SV_MIC_BOOST (SV_LAST_MIXER + 2)
996 #define SV_RECORD_GAIN (SV_LAST_MIXER + 3)
997 #define SV_SRS_MODE (SV_LAST_MIXER + 4)
998
999 int
1000 sv_query_devinfo(void *addr, mixer_devinfo_t *dip)
1001 {
1002 int i;
1003
1004 /* It's a class */
1005 if (dip->index <= SV_LAST_CLASS) {
1006 dip->type = AUDIO_MIXER_CLASS;
1007 dip->mixer_class = dip->index;
1008 dip->next = dip->prev = AUDIO_MIXER_LAST;
1009 strcpy(dip->label.name, mixer_classes[dip->index]);
1010 return 0;
1011 }
1012
1013 if (dip->index >= SV_FIRST_MIXER &&
1014 dip->index <= SV_LAST_MIXER) {
1015 int off, mute ,idx;
1016
1017 off = dip->index - SV_FIRST_MIXER;
1018 mute = (off % SV_DEVICES_PER_PORT);
1019 idx = off / SV_DEVICES_PER_PORT;
1020 dip->mixer_class = ports[idx].class;
1021 strcpy(dip->label.name, ports[idx].audio);
1022
1023 if (!mute) {
1024 dip->type = AUDIO_MIXER_VALUE;
1025 dip->prev = AUDIO_MIXER_LAST;
1026 dip->next = dip->index + 1;
1027
1028 if (ports[idx].r_port != 0)
1029 dip->un.v.num_channels = 2;
1030 else
1031 dip->un.v.num_channels = 1;
1032
1033 strcpy(dip->un.v.units.name, AudioNvolume);
1034 } else {
1035 dip->type = AUDIO_MIXER_ENUM;
1036 dip->prev = dip->index - 1;
1037 dip->next = AUDIO_MIXER_LAST;
1038
1039 strcpy(dip->label.name, AudioNmute);
1040 dip->un.e.num_mem = 2;
1041 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1042 dip->un.e.member[0].ord = 0;
1043 strcpy(dip->un.e.member[1].label.name, AudioNon);
1044 dip->un.e.member[1].ord = 1;
1045 }
1046
1047 return 0;
1048 }
1049
1050 switch (dip->index) {
1051 case SV_RECORD_SOURCE:
1052 dip->mixer_class = SV_RECORD_CLASS;
1053 dip->prev = AUDIO_MIXER_LAST;
1054 dip->next = SV_RECORD_GAIN;
1055 strcpy(dip->label.name, AudioNsource);
1056 dip->type = AUDIO_MIXER_ENUM;
1057
1058 dip->un.e.num_mem = ARRAY_SIZE(record_sources);
1059 for (i = 0; i < ARRAY_SIZE(record_sources); i++) {
1060 strcpy(dip->un.e.member[i].label.name,
1061 record_sources[i].name);
1062 dip->un.e.member[i].ord = record_sources[i].idx;
1063 }
1064 return 0;
1065
1066 case SV_RECORD_GAIN:
1067 dip->mixer_class = SV_RECORD_CLASS;
1068 dip->prev = SV_RECORD_SOURCE;
1069 dip->next = AUDIO_MIXER_LAST;
1070 strcpy(dip->label.name, "gain");
1071 dip->type = AUDIO_MIXER_VALUE;
1072 dip->un.v.num_channels = 1;
1073 strcpy(dip->un.v.units.name, AudioNvolume);
1074 return 0;
1075
1076 case SV_MIC_BOOST:
1077 dip->mixer_class = SV_RECORD_CLASS;
1078 dip->prev = AUDIO_MIXER_LAST;
1079 dip->next = AUDIO_MIXER_LAST;
1080 strcpy(dip->label.name, "micboost");
1081 goto on_off;
1082
1083 case SV_SRS_MODE:
1084 dip->mixer_class = SV_OUTPUT_CLASS;
1085 dip->prev = dip->next = AUDIO_MIXER_LAST;
1086 strcpy(dip->label.name, AudioNspatial);
1087
1088 on_off:
1089 dip->type = AUDIO_MIXER_ENUM;
1090 dip->un.e.num_mem = 2;
1091 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1092 dip->un.e.member[0].ord = 0;
1093 strcpy(dip->un.e.member[1].label.name, AudioNon);
1094 dip->un.e.member[1].ord = 1;
1095 return 0;
1096 }
1097
1098 return ENXIO;
1099 }
1100
1101 int
1102 sv_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1103 {
1104 struct sv_softc *sc;
1105 uint8_t reg;
1106 int idx;
1107
1108 sc = addr;
1109 if (cp->dev >= SV_FIRST_MIXER &&
1110 cp->dev <= SV_LAST_MIXER) {
1111 int off, mute;
1112
1113 off = cp->dev - SV_FIRST_MIXER;
1114 mute = (off % SV_DEVICES_PER_PORT);
1115 idx = off / SV_DEVICES_PER_PORT;
1116
1117 if (mute) {
1118 if (cp->type != AUDIO_MIXER_ENUM)
1119 return EINVAL;
1120
1121 reg = sv_read_indirect(sc, ports[idx].l_port);
1122 if (cp->un.ord)
1123 reg |= SV_MUTE_BIT;
1124 else
1125 reg &= ~SV_MUTE_BIT;
1126 sv_write_indirect(sc, ports[idx].l_port, reg);
1127
1128 if (ports[idx].r_port) {
1129 reg = sv_read_indirect(sc, ports[idx].r_port);
1130 if (cp->un.ord)
1131 reg |= SV_MUTE_BIT;
1132 else
1133 reg &= ~SV_MUTE_BIT;
1134 sv_write_indirect(sc, ports[idx].r_port, reg);
1135 }
1136 } else {
1137 int lval, rval;
1138
1139 if (cp->type != AUDIO_MIXER_VALUE)
1140 return EINVAL;
1141
1142 if (cp->un.value.num_channels != 1 &&
1143 cp->un.value.num_channels != 2)
1144 return (EINVAL);
1145
1146 if (ports[idx].r_port == 0) {
1147 if (cp->un.value.num_channels != 1)
1148 return (EINVAL);
1149 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1150 rval = 0; /* shut up GCC */
1151 } else {
1152 if (cp->un.value.num_channels != 2)
1153 return (EINVAL);
1154
1155 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1156 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1157 }
1158
1159
1160 reg = sv_read_indirect(sc, ports[idx].l_port);
1161 reg &= ~(ports[idx].mask);
1162 lval = (AUDIO_MAX_GAIN - lval) * ports[idx].mask /
1163 AUDIO_MAX_GAIN;
1164 reg |= lval;
1165 sv_write_indirect(sc, ports[idx].l_port, reg);
1166
1167 if (ports[idx].r_port != 0) {
1168 reg = sv_read_indirect(sc, ports[idx].r_port);
1169 reg &= ~(ports[idx].mask);
1170
1171 rval = (AUDIO_MAX_GAIN - rval) * ports[idx].mask /
1172 AUDIO_MAX_GAIN;
1173 reg |= rval;
1174
1175 sv_write_indirect(sc, ports[idx].r_port, reg);
1176 }
1177
1178 sv_read_indirect(sc, ports[idx].l_port);
1179 }
1180
1181 return 0;
1182 }
1183
1184
1185 switch (cp->dev) {
1186 case SV_RECORD_SOURCE:
1187 if (cp->type != AUDIO_MIXER_ENUM)
1188 return EINVAL;
1189
1190 for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) {
1191 if (record_sources[idx].idx == cp->un.ord)
1192 goto found;
1193 }
1194
1195 return EINVAL;
1196
1197 found:
1198 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1199 reg &= ~SV_REC_SOURCE_MASK;
1200 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1201 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1202
1203 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1204 reg &= ~SV_REC_SOURCE_MASK;
1205 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1206 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1207 return 0;
1208
1209 case SV_RECORD_GAIN:
1210 {
1211 int val;
1212
1213 if (cp->type != AUDIO_MIXER_VALUE)
1214 return EINVAL;
1215
1216 if (cp->un.value.num_channels != 1)
1217 return EINVAL;
1218
1219 val = (cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]
1220 * SV_REC_GAIN_MASK) / AUDIO_MAX_GAIN;
1221
1222 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1223 reg &= ~SV_REC_GAIN_MASK;
1224 reg |= val;
1225 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1226
1227 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1228 reg &= ~SV_REC_GAIN_MASK;
1229 reg |= val;
1230 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1231 }
1232 return (0);
1233
1234 case SV_MIC_BOOST:
1235 if (cp->type != AUDIO_MIXER_ENUM)
1236 return EINVAL;
1237
1238 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1239 if (cp->un.ord) {
1240 reg |= SV_MIC_BOOST_BIT;
1241 } else {
1242 reg &= ~SV_MIC_BOOST_BIT;
1243 }
1244
1245 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1246 return 0;
1247
1248 case SV_SRS_MODE:
1249 if (cp->type != AUDIO_MIXER_ENUM)
1250 return EINVAL;
1251
1252 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1253 if (cp->un.ord) {
1254 reg &= ~SV_SRS_SPACE_ONOFF;
1255 } else {
1256 reg |= SV_SRS_SPACE_ONOFF;
1257 }
1258
1259 sv_write_indirect(sc, SV_SRS_SPACE_CONTROL, reg);
1260 return 0;
1261 }
1262
1263 return EINVAL;
1264 }
1265
1266 int
1267 sv_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1268 {
1269 struct sv_softc *sc;
1270 int val;
1271 uint8_t reg;
1272
1273 sc = addr;
1274 if (cp->dev >= SV_FIRST_MIXER &&
1275 cp->dev <= SV_LAST_MIXER) {
1276 int off = cp->dev - SV_FIRST_MIXER;
1277 int mute = (off % 2);
1278 int idx = off / 2;
1279
1280 off = cp->dev - SV_FIRST_MIXER;
1281 mute = (off % 2);
1282 idx = off / 2;
1283 if (mute) {
1284 if (cp->type != AUDIO_MIXER_ENUM)
1285 return EINVAL;
1286
1287 reg = sv_read_indirect(sc, ports[idx].l_port);
1288 cp->un.ord = ((reg & SV_MUTE_BIT) ? 1 : 0);
1289 } else {
1290 if (cp->type != AUDIO_MIXER_VALUE)
1291 return EINVAL;
1292
1293 if (cp->un.value.num_channels != 1 &&
1294 cp->un.value.num_channels != 2)
1295 return EINVAL;
1296
1297 if ((ports[idx].r_port == 0 &&
1298 cp->un.value.num_channels != 1) ||
1299 (ports[idx].r_port != 0 &&
1300 cp->un.value.num_channels != 2))
1301 return EINVAL;
1302
1303 reg = sv_read_indirect(sc, ports[idx].l_port);
1304 reg &= ports[idx].mask;
1305
1306 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1307
1308 if (ports[idx].r_port != 0) {
1309 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = val;
1310
1311 reg = sv_read_indirect(sc, ports[idx].r_port);
1312 reg &= ports[idx].mask;
1313
1314 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN)
1315 / ports[idx].mask);
1316 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = val;
1317 } else
1318 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = val;
1319 }
1320
1321 return 0;
1322 }
1323
1324 switch (cp->dev) {
1325 case SV_RECORD_SOURCE:
1326 if (cp->type != AUDIO_MIXER_ENUM)
1327 return EINVAL;
1328
1329 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1330 cp->un.ord = ((reg & SV_REC_SOURCE_MASK) >> SV_REC_SOURCE_SHIFT);
1331
1332 return 0;
1333
1334 case SV_RECORD_GAIN:
1335 if (cp->type != AUDIO_MIXER_VALUE)
1336 return EINVAL;
1337 if (cp->un.value.num_channels != 1)
1338 return EINVAL;
1339
1340 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL) & SV_REC_GAIN_MASK;
1341 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1342 (((unsigned int)reg) * AUDIO_MAX_GAIN) / SV_REC_GAIN_MASK;
1343
1344 return 0;
1345
1346 case SV_MIC_BOOST:
1347 if (cp->type != AUDIO_MIXER_ENUM)
1348 return EINVAL;
1349 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1350 cp->un.ord = ((reg & SV_MIC_BOOST_BIT) ? 1 : 0);
1351 return 0;
1352
1353 case SV_SRS_MODE:
1354 if (cp->type != AUDIO_MIXER_ENUM)
1355 return EINVAL;
1356 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1357 cp->un.ord = ((reg & SV_SRS_SPACE_ONOFF) ? 0 : 1);
1358 return 0;
1359 }
1360
1361 return EINVAL;
1362 }
1363
1364 static void
1365 sv_init_mixer(struct sv_softc *sc)
1366 {
1367 mixer_ctrl_t cp;
1368 int i;
1369
1370 cp.type = AUDIO_MIXER_ENUM;
1371 cp.dev = SV_SRS_MODE;
1372 cp.un.ord = 0;
1373
1374 sv_mixer_set_port(sc, &cp);
1375
1376 for (i = 0; i < ARRAY_SIZE(ports); i++) {
1377 if (ports[i].audio == AudioNdac) {
1378 cp.type = AUDIO_MIXER_ENUM;
1379 cp.dev = SV_FIRST_MIXER + i * SV_DEVICES_PER_PORT + 1;
1380 cp.un.ord = 0;
1381 sv_mixer_set_port(sc, &cp);
1382 break;
1383 }
1384 }
1385 }
1386
1387 void *
1388 sv_malloc(void *addr, int direction, size_t size,
1389 struct malloc_type *pool, int flags)
1390 {
1391 struct sv_softc *sc;
1392 struct sv_dma *p;
1393 int error;
1394
1395 sc = addr;
1396 p = malloc(sizeof(*p), pool, flags);
1397 if (p == NULL)
1398 return NULL;
1399 error = sv_allocmem(sc, size, 16, direction, p);
1400 if (error) {
1401 free(p, pool);
1402 return 0;
1403 }
1404 p->next = sc->sc_dmas;
1405 sc->sc_dmas = p;
1406 return KERNADDR(p);
1407 }
1408
1409 void
1410 sv_free(void *addr, void *ptr, struct malloc_type *pool)
1411 {
1412 struct sv_softc *sc;
1413 struct sv_dma **pp, *p;
1414
1415 sc = addr;
1416 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1417 if (KERNADDR(p) == ptr) {
1418 sv_freemem(sc, p);
1419 *pp = p->next;
1420 free(p, pool);
1421 return;
1422 }
1423 }
1424 }
1425
1426 size_t
1427 sv_round_buffersize(void *addr, int direction, size_t size)
1428 {
1429
1430 return size;
1431 }
1432
1433 paddr_t
1434 sv_mappage(void *addr, void *mem, off_t off, int prot)
1435 {
1436 struct sv_softc *sc;
1437 struct sv_dma *p;
1438
1439 sc = addr;
1440 if (off < 0)
1441 return -1;
1442 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1443 continue;
1444 if (p == NULL)
1445 return -1;
1446 return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1447 off, prot, BUS_DMA_WAITOK);
1448 }
1449
1450 int
1451 sv_get_props(void *addr)
1452 {
1453 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1454 }
1455