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