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/bus.h> 31 #include <sys/conf.h> 32 #include <sys/fcntl.h> 33 #include <sys/lock.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/sx.h> 38 #include <sys/systm.h> 39 #include <sys/uio.h> 40 41 #include <dev/iicbus/iiconf.h> 42 #include <dev/iicbus/iicbus.h> 43 #include <dev/iicbus/iic.h> 44 45 #include "iicbus_if.h" 46 47 #define BUFSIZE 1024 48 49 struct iic_softc { 50 51 device_t sc_dev; 52 u_char sc_addr; /* 7 bit address on iicbus */ 53 int sc_count; /* >0 if device opened */ 54 55 char sc_buffer[BUFSIZE]; /* output buffer */ 56 char sc_inbuf[BUFSIZE]; /* input buffer */ 57 58 struct cdev *sc_devnode; 59 struct sx sc_lock; 60 }; 61 62 #define IIC_LOCK(sc) sx_xlock(&(sc)->sc_lock) 63 #define IIC_UNLOCK(sc) sx_xunlock(&(sc)->sc_lock) 64 65 static int iic_probe(device_t); 66 static int iic_attach(device_t); 67 static int iic_detach(device_t); 68 static void iic_identify(driver_t *driver, device_t parent); 69 70 static devclass_t iic_devclass; 71 72 static device_method_t iic_methods[] = { 73 /* device interface */ 74 DEVMETHOD(device_identify, iic_identify), 75 DEVMETHOD(device_probe, iic_probe), 76 DEVMETHOD(device_attach, iic_attach), 77 DEVMETHOD(device_detach, iic_detach), 78 79 /* iicbus interface */ 80 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 81 82 { 0, 0 } 83 }; 84 85 static driver_t iic_driver = { 86 "iic", 87 iic_methods, 88 sizeof(struct iic_softc), 89 }; 90 91 static d_open_t iicopen; 92 static d_close_t iicclose; 93 static d_write_t iicwrite; 94 static d_read_t iicread; 95 static d_ioctl_t iicioctl; 96 97 static struct cdevsw iic_cdevsw = { 98 .d_version = D_VERSION, 99 .d_flags = D_TRACKCLOSE, 100 .d_open = iicopen, 101 .d_close = iicclose, 102 .d_read = iicread, 103 .d_write = iicwrite, 104 .d_ioctl = iicioctl, 105 .d_name = "iic", 106 }; 107 108 static void 109 iic_identify(driver_t *driver, device_t parent) 110 { 111 112 if (device_find_child(parent, "iic", -1) == NULL) 113 BUS_ADD_CHILD(parent, 0, "iic", 0); 114 } 115 116 static int 117 iic_probe(device_t dev) 118 { 119 device_set_desc(dev, "I2C generic I/O"); 120 return (BUS_PROBE_NOWILDCARD); 121 } 122 123 static int 124 iic_attach(device_t dev) 125 { 126 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 127 128 sc->sc_dev = dev; 129 sx_init(&sc->sc_lock, "iic"); 130 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev), 131 UID_ROOT, GID_WHEEL, 132 0600, "iic%d", device_get_unit(dev)); 133 if (sc->sc_devnode == NULL) { 134 device_printf(dev, "failed to create character device\n"); 135 sx_destroy(&sc->sc_lock); 136 return (ENXIO); 137 } 138 sc->sc_devnode->si_drv1 = sc; 139 140 return (0); 141 } 142 143 static int 144 iic_detach(device_t dev) 145 { 146 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 147 148 if (sc->sc_devnode) 149 destroy_dev(sc->sc_devnode); 150 sx_destroy(&sc->sc_lock); 151 152 return (0); 153 } 154 155 static int 156 iicopen(struct cdev *dev, int flags, int fmt, struct thread *td) 157 { 158 struct iic_softc *sc = dev->si_drv1; 159 160 IIC_LOCK(sc); 161 if (sc->sc_count > 0) { 162 IIC_UNLOCK(sc); 163 return (EBUSY); 164 } 165 166 sc->sc_count++; 167 IIC_UNLOCK(sc); 168 169 return (0); 170 } 171 172 static int 173 iicclose(struct cdev *dev, int flags, int fmt, struct thread *td) 174 { 175 struct iic_softc *sc = dev->si_drv1; 176 177 IIC_LOCK(sc); 178 if (!sc->sc_count) { 179 /* XXX: I don't think this can happen. */ 180 IIC_UNLOCK(sc); 181 return (EINVAL); 182 } 183 184 sc->sc_count--; 185 186 if (sc->sc_count < 0) 187 panic("%s: iic_count < 0!", __func__); 188 IIC_UNLOCK(sc); 189 190 return (0); 191 } 192 193 static int 194 iicwrite(struct cdev *dev, struct uio * uio, int ioflag) 195 { 196 struct iic_softc *sc = dev->si_drv1; 197 device_t iicdev = sc->sc_dev; 198 int sent, error, count; 199 200 IIC_LOCK(sc); 201 if (!sc->sc_addr) { 202 IIC_UNLOCK(sc); 203 return (EINVAL); 204 } 205 206 if (sc->sc_count == 0) { 207 /* XXX: I don't think this can happen. */ 208 IIC_UNLOCK(sc); 209 return (EINVAL); 210 } 211 212 error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 213 IIC_DONTWAIT); 214 if (error) { 215 IIC_UNLOCK(sc); 216 return (error); 217 } 218 219 count = min(uio->uio_resid, BUFSIZE); 220 uiomove(sc->sc_buffer, count, uio); 221 222 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 223 sc->sc_buffer, count, &sent); 224 225 iicbus_release_bus(device_get_parent(iicdev), iicdev); 226 IIC_UNLOCK(sc); 227 228 return (error); 229 } 230 231 static int 232 iicread(struct cdev *dev, struct uio * uio, int ioflag) 233 { 234 struct iic_softc *sc = dev->si_drv1; 235 device_t iicdev = sc->sc_dev; 236 int len, error = 0; 237 int bufsize; 238 239 IIC_LOCK(sc); 240 if (!sc->sc_addr) { 241 IIC_UNLOCK(sc); 242 return (EINVAL); 243 } 244 245 if (sc->sc_count == 0) { 246 /* XXX: I don't think this can happen. */ 247 IIC_UNLOCK(sc); 248 return (EINVAL); 249 } 250 251 error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 252 IIC_DONTWAIT); 253 if (error) { 254 IIC_UNLOCK(sc); 255 return (error); 256 } 257 258 /* max amount of data to read */ 259 len = min(uio->uio_resid, BUFSIZE); 260 261 error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 262 sc->sc_inbuf, len, &bufsize); 263 if (error) { 264 IIC_UNLOCK(sc); 265 return (error); 266 } 267 268 if (bufsize > uio->uio_resid) 269 panic("%s: too much data read!", __func__); 270 271 iicbus_release_bus(device_get_parent(iicdev), iicdev); 272 273 error = uiomove(sc->sc_inbuf, bufsize, uio); 274 IIC_UNLOCK(sc); 275 return (error); 276 } 277 278 static int 279 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 280 { 281 struct iic_softc *sc = dev->si_drv1; 282 device_t iicdev = sc->sc_dev; 283 device_t parent = device_get_parent(iicdev); 284 struct iiccmd *s = (struct iiccmd *)data; 285 struct iic_rdwr_data *d = (struct iic_rdwr_data *)data; 286 struct iic_msg *m; 287 int error, count, i; 288 char *buf = NULL; 289 void **usrbufs = NULL; 290 291 if ((error = iicbus_request_bus(parent, iicdev, 292 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR)))) 293 return (error); 294 295 switch (cmd) { 296 case I2CSTART: 297 IIC_LOCK(sc); 298 error = iicbus_start(parent, s->slave, 0); 299 300 /* 301 * Implicitly set the chip addr to the slave addr passed as 302 * parameter. Consequently, start/stop shall be called before 303 * the read or the write of a block. 304 */ 305 if (!error) 306 sc->sc_addr = s->slave; 307 IIC_UNLOCK(sc); 308 309 break; 310 311 case I2CSTOP: 312 error = iicbus_stop(parent); 313 break; 314 315 case I2CRSTCARD: 316 error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL); 317 break; 318 319 case I2CWRITE: 320 if (s->count <= 0) { 321 error = EINVAL; 322 break; 323 } 324 buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK); 325 error = copyin(s->buf, buf, s->count); 326 if (error) 327 break; 328 error = iicbus_write(parent, buf, s->count, &count, 10); 329 break; 330 331 case I2CREAD: 332 if (s->count <= 0) { 333 error = EINVAL; 334 break; 335 } 336 buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK); 337 error = iicbus_read(parent, buf, s->count, &count, s->last, 10); 338 if (error) 339 break; 340 error = copyout(buf, s->buf, s->count); 341 break; 342 343 case I2CRDWR: 344 buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_TEMP, M_WAITOK); 345 usrbufs = malloc(sizeof(void *) * d->nmsgs, M_TEMP, M_ZERO | M_WAITOK); 346 error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs); 347 if (error) 348 break; 349 /* Alloc kernel buffers for userland data, copyin write data */ 350 for (i = 0; i < d->nmsgs; i++) { 351 m = &((struct iic_msg *)buf)[i]; 352 usrbufs[i] = m->buf; 353 m->buf = malloc(m->len, M_TEMP, M_WAITOK); 354 if (!(m->flags & IIC_M_RD)) 355 copyin(usrbufs[i], m->buf, m->len); 356 } 357 error = iicbus_transfer(iicdev, (struct iic_msg *)buf, d->nmsgs); 358 /* Copyout all read segments, free up kernel buffers */ 359 for (i = 0; i < d->nmsgs; i++) { 360 m = &((struct iic_msg *)buf)[i]; 361 if (m->flags & IIC_M_RD) 362 copyout(m->buf, usrbufs[i], m->len); 363 free(m->buf, M_TEMP); 364 } 365 free(usrbufs, M_TEMP); 366 break; 367 default: 368 error = ENOTTY; 369 } 370 371 iicbus_release_bus(parent, iicdev); 372 373 if (buf != NULL) 374 free(buf, M_TEMP); 375 return (error); 376 } 377 378 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 379 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 380 MODULE_VERSION(iic, 1); 381