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 | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 11 | #include <linux/file.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 12 | #include <linux/gpio.h> |
| 13 | #include <linux/gpio/driver.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/irqreturn.h> |
| 16 | #include <linux/kernel.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 17 | #include <linux/kfifo.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/pinctrl/consumer.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 20 | #include <linux/poll.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 22 | #include <linux/timekeeping.h> |
Kent Gibson | d189f62 | 2020-07-08 12:15:45 +0800 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 24 | #include <uapi/linux/gpio.h> |
| 25 | |
| 26 | #include "gpiolib.h" |
| 27 | #include "gpiolib-cdev.h" |
| 28 | |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 29 | /* |
| 30 | * Array sizes must ensure 64-bit alignment and not create holes in the |
| 31 | * struct packing. |
| 32 | */ |
| 33 | static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); |
| 34 | static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); |
| 35 | |
| 36 | /* |
| 37 | * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility |
| 38 | */ |
| 39 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); |
| 40 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); |
| 41 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); |
| 42 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); |
| 43 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); |
| 44 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); |
| 45 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); |
| 46 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); |
| 47 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 48 | /* Character device interface to GPIO. |
| 49 | * |
| 50 | * The GPIO character device, /dev/gpiochipN, provides userspace an |
| 51 | * interface to gpiolib GPIOs via ioctl()s. |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * GPIO line handle management |
| 56 | */ |
| 57 | |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 58 | #ifdef CONFIG_GPIO_CDEV_V1 |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 59 | /** |
| 60 | * struct linehandle_state - contains the state of a userspace handle |
| 61 | * @gdev: the GPIO device the handle pertains to |
| 62 | * @label: consumer label used to tag descriptors |
| 63 | * @descs: the GPIO descriptors held by this handle |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 64 | * @num_descs: the number of descriptors held in the descs array |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 65 | */ |
| 66 | struct linehandle_state { |
| 67 | struct gpio_device *gdev; |
| 68 | const char *label; |
| 69 | struct gpio_desc *descs[GPIOHANDLES_MAX]; |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 70 | u32 num_descs; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
| 74 | (GPIOHANDLE_REQUEST_INPUT | \ |
| 75 | GPIOHANDLE_REQUEST_OUTPUT | \ |
| 76 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ |
| 77 | GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ |
| 78 | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ |
| 79 | GPIOHANDLE_REQUEST_BIAS_DISABLE | \ |
| 80 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ |
| 81 | GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 82 | |
| 83 | static int linehandle_validate_flags(u32 flags) |
| 84 | { |
| 85 | /* Return an error if an unknown flag is set */ |
| 86 | if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | /* |
| 90 | * Do not allow both INPUT & OUTPUT flags to be set as they are |
| 91 | * contradictory. |
| 92 | */ |
| 93 | if ((flags & GPIOHANDLE_REQUEST_INPUT) && |
| 94 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) |
| 95 | return -EINVAL; |
| 96 | |
| 97 | /* |
| 98 | * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If |
| 99 | * the hardware actually supports enabling both at the same time the |
| 100 | * electrical result would be disastrous. |
| 101 | */ |
| 102 | if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && |
| 103 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ |
| 107 | if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && |
| 108 | ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 109 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | /* Bias flags only allowed for input or output mode. */ |
| 113 | if (!((flags & GPIOHANDLE_REQUEST_INPUT) || |
| 114 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) && |
| 115 | ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || |
| 116 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || |
| 117 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | /* Only one bias flag can be set. */ |
| 121 | if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 122 | (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 123 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 124 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 125 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 131 | static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) |
| 132 | { |
| 133 | assign_bit(FLAG_ACTIVE_LOW, flagsp, |
| 134 | lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); |
| 135 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
| 136 | lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); |
| 137 | assign_bit(FLAG_OPEN_SOURCE, flagsp, |
| 138 | lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); |
| 139 | assign_bit(FLAG_PULL_UP, flagsp, |
| 140 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); |
| 141 | assign_bit(FLAG_PULL_DOWN, flagsp, |
| 142 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); |
| 143 | assign_bit(FLAG_BIAS_DISABLE, flagsp, |
| 144 | lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); |
| 145 | } |
| 146 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 147 | static long linehandle_set_config(struct linehandle_state *lh, |
| 148 | void __user *ip) |
| 149 | { |
| 150 | struct gpiohandle_config gcnf; |
| 151 | struct gpio_desc *desc; |
| 152 | int i, ret; |
| 153 | u32 lflags; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 154 | |
| 155 | if (copy_from_user(&gcnf, ip, sizeof(gcnf))) |
| 156 | return -EFAULT; |
| 157 | |
| 158 | lflags = gcnf.flags; |
| 159 | ret = linehandle_validate_flags(lflags); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 163 | for (i = 0; i < lh->num_descs; i++) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 164 | desc = lh->descs[i]; |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 165 | linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Lines have to be requested explicitly for input |
| 169 | * or output, else the line will be treated "as is". |
| 170 | */ |
| 171 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 172 | int val = !!gcnf.default_values[i]; |
| 173 | |
| 174 | ret = gpiod_direction_output(desc, val); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 178 | ret = gpiod_direction_input(desc); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | } |
| 182 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 183 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 184 | GPIOLINE_CHANGED_CONFIG, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 189 | static long linehandle_ioctl(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 190 | unsigned long arg) |
| 191 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 192 | struct linehandle_state *lh = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 193 | void __user *ip = (void __user *)arg; |
| 194 | struct gpiohandle_data ghd; |
| 195 | DECLARE_BITMAP(vals, GPIOHANDLES_MAX); |
| 196 | int i; |
| 197 | |
| 198 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 199 | /* NOTE: It's ok to read values of output lines. */ |
| 200 | int ret = gpiod_get_array_value_complex(false, |
| 201 | true, |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 202 | lh->num_descs, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 203 | lh->descs, |
| 204 | NULL, |
| 205 | vals); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
| 209 | memset(&ghd, 0, sizeof(ghd)); |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 210 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 211 | ghd.values[i] = test_bit(i, vals); |
| 212 | |
| 213 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 214 | return -EFAULT; |
| 215 | |
| 216 | return 0; |
| 217 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
| 218 | /* |
| 219 | * All line descriptors were created at once with the same |
| 220 | * flags so just check if the first one is really output. |
| 221 | */ |
| 222 | if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) |
| 223 | return -EPERM; |
| 224 | |
| 225 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
| 226 | return -EFAULT; |
| 227 | |
| 228 | /* Clamp all values to [0,1] */ |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 229 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 230 | __assign_bit(i, vals, ghd.values[i]); |
| 231 | |
| 232 | /* Reuse the array setting function */ |
| 233 | return gpiod_set_array_value_complex(false, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 234 | true, |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 235 | lh->num_descs, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 236 | lh->descs, |
| 237 | NULL, |
| 238 | vals); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 239 | } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { |
| 240 | return linehandle_set_config(lh, ip); |
| 241 | } |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 246 | static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 247 | unsigned long arg) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 248 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 249 | return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 250 | } |
| 251 | #endif |
| 252 | |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 253 | static void linehandle_free(struct linehandle_state *lh) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 254 | { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 255 | int i; |
| 256 | |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 257 | for (i = 0; i < lh->num_descs; i++) |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 258 | if (lh->descs[i]) |
| 259 | gpiod_free(lh->descs[i]); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 260 | kfree(lh->label); |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 261 | put_device(&lh->gdev->dev); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 262 | kfree(lh); |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static int linehandle_release(struct inode *inode, struct file *file) |
| 266 | { |
| 267 | linehandle_free(file->private_data); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static const struct file_operations linehandle_fileops = { |
| 272 | .release = linehandle_release, |
| 273 | .owner = THIS_MODULE, |
| 274 | .llseek = noop_llseek, |
| 275 | .unlocked_ioctl = linehandle_ioctl, |
| 276 | #ifdef CONFIG_COMPAT |
| 277 | .compat_ioctl = linehandle_ioctl_compat, |
| 278 | #endif |
| 279 | }; |
| 280 | |
| 281 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) |
| 282 | { |
| 283 | struct gpiohandle_request handlereq; |
| 284 | struct linehandle_state *lh; |
| 285 | struct file *file; |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 286 | int fd, i, ret; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 287 | u32 lflags; |
| 288 | |
| 289 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
| 290 | return -EFAULT; |
| 291 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | lflags = handlereq.flags; |
| 295 | |
| 296 | ret = linehandle_validate_flags(lflags); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | |
| 300 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
| 301 | if (!lh) |
| 302 | return -ENOMEM; |
| 303 | lh->gdev = gdev; |
| 304 | get_device(&gdev->dev); |
| 305 | |
| 306 | /* Make sure this is terminated */ |
| 307 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; |
| 308 | if (strlen(handlereq.consumer_label)) { |
| 309 | lh->label = kstrdup(handlereq.consumer_label, |
| 310 | GFP_KERNEL); |
| 311 | if (!lh->label) { |
| 312 | ret = -ENOMEM; |
| 313 | goto out_free_lh; |
| 314 | } |
| 315 | } |
| 316 | |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 317 | lh->num_descs = handlereq.lines; |
| 318 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 319 | /* Request each GPIO */ |
| 320 | for (i = 0; i < handlereq.lines; i++) { |
| 321 | u32 offset = handlereq.lineoffsets[i]; |
| 322 | struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); |
| 323 | |
| 324 | if (IS_ERR(desc)) { |
| 325 | ret = PTR_ERR(desc); |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 326 | goto out_free_lh; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | ret = gpiod_request(desc, lh->label); |
| 330 | if (ret) |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 331 | goto out_free_lh; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 332 | lh->descs[i] = desc; |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 333 | linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 334 | |
| 335 | ret = gpiod_set_transitory(desc, false); |
| 336 | if (ret < 0) |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 337 | goto out_free_lh; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Lines have to be requested explicitly for input |
| 341 | * or output, else the line will be treated "as is". |
| 342 | */ |
| 343 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 344 | int val = !!handlereq.default_values[i]; |
| 345 | |
| 346 | ret = gpiod_direction_output(desc, val); |
| 347 | if (ret) |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 348 | goto out_free_lh; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 349 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 350 | ret = gpiod_direction_input(desc); |
| 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 | } |
| 354 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 355 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 356 | GPIOLINE_CHANGED_REQUESTED, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 357 | |
| 358 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
| 359 | offset); |
| 360 | } |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 361 | |
| 362 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
| 363 | if (fd < 0) { |
| 364 | ret = fd; |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 365 | goto out_free_lh; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | file = anon_inode_getfile("gpio-linehandle", |
| 369 | &linehandle_fileops, |
| 370 | lh, |
| 371 | O_RDONLY | O_CLOEXEC); |
| 372 | if (IS_ERR(file)) { |
| 373 | ret = PTR_ERR(file); |
| 374 | goto out_put_unused_fd; |
| 375 | } |
| 376 | |
| 377 | handlereq.fd = fd; |
| 378 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
| 379 | /* |
| 380 | * fput() will trigger the release() callback, so do not go onto |
| 381 | * the regular error cleanup path here. |
| 382 | */ |
| 383 | fput(file); |
| 384 | put_unused_fd(fd); |
| 385 | return -EFAULT; |
| 386 | } |
| 387 | |
| 388 | fd_install(fd, file); |
| 389 | |
| 390 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
Kent Gibson | 52b7b59 | 2020-07-08 12:15:49 +0800 | [diff] [blame] | 391 | lh->num_descs); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | |
| 395 | out_put_unused_fd: |
| 396 | put_unused_fd(fd); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 397 | out_free_lh: |
Kent Gibson | 883f919 | 2020-07-08 12:15:55 +0800 | [diff] [blame] | 398 | linehandle_free(lh); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 399 | return ret; |
| 400 | } |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 401 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
| 402 | |
| 403 | /** |
| 404 | * struct line - contains the state of a requested line |
| 405 | * @desc: the GPIO descriptor for this line. |
| 406 | */ |
| 407 | struct line { |
| 408 | struct gpio_desc *desc; |
| 409 | }; |
| 410 | |
| 411 | /** |
| 412 | * struct linereq - contains the state of a userspace line request |
| 413 | * @gdev: the GPIO device the line request pertains to |
| 414 | * @label: consumer label used to tag GPIO descriptors |
| 415 | * @num_lines: the number of lines in the lines array |
| 416 | * @lines: the lines held by this line request, with @num_lines elements. |
| 417 | */ |
| 418 | struct linereq { |
| 419 | struct gpio_device *gdev; |
| 420 | const char *label; |
| 421 | u32 num_lines; |
| 422 | struct line lines[]; |
| 423 | }; |
| 424 | |
| 425 | #define GPIO_V2_LINE_BIAS_FLAGS \ |
| 426 | (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ |
| 427 | GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ |
| 428 | GPIO_V2_LINE_FLAG_BIAS_DISABLED) |
| 429 | |
| 430 | #define GPIO_V2_LINE_DIRECTION_FLAGS \ |
| 431 | (GPIO_V2_LINE_FLAG_INPUT | \ |
| 432 | GPIO_V2_LINE_FLAG_OUTPUT) |
| 433 | |
| 434 | #define GPIO_V2_LINE_DRIVE_FLAGS \ |
| 435 | (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ |
| 436 | GPIO_V2_LINE_FLAG_OPEN_SOURCE) |
| 437 | |
| 438 | #define GPIO_V2_LINE_VALID_FLAGS \ |
| 439 | (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ |
| 440 | GPIO_V2_LINE_DIRECTION_FLAGS | \ |
| 441 | GPIO_V2_LINE_DRIVE_FLAGS | \ |
| 442 | GPIO_V2_LINE_BIAS_FLAGS) |
| 443 | |
| 444 | static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, |
| 445 | unsigned int line_idx) |
| 446 | { |
| 447 | unsigned int i; |
| 448 | u64 mask = BIT_ULL(line_idx); |
| 449 | |
| 450 | for (i = 0; i < lc->num_attrs; i++) { |
| 451 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && |
| 452 | (lc->attrs[i].mask & mask)) |
| 453 | return lc->attrs[i].attr.flags; |
| 454 | } |
| 455 | return lc->flags; |
| 456 | } |
| 457 | |
| 458 | static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, |
| 459 | unsigned int line_idx) |
| 460 | { |
| 461 | unsigned int i; |
| 462 | u64 mask = BIT_ULL(line_idx); |
| 463 | |
| 464 | for (i = 0; i < lc->num_attrs; i++) { |
| 465 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && |
| 466 | (lc->attrs[i].mask & mask)) |
| 467 | return !!(lc->attrs[i].attr.values & mask); |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int gpio_v2_line_flags_validate(u64 flags) |
| 473 | { |
| 474 | /* Return an error if an unknown flag is set */ |
| 475 | if (flags & ~GPIO_V2_LINE_VALID_FLAGS) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | /* |
| 479 | * Do not allow both INPUT and OUTPUT flags to be set as they are |
| 480 | * contradictory. |
| 481 | */ |
| 482 | if ((flags & GPIO_V2_LINE_FLAG_INPUT) && |
| 483 | (flags & GPIO_V2_LINE_FLAG_OUTPUT)) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | /* |
| 487 | * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single |
| 488 | * request. If the hardware actually supports enabling both at the |
| 489 | * same time the electrical result would be disastrous. |
| 490 | */ |
| 491 | if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && |
| 492 | (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) |
| 493 | return -EINVAL; |
| 494 | |
| 495 | /* Drive requires explicit output direction. */ |
| 496 | if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && |
| 497 | !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) |
| 498 | return -EINVAL; |
| 499 | |
| 500 | /* Bias requires explicit direction. */ |
| 501 | if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && |
| 502 | !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | /* Only one bias flag can be set. */ |
| 506 | if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && |
| 507 | (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | |
| 508 | GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || |
| 509 | ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && |
| 510 | (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, |
| 517 | unsigned int num_lines) |
| 518 | { |
| 519 | unsigned int i; |
| 520 | u64 flags; |
| 521 | int ret; |
| 522 | |
| 523 | if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | for (i = 0; i < num_lines; i++) { |
| 530 | flags = gpio_v2_line_config_flags(lc, i); |
| 531 | ret = gpio_v2_line_flags_validate(flags); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, |
| 539 | unsigned long *flagsp) |
| 540 | { |
| 541 | assign_bit(FLAG_ACTIVE_LOW, flagsp, |
| 542 | flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); |
| 543 | |
| 544 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) |
| 545 | set_bit(FLAG_IS_OUT, flagsp); |
| 546 | else if (flags & GPIO_V2_LINE_FLAG_INPUT) |
| 547 | clear_bit(FLAG_IS_OUT, flagsp); |
| 548 | |
| 549 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
| 550 | flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); |
| 551 | assign_bit(FLAG_OPEN_SOURCE, flagsp, |
| 552 | flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); |
| 553 | |
| 554 | assign_bit(FLAG_PULL_UP, flagsp, |
| 555 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); |
| 556 | assign_bit(FLAG_PULL_DOWN, flagsp, |
| 557 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); |
| 558 | assign_bit(FLAG_BIAS_DISABLE, flagsp, |
| 559 | flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); |
| 560 | } |
| 561 | |
| 562 | static long linereq_get_values(struct linereq *lr, void __user *ip) |
| 563 | { |
| 564 | struct gpio_v2_line_values lv; |
| 565 | DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); |
| 566 | struct gpio_desc **descs; |
| 567 | unsigned int i, didx, num_get; |
| 568 | int ret; |
| 569 | |
| 570 | /* NOTE: It's ok to read values of output lines. */ |
| 571 | if (copy_from_user(&lv, ip, sizeof(lv))) |
| 572 | return -EFAULT; |
| 573 | |
| 574 | for (num_get = 0, i = 0; i < lr->num_lines; i++) { |
| 575 | if (lv.mask & BIT_ULL(i)) { |
| 576 | num_get++; |
| 577 | descs = &lr->lines[i].desc; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (num_get == 0) |
| 582 | return -EINVAL; |
| 583 | |
| 584 | if (num_get != 1) { |
| 585 | descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); |
| 586 | if (!descs) |
| 587 | return -ENOMEM; |
| 588 | for (didx = 0, i = 0; i < lr->num_lines; i++) { |
| 589 | if (lv.mask & BIT_ULL(i)) { |
| 590 | descs[didx] = lr->lines[i].desc; |
| 591 | didx++; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | ret = gpiod_get_array_value_complex(false, true, num_get, |
| 596 | descs, NULL, vals); |
| 597 | |
| 598 | if (num_get != 1) |
| 599 | kfree(descs); |
| 600 | if (ret) |
| 601 | return ret; |
| 602 | |
| 603 | lv.bits = 0; |
| 604 | for (didx = 0, i = 0; i < lr->num_lines; i++) { |
| 605 | if (lv.mask & BIT_ULL(i)) { |
| 606 | if (test_bit(didx, vals)) |
| 607 | lv.bits |= BIT_ULL(i); |
| 608 | didx++; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (copy_to_user(ip, &lv, sizeof(lv))) |
| 613 | return -EFAULT; |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static long linereq_ioctl(struct file *file, unsigned int cmd, |
| 619 | unsigned long arg) |
| 620 | { |
| 621 | struct linereq *lr = file->private_data; |
| 622 | void __user *ip = (void __user *)arg; |
| 623 | |
| 624 | if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) |
| 625 | return linereq_get_values(lr, ip); |
| 626 | |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
| 630 | #ifdef CONFIG_COMPAT |
| 631 | static long linereq_ioctl_compat(struct file *file, unsigned int cmd, |
| 632 | unsigned long arg) |
| 633 | { |
| 634 | return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 635 | } |
| 636 | #endif |
| 637 | |
| 638 | static void linereq_free(struct linereq *lr) |
| 639 | { |
| 640 | unsigned int i; |
| 641 | |
| 642 | for (i = 0; i < lr->num_lines; i++) { |
| 643 | if (lr->lines[i].desc) |
| 644 | gpiod_free(lr->lines[i].desc); |
| 645 | } |
| 646 | kfree(lr->label); |
| 647 | put_device(&lr->gdev->dev); |
| 648 | kfree(lr); |
| 649 | } |
| 650 | |
| 651 | static int linereq_release(struct inode *inode, struct file *file) |
| 652 | { |
| 653 | struct linereq *lr = file->private_data; |
| 654 | |
| 655 | linereq_free(lr); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static const struct file_operations line_fileops = { |
| 660 | .release = linereq_release, |
| 661 | .owner = THIS_MODULE, |
| 662 | .llseek = noop_llseek, |
| 663 | .unlocked_ioctl = linereq_ioctl, |
| 664 | #ifdef CONFIG_COMPAT |
| 665 | .compat_ioctl = linereq_ioctl_compat, |
| 666 | #endif |
| 667 | }; |
| 668 | |
| 669 | static int linereq_create(struct gpio_device *gdev, void __user *ip) |
| 670 | { |
| 671 | struct gpio_v2_line_request ulr; |
| 672 | struct gpio_v2_line_config *lc; |
| 673 | struct linereq *lr; |
| 674 | struct file *file; |
| 675 | u64 flags; |
| 676 | unsigned int i; |
| 677 | int fd, ret; |
| 678 | |
| 679 | if (copy_from_user(&ulr, ip, sizeof(ulr))) |
| 680 | return -EFAULT; |
| 681 | |
| 682 | if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) |
| 683 | return -EINVAL; |
| 684 | |
| 685 | if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | lc = &ulr.config; |
| 689 | ret = gpio_v2_line_config_validate(lc, ulr.num_lines); |
| 690 | if (ret) |
| 691 | return ret; |
| 692 | |
| 693 | lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); |
| 694 | if (!lr) |
| 695 | return -ENOMEM; |
| 696 | |
| 697 | lr->gdev = gdev; |
| 698 | get_device(&gdev->dev); |
| 699 | |
| 700 | /* Make sure this is terminated */ |
| 701 | ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; |
| 702 | if (strlen(ulr.consumer)) { |
| 703 | /* label is only initialized if consumer is set */ |
| 704 | lr->label = kstrdup(ulr.consumer, GFP_KERNEL); |
| 705 | if (!lr->label) { |
| 706 | ret = -ENOMEM; |
| 707 | goto out_free_linereq; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | lr->num_lines = ulr.num_lines; |
| 712 | |
| 713 | /* Request each GPIO */ |
| 714 | for (i = 0; i < ulr.num_lines; i++) { |
| 715 | u32 offset = ulr.offsets[i]; |
| 716 | struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); |
| 717 | |
| 718 | if (IS_ERR(desc)) { |
| 719 | ret = PTR_ERR(desc); |
| 720 | goto out_free_linereq; |
| 721 | } |
| 722 | |
| 723 | ret = gpiod_request(desc, lr->label); |
| 724 | if (ret) |
| 725 | goto out_free_linereq; |
| 726 | |
| 727 | lr->lines[i].desc = desc; |
| 728 | flags = gpio_v2_line_config_flags(lc, i); |
| 729 | gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); |
| 730 | |
| 731 | ret = gpiod_set_transitory(desc, false); |
| 732 | if (ret < 0) |
| 733 | goto out_free_linereq; |
| 734 | |
| 735 | /* |
| 736 | * Lines have to be requested explicitly for input |
| 737 | * or output, else the line will be treated "as is". |
| 738 | */ |
| 739 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { |
| 740 | int val = gpio_v2_line_config_output_value(lc, i); |
| 741 | |
| 742 | ret = gpiod_direction_output(desc, val); |
| 743 | if (ret) |
| 744 | goto out_free_linereq; |
| 745 | } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { |
| 746 | ret = gpiod_direction_input(desc); |
| 747 | if (ret) |
| 748 | goto out_free_linereq; |
| 749 | } |
| 750 | |
| 751 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 752 | GPIOLINE_CHANGED_REQUESTED, desc); |
| 753 | |
| 754 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
| 755 | offset); |
| 756 | } |
| 757 | |
| 758 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
| 759 | if (fd < 0) { |
| 760 | ret = fd; |
| 761 | goto out_free_linereq; |
| 762 | } |
| 763 | |
| 764 | file = anon_inode_getfile("gpio-line", &line_fileops, lr, |
| 765 | O_RDONLY | O_CLOEXEC); |
| 766 | if (IS_ERR(file)) { |
| 767 | ret = PTR_ERR(file); |
| 768 | goto out_put_unused_fd; |
| 769 | } |
| 770 | |
| 771 | ulr.fd = fd; |
| 772 | if (copy_to_user(ip, &ulr, sizeof(ulr))) { |
| 773 | /* |
| 774 | * fput() will trigger the release() callback, so do not go onto |
| 775 | * the regular error cleanup path here. |
| 776 | */ |
| 777 | fput(file); |
| 778 | put_unused_fd(fd); |
| 779 | return -EFAULT; |
| 780 | } |
| 781 | |
| 782 | fd_install(fd, file); |
| 783 | |
| 784 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
| 785 | lr->num_lines); |
| 786 | |
| 787 | return 0; |
| 788 | |
| 789 | out_put_unused_fd: |
| 790 | put_unused_fd(fd); |
| 791 | out_free_linereq: |
| 792 | linereq_free(lr); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | #ifdef CONFIG_GPIO_CDEV_V1 |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * GPIO line event management |
| 800 | */ |
| 801 | |
| 802 | /** |
| 803 | * struct lineevent_state - contains the state of a userspace event |
| 804 | * @gdev: the GPIO device the event pertains to |
| 805 | * @label: consumer label used to tag descriptors |
| 806 | * @desc: the GPIO descriptor held by this event |
| 807 | * @eflags: the event flags this line was requested with |
| 808 | * @irq: the interrupt that trigger in response to events on this GPIO |
| 809 | * @wait: wait queue that handles blocking reads of events |
| 810 | * @events: KFIFO for the GPIO events |
| 811 | * @timestamp: cache for the timestamp storing it between hardirq |
| 812 | * and IRQ thread, used to bring the timestamp close to the actual |
| 813 | * event |
| 814 | */ |
| 815 | struct lineevent_state { |
| 816 | struct gpio_device *gdev; |
| 817 | const char *label; |
| 818 | struct gpio_desc *desc; |
| 819 | u32 eflags; |
| 820 | int irq; |
| 821 | wait_queue_head_t wait; |
| 822 | DECLARE_KFIFO(events, struct gpioevent_data, 16); |
| 823 | u64 timestamp; |
| 824 | }; |
| 825 | |
| 826 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ |
| 827 | (GPIOEVENT_REQUEST_RISING_EDGE | \ |
| 828 | GPIOEVENT_REQUEST_FALLING_EDGE) |
| 829 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 830 | static __poll_t lineevent_poll(struct file *file, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 831 | struct poll_table_struct *wait) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 832 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 833 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 834 | __poll_t events = 0; |
| 835 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 836 | poll_wait(file, &le->wait, wait); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 837 | |
| 838 | if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) |
| 839 | events = EPOLLIN | EPOLLRDNORM; |
| 840 | |
| 841 | return events; |
| 842 | } |
| 843 | |
| 844 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 845 | static ssize_t lineevent_read(struct file *file, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 846 | char __user *buf, |
| 847 | size_t count, |
| 848 | loff_t *f_ps) |
| 849 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 850 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 851 | struct gpioevent_data ge; |
| 852 | ssize_t bytes_read = 0; |
| 853 | int ret; |
| 854 | |
| 855 | if (count < sizeof(ge)) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | do { |
| 859 | spin_lock(&le->wait.lock); |
| 860 | if (kfifo_is_empty(&le->events)) { |
| 861 | if (bytes_read) { |
| 862 | spin_unlock(&le->wait.lock); |
| 863 | return bytes_read; |
| 864 | } |
| 865 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 866 | if (file->f_flags & O_NONBLOCK) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 867 | spin_unlock(&le->wait.lock); |
| 868 | return -EAGAIN; |
| 869 | } |
| 870 | |
| 871 | ret = wait_event_interruptible_locked(le->wait, |
| 872 | !kfifo_is_empty(&le->events)); |
| 873 | if (ret) { |
| 874 | spin_unlock(&le->wait.lock); |
| 875 | return ret; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | ret = kfifo_out(&le->events, &ge, 1); |
| 880 | spin_unlock(&le->wait.lock); |
| 881 | if (ret != 1) { |
| 882 | /* |
| 883 | * This should never happen - we were holding the lock |
| 884 | * from the moment we learned the fifo is no longer |
| 885 | * empty until now. |
| 886 | */ |
| 887 | ret = -EIO; |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | if (copy_to_user(buf + bytes_read, &ge, sizeof(ge))) |
| 892 | return -EFAULT; |
| 893 | bytes_read += sizeof(ge); |
| 894 | } while (count >= bytes_read + sizeof(ge)); |
| 895 | |
| 896 | return bytes_read; |
| 897 | } |
| 898 | |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 899 | static void lineevent_free(struct lineevent_state *le) |
| 900 | { |
| 901 | if (le->irq) |
| 902 | free_irq(le->irq, le); |
| 903 | if (le->desc) |
| 904 | gpiod_free(le->desc); |
| 905 | kfree(le->label); |
| 906 | put_device(&le->gdev->dev); |
| 907 | kfree(le); |
| 908 | } |
| 909 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 910 | static int lineevent_release(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 911 | { |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 912 | lineevent_free(file->private_data); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 916 | static long lineevent_ioctl(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 917 | unsigned long arg) |
| 918 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 919 | struct lineevent_state *le = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 920 | void __user *ip = (void __user *)arg; |
| 921 | struct gpiohandle_data ghd; |
| 922 | |
| 923 | /* |
| 924 | * We can get the value for an event line but not set it, |
| 925 | * because it is input by definition. |
| 926 | */ |
| 927 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 928 | int val; |
| 929 | |
| 930 | memset(&ghd, 0, sizeof(ghd)); |
| 931 | |
| 932 | val = gpiod_get_value_cansleep(le->desc); |
| 933 | if (val < 0) |
| 934 | return val; |
| 935 | ghd.values[0] = val; |
| 936 | |
| 937 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 938 | return -EFAULT; |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | |
| 945 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 946 | static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 947 | unsigned long arg) |
| 948 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 949 | return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 950 | } |
| 951 | #endif |
| 952 | |
| 953 | static const struct file_operations lineevent_fileops = { |
| 954 | .release = lineevent_release, |
| 955 | .read = lineevent_read, |
| 956 | .poll = lineevent_poll, |
| 957 | .owner = THIS_MODULE, |
| 958 | .llseek = noop_llseek, |
| 959 | .unlocked_ioctl = lineevent_ioctl, |
| 960 | #ifdef CONFIG_COMPAT |
| 961 | .compat_ioctl = lineevent_ioctl_compat, |
| 962 | #endif |
| 963 | }; |
| 964 | |
| 965 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
| 966 | { |
| 967 | struct lineevent_state *le = p; |
| 968 | struct gpioevent_data ge; |
| 969 | int ret; |
| 970 | |
| 971 | /* Do not leak kernel stack to userspace */ |
| 972 | memset(&ge, 0, sizeof(ge)); |
| 973 | |
| 974 | /* |
| 975 | * We may be running from a nested threaded interrupt in which case |
| 976 | * we didn't get the timestamp from lineevent_irq_handler(). |
| 977 | */ |
| 978 | if (!le->timestamp) |
| 979 | ge.timestamp = ktime_get_ns(); |
| 980 | else |
| 981 | ge.timestamp = le->timestamp; |
| 982 | |
| 983 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
| 984 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
| 985 | int level = gpiod_get_value_cansleep(le->desc); |
| 986 | |
| 987 | if (level) |
| 988 | /* Emit low-to-high event */ |
| 989 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 990 | else |
| 991 | /* Emit high-to-low event */ |
| 992 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
| 993 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { |
| 994 | /* Emit low-to-high event */ |
| 995 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 996 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
| 997 | /* Emit high-to-low event */ |
| 998 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
| 999 | } else { |
| 1000 | return IRQ_NONE; |
| 1001 | } |
| 1002 | |
| 1003 | ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, |
| 1004 | 1, &le->wait.lock); |
| 1005 | if (ret) |
| 1006 | wake_up_poll(&le->wait, EPOLLIN); |
| 1007 | else |
| 1008 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); |
| 1009 | |
| 1010 | return IRQ_HANDLED; |
| 1011 | } |
| 1012 | |
| 1013 | static irqreturn_t lineevent_irq_handler(int irq, void *p) |
| 1014 | { |
| 1015 | struct lineevent_state *le = p; |
| 1016 | |
| 1017 | /* |
| 1018 | * Just store the timestamp in hardirq context so we get it as |
| 1019 | * close in time as possible to the actual event. |
| 1020 | */ |
| 1021 | le->timestamp = ktime_get_ns(); |
| 1022 | |
| 1023 | return IRQ_WAKE_THREAD; |
| 1024 | } |
| 1025 | |
| 1026 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
| 1027 | { |
| 1028 | struct gpioevent_request eventreq; |
| 1029 | struct lineevent_state *le; |
| 1030 | struct gpio_desc *desc; |
| 1031 | struct file *file; |
| 1032 | u32 offset; |
| 1033 | u32 lflags; |
| 1034 | u32 eflags; |
| 1035 | int fd; |
| 1036 | int ret; |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1037 | int irq, irqflags = 0; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1038 | |
| 1039 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) |
| 1040 | return -EFAULT; |
| 1041 | |
| 1042 | offset = eventreq.lineoffset; |
| 1043 | lflags = eventreq.handleflags; |
| 1044 | eflags = eventreq.eventflags; |
| 1045 | |
| 1046 | desc = gpiochip_get_desc(gdev->chip, offset); |
| 1047 | if (IS_ERR(desc)) |
| 1048 | return PTR_ERR(desc); |
| 1049 | |
| 1050 | /* Return an error if a unknown flag is set */ |
| 1051 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || |
| 1052 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) |
| 1053 | return -EINVAL; |
| 1054 | |
| 1055 | /* This is just wrong: we don't look for events on output lines */ |
| 1056 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || |
| 1057 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 1058 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 1059 | return -EINVAL; |
| 1060 | |
| 1061 | /* Only one bias flag can be set. */ |
| 1062 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 1063 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
| 1064 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
| 1065 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 1066 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 1067 | return -EINVAL; |
| 1068 | |
| 1069 | le = kzalloc(sizeof(*le), GFP_KERNEL); |
| 1070 | if (!le) |
| 1071 | return -ENOMEM; |
| 1072 | le->gdev = gdev; |
| 1073 | get_device(&gdev->dev); |
| 1074 | |
| 1075 | /* Make sure this is terminated */ |
| 1076 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; |
| 1077 | if (strlen(eventreq.consumer_label)) { |
| 1078 | le->label = kstrdup(eventreq.consumer_label, |
| 1079 | GFP_KERNEL); |
| 1080 | if (!le->label) { |
| 1081 | ret = -ENOMEM; |
| 1082 | goto out_free_le; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | ret = gpiod_request(desc, le->label); |
| 1087 | if (ret) |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1088 | goto out_free_le; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1089 | le->desc = desc; |
| 1090 | le->eflags = eflags; |
| 1091 | |
Kent Gibson | c274b58 | 2020-07-08 12:15:47 +0800 | [diff] [blame] | 1092 | linehandle_flags_to_desc_flags(lflags, &desc->flags); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1093 | |
| 1094 | ret = gpiod_direction_input(desc); |
| 1095 | if (ret) |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1096 | goto out_free_le; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1097 | |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 1098 | blocking_notifier_call_chain(&desc->gdev->notifier, |
| 1099 | GPIOLINE_CHANGED_REQUESTED, desc); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1100 | |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1101 | irq = gpiod_to_irq(desc); |
| 1102 | if (irq <= 0) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1103 | ret = -ENODEV; |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1104 | goto out_free_le; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1105 | } |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1106 | le->irq = irq; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1107 | |
| 1108 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) |
| 1109 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 1110 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
| 1111 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
| 1112 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 1113 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
| 1114 | irqflags |= IRQF_ONESHOT; |
| 1115 | |
| 1116 | INIT_KFIFO(le->events); |
| 1117 | init_waitqueue_head(&le->wait); |
| 1118 | |
| 1119 | /* Request a thread to read the events */ |
| 1120 | ret = request_threaded_irq(le->irq, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 1121 | lineevent_irq_handler, |
| 1122 | lineevent_irq_thread, |
| 1123 | irqflags, |
| 1124 | le->label, |
| 1125 | le); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1126 | if (ret) |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1127 | goto out_free_le; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1128 | |
| 1129 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
| 1130 | if (fd < 0) { |
| 1131 | ret = fd; |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1132 | goto out_free_le; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | file = anon_inode_getfile("gpio-event", |
| 1136 | &lineevent_fileops, |
| 1137 | le, |
| 1138 | O_RDONLY | O_CLOEXEC); |
| 1139 | if (IS_ERR(file)) { |
| 1140 | ret = PTR_ERR(file); |
| 1141 | goto out_put_unused_fd; |
| 1142 | } |
| 1143 | |
| 1144 | eventreq.fd = fd; |
| 1145 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
| 1146 | /* |
| 1147 | * fput() will trigger the release() callback, so do not go onto |
| 1148 | * the regular error cleanup path here. |
| 1149 | */ |
| 1150 | fput(file); |
| 1151 | put_unused_fd(fd); |
| 1152 | return -EFAULT; |
| 1153 | } |
| 1154 | |
| 1155 | fd_install(fd, file); |
| 1156 | |
| 1157 | return 0; |
| 1158 | |
| 1159 | out_put_unused_fd: |
| 1160 | put_unused_fd(fd); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1161 | out_free_le: |
Kent Gibson | 4682427 | 2020-07-08 12:15:56 +0800 | [diff] [blame] | 1162 | lineevent_free(le); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 1166 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
| 1167 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1168 | static void gpio_desc_to_lineinfo(struct gpio_desc *desc, |
| 1169 | struct gpioline_info *info) |
| 1170 | { |
| 1171 | struct gpio_chip *gc = desc->gdev->chip; |
| 1172 | bool ok_for_pinctrl; |
| 1173 | unsigned long flags; |
| 1174 | |
Kent Gibson | 69e4e13 | 2020-09-28 08:27:49 +0800 | [diff] [blame] | 1175 | memset(info, 0, sizeof(*info)); |
Kent Gibson | 0dc11e3 | 2020-09-28 08:27:48 +0800 | [diff] [blame] | 1176 | info->line_offset = gpio_chip_hwgpio(desc); |
| 1177 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1178 | /* |
| 1179 | * This function takes a mutex so we must check this before taking |
| 1180 | * the spinlock. |
| 1181 | * |
| 1182 | * FIXME: find a non-racy way to retrieve this information. Maybe a |
| 1183 | * lock common to both frameworks? |
| 1184 | */ |
| 1185 | ok_for_pinctrl = |
| 1186 | pinctrl_gpio_can_use_line(gc->base + info->line_offset); |
| 1187 | |
| 1188 | spin_lock_irqsave(&gpio_lock, flags); |
| 1189 | |
Kent Gibson | 69e4e13 | 2020-09-28 08:27:49 +0800 | [diff] [blame] | 1190 | if (desc->name) |
| 1191 | strscpy(info->name, desc->name, sizeof(info->name)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1192 | |
Kent Gibson | 69e4e13 | 2020-09-28 08:27:49 +0800 | [diff] [blame] | 1193 | if (desc->label) |
| 1194 | strscpy(info->consumer, desc->label, sizeof(info->consumer)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1195 | |
| 1196 | /* |
| 1197 | * Userspace only need to know that the kernel is using this GPIO so |
| 1198 | * it can't use it. |
| 1199 | */ |
| 1200 | info->flags = 0; |
| 1201 | if (test_bit(FLAG_REQUESTED, &desc->flags) || |
| 1202 | test_bit(FLAG_IS_HOGGED, &desc->flags) || |
| 1203 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || |
| 1204 | test_bit(FLAG_EXPORT, &desc->flags) || |
| 1205 | test_bit(FLAG_SYSFS, &desc->flags) || |
| 1206 | !ok_for_pinctrl) |
| 1207 | info->flags |= GPIOLINE_FLAG_KERNEL; |
| 1208 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
| 1209 | info->flags |= GPIOLINE_FLAG_IS_OUT; |
| 1210 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1211 | info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
| 1212 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1213 | info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | |
| 1214 | GPIOLINE_FLAG_IS_OUT); |
| 1215 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1216 | info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | |
| 1217 | GPIOLINE_FLAG_IS_OUT); |
| 1218 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
| 1219 | info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; |
| 1220 | if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
| 1221 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; |
| 1222 | if (test_bit(FLAG_PULL_UP, &desc->flags)) |
| 1223 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; |
| 1224 | |
| 1225 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1226 | } |
| 1227 | |
| 1228 | struct gpio_chardev_data { |
| 1229 | struct gpio_device *gdev; |
| 1230 | wait_queue_head_t wait; |
| 1231 | DECLARE_KFIFO(events, struct gpioline_info_changed, 32); |
| 1232 | struct notifier_block lineinfo_changed_nb; |
| 1233 | unsigned long *watched_lines; |
| 1234 | }; |
| 1235 | |
| 1236 | /* |
| 1237 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
| 1238 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1239 | 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] | 1240 | { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1241 | struct gpio_chardev_data *cdev = file->private_data; |
| 1242 | struct gpio_device *gdev = cdev->gdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1243 | struct gpio_chip *gc = gdev->chip; |
| 1244 | void __user *ip = (void __user *)arg; |
| 1245 | struct gpio_desc *desc; |
| 1246 | __u32 offset; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1247 | |
| 1248 | /* We fail any subsequent ioctl():s when the chip is gone */ |
| 1249 | if (!gc) |
| 1250 | return -ENODEV; |
| 1251 | |
| 1252 | /* Fill in the struct and pass to userspace */ |
| 1253 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
| 1254 | struct gpiochip_info chipinfo; |
| 1255 | |
| 1256 | memset(&chipinfo, 0, sizeof(chipinfo)); |
| 1257 | |
Kent Gibson | 69e4e13 | 2020-09-28 08:27:49 +0800 | [diff] [blame] | 1258 | strscpy(chipinfo.name, dev_name(&gdev->dev), |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1259 | sizeof(chipinfo.name)); |
Kent Gibson | 69e4e13 | 2020-09-28 08:27:49 +0800 | [diff] [blame] | 1260 | strscpy(chipinfo.label, gdev->label, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1261 | sizeof(chipinfo.label)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1262 | chipinfo.lines = gdev->ngpio; |
| 1263 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
| 1264 | return -EFAULT; |
| 1265 | return 0; |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 1266 | #ifdef CONFIG_GPIO_CDEV_V1 |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1267 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
| 1268 | struct gpioline_info lineinfo; |
| 1269 | |
| 1270 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 1271 | return -EFAULT; |
| 1272 | |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1273 | /* this doubles as a range check on line_offset */ |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1274 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
| 1275 | if (IS_ERR(desc)) |
| 1276 | return PTR_ERR(desc); |
| 1277 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1278 | gpio_desc_to_lineinfo(desc, &lineinfo); |
| 1279 | |
| 1280 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
| 1281 | return -EFAULT; |
| 1282 | return 0; |
| 1283 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
| 1284 | return linehandle_create(gdev, ip); |
| 1285 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
| 1286 | return lineevent_create(gdev, ip); |
| 1287 | } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { |
| 1288 | struct gpioline_info lineinfo; |
| 1289 | |
| 1290 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 1291 | return -EFAULT; |
| 1292 | |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1293 | /* this doubles as a range check on line_offset */ |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1294 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
| 1295 | if (IS_ERR(desc)) |
| 1296 | return PTR_ERR(desc); |
| 1297 | |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1298 | if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1299 | return -EBUSY; |
| 1300 | |
| 1301 | gpio_desc_to_lineinfo(desc, &lineinfo); |
| 1302 | |
Kent Gibson | f30ef3e | 2020-07-08 12:15:53 +0800 | [diff] [blame] | 1303 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1304 | clear_bit(lineinfo.line_offset, cdev->watched_lines); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1305 | return -EFAULT; |
Kent Gibson | f30ef3e | 2020-07-08 12:15:53 +0800 | [diff] [blame] | 1306 | } |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1307 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1308 | return 0; |
Kent Gibson | 3c0d9c6 | 2020-09-28 08:27:54 +0800 | [diff] [blame^] | 1309 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
| 1310 | } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { |
| 1311 | return linereq_create(gdev, ip); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1312 | } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { |
| 1313 | if (copy_from_user(&offset, ip, sizeof(offset))) |
| 1314 | return -EFAULT; |
| 1315 | |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1316 | if (offset >= cdev->gdev->ngpio) |
| 1317 | return -EINVAL; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1318 | |
Kent Gibson | 1bf7ba4 | 2020-07-08 12:15:54 +0800 | [diff] [blame] | 1319 | if (!test_and_clear_bit(offset, cdev->watched_lines)) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1320 | return -EBUSY; |
| 1321 | |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | return -EINVAL; |
| 1325 | } |
| 1326 | |
| 1327 | #ifdef CONFIG_COMPAT |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1328 | static long gpio_ioctl_compat(struct file *file, unsigned int cmd, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1329 | unsigned long arg) |
| 1330 | { |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1331 | return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1332 | } |
| 1333 | #endif |
| 1334 | |
| 1335 | static struct gpio_chardev_data * |
| 1336 | to_gpio_chardev_data(struct notifier_block *nb) |
| 1337 | { |
| 1338 | return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); |
| 1339 | } |
| 1340 | |
| 1341 | static int lineinfo_changed_notify(struct notifier_block *nb, |
| 1342 | unsigned long action, void *data) |
| 1343 | { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1344 | struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1345 | struct gpioline_info_changed chg; |
| 1346 | struct gpio_desc *desc = data; |
| 1347 | int ret; |
| 1348 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1349 | if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1350 | return NOTIFY_DONE; |
| 1351 | |
| 1352 | memset(&chg, 0, sizeof(chg)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1353 | chg.event_type = action; |
| 1354 | chg.timestamp = ktime_get_ns(); |
| 1355 | gpio_desc_to_lineinfo(desc, &chg.info); |
| 1356 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1357 | ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1358 | if (ret) |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1359 | wake_up_poll(&cdev->wait, EPOLLIN); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1360 | else |
| 1361 | pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); |
| 1362 | |
| 1363 | return NOTIFY_OK; |
| 1364 | } |
| 1365 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1366 | static __poll_t lineinfo_watch_poll(struct file *file, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1367 | struct poll_table_struct *pollt) |
| 1368 | { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1369 | struct gpio_chardev_data *cdev = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1370 | __poll_t events = 0; |
| 1371 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1372 | poll_wait(file, &cdev->wait, pollt); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1373 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1374 | if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, |
| 1375 | &cdev->wait.lock)) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1376 | events = EPOLLIN | EPOLLRDNORM; |
| 1377 | |
| 1378 | return events; |
| 1379 | } |
| 1380 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1381 | static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1382 | size_t count, loff_t *off) |
| 1383 | { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1384 | struct gpio_chardev_data *cdev = file->private_data; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1385 | struct gpioline_info_changed event; |
| 1386 | ssize_t bytes_read = 0; |
| 1387 | int ret; |
| 1388 | |
| 1389 | if (count < sizeof(event)) |
| 1390 | return -EINVAL; |
| 1391 | |
| 1392 | do { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1393 | spin_lock(&cdev->wait.lock); |
| 1394 | if (kfifo_is_empty(&cdev->events)) { |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1395 | if (bytes_read) { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1396 | spin_unlock(&cdev->wait.lock); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1397 | return bytes_read; |
| 1398 | } |
| 1399 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1400 | if (file->f_flags & O_NONBLOCK) { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1401 | spin_unlock(&cdev->wait.lock); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1402 | return -EAGAIN; |
| 1403 | } |
| 1404 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1405 | ret = wait_event_interruptible_locked(cdev->wait, |
| 1406 | !kfifo_is_empty(&cdev->events)); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1407 | if (ret) { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1408 | spin_unlock(&cdev->wait.lock); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
| 1411 | } |
| 1412 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1413 | ret = kfifo_out(&cdev->events, &event, 1); |
| 1414 | spin_unlock(&cdev->wait.lock); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1415 | if (ret != 1) { |
| 1416 | ret = -EIO; |
| 1417 | break; |
| 1418 | /* We should never get here. See lineevent_read(). */ |
| 1419 | } |
| 1420 | |
| 1421 | if (copy_to_user(buf + bytes_read, &event, sizeof(event))) |
| 1422 | return -EFAULT; |
| 1423 | bytes_read += sizeof(event); |
| 1424 | } while (count >= bytes_read + sizeof(event)); |
| 1425 | |
| 1426 | return bytes_read; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * gpio_chrdev_open() - open the chardev for ioctl operations |
| 1431 | * @inode: inode for this chardev |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1432 | * @file: file struct for storing private data |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1433 | * Returns 0 on success |
| 1434 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1435 | static int gpio_chrdev_open(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1436 | { |
| 1437 | struct gpio_device *gdev = container_of(inode->i_cdev, |
Kent Gibson | a18512e | 2020-07-08 12:15:46 +0800 | [diff] [blame] | 1438 | struct gpio_device, chrdev); |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1439 | struct gpio_chardev_data *cdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1440 | int ret = -ENOMEM; |
| 1441 | |
| 1442 | /* Fail on open if the backing gpiochip is gone */ |
| 1443 | if (!gdev->chip) |
| 1444 | return -ENODEV; |
| 1445 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1446 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
| 1447 | if (!cdev) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1448 | return -ENOMEM; |
| 1449 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1450 | cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); |
| 1451 | if (!cdev->watched_lines) |
| 1452 | goto out_free_cdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1453 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1454 | init_waitqueue_head(&cdev->wait); |
| 1455 | INIT_KFIFO(cdev->events); |
| 1456 | cdev->gdev = gdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1457 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1458 | cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 1459 | ret = blocking_notifier_chain_register(&gdev->notifier, |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1460 | &cdev->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1461 | if (ret) |
| 1462 | goto out_free_bitmap; |
| 1463 | |
| 1464 | get_device(&gdev->dev); |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1465 | file->private_data = cdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1466 | |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1467 | ret = nonseekable_open(inode, file); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1468 | if (ret) |
| 1469 | goto out_unregister_notifier; |
| 1470 | |
| 1471 | return ret; |
| 1472 | |
| 1473 | out_unregister_notifier: |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 1474 | blocking_notifier_chain_unregister(&gdev->notifier, |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1475 | &cdev->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1476 | out_free_bitmap: |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1477 | bitmap_free(cdev->watched_lines); |
| 1478 | out_free_cdev: |
| 1479 | kfree(cdev); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * gpio_chrdev_release() - close chardev after ioctl operations |
| 1485 | * @inode: inode for this chardev |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1486 | * @file: file struct for storing private data |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1487 | * Returns 0 on success |
| 1488 | */ |
Kent Gibson | 49bc527 | 2020-07-08 12:15:48 +0800 | [diff] [blame] | 1489 | static int gpio_chrdev_release(struct inode *inode, struct file *file) |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1490 | { |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1491 | struct gpio_chardev_data *cdev = file->private_data; |
| 1492 | struct gpio_device *gdev = cdev->gdev; |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1493 | |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1494 | bitmap_free(cdev->watched_lines); |
Kent Gibson | 6accc37 | 2020-07-08 12:15:51 +0800 | [diff] [blame] | 1495 | blocking_notifier_chain_unregister(&gdev->notifier, |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1496 | &cdev->lineinfo_changed_nb); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1497 | put_device(&gdev->dev); |
Kent Gibson | e2b781c5 | 2020-07-08 12:15:52 +0800 | [diff] [blame] | 1498 | kfree(cdev); |
Kent Gibson | 925ca36 | 2020-06-16 17:36:15 +0800 | [diff] [blame] | 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static const struct file_operations gpio_fileops = { |
| 1504 | .release = gpio_chrdev_release, |
| 1505 | .open = gpio_chrdev_open, |
| 1506 | .poll = lineinfo_watch_poll, |
| 1507 | .read = lineinfo_watch_read, |
| 1508 | .owner = THIS_MODULE, |
| 1509 | .llseek = no_llseek, |
| 1510 | .unlocked_ioctl = gpio_ioctl, |
| 1511 | #ifdef CONFIG_COMPAT |
| 1512 | .compat_ioctl = gpio_ioctl_compat, |
| 1513 | #endif |
| 1514 | }; |
| 1515 | |
| 1516 | int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) |
| 1517 | { |
| 1518 | int ret; |
| 1519 | |
| 1520 | cdev_init(&gdev->chrdev, &gpio_fileops); |
| 1521 | gdev->chrdev.owner = THIS_MODULE; |
| 1522 | gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); |
| 1523 | |
| 1524 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); |
| 1525 | if (ret) |
| 1526 | return ret; |
| 1527 | |
| 1528 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", |
| 1529 | MAJOR(devt), gdev->id); |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | void gpiolib_cdev_unregister(struct gpio_device *gdev) |
| 1535 | { |
| 1536 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
| 1537 | } |