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