tools/gpio: re-work gpio hammer with gpio operations

Signed-off-by: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
This commit is contained in:
Bamvor Jian Zhang 2016-10-14 10:48:26 +08:00 committed by Linus Walleij
parent e1acec0e75
commit 74100bb967

View File

@ -23,54 +23,31 @@
#include <getopt.h> #include <getopt.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/gpio.h> #include <linux/gpio.h>
#include "gpio-utils.h"
int hammer_device(const char *device_name, unsigned int *lines, int nlines, int hammer_device(const char *device_name, unsigned int *lines, int nlines,
unsigned int loops) unsigned int loops)
{ {
struct gpiohandle_request req;
struct gpiohandle_data data; struct gpiohandle_data data;
char *chrdev_name;
char swirr[] = "-\\|/"; char swirr[] = "-\\|/";
int fd; int fd;
int ret; int ret;
int i, j; int i, j;
unsigned int iteration = 0; unsigned int iteration = 0;
ret = asprintf(&chrdev_name, "/dev/%s", device_name); memset(&data.values, 0, sizeof(data.values));
ret = gpiotools_request_linehandle(device_name, lines, nlines,
GPIOHANDLE_REQUEST_OUTPUT, &data,
"gpio-hammler");
if (ret < 0) if (ret < 0)
return -ENOMEM; goto exit_error;
else
fd = ret;
fd = open(chrdev_name, 0); ret = gpiotools_get_values(fd, &data);
if (fd == -1) { if (ret < 0)
ret = -errno;
fprintf(stderr, "Failed to open %s\n", chrdev_name);
goto exit_close_error; goto exit_close_error;
}
/* Request lines as output */
for (i = 0; i < nlines; i++)
req.lineoffsets[i] = lines[i];
req.flags = GPIOHANDLE_REQUEST_OUTPUT; /* Request as output */
strcpy(req.consumer_label, "gpio-hammer");
req.lines = nlines;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue GET LINEHANDLE "
"IOCTL (%d)\n",
ret);
goto exit_close_error;
}
/* Read initial states */
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
"VALUES IOCTL (%d)\n",
ret);
goto exit_close_error;
}
fprintf(stdout, "Hammer lines ["); fprintf(stdout, "Hammer lines [");
for (i = 0; i < nlines; i++) { for (i = 0; i < nlines; i++) {
fprintf(stdout, "%d", lines[i]); fprintf(stdout, "%d", lines[i]);
@ -92,23 +69,14 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,
for (i = 0; i < nlines; i++) for (i = 0; i < nlines; i++)
data.values[i] = !data.values[i]; data.values[i] = !data.values[i];
ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); ret = gpiotools_set_values(fd, &data);
if (ret == -1) { if (ret < 0)
ret = -errno;
fprintf(stderr, "Failed to issue GPIOHANDLE SET LINE "
"VALUES IOCTL (%d)\n",
ret);
goto exit_close_error; goto exit_close_error;
}
/* Re-read values to get status */ /* Re-read values to get status */
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); ret = gpiotools_get_values(fd, &data);
if (ret == -1) { if (ret < 0)
ret = -errno;
fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
"VALUES IOCTL (%d)\n",
ret);
goto exit_close_error; goto exit_close_error;
}
fprintf(stdout, "[%c] ", swirr[j]); fprintf(stdout, "[%c] ", swirr[j]);
j++; j++;
@ -132,9 +100,8 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,
ret = 0; ret = 0;
exit_close_error: exit_close_error:
if (close(fd) == -1) gpiotools_release_linehandle(fd);
perror("Failed to close GPIO character device file"); exit_error:
free(chrdev_name);
return ret; return ret;
} }