vs.c revision 1.22 1 /* $NetBSD: vs.c,v 1.22 2003/07/15 01:44:52 lukem Exp $ */
2
3 /*
4 * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Tetsuya Isaki.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * VS - OKI MSM6258 ADPCM voice synthesizer device driver.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: vs.c,v 1.22 2003/07/15 01:44:52 lukem Exp $");
39
40 #include "audio.h"
41 #include "vs.h"
42 #if NAUDIO > 0 && NVS > 0
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47
48 #include <sys/audioio.h>
49 #include <dev/audio_if.h>
50
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53
54 #include <dev/ic/msm6258var.h>
55
56 #include <arch/x68k/dev/dmacvar.h>
57 #include <arch/x68k/dev/intiovar.h>
58 #include <arch/x68k/dev/opmreg.h>
59
60 #include <arch/x68k/dev/vsvar.h>
61
62 #ifdef VS_DEBUG
63 #define DPRINTF(y,x) if(vs_debug>=(y))printf x
64 static int vs_debug;
65 #ifdef AUDIO_DEBUG
66 extern int audiodebug;
67 #endif
68 #else
69 #define DPRINTF(y,x)
70 #endif
71
72 static int vs_match __P((struct device *, struct cfdata *, void *));
73 static void vs_attach __P((struct device *, struct device *, void *));
74
75 static int vs_dmaintr __P((void *));
76 static int vs_dmaerrintr __P((void *));
77
78 /* MI audio layer interface */
79 static int vs_open __P((void *, int));
80 static void vs_close __P((void *));
81 static int vs_query_encoding __P((void *, struct audio_encoding *));
82 static int vs_set_params __P((void *, int, int, struct audio_params *,
83 struct audio_params *));
84 static int vs_trigger_output __P((void *, void *, void *, int,
85 void (*)(void *), void *,
86 struct audio_params *));
87 static int vs_trigger_input __P((void *, void *, void *, int,
88 void (*)(void *), void *,
89 struct audio_params *));
90 static int vs_halt_output __P((void *));
91 static int vs_halt_input __P((void *));
92 static int vs_allocmem __P((struct vs_softc *, size_t, size_t, size_t, int,
93 struct vs_dma *));
94 static void vs_freemem __P((struct vs_dma *));
95 static int vs_getdev __P((void *, struct audio_device *));
96 static int vs_set_port __P((void *, mixer_ctrl_t *));
97 static int vs_get_port __P((void *, mixer_ctrl_t *));
98 static int vs_query_devinfo __P((void *, mixer_devinfo_t *));
99 static void *vs_allocm __P((void *, int, size_t, struct malloc_type *, int));
100 static void vs_freem __P((void *, void *, struct malloc_type *));
101 static size_t vs_round_buffersize __P((void *, int, size_t));
102 static int vs_get_props __P((void *));
103
104 /* lower functions */
105 static int vs_round_sr(u_long);
106 static void vs_set_sr(struct vs_softc *sc, int);
107 static inline void vs_set_po(struct vs_softc *sc, u_long);
108
109 extern struct cfdriver vs_cd;
110
111 CFATTACH_DECL(vs, sizeof(struct vs_softc),
112 vs_match, vs_attach, NULL, NULL);
113
114 static struct audio_hw_if vs_hw_if = {
115 vs_open,
116 vs_close,
117 NULL, /* drain */
118
119 vs_query_encoding,
120 vs_set_params,
121 NULL, /* round_blocksize */
122 NULL, /* commit_settings */
123
124 NULL, /* init_output */
125 NULL, /* init_input */
126 NULL, /* start_output */
127 NULL, /* start_input */
128
129 vs_halt_output,
130 vs_halt_input,
131 NULL, /* speaker_ctl */
132
133 vs_getdev,
134 NULL, /* setfd */
135
136 vs_set_port,
137 vs_get_port,
138 vs_query_devinfo,
139
140 vs_allocm,
141 vs_freem,
142 vs_round_buffersize,
143 NULL, /* mappage */
144
145 vs_get_props,
146
147 vs_trigger_output,
148 vs_trigger_input,
149
150 NULL,
151 };
152
153 static struct audio_device vs_device = {
154 "OKI MSM6258",
155 "",
156 "vs"
157 };
158
159 struct {
160 u_long rate;
161 u_char clk;
162 u_char den;
163 } vs_l2r[] = {
164 { VS_RATE_15K, VS_CLK_8MHZ, VS_SRATE_512 },
165 { VS_RATE_10K, VS_CLK_8MHZ, VS_SRATE_768 },
166 { VS_RATE_7K, VS_CLK_8MHZ, VS_SRATE_1024},
167 { VS_RATE_5K, VS_CLK_4MHZ, VS_SRATE_768 },
168 { VS_RATE_3K, VS_CLK_4MHZ, VS_SRATE_1024}
169 };
170
171 #define NUM_RATE (sizeof(vs_l2r)/sizeof(vs_l2r[0]))
172
173 struct {
174 char *name;
175 int encoding;
176 int precision;
177 } vs_encodings[] = {
178 {AudioEadpcm, AUDIO_ENCODING_ADPCM, 4},
179 {AudioEslinear, AUDIO_ENCODING_SLINEAR, 8},
180 {AudioEulinear, AUDIO_ENCODING_ULINEAR, 8},
181 {AudioEmulaw, AUDIO_ENCODING_ULAW, 8},
182 {AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16},
183 {AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16},
184 };
185
186 static int
187 vs_match(struct device *parent, struct cfdata *cf, void *aux)
188 {
189 struct intio_attach_args *ia = aux;
190
191 if (strcmp(ia->ia_name, "vs") || cf->cf_unit > 0)
192 return 0;
193
194 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
195 ia->ia_addr = VS_ADDR;
196 if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
197 ia->ia_dma = VS_DMA;
198 if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
199 ia->ia_dmaintr = VS_DMAINTR;
200
201 /* fixed parameters */
202 if (ia->ia_addr != VS_ADDR)
203 return 0;
204 if (ia->ia_dma != VS_DMA)
205 return 0;
206 if (ia->ia_dmaintr != VS_DMAINTR)
207 return 0;
208
209 #ifdef VS_DEBUG
210 vs_debug = 1;
211 #ifdef AUDIO_DEBUG
212 audiodebug = 2;
213 #endif
214 #endif
215
216 return 1;
217 }
218
219 static void
220 vs_attach(struct device *parent, struct device *self, void *aux)
221 {
222 struct vs_softc *sc = (struct vs_softc *)self;
223 bus_space_tag_t iot;
224 bus_space_handle_t ioh;
225 struct intio_attach_args *ia = aux;
226
227 printf("\n");
228
229 /* Re-map the I/O space */
230 iot = ia->ia_bst;
231 bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
232
233 /* Initialize sc */
234 sc->sc_iot = iot;
235 sc->sc_ioh = ioh;
236 sc->sc_hw_if = &vs_hw_if;
237 sc->sc_addr = (caddr_t) ia->ia_addr;
238 sc->sc_dmas = NULL;
239
240 /* Initialize codec */
241 sc->sc_codec = msm6258_codec_init();
242 if (sc->sc_codec == NULL) {
243 printf ("Could not init codec\n");
244 return;
245 }
246
247 /* XXX */
248 bus_space_map(iot, PPI_ADDR, PPI_MAPSIZE, BUS_SPACE_MAP_SHIFTED,
249 &sc->sc_ppi);
250
251 /* Initialize DMAC */
252 sc->sc_dmat = ia->ia_dmat;
253 sc->sc_dma_ch = dmac_alloc_channel(parent, ia->ia_dma, "vs",
254 ia->ia_dmaintr, vs_dmaintr, sc,
255 ia->ia_dmaintr+1, vs_dmaerrintr, sc);
256
257 printf("%s: MSM6258V ADPCM voice synthesizer\n", sc->sc_dev.dv_xname);
258
259 audio_attach_mi(&vs_hw_if, sc, &sc->sc_dev);
260 }
261
262 /*
263 * vs interrupt handler
264 */
265 static int
266 vs_dmaintr(void *hdl)
267 {
268 struct vs_softc *sc = hdl;
269
270 DPRINTF(2, ("vs_dmaintr\n"));
271
272 if (sc->sc_pintr) {
273 /* start next transfer */
274 sc->sc_current.dmap += sc->sc_current.blksize;
275 if (sc->sc_current.dmap + sc->sc_current.blksize
276 > sc->sc_current.bufsize)
277 sc->sc_current.dmap -= sc->sc_current.bufsize;
278 dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
279 sc->sc_current.xfer,
280 sc->sc_current.dmap,
281 sc->sc_current.blksize);
282 sc->sc_pintr(sc->sc_parg);
283 } else if (sc->sc_rintr) {
284 /* start next transfer */
285 sc->sc_current.dmap += sc->sc_current.blksize;
286 if (sc->sc_current.dmap + sc->sc_current.blksize
287 > sc->sc_current.bufsize)
288 sc->sc_current.dmap -= sc->sc_current.bufsize;
289 dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
290 sc->sc_current.xfer,
291 sc->sc_current.dmap,
292 sc->sc_current.blksize);
293 sc->sc_rintr(sc->sc_rarg);
294 } else {
295 printf("vs_dmaintr: spurious interrupt\n");
296 }
297
298 return 1;
299 }
300
301 static int
302 vs_dmaerrintr(void *hdl)
303 {
304 struct vs_softc *sc = hdl;
305
306 DPRINTF(1, ("%s: DMA transfer error.\n", sc->sc_dev.dv_xname));
307 /* XXX */
308 vs_dmaintr(sc);
309
310 return 1;
311 }
312
313
314 /*
315 * audio MD layer interfaces
316 */
317
318 static int
319 vs_open(void *hdl, int flags)
320 {
321 struct vs_softc *sc = hdl;
322
323 DPRINTF(1, ("vs_open: flags=%d\n", flags));
324
325 sc->sc_pintr = NULL;
326 sc->sc_rintr = NULL;
327
328 msm6258_codec_open(sc);
329
330 return 0;
331 }
332
333 static void
334 vs_close(void *hdl)
335 {
336 DPRINTF(1, ("vs_close\n"));
337 }
338
339 static int
340 vs_query_encoding(void *hdl, struct audio_encoding *fp)
341 {
342 DPRINTF(1, ("vs_query_encoding\n"));
343
344 if (fp->index >= sizeof(vs_encodings) / sizeof(vs_encodings[0]))
345 return EINVAL;
346
347 strcpy(fp->name, vs_encodings[fp->index].name);
348 fp->encoding = vs_encodings[fp->index].encoding;
349 fp->precision = vs_encodings[fp->index].precision;
350 if (fp->encoding == AUDIO_ENCODING_ADPCM)
351 fp->flags = 0;
352 else
353 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
354 return 0;
355 }
356
357 static int
358 vs_round_sr(u_long rate)
359 {
360 int i;
361 int diff = rate;
362 int nearest = 0;
363
364 for (i = 0; i < NUM_RATE; i++) {
365 if (rate >= vs_l2r[i].rate) {
366 if (rate - vs_l2r[i].rate < diff) {
367 diff = rate - vs_l2r[i].rate;
368 nearest = i;
369 }
370 } else {
371 if (vs_l2r[i].rate - rate < diff) {
372 diff = vs_l2r[i].rate - rate;
373 nearest = i;
374 }
375 }
376 }
377 if (diff * 100 / rate > 15)
378 return -1;
379 else
380 return nearest;
381 }
382
383 static int
384 vs_set_params(void *hdl, int setmode, int usemode,
385 struct audio_params *play, struct audio_params *rec)
386 {
387 struct vs_softc *sc = hdl;
388 struct audio_params *p;
389 int mode;
390 int rate;
391 void (*pswcode)(void *, u_char *, int);
392 void (*rswcode)(void *, u_char *, int);
393
394 DPRINTF(1, ("vs_set_params: setmode=%d, usemode=%d\n",
395 setmode, usemode));
396
397 /* set first record info, then play info */
398 for (mode = AUMODE_RECORD; mode != -1;
399 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
400 if ((setmode & mode) == 0)
401 continue;
402
403 p = (mode == AUMODE_PLAY) ? play : rec;
404
405 if (p->channels != 1)
406 return (EINVAL);
407
408 rate = p->sample_rate;
409 pswcode = NULL;
410 rswcode = NULL;
411 p->factor = 1;
412 p->factor_denom = 0;
413 p->hw_precision = 4;
414 p->hw_encoding = AUDIO_ENCODING_ADPCM;
415 DPRINTF(1, ("vs_set_params: encoding=%d, precision=%d\n",
416 p->encoding, p->precision));
417 switch (p->precision) {
418 case 4:
419 if (p->encoding == AUDIO_ENCODING_ADPCM)
420 p->factor_denom = 1;
421 break;
422 case 8:
423 switch (p->encoding) {
424 case AUDIO_ENCODING_ULAW:
425 p->factor_denom = 2;
426 pswcode = msm6258_mulaw_to_adpcm;
427 rswcode = msm6258_adpcm_to_mulaw;
428 break;
429 case AUDIO_ENCODING_SLINEAR:
430 case AUDIO_ENCODING_SLINEAR_LE:
431 case AUDIO_ENCODING_SLINEAR_BE:
432 p->factor_denom = 2;
433 pswcode = msm6258_slinear8_to_adpcm;
434 rswcode = msm6258_adpcm_to_slinear8;
435 break;
436 case AUDIO_ENCODING_ULINEAR:
437 case AUDIO_ENCODING_ULINEAR_LE:
438 case AUDIO_ENCODING_ULINEAR_BE:
439 p->factor_denom = 2;
440 pswcode = msm6258_ulinear8_to_adpcm;
441 rswcode = msm6258_adpcm_to_ulinear8;
442 break;
443 }
444 break;
445 case 16:
446 switch (p->encoding) {
447 case AUDIO_ENCODING_SLINEAR_LE:
448 p->factor_denom = 4;
449 pswcode = msm6258_slinear16_le_to_adpcm;
450 rswcode = msm6258_adpcm_to_slinear16_le;
451 break;
452 case AUDIO_ENCODING_SLINEAR_BE:
453 p->factor_denom = 4;
454 pswcode = msm6258_slinear16_be_to_adpcm;
455 rswcode = msm6258_adpcm_to_slinear16_be;
456 break;
457 }
458 break;
459 }
460 if (p->factor_denom == 0) {
461 DPRINTF(1, ("vs_set_params: mode=%d, encoding=%d\n",
462 mode, p->encoding));
463 return EINVAL;
464 }
465
466 DPRINTF(1, ("vs_set_params: rate=%d -> ", rate));
467 rate = vs_round_sr(rate);
468 DPRINTF(1, ("%d\n", rate));
469 if (rate < 0)
470 return (EINVAL);
471 if (mode == AUMODE_PLAY) {
472 p->sw_code = pswcode;
473 sc->sc_current.prate = rate;
474 } else {
475 p->sw_code = rswcode;
476 sc->sc_current.rrate = rate;
477 }
478 }
479
480 return 0;
481 }
482
483 static void
484 vs_set_sr(struct vs_softc *sc, int rate)
485 {
486 DPRINTF(1, ("setting sample rate to %d, %d\n",
487 rate, (int)vs_l2r[rate].rate));
488 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
489 (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
490 PPI_PORTC) & 0xf0)
491 | vs_l2r[rate].den);
492 adpcm_chgclk(vs_l2r[rate].clk);
493 }
494
495 static inline void
496 vs_set_po(struct vs_softc *sc, u_long po)
497 {
498 bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
499 (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
500 & 0xfc) | po);
501 }
502
503 static int
504 vs_trigger_output(void *hdl, void *start, void *end, int bsize,
505 void (*intr)(void *), void *arg,
506 struct audio_params *p)
507 {
508 struct vs_softc *sc = hdl;
509 struct vs_dma *vd;
510 struct dmac_dma_xfer *xf;
511 struct dmac_channel_stat *chan = sc->sc_dma_ch;
512
513 DPRINTF(2, ("vs_trigger_output: start=%p, bsize=%d, intr=%p, arg=%p\n",
514 start, bsize, intr, arg));
515
516 sc->sc_pintr = intr;
517 sc->sc_parg = arg;
518 sc->sc_current.blksize = bsize;
519 sc->sc_current.bufsize = (char*)end - (char*)start;
520 sc->sc_current.dmap = 0;
521
522 /* Find DMA buffer. */
523 for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
524 vd = vd->vd_next)
525 ;
526 if (vd == NULL) {
527 printf("%s: trigger_output: bad addr %p\n",
528 sc->sc_dev.dv_xname, start);
529 return (EINVAL);
530 }
531
532 vs_set_sr(sc, sc->sc_current.prate);
533 vs_set_po(sc, VS_PANOUT_LR);
534
535 xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
536 sc->sc_current.xfer = xf;
537 chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
538 DMAC_DCR_OPS_8BIT);
539 chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
540 xf->dx_ocr = DMAC_OCR_DIR_MTD;
541 xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
542 xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
543
544 dmac_load_xfer(chan->ch_softc, xf);
545 dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
546 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 2);
547
548 return 0;
549 }
550
551 static int
552 vs_trigger_input(void *hdl, void *start, void *end, int bsize,
553 void (*intr)(void *), void *arg,
554 struct audio_params *p)
555 {
556 struct vs_softc *sc = hdl;
557 struct vs_dma *vd;
558 struct dmac_dma_xfer *xf;
559 struct dmac_channel_stat *chan = sc->sc_dma_ch;
560
561 DPRINTF(2, ("vs_trigger_input: start=%p, bsize=%d, intr=%p, arg=%p\n",
562 start, bsize, intr, arg));
563
564 sc->sc_rintr = intr;
565 sc->sc_rarg = arg;
566 sc->sc_current.blksize = bsize;
567 sc->sc_current.bufsize = (char*)end - (char*)start;
568 sc->sc_current.dmap = 0;
569
570 /* Find DMA buffer. */
571 for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
572 vd = vd->vd_next)
573 ;
574 if (vd == NULL) {
575 printf("%s: trigger_output: bad addr %p\n",
576 sc->sc_dev.dv_xname, start);
577 return (EINVAL);
578 }
579
580 vs_set_sr(sc, sc->sc_current.rrate);
581 xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
582 sc->sc_current.xfer = xf;
583 chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
584 DMAC_DCR_OPS_8BIT);
585 chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
586 xf->dx_ocr = DMAC_OCR_DIR_DTM;
587 xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
588 xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
589
590 dmac_load_xfer(chan->ch_softc, xf);
591 dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
592 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 4);
593
594 return 0;
595 }
596
597 static int
598 vs_halt_output(void *hdl)
599 {
600 struct vs_softc *sc = hdl;
601
602 DPRINTF(1, ("vs_halt_output\n"));
603
604 /* stop ADPCM play */
605 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
606 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
607
608 return 0;
609 }
610
611 static int
612 vs_halt_input(void *hdl)
613 {
614 struct vs_softc *sc = hdl;
615
616 DPRINTF(1, ("vs_halt_input\n"));
617
618 /* stop ADPCM recoding */
619 dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
620 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
621
622 return 0;
623 }
624
625 static int
626 vs_allocmem(struct vs_softc *sc, size_t size, size_t align, size_t boundary,
627 int flags, struct vs_dma *vd)
628 {
629 int error, wait;
630
631 #ifdef DIAGNOSTIC
632 if (size > DMAC_MAXSEGSZ)
633 panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
634 #endif
635
636 wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
637 vd->vd_size = size;
638
639 error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
640 vd->vd_segs,
641 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
642 &vd->vd_nsegs, wait);
643 if (error)
644 goto out;
645
646 error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
647 vd->vd_size, &vd->vd_addr,
648 wait | BUS_DMA_COHERENT);
649 if (error)
650 goto free;
651
652 error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
653 0, wait, &vd->vd_map);
654 if (error)
655 goto unmap;
656
657 error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
658 vd->vd_size, NULL, wait);
659 if (error)
660 goto destroy;
661
662 return (0);
663
664 destroy:
665 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
666 unmap:
667 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
668 free:
669 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
670 out:
671 return (error);
672 }
673
674 static void
675 vs_freemem(struct vs_dma *vd)
676 {
677
678 bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
679 bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
680 bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
681 bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
682 }
683
684 static int
685 vs_getdev(void *hdl, struct audio_device *retp)
686 {
687 DPRINTF(1, ("vs_getdev\n"));
688
689 *retp = vs_device;
690 return 0;
691 }
692
693 static int
694 vs_set_port(void *hdl, mixer_ctrl_t *cp)
695 {
696 DPRINTF(1, ("vs_set_port\n"));
697 return 0;
698 }
699
700 static int
701 vs_get_port(void *hdl, mixer_ctrl_t *cp)
702 {
703 DPRINTF(1, ("vs_get_port\n"));
704 return 0;
705 }
706
707 static int
708 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
709 {
710 DPRINTF(1, ("vs_query_devinfo\n"));
711 switch (mi->index) {
712 default:
713 return EINVAL;
714 }
715 return 0;
716 }
717
718 static void *
719 vs_allocm(void *hdl, int direction, size_t size, struct malloc_type *type,
720 int flags)
721 {
722 struct vs_softc *sc = hdl;
723 struct vs_dma *vd;
724 int error;
725
726 if ((vd = malloc(size, type, flags)) == NULL)
727 return (NULL);
728
729 vd->vd_dmat = sc->sc_dmat;
730
731 error = vs_allocmem(sc, size, 32, 0, flags, vd);
732 if (error) {
733 free(vd, type);
734 return (NULL);
735 }
736 vd->vd_next = sc->sc_dmas;
737 sc->sc_dmas = vd;
738
739 return (KVADDR(vd));
740 }
741
742 static void
743 vs_freem(void *hdl, void *addr, struct malloc_type *type)
744 {
745 struct vs_softc *sc = hdl;
746 struct vs_dma *p, **pp;
747
748 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
749 if (KVADDR(p) == addr) {
750 vs_freemem(p);
751 *pp = p->vd_next;
752 free(p, type);
753 return;
754 }
755 }
756 }
757
758 static size_t
759 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
760 {
761 if (bufsize > DMAC_MAXSEGSZ)
762 bufsize = DMAC_MAXSEGSZ;
763
764 return bufsize;
765 }
766
767 #if 0
768 paddr_t
769 vs_mappage(void *addr, void *mem, off_t off, int prot)
770 {
771 struct vs_softc *sc = addr;
772 struct vs_dma *p;
773
774 if (off < 0)
775 return (-1);
776 for (p = sc->sc_dmas; p != NULL && KVADDR(p) != mem;
777 p = p->vd_next)
778 ;
779 if (p == NULL) {
780 printf("%s: mappage: bad addr %p\n",
781 sc->sc_dev.dv_xname, start);
782 return (-1);
783 }
784
785 return (bus_dmamem_mmap(sc->sc_dmat, p->vd_segs, p->vd_nsegs,
786 off, prot, BUS_DMA_WAITOK));
787 }
788 #endif
789
790 static int
791 vs_get_props(void *hdl)
792 {
793 DPRINTF(1, ("vs_get_props\n"));
794 return 0 /* | dependent | half duplex | no mmap */;
795 }
796 #endif /* NAUDIO > 0 && NVS > 0*/
797