Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.40.6.1
      1  1.40.6.1  macallan /*	$NetBSD: awacs.c,v 1.40.6.1 2011/11/22 00:48:48 macallan Exp $	*/
      2       1.1    tsubai 
      3       1.1    tsubai /*-
      4       1.1    tsubai  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5       1.1    tsubai  *
      6       1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      7       1.1    tsubai  * modification, are permitted provided that the following conditions
      8       1.1    tsubai  * are met:
      9       1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     10       1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     11       1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     13       1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     14       1.1    tsubai  * 3. The name of the author may not be used to endorse or promote products
     15       1.1    tsubai  *    derived from this software without specific prior written permission.
     16       1.1    tsubai  *
     17       1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26       1.1    tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1    tsubai  */
     28      1.19     lukem 
     29      1.19     lukem #include <sys/cdefs.h>
     30  1.40.6.1  macallan __KERNEL_RCSID(0, "$NetBSD: awacs.c,v 1.40.6.1 2011/11/22 00:48:48 macallan Exp $");
     31       1.1    tsubai 
     32       1.1    tsubai #include <sys/param.h>
     33       1.1    tsubai #include <sys/audioio.h>
     34       1.1    tsubai #include <sys/device.h>
     35       1.1    tsubai #include <sys/systm.h>
     36      1.28  macallan #include <sys/kthread.h>
     37      1.28  macallan #include <sys/kernel.h>
     38  1.40.6.1  macallan #include <sys/mutex.h>
     39  1.40.6.1  macallan #include <sys/condvar.h>
     40       1.1    tsubai 
     41       1.1    tsubai #include <dev/auconv.h>
     42       1.1    tsubai #include <dev/audio_if.h>
     43       1.1    tsubai #include <dev/mulaw.h>
     44       1.1    tsubai 
     45      1.39  macallan #include <uvm/uvm_extern.h>
     46       1.1    tsubai #include <machine/autoconf.h>
     47       1.1    tsubai #include <machine/pio.h>
     48      1.10      matt 
     49      1.10      matt #include <dev/ofw/openfirm.h>
     50       1.1    tsubai #include <macppc/dev/dbdma.h>
     51       1.1    tsubai 
     52      1.25  macallan #include <dev/i2c/sgsmixvar.h>
     53      1.25  macallan #include "sgsmix.h"
     54      1.28  macallan #include "opt_awacs.h"
     55      1.25  macallan 
     56       1.1    tsubai #ifdef AWACS_DEBUG
     57       1.1    tsubai # define DPRINTF printf
     58       1.1    tsubai #else
     59       1.1    tsubai # define DPRINTF while (0) printf
     60       1.1    tsubai #endif
     61       1.1    tsubai 
     62       1.9    tsubai /* sc_flags values */
     63       1.9    tsubai #define AWACS_CAP_BSWAP		0x0001
     64       1.9    tsubai 
     65       1.1    tsubai struct awacs_softc {
     66      1.34  macallan 	device_t sc_dev;
     67       1.9    tsubai 	int sc_flags;
     68      1.40  macallan 	bus_space_tag_t sc_tag;
     69      1.40  macallan 	bus_space_handle_t	sc_regh;
     70      1.40  macallan 	bus_space_handle_t	sc_idmah;
     71      1.40  macallan 	bus_space_handle_t	sc_odmah;
     72      1.18       wiz 	void (*sc_ointr)(void *);	/* DMA completion intr handler */
     73       1.1    tsubai 	void *sc_oarg;			/* arg for sc_ointr() */
     74       1.1    tsubai 	int sc_opages;			/* # of output pages */
     75       1.1    tsubai 
     76      1.18       wiz 	void (*sc_iintr)(void *);	/* DMA completion intr handler */
     77       1.1    tsubai 	void *sc_iarg;			/* arg for sc_iintr() */
     78       1.1    tsubai 
     79      1.28  macallan 	uint32_t sc_record_source;	/* recording source mask */
     80      1.28  macallan 	uint32_t sc_output_mask;	/* output mask */
     81      1.28  macallan 	uint32_t sc_headphones_mask;	/* which reading of the gpio means */
     82      1.28  macallan 	uint32_t sc_headphones_in;	/* headphones are present */
     83       1.1    tsubai 
     84      1.25  macallan 	int sc_screamer;
     85      1.25  macallan 	int sc_have_perch;
     86      1.28  macallan 	int vol_l, vol_r;
     87      1.25  macallan 	int sc_bass, sc_treble;
     88      1.29        ad 	lwp_t *sc_thread;
     89  1.40.6.1  macallan 	kcondvar_t sc_event;
     90      1.28  macallan 	int sc_output_wanted;
     91      1.35  jmcneill 	int sc_need_parallel_output;
     92      1.25  macallan #if NSGSMIX > 0
     93      1.34  macallan 	device_t sc_sgsmix;
     94      1.25  macallan #endif
     95      1.25  macallan 
     96       1.1    tsubai 	u_int sc_codecctl0;
     97       1.1    tsubai 	u_int sc_codecctl1;
     98       1.1    tsubai 	u_int sc_codecctl2;
     99       1.1    tsubai 	u_int sc_codecctl4;
    100      1.25  macallan 	u_int sc_codecctl5;
    101      1.25  macallan 	u_int sc_codecctl6;
    102      1.25  macallan 	u_int sc_codecctl7;
    103       1.1    tsubai 	u_int sc_soundctl;
    104       1.1    tsubai 
    105       1.1    tsubai 	struct dbdma_regmap *sc_odma;
    106       1.1    tsubai 	struct dbdma_regmap *sc_idma;
    107       1.1    tsubai 	struct dbdma_command *sc_odmacmd;
    108       1.1    tsubai 	struct dbdma_command *sc_idmacmd;
    109      1.22      kent 
    110      1.22      kent #define AWACS_NFORMATS	2
    111      1.22      kent 	struct audio_format sc_formats[AWACS_NFORMATS];
    112  1.40.6.1  macallan 
    113  1.40.6.1  macallan 	kmutex_t sc_lock;
    114  1.40.6.1  macallan 	kmutex_t sc_intr_lock;
    115       1.1    tsubai };
    116       1.1    tsubai 
    117      1.34  macallan static int awacs_match(device_t, struct cfdata *, void *);
    118      1.34  macallan static void awacs_attach(device_t, device_t, void *);
    119      1.28  macallan static int awacs_intr(void *);
    120      1.28  macallan static int awacs_status_intr(void *);
    121      1.28  macallan 
    122      1.28  macallan static void awacs_close(void *);
    123      1.28  macallan static int awacs_query_encoding(void *, struct audio_encoding *);
    124      1.28  macallan static int awacs_set_params(void *, int, int, audio_params_t *, audio_params_t *,
    125      1.23      kent 		     stream_filter_list_t *, stream_filter_list_t *);
    126      1.28  macallan 
    127      1.28  macallan static int awacs_round_blocksize(void *, int, int, const audio_params_t *);
    128      1.28  macallan static int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
    129      1.23      kent 			 void *, const audio_params_t *);
    130      1.28  macallan static int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
    131      1.23      kent 			void *, const audio_params_t *);
    132      1.28  macallan static int awacs_halt_output(void *);
    133      1.28  macallan static int awacs_halt_input(void *);
    134      1.28  macallan static int awacs_getdev(void *, struct audio_device *);
    135      1.28  macallan static int awacs_set_port(void *, mixer_ctrl_t *);
    136      1.28  macallan static int awacs_get_port(void *, mixer_ctrl_t *);
    137      1.28  macallan static int awacs_query_devinfo(void *, mixer_devinfo_t *);
    138      1.28  macallan static size_t awacs_round_buffersize(void *, int, size_t);
    139      1.28  macallan static paddr_t awacs_mappage(void *, void *, off_t, int);
    140      1.28  macallan static int awacs_get_props(void *);
    141  1.40.6.1  macallan static void awacs_get_locks(void *, kmutex_t **, kmutex_t **);
    142       1.1    tsubai 
    143       1.1    tsubai static inline u_int awacs_read_reg(struct awacs_softc *, int);
    144       1.1    tsubai static inline void awacs_write_reg(struct awacs_softc *, int, int);
    145      1.28  macallan static void awacs_write_codec(struct awacs_softc *, int);
    146      1.28  macallan 
    147      1.34  macallan void awacs_set_volume(struct awacs_softc *, int, int);
    148      1.28  macallan static void awacs_set_speaker_volume(struct awacs_softc *, int, int);
    149      1.28  macallan static void awacs_set_ext_volume(struct awacs_softc *, int, int);
    150      1.28  macallan static void awacs_set_loopthrough_volume(struct awacs_softc *, int, int);
    151      1.28  macallan static int awacs_set_rate(struct awacs_softc *, const audio_params_t *);
    152      1.28  macallan static void awacs_select_output(struct awacs_softc *, int);
    153      1.28  macallan static int awacs_check_headphones(struct awacs_softc *);
    154      1.28  macallan static void awacs_thread(void *);
    155      1.28  macallan 
    156      1.25  macallan #if NSGSMIX > 0
    157      1.25  macallan static void awacs_set_bass(struct awacs_softc *, int);
    158      1.25  macallan static void awacs_set_treble(struct awacs_softc *, int);
    159      1.25  macallan #endif
    160      1.34  macallan static int awacs_setup_sgsmix(device_t);
    161       1.1    tsubai 
    162      1.34  macallan CFATTACH_DECL_NEW(awacs, sizeof(struct awacs_softc),
    163      1.14   thorpej     awacs_match, awacs_attach, NULL, NULL);
    164       1.1    tsubai 
    165      1.21      yamt const struct audio_hw_if awacs_hw_if = {
    166      1.22      kent 	NULL,			/* open */
    167       1.1    tsubai 	awacs_close,
    168       1.1    tsubai 	NULL,
    169       1.1    tsubai 	awacs_query_encoding,
    170       1.1    tsubai 	awacs_set_params,
    171       1.1    tsubai 	awacs_round_blocksize,
    172       1.1    tsubai 	NULL,
    173       1.1    tsubai 	NULL,
    174       1.1    tsubai 	NULL,
    175       1.1    tsubai 	NULL,
    176       1.1    tsubai 	NULL,
    177       1.1    tsubai 	awacs_halt_output,
    178       1.1    tsubai 	awacs_halt_input,
    179       1.1    tsubai 	NULL,
    180       1.1    tsubai 	awacs_getdev,
    181       1.1    tsubai 	NULL,
    182       1.1    tsubai 	awacs_set_port,
    183       1.1    tsubai 	awacs_get_port,
    184       1.1    tsubai 	awacs_query_devinfo,
    185       1.1    tsubai 	NULL,
    186       1.1    tsubai 	NULL,
    187       1.1    tsubai 	awacs_round_buffersize,
    188       1.1    tsubai 	awacs_mappage,
    189       1.1    tsubai 	awacs_get_props,
    190       1.1    tsubai 	awacs_trigger_output,
    191       1.1    tsubai 	awacs_trigger_input,
    192      1.12  augustss 	NULL,
    193  1.40.6.1  macallan 	NULL,
    194  1.40.6.1  macallan 	awacs_get_locks,
    195       1.1    tsubai };
    196       1.1    tsubai 
    197       1.1    tsubai struct audio_device awacs_device = {
    198       1.1    tsubai 	"AWACS",
    199       1.1    tsubai 	"",
    200       1.1    tsubai 	"awacs"
    201       1.1    tsubai };
    202       1.1    tsubai 
    203      1.22      kent #define AWACS_NFORMATS		2
    204      1.22      kent #define AWACS_FORMATS_LE	0
    205      1.22      kent static const struct audio_format awacs_formats[AWACS_NFORMATS] = {
    206      1.22      kent 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    207      1.34  macallan 	 2, AUFMT_STEREO, 8,
    208      1.34  macallan 	 {7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100}},
    209      1.22      kent 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    210      1.34  macallan 	 2, AUFMT_STEREO, 8,
    211      1.34  macallan 	 {7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100}},
    212      1.22      kent };
    213      1.22      kent 
    214       1.1    tsubai /* register offset */
    215       1.1    tsubai #define AWACS_SOUND_CTRL	0x00
    216       1.1    tsubai #define AWACS_CODEC_CTRL	0x10
    217       1.1    tsubai #define AWACS_CODEC_STATUS	0x20
    218       1.1    tsubai #define AWACS_CLIP_COUNT	0x30
    219       1.1    tsubai #define AWACS_BYTE_SWAP		0x40
    220       1.1    tsubai 
    221       1.1    tsubai /* sound control */
    222       1.1    tsubai #define AWACS_INPUT_SUBFRAME0	0x00000001
    223       1.1    tsubai #define AWACS_INPUT_SUBFRAME1	0x00000002
    224       1.1    tsubai #define AWACS_INPUT_SUBFRAME2	0x00000004
    225       1.1    tsubai #define AWACS_INPUT_SUBFRAME3	0x00000008
    226       1.1    tsubai 
    227       1.1    tsubai #define AWACS_OUTPUT_SUBFRAME0	0x00000010
    228       1.1    tsubai #define AWACS_OUTPUT_SUBFRAME1	0x00000020
    229       1.1    tsubai #define AWACS_OUTPUT_SUBFRAME2	0x00000040
    230       1.1    tsubai #define AWACS_OUTPUT_SUBFRAME3	0x00000080
    231       1.1    tsubai 
    232       1.1    tsubai #define AWACS_RATE_44100	0x00000000
    233       1.1    tsubai #define AWACS_RATE_29400	0x00000100
    234       1.1    tsubai #define AWACS_RATE_22050	0x00000200
    235       1.1    tsubai #define AWACS_RATE_17640	0x00000300
    236       1.1    tsubai #define AWACS_RATE_14700	0x00000400
    237       1.1    tsubai #define AWACS_RATE_11025	0x00000500
    238       1.1    tsubai #define AWACS_RATE_8820		0x00000600
    239       1.1    tsubai #define AWACS_RATE_7350		0x00000700
    240       1.1    tsubai #define AWACS_RATE_MASK		0x00000700
    241       1.1    tsubai 
    242      1.28  macallan #define AWACS_ERROR		0x00000800
    243      1.28  macallan #define AWACS_PORTCHG		0x00001000
    244      1.28  macallan #define AWACS_INTR_ERROR	0x00002000	/* interrupt on error */
    245      1.28  macallan #define AWACS_INTR_PORTCHG	0x00004000	/* interrupt on port change */
    246      1.28  macallan 
    247      1.28  macallan #define AWACS_STATUS_SUBFRAME	0x00018000	/* mask */
    248      1.27  jmcneill 
    249       1.1    tsubai /* codec control */
    250       1.1    tsubai #define AWACS_CODEC_ADDR0	0x00000000
    251       1.1    tsubai #define AWACS_CODEC_ADDR1	0x00001000
    252       1.1    tsubai #define AWACS_CODEC_ADDR2	0x00002000
    253       1.1    tsubai #define AWACS_CODEC_ADDR4	0x00004000
    254      1.25  macallan #define AWACS_CODEC_ADDR5	0x00005000
    255      1.25  macallan #define AWACS_CODEC_ADDR6	0x00006000
    256      1.25  macallan #define AWACS_CODEC_ADDR7	0x00007000
    257       1.1    tsubai #define AWACS_CODEC_EMSEL0	0x00000000
    258       1.1    tsubai #define AWACS_CODEC_EMSEL1	0x00400000
    259       1.1    tsubai #define AWACS_CODEC_EMSEL2	0x00800000
    260       1.1    tsubai #define AWACS_CODEC_EMSEL4	0x00c00000
    261       1.1    tsubai #define AWACS_CODEC_BUSY	0x01000000
    262       1.1    tsubai 
    263       1.1    tsubai /* cc0 */
    264       1.1    tsubai #define AWACS_DEFAULT_CD_GAIN	0x000000bb
    265       1.1    tsubai #define AWACS_INPUT_CD		0x00000200
    266       1.4       wiz #define AWACS_INPUT_LINE	0x00000400
    267       1.4       wiz #define AWACS_INPUT_MICROPHONE	0x00000800
    268       1.1    tsubai #define AWACS_INPUT_MASK	0x00000e00
    269       1.1    tsubai 
    270       1.1    tsubai /* cc1 */
    271      1.25  macallan #define AWACS_LOOP_THROUGH	0x00000040
    272       1.1    tsubai #define AWACS_MUTE_SPEAKER	0x00000080
    273       1.1    tsubai #define AWACS_MUTE_HEADPHONE	0x00000200
    274      1.25  macallan #define AWACS_PARALLEL_OUTPUT	0x00000c00
    275       1.1    tsubai 
    276      1.25  macallan /* output */
    277      1.25  macallan #define OUTPUT_SPEAKER		1
    278      1.25  macallan #define OUTPUT_HEADPHONES	2
    279      1.28  macallan 
    280      1.28  macallan /* codec status */
    281      1.28  macallan 
    282      1.28  macallan static const char *screamer[] = {"screamer", NULL};
    283      1.28  macallan 
    284      1.28  macallan /*
    285      1.28  macallan  * list machines that have the headphone detect GPIO reversed here.
    286      1.28  macallan  * so far the only known case is the PowerBook 3400c and similar machines
    287      1.28  macallan  */
    288      1.28  macallan static const char *detect_reversed[] = {"AAPL,3400/2400",
    289      1.28  macallan 					"AAPL,3500",
    290      1.28  macallan 					NULL};
    291      1.28  macallan 
    292      1.32  macallan static const char *use_gpio4[] = {	"PowerMac3,3",
    293      1.32  macallan 					NULL};
    294      1.32  macallan 
    295      1.35  jmcneill /*
    296      1.35  jmcneill  * list of machines that do not require AWACS_PARALLEL_OUTPUT
    297      1.35  jmcneill  */
    298      1.35  jmcneill static const char *no_parallel_output[] = {	"PowerBook3,1",
    299      1.35  jmcneill 						NULL};
    300      1.35  jmcneill 
    301      1.28  macallan static int
    302      1.34  macallan awacs_match(device_t parent, struct cfdata *match, void *aux)
    303       1.1    tsubai {
    304      1.23      kent 	struct confargs *ca;
    305       1.1    tsubai 
    306      1.23      kent 	ca = aux;
    307      1.16     soren 
    308      1.32  macallan 	if (strcmp(ca->ca_name, "awacs") == 0 ||
    309      1.32  macallan 	    strcmp(ca->ca_name, "davbus") == 0)
    310      1.32  macallan 		return 100;
    311       1.1    tsubai 
    312       1.1    tsubai 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    313       1.2    tsubai 		return 0;
    314       1.2    tsubai 
    315      1.32  macallan 	if (strcmp(ca->ca_name, "i2s") == 0)
    316      1.32  macallan 		return 1;
    317      1.32  macallan 
    318      1.32  macallan 	return 0;
    319       1.1    tsubai }
    320       1.1    tsubai 
    321      1.28  macallan static void
    322      1.34  macallan awacs_attach(device_t parent, device_t self, void *aux)
    323       1.1    tsubai {
    324      1.23      kent 	struct awacs_softc *sc;
    325      1.40  macallan 	struct confargs *ca = aux;
    326       1.9    tsubai 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    327      1.25  macallan 	int len = -1, perch;
    328      1.28  macallan 	int root_node;
    329      1.28  macallan 	char compat[256];
    330      1.23      kent 
    331      1.34  macallan 	sc = device_private(self);
    332      1.34  macallan 	sc->sc_dev = self;
    333      1.40  macallan 	sc->sc_tag = ca->ca_tag;
    334       1.1    tsubai 
    335      1.40  macallan 	if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[0],
    336      1.40  macallan 	    ca->ca_reg[1], 0, &sc->sc_regh) != 0)
    337      1.40  macallan 		printf("couldn't map codec registers\n");
    338      1.40  macallan 	if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[2],
    339      1.40  macallan 	    ca->ca_reg[3], BUS_SPACE_MAP_LINEAR, &sc->sc_odmah) != 0)
    340      1.40  macallan 		printf("couldn't map DMA out registers\n");
    341      1.40  macallan 	if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[4],
    342      1.40  macallan 	    ca->ca_reg[5], BUS_SPACE_MAP_LINEAR, &sc->sc_idmah) != 0)
    343      1.40  macallan 		printf("couldn't map DMA in registers\n");
    344       1.1    tsubai 
    345      1.40  macallan 	sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
    346      1.40  macallan 	sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah);
    347       1.1    tsubai 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    348       1.1    tsubai 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    349       1.1    tsubai 
    350      1.16     soren 	if (strcmp(ca->ca_name, "i2s") == 0) {
    351      1.16     soren 		int node, intr[6];
    352      1.16     soren 
    353      1.16     soren 		node = OF_child(ca->ca_node);
    354      1.20   thorpej 		if (node == 0) {
    355      1.16     soren 			printf("no i2s-a child\n");
    356      1.16     soren 			return;
    357      1.16     soren 		}
    358      1.16     soren 		if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
    359      1.16     soren 			printf("no interrupt property\n");
    360      1.16     soren 			return;
    361      1.16     soren 		}
    362      1.16     soren 
    363      1.16     soren 		cirq = intr[0];
    364      1.16     soren 		oirq = intr[2];
    365      1.16     soren 		iirq = intr[4];
    366      1.16     soren 		cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    367      1.16     soren 		oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    368      1.16     soren 		iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    369      1.16     soren 	} else if (ca->ca_nintr == 24) {
    370       1.9    tsubai 		cirq = ca->ca_intr[0];
    371       1.9    tsubai 		oirq = ca->ca_intr[2];
    372       1.9    tsubai 		iirq = ca->ca_intr[4];
    373       1.9    tsubai 		cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
    374       1.9    tsubai 		oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
    375       1.9    tsubai 		iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
    376       1.9    tsubai 	} else {
    377       1.9    tsubai 		cirq = ca->ca_intr[0];
    378       1.9    tsubai 		oirq = ca->ca_intr[1];
    379       1.9    tsubai 		iirq = ca->ca_intr[2];
    380      1.31   garbled 		cirq_type = oirq_type = iirq_type = IST_EDGE;
    381       1.9    tsubai 	}
    382      1.16     soren 
    383      1.28  macallan 	intr_establish(cirq, cirq_type, IPL_BIO, awacs_status_intr, sc);
    384  1.40.6.1  macallan 	intr_establish(oirq, oirq_type, IPL_SCHED, awacs_intr, sc);
    385  1.40.6.1  macallan 	intr_establish(iirq, iirq_type, IPL_SCHED, awacs_intr, sc);
    386  1.40.6.1  macallan 
    387  1.40.6.1  macallan 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    388  1.40.6.1  macallan 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    389  1.40.6.1  macallan 
    390  1.40.6.1  macallan 	cv_init(&sc->sc_event, "awacs_wait");
    391      1.25  macallan 
    392      1.25  macallan 	/* check if the chip is a screamer */
    393      1.28  macallan 	sc->sc_screamer = (of_compatible(ca->ca_node, screamer) != -1);
    394      1.28  macallan 	if (!sc->sc_screamer) {
    395      1.28  macallan 		/* look for 'sound' child node */
    396      1.28  macallan 		int sound_node;
    397      1.28  macallan 
    398      1.28  macallan 		sound_node = OF_child(ca->ca_node);
    399      1.28  macallan 		while ((sound_node != 0) && (!sc->sc_screamer)) {
    400      1.28  macallan 
    401      1.28  macallan 			sc->sc_screamer =
    402      1.28  macallan 			    (of_compatible(sound_node, screamer) != -1);
    403      1.28  macallan 			sound_node = OF_peer(sound_node);
    404      1.25  macallan 		}
    405      1.25  macallan 	}
    406      1.28  macallan 
    407      1.28  macallan 	if (sc->sc_screamer) {
    408      1.28  macallan 		printf(" Screamer");
    409      1.25  macallan 	}
    410      1.25  macallan 
    411      1.28  macallan 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
    412      1.28  macallan 
    413      1.28  macallan 	sc->vol_l = 0;
    414      1.28  macallan 	sc->vol_r = 0;
    415      1.25  macallan 
    416      1.22      kent 	memcpy(&sc->sc_formats, awacs_formats, sizeof(awacs_formats));
    417      1.28  macallan 
    418       1.9    tsubai 	/* XXX Uni-North based models don't have byteswap capability. */
    419      1.22      kent 	if (OF_finddevice("/uni-n") == -1) {
    420      1.28  macallan 
    421       1.9    tsubai 		sc->sc_flags |= AWACS_CAP_BSWAP;
    422      1.22      kent 	} else {
    423      1.28  macallan 
    424      1.22      kent 		AUFMT_INVALIDATE(&sc->sc_formats[AWACS_FORMATS_LE]);
    425      1.22      kent 	}
    426       1.1    tsubai 
    427       1.1    tsubai 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
    428      1.28  macallan 		AWACS_RATE_44100 | AWACS_INTR_PORTCHG;
    429       1.1    tsubai 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    430       1.1    tsubai 
    431       1.1    tsubai 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
    432       1.1    tsubai 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
    433       1.1    tsubai 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
    434       1.1    tsubai 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
    435      1.25  macallan 	sc->sc_codecctl5 = AWACS_CODEC_ADDR5 | AWACS_CODEC_EMSEL0;
    436      1.25  macallan 	sc->sc_codecctl6 = AWACS_CODEC_ADDR6 | AWACS_CODEC_EMSEL0;
    437      1.25  macallan 	sc->sc_codecctl7 = AWACS_CODEC_ADDR7 | AWACS_CODEC_EMSEL0;
    438       1.1    tsubai 
    439       1.1    tsubai 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
    440       1.1    tsubai 	awacs_write_codec(sc, sc->sc_codecctl0);
    441       1.1    tsubai 
    442      1.28  macallan 	/* Set loopthrough for external mixer on beige G3 */
    443      1.35  jmcneill 	sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
    444      1.25  macallan 
    445      1.34  macallan         printf("%s: ", device_xname(sc->sc_dev));
    446      1.28  macallan 
    447      1.30  macallan 	/*
    448      1.30  macallan 	 * all(?) awacs have GPIOs to detect if there's something plugged into
    449      1.30  macallan 	 * the headphone jack. The other GPIOs are either used for other jacks
    450      1.30  macallan 	 * ( the PB3400c's microphone jack for instance ) or unused.
    451      1.30  macallan 	 * The problem is that there are at least three different ways how
    452      1.30  macallan 	 * those GPIOs are wired to the actual jacks.
    453      1.30  macallan 	 * For now we bother only with headphone detection
    454      1.30  macallan 	 */
    455      1.30  macallan 	perch = OF_finddevice("/perch");
    456      1.28  macallan 	root_node = OF_finddevice("/");
    457      1.28  macallan 	if (of_compatible(root_node, detect_reversed) != -1) {
    458      1.28  macallan 
    459      1.28  macallan 		/* 0x02 is for the microphone jack, high active */
    460      1.28  macallan 		/*
    461      1.28  macallan 		 * for some reason the gpio for the headphones jack is low
    462      1.28  macallan 		 * active on the PB3400 and similar machines
    463      1.28  macallan 		 */
    464      1.28  macallan 		sc->sc_headphones_mask = 0x8;
    465      1.28  macallan 		sc->sc_headphones_in = 0x0;
    466      1.32  macallan 	} else if ((perch != -1) ||
    467      1.32  macallan 	    (of_compatible(root_node, use_gpio4) != -1)) {
    468      1.28  macallan 		/*
    469      1.30  macallan 		 * this is for the beige G3's 'personality card' which uses
    470      1.30  macallan 		 * yet another wiring of the headphone detect GPIOs
    471      1.32  macallan 		 * some G4s use it as well
    472      1.28  macallan 		 */
    473      1.28  macallan 		sc->sc_headphones_mask = 0x04;
    474      1.28  macallan 		sc->sc_headphones_in = 0x04;
    475      1.28  macallan 	} else {
    476      1.30  macallan 		/* while on most machines it's high active as well */
    477      1.28  macallan 		sc->sc_headphones_mask = 0x8;
    478      1.28  macallan 		sc->sc_headphones_in = 0x8;
    479      1.28  macallan 	}
    480      1.35  jmcneill 
    481      1.35  jmcneill 	if (of_compatible(root_node, no_parallel_output) != -1)
    482      1.35  jmcneill 		sc->sc_need_parallel_output = 0;
    483      1.35  jmcneill 	else {
    484      1.35  jmcneill 		sc->sc_need_parallel_output = 1;
    485      1.35  jmcneill 		sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
    486      1.35  jmcneill 	}
    487      1.40  macallan 
    488      1.28  macallan 	if (awacs_check_headphones(sc)) {
    489      1.28  macallan 
    490      1.28  macallan                 /* default output to headphones */
    491      1.28  macallan                 printf("headphones\n");
    492      1.25  macallan                 sc->sc_output_mask = OUTPUT_HEADPHONES;
    493      1.25  macallan         } else {
    494      1.28  macallan 
    495      1.25  macallan                 /* default output to speakers */
    496      1.28  macallan                 printf("speaker\n");
    497      1.25  macallan                 sc->sc_output_mask = OUTPUT_SPEAKER;
    498      1.25  macallan         }
    499      1.28  macallan 	sc->sc_output_wanted = sc->sc_output_mask;
    500      1.28  macallan 	awacs_select_output(sc, sc->sc_output_mask);
    501       1.1    tsubai 
    502      1.25  macallan 	delay(100);
    503      1.25  macallan 	if (sc->sc_screamer) {
    504      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl6);
    505      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl5);
    506      1.25  macallan 		delay(2);
    507      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl1);
    508      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl7);
    509      1.25  macallan 	}
    510       1.1    tsubai 
    511       1.4       wiz 	/* default input from CD */
    512       1.4       wiz 	sc->sc_record_source = 1 << 0;
    513       1.4       wiz 	sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    514       1.4       wiz 	sc->sc_codecctl0 |= AWACS_INPUT_CD;
    515       1.4       wiz 	awacs_write_codec(sc, sc->sc_codecctl0);
    516       1.4       wiz 
    517       1.1    tsubai 	/* Enable interrupts and looping mode. */
    518       1.1    tsubai 	/* XXX ... */
    519       1.1    tsubai 
    520      1.35  jmcneill 	sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
    521      1.35  jmcneill 	if (sc->sc_need_parallel_output)
    522      1.35  jmcneill 		sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
    523      1.25  macallan 	awacs_write_codec(sc, sc->sc_codecctl1);
    524      1.25  macallan 
    525      1.25  macallan #if NSGSMIX > 0
    526      1.25  macallan 	sc->sc_sgsmix = NULL;
    527      1.25  macallan #endif
    528      1.25  macallan 	sc->sc_have_perch = 0;
    529      1.30  macallan 	if (perch != -1) {
    530      1.25  macallan 
    531      1.25  macallan 		len = OF_getprop(perch, "compatible", compat, 255);
    532      1.25  macallan 		if (len > 0) {
    533      1.25  macallan 			printf("%s: found '%s' personality card\n",
    534      1.34  macallan 			    device_xname(sc->sc_dev), compat);
    535      1.25  macallan 			sc->sc_have_perch = 1;
    536      1.34  macallan 			config_finalize_register(sc->sc_dev,
    537      1.33  macallan 			    awacs_setup_sgsmix);
    538      1.25  macallan 		}
    539      1.25  macallan 	}
    540      1.25  macallan 
    541      1.25  macallan 	/* Set initial volume[s] */
    542      1.28  macallan 	awacs_set_volume(sc, 144, 144);
    543      1.33  macallan 	awacs_set_loopthrough_volume(sc, 0, 0);
    544      1.25  macallan 
    545      1.34  macallan 	audio_attach_mi(&awacs_hw_if, sc, sc->sc_dev);
    546      1.28  macallan 
    547      1.29        ad 	if (kthread_create(PRI_NONE, 0, NULL, awacs_thread, sc,
    548      1.29        ad 	    &sc->sc_thread, "%s", "awacs") != 0) {
    549      1.29        ad 		printf("awacs: unable to create event kthread");
    550      1.29        ad 	}
    551      1.37  macallan 	pmf_device_register(sc->sc_dev, NULL, NULL);
    552       1.1    tsubai }
    553       1.1    tsubai 
    554      1.25  macallan static int
    555      1.34  macallan awacs_setup_sgsmix(device_t cookie)
    556      1.25  macallan {
    557      1.34  macallan 	struct awacs_softc *sc = device_private(cookie);
    558      1.25  macallan #if NSGSMIX > 0
    559      1.34  macallan 	device_t dv;
    560      1.36    dyoung 	deviter_t di;
    561      1.25  macallan #endif
    562      1.25  macallan 
    563      1.25  macallan 	if (!sc->sc_have_perch)
    564      1.25  macallan 		return 0;
    565      1.25  macallan #if NSGSMIX > 0
    566      1.25  macallan 	/* look for sgsmix */
    567      1.36    dyoung 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    568      1.36    dyoung 	     dv != NULL;
    569      1.36    dyoung 	     dv = deviter_next(&di)) {
    570      1.25  macallan 		if (device_is_a(dv, "sgsmix")) {
    571      1.25  macallan 			sc->sc_sgsmix = dv;
    572      1.25  macallan 			break;
    573      1.25  macallan 		}
    574      1.25  macallan 	}
    575      1.36    dyoung 	deviter_release(&di);
    576      1.25  macallan 	if (sc->sc_sgsmix == NULL)
    577      1.25  macallan 		return 0;
    578      1.25  macallan 
    579      1.34  macallan 	printf("%s: using %s\n", device_xname(sc->sc_dev),
    580      1.34  macallan 	    device_xname(sc->sc_sgsmix));
    581      1.25  macallan 
    582      1.40  macallan 	sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
    583      1.40  macallan 	awacs_write_codec(sc, sc->sc_codecctl1);
    584      1.40  macallan 
    585  1.40.6.1  macallan 	mutex_enter(&sc->sc_intr_lock);
    586      1.25  macallan 	awacs_select_output(sc, sc->sc_output_mask);
    587      1.28  macallan 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    588      1.25  macallan 	awacs_set_bass(sc, 128);
    589      1.28  macallan 	awacs_set_treble(sc, 128);
    590  1.40.6.1  macallan 	cv_signal(&sc->sc_event);
    591  1.40.6.1  macallan 	mutex_exit(&sc->sc_intr_lock);
    592      1.25  macallan #endif
    593      1.25  macallan 	return 0;
    594      1.25  macallan }
    595      1.25  macallan 
    596      1.25  macallan 
    597      1.11    simonb static inline u_int
    598      1.23      kent awacs_read_reg(struct awacs_softc *sc, int reg)
    599       1.1    tsubai {
    600       1.1    tsubai 
    601      1.40  macallan 	return bus_space_read_4(sc->sc_tag, sc->sc_regh, reg);
    602       1.1    tsubai }
    603       1.1    tsubai 
    604      1.11    simonb static inline void
    605      1.23      kent awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    606       1.1    tsubai {
    607       1.1    tsubai 
    608      1.40  macallan 	bus_space_write_4(sc->sc_tag, sc->sc_regh, reg, val);
    609       1.1    tsubai }
    610       1.1    tsubai 
    611      1.28  macallan static void
    612      1.23      kent awacs_write_codec(struct awacs_softc *sc, int value)
    613       1.1    tsubai {
    614      1.23      kent 
    615      1.25  macallan 	do {
    616      1.25  macallan 		delay(100);
    617      1.25  macallan 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    618      1.25  macallan 
    619       1.1    tsubai 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    620      1.25  macallan 
    621      1.25  macallan 	do {
    622      1.25  macallan 		delay(100);
    623      1.25  macallan 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    624       1.1    tsubai }
    625       1.1    tsubai 
    626      1.28  macallan static int
    627      1.23      kent awacs_intr(void *v)
    628       1.1    tsubai {
    629      1.23      kent 	struct awacs_softc *sc;
    630      1.23      kent 	struct dbdma_command *cmd;
    631      1.23      kent 	int count;
    632       1.1    tsubai 	int status;
    633       1.1    tsubai 
    634      1.23      kent 	sc = v;
    635  1.40.6.1  macallan 	mutex_spin_enter(&sc->sc_intr_lock);
    636      1.23      kent 	cmd = sc->sc_odmacmd;
    637      1.23      kent 	count = sc->sc_opages;
    638       1.1    tsubai 	/* Fill used buffer(s). */
    639       1.1    tsubai 	while (count-- > 0) {
    640       1.1    tsubai 		/* if DBDMA_INT_ALWAYS */
    641       1.1    tsubai 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    642       1.1    tsubai 			status = in16rb(&cmd->d_status);
    643       1.1    tsubai 			cmd->d_status = 0;
    644       1.1    tsubai 			if (status)	/* status == 0x8400 */
    645       1.1    tsubai 				if (sc->sc_ointr)
    646       1.1    tsubai 					(*sc->sc_ointr)(sc->sc_oarg);
    647       1.1    tsubai 		}
    648       1.1    tsubai 		cmd++;
    649       1.1    tsubai 	}
    650  1.40.6.1  macallan 	mutex_spin_exit(&sc->sc_intr_lock);
    651       1.1    tsubai 
    652       1.1    tsubai 	return 1;
    653       1.1    tsubai }
    654       1.1    tsubai 
    655       1.1    tsubai /*
    656       1.1    tsubai  * Close function is called at splaudio().
    657       1.1    tsubai  */
    658      1.28  macallan static void
    659      1.23      kent awacs_close(void *h)
    660       1.1    tsubai {
    661      1.23      kent 	struct awacs_softc *sc;
    662       1.1    tsubai 
    663      1.23      kent 	sc = h;
    664       1.1    tsubai 	awacs_halt_output(sc);
    665       1.1    tsubai 	awacs_halt_input(sc);
    666       1.1    tsubai 
    667       1.1    tsubai 	sc->sc_ointr = 0;
    668       1.1    tsubai 	sc->sc_iintr = 0;
    669       1.1    tsubai }
    670       1.1    tsubai 
    671      1.28  macallan static int
    672      1.23      kent awacs_query_encoding(void *h, struct audio_encoding *ae)
    673       1.1    tsubai {
    674      1.23      kent 	struct awacs_softc *sc;
    675       1.9    tsubai 
    676      1.23      kent 	sc = h;
    677       1.9    tsubai 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    678       1.9    tsubai 
    679       1.1    tsubai 	switch (ae->index) {
    680       1.1    tsubai 	case 0:
    681       1.1    tsubai 		strcpy(ae->name, AudioEslinear);
    682       1.1    tsubai 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    683       1.1    tsubai 		ae->precision = 16;
    684       1.1    tsubai 		ae->flags = 0;
    685       1.1    tsubai 		return 0;
    686       1.1    tsubai 	case 1:
    687       1.1    tsubai 		strcpy(ae->name, AudioEslinear_be);
    688       1.1    tsubai 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    689       1.1    tsubai 		ae->precision = 16;
    690       1.1    tsubai 		ae->flags = 0;
    691       1.1    tsubai 		return 0;
    692       1.1    tsubai 	case 2:
    693       1.1    tsubai 		strcpy(ae->name, AudioEslinear_le);
    694       1.1    tsubai 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    695       1.1    tsubai 		ae->precision = 16;
    696       1.9    tsubai 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    697       1.9    tsubai 			ae->flags = 0;
    698       1.1    tsubai 		return 0;
    699       1.1    tsubai 	case 3:
    700       1.6    tsubai 		strcpy(ae->name, AudioEulinear_be);
    701       1.6    tsubai 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    702       1.6    tsubai 		ae->precision = 16;
    703       1.6    tsubai 		return 0;
    704       1.6    tsubai 	case 4:
    705       1.6    tsubai 		strcpy(ae->name, AudioEulinear_le);
    706       1.6    tsubai 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    707       1.6    tsubai 		ae->precision = 16;
    708       1.6    tsubai 		return 0;
    709       1.6    tsubai 	case 5:
    710       1.1    tsubai 		strcpy(ae->name, AudioEmulaw);
    711       1.1    tsubai 		ae->encoding = AUDIO_ENCODING_ULAW;
    712       1.1    tsubai 		ae->precision = 8;
    713       1.1    tsubai 		return 0;
    714       1.6    tsubai 	case 6:
    715       1.1    tsubai 		strcpy(ae->name, AudioEalaw);
    716       1.1    tsubai 		ae->encoding = AUDIO_ENCODING_ALAW;
    717       1.1    tsubai 		ae->precision = 8;
    718       1.1    tsubai 		return 0;
    719       1.1    tsubai 	default:
    720       1.1    tsubai 		return EINVAL;
    721       1.1    tsubai 	}
    722       1.1    tsubai }
    723       1.1    tsubai 
    724      1.28  macallan static int
    725      1.23      kent awacs_set_params(void *h, int setmode, int usemode,
    726      1.23      kent 		 audio_params_t *play, audio_params_t *rec,
    727      1.23      kent 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    728       1.1    tsubai {
    729      1.23      kent 	struct awacs_softc *sc;
    730      1.23      kent 	audio_params_t *p;
    731      1.22      kent 	stream_filter_list_t *fil;
    732      1.22      kent 	int mode, i;
    733       1.1    tsubai 
    734      1.23      kent 	sc = h;
    735      1.23      kent 	p = NULL;
    736       1.1    tsubai 	/*
    737       1.1    tsubai 	 * This device only has one clock, so make the sample rates match.
    738       1.1    tsubai 	 */
    739       1.1    tsubai 	if (play->sample_rate != rec->sample_rate &&
    740       1.1    tsubai 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    741       1.1    tsubai 		if (setmode == AUMODE_PLAY) {
    742       1.1    tsubai 			rec->sample_rate = play->sample_rate;
    743       1.1    tsubai 			setmode |= AUMODE_RECORD;
    744       1.1    tsubai 		} else if (setmode == AUMODE_RECORD) {
    745       1.1    tsubai 			play->sample_rate = rec->sample_rate;
    746       1.1    tsubai 			setmode |= AUMODE_PLAY;
    747       1.1    tsubai 		} else
    748       1.1    tsubai 			return EINVAL;
    749       1.1    tsubai 	}
    750       1.1    tsubai 
    751       1.1    tsubai 	for (mode = AUMODE_RECORD; mode != -1;
    752       1.1    tsubai 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    753       1.1    tsubai 		if ((setmode & mode) == 0)
    754       1.1    tsubai 			continue;
    755       1.1    tsubai 
    756       1.1    tsubai 		p = mode == AUMODE_PLAY ? play : rec;
    757      1.22      kent 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    758      1.22      kent 		switch (p->sample_rate) {
    759      1.22      kent 		case 48000:	/* aurateconv */
    760      1.22      kent 		case 44100:
    761      1.22      kent 		case 29400:
    762      1.22      kent 		case 22050:
    763      1.22      kent 		case 17640:
    764      1.22      kent 		case 14700:
    765      1.22      kent 		case 11025:
    766      1.22      kent 		case 8820:
    767      1.22      kent 		case 8000:	/* aurateconv */
    768      1.22      kent 		case 7350:
    769      1.22      kent 			break;
    770      1.22      kent 		default:
    771       1.1    tsubai 			return EINVAL;
    772      1.22      kent 		}
    773       1.1    tsubai 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    774      1.22      kent 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    775      1.26   thorpej 					 mode, p, true, fil);
    776      1.22      kent 		if (i < 0)
    777       1.9    tsubai 			return EINVAL;
    778      1.22      kent 		if (i == AWACS_FORMATS_LE)
    779      1.22      kent 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    780      1.22      kent 		if (fil->req_size > 0)
    781      1.22      kent 			p = &fil->filters[0].param;
    782      1.22      kent 		if (awacs_set_rate(sc, p))
    783       1.1    tsubai 			return EINVAL;
    784       1.1    tsubai 	}
    785       1.1    tsubai 	return 0;
    786       1.1    tsubai }
    787       1.1    tsubai 
    788      1.28  macallan static int
    789      1.23      kent awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    790       1.1    tsubai {
    791      1.23      kent 
    792      1.17   thorpej 	if (size < PAGE_SIZE)
    793      1.17   thorpej 		size = PAGE_SIZE;
    794       1.1    tsubai 	return size & ~PGOFSET;
    795       1.1    tsubai }
    796       1.1    tsubai 
    797      1.28  macallan static int
    798      1.23      kent awacs_halt_output(void *h)
    799       1.1    tsubai {
    800      1.23      kent 	struct awacs_softc *sc;
    801       1.1    tsubai 
    802      1.23      kent 	sc = h;
    803       1.1    tsubai 	dbdma_stop(sc->sc_odma);
    804       1.1    tsubai 	dbdma_reset(sc->sc_odma);
    805       1.1    tsubai 	return 0;
    806       1.1    tsubai }
    807       1.1    tsubai 
    808      1.28  macallan static int
    809      1.23      kent awacs_halt_input(void *h)
    810       1.1    tsubai {
    811      1.23      kent 	struct awacs_softc *sc;
    812       1.1    tsubai 
    813      1.23      kent 	sc = h;
    814       1.1    tsubai 	dbdma_stop(sc->sc_idma);
    815       1.1    tsubai 	dbdma_reset(sc->sc_idma);
    816       1.1    tsubai 	return 0;
    817       1.1    tsubai }
    818       1.1    tsubai 
    819      1.28  macallan static int
    820      1.23      kent awacs_getdev(void *h, struct audio_device *retp)
    821       1.1    tsubai {
    822      1.23      kent 
    823       1.1    tsubai 	*retp = awacs_device;
    824       1.1    tsubai 	return 0;
    825       1.1    tsubai }
    826       1.1    tsubai 
    827       1.1    tsubai enum {
    828       1.8    tsubai 	AWACS_MONITOR_CLASS,
    829       1.8    tsubai 	AWACS_OUTPUT_CLASS,
    830       1.8    tsubai 	AWACS_RECORD_CLASS,
    831       1.1    tsubai 	AWACS_OUTPUT_SELECT,
    832      1.28  macallan 	AWACS_VOL_MASTER,
    833       1.4       wiz 	AWACS_INPUT_SELECT,
    834       1.4       wiz 	AWACS_VOL_INPUT,
    835      1.33  macallan 	AWACS_VOL_MONITOR,
    836      1.25  macallan 	AWACS_BASS,
    837      1.25  macallan 	AWACS_TREBLE,
    838       1.1    tsubai 	AWACS_ENUM_LAST
    839       1.1    tsubai };
    840       1.1    tsubai 
    841      1.28  macallan static int
    842      1.23      kent awacs_set_port(void *h, mixer_ctrl_t *mc)
    843       1.1    tsubai {
    844      1.23      kent 	struct awacs_softc *sc;
    845       1.1    tsubai 	int l, r;
    846       1.1    tsubai 
    847       1.1    tsubai 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    848      1.23      kent 	sc = h;
    849       1.1    tsubai 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    850       1.1    tsubai 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    851       1.1    tsubai 
    852       1.1    tsubai 	switch (mc->dev) {
    853       1.1    tsubai 	case AWACS_OUTPUT_SELECT:
    854       1.8    tsubai 		/* No change necessary? */
    855       1.3       wiz 		if (mc->un.mask == sc->sc_output_mask)
    856       1.3       wiz 			return 0;
    857  1.40.6.1  macallan 		mutex_enter(&sc->sc_intr_lock);
    858      1.25  macallan 		awacs_select_output(sc, mc->un.mask);
    859  1.40.6.1  macallan 		mutex_exit(&sc->sc_intr_lock);
    860       1.1    tsubai 		return 0;
    861       1.1    tsubai 
    862      1.28  macallan 	case AWACS_VOL_MASTER:
    863  1.40.6.1  macallan 		mutex_enter(&sc->sc_intr_lock);
    864      1.28  macallan 		awacs_set_volume(sc, l, r);
    865  1.40.6.1  macallan 		mutex_exit(&sc->sc_intr_lock);
    866       1.1    tsubai 		return 0;
    867       1.4       wiz 
    868       1.4       wiz 	case AWACS_INPUT_SELECT:
    869       1.4       wiz 		/* no change necessary? */
    870       1.4       wiz 		if (mc->un.mask == sc->sc_record_source)
    871       1.4       wiz 			return 0;
    872       1.8    tsubai 		switch (mc->un.mask) {
    873       1.8    tsubai 		case 1 << 0: /* CD */
    874       1.4       wiz 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    875       1.4       wiz 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    876       1.4       wiz 			awacs_write_codec(sc, sc->sc_codecctl0);
    877       1.4       wiz 			break;
    878       1.8    tsubai 		case 1 << 1: /* microphone */
    879       1.4       wiz 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    880       1.4       wiz 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    881       1.4       wiz 			awacs_write_codec(sc, sc->sc_codecctl0);
    882       1.4       wiz 			break;
    883       1.8    tsubai 		case 1 << 2: /* line in */
    884       1.4       wiz 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    885       1.4       wiz 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    886       1.4       wiz 			awacs_write_codec(sc, sc->sc_codecctl0);
    887       1.4       wiz 			break;
    888       1.4       wiz 		default: /* invalid argument */
    889       1.8    tsubai 			return EINVAL;
    890       1.4       wiz 		}
    891       1.4       wiz 		sc->sc_record_source = mc->un.mask;
    892       1.4       wiz 		return 0;
    893       1.8    tsubai 
    894       1.8    tsubai 	case AWACS_VOL_INPUT:
    895       1.8    tsubai 		sc->sc_codecctl0 &= ~0xff;
    896       1.8    tsubai 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    897       1.8    tsubai 		awacs_write_codec(sc, sc->sc_codecctl0);
    898       1.8    tsubai 		return 0;
    899      1.25  macallan 
    900      1.33  macallan 	case AWACS_VOL_MONITOR:
    901      1.33  macallan 		awacs_set_loopthrough_volume(sc, l, r);
    902      1.33  macallan 		return 0;
    903      1.33  macallan 
    904      1.25  macallan #if NSGSMIX > 0
    905      1.25  macallan 	case AWACS_BASS:
    906  1.40.6.1  macallan 		mutex_enter(&sc->sc_intr_lock);
    907      1.25  macallan 		awacs_set_bass(sc, l);
    908  1.40.6.1  macallan 		mutex_exit(&sc->sc_intr_lock);
    909      1.25  macallan 		return 0;
    910      1.25  macallan 
    911      1.25  macallan 	case AWACS_TREBLE:
    912  1.40.6.1  macallan 		mutex_enter(&sc->sc_intr_lock);
    913      1.25  macallan 		awacs_set_treble(sc, l);
    914  1.40.6.1  macallan 		mutex_exit(&sc->sc_intr_lock);
    915      1.25  macallan 		return 0;
    916      1.25  macallan #endif
    917       1.1    tsubai 	}
    918       1.1    tsubai 
    919       1.1    tsubai 	return ENXIO;
    920       1.1    tsubai }
    921       1.1    tsubai 
    922      1.28  macallan static int
    923      1.23      kent awacs_get_port(void *h, mixer_ctrl_t *mc)
    924       1.1    tsubai {
    925      1.23      kent 	struct awacs_softc *sc;
    926      1.25  macallan 	int l, r, vol;
    927       1.1    tsubai 
    928      1.23      kent 	sc = h;
    929       1.1    tsubai 	switch (mc->dev) {
    930       1.1    tsubai 	case AWACS_OUTPUT_SELECT:
    931       1.1    tsubai 		mc->un.mask = sc->sc_output_mask;
    932       1.1    tsubai 		return 0;
    933       1.1    tsubai 
    934      1.28  macallan 	case AWACS_VOL_MASTER:
    935      1.28  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    936      1.28  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    937       1.4       wiz 		return 0;
    938       1.4       wiz 
    939       1.4       wiz 	case AWACS_INPUT_SELECT:
    940       1.4       wiz 		mc->un.mask = sc->sc_record_source;
    941       1.4       wiz 		return 0;
    942       1.4       wiz 
    943       1.4       wiz 	case AWACS_VOL_INPUT:
    944       1.4       wiz 		vol = sc->sc_codecctl0 & 0xff;
    945       1.4       wiz 		l = (vol & 0xf0);
    946       1.4       wiz 		r = (vol & 0x0f) << 4;
    947       1.1    tsubai 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    948       1.1    tsubai 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    949       1.1    tsubai 		return 0;
    950      1.33  macallan 
    951      1.33  macallan 	case AWACS_VOL_MONITOR:
    952      1.33  macallan 		vol = sc->sc_codecctl5 & 0x3cf;
    953      1.33  macallan 		l = (vol & 0x3c0) >> 6;
    954      1.33  macallan 		r = vol & 0xf;
    955      1.33  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
    956      1.33  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
    957      1.33  macallan 		return 0;
    958      1.33  macallan 
    959      1.25  macallan #if NSGSMIX > 0
    960      1.25  macallan 	case AWACS_BASS:
    961      1.25  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    962      1.25  macallan 		return 0;
    963      1.25  macallan 
    964      1.25  macallan 	case AWACS_TREBLE:
    965      1.25  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    966      1.25  macallan 		return 0;
    967      1.25  macallan #endif
    968       1.1    tsubai 
    969       1.1    tsubai 	default:
    970       1.1    tsubai 		return ENXIO;
    971       1.1    tsubai 	}
    972       1.1    tsubai 
    973       1.1    tsubai 	return 0;
    974       1.1    tsubai }
    975       1.1    tsubai 
    976      1.28  macallan static int
    977      1.23      kent awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    978       1.1    tsubai {
    979      1.25  macallan #if NSGSMIX > 0
    980      1.25  macallan 	struct awacs_softc *sc = h;
    981      1.25  macallan #endif
    982       1.1    tsubai 
    983       1.1    tsubai 	switch (dip->index) {
    984       1.1    tsubai 
    985       1.1    tsubai 	case AWACS_OUTPUT_SELECT:
    986      1.37  macallan 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    987       1.1    tsubai 		strcpy(dip->label.name, AudioNoutput);
    988       1.1    tsubai 		dip->type = AUDIO_MIXER_SET;
    989       1.1    tsubai 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    990       1.1    tsubai 		dip->un.s.num_mem = 2;
    991       1.1    tsubai 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    992       1.1    tsubai 		dip->un.s.member[0].mask = 1 << 0;
    993       1.1    tsubai 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    994       1.1    tsubai 		dip->un.s.member[1].mask = 1 << 1;
    995       1.1    tsubai 		return 0;
    996       1.1    tsubai 
    997      1.28  macallan 	case AWACS_VOL_MASTER:
    998      1.37  macallan 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    999      1.28  macallan 		strcpy(dip->label.name, AudioNmaster);
   1000       1.1    tsubai 		dip->type = AUDIO_MIXER_VALUE;
   1001       1.1    tsubai 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1002       1.1    tsubai 		dip->un.v.num_channels = 2;
   1003      1.37  macallan 		dip->un.v.delta = 16;
   1004       1.1    tsubai 		strcpy(dip->un.v.units.name, AudioNvolume);
   1005       1.1    tsubai 		return 0;
   1006       1.1    tsubai 
   1007      1.33  macallan 	case AWACS_VOL_MONITOR:
   1008      1.33  macallan 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1009      1.33  macallan 		strcpy(dip->label.name, AudioNmonitor);
   1010      1.33  macallan 		dip->type = AUDIO_MIXER_VALUE;
   1011      1.33  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1012      1.33  macallan 		dip->un.v.num_channels = 2;
   1013      1.33  macallan 		strcpy(dip->un.v.units.name, AudioNvolume);
   1014      1.33  macallan 		return 0;
   1015      1.33  macallan 
   1016      1.25  macallan #if NSGSMIX > 0
   1017      1.25  macallan 	case AWACS_BASS:
   1018      1.25  macallan 		if (sc->sc_sgsmix == NULL)
   1019      1.25  macallan 			return ENXIO;
   1020      1.37  macallan 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1021      1.25  macallan 		strcpy(dip->label.name, AudioNbass);
   1022      1.25  macallan 		dip->type = AUDIO_MIXER_VALUE;
   1023      1.25  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1024      1.25  macallan 		dip->un.v.num_channels = 1;
   1025      1.25  macallan 		strcpy(dip->un.v.units.name, AudioNbass);
   1026      1.25  macallan 		return 0;
   1027      1.25  macallan 
   1028      1.25  macallan 	case AWACS_TREBLE:
   1029      1.25  macallan 		if (sc->sc_sgsmix == NULL)
   1030      1.25  macallan 			return ENXIO;
   1031      1.37  macallan 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1032      1.25  macallan 		strcpy(dip->label.name, AudioNtreble);
   1033      1.25  macallan 		dip->type = AUDIO_MIXER_VALUE;
   1034      1.25  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1035      1.25  macallan 		dip->un.v.num_channels = 1;
   1036      1.25  macallan 		strcpy(dip->un.v.units.name, AudioNtreble);
   1037      1.25  macallan 		return 0;
   1038      1.25  macallan #endif
   1039      1.25  macallan 
   1040       1.4       wiz 	case AWACS_INPUT_SELECT:
   1041       1.8    tsubai 		dip->mixer_class = AWACS_RECORD_CLASS;
   1042       1.8    tsubai 		strcpy(dip->label.name, AudioNsource);
   1043       1.4       wiz 		dip->type = AUDIO_MIXER_SET;
   1044       1.4       wiz 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1045       1.4       wiz 		dip->un.s.num_mem = 3;
   1046       1.4       wiz 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
   1047       1.4       wiz 		dip->un.s.member[0].mask = 1 << 0;
   1048       1.4       wiz 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1049       1.4       wiz 		dip->un.s.member[1].mask = 1 << 1;
   1050       1.4       wiz 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1051       1.4       wiz 		dip->un.s.member[2].mask = 1 << 2;
   1052       1.4       wiz 		return 0;
   1053       1.4       wiz 
   1054       1.4       wiz 	case AWACS_VOL_INPUT:
   1055       1.8    tsubai 		dip->mixer_class = AWACS_RECORD_CLASS;
   1056       1.8    tsubai 		strcpy(dip->label.name, AudioNrecord);
   1057       1.4       wiz 		dip->type = AUDIO_MIXER_VALUE;
   1058       1.4       wiz 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1059       1.4       wiz 		dip->un.v.num_channels = 2;
   1060       1.4       wiz 		strcpy(dip->un.v.units.name, AudioNvolume);
   1061       1.4       wiz 		return 0;
   1062       1.4       wiz 
   1063       1.1    tsubai 	case AWACS_MONITOR_CLASS:
   1064       1.1    tsubai 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1065       1.1    tsubai 		strcpy(dip->label.name, AudioCmonitor);
   1066       1.1    tsubai 		dip->type = AUDIO_MIXER_CLASS;
   1067       1.1    tsubai 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1068       1.1    tsubai 		return 0;
   1069       1.1    tsubai 
   1070       1.1    tsubai 	case AWACS_OUTPUT_CLASS:
   1071       1.1    tsubai 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1072       1.1    tsubai 		strcpy(dip->label.name, AudioCoutputs);
   1073       1.4       wiz 		dip->type = AUDIO_MIXER_CLASS;
   1074       1.4       wiz 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1075       1.4       wiz 		return 0;
   1076       1.4       wiz 
   1077       1.4       wiz 	case AWACS_RECORD_CLASS:
   1078       1.8    tsubai 		dip->mixer_class = AWACS_RECORD_CLASS;
   1079       1.4       wiz 		strcpy(dip->label.name, AudioCrecord);
   1080       1.1    tsubai 		dip->type = AUDIO_MIXER_CLASS;
   1081       1.1    tsubai 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1082       1.1    tsubai 		return 0;
   1083       1.1    tsubai 	}
   1084       1.1    tsubai 
   1085       1.1    tsubai 	return ENXIO;
   1086       1.1    tsubai }
   1087       1.1    tsubai 
   1088      1.28  macallan static size_t
   1089      1.23      kent awacs_round_buffersize(void *h, int dir, size_t size)
   1090       1.1    tsubai {
   1091      1.23      kent 
   1092       1.1    tsubai 	if (size > 65536)
   1093       1.1    tsubai 		size = 65536;
   1094       1.1    tsubai 	return size;
   1095       1.1    tsubai }
   1096       1.1    tsubai 
   1097      1.28  macallan static paddr_t
   1098      1.23      kent awacs_mappage(void *h, void *mem, off_t off, int prot)
   1099       1.1    tsubai {
   1100      1.23      kent 
   1101       1.1    tsubai 	if (off < 0)
   1102       1.1    tsubai 		return -1;
   1103       1.1    tsubai 	return -1;	/* XXX */
   1104       1.1    tsubai }
   1105       1.1    tsubai 
   1106      1.28  macallan static int
   1107      1.23      kent awacs_get_props(void *h)
   1108       1.1    tsubai {
   1109       1.1    tsubai 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1110       1.1    tsubai }
   1111       1.1    tsubai 
   1112      1.28  macallan static int
   1113      1.23      kent awacs_trigger_output(void *h, void *start, void *end, int bsize,
   1114      1.23      kent 		     void (*intr)(void *), void *arg,
   1115      1.23      kent 		     const audio_params_t *param)
   1116       1.1    tsubai {
   1117      1.23      kent 	struct awacs_softc *sc;
   1118      1.23      kent 	struct dbdma_command *cmd;
   1119       1.1    tsubai 	vaddr_t va;
   1120       1.1    tsubai 	int i, len, intmode;
   1121       1.1    tsubai 
   1122       1.1    tsubai 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1123      1.23      kent 	sc = h;
   1124      1.23      kent 	cmd = sc->sc_odmacmd;
   1125       1.1    tsubai 	sc->sc_ointr = intr;
   1126       1.1    tsubai 	sc->sc_oarg = arg;
   1127      1.17   thorpej 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
   1128       1.1    tsubai 
   1129       1.1    tsubai #ifdef DIAGNOSTIC
   1130       1.1    tsubai 	if (sc->sc_opages > 16)
   1131       1.1    tsubai 		panic("awacs_trigger_output");
   1132       1.1    tsubai #endif
   1133       1.1    tsubai 
   1134       1.1    tsubai 	va = (vaddr_t)start;
   1135       1.1    tsubai 	len = 0;
   1136       1.1    tsubai 	for (i = sc->sc_opages; i > 0; i--) {
   1137      1.17   thorpej 		len += PAGE_SIZE;
   1138       1.1    tsubai 		if (len < bsize)
   1139       1.1    tsubai 			intmode = DBDMA_INT_NEVER;
   1140       1.1    tsubai 		else {
   1141       1.1    tsubai 			len = 0;
   1142       1.1    tsubai 			intmode = DBDMA_INT_ALWAYS;
   1143       1.1    tsubai 		}
   1144       1.1    tsubai 
   1145      1.17   thorpej 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
   1146       1.1    tsubai 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1147      1.17   thorpej 		va += PAGE_SIZE;
   1148       1.1    tsubai 		cmd++;
   1149       1.1    tsubai 	}
   1150       1.1    tsubai 
   1151       1.1    tsubai 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
   1152       1.1    tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
   1153      1.31   garbled 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1154       1.1    tsubai 
   1155       1.1    tsubai 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1156       1.1    tsubai 
   1157       1.1    tsubai 	return 0;
   1158       1.1    tsubai }
   1159       1.1    tsubai 
   1160      1.28  macallan static int
   1161      1.23      kent awacs_trigger_input(void *h, void *start, void *end, int bsize,
   1162      1.23      kent 		    void (*intr)(void *), void *arg,
   1163      1.23      kent 		    const audio_params_t *param)
   1164       1.1    tsubai {
   1165      1.23      kent 
   1166      1.28  macallan 	DPRINTF("awacs_trigger_input called\n");
   1167       1.1    tsubai 	return 1;
   1168       1.1    tsubai }
   1169  1.40.6.1  macallan 
   1170  1.40.6.1  macallan static void
   1171  1.40.6.1  macallan awacs_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
   1172  1.40.6.1  macallan {
   1173  1.40.6.1  macallan 	struct awacs_softc *sc = opaque;
   1174  1.40.6.1  macallan 
   1175  1.40.6.1  macallan 	*intr = &sc->sc_intr_lock;
   1176  1.40.6.1  macallan 	*thread = &sc->sc_lock;
   1177  1.40.6.1  macallan }
   1178       1.1    tsubai 
   1179      1.28  macallan static void
   1180      1.25  macallan awacs_select_output(struct awacs_softc *sc, int mask)
   1181      1.25  macallan {
   1182      1.25  macallan 
   1183      1.25  macallan #if NSGSMIX > 0
   1184      1.25  macallan 	if (sc->sc_sgsmix) {
   1185      1.25  macallan 		if (mask & OUTPUT_HEADPHONES) {
   1186      1.25  macallan 			/* mute speakers */
   1187      1.25  macallan 			sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
   1188      1.25  macallan 			sgsmix_set_headphone_vol(sc->sc_sgsmix,
   1189      1.28  macallan 			    sc->vol_l, sc->vol_r);
   1190      1.25  macallan 		}
   1191      1.25  macallan 		if (mask & OUTPUT_SPEAKER) {
   1192      1.25  macallan 			/* mute headphones */
   1193      1.25  macallan 			sgsmix_set_speaker_vol(sc->sc_sgsmix,
   1194      1.28  macallan 			    sc->vol_l, sc->vol_r);
   1195      1.25  macallan 			sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
   1196      1.25  macallan 		}
   1197      1.25  macallan 	} else {
   1198      1.25  macallan #endif
   1199      1.25  macallan 	sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
   1200      1.28  macallan 	if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
   1201      1.28  macallan 		if (mask & OUTPUT_SPEAKER)
   1202      1.28  macallan 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
   1203      1.28  macallan 		if (mask & OUTPUT_HEADPHONES)
   1204      1.28  macallan 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
   1205      1.28  macallan 	}
   1206      1.25  macallan 	awacs_write_codec(sc, sc->sc_codecctl1);
   1207      1.25  macallan #if NSGSMIX > 0
   1208      1.25  macallan 	}
   1209      1.25  macallan #endif
   1210      1.25  macallan 	sc->sc_output_mask = mask;
   1211      1.25  macallan }
   1212      1.25  macallan 
   1213      1.28  macallan static void
   1214      1.23      kent awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
   1215       1.1    tsubai {
   1216       1.1    tsubai 
   1217      1.25  macallan #if NSGSMIX > 0
   1218      1.25  macallan 	if (sc->sc_sgsmix) {
   1219      1.25  macallan 		if (sc->sc_output_mask & OUTPUT_SPEAKER)
   1220      1.25  macallan 			sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
   1221      1.25  macallan 	} else
   1222      1.25  macallan #endif
   1223      1.25  macallan 	{
   1224      1.25  macallan 		int lval;
   1225      1.25  macallan 		int rval;
   1226      1.28  macallan 		uint32_t codecctl = sc->sc_codecctl1;
   1227      1.25  macallan 
   1228      1.28  macallan 		lval = 15 - ((left  & 0xf0) >> 4);
   1229      1.28  macallan 		rval = 15 - ((right & 0xf0) >> 4);
   1230      1.25  macallan 		DPRINTF("speaker_volume %d %d\n", lval, rval);
   1231      1.25  macallan 
   1232      1.25  macallan 		sc->sc_codecctl4 &= ~0x3cf;
   1233      1.25  macallan 		sc->sc_codecctl4 |= (lval << 6) | rval;
   1234      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl4);
   1235      1.28  macallan 		if ((left == 0) && (right == 0)) {
   1236      1.28  macallan 			/*
   1237      1.28  macallan 			 * max. attenuation doesn't mean silence so we need to
   1238      1.28  macallan 			 * mute the output channel here
   1239      1.28  macallan 			 */
   1240      1.28  macallan 			codecctl |= AWACS_MUTE_SPEAKER;
   1241      1.28  macallan 		} else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
   1242      1.28  macallan 			codecctl &= ~AWACS_MUTE_SPEAKER;
   1243      1.28  macallan 		}
   1244      1.28  macallan 
   1245      1.28  macallan 		if (codecctl != sc->sc_codecctl1) {
   1246      1.28  macallan 
   1247      1.28  macallan 			sc->sc_codecctl1 = codecctl;
   1248      1.28  macallan 			awacs_write_codec(sc, sc->sc_codecctl1);
   1249      1.28  macallan 		}
   1250      1.25  macallan 	}
   1251      1.25  macallan }
   1252      1.25  macallan 
   1253      1.28  macallan static void
   1254      1.25  macallan awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
   1255      1.25  macallan {
   1256      1.25  macallan 
   1257      1.25  macallan #if NSGSMIX > 0
   1258      1.25  macallan 	if (sc->sc_sgsmix) {
   1259      1.25  macallan 		if (sc->sc_output_mask & OUTPUT_HEADPHONES)
   1260      1.25  macallan 			sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
   1261      1.25  macallan 	} else
   1262      1.25  macallan #endif
   1263      1.25  macallan 	{
   1264      1.25  macallan 		int lval;
   1265      1.25  macallan 		int rval;
   1266      1.28  macallan 		uint32_t codecctl = sc->sc_codecctl1;
   1267      1.25  macallan 
   1268      1.28  macallan 		lval = 15 - ((left  & 0xf0) >> 4);
   1269      1.28  macallan 		rval = 15 - ((right & 0xf0) >> 4);
   1270      1.25  macallan 		DPRINTF("ext_volume %d %d\n", lval, rval);
   1271      1.25  macallan 
   1272      1.25  macallan 		sc->sc_codecctl2 &= ~0x3cf;
   1273      1.25  macallan 		sc->sc_codecctl2 |= (lval << 6) | rval;
   1274      1.25  macallan 		awacs_write_codec(sc, sc->sc_codecctl2);
   1275      1.28  macallan 
   1276      1.28  macallan 		if ((left == 0) && (right == 0)) {
   1277      1.28  macallan 			/*
   1278      1.28  macallan 			 * max. attenuation doesn't mean silence so we need to
   1279      1.28  macallan 			 * mute the output channel here
   1280      1.28  macallan 			 */
   1281      1.28  macallan 			codecctl |= AWACS_MUTE_HEADPHONE;
   1282      1.28  macallan 		} else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
   1283      1.28  macallan 
   1284      1.28  macallan 			codecctl &= ~AWACS_MUTE_HEADPHONE;
   1285      1.28  macallan 		}
   1286      1.28  macallan 
   1287      1.28  macallan 		if (codecctl != sc->sc_codecctl1) {
   1288      1.28  macallan 
   1289      1.28  macallan 			sc->sc_codecctl1 = codecctl;
   1290      1.28  macallan 			awacs_write_codec(sc, sc->sc_codecctl1);
   1291      1.28  macallan 		}
   1292      1.25  macallan 	}
   1293      1.28  macallan }
   1294      1.28  macallan 
   1295      1.34  macallan void
   1296      1.28  macallan awacs_set_volume(struct awacs_softc *sc, int left, int right)
   1297      1.28  macallan {
   1298      1.28  macallan 
   1299      1.28  macallan 	awacs_set_ext_volume(sc, left, right);
   1300      1.28  macallan 	awacs_set_speaker_volume(sc, left, right);
   1301      1.28  macallan 
   1302      1.28  macallan 	sc->vol_l = left;
   1303      1.28  macallan 	sc->vol_r = right;
   1304      1.25  macallan }
   1305      1.25  macallan 
   1306      1.25  macallan #if NSGSMIX > 0
   1307      1.25  macallan static void
   1308      1.25  macallan awacs_set_bass(struct awacs_softc *sc, int bass)
   1309      1.25  macallan {
   1310      1.25  macallan 
   1311      1.25  macallan 	if (sc->sc_bass == bass)
   1312      1.25  macallan 		return;
   1313      1.25  macallan 
   1314      1.25  macallan 	sc->sc_bass = bass;
   1315      1.25  macallan 	if (sc->sc_sgsmix)
   1316      1.34  macallan 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1317      1.34  macallan 		    sc->sc_treble);
   1318      1.25  macallan }
   1319      1.25  macallan 
   1320      1.25  macallan static void
   1321      1.25  macallan awacs_set_treble(struct awacs_softc *sc, int treble)
   1322      1.25  macallan {
   1323      1.25  macallan 
   1324      1.25  macallan 	if (sc->sc_treble == treble)
   1325      1.25  macallan 		return;
   1326       1.1    tsubai 
   1327      1.25  macallan 	sc->sc_treble = treble;
   1328      1.25  macallan 	if (sc->sc_sgsmix)
   1329      1.34  macallan 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1330      1.34  macallan 		    sc->sc_treble);
   1331       1.1    tsubai }
   1332      1.25  macallan #endif
   1333       1.1    tsubai 
   1334       1.1    tsubai void
   1335      1.25  macallan awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
   1336       1.1    tsubai {
   1337      1.23      kent 	int lval;
   1338      1.23      kent 	int rval;
   1339       1.1    tsubai 
   1340      1.28  macallan 	lval = 15 - ((left  & 0xff) >> 4);
   1341      1.28  macallan 	rval = 15 - ((right & 0xff) >> 4);
   1342      1.25  macallan 	DPRINTF("loopthrough_volume %d %d\n", lval, rval);
   1343       1.1    tsubai 
   1344      1.25  macallan 	sc->sc_codecctl5 &= ~0x3cf;
   1345      1.25  macallan 	sc->sc_codecctl5 |= (lval << 6) | rval;
   1346      1.25  macallan 	awacs_write_codec(sc, sc->sc_codecctl5);
   1347       1.1    tsubai }
   1348       1.1    tsubai 
   1349       1.1    tsubai int
   1350      1.23      kent awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
   1351       1.1    tsubai {
   1352       1.1    tsubai 	int c;
   1353       1.1    tsubai 
   1354      1.15       wiz 	switch (p->sample_rate) {
   1355       1.1    tsubai 	case 44100:
   1356       1.1    tsubai 		c = AWACS_RATE_44100;
   1357       1.1    tsubai 		break;
   1358       1.1    tsubai 	case 29400:
   1359       1.1    tsubai 		c = AWACS_RATE_29400;
   1360       1.1    tsubai 		break;
   1361       1.1    tsubai 	case 22050:
   1362       1.1    tsubai 		c = AWACS_RATE_22050;
   1363       1.1    tsubai 		break;
   1364       1.1    tsubai 	case 17640:
   1365       1.1    tsubai 		c = AWACS_RATE_17640;
   1366       1.1    tsubai 		break;
   1367       1.1    tsubai 	case 14700:
   1368       1.1    tsubai 		c = AWACS_RATE_14700;
   1369       1.1    tsubai 		break;
   1370       1.1    tsubai 	case 11025:
   1371       1.1    tsubai 		c = AWACS_RATE_11025;
   1372       1.1    tsubai 		break;
   1373       1.1    tsubai 	case 8820:
   1374       1.1    tsubai 		c = AWACS_RATE_8820;
   1375       1.1    tsubai 		break;
   1376       1.1    tsubai 	case 7350:
   1377       1.1    tsubai 		c = AWACS_RATE_7350;
   1378       1.1    tsubai 		break;
   1379       1.1    tsubai 	default:
   1380       1.1    tsubai 		return -1;
   1381       1.1    tsubai 	}
   1382       1.1    tsubai 
   1383       1.1    tsubai 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
   1384       1.1    tsubai 	sc->sc_soundctl |= c;
   1385       1.1    tsubai 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
   1386       1.1    tsubai 
   1387       1.1    tsubai 	return 0;
   1388       1.1    tsubai }
   1389      1.28  macallan 
   1390      1.28  macallan static int
   1391      1.28  macallan awacs_check_headphones(struct awacs_softc *sc)
   1392      1.28  macallan {
   1393      1.28  macallan 	uint32_t reg;
   1394      1.28  macallan 	reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
   1395      1.34  macallan 	DPRINTF("%s: codec status reg %08x\n", device_xname(sc->sc_dev), reg);
   1396      1.28  macallan 	return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
   1397      1.28  macallan }
   1398      1.28  macallan 
   1399      1.28  macallan static int
   1400      1.28  macallan awacs_status_intr(void *cookie)
   1401      1.28  macallan {
   1402      1.28  macallan 	struct awacs_softc *sc = cookie;
   1403      1.28  macallan 	int mask;
   1404      1.28  macallan 
   1405  1.40.6.1  macallan 	mutex_spin_enter(&sc->sc_intr_lock);
   1406      1.28  macallan 	mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
   1407      1.28  macallan 	if (mask != sc->sc_output_mask) {
   1408      1.28  macallan 
   1409      1.28  macallan 		sc->sc_output_wanted = mask;
   1410  1.40.6.1  macallan 		cv_signal(&sc->sc_event);
   1411      1.28  macallan 	}
   1412      1.28  macallan 	/* clear the interrupt */
   1413      1.28  macallan 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
   1414  1.40.6.1  macallan 	mutex_spin_exit(&sc->sc_intr_lock);
   1415      1.28  macallan 	return 1;
   1416      1.28  macallan }
   1417      1.28  macallan 
   1418      1.28  macallan static void
   1419      1.28  macallan awacs_thread(void *cookie)
   1420      1.28  macallan {
   1421      1.28  macallan 	struct awacs_softc *sc = cookie;
   1422      1.28  macallan 
   1423  1.40.6.1  macallan 	mutex_enter(&sc->sc_intr_lock);
   1424      1.28  macallan 	while (1) {
   1425  1.40.6.1  macallan 		cv_timedwait(&sc->sc_event, &sc->sc_intr_lock, hz);
   1426      1.28  macallan 		if (sc->sc_output_wanted == sc->sc_output_mask)
   1427      1.28  macallan 			continue;
   1428      1.28  macallan 
   1429      1.28  macallan 		awacs_select_output(sc, sc->sc_output_wanted);
   1430      1.34  macallan 		DPRINTF("%s: switching to %s\n", device_xname(sc->sc_dev),
   1431      1.28  macallan 		    (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
   1432      1.28  macallan 		    "speaker" : "headphones");
   1433      1.28  macallan 	}
   1434      1.28  macallan }
   1435