From patchwork Fri Oct 14 02:48:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhangjian \(Bamvor\)" X-Patchwork-Id: 77642 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp110825qge; Thu, 13 Oct 2016 19:45:36 -0700 (PDT) X-Received: by 10.98.103.143 with SMTP id t15mr14632535pfj.149.1476413136824; Thu, 13 Oct 2016 19:45:36 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rf4si13963910pab.9.2016.10.13.19.45.36; Thu, 13 Oct 2016 19:45:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753817AbcJNCpf (ORCPT + 4 others); Thu, 13 Oct 2016 22:45:35 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:18618 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753507AbcJNCpe (ORCPT ); Thu, 13 Oct 2016 22:45:34 -0400 Received: from 172.24.1.36 (EHLO szxeml434-hub.china.huawei.com) ([172.24.1.36]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DOS17064; Fri, 14 Oct 2016 10:45:11 +0800 (CST) Received: from linux696.huawei.com (10.110.52.23) by szxeml434-hub.china.huawei.com (10.82.67.225) with Microsoft SMTP Server id 14.3.235.1; Fri, 14 Oct 2016 10:45:06 +0800 From: Bamvor Jian Zhang To: CC: , , , Subject: [PATCH v4 1/3] tools/gpio: add gpio basic opereations Date: Fri, 14 Oct 2016 10:48:25 +0800 Message-ID: <1476413307-1397-2-git-send-email-bamvor.zhangjian@huawei.com> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1476413307-1397-1-git-send-email-bamvor.zhangjian@huawei.com> References: <1476413307-1397-1-git-send-email-bamvor.zhangjian@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.110.52.23] X-CFilter-Loop: Reflected Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bamvor Jian Zhang Add basic gpio operations. User could get/set gpio value for specific line of gpiochip. Reference "tools/gpio/gpio-hammer.c" or "tools/testing/selftest/gpio/gpio-mockup-chardev.c" for how to use it. Signed-off-by: Bamvor Jian Zhang Reviewed-by: Michael Welling --- tools/gpio/gpio-utils.c | 257 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/gpio/gpio-utils.h | 16 +++ 2 files changed, 273 insertions(+) -- 1.8.4.5 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c index 8208718..f6209cd 100644 --- a/tools/gpio/gpio-utils.c +++ b/tools/gpio/gpio-utils.c @@ -2,10 +2,267 @@ * GPIO tools - helpers library for the GPIO tools * * Copyright (C) 2015 Linus Walleij + * Copyright (C) 2016 Bamvor Jian Zhang * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "gpio-utils.h" + +#define COMSUMER "gpio-utils" + +/** + * doc: Operation of gpio + * + * Provide the api of gpiochip for chardev interface. There are two + * types of api. The first one provide as same function as each + * ioctl, including request and release for lines of gpio, read/write + * the value of gpio. If the user want to do lots of read and write of + * lines of gpio, user should use this type of api. + * + * The second one provide the easy to use api for user. Each of the + * following api will request gpio lines, do the operation and then + * release these lines. + */ +/** + * gpiotools_request_linehandle() - request gpio lines in a gpiochip + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0" + * @lines: An array desired lines, specified by offset + * index for the associated GPIO device. + * @nline: The number of lines to request. + * @flag: The new flag for requsted gpio. Reference + * "linux/gpio.h" for the meaning of flag. + * @data: Default value will be set to gpio when flag is + * GPIOHANDLE_REQUEST_OUTPUT. + * @consumer_label: The name of consumer, such as "sysfs", + * "powerkey". This is useful for other users to + * know who is using. + * + * Request gpio lines through the ioctl provided by chardev. User + * could call gpiotools_set_values() and gpiotools_get_values() to + * read and write respectively through the returned fd. Call + * gpiotools_release_linehandle() to release these lines after that. + * + * Return: On success return the fd; + * On failure return the errno. + */ +int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, + unsigned int nlines, unsigned int flag, + struct gpiohandle_data *data, + const char *consumer_label) +{ + struct gpiohandle_request req; + char *chrdev_name; + int fd; + int i; + int ret; + + ret = asprintf(&chrdev_name, "/dev/%s", device_name); + if (ret < 0) + return -ENOMEM; + + fd = open(chrdev_name, 0); + if (fd == -1) { + ret = -errno; + fprintf(stderr, "Failed to open %s\n", chrdev_name); + goto exit_close_error; + } + + for (i = 0; i < nlines; i++) + req.lineoffsets[i] = lines[i]; + + req.flags = flag; + strcpy(req.consumer_label, consumer_label); + req.lines = nlines; + if (flag & GPIOHANDLE_REQUEST_OUTPUT) + memcpy(req.default_values, data, sizeof(req.default_values)); + + ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req); + if (ret == -1) { + ret = -errno; + fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n", + ret); + } + +exit_close_error: + if (close(fd) == -1) + perror("Failed to close GPIO character device file"); + free(chrdev_name); + return ret < 0 ? ret : req.fd; +} +/** + * gpiotools_set_values(): Set the value of gpio(s) + * @fd: The fd returned by + * gpiotools_request_linehandle(). + * @data: The array of values want to set. + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_set_values(const int fd, struct gpiohandle_data *data) +{ + int ret; + + ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data); + if (ret == -1) { + ret = -errno; + fprintf(stderr, "Failed to issue %s (%d)\n", + "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret); + } + + return ret; +} + +/** + * gpiotools_get_values(): Get the value of gpio(s) + * @fd: The fd returned by + * gpiotools_request_linehandle(). + * @data: The array of values get from hardware. + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_get_values(const int fd, struct gpiohandle_data *data) +{ + int ret; + + ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data); + if (ret == -1) { + ret = -errno; + fprintf(stderr, "Failed to issue %s (%d)\n", + "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret); + } + + return ret; +} + +/** + * gpiotools_release_linehandle(): Release the line(s) of gpiochip + * @fd: The fd returned by + * gpiotools_request_linehandle(). + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_release_linehandle(const int fd) +{ + int ret; + + ret = close(fd); + if (ret == -1) { + perror("Failed to close GPIO LINEHANDLE device file"); + ret = -errno; + } + + return ret; +} + +/** + * gpiotools_get(): Get value from specific line + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0" + * @line: number of line, such as 2. + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_get(const char *device_name, unsigned int line) +{ + struct gpiohandle_data data; + unsigned int lines[] = {line}; + + gpiotools_gets(device_name, lines, 1, &data); + return data.values[0]; +} + + +/** + * gpiotools_gets(): Get values from specific lines. + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0". + * @lines: An array desired lines, specified by offset + * index for the associated GPIO device. + * @nline: The number of lines to request. + * @data: The array of values get from gpiochip. + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_gets(const char *device_name, unsigned int *lines, + unsigned int nlines, struct gpiohandle_data *data) +{ + int fd; + int ret; + int ret_close; + + ret = gpiotools_request_linehandle(device_name, lines, nlines, + GPIOHANDLE_REQUEST_INPUT, data, + COMSUMER); + if (ret < 0) + return ret; + + fd = ret; + ret = gpiotools_get_values(fd, data); + ret_close = gpiotools_release_linehandle(fd); + return ret < 0 ? ret : ret_close; +} + +/** + * gpiotools_set(): Set value to specific line + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0" + * @line: number of line, such as 2. + * @value: The value of gpio, must be 0(low) or 1(high). + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_set(const char *device_name, unsigned int line, + unsigned int value) +{ + struct gpiohandle_data data; + unsigned int lines[] = {line}; + + data.values[0] = value; + return gpiotools_sets(device_name, lines, 1, &data); +} + +/** + * gpiotools_sets(): Set values to specific lines. + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0". + * @lines: An array desired lines, specified by offset + * index for the associated GPIO device. + * @nline: The number of lines to request. + * @data: The array of values set to gpiochip, must be + * 0(low) or 1(high). + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_sets(const char *device_name, unsigned int *lines, + unsigned int nlines, struct gpiohandle_data *data) +{ + int ret; + + ret = gpiotools_request_linehandle(device_name, lines, nlines, + GPIOHANDLE_REQUEST_OUTPUT, data, + COMSUMER); + if (ret < 0) + return ret; + + return gpiotools_release_linehandle(ret); +} + diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h index 5f57133..344ea04 100644 --- a/tools/gpio/gpio-utils.h +++ b/tools/gpio/gpio-utils.h @@ -24,4 +24,20 @@ static inline int check_prefix(const char *str, const char *prefix) strncmp(str, prefix, strlen(prefix)) == 0; } +int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, + unsigned int nlines, unsigned int flag, + struct gpiohandle_data *data, + const char *consumer_label); +int gpiotools_set_values(const int fd, struct gpiohandle_data *data); +int gpiotools_get_values(const int fd, struct gpiohandle_data *data); +int gpiotools_release_linehandle(const int fd); + +int gpiotools_get(const char *device_name, unsigned int line); +int gpiotools_gets(const char *device_name, unsigned int *lines, + unsigned int nlines, struct gpiohandle_data *data); +int gpiotools_set(const char *device_name, unsigned int line, + unsigned int value); +int gpiotools_sets(const char *device_name, unsigned int *lines, + unsigned int nlines, struct gpiohandle_data *data); + #endif /* _GPIO_UTILS_H_ */