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