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