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