History

usb: raw-gadget: fix gadget endpoint selection

Currently automatic gadget endpoint selection based on required features doesn't work. Raw Gadget tries iterating over the list of available endpoints and finding one that has the right direction and transfer type. Unfortunately selecting arbitrary gadget endpoints (even if they satisfy feature requirements) doesn't work, as (depending on the UDC driver) they might have fixed addresses, and one also needs to provide matching endpoint addresses in the descriptors sent to the host. The composite framework deals with this by assigning endpoint addresses in usb_ep_autoconfig() before enumeration starts. This approach won't work with Raw Gadget as the endpoints are supposed to be enabled after a set_configuration/set_interface request from the host, so it's too late to patch the endpoint descriptors that had already been sent to the host. For Raw Gadget we take another approach. Similarly to GadgetFS, we allow the user to make the decision as to which gadget endpoints to use. This patch adds another Raw Gadget ioctl USB_RAW_IOCTL_EPS_INFO that exposes information about all non-control endpoints that a currently connected UDC has. This information includes endpoints addresses, as well as their capabilities and limits to allow the user to choose the most fitting gadget endpoint. The USB_RAW_IOCTL_EP_ENABLE ioctl is updated to use the proper endpoint validation routine usb_gadget_ep_match_desc(). These changes affect the portability of the gadgets that use Raw Gadget when running on different UDCs. Nevertheless, as long as the user relies on the information provided by USB_RAW_IOCTL_EPS_INFO to dynamically choose endpoint addresses, UDC-agnostic gadgets can still be written with Raw Gadget.

Andrey Konovalov committed 6 years ago

Commit 97df5e51 parent 17ff3b73 files · +194 −70Browse files

Unified · Split

3 files
  1. Documentation/usb/raw-gadget.rst +2 −3
  2. drivers/usb/gadget/legacy/raw_gadget.c +123 −64
  3. include/uapi/linux/usb/raw_gadget.h +69 −3
