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