]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/gpio/gpio-utils.c
KVM: SVM: Move spec control call after restore of GS
[mirror_ubuntu-artful-kernel.git] / tools / gpio / gpio-utils.c
1 /*
2 * GPIO tools - helpers library for the GPIO tools
3 *
4 * Copyright (C) 2015 Linus Walleij
5 * Copyright (C) 2016 Bamvor Jian Zhang
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 */
11
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <getopt.h>
19 #include <sys/ioctl.h>
20 #include <linux/gpio.h>
21 #include "gpio-utils.h"
22
23 #define COMSUMER "gpio-utils"
24
25 /**
26 * doc: Operation of gpio
27 *
28 * Provide the api of gpiochip for chardev interface. There are two
29 * types of api. The first one provide as same function as each
30 * ioctl, including request and release for lines of gpio, read/write
31 * the value of gpio. If the user want to do lots of read and write of
32 * lines of gpio, user should use this type of api.
33 *
34 * The second one provide the easy to use api for user. Each of the
35 * following api will request gpio lines, do the operation and then
36 * release these lines.
37 */
38 /**
39 * gpiotools_request_linehandle() - request gpio lines in a gpiochip
40 * @device_name: The name of gpiochip without prefix "/dev/",
41 * such as "gpiochip0"
42 * @lines: An array desired lines, specified by offset
43 * index for the associated GPIO device.
44 * @nline: The number of lines to request.
45 * @flag: The new flag for requsted gpio. Reference
46 * "linux/gpio.h" for the meaning of flag.
47 * @data: Default value will be set to gpio when flag is
48 * GPIOHANDLE_REQUEST_OUTPUT.
49 * @consumer_label: The name of consumer, such as "sysfs",
50 * "powerkey". This is useful for other users to
51 * know who is using.
52 *
53 * Request gpio lines through the ioctl provided by chardev. User
54 * could call gpiotools_set_values() and gpiotools_get_values() to
55 * read and write respectively through the returned fd. Call
56 * gpiotools_release_linehandle() to release these lines after that.
57 *
58 * Return: On success return the fd;
59 * On failure return the errno.
60 */
61 int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
62 unsigned int nlines, unsigned int flag,
63 struct gpiohandle_data *data,
64 const char *consumer_label)
65 {
66 struct gpiohandle_request req;
67 char *chrdev_name;
68 int fd;
69 int i;
70 int ret;
71
72 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
73 if (ret < 0)
74 return -ENOMEM;
75
76 fd = open(chrdev_name, 0);
77 if (fd == -1) {
78 ret = -errno;
79 fprintf(stderr, "Failed to open %s\n", chrdev_name);
80 goto exit_close_error;
81 }
82
83 for (i = 0; i < nlines; i++)
84 req.lineoffsets[i] = lines[i];
85
86 req.flags = flag;
87 strcpy(req.consumer_label, consumer_label);
88 req.lines = nlines;
89 if (flag & GPIOHANDLE_REQUEST_OUTPUT)
90 memcpy(req.default_values, data, sizeof(req.default_values));
91
92 ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
93 if (ret == -1) {
94 ret = -errno;
95 fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n",
96 ret);
97 }
98
99 exit_close_error:
100 if (close(fd) == -1)
101 perror("Failed to close GPIO character device file");
102 free(chrdev_name);
103 return ret < 0 ? ret : req.fd;
104 }
105 /**
106 * gpiotools_set_values(): Set the value of gpio(s)
107 * @fd: The fd returned by
108 * gpiotools_request_linehandle().
109 * @data: The array of values want to set.
110 *
111 * Return: On success return 0;
112 * On failure return the errno.
113 */
114 int gpiotools_set_values(const int fd, struct gpiohandle_data *data)
115 {
116 int ret;
117
118 ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data);
119 if (ret == -1) {
120 ret = -errno;
121 fprintf(stderr, "Failed to issue %s (%d)\n",
122 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret);
123 }
124
125 return ret;
126 }
127
128 /**
129 * gpiotools_get_values(): Get the value of gpio(s)
130 * @fd: The fd returned by
131 * gpiotools_request_linehandle().
132 * @data: The array of values get from hardware.
133 *
134 * Return: On success return 0;
135 * On failure return the errno.
136 */
137 int gpiotools_get_values(const int fd, struct gpiohandle_data *data)
138 {
139 int ret;
140
141 ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data);
142 if (ret == -1) {
143 ret = -errno;
144 fprintf(stderr, "Failed to issue %s (%d)\n",
145 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret);
146 }
147
148 return ret;
149 }
150
151 /**
152 * gpiotools_release_linehandle(): Release the line(s) of gpiochip
153 * @fd: The fd returned by
154 * gpiotools_request_linehandle().
155 *
156 * Return: On success return 0;
157 * On failure return the errno.
158 */
159 int gpiotools_release_linehandle(const int fd)
160 {
161 int ret;
162
163 ret = close(fd);
164 if (ret == -1) {
165 perror("Failed to close GPIO LINEHANDLE device file");
166 ret = -errno;
167 }
168
169 return ret;
170 }
171
172 /**
173 * gpiotools_get(): Get value from specific line
174 * @device_name: The name of gpiochip without prefix "/dev/",
175 * such as "gpiochip0"
176 * @line: number of line, such as 2.
177 *
178 * Return: On success return 0;
179 * On failure return the errno.
180 */
181 int gpiotools_get(const char *device_name, unsigned int line)
182 {
183 struct gpiohandle_data data;
184 unsigned int lines[] = {line};
185
186 gpiotools_gets(device_name, lines, 1, &data);
187 return data.values[0];
188 }
189
190
191 /**
192 * gpiotools_gets(): Get values from specific lines.
193 * @device_name: The name of gpiochip without prefix "/dev/",
194 * such as "gpiochip0".
195 * @lines: An array desired lines, specified by offset
196 * index for the associated GPIO device.
197 * @nline: The number of lines to request.
198 * @data: The array of values get from gpiochip.
199 *
200 * Return: On success return 0;
201 * On failure return the errno.
202 */
203 int gpiotools_gets(const char *device_name, unsigned int *lines,
204 unsigned int nlines, struct gpiohandle_data *data)
205 {
206 int fd;
207 int ret;
208 int ret_close;
209
210 ret = gpiotools_request_linehandle(device_name, lines, nlines,
211 GPIOHANDLE_REQUEST_INPUT, data,
212 COMSUMER);
213 if (ret < 0)
214 return ret;
215
216 fd = ret;
217 ret = gpiotools_get_values(fd, data);
218 ret_close = gpiotools_release_linehandle(fd);
219 return ret < 0 ? ret : ret_close;
220 }
221
222 /**
223 * gpiotools_set(): Set value to specific line
224 * @device_name: The name of gpiochip without prefix "/dev/",
225 * such as "gpiochip0"
226 * @line: number of line, such as 2.
227 * @value: The value of gpio, must be 0(low) or 1(high).
228 *
229 * Return: On success return 0;
230 * On failure return the errno.
231 */
232 int gpiotools_set(const char *device_name, unsigned int line,
233 unsigned int value)
234 {
235 struct gpiohandle_data data;
236 unsigned int lines[] = {line};
237
238 data.values[0] = value;
239 return gpiotools_sets(device_name, lines, 1, &data);
240 }
241
242 /**
243 * gpiotools_sets(): Set values to specific lines.
244 * @device_name: The name of gpiochip without prefix "/dev/",
245 * such as "gpiochip0".
246 * @lines: An array desired lines, specified by offset
247 * index for the associated GPIO device.
248 * @nline: The number of lines to request.
249 * @data: The array of values set to gpiochip, must be
250 * 0(low) or 1(high).
251 *
252 * Return: On success return 0;
253 * On failure return the errno.
254 */
255 int gpiotools_sets(const char *device_name, unsigned int *lines,
256 unsigned int nlines, struct gpiohandle_data *data)
257 {
258 int ret;
259
260 ret = gpiotools_request_linehandle(device_name, lines, nlines,
261 GPIOHANDLE_REQUEST_OUTPUT, data,
262 COMSUMER);
263 if (ret < 0)
264 return ret;
265
266 return gpiotools_release_linehandle(ret);
267 }