Hi folks,
I needed to use omconsole and openocd simultaneously to develop on freerunner
and sheevaplug. After a bit of tinkering, I did a messy hack into libusb to do
so. This should not go upstream, as it is completely unsafe to (ab)use the USB
specification in such a way, but maybe it could land into the ports tree to
help developers ?
Cheers,
--
Vincent / dermiste
Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/libusb/Makefile,v
retrieving revision 1.17
diff -u -r1.17 Makefile
--- Makefile 16 Sep 2007 02:53:01 -0000 1.17
+++ Makefile 27 May 2010 16:05:28 -0000
@@ -2,6 +2,8 @@
COMMENT= USB access library
+FLAVORS= ft2232
+
DISTNAME= libusb-0.1.12
SHARED_LIBS= usb 9.0 \
usbpp 10.0
@@ -27,5 +29,8 @@
CONFIGURE_STYLE= gnu
CONFIGURE_ARGS+= ${CONFIGURE_SHARED} \
--disable-build-docs
+.if ${FLAVOR:L:Mft2232}
+COPTS+= -DBSD_FTDI_HACK
+.endif
.include <bsd.port.mk>
Index: patches/patch-bsd_c
===================================================================
RCS file: /cvs/ports/devel/libusb/patches/patch-bsd_c,v
retrieving revision 1.7
diff -u -r1.7 patch-bsd_c
--- patches/patch-bsd_c 15 Nov 2006 18:34:13 -0000 1.7
+++ patches/patch-bsd_c 27 May 2010 16:05:28 -0000
@@ -1,7 +1,108 @@
$OpenBSD: patch-bsd_c,v 1.7 2006/11/15 18:34:13 pvalchev Exp $
---- bsd.c.orig Fri Mar 3 19:52:46 2006
-+++ bsd.c Tue Nov 14 21:00:00 2006
-@@ -361,7 +361,7 @@ int usb_bulk_read(usb_dev_handle *dev, i
+--- bsd.c.orig Sat Mar 4 03:52:46 2006
++++ bsd.c Sat May 22 11:25:48 2010
+@@ -139,7 +139,6 @@ int usb_os_open(usb_dev_handle *dev)
+ {
+ int i;
+ struct bsd_usb_dev_handle_info *info;
+- char ctlpath[PATH_MAX + 1];
+
+ info = malloc(sizeof(struct bsd_usb_dev_handle_info));
+ if (!info)
+@@ -147,20 +146,23 @@ int usb_os_open(usb_dev_handle *dev)
+ dev->impl_info = info;
+
+ #ifdef __FreeBSD_kernel__
+- snprintf(ctlpath, PATH_MAX, "%s", dev->device->filename);
++ snprintf(dev->ctlpath, PATH_MAX, "%s", dev->device->filename);
+ #else
+- snprintf(ctlpath, PATH_MAX, "%s.00", dev->device->filename);
++ snprintf(dev->ctlpath, PATH_MAX, "%s.00", dev->device->filename);
+ #endif
+- dev->fd = open(ctlpath, O_RDWR);
++ dev->fd = open(dev->ctlpath, O_RDWR);
+ if (dev->fd < 0) {
+- dev->fd = open(ctlpath, O_RDONLY);
++ dev->fd = open(dev->ctlpath, O_RDONLY);
+ if (dev->fd < 0) {
+ free(info);
+ USB_ERROR_STR(-errno, "failed to open %s: %s",
+- ctlpath, strerror(errno));
++ dev->ctlpath, strerror(errno));
+ }
+ }
+
++ close(dev->fd);
++ dev->fd = -1;
++
+ /* Mark the endpoints as not yet open */
+ for (i = 0; i < USB_MAX_ENDPOINTS; i++)
+ info->ep_fd[i] = -1;
+@@ -199,6 +201,19 @@ int usb_set_configuration(usb_dev_handle *dev, int con
+ {
+ int ret;
+
++#ifdef BSD_FTDI_HACK
++ if (dev->fd <= 0) {
++ dev->fd = open(dev->ctlpath, O_RDWR);
++ if (dev->fd < 0) {
++ dev->fd = open(dev->ctlpath, O_RDONLY);
++ if (dev->fd < 0) {
++ USB_ERROR_STR(-errno, "failed to open %s: %s",
++ dev->ctlpath, strerror(errno));
++ }
++ }
++ }
++#endif
++
+ ret = ioctl(dev->fd, USB_SET_CONFIG, &configuration);
+ if (ret < 0)
+ USB_ERROR_STR(-errno, "could not set config %d: %s", configuration,
+@@ -206,6 +221,11 @@ int usb_set_configuration(usb_dev_handle *dev, int con
+
+ dev->config = configuration;
+
++#ifdef BSD_FTDI_HACK
++ close(dev->fd);
++ dev->fd = -1;
++#endif
++
+ return 0;
+ }
+
+@@ -230,6 +250,19 @@ int usb_set_altinterface(usb_dev_handle *dev, int alte
+ int ret;
+ struct usb_alt_interface intf;
+
++#ifdef BSD_FTDI_HACK
++ if (dev->fd <= 0) {
++ dev->fd = open(dev->ctlpath, O_RDWR);
++ if (dev->fd < 0) {
++ dev->fd = open(dev->ctlpath, O_RDONLY);
++ if (dev->fd < 0) {
++ USB_ERROR_STR(-errno, "failed to open %s: %s",
++ dev->ctlpath, strerror(errno));
++ }
++ }
++ }
++#endif
++
+ if (dev->interface < 0)
+ USB_ERROR(-EINVAL);
+
+@@ -243,6 +276,11 @@ int usb_set_altinterface(usb_dev_handle *dev, int alte
+
+ dev->altsetting = alternate;
+
++#ifdef BSD_FTDI_HACK
++ close(dev->fd);
++ dev->fd = -1;
++#endif
++
+ return 0;
+ }
+
+@@ -361,7 +399,7 @@ int usb_bulk_read(usb_dev_handle *dev, int ep, char *b
int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
int timeout)
{
@@ -10,7 +111,7 @@
/* Ensure the endpoint address is correct */
ep &= ~USB_ENDPOINT_IN;
-@@ -383,8 +383,7 @@ int usb_interrupt_write(usb_dev_handle *
+@@ -383,8 +421,7 @@ int usb_interrupt_write(usb_dev_handle *dev, int ep, c
USB_ERROR_STR(-errno, "error setting timeout: %s",
strerror(errno));
@@ -20,7 +121,7 @@
if (ret < 0)
#ifdef __FreeBSD_kernel__
USB_ERROR_STR(-errno, "error writing to interrupt endpoint %s.%d: %s",
-@@ -394,16 +393,13 @@ int usb_interrupt_write(usb_dev_handle *
+@@ -394,16 +431,13 @@ int usb_interrupt_write(usb_dev_handle *dev, int ep, c
dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif
@@ -39,7 +140,7 @@
/* Ensure the endpoint address is correct */
ep |= USB_ENDPOINT_IN;
-@@ -428,8 +424,7 @@ int usb_interrupt_read(usb_dev_handle *d
+@@ -428,8 +462,7 @@ int usb_interrupt_read(usb_dev_handle *dev, int ep, ch
if (ret < 0)
USB_ERROR_STR(-errno, "error setting short xfer: %s", strerror(errno));
@@ -49,7 +150,7 @@
if (ret < 0)
#ifdef __FreeBSD_kernel__
USB_ERROR_STR(-errno, "error reading from interrupt endpoint %s.%d: %s",
-@@ -438,10 +433,7 @@ int usb_interrupt_read(usb_dev_handle *d
+@@ -438,10 +471,7 @@ int usb_interrupt_read(usb_dev_handle *dev, int ep, ch
USB_ERROR_STR(-errno, "error reading from interrupt endpoint %s.%02d:
%s",
dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
#endif
@@ -61,3 +162,35 @@
}
int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
+@@ -450,6 +480,19 @@ int usb_control_msg(usb_dev_handle *dev, int requestty
+ struct usb_ctl_request req;
+ int ret;
+
++#ifdef BSD_FTDI_HACK
++ if (dev->fd <= 0) {
++ dev->fd = open(dev->ctlpath, O_RDWR);
++ if (dev->fd < 0) {
++ dev->fd = open(dev->ctlpath, O_RDONLY);
++ if (dev->fd < 0) {
++ USB_ERROR_STR(-errno, "failed to open %s: %s",
++ dev->ctlpath, strerror(errno));
++ }
++ }
++ }
++#endif
++
+ if (usb_debug >= 3)
+ fprintf(stderr, "usb_control_msg: %d %d %d %d %p %d %d\n",
+ requesttype, request, value, index, bytes, size, timeout);
+@@ -476,6 +519,11 @@ int usb_control_msg(usb_dev_handle *dev, int requestty
+ if (ret < 0)
+ USB_ERROR_STR(-errno, "error sending control message: %s",
+ strerror(errno));
++
++#ifdef BSD_FTDI_HACK
++ close(dev->fd);
++ dev->fd = -1;
++#endif
+
+ return UGETW(req.ucr_request.wLength);
+ }
Index: patches/patch-usbi_h
===================================================================
RCS file: patches/patch-usbi_h
diff -N patches/patch-usbi_h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ patches/patch-usbi_h 27 May 2010 16:05:28 -0000
@@ -0,0 +1,11 @@
+$OpenBSD$
+--- usbi.h.orig Sat May 22 10:51:26 2010
++++ usbi.h Sat May 22 10:49:29 2010
+@@ -40,6 +40,7 @@ extern int usb_debug;
+
+ struct usb_dev_handle {
+ int fd;
++ char ctlpath[PATH_MAX + 1];
+
+ struct usb_bus *bus;
+ struct usb_device *device;