xref: /netbsd-src/sys/dev/gpio/gpio.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /* $NetBSD: gpio.c,v 1.56 2014/06/29 09:32:24 kardel Exp $ */
2 /*	$OpenBSD: gpio.c,v 1.6 2006/01/14 12:33:49 grange Exp $	*/
3 
4 /*
5  * Copyright (c) 2008, 2009, 2010, 2011 Marc Balmer <marc@msys.ch>
6  * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: gpio.c,v 1.56 2014/06/29 09:32:24 kardel Exp $");
23 
24 /*
25  * General Purpose Input/Output framework.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/callout.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/device.h>
33 #include <sys/fcntl.h>
34 #include <sys/ioctl.h>
35 #include <sys/gpio.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/kmem.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/queue.h>
42 #include <sys/kauth.h>
43 #include <sys/module.h>
44 #include <dev/gpio/gpiovar.h>
45 
46 #include "locators.h"
47 
48 #ifdef GPIO_DEBUG
49 #define DPRINTFN(n, x)	do { if (gpiodebug > (n)) printf x; } while (0)
50 int gpiodebug = 0;
51 #else
52 #define DPRINTFN(n, x)
53 #endif
54 #define DPRINTF(x)	DPRINTFN(0, x)
55 
56 struct gpio_softc {
57 	device_t		 sc_dev;
58 
59 	gpio_chipset_tag_t	 sc_gc;		/* GPIO controller */
60 	gpio_pin_t		*sc_pins;	/* pins array */
61 	int			 sc_npins;	/* number of pins */
62 
63 	kmutex_t		 sc_mtx;
64 	kcondvar_t		 sc_ioctl;	/* ioctl in progress */
65 	int			 sc_ioctl_busy;	/* ioctl is busy */
66 	kcondvar_t		 sc_attach;	/* attach/detach in progress */
67 	int			 sc_attach_busy;/* busy in attach/detach */
68 #ifdef COMPAT_50
69 	LIST_HEAD(, gpio_dev)	 sc_devs;	/* devices */
70 #endif
71 	LIST_HEAD(, gpio_name)	 sc_names;	/* named pins */
72 };
73 
74 static int	gpio_match(device_t, cfdata_t, void *);
75 int		gpio_submatch(device_t, cfdata_t, const int *, void *);
76 static void	gpio_attach(device_t, device_t, void *);
77 static int	gpio_rescan(device_t, const char *, const int *);
78 static void	gpio_childdetached(device_t, device_t);
79 static bool	gpio_resume(device_t, const pmf_qual_t *);
80 static int	gpio_detach(device_t, int);
81 static int	gpio_search(device_t, cfdata_t, const int *, void *);
82 static int	gpio_print(void *, const char *);
83 static int	gpio_pinbyname(struct gpio_softc *, char *);
84 static int	gpio_ioctl(struct gpio_softc *, u_long, void *, int,
85     struct lwp *);
86 
87 #ifdef COMPAT_50
88 /* Old API */
89 static int	gpio_ioctl_oapi(struct gpio_softc *, u_long, void *, int,
90     kauth_cred_t);
91 #endif
92 
93 CFATTACH_DECL3_NEW(gpio, sizeof(struct gpio_softc),
94     gpio_match, gpio_attach, gpio_detach, NULL, gpio_rescan,
95     gpio_childdetached, DVF_DETACH_SHUTDOWN);
96 
97 dev_type_open(gpioopen);
98 dev_type_close(gpioclose);
99 dev_type_ioctl(gpioioctl);
100 dev_type_ioctl(gpioioctl_locked);
101 
102 const struct cdevsw gpio_cdevsw = {
103 	.d_open = gpioopen,
104 	.d_close = gpioclose,
105 	.d_read = noread,
106 	.d_write = nowrite,
107 	.d_ioctl = gpioioctl,
108 	.d_stop = nostop,
109 	.d_tty = notty,
110 	.d_poll = nopoll,
111 	.d_mmap = nommap,
112 	.d_kqfilter = nokqfilter,
113 	.d_flag = D_OTHER | D_MPSAFE
114 };
115 
116 extern struct cfdriver gpio_cd;
117 
118 static int
119 gpio_match(device_t parent, cfdata_t cf, void *aux)
120 {
121 	return 1;
122 }
123 
124 int
125 gpio_submatch(device_t parent, cfdata_t cf, const int *ip, void *aux)
126 {
127 	struct gpio_attach_args *ga = aux;
128 
129 	if (ga->ga_offset == -1)
130 		return 0;
131 
132 	return strcmp(ga->ga_dvname, cf->cf_name) == 0;
133 }
134 
135 static bool
136 gpio_resume(device_t self, const pmf_qual_t *qual)
137 {
138 	struct gpio_softc *sc = device_private(self);
139 	int pin;
140 
141 	for (pin = 0; pin < sc->sc_npins; pin++) {
142 		gpiobus_pin_ctl(sc->sc_gc, pin, sc->sc_pins[pin].pin_flags);
143 		gpiobus_pin_write(sc->sc_gc, pin, sc->sc_pins[pin].pin_state);
144 	}
145 	return true;
146 }
147 
148 static void
149 gpio_childdetached(device_t self, device_t child)
150 {
151 #ifdef COMPAT_50
152 	struct gpio_dev *gdev;
153 	struct gpio_softc *sc;
154 	int error;
155 
156 	/*
157 	 * gpio_childetached is serialized because it can be entered in
158 	 * different ways concurrently, e.g. via the GPIODETACH ioctl and
159 	 * drvctl(8) or modunload(8).
160 	 */
161 	sc = device_private(self);
162 	error = 0;
163 	mutex_enter(&sc->sc_mtx);
164 	while (sc->sc_attach_busy) {
165 		error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
166 		if (error)
167 			break;
168 	}
169 	if (!error)
170 		sc->sc_attach_busy = 1;
171 	mutex_exit(&sc->sc_mtx);
172 	if (error)
173 		return;
174 
175 	LIST_FOREACH(gdev, &sc->sc_devs, sc_next)
176 		if (gdev->sc_dev == child) {
177 			LIST_REMOVE(gdev, sc_next);
178 			kmem_free(gdev, sizeof(struct gpio_dev));
179 			break;
180 		}
181 
182 	mutex_enter(&sc->sc_mtx);
183 	sc->sc_attach_busy = 0;
184 	cv_signal(&sc->sc_attach);
185 	mutex_exit(&sc->sc_mtx);
186 #endif
187 }
188 
189 static int
190 gpio_rescan(device_t self, const char *ifattr, const int *locators)
191 {
192 	struct gpio_softc *sc = device_private(self);
193 
194 	config_search_loc(gpio_search, self, ifattr, locators, sc);
195 
196 	return 0;
197 }
198 
199 static void
200 gpio_attach(device_t parent, device_t self, void *aux)
201 {
202 	struct gpio_softc *sc = device_private(self);
203 	struct gpiobus_attach_args *gba = aux;
204 
205 	sc->sc_dev = self;
206 	sc->sc_gc = gba->gba_gc;
207 	sc->sc_pins = gba->gba_pins;
208 	sc->sc_npins = gba->gba_npins;
209 
210 	aprint_normal(": %d pins\n", sc->sc_npins);
211 	aprint_naive("\n");
212 
213 	if (!pmf_device_register(self, NULL, gpio_resume))
214 		aprint_error_dev(self, "couldn't establish power handler\n");
215 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
216 	cv_init(&sc->sc_ioctl, "gpioctl");
217 	cv_init(&sc->sc_attach, "gpioatch");
218 	/*
219 	 * Attach all devices that can be connected to the GPIO pins
220 	 * described in the kernel configuration file.
221 	 */
222 	gpio_rescan(self, "gpio", NULL);
223 }
224 
225 static int
226 gpio_detach(device_t self, int flags)
227 {
228 	struct gpio_softc *sc;
229 	int rc;
230 
231 	sc = device_private(self);
232 
233 	if ((rc = config_detach_children(self, flags)) != 0)
234 		return rc;
235 	mutex_destroy(&sc->sc_mtx);
236 	cv_destroy(&sc->sc_ioctl);
237 #if 0
238 	int maj, mn;
239 
240 	/* Locate the major number */
241 	for (maj = 0; maj < nchrdev; maj++)
242 		if (cdevsw[maj].d_open == gpioopen)
243 			break;
244 
245 	/* Nuke the vnodes for any open instances (calls close) */
246 	mn = device_unit(self);
247 	vdevgone(maj, mn, mn, VCHR);
248 #endif
249 	return 0;
250 }
251 
252 static int
253 gpio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
254 {
255 	struct gpio_attach_args ga;
256 	size_t namlen;
257 
258 	ga.ga_gpio = aux;
259 	ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
260 	ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
261 	ga.ga_flags = cf->cf_loc[GPIOCF_FLAG];
262 	namlen = strlen(cf->cf_name) + 1;
263 	ga.ga_dvname = kmem_alloc(namlen, KM_NOSLEEP);
264 	if (ga.ga_dvname == NULL)
265 		return 0;
266 	strcpy(ga.ga_dvname, cf->cf_name);
267 
268 	if (config_match(parent, cf, &ga) > 0)
269 		config_attach(parent, cf, &ga, gpio_print);
270 	kmem_free(ga.ga_dvname, namlen);
271 	return 0;
272 }
273 
274 int
275 gpio_print(void *aux, const char *pnp)
276 {
277 	struct gpio_attach_args *ga = aux;
278 	int i;
279 
280 	aprint_normal(" pins");
281 	for (i = 0; i < 32; i++)
282 		if (ga->ga_mask & (1 << i))
283 			aprint_normal(" %d", ga->ga_offset + i);
284 
285 	return UNCONF;
286 }
287 
288 int
289 gpiobus_print(void *aux, const char *pnp)
290 {
291 #if 0
292 	struct gpiobus_attach_args *gba = aux;
293 #endif
294 	if (pnp != NULL)
295 		aprint_normal("gpiobus at %s", pnp);
296 
297 	return UNCONF;
298 }
299 
300 /* return 1 if all pins can be mapped, 0 if not */
301 int
302 gpio_pin_can_map(void *gpio, int offset, uint32_t mask)
303 {
304 	struct gpio_softc *sc = gpio;
305 	int npins, pin, i;
306 
307 	npins = gpio_npins(mask);
308 	if (npins > sc->sc_npins)
309 		return 0;
310 
311 	for (npins = 0, i = 0; i < 32; i++)
312 		if (mask & (1 << i)) {
313 			pin = offset + i;
314 			if (pin < 0 || pin >= sc->sc_npins)
315 				return 0;
316 			if (sc->sc_pins[pin].pin_mapped)
317 				return 0;
318 		}
319 
320 	return 1;
321 }
322 
323 int
324 gpio_pin_map(void *gpio, int offset, uint32_t mask, struct gpio_pinmap *map)
325 {
326 	struct gpio_softc *sc = gpio;
327 	int npins, pin, i;
328 
329 	npins = gpio_npins(mask);
330 	if (npins > sc->sc_npins)
331 		return 1;
332 
333 	for (npins = 0, i = 0; i < 32; i++)
334 		if (mask & (1 << i)) {
335 			pin = offset + i;
336 			if (pin < 0 || pin >= sc->sc_npins)
337 				return 1;
338 			if (sc->sc_pins[pin].pin_mapped)
339 				return 1;
340 			sc->sc_pins[pin].pin_mapped = 1;
341 			map->pm_map[npins++] = pin;
342 		}
343 	map->pm_size = npins;
344 
345 	return 0;
346 }
347 
348 void
349 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
350 {
351 	struct gpio_softc *sc = gpio;
352 	int pin, i;
353 
354 	for (i = 0; i < map->pm_size; i++) {
355 		pin = map->pm_map[i];
356 		sc->sc_pins[pin].pin_mapped = 0;
357 	}
358 }
359 
360 int
361 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
362 {
363 	struct gpio_softc *sc = gpio;
364 
365 	return gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]);
366 }
367 
368 void
369 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
370 {
371 	struct gpio_softc *sc = gpio;
372 
373 	gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value);
374 	sc->sc_pins[map->pm_map[pin]].pin_state = value;
375 }
376 
377 void
378 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
379 {
380 	struct gpio_softc *sc = gpio;
381 
382 	return gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
383 }
384 
385 int
386 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
387 {
388 	struct gpio_softc *sc = gpio;
389 
390 	return sc->sc_pins[map->pm_map[pin]].pin_caps;
391 }
392 
393 int
394 gpio_npins(uint32_t mask)
395 {
396 	int npins, i;
397 
398 	for (npins = 0, i = 0; i < 32; i++)
399 		if (mask & (1 << i))
400 			npins++;
401 
402 	return npins;
403 }
404 
405 int
406 gpio_lock(void *data)
407 {
408 	struct gpio_softc *sc;
409 	int error;
410 
411 	error = 0;
412 	sc = data;
413 	mutex_enter(&sc->sc_mtx);
414 	while (sc->sc_ioctl_busy) {
415 		error = cv_wait_sig(&sc->sc_ioctl, &sc->sc_mtx);
416 		if (error)
417 			break;
418 	}
419 	if (!error)
420 		sc->sc_ioctl_busy = 1;
421 	mutex_exit(&sc->sc_mtx);
422 	return error;
423 }
424 
425 void
426 gpio_unlock(void *data)
427 {
428 	struct gpio_softc *sc;
429 
430 	sc = data;
431 	mutex_enter(&sc->sc_mtx);
432 	sc->sc_ioctl_busy = 0;
433 	cv_signal(&sc->sc_ioctl);
434 	mutex_exit(&sc->sc_mtx);
435 }
436 
437 int
438 gpioopen(dev_t dev, int flag, int mode, struct lwp *l)
439 {
440 	struct gpio_softc *sc;
441 
442 	sc = device_lookup_private(&gpio_cd, minor(dev));
443 	if (sc == NULL)
444 		return ENXIO;
445 
446 	return gpiobus_open(sc->sc_gc, sc->sc_dev);
447 }
448 
449 int
450 gpioclose(dev_t dev, int flag, int mode, struct lwp *l)
451 {
452 	struct gpio_softc *sc;
453 
454 	sc = device_lookup_private(&gpio_cd, minor(dev));
455 	return gpiobus_close(sc->sc_gc, sc->sc_dev);
456 }
457 
458 static int
459 gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
460 {
461         struct gpio_name *nm;
462 
463         LIST_FOREACH(nm, &sc->sc_names, gp_next)
464                 if (!strcmp(nm->gp_name, gp_name))
465                         return nm->gp_pin;
466         return -1;
467 }
468 
469 int
470 gpioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
471 {
472 	int error;
473 	struct gpio_softc *sc;
474 
475 	sc = device_lookup_private(&gpio_cd, minor(dev));
476 
477 	error = gpio_lock(sc);
478 	if (error)
479 		return error;
480 
481 	error = gpio_ioctl(sc, cmd, data, flag, l);
482 	gpio_unlock(sc);
483 	return error;
484 }
485 
486 static int
487 gpio_ioctl(struct gpio_softc *sc, u_long cmd, void *data, int flag,
488     struct lwp *l)
489 {
490 	gpio_chipset_tag_t gc;
491 	struct gpio_info *info;
492 	struct gpio_attach *attach;
493 	struct gpio_attach_args ga;
494 	struct gpio_req *req;
495 	struct gpio_name *nm;
496 	struct gpio_set *set;
497 #ifdef COMPAT_50
498 	struct gpio_dev *gdev;
499 #endif
500 	device_t dv;
501 	cfdata_t cf;
502 	kauth_cred_t cred;
503 	int locs[GPIOCF_NLOCS];
504 	int error, pin, value, flags, npins;
505 
506 	gc = sc->sc_gc;
507 	ga.ga_flags = 0;
508 
509 	if (cmd != GPIOINFO && !device_is_active(sc->sc_dev)) {
510 		DPRINTF(("%s: device is not active\n",
511 		    device_xname(sc->sc_dev)));
512 		return EBUSY;
513 	}
514 
515 	cred = kauth_cred_get();
516 
517 	switch (cmd) {
518 	case GPIOINFO:
519 		info = data;
520 		if (!kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
521 		    NULL, NULL, NULL, NULL))
522 			info->gpio_npins = sc->sc_npins;
523 		else {
524 			for (pin = npins = 0; pin < sc->sc_npins; pin++)
525 				if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
526 					++npins;
527 			info->gpio_npins = npins;
528 		}
529 		break;
530 	case GPIOREAD:
531 		req = data;
532 
533 		if (req->gp_name[0] != '\0')
534 			pin = gpio_pinbyname(sc, req->gp_name);
535 		else
536 			pin = req->gp_pin;
537 
538 		if (pin < 0 || pin >= sc->sc_npins)
539 			return EINVAL;
540 
541 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
542 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
543 		    NULL, NULL, NULL, NULL))
544 			return EPERM;
545 
546 		/* return read value */
547 		req->gp_value = gpiobus_pin_read(gc, pin);
548 		break;
549 	case GPIOWRITE:
550 		if ((flag & FWRITE) == 0)
551 			return EBADF;
552 
553 		req = data;
554 
555 		if (req->gp_name[0] != '\0')
556 			pin = gpio_pinbyname(sc, req->gp_name);
557 		else
558 			pin = req->gp_pin;
559 
560 		if (pin < 0 || pin >= sc->sc_npins)
561 			return EINVAL;
562 
563 		if (sc->sc_pins[pin].pin_mapped)
564 			return EBUSY;
565 
566 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
567 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
568 		    NULL, NULL, NULL, NULL))
569 			return EPERM;
570 
571 		value = req->gp_value;
572 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
573 			return EINVAL;
574 
575 		/* return old value */
576 		req->gp_value = gpiobus_pin_read(gc, pin);
577 		gpiobus_pin_write(gc, pin, value);
578 		/* update current value */
579 		sc->sc_pins[pin].pin_state = value;
580 		break;
581 	case GPIOTOGGLE:
582 		if ((flag & FWRITE) == 0)
583 			return EBADF;
584 
585 		req = data;
586 
587 		if (req->gp_name[0] != '\0')
588 			pin = gpio_pinbyname(sc, req->gp_name);
589 		else
590 			pin = req->gp_pin;
591 
592 		if (pin < 0 || pin >= sc->sc_npins)
593 			return EINVAL;
594 
595 		if (sc->sc_pins[pin].pin_mapped)
596 			return EBUSY;
597 
598 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
599 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
600 		    NULL, NULL, NULL, NULL))
601 			return EPERM;
602 
603 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
604 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
605 		gpiobus_pin_write(gc, pin, value);
606 		/* return old value */
607 		req->gp_value = sc->sc_pins[pin].pin_state;
608 		/* update current value */
609 		sc->sc_pins[pin].pin_state = value;
610 		break;
611 	case GPIOATTACH:
612 		attach = data;
613 		ga.ga_flags = attach->ga_flags;
614 #ifdef COMPAT_50
615 		/* FALLTHROUGH */
616 	case GPIOATTACH50:
617 		/*
618 		 * The double assignment to 'attach' in case of GPIOATTACH
619 		 * and COMPAT_50 is on purpose. It ensures backward
620 		 * compatability in case we are called through the old
621 		 * GPIOATTACH50 ioctl(2), which had not the ga_flags field
622 		 * in struct gpio_attach.
623 		 */
624 		attach = data;
625 #endif
626 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
627 		    NULL, NULL, NULL, NULL))
628 			return EPERM;
629 
630 		/* do not try to attach if the pins are already mapped */
631 		if (!gpio_pin_can_map(sc, attach->ga_offset, attach->ga_mask))
632 			return EBUSY;
633 
634 		error = 0;
635 		mutex_enter(&sc->sc_mtx);
636 		while (sc->sc_attach_busy) {
637 			error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
638 			if (error)
639 				break;
640 		}
641 		if (!error)
642 			sc->sc_attach_busy = 1;
643 		mutex_exit(&sc->sc_mtx);
644 		if (error)
645 			return EBUSY;
646 
647 		ga.ga_gpio = sc;
648 		/* Don't access attach->ga_flags here. */
649 		ga.ga_dvname = attach->ga_dvname;
650 		ga.ga_offset = attach->ga_offset;
651 		ga.ga_mask = attach->ga_mask;
652 		DPRINTF(("%s: attach %s with offset %d, mask "
653 		    "0x%02x, and flags 0x%02x\n", device_xname(sc->sc_dev),
654 		    ga.ga_dvname, ga.ga_offset, ga.ga_mask, ga.ga_flags));
655 
656 		locs[GPIOCF_OFFSET] = ga.ga_offset;
657 		locs[GPIOCF_MASK] = ga.ga_mask;
658 		locs[GPIOCF_FLAG] = ga.ga_flags;
659 
660 		cf = config_search_loc(NULL, sc->sc_dev, "gpio", locs, &ga);
661 		if (cf != NULL) {
662 			dv = config_attach_loc(sc->sc_dev, cf, locs, &ga,
663 			    gpiobus_print);
664 #ifdef COMPAT_50
665 			if (dv != NULL) {
666 				gdev = kmem_alloc(sizeof(struct gpio_dev),
667 				    KM_SLEEP);
668 				gdev->sc_dev = dv;
669 				LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
670 			} else
671 				error = EINVAL;
672 #else
673 			if (dv == NULL)
674 				error = EINVAL;
675 #endif
676 		} else
677 			error = EINVAL;
678 		mutex_enter(&sc->sc_mtx);
679 		sc->sc_attach_busy = 0;
680 		cv_signal(&sc->sc_attach);
681 		mutex_exit(&sc->sc_mtx);
682 		return error;
683 	case GPIOSET:
684 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
685 		    NULL, NULL, NULL, NULL))
686 			return EPERM;
687 
688 		set = data;
689 
690 		if (set->gp_name[0] != '\0')
691 			pin = gpio_pinbyname(sc, set->gp_name);
692 		else
693 			pin = set->gp_pin;
694 
695 		if (pin < 0 || pin >= sc->sc_npins)
696 			return EINVAL;
697 		flags = set->gp_flags;
698 
699 		/* check that the controller supports all requested flags */
700 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
701 			return ENODEV;
702 		flags = set->gp_flags;
703 
704 		set->gp_caps = sc->sc_pins[pin].pin_caps;
705 		/* return old value */
706 		set->gp_flags = sc->sc_pins[pin].pin_flags;
707 
708 		if (flags > 0) {
709 			flags |= GPIO_PIN_SET;
710 			gpiobus_pin_ctl(gc, pin, flags);
711 			/* update current value */
712 			sc->sc_pins[pin].pin_flags = flags;
713 		}
714 
715 		/* rename pin or new pin? */
716 		if (set->gp_name2[0] != '\0') {
717 			struct gpio_name *gnm;
718 
719 			gnm = NULL;
720 			LIST_FOREACH(nm, &sc->sc_names, gp_next) {
721 				if (!strcmp(nm->gp_name, set->gp_name2) &&
722 				    nm->gp_pin != pin)
723 					return EINVAL;	/* duplicate name */
724 				if (nm->gp_pin == pin)
725 					gnm = nm;
726 			}
727 			if (gnm != NULL)
728 				strlcpy(gnm->gp_name, set->gp_name2,
729 				    sizeof(gnm->gp_name));
730 			else  {
731 				nm = kmem_alloc(sizeof(struct gpio_name),
732 				    KM_SLEEP);
733 				strlcpy(nm->gp_name, set->gp_name2,
734 				    sizeof(nm->gp_name));
735 				nm->gp_pin = set->gp_pin;
736 				LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
737 			}
738 		}
739 		break;
740 	case GPIOUNSET:
741 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
742 		    NULL, NULL, NULL, NULL))
743 			return EPERM;
744 
745 		set = data;
746 		if (set->gp_name[0] != '\0')
747 			pin = gpio_pinbyname(sc, set->gp_name);
748 		else
749 			pin = set->gp_pin;
750 
751 		if (pin < 0 || pin >= sc->sc_npins)
752 			return EINVAL;
753 		if (sc->sc_pins[pin].pin_mapped)
754 			return EBUSY;
755 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
756 			return EINVAL;
757 
758 		LIST_FOREACH(nm, &sc->sc_names, gp_next) {
759 			if (nm->gp_pin == pin) {
760 				LIST_REMOVE(nm, gp_next);
761 				kmem_free(nm, sizeof(struct gpio_name));
762 				break;
763 			}
764 		}
765 		sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
766 		break;
767 	default:
768 #ifdef COMPAT_50
769 		/* Try the old API */
770 		DPRINTF(("%s: trying the old API\n", device_xname(sc->sc_dev)));
771 		return gpio_ioctl_oapi(sc, cmd, data, flag, cred);
772 #else
773 		return ENOTTY;
774 #endif
775 	}
776 	return 0;
777 }
778 
779 #ifdef COMPAT_50
780 static int
781 gpio_ioctl_oapi(struct gpio_softc *sc, u_long cmd, void *data, int flag,
782     kauth_cred_t cred)
783 {
784 	gpio_chipset_tag_t gc;
785 	struct gpio_pin_op *op;
786 	struct gpio_pin_ctl *ctl;
787 	struct gpio_attach *attach;
788 	struct gpio_dev *gdev;
789 
790 	int error, pin, value, flags;
791 
792 	gc = sc->sc_gc;
793 
794 	switch (cmd) {
795 	case GPIOPINREAD:
796 		op = data;
797 
798 		pin = op->gp_pin;
799 
800 		if (pin < 0 || pin >= sc->sc_npins)
801 			return EINVAL;
802 
803 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
804 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
805 		    NULL, NULL, NULL, NULL))
806 			return EPERM;
807 
808 		/* return read value */
809 		op->gp_value = gpiobus_pin_read(gc, pin);
810 		break;
811 	case GPIOPINWRITE:
812 		if ((flag & FWRITE) == 0)
813 			return EBADF;
814 
815 		op = data;
816 
817 		pin = op->gp_pin;
818 
819 		if (pin < 0 || pin >= sc->sc_npins)
820 			return EINVAL;
821 
822 		if (sc->sc_pins[pin].pin_mapped)
823 			return EBUSY;
824 
825 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
826 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
827 		    NULL, NULL, NULL, NULL))
828 			return EPERM;
829 
830 		value = op->gp_value;
831 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
832 			return EINVAL;
833 
834 		gpiobus_pin_write(gc, pin, value);
835 		/* return old value */
836 		op->gp_value = sc->sc_pins[pin].pin_state;
837 		/* update current value */
838 		sc->sc_pins[pin].pin_state = value;
839 		break;
840 	case GPIOPINTOGGLE:
841 		if ((flag & FWRITE) == 0)
842 			return EBADF;
843 
844 		op = data;
845 
846 		pin = op->gp_pin;
847 
848 		if (pin < 0 || pin >= sc->sc_npins)
849 			return EINVAL;
850 
851 		if (sc->sc_pins[pin].pin_mapped)
852 			return EBUSY;
853 
854 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
855 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
856 		    NULL, NULL, NULL, NULL))
857 			return EPERM;
858 
859 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
860 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
861 		gpiobus_pin_write(gc, pin, value);
862 		/* return old value */
863 		op->gp_value = sc->sc_pins[pin].pin_state;
864 		/* update current value */
865 		sc->sc_pins[pin].pin_state = value;
866 		break;
867 	case GPIOPINCTL:
868 		ctl = data;
869 
870 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
871 		    NULL, NULL, NULL, NULL))
872 			return EPERM;
873 
874 		pin = ctl->gp_pin;
875 
876 		if (pin < 0 || pin >= sc->sc_npins)
877 			return EINVAL;
878 		if (sc->sc_pins[pin].pin_mapped)
879 			return EBUSY;
880 		flags = ctl->gp_flags;
881 
882 		/* check that the controller supports all requested flags */
883 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
884 			return ENODEV;
885 
886 		ctl->gp_caps = sc->sc_pins[pin].pin_caps;
887 		/* return old value */
888 		ctl->gp_flags = sc->sc_pins[pin].pin_flags;
889 		if (flags > 0) {
890 			gpiobus_pin_ctl(gc, pin, flags);
891 			/* update current value */
892 			sc->sc_pins[pin].pin_flags = flags;
893 		}
894 		break;
895 	case GPIODETACH50:
896 		/* FALLTHOUGH */
897 	case GPIODETACH:
898 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
899 		    NULL, NULL, NULL, NULL))
900 			return EPERM;
901 
902 		error = 0;
903 		mutex_enter(&sc->sc_mtx);
904 		while (sc->sc_attach_busy) {
905 			error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
906 			if (error)
907 				break;
908 		}
909 		if (!error)
910 			sc->sc_attach_busy = 1;
911 		mutex_exit(&sc->sc_mtx);
912 		if (error)
913 			return EBUSY;
914 
915 		attach = data;
916 		LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
917 			if (strcmp(device_xname(gdev->sc_dev),
918 			    attach->ga_dvname) == 0) {
919 				mutex_enter(&sc->sc_mtx);
920 				sc->sc_attach_busy = 0;
921 				cv_signal(&sc->sc_attach);
922 				mutex_exit(&sc->sc_mtx);
923 
924 				if (config_detach(gdev->sc_dev, 0) == 0)
925 					return 0;
926 				break;
927 			}
928 		}
929 		if (gdev == NULL) {
930 			mutex_enter(&sc->sc_mtx);
931 			sc->sc_attach_busy = 0;
932 			cv_signal(&sc->sc_attach);
933 			mutex_exit(&sc->sc_mtx);
934 		}
935 		return EINVAL;
936 
937 	default:
938 		return ENOTTY;
939 	}
940 	return 0;
941 }
942 #endif	/* COMPAT_50 */
943 
944 MODULE(MODULE_CLASS_DRIVER, gpio, NULL);
945 
946 #ifdef _MODULE
947 #include "ioconf.c"
948 #endif
949 
950 static int
951 gpio_modcmd(modcmd_t cmd, void *opaque)
952 {
953 #ifdef _MODULE
954 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
955 	int error;
956 #endif
957 	switch (cmd) {
958 	case MODULE_CMD_INIT:
959 #ifdef _MODULE
960 		error = config_init_component(cfdriver_ioconf_gpio,
961 		    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
962 		if (error) {
963 			aprint_error("%s: unable to init component\n",
964 			    gpio_cd.cd_name);
965 			return error;
966 		}
967 		error = devsw_attach(gpio_cd.cd_name, NULL, &bmajor,
968 		    &gpio_cdevsw, &cmajor);
969 		if (error) {
970 			aprint_error("%s: unable to register devsw\n",
971 			    gpio_cd.cd_name);
972 			return config_fini_component(cfdriver_ioconf_gpio,
973 			    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
974 		}
975 #endif
976 		return 0;
977 	case MODULE_CMD_FINI:
978 #ifdef _MODULE
979 		config_fini_component(cfdriver_ioconf_gpio,
980 		    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
981 		devsw_detach(NULL, &gpio_cdevsw);
982 #endif
983 		return 0;
984 	default:
985 		return ENOTTY;
986 	}
987 }
988