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