Index: head/sys/mips/mediatek/mtk_gpio_v1.c =================================================================== --- head/sys/mips/mediatek/mtk_gpio_v1.c +++ head/sys/mips/mediatek/mtk_gpio_v1.c @@ -431,19 +431,29 @@ static int mtk_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) { - struct mtk_gpio_softc *sc = device_get_softc(dev); + struct mtk_gpio_softc *sc; + int ret; + + sc = device_get_softc(dev); + ret = 0; - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) + if (pin >= sc->num_pins) return (EINVAL); MTK_GPIO_LOCK(sc); + if(!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { + ret = EINVAL; + goto out; + } + if (value) MTK_WRITE_4(sc, GPIO_PIOSET(sc), (1u << pin)); else MTK_WRITE_4(sc, GPIO_PIORESET(sc), (1u << pin)); - MTK_GPIO_UNLOCK(sc); - return (0); +out: + MTK_GPIO_UNLOCK(sc); + return (ret); } static int @@ -451,33 +461,50 @@ { struct mtk_gpio_softc *sc; uint32_t data; + int ret; sc = device_get_softc(dev); + ret = 0; - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) + if (pin >= sc->num_pins) return (EINVAL); MTK_GPIO_LOCK(sc); + if(!(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) { + ret = EINVAL; + goto out; + } data = MTK_READ_4(sc, GPIO_PIODATA(sc)); - MTK_GPIO_UNLOCK(sc); *val = (data & (1u << pin)) ? 1 : 0; - return (0); +out: + MTK_GPIO_UNLOCK(sc); + return (ret); } static int mtk_gpio_pin_toggle(device_t dev, uint32_t pin) { - struct mtk_gpio_softc *sc = device_get_softc(dev); + struct mtk_gpio_softc *sc; + int ret; - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) + if (pin >= sc->num_pins) return (EINVAL); + sc = device_get_softc(dev); + ret = 0; + MTK_GPIO_LOCK(sc); + if (!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { + ret = EINVAL; + goto out; + } MTK_WRITE_4(sc, GPIO_PIOTOG(sc), (1u << pin)); + +out: MTK_GPIO_UNLOCK(sc); - return (0); + return (ret); } static int @@ -571,7 +598,9 @@ pisrc = (struct mtk_gpio_pin_irqsrc *)isrc; sc = device_get_softc(dev); + MTK_GPIO_LOCK(sc); MTK_WRITE_4(sc, GPIO_PIOINT(sc), 1u << pisrc->irq); + MTK_GPIO_UNLOCK(sc); } static int Index: head/sys/mips/mediatek/mtk_gpio_v2.c =================================================================== --- head/sys/mips/mediatek/mtk_gpio_v2.c +++ head/sys/mips/mediatek/mtk_gpio_v2.c @@ -425,19 +425,29 @@ static int mtk_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) { - struct mtk_gpio_softc *sc = device_get_softc(dev); + struct mtk_gpio_softc *sc; + int ret; - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) + if (pin >= sc->num_pins) return (EINVAL); + sc = device_get_softc(dev); + ret = 0; + MTK_GPIO_LOCK(sc); + if (!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { + ret = EINVAL; + goto out; + } if (value) MTK_WRITE_4(sc, GPIO_PIOSET(sc), (1u << pin)); else MTK_WRITE_4(sc, GPIO_PIORESET(sc), (1u << pin)); + +out: MTK_GPIO_UNLOCK(sc); - return (0); + return (ret); } static int @@ -445,39 +455,56 @@ { struct mtk_gpio_softc *sc; uint32_t data; + int ret; - sc = device_get_softc(dev); - - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) + if (pin >= sc->num_pins) return (EINVAL); + sc = device_get_softc(dev); + ret = 0; + MTK_GPIO_LOCK(sc); + if (!(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) { + ret = EINVAL; + goto out; + } data = MTK_READ_4(sc, GPIO_PIODATA(sc)); - MTK_GPIO_UNLOCK(sc); *val = (data & (1u << pin)) ? 1 : 0; - return (0); +out: + MTK_GPIO_UNLOCK(sc); + return (ret); } static int mtk_gpio_pin_toggle(device_t dev, uint32_t pin) { - struct mtk_gpio_softc *sc = device_get_softc(dev); + struct mtk_gpio_softc *sc; uint32_t val; + int ret; - if (pin >= sc->num_pins || !(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) + if (pin >= sc->num_pins) return (EINVAL); + sc = device_get_softc(dev); + ret = 0; + MTK_GPIO_LOCK(sc); + if(!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { + ret = EINVAL; + goto out; + } val = MTK_READ_4(sc, GPIO_PIODATA(sc)); val &= (1u << pin); if (val) MTK_WRITE_4(sc, GPIO_PIORESET(sc), (1u << pin)); else MTK_WRITE_4(sc, GPIO_PIOSET(sc), (1u << pin)); + +out: MTK_GPIO_UNLOCK(sc); - return (0); + return (ret); } static int @@ -571,7 +598,9 @@ pisrc = (struct mtk_gpio_pin_irqsrc *)isrc; sc = device_get_softc(dev); + MTK_GPIO_LOCK(sc); MTK_WRITE_4(sc, GPIO_PIOINT(sc), 1u << pisrc->irq); + MTK_GPIO_UNLOCK(sc); } static int