changeset 144:35acc12e98d3

Porting teensy-loader-cli to use libusb-1.0 (from 0.1). - Currently only Linux tested.
author Jacob Alexander <haata@kiibohd.com>
date Fri, 18 Apr 2014 00:18:02 -0700
parents f2e4dd42bdab
children 3d21ecd24e31
files CMakeLists.txt LoadFile/CMakeLists.txt LoadFile/FindLibUSB-1.0.cmake LoadFile/FindLibUSB.cmake LoadFile/teensy_loader_cli.c README setup.cmake
diffstat 7 files changed, 203 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Apr 17 19:20:56 2014 -0700
+++ b/CMakeLists.txt	Fri Apr 18 00:18:02 2014 -0700
@@ -25,8 +25,8 @@
 #| "avr"       # Teensy++ 2.0
 #| "arm"       # Teensy   3.0
 #| "arm"       # Teensy   3.1
-set( COMPILER_FAMILY "arm" )
-#set( COMPILER_FAMILY "avr" )
+#set( COMPILER_FAMILY "arm" )
+set( COMPILER_FAMILY "avr" )
 
 message( STATUS "Compiler Family:" )
 message( "${COMPILER_FAMILY}" )
--- a/LoadFile/CMakeLists.txt	Thu Apr 17 19:20:56 2014 -0700
+++ b/LoadFile/CMakeLists.txt	Fri Apr 18 00:18:02 2014 -0700
@@ -48,7 +48,7 @@
 #| Linux - libusb
 if( CMAKE_SYSTEM_NAME MATCHES "Linux" )
 	# Find libusb (not 1.0)
-	find_package( LibUSB REQUIRED )
+	find_package( LibUSB-1.0 REQUIRED )
 
 	# Defines
 	set( DEFINES -s -DUSE_LIBUSB )
@@ -94,7 +94,7 @@
 #
 
 #| Default CFLAGS
-set( CFLAGS -O2 -Wall )
+set( CFLAGS -O2 -Wall -std=gnu99 )
 
 add_definitions( ${CFLAGS} ${DEFINES} )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LoadFile/FindLibUSB-1.0.cmake	Fri Apr 18 00:18:02 2014 -0700
