| 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA |
| 15 | * |
| 16 | * Feedback, Bugs... blogic@openwrt.org |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <linux/gpio_dev.h> |
| 26 | #include <linux/ioctl.h> |
| 27 | |
| 28 | void |
| 29 | print_usage() |
| 30 | { |
| 31 | printf("gpioctl dirin|dirout|get|set|clear gpio\n"); |
| 32 | exit(0); |
| 33 | } |
| 34 | |
| 35 | int |
| 36 | main(int argc, char **argv) |
| 37 | { |
| 38 | int gpio_pin; |
| 39 | int fd; |
| 40 | int result = 0; |
| 41 | |
| 42 | if (argc != 3) |
| 43 | { |
| 44 | print_usage(); |
| 45 | } |
| 46 | |
| 47 | if ((fd = open("/dev/gpio", O_RDWR)) < 0) |
| 48 | { |
| 49 | printf("Error whilst opening /dev/gpio\n"); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | gpio_pin = atoi(argv[2]); |
| 54 | |
| 55 | printf("using gpio pin %d\n", gpio_pin); |
| 56 | |
| 57 | if (!strcmp(argv[1], "dirin")) |
| 58 | { |
| 59 | ioctl(fd, GPIO_DIR_IN, gpio_pin); |
| 60 | } else if (!strcmp(argv[1], "dirout")) |
| 61 | { |
| 62 | ioctl(fd, GPIO_DIR_OUT, gpio_pin); |
| 63 | } else if (!strcmp(argv[1], "get")) |
| 64 | { |
| 65 | result = ioctl(fd, GPIO_GET, gpio_pin); |
| 66 | printf("Pin %d is %s\n", gpio_pin, (result ? "HIGH" : "LOW")); |
| 67 | } else if (!strcmp(argv[1], "set")) |
| 68 | { |
| 69 | ioctl(fd, GPIO_SET, gpio_pin); |
| 70 | } else if (!strcmp(argv[1], "clear")) |
| 71 | { |
| 72 | ioctl(fd, GPIO_CLEAR, gpio_pin); |
| 73 | } else print_usage(); |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |