xref: /freebsd/sys/dev/iicbus/iic.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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/bus.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <sys/lock.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sx.h>
38 #include <sys/systm.h>
39 #include <sys/uio.h>
40 #include <sys/errno.h>
41 
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iic.h>
45 
46 #include "iicbus_if.h"
47 
48 struct iic_softc {
49 	device_t sc_dev;
50 	struct cdev *sc_devnode;
51 };
52 
53 struct iic_cdevpriv {
54 	struct sx lock;
55 	struct iic_softc *sc;
56 	bool started;
57 	uint8_t addr;
58 };
59 
60 
61 #define	IIC_LOCK(cdp)			sx_xlock(&(cdp)->lock)
62 #define	IIC_UNLOCK(cdp)			sx_xunlock(&(cdp)->lock)
63 
64 static MALLOC_DEFINE(M_IIC, "iic", "I2C device data");
65 
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);
74 
75 static devclass_t iic_devclass;
76 
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),
83 
84 	/* iicbus interface */
85 	DEVMETHOD(iicbus_intr,		iicbus_generic_intr),
86 
87 	{ 0, 0 }
88 };
89 
90 static driver_t iic_driver = {
91 	"iic",
92 	iic_methods,
93 	sizeof(struct iic_softc),
94 };
95 
96 static	d_open_t	iicopen;
97 static	d_ioctl_t	iicioctl;
98 
99 static struct cdevsw iic_cdevsw = {
100 	.d_version =	D_VERSION,
101 	.d_open =	iicopen,
102 	.d_read =	iicuio,
103 	.d_write =	iicuio,
104 	.d_ioctl =	iicioctl,
105 	.d_name =	"iic",
106 };
107 
108 static void
109 iic_identify(driver_t *driver, device_t parent)
110 {
111 
112 	if (device_find_child(parent, "iic", -1) == NULL)
113 		BUS_ADD_CHILD(parent, 0, "iic", -1);
114 }
115 
116 static int
117 iic_probe(device_t dev)
118 {
119 	if (iicbus_get_addr(dev) > 0)
120 		return (ENXIO);
121 
122 	device_set_desc(dev, "I2C generic I/O");
123 
124 	return (0);
125 }
126 
127 static int
128 iic_attach(device_t dev)
129 {
130 	struct iic_softc *sc;
131 
132 	sc = device_get_softc(dev);
133 	sc->sc_dev = dev;
134 	sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
135 			UID_ROOT, GID_WHEEL,
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 		return (ENXIO);
140 	}
141 	sc->sc_devnode->si_drv1 = sc;
142 
143 	return (0);
144 }
145 
146 static int
147 iic_detach(device_t dev)
148 {
149 	struct iic_softc *sc;
150 
151 	sc = device_get_softc(dev);
152 
153 	if (sc->sc_devnode)
154 		destroy_dev(sc->sc_devnode);
155 
156 	return (0);
157 }
158 
159 static int
160 iicopen(struct cdev *dev, int flags, int fmt, struct thread *td)
161 {
162 	struct iic_cdevpriv *priv;
163 	int error;
164 
165 	priv = malloc(sizeof(*priv), M_IIC, M_WAITOK | M_ZERO);
166 
167 	sx_init(&priv->lock, "iic");
168 	priv->sc = dev->si_drv1;
169 
170 	error = devfs_set_cdevpriv(priv, iicdtor);
171 	if (error != 0)
172 		free(priv, M_IIC);
173 
174 	return (error);
175 }
176 
177 static void
178 iicdtor(void *data)
179 {
180 	device_t iicdev, parent;
181 	struct iic_cdevpriv *priv;
182 
183 	priv = data;
184 	KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
185 
186 	iicdev = priv->sc->sc_dev;
187 	parent = device_get_parent(iicdev);
188 
189 	if (priv->started) {
190 		iicbus_stop(parent);
191 		iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
192 		iicbus_release_bus(parent, iicdev);
193 	}
194 
195 	sx_destroy(&priv->lock);
196 	free(priv, M_IIC);
197 }
198 
199 static int
200 iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int last)
201 {
202 	device_t parent;
203 	int error, num_bytes, transferred_bytes, written_bytes;
204 	char buffer[128];
205 
206 	parent = device_get_parent(priv->sc->sc_dev);
207 	error = 0;
208 
209 	/*
210 	 * We can only transfer up to sizeof(buffer) bytes in 1 shot, so loop until
211 	 * everything has been transferred.
212 	*/
213 	while ((error == 0) && (uio->uio_resid > 0)) {
214 
215 		num_bytes = MIN(uio->uio_resid, sizeof(buffer));
216 		transferred_bytes = 0;
217 
218 		if (uio->uio_rw == UIO_WRITE) {
219 			error = uiomove(buffer, num_bytes, uio);
220 
221 			while ((error == 0) && (transferred_bytes < num_bytes)) {
222 				written_bytes = 0;
223 				error = iicbus_write(parent, &buffer[transferred_bytes],
224 				    num_bytes - transferred_bytes, &written_bytes, 0);
225 				transferred_bytes += written_bytes;
226 			}
227 
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);
232 			if (error == 0)
233 				error = uiomove(buffer, transferred_bytes, uio);
234 		}
235 	}
236 
237 	return (error);
238 }
239 
240 static int
241 iicuio(struct cdev *dev, struct uio *uio, int ioflag)
242 {
243 	device_t parent;
244 	struct iic_cdevpriv *priv;
245 	int error;
246 	uint8_t addr;
247 
248 	priv = NULL;
249 	error = devfs_get_cdevpriv((void**)&priv);
250 
251 	if (error != 0)
252 		return (error);
253 	KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
254 
255 	IIC_LOCK(priv);
256 	if (priv->started || (priv->addr == 0)) {
257 		IIC_UNLOCK(priv);
258 		return (ENXIO);
259 	}
260 	parent = device_get_parent(priv->sc->sc_dev);
261 
262 	error = iicbus_request_bus(parent, priv->sc->sc_dev,
263 	    (ioflag & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
264 	if (error != 0) {
265 		IIC_UNLOCK(priv);
266 		return (error);
267 	}
268 
269 	if (uio->uio_rw == UIO_READ)
270 		addr = priv->addr | LSB;
271 	else
272 		addr = priv->addr & ~LSB;
273 
274 	error = iicbus_start(parent, addr, 0);
275 	if (error != 0)
276 	{
277 		iicbus_release_bus(parent, priv->sc->sc_dev);
278 		IIC_UNLOCK(priv);
279 		return (error);
280 	}
281 
282 	error = iicuio_move(priv, uio, IIC_LAST_READ);
283 
284 	iicbus_stop(parent);
285 	iicbus_release_bus(parent, priv->sc->sc_dev);
286 	IIC_UNLOCK(priv);
287 	return (error);
288 }
289 
290 static int
291 iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags)
292 {
293 	struct iic_msg *buf, *m;
294 	void **usrbufs;
295 	device_t iicdev, parent;
296 	int error;
297 	uint32_t i;
298 
299 	iicdev = priv->sc->sc_dev;
300 	parent = device_get_parent(iicdev);
301 	error = 0;
302 
303 	buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_IIC, M_WAITOK);
304 
305 	error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs);
306 
307 	/* Alloc kernel buffers for userland data, copyin write data */
308 	usrbufs = malloc(sizeof(void *) * d->nmsgs, M_IIC, M_WAITOK | M_ZERO);
309 
310 	for (i = 0; i < d->nmsgs; i++) {
311 		m = &(buf[i]);
312 		usrbufs[i] = m->buf;
313 
314 		/*
315 		 * At least init the buffer to NULL so we can safely free() it later.
316 		 * If the copyin() to buf failed, don't try to malloc bogus m->len.
317 		 */
318 		m->buf = NULL;
319 		if (error != 0)
320 			continue;
321 		m->buf = malloc(m->len, M_IIC, M_WAITOK);
322 		if (!(m->flags & IIC_M_RD))
323 			error = copyin(usrbufs[i], m->buf, m->len);
324 	}
325 
326 	if (error == 0)
327 		error = iicbus_request_bus(parent, iicdev,
328 		    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
329 
330 	if (error == 0) {
331 		error = iicbus_transfer(iicdev, buf, d->nmsgs);
332 		iicbus_release_bus(parent, iicdev);
333 	}
334 
335 	/* Copyout all read segments, free up kernel buffers */
336 	for (i = 0; i < d->nmsgs; i++) {
337 		m = &(buf[i]);
338 		if ((error == 0) && (m->flags & IIC_M_RD))
339 			error = copyout(m->buf, usrbufs[i], m->len);
340 		free(m->buf, M_IIC);
341 	}
342 
343 	free(usrbufs, M_IIC);
344 	free(buf, M_IIC);
345 	return (error);
346 }
347 
348 static int
349 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
350 {
351 	device_t parent, iicdev;
352 	struct iiccmd *s;
353 	struct uio ubuf;
354 	struct iovec uvec;
355 	struct iic_cdevpriv *priv;
356 	int error;
357 
358 	s = (struct iiccmd *)data;
359 	error = devfs_get_cdevpriv((void**)&priv);
360 	if (error != 0)
361 		return (error);
362 
363 	KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
364 
365 	iicdev = priv->sc->sc_dev;
366 	parent = device_get_parent(iicdev);
367 	IIC_LOCK(priv);
368 
369 
370 	switch (cmd) {
371 	case I2CSTART:
372 		if (priv->started) {
373 			error = EINVAL;
374 			break;
375 		}
376 		error = iicbus_request_bus(parent, iicdev,
377 		    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
378 
379 		if (error == 0)
380 			error = iicbus_start(parent, s->slave, 0);
381 
382 		if (error == 0) {
383 			priv->addr = s->slave;
384 			priv->started = true;
385 		} else
386 			iicbus_release_bus(parent, iicdev);
387 
388 		break;
389 
390 	case I2CSTOP:
391 		if (priv->started) {
392 			error = iicbus_stop(parent);
393 			iicbus_release_bus(parent, iicdev);
394 			priv->started = false;
395 		}
396 
397 		break;
398 
399 	case I2CRSTCARD:
400 		/*
401 		 * Bus should be owned before we reset it.
402 		 * We allow the bus to be already owned as the result of an in-progress
403 		 * sequence; however, bus reset will always be followed by release
404 		 * (a new start is presumably needed for I/O anyway). */
405 		if (!priv->started)
406 			error = iicbus_request_bus(parent, iicdev,
407 			    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
408 
409 		if (error == 0) {
410 			error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
411 			/*
412 			 * Ignore IIC_ENOADDR as it only means we have a master-only
413 			 * controller.
414 			 */
415 			if (error == IIC_ENOADDR)
416 				error = 0;
417 
418 			iicbus_release_bus(parent, iicdev);
419 			priv->started = false;
420 		}
421 		break;
422 
423 	case I2CWRITE:
424 		if (!priv->started) {
425 			error = EINVAL;
426 			break;
427 		}
428 		uvec.iov_base = s->buf;
429 		uvec.iov_len = s->count;
430 		ubuf.uio_iov = &uvec;
431 		ubuf.uio_iovcnt = 1;
432 		ubuf.uio_segflg = UIO_USERSPACE;
433 		ubuf.uio_td = td;
434 		ubuf.uio_resid = s->count;
435 		ubuf.uio_offset = 0;
436 		ubuf.uio_rw = UIO_WRITE;
437 		error = iicuio_move(priv, &ubuf, 0);
438 		break;
439 
440 	case I2CREAD:
441 		if (!priv->started) {
442 			error = EINVAL;
443 			break;
444 		}
445 		uvec.iov_base = s->buf;
446 		uvec.iov_len = s->count;
447 		ubuf.uio_iov = &uvec;
448 		ubuf.uio_iovcnt = 1;
449 		ubuf.uio_segflg = UIO_USERSPACE;
450 		ubuf.uio_td = td;
451 		ubuf.uio_resid = s->count;
452 		ubuf.uio_offset = 0;
453 		ubuf.uio_rw = UIO_READ;
454 		error = iicuio_move(priv, &ubuf, s->last);
455 		break;
456 
457 	case I2CRDWR:
458 		/*
459 		 * The rdwr list should be a self-contained set of
460 		 * transactions.  Fail if another transaction is in progress.
461                  */
462 		if (priv->started) {
463 			error = EINVAL;
464 			break;
465 		}
466 
467 		error = iicrdwr(priv, (struct iic_rdwr_data *)data, flags);
468 
469 		break;
470 
471 	case I2CRPTSTART:
472 		if (!priv->started) {
473 			error = EINVAL;
474 			break;
475 		}
476 		error = iicbus_repeated_start(parent, s->slave, 0);
477 		break;
478 
479 	case I2CSADDR:
480 		priv->addr = *((uint8_t*)data);
481 		break;
482 
483 	default:
484 		error = ENOTTY;
485 	}
486 
487 	IIC_UNLOCK(priv);
488 	return (error);
489 }
490 
491 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
492 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
493 MODULE_VERSION(iic, 1);
494