awacs.c revision 1.4.2.2 1 /* $NetBSD: awacs.c,v 1.4.2.2 2001/06/21 19:27:23 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/audioio.h>
31 #include <sys/device.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34
35 #include <dev/auconv.h>
36 #include <dev/audio_if.h>
37 #include <dev/mulaw.h>
38
39 #include <uvm/uvm_extern.h>
40
41 #include <machine/autoconf.h>
42 #include <machine/pio.h>
43
44 #include <dev/ofw/openfirm.h>
45 #include <macppc/dev/dbdma.h>
46
47 #ifdef AWACS_DEBUG
48 # define DPRINTF printf
49 #else
50 # define DPRINTF while (0) printf
51 #endif
52
53 /* sc_flags values */
54 #define AWACS_CAP_BSWAP 0x0001
55
56 struct awacs_softc {
57 struct device sc_dev;
58 int sc_flags;
59
60 void (*sc_ointr)(void *); /* dma completion intr handler */
61 void *sc_oarg; /* arg for sc_ointr() */
62 int sc_opages; /* # of output pages */
63
64 void (*sc_iintr)(void *); /* dma completion intr handler */
65 void *sc_iarg; /* arg for sc_iintr() */
66
67 u_int sc_record_source; /* recording source mask */
68 u_int sc_output_mask; /* output source mask */
69
70 char *sc_reg;
71 u_int sc_codecctl0;
72 u_int sc_codecctl1;
73 u_int sc_codecctl2;
74 u_int sc_codecctl4;
75 u_int sc_soundctl;
76
77 struct dbdma_regmap *sc_odma;
78 struct dbdma_regmap *sc_idma;
79 struct dbdma_command *sc_odmacmd;
80 struct dbdma_command *sc_idmacmd;
81 };
82
83 int awacs_match(struct device *, struct cfdata *, void *);
84 void awacs_attach(struct device *, struct device *, void *);
85 int awacs_intr(void *);
86
87 int awacs_open(void *, int);
88 void awacs_close(void *);
89 int awacs_query_encoding(void *, struct audio_encoding *);
90 int awacs_set_params(void *, int, int, struct audio_params *,
91 struct audio_params *);
92 int awacs_round_blocksize(void *, int);
93 int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
94 void *, struct audio_params *);
95 int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
96 void *, struct audio_params *);
97 int awacs_halt_output(void *);
98 int awacs_halt_input(void *);
99 int awacs_getdev(void *, struct audio_device *);
100 int awacs_set_port(void *, mixer_ctrl_t *);
101 int awacs_get_port(void *, mixer_ctrl_t *);
102 int awacs_query_devinfo(void *, mixer_devinfo_t *);
103 size_t awacs_round_buffersize(void *, int, size_t);
104 paddr_t awacs_mappage(void *, void *, off_t, int);
105 int awacs_get_props(void *);
106
107 static inline u_int awacs_read_reg(struct awacs_softc *, int);
108 static inline void awacs_write_reg(struct awacs_softc *, int, int);
109 void awacs_write_codec(struct awacs_softc *, int);
110 void awacs_set_speaker_volume(struct awacs_softc *, int, int);
111 void awacs_set_ext_volume(struct awacs_softc *, int, int);
112 int awacs_set_rate(struct awacs_softc *, int);
113
114 static void mono16_to_stereo16(void *, u_char *, int);
115 static void swap_bytes_mono16_to_stereo16(void *, u_char *, int);
116
117 struct cfattach awacs_ca = {
118 sizeof(struct awacs_softc), awacs_match, awacs_attach
119 };
120
121 struct audio_hw_if awacs_hw_if = {
122 awacs_open,
123 awacs_close,
124 NULL,
125 awacs_query_encoding,
126 awacs_set_params,
127 awacs_round_blocksize,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 awacs_halt_output,
134 awacs_halt_input,
135 NULL,
136 awacs_getdev,
137 NULL,
138 awacs_set_port,
139 awacs_get_port,
140 awacs_query_devinfo,
141 NULL,
142 NULL,
143 awacs_round_buffersize,
144 awacs_mappage,
145 awacs_get_props,
146 awacs_trigger_output,
147 awacs_trigger_input,
148 };
149
150 struct audio_device awacs_device = {
151 "AWACS",
152 "",
153 "awacs"
154 };
155
156 /* register offset */
157 #define AWACS_SOUND_CTRL 0x00
158 #define AWACS_CODEC_CTRL 0x10
159 #define AWACS_CODEC_STATUS 0x20
160 #define AWACS_CLIP_COUNT 0x30
161 #define AWACS_BYTE_SWAP 0x40
162
163 /* sound control */
164 #define AWACS_INPUT_SUBFRAME0 0x00000001
165 #define AWACS_INPUT_SUBFRAME1 0x00000002
166 #define AWACS_INPUT_SUBFRAME2 0x00000004
167 #define AWACS_INPUT_SUBFRAME3 0x00000008
168
169 #define AWACS_OUTPUT_SUBFRAME0 0x00000010
170 #define AWACS_OUTPUT_SUBFRAME1 0x00000020
171 #define AWACS_OUTPUT_SUBFRAME2 0x00000040
172 #define AWACS_OUTPUT_SUBFRAME3 0x00000080
173
174 #define AWACS_RATE_44100 0x00000000
175 #define AWACS_RATE_29400 0x00000100
176 #define AWACS_RATE_22050 0x00000200
177 #define AWACS_RATE_17640 0x00000300
178 #define AWACS_RATE_14700 0x00000400
179 #define AWACS_RATE_11025 0x00000500
180 #define AWACS_RATE_8820 0x00000600
181 #define AWACS_RATE_7350 0x00000700
182 #define AWACS_RATE_MASK 0x00000700
183
184 /* codec control */
185 #define AWACS_CODEC_ADDR0 0x00000000
186 #define AWACS_CODEC_ADDR1 0x00001000
187 #define AWACS_CODEC_ADDR2 0x00002000
188 #define AWACS_CODEC_ADDR4 0x00004000
189 #define AWACS_CODEC_EMSEL0 0x00000000
190 #define AWACS_CODEC_EMSEL1 0x00400000
191 #define AWACS_CODEC_EMSEL2 0x00800000
192 #define AWACS_CODEC_EMSEL4 0x00c00000
193 #define AWACS_CODEC_BUSY 0x01000000
194
195 /* cc0 */
196 #define AWACS_DEFAULT_CD_GAIN 0x000000bb
197 #define AWACS_INPUT_CD 0x00000200
198 #define AWACS_INPUT_LINE 0x00000400
199 #define AWACS_INPUT_MICROPHONE 0x00000800
200 #define AWACS_INPUT_MASK 0x00000e00
201
202 /* cc1 */
203 #define AWACS_MUTE_SPEAKER 0x00000080
204 #define AWACS_MUTE_HEADPHONE 0x00000200
205
206 int
207 awacs_match(parent, match, aux)
208 struct device *parent;
209 struct cfdata *match;
210 void *aux;
211 {
212 struct confargs *ca = aux;
213
214 if (strcmp(ca->ca_name, "awacs") != 0 &&
215 strcmp(ca->ca_name, "davbus") != 0)
216 return 0;
217
218 if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
219 return 0;
220
221 return 1;
222 }
223
224 void
225 awacs_attach(parent, self, aux)
226 struct device *parent;
227 struct device *self;
228 void *aux;
229 {
230 struct awacs_softc *sc = (struct awacs_softc *)self;
231 struct confargs *ca = aux;
232 int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
233
234 ca->ca_reg[0] += ca->ca_baseaddr;
235 ca->ca_reg[2] += ca->ca_baseaddr;
236 ca->ca_reg[4] += ca->ca_baseaddr;
237
238 sc->sc_reg = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
239
240 sc->sc_odma = mapiodev(ca->ca_reg[2], ca->ca_reg[3]); /* out */
241 sc->sc_idma = mapiodev(ca->ca_reg[4], ca->ca_reg[5]); /* in */
242 sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
243 sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
244
245 if (ca->ca_nintr == 24) {
246 cirq = ca->ca_intr[0];
247 oirq = ca->ca_intr[2];
248 iirq = ca->ca_intr[4];
249 cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
250 oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
251 iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
252 } else {
253 cirq = ca->ca_intr[0];
254 oirq = ca->ca_intr[1];
255 iirq = ca->ca_intr[2];
256 cirq_type = oirq_type = iirq_type = IST_LEVEL;
257 }
258 intr_establish(cirq, cirq_type, IPL_AUDIO, awacs_intr, sc);
259 intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
260 /* intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc); */
261
262 printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
263
264 /* XXX Uni-North based models don't have byteswap capability. */
265 if (OF_finddevice("/uni-n") == -1)
266 sc->sc_flags |= AWACS_CAP_BSWAP;
267
268 sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
269 AWACS_RATE_44100;
270 awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
271
272 sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
273 sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
274 sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
275 sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
276
277 sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
278 awacs_write_codec(sc, sc->sc_codecctl0);
279
280 /* Set initial volume[s] */
281 awacs_set_speaker_volume(sc, 80, 80);
282
283 /* Set loopback (for CD?) */
284 /* sc->sc_codecctl1 |= 0x440; */
285 sc->sc_codecctl1 |= 0x40;
286 awacs_write_codec(sc, sc->sc_codecctl1);
287
288 /* default output to speakers */
289 sc->sc_output_mask = 1 << 0;
290 sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
291 sc->sc_codecctl1 |= AWACS_MUTE_HEADPHONE;
292 awacs_write_codec(sc, sc->sc_codecctl1);
293
294 /* default input from CD */
295 sc->sc_record_source = 1 << 0;
296 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
297 sc->sc_codecctl0 |= AWACS_INPUT_CD;
298 awacs_write_codec(sc, sc->sc_codecctl0);
299
300 /* Enable interrupts and looping mode. */
301 /* XXX ... */
302
303 audio_attach_mi(&awacs_hw_if, sc, &sc->sc_dev);
304 }
305
306 static inline u_int
307 awacs_read_reg(sc, reg)
308 struct awacs_softc *sc;
309 int reg;
310 {
311 char *addr = sc->sc_reg;
312
313 return in32rb(addr + reg);
314 }
315
316 static inline void
317 awacs_write_reg(sc, reg, val)
318 struct awacs_softc *sc;
319 int reg, val;
320 {
321 char *addr = sc->sc_reg;
322
323 out32rb(addr + reg, val);
324 }
325
326 void
327 awacs_write_codec(sc, value)
328 struct awacs_softc *sc;
329 int value;
330 {
331 awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
332 while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
333 }
334
335 int
336 awacs_intr(v)
337 void *v;
338 {
339 struct awacs_softc *sc = v;
340 struct dbdma_command *cmd = sc->sc_odmacmd;
341 int count = sc->sc_opages;
342 int status;
343
344 /* Fill used buffer(s). */
345 while (count-- > 0) {
346 /* if DBDMA_INT_ALWAYS */
347 if (in16rb(&cmd->d_command) & 0x30) { /* XXX */
348 status = in16rb(&cmd->d_status);
349 cmd->d_status = 0;
350 if (status) /* status == 0x8400 */
351 if (sc->sc_ointr)
352 (*sc->sc_ointr)(sc->sc_oarg);
353 }
354 cmd++;
355 }
356
357 return 1;
358 }
359
360 int
361 awacs_open(h, flags)
362 void *h;
363 int flags;
364 {
365 return 0;
366 }
367
368 /*
369 * Close function is called at splaudio().
370 */
371 void
372 awacs_close(h)
373 void *h;
374 {
375 struct awacs_softc *sc = h;
376
377 awacs_halt_output(sc);
378 awacs_halt_input(sc);
379
380 sc->sc_ointr = 0;
381 sc->sc_iintr = 0;
382 }
383
384 int
385 awacs_query_encoding(h, ae)
386 void *h;
387 struct audio_encoding *ae;
388 {
389 struct awacs_softc *sc = h;
390
391 ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
392
393 switch (ae->index) {
394 case 0:
395 strcpy(ae->name, AudioEslinear);
396 ae->encoding = AUDIO_ENCODING_SLINEAR;
397 ae->precision = 16;
398 ae->flags = 0;
399 return 0;
400 case 1:
401 strcpy(ae->name, AudioEslinear_be);
402 ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
403 ae->precision = 16;
404 ae->flags = 0;
405 return 0;
406 case 2:
407 strcpy(ae->name, AudioEslinear_le);
408 ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
409 ae->precision = 16;
410 if (sc->sc_flags & AWACS_CAP_BSWAP)
411 ae->flags = 0;
412 return 0;
413 case 3:
414 strcpy(ae->name, AudioEulinear_be);
415 ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
416 ae->precision = 16;
417 return 0;
418 case 4:
419 strcpy(ae->name, AudioEulinear_le);
420 ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
421 ae->precision = 16;
422 return 0;
423 case 5:
424 strcpy(ae->name, AudioEmulaw);
425 ae->encoding = AUDIO_ENCODING_ULAW;
426 ae->precision = 8;
427 return 0;
428 case 6:
429 strcpy(ae->name, AudioEalaw);
430 ae->encoding = AUDIO_ENCODING_ALAW;
431 ae->precision = 8;
432 return 0;
433 default:
434 return EINVAL;
435 }
436 }
437
438 static void
439 mono16_to_stereo16(v, p, cc)
440 void *v;
441 u_char *p;
442 int cc;
443 {
444 int x;
445 int16_t *src, *dst;
446
447 src = (void *)(p + cc);
448 dst = (void *)(p + cc * 2);
449 while (cc > 0) {
450 x = *--src;
451 *--dst = x;
452 *--dst = x;
453 cc -= 2;
454 }
455 }
456
457 static void
458 swap_bytes_mono16_to_stereo16(v, p, cc)
459 void *v;
460 u_char *p;
461 int cc;
462 {
463 swap_bytes(v, p, cc);
464 mono16_to_stereo16(v, p, cc);
465 }
466
467 int
468 awacs_set_params(h, setmode, usemode, play, rec)
469 void *h;
470 int setmode, usemode;
471 struct audio_params *play, *rec;
472 {
473 struct awacs_softc *sc = h;
474 struct audio_params *p;
475 int mode, rate;
476
477 /*
478 * This device only has one clock, so make the sample rates match.
479 */
480 if (play->sample_rate != rec->sample_rate &&
481 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
482 if (setmode == AUMODE_PLAY) {
483 rec->sample_rate = play->sample_rate;
484 setmode |= AUMODE_RECORD;
485 } else if (setmode == AUMODE_RECORD) {
486 play->sample_rate = rec->sample_rate;
487 setmode |= AUMODE_PLAY;
488 } else
489 return EINVAL;
490 }
491
492 for (mode = AUMODE_RECORD; mode != -1;
493 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
494 if ((setmode & mode) == 0)
495 continue;
496
497 p = mode == AUMODE_PLAY ? play : rec;
498
499 if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
500 (p->precision != 8 && p->precision != 16) ||
501 (p->channels != 1 && p->channels != 2))
502 return EINVAL;
503
504 p->factor = 1;
505 p->sw_code = 0;
506 awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
507
508 switch (p->encoding) {
509
510 case AUDIO_ENCODING_SLINEAR_LE:
511 if (sc->sc_flags & AWACS_CAP_BSWAP)
512 awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
513 else {
514 if (p->channels == 2 && p->precision == 16) {
515 p->sw_code = swap_bytes;
516 break;
517 }
518 if (p->channels == 1 && p->precision == 16) {
519 p->factor = 2;
520 p->sw_code =
521 swap_bytes_mono16_to_stereo16;
522 break;
523 }
524 return EINVAL;
525 }
526 case AUDIO_ENCODING_SLINEAR_BE:
527 if (p->channels == 1 && p->precision == 16) {
528 p->factor = 2;
529 p->sw_code = mono16_to_stereo16;
530 break;
531 }
532 if (p->channels == 2 && p->precision == 16)
533 break;
534
535 return EINVAL;
536
537 case AUDIO_ENCODING_ULINEAR_LE:
538 if (p->channels == 2 && p->precision == 16) {
539 p->sw_code = swap_bytes_change_sign16_be;
540 break;
541 }
542 return EINVAL;
543
544 case AUDIO_ENCODING_ULINEAR_BE:
545 if (p->channels == 2 && p->precision == 16) {
546 p->sw_code = change_sign16_be;
547 break;
548 }
549 return EINVAL;
550
551 case AUDIO_ENCODING_ULAW:
552 if (mode == AUMODE_PLAY) {
553 p->factor = 2;
554 p->sw_code = mulaw_to_slinear16_be;
555 break;
556 } else
557 break; /* XXX */
558
559 return EINVAL;
560
561 case AUDIO_ENCODING_ALAW:
562 if (mode == AUMODE_PLAY) {
563 p->factor = 2;
564 p->sw_code = alaw_to_slinear16_be;
565 break;
566 }
567 return EINVAL;
568
569 default:
570 return EINVAL;
571 }
572 }
573
574 /* Set the speed */
575 rate = p->sample_rate;
576
577 if (awacs_set_rate(sc, rate))
578 return EINVAL;
579
580 return 0;
581 }
582
583 int
584 awacs_round_blocksize(h, size)
585 void *h;
586 int size;
587 {
588 if (size < NBPG)
589 size = NBPG;
590 return size & ~PGOFSET;
591 }
592
593 int
594 awacs_halt_output(h)
595 void *h;
596 {
597 struct awacs_softc *sc = h;
598
599 dbdma_stop(sc->sc_odma);
600 dbdma_reset(sc->sc_odma);
601 return 0;
602 }
603
604 int
605 awacs_halt_input(h)
606 void *h;
607 {
608 struct awacs_softc *sc = h;
609
610 dbdma_stop(sc->sc_idma);
611 dbdma_reset(sc->sc_idma);
612 return 0;
613 }
614
615 int
616 awacs_getdev(h, retp)
617 void *h;
618 struct audio_device *retp;
619 {
620 *retp = awacs_device;
621 return 0;
622 }
623
624 enum {
625 AWACS_MONITOR_CLASS,
626 AWACS_OUTPUT_CLASS,
627 AWACS_RECORD_CLASS,
628 AWACS_OUTPUT_SELECT,
629 AWACS_VOL_SPEAKER,
630 AWACS_VOL_HEADPHONE,
631 AWACS_INPUT_SELECT,
632 AWACS_VOL_INPUT,
633 AWACS_ENUM_LAST
634 };
635
636 int
637 awacs_set_port(h, mc)
638 void *h;
639 mixer_ctrl_t *mc;
640 {
641 struct awacs_softc *sc = h;
642 int l, r;
643
644 DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
645
646 l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
647 r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
648
649 switch (mc->dev) {
650 case AWACS_OUTPUT_SELECT:
651 /* No change necessary? */
652 if (mc->un.mask == sc->sc_output_mask)
653 return 0;
654
655 sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
656 if (mc->un.mask & 1 << 0)
657 sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
658 if (mc->un.mask & 1 << 1)
659 sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
660
661 awacs_write_codec(sc, sc->sc_codecctl1);
662 sc->sc_output_mask = mc->un.mask;
663 return 0;
664
665 case AWACS_VOL_SPEAKER:
666 awacs_set_speaker_volume(sc, l, r);
667 return 0;
668
669 case AWACS_VOL_HEADPHONE:
670 awacs_set_ext_volume(sc, l, r);
671 return 0;
672
673 case AWACS_INPUT_SELECT:
674 /* no change necessary? */
675 if (mc->un.mask == sc->sc_record_source)
676 return 0;
677 switch (mc->un.mask) {
678 case 1 << 0: /* CD */
679 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
680 sc->sc_codecctl0 |= AWACS_INPUT_CD;
681 awacs_write_codec(sc, sc->sc_codecctl0);
682 break;
683 case 1 << 1: /* microphone */
684 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
685 sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
686 awacs_write_codec(sc, sc->sc_codecctl0);
687 break;
688 case 1 << 2: /* line in */
689 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
690 sc->sc_codecctl0 |= AWACS_INPUT_LINE;
691 awacs_write_codec(sc, sc->sc_codecctl0);
692 break;
693 default: /* invalid argument */
694 return EINVAL;
695 }
696 sc->sc_record_source = mc->un.mask;
697 return 0;
698
699 case AWACS_VOL_INPUT:
700 sc->sc_codecctl0 &= ~0xff;
701 sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
702 awacs_write_codec(sc, sc->sc_codecctl0);
703 return 0;
704 }
705
706 return ENXIO;
707 }
708
709 int
710 awacs_get_port(h, mc)
711 void *h;
712 mixer_ctrl_t *mc;
713 {
714 struct awacs_softc *sc = h;
715 int vol, l, r;
716
717 DPRINTF("awacs_get_port dev = %d, type = %d\n", mc->dev, mc->type);
718
719 switch (mc->dev) {
720 case AWACS_OUTPUT_SELECT:
721 mc->un.mask = sc->sc_output_mask;
722 return 0;
723
724 case AWACS_VOL_SPEAKER:
725 vol = sc->sc_codecctl4;
726 l = (15 - ((vol & 0x3c0) >> 6)) * 16;
727 r = (15 - (vol & 0x0f)) * 16;
728 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
729 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
730 return 0;
731
732 case AWACS_VOL_HEADPHONE:
733 vol = sc->sc_codecctl2;
734 l = (15 - ((vol & 0x3c0) >> 6)) * 16;
735 r = (15 - (vol & 0x0f)) * 16;
736 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
737 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
738 return 0;
739
740 case AWACS_INPUT_SELECT:
741 mc->un.mask = sc->sc_record_source;
742 return 0;
743
744 case AWACS_VOL_INPUT:
745 vol = sc->sc_codecctl0 & 0xff;
746 l = (vol & 0xf0);
747 r = (vol & 0x0f) << 4;
748 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
749 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
750 return 0;
751
752 default:
753 return ENXIO;
754 }
755
756 return 0;
757 }
758
759 int
760 awacs_query_devinfo(h, dip)
761 void *h;
762 mixer_devinfo_t *dip;
763 {
764
765 DPRINTF("query_devinfo %d\n", dip->index);
766
767 switch (dip->index) {
768
769 case AWACS_OUTPUT_SELECT:
770 dip->mixer_class = AWACS_MONITOR_CLASS;
771 strcpy(dip->label.name, AudioNoutput);
772 dip->type = AUDIO_MIXER_SET;
773 dip->prev = dip->next = AUDIO_MIXER_LAST;
774 dip->un.s.num_mem = 2;
775 strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
776 dip->un.s.member[0].mask = 1 << 0;
777 strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
778 dip->un.s.member[1].mask = 1 << 1;
779 return 0;
780
781 case AWACS_VOL_SPEAKER:
782 dip->mixer_class = AWACS_OUTPUT_CLASS;
783 strcpy(dip->label.name, AudioNspeaker);
784 dip->type = AUDIO_MIXER_VALUE;
785 dip->prev = dip->next = AUDIO_MIXER_LAST;
786 dip->un.v.num_channels = 2;
787 strcpy(dip->un.v.units.name, AudioNvolume);
788 return 0;
789
790 case AWACS_VOL_HEADPHONE:
791 dip->mixer_class = AWACS_OUTPUT_CLASS;
792 strcpy(dip->label.name, AudioNheadphone);
793 dip->type = AUDIO_MIXER_VALUE;
794 dip->prev = dip->next = AUDIO_MIXER_LAST;
795 dip->un.v.num_channels = 2;
796 strcpy(dip->un.v.units.name, AudioNvolume);
797 return 0;
798
799 case AWACS_INPUT_SELECT:
800 dip->mixer_class = AWACS_RECORD_CLASS;
801 strcpy(dip->label.name, AudioNsource);
802 dip->type = AUDIO_MIXER_SET;
803 dip->prev = dip->next = AUDIO_MIXER_LAST;
804 dip->un.s.num_mem = 3;
805 strcpy(dip->un.s.member[0].label.name, AudioNcd);
806 dip->un.s.member[0].mask = 1 << 0;
807 strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
808 dip->un.s.member[1].mask = 1 << 1;
809 strcpy(dip->un.s.member[2].label.name, AudioNline);
810 dip->un.s.member[2].mask = 1 << 2;
811 return 0;
812
813 case AWACS_VOL_INPUT:
814 dip->mixer_class = AWACS_RECORD_CLASS;
815 strcpy(dip->label.name, AudioNrecord);
816 dip->type = AUDIO_MIXER_VALUE;
817 dip->prev = dip->next = AUDIO_MIXER_LAST;
818 dip->un.v.num_channels = 2;
819 strcpy(dip->un.v.units.name, AudioNvolume);
820 return 0;
821
822 case AWACS_MONITOR_CLASS:
823 dip->mixer_class = AWACS_MONITOR_CLASS;
824 strcpy(dip->label.name, AudioCmonitor);
825 dip->type = AUDIO_MIXER_CLASS;
826 dip->next = dip->prev = AUDIO_MIXER_LAST;
827 return 0;
828
829 case AWACS_OUTPUT_CLASS:
830 dip->mixer_class = AWACS_OUTPUT_CLASS;
831 strcpy(dip->label.name, AudioCoutputs);
832 dip->type = AUDIO_MIXER_CLASS;
833 dip->next = dip->prev = AUDIO_MIXER_LAST;
834 return 0;
835
836 case AWACS_RECORD_CLASS:
837 dip->mixer_class = AWACS_RECORD_CLASS;
838 strcpy(dip->label.name, AudioCrecord);
839 dip->type = AUDIO_MIXER_CLASS;
840 dip->next = dip->prev = AUDIO_MIXER_LAST;
841 return 0;
842 }
843
844 return ENXIO;
845 }
846
847 size_t
848 awacs_round_buffersize(h, dir, size)
849 void *h;
850 int dir;
851 size_t size;
852 {
853 if (size > 65536)
854 size = 65536;
855 return size;
856 }
857
858 paddr_t
859 awacs_mappage(h, mem, off, prot)
860 void *h;
861 void *mem;
862 off_t off;
863 int prot;
864 {
865 if (off < 0)
866 return -1;
867 return -1; /* XXX */
868 }
869
870 int
871 awacs_get_props(h)
872 void *h;
873 {
874 return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
875 }
876
877 int
878 awacs_trigger_output(h, start, end, bsize, intr, arg, param)
879 void *h;
880 void *start, *end;
881 int bsize;
882 void (*intr)(void *);
883 void *arg;
884 struct audio_params *param;
885 {
886 struct awacs_softc *sc = h;
887 struct dbdma_command *cmd = sc->sc_odmacmd;
888 vaddr_t va;
889 int i, len, intmode;
890
891 DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
892
893 sc->sc_ointr = intr;
894 sc->sc_oarg = arg;
895 sc->sc_opages = ((char *)end - (char *)start) / NBPG;
896
897 #ifdef DIAGNOSTIC
898 if (sc->sc_opages > 16)
899 panic("awacs_trigger_output");
900 #endif
901
902 va = (vaddr_t)start;
903 len = 0;
904 for (i = sc->sc_opages; i > 0; i--) {
905 len += NBPG;
906 if (len < bsize)
907 intmode = DBDMA_INT_NEVER;
908 else {
909 len = 0;
910 intmode = DBDMA_INT_ALWAYS;
911 }
912
913 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
914 intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
915 va += NBPG;
916 cmd++;
917 }
918
919 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
920 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
921 dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
922
923 dbdma_start(sc->sc_odma, sc->sc_odmacmd);
924
925 return 0;
926 }
927
928 int
929 awacs_trigger_input(h, start, end, bsize, intr, arg, param)
930 void *h;
931 void *start, *end;
932 int bsize;
933 void (*intr)(void *);
934 void *arg;
935 struct audio_params *param;
936 {
937 printf("awacs_trigger_input called\n");
938
939 return 1;
940 }
941
942 void
943 awacs_set_speaker_volume(sc, left, right)
944 struct awacs_softc *sc;
945 int left, right;
946 {
947 int lval = 15 - (left & 0xff) / 16;
948 int rval = 15 - (right & 0xff) / 16;
949
950 DPRINTF("speaker_volume %d %d\n", lval, rval);
951
952 sc->sc_codecctl4 &= ~0x3cf;
953 sc->sc_codecctl4 |= (lval << 6) | rval;
954 awacs_write_codec(sc, sc->sc_codecctl4);
955 }
956
957 void
958 awacs_set_ext_volume(sc, left, right)
959 struct awacs_softc *sc;
960 int left, right;
961 {
962 int lval = 15 - (left & 0xff) / 16;
963 int rval = 15 - (right & 0xff) / 16;
964
965 DPRINTF("ext_volume %d %d\n", lval, rval);
966
967 sc->sc_codecctl2 &= ~0x3cf;
968 sc->sc_codecctl2 |= (lval << 6) | rval;
969 awacs_write_codec(sc, sc->sc_codecctl2);
970 }
971
972 int
973 awacs_set_rate(sc, rate)
974 struct awacs_softc *sc;
975 int rate;
976 {
977 int c;
978
979 switch (rate) {
980
981 case 44100:
982 c = AWACS_RATE_44100;
983 break;
984 case 29400:
985 c = AWACS_RATE_29400;
986 break;
987 case 22050:
988 c = AWACS_RATE_22050;
989 break;
990 case 17640:
991 c = AWACS_RATE_17640;
992 break;
993 case 14700:
994 c = AWACS_RATE_14700;
995 break;
996 case 11025:
997 c = AWACS_RATE_11025;
998 break;
999 case 8820:
1000 c = AWACS_RATE_8820;
1001 break;
1002 case 8000: /* XXX */
1003 case 7350:
1004 c = AWACS_RATE_7350;
1005 break;
1006 default:
1007 return -1;
1008 }
1009
1010 sc->sc_soundctl &= ~AWACS_RATE_MASK;
1011 sc->sc_soundctl |= c;
1012 awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
1013
1014 return 0;
1015 }
1016