Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: gpio: add multi-line monitoring to gpio-event-mon

Extend gpio-event-mon to support monitoring multiple lines.
This would require multiple lineevent requests to implement using uAPI v1,
but can be performed with a single line request using uAPI v2.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

authored by

Kent Gibson and committed by
Bartosz Golaszewski
62757c32 0acda979

+34 -11
+34 -11
tools/gpio/gpio-event-mon.c
··· 26 26 #include "gpio-utils.h" 27 27 28 28 int monitor_device(const char *device_name, 29 - unsigned int line, 29 + unsigned int *lines, 30 + unsigned int num_lines, 30 31 struct gpio_v2_line_config *config, 31 32 unsigned int loops) 32 33 { ··· 48 47 goto exit_free_name; 49 48 } 50 49 51 - ret = gpiotools_request_line(device_name, &line, 1, config, 50 + ret = gpiotools_request_line(device_name, lines, num_lines, config, 52 51 "gpio-event-mon"); 53 52 if (ret < 0) 54 53 goto exit_device_close; ··· 56 55 lfd = ret; 57 56 58 57 /* Read initial states */ 59 - values.mask = 1; 58 + values.mask = 0; 60 59 values.bits = 0; 60 + for (i = 0; i < num_lines; i++) 61 + gpiotools_set_bit(&values.mask, i); 61 62 ret = gpiotools_get_values(lfd, &values); 62 63 if (ret < 0) { 63 64 fprintf(stderr, ··· 68 65 goto exit_line_close; 69 66 } 70 67 71 - fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); 72 - fprintf(stdout, "Initial line value: %d\n", 73 - gpiotools_test_bit(values.bits, 0)); 68 + if (num_lines == 1) { 69 + fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name); 70 + fprintf(stdout, "Initial line value: %d\n", 71 + gpiotools_test_bit(values.bits, 0)); 72 + } else { 73 + fprintf(stdout, "Monitoring lines %d", lines[0]); 74 + for (i = 1; i < num_lines - 1; i++) 75 + fprintf(stdout, ", %d", lines[i]); 76 + fprintf(stdout, " and %d on %s\n", lines[i], device_name); 77 + fprintf(stdout, "Initial line values: %d", 78 + gpiotools_test_bit(values.bits, 0)); 79 + for (i = 1; i < num_lines - 1; i++) 80 + fprintf(stdout, ", %d", 81 + gpiotools_test_bit(values.bits, i)); 82 + fprintf(stdout, " and %d\n", 83 + gpiotools_test_bit(values.bits, i)); 84 + } 74 85 75 86 while (1) { 76 87 struct gpio_v2_line_event event; ··· 143 126 fprintf(stderr, "Usage: gpio-event-mon [options]...\n" 144 127 "Listen to events on GPIO lines, 0->1 1->0\n" 145 128 " -n <name> Listen on GPIOs on a named device (must be stated)\n" 146 - " -o <n> Offset to monitor\n" 129 + " -o <n> Offset of line to monitor (may be repeated)\n" 147 130 " -d Set line as open drain\n" 148 131 " -s Set line as open source\n" 149 132 " -r Listen for rising edges\n" ··· 163 146 int main(int argc, char **argv) 164 147 { 165 148 const char *device_name = NULL; 166 - unsigned int line = -1; 149 + unsigned int lines[GPIO_V2_LINES_MAX]; 150 + unsigned int num_lines = 0; 167 151 unsigned int loops = 0; 168 152 struct gpio_v2_line_config config; 169 153 int c; ··· 180 162 device_name = optarg; 181 163 break; 182 164 case 'o': 183 - line = strtoul(optarg, NULL, 10); 165 + if (num_lines >= GPIO_V2_LINES_MAX) { 166 + print_usage(); 167 + return -1; 168 + } 169 + lines[num_lines] = strtoul(optarg, NULL, 10); 170 + num_lines++; 184 171 break; 185 172 case 'd': 186 173 config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; ··· 205 182 } 206 183 } 207 184 208 - if (!device_name || line == -1) { 185 + if (!device_name || num_lines == 0) { 209 186 print_usage(); 210 187 return -1; 211 188 } ··· 214 191 "falling edges\n"); 215 192 config.flags |= EDGE_FLAGS; 216 193 } 217 - return monitor_device(device_name, line, &config, loops); 194 + return monitor_device(device_name, lines, num_lines, &config, loops); 218 195 }