Home | History | Annotate | Line # | Download | only in gpio
gpio.c revision 1.59
      1 /* $NetBSD: gpio.c,v 1.59 2017/07/06 10:43:06 jmcneill 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 (at) msys.ch>
      6  * Copyright (c) 2004, 2006 Alexander Yurchenko <grange (at) 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.59 2017/07/06 10:43:06 jmcneill 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_discard = nodiscard,
    114 	.d_flag = D_OTHER | D_MPSAFE
    115 };
    116 
    117 extern struct cfdriver gpio_cd;
    118 
    119 static int
    120 gpio_match(device_t parent, cfdata_t cf, void *aux)
    121 {
    122 	return 1;
    123 }
    124 
    125 int
    126 gpio_submatch(device_t parent, cfdata_t cf, const int *ip, void *aux)
    127 {
    128 	struct gpio_attach_args *ga = aux;
    129 
    130 	if (ga->ga_offset == -1)
    131 		return 0;
    132 
    133 	return strcmp(ga->ga_dvname, cf->cf_name) == 0;
    134 }
    135 
    136 static bool
    137 gpio_resume(device_t self, const pmf_qual_t *qual)
    138 {
    139 	struct gpio_softc *sc = device_private(self);
    140 	int pin;
    141 
    142 	for (pin = 0; pin < sc->sc_npins; pin++) {
    143 		gpiobus_pin_ctl(sc->sc_gc, pin, sc->sc_pins[pin].pin_flags);
    144 		gpiobus_pin_write(sc->sc_gc, pin, sc->sc_pins[pin].pin_state);
    145 	}
    146 	return true;
    147 }
    148 
    149 static void
    150 gpio_childdetached(device_t self, device_t child)
    151 {
    152 #ifdef COMPAT_50
    153 	struct gpio_dev *gdev;
    154 	struct gpio_softc *sc;
    155 	int error;
    156 
    157 	/*
    158 	 * gpio_childetached is serialized because it can be entered in
    159 	 * different ways concurrently, e.g. via the GPIODETACH ioctl and
    160 	 * drvctl(8) or modunload(8).
    161 	 */
    162 	sc = device_private(self);
    163 	error = 0;
    164 	mutex_enter(&sc->sc_mtx);
    165 	while (sc->sc_attach_busy) {
    166 		error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
    167 		if (error)
    168 			break;
    169 	}
    170 	if (!error)
    171 		sc->sc_attach_busy = 1;
    172 	mutex_exit(&sc->sc_mtx);
    173 	if (error)
    174 		return;
    175 
    176 	LIST_FOREACH(gdev, &sc->sc_devs, sc_next)
    177 		if (gdev->sc_dev == child) {
    178 			LIST_REMOVE(gdev, sc_next);
    179 			kmem_free(gdev, sizeof(struct gpio_dev));
    180 			break;
    181 		}
    182 
    183 	mutex_enter(&sc->sc_mtx);
    184 	sc->sc_attach_busy = 0;
    185 	cv_signal(&sc->sc_attach);
    186 	mutex_exit(&sc->sc_mtx);
    187 #endif
    188 }
    189 
    190 static int
    191 gpio_rescan(device_t self, const char *ifattr, const int *locators)
    192 {
    193 	struct gpio_softc *sc = device_private(self);
    194 
    195 	config_search_loc(gpio_search, self, ifattr, locators, sc);
    196 
    197 	return 0;
    198 }
    199 
    200 static void
    201 gpio_attach(device_t parent, device_t self, void *aux)
    202 {
    203 	struct gpio_softc *sc = device_private(self);
    204 	struct gpiobus_attach_args *gba = aux;
    205 	struct gpio_name *nm;
    206 	int pin;
    207 
    208 	sc->sc_dev = self;
    209 	sc->sc_gc = gba->gba_gc;
    210 	sc->sc_pins = gba->gba_pins;
    211 	sc->sc_npins = gba->gba_npins;
    212 
    213 	aprint_normal(": %d pins\n", sc->sc_npins);
    214 	aprint_naive("\n");
    215 
    216 	/* Configure default pin names */
    217 	for (pin = 0; pin < sc->sc_npins; pin++) {
    218 		if (sc->sc_pins[pin].pin_defname[0] == '\0')
    219 			continue;
    220 		nm = kmem_alloc(sizeof(*nm), KM_SLEEP);
    221 		strlcpy(nm->gp_name, sc->sc_pins[pin].pin_defname,
    222 		    sizeof(nm->gp_name));
    223 		nm->gp_pin = pin;
    224 		LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
    225 	}
    226 
    227 	if (!pmf_device_register(self, NULL, gpio_resume))
    228 		aprint_error_dev(self, "couldn't establish power handler\n");
    229 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
    230 	cv_init(&sc->sc_ioctl, "gpioctl");
    231 	cv_init(&sc->sc_attach, "gpioatch");
    232 	/*
    233 	 * Attach all devices that can be connected to the GPIO pins
    234 	 * described in the kernel configuration file.
    235 	 */
    236 	gpio_rescan(self, "gpio", NULL);
    237 }
    238 
    239 static int
    240 gpio_detach(device_t self, int flags)
    241 {
    242 	struct gpio_softc *sc;
    243 	int rc;
    244 
    245 	sc = device_private(self);
    246 
    247 	if ((rc = config_detach_children(self, flags)) != 0)
    248 		return rc;
    249 	mutex_destroy(&sc->sc_mtx);
    250 	cv_destroy(&sc->sc_ioctl);
    251 #if 0
    252 	int maj, mn;
    253 
    254 	/* Locate the major number */
    255 	for (maj = 0; maj < nchrdev; maj++)
    256 		if (cdevsw[maj].d_open == gpioopen)
    257 			break;
    258 
    259 	/* Nuke the vnodes for any open instances (calls close) */
    260 	mn = device_unit(self);
    261 	vdevgone(maj, mn, mn, VCHR);
    262 #endif
    263 	return 0;
    264 }
    265 
    266 static int
    267 gpio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    268 {
    269 	struct gpio_attach_args ga;
    270 	size_t namlen;
    271 
    272 	ga.ga_gpio = aux;
    273 	ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
    274 	ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
    275 	ga.ga_flags = cf->cf_loc[GPIOCF_FLAG];
    276 	namlen = strlen(cf->cf_name) + 1;
    277 	ga.ga_dvname = kmem_alloc(namlen, KM_NOSLEEP);
    278 	if (ga.ga_dvname == NULL)
    279 		return 0;
    280 	strcpy(ga.ga_dvname, cf->cf_name);
    281 
    282 	if (config_match(parent, cf, &ga) > 0)
    283 		config_attach(parent, cf, &ga, gpio_print);
    284 	kmem_free(ga.ga_dvname, namlen);
    285 	return 0;
    286 }
    287 
    288 int
    289 gpio_print(void *aux, const char *pnp)
    290 {
    291 	struct gpio_attach_args *ga = aux;
    292 	int i;
    293 
    294 	aprint_normal(" pins");
    295 	for (i = 0; i < 32; i++)
    296 		if (ga->ga_mask & (1 << i))
    297 			aprint_normal(" %d", ga->ga_offset + i);
    298 
    299 	return UNCONF;
    300 }
    301 
    302 int
    303 gpiobus_print(void *aux, const char *pnp)
    304 {
    305 #if 0
    306 	struct gpiobus_attach_args *gba = aux;
    307 #endif
    308 	if (pnp != NULL)
    309 		aprint_normal("gpiobus at %s", pnp);
    310 
    311 	return UNCONF;
    312 }
    313 
    314 /* called from backends when a interrupt even occurs */
    315 void
    316 gpio_intr(device_t self, uint32_t evts)
    317 {
    318 	struct gpio_softc *sc = device_private(self);
    319 	void (*callback)(void *);
    320 	void *callback_arg;
    321 
    322 	for (int i = 0; i < sc->sc_npins; i++) {
    323 		if (evts & (1 << i)) {
    324 			mutex_enter(&sc->sc_mtx);
    325 			callback = sc->sc_pins[i].pin_callback;
    326 			callback_arg = sc->sc_pins[i].pin_callback_arg;
    327 			DPRINTFN(2, ("gpio pin %d event callback %p\n", i, callback));
    328 			if (callback != NULL) {
    329 				callback(callback_arg);
    330 			}
    331 			mutex_exit(&sc->sc_mtx);
    332 		}
    333 	}
    334 }
    335 
    336 void *
    337 gpio_find_device(const char *name)
    338 {
    339 	device_t gpio_dev;
    340 	gpio_dev = device_find_by_xname(name);
    341 	if (gpio_dev == NULL)
    342 		return NULL;
    343 	return device_private(gpio_dev);
    344 }
    345 
    346 const char *
    347 gpio_get_name(void *gpio)
    348 {
    349 	struct gpio_softc *sc = gpio;
    350 	return device_xname(sc->sc_dev);
    351 }
    352 
    353 /* return 1 if all pins can be mapped, 0 if not */
    354 int
    355 gpio_pin_can_map(void *gpio, int offset, uint32_t mask)
    356 {
    357 	struct gpio_softc *sc = gpio;
    358 	int npins, pin, i;
    359 
    360 	npins = gpio_npins(mask);
    361 	if (npins > sc->sc_npins)
    362 		return 0;
    363 
    364 	for (npins = 0, i = 0; i < 32; i++)
    365 		if (mask & (1 << i)) {
    366 			pin = offset + i;
    367 			if (pin < 0 || pin >= sc->sc_npins)
    368 				return 0;
    369 			if (sc->sc_pins[pin].pin_mapped)
    370 				return 0;
    371 		}
    372 
    373 	return 1;
    374 }
    375 
    376 int
    377 gpio_pin_map(void *gpio, int offset, uint32_t mask, struct gpio_pinmap *map)
    378 {
    379 	struct gpio_softc *sc = gpio;
    380 	int npins, pin, i;
    381 
    382 	npins = gpio_npins(mask);
    383 	if (npins > sc->sc_npins)
    384 		return 1;
    385 
    386 	for (npins = 0, i = 0; i < 32; i++)
    387 		if (mask & (1 << i)) {
    388 			pin = offset + i;
    389 			if (pin < 0 || pin >= sc->sc_npins)
    390 				return 1;
    391 			if (sc->sc_pins[pin].pin_mapped)
    392 				return 1;
    393 			sc->sc_pins[pin].pin_mapped = 1;
    394 			map->pm_map[npins++] = pin;
    395 		}
    396 	map->pm_size = npins;
    397 
    398 	return 0;
    399 }
    400 
    401 void
    402 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
    403 {
    404 	struct gpio_softc *sc = gpio;
    405 	int pin, i;
    406 
    407 	for (i = 0; i < map->pm_size; i++) {
    408 		pin = map->pm_map[i];
    409 		sc->sc_pins[pin].pin_mapped = 0;
    410 	}
    411 }
    412 
    413 int
    414 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
    415 {
    416 	struct gpio_softc *sc = gpio;
    417 
    418 	return gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]);
    419 }
    420 
    421 void
    422 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
    423 {
    424 	struct gpio_softc *sc = gpio;
    425 
    426 	gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value);
    427 	sc->sc_pins[map->pm_map[pin]].pin_state = value;
    428 }
    429 
    430 void
    431 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
    432 {
    433 	struct gpio_softc *sc = gpio;
    434 	struct gpio_pin *pinp = &sc->sc_pins[map->pm_map[pin]];
    435 
    436 	KASSERT((flags & GPIO_PIN_EVENTS) == 0);
    437 	mutex_enter(&sc->sc_mtx);
    438 	gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
    439 	pinp->pin_callback = NULL;
    440 	pinp->pin_callback_arg = NULL;
    441 	mutex_exit(&sc->sc_mtx);
    442 }
    443 
    444 int
    445 gpio_pin_ctl_intr(void *gpio, struct gpio_pinmap *map, int pin, int flags,
    446     int ipl, void (*callback)(void *), void *arg)
    447 {
    448 	struct gpio_softc *sc = gpio;
    449 	struct gpio_pin *pinp = &sc->sc_pins[map->pm_map[pin]];
    450 	KASSERT((flags & GPIO_PIN_EVENTS) != 0);
    451 	if (ipl != IPL_VM)
    452 		return EINVAL;
    453 	mutex_enter(&sc->sc_mtx);
    454 	if (pinp->pin_callback != NULL) {
    455 		mutex_exit(&sc->sc_mtx);
    456 		return EEXIST;
    457 	}
    458 	pinp->pin_callback = callback;
    459 	pinp->pin_callback_arg = arg;
    460 	gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
    461 	mutex_exit(&sc->sc_mtx);
    462 	return 0;
    463 }
    464 
    465 void
    466 gpio_pin_irqen(void *gpio, struct gpio_pinmap *map, int pin, bool en)
    467 {
    468 	struct gpio_softc *sc = gpio;
    469 	gpiobus_pin_irqen(sc->sc_gc, map->pm_map[pin], en);
    470 }
    471 
    472 int
    473 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
    474 {
    475 	struct gpio_softc *sc = gpio;
    476 
    477 	return sc->sc_pins[map->pm_map[pin]].pin_caps;
    478 }
    479 
    480 int
    481 gpio_npins(uint32_t mask)
    482 {
    483 	int npins, i;
    484 
    485 	for (npins = 0, i = 0; i < 32; i++)
    486 		if (mask & (1 << i))
    487 			npins++;
    488 
    489 	return npins;
    490 }
    491 
    492 int
    493 gpio_lock(void *data)
    494 {
    495 	struct gpio_softc *sc;
    496 	int error;
    497 
    498 	error = 0;
    499 	sc = data;
    500 	mutex_enter(&sc->sc_mtx);
    501 	while (sc->sc_ioctl_busy) {
    502 		error = cv_wait_sig(&sc->sc_ioctl, &sc->sc_mtx);
    503 		if (error)
    504 			break;
    505 	}
    506 	if (!error)
    507 		sc->sc_ioctl_busy = 1;
    508 	mutex_exit(&sc->sc_mtx);
    509 	return error;
    510 }
    511 
    512 void
    513 gpio_unlock(void *data)
    514 {
    515 	struct gpio_softc *sc;
    516 
    517 	sc = data;
    518 	mutex_enter(&sc->sc_mtx);
    519 	sc->sc_ioctl_busy = 0;
    520 	cv_signal(&sc->sc_ioctl);
    521 	mutex_exit(&sc->sc_mtx);
    522 }
    523 
    524 int
    525 gpioopen(dev_t dev, int flag, int mode, struct lwp *l)
    526 {
    527 	struct gpio_softc *sc;
    528 
    529 	sc = device_lookup_private(&gpio_cd, minor(dev));
    530 	if (sc == NULL)
    531 		return ENXIO;
    532 
    533 	return gpiobus_open(sc->sc_gc, sc->sc_dev);
    534 }
    535 
    536 int
    537 gpioclose(dev_t dev, int flag, int mode, struct lwp *l)
    538 {
    539 	struct gpio_softc *sc;
    540 
    541 	sc = device_lookup_private(&gpio_cd, minor(dev));
    542 	return gpiobus_close(sc->sc_gc, sc->sc_dev);
    543 }
    544 
    545 static int
    546 gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
    547 {
    548         struct gpio_name *nm;
    549 
    550         LIST_FOREACH(nm, &sc->sc_names, gp_next)
    551                 if (!strcmp(nm->gp_name, gp_name))
    552                         return nm->gp_pin;
    553         return -1;
    554 }
    555 
    556 int
    557 gpioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    558 {
    559 	int error;
    560 	struct gpio_softc *sc;
    561 
    562 	sc = device_lookup_private(&gpio_cd, minor(dev));
    563 
    564 	error = gpio_lock(sc);
    565 	if (error)
    566 		return error;
    567 
    568 	error = gpio_ioctl(sc, cmd, data, flag, l);
    569 	gpio_unlock(sc);
    570 	return error;
    571 }
    572 
    573 static int
    574 gpio_ioctl(struct gpio_softc *sc, u_long cmd, void *data, int flag,
    575     struct lwp *l)
    576 {
    577 	gpio_chipset_tag_t gc;
    578 	struct gpio_info *info;
    579 	struct gpio_attach *attach;
    580 	struct gpio_attach_args ga;
    581 	struct gpio_req *req;
    582 	struct gpio_name *nm;
    583 	struct gpio_set *set;
    584 #ifdef COMPAT_50
    585 	struct gpio_dev *gdev;
    586 #endif
    587 	device_t dv;
    588 	cfdata_t cf;
    589 	kauth_cred_t cred;
    590 	int locs[GPIOCF_NLOCS];
    591 	int error, pin, value, flags, npins;
    592 
    593 	gc = sc->sc_gc;
    594 	ga.ga_flags = 0;
    595 
    596 	if (cmd != GPIOINFO && !device_is_active(sc->sc_dev)) {
    597 		DPRINTF(("%s: device is not active\n",
    598 		    device_xname(sc->sc_dev)));
    599 		return EBUSY;
    600 	}
    601 
    602 	cred = kauth_cred_get();
    603 
    604 	switch (cmd) {
    605 	case GPIOINFO:
    606 		info = data;
    607 		if (!kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    608 		    NULL, NULL, NULL, NULL))
    609 			info->gpio_npins = sc->sc_npins;
    610 		else {
    611 			for (pin = npins = 0; pin < sc->sc_npins; pin++)
    612 				if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
    613 					++npins;
    614 			info->gpio_npins = npins;
    615 		}
    616 		break;
    617 	case GPIOREAD:
    618 		req = data;
    619 
    620 		if (req->gp_name[0] != '\0')
    621 			pin = gpio_pinbyname(sc, req->gp_name);
    622 		else
    623 			pin = req->gp_pin;
    624 
    625 		if (pin < 0 || pin >= sc->sc_npins)
    626 			return EINVAL;
    627 
    628 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    629 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    630 		    NULL, NULL, NULL, NULL))
    631 			return EPERM;
    632 
    633 		/* return read value */
    634 		req->gp_value = gpiobus_pin_read(gc, pin);
    635 		break;
    636 	case GPIOWRITE:
    637 		if ((flag & FWRITE) == 0)
    638 			return EBADF;
    639 
    640 		req = data;
    641 
    642 		if (req->gp_name[0] != '\0')
    643 			pin = gpio_pinbyname(sc, req->gp_name);
    644 		else
    645 			pin = req->gp_pin;
    646 
    647 		if (pin < 0 || pin >= sc->sc_npins)
    648 			return EINVAL;
    649 
    650 		if (sc->sc_pins[pin].pin_mapped)
    651 			return EBUSY;
    652 
    653 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    654 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    655 		    NULL, NULL, NULL, NULL))
    656 			return EPERM;
    657 
    658 		value = req->gp_value;
    659 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
    660 			return EINVAL;
    661 
    662 		/* return old value */
    663 		req->gp_value = gpiobus_pin_read(gc, pin);
    664 		gpiobus_pin_write(gc, pin, value);
    665 		/* update current value */
    666 		sc->sc_pins[pin].pin_state = value;
    667 		break;
    668 	case GPIOTOGGLE:
    669 		if ((flag & FWRITE) == 0)
    670 			return EBADF;
    671 
    672 		req = data;
    673 
    674 		if (req->gp_name[0] != '\0')
    675 			pin = gpio_pinbyname(sc, req->gp_name);
    676 		else
    677 			pin = req->gp_pin;
    678 
    679 		if (pin < 0 || pin >= sc->sc_npins)
    680 			return EINVAL;
    681 
    682 		if (sc->sc_pins[pin].pin_mapped)
    683 			return EBUSY;
    684 
    685 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    686 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    687 		    NULL, NULL, NULL, NULL))
    688 			return EPERM;
    689 
    690 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
    691 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
    692 		gpiobus_pin_write(gc, pin, value);
    693 		/* return old value */
    694 		req->gp_value = sc->sc_pins[pin].pin_state;
    695 		/* update current value */
    696 		sc->sc_pins[pin].pin_state = value;
    697 		break;
    698 	case GPIOATTACH:
    699 		attach = data;
    700 		ga.ga_flags = attach->ga_flags;
    701 #ifdef COMPAT_50
    702 		/* FALLTHROUGH */
    703 	case GPIOATTACH50:
    704 		/*
    705 		 * The double assignment to 'attach' in case of GPIOATTACH
    706 		 * and COMPAT_50 is on purpose. It ensures backward
    707 		 * compatability in case we are called through the old
    708 		 * GPIOATTACH50 ioctl(2), which had not the ga_flags field
    709 		 * in struct gpio_attach.
    710 		 */
    711 		attach = data;
    712 #endif
    713 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    714 		    NULL, NULL, NULL, NULL))
    715 			return EPERM;
    716 
    717 		/* do not try to attach if the pins are already mapped */
    718 		if (!gpio_pin_can_map(sc, attach->ga_offset, attach->ga_mask))
    719 			return EBUSY;
    720 
    721 		error = 0;
    722 		mutex_enter(&sc->sc_mtx);
    723 		while (sc->sc_attach_busy) {
    724 			error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
    725 			if (error)
    726 				break;
    727 		}
    728 		if (!error)
    729 			sc->sc_attach_busy = 1;
    730 		mutex_exit(&sc->sc_mtx);
    731 		if (error)
    732 			return EBUSY;
    733 
    734 		ga.ga_gpio = sc;
    735 		/* Don't access attach->ga_flags here. */
    736 		ga.ga_dvname = attach->ga_dvname;
    737 		ga.ga_offset = attach->ga_offset;
    738 		ga.ga_mask = attach->ga_mask;
    739 		DPRINTF(("%s: attach %s with offset %d, mask "
    740 		    "0x%02x, and flags 0x%02x\n", device_xname(sc->sc_dev),
    741 		    ga.ga_dvname, ga.ga_offset, ga.ga_mask, ga.ga_flags));
    742 
    743 		locs[GPIOCF_OFFSET] = ga.ga_offset;
    744 		locs[GPIOCF_MASK] = ga.ga_mask;
    745 		locs[GPIOCF_FLAG] = ga.ga_flags;
    746 
    747 		cf = config_search_loc(NULL, sc->sc_dev, "gpio", locs, &ga);
    748 		if (cf != NULL) {
    749 			dv = config_attach_loc(sc->sc_dev, cf, locs, &ga,
    750 			    gpiobus_print);
    751 #ifdef COMPAT_50
    752 			if (dv != NULL) {
    753 				gdev = kmem_alloc(sizeof(struct gpio_dev),
    754 				    KM_SLEEP);
    755 				gdev->sc_dev = dv;
    756 				LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
    757 			} else
    758 				error = EINVAL;
    759 #else
    760 			if (dv == NULL)
    761 				error = EINVAL;
    762 #endif
    763 		} else
    764 			error = EINVAL;
    765 		mutex_enter(&sc->sc_mtx);
    766 		sc->sc_attach_busy = 0;
    767 		cv_signal(&sc->sc_attach);
    768 		mutex_exit(&sc->sc_mtx);
    769 		return error;
    770 	case GPIOSET:
    771 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    772 		    NULL, NULL, NULL, NULL))
    773 			return EPERM;
    774 
    775 		set = data;
    776 
    777 		if (set->gp_name[0] != '\0')
    778 			pin = gpio_pinbyname(sc, set->gp_name);
    779 		else
    780 			pin = set->gp_pin;
    781 
    782 		if (pin < 0 || pin >= sc->sc_npins)
    783 			return EINVAL;
    784 		flags = set->gp_flags;
    785 
    786 		/* check that the controller supports all requested flags */
    787 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
    788 			return ENODEV;
    789 		flags = set->gp_flags;
    790 
    791 		set->gp_caps = sc->sc_pins[pin].pin_caps;
    792 		/* return old value */
    793 		set->gp_flags = sc->sc_pins[pin].pin_flags;
    794 
    795 		if (flags > 0) {
    796 			flags |= GPIO_PIN_SET;
    797 			gpiobus_pin_ctl(gc, pin, flags);
    798 			/* update current value */
    799 			sc->sc_pins[pin].pin_flags = flags;
    800 		}
    801 
    802 		/* rename pin or new pin? */
    803 		if (set->gp_name2[0] != '\0') {
    804 			struct gpio_name *gnm;
    805 
    806 			gnm = NULL;
    807 			LIST_FOREACH(nm, &sc->sc_names, gp_next) {
    808 				if (!strcmp(nm->gp_name, set->gp_name2) &&
    809 				    nm->gp_pin != pin)
    810 					return EINVAL;	/* duplicate name */
    811 				if (nm->gp_pin == pin)
    812 					gnm = nm;
    813 			}
    814 			if (gnm != NULL)
    815 				strlcpy(gnm->gp_name, set->gp_name2,
    816 				    sizeof(gnm->gp_name));
    817 			else  {
    818 				nm = kmem_alloc(sizeof(struct gpio_name),
    819 				    KM_SLEEP);
    820 				strlcpy(nm->gp_name, set->gp_name2,
    821 				    sizeof(nm->gp_name));
    822 				nm->gp_pin = set->gp_pin;
    823 				LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
    824 			}
    825 		}
    826 		break;
    827 	case GPIOUNSET:
    828 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    829 		    NULL, NULL, NULL, NULL))
    830 			return EPERM;
    831 
    832 		set = data;
    833 		if (set->gp_name[0] != '\0')
    834 			pin = gpio_pinbyname(sc, set->gp_name);
    835 		else
    836 			pin = set->gp_pin;
    837 
    838 		if (pin < 0 || pin >= sc->sc_npins)
    839 			return EINVAL;
    840 		if (sc->sc_pins[pin].pin_mapped)
    841 			return EBUSY;
    842 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
    843 			return EINVAL;
    844 
    845 		LIST_FOREACH(nm, &sc->sc_names, gp_next) {
    846 			if (nm->gp_pin == pin) {
    847 				LIST_REMOVE(nm, gp_next);
    848 				kmem_free(nm, sizeof(struct gpio_name));
    849 				break;
    850 			}
    851 		}
    852 		sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
    853 		break;
    854 	default:
    855 #ifdef COMPAT_50
    856 		/* Try the old API */
    857 		DPRINTF(("%s: trying the old API\n", device_xname(sc->sc_dev)));
    858 		return gpio_ioctl_oapi(sc, cmd, data, flag, cred);
    859 #else
    860 		return ENOTTY;
    861 #endif
    862 	}
    863 	return 0;
    864 }
    865 
    866 #ifdef COMPAT_50
    867 static int
    868 gpio_ioctl_oapi(struct gpio_softc *sc, u_long cmd, void *data, int flag,
    869     kauth_cred_t cred)
    870 {
    871 	gpio_chipset_tag_t gc;
    872 	struct gpio_pin_op *op;
    873 	struct gpio_pin_ctl *ctl;
    874 	struct gpio_attach *attach;
    875 	struct gpio_dev *gdev;
    876 
    877 	int error, pin, value, flags;
    878 
    879 	gc = sc->sc_gc;
    880 
    881 	switch (cmd) {
    882 	case GPIOPINREAD:
    883 		op = data;
    884 
    885 		pin = op->gp_pin;
    886 
    887 		if (pin < 0 || pin >= sc->sc_npins)
    888 			return EINVAL;
    889 
    890 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    891 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    892 		    NULL, NULL, NULL, NULL))
    893 			return EPERM;
    894 
    895 		/* return read value */
    896 		op->gp_value = gpiobus_pin_read(gc, pin);
    897 		break;
    898 	case GPIOPINWRITE:
    899 		if ((flag & FWRITE) == 0)
    900 			return EBADF;
    901 
    902 		op = data;
    903 
    904 		pin = op->gp_pin;
    905 
    906 		if (pin < 0 || pin >= sc->sc_npins)
    907 			return EINVAL;
    908 
    909 		if (sc->sc_pins[pin].pin_mapped)
    910 			return EBUSY;
    911 
    912 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    913 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    914 		    NULL, NULL, NULL, NULL))
    915 			return EPERM;
    916 
    917 		value = op->gp_value;
    918 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
    919 			return EINVAL;
    920 
    921 		gpiobus_pin_write(gc, pin, value);
    922 		/* return old value */
    923 		op->gp_value = sc->sc_pins[pin].pin_state;
    924 		/* update current value */
    925 		sc->sc_pins[pin].pin_state = value;
    926 		break;
    927 	case GPIOPINTOGGLE:
    928 		if ((flag & FWRITE) == 0)
    929 			return EBADF;
    930 
    931 		op = data;
    932 
    933 		pin = op->gp_pin;
    934 
    935 		if (pin < 0 || pin >= sc->sc_npins)
    936 			return EINVAL;
    937 
    938 		if (sc->sc_pins[pin].pin_mapped)
    939 			return EBUSY;
    940 
    941 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    942 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    943 		    NULL, NULL, NULL, NULL))
    944 			return EPERM;
    945 
    946 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
    947 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
    948 		gpiobus_pin_write(gc, pin, value);
    949 		/* return old value */
    950 		op->gp_value = sc->sc_pins[pin].pin_state;
    951 		/* update current value */
    952 		sc->sc_pins[pin].pin_state = value;
    953 		break;
    954 	case GPIOPINCTL:
    955 		ctl = data;
    956 
    957 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    958 		    NULL, NULL, NULL, NULL))
    959 			return EPERM;
    960 
    961 		pin = ctl->gp_pin;
    962 
    963 		if (pin < 0 || pin >= sc->sc_npins)
    964 			return EINVAL;
    965 		if (sc->sc_pins[pin].pin_mapped)
    966 			return EBUSY;
    967 		flags = ctl->gp_flags;
    968 
    969 		/* check that the controller supports all requested flags */
    970 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
    971 			return ENODEV;
    972 
    973 		ctl->gp_caps = sc->sc_pins[pin].pin_caps;
    974 		/* return old value */
    975 		ctl->gp_flags = sc->sc_pins[pin].pin_flags;
    976 		if (flags > 0) {
    977 			gpiobus_pin_ctl(gc, pin, flags);
    978 			/* update current value */
    979 			sc->sc_pins[pin].pin_flags = flags;
    980 		}
    981 		break;
    982 	case GPIODETACH50:
    983 		/* FALLTHOUGH */
    984 	case GPIODETACH:
    985 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    986 		    NULL, NULL, NULL, NULL))
    987 			return EPERM;
    988 
    989 		error = 0;
    990 		mutex_enter(&sc->sc_mtx);
    991 		while (sc->sc_attach_busy) {
    992 			error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
    993 			if (error)
    994 				break;
    995 		}
    996 		if (!error)
    997 			sc->sc_attach_busy = 1;
    998 		mutex_exit(&sc->sc_mtx);
    999 		if (error)
   1000 			return EBUSY;
   1001 
   1002 		attach = data;
   1003 		LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
   1004 			if (strcmp(device_xname(gdev->sc_dev),
   1005 			    attach->ga_dvname) == 0) {
   1006 				mutex_enter(&sc->sc_mtx);
   1007 				sc->sc_attach_busy = 0;
   1008 				cv_signal(&sc->sc_attach);
   1009 				mutex_exit(&sc->sc_mtx);
   1010 
   1011 				if (config_detach(gdev->sc_dev, 0) == 0)
   1012 					return 0;
   1013 				break;
   1014 			}
   1015 		}
   1016 		if (gdev == NULL) {
   1017 			mutex_enter(&sc->sc_mtx);
   1018 			sc->sc_attach_busy = 0;
   1019 			cv_signal(&sc->sc_attach);
   1020 			mutex_exit(&sc->sc_mtx);
   1021 		}
   1022 		return EINVAL;
   1023 
   1024 	default:
   1025 		return ENOTTY;
   1026 	}
   1027 	return 0;
   1028 }
   1029 #endif	/* COMPAT_50 */
   1030 
   1031 MODULE(MODULE_CLASS_DRIVER, gpio, NULL);
   1032 
   1033 #ifdef _MODULE
   1034 #include "ioconf.c"
   1035 #endif
   1036 
   1037 static int
   1038 gpio_modcmd(modcmd_t cmd, void *opaque)
   1039 {
   1040 #ifdef _MODULE
   1041 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
   1042 	int error;
   1043 #endif
   1044 	switch (cmd) {
   1045 	case MODULE_CMD_INIT:
   1046 #ifdef _MODULE
   1047 		error = config_init_component(cfdriver_ioconf_gpio,
   1048 		    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
   1049 		if (error) {
   1050 			aprint_error("%s: unable to init component\n",
   1051 			    gpio_cd.cd_name);
   1052 			return error;
   1053 		}
   1054 		error = devsw_attach(gpio_cd.cd_name, NULL, &bmajor,
   1055 		    &gpio_cdevsw, &cmajor);
   1056 		if (error) {
   1057 			aprint_error("%s: unable to register devsw\n",
   1058 			    gpio_cd.cd_name);
   1059 			return config_fini_component(cfdriver_ioconf_gpio,
   1060 			    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
   1061 		}
   1062 #endif
   1063 		return 0;
   1064 	case MODULE_CMD_FINI:
   1065 #ifdef _MODULE
   1066 		config_fini_component(cfdriver_ioconf_gpio,
   1067 		    cfattach_ioconf_gpio, cfdata_ioconf_gpio);
   1068 		devsw_detach(NULL, &gpio_cdevsw);
   1069 #endif
   1070 		return 0;
   1071 	default:
   1072 		return ENOTTY;
   1073 	}
   1074 }
   1075