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 | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 4 | #include <linux/bitmap.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 5 | #include <linux/cdev.h> |
| 6 | #include <linux/compat.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 9 | #include <linux/file.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 10 | #include <linux/gpio.h> |
| 11 | #include <linux/gpio/driver.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/irqreturn.h> |
| 14 | #include <linux/kernel.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 15 | #include <linux/kfifo.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/pinctrl/consumer.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 18 | #include <linux/poll.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 20 | #include <linux/timekeeping.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 22 | #include <uapi/linux/gpio.h> |
| 23 | |
| 24 | #include "gpiolib.h" |
| 25 | #include "gpiolib-cdev.h" |
| 26 | |
| 27 | /* Character device interface to GPIO. |
| 28 | * |
| 29 | * The GPIO character device, /dev/gpiochipN, provides userspace an |
| 30 | * interface to gpiolib GPIOs via ioctl()s. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * GPIO line handle management |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * struct linehandle_state - contains the state of a userspace handle |
| 39 | * @gdev: the GPIO device the handle pertains to |
| 40 | * @label: consumer label used to tag descriptors |
| 41 | * @descs: the GPIO descriptors held by this handle |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 42 | * @num_descs: the number of descriptors held in the descs array |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 43 | */ |
| 44 | struct linehandle_state { |
| 45 | struct gpio_device *gdev; |
| 46 | const char *label; |
| 47 | struct gpio_desc *descs[GPIOHANDLES_MAX]; |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 48 | u32 num_descs; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
| 52 | (GPIOHANDLE_REQUEST_INPUT | \ |
| 53 | GPIOHANDLE_REQUEST_OUTPUT | \ |
| 54 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ |
| 55 | GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ |
| 56 | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ |
| 57 | GPIOHANDLE_REQUEST_BIAS_DISABLE | \ |
| 58 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ |
| 59 | GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 60 | |
| 61 | static int linehandle_validate_flags(u32 flags) |
| 62 | { |
| 63 | /* Return an error if an unknown flag is set */ |
| 64 | if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | /* |
| 68 | * Do not allow both INPUT & OUTPUT flags to be set as they are |
| 69 | * contradictory. |
| 70 | */ |
| 71 | if ((flags & GPIOHANDLE_REQUEST_INPUT) && |
| 72 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | /* |
| 76 | * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If |
| 77 | * the hardware actually supports enabling both at the same time the |
| 78 | * electrical result would be disastrous. |
| 79 | */ |
| 80 | if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && |
| 81 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 82 | return -EINVAL; |
| 83 | |
| 84 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ |
| 85 | if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && |
| 86 | ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 87 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | /* Bias flags only allowed for input or output mode. */ |
| 91 | if (!((flags & GPIOHANDLE_REQUEST_INPUT) || |
| 92 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) && |
| 93 | ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || |
| 94 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || |
| 95 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | /* Only one bias flag can be set. */ |
| 99 | if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 100 | (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 101 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 102 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 103 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 109 | static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) |
| 110 | { |
| 111 | assign_bit(FLAG_ACTIVE_LOW, flagsp, |
| 112 | lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); |
| 113 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
| 114 | lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); |
| 115 | assign_bit(FLAG_OPEN_SOURCE, flagsp, |
| 116 | lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); |
| 117 | assign_bit(FLAG_PULL_UP, flagsp, |
| 118 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); |
| 119 | assign_bit(FLAG_PULL_DOWN, flagsp, |
| 120 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); |
| 121 | assign_bit(FLAG_BIAS_DISABLE, flagsp, |
| 122 | lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); |
| 123 | } |
| 124 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 125 | static long linehandle_set_config(struct linehandle_state *lh, |
| 126 | void __user *ip) |
| 127 | { |
| 128 | struct gpiohandle_config gcnf; |
| 129 | struct gpio_desc *desc; |
| 130 | int i, ret; |
| 131 | u32 lflags; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 132 | |
| 133 | if (copy_from_user(&gcnf, ip, sizeof(gcnf))) |
| 134 | return -EFAULT; |
| 135 | |
| 136 | lflags = gcnf.flags; |
| 137 | ret = linehandle_validate_flags(lflags); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 141 | for (i = 0; i < lh->num_descs; i++) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 142 | desc = lh->descs[i]; |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 143 | linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Lines have to be requested explicitly for input |
| 147 | * or output, else the line will be treated "as is". |
| 148 | */ |
| 149 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 150 | int val = !!gcnf.default_values[i]; |
| 151 | |
| 152 | ret = gpiod_direction_output(desc, val); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 156 | ret = gpiod_direction_input(desc); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | } |
| 160 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 161 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 162 | GPIOLINE_CHANGED_CONFIG, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 167 | static long linehandle_ioctl(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 168 | unsigned long arg) |
| 169 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 170 | struct linehandle_state *lh = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 171 | void __user *ip = (void __user *)arg; |
| 172 | struct gpiohandle_data ghd; |
| 173 | DECLARE_BITMAP(vals, GPIOHANDLES_MAX); |
| 174 | int i; |
| 175 | |
| 176 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 177 | /* NOTE: It's ok to read values of output lines. */ |
| 178 | int ret = gpiod_get_array_value_complex(false, |
| 179 | true, |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 180 | lh->num_descs, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 181 | lh->descs, |
| 182 | NULL, |
| 183 | vals); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | memset(&ghd, 0, sizeof(ghd)); |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 188 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 189 | ghd.values[i] = test_bit(i, vals); |
| 190 | |
| 191 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 192 | return -EFAULT; |
| 193 | |
| 194 | return 0; |
| 195 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
| 196 | /* |
| 197 | * All line descriptors were created at once with the same |
| 198 | * flags so just check if the first one is really output. |
| 199 | */ |
| 200 | if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) |
| 201 | return -EPERM; |
| 202 | |
| 203 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
| 204 | return -EFAULT; |
| 205 | |
| 206 | /* Clamp all values to [0,1] */ |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 207 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 208 | __assign_bit(i, vals, ghd.values[i]); |
| 209 | |
| 210 | /* Reuse the array setting function */ |
| 211 | return gpiod_set_array_value_complex(false, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 212 | true, |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 213 | lh->num_descs, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 214 | lh->descs, |
| 215 | NULL, |
| 216 | vals); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 217 | } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { |
| 218 | return linehandle_set_config(lh, ip); |
| 219 | } |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 224 | static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 225 | unsigned long arg) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 226 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 227 | return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 228 | } |
| 229 | #endif |
| 230 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 231 | static int linehandle_release(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 232 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 233 | struct linehandle_state *lh = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 234 | struct gpio_device *gdev = lh->gdev; |
| 235 | int i; |
| 236 | |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 237 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 238 | gpiod_free(lh->descs[i]); |
| 239 | kfree(lh->label); |
| 240 | kfree(lh); |
| 241 | put_device(&gdev->dev); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static const struct file_operations linehandle_fileops = { |
| 246 | .release = linehandle_release, |
| 247 | .owner = THIS_MODULE, |
| 248 | .llseek = noop_llseek, |
| 249 | .unlocked_ioctl = linehandle_ioctl, |
| 250 | #ifdef CONFIG_COMPAT |
| 251 | .compat_ioctl = linehandle_ioctl_compat, |
| 252 | #endif |
| 253 | }; |
| 254 | |
| 255 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) |
| 256 | { |
| 257 | struct gpiohandle_request handlereq; |
| 258 | struct linehandle_state *lh; |
| 259 | struct file *file; |
| 260 | int fd, i, count = 0, ret; |
| 261 | u32 lflags; |
| 262 | |
| 263 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
| 264 | return -EFAULT; |
| 265 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | lflags = handlereq.flags; |
| 269 | |
| 270 | ret = linehandle_validate_flags(lflags); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | |
| 274 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
| 275 | if (!lh) |
| 276 | return -ENOMEM; |
| 277 | lh->gdev = gdev; |
| 278 | get_device(&gdev->dev); |
| 279 | |
| 280 | /* Make sure this is terminated */ |
| 281 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; |
| 282 | if (strlen(handlereq.consumer_label)) { |
| 283 | lh->label = kstrdup(handlereq.consumer_label, |
| 284 | GFP_KERNEL); |
| 285 | if (!lh->label) { |
| 286 | ret = -ENOMEM; |
| 287 | goto out_free_lh; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* Request each GPIO */ |
| 292 | for (i = 0; i < handlereq.lines; i++) { |
| 293 | u32 offset = handlereq.lineoffsets[i]; |
| 294 | struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); |
| 295 | |
| 296 | if (IS_ERR(desc)) { |
| 297 | ret = PTR_ERR(desc); |
| 298 | goto out_free_descs; |
| 299 | } |
| 300 | |
| 301 | ret = gpiod_request(desc, lh->label); |
| 302 | if (ret) |
| 303 | goto out_free_descs; |
| 304 | lh->descs[i] = desc; |
| 305 | count = i + 1; |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 306 | linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 307 | |
| 308 | ret = gpiod_set_transitory(desc, false); |
| 309 | if (ret < 0) |
| 310 | goto out_free_descs; |
| 311 | |
| 312 | /* |
| 313 | * Lines have to be requested explicitly for input |
| 314 | * or output, else the line will be treated "as is". |
| 315 | */ |
| 316 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 317 | int val = !!handlereq.default_values[i]; |
| 318 | |
| 319 | ret = gpiod_direction_output(desc, val); |
| 320 | if (ret) |
| 321 | goto out_free_descs; |
| 322 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 323 | ret = gpiod_direction_input(desc); |
| 324 | if (ret) |
| 325 | goto out_free_descs; |
| 326 | } |
| 327 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 328 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 329 | GPIOLINE_CHANGED_REQUESTED, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 330 | |
| 331 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
| 332 | offset); |
| 333 | } |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 334 | lh->num_descs = handlereq.lines; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 335 | |
| 336 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
| 337 | if (fd < 0) { |
| 338 | ret = fd; |
| 339 | goto out_free_descs; |
| 340 | } |
| 341 | |
| 342 | file = anon_inode_getfile("gpio-linehandle", |
| 343 | &linehandle_fileops, |
| 344 | lh, |
| 345 | O_RDONLY | O_CLOEXEC); |
| 346 | if (IS_ERR(file)) { |
| 347 | ret = PTR_ERR(file); |
| 348 | goto out_put_unused_fd; |
| 349 | } |
| 350 | |
| 351 | handlereq.fd = fd; |
| 352 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
| 353 | /* |
| 354 | * fput() will trigger the release() callback, so do not go onto |
| 355 | * the regular error cleanup path here. |
| 356 | */ |
| 357 | fput(file); |
| 358 | put_unused_fd(fd); |
| 359 | return -EFAULT; |
| 360 | } |
| 361 | |
| 362 | fd_install(fd, file); |
| 363 | |
| 364 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 365 | lh->num_descs); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 366 | |
| 367 | return 0; |
| 368 | |
| 369 | out_put_unused_fd: |
| 370 | put_unused_fd(fd); |
| 371 | out_free_descs: |
| 372 | for (i = 0; i < count; i++) |
| 373 | gpiod_free(lh->descs[i]); |
| 374 | kfree(lh->label); |
| 375 | out_free_lh: |
| 376 | kfree(lh); |
| 377 | put_device(&gdev->dev); |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * GPIO line event management |
| 383 | */ |
| 384 | |
| 385 | /** |
| 386 | * struct lineevent_state - contains the state of a userspace event |
| 387 | * @gdev: the GPIO device the event pertains to |
| 388 | * @label: consumer label used to tag descriptors |
| 389 | * @desc: the GPIO descriptor held by this event |
| 390 | * @eflags: the event flags this line was requested with |
| 391 | * @irq: the interrupt that trigger in response to events on this GPIO |
| 392 | * @wait: wait queue that handles blocking reads of events |
| 393 | * @events: KFIFO for the GPIO events |
| 394 | * @timestamp: cache for the timestamp storing it between hardirq |
| 395 | * and IRQ thread, used to bring the timestamp close to the actual |
| 396 | * event |
| 397 | */ |
| 398 | struct lineevent_state { |
| 399 | struct gpio_device *gdev; |
| 400 | const char *label; |
| 401 | struct gpio_desc *desc; |
| 402 | u32 eflags; |
| 403 | int irq; |
| 404 | wait_queue_head_t wait; |
| 405 | DECLARE_KFIFO(events, struct gpioevent_data, 16); |
| 406 | u64 timestamp; |
| 407 | }; |
| 408 | |
| 409 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ |
| 410 | (GPIOEVENT_REQUEST_RISING_EDGE | \ |
| 411 | GPIOEVENT_REQUEST_FALLING_EDGE) |
| 412 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 413 | static __poll_t lineevent_poll(struct file *file, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 414 | struct poll_table_struct *wait) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 415 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 416 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 417 | __poll_t events = 0; |
| 418 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 419 | poll_wait(file, &le->wait, wait); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 420 | |
| 421 | if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) |
| 422 | events = EPOLLIN | EPOLLRDNORM; |
| 423 | |
| 424 | return events; |
| 425 | } |
| 426 | |
| 427 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 428 | static ssize_t lineevent_read(struct file *file, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 429 | char __user *buf, |
| 430 | size_t count, |
| 431 | loff_t *f_ps) |
| 432 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 433 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 434 | struct gpioevent_data ge; |
| 435 | ssize_t bytes_read = 0; |
| 436 | int ret; |
| 437 | |
| 438 | if (count < sizeof(ge)) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | do { |
| 442 | spin_lock(&le->wait.lock); |
| 443 | if (kfifo_is_empty(&le->events)) { |
| 444 | if (bytes_read) { |
| 445 | spin_unlock(&le->wait.lock); |
| 446 | return bytes_read; |
| 447 | } |
| 448 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 449 | if (file->f_flags & O_NONBLOCK) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 450 | spin_unlock(&le->wait.lock); |
| 451 | return -EAGAIN; |
| 452 | } |
| 453 | |
| 454 | ret = wait_event_interruptible_locked(le->wait, |
| 455 | !kfifo_is_empty(&le->events)); |
| 456 | if (ret) { |
| 457 | spin_unlock(&le->wait.lock); |
| 458 | return ret; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | ret = kfifo_out(&le->events, &ge, 1); |
| 463 | spin_unlock(&le->wait.lock); |
| 464 | if (ret != 1) { |
| 465 | /* |
| 466 | * This should never happen - we were holding the lock |
| 467 | * from the moment we learned the fifo is no longer |
| 468 | * empty until now. |
| 469 | */ |
| 470 | ret = -EIO; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | if (copy_to_user(buf + bytes_read, &ge, sizeof(ge))) |
| 475 | return -EFAULT; |
| 476 | bytes_read += sizeof(ge); |
| 477 | } while (count >= bytes_read + sizeof(ge)); |
| 478 | |
| 479 | return bytes_read; |
| 480 | } |
| 481 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 482 | static int lineevent_release(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 483 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 484 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 485 | struct gpio_device *gdev = le->gdev; |
| 486 | |
| 487 | free_irq(le->irq, le); |
| 488 | gpiod_free(le->desc); |
| 489 | kfree(le->label); |
| 490 | kfree(le); |
| 491 | put_device(&gdev->dev); |
| 492 | return 0; |
| 493 | } |
| 494 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 495 | static long lineevent_ioctl(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 496 | unsigned long arg) |
| 497 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 498 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 499 | void __user *ip = (void __user *)arg; |
| 500 | struct gpiohandle_data ghd; |
| 501 | |
| 502 | /* |
| 503 | * We can get the value for an event line but not set it, |
| 504 | * because it is input by definition. |
| 505 | */ |
| 506 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 507 | int val; |
| 508 | |
| 509 | memset(&ghd, 0, sizeof(ghd)); |
| 510 | |
| 511 | val = gpiod_get_value_cansleep(le->desc); |
| 512 | if (val < 0) |
| 513 | return val; |
| 514 | ghd.values[0] = val; |
| 515 | |
| 516 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 517 | return -EFAULT; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | return -EINVAL; |
| 522 | } |
| 523 | |
| 524 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 525 | static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 526 | unsigned long arg) |
| 527 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 528 | return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 529 | } |
| 530 | #endif |
| 531 | |
| 532 | static const struct file_operations lineevent_fileops = { |
| 533 | .release = lineevent_release, |
| 534 | .read = lineevent_read, |
| 535 | .poll = lineevent_poll, |
| 536 | .owner = THIS_MODULE, |
| 537 | .llseek = noop_llseek, |
| 538 | .unlocked_ioctl = lineevent_ioctl, |
| 539 | #ifdef CONFIG_COMPAT |
| 540 | .compat_ioctl = lineevent_ioctl_compat, |
| 541 | #endif |
| 542 | }; |
| 543 | |
| 544 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
| 545 | { |
| 546 | struct lineevent_state *le = p; |
| 547 | struct gpioevent_data ge; |
| 548 | int ret; |
| 549 | |
| 550 | /* Do not leak kernel stack to userspace */ |
| 551 | memset(&ge, 0, sizeof(ge)); |
| 552 | |
| 553 | /* |
| 554 | * We may be running from a nested threaded interrupt in which case |
| 555 | * we didn't get the timestamp from lineevent_irq_handler(). |
| 556 | */ |
| 557 | if (!le->timestamp) |
| 558 | ge.timestamp = ktime_get_ns(); |
| 559 | else |
| 560 | ge.timestamp = le->timestamp; |
| 561 | |
| 562 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
| 563 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
| 564 | int level = gpiod_get_value_cansleep(le->desc); |
| 565 | |
| 566 | if (level) |
| 567 | /* Emit low-to-high event */ |
| 568 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 569 | else |
| 570 | /* Emit high-to-low event */ |
| 571 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
| 572 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { |
| 573 | /* Emit low-to-high event */ |
| 574 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 575 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
| 576 | /* Emit high-to-low event */ |
| 577 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
| 578 | } else { |
| 579 | return IRQ_NONE; |
| 580 | } |
| 581 | |
| 582 | ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, |
| 583 | 1, &le->wait.lock); |
| 584 | if (ret) |
| 585 | wake_up_poll(&le->wait, EPOLLIN); |
| 586 | else |
| 587 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); |
| 588 | |
| 589 | return IRQ_HANDLED; |
| 590 | } |
| 591 | |
| 592 | static irqreturn_t lineevent_irq_handler(int irq, void *p) |
| 593 | { |
| 594 | struct lineevent_state *le = p; |
| 595 | |
| 596 | /* |
| 597 | * Just store the timestamp in hardirq context so we get it as |
| 598 | * close in time as possible to the actual event. |
| 599 | */ |
| 600 | le->timestamp = ktime_get_ns(); |
| 601 | |
| 602 | return IRQ_WAKE_THREAD; |
| 603 | } |
| 604 | |
| 605 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
| 606 | { |
| 607 | struct gpioevent_request eventreq; |
| 608 | struct lineevent_state *le; |
| 609 | struct gpio_desc *desc; |
| 610 | struct file *file; |
| 611 | u32 offset; |
| 612 | u32 lflags; |
| 613 | u32 eflags; |
| 614 | int fd; |
| 615 | int ret; |
| 616 | int irqflags = 0; |
| 617 | |
| 618 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) |
| 619 | return -EFAULT; |
| 620 | |
| 621 | offset = eventreq.lineoffset; |
| 622 | lflags = eventreq.handleflags; |
| 623 | eflags = eventreq.eventflags; |
| 624 | |
| 625 | desc = gpiochip_get_desc(gdev->chip, offset); |
| 626 | if (IS_ERR(desc)) |
| 627 | return PTR_ERR(desc); |
| 628 | |
| 629 | /* Return an error if a unknown flag is set */ |
| 630 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || |
| 631 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) |
| 632 | return -EINVAL; |
| 633 | |
| 634 | /* This is just wrong: we don't look for events on output lines */ |
| 635 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || |
| 636 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 637 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | /* Only one bias flag can be set. */ |
| 641 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 642 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
| 643 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
| 644 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 645 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 646 | return -EINVAL; |
| 647 | |
| 648 | le = kzalloc(sizeof(*le), GFP_KERNEL); |
| 649 | if (!le) |
| 650 | return -ENOMEM; |
| 651 | le->gdev = gdev; |
| 652 | get_device(&gdev->dev); |
| 653 | |
| 654 | /* Make sure this is terminated */ |
| 655 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; |
| 656 | if (strlen(eventreq.consumer_label)) { |
| 657 | le->label = kstrdup(eventreq.consumer_label, |
| 658 | GFP_KERNEL); |
| 659 | if (!le->label) { |
| 660 | ret = -ENOMEM; |
| 661 | goto out_free_le; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | ret = gpiod_request(desc, le->label); |
| 666 | if (ret) |
| 667 | goto out_free_label; |
| 668 | le->desc = desc; |
| 669 | le->eflags = eflags; |
| 670 | |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 671 | linehandle_flags_to_desc_flags(lflags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 672 | |
| 673 | ret = gpiod_direction_input(desc); |
| 674 | if (ret) |
| 675 | goto out_free_desc; |
| 676 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 677 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 678 | GPIOLINE_CHANGED_REQUESTED, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 679 | |
| 680 | le->irq = gpiod_to_irq(desc); |
| 681 | if (le->irq <= 0) { |
| 682 | ret = -ENODEV; |
| 683 | goto out_free_desc; |
| 684 | } |
| 685 | |
| 686 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) |
| 687 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 688 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
| 689 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
| 690 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 691 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
| 692 | irqflags |= IRQF_ONESHOT; |
| 693 | |
| 694 | INIT_KFIFO(le->events); |
| 695 | init_waitqueue_head(&le->wait); |
| 696 | |
| 697 | /* Request a thread to read the events */ |
| 698 | ret = request_threaded_irq(le->irq, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 699 | lineevent_irq_handler, |
| 700 | lineevent_irq_thread, |
| 701 | irqflags, |
| 702 | le->label, |
| 703 | le); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 704 | if (ret) |
| 705 | goto out_free_desc; |
| 706 | |
| 707 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
| 708 | if (fd < 0) { |
| 709 | ret = fd; |
| 710 | goto out_free_irq; |
| 711 | } |
| 712 | |
| 713 | file = anon_inode_getfile("gpio-event", |
| 714 | &lineevent_fileops, |
| 715 | le, |
| 716 | O_RDONLY | O_CLOEXEC); |
| 717 | if (IS_ERR(file)) { |
| 718 | ret = PTR_ERR(file); |
| 719 | goto out_put_unused_fd; |
| 720 | } |
| 721 | |
| 722 | eventreq.fd = fd; |
| 723 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
| 724 | /* |
| 725 | * fput() will trigger the release() callback, so do not go onto |
| 726 | * the regular error cleanup path here. |
| 727 | */ |
| 728 | fput(file); |
| 729 | put_unused_fd(fd); |
| 730 | return -EFAULT; |
| 731 | } |
| 732 | |
| 733 | fd_install(fd, file); |
| 734 | |
| 735 | return 0; |
| 736 | |
| 737 | out_put_unused_fd: |
| 738 | put_unused_fd(fd); |
| 739 | out_free_irq: |
| 740 | free_irq(le->irq, le); |
| 741 | out_free_desc: |
| 742 | gpiod_free(le->desc); |
| 743 | out_free_label: |
| 744 | kfree(le->label); |
| 745 | out_free_le: |
| 746 | kfree(le); |
| 747 | put_device(&gdev->dev); |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | static void gpio_desc_to_lineinfo(struct gpio_desc *desc, |
| 752 | struct gpioline_info *info) |
| 753 | { |
| 754 | struct gpio_chip *gc = desc->gdev->chip; |
| 755 | bool ok_for_pinctrl; |
| 756 | unsigned long flags; |
| 757 | |
| 758 | /* |
| 759 | * This function takes a mutex so we must check this before taking |
| 760 | * the spinlock. |
| 761 | * |
| 762 | * FIXME: find a non-racy way to retrieve this information. Maybe a |
| 763 | * lock common to both frameworks? |
| 764 | */ |
| 765 | ok_for_pinctrl = |
| 766 | pinctrl_gpio_can_use_line(gc->base + info->line_offset); |
| 767 | |
| 768 | spin_lock_irqsave(&gpio_lock, flags); |
| 769 | |
| 770 | if (desc->name) { |
| 771 | strncpy(info->name, desc->name, sizeof(info->name)); |
| 772 | info->name[sizeof(info->name) - 1] = '\0'; |
| 773 | } else { |
| 774 | info->name[0] = '\0'; |
| 775 | } |
| 776 | |
| 777 | if (desc->label) { |
| 778 | strncpy(info->consumer, desc->label, sizeof(info->consumer)); |
| 779 | info->consumer[sizeof(info->consumer) - 1] = '\0'; |
| 780 | } else { |
| 781 | info->consumer[0] = '\0'; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Userspace only need to know that the kernel is using this GPIO so |
| 786 | * it can't use it. |
| 787 | */ |
| 788 | info->flags = 0; |
| 789 | if (test_bit(FLAG_REQUESTED, &desc->flags) || |
| 790 | test_bit(FLAG_IS_HOGGED, &desc->flags) || |
| 791 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || |
| 792 | test_bit(FLAG_EXPORT, &desc->flags) || |
| 793 | test_bit(FLAG_SYSFS, &desc->flags) || |
| 794 | !ok_for_pinctrl) |
| 795 | info->flags |= GPIOLINE_FLAG_KERNEL; |
| 796 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
| 797 | info->flags |= GPIOLINE_FLAG_IS_OUT; |
| 798 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 799 | info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
| 800 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 801 | info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | |
| 802 | GPIOLINE_FLAG_IS_OUT); |
| 803 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 804 | info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | |
| 805 | GPIOLINE_FLAG_IS_OUT); |
| 806 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
| 807 | info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; |
| 808 | if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
| 809 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; |
| 810 | if (test_bit(FLAG_PULL_UP, &desc->flags)) |
| 811 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; |
| 812 | |
| 813 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 814 | } |
| 815 | |
| 816 | struct gpio_chardev_data { |
| 817 | struct gpio_device *gdev; |
| 818 | wait_queue_head_t wait; |
| 819 | DECLARE_KFIFO(events, struct gpioline_info_changed, 32); |
| 820 | struct notifier_block lineinfo_changed_nb; |
| 821 | unsigned long *watched_lines; |
| 822 | }; |
| 823 | |
| 824 | /* |
| 825 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
| 826 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 827 | 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] | 828 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 829 | struct gpio_chardev_data *priv = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 830 | struct gpio_device *gdev = priv->gdev; |
| 831 | struct gpio_chip *gc = gdev->chip; |
| 832 | void __user *ip = (void __user *)arg; |
| 833 | struct gpio_desc *desc; |
| 834 | __u32 offset; |
| 835 | int hwgpio; |
| 836 | |
| 837 | /* We fail any subsequent ioctl():s when the chip is gone */ |
| 838 | if (!gc) |
| 839 | return -ENODEV; |
| 840 | |
| 841 | /* Fill in the struct and pass to userspace */ |
| 842 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
| 843 | struct gpiochip_info chipinfo; |
| 844 | |
| 845 | memset(&chipinfo, 0, sizeof(chipinfo)); |
| 846 | |
| 847 | strncpy(chipinfo.name, dev_name(&gdev->dev), |
| 848 | sizeof(chipinfo.name)); |
| 849 | chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; |
| 850 | strncpy(chipinfo.label, gdev->label, |
| 851 | sizeof(chipinfo.label)); |
| 852 | chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; |
| 853 | chipinfo.lines = gdev->ngpio; |
| 854 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
| 855 | return -EFAULT; |
| 856 | return 0; |
| 857 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
| 858 | struct gpioline_info lineinfo; |
| 859 | |
| 860 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 861 | return -EFAULT; |
| 862 | |
| 863 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
| 864 | if (IS_ERR(desc)) |
| 865 | return PTR_ERR(desc); |
| 866 | |
| 867 | hwgpio = gpio_chip_hwgpio(desc); |
| 868 | |
| 869 | gpio_desc_to_lineinfo(desc, &lineinfo); |
| 870 | |
| 871 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
| 872 | return -EFAULT; |
| 873 | return 0; |
| 874 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
| 875 | return linehandle_create(gdev, ip); |
| 876 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
| 877 | return lineevent_create(gdev, ip); |
| 878 | } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { |
| 879 | struct gpioline_info lineinfo; |
| 880 | |
| 881 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 882 | return -EFAULT; |
| 883 | |
| 884 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
| 885 | if (IS_ERR(desc)) |
| 886 | return PTR_ERR(desc); |
| 887 | |
| 888 | hwgpio = gpio_chip_hwgpio(desc); |
| 889 | |
| 890 | if (test_bit(hwgpio, priv->watched_lines)) |
| 891 | return -EBUSY; |
| 892 | |
| 893 | gpio_desc_to_lineinfo(desc, &lineinfo); |
| 894 | |
| 895 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
| 896 | return -EFAULT; |
| 897 | |
| 898 | set_bit(hwgpio, priv->watched_lines); |
| 899 | return 0; |
| 900 | } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { |
| 901 | if (copy_from_user(&offset, ip, sizeof(offset))) |
| 902 | return -EFAULT; |
| 903 | |
| 904 | desc = gpiochip_get_desc(gc, offset); |
| 905 | if (IS_ERR(desc)) |
| 906 | return PTR_ERR(desc); |
| 907 | |
| 908 | hwgpio = gpio_chip_hwgpio(desc); |
| 909 | |
| 910 | if (!test_bit(hwgpio, priv->watched_lines)) |
| 911 | return -EBUSY; |
| 912 | |
| 913 | clear_bit(hwgpio, priv->watched_lines); |
| 914 | return 0; |
| 915 | } |
| 916 | return -EINVAL; |
| 917 | } |
| 918 | |
| 919 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 920 | static long gpio_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 921 | unsigned long arg) |
| 922 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 923 | return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 924 | } |
| 925 | #endif |
| 926 | |
| 927 | static struct gpio_chardev_data * |
| 928 | to_gpio_chardev_data(struct notifier_block *nb) |
| 929 | { |
| 930 | return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); |
| 931 | } |
| 932 | |
| 933 | static int lineinfo_changed_notify(struct notifier_block *nb, |
| 934 | unsigned long action, void *data) |
| 935 | { |
| 936 | struct gpio_chardev_data *priv = to_gpio_chardev_data(nb); |
| 937 | struct gpioline_info_changed chg; |
| 938 | struct gpio_desc *desc = data; |
| 939 | int ret; |
| 940 | |
| 941 | if (!test_bit(gpio_chip_hwgpio(desc), priv->watched_lines)) |
| 942 | return NOTIFY_DONE; |
| 943 | |
| 944 | memset(&chg, 0, sizeof(chg)); |
| 945 | chg.info.line_offset = gpio_chip_hwgpio(desc); |
| 946 | chg.event_type = action; |
| 947 | chg.timestamp = ktime_get_ns(); |
| 948 | gpio_desc_to_lineinfo(desc, &chg.info); |
| 949 | |
| 950 | ret = kfifo_in_spinlocked(&priv->events, &chg, 1, &priv->wait.lock); |
| 951 | if (ret) |
| 952 | wake_up_poll(&priv->wait, EPOLLIN); |
| 953 | else |
| 954 | pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); |
| 955 | |
| 956 | return NOTIFY_OK; |
| 957 | } |
| 958 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 959 | static __poll_t lineinfo_watch_poll(struct file *file, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 960 | struct poll_table_struct *pollt) |
| 961 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 962 | struct gpio_chardev_data *priv = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 963 | __poll_t events = 0; |
| 964 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 965 | poll_wait(file, &priv->wait, pollt); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 966 | |
| 967 | if (!kfifo_is_empty_spinlocked_noirqsave(&priv->events, |
| 968 | &priv->wait.lock)) |
| 969 | events = EPOLLIN | EPOLLRDNORM; |
| 970 | |
| 971 | return events; |
| 972 | } |
| 973 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 974 | static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 975 | size_t count, loff_t *off) |
| 976 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 977 | struct gpio_chardev_data *priv = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 978 | struct gpioline_info_changed event; |
| 979 | ssize_t bytes_read = 0; |
| 980 | int ret; |
| 981 | |
| 982 | if (count < sizeof(event)) |
| 983 | return -EINVAL; |
| 984 | |
| 985 | do { |
| 986 | spin_lock(&priv->wait.lock); |
| 987 | if (kfifo_is_empty(&priv->events)) { |
| 988 | if (bytes_read) { |
| 989 | spin_unlock(&priv->wait.lock); |
| 990 | return bytes_read; |
| 991 | } |
| 992 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 993 | if (file->f_flags & O_NONBLOCK) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 994 | spin_unlock(&priv->wait.lock); |
| 995 | return -EAGAIN; |
| 996 | } |
| 997 | |
| 998 | ret = wait_event_interruptible_locked(priv->wait, |
| 999 | !kfifo_is_empty(&priv->events)); |
| 1000 | if (ret) { |
| 1001 | spin_unlock(&priv->wait.lock); |
| 1002 | return ret; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | ret = kfifo_out(&priv->events, &event, 1); |
| 1007 | spin_unlock(&priv->wait.lock); |
| 1008 | if (ret != 1) { |
| 1009 | ret = -EIO; |
| 1010 | break; |
| 1011 | /* We should never get here. See lineevent_read(). */ |
| 1012 | } |
| 1013 | |
| 1014 | if (copy_to_user(buf + bytes_read, &event, sizeof(event))) |
| 1015 | return -EFAULT; |
| 1016 | bytes_read += sizeof(event); |
| 1017 | } while (count >= bytes_read + sizeof(event)); |
| 1018 | |
| 1019 | return bytes_read; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * gpio_chrdev_open() - open the chardev for ioctl operations |
| 1024 | * @inode: inode for this chardev |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1025 | * @file: file struct for storing private data |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1026 | * Returns 0 on success |
| 1027 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1028 | static int gpio_chrdev_open(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1029 | { |
| 1030 | struct gpio_device *gdev = container_of(inode->i_cdev, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 1031 | struct gpio_device, chrdev); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1032 | struct gpio_chardev_data *priv; |
| 1033 | int ret = -ENOMEM; |
| 1034 | |
| 1035 | /* Fail on open if the backing gpiochip is gone */ |
| 1036 | if (!gdev->chip) |
| 1037 | return -ENODEV; |
| 1038 | |
| 1039 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 1040 | if (!priv) |
| 1041 | return -ENOMEM; |
| 1042 | |
| 1043 | priv->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); |
| 1044 | if (!priv->watched_lines) |
| 1045 | goto out_free_priv; |
| 1046 | |
| 1047 | init_waitqueue_head(&priv->wait); |
| 1048 | INIT_KFIFO(priv->events); |
| 1049 | priv->gdev = gdev; |
| 1050 | |
| 1051 | priv->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 1052 | ret = blocking_notifier_chain_register(&gdev->notifier, |
| 1053 | &priv->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1054 | if (ret) |
| 1055 | goto out_free_bitmap; |
| 1056 | |
| 1057 | get_device(&gdev->dev); |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1058 | file->private_data = priv; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1059 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1060 | ret = nonseekable_open(inode, file); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1061 | if (ret) |
| 1062 | goto out_unregister_notifier; |
| 1063 | |
| 1064 | return ret; |
| 1065 | |
| 1066 | out_unregister_notifier: |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 1067 | blocking_notifier_chain_unregister(&gdev->notifier, |
| 1068 | &priv->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1069 | out_free_bitmap: |
| 1070 | bitmap_free(priv->watched_lines); |
| 1071 | out_free_priv: |
| 1072 | kfree(priv); |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * gpio_chrdev_release() - close chardev after ioctl operations |
| 1078 | * @inode: inode for this chardev |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1079 | * @file: file struct for storing private data |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1080 | * Returns 0 on success |
| 1081 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1082 | static int gpio_chrdev_release(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1083 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1084 | struct gpio_chardev_data *priv = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1085 | struct gpio_device *gdev = priv->gdev; |
| 1086 | |
| 1087 | bitmap_free(priv->watched_lines); |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame^] | 1088 | blocking_notifier_chain_unregister(&gdev->notifier, |
| 1089 | &priv->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1090 | put_device(&gdev->dev); |
| 1091 | kfree(priv); |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | static const struct file_operations gpio_fileops = { |
| 1097 | .release = gpio_chrdev_release, |
| 1098 | .open = gpio_chrdev_open, |
| 1099 | .poll = lineinfo_watch_poll, |
| 1100 | .read = lineinfo_watch_read, |
| 1101 | .owner = THIS_MODULE, |
| 1102 | .llseek = no_llseek, |
| 1103 | .unlocked_ioctl = gpio_ioctl, |
| 1104 | #ifdef CONFIG_COMPAT |
| 1105 | .compat_ioctl = gpio_ioctl_compat, |
| 1106 | #endif |
| 1107 | }; |
| 1108 | |
| 1109 | int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) |
| 1110 | { |
| 1111 | int ret; |
| 1112 | |
| 1113 | cdev_init(&gdev->chrdev, &gpio_fileops); |
| 1114 | gdev->chrdev.owner = THIS_MODULE; |
| 1115 | gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); |
| 1116 | |
| 1117 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); |
| 1118 | if (ret) |
| 1119 | return ret; |
| 1120 | |
| 1121 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", |
| 1122 | MAJOR(devt), gdev->id); |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | void gpiolib_cdev_unregister(struct gpio_device *gdev) |
| 1128 | { |
| 1129 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
| 1130 | } |