Index: stable/12/sys/dev/gpio/gpiobus.c =================================================================== --- stable/12/sys/dev/gpio/gpiobus.c (revision 368701) +++ stable/12/sys/dev/gpio/gpiobus.c (revision 368702) @@ -1,1133 +1,1142 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #ifdef INTRNG #include #endif #include #include #include #include #include "gpiobus_if.h" #undef GPIOBUS_DEBUG #ifdef GPIOBUS_DEBUG #define dprintf printf #else #define dprintf(x, arg...) #endif static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t); static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); static int gpiobus_probe(device_t); static int gpiobus_attach(device_t); static int gpiobus_detach(device_t); static int gpiobus_suspend(device_t); static int gpiobus_resume(device_t); static void gpiobus_probe_nomatch(device_t, device_t); static int gpiobus_print_child(device_t, device_t); static int gpiobus_child_location_str(device_t, device_t, char *, size_t); static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); static device_t gpiobus_add_child(device_t, u_int, const char *, int); static void gpiobus_hinted_child(device_t, const char *, int); /* * GPIOBUS interface */ static int gpiobus_acquire_bus(device_t, device_t, int); static void gpiobus_release_bus(device_t, device_t); static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 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); /* * gpiobus_pin flags * The flags in struct gpiobus_pin are not related to the flags used by the * low-level controller driver in struct gpio_pin. Currently, only pins * acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in * the FDT properties. In theory, these flags are defined per-platform. In * practice they are always the flags from the dt-bindings/gpio/gpio.h file. * The only one of those flags we currently support is for handling active-low * pins, so we just define that flag here instead of including a GPL'd header. */ #define GPIO_ACTIVE_LOW 1 /* * XXX -> Move me to better place - gpio_subr.c? * Also, this function must be changed when interrupt configuration * data will be moved into struct resource. */ #ifdef INTRNG struct resource * gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, gpio_pin_t pin, uint32_t intr_mode) { u_int irq; struct intr_map_data_gpio *gpio_data; struct resource *res; gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data( INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO); gpio_data->gpio_pin_num = pin->pin; gpio_data->gpio_pin_flags = pin->flags; gpio_data->gpio_intr_mode = intr_mode; irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data); res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1, alloc_flags); if (res == NULL) { intr_free_intr_map_data((struct intr_map_data *)gpio_data); return (NULL); } rman_set_virtual(res, gpio_data); return (res); } #else struct resource * gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, gpio_pin_t pin, uint32_t intr_mode) { return (NULL); } #endif int gpio_check_flags(uint32_t caps, uint32_t flags) { /* Filter unwanted flags. */ flags &= caps; /* 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); + /* Cannot mix output and interrupt flags together */ + if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK) + return (EINVAL); + /* Only one interrupt flag can be defined at once */ + if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1)) + return (EINVAL); + /* The interrupt attached flag cannot be set */ + if (flags & GPIO_INTR_ATTACHED) + return (EINVAL); return (0); } int gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin) { gpio_pin_t pin; int err; err = gpiobus_acquire_pin(busdev, pinnum); if (err != 0) return (EBUSY); pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO); pin->dev = device_get_parent(busdev); pin->pin = pinnum; pin->flags = 0; *ppin = pin; return (0); } int gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin) { struct gpiobus_ivar *devi; devi = GPIOBUS_IVAR(childdev); if (idx >= devi->npins) return (EINVAL); return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev), devi->pins[idx], ppin)); } int gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps) { KASSERT(pin != NULL, ("GPIO pin is NULL.")); KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps)); } int gpio_pin_is_active(gpio_pin_t pin, bool *active) { int rv; uint32_t tmp; KASSERT(pin != NULL, ("GPIO pin is NULL.")); KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp); if (rv != 0) { return (rv); } if (pin->flags & GPIO_ACTIVE_LOW) *active = tmp == 0; else *active = tmp != 0; return (0); } void gpio_pin_release(gpio_pin_t gpio) { device_t busdev; if (gpio == NULL) return; KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL.")); busdev = GPIO_GET_BUS(gpio->dev); if (busdev != NULL) gpiobus_release_pin(busdev, gpio->pin); free(gpio, M_DEVBUF); } int gpio_pin_set_active(gpio_pin_t pin, bool active) { int rv; uint32_t tmp; if (pin->flags & GPIO_ACTIVE_LOW) tmp = active ? 0 : 1; else tmp = active ? 1 : 0; KASSERT(pin != NULL, ("GPIO pin is NULL.")); KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp); return (rv); } int gpio_pin_setflags(gpio_pin_t pin, uint32_t flags) { int rv; KASSERT(pin != NULL, ("GPIO pin is NULL.")); KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); return (rv); } static void gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen) { char tmp[128]; int i, range_start, range_stop, need_coma; if (devi->npins == 0) return; need_coma = 0; range_start = range_stop = devi->pins[0]; for (i = 1; i < devi->npins; i++) { if (devi->pins[i] != (range_stop + 1)) { if (need_coma) strlcat(buf, ",", buflen); memset(tmp, 0, sizeof(tmp)); if (range_start != range_stop) snprintf(tmp, sizeof(tmp) - 1, "%d-%d", range_start, range_stop); else snprintf(tmp, sizeof(tmp) - 1, "%d", range_start); strlcat(buf, tmp, buflen); range_start = range_stop = devi->pins[i]; need_coma = 1; } else range_stop++; } if (need_coma) strlcat(buf, ",", buflen); memset(tmp, 0, sizeof(tmp)); if (range_start != range_stop) snprintf(tmp, sizeof(tmp) - 1, "%d-%d", range_start, range_stop); else snprintf(tmp, sizeof(tmp) - 1, "%d", range_start); strlcat(buf, tmp, buflen); } device_t gpiobus_attach_bus(device_t dev) { device_t busdev; busdev = device_add_child(dev, "gpiobus", -1); if (busdev == NULL) return (NULL); if (device_add_child(dev, "gpioc", -1) == NULL) { device_delete_child(dev, busdev); return (NULL); } #ifdef FDT ofw_gpiobus_register_provider(dev); #endif bus_generic_attach(dev); return (busdev); } int gpiobus_detach_bus(device_t dev) { int err; #ifdef FDT ofw_gpiobus_unregister_provider(dev); #endif err = bus_generic_detach(dev); if (err != 0) return (err); return (device_delete_children(dev)); } int gpiobus_init_softc(device_t dev) { struct gpiobus_softc *sc; sc = GPIOBUS_SOFTC(dev); sc->sc_busdev = dev; sc->sc_dev = device_get_parent(dev); sc->sc_intr_rman.rm_type = RMAN_ARRAY; sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; if (rman_init(&sc->sc_intr_rman) != 0 || rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) panic("%s: failed to set up rman.", __func__); if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) return (ENXIO); KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins")); /* Pins = GPIO_PIN_MAX() + 1 */ sc->sc_npins++; sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, M_NOWAIT | M_ZERO); if (sc->sc_pins == NULL) return (ENOMEM); /* Initialize the bus lock. */ GPIOBUS_LOCK_INIT(sc); return (0); } int gpiobus_alloc_ivars(struct gpiobus_ivar *devi) { /* Allocate pins and flags memory. */ devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, M_NOWAIT | M_ZERO); if (devi->pins == NULL) return (ENOMEM); return (0); } void gpiobus_free_ivars(struct gpiobus_ivar *devi) { if (devi->pins) { free(devi->pins, M_DEVBUF); devi->pins = NULL; } devi->npins = 0; } int gpiobus_acquire_pin(device_t bus, uint32_t pin) { struct gpiobus_softc *sc; sc = device_get_softc(bus); /* Consistency check. */ if (pin >= sc->sc_npins) { device_printf(bus, "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); return (-1); } /* Mark pin as mapped and give warning if it's already mapped. */ if (sc->sc_pins[pin].mapped) { device_printf(bus, "warning: pin %d is already mapped\n", pin); return (-1); } sc->sc_pins[pin].mapped = 1; return (0); } /* Release mapped pin */ int gpiobus_release_pin(device_t bus, uint32_t pin) { struct gpiobus_softc *sc; sc = device_get_softc(bus); /* Consistency check. */ if (pin >= sc->sc_npins) { device_printf(bus, "invalid pin %d, max=%d\n", pin, sc->sc_npins - 1); return (-1); } if (!sc->sc_pins[pin].mapped) { device_printf(bus, "pin %d is not mapped\n", pin); return (-1); } sc->sc_pins[pin].mapped = 0; return (0); } static int gpiobus_acquire_child_pins(device_t dev, device_t child) { struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); int i; for (i = 0; i < devi->npins; i++) { /* Reserve the GPIO pin. */ if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) { device_printf(child, "cannot acquire pin %d\n", devi->pins[i]); while (--i >= 0) { (void)gpiobus_release_pin(dev, devi->pins[i]); } gpiobus_free_ivars(devi); return (EBUSY); } } for (i = 0; i < devi->npins; i++) { /* Use the child name as pin name. */ GPIOBUS_PIN_SETNAME(dev, devi->pins[i], device_get_nameunit(child)); } return (0); } static int gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) { struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); int i, npins; npins = 0; for (i = 0; i < 32; i++) { if (mask & (1 << i)) npins++; } if (npins == 0) { device_printf(child, "empty pin mask\n"); return (EINVAL); } devi->npins = npins; if (gpiobus_alloc_ivars(devi) != 0) { device_printf(child, "cannot allocate device ivars\n"); return (EINVAL); } npins = 0; for (i = 0; i < 32; i++) { if ((mask & (1 << i)) == 0) continue; devi->pins[npins++] = i; } return (0); } static int gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child, const char *pins) { struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); const char *p; char *endp; unsigned long pin; int i, npins; npins = 0; p = pins; for (;;) { pin = strtoul(p, &endp, 0); if (endp == p) break; npins++; if (*endp == '\0') break; p = endp + 1; } if (*endp != '\0') { device_printf(child, "garbage in the pin list: %s\n", endp); return (EINVAL); } if (npins == 0) { device_printf(child, "empty pin list\n"); return (EINVAL); } devi->npins = npins; if (gpiobus_alloc_ivars(devi) != 0) { device_printf(child, "cannot allocate device ivars\n"); return (EINVAL); } i = 0; p = pins; for (;;) { pin = strtoul(p, &endp, 0); devi->pins[i] = pin; if (*endp == '\0') break; i++; p = endp + 1; } return (0); } static int gpiobus_probe(device_t dev) { device_set_desc(dev, "GPIO bus"); return (BUS_PROBE_GENERIC); } static int gpiobus_attach(device_t dev) { int err; err = gpiobus_init_softc(dev); if (err != 0) return (err); /* * Get parent's pins and mark them as unmapped */ bus_generic_probe(dev); bus_enumerate_hinted_children(dev); return (bus_generic_attach(dev)); } /* * Since this is not a self-enumerating bus, and since we always add * children in attach, we have to always delete children here. */ static int gpiobus_detach(device_t dev) { struct gpiobus_softc *sc; struct gpiobus_ivar *devi; device_t *devlist; int i, err, ndevs; sc = GPIOBUS_SOFTC(dev); KASSERT(mtx_initialized(&sc->sc_mtx), ("gpiobus mutex not initialized")); GPIOBUS_LOCK_DESTROY(sc); if ((err = bus_generic_detach(dev)) != 0) return (err); if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) return (err); for (i = 0; i < ndevs; i++) { devi = GPIOBUS_IVAR(devlist[i]); gpiobus_free_ivars(devi); resource_list_free(&devi->rl); free(devi, M_DEVBUF); device_delete_child(dev, devlist[i]); } free(devlist, M_TEMP); rman_fini(&sc->sc_intr_rman); if (sc->sc_pins) { for (i = 0; i < sc->sc_npins; i++) { if (sc->sc_pins[i].name != NULL) free(sc->sc_pins[i].name, M_DEVBUF); sc->sc_pins[i].name = NULL; } free(sc->sc_pins, M_DEVBUF); sc->sc_pins = NULL; } return (0); } static int gpiobus_suspend(device_t dev) { return (bus_generic_suspend(dev)); } static int gpiobus_resume(device_t dev) { return (bus_generic_resume(dev)); } static void gpiobus_probe_nomatch(device_t dev, device_t child) { char pins[128]; struct gpiobus_ivar *devi; devi = GPIOBUS_IVAR(child); memset(pins, 0, sizeof(pins)); gpiobus_print_pins(devi, pins, sizeof(pins)); if (devi->npins > 1) device_printf(dev, " at pins %s", pins); else device_printf(dev, " at pin %s", pins); resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); printf("\n"); } static int gpiobus_print_child(device_t dev, device_t child) { char pins[128]; int retval = 0; struct gpiobus_ivar *devi; devi = GPIOBUS_IVAR(child); memset(pins, 0, sizeof(pins)); retval += bus_print_child_header(dev, child); if (devi->npins > 0) { if (devi->npins > 1) retval += printf(" at pins "); else retval += printf(" at pin "); gpiobus_print_pins(devi, pins, sizeof(pins)); retval += printf("%s", pins); } resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); retval += bus_print_child_footer(dev, child); return (retval); } static int gpiobus_child_location_str(device_t bus, device_t child, char *buf, size_t buflen) { struct gpiobus_ivar *devi; devi = GPIOBUS_IVAR(child); if (devi->npins > 1) strlcpy(buf, "pins=", buflen); else strlcpy(buf, "pin=", buflen); gpiobus_print_pins(devi, buf, buflen); return (0); } static int gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, size_t buflen) { *buf = '\0'; return (0); } static device_t gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) { device_t child; struct gpiobus_ivar *devi; child = device_add_child_ordered(dev, order, name, unit); if (child == NULL) return (child); devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); if (devi == NULL) { device_delete_child(dev, child); return (NULL); } resource_list_init(&devi->rl); device_set_ivars(child, devi); return (child); } static int gpiobus_rescan(device_t dev) { /* * Re-scan is supposed to remove and add children, but if someone has * deleted the hints for a child we attached earlier, we have no easy * way to handle that. So this just attaches new children for whom new * hints or drivers have arrived since we last tried. */ bus_enumerate_hinted_children(dev); bus_generic_attach(dev); return (0); } static void gpiobus_hinted_child(device_t bus, const char *dname, int dunit) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); struct gpiobus_ivar *devi; device_t child; const char *pins; int irq, pinmask; if (device_find_child(bus, dname, dunit) != NULL) { return; } child = BUS_ADD_CHILD(bus, 0, dname, dunit); devi = GPIOBUS_IVAR(child); if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) { if (gpiobus_parse_pins(sc, child, pinmask)) { resource_list_free(&devi->rl); free(devi, M_DEVBUF); device_delete_child(bus, child); return; } } else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) { if (gpiobus_parse_pin_list(sc, child, pins)) { resource_list_free(&devi->rl); free(devi, M_DEVBUF); device_delete_child(bus, child); return; } } if (resource_int_value(dname, dunit, "irq", &irq) == 0) { if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) device_printf(bus, "warning: bus_set_resource() failed\n"); } } static int gpiobus_set_resource(device_t dev, device_t child, int type, int rid, rman_res_t start, rman_res_t count) { struct gpiobus_ivar *devi; struct resource_list_entry *rle; dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", __func__, dev, child, type, rid, (void *)(intptr_t)start, count); devi = GPIOBUS_IVAR(child); rle = resource_list_add(&devi->rl, type, rid, start, start + count - 1, count); if (rle == NULL) return (ENXIO); return (0); } static int gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) { struct gpiobus_ivar *devi; devi = GPIOBUS_IVAR(child); switch (which) { case GPIOBUS_IVAR_NPINS: *result = devi->npins; break; case GPIOBUS_IVAR_PINS: /* Children do not ever need to directly examine this. */ return (ENOTSUP); default: return (ENOENT); } return (0); } static int gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) { struct gpiobus_ivar *devi; const uint32_t *ptr; int i; devi = GPIOBUS_IVAR(child); switch (which) { case GPIOBUS_IVAR_NPINS: /* GPIO ivars are set once. */ if (devi->npins != 0) { return (EBUSY); } devi->npins = value; if (gpiobus_alloc_ivars(devi) != 0) { device_printf(child, "cannot allocate device ivars\n"); devi->npins = 0; return (ENOMEM); } break; case GPIOBUS_IVAR_PINS: ptr = (const uint32_t *)value; for (i = 0; i < devi->npins; i++) devi->pins[i] = ptr[i]; if (gpiobus_acquire_child_pins(dev, child) != 0) return (EBUSY); break; default: return (ENOENT); } return (0); } static struct resource * gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) { struct gpiobus_softc *sc; struct resource *rv; struct resource_list *rl; struct resource_list_entry *rle; int isdefault; if (type != SYS_RES_IRQ) return (NULL); isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); rle = NULL; if (isdefault) { rl = BUS_GET_RESOURCE_LIST(bus, child); if (rl == NULL) return (NULL); rle = resource_list_find(rl, type, *rid); if (rle == NULL) return (NULL); if (rle->res != NULL) panic("%s: resource entry is busy", __func__); start = rle->start; count = rle->count; end = rle->end; } sc = device_get_softc(bus); rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, child); if (rv == NULL) return (NULL); rman_set_rid(rv, *rid); if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, *rid, rv) != 0) { rman_release_resource(rv); return (NULL); } return (rv); } static int gpiobus_release_resource(device_t bus __unused, device_t child, int type, int rid, struct resource *r) { int error; if (rman_get_flags(r) & RF_ACTIVE) { error = bus_deactivate_resource(child, type, rid, r); if (error) return (error); } return (rman_release_resource(r)); } static struct resource_list * gpiobus_get_resource_list(device_t bus __unused, device_t child) { struct gpiobus_ivar *ivar; ivar = GPIOBUS_IVAR(child); return (&ivar->rl); } static int gpiobus_acquire_bus(device_t busdev, device_t child, int how) { struct gpiobus_softc *sc; sc = device_get_softc(busdev); GPIOBUS_ASSERT_UNLOCKED(sc); GPIOBUS_LOCK(sc); if (sc->sc_owner != NULL) { if (sc->sc_owner == child) panic("%s: %s still owns the bus.", device_get_nameunit(busdev), device_get_nameunit(child)); if (how == GPIOBUS_DONTWAIT) { GPIOBUS_UNLOCK(sc); return (EWOULDBLOCK); } while (sc->sc_owner != NULL) mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); } sc->sc_owner = child; GPIOBUS_UNLOCK(sc); return (0); } static void gpiobus_release_bus(device_t busdev, device_t child) { struct gpiobus_softc *sc; sc = device_get_softc(busdev); GPIOBUS_ASSERT_UNLOCKED(sc); GPIOBUS_LOCK(sc); if (sc->sc_owner == NULL) panic("%s: %s releasing unowned bus.", device_get_nameunit(busdev), device_get_nameunit(child)); if (sc->sc_owner != child) panic("%s: %s trying to release bus owned by %s", device_get_nameunit(busdev), device_get_nameunit(child), device_get_nameunit(sc->sc_owner)); sc->sc_owner = NULL; wakeup(sc); GPIOBUS_UNLOCK(sc); } static int gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, uint32_t flags) { 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)); } static int gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, uint32_t *flags) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); } static int gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, uint32_t *caps) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); } static int gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, unsigned int value) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); } static int gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, unsigned int *value) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); } static int gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); } static int gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) { struct gpiobus_softc *sc; sc = GPIOBUS_SOFTC(dev); if (pin > sc->sc_npins) return (EINVAL); /* Did we have a name for this pin ? */ if (sc->sc_pins[pin].name != NULL) { memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); return (0); } /* Return the default pin name. */ return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); } static int gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) { struct gpiobus_softc *sc; sc = GPIOBUS_SOFTC(dev); if (pin > sc->sc_npins) return (EINVAL); if (name == NULL) return (EINVAL); /* Save the pin name. */ if (sc->sc_pins[pin].name == NULL) sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, M_WAITOK | M_ZERO); strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); return (0); } static device_method_t gpiobus_methods[] = { /* Device interface */ DEVMETHOD(device_probe, gpiobus_probe), DEVMETHOD(device_attach, gpiobus_attach), DEVMETHOD(device_detach, gpiobus_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, gpiobus_suspend), DEVMETHOD(device_resume, gpiobus_resume), /* Bus interface */ DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), DEVMETHOD(bus_config_intr, bus_generic_config_intr), DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), DEVMETHOD(bus_set_resource, gpiobus_set_resource), DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), DEVMETHOD(bus_release_resource, gpiobus_release_resource), DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), DEVMETHOD(bus_add_child, gpiobus_add_child), DEVMETHOD(bus_rescan, gpiobus_rescan), DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), DEVMETHOD(bus_print_child, gpiobus_print_child), DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), DEVMETHOD(bus_read_ivar, gpiobus_read_ivar), DEVMETHOD(bus_write_ivar, gpiobus_write_ivar), /* GPIO protocol */ DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), DEVMETHOD_END }; driver_t gpiobus_driver = { "gpiobus", gpiobus_methods, sizeof(struct gpiobus_softc) }; devclass_t gpiobus_devclass; EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); MODULE_VERSION(gpiobus, 1); Index: stable/12/sys/dev/gpio/gpioc.c =================================================================== --- stable/12/sys/dev/gpio/gpioc.c (revision 368701) +++ stable/12/sys/dev/gpio/gpioc.c (revision 368702) @@ -1,231 +1,1065 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include +#include +#include +#include +#include #include #include +#include +#include +#include #include #include #include "gpio_if.h" #include "gpiobus_if.h" #undef GPIOC_DEBUG #ifdef GPIOC_DEBUG #define dprintf printf +#define ddevice_printf device_printf #else #define dprintf(x, arg...) +#define ddevice_printf(dev, x, arg...) #endif -static int gpioc_probe(device_t dev); -static int gpioc_attach(device_t dev); -static int gpioc_detach(device_t dev); +struct gpioc_softc { + device_t sc_dev; /* gpiocX dev */ + device_t sc_pdev; /* gpioX dev */ + struct cdev *sc_ctl_dev; /* controller device */ + int sc_unit; + int sc_npins; + struct gpioc_pin_intr *sc_pin_intr; +}; +struct gpioc_pin_intr { + struct gpioc_softc *sc; + gpio_pin_t pin; + bool config_locked; + int intr_rid; + struct resource *intr_res; + void *intr_cookie; + struct mtx mtx; + SLIST_HEAD(gpioc_privs_list, gpioc_privs) privs; +}; + + +struct gpioc_cdevpriv { + struct gpioc_softc *sc; + struct selinfo selinfo; + bool async; + uint8_t report_option; + struct sigio *sigio; + struct mtx mtx; + struct gpioc_pin_event *events; + int numevents; + int evidx_head; + int evidx_tail; + SLIST_HEAD(gpioc_pins_list, gpioc_pins) pins; +}; + +struct gpioc_privs { + struct gpioc_cdevpriv *priv; + SLIST_ENTRY(gpioc_privs) next; +}; + +struct gpioc_pins { + struct gpioc_pin_intr *pin; + int eventcount; + int firstevent; + SLIST_ENTRY(gpioc_pins) next; +}; + +struct gpioc_pin_event { + struct gpioc_pins *privpin; + sbintime_t event_time; + bool event_pin_state; +}; + +static MALLOC_DEFINE(M_GPIOC, "gpioc", "gpioc device data"); + +static int gpioc_allocate_pin_intr(struct gpioc_pin_intr*, uint32_t); +static int gpioc_release_pin_intr(struct gpioc_pin_intr*); +static int gpioc_attach_priv_pin(struct gpioc_cdevpriv*, + struct gpioc_pin_intr*); +static int gpioc_detach_priv_pin(struct gpioc_cdevpriv*, + struct gpioc_pin_intr*); +static bool gpioc_intr_reconfig_allowed(struct gpioc_cdevpriv*, + struct gpioc_pin_intr *intr_conf); +static uint32_t gpioc_get_intr_config(struct gpioc_softc*, + struct gpioc_cdevpriv*, uint32_t pin); +static int gpioc_set_intr_config(struct gpioc_softc*, + struct gpioc_cdevpriv*, uint32_t, uint32_t); +static void gpioc_interrupt_handler(void*); + +static int gpioc_kqread(struct knote*, long); +static void gpioc_kqdetach(struct knote*); + +static int gpioc_probe(device_t dev); +static int gpioc_attach(device_t dev); +static int gpioc_detach(device_t dev); + +static void gpioc_cdevpriv_dtor(void*); + +static d_open_t gpioc_open; +static d_read_t gpioc_read; static d_ioctl_t gpioc_ioctl; +static d_poll_t gpioc_poll; +static d_kqfilter_t gpioc_kqfilter; static struct cdevsw gpioc_cdevsw = { .d_version = D_VERSION, + .d_open = gpioc_open, + .d_read = gpioc_read, .d_ioctl = gpioc_ioctl, + .d_poll = gpioc_poll, + .d_kqfilter = gpioc_kqfilter, .d_name = "gpioc", }; -struct gpioc_softc { - device_t sc_dev; /* gpiocX dev */ - device_t sc_pdev; /* gpioX dev */ - struct cdev *sc_ctl_dev; /* controller device */ - int sc_unit; +static struct filterops gpioc_read_filterops = { + .f_isfd = true, + .f_attach = NULL, + .f_detach = gpioc_kqdetach, + .f_event = gpioc_kqread, + .f_touch = NULL }; +static struct gpioc_pin_event * +next_head_event(struct gpioc_cdevpriv *priv) +{ + struct gpioc_pin_event *rv; + + rv = &priv->events[priv->evidx_head++]; + if (priv->evidx_head == priv->numevents) + priv->evidx_head = 0; + return (rv); +} + +static struct gpioc_pin_event * +next_tail_event(struct gpioc_cdevpriv *priv) +{ + struct gpioc_pin_event *rv; + + rv = &priv->events[priv->evidx_tail++]; + if (priv->evidx_tail == priv->numevents) + priv->evidx_tail = 0; + return (rv); +} + +static size_t +number_of_events(struct gpioc_cdevpriv *priv) +{ + if (priv->evidx_head >= priv->evidx_tail) + return (priv->evidx_head - priv->evidx_tail); + else + return (priv->numevents + priv->evidx_head - priv->evidx_tail); +} + static int +gpioc_allocate_pin_intr(struct gpioc_pin_intr *intr_conf, uint32_t flags) +{ + int err; + + intr_conf->config_locked = true; + mtx_unlock(&intr_conf->mtx); + + intr_conf->intr_res = gpio_alloc_intr_resource(intr_conf->pin->dev, + &intr_conf->intr_rid, RF_ACTIVE, intr_conf->pin, flags); + if (intr_conf->intr_res == NULL) { + err = ENXIO; + goto error_exit; + } + + err = bus_setup_intr(intr_conf->pin->dev, intr_conf->intr_res, + INTR_TYPE_MISC | INTR_MPSAFE, NULL, gpioc_interrupt_handler, + intr_conf, &intr_conf->intr_cookie); + if (err != 0) + goto error_exit; + + intr_conf->pin->flags = flags; + +error_exit: + mtx_lock(&intr_conf->mtx); + intr_conf->config_locked = false; + wakeup(&intr_conf->config_locked); + + return (err); +} + +static int +gpioc_release_pin_intr(struct gpioc_pin_intr *intr_conf) +{ + int err; + + intr_conf->config_locked = true; + mtx_unlock(&intr_conf->mtx); + + if (intr_conf->intr_cookie != NULL) { + err = bus_teardown_intr(intr_conf->pin->dev, + intr_conf->intr_res, intr_conf->intr_cookie); + if (err != 0) + goto error_exit; + else + intr_conf->intr_cookie = NULL; + } + + if (intr_conf->intr_res != NULL) { + err = bus_release_resource(intr_conf->pin->dev, SYS_RES_IRQ, + intr_conf->intr_rid, intr_conf->intr_res); + if (err != 0) + goto error_exit; + else { + intr_conf->intr_rid = 0; + intr_conf->intr_res = NULL; + } + } + + intr_conf->pin->flags = 0; + err = 0; + +error_exit: + mtx_lock(&intr_conf->mtx); + intr_conf->config_locked = false; + wakeup(&intr_conf->config_locked); + + return (err); +} + +static int +gpioc_attach_priv_pin(struct gpioc_cdevpriv *priv, + struct gpioc_pin_intr *intr_conf) +{ + struct gpioc_privs *priv_link; + struct gpioc_pins *pin_link; + unsigned int consistency_a, consistency_b; + + consistency_a = 0; + consistency_b = 0; + mtx_assert(&intr_conf->mtx, MA_OWNED); + mtx_lock(&priv->mtx); + SLIST_FOREACH(priv_link, &intr_conf->privs, next) { + if (priv_link->priv == priv) + consistency_a++; + } + KASSERT(consistency_a <= 1, + ("inconsistent links between pin config and cdevpriv")); + SLIST_FOREACH(pin_link, &priv->pins, next) { + if (pin_link->pin == intr_conf) + consistency_b++; + } + KASSERT(consistency_a == consistency_b, + ("inconsistent links between pin config and cdevpriv")); + if (consistency_a == 1 && consistency_b == 1) { + mtx_unlock(&priv->mtx); + return (EEXIST); + } + priv_link = malloc(sizeof(struct gpioc_privs), M_GPIOC, + M_NOWAIT | M_ZERO); + if (priv_link == NULL) + { + mtx_unlock(&priv->mtx); + return (ENOMEM); + } + pin_link = malloc(sizeof(struct gpioc_pins), M_GPIOC, + M_NOWAIT | M_ZERO); + if (pin_link == NULL) { + mtx_unlock(&priv->mtx); + return (ENOMEM); + } + priv_link->priv = priv; + pin_link->pin = intr_conf; + SLIST_INSERT_HEAD(&intr_conf->privs, priv_link, next); + SLIST_INSERT_HEAD(&priv->pins, pin_link, next); + mtx_unlock(&priv->mtx); + + return (0); +} + +static int +gpioc_detach_priv_pin(struct gpioc_cdevpriv *priv, + struct gpioc_pin_intr *intr_conf) +{ + struct gpioc_privs *priv_link, *priv_link_temp; + struct gpioc_pins *pin_link, *pin_link_temp; + unsigned int consistency_a, consistency_b; + + consistency_a = 0; + consistency_b = 0; + mtx_assert(&intr_conf->mtx, MA_OWNED); + mtx_lock(&priv->mtx); + SLIST_FOREACH_SAFE(priv_link, &intr_conf->privs, next, priv_link_temp) { + if (priv_link->priv == priv) { + SLIST_REMOVE(&intr_conf->privs, priv_link, gpioc_privs, + next); + free(priv_link, M_GPIOC); + consistency_a++; + } + } + KASSERT(consistency_a <= 1, + ("inconsistent links between pin config and cdevpriv")); + SLIST_FOREACH_SAFE(pin_link, &priv->pins, next, pin_link_temp) { + if (pin_link->pin == intr_conf) { + /* + * If the pin we're removing has events in the priv's + * event fifo, we can't leave dangling pointers from + * those events to the gpioc_pins struct we're about to + * free. We also can't remove random items and leave + * holes in the events fifo, so just empty it out. + */ + if (pin_link->eventcount > 0) { + priv->evidx_head = priv->evidx_tail = 0; + } + SLIST_REMOVE(&priv->pins, pin_link, gpioc_pins, next); + free(pin_link, M_GPIOC); + consistency_b++; + } + } + KASSERT(consistency_a == consistency_b, + ("inconsistent links between pin config and cdevpriv")); + mtx_unlock(&priv->mtx); + + return (0); +} + +static bool +gpioc_intr_reconfig_allowed(struct gpioc_cdevpriv *priv, + struct gpioc_pin_intr *intr_conf) +{ + struct gpioc_privs *priv_link; + + mtx_assert(&intr_conf->mtx, MA_OWNED); + + if (SLIST_EMPTY(&intr_conf->privs)) + return (true); + + SLIST_FOREACH(priv_link, &intr_conf->privs, next) { + if (priv_link->priv != priv) + return (false); + } + + return (true); +} + + +static uint32_t +gpioc_get_intr_config(struct gpioc_softc *sc, struct gpioc_cdevpriv *priv, + uint32_t pin) +{ + struct gpioc_pin_intr *intr_conf = &sc->sc_pin_intr[pin]; + struct gpioc_privs *priv_link; + uint32_t flags; + + flags = intr_conf->pin->flags; + + if (flags == 0) + return (0); + + mtx_lock(&intr_conf->mtx); + SLIST_FOREACH(priv_link, &intr_conf->privs, next) { + if (priv_link->priv == priv) { + flags |= GPIO_INTR_ATTACHED; + break; + } + } + mtx_unlock(&intr_conf->mtx); + + return (flags); +} + +static int +gpioc_set_intr_config(struct gpioc_softc *sc, struct gpioc_cdevpriv *priv, + uint32_t pin, uint32_t flags) +{ + struct gpioc_pin_intr *intr_conf = &sc->sc_pin_intr[pin]; + int res; + + res = 0; + if (intr_conf->pin->flags == 0 && flags == 0) { + /* No interrupt configured and none requested: Do nothing. */ + return (0); + } + mtx_lock(&intr_conf->mtx); + while (intr_conf->config_locked == true) + mtx_sleep(&intr_conf->config_locked, &intr_conf->mtx, 0, + "gpicfg", 0); + if (intr_conf->pin->flags == 0 && flags != 0) { + /* + * No interrupt is configured, but one is requested: Allocate + * and setup interrupt on the according pin. + */ + res = gpioc_allocate_pin_intr(intr_conf, flags); + if (res == 0) + res = gpioc_attach_priv_pin(priv, intr_conf); + if (res == EEXIST) + res = 0; + } else if (intr_conf->pin->flags == flags) { + /* + * Same interrupt requested as already configured: Attach the + * cdevpriv to the corresponding pin. + */ + res = gpioc_attach_priv_pin(priv, intr_conf); + if (res == EEXIST) + res = 0; + } else if (intr_conf->pin->flags != 0 && flags == 0) { + /* + * Interrupt configured, but none requested: Teardown and + * release the pin when no other cdevpriv is attached. Otherwise + * just detach pin and cdevpriv from each other. + */ + if (gpioc_intr_reconfig_allowed(priv, intr_conf)) { + res = gpioc_release_pin_intr(intr_conf); + } + if (res == 0) + res = gpioc_detach_priv_pin(priv, intr_conf); + } else { + /* + * Other flag requested than configured: Reconfigure when no + * other cdevpriv is are attached to the pin. + */ + if (!gpioc_intr_reconfig_allowed(priv, intr_conf)) + res = EBUSY; + else { + res = gpioc_release_pin_intr(intr_conf); + if (res == 0) + res = gpioc_allocate_pin_intr(intr_conf, flags); + if (res == 0) + res = gpioc_attach_priv_pin(priv, intr_conf); + if (res == EEXIST) + res = 0; + } + } + mtx_unlock(&intr_conf->mtx); + + return (res); +} + +static void +gpioc_interrupt_handler(void *arg) +{ + struct gpioc_pin_intr *intr_conf; + struct gpioc_privs *privs; + struct gpioc_softc *sc; + sbintime_t evtime; + uint32_t pin_state; + + intr_conf = arg; + sc = intr_conf->sc; + + /* Capture time and pin state first. */ + evtime = sbinuptime(); + if (intr_conf->pin->flags & GPIO_INTR_EDGE_BOTH) + GPIO_PIN_GET(sc->sc_pdev, intr_conf->pin->pin, &pin_state); + else if (intr_conf->pin->flags & GPIO_INTR_EDGE_RISING) + pin_state = true; + else + pin_state = false; + + mtx_lock(&intr_conf->mtx); + + if (intr_conf->config_locked == true) { + ddevice_printf(sc->sc_dev, "Interrupt configuration in " + "progress. Discarding interrupt on pin %d.\n", + intr_conf->pin->pin); + mtx_unlock(&intr_conf->mtx); + return; + } + + if (SLIST_EMPTY(&intr_conf->privs)) { + ddevice_printf(sc->sc_dev, "No file descriptor associated with " + "occurred interrupt on pin %d.\n", intr_conf->pin->pin); + mtx_unlock(&intr_conf->mtx); + return; + } + + SLIST_FOREACH(privs, &intr_conf->privs, next) { + struct gpioc_cdevpriv *priv = privs->priv; + struct gpioc_pins *privpin; + struct gpioc_pin_event *event; + mtx_lock(&priv->mtx); + SLIST_FOREACH(privpin, &priv->pins, next) { + if (privpin->pin == intr_conf) + break; + } + if (privpin == NULL) { + /* Should be impossible. */ + ddevice_printf(sc->sc_dev, "Cannot find privpin\n"); + mtx_unlock(&priv->mtx); + continue; + } + + if (priv->report_option == GPIO_EVENT_REPORT_DETAIL) { + event = next_head_event(priv); + /* If head is overtaking tail, advance tail. */ + if (priv->evidx_head == priv->evidx_tail) + next_tail_event(priv); + } else { + if (privpin->eventcount > 0) + event = &priv->events[privpin->firstevent + 1]; + else { + privpin->firstevent = priv->evidx_head; + event = next_head_event(priv); + event->privpin = privpin; + event->event_time = evtime; + event->event_pin_state = pin_state; + event = next_head_event(priv); + } + ++privpin->eventcount; + } + event->privpin = privpin; + event->event_time = evtime; + event->event_pin_state = pin_state; + wakeup(priv); + selwakeup(&priv->selinfo); + KNOTE_LOCKED(&priv->selinfo.si_note, 0); + if (priv->async == true && priv->sigio != NULL) + pgsigio(&priv->sigio, SIGIO, 0); + mtx_unlock(&priv->mtx); + } + + mtx_unlock(&intr_conf->mtx); +} + +static int gpioc_probe(device_t dev) { device_set_desc(dev, "GPIO controller"); return (0); } static int gpioc_attach(device_t dev) { int err; struct gpioc_softc *sc; struct make_dev_args devargs; sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_pdev = device_get_parent(dev); sc->sc_unit = device_get_unit(dev); + + err = GPIO_PIN_MAX(sc->sc_pdev, &sc->sc_npins); + sc->sc_npins++; /* Number of pins is one more than max pin number. */ + if (err != 0) + return (err); + sc->sc_pin_intr = malloc(sizeof(struct gpioc_pin_intr) * sc->sc_npins, + M_GPIOC, M_WAITOK | M_ZERO); + for (int i = 0; i <= sc->sc_npins; i++) { + sc->sc_pin_intr[i].pin = malloc(sizeof(struct gpiobus_pin), + M_GPIOC, M_WAITOK | M_ZERO); + sc->sc_pin_intr[i].sc = sc; + sc->sc_pin_intr[i].pin->pin = i; + sc->sc_pin_intr[i].pin->dev = sc->sc_pdev; + mtx_init(&sc->sc_pin_intr[i].mtx, "gpioc pin", NULL, MTX_DEF); + SLIST_INIT(&sc->sc_pin_intr[i].privs); + } + make_dev_args_init(&devargs); devargs.mda_devsw = &gpioc_cdevsw; devargs.mda_uid = UID_ROOT; devargs.mda_gid = GID_WHEEL; devargs.mda_mode = 0600; devargs.mda_si_drv1 = sc; err = make_dev_s(&devargs, &sc->sc_ctl_dev, "gpioc%d", sc->sc_unit); if (err != 0) { - printf("Failed to create gpioc%d", sc->sc_unit); + device_printf(dev, "Failed to create gpioc%d", sc->sc_unit); return (ENXIO); } return (0); } static int gpioc_detach(device_t dev) { struct gpioc_softc *sc = device_get_softc(dev); int err; if (sc->sc_ctl_dev) destroy_dev(sc->sc_ctl_dev); + for (int i = 0; i <= sc->sc_npins; i++) { + mtx_destroy(&sc->sc_pin_intr[i].mtx); + free(&sc->sc_pin_intr[i].pin, M_GPIOC); + } + free(sc->sc_pin_intr, M_GPIOC); + if ((err = bus_generic_detach(dev)) != 0) return (err); return (0); } +static void +gpioc_cdevpriv_dtor(void *data) +{ + struct gpioc_cdevpriv *priv; + struct gpioc_privs *priv_link, *priv_link_temp; + struct gpioc_pins *pin_link, *pin_link_temp; + unsigned int consistency; + + priv = data; + + SLIST_FOREACH_SAFE(pin_link, &priv->pins, next, pin_link_temp) { + consistency = 0; + mtx_lock(&pin_link->pin->mtx); + while (pin_link->pin->config_locked == true) + mtx_sleep(&pin_link->pin->config_locked, + &pin_link->pin->mtx, 0, "gpicfg", 0); + SLIST_FOREACH_SAFE(priv_link, &pin_link->pin->privs, next, + priv_link_temp) { + if (priv_link->priv == priv) { + SLIST_REMOVE(&pin_link->pin->privs, priv_link, + gpioc_privs, next); + free(priv_link, M_GPIOC); + consistency++; + } + } + KASSERT(consistency == 1, + ("inconsistent links between pin config and cdevpriv")); + if (gpioc_intr_reconfig_allowed(priv, pin_link->pin)) { + gpioc_release_pin_intr(pin_link->pin); + } + mtx_unlock(&pin_link->pin->mtx); + SLIST_REMOVE(&priv->pins, pin_link, gpioc_pins, next); + free(pin_link, M_GPIOC); + } + + wakeup(&priv); + knlist_clear(&priv->selinfo.si_note, 0); + seldrain(&priv->selinfo); + knlist_destroy(&priv->selinfo.si_note); + funsetown(&priv->sigio); + + mtx_destroy(&priv->mtx); + free(priv->events, M_GPIOC); + free(data, M_GPIOC); +} + +static int +gpioc_open(struct cdev *dev, int oflags, int devtype, struct thread *td) +{ + struct gpioc_cdevpriv *priv; + int err; + + priv = malloc(sizeof(*priv), M_GPIOC, M_WAITOK | M_ZERO); + priv->sc = dev->si_drv1; + priv->report_option = GPIO_EVENT_REPORT_DETAIL; + err = devfs_set_cdevpriv(priv, gpioc_cdevpriv_dtor); + if (err != 0) { + gpioc_cdevpriv_dtor(priv); + return (err); + } + mtx_init(&priv->mtx, "gpioc priv", NULL, MTX_DEF); + knlist_init_mtx(&priv->selinfo.si_note, &priv->mtx); + + /* + * Allocate a circular buffer for events. The scheme we use for summary + * reporting assumes there will always be a pair of events available to + * record the first/last events on any pin, so we allocate 2 * npins. + * Even though we actually default to detailed event reporting, 2 * + * npins isn't a horrible fifo size for that either. + */ + priv->numevents = priv->sc->sc_npins * 2; + priv->events = malloc(priv->numevents * sizeof(struct gpio_event_detail), + M_GPIOC, M_WAITOK | M_ZERO); + + return (0); +} + +static int +gpioc_read(struct cdev *dev, struct uio *uio, int ioflag) +{ + struct gpioc_cdevpriv *priv; + struct gpioc_pin_event *event; + union { + struct gpio_event_summary sum; + struct gpio_event_detail evt; + uint8_t data[1]; + } recbuf; + size_t recsize; + int err; + + if ((err = devfs_get_cdevpriv((void **)&priv)) != 0) + return (err); + + if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY) + recsize = sizeof(struct gpio_event_summary); + else + recsize = sizeof(struct gpio_event_detail); + + if (uio->uio_resid < recsize) + return (EINVAL); + + mtx_lock(&priv->mtx); + while (priv->evidx_head == priv->evidx_tail) { + if (SLIST_EMPTY(&priv->pins)) { + err = ENXIO; + break; + } else if (ioflag & O_NONBLOCK) { + err = EWOULDBLOCK; + break; + } else { + err = mtx_sleep(priv, &priv->mtx, PCATCH, "gpintr", 0); + if (err != 0) + break; + } + } + + while (err == 0 && uio->uio_resid >= recsize && + priv->evidx_tail != priv->evidx_head) { + event = next_tail_event(priv); + if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY) { + recbuf.sum.gp_first_time = event->event_time; + recbuf.sum.gp_pin = event->privpin->pin->pin->pin; + recbuf.sum.gp_count = event->privpin->eventcount; + recbuf.sum.gp_first_state = event->event_pin_state; + event = next_tail_event(priv); + recbuf.sum.gp_last_time = event->event_time; + recbuf.sum.gp_last_state = event->event_pin_state; + event->privpin->eventcount = 0; + event->privpin->firstevent = 0; + } else { + recbuf.evt.gp_time = event->event_time; + recbuf.evt.gp_pin = event->privpin->pin->pin->pin; + recbuf.evt.gp_pinstate = event->event_pin_state; + } + mtx_unlock(&priv->mtx); + err = uiomove(recbuf.data, recsize, uio); + mtx_lock(&priv->mtx); + } + mtx_unlock(&priv->mtx); + return (err); +} + static int gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, struct thread *td) { device_t bus; int max_pin, res; struct gpioc_softc *sc = cdev->si_drv1; + struct gpioc_cdevpriv *priv; struct gpio_pin pin; struct gpio_req req; struct gpio_access_32 *a32; struct gpio_config_32 *c32; - uint32_t caps; + struct gpio_event_config *evcfg; + uint32_t caps, intrflags; bus = GPIO_GET_BUS(sc->sc_pdev); if (bus == NULL) return (EINVAL); switch (cmd) { - case GPIOMAXPIN: - max_pin = -1; - res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin); - bcopy(&max_pin, arg, sizeof(max_pin)); + case GPIOMAXPIN: + max_pin = -1; + res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin); + bcopy(&max_pin, arg, sizeof(max_pin)); + break; + case GPIOGETCONFIG: + bcopy(arg, &pin, sizeof(pin)); + dprintf("get config pin %d\n", pin.gp_pin); + res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin, + &pin.gp_flags); + /* Fail early */ + if (res) break; - case GPIOGETCONFIG: - bcopy(arg, &pin, sizeof(pin)); - dprintf("get config pin %d\n", pin.gp_pin); - res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin, - &pin.gp_flags); - /* Fail early */ - if (res) - break; - GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps); - GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name); - bcopy(&pin, arg, sizeof(pin)); + res = devfs_get_cdevpriv((void **)&priv); + if (res) break; - case GPIOSETCONFIG: - bcopy(arg, &pin, sizeof(pin)); - dprintf("set config pin %d\n", pin.gp_pin); - 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); + pin.gp_flags |= gpioc_get_intr_config(sc, priv, + pin.gp_pin); + GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps); + GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name); + bcopy(&pin, arg, sizeof(pin)); + break; + case GPIOSETCONFIG: + bcopy(arg, &pin, sizeof(pin)); + dprintf("set config pin %d\n", pin.gp_pin); + res = devfs_get_cdevpriv((void **)&priv); + if (res != 0) break; - case GPIOGET: - bcopy(arg, &req, sizeof(req)); - res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin, - &req.gp_value); - dprintf("read pin %d -> %d\n", - req.gp_pin, req.gp_value); - bcopy(&req, arg, sizeof(req)); + res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps); + if (res != 0) break; - case GPIOSET: - bcopy(arg, &req, sizeof(req)); - res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin, - req.gp_value); - dprintf("write pin %d -> %d\n", - req.gp_pin, req.gp_value); + res = gpio_check_flags(caps, pin.gp_flags); + if (res != 0) break; - case GPIOTOGGLE: - bcopy(arg, &req, sizeof(req)); - dprintf("toggle pin %d\n", - req.gp_pin); - res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin); + intrflags = pin.gp_flags & GPIO_INTR_MASK; + /* + * We can do only edge interrupts, and only if the + * hardware supports that interrupt type on that pin. + */ + switch (intrflags) { + case GPIO_INTR_NONE: break; - case GPIOSETNAME: - bcopy(arg, &pin, sizeof(pin)); - dprintf("set name on pin %d\n", pin.gp_pin); - res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin, - pin.gp_name); + case GPIO_INTR_EDGE_RISING: + case GPIO_INTR_EDGE_FALLING: + case GPIO_INTR_EDGE_BOTH: + if ((intrflags & caps) == 0) + res = EOPNOTSUPP; break; - case GPIOACCESS32: - a32 = (struct gpio_access_32 *)arg; - res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin, - a32->clear_pins, a32->change_pins, &a32->orig_pins); + default: + res = EINVAL; break; - case GPIOCONFIG32: - c32 = (struct gpio_config_32 *)arg; - res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin, - c32->num_pins, c32->pin_flags); + } + if (res != 0) break; - default: - return (ENOTTY); + res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin, + (pin.gp_flags & ~GPIO_INTR_MASK)); + if (res != 0) break; + res = gpioc_set_intr_config(sc, priv, pin.gp_pin, + intrflags); + break; + case GPIOGET: + bcopy(arg, &req, sizeof(req)); + res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin, + &req.gp_value); + dprintf("read pin %d -> %d\n", + req.gp_pin, req.gp_value); + bcopy(&req, arg, sizeof(req)); + break; + case GPIOSET: + bcopy(arg, &req, sizeof(req)); + res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin, + req.gp_value); + dprintf("write pin %d -> %d\n", + req.gp_pin, req.gp_value); + break; + case GPIOTOGGLE: + bcopy(arg, &req, sizeof(req)); + dprintf("toggle pin %d\n", + req.gp_pin); + res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin); + break; + case GPIOSETNAME: + bcopy(arg, &pin, sizeof(pin)); + dprintf("set name on pin %d\n", pin.gp_pin); + res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin, + pin.gp_name); + break; + case GPIOACCESS32: + a32 = (struct gpio_access_32 *)arg; + res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin, + a32->clear_pins, a32->change_pins, &a32->orig_pins); + break; + case GPIOCONFIG32: + c32 = (struct gpio_config_32 *)arg; + res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin, + c32->num_pins, c32->pin_flags); + break; + case GPIOCONFIGEVENTS: + evcfg = (struct gpio_event_config *)arg; + res = devfs_get_cdevpriv((void **)&priv); + if (res != 0) + break; + /* If any pins have been configured, changes aren't allowed. */ + if (!SLIST_EMPTY(&priv->pins)) { + res = EINVAL; + break; + } + if (evcfg->gp_report_type != GPIO_EVENT_REPORT_DETAIL && + evcfg->gp_report_type != GPIO_EVENT_REPORT_SUMMARY) { + res = EINVAL; + break; + } + priv->report_option = evcfg->gp_report_type; + /* Reallocate the events buffer if the user wants it bigger. */ + if (priv->report_option == GPIO_EVENT_REPORT_DETAIL && + priv->numevents < evcfg->gp_fifo_size) { + free(priv->events, M_GPIOC); + priv->numevents = evcfg->gp_fifo_size; + priv->events = malloc(priv->numevents * + sizeof(struct gpio_event_detail), M_GPIOC, + M_WAITOK | M_ZERO); + priv->evidx_head = priv->evidx_tail = 0; + } + break; + case FIONBIO: + /* + * This dummy handler is necessary to prevent fcntl() + * from failing. The actual handling of non-blocking IO + * is done using the O_NONBLOCK ioflag passed to the + * read() syscall. + */ + res = 0; + break; + case FIOASYNC: + res = devfs_get_cdevpriv((void **)&priv); + if (res == 0) { + if (*(int *)arg == FASYNC) + priv->async = true; + else + priv->async = false; + } + break; + case FIOGETOWN: + res = devfs_get_cdevpriv((void **)&priv); + if (res == 0) + *(int *)arg = fgetown(&priv->sigio); + break; + case FIOSETOWN: + res = devfs_get_cdevpriv((void **)&priv); + if (res == 0) + res = fsetown(*(int *)arg, &priv->sigio); + break; + default: + return (ENOTTY); + break; } return (res); +} + +static int +gpioc_poll(struct cdev *dev, int events, struct thread *td) +{ + struct gpioc_cdevpriv *priv; + int err; + int revents; + + revents = 0; + + err = devfs_get_cdevpriv((void **)&priv); + if (err != 0) { + revents = POLLERR; + return (revents); + } + + if (SLIST_EMPTY(&priv->pins)) { + revents = POLLHUP; + return (revents); + } + + if (events & (POLLIN | POLLRDNORM)) { + if (priv->evidx_head != priv->evidx_tail) + revents |= events & (POLLIN | POLLRDNORM); + else + selrecord(td, &priv->selinfo); + } + + return (revents); +} + +static int +gpioc_kqfilter(struct cdev *dev, struct knote *kn) +{ + struct gpioc_cdevpriv *priv; + struct knlist *knlist; + int err; + + err = devfs_get_cdevpriv((void **)&priv); + if (err != 0) + return err; + + if (SLIST_EMPTY(&priv->pins)) + return (ENXIO); + + switch(kn->kn_filter) { + case EVFILT_READ: + kn->kn_fop = &gpioc_read_filterops; + kn->kn_hook = (void *)priv; + break; + default: + return (EOPNOTSUPP); + } + + knlist = &priv->selinfo.si_note; + knlist_add(knlist, kn, 0); + + return (0); +} + +static int +gpioc_kqread(struct knote *kn, long hint) +{ + struct gpioc_cdevpriv *priv = kn->kn_hook; + size_t recsize; + + + if (SLIST_EMPTY(&priv->pins)) { + kn->kn_flags |= EV_EOF; + return (1); + } else { + if (priv->evidx_head != priv->evidx_tail) { + if (priv->report_option == GPIO_EVENT_REPORT_SUMMARY) + recsize = sizeof(struct gpio_event_summary); + else + recsize = sizeof(struct gpio_event_detail); + kn->kn_data = recsize * number_of_events(priv); + return (1); + } + } + return (0); +} + +static void +gpioc_kqdetach(struct knote *kn) +{ + struct gpioc_cdevpriv *priv = kn->kn_hook; + struct knlist *knlist = &priv->selinfo.si_note; + + knlist_remove(knlist, kn, 0); } static device_method_t gpioc_methods[] = { /* Device interface */ DEVMETHOD(device_probe, gpioc_probe), DEVMETHOD(device_attach, gpioc_attach), DEVMETHOD(device_detach, gpioc_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), DEVMETHOD_END }; driver_t gpioc_driver = { "gpioc", gpioc_methods, sizeof(struct gpioc_softc) }; devclass_t gpioc_devclass; DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0); MODULE_VERSION(gpioc, 1); Index: stable/12/sys/sys/gpio.h =================================================================== --- stable/12/sys/sys/gpio.h (revision 368701) +++ stable/12/sys/sys/gpio.h (revision 368702) @@ -1,179 +1,235 @@ /* $NetBSD: gpio.h,v 1.7 2009/09/25 20:27:50 mbalmer Exp $ */ /* $OpenBSD: gpio.h,v 1.7 2008/11/26 14:51:20 mbalmer Exp $ */ /*- * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND ISC) * * Copyright (c) 2009, Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ * */ /* * Copyright (c) 2009 Marc Balmer * Copyright (c) 2004 Alexander Yurchenko * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef __GPIO_H__ #define __GPIO_H__ #include +#ifndef _KERNEL +#include +#endif /* GPIO pin states */ #define GPIO_PIN_LOW 0x00 /* low level (logical 0) */ #define GPIO_PIN_HIGH 0x01 /* high level (logical 1) */ /* Max name length of a pin */ #define GPIOMAXNAME 64 /* GPIO pin configuration flags */ #define GPIO_PIN_INPUT 0x00000001 /* input direction */ #define GPIO_PIN_OUTPUT 0x00000002 /* output direction */ #define GPIO_PIN_OPENDRAIN 0x00000004 /* open-drain output */ #define GPIO_PIN_PUSHPULL 0x00000008 /* push-pull output */ #define GPIO_PIN_TRISTATE 0x00000010 /* output disabled */ #define GPIO_PIN_PULLUP 0x00000020 /* internal pull-up enabled */ #define GPIO_PIN_PULLDOWN 0x00000040 /* internal pull-down enabled */ #define GPIO_PIN_INVIN 0x00000080 /* invert input */ #define GPIO_PIN_INVOUT 0x00000100 /* invert output */ #define GPIO_PIN_PULSATE 0x00000200 /* pulsate in hardware */ #define GPIO_PIN_PRESET_LOW 0x00000400 /* preset pin to high or */ #define GPIO_PIN_PRESET_HIGH 0x00000800 /* low before enabling output */ /* GPIO interrupt capabilities */ #define GPIO_INTR_NONE 0x00000000 /* no interrupt support */ #define GPIO_INTR_LEVEL_LOW 0x00010000 /* level trigger, low */ #define GPIO_INTR_LEVEL_HIGH 0x00020000 /* level trigger, high */ #define GPIO_INTR_EDGE_RISING 0x00040000 /* edge trigger, rising */ #define GPIO_INTR_EDGE_FALLING 0x00080000 /* edge trigger, falling */ #define GPIO_INTR_EDGE_BOTH 0x00100000 /* edge trigger, both */ -#define GPIO_INTR_MASK (GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | \ - GPIO_INTR_EDGE_RISING | \ - GPIO_INTR_EDGE_FALLING | GPIO_INTR_EDGE_BOTH) +#define GPIO_INTR_ATTACHED 0x00200000 /* interrupt attached to file */ +#define GPIO_INTR_MASK (GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | \ + GPIO_INTR_EDGE_RISING | \ + GPIO_INTR_EDGE_FALLING | GPIO_INTR_EDGE_BOTH | \ + GPIO_INTR_ATTACHED) struct gpio_pin { uint32_t gp_pin; /* pin number */ char gp_name[GPIOMAXNAME]; /* human-readable name */ uint32_t gp_caps; /* capabilities */ uint32_t gp_flags; /* current flags */ }; /* GPIO pin request (read/write/toggle) */ struct gpio_req { uint32_t gp_pin; /* pin number */ uint32_t gp_value; /* value */ }; /* + * Reporting gpio pin-change per-event details to userland. + * + * When configured for detail reporting, each call to read(2) will return one or + * more of these structures (or will return EWOULDBLOCK in non-blocking IO mode + * when there are no new events to report). + */ +struct gpio_event_detail { + sbintime_t gp_time; /* Time of event */ + uint16_t gp_pin; /* Pin number */ + bool gp_pinstate; /* Pin state at time of event */ +}; + +/* + * Reporting gpio pin-change summary data to userland. + * + * When configured for summary reporting, each call to read(2) will return one + * or more of these structures (or will return EWOULDBLOCK in non-blocking IO + * mode when there are no new events to report). + */ +struct gpio_event_summary { + sbintime_t gp_first_time; /* Time of first event */ + sbintime_t gp_last_time; /* Time of last event */ + uint16_t gp_pin; /* Pin number */ + uint16_t gp_count; /* Event count */ + bool gp_first_state; /* Pin state at first event */ + bool gp_last_state; /* Pin state at last event */ +}; + +/* + * Configuring event reporting to userland. + * + * The default is to deliver gpio_event_detail reporting, with a default fifo + * size of 2 * number of pins belonging to the gpioc device instance. To change + * it, you must use the GPIOCONFIGEVENTS ioctl before using GPIOSETCONFIG to + * configure reporting interrupt events on any pins. This config is tracked on + * a per-open-descriptor basis. + */ +enum { + GPIO_EVENT_REPORT_DETAIL, /* Report detail on each event */ + GPIO_EVENT_REPORT_SUMMARY, /* Report summary of events */ +}; +struct gpio_event_config { + uint32_t gp_report_type; /* Detail or summary reporting */ + uint32_t gp_fifo_size; /* FIFO size (used for detail only) */ +}; + +/* * gpio_access_32 / GPIOACCESS32 * * Simultaneously read and/or change up to 32 adjacent pins. * If the device cannot change the pins simultaneously, returns EOPNOTSUPP. * * This accesses an adjacent set of up to 32 pins starting at first_pin within * the device's collection of pins. How the hardware pins are mapped to the 32 * bits in the arguments is device-specific. It is expected that lower-numbered * pins in the device's number space map linearly to lower-ordered bits within * the 32-bit words (i.e., bit 0 is first_pin, bit 1 is first_pin+1, etc). * Other mappings are possible; know your device. * * Some devices may limit the value of first_pin to 0, or to multiples of 16 or * 32 or some other hardware-specific number; to access pin 2 would require * first_pin to be zero and then manipulate bit (1 << 2) in the 32-bit word. * Invalid values in first_pin result in an EINVAL error return. * * The starting state of the pins is captured and stored in orig_pins, then the * pins are set to ((starting_state & ~clear_pins) ^ change_pins). * * Clear Change Hardware pin after call * 0 0 No change * 0 1 Opposite of current value * 1 0 Cleared * 1 1 Set */ struct gpio_access_32 { uint32_t first_pin; /* First pin in group of 32 adjacent */ uint32_t clear_pins; /* Pins are changed using: */ uint32_t change_pins; /* ((hwstate & ~clear_pins) ^ change_pins) */ uint32_t orig_pins; /* Returned hwstate of pins before change. */ }; /* * gpio_config_32 / GPIOCONFIG32 * * Simultaneously configure up to 32 adjacent pins. This is intended to change * the configuration of all the pins simultaneously, such that pins configured * for output all begin to drive the configured values simultaneously, but not * all hardware can do that, so the driver "does the best it can" in this * regard. Notably unlike pin_access_32(), this does NOT fail if the pins * cannot be atomically configured; it is expected that callers understand the * hardware and have decided to live with any such limitations it may have. * * The pin_flags argument is an array of GPIO_PIN_xxxx flags. If the array * contains any GPIO_PIN_OUTPUT flags, the driver will manipulate the hardware * such that all output pins become driven with the proper initial values * simultaneously if it can. The elements in the array map to pins in the same - * way that bits are mapped by pin_acces_32(), and the same restrictions may + * way that bits are mapped by pin_access_32(), and the same restrictions may * apply. For example, to configure pins 2 and 3 it may be necessary to set * first_pin to zero and only populate pin_flags[2] and pin_flags[3]. If a * given array entry doesn't contain GPIO_PIN_INPUT or GPIO_PIN_OUTPUT then no * configuration is done for that pin. * * Some devices may limit the value of first_pin to 0, or to multiples of 16 or * 32 or some other hardware-specific number. Invalid values in first_pin or * num_pins result in an error return with errno set to EINVAL. + * + * You cannot configure interrupts (userland pin-change notifications) with + * this function; each interrupt pin must be individually configured. */ struct gpio_config_32 { uint32_t first_pin; uint32_t num_pins; uint32_t pin_flags[32]; }; /* * ioctls */ #define GPIOMAXPIN _IOR('G', 0, int) #define GPIOGETCONFIG _IOWR('G', 1, struct gpio_pin) #define GPIOSETCONFIG _IOW('G', 2, struct gpio_pin) #define GPIOGET _IOWR('G', 3, struct gpio_req) #define GPIOSET _IOW('G', 4, struct gpio_req) #define GPIOTOGGLE _IOWR('G', 5, struct gpio_req) #define GPIOSETNAME _IOW('G', 6, struct gpio_pin) #define GPIOACCESS32 _IOWR('G', 7, struct gpio_access_32) #define GPIOCONFIG32 _IOW('G', 8, struct gpio_config_32) +#define GPIOCONFIGEVENTS _IOW('G', 9, struct gpio_event_config) #endif /* __GPIO_H__ */ Index: stable/12/tools/test/README =================================================================== --- stable/12/tools/test/README (revision 368701) +++ stable/12/tools/test/README (revision 368702) @@ -1,24 +1,25 @@ $FreeBSD$ This directory is for standalone test programs. For the FreeBSD Test Suite, which uses Kyua, please see /usr/src/tests/ A test program is one that exercises a particular bit of the system and either tries to break it or measures its performance. Please make a subdir per program, and add a brief description to this file. auxinfo Return information on page sizes, CPUs, and OS release date. devrandom Programs to test /dev/*random. +gpioevents Test delivery of gpio pin-change events to userland. hwpmc Automatically trigger every event in hwpmc(4). iconv Character set conversion tests. malloc A program to test and benchmark malloc(). net A set of generic test programs for networking. netfibs Programs to test multi-FIB network stacks. posixshm A program to test POSIX shared memory. ppsapi Test 1 Pulse Per Second (1PPS) input for time control. pthread_vfork Check that vfork and pthreads work together. ptrace Verify that ptrace works with syscalls, vfork etc. sort Tests for the sort command, including a full regression. testfloat Programs to test floating-point implementations upsdl Test of mmap functionality. Index: stable/12/tools/test/gpioevents/Makefile =================================================================== --- stable/12/tools/test/gpioevents/Makefile (nonexistent) +++ stable/12/tools/test/gpioevents/Makefile (revision 368702) @@ -0,0 +1,12 @@ +# $FreeBSD$ + +PROG= gpioevents +SRCS= gpioevents.c + +LIBADD= gpio + +MK_MAN= no + +BINDIR= /usr/bin + +.include Property changes on: stable/12/tools/test/gpioevents/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/12/tools/test/gpioevents/gpioevents.c =================================================================== --- stable/12/tools/test/gpioevents/gpioevents.c (nonexistent) +++ stable/12/tools/test/gpioevents/gpioevents.c (revision 368702) @@ -0,0 +1,642 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2018 Christian Kramer + * Copyright (c) 2020 Ian Lepore + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + * + * make LDFLAGS+=-lgpio gpioevents + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +static bool be_verbose = false; +static int report_format = GPIO_EVENT_REPORT_DETAIL; +static struct timespec utc_offset; + +static volatile sig_atomic_t sigio = 0; + +static void +sigio_handler(int sig __unused){ + sigio = 1; +} + +static void +usage() +{ + fprintf(stderr, "usage: %s [-f ctldev] [-m method] [-s] [-n] [-S] [-u]" + "[-t timeout] [-d delay-usec] pin intr-config [pin intr-config ...]\n\n", + getprogname()); + fprintf(stderr, " -d delay before each call to read/poll/select/etc\n"); + fprintf(stderr, " -n Non-blocking IO\n"); + fprintf(stderr, " -s Single-shot (else loop continuously)\n"); + fprintf(stderr, " -S Report summary data (else report each event)\n"); + fprintf(stderr, " -u Show timestamps as UTC (else monotonic time)\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Possible options for method:\n\n"); + fprintf(stderr, " r\tread (default)\n"); + fprintf(stderr, " p\tpoll\n"); + fprintf(stderr, " s\tselect\n"); + fprintf(stderr, " k\tkqueue\n"); + fprintf(stderr, " a\taio_read (needs sysctl vfs.aio.enable_unsafe=1)\n"); + fprintf(stderr, " i\tsignal-driven I/O\n\n"); + fprintf(stderr, "Possible options for intr-config:\n\n"); + fprintf(stderr, " no\t no interrupt\n"); + fprintf(stderr, " er\t edge rising\n"); + fprintf(stderr, " ef\t edge falling\n"); + fprintf(stderr, " eb\t edge both\n"); +} + +static void +verbose(const char *fmt, ...) +{ + va_list args; + + if (!be_verbose) + return; + + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +} + +static const char* +poll_event_to_str(short event) +{ + switch (event) { + case POLLIN: + return "POLLIN"; + case POLLPRI: + return "POLLPRI:"; + case POLLOUT: + return "POLLOUT:"; + case POLLRDNORM: + return "POLLRDNORM"; + case POLLRDBAND: + return "POLLRDBAND"; + case POLLWRBAND: + return "POLLWRBAND"; + case POLLINIGNEOF: + return "POLLINIGNEOF"; + case POLLERR: + return "POLLERR"; + case POLLHUP: + return "POLLHUP"; + case POLLNVAL: + return "POLLNVAL"; + default: + return "unknown event"; + } +} + +static void +print_poll_events(short event) +{ + short curr_event = 0; + bool first = true; + + for (size_t i = 0; i <= sizeof(short) * CHAR_BIT - 1; i++) { + curr_event = 1 << i; + if ((event & curr_event) == 0) + continue; + if (!first) { + printf(" | "); + } else { + first = false; + } + printf("%s", poll_event_to_str(curr_event)); + } +} + +static void +calc_utc_offset() +{ + struct timespec monotime, utctime; + + clock_gettime(CLOCK_MONOTONIC, &monotime); + clock_gettime(CLOCK_REALTIME, &utctime); + timespecsub(&utctime, &monotime, &utc_offset); +} + +static void +print_timestamp(const char *str, sbintime_t timestamp) +{ + struct timespec ts; + char timebuf[32]; + + ts = sbttots(timestamp); + + if (!timespecisset(&utc_offset)) { + printf("%s %jd.%09ld ", str, (intmax_t)ts.tv_sec, ts.tv_nsec); + } else { + timespecadd(&utc_offset, &ts, &ts); + strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%S", + gmtime(&ts.tv_sec)); + printf("%s %s.%09ld ", str, timebuf, ts.tv_nsec); + } +} + +static void +print_event_detail(const struct gpio_event_detail *det) +{ + print_timestamp("time", det->gp_time); + printf("pin %hu state %u\n", det->gp_pin, det->gp_pinstate); +} + +static void +print_event_summary(const struct gpio_event_summary *sum) +{ + print_timestamp("first_time", sum->gp_first_time); + print_timestamp("last_time", sum->gp_last_time); + printf("pin %hu count %hu first state %u last state %u\n", + sum->gp_pin, sum->gp_count, + sum->gp_first_state, sum->gp_last_state); +} + +static void +print_gpio_event(const void *buf) +{ + if (report_format == GPIO_EVENT_REPORT_DETAIL) + print_event_detail((const struct gpio_event_detail *)buf); + else + print_event_summary((const struct gpio_event_summary *)buf); +} + +static void +run_read(bool loop, int handle, const char *file, u_int delayus) +{ + const size_t numrecs = 64; + union { + const struct gpio_event_summary sum[numrecs]; + const struct gpio_event_detail det[numrecs]; + uint8_t data[1]; + } buffer; + ssize_t reccount, recsize, res; + + if (report_format == GPIO_EVENT_REPORT_DETAIL) + recsize = sizeof(struct gpio_event_detail); + else + recsize = sizeof(struct gpio_event_summary); + + do { + if (delayus != 0) { + verbose("sleep %f seconds before read()\n", + delayus / 1000000.0); + usleep(delayus); + } + verbose("read into %zd byte buffer\n", sizeof(buffer)); + res = read(handle, buffer.data, sizeof(buffer)); + if (res < 0) + err(EXIT_FAILURE, "Cannot read from %s", file); + + if ((res % recsize) != 0) { + fprintf(stderr, "%s: read() %zd bytes from %s; " + "expected a multiple of %zu\n", + getprogname(), res, file, recsize); + } else { + reccount = res / recsize; + verbose("read returned %zd bytes; %zd events\n", res, + reccount); + for (ssize_t i = 0; i < reccount; ++i) { + if (report_format == GPIO_EVENT_REPORT_DETAIL) + print_event_detail(&buffer.det[i]); + else + print_event_summary(&buffer.sum[i]); + } + } + } while (loop); +} + +static void +run_poll(bool loop, int handle, const char *file, int timeout, u_int delayus) +{ + struct pollfd fds; + int res; + + fds.fd = handle; + fds.events = POLLIN | POLLRDNORM; + fds.revents = 0; + + do { + if (delayus != 0) { + verbose("sleep %f seconds before poll()\n", + delayus / 1000000.0); + usleep(delayus); + } + res = poll(&fds, 1, timeout); + if (res < 0) { + err(EXIT_FAILURE, "Cannot poll() %s", file); + } else if (res == 0) { + printf("%s: poll() timed out on %s\n", getprogname(), + file); + } else { + printf("%s: poll() returned %i (revents: ", + getprogname(), res); + print_poll_events(fds.revents); + printf(") on %s\n", file); + if (fds.revents & (POLLHUP | POLLERR)) { + err(EXIT_FAILURE, "Recieved POLLHUP or POLLERR " + "on %s", file); + } + run_read(false, handle, file, 0); + } + } while (loop); +} + +static void +run_select(bool loop, int handle, const char *file, int timeout, u_int delayus) +{ + fd_set readfds; + struct timeval tv; + struct timeval *tv_ptr; + int res; + + FD_ZERO(&readfds); + FD_SET(handle, &readfds); + if (timeout != INFTIM) { + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + tv_ptr = &tv; + } else { + tv_ptr = NULL; + } + + do { + if (delayus != 0) { + verbose("sleep %f seconds before select()\n", + delayus / 1000000.0); + usleep(delayus); + } + res = select(FD_SETSIZE, &readfds, NULL, NULL, tv_ptr); + if (res < 0) { + err(EXIT_FAILURE, "Cannot select() %s", file); + } else if (res == 0) { + printf("%s: select() timed out on %s\n", getprogname(), + file); + } else { + printf("%s: select() returned %i on %s\n", + getprogname(), res, file); + run_read(false, handle, file, 0); + } + } while (loop); +} + +static void +run_kqueue(bool loop, int handle, const char *file, int timeout, u_int delayus) +{ + struct kevent event[1]; + struct kevent tevent[1]; + int kq = -1; + int nev = -1; + struct timespec tv; + struct timespec *tv_ptr; + + if (timeout != INFTIM) { + tv.tv_sec = timeout / 1000; + tv.tv_nsec = (timeout % 1000) * 10000000; + tv_ptr = &tv; + } else { + tv_ptr = NULL; + } + + kq = kqueue(); + if (kq == -1) + err(EXIT_FAILURE, "kqueue() %s", file); + + EV_SET(&event[0], handle, EVFILT_READ, EV_ADD, 0, 0, NULL); + nev = kevent(kq, event, 1, NULL, 0, NULL); + if (nev == -1) + err(EXIT_FAILURE, "kevent() %s", file); + + do { + if (delayus != 0) { + verbose("sleep %f seconds before kevent()\n", + delayus / 1000000.0); + usleep(delayus); + } + nev = kevent(kq, NULL, 0, tevent, 1, tv_ptr); + if (nev == -1) { + err(EXIT_FAILURE, "kevent() %s", file); + } else if (nev == 0) { + printf("%s: kevent() timed out on %s\n", getprogname(), + file); + } else { + printf("%s: kevent() returned %i events (flags: %d) on " + "%s\n", getprogname(), nev, tevent[0].flags, file); + if (tevent[0].flags & EV_EOF) { + err(EXIT_FAILURE, "Recieved EV_EOF on %s", + file); + } + run_read(false, handle, file, 0); + } + } while (loop); +} + +static void +run_aio_read(bool loop, int handle, const char *file, u_int delayus) +{ + uint8_t buffer[1024]; + size_t recsize; + ssize_t res; + struct aiocb iocb; + + /* + * Note that async IO to character devices is no longer allowed by + * default (since freebsd 11). This code is still here (for now) + * because you can use sysctl vfs.aio.enable_unsafe=1 to bypass the + * prohibition and run this code. + */ + + if (report_format == GPIO_EVENT_REPORT_DETAIL) + recsize = sizeof(struct gpio_event_detail); + else + recsize = sizeof(struct gpio_event_summary); + + bzero(&iocb, sizeof(iocb)); + + iocb.aio_fildes = handle; + iocb.aio_nbytes = sizeof(buffer); + iocb.aio_offset = 0; + iocb.aio_buf = buffer; + + do { + if (delayus != 0) { + verbose("sleep %f seconds before aio_read()\n", + delayus / 1000000.0); + usleep(delayus); + } + res = aio_read(&iocb); + if (res < 0) + err(EXIT_FAILURE, "Cannot aio_read from %s", file); + do { + res = aio_error(&iocb); + } while (res == EINPROGRESS); + if (res < 0) + err(EXIT_FAILURE, "aio_error on %s", file); + res = aio_return(&iocb); + if (res < 0) + err(EXIT_FAILURE, "aio_return on %s", file); + if ((res % recsize) != 0) { + fprintf(stderr, "%s: aio_read() %zd bytes from %s; " + "expected a multiple of %zu\n", + getprogname(), res, file, recsize); + } else { + for (ssize_t i = 0; i < res; i += recsize) + print_gpio_event(&buffer[i]); + } + } while (loop); +} + + +static void +run_sigio(bool loop, int handle, const char *file) +{ + int res; + struct sigaction sigact; + int flags; + int pid; + + bzero(&sigact, sizeof(sigact)); + sigact.sa_handler = sigio_handler; + if (sigaction(SIGIO, &sigact, NULL) < 0) + err(EXIT_FAILURE, "cannot set SIGIO handler on %s", file); + flags = fcntl(handle, F_GETFL); + flags |= O_ASYNC; + res = fcntl(handle, F_SETFL, flags); + if (res < 0) + err(EXIT_FAILURE, "fcntl(F_SETFL) on %s", file); + pid = getpid(); + res = fcntl(handle, F_SETOWN, pid); + if (res < 0) + err(EXIT_FAILURE, "fnctl(F_SETOWN) on %s", file); + + do { + if (sigio == 1) { + sigio = 0; + printf("%s: recieved SIGIO on %s\n", getprogname(), + file); + run_read(false, handle, file, 0); + } + pause(); + } while (loop); +} + +int +main(int argc, char *argv[]) +{ + int ch; + const char *file = "/dev/gpioc0"; + char method = 'r'; + bool loop = true; + bool nonblock = false; + u_int delayus = 0; + int flags; + int timeout = INFTIM; + int handle; + int res; + gpio_config_t pin_config; + + while ((ch = getopt(argc, argv, "d:f:m:sSnt:uv")) != -1) { + switch (ch) { + case 'd': + delayus = strtol(optarg, NULL, 10); + if (errno != 0) { + warn("Invalid delay value"); + usage(); + return EXIT_FAILURE; + } + break; + case 'f': + file = optarg; + break; + case 'm': + method = optarg[0]; + break; + case 's': + loop = false; + break; + case 'S': + report_format = GPIO_EVENT_REPORT_SUMMARY; + break; + case 'n': + nonblock= true; + break; + case 't': + errno = 0; + timeout = strtol(optarg, NULL, 10); + if (errno != 0) { + warn("Invalid timeout value"); + usage(); + return EXIT_FAILURE; + } + break; + case 'u': + calc_utc_offset(); + break; + case 'v': + be_verbose = true; + break; + default: + usage(); + return EXIT_FAILURE; + } + } + argv += optind; + argc -= optind; + + if (argc == 0) { + fprintf(stderr, "%s: No pin number specified.\n", + getprogname()); + usage(); + return EXIT_FAILURE; + } + + if (argc == 1) { + fprintf(stderr, "%s: No trigger type specified.\n", + getprogname()); + usage(); + return EXIT_FAILURE; + } + + if (argc % 2 == 1) { + fprintf(stderr, "%s: Invalid number of pin intr-conf pairs.\n", + getprogname()); + usage(); + return EXIT_FAILURE; + } + + handle = gpio_open_device(file); + if (handle == GPIO_INVALID_HANDLE) + err(EXIT_FAILURE, "Cannot open %s", file); + + if (report_format == GPIO_EVENT_REPORT_SUMMARY) { + struct gpio_event_config cfg = + {GPIO_EVENT_REPORT_SUMMARY, 0}; + + res = ioctl(handle, GPIOCONFIGEVENTS, &cfg); + if (res < 0) + err(EXIT_FAILURE, "GPIOCONFIGEVENTS failed on %s", file); + } + + if (nonblock == true) { + flags = fcntl(handle, F_GETFL); + flags |= O_NONBLOCK; + res = fcntl(handle, F_SETFL, flags); + if (res < 0) + err(EXIT_FAILURE, "cannot set O_NONBLOCK on %s", file); + } + + for (int i = 0; i <= argc - 2; i += 2) { + + errno = 0; + pin_config.g_pin = strtol(argv[i], NULL, 10); + if (errno != 0) { + warn("Invalid pin number"); + usage(); + return EXIT_FAILURE; + } + + if (strnlen(argv[i + 1], 2) < 2) { + fprintf(stderr, "%s: Invalid trigger type (argument " + "too short).\n", getprogname()); + usage(); + return EXIT_FAILURE; + } + + switch((argv[i + 1][0] << 8) + argv[i + 1][1]) { + case ('n' << 8) + 'o': + pin_config.g_flags = GPIO_INTR_NONE; + break; + case ('e' << 8) + 'r': + pin_config.g_flags = GPIO_INTR_EDGE_RISING; + break; + case ('e' << 8) + 'f': + pin_config.g_flags = GPIO_INTR_EDGE_FALLING; + break; + case ('e' << 8) + 'b': + pin_config.g_flags = GPIO_INTR_EDGE_BOTH; + break; + default: + fprintf(stderr, "%s: Invalid trigger type.\n", + getprogname()); + usage(); + return EXIT_FAILURE; + } + + pin_config.g_flags |= GPIO_PIN_INPUT | GPIO_PIN_PULLUP; + + res = gpio_pin_set_flags(handle, &pin_config); + if (res < 0) + err(EXIT_FAILURE, "configuration of pin %d on %s " + "failed (flags=%d)", pin_config.g_pin, file, + pin_config.g_flags); + } + + switch (method) { + case 'r': + run_read(loop, handle, file, delayus); + break; + case 'p': + run_poll(loop, handle, file, timeout, delayus); + break; + case 's': + run_select(loop, handle, file, timeout, delayus); + break; + case 'k': + run_kqueue(loop, handle, file, timeout, delayus); + break; + case 'a': + run_aio_read(loop, handle, file, delayus); + break; + case 'i': + run_sigio(loop, handle, file); + break; + default: + fprintf(stderr, "%s: Unknown method.\n", getprogname()); + usage(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} Property changes on: stable/12/tools/test/gpioevents/gpioevents.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/12/usr.sbin/gpioctl/gpioctl.c =================================================================== --- stable/12/usr.sbin/gpioctl/gpioctl.c (revision 368701) +++ stable/12/usr.sbin/gpioctl/gpioctl.c (revision 368702) @@ -1,380 +1,382 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009, Oleksandr Tymoshenko * Copyright (c) 2014, Rui Paulo * Copyright (c) 2015, Emmanuel Vadot * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #define PIN_TYPE_NUMBER 1 #define PIN_TYPE_NAME 2 struct flag_desc { const char *name; uint32_t flag; }; static struct flag_desc gpio_flags[] = { { "IN", GPIO_PIN_INPUT }, { "OUT", GPIO_PIN_OUTPUT }, { "OD", GPIO_PIN_OPENDRAIN }, { "PP", GPIO_PIN_PUSHPULL }, { "TS", GPIO_PIN_TRISTATE }, { "PU", GPIO_PIN_PULLUP }, { "PD", GPIO_PIN_PULLDOWN }, { "II", GPIO_PIN_INVIN }, { "IO", GPIO_PIN_INVOUT }, { "PULSE", GPIO_PIN_PULSATE }, { "INTRLL", GPIO_INTR_LEVEL_LOW}, { "INTRLH", GPIO_INTR_LEVEL_HIGH}, { "INTRER", GPIO_INTR_EDGE_RISING}, { "INTREF", GPIO_INTR_EDGE_FALLING}, { "INTREB", GPIO_INTR_EDGE_BOTH}, { NULL, 0 }, }; int str2cap(const char *str); static void usage(void) { fprintf(stderr, "Usage:\n"); fprintf(stderr, "\tgpioctl [-f ctldev] -l [-v]\n"); fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -t pin\n"); fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -c pin flag ...\n"); fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -n pin pin-name\n"); fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] pin [0|1]\n"); exit(1); } static const char * cap2str(uint32_t cap) { struct flag_desc * pdesc = gpio_flags; while (pdesc->name) { if (pdesc->flag == cap) return pdesc->name; pdesc++; } return "UNKNOWN"; } int str2cap(const char *str) { struct flag_desc * pdesc = gpio_flags; while (pdesc->name) { if (strcasecmp(str, pdesc->name) == 0) return pdesc->flag; pdesc++; } return (-1); } /* * Our handmade function for converting string to number */ static int str2int(const char *s, int *ok) { char *endptr; int res = strtod(s, &endptr); if (endptr != s + strlen(s) ) *ok = 0; else *ok = 1; return res; } static void print_caps(int caps) { int i, need_coma; need_coma = 0; printf("<"); for (i = 0; i < 32; i++) { if (caps & (1 << i)) { if (need_coma) printf(","); printf("%s", cap2str(1 << i)); need_coma = 1; } } printf(">"); } static void dump_pins(gpio_handle_t handle, int verbose) { int i, maxpin, pinv; gpio_config_t *cfgs; gpio_config_t *pin; maxpin = gpio_pin_list(handle, &cfgs); if (maxpin < 0) { perror("gpio_pin_list"); exit(1); } for (i = 0; i <= maxpin; i++) { pin = cfgs + i; pinv = gpio_pin_get(handle, pin->g_pin); printf("pin %02d:\t%d\t%s", pin->g_pin, pinv, pin->g_name); print_caps(pin->g_flags); if (verbose) { printf(", caps:"); print_caps(pin->g_caps); } printf("\n"); } free(cfgs); } static int get_pinnum_by_name(gpio_handle_t handle, const char *name) { int i, maxpin, pinn; gpio_config_t *cfgs; gpio_config_t *pin; pinn = -1; maxpin = gpio_pin_list(handle, &cfgs); if (maxpin < 0) { perror("gpio_pin_list"); exit(1); } for (i = 0; i <= maxpin; i++) { pin = cfgs + i; gpio_pin_get(handle, pin->g_pin); if (!strcmp(name, pin->g_name)) { pinn = i; break; } } free(cfgs); return pinn; } static void fail(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); exit(1); } int main(int argc, char **argv) { int i; gpio_config_t pin; gpio_handle_t handle; char *ctlfile = NULL; int pinn, pinv, pin_type, ch; int flags, flag, ok; int config, list, name, toggle, verbose; config = toggle = verbose = list = name = pin_type = 0; while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) { switch (ch) { case 'c': config = 1; break; case 'f': ctlfile = optarg; break; case 'l': list = 1; break; case 'n': name = 1; break; case 'N': pin_type = PIN_TYPE_NAME; break; case'p': pin_type = PIN_TYPE_NUMBER; break; case 't': toggle = 1; break; case 'v': verbose = 1; break; default: usage(); break; } } argv += optind; argc -= optind; if (ctlfile == NULL) handle = gpio_open(0); else handle = gpio_open_device(ctlfile); if (handle == GPIO_INVALID_HANDLE) { perror("gpio_open"); exit(1); } if (list) { dump_pins(handle, verbose); gpio_close(handle); exit(0); } if (argc == 0) usage(); /* Find the pin number by the name */ switch (pin_type) { default: /* First test if it is a pin number */ pinn = str2int(argv[0], &ok); if (ok) { /* Test if we have any pin named by this number and tell the user */ if (get_pinnum_by_name(handle, argv[0]) != -1) fail("%s is also a pin name, use -p or -N\n", argv[0]); } else { /* Test if it is a name */ if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1) fail("Can't find pin named \"%s\"\n", argv[0]); } break; case PIN_TYPE_NUMBER: pinn = str2int(argv[0], &ok); if (!ok) fail("Invalid pin number: %s\n", argv[0]); break; case PIN_TYPE_NAME: if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1) fail("Can't find pin named \"%s\"\n", argv[0]); break; } /* Set the pin name. */ if (name) { if (argc != 2) usage(); if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) { perror("gpio_pin_set_name"); exit(1); } exit(0); } if (toggle) { /* * -t pin assumes no additional arguments */ if (argc > 1) usage(); if (gpio_pin_toggle(handle, pinn) < 0) { perror("gpio_pin_toggle"); exit(1); } gpio_close(handle); exit(0); } if (config) { flags = 0; for (i = 1; i < argc; i++) { flag = str2cap(argv[i]); if (flag < 0) fail("Invalid flag: %s\n", argv[i]); + else if ((flag & GPIO_INTR_MASK) != 0) + fail("Interrupt capability %s cannot be set as configuration flag\n", argv[i]); flags |= flag; } pin.g_pin = pinn; pin.g_flags = flags; if (gpio_pin_set_flags(handle, &pin) < 0) { perror("gpio_pin_set_flags"); exit(1); } exit(0); } /* * Last two cases - set value or print value */ if ((argc == 0) || (argc > 2)) usage(); /* * Read pin value */ if (argc == 1) { pinv = gpio_pin_get(handle, pinn); if (pinv < 0) { perror("gpio_pin_get"); exit(1); } printf("%d\n", pinv); exit(0); } /* Is it valid number (0 or 1) ? */ pinv = str2int(argv[1], &ok); if (ok == 0 || ((pinv != 0) && (pinv != 1))) fail("Invalid pin value: %s\n", argv[1]); /* * Set pin value */ if (gpio_pin_set(handle, pinn, pinv) < 0) { perror("gpio_pin_set"); exit(1); } gpio_close(handle); exit(0); } Index: stable/12 =================================================================== --- stable/12 (revision 368701) +++ stable/12 (revision 368702) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r368585