Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: azonenberg/openfpga
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 85c1d9a4158f
Choose a base ref
...
head repository: azonenberg/openfpga
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b8dcb589dfdc
Choose a head ref
  • 13 commits
  • 17 files changed
  • 3 contributors

Commits on May 22, 2017

  1. Switch from libusb to hidapi

    cyrozap authored and ArcaneNibble committed May 22, 2017
    Copy the full SHA
    76fce9f View commit details
  2. Copy the full SHA
    371be48 View commit details
  3. Copy the full SHA
    7fe963f View commit details
  4. Fix OS X build

    cyrozap authored and ArcaneNibble committed May 22, 2017
    Copy the full SHA
    7e3c091 View commit details
  5. Copy the full SHA
    77e2851 View commit details
  6. Copy the full SHA
    abd9f7f View commit details
  7. vendor: Vendor our own copy of hidapi

    hidapi has a lot of tooling that is annoying to use. Because it is just
    a single file, just build it with cmake.
    ArcaneNibble committed May 22, 2017
    Copy the full SHA
    4a36228 View commit details
  8. Copy the full SHA
    605cadc View commit details
  9. Copy the full SHA
    22b4fda View commit details
  10. Copy the full SHA
    36bbae6 View commit details
  11. Copy the full SHA
    34a4e89 View commit details
  12. hidapi: Cruft removal

    Removed all of the stuff we definitely do not need. Further cleanup can
    be done later.
    ArcaneNibble committed May 22, 2017
    Copy the full SHA
    4960e43 View commit details

Commits on Jun 7, 2017

  1. Copy the full SHA
    b8dcb58 View commit details
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
# Root build script

cmake_minimum_required(VERSION 2.8)
project(openfpga CXX)
project(openfpga C CXX)
enable_testing()
include(GNUInstallDirs)

1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(vendor)
add_subdirectory(gpdevboard)
add_subdirectory(greenpak4)
add_subdirectory(gp4prog)
2 changes: 1 addition & 1 deletion src/gpdevboard/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -7,4 +7,4 @@ target_include_directories(gpdevboard
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(gpdevboard
usb-1.0 log)
hidapi log)
4 changes: 3 additions & 1 deletion src/gpdevboard/gpdevboard.h
Original file line number Diff line number Diff line change
@@ -25,10 +25,12 @@
#include <string>
#include <vector>

#include "hidapi.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// USB command wrappers

typedef struct libusb_device_handle* hdevice;
typedef hid_device* hdevice;

bool USBSetup();
void USBCleanup(hdevice hdev);
175 changes: 59 additions & 116 deletions src/gpdevboard/usb.cpp
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
**********************************************************************************************************************/

#include <libusb-1.0/libusb.h>

//Windows appears to define an ERROR macro in its headers.
//Conflicts with ERROR enum defined in log.h.
#if defined(_WIN32)
@@ -27,6 +25,8 @@
#include <log.h>
#include <gpdevboard.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>

using namespace std;

@@ -35,25 +35,27 @@ using namespace std;

bool SendInterruptTransfer(hdevice hdev, const uint8_t* buf, size_t size)
{
int transferred;
int err = 0;
if(0 != (err = libusb_interrupt_transfer(hdev, 2|LIBUSB_ENDPOINT_OUT,
const_cast<uint8_t*>(buf), size, &transferred, 250)))
//We need to prepend a zero byte here because this is the "report ID."
//There aren't actually report IDs in use, but the way hidapi works
//always requires this.
uint8_t *new_buf = (uint8_t *)malloc(size + 1);
new_buf[0] = 0x00;
memcpy(&new_buf[1], buf, size);
if(hid_write(hdev, new_buf, size + 1) < 0)
{
LogError("libusb_interrupt_transfer failed (%s)\n", libusb_error_name(err));
free(new_buf);
LogError("hid_write failed (%ls)\n", hid_error(hdev));
return false;
}
free(new_buf);
return true;
}

