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