Documentation/usb/raw-gadget.rst+2−3
@@ -27,9 +27,8 @@ differences are: · 26 unmodified lines
2727 3. Raw Gadget provides a way to select a UDC device/driver to bind to,
2828 while GadgetFS currently binds to the first available UDC.
2929
30−4. Raw Gadget uses predictable endpoint names (handles) across different
31− UDCs (as long as UDCs have enough endpoints of each required transfer
32− type).
30+4. Raw Gadget explicitly exposes information about endpoints addresses and
31+ capabilities allowing a user to write UDC-agnostic gadgets.
3332
3433 5. Raw Gadget has ioctl-based interface instead of a filesystem-based one.
3534
drivers/usb/gadget/legacy/raw_gadget.c+123−64
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 */
88
99 #include <linux/compiler.h>
10+#include <linux/ctype.h>
1011 #include <linux/debugfs.h>
1112 #include <linux/delay.h>
1213 #include <linux/kref.h>
@@ -123,8 +124,6 @@ static void raw_event_queue_destroy(struct raw_event_queue *queue) · 110 unmodified lines
123124
124125 struct raw_dev;
125126
126−#define USB_RAW_MAX_ENDPOINTS 32
127−
128127 enum ep_state {
129128 STATE_EP_DISABLED,
130129 STATE_EP_ENABLED,
@@ -134,6 +133,7 @@ struct raw_ep { · 3 unmodified lines
134133 struct raw_dev *dev;
135134 enum ep_state state;
136135 struct usb_ep *ep;
136+ u8 addr;
137137 struct usb_request *req;
138138 bool urb_queued;
139139 bool disabling;
@@ -168,7 +168,8 @@ struct raw_dev { · 28 unmodified lines
168168 bool ep0_out_pending;
169169 bool ep0_urb_queued;
170170 ssize_t ep0_status;
171− struct raw_ep eps[USB_RAW_MAX_ENDPOINTS];
171+ struct raw_ep eps[USB_RAW_EPS_NUM_MAX];
172+ int eps_num;
172173
173174 struct completion ep0_done;
174175 struct raw_event_queue queue;
@@ -202,7 +203,7 @@ static void dev_free(struct kref *kref) · 27 unmodified lines
202203 usb_ep_free_request(dev->gadget->ep0, dev->req);
203204 }
204205 raw_event_queue_destroy(&dev->queue);
205− for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) {
206+ for (i = 0; i < dev->eps_num; i++) {
206207 if (dev->eps[i].state != STATE_EP_ENABLED)
207208 continue;
208209 usb_ep_disable(dev->eps[i].ep);
@@ -249,12 +250,26 @@ static void gadget_ep0_complete(struct usb_ep *ep, struct usb_request *req) · 40 unmodified lines
249250 complete(&dev->ep0_done);
250251 }
251252
253+static u8 get_ep_addr(const char *name)
254+{
255+ /* If the endpoint has fixed function (named as e.g. "ep12out-bulk"),
256+ * parse the endpoint address from its name. We deliberately use
257+ * deprecated simple_strtoul() function here, as the number isn't
258+ * followed by '\0' nor '\n'.
259+ */
260+ if (isdigit(name[2]))
261+ return simple_strtoul(&name[2], NULL, 10);
262+ /* Otherwise the endpoint is configurable (named as e.g. "ep-a"). */
263+ return USB_RAW_EP_ADDR_ANY;
264+}
265+
252266 static int gadget_bind(struct usb_gadget *gadget,
253267 struct usb_gadget_driver *driver)
254268 {
255− int ret = 0;
269+ int ret = 0, i = 0;
256270 struct raw_dev *dev = container_of(driver, struct raw_dev, driver);
257271 struct usb_request *req;
272+ struct usb_ep *ep;
258273 unsigned long flags;
259274
260275 if (strcmp(gadget->name, dev->udc_name) != 0)
@@ -273,6 +288,13 @@ static int gadget_bind(struct usb_gadget *gadget, · 12 unmodified lines
273288 dev->req->context = dev;
274289 dev->req->complete = gadget_ep0_complete;
275290 dev->gadget = gadget;
291+ gadget_for_each_ep(ep, dev->gadget) {
292+ dev->eps[i].ep = ep;
293+ dev->eps[i].addr = get_ep_addr(ep->name);
294+ dev->eps[i].state = STATE_EP_DISABLED;
295+ i++;
296+ }
297+ dev->eps_num = i;
276298 spin_unlock_irqrestore(&dev->lock, flags);
277299
278300 /* Matches kref_put() in gadget_unbind(). */
@@ -555,7 +577,7 @@ static void *raw_alloc_io_data(struct usb_raw_ep_io *io, void __user *ptr, · 276 unmodified lines
555577
556578 if (copy_from_user(io, ptr, sizeof(*io)))
557579 return ERR_PTR(-EFAULT);
558− if (io->ep >= USB_RAW_MAX_ENDPOINTS)
580+ if (io->ep >= USB_RAW_EPS_NUM_MAX)
559581 return ERR_PTR(-EINVAL);
560582 if (!usb_raw_io_flags_valid(io->flags))
561583 return ERR_PTR(-EINVAL);
@@ -682,40 +704,12 @@ free: · 120 unmodified lines
682704 return ret;
683705 }
684706
685−static bool check_ep_caps(struct usb_ep *ep,
686− struct usb_endpoint_descriptor *desc)
687−{
688− switch (usb_endpoint_type(desc)) {
689− case USB_ENDPOINT_XFER_ISOC:
690− if (!ep->caps.type_iso)
691− return false;
692− break;
693− case USB_ENDPOINT_XFER_BULK:
694− if (!ep->caps.type_bulk)
695− return false;
696− break;
697− case USB_ENDPOINT_XFER_INT:
698− if (!ep->caps.type_int)
699− return false;
700− break;
701− default:
702− return false;
703− }
704−
705− if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
706− return false;
707− if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
708− return false;
709−
710− return true;
711−}
712−
713707 static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value)
714708 {
715709 int ret = 0, i;
716710 unsigned long flags;
717711 struct usb_endpoint_descriptor *desc;
718− struct usb_ep *ep = NULL;
712+ struct raw_ep *ep;
719713
720714 desc = memdup_user((void __user *)value, sizeof(*desc));
721715 if (IS_ERR(desc))
@@ -743,41 +737,32 @@ static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value) · 21 unmodified lines
743737 goto out_free;
744738 }
745739
746− for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) {
747− if (dev->eps[i].state == STATE_EP_ENABLED)
740+ for (i = 0; i < dev->eps_num; i++) {
741+ ep = &dev->eps[i];
742+ if (ep->state != STATE_EP_DISABLED)
748743 continue;
749− break;
750− }
751− if (i == USB_RAW_MAX_ENDPOINTS) {
752− dev_dbg(&dev->gadget->dev,
753− "fail, no device endpoints available\n");
754− ret = -EBUSY;
755− goto out_free;
756− }
757−
758− gadget_for_each_ep(ep, dev->gadget) {
759− if (ep->enabled)
744+ if (ep->addr != usb_endpoint_num(desc) &&
745+ ep->addr != USB_RAW_EP_ADDR_ANY)
760746 continue;
761− if (!check_ep_caps(ep, desc))
747+ if (!usb_gadget_ep_match_desc(dev->gadget, ep->ep, desc, NULL))
762748 continue;
763− ep->desc = desc;
764− ret = usb_ep_enable(ep);
749+ ep->ep->desc = desc;
750+ ret = usb_ep_enable(ep->ep);
765751 if (ret < 0) {
766752 dev_err(&dev->gadget->dev,
767753 "fail, usb_ep_enable returned %d\n", ret);
768754 goto out_free;
769755 }
770− dev->eps[i].req = usb_ep_alloc_request(ep, GFP_ATOMIC);
771− if (!dev->eps[i].req) {
756+ ep->req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
757+ if (!ep->req) {
772758 dev_err(&dev->gadget->dev,
773759 "fail, usb_ep_alloc_request failed\n");
774− usb_ep_disable(ep);
760+ usb_ep_disable(ep->ep);
775761 ret = -ENOMEM;
776762 goto out_free;
777763 }
778− dev->eps[i].ep = ep;
779− dev->eps[i].state = STATE_EP_ENABLED;
780− ep->driver_data = &dev->eps[i];
764+ ep->state = STATE_EP_ENABLED;
765+ ep->ep->driver_data = ep;
781766 ret = i;
782767 goto out_unlock;
783768 }
@@ -796,10 +781,6 @@ static int raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value) · 12 unmodified lines
796781 {
797782 int ret = 0, i = value;
798783 unsigned long flags;
799− const void *desc;
800−
801− if (i < 0 || i >= USB_RAW_MAX_ENDPOINTS)
802− return -EINVAL;
803784
804785 spin_lock_irqsave(&dev->lock, flags);
805786 if (dev->state != STATE_DEV_RUNNING) {
@@ -812,6 +793,11 @@ static int raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value) · 6 unmodified lines
812793 ret = -EBUSY;
813794 goto out_unlock;
814795 }
796+ if (i < 0 || i >= dev->eps_num) {
797+ dev_dbg(dev->dev, "fail, invalid endpoint\n");
798+ ret = -EBUSY;
799+ goto out_unlock;
800+ }
815801 if (dev->eps[i].state != STATE_EP_ENABLED) {
816802 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n");
817803 ret = -EINVAL;
@@ -836,10 +822,9 @@ static int raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value) · 18 unmodified lines
836822
837823 spin_lock_irqsave(&dev->lock, flags);
838824 usb_ep_free_request(dev->eps[i].ep, dev->eps[i].req);
839− desc = dev->eps[i].ep->desc;
825+ kfree(dev->eps[i].ep->desc);
840826 dev->eps[i].ep = NULL;
841827 dev->eps[i].state = STATE_EP_DISABLED;
842− kfree(desc);
843828 dev->eps[i].disabling = false;
844829
845830 out_unlock:
@@ -868,7 +853,7 @@ static int raw_process_ep_io(struct raw_dev *dev, struct usb_raw_ep_io *io, · 22 unmodified lines
868853 {
869854 int ret = 0;
870855 unsigned long flags;
871− struct raw_ep *ep = &dev->eps[io->ep];
856+ struct raw_ep *ep;
872857 DECLARE_COMPLETION_ONSTACK(done);
873858
874859 spin_lock_irqsave(&dev->lock, flags);
@@ -882,6 +867,12 @@ static int raw_process_ep_io(struct raw_dev *dev, struct usb_raw_ep_io *io, · 7 unmodified lines
882867 ret = -EBUSY;
883868 goto out_unlock;
884869 }
870+ if (io->ep >= dev->eps_num) {
871+ dev_dbg(&dev->gadget->dev, "fail, invalid endpoint\n");
872+ ret = -EINVAL;
873+ goto out_unlock;
874+ }
875+ ep = &dev->eps[io->ep];
885876 if (ep->state != STATE_EP_ENABLED) {
886877 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n");
887878 ret = -EBUSY;
@@ -1027,6 +1018,71 @@ out_unlock: · 139 unmodified lines
10271018 return ret;
10281019 }
10291020
1021+static void fill_ep_caps(struct usb_ep_caps *caps,
1022+ struct usb_raw_ep_caps *raw_caps)
1023+{
1024+ raw_caps->type_control = caps->type_control;
1025+ raw_caps->type_iso = caps->type_iso;
1026+ raw_caps->type_bulk = caps->type_bulk;
1027+ raw_caps->type_int = caps->type_int;
1028+ raw_caps->dir_in = caps->dir_in;
1029+ raw_caps->dir_out = caps->dir_out;
1030+}
1031+
1032+static void fill_ep_limits(struct usb_ep *ep, struct usb_raw_ep_limits *limits)
1033+{
1034+ limits->maxpacket_limit = ep->maxpacket_limit;
1035+ limits->max_streams = ep->max_streams;
1036+}
1037+
1038+static int raw_ioctl_eps_info(struct raw_dev *dev, unsigned long value)
1039+{
1040+ int ret = 0, i;
1041+ unsigned long flags;
1042+ struct usb_raw_eps_info *info;
1043+ struct raw_ep *ep;
1044+
1045+ info = kmalloc(sizeof(*info), GFP_KERNEL);
1046+ if (!info) {
1047+ ret = -ENOMEM;
1048+ goto out;
1049+ }
1050+
1051+ spin_lock_irqsave(&dev->lock, flags);
1052+ if (dev->state != STATE_DEV_RUNNING) {
1053+ dev_dbg(dev->dev, "fail, device is not running\n");
1054+ ret = -EINVAL;
1055+ spin_unlock_irqrestore(&dev->lock, flags);
1056+ goto out_free;
1057+ }
1058+ if (!dev->gadget) {
1059+ dev_dbg(dev->dev, "fail, gadget is not bound\n");
1060+ ret = -EBUSY;
1061+ spin_unlock_irqrestore(&dev->lock, flags);
1062+ goto out_free;
1063+ }
1064+
1065+ memset(info, 0, sizeof(*info));
1066+ for (i = 0; i < dev->eps_num; i++) {
1067+ ep = &dev->eps[i];
1068+ strscpy(&info->eps[i].name[0], ep->ep->name,
1069+ USB_RAW_EP_NAME_MAX);
1070+ info->eps[i].addr = ep->addr;
1071+ fill_ep_caps(&ep->ep->caps, &info->eps[i].caps);
1072+ fill_ep_limits(ep->ep, &info->eps[i].limits);
1073+ }
1074+ ret = dev->eps_num;
1075+ spin_unlock_irqrestore(&dev->lock, flags);
1076+
1077+ if (copy_to_user((void __user *)value, info, sizeof(*info)))
1078+ ret = -EFAULT;
1079+
1080+out_free:
1081+ kfree(info);
1082+out:
1083+ return ret;
1084+}
1085+
10301086 static long raw_ioctl(struct file *fd, unsigned int cmd, unsigned long value)
10311087 {
10321088 struct raw_dev *dev = fd->private_data;
@@ -1069,6 +1125,9 @@ static long raw_ioctl(struct file *fd, unsigned int cmd, unsigned long value) · 36 unmodified lines
10691125 case USB_RAW_IOCTL_VBUS_DRAW:
10701126 ret = raw_ioctl_vbus_draw(dev, value);
10711127 break;
1128+ case USB_RAW_IOCTL_EPS_INFO:
1129+ ret = raw_ioctl_eps_info(dev, value);
1130+ break;
10721131 default:
10731132 ret = -EINVAL;
10741133 }
include/uapi/linux/usb/raw_gadget.h+69−3
@@ -93,6 +93,64 @@ struct usb_raw_ep_io { · 92 unmodified lines
9393 __u8 data[0];
9494 };
9595
96+/* Maximum number of non-control endpoints in struct usb_raw_eps_info. */
97+#define USB_RAW_EPS_NUM_MAX 30
98+
99+/* Maximum length of UDC endpoint name in struct usb_raw_ep_info. */
100+#define USB_RAW_EP_NAME_MAX 16
101+
102+/* Used as addr in struct usb_raw_ep_info if endpoint accepts any address. */
103+#define USB_RAW_EP_ADDR_ANY 0xff
104+
105+/*
106+ * struct usb_raw_ep_caps - exposes endpoint capabilities from struct usb_ep
107+ * (technically from its member struct usb_ep_caps).
108+ */
109+struct usb_raw_ep_caps {
110+ __u32 type_control : 1;
111+ __u32 type_iso : 1;
112+ __u32 type_bulk : 1;
113+ __u32 type_int : 1;
114+ __u32 dir_in : 1;
115+ __u32 dir_out : 1;
116+};
117+
118+/*
119+ * struct usb_raw_ep_limits - exposes endpoint limits from struct usb_ep.
120+ * @maxpacket_limit: Maximum packet size value supported by this endpoint.
121+ * @max_streams: maximum number of streams supported by this endpoint
122+ * (actual number is 2^n).
123+ * @reserved: Empty, reserved for potential future extensions.
124+ */
125+struct usb_raw_ep_limits {
126+ __u16 maxpacket_limit;
127+ __u16 max_streams;
128+ __u32 reserved;
129+};
130+
131+/*
132+ * struct usb_raw_ep_info - stores information about a gadget endpoint.
133+ * @name: Name of the endpoint as it is defined in the UDC driver.
134+ * @addr: Address of the endpoint that must be specified in the endpoint
135+ * descriptor passed to USB_RAW_IOCTL_EP_ENABLE ioctl.
136+ * @caps: Endpoint capabilities.
137+ * @limits: Endpoint limits.
138+ */
139+struct usb_raw_ep_info {
140+ __u8 name[USB_RAW_EP_NAME_MAX];
141+ __u32 addr;
142+ struct usb_raw_ep_caps caps;
143+ struct usb_raw_ep_limits limits;
144+};
145+
146+/*
147+ * struct usb_raw_eps_info - argument for USB_RAW_IOCTL_EPS_INFO ioctl.
148+ * eps: Structures that store information about non-control endpoints.
149+ */
150+struct usb_raw_eps_info {
151+ struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
152+};
153+
96154 /*
97155 * Initializes a Raw Gadget instance.
98156 * Accepts a pointer to the usb_raw_init struct as an argument.
@@ -126,9 +184,9 @@ struct usb_raw_ep_io { · 27 unmodified lines
126184 #define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
127185
128186 /*
129− * Finds an endpoint that supports the transfer type specified in the
130− * descriptor and enables it.
131− * Accepts a pointer to the usb_endpoint_descriptor struct as an argument.
187+ * Finds an endpoint that satisfies the parameters specified in the provided
188+ * descriptors (address, transfer type, etc.) and enables it.
189+ * Accepts a pointer to the usb_raw_ep_descs struct as an argument.
132190 * Returns enabled endpoint handle on success or negative error code on failure.
133191 */
134192 #define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
@@ -165,4 +223,12 @@ struct usb_raw_ep_io { · 30 unmodified lines
165223 */
166224 #define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
167225
226+/*
227+ * Fills in the usb_raw_eps_info structure with information about non-control
228+ * endpoints available for the currently connected UDC.
229+ * Returns the number of available endpoints on success or negative error code
230+ * on failure.
231+ */
232+#define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
233+
168234 #endif /* _UAPI__LINUX_USB_RAW_GADGET_H */