bool ReceiveInterruptTransfer(hdevice hdev, uint8_t* buf, size_t size)
{
int transferred;
int err = 0;
if(0 != (err = libusb_interrupt_transfer(hdev, 1|LIBUSB_ENDPOINT_IN,
buf, size, &transferred, 250)))
if(hid_read_timeout(hdev, buf, size, 250) < 0)
{
LogError("libusb_interrupt_transfer failed (%s)\n", libusb_error_name(err));
LogError("hid_read_timeout failed (%ls)\n", hid_error(hdev));
return false;
}
return true;
@@ -65,18 +67,18 @@ bool ReceiveInterruptTransfer(hdevice hdev, uint8_t* buf, size_t size)
//Set up USB stuff
bool USBSetup()
{
if(0 != libusb_init(NULL))
if(0 != hid_init())
{
LogError("libusb_init failed\n");
LogError("hid_init failed\n");
return false;
}
return true;
}

void USBCleanup(hdevice hdev)
{
libusb_close(hdev);
libusb_exit(NULL);
hid_close(hdev);
hid_exit();
}

/**
@@ -97,111 +99,31 @@ hdevice OpenDevice(uint16_t idVendor, uint16_t idProduct, int nboard)
return NULL;
}

libusb_device** list;
ssize_t devcount = libusb_get_device_list(NULL, &list);
if(devcount < 0)
{
LogError("libusb_get_device_list failed\n");
return NULL;
}
libusb_device* device = NULL;
bool found = false;
for(ssize_t i=0; i<devcount; i++)
{
device = list[i];

libusb_device_descriptor desc;
if(0 != libusb_get_device_descriptor(device, &desc))
continue;

//Skip anything from the wrong vendor
if(desc.idVendor != idVendor)
continue;

LogDebug("Found Silego device at bus %d, port %d\n",
libusb_get_bus_number(device),
libusb_get_port_number(device));

//If we are looking for one of several boards, skip the early ones
if(nboard > 0)
{
nboard --;
continue;
}

//If we match the PID, we're good to go
if(desc.idProduct == idProduct)
{
found = true;
int dev_index = 0;
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(idVendor, idProduct);
cur_dev = devs;
while (cur_dev) {
LogDebug("Found Silego device at %s\n", cur_dev->path);
if (dev_index == nboard)
break;
}
}
libusb_device_handle* hdev;
if(found)
{
LogVerbose("Using device at bus %d, port %d\n",
libusb_get_bus_number(device),
libusb_get_port_number(device));
if(0 != libusb_open(device, &hdev))
{
LogError("libusb_open failed\n");
return NULL;
}
}
libusb_free_device_list(list, 1);
if(!found)
{
return NULL;
cur_dev = cur_dev->next;
dev_index++;
}

#if !(defined(_WIN32) || defined(__APPLE__))
//Detach the kernel driver, if any
int err = libusb_detach_kernel_driver(hdev, 0);
if( (0 != err) && (LIBUSB_ERROR_NOT_FOUND != err) )
hdevice hdev;
if (!cur_dev)
{
LogError("Can't detach kernel driver\n");
hid_free_enumeration(devs);
return NULL;
}
#else
int err = 0;
#endif

//Set the device configuration
//If this fails, with LIBUSB_ERROR_BUSY, poll every 100 ms until the device is free
if(0 != (err = libusb_set_configuration(hdev, 1)))
LogVerbose("Using device at %s\n", cur_dev->path);
hdev = hid_open_path(cur_dev->path);
hid_free_enumeration(devs);
if (!hdev)
{
if(err == LIBUSB_ERROR_BUSY)
{
LogNotice("USB device is currently busy, blocking until it's free...\n");
while(true)
{
err = libusb_set_configuration(hdev, 1);

if(err == LIBUSB_ERROR_BUSY)
{
usleep(1000 * 100);
continue;
}

if(err == LIBUSB_SUCCESS)
break;

LogError("Failed to select device configuration (err = %d)\n", err);
return NULL;
}
}

else
{
LogError("Failed to select device configuration (err = %d)\n", err);
return NULL;
}
}

//Claim interface 0
if(0 != libusb_claim_interface(hdev, 0))
{
LogError("Failed to claim interface\n");
LogError("hid_open_path failed\n");
return NULL;
}

@@ -212,12 +134,33 @@ hdevice OpenDevice(uint16_t idVendor, uint16_t idProduct, int nboard)
bool GetStringDescriptor(hdevice hdev, uint8_t index, string &desc)
{
char strbuf[128];
if(libusb_get_string_descriptor_ascii(hdev, index, (unsigned char*)strbuf, sizeof(strbuf)) < 0)
wchar_t wstrbuf[sizeof(strbuf)];

switch (index)
{
LogFatal("libusb_get_string_descriptor_ascii failed\n");
return false;
case 1:
if (hid_get_product_string(hdev, wstrbuf, sizeof(strbuf)) < 0)
{
LogFatal("hid_get_product_string failed\n");
return false;
}
break;

case 2:
if (hid_get_manufacturer_string(hdev, wstrbuf, sizeof(strbuf)) < 0)
{
LogFatal("hid_get_manufacturer_string failed\n");
return false;
}
break;

default:
LogFatal("Invalid index %d\n", index);
return false;
}

wcstombs(strbuf, wstrbuf, sizeof(strbuf));

desc = strbuf;
return true;
}
5 changes: 2 additions & 3 deletions src/gpdevboard/utils.cpp
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
#include <unistd.h>
#include <cmath>
#include <cstring>
#include <libusb-1.0/libusb.h>

#include <log.h>
#include "gpdevboard.h"
@@ -62,7 +61,7 @@ bool CheckStatus(hdevice hdev)
*/
bool ResetDevicesInBootloader()
{
//Set up libusb
//Set up hidapi
if(!USBSetup())
return false;

@@ -82,7 +81,7 @@ bool ResetDevicesInBootloader()
for(auto h : handles)
{
SwitchMode(h);
libusb_close(h);
hid_close(h);
}

g_devicesResetFromBootloader = true;
1 change: 1 addition & 0 deletions src/vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(hidapi)
12 changes: 12 additions & 0 deletions src/vendor/hidapi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# FIXME: Make other platforms work
STRING (REGEX MATCH "Linux" IS_LINUX ${CMAKE_SYSTEM_NAME})

if(IS_LINUX)
add_subdirectory(linux)
elseif(WIN32)
add_subdirectory(windows)
elseif(APPLE)
add_subdirectory(macos)
else()
message(FATAL_ERROR "HID backend not supported for this platform")
endif()
26 changes: 26 additions & 0 deletions src/vendor/hidapi/LICENSE-bsd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2010, Alan Ott, Signal 11 Software
All rights reserved.

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 Signal 11 Software 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 HOLDER 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.
2 changes: 2 additions & 0 deletions src/vendor/hidapi/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This project has elected to use hidapi code under the terms of the BSD license.
See LICENSE-bsd.txt for details.
Loading