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