auvia.c revision 1.14 1 /* $NetBSD: auvia.c,v 1.14 2001/11/13 07:48:41 lukem 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/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.14 2001/11/13 07:48:41 lukem Exp $");
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/audioio.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pcivar.h>
62
63 #include <dev/audio_if.h>
64 #include <dev/mulaw.h>
65 #include <dev/auconv.h>
66
67 #include <dev/ic/ac97reg.h>
68 #include <dev/ic/ac97var.h>
69
70 #include <dev/pci/auviavar.h>
71
72 struct auvia_dma {
73 struct auvia_dma *next;
74 caddr_t addr;
75 size_t size;
76 bus_dmamap_t map;
77 bus_dma_segment_t seg;
78 };
79
80 struct auvia_dma_op {
81 u_int32_t ptr;
82 u_int32_t flags;
83 #define AUVIA_DMAOP_EOL 0x80000000
84 #define AUVIA_DMAOP_FLAG 0x40000000
85 #define AUVIA_DMAOP_STOP 0x20000000
86 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
87 };
88
89 /* rev. H and later seem to support only fixed rate 48 kHz */
90 #define AUVIA_FIXED_RATE 48000
91
92 int auvia_match(struct device *, struct cfdata *, void *);
93 void auvia_attach(struct device *, struct device *, void *);
94 int auvia_open(void *, int);
95 void auvia_close(void *);
96 int auvia_query_encoding(void *addr, struct audio_encoding *fp);
97 int auvia_set_params(void *, int, int, struct audio_params *,
98 struct audio_params *);
99 int auvia_round_blocksize(void *, int);
100 int auvia_halt_output(void *);
101 int auvia_halt_input(void *);
102 int auvia_getdev(void *, struct audio_device *);
103 int auvia_set_port(void *, mixer_ctrl_t *);
104 int auvia_get_port(void *, mixer_ctrl_t *);
105 int auvia_query_devinfo(void *, mixer_devinfo_t *);
106 void * auvia_malloc(void *, int, size_t, int, int);
107 void auvia_free(void *, void *, int);
108 size_t auvia_round_buffersize(void *, int, size_t);
109 paddr_t auvia_mappage(void *, void *, off_t, int);
110 int auvia_get_props(void *);
111 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
112 struct auvia_dma *, void *, void *, int);
113 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
114 void *, struct audio_params *);
115 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
116 void *, struct audio_params *);
117
118 int auvia_intr __P((void *));
119
120 struct cfattach auvia_ca = {
121 sizeof (struct auvia_softc), auvia_match, auvia_attach
122 };
123
124 #define AUVIA_PCICONF_JUNK 0x40
125 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */
126 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */
127 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */
128 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */
129 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */
130 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */
131 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */
132 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */
133
134 #define AUVIA_PLAY_STAT 0x00
135 #define AUVIA_RECORD_STAT 0x10
136 #define AUVIA_RPSTAT_INTR 0x03
137 #define AUVIA_PLAY_CONTROL 0x01
138 #define AUVIA_RECORD_CONTROL 0x11
139 #define AUVIA_RPCTRL_START 0x80
140 #define AUVIA_RPCTRL_TERMINATE 0x40
141 #define AUVIA_PLAY_MODE 0x02
142 #define AUVIA_RECORD_MODE 0x12
143 #define AUVIA_RPMODE_INTR_FLAG 0x01
144 #define AUVIA_RPMODE_INTR_EOL 0x02
145 #define AUVIA_RPMODE_STEREO 0x10
146 #define AUVIA_RPMODE_16BIT 0x20
147 #define AUVIA_RPMODE_AUTOSTART 0x80
148 #define AUVIA_PLAY_DMAOPS_BASE 0x04
149 #define AUVIA_RECORD_DMAOPS_BASE 0x14
150
151 #define AUVIA_CODEC_CTL 0x80
152 #define AUVIA_CODEC_READ 0x00800000
153 #define AUVIA_CODEC_BUSY 0x01000000
154 #define AUVIA_CODEC_PRIVALID 0x02000000
155 #define AUVIA_CODEC_INDEX(x) ((x)<<16)
156
157 #define TIMEOUT 50
158
159 struct audio_hw_if auvia_hw_if = {
160 auvia_open,
161 auvia_close,
162 NULL, /* drain */
163 auvia_query_encoding,
164 auvia_set_params,
165 auvia_round_blocksize,
166 NULL, /* commit_settings */
167 NULL, /* init_output */
168 NULL, /* init_input */
169 NULL, /* start_output */
170 NULL, /* start_input */
171 auvia_halt_output,
172 auvia_halt_input,
173 NULL, /* speaker_ctl */
174 auvia_getdev,
175 NULL, /* setfd */
176 auvia_set_port,
177 auvia_get_port,
178 auvia_query_devinfo,
179 auvia_malloc,
180 auvia_free,
181 auvia_round_buffersize,
182 auvia_mappage,
183 auvia_get_props,
184 auvia_trigger_output,
185 auvia_trigger_input,
186 NULL, /* dev_ioctl */
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(pa, &ih)) {
239 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
240 return;
241 }
242 intrstr = pci_intr_string(pc, ih);
243
244 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
245 if (sc->sc_ih == NULL) {
246 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
247 if (intrstr != NULL)
248 printf(" at %s", intrstr);
249 printf("\n");
250 return;
251 }
252
253 sc->sc_dmat = pa->pa_dmat;
254 sc->sc_pc = pc;
255 sc->sc_pt = pt;
256
257 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
258
259 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
260 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
261 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
262 return;
263 }
264
265 /* disable SBPro compat & others */
266 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
267
268 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
269 /* XXX what to do about MIDI, FM, joystick? */
270
271 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
272 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
273
274 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
275
276 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
277
278 sc->host_if.arg = sc;
279 sc->host_if.attach = auvia_attach_codec;
280 sc->host_if.read = auvia_read_codec;
281 sc->host_if.write = auvia_write_codec;
282 sc->host_if.reset = auvia_reset_codec;
283
284 if ((r = ac97_attach(&sc->host_if)) != 0) {
285 printf("%s: can't attach codec (error 0x%X)\n",
286 sc->sc_dev.dv_xname, r);
287 return;
288 }
289
290 /*
291 * Print a warning if the codec doesn't support hardware variable
292 * rate audio.
293 */
294 if (auvia_read_codec(sc, AC97_REG_EXTENDED_ID, &v)
295 || !(v & AC97_CODEC_DOES_VRA)) {
296 printf("%s: warning: codec doesn't support hardware AC'97 2.0 Variable Rate Audio\n",
297 sc->sc_dev.dv_xname);
298 sc->sc_fixed_rate = AUVIA_FIXED_RATE; /* XXX wrong value */
299 } else {
300 /* enable VRA */
301 auvia_write_codec(sc, AC97_REG_EXTENDED_STATUS,
302 AC97_ENAB_VRA | AC97_ENAB_MICVRA);
303 sc->sc_fixed_rate = 0;
304 }
305
306 /* disable mutes */
307 for (i = 0; i < 4; i++) {
308 static struct {
309 char *class, *device;
310 } d[] = {
311 { AudioCoutputs, AudioNmaster},
312 { AudioCinputs, AudioNdac},
313 { AudioCinputs, AudioNcd},
314 { AudioCinputs, AudioNline},
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_PCM_FRONT_DAC_RATE : AC97_REG_PCM_LR_ADC_RATE;
552
553 if (!sc->sc_fixed_rate) {
554 auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate);
555 auvia_read_codec(sc, reg, ®val);
556 p->sample_rate = regval;
557 } else
558 p->sample_rate = sc->sc_fixed_rate;
559
560 p->factor = 1;
561 p->sw_code = 0;
562 switch (p->encoding) {
563 case AUDIO_ENCODING_SLINEAR_BE:
564 if (p->precision == 16)
565 p->sw_code = swap_bytes;
566 else
567 p->sw_code = change_sign8;
568 break;
569 case AUDIO_ENCODING_SLINEAR_LE:
570 if (p->precision != 16)
571 p->sw_code = change_sign8;
572 break;
573 case AUDIO_ENCODING_ULINEAR_BE:
574 if (p->precision == 16) {
575 if (mode == AUMODE_PLAY)
576 p->sw_code = swap_bytes_change_sign16_le;
577 else
578 p->sw_code = change_sign16_swap_bytes_le;
579 }
580 break;
581 case AUDIO_ENCODING_ULINEAR_LE:
582 if (p->precision == 16)
583 p->sw_code = change_sign16_le;
584 break;
585 case AUDIO_ENCODING_ULAW:
586 if (mode == AUMODE_PLAY) {
587 p->factor = 2;
588 p->sw_code = mulaw_to_slinear16_le;
589 } else
590 p->sw_code = ulinear8_to_mulaw;
591 break;
592 case AUDIO_ENCODING_ALAW:
593 if (mode == AUMODE_PLAY) {
594 p->factor = 2;
595 p->sw_code = alaw_to_slinear16_le;
596 } else
597 p->sw_code = ulinear8_to_alaw;
598 break;
599 default:
600 return (EINVAL);
601 }
602
603 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
604 | (p->precision * p->factor == 16 ?
605 AUVIA_RPMODE_16BIT : 0)
606 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
607 | AUVIA_RPMODE_AUTOSTART;
608
609 if (mode == AUMODE_PLAY) {
610 sc->sc_play.sc_reg = regval;
611 } else {
612 sc->sc_record.sc_reg = regval;
613 }
614 }
615
616 return 0;
617 }
618
619
620 int
621 auvia_round_blocksize(void *addr, int blk)
622 {
623 return (blk & -32);
624 }
625
626
627 int
628 auvia_halt_output(void *addr)
629 {
630 struct auvia_softc *sc = addr;
631
632 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
633 AUVIA_RPCTRL_TERMINATE);
634
635 return 0;
636 }
637
638
639 int
640 auvia_halt_input(void *addr)
641 {
642 struct auvia_softc *sc = addr;
643
644 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
645 AUVIA_RPCTRL_TERMINATE);
646
647 return 0;
648 }
649
650
651 int
652 auvia_getdev(void *addr, struct audio_device *retp)
653 {
654 struct auvia_softc *sc = addr;
655
656 if (retp) {
657 strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name));
658 strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
659 strncpy(retp->config, "auvia", sizeof(retp->config));
660 }
661
662 return 0;
663 }
664
665
666 int
667 auvia_set_port(void *addr, mixer_ctrl_t *cp)
668 {
669 struct auvia_softc *sc = addr;
670
671 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
672 }
673
674
675 int
676 auvia_get_port(void *addr, mixer_ctrl_t *cp)
677 {
678 struct auvia_softc *sc = addr;
679
680 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
681 }
682
683
684 int
685 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
686 {
687 struct auvia_softc *sc = addr;
688
689 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
690 }
691
692
693 void *
694 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags)
695 {
696 struct auvia_softc *sc = addr;
697 struct auvia_dma *p;
698 int error;
699 int rseg;
700
701 p = malloc(sizeof(*p), pool, flags);
702 if (!p)
703 return 0;
704
705 p->size = size;
706 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
707 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
708 printf("%s: unable to allocate dma, error = %d\n",
709 sc->sc_dev.dv_xname, error);
710 goto fail_alloc;
711 }
712
713 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
714 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
715 printf("%s: unable to map dma, error = %d\n",
716 sc->sc_dev.dv_xname, error);
717 goto fail_map;
718 }
719
720 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
721 BUS_DMA_NOWAIT, &p->map)) != 0) {
722 printf("%s: unable to create dma map, error = %d\n",
723 sc->sc_dev.dv_xname, error);
724 goto fail_create;
725 }
726
727 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
728 BUS_DMA_NOWAIT)) != 0) {
729 printf("%s: unable to load dma map, error = %d\n",
730 sc->sc_dev.dv_xname, error);
731 goto fail_load;
732 }
733
734 p->next = sc->sc_dmas;
735 sc->sc_dmas = p;
736
737 return p->addr;
738
739
740 fail_load:
741 bus_dmamap_destroy(sc->sc_dmat, p->map);
742 fail_create:
743 bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
744 fail_map:
745 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
746 fail_alloc:
747 free(p, pool);
748 return 0;
749 }
750
751
752 void
753 auvia_free(void *addr, void *ptr, int pool)
754 {
755 struct auvia_softc *sc = addr;
756 struct auvia_dma **pp, *p;
757
758 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
759 if (p->addr == ptr) {
760 bus_dmamap_unload(sc->sc_dmat, p->map);
761 bus_dmamap_destroy(sc->sc_dmat, p->map);
762 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
763 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
764
765 *pp = p->next;
766 free(p, pool);
767 return;
768 }
769
770 panic("auvia_free: trying to free unallocated memory");
771 }
772
773
774 size_t
775 auvia_round_buffersize(void *addr, int direction, size_t size)
776 {
777 return size;
778 }
779
780
781 paddr_t
782 auvia_mappage(void *addr, void *mem, off_t off, int prot)
783 {
784 struct auvia_softc *sc = addr;
785 struct auvia_dma *p;
786
787 if (off < 0)
788 return -1;
789
790 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
791 ;
792
793 if (!p)
794 return -1;
795
796 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
797 BUS_DMA_WAITOK);
798 }
799
800
801 int
802 auvia_get_props(void *addr)
803 {
804 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT
805 | AUDIO_PROP_FULLDUPLEX;
806 }
807
808
809 int
810 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
811 struct auvia_dma *p, void *start, void *end, int blksize)
812 {
813 struct auvia_dma_op *op;
814 struct auvia_dma *dp;
815 bus_addr_t s, e;
816 size_t l;
817 int segs;
818
819 s = p->map->dm_segs[0].ds_addr;
820 l = ((char *)end - (char *)start);
821 e = s + l;
822 segs = (l + blksize - 1) / blksize;
823
824 if (segs > (ch->sc_dma_op_count)) {
825 /* if old list was too small, free it */
826 if (ch->sc_dma_ops) {
827 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
828 }
829
830 ch->sc_dma_ops = auvia_malloc(sc, 0,
831 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
832
833 if (ch->sc_dma_ops == NULL) {
834 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
835 return 1;
836 }
837
838 for (dp = sc->sc_dmas;
839 dp && dp->addr != (void *)(ch->sc_dma_ops);
840 dp = dp->next)
841 ;
842
843 if (!dp)
844 panic("%s: build_dma_ops: where'd my memory go??? "
845 "address (%p)\n", sc->sc_dev.dv_xname,
846 ch->sc_dma_ops);
847
848 ch->sc_dma_op_count = segs;
849 ch->sc_dma_ops_dma = dp;
850 }
851
852 dp = ch->sc_dma_ops_dma;
853 op = ch->sc_dma_ops;
854
855 while (l) {
856 op->ptr = s;
857 l = l - blksize;
858 if (!l) {
859 /* if last block */
860 op->flags = AUVIA_DMAOP_EOL | blksize;
861 } else {
862 op->flags = AUVIA_DMAOP_FLAG | blksize;
863 }
864 s += blksize;
865 op++;
866 }
867
868 return 0;
869 }
870
871
872 int
873 auvia_trigger_output(void *addr, void *start, void *end,
874 int blksize, void (*intr)(void *), void *arg,
875 struct audio_params *param)
876 {
877 struct auvia_softc *sc = addr;
878 struct auvia_softc_chan *ch = &(sc->sc_play);
879 struct auvia_dma *p;
880
881 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
882 ;
883
884 if (!p)
885 panic("auvia_trigger_output: request with bad start "
886 "address (%p)\n", start);
887
888 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
889 return 1;
890 }
891
892 ch->sc_intr = intr;
893 ch->sc_arg = arg;
894
895 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE,
896 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
897
898 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE,
899 ch->sc_reg);
900
901 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
902 AUVIA_RPCTRL_START);
903
904 return 0;
905 }
906
907
908 int
909 auvia_trigger_input(void *addr, void *start, void *end,
910 int blksize, void (*intr)(void *), void *arg,
911 struct audio_params *param)
912 {
913 struct auvia_softc *sc = addr;
914 struct auvia_softc_chan *ch = &(sc->sc_record);
915 struct auvia_dma *p;
916
917 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
918 ;
919
920 if (!p)
921 panic("auvia_trigger_input: request with bad start "
922 "address (%p)\n", start);
923
924 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
925 return 1;
926 }
927
928 ch->sc_intr = intr;
929 ch->sc_arg = arg;
930
931 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE,
932 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
933
934 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE,
935 ch->sc_reg);
936
937 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
938 AUVIA_RPCTRL_START);
939
940 return 0;
941 }
942
943
944 int
945 auvia_intr(void *arg)
946 {
947 struct auvia_softc *sc = arg;
948 u_int8_t r;
949
950 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT);
951 if (r & AUVIA_RPSTAT_INTR) {
952 if (sc->sc_record.sc_intr)
953 sc->sc_record.sc_intr(sc->sc_record.sc_arg);
954
955 /* clear interrupts */
956 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT,
957 AUVIA_RPSTAT_INTR);
958 }
959 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT);
960 if (r & AUVIA_RPSTAT_INTR) {
961 if (sc->sc_play.sc_intr)
962 sc->sc_play.sc_intr(sc->sc_play.sc_arg);
963
964 /* clear interrupts */
965 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT,
966 AUVIA_RPSTAT_INTR);
967 }
968
969 return 1;
970 }
971