Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to libusb-1.0 #25

Merged
merged 5 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions examples/custom-class/commandline/set-led.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ respectively.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h> /* this is libusb */
#include <libusb.h> /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */

#include "../firmware/requests.h" /* custom request numbers */
Expand All @@ -39,13 +39,17 @@ static void usage(char *name)

int main(int argc, char **argv)
{
usb_dev_handle *handle = NULL;
libusb_device_handle *handle = NULL;
const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
char buffer[4];
int cnt, vid, pid, isOn;
int cnt, vid, pid, isOn, r;

usb_init();
r = libusb_init(NULL);
if (0 != r) {
fprintf(stderr, "Warning: cannot initialize libusb: %s\n", libusb_strerror(r));
exit(1);
}
if(argc < 2){ /* we need at least one argument */
usage(argv[0]);
exit(1);
Expand All @@ -66,35 +70,37 @@ int cnt, vid, pid, isOn;
* needs it: */
#if 0
int retries = 1, usbConfiguration = 1, usbInterface = 0;
if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
r = libusb_set_configuration(handle, usbConfiguration);
if(r != 0 && showWarnings){
fprintf(stderr, "Warning: could not set configuration: %s\n", libusb_strerror(r));
}
/* now try to claim the interface and detach the kernel HID driver on
* Linux and other operating systems which support the call. */
while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
while((len = libusb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
r = libusb_detach_kernel_driver(handle, 0);
if(r != 0 && showWarnings){
fprintf(stderr, "Warning: could not detach kernel driver: %s\n", libusb_strerror(r));
}
#endif
}
#endif

if(strcasecmp(argv[1], "status") == 0){
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, (unsigned char *)buffer, sizeof(buffer), 5000);
if(cnt < 1){
if(cnt < 0){
fprintf(stderr, "USB error: %s\n", usb_strerror());
fprintf(stderr, "USB error: %s\n", libusb_strerror(cnt));
}else{
fprintf(stderr, "only %d bytes received.\n", cnt);
}
}else{
printf("LED is %s\n", buffer[0] ? "on" : "off");
}
}else if((isOn = (strcasecmp(argv[1], "on") == 0)) || strcasecmp(argv[1], "off") == 0){
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, isOn, 0, buffer, 0, 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, isOn, 0, (unsigned char *)buffer, 0, 5000);
if(cnt < 0){
fprintf(stderr, "USB error: %s\n", usb_strerror());
fprintf(stderr, "USB error: %s\n", libusb_strerror(cnt));
}
#if ENABLE_TEST
}else if(strcasecmp(argv[1], "test") == 0){
Expand All @@ -107,9 +113,9 @@ int cnt, vid, pid, isOn;
fprintf(stderr, "\r%05d", i+1);
fflush(stderr);
}
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_ECHO, value, index, buffer, sizeof(buffer), 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_ECHO, value, index, (unsigned char *)buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, libusb_strerror(cnt));
break;
}else if(cnt != 4){
fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of 4\n", i, cnt);
Expand All @@ -129,6 +135,6 @@ int cnt, vid, pid, isOn;
usage(argv[0]);
exit(1);
}
usb_close(handle);
libusb_close(handle);
return 0;
}
35 changes: 22 additions & 13 deletions examples/drivertest/commandline/runtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ General Description:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h> /* this is libusb */
#include <time.h>
#include <libusb.h> /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */

#include "../firmware/requests.h" /* custom request numbers */
Expand Down Expand Up @@ -79,13 +80,17 @@ int i, rval = 0;