@@ -0,0 +1,96 @@
+# Attempts to find libusb-1.0
+#
+#  LIBUSB_FOUND - system has libusb
+#  LIBUSB_INCLUDE_DIRS - the libusb include directory
+#  LIBUSB_LIBRARIES - Link these to use libusb
+#  LIBUSB_DEFINITIONS - Compiler switches required for using libusb
+#
+#  Adapted from cmake-modules Google Code project
+#
+#  Copyright (c) 2006 Andreas Schneider <mail@cynapses.org>
+#
+#  (Changes for libusb) Copyright (c) 2014 Jacob Alexander <haata@kiibohd.com>
+#
+# Redistribution and use is allowed according to the terms of the New BSD license.
+#
+# CMake-Modules Project New BSD License
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+#   list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of the CMake-Modules Project nor the names of its
+#   contributors may be used to endorse or promote products derived from this
+#   software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+
+if ( LIBUSB_LIBRARIES AND LIBUSB_INCLUDE_DIRS )
+	# in cache already
+	set( LIBUSB_FOUND TRUE )
+else ()
+	find_path( LIBUSB_INCLUDE_DIR
+		NAMES
+			libusb.h
+		PATHS
+			/usr/include
+			/usr/local/include
+			/opt/local/include
+			/sw/include
+			/include
+		PATH_SUFFIXES
+			libusb-1.0
+	)
+
+	find_library( LIBUSB_LIBRARY
+		NAMES
+			usb-1.0
+		PATHS
+			/usr/lib
+			/usr/local/lib
+			/opt/local/lib
+			/sw/lib
+			/lib
+	)
+
+	set( LIBUSB_INCLUDE_DIRS ${LIBUSB_INCLUDE_DIR} )
+	set( LIBUSB_LIBRARIES ${LIBUSB_LIBRARY} )
+
+	if ( LIBUSB_INCLUDE_DIRS AND LIBUSB_LIBRARIES )
+		 set( LIBUSB_FOUND TRUE )
+	endif ()
+
+	if ( LIBUSB_FOUND )
+		if ( NOT LIBUSB_FIND_QUIETLY )
+			message( STATUS "Found libusb:" )
+			message( STATUS " - Includes: ${LIBUSB_INCLUDE_DIRS}" )
+			message( STATUS " - Libraries: ${LIBUSB_LIBRARIES}" )
+		endif ()
+	else ()
+		if ( LIBUSB_FIND_REQUIRED )
+			message( FATAL_ERROR "Could not find libusb-1.0" )
+		endif ()
+	endif ()
+
+	# show the LIBUSB_INCLUDE_DIRS and LIBUSB_LIBRARIES variables only in the advanced view
+	mark_as_advanced( LIBUSB_INCLUDE_DIRS LIBUSB_LIBRARIES )
+
+endif ()
+
--- a/LoadFile/FindLibUSB.cmake	Thu Apr 17 19:20:56 2014 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-# Attempts to find libusb (not libusb-1.0)
-#
-#  LIBUSB_FOUND - system has libusb
-#  LIBUSB_INCLUDE_DIRS - the libusb include directory
-#  LIBUSB_LIBRARIES - Link these to use libusb
-#  LIBUSB_DEFINITIONS - Compiler switches required for using libusb
-#
-#  Adapted from cmake-modules Google Code project
-#
-#  Copyright (c) 2006 Andreas Schneider <mail@cynapses.org>
-#
-#  (Changes for libusb) Copyright (c) 2014 Jacob Alexander <haata@kiibohd.com>
-#
-# Redistribution and use is allowed according to the terms of the New BSD license.
-#
-# CMake-Modules Project New BSD License
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
-#
-# * Redistributions of source code must retain the above copyright notice, this
-#   list of conditions and the following disclaimer.
-#
-# * Redistributions in binary form must reproduce the above copyright notice,
-#   this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-#
-# * Neither the name of the CMake-Modules Project nor the names of its
-#   contributors may be used to endorse or promote products derived from this
-#   software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
-# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
-# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-
-if ( LIBUSB_LIBRARIES AND LIBUSB_INCLUDE_DIRS )
-	# in cache already
-	set( LIBUSB_FOUND TRUE )
-else ()
-	find_path( LIBUSB_INCLUDE_DIR
-		NAMES
-			usb.h
-		PATHS
-			/usr/include
-			/usr/local/include
-			/opt/local/include
-			/sw/include
-			/include
-	)
-
-	find_library( LIBUSB_LIBRARY
-		NAMES
-			usb
-		PATHS
-			/usr/lib
-			/usr/local/lib
-			/opt/local/lib
-			/sw/lib
-			/lib
-	)
-
-	set( LIBUSB_INCLUDE_DIRS ${LIBUSB_INCLUDE_DIR} )
-	set( LIBUSB_LIBRARIES ${LIBUSB_LIBRARY} )
-
-	if ( LIBUSB_INCLUDE_DIRS AND LIBUSB_LIBRARIES )
-		 set( LIBUSB_FOUND TRUE )
-	endif ()
-
-	if ( LIBUSB_FOUND )
-		if ( NOT LIBUSB_FIND_QUIETLY )
-			message( STATUS "Found libusb:" )
-			message( STATUS " - Includes: ${LIBUSB_INCLUDE_DIRS}" )
-			message( STATUS " - Libraries: ${LIBUSB_LIBRARIES}" )
-		endif ()
-	else ()
-		if ( LIBUSB_FIND_REQUIRED )
-			message( FATAL_ERROR "Could not find libusb" )
-		endif ()
-	endif ()
-
-	# show the LIBUSB_INCLUDE_DIRS and LIBUSB_LIBRARIES variables only in the advanced view
-	mark_as_advanced( LIBUSB_INCLUDE_DIRS LIBUSB_LIBRARIES )
-
-endif ()
-
--- a/LoadFile/teensy_loader_cli.c	Thu Apr 17 19:20:56 2014 -0700
+++ b/LoadFile/teensy_loader_cli.c	Fri Apr 18 00:18:02 2014 -0700
@@ -198,106 +198,137 @@
 
 #if defined(USE_LIBUSB)
 
