Commit ea3f7fee authored by Bernhard Nortmann's avatar Bernhard Nortmann
Browse files

fel: Rework some of the USB functions' logic



This moves claiming / releasing the interface into the respective
"open" / "close" functions. The USB code in main() is now trimmed
down to:

feldev_init();
handle = open_fel_device(...);
feldev_done(handle);
Signed-off-by: default avatarBernhard Nortmann <bernhard.nortmann@web.de>
parent edf6d6c5
...@@ -1422,52 +1422,72 @@ static feldev_handle *open_fel_device(int busnum, int devnum, ...@@ -1422,52 +1422,72 @@ static feldev_handle *open_fel_device(int busnum, int devnum,
} }
exit(1); exit(1);
} }
return result; } else {
} /* look for specific bus and device number */
bool found = false;
/* look for specific bus and device number */ ssize_t rc, i;
pr_info("Selecting USB Bus %03d Device %03d\n", busnum, devnum); libusb_device **list;
bool found = false;
ssize_t rc, i; rc = libusb_get_device_list(NULL, &list);
libusb_device **list; if (rc < 0)
usb_error(rc, "libusb_get_device_list()", 1);
rc = libusb_get_device_list(NULL, &list); for (i = 0; i < rc; i++) {
if (rc < 0) if (libusb_get_bus_number(list[i]) == busnum
usb_error(rc, "libusb_get_device_list()", 1); && libusb_get_device_address(list[i]) == devnum) {
for (i = 0; i < rc; i++) { found = true; /* bus:devnum matched */
if (libusb_get_bus_number(list[i]) == busnum struct libusb_device_descriptor desc;
&& libusb_get_device_address(list[i]) == devnum) { libusb_get_device_descriptor(list[i], &desc);
found = true; /* bus:devnum matched */ if (desc.idVendor != vendor_id
struct libusb_device_descriptor desc; || desc.idProduct != product_id) {
libusb_get_device_descriptor(list[i], &desc); fprintf(stderr, "ERROR: Bus %03d Device %03d not a FEL device "
if (desc.idVendor != vendor_id "(expected %04x:%04x, got %04x:%04x)\n", busnum, devnum,
|| desc.idProduct != product_id) { vendor_id, product_id, desc.idVendor, desc.idProduct);
fprintf(stderr, "ERROR: Bus %03d Device %03d not a FEL device " exit(1);
"(expected %04x:%04x, got %04x:%04x)\n", busnum, devnum, }
vendor_id, product_id, desc.idVendor, desc.idProduct); /* open handle to this specific device (incrementing its refcount) */
exit(1); rc = libusb_open(list[i], &result->usb->handle);
if (rc != 0)
usb_error(rc, "libusb_open()", 1);
break;
} }
/* open handle to this specific device (incrementing its refcount) */
rc = libusb_open(list[i], &result->usb->handle);
if (rc != 0)
usb_error(rc, "libusb_open()", 1);
break;
} }
} libusb_free_device_list(list, true);
libusb_free_device_list(list, true);
if (!found) { if (!found) {
fprintf(stderr, "ERROR: Bus %03d Device %03d not found in libusb device list\n", fprintf(stderr, "ERROR: Bus %03d Device %03d not found in libusb device list\n",
busnum, devnum); busnum, devnum);
exit(1); exit(1);
}
} }
feldev_claim(result); /* claim interface, detect USB endpoints */
return result; return result;
} }
void feldev_close(feldev_handle *dev) void feldev_close(feldev_handle *dev)
{ {
libusb_close(dev->usb->handle); if (dev) {
free(dev->usb); /* release memory allocated for felusb_handle struct */ if (dev->usb->handle) {
feldev_release(dev);
libusb_close(dev->usb->handle);
}
free(dev->usb); /* release memory allocated for felusb_handle */
}
}
void feldev_init(void)
{
int rc = libusb_init(NULL);
if (rc != 0)
usb_error(rc, "libusb_init()", 1);
}
void feldev_done(feldev_handle *dev)
{
feldev_close(dev);
free(dev);
libusb_exit(NULL);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
...@@ -1539,17 +1559,15 @@ int main(int argc, char **argv) ...@@ -1539,17 +1559,15 @@ int main(int argc, char **argv)
fprintf(stderr, "ERROR: Expected 'bus:devnum', got '%s'.\n", dev_arg); fprintf(stderr, "ERROR: Expected 'bus:devnum', got '%s'.\n", dev_arg);
exit(1); exit(1);
} }
pr_info("Selecting USB Bus %03d Device %03d\n", busnum, devnum);
} else } else
break; /* no valid (prefix) option detected, exit loop */ break; /* no valid (prefix) option detected, exit loop */
argc -= 1; argc -= 1;
argv += 1; argv += 1;
} }
int rc = libusb_init(NULL); feldev_init();
assert(rc == 0);
handle = open_fel_device(busnum, devnum, AW_USB_VENDOR_ID, AW_USB_PRODUCT_ID); handle = open_fel_device(busnum, devnum, AW_USB_VENDOR_ID, AW_USB_PRODUCT_ID);
assert(handle != NULL);
feldev_claim(handle);
while (argc > 1 ) { while (argc > 1 ) {
int skip = 1; int skip = 1;
...@@ -1645,10 +1663,7 @@ int main(int argc, char **argv) ...@@ -1645,10 +1663,7 @@ int main(int argc, char **argv)
aw_fel_execute(handle, uboot_entry); aw_fel_execute(handle, uboot_entry);
} }
feldev_release(handle); feldev_done(handle);
feldev_close(handle);
free(handle);
libusb_exit(NULL);
return 0; return 0;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment