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