Home | History | Annotate | Line # | Download | only in nvidia
tegra_cec.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: tegra_cec.c,v 1.2 2015/12/13 17:39:19 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_cec.c,v 1.2 2015/12/13 17:39:19 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/mutex.h>
     39  1.1  jmcneill #include <sys/condvar.h>
     40  1.1  jmcneill #include <sys/poll.h>
     41  1.1  jmcneill #include <sys/select.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/hdmicec/hdmicecio.h>
     44  1.1  jmcneill #include <dev/hdmicec/hdmicec_if.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     47  1.1  jmcneill #include <arm/nvidia/tegra_pmcreg.h>
     48  1.1  jmcneill #include <arm/nvidia/tegra_cecreg.h>
     49  1.1  jmcneill 
     50  1.2  jmcneill #include <dev/fdt/fdtvar.h>
     51  1.2  jmcneill 
     52  1.1  jmcneill #define CEC_VENDORID_NVIDIA	0x00044b
     53  1.1  jmcneill 
     54  1.1  jmcneill static int	tegra_cec_match(device_t, cfdata_t, void *);
     55  1.1  jmcneill static void	tegra_cec_attach(device_t, device_t, void *);
     56  1.1  jmcneill 
     57  1.1  jmcneill static int	tegra_cec_intr(void *);
     58  1.1  jmcneill 
     59  1.1  jmcneill struct tegra_cec_softc {
     60  1.1  jmcneill 	device_t		sc_dev;
     61  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     62  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     63  1.1  jmcneill 	void			*sc_ih;
     64  1.1  jmcneill 
     65  1.1  jmcneill 	kmutex_t		sc_lock;
     66  1.1  jmcneill 	kcondvar_t		sc_cv;
     67  1.1  jmcneill 
     68  1.1  jmcneill 	const char		*sc_hdmidevname;
     69  1.1  jmcneill 	device_t		sc_cecdev;
     70  1.1  jmcneill 
     71  1.1  jmcneill 	struct selinfo		sc_selinfo;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	uint8_t			sc_rxbuf[16];
     74  1.1  jmcneill 	int			sc_rxlen;
     75  1.1  jmcneill 	bool			sc_rxdone;
     76  1.1  jmcneill 
     77  1.1  jmcneill 	uint8_t			sc_txbuf[16];
     78  1.1  jmcneill 	int			sc_txlen;
     79  1.1  jmcneill 	int			sc_txcur;
     80  1.1  jmcneill 	int			sc_txerr;
     81  1.1  jmcneill 	bool			sc_txdone;
     82  1.1  jmcneill };
     83  1.1  jmcneill 
     84  1.1  jmcneill static void	tegra_cec_reset(struct tegra_cec_softc *);
     85  1.1  jmcneill 
     86  1.1  jmcneill static int	tegra_cec_open(void *, int);
     87  1.1  jmcneill static void	tegra_cec_close(void *);
     88  1.1  jmcneill static int	tegra_cec_ioctl(void *, u_long, void *, int, lwp_t *);
     89  1.1  jmcneill static int	tegra_cec_send(void *, const uint8_t *, size_t);
     90  1.1  jmcneill static ssize_t	tegra_cec_recv(void *, uint8_t *, size_t);
     91  1.1  jmcneill static int	tegra_cec_poll(void *, int, lwp_t *);
     92  1.1  jmcneill 
     93  1.1  jmcneill static const struct hdmicec_hw_if tegra_cec_hw_if = {
     94  1.1  jmcneill 	.open = tegra_cec_open,
     95  1.1  jmcneill 	.close = tegra_cec_close,
     96  1.1  jmcneill 	.ioctl = tegra_cec_ioctl,
     97  1.1  jmcneill 	.send = tegra_cec_send,
     98  1.1  jmcneill 	.recv = tegra_cec_recv,
     99  1.1  jmcneill 	.poll = tegra_cec_poll,
    100  1.1  jmcneill };
    101  1.1  jmcneill 
    102  1.1  jmcneill CFATTACH_DECL_NEW(tegra_cec, sizeof(struct tegra_cec_softc),
    103  1.1  jmcneill 	tegra_cec_match, tegra_cec_attach, NULL, NULL);
    104  1.1  jmcneill 
    105  1.1  jmcneill #define CEC_READ(sc, reg)			\
    106  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    107  1.1  jmcneill #define CEC_WRITE(sc, reg, val)			\
    108  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    109  1.1  jmcneill #define CEC_SET_CLEAR(sc, reg, set, clr)	\
    110  1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
    111  1.1  jmcneill 
    112  1.1  jmcneill static int
    113  1.1  jmcneill tegra_cec_match(device_t parent, cfdata_t cf, void *aux)
    114  1.1  jmcneill {
    115  1.2  jmcneill 	const char * const compatible[] = { "nvidia,tegra124-cec", NULL };
    116  1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    117  1.2  jmcneill 
    118  1.2  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    119  1.1  jmcneill }
    120  1.1  jmcneill 
    121  1.1  jmcneill static void
    122  1.1  jmcneill tegra_cec_attach(device_t parent, device_t self, void *aux)
    123  1.1  jmcneill {
    124  1.1  jmcneill 	struct tegra_cec_softc * const sc = device_private(self);
    125  1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    126  1.1  jmcneill 	prop_dictionary_t prop = device_properties(self);
    127  1.1  jmcneill 	struct hdmicec_attach_args caa;
    128  1.2  jmcneill 	char intrstr[128];
    129  1.2  jmcneill 	bus_addr_t addr;
    130  1.2  jmcneill 	bus_size_t size;
    131  1.2  jmcneill 	int error;
    132  1.2  jmcneill 
    133  1.2  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    134  1.2  jmcneill 		aprint_error(": couldn't get registers\n");
    135  1.2  jmcneill 		return;
    136  1.2  jmcneill 	}
    137  1.1  jmcneill 
    138  1.1  jmcneill 	sc->sc_dev = self;
    139  1.2  jmcneill 	sc->sc_bst = faa->faa_bst;
    140  1.2  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    141  1.2  jmcneill 	if (error) {
    142  1.2  jmcneill 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
    143  1.2  jmcneill 		return;
    144  1.2  jmcneill 	}
    145  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    146  1.1  jmcneill 	cv_init(&sc->sc_cv, "tegracec");
    147  1.1  jmcneill 	selinit(&sc->sc_selinfo);
    148  1.1  jmcneill 
    149  1.1  jmcneill 	aprint_naive("\n");
    150  1.1  jmcneill 	aprint_normal(": HDMI CEC\n");
    151  1.1  jmcneill 
    152  1.2  jmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    153  1.2  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    154  1.2  jmcneill 		return;
    155  1.2  jmcneill 	}
    156  1.2  jmcneill 
    157  1.2  jmcneill 	sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_VM,
    158  1.2  jmcneill 	    FDT_INTR_MPSAFE, tegra_cec_intr, sc);
    159  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    160  1.2  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    161  1.2  jmcneill 		    intrstr);
    162  1.1  jmcneill 		return;
    163  1.1  jmcneill 	}
    164  1.2  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    165  1.1  jmcneill 
    166  1.1  jmcneill 	prop_dictionary_get_cstring_nocopy(prop, "hdmi-device",
    167  1.1  jmcneill 	    &sc->sc_hdmidevname);
    168  1.1  jmcneill 
    169  1.1  jmcneill 	tegra_car_periph_cec_enable();
    170  1.1  jmcneill 
    171  1.1  jmcneill 	CEC_WRITE(sc, CEC_SW_CONTROL_REG, 0);
    172  1.1  jmcneill 	CEC_WRITE(sc, CEC_INPUT_FILTER_REG, 0);
    173  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    174  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
    175  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	memset(&caa, 0, sizeof(caa));
    178  1.1  jmcneill 	caa.priv = sc;
    179  1.1  jmcneill 	caa.hwif = &tegra_cec_hw_if;
    180  1.1  jmcneill 	sc->sc_cecdev = config_found(self, &caa, NULL);
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.1  jmcneill static int
    184  1.1  jmcneill tegra_cec_intr(void *priv)
    185  1.1  jmcneill {
    186  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    187  1.1  jmcneill 	uint32_t val;
    188  1.1  jmcneill 	int handled = 0;
    189  1.1  jmcneill 
    190  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    191  1.1  jmcneill 	const uint32_t int_stat = CEC_READ(sc, CEC_INT_STAT_REG);
    192  1.1  jmcneill 
    193  1.1  jmcneill 	if (int_stat & CEC_INT_RX_REGISTER_FULL) {
    194  1.1  jmcneill 		val = CEC_READ(sc, CEC_RX_REGISTER_REG);
    195  1.1  jmcneill 		sc->sc_rxbuf[sc->sc_rxlen++] =
    196  1.1  jmcneill 		    __SHIFTOUT(val, CEC_RX_REGISTER_DATA);
    197  1.1  jmcneill 		if ((val & CEC_RX_REGISTER_EOM) != 0 ||
    198  1.1  jmcneill 		    sc->sc_rxlen == 16) {
    199  1.1  jmcneill 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    200  1.1  jmcneill 			    CEC_INT_RX_REGISTER_FULL);
    201  1.1  jmcneill 			sc->sc_rxdone = true;
    202  1.1  jmcneill 			cv_broadcast(&sc->sc_cv);
    203  1.1  jmcneill 			selnotify(&sc->sc_selinfo, POLLIN|POLLRDNORM,
    204  1.1  jmcneill 			    NOTE_SUBMIT);
    205  1.1  jmcneill 		}
    206  1.1  jmcneill 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_RX_REGISTER_FULL);
    207  1.1  jmcneill 		++handled;
    208  1.1  jmcneill 	}
    209  1.1  jmcneill 
    210  1.1  jmcneill 	if (int_stat & CEC_INT_TX_REGISTER_EMPTY) {
    211  1.1  jmcneill 		if (sc->sc_txcur < sc->sc_txlen) {
    212  1.1  jmcneill 			const uint8_t destination = sc->sc_txbuf[0] & 0xf;
    213  1.1  jmcneill 			val = __SHIFTIN(sc->sc_txbuf[sc->sc_txcur],
    214  1.1  jmcneill 			    CEC_TX_REGISTER_DATA);
    215  1.1  jmcneill 			if (sc->sc_txcur == 0)
    216  1.1  jmcneill 				val |= CEC_TX_REGISTER_GENERATE_START_BIT;
    217  1.1  jmcneill 			if (sc->sc_txcur == sc->sc_txlen - 1)
    218  1.1  jmcneill 				val |= CEC_TX_REGISTER_EOM;
    219  1.1  jmcneill 			if (destination == 0xf)
    220  1.1  jmcneill 				val |= CEC_TX_REGISTER_ADDRESS_MODE;
    221  1.1  jmcneill 
    222  1.1  jmcneill 			CEC_WRITE(sc, CEC_TX_REGISTER_REG, val);
    223  1.1  jmcneill 			CEC_WRITE(sc, CEC_INT_STAT_REG,
    224  1.1  jmcneill 			    CEC_INT_TX_REGISTER_EMPTY);
    225  1.1  jmcneill 			++sc->sc_txcur;
    226  1.1  jmcneill 		} else {
    227  1.1  jmcneill 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    228  1.1  jmcneill 			    CEC_INT_TX_REGISTER_EMPTY);
    229  1.1  jmcneill 		}
    230  1.1  jmcneill 		++handled;
    231  1.1  jmcneill 	}
    232  1.1  jmcneill 
    233  1.1  jmcneill 	if (int_stat & CEC_INT_TX_FRAME_TRANSMITTED) {
    234  1.1  jmcneill 		CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    235  1.1  jmcneill 		    CEC_INT_TX_FRAME_TRANSMITTED |
    236  1.1  jmcneill 		    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
    237  1.1  jmcneill 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_TX_FRAME_TRANSMITTED);
    238  1.1  jmcneill 		if (int_stat & CEC_INT_TX_FRAME_OR_BLOCK_NAKD) {
    239  1.1  jmcneill 			CEC_WRITE(sc, CEC_INT_STAT_REG,
    240  1.1  jmcneill 			    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
    241  1.1  jmcneill 			sc->sc_txerr = ECONNREFUSED;
    242  1.1  jmcneill 			tegra_cec_reset(sc);
    243  1.1  jmcneill 		}
    244  1.1  jmcneill 		sc->sc_txdone = true;
    245  1.1  jmcneill 		cv_broadcast(&sc->sc_cv);
    246  1.1  jmcneill 		++handled;
    247  1.1  jmcneill 	}
    248  1.1  jmcneill 
    249  1.1  jmcneill 	if (int_stat & CEC_INT_TX_REGISTER_UNDERRUN) {
    250  1.1  jmcneill 		tegra_cec_reset(sc);
    251  1.1  jmcneill 		cv_broadcast(&sc->sc_cv);
    252  1.1  jmcneill 		++handled;
    253  1.1  jmcneill 	}
    254  1.1  jmcneill 
    255  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    256  1.1  jmcneill 
    257  1.1  jmcneill 	return handled;
    258  1.1  jmcneill }
    259  1.1  jmcneill 
    260  1.1  jmcneill static void
    261  1.1  jmcneill tegra_cec_reset(struct tegra_cec_softc *sc)
    262  1.1  jmcneill {
    263  1.1  jmcneill 	uint32_t val;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    266  1.1  jmcneill 
    267  1.1  jmcneill 	val = CEC_READ(sc, CEC_HW_CONTROL_REG);
    268  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    269  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    270  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, val);
    271  1.1  jmcneill }
    272  1.1  jmcneill 
    273  1.1  jmcneill static int
    274  1.1  jmcneill tegra_cec_open(void *priv, int flag)
    275  1.1  jmcneill {
    276  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    277  1.1  jmcneill 
    278  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    279  1.1  jmcneill 	sc->sc_rxlen = 0;
    280  1.1  jmcneill 	sc->sc_rxdone = false;
    281  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, CEC_INT_RX_REGISTER_FULL);
    282  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, CEC_HW_CONTROL_TX_RX_MODE);
    283  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    284  1.1  jmcneill 
    285  1.1  jmcneill 	return 0;
    286  1.1  jmcneill }
    287  1.1  jmcneill 
    288  1.1  jmcneill static void
    289  1.1  jmcneill tegra_cec_close(void *priv)
    290  1.1  jmcneill {
    291  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    292  1.1  jmcneill 
    293  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    294  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    295  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
    296  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    297  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    298  1.1  jmcneill }
    299  1.1  jmcneill 
    300  1.1  jmcneill static int
    301  1.1  jmcneill tegra_cec_get_phys_addr(struct tegra_cec_softc *sc, uint16_t *phys_addr)
    302  1.1  jmcneill {
    303  1.1  jmcneill 	device_t hdmidev;
    304  1.1  jmcneill 
    305  1.1  jmcneill 	if (sc->sc_hdmidevname == NULL)
    306  1.1  jmcneill 		return EIO;
    307  1.1  jmcneill 	hdmidev = device_find_by_xname(sc->sc_hdmidevname);
    308  1.1  jmcneill 	if (hdmidev == NULL)
    309  1.1  jmcneill 		return ENXIO;
    310  1.1  jmcneill 
    311  1.1  jmcneill 	const prop_dictionary_t prop = device_properties(hdmidev);
    312  1.1  jmcneill 	if (!prop_dictionary_get_uint16(prop, "physical-address", phys_addr))
    313  1.1  jmcneill 		return ENOTCONN;
    314  1.1  jmcneill 
    315  1.1  jmcneill 	return 0;
    316  1.1  jmcneill }
    317  1.1  jmcneill 
    318  1.1  jmcneill static int
    319  1.1  jmcneill tegra_cec_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
    320  1.1  jmcneill {
    321  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    322  1.1  jmcneill 	uint32_t val;
    323  1.1  jmcneill 
    324  1.1  jmcneill 	switch (cmd) {
    325  1.1  jmcneill 	case CEC_GET_PHYS_ADDR:
    326  1.1  jmcneill 		return tegra_cec_get_phys_addr(sc, data);
    327  1.1  jmcneill 	case CEC_GET_LOG_ADDRS:
    328  1.1  jmcneill 		val = CEC_READ(sc, CEC_HW_CONTROL_REG);
    329  1.1  jmcneill 		*(uint16_t *)data =
    330  1.1  jmcneill 		    __SHIFTOUT(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
    331  1.1  jmcneill 		return 0;
    332  1.1  jmcneill 	case CEC_SET_LOG_ADDRS:
    333  1.1  jmcneill 		val = *(uint16_t *)data & 0x7fff;
    334  1.1  jmcneill 		CEC_SET_CLEAR(sc, CEC_HW_CONTROL_REG,
    335  1.1  jmcneill 		    __SHIFTIN(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS),
    336  1.1  jmcneill 		    CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
    337  1.1  jmcneill 		return 0;
    338  1.1  jmcneill 	case CEC_GET_VENDOR_ID:
    339  1.1  jmcneill 		*(uint32_t *)data = CEC_VENDORID_NVIDIA;
    340  1.1  jmcneill 		return 0;
    341  1.1  jmcneill 	default:
    342  1.1  jmcneill 		return EINVAL;
    343  1.1  jmcneill 	}
    344  1.1  jmcneill }
    345  1.1  jmcneill 
    346  1.1  jmcneill static int
    347  1.1  jmcneill tegra_cec_send(void *priv, const uint8_t *data, size_t len)
    348  1.1  jmcneill {
    349  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    350  1.1  jmcneill 	int error = 0;
    351  1.1  jmcneill 
    352  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    353  1.1  jmcneill 
    354  1.1  jmcneill 	sc->sc_txdone = false;
    355  1.1  jmcneill 	sc->sc_txcur = 0;
    356  1.1  jmcneill 	sc->sc_txerr = 0;
    357  1.1  jmcneill 	memcpy(sc->sc_txbuf, data, len);
    358  1.1  jmcneill 	sc->sc_txlen = len;
    359  1.1  jmcneill 
    360  1.1  jmcneill 	CEC_SET_CLEAR(sc, CEC_INT_MASK_REG,
    361  1.1  jmcneill 	    CEC_INT_TX_REGISTER_EMPTY |
    362  1.1  jmcneill 	    CEC_INT_TX_FRAME_TRANSMITTED |
    363  1.1  jmcneill 	    CEC_INT_TX_FRAME_OR_BLOCK_NAKD, 0);
    364  1.1  jmcneill 
    365  1.1  jmcneill 	while (sc->sc_txdone == false) {
    366  1.1  jmcneill 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
    367  1.1  jmcneill 		if (error)
    368  1.1  jmcneill 			break;
    369  1.1  jmcneill 	}
    370  1.1  jmcneill 
    371  1.1  jmcneill 	if (sc->sc_txdone)
    372  1.1  jmcneill 		error = sc->sc_txerr;
    373  1.1  jmcneill 
    374  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    375  1.1  jmcneill 
    376  1.1  jmcneill 	return error;
    377  1.1  jmcneill }
    378  1.1  jmcneill 
    379  1.1  jmcneill static ssize_t
    380  1.1  jmcneill tegra_cec_recv(void *priv, uint8_t *data, size_t len)
    381  1.1  jmcneill {
    382  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    383  1.1  jmcneill 	ssize_t alen = -1;
    384  1.1  jmcneill 	int error = 0;
    385  1.1  jmcneill 
    386  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    387  1.1  jmcneill 
    388  1.1  jmcneill 	while (sc->sc_rxdone == false) {
    389  1.1  jmcneill 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
    390  1.1  jmcneill 		if (error)
    391  1.1  jmcneill 			break;
    392  1.1  jmcneill 	}
    393  1.1  jmcneill 
    394  1.1  jmcneill 	if (sc->sc_rxdone) {
    395  1.1  jmcneill 		memcpy(data, sc->sc_rxbuf, sc->sc_rxlen);
    396  1.1  jmcneill 		alen = sc->sc_rxlen;
    397  1.1  jmcneill 		sc->sc_rxlen = 0;
    398  1.1  jmcneill 		sc->sc_rxdone = false;
    399  1.1  jmcneill 	}
    400  1.1  jmcneill 
    401  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    402  1.1  jmcneill 
    403  1.1  jmcneill 	return alen;
    404  1.1  jmcneill }
    405  1.1  jmcneill 
    406  1.1  jmcneill static int
    407  1.1  jmcneill tegra_cec_poll(void *priv, int events, lwp_t *l)
    408  1.1  jmcneill {
    409  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    410  1.1  jmcneill 	int revents;
    411  1.1  jmcneill 
    412  1.1  jmcneill 	revents = events & (POLLOUT | POLLWRNORM);
    413  1.1  jmcneill 
    414  1.1  jmcneill 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    415  1.1  jmcneill 		return revents;
    416  1.1  jmcneill 
    417  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    418  1.1  jmcneill 	if (sc->sc_rxdone) {
    419  1.1  jmcneill 		revents = (events & (POLLIN | POLLRDNORM));
    420  1.1  jmcneill 	} else {
    421  1.1  jmcneill 		selrecord(l, &sc->sc_selinfo);
    422  1.1  jmcneill 		revents = 0;
    423  1.1  jmcneill 	}
    424  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    425  1.1  jmcneill 
    426  1.1  jmcneill 	return revents;
    427  1.1  jmcneill }
    428