Debian Codegraph @

lib/chip.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]>
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <gpiod.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include "internal.h"
13
14 struct gpiod_chip {
15 int fd;
16 char *path;
17 };
18
19 GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path)
20 {
21 struct gpiod_chip *chip;
22 int fd;
23
24 if (!path) {
25 errno = EINVAL;
26 return NULL;
27 }
28
29 if (!gpiod_check_gpiochip_device(path, true))
30 return NULL;
31
32 fd = open(path, O_RDWR | O_CLOEXEC);
33 if (fd < 0)
34 return NULL;
35
36 chip = malloc(sizeof(*chip));
37 if (!chip)
38 goto err_close_fd;
39
40 memset(chip, 0, sizeof(*chip));
41
42 chip->path = strdup(path);
43 if (!chip->path)
44 goto err_free_chip;
45
46 chip->fd = fd;
47
48 return chip;
49
50 err_free_chip:
51 free(chip);
52 err_close_fd:
53 close(fd);
54
55 return NULL;
56 }
57
58 GPIOD_API void gpiod_chip_close(struct gpiod_chip *chip)
59 {
60 if (!chip)
61 return;
62
63 close(chip->fd);
64 free(chip->path);
65 free(chip);
66 }
67
68 static int read_chip_info(int fd, struct gpiochip_info *info)
69 {
70 int ret;
71
72 memset(info, 0, sizeof(*info));
73
74 ret = gpiod_ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, info);
75 if (ret)
76 return -1;
77
78 return 0;
79 }
80
81 GPIOD_API struct gpiod_chip_info *gpiod_chip_get_info(struct gpiod_chip *chip)
82 {
83 struct gpiochip_info info;
84 int ret;
85
86 assert(chip);
87
88 ret = read_chip_info(chip->fd, &info);
89 if (ret)
90 return NULL;
91
92 return gpiod_chip_info_from_uapi(&info);
93 }
94
95 GPIOD_API const char *gpiod_chip_get_path(struct gpiod_chip *chip)
96 {
97 assert(chip);
98
99 return chip->path;
100 }
101
102 static int chip_read_line_info(int fd, unsigned int offset,
103 struct gpio_v2_line_info *info, bool watch)
104 {
105 int ret, cmd;
106
107 memset(info, 0, sizeof(*info));
108 info->offset = offset;
109
110 cmd = watch ? GPIO_V2_GET_LINEINFO_WATCH_IOCTL :
111 GPIO_V2_GET_LINEINFO_IOCTL;
112
113 ret = gpiod_ioctl(fd, cmd, info);
114 if (ret)
115 return -1;
116
117 return 0;
118 }
119
120 static struct gpiod_line_info *
121 chip_get_line_info(struct gpiod_chip *chip, unsigned int offset, bool watch)
122 {
123 struct gpio_v2_line_info info;
124 int ret;
125
126 assert(chip);
127
128 ret = chip_read_line_info(chip->fd, offset, &info, watch);
129 if (ret)
130 return NULL;
131
132 return gpiod_line_info_from_uapi(&info);
133 }
134
135 GPIOD_API struct gpiod_line_info *
136 gpiod_chip_get_line_info(struct gpiod_chip *chip, unsigned int offset)
137 {
138 return chip_get_line_info(chip, offset, false);
139 }
140
141 GPIOD_API struct gpiod_line_info *
142 gpiod_chip_watch_line_info(struct gpiod_chip *chip, unsigned int offset)
143 {
144 return chip_get_line_info(chip, offset, true);
145 }
146
147 GPIOD_API int gpiod_chip_unwatch_line_info(struct gpiod_chip *chip,
148 unsigned int offset)
149 {
150 assert(chip);
151
152 return gpiod_ioctl(chip->fd, GPIO_GET_LINEINFO_UNWATCH_IOCTL, &offset);
153 }
154
155 GPIOD_API int gpiod_chip_get_fd(struct gpiod_chip *chip)
156 {
157 assert(chip);
158
159 return chip->fd;
160 }
161
162 GPIOD_API int gpiod_chip_wait_info_event(struct gpiod_chip *chip,
163 int64_t timeout_ns)
164 {
165 assert(chip);
166
167 return gpiod_poll_fd(chip->fd, timeout_ns);
168 }
169
170 GPIOD_API struct gpiod_info_event *
171 gpiod_chip_read_info_event(struct gpiod_chip *chip)
172 {
173 assert(chip);
174
175 return gpiod_info_event_read_fd(chip->fd);
176 }
177
178 GPIOD_API int gpiod_chip_get_line_offset_from_name(struct gpiod_chip *chip,
179 const char *name)
180 {
181 struct gpio_v2_line_info linfo;
182 struct gpiochip_info chinfo;
183 unsigned int offset;
184 int ret;
185
186 assert(chip);
187
188 if (!name) {
189 errno = EINVAL;
190 return -1;
191 }
192
193 ret = read_chip_info(chip->fd, &chinfo);
194 if (ret)
195 return -1;
196
197 for (offset = 0; offset < chinfo.lines; offset++) {
198 ret = chip_read_line_info(chip->fd, offset, &linfo, false);
199 if (ret)
200 return -1;
201
202 if (strcmp(name, linfo.name) == 0)
203 return offset;
204 }
205
206 errno = ENOENT;
207 return -1;
208 }
209
210 GPIOD_API struct gpiod_line_request *
211 gpiod_chip_request_lines(struct gpiod_chip *chip,
212 struct gpiod_request_config *req_cfg,
213 struct gpiod_line_config *line_cfg)
214 {
215 struct gpio_v2_line_request uapi_req;
216 struct gpiod_line_request *request;
217 struct gpiochip_info info;
218 int ret;
219
220 assert(chip);
221
222 if (!line_cfg) {
223 errno = EINVAL;
224 return NULL;
225 }
226
227 memset(&uapi_req, 0, sizeof(uapi_req));
228
229 if (req_cfg)
230 gpiod_request_config_to_uapi(req_cfg, &uapi_req);
231
232 ret = gpiod_line_config_to_uapi(line_cfg, &uapi_req);
233 if (ret)
234 return NULL;
235
236 ret = read_chip_info(chip->fd, &info);
237 if (ret)
238 return NULL;
239
240 ret = gpiod_ioctl(chip->fd, GPIO_V2_GET_LINE_IOCTL, &uapi_req);
241 if (ret)
242 return NULL;
243
244 request = gpiod_line_request_from_uapi(&uapi_req, info.name);
245 if (!request) {
246 close(uapi_req.fd);
247 return NULL;
248 }
249
250 return request;
251 }