diff --git a/Makefile b/Makefile index 66033e3..9918e36 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ # CROSS ?= +USB ?= libusb AS := $(CROSS)gcc -x assembler-with-cpp CC := $(CROSS)gcc @@ -23,10 +24,15 @@ ODFLAGS := MCFLAGS := LIBDIRS := -LIBS := `pkg-config --libs libusb-1.0` - -INCDIRS := -I . `pkg-config --cflags libusb-1.0` -SRCDIRS := . chips +ifeq ($(USB), winusb) +CFLAGS += -D_WIN32 -DUSE_WINUSB_DRV +LIBS := -lsetupapi -lhid -lwinusb +INCDIRS := -I . +else +LIBS := -lusb-1.0 +INCDIRS := -I . -I /usr/include/libusb-1.0 +endif +SRCDIRS := . chips usb SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S)) CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)) diff --git a/Makefile.win b/Makefile.win index f6d8fe8..b531a5f 100644 --- a/Makefile.win +++ b/Makefile.win @@ -3,6 +3,7 @@ # CROSS ?= x86_64-w64-mingw32- +USB ?= libusb AS := $(CROSS)gcc -x assembler-with-cpp CC := $(CROSS)gcc @@ -23,10 +24,15 @@ ODFLAGS := MCFLAGS := LIBDIRS := -LIBS := -L/usr/x86_64-w64-mingw32/lib -lusb-1.0 - -INCDIRS := -I . -I /usr/x86_64-w64-mingw32/include/libusb-1.0 -SRCDIRS := . chips +ifeq ($(USB), winusb) +CFLAGS += -D_WIN32 -DUSE_WINUSB_DRV +LIBS := -lsetupapi -lhid -lwinusb +INCDIRS := -I . +else +LIBS := -lusb-1.0 +INCDIRS := -I . -I /usr/include/libusb-1.0 +endif +SRCDIRS := . chips usb SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S)) CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)) diff --git a/chips/t153.c b/chips/t153.c new file mode 100644 index 0000000..2aa3cdd --- /dev/null +++ b/chips/t153.c @@ -0,0 +1,94 @@ +#include + + +static int chip_detect(struct xfel_ctx_t * ctx, uint32_t id) +{ + if(id == 0x00192200) + { + return 1; + } + return 0; +} + +static uint32_t payload_read32(struct xfel_ctx_t * ctx, uint32_t addr) +{ + static const uint8_t payload[] = { + 0x00, 0x00, 0xa0, 0xe3, 0x17, 0x0f, 0x08, 0xee, 0x15, 0x0f, 0x07, 0xee, + 0xd5, 0x0f, 0x07, 0xee, 0x9a, 0x0f, 0x07, 0xee, 0x95, 0x0f, 0x07, 0xee, + 0xff, 0xff, 0xff, 0xea, 0x0c, 0x00, 0x9f, 0xe5, 0x0c, 0x10, 0x8f, 0xe2, + 0x00, 0x20, 0x90, 0xe5, 0x00, 0x20, 0x81, 0xe5, 0x1e, 0xff, 0x2f, 0xe1, + }; + uint32_t adr = cpu_to_le32(addr); + uint32_t val; + + fel_write(ctx, ctx->version.scratchpad, (void *)payload, sizeof(payload)); + fel_write(ctx, ctx->version.scratchpad + sizeof(payload), (void *)&adr, sizeof(adr)); + fel_exec(ctx, ctx->version.scratchpad); + fel_read(ctx, ctx->version.scratchpad + sizeof(payload) + sizeof(adr), (void *)&val, sizeof(val)); + return le32_to_cpu(val); +} + +static void payload_write32(struct xfel_ctx_t * ctx, uint32_t addr, uint32_t val) +{ + static const uint8_t payload[] = { + 0x00, 0x00, 0xa0, 0xe3, 0x17, 0x0f, 0x08, 0xee, 0x15, 0x0f, 0x07, 0xee, + 0xd5, 0x0f, 0x07, 0xee, 0x9a, 0x0f, 0x07, 0xee, 0x95, 0x0f, 0x07, 0xee, + 0xff, 0xff, 0xff, 0xea, 0x08, 0x00, 0x9f, 0xe5, 0x08, 0x10, 0x9f, 0xe5, + 0x00, 0x10, 0x80, 0xe5, 0x1e, 0xff, 0x2f, 0xe1, + }; + uint32_t params[2] = { + cpu_to_le32(addr), + cpu_to_le32(val), + }; + + fel_write(ctx, ctx->version.scratchpad, (void *)payload, sizeof(payload)); + fel_write(ctx, ctx->version.scratchpad + sizeof(payload), (void *)params, sizeof(params)); + fel_exec(ctx, ctx->version.scratchpad); +} + +static int chip_reset(struct xfel_ctx_t * ctx) +{ + return 0; +} + +static int chip_sid(struct xfel_ctx_t * ctx, char * sid) +{ + return 0; +} + +static int chip_jtag(struct xfel_ctx_t * ctx) +{ + return 0; +} + +static int chip_ddr(struct xfel_ctx_t * ctx, const char * type) +{ + return 0; +} + +static int chip_spi_init(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen) +{ + return 0; +} + +static int chip_spi_run(struct xfel_ctx_t * ctx, uint8_t * cbuf, uint32_t clen) +{ + return 0; +} + +static int chip_extra(struct xfel_ctx_t * ctx, int argc, char * argv[]) +{ + return 0; +} + +struct chip_t t153 = { + .name = "T153", + .detect = chip_detect, + .reset = chip_reset, + .sid = chip_sid, + .jtag = chip_jtag, + .ddr = chip_ddr, + .spi_init = chip_spi_init, + .spi_run = chip_spi_run, + .extra = chip_extra, +}; diff --git a/fel.c b/fel.c index 147b395..bc0e391 100644 --- a/fel.c +++ b/fel.c @@ -1,4 +1,5 @@ #include +#include extern struct chip_t a10; extern struct chip_t a13_a10s_r8; @@ -32,6 +33,7 @@ extern struct chip_t a733; extern struct chip_t t536; extern struct chip_t a537_a333; extern struct chip_t h135; +extern struct chip_t t153; static struct chip_t * chips[] = { @@ -67,17 +69,9 @@ static struct chip_t * chips[] = { &t536, &a537_a333, &h135, + &t153, }; -struct usb_request_t { - char magic[8]; - uint32_t length; - uint32_t unknown1; - uint16_t request; - uint32_t length2; - char pad[10]; -} __attribute__((packed)); - struct fel_request_t { uint32_t request; uint32_t address; @@ -85,77 +79,6 @@ struct fel_request_t { uint32_t pad; } __attribute__((packed)); -static inline void usb_bulk_send(libusb_device_handle * hdl, int ep, const char * buf, size_t len) -{ - size_t max_chunk = 128 * 1024; - size_t chunk; - int r, bytes; - - while(len > 0) - { - chunk = len < max_chunk ? len : max_chunk; - r = libusb_bulk_transfer(hdl, ep, (void *)buf, chunk, &bytes, 10000); - if(r != 0) - { - printf("usb bulk send error\r\n"); - exit(-1); - } - len -= bytes; - buf += bytes; - } -} - -static inline void usb_bulk_recv(libusb_device_handle * hdl, int ep, char * buf, size_t len) -{ - int r, bytes; - - while(len > 0) - { - r = libusb_bulk_transfer(hdl, ep, (void *)buf, len, &bytes, 10000); - if(r != 0) - { - printf("usb bulk recv error\r\n"); - exit(-1); - } - len -= bytes; - buf += bytes; - } -} - -static inline void send_usb_request(struct xfel_ctx_t * ctx, int type, size_t length) -{ - struct usb_request_t req = { - .magic = "AWUC", - .request = cpu_to_le16(type), - .length = cpu_to_le32(length), - .unknown1 = cpu_to_le32(0x0c000000) - }; - req.length2 = req.length; - usb_bulk_send(ctx->hdl, ctx->epout, (const char *)&req, sizeof(struct usb_request_t)); -} - -static inline void read_usb_response(struct xfel_ctx_t * ctx) -{ - char buf[13]; - - usb_bulk_recv(ctx->hdl, ctx->epin, (char *)buf, sizeof(buf)); - assert(strcmp(buf, "AWUS") == 0); -} - -static inline void usb_write(struct xfel_ctx_t * ctx, const void * buf, size_t len) -{ - send_usb_request(ctx, 0x12, len); - usb_bulk_send(ctx->hdl, ctx->epout, (const char *)buf, len); - read_usb_response(ctx); -} - -static inline void usb_read(struct xfel_ctx_t * ctx, const void * data, size_t len) -{ - send_usb_request(ctx, 0x11, len); - usb_bulk_send(ctx->hdl, ctx->epin, (const char *)data, len); - read_usb_response(ctx); -} - static inline void send_fel_request(struct xfel_ctx_t * ctx, int type, uint32_t addr, uint32_t length) { struct fel_request_t req = { @@ -195,43 +118,9 @@ static inline int fel_version(struct xfel_ctx_t * ctx) int fel_init(struct xfel_ctx_t * ctx) { - if(ctx && ctx->hdl) + if(usb_init(ctx)) { - struct libusb_config_descriptor * config; - int if_idx, set_idx, ep_idx; - const struct libusb_interface * iface; - const struct libusb_interface_descriptor * setting; - const struct libusb_endpoint_descriptor * ep; - - if(libusb_kernel_driver_active(ctx->hdl, 0)) - libusb_detach_kernel_driver(ctx->hdl, 0); - - if(libusb_claim_interface(ctx->hdl, 0) == 0) - { - if(libusb_get_active_config_descriptor(libusb_get_device(ctx->hdl), &config) == 0) - { - for(if_idx = 0; if_idx < config->bNumInterfaces; if_idx++) - { - iface = config->interface + if_idx; - for(set_idx = 0; set_idx < iface->num_altsetting; set_idx++) - { - setting = iface->altsetting + set_idx; - for(ep_idx = 0; ep_idx < setting->bNumEndpoints; ep_idx++) - { - ep = setting->endpoint + ep_idx; - if((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) != LIBUSB_TRANSFER_TYPE_BULK) - continue; - if((ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) - ctx->epin = ep->bEndpointAddress; - else - ctx->epout = ep->bEndpointAddress; - } - } - } - libusb_free_config_descriptor(config); - return fel_version(ctx); - } - } + return fel_version(ctx); } return 0; } diff --git a/fel.h b/fel.h index 8a0ffec..03dc58e 100644 --- a/fel.h +++ b/fel.h @@ -13,7 +13,8 @@ struct xfel_ctx_t; struct chip_t; struct xfel_ctx_t { - libusb_device_handle * hdl; + void *hdl; + char *dev_name; int epout; int epin; struct { diff --git a/main.c b/main.c index 8890386..c6e8e29 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include static void usage(void) { @@ -52,30 +52,16 @@ int main(int argc, char * argv[]) } } - libusb_device ** list = NULL; - libusb_context * context = NULL; - libusb_init(&context); - int count = libusb_get_device_list(context, &list); - for(int i = 0; i < count; i++) + if(!usb_open(&ctx)) { - libusb_device * device = list[i]; - struct libusb_device_descriptor desc; - if(libusb_get_device_descriptor(device, &desc) != 0) - printf("ERROR: Can't get device list\r\n"); - if((desc.idVendor == 0x1f3a) && (desc.idProduct == 0xefe8)) - { - if(libusb_open(device, &ctx.hdl) != 0) - printf("ERROR: Can't connect to device\r\n"); - break; - } + printf("ERROR: Can't connect to device\r\n"); + return -1; } if(!fel_init(&ctx)) { printf("ERROR: No FEL device found!\r\n"); - if(ctx.hdl) - libusb_close(ctx.hdl); - libusb_exit(NULL); + usb_close(&ctx); return -1; } if(!strcmp(argv[1], "version")) @@ -433,9 +419,7 @@ int main(int argc, char * argv[]) } else usage(); - if(ctx.hdl) - libusb_close(ctx.hdl); - libusb_exit(NULL); + usb_close(&ctx); return 0; } diff --git a/usb.c b/usb.c new file mode 100644 index 0000000..40772ca --- /dev/null +++ b/usb.c @@ -0,0 +1,52 @@ +#include +#include +#include + +static inline void send_usb_request(struct xfel_ctx_t * ctx, int type, size_t length) +{ + struct usb_request_t req = { + .magic = "AWUC", + .request = cpu_to_le16(type), + .length = cpu_to_le32(length), + .unknown1 = cpu_to_le32(0x0c000000) + }; + req.length2 = req.length; + usb_ops->bulk_send(ctx->hdl, ctx->epout, (const char *)&req, sizeof(struct usb_request_t)); +} + +static inline void read_usb_response(struct xfel_ctx_t * ctx) +{ + char buf[13]; + + usb_ops->bulk_recv(ctx->hdl, ctx->epin, (char *)buf, sizeof(buf)); + assert(strcmp(buf, "AWUS") == 0); +} + +void usb_write(struct xfel_ctx_t * ctx, const void * buf, size_t len) +{ + send_usb_request(ctx, 0x12, len); + usb_ops->bulk_send(ctx->hdl, ctx->epout, (const char *)buf, len); + read_usb_response(ctx); +} + +void usb_read(struct xfel_ctx_t * ctx, const void * data, size_t len) +{ + send_usb_request(ctx, 0x11, len); + usb_ops->bulk_recv(ctx->hdl, ctx->epin, (char *)data, len); + read_usb_response(ctx); +} + +int usb_open(struct xfel_ctx_t * ctx) +{ + return usb_ops->open(ctx); +} + +void usb_close(struct xfel_ctx_t * ctx) +{ + usb_ops->close(ctx); +} + +int usb_init(struct xfel_ctx_t * ctx) +{ + return usb_ops->init(ctx); +} diff --git a/usb.h b/usb.h new file mode 100644 index 0000000..8c8c812 --- /dev/null +++ b/usb.h @@ -0,0 +1,41 @@ +#ifndef __USB_H__ +#define __USB_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct xfel_ctx_t; + +struct usb_request_t { + char magic[8]; + uint32_t length; + uint32_t unknown1; + uint16_t request; + uint32_t length2; + char pad[10]; +} __attribute__((packed)); + +struct usb_ops_t { + void (*bulk_send)(void * hdl, int ep, const char * buf, size_t len); + void (*bulk_recv)(void * hdl, int ep, char * buf, size_t len); + int (*open)(struct xfel_ctx_t * ctx); + void (*close)(struct xfel_ctx_t * ctx); + int (*init)(struct xfel_ctx_t * ctx); +}; + +extern struct usb_ops_t * usb_ops; + +void usb_write(struct xfel_ctx_t * ctx, const void * buf, size_t len); +void usb_read(struct xfel_ctx_t * ctx, const void * data, size_t len); +int usb_init(struct xfel_ctx_t * ctx); +int usb_open(struct xfel_ctx_t * ctx); +void usb_close(struct xfel_ctx_t * ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_H__ */ diff --git a/usb/libusb.c b/usb/libusb.c new file mode 100644 index 0000000..7f7f5a3 --- /dev/null +++ b/usb/libusb.c @@ -0,0 +1,130 @@ +#include +#include +#include + +#if !defined(USE_WINUSB_DRV) +#include + +static void xfel_libusb_bulk_send(void * hdl, int ep, const char * buf, size_t len) +{ + libusb_device_handle *handle = (libusb_device_handle *) hdl; + size_t max_chunk = 128 * 1024; + size_t chunk; + int r, bytes; + + while(len > 0) + { + chunk = len < max_chunk ? len : max_chunk; + r = libusb_bulk_transfer(handle, ep, (void *)buf, chunk, &bytes, 10000); + if(r != 0) + { + printf("usb bulk send error\r\n"); + exit(-1); + } + len -= bytes; + buf += bytes; + } +} + +static void xfel_libusb_bulk_recv(void * hdl, int ep, char * buf, size_t len) +{ + libusb_device_handle *handle = (libusb_device_handle *) hdl; + int r, bytes; + + while(len > 0) + { + r = libusb_bulk_transfer(handle, ep, (void *)buf, len, &bytes, 10000); + if(r != 0) + { + printf("usb bulk recv error\r\n"); + exit(-1); + } + len -= bytes; + buf += bytes; + } +} + +static int xfel_libusb_open(struct xfel_ctx_t * ctx) +{ + libusb_device ** list = NULL; + libusb_context * context = NULL; + libusb_init(&context); + int count = libusb_get_device_list(context, &list); + for(int i = 0; i < count; i++) + { + libusb_device * device = list[i]; + struct libusb_device_descriptor desc; + if(libusb_get_device_descriptor(device, &desc) != 0) + printf("ERROR: Can't get device list\r\n"); + if((desc.idVendor == 0x1f3a) && (desc.idProduct == 0xefe8)) + { + libusb_device_handle *libusb_hdl = NULL; + if(libusb_open(device, &libusb_hdl) != 0) + printf("ERROR: Can't connect to device\r\n"); + ctx->hdl = (void *) libusb_hdl; + break; + } + } + libusb_free_device_list(list, 1); + return ctx->hdl ? 1 : 0; +} + +static void xfel_libusb_close(struct xfel_ctx_t * ctx) +{ + if(ctx->hdl) + libusb_close(ctx->hdl); + libusb_exit(NULL); +} + +static int xfel_libusb_init(struct xfel_ctx_t * ctx) +{ + if(ctx && ctx->hdl) + { + struct libusb_config_descriptor * config; + int if_idx, set_idx, ep_idx; + const struct libusb_interface * iface; + const struct libusb_interface_descriptor * setting; + const struct libusb_endpoint_descriptor * ep; + + if(libusb_kernel_driver_active(ctx->hdl, 0)) + libusb_detach_kernel_driver(ctx->hdl, 0); + + if(libusb_claim_interface(ctx->hdl, 0) == 0) + { + if(libusb_get_active_config_descriptor(libusb_get_device(ctx->hdl), &config) == 0) + { + for(if_idx = 0; if_idx < config->bNumInterfaces; if_idx++) + { + iface = config->interface + if_idx; + for(set_idx = 0; set_idx < iface->num_altsetting; set_idx++) + { + setting = iface->altsetting + set_idx; + for(ep_idx = 0; ep_idx < setting->bNumEndpoints; ep_idx++) + { + ep = setting->endpoint + ep_idx; + if((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) != LIBUSB_TRANSFER_TYPE_BULK) + continue; + if((ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) + ctx->epin = ep->bEndpointAddress; + else + ctx->epout = ep->bEndpointAddress; + } + } + } + libusb_free_config_descriptor(config); + return 1; + } + } + } + return 0; +} + +struct usb_ops_t xfel_libusb_ops = { + .bulk_send = xfel_libusb_bulk_send, + .bulk_recv = xfel_libusb_bulk_recv, + .open = xfel_libusb_open, + .close = xfel_libusb_close, + .init = xfel_libusb_init, +}; + +#endif /* !USE_WINUSB_DRV */ \ No newline at end of file diff --git a/usb/usbops.c b/usb/usbops.c new file mode 100644 index 0000000..6bd10bc --- /dev/null +++ b/usb/usbops.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#if defined(USE_WINUSB_DRV) && defined(_WIN32) +extern struct usb_ops_t xfel_winusb_ops; +struct usb_ops_t * usb_ops = &xfel_winusb_ops; +#else // defined(USE_WINUSB_DRV) && defined(_WIN32) +extern struct usb_ops_t xfel_libusb_ops; +struct usb_ops_t * usb_ops = &xfel_libusb_ops; +#endif // defined(USE_WINUSB_DRV) && defined(_WIN32) diff --git a/usb/winusb.c b/usb/winusb.c new file mode 100644 index 0000000..e53b303 --- /dev/null +++ b/usb/winusb.c @@ -0,0 +1,181 @@ +#include +#include +#include + +#if defined(USE_WINUSB_DRV) && defined(_WIN32) +#include +#include +#include +#include + +static int match_vid_pid(const char *device_path) { + if (!device_path) + return 0; + char pattern[64]; + snprintf(pattern, sizeof(pattern), "vid_%04x&pid_%04x", 0x1f3a, 0xefe8); + const size_t n = strlen(device_path); + + char *lower = (char *) malloc(n + 1); + + if (!lower) + return 0; + for (size_t i = 0; i < n; ++i) + lower[i] = (char) tolower((unsigned char) device_path[i]); + + lower[n] = '\0'; + + const int found = strstr(lower, pattern) != NULL; + free(lower); + return found; +} + +static void xfel_winusb_bulk_send(void * hdl, int ep, const char * buf, size_t len) +{ + HANDLE const usb_handle = (HANDLE) hdl; + const size_t max_chunk = 128 * 1024; + DWORD bytes_sent = 0; + + char *tmp_buf = (char *)malloc(max_chunk); + if (!tmp_buf) { + printf("usb bulk send malloc error\r\n"); + exit(-1); + } + + while (len > 0) { + const size_t chunk = len <= max_chunk ? len : max_chunk; + memcpy(tmp_buf, buf, chunk); + const DWORD dwIoControlCode = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0807, METHOD_OUT_DIRECT, FILE_ANY_ACCESS); + BOOL const result = + DeviceIoControl(usb_handle, dwIoControlCode, NULL, (DWORD) 0, (LPVOID) tmp_buf, chunk, &bytes_sent, NULL); + if (!result || bytes_sent == 0) { + free(tmp_buf); + printf("usb bulk send error\r\n"); + exit(-1); + } + len -= bytes_sent; + buf += bytes_sent; + } + free(tmp_buf); +} + +static void xfel_winusb_bulk_recv(void * hdl, int ep, char * buf, size_t len) +{ + HANDLE const usb_handle = (HANDLE) hdl; + DWORD bytes_received = 0; + + char *tmp_buf = (char *)malloc(len); + if (!tmp_buf) { + printf("usb bulk recv malloc error\r\n"); + exit(-1); + } + + const DWORD dwIoControlCode = CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0808, METHOD_IN_DIRECT, FILE_ANY_ACCESS); + + const BOOL result = + DeviceIoControl(usb_handle, dwIoControlCode, NULL, 0, (LPVOID) tmp_buf, (DWORD) len, &bytes_received, NULL); + + if (!result || bytes_received == 0) { + free(tmp_buf); + printf("usb bulk recv error\r\n"); + exit(-1); + } + + memcpy(buf, tmp_buf, bytes_received); + free(tmp_buf); +} + +static int xfel_winusb_open(struct xfel_ctx_t * ctx) +{ + SP_DEVICE_INTERFACE_DATA interface_data; + ULONG index = 0; + BOOL device_found = FALSE; + + const LPGUID usb_device_guid = (LPGUID) &GUID_DEVINTERFACE_USB_DEVICE; + + const HDEVINFO device_info_set = + SetupDiGetClassDevs(usb_device_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); + + if (device_info_set == INVALID_HANDLE_VALUE) { + printf("usb winusb open error\r\n"); + return 0; + } + + BOOL result = TRUE; + + while (result) { + interface_data.cbSize = sizeof(interface_data); + result = SetupDiEnumDeviceInterfaces(device_info_set, NULL, usb_device_guid, (ULONG) index, &interface_data); + + if (result) { + DWORD required_size = 0; + SetupDiGetDeviceInterfaceDetail(device_info_set, &interface_data, NULL, 0, &required_size, NULL); + if (required_size == 0) { + index++; + continue; + } + + const PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = + (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(required_size); + if (detail_data == NULL) { + SetupDiDestroyDeviceInfoList(device_info_set); + return 0; + } + detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); + + if (SetupDiGetDeviceInterfaceDetail(device_info_set, &interface_data, detail_data, required_size, NULL, + NULL)) { + if (match_vid_pid(detail_data->DevicePath)) { + ctx->dev_name = (char *) malloc(strlen(detail_data->DevicePath) + 1); + if (ctx->dev_name != NULL) { + strcpy(ctx->dev_name, detail_data->DevicePath); + device_found = TRUE; + free(detail_data); + break; + } + free(detail_data); + SetupDiDestroyDeviceInfoList(device_info_set); + return 0; + } + } + free(detail_data); + index++; + } + } + + SetupDiDestroyDeviceInfoList(device_info_set); + + return device_found ? 1 : 0; +} + +static void xfel_winusb_close(struct xfel_ctx_t * ctx) +{ + if(ctx->hdl && ctx->hdl != (void *)INVALID_HANDLE_VALUE) + CloseHandle((HANDLE)ctx->hdl); +} + +static int xfel_winusb_init(struct xfel_ctx_t * ctx) +{ + if (!ctx || !ctx->dev_name) { + printf("usb winusb init error, ctx or dev_name is NULL\r\n"); + return 0; + } + ctx->hdl = CreateFile(ctx->dev_name, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, + OPEN_EXISTING, 0, NULL); + + if (ctx->hdl == INVALID_HANDLE_VALUE) { + printf("usb winusb init error, CreateFile failed\r\n"); + return 0; + } + + return 1; +} + +struct usb_ops_t xfel_winusb_ops = { + .bulk_send = xfel_winusb_bulk_send, + .bulk_recv = xfel_winusb_bulk_recv, + .open = xfel_winusb_open, + .close = xfel_winusb_close, + .init = xfel_winusb_init, +}; + +#endif // defined(USE_WINUSB_DRV) && defined(_WIN32) diff --git a/x.h b/x.h index b7e0356..38c513c 100644 --- a/x.h +++ b/x.h @@ -31,7 +31,6 @@ extern "C" { #include #include #include -#include /* * byteorder