hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/tools/gpio/gpio-hammer.c
....@@ -1,11 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * gpio-hammer - example swiss army knife to shake GPIO lines on a system
34 *
45 * Copyright (C) 2016 Linus Walleij
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
96 *
107 * Usage:
118 * gpio-hammer -n <device-name> -o <offset1> -o <offset2>
....@@ -25,39 +22,46 @@
2522 #include <linux/gpio.h>
2623 #include "gpio-utils.h"
2724
28
-int hammer_device(const char *device_name, unsigned int *lines, int nlines,
25
+int hammer_device(const char *device_name, unsigned int *lines, int num_lines,
2926 unsigned int loops)
3027 {
31
- struct gpiohandle_data data;
28
+ struct gpio_v2_line_values values;
29
+ struct gpio_v2_line_config config;
3230 char swirr[] = "-\\|/";
3331 int fd;
3432 int ret;
3533 int i, j;
3634 unsigned int iteration = 0;
3735
38
- memset(&data.values, 0, sizeof(data.values));
39
- ret = gpiotools_request_linehandle(device_name, lines, nlines,
40
- GPIOHANDLE_REQUEST_OUTPUT, &data,
41
- "gpio-hammer");
36
+ memset(&config, 0, sizeof(config));
37
+ config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
38
+
39
+ ret = gpiotools_request_line(device_name, lines, num_lines,
40
+ &config, "gpio-hammer");
4241 if (ret < 0)
4342 goto exit_error;
4443 else
4544 fd = ret;
4645
47
- ret = gpiotools_get_values(fd, &data);
46
+ values.mask = 0;
47
+ values.bits = 0;
48
+ for (i = 0; i < num_lines; i++)
49
+ gpiotools_set_bit(&values.mask, i);
50
+
51
+ ret = gpiotools_get_values(fd, &values);
4852 if (ret < 0)
4953 goto exit_close_error;
5054
5155 fprintf(stdout, "Hammer lines [");
52
- for (i = 0; i < nlines; i++) {
56
+ for (i = 0; i < num_lines; i++) {
5357 fprintf(stdout, "%d", lines[i]);
54
- if (i != (nlines - 1))
58
+ if (i != (num_lines - 1))
5559 fprintf(stdout, ", ");
5660 }
5761 fprintf(stdout, "] on %s, initial states: [", device_name);
58
- for (i = 0; i < nlines; i++) {
59
- fprintf(stdout, "%d", data.values[i]);
60
- if (i != (nlines - 1))
62
+ for (i = 0; i < num_lines; i++) {
63
+ fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i));
64
+ if (i != (num_lines - 1))
6165 fprintf(stdout, ", ");
6266 }
6367 fprintf(stdout, "]\n");
....@@ -66,27 +70,28 @@
6670 j = 0;
6771 while (1) {
6872 /* Invert all lines so we blink */
69
- for (i = 0; i < nlines; i++)
70
- data.values[i] = !data.values[i];
73
+ for (i = 0; i < num_lines; i++)
74
+ gpiotools_change_bit(&values.bits, i);
7175
72
- ret = gpiotools_set_values(fd, &data);
76
+ ret = gpiotools_set_values(fd, &values);
7377 if (ret < 0)
7478 goto exit_close_error;
7579
7680 /* Re-read values to get status */
77
- ret = gpiotools_get_values(fd, &data);
81
+ ret = gpiotools_get_values(fd, &values);
7882 if (ret < 0)
7983 goto exit_close_error;
8084
8185 fprintf(stdout, "[%c] ", swirr[j]);
8286 j++;
83
- if (j == sizeof(swirr)-1)
87
+ if (j == sizeof(swirr) - 1)
8488 j = 0;
8589
8690 fprintf(stdout, "[");
87
- for (i = 0; i < nlines; i++) {
88
- fprintf(stdout, "%d: %d", lines[i], data.values[i]);
89
- if (i != (nlines - 1))
91
+ for (i = 0; i < num_lines; i++) {
92
+ fprintf(stdout, "%d: %d", lines[i],
93
+ gpiotools_test_bit(values.bits, i));
94
+ if (i != (num_lines - 1))
9095 fprintf(stdout, ", ");
9196 }
9297 fprintf(stdout, "]\r");
....@@ -100,7 +105,7 @@
100105 ret = 0;
101106
102107 exit_close_error:
103
- gpiotools_release_linehandle(fd);
108
+ gpiotools_release_line(fd);
104109 exit_error:
105110 return ret;
106111 }
....@@ -124,7 +129,7 @@
124129 const char *device_name = NULL;
125130 unsigned int lines[GPIOHANDLES_MAX];
126131 unsigned int loops = 0;
127
- int nlines;
132
+ int num_lines;
128133 int c;
129134 int i;
130135
....@@ -156,16 +161,16 @@
156161
157162 if (i >= GPIOHANDLES_MAX) {
158163 fprintf(stderr,
159
- "Only %d occurences of '-o' are allowed, %d were found\n",
164
+ "Only %d occurrences of '-o' are allowed, %d were found\n",
160165 GPIOHANDLES_MAX, i + 1);
161166 return -1;
162167 }
163168
164
- nlines = i;
169
+ num_lines = i;
165170
166
- if (!device_name || !nlines) {
171
+ if (!device_name || !num_lines) {
167172 print_usage();
168173 return -1;
169174 }
170
- return hammer_device(device_name, lines, nlines, loops);
175
+ return hammer_device(device_name, lines, num_lines, loops);
171176 }