xref: /freebsd/sys/dev/gpio/gpioc.c (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/gpio.h>
37 #include <sys/ioccom.h>
38 #include <sys/filio.h>
39 #include <sys/fcntl.h>
40 #include <sys/sigio.h>
41 #include <sys/signalvar.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/uio.h>
45 #include <sys/poll.h>
46 #include <sys/selinfo.h>
47 #include <sys/module.h>
48 
49 #include <dev/gpio/gpiobusvar.h>
50 
51 #include "gpio_if.h"
52 #include "gpiobus_if.h"
53 
54 #undef GPIOC_DEBUG
55 #ifdef GPIOC_DEBUG
56 #define dprintf printf
57 #define ddevice_printf device_printf
58 #else
59 #define dprintf(x, arg...)
60 #define ddevice_printf(dev, x, arg...)
61 #endif
62 
63 struct gpioc_softc {
64 	device_t		sc_dev;		/* gpiocX dev */
65 	device_t		sc_pdev;	/* gpioX dev */
66 	struct cdev		*sc_ctl_dev;	/* controller device */
67 	int			sc_unit;
68 	int			sc_npins;
69 	struct gpioc_pin_intr	*sc_pin_intr;
70 };
71 
72 struct gpioc_pin_intr {
73 	struct gpioc_softc				*sc;
74 	gpio_pin_t					pin;
75 	bool						config_locked;
76 	int						intr_rid;
77 	struct resource					*intr_res;
78 	void						*intr_cookie;
79 	struct mtx					mtx;
80 	SLIST_HEAD(gpioc_privs_list, gpioc_privs)	privs;
81 };
82 
83 
84 struct gpioc_cdevpriv {
85 	struct gpioc_softc			*sc;
86 	struct selinfo				selinfo;
87 	bool					async;
88 	uint8_t					report_option;
89 	struct sigio				*sigio;
90 	struct mtx				mtx;
91 	struct gpioc_pin_event			*events;
92 	int					numevents;
93 	int					evidx_head;
94 	int					evidx_tail;
95 	SLIST_HEAD(gpioc_pins_list, gpioc_pins)	pins;
96 };
97 
98 struct gpioc_privs {
99 	struct gpioc_cdevpriv		*priv;
100 	SLIST_ENTRY(gpioc_privs)	next;
101 };
102 
103 struct gpioc_pins {
104 	struct gpioc_pin_intr	*pin;
105 	int			eventcount;
106 	int			firstevent;
107 	SLIST_ENTRY(gpioc_pins)	next;
108 };
109 
110 struct gpioc_pin_event {
111 	struct gpioc_pins	*privpin;
112 	sbintime_t		event_time;
113 	bool			event_pin_state;
114 };
115 
116 static MALLOC_DEFINE(M_GPIOC, "gpioc", "gpioc device data");
117 
118 static int	gpioc_allocate_pin_intr(struct gpioc_pin_intr*, uint32_t);
119 static int	gpioc_release_pin_intr(struct gpioc_pin_intr*);
120 static int	gpioc_attach_priv_pin(struct gpioc_cdevpriv*,
121 		    struct gpioc_pin_intr*);
122 static int	gpioc_detach_priv_pin(struct gpioc_cdevpriv*,
123 		    struct gpioc_pin_intr*);
124 static bool	gpioc_intr_reconfig_allowed(struct gpioc_cdevpriv*,
125 		    struct gpioc_pin_intr *intr_conf);
126 static uint32_t	gpioc_get_intr_config(struct gpioc_softc*,
127 		    struct gpioc_cdevpriv*, uint32_t pin);
128 static int	gpioc_set_intr_config(struct gpioc_softc*,
129 		    struct gpioc_cdevpriv*, uint32_t, uint32_t);
130 static void	gpioc_interrupt_handler(void*);
131 
132 static int	gpioc_kqread(struct knote*, long);
133 static void	gpioc_kqdetach(struct knote*);
134 
135 static int	gpioc_probe(device_t dev);
136 static int	gpioc_attach(device_t dev);
137 static int	gpioc_detach(device_t dev);
138 
139 static void	gpioc_cdevpriv_dtor(void*);
140 
141 static d_open_t		gpioc_open;
142 static d_read_t		gpioc_read;
143 static d_ioctl_t	gpioc_ioctl;
144 static d_poll_t		gpioc_poll;
145 static d_kqfilter_t	gpioc_kqfilter;
146 
147 static struct cdevsw gpioc_cdevsw = {
148 	.d_version	= D_VERSION,
149 	.d_open		= gpioc_open,
150 	.d_read		= gpioc_read,
151 	.d_ioctl	= gpioc_ioctl,
152 	.d_poll		= gpioc_poll,
153 	.d_kqfilter	= gpioc_kqfilter,
154 	.d_name		= "gpioc",
155 };
156 
157 static struct filterops gpioc_read_filterops = {
158 	.f_isfd =	true,
159 	.f_attach =	NULL,
160 	.f_detach =	gpioc_kqdetach,
161 	.f_event =	gpioc_kqread,
162 	.f_touch =	NULL
163 };
164 
165 static struct gpioc_pin_event *
166 next_head_event(struct gpioc_cdevpriv *priv)
167 {
168 	struct gpioc_pin_event *rv;
169 
170 	rv = &priv->events[priv->evidx_head++];
171 	if (priv->evidx_head == priv->numevents)
172 		priv->evidx_head = 0;
173 	return (rv);
174 }
175 
176 static struct gpioc_pin_event *
177 next_tail_event(struct gpioc_cdevpriv *priv)
178 {
179 	struct gpioc_pin_event *rv;
180 
181 	rv = &priv->events[priv->evidx_tail++];
182 	if (priv->evidx_tail == priv->numevents)
183 		priv->evidx_tail = 0;
184 	return (rv);
185 }
186 
187 static size_t
188 number_of_events(struct gpioc_cdevpriv *priv)
189 {
190 	if (priv->evidx_head >= priv->evidx_tail)
191 		return (priv->evidx_head - priv->evidx_tail);
192 	else
193 		return (priv->numevents + priv->evidx_head - priv->evidx_tail);
194 }
195 
196 static int
197 gpioc_allocate_pin_intr(struct gpioc_pin_intr *intr_conf, uint32_t flags)
198 {
199 	int err;
200 
201 	intr_conf->config_locked = true;
202 	mtx_unlock(&intr_conf->mtx);
203 
204 	intr_conf->intr_res = gpio_alloc_intr_resource(intr_conf->pin->dev,
205 	    &intr_conf->intr_rid, RF_ACTIVE, intr_conf->pin, flags);
206 	if (intr_conf->intr_res == NULL) {
207 		err = ENXIO;
208 		goto error_exit;
209 	}
210 
211 	err = bus_setup_intr(intr_conf->pin->dev, intr_conf->intr_res,
212 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, gpioc_interrupt_handler,
213 	    intr_conf, &intr_conf->intr_cookie);
214 	if (err != 0)
215 		goto error_exit;
216 
217 	intr_conf->pin->flags = flags;
218 
219 error_exit:
220 	mtx_lock(&intr_conf->mtx);
221 	intr_conf->config_locked = false;
222 	wakeup(&intr_conf->config_locked);
223 
224 	return (err);
225 }
226 
227 static int
228 gpioc_release_pin_intr(struct gpioc_pin_intr *intr_conf)
229 {
230 	int err;
231 
232 	intr_conf->config_locked = true;
233 	mtx_unlock(&intr_conf->mtx);
234 
235 	if (intr_conf->intr_cookie != NULL) {
236 		err = bus_teardown_intr(intr_conf->pin->dev,
237 		    intr_conf->intr_res, intr_conf->intr_cookie);
238 		if (err != 0)
239 			goto error_exit;
240 		else
241 			intr_conf->intr_cookie = NULL;
242 	}
243 
244 	if (intr_conf->intr_res != NULL) {
245 		err = bus_release_resource(intr_conf->pin->dev, SYS_RES_IRQ,
246 		    intr_conf->intr_rid, intr_conf->intr_res);
247 		if (err != 0)
248 			goto error_exit;
249 		else {
250 			intr_conf->intr_rid = 0;
251 			intr_conf->intr_res = NULL;
252 		}
253 	}
254 
255 	intr_conf->pin->flags = 0;
256 	err = 0;
257 
258 error_exit:
259 	mtx_lock(&intr_conf->mtx);
260 	intr_conf->config_locked = false;
261 	wakeup(&intr_conf->config_locked);
262 
263 	return (err);
264 }
265 
266 static int
267 gpioc_attach_priv_pin(struct gpioc_cdevpriv *priv,
268     struct gpioc_pin_intr *intr_conf)
269 {
270 	struct gpioc_privs	*priv_link;
271 	struct gpioc_pins	*pin_link;
272 	unsigned int		consistency_a __diagused;
273 	unsigned int		consistency_b __diagused;
274 
275 	consistency_a = 0;
276 	consistency_b = 0;
277 	mtx_assert(&intr_conf->mtx, MA_OWNED);
278 	mtx_lock(&priv->mtx);
279 	SLIST_FOREACH(priv_link, &intr_conf->privs, next) {
280 		if (priv_link->priv == priv)
281 			consistency_a++;
282 	}
283 	KASSERT(consistency_a <= 1,
284 	    ("inconsistent links between pin config and cdevpriv"));
285 	SLIST_FOREACH(pin_link, &priv->pins, next) {
286 		if (pin_link->pin == intr_conf)
287 			consistency_b++;
288 	}
289 	KASSERT(consistency_a == consistency_b,
290 	    ("inconsistent links between pin config and cdevpriv"));
291 	if (consistency_a == 1 && consistency_b == 1) {
292 		mtx_unlock(&priv->mtx);
293 		return (EEXIST);
294 	}
295 	priv_link = malloc(sizeof(struct gpioc_privs), M_GPIOC,
296 	    M_NOWAIT | M_ZERO);
297 	if (priv_link == NULL)
298 	{
299 		mtx_unlock(&priv->mtx);
300 		return (ENOMEM);
301 	}
302 	pin_link = malloc(sizeof(struct gpioc_pins), M_GPIOC,
303 	    M_NOWAIT | M_ZERO);
304 	if (pin_link == NULL) {
305 		mtx_unlock(&priv->mtx);
306 		return (ENOMEM);
307 	}
308 	priv_link->priv = priv;
309 	pin_link->pin = intr_conf;
310 	SLIST_INSERT_HEAD(&intr_conf->privs, priv_link, next);
311 	SLIST_INSERT_HEAD(&priv->pins, pin_link, next);
312 	mtx_unlock(&priv->mtx);
313 
314 	return (0);
315 }
316 
317 static int
318 gpioc_detach_priv_pin(struct gpioc_cdevpriv *priv,
319     struct gpioc_pin_intr *intr_conf)
320 {
321 	struct gpioc_privs	*priv_link, *priv_link_temp;
322 	struct gpioc_pins	*pin_link, *pin_link_temp;
323 	unsigned int		consistency_a, consistency_b;
324 
325 	consistency_a = 0;
326 	consistency_b = 0;
327 	mtx_assert(&intr_conf->mtx, MA_OWNED);
328 	mtx_lock(&priv->mtx);
329 	SLIST_FOREACH_SAFE(priv_link, &intr_conf->privs, next, priv_link_temp) {
330 		if (priv_link->priv == priv) {
331 			SLIST_REMOVE(&intr_conf->privs, priv_link, gpioc_privs,
332 			    next);
333 			free(priv_link, M_GPIOC);
334 			consistency_a++;
335 		}
336 	}
337 	KASSERT(consistency_a <= 1,
338 	    ("inconsistent links between pin config and cdevpriv"));
339 	SLIST_FOREACH_SAFE(pin_link, &priv->pins, next, pin_link_temp) {
340 		if (pin_link->pin == intr_conf) {
341 			/*
342 			 * If the pin we're removing has events in the priv's
343 			 * event fifo, we can't leave dangling pointers from
344 			 * those events to the gpioc_pins struct we're about to
345 			 * free.  We also can't remove random items and leave
346 			 * holes in the events fifo, so just empty it out.
347 			 */
348 			if (pin_link->eventcount > 0) {
349 				priv->evidx_head = priv->evidx_tail = 0;
350 			}
351 			SLIST_REMOVE(&priv->pins, pin_link, gpioc_pins, next);
352 			free(pin_link, M_GPIOC);
353 			consistency_b++;
354 		}
355 	}
356 	KASSERT(consistency_a == consistency_b,
357 	    ("inconsistent links between pin config and cdevpriv"));
358 	mtx_unlock(&priv->mtx);
359 
360 	return (0);
361 }
362 
363 static bool
364 gpioc_intr_reconfig_allowed(struct gpioc_cdevpriv *priv,
365     struct gpioc_pin_intr *intr_conf)
366 {
367 	struct gpioc_privs	*priv_link;
368 
369 	mtx_assert(&intr_conf->mtx, MA_OWNED);
370 
371 	if (SLIST_EMPTY(&intr_conf->privs))
372 		return (true);
373 
374 	SLIST_FOREACH(priv_link, &intr_conf->privs, next) {
375 		if (priv_link->priv != priv)
376 			return (false);
377 	}
378 
379 	return (true);
380 }
381 
382 
383 static uint32_t
384 gpioc_get_intr_config(struct gpioc_softc *sc, struct gpioc_cdevpriv *priv,
385     uint32_t pin)
386 {
387 	struct gpioc_pin_intr	*intr_conf = &sc->sc_pin_intr[pin];
388 	struct gpioc_privs	*priv_link;
389 	uint32_t		flags;
390 
391 	flags = intr_conf->pin->flags;
392 
393 	if (flags == 0)
394 		return (0);
395 
396 	mtx_lock(&intr_conf->mtx);
397 	SLIST_FOREACH(priv_link, &intr_conf->privs, next) {
398 		if (priv_link->priv == priv) {
399 			flags |= GPIO_INTR_ATTACHED;
400 			break;
401 		}
402 	}
403 	mtx_unlock(&intr_conf->mtx);
404 
405 	return (flags);
406 }
407 
408 static int
409 gpioc_set_intr_config(struct gpioc_softc *sc, struct gpioc_cdevpriv *priv,
410     uint32_t pin, uint32_t flags)
411 {
412 	struct gpioc_pin_intr *intr_conf = &sc->sc_pin_intr[pin];
413 	int res;
414 
415 	res = 0;
416 	if (intr_conf->pin->flags == 0 && flags == 0) {
417 		/* No interrupt configured and none requested: Do nothing. */
418 		return (0);
419 	}
420 	mtx_lock(&intr_conf->mtx);
421 	while (intr_conf->config_locked == true)
422 		mtx_sleep(&intr_conf->config_locked, &intr_conf->mtx, 0,
423 		    "gpicfg", 0);
424 	if (intr_conf->pin->flags == 0 && flags != 0) {
425 		/*
426 		 * No interrupt is configured, but one is requested: Allocate
427 		 * and setup interrupt on the according pin.
428 		 */
429 		res = gpioc_allocate_pin_intr(intr_conf, flags);
430 		if (res == 0)
431 			res = gpioc_attach_priv_pin(priv, intr_conf);
432 		if (res == EEXIST)
433 			res = 0;
434 	} else if (intr_conf->pin->flags == flags) {
435 		/*
436 		 * Same interrupt requested as already configured: Attach the
437 		 * cdevpriv to the corresponding pin.
438 		 */
439 		res = gpioc_attach_priv_pin(priv, intr_conf);
440 		if (res == EEXIST)
441 			res = 0;
442 	} else if (intr_conf->pin->flags != 0 && flags == 0) {
443 		/*
444 		 * Interrupt configured, but none requested: Teardown and
445 		 * release the pin when no other cdevpriv is attached. Otherwise
446 		 * just detach pin and cdevpriv from each other.
447 		 */
448 		if (gpioc_intr_reconfig_allowed(priv, intr_conf)) {
449 			res = gpioc_release_pin_intr(intr_conf);
450 		}
451 		if (res == 0)
452 			res = gpioc_detach_priv_pin(priv, intr_conf);
453 	} else {
454 		/*
455 		 * Other flag requested than configured: Reconfigure when no
456 		 * other cdevpriv is are attached to the pin.
457 		 */
458 		if (!gpioc_intr_reconfig_allowed(priv, intr_conf))
459 			res = EBUSY;
460 		else {
461 			res = gpioc_release_pin_intr(intr_conf);
462 			if (res == 0)
463 				res = gpioc_allocate_pin_intr(intr_conf, flags);
464 			if (res == 0)
465 				res = gpioc_attach_priv_pin(priv, intr_conf);
466 			if (res == EEXIST)
467 				res = 0;
468 		}
469 	}
470 	mtx_unlock(&intr_conf->mtx);
471 
472 	return (res);
473 }
474 
475 static void
476 gpioc_interrupt_handler(void *arg)
477 {
478 	struct gpioc_pin_intr *intr_conf;
479 	struct gpioc_privs *privs;
480 	struct gpioc_softc *sc;
481 	sbintime_t evtime;
482 	uint32_t pin_state;
483 
484 	intr_conf = arg;
485 	sc = intr_conf->sc;
486 
487 	/* Capture time and pin state first. */
488 	evtime = sbinuptime();
489 	if (intr_conf->pin->flags & GPIO_INTR_EDGE_BOTH)
490 		GPIO_PIN_GET(sc->sc_pdev, intr_conf->pin->pin, &pin_state);
491 	else if (intr_conf->pin->flags & GPIO_INTR_EDGE_RISING)
492 		pin_state = true;
493 	else
494 		pin_state = false;
495 
496 	mtx_lock(&intr_conf->mtx);
497 
498 	if (intr_conf->config_locked == true) {
499 		ddevice_printf(sc->sc_dev, "Interrupt configuration in "
500 		    "progress. Discarding interrupt on pin %d.\n",
501 		    intr_conf->pin->pin);
502 		mtx_unlock(&intr_conf->mtx);
503 		return;
504 	}
505 
506 	if (SLIST_EMPTY(&intr_conf->privs)) {
507 		ddevice_printf(sc->sc_dev, "No file descriptor associated with "
508 		    "occurred interrupt on pin %d.\n", intr_conf->pin->pin);
509 		mtx_unlock(&intr_conf->mtx);
510 		return;
511 	}
512 
513 	SLIST_FOREACH(privs, &intr_conf->privs, next) {
514 		struct gpioc_cdevpriv *priv = privs->priv;
515 		struct gpioc_pins *privpin;
516 		struct gpioc_pin_event *event;
517 		mtx_lock(&priv->mtx);
518 		SLIST_FOREACH(privpin, &priv->pins, next) {
519 			if (privpin->pin == intr_conf)
520 				break;
521 		}
522 		if (privpin == NULL) {
523 			/* Should be impossible. */
524 			ddevice_printf(sc->sc_dev, "Cannot find privpin\n");
525 			mtx_unlock(&priv->mtx);
526 			continue;
527 		}
528 
529 		if (priv->report_option == GPIO_EVENT_REPORT_DETAIL) {
530 			event = next_head_event(priv);
531 			/* If head is overtaking tail, advance tail. */
532 			if (priv->evidx_head == priv->evidx_tail)
533 				next_tail_event(priv);
534 		} else {
535 			if (privpin->eventcount > 0)
536 				event = &priv->events[privpin->firstevent + 1];
537 			else {
538 				privpin->firstevent = priv->evidx_head;
539 				event = next_head_event(priv);
540 				event->privpin = privpin;
541 				event->event_time = evtime;
542 				event->event_pin_state = pin_state;
543 				event = next_head_event(priv);
544 			}
545 			++privpin->eventcount;
546 		}
547 		event->privpin = privpin;
548 		event->event_time = evtime;
549 		event->event_pin_state = pin_state;
550 		wakeup(priv);
551 		selwakeup(&priv->selinfo);
552 		KNOTE_LOCKED(&priv->selinfo.si_note, 0);
553 		if (priv->async == true && priv->sigio != NULL)
554 			pgsigio(&priv->sigio, SIGIO, 0);
555 		mtx_unlock(&priv->mtx);
556 	}
557 
558 	mtx_unlock(&intr_conf->mtx);
559 }
560 
561 static int
562 gpioc_probe(device_t dev)
563 {
564 	device_set_desc(dev, "GPIO controller");
565 	return (0);
566 }
567 
568 static int
569 gpioc_attach(device_t dev)
570 {
571 	int err;
572 	struct gpioc_softc *sc;
573 	struct make_dev_args devargs;
574 
575 	sc = device_get_softc(dev);
576 	sc->sc_dev = dev;
577 	sc->sc_pdev = device_get_parent(dev);
578 	sc->sc_unit = device_get_unit(dev);
579 
580 	err = GPIO_PIN_MAX(sc->sc_pdev, &sc->sc_npins);
581 	sc->sc_npins++; /* Number of pins is one more than max pin number. */
582 	if (err != 0)
583 		return (err);
584 	sc->sc_pin_intr = malloc(sizeof(struct gpioc_pin_intr) * sc->sc_npins,
585 	    M_GPIOC, M_WAITOK | M_ZERO);
586 	for (int i = 0; i < sc->sc_npins; i++) {
587 		sc->sc_pin_intr[i].pin = malloc(sizeof(struct gpiobus_pin),
588 		    M_GPIOC, M_WAITOK | M_ZERO);
589 		sc->sc_pin_intr[i].sc = sc;
590 		sc->sc_pin_intr[i].pin->pin = i;
591 		sc->sc_pin_intr[i].pin->dev = sc->sc_pdev;
592 		mtx_init(&sc->sc_pin_intr[i].mtx, "gpioc pin", NULL, MTX_DEF);
593 		SLIST_INIT(&sc->sc_pin_intr[i].privs);
594 	}
595 
596 	make_dev_args_init(&devargs);
597 	devargs.mda_devsw = &gpioc_cdevsw;
598 	devargs.mda_uid = UID_ROOT;
599 	devargs.mda_gid = GID_WHEEL;
600 	devargs.mda_mode = 0600;
601 	devargs.mda_si_drv1 = sc;
602 	err = make_dev_s(&devargs, &sc->sc_ctl_dev, "gpioc%d", sc->sc_unit);
603 	if (err != 0) {
604 		device_printf(dev, "Failed to create gpioc%d", sc->sc_unit);
605 		return (ENXIO);
606 	}
607 
608 	return (0);
609 }
610 
611 static int
612 gpioc_detach(device_t dev)
613 {
614 	struct gpioc_softc *sc = device_get_softc(dev);
615 	int err;
616 
617 	if (sc->sc_ctl_dev)
618 		destroy_dev(sc->sc_ctl_dev);
619 
620 	for (int i = 0; i < sc->sc_npins; i++) {
621 		mtx_destroy(&sc->sc_pin_intr[i].mtx);
622 		free(sc->sc_pin_intr[i].pin, M_GPIOC);
623 	}
624 	free(sc->sc_pin_intr, M_GPIOC);
625 
626 	if ((err = bus_generic_detach(dev)) != 0)
627 		return (err);
628 
629 	return (0);
630 }
631 
632 static void
633 gpioc_cdevpriv_dtor(void *data)
634 {
635 	struct gpioc_cdevpriv	*priv;
636 	struct gpioc_privs	*priv_link, *priv_link_temp;
637 	struct gpioc_pins	*pin_link, *pin_link_temp;
638 	unsigned int		consistency __diagused;
639 
640 	priv = data;
641 
642 	SLIST_FOREACH_SAFE(pin_link, &priv->pins, next, pin_link_temp) {
643 		consistency = 0;
644 		mtx_lock(&pin_link->pin->mtx);
645 		while (pin_link->pin->config_locked == true)
646 			mtx_sleep(&pin_link->pin->config_locked,
647 			    &pin_link->pin->mtx, 0, "gpicfg", 0);
648 		SLIST_FOREACH_SAFE(priv_link, &pin_link->pin->privs, next,
649 		    priv_link_temp) {
650 			if (priv_link->priv == priv) {
651 				SLIST_REMOVE(&pin_link->pin->privs, priv_link,
652 				    gpioc_privs, next);
653 				free(priv_link, M_GPIOC);
654 				consistency++;
655 			}
656 		}
657 		KASSERT(consistency == 1,
658 		    ("inconsistent links between pin config and cdevpriv"));
659 		if (gpioc_intr_reconfig_allowed(priv, pin_link->pin)) {
660 			gpioc_release_pin_intr(pin_link->pin);
661 		}
662 		mtx_unlock(&pin_link->pin->mtx);
663 		SLIST_REMOVE(&priv->pins, pin_link, gpioc_pins, next);
664 		free(pin_link, M_GPIOC);
665 	}
666 
667 	wakeup(&priv);
668 	knlist_clear(&priv->selinfo.si_note, 0);
669 	seldrain(&priv->selinfo);
670 	knlist_destroy(&priv->selinfo.si_note);
671 	funsetown(&priv->sigio);
672 
673 	mtx_destroy(&priv->mtx);
674 	free(priv->events, M_GPIOC);
675 	free(data, M_GPIOC);
676 }
677 
678 static int
679 gpioc_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
680 {
681 	struct gpioc_cdevpriv *priv;
682 	int err;
683 
684 	priv = malloc(sizeof(*priv), M_GPIOC, M_WAITOK | M_ZERO);
685 	priv->sc = dev->si_drv1;
686 	priv->report_option = GPIO_EVENT_REPORT_DETAIL;
687 	err = devfs_set_cdevpriv(priv, gpioc_cdevpriv_dtor);
688 	if (err != 0) {
689 		gpioc_cdevpriv_dtor(priv);
690 		return (err);
691 	}
692 	mtx_init(&priv->mtx, "gpioc priv", NULL, MTX_DEF);
693 	knlist_init_mtx(&priv->selinfo.si_note, &priv->mtx);
694 
695 	/*
696 	 * Allocate a circular buffer for events.  The scheme we use for summary
697 	 * reporting assumes there will always be a pair of events available to
698 	 * record the first/last events on any pin, so we allocate 2 * npins.
699 	 * Even though we actually default to detailed event reporting, 2 *
700 	 * npins isn't a horrible fifo size for that either.
701 	 */
702 	priv->numevents = priv->sc->sc_npins * 2;
703 	priv->events = malloc(priv->numevents * sizeof(struct gpio_event_detail),
704 	    M_GPIOC, M_WAITOK | M_ZERO);
705 
706 	return (0);
707 }
708 
709 static int
710 gpioc_read(struct cdev *dev, struct uio *uio, int ioflag)
711 {
712 	struct gpioc_cdevpriv *priv;
713 	struct gpioc_pin_event *event;
714 	union {
715 		struct gpio_event_summary sum;
716 		struct gpio_event_detail  evt;
717 		uint8_t 		  data[1];
718 	} recbuf;
719 	size_t recsize;
720 	int err;
721 
722 	if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
723 		return (err);
724 
725 	if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY)
726 		recsize = sizeof(struct gpio_event_summary);
727 	else
728 		recsize = sizeof(struct gpio_event_detail);
729 
730 	if (uio->uio_resid < recsize)
731 		return (EINVAL);
732 
733 	mtx_lock(&priv->mtx);
734 	while (priv->evidx_head == priv->evidx_tail) {
735 		if (SLIST_EMPTY(&priv->pins)) {
736 			err = ENXIO;
737 			break;
738 		} else if (ioflag & O_NONBLOCK) {
739 			err = EWOULDBLOCK;
740 			break;
741 		} else {
742 			err = mtx_sleep(priv, &priv->mtx, PCATCH, "gpintr", 0);
743 			if (err != 0)
744 				break;
745 		}
746 	}
747 
748 	while (err == 0 && uio->uio_resid >= recsize &&
749            priv->evidx_tail != priv->evidx_head) {
750 		event = next_tail_event(priv);
751 		if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY) {
752 			recbuf.sum.gp_first_time = event->event_time;
753 			recbuf.sum.gp_pin = event->privpin->pin->pin->pin;
754 			recbuf.sum.gp_count = event->privpin->eventcount;
755 			recbuf.sum.gp_first_state = event->event_pin_state;
756 			event = next_tail_event(priv);
757 			recbuf.sum.gp_last_time = event->event_time;
758 			recbuf.sum.gp_last_state = event->event_pin_state;
759 			event->privpin->eventcount = 0;
760 			event->privpin->firstevent = 0;
761 		} else {
762 			recbuf.evt.gp_time = event->event_time;
763 			recbuf.evt.gp_pin = event->privpin->pin->pin->pin;
764 			recbuf.evt.gp_pinstate = event->event_pin_state;
765 		}
766 		mtx_unlock(&priv->mtx);
767 		err = uiomove(recbuf.data, recsize, uio);
768 		mtx_lock(&priv->mtx);
769 	}
770 	mtx_unlock(&priv->mtx);
771 	return (err);
772 }
773 
774 static int
775 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
776     struct thread *td)
777 {
778 	device_t bus;
779 	int max_pin, res;
780 	struct gpioc_softc *sc = cdev->si_drv1;
781 	struct gpioc_cdevpriv *priv;
782 	struct gpio_pin pin;
783 	struct gpio_req req;
784 	struct gpio_access_32 *a32;
785 	struct gpio_config_32 *c32;
786 	struct gpio_event_config *evcfg;
787 	uint32_t caps, intrflags;
788 
789 	bus = GPIO_GET_BUS(sc->sc_pdev);
790 	if (bus == NULL)
791 		return (EINVAL);
792 	switch (cmd) {
793 	case GPIOMAXPIN:
794 		max_pin = -1;
795 		res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
796 		bcopy(&max_pin, arg, sizeof(max_pin));
797 		break;
798 	case GPIOGETCONFIG:
799 		bcopy(arg, &pin, sizeof(pin));
800 		dprintf("get config pin %d\n", pin.gp_pin);
801 		res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
802 		    &pin.gp_flags);
803 		/* Fail early */
804 		if (res)
805 			break;
806 		res = devfs_get_cdevpriv((void **)&priv);
807 		if (res)
808 			break;
809 		pin.gp_flags |= gpioc_get_intr_config(sc, priv,
810 		    pin.gp_pin);
811 		GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
812 		GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name);
813 		bcopy(&pin, arg, sizeof(pin));
814 		break;
815 	case GPIOSETCONFIG:
816 		bcopy(arg, &pin, sizeof(pin));
817 		dprintf("set config pin %d\n", pin.gp_pin);
818 		res = devfs_get_cdevpriv((void **)&priv);
819 		if (res != 0)
820 			break;
821 		res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps);
822 		if (res != 0)
823 			break;
824 		res = gpio_check_flags(caps, pin.gp_flags);
825 		if (res != 0)
826 			break;
827 		intrflags = pin.gp_flags & GPIO_INTR_MASK;
828 		/*
829 		 * We can do only edge interrupts, and only if the
830 		 * hardware supports that interrupt type on that pin.
831 		 */
832 		switch (intrflags) {
833 		case GPIO_INTR_NONE:
834 			break;
835 		case GPIO_INTR_EDGE_RISING:
836 		case GPIO_INTR_EDGE_FALLING:
837 		case GPIO_INTR_EDGE_BOTH:
838 			if ((intrflags & caps) == 0)
839 				res = EOPNOTSUPP;
840 			break;
841 		default:
842 			res = EINVAL;
843 			break;
844 		}
845 		if (res != 0)
846 			break;
847 		res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
848 		    (pin.gp_flags & ~GPIO_INTR_MASK));
849 		if (res != 0)
850 			break;
851 		res = gpioc_set_intr_config(sc, priv, pin.gp_pin,
852 		    intrflags);
853 		break;
854 	case GPIOGET:
855 		bcopy(arg, &req, sizeof(req));
856 		res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
857 		    &req.gp_value);
858 		dprintf("read pin %d -> %d\n",
859 		    req.gp_pin, req.gp_value);
860 		bcopy(&req, arg, sizeof(req));
861 		break;
862 	case GPIOSET:
863 		bcopy(arg, &req, sizeof(req));
864 		res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
865 		    req.gp_value);
866 		dprintf("write pin %d -> %d\n",
867 		    req.gp_pin, req.gp_value);
868 		break;
869 	case GPIOTOGGLE:
870 		bcopy(arg, &req, sizeof(req));
871 		dprintf("toggle pin %d\n",
872 		    req.gp_pin);
873 		res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
874 		break;
875 	case GPIOSETNAME:
876 		bcopy(arg, &pin, sizeof(pin));
877 		dprintf("set name on pin %d\n", pin.gp_pin);
878 		res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin,
879 		    pin.gp_name);
880 		break;
881 	case GPIOACCESS32:
882 		a32 = (struct gpio_access_32 *)arg;
883 		res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin,
884 		    a32->clear_pins, a32->change_pins, &a32->orig_pins);
885 		break;
886 	case GPIOCONFIG32:
887 		c32 = (struct gpio_config_32 *)arg;
888 		res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin,
889 		    c32->num_pins, c32->pin_flags);
890 		break;
891 	case GPIOCONFIGEVENTS:
892 		evcfg = (struct gpio_event_config *)arg;
893 		res = devfs_get_cdevpriv((void **)&priv);
894 		if (res != 0)
895 			break;
896 		/* If any pins have been configured, changes aren't allowed. */
897 		if (!SLIST_EMPTY(&priv->pins)) {
898 			res = EINVAL;
899 			break;
900 		}
901 		if (evcfg->gp_report_type != GPIO_EVENT_REPORT_DETAIL &&
902 		    evcfg->gp_report_type != GPIO_EVENT_REPORT_SUMMARY) {
903 			res = EINVAL;
904 			break;
905 		}
906 		priv->report_option = evcfg->gp_report_type;
907 		/* Reallocate the events buffer if the user wants it bigger. */
908 		if (priv->report_option == GPIO_EVENT_REPORT_DETAIL &&
909 		    priv->numevents < evcfg->gp_fifo_size) {
910 			free(priv->events, M_GPIOC);
911 			priv->numevents = evcfg->gp_fifo_size;
912 			priv->events = malloc(priv->numevents *
913 			    sizeof(struct gpio_event_detail), M_GPIOC,
914 			    M_WAITOK | M_ZERO);
915 			priv->evidx_head = priv->evidx_tail = 0;
916 		}
917 		break;
918 	case FIONBIO:
919 		/*
920 		 * This dummy handler is necessary to prevent fcntl()
921 		 * from failing. The actual handling of non-blocking IO
922 		 * is done using the O_NONBLOCK ioflag passed to the
923 		 * read() syscall.
924 		 */
925 		res = 0;
926 		break;
927 	case FIOASYNC:
928 		res = devfs_get_cdevpriv((void **)&priv);
929 		if (res == 0) {
930 			if (*(int *)arg == FASYNC)
931 				priv->async = true;
932 			else
933 				priv->async = false;
934 		}
935 		break;
936 	case FIOGETOWN:
937 		res = devfs_get_cdevpriv((void **)&priv);
938 		if (res == 0)
939 			*(int *)arg = fgetown(&priv->sigio);
940 		break;
941 	case FIOSETOWN:
942 		res = devfs_get_cdevpriv((void **)&priv);
943 		if (res == 0)
944 			res = fsetown(*(int *)arg, &priv->sigio);
945 		break;
946 	default:
947 		return (ENOTTY);
948 		break;
949 	}
950 
951 	return (res);
952 }
953 
954 static int
955 gpioc_poll(struct cdev *dev, int events, struct thread *td)
956 {
957 	struct gpioc_cdevpriv *priv;
958 	int err;
959 	int revents;
960 
961 	revents = 0;
962 
963 	err = devfs_get_cdevpriv((void **)&priv);
964 	if (err != 0) {
965 		revents = POLLERR;
966 		return (revents);
967 	}
968 
969 	if (SLIST_EMPTY(&priv->pins)) {
970 		revents = POLLHUP;
971 		return (revents);
972 	}
973 
974 	if (events & (POLLIN | POLLRDNORM)) {
975 		if (priv->evidx_head != priv->evidx_tail)
976 			revents |= events & (POLLIN | POLLRDNORM);
977 		else
978 			selrecord(td, &priv->selinfo);
979 	}
980 
981 	return (revents);
982 }
983 
984 static int
985 gpioc_kqfilter(struct cdev *dev, struct knote *kn)
986 {
987 	struct gpioc_cdevpriv *priv;
988 	struct knlist *knlist;
989 	int err;
990 
991 	err = devfs_get_cdevpriv((void **)&priv);
992 	if (err != 0)
993 		return err;
994 
995 	if (SLIST_EMPTY(&priv->pins))
996 		return (ENXIO);
997 
998 	switch(kn->kn_filter) {
999 	case EVFILT_READ:
1000 		kn->kn_fop = &gpioc_read_filterops;
1001 		kn->kn_hook = (void *)priv;
1002 		break;
1003 	default:
1004 		return (EOPNOTSUPP);
1005 	}
1006 
1007 	knlist = &priv->selinfo.si_note;
1008 	knlist_add(knlist, kn, 0);
1009 
1010 	return (0);
1011 }
1012 
1013 static int
1014 gpioc_kqread(struct knote *kn, long hint)
1015 {
1016 	struct gpioc_cdevpriv *priv = kn->kn_hook;
1017 	size_t recsize;
1018 
1019 
1020 	if (SLIST_EMPTY(&priv->pins)) {
1021 		kn->kn_flags |= EV_EOF;
1022 		return (1);
1023 	} else {
1024 		if (priv->evidx_head != priv->evidx_tail) {
1025 			if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY)
1026 				recsize = sizeof(struct gpio_event_summary);
1027 			else
1028 				recsize = sizeof(struct gpio_event_detail);
1029 			kn->kn_data = recsize * number_of_events(priv);
1030 			return (1);
1031 		}
1032 	}
1033 	return (0);
1034 }
1035 
1036 static void
1037 gpioc_kqdetach(struct knote *kn)
1038 {
1039 	struct gpioc_cdevpriv *priv = kn->kn_hook;
1040 	struct knlist *knlist = &priv->selinfo.si_note;
1041 
1042 	knlist_remove(knlist, kn, 0);
1043 }
1044 
1045 static device_method_t gpioc_methods[] = {
1046 	/* Device interface */
1047 	DEVMETHOD(device_probe,		gpioc_probe),
1048 	DEVMETHOD(device_attach,	gpioc_attach),
1049 	DEVMETHOD(device_detach,	gpioc_detach),
1050 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1051 	DEVMETHOD(device_suspend,	bus_generic_suspend),
1052 	DEVMETHOD(device_resume,	bus_generic_resume),
1053 
1054 	DEVMETHOD_END
1055 };
1056 
1057 driver_t gpioc_driver = {
1058 	"gpioc",
1059 	gpioc_methods,
1060 	sizeof(struct gpioc_softc)
1061 };
1062 
1063 DRIVER_MODULE(gpioc, gpio, gpioc_driver, 0, 0);
1064 MODULE_VERSION(gpioc, 1);
1065