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: stable/10/sys/dev/iicbus/iic.c 300948 2016-05-29 07:14:51Z jah $
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
iic_identify(driver_t * driver,device_t parent)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
iic_probe(device_t dev)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
iic_attach(device_t dev)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
iic_detach(device_t dev)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
iicopen(struct cdev * dev,int flags,int fmt,struct thread * td)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
iicdtor(void * data)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
iicuio_move(struct iic_cdevpriv * priv,struct uio * uio,int last)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
iicuio(struct cdev * dev,struct uio * uio,int ioflag)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
iicrdwr(struct iic_cdevpriv * priv,struct iic_rdwr_data * d,int flags)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, i;
297 
298 	iicdev = priv->sc->sc_dev;
299 	parent = device_get_parent(iicdev);
300 	error = 0;
301 
302 	if (d->nmsgs > IIC_RDRW_MAX_MSGS)
303 		return (EINVAL);
304 
305 	buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_IIC, M_WAITOK);
306 
307 	error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs);
308 	if (error != 0) {
309 		free(buf, M_IIC);
310 		return (error);
311 	}
312 
313 	/* Alloc kernel buffers for userland data, copyin write data */
314 	usrbufs = malloc(sizeof(void *) * d->nmsgs, M_IIC, M_WAITOK | M_ZERO);
315 
316 	for (i = 0; i < d->nmsgs; i++) {
317 		m = &(buf[i]);
318 		usrbufs[i] = m->buf;
319 
320 		/*
321 		 * At least init the buffer to NULL so we can safely free() it later.
322 		 * If the copyin() to buf failed, don't try to malloc bogus m->len.
323 		 */
324 		m->buf = NULL;
325 		if (error != 0)
326 			continue;
327 
328 		/* m->len is uint16_t, so allocation size is capped at 64K. */
329 		m->buf = malloc(m->len, M_IIC, M_WAITOK);
330 		if (!(m->flags & IIC_M_RD))
331 			error = copyin(usrbufs[i], m->buf, m->len);
332 	}
333 
334 	if (error == 0)
335 		error = iicbus_request_bus(parent, iicdev,
336 		    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
337 
338 	if (error == 0) {
339 		error = iicbus_transfer(iicdev, buf, d->nmsgs);
340 		iicbus_release_bus(parent, iicdev);
341 	}
342 
343 	/* Copyout all read segments, free up kernel buffers */
344 	for (i = 0; i < d->nmsgs; i++) {
345 		m = &(buf[i]);
346 		if ((error == 0) && (m->flags & IIC_M_RD))
347 			error = copyout(m->buf, usrbufs[i], m->len);
348 		free(m->buf, M_IIC);
349 	}
350 
351 	free(usrbufs, M_IIC);
352 	free(buf, M_IIC);
353 	return (error);
354 }
355 
356 static int
iicioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)357 iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
358 {
359 	device_t parent, iicdev;
360 	struct iiccmd *s;
361 	struct uio ubuf;
362 	struct iovec uvec;
363 	struct iic_cdevpriv *priv;
364 	int error;
365 
366 	s = (struct iiccmd *)data;
367 	error = devfs_get_cdevpriv((void**)&priv);
368 	if (error != 0)
369 		return (error);
370 
371 	KASSERT(priv != NULL, ("iic cdevpriv should not be NULL!"));
372 
373 	iicdev = priv->sc->sc_dev;
374 	parent = device_get_parent(iicdev);
375 	IIC_LOCK(priv);
376 
377 
378 	switch (cmd) {
379 	case I2CSTART:
380 		if (priv->started) {
381 			error = EINVAL;
382 			break;
383 		}
384 		error = iicbus_request_bus(parent, iicdev,
385 		    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
386 
387 		if (error == 0)
388 			error = iicbus_start(parent, s->slave, 0);
389 
390 		if (error == 0) {
391 			priv->addr = s->slave;
392 			priv->started = true;
393 		} else
394 			iicbus_release_bus(parent, iicdev);
395 
396 		break;
397 
398 	case I2CSTOP:
399 		if (priv->started) {
400 			error = iicbus_stop(parent);
401 			iicbus_release_bus(parent, iicdev);
402 			priv->started = false;
403 		}
404 
405 		break;
406 
407 	case I2CRSTCARD:
408 		/*
409 		 * Bus should be owned before we reset it.
410 		 * We allow the bus to be already owned as the result of an in-progress
411 		 * sequence; however, bus reset will always be followed by release
412 		 * (a new start is presumably needed for I/O anyway). */
413 		if (!priv->started)
414 			error = iicbus_request_bus(parent, iicdev,
415 			    (flags & O_NONBLOCK) ? IIC_DONTWAIT : (IIC_WAIT | IIC_INTR));
416 
417 		if (error == 0) {
418 			error = iicbus_reset(parent, IIC_UNKNOWN, 0, NULL);
419 			/*
420 			 * Ignore IIC_ENOADDR as it only means we have a master-only
421 			 * controller.
422 			 */
423 			if (error == IIC_ENOADDR)
424 				error = 0;
425 
426 			iicbus_release_bus(parent, iicdev);
427 			priv->started = false;
428 		}
429 		break;
430 
431 	case I2CWRITE:
432 		if (!priv->started) {
433 			error = EINVAL;
434 			break;
435 		}
436 		uvec.iov_base = s->buf;
437 		uvec.iov_len = s->count;
438 		ubuf.uio_iov = &uvec;
439 		ubuf.uio_iovcnt = 1;
440 		ubuf.uio_segflg = UIO_USERSPACE;
441 		ubuf.uio_td = td;
442 		ubuf.uio_resid = s->count;
443 		ubuf.uio_offset = 0;
444 		ubuf.uio_rw = UIO_WRITE;
445 		error = iicuio_move(priv, &ubuf, 0);
446 		break;
447 
448 	case I2CREAD:
449 		if (!priv->started) {
450 			error = EINVAL;
451 			break;
452 		}
453 		uvec.iov_base = s->buf;
454 		uvec.iov_len = s->count;
455 		ubuf.uio_iov = &uvec;
456 		ubuf.uio_iovcnt = 1;
457 		ubuf.uio_segflg = UIO_USERSPACE;
458 		ubuf.uio_td = td;
459 		ubuf.uio_resid = s->count;
460 		ubuf.uio_offset = 0;
461 		ubuf.uio_rw = UIO_READ;
462 		error = iicuio_move(priv, &ubuf, s->last);
463 		break;
464 
465 	case I2CRDWR:
466 		/*
467 		 * The rdwr list should be a self-contained set of
468 		 * transactions.  Fail if another transaction is in progress.
469                  */
470 		if (priv->started) {
471 			error = EINVAL;
472 			break;
473 		}
474 
475 		error = iicrdwr(priv, (struct iic_rdwr_data *)data, flags);
476 
477 		break;
478 
479 	case I2CRPTSTART:
480 		if (!priv->started) {
481 			error = EINVAL;
482 			break;
483 		}
484 		error = iicbus_repeated_start(parent, s->slave, 0);
485 		break;
486 
487 	case I2CSADDR:
488 		priv->addr = *((uint8_t*)data);
489 		break;
490 
491 	default:
492 		error = ENOTTY;
493 	}
494 
495 	IIC_UNLOCK(priv);
496 	return (error);
497 }
498 
499 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
500 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
501 MODULE_VERSION(iic, 1);
502