Home | History | Annotate | Line # | Download | only in obio
ascaudio.c revision 1.10
      1 /* $NetBSD: ascaudio.c,v 1.10 2025/05/12 00:40:40 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.10 2025/05/12 00:40:40 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			8192
     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(void *);
    110 static int	ascaudio_set_port(void *, mixer_ctrl_t *);
    111 static int	ascaudio_get_port(void *, mixer_ctrl_t *);
    112 static int	ascaudio_getdev(void *, struct audio_device *);
    113 static int	ascaudio_query_devinfo(void *, mixer_devinfo_t *);
    114 static int	ascaudio_get_props(void *);
    115 static int
    116 	    ascaudio_round_blocksize(void *, int, int, const audio_params_t *);
    117 static void	ascaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    118 static void 	ascaudio_intr(void *);
    119 static int 	ascaudio_intr_est(void *);
    120 static void	ascaudio_intr_enable(void);
    121 static void	ascaudio_done_output(void *);
    122 static void	ascaudio_done_input(void *);
    123 static void	configure_dfac(uint8_t);
    124 
    125 static const struct audio_hw_if ascaudio_hw_if = {
    126 	.query_format	 = ascaudio_query_format,
    127 	.set_format	 = ascaudio_set_format,
    128 	.start_output	 = ascaudio_start_output,
    129 	.start_input	 = ascaudio_start_input,
    130 	.halt_output	 = ascaudio_halt,
    131 	.halt_input	 = ascaudio_halt,
    132 	.set_port	 = ascaudio_set_port,
    133 	.get_port	 = ascaudio_get_port,
    134 	.getdev		 = ascaudio_getdev,
    135 	.query_devinfo	 = ascaudio_query_devinfo,
    136 	.get_props 	 = ascaudio_get_props,
    137 	.round_blocksize = ascaudio_round_blocksize,
    138 	.get_locks	 = ascaudio_get_locks,
    139 };
    140 
    141 enum {
    142 	ASC_OUTPUT_CLASS,
    143 	ASC_INPUT_CLASS,
    144 	ASC_OUTPUT_MASTER_VOLUME,
    145 	ASC_INPUT_DAC_VOLUME,
    146 	ASC_ENUM_LAST,
    147 };
    148 
    149 static int
    150 ascaudiomatch(device_t parent, cfdata_t cf, void *aux)
    151 {
    152 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    153 	bus_addr_t addr;
    154 	bus_space_handle_t bsh;
    155 	int rval = 0;
    156 
    157 	if (oa->oa_addr != (-1))
    158 		addr = (bus_addr_t)oa->oa_addr;
    159 	else if (current_mac_model->machineid == MACH_MACTV)
    160 		return 0;
    161 	else if (current_mac_model->machineid == MACH_MACIIFX)
    162 		addr = (bus_addr_t)MAC68K_IIFX_ASCAUDIO_BASE;
    163 	else
    164 		addr = (bus_addr_t)MAC68K_ASCAUDIO_BASE;
    165 
    166 	if (bus_space_map(oa->oa_tag, addr, MAC68K_ASCAUDIO_LEN, 0, &bsh))
    167 		return (0);
    168 
    169 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
    170 		rval = 1;
    171 	} else
    172 		rval = 0;
    173 
    174 	bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASCAUDIO_LEN);
    175 
    176 	return rval;
    177 }
    178 
    179 static void
    180 ascaudioattach(device_t parent, device_t self, void *aux)
    181 {
    182 	struct ascaudio_softc *sc = device_private(self);
    183 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    184 	bus_addr_t addr;
    185 	uint8_t tmp;
    186 
    187 	sc->sc_dev = self;
    188 	sc->sc_tag = oa->oa_tag;
    189 
    190 	if (oa->oa_addr != (-1))
    191 		addr = (bus_addr_t)oa->oa_addr;
    192 	else if (current_mac_model->machineid == MACH_MACIIFX)
    193 		addr = (bus_addr_t)MAC68K_IIFX_ASCAUDIO_BASE;
    194 	else
    195 		addr = (bus_addr_t)MAC68K_ASCAUDIO_BASE;
    196 	if (bus_space_map(sc->sc_tag, addr, MAC68K_ASCAUDIO_LEN, 0,
    197 	    &sc->sc_handle)) {
    198 		printf(": can't map memory space\n");
    199 		return;
    200 	}
    201 
    202 	/* Pull in the options flags. */
    203 	sc->sc_options = ((device_cfdata(self)->cf_flags) &
    204 			    ASCAUDIO_OPTIONS_MASK);
    205 
    206 	sc->sc_playbuf = kmem_alloc(BUFSIZE, KM_SLEEP);
    207 	sc->sc_recbuf = kmem_alloc(BUFSIZE, KM_SLEEP);
    208 	sc->sc_rptr = sc->sc_recbuf;
    209 	sc->sc_getptr = sc->sc_recbuf;
    210 	sc->sc_wptr = sc->sc_playbuf;
    211 	sc->sc_putptr = sc->sc_playbuf;
    212 
    213 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    214 
    215 	sc->sc_ver = bus_space_read_1(oa->oa_tag, sc->sc_handle, 0x800);
    216 
    217 	if (sc->sc_options & HIGHQUALITY) {
    218 		tmp = bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCRATE);
    219 		switch (tmp) {
    220 			case 2:
    221 				sc->sc_rate = 22050;
    222 				break;
    223 			case 3:
    224 				sc->sc_rate = 44100;
    225 				break;
    226 			default:
    227 				sc->sc_rate = 22254;
    228 				break;
    229 		}
    230 
    231 		tmp = bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCTRL);
    232 		if (tmp & STEREO)
    233 			sc->sc_speakers = 2;
    234 		else
    235 			sc->sc_speakers = 1;
    236 
    237 	} else {
    238 		__USE(tmp);
    239 		sc->sc_rate = 22254;
    240 		sc->sc_speakers = 1;
    241 	}
    242 
    243 	if (sc->sc_options & LOWQUALITY) {
    244 		sc->sc_slowcpu = true;
    245 		if (sc->sc_slowcpu)
    246 			sc->sc_rate /= 2;
    247 	}
    248 
    249 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    250 		printf(": Enhanced Apple Sound Chip");
    251 	else
    252 		printf(": Apple Sound Chip");
    253 
    254 	if (oa->oa_addr != (-1))
    255 		printf(" at %x", oa->oa_addr);
    256 	printf("\n");
    257 
    258 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    259 
    260 	if (mac68k_machine.aux_interrupts) {
    261 		intr_establish(ascaudio_intr_est, sc, ASCIRQ);
    262 	} else {
    263 		via2_register_irq(VIA2_ASC, ascaudio_intr, sc);
    264 	}
    265 	ascaudio_intr_enable();
    266 
    267 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    268 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    269 	callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
    270 	callout_setfunc(&sc->sc_pcallout, ascaudio_done_output, sc);
    271 	callout_init(&sc->sc_rcallout, CALLOUT_MPSAFE);
    272 	callout_setfunc(&sc->sc_rcallout, ascaudio_done_input, sc);
    273 
    274 	sc->sc_vol = 180;
    275 	sc->sc_recvol = 255;
    276 
    277 	sc->sc_audiodev = audio_attach_mi(&ascaudio_hw_if, sc, sc->sc_dev);
    278 
    279 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    280 		aprint_error_dev(sc->sc_dev,
    281 		    "couldn't establish power handler\n");
    282 
    283 	if (sc->sc_ver != EASC_VER && sc->sc_ver != EASC_VER2)
    284 		return;
    285 
    286 	if (sc->sc_options & HIGHQUALITY)
    287 		tmp = CDQUALITY;
    288 	else
    289 		tmp = MACDEFAULTS;
    290 
    291 	bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOCTRLA, tmp);
    292 	bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOCTRLB, tmp);
    293 
    294 }
    295 
    296 int
    297 ascaudioopen(dev_t dev, int flag, int mode, struct lwp *l)
    298 {
    299 	struct ascaudio_softc *sc;
    300 
    301 	sc = device_lookup_private(&ascaudio_cd, ASCAUDIOUNIT(dev));
    302 	if (sc == NULL)
    303 		return (ENXIO);
    304 	if (sc->sc_open)
    305 		return (EBUSY);
    306 	sc->sc_open = 1;
    307 
    308 	return (0);
    309 }
    310 
    311 int
    312 ascaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
    313 {
    314 	struct ascaudio_softc *sc;
    315 
    316 	sc = device_lookup_private(&ascaudio_cd, ASCAUDIOUNIT(dev));
    317 	sc->sc_open = 0;
    318 
    319 	return (0);
    320 }
    321 
    322 int
    323 ascaudioread(dev_t dev, struct uio *uio, int ioflag)
    324 {
    325 	return (ENXIO);
    326 }
    327 
    328 int
    329 ascaudiowrite(dev_t dev, struct uio *uio, int ioflag)
    330 {
    331 	return (ENXIO);
    332 }
    333 
    334 int
    335 ascaudioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    336 {
    337 	int error;
    338 #ifdef notyet
    339 	struct ascaudio_softc *sc;
    340 	int unit = ASCAUDIOUNIT(dev);
    341 
    342 	sc = device_lookup_private(&ascaudio_cd, unit);
    343 #endif
    344 	error = 0;
    345 
    346 	switch (cmd) {
    347 	default:
    348 		error = EINVAL;
    349 		break;
    350 	}
    351 	return (error);
    352 }
    353 
    354 #define ASCAUDIO_NFORMATS	2
    355 static int
    356 ascaudio_query_format(void *opaque, struct audio_format_query *ae)
    357 {
    358 	struct ascaudio_softc *sc = opaque;
    359 
    360 	const struct audio_format asc_formats[ASCAUDIO_NFORMATS] = {
    361 	      { .mode		= AUMODE_PLAY,
    362 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    363 		.validbits	= 8,
    364 		.precision	= 8,
    365 		.channels	= sc->sc_speakers,
    366 		.channel_mask	= sc->sc_speakers == 2 ? AUFMT_STEREO :
    367 		    AUFMT_MONAURAL,
    368 		.frequency_type	= 1,
    369 		.frequency	= { sc->sc_rate }, },
    370 	      { .mode		= AUMODE_RECORD,
    371 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    372 		.validbits	= 8,
    373 		.precision	= 8,
    374 		.channels	= 1,
    375 		.channel_mask	= AUFMT_MONAURAL,
    376 		.frequency_type	= 1,
    377 		.frequency	= { 11025 }, }
    378 	};
    379 
    380 	return audio_query_format(asc_formats, ASCAUDIO_NFORMATS, ae);
    381 }
    382 
    383 static int
    384 ascaudio_set_format(void *opaque, int setmode,
    385     const audio_params_t *play, const audio_params_t *rec,
    386     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    387 {
    388 	struct ascaudio_softc *sc = opaque;
    389 
    390 	KASSERT(mutex_owned(&sc->sc_lock));
    391 
    392 	return 0;
    393 }
    394 
    395 static int
    396 ascaudio_start_output(void *opaque, void *block, int blksize,
    397     void (*intr)(void *), void *intrarg)
    398 {
    399 	struct ascaudio_softc *sc;
    400 	uint8_t *loc, tmp;
    401 	int total;
    402 
    403 	sc = (struct ascaudio_softc *)opaque;
    404 	if (!sc)
    405 		return (ENODEV);
    406 
    407 	sc->sc_pintr = intr;
    408 	sc->sc_pintrarg = intrarg;
    409 
    410 	loc = block;
    411  	if (bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCMODE) !=
    412 								 MODEFIFO) {
    413 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    414 
    415 		if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    416 			/* disable half interrupts channel a */
    417 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA,
    418 			    DISABLEHALFIRQ);
    419 			/* Disable half interrupts channel b */
    420 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB,
    421 			    DISABLEHALFIRQ);
    422 			configure_dfac(DFAC_DISABLE);
    423 		}
    424 
    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 		    CLEARFIFO);
    428 		tmp = 0;
    429 		bus_space_write_1(sc->sc_tag, sc->sc_handle, APLAYREC, tmp);
    430 
    431 		if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    432 			/* enable interrupts channel b */
    433 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, 0);
    434 		}
    435 	}
    436 
    437 	/* set the volume. */
    438 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    439 		/* DO NOT CHANGE THESE VALUES UNLESS TESTED.
    440 		   CAN BE VERY LOUD!!!! */
    441 		tmp = sc->sc_vol >> 5;
    442 		KASSERT(tmp <= MACOS_HIGH_VOL);
    443  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_LEFT_VOL, tmp);
    444  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_LEFT_VOL, tmp);
    445  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_RIGHT_VOL, tmp);
    446  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_RIGHT_VOL, tmp);
    447 	}
    448 	bus_space_write_1(sc->sc_tag, sc->sc_handle, INTVOL, sc->sc_vol);
    449 
    450 	total = blksize;
    451 	if (sc->sc_putptr + blksize >= sc->sc_playbuf + BUFSIZE)
    452 		total = sc->sc_playbuf + BUFSIZE - sc->sc_putptr;
    453 
    454 	if (total) {
    455 		memcpy(sc->sc_putptr, loc, total);
    456 		sc->sc_putptr += total;
    457 		loc += total;
    458 	}
    459 
    460 	total = blksize - total;
    461 	if (total) {
    462 		sc->sc_putptr = sc->sc_playbuf;
    463 		memcpy(sc->sc_playbuf, loc, total);
    464 		sc->sc_putptr += total;
    465 	}
    466 
    467 	sc->sc_avail += blksize;
    468 	if (sc->sc_avail > BUFSIZE)
    469 		sc->sc_avail = BUFSIZE;
    470 
    471 	/* start fifo playback */
    472 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODEFIFO);
    473 
    474 	return 0;
    475 }
    476 
    477 static int
    478 ascaudio_start_input(void *opaque, void *block, int blksize,
    479     void (*intr)(void *), void *intrarg)
    480 {
    481 	struct ascaudio_softc *sc;
    482 	uint8_t tmp;
    483 	int total;
    484 
    485 	sc = (struct ascaudio_softc *)opaque;
    486 	if (!sc)
    487 		return (ENODEV);
    488 
    489 	uint8_t *loc;
    490 	loc = block;
    491 
    492 	sc->sc_rintr = intr;
    493 	sc->sc_rintrarg = intrarg;
    494 
    495  	if (bus_space_read_1(sc->sc_tag, sc->sc_handle, ASCMODE) !=
    496 								 MODEFIFO) {
    497 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    498 
    499 		if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    500 			/* disable half interrupts channel a */
    501 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA,
    502 			    DISABLEHALFIRQ);
    503 			/* Disable half interrupts channel b */
    504 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB,
    505 			    DISABLEHALFIRQ);
    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 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCTEST, 0);
    514 
    515 		/* start fifo playback */
    516 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODEFIFO);
    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 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    524 		    CLEARFIFO);
    525 
    526 #if 0
    527 		bus_space_write_4(sc->sc_tag, sc->sc_handle, FIFO_A_ALT, 0);
    528 		bus_space_write_4(sc->sc_tag, sc->sc_handle, FIFO_B_ALT, 0);
    529 #endif
    530 
    531 		bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM,
    532 		    CLEARFIFO | NONCOMP);
    533 
    534 		tmp = RECORDA;
    535 		bus_space_write_1(sc->sc_tag, sc->sc_handle, APLAYREC, tmp);
    536 
    537 #if 0
    538 		int i;
    539 		for (i = 0; i < 0x400; i++) {
    540 			bus_space_read_1(sc->sc_tag, sc->sc_handle, FIFO_A);
    541 			bus_space_read_1(sc->sc_tag, sc->sc_handle, FIFO_B);
    542 		}
    543 #endif
    544 
    545 		memset(loc, 0x80, blksize);
    546 
    547 		return 0;
    548 	}
    549 
    550 	/* set the volume. */
    551 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2) {
    552 		/* DO NOT CHANGE THESE VALUES UNLESS TESTED.
    553 		   CAN BE VERY LOUD!!!! */
    554 		tmp = sc->sc_recvol >> 5;
    555 		KASSERT(tmp <= MACOS_HIGH_VOL);
    556  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_LEFT_VOL, tmp);
    557  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_LEFT_VOL, tmp);
    558  		bus_space_write_1(sc->sc_tag, sc->sc_handle, A_RIGHT_VOL, tmp);
    559  		bus_space_write_1(sc->sc_tag, sc->sc_handle, B_RIGHT_VOL, tmp);
    560 	}
    561 	bus_space_write_1(sc->sc_tag, sc->sc_handle, INTVOL, sc->sc_recvol);
    562 
    563 	total = blksize;
    564 	if (sc->sc_getptr + blksize >= sc->sc_recbuf + BUFSIZE)
    565 		total = sc->sc_recbuf + BUFSIZE - sc->sc_getptr;
    566 
    567 	if (total) {
    568 		memcpy(loc, sc->sc_getptr, total);
    569 		sc->sc_getptr += total;
    570 		loc += total;
    571 	}
    572 
    573 	total = blksize - total;
    574 	if (total) {
    575 		sc->sc_getptr = sc->sc_recbuf;
    576 		memcpy(loc, sc->sc_getptr, total);
    577 		sc->sc_getptr += total;
    578 	}
    579 
    580 	sc->sc_recavail -= blksize;
    581 
    582 	return 0;
    583 }
    584 
    585 static int
    586 ascaudio_halt(void *opaque)
    587 {
    588 	ascaudio_softc_t *sc;
    589 
    590 	sc = (ascaudio_softc_t *)opaque;
    591 
    592 	KASSERT(mutex_owned(&sc->sc_lock));
    593 
    594 	callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
    595 	callout_halt(&sc->sc_rcallout, &sc->sc_intr_lock);
    596 
    597 	bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE, MODESTOP);
    598 
    599 	sc->sc_pintr = NULL;
    600 	sc->sc_pintrarg = NULL;
    601 	sc->sc_rintr = NULL;
    602 	sc->sc_rintrarg = NULL;
    603 
    604 	sc->sc_avail = 0;
    605 	sc->sc_recavail = 0;
    606 
    607 	bus_space_write_1(sc->sc_tag, sc->sc_handle, FIFOPARAM, CLEARFIFO);
    608 
    609 	sc->sc_rptr = sc->sc_recbuf;
    610 	sc->sc_getptr = sc->sc_recbuf;
    611 	sc->sc_wptr = sc->sc_playbuf;
    612 	sc->sc_putptr = sc->sc_playbuf;
    613 
    614 	if (sc->sc_ver != EASC_VER && sc->sc_ver != EASC_VER2)
    615 		return 0;
    616 
    617 	configure_dfac(DFAC_DISABLE);
    618 
    619 	/* disable half interrupts channel a */
    620 	bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA, DISABLEHALFIRQ);
    621 	/* disable half interrupts channel b */
    622 	bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, DISABLEHALFIRQ);
    623 
    624 	return 0;
    625 }
    626 
    627 static int
    628 ascaudio_getdev(void *opaque, struct audio_device *ret)
    629 {
    630 	strlcpy(ret->name, "Apple ASC Audio", sizeof(ret->name));
    631 	strlcpy(ret->version, osrelease, sizeof(ret->version));
    632 	strlcpy(ret->config, "ascaudio", sizeof(ret->config));
    633 
    634 	return 0;
    635 }
    636 
    637 static int
    638 ascaudio_set_port(void *opaque, mixer_ctrl_t *mc)
    639 {
    640 	struct ascaudio_softc *sc = opaque;
    641 
    642 	KASSERT(mutex_owned(&sc->sc_lock));
    643 
    644 	switch (mc->dev) {
    645 	case ASC_OUTPUT_MASTER_VOLUME:
    646 		if (mc->un.value.num_channels != 1)
    647 			return EINVAL;
    648 		sc->sc_vol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    649 		return 0;
    650 	case ASC_INPUT_DAC_VOLUME:
    651 		if (mc->un.value.num_channels != 1)
    652 			return EINVAL;
    653 		sc->sc_recvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    654 		return 0;
    655 	}
    656 
    657 	return ENXIO;
    658 }
    659 
    660 static int
    661 ascaudio_get_port(void *opaque, mixer_ctrl_t *mc)
    662 {
    663 	struct ascaudio_softc *sc = opaque;
    664 
    665 	KASSERT(mutex_owned(&sc->sc_lock));
    666 
    667 	switch (mc->dev) {
    668 	case ASC_OUTPUT_MASTER_VOLUME:
    669 		if (mc->un.value.num_channels != 1)
    670 			return EINVAL;
    671 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_vol;
    672 		return 0;
    673 	case ASC_INPUT_DAC_VOLUME:
    674 		if (mc->un.value.num_channels != 1)
    675 			return EINVAL;
    676 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_recvol;
    677 		return 0;
    678 	}
    679 
    680 	return ENXIO;
    681 }
    682 
    683 static int
    684 ascaudio_query_devinfo(void *opaque, mixer_devinfo_t *di)
    685 {
    686 	ascaudio_softc_t *sc __diagused;
    687 
    688 	sc = (ascaudio_softc_t *)opaque;
    689 
    690 	KASSERT(mutex_owned(&sc->sc_lock));
    691 
    692 	switch (di->index) {
    693 	case ASC_OUTPUT_CLASS:
    694 		di->mixer_class = ASC_OUTPUT_CLASS;
    695 		strcpy(di->label.name, AudioCoutputs);
    696 		di->type = AUDIO_MIXER_CLASS;
    697 		di->next = di->prev = AUDIO_MIXER_LAST;
    698 		return 0;
    699 	case ASC_INPUT_CLASS:
    700 		di->mixer_class = ASC_INPUT_CLASS;
    701 		strcpy(di->label.name, AudioCinputs);
    702 		di->type = AUDIO_MIXER_CLASS;
    703 		di->next = di->prev = AUDIO_MIXER_LAST;
    704 		return 0;
    705 	case ASC_OUTPUT_MASTER_VOLUME:
    706 		di->mixer_class = ASC_OUTPUT_CLASS;
    707 		strcpy(di->label.name, AudioNmaster);
    708 		di->type = AUDIO_MIXER_VALUE;
    709 		di->next = di->prev = AUDIO_MIXER_LAST;
    710 		di->un.v.num_channels = 1;
    711 		strcpy(di->un.v.units.name, AudioNvolume);
    712 		return 0;
    713 	case ASC_INPUT_DAC_VOLUME:
    714 		di->mixer_class = ASC_INPUT_CLASS;
    715 		strcpy(di->label.name, AudioNdac);
    716 		di->type = AUDIO_MIXER_VALUE;
    717 		di->next = di->prev = AUDIO_MIXER_LAST;
    718 		di->un.v.num_channels = 1;
    719 		strcpy(di->un.v.units.name, AudioNvolume);
    720 		return 0;
    721 	}
    722 
    723 	return ENXIO;
    724 }
    725 
    726 static int
    727 ascaudio_get_props(void *opaque)
    728 {
    729 
    730 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
    731 	    AUDIO_PROP_INDEPENDENT;
    732 }
    733 
    734 static int
    735 ascaudio_round_blocksize(void *opaque, int blksize, int mode,
    736     const audio_params_t *p)
    737 {
    738 	ascaudio_softc_t *sc __diagused;
    739 
    740 	sc = (ascaudio_softc_t *)opaque;
    741 	KASSERT(mutex_owned(&sc->sc_lock));
    742 
    743 	if (mode == AUMODE_PLAY)
    744 		return PLAYBLKSIZE;
    745 	else
    746 		return RECBLKSIZE;
    747 }
    748 
    749 static void
    750 ascaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    751 {
    752 	ascaudio_softc_t *sc;
    753 
    754 	sc = (ascaudio_softc_t *)opaque;
    755 
    756 	*intr = &sc->sc_intr_lock;
    757 	*thread = &sc->sc_lock;
    758 }
    759 
    760 static int
    761 ascaudio_intr_est(void *arg)
    762 {
    763 	ascaudio_intr(arg);
    764 
    765 	return 0;
    766 }
    767 
    768 static void
    769 ascaudio_intr(void *arg)
    770 {
    771 	struct ascaudio_softc *sc = arg;
    772 	int8_t val;
    773 	int loc_a, loc_b, total, count, i;
    774 
    775 	if (!sc)
    776 		return;
    777 
    778 	if (!sc->sc_pintr && !sc->sc_rintr)
    779 		return;
    780 
    781 	mutex_enter(&sc->sc_intr_lock);
    782 
    783 	count = 0x200;
    784 	if (sc->sc_rintr) {
    785 		if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    786 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    787 			    IRQA, DISABLEHALFIRQ);
    788 
    789 		total = count;
    790 		if (sc->sc_rptr + count >= sc->sc_recbuf + BUFSIZE)
    791 			count = sc->sc_recbuf + BUFSIZE - sc->sc_rptr;
    792 		while (total) {
    793 			if (sc->sc_ver == EASC_VER2) {
    794 				loc_a = FIFO_A_ALT;
    795 				loc_b = FIFO_B_ALT;
    796 			} else {
    797 				loc_a = FIFO_A;
    798 				loc_b = 0;
    799 			}
    800 			for (i = 0; i < count; i++) {
    801 				val = bus_space_read_1(sc->sc_tag,
    802 				    sc->sc_handle, loc_a);
    803 				val ^= 0x80;
    804 				val = val * sc->sc_recvol / 64;
    805 				*sc->sc_rptr++ = val;
    806 				if (loc_b) {
    807 					(void)bus_space_read_1
    808 					    (sc->sc_tag, sc->sc_handle, loc_b);
    809 				}
    810 			}
    811 			if (sc->sc_rptr >= sc->sc_recbuf + BUFSIZE)
    812 				sc->sc_rptr = sc->sc_recbuf;
    813 			total -= count;
    814 			sc->sc_recavail += count;
    815 			count = total;
    816 		}
    817 
    818 		if (sc->sc_recavail > BUFSIZE)
    819 			sc->sc_recavail = BUFSIZE;
    820 
    821 		if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    822 			bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQA, 0);
    823 
    824 		goto more;
    825 	}
    826 
    827 	count = 0x200;
    828 	if (sc->sc_slowcpu)
    829 		count /= 2;
    830 
    831 	if (sc->sc_avail < count) {
    832 		if (sc->sc_avail) {
    833 			count = sc->sc_avail;
    834 			goto fill_fifo;
    835 		}
    836 		if (sc->sc_pintr) {
    837 			for (i = 0; i < 0x200; i++) {
    838 				bus_space_write_1(sc->sc_tag,
    839 				    sc->sc_handle, FIFO_A, 0x80);
    840 				bus_space_write_1(sc->sc_tag,
    841 				    sc->sc_handle, FIFO_B, 0x80);
    842 			}
    843 		} else {
    844 			if (sc->sc_slowcpu)
    845 				count *= 2;
    846 			for (i = 0; i < count; i++) {
    847 				bus_space_write_1(sc->sc_tag,
    848 				    sc->sc_handle, FIFO_B, 0x80);
    849 			}
    850 		}
    851 		goto more;
    852 	}
    853 
    854 fill_fifo:
    855 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    856 		bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB,
    857 		    DISABLEHALFIRQ);
    858 
    859 	total = count;
    860 	if (sc->sc_wptr + count >= sc->sc_playbuf + BUFSIZE)
    861 		count = sc->sc_playbuf + BUFSIZE - sc->sc_wptr;
    862 
    863 	while (total) {
    864 		if (sc->sc_slowcpu) {
    865 			for (i = 0; i < count; i++) {
    866 				val = *sc->sc_wptr++;
    867 				val ^= 0x80;
    868 				bus_space_write_1(sc->sc_tag,
    869 				    sc->sc_handle, FIFO_A, val);
    870 				bus_space_write_1(sc->sc_tag,
    871 				    sc->sc_handle, FIFO_B, val);
    872 				bus_space_write_1(sc->sc_tag,
    873 				    sc->sc_handle, FIFO_A, val);
    874 				bus_space_write_1(sc->sc_tag,
    875 				    sc->sc_handle, FIFO_B, val);
    876 			}
    877 		} else {
    878 			for (i = 0; i < count; i++) {
    879 				val = *sc->sc_wptr++;
    880 				val ^= 0x80;
    881 				bus_space_write_1(sc->sc_tag,
    882 				    sc->sc_handle, FIFO_A, val);
    883 				bus_space_write_1(sc->sc_tag,
    884 				    sc->sc_handle, FIFO_B, val);
    885 			}
    886 		}
    887 		if (sc->sc_wptr >= sc->sc_playbuf + BUFSIZE)
    888 			sc->sc_wptr = sc->sc_playbuf;
    889 		total -= count;
    890 		sc->sc_avail -= count;
    891 		count = total;
    892 	}
    893 	if (sc->sc_ver == EASC_VER || sc->sc_ver == EASC_VER2)
    894 		bus_space_write_1(sc->sc_tag, sc->sc_handle, IRQB, 0);
    895 
    896 more:
    897 	if (sc->sc_pintr && (sc->sc_avail <= PLAYBLKSIZE))
    898 		callout_schedule(&sc->sc_pcallout, 0);
    899 
    900 	if (sc->sc_rintr && (sc->sc_recavail >= RECBLKSIZE))
    901 		callout_schedule(&sc->sc_rcallout, 0);
    902 
    903 	mutex_exit(&sc->sc_intr_lock);
    904 }
    905 
    906 static void
    907 ascaudio_intr_enable(void)
    908 {
    909 	int s;
    910 
    911 	s = splhigh();
    912 	if (VIA2 == VIA2OFF)
    913 		via2_reg(vIER) = 0x80 | V2IF_ASC;
    914 	else
    915 		via2_reg(rIER) = 0x80 | V2IF_ASC;
    916 	splx(s);
    917 }
    918 
    919 static void
    920 ascaudio_done_output(void *arg)
    921 {
    922 	struct ascaudio_softc *sc = arg;
    923 
    924 	mutex_enter(&sc->sc_intr_lock);
    925 	if (sc->sc_pintr)
    926 		(*sc->sc_pintr)(sc->sc_pintrarg);
    927 	mutex_exit(&sc->sc_intr_lock);
    928 }
    929 
    930 static void
    931 ascaudio_done_input(void *arg)
    932 {
    933 	struct ascaudio_softc *sc = arg;
    934 
    935 	mutex_enter(&sc->sc_intr_lock);
    936 	if (sc->sc_rintr)
    937 		(*sc->sc_rintr)(sc->sc_rintrarg);
    938 	mutex_exit(&sc->sc_intr_lock);
    939 }
    940 static void
    941 configure_dfac(uint8_t config)
    942 {
    943 	int i;
    944 
    945 	if (pmHardware == PM_HW_PB5XX)
    946 		return;		/* These macs use the pm to configure dfac */
    947 
    948 	for (i = 0; i < 8; i++) {
    949 		via_reg(VIA2, vBufB) &= ~DFAC_CLOCK;
    950 
    951 		if (config & 0x1)
    952 			via_reg(VIA2, vBufB) |= DFAC_DATA;
    953 		else
    954 			via_reg(VIA2, vBufB) &= ~DFAC_DATA;
    955 
    956 		via_reg(VIA2, vBufB) |= DFAC_CLOCK;
    957 		config >>= 1;
    958 	}
    959 	via_reg(VIA2, vBufB) &= ~DFAC_CLOCK;
    960 	via_reg(VIA2, vBufB) |= DFAC_LATCH;
    961 	via_reg(VIA2, vBufB) &= ~DFAC_LATCH;
    962 }
    963 
    964 #ifdef _MODULE
    965 
    966 MODULE(MODULE_CLASS_DRIVER, ascaudio, "audio");
    967 
    968 static const struct cfiattrdata audiobuscf_iattrdata = {
    969 	"audiobus", 0, { { NULL, NULL, 0 }, }
    970 };
    971 static const struct cfiattrdata * const ascaudio_attrs[] = {
    972 	&audiobuscf_iattrdata, NULL
    973 };
    974 
    975 CFDRIVER_DECL(ascaudio, DV_DULL, ascaud_attrs);
    976 extern struct cfattach ascaudio_ca;
    977 static int ascaudioloc[] = { -1, -1 };
    978 
    979 static struct cfdata ascaudio_cfdata[] = {
    980 	{
    981 		.cf_name = "ascaudio",
    982 		.cf_atname = "ascaudio",
    983 		.cf_unit = 0,
    984 		.cf_fstate = FSTATE_STAR,
    985 		.cf_loc = ascaudioloc,
    986 		.cf_flags = 0,
    987 		.cf_pspec = NULL,
    988 	},
    989 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    990 };
    991 
    992 static int
    993 ascaudio_modcmd(modcmd_t cmd, void *arg)
    994 {
    995 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    996 	int error;
    997 
    998 	switch (cmd) {
    999 	case MODULE_CMD_INIT:
   1000 		error = config_cfdriver_attach(&ascaudio_cd);
   1001 		if (error) {
   1002 			return error;
   1003 		}
   1004 
   1005 		error = config_cfattach_attach(ascaudio_cd.cd_name, &ascaud_ca);
   1006 		if (error) {
   1007 			config_cfdriver_detach(&ascaudio_cd);
   1008 			aprint_error("%s: unable to register cfattach\n",
   1009 				ascaudio_cd.cd_name);
   1010 
   1011 			return error;
   1012 		}
   1013 
   1014 		error = config_cfdata_attach(ascaudio_cfdata, 1);
   1015 		if (error) {
   1016 			config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1017 			config_cfdriver_detach(&ascaudio_cd);
   1018 			aprint_error("%s: unable to register cfdata\n",
   1019 				ascaudio_cd.cd_name);
   1020 
   1021 			return error;
   1022 		}
   1023 
   1024 		error = devsw_attach(ascaudio_cd.cd_name, NULL, &bmajor,
   1025 		    &ascaudio_cdevsw, &cmajor);
   1026 		if (error) {
   1027 			error = config_cfdata_detach(ascaudio_cfdata);
   1028 			if (error) {
   1029 				return error;
   1030 			}
   1031 			config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1032 			config_cfdriver_detach(&ascaudio_cd);
   1033 			aprint_error("%s: unable to register devsw\n",
   1034 				ascaudio_cd.cd_name);
   1035 
   1036 			return error;
   1037 		}
   1038 
   1039 		(void)config_attach_pseudo(ascaudio_cfdata);
   1040 
   1041 		return 0;
   1042 	case MODULE_CMD_FINI:
   1043 		error = config_cfdata_detach(ascaudio_cfdata);
   1044 		if (error) {
   1045 			return error;
   1046 		}
   1047 
   1048 		config_cfattach_detach(ascaudio_cd.cd_name, &ascaud_ca);
   1049 		config_cfdriver_detach(&ascaudio_cd);
   1050 		devsw_detach(NULL, &ascaudio_cdevsw);
   1051 
   1052 		return 0;
   1053 	default:
   1054 		return ENOTTY;
   1055 	}
   1056 }
   1057 
   1058 #endif
   1059