-// http://libusb.sourceforge.net/doc/index.html
-#include <usb.h>
+#include <libusb-1.0/libusb.h>
 
-usb_dev_handle * open_usb_device(int vid, int pid)
+struct libusb_device_handle *open_usb_device( int vid, int pid )
 {
-	struct usb_bus *bus;
-	struct usb_device *dev;
-	usb_dev_handle *h;
-	char buf[128];
-	int r;
+	libusb_device **devs;
+	struct libusb_device_handle *handle = NULL;
+	struct libusb_device_handle *ret = NULL;
+
+	if ( libusb_init( NULL ) != 0 )
+		fprintf( stderr, "libusb_init failed.\n" );
+
+	size_t count = libusb_get_device_list( NULL, &devs );
+	if ( count < 0 )
+		fprintf( stderr, "libusb_get_device_list failed.\n" );
+
+	for ( size_t c = 0; c < count; c++ )
+	{
+		struct libusb_device_descriptor desc;
+		libusb_device *dev = devs[c];
+
+		if ( libusb_get_device_descriptor( dev, &desc ) < 0 )
+			fprintf( stderr, "libusb_get_device_descriptor failed.\n" );
+
+		//printf("ID: %04x  Product: %04x\n", desc.idVendor, desc.idProduct );
+		// Not correct device
+		if ( desc.idVendor != vid || desc.idProduct != pid )
+			continue;
 
-	usb_init();
-	usb_find_busses();
-	usb_find_devices();
-	//printf_verbose("\nSearching for USB device:\n");
-	for (bus = usb_get_busses(); bus; bus = bus->next) {
-		for (dev = bus->devices; dev; dev = dev->next) {
-			//printf_verbose("bus \"%s\", device \"%s\" vid=%04X, pid=%04X\n",
-			//	bus->dirname, dev->filename,
-			//	dev->descriptor.idVendor,
-			//	dev->descriptor.idProduct
-			//);
-			if (dev->descriptor.idVendor != vid) continue;
-			if (dev->descriptor.idProduct != pid) continue;
-			h = usb_open(dev);
-			if (!h) {
-				printf_verbose("Found device but unable to open");
-				continue;
-			}
-			#ifdef LIBUSB_HAS_GET_DRIVER_NP
-			r = usb_get_driver_np(h, 0, buf, sizeof(buf));
-			if (r >= 0) {
-				r = usb_detach_kernel_driver_np(h, 0);
-				if (r < 0) {
-					usb_close(h);
-					printf_verbose("Device is in use by \"%s\" driver", buf);
-					continue;
-				}
-			}
-			#endif
-			// Mac OS-X - removing this call to usb_claim_interface() might allow
-			// this to work, even though it is a clear misuse of the libusb API.
-			// normally Apple's IOKit should be used on Mac OS-X
-			r = usb_claim_interface(h, 0);
-			if (r < 0) {
-				usb_close(h);
-				printf_verbose("Unable to claim interface, check USB permissions");
-				continue;
-			}
-			return h;
+		// Attempt to open the device
+		if ( libusb_open( dev, &handle ) != 0 )
+		{
+			fprintf( stderr, "Found device but unable to open\n" );
+			continue;
 		}
+
+		// Only required on Linux, other platforms will just ignore this call
+		libusb_detach_kernel_driver( handle, 0 );
+
+		// Mac OS-X - removing this call to usb_claim_interface() might allow
+		// this to work, even though it is a clear misuse of the libusb API.
+		// normally Apple's IOKit should be used on Mac OS-X
+		// XXX Is this still valid with libusb-1.0?
+
+		// Attempt to claim interface
+		int err = libusb_claim_interface( handle, 0 );
+		if ( err < 0 )
+		{
+			libusb_close( handle );
+			fprintf( stderr, "Unable to claim interface, check USB permissions: %d\n", err );
+			continue;
+		}
+
+		ret = handle;
+		break;
 	}
-	return NULL;
+
+	libusb_free_device_list( devs, 1 );
+
+	return ret;
 }
 
-static usb_dev_handle *libusb_teensy_handle = NULL;
+static struct libusb_device_handle *libusb_teensy_handle = NULL;
 
-int teensy_open(void)
+int teensy_open()
 {
 	teensy_close();
-	libusb_teensy_handle = open_usb_device(0x16C0, 0x0478);
-	if (libusb_teensy_handle) return 1;
+
+	libusb_teensy_handle = open_usb_device( 0x16C0, 0x0478 );
+
+	if ( libusb_teensy_handle )
+		return 1;
+
 	return 0;
 }
 
-int teensy_write(void *buf, int len, double timeout)
+int teensy_write( void *buf, int len, double timeout )
 {
 	int r;
 
-	if (!libusb_teensy_handle) return 0;
-	while (timeout > 0) {
-		r = usb_control_msg(libusb_teensy_handle, 0x21, 9, 0x0200, 0,
-			(char *)buf, len, (int)(timeout * 1000.0));
-		if (r >= 0) return 1;
+	if ( !libusb_teensy_handle )
+		return 0;
+
+	while ( timeout > 0 ) {
+		r = libusb_control_transfer( libusb_teensy_handle,
+			0x21, 9, 0x0200, 0,
+			(unsigned char *)buf, len,
+			(int)(timeout * 1000.0) );
+
+		if ( r >= 0 )
+			return 1;
+
 		//printf("teensy_write, r=%d\n", r);
-		usleep(10000);
+		usleep( 10000 );
 		timeout -= 0.01;  // TODO: subtract actual elapsed time
 	}
+
 	return 0;
 }
 
-void teensy_close(void)
+void teensy_close()
 {
-	if (!libusb_teensy_handle) return;
-	usb_release_interface(libusb_teensy_handle, 0);
-	usb_close(libusb_teensy_handle);
+	if ( !libusb_teensy_handle)
+		return;
+
+	libusb_release_interface( libusb_teensy_handle, 0 );
+	libusb_close( libusb_teensy_handle );
+
 	libusb_teensy_handle = NULL;
 }
 
-int hard_reboot(void)
+int hard_reboot()
 {
-	usb_dev_handle *rebootor;
+	struct libusb_device_handle *rebootor;
 	int r;
 
-	rebootor = open_usb_device(0x16C0, 0x0477);
-	if (!rebootor) return 0;
-	r = usb_control_msg(rebootor, 0x21, 9, 0x0200, 0, "reboot", 6, 100);
-	usb_release_interface(rebootor, 0);
-	usb_close(rebootor);
-	if (r < 0) return 0;
+	rebootor = open_usb_device( 0x16C0, 0x0477 );
+
+	if (!rebootor)
+		return 0;
+
+	r = libusb_control_transfer( rebootor,
+		0x21, 9, 0x0200, 0,
+		(unsigned char*)"reboot", 6,
+		100 );
+
+	libusb_release_interface( rebootor, 0 );
+	libusb_close( rebootor );
+
+	if (r < 0)
+		return 0;
+
 	return 1;
 }
 
--- a/README	Thu Apr 17 19:20:56 2014 -0700
+++ b/README	Fri Apr 18 00:18:02 2014 -0700
@@ -45,7 +45,6 @@
 - git (needed for some compilation info)
 - cmake
 - gcc-core
-- gcc-g++ or gcc-c++
 
 And make sure CMake is *NOT* installed through Cygwin. This is extremely important.
 If this is not possible, you'll have to play with your paths in Cygwin to prioritize the Windows version of CMake.
--- a/setup.cmake	Thu Apr 17 19:20:56 2014 -0700
+++ b/setup.cmake	Fri Apr 18 00:18:02 2014 -0700
@@ -20,7 +20,7 @@
 #| Please look at the {Scan,Macro,USB,Debug}/module.txt for information on the modules and how to create new ones
 
 ##| Deals with acquiring the keypress information and turning it into a key index
-set(   ScanModule "ADCTest" )
+set(   ScanModule "DPH" )
 
 ##| Provides the mapping functions for DefaultMap and handles any macro processing before sending to the OutputModule
 set(  MacroModule "PartialMap" )