Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.2.2.3
      1 /*	$NetBSD: awacs.c,v 1.2.2.3 2000/11/22 16:00:37 bouyer 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 #include <macppc/dev/dbdma.h>
     44 
     45 #ifdef AWACS_DEBUG
     46 # define DPRINTF printf
     47 #else
     48 # define DPRINTF while (0) printf
     49 #endif
     50 
     51 struct awacs_softc {
     52 	struct device sc_dev;
     53 
     54 	void (*sc_ointr)(void *);	/* dma completion intr handler */
     55 	void *sc_oarg;			/* arg for sc_ointr() */
     56 	int sc_opages;			/* # of output pages */
     57 
     58 	void (*sc_iintr)(void *);	/* dma completion intr handler */
     59 	void *sc_iarg;			/* arg for sc_iintr() */
     60 
     61 	u_int sc_record_source;		/* recording source mask */
     62 	u_int sc_output_mask;		/* output source mask */
     63 
     64 	char *sc_reg;
     65 	u_int sc_codecctl0;
     66 	u_int sc_codecctl1;
     67 	u_int sc_codecctl2;
     68 	u_int sc_codecctl4;
     69 	u_int sc_soundctl;
     70 
     71 	struct dbdma_regmap *sc_odma;
     72 	struct dbdma_regmap *sc_idma;
     73 	struct dbdma_command *sc_odmacmd;
     74 	struct dbdma_command *sc_idmacmd;
     75 };
     76 
     77 int awacs_match(struct device *, struct cfdata *, void *);
     78 void awacs_attach(struct device *, struct device *, void *);
     79 int awacs_intr(void *);
     80 
     81 int awacs_open(void *, int);
     82 void awacs_close(void *);
     83 int awacs_query_encoding(void *, struct audio_encoding *);
     84 int awacs_set_params(void *, int, int, struct audio_params *,
     85 			 struct audio_params *);
     86 int awacs_round_blocksize(void *, int);
     87 int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
     88 			     void *, struct audio_params *);
     89 int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
     90 			    void *, struct audio_params *);
     91 int awacs_halt_output(void *);
     92 int awacs_halt_input(void *);
     93 int awacs_getdev(void *, struct audio_device *);
     94 int awacs_set_port(void *, mixer_ctrl_t *);
     95 int awacs_get_port(void *, mixer_ctrl_t *);
     96 int awacs_query_devinfo(void *, mixer_devinfo_t *);
     97 size_t awacs_round_buffersize(void *, int, size_t);
     98 paddr_t awacs_mappage(void *, void *, off_t, int);
     99 int awacs_get_props(void *);
    100 
    101 static inline u_int awacs_read_reg(struct awacs_softc *, int);
    102 static inline void awacs_write_reg(struct awacs_softc *, int, int);
    103 void awacs_write_codec(struct awacs_softc *, int);
    104 void awacs_set_speaker_volume(struct awacs_softc *, int, int);
    105 void awacs_set_ext_volume(struct awacs_softc *, int, int);
    106 int awacs_set_rate(struct awacs_softc *, int);
    107 
    108 struct cfattach awacs_ca = {
    109 	sizeof(struct awacs_softc), awacs_match, awacs_attach
    110 };
    111 
    112 struct audio_hw_if awacs_hw_if = {
    113 	awacs_open,
    114 	awacs_close,
    115 	NULL,
    116 	awacs_query_encoding,
    117 	awacs_set_params,
    118 	awacs_round_blocksize,
    119 	NULL,
    120 	NULL,
    121 	NULL,
    122 	NULL,
    123 	NULL,
    124 	awacs_halt_output,
    125 	awacs_halt_input,
    126 	NULL,
    127 	awacs_getdev,
    128 	NULL,
    129 	awacs_set_port,
    130 	awacs_get_port,
    131 	awacs_query_devinfo,
    132 	NULL,
    133 	NULL,
    134 	awacs_round_buffersize,
    135 	awacs_mappage,
    136 	awacs_get_props,
    137 	awacs_trigger_output,
    138 	awacs_trigger_input,
    139 };
    140 
    141 struct audio_device awacs_device = {
    142 	"AWACS",
    143 	"",
    144 	"awacs"
    145 };
    146 
    147 /* register offset */
    148 #define AWACS_SOUND_CTRL	0x00
    149 #define AWACS_CODEC_CTRL	0x10
    150 #define AWACS_CODEC_STATUS	0x20
    151 #define AWACS_CLIP_COUNT	0x30
    152 #define AWACS_BYTE_SWAP		0x40
    153 
    154 /* sound control */
    155 #define AWACS_INPUT_SUBFRAME0	0x00000001
    156 #define AWACS_INPUT_SUBFRAME1	0x00000002
    157 #define AWACS_INPUT_SUBFRAME2	0x00000004
    158 #define AWACS_INPUT_SUBFRAME3	0x00000008
    159 
    160 #define AWACS_OUTPUT_SUBFRAME0	0x00000010
    161 #define AWACS_OUTPUT_SUBFRAME1	0x00000020
    162 #define AWACS_OUTPUT_SUBFRAME2	0x00000040
    163 #define AWACS_OUTPUT_SUBFRAME3	0x00000080
    164 
    165 #define AWACS_RATE_44100	0x00000000
    166 #define AWACS_RATE_29400	0x00000100
    167 #define AWACS_RATE_22050	0x00000200
    168 #define AWACS_RATE_17640	0x00000300
    169 #define AWACS_RATE_14700	0x00000400
    170 #define AWACS_RATE_11025	0x00000500
    171 #define AWACS_RATE_8820		0x00000600
    172 #define AWACS_RATE_7350		0x00000700
    173 #define AWACS_RATE_MASK		0x00000700
    174 
    175 /* codec control */
    176 #define AWACS_CODEC_ADDR0	0x00000000
    177 #define AWACS_CODEC_ADDR1	0x00001000
    178 #define AWACS_CODEC_ADDR2	0x00002000
    179 #define AWACS_CODEC_ADDR4	0x00004000
    180 #define AWACS_CODEC_EMSEL0	0x00000000
    181 #define AWACS_CODEC_EMSEL1	0x00400000
    182 #define AWACS_CODEC_EMSEL2	0x00800000
    183 #define AWACS_CODEC_EMSEL4	0x00c00000
    184 #define AWACS_CODEC_BUSY	0x01000000
    185 
    186 /* cc0 */
    187 #define AWACS_DEFAULT_CD_GAIN	0x000000bb
    188 #define AWACS_INPUT_CD		0x00000200
    189 #define AWACS_INPUT_MIC		0x00000400
    190 #define AWACS_INPUT_MASK	0x00000e00
    191 
    192 /* cc1 */
    193 #define AWACS_MUTE_SPEAKER	0x00000080
    194 #define AWACS_MUTE_HEADPHONE	0x00000200
    195 
    196 int
    197 awacs_match(parent, match, aux)
    198 	struct device *parent;
    199 	struct cfdata *match;
    200 	void *aux;
    201 {
    202 	struct confargs *ca = aux;
    203 
    204 	if (strcmp(ca->ca_name, "awacs") != 0 &&
    205 	    strcmp(ca->ca_name, "davbus") != 0)
    206 		return 0;
    207 
    208 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    209 		return 0;
    210 
    211 	/* XXX for now */
    212 	if (ca->ca_nintr > 12)
    213 		return 0;
    214 
    215 	return 1;
    216 }
    217 
    218 void
    219 awacs_attach(parent, self, aux)
    220 	struct device *parent;
    221 	struct device *self;
    222 	void *aux;
    223 {
    224 	struct awacs_softc *sc = (struct awacs_softc *)self;
    225 	struct confargs *ca = aux;
    226 
    227 	ca->ca_reg[0] += ca->ca_baseaddr;
    228 	ca->ca_reg[2] += ca->ca_baseaddr;
    229 	ca->ca_reg[4] += ca->ca_baseaddr;
    230 
    231 	sc->sc_reg = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
    232 
    233 	sc->sc_odma = mapiodev(ca->ca_reg[2], ca->ca_reg[3]); /* out */
    234 	sc->sc_idma = mapiodev(ca->ca_reg[4], ca->ca_reg[5]); /* in */
    235 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    236 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    237 
    238 	intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_AUDIO, awacs_intr, sc);
    239 	intr_establish(ca->ca_intr[1], IST_LEVEL, IPL_AUDIO, awacs_intr, sc);
    240 	intr_establish(ca->ca_intr[2], IST_LEVEL, IPL_AUDIO, awacs_intr, sc);
    241 
    242 	printf(": irq %d,%d,%d\n",
    243 		ca->ca_intr[0], ca->ca_intr[1], ca->ca_intr[2]);
    244 
    245 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
    246 		AWACS_RATE_44100;
    247 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    248 
    249 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
    250 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
    251 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
    252 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
    253 
    254 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
    255 	awacs_write_codec(sc, sc->sc_codecctl0);
    256 
    257 	/* Set initial volume[s] */
    258 	awacs_set_speaker_volume(sc, 80, 80);
    259 
    260 	/* Set loopback (for CD?) */
    261 	sc->sc_codecctl1 |= 0x440;
    262 	awacs_write_codec(sc, sc->sc_codecctl1);
    263 
    264 	sc->sc_output_mask = 1 << 0;
    265 
    266 	sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
    267 	sc->sc_codecctl1 |= AWACS_MUTE_HEADPHONE;
    268 	awacs_write_codec(sc, sc->sc_codecctl1);
    269 
    270 	/* Enable interrupts and looping mode. */
    271 	/* XXX ... */
    272 
    273 	audio_attach_mi(&awacs_hw_if, sc, &sc->sc_dev);
    274 }
    275 
    276 u_int
    277 awacs_read_reg(sc, reg)
    278 	struct awacs_softc *sc;
    279 	int reg;
    280 {
    281 	char *addr = sc->sc_reg;
    282 
    283 	return in32rb(addr + reg);
    284 }
    285 
    286 void
    287 awacs_write_reg(sc, reg, val)
    288 	struct awacs_softc *sc;
    289 	int reg, val;
    290 {
    291 	char *addr = sc->sc_reg;
    292 
    293 	out32rb(addr + reg, val);
    294 }
    295 
    296 void
    297 awacs_write_codec(sc, value)
    298 	struct awacs_softc *sc;
    299 	int value;
    300 {
    301 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    302 	while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    303 }
    304 
    305 int
    306 awacs_intr(v)
    307 	void *v;
    308 {
    309 	struct awacs_softc *sc = v;
    310 	struct dbdma_command *cmd = sc->sc_odmacmd;
    311 	int count = sc->sc_opages;
    312 	int status;
    313 
    314 	/* Fill used buffer(s). */
    315 	while (count-- > 0) {
    316 		/* if DBDMA_INT_ALWAYS */
    317 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    318 			status = in16rb(&cmd->d_status);
    319 			cmd->d_status = 0;
    320 			if (status)	/* status == 0x8400 */
    321 				if (sc->sc_ointr)
    322 					(*sc->sc_ointr)(sc->sc_oarg);
    323 		}
    324 		cmd++;
    325 	}
    326 
    327 	return 1;
    328 }
    329 
    330 int
    331 awacs_open(h, flags)
    332 	void *h;
    333 	int flags;
    334 {
    335 	return 0;
    336 }
    337 
    338 /*
    339  * Close function is called at splaudio().
    340  */
    341 void
    342 awacs_close(h)
    343 	void *h;
    344 {
    345 	struct awacs_softc *sc = h;
    346 
    347 	awacs_halt_output(sc);
    348 	awacs_halt_input(sc);
    349 
    350 	sc->sc_ointr = 0;
    351 	sc->sc_iintr = 0;
    352 }
    353 
    354 int
    355 awacs_query_encoding(h, ae)
    356 	void *h;
    357 	struct audio_encoding *ae;
    358 {
    359 	switch (ae->index) {
    360 	case 0:
    361 		strcpy(ae->name, AudioEslinear);
    362 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    363 		ae->precision = 16;
    364 		ae->flags = 0;
    365 		return 0;
    366 	case 1:
    367 		strcpy(ae->name, AudioEslinear_be);
    368 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    369 		ae->precision = 16;
    370 		ae->flags = 0;
    371 		return 0;
    372 	case 2:
    373 		strcpy(ae->name, AudioEslinear_le);
    374 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    375 		ae->precision = 16;
    376 		ae->flags = 0;
    377 		return 0;
    378 	case 3:
    379 		strcpy(ae->name, AudioEmulaw);
    380 		ae->encoding = AUDIO_ENCODING_ULAW;
    381 		ae->precision = 8;
    382 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    383 		return 0;
    384 	case 4:
    385 		strcpy(ae->name, AudioEalaw);
    386 		ae->encoding = AUDIO_ENCODING_ALAW;
    387 		ae->precision = 8;
    388 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    389 		return 0;
    390 	default:
    391 		return EINVAL;
    392 	}
    393 }
    394 
    395 static void
    396 mono16_to_stereo16(v, p, cc)
    397 	void *v;
    398 	u_char *p;
    399 	int cc;
    400 {
    401 	int x;
    402 	int16_t *src, *dst;
    403 
    404 	src = (void *)(p + cc);
    405 	dst = (void *)(p + cc * 2);
    406 	while (cc > 0) {
    407 		x = *--src;
    408 		*--dst = x;
    409 		*--dst = x;
    410 		cc -= 2;
    411 	}
    412 }
    413 
    414 int
    415 awacs_set_params(h, setmode, usemode, play, rec)
    416 	void *h;
    417 	int setmode, usemode;
    418 	struct audio_params *play, *rec;
    419 {
    420 	struct awacs_softc *sc = h;
    421 	struct audio_params *p;
    422 	int mode, rate;
    423 
    424 	/*
    425 	 * This device only has one clock, so make the sample rates match.
    426 	 */
    427 	if (play->sample_rate != rec->sample_rate &&
    428 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    429 		if (setmode == AUMODE_PLAY) {
    430 			rec->sample_rate = play->sample_rate;
    431 			setmode |= AUMODE_RECORD;
    432 		} else if (setmode == AUMODE_RECORD) {
    433 			play->sample_rate = rec->sample_rate;
    434 			setmode |= AUMODE_PLAY;
    435 		} else
    436 			return EINVAL;
    437 	}
    438 
    439 	for (mode = AUMODE_RECORD; mode != -1;
    440 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    441 		if ((setmode & mode) == 0)
    442 			continue;
    443 
    444 		p = mode == AUMODE_PLAY ? play : rec;
    445 
    446 		if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
    447 		    (p->precision != 8 && p->precision != 16) ||
    448 		    (p->channels != 1 && p->channels != 2))
    449 			return EINVAL;
    450 
    451 		p->factor = 1;
    452 		p->sw_code = 0;
    453 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    454 
    455 		switch (p->encoding) {
    456 
    457 		case AUDIO_ENCODING_SLINEAR_LE:
    458 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    459 		case AUDIO_ENCODING_SLINEAR_BE:
    460 			if (p->channels == 1) {
    461 				p->factor = 2;
    462 				p->sw_code = mono16_to_stereo16;
    463 				break;
    464 			}
    465 			if (p->precision != 16)
    466 				return EINVAL;
    467 				/* p->sw_code = change_sign8; */
    468 			break;
    469 
    470 		case AUDIO_ENCODING_ULINEAR_LE:
    471 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    472 		case AUDIO_ENCODING_ULINEAR_BE:
    473 			if (p->precision == 16)
    474 				p->sw_code = change_sign16_be;
    475 			else
    476 				return EINVAL;
    477 			break;
    478 
    479 		case AUDIO_ENCODING_ULAW:
    480 			if (mode == AUMODE_PLAY) {
    481 				p->factor = 2;
    482 				p->sw_code = mulaw_to_slinear16_be;
    483 			} else
    484 				p->sw_code = ulinear8_to_mulaw;
    485 			break;
    486 
    487 		case AUDIO_ENCODING_ALAW:
    488 			if (mode == AUMODE_PLAY) {
    489 				p->factor = 2;
    490 				p->sw_code = alaw_to_slinear16_be;
    491 			} else
    492 				p->sw_code = ulinear8_to_alaw;
    493 			break;
    494 
    495 		default:
    496 			return EINVAL;
    497 		}
    498 	}
    499 
    500 	/* Set the speed */
    501 	rate = p->sample_rate;
    502 
    503 	awacs_set_rate(sc, rate);
    504 
    505 	return 0;
    506 }
    507 
    508 int
    509 awacs_round_blocksize(h, size)
    510 	void *h;
    511 	int size;
    512 {
    513 	return size & ~PGOFSET;
    514 }
    515 
    516 int
    517 awacs_halt_output(h)
    518 	void *h;
    519 {
    520 	struct awacs_softc *sc = h;
    521 
    522 	dbdma_stop(sc->sc_odma);
    523 	dbdma_reset(sc->sc_odma);
    524 	return 0;
    525 }
    526 
    527 int
    528 awacs_halt_input(h)
    529 	void *h;
    530 {
    531 	struct awacs_softc *sc = h;
    532 
    533 	dbdma_stop(sc->sc_idma);
    534 	dbdma_reset(sc->sc_idma);
    535 	return 0;
    536 }
    537 
    538 int
    539 awacs_getdev(h, retp)
    540 	void *h;
    541 	struct audio_device *retp;
    542 {
    543 	*retp = awacs_device;
    544 	return 0;
    545 }
    546 
    547 enum {
    548 	AWACS_OUTPUT_SELECT,
    549 	AWACS_VOL_SPEAKER,
    550 	AWACS_VOL_HEADPHONE,
    551 	AWACS_OUTPUT_CLASS,
    552 	AWACS_MONITOR_CLASS,
    553 	AWACS_ENUM_LAST
    554 };
    555 
    556 int
    557 awacs_set_port(h, mc)
    558 	void *h;
    559 	mixer_ctrl_t *mc;
    560 {
    561 	struct awacs_softc *sc = h;
    562 	int l, r;
    563 
    564 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    565 
    566 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    567 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    568 
    569 	switch (mc->dev) {
    570 	case AWACS_OUTPUT_SELECT:
    571 		printf("output_select mask = 0x%x\n", mc->un.mask);
    572 		sc->sc_output_mask = mc->un.mask;
    573 		return 0;
    574 
    575 	case AWACS_VOL_SPEAKER:
    576 		awacs_set_speaker_volume(sc, l, r);
    577 		return 0;
    578 
    579 	case AWACS_VOL_HEADPHONE:
    580 		awacs_set_ext_volume(sc, l, r);
    581 		return 0;
    582 	}
    583 
    584 	return ENXIO;
    585 }
    586 
    587 int
    588 awacs_get_port(h, mc)
    589 	void *h;
    590 	mixer_ctrl_t *mc;
    591 {
    592 	struct awacs_softc *sc = h;
    593 	int vol, l, r;
    594 
    595 	DPRINTF("awacs_get_port dev = %d, type = %d\n", mc->dev, mc->type);
    596 
    597 	switch (mc->dev) {
    598 	case AWACS_OUTPUT_SELECT:
    599 		mc->un.mask = sc->sc_output_mask;
    600 		return 0;
    601 
    602 	case AWACS_VOL_SPEAKER:
    603 		vol = sc->sc_codecctl4;
    604 		l = (15 - ((vol & 0x3c0) >> 6)) * 16;
    605 		r = (15 - (vol & 0x0f)) * 16;
    606 		mc->un.mask = 1 << 0;
    607 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    608 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    609 		return 0;
    610 
    611 	case AWACS_VOL_HEADPHONE:
    612 		vol = sc->sc_codecctl2;
    613 		l = (15 - ((vol & 0x3c0) >> 6)) * 16;
    614 		r = (15 - (vol & 0x0f)) * 16;
    615 		mc->un.mask = 1 << 1;
    616 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    617 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    618 		return 0;
    619 
    620 	default:
    621 		return ENXIO;
    622 	}
    623 
    624 	return 0;
    625 }
    626 
    627 int
    628 awacs_query_devinfo(h, dip)
    629 	void *h;
    630 	mixer_devinfo_t *dip;
    631 {
    632 
    633 	DPRINTF("query_devinfo %d\n", dip->index);
    634 
    635 	switch (dip->index) {
    636 
    637 	case AWACS_OUTPUT_SELECT:
    638 		dip->mixer_class = AWACS_MONITOR_CLASS;
    639 		strcpy(dip->label.name, AudioNoutput);
    640 		dip->type = AUDIO_MIXER_SET;
    641 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    642 		dip->un.s.num_mem = 2;
    643 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    644 		dip->un.s.member[0].mask = 1 << 0;
    645 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    646 		dip->un.s.member[1].mask = 1 << 1;
    647 		return 0;
    648 
    649 	case AWACS_VOL_SPEAKER:
    650 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    651 		strcpy(dip->label.name, AudioNspeaker);
    652 		dip->type = AUDIO_MIXER_VALUE;
    653 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    654 		dip->un.v.num_channels = 2;
    655 		strcpy(dip->un.v.units.name, AudioNvolume);
    656 		return 0;
    657 
    658 	case AWACS_VOL_HEADPHONE:
    659 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    660 		strcpy(dip->label.name, AudioNheadphone);
    661 		dip->type = AUDIO_MIXER_VALUE;
    662 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    663 		dip->un.v.num_channels = 2;
    664 		strcpy(dip->un.v.units.name, AudioNvolume);
    665 		return 0;
    666 
    667 	case AWACS_MONITOR_CLASS:
    668 		dip->mixer_class = AWACS_MONITOR_CLASS;
    669 		strcpy(dip->label.name, AudioCmonitor);
    670 		dip->type = AUDIO_MIXER_CLASS;
    671 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    672 		return 0;
    673 
    674 	case AWACS_OUTPUT_CLASS:
    675 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    676 		strcpy(dip->label.name, AudioCoutputs);
    677 		dip->type = AUDIO_MIXER_CLASS;
    678 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    679 		return 0;
    680 	}
    681 
    682 	return ENXIO;
    683 }
    684 
    685 size_t
    686 awacs_round_buffersize(h, dir, size)
    687 	void *h;
    688 	int dir;
    689 	size_t size;
    690 {
    691 	if (size > 65536)
    692 		size = 65536;
    693 	return size;
    694 }
    695 
    696 paddr_t
    697 awacs_mappage(h, mem, off, prot)
    698 	void *h;
    699 	void *mem;
    700 	off_t off;
    701 	int prot;
    702 {
    703 	if (off < 0)
    704 		return -1;
    705 	return -1;	/* XXX */
    706 }
    707 
    708 int
    709 awacs_get_props(h)
    710 	void *h;
    711 {
    712 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
    713 }
    714 
    715 int
    716 awacs_trigger_output(h, start, end, bsize, intr, arg, param)
    717 	void *h;
    718 	void *start, *end;
    719 	int bsize;
    720 	void (*intr)(void *);
    721 	void *arg;
    722 	struct audio_params *param;
    723 {
    724 	struct awacs_softc *sc = h;
    725 	struct dbdma_command *cmd = sc->sc_odmacmd;
    726 	vaddr_t va;
    727 	int i, len, intmode;
    728 
    729 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
    730 
    731 	sc->sc_ointr = intr;
    732 	sc->sc_oarg = arg;
    733 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
    734 
    735 #ifdef DIAGNOSTIC
    736 	if (sc->sc_opages > 16)
    737 		panic("awacs_trigger_output");
    738 #endif
    739 
    740 	va = (vaddr_t)start;
    741 	len = 0;
    742 	for (i = sc->sc_opages; i > 0; i--) {
    743 		len += NBPG;
    744 		if (len < bsize)
    745 			intmode = DBDMA_INT_NEVER;
    746 		else {
    747 			len = 0;
    748 			intmode = DBDMA_INT_ALWAYS;
    749 		}
    750 
    751 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
    752 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    753 		va += NBPG;
    754 		cmd++;
    755 	}
    756 
    757 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
    758 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
    759 	dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
    760 
    761 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
    762 
    763 	return 0;
    764 }
    765 
    766 int
    767 awacs_trigger_input(h, start, end, bsize, intr, arg, param)
    768 	void *h;
    769 	void *start, *end;
    770 	int bsize;
    771 	void (*intr)(void *);
    772 	void *arg;
    773 	struct audio_params *param;
    774 {
    775 	printf("awacs_trigger_input called\n");
    776 
    777 	return 1;
    778 }
    779 
    780 void
    781 awacs_set_speaker_volume(sc, left, right)
    782 	struct awacs_softc *sc;
    783 	int left, right;
    784 {
    785 	int lval = 15 - (left  & 0xff) / 16;
    786 	int rval = 15 - (right & 0xff) / 16;
    787 
    788 	DPRINTF("speaker_volume %d %d\n", lval, rval);
    789 
    790 	sc->sc_codecctl4 &= ~0x3cf;
    791 	sc->sc_codecctl4 |= (lval << 6) | rval;
    792 	awacs_write_codec(sc, sc->sc_codecctl4);
    793 }
    794 
    795 void
    796 awacs_set_ext_volume(sc, left, right)
    797 	struct awacs_softc *sc;
    798 	int left, right;
    799 {
    800 	int lval = 15 - (left  & 0xff) / 16;
    801 	int rval = 15 - (right & 0xff) / 16;
    802 
    803 	DPRINTF("ext_volume %d %d\n", lval, rval);
    804 
    805 	sc->sc_codecctl2 &= ~0x3cf;
    806 	sc->sc_codecctl2 |= (lval << 6) | rval;
    807 	awacs_write_codec(sc, sc->sc_codecctl2);
    808 }
    809 
    810 int
    811 awacs_set_rate(sc, rate)
    812 	struct awacs_softc *sc;
    813 	int rate;
    814 {
    815 	int c;
    816 
    817 	switch (rate) {
    818 
    819 	case 44100:
    820 		c = AWACS_RATE_44100;
    821 		break;
    822 	case 29400:
    823 		c = AWACS_RATE_29400;
    824 		break;
    825 	case 22050:
    826 		c = AWACS_RATE_22050;
    827 		break;
    828 	case 17640:
    829 		c = AWACS_RATE_17640;
    830 		break;
    831 	case 14700:
    832 		c = AWACS_RATE_14700;
    833 		break;
    834 	case 11025:
    835 		c = AWACS_RATE_11025;
    836 		break;
    837 	case 8820:
    838 		c = AWACS_RATE_8820;
    839 		break;
    840 	case 7350:
    841 		c = AWACS_RATE_7350;
    842 		break;
    843 	default:
    844 		return -1;
    845 	}
    846 
    847 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
    848 	sc->sc_soundctl |= c;
    849 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    850 
    851 	return 0;
    852 }
    853