sv.c revision 1.6 1 /* $NetBSD: sv.c,v 1.6 1999/02/19 02:27:59 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(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 int sv_mappage __P((void *, void *, int, 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(pc, pa->pa_intrtag, pa->pa_intrpin,
424 pa->pa_intrline, &ih)) {
425 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
426 return;
427 }
428 intrstr = pci_intr_string(pc, ih);
429 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, sv_intr, sc);
430 if (sc->sc_ih == NULL) {
431 printf("%s: couldn't establish interrupt",
432 sc->sc_dev.dv_xname);
433 if (intrstr != NULL)
434 printf(" at %s", intrstr);
435 printf("\n");
436 return;
437 }
438 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
439 printf("%s: rev %d", sc->sc_dev.dv_xname,
440 sv_read_indirect(sc, SV_REVISION_LEVEL));
441 if (sv_read(sc, SV_CODEC_CONTROL) & SV_CTL_MD1)
442 printf(", reverb SRAM present");
443 if (!(sv_read_indirect(sc, SV_WAVETABLE_SOURCE_SELECT) & SV_WSS_WT0))
444 printf(", wavetable ROM present");
445 printf("\n");
446
447 sv_init_mixer(sc);
448
449 audio_attach_mi(&sv_hw_if, sc, &sc->sc_dev);
450
451 arg.type = AUDIODEV_TYPE_OPL;
452 arg.hwif = 0;
453 arg.hdl = 0;
454 (void)config_found(&sc->sc_dev, &arg, audioprint);
455
456 sc->sc_pa = *pa; /* for deferred setup */
457 config_defer(self, sv_defer);
458 }
459
460 #ifdef AUDIO_DEBUG
461 void
462 sv_dumpregs(sc)
463 struct sv_softc *sc;
464 {
465 int idx;
466
467 #if 0
468 for (idx = 0; idx < 0x50; idx += 4)
469 printf ("%02x = %x\n", idx,
470 pci_conf_read(pa->pa_pc, pa->pa_tag, idx));
471 #endif
472
473 for (idx = 0; idx < 6; idx++)
474 printf ("REG %02x = %02x\n", idx, sv_read(sc, idx));
475
476 for (idx = 0; idx < 0x32; idx++)
477 printf ("IREG %02x = %02x\n", idx, sv_read_indirect(sc, idx));
478
479 for (idx = 0; idx < 0x10; idx++)
480 printf ("DMA %02x = %02x\n", idx,
481 bus_space_read_1(sc->sc_iot, sc->sc_dmaa_ioh, idx));
482 }
483 #endif
484
485 int
486 sv_intr(p)
487 void *p;
488 {
489 struct sv_softc *sc = p;
490 u_int8_t intr;
491
492 intr = sv_read(sc, SV_CODEC_STATUS);
493 DPRINTFN(5,("sv_intr: intr=0x%x\n", intr));
494
495 if (!(intr & (SV_INTSTATUS_DMAA | SV_INTSTATUS_DMAC)))
496 return (0);
497
498 if (intr & SV_INTSTATUS_DMAA) {
499 if (sc->sc_pintr)
500 sc->sc_pintr(sc->sc_parg);
501 }
502
503 if (intr & SV_INTSTATUS_DMAC) {
504 if (sc->sc_rintr)
505 sc->sc_rintr(sc->sc_rarg);
506 }
507
508 return (1);
509 }
510
511 int
512 sv_allocmem(sc, size, align, p)
513 struct sv_softc *sc;
514 size_t size;
515 size_t align;
516 struct sv_dma *p;
517 {
518 int error;
519
520 p->size = size;
521 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
522 p->segs, ARRAY_SIZE(p->segs),
523 &p->nsegs, BUS_DMA_NOWAIT);
524 if (error)
525 return (error);
526
527 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
528 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
529 if (error)
530 goto free;
531
532 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
533 0, BUS_DMA_NOWAIT, &p->map);
534 if (error)
535 goto unmap;
536
537 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
538 BUS_DMA_NOWAIT);
539 if (error)
540 goto destroy;
541 DPRINTF(("sv_allocmem: pa=%lx va=%lx pba=%lx\n",
542 (long)p->segs[0].ds_addr, (long)KERNADDR(p), (long)DMAADDR(p)));
543 return (0);
544
545 destroy:
546 bus_dmamap_destroy(sc->sc_dmatag, p->map);
547 unmap:
548 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
549 free:
550 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
551 return (error);
552 }
553
554 int
555 sv_freemem(sc, p)
556 struct sv_softc *sc;
557 struct sv_dma *p;
558 {
559 bus_dmamap_unload(sc->sc_dmatag, p->map);
560 bus_dmamap_destroy(sc->sc_dmatag, p->map);
561 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
562 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
563 return (0);
564 }
565
566 int
567 sv_open(addr, flags)
568 void *addr;
569 int flags;
570 {
571 struct sv_softc *sc = addr;
572
573 DPRINTF(("sv_open\n"));
574 if (!sc->sc_dmaset)
575 return (ENXIO);
576 sc->sc_pintr = 0;
577 sc->sc_rintr = 0;
578
579 return (0);
580 }
581
582 /*
583 * Close function is called at splaudio().
584 */
585 void
586 sv_close(addr)
587 void *addr;
588 {
589 struct sv_softc *sc = addr;
590
591 DPRINTF(("sv_close\n"));
592 sv_halt_output(sc);
593 sv_halt_input(sc);
594
595 sc->sc_pintr = 0;
596 sc->sc_rintr = 0;
597 }
598
599 int
600 sv_query_encoding(addr, fp)
601 void *addr;
602 struct audio_encoding *fp;
603 {
604 switch (fp->index) {
605 case 0:
606 strcpy(fp->name, AudioEulinear);
607 fp->encoding = AUDIO_ENCODING_ULINEAR;
608 fp->precision = 8;
609 fp->flags = 0;
610 return (0);
611 case 1:
612 strcpy(fp->name, AudioEmulaw);
613 fp->encoding = AUDIO_ENCODING_ULAW;
614 fp->precision = 8;
615 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
616 return (0);
617 case 2:
618 strcpy(fp->name, AudioEalaw);
619 fp->encoding = AUDIO_ENCODING_ALAW;
620 fp->precision = 8;
621 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
622 return (0);
623 case 3:
624 strcpy(fp->name, AudioEslinear);
625 fp->encoding = AUDIO_ENCODING_SLINEAR;
626 fp->precision = 8;
627 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
628 return (0);
629 case 4:
630 strcpy(fp->name, AudioEslinear_le);
631 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
632 fp->precision = 16;
633 fp->flags = 0;
634 return (0);
635 case 5:
636 strcpy(fp->name, AudioEulinear_le);
637 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
638 fp->precision = 16;
639 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
640 return (0);
641 case 6:
642 strcpy(fp->name, AudioEslinear_be);
643 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
644 fp->precision = 16;
645 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
646 return (0);
647 case 7:
648 strcpy(fp->name, AudioEulinear_be);
649 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
650 fp->precision = 16;
651 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
652 return (0);
653 default:
654 return (EINVAL);
655 }
656 }
657
658 int
659 sv_set_params(addr, setmode, usemode, play, rec)
660 void *addr;
661 int setmode, usemode;
662 struct audio_params *play, *rec;
663 {
664 struct sv_softc *sc = addr;
665 struct audio_params *p;
666 int mode;
667 u_int32_t val;
668
669 /*
670 * This device only has one clock, so make the sample rates match.
671 */
672 if (play->sample_rate != rec->sample_rate &&
673 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
674 if (setmode == AUMODE_PLAY) {
675 rec->sample_rate = play->sample_rate;
676 setmode |= AUMODE_RECORD;
677 } else if (setmode == AUMODE_RECORD) {
678 play->sample_rate = rec->sample_rate;
679 setmode |= AUMODE_PLAY;
680 } else
681 return (EINVAL);
682 }
683
684 for (mode = AUMODE_RECORD; mode != -1;
685 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
686 if ((setmode & mode) == 0)
687 continue;
688
689 p = mode == AUMODE_PLAY ? play : rec;
690
691 if (p->sample_rate < 2000 || p->sample_rate > 48000 ||
692 (p->precision != 8 && p->precision != 16) ||
693 (p->channels != 1 && p->channels != 2))
694 return (EINVAL);
695
696 p->factor = 1;
697 p->sw_code = 0;
698 switch (p->encoding) {
699 case AUDIO_ENCODING_SLINEAR_BE:
700 if (p->precision == 16)
701 p->sw_code = swap_bytes;
702 else
703 p->sw_code = change_sign8;
704 break;
705 case AUDIO_ENCODING_SLINEAR_LE:
706 if (p->precision != 16)
707 p->sw_code = change_sign8;
708 break;
709 case AUDIO_ENCODING_ULINEAR_BE:
710 if (p->precision == 16) {
711 if (mode == AUMODE_PLAY)
712 p->sw_code = swap_bytes_change_sign16;
713 else
714 p->sw_code = change_sign16_swap_bytes;
715 }
716 break;
717 case AUDIO_ENCODING_ULINEAR_LE:
718 if (p->precision == 16)
719 p->sw_code = change_sign16;
720 break;
721 case AUDIO_ENCODING_ULAW:
722 if (mode == AUMODE_PLAY) {
723 p->factor = 2;
724 p->sw_code = mulaw_to_slinear16;
725 } else
726 p->sw_code = ulinear8_to_mulaw;
727 break;
728 case AUDIO_ENCODING_ALAW:
729 if (mode == AUMODE_PLAY) {
730 p->factor = 2;
731 p->sw_code = alaw_to_slinear16;
732 } else
733 p->sw_code = ulinear8_to_alaw;
734 break;
735 default:
736 return (EINVAL);
737 }
738 }
739
740 val = p->sample_rate * 65536 / 48000;
741
742 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_0, val & 0xff);
743 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_1, val >> 8);
744
745 #define F_REF 24576000
746
747 #define ABS(x) (((x) < 0) ? (-x) : (x))
748
749 if (setmode & AUMODE_RECORD) {
750 /* The ADC reference frequency (f_out) is 512 * sample rate */
751
752 /* f_out is dervied from the 24.576MHZ crystal by three values:
753 M & N & R. The equation is as follows:
754
755 f_out = (m + 2) * f_ref / ((n + 2) * (2 ^ a))
756
757 with the constraint that:
758
759 80 MhZ < (m + 2) / (n + 2) * f_ref <= 150Mhz
760 and n, m >= 1
761 */
762
763 int goal_f_out = 512 * rec->sample_rate;
764 int a, n, m, best_n = 0, best_m = 0, best_error = 10000000;
765 int pll_sample;
766 int error;
767
768 for (a = 0; a < 8; a++) {
769 if ((goal_f_out * (1 << a)) >= 80000000)
770 break;
771 }
772
773 /* a != 8 because sample_rate >= 2000 */
774
775 for (n = 33; n > 2; n--) {
776 m = (goal_f_out * n * (1 << a)) / F_REF;
777 if ((m > 257) || (m < 3))
778 continue;
779
780 pll_sample = (m * F_REF) / (n * (1 << a));
781 pll_sample /= 512;
782
783 /* Threshold might be good here */
784 error = pll_sample - rec->sample_rate;
785 error = ABS(error);
786
787 if (error < best_error) {
788 best_error = error;
789 best_n = n;
790 best_m = m;
791 if (error == 0) break;
792 }
793 }
794
795 best_n -= 2;
796 best_m -= 2;
797
798 sv_write_indirect(sc, SV_ADC_PLL_M, best_m);
799 sv_write_indirect(sc, SV_ADC_PLL_N,
800 best_n | (a << SV_PLL_R_SHIFT));
801 }
802
803 return (0);
804 }
805
806 int
807 sv_round_blocksize(addr, blk)
808 void *addr;
809 int blk;
810 {
811 return (blk & -32); /* keep good alignment */
812 }
813
814 int
815 sv_trigger_output(addr, start, end, blksize, intr, arg, param)
816 void *addr;
817 void *start, *end;
818 int blksize;
819 void (*intr) __P((void *));
820 void *arg;
821 struct audio_params *param;
822 {
823 struct sv_softc *sc = addr;
824 struct sv_dma *p;
825 u_int8_t mode;
826 int dma_count;
827
828 DPRINTFN(1, ("sv_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
829 addr, start, end, blksize, intr, arg));
830 sc->sc_pintr = intr;
831 sc->sc_parg = arg;
832
833 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
834 mode &= ~(SV_DMAA_FORMAT16 | SV_DMAA_STEREO);
835 if (param->precision * param->factor == 16)
836 mode |= SV_DMAA_FORMAT16;
837 if (param->channels == 2)
838 mode |= SV_DMAA_STEREO;
839 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
840
841 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
842 ;
843 if (!p) {
844 printf("sv_trigger_output: bad addr %p\n", start);
845 return (EINVAL);
846 }
847
848 dma_count = ((char *)end - (char *)start) - 1;
849 DPRINTF(("sv_trigger_output: dma start loop input addr=%x cc=%d\n",
850 (int)DMAADDR(p), dma_count));
851
852 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0,
853 DMAADDR(p));
854 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_COUNT0,
855 dma_count);
856 bus_space_write_1(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_MODE,
857 DMA37MD_READ | DMA37MD_LOOP);
858
859 DPRINTF(("sv_trigger_output: current addr=%x\n",
860 bus_space_read_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0)));
861
862 dma_count = blksize - 1;
863
864 sv_write_indirect(sc, SV_DMAA_COUNT1, dma_count >> 8);
865 sv_write_indirect(sc, SV_DMAA_COUNT0, dma_count & 0xFF);
866
867 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
868 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_PLAY_ENABLE);
869
870 return (0);
871 }
872
873 int
874 sv_trigger_input(addr, start, end, blksize, intr, arg, param)
875 void *addr;
876 void *start, *end;
877 int blksize;
878 void (*intr) __P((void *));
879 void *arg;
880 struct audio_params *param;
881 {
882 struct sv_softc *sc = addr;
883 struct sv_dma *p;
884 u_int8_t mode;
885 int dma_count;
886
887 DPRINTFN(1, ("sv_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
888 addr, start, end, blksize, intr, arg));
889 sc->sc_rintr = intr;
890 sc->sc_rarg = arg;
891
892 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
893 mode &= ~(SV_DMAC_FORMAT16 | SV_DMAC_STEREO);
894 if (param->precision * param->factor == 16)
895 mode |= SV_DMAC_FORMAT16;
896 if (param->channels == 2)
897 mode |= SV_DMAC_STEREO;
898 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
899
900 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
901 ;
902 if (!p) {
903 printf("sv_trigger_input: bad addr %p\n", start);
904 return (EINVAL);
905 }
906
907 dma_count = (((char *)end - (char *)start) >> 1) - 1;
908 DPRINTF(("sv_trigger_input: dma start loop input addr=%x cc=%d\n",
909 (int)DMAADDR(p), dma_count));
910
911 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0,
912 DMAADDR(p));
913 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_COUNT0,
914 dma_count);
915 bus_space_write_1(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_MODE,
916 DMA37MD_WRITE | DMA37MD_LOOP);
917
918 DPRINTF(("sv_trigger_input: current addr=%x\n",
919 bus_space_read_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0)));
920
921 dma_count = (blksize >> 1) - 1;
922
923 sv_write_indirect(sc, SV_DMAC_COUNT1, dma_count >> 8);
924 sv_write_indirect(sc, SV_DMAC_COUNT0, dma_count & 0xFF);
925
926 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
927 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_RECORD_ENABLE);
928
929 return (0);
930 }
931
932 int
933 sv_halt_output(addr)
934 void *addr;
935 {
936 struct sv_softc *sc = addr;
937 u_int8_t mode;
938
939 DPRINTF(("sv: sv_halt_output\n"));
940 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
941 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_PLAY_ENABLE);
942
943 return (0);
944 }
945
946 int
947 sv_halt_input(addr)
948 void *addr;
949 {
950 struct sv_softc *sc = addr;
951 u_int8_t mode;
952
953 DPRINTF(("sv: sv_halt_input\n"));
954 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
955 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_RECORD_ENABLE);
956
957 return (0);
958 }
959
960 int
961 sv_getdev(addr, retp)
962 void *addr;
963 struct audio_device *retp;
964 {
965 *retp = sv_device;
966 return (0);
967 }
968
969
970 /*
971 * Mixer related code is here
972 *
973 */
974
975 #define SV_INPUT_CLASS 0
976 #define SV_OUTPUT_CLASS 1
977 #define SV_RECORD_CLASS 2
978
979 #define SV_LAST_CLASS 2
980
981 static const char *mixer_classes[] =
982 { AudioCinputs, AudioCoutputs, AudioCrecord };
983
984 static const struct {
985 u_int8_t l_port;
986 u_int8_t r_port;
987 u_int8_t mask;
988 u_int8_t class;
989 const char *audio;
990 } ports[] = {
991 { SV_LEFT_AUX1_INPUT_CONTROL, SV_RIGHT_AUX1_INPUT_CONTROL, SV_AUX1_MASK,
992 SV_INPUT_CLASS, "aux1" },
993 { SV_LEFT_CD_INPUT_CONTROL, SV_RIGHT_CD_INPUT_CONTROL, SV_CD_MASK,
994 SV_INPUT_CLASS, AudioNcd },
995 { SV_LEFT_LINE_IN_INPUT_CONTROL, SV_RIGHT_LINE_IN_INPUT_CONTROL, SV_LINE_IN_MASK,
996 SV_INPUT_CLASS, AudioNline },
997 { SV_MIC_INPUT_CONTROL, 0, SV_MIC_MASK, SV_INPUT_CLASS, AudioNmicrophone },
998 { SV_LEFT_SYNTH_INPUT_CONTROL, SV_RIGHT_SYNTH_INPUT_CONTROL,
999 SV_SYNTH_MASK, SV_INPUT_CLASS, AudioNfmsynth },
1000 { SV_LEFT_AUX2_INPUT_CONTROL, SV_RIGHT_AUX2_INPUT_CONTROL, SV_AUX2_MASK,
1001 SV_INPUT_CLASS, "aux2" },
1002 { SV_LEFT_PCM_INPUT_CONTROL, SV_RIGHT_PCM_INPUT_CONTROL, SV_PCM_MASK,
1003 SV_INPUT_CLASS, AudioNdac },
1004 { SV_LEFT_MIXER_OUTPUT_CONTROL, SV_RIGHT_MIXER_OUTPUT_CONTROL,
1005 SV_MIXER_OUT_MASK, SV_OUTPUT_CLASS, AudioNmaster }
1006 };
1007
1008
1009 static const struct {
1010 int idx;
1011 const char *name;
1012 } record_sources[] = {
1013 { SV_REC_CD, AudioNcd },
1014 { SV_REC_DAC, AudioNdac },
1015 { SV_REC_AUX2, "aux2" },
1016 { SV_REC_LINE, AudioNline },
1017 { SV_REC_AUX1, "aux1" },
1018 { SV_REC_MIC, AudioNmicrophone },
1019 { SV_REC_MIXER, AudioNmixerout }
1020 };
1021
1022
1023 #define SV_DEVICES_PER_PORT 2
1024 #define SV_FIRST_MIXER (SV_LAST_CLASS + 1)
1025 #define SV_LAST_MIXER (SV_DEVICES_PER_PORT * (ARRAY_SIZE(ports)) + SV_LAST_CLASS)
1026 #define SV_RECORD_SOURCE (SV_LAST_MIXER + 1)
1027 #define SV_MIC_BOOST (SV_LAST_MIXER + 2)
1028 #define SV_RECORD_GAIN (SV_LAST_MIXER + 3)
1029 #define SV_SRS_MODE (SV_LAST_MIXER + 4)
1030
1031 int
1032 sv_query_devinfo(addr, dip)
1033 void *addr;
1034 mixer_devinfo_t *dip;
1035 {
1036 int i;
1037
1038 /* It's a class */
1039 if (dip->index <= SV_LAST_CLASS) {
1040 dip->type = AUDIO_MIXER_CLASS;
1041 dip->mixer_class = dip->index;
1042 dip->next = dip->prev = AUDIO_MIXER_LAST;
1043 strcpy(dip->label.name,
1044 mixer_classes[dip->index]);
1045 return (0);
1046 }
1047
1048 if (dip->index >= SV_FIRST_MIXER &&
1049 dip->index <= SV_LAST_MIXER) {
1050 int off = dip->index - SV_FIRST_MIXER;
1051 int mute = (off % SV_DEVICES_PER_PORT);
1052 int idx = off / SV_DEVICES_PER_PORT;
1053
1054 dip->mixer_class = ports[idx].class;
1055 strcpy(dip->label.name, ports[idx].audio);
1056
1057 if (!mute) {
1058 dip->type = AUDIO_MIXER_VALUE;
1059 dip->prev = AUDIO_MIXER_LAST;
1060 dip->next = dip->index + 1;
1061
1062 if (ports[idx].r_port != 0)
1063 dip->un.v.num_channels = 2;
1064 else
1065 dip->un.v.num_channels = 1;
1066
1067 strcpy(dip->un.v.units.name, AudioNvolume);
1068 } else {
1069 dip->type = AUDIO_MIXER_ENUM;
1070 dip->prev = dip->index - 1;
1071 dip->next = AUDIO_MIXER_LAST;
1072
1073 strcpy(dip->label.name, AudioNmute);
1074 dip->un.e.num_mem = 2;
1075 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1076 dip->un.e.member[0].ord = 0;
1077 strcpy(dip->un.e.member[1].label.name, AudioNon);
1078 dip->un.e.member[1].ord = 1;
1079 }
1080
1081 return (0);
1082 }
1083
1084 switch (dip->index) {
1085 case SV_RECORD_SOURCE:
1086 dip->mixer_class = SV_RECORD_CLASS;
1087 dip->prev = AUDIO_MIXER_LAST;
1088 dip->next = SV_RECORD_GAIN;
1089 strcpy(dip->label.name, AudioNsource);
1090 dip->type = AUDIO_MIXER_ENUM;
1091
1092 dip->un.e.num_mem = ARRAY_SIZE(record_sources);
1093 for (i = 0; i < ARRAY_SIZE(record_sources); i++) {
1094 strcpy(dip->un.e.member[i].label.name,
1095 record_sources[i].name);
1096 dip->un.e.member[i].ord = record_sources[i].idx;
1097 }
1098 return (0);
1099
1100 case SV_RECORD_GAIN:
1101 dip->mixer_class = SV_RECORD_CLASS;
1102 dip->prev = SV_RECORD_SOURCE;
1103 dip->next = AUDIO_MIXER_LAST;
1104 strcpy(dip->label.name, "gain");
1105 dip->type = AUDIO_MIXER_VALUE;
1106 dip->un.v.num_channels = 1;
1107 strcpy(dip->un.v.units.name, AudioNvolume);
1108 return (0);
1109
1110 case SV_MIC_BOOST:
1111 dip->mixer_class = SV_RECORD_CLASS;
1112 dip->prev = AUDIO_MIXER_LAST;
1113 dip->next = AUDIO_MIXER_LAST;
1114 strcpy(dip->label.name, "micboost");
1115 goto on_off;
1116
1117 case SV_SRS_MODE:
1118 dip->mixer_class = SV_OUTPUT_CLASS;
1119 dip->prev = dip->next = AUDIO_MIXER_LAST;
1120 strcpy(dip->label.name, AudioNspatial);
1121
1122 on_off:
1123 dip->type = AUDIO_MIXER_ENUM;
1124 dip->un.e.num_mem = 2;
1125 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1126 dip->un.e.member[0].ord = 0;
1127 strcpy(dip->un.e.member[1].label.name, AudioNon);
1128 dip->un.e.member[1].ord = 1;
1129 return (0);
1130 }
1131
1132 return (ENXIO);
1133 }
1134
1135 int
1136 sv_mixer_set_port(addr, cp)
1137 void *addr;
1138 mixer_ctrl_t *cp;
1139 {
1140 struct sv_softc *sc = addr;
1141 u_int8_t reg;
1142 int idx;
1143
1144 if (cp->dev >= SV_FIRST_MIXER &&
1145 cp->dev <= SV_LAST_MIXER) {
1146 int off = cp->dev - SV_FIRST_MIXER;
1147 int mute = (off % SV_DEVICES_PER_PORT);
1148 idx = off / SV_DEVICES_PER_PORT;
1149
1150 if (mute) {
1151 if (cp->type != AUDIO_MIXER_ENUM)
1152 return (EINVAL);
1153
1154 reg = sv_read_indirect(sc, ports[idx].l_port);
1155 if (cp->un.ord)
1156 reg |= SV_MUTE_BIT;
1157 else
1158 reg &= ~SV_MUTE_BIT;
1159 sv_write_indirect(sc, ports[idx].l_port, reg);
1160
1161 if (ports[idx].r_port) {
1162 reg = sv_read_indirect(sc, ports[idx].r_port);
1163 if (cp->un.ord)
1164 reg |= SV_MUTE_BIT;
1165 else
1166 reg &= ~SV_MUTE_BIT;
1167 sv_write_indirect(sc, ports[idx].r_port, reg);
1168 }
1169 } else {
1170 int lval, rval;
1171
1172 if (cp->type != AUDIO_MIXER_VALUE)
1173 return (EINVAL);
1174
1175 if (cp->un.value.num_channels != 1 &&
1176 cp->un.value.num_channels != 2)
1177 return (EINVAL);
1178
1179 if (ports[idx].r_port == 0) {
1180 if (cp->un.value.num_channels != 1)
1181 return (EINVAL);
1182 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1183 rval = 0; /* shut up GCC */
1184 } else {
1185 if (cp->un.value.num_channels != 2)
1186 return (EINVAL);
1187
1188 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1189 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1190 }
1191
1192
1193 reg = sv_read_indirect(sc, ports[idx].l_port);
1194 reg &= ~(ports[idx].mask);
1195 lval = (AUDIO_MAX_GAIN - lval) * ports[idx].mask /
1196 AUDIO_MAX_GAIN;
1197 reg |= lval;
1198 sv_write_indirect(sc, ports[idx].l_port, reg);
1199
1200 if (ports[idx].r_port != 0) {
1201 reg = sv_read_indirect(sc, ports[idx].r_port);
1202 reg &= ~(ports[idx].mask);
1203
1204 rval = (AUDIO_MAX_GAIN - rval) * ports[idx].mask /
1205 AUDIO_MAX_GAIN;
1206 reg |= rval;
1207
1208 sv_write_indirect(sc, ports[idx].r_port, reg);
1209 }
1210
1211 sv_read_indirect(sc, ports[idx].l_port);
1212 }
1213
1214 return (0);
1215 }
1216
1217
1218 switch (cp->dev) {
1219 case SV_RECORD_SOURCE:
1220 if (cp->type != AUDIO_MIXER_ENUM)
1221 return (EINVAL);
1222
1223 for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) {
1224 if (record_sources[idx].idx == cp->un.ord)
1225 goto found;
1226 }
1227
1228 return (EINVAL);
1229
1230 found:
1231 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1232 reg &= ~SV_REC_SOURCE_MASK;
1233 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1234 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1235
1236 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1237 reg &= ~SV_REC_SOURCE_MASK;
1238 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1239 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1240 return (0);
1241
1242 case SV_RECORD_GAIN:
1243 {
1244 int val;
1245
1246 if (cp->type != AUDIO_MIXER_VALUE)
1247 return (EINVAL);
1248
1249 if (cp->un.value.num_channels != 1)
1250 return (EINVAL);
1251
1252 val = (cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] * SV_REC_GAIN_MASK)
1253 / AUDIO_MAX_GAIN;
1254
1255 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1256 reg &= ~SV_REC_GAIN_MASK;
1257 reg |= val;
1258 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1259
1260 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1261 reg &= ~SV_REC_GAIN_MASK;
1262 reg |= val;
1263 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1264 }
1265 return (0);
1266
1267 case SV_MIC_BOOST:
1268 if (cp->type != AUDIO_MIXER_ENUM)
1269 return (EINVAL);
1270
1271 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1272 if (cp->un.ord) {
1273 reg |= SV_MIC_BOOST_BIT;
1274 } else {
1275 reg &= ~SV_MIC_BOOST_BIT;
1276 }
1277
1278 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1279 return (0);
1280
1281 case SV_SRS_MODE:
1282 if (cp->type != AUDIO_MIXER_ENUM)
1283 return (EINVAL);
1284
1285 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1286 if (cp->un.ord) {
1287 reg &= ~SV_SRS_SPACE_ONOFF;
1288 } else {
1289 reg |= SV_SRS_SPACE_ONOFF;
1290 }
1291
1292 sv_write_indirect(sc, SV_SRS_SPACE_CONTROL, reg);
1293 return (0);
1294 }
1295
1296 return (EINVAL);
1297 }
1298
1299 int
1300 sv_mixer_get_port(addr, cp)
1301 void *addr;
1302 mixer_ctrl_t *cp;
1303 {
1304 struct sv_softc *sc = addr;
1305 int val;
1306 u_int8_t reg;
1307
1308 if (cp->dev >= SV_FIRST_MIXER &&
1309 cp->dev <= SV_LAST_MIXER) {
1310 int off = cp->dev - SV_FIRST_MIXER;
1311 int mute = (off % 2);
1312 int idx = off / 2;
1313
1314 if (mute) {
1315 if (cp->type != AUDIO_MIXER_ENUM)
1316 return (EINVAL);
1317
1318 reg = sv_read_indirect(sc, ports[idx].l_port);
1319 cp->un.ord = ((reg & SV_MUTE_BIT) ? 1 : 0);
1320 } else {
1321 if (cp->type != AUDIO_MIXER_VALUE)
1322 return (EINVAL);
1323
1324 if (cp->un.value.num_channels != 1 &&
1325 cp->un.value.num_channels != 2)
1326 return (EINVAL);
1327
1328 if ((ports[idx].r_port == 0 &&
1329 cp->un.value.num_channels != 1) ||
1330 (ports[idx].r_port != 0 &&
1331 cp->un.value.num_channels != 2))
1332 return (EINVAL);
1333
1334 reg = sv_read_indirect(sc, ports[idx].l_port);
1335 reg &= ports[idx].mask;
1336
1337 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1338
1339 if (ports[idx].r_port != 0) {
1340 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = val;
1341
1342 reg = sv_read_indirect(sc, ports[idx].r_port);
1343 reg &= ports[idx].mask;
1344
1345 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1346 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = val;
1347 } else
1348 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = val;
1349 }
1350
1351 return (0);
1352 }
1353
1354 switch (cp->dev) {
1355 case SV_RECORD_SOURCE:
1356 if (cp->type != AUDIO_MIXER_ENUM)
1357 return (EINVAL);
1358
1359 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1360 cp->un.ord = ((reg & SV_REC_SOURCE_MASK) >> SV_REC_SOURCE_SHIFT);
1361
1362 return (0);
1363
1364 case SV_RECORD_GAIN:
1365 if (cp->type != AUDIO_MIXER_VALUE)
1366 return (EINVAL);
1367 if (cp->un.value.num_channels != 1)
1368 return (EINVAL);
1369
1370 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL) & SV_REC_GAIN_MASK;
1371 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1372 (((unsigned int)reg) * AUDIO_MAX_GAIN) / SV_REC_GAIN_MASK;
1373
1374 return (0);
1375
1376 case SV_MIC_BOOST:
1377 if (cp->type != AUDIO_MIXER_ENUM)
1378 return (EINVAL);
1379 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1380 cp->un.ord = ((reg & SV_MIC_BOOST_BIT) ? 1 : 0);
1381 return (0);
1382
1383
1384 case SV_SRS_MODE:
1385 if (cp->type != AUDIO_MIXER_ENUM)
1386 return (EINVAL);
1387 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1388 cp->un.ord = ((reg & SV_SRS_SPACE_ONOFF) ? 0 : 1);
1389 return (0);
1390 }
1391
1392 return (EINVAL);
1393 }
1394
1395
1396 static void
1397 sv_init_mixer(sc)
1398 struct sv_softc *sc;
1399 {
1400 mixer_ctrl_t cp;
1401 int i;
1402
1403 cp.type = AUDIO_MIXER_ENUM;
1404 cp.dev = SV_SRS_MODE;
1405 cp.un.ord = 0;
1406
1407 sv_mixer_set_port(sc, &cp);
1408
1409 for (i = 0; i < ARRAY_SIZE(ports); i++) {
1410 if (ports[i].audio == AudioNdac) {
1411 cp.type = AUDIO_MIXER_ENUM;
1412 cp.dev = SV_FIRST_MIXER + i * SV_DEVICES_PER_PORT + 1;
1413 cp.un.ord = 0;
1414 sv_mixer_set_port(sc, &cp);
1415 break;
1416 }
1417 }
1418 }
1419
1420 void *
1421 sv_malloc(addr, direction, size, pool, flags)
1422 void *addr;
1423 int direction;
1424 size_t size;
1425 int pool, flags;
1426 {
1427 struct sv_softc *sc = addr;
1428 struct sv_dma *p;
1429 int error;
1430
1431 p = malloc(sizeof(*p), pool, flags);
1432 if (!p)
1433 return (0);
1434 error = sv_allocmem(sc, size, 16, p);
1435 if (error) {
1436 free(p, pool);
1437 return (0);
1438 }
1439 p->next = sc->sc_dmas;
1440 sc->sc_dmas = p;
1441 return (KERNADDR(p));
1442 }
1443
1444 void
1445 sv_free(addr, ptr, pool)
1446 void *addr;
1447 void *ptr;
1448 int pool;
1449 {
1450 struct sv_softc *sc = addr;
1451 struct sv_dma **p;
1452
1453 for (p = &sc->sc_dmas; *p; p = &(*p)->next) {
1454 if (KERNADDR(*p) == ptr) {
1455 sv_freemem(sc, *p);
1456 *p = (*p)->next;
1457 free(*p, pool);
1458 return;
1459 }
1460 }
1461 }
1462
1463 size_t
1464 sv_round_buffersize(addr, direction, size)
1465 void *addr;
1466 int direction;
1467 size_t size;
1468 {
1469 return (size);
1470 }
1471
1472 int
1473 sv_mappage(addr, mem, off, prot)
1474 void *addr;
1475 void *mem;
1476 int off;
1477 int prot;
1478 {
1479 struct sv_softc *sc = addr;
1480 struct sv_dma *p;
1481
1482 if (off < 0)
1483 return (-1);
1484 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1485 ;
1486 if (!p)
1487 return (-1);
1488 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1489 off, prot, BUS_DMA_WAITOK));
1490 }
1491
1492 int
1493 sv_get_props(addr)
1494 void *addr;
1495 {
1496 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
1497 }
1498