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