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