blob: 1cebd0727954ddbd12db4ee3a7be19450fa341e8 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001From 61c5fe3758a0febdee33429f5be16f69279045cc Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Rui Miguel Silva <rui.silva@linaro.org>
3Date: Mon, 28 Jun 2021 23:20:55 +0100
Patrick Williams92b42cb2022-09-03 06:53:57 -05004Subject: [PATCH 03/24] usb: common: move urb code to common
Brad Bishopbec4ebc2022-08-03 09:55:16 -04005
6Move urb code from musb only use to a more common scope, so other
7drivers in the future can use the handling of urb in usb.
8
9Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
10---
11 drivers/usb/common/Makefile | 2 +
12 drivers/usb/common/usb_urb.c | 160 ++++++++++++++++++
13 drivers/usb/host/r8a66597-hcd.c | 30 +---
14 drivers/usb/musb-new/musb_core.c | 2 +-
15 drivers/usb/musb-new/musb_host.c | 2 +-
16 drivers/usb/musb-new/musb_host.h | 2 +-
17 drivers/usb/musb-new/musb_uboot.c | 38 +----
18 drivers/usb/musb-new/musb_uboot.h | 2 +-
19 .../linux/usb/usb_urb_compat.h | 46 ++++-
20 include/usb_defs.h | 32 ++++
21 10 files changed, 240 insertions(+), 76 deletions(-)
22 create mode 100644 drivers/usb/common/usb_urb.c
23 rename drivers/usb/musb-new/usb-compat.h => include/linux/usb/usb_urb_compat.h (60%)
24
25diff --git a/drivers/usb/common/Makefile b/drivers/usb/common/Makefile
26index 3bedbf213f47..dc05cb0a5077 100644
27--- a/drivers/usb/common/Makefile
28+++ b/drivers/usb/common/Makefile
29@@ -4,5 +4,7 @@
30 #
31
32 obj-$(CONFIG_$(SPL_)DM_USB) += common.o
33+obj-$(CONFIG_USB_MUSB_HCD) += usb_urb.o
34+obj-$(CONFIG_USB_MUSB_UDC) += usb_urb.o
35 obj-$(CONFIG_USB_EHCI_FSL) += fsl-dt-fixup.o fsl-errata.o
36 obj-$(CONFIG_USB_XHCI_FSL) += fsl-dt-fixup.o fsl-errata.o
37diff --git a/drivers/usb/common/usb_urb.c b/drivers/usb/common/usb_urb.c
38new file mode 100644
39index 000000000000..be3b6b9f32e8
40--- /dev/null
41+++ b/drivers/usb/common/usb_urb.c
42@@ -0,0 +1,160 @@
43+// SPDX-License-Identifier: GPL-2.0
44+/*
45+ * Common code for usb urb handling, based on the musb-new code
46+ *
47+ * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
48+ *
49+ */
50+
51+#include <dm/device.h>
52+#include <dm/device_compat.h>
53+#include <linux/usb/usb_urb_compat.h>
54+
55+#include <time.h>
56+#include <usb.h>
57+
58+#if CONFIG_IS_ENABLED(DM_USB)
59+struct usb_device *usb_dev_get_parent(struct usb_device *udev)
60+{
61+ struct udevice *parent = udev->dev->parent;
62+
63+ /*
64+ * When called from usb-uclass.c: usb_scan_device() udev->dev points
65+ * to the parent udevice, not the actual udevice belonging to the
66+ * udev as the device is not instantiated yet.
67+ *
68+ * If dev is an usb-bus, then we are called from usb_scan_device() for
69+ * an usb-device plugged directly into the root port, return NULL.
70+ */
71+ if (device_get_uclass_id(udev->dev) == UCLASS_USB)
72+ return NULL;
73+
74+ /*
75+ * If these 2 are not the same we are being called from
76+ * usb_scan_device() and udev itself is the parent.
77+ */
78+ if (dev_get_parent_priv(udev->dev) != udev)
79+ return udev;
80+
81+ /* We are being called normally, use the parent pointer */
82+ if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
83+ return dev_get_parent_priv(parent);
84+
85+ return NULL;
86+}
87+#else
88+struct usb_device *usb_dev_get_parent(struct usb_device *udev)
89+{
90+ return udev->parent;
91+}
92+#endif
93+
94+static void usb_urb_complete(struct urb *urb)
95+{
96+ urb->dev->status &= ~USB_ST_NOT_PROC;
97+ urb->dev->act_len = urb->actual_length;
98+
99+ if (urb->status == -EINPROGRESS)
100+ urb->status = 0;
101+}
102+
103+void usb_urb_fill(struct urb *urb, struct usb_host_endpoint *hep,
104+ struct usb_device *dev, int endpoint_type,
105+ unsigned long pipe, void *buffer, int len,
106+ struct devrequest *setup, int interval)
107+{
108+ int epnum = usb_pipeendpoint(pipe);
109+ int is_in = usb_pipein(pipe);
110+ u16 maxpacketsize = is_in ? dev->epmaxpacketin[epnum] :
111+ dev->epmaxpacketout[epnum];
112+
113+ memset(urb, 0, sizeof(struct urb));
114+ memset(hep, 0, sizeof(struct usb_host_endpoint));
115+ INIT_LIST_HEAD(&hep->urb_list);
116+ INIT_LIST_HEAD(&urb->urb_list);
117+ urb->ep = hep;
118+ urb->complete = usb_urb_complete;
119+ urb->status = -EINPROGRESS;
120+ urb->dev = dev;
121+ urb->pipe = pipe;
122+ urb->transfer_buffer = buffer;
123+ urb->transfer_dma = (unsigned long)buffer;
124+ urb->transfer_buffer_length = len;
125+ urb->setup_packet = (unsigned char *)setup;
126+
127+ urb->ep->desc.wMaxPacketSize = __cpu_to_le16(maxpacketsize);
128+ urb->ep->desc.bmAttributes = endpoint_type;
129+ urb->ep->desc.bEndpointAddress = ((is_in ? USB_DIR_IN : USB_DIR_OUT) |
130+ epnum);
131+ urb->ep->desc.bInterval = interval;
132+}
133+
134+int usb_urb_submit(struct usb_hcd *hcd, struct urb *urb)
135+{
136+ const struct usb_urb_ops *ops = hcd->urb_ops;
137+ unsigned long timeout;
138+ int ret;
139+
140+ if (!ops)
141+ return -EINVAL;
142+
143+ ret = ops->urb_enqueue(hcd, urb, 0);
144+ if (ret < 0) {
145+ printf("Failed to enqueue URB to controller\n");
146+ return ret;
147+ }
148+
149+ timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
150+ do {
151+ if (ctrlc())
152+ return -EIO;
153+ ops->isr(0, hcd);
154+ } while (urb->status == -EINPROGRESS && get_timer(0) < timeout);
155+
156+ if (urb->status == -EINPROGRESS)
157+ ops->urb_dequeue(hcd, urb, -ETIME);
158+
159+ return urb->status;
160+}
161+
162+int usb_urb_submit_control(struct usb_hcd *hcd, struct urb *urb,
163+ struct usb_host_endpoint *hep,
164+ struct usb_device *dev, unsigned long pipe,
165+ void *buffer, int len, struct devrequest *setup,
166+ int interval, enum usb_device_speed speed)
167+{
168+ const struct usb_urb_ops *ops = hcd->urb_ops;
169+
170+ usb_urb_fill(urb, hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe, buffer,
171+ len, setup, 0);
172+
173+ /* Fix speed for non hub-attached devices */
174+ if (!usb_dev_get_parent(dev)) {
175+ dev->speed = speed;
176+ if (ops->hub_control)
177+ return ops->hub_control(hcd, dev, pipe, buffer, len,
178+ setup);
179+ }
180+
181+ return usb_urb_submit(hcd, urb);
182+}
183+
184+int usb_urb_submit_bulk(struct usb_hcd *hcd, struct urb *urb,
185+ struct usb_host_endpoint *hep, struct usb_device *dev,
186+ unsigned long pipe, void *buffer, int len)
187+{
188+ usb_urb_fill(urb, hep, dev, USB_ENDPOINT_XFER_BULK, pipe, buffer, len,
189+ NULL, 0);
190+
191+ return usb_urb_submit(hcd, urb);
192+}
193+
194+int usb_urb_submit_irq(struct usb_hcd *hcd, struct urb *urb,
195+ struct usb_host_endpoint *hep, struct usb_device *dev,
196+ unsigned long pipe, void *buffer, int len, int interval)
197+{
198+ usb_urb_fill(urb, hep, dev, USB_ENDPOINT_XFER_INT, pipe, buffer, len,
199+ NULL, interval);
200+
201+ return usb_urb_submit(hcd, urb);
202+}
203diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
204index f1fc93f3d403..3ccbc16da379 100644
205--- a/drivers/usb/host/r8a66597-hcd.c
206+++ b/drivers/usb/host/r8a66597-hcd.c
207@@ -14,6 +14,7 @@
208 #include <dm/device_compat.h>
209 #include <linux/delay.h>
210 #include <linux/iopoll.h>
211+#include <linux/usb/usb_urb_compat.h>
212 #include <power/regulator.h>
213
214 #include "r8a66597.h"
215@@ -24,35 +25,6 @@
216 #define R8A66597_DPRINT(...)
217 #endif
218
219-static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
220-{
221- struct udevice *parent = udev->dev->parent;
222-
223- /*
224- * When called from usb-uclass.c: usb_scan_device() udev->dev points
225- * to the parent udevice, not the actual udevice belonging to the
226- * udev as the device is not instantiated yet.
227- *
228- * If dev is an usb-bus, then we are called from usb_scan_device() for
229- * an usb-device plugged directly into the root port, return NULL.
230- */
231- if (device_get_uclass_id(udev->dev) == UCLASS_USB)
232- return NULL;
233-
234- /*
235- * If these 2 are not the same we are being called from
236- * usb_scan_device() and udev itself is the parent.
237- */
238- if (dev_get_parent_priv(udev->dev) != udev)
239- return udev;
240-
241- /* We are being called normally, use the parent pointer */
242- if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
243- return dev_get_parent_priv(parent);
244-
245- return NULL;
246-}
247-
248 static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
249 {
250 struct usb_device *parent = usb_dev_get_parent(dev);
251diff --git a/drivers/usb/musb-new/musb_core.c b/drivers/usb/musb-new/musb_core.c
252index 18d9bc805f8a..fc7af7484e4c 100644
253--- a/drivers/usb/musb-new/musb_core.c
254+++ b/drivers/usb/musb-new/musb_core.c
255@@ -89,9 +89,9 @@
256 #include <linux/usb/ch9.h>
257 #include <linux/usb/gadget.h>
258 #include <linux/usb/musb.h>
259+#include <linux/usb/usb_urb_compat.h>
260 #include <asm/io.h>
261 #include "linux-compat.h"
262-#include "usb-compat.h"
263 #endif
264
265 #include "musb_core.h"
266diff --git a/drivers/usb/musb-new/musb_host.c b/drivers/usb/musb-new/musb_host.c
267index acb2d40f3b5a..e5905d90d66f 100644
268--- a/drivers/usb/musb-new/musb_host.c
269+++ b/drivers/usb/musb-new/musb_host.c
270@@ -26,8 +26,8 @@
271 #include <dm/device_compat.h>
272 #include <usb.h>
273 #include <linux/bug.h>
274+#include <linux/usb/usb_urb_compat.h>
275 #include "linux-compat.h"
276-#include "usb-compat.h"
277 #endif
278
279 #include "musb_core.h"
280diff --git a/drivers/usb/musb-new/musb_host.h b/drivers/usb/musb-new/musb_host.h
281index afc8fa35a738..5a604bdb0cf2 100644
282--- a/drivers/usb/musb-new/musb_host.h
283+++ b/drivers/usb/musb-new/musb_host.h
284@@ -10,7 +10,7 @@
285 #ifndef _MUSB_HOST_H
286 #define _MUSB_HOST_H
287 #ifdef __UBOOT__
288-#include "usb-compat.h"
289+#include <linux/usb/usb_urb_compat.h>
290 #endif
291
292 static inline struct usb_hcd *musb_to_hcd(struct musb *musb)
293diff --git a/drivers/usb/musb-new/musb_uboot.c b/drivers/usb/musb-new/musb_uboot.c
294index 61ff68def2fa..d186facc7e02 100644
295--- a/drivers/usb/musb-new/musb_uboot.c
296+++ b/drivers/usb/musb-new/musb_uboot.c
297@@ -8,10 +8,10 @@
298 #include <linux/errno.h>
299 #include <linux/usb/ch9.h>
300 #include <linux/usb/gadget.h>
301+#include <linux/usb/usb_urb_compat.h>
302
303 #include <usb.h>
304 #include "linux-compat.h"
305-#include "usb-compat.h"
306 #include "musb_core.h"
307 #include "musb_host.h"
308 #include "musb_gadget.h"
309@@ -453,39 +453,3 @@ struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
310
311 return *musbp;
312 }
313-
314-#if CONFIG_IS_ENABLED(DM_USB)
315-struct usb_device *usb_dev_get_parent(struct usb_device *udev)
316-{
317- struct udevice *parent = udev->dev->parent;
318-
319- /*
320- * When called from usb-uclass.c: usb_scan_device() udev->dev points
321- * to the parent udevice, not the actual udevice belonging to the
322- * udev as the device is not instantiated yet.
323- *
324- * If dev is an usb-bus, then we are called from usb_scan_device() for
325- * an usb-device plugged directly into the root port, return NULL.
326- */
327- if (device_get_uclass_id(udev->dev) == UCLASS_USB)
328- return NULL;
329-
330- /*
331- * If these 2 are not the same we are being called from
332- * usb_scan_device() and udev itself is the parent.
333- */
334- if (dev_get_parent_priv(udev->dev) != udev)
335- return udev;
336-
337- /* We are being called normally, use the parent pointer */
338- if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
339- return dev_get_parent_priv(parent);
340-
341- return NULL;
342-}
343-#else
344-struct usb_device *usb_dev_get_parent(struct usb_device *udev)
345-{
346- return udev->parent;
347-}
348-#endif
349diff --git a/drivers/usb/musb-new/musb_uboot.h b/drivers/usb/musb-new/musb_uboot.h
350index 18282efccc9d..6b162f03b19e 100644
351--- a/drivers/usb/musb-new/musb_uboot.h
352+++ b/drivers/usb/musb-new/musb_uboot.h
353@@ -8,8 +8,8 @@
354 #define __MUSB_UBOOT_H__
355
356 #include <usb.h>
357+#include <linux/usb/usb_urb_compat.h>
358 #include "linux-compat.h"
359-#include "usb-compat.h"
360 #include "musb_core.h"
361
362 struct musb_host_data {
363diff --git a/drivers/usb/musb-new/usb-compat.h b/include/linux/usb/usb_urb_compat.h
364similarity index 60%
365rename from drivers/usb/musb-new/usb-compat.h
366rename to include/linux/usb/usb_urb_compat.h
367index df68c9220a7a..5ed96fa64e96 100644
368--- a/drivers/usb/musb-new/usb-compat.h
369+++ b/include/linux/usb/usb_urb_compat.h
370@@ -1,16 +1,31 @@
371-#ifndef __USB_COMPAT_H__
372-#define __USB_COMPAT_H__
373+#ifndef __USB_URB_COMPAT_H__
374+#define __USB_URB_COMPAT_H__
375
376-#include "usb.h"
377+#include <linux/compat.h>
378+#include <usb.h>
379
380 struct udevice;
381+struct urb;
382+struct usb_hcd;
383+
384+
385+struct usb_urb_ops {
386+ int (*urb_enqueue)(struct usb_hcd *hcd, struct urb *urb,
387+ gfp_t mem_flags);
388+ int (*urb_dequeue)(struct usb_hcd *hcd, struct urb *urb, int status);
389+ int (*hub_control)(struct usb_hcd *hcd, struct usb_device *dev,
390+ unsigned long pipe, void *buffer, int len,
391+ struct devrequest *setup);
392+ irqreturn_t (*isr)(int irq, void *priv);
393+};
394
395 struct usb_hcd {
396 void *hcd_priv;
397+ const struct usb_urb_ops *urb_ops;
398 };
399
400 struct usb_host_endpoint {
401- struct usb_endpoint_descriptor desc;
402+ struct usb_endpoint_descriptor desc;
403 struct list_head urb_list;
404 void *hcpriv;
405 };
406@@ -23,8 +38,6 @@ struct usb_host_endpoint {
407 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
408 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
409
410-struct urb;
411-
412 typedef void (*usb_complete_t)(struct urb *);
413
414 struct urb {
415@@ -76,4 +89,25 @@ static inline int usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd,
416 */
417 struct usb_device *usb_dev_get_parent(struct usb_device *udev);
418
419+int usb_urb_submit_control(struct usb_hcd *hcd, struct urb *urb,
420+ struct usb_host_endpoint *hep,
421+ struct usb_device *dev, unsigned long pipe,
422+ void *buffer, int len, struct devrequest *setup,
423+ int interval, enum usb_device_speed speed);
424+
425+int usb_urb_submit_bulk(struct usb_hcd *hcd, struct urb *urb,
426+ struct usb_host_endpoint *hep, struct usb_device *dev,
427+ unsigned long pipe, void *buffer, int len);
428+
429+int usb_urb_submit_irq(struct usb_hcd *hcd, struct urb *urb,
430+ struct usb_host_endpoint *hep, struct usb_device *dev,
431+ unsigned long pipe, void *buffer, int len, int interval);
432+
433+void usb_urb_fill(struct urb *urb, struct usb_host_endpoint *hep,
434+ struct usb_device *dev, int endpoint_type,
435+ unsigned long pipe, void *buffer, int len,
436+ struct devrequest *setup, int interval);
437+
438+int usb_urb_submit(struct usb_hcd *hcd, struct urb *urb);
439+
440 #endif /* __USB_COMPAT_H__ */
441diff --git a/include/usb_defs.h b/include/usb_defs.h
442index 6dd2c997f9b3..ec00161710a5 100644
443--- a/include/usb_defs.h
444+++ b/include/usb_defs.h
445@@ -81,6 +81,32 @@
446 #define EndpointOutRequest \
447 ((USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8)
448
449+/* class requests from the USB 2.0 hub spec, table 11-15 */
450+#define HUB_CLASS_REQ(dir, type, request) ((((dir) | (type)) << 8) | (request))
451+/* GetBusState and SetHubDescriptor are optional, omitted */
452+#define ClearHubFeature HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_HUB, \
453+ USB_REQ_CLEAR_FEATURE)
454+#define ClearPortFeature HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, \
455+ USB_REQ_CLEAR_FEATURE)
456+#define GetHubDescriptor HUB_CLASS_REQ(USB_DIR_IN, USB_RT_HUB, \
457+ USB_REQ_GET_DESCRIPTOR)
458+#define GetHubStatus HUB_CLASS_REQ(USB_DIR_IN, USB_RT_HUB, \
459+ USB_REQ_GET_STATUS)
460+#define GetPortStatus HUB_CLASS_REQ(USB_DIR_IN, USB_RT_PORT, \
461+ USB_REQ_GET_STATUS)
462+#define SetHubFeature HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_HUB, \
463+ USB_REQ_SET_FEATURE)
464+#define SetPortFeature HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, \
465+ USB_REQ_SET_FEATURE)
466+#define ClearTTBuffer HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, \
467+ HUB_CLEAR_TT_BUFFER)
468+#define ResetTT HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, \
469+ HUB_RESET_TT)
470+#define GetTTState HUB_CLASS_REQ(USB_DIR_IN, USB_RT_PORT, \
471+ HUB_GET_TT_STATE)
472+#define StopTT HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, \
473+ HUB_STOP_TT)
474+
475 /* Descriptor types */
476 #define USB_DT_DEVICE 0x01
477 #define USB_DT_CONFIG 0x02
478@@ -289,10 +315,16 @@
479 #define USB_SS_PORT_STAT_C_CONFIG_ERROR 0x0080
480
481 /* wHubCharacteristics (masks) */
482+#define HUB_CHAR_COMMON_OCPM 0x0000 /* All ports Over-Current reporting */
483+#define HUB_CHAR_INDV_PORT_LPSM 0x0001 /* per-port power control */
484+#define HUB_CHAR_NO_LPSM 0x0002 /* no power switching */
485 #define HUB_CHAR_LPSM 0x0003
486 #define HUB_CHAR_COMPOUND 0x0004
487+#define HUB_CHAR_INDV_PORT_OCPM 0x0008 /* per-port Over-current reporting */
488+#define HUB_CHAR_NO_OCPM 0x0010 /* No Over-current Protection support */
489 #define HUB_CHAR_OCPM 0x0018
490 #define HUB_CHAR_TTTT 0x0060 /* TT Think Time mask */
491+#define HUB_CHAR_PORTIND 0x0080 /* per-port indicators (LEDs) */
492
493 /*
494 * Hub Status & Hub Change bit masks
495--
Patrick Williams92b42cb2022-09-03 06:53:57 -05004962.37.1
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400497