Commit 472ac4759df557c00248e557beb869f4fe7d75f7 introduced
a possible regression by relying on the availability of
libusb_strerror(). There are libusb versions out there _not_
offering this function, which breaks compilation.

Introducing a separate helper function allows us to work around
this, refactor existing code and have more streamlined error
reporting - avoiding the message string retrieval where needed.
The usb_error() function can also optionally abort execution
with a given exit code.

Signed-off-by: Bernhard Nortmann <bernhard.nortm...@web.de>
---
 fel.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/fel.c b/fel.c
index 62f2fa8..3763fd8 100644
--- a/fel.c
+++ b/fel.c
@@ -40,6 +40,20 @@
 static const uint16_t AW_USB_VENDOR_ID  = 0x1F3A;
 static const uint16_t AW_USB_PRODUCT_ID = 0xEFE8;
 
+/* a helper function to report libusb errors */
+void usb_error(int rc, const char *caption, int exitcode)
+{
+       if (caption)
+               fprintf(stderr, "%s ", caption);
+#if defined(libusb_strerror)
+       fprintf(stderr, "ERROR %d: %s\n", rc, libusb_strerror(rc));
+#else
+       fprintf(stderr, "ERROR %d\n", rc);
+#endif
+       if (exitcode != 0)
+               exit(exitcode);
+}
+
 struct  aw_usb_request {
        char signature[8];
        uint32_t length;
@@ -97,10 +111,8 @@ void usb_bulk_send(libusb_device_handle *usb, int ep, const 
void *data,
        while (length > 0) {
                chunk = length < max_chunk ? length : max_chunk;
                rc = libusb_bulk_transfer(usb, ep, (void *)data, chunk, &sent, 
timeout);
-               if (rc != 0) {
-                       fprintf(stderr, "libusb usb_bulk_send error %d\n", rc);
-                       exit(2);
-               }
+               if (rc != 0)
+                       usb_error(rc, "usb_bulk_send()", 2);
                length -= sent;
                data += sent;
 
@@ -114,10 +126,8 @@ void usb_bulk_recv(libusb_device_handle *usb, int ep, void 
*data, int length)
        int rc, recv;
        while (length > 0) {
                rc = libusb_bulk_transfer(usb, ep, data, length, &recv, 
timeout);
-               if (rc != 0) {
-                       fprintf(stderr, "usb_bulk_recv error %d\n", rc);
-                       exit(2);
-               }
+               if (rc != 0)
+                       usb_error(rc, "usb_bulk_recv()", 2);
                length -= recv;
                data += recv;
        }
@@ -1321,11 +1331,8 @@ static libusb_device_handle *open_fel_device(int busnum, 
int devnum,
        libusb_device **list;
 
        rc = libusb_get_device_list(NULL, &list);
-       if (rc < 0) {
-               fprintf(stderr, "libusb_get_device_list() ERROR: %s\n",
-                       libusb_strerror(rc));
-               exit(1);
-       }
+       if (rc < 0)
+               usb_error(rc, "libusb_get_device_list()", 1);
        for (i = 0; i < rc; i++) {
                if (libusb_get_bus_number(list[i]) == busnum
                    && libusb_get_device_address(list[i]) == devnum) {
@@ -1341,11 +1348,8 @@ static libusb_device_handle *open_fel_device(int busnum, 
int devnum,
                        }
                        /* open handle to this specific device (incrementing 
its refcount) */
                        rc = libusb_open(list[i], &result);
-                       if (rc != 0) {
-                               fprintf(stderr, "libusb_open() ERROR: %s\n",
-                                       libusb_strerror(rc));
-                               exit(1);
-                       }
+                       if (rc != 0)
+                               usb_error(rc, "libusb_open()", 1);
                        break;
                }
        }
-- 
2.4.10

-- 
You received this message because you are subscribed to the Google Groups 
"linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to linux-sunxi+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to