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