diff --git a/sys/arm/allwinner/a10_gpio.c b/sys/arm/allwinner/a10_gpio.c index a5321062cc78..23bf399d3dc3 100644 --- a/sys/arm/allwinner/a10_gpio.c +++ b/sys/arm/allwinner/a10_gpio.c @@ -302,20 +302,6 @@ a10_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->sc_gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->sc_gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together. */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - - /* Can't mix pull-up/pull-down together. */ - if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) == - (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) - return (EINVAL); - a10_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags); return (0); diff --git a/sys/arm/broadcom/bcm2835/bcm2835_gpio.c b/sys/arm/broadcom/bcm2835/bcm2835_gpio.c index 41f27a71c490..0ade78fd53ab 100644 --- a/sys/arm/broadcom/bcm2835/bcm2835_gpio.c +++ b/sys/arm/broadcom/bcm2835/bcm2835_gpio.c @@ -397,20 +397,6 @@ bcm_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (bcm_gpio_pin_is_ro(sc, pin)) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->sc_gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together. */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - - /* Can't mix pull-up/pull-down together. */ - if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) == - (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) - return (EINVAL); - bcm_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags); return (0); diff --git a/sys/arm/freescale/imx/imx_gpio.c b/sys/arm/freescale/imx/imx_gpio.c index b36548943210..c23f75be3f7c 100644 --- a/sys/arm/freescale/imx/imx_gpio.c +++ b/sys/arm/freescale/imx/imx_gpio.c @@ -268,18 +268,8 @@ imx51_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - imx51_gpio_pin_configure(sc, &sc->gpio_pins[i], flags); - return (0); } diff --git a/sys/arm/freescale/vybrid/vf_gpio.c b/sys/arm/freescale/vybrid/vf_gpio.c index b83a877c56b9..a31ff782aacf 100644 --- a/sys/arm/freescale/vybrid/vf_gpio.c +++ b/sys/arm/freescale/vybrid/vf_gpio.c @@ -312,15 +312,6 @@ vf_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - vf_gpio_pin_configure(sc, &sc->gpio_pins[i], flags); return (0); diff --git a/sys/arm/rockchip/rk30xx_gpio.c b/sys/arm/rockchip/rk30xx_gpio.c index fc24947c6562..5c7dc992be0e 100644 --- a/sys/arm/rockchip/rk30xx_gpio.c +++ b/sys/arm/rockchip/rk30xx_gpio.c @@ -318,20 +318,6 @@ rk30_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->sc_gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->sc_gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together. */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - - /* Can't mix pull-up/pull-down together. */ - if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) == - (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) - return (EINVAL); - rk30_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags); return (0); diff --git a/sys/arm/samsung/exynos/exynos5_pad.c b/sys/arm/samsung/exynos/exynos5_pad.c index d00eddca5589..c28ebb1c68e3 100644 --- a/sys/arm/samsung/exynos/exynos5_pad.c +++ b/sys/arm/samsung/exynos/exynos5_pad.c @@ -764,15 +764,6 @@ pad_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - pad_pin_configure(sc, &sc->gpio_pins[i], flags); return (0); diff --git a/sys/arm/ti/ti_gpio.c b/sys/arm/ti/ti_gpio.c index eb0c880214d3..612ceb9a947a 100644 --- a/sys/arm/ti/ti_gpio.c +++ b/sys/arm/ti/ti_gpio.c @@ -462,16 +462,6 @@ ti_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) uint32_t mask = (1UL << (pin % PINS_PER_BANK)); uint32_t reg_val; - /* Sanity check the flags supplied are valid, i.e. not input and output */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == 0x0000) - return (EINVAL); - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) == - (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) - return (EINVAL); - TI_GPIO_LOCK(sc); /* Sanity check the pin number is valid */ diff --git a/sys/arm/xscale/ixp425/avila_gpio.c b/sys/arm/xscale/ixp425/avila_gpio.c index 48f970d7f4bd..4a2d1e94ff4c 100644 --- a/sys/arm/xscale/ixp425/avila_gpio.c +++ b/sys/arm/xscale/ixp425/avila_gpio.c @@ -220,16 +220,8 @@ avila_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask)) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->sc_pins[pin].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - avila_gpio_pin_configure(sc, &sc->sc_pins[pin], flags); + return (0); } diff --git a/sys/arm/xscale/ixp425/cambria_gpio.c b/sys/arm/xscale/ixp425/cambria_gpio.c index baa34838f842..7a773db65636 100644 --- a/sys/arm/xscale/ixp425/cambria_gpio.c +++ b/sys/arm/xscale/ixp425/cambria_gpio.c @@ -317,15 +317,6 @@ cambria_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (pin >= GPIO_PINS) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->sc_pins[pin].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - GPIO_LOCK(sc); sc->sc_pins[pin].gp_flags = flags; diff --git a/sys/dev/gpio/gpiobus.c b/sys/dev/gpio/gpiobus.c index dcf6049c49ed..efff2c0b5f59 100644 --- a/sys/dev/gpio/gpiobus.c +++ b/sys/dev/gpio/gpiobus.c @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -70,6 +71,23 @@ static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); static int gpiobus_pin_toggle(device_t, device_t, uint32_t); +int +gpio_check_flags(uint32_t caps, uint32_t flags) +{ + + /* Check for unwanted flags. */ + if ((flags & caps) == 0 || (flags & caps) != flags) + return (EINVAL); + /* Cannot mix input/output together. */ + if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT) + return (EINVAL); + /* Cannot mix pull-up/pull-down together. */ + if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN) + return (EINVAL); + + return (0); +} + static void gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen) { @@ -490,11 +508,16 @@ gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); + uint32_t caps; if (pin >= devi->npins) return (EINVAL); + if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) + return (EINVAL); + if (gpio_check_flags(caps, flags) != 0) + return (EINVAL); - return GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags); + return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); } static int diff --git a/sys/dev/gpio/gpiobusvar.h b/sys/dev/gpio/gpiobusvar.h index 3ec8a178c516..a1c2be0461ba 100644 --- a/sys/dev/gpio/gpiobusvar.h +++ b/sys/dev/gpio/gpiobusvar.h @@ -94,6 +94,7 @@ gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells, device_t ofw_gpiobus_add_fdt_child(device_t, phandle_t); #endif +int gpio_check_flags(uint32_t, uint32_t); int gpiobus_init_softc(device_t); extern driver_t gpiobus_driver; diff --git a/sys/dev/gpio/gpioc.c b/sys/dev/gpio/gpioc.c index 156f05c2b196..2fad4dffb286 100644 --- a/sys/dev/gpio/gpioc.c +++ b/sys/dev/gpio/gpioc.c @@ -29,19 +29,16 @@ __FBSDID("$FreeBSD$"); #include #include -#include - #include #include +#include #include #include #include #include -#include -#include -#include -#include +#include + #include "gpio_if.h" #undef GPIOC_DEBUG @@ -119,6 +116,7 @@ gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, struct gpioc_softc *sc = cdev->si_drv1; struct gpio_pin pin; struct gpio_req req; + uint32_t caps; switch (cmd) { case GPIOMAXPIN: @@ -141,8 +139,12 @@ gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, case GPIOSETCONFIG: bcopy(arg, &pin, sizeof(pin)); dprintf("set config pin %d\n", pin.gp_pin); - res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin, - pin.gp_flags); + res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps); + if (res == 0) + res = gpio_check_flags(caps, pin.gp_flags); + if (res == 0) + res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin, + pin.gp_flags); break; case GPIOGET: bcopy(arg, &req, sizeof(req)); diff --git a/sys/mips/atheros/ar71xx_gpio.c b/sys/mips/atheros/ar71xx_gpio.c index 0e1633229cb2..45ac8ffda2ed 100644 --- a/sys/mips/atheros/ar71xx_gpio.c +++ b/sys/mips/atheros/ar71xx_gpio.c @@ -241,16 +241,8 @@ ar71xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - ar71xx_gpio_pin_configure(sc, &sc->gpio_pins[i], flags); + return (0); } diff --git a/sys/mips/cavium/octeon_gpio.c b/sys/mips/cavium/octeon_gpio.c index 26b42eaca87c..36868481a996 100644 --- a/sys/mips/cavium/octeon_gpio.c +++ b/sys/mips/cavium/octeon_gpio.c @@ -219,16 +219,8 @@ octeon_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - octeon_gpio_pin_configure(sc, &sc->gpio_pins[i], flags); + return (0); } diff --git a/sys/mips/rt305x/rt305x_gpio.c b/sys/mips/rt305x/rt305x_gpio.c index 5f7defbcc4e5..a5d72549bd03 100644 --- a/sys/mips/rt305x/rt305x_gpio.c +++ b/sys/mips/rt305x/rt305x_gpio.c @@ -242,18 +242,8 @@ rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (i >= sc->gpio_npins) return (EINVAL); - /* Check for unwanted flags. */ - if ((flags & sc->gpio_pins[i].gp_caps) != flags) - return (EINVAL); - - /* Can't mix input/output together */ - if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == - (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) - return (EINVAL); - rt305x_gpio_pin_configure(sc, &sc->gpio_pins[i], flags); - return (0); } diff --git a/sys/powerpc/wii/wii_gpio.c b/sys/powerpc/wii/wii_gpio.c index b9ecccb39809..e6d76b8c2ab1 100644 --- a/sys/powerpc/wii/wii_gpio.c +++ b/sys/powerpc/wii/wii_gpio.c @@ -295,11 +295,6 @@ wiigpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) if (pin >= WIIGPIO_NPINS) return (EINVAL); - if ((flags & ~(GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) != 0) - return (EINVAL); - if ((flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) == - (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) - return (EINVAL); sc = device_get_softc(dev); pinbank = WIIGPIO_PINBANK(pin); pinmask = WIIGPIO_PINMASK(pin);