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