1 /*- 2 * Copyright (c) 1998 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 39 #include <dev/iicbus/iiconf.h> 40 #include <dev/iicbus/iicbus.h> 41 42 #include <machine/iic.h> 43 44 #include "iicbus_if.h" 45 46 #define BUFSIZE 1024 47 48 struct iic_softc { 49 50 u_char sc_addr; /* address on iicbus */ 51 int sc_count; /* >0 if device opened */ 52 53 char sc_buffer[BUFSIZE]; /* output buffer */ 54 char sc_inbuf[BUFSIZE]; /* input buffer */ 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 66 static devclass_t iic_devclass; 67 68 static device_method_t iic_methods[] = { 69 /* device interface */ 70 DEVMETHOD(device_probe, iic_probe), 71 DEVMETHOD(device_attach, iic_attach), 72 73 /* iicbus interface */ 74 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 75 76 { 0, 0 } 77 }; 78 79 static driver_t iic_driver = { 80 "iic", 81 iic_methods, 82 sizeof(struct iic_softc), 83 }; 84 85 static d_open_t iicopen; 86 static d_close_t iicclose; 87 static d_write_t iicwrite; 88 static d_read_t iicread; 89 static d_ioctl_t iicioctl; 90 91 #define CDEV_MAJOR 105 92 static struct cdevsw iic_cdevsw = { 93 /* open */ iicopen, 94 /* close */ iicclose, 95 /* read */ iicread, 96 /* write */ iicwrite, 97 /* ioctl */ iicioctl, 98 /* poll */ nopoll, 99 /* mmap */ nommap, 100 /* strategy */ nostrategy, 101 /* name */ "iic", 102 /* maj */ CDEV_MAJOR, 103 /* dump */ nodump, 104 /* psize */ nopsize, 105 /* flags */ 0, 106 /* bmaj */ -1 107 }; 108 109 /* 110 * iicprobe() 111 */ 112 static int 113 iic_probe(device_t dev) 114 { 115 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 116 117 sc->sc_addr = iicbus_get_addr(dev); 118 119 /* XXX detect chip with start/stop conditions */ 120 121 return (0); 122 } 123 124 /* 125 * iicattach() 126 */ 127 static int 128 iic_attach(device_t dev) 129 { 130 make_dev(&iic_cdevsw, device_get_unit(dev), /* XXX cleanup */ 131 UID_ROOT, GID_WHEEL, 132 0600, "iic%d", device_get_unit(dev)); 133 return (0); 134 } 135 136 static int 137 iicopen (dev_t dev, int flags, int fmt, struct proc *p) 138 { 139 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 140 141 if (!sc) 142 return (EINVAL); 143 144 if (sc->sc_count > 0) 145 return (EBUSY); 146 147 sc->sc_count++; 148 149 return (0); 150 } 151 152 static int 153 iicclose(dev_t dev, int flags, int fmt, struct proc *p) 154 { 155 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 156 157 if (!sc) 158 return (EINVAL); 159 160 if (!sc->sc_count) 161 return (EINVAL); 162 163 sc->sc_count--; 164 165 if (sc->sc_count < 0) 166 panic("%s: iic_count < 0!", __FUNCTION__); 167 168 return (0); 169 } 170 171 static int 172 iicwrite(dev_t dev, struct uio * uio, int ioflag) 173 { 174 device_t iicdev = IIC_DEVICE(minor(dev)); 175 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 176 int sent, error, count; 177 178 if (!sc || !iicdev) 179 return (EINVAL); 180 181 if (sc->sc_count == 0) 182 return (EINVAL); 183 184 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 185 return (error); 186 187 count = min(uio->uio_resid, BUFSIZE); 188 uiomove(sc->sc_buffer, count, uio); 189 190 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 191 sc->sc_buffer, count, &sent); 192 193 iicbus_release_bus(device_get_parent(iicdev), iicdev); 194 195 return(error); 196 } 197 198 static int 199 iicread(dev_t dev, struct uio * uio, int ioflag) 200 { 201 device_t iicdev = IIC_DEVICE(minor(dev)); 202 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 203 int len, error = 0; 204 int bufsize; 205 206 if (!sc || !iicdev) 207 return (EINVAL); 208 209 if (sc->sc_count == 0) 210 return (EINVAL); 211 212 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 213 return (error); 214 215 /* max amount of data to read */ 216 len = min(uio->uio_resid, BUFSIZE); 217 218 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 219 sc->sc_inbuf, len, &bufsize))) 220 return (error); 221 222 if (bufsize > uio->uio_resid) 223 panic("%s: too much data read!", __FUNCTION__); 224 225 iicbus_release_bus(device_get_parent(iicdev), iicdev); 226 227 return (uiomove(sc->sc_inbuf, bufsize, uio)); 228 } 229 230 static int 231 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 232 { 233 device_t iicdev = IIC_DEVICE(minor(dev)); 234 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 235 device_t parent = device_get_parent(iicdev); 236 struct iiccmd *s = (struct iiccmd *)data; 237 int error, count; 238 239 if (!sc) 240 return (EINVAL); 241 242 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 243 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 244 (IIC_WAIT | IIC_INTR)))) 245 return (error); 246 247 switch (cmd) { 248 case I2CSTART: 249 error = iicbus_start(parent, s->slave, 0); 250 break; 251 252 case I2CSTOP: 253 error = iicbus_stop(parent); 254 break; 255 256 case I2CRSTCARD: 257 error = iicbus_reset(parent, 0, 0, NULL); 258 break; 259 260 case I2CWRITE: 261 error = iicbus_write(parent, s->buf, s->count, &count, 0); 262 break; 263 264 case I2CREAD: 265 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); 266 break; 267 268 default: 269 error = ENODEV; 270 } 271 272 iicbus_release_bus(device_get_parent(iicdev), iicdev); 273 274 return (error); 275 } 276 277 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 278