sys/bus/iicbus/iic.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | /*- * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/dev/iicbus/iic.c,v 1.43 2009/01/26 13:53:39 raj Exp $ */ #include <sys/param.h> #include <sys/buf.h> #include <sys/bus.h> #include <sys/conf.h> #include <sys/fcntl.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/module.h> #include <sys/systm.h> #include <sys/uio.h> #include <bus/iicbus/iiconf.h> #include <bus/iicbus/iicbus.h> #include <bus/iicbus/iic.h> #include "iicbus_if.h" #define BUFSIZE 1024 struct iic_softc { device_t sc_dev; u_char sc_addr; /* 7 bit address on iicbus */ int sc_count; /* >0 if device opened */ char sc_buffer[BUFSIZE]; /* output buffer */ char sc_inbuf[BUFSIZE]; /* input buffer */ cdev_t sc_devnode; }; #define IIC_LOCK(sc) #define IIC_UNLOCK(sc) static int iic_probe(device_t); static int iic_attach(device_t); static int iic_detach(device_t); static void iic_identify(driver_t *driver, device_t parent); static devclass_t iic_devclass; static device_method_t iic_methods[] = { /* device interface */ DEVMETHOD(device_identify, iic_identify), DEVMETHOD(device_probe, iic_probe), DEVMETHOD(device_attach, iic_attach), DEVMETHOD(device_detach, iic_detach), /* iicbus interface */ DEVMETHOD(iicbus_intr, iicbus_generic_intr), DEVMETHOD_END }; static driver_t iic_driver = { "iic", iic_methods, sizeof(struct iic_softc), }; static d_open_t iicopen; static d_close_t iicclose; static d_write_t iicwrite; static d_read_t iicread; static d_ioctl_t iicioctl; static struct dev_ops iic_ops = { { "iic", 0, 0 }, .d_open = iicopen, .d_close = iicclose, .d_read = iicread, .d_write = iicwrite, .d_ioctl = iicioctl, }; static void iic_identify(driver_t *driver, device_t parent) { if (device_find_child(parent, "iic", -1) == NULL) BUS_ADD_CHILD(parent, parent, 0, "iic", -1); } static int iic_probe(device_t dev) { if (iicbus_get_addr(dev) > 0) return (ENXIO); device_set_desc(dev, "I2C generic I/O"); return (0); } static int iic_attach(device_t dev) { struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); sc->sc_devnode = make_dev(&iic_ops, device_get_unit(dev), UID_ROOT, GID_WHEEL, 0600, "iic%d", device_get_unit(dev)); if (sc->sc_devnode == NULL) { device_printf(dev, "failed to create character device\n"); return (ENXIO); } sc->sc_devnode->si_drv1 = sc; return (0); } static int iic_detach(device_t dev) { struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); if (sc->sc_devnode) dev_ops_remove_minor(&iic_ops, device_get_unit(dev)); return (0); } static int iicopen(struct dev_open_args *ap) { cdev_t dev = ap->a_head.a_dev; struct iic_softc *sc = dev->si_drv1; IIC_LOCK(sc); if (sc->sc_count > 0) { IIC_UNLOCK(sc); return (EBUSY); } sc->sc_count++; IIC_UNLOCK(sc); return (0); } static int iicclose(struct dev_close_args *ap) { cdev_t dev = ap->a_head.a_dev; struct iic_softc *sc = dev->si_drv1; IIC_LOCK(sc); if (!sc->sc_count) { /* XXX: I don't think this can happen. */ IIC_UNLOCK(sc); return (EINVAL); } sc->sc_count--; if (sc->sc_count < 0) panic("%s: iic_count < 0!", __func__); IIC_UNLOCK(sc); return (0); } static int iicwrite(struct dev_write_args *ap) { cdev_t dev = ap->a_head.a_dev; struct uio *uio = ap->a_uio; struct iic_softc *sc = dev->si_drv1; device_t iicdev = sc->sc_dev; int sent, error, count; IIC_LOCK(sc); if (!sc->sc_addr) { IIC_UNLOCK(sc); return (EINVAL); } if (sc->sc_count == 0) { /* XXX: I don't think this can happen. */ IIC_UNLOCK(sc); return (EINVAL); } error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT); if (error) { IIC_UNLOCK(sc); return (error); } count = (int)szmin(uio->uio_resid, BUFSIZE); uiomove(sc->sc_buffer, (size_t)count, uio); error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, sc->sc_buffer, count, &sent); iicbus_release_bus(device_get_parent(iicdev), iicdev); IIC_UNLOCK(sc); return (error); } static int iicread(struct dev_read_args *ap) { cdev_t dev = ap->a_head.a_dev; struct uio *uio = ap->a_uio; struct iic_softc *sc = dev->si_drv1; device_t iicdev = sc->sc_dev; int len, error = 0; int bufsize; IIC_LOCK(sc); if (!sc->sc_addr) { IIC_UNLOCK(sc); return (EINVAL); } if (sc->sc_count == 0) { /* XXX: I don't think this can happen. */ IIC_UNLOCK(sc); return (EINVAL); } error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT); if (error) { IIC_UNLOCK(sc); return (error); } /* max amount of data to read */ len = (int)szmin(uio->uio_resid, BUFSIZE); error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, sc->sc_inbuf, len, &bufsize); if (error) { IIC_UNLOCK(sc); return (error); } if (bufsize > uio->uio_resid) panic("%s: too much data read!", __func__); iicbus_release_bus(device_get_parent(iicdev), iicdev); error = uiomove(sc->sc_inbuf, (size_t)bufsize, uio); IIC_UNLOCK(sc); return (error); } static int iicioctl(struct dev_ioctl_args *ap) { cdev_t dev = ap->a_head.a_dev; u_long cmd = ap->a_cmd; caddr_t data = ap->a_data; int flags = ap->a_fflag; struct iic_softc *sc = dev->si_drv1; device_t iicdev = sc->sc_dev; device_t parent = device_get_parent(iicdev); struct iiccmd *s = (struct iiccmd *)data; struct iic_rdwr_data *d = (struct iic_rdwr_data *)data; struct iic_msg *m; int error, count, i; char *buf = NULL; void **usrbufs = NULL; if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR)))) return (error); switch (cmd) { case I2CSTART: IIC_LOCK(sc); error = iicbus_start(parent, s->slave, 0); /* * Implicitly set the chip addr to the slave addr passed as * parameter. Consequently, start/stop shall be called before * the read or the write of a block. */ if (!error) sc->sc_addr = s->slave; IIC_UNLOCK(sc); break; case I2CSTOP: error = iicbus_stop(parent); break; case I2CRSTCARD: error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL); break; case I2CWRITE: if (s->count <= 0) { error = EINVAL; break; } buf = kmalloc((unsigned long)s->count, M_TEMP, M_WAITOK); error = copyin(s->buf, buf, s->count); if (error) break; error = iicbus_write(parent, buf, s->count, &count, 10); break; case I2CREAD: if (s->count <= 0) { error = EINVAL; break; } buf = kmalloc((unsigned long)s->count, M_TEMP, M_WAITOK); error = iicbus_read(parent, buf, s->count, &count, s->last, 10); if (error) break; error = copyout(buf, s->buf, s->count); break; case I2CRDWR: buf = kmalloc(sizeof(*d->msgs) * d->nmsgs, M_TEMP, M_WAITOK); usrbufs = kmalloc(sizeof(void *) * d->nmsgs, M_TEMP, M_ZERO | M_WAITOK); error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs); if (error) break; /* Alloc kernel buffers for userland data, copyin write data */ for (i = 0; i < d->nmsgs; i++) { m = &((struct iic_msg *)buf)[i]; usrbufs[i] = m->buf; m->buf = kmalloc(m->len, M_TEMP, M_WAITOK); if (!(m->flags & IIC_M_RD)) copyin(usrbufs[i], m->buf, m->len); } error = iicbus_transfer(parent, (struct iic_msg *)buf, d->nmsgs); /* Copyout all read segments, free up kernel buffers */ for (i = 0; i < d->nmsgs; i++) { m = &((struct iic_msg *)buf)[i]; if (m->flags & IIC_M_RD) copyout(m->buf, usrbufs[i], m->len); kfree(m->buf, M_TEMP); } kfree(usrbufs, M_TEMP); break; case I2CRPTSTART: error = iicbus_repeated_start(parent, s->slave, 0); break; default: error = ENOTTY; } iicbus_release_bus(device_get_parent(iicdev), iicdev); if (buf != NULL) kfree(buf, M_TEMP); return (error); } DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, NULL, NULL); MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); MODULE_VERSION(iic, 1); |