auvia.c revision 1.7 1 /* $NetBSD: auvia.c,v 1.7 2000/11/15 21:06:33 jdolecek 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/ac97var.h>
65
66 #include <dev/pci/auviavar.h>
67
68 struct auvia_dma {
69 struct auvia_dma *next;
70 caddr_t addr;
71 size_t size;
72 bus_dmamap_t map;
73 bus_dma_segment_t seg;
74 };
75
76 struct auvia_dma_op {
77 u_int32_t ptr;
78 u_int32_t flags;
79 #define AUVIA_DMAOP_EOL 0x80000000
80 #define AUVIA_DMAOP_FLAG 0x40000000
81 #define AUVIA_DMAOP_STOP 0x20000000
82 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
83 };
84
85 int auvia_match(struct device *, struct cfdata *, void *);
86 void auvia_attach(struct device *, struct device *, void *);
87 int auvia_open(void *, int);
88 void auvia_close(void *);
89 int auvia_query_encoding(void *addr, struct audio_encoding *fp);
90 int auvia_set_params(void *, int, int, struct audio_params *,
91 struct audio_params *);
92 int auvia_round_blocksize(void *, int);
93 int auvia_halt_output(void *);
94 int auvia_halt_input(void *);
95 int auvia_getdev(void *, struct audio_device *);
96 int auvia_set_port(void *, mixer_ctrl_t *);
97 int auvia_get_port(void *, mixer_ctrl_t *);
98 int auvia_query_devinfo(void *, mixer_devinfo_t *);
99 void * auvia_malloc(void *, int, size_t, int, int);
100 void auvia_free(void *, void *, int);
101 size_t auvia_round_buffersize(void *, int, size_t);
102 paddr_t auvia_mappage(void *, void *, off_t, int);
103 int auvia_get_props(void *);
104 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
105 struct auvia_dma *, void *, void *, int);
106 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
107 void *, struct audio_params *);
108 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
109 void *, struct audio_params *);
110
111 int auvia_intr __P((void *));
112
113 struct cfattach auvia_ca = {
114 sizeof (struct auvia_softc), auvia_match, auvia_attach
115 };
116
117 #define AUVIA_PCICONF_JUNK 0x40
118 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */
119 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */
120 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */
121 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */
122 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */
123 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */
124 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */
125 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */
126
127 #define AUVIA_PLAY_STAT 0x00
128 #define AUVIA_RECORD_STAT 0x10
129 #define AUVIA_RPSTAT_INTR 0x03
130 #define AUVIA_PLAY_CONTROL 0x01
131 #define AUVIA_RECORD_CONTROL 0x11
132 #define AUVIA_RPCTRL_START 0x80
133 #define AUVIA_RPCTRL_TERMINATE 0x40
134 #define AUVIA_PLAY_MODE 0x02
135 #define AUVIA_RECORD_MODE 0x12
136 #define AUVIA_RPMODE_INTR_FLAG 0x01
137 #define AUVIA_RPMODE_INTR_EOL 0x02
138 #define AUVIA_RPMODE_STEREO 0x10
139 #define AUVIA_RPMODE_16BIT 0x20
140 #define AUVIA_RPMODE_AUTOSTART 0x80
141 #define AUVIA_PLAY_DMAOPS_BASE 0x04
142 #define AUVIA_RECORD_DMAOPS_BASE 0x14
143
144 #define AUVIA_CODEC_CTL 0x80
145 #define AUVIA_CODEC_READ 0x00800000
146 #define AUVIA_CODEC_BUSY 0x01000000
147 #define AUVIA_CODEC_PRIVALID 0x02000000
148 #define AUVIA_CODEC_INDEX(x) ((x)<<16)
149
150 #define TIMEOUT 50
151
152 #define AC97_REG_EXT_AUDIO_ID 0x28
153 #define AC97_CODEC_DOES_VRA 0x0001
154 #define AC97_REG_EXT_AUDIO_STAT 0x2A
155 #define AC97_ENAB_VRA 0x0001
156 #define AC97_ENAB_MICVRA 0x0004
157 #define AC97_REG_EXT_DAC_RATE 0x2C
158 #define AC97_REG_EXT_ADC_RATE 0x32
159
160 struct audio_hw_if auvia_hw_if = {
161 auvia_open,
162 auvia_close,
163 NULL, /* drain */
164 auvia_query_encoding,
165 auvia_set_params,
166 auvia_round_blocksize,
167 NULL, /* commit_settings */
168 NULL, /* init_output */
169 NULL, /* init_input */
170 NULL, /* start_output */
171 NULL, /* start_input */
172 auvia_halt_output,
173 auvia_halt_input,
174 NULL, /* speaker_ctl */
175 auvia_getdev,
176 NULL, /* setfd */
177 auvia_set_port,
178 auvia_get_port,
179 auvia_query_devinfo,
180 auvia_malloc,
181 auvia_free,
182 auvia_round_buffersize,
183 auvia_mappage,
184 auvia_get_props,
185 auvia_trigger_output,
186 auvia_trigger_input,
187 };
188
189 int auvia_attach_codec(void *, struct ac97_codec_if *);
190 int auvia_write_codec(void *, u_int8_t, u_int16_t);
191 int auvia_read_codec(void *, u_int8_t, u_int16_t *);
192 void auvia_reset_codec(void *);
193 int auvia_waitready_codec(struct auvia_softc *sc);
194 int auvia_waitvalid_codec(struct auvia_softc *sc);
195
196
197 int
198 auvia_match(struct device *parent, struct cfdata *match, void *aux)
199 {
200 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
201
202 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
203 return 0;
204 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT82C686A_AC97)
205 return 0;
206
207 return 1;
208 }
209
210
211 void
212 auvia_attach(struct device *parent, struct device *self, void *aux)
213 {
214 struct pci_attach_args *pa = aux;
215 struct auvia_softc *sc = (struct auvia_softc *) self;
216 const char *intrstr = NULL;
217 struct mixer_ctrl ctl;
218 pci_chipset_tag_t pc = pa->pa_pc;
219 pcitag_t pt = pa->pa_tag;
220 pci_intr_handle_t ih;
221 pcireg_t pr;
222 u_int16_t v;
223 int r, i;
224
225 r = PCI_REVISION(pa->pa_class);
226 sc->sc_revision[1] = '\0';
227 if (r == 0x20) {
228 sc->sc_revision[0] = 'H';
229 } else if ((r >= 0x10) && (r <= 0x14)) {
230 sc->sc_revision[0] = 'A' + (r - 0x10);
231 } else {
232 sprintf(sc->sc_revision, "0x%02X", r);
233 }
234
235 printf(": VIA VT82C686A AC'97 Audio (rev %s)\n",
236 sc->sc_revision);
237
238 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
239 &ih)) {
240 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
241 return;
242 }
243 intrstr = pci_intr_string(pc, ih);
244
245 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
246 if (sc->sc_ih == NULL) {
247 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
248 if (intrstr != NULL)
249 printf(" at %s", intrstr);
250 printf("\n");
251 return;
252 }
253
254 sc->sc_dmat = pa->pa_dmat;
255 sc->sc_pc = pc;
256 sc->sc_pt = pt;
257
258 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
259
260 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
261 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
262 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
263 return;
264 }
265
266 /* disable SBPro compat & others */
267 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
268
269 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
270 /* XXX what to do about MIDI, FM, joystick? */
271
272 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
273 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
274
275 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
276
277 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
278
279 sc->host_if.arg = sc;
280 sc->host_if.attach = auvia_attach_codec;
281 sc->host_if.read = auvia_read_codec;
282 sc->host_if.write = auvia_write_codec;
283 sc->host_if.reset = auvia_reset_codec;
284
285 if ((r = ac97_attach(&sc->host_if)) != 0) {
286 printf("%s: can't attach codec (error 0x%X)\n",
287 sc->sc_dev.dv_xname, r);
288 return;
289 }
290
291 /*
292 * Print a warning if the codec doesn't support hardware variable
293 * rate audio.
294 */
295 if (auvia_read_codec(sc, AC97_REG_EXT_AUDIO_ID, &v)
296 || !(v & AC97_CODEC_DOES_VRA)) {
297 /* XXX */
298
299 printf("%s: warning: codec doesn't support hardware AC'97 2.0 Variable Rate Audio\n",
300 sc->sc_dev.dv_xname);
301 } else {
302 /* enable VRA */
303 auvia_write_codec(sc, AC97_REG_EXT_AUDIO_STAT,
304 AC97_ENAB_VRA | AC97_ENAB_MICVRA);
305 }
306
307 /* disable mutes */
308 for (i = 0; i < 4; i++) {
309 static struct {
310 char *class, *device;
311 } d[] = {
312 { AudioCoutputs, AudioNmaster},
313 { AudioCinputs, AudioNdac},
314 { AudioCinputs, AudioNcd},
315 { AudioCrecord, AudioNvolume},
316 };
317
318 ctl.type = AUDIO_MIXER_ENUM;
319 ctl.un.ord = 0;
320
321 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
322 d[i].class, d[i].device, AudioNmute);
323 auvia_set_port(sc, &ctl);
324 }
325
326 /* set a reasonable default volume */
327
328 ctl.type = AUDIO_MIXER_VALUE;
329 ctl.un.value.num_channels = 2;
330 ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
331 ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199;
332
333 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
334 AudioCoutputs, AudioNmaster, NULL);
335 auvia_set_port(sc, &ctl);
336
337 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
338 }
339
340
341 int
342 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
343 {
344 struct auvia_softc *sc = addr;
345
346 sc->codec_if = cif;
347
348 return 0;
349 }
350
351
352 void
353 auvia_reset_codec(void *addr)
354 {
355 #ifdef notyet /* XXX seems to make codec become unready... ??? */
356 struct auvia_softc *sc = addr;
357 pcireg_t r;
358
359 /* perform a codec cold reset */
360
361 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
362
363 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */
364 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
365 delay(2);
366
367 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */
368 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
369 delay(200);
370
371 auvia_waitready_codec(sc);
372 #endif
373 }
374
375
376 int
377 auvia_waitready_codec(struct auvia_softc *sc)
378 {
379 int i;
380
381 /* poll until codec not busy */
382 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
383 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
384 delay(1);
385 if (i >= TIMEOUT) {
386 printf("%s: codec busy\n", sc->sc_dev.dv_xname);
387 return 1;
388 }
389
390 return 0;
391 }
392
393
394 int
395 auvia_waitvalid_codec(struct auvia_softc *sc)
396 {
397 int i;
398
399 /* poll until codec valid */
400 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
401 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
402 delay(1);
403 if (i >= TIMEOUT) {
404 printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
405 return 1;
406 }
407
408 return 0;
409 }
410
411
412 int
413 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
414 {
415 struct auvia_softc *sc = addr;
416
417 if (auvia_waitready_codec(sc))
418 return 1;
419
420 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
421 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
422
423 return 0;
424 }
425
426
427 int
428 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
429 {
430 struct auvia_softc *sc = addr;
431
432 if (auvia_waitready_codec(sc))
433 return 1;
434
435 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
436 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
437
438 if (auvia_waitready_codec(sc))
439 return 1;
440
441 if (auvia_waitvalid_codec(sc))
442 return 1;
443
444 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
445
446 return 0;
447 }
448
449
450 int
451 auvia_open(void *addr, int flags)
452 {
453 return 0;
454 }
455
456
457 void
458 auvia_close(void *addr)
459 {
460 struct auvia_softc *sc = addr;
461
462 auvia_halt_output(sc);
463 auvia_halt_input(sc);
464
465 sc->sc_play.sc_intr = NULL;
466 sc->sc_record.sc_intr = NULL;
467 }
468
469
470 int
471 auvia_query_encoding(void *addr, struct audio_encoding *fp)
472 {
473 switch (fp->index) {
474 case 0:
475 strcpy(fp->name, AudioEulinear);
476 fp->encoding = AUDIO_ENCODING_ULINEAR;
477 fp->precision = 8;
478 fp->flags = 0;
479 return (0);
480 case 1:
481 strcpy(fp->name, AudioEmulaw);
482 fp->encoding = AUDIO_ENCODING_ULAW;
483 fp->precision = 8;
484 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
485 return (0);
486 case 2:
487 strcpy(fp->name, AudioEalaw);
488 fp->encoding = AUDIO_ENCODING_ALAW;
489 fp->precision = 8;
490 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
491 return (0);
492 case 3:
493 strcpy(fp->name, AudioEslinear);
494 fp->encoding = AUDIO_ENCODING_SLINEAR;
495 fp->precision = 8;
496 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
497 return (0);
498 case 4:
499 strcpy(fp->name, AudioEslinear_le);
500 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
501 fp->precision = 16;
502 fp->flags = 0;
503 return (0);
504 case 5:
505 strcpy(fp->name, AudioEulinear_le);
506 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
507 fp->precision = 16;
508 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
509 return (0);
510 case 6:
511 strcpy(fp->name, AudioEslinear_be);
512 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
513 fp->precision = 16;
514 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
515 return (0);
516 case 7:
517 strcpy(fp->name, AudioEulinear_be);
518 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
519 fp->precision = 16;
520 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
521 return (0);
522 default:
523 return (EINVAL);
524 }
525 }
526
527
528 int
529 auvia_set_params(void *addr, int setmode, int usemode,
530 struct audio_params *play, struct audio_params *rec)
531 {
532 struct auvia_softc *sc = addr;
533 struct audio_params *p;
534 u_int16_t regval;
535 int reg, mode;
536
537 /* for mode in (RECORD, PLAY) */
538 for (mode = AUMODE_RECORD; mode != -1;
539 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
540 if ((setmode & mode) == 0)
541 continue;
542
543 p = mode == AUMODE_PLAY ? play : rec;
544
545 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
546 (p->precision != 8 && p->precision != 16) ||
547 (p->channels != 1 && p->channels != 2))
548 return (EINVAL);
549
550 reg = mode == AUMODE_PLAY ?
551 AC97_REG_EXT_DAC_RATE : AC97_REG_EXT_ADC_RATE;
552
553 auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate);
554 auvia_read_codec(sc, reg, ®val);
555 p->sample_rate = regval;
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