2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
34 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/systm.h>
42 #include <sys/errno.h>
44 #include <dev/iicbus/iiconf.h>
45 #include <dev/iicbus/iicbus.h>
46 #include <dev/iicbus/iic.h>
48 #include "iicbus_if.h"
52 struct cdev *sc_devnode;
63 #define IIC_LOCK(cdp) sx_xlock(&(cdp)->lock)
64 #define IIC_UNLOCK(cdp) sx_xunlock(&(cdp)->lock)
66 static MALLOC_DEFINE(M_IIC, "iic", "I2C device data");
68 static int iic_probe(device_t);
69 static int iic_attach(device_t);
70 static int iic_detach(device_t);
71 static void iic_identify(driver_t *driver, device_t parent);
72 static void iicdtor(void *data);
73 static int iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last);
74 static int iicuio(struct cdev *dev, struct uio *uio, int ioflag);
75 static int iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags);
77 static devclass_t iic_devclass;
79 static device_method_t iic_methods[] = {
80 /* device interface */
81 DEVMETHOD(device_identify, iic_identify),
82 DEVMETHOD(device_probe, iic_probe),
83 DEVMETHOD(device_attach, iic_attach),
84 DEVMETHOD(device_detach, iic_detach),
86 /* iicbus interface */
87 DEVMETHOD(iicbus_intr, iicbus_generic_intr),
92 static driver_t iic_driver = {
95 sizeof(struct iic_softc),
98 static d_open_t iicopen;
99 static d_ioctl_t iicioctl;
101 static struct cdevsw iic_cdevsw = {
102 .d_version = D_VERSION,
111 iic_identify(driver_t *driver, device_t parent)
114 if (device_find_child(parent, "iic", -1) == NULL)
115 BUS_ADD_CHILD(parent, 0, "iic", -1);
119 iic_probe(device_t dev)
121 if (iicbus_get_addr(dev) > 0)
124 device_set_desc(dev, "I2C generic I/O");
130 iic_attach(device_t dev)
132 struct iic_softc *sc;
134 sc = device_get_softc(dev);
136 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
138 0600, "iic%d", device_get_unit(dev));
139 if (sc->sc_devnode == NULL) {
140 device_printf(dev, "failed to create character device\n");
143 sc->sc_devnode->si_drv1 = sc;
149 iic_detach(device_t dev)
151 struct iic_softc *sc;
153 sc = device_get_softc(dev);
156 destroy_dev(sc->sc_devnode);
162 iicopen(struct cdev *dev, int flags, int fmt, struct thread *td)
164 struct iic_cdevpriv *priv;
167 priv = malloc(sizeof(*priv), M_IIC, M_WAITOK | M_ZERO);
169 sx_init(&priv->lock, "iic");
170 priv->sc = dev->si_drv1;
172 error = devfs_set_cdevpriv(priv, iicdtor);
182 device_t iicdev, parent;
183 struct iic_cdevpriv *priv;
186 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
188 iicdev = priv->sc->sc_dev;
189 parent = device_get_parent(iicdev);
193 iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
194 iicbus_release_bus(parent, iicdev);
197 sx_destroy(&priv->lock);
202 iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last)
205 int error, num_bytes, transferred_bytes, written_bytes;
208 parent = device_get_parent(priv->sc->sc_dev);
212 * We can only transfer up to sizeof(buffer) bytes in 1 shot, so loop until
213 * everything has been transferred.
215 while ((error == 0) && (uio->uio_resid > 0)) {
217 num_bytes = MIN(uio->uio_resid, sizeof(buffer));
218 transferred_bytes = 0;
220 if (uio->uio_rw == UIO_WRITE) {
221 error = uiomove(buffer, num_bytes, uio);
223 while ((error == 0) && (transferred_bytes < num_bytes)) {
225 error = iicbus_write(parent, &buffer[transferred_bytes],
226 num_bytes - transferred_bytes, &written_bytes, 0);
227 transferred_bytes += written_bytes;
230 } else if (uio->uio_rw == UIO_READ) {
231 error = iicbus_read(parent, buffer,
232 num_bytes, &transferred_bytes,
233 ((uio->uio_resid <= sizeof(buffer)) ? last : 0), 0);
235 error = uiomove(buffer, transferred_bytes, uio);
243 iicuio(struct cdev *dev, struct uio *uio, int ioflag)
246 struct iic_cdevpriv *priv;
251 error = devfs_get_cdevpriv((void**)&priv);
255 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
258 if (priv->started || (priv->addr == 0)) {
262 parent = device_get_parent(priv->sc->sc_dev);
264 error = iicbus_request_bus(parent, priv->sc->sc_dev,
265 (ioflag & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
271 if (uio->uio_rw == UIO_READ)
272 addr = priv->addr | LSB;
274 addr = priv->addr & ~LSB;
276 error = iicbus_start(parent, addr, 0);
279 iicbus_release_bus(parent, priv->sc->sc_dev);
284 error = iicuio_move(priv, uio, IIC_LAST_READ);
287 iicbus_release_bus(parent, priv->sc->sc_dev);
293 iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags)
295 struct iic_msg *buf, *m;
297 device_t iicdev, parent;
301 iicdev = priv->sc->sc_dev;
302 parent = device_get_parent(iicdev);
305 if (d->nmsgs > IIC_RDRW_MAX_MSGS)
308 buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_IIC, M_WAITOK);
310 error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs);
316 /* Alloc kernel buffers for userland data, copyin write data */
317 usrbufs = malloc(sizeof(void *) * d->nmsgs, M_IIC, M_WAITOK | M_ZERO);
319 for (i = 0; i < d->nmsgs; i++) {
324 * At least init the buffer to NULL so we can safely free() it later.
325 * If the copyin() to buf failed, don't try to malloc bogus m->len.
331 /* m->len is uint16_t, so allocation size is capped at 64K. */
332 m->buf = malloc(m->len, M_IIC, M_WAITOK);
333 if (!(m->flags & IIC_M_RD))
334 error = copyin(usrbufs[i], m->buf, m->len);
338 error = iicbus_request_bus(parent, iicdev,
339 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
342 error = iicbus_transfer(iicdev, buf, d->nmsgs);
343 iicbus_release_bus(parent, iicdev);
346 /* Copyout all read segments, free up kernel buffers */
347 for (i = 0; i < d->nmsgs; i++) {
349 if ((error == 0) && (m->flags & IIC_M_RD))
350 error = copyout(m->buf, usrbufs[i], m->len);
354 free(usrbufs, M_IIC);
360 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
362 device_t parent, iicdev;
366 struct iic_cdevpriv *priv;
369 s = (struct iiccmd *)data;
370 error = devfs_get_cdevpriv((void**)&priv);
374 KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
376 iicdev = priv->sc->sc_dev;
377 parent = device_get_parent(iicdev);
387 error = iicbus_request_bus(parent, iicdev,
388 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
391 error = iicbus_start(parent, s->slave, 0);
394 priv->addr = s->slave;
395 priv->started = true;
397 iicbus_release_bus(parent, iicdev);
403 error = iicbus_stop(parent);
404 iicbus_release_bus(parent, iicdev);
405 priv->started = false;
412 * Bus should be owned before we reset it.
413 * We allow the bus to be already owned as the result of an in-progress
414 * sequence; however, bus reset will always be followed by release
415 * (a new start is presumably needed for I/O anyway). */
417 error = iicbus_request_bus(parent, iicdev,
418 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
421 error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
423 * Ignore IIC_ENOADDR as it only means we have a master-only
426 if (error == IIC_ENOADDR)
429 iicbus_release_bus(parent, iicdev);
430 priv->started = false;
435 if (!priv->started) {
439 uvec.iov_base = s->buf;
440 uvec.iov_len = s->count;
441 ubuf.uio_iov = &uvec;
443 ubuf.uio_segflg = UIO_USERSPACE;
445 ubuf.uio_resid = s->count;
447 ubuf.uio_rw = UIO_WRITE;
448 error = iicuio_move(priv, &ubuf, 0);
452 if (!priv->started) {
456 uvec.iov_base = s->buf;
457 uvec.iov_len = s->count;
458 ubuf.uio_iov = &uvec;
460 ubuf.uio_segflg = UIO_USERSPACE;
462 ubuf.uio_resid = s->count;
464 ubuf.uio_rw = UIO_READ;
465 error = iicuio_move(priv, &ubuf, s->last);
470 * The rdwr list should be a self-contained set of
471 * transactions. Fail if another transaction is in progress.
478 error = iicrdwr(priv, (struct iic_rdwr_data *)data, flags);
483 if (!priv->started) {
487 error = iicbus_repeated_start(parent, s->slave, 0);
491 priv->addr = *((uint8_t*)data);
502 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
503 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
504 MODULE_VERSION(iic, 1);