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