| 1 | // SPDX-License-Identifier: LGPL-2.1-or-latercxx . . $ `<file>/lib/chip.c`/ |
| 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>cxx . . $ `<file>/include/gpiod.h`/ |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 13 | |
| 14 | struct gpiod_chipcxx . . $ gpiod_chip# { |
| 15 | int fdcxx . . $ gpiod_chip#fd.; |
| 16 | char *pathcxx . . $ gpiod_chip#path.; |
| 17 | }; |
| 18 | |
| 19 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_chipcxx . . $ gpiod_chip# *gpiod_chip_opencxx . . $ gpiod_chip_open(c9269a3a57179fd4).(const char *path) |
| 20 | { |
| 21 | struct gpiod_chipcxx . . $ gpiod_chip# *chip; |
| 22 | int fd; |
| 23 | |
| 24 | if (!path) { |
| 25 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 26 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 27 | } |
| 28 | |
| 29 | if (!gpiod_check_gpiochip_devicecxx . . $ gpiod_check_gpiochip_device(e96b1cf346ae2c6).(path, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!)) |
| 30 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 31 | |
| 32 | fd = opencxx . . $ open(e156e3aa1e5c4ad1).(path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:142:10`!); |
| 33 | if (fd < 0) |
| 34 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 35 | |
| 36 | chip = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*chip)); |
| 37 | if (!chip) |
| 38 | goto err_close_fd; |
| 39 | |
| 40 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(chip, 0, sizeof(*chip)); |
| 41 | |
| 42 | chip->pathcxx . . $ gpiod_chip#path. = strdupcxx . . $ strdup(a8b8e32347182212).(path); |
| 43 | if (!chip->pathcxx . . $ gpiod_chip#path.) |
| 44 | goto err_free_chip; |
| 45 | |
| 46 | chip->fdcxx . . $ gpiod_chip#fd. = fd; |
| 47 | |
| 48 | return chip; |
| 49 | |
| 50 | err_free_chip: |
| 51 | freecxx . . $ free(5d22bdacc48458e8).(chip); |
| 52 | err_close_fd: |
| 53 | closecxx . . $ close(7864480464b09eea).(fd); |
| 54 | |
| 55 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 56 | } |
| 57 | |
| 58 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! void gpiod_chip_closecxx . . $ gpiod_chip_close(cd4fa8278d41b0b5).(struct gpiod_chipcxx . . $ gpiod_chip# *chip) |
| 59 | { |
| 60 | if (!chip) |
| 61 | return; |
| 62 | |
| 63 | closecxx . . $ close(7864480464b09eea).(chip->fdcxx . . $ gpiod_chip#fd.); |
| 64 | freecxx . . $ free(5d22bdacc48458e8).(chip->pathcxx . . $ gpiod_chip#path.); |
| 65 | freecxx . . $ free(5d22bdacc48458e8).(chip); |
| 66 | } |
| 67 | |
| 68 | static int read_chip_infocxx . . $ read_chip_info(3c216e5ef10af722).(int fd, struct gpiochip_infocxx . . $ gpiochip_info# *info) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(info, 0, sizeof(*info)); |
| 73 | |
| 74 | ret = gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(fd, GPIO_GET_CHIPINFO_IOCTLcxx . . $ `lib/uapi/gpio.h:505:9`!, info); |
| 75 | if (ret) |
| 76 | return -1; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_chip_infocxx . . $ gpiod_chip_info# *gpiod_chip_get_infocxx . . $ gpiod_chip_get_info(42b6d1defdadce5f).(struct gpiod_chipcxx . . $ gpiod_chip# *chip) |
| 82 | { |
| 83 | struct gpiochip_infocxx . . $ gpiochip_info# info; |
| 84 | int ret; |
| 85 | |
| 86 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 87 | |
| 88 | ret = read_chip_infocxx . . $ read_chip_info(3c216e5ef10af722).(chip->fdcxx . . $ gpiod_chip#fd., &info); |
| 89 | if (ret) |
| 90 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 91 | |
| 92 | return gpiod_chip_info_from_uapicxx . . $ gpiod_chip_info_from_uapi(d34692967859f3f7).(&info); |
| 93 | } |
| 94 | |
| 95 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! const char *gpiod_chip_get_pathcxx . . $ gpiod_chip_get_path(52334aad5b40fb2e).(struct gpiod_chipcxx . . $ gpiod_chip# *chip) |
| 96 | { |
| 97 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 98 | |
| 99 | return chip->pathcxx . . $ gpiod_chip#path.; |
| 100 | } |
| 101 | |
| 102 | static int chip_read_line_infocxx . . $ chip_read_line_info(1de99d13f3e306ab).(int fd, unsigned int offset, |
| 103 | struct gpio_v2_line_infocxx . . $ gpio_v2_line_info# *info, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! watch) |
| 104 | { |
| 105 | int ret, cmd; |
| 106 | |
| 107 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(info, 0, sizeof(*info)); |
| 108 | info->offsetcxx . . $ gpio_v2_line_info#offset. = offset; |
| 109 | |
| 110 | cmd = watch ? GPIO_V2_GET_LINEINFO_WATCH_IOCTLcxx . . $ `lib/uapi/gpio.h:512:9`! : |
| 111 | GPIO_V2_GET_LINEINFO_IOCTLcxx . . $ `lib/uapi/gpio.h:511:9`!; |
| 112 | |
| 113 | ret = gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(fd, cmd, info); |
| 114 | if (ret) |
| 115 | return -1; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static struct gpiod_line_infocxx . . $ gpiod_line_info# * |
| 121 | chip_get_line_infocxx . . $ chip_get_line_info(accb74af5470bff4).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, unsigned int offset, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! watch) |
| 122 | { |
| 123 | struct gpio_v2_line_infocxx . . $ gpio_v2_line_info# info; |
| 124 | int ret; |
| 125 | |
| 126 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 127 | |
| 128 | ret = chip_read_line_infocxx . . $ chip_read_line_info(1de99d13f3e306ab).(chip->fdcxx . . $ gpiod_chip#fd., offset, &info, watch); |
| 129 | if (ret) |
| 130 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 131 | |
| 132 | return gpiod_line_info_from_uapicxx . . $ gpiod_line_info_from_uapi(273b4a357bdfb836).(&info); |
| 133 | } |
| 134 | |
| 135 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_line_infocxx . . $ gpiod_line_info# * |
| 136 | gpiod_chip_get_line_infocxx . . $ gpiod_chip_get_line_info(bfaacc9ce1d34d81).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, unsigned int offset) |
| 137 | { |
| 138 | return chip_get_line_infocxx . . $ chip_get_line_info(accb74af5470bff4).(chip, offset, falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!); |
| 139 | } |
| 140 | |
| 141 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_line_infocxx . . $ gpiod_line_info# * |
| 142 | gpiod_chip_watch_line_infocxx . . $ gpiod_chip_watch_line_info(bfaacc9ce1d34d81).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, unsigned int offset) |
| 143 | { |
| 144 | return chip_get_line_infocxx . . $ chip_get_line_info(accb74af5470bff4).(chip, offset, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!); |
| 145 | } |
| 146 | |
| 147 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_chip_unwatch_line_infocxx . . $ gpiod_chip_unwatch_line_info(2cc57de46c57d5b4).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, |
| 148 | unsigned int offset) |
| 149 | { |
| 150 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 151 | |
| 152 | return gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(chip->fdcxx . . $ gpiod_chip#fd., GPIO_GET_LINEINFO_UNWATCH_IOCTLcxx . . $ `lib/uapi/gpio.h:506:9`!, &offset); |
| 153 | } |
| 154 | |
| 155 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_chip_get_fdcxx . . $ gpiod_chip_get_fd(6a6cbfb0cf67595d).(struct gpiod_chipcxx . . $ gpiod_chip# *chip) |
| 156 | { |
| 157 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 158 | |
| 159 | return chip->fdcxx . . $ gpiod_chip#fd.; |
| 160 | } |
| 161 | |
| 162 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_chip_wait_info_eventcxx . . $ gpiod_chip_wait_info_event(f09f6bd5b5e64da).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, |
| 163 | int64_tcxx . . $ int64_t# timeout_ns) |
| 164 | { |
| 165 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 166 | |
| 167 | return gpiod_poll_fdcxx . . $ gpiod_poll_fd(bba9d6e9fb34fad7).(chip->fdcxx . . $ gpiod_chip#fd., timeout_ns); |
| 168 | } |
| 169 | |
| 170 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_info_eventcxx . . $ gpiod_info_event# * |
| 171 | gpiod_chip_read_info_eventcxx . . $ gpiod_chip_read_info_event(6d52984944c5742e).(struct gpiod_chipcxx . . $ gpiod_chip# *chip) |
| 172 | { |
| 173 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 174 | |
| 175 | return gpiod_info_event_read_fdcxx . . $ gpiod_info_event_read_fd(8e0dbd7b7e89c9bb).(chip->fdcxx . . $ gpiod_chip#fd.); |
| 176 | } |
| 177 | |
| 178 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_chip_get_line_offset_from_namecxx . . $ gpiod_chip_get_line_offset_from_name(3db92956a15e17f8).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, |
| 179 | const char *name) |
| 180 | { |
| 181 | struct gpio_v2_line_infocxx . . $ gpio_v2_line_info# linfo; |
| 182 | struct gpiochip_infocxx . . $ gpiochip_info# chinfo; |
| 183 | unsigned int offset; |
| 184 | int ret; |
| 185 | |
| 186 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 187 | |
| 188 | if (!name) { |
| 189 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | ret = read_chip_infocxx . . $ read_chip_info(3c216e5ef10af722).(chip->fdcxx . . $ gpiod_chip#fd., &chinfo); |
| 194 | if (ret) |
| 195 | return -1; |
| 196 | |
| 197 | for (offset = 0; offset < chinfo.linescxx . . $ gpiochip_info#lines.; offset++) { |
| 198 | ret = chip_read_line_infocxx . . $ chip_read_line_info(1de99d13f3e306ab).(chip->fdcxx . . $ gpiod_chip#fd., offset, &linfo, falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!); |
| 199 | if (ret) |
| 200 | return -1; |
| 201 | |
| 202 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, linfo.namecxx . . $ gpio_v2_line_info#name.) == 0) |
| 203 | return offset; |
| 204 | } |
| 205 | |
| 206 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!; |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_line_requestcxx . . $ gpiod_line_request# * |
| 211 | gpiod_chip_request_linescxx . . $ gpiod_chip_request_lines(e9f9a59a346dae77).(struct gpiod_chipcxx . . $ gpiod_chip# *chip, |
| 212 | struct gpiod_request_configcxx . . $ gpiod_request_config# *req_cfg, |
| 213 | struct gpiod_line_configcxx . . $ gpiod_line_config# *line_cfg) |
| 214 | { |
| 215 | struct gpio_v2_line_requestcxx . . $ gpio_v2_line_request# uapi_req; |
| 216 | struct gpiod_line_requestcxx . . $ gpiod_line_request# *request; |
| 217 | struct gpiochip_infocxx . . $ gpiochip_info# info; |
| 218 | int ret; |
| 219 | |
| 220 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(chip); |
| 221 | |
| 222 | if (!line_cfg) { |
| 223 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 224 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 225 | } |
| 226 | |
| 227 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&uapi_req, 0, sizeof(uapi_req)); |
| 228 | |
| 229 | if (req_cfg) |
| 230 | gpiod_request_config_to_uapicxx . . $ gpiod_request_config_to_uapi(a141c21c0e74b916).(req_cfg, &uapi_req); |
| 231 | |
| 232 | ret = gpiod_line_config_to_uapicxx . . $ gpiod_line_config_to_uapi(180b8730a233f01).(line_cfg, &uapi_req); |
| 233 | if (ret) |
| 234 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 235 | |
| 236 | ret = read_chip_infocxx . . $ read_chip_info(3c216e5ef10af722).(chip->fdcxx . . $ gpiod_chip#fd., &info); |
| 237 | if (ret) |
| 238 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 239 | |
| 240 | ret = gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(chip->fdcxx . . $ gpiod_chip#fd., GPIO_V2_GET_LINE_IOCTLcxx . . $ `lib/uapi/gpio.h:513:9`!, &uapi_req); |
| 241 | if (ret) |
| 242 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 243 | |
| 244 | request = gpiod_line_request_from_uapicxx . . $ gpiod_line_request_from_uapi(aba1763f564611d6).(&uapi_req, info.namecxx . . $ gpiochip_info#name.); |
| 245 | if (!request) { |
| 246 | closecxx . . $ close(7864480464b09eea).(uapi_req.fdcxx . . $ gpio_v2_line_request#fd.); |
| 247 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 248 | } |
| 249 | |
| 250 | return request; |
| 251 | } |