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