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