1
2
3
4
5
6
7
8
9#include <unistd.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <errno.h>
13#include <string.h>
14#include <fcntl.h>
15#include <getopt.h>
16#include <sys/ioctl.h>
17#include <linux/gpio.h>
18#include "gpio-utils.h"
19
20#define CONSUMER "gpio-utils"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57int gpiotools_request_line(const char *device_name, unsigned int *lines,
58 unsigned int num_lines,
59 struct gpio_v2_line_config *config,
60 const char *consumer)
61{
62 struct gpio_v2_line_request req;
63 char *chrdev_name;
64 int fd;
65 int i;
66 int ret;
67
68 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
69 if (ret < 0)
70 return -ENOMEM;
71
72 fd = open(chrdev_name, 0);
73 if (fd == -1) {
74 ret = -errno;
75 fprintf(stderr, "Failed to open %s, %s\n",
76 chrdev_name, strerror(errno));
77 goto exit_free_name;
78 }
79
80 memset(&req, 0, sizeof(req));
81 for (i = 0; i < num_lines; i++)
82 req.offsets[i] = lines[i];
83
84 req.config = *config;
85 strcpy(req.consumer, consumer);
86 req.num_lines = num_lines;
87
88 ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
89 if (ret == -1) {
90 ret = -errno;
91 fprintf(stderr, "Failed to issue %s (%d), %s\n",
92 "GPIO_GET_LINE_IOCTL", ret, strerror(errno));
93 }
94
95 if (close(fd) == -1)
96 perror("Failed to close GPIO character device file");
97exit_free_name:
98 free(chrdev_name);
99 return ret < 0 ? ret : req.fd;
100}
101
102
103
104
105
106
107
108
109
110
111int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values)
112{
113 int ret;
114
115 ret = ioctl(fd, GPIO_V2_LINE_SET_VALUES_IOCTL, values);
116 if (ret == -1) {
117 ret = -errno;
118 fprintf(stderr, "Failed to issue %s (%d), %s\n",
119 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret,
120 strerror(errno));
121 }
122
123 return ret;
124}
125
126
127
128
129
130
131
132
133
134
135int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values)
136{
137 int ret;
138
139 ret = ioctl(fd, GPIO_V2_LINE_GET_VALUES_IOCTL, values);
140 if (ret == -1) {
141 ret = -errno;
142 fprintf(stderr, "Failed to issue %s (%d), %s\n",
143 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret,
144 strerror(errno));
145 }
146
147 return ret;
148}
149
150
151
152
153
154
155
156
157
158int gpiotools_release_line(const int fd)
159{
160 int ret;
161
162 ret = close(fd);
163 if (ret == -1) {
164 perror("Failed to close GPIO LINE device file");
165 ret = -errno;
166 }
167
168 return ret;
169}
170
171
172
173
174
175
176
177
178
179
180int gpiotools_get(const char *device_name, unsigned int line)
181{
182 int ret;
183 unsigned int value;
184 unsigned int lines[] = {line};
185
186 ret = gpiotools_gets(device_name, lines, 1, &value);
187 if (ret)
188 return ret;
189 return value;
190}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205int gpiotools_gets(const char *device_name, unsigned int *lines,
206 unsigned int num_lines, unsigned int *values)
207{
208 int fd, i;
209 int ret;
210 int ret_close;
211 struct gpio_v2_line_config config;
212 struct gpio_v2_line_values lv;
213
214 memset(&config, 0, sizeof(config));
215 config.flags = GPIO_V2_LINE_FLAG_INPUT;
216 ret = gpiotools_request_line(device_name, lines, num_lines,
217 &config, CONSUMER);
218 if (ret < 0)
219 return ret;
220
221 fd = ret;
222 for (i = 0; i < num_lines; i++)
223 gpiotools_set_bit(&lv.mask, i);
224 ret = gpiotools_get_values(fd, &lv);
225 if (!ret)
226 for (i = 0; i < num_lines; i++)
227 values[i] = gpiotools_test_bit(lv.bits, i);
228 ret_close = gpiotools_release_line(fd);
229 return ret < 0 ? ret : ret_close;
230}
231
232
233
234
235
236
237
238
239
240
241
242int gpiotools_set(const char *device_name, unsigned int line,
243 unsigned int value)
244{
245 unsigned int lines[] = {line};
246
247 return gpiotools_sets(device_name, lines, 1, &value);
248}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263int gpiotools_sets(const char *device_name, unsigned int *lines,
264 unsigned int num_lines, unsigned int *values)
265{
266 int ret, i;
267 struct gpio_v2_line_config config;
268
269 memset(&config, 0, sizeof(config));
270 config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
271 config.num_attrs = 1;
272 config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES;
273 for (i = 0; i < num_lines; i++) {
274 gpiotools_set_bit(&config.attrs[0].mask, i);
275 gpiotools_assign_bit(&config.attrs[0].attr.values,
276 i, values[i]);
277 }
278 ret = gpiotools_request_line(device_name, lines, num_lines,
279 &config, CONSUMER);
280 if (ret < 0)
281 return ret;
282
283 return gpiotools_release_line(ret);
284}
285