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