From d5028a12ca539638a6ae5d923e7dba0e4f30a45a Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Mon, 11 Jan 2016 08:41:58 +0100 Subject: all: Switch to libusb-1.0 libusb 0.1 is deprecated. This updates 0xFFFF to use the newer libusb 1.0 API instead. --- src/Makefile | 2 +- src/cold-flash.c | 85 +++++++++++++++++--------------- src/disk.c | 14 ++++-- src/mkii.c | 29 +++++++---- src/nolo.c | 60 +++++++++++++---------- src/usb-device.c | 144 ++++++++++++++++++++++--------------------------------- src/usb-device.h | 4 +- 7 files changed, 170 insertions(+), 168 deletions(-) diff --git a/src/Makefile b/src/Makefile index 6b8ef08..6682436 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ HOST_CC = $(HOST_COMPILE)$(CC) CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 -LIBS += -lusb +LIBS += -lusb-1.0 DEPENDS = Makefile ../config.mk diff --git a/src/cold-flash.c b/src/cold-flash.c index eb2ca67..1c9d95b 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -21,10 +21,10 @@ #include #include #include -#include -#include "global.h" +#include +#include "global.h" #include "cold-flash.h" #include "image.h" #include "usb-device.h" @@ -168,37 +168,41 @@ struct xloader_msg xloader_msg_create(uint32_t type, struct image * image) { } -static int read_asic(usb_dev_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { +static int read_asic(libusb_device_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { - int ret; + int ret, transferred; printf("Waiting for ASIC ID...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)asic_buffer, size, READ_TIMEOUT); - if ( ret != asic_size ) + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)asic_buffer, size, &transferred, READ_TIMEOUT); + if ( ret < 0 ) + ERROR_RETURN("Cannot read ASIC ID", -1); + if ( transferred != asic_size ) ERROR_RETURN("Invalid size of ASIC ID", -1); return 0; } -static int send_2nd(usb_dev_handle * udev, struct image * image) { +static int send_2nd(libusb_device_handle * udev, struct image * image) { uint8_t buffer[1024]; uint32_t need, readed; - int ret; + int ret, transferred; printf("Sending OMAP peripheral boot message...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(omap_peripheral_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(omap_peripheral_msg) ) ERROR_RETURN("Sending OMAP peripheral boot message failed", -1); - printf("Sending 2nd X-Loader image size...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&image->size, 4, WRITE_TIMEOUT); SLEEP(5000); - if ( ret != 4 ) + + printf("Sending 2nd X-Loader image size...\n"); + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&image->size, 4, &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("Sending 2nd X-Loader image size failed", -1); + SLEEP(5000); + printf("Sending 2nd X-Loader image...\n"); printf_progressbar(0, image->size); image_seek(image, 0); @@ -210,35 +214,37 @@ static int send_2nd(usb_dev_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) + if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed", -1); - readed += ret; + if ( ret != transferred ) + PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed (incomplete bulk transfer)", -1); + readed += transferred; printf_progressbar(readed, image->size); } - SLEEP(50000); + SLEEP(50000); return 0; } -static int send_secondary(usb_dev_handle * udev, struct image * image) { +static int send_secondary(libusb_device_handle * udev, struct image * image) { struct xloader_msg init_msg; uint8_t buffer[1024]; uint32_t need, readed; - int ret; + int ret, transferred; init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, image); printf("Sending X-Loader init message...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(init_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&init_msg, sizeof(init_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(init_msg) ) ERROR_RETURN("Sending X-Loader init message failed", -1); printf("Waiting for X-Loader response...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret != 4 ) + SLEEP(5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); printf("Sending Secondary image...\n"); @@ -252,25 +258,27 @@ static int send_secondary(usb_dev_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) + if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending Secondary image failed", -1); - readed += ret; + if ( ret != transferred ) + PRINTF_ERROR_RETURN("Sending Secondary image failed (incomplete bulk transfer)", -1); + readed += transferred; printf_progressbar(readed, image->size); } - SLEEP(5000); printf("Waiting for X-Loader response...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret != 4 ) + SLEEP(5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); return 0; } -static int ping_timeout(usb_dev_handle * udev) { +static int ping_timeout(libusb_device_handle * udev) { - int ret; + int ret, transferred; int pong = 0; int try_ping = 10; @@ -280,16 +288,16 @@ static int ping_timeout(usb_dev_handle * udev) { int try_read = 4; printf("Sending X-Loader ping message\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&ping_msg, sizeof(ping_msg), WRITE_TIMEOUT); - if ( ret != sizeof(ping_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&ping_msg, sizeof(ping_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(ping_msg) ) ERROR_RETURN("Sending X-Loader ping message failed", -1); printf("Waiting for X-Loader pong response...\n"); while ( try_read > 0 ) { uint32_t ping_read; - ret = usb_bulk_read(udev, READ_DEV, (char *)&ping_read, sizeof(ping_read), READ_TIMEOUT); - if ( ret == sizeof(ping_read) ) { + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&ping_read, sizeof(ping_read), &transferred, READ_TIMEOUT); + if ( ret == 0 && transferred == sizeof(ping_read) ) { printf("Got it\n"); pong = 1; break; @@ -401,12 +409,11 @@ int cold_flash(struct usb_device_info * dev, struct image * x2nd, struct image * int leave_cold_flash(struct usb_device_info * dev) { - int ret; + int ret, transferred; printf("Sending OMAP memory boot message...\n"); - ret = usb_bulk_write(dev->udev, WRITE_DEV, (char *)&omap_memory_msg, sizeof(omap_memory_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(omap_memory_msg) ) + ret = libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char *)&omap_memory_msg, sizeof(omap_memory_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); SLEEP(250000); diff --git a/src/disk.c b/src/disk.c index daa3711..5988305 100644 --- a/src/disk.c +++ b/src/disk.c @@ -277,14 +277,20 @@ int disk_init(struct usb_device_info * dev) { unsigned int devnum; unsigned int busnum; - struct usb_device * device; + uint8_t usbdevnum; + uint8_t usbbusnum; - device = usb_device(dev->udev); - if ( ! device || ! device->bus ) { + struct libusb_device * device; + + device = libusb_get_device(dev->udev); + if ( ! device ) { ERROR_INFO("Cannot read usb devnum and busnum"); return -1; } + usbbusnum = libusb_get_bus_number(device); + usbdevnum = libusb_get_port_number(device); + dir = opendir("/sys/dev/block/"); if ( ! dir ) { ERROR_INFO("Cannot open '/sys/dev/block/' directory"); @@ -324,7 +330,7 @@ int disk_init(struct usb_device_info * dev) { fclose(f); - if ( devnum != device->devnum || device->bus->location != busnum ) + if ( devnum != usbdevnum || usbbusnum != busnum ) continue; if ( sscanf(dirent->d_name, "%d:%d", &maj, &min) != 2 ) { diff --git a/src/mkii.c b/src/mkii.c index e2fb23f..742c1da 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -29,6 +29,9 @@ #include "device.h" #include "usb-device.h" +#define READ_DEV 0x81 +#define WRITE_DEV 0x01 + #define MKII_OUT 0x8810001B #define MKII_IN 0x8800101B @@ -48,9 +51,9 @@ struct mkii_message { } __attribute__((__packed__)); -static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { +static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { - int ret; + int ret, transferred; static uint8_t number = 0; in_msg->header = MKII_OUT; @@ -59,15 +62,17 @@ static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_me in_msg->num = number++; in_msg->type = type; - ret = usb_bulk_write(udev, 1, (char *)in_msg, data_size + sizeof(*in_msg), 5000); + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)in_msg, data_size + sizeof(*in_msg), &transferred, 5000); if ( ret < 0 ) return ret; - if ( (size_t)ret != data_size + sizeof(*in_msg) ) + if ( (size_t)transferred != data_size + sizeof(*in_msg) ) return -1; - ret = usb_bulk_read(udev, 129, (char *)out_msg, out_size, 5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)out_msg, out_size, &transferred, 5000); if ( ret < 0 ) return ret; + if ( (size_t)transferred < sizeof(*out_msg) ) + return -1; if ( out_msg->header != MKII_IN ) return -1; @@ -75,13 +80,13 @@ static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_me if ( out_msg->type != (type | MKII_RESPONCE) ) return -1; - if ( (size_t)ret < sizeof(*out_msg) ) + if ( (size_t)transferred < sizeof(*out_msg) ) return -1; - if ( ntohs(out_msg->size) != ret - sizeof(*out_msg) + 4 ) + if ( ntohs(out_msg->size) != transferred - sizeof(*out_msg) + 4 ) return -1; - return ret - sizeof(*out_msg); + return transferred - sizeof(*out_msg); } @@ -94,6 +99,8 @@ int mkii_init(struct usb_device_info * dev) { char * newptr; char * ptr; enum image_type type; + struct libusb_device *udev; + struct libusb_config_descriptor *desc; printf("Initializing Mk II protocol...\n"); @@ -154,7 +161,11 @@ int mkii_init(struct usb_device_info * dev) { printf("\n"); memset(buf, 0, sizeof(buf)); - usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); + + udev = libusb_get_device(dev->udev); + ret = libusb_get_config_descriptor(udev, dev->flash_device->configuration, &desc); + if ( ret == 0 ) + libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= (1UL << 31); diff --git a/src/nolo.c b/src/nolo.c index 5e51a36..e94a652 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -22,13 +22,16 @@ #include #include -#include +#include #include "nolo.h" #include "image.h" #include "global.h" #include "printf-utils.h" +#define READ_DEV 0x81 +#define WRITE_DEV 0x01 + /* Request type */ #define NOLO_WRITE 64 #define NOLO_QUERY 192 @@ -90,7 +93,7 @@ static void nolo_error_log(struct usb_device_info * dev, int only_clear) { memset(buf, 0, sizeof(buf)); - ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, buf, sizeof(buf), 2000); + ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); if ( ret < 0 ) break; @@ -120,7 +123,7 @@ static int nolo_identify_string(struct usb_device_info * dev, const char * str, memset(buf, 0, sizeof(buf)); - ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (char *)buf, sizeof(buf), 2000); + ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); if ( ret < 0 ) NOLO_ERROR_RETURN("NOLO_IDENTIFY failed", -1); @@ -150,10 +153,10 @@ static int nolo_set_string(struct usb_device_info * dev, char * str, char * arg) if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_STRING failed", -1); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, arg, strlen(arg), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, (unsigned char*)arg, strlen(arg), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_STRING failed", -1); return 0; @@ -164,10 +167,10 @@ static int nolo_get_string(struct usb_device_info * dev, char * str, char * out, int ret = 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) return -1; - if ( ( ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, out, size-1, 2000) ) < 0 ) + if ( ( ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, (unsigned char*)out, size-1, 2000) ) < 0 ) return -1; if ( (size_t)ret > size-1 ) @@ -211,7 +214,7 @@ int nolo_init(struct usb_device_info * dev) { printf("Initializing NOLO...\n"); while ( val != 0 ) - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (char *)&val, 4, 2000) == -1 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (unsigned char *)&val, 4, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_STATUS failed", -1); /* clear error log */ @@ -256,6 +259,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i uint32_t readed; int request; int ret; + int transferred; if ( flash ) printf("Send and flash image:\n"); @@ -370,7 +374,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i printf("Sending image header...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, request, 0, 0, buf, ptr-buf, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, request, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("Sending image header failed", -1); } @@ -389,19 +393,23 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i if ( ret == 0 ) break; if ( ! simulate ) { - if ( usb_bulk_write(dev->udev, 2, buf, ret, 5000) != ret ) { + if ( libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char*)buf, ret, &transferred, 5000) < 0 ) { PRINTF_END(); NOLO_ERROR_RETURN("Sending image failed", -1); } + if ( transferred != ret ) { + PRINTF_END(); + NOLO_ERROR_RETURN("Sending image was incomplete!", -1); + } } - readed += ret; + readed += transferred; printf_progressbar(readed, image->size); } if ( flash ) { printf("Finishing flashing...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) NOLO_ERROR_RETURN("Finishing failed", -1); } } @@ -458,7 +466,7 @@ int nolo_flash_image(struct usb_device_info * dev, struct image * image) { printf("Flashing image...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) < 0 ) NOLO_ERROR_RETURN("Flashing failed", -1); } @@ -579,7 +587,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { cmdline = NULL; } - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (char *)cmdline, size, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (unsigned char *)cmdline, size, 2000) < 0 ) NOLO_ERROR_RETURN("Booting failed", -1); return 0; @@ -589,7 +597,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { int nolo_reboot_device(struct usb_device_info * dev) { printf("Rebooting device...\n"); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_REBOOT failed", -1); return 0; @@ -598,7 +606,7 @@ int nolo_reboot_device(struct usb_device_info * dev) { int nolo_get_root_device(struct usb_device_info * dev) { uint8_t device = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (char *)&device, 1, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (unsigned char *)&device, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get root device", -1); return device; @@ -609,7 +617,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { printf("Setting root device to %d...\n", device); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot set root device", -1); return 0; @@ -618,7 +626,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { int nolo_get_usb_host_mode(struct usb_device_info * dev) { uint32_t enabled = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (void *)&enabled, 4, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (unsigned char *)&enabled, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get USB host mode status", -1); return enabled ? 1 : 0; @@ -629,7 +637,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { printf("%s USB host mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change USB host mode status", -1); return 0; @@ -638,7 +646,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { int nolo_get_rd_mode(struct usb_device_info * dev) { uint8_t enabled = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (char *)&enabled, 1, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (unsigned char *)&enabled, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D mode status", -1); return enabled ? 1 : 0; @@ -649,7 +657,7 @@ int nolo_set_rd_mode(struct usb_device_info * dev, int enable) { printf("%s R&D mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change R&D mode status", -1); return 0; @@ -662,7 +670,7 @@ int nolo_get_rd_flags(struct usb_device_info * dev, char * flags, size_t size) { uint16_t add_flags = 0; char * ptr = flags; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (char *)&add_flags, 2, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (unsigned char *)&add_flags, 2, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D flags", -1); if ( add_flags & NOLO_RD_FLAG_NO_OMAP_WD ) @@ -762,10 +770,10 @@ int nolo_set_rd_flags(struct usb_device_info * dev, const char * flags) { if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot add R&D flags", -1); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot del R&D flags", -1); return 0; @@ -825,7 +833,7 @@ int nolo_get_nolo_ver(struct usb_device_info * dev, char * ver, size_t size) { uint32_t version = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (char *)&version, 4, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (unsigned char *)&version, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get NOLO version", -1); if ( (version & 255) > 1 ) @@ -884,7 +892,7 @@ int nolo_set_sw_ver(struct usb_device_info * dev, const char * ver) { memcpy(ptr, ver, len); ptr += len; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, buf, ptr-buf, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_SW_RELEASE failed", -1); return 0; diff --git a/src/usb-device.c b/src/usb-device.c index a4b1f8b..9f5c18d 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -25,11 +25,7 @@ #include #include -#include - -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP -#include -#endif +#include #include "global.h" #include "device.h" @@ -77,38 +73,31 @@ static void usb_flash_device_info_print(const struct usb_flash_device * dev) { } -static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) { - -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP - struct { - int ifno; - int ioctl_code; - void * data; - } command = { - .ifno = interface, - .ioctl_code = _IO('U', 23), - .data = NULL, - }; +static void usb_reattach_kernel_driver(libusb_device_handle * udev, int interface) { - if ( interface < 0 ) - return; - - usb_release_interface(udev, interface); - ioctl(*((int *)udev), _IOWR('U', 18, command), &command); -#endif + PRINTF_LINE("Reattach kernel driver to USB interface..."); + PRINTF_END(); + libusb_release_interface(udev, interface); + libusb_attach_kernel_driver(udev, interface); } -static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * dev, char * product, size_t size) { +static void usb_descriptor_info_print(libusb_device_handle * udev, struct libusb_device * dev, char * product, size_t size) { + struct libusb_device_descriptor desc; char buf[1024]; char buf2[1024]; unsigned int x; int ret; int i; + if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { + PRINTF_LINE("libusb_get_device_descriptor() failed"); + PRINTF_END(); + return; + } memset(buf, 0, sizeof(buf)); - usb_get_string_simple(udev, dev->descriptor.iProduct, buf, sizeof(buf)); + libusb_get_string_descriptor_ascii(udev, desc.iProduct, (unsigned char *)buf, sizeof(buf)); PRINTF_LINE("USB device product string: %s", buf[0] ? buf : "(not detected)"); PRINTF_END(); @@ -117,7 +106,7 @@ static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * memset(buf, 0, sizeof(buf)); memset(buf2, 0, sizeof(buf2)); - ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, buf, sizeof(buf)); + ret = libusb_get_string_descriptor_ascii(udev, desc.iSerialNumber, (unsigned char *)buf, sizeof(buf)); if ( ! isalnum(buf[0]) ) buf[0] = 0; for ( i = 0; i < ret; i+=2 ) { @@ -136,15 +125,23 @@ static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * } -static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { +static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) { - int i; + int err, i; char product[1024]; + libusb_device_handle * udev; struct usb_device_info * ret = NULL; + struct libusb_device_descriptor desc; + + if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { + PRINTF_LINE("libusb_get_device_descriptor failed"); + PRINTF_END(); + return NULL; + } for ( i = 0; usb_devices[i].vendor; ++i ) { - if ( dev->descriptor.idVendor == usb_devices[i].vendor && dev->descriptor.idProduct == usb_devices[i].product ) { + if ( desc.idVendor == usb_devices[i].vendor && desc.idProduct == usb_devices[i].product ) { printf("\b\b "); PRINTF_END(); @@ -153,9 +150,10 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { PRINTF_END(); PRINTF_LINE("Opening USB..."); - usb_dev_handle * udev = usb_open(dev); - if ( ! udev ) { - PRINTF_ERROR("usb_open failed"); + + err = libusb_open(dev, &udev); + if ( err < 0 ) { + PRINTF_ERROR("libusb_open failed"); fprintf(stderr, "\n"); return NULL; } @@ -164,17 +162,15 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( usb_devices[i].interface >= 0 ) { -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP PRINTF_LINE("Detaching kernel from USB interface..."); - usb_detach_kernel_driver_np(udev, usb_devices[i].interface); -#endif + libusb_detach_kernel_driver(udev, usb_devices[i].interface); PRINTF_LINE("Claiming USB interface..."); - if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) { - PRINTF_ERROR("usb_claim_interface failed"); + if ( libusb_claim_interface(udev, usb_devices[i].interface) < 0 ) { + PRINTF_ERROR("libusb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } @@ -182,22 +178,22 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( usb_devices[i].alternate >= 0 ) { PRINTF_LINE("Setting alternate USB interface..."); - if ( usb_set_altinterface(udev, usb_devices[i].alternate) < 0 ) { - PRINTF_ERROR("usb_claim_interface failed"); + if ( libusb_set_interface_alt_setting(udev, usb_devices[i].interface, usb_devices[i].alternate) < 0 ) { + PRINTF_ERROR("libusb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } } if ( usb_devices[i].configuration >= 0 ) { PRINTF_LINE("Setting USB configuration..."); - if ( usb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { - PRINTF_ERROR("usb_set_configuration failed"); + if ( libusb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { + PRINTF_ERROR("libusb_set_configuration failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } } @@ -206,7 +202,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( ! ret ) { ALLOC_ERROR(); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } @@ -232,7 +228,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { ERROR("Device mishmash"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); free(ret); return NULL; } @@ -249,28 +245,6 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { } -static struct usb_device_info * usb_search_device(struct usb_device * dev, int level) { - - int i; - struct usb_device_info * ret = NULL; - - if ( ! dev ) - return NULL; - - ret = usb_device_is_valid(dev); - if ( ret ) - return ret; - - for ( i = 0; i < dev->num_children; i++ ) { - ret = usb_search_device(dev->children[i], level + 1); - if ( ret ) - break; - } - - return ret; - -} - static volatile sig_atomic_t signal_quit; static void signal_handler(int signum) { @@ -282,14 +256,18 @@ static void signal_handler(int signum) { struct usb_device_info * usb_open_and_wait_for_device(void) { - struct usb_bus * bus; + libusb_device **devs; + libusb_device **dev; struct usb_device_info * ret = NULL; int i = 0; void (*prev)(int); static char progress[] = {'/','-','\\', '|'}; - usb_init(); - usb_find_busses(); + if ( libusb_init(NULL) < 0 ) { + PRINTF_LINE("libusb_init failed"); + PRINTF_END(); + return NULL; + } PRINTF_BACK(); printf("\n"); @@ -302,24 +280,16 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]); - usb_find_devices(); - - for ( bus = usb_get_busses(); bus; bus = bus->next ) { - - if ( bus->root_dev ) - ret = usb_search_device(bus->root_dev, 0); - else { - struct usb_device *dev; - for ( dev = bus->devices; dev; dev = dev->next ) { - ret = usb_search_device(dev, 0); - if ( ret ) - break; - } - } + if ( libusb_get_device_list(NULL, &devs) < 0 ) { + PRINTF_LINE("Listing USB devices failed"); + PRINTF_END(); + break; + } + for ( dev = devs; *dev != NULL; ++dev ) { + ret = usb_device_is_valid(*dev); if ( ret ) break; - } if ( ret ) @@ -345,7 +315,7 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { void usb_close_device(struct usb_device_info * dev) { usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); - usb_close(dev->udev); + libusb_close(dev->udev); free(dev); } diff --git a/src/usb-device.h b/src/usb-device.h index 850c550..9b2bcc4 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -20,7 +20,7 @@ #ifndef USB_DEVICE_H #define USB_DEVICE_H -#include +#include #include "device.h" @@ -47,7 +47,7 @@ struct usb_device_info { enum device device; int16_t hwrev; const struct usb_flash_device * flash_device; - usb_dev_handle * udev; + libusb_device_handle * udev; int data; }; -- cgit v1.2.3