| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <errno.h> |
| 5 | #include <poll.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/ioctl.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/sysmacros.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "internal.h" |
| 17 | |
| 18 | bool gpiod_check_gpiochip_device(const char *path, bool set_errno) |
| 19 | { |
| 20 | char *realname, *sysfsp, devpath[64]; |
| 21 | struct stat statbuf; |
| 22 | bool ret = false; |
| 23 | int rv; |
| 24 | |
| 25 | if (!path) { |
| 26 | errno = EINVAL; |
| 27 | goto out; |
| 28 | } |
| 29 | |
| 30 | rv = lstat(path, &statbuf); |
| 31 | if (rv) |
| 32 | goto out; |
| 33 | |
| 34 | /* |
| 35 | * Is it a symbolic link? We have to resolve it before checking |
| 36 | * the rest. |
| 37 | */ |
| 38 | realname = S_ISLNK(statbuf.st_mode) ? realpath(path, NULL) : |
| 39 | strdup(path); |
| 40 | if (realname == NULL) |
| 41 | goto out; |
| 42 | |
| 43 | rv = stat(realname, &statbuf); |
| 44 | if (rv) |
| 45 | goto out_free_realname; |
| 46 | |
| 47 | /* Is it a character device? */ |
| 48 | if (!S_ISCHR(statbuf.st_mode)) { |
| 49 | errno = ENOTTY; |
| 50 | goto out_free_realname; |
| 51 | } |
| 52 | |
| 53 | /* Is the device associated with the GPIO subsystem? */ |
| 54 | snprintf(devpath, sizeof(devpath), "/sys/dev/char/%u:%u/subsystem", |
| 55 | major(statbuf.st_rdev), minor(statbuf.st_rdev)); |
| 56 | |
| 57 | sysfsp = realpath(devpath, NULL); |
| 58 | if (!sysfsp) |
| 59 | goto out_free_realname; |
| 60 | |
| 61 | /* |
| 62 | * In glibc, if any of the underlying readlink() calls fail (which is |
| 63 | * perfectly normal when resolving paths), errno is not cleared. |
| 64 | */ |
| 65 | errno = 0; |
| 66 | |
| 67 | if (strcmp(sysfsp, "/sys/bus/gpio") != 0) { |
| 68 | /* This is a character device but not the one we're after. */ |
| 69 | errno = ENODEV; |
| 70 | goto out_free_sysfsp; |
| 71 | } |
| 72 | |
| 73 | ret = true; |
| 74 | |
| 75 | out_free_sysfsp: |
| 76 | free(sysfsp); |
| 77 | out_free_realname: |
| 78 | free(realname); |
| 79 | out: |
| 80 | if (!set_errno) |
| 81 | errno = 0; |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | int gpiod_poll_fd(int fd, int64_t timeout_ns) |
| 86 | { |
| 87 | struct timespec ts; |
| 88 | struct pollfd pfd; |
| 89 | int ret; |
| 90 | |
| 91 | memset(&pfd, 0, sizeof(pfd)); |
| 92 | pfd.fd = fd; |
| 93 | pfd.events = POLLIN | POLLPRI; |
| 94 | |
| 95 | if (timeout_ns >= 0) { |
| 96 | ts.tv_sec = timeout_ns / 1000000000ULL; |
| 97 | ts.tv_nsec = timeout_ns % 1000000000ULL; |
| 98 | } |
| 99 | |
| 100 | ret = ppoll(&pfd, 1, timeout_ns < 0 ? NULL : &ts, NULL); |
| 101 | if (ret < 0) |
| 102 | return -1; |
| 103 | else if (ret == 0) |
| 104 | return 0; |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | int gpiod_set_output_value(enum gpiod_line_value in, enum gpiod_line_value *out) |
| 110 | { |
| 111 | switch (in) { |
| 112 | case GPIOD_LINE_VALUE_INACTIVE: |
| 113 | case GPIOD_LINE_VALUE_ACTIVE: |
| 114 | *out = in; |
| 115 | break; |
| 116 | default: |
| 117 | *out = GPIOD_LINE_VALUE_INACTIVE; |
| 118 | errno = EINVAL; |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int gpiod_ioctl(int fd, unsigned long request, void *arg) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | ret = ioctl(fd, request, arg); |
| 130 | if (ret <= 0) |
| 131 | return ret; |
| 132 | |
| 133 | errno = EBADE; |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | void gpiod_line_mask_zero(uint64_t *mask) |
| 138 | { |
| 139 | *mask = 0ULL; |
| 140 | } |
| 141 | |
| 142 | bool gpiod_line_mask_test_bit(const uint64_t *mask, unsigned int nr) |
| 143 | { |
| 144 | return *mask & (1ULL << nr); |
| 145 | } |
| 146 | |
| 147 | void gpiod_line_mask_set_bit(uint64_t *mask, unsigned int nr) |
| 148 | { |
| 149 | *mask |= (1ULL << nr); |
| 150 | } |
| 151 | |
| 152 | void gpiod_line_mask_assign_bit(uint64_t *mask, unsigned int nr, bool value) |
| 153 | { |
| 154 | if (value) |
| 155 | gpiod_line_mask_set_bit(mask, nr); |
| 156 | else |
| 157 | *mask &= ~(1ULL << nr); |
| 158 | } |