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