Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * gpio-watch - monitor unrequested lines for property changes using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *              character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2019 BayLibre SAS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct gpio_v2_line_info_changed chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct gpio_v2_line_info req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct pollfd pfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int fd, i, j, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	char *event, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ssize_t rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (argc < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		goto err_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	fd = open(argv[1], O_RDWR | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		perror("unable to open gpiochip");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	for (i = 0, j = 2; i < argc - 2; i++, j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		memset(&req, 0, sizeof(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		req.offset = strtoul(argv[j], &end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		if (*end != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			goto err_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			perror("unable to set up line watch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	pfd.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	pfd.events = POLLIN | POLLPRI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		ret = poll(&pfd, 1, 5000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			perror("error polling the linechanged fd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			memset(&chg, 0, sizeof(chg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			rd = read(pfd.fd, &chg, sizeof(chg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			if (rd < 0 || rd != sizeof(chg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 				if (rd != sizeof(chg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 					errno = EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				perror("error reading line change event");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			switch (chg.event_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			case GPIO_V2_LINE_CHANGED_REQUESTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				event = "requested";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			case GPIO_V2_LINE_CHANGED_RELEASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				event = "released";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			case GPIO_V2_LINE_CHANGED_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				event = "config changed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 					"invalid event type received from the kernel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			printf("line %u: %s at %" PRIu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			       chg.info.offset, event, (uint64_t)chg.timestamp_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) err_usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }