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