see how this gpio_core.h works for you. add netbsd-specific logic inside of gpio_core.c, and then build your parallel port driver on top of the api exposed in gpio_core.h. afaict, the api in that header should be more than enough to do everything you need ...
i'm not worried about the cable names (just yet) ... let's make sure this gpio core can do what you need first. -mike Index: src/tap/gpio_core.c =================================================================== --- src/tap/gpio_core.c (revision 0) +++ src/tap/gpio_core.c (revision 0) @@ -0,0 +1,158 @@ +/* + * (C) Copyright 2010 + * Stefano Babic, DENX Software Engineering, [email protected]. + * GPIO JTAG Cable Driver + * + * Based on TS7800 GPIO JTAG Cable Driver + * Copyright (C) 2008 Catalin Ionescu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include <sysdep.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include <urjtag/log.h> + +#include "gpio_core.h" + +#ifdef __linux__ + +#define GPIO_PATH "/sys/class/gpio/" +#define GPIO_EXPORT_PATH GPIO_PATH "export" +#define GPIO_UNEXPORT_PATH GPIO_PATH "unexport" + +struct gpio { + unsigned gpio; + int fd_val, fd_dir; +}; + +static int gpio_export (unsigned gpio, int export) +{ + char *fname; + FILE *fp; + + if (export) + fname = GPIO_EXPORT_PATH; + else + fname = GPIO_UNEXPORT_PATH; + + fp = fopen (fname, FOPEN_W); + if (!fp) + { + urj_warning (_("%s: cannot open to (un)export GPIO %u\n"), fname, gpio); + return URJ_STATUS_FAIL; + } + + fprintf (fp, "%u", gpio); + fclose (fp); + + return URJ_STATUS_OK; +} + +urj_gpio_t *gpio_request (unsigned gpio_num) +{ + urj_gpio_t *gpio; + char fname[50]; + int ret; + + ret = gpio_export (gpio_num, 1); + if (ret != URJ_STATUS_OK) + return NULL; + + gpio = malloc (sizeof (*gpio)); + if (!gpio) + return NULL; + + gpio->gpio = gpio_num; + + snprintf (fname, sizeof (fname) - 1, + "%sgpio%u/direction", GPIO_PATH, gpio_num); + fname[sizeof (fname) - 1] = '\0'; + gpio->fd_dir = open (fname, O_RDWR); + if (gpio->fd_dir < 0) + goto err; + + snprintf (fname, sizeof (fname) - 1, + "%sgpio%u/value", GPIO_PATH, gpio_num); + fname[sizeof (fname) - 1] = '\0'; + gpio->fd_val = open (fname, O_RDWR); + if (gpio->fd_val < 0) + goto err_dir; + + return gpio; + + err_dir: + close (gpio->fd_dir); + err: + free (gpio); + return NULL; +} + +void gpio_free (urj_gpio_t *gpio) +{ + gpio_export (gpio->gpio, 0); + close (gpio->fd_dir); + close (gpio->fd_val); + free (gpio); +} + +static int gpio_write_string (int fd, const char *string, size_t len) +{ + return pwrite (fd, string, len, 0) == len ? URJ_STATUS_OK : URJ_STATUS_FAIL; +} + +int gpio_direction_output (urj_gpio_t *gpio) +{ + return gpio_write_string (gpio->fd_dir, "out", 3); +} + +int gpio_direction_input (urj_gpio_t *gpio) +{ + return gpio_write_string (gpio->fd_dir, "in", 2); +} + +int gpio_get_value (urj_gpio_t *gpio) +{ + ssize_t ret; + char value; + + ret = pread (gpio->fd_val, &value, 1, 0); + + if (ret != 1) + { + urj_warning (_("Error getting value of gpio %u\n"), gpio->gpio); + return URJ_STATUS_FAIL; + } + + return value == '1'; +} + +int gpio_set_value (urj_gpio_t *gpio, int value) +{ + char gpio_value = value + '0'; + return gpio_write_string (gpio->fd_val, &gpio_value, 1); +} + +#endif Index: src/tap/gpio_core.h =================================================================== --- src/tap/gpio_core.h (revision 0) +++ src/tap/gpio_core.h (revision 0) @@ -0,0 +1,40 @@ +/* + * (C) Copyright 2010 + * Stefano Babic, DENX Software Engineering, [email protected]. + * GPIO JTAG Cable Driver + * + * Based on TS7800 GPIO JTAG Cable Driver + * Copyright (C) 2008 Catalin Ionescu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#ifndef URJ_GPIO_CORE_H +#define URJ_GPIO_CORE_H + +typedef struct gpio urj_gpio_t; + +extern urj_gpio_t *gpio_request (unsigned gpio); +extern void gpio_free (urj_gpio_t *gpio); + +extern int gpio_direction_input (urj_gpio_t *gpio); +extern int gpio_direction_output (urj_gpio_t *gpio); + +extern int gpio_get_value (urj_gpio_t *gpio); +extern int gpio_set_value (urj_gpio_t *gpio, int value); + +#endif Index: src/tap/cable/gpio.c =================================================================== --- src/tap/cable/gpio.c (revision 2012) +++ src/tap/cable/gpio.c (working copy) @@ -39,9 +39,7 @@ #include "generic.h" -#define GPIO_PATH "/sys/class/gpio/" -#define GPIO_EXPORT_PATH GPIO_PATH "export" -#define GPIO_UNEXPORT_PATH GPIO_PATH "unexport" +#include "gpio_core.h" /* pin mapping */ enum { @@ -53,124 +51,33 @@ enum { }; typedef struct { - unsigned int jtag_gpios[4]; - int signals; - uint32_t lastout; - int fd_gpios[4]; + urj_gpio_t *gpios[4]; + unsigned jtag_gpios[4]; + int signals; + uint32_t lastout; } gpio_params_t; -static int gpio_export (unsigned int gpio, int export) -{ - char *fname; - FILE *fp; - - if (export) - fname = GPIO_EXPORT_PATH; - else - fname = GPIO_UNEXPORT_PATH; - - fp = fopen (fname, FOPEN_W); - if (!fp) - { - urj_warning (_("%s: cannot open to (un)export GPIO %u\n"), fname, gpio); - return URJ_STATUS_FAIL; - } - - fprintf (fp, "%u", gpio); - fclose (fp); - - return URJ_STATUS_OK; -} - -static int gpio_direction (unsigned int gpio, int out) -{ - int ret; - char fname[50]; - FILE *fp; - - snprintf (fname, sizeof (fname) - 1, - "%sgpio%u/direction", GPIO_PATH, gpio); - fname[sizeof (fname) - 1] = '\0'; - - fp = fopen (fname, FOPEN_W); - if (!fp) - { - urj_warning (_("%s: cannot open to set direction\n"), fname); - return URJ_STATUS_FAIL; - } - - ret = fprintf (fp, "%s", out ? "out" : "in"); - fclose (fp); - - if (ret != strlen (out ? "out" : "in")) - { - urj_warning (_("Error setting direction gpio %u %s %d\n"), - gpio, out ? "out" : "in", ret); - return URJ_STATUS_FAIL; - } - - return URJ_STATUS_OK; -} - -static int gpio_set_value (int fd, int value) -{ - ssize_t ret; - char gpio_value = value + '0'; - - ret = write (fd, &gpio_value, 1); - if (ret != 1) - { - urj_warning (_("Error setting value gpio\n")); - return URJ_STATUS_FAIL; - } - - return URJ_STATUS_OK; -} - -static int gpio_get_value (int fd, unsigned int gpio) -{ - ssize_t ret; - char value; - - ret = pread (fd, &value, 1, 0); - - if (ret != 1) - { - urj_warning (_("Error getting value of gpio %u\n"), gpio); - return URJ_STATUS_FAIL; - } - - return value == '1'; -} - static int gpio_open (urj_cable_t *cable) { gpio_params_t *p = cable->params; - char fname[50]; - int i, ret; + int i; /* Export all gpios */ for (i = 0; i < GPIO_REQUIRED; i++) { - unsigned int gpio = p->jtag_gpios[i]; + unsigned gpio = p->jtag_gpios[i]; - ret = gpio_export (gpio, 1); - if (ret) + p->gpios[i] = gpio_request (gpio); + if (!p->gpios[i]) { urj_warning (_("gpio[%d] %u cannot be exported\n"), i, gpio); return URJ_STATUS_FAIL; } - gpio_direction (gpio, (i == GPIO_TDO) ? 0 : 1); - - snprintf (fname, sizeof (fname), "%sgpio%u/value", GPIO_PATH, gpio); - fname[sizeof (fname) - 1] = '\0'; - p->fd_gpios[i] = open (fname, O_RDWR); - if (p->fd_gpios[i] < 0) - { - urj_warning (_("%s: cannot open gpio[%d] %u\n"), fname, i, gpio); - return URJ_STATUS_FAIL; - } + if (i == GPIO_TDO) + gpio_direction_input (p->gpios[i]); + else + gpio_direction_output (p->gpios[i]); } return URJ_STATUS_OK; @@ -179,15 +86,11 @@ gpio_open (urj_cable_t *cable) static int gpio_close (urj_cable_t *cable) { - int i; gpio_params_t *p = cable->params; + int i; for (i = 0; i < GPIO_REQUIRED; i++) - { - if (p->fd_gpios[i]) - close (p->fd_gpios[i]); - gpio_export (p->jtag_gpios[i], 0); - } + gpio_free (p->gpios[i]); return URJ_STATUS_OK; } @@ -309,14 +212,14 @@ gpio_clock (urj_cable_t *cable, int tms, tms = tms ? 1 : 0; tdi = tdi ? 1 : 0; - gpio_set_value (p->fd_gpios[GPIO_TMS], tms); - gpio_set_value (p->fd_gpios[GPIO_TDI], tdi); + gpio_set_value (p->gpios[GPIO_TMS], tms); + gpio_set_value (p->gpios[GPIO_TDI], tdi); for (i = 0; i < n; i++) { - gpio_set_value (p->fd_gpios[GPIO_TCK], 0); - gpio_set_value (p->fd_gpios[GPIO_TCK], 1); - gpio_set_value (p->fd_gpios[GPIO_TCK], 0); + gpio_set_value (p->gpios[GPIO_TCK], 0); + gpio_set_value (p->gpios[GPIO_TCK], 1); + gpio_set_value (p->gpios[GPIO_TCK], 0); } } @@ -325,14 +228,14 @@ gpio_get_tdo ( urj_cable_t *cable ) { gpio_params_t *p = cable->params; - gpio_set_value(p->fd_gpios[GPIO_TCK], 0); - gpio_set_value(p->fd_gpios[GPIO_TDI], 0); - gpio_set_value(p->fd_gpios[GPIO_TMS], 0); + gpio_set_value(p->gpios[GPIO_TCK], 0); + gpio_set_value(p->gpios[GPIO_TDI], 0); + gpio_set_value(p->gpios[GPIO_TMS], 0); p->lastout &= ~(URJ_POD_CS_TMS | URJ_POD_CS_TDI | URJ_POD_CS_TCK); urj_tap_cable_wait (cable); - return gpio_get_value (p->fd_gpios[GPIO_TDO], p->jtag_gpios[GPIO_TDO]); + return gpio_get_value (p->gpios[GPIO_TDO]); } static int @@ -360,11 +263,11 @@ gpio_set_signal (urj_cable_t *cable, int if (mask != 0) { if (mask & URJ_POD_CS_TMS) - gpio_set_value (p->fd_gpios[GPIO_TMS], val & URJ_POD_CS_TMS); + gpio_set_value (p->gpios[GPIO_TMS], val & URJ_POD_CS_TMS); if (mask & URJ_POD_CS_TDI) - gpio_set_value (p->fd_gpios[GPIO_TDI], val & URJ_POD_CS_TDI); + gpio_set_value (p->gpios[GPIO_TDI], val & URJ_POD_CS_TDI); if (mask & URJ_POD_CS_TCK) - gpio_set_value (p->fd_gpios[GPIO_TCK], val & URJ_POD_CS_TCK); + gpio_set_value (p->gpios[GPIO_TCK], val & URJ_POD_CS_TCK); } p->lastout = val & mask; Index: src/tap/Makefile.am =================================================================== --- src/tap/Makefile.am (revision 2012) +++ src/tap/Makefile.am (working copy) @@ -79,6 +79,7 @@ endif if ENABLE_CABLE_GPIO libtap_la_SOURCES += \ + gpio_core.c \ cable/gpio.c endif ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-novd2d _______________________________________________ UrJTAG-development mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/urjtag-development
