Home | History | Annotate | Line # | Download | only in gpio
gpio.c revision 1.30
      1 /* $NetBSD: gpio.c,v 1.30 2010/01/08 19:48:18 dyoung Exp $ */
      2 /*	$OpenBSD: gpio.c,v 1.6 2006/01/14 12:33:49 grange Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2008, 2009 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.30 2010/01/08 19:48:18 dyoung Exp $");
     23 
     24 /*
     25  * General Purpose Input/Output framework.
     26  */
     27 
     28 #include <sys/param.h>
     29 #include <sys/systm.h>
     30 #include <sys/conf.h>
     31 #include <sys/device.h>
     32 #include <sys/fcntl.h>
     33 #include <sys/ioctl.h>
     34 #include <sys/gpio.h>
     35 #include <sys/vnode.h>
     36 #include <sys/kmem.h>
     37 #include <sys/queue.h>
     38 #include <sys/kauth.h>
     39 
     40 #include <dev/gpio/gpiovar.h>
     41 
     42 #include "locators.h"
     43 
     44 #ifdef GPIO_DEBUG
     45 #define DPRINTFN(n, x)	do { if (gpiodebug > (n)) printf x; } while (0)
     46 int gpiodebug = 0;
     47 #else
     48 #define DPRINTFN(n, x)
     49 #endif
     50 #define DPRINTF(x)	DPRINTFN(0, x)
     51 
     52 struct gpio_softc {
     53 	device_t		 sc_dev;
     54 
     55 	gpio_chipset_tag_t	 sc_gc;		/* GPIO controller */
     56 	gpio_pin_t		*sc_pins;	/* pins array */
     57 	int			 sc_npins;	/* number of pins */
     58 
     59 	int			 sc_opened;
     60 	LIST_HEAD(, gpio_dev)	 sc_devs;	/* devices */
     61 	LIST_HEAD(, gpio_name)	 sc_names;	/* named pins */
     62 };
     63 
     64 int	gpio_match(device_t, cfdata_t, void *);
     65 int	gpio_submatch(device_t, cfdata_t, const int *, void *);
     66 void	gpio_attach(device_t, device_t, void *);
     67 int	gpio_rescan(device_t, const char *, const int *);
     68 void	gpio_childdetached(device_t, device_t);
     69 bool	gpio_resume(device_t, pmf_qual_t);
     70 int	gpio_detach(device_t, int);
     71 int	gpio_search(device_t, cfdata_t, const int *, void *);
     72 int	gpio_print(void *, const char *);
     73 int	gpio_pinbyname(struct gpio_softc *, char *);
     74 
     75 /* Old API */
     76 int	gpio_ioctl_oapi(struct gpio_softc *, u_long, void *, int, kauth_cred_t);
     77 
     78 CFATTACH_DECL3_NEW(gpio, sizeof(struct gpio_softc),
     79     gpio_match, gpio_attach, gpio_detach, NULL, gpio_rescan,
     80     gpio_childdetached, DVF_DETACH_SHUTDOWN);
     81 
     82 dev_type_open(gpioopen);
     83 dev_type_close(gpioclose);
     84 dev_type_ioctl(gpioioctl);
     85 
     86 const struct cdevsw gpio_cdevsw = {
     87 	gpioopen, gpioclose, noread, nowrite, gpioioctl,
     88 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     89 };
     90 
     91 extern struct cfdriver gpio_cd;
     92 
     93 int
     94 gpio_match(device_t parent, cfdata_t cf, void *aux)
     95 {
     96 	return 1;
     97 }
     98 
     99 int
    100 gpio_submatch(device_t parent, cfdata_t cf, const int *ip, void *aux)
    101 {
    102 	struct gpio_attach_args *ga = aux;
    103 
    104 	if (ga->ga_offset == -1)
    105 		return 0;
    106 
    107 	return strcmp(ga->ga_dvname, cf->cf_name) == 0;
    108 }
    109 
    110 bool
    111 gpio_resume(device_t self, pmf_qual_t qual)
    112 {
    113 	struct gpio_softc *sc = device_private(self);
    114 	int pin;
    115 
    116 	for (pin = 0; pin < sc->sc_npins; pin++) {
    117 		gpiobus_pin_ctl(sc->sc_gc, pin, sc->sc_pins[pin].pin_flags);
    118 		gpiobus_pin_write(sc->sc_gc, pin, sc->sc_pins[pin].pin_state);
    119 	}
    120 	return true;
    121 }
    122 
    123 void
    124 gpio_childdetached(device_t self, device_t child)
    125 {
    126 	/* gpio(4) keeps no references to its children, so do nothing. */
    127 }
    128 
    129 int
    130 gpio_rescan(device_t self, const char *ifattr, const int *locators)
    131 {
    132 	struct gpio_softc *sc = device_private(self);
    133 
    134 	config_search_loc(gpio_search, self, ifattr, locators, sc);
    135 
    136 	return 0;
    137 }
    138 
    139 void
    140 gpio_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct gpio_softc *sc = device_private(self);
    143 	struct gpiobus_attach_args *gba = aux;
    144 
    145 	sc->sc_dev = self;
    146 	sc->sc_gc = gba->gba_gc;
    147 	sc->sc_pins = gba->gba_pins;
    148 	sc->sc_npins = gba->gba_npins;
    149 
    150 	printf(": %d pins\n", sc->sc_npins);
    151 
    152 	if (!pmf_device_register(self, NULL, gpio_resume))
    153 		aprint_error_dev(self, "couldn't establish power handler\n");
    154 
    155 	/*
    156 	 * Attach all devices that can be connected to the GPIO pins
    157 	 * described in the kernel configuration file.
    158 	 */
    159 	gpio_rescan(self, "gpio", NULL);
    160 }
    161 
    162 int
    163 gpio_detach(device_t self, int flags)
    164 {
    165 	int rc;
    166 
    167 	if ((rc = config_detach_children(self, flags)) != 0)
    168 		return rc;
    169 
    170 #if 0
    171 	int maj, mn;
    172 
    173 	/* Locate the major number */
    174 	for (maj = 0; maj < nchrdev; maj++)
    175 		if (cdevsw[maj].d_open == gpioopen)
    176 			break;
    177 
    178 	/* Nuke the vnodes for any open instances (calls close) */
    179 	mn = device_unit(self);
    180 	vdevgone(maj, mn, mn, VCHR);
    181 #endif
    182 	return 0;
    183 }
    184 
    185 int
    186 gpio_search(device_t parent, cfdata_t cf,
    187     const int *ldesc, void *aux)
    188 {
    189 	struct gpio_attach_args ga;
    190 
    191 	ga.ga_gpio = aux;
    192 	ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
    193 	ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
    194 
    195 	if (config_match(parent, cf, &ga) > 0)
    196 		config_attach(parent, cf, &ga, gpio_print);
    197 
    198 	return 0;
    199 }
    200 
    201 int
    202 gpio_print(void *aux, const char *pnp)
    203 {
    204 	struct gpio_attach_args *ga = aux;
    205 	int i;
    206 
    207 	printf(" pins");
    208 	for (i = 0; i < 32; i++)
    209 		if (ga->ga_mask & (1 << i))
    210 			printf(" %d", ga->ga_offset + i);
    211 
    212 	return UNCONF;
    213 }
    214 
    215 int
    216 gpiobus_print(void *aux, const char *pnp)
    217 {
    218 #if 0
    219 	struct gpiobus_attach_args *gba = aux;
    220 #endif
    221 	if (pnp != NULL)
    222 		printf("gpiobus at %s", pnp);
    223 
    224 	return UNCONF;
    225 }
    226 
    227 /* return 1 if all pins can be mapped, 0 if not */
    228 
    229 int
    230 gpio_pin_can_map(void *gpio, int offset, u_int32_t mask)
    231 {
    232 	struct gpio_softc *sc = gpio;
    233 	int npins, pin, i;
    234 
    235 	npins = gpio_npins(mask);
    236 	if (npins > sc->sc_npins)
    237 		return 0;
    238 
    239 	for (npins = 0, i = 0; i < 32; i++)
    240 		if (mask & (1 << i)) {
    241 			pin = offset + i;
    242 			if (pin < 0 || pin >= sc->sc_npins)
    243 				return 0;
    244 			if (sc->sc_pins[pin].pin_mapped)
    245 				return 0;
    246 		}
    247 
    248 	return 1;
    249 }
    250 
    251 int
    252 gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map)
    253 {
    254 	struct gpio_softc *sc = gpio;
    255 	int npins, pin, i;
    256 
    257 	npins = gpio_npins(mask);
    258 	if (npins > sc->sc_npins)
    259 		return 1;
    260 
    261 	for (npins = 0, i = 0; i < 32; i++)
    262 		if (mask & (1 << i)) {
    263 			pin = offset + i;
    264 			if (pin < 0 || pin >= sc->sc_npins)
    265 				return 1;
    266 			if (sc->sc_pins[pin].pin_mapped)
    267 				return 1;
    268 			sc->sc_pins[pin].pin_mapped = 1;
    269 			map->pm_map[npins++] = pin;
    270 		}
    271 	map->pm_size = npins;
    272 
    273 	return 0;
    274 }
    275 
    276 void
    277 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
    278 {
    279 	struct gpio_softc *sc = gpio;
    280 	int pin, i;
    281 
    282 	for (i = 0; i < map->pm_size; i++) {
    283 		pin = map->pm_map[i];
    284 		sc->sc_pins[pin].pin_mapped = 0;
    285 	}
    286 }
    287 
    288 int
    289 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
    290 {
    291 	struct gpio_softc *sc = gpio;
    292 
    293 	return gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]);
    294 }
    295 
    296 void
    297 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
    298 {
    299 	struct gpio_softc *sc = gpio;
    300 
    301 	gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value);
    302 	sc->sc_pins[map->pm_map[pin]].pin_state = value;
    303 }
    304 
    305 void
    306 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
    307 {
    308 	struct gpio_softc *sc = gpio;
    309 
    310 	return gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
    311 }
    312 
    313 int
    314 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
    315 {
    316 	struct gpio_softc *sc = gpio;
    317 
    318 	return sc->sc_pins[map->pm_map[pin]].pin_caps;
    319 }
    320 
    321 int
    322 gpio_npins(u_int32_t mask)
    323 {
    324 	int npins, i;
    325 
    326 	for (npins = 0, i = 0; i < 32; i++)
    327 		if (mask & (1 << i))
    328 			npins++;
    329 
    330 	return npins;
    331 }
    332 
    333 int
    334 gpioopen(dev_t dev, int flag, int mode, struct lwp *l)
    335 {
    336 	struct gpio_softc *sc;
    337 	int ret;
    338 
    339 	sc = device_lookup_private(&gpio_cd, minor(dev));
    340 	if (sc == NULL)
    341 		return ENXIO;
    342 	DPRINTF(("%s: opening\n", device_xname(sc->sc_dev)));
    343 	if (sc->sc_opened) {
    344 		DPRINTF(("%s: already opened\n", device_xname(sc->sc_dev)));
    345 		return EBUSY;
    346 	}
    347 
    348 	if ((ret = gpiobus_open(sc->sc_gc, sc->sc_dev))) {
    349 		DPRINTF(("%s: gpiobus_open returned %d\n",
    350 		    device_xname(sc->sc_dev),
    351 		    ret));
    352 		return ret;
    353 	}
    354 
    355 	sc->sc_opened = 1;
    356 
    357 	return 0;
    358 }
    359 
    360 int
    361 gpioclose(dev_t dev, int flag, int mode, struct lwp *l)
    362 {
    363 	struct gpio_softc *sc;
    364 
    365 	sc = device_lookup_private(&gpio_cd, minor(dev));
    366 	DPRINTF(("%s: closing\n", device_xname(sc->sc_dev)));
    367 	gpiobus_close(sc->sc_gc, sc->sc_dev);
    368 	sc->sc_opened = 0;
    369 
    370 	return 0;
    371 }
    372 
    373 int
    374 gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
    375 {
    376         struct gpio_name *nm;
    377 
    378         LIST_FOREACH(nm, &sc->sc_names, gp_next)
    379                 if (!strcmp(nm->gp_name, gp_name))
    380                         return nm->gp_pin;
    381         return -1;
    382 }
    383 
    384 int
    385 gpioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    386 {
    387 	struct gpio_softc *sc;
    388 	gpio_chipset_tag_t gc;
    389 	struct gpio_info *info;
    390 	struct gpio_attach *attach;
    391 	struct gpio_attach_args ga;
    392 	struct gpio_dev *gdev;
    393 	struct gpio_req *req;
    394 	struct gpio_name *nm;
    395 	struct gpio_set *set;
    396 	device_t dv;
    397 	cfdata_t cf;
    398 	kauth_cred_t cred;
    399 	int locs[GPIOCF_NLOCS];
    400 	int pin, value, flags, npins;
    401 
    402 	sc = device_lookup_private(&gpio_cd, minor(dev));
    403 	gc = sc->sc_gc;
    404 
    405 	if (cmd != GPIOINFO && !device_is_active(sc->sc_dev)) {
    406 		DPRINTF(("%s: device is not active\n",
    407 		    device_xname(sc->sc_dev)));
    408 		return EBUSY;
    409 	}
    410 
    411 	cred = kauth_cred_get();
    412 
    413 	switch (cmd) {
    414 	case GPIOINFO:
    415 		info = (struct gpio_info *)data;
    416 		if (!kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    417 		    NULL, NULL, NULL, NULL))
    418 			info->gpio_npins = sc->sc_npins;
    419 		else {
    420 			for (pin = npins = 0; pin < sc->sc_npins; pin++)
    421 				if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
    422 					++npins;
    423 			info->gpio_npins = npins;
    424 		}
    425 		break;
    426 	case GPIOREAD:
    427 		req = (struct gpio_req *)data;
    428 
    429 		if (req->gp_name[0] != '\0') {
    430 			pin = gpio_pinbyname(sc, req->gp_name);
    431 			if (pin == -1)
    432 				return EINVAL;
    433 		} else
    434 			pin = req->gp_pin;
    435 
    436 		if (pin < 0 || pin >= sc->sc_npins)
    437 			return EINVAL;
    438 
    439 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    440 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    441 		    NULL, NULL, NULL, NULL))
    442 			return EPERM;
    443 
    444 		/* return read value */
    445 		req->gp_value = gpiobus_pin_read(gc, pin);
    446 		break;
    447 	case GPIOWRITE:
    448 		if ((flag & FWRITE) == 0)
    449 			return EBADF;
    450 
    451 		req = (struct gpio_req *)data;
    452 
    453 		if (req->gp_name[0] != '\0') {
    454 			pin = gpio_pinbyname(sc, req->gp_name);
    455 			if (pin == -1)
    456 				return EINVAL;
    457 		} else
    458 			pin = req->gp_pin;
    459 
    460 		if (pin < 0 || pin >= sc->sc_npins)
    461 			return EINVAL;
    462 
    463 		if (sc->sc_pins[pin].pin_mapped)
    464 			return EBUSY;
    465 
    466 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    467 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    468 		    NULL, NULL, NULL, NULL))
    469 			return EPERM;
    470 
    471 		value = req->gp_value;
    472 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
    473 			return EINVAL;
    474 
    475 		gpiobus_pin_write(gc, pin, value);
    476 		/* return old value */
    477 		req->gp_value = sc->sc_pins[pin].pin_state;
    478 		/* update current value */
    479 		sc->sc_pins[pin].pin_state = value;
    480 		break;
    481 	case GPIOTOGGLE:
    482 		if ((flag & FWRITE) == 0)
    483 			return EBADF;
    484 
    485 		req = (struct gpio_req *)data;
    486 
    487 		if (req->gp_name[0] != '\0') {
    488 			pin = gpio_pinbyname(sc, req->gp_name);
    489 			if (pin == -1)
    490 				return EINVAL;
    491 		} else
    492 			pin = req->gp_pin;
    493 
    494 		if (pin < 0 || pin >= sc->sc_npins)
    495 			return EINVAL;
    496 
    497 		if (sc->sc_pins[pin].pin_mapped)
    498 			return EBUSY;
    499 
    500 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    501 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    502 		    NULL, NULL, NULL, NULL))
    503 			return EPERM;
    504 
    505 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
    506 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
    507 		gpiobus_pin_write(gc, pin, value);
    508 		/* return old value */
    509 		req->gp_value = sc->sc_pins[pin].pin_state;
    510 		/* update current value */
    511 		sc->sc_pins[pin].pin_state = value;
    512 		break;
    513 	case GPIOATTACH:
    514 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    515 		    NULL, NULL, NULL, NULL))
    516 			return EPERM;
    517 
    518 		attach = (struct gpio_attach *)data;
    519 
    520 		/* do not try to attach if the pins are already mapped */
    521 		if (!gpio_pin_can_map(sc, attach->ga_offset, attach->ga_mask))
    522 			return EBUSY;
    523 
    524 		ga.ga_gpio = sc;
    525 		ga.ga_dvname = attach->ga_dvname;
    526 		ga.ga_offset = attach->ga_offset;
    527 		ga.ga_mask = attach->ga_mask;
    528 		DPRINTF(("%s: attach %s with offset %d and mask 0x%02x\n",
    529 		    device_xname(sc->sc_dev), ga.ga_dvname, ga.ga_offset,
    530 		    ga.ga_mask));
    531 
    532 		locs[GPIOCF_OFFSET] = ga.ga_offset;
    533 		locs[GPIOCF_MASK] = ga.ga_mask;
    534 
    535 		cf = config_search_loc(NULL, sc->sc_dev, "gpio", locs, &ga);
    536 		if (cf != NULL) {
    537 			dv = config_attach_loc(sc->sc_dev, cf, locs, &ga,
    538 			    gpiobus_print);
    539 			if (dv != NULL) {
    540 				gdev = kmem_alloc(sizeof(struct gpio_dev),
    541 				    KM_SLEEP);
    542 				gdev->sc_dev = dv;
    543 				LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
    544 			} else
    545 				return EINVAL;
    546 		} else
    547 			return EINVAL;
    548 		break;
    549 	case GPIODETACH:
    550 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    551 		    NULL, NULL, NULL, NULL))
    552 			return EPERM;
    553 
    554 		attach = (struct gpio_attach *)data;
    555 		LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
    556 			if (strcmp(device_xname(gdev->sc_dev),
    557 			    attach->ga_dvname) == 0) {
    558 				if (config_detach(gdev->sc_dev, 0) == 0) {
    559 					LIST_REMOVE(gdev, sc_next);
    560 					kmem_free(gdev,
    561 					    sizeof(struct gpio_dev));
    562 					return 0;
    563 				}
    564 				break;
    565 			}
    566 		}
    567 		return EINVAL;
    568 		break;
    569 	case GPIOSET:
    570 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    571 		    NULL, NULL, NULL, NULL))
    572 			return EPERM;
    573 
    574 		set = (struct gpio_set *)data;
    575 
    576 		if (set->gp_name[0] != '\0') {
    577 			pin = gpio_pinbyname(sc, set->gp_name);
    578 			if (pin == -1)
    579 				return EINVAL;
    580 		} else
    581 			pin = set->gp_pin;
    582 		if (pin < 0 || pin >= sc->sc_npins)
    583 			return EINVAL;
    584 		flags = set->gp_flags;
    585 
    586 		/* check that the controller supports all requested flags */
    587 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
    588 			return ENODEV;
    589 		flags = set->gp_flags | GPIO_PIN_SET;
    590 
    591 		set->gp_caps = sc->sc_pins[pin].pin_caps;
    592 		/* return old value */
    593 		set->gp_flags = sc->sc_pins[pin].pin_flags;
    594 		if (flags > 0) {
    595 			gpiobus_pin_ctl(gc, pin, flags);
    596 			/* update current value */
    597 			sc->sc_pins[pin].pin_flags = flags;
    598 		}
    599 
    600 		/* rename pin or new pin? */
    601 		if (set->gp_name2[0] != '\0') {
    602 			struct gpio_name *gnm;
    603 
    604 			gnm = NULL;
    605 			LIST_FOREACH(nm, &sc->sc_names, gp_next) {
    606 				if (!strcmp(nm->gp_name, set->gp_name2) &&
    607 				    nm->gp_pin != pin)
    608 					return EINVAL;	/* duplicate name */
    609 				if (nm->gp_pin == pin)
    610 					gnm = nm;
    611 			}
    612 			if (gnm != NULL)
    613 				strlcpy(gnm->gp_name, set->gp_name2,
    614 				    sizeof(gnm->gp_name));
    615 			else  {
    616 				nm = kmem_alloc(sizeof(struct gpio_name),
    617 				    KM_SLEEP);
    618 				strlcpy(nm->gp_name, set->gp_name2,
    619 				    sizeof(nm->gp_name));
    620 				nm->gp_pin = set->gp_pin;
    621 				LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
    622 			}
    623 		}
    624 		break;
    625 	case GPIOUNSET:
    626 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    627 		    NULL, NULL, NULL, NULL))
    628 			return EPERM;
    629 
    630 		set = (struct gpio_set *)data;
    631 		if (set->gp_name[0] != '\0') {
    632 			pin = gpio_pinbyname(sc, set->gp_name);
    633 			if (pin == -1)
    634 				return EINVAL;
    635 		} else
    636 			pin = set->gp_pin;
    637 
    638 		if (pin < 0 || pin >= sc->sc_npins)
    639 			return EINVAL;
    640 		if (sc->sc_pins[pin].pin_mapped)
    641 			return EBUSY;
    642 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
    643 			return EINVAL;
    644 
    645 		LIST_FOREACH(nm, &sc->sc_names, gp_next) {
    646 			if (nm->gp_pin == pin) {
    647 				LIST_REMOVE(nm, gp_next);
    648 				kmem_free(nm, sizeof(struct gpio_name));
    649 				break;
    650 			}
    651 		}
    652 		sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
    653 		break;
    654 	default:
    655 		/* Try the old API */
    656 		DPRINTF(("%s: trying the old API\n", device_xname(sc->sc_dev)));
    657 		return gpio_ioctl_oapi(sc, cmd, data, flag, cred);
    658 	}
    659 	return 0;
    660 }
    661 
    662 int
    663 gpio_ioctl_oapi(struct gpio_softc *sc, u_long cmd, void *data, int flag,
    664     kauth_cred_t cred)
    665 {
    666 	gpio_chipset_tag_t gc;
    667 	struct gpio_pin_op *op;
    668 	struct gpio_pin_ctl *ctl;
    669 	int pin, value, flags;
    670 
    671 	gc = sc->sc_gc;
    672 
    673 	switch (cmd) {
    674 	case GPIOPINREAD:
    675 		op = (struct gpio_pin_op *)data;
    676 
    677 		pin = op->gp_pin;
    678 
    679 		if (pin < 0 || pin >= sc->sc_npins)
    680 			return EINVAL;
    681 
    682 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    683 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    684 		    NULL, NULL, NULL, NULL))
    685 			return EPERM;
    686 
    687 		/* return read value */
    688 		op->gp_value = gpiobus_pin_read(gc, pin);
    689 		break;
    690 	case GPIOPINWRITE:
    691 		if ((flag & FWRITE) == 0)
    692 			return EBADF;
    693 
    694 		op = (struct gpio_pin_op *)data;
    695 
    696 		pin = op->gp_pin;
    697 
    698 		if (pin < 0 || pin >= sc->sc_npins)
    699 			return EINVAL;
    700 
    701 		if (sc->sc_pins[pin].pin_mapped)
    702 			return EBUSY;
    703 
    704 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    705 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    706 		    NULL, NULL, NULL, NULL))
    707 			return EPERM;
    708 
    709 		value = op->gp_value;
    710 		if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
    711 			return EINVAL;
    712 
    713 		gpiobus_pin_write(gc, pin, value);
    714 		/* return old value */
    715 		op->gp_value = sc->sc_pins[pin].pin_state;
    716 		/* update current value */
    717 		sc->sc_pins[pin].pin_state = value;
    718 		break;
    719 	case GPIOPINTOGGLE:
    720 		if ((flag & FWRITE) == 0)
    721 			return EBADF;
    722 
    723 		op = (struct gpio_pin_op *)data;
    724 
    725 		pin = op->gp_pin;
    726 
    727 		if (pin < 0 || pin >= sc->sc_npins)
    728 			return EINVAL;
    729 
    730 		if (sc->sc_pins[pin].pin_mapped)
    731 			return EBUSY;
    732 
    733 		if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
    734 		    kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    735 		    NULL, NULL, NULL, NULL))
    736 			return EPERM;
    737 
    738 		value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
    739 		    GPIO_PIN_HIGH : GPIO_PIN_LOW);
    740 		gpiobus_pin_write(gc, pin, value);
    741 		/* return old value */
    742 		op->gp_value = sc->sc_pins[pin].pin_state;
    743 		/* update current value */
    744 		sc->sc_pins[pin].pin_state = value;
    745 		break;
    746 	case GPIOPINCTL:
    747 		ctl = (struct gpio_pin_ctl *) data;
    748 
    749 		if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
    750 		    NULL, NULL, NULL, NULL))
    751 			return EPERM;
    752 
    753 		pin = ctl->gp_pin;
    754 
    755 		if (pin < 0 || pin >= sc->sc_npins)
    756 			return EINVAL;
    757 		if (sc->sc_pins[pin].pin_mapped)
    758 			return EBUSY;
    759 		flags = ctl->gp_flags;
    760 
    761 		/* check that the controller supports all requested flags */
    762 		if ((flags & sc->sc_pins[pin].pin_caps) != flags)
    763 			return ENODEV;
    764 
    765 		ctl->gp_caps = sc->sc_pins[pin].pin_caps;
    766 		/* return old value */
    767 		ctl->gp_flags = sc->sc_pins[pin].pin_flags;
    768 		if (flags > 0) {
    769 			gpiobus_pin_ctl(gc, pin, flags);
    770 			/* update current value */
    771 			sc->sc_pins[pin].pin_flags = flags;
    772 		}
    773 		break;
    774 	default:
    775 		return ENOTTY;
    776 	}
    777 	return 0;
    778 }
    779