Home | History | Annotate | Line # | Download | only in obio
ascaudio.c revision 1.16
      1 /* $NetBSD: ascaudio.c,v 1.16 2025/06/04 09:27:09 nat Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017, 2023 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* Based on pad(4) and asc(4) */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: ascaudio.c,v 1.16 2025/06/04 09:27:09 nat Exp $");
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/conf.h>
     37 #include <sys/buf.h>
     38 #include <sys/kauth.h>
     39 #include <sys/kmem.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/proc.h>
     43 #include <sys/audioio.h>
     44 #include <sys/module.h>
     45 #include <sys/atomic.h>
     46 
     47 #include <uvm/uvm_extern.h>
     48 
     49 #include <dev/audio/audio_if.h>
     50 #include <dev/audio/audiovar.h>
     51 
     52 #include <machine/autoconf.h>
     53 #include <machine/cpu.h>
     54 #include <machine/bus.h>
     55 #include <machine/viareg.h>
     56 
     57 #include <mac68k/dev/pm_direct.h>
     58 #include <mac68k/obio/ascaudiovar.h>
     59 #include <mac68k/obio/ascreg.h>
     60 #include <mac68k/obio/obiovar.h>
     61 
     62 #define	MAC68K_ASCAUDIO_BASE		0x50f14000
     63 #define	MAC68K_IIFX_ASCAUDIO_BASE	0x50f10000
     64 #define	MAC68K_ASCAUDIO_LEN		0x2000
     65 
     66 #define BUFSIZE 			32768
     67 #define PLAYBLKSIZE			8192
     68 #define RECBLKSIZE			1024
     69 
     70 #define ASC_VIA_CLR_INTR()     via_reg(VIA2, vIFR) = V2IF_ASC
     71 
     72 static int	ascaudiomatch(device_t, cfdata_t, void *);
     73 static void	ascaudioattach(device_t, device_t, void *);
     74 
     75 CFATTACH_DECL_NEW(ascaudio, sizeof(struct ascaudio_softc),
     76     ascaudiomatch, ascaudioattach, NULL, NULL);
     77 
     78 extern struct cfdriver ascaudio_cd;
     79 
     80 dev_type_open(ascaudioopen);
     81 dev_type_close(ascaudioclose);
     82 dev_type_read(ascaudioread);
     83 dev_type_write(ascaudiowrite);
     84 dev_type_ioctl(ascaudioioctl);
     85 
     86 const struct cdevsw ascaudio_cdevsw = {
     87 	.d_open = ascaudioopen,
     88 	.d_close = ascaudioclose,
     89 	.d_read = ascaudioread,
     90 	.d_write = ascaudiowrite,
     91 	.d_ioctl = ascaudioioctl,
     92 	.d_stop = nostop,
     93 	.d_tty = notty,
     94 	.d_poll = nopoll,
     95 	.d_mmap = nommap,
     96 	.d_kqfilter = nokqfilter,
     97 	.d_discard = nodiscard,
     98 	.d_flag = 0
     99 };
    100 
    101 static int	ascaudio_query_format(void *, struct audio_format_query *);
    102 static int	ascaudio_set_format(void *, int,
    103 		    const audio_params_t *, const audio_params_t *,
    104 		    audio_filter_reg_t *, audio_filter_reg_t *);
    105 static int	ascaudio_start_output(void *, void *, int,
    106 				    void (*)(void *), void *);
    107 static int	ascaudio_start_input(void *, void *, int,
    108 				   void (*)(void *), void *);
    109 static int	ascaudio_halt_input(void *);
    110 static int	ascaudio_halt_output(void *);
    111 static int	ascaudio_set_port(void *, mixer_ctrl_t *);
    112 static int	ascaudio_get_port(void *, mixer_ctrl_t *);
    113 static int	ascaudio_getdev(void *, struct audio_device *);
    114 static int	ascaudio_query_devinfo(void *, mixer_devinfo_t *);
    115 static int	ascaudio_get_props(void *);
    116 static int
    117 	    ascaudio_round_blocksize(void *, int, int, const audio_params_t *);
    118 static void	ascaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    119 static void 	ascaudio_intr(void *);
    120 static int 	ascaudio_intr_est(void *);
    121 static void	ascaudio_intr_enable(void);
    122 static void	ascaudio_done_output(void *);
    123 static void	ascaudio_done_input(void *);
    124 static void	configure_dfac(uint8_t);
    125 
    126 static const struct audio_hw_if ascaudio_hw_if = {
    127 	.query_format	 = ascaudio_query_format,
    128 	.set_format	 = ascaudio_set_format,
    129 	.start_output	 = ascaudio_start_output,
    130 	.start_input	 = ascaudio_start_input,
    131 	.halt_output	 = ascaudio_halt_output,
    132 	.halt_input	 = ascaudio_halt_input,
    133 	.set_port	 = ascaudio_set_port,
    134 	.get_port	 = ascaudio_get_port,
    135 	.getdev		 = ascaudio_getdev,
    136 	.query_devinfo	 = ascaudio_query_devinfo,
    137 	.get_props 	 = ascaudio_get_props,
    138 	.round_blocksize = ascaudio_round_blocksize,
    139 	.get_locks	 = ascaudio_get_locks,
    140 };
    141 
    142 enum {
    143 	ASC_OUTPUT_CLASS,
    144 	ASC_INPUT_CLASS,
    145 	ASC_OUTPUT_MASTER_VOLUME,
    146 	ASC_INPUT_DAC_VOLUME,
    147 	ASC_ENUM_LAST,
    148 };
    149 
    150 static int
    151 ascaudiomatch(device_t parent, cfdata_t cf, void *aux)
    152 {
    153 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    154 	bus_addr_t addr;
    155 	bus_space_handle_t bsh;
    156 	int rval = 0;
    157 
    158 	if (oa->oa_addr != (-1))
    159 		addr = (bus_addr_t)oa->oa_addr;
    160 	else if (current_mac_model->machineid == MACH_MACTV)
    161 		return 0;
    162 	else if (current_mac_model->machineid == MACH_MACIIFX)
    163 		addr = (bus_addr_t)MAC68K_IIFX_ASCAUDIO_BASE;
    164 	else
    165 		addr = (bus_addr_t)MAC68K_ASCAUDIO_BASE;
    166 
    167 	if (bus_space_map(oa->oa_tag, addr, MAC68K_ASCAUDIO_LEN, 0, &bsh))
    168 		return (0);
    169 
    170 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
    171 		rval = 1;
    172 	} else
    173 		rval = 0;
    174 
    175 	bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASCAUDIO_LEN);
    176 
    177 	return rval;
    178 }
    179 
    180 static void
    181 ascaudioattach(device_t parent, device_t self, void *aux)
    182 {
    183 	struct ascaudio_softc *sc = device_private(self);
    184 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    185 	bus_addr_t addr;
    186 	uint8_t tmp;
    187 
    188 	sc->sc_dev = self;
    189 	sc->sc_tag = oa->oa_tag;
    190 
    191 	if (oa->oa_addr != (-1))
    192 		addr = (bus_addr_t)oa->oa_addr;
    193 	else if (current_mac_model->machineid == MACH_MACIIFX)
    194 		addr = (bus_addr_t)MAC68K_IIFX_ASCAUDIO_BASE;
    195 	else
    196 		addr = (bus_addr_t)MAC68K_ASCAUDIO_BASE;
    197 	if (bus_space_map(sc->sc_tag, addr, MAC68K_ASCAUDIO_LEN, 0,
    198 	    &sc->sc_handle)) {
    199 		printf(": can't map memory space\n");
    200 		return;
    201 	}
    202 
    203 	/* Pull in the options flags. */
    204 	sc->sc_options = ((device_cfdata(self)->cf_flags) &
    205 			    ASCAUDIO_OPTIONS_MASK);
    206 
    207 	sc->sc_playbuf = kmem_alloc(BUFSIZE, KM_SLEEP);
    208 	sc->sc_recbuf = kmem_alloc(BUFSIZE, KM_SLEEP);
    209 	sc->sc_rptr = sc->sc_recbuf;
    210 	sc->sc_getptr = sc->sc_recbuf;
    211 	sc->sc_wptr = sc->sc_playbuf;
    212 	sc->sc_putptr = sc->sc_playbuf;
    213 
    214 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    215 
    216 	sc->sc_ver = bus_space_read_1(oa->oa_tag, sc->sc_handle, 0x800);
    217 
    218 	if (sc->sc_options & HIGHQUALITY) {
    219 		tmp = bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCRATE);
    220 		switch (tmp) {
    221 			case 2:
    222 				sc->sc_rate = 22050;
    223 				break;
    224 			case 3:
    225 				sc->sc_rate = 44100;
    226 				break;
    227 			default:
    228 				sc->sc_rate = 22254;
    229 				break;
    230 		}
    231 
    232 		tmp = bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCTRL);
    233 		if (tmp & STEREO)
    234 			sc->sc_speakers = 2;
    235 		else
    236 			sc->sc_speakers = 1;
    237 
    238 	} else {
    239 		__USE(tmp);
    240 		sc->sc_rate = 22254;
    241 		sc->sc_speakers = 1;
    242 	}
    243 
    244 	if (sc->sc_options & LOWQUALITY) {
    245 		sc->sc_slowcpu = true;
    246 		if (sc->sc_slowcpu)
    247 			sc->sc_rate /= 2;
    248 	}
    249 
    250 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    251 		printf(": Enhanced Apple Sound Chip");
    252 	else
    253 		printf(": Apple Sound Chip");
    254 
    255 	if (oa->oa_addr != (-1))
    256 		printf(" at %x", oa->oa_addr);
    257 	printf("\n");
    258 
    259 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    260 
    261 	if (mac68k_machine.aux_interrupts) {
    262 		intr_establish(ascaudio_intr_est, sc, ASCIRQ);
    263 	} else {
    264 		via2_register_irq(VIA2_ASC, ascaudio_intr, sc);
    265 	}
    266 	ascaudio_intr_enable();
    267 
    268 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    269 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    270 	callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
    271 	callout_setfunc(&sc->sc_pcallout, ascaudio_done_output, sc);
    272 	callout_init(&sc->sc_rcallout, CALLOUT_MPSAFE);
    273 	callout_setfunc(&sc->sc_rcallout, ascaudio_done_input, sc);
    274 
    275 	sc->sc_vol = 180;
    276 	sc->sc_recvol = 255;
    277 
    278 	sc->sc_audiodev = audio_attach_mi(&ascaudio_hw_if, sc, sc->sc_dev);
    279 
    280 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    281 		aprint_error_dev(sc->sc_dev,
    282 		    "couldn't establish power handler\n");
    283 
    284 	if (sc->sc_ver != EASC_VER && sc->sc_ver != EASC_VER2)
    285 		return;
    286 
    287 	if (sc->sc_options & HIGHQUALITY)
    288 		tmp = CDQUALITY;
    289 	else
    290 		tmp = MACDEFAULTS;
    291 
    292 	bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOCTRLA, tmp);
    293 	bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOCTRLB, tmp);
    294 
    295 }
    296 
    297 int
    298 ascaudioopen(dev_t dev, int flag, int mode, struct lwp *l)
    299 {
    300 	struct ascaudio_softc *sc;
    301 
    302 	sc = device_lookup_private(&ascaudio_cd, ASCAUDIOUNIT(dev));
    303 	if (sc == NULL)
    304 		return (ENXIO);
    305 	if (sc->sc_open)
    306 		return (EBUSY);
    307 	sc->sc_open = 1;
    308 
    309 	return (0);
    310 }
    311 
    312 int
    313 ascaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
    314 {
    315 	struct ascaudio_softc *sc;
    316 
    317 	sc = device_lookup_private(&ascaudio_cd, ASCAUDIOUNIT(dev));
    318 	sc->sc_open = 0;
    319 
    320 	return (0);
    321 }
    322 
    323 int
    324 ascaudioread(dev_t dev, struct uio *uio, int ioflag)
    325 {
    326 	return (ENXIO);
    327 }
    328 
    329 int
    330 ascaudiowrite(dev_t dev, struct uio *uio, int ioflag)
    331 {
    332 	return (ENXIO);
    333 }
    334 
    335 int
    336 ascaudioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    337 {
    338 	int error;
    339 #ifdef notyet
    340 	struct ascaudio_softc *sc;
    341 	int unit = ASCAUDIOUNIT(dev);
    342 
    343 	sc = device_lookup_private(&ascaudio_cd, unit);
    344 #endif
    345 	error = 0;
    346 
    347 	switch (cmd) {
    348 	default:
    349 		error = EINVAL;
    350 		break;
    351 	}
    352 	return (error);
    353 }
    354 
    355 #define ASCAUDIO_NFORMATS	3
    356 static int
    357 ascaudio_query_format(void *opaque, struct audio_format_query *ae)
    358 {
    359 	struct ascaudio_softc *sc = opaque;
    360 
    361 	const struct audio_format asc_formats[ASCAUDIO_NFORMATS] = {
    362 	      { .mode		= AUMODE_PLAY,
    363 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    364 		.validbits	= 8,
    365 		.precision	= 8,
    366 		.channels	= sc->sc_speakers,
    367 		.channel_mask	= sc->sc_speakers == 2 ? AUFMT_STEREO :
    368 		    AUFMT_MONAURAL,
    369 		.frequency_type	= 1,
    370 		.frequency	= { sc->sc_rate }, },
    371 	      { .mode		= AUMODE_RECORD,
    372 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    373 		.validbits	= 8,
    374 		.precision	= 8,
    375 		.channels	= 1,
    376 		.channel_mask	= AUFMT_MONAURAL,
    377 		.frequency_type	= 1,
    378 		.frequency	= { 11025 }, },
    379 	      { .mode		= AUMODE_RECORD,
    380 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    381 		.validbits	= 8,
    382 		.precision	= 8,
    383 		.channels	= 1,
    384 		.channel_mask	= AUFMT_MONAURAL,
    385 		.frequency_type	= 1,
    386 		.frequency	= { 22050 }, }
    387 	};
    388 
    389 	return audio_query_format(asc_formats, ASCAUDIO_NFORMATS, ae);
    390 }
    391 
    392 static int
    393 ascaudio_set_format(void *opaque, int setmode,
    394     const audio_params_t *play, const audio_params_t *rec,
    395     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    396 {
    397 	struct ascaudio_softc *sc = opaque;
    398 
    399 	KASSERT(mutex_owned(&sc->sc_lock));
    400 
    401 	sc->sc_recfreq = rec->sample_rate;
    402 
    403 	return 0;
    404 }
    405 
    406 static int
    407 ascaudio_start_output(void *opaque, void *block, int blksize,
    408     void (*intr)(void *), void *intrarg)
    409 {
    410 	struct ascaudio_softc *sc;
    411 	uint8_t *loc, tmp;
    412 	int total;
    413 
    414 	sc = (struct ascaudio_softc *)opaque;
    415 	if (!sc)
    416 		return (ENODEV);
    417 
    418 	sc->sc_pintr = intr;
    419 	sc->sc_pintrarg = intrarg;
    420 
    421 	loc = block;
    422  	if (bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCMODE) !=
    423 								 MODEFIFO) {
    424 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    425 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCTEST, 0);
    426 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    427 		    NONCOMP);
    428 
    429 	}
    430 
    431 	/* set the volume. */
    432 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    433 		/* DO NOT CHANGE THESE VALUES UNLESS TESTED.
    434 		   CAN BE VERY LOUD!!!! */
    435 		tmp = sc->sc_vol >> 5;
    436 		KASSERT(tmp <= MACOS_HIGH_VOL);
    437  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_LEFT_VOL, tmp);
    438  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_LEFT_VOL, tmp);
    439  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_RIGHT_VOL, tmp);
    440  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_RIGHT_VOL, tmp);
    441 	}
    442 	bus_space_write_1(sc->sc_tag, sc->sc_handle, INTVOL, sc->sc_vol);
    443 
    444 	total = blksize;
    445 	if (sc->sc_putptr + blksize >= sc->sc_playbuf + BUFSIZE)
    446 		total = sc->sc_playbuf + BUFSIZE - sc->sc_putptr;
    447 
    448 	if (total) {
    449 		memcpy(sc->sc_putptr, loc, total);
    450 		sc->sc_putptr += total;
    451 		loc += total;
    452 	}
    453 
    454 	total = blksize - total;
    455 	if (total) {
    456 		sc->sc_putptr = sc->sc_playbuf;
    457 		memcpy(sc->sc_playbuf, loc, total);
    458 		sc->sc_putptr += total;
    459 	}
    460 
    461 	sc->sc_avail += blksize;
    462 	if (sc->sc_avail > BUFSIZE)
    463 		sc->sc_avail = BUFSIZE;
    464 
    465 	/* start fifo playback */
    466 	if ((sc->sc_rintr == NULL) && bus_space_read_1(sc->sc_tag,
    467 	    sc->sc_handle, ASCMODE) != MODEFIFO)
    468 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODEFIFO);
    469 
    470 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    471 		/* enable interrupts channel b */
    472 		bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, 0);
    473 	}
    474 
    475 	return 0;
    476 }
    477 
    478 static int
    479 ascaudio_start_input(void *opaque, void *block, int blksize,
    480     void (*intr)(void *), void *intrarg)
    481 {
    482 	struct ascaudio_softc *sc;
    483 	uint8_t tmp;
    484 	int total;
    485 
    486 	sc = (struct ascaudio_softc *)opaque;
    487 	if (!sc)
    488 		return (ENODEV);
    489 
    490 	uint8_t *loc;
    491 	loc = block;
    492 
    493 	sc->sc_rintr = intr;
    494 	sc->sc_rintrarg = intrarg;
    495 
    496  	if (bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCMODE) !=
    497 								 MODEFIFO) {
    498 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    499 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCTEST, 0);
    500 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    501 		    NONCOMP);
    502 		memset(loc, 0x80, blksize);
    503 	}
    504 
    505 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    506 		/*
    507 		 * Set up dfac for microphone.
    508 		 * DO NOT SET BITS 5-7 due to loud feedback squeal.
    509 		 */
    510 		configure_dfac(DFAC_GAIN_HIGH);
    511 	}
    512 
    513 	tmp = RECORDA;
    514 	if (sc->sc_recfreq == 22050)
    515 		tmp |= REC22KHZ;
    516 	bus_space_write_1(sc->sc_tag, sc->sc_handle, APLAYREC, tmp);
    517 
    518 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    519 		/* enable interrupts channel a */
    520 		bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA, 0);
    521 	}
    522 
    523 	/* start fifo playback */
    524 	if ((sc->sc_pintr == NULL) && bus_space_read_1(sc->sc_tag,
    525 	    sc->sc_handle, ASCMODE) != MODEFIFO) {
    526 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODEFIFO);
    527 
    528 		return 0;
    529 	}
    530 
    531 	/* set the volume. */
    532 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    533 		/* DO NOT CHANGE THESE VALUES UNLESS TESTED.
    534 		   CAN BE VERY LOUD!!!! */
    535 		tmp = sc->sc_recvol >> 5;
    536 		KASSERT(tmp <= MACOS_HIGH_VOL);
    537  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_LEFT_VOL, tmp);
    538  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_LEFT_VOL, tmp);
    539  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_RIGHT_VOL, tmp);
    540  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_RIGHT_VOL, tmp);
    541 	}
    542 	bus_space_write_1(sc->sc_tag, sc->sc_handle, INTVOL, sc->sc_recvol);
    543 
    544 	total = blksize;
    545 	if (sc->sc_getptr + blksize >= sc->sc_recbuf + BUFSIZE)
    546 		total = sc->sc_recbuf + BUFSIZE - sc->sc_getptr;
    547 
    548 	if (total) {
    549 		memcpy(loc, sc->sc_getptr, total);
    550 		sc->sc_getptr += total;
    551 		loc += total;
    552 	}
    553 
    554 	total = blksize - total;
    555 	if (total) {
    556 		sc->sc_getptr = sc->sc_recbuf;
    557 		memcpy(loc, sc->sc_getptr, total);
    558 		sc->sc_getptr += total;
    559 	}
    560 
    561 	sc->sc_recavail -= blksize;
    562 
    563 	return 0;
    564 }
    565 
    566 static int
    567 ascaudio_halt_input(void *opaque)
    568 {
    569 	ascaudio_softc_t *sc;
    570 
    571 	sc = (ascaudio_softc_t *)opaque;
    572 
    573 	KASSERT(mutex_owned(&sc->sc_lock));
    574 
    575 	callout_halt(&sc->sc_rcallout, &sc->sc_intr_lock);
    576 
    577 	sc->sc_rintr = NULL;
    578 	sc->sc_rintrarg = NULL;
    579 	sc->sc_recavail = 0;
    580 
    581 	bus_space_write_1(sc->sc_tag, sc->sc_handle, APLAYREC, 0);
    582 
    583 	if (sc->sc_pintr == NULL) {
    584 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    585 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    586 		    CLEARFIFO);
    587 	}
    588 
    589 	sc->sc_rptr = sc->sc_recbuf;
    590 	sc->sc_getptr = sc->sc_recbuf;
    591 
    592 	if (sc->sc_ver != EASC_VER && sc->sc_ver != EASC_VER2)
    593 		return 0;
    594 
    595 	configure_dfac(DFAC_DISABLE);
    596 
    597 	/* disable half interrupts channel a */
    598 	bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA, DISABLEHALFIRQ);
    599 
    600 	return 0;
    601 }
    602 
    603 static int
    604 ascaudio_halt_output(void *opaque)
    605 {
    606 	ascaudio_softc_t *sc;
    607 
    608 	sc = (ascaudio_softc_t *)opaque;
    609 
    610 	KASSERT(mutex_owned(&sc->sc_lock));
    611 
    612 	callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
    613 
    614 	sc->sc_pintr = NULL;
    615 	sc->sc_pintrarg = NULL;
    616 	sc->sc_avail = 0;
    617 
    618 	if (sc->sc_rintr == NULL) {
    619 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    620 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    621 		    CLEARFIFO);
    622 	}
    623 
    624 	sc->sc_wptr = sc->sc_playbuf;
    625 	sc->sc_putptr = sc->sc_playbuf;
    626 
    627 	if (sc->sc_ver != EASC_VER && sc->sc_ver != EASC_VER2)
    628 		return 0;
    629 
    630 	/* disable half interrupts channel b */
    631 	bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, DISABLEHALFIRQ);
    632 
    633 	return 0;
    634 }
    635 
    636 static int
    637 ascaudio_getdev(void *opaque, struct audio_device *ret)
    638 {
    639 	strlcpy(ret->name, "Apple ASC Audio", sizeof(ret->name));
    640 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    641 	strlcpy(ret->config, "ascaudio", sizeof(ret->config));
    642 
    643 	return 0;
    644 }
    645 
    646 static int
    647 ascaudio_set_port(void *opaque, mixer_ctrl_t *mc)
    648 {
    649 	struct ascaudio_softc *sc = opaque;
    650 
    651 	KASSERT(mutex_owned(&sc->sc_lock));
    652 
    653 	switch (mc->dev) {
    654 	case ASC_OUTPUT_MASTER_VOLUME:
    655 		if (mc->un.value.num_channels != 1)
    656 			return EINVAL;
    657 		sc->sc_vol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    658 		return 0;
    659 	case ASC_INPUT_DAC_VOLUME:
    660 		if (mc->un.value.num_channels != 1)
    661 			return EINVAL;
    662 		sc->sc_recvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    663 		return 0;
    664 	}
    665 
    666 	return ENXIO;
    667 }
    668 
    669 static int
    670 ascaudio_get_port(void *opaque, mixer_ctrl_t *mc)
    671 {
    672 	struct ascaudio_softc *sc = opaque;
    673 
    674 	KASSERT(mutex_owned(&sc->sc_lock));
    675 
    676 	switch (mc->dev) {
    677 	case ASC_OUTPUT_MASTER_VOLUME:
    678 		if (mc->un.value.num_channels != 1)
    679 			return EINVAL;
    680 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_vol;
    681 		return 0;
    682 	case ASC_INPUT_DAC_VOLUME:
    683 		if (mc->un.value.num_channels != 1)
    684 			return EINVAL;
    685 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_recvol;
    686 		return 0;
    687 	}
    688 
    689 	return ENXIO;
    690 }
    691 
    692 static int
    693 ascaudio_query_devinfo(void *opaque, mixer_devinfo_t *di)
    694 {
    695 	ascaudio_softc_t *sc __diagused;
    696 
    697 	sc = (ascaudio_softc_t *)opaque;
    698 
    699 	KASSERT(mutex_owned(&sc->sc_lock));
    700 
    701 	switch (di->index) {
    702 	case ASC_OUTPUT_CLASS:
    703 		di->mixer_class = ASC_OUTPUT_CLASS;
    704 		strcpy(di->label.name, AudioCoutputs);
    705 		di->type = AUDIO_MIXER_CLASS;
    706 		di->next = di->prev = AUDIO_MIXER_LAST;
    707 		return 0;
    708 	case ASC_INPUT_CLASS:
    709 		di->mixer_class = ASC_INPUT_CLASS;
    710 		strcpy(di->label.name, AudioCinputs);
    711 		di->type = AUDIO_MIXER_CLASS;
    712 		di->next = di->prev = AUDIO_MIXER_LAST;
    713 		return 0;
    714 	case ASC_OUTPUT_MASTER_VOLUME:
    715 		di->mixer_class = ASC_OUTPUT_CLASS;
    716 		strcpy(di->label.name, AudioNmaster);
    717 		di->type = AUDIO_MIXER_VALUE;
    718 		di->next = di->prev = AUDIO_MIXER_LAST;
    719 		di->un.v.num_channels = 1;
    720 		strcpy(di->un.v.units.name, AudioNvolume);
    721 		return 0;
    722 	case ASC_INPUT_DAC_VOLUME:
    723 		di->mixer_class = ASC_INPUT_CLASS;
    724 		strcpy(di->label.name, AudioNdac);
    725 		di->type = AUDIO_MIXER_VALUE;
    726 		di->next = di->prev = AUDIO_MIXER_LAST;
    727 		di->un.v.num_channels = 1;
    728 		strcpy(di->un.v.units.name, AudioNvolume);
    729 		return 0;
    730 	}
    731 
    732 	return ENXIO;
    733 }
    734 
    735 static int
    736 ascaudio_get_props(void *opaque)
    737 {
    738 
    739 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
    740 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    741 }
    742 
    743 static int
    744 ascaudio_round_blocksize(void *opaque, int blksize, int mode,
    745     const audio_params_t *p)
    746 {
    747 	ascaudio_softc_t *sc __diagused;
    748 
    749 	sc = (ascaudio_softc_t *)opaque;
    750 	KASSERT(mutex_owned(&sc->sc_lock));
    751 
    752 	if (mode == AUMODE_PLAY)
    753 		return PLAYBLKSIZE;
    754 	else
    755 		return RECBLKSIZE;
    756 }
    757 
    758 static void
    759 ascaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    760 {
    761 	ascaudio_softc_t *sc;
    762 
    763 	sc = (ascaudio_softc_t *)opaque;
    764 
    765 	*intr = &sc->sc_intr_lock;
    766 	*thread = &sc->sc_lock;
    767 }
    768 
    769 static int
    770 ascaudio_intr_est(void *arg)
    771 {
    772 	ascaudio_intr(arg);
    773 
    774 	return 0;
    775 }
    776 
    777 static void
    778 ascaudio_intr(void *arg)
    779 {
    780 	struct ascaudio_softc *sc = arg;
    781 	uint8_t status;
    782 	int8_t val;
    783 	int loc_a, loc_b, total, count, i;
    784 
    785 	if (!sc)
    786 		return;
    787 
    788 	if (!sc->sc_pintr && !sc->sc_rintr)
    789 		return;
    790 
    791 	mutex_enter(&sc->sc_intr_lock);
    792 
    793 	status = bus_space_read_1(sc->sc_tag, sc->sc_handle, FIFOSTATUS);
    794 
    795 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    796 		if (sc->sc_rintr) {
    797 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    798 			    IRQA, DISABLEHALFIRQ);
    799 		}
    800 		if (sc->sc_pintr) {
    801 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    802 			    IRQB, DISABLEHALFIRQ);
    803 		}
    804 	}
    805 
    806 	if (!(status & A_HALF))
    807 		count = 0x200;
    808 	else
    809 		count = 0;
    810 
    811 	if (count && sc->sc_rintr) {
    812 		total = count;
    813 		if (sc->sc_rptr + count >= sc->sc_recbuf + BUFSIZE)
    814 			count = sc->sc_recbuf + BUFSIZE - sc->sc_rptr;
    815 		while (total) {
    816 			if (sc->sc_ver == EASC_VER2) {
    817 				loc_a = FIFO_A_ALT;
    818 				loc_b = FIFO_B_ALT;
    819 			} else {
    820 				loc_a = FIFO_A;
    821 				loc_b = 0;
    822 			}
    823 			for (i = 0; i < count; i++) {
    824 				val = bus_space_read_1(sc->sc_tag,
    825 				    sc->sc_handle, loc_a);
    826 				val ^= 0x80;
    827 				val = val * sc->sc_recvol / 64;
    828 				*sc->sc_rptr++ = val;
    829 				if (sc->sc_pintr == NULL && loc_b) {
    830 					(void)bus_space_read_1
    831 					    (sc->sc_tag, sc->sc_handle, loc_b);
    832 				}
    833 			}
    834 			if (sc->sc_rptr >= sc->sc_recbuf + BUFSIZE)
    835 				sc->sc_rptr = sc->sc_recbuf;
    836 			total -= count;
    837 			sc->sc_recavail += count;
    838 			count = total;
    839 		}
    840 
    841 		if (sc->sc_recavail > BUFSIZE)
    842 			sc->sc_recavail = BUFSIZE;
    843 	}
    844 
    845 	if (sc->sc_pintr == NULL)
    846 		goto more;
    847 
    848 	if (status & B_HALF)
    849 		count = 0x200;
    850 	else
    851 		count = 0;
    852 
    853 	if (sc->sc_slowcpu)
    854 		count /= 2;
    855 
    856 	if (count && sc->sc_avail < count) {
    857 		if (sc->sc_avail) {
    858 			count = sc->sc_avail;
    859 			goto fill_fifo;
    860 		}
    861 		if (sc->sc_pintr) {
    862 			for (i = 0; i < 0x200; i++) {
    863 				if (sc->sc_rintr == NULL) {
    864 					bus_space_write_1(sc->sc_tag,
    865 					    sc->sc_handle, FIFO_A, 0x80);
    866 				}
    867 				bus_space_write_1(sc->sc_tag,
    868 				    sc->sc_handle, FIFO_B, 0x80);
    869 			}
    870 		} else {
    871 			if (sc->sc_slowcpu)
    872 				count *= 2;
    873 			for (i = 0; i < count; i++) {
    874 				bus_space_write_1(sc->sc_tag,
    875 				    sc->sc_handle, FIFO_B, 0x80);
    876 			}
    877 		}
    878 		goto more;
    879 	}
    880 
    881 fill_fifo:
    882 	total = count;
    883 	if (sc->sc_wptr + count >= sc->sc_playbuf + BUFSIZE)
    884 		count = sc->sc_playbuf + BUFSIZE - sc->sc_wptr;
    885 
    886 	while (total) {
    887 		if (sc->sc_slowcpu) {
    888 			for (i = 0; i < count; i++) {
    889 				val = *sc->sc_wptr++;
    890 				val ^= 0x80;
    891 				if (sc->sc_rintr == NULL) {
    892 					bus_space_write_1(sc->sc_tag,
    893 					    sc->sc_handle, FIFO_A, val);
    894 					bus_space_write_1(sc->sc_tag,
    895 					    sc->sc_handle, FIFO_A, val);
    896 				}
    897 				bus_space_write_1(sc->sc_tag,
    898 				    sc->sc_handle, FIFO_B, val);
    899 				bus_space_write_1(sc->sc_tag,
    900 				    sc->sc_handle, FIFO_B, val);
    901 			}
    902 		} else {
    903 			for (i = 0; i < count; i++) {
    904 				val = *sc->sc_wptr++;
    905 				val ^= 0x80;
    906 				if (sc->sc_rintr == NULL) {
    907 					bus_space_write_1(sc->sc_tag,
    908 					    sc->sc_handle, FIFO_A, val);
    909 				}
    910 				bus_space_write_1(sc->sc_tag,
    911 				    sc->sc_handle, FIFO_B, val);
    912 			}
    913 		}
    914 		if (sc->sc_wptr >= sc->sc_playbuf + BUFSIZE)
    915 			sc->sc_wptr = sc->sc_playbuf;
    916 		total -= count;
    917 		sc->sc_avail -= count;
    918 		count = total;
    919 	}
    920 
    921 more:
    922 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    923 		if (sc->sc_rintr)
    924 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA, 0);
    925 		if (sc->sc_pintr)
    926 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, 0);
    927 	}
    928 
    929 	if (sc->sc_pintr && (sc->sc_avail <= PLAYBLKSIZE))
    930 		callout_schedule(&sc->sc_pcallout, 0);
    931 
    932 	if (sc->sc_rintr && (sc->sc_recavail >= RECBLKSIZE))
    933 		callout_schedule(&sc->sc_rcallout, 0);
    934 
    935 	mutex_exit(&sc->sc_intr_lock);
    936 }
    937 
    938 static void
    939 ascaudio_intr_enable(void)
    940 {
    941 	int s;
    942 
    943 	s = splhigh();
    944 	if (VIA2 == VIA2OFF)
    945 		via2_reg(vIER) = 0x80 | V2IF_ASC;
    946 	else
    947 		via2_reg(rIER) = 0x80 | V2IF_ASC;
    948 	splx(s);
    949 }
    950 
    951 static void
    952 ascaudio_done_output(void *arg)
    953 {
    954 	struct ascaudio_softc *sc = arg;
    955 
    956 	mutex_enter(&sc->sc_intr_lock);
    957 	if (sc->sc_pintr)
    958 		(*sc->sc_pintr)(sc->sc_pintrarg);
    959 	mutex_exit(&sc->sc_intr_lock);
    960 }
    961 
    962 static void
    963 ascaudio_done_input(void *arg)
    964 {
    965 	struct ascaudio_softc *sc = arg;
    966 
    967 	mutex_enter(&sc->sc_intr_lock);
    968 	if (sc->sc_rintr)
    969 		(*sc->sc_rintr)(sc->sc_rintrarg);
    970 	mutex_exit(&sc->sc_intr_lock);
    971 }
    972 
    973 static void
    974 configure_dfac(uint8_t config)
    975 {
    976 	int i;
    977 
    978 	if (pmHardware == PM_HW_PB5XX)
    979 		return;		/* These macs use the pm to configure dfac */
    980 
    981 	for (i = 0; i < 8; i++) {
    982 		via_reg(VIA2, vBufB) &= ~DFAC_CLOCK;
    983 
    984 		if (config & 0x1)
    985 			via_reg(VIA2, vBufB) |= DFAC_DATA;
    986 		else
    987 			via_reg(VIA2, vBufB) &= ~DFAC_DATA;
    988 
    989 		via_reg(VIA2, vBufB) |= DFAC_CLOCK;
    990 		config >>= 1;
    991 	}
    992 	via_reg(VIA2, vBufB) &= ~DFAC_CLOCK;
    993 	via_reg(VIA2, vBufB) |= DFAC_LATCH;
    994 	via_reg(VIA2, vBufB) &= ~DFAC_LATCH;
    995 }
    996 
    997 #ifdef _MODULE
    998 
    999 MODULE(MODULE_CLASS_DRIVER, ascaudio, "audio");
   1000 
   1001 static const struct cfiattrdata audiobuscf_iattrdata = {
   1002 	"audiobus", 0, { { NULL, NULL, 0 }, }
   1003 };
   1004 static const struct cfiattrdata * const ascaudio_attrs[] = {
   1005 	&audiobuscf_iattrdata, NULL
   1006 };
   1007 
   1008 CFDRIVER_DECL(ascaudio, DV_DULL, ascaud_attrs);
   1009 extern struct cfattach ascaudio_ca;
   1010 static int ascaudioloc[] = { -1, -1 };
   1011 
   1012 static struct cfdata ascaudio_cfdata[] = {
   1013 	{
   1014 		.cf_name = "ascaudio",
   1015 		.cf_atname = "ascaudio",
   1016 		.cf_unit = 0,
   1017 		.cf_fstate = FSTATE_STAR,
   1018 		.cf_loc = ascaudioloc,
   1019 		.cf_flags = 0,
   1020 		.cf_pspec = NULL,
   1021 	},
   1022 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
   1023 };
   1024 
   1025 static int
   1026 ascaudio_modcmd(modcmd_t cmd, void *arg)
   1027 {
   1028 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
   1029 	int error;
   1030 
   1031 	switch (cmd) {
   1032 	case MODULE_CMD_INIT:
   1033 		error = config_cfdriver_attach(&ascaudio_cd);
   1034 		if (error) {
   1035 			return error;
   1036 		}
   1037 
   1038 		error = config_cfattach_attach(ascaudio_cd.cd_name, &ascaud_ca);
   1039 		if (error) {
   1040 			config_cfdriver_detach(&ascaudio_cd);
   1041 			aprint_error("%s: unable to register cfattach\n",
   1042 				ascaudio_cd.cd_name);
   1043 
   1044 			return error;
   1045 		}
   1046 
   1047 		error = config_cfdata_attach(ascaudio_cfdata, 1);
   1048 		if (error) {
   1049 			config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1050 			config_cfdriver_detach(&ascaudio_cd);
   1051 			aprint_error("%s: unable to register cfdata\n",
   1052 				ascaudio_cd.cd_name);
   1053 
   1054 			return error;
   1055 		}
   1056 
   1057 		error = devsw_attach(ascaudio_cd.cd_name, NULL, &bmajor,
   1058 		    &ascaudio_cdevsw, &cmajor);
   1059 		if (error) {
   1060 			error = config_cfdata_detach(ascaudio_cfdata);
   1061 			if (error) {
   1062 				return error;
   1063 			}
   1064 			config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1065 			config_cfdriver_detach(&ascaudio_cd);
   1066 			aprint_error("%s: unable to register devsw\n",
   1067 				ascaudio_cd.cd_name);
   1068 
   1069 			return error;
   1070 		}
   1071 
   1072 		(void)config_attach_pseudo(ascaudio_cfdata);
   1073 
   1074 		return 0;
   1075 	case MODULE_CMD_FINI:
   1076 		error = config_cfdata_detach(ascaudio_cfdata);
   1077 		if (error) {
   1078 			return error;
   1079 		}
   1080 
   1081 		config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1082 		config_cfdriver_detach(&ascaudio_cd);
   1083 		devsw_detach(NULL, &ascaudio_cdevsw);
   1084 
   1085 		return 0;
   1086 	default:
   1087 		return ENOTTY;
   1088 	}
   1089 }
   1090 
   1091 #endif
   1092