2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2014, Rui Paulo <rpaulo@FreeBSD.org>
4 * Copyright (c) 2015, Emmanuel Vadot <manu@bidouilliste.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
44 #define PIN_TYPE_NUMBER 1
45 #define PIN_TYPE_NAME 2
52 static struct flag_desc gpio_flags[] = {
53 { "IN", GPIO_PIN_INPUT },
54 { "OUT", GPIO_PIN_OUTPUT },
55 { "OD", GPIO_PIN_OPENDRAIN },
56 { "PP", GPIO_PIN_PUSHPULL },
57 { "TS", GPIO_PIN_TRISTATE },
58 { "PU", GPIO_PIN_PULLUP },
59 { "PD", GPIO_PIN_PULLDOWN },
60 { "II", GPIO_PIN_INVIN },
61 { "IO", GPIO_PIN_INVOUT },
62 { "PULSE", GPIO_PIN_PULSATE },
66 int str2cap(const char *str);
71 fprintf(stderr, "Usage:\n");
72 fprintf(stderr, "\tgpioctl [-f ctldev] -l [-v]\n");
73 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -t pin\n");
74 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -c pin flag ...\n");
75 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -n pin pin-name\n");
76 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] pin [0|1]\n");
83 struct flag_desc * pdesc = gpio_flags;
85 if (pdesc->flag == cap)
94 str2cap(const char *str)
96 struct flag_desc * pdesc = gpio_flags;
98 if (strcasecmp(str, pdesc->name) == 0)
107 * Our handmade function for converting string to number
110 str2int(const char *s, int *ok)
113 int res = strtod(s, &endptr);
114 if (endptr != s + strlen(s) )
129 for (i = 0; i < 32; i++) {
130 if (caps & (1 << i)) {
133 printf("%s", cap2str(1 << i));
141 dump_pins(gpio_handle_t handle, int verbose)
147 maxpin = gpio_pin_list(handle, &cfgs);
149 perror("gpio_pin_list");
153 for (i = 0; i <= maxpin; i++) {
155 pinv = gpio_pin_get(handle, pin->g_pin);
156 printf("pin %02d:\t%d\t%s", pin->g_pin, pinv,
159 print_caps(pin->g_flags);
163 print_caps(pin->g_caps);
171 get_pinnum_by_name(gpio_handle_t handle, const char *name) {
177 maxpin = gpio_pin_list(handle, &cfgs);
179 perror("gpio_pin_list");
183 for (i = 0; i <= maxpin; i++) {
185 gpio_pin_get(handle, pin->g_pin);
186 if (!strcmp(name, pin->g_name)) {
197 fail(const char *fmt, ...)
202 vfprintf(stderr, fmt, ap);
208 main(int argc, char **argv)
212 gpio_handle_t handle;
213 char *ctlfile = NULL;
214 int pinn, pinv, pin_type, ch;
216 int config, list, name, toggle, verbose;
218 config = toggle = verbose = list = name = pin_type = 0;
220 while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
235 pin_type = PIN_TYPE_NAME;
238 pin_type = PIN_TYPE_NUMBER;
254 handle = gpio_open(0);
256 handle = gpio_open_device(ctlfile);
257 if (handle == GPIO_INVALID_HANDLE) {
263 dump_pins(handle, verbose);
271 /* Find the pin number by the name */
274 /* First test if it is a pin number */
275 pinn = str2int(argv[0], &ok);
277 /* Test if we have any pin named by this number and tell the user */
278 if (get_pinnum_by_name(handle, argv[0]) != -1)
279 fail("%s is also a pin name, use -p or -N\n", argv[0]);
281 /* Test if it is a name */
282 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
283 fail("Can't find pin named \"%s\"\n", argv[0]);
286 case PIN_TYPE_NUMBER:
287 pinn = str2int(argv[0], &ok);
289 fail("Invalid pin number: %s\n", argv[0]);
292 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
293 fail("Can't find pin named \"%s\"\n", argv[0]);
297 /* Set the pin name. */
301 if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
302 perror("gpio_pin_set_name");
310 * -t pin assumes no additional arguments
314 if (gpio_pin_toggle(handle, pinn) < 0) {
315 perror("gpio_pin_toggle");
324 for (i = 1; i < argc; i++) {
325 flag = str2cap(argv[i]);
327 fail("Invalid flag: %s\n", argv[i]);
332 if (gpio_pin_set_flags(handle, &pin) < 0) {
333 perror("gpio_pin_set_flags");
340 * Last two cases - set value or print value
342 if ((argc == 0) || (argc > 2))
349 pinv = gpio_pin_get(handle, pinn);
351 perror("gpio_pin_get");
354 printf("%d\n", pinv);
358 /* Is it valid number (0 or 1) ? */
359 pinv = str2int(argv[1], &ok);
360 if (ok == 0 || ((pinv != 0) && (pinv != 1)))
361 fail("Invalid pin value: %s\n", argv[1]);
366 if (gpio_pin_set(handle, pinn, pinv) < 0) {
367 perror("gpio_pin_set");