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