From 214ecc86229ed46173b6d1f1bcc797dc2d6771d2 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Thu, 3 Nov 2011 01:07:15 +0000 Subject: [PATCH] Successfully locate and open stlinkv1 by usb Very very raw, this just finds the device and opens the usb handle. --- Makefile | 2 +- src/stlink-sg.c | 84 ++++++++++++++++++++++++++++++++++++++++++++----- src/stlink-sg.h | 3 ++ src/test_sg.c | 3 +- 4 files changed, 82 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index c3c345a..646dde1 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VPATH=src SOURCES_LIB=stlink-common.c stlink-usb.c stlink-sg.c uglylogging.c OBJS_LIB=$(SOURCES_LIB:.c=.o) -TEST_PROGRAMS=test_usb #test_sg +TEST_PROGRAMS=test_usb test_sg LDFLAGS=-lusb-1.0 -L. -lstlink CFLAGS+=-g diff --git a/src/stlink-sg.c b/src/stlink-sg.c index 5d1f970..3b9958d 100644 --- a/src/stlink-sg.c +++ b/src/stlink-sg.c @@ -736,26 +736,94 @@ stlink_backend_t _stlink_sg_backend = { _stlink_sg_force_debug }; -stlink_t* stlink_open(const char *dev_name, const int verbose) { - fprintf(stderr, "\n*** stlink_open [%s] ***\n", dev_name); - int sg_fd = scsi_pt_open_device(dev_name, RDWR, verbose); - if (sg_fd < 0) { - fprintf(stderr, "error opening device: %s: %s\n", dev_name, - safe_strerror(-sg_fd)); +#if using_stm8_stuff_XXXX +stlink *stlink_open(libusb_context *usb_context) +{ + stlink *stl = malloc(sizeof(stlink)); + stl->handle = libusb_open_device_with_vid_pid(usb_context, USB_VID_ST, USB_PID_STLINK); + if (stl->handle == NULL) { + free(stl); + return NULL; + } + + libusb_device *dev = libusb_get_device(stl->handle); + struct libusb_config_descriptor *conf_desc; + int ret = libusb_get_config_descriptor(dev, 0, &conf_desc); + if (ret != LIBUSB_SUCCESS) { + libusb_close(stl->handle); + free(stl); + return NULL; + } + for (int i = 0; i < conf_desc->bNumInterfaces; i++) { + printf("interface %d\n", i); + for (int j = 0; j < conf_desc->interface[i].num_altsetting; j++) { + for (int k = 0; k < conf_desc->interface[i].altsetting[j].bNumEndpoints; k++) { + const struct libusb_endpoint_descriptor *endpoint; + endpoint = &conf_desc->interface[i].altsetting[j].endpoint[k]; + if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { + stl->endpoint_in = endpoint->bEndpointAddress; + printf("Found IN endpoint\n"); + } else { + stl->endpoint_out = endpoint->bEndpointAddress; + printf("Found OUT endpoint\n"); + } + } + } + } + libusb_free_config_descriptor(conf_desc); + + ret = libusb_kernel_driver_active(stl->handle, 0); + if (ret == 1) { + printf("kernel driver active\n"); + } else if (ret == 0) { + //printf("kernel driver not active\n"); + } else { + fprintf(stderr, "libusb_kernel_driver_active = %d\n", ret); + } + ret = libusb_claim_interface(stl->handle, 0); + if (ret != LIBUSB_SUCCESS) { + fprintf(stderr, "claiming interface failed: %d\n", ret); + libusb_close(stl->handle); + free(stl); return NULL; } + return stl; +} +#endif + + +static stlink_t* stlink_open(const char *dev_name, const int verbose) { + fprintf(stderr, "\n*** stlink_open [%s] ***\n", dev_name); + stlink_t *sl = malloc(sizeof (stlink_t)); struct stlink_libsg *slsg = malloc(sizeof (struct stlink_libsg)); if (sl == NULL || slsg == NULL) { - fprintf(stderr, "Couldn't malloc stlink and stlink_sg structures out of memory!\n"); + WLOG("Couldn't malloc stlink and stlink_sg structures out of memory!\n"); + return NULL; + } + + if (libusb_init(&(slsg->libusb_ctx))) { + WLOG("failed to init libusb context, wrong version of libraries?\n"); + free(sl); + free(slsg); return NULL; } + + slsg->handle = libusb_open_device_with_vid_pid(slsg->libusb_ctx, USB_ST_VID, USB_STLINK_PID); + if (slsg->handle == NULL) { + WLOG("Failed to find an stlink v1 by VID:PID\n"); + free(sl); + free(slsg); + return NULL; + } + + DLOG("Successfully opened stlinkv1 by libusb :)\n"); + sl->verbose = verbose; sl->backend_data = slsg; sl->backend = &_stlink_sg_backend; - slsg->sg_fd = sg_fd; sl->core_stat = STLINK_CORE_STAT_UNKNOWN; slsg->q_addr = 0; clear_buf(sl); diff --git a/src/stlink-sg.h b/src/stlink-sg.h index 723c66a..c9776cd 100644 --- a/src/stlink-sg.h +++ b/src/stlink-sg.h @@ -41,6 +41,9 @@ extern "C" { struct stlink_libsg { + libusb_context* libusb_ctx; + libusb_device_handle *handle; + int sg_fd; int do_scsi_pt_err; diff --git a/src/test_sg.c b/src/test_sg.c index 30f867a..742ba44 100644 --- a/src/test_sg.c +++ b/src/test_sg.c @@ -29,10 +29,11 @@ int main(int argc, char *argv[]) { dev_name = argv[1]; break; default: + fprintf(stderr, "bzzt\n"); return EXIT_FAILURE; } - stlink_t *sl = stlink_v1_open(dev_name, 10); + stlink_t *sl = stlink_v1_open(dev_name, 99); if (sl == NULL) return EXIT_FAILURE; -- 2.30.2