2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
32 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/systm.h>
40 #include <sys/errno.h>
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iic.h>
46 #include "iicbus_if.h"
50 struct cdev *sc_devnode;
61 #define IIC_LOCK(cdp) sx_xlock(&(cdp)->lock)
62 #define IIC_UNLOCK(cdp) sx_xunlock(&(cdp)->lock)
64 static MALLOC_DEFINE(M_IIC, "iic", "I2C device data");
66 static int iic_probe(device_t);
67 static int iic_attach(device_t);
68 static int iic_detach(device_t);
69 static void iic_identify(driver_t *driver, device_t parent);
70 static void iicdtor(void *data);
71 static int iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last);
72 static int iicuio(struct cdev *dev, struct uio *uio, int ioflag);
73 static int iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags);
75 static devclass_t iic_devclass;
77 static device_method_t iic_methods[] = {
78 /* device interface */
79 DEVMETHOD(device_identify, iic_identify),
80 DEVMETHOD(device_probe, iic_probe),
81 DEVMETHOD(device_attach, iic_attach),
82 DEVMETHOD(device_detach, iic_detach),
84 /* iicbus interface */
85 DEVMETHOD(iicbus_intr, iicbus_generic_intr),
90 static driver_t iic_driver = {
93 sizeof(struct iic_softc),
96 static d_open_t iicopen;
97 static d_ioctl_t iicioctl;
99 static struct cdevsw iic_cdevsw = {
100 .d_version = D_VERSION,
109 iic_identify(driver_t *driver, device_t parent)
112 if (device_find_child(parent, "iic", -1) == NULL)
113 BUS_ADD_CHILD(parent, 0, "iic", -1);
117 iic_probe(device_t dev)
119 if (iicbus_get_addr(dev) > 0)
122 device_set_desc(dev, "I2C generic I/O");
128 iic_attach(device_t dev)
130 struct iic_softc *sc;
132 sc = device_get_softc(dev);
134 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
136 0600, "iic%d", device_get_unit(dev));
137 if (sc->sc_devnode == NULL) {
138 device_printf(dev, "failed to create character device\n");
141 sc->sc_devnode->si_drv1 = sc;
147 iic_detach(device_t dev)
149 struct iic_softc *sc;
151 sc = device_get_softc(dev);
154 destroy_dev(sc->sc_devnode);
160 iicopen(struct cdev *dev, int flags, int fmt, struct thread *td)
162 struct iic_cdevpriv *priv;
165 priv = malloc(sizeof(*priv), M_IIC, M_WAITOK | M_ZERO);
167 sx_init(&priv->lock, "iic");
168 priv->sc = dev->si_drv1;
170 error = devfs_set_cdevpriv(priv, iicdtor);
180 device_t iicdev, parent;
181 struct iic_cdevpriv *priv;
184 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
186 iicdev = priv->sc->sc_dev;
187 parent = device_get_parent(iicdev);
191 iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
192 iicbus_release_bus(parent, iicdev);
195 sx_destroy(&priv->lock);
200 iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last)
203 int error, num_bytes, transferred_bytes, written_bytes;
206 parent = device_get_parent(priv->sc->sc_dev);
210 * We can only transfer up to sizeof(buffer) bytes in 1 shot, so loop until
211 * everything has been transferred.
213 while ((error == 0) && (uio->uio_resid > 0)) {
215 num_bytes = MIN(uio->uio_resid, sizeof(buffer));
216 transferred_bytes = 0;
218 if (uio->uio_rw == UIO_WRITE) {
219 error = uiomove(buffer, num_bytes, uio);
221 while ((error == 0) && (transferred_bytes < num_bytes)) {
223 error = iicbus_write(parent, &buffer[transferred_bytes],
224 num_bytes - transferred_bytes, &written_bytes, 0);
225 transferred_bytes += written_bytes;
228 } else if (uio->uio_rw == UIO_READ) {
229 error = iicbus_read(parent, buffer,
230 num_bytes, &transferred_bytes,
231 ((uio->uio_resid <= sizeof(buffer)) ? last : 0), 0);
233 error = uiomove(buffer, transferred_bytes, uio);
241 iicuio(struct cdev *dev, struct uio *uio, int ioflag)
244 struct iic_cdevpriv *priv;
249 error = devfs_get_cdevpriv((void**)&priv);
253 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
256 if (priv->started || (priv->addr == 0)) {
260 parent = device_get_parent(priv->sc->sc_dev);
262 error = iicbus_request_bus(parent, priv->sc->sc_dev,
263 (ioflag & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
269 if (uio->uio_rw == UIO_READ)
270 addr = priv->addr | LSB;
272 addr = priv->addr & ~LSB;
274 error = iicbus_start(parent, addr, 0);
277 iicbus_release_bus(parent, priv->sc->sc_dev);
282 error = iicuio_move(priv, uio, IIC_LAST_READ);
285 iicbus_release_bus(parent, priv->sc->sc_dev);
291 iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags)
293 struct iic_msg *buf, *m;
295 device_t iicdev, parent;
298 iicdev = priv->sc->sc_dev;
299 parent = device_get_parent(iicdev);
302 buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_IIC, M_WAITOK);
304 error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs);
306 /* Alloc kernel buffers for userland data, copyin write data */
307 usrbufs = malloc(sizeof(void *) * d->nmsgs, M_IIC, M_WAITOK | M_ZERO);
309 for (i = 0; i < d->nmsgs; i++) {
314 * At least init the buffer to NULL so we can safely free() it later.
315 * If the copyin() to buf failed, don't try to malloc bogus m->len.
320 m->buf = malloc(m->len, M_IIC, M_WAITOK);
321 if (!(m->flags & IIC_M_RD))
322 error = copyin(usrbufs[i], m->buf, m->len);
326 error = iicbus_request_bus(parent, iicdev,
327 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
330 error = iicbus_transfer(iicdev, buf, d->nmsgs);
331 iicbus_release_bus(parent, iicdev);
334 /* Copyout all read segments, free up kernel buffers */
335 for (i = 0; i < d->nmsgs; i++) {
337 if ((error == 0) && (m->flags & IIC_M_RD))
338 error = copyout(m->buf, usrbufs[i], m->len);
342 free(usrbufs, M_IIC);
348 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
350 device_t parent, iicdev;
354 struct iic_cdevpriv *priv;
357 s = (struct iiccmd *)data;
358 error = devfs_get_cdevpriv((void**)&priv);
362 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
364 iicdev = priv->sc->sc_dev;
365 parent = device_get_parent(iicdev);
375 error = iicbus_request_bus(parent, iicdev,
376 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
379 error = iicbus_start(parent, s->slave, 0);
382 priv->addr = s->slave;
383 priv->started = true;
385 iicbus_release_bus(parent, iicdev);
391 error = iicbus_stop(parent);
392 iicbus_release_bus(parent, iicdev);
393 priv->started = false;
400 * Bus should be owned before we reset it.
401 * We allow the bus to be already owned as the result of an in-progress
402 * sequence; however, bus reset will always be followed by release
403 * (a new start is presumably needed for I/O anyway). */
405 error = iicbus_request_bus(parent, iicdev,
406 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
409 error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
411 * Ignore IIC_ENOADDR as it only means we have a master-only
414 if (error == IIC_ENOADDR)
417 iicbus_release_bus(parent, iicdev);
418 priv->started = false;
423 if (!priv->started) {
427 uvec.iov_base = s->buf;
428 uvec.iov_len = s->count;
429 ubuf.uio_iov = &uvec;
431 ubuf.uio_segflg = UIO_USERSPACE;
433 ubuf.uio_resid = s->count;
435 ubuf.uio_rw = UIO_WRITE;
436 error = iicuio_move(priv, &ubuf, 0);
440 if (!priv->started) {
444 uvec.iov_base = s->buf;
445 uvec.iov_len = s->count;
446 ubuf.uio_iov = &uvec;
448 ubuf.uio_segflg = UIO_USERSPACE;
450 ubuf.uio_resid = s->count;
452 ubuf.uio_rw = UIO_READ;
453 error = iicuio_move(priv, &ubuf, s->last);
458 * The rdwr list should be a self-contained set of
459 * transactions. Fail if another transaction is in progress.
466 error = iicrdwr(priv, (struct iic_rdwr_data *)data, flags);
471 if (!priv->started) {
475 error = iicbus_repeated_start(parent, s->slave, 0);
479 priv->addr = *((uint8_t*)data);
490 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
491 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
492 MODULE_VERSION(iic, 1);