2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Copyright (c) 2014, Rui Paulo <rpaulo@FreeBSD.org>
6 * Copyright (c) 2015, Emmanuel Vadot <manu@bidouilliste.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
46 #define PIN_TYPE_NUMBER 1
47 #define PIN_TYPE_NAME 2
54 static struct flag_desc gpio_flags[] = {
55 { "IN", GPIO_PIN_INPUT },
56 { "OUT", GPIO_PIN_OUTPUT },
57 { "OD", GPIO_PIN_OPENDRAIN },
58 { "PP", GPIO_PIN_PUSHPULL },
59 { "TS", GPIO_PIN_TRISTATE },
60 { "PU", GPIO_PIN_PULLUP },
61 { "PD", GPIO_PIN_PULLDOWN },
62 { "II", GPIO_PIN_INVIN },
63 { "IO", GPIO_PIN_INVOUT },
64 { "PULSE", GPIO_PIN_PULSATE },
65 { "INTRLL", GPIO_INTR_LEVEL_LOW},
66 { "INTRLH", GPIO_INTR_LEVEL_HIGH},
67 { "INTRER", GPIO_INTR_EDGE_RISING},
68 { "INTREF", GPIO_INTR_EDGE_FALLING},
69 { "INTREB", GPIO_INTR_EDGE_BOTH},
73 int str2cap(const char *str);
78 fprintf(stderr, "Usage:\n");
79 fprintf(stderr, "\tgpioctl [-f ctldev] -l [-v]\n");
80 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -t pin\n");
81 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -c pin flag ...\n");
82 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -n pin pin-name\n");
83 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] pin [0|1]\n");
90 struct flag_desc * pdesc = gpio_flags;
92 if (pdesc->flag == cap)
101 str2cap(const char *str)
103 struct flag_desc * pdesc = gpio_flags;
104 while (pdesc->name) {
105 if (strcasecmp(str, pdesc->name) == 0)
114 * Our handmade function for converting string to number
117 str2int(const char *s, int *ok)
120 int res = strtod(s, &endptr);
121 if (endptr != s + strlen(s) )
136 for (i = 0; i < 32; i++) {
137 if (caps & (1 << i)) {
140 printf("%s", cap2str(1 << i));
148 dump_pins(gpio_handle_t handle, int verbose)
154 maxpin = gpio_pin_list(handle, &cfgs);
156 perror("gpio_pin_list");
160 for (i = 0; i <= maxpin; i++) {
162 pinv = gpio_pin_get(handle, pin->g_pin);
163 printf("pin %02d:\t%d\t%s", pin->g_pin, pinv,
166 print_caps(pin->g_flags);
170 print_caps(pin->g_caps);
178 get_pinnum_by_name(gpio_handle_t handle, const char *name) {
184 maxpin = gpio_pin_list(handle, &cfgs);
186 perror("gpio_pin_list");
190 for (i = 0; i <= maxpin; i++) {
192 gpio_pin_get(handle, pin->g_pin);
193 if (!strcmp(name, pin->g_name)) {
204 fail(const char *fmt, ...)
209 vfprintf(stderr, fmt, ap);
215 main(int argc, char **argv)
219 gpio_handle_t handle;
220 char *ctlfile = NULL;
221 int pinn, pinv, pin_type, ch;
223 int config, list, name, toggle, verbose;
225 config = toggle = verbose = list = name = pin_type = 0;
227 while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
242 pin_type = PIN_TYPE_NAME;
245 pin_type = PIN_TYPE_NUMBER;
261 handle = gpio_open(0);
263 handle = gpio_open_device(ctlfile);
264 if (handle == GPIO_INVALID_HANDLE) {
270 dump_pins(handle, verbose);
278 /* Find the pin number by the name */
281 /* First test if it is a pin number */
282 pinn = str2int(argv[0], &ok);
284 /* Test if we have any pin named by this number and tell the user */
285 if (get_pinnum_by_name(handle, argv[0]) != -1)
286 fail("%s is also a pin name, use -p or -N\n", argv[0]);
288 /* Test if it is a name */
289 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
290 fail("Can't find pin named \"%s\"\n", argv[0]);
293 case PIN_TYPE_NUMBER:
294 pinn = str2int(argv[0], &ok);
296 fail("Invalid pin number: %s\n", argv[0]);
299 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
300 fail("Can't find pin named \"%s\"\n", argv[0]);
304 /* Set the pin name. */
308 if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
309 perror("gpio_pin_set_name");
317 * -t pin assumes no additional arguments
321 if (gpio_pin_toggle(handle, pinn) < 0) {
322 perror("gpio_pin_toggle");
331 for (i = 1; i < argc; i++) {
332 flag = str2cap(argv[i]);
334 fail("Invalid flag: %s\n", argv[i]);
339 if (gpio_pin_set_flags(handle, &pin) < 0) {
340 perror("gpio_pin_set_flags");
347 * Last two cases - set value or print value
349 if ((argc == 0) || (argc > 2))
356 pinv = gpio_pin_get(handle, pinn);
358 perror("gpio_pin_get");
361 printf("%d\n", pinv);
365 /* Is it valid number (0 or 1) ? */
366 pinv = str2int(argv[1], &ok);
367 if (ok == 0 || ((pinv != 0) && (pinv != 1)))
368 fail("Invalid pin value: %s\n", argv[1]);
373 if (gpio_pin_set(handle, pinn, pinv) < 0) {
374 perror("gpio_pin_set");