auvia.c revision 1.3 1 /* $NetBSD: auvia.c,v 1.3 2000/05/15 01:27:46 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tyler C. Sarna
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * VIA Technologies VT82C686A Southbridge Audio Driver
41 *
42 * Documentation links:
43 *
44 * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf
45 * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf
46 * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec)
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/device.h>
53 #include <sys/audioio.h>
54
55 #include <dev/pci/pcidevs.h>
56 #include <dev/pci/pcivar.h>
57
58 #include <dev/audio_if.h>
59 #include <dev/mulaw.h>
60 #include <dev/auconv.h>
61
62 #include <dev/ic/ac97var.h>
63
64 #include <dev/pci/auviavar.h>
65
66 struct auvia_dma {
67 struct auvia_dma *next;
68 caddr_t addr;
69 size_t size;
70 bus_dmamap_t map;
71 bus_dma_segment_t seg;
72 };
73
74 struct auvia_dma_op {
75 u_int32_t ptr;
76 u_int32_t flags;
77 #define AUVIA_DMAOP_EOL 0x80000000
78 #define AUVIA_DMAOP_FLAG 0x40000000
79 #define AUVIA_DMAOP_STOP 0x20000000
80 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
81 };
82
83 int auvia_match(struct device *, struct cfdata *, void *);
84 void auvia_attach(struct device *, struct device *, void *);
85 int auvia_open(void *, int);
86 void auvia_close(void *);
87 int auvia_query_encoding(void *addr, struct audio_encoding *fp);
88 int auvia_set_params(void *, int, int, struct audio_params *,
89 struct audio_params *);
90 int auvia_round_blocksize(void *, int);
91 int auvia_halt_output(void *);
92 int auvia_halt_input(void *);
93 int auvia_getdev(void *, struct audio_device *);
94 int auvia_set_port(void *, mixer_ctrl_t *);
95 int auvia_get_port(void *, mixer_ctrl_t *);
96 int auvia_query_devinfo(void *, mixer_devinfo_t *);
97 void * auvia_malloc(void *, int, size_t, int, int);
98 void auvia_free(void *, void *, int);
99 size_t auvia_round_buffersize(void *, int, size_t);
100 int auvia_mappage(void *, void *, int, int);
101 int auvia_get_props(void *);
102 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
103 struct auvia_dma *, void *, void *, int);
104 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
105 void *, struct audio_params *);
106 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
107 void *, struct audio_params *);
108
109 int auvia_intr __P((void *));
110
111 struct cfattach auvia_ca = {
112 sizeof (struct auvia_softc), auvia_match, auvia_attach
113 };
114
115 #define AUVIA_PCICONF_JUNK 0x40
116 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */
117 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */
118 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */
119 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */
120 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */
121 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */
122 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */
123 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */
124
125 #define AUVIA_PLAY_STAT 0x00
126 #define AUVIA_RECORD_STAT 0x10
127 #define AUVIA_RPSTAT_INTR 0x03
128 #define AUVIA_PLAY_CONTROL 0x01
129 #define AUVIA_RECORD_CONTROL 0x11
130 #define AUVIA_RPCTRL_START 0x80
131 #define AUVIA_RPCTRL_TERMINATE 0x40
132 #define AUVIA_PLAY_MODE 0x02
133 #define AUVIA_RECORD_MODE 0x12
134 #define AUVIA_RPMODE_INTR_FLAG 0x01
135 #define AUVIA_RPMODE_INTR_EOL 0x02
136 #define AUVIA_RPMODE_STEREO 0x10
137 #define AUVIA_RPMODE_16BIT 0x20
138 #define AUVIA_RPMODE_AUTOSTART 0x80
139 #define AUVIA_PLAY_DMAOPS_BASE 0x04
140 #define AUVIA_RECORD_DMAOPS_BASE 0x14
141
142 #define AUVIA_CODEC_CTL 0x80
143 #define AUVIA_CODEC_READ 0x00800000
144 #define AUVIA_CODEC_BUSY 0x01000000
145 #define AUVIA_CODEC_PRIVALID 0x02000000
146 #define AUVIA_CODEC_INDEX(x) ((x)<<16)
147
148 #define TIMEOUT 50
149
150 #define AC97_REG_EXT_AUDIO_ID 0x28
151 #define AC97_CODEC_DOES_VRA 0x0001
152 #define AC97_REG_EXT_AUDIO_STAT 0x2A
153 #define AC97_ENAB_VRA 0x0001
154 #define AC97_ENAB_MICVRA 0x0004
155 #define AC97_REG_EXT_DAC_RATE 0x2C
156 #define AC97_REG_EXT_ADC_RATE 0x32
157
158 struct audio_hw_if auvia_hw_if = {
159 auvia_open,
160 auvia_close,
161 NULL, /* drain */
162 auvia_query_encoding,
163 auvia_set_params,
164 auvia_round_blocksize,
165 NULL, /* commit_settings */
166 NULL, /* init_output */
167 NULL, /* init_input */
168 NULL, /* start_output */
169 NULL, /* start_input */
170 auvia_halt_output,
171 auvia_halt_input,
172 NULL, /* speaker_ctl */
173 auvia_getdev,
174 NULL, /* setfd */
175 auvia_set_port,
176 auvia_get_port,
177 auvia_query_devinfo,
178 auvia_malloc,
179 auvia_free,
180 auvia_round_buffersize,
181 auvia_mappage,
182 auvia_get_props,
183 auvia_trigger_output,
184 auvia_trigger_input,
185 };
186
187 int auvia_attach_codec(void *, struct ac97_codec_if *);
188 int auvia_write_codec(void *, u_int8_t, u_int16_t);
189 int auvia_read_codec(void *, u_int8_t, u_int16_t *);
190 void auvia_reset_codec(void *);
191 int auvia_waitready_codec(struct auvia_softc *sc);
192 int auvia_waitvalid_codec(struct auvia_softc *sc);
193
194
195 int
196 auvia_match(struct device *parent, struct cfdata *match, void *aux)
197 {
198 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
199
200 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
201 return 0;
202 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT82C686A_AC97)
203 return 0;
204
205 return 1;
206 }
207
208
209 void
210 auvia_attach(struct device *parent, struct device *self, void *aux)
211 {
212 struct pci_attach_args *pa = aux;
213 struct auvia_softc *sc = (struct auvia_softc *) self;
214 const char *intrstr = NULL;
215 struct mixer_ctrl ctl;
216 pci_chipset_tag_t pc = pa->pa_pc;
217 pcitag_t pt = pa->pa_tag;
218 pci_intr_handle_t ih;
219 pcireg_t pr;
220 u_int16_t v;
221 int r, i;
222
223 r = PCI_REVISION(pa->pa_class);
224 sc->sc_revision[1] = '\0';
225 if (r == 0x20) {
226 sc->sc_revision[0] = 'H';
227 } else if ((r >= 0x10) && (r <= 0x14)) {
228 sc->sc_revision[0] = 'A' + (r - 0x10);
229 } else {
230 sprintf(sc->sc_revision, "0x%02X", r);
231 }
232
233 printf(": VIA VT82C686A AC'97 Audio (rev %s)\n",
234 sc->sc_revision);
235
236 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
237 &ih)) {
238 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
239 return;
240 }
241 intrstr = pci_intr_string(pc, ih);
242
243 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
244 if (sc->sc_ih == NULL) {
245 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
246 if (intrstr != NULL)
247 printf(" at %s", intrstr);
248 printf("\n");
249 return;
250 }
251
252 sc->sc_dmat = pa->pa_dmat;
253 sc->sc_pc = pc;
254 sc->sc_pt = pt;
255
256 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
257
258 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
259 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
260 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
261 return;
262 }
263
264 /* disable SBPro compat & others */
265 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
266
267 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
268 /* XXX what to do about MIDI, FM, joystick? */
269
270 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
271 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
272
273 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
274
275 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
276
277 sc->host_if.arg = sc;
278 sc->host_if.attach = auvia_attach_codec;
279 sc->host_if.read = auvia_read_codec;
280 sc->host_if.write = auvia_write_codec;
281 sc->host_if.reset = auvia_reset_codec;
282
283 if ((r = ac97_attach(&sc->host_if)) != 0) {
284 printf("%s: can't attach codec (error 0x%X)\n",
285 sc->sc_dev.dv_xname, r);
286 return;
287 }
288
289 if (auvia_read_codec(sc, AC97_REG_EXT_AUDIO_ID, &v)
290 || !(v & AC97_CODEC_DOES_VRA)) {
291 /* XXX */
292
293 printf("%s: codec must support AC'97 2.0 Variable Rate Audio\n",
294 sc->sc_dev.dv_xname);
295 return;
296 } else {
297 /* enable VRA */
298 auvia_write_codec(sc, AC97_REG_EXT_AUDIO_STAT,
299 AC97_ENAB_VRA | AC97_ENAB_MICVRA);
300 }
301
302 /* disable mutes */
303 for (i = 0; i < 4; i++) {
304 static struct {
305 char *class, *device;
306 } d[] = {
307 { AudioCoutputs, AudioNmaster},
308 { AudioCinputs, AudioNdac},
309 { AudioCinputs, AudioNcd},
310 { AudioCrecord, AudioNvolume},
311 };
312
313 ctl.type = AUDIO_MIXER_ENUM;
314 ctl.un.ord = 0;
315
316 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
317 d[i].class, d[i].device, AudioNmute);
318 auvia_set_port(sc, &ctl);
319 }
320
321 /* set a reasonable default volume */
322
323 ctl.type = AUDIO_MIXER_VALUE;
324 ctl.un.value.num_channels = 2;
325 ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
326 ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199;
327
328 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
329 AudioCoutputs, AudioNmaster, NULL);
330 auvia_set_port(sc, &ctl);
331
332 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
333 }
334
335
336 int
337 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
338 {
339 struct auvia_softc *sc = addr;
340
341 sc->codec_if = cif;
342
343 return 0;
344 }
345
346
347 void
348 auvia_reset_codec(void *addr)
349 {
350 #ifdef notyet /* XXX seems to make codec become unready... ??? */
351 struct auvia_softc *sc = addr;
352 pcireg_t r;
353
354 /* perform a codec cold reset */
355
356 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
357
358 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */
359 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
360 delay(2);
361
362 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */
363 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
364 delay(200);
365
366 auvia_waitready_codec(sc);
367 #endif
368 }
369
370
371 int
372 auvia_waitready_codec(struct auvia_softc *sc)
373 {
374 int i;
375
376 /* poll until codec not busy */
377 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
378 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
379 delay(1);
380 if (i >= TIMEOUT) {
381 printf("%s: codec busy\n", sc->sc_dev.dv_xname);
382 return 1;
383 }
384
385 return 0;
386 }
387
388
389 int
390 auvia_waitvalid_codec(struct auvia_softc *sc)
391 {
392 int i;
393
394 /* poll until codec valid */
395 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
396 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
397 delay(1);
398 if (i >= TIMEOUT) {
399 printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
400 return 1;
401 }
402
403 return 0;
404 }
405
406
407 int
408 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
409 {
410 struct auvia_softc *sc = addr;
411
412 if (auvia_waitready_codec(sc))
413 return 1;
414
415 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
416 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
417
418 return 0;
419 }
420
421
422 int
423 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
424 {
425 struct auvia_softc *sc = addr;
426
427 if (auvia_waitready_codec(sc))
428 return 1;
429
430 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
431 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
432
433 if (auvia_waitready_codec(sc))
434 return 1;
435
436 if (auvia_waitvalid_codec(sc))
437 return 1;
438
439 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
440
441 return 0;
442 }
443
444
445 int
446 auvia_open(void *addr, int flags)
447 {
448 return 0;
449 }
450
451
452 void
453 auvia_close(void *addr)
454 {
455 struct auvia_softc *sc = addr;
456
457 auvia_halt_output(sc);
458 auvia_halt_input(sc);
459
460 sc->sc_play.sc_intr = NULL;
461 sc->sc_record.sc_intr = NULL;
462 }
463
464
465 int
466 auvia_query_encoding(void *addr, struct audio_encoding *fp)
467 {
468 switch (fp->index) {
469 case 0:
470 strcpy(fp->name, AudioEulinear);
471 fp->encoding = AUDIO_ENCODING_ULINEAR;
472 fp->precision = 8;
473 fp->flags = 0;
474 return (0);
475 case 1:
476 strcpy(fp->name, AudioEmulaw);
477 fp->encoding = AUDIO_ENCODING_ULAW;
478 fp->precision = 8;
479 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
480 return (0);
481 case 2:
482 strcpy(fp->name, AudioEalaw);
483 fp->encoding = AUDIO_ENCODING_ALAW;
484 fp->precision = 8;
485 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
486 return (0);
487 case 3:
488 strcpy(fp->name, AudioEslinear);
489 fp->encoding = AUDIO_ENCODING_SLINEAR;
490 fp->precision = 8;
491 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
492 return (0);
493 case 4:
494 strcpy(fp->name, AudioEslinear_le);
495 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
496 fp->precision = 16;
497 fp->flags = 0;
498 return (0);
499 case 5:
500 strcpy(fp->name, AudioEulinear_le);
501 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
502 fp->precision = 16;
503 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
504 return (0);
505 case 6:
506 strcpy(fp->name, AudioEslinear_be);
507 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
508 fp->precision = 16;
509 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
510 return (0);
511 case 7:
512 strcpy(fp->name, AudioEulinear_be);
513 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
514 fp->precision = 16;
515 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
516 return (0);
517 default:
518 return (EINVAL);
519 }
520 }
521
522
523 int
524 auvia_set_params(void *addr, int setmode, int usemode,
525 struct audio_params *play, struct audio_params *rec)
526 {
527 struct auvia_softc *sc = addr;
528 struct audio_params *p;
529 u_int16_t regval;
530 int reg, mode;
531
532 /* for mode in (RECORD, PLAY) */
533 for (mode = AUMODE_RECORD; mode != -1;
534 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
535 if ((setmode & mode) == 0)
536 continue;
537
538 p = mode == AUMODE_PLAY ? play : rec;
539
540 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
541 (p->precision != 8 && p->precision != 16) ||
542 (p->channels != 1 && p->channels != 2))
543 return (EINVAL);
544
545 reg = mode == AUMODE_PLAY ?
546 AC97_REG_EXT_DAC_RATE : AC97_REG_EXT_ADC_RATE;
547
548 auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate);
549 auvia_read_codec(sc, reg, ®val);
550 p->sample_rate = regval;
551
552 p->factor = 1;
553 p->sw_code = 0;
554 switch (p->encoding) {
555 case AUDIO_ENCODING_SLINEAR_BE:
556 if (p->precision == 16)
557 p->sw_code = swap_bytes;
558 else
559 p->sw_code = change_sign8;
560 break;
561 case AUDIO_ENCODING_SLINEAR_LE:
562 if (p->precision != 16)
563 p->sw_code = change_sign8;
564 break;
565 case AUDIO_ENCODING_ULINEAR_BE:
566 if (p->precision == 16) {
567 if (mode == AUMODE_PLAY)
568 p->sw_code = swap_bytes_change_sign16_le;
569 else
570 p->sw_code = change_sign16_swap_bytes_le;
571 }
572 break;
573 case AUDIO_ENCODING_ULINEAR_LE:
574 if (p->precision == 16)
575 p->sw_code = change_sign16_le;
576 break;
577 case AUDIO_ENCODING_ULAW:
578 if (mode == AUMODE_PLAY) {
579 p->factor = 2;
580 p->sw_code = mulaw_to_slinear16_le;
581 } else
582 p->sw_code = ulinear8_to_mulaw;
583 break;
584 case AUDIO_ENCODING_ALAW:
585 if (mode == AUMODE_PLAY) {
586 p->factor = 2;
587 p->sw_code = alaw_to_slinear16_le;
588 } else
589 p->sw_code = ulinear8_to_alaw;
590 break;
591 default:
592 return (EINVAL);
593 }
594
595 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
596 | (p->precision * p->factor == 16 ?
597 AUVIA_RPMODE_16BIT : 0)
598 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
599 | AUVIA_RPMODE_AUTOSTART;
600
601 if (mode == AUMODE_PLAY) {
602 sc->sc_play.sc_reg = regval;
603 } else {
604 sc->sc_record.sc_reg = regval;
605 }
606 }
607
608 return 0;
609 }
610
611
612 int
613 auvia_round_blocksize(void *addr, int blk)
614 {
615 return (blk & -32);
616 }
617
618
619 int
620 auvia_halt_output(void *addr)
621 {
622 struct auvia_softc *sc = addr;
623
624 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
625 AUVIA_RPCTRL_TERMINATE);
626
627 return 0;
628 }
629
630
631 int
632 auvia_halt_input(void *addr)
633 {
634 struct auvia_softc *sc = addr;
635
636 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
637 AUVIA_RPCTRL_TERMINATE);
638
639 return 0;
640 }
641
642
643 int
644 auvia_getdev(void *addr, struct audio_device *retp)
645 {
646 struct auvia_softc *sc = addr;
647
648 if (retp) {
649 strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name));
650 strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
651 strncpy(retp->config, "auvia", sizeof(retp->config));
652 }
653
654 return 0;
655 }
656
657
658 int
659 auvia_set_port(void *addr, mixer_ctrl_t *cp)
660 {
661 struct auvia_softc *sc = addr;
662
663 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
664 }
665
666
667 int
668 auvia_get_port(void *addr, mixer_ctrl_t *cp)
669 {
670 struct auvia_softc *sc = addr;
671
672 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
673 }
674
675
676 int
677 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
678 {
679 struct auvia_softc *sc = addr;
680
681 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
682 }
683
684
685 void *
686 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags)
687 {
688 struct auvia_softc *sc = addr;
689 struct auvia_dma *p;
690 int error;
691 int rseg;
692
693 p = malloc(sizeof(*p), pool, flags);
694 if (!p)
695 return 0;
696
697 p->size = size;
698 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1,
699 &rseg, BUS_DMA_NOWAIT)) != 0) {
700 printf("%s: unable to allocate dma, error = %d\n",
701 sc->sc_dev.dv_xname, error);
702 goto fail_alloc;
703 }
704
705 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
706 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
707 printf("%s: unable to map dma, error = %d\n",
708 sc->sc_dev.dv_xname, error);
709 goto fail_map;
710 }
711
712 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
713 BUS_DMA_NOWAIT, &p->map)) != 0) {
714 printf("%s: unable to create dma map, error = %d\n",
715 sc->sc_dev.dv_xname, error);
716 goto fail_create;
717 }
718
719 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
720 BUS_DMA_NOWAIT)) != 0) {
721 printf("%s: unable to load dma map, error = %d\n",
722 sc->sc_dev.dv_xname, error);
723 goto fail_load;
724 }
725
726 p->next = sc->sc_dmas;
727 sc->sc_dmas = p;
728
729 return p->addr;
730
731
732 fail_load:
733 bus_dmamap_destroy(sc->sc_dmat, p->map);
734 fail_create:
735 bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
736 fail_map:
737 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
738 fail_alloc:
739 free(p, pool);
740 return 0;
741 }
742
743
744 void
745 auvia_free(void *addr, void *ptr, int pool)
746 {
747 struct auvia_softc *sc = addr;
748 struct auvia_dma **pp, *p;
749
750 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
751 if (p->addr == ptr) {
752 bus_dmamap_unload(sc->sc_dmat, p->map);
753 bus_dmamap_destroy(sc->sc_dmat, p->map);
754 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
755 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
756
757 *pp = p->next;
758 free(p, pool);
759 return;
760 }
761
762 panic("auvia_free: trying to free unallocated memory");
763 }
764
765
766 size_t
767 auvia_round_buffersize(void *addr, int direction, size_t size)
768 {
769 return size;
770 }
771
772
773 int
774 auvia_mappage(void *addr, void *mem, int off, int prot)
775 {
776 struct auvia_softc *sc = addr;
777 struct auvia_dma *p;
778
779 if (off < 0)
780 return -1;
781
782 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
783 ;
784
785 if (!p)
786 return -1;
787
788 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
789 BUS_DMA_WAITOK);
790 }
791
792
793 int
794 auvia_get_props(void *addr)
795 {
796 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT
797 | AUDIO_PROP_FULLDUPLEX;
798 }
799
800
801 int
802 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
803 struct auvia_dma *p, void *start, void *end, int blksize)
804 {
805 struct auvia_dma_op *op;
806 struct auvia_dma *dp;
807 bus_addr_t s, e;
808 size_t l;
809 int segs;
810
811 s = p->map->dm_segs[0].ds_addr;
812 l = ((char *)end - (char *)start);
813 e = s + l;
814 segs = (l + blksize - 1) / blksize;
815
816 if (segs > (ch->sc_dma_op_count)) {
817 /* if old list was too small, free it */
818 if (ch->sc_dma_ops) {
819 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
820 }
821
822 ch->sc_dma_ops = auvia_malloc(sc, 0,
823 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
824
825 if (ch->sc_dma_ops == NULL) {
826 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
827 return 1;
828 }
829
830 for (dp = sc->sc_dmas;
831 dp && dp->addr != (void *)(ch->sc_dma_ops);
832 dp = dp->next)
833 ;
834
835 if (!dp)
836 panic("%s: build_dma_ops: where'd my memory go??? "
837 "address (%p)\n", sc->sc_dev.dv_xname,
838 ch->sc_dma_ops);
839
840 ch->sc_dma_op_count = segs;
841 ch->sc_dma_ops_dma = dp;
842 }
843
844 dp = ch->sc_dma_ops_dma;
845 op = ch->sc_dma_ops;
846
847 while (l) {
848 op->ptr = s;
849 l = l - blksize;
850 if (!l) {
851 /* if last block */
852 op->flags = AUVIA_DMAOP_EOL | blksize;
853 } else {
854 op->flags = AUVIA_DMAOP_FLAG | blksize;
855 }
856 s += blksize;
857 op++;
858 }
859
860 return 0;
861 }
862
863
864 int
865 auvia_trigger_output(void *addr, void *start, void *end,
866 int blksize, void (*intr)(void *), void *arg,
867 struct audio_params *param)
868 {
869 struct auvia_softc *sc = addr;
870 struct auvia_softc_chan *ch = &(sc->sc_play);
871 struct auvia_dma *p;
872
873 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
874 ;
875
876 if (!p)
877 panic("auvia_trigger_output: request with bad start "
878 "address (%p)\n", start);
879
880 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
881 return 1;
882 }
883
884 ch->sc_intr = intr;
885 ch->sc_arg = arg;
886
887 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE,
888 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
889
890 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE,
891 ch->sc_reg);
892
893 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
894 AUVIA_RPCTRL_START);
895
896 return 0;
897 }
898
899
900 int
901 auvia_trigger_input(void *addr, void *start, void *end,
902 int blksize, void (*intr)(void *), void *arg,
903 struct audio_params *param)
904 {
905 struct auvia_softc *sc = addr;
906 struct auvia_softc_chan *ch = &(sc->sc_record);
907 struct auvia_dma *p;
908
909 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
910 ;
911
912 if (!p)
913 panic("auvia_trigger_input: request with bad start "
914 "address (%p)\n", start);
915
916 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
917 return 1;
918 }
919
920 ch->sc_intr = intr;
921 ch->sc_arg = arg;
922
923 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE,
924 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
925
926 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE,
927 ch->sc_reg);
928
929 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
930 AUVIA_RPCTRL_START);
931
932 return 0;
933 }
934
935
936 int
937 auvia_intr(void *arg)
938 {
939 struct auvia_softc *sc = arg;
940 u_int8_t r;
941
942 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT);
943 if (r & AUVIA_RPSTAT_INTR) {
944 if (sc->sc_record.sc_intr)
945 sc->sc_record.sc_intr(sc->sc_record.sc_arg);
946
947 /* clear interrupts */
948 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT,
949 AUVIA_RPSTAT_INTR);
950 }
951 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT);
952 if (r & AUVIA_RPSTAT_INTR) {
953 if (sc->sc_play.sc_intr)
954 sc->sc_play.sc_intr(sc->sc_play.sc_arg);
955
956 /* clear interrupts */
957 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT,
958 AUVIA_RPSTAT_INTR);
959 }
960
961 return 1;
962 }
963