Skip to content

Commit

Permalink
Merge pull request #6 from xairy/main
Browse files Browse the repository at this point in the history
Support devices with multiple interfaces
  • Loading branch information
AristoChen authored May 25, 2023
2 parents 6aecd45 + 8f254c8 commit b0ccd74
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 206 deletions.
42 changes: 36 additions & 6 deletions device-libusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ int connect_device(int vendor_id, int product_id) {
return result;
}

int config = 0;
result = libusb_get_configuration(dev_handle, &config);
if (result != LIBUSB_SUCCESS) {
fprintf(stderr, "libusb_get_configuration() failed: %s\n",
libusb_strerror((libusb_error)result));
return result;
}

for (int i = 0; i < device_device_desc.bNumConfigurations; i++) {
if (device_config_desc[i]->bConfigurationValue != config)
continue;
for (int j = 0; j < device_config_desc[i]->bNumInterfaces; j++)
libusb_detach_kernel_driver(dev_handle, j);
}

result = libusb_reset_device(dev_handle);
if (result != LIBUSB_SUCCESS) {
fprintf(stderr, "libusb_reset_device() failed: %s\n",
libusb_strerror((libusb_error)result));
return result;
}

//check that device is responsive
unsigned char unused[4];
result = libusb_get_string_descriptor(dev_handle, 0, 0, unused, sizeof(unused));
Expand Down Expand Up @@ -164,19 +186,27 @@ void set_configuration(int configuration) {
}
}

void claim_interface(uint8_t interface) {
void claim_interface(int interface) {
int result = libusb_claim_interface(dev_handle, interface);
if (result != LIBUSB_SUCCESS) {
fprintf(stderr, "Error claiming interface(%d): %s\n",
(unsigned)interface, libusb_strerror((libusb_error)result));
interface, libusb_strerror((libusb_error)result));
}
}

void release_interface(uint8_t interface) {
void release_interface(int interface) {
int result = libusb_release_interface(dev_handle, interface);
if (result != LIBUSB_SUCCESS && result != LIBUSB_ERROR_NOT_FOUND) {
fprintf(stderr, "Error releasing interface(%d): %s\n",
(unsigned)interface, libusb_strerror((libusb_error)result));
interface, libusb_strerror((libusb_error)result));
}
}

void set_interface_alt_setting(int interface, int altsetting) {
int result = libusb_set_interface_alt_setting(dev_handle, interface, altsetting);
if (result != LIBUSB_SUCCESS) {
fprintf(stderr, "Error setting interface altsetting(%d, %d): %s\n",
interface, altsetting, libusb_strerror((libusb_error)result));
}
}

Expand Down Expand Up @@ -274,7 +304,7 @@ void receive_data(uint8_t endpoint, uint8_t attributes, uint16_t maxPacketSize,
fprintf(stderr, "Isochronous(read) endpoint EP%02x unhandled.\n", endpoint);
break;
case USB_ENDPOINT_XFER_BULK:
*dataptr = (uint8_t *) malloc(maxPacketSize * 8);
*dataptr = new uint8_t[maxPacketSize * 8];
do {
result = libusb_bulk_transfer(dev_handle, endpoint, *dataptr, maxPacketSize, length, timeout);
if (result == LIBUSB_SUCCESS && verbose_level > 2)
Expand All @@ -286,7 +316,7 @@ void receive_data(uint8_t endpoint, uint8_t attributes, uint16_t maxPacketSize,
} while ((result == LIBUSB_ERROR_PIPE || result == LIBUSB_ERROR_TIMEOUT) && attempt < MAX_ATTEMPTS);
break;
case USB_ENDPOINT_XFER_INT:
*dataptr = (uint8_t *) malloc(maxPacketSize);
*dataptr = new uint8_t[maxPacketSize];
result = libusb_interrupt_transfer(dev_handle, endpoint, *dataptr, maxPacketSize, length, timeout);
if (result == LIBUSB_SUCCESS && verbose_level > 2)
printf("Received int data(%d) bytes\n", *length);
Expand Down
5 changes: 3 additions & 2 deletions device-libusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ extern pthread_t hotplug_monitor_thread;

int connect_device(int vendorId, int productId);
void set_configuration(int configuration);
void claim_interface(uint8_t interface);
void release_interface(uint8_t interface);
void claim_interface(int interface);
void release_interface(int interface);
void set_interface_alt_setting(int interface, int altsetting);
int control_request(const usb_ctrlrequest *setup_packet, int *nbytes,
unsigned char **dataptr, int timeout);
void send_data(uint8_t endpoint, uint8_t attributes, uint8_t *dataptr,
Expand Down
5 changes: 1 addition & 4 deletions host-raw-gadget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

#include "host-raw-gadget.h"

struct usb_device_descriptor host_device_desc;
struct raw_gadget_config_descriptor *host_config_desc;

struct endpoint_thread *ep_thread_list;
struct raw_gadget_device host_device_desc;

/*----------------------------------------------------------------------*/

Expand Down
55 changes: 29 additions & 26 deletions host-raw-gadget.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,6 @@ struct usb_raw_eps_info {

/*----------------------------------------------------------------------*/

struct raw_gadget_interface_descriptor {
struct usb_interface_descriptor interface;
struct usb_endpoint_descriptor *endpoints;
};

struct raw_gadget_interface {
struct raw_gadget_interface_descriptor *altsetting;
int num_altsetting;
};

struct raw_gadget_config_descriptor {
struct usb_config_descriptor config;
struct raw_gadget_interface *interfaces;
};

extern struct usb_device_descriptor host_device_desc;
extern struct raw_gadget_config_descriptor *host_config_desc;

/*----------------------------------------------------------------------*/

#define EP_MAX_PACKET_CONTROL 1024
#define EP_MAX_PACKET_BULK 1024
#define EP_MAX_PACKET_INT 8
Expand Down Expand Up @@ -135,21 +115,44 @@ struct usb_raw_transfer_io {

struct thread_info {
int fd;
int ep_num = -1;
int ep_num;
struct usb_endpoint_descriptor endpoint;
std::string transfer_type;
std::string dir;
std::deque<usb_raw_transfer_io> *data_queue;
std::mutex *data_mutex;
};

struct endpoint_thread {
pthread_t ep_thread_read;
pthread_t ep_thread_write;
struct thread_info ep_thread_info;
struct raw_gadget_endpoint {
struct usb_endpoint_descriptor endpoint;
pthread_t thread_read;
pthread_t thread_write;
struct thread_info thread_info;
};

struct raw_gadget_altsetting {
struct usb_interface_descriptor interface;
struct raw_gadget_endpoint *endpoints;
};

struct raw_gadget_interface {
struct raw_gadget_altsetting *altsettings;
int num_altsettings;
int current_altsetting;
};

struct raw_gadget_config {
struct usb_config_descriptor config;
struct raw_gadget_interface *interfaces;
};

struct raw_gadget_device {
struct usb_device_descriptor device;
struct raw_gadget_config *configs;
int current_config;
};

extern endpoint_thread *ep_thread_list;
extern struct raw_gadget_device host_device_desc;

/*----------------------------------------------------------------------*/

Expand Down
4 changes: 0 additions & 4 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ extern int verbose_level;
extern bool please_stop_ep0;
extern bool please_stop_eps;

extern int desired_configuration;
extern int desired_interface;
extern int desired_interface_altsetting;

extern bool injection_enabled;
extern std::string injection_file;
extern Json::Value injection_config;
Expand Down
Loading

0 comments on commit b0ccd74

Please sign in to comment.