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