1 /*- 2 * Copyright (c) 1998, 2001 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 * 28 */ 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/module.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/uio.h> 36 #include <sys/fcntl.h> 37 38 #include <dev/iicbus/iiconf.h> 39 #include <dev/iicbus/iicbus.h> 40 #include <dev/iicbus/iic.h> 41 42 #include "iicbus_if.h" 43 44 #define BUFSIZE 1024 45 46 struct iic_softc { 47 48 u_char sc_addr; /* 7 bit address on iicbus */ 49 int sc_count; /* >0 if device opened */ 50 51 char sc_buffer[BUFSIZE]; /* output buffer */ 52 char sc_inbuf[BUFSIZE]; /* input buffer */ 53 54 dev_t sc_devnode; 55 }; 56 57 #define IIC_SOFTC(unit) \ 58 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 59 60 #define IIC_DEVICE(unit) \ 61 (devclass_get_device(iic_devclass, (unit))) 62 63 static int iic_probe(device_t); 64 static int iic_attach(device_t); 65 static int iic_detach(device_t); 66 static void iic_identify(driver_t *driver, device_t parent); 67 68 static devclass_t iic_devclass; 69 70 static device_method_t iic_methods[] = { 71 /* device interface */ 72 DEVMETHOD(device_identify, iic_identify), 73 DEVMETHOD(device_probe, iic_probe), 74 DEVMETHOD(device_attach, iic_attach), 75 DEVMETHOD(device_detach, iic_detach), 76 77 /* iicbus interface */ 78 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 79 80 { 0, 0 } 81 }; 82 83 static driver_t iic_driver = { 84 "iic", 85 iic_methods, 86 sizeof(struct iic_softc), 87 }; 88 89 static d_open_t iicopen; 90 static d_close_t iicclose; 91 static d_write_t iicwrite; 92 static d_read_t iicread; 93 static d_ioctl_t iicioctl; 94 95 #define CDEV_MAJOR 105 96 static struct cdevsw iic_cdevsw = { 97 /* open */ iicopen, 98 /* close */ iicclose, 99 /* read */ iicread, 100 /* write */ iicwrite, 101 /* ioctl */ iicioctl, 102 /* poll */ nopoll, 103 /* mmap */ nommap, 104 /* strategy */ nostrategy, 105 /* name */ "iic", 106 /* maj */ CDEV_MAJOR, 107 /* dump */ nodump, 108 /* psize */ nopsize, 109 /* flags */ 0, 110 }; 111 112 static void 113 iic_identify(driver_t *driver, device_t parent) 114 { 115 BUS_ADD_CHILD(parent, 0, "iic", 0); 116 } 117 118 static int 119 iic_probe(device_t dev) 120 { 121 device_set_desc(dev, "I2C generic I/O"); 122 return (0); 123 } 124 125 static int 126 iic_attach(device_t dev) 127 { 128 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 129 130 if (!sc) 131 return (ENOMEM); 132 133 bzero(sc, sizeof(struct iic_softc)); 134 135 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev), 136 UID_ROOT, GID_WHEEL, 137 0600, "iic%d", device_get_unit(dev)); 138 139 return (0); 140 } 141 142 static int 143 iic_detach(device_t dev) 144 { 145 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 146 147 if (sc->sc_devnode) 148 destroy_dev(sc->sc_devnode); 149 150 return (0); 151 } 152 153 static int 154 iicopen (dev_t dev, int flags, int fmt, struct thread *td) 155 { 156 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 157 158 if (!sc) 159 return (EINVAL); 160 161 if (sc->sc_count > 0) 162 return (EBUSY); 163 164 sc->sc_count++; 165 166 return (0); 167 } 168 169 static int 170 iicclose(dev_t dev, int flags, int fmt, struct thread *td) 171 { 172 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 173 174 if (!sc) 175 return (EINVAL); 176 177 if (!sc->sc_count) 178 return (EINVAL); 179 180 sc->sc_count--; 181 182 if (sc->sc_count < 0) 183 panic("%s: iic_count < 0!", __func__); 184 185 return (0); 186 } 187 188 static int 189 iicwrite(dev_t dev, struct uio * uio, int ioflag) 190 { 191 device_t iicdev = IIC_DEVICE(minor(dev)); 192 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 193 int sent, error, count; 194 195 if (!sc || !iicdev || !sc->sc_addr) 196 return (EINVAL); 197 198 if (sc->sc_count == 0) 199 return (EINVAL); 200 201 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 202 return (error); 203 204 count = min(uio->uio_resid, BUFSIZE); 205 uiomove(sc->sc_buffer, count, uio); 206 207 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 208 sc->sc_buffer, count, &sent); 209 210 iicbus_release_bus(device_get_parent(iicdev), iicdev); 211 212 return(error); 213 } 214 215 static int 216 iicread(dev_t dev, struct uio * uio, int ioflag) 217 { 218 device_t iicdev = IIC_DEVICE(minor(dev)); 219 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 220 int len, error = 0; 221 int bufsize; 222 223 if (!sc || !iicdev || !sc->sc_addr) 224 return (EINVAL); 225 226 if (sc->sc_count == 0) 227 return (EINVAL); 228 229 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 230 return (error); 231 232 /* max amount of data to read */ 233 len = min(uio->uio_resid, BUFSIZE); 234 235 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 236 sc->sc_inbuf, len, &bufsize))) 237 return (error); 238 239 if (bufsize > uio->uio_resid) 240 panic("%s: too much data read!", __func__); 241 242 iicbus_release_bus(device_get_parent(iicdev), iicdev); 243 244 return (uiomove(sc->sc_inbuf, bufsize, uio)); 245 } 246 247 static int 248 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 249 { 250 device_t iicdev = IIC_DEVICE(minor(dev)); 251 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 252 device_t parent = device_get_parent(iicdev); 253 struct iiccmd *s = (struct iiccmd *)data; 254 int error, count; 255 256 if (!sc) 257 return (EINVAL); 258 259 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 260 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 261 (IIC_WAIT | IIC_INTR)))) 262 return (error); 263 264 switch (cmd) { 265 case I2CSTART: 266 error = iicbus_start(parent, s->slave, 0); 267 268 /* 269 * Implicitly set the chip addr to the slave addr passed as 270 * parameter. Consequently, start/stop shall be called before 271 * the read or the write of a block. 272 */ 273 if (!error) 274 sc->sc_addr = s->slave; 275 276 break; 277 278 case I2CSTOP: 279 error = iicbus_stop(parent); 280 break; 281 282 case I2CRSTCARD: 283 error = iicbus_reset(parent, 0, 0, NULL); 284 break; 285 286 case I2CWRITE: 287 error = iicbus_write(parent, s->buf, s->count, &count, 10); 288 break; 289 290 case I2CREAD: 291 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10); 292 break; 293 294 default: 295 error = ENODEV; 296 } 297 298 iicbus_release_bus(device_get_parent(iicdev), iicdev); 299 300 return (error); 301 } 302 303 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 304 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 305 MODULE_VERSION(iic, 1); 306