1 1.2 riastrad /* $NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh 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 riastrad __KERNEL_RCSID(0, "$NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/types.h> 34 1.1 jmcneill #include <sys/device.h> 35 1.1 jmcneill #include <sys/conf.h> 36 1.1 jmcneill #include <sys/ioctl.h> 37 1.1 jmcneill #include <sys/select.h> 38 1.1 jmcneill #include <sys/atomic.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/hdmicec/hdmicec_if.h> 41 1.1 jmcneill #include <dev/hdmicec/hdmicecio.h> 42 1.1 jmcneill 43 1.2 riastrad #include "ioconf.h" 44 1.2 riastrad 45 1.1 jmcneill #define CEC_MAX_FRAMESIZE 16 46 1.1 jmcneill 47 1.1 jmcneill struct hdmicec_softc { 48 1.1 jmcneill device_t sc_dev; 49 1.1 jmcneill 50 1.1 jmcneill void * sc_priv; 51 1.1 jmcneill const struct hdmicec_hw_if *sc_hwif; 52 1.1 jmcneill 53 1.1 jmcneill u_int sc_busy; 54 1.1 jmcneill }; 55 1.1 jmcneill 56 1.1 jmcneill static dev_type_open(hdmicec_open); 57 1.1 jmcneill static dev_type_close(hdmicec_close); 58 1.1 jmcneill static dev_type_read(hdmicec_read); 59 1.1 jmcneill static dev_type_write(hdmicec_write); 60 1.1 jmcneill static dev_type_ioctl(hdmicec_ioctl); 61 1.1 jmcneill static dev_type_poll(hdmicec_poll); 62 1.1 jmcneill 63 1.1 jmcneill static int hdmicec_match(device_t, cfdata_t, void *); 64 1.1 jmcneill static void hdmicec_attach(device_t, device_t, void *); 65 1.1 jmcneill 66 1.1 jmcneill const struct cdevsw hdmicec_cdevsw = { 67 1.1 jmcneill .d_open = hdmicec_open, 68 1.1 jmcneill .d_close = hdmicec_close, 69 1.1 jmcneill .d_read = hdmicec_read, 70 1.1 jmcneill .d_write = hdmicec_write, 71 1.1 jmcneill .d_ioctl = hdmicec_ioctl, 72 1.1 jmcneill .d_stop = nostop, 73 1.1 jmcneill .d_tty = notty, 74 1.1 jmcneill .d_poll = hdmicec_poll, 75 1.1 jmcneill .d_mmap = nommap, 76 1.1 jmcneill .d_kqfilter = nokqfilter, 77 1.1 jmcneill .d_flag = D_MPSAFE 78 1.1 jmcneill }; 79 1.1 jmcneill 80 1.1 jmcneill CFATTACH_DECL_NEW(hdmicec, sizeof(struct hdmicec_softc), hdmicec_match, 81 1.1 jmcneill hdmicec_attach, NULL, NULL); 82 1.1 jmcneill 83 1.1 jmcneill static int 84 1.1 jmcneill hdmicec_match(device_t parent, cfdata_t match, void *opaque) 85 1.1 jmcneill { 86 1.1 jmcneill return 1; 87 1.1 jmcneill } 88 1.1 jmcneill 89 1.1 jmcneill static void 90 1.1 jmcneill hdmicec_attach(device_t parent, device_t self, void *opaque) 91 1.1 jmcneill { 92 1.1 jmcneill struct hdmicec_softc *sc = device_private(self); 93 1.1 jmcneill struct hdmicec_attach_args *caa = opaque; 94 1.1 jmcneill 95 1.1 jmcneill sc->sc_dev = self; 96 1.1 jmcneill sc->sc_priv = caa->priv; 97 1.1 jmcneill sc->sc_hwif = caa->hwif; 98 1.1 jmcneill 99 1.1 jmcneill aprint_naive("\n"); 100 1.1 jmcneill aprint_normal("\n"); 101 1.1 jmcneill } 102 1.1 jmcneill 103 1.1 jmcneill static int 104 1.1 jmcneill hdmicec_open(dev_t dev, int flag, int fmt, lwp_t *l) 105 1.1 jmcneill { 106 1.1 jmcneill struct hdmicec_softc *sc = 107 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 108 1.1 jmcneill int error; 109 1.1 jmcneill 110 1.1 jmcneill if (sc == NULL) 111 1.1 jmcneill return ENXIO; 112 1.1 jmcneill 113 1.1 jmcneill if (atomic_cas_uint(&sc->sc_busy, 0, 1) != 1) 114 1.1 jmcneill return EBUSY; 115 1.1 jmcneill 116 1.1 jmcneill if (sc->sc_hwif->open != NULL) { 117 1.1 jmcneill error = sc->sc_hwif->open(sc->sc_priv, flag); 118 1.1 jmcneill if (error) { 119 1.1 jmcneill atomic_swap_uint(&sc->sc_busy, 0); 120 1.1 jmcneill return error; 121 1.1 jmcneill } 122 1.1 jmcneill } 123 1.1 jmcneill 124 1.1 jmcneill return 0; 125 1.1 jmcneill } 126 1.1 jmcneill 127 1.1 jmcneill static int 128 1.1 jmcneill hdmicec_close(dev_t dev, int flag, int fmt, lwp_t *l) 129 1.1 jmcneill { 130 1.1 jmcneill struct hdmicec_softc *sc = 131 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 132 1.1 jmcneill 133 1.1 jmcneill if (sc->sc_hwif->close) 134 1.1 jmcneill sc->sc_hwif->close(sc->sc_priv); 135 1.1 jmcneill 136 1.1 jmcneill atomic_swap_uint(&sc->sc_busy, 0); 137 1.1 jmcneill return 0; 138 1.1 jmcneill } 139 1.1 jmcneill 140 1.1 jmcneill static int 141 1.1 jmcneill hdmicec_read(dev_t dev, struct uio *uio, int flags) 142 1.1 jmcneill { 143 1.1 jmcneill struct hdmicec_softc *sc = 144 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 145 1.1 jmcneill uint8_t data[CEC_MAX_FRAMESIZE]; 146 1.1 jmcneill ssize_t len; 147 1.1 jmcneill int error; 148 1.1 jmcneill 149 1.1 jmcneill if (uio->uio_resid < CEC_MAX_FRAMESIZE) 150 1.1 jmcneill return EINVAL; 151 1.1 jmcneill 152 1.1 jmcneill len = sc->sc_hwif->recv(sc->sc_priv, data, sizeof(data)); 153 1.1 jmcneill if (len < 0) 154 1.1 jmcneill return EIO; 155 1.1 jmcneill 156 1.1 jmcneill error = uiomove(data, len, uio); 157 1.1 jmcneill if (error) 158 1.1 jmcneill return error; 159 1.1 jmcneill 160 1.1 jmcneill return 0; 161 1.1 jmcneill } 162 1.1 jmcneill 163 1.1 jmcneill static int 164 1.1 jmcneill hdmicec_write(dev_t dev, struct uio *uio, int flags) 165 1.1 jmcneill { 166 1.1 jmcneill struct hdmicec_softc *sc = 167 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 168 1.1 jmcneill uint8_t data[CEC_MAX_FRAMESIZE]; 169 1.1 jmcneill size_t len = uio->uio_resid; 170 1.1 jmcneill int error; 171 1.1 jmcneill 172 1.1 jmcneill if (len > CEC_MAX_FRAMESIZE) 173 1.1 jmcneill return EINVAL; 174 1.1 jmcneill 175 1.1 jmcneill error = uiomove(data, len, uio); 176 1.1 jmcneill if (error) 177 1.1 jmcneill return error; 178 1.1 jmcneill 179 1.1 jmcneill return sc->sc_hwif->send(sc->sc_priv, data, len); 180 1.1 jmcneill } 181 1.1 jmcneill 182 1.1 jmcneill static int 183 1.1 jmcneill hdmicec_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 184 1.1 jmcneill { 185 1.1 jmcneill struct hdmicec_softc *sc = 186 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 187 1.1 jmcneill 188 1.1 jmcneill return sc->sc_hwif->ioctl(sc->sc_priv, cmd, data, flag, l); 189 1.1 jmcneill } 190 1.1 jmcneill 191 1.1 jmcneill static int 192 1.1 jmcneill hdmicec_poll(dev_t dev, int events, lwp_t *l) 193 1.1 jmcneill { 194 1.1 jmcneill struct hdmicec_softc *sc = 195 1.1 jmcneill device_lookup_private(&hdmicec_cd, minor(dev)); 196 1.1 jmcneill 197 1.1 jmcneill return sc->sc_hwif->poll(sc->sc_priv, events, l); 198 1.1 jmcneill } 199