diff mbox series

[2/3] backports: Add usb_check_bulk_endpoints()

Message ID 20240122213308.149303-2-hauke@hauke-m.de (mailing list archive)
State New, archived
Headers show
Series [1/3] patches: Refresh on top of kernel 5.15.147 | expand

Commit Message

Hauke Mehrtens Jan. 22, 2024, 9:33 p.m. UTC
Add the usb_check_bulk_endpoints() function.

This function was added to kernel 6.4 and backported to kernel 5.15.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 backport/backport-include/linux/usb.h |  6 ++++
 backport/compat/backport-5.15.c       | 51 +++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
diff mbox series

Patch

diff --git a/backport/backport-include/linux/usb.h b/backport/backport-include/linux/usb.h
index acb5b2ee..4830aebd 100644
--- a/backport/backport-include/linux/usb.h
+++ b/backport/backport-include/linux/usb.h
@@ -13,4 +13,10 @@  usb_find_common_endpoints(struct usb_host_interface *alt,
 		struct usb_endpoint_descriptor **int_out);
 #endif /* < 4.12 */
 
+#if LINUX_VERSION_IS_LESS(5,15,0)
+#define usb_check_bulk_endpoints LINUX_BACKPORT(usb_check_bulk_endpoints)
+bool usb_check_bulk_endpoints(
+		const struct usb_interface *intf, const u8 *ep_addrs);
+#endif /* < 5.15 */
+
 #endif /* __BACKPORT_LINUX_USB_H */
diff --git a/backport/compat/backport-5.15.c b/backport/compat/backport-5.15.c
index fafe9c77..11aa74f6 100644
--- a/backport/compat/backport-5.15.c
+++ b/backport/compat/backport-5.15.c
@@ -4,6 +4,7 @@ 
 #include <linux/export.h>
 #include <linux/uaccess.h>
 #include <linux/netdevice.h>
+#include <linux/usb.h>
 
 #include <uapi/linux/if.h>
 
@@ -53,3 +54,53 @@  int put_user_ifreq(struct ifreq *ifr, void __user *arg)
 EXPORT_SYMBOL(put_user_ifreq);
 
 #endif /* >= 4.6.0 */
+
+/**
+ * usb_find_endpoint() - Given an endpoint address, search for the endpoint's
+ * usb_host_endpoint structure in an interface's current altsetting.
+ * @intf: the interface whose current altsetting should be searched
+ * @ep_addr: the endpoint address (number and direction) to find
+ *
+ * Search the altsetting's list of endpoints for one with the specified address.
+ *
+ * Return: Pointer to the usb_host_endpoint if found, %NULL otherwise.
+ */
+static const struct usb_host_endpoint *usb_find_endpoint(
+		const struct usb_interface *intf, unsigned int ep_addr)
+{
+	int n;
+	const struct usb_host_endpoint *ep;
+
+	n = intf->cur_altsetting->desc.bNumEndpoints;
+	ep = intf->cur_altsetting->endpoint;
+	for (; n > 0; (--n, ++ep)) {
+		if (ep->desc.bEndpointAddress == ep_addr)
+			return ep;
+	}
+	return NULL;
+}
+
+/**
+ * usb_check_bulk_endpoints - Check whether an interface's current altsetting
+ * contains a set of bulk endpoints with the given addresses.
+ * @intf: the interface whose current altsetting should be searched
+ * @ep_addrs: 0-terminated array of the endpoint addresses (number and
+ * direction) to look for
+ *
+ * Search for endpoints with the specified addresses and check their types.
+ *
+ * Return: %true if all the endpoints are found and are bulk, %false otherwise.
+ */
+bool usb_check_bulk_endpoints(
+		const struct usb_interface *intf, const u8 *ep_addrs)
+{
+	const struct usb_host_endpoint *ep;
+
+	for (; *ep_addrs; ++ep_addrs) {
+		ep = usb_find_endpoint(intf, *ep_addrs);
+		if (!ep || !usb_endpoint_xfer_bulk(&ep->desc))
+			return false;
+	}
+	return true;
+}
+EXPORT_SYMBOL_GPL(usb_check_bulk_endpoints);