summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2016-02-12 16:25:22 -0500
committerLinus Walleij <linus.walleij@linaro.org>2016-02-19 03:48:46 -0500
commit521a2ad6f862a28e2e43cb3e254a26bf0f9452e9 (patch)
tree94cc59e775b6691cf2aded1ef98bfb8a11c3f65a
parentdf4878e969ccc047da45d2cd3af5d08031da1593 (diff)
gpio: add userspace ABI for GPIO line information
This adds a GPIO line ABI for getting name, label and a few select flags from the kernel. This hides the kernel internals and only tells userspace what it may need to know: the different in-kernel consumers are masked behind the flag "kernel" and that is all userspace needs to know. However electric characteristics like active low, open drain etc are reflected to userspace, as this is important information. We provide information on all lines on all chips, later on we will likely add a flag for the chardev consumer so we can filter and display only the lines userspace actually uses in e.g. lsgpio, but then we first need an ABI for userspace to grab and use (get/set/select direction) a GPIO line. Sample output from "lsgpio" on ux500: GPIO chip: gpiochip7, "8011e000.gpio", 32 GPIO lines line 0: unnamed unlabeled line 1: unnamed unlabeled (...) line 25: unnamed "SFH7741 Proximity Sensor" [kernel output open-drain] line 26: unnamed unlabeled (...) Tested-by: Michael Welling <mwelling@ieee.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/gpio/gpiolib.c51
-rw-r--r--include/uapi/linux/gpio.h26
-rw-r--r--tools/gpio/gpio-utils.h2
-rw-r--r--tools/gpio/lsgpio.c91
4 files changed, 156 insertions, 14 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 797c790aa750..3580c0de9d5a 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -331,14 +331,15 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
331 struct gpio_device *gdev = filp->private_data; 331 struct gpio_device *gdev = filp->private_data;
332 struct gpio_chip *chip = gdev->chip; 332 struct gpio_chip *chip = gdev->chip;
333 int __user *ip = (int __user *)arg; 333 int __user *ip = (int __user *)arg;
334 struct gpiochip_info chipinfo;
335 334
336 /* We fail any subsequent ioctl():s when the chip is gone */ 335 /* We fail any subsequent ioctl():s when the chip is gone */
337 if (!chip) 336 if (!chip)
338 return -ENODEV; 337 return -ENODEV;
339 338
339 /* Fill in the struct and pass to userspace */
340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) { 340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
341 /* Fill in the struct and pass to userspace */ 341 struct gpiochip_info chipinfo;
342
342 strncpy(chipinfo.name, dev_name(&gdev->dev), 343 strncpy(chipinfo.name, dev_name(&gdev->dev),
343 sizeof(chipinfo.name)); 344 sizeof(chipinfo.name));
344 chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; 345 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
@@ -349,6 +350,52 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
349 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 350 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
350 return -EFAULT; 351 return -EFAULT;
351 return 0; 352 return 0;
353 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
354 struct gpioline_info lineinfo;
355 struct gpio_desc *desc;
356
357 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
358 return -EFAULT;
359 if (lineinfo.line_offset > gdev->ngpio)
360 return -EINVAL;
361
362 desc = &gdev->descs[lineinfo.line_offset];
363 if (desc->name) {
364 strncpy(lineinfo.name, desc->name,
365 sizeof(lineinfo.name));
366 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
367 } else {
368 lineinfo.name[0] = '\0';
369 }
370 if (desc->label) {
371 strncpy(lineinfo.label, desc->label,
372 sizeof(lineinfo.label));
373 lineinfo.label[sizeof(lineinfo.label)-1] = '\0';
374 } else {
375 lineinfo.label[0] = '\0';
376 }
377
378 /*
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
381 */
382 lineinfo.flags = 0;
383 if (desc->flags & (FLAG_REQUESTED | FLAG_IS_HOGGED |
384 FLAG_USED_AS_IRQ | FLAG_EXPORT |
385 FLAG_SYSFS))
386 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
387 if (desc->flags & FLAG_IS_OUT)
388 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
389 if (desc->flags & FLAG_ACTIVE_LOW)
390 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
391 if (desc->flags & FLAG_OPEN_DRAIN)
392 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
393 if (desc->flags & FLAG_OPEN_SOURCE)
394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
395
396 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
397 return -EFAULT;
398 return 0;
352 } 399 }
353 return -EINVAL; 400 return -EINVAL;
354} 401}
diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h
index 3f93e1bcd3dd..416ce47f2291 100644
--- a/include/uapi/linux/gpio.h
+++ b/include/uapi/linux/gpio.h
@@ -25,6 +25,32 @@ struct gpiochip_info {
25 __u32 lines; 25 __u32 lines;
26}; 26};
27 27
28/* Line is in use by the kernel */
29#define GPIOLINE_FLAG_KERNEL (1UL << 0)
30#define GPIOLINE_FLAG_IS_OUT (1UL << 1)
31#define GPIOLINE_FLAG_ACTIVE_LOW (1UL << 2)
32#define GPIOLINE_FLAG_OPEN_DRAIN (1UL << 3)
33#define GPIOLINE_FLAG_OPEN_SOURCE (1UL << 4)
34
35/**
36 * struct gpioline_info - Information about a certain GPIO line
37 * @line_offset: the local offset on this GPIO device, fill in when
38 * requesting information from the kernel
39 * @flags: various flags for this line
40 * @name: the name of this GPIO line
41 * @label: a functional name for this GPIO line
42 * @kernel: this GPIO is in use by the kernel
43 * @out: this GPIO is an output line (false means it is an input)
44 * @active_low: this GPIO is active low
45 */
46struct gpioline_info {
47 __u32 line_offset;
48 __u32 flags;
49 char name[32];
50 char label[32];
51};
52
28#define GPIO_GET_CHIPINFO_IOCTL _IOR('o', 0x01, struct gpiochip_info) 53#define GPIO_GET_CHIPINFO_IOCTL _IOR('o', 0x01, struct gpiochip_info)
54#define GPIO_GET_LINEINFO_IOCTL _IOWR('o', 0x02, struct gpioline_info)
29 55
30#endif /* _UAPI_GPIO_H_ */ 56#endif /* _UAPI_GPIO_H_ */
diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h
index b18209a45ad3..5f57133b8c04 100644
--- a/tools/gpio/gpio-utils.h
+++ b/tools/gpio/gpio-utils.h
@@ -16,6 +16,8 @@
16 16
17#include <string.h> 17#include <string.h>
18 18
19#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
20
19static inline int check_prefix(const char *str, const char *prefix) 21static inline int check_prefix(const char *str, const char *prefix)
20{ 22{
21 return strlen(str) > strlen(prefix) && 23 return strlen(str) > strlen(prefix) &&
diff --git a/tools/gpio/lsgpio.c b/tools/gpio/lsgpio.c
index 692233f561fb..5535ce81f8f7 100644
--- a/tools/gpio/lsgpio.c
+++ b/tools/gpio/lsgpio.c
@@ -26,12 +26,56 @@
26 26
27#include "gpio-utils.h" 27#include "gpio-utils.h"
28 28
29struct gpio_flag {
30 char *name;
31 unsigned long mask;
32};
33
34struct gpio_flag flagnames[] = {
35 {
36 .name = "kernel",
37 .mask = GPIOLINE_FLAG_KERNEL,
38 },
39 {
40 .name = "output",
41 .mask = GPIOLINE_FLAG_IS_OUT,
42 },
43 {
44 .name = "active-low",
45 .mask = GPIOLINE_FLAG_ACTIVE_LOW,
46 },
47 {
48 .name = "open-drain",
49 .mask = GPIOLINE_FLAG_OPEN_DRAIN,
50 },
51 {
52 .name = "open-source",
53 .mask = GPIOLINE_FLAG_OPEN_SOURCE,
54 },
55};
56
57void print_flags(unsigned long flags)
58{
59 int i;
60 int printed = 0;
61
62 for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
63 if (flags & flagnames[i].mask) {
64 if (printed)
65 fprintf(stdout, " ");
66 fprintf(stdout, "%s", flagnames[i].name);
67 printed++;
68 }
69 }
70}
71
29int list_device(const char *device_name) 72int list_device(const char *device_name)
30{ 73{
31 struct gpiochip_info cinfo; 74 struct gpiochip_info cinfo;
32 char *chrdev_name; 75 char *chrdev_name;
33 int fd; 76 int fd;
34 int ret; 77 int ret;
78 int i;
35 79
36 ret = asprintf(&chrdev_name, "/dev/%s", device_name); 80 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
37 if (ret < 0) 81 if (ret < 0)
@@ -41,32 +85,55 @@ int list_device(const char *device_name)
41 if (fd == -1) { 85 if (fd == -1) {
42 ret = -errno; 86 ret = -errno;
43 fprintf(stderr, "Failed to open %s\n", chrdev_name); 87 fprintf(stderr, "Failed to open %s\n", chrdev_name);
44 goto free_chrdev_name; 88 goto exit_close_error;
45 } 89 }
46 90
47 /* Inspect this GPIO chip */ 91 /* Inspect this GPIO chip */
48 ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo); 92 ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
49 if (ret == -1) { 93 if (ret == -1) {
50 ret = -errno; 94 ret = -errno;
51 fprintf(stderr, "Failed to retrieve GPIO fd\n"); 95 perror("Failed to issue CHIPINFO IOCTL\n");
52 if (close(fd) == -1) 96 goto exit_close_error;
53 perror("Failed to close GPIO character device file");
54
55 goto free_chrdev_name;
56 } 97 }
57 fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n", 98 fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
58 cinfo.name, cinfo.label, cinfo.lines); 99 cinfo.name, cinfo.label, cinfo.lines);
59 100
60 if (close(fd) == -1) { 101 /* Loop over the lines and print info */
61 ret = -errno; 102 for (i = 0; i < cinfo.lines; i++) {
62 goto free_chrdev_name; 103 struct gpioline_info linfo;
104
105 memset(&linfo, 0, sizeof(linfo));
106 linfo.line_offset = i;
107
108 ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo);
109 if (ret == -1) {
110 ret = -errno;
111 perror("Failed to issue LINEINFO IOCTL\n");
112 goto exit_close_error;
113 }
114 fprintf(stdout, "\tline %d:", linfo.line_offset);
115 if (linfo.name[0])
116 fprintf(stdout, " %s", linfo.name);
117 else
118 fprintf(stdout, " unnamed");
119 if (linfo.label[0])
120 fprintf(stdout, " \"%s\"", linfo.label);
121 else
122 fprintf(stdout, " unlabeled");
123 if (linfo.flags) {
124 fprintf(stdout, " [");
125 print_flags(linfo.flags);
126 fprintf(stdout, "]");
127 }
128 fprintf(stdout, "\n");
129
63 } 130 }
64 131
65free_chrdev_name: 132exit_close_error:
133 if (close(fd) == -1)
134 perror("Failed to close GPIO character device file");
66 free(chrdev_name); 135 free(chrdev_name);
67
68 return ret; 136 return ret;
69
70} 137}
71 138
72void print_usage(void) 139void print_usage(void)