^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/build_bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/irqreturn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/timekeeping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <uapi/linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "gpiolib-cdev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Array sizes must ensure 64-bit alignment and not create holes in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * struct packing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Character device interface to GPIO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * The GPIO character device, /dev/gpiochipN, provides userspace an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * interface to gpiolib GPIOs via ioctl()s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * GPIO line handle management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * struct linehandle_state - contains the state of a userspace handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @gdev: the GPIO device the handle pertains to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @label: consumer label used to tag descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @descs: the GPIO descriptors held by this handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @num_descs: the number of descriptors held in the descs array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct linehandle_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const char *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct gpio_desc *descs[GPIOHANDLES_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u32 num_descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define GPIOHANDLE_REQUEST_VALID_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) (GPIOHANDLE_REQUEST_INPUT | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) GPIOHANDLE_REQUEST_OUTPUT | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) GPIOHANDLE_REQUEST_ACTIVE_LOW | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) GPIOHANDLE_REQUEST_BIAS_DISABLE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) GPIOHANDLE_REQUEST_OPEN_DRAIN | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) GPIOHANDLE_REQUEST_OPEN_SOURCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int linehandle_validate_flags(u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Return an error if an unknown flag is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Do not allow both INPUT & OUTPUT flags to be set as they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * contradictory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) (flags & GPIOHANDLE_REQUEST_OUTPUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * the hardware actually supports enabling both at the same time the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * electrical result would be disastrous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Bias flags only allowed for input or output mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Only one bias flag can be set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) assign_bit(FLAG_ACTIVE_LOW, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) assign_bit(FLAG_OPEN_DRAIN, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) assign_bit(FLAG_OPEN_SOURCE, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) assign_bit(FLAG_PULL_UP, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) assign_bit(FLAG_PULL_DOWN, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) assign_bit(FLAG_BIAS_DISABLE, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static long linehandle_set_config(struct linehandle_state *lh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct gpiohandle_config gcnf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u32 lflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) lflags = gcnf.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = linehandle_validate_flags(lflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) for (i = 0; i < lh->num_descs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) desc = lh->descs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Lines have to be requested explicitly for input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * or output, else the line will be treated "as is".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int val = !!gcnf.default_values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = gpiod_direction_output(desc, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ret = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) blocking_notifier_call_chain(&desc->gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) GPIO_V2_LINE_CHANGED_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static long linehandle_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct linehandle_state *lh = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) void __user *ip = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct gpiohandle_data ghd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* NOTE: It's ok to read values of output lines. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int ret = gpiod_get_array_value_complex(false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) lh->num_descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) lh->descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) memset(&ghd, 0, sizeof(ghd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) for (i = 0; i < lh->num_descs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ghd.values[i] = test_bit(i, vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (copy_to_user(ip, &ghd, sizeof(ghd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * All line descriptors were created at once with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * flags so just check if the first one is really output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (copy_from_user(&ghd, ip, sizeof(ghd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Clamp all values to [0,1] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) for (i = 0; i < lh->num_descs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) __assign_bit(i, vals, ghd.values[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* Reuse the array setting function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return gpiod_set_array_value_complex(false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) lh->num_descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) lh->descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return linehandle_set_config(lh, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void linehandle_free(struct linehandle_state *lh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) for (i = 0; i < lh->num_descs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (lh->descs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) gpiod_free(lh->descs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kfree(lh->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) put_device(&lh->gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) kfree(lh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int linehandle_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) linehandle_free(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static const struct file_operations linehandle_fileops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .release = linehandle_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .unlocked_ioctl = linehandle_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .compat_ioctl = linehandle_ioctl_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int linehandle_create(struct gpio_device *gdev, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct gpiohandle_request handlereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct linehandle_state *lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int fd, i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) u32 lflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) lflags = handlereq.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = linehandle_validate_flags(lflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) lh = kzalloc(sizeof(*lh), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!lh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) lh->gdev = gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (handlereq.consumer_label[0] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* label is only initialized if consumer_label is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) lh->label = kstrndup(handlereq.consumer_label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) sizeof(handlereq.consumer_label) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!lh->label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) lh->num_descs = handlereq.lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* Request each GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) for (i = 0; i < handlereq.lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) u32 offset = handlereq.lineoffsets[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = gpiod_request(desc, lh->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) lh->descs[i] = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = gpiod_set_transitory(desc, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * Lines have to be requested explicitly for input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * or output, else the line will be treated "as is".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int val = !!handlereq.default_values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) ret = gpiod_direction_output(desc, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) blocking_notifier_call_chain(&desc->gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) GPIO_V2_LINE_CHANGED_REQUESTED, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) ret = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto out_free_lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) file = anon_inode_getfile("gpio-linehandle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) &linehandle_fileops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) lh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto out_put_unused_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) handlereq.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * fput() will trigger the release() callback, so do not go onto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * the regular error cleanup path here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) fd_install(fd, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) lh->num_descs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) out_put_unused_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) out_free_lh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) linehandle_free(lh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #endif /* CONFIG_GPIO_CDEV_V1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * struct line - contains the state of a requested line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * @desc: the GPIO descriptor for this line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @req: the corresponding line request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @irq: the interrupt triggered in response to events on this GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * @timestamp_ns: cache for the timestamp storing it between hardirq and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * IRQ thread, used to bring the timestamp close to the actual event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * @req_seqno: the seqno for the current edge event in the sequence of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * events for the corresponding line request. This is drawn from the @req.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * @line_seqno: the seqno for the current edge event in the sequence of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * events for this line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @work: the worker that implements software debouncing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @sw_debounced: flag indicating if the software debouncer is active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * @level: the current debounced physical level of the line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct line {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * -- edge detector specific fields --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct linereq *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) u64 eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * timestamp_ns and req_seqno are accessed only by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * edge_irq_handler() and edge_irq_thread(), which are themselves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * mutually exclusive, so no additional protection is necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) u64 timestamp_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) u32 req_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * line_seqno is accessed by either edge_irq_thread() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * debounce_work_func(), which are themselves mutually exclusive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * so no additional protection is necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) u32 line_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * -- debouncer specific fields --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * sw_debounce is accessed by linereq_set_config(), which is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * only setter, and linereq_get_values(), which can live with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * slightly stale value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) unsigned int sw_debounced;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * level is accessed by debounce_work_func(), which is the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * setter, and linereq_get_values() which can live with a slightly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * stale value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) unsigned int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * struct linereq - contains the state of a userspace line request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @gdev: the GPIO device the line request pertains to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @label: consumer label used to tag GPIO descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * @num_lines: the number of lines in the lines array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * @wait: wait queue that handles blocking reads of events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * @event_buffer_size: the number of elements allocated in @events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * @events: KFIFO for the GPIO events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * @seqno: the sequence number for edge events generated on all lines in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * this line request. Note that this is not used when @num_lines is 1, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * the line_seqno is then the same and is cheaper to calculate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * of configuration, particularly multi-step accesses to desc flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * @lines: the lines held by this line request, with @num_lines elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct linereq {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) const char *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) u32 num_lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) u32 event_buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) atomic_t seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct mutex config_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct line lines[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) #define GPIO_V2_LINE_BIAS_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) GPIO_V2_LINE_FLAG_BIAS_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) #define GPIO_V2_LINE_DIRECTION_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) (GPIO_V2_LINE_FLAG_INPUT | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) GPIO_V2_LINE_FLAG_OUTPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) #define GPIO_V2_LINE_DRIVE_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) GPIO_V2_LINE_FLAG_OPEN_SOURCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) #define GPIO_V2_LINE_EDGE_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) (GPIO_V2_LINE_FLAG_EDGE_RISING | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) GPIO_V2_LINE_FLAG_EDGE_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) #define GPIO_V2_LINE_VALID_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) GPIO_V2_LINE_DIRECTION_FLAGS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) GPIO_V2_LINE_DRIVE_FLAGS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) GPIO_V2_LINE_EDGE_FLAGS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) GPIO_V2_LINE_BIAS_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static void linereq_put_event(struct linereq *lr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct gpio_v2_line_event *le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) bool overflow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) spin_lock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (kfifo_is_full(&lr->events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) overflow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) kfifo_skip(&lr->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) kfifo_in(&lr->events, le, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) spin_unlock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) wake_up_poll(&lr->wait, EPOLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) pr_debug_ratelimited("event FIFO is full - event dropped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static irqreturn_t edge_irq_thread(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct line *line = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct linereq *lr = line->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct gpio_v2_line_event le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* Do not leak kernel stack to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) memset(&le, 0, sizeof(le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (line->timestamp_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) le.timestamp_ns = line->timestamp_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * We may be running from a nested threaded interrupt in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * which case we didn't get the timestamp from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * edge_irq_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) le.timestamp_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (lr->num_lines != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) line->req_seqno = atomic_inc_return(&lr->seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) line->timestamp_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) GPIO_V2_LINE_FLAG_EDGE_FALLING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) int level = gpiod_get_value_cansleep(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /* Emit low-to-high event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /* Emit high-to-low event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) /* Emit low-to-high event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* Emit high-to-low event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) line->line_seqno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) le.line_seqno = line->line_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) le.offset = gpio_chip_hwgpio(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) linereq_put_event(lr, &le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static irqreturn_t edge_irq_handler(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct line *line = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct linereq *lr = line->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * Just store the timestamp in hardirq context so we get it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * close in time as possible to the actual event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) line->timestamp_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (lr->num_lines != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) line->req_seqno = atomic_inc_return(&lr->seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return IRQ_WAKE_THREAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * returns the current debounced logical value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static bool debounced_value(struct line *line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) bool value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * minor race - debouncer may be stopped here, so edge_detector_stop()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * must leave the value unchanged so the following will read the level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * from when the debouncer was last running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) value = READ_ONCE(line->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) value = !value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static irqreturn_t debounce_irq_handler(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct line *line = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) mod_delayed_work(system_wq, &line->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static void debounce_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct gpio_v2_line_event le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct line *line = container_of(work, struct line, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) struct linereq *lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) level = gpiod_get_raw_value_cansleep(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (level < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) pr_debug_ratelimited("debouncer failed to read line value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (READ_ONCE(line->level) == level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) WRITE_ONCE(line->level, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /* -- edge detection -- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!line->eflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /* switch from physical level to logical - if they differ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) level = !level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /* ignore edges that are not being monitored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /* Do not leak kernel stack to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) memset(&le, 0, sizeof(le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) lr = line->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) le.timestamp_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) le.offset = gpio_chip_hwgpio(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) line->line_seqno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) le.line_seqno = line->line_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) le.seqno = (lr->num_lines == 1) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) le.line_seqno : atomic_inc_return(&lr->seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /* Emit low-to-high event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /* Emit high-to-low event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) linereq_put_event(lr, &le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static int debounce_setup(struct line *line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) unsigned int debounce_period_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int ret, level, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* try hardware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ret = gpiod_set_debounce(line->desc, debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (ret != -ENOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (debounce_period_us) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /* setup software debounce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) level = gpiod_get_raw_value_cansleep(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (level < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) irq = gpiod_to_irq(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) WRITE_ONCE(line->level, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) ret = request_irq(irq, debounce_irq_handler, irqflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) line->req->label, line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) WRITE_ONCE(line->sw_debounced, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) line->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) unsigned int line_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) u64 mask = BIT_ULL(line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) for (i = 0; i < lc->num_attrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) (lc->attrs[i].mask & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) unsigned int line_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) u64 mask = BIT_ULL(line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) for (i = 0; i < lc->num_attrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) (lc->attrs[i].mask & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return lc->attrs[i].attr.debounce_period_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static void edge_detector_stop(struct line *line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (line->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) free_irq(line->irq, line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) line->irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) cancel_delayed_work_sync(&line->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) WRITE_ONCE(line->sw_debounced, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) line->eflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (line->desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) WRITE_ONCE(line->desc->debounce_period_us, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) /* do not change line->level - see comment in debounced_value() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static int edge_detector_setup(struct line *line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) unsigned int line_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) u64 eflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) u32 debounce_period_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) unsigned long irqflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) int irq, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (eflags && !kfifo_initialized(&line->req->events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) ret = kfifo_alloc(&line->req->events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) line->req->event_buffer_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) line->eflags = eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (gpio_v2_line_config_debounced(lc, line_idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) ret = debounce_setup(line, debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /* detection disabled or sw debouncer will provide edge detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (!eflags || READ_ONCE(line->sw_debounced))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) irq = gpiod_to_irq(line->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) irqflags |= IRQF_ONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /* Request a thread to read the events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) irqflags, line->req->label, line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) line->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) static int edge_detector_update(struct line *line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) unsigned int line_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) u64 eflags, bool polarity_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) unsigned int debounce_period_us =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) gpio_v2_line_config_debounce_period(lc, line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if ((line->eflags == eflags) && !polarity_change &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) /* sw debounced and still will be...*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) line->eflags = eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* reconfiguring edge detection or sw debounce being disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) (!debounce_period_us && READ_ONCE(line->sw_debounced)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) edge_detector_stop(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) return edge_detector_setup(line, lc, line_idx, eflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) unsigned int line_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) u64 mask = BIT_ULL(line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) for (i = 0; i < lc->num_attrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) (lc->attrs[i].mask & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return lc->attrs[i].attr.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return lc->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) unsigned int line_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) u64 mask = BIT_ULL(line_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) for (i = 0; i < lc->num_attrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) (lc->attrs[i].mask & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return !!(lc->attrs[i].attr.values & mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) static int gpio_v2_line_flags_validate(u64 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) /* Return an error if an unknown flag is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * Do not allow both INPUT and OUTPUT flags to be set as they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * contradictory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) (flags & GPIO_V2_LINE_FLAG_OUTPUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) /* Edge detection requires explicit input. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) !(flags & GPIO_V2_LINE_FLAG_INPUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * request. If the hardware actually supports enabling both at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * same time the electrical result would be disastrous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) /* Drive requires explicit output direction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /* Bias requires explicit direction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) /* Only one bias flag can be set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) unsigned int num_lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) for (i = 0; i < num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) flags = gpio_v2_line_config_flags(lc, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) ret = gpio_v2_line_flags_validate(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) /* debounce requires explicit input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (gpio_v2_line_config_debounced(lc, i) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) !(flags & GPIO_V2_LINE_FLAG_INPUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) unsigned long *flagsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) assign_bit(FLAG_ACTIVE_LOW, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) set_bit(FLAG_IS_OUT, flagsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) else if (flags & GPIO_V2_LINE_FLAG_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) clear_bit(FLAG_IS_OUT, flagsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) assign_bit(FLAG_EDGE_RISING, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) assign_bit(FLAG_EDGE_FALLING, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) assign_bit(FLAG_OPEN_DRAIN, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) assign_bit(FLAG_OPEN_SOURCE, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) assign_bit(FLAG_PULL_UP, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) assign_bit(FLAG_PULL_DOWN, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) assign_bit(FLAG_BIAS_DISABLE, flagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) static long linereq_get_values(struct linereq *lr, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct gpio_v2_line_values lv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct gpio_desc **descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) unsigned int i, didx, num_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) bool val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /* NOTE: It's ok to read values of output lines. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (copy_from_user(&lv, ip, sizeof(lv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) for (num_get = 0, i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (lv.mask & BIT_ULL(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) num_get++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) descs = &lr->lines[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (num_get == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (num_get != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (!descs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) for (didx = 0, i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (lv.mask & BIT_ULL(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) descs[didx] = lr->lines[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) didx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) ret = gpiod_get_array_value_complex(false, true, num_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) descs, NULL, vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (num_get != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) kfree(descs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) lv.bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) for (didx = 0, i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (lv.mask & BIT_ULL(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (lr->lines[i].sw_debounced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) val = debounced_value(&lr->lines[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) val = test_bit(didx, vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) lv.bits |= BIT_ULL(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) didx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) if (copy_to_user(ip, &lv, sizeof(lv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) static long linereq_set_values_unlocked(struct linereq *lr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) struct gpio_v2_line_values *lv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) struct gpio_desc **descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) unsigned int i, didx, num_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) bitmap_zero(vals, GPIO_V2_LINES_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) for (num_set = 0, i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (lv->mask & BIT_ULL(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (lv->bits & BIT_ULL(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) __set_bit(num_set, vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) num_set++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) descs = &lr->lines[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (num_set == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (num_set != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) /* build compacted desc array and values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (!descs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) for (didx = 0, i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) if (lv->mask & BIT_ULL(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) descs[didx] = lr->lines[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) didx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) ret = gpiod_set_array_value_complex(false, true, num_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) descs, NULL, vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (num_set != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) kfree(descs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static long linereq_set_values(struct linereq *lr, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct gpio_v2_line_values lv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (copy_from_user(&lv, ip, sizeof(lv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) mutex_lock(&lr->config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) ret = linereq_set_values_unlocked(lr, &lv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) mutex_unlock(&lr->config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) static long linereq_set_config_unlocked(struct linereq *lr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) struct gpio_v2_line_config *lc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) bool polarity_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) for (i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) desc = lr->lines[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) flags = gpio_v2_line_config_flags(lc, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) polarity_change =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) * Lines have to be requested explicitly for input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) * or output, else the line will be treated "as is".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) int val = gpio_v2_line_config_output_value(lc, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) edge_detector_stop(&lr->lines[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) ret = gpiod_direction_output(desc, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) ret = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) ret = edge_detector_update(&lr->lines[i], lc, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) flags & GPIO_V2_LINE_EDGE_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) polarity_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) blocking_notifier_call_chain(&desc->gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) GPIO_V2_LINE_CHANGED_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static long linereq_set_config(struct linereq *lr, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) struct gpio_v2_line_config lc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (copy_from_user(&lc, ip, sizeof(lc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) mutex_lock(&lr->config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) ret = linereq_set_config_unlocked(lr, &lc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) mutex_unlock(&lr->config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) static long linereq_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) struct linereq *lr = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) void __user *ip = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) return linereq_get_values(lr, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return linereq_set_values(lr, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) return linereq_set_config(lr, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static __poll_t linereq_poll(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct linereq *lr = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) __poll_t events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) poll_wait(file, &lr->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) &lr->wait.lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) events = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) return events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) static ssize_t linereq_read(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) loff_t *f_ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) struct linereq *lr = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) struct gpio_v2_line_event le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) if (count < sizeof(le))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) spin_lock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (kfifo_is_empty(&lr->events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (bytes_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) spin_unlock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) spin_unlock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) ret = wait_event_interruptible_locked(lr->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) !kfifo_is_empty(&lr->events));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) spin_unlock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) ret = kfifo_out(&lr->events, &le, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) spin_unlock(&lr->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) * This should never happen - we were holding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * lock from the moment we learned the fifo is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * longer empty until now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) bytes_read += sizeof(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) } while (count >= bytes_read + sizeof(le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) static void linereq_free(struct linereq *lr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) for (i = 0; i < lr->num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) edge_detector_stop(&lr->lines[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) if (lr->lines[i].desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) gpiod_free(lr->lines[i].desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) kfifo_free(&lr->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) kfree(lr->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) put_device(&lr->gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) kfree(lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) static int linereq_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) struct linereq *lr = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) linereq_free(lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) static const struct file_operations line_fileops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) .release = linereq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) .read = linereq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) .poll = linereq_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) .unlocked_ioctl = linereq_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) .compat_ioctl = linereq_ioctl_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static int linereq_create(struct gpio_device *gdev, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) struct gpio_v2_line_request ulr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) struct gpio_v2_line_config *lc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) struct linereq *lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) int fd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (copy_from_user(&ulr, ip, sizeof(ulr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) lc = &ulr.config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (!lr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) lr->gdev = gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) for (i = 0; i < ulr.num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) lr->lines[i].req = lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) WRITE_ONCE(lr->lines[i].sw_debounced, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) if (ulr.consumer[0] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) /* label is only initialized if consumer is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) if (!lr->label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) mutex_init(&lr->config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) init_waitqueue_head(&lr->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) lr->event_buffer_size = ulr.event_buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (lr->event_buffer_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) lr->event_buffer_size = ulr.num_lines * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) atomic_set(&lr->seqno, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) lr->num_lines = ulr.num_lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) /* Request each GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) for (i = 0; i < ulr.num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) u32 offset = ulr.offsets[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) ret = PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) ret = gpiod_request(desc, lr->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) lr->lines[i].desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) flags = gpio_v2_line_config_flags(lc, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ret = gpiod_set_transitory(desc, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) * Lines have to be requested explicitly for input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) * or output, else the line will be treated "as is".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) int val = gpio_v2_line_config_output_value(lc, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) ret = gpiod_direction_output(desc, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) ret = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) ret = edge_detector_setup(&lr->lines[i], lc, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) flags & GPIO_V2_LINE_EDGE_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) blocking_notifier_call_chain(&desc->gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) GPIO_V2_LINE_CHANGED_REQUESTED, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) ret = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) goto out_free_linereq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) file = anon_inode_getfile("gpio-line", &line_fileops, lr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) ret = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) goto out_put_unused_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) ulr.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) if (copy_to_user(ip, &ulr, sizeof(ulr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) * fput() will trigger the release() callback, so do not go onto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) * the regular error cleanup path here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) fd_install(fd, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) lr->num_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) out_put_unused_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) out_free_linereq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) linereq_free(lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) * GPIO line event management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) * struct lineevent_state - contains the state of a userspace event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) * @gdev: the GPIO device the event pertains to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * @label: consumer label used to tag descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * @desc: the GPIO descriptor held by this event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) * @eflags: the event flags this line was requested with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) * @irq: the interrupt that trigger in response to events on this GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) * @wait: wait queue that handles blocking reads of events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) * @events: KFIFO for the GPIO events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) * @timestamp: cache for the timestamp storing it between hardirq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) * and IRQ thread, used to bring the timestamp close to the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) * event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) struct lineevent_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) const char *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) u32 eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) DECLARE_KFIFO(events, struct gpioevent_data, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) u64 timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) #define GPIOEVENT_REQUEST_VALID_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) (GPIOEVENT_REQUEST_RISING_EDGE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) GPIOEVENT_REQUEST_FALLING_EDGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) static __poll_t lineevent_poll(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) struct lineevent_state *le = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) __poll_t events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) poll_wait(file, &le->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) events = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) return events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) static ssize_t lineevent_get_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) /* i386 has no padding after 'id' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) if (in_ia32_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) struct compat_gpioeevent_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) compat_u64 timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return sizeof(struct compat_gpioeevent_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) return sizeof(struct gpioevent_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) static ssize_t lineevent_read(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) loff_t *f_ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) struct lineevent_state *le = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) struct gpioevent_data ge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) ssize_t ge_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) * When compatible system call is being used the struct gpioevent_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) * in case of at least ia32, has different size due to the alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) * differences. Because we have first member 64 bits followed by one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) * 32 bits there is no gap between them. The only difference is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) * padding at the end of the data structure. Hence, we calculate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) * actual sizeof() and pass this as an argument to copy_to_user() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) * drop unneeded bytes from the output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) ge_size = lineevent_get_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (count < ge_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) spin_lock(&le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) if (kfifo_is_empty(&le->events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) if (bytes_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) spin_unlock(&le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) spin_unlock(&le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) ret = wait_event_interruptible_locked(le->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) !kfifo_is_empty(&le->events));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) spin_unlock(&le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) ret = kfifo_out(&le->events, &ge, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) spin_unlock(&le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) * This should never happen - we were holding the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * from the moment we learned the fifo is no longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * empty until now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) if (copy_to_user(buf + bytes_read, &ge, ge_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) bytes_read += ge_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) } while (count >= bytes_read + ge_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) static void lineevent_free(struct lineevent_state *le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) if (le->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) free_irq(le->irq, le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) if (le->desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) gpiod_free(le->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) kfree(le->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) put_device(&le->gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) kfree(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) static int lineevent_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) lineevent_free(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) static long lineevent_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) struct lineevent_state *le = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) void __user *ip = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) struct gpiohandle_data ghd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) * We can get the value for an event line but not set it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) * because it is input by definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) memset(&ghd, 0, sizeof(ghd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) val = gpiod_get_value_cansleep(le->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) ghd.values[0] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) if (copy_to_user(ip, &ghd, sizeof(ghd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) static const struct file_operations lineevent_fileops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) .release = lineevent_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) .read = lineevent_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) .poll = lineevent_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) .unlocked_ioctl = lineevent_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) .compat_ioctl = lineevent_ioctl_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) static irqreturn_t lineevent_irq_thread(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) struct lineevent_state *le = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) struct gpioevent_data ge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) /* Do not leak kernel stack to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) memset(&ge, 0, sizeof(ge));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) * We may be running from a nested threaded interrupt in which case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) * we didn't get the timestamp from lineevent_irq_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (!le->timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) ge.timestamp = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) ge.timestamp = le->timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) int level = gpiod_get_value_cansleep(le->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) /* Emit low-to-high event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) ge.id = GPIOEVENT_EVENT_RISING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) /* Emit high-to-low event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) /* Emit low-to-high event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) ge.id = GPIOEVENT_EVENT_RISING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) /* Emit high-to-low event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 1, &le->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) wake_up_poll(&le->wait, EPOLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) pr_debug_ratelimited("event FIFO is full - event dropped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) static irqreturn_t lineevent_irq_handler(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) struct lineevent_state *le = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) * Just store the timestamp in hardirq context so we get it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) * close in time as possible to the actual event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) le->timestamp = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) return IRQ_WAKE_THREAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) static int lineevent_create(struct gpio_device *gdev, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) struct gpioevent_request eventreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) struct lineevent_state *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) u32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) u32 lflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) u32 eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) int irq, irqflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) offset = eventreq.lineoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) lflags = eventreq.handleflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) eflags = eventreq.eventflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) desc = gpiochip_get_desc(gdev->chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) /* Return an error if a unknown flag is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) /* This is just wrong: we don't look for events on output lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) /* Only one bias flag can be set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) le = kzalloc(sizeof(*le), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (!le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) le->gdev = gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) if (eventreq.consumer_label[0] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) /* label is only initialized if consumer_label is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) le->label = kstrndup(eventreq.consumer_label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) sizeof(eventreq.consumer_label) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) if (!le->label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) ret = gpiod_request(desc, le->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) le->desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) le->eflags = eflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) linehandle_flags_to_desc_flags(lflags, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) ret = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) blocking_notifier_call_chain(&desc->gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) GPIO_V2_LINE_CHANGED_REQUESTED, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) irq = gpiod_to_irq(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) if (irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) le->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) irqflags |= IRQF_ONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) INIT_KFIFO(le->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) init_waitqueue_head(&le->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) /* Request a thread to read the events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) ret = request_threaded_irq(le->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) lineevent_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) lineevent_irq_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) irqflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) le->label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) ret = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) goto out_free_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) file = anon_inode_getfile("gpio-event",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) &lineevent_fileops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) le,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) ret = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) goto out_put_unused_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) eventreq.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) * fput() will trigger the release() callback, so do not go onto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) * the regular error cleanup path here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) fd_install(fd, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) out_put_unused_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) out_free_le:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) lineevent_free(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) struct gpioline_info *info_v1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) u64 flagsv2 = info_v2->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) info_v1->line_offset = info_v2->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) info_v1->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) info_v1->flags |= GPIOLINE_FLAG_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) static void gpio_v2_line_info_changed_to_v1(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) struct gpio_v2_line_info_changed *lic_v2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) struct gpioline_info_changed *lic_v1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) memset(lic_v1, 0, sizeof(*lic_v1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) lic_v1->timestamp = lic_v2->timestamp_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) lic_v1->event_type = lic_v2->event_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) #endif /* CONFIG_GPIO_CDEV_V1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) struct gpio_v2_line_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) struct gpio_chip *gc = desc->gdev->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) bool ok_for_pinctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) u32 debounce_period_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) unsigned int num_attrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) memset(info, 0, sizeof(*info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) info->offset = gpio_chip_hwgpio(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) * This function takes a mutex so we must check this before taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) * the spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) * FIXME: find a non-racy way to retrieve this information. Maybe a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) * lock common to both frameworks?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) ok_for_pinctrl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) pinctrl_gpio_can_use_line(gc->base + info->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) spin_lock_irqsave(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (desc->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) strscpy(info->name, desc->name, sizeof(info->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (desc->label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) strscpy(info->consumer, desc->label, sizeof(info->consumer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) * Userspace only need to know that the kernel is using this GPIO so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) * it can't use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) info->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) if (test_bit(FLAG_REQUESTED, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) test_bit(FLAG_IS_HOGGED, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) test_bit(FLAG_EXPORT, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) test_bit(FLAG_SYSFS, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) !ok_for_pinctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) info->flags |= GPIO_V2_LINE_FLAG_USED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) if (test_bit(FLAG_IS_OUT, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) info->flags |= GPIO_V2_LINE_FLAG_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) if (test_bit(FLAG_PULL_DOWN, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) if (test_bit(FLAG_PULL_UP, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) if (test_bit(FLAG_EDGE_RISING, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) debounce_period_us = READ_ONCE(desc->debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) if (debounce_period_us) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) info->attrs[num_attrs].debounce_period_us = debounce_period_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) info->num_attrs = num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) spin_unlock_irqrestore(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) struct gpio_chardev_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) struct notifier_block lineinfo_changed_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) unsigned long *watched_lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) atomic_t watch_abi_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) struct gpio_device *gdev = cdev->gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) struct gpiochip_info chipinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) memset(&chipinfo, 0, sizeof(chipinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) chipinfo.lines = gdev->ngpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) * returns 0 if the versions match, else the previously selected ABI version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) unsigned int version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) if (abiv == version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) return abiv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) bool watch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) struct gpioline_info lineinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) struct gpio_v2_line_info lineinfo_v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) /* this doubles as a range check on line_offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) if (watch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) if (lineinfo_ensure_abi_version(cdev, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) gpio_desc_to_lineinfo(desc, &lineinfo_v2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) if (watch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) clear_bit(lineinfo.line_offset, cdev->watched_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) bool watch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) struct gpio_v2_line_info lineinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) if (watch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (lineinfo_ensure_abi_version(cdev, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) gpio_desc_to_lineinfo(desc, &lineinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) if (watch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) clear_bit(lineinfo.offset, cdev->watched_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) __u32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) if (copy_from_user(&offset, ip, sizeof(offset)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) if (offset >= cdev->gdev->ngpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) if (!test_and_clear_bit(offset, cdev->watched_lines))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) * gpio_ioctl() - ioctl handler for the GPIO chardev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) struct gpio_chardev_data *cdev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) struct gpio_device *gdev = cdev->gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) void __user *ip = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) /* We fail any subsequent ioctl():s when the chip is gone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) if (!gdev->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) /* Fill in the struct and pass to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) return chipinfo_get(cdev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) return linehandle_create(gdev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) return lineevent_create(gdev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) } else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) return lineinfo_get_v1(cdev, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) cmd == GPIO_GET_LINEINFO_WATCH_IOCTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) #endif /* CONFIG_GPIO_CDEV_V1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) return lineinfo_get(cdev, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) } else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) return linereq_create(gdev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) return lineinfo_unwatch(cdev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) static struct gpio_chardev_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) to_gpio_chardev_data(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) static int lineinfo_changed_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) struct gpio_v2_line_info_changed chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) struct gpio_desc *desc = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) memset(&chg, 0, sizeof(chg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) chg.event_type = action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) chg.timestamp_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) gpio_desc_to_lineinfo(desc, &chg.info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) wake_up_poll(&cdev->wait, EPOLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) static __poll_t lineinfo_watch_poll(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) struct poll_table_struct *pollt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) struct gpio_chardev_data *cdev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) __poll_t events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) poll_wait(file, &cdev->wait, pollt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) &cdev->wait.lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) events = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) return events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) size_t count, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) struct gpio_chardev_data *cdev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) struct gpio_v2_line_info_changed event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) size_t event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) #ifndef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) event_size = sizeof(struct gpio_v2_line_info_changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) if (count < event_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) spin_lock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) if (kfifo_is_empty(&cdev->events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) if (bytes_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) spin_unlock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) spin_unlock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) ret = wait_event_interruptible_locked(cdev->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) !kfifo_is_empty(&cdev->events));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) spin_unlock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) /* must be after kfifo check so watch_abi_version is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) if (atomic_read(&cdev->watch_abi_version) == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) event_size = sizeof(struct gpio_v2_line_info_changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) event_size = sizeof(struct gpioline_info_changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) if (count < event_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) spin_unlock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) ret = kfifo_out(&cdev->events, &event, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) spin_unlock(&cdev->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) /* We should never get here. See lineevent_read(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) #ifdef CONFIG_GPIO_CDEV_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) if (copy_to_user(buf + bytes_read, &event, event_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) struct gpioline_info_changed event_v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) gpio_v2_line_info_changed_to_v1(&event, &event_v1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) if (copy_to_user(buf + bytes_read, &event_v1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) event_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) if (copy_to_user(buf + bytes_read, &event, event_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) bytes_read += event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) } while (count >= bytes_read + sizeof(event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) * gpio_chrdev_open() - open the chardev for ioctl operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) * @inode: inode for this chardev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) * @file: file struct for storing private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) * Returns 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) static int gpio_chrdev_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) struct gpio_device *gdev = container_of(inode->i_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) struct gpio_device, chrdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) struct gpio_chardev_data *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) /* Fail on open if the backing gpiochip is gone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) if (!gdev->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) if (!cdev->watched_lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) goto out_free_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) init_waitqueue_head(&cdev->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) INIT_KFIFO(cdev->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) cdev->gdev = gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) ret = blocking_notifier_chain_register(&gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) &cdev->lineinfo_changed_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) goto out_free_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) file->private_data = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) ret = nonseekable_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) goto out_unregister_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) out_unregister_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) blocking_notifier_chain_unregister(&gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) &cdev->lineinfo_changed_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) out_free_bitmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) bitmap_free(cdev->watched_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) out_free_cdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) kfree(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) * gpio_chrdev_release() - close chardev after ioctl operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) * @inode: inode for this chardev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) * @file: file struct for storing private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) * Returns 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) static int gpio_chrdev_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) struct gpio_chardev_data *cdev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) struct gpio_device *gdev = cdev->gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) bitmap_free(cdev->watched_lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) blocking_notifier_chain_unregister(&gdev->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) &cdev->lineinfo_changed_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) put_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) kfree(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) static const struct file_operations gpio_fileops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) .release = gpio_chrdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) .open = gpio_chrdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) .poll = lineinfo_watch_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) .read = lineinfo_watch_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) .unlocked_ioctl = gpio_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) .compat_ioctl = gpio_ioctl_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) cdev_init(&gdev->chrdev, &gpio_fileops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) gdev->chrdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) MAJOR(devt), gdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) void gpiolib_cdev_unregister(struct gpio_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) cdev_device_del(&gdev->chrdev, &gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) }