]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
USB: core: add helpers to retrieve endpoints in reverse order
authorJohan Hovold <johan@kernel.org>
Fri, 17 Mar 2017 10:35:31 +0000 (11:35 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 23 Mar 2017 12:53:16 +0000 (13:53 +0100)
Several drivers have implemented their endpoint look-up loops in such a
way that they have picked the last endpoint descriptor of the specified
type should more than one such descriptor exist.

To avoid any regressions, add corresponding helpers to lookup endpoints
by searching the endpoint descriptors in reverse order.

Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/usb.c
include/linux/usb.h

index 5d65504770f5c3d0e260e792cc1d6f77b4177025..1ec9d248781eb3d90d7c148a0cada21ada2dd2e8 100644 (file)
@@ -74,6 +74,48 @@ MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
 #define usb_autosuspend_delay          0
 #endif
 
+static bool match_endpoint(struct usb_endpoint_descriptor *epd,
+               struct usb_endpoint_descriptor **bulk_in,
+               struct usb_endpoint_descriptor **bulk_out,
+               struct usb_endpoint_descriptor **int_in,
+               struct usb_endpoint_descriptor **int_out)
+{
+       switch (usb_endpoint_type(epd)) {
+       case USB_ENDPOINT_XFER_BULK:
+               if (usb_endpoint_dir_in(epd)) {
+                       if (bulk_in && !*bulk_in) {
+                               *bulk_in = epd;
+                               break;
+                       }
+               } else {
+                       if (bulk_out && !*bulk_out) {
+                               *bulk_out = epd;
+                               break;
+                       }
+               }
+
+               return false;
+       case USB_ENDPOINT_XFER_INT:
+               if (usb_endpoint_dir_in(epd)) {
+                       if (int_in && !*int_in) {
+                               *int_in = epd;
+                               break;
+                       }
+               } else {
+                       if (int_out && !*int_out) {
+                               *int_out = epd;
+                               break;
+                       }
+               }
+
+               return false;
+       default:
+               return false;
+       }
+
+       return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&
+                       (!int_in || *int_in) && (!int_out || *int_out);
+}
 
 /**
  * usb_find_common_endpoints() -- look up common endpoint descriptors
@@ -113,50 +155,48 @@ int usb_find_common_endpoints(struct usb_host_interface *alt,
        for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
                epd = &alt->endpoint[i].desc;
 
-               switch (usb_endpoint_type(epd)) {
-               case USB_ENDPOINT_XFER_BULK:
-                       if (usb_endpoint_dir_in(epd)) {
-                               if (bulk_in && !*bulk_in) {
-                                       *bulk_in = epd;
-                                       break;
-                               }
-                       } else {
-                               if (bulk_out && !*bulk_out) {
-                                       *bulk_out = epd;
-                                       break;
-                               }
-                       }
+               if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
+                       return 0;
+       }
 
-                       continue;
-               case USB_ENDPOINT_XFER_INT:
-                       if (usb_endpoint_dir_in(epd)) {
-                               if (int_in && !*int_in) {
-                                       *int_in = epd;
-                                       break;
-                               }
-                       } else {
-                               if (int_out && !*int_out) {
-                                       *int_out = epd;
-                                       break;
-                               }
-                       }
+       return -ENXIO;
+}
+EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
 
-                       continue;
-               default:
-                       continue;
-               }
+/**
+ * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
+ *
+ * Same as usb_find_common_endpoints(), but the endpoint descriptors are
+ * searched in reverse order (see usb_find_common_endpoints() for details).
+ */
+int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **bulk_in,
+               struct usb_endpoint_descriptor **bulk_out,
+               struct usb_endpoint_descriptor **int_in,
+               struct usb_endpoint_descriptor **int_out)
+{
+       struct usb_endpoint_descriptor *epd;
+       int i;
+
+       if (bulk_in)
+               *bulk_in = NULL;
+       if (bulk_out)
+               *bulk_out = NULL;
+       if (int_in)
+               *int_in = NULL;
+       if (int_out)
+               *int_out = NULL;
+
+       for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {
+               epd = &alt->endpoint[i].desc;
 
-               if ((!bulk_in || *bulk_in) &&
-                               (!bulk_out || *bulk_out) &&
-                               (!int_in || *int_in) &&
-                               (!int_out || *int_out)) {
+               if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
                        return 0;
-               }
        }
 
        return -ENXIO;
 }
-EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
+EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
 
 /**
  * usb_find_alt_setting() - Given a configuration, find the alternate setting
index 7041cc9507370a6934f5b3cbbab6619b76984120..226557362d36ae79656379dde441f94983728d53 100644 (file)
@@ -106,6 +106,13 @@ usb_find_common_endpoints(struct usb_host_interface *alt,
                struct usb_endpoint_descriptor **int_in,
                struct usb_endpoint_descriptor **int_out);
 
+int __must_check
+usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **bulk_in,
+               struct usb_endpoint_descriptor **bulk_out,
+               struct usb_endpoint_descriptor **int_in,
+               struct usb_endpoint_descriptor **int_out);
+
 static inline int __must_check
 usb_find_bulk_in_endpoint(struct usb_host_interface *alt,
                struct usb_endpoint_descriptor **bulk_in)
@@ -134,6 +141,34 @@ usb_find_int_out_endpoint(struct usb_host_interface *alt,
        return usb_find_common_endpoints(alt, NULL, NULL, NULL, int_out);
 }
 
+static inline int __must_check
+usb_find_last_bulk_in_endpoint(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **bulk_in)
+{
+       return usb_find_common_endpoints_reverse(alt, bulk_in, NULL, NULL, NULL);
+}
+
+static inline int __must_check
+usb_find_last_bulk_out_endpoint(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **bulk_out)
+{
+       return usb_find_common_endpoints_reverse(alt, NULL, bulk_out, NULL, NULL);
+}
+
+static inline int __must_check
+usb_find_last_int_in_endpoint(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **int_in)
+{
+       return usb_find_common_endpoints_reverse(alt, NULL, NULL, int_in, NULL);
+}
+
+static inline int __must_check
+usb_find_last_int_out_endpoint(struct usb_host_interface *alt,
+               struct usb_endpoint_descriptor **int_out)
+{
+       return usb_find_common_endpoints_reverse(alt, NULL, NULL, NULL, int_out);
+}
+
 /**
  * struct usb_interface - what usb device drivers talk to
  * @altsetting: array of interface structures, one for each alternate