xref: /openbsd-src/sys/dev/pci/sv.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*      $OpenBSD: sv.c,v 1.27 2010/07/15 03:43:11 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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
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 		break;
657 	default:
658 		return (EINVAL);
659 	}
660 	fp->bps = AUDIO_BPS(fp->precision);
661 	fp->msb = 1;
662 
663 	return (0);
664 }
665 
666 int
667 sv_set_params(addr, setmode, usemode, p, r)
668 	void *addr;
669 	int setmode, usemode;
670 	struct audio_params *p, *r;
671 {
672 	struct sv_softc *sc = addr;
673 	void (*pswcode)(void *, u_char *buf, int cnt);
674 	void (*rswcode)(void *, u_char *buf, int cnt);
675         u_int32_t mode, val;
676         u_int8_t reg;
677 
678         pswcode = rswcode = 0;
679         switch (p->encoding) {
680         case AUDIO_ENCODING_SLINEAR_BE:
681         	if (p->precision == 16)
682                 	rswcode = pswcode = swap_bytes;
683 		else
684 			pswcode = rswcode = change_sign8;
685 		break;
686         case AUDIO_ENCODING_SLINEAR_LE:
687         	if (p->precision != 16)
688 			pswcode = rswcode = change_sign8;
689         	break;
690         case AUDIO_ENCODING_ULINEAR_BE:
691         	if (p->precision == 16) {
692 			pswcode = swap_bytes_change_sign16_le;
693 			rswcode = change_sign16_swap_bytes_le;
694 		}
695 		break;
696         case AUDIO_ENCODING_ULINEAR_LE:
697         	if (p->precision == 16)
698 			pswcode = rswcode = change_sign16_le;
699         	break;
700         case AUDIO_ENCODING_ULAW:
701         	pswcode = mulaw_to_ulinear8;
702                 rswcode = ulinear8_to_mulaw;
703                 break;
704         case AUDIO_ENCODING_ALAW:
705                 pswcode = alaw_to_ulinear8;
706                 rswcode = ulinear8_to_alaw;
707                 break;
708         default:
709         	return (EINVAL);
710         }
711 
712 	if (p->precision == 16)
713 		mode = SV_DMAA_FORMAT16 | SV_DMAC_FORMAT16;
714 	else
715 		mode = 0;
716 	if (p->channels > 2)
717 		p->channels = 2;
718         if (p->channels == 2)
719         	mode |= SV_DMAA_STEREO | SV_DMAC_STEREO;
720         if (p->sample_rate < 2000)
721 		p->sample_rate = 2000;
722 	if (p->sample_rate > 48000)
723 		p->sample_rate = 48000;
724 
725         p->sw_code = pswcode;
726         r->sw_code = rswcode;
727 	p->bps = AUDIO_BPS(p->precision);
728 	r->bps = AUDIO_BPS(r->precision);
729 	p->msb = r->msb = 1;
730 
731         /* Set the encoding */
732 	reg = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
733 	reg &= ~(SV_DMAA_FORMAT16 | SV_DMAC_FORMAT16 | SV_DMAA_STEREO |
734 		 SV_DMAC_STEREO);
735 	reg |= (mode);
736 	sv_write_indirect(sc, SV_DMA_DATA_FORMAT, reg);
737 
738 	val = p->sample_rate * 65536 / 48000;
739 
740 	sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_0, (val & 0xff));
741 	sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_1, (val >> 8));
742 
743 #define F_REF 24576000
744 
745 	if (setmode & AUMODE_RECORD)
746 	{
747 	  /* The ADC reference frequency (f_out) is 512 * the sample rate */
748 
749 	  /* f_out is dervied from the 24.576MHZ crystal by three values:
750 	     M & N & R. The equation is as follows:
751 
752 	     f_out = (m + 2) * f_ref / ((n + 2) * (2 ^ a))
753 
754 	     with the constraint that:
755 
756 	     80 MhZ < (m + 2) / (n + 2) * f_ref <= 150MHz
757 	     and n, m >= 1
758 	  */
759 
760 	  int  goal_f_out = 512 * r->sample_rate;
761 	  int  a, n, m, best_n, best_m, best_error = 10000000;
762 	  int  pll_sample;
763 
764 	  for (a = 0; a < 8; a++) {
765 	    if ((goal_f_out * (1 << a)) >= 80000000)
766 	      break;
767 	  }
768 
769 	  /* a != 8 because sample_rate >= 2000 */
770 
771 	  for (n = 33; n > 2; n--) {
772 	    int error;
773 
774 	    m = (goal_f_out * n * (1 << a)) / F_REF;
775 
776 	    if ((m > 257) || (m < 3)) continue;
777 
778 	    pll_sample = (m * F_REF) / (n * (1 << a));
779 	    pll_sample /= 512;
780 
781 	    /* Threshold might be good here */
782 	    error = pll_sample - r->sample_rate;
783 	    error = abs(error);
784 
785 	    if (error < best_error) {
786 	      best_error = error;
787 	      best_n = n;
788 	      best_m = m;
789 	      if (error == 0) break;
790 	    }
791 	  }
792 
793 
794 	  best_n -= 2;
795 	  best_m -= 2;
796 
797 	  sv_write_indirect(sc, SV_ADC_PLL_M, best_m);
798 	  sv_write_indirect(sc, SV_ADC_PLL_N, best_n | (a << SV_PLL_R_SHIFT));
799 	}
800         return (0);
801 }
802 
803 int
804 sv_round_blocksize(addr, blk)
805 	void *addr;
806 	int blk;
807 {
808 	return ((blk + 31) & -32);	/* keep good alignment */
809 }
810 
811 int
812 sv_dma_init_input(addr, buf, cc)
813 	void *addr;
814 	void *buf;
815 	int cc;
816 {
817 	struct sv_softc *sc = addr;
818 	struct sv_dma *p;
819 	int dma_count;
820 
821 	DPRINTF(("sv_dma_init_input: dma start loop input addr=%p cc=%d\n",
822 		 buf, cc));
823         for (p = sc->sc_dmas; p && KERNADDR(p) != buf; p = p->next)
824 		;
825 	if (!p) {
826 		printf("sv_dma_init_input: bad addr %p\n", buf);
827 		return (EINVAL);
828 	}
829 
830 	dma_count = (cc >> 1) - 1;
831 
832 	bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0,
833 			  DMAADDR(p));
834 	bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_COUNT0,
835 			  dma_count);
836 	bus_space_write_1(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_MODE,
837 			  DMA37MD_WRITE | DMA37MD_LOOP);
838 
839 	return (0);
840 }
841 
842 int
843 sv_dma_init_output(addr, buf, cc)
844 	void *addr;
845 	void *buf;
846 	int cc;
847 {
848 	struct sv_softc *sc = addr;
849 	struct sv_dma *p;
850 	int dma_count;
851 
852 	DPRINTF(("eap: dma start loop output buf=%p cc=%d\n", buf, cc));
853         for (p = sc->sc_dmas; p && KERNADDR(p) != buf; p = p->next)
854 		;
855 	if (!p) {
856 		printf("sv_dma_init_output: bad addr %p\n", buf);
857 		return (EINVAL);
858 	}
859 
860 	dma_count = cc - 1;
861 
862 	bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0,
863 			  DMAADDR(p));
864 	bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_COUNT0,
865 			  dma_count);
866 	bus_space_write_1(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_MODE,
867 			  DMA37MD_READ | DMA37MD_LOOP);
868 
869 	return (0);
870 }
871 
872 int
873 sv_dma_output(addr, p, cc, intr, arg)
874 	void *addr;
875 	void *p;
876 	int cc;
877 	void (*intr)(void *);
878 	void *arg;
879 {
880 	struct sv_softc *sc = addr;
881 	u_int8_t mode;
882 
883 	DPRINTFN(1,
884                  ("sv_dma_output: sc=%p buf=%p cc=%d intr=%p(%p)\n",
885                   addr, p, cc, intr, arg));
886 
887 	sc->sc_pintr = intr;
888 	sc->sc_parg = arg;
889 	if (!(sc->sc_enable & SV_PLAY_ENABLE)) {
890 	        int dma_count = cc - 1;
891 
892 		sv_write_indirect(sc, SV_DMAA_COUNT1, dma_count >> 8);
893 		sv_write_indirect(sc, SV_DMAA_COUNT0, (dma_count & 0xFF));
894 
895 		mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
896 		mode |= SV_PLAY_ENABLE;
897 		sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode);
898 		sc->sc_enable |= SV_PLAY_ENABLE;
899 	}
900         return (0);
901 }
902 
903 int
904 sv_dma_input(addr, p, cc, intr, arg)
905 	void *addr;
906 	void *p;
907 	int cc;
908 	void (*intr)(void *);
909 	void *arg;
910 {
911 	struct sv_softc *sc = addr;
912 	u_int8_t mode;
913 
914 	DPRINTFN(1, ("sv_dma_input: sc=%p buf=%p cc=%d intr=%p(%p)\n",
915 		     addr, p, cc, intr, arg));
916 	sc->sc_rintr = intr;
917 	sc->sc_rarg = arg;
918 	if (!(sc->sc_enable & SV_RECORD_ENABLE)) {
919 	        int dma_count = (cc >> 1) - 1;
920 
921 		sv_write_indirect(sc, SV_DMAC_COUNT1, dma_count >> 8);
922 		sv_write_indirect(sc, SV_DMAC_COUNT0, (dma_count & 0xFF));
923 
924 		mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
925 		mode |= SV_RECORD_ENABLE;
926 		sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode);
927 		sc->sc_enable |= SV_RECORD_ENABLE;
928 	}
929         return (0);
930 }
931 
932 int
933 sv_halt_out_dma(addr)
934 	void *addr;
935 {
936 	struct sv_softc *sc = addr;
937 	u_int8_t mode;
938 
939         DPRINTF(("eap: sv_halt_out_dma\n"));
940 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
941 	mode &= ~SV_PLAY_ENABLE;
942 	sc->sc_enable &= ~SV_PLAY_ENABLE;
943 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode);
944 
945         return (0);
946 }
947 
948 int
949 sv_halt_in_dma(addr)
950 	void *addr;
951 {
952 	struct sv_softc *sc = addr;
953 	u_int8_t mode;
954 
955         DPRINTF(("eap: sv_halt_in_dma\n"));
956 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
957 	mode &= ~SV_RECORD_ENABLE;
958 	sc->sc_enable &= ~SV_RECORD_ENABLE;
959 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode);
960 
961         return (0);
962 }
963 
964 int
965 sv_getdev(addr, retp)
966 	void *addr;
967         struct audio_device *retp;
968 {
969 	*retp = sv_device;
970         return (0);
971 }
972 
973 
974 /*
975  * Mixer related code is here
976  *
977  */
978 
979 #define SV_INPUT_CLASS 0
980 #define SV_OUTPUT_CLASS 1
981 #define SV_RECORD_CLASS 2
982 
983 #define SV_LAST_CLASS 2
984 
985 static const char *mixer_classes[] = { AudioCinputs, AudioCoutputs, AudioCrecord };
986 
987 static const struct {
988   u_int8_t   l_port;
989   u_int8_t   r_port;
990   u_int8_t   mask;
991   u_int8_t   class;
992   const char *audio;
993 } ports[] = {
994   { SV_LEFT_AUX1_INPUT_CONTROL, SV_RIGHT_AUX1_INPUT_CONTROL, SV_AUX1_MASK,
995     SV_INPUT_CLASS, "aux1" },
996   { SV_LEFT_CD_INPUT_CONTROL, SV_RIGHT_CD_INPUT_CONTROL, SV_CD_MASK,
997     SV_INPUT_CLASS, AudioNcd },
998   { SV_LEFT_LINE_IN_INPUT_CONTROL, SV_RIGHT_LINE_IN_INPUT_CONTROL, SV_LINE_IN_MASK,
999     SV_INPUT_CLASS, AudioNline },
1000   { SV_MIC_INPUT_CONTROL, 0, SV_MIC_MASK, SV_INPUT_CLASS, AudioNmicrophone },
1001   { SV_LEFT_SYNTH_INPUT_CONTROL, SV_RIGHT_SYNTH_INPUT_CONTROL,
1002     SV_SYNTH_MASK, SV_INPUT_CLASS, AudioNfmsynth },
1003   { SV_LEFT_AUX2_INPUT_CONTROL, SV_RIGHT_AUX2_INPUT_CONTROL, SV_AUX2_MASK,
1004     SV_INPUT_CLASS, "aux2" },
1005   { SV_LEFT_PCM_INPUT_CONTROL, SV_RIGHT_PCM_INPUT_CONTROL, SV_PCM_MASK,
1006     SV_INPUT_CLASS, AudioNdac },
1007   { SV_LEFT_MIXER_OUTPUT_CONTROL, SV_RIGHT_MIXER_OUTPUT_CONTROL,
1008     SV_MIXER_OUT_MASK, SV_OUTPUT_CLASS, AudioNmaster }
1009 };
1010 
1011 
1012 static const struct {
1013   int idx;
1014   const char *name;
1015 } record_sources[] = {
1016   { SV_REC_CD, AudioNcd },
1017   { SV_REC_DAC, AudioNdac },
1018   { SV_REC_AUX2, "aux2" },
1019   { SV_REC_LINE, AudioNline },
1020   { SV_REC_AUX1, "aux1" },
1021   { SV_REC_MIC, AudioNmicrophone },
1022   { SV_REC_MIXER, AudioNmixerout }
1023 };
1024 
1025 
1026 #define SV_DEVICES_PER_PORT 2
1027 #define SV_FIRST_MIXER (SV_LAST_CLASS + 1)
1028 #define SV_LAST_MIXER (SV_DEVICES_PER_PORT * (ARRAY_SIZE(ports)) + SV_LAST_CLASS)
1029 #define SV_RECORD_SOURCE (SV_LAST_MIXER + 1)
1030 #define SV_MIC_BOOST (SV_LAST_MIXER + 2)
1031 #define SV_RECORD_GAIN (SV_LAST_MIXER + 3)
1032 #define SV_SRS_MODE (SV_LAST_MIXER + 4)
1033 
1034 int
1035 sv_query_devinfo(addr, dip)
1036 	void *addr;
1037 	mixer_devinfo_t *dip;
1038 {
1039 
1040   if (dip->index < 0)
1041     return (ENXIO);
1042 
1043   /* It's a class */
1044   if (dip->index <= SV_LAST_CLASS) {
1045     dip->type = AUDIO_MIXER_CLASS;
1046     dip->mixer_class = dip->index;
1047     dip->next = dip->prev = AUDIO_MIXER_LAST;
1048     strlcpy(dip->label.name, mixer_classes[dip->index],
1049 	    sizeof dip->label.name);
1050     return (0);
1051   }
1052 
1053   if (dip->index >= SV_FIRST_MIXER &&
1054       dip->index <= SV_LAST_MIXER) {
1055     int off = dip->index - SV_FIRST_MIXER;
1056     int mute = (off % SV_DEVICES_PER_PORT);
1057     int idx = off / SV_DEVICES_PER_PORT;
1058 
1059     dip->mixer_class = ports[idx].class;
1060     strlcpy(dip->label.name, ports[idx].audio, sizeof dip->label.name);
1061 
1062     if (!mute) {
1063       dip->type = AUDIO_MIXER_VALUE;
1064       dip->prev = AUDIO_MIXER_LAST;
1065       dip->next = dip->index + 1;
1066 
1067       if (ports[idx].r_port != 0)
1068 	dip->un.v.num_channels = 2;
1069       else
1070 	dip->un.v.num_channels = 1;
1071 
1072       strlcpy(dip->un.v.units.name, AudioNvolume, sizeof dip->un.v.units.name);
1073 
1074     } else {
1075       dip->type = AUDIO_MIXER_ENUM;
1076       dip->prev = dip->index - 1;
1077       dip->next = AUDIO_MIXER_LAST;
1078 
1079       strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
1080       dip->un.e.num_mem = 2;
1081       strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1082 	  sizeof dip->un.e.member[0].label.name);
1083       dip->un.e.member[0].ord = 0;
1084       strlcpy(dip->un.e.member[1].label.name, AudioNon,
1085 	  sizeof dip->un.e.member[1].label.name);
1086       dip->un.e.member[1].ord = 1;
1087 
1088     }
1089 
1090     return (0);
1091   }
1092 
1093   switch (dip->index) {
1094   case SV_RECORD_SOURCE:
1095     dip->mixer_class = SV_RECORD_CLASS;
1096     dip->prev = AUDIO_MIXER_LAST;
1097     dip->next = SV_RECORD_GAIN;
1098     strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
1099     dip->type = AUDIO_MIXER_ENUM;
1100 
1101     dip->un.e.num_mem = ARRAY_SIZE(record_sources);
1102 
1103     {
1104       int idx;
1105       for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) {
1106 	strlcpy(dip->un.e.member[idx].label.name, record_sources[idx].name,
1107 	    sizeof dip->un.e.member[idx].label.name);
1108 	dip->un.e.member[idx].ord = record_sources[idx].idx;
1109       }
1110     }
1111     return (0);
1112 
1113   case SV_RECORD_GAIN:
1114     dip->mixer_class = SV_RECORD_CLASS;
1115     dip->prev = SV_RECORD_SOURCE;
1116     dip->next = AUDIO_MIXER_LAST;
1117     strlcpy(dip->label.name, "gain", sizeof dip->label.name);
1118     dip->type = AUDIO_MIXER_VALUE;
1119     dip->un.v.num_channels = 1;
1120     strlcpy(dip->un.v.units.name, AudioNvolume, sizeof dip->un.v.units.name);
1121     return (0);
1122 
1123   case SV_MIC_BOOST:
1124     dip->mixer_class = SV_RECORD_CLASS;
1125     dip->prev = AUDIO_MIXER_LAST;
1126     dip->next = AUDIO_MIXER_LAST;
1127     strlcpy(dip->label.name, "micboost", sizeof dip->label.name);
1128     goto on_off;
1129 
1130   case SV_SRS_MODE:
1131     dip->mixer_class = SV_OUTPUT_CLASS;
1132     dip->prev = dip->next = AUDIO_MIXER_LAST;
1133     strlcpy(dip->label.name, AudioNspatial, sizeof dip->label.name);
1134 
1135 on_off:
1136     dip->type = AUDIO_MIXER_ENUM;
1137     dip->un.e.num_mem = 2;
1138     strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1139 	sizeof dip->un.e.member[0].label.name);
1140     dip->un.e.member[0].ord = 0;
1141     strlcpy(dip->un.e.member[1].label.name, AudioNon,
1142 	sizeof dip->un.e.member[1].label.name);
1143     dip->un.e.member[1].ord = 1;
1144     return (0);
1145   }
1146 
1147   return (ENXIO);
1148 }
1149 
1150 int
1151 sv_mixer_set_port(addr, cp)
1152 	void *addr;
1153 	mixer_ctrl_t *cp;
1154 {
1155   struct sv_softc *sc = addr;
1156   u_int8_t reg;
1157   int idx;
1158 
1159   if (cp->dev >= SV_FIRST_MIXER &&
1160       cp->dev <= SV_LAST_MIXER) {
1161     int off = cp->dev - SV_FIRST_MIXER;
1162     int mute = (off % SV_DEVICES_PER_PORT);
1163     idx = off / SV_DEVICES_PER_PORT;
1164 
1165     if (mute) {
1166       if (cp->type != AUDIO_MIXER_ENUM)
1167 	return (EINVAL);
1168 
1169       reg = sv_read_indirect(sc, ports[idx].l_port);
1170       if (cp->un.ord)
1171 	reg |= SV_MUTE_BIT;
1172       else
1173 	reg &= ~SV_MUTE_BIT;
1174       sv_write_indirect(sc, ports[idx].l_port, reg);
1175 
1176       if (ports[idx].r_port) {
1177 	reg = sv_read_indirect(sc, ports[idx].r_port);
1178 	if (cp->un.ord)
1179 	  reg |= SV_MUTE_BIT;
1180 	else
1181 	  reg &= ~SV_MUTE_BIT;
1182 	sv_write_indirect(sc, ports[idx].r_port, reg);
1183       }
1184     } else {
1185       int  lval, rval;
1186 
1187       if (cp->type != AUDIO_MIXER_VALUE)
1188 	return (EINVAL);
1189 
1190       if (cp->un.value.num_channels != 1 &&
1191 	  cp->un.value.num_channels != 2)
1192 	return (EINVAL);
1193 
1194       if (ports[idx].r_port == 0) {
1195 	if (cp->un.value.num_channels != 1)
1196 	  return (EINVAL);
1197 	lval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1198       } else {
1199 	if (cp->un.value.num_channels != 2)
1200 	  return (EINVAL);
1201 
1202 	lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1203 	rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1204       }
1205 
1206       sc->sc_trd = 1;
1207 
1208       reg = sv_read_indirect(sc, ports[idx].l_port);
1209       reg &= ~(ports[idx].mask);
1210       lval = ((AUDIO_MAX_GAIN - lval) * ports[idx].mask) / AUDIO_MAX_GAIN;
1211       reg |= lval;
1212       sv_write_indirect(sc, ports[idx].l_port, reg);
1213 
1214       if (ports[idx].r_port != 0) {
1215 	reg = sv_read_indirect(sc, ports[idx].r_port);
1216 	reg &= ~(ports[idx].mask);
1217 
1218 	rval = ((AUDIO_MAX_GAIN - rval) * ports[idx].mask) / AUDIO_MAX_GAIN;
1219 	reg |= rval;
1220 
1221 	sv_write_indirect(sc, ports[idx].r_port, reg);
1222       }
1223 
1224       sc->sc_trd = 0;
1225       sv_read_indirect(sc, ports[idx].l_port);
1226     }
1227 
1228     return (0);
1229   }
1230 
1231 
1232   switch (cp->dev) {
1233   case SV_RECORD_SOURCE:
1234     if (cp->type != AUDIO_MIXER_ENUM)
1235       return (EINVAL);
1236 
1237     for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) {
1238       if (record_sources[idx].idx == cp->un.ord)
1239 	goto found;
1240     }
1241 
1242     return (EINVAL);
1243 
1244   found:
1245     reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1246     reg &= ~SV_REC_SOURCE_MASK;
1247     reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1248     sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1249 
1250     reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1251     reg &= ~SV_REC_SOURCE_MASK;
1252     reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1253     sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1254     return (0);
1255 
1256   case SV_RECORD_GAIN:
1257     {
1258       int val;
1259 
1260       if (cp->type != AUDIO_MIXER_VALUE)
1261 	return (EINVAL);
1262 
1263       if (cp->un.value.num_channels != 1)
1264 	return (EINVAL);
1265 
1266       val = (cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] * SV_REC_GAIN_MASK)
1267 	/ AUDIO_MAX_GAIN;
1268 
1269       reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1270       reg &= ~SV_REC_GAIN_MASK;
1271       reg |= val;
1272       sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1273 
1274       reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1275       reg &= ~SV_REC_GAIN_MASK;
1276       reg |= val;
1277       sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1278 
1279     }
1280 
1281     return (0);
1282 
1283   case SV_MIC_BOOST:
1284     if (cp->type != AUDIO_MIXER_ENUM)
1285       return (EINVAL);
1286 
1287     reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1288     if (cp->un.ord) {
1289       reg |= SV_MIC_BOOST_BIT;
1290     } else {
1291       reg &= ~SV_MIC_BOOST_BIT;
1292     }
1293 
1294     sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1295     return (0);
1296 
1297   case SV_SRS_MODE:
1298     if (cp->type != AUDIO_MIXER_ENUM)
1299       return (EINVAL);
1300 
1301     reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1302     if (cp->un.ord) {
1303       reg &= ~SV_SRS_SPACE_ONOFF;
1304     } else {
1305       reg |= SV_SRS_SPACE_ONOFF;
1306     }
1307 
1308     sv_write_indirect(sc, SV_SRS_SPACE_CONTROL, reg);
1309     return (0);
1310   }
1311 
1312   return (EINVAL);
1313 }
1314 
1315 int
1316 sv_mixer_get_port(addr, cp)
1317 	void *addr;
1318 	mixer_ctrl_t *cp;
1319 {
1320   struct sv_softc *sc = addr;
1321   int val;
1322   u_int8_t reg;
1323 
1324   if (cp->dev >= SV_FIRST_MIXER &&
1325       cp->dev <= SV_LAST_MIXER) {
1326     int off = cp->dev - SV_FIRST_MIXER;
1327     int mute = (off % 2);
1328     int idx = off / 2;
1329 
1330     if (mute) {
1331       if (cp->type != AUDIO_MIXER_ENUM)
1332 	return (EINVAL);
1333 
1334       reg = sv_read_indirect(sc, ports[idx].l_port);
1335       cp->un.ord = ((reg & SV_MUTE_BIT) ? 1 : 0);
1336     } else {
1337       if (cp->type != AUDIO_MIXER_VALUE)
1338 	return (EINVAL);
1339 
1340       if (cp->un.value.num_channels != 1 &&
1341 	  cp->un.value.num_channels != 2)
1342 	return (EINVAL);
1343 
1344       if ((ports[idx].r_port == 0 &&
1345 	   cp->un.value.num_channels != 1) ||
1346 	  (ports[idx].r_port != 0 &&
1347 	   cp->un.value.num_channels != 2))
1348 	return (EINVAL);
1349 
1350       reg = sv_read_indirect(sc, ports[idx].l_port);
1351       reg &= ports[idx].mask;
1352 
1353       val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1354 
1355       if (ports[idx].r_port != 0) {
1356 	cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = val;
1357 
1358 	reg = sv_read_indirect(sc, ports[idx].r_port);
1359 	reg &= ports[idx].mask;
1360 
1361 	val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1362 	cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = val;
1363       } else
1364 	cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = val;
1365     }
1366 
1367     return (0);
1368   }
1369 
1370   switch (cp->dev) {
1371   case SV_RECORD_SOURCE:
1372     if (cp->type != AUDIO_MIXER_ENUM)
1373       return (EINVAL);
1374 
1375     reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1376     cp->un.ord = ((reg & SV_REC_SOURCE_MASK) >> SV_REC_SOURCE_SHIFT);
1377 
1378     return (0);
1379 
1380   case SV_RECORD_GAIN:
1381     if (cp->type != AUDIO_MIXER_VALUE)
1382       return (EINVAL);
1383 
1384     if (cp->un.value.num_channels != 1)
1385       return (EINVAL);
1386 
1387     reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL) & SV_REC_GAIN_MASK;
1388     cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1389       (((unsigned int)reg) * AUDIO_MAX_GAIN) / SV_REC_GAIN_MASK;
1390 
1391     return (0);
1392 
1393   case SV_MIC_BOOST:
1394     if (cp->type != AUDIO_MIXER_ENUM)
1395       return (EINVAL);
1396 
1397     reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1398     cp->un.ord = ((reg & SV_MIC_BOOST_BIT) ? 1 : 0);
1399 
1400     return (0);
1401 
1402 
1403   case SV_SRS_MODE:
1404     if (cp->type != AUDIO_MIXER_ENUM)
1405       return (EINVAL);
1406 
1407     reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1408 
1409     cp->un.ord = ((reg & SV_SRS_SPACE_ONOFF) ? 0 : 1);
1410     return (0);
1411   }
1412 
1413   return (EINVAL);
1414 }
1415 
1416 
1417 static void
1418 sv_init_mixer(sc)
1419      struct sv_softc *sc;
1420 {
1421   mixer_ctrl_t cp;
1422   int idx;
1423 
1424   cp.type = AUDIO_MIXER_ENUM;
1425   cp.dev = SV_SRS_MODE;
1426   cp.un.ord = 0;
1427 
1428   sv_mixer_set_port(sc, &cp);
1429 
1430   for (idx = 0; idx < ARRAY_SIZE(ports); idx++) {
1431     if (strcmp(ports[idx].audio, AudioNdac) == 0) {
1432       cp.type = AUDIO_MIXER_ENUM;
1433       cp.dev = SV_FIRST_MIXER + idx * SV_DEVICES_PER_PORT + 1;
1434       cp.un.ord = 0;
1435       sv_mixer_set_port(sc, &cp);
1436       break;
1437     }
1438   }
1439 }
1440 
1441 void *
1442 sv_malloc(addr, direction, size, pool, flags)
1443 	void *addr;
1444 	int direction;
1445 	size_t size;
1446 	int pool;
1447 	int flags;
1448 {
1449 	struct sv_softc *sc = addr;
1450         struct sv_dma *p;
1451         int error;
1452 
1453         p = malloc(sizeof(*p), pool, flags);
1454         if (!p)
1455                 return (0);
1456         error = sv_allocmem(sc, size, 16, p);
1457         if (error) {
1458                 free(p, pool);
1459         	return (0);
1460         }
1461         p->next = sc->sc_dmas;
1462         sc->sc_dmas = p;
1463 	return (KERNADDR(p));
1464 }
1465 
1466 void
1467 sv_free(addr, ptr, pool)
1468 	void *addr;
1469 	void *ptr;
1470 	int pool;
1471 {
1472 	struct sv_softc *sc = addr;
1473         struct sv_dma **p;
1474 
1475         for (p = &sc->sc_dmas; *p; p = &(*p)->next) {
1476                 if (KERNADDR(*p) == ptr) {
1477                         sv_freemem(sc, *p);
1478                         *p = (*p)->next;
1479                         free(*p, pool);
1480                         return;
1481                 }
1482         }
1483 }
1484 
1485 paddr_t
1486 sv_mappage(addr, mem, off, prot)
1487 	void *addr;
1488         void *mem;
1489         off_t off;
1490 	int prot;
1491 {
1492 	struct sv_softc *sc = addr;
1493         struct sv_dma *p;
1494 
1495         for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1496 		;
1497 	if (!p)
1498 		return (-1);
1499 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1500 				off, prot, BUS_DMA_WAITOK));
1501 }
1502 
1503 int
1504 sv_get_props(addr)
1505 	void *addr;
1506 {
1507 	return (AUDIO_PROP_MMAP | AUDIO_PROP_FULLDUPLEX);
1508 }
1509