int main(int argc, char **argv)
{
usb_dev_handle *handle = NULL;
libusb_device_handle *handle = NULL;
const uchar rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
char txBuffer[64], rxBuffer[64];
int cnt, vid, pid, i, j;
int cnt, vid, pid, i, j, r;

usb_init();
r = libusb_init(NULL);
if (0 != r) {
fprintf(stderr, "Warning: cannot initialize libusb: %s\n", libusb_strerror(r));
exit(1);
}
/* compute VID/PID from usbconfig.h so that there is a central source of information */
vid = rawVid[1] * 256 + rawVid[0];
pid = rawPid[1] * 256 + rawPid[0];
Expand All @@ -98,25 +103,29 @@ int cnt, vid, pid, i, j;
if(argc > 2){ /* set osccal */
int osccal = atoi(argv[2]);
printf("setting osccal to %d\n", osccal);
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_SET_OSCCAL, osccal, 0, txBuffer, 0, 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN, CUSTOM_RQ_SET_OSCCAL, osccal, 0, (unsigned char *)txBuffer, 0, 5000);
if(cnt < 0){
fprintf(stderr, "\nUSB error setting osccal: %s\n", usb_strerror());
fprintf(stderr, "\nUSB error setting osccal: %s\n", libusb_strerror(cnt));
}
}else{
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_OSCCAL, 0, 0, rxBuffer, 1, 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN, CUSTOM_RQ_GET_OSCCAL, 0, 0, (unsigned char *)rxBuffer, 1, 5000);
if(cnt < 0){
fprintf(stderr, "\nUSB error getting osccal: %s\n", usb_strerror());
fprintf(stderr, "\nUSB error getting osccal: %s\n", libusb_strerror(cnt));
}else{
printf("osccal = %d\n", (unsigned char)rxBuffer[0]);
}
}
}else{
#ifdef __linux__
srandom(time(NULL));
#else
srandomdev();
#endif
for(i = 0; i <= 100000; i++){
fillBuffer(txBuffer, sizeof(txBuffer));
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_DATA, 0, 0, txBuffer, sizeof(txBuffer), 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, CUSTOM_RQ_SET_DATA, 0, 0, (unsigned char *)txBuffer, sizeof(txBuffer), 5000);
if(cnt < 0){
fprintf(stderr, "\nUSB tx error in iteration %d: %s\n", i, usb_strerror());
fprintf(stderr, "\nUSB tx error in iteration %d: %s\n", i, libusb_strerror(cnt));
break;
}else if(cnt != sizeof(txBuffer)){
fprintf(stderr, "\nerror in iteration %d: %d bytes sent instead of %d\n", i, cnt, (int)sizeof(txBuffer));
Expand All @@ -125,9 +134,9 @@ int cnt, vid, pid, i, j;
for(j = 0; j < sizeof(rxBuffer); j++){
rxBuffer[j] = ~txBuffer[j];
}
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DATA, 0, 0, rxBuffer, sizeof(rxBuffer), 5000);
cnt = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN, CUSTOM_RQ_GET_DATA, 0, 0, (unsigned char *)rxBuffer, sizeof(rxBuffer), 5000);
if(cnt < 0){
fprintf(stderr, "\nUSB rx error in iteration %d: %s\n", i, usb_strerror());
fprintf(stderr, "\nUSB rx error in iteration %d: %s\n", i, libusb_strerror(cnt));
break;
}else if(cnt != sizeof(txBuffer)){
fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of %d\n", i, cnt, (int)sizeof(rxBuffer));
Expand All @@ -146,6 +155,6 @@ int cnt, vid, pid, i, j;
}
fprintf(stderr, "\nTest completed.\n");
}
usb_close(handle);
libusb_close(handle);
return 0;
}
10 changes: 5 additions & 5 deletions examples/hid-data/commandline/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
# system:

# Use the following 3 lines on Unix and Mac OS X:
USBFLAGS= `libusb-config --cflags`
USBLIBS= `libusb-config --libs`
USBFLAGS= `pkg-config --cflags libusb-1.0`
USBLIBS= `pkg-config --libs libusb-1.0`
EXE_SUFFIX=

# Use the following 3 lines on Windows and comment out the 3 above:
#USBFLAGS=
#USBLIBS= -lhid -lusb -lsetupapi
#EXE_SUFFIX= .exe
#USBFLAGS = -I/usr/local/include/libusb-1.0
#USBLIBS = -L/usr/local/lib -lhid -lusb-1.0 -lsetupapi
#EXE_SUFFIX = .exe

CC= gcc
CFLAGS= -O -Wall $(USBFLAGS)
Expand Down
4 changes: 2 additions & 2 deletions examples/hid-data/commandline/Makefile.windows
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

include Makefile

USBFLAGS=
USBLIBS= -lhid -lsetupapi
USBFLAGS = -I/usr/local/mingw/include/libusb-1.0
USBLIBS = -L/usr/local/mingw/lib -lhid -lusb-1.0 -lsetupapi
EXE_SUFFIX= .exe
8 changes: 4 additions & 4 deletions examples/usbtool/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
# This Makefile has been tested on Mac OS X, Linux and Windows.

# Use the following 3 lines on Unix (uncomment the framework on Mac OS X):
USBFLAGS = `libusb-config --cflags`
USBLIBS = `libusb-config --libs`
USBFLAGS = `pkg-config --cflags libusb-1.0`
USBLIBS = `pkg-config --libs libusb-1.0`
EXE_SUFFIX =

# Use the following 3 lines on Windows and comment out the 3 above. You may
# have to change the include paths to where you installed libusb-win32
#USBFLAGS = -I/usr/local/include
#USBLIBS = -L/usr/local/lib -lusb
#USBFLAGS = -I/usr/local/include/libusb-1.0
#USBLIBS = -L/usr/local/lib -lusb-1.0
#EXE_SUFFIX = .exe

NAME = usbtool
Expand Down
4 changes: 2 additions & 2 deletions examples/usbtool/Makefile.windows
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

include Makefile

USBFLAGS = -I/usr/local/mingw/include
USBLIBS = -L/usr/local/mingw/lib -lusb
USBFLAGS = -I/usr/local/mingw/include/libusb-1.0
USBLIBS = -L/usr/local/mingw/lib -lusb-1.0
EXE_SUFFIX = .exe
39 changes: 21 additions & 18 deletions examples/usbtool/usbtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ On Windows use libusb-win32 from http://libusb-win32.sourceforge.net/.
#include <ctype.h>
#include <errno.h>

#include <usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */
#include <libusb.h> /* this is libusb, see http://libusb.sourceforge.net/ */
#include "opendevice.h" /* common code moved to separate module */

#define DEFAULT_USB_VID 0 /* any */
Expand Down Expand Up @@ -147,8 +147,9 @@ int i, numEntries;

int main(int argc, char **argv)
{
usb_dev_handle *handle = NULL;
libusb_device_handle *handle = NULL;
int opt, len, action, argcnt;
int r = 0;
char *myName = argv[0], *s, *rxBuffer = NULL;
FILE *fp;

Expand Down Expand Up @@ -260,7 +261,7 @@ FILE *fp;
if(argc > argcnt){
fprintf(stderr, "Warning: only %d arguments expected, rest ignored.\n", argcnt);
}
usb_init();
libusb_init(NULL);
if(usbOpenDevice(&handle, vendorID, vendorNamePattern, productID, productNamePattern, serialPattern, action == ACTION_LIST ? stdout : NULL, showWarnings ? stderr : NULL) != 0){
fprintf(stderr, "Could not find USB device with VID=0x%x PID=0x%x Vname=%s Pname=%s Serial=%s\n", vendorID, productID, vendorNamePattern, productNamePattern, serialPattern);
exit(1);
Expand All @@ -280,43 +281,45 @@ FILE *fp;
usbIndex = myAtoi(argv[6]);
requestType = ((usbDirection & 1) << 7) | ((usbType & 3) << 5) | (usbRecipient & 0x1f);
if(usbDirection){ /* IN transfer */
len = usb_control_msg(handle, requestType, usbRequest, usbValue, usbIndex, rxBuffer, usbCount, usbTimeout);
len = libusb_control_transfer(handle, requestType, usbRequest, usbValue, usbIndex, (unsigned char *)rxBuffer, usbCount, usbTimeout);
}else{ /* OUT transfer */
len = usb_control_msg(handle, requestType, usbRequest, usbValue, usbIndex, sendBytes, sendByteCount, usbTimeout);
len = libusb_control_transfer(handle, requestType, usbRequest, usbValue, usbIndex, (unsigned char *)sendBytes, sendByteCount, usbTimeout);
}
}else{ /* must be ACTION_INTERRUPT or ACTION_BULK */
int retries = 1;
if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
r = libusb_set_configuration(handle, usbConfiguration);
if(r < 0 && showWarnings){
fprintf(stderr, "Warning: could not set configuration: %s\n", libusb_strerror(r));
}
/* now try to claim the interface and detach the kernel HID driver on
* linux and other operating systems which support the call.
*/
while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
while((len = libusb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
r = libusb_detach_kernel_driver(handle, 0);
if(r != LIBUSB_SUCCESS && showWarnings){
fprintf(stderr, "Warning: could not detach kernel driver: %s\n", libusb_strerror(r));
}
#endif
}
if(len != 0 && showWarnings)
fprintf(stderr, "Warning: could not claim interface: %s\n", usb_strerror());
fprintf(stderr, "Warning: could not claim interface: %s\n", libusb_strerror(len));
if(action == ACTION_INTERRUPT){
if(usbDirection){ /* IN transfer */
len = usb_interrupt_read(handle, endpoint, rxBuffer, usbCount, usbTimeout);
r = libusb_interrupt_transfer(handle, endpoint, (unsigned char *)rxBuffer, usbCount, &len, usbTimeout);
}else{
len = usb_interrupt_write(handle, endpoint, sendBytes, sendByteCount, usbTimeout);
r = libusb_interrupt_transfer(handle, endpoint, (unsigned char *)sendBytes, sendByteCount, &len, usbTimeout);
}
}else{
if(usbDirection){ /* IN transfer */
len = usb_bulk_read(handle, endpoint, rxBuffer, usbCount, usbTimeout);
r = libusb_bulk_transfer(handle, endpoint, (unsigned char *)rxBuffer, usbCount, &len, usbTimeout);
}else{
len = usb_bulk_write(handle, endpoint, sendBytes, sendByteCount, usbTimeout);
r = libusb_bulk_transfer(handle, endpoint, (unsigned char *)sendBytes, sendByteCount, &len, usbTimeout);
}
}
}
if(len < 0){
fprintf(stderr, "USB error: %s\n", usb_strerror());
if(r != 0){
fprintf(stderr, "USB error: %s\n", libusb_strerror(r));
exit(1);
}
if(usbDirection == 0) /* OUT */
Expand Down Expand Up @@ -348,7 +351,7 @@ FILE *fp;
fprintf(fp, "\n");
}
}
usb_close(handle);
libusb_close(handle);
if(rxBuffer != NULL)
free(rxBuffer);
return 0;
Expand Down
Loading