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>
41 #include <dev/iicbus/iiconf.h>
42 #include <dev/iicbus/iicbus.h>
43 #include <dev/iicbus/iic.h>
45 #include "iicbus_if.h"
52 u_char sc_addr; /* 7 bit address on iicbus */
53 int sc_count; /* >0 if device opened */
55 char sc_buffer[BUFSIZE]; /* output buffer */
56 char sc_inbuf[BUFSIZE]; /* input buffer */
58 struct cdev *sc_devnode;
62 #define IIC_LOCK(sc) sx_xlock(&(sc)->sc_lock)
63 #define IIC_UNLOCK(sc) sx_xunlock(&(sc)->sc_lock)
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);
70 static devclass_t iic_devclass;
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),
79 /* iicbus interface */
80 DEVMETHOD(iicbus_intr, iicbus_generic_intr),
85 static driver_t iic_driver = {
88 sizeof(struct iic_softc),
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;
97 static struct cdevsw iic_cdevsw = {
98 .d_version = D_VERSION,
99 .d_flags = D_TRACKCLOSE,
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 = (struct iic_softc *)device_get_softc(dev);
133 sx_init(&sc->sc_lock, "iic");
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");
139 sx_destroy(&sc->sc_lock);
142 sc->sc_devnode->si_drv1 = sc;
148 iic_detach(device_t dev)
150 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
153 destroy_dev(sc->sc_devnode);
154 sx_destroy(&sc->sc_lock);
160 iicopen(struct cdev *dev, int flags, int fmt, struct thread *td)
162 struct iic_softc *sc = dev->si_drv1;
165 if (sc->sc_count > 0) {
177 iicclose(struct cdev *dev, int flags, int fmt, struct thread *td)
179 struct iic_softc *sc = dev->si_drv1;
183 /* XXX: I don't think this can happen. */
190 if (sc->sc_count < 0)
191 panic("%s: iic_count < 0!", __func__);
198 iicwrite(struct cdev *dev, struct uio * uio, int ioflag)
200 struct iic_softc *sc = dev->si_drv1;
201 device_t iicdev = sc->sc_dev;
202 int sent, error, count;
210 if (sc->sc_count == 0) {
211 /* XXX: I don't think this can happen. */
216 error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
223 count = min(uio->uio_resid, BUFSIZE);
224 uiomove(sc->sc_buffer, count, uio);
226 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
227 sc->sc_buffer, count, &sent);
229 iicbus_release_bus(device_get_parent(iicdev), iicdev);
236 iicread(struct cdev *dev, struct uio * uio, int ioflag)
238 struct iic_softc *sc = dev->si_drv1;
239 device_t iicdev = sc->sc_dev;
249 if (sc->sc_count == 0) {
250 /* XXX: I don't think this can happen. */
255 error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
262 /* max amount of data to read */
263 len = min(uio->uio_resid, BUFSIZE);
265 error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
266 sc->sc_inbuf, len, &bufsize);
272 if (bufsize > uio->uio_resid)
273 panic("%s: too much data read!", __func__);
275 iicbus_release_bus(device_get_parent(iicdev), iicdev);
277 error = uiomove(sc->sc_inbuf, bufsize, uio);
283 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
285 struct iic_softc *sc = dev->si_drv1;
286 device_t iicdev = sc->sc_dev;
287 device_t parent = device_get_parent(iicdev);
288 struct iiccmd *s = (struct iiccmd *)data;
289 struct iic_rdwr_data *d = (struct iic_rdwr_data *)data;
293 void **usrbufs = NULL;
295 if ((error = iicbus_request_bus(parent, iicdev,
296 (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR))))
302 error = iicbus_start(parent, s->slave, 0);
305 * Implicitly set the chip addr to the slave addr passed as
306 * parameter. Consequently, start/stop shall be called before
307 * the read or the write of a block.
310 sc->sc_addr = s->slave;
316 error = iicbus_stop(parent);
320 error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
328 buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK);
329 error = copyin(s->buf, buf, s->count);
332 error = iicbus_write(parent, buf, s->count, &count, 10);
340 buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK);
341 error = iicbus_read(parent, buf, s->count, &count, s->last, 10);
344 error = copyout(buf, s->buf, s->count);
348 buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_TEMP, M_WAITOK);
349 error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs);
352 /* Alloc kernel buffers for userland data, copyin write data */
353 usrbufs = malloc(sizeof(void *) * d->nmsgs, M_TEMP, M_ZERO | M_WAITOK);
354 for (i = 0; i < d->nmsgs; i++) {
355 m = &((struct iic_msg *)buf)[i];
357 m->buf = malloc(m->len, M_TEMP, M_WAITOK);
358 if (!(m->flags & IIC_M_RD))
359 copyin(usrbufs[i], m->buf, m->len);
361 error = iicbus_transfer(iicdev, (struct iic_msg *)buf, d->nmsgs);
362 /* Copyout all read segments, free up kernel buffers */
363 for (i = 0; i < d->nmsgs; i++) {
364 m = &((struct iic_msg *)buf)[i];
365 if (m->flags & IIC_M_RD)
366 copyout(m->buf, usrbufs[i], m->len);
367 free(m->buf, M_TEMP);
369 free(usrbufs, M_TEMP);
373 error = iicbus_repeated_start(parent, s->slave, 0);
380 iicbus_release_bus(parent, iicdev);
387 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
388 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
389 MODULE_VERSION(iic, 1);