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 },
68 int str2cap(const char *str);
73 fprintf(stderr, "Usage:\n");
74 fprintf(stderr, "\tgpioctl [-f ctldev] -l [-v]\n");
75 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -t pin\n");
76 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -c pin flag ...\n");
77 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] -n pin pin-name\n");
78 fprintf(stderr, "\tgpioctl [-f ctldev] [-pN] pin [0|1]\n");
85 struct flag_desc * pdesc = gpio_flags;
87 if (pdesc->flag == cap)
96 str2cap(const char *str)
98 struct flag_desc * pdesc = gpio_flags;
100 if (strcasecmp(str, pdesc->name) == 0)
109 * Our handmade function for converting string to number
112 str2int(const char *s, int *ok)
115 int res = strtod(s, &endptr);
116 if (endptr != s + strlen(s) )
131 for (i = 0; i < 32; i++) {
132 if (caps & (1 << i)) {
135 printf("%s", cap2str(1 << i));
143 dump_pins(gpio_handle_t handle, int verbose)
149 maxpin = gpio_pin_list(handle, &cfgs);
151 perror("gpio_pin_list");
155 for (i = 0; i <= maxpin; i++) {
157 pinv = gpio_pin_get(handle, pin->g_pin);
158 printf("pin %02d:\t%d\t%s", pin->g_pin, pinv,
161 print_caps(pin->g_flags);
165 print_caps(pin->g_caps);
173 get_pinnum_by_name(gpio_handle_t handle, const char *name) {
179 maxpin = gpio_pin_list(handle, &cfgs);
181 perror("gpio_pin_list");
185 for (i = 0; i <= maxpin; i++) {
187 gpio_pin_get(handle, pin->g_pin);
188 if (!strcmp(name, pin->g_name)) {
199 fail(const char *fmt, ...)
204 vfprintf(stderr, fmt, ap);
210 main(int argc, char **argv)
214 gpio_handle_t handle;
215 char *ctlfile = NULL;
216 int pinn, pinv, pin_type, ch;
218 int config, list, name, toggle, verbose;
220 config = toggle = verbose = list = name = pin_type = 0;
222 while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
237 pin_type = PIN_TYPE_NAME;
240 pin_type = PIN_TYPE_NUMBER;
256 handle = gpio_open(0);
258 handle = gpio_open_device(ctlfile);
259 if (handle == GPIO_INVALID_HANDLE) {
265 dump_pins(handle, verbose);
273 /* Find the pin number by the name */
276 /* First test if it is a pin number */
277 pinn = str2int(argv[0], &ok);
279 /* Test if we have any pin named by this number and tell the user */
280 if (get_pinnum_by_name(handle, argv[0]) != -1)
281 fail("%s is also a pin name, use -p or -N\n", argv[0]);
283 /* Test if it is a name */
284 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
285 fail("Can't find pin named \"%s\"\n", argv[0]);
288 case PIN_TYPE_NUMBER:
289 pinn = str2int(argv[0], &ok);
291 fail("Invalid pin number: %s\n", argv[0]);
294 if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
295 fail("Can't find pin named \"%s\"\n", argv[0]);
299 /* Set the pin name. */
303 if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
304 perror("gpio_pin_set_name");
312 * -t pin assumes no additional arguments
316 if (gpio_pin_toggle(handle, pinn) < 0) {
317 perror("gpio_pin_toggle");
326 for (i = 1; i < argc; i++) {
327 flag = str2cap(argv[i]);
329 fail("Invalid flag: %s\n", argv[i]);
334 if (gpio_pin_set_flags(handle, &pin) < 0) {
335 perror("gpio_pin_set_flags");
342 * Last two cases - set value or print value
344 if ((argc == 0) || (argc > 2))
351 pinv = gpio_pin_get(handle, pinn);
353 perror("gpio_pin_get");
356 printf("%d\n", pinv);
360 /* Is it valid number (0 or 1) ? */
361 pinv = str2int(argv[1], &ok);
362 if (ok == 0 || ((pinv != 0) && (pinv != 1)))
363 fail("Invalid pin value: %s\n", argv[1]);
368 if (gpio_pin_set(handle, pinn, pinv) < 0) {
369 perror("gpio_pin_set");