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