No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / gpioctl / gpioctl.c
blob22e89f56404253e97c872ece10368f761380e3c7
1 /* $NetBSD: gpioctl.c,v 1.7 2009/09/25 19:37:03 mbalmer Exp $ */
3 /*
4 * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org>
5 * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Program to control GPIO devices.
24 #include <sys/types.h>
25 #include <sys/gpio.h>
26 #include <sys/ioctl.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <paths.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
39 char *dev;
40 int devfd = -1;
41 int quiet = 0;
43 void getinfo(void);
44 void gpioread(int, char *);
45 void gpiowrite(int, char *, int);
46 void gpioset(int pin, char *name, int flags, char *alias);
47 void gpiounset(int pin, char *name);
48 void devattach(char *, int, u_int32_t);
49 void devdetach(char *);
51 __dead void usage(void);
53 extern long long strtonum(const char *numstr, long long minval,
54 long long maxval, const char **errstrp);
56 const struct bitstr {
57 unsigned int mask;
58 const char *string;
59 } pinflags[] = {
60 { GPIO_PIN_INPUT, "in" },
61 { GPIO_PIN_OUTPUT, "out" },
62 { GPIO_PIN_INOUT, "inout" },
63 { GPIO_PIN_OPENDRAIN, "od" },
64 { GPIO_PIN_PUSHPULL, "pp" },
65 { GPIO_PIN_TRISTATE, "tri" },
66 { GPIO_PIN_PULLUP, "pu" },
67 { GPIO_PIN_PULLDOWN, "pd" },
68 { GPIO_PIN_INVIN, "iin" },
69 { GPIO_PIN_INVOUT, "iout" },
70 { GPIO_PIN_PULSATE, "pulsate" },
71 { 0, NULL },
74 int
75 main(int argc, char *argv[])
77 const struct bitstr *bs;
78 int pin, ch, n, fl = 0, value = 0;
79 const char *errstr;
80 char *ep;
81 int ga_offset = -1;
82 u_int32_t ga_mask = 0;
83 long lval;
84 char *nam = NULL;
85 char devn[32];
87 while ((ch = getopt(argc, argv, "q")) != -1)
88 switch (ch) {
89 case 'q':
90 quiet = 1;
91 break;
92 default:
93 usage();
94 /* NOTREACHED */
96 argc -= optind;
97 argv += optind;
99 if (argc < 1)
100 usage();
101 dev = argv[0];
103 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
104 (void)snprintf(devn, sizeof(devn), "%s%s", _PATH_DEV, dev);
105 dev = devn;
108 if ((devfd = open(dev, O_RDWR)) == -1)
109 err(EXIT_FAILURE, "%s", dev);
111 if (argc == 1) {
112 getinfo();
113 return EXIT_SUCCESS;
116 if (!strcmp(argv[1], "attach")) {
117 char *driver, *offset, *mask;
119 if (argc != 5)
120 usage();
122 driver = argv[2];
123 offset = argv[3];
124 mask = argv[4];
126 ga_offset = strtonum(offset, 0, INT_MAX, &errstr);
127 if (errstr)
128 errx(EXIT_FAILURE, "offset is %s: %s", errstr, offset);
129 lval = strtol(mask, &ep, 0);
130 if (*mask == '\0' || *ep != '\0')
131 errx(EXIT_FAILURE, "invalid mask (not a number)");
132 if ((errno == ERANGE && (lval == LONG_MAX
133 || lval == LONG_MIN)) || (unsigned long)lval > UINT_MAX)
134 errx(EXIT_FAILURE, "mask out of range");
135 ga_mask = lval;
136 devattach(driver, ga_offset, ga_mask);
137 return EXIT_SUCCESS;
138 } else if (!strcmp(argv[1], "detach")) {
139 if (argc != 3)
140 usage();
141 devdetach(argv[2]);
142 } else {
143 char *nm = NULL;
145 /* expecting a pin number or name */
146 pin = strtonum(argv[1], 0, INT_MAX, &errstr);
147 if (errstr)
148 nm = argv[1]; /* try named pin */
149 if (argc > 2) {
150 if (!strcmp(argv[2], "set")) {
151 for (n = 3; n < argc; n++) {
152 for (bs = pinflags; bs->string != NULL;
153 bs++) {
154 if (!strcmp(argv[n],
155 bs->string)) {
156 fl |= bs->mask;
157 break;
160 if (bs->string == NULL)
161 nam = argv[n];
163 gpioset(pin, nm, fl, nam);
164 } else if (!strcmp(argv[2], "unset")) {
165 gpiounset(pin, nm);
166 } else {
167 value = strtonum(argv[2], INT_MIN, INT_MAX,
168 &errstr);
169 if (errstr) {
170 if (!strcmp(argv[2], "on"))
171 value = 1;
172 else if (!strcmp(argv[2], "off"))
173 value = 0;
174 else if (!strcmp(argv[2], "toggle"))
175 value = 2;
176 else
177 errx(EXIT_FAILURE,
178 "%s: invalid value",
179 argv[2]);
181 gpiowrite(pin, nm, value);
183 } else
184 gpioread(pin, nm);
187 return EXIT_SUCCESS;
190 void
191 getinfo(void)
193 struct gpio_info info;
195 if (ioctl(devfd, GPIOINFO, &info) == -1)
196 err(EXIT_FAILURE, "GPIOINFO");
198 if (quiet)
199 return;
201 printf("%s: %d pins\n", dev, info.gpio_npins);
204 void
205 gpioread(int pin, char *gp_name)
207 struct gpio_req req;
209 memset(&req, 0, sizeof(req));
210 if (gp_name != NULL)
211 strlcpy(req.gp_name, gp_name, sizeof(req.gp_name));
212 else
213 req.gp_pin = pin;
215 if (ioctl(devfd, GPIOREAD, &req) == -1)
216 err(EXIT_FAILURE, "GPIOREAD");
218 if (quiet)
219 return;
221 if (gp_name)
222 printf("pin %s: state %d\n", gp_name, req.gp_value);
223 else
224 printf("pin %d: state %d\n", pin, req.gp_value);
227 void
228 gpiowrite(int pin, char *gp_name, int value)
230 struct gpio_req req;
232 if (value < 0 || value > 2)
233 errx(EXIT_FAILURE, "%d: invalid value", value);
235 memset(&req, 0, sizeof(req));
236 if (gp_name != NULL)
237 strlcpy(req.gp_name, gp_name, sizeof(req.gp_name));
238 else
239 req.gp_pin = pin;
240 req.gp_value = (value == 0 ? GPIO_PIN_LOW : GPIO_PIN_HIGH);
241 if (value < 2) {
242 if (ioctl(devfd, GPIOWRITE, &req) == -1)
243 err(EXIT_FAILURE, "GPIOWRITE");
244 } else {
245 if (ioctl(devfd, GPIOTOGGLE, &req) == -1)
246 err(EXIT_FAILURE, "GPIOTOGGLE");
249 if (quiet)
250 return;
252 if (gp_name)
253 printf("pin %s: state %d -> %d\n", gp_name, req.gp_value,
254 (value < 2 ? value : 1 - req.gp_value));
255 else
256 printf("pin %d: state %d -> %d\n", pin, req.gp_value,
257 (value < 2 ? value : 1 - req.gp_value));
260 void
261 gpioset(int pin, char *name, int fl, char *alias)
263 struct gpio_set set;
264 const struct bitstr *bs;
266 memset(&set, 0, sizeof(set));
267 if (name != NULL)
268 strlcpy(set.gp_name, name, sizeof(set.gp_name));
269 else
270 set.gp_pin = pin;
271 set.gp_flags = fl;
273 if (alias != NULL)
274 strlcpy(set.gp_name2, alias, sizeof(set.gp_name2));
276 if (ioctl(devfd, GPIOSET, &set) == -1)
277 err(EXIT_FAILURE, "GPIOSET");
279 if (quiet)
280 return;
282 if (name != NULL)
283 printf("pin %s: caps:", name);
284 else
285 printf("pin %d: caps:", pin);
286 for (bs = pinflags; bs->string != NULL; bs++)
287 if (set.gp_caps & bs->mask)
288 printf(" %s", bs->string);
289 printf(", flags:");
290 for (bs = pinflags; bs->string != NULL; bs++)
291 if (set.gp_flags & bs->mask)
292 printf(" %s", bs->string);
293 if (fl > 0) {
294 printf(" ->");
295 for (bs = pinflags; bs->string != NULL; bs++)
296 if (fl & bs->mask)
297 printf(" %s", bs->string);
299 printf("\n");
302 void
303 gpiounset(int pin, char *name)
305 struct gpio_set set;
307 memset(&set, 0, sizeof(set));
308 if (name != NULL)
309 strlcpy(set.gp_name, name, sizeof(set.gp_name));
310 else
311 set.gp_pin = pin;
313 if (ioctl(devfd, GPIOUNSET, &set) == -1)
314 err(EXIT_FAILURE, "GPIOUNSET");
317 void
318 devattach(char *dvname, int offset, u_int32_t mask)
320 struct gpio_attach attach;
322 memset(&attach, 0, sizeof(attach));
323 strlcpy(attach.ga_dvname, dvname, sizeof(attach.ga_dvname));
324 attach.ga_offset = offset;
325 attach.ga_mask = mask;
326 if (ioctl(devfd, GPIOATTACH, &attach) == -1)
327 err(EXIT_FAILURE, "GPIOATTACH");
330 void
331 devdetach(char *dvname)
333 struct gpio_attach attach;
335 memset(&attach, 0, sizeof(attach));
336 strlcpy(attach.ga_dvname, dvname, sizeof(attach.ga_dvname));
337 if (ioctl(devfd, GPIODETACH, &attach) == -1)
338 err(EXIT_FAILURE, "GPIODETACH");
341 void
342 usage(void)
344 extern char *__progname;
346 fprintf(stderr, "usage: %s [-q] device [pin] [0 | 1 | 2 | "
347 "on | off | toggle]\n", __progname);
348 fprintf(stderr, " %s [-q] device pin set [flags] [name]\n",
349 __progname);
350 fprintf(stderr, " %s [-q] device pin unset\n", __progname);
351 fprintf(stderr, " %s [-q] device attach device offset mask\n",
352 __progname);
353 fprintf(stderr, " %s [-q] device detach device\n", __progname);
355 exit(EXIT_FAILURE);