Home | History | Annotate | Line # | Download | only in dev
ucbsnd.c revision 1.11
      1 /*	$NetBSD: ucbsnd.c,v 1.11 2002/10/02 05:26:47 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
     41  *	Audio codec part.
     42  *
     43  * /dev/ucbsnd0 : sampling rate 22.154kHz monoral 16bit straight PCM device.
     44  */
     45 
     46 #include "opt_use_poll.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/conf.h>
     51 #include <sys/malloc.h>
     52 #include <sys/device.h>
     53 #include <sys/proc.h>
     54 #include <sys/endian.h>
     55 
     56 #include <mips/cache.h>
     57 
     58 #include <machine/bus.h>
     59 #include <machine/intr.h>
     60 
     61 #include <hpcmips/tx/tx39var.h>
     62 #include <hpcmips/tx/tx39sibvar.h>
     63 #include <hpcmips/tx/tx39sibreg.h>
     64 #include <hpcmips/tx/tx39icureg.h>
     65 #include <hpcmips/tx/txsnd.h>
     66 
     67 #include <hpcmips/dev/ucb1200var.h>
     68 #include <hpcmips/dev/ucb1200reg.h>
     69 
     70 #define AUDIOUNIT(x)		(minor(x)&0x0f)
     71 #define AUDIODEV(x)		(minor(x)&0xf0)
     72 #define	splaudio	splbio	/* XXX */
     73 
     74 #ifdef UCBSNDDEBUG
     75 int	ucbsnd_debug = 1;
     76 #define	DPRINTF(arg) if (ucbsnd_debug) printf arg;
     77 #define	DPRINTFN(n, arg) if (ucbsnd_debug > (n)) printf arg;
     78 #else
     79 #define	DPRINTF(arg)
     80 #define DPRINTFN(n, arg)
     81 #endif
     82 
     83 #define UCBSND_BUFBLOCK		5
     84 /*
     85  * XXX temporary DMA buffer
     86  */
     87 static u_int8_t dmabuf_static[TX39_SIBDMA_SIZE * UCBSND_BUFBLOCK] __attribute__((__aligned__(16))); /* XXX */
     88 static size_t	dmabufcnt_static[UCBSND_BUFBLOCK]; /* XXX */
     89 
     90 enum ucbsnd_state {
     91 /* 0 */	UCBSND_IDLE,
     92 /* 1 */	UCBSND_INIT,
     93 /* 2 */ UCBSND_ENABLE_SAMPLERATE,
     94 /* 3 */ UCBSND_ENABLE_OUTPUTPATH,
     95 /* 4 */ UCBSND_ENABLE_SETVOLUME,
     96 /* 5 */ UCBSND_ENABLE_SPEAKER0,
     97 /* 6 */ UCBSND_ENABLE_SPEAKER1,
     98 /* 7 */ UCBSND_TRANSITION_PIO,
     99 /* 8 */ UCBSND_PIO,
    100 /* 9 */ UCBSND_TRANSITION_DISABLE,
    101 /*10 */ UCBSND_DISABLE_OUTPUTPATH,
    102 /*11 */ UCBSND_DISABLE_SPEAKER0,
    103 /*12 */ UCBSND_DISABLE_SPEAKER1,
    104 /*13 */	UCBSND_DISABLE_SIB,
    105 /*14 */ UCBSND_DMASTART,
    106 /*15 */ UCBSND_DMAEND,
    107 };
    108 
    109 struct ring_buf {
    110 	u_int32_t rb_buf;	/* buffer start address */
    111 	size_t	*rb_bufcnt;	/* effective data count (max rb_blksize)*/
    112 
    113 	size_t	rb_bufsize;	/* total amount of buffer */
    114 	int	rb_blksize;	/* DMA block size */
    115 	int	rb_maxblks;	/* # of blocks in ring */
    116 
    117 	int	rb_inp;		/* start of input (to buffer) */
    118 	int	rb_outp;	/* output pointer */
    119 };
    120 
    121 struct ucbsnd_softc {
    122 	struct device		sc_dev;
    123 	struct device		*sc_sib; /* parent (TX39 SIB module) */
    124 	struct device		*sc_ucb; /* parent (UCB1200 module) */
    125 	tx_chipset_tag_t	sc_tc;
    126 
    127 	struct	tx_sound_tag	sc_tag;
    128 	int			sc_mute;
    129 
    130 	/*
    131 	 *  audio codec state machine
    132 	 */
    133 	int		sa_transfer_mode;
    134 #define UCBSND_TRANSFERMODE_DMA		0
    135 #define UCBSND_TRANSFERMODE_PIO		1
    136 	enum ucbsnd_state sa_state;
    137 	int		sa_snd_attenuation;
    138 #define UCBSND_DEFAULT_ATTENUATION	0	/* Full volume */
    139 	int		sa_snd_rate; /* passed down from SIB module */
    140 	int		sa_tel_rate;
    141 	void*		sa_sf0ih;
    142 	void*		sa_sndih;
    143 	int		sa_retry;
    144 	int		sa_cnt; /* misc counter */
    145 
    146 	/*
    147 	 *  input buffer
    148 	 */
    149 	size_t		sa_dmacnt;
    150 	struct ring_buf sc_rb;
    151 };
    152 
    153 int	ucbsnd_match(struct device*, struct cfdata*, void*);
    154 void	ucbsnd_attach(struct device*, struct device*, void*);
    155 
    156 int	ucbsnd_exec_output(void*);
    157 int	ucbsnd_busy(void*);
    158 
    159 void	ucbsnd_sound_init(struct ucbsnd_softc*);
    160 void	__ucbsnd_sound_click(tx_sound_tag_t);
    161 void	__ucbsnd_sound_mute(tx_sound_tag_t, int);
    162 
    163 int	ucbsndwrite_subr(struct ucbsnd_softc *, u_int32_t *, size_t,
    164 	    struct uio *);
    165 
    166 int	ringbuf_allocate(struct ring_buf*, size_t, int);
    167 void	ringbuf_deallocate(struct ring_buf*);
    168 void	ringbuf_reset(struct ring_buf*);
    169 int	ringbuf_full(struct ring_buf*);
    170 void	*ringbuf_producer_get(struct ring_buf*);
    171 void	ringbuf_producer_return(struct ring_buf*, size_t);
    172 void	*ringbuf_consumer_get(struct ring_buf*, size_t*);
    173 void	ringbuf_consumer_return(struct ring_buf*);
    174 
    175 CFATTACH_DECL(ucbsnd, sizeof(struct ucbsnd_softc),
    176     ucbsnd_match, ucbsnd_attach, NULL, NULL);
    177 
    178 dev_type_open(ucbsndopen);
    179 dev_type_close(ucbsndclose);
    180 dev_type_read(ucbsndread);
    181 dev_type_write(ucbsndwrite);
    182 dev_type_ioctl(ucbsndioctl);
    183 dev_type_poll(ucbsndpoll);
    184 dev_type_mmap(ucbsndmmap);
    185 
    186 const struct cdevsw ucbsnd_cdevsw = {
    187 	ucbsndopen, ucbsndclose, ucbsndread, ucbsndwrite, ucbsndioctl,
    188 	nostop, notty, ucbsndpoll, ucbsndmmap,
    189 };
    190 
    191 int
    192 ucbsnd_match(struct device *parent, struct cfdata *cf, void *aux)
    193 {
    194 
    195 	return (1);
    196 }
    197 
    198 void
    199 ucbsnd_attach(struct device *parent, struct device *self, void *aux)
    200 {
    201 	struct ucb1200_attach_args *ucba = aux;
    202 	struct ucbsnd_softc *sc = (void*)self;
    203 	tx_chipset_tag_t tc;
    204 
    205 	tc = sc->sc_tc = ucba->ucba_tc;
    206 	sc->sc_sib = ucba->ucba_sib;
    207 	sc->sc_ucb = ucba->ucba_ucb;
    208 
    209 	/* register sound functions */
    210 	ucbsnd_sound_init(sc);
    211 
    212 	sc->sa_snd_rate = ucba->ucba_snd_rate;
    213 	sc->sa_tel_rate = ucba->ucba_tel_rate;
    214 
    215 	sc->sa_snd_attenuation = UCBSND_DEFAULT_ATTENUATION;
    216 #define KHZ(a) ((a) / 1000), (((a) % 1000))
    217 	printf(": audio %d.%03d kHz telecom %d.%03d kHz",
    218 	    KHZ((tx39sib_clock(sc->sc_sib) * 2) /
    219 		(sc->sa_snd_rate * 64)),
    220 	    KHZ((tx39sib_clock(sc->sc_sib) * 2) /
    221 		(sc->sa_tel_rate * 64)));
    222 
    223 	ucb1200_state_install(parent, ucbsnd_busy, self,
    224 	    UCB1200_SND_MODULE);
    225 
    226 	ringbuf_allocate(&sc->sc_rb, TX39_SIBDMA_SIZE, UCBSND_BUFBLOCK);
    227 
    228 	printf("\n");
    229 }
    230 
    231 int
    232 ucbsnd_busy(void *arg)
    233 {
    234 	struct ucbsnd_softc *sc = arg;
    235 
    236 	return (sc->sa_state != UCBSND_IDLE);
    237 }
    238 
    239 int
    240 ucbsnd_exec_output(void *arg)
    241 {
    242 	struct ucbsnd_softc *sc = arg;
    243 	tx_chipset_tag_t tc = sc->sc_tc;
    244 	txreg_t reg;
    245 	u_int32_t *buf;
    246 	size_t bufcnt;
    247 
    248 	switch (sc->sa_state) {
    249 	default:
    250 		panic("ucbsnd_exec_output: invalid state %d", sc->sa_state);
    251 		/* NOTREACHED */
    252 		break;
    253 
    254 	case UCBSND_IDLE:
    255 		/* nothing to do */
    256 		return (0);
    257 
    258 	case UCBSND_INIT:
    259 		sc->sa_sf0ih = tx_intr_establish(
    260 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
    261 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    262 
    263 		sc->sa_state = UCBSND_ENABLE_SAMPLERATE;
    264 		return (0);
    265 
    266 	case UCBSND_ENABLE_SAMPLERATE:
    267 		/* Enable UCB1200 side sample rate */
    268 		reg = TX39_SIBSF0_WRITE;
    269 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLA_REG);
    270 		reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_rate);
    271 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    272 
    273 		sc->sa_state = UCBSND_ENABLE_OUTPUTPATH;
    274 		return (0);
    275 
    276 	case UCBSND_ENABLE_OUTPUTPATH:
    277 		/* Enable UCB1200 side */
    278 		reg = TX39_SIBSF0_WRITE;
    279 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
    280 		reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_attenuation |
    281 		    UCB1200_AUDIOCTRLB_OUTEN);
    282 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    283 
    284 		/* Enable SIB side */
    285 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
    286 		tx_conf_write(tc, TX39_SIBCTRL_REG,
    287 		    reg | TX39_SIBCTRL_ENSND);
    288 
    289 		sc->sa_state = UCBSND_ENABLE_SPEAKER0;
    290 		sc->sa_retry = 10;
    291 		return (0);
    292 	case UCBSND_ENABLE_SPEAKER0:
    293 		/* Speaker on */
    294 
    295 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
    296 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    297 
    298 		sc->sa_state = UCBSND_ENABLE_SPEAKER1;
    299 		return (0);
    300 
    301 	case UCBSND_ENABLE_SPEAKER1:
    302 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
    303 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
    304 		    --sc->sa_retry > 0) {
    305 
    306 			sc->sa_state = UCBSND_ENABLE_SPEAKER0;
    307 			return (0);
    308 		}
    309 
    310 		if (sc->sa_retry <= 0) {
    311 			printf("ucbsnd_exec_output: subframe0 busy\n");
    312 
    313 			sc->sa_state = UCBSND_IDLE;
    314 			return (0);
    315 		}
    316 
    317 		reg |= TX39_SIBSF0_WRITE;
    318 		reg |= UCB1200_IO_DATA_SPEAKER;
    319 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    320 
    321 		/*
    322 		 * Begin to transfer.
    323 		 */
    324 		switch (sc->sa_transfer_mode) {
    325 		case UCBSND_TRANSFERMODE_DMA:
    326 			sc->sa_state = UCBSND_DMASTART;
    327 			sc->sa_dmacnt = 0;
    328 			break;
    329 		case UCBSND_TRANSFERMODE_PIO:
    330 			sc->sa_state = UCBSND_TRANSITION_PIO;
    331 			break;
    332 		}
    333 
    334 		return (0);
    335 	case UCBSND_DMASTART:
    336 		/* get data */
    337 		if (sc->sa_dmacnt) /* return previous buffer */
    338 			ringbuf_consumer_return(&sc->sc_rb);
    339 		buf = ringbuf_consumer_get(&sc->sc_rb, &bufcnt);
    340 		if (buf == 0) {
    341 			sc->sa_state = UCBSND_DMAEND;
    342 			return (0);
    343 		}
    344 
    345 		if (sc->sa_dmacnt == 0) {
    346 			/* change interrupt source */
    347 			if (sc->sa_sf0ih) {
    348 				tx_intr_disestablish(tc, sc->sa_sf0ih);
    349 				sc->sa_sf0ih = 0;
    350 			}
    351 			sc->sa_sndih = tx_intr_establish(
    352 				tc, MAKEINTR(1, TX39_INTRSTATUS1_SND1_0INT),
    353 				IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    354 		} else {
    355 			wakeup(&sc->sc_rb);
    356 		}
    357 
    358 		/* set DMA buffer address */
    359 		tx_conf_write(tc, TX39_SIBSNDTXSTART_REG,
    360 		    MIPS_KSEG0_TO_PHYS(buf));
    361 
    362 		/* set DMA buffer size */
    363 		tx_conf_write(tc, TX39_SIBSIZE_REG,
    364 		    TX39_SIBSIZE_SNDSIZE_SET(0, bufcnt));
    365 
    366 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
    367 
    368 		/* kick DMA */
    369 		reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
    370 		reg |= TX39_SIBDMACTRL_ENDMATXSND;
    371 		tx_conf_write(tc, TX39_SIBDMACTRL_REG, reg);
    372 
    373 		/* set next */
    374 		sc->sa_dmacnt += bufcnt;
    375 
    376 		break;
    377 
    378 	case UCBSND_DMAEND:
    379 		sc->sa_state = UCBSND_TRANSITION_DISABLE;
    380 		break;
    381 	case UCBSND_TRANSITION_PIO:
    382 		/* change interrupt source */
    383 		if (sc->sa_sf0ih) {
    384 			tx_intr_disestablish(tc, sc->sa_sf0ih);
    385 			sc->sa_sf0ih = 0;
    386 		}
    387 		sc->sa_sndih = tx_intr_establish(
    388 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SNDININT),
    389 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    390 
    391 		sc->sa_state = UCBSND_PIO;
    392 		sc->sa_cnt = 0;
    393 		return (0);
    394 
    395 	case UCBSND_PIO:
    396 	{
    397 		/* PIO test routine */
    398 		int dummy_data = sc->sa_cnt * 3;
    399 		tx_conf_write(tc, TX39_SIBSNDHOLD_REG,
    400 		    dummy_data << 16 | dummy_data);
    401 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
    402 		if (sc->sa_cnt++ > 50) {
    403 			sc->sa_state = UCBSND_TRANSITION_DISABLE;
    404 		}
    405 		return (0);
    406 	}
    407 	case UCBSND_TRANSITION_DISABLE:
    408 		/* change interrupt source */
    409 		if (sc->sa_sndih) {
    410 			tx_intr_disestablish(tc, sc->sa_sndih);
    411 			sc->sa_sndih = 0;
    412 		}
    413 		sc->sa_sf0ih = tx_intr_establish(
    414 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
    415 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    416 
    417 		sc->sa_state = UCBSND_DISABLE_OUTPUTPATH;
    418 		return (0);
    419 
    420 	case UCBSND_DISABLE_OUTPUTPATH:
    421 		/* disable codec output path and mute */
    422 		reg = TX39_SIBSF0_WRITE;
    423 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
    424 		reg = TX39_SIBSF0_REGDATA_SET(reg, UCB1200_AUDIOCTRLB_MUTE);
    425 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    426 
    427 		sc->sa_state = UCBSND_DISABLE_SPEAKER0;
    428 		sc->sa_retry = 10;
    429 		return (0);
    430 
    431 	case UCBSND_DISABLE_SPEAKER0:
    432 		/* Speaker off */
    433 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
    434 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    435 
    436 		sc->sa_state = UCBSND_DISABLE_SPEAKER1;
    437 		return (0);
    438 
    439 	case UCBSND_DISABLE_SPEAKER1:
    440 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
    441 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
    442 		    --sc->sa_retry > 0) {
    443 
    444 			sc->sa_state = UCBSND_DISABLE_SPEAKER0;
    445 			return (0);
    446 		}
    447 
    448 		if (sc->sa_retry <= 0) {
    449 			printf("ucbsnd_exec_output: subframe0 busy\n");
    450 
    451 			sc->sa_state = UCBSND_IDLE;
    452 			return (0);
    453 		}
    454 
    455 		reg |= TX39_SIBSF0_WRITE;
    456 		reg &= ~UCB1200_IO_DATA_SPEAKER;
    457 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    458 
    459 		sc->sa_state = UCBSND_DISABLE_SIB;
    460 		return (0);
    461 
    462 	case UCBSND_DISABLE_SIB:
    463 		/* Disable SIB side */
    464 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
    465 		reg &= ~TX39_SIBCTRL_ENSND;
    466 		tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
    467 
    468 		/* end audio disable sequence */
    469 		if (sc->sa_sf0ih) {
    470 			tx_intr_disestablish(tc, sc->sa_sf0ih);
    471 			sc->sa_sf0ih = 0;
    472 		}
    473 		sc->sa_state = UCBSND_IDLE;
    474 
    475 		return (0);
    476 	}
    477 
    478 	return (0);
    479 }
    480 
    481 /*
    482  * global sound interface.
    483  */
    484 void
    485 ucbsnd_sound_init(struct ucbsnd_softc *sc)
    486 {
    487 	tx_sound_tag_t ts = &sc->sc_tag;
    488 	tx_chipset_tag_t tc = sc->sc_tc;
    489 
    490 	ts->ts_v = sc;
    491 	ts->ts_click	= __ucbsnd_sound_click;
    492 	ts->ts_mute	= __ucbsnd_sound_mute;
    493 
    494 	tx_conf_register_sound(tc, ts);
    495 }
    496 
    497 void
    498 __ucbsnd_sound_click(tx_sound_tag_t arg)
    499 {
    500 	struct ucbsnd_softc *sc = (void*)arg;
    501 
    502 	if (!sc->sc_mute && sc->sa_state == UCBSND_IDLE) {
    503 		sc->sa_transfer_mode = UCBSND_TRANSFERMODE_PIO;
    504 		sc->sa_state = UCBSND_INIT;
    505 		ucbsnd_exec_output((void*)sc);
    506 	}
    507 }
    508 
    509 void
    510 __ucbsnd_sound_mute(tx_sound_tag_t arg, int onoff)
    511 {
    512 	struct ucbsnd_softc *sc = (void*)arg;
    513 
    514 	sc->sc_mute = onoff;
    515 }
    516 
    517 /*
    518  * device access
    519  */
    520 extern struct cfdriver ucbsnd_cd;
    521 
    522 int
    523 ucbsndopen(dev_t dev, int flags, int ifmt, struct proc *p)
    524 {
    525 	int unit = AUDIOUNIT(dev);
    526 	struct ucbsnd_softc *sc;
    527 	int s;
    528 
    529 	if (unit >= ucbsnd_cd.cd_ndevs ||
    530 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
    531 		return (ENXIO);
    532 
    533 	s = splaudio();
    534 	ringbuf_reset(&sc->sc_rb);
    535 	splx(s);
    536 
    537 	return (0);
    538 }
    539 
    540 int
    541 ucbsndclose(dev_t dev, int flags, int ifmt, struct proc *p)
    542 {
    543 	int unit = AUDIOUNIT(dev);
    544 	struct ucbsnd_softc *sc;
    545 
    546 	if (unit >= ucbsnd_cd.cd_ndevs ||
    547 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
    548 		return (ENXIO);
    549 
    550 	return (0);
    551 }
    552 
    553 int
    554 ucbsndread(dev_t dev, struct uio *uio, int ioflag)
    555 {
    556 	int unit = AUDIOUNIT(dev);
    557 	struct ucbsnd_softc *sc;
    558 	int error = 0;
    559 
    560 	if (unit >= ucbsnd_cd.cd_ndevs ||
    561 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
    562 		return (ENXIO);
    563 	/* not supported yet */
    564 
    565 	return (error);
    566 }
    567 
    568 int
    569 ucbsndwrite_subr(struct ucbsnd_softc *sc, u_int32_t *buf, size_t bufsize,
    570     struct uio *uio)
    571 {
    572 	int i, s, error;
    573 
    574 	error = uiomove(buf, bufsize, uio);
    575 	/*
    576 	 * inverse endian for UCB1200
    577 	 */
    578 	for (i = 0; i < bufsize / sizeof(int); i++)
    579 		buf[i] = htobe32(buf[i]);
    580 	mips_dcache_wbinv_range((vaddr_t)buf, bufsize);
    581 
    582 	ringbuf_producer_return(&sc->sc_rb, bufsize);
    583 
    584 	s = splaudio();
    585 	if (sc->sa_state == UCBSND_IDLE && ringbuf_full(&sc->sc_rb)) {
    586 		sc->sa_transfer_mode = UCBSND_TRANSFERMODE_DMA;
    587 		sc->sa_state = UCBSND_INIT;
    588 		ucbsnd_exec_output((void*)sc);
    589 	}
    590 	splx(s);
    591 
    592 	return (error);
    593 }
    594 
    595 int
    596 ucbsndwrite(dev_t dev, struct uio *uio, int ioflag)
    597 {
    598 	int unit = AUDIOUNIT(dev);
    599 	struct ucbsnd_softc *sc;
    600 	int len, error = 0;
    601 	int i, n, s, rest;
    602 	void *buf;
    603 
    604 	if (unit >= ucbsnd_cd.cd_ndevs ||
    605 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
    606 		return (ENXIO);
    607 
    608 	len = uio->uio_resid;
    609 	n = (len + TX39_SIBDMA_SIZE - 1) / TX39_SIBDMA_SIZE;
    610 	rest = len % TX39_SIBDMA_SIZE;
    611 
    612 	if (rest)
    613 		--n;
    614 
    615 	for (i = 0; i < n; i++) {
    616 		while (!(buf = ringbuf_producer_get(&sc->sc_rb))) {
    617 			error = tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 1000);
    618 			if (error)
    619 				goto errout;
    620 		}
    621 
    622 		error = ucbsndwrite_subr(sc, buf, TX39_SIBDMA_SIZE, uio);
    623 		if (error)
    624 			goto out;
    625 	}
    626 
    627 	if (rest) {
    628 		while (!(buf = ringbuf_producer_get(&sc->sc_rb))) {
    629 			error = tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 1000);
    630 			if (error)
    631 				goto errout;
    632 		}
    633 
    634 		error = ucbsndwrite_subr(sc, buf, rest, uio);
    635 	}
    636 
    637  out:
    638 	return (error);
    639  errout:
    640 	printf("%s: timeout. reset ring-buffer.\n", sc->sc_dev.dv_xname);
    641 	s = splaudio();
    642 	ringbuf_reset(&sc->sc_rb);
    643 	splx(s);
    644 
    645 	return (error);
    646 }
    647 
    648 int
    649 ucbsndioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    650 {
    651 	int error = 0;
    652 
    653 	/* not coded yet */
    654 
    655 	return (error);
    656 }
    657 
    658 int
    659 ucbsndpoll(dev_t dev, int events, struct proc *p)
    660 {
    661 	int error = 0;
    662 
    663 	/* not coded yet */
    664 
    665 	return (error);
    666 }
    667 
    668 paddr_t
    669 ucbsndmmap(dev_t dev, off_t off, int prot)
    670 {
    671 	int error = 0;
    672 
    673 	/* not coded yet */
    674 
    675 	return (error);
    676 }
    677 
    678 /*
    679  * Ring buffer.
    680  */
    681 int
    682 ringbuf_allocate(struct ring_buf *rb, size_t blksize, int maxblk)
    683 {
    684 	rb->rb_bufsize = blksize * maxblk;
    685 	rb->rb_blksize = blksize;
    686 	rb->rb_maxblks = maxblk;
    687 #if notyet
    688 	rb->rb_buf = (u_int32_t)malloc(rb->rb_bufsize, M_DEVBUF, M_WAITOK);
    689 #else
    690 	rb->rb_buf = (u_int32_t)dmabuf_static;
    691 #endif
    692 	if (rb->rb_buf == 0) {
    693 		printf("ringbuf_allocate: can't allocate buffer\n");
    694 		return (1);
    695 	}
    696 	memset((char*)rb->rb_buf, 0, rb->rb_bufsize);
    697 #if notyet
    698 	rb->rb_bufcnt = malloc(rb->rb_maxblks * sizeof(size_t), M_DEVBUF,
    699 	    M_WAITOK);
    700 #else
    701 	rb->rb_bufcnt = dmabufcnt_static;
    702 #endif
    703 	if (rb->rb_bufcnt == 0) {
    704 		printf("ringbuf_allocate: can't allocate buffer\n");
    705 		return (1);
    706 	}
    707 	memset((char*)rb->rb_bufcnt, 0, rb->rb_maxblks * sizeof(size_t));
    708 
    709 	ringbuf_reset(rb);
    710 
    711 	return (0);
    712 }
    713 
    714 void
    715 ringbuf_deallocate(struct ring_buf *rb)
    716 {
    717 #if notyet
    718 	free((void*)rb->rb_buf, M_DEVBUF);
    719 	free(rb->rb_bufcnt, M_DEVBUF);
    720 #endif
    721 }
    722 
    723 void
    724 ringbuf_reset(struct ring_buf *rb)
    725 {
    726 	rb->rb_outp = 0;
    727 	rb->rb_inp = 0;
    728 }
    729 
    730 int
    731 ringbuf_full(struct ring_buf *rb)
    732 {
    733 	int ret;
    734 
    735 	ret = rb->rb_outp == rb->rb_maxblks;
    736 
    737 	return (ret);
    738 }
    739 
    740 void*
    741 ringbuf_producer_get(struct ring_buf *rb)
    742 {
    743 	u_int32_t ret;
    744 	int s;
    745 
    746 	s = splaudio();
    747 	ret = ringbuf_full(rb) ? 0 :
    748 	    rb->rb_buf + rb->rb_inp * rb->rb_blksize;
    749 	splx(s);
    750 
    751 	return (void *)ret;
    752 }
    753 
    754 void
    755 ringbuf_producer_return(struct ring_buf *rb, size_t cnt)
    756 {
    757 	int s;
    758 
    759 	assert(cnt <= rb->rb_blksize);
    760 
    761 	s = splaudio();
    762 	rb->rb_outp++;
    763 
    764 	rb->rb_bufcnt[rb->rb_inp] = cnt;
    765 	rb->rb_inp = (rb->rb_inp + 1) % rb->rb_maxblks;
    766 	splx(s);
    767 }
    768 
    769 void*
    770 ringbuf_consumer_get(struct ring_buf *rb, size_t *cntp)
    771 {
    772 	u_int32_t p;
    773 	int idx;
    774 
    775 	if (rb->rb_outp == 0)
    776 		return (0);
    777 
    778 	idx = (rb->rb_inp - rb->rb_outp + rb->rb_maxblks) % rb->rb_maxblks;
    779 
    780 	p = rb->rb_buf + idx * rb->rb_blksize;
    781 	*cntp = rb->rb_bufcnt[idx];
    782 
    783 	return (void *)p;
    784 }
    785 
    786 void
    787 ringbuf_consumer_return(struct ring_buf *rb)
    788 {
    789 
    790 	if (rb->rb_outp > 0)
    791 		rb->rb_outp--;
    792 }
    793