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 chardev test helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2016 Bamvor Jian Zhang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <libmount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "../../../gpio/gpio-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define CONSUMER	"gpio-selftest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define	GC_NUM		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) enum direction {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static int get_debugfs(char **path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct libmnt_context *cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct libmnt_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct libmnt_iter *itr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct libmnt_fs *fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int found = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	cxt = mnt_new_context();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (!cxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		err(EXIT_FAILURE, "libmount context allocation failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	itr = mnt_new_iter(MNT_ITER_FORWARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (!itr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		err(EXIT_FAILURE, "failed to initialize libmount iterator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (mnt_context_get_mtab(cxt, &tb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		err(EXIT_FAILURE, "failed to read mtab");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	while (mnt_table_next_fs(tb, itr, &fs) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		const char *type = mnt_fs_get_fstype(fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		if (!strcmp(type, "debugfs")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		ret = asprintf(path, "%s/gpio", mnt_fs_get_target(fs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			err(EXIT_FAILURE, "failed to format string");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	mnt_free_iter(itr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	mnt_free_context(cxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int gpio_debugfs_get(const char *consumer, int *dir, int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	char *debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	char *line = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	char *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (get_debugfs(&debugfs) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		err(EXIT_FAILURE, "debugfs is not mounted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	f = fopen(debugfs, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		err(EXIT_FAILURE, "read from gpio debugfs failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * gpio-2   (                    |gpio-selftest               ) in  lo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	while (getline(&line, &len, f) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		cur = strstr(line, consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (cur == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		cur = strchr(line, ')');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		if (!cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		cur += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (!strncmp(cur, "out", 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			*dir = OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			cur += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		} else if (!strncmp(cur, "in", 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			*dir = IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			cur += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (!strncmp(cur, "hi", 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			*value = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		else if (!strncmp(cur, "lo", 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	free(debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	free(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct gpiochip_info *list_gpiochip(const char *gpiochip_name, int *ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct gpiochip_info *cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct gpiochip_info *current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	const struct dirent *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	DIR *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	char *chrdev_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	cinfo = calloc(sizeof(struct gpiochip_info) * 4, GC_NUM + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!cinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		err(EXIT_FAILURE, "gpiochip_info allocation failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	current = cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	dp = opendir("/dev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		*ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		*ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	while (ent = readdir(dp), ent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (check_prefix(ent->d_name, "gpiochip")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			*ret = asprintf(&chrdev_name, "/dev/%s", ent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			if (*ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			fd = open(chrdev_name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				*ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				fprintf(stderr, "Failed to open %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 					chrdev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				goto error_close_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			*ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			if (*ret == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				perror("Failed to issue CHIPINFO IOCTL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				goto error_close_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			if (strcmp(current->label, gpiochip_name) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			    || check_prefix(current->label, gpiochip_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				*ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				current++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if ((!*ret && i == 0) || *ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		free(cinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		cinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!*ret && i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		cinfo = realloc(cinfo, sizeof(struct gpiochip_info) * 4 * i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		*ret = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error_close_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	closedir(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (*ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		err(EXIT_FAILURE, "list gpiochip failed: %s", strerror(*ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int gpio_pin_test(struct gpiochip_info *cinfo, int line, int flag, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct gpiohandle_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	unsigned int lines[] = {line};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int debugfs_dir = IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int debugfs_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	data.values[0] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ret = gpiotools_request_linehandle(cinfo->name, lines, 1, flag, &data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 					   CONSUMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		goto fail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		fd = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	ret = gpio_debugfs_get(CONSUMER, &debugfs_dir, &debugfs_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		goto fail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (flag & GPIOHANDLE_REQUEST_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (debugfs_dir != IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			errno = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	} else if (flag & GPIOHANDLE_REQUEST_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if (flag & GPIOHANDLE_REQUEST_ACTIVE_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			debugfs_value = !debugfs_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (!(debugfs_dir == OUT && value == debugfs_value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			errno = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	gpiotools_release_linehandle(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) fail_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		err(EXIT_FAILURE, "gpio<%s> line<%d> test flag<0x%x> value<%d>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		    cinfo->name, line, flag, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) void gpio_pin_tests(struct gpiochip_info *cinfo, unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	printf("line<%d>", line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_OUTPUT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	printf(".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_OUTPUT, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	printf(".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	gpio_pin_test(cinfo, line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		      GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_ACTIVE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		      0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	printf(".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	gpio_pin_test(cinfo, line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		      GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_ACTIVE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		      1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	printf(".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_INPUT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	printf(".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  * ./gpio-mockup-chardev gpio_chip_name_prefix is_valid_gpio_chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * Return 0 if successful or exit with EXIT_FAILURE if test failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * gpio_chip_name_prefix: The prefix of gpiochip you want to test. E.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *			  gpio-mockup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * is_valid_gpio_chip:	  Whether the gpio_chip is valid. 1 means valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  *			  0 means invalid which could not be found by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  *			  list_gpiochip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	char *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	int valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct gpiochip_info *cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct gpiochip_info *current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (argc < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		printf("Usage: %s prefix is_valid", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	prefix = argv[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	valid = strcmp(argv[2], "true") == 0 ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	printf("Test gpiochip %s: ", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	cinfo = list_gpiochip(prefix, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (!cinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		if (!valid && ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			printf("Invalid test successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	} else if (cinfo && !valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	current = cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	for (i = 0; i < ret; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		gpio_pin_tests(current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		gpio_pin_tests(current, current->lines - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		gpio_pin_tests(current, random() % current->lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		current++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	printf("successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		fprintf(stderr, "gpio<%s> test failed\n", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (cinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		free(cinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }