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