aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-03-07 19:47:36 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-03-07 19:47:36 -0500
commit7338a0659365ec0fbdc6f79de8b9fba2280fd155 (patch)
tree876358e9d7187cad93d41b9e9918653fc24fa38a
parentc9050b64940a1b47dfa623630431fec400edbf33 (diff)
parent4b76e14d95f7b69e71eabc002dcb0dcb9ebb5340 (diff)
Merge tag 'usb-for-v3.15' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb into usb-next
Felipe writes: usb: patches for v3.15 another substantial pull request with new features all over the place. dwc3 got a bit closer towards hibernation support with after a few patches re-factoring code to be reused for hibernation. Also in dwc3 two new workarounds for known silicon bugs have been implemented, some randconfig build errors have been fixed, and it was taught about the new generic phy layer. MUSB on AM335x now supports isochronous transfers thanks to George Cherian's work. The atmel_usba driver got two crash fixes: one when no endpoint was specified in DeviceTree data and another when stopping the UDC in DEBUG builds. Function FS got a much needed fix to ffs_epfile_io() which was copying too much data to userspace in some cases. The printer gadget got a fix for a possible deadlock and plugged a memory leak. Ethernet drivers now use NAPI for RX which gives improved throughput. Other than that, the usual miscelaneous fixes, cleanups, and the like. Signed-of-by: Felipe Balbi <balbi@ti.com>
-rw-r--r--Documentation/devicetree/bindings/phy/ti-phy.txt86
-rw-r--r--Documentation/devicetree/bindings/usb/dwc3.txt6
-rw-r--r--Documentation/devicetree/bindings/usb/mxs-phy.txt8
-rw-r--r--Documentation/devicetree/bindings/usb/omap-usb.txt24
-rw-r--r--Documentation/devicetree/bindings/usb/usb-phy.txt48
-rw-r--r--drivers/usb/dwc3/core.c251
-rw-r--r--drivers/usb/dwc3/core.h105
-rw-r--r--drivers/usb/dwc3/dwc3-omap.c5
-rw-r--r--drivers/usb/dwc3/gadget.c183
-rw-r--r--drivers/usb/dwc3/gadget.h12
-rw-r--r--drivers/usb/gadget/Kconfig1
-rw-r--r--drivers/usb/gadget/at91_udc.c14
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c16
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.h2
-rw-r--r--drivers/usb/gadget/f_fs.c612
-rw-r--r--drivers/usb/gadget/gr_udc.c10
-rw-r--r--drivers/usb/gadget/printer.c8
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c143
-rw-r--r--drivers/usb/gadget/s3c-hsudc.c1
-rw-r--r--drivers/usb/gadget/u_ether.c101
-rw-r--r--drivers/usb/gadget/u_fs.h30
-rw-r--r--drivers/usb/musb/Kconfig2
-rw-r--r--drivers/usb/musb/musb_core.c5
-rw-r--r--drivers/usb/musb/musb_cppi41.c69
-rw-r--r--drivers/usb/musb/musb_dsps.c58
-rw-r--r--drivers/usb/musb/musb_host.c30
-rw-r--r--drivers/usb/phy/phy-fsm-usb.c9
-rw-r--r--drivers/usb/phy/phy-mxs-usb.c310
-rw-r--r--drivers/usb/phy/phy-rcar-gen2-usb.c6
-rw-r--r--include/linux/usb/phy.h16
-rw-r--r--include/uapi/linux/usb/functionfs.h44
31 files changed, 1739 insertions, 476 deletions
diff --git a/Documentation/devicetree/bindings/phy/ti-phy.txt b/Documentation/devicetree/bindings/phy/ti-phy.txt
new file mode 100644
index 000000000000..788fb0fa3762
--- /dev/null
+++ b/Documentation/devicetree/bindings/phy/ti-phy.txt
@@ -0,0 +1,86 @@
1TI PHY: DT DOCUMENTATION FOR PHYs in TI PLATFORMs
2
3OMAP CONTROL PHY
4
5Required properties:
6 - compatible: Should be one of
7 "ti,control-phy-otghs" - if it has otghs_control mailbox register as on OMAP4.
8 "ti,control-phy-usb2" - if it has Power down bit in control_dev_conf register
9 e.g. USB2_PHY on OMAP5.
10 "ti,control-phy-pipe3" - if it has DPLL and individual Rx & Tx power control
11 e.g. USB3 PHY and SATA PHY on OMAP5.
12 "ti,control-phy-usb2-dra7" - if it has power down register like USB2 PHY on
13 DRA7 platform.
14 "ti,control-phy-usb2-am437" - if it has power down register like USB2 PHY on
15 AM437 platform.
16 - reg : Address and length of the register set for the device. It contains
17 the address of "otghs_control" for control-phy-otghs or "power" register
18 for other types.
19 - reg-names: should be "otghs_control" control-phy-otghs and "power" for
20 other types.
21
22omap_control_usb: omap-control-usb@4a002300 {
23 compatible = "ti,control-phy-otghs";
24 reg = <0x4a00233c 0x4>;
25 reg-names = "otghs_control";
26};
27
28OMAP USB2 PHY
29
30Required properties:
31 - compatible: Should be "ti,omap-usb2"
32 - reg : Address and length of the register set for the device.
33 - #phy-cells: determine the number of cells that should be given in the
34 phandle while referencing this phy.
35
36Optional properties:
37 - ctrl-module : phandle of the control module used by PHY driver to power on
38 the PHY.
39
40This is usually a subnode of ocp2scp to which it is connected.
41
42usb2phy@4a0ad080 {
43 compatible = "ti,omap-usb2";
44 reg = <0x4a0ad080 0x58>;
45 ctrl-module = <&omap_control_usb>;
46 #phy-cells = <0>;
47};
48
49TI PIPE3 PHY
50
51Required properties:
52 - compatible: Should be "ti,phy-usb3" or "ti,phy-pipe3-sata".
53 "ti,omap-usb3" is deprecated.
54 - reg : Address and length of the register set for the device.
55 - reg-names: The names of the register addresses corresponding to the registers
56 filled in "reg".
57 - #phy-cells: determine the number of cells that should be given in the
58 phandle while referencing this phy.
59 - clocks: a list of phandles and clock-specifier pairs, one for each entry in
60 clock-names.
61 - clock-names: should include:
62 * "wkupclk" - wakeup clock.
63 * "sysclk" - system clock.
64 * "refclk" - reference clock.
65
66Optional properties:
67 - ctrl-module : phandle of the control module used by PHY driver to power on
68 the PHY.
69
70This is usually a subnode of ocp2scp to which it is connected.
71
72usb3phy@4a084400 {
73 compatible = "ti,phy-usb3";
74 reg = <0x4a084400 0x80>,
75 <0x4a084800 0x64>,
76 <0x4a084c00 0x40>;
77 reg-names = "phy_rx", "phy_tx", "pll_ctrl";
78 ctrl-module = <&omap_control_usb>;
79 #phy-cells = <0>;
80 clocks = <&usb_phy_cm_clk32k>,
81 <&sys_clkin>,
82 <&usb_otg_ss_refclk960m>;
83 clock-names = "wkupclk",
84 "sysclk",
85 "refclk";
86};
diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt
index e807635f9e1c..471366d6a129 100644
--- a/Documentation/devicetree/bindings/usb/dwc3.txt
+++ b/Documentation/devicetree/bindings/usb/dwc3.txt
@@ -6,11 +6,13 @@ Required properties:
6 - compatible: must be "snps,dwc3" 6 - compatible: must be "snps,dwc3"
7 - reg : Address and length of the register set for the device 7 - reg : Address and length of the register set for the device
8 - interrupts: Interrupts used by the dwc3 controller. 8 - interrupts: Interrupts used by the dwc3 controller.
9
10Optional properties:
9 - usb-phy : array of phandle for the PHY device. The first element 11 - usb-phy : array of phandle for the PHY device. The first element
10 in the array is expected to be a handle to the USB2/HS PHY and 12 in the array is expected to be a handle to the USB2/HS PHY and
11 the second element is expected to be a handle to the USB3/SS PHY 13 the second element is expected to be a handle to the USB3/SS PHY
12 14 - phys: from the *Generic PHY* bindings
13Optional properties: 15 - phy-names: from the *Generic PHY* bindings
14 - tx-fifo-resize: determines if the FIFO *has* to be reallocated. 16 - tx-fifo-resize: determines if the FIFO *has* to be reallocated.
15 17
16This is usually a subnode to DWC3 glue to which it is connected. 18This is usually a subnode to DWC3 glue to which it is connected.
diff --git a/Documentation/devicetree/bindings/usb/mxs-phy.txt b/Documentation/devicetree/bindings/usb/mxs-phy.txt
index 5835b27146ea..cef181a9d8bd 100644
--- a/Documentation/devicetree/bindings/usb/mxs-phy.txt
+++ b/Documentation/devicetree/bindings/usb/mxs-phy.txt
@@ -1,13 +1,19 @@
1* Freescale MXS USB Phy Device 1* Freescale MXS USB Phy Device
2 2
3Required properties: 3Required properties:
4- compatible: Should be "fsl,imx23-usbphy" 4- compatible: should contain:
5 * "fsl,imx23-usbphy" for imx23 and imx28
6 * "fsl,imx6q-usbphy" for imx6dq and imx6dl
7 * "fsl,imx6sl-usbphy" for imx6sl
8 "fsl,imx23-usbphy" is still a fallback for other strings
5- reg: Should contain registers location and length 9- reg: Should contain registers location and length
6- interrupts: Should contain phy interrupt 10- interrupts: Should contain phy interrupt
11- fsl,anatop: phandle for anatop register, it is only for imx6 SoC series
7 12
8Example: 13Example:
9usbphy1: usbphy@020c9000 { 14usbphy1: usbphy@020c9000 {
10 compatible = "fsl,imx6q-usbphy", "fsl,imx23-usbphy"; 15 compatible = "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
11 reg = <0x020c9000 0x1000>; 16 reg = <0x020c9000 0x1000>;
12 interrupts = <0 44 0x04>; 17 interrupts = <0 44 0x04>;
18 fsl,anatop = <&anatop>;
13}; 19};
diff --git a/Documentation/devicetree/bindings/usb/omap-usb.txt b/Documentation/devicetree/bindings/usb/omap-usb.txt
index c495135115cb..38b2faec4199 100644
--- a/Documentation/devicetree/bindings/usb/omap-usb.txt
+++ b/Documentation/devicetree/bindings/usb/omap-usb.txt
@@ -76,27 +76,3 @@ omap_dwc3 {
76 ranges; 76 ranges;
77}; 77};
78 78
79OMAP CONTROL USB
80
81Required properties:
82 - compatible: Should be one of
83 "ti,control-phy-otghs" - if it has otghs_control mailbox register as on OMAP4.
84 "ti,control-phy-usb2" - if it has Power down bit in control_dev_conf register
85 e.g. USB2_PHY on OMAP5.
86 "ti,control-phy-pipe3" - if it has DPLL and individual Rx & Tx power control
87 e.g. USB3 PHY and SATA PHY on OMAP5.
88 "ti,control-phy-dra7usb2" - if it has power down register like USB2 PHY on
89 DRA7 platform.
90 "ti,control-phy-am437usb2" - if it has power down register like USB2 PHY on
91 AM437 platform.
92 - reg : Address and length of the register set for the device. It contains
93 the address of "otghs_control" for control-phy-otghs or "power" register
94 for other types.
95 - reg-names: should be "otghs_control" control-phy-otghs and "power" for
96 other types.
97
98omap_control_usb: omap-control-usb@4a002300 {
99 compatible = "ti,control-phy-otghs";
100 reg = <0x4a00233c 0x4>;
101 reg-names = "otghs_control";
102};
diff --git a/Documentation/devicetree/bindings/usb/usb-phy.txt b/Documentation/devicetree/bindings/usb/usb-phy.txt
deleted file mode 100644
index c0245c888982..000000000000
--- a/Documentation/devicetree/bindings/usb/usb-phy.txt
+++ /dev/null
@@ -1,48 +0,0 @@
1USB PHY
2
3OMAP USB2 PHY
4
5Required properties:
6 - compatible: Should be "ti,omap-usb2"
7 - reg : Address and length of the register set for the device.
8 - #phy-cells: determine the number of cells that should be given in the
9 phandle while referencing this phy.
10
11Optional properties:
12 - ctrl-module : phandle of the control module used by PHY driver to power on
13 the PHY.
14
15This is usually a subnode of ocp2scp to which it is connected.
16
17usb2phy@4a0ad080 {
18 compatible = "ti,omap-usb2";
19 reg = <0x4a0ad080 0x58>;
20 ctrl-module = <&omap_control_usb>;
21 #phy-cells = <0>;
22};
23
24OMAP USB3 PHY
25
26Required properties:
27 - compatible: Should be "ti,omap-usb3"
28 - reg : Address and length of the register set for the device.
29 - reg-names: The names of the register addresses corresponding to the registers
30 filled in "reg".
31 - #phy-cells: determine the number of cells that should be given in the
32 phandle while referencing this phy.
33
34Optional properties:
35 - ctrl-module : phandle of the control module used by PHY driver to power on
36 the PHY.
37
38This is usually a subnode of ocp2scp to which it is connected.
39
40usb3phy@4a084400 {
41 compatible = "ti,omap-usb3";
42 reg = <0x4a084400 0x80>,
43 <0x4a084800 0x64>,
44 <0x4a084c00 0x40>;
45 reg-names = "phy_rx", "phy_tx", "pll_ctrl";
46 ctrl-module = <&omap_control_usb>;
47 #phy-cells = <0>;
48};
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index a49217ae3533..d001417e8e37 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -61,9 +61,10 @@ void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
61 * dwc3_core_soft_reset - Issues core soft reset and PHY reset 61 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
62 * @dwc: pointer to our context structure 62 * @dwc: pointer to our context structure
63 */ 63 */
64static void dwc3_core_soft_reset(struct dwc3 *dwc) 64static int dwc3_core_soft_reset(struct dwc3 *dwc)
65{ 65{
66 u32 reg; 66 u32 reg;
67 int ret;
67 68
68 /* Before Resetting PHY, put Core in Reset */ 69 /* Before Resetting PHY, put Core in Reset */
69 reg = dwc3_readl(dwc->regs, DWC3_GCTL); 70 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
@@ -82,6 +83,15 @@ static void dwc3_core_soft_reset(struct dwc3 *dwc)
82 83
83 usb_phy_init(dwc->usb2_phy); 84 usb_phy_init(dwc->usb2_phy);
84 usb_phy_init(dwc->usb3_phy); 85 usb_phy_init(dwc->usb3_phy);
86 ret = phy_init(dwc->usb2_generic_phy);
87 if (ret < 0)
88 return ret;
89
90 ret = phy_init(dwc->usb3_generic_phy);
91 if (ret < 0) {
92 phy_exit(dwc->usb2_generic_phy);
93 return ret;
94 }
85 mdelay(100); 95 mdelay(100);
86 96
87 /* Clear USB3 PHY reset */ 97 /* Clear USB3 PHY reset */
@@ -100,6 +110,8 @@ static void dwc3_core_soft_reset(struct dwc3 *dwc)
100 reg = dwc3_readl(dwc->regs, DWC3_GCTL); 110 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
101 reg &= ~DWC3_GCTL_CORESOFTRESET; 111 reg &= ~DWC3_GCTL_CORESOFTRESET;
102 dwc3_writel(dwc->regs, DWC3_GCTL, reg); 112 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
113
114 return 0;
103} 115}
104 116
105/** 117/**
@@ -242,6 +254,90 @@ static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
242 } 254 }
243} 255}
244 256
257static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
258{
259 if (!dwc->has_hibernation)
260 return 0;
261
262 if (!dwc->nr_scratch)
263 return 0;
264
265 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
266 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
267 if (!dwc->scratchbuf)
268 return -ENOMEM;
269
270 return 0;
271}
272
273static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
274{
275 dma_addr_t scratch_addr;
276 u32 param;
277 int ret;
278
279 if (!dwc->has_hibernation)
280 return 0;
281
282 if (!dwc->nr_scratch)
283 return 0;
284
285 /* should never fall here */
286 if (!WARN_ON(dwc->scratchbuf))
287 return 0;
288
289 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
290 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
291 DMA_BIDIRECTIONAL);
292 if (dma_mapping_error(dwc->dev, scratch_addr)) {
293 dev_err(dwc->dev, "failed to map scratch buffer\n");
294 ret = -EFAULT;
295 goto err0;
296 }
297
298 dwc->scratch_addr = scratch_addr;
299
300 param = lower_32_bits(scratch_addr);
301
302 ret = dwc3_send_gadget_generic_command(dwc,
303 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
304 if (ret < 0)
305 goto err1;
306
307 param = upper_32_bits(scratch_addr);
308
309 ret = dwc3_send_gadget_generic_command(dwc,
310 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
311 if (ret < 0)
312 goto err1;
313
314 return 0;
315
316err1:
317 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
318 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
319
320err0:
321 return ret;
322}
323
324static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
325{
326 if (!dwc->has_hibernation)
327 return;
328
329 if (!dwc->nr_scratch)
330 return;
331
332 /* should never fall here */
333 if (!WARN_ON(dwc->scratchbuf))
334 return;
335
336 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
337 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
338 kfree(dwc->scratchbuf);
339}
340
245static void dwc3_core_num_eps(struct dwc3 *dwc) 341static void dwc3_core_num_eps(struct dwc3 *dwc)
246{ 342{
247 struct dwc3_hwparams *parms = &dwc->hwparams; 343 struct dwc3_hwparams *parms = &dwc->hwparams;
@@ -277,6 +373,7 @@ static void dwc3_cache_hwparams(struct dwc3 *dwc)
277static int dwc3_core_init(struct dwc3 *dwc) 373static int dwc3_core_init(struct dwc3 *dwc)
278{ 374{
279 unsigned long timeout; 375 unsigned long timeout;
376 u32 hwparams4 = dwc->hwparams.hwparams4;
280 u32 reg; 377 u32 reg;
281 int ret; 378 int ret;
282 379
@@ -306,7 +403,9 @@ static int dwc3_core_init(struct dwc3 *dwc)
306 cpu_relax(); 403 cpu_relax();
307 } while (true); 404 } while (true);
308 405
309 dwc3_core_soft_reset(dwc); 406 ret = dwc3_core_soft_reset(dwc);
407 if (ret)
408 goto err0;
310 409
311 reg = dwc3_readl(dwc->regs, DWC3_GCTL); 410 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
312 reg &= ~DWC3_GCTL_SCALEDOWN_MASK; 411 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
@@ -314,7 +413,29 @@ static int dwc3_core_init(struct dwc3 *dwc)
314 413
315 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { 414 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
316 case DWC3_GHWPARAMS1_EN_PWROPT_CLK: 415 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
317 reg &= ~DWC3_GCTL_DSBLCLKGTNG; 416 /**
417 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
418 * issue which would cause xHCI compliance tests to fail.
419 *
420 * Because of that we cannot enable clock gating on such
421 * configurations.
422 *
423 * Refers to:
424 *
425 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
426 * SOF/ITP Mode Used
427 */
428 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
429 dwc->dr_mode == USB_DR_MODE_OTG) &&
430 (dwc->revision >= DWC3_REVISION_210A &&
431 dwc->revision <= DWC3_REVISION_250A))
432 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
433 else
434 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
435 break;
436 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
437 /* enable hibernation here */
438 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
318 break; 439 break;
319 default: 440 default:
320 dev_dbg(dwc->dev, "No power optimization available\n"); 441 dev_dbg(dwc->dev, "No power optimization available\n");
@@ -333,16 +454,36 @@ static int dwc3_core_init(struct dwc3 *dwc)
333 454
334 dwc3_writel(dwc->regs, DWC3_GCTL, reg); 455 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
335 456
457 ret = dwc3_alloc_scratch_buffers(dwc);
458 if (ret)
459 goto err1;
460
461 ret = dwc3_setup_scratch_buffers(dwc);
462 if (ret)
463 goto err2;
464
336 return 0; 465 return 0;
337 466
467err2:
468 dwc3_free_scratch_buffers(dwc);
469
470err1:
471 usb_phy_shutdown(dwc->usb2_phy);
472 usb_phy_shutdown(dwc->usb3_phy);
473 phy_exit(dwc->usb2_generic_phy);
474 phy_exit(dwc->usb3_generic_phy);
475
338err0: 476err0:
339 return ret; 477 return ret;
340} 478}
341 479
342static void dwc3_core_exit(struct dwc3 *dwc) 480static void dwc3_core_exit(struct dwc3 *dwc)
343{ 481{
482 dwc3_free_scratch_buffers(dwc);
344 usb_phy_shutdown(dwc->usb2_phy); 483 usb_phy_shutdown(dwc->usb2_phy);
345 usb_phy_shutdown(dwc->usb3_phy); 484 usb_phy_shutdown(dwc->usb3_phy);
485 phy_exit(dwc->usb2_generic_phy);
486 phy_exit(dwc->usb3_generic_phy);
346} 487}
347 488
348#define DWC3_ALIGN_MASK (16 - 1) 489#define DWC3_ALIGN_MASK (16 - 1)
@@ -411,32 +552,52 @@ static int dwc3_probe(struct platform_device *pdev)
411 552
412 if (IS_ERR(dwc->usb2_phy)) { 553 if (IS_ERR(dwc->usb2_phy)) {
413 ret = PTR_ERR(dwc->usb2_phy); 554 ret = PTR_ERR(dwc->usb2_phy);
414 555 if (ret == -ENXIO || ret == -ENODEV) {
415 /* 556 dwc->usb2_phy = NULL;
416 * if -ENXIO is returned, it means PHY layer wasn't 557 } else if (ret == -EPROBE_DEFER) {
417 * enabled, so it makes no sense to return -EPROBE_DEFER
418 * in that case, since no PHY driver will ever probe.
419 */
420 if (ret == -ENXIO)
421 return ret; 558 return ret;
422 559 } else {
423 dev_err(dev, "no usb2 phy configured\n"); 560 dev_err(dev, "no usb2 phy configured\n");
424 return -EPROBE_DEFER; 561 return ret;
562 }
425 } 563 }
426 564
427 if (IS_ERR(dwc->usb3_phy)) { 565 if (IS_ERR(dwc->usb3_phy)) {
428 ret = PTR_ERR(dwc->usb3_phy); 566 ret = PTR_ERR(dwc->usb3_phy);
567 if (ret == -ENXIO || ret == -ENODEV) {
568 dwc->usb3_phy = NULL;
569 } else if (ret == -EPROBE_DEFER) {
570 return ret;
571 } else {
572 dev_err(dev, "no usb3 phy configured\n");
573 return ret;
574 }
575 }
429 576
430 /* 577 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
431 * if -ENXIO is returned, it means PHY layer wasn't 578 if (IS_ERR(dwc->usb2_generic_phy)) {
432 * enabled, so it makes no sense to return -EPROBE_DEFER 579 ret = PTR_ERR(dwc->usb2_generic_phy);
433 * in that case, since no PHY driver will ever probe. 580 if (ret == -ENOSYS || ret == -ENODEV) {
434 */ 581 dwc->usb2_generic_phy = NULL;
435 if (ret == -ENXIO) 582 } else if (ret == -EPROBE_DEFER) {
583 return ret;
584 } else {
585 dev_err(dev, "no usb2 phy configured\n");
436 return ret; 586 return ret;
587 }
588 }
437 589
438 dev_err(dev, "no usb3 phy configured\n"); 590 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
439 return -EPROBE_DEFER; 591 if (IS_ERR(dwc->usb3_generic_phy)) {
592 ret = PTR_ERR(dwc->usb3_generic_phy);
593 if (ret == -ENOSYS || ret == -ENODEV) {
594 dwc->usb3_generic_phy = NULL;
595 } else if (ret == -EPROBE_DEFER) {
596 return ret;
597 } else {
598 dev_err(dev, "no usb3 phy configured\n");
599 return ret;
600 }
440 } 601 }
441 602
442 dwc->xhci_resources[0].start = res->start; 603 dwc->xhci_resources[0].start = res->start;
@@ -479,6 +640,14 @@ static int dwc3_probe(struct platform_device *pdev)
479 goto err0; 640 goto err0;
480 } 641 }
481 642
643 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
644 dwc->dr_mode = USB_DR_MODE_HOST;
645 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
646 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
647
648 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
649 dwc->dr_mode = USB_DR_MODE_OTG;
650
482 ret = dwc3_core_init(dwc); 651 ret = dwc3_core_init(dwc);
483 if (ret) { 652 if (ret) {
484 dev_err(dev, "failed to initialize core\n"); 653 dev_err(dev, "failed to initialize core\n");
@@ -487,21 +656,20 @@ static int dwc3_probe(struct platform_device *pdev)
487 656
488 usb_phy_set_suspend(dwc->usb2_phy, 0); 657 usb_phy_set_suspend(dwc->usb2_phy, 0);
489 usb_phy_set_suspend(dwc->usb3_phy, 0); 658 usb_phy_set_suspend(dwc->usb3_phy, 0);
659 ret = phy_power_on(dwc->usb2_generic_phy);
660 if (ret < 0)
661 goto err1;
662
663 ret = phy_power_on(dwc->usb3_generic_phy);
664 if (ret < 0)
665 goto err_usb2phy_power;
490 666
491 ret = dwc3_event_buffers_setup(dwc); 667 ret = dwc3_event_buffers_setup(dwc);
492 if (ret) { 668 if (ret) {
493 dev_err(dwc->dev, "failed to setup event buffers\n"); 669 dev_err(dwc->dev, "failed to setup event buffers\n");
494 goto err1; 670 goto err_usb3phy_power;
495 } 671 }
496 672
497 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
498 dwc->dr_mode = USB_DR_MODE_HOST;
499 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
500 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
501
502 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
503 dwc->dr_mode = USB_DR_MODE_OTG;
504
505 switch (dwc->dr_mode) { 673 switch (dwc->dr_mode) {
506 case USB_DR_MODE_PERIPHERAL: 674 case USB_DR_MODE_PERIPHERAL:
507 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); 675 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
@@ -568,6 +736,12 @@ err3:
568err2: 736err2:
569 dwc3_event_buffers_cleanup(dwc); 737 dwc3_event_buffers_cleanup(dwc);
570 738
739err_usb3phy_power:
740 phy_power_off(dwc->usb3_generic_phy);
741
742err_usb2phy_power:
743 phy_power_off(dwc->usb2_generic_phy);
744
571err1: 745err1:
572 usb_phy_set_suspend(dwc->usb2_phy, 1); 746 usb_phy_set_suspend(dwc->usb2_phy, 1);
573 usb_phy_set_suspend(dwc->usb3_phy, 1); 747 usb_phy_set_suspend(dwc->usb3_phy, 1);
@@ -585,6 +759,8 @@ static int dwc3_remove(struct platform_device *pdev)
585 759
586 usb_phy_set_suspend(dwc->usb2_phy, 1); 760 usb_phy_set_suspend(dwc->usb2_phy, 1);
587 usb_phy_set_suspend(dwc->usb3_phy, 1); 761 usb_phy_set_suspend(dwc->usb3_phy, 1);
762 phy_power_off(dwc->usb2_generic_phy);
763 phy_power_off(dwc->usb3_generic_phy);
588 764
589 pm_runtime_put_sync(&pdev->dev); 765 pm_runtime_put_sync(&pdev->dev);
590 pm_runtime_disable(&pdev->dev); 766 pm_runtime_disable(&pdev->dev);
@@ -682,6 +858,8 @@ static int dwc3_suspend(struct device *dev)
682 858
683 usb_phy_shutdown(dwc->usb3_phy); 859 usb_phy_shutdown(dwc->usb3_phy);
684 usb_phy_shutdown(dwc->usb2_phy); 860 usb_phy_shutdown(dwc->usb2_phy);
861 phy_exit(dwc->usb2_generic_phy);
862 phy_exit(dwc->usb3_generic_phy);
685 863
686 return 0; 864 return 0;
687} 865}
@@ -690,9 +868,17 @@ static int dwc3_resume(struct device *dev)
690{ 868{
691 struct dwc3 *dwc = dev_get_drvdata(dev); 869 struct dwc3 *dwc = dev_get_drvdata(dev);
692 unsigned long flags; 870 unsigned long flags;
871 int ret;
693 872
694 usb_phy_init(dwc->usb3_phy); 873 usb_phy_init(dwc->usb3_phy);
695 usb_phy_init(dwc->usb2_phy); 874 usb_phy_init(dwc->usb2_phy);
875 ret = phy_init(dwc->usb2_generic_phy);
876 if (ret < 0)
877 return ret;
878
879 ret = phy_init(dwc->usb3_generic_phy);
880 if (ret < 0)
881 goto err_usb2phy_init;
696 882
697 spin_lock_irqsave(&dwc->lock, flags); 883 spin_lock_irqsave(&dwc->lock, flags);
698 884
@@ -716,6 +902,11 @@ static int dwc3_resume(struct device *dev)
716 pm_runtime_enable(dev); 902 pm_runtime_enable(dev);
717 903
718 return 0; 904 return 0;
905
906err_usb2phy_init:
907 phy_exit(dwc->usb2_generic_phy);
908
909 return ret;
719} 910}
720 911
721static const struct dev_pm_ops dwc3_dev_pm_ops = { 912static const struct dev_pm_ops dwc3_dev_pm_ops = {
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index f8af8d44af85..57332e3768e4 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -31,11 +31,14 @@
31#include <linux/usb/gadget.h> 31#include <linux/usb/gadget.h>
32#include <linux/usb/otg.h> 32#include <linux/usb/otg.h>
33 33
34#include <linux/phy/phy.h>
35
34/* Global constants */ 36/* Global constants */
35#define DWC3_EP0_BOUNCE_SIZE 512 37#define DWC3_EP0_BOUNCE_SIZE 512
36#define DWC3_ENDPOINTS_NUM 32 38#define DWC3_ENDPOINTS_NUM 32
37#define DWC3_XHCI_RESOURCES_NUM 2 39#define DWC3_XHCI_RESOURCES_NUM 2
38 40
41#define DWC3_SCRATCHBUF_SIZE 4096 /* each buffer is assumed to be 4KiB */
39#define DWC3_EVENT_SIZE 4 /* bytes */ 42#define DWC3_EVENT_SIZE 4 /* bytes */
40#define DWC3_EVENT_MAX_NUM 64 /* 2 events/endpoint */ 43#define DWC3_EVENT_MAX_NUM 64 /* 2 events/endpoint */
41#define DWC3_EVENT_BUFFERS_SIZE (DWC3_EVENT_SIZE * DWC3_EVENT_MAX_NUM) 44#define DWC3_EVENT_BUFFERS_SIZE (DWC3_EVENT_SIZE * DWC3_EVENT_MAX_NUM)
@@ -157,6 +160,7 @@
157#define DWC3_GCTL_PRTCAP_OTG 3 160#define DWC3_GCTL_PRTCAP_OTG 3
158 161
159#define DWC3_GCTL_CORESOFTRESET (1 << 11) 162#define DWC3_GCTL_CORESOFTRESET (1 << 11)
163#define DWC3_GCTL_SOFITPSYNC (1 << 10)
160#define DWC3_GCTL_SCALEDOWN(n) ((n) << 4) 164#define DWC3_GCTL_SCALEDOWN(n) ((n) << 4)
161#define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3) 165#define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3)
162#define DWC3_GCTL_DISSCRAMBLE (1 << 3) 166#define DWC3_GCTL_DISSCRAMBLE (1 << 3)
@@ -318,7 +322,7 @@
318/* Device Endpoint Command Register */ 322/* Device Endpoint Command Register */
319#define DWC3_DEPCMD_PARAM_SHIFT 16 323#define DWC3_DEPCMD_PARAM_SHIFT 16
320#define DWC3_DEPCMD_PARAM(x) ((x) << DWC3_DEPCMD_PARAM_SHIFT) 324#define DWC3_DEPCMD_PARAM(x) ((x) << DWC3_DEPCMD_PARAM_SHIFT)
321#define DWC3_DEPCMD_GET_RSC_IDX(x) (((x) >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f) 325#define DWC3_DEPCMD_GET_RSC_IDX(x) (((x) >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
322#define DWC3_DEPCMD_STATUS(x) (((x) >> 15) & 1) 326#define DWC3_DEPCMD_STATUS(x) (((x) >> 15) & 1)
323#define DWC3_DEPCMD_HIPRI_FORCERM (1 << 11) 327#define DWC3_DEPCMD_HIPRI_FORCERM (1 << 11)
324#define DWC3_DEPCMD_CMDACT (1 << 10) 328#define DWC3_DEPCMD_CMDACT (1 << 10)
@@ -393,6 +397,7 @@ struct dwc3_event_buffer {
393 * @busy_slot: first slot which is owned by HW 397 * @busy_slot: first slot which is owned by HW
394 * @desc: usb_endpoint_descriptor pointer 398 * @desc: usb_endpoint_descriptor pointer
395 * @dwc: pointer to DWC controller 399 * @dwc: pointer to DWC controller
400 * @saved_state: ep state saved during hibernation
396 * @flags: endpoint flags (wedged, stalled, ...) 401 * @flags: endpoint flags (wedged, stalled, ...)
397 * @current_trb: index of current used trb 402 * @current_trb: index of current used trb
398 * @number: endpoint number (1 - 15) 403 * @number: endpoint number (1 - 15)
@@ -415,6 +420,7 @@ struct dwc3_ep {
415 const struct usb_ss_ep_comp_descriptor *comp_desc; 420 const struct usb_ss_ep_comp_descriptor *comp_desc;
416 struct dwc3 *dwc; 421 struct dwc3 *dwc;
417 422
423 u32 saved_state;
418 unsigned flags; 424 unsigned flags;
419#define DWC3_EP_ENABLED (1 << 0) 425#define DWC3_EP_ENABLED (1 << 0)
420#define DWC3_EP_STALL (1 << 1) 426#define DWC3_EP_STALL (1 << 1)
@@ -598,6 +604,7 @@ struct dwc3_scratchpad_array {
598 * @ep0_trb: dma address of ep0_trb 604 * @ep0_trb: dma address of ep0_trb
599 * @ep0_usb_req: dummy req used while handling STD USB requests 605 * @ep0_usb_req: dummy req used while handling STD USB requests
600 * @ep0_bounce_addr: dma address of ep0_bounce 606 * @ep0_bounce_addr: dma address of ep0_bounce
607 * @scratch_addr: dma address of scratchbuf
601 * @lock: for synchronizing 608 * @lock: for synchronizing
602 * @dev: pointer to our struct device 609 * @dev: pointer to our struct device
603 * @xhci: pointer to our xHCI child 610 * @xhci: pointer to our xHCI child
@@ -606,6 +613,7 @@ struct dwc3_scratchpad_array {
606 * @gadget_driver: pointer to the gadget driver 613 * @gadget_driver: pointer to the gadget driver
607 * @regs: base address for our registers 614 * @regs: base address for our registers
608 * @regs_size: address space size 615 * @regs_size: address space size
616 * @nr_scratch: number of scratch buffers
609 * @num_event_buffers: calculated number of event buffers 617 * @num_event_buffers: calculated number of event buffers
610 * @u1u2: only used on revisions <1.83a for workaround 618 * @u1u2: only used on revisions <1.83a for workaround
611 * @maximum_speed: maximum speed requested (mainly for testing purposes) 619 * @maximum_speed: maximum speed requested (mainly for testing purposes)
@@ -613,16 +621,10 @@ struct dwc3_scratchpad_array {
613 * @dr_mode: requested mode of operation 621 * @dr_mode: requested mode of operation
614 * @usb2_phy: pointer to USB2 PHY 622 * @usb2_phy: pointer to USB2 PHY
615 * @usb3_phy: pointer to USB3 PHY 623 * @usb3_phy: pointer to USB3 PHY
624 * @usb2_generic_phy: pointer to USB2 PHY
625 * @usb3_generic_phy: pointer to USB3 PHY
616 * @dcfg: saved contents of DCFG register 626 * @dcfg: saved contents of DCFG register
617 * @gctl: saved contents of GCTL register 627 * @gctl: saved contents of GCTL register
618 * @is_selfpowered: true when we are selfpowered
619 * @three_stage_setup: set if we perform a three phase setup
620 * @ep0_bounced: true when we used bounce buffer
621 * @ep0_expect_in: true when we expect a DATA IN transfer
622 * @start_config_issued: true when StartConfig command has been issued
623 * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
624 * @needs_fifo_resize: not all users might want fifo resizing, flag it
625 * @resize_fifos: tells us it's ok to reconfigure our TxFIFO sizes.
626 * @isoch_delay: wValue from Set Isochronous Delay request; 628 * @isoch_delay: wValue from Set Isochronous Delay request;
627 * @u2sel: parameter from Set SEL request. 629 * @u2sel: parameter from Set SEL request.
628 * @u2pel: parameter from Set SEL request. 630 * @u2pel: parameter from Set SEL request.
@@ -637,15 +639,31 @@ struct dwc3_scratchpad_array {
637 * @mem: points to start of memory which is used for this struct. 639 * @mem: points to start of memory which is used for this struct.
638 * @hwparams: copy of hwparams registers 640 * @hwparams: copy of hwparams registers
639 * @root: debugfs root folder pointer 641 * @root: debugfs root folder pointer
642 * @regset: debugfs pointer to regdump file
643 * @test_mode: true when we're entering a USB test mode
644 * @test_mode_nr: test feature selector
645 * @delayed_status: true when gadget driver asks for delayed status
646 * @ep0_bounced: true when we used bounce buffer
647 * @ep0_expect_in: true when we expect a DATA IN transfer
648 * @has_hibernation: true when dwc3 was configured with Hibernation
649 * @is_selfpowered: true when we are selfpowered
650 * @needs_fifo_resize: not all users might want fifo resizing, flag it
651 * @pullups_connected: true when Run/Stop bit is set
652 * @resize_fifos: tells us it's ok to reconfigure our TxFIFO sizes.
653 * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
654 * @start_config_issued: true when StartConfig command has been issued
655 * @three_stage_setup: set if we perform a three phase setup
640 */ 656 */
641struct dwc3 { 657struct dwc3 {
642 struct usb_ctrlrequest *ctrl_req; 658 struct usb_ctrlrequest *ctrl_req;
643 struct dwc3_trb *ep0_trb; 659 struct dwc3_trb *ep0_trb;
644 void *ep0_bounce; 660 void *ep0_bounce;
661 void *scratchbuf;
645 u8 *setup_buf; 662 u8 *setup_buf;
646 dma_addr_t ctrl_req_addr; 663 dma_addr_t ctrl_req_addr;
647 dma_addr_t ep0_trb_addr; 664 dma_addr_t ep0_trb_addr;
648 dma_addr_t ep0_bounce_addr; 665 dma_addr_t ep0_bounce_addr;
666 dma_addr_t scratch_addr;
649 struct dwc3_request ep0_usb_req; 667 struct dwc3_request ep0_usb_req;
650 668
651 /* device lock */ 669 /* device lock */
@@ -665,6 +683,9 @@ struct dwc3 {
665 struct usb_phy *usb2_phy; 683 struct usb_phy *usb2_phy;
666 struct usb_phy *usb3_phy; 684 struct usb_phy *usb3_phy;
667 685
686 struct phy *usb2_generic_phy;
687 struct phy *usb3_generic_phy;
688
668 void __iomem *regs; 689 void __iomem *regs;
669 size_t regs_size; 690 size_t regs_size;
670 691
@@ -674,6 +695,7 @@ struct dwc3 {
674 u32 dcfg; 695 u32 dcfg;
675 u32 gctl; 696 u32 gctl;
676 697
698 u32 nr_scratch;
677 u32 num_event_buffers; 699 u32 num_event_buffers;
678 u32 u1u2; 700 u32 u1u2;
679 u32 maximum_speed; 701 u32 maximum_speed;
@@ -695,17 +717,9 @@ struct dwc3 {
695#define DWC3_REVISION_230A 0x5533230a 717#define DWC3_REVISION_230A 0x5533230a
696#define DWC3_REVISION_240A 0x5533240a 718#define DWC3_REVISION_240A 0x5533240a
697#define DWC3_REVISION_250A 0x5533250a 719#define DWC3_REVISION_250A 0x5533250a
698 720#define DWC3_REVISION_260A 0x5533260a
699 unsigned is_selfpowered:1; 721#define DWC3_REVISION_270A 0x5533270a
700 unsigned three_stage_setup:1; 722#define DWC3_REVISION_280A 0x5533280a
701 unsigned ep0_bounced:1;
702 unsigned ep0_expect_in:1;
703 unsigned start_config_issued:1;
704 unsigned setup_packet_pending:1;
705 unsigned delayed_status:1;
706 unsigned needs_fifo_resize:1;
707 unsigned resize_fifos:1;
708 unsigned pullups_connected:1;
709 723
710 enum dwc3_ep0_next ep0_next_event; 724 enum dwc3_ep0_next ep0_next_event;
711 enum dwc3_ep0_state ep0state; 725 enum dwc3_ep0_state ep0state;
@@ -730,6 +744,18 @@ struct dwc3 {
730 744
731 u8 test_mode; 745 u8 test_mode;
732 u8 test_mode_nr; 746 u8 test_mode_nr;
747
748 unsigned delayed_status:1;
749 unsigned ep0_bounced:1;
750 unsigned ep0_expect_in:1;
751 unsigned has_hibernation:1;
752 unsigned is_selfpowered:1;
753 unsigned needs_fifo_resize:1;
754 unsigned pullups_connected:1;
755 unsigned resize_fifos:1;
756 unsigned setup_packet_pending:1;
757 unsigned start_config_issued:1;
758 unsigned three_stage_setup:1;
733}; 759};
734 760
735/* -------------------------------------------------------------------------- */ 761/* -------------------------------------------------------------------------- */
@@ -815,15 +841,15 @@ struct dwc3_event_depevt {
815 * 12 - VndrDevTstRcved 841 * 12 - VndrDevTstRcved
816 * @reserved15_12: Reserved, not used 842 * @reserved15_12: Reserved, not used
817 * @event_info: Information about this event 843 * @event_info: Information about this event
818 * @reserved31_24: Reserved, not used 844 * @reserved31_25: Reserved, not used
819 */ 845 */
820struct dwc3_event_devt { 846struct dwc3_event_devt {
821 u32 one_bit:1; 847 u32 one_bit:1;
822 u32 device_event:7; 848 u32 device_event:7;
823 u32 type:4; 849 u32 type:4;
824 u32 reserved15_12:4; 850 u32 reserved15_12:4;
825 u32 event_info:8; 851 u32 event_info:9;
826 u32 reserved31_24:8; 852 u32 reserved31_25:7;
827} __packed; 853} __packed;
828 854
829/** 855/**
@@ -856,6 +882,19 @@ union dwc3_event {
856 struct dwc3_event_gevt gevt; 882 struct dwc3_event_gevt gevt;
857}; 883};
858 884
885/**
886 * struct dwc3_gadget_ep_cmd_params - representation of endpoint command
887 * parameters
888 * @param2: third parameter
889 * @param1: second parameter
890 * @param0: first parameter
891 */
892struct dwc3_gadget_ep_cmd_params {
893 u32 param2;
894 u32 param1;
895 u32 param0;
896};
897
859/* 898/*
860 * DWC3 Features to be used as Driver Data 899 * DWC3 Features to be used as Driver Data
861 */ 900 */
@@ -881,11 +920,31 @@ static inline void dwc3_host_exit(struct dwc3 *dwc)
881#if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE) 920#if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
882int dwc3_gadget_init(struct dwc3 *dwc); 921int dwc3_gadget_init(struct dwc3 *dwc);
883void dwc3_gadget_exit(struct dwc3 *dwc); 922void dwc3_gadget_exit(struct dwc3 *dwc);
923int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode);
924int dwc3_gadget_get_link_state(struct dwc3 *dwc);
925int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state);
926int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
927 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params);
928int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param);
884#else 929#else
885static inline int dwc3_gadget_init(struct dwc3 *dwc) 930static inline int dwc3_gadget_init(struct dwc3 *dwc)
886{ return 0; } 931{ return 0; }
887static inline void dwc3_gadget_exit(struct dwc3 *dwc) 932static inline void dwc3_gadget_exit(struct dwc3 *dwc)
888{ } 933{ }
934static inline int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
935{ return 0; }
936static inline int dwc3_gadget_get_link_state(struct dwc3 *dwc)
937{ return 0; }
938static inline int dwc3_gadget_set_link_state(struct dwc3 *dwc,
939 enum dwc3_link_state state)
940{ return 0; }
941
942static inline int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
943 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
944{ return 0; }
945static inline int dwc3_send_gadget_generic_command(struct dwc3 *dwc,
946 int cmd, u32 param)
947{ return 0; }
889#endif 948#endif
890 949
891/* power management interface */ 950/* power management interface */
diff --git a/drivers/usb/dwc3/dwc3-omap.c b/drivers/usb/dwc3/dwc3-omap.c
index b269dbd47fc4..80b3357034b7 100644
--- a/drivers/usb/dwc3/dwc3-omap.c
+++ b/drivers/usb/dwc3/dwc3-omap.c
@@ -424,11 +424,6 @@ static int dwc3_omap_probe(struct platform_device *pdev)
424 } 424 }
425 425
426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
427 if (!res) {
428 dev_err(dev, "missing memory base resource\n");
429 return -EINVAL;
430 }
431
432 base = devm_ioremap_resource(dev, res); 427 base = devm_ioremap_resource(dev, res);
433 if (IS_ERR(base)) 428 if (IS_ERR(base))
434 return PTR_ERR(base); 429 return PTR_ERR(base);
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 2da0a5a2803a..a740eac74d56 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -68,6 +68,22 @@ int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
68} 68}
69 69
70/** 70/**
71 * dwc3_gadget_get_link_state - Gets current state of USB Link
72 * @dwc: pointer to our context structure
73 *
74 * Caller should take care of locking. This function will
75 * return the link state on success (>= 0) or -ETIMEDOUT.
76 */
77int dwc3_gadget_get_link_state(struct dwc3 *dwc)
78{
79 u32 reg;
80
81 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
82
83 return DWC3_DSTS_USBLNKST(reg);
84}
85
86/**
71 * dwc3_gadget_set_link_state - Sets USB Link to a particular State 87 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
72 * @dwc: pointer to our context structure 88 * @dwc: pointer to our context structure
73 * @state: the state to put link into 89 * @state: the state to put link into
@@ -417,7 +433,7 @@ static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
417static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, 433static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
418 const struct usb_endpoint_descriptor *desc, 434 const struct usb_endpoint_descriptor *desc,
419 const struct usb_ss_ep_comp_descriptor *comp_desc, 435 const struct usb_ss_ep_comp_descriptor *comp_desc,
420 bool ignore) 436 bool ignore, bool restore)
421{ 437{
422 struct dwc3_gadget_ep_cmd_params params; 438 struct dwc3_gadget_ep_cmd_params params;
423 439
@@ -436,6 +452,11 @@ static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
436 if (ignore) 452 if (ignore)
437 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; 453 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
438 454
455 if (restore) {
456 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
457 params.param2 |= dep->saved_state;
458 }
459
439 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN 460 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
440 | DWC3_DEPCFG_XFER_NOT_READY_EN; 461 | DWC3_DEPCFG_XFER_NOT_READY_EN;
441 462
@@ -494,7 +515,7 @@ static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
494static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, 515static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
495 const struct usb_endpoint_descriptor *desc, 516 const struct usb_endpoint_descriptor *desc,
496 const struct usb_ss_ep_comp_descriptor *comp_desc, 517 const struct usb_ss_ep_comp_descriptor *comp_desc,
497 bool ignore) 518 bool ignore, bool restore)
498{ 519{
499 struct dwc3 *dwc = dep->dwc; 520 struct dwc3 *dwc = dep->dwc;
500 u32 reg; 521 u32 reg;
@@ -508,7 +529,8 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
508 return ret; 529 return ret;
509 } 530 }
510 531
511 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore); 532 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
533 restore);
512 if (ret) 534 if (ret)
513 return ret; 535 return ret;
514 536
@@ -548,13 +570,13 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
548 return 0; 570 return 0;
549} 571}
550 572
551static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum); 573static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
552static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) 574static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
553{ 575{
554 struct dwc3_request *req; 576 struct dwc3_request *req;
555 577
556 if (!list_empty(&dep->req_queued)) { 578 if (!list_empty(&dep->req_queued)) {
557 dwc3_stop_active_transfer(dwc, dep->number); 579 dwc3_stop_active_transfer(dwc, dep->number, true);
558 580
559 /* - giveback all requests to gadget driver */ 581 /* - giveback all requests to gadget driver */
560 while (!list_empty(&dep->req_queued)) { 582 while (!list_empty(&dep->req_queued)) {
@@ -659,7 +681,7 @@ static int dwc3_gadget_ep_enable(struct usb_ep *ep,
659 } 681 }
660 682
661 spin_lock_irqsave(&dwc->lock, flags); 683 spin_lock_irqsave(&dwc->lock, flags);
662 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false); 684 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
663 spin_unlock_irqrestore(&dwc->lock, flags); 685 spin_unlock_irqrestore(&dwc->lock, flags);
664 686
665 return ret; 687 return ret;
@@ -771,9 +793,6 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
771 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 793 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
772 else 794 else
773 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; 795 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
774
775 if (!req->request.no_interrupt && !chain)
776 trb->ctrl |= DWC3_TRB_CTRL_IOC;
777 break; 796 break;
778 797
779 case USB_ENDPOINT_XFER_BULK: 798 case USB_ENDPOINT_XFER_BULK:
@@ -788,6 +807,9 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
788 BUG(); 807 BUG();
789 } 808 }
790 809
810 if (!req->request.no_interrupt && !chain)
811 trb->ctrl |= DWC3_TRB_CTRL_IOC;
812
791 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 813 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
792 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 814 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
793 trb->ctrl |= DWC3_TRB_CTRL_CSP; 815 trb->ctrl |= DWC3_TRB_CTRL_CSP;
@@ -1077,7 +1099,7 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1077 */ 1099 */
1078 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1100 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1079 if (list_empty(&dep->req_queued)) { 1101 if (list_empty(&dep->req_queued)) {
1080 dwc3_stop_active_transfer(dwc, dep->number); 1102 dwc3_stop_active_transfer(dwc, dep->number, true);
1081 dep->flags = DWC3_EP_ENABLED; 1103 dep->flags = DWC3_EP_ENABLED;
1082 } 1104 }
1083 return 0; 1105 return 0;
@@ -1107,6 +1129,23 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1107 return ret; 1129 return ret;
1108 } 1130 }
1109 1131
1132 /*
1133 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1134 * right away, otherwise host will not know we have streams to be
1135 * handled.
1136 */
1137 if (dep->stream_capable) {
1138 int ret;
1139
1140 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1141 if (ret && ret != -EBUSY) {
1142 struct dwc3 *dwc = dep->dwc;
1143
1144 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1145 dep->name);
1146 }
1147 }
1148
1110 return 0; 1149 return 0;
1111} 1150}
1112 1151
@@ -1163,7 +1202,7 @@ static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1163 } 1202 }
1164 if (r == req) { 1203 if (r == req) {
1165 /* wait until it is processed */ 1204 /* wait until it is processed */
1166 dwc3_stop_active_transfer(dwc, dep->number); 1205 dwc3_stop_active_transfer(dwc, dep->number, true);
1167 goto out1; 1206 goto out1;
1168 } 1207 }
1169 dev_err(dwc->dev, "request %p was not queued to %s\n", 1208 dev_err(dwc->dev, "request %p was not queued to %s\n",
@@ -1194,8 +1233,7 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1194 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, 1233 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1195 DWC3_DEPCMD_SETSTALL, &params); 1234 DWC3_DEPCMD_SETSTALL, &params);
1196 if (ret) 1235 if (ret)
1197 dev_err(dwc->dev, "failed to %s STALL on %s\n", 1236 dev_err(dwc->dev, "failed to set STALL on %s\n",
1198 value ? "set" : "clear",
1199 dep->name); 1237 dep->name);
1200 else 1238 else
1201 dep->flags |= DWC3_EP_STALL; 1239 dep->flags |= DWC3_EP_STALL;
@@ -1203,8 +1241,7 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1203 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, 1241 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1204 DWC3_DEPCMD_CLEARSTALL, &params); 1242 DWC3_DEPCMD_CLEARSTALL, &params);
1205 if (ret) 1243 if (ret)
1206 dev_err(dwc->dev, "failed to %s STALL on %s\n", 1244 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1207 value ? "set" : "clear",
1208 dep->name); 1245 dep->name);
1209 else 1246 else
1210 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 1247 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
@@ -1387,7 +1424,7 @@ static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1387 return 0; 1424 return 0;
1388} 1425}
1389 1426
1390static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on) 1427static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1391{ 1428{
1392 u32 reg; 1429 u32 reg;
1393 u32 timeout = 500; 1430 u32 timeout = 500;
@@ -1402,9 +1439,17 @@ static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1402 if (dwc->revision >= DWC3_REVISION_194A) 1439 if (dwc->revision >= DWC3_REVISION_194A)
1403 reg &= ~DWC3_DCTL_KEEP_CONNECT; 1440 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1404 reg |= DWC3_DCTL_RUN_STOP; 1441 reg |= DWC3_DCTL_RUN_STOP;
1442
1443 if (dwc->has_hibernation)
1444 reg |= DWC3_DCTL_KEEP_CONNECT;
1445
1405 dwc->pullups_connected = true; 1446 dwc->pullups_connected = true;
1406 } else { 1447 } else {
1407 reg &= ~DWC3_DCTL_RUN_STOP; 1448 reg &= ~DWC3_DCTL_RUN_STOP;
1449
1450 if (dwc->has_hibernation && !suspend)
1451 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1452
1408 dwc->pullups_connected = false; 1453 dwc->pullups_connected = false;
1409 } 1454 }
1410 1455
@@ -1442,7 +1487,7 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1442 is_on = !!is_on; 1487 is_on = !!is_on;
1443 1488
1444 spin_lock_irqsave(&dwc->lock, flags); 1489 spin_lock_irqsave(&dwc->lock, flags);
1445 ret = dwc3_gadget_run_stop(dwc, is_on); 1490 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1446 spin_unlock_irqrestore(&dwc->lock, flags); 1491 spin_unlock_irqrestore(&dwc->lock, flags);
1447 1492
1448 return ret; 1493 return ret;
@@ -1549,14 +1594,16 @@ static int dwc3_gadget_start(struct usb_gadget *g,
1549 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 1594 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1550 1595
1551 dep = dwc->eps[0]; 1596 dep = dwc->eps[0];
1552 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); 1597 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1598 false);
1553 if (ret) { 1599 if (ret) {
1554 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1600 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1555 goto err2; 1601 goto err2;
1556 } 1602 }
1557 1603
1558 dep = dwc->eps[1]; 1604 dep = dwc->eps[1];
1559 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); 1605 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1606 false);
1560 if (ret) { 1607 if (ret) {
1561 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1608 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1562 goto err3; 1609 goto err3;
@@ -1849,15 +1896,12 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1849 */ 1896 */
1850 dep->flags = DWC3_EP_PENDING_REQUEST; 1897 dep->flags = DWC3_EP_PENDING_REQUEST;
1851 } else { 1898 } else {
1852 dwc3_stop_active_transfer(dwc, dep->number); 1899 dwc3_stop_active_transfer(dwc, dep->number, true);
1853 dep->flags = DWC3_EP_ENABLED; 1900 dep->flags = DWC3_EP_ENABLED;
1854 } 1901 }
1855 return 1; 1902 return 1;
1856 } 1903 }
1857 1904
1858 if ((event->status & DEPEVT_STATUS_IOC) &&
1859 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1860 return 0;
1861 return 1; 1905 return 1;
1862} 1906}
1863 1907
@@ -1999,7 +2043,25 @@ static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1999 } 2043 }
2000} 2044}
2001 2045
2002static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum) 2046static void dwc3_suspend_gadget(struct dwc3 *dwc)
2047{
2048 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2049 spin_unlock(&dwc->lock);
2050 dwc->gadget_driver->suspend(&dwc->gadget);
2051 spin_lock(&dwc->lock);
2052 }
2053}
2054
2055static void dwc3_resume_gadget(struct dwc3 *dwc)
2056{
2057 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2058 spin_unlock(&dwc->lock);
2059 dwc->gadget_driver->resume(&dwc->gadget);
2060 spin_lock(&dwc->lock);
2061 }
2062}
2063
2064static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2003{ 2065{
2004 struct dwc3_ep *dep; 2066 struct dwc3_ep *dep;
2005 struct dwc3_gadget_ep_cmd_params params; 2067 struct dwc3_gadget_ep_cmd_params params;
@@ -2031,7 +2093,8 @@ static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2031 */ 2093 */
2032 2094
2033 cmd = DWC3_DEPCMD_ENDTRANSFER; 2095 cmd = DWC3_DEPCMD_ENDTRANSFER;
2034 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC; 2096 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2097 cmd |= DWC3_DEPCMD_CMDIOC;
2035 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); 2098 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2036 memset(&params, 0, sizeof(params)); 2099 memset(&params, 0, sizeof(params));
2037 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params); 2100 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
@@ -2260,17 +2323,23 @@ static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2260 reg |= DWC3_DCTL_HIRD_THRES(12); 2323 reg |= DWC3_DCTL_HIRD_THRES(12);
2261 2324
2262 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2325 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2326 } else {
2327 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2328 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2329 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2263 } 2330 }
2264 2331
2265 dep = dwc->eps[0]; 2332 dep = dwc->eps[0];
2266 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true); 2333 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2334 false);
2267 if (ret) { 2335 if (ret) {
2268 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2336 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2269 return; 2337 return;
2270 } 2338 }
2271 2339
2272 dep = dwc->eps[1]; 2340 dep = dwc->eps[1];
2273 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true); 2341 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2342 false);
2274 if (ret) { 2343 if (ret) {
2275 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2344 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2276 return; 2345 return;
@@ -2378,9 +2447,50 @@ static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2378 2447
2379 dwc->link_state = next; 2448 dwc->link_state = next;
2380 2449
2450 switch (next) {
2451 case DWC3_LINK_STATE_U1:
2452 if (dwc->speed == USB_SPEED_SUPER)
2453 dwc3_suspend_gadget(dwc);
2454 break;
2455 case DWC3_LINK_STATE_U2:
2456 case DWC3_LINK_STATE_U3:
2457 dwc3_suspend_gadget(dwc);
2458 break;
2459 case DWC3_LINK_STATE_RESUME:
2460 dwc3_resume_gadget(dwc);
2461 break;
2462 default:
2463 /* do nothing */
2464 break;
2465 }
2466
2381 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state); 2467 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
2382} 2468}
2383 2469
2470static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2471 unsigned int evtinfo)
2472{
2473 unsigned int is_ss = evtinfo & BIT(4);
2474
2475 /**
2476 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2477 * have a known issue which can cause USB CV TD.9.23 to fail
2478 * randomly.
2479 *
2480 * Because of this issue, core could generate bogus hibernation
2481 * events which SW needs to ignore.
2482 *
2483 * Refers to:
2484 *
2485 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2486 * Device Fallback from SuperSpeed
2487 */
2488 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2489 return;
2490
2491 /* enter hibernation here */
2492}
2493
2384static void dwc3_gadget_interrupt(struct dwc3 *dwc, 2494static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2385 const struct dwc3_event_devt *event) 2495 const struct dwc3_event_devt *event)
2386{ 2496{
@@ -2397,6 +2507,13 @@ static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2397 case DWC3_DEVICE_EVENT_WAKEUP: 2507 case DWC3_DEVICE_EVENT_WAKEUP:
2398 dwc3_gadget_wakeup_interrupt(dwc); 2508 dwc3_gadget_wakeup_interrupt(dwc);
2399 break; 2509 break;
2510 case DWC3_DEVICE_EVENT_HIBER_REQ:
2511 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2512 "unexpected hibernation event\n"))
2513 break;
2514
2515 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2516 break;
2400 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 2517 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2401 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 2518 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2402 break; 2519 break;
@@ -2661,8 +2778,10 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
2661 2778
2662int dwc3_gadget_prepare(struct dwc3 *dwc) 2779int dwc3_gadget_prepare(struct dwc3 *dwc)
2663{ 2780{
2664 if (dwc->pullups_connected) 2781 if (dwc->pullups_connected) {
2665 dwc3_gadget_disable_irq(dwc); 2782 dwc3_gadget_disable_irq(dwc);
2783 dwc3_gadget_run_stop(dwc, true, true);
2784 }
2666 2785
2667 return 0; 2786 return 0;
2668} 2787}
@@ -2671,7 +2790,7 @@ void dwc3_gadget_complete(struct dwc3 *dwc)
2671{ 2790{
2672 if (dwc->pullups_connected) { 2791 if (dwc->pullups_connected) {
2673 dwc3_gadget_enable_irq(dwc); 2792 dwc3_gadget_enable_irq(dwc);
2674 dwc3_gadget_run_stop(dwc, true); 2793 dwc3_gadget_run_stop(dwc, true, false);
2675 } 2794 }
2676} 2795}
2677 2796
@@ -2694,12 +2813,14 @@ int dwc3_gadget_resume(struct dwc3 *dwc)
2694 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2813 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2695 2814
2696 dep = dwc->eps[0]; 2815 dep = dwc->eps[0];
2697 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); 2816 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2817 false);
2698 if (ret) 2818 if (ret)
2699 goto err0; 2819 goto err0;
2700 2820
2701 dep = dwc->eps[1]; 2821 dep = dwc->eps[1];
2702 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); 2822 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2823 false);
2703 if (ret) 2824 if (ret)
2704 goto err1; 2825 goto err1;
2705 2826
diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h
index febe1aa7b714..a0ee75b68a80 100644
--- a/drivers/usb/dwc3/gadget.h
+++ b/drivers/usb/dwc3/gadget.h
@@ -56,12 +56,6 @@ struct dwc3;
56/* DEPXFERCFG parameter 0 */ 56/* DEPXFERCFG parameter 0 */
57#define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffff) 57#define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffff)
58 58
59struct dwc3_gadget_ep_cmd_params {
60 u32 param2;
61 u32 param1;
62 u32 param0;
63};
64
65/* -------------------------------------------------------------------------- */ 59/* -------------------------------------------------------------------------- */
66 60
67#define to_dwc3_request(r) (container_of(r, struct dwc3_request, request)) 61#define to_dwc3_request(r) (container_of(r, struct dwc3_request, request))
@@ -85,9 +79,6 @@ static inline void dwc3_gadget_move_request_queued(struct dwc3_request *req)
85void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 79void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
86 int status); 80 int status);
87 81
88int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode);
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state);
90
91void dwc3_ep0_interrupt(struct dwc3 *dwc, 82void dwc3_ep0_interrupt(struct dwc3 *dwc,
92 const struct dwc3_event_depevt *event); 83 const struct dwc3_event_depevt *event);
93void dwc3_ep0_out_start(struct dwc3 *dwc); 84void dwc3_ep0_out_start(struct dwc3 *dwc);
@@ -95,9 +86,6 @@ int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
95int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, 86int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
96 gfp_t gfp_flags); 87 gfp_t gfp_flags);
97int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value); 88int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value);
98int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
99 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params);
100int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param);
101 89
102/** 90/**
103 * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW 91 * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 42f017afd366..3557c7e5040d 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -301,7 +301,6 @@ config USB_PXA27X
301 gadget drivers to also be dynamically linked. 301 gadget drivers to also be dynamically linked.
302 302
303config USB_S3C_HSOTG 303config USB_S3C_HSOTG
304 depends on ARM
305 tristate "Designware/S3C HS/OtG USB Device controller" 304 tristate "Designware/S3C HS/OtG USB Device controller"
306 help 305 help
307 The Designware USB2.0 high-speed gadget controller 306 The Designware USB2.0 high-speed gadget controller
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index cea8c20a1425..f605ad8c1902 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -1758,15 +1758,15 @@ static int at91udc_probe(struct platform_device *pdev)
1758 1758
1759 /* newer chips have more FIFO memory than rm9200 */ 1759 /* newer chips have more FIFO memory than rm9200 */
1760 if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) { 1760 if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
1761 usb_ep_set_maxpacket_limit(&udc->ep[0].ep, 64); 1761 udc->ep[0].maxpacket = 64;
1762 usb_ep_set_maxpacket_limit(&udc->ep[3].ep, 64); 1762 udc->ep[3].maxpacket = 64;
1763 usb_ep_set_maxpacket_limit(&udc->ep[4].ep, 512); 1763 udc->ep[4].maxpacket = 512;
1764 usb_ep_set_maxpacket_limit(&udc->ep[5].ep, 512); 1764 udc->ep[5].maxpacket = 512;
1765 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) { 1765 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
1766 usb_ep_set_maxpacket_limit(&udc->ep[3].ep, 64); 1766 udc->ep[3].maxpacket = 64;
1767 } else if (cpu_is_at91sam9263()) { 1767 } else if (cpu_is_at91sam9263()) {
1768 usb_ep_set_maxpacket_limit(&udc->ep[0].ep, 64); 1768 udc->ep[0].maxpacket = 64;
1769 usb_ep_set_maxpacket_limit(&udc->ep[3].ep, 64); 1769 udc->ep[3].maxpacket = 64;
1770 } 1770 }
1771 1771
1772 udc->udp_baseaddr = ioremap(res->start, resource_size(res)); 1772 udc->udp_baseaddr = ioremap(res->start, resource_size(res));
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 52771d4c44bc..9f65324f9ae0 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -1661,7 +1661,7 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1661 if (dma_status) { 1661 if (dma_status) {
1662 int i; 1662 int i;
1663 1663
1664 for (i = 1; i < USBA_NR_ENDPOINTS; i++) 1664 for (i = 1; i < USBA_NR_DMAS; i++)
1665 if (dma_status & (1 << i)) 1665 if (dma_status & (1 << i))
1666 usba_dma_irq(udc, &udc->usba_ep[i]); 1666 usba_dma_irq(udc, &udc->usba_ep[i]);
1667 } 1667 }
@@ -1670,7 +1670,7 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1670 if (ep_status) { 1670 if (ep_status) {
1671 int i; 1671 int i;
1672 1672
1673 for (i = 0; i < USBA_NR_ENDPOINTS; i++) 1673 for (i = 0; i < udc->num_ep; i++)
1674 if (ep_status & (1 << i)) { 1674 if (ep_status & (1 << i)) {
1675 if (ep_is_control(&udc->usba_ep[i])) 1675 if (ep_is_control(&udc->usba_ep[i]))
1676 usba_control_irq(udc, &udc->usba_ep[i]); 1676 usba_control_irq(udc, &udc->usba_ep[i]);
@@ -1827,12 +1827,12 @@ static int atmel_usba_stop(struct usb_gadget *gadget,
1827 toggle_bias(0); 1827 toggle_bias(0);
1828 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1828 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1829 1829
1830 udc->driver = NULL;
1831
1832 clk_disable_unprepare(udc->hclk); 1830 clk_disable_unprepare(udc->hclk);
1833 clk_disable_unprepare(udc->pclk); 1831 clk_disable_unprepare(udc->pclk);
1834 1832
1835 DBG(DBG_GADGET, "unregistered driver `%s'\n", driver->driver.name); 1833 DBG(DBG_GADGET, "unregistered driver `%s'\n", udc->driver->driver.name);
1834
1835 udc->driver = NULL;
1836 1836
1837 return 0; 1837 return 0;
1838} 1838}
@@ -1914,6 +1914,12 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
1914 i++; 1914 i++;
1915 } 1915 }
1916 1916
1917 if (i == 0) {
1918 dev_err(&pdev->dev, "of_probe: no endpoint specified\n");
1919 ret = -EINVAL;
1920 goto err;
1921 }
1922
1917 return eps; 1923 return eps;
1918err: 1924err:
1919 return ERR_PTR(ret); 1925 return ERR_PTR(ret);
diff --git a/drivers/usb/gadget/atmel_usba_udc.h b/drivers/usb/gadget/atmel_usba_udc.h
index 2922db50befe..a70706e8cb02 100644
--- a/drivers/usb/gadget/atmel_usba_udc.h
+++ b/drivers/usb/gadget/atmel_usba_udc.h
@@ -210,7 +210,7 @@
210#define USBA_FIFO_BASE(x) ((x) << 16) 210#define USBA_FIFO_BASE(x) ((x) << 16)
211 211
212/* Synth parameters */ 212/* Synth parameters */
213#define USBA_NR_ENDPOINTS 7 213#define USBA_NR_DMAS 7
214 214
215#define EP0_FIFO_SIZE 64 215#define EP0_FIFO_SIZE 64
216#define EP0_EPT_SIZE USBA_EPT_SIZE_64 216#define EP0_EPT_SIZE USBA_EPT_SIZE_64
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index 2b4334394076..10c086afa2f4 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -28,6 +28,10 @@
28#include <linux/usb/composite.h> 28#include <linux/usb/composite.h>
29#include <linux/usb/functionfs.h> 29#include <linux/usb/functionfs.h>
30 30
31#include <linux/aio.h>
32#include <linux/mmu_context.h>
33#include <linux/poll.h>
34
31#include "u_fs.h" 35#include "u_fs.h"
32#include "configfs.h" 36#include "configfs.h"
33 37
@@ -99,6 +103,14 @@ static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
99} 103}
100 104
101 105
106static inline enum ffs_setup_state
107ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
108{
109 return (enum ffs_setup_state)
110 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
111}
112
113
102static void ffs_func_eps_disable(struct ffs_function *func); 114static void ffs_func_eps_disable(struct ffs_function *func);
103static int __must_check ffs_func_eps_enable(struct ffs_function *func); 115static int __must_check ffs_func_eps_enable(struct ffs_function *func);
104 116
@@ -122,8 +134,8 @@ struct ffs_ep {
122 struct usb_ep *ep; /* P: ffs->eps_lock */ 134 struct usb_ep *ep; /* P: ffs->eps_lock */
123 struct usb_request *req; /* P: epfile->mutex */ 135 struct usb_request *req; /* P: epfile->mutex */
124 136
125 /* [0]: full speed, [1]: high speed */ 137 /* [0]: full speed, [1]: high speed, [2]: super speed */
126 struct usb_endpoint_descriptor *descs[2]; 138 struct usb_endpoint_descriptor *descs[3];
127 139
128 u8 num; 140 u8 num;
129 141
@@ -148,6 +160,25 @@ struct ffs_epfile {
148 unsigned char _pad; 160 unsigned char _pad;
149}; 161};
150 162
163/* ffs_io_data structure ***************************************************/
164
165struct ffs_io_data {
166 bool aio;
167 bool read;
168
169 struct kiocb *kiocb;
170 const struct iovec *iovec;
171 unsigned long nr_segs;
172 char __user *buf;
173 size_t len;
174
175 struct mm_struct *mm;
176 struct work_struct work;
177
178 struct usb_ep *ep;
179 struct usb_request *req;
180};
181
151static int __must_check ffs_epfiles_create(struct ffs_data *ffs); 182static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
152static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); 183static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
153 184
@@ -161,8 +192,10 @@ ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
161DEFINE_MUTEX(ffs_lock); 192DEFINE_MUTEX(ffs_lock);
162EXPORT_SYMBOL(ffs_lock); 193EXPORT_SYMBOL(ffs_lock);
163 194
164static struct ffs_dev *ffs_find_dev(const char *name); 195static struct ffs_dev *_ffs_find_dev(const char *name);
196static struct ffs_dev *_ffs_alloc_dev(void);
165static int _ffs_name_dev(struct ffs_dev *dev, const char *name); 197static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
198static void _ffs_free_dev(struct ffs_dev *dev);
166static void *ffs_acquire_dev(const char *dev_name); 199static void *ffs_acquire_dev(const char *dev_name);
167static void ffs_release_dev(struct ffs_data *ffs_data); 200static void ffs_release_dev(struct ffs_data *ffs_data);
168static int ffs_ready(struct ffs_data *ffs); 201static int ffs_ready(struct ffs_data *ffs);
@@ -218,7 +251,7 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
218 } 251 }
219 252
220 ffs->setup_state = FFS_NO_SETUP; 253 ffs->setup_state = FFS_NO_SETUP;
221 return ffs->ep0req_status; 254 return req->status ? req->status : req->actual;
222} 255}
223 256
224static int __ffs_ep0_stall(struct ffs_data *ffs) 257static int __ffs_ep0_stall(struct ffs_data *ffs)
@@ -244,7 +277,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
244 ENTER(); 277 ENTER();
245 278
246 /* Fast check if setup was canceled */ 279 /* Fast check if setup was canceled */
247 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) 280 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
248 return -EIDRM; 281 return -EIDRM;
249 282
250 /* Acquire mutex */ 283 /* Acquire mutex */
@@ -310,8 +343,8 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
310 * rather then _irqsave 343 * rather then _irqsave
311 */ 344 */
312 spin_lock_irq(&ffs->ev.waitq.lock); 345 spin_lock_irq(&ffs->ev.waitq.lock);
313 switch (FFS_SETUP_STATE(ffs)) { 346 switch (ffs_setup_state_clear_cancelled(ffs)) {
314 case FFS_SETUP_CANCELED: 347 case FFS_SETUP_CANCELLED:
315 ret = -EIDRM; 348 ret = -EIDRM;
316 goto done_spin; 349 goto done_spin;
317 350
@@ -346,7 +379,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
346 /* 379 /*
347 * We are guaranteed to be still in FFS_ACTIVE state 380 * We are guaranteed to be still in FFS_ACTIVE state
348 * but the state of setup could have changed from 381 * but the state of setup could have changed from
349 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need 382 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
350 * to check for that. If that happened we copied data 383 * to check for that. If that happened we copied data
351 * from user space in vain but it's unlikely. 384 * from user space in vain but it's unlikely.
352 * 385 *
@@ -355,7 +388,8 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
355 * transition can be performed and it's protected by 388 * transition can be performed and it's protected by
356 * mutex. 389 * mutex.
357 */ 390 */
358 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { 391 if (ffs_setup_state_clear_cancelled(ffs) ==
392 FFS_SETUP_CANCELLED) {
359 ret = -EIDRM; 393 ret = -EIDRM;
360done_spin: 394done_spin:
361 spin_unlock_irq(&ffs->ev.waitq.lock); 395 spin_unlock_irq(&ffs->ev.waitq.lock);
@@ -421,7 +455,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
421 ENTER(); 455 ENTER();
422 456
423 /* Fast check if setup was canceled */ 457 /* Fast check if setup was canceled */
424 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) 458 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
425 return -EIDRM; 459 return -EIDRM;
426 460
427 /* Acquire mutex */ 461 /* Acquire mutex */
@@ -441,8 +475,8 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
441 */ 475 */
442 spin_lock_irq(&ffs->ev.waitq.lock); 476 spin_lock_irq(&ffs->ev.waitq.lock);
443 477
444 switch (FFS_SETUP_STATE(ffs)) { 478 switch (ffs_setup_state_clear_cancelled(ffs)) {
445 case FFS_SETUP_CANCELED: 479 case FFS_SETUP_CANCELLED:
446 ret = -EIDRM; 480 ret = -EIDRM;
447 break; 481 break;
448 482
@@ -489,7 +523,8 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
489 spin_lock_irq(&ffs->ev.waitq.lock); 523 spin_lock_irq(&ffs->ev.waitq.lock);
490 524
491 /* See ffs_ep0_write() */ 525 /* See ffs_ep0_write() */
492 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { 526 if (ffs_setup_state_clear_cancelled(ffs) ==
527 FFS_SETUP_CANCELLED) {
493 ret = -EIDRM; 528 ret = -EIDRM;
494 break; 529 break;
495 } 530 }
@@ -558,6 +593,45 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
558 return ret; 593 return ret;
559} 594}
560 595
596static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
597{
598 struct ffs_data *ffs = file->private_data;
599 unsigned int mask = POLLWRNORM;
600 int ret;
601
602 poll_wait(file, &ffs->ev.waitq, wait);
603
604 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
605 if (unlikely(ret < 0))
606 return mask;
607
608 switch (ffs->state) {
609 case FFS_READ_DESCRIPTORS:
610 case FFS_READ_STRINGS:
611 mask |= POLLOUT;
612 break;
613
614 case FFS_ACTIVE:
615 switch (ffs->setup_state) {
616 case FFS_NO_SETUP:
617 if (ffs->ev.count)
618 mask |= POLLIN;
619 break;
620
621 case FFS_SETUP_PENDING:
622 case FFS_SETUP_CANCELLED:
623 mask |= (POLLIN | POLLOUT);
624 break;
625 }
626 case FFS_CLOSING:
627 break;
628 }
629
630 mutex_unlock(&ffs->mutex);
631
632 return mask;
633}
634
561static const struct file_operations ffs_ep0_operations = { 635static const struct file_operations ffs_ep0_operations = {
562 .llseek = no_llseek, 636 .llseek = no_llseek,
563 637
@@ -566,6 +640,7 @@ static const struct file_operations ffs_ep0_operations = {
566 .read = ffs_ep0_read, 640 .read = ffs_ep0_read,
567 .release = ffs_ep0_release, 641 .release = ffs_ep0_release,
568 .unlocked_ioctl = ffs_ep0_ioctl, 642 .unlocked_ioctl = ffs_ep0_ioctl,
643 .poll = ffs_ep0_poll,
569}; 644};
570 645
571 646
@@ -581,8 +656,52 @@ static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
581 } 656 }
582} 657}
583 658
584static ssize_t ffs_epfile_io(struct file *file, 659static void ffs_user_copy_worker(struct work_struct *work)
585 char __user *buf, size_t len, int read) 660{
661 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
662 work);
663 int ret = io_data->req->status ? io_data->req->status :
664 io_data->req->actual;
665
666 if (io_data->read && ret > 0) {
667 int i;
668 size_t pos = 0;
669 use_mm(io_data->mm);
670 for (i = 0; i < io_data->nr_segs; i++) {
671 if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
672 &io_data->buf[pos],
673 io_data->iovec[i].iov_len))) {
674 ret = -EFAULT;
675 break;
676 }
677 pos += io_data->iovec[i].iov_len;
678 }
679 unuse_mm(io_data->mm);
680 }
681
682 aio_complete(io_data->kiocb, ret, ret);
683
684 usb_ep_free_request(io_data->ep, io_data->req);
685
686 io_data->kiocb->private = NULL;
687 if (io_data->read)
688 kfree(io_data->iovec);
689 kfree(io_data->buf);
690 kfree(io_data);
691}
692
693static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
694 struct usb_request *req)
695{
696 struct ffs_io_data *io_data = req->context;
697
698 ENTER();
699
700 INIT_WORK(&io_data->work, ffs_user_copy_worker);
701 schedule_work(&io_data->work);
702}
703
704static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
586{ 705{
587 struct ffs_epfile *epfile = file->private_data; 706 struct ffs_epfile *epfile = file->private_data;
588 struct ffs_ep *ep; 707 struct ffs_ep *ep;
@@ -612,7 +731,7 @@ static ssize_t ffs_epfile_io(struct file *file,
612 } 731 }
613 732
614 /* Do we halt? */ 733 /* Do we halt? */
615 halt = !read == !epfile->in; 734 halt = (!io_data->read == !epfile->in);
616 if (halt && epfile->isoc) { 735 if (halt && epfile->isoc) {
617 ret = -EINVAL; 736 ret = -EINVAL;
618 goto error; 737 goto error;
@@ -630,15 +749,32 @@ static ssize_t ffs_epfile_io(struct file *file,
630 * Controller may require buffer size to be aligned to 749 * Controller may require buffer size to be aligned to
631 * maxpacketsize of an out endpoint. 750 * maxpacketsize of an out endpoint.
632 */ 751 */
633 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len; 752 data_len = io_data->read ?
753 usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
754 io_data->len;
634 755
635 data = kmalloc(data_len, GFP_KERNEL); 756 data = kmalloc(data_len, GFP_KERNEL);
636 if (unlikely(!data)) 757 if (unlikely(!data))
637 return -ENOMEM; 758 return -ENOMEM;
638 759 if (io_data->aio && !io_data->read) {
639 if (!read && unlikely(copy_from_user(data, buf, len))) { 760 int i;
640 ret = -EFAULT; 761 size_t pos = 0;
641 goto error; 762 for (i = 0; i < io_data->nr_segs; i++) {
763 if (unlikely(copy_from_user(&data[pos],
764 io_data->iovec[i].iov_base,
765 io_data->iovec[i].iov_len))) {
766 ret = -EFAULT;
767 goto error;
768 }
769 pos += io_data->iovec[i].iov_len;
770 }
771 } else {
772 if (!io_data->read &&
773 unlikely(__copy_from_user(data, io_data->buf,
774 io_data->len))) {
775 ret = -EFAULT;
776 goto error;
777 }
642 } 778 }
643 } 779 }
644 780
@@ -661,40 +797,74 @@ static ssize_t ffs_epfile_io(struct file *file,
661 ret = -EBADMSG; 797 ret = -EBADMSG;
662 } else { 798 } else {
663 /* Fire the request */ 799 /* Fire the request */
664 DECLARE_COMPLETION_ONSTACK(done); 800 struct usb_request *req;
665 801
666 struct usb_request *req = ep->req; 802 if (io_data->aio) {
667 req->context = &done; 803 req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
668 req->complete = ffs_epfile_io_complete; 804 if (unlikely(!req))
669 req->buf = data; 805 goto error;
670 req->length = data_len;
671 806
672 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); 807 req->buf = data;
808 req->length = io_data->len;
673 809
674 spin_unlock_irq(&epfile->ffs->eps_lock); 810 io_data->buf = data;
811 io_data->ep = ep->ep;
812 io_data->req = req;
675 813
676 if (unlikely(ret < 0)) { 814 req->context = io_data;
677 /* nop */ 815 req->complete = ffs_epfile_async_io_complete;
678 } else if (unlikely(wait_for_completion_interruptible(&done))) { 816
679 ret = -EINTR; 817 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
680 usb_ep_dequeue(ep->ep, req); 818 if (unlikely(ret)) {
819 usb_ep_free_request(ep->ep, req);
820 goto error;
821 }
822 ret = -EIOCBQUEUED;
823
824 spin_unlock_irq(&epfile->ffs->eps_lock);
681 } else { 825 } else {
682 /* 826 DECLARE_COMPLETION_ONSTACK(done);
683 * XXX We may end up silently droping data here. 827
684 * Since data_len (i.e. req->length) may be bigger 828 req = ep->req;
685 * than len (after being rounded up to maxpacketsize), 829 req->buf = data;
686 * we may end up with more data then user space has 830 req->length = io_data->len;
687 * space for. 831
688 */ 832 req->context = &done;
689 ret = ep->status; 833 req->complete = ffs_epfile_io_complete;
690 if (read && ret > 0 && 834
691 unlikely(copy_to_user(buf, data, 835 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
692 min_t(size_t, ret, len)))) 836
693 ret = -EFAULT; 837 spin_unlock_irq(&epfile->ffs->eps_lock);
838
839 if (unlikely(ret < 0)) {
840 /* nop */
841 } else if (unlikely(
842 wait_for_completion_interruptible(&done))) {
843 ret = -EINTR;
844 usb_ep_dequeue(ep->ep, req);
845 } else {
846 /*
847 * XXX We may end up silently droping data
848 * here. Since data_len (i.e. req->length) may
849 * be bigger than len (after being rounded up
850 * to maxpacketsize), we may end up with more
851 * data then user space has space for.
852 */
853 ret = ep->status;
854 if (io_data->read && ret > 0) {
855 ret = min_t(size_t, ret, io_data->len);
856
857 if (unlikely(copy_to_user(io_data->buf,
858 data, ret)))
859 ret = -EFAULT;
860 }
861 }
862 kfree(data);
694 } 863 }
695 } 864 }
696 865
697 mutex_unlock(&epfile->mutex); 866 mutex_unlock(&epfile->mutex);
867 return ret;
698error: 868error:
699 kfree(data); 869 kfree(data);
700 return ret; 870 return ret;
@@ -704,17 +874,31 @@ static ssize_t
704ffs_epfile_write(struct file *file, const char __user *buf, size_t len, 874ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
705 loff_t *ptr) 875 loff_t *ptr)
706{ 876{
877 struct ffs_io_data io_data;
878
707 ENTER(); 879 ENTER();
708 880
709 return ffs_epfile_io(file, (char __user *)buf, len, 0); 881 io_data.aio = false;
882 io_data.read = false;
883 io_data.buf = (char * __user)buf;
884 io_data.len = len;
885
886 return ffs_epfile_io(file, &io_data);
710} 887}
711 888
712static ssize_t 889static ssize_t
713ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr) 890ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
714{ 891{
892 struct ffs_io_data io_data;
893
715 ENTER(); 894 ENTER();
716 895
717 return ffs_epfile_io(file, buf, len, 1); 896 io_data.aio = false;
897 io_data.read = true;
898 io_data.buf = buf;
899 io_data.len = len;
900
901 return ffs_epfile_io(file, &io_data);
718} 902}
719 903
720static int 904static int
@@ -733,6 +917,89 @@ ffs_epfile_open(struct inode *inode, struct file *file)
733 return 0; 917 return 0;
734} 918}
735 919
920static int ffs_aio_cancel(struct kiocb *kiocb)
921{
922 struct ffs_io_data *io_data = kiocb->private;
923 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
924 int value;
925
926 ENTER();
927
928 spin_lock_irq(&epfile->ffs->eps_lock);
929
930 if (likely(io_data && io_data->ep && io_data->req))
931 value = usb_ep_dequeue(io_data->ep, io_data->req);
932 else
933 value = -EINVAL;
934
935 spin_unlock_irq(&epfile->ffs->eps_lock);
936
937 return value;
938}
939
940static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
941 const struct iovec *iovec,
942 unsigned long nr_segs, loff_t loff)
943{
944 struct ffs_io_data *io_data;
945
946 ENTER();
947
948 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
949 if (unlikely(!io_data))
950 return -ENOMEM;
951
952 io_data->aio = true;
953 io_data->read = false;
954 io_data->kiocb = kiocb;
955 io_data->iovec = iovec;
956 io_data->nr_segs = nr_segs;
957 io_data->len = kiocb->ki_nbytes;
958 io_data->mm = current->mm;
959
960 kiocb->private = io_data;
961
962 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
963
964 return ffs_epfile_io(kiocb->ki_filp, io_data);
965}
966
967static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
968 const struct iovec *iovec,
969 unsigned long nr_segs, loff_t loff)
970{
971 struct ffs_io_data *io_data;
972 struct iovec *iovec_copy;
973
974 ENTER();
975
976 iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
977 if (unlikely(!iovec_copy))
978 return -ENOMEM;
979
980 memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
981
982 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
983 if (unlikely(!io_data)) {
984 kfree(iovec_copy);
985 return -ENOMEM;
986 }
987
988 io_data->aio = true;
989 io_data->read = true;
990 io_data->kiocb = kiocb;
991 io_data->iovec = iovec_copy;
992 io_data->nr_segs = nr_segs;
993 io_data->len = kiocb->ki_nbytes;
994 io_data->mm = current->mm;
995
996 kiocb->private = io_data;
997
998 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
999
1000 return ffs_epfile_io(kiocb->ki_filp, io_data);
1001}
1002
736static int 1003static int
737ffs_epfile_release(struct inode *inode, struct file *file) 1004ffs_epfile_release(struct inode *inode, struct file *file)
738{ 1005{
@@ -789,6 +1056,8 @@ static const struct file_operations ffs_epfile_operations = {
789 .open = ffs_epfile_open, 1056 .open = ffs_epfile_open,
790 .write = ffs_epfile_write, 1057 .write = ffs_epfile_write,
791 .read = ffs_epfile_read, 1058 .read = ffs_epfile_read,
1059 .aio_write = ffs_epfile_aio_write,
1060 .aio_read = ffs_epfile_aio_read,
792 .release = ffs_epfile_release, 1061 .release = ffs_epfile_release,
793 .unlocked_ioctl = ffs_epfile_ioctl, 1062 .unlocked_ioctl = ffs_epfile_ioctl,
794}; 1063};
@@ -1172,7 +1441,7 @@ static void ffs_data_clear(struct ffs_data *ffs)
1172 if (ffs->epfiles) 1441 if (ffs->epfiles)
1173 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count); 1442 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1174 1443
1175 kfree(ffs->raw_descs); 1444 kfree(ffs->raw_descs_data);
1176 kfree(ffs->raw_strings); 1445 kfree(ffs->raw_strings);
1177 kfree(ffs->stringtabs); 1446 kfree(ffs->stringtabs);
1178} 1447}
@@ -1184,14 +1453,15 @@ static void ffs_data_reset(struct ffs_data *ffs)
1184 ffs_data_clear(ffs); 1453 ffs_data_clear(ffs);
1185 1454
1186 ffs->epfiles = NULL; 1455 ffs->epfiles = NULL;
1456 ffs->raw_descs_data = NULL;
1187 ffs->raw_descs = NULL; 1457 ffs->raw_descs = NULL;
1188 ffs->raw_strings = NULL; 1458 ffs->raw_strings = NULL;
1189 ffs->stringtabs = NULL; 1459 ffs->stringtabs = NULL;
1190 1460
1191 ffs->raw_descs_length = 0; 1461 ffs->raw_descs_length = 0;
1192 ffs->raw_fs_descs_length = 0;
1193 ffs->fs_descs_count = 0; 1462 ffs->fs_descs_count = 0;
1194 ffs->hs_descs_count = 0; 1463 ffs->hs_descs_count = 0;
1464 ffs->ss_descs_count = 0;
1195 1465
1196 ffs->strings_count = 0; 1466 ffs->strings_count = 0;
1197 ffs->interfaces_count = 0; 1467 ffs->interfaces_count = 0;
@@ -1334,7 +1604,24 @@ static int ffs_func_eps_enable(struct ffs_function *func)
1334 spin_lock_irqsave(&func->ffs->eps_lock, flags); 1604 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1335 do { 1605 do {
1336 struct usb_endpoint_descriptor *ds; 1606 struct usb_endpoint_descriptor *ds;
1337 ds = ep->descs[ep->descs[1] ? 1 : 0]; 1607 int desc_idx;
1608
1609 if (ffs->gadget->speed == USB_SPEED_SUPER)
1610 desc_idx = 2;
1611 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1612 desc_idx = 1;
1613 else
1614 desc_idx = 0;
1615
1616 /* fall-back to lower speed if desc missing for current speed */
1617 do {
1618 ds = ep->descs[desc_idx];
1619 } while (!ds && --desc_idx >= 0);
1620
1621 if (!ds) {
1622 ret = -EINVAL;
1623 break;
1624 }
1338 1625
1339 ep->ep->driver_data = ep; 1626 ep->ep->driver_data = ep;
1340 ep->ep->desc = ds; 1627 ep->ep->desc = ds;
@@ -1469,6 +1756,12 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1469 } 1756 }
1470 break; 1757 break;
1471 1758
1759 case USB_DT_SS_ENDPOINT_COMP:
1760 pr_vdebug("EP SS companion descriptor\n");
1761 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1762 goto inv_length;
1763 break;
1764
1472 case USB_DT_OTHER_SPEED_CONFIG: 1765 case USB_DT_OTHER_SPEED_CONFIG:
1473 case USB_DT_INTERFACE_POWER: 1766 case USB_DT_INTERFACE_POWER:
1474 case USB_DT_DEBUG: 1767 case USB_DT_DEBUG:
@@ -1579,60 +1872,76 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
1579static int __ffs_data_got_descs(struct ffs_data *ffs, 1872static int __ffs_data_got_descs(struct ffs_data *ffs,
1580 char *const _data, size_t len) 1873 char *const _data, size_t len)
1581{ 1874{
1582 unsigned fs_count, hs_count; 1875 char *data = _data, *raw_descs;
1583 int fs_len, ret = -EINVAL; 1876 unsigned counts[3], flags;
1584 char *data = _data; 1877 int ret = -EINVAL, i;
1585 1878
1586 ENTER(); 1879 ENTER();
1587 1880
1588 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC || 1881 if (get_unaligned_le32(data + 4) != len)
1589 get_unaligned_le32(data + 4) != len))
1590 goto error; 1882 goto error;
1591 fs_count = get_unaligned_le32(data + 8);
1592 hs_count = get_unaligned_le32(data + 12);
1593
1594 if (!fs_count && !hs_count)
1595 goto einval;
1596 1883
1597 data += 16; 1884 switch (get_unaligned_le32(data)) {
1598 len -= 16; 1885 case FUNCTIONFS_DESCRIPTORS_MAGIC:
1599 1886 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
1600 if (likely(fs_count)) { 1887 data += 8;
1601 fs_len = ffs_do_descs(fs_count, data, len, 1888 len -= 8;
1602 __ffs_data_do_entity, ffs); 1889 break;
1603 if (unlikely(fs_len < 0)) { 1890 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
1604 ret = fs_len; 1891 flags = get_unaligned_le32(data + 8);
1892 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
1893 FUNCTIONFS_HAS_HS_DESC |
1894 FUNCTIONFS_HAS_SS_DESC)) {
1895 ret = -ENOSYS;
1605 goto error; 1896 goto error;
1606 } 1897 }
1898 data += 12;
1899 len -= 12;
1900 break;
1901 default:
1902 goto error;
1903 }
1607 1904
1608 data += fs_len; 1905 /* Read fs_count, hs_count and ss_count (if present) */
1609 len -= fs_len; 1906 for (i = 0; i < 3; ++i) {
1610 } else { 1907 if (!(flags & (1 << i))) {
1611 fs_len = 0; 1908 counts[i] = 0;
1909 } else if (len < 4) {
1910 goto error;
1911 } else {
1912 counts[i] = get_unaligned_le32(data);
1913 data += 4;
1914 len -= 4;
1915 }
1612 } 1916 }
1613 1917
1614 if (likely(hs_count)) { 1918 /* Read descriptors */
1615 ret = ffs_do_descs(hs_count, data, len, 1919 raw_descs = data;
1920 for (i = 0; i < 3; ++i) {
1921 if (!counts[i])
1922 continue;
1923 ret = ffs_do_descs(counts[i], data, len,
1616 __ffs_data_do_entity, ffs); 1924 __ffs_data_do_entity, ffs);
1617 if (unlikely(ret < 0)) 1925 if (ret < 0)
1618 goto error; 1926 goto error;
1619 } else { 1927 data += ret;
1620 ret = 0; 1928 len -= ret;
1621 } 1929 }
1622 1930
1623 if (unlikely(len != ret)) 1931 if (raw_descs == data || len) {
1624 goto einval; 1932 ret = -EINVAL;
1933 goto error;
1934 }
1625 1935
1626 ffs->raw_fs_descs_length = fs_len; 1936 ffs->raw_descs_data = _data;
1627 ffs->raw_descs_length = fs_len + ret; 1937 ffs->raw_descs = raw_descs;
1628 ffs->raw_descs = _data; 1938 ffs->raw_descs_length = data - raw_descs;
1629 ffs->fs_descs_count = fs_count; 1939 ffs->fs_descs_count = counts[0];
1630 ffs->hs_descs_count = hs_count; 1940 ffs->hs_descs_count = counts[1];
1941 ffs->ss_descs_count = counts[2];
1631 1942
1632 return 0; 1943 return 0;
1633 1944
1634einval:
1635 ret = -EINVAL;
1636error: 1945error:
1637 kfree(_data); 1946 kfree(_data);
1638 return ret; 1947 return ret;
@@ -1789,7 +2098,7 @@ static void __ffs_event_add(struct ffs_data *ffs,
1789 * the source does nothing. 2098 * the source does nothing.
1790 */ 2099 */
1791 if (ffs->setup_state == FFS_SETUP_PENDING) 2100 if (ffs->setup_state == FFS_SETUP_PENDING)
1792 ffs->setup_state = FFS_SETUP_CANCELED; 2101 ffs->setup_state = FFS_SETUP_CANCELLED;
1793 2102
1794 switch (type) { 2103 switch (type) {
1795 case FUNCTIONFS_RESUME: 2104 case FUNCTIONFS_RESUME:
@@ -1850,21 +2159,28 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1850 struct usb_endpoint_descriptor *ds = (void *)desc; 2159 struct usb_endpoint_descriptor *ds = (void *)desc;
1851 struct ffs_function *func = priv; 2160 struct ffs_function *func = priv;
1852 struct ffs_ep *ffs_ep; 2161 struct ffs_ep *ffs_ep;
1853 2162 unsigned ep_desc_id, idx;
1854 /* 2163 static const char *speed_names[] = { "full", "high", "super" };
1855 * If hs_descriptors is not NULL then we are reading hs
1856 * descriptors now
1857 */
1858 const int isHS = func->function.hs_descriptors != NULL;
1859 unsigned idx;
1860 2164
1861 if (type != FFS_DESCRIPTOR) 2165 if (type != FFS_DESCRIPTOR)
1862 return 0; 2166 return 0;
1863 2167
1864 if (isHS) 2168 /*
2169 * If ss_descriptors is not NULL, we are reading super speed
2170 * descriptors; if hs_descriptors is not NULL, we are reading high
2171 * speed descriptors; otherwise, we are reading full speed
2172 * descriptors.
2173 */
2174 if (func->function.ss_descriptors) {
2175 ep_desc_id = 2;
2176 func->function.ss_descriptors[(long)valuep] = desc;
2177 } else if (func->function.hs_descriptors) {
2178 ep_desc_id = 1;
1865 func->function.hs_descriptors[(long)valuep] = desc; 2179 func->function.hs_descriptors[(long)valuep] = desc;
1866 else 2180 } else {
2181 ep_desc_id = 0;
1867 func->function.fs_descriptors[(long)valuep] = desc; 2182 func->function.fs_descriptors[(long)valuep] = desc;
2183 }
1868 2184
1869 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) 2185 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1870 return 0; 2186 return 0;
@@ -1872,13 +2188,13 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1872 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1; 2188 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
1873 ffs_ep = func->eps + idx; 2189 ffs_ep = func->eps + idx;
1874 2190
1875 if (unlikely(ffs_ep->descs[isHS])) { 2191 if (unlikely(ffs_ep->descs[ep_desc_id])) {
1876 pr_vdebug("two %sspeed descriptors for EP %d\n", 2192 pr_err("two %sspeed descriptors for EP %d\n",
1877 isHS ? "high" : "full", 2193 speed_names[ep_desc_id],
1878 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 2194 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1879 return -EINVAL; 2195 return -EINVAL;
1880 } 2196 }
1881 ffs_ep->descs[isHS] = ds; 2197 ffs_ep->descs[ep_desc_id] = ds;
1882 2198
1883 ffs_dump_mem(": Original ep desc", ds, ds->bLength); 2199 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
1884 if (ffs_ep->ep) { 2200 if (ffs_ep->ep) {
@@ -2022,8 +2338,10 @@ static int _ffs_func_bind(struct usb_configuration *c,
2022 const int full = !!func->ffs->fs_descs_count; 2338 const int full = !!func->ffs->fs_descs_count;
2023 const int high = gadget_is_dualspeed(func->gadget) && 2339 const int high = gadget_is_dualspeed(func->gadget) &&
2024 func->ffs->hs_descs_count; 2340 func->ffs->hs_descs_count;
2341 const int super = gadget_is_superspeed(func->gadget) &&
2342 func->ffs->ss_descs_count;
2025 2343
2026 int ret; 2344 int fs_len, hs_len, ret;
2027 2345
2028 /* Make it a single chunk, less management later on */ 2346 /* Make it a single chunk, less management later on */
2029 vla_group(d); 2347 vla_group(d);
@@ -2032,15 +2350,16 @@ static int _ffs_func_bind(struct usb_configuration *c,
2032 full ? ffs->fs_descs_count + 1 : 0); 2350 full ? ffs->fs_descs_count + 1 : 0);
2033 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs, 2351 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2034 high ? ffs->hs_descs_count + 1 : 0); 2352 high ? ffs->hs_descs_count + 1 : 0);
2353 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2354 super ? ffs->ss_descs_count + 1 : 0);
2035 vla_item_with_sz(d, short, inums, ffs->interfaces_count); 2355 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2036 vla_item_with_sz(d, char, raw_descs, 2356 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
2037 high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2038 char *vlabuf; 2357 char *vlabuf;
2039 2358
2040 ENTER(); 2359 ENTER();
2041 2360
2042 /* Only high speed but not supported by gadget? */ 2361 /* Has descriptors only for speeds gadget does not support */
2043 if (unlikely(!(full | high))) 2362 if (unlikely(!(full | high | super)))
2044 return -ENOTSUPP; 2363 return -ENOTSUPP;
2045 2364
2046 /* Allocate a single chunk, less management later on */ 2365 /* Allocate a single chunk, less management later on */
@@ -2050,8 +2369,10 @@ static int _ffs_func_bind(struct usb_configuration *c,
2050 2369
2051 /* Zero */ 2370 /* Zero */
2052 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz); 2371 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2053 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16, 2372 /* Copy descriptors */
2054 d_raw_descs__sz); 2373 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2374 ffs->raw_descs_length);
2375
2055 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz); 2376 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2056 for (ret = ffs->eps_count; ret; --ret) { 2377 for (ret = ffs->eps_count; ret; --ret) {
2057 struct ffs_ep *ptr; 2378 struct ffs_ep *ptr;
@@ -2073,22 +2394,38 @@ static int _ffs_func_bind(struct usb_configuration *c,
2073 */ 2394 */
2074 if (likely(full)) { 2395 if (likely(full)) {
2075 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); 2396 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2076 ret = ffs_do_descs(ffs->fs_descs_count, 2397 fs_len = ffs_do_descs(ffs->fs_descs_count,
2077 vla_ptr(vlabuf, d, raw_descs), 2398 vla_ptr(vlabuf, d, raw_descs),
2078 d_raw_descs__sz, 2399 d_raw_descs__sz,
2079 __ffs_func_bind_do_descs, func); 2400 __ffs_func_bind_do_descs, func);
2080 if (unlikely(ret < 0)) 2401 if (unlikely(fs_len < 0)) {
2402 ret = fs_len;
2081 goto error; 2403 goto error;
2404 }
2082 } else { 2405 } else {
2083 ret = 0; 2406 fs_len = 0;
2084 } 2407 }
2085 2408
2086 if (likely(high)) { 2409 if (likely(high)) {
2087 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); 2410 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2088 ret = ffs_do_descs(ffs->hs_descs_count, 2411 hs_len = ffs_do_descs(ffs->hs_descs_count,
2089 vla_ptr(vlabuf, d, raw_descs) + ret, 2412 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2090 d_raw_descs__sz - ret, 2413 d_raw_descs__sz - fs_len,
2091 __ffs_func_bind_do_descs, func); 2414 __ffs_func_bind_do_descs, func);
2415 if (unlikely(hs_len < 0)) {
2416 ret = hs_len;
2417 goto error;
2418 }
2419 } else {
2420 hs_len = 0;
2421 }
2422
2423 if (likely(super)) {
2424 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2425 ret = ffs_do_descs(ffs->ss_descs_count,
2426 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2427 d_raw_descs__sz - fs_len - hs_len,
2428 __ffs_func_bind_do_descs, func);
2092 if (unlikely(ret < 0)) 2429 if (unlikely(ret < 0))
2093 goto error; 2430 goto error;
2094 } 2431 }
@@ -2099,7 +2436,8 @@ static int _ffs_func_bind(struct usb_configuration *c,
2099 * now. 2436 * now.
2100 */ 2437 */
2101 ret = ffs_do_descs(ffs->fs_descs_count + 2438 ret = ffs_do_descs(ffs->fs_descs_count +
2102 (high ? ffs->hs_descs_count : 0), 2439 (high ? ffs->hs_descs_count : 0) +
2440 (super ? ffs->ss_descs_count : 0),
2103 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz, 2441 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
2104 __ffs_func_bind_do_nums, func); 2442 __ffs_func_bind_do_nums, func);
2105 if (unlikely(ret < 0)) 2443 if (unlikely(ret < 0))
@@ -2258,7 +2596,7 @@ static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2258 2596
2259static LIST_HEAD(ffs_devices); 2597static LIST_HEAD(ffs_devices);
2260 2598
2261static struct ffs_dev *_ffs_find_dev(const char *name) 2599static struct ffs_dev *_ffs_do_find_dev(const char *name)
2262{ 2600{
2263 struct ffs_dev *dev; 2601 struct ffs_dev *dev;
2264 2602
@@ -2275,7 +2613,7 @@ static struct ffs_dev *_ffs_find_dev(const char *name)
2275/* 2613/*
2276 * ffs_lock must be taken by the caller of this function 2614 * ffs_lock must be taken by the caller of this function
2277 */ 2615 */
2278static struct ffs_dev *ffs_get_single_dev(void) 2616static struct ffs_dev *_ffs_get_single_dev(void)
2279{ 2617{
2280 struct ffs_dev *dev; 2618 struct ffs_dev *dev;
2281 2619
@@ -2291,15 +2629,15 @@ static struct ffs_dev *ffs_get_single_dev(void)
2291/* 2629/*
2292 * ffs_lock must be taken by the caller of this function 2630 * ffs_lock must be taken by the caller of this function
2293 */ 2631 */
2294static struct ffs_dev *ffs_find_dev(const char *name) 2632static struct ffs_dev *_ffs_find_dev(const char *name)
2295{ 2633{
2296 struct ffs_dev *dev; 2634 struct ffs_dev *dev;
2297 2635
2298 dev = ffs_get_single_dev(); 2636 dev = _ffs_get_single_dev();
2299 if (dev) 2637 if (dev)
2300 return dev; 2638 return dev;
2301 2639
2302 return _ffs_find_dev(name); 2640 return _ffs_do_find_dev(name);
2303} 2641}
2304 2642
2305/* Configfs support *********************************************************/ 2643/* Configfs support *********************************************************/
@@ -2335,7 +2673,7 @@ static void ffs_free_inst(struct usb_function_instance *f)
2335 2673
2336 opts = to_f_fs_opts(f); 2674 opts = to_f_fs_opts(f);
2337 ffs_dev_lock(); 2675 ffs_dev_lock();
2338 ffs_free_dev(opts->dev); 2676 _ffs_free_dev(opts->dev);
2339 ffs_dev_unlock(); 2677 ffs_dev_unlock();
2340 kfree(opts); 2678 kfree(opts);
2341} 2679}
@@ -2390,7 +2728,7 @@ static struct usb_function_instance *ffs_alloc_inst(void)
2390 opts->func_inst.set_inst_name = ffs_set_inst_name; 2728 opts->func_inst.set_inst_name = ffs_set_inst_name;
2391 opts->func_inst.free_func_inst = ffs_free_inst; 2729 opts->func_inst.free_func_inst = ffs_free_inst;
2392 ffs_dev_lock(); 2730 ffs_dev_lock();
2393 dev = ffs_alloc_dev(); 2731 dev = _ffs_alloc_dev();
2394 ffs_dev_unlock(); 2732 ffs_dev_unlock();
2395 if (IS_ERR(dev)) { 2733 if (IS_ERR(dev)) {
2396 kfree(opts); 2734 kfree(opts);
@@ -2446,6 +2784,7 @@ static void ffs_func_unbind(struct usb_configuration *c,
2446 */ 2784 */
2447 func->function.fs_descriptors = NULL; 2785 func->function.fs_descriptors = NULL;
2448 func->function.hs_descriptors = NULL; 2786 func->function.hs_descriptors = NULL;
2787 func->function.ss_descriptors = NULL;
2449 func->interfaces_nums = NULL; 2788 func->interfaces_nums = NULL;
2450 2789
2451 ffs_event_add(ffs, FUNCTIONFS_UNBIND); 2790 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
@@ -2478,12 +2817,12 @@ static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2478/* 2817/*
2479 * ffs_lock must be taken by the caller of this function 2818 * ffs_lock must be taken by the caller of this function
2480 */ 2819 */
2481struct ffs_dev *ffs_alloc_dev(void) 2820static struct ffs_dev *_ffs_alloc_dev(void)
2482{ 2821{
2483 struct ffs_dev *dev; 2822 struct ffs_dev *dev;
2484 int ret; 2823 int ret;
2485 2824
2486 if (ffs_get_single_dev()) 2825 if (_ffs_get_single_dev())
2487 return ERR_PTR(-EBUSY); 2826 return ERR_PTR(-EBUSY);
2488 2827
2489 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2828 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
@@ -2511,10 +2850,10 @@ static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2511{ 2850{
2512 struct ffs_dev *existing; 2851 struct ffs_dev *existing;
2513 2852
2514 existing = _ffs_find_dev(name); 2853 existing = _ffs_do_find_dev(name);
2515 if (existing) 2854 if (existing)
2516 return -EBUSY; 2855 return -EBUSY;
2517 2856
2518 dev->name = name; 2857 dev->name = name;
2519 2858
2520 return 0; 2859 return 0;
@@ -2555,7 +2894,7 @@ EXPORT_SYMBOL(ffs_single_dev);
2555/* 2894/*
2556 * ffs_lock must be taken by the caller of this function 2895 * ffs_lock must be taken by the caller of this function
2557 */ 2896 */
2558void ffs_free_dev(struct ffs_dev *dev) 2897static void _ffs_free_dev(struct ffs_dev *dev)
2559{ 2898{
2560 list_del(&dev->entry); 2899 list_del(&dev->entry);
2561 if (dev->name_allocated) 2900 if (dev->name_allocated)
@@ -2572,7 +2911,7 @@ static void *ffs_acquire_dev(const char *dev_name)
2572 ENTER(); 2911 ENTER();
2573 ffs_dev_lock(); 2912 ffs_dev_lock();
2574 2913
2575 ffs_dev = ffs_find_dev(dev_name); 2914 ffs_dev = _ffs_find_dev(dev_name);
2576 if (!ffs_dev) 2915 if (!ffs_dev)
2577 ffs_dev = ERR_PTR(-ENODEV); 2916 ffs_dev = ERR_PTR(-ENODEV);
2578 else if (ffs_dev->mounted) 2917 else if (ffs_dev->mounted)
@@ -2595,11 +2934,12 @@ static void ffs_release_dev(struct ffs_data *ffs_data)
2595 ffs_dev_lock(); 2934 ffs_dev_lock();
2596 2935
2597 ffs_dev = ffs_data->private_data; 2936 ffs_dev = ffs_data->private_data;
2598 if (ffs_dev) 2937 if (ffs_dev) {
2599 ffs_dev->mounted = false; 2938 ffs_dev->mounted = false;
2600 2939
2601 if (ffs_dev->ffs_release_dev_callback) 2940 if (ffs_dev->ffs_release_dev_callback)
2602 ffs_dev->ffs_release_dev_callback(ffs_dev); 2941 ffs_dev->ffs_release_dev_callback(ffs_dev);
2942 }
2603 2943
2604 ffs_dev_unlock(); 2944 ffs_dev_unlock();
2605} 2945}
diff --git a/drivers/usb/gadget/gr_udc.c b/drivers/usb/gadget/gr_udc.c
index 914cbd84ee40..f984ee75324d 100644
--- a/drivers/usb/gadget/gr_udc.c
+++ b/drivers/usb/gadget/gr_udc.c
@@ -225,14 +225,8 @@ static void gr_dfs_create(struct gr_udc *dev)
225 const char *name = "gr_udc_state"; 225 const char *name = "gr_udc_state";
226 226
227 dev->dfs_root = debugfs_create_dir(dev_name(dev->dev), NULL); 227 dev->dfs_root = debugfs_create_dir(dev_name(dev->dev), NULL);
228 if (IS_ERR(dev->dfs_root)) { 228 dev->dfs_state = debugfs_create_file(name, 0444, dev->dfs_root, dev,
229 dev_err(dev->dev, "Failed to create debugfs directory\n"); 229 &gr_dfs_fops);
230 return;
231 }
232 dev->dfs_state = debugfs_create_file(name, 0444, dev->dfs_root,
233 dev, &gr_dfs_fops);
234 if (IS_ERR(dev->dfs_state))
235 dev_err(dev->dev, "Failed to create debugfs file %s\n", name);
236} 230}
237 231
238static void gr_dfs_delete(struct gr_udc *dev) 232static void gr_dfs_delete(struct gr_udc *dev)
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index 69b76efd11e9..6474081dcbaf 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -427,12 +427,17 @@ setup_rx_reqs(struct printer_dev *dev)
427 req->length = USB_BUFSIZE; 427 req->length = USB_BUFSIZE;
428 req->complete = rx_complete; 428 req->complete = rx_complete;
429 429
430 /* here, we unlock, and only unlock, to avoid deadlock. */
431 spin_unlock(&dev->lock);
430 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); 432 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
433 spin_lock(&dev->lock);
431 if (error) { 434 if (error) {
432 DBG(dev, "rx submit --> %d\n", error); 435 DBG(dev, "rx submit --> %d\n", error);
433 list_add(&req->list, &dev->rx_reqs); 436 list_add(&req->list, &dev->rx_reqs);
434 break; 437 break;
435 } else { 438 }
439 /* if the req is empty, then add it into dev->rx_reqs_active. */
440 else if (list_empty(&req->list)) {
436 list_add(&req->list, &dev->rx_reqs_active); 441 list_add(&req->list, &dev->rx_reqs_active);
437 } 442 }
438 } 443 }
@@ -1133,6 +1138,7 @@ static int __init printer_bind_config(struct usb_configuration *c)
1133 NULL, "g_printer"); 1138 NULL, "g_printer");
1134 if (IS_ERR(dev->pdev)) { 1139 if (IS_ERR(dev->pdev)) {
1135 ERROR(dev, "Failed to create device: g_printer\n"); 1140 ERROR(dev, "Failed to create device: g_printer\n");
1141 status = PTR_ERR(dev->pdev);
1136 goto fail; 1142 goto fail;
1137 } 1143 }
1138 1144
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 1172eaeddd85..2a9cb674926a 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -617,7 +617,7 @@ static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
617 to_write = DIV_ROUND_UP(to_write, 4); 617 to_write = DIV_ROUND_UP(to_write, 4);
618 data = hs_req->req.buf + buf_pos; 618 data = hs_req->req.buf + buf_pos;
619 619
620 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write); 620 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
621 621
622 return (to_write >= can_write) ? -ENOSPC : 0; 622 return (to_write >= can_write) ? -ENOSPC : 0;
623} 623}
@@ -720,8 +720,8 @@ static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
720 ureq->length, ureq->actual); 720 ureq->length, ureq->actual);
721 if (0) 721 if (0)
722 dev_dbg(hsotg->dev, 722 dev_dbg(hsotg->dev,
723 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n", 723 "REQ buf %p len %d dma 0x%pad noi=%d zp=%d snok=%d\n",
724 ureq->buf, length, ureq->dma, 724 ureq->buf, length, &ureq->dma,
725 ureq->no_interrupt, ureq->zero, ureq->short_not_ok); 725 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
726 726
727 maxreq = get_ep_limit(hs_ep); 727 maxreq = get_ep_limit(hs_ep);
@@ -789,8 +789,8 @@ static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
789 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); 789 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
790 writel(ureq->dma, hsotg->regs + dma_reg); 790 writel(ureq->dma, hsotg->regs + dma_reg);
791 791
792 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n", 792 dev_dbg(hsotg->dev, "%s: 0x%pad => 0x%08x\n",
793 __func__, ureq->dma, dma_reg); 793 __func__, &ureq->dma, dma_reg);
794 } 794 }
795 795
796 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */ 796 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
@@ -1186,6 +1186,41 @@ static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1186static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg); 1186static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
1187 1187
1188/** 1188/**
1189 * s3c_hsotg_stall_ep0 - stall ep0
1190 * @hsotg: The device state
1191 *
1192 * Set stall for ep0 as response for setup request.
1193 */
1194static void s3c_hsotg_stall_ep0(struct s3c_hsotg *hsotg) {
1195 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1196 u32 reg;
1197 u32 ctrl;
1198
1199 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1200 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1201
1202 /*
1203 * DxEPCTL_Stall will be cleared by EP once it has
1204 * taken effect, so no need to clear later.
1205 */
1206
1207 ctrl = readl(hsotg->regs + reg);
1208 ctrl |= DxEPCTL_Stall;
1209 ctrl |= DxEPCTL_CNAK;
1210 writel(ctrl, hsotg->regs + reg);
1211
1212 dev_dbg(hsotg->dev,
1213 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1214 ctrl, reg, readl(hsotg->regs + reg));
1215
1216 /*
1217 * complete won't be called, so we enqueue
1218 * setup request here
1219 */
1220 s3c_hsotg_enqueue_setup(hsotg);
1221}
1222
1223/**
1189 * s3c_hsotg_process_control - process a control request 1224 * s3c_hsotg_process_control - process a control request
1190 * @hsotg: The device state 1225 * @hsotg: The device state
1191 * @ctrl: The control request received 1226 * @ctrl: The control request received
@@ -1262,38 +1297,8 @@ static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1262 * so respond with a STALL for the status stage to indicate failure. 1297 * so respond with a STALL for the status stage to indicate failure.
1263 */ 1298 */
1264 1299
1265 if (ret < 0) { 1300 if (ret < 0)
1266 u32 reg; 1301 s3c_hsotg_stall_ep0(hsotg);
1267 u32 ctrl;
1268
1269 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1270 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1271
1272 /*
1273 * DxEPCTL_Stall will be cleared by EP once it has
1274 * taken effect, so no need to clear later.
1275 */
1276
1277 ctrl = readl(hsotg->regs + reg);
1278 ctrl |= DxEPCTL_Stall;
1279 ctrl |= DxEPCTL_CNAK;
1280 writel(ctrl, hsotg->regs + reg);
1281
1282 dev_dbg(hsotg->dev,
1283 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1284 ctrl, reg, readl(hsotg->regs + reg));
1285
1286 /*
1287 * don't believe we need to anything more to get the EP
1288 * to reply with a STALL packet
1289 */
1290
1291 /*
1292 * complete won't be called, so we enqueue
1293 * setup request here
1294 */
1295 s3c_hsotg_enqueue_setup(hsotg);
1296 }
1297} 1302}
1298 1303
1299/** 1304/**
@@ -1488,7 +1493,7 @@ static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1488 * note, we might over-write the buffer end by 3 bytes depending on 1493 * note, we might over-write the buffer end by 3 bytes depending on
1489 * alignment of the data. 1494 * alignment of the data.
1490 */ 1495 */
1491 readsl(fifo, hs_req->req.buf + read_ptr, to_read); 1496 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
1492} 1497}
1493 1498
1494/** 1499/**
@@ -2832,6 +2837,15 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2832 2837
2833 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); 2838 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2834 2839
2840 if (index == 0) {
2841 if (value)
2842 s3c_hsotg_stall_ep0(hs);
2843 else
2844 dev_warn(hs->dev,
2845 "%s: can't clear halt on ep0\n", __func__);
2846 return 0;
2847 }
2848
2835 /* write both IN and OUT control registers */ 2849 /* write both IN and OUT control registers */
2836 2850
2837 epreg = DIEPCTL(index); 2851 epreg = DIEPCTL(index);
@@ -3760,10 +3774,55 @@ static int s3c_hsotg_remove(struct platform_device *pdev)
3760 return 0; 3774 return 0;
3761} 3775}
3762 3776
3763#if 1 3777static int s3c_hsotg_suspend(struct platform_device *pdev, pm_message_t state)
3764#define s3c_hsotg_suspend NULL 3778{
3765#define s3c_hsotg_resume NULL 3779 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3766#endif 3780 unsigned long flags;
3781 int ret = 0;
3782
3783 if (hsotg->driver)
3784 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3785 hsotg->driver->driver.name);
3786
3787 spin_lock_irqsave(&hsotg->lock, flags);
3788 s3c_hsotg_disconnect(hsotg);
3789 s3c_hsotg_phy_disable(hsotg);
3790 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3791 spin_unlock_irqrestore(&hsotg->lock, flags);
3792
3793 if (hsotg->driver) {
3794 int ep;
3795 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3796 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3797
3798 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3799 hsotg->supplies);
3800 }
3801
3802 return ret;
3803}
3804
3805static int s3c_hsotg_resume(struct platform_device *pdev)
3806{
3807 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3808 unsigned long flags;
3809 int ret = 0;
3810
3811 if (hsotg->driver) {
3812 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3813 hsotg->driver->driver.name);
3814 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3815 hsotg->supplies);
3816 }
3817
3818 spin_lock_irqsave(&hsotg->lock, flags);
3819 hsotg->last_rst = jiffies;
3820 s3c_hsotg_phy_enable(hsotg);
3821 s3c_hsotg_core_init(hsotg);
3822 spin_unlock_irqrestore(&hsotg->lock, flags);
3823
3824 return ret;
3825}
3767 3826
3768#ifdef CONFIG_OF 3827#ifdef CONFIG_OF
3769static const struct of_device_id s3c_hsotg_of_ids[] = { 3828static const struct of_device_id s3c_hsotg_of_ids[] = {
diff --git a/drivers/usb/gadget/s3c-hsudc.c b/drivers/usb/gadget/s3c-hsudc.c
index ea4bbfe72ec0..10c6a128250c 100644
--- a/drivers/usb/gadget/s3c-hsudc.c
+++ b/drivers/usb/gadget/s3c-hsudc.c
@@ -1344,7 +1344,6 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1344 1344
1345 return 0; 1345 return 0;
1346err_add_udc: 1346err_add_udc:
1347err_add_device:
1348 clk_disable(hsudc->uclk); 1347 clk_disable(hsudc->uclk);
1349err_res: 1348err_res:
1350 if (!IS_ERR_OR_NULL(hsudc->transceiver)) 1349 if (!IS_ERR_OR_NULL(hsudc->transceiver))
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index b7d4f82872b7..50d09c289137 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -48,6 +48,8 @@
48 48
49#define UETH__VERSION "29-May-2008" 49#define UETH__VERSION "29-May-2008"
50 50
51#define GETHER_NAPI_WEIGHT 32
52
51struct eth_dev { 53struct eth_dev {
52 /* lock is held while accessing port_usb 54 /* lock is held while accessing port_usb
53 */ 55 */
@@ -72,6 +74,7 @@ struct eth_dev {
72 struct sk_buff_head *list); 74 struct sk_buff_head *list);
73 75
74 struct work_struct work; 76 struct work_struct work;
77 struct napi_struct rx_napi;
75 78
76 unsigned long todo; 79 unsigned long todo;
77#define WORK_RX_MEMORY 0 80#define WORK_RX_MEMORY 0
@@ -253,18 +256,16 @@ enomem:
253 DBG(dev, "rx submit --> %d\n", retval); 256 DBG(dev, "rx submit --> %d\n", retval);
254 if (skb) 257 if (skb)
255 dev_kfree_skb_any(skb); 258 dev_kfree_skb_any(skb);
256 spin_lock_irqsave(&dev->req_lock, flags);
257 list_add(&req->list, &dev->rx_reqs);
258 spin_unlock_irqrestore(&dev->req_lock, flags);
259 } 259 }
260 return retval; 260 return retval;
261} 261}
262 262
263static void rx_complete(struct usb_ep *ep, struct usb_request *req) 263static void rx_complete(struct usb_ep *ep, struct usb_request *req)
264{ 264{
265 struct sk_buff *skb = req->context, *skb2; 265 struct sk_buff *skb = req->context;
266 struct eth_dev *dev = ep->driver_data; 266 struct eth_dev *dev = ep->driver_data;
267 int status = req->status; 267 int status = req->status;
268 bool rx_queue = 0;
268 269
269 switch (status) { 270 switch (status) {
270 271
@@ -288,30 +289,8 @@ static void rx_complete(struct usb_ep *ep, struct usb_request *req)
288 } else { 289 } else {
289 skb_queue_tail(&dev->rx_frames, skb); 290 skb_queue_tail(&dev->rx_frames, skb);
290 } 291 }
291 skb = NULL; 292 if (!status)
292 293 rx_queue = 1;
293 skb2 = skb_dequeue(&dev->rx_frames);
294 while (skb2) {
295 if (status < 0
296 || ETH_HLEN > skb2->len
297 || skb2->len > VLAN_ETH_FRAME_LEN) {
298 dev->net->stats.rx_errors++;
299 dev->net->stats.rx_length_errors++;
300 DBG(dev, "rx length %d\n", skb2->len);
301 dev_kfree_skb_any(skb2);
302 goto next_frame;
303 }
304 skb2->protocol = eth_type_trans(skb2, dev->net);
305 dev->net->stats.rx_packets++;
306 dev->net->stats.rx_bytes += skb2->len;
307
308 /* no buffer copies needed, unless hardware can't
309 * use skb buffers.
310 */
311 status = netif_rx(skb2);
312next_frame:
313 skb2 = skb_dequeue(&dev->rx_frames);
314 }
315 break; 294 break;
316 295
317 /* software-driven interface shutdown */ 296 /* software-driven interface shutdown */
@@ -334,22 +313,20 @@ quiesce:
334 /* FALLTHROUGH */ 313 /* FALLTHROUGH */
335 314
336 default: 315 default:
316 rx_queue = 1;
317 dev_kfree_skb_any(skb);
337 dev->net->stats.rx_errors++; 318 dev->net->stats.rx_errors++;
338 DBG(dev, "rx status %d\n", status); 319 DBG(dev, "rx status %d\n", status);
339 break; 320 break;
340 } 321 }
341 322
342 if (skb)
343 dev_kfree_skb_any(skb);
344 if (!netif_running(dev->net)) {
345clean: 323clean:
346 spin_lock(&dev->req_lock); 324 spin_lock(&dev->req_lock);
347 list_add(&req->list, &dev->rx_reqs); 325 list_add(&req->list, &dev->rx_reqs);
348 spin_unlock(&dev->req_lock); 326 spin_unlock(&dev->req_lock);
349 req = NULL; 327
350 } 328 if (rx_queue && likely(napi_schedule_prep(&dev->rx_napi)))
351 if (req) 329 __napi_schedule(&dev->rx_napi);
352 rx_submit(dev, req, GFP_ATOMIC);
353} 330}
354 331
355static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n) 332static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
@@ -414,16 +391,24 @@ static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
414{ 391{
415 struct usb_request *req; 392 struct usb_request *req;
416 unsigned long flags; 393 unsigned long flags;
394 int rx_counts = 0;
417 395
418 /* fill unused rxq slots with some skb */ 396 /* fill unused rxq slots with some skb */
419 spin_lock_irqsave(&dev->req_lock, flags); 397 spin_lock_irqsave(&dev->req_lock, flags);
420 while (!list_empty(&dev->rx_reqs)) { 398 while (!list_empty(&dev->rx_reqs)) {
399
400 if (++rx_counts > qlen(dev->gadget, dev->qmult))
401 break;
402
421 req = container_of(dev->rx_reqs.next, 403 req = container_of(dev->rx_reqs.next,
422 struct usb_request, list); 404 struct usb_request, list);
423 list_del_init(&req->list); 405 list_del_init(&req->list);
424 spin_unlock_irqrestore(&dev->req_lock, flags); 406 spin_unlock_irqrestore(&dev->req_lock, flags);
425 407
426 if (rx_submit(dev, req, gfp_flags) < 0) { 408 if (rx_submit(dev, req, gfp_flags) < 0) {
409 spin_lock_irqsave(&dev->req_lock, flags);
410 list_add(&req->list, &dev->rx_reqs);
411 spin_unlock_irqrestore(&dev->req_lock, flags);
427 defer_kevent(dev, WORK_RX_MEMORY); 412 defer_kevent(dev, WORK_RX_MEMORY);
428 return; 413 return;
429 } 414 }
@@ -433,6 +418,41 @@ static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
433 spin_unlock_irqrestore(&dev->req_lock, flags); 418 spin_unlock_irqrestore(&dev->req_lock, flags);
434} 419}
435 420
421static int gether_poll(struct napi_struct *napi, int budget)
422{
423 struct eth_dev *dev = container_of(napi, struct eth_dev, rx_napi);
424 struct sk_buff *skb;
425 unsigned int work_done = 0;
426 int status = 0;
427
428 while ((skb = skb_dequeue(&dev->rx_frames))) {
429 if (status < 0
430 || ETH_HLEN > skb->len
431 || skb->len > VLAN_ETH_FRAME_LEN) {
432 dev->net->stats.rx_errors++;
433 dev->net->stats.rx_length_errors++;
434 DBG(dev, "rx length %d\n", skb->len);
435 dev_kfree_skb_any(skb);
436 continue;
437 }
438 skb->protocol = eth_type_trans(skb, dev->net);
439 dev->net->stats.rx_packets++;
440 dev->net->stats.rx_bytes += skb->len;
441
442 status = netif_rx_ni(skb);
443 }
444
445 if (netif_running(dev->net)) {
446 rx_fill(dev, GFP_KERNEL);
447 work_done++;
448 }
449
450 if (work_done < budget)
451 napi_complete(&dev->rx_napi);
452
453 return work_done;
454}
455
436static void eth_work(struct work_struct *work) 456static void eth_work(struct work_struct *work)
437{ 457{
438 struct eth_dev *dev = container_of(work, struct eth_dev, work); 458 struct eth_dev *dev = container_of(work, struct eth_dev, work);
@@ -625,6 +645,7 @@ static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
625 /* and open the tx floodgates */ 645 /* and open the tx floodgates */
626 atomic_set(&dev->tx_qlen, 0); 646 atomic_set(&dev->tx_qlen, 0);
627 netif_wake_queue(dev->net); 647 netif_wake_queue(dev->net);
648 napi_enable(&dev->rx_napi);
628} 649}
629 650
630static int eth_open(struct net_device *net) 651static int eth_open(struct net_device *net)
@@ -651,6 +672,7 @@ static int eth_stop(struct net_device *net)
651 unsigned long flags; 672 unsigned long flags;
652 673
653 VDBG(dev, "%s\n", __func__); 674 VDBG(dev, "%s\n", __func__);
675 napi_disable(&dev->rx_napi);
654 netif_stop_queue(net); 676 netif_stop_queue(net);
655 677
656 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", 678 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
@@ -768,6 +790,7 @@ struct eth_dev *gether_setup_name(struct usb_gadget *g,
768 return ERR_PTR(-ENOMEM); 790 return ERR_PTR(-ENOMEM);
769 791
770 dev = netdev_priv(net); 792 dev = netdev_priv(net);
793 netif_napi_add(net, &dev->rx_napi, gether_poll, GETHER_NAPI_WEIGHT);
771 spin_lock_init(&dev->lock); 794 spin_lock_init(&dev->lock);
772 spin_lock_init(&dev->req_lock); 795 spin_lock_init(&dev->req_lock);
773 INIT_WORK(&dev->work, eth_work); 796 INIT_WORK(&dev->work, eth_work);
@@ -830,6 +853,7 @@ struct net_device *gether_setup_name_default(const char *netname)
830 return ERR_PTR(-ENOMEM); 853 return ERR_PTR(-ENOMEM);
831 854
832 dev = netdev_priv(net); 855 dev = netdev_priv(net);
856 netif_napi_add(net, &dev->rx_napi, gether_poll, GETHER_NAPI_WEIGHT);
833 spin_lock_init(&dev->lock); 857 spin_lock_init(&dev->lock);
834 spin_lock_init(&dev->req_lock); 858 spin_lock_init(&dev->req_lock);
835 INIT_WORK(&dev->work, eth_work); 859 INIT_WORK(&dev->work, eth_work);
@@ -1113,6 +1137,7 @@ void gether_disconnect(struct gether *link)
1113{ 1137{
1114 struct eth_dev *dev = link->ioport; 1138 struct eth_dev *dev = link->ioport;
1115 struct usb_request *req; 1139 struct usb_request *req;
1140 struct sk_buff *skb;
1116 1141
1117 WARN_ON(!dev); 1142 WARN_ON(!dev);
1118 if (!dev) 1143 if (!dev)
@@ -1139,6 +1164,12 @@ void gether_disconnect(struct gether *link)
1139 spin_lock(&dev->req_lock); 1164 spin_lock(&dev->req_lock);
1140 } 1165 }
1141 spin_unlock(&dev->req_lock); 1166 spin_unlock(&dev->req_lock);
1167
1168 spin_lock(&dev->rx_frames.lock);
1169 while ((skb = __skb_dequeue(&dev->rx_frames)))
1170 dev_kfree_skb_any(skb);
1171 spin_unlock(&dev->rx_frames.lock);
1172
1142 link->in_ep->driver_data = NULL; 1173 link->in_ep->driver_data = NULL;
1143 link->in_ep->desc = NULL; 1174 link->in_ep->desc = NULL;
1144 1175
diff --git a/drivers/usb/gadget/u_fs.h b/drivers/usb/gadget/u_fs.h
index bc2d3718219b..bf0ba375d459 100644
--- a/drivers/usb/gadget/u_fs.h
+++ b/drivers/usb/gadget/u_fs.h
@@ -65,10 +65,8 @@ static inline void ffs_dev_unlock(void)
65 mutex_unlock(&ffs_lock); 65 mutex_unlock(&ffs_lock);
66} 66}
67 67
68struct ffs_dev *ffs_alloc_dev(void);
69int ffs_name_dev(struct ffs_dev *dev, const char *name); 68int ffs_name_dev(struct ffs_dev *dev, const char *name);
70int ffs_single_dev(struct ffs_dev *dev); 69int ffs_single_dev(struct ffs_dev *dev);
71void ffs_free_dev(struct ffs_dev *dev);
72 70
73struct ffs_epfile; 71struct ffs_epfile;
74struct ffs_function; 72struct ffs_function;
@@ -125,7 +123,7 @@ enum ffs_setup_state {
125 * setup. If this state is set read/write on ep0 return 123 * setup. If this state is set read/write on ep0 return
126 * -EIDRM. This state is only set when adding event. 124 * -EIDRM. This state is only set when adding event.
127 */ 125 */
128 FFS_SETUP_CANCELED 126 FFS_SETUP_CANCELLED
129}; 127};
130 128
131struct ffs_data { 129struct ffs_data {
@@ -156,7 +154,6 @@ struct ffs_data {
156 */ 154 */
157 struct usb_request *ep0req; /* P: mutex */ 155 struct usb_request *ep0req; /* P: mutex */
158 struct completion ep0req_completion; /* P: mutex */ 156 struct completion ep0req_completion; /* P: mutex */
159 int ep0req_status; /* P: mutex */
160 157
161 /* reference counter */ 158 /* reference counter */
162 atomic_t ref; 159 atomic_t ref;
@@ -168,19 +165,18 @@ struct ffs_data {
168 165
169 /* 166 /*
170 * Possible transitions: 167 * Possible transitions:
171 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock 168 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
172 * happens only in ep0 read which is P: mutex 169 * happens only in ep0 read which is P: mutex
173 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock 170 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
174 * happens only in ep0 i/o which is P: mutex 171 * happens only in ep0 i/o which is P: mutex
175 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock 172 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELLED -- P: ev.waitq.lock
176 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg 173 * + FFS_SETUP_CANCELLED -> FFS_NO_SETUP -- cmpxchg
174 *
175 * This field should never be accessed directly and instead
176 * ffs_setup_state_clear_cancelled function should be used.
177 */ 177 */
178 enum ffs_setup_state setup_state; 178 enum ffs_setup_state setup_state;
179 179
180#define FFS_SETUP_STATE(ffs) \
181 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
182 FFS_SETUP_CANCELED, FFS_NO_SETUP))
183
184 /* Events & such. */ 180 /* Events & such. */
185 struct { 181 struct {
186 u8 types[4]; 182 u8 types[4];
@@ -210,16 +206,16 @@ struct ffs_data {
210 206
211 /* filled by __ffs_data_got_descs() */ 207 /* filled by __ffs_data_got_descs() */
212 /* 208 /*
213 * Real descriptors are 16 bytes after raw_descs (so you need 209 * raw_descs is what you kfree, real_descs points inside of raw_descs,
214 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the 210 * where full speed, high speed and super speed descriptors start.
215 * first full speed descriptor). raw_descs_length and 211 * real_descs_length is the length of all those descriptors.
216 * raw_fs_descs_length do not have those 16 bytes added.
217 */ 212 */
213 const void *raw_descs_data;
218 const void *raw_descs; 214 const void *raw_descs;
219 unsigned raw_descs_length; 215 unsigned raw_descs_length;
220 unsigned raw_fs_descs_length;
221 unsigned fs_descs_count; 216 unsigned fs_descs_count;
222 unsigned hs_descs_count; 217 unsigned hs_descs_count;
218 unsigned ss_descs_count;
223 219
224 unsigned short strings_count; 220 unsigned short strings_count;
225 unsigned short interfaces_count; 221 unsigned short interfaces_count;
diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
index 688dc8bb192d..8b789792f6fa 100644
--- a/drivers/usb/musb/Kconfig
+++ b/drivers/usb/musb/Kconfig
@@ -43,6 +43,7 @@ config USB_MUSB_HOST
43config USB_MUSB_GADGET 43config USB_MUSB_GADGET
44 bool "Gadget only mode" 44 bool "Gadget only mode"
45 depends on USB_GADGET=y || USB_GADGET=USB_MUSB_HDRC 45 depends on USB_GADGET=y || USB_GADGET=USB_MUSB_HDRC
46 depends on HAS_DMA
46 help 47 help
47 Select this when you want to use MUSB in gadget mode only, 48 Select this when you want to use MUSB in gadget mode only,
48 thereby the host feature will be regressed. 49 thereby the host feature will be regressed.
@@ -50,6 +51,7 @@ config USB_MUSB_GADGET
50config USB_MUSB_DUAL_ROLE 51config USB_MUSB_DUAL_ROLE
51 bool "Dual Role mode" 52 bool "Dual Role mode"
52 depends on ((USB=y || USB=USB_MUSB_HDRC) && (USB_GADGET=y || USB_GADGET=USB_MUSB_HDRC)) 53 depends on ((USB=y || USB=USB_MUSB_HDRC) && (USB_GADGET=y || USB_GADGET=USB_MUSB_HDRC))
54 depends on HAS_DMA
53 help 55 help
54 This is the default mode of working of MUSB controller where 56 This is the default mode of working of MUSB controller where
55 both host and gadget features are enabled. 57 both host and gadget features are enabled.
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 239ad0b1ceb6..07576907e2c6 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -438,7 +438,6 @@ void musb_hnp_stop(struct musb *musb)
438static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb, 438static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
439 u8 devctl) 439 u8 devctl)
440{ 440{
441 struct usb_otg *otg = musb->xceiv->otg;
442 irqreturn_t handled = IRQ_NONE; 441 irqreturn_t handled = IRQ_NONE;
443 442
444 dev_dbg(musb->controller, "<== DevCtl=%02x, int_usb=0x%x\n", devctl, 443 dev_dbg(musb->controller, "<== DevCtl=%02x, int_usb=0x%x\n", devctl,
@@ -656,7 +655,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
656 break; 655 break;
657 case OTG_STATE_B_PERIPHERAL: 656 case OTG_STATE_B_PERIPHERAL:
658 musb_g_suspend(musb); 657 musb_g_suspend(musb);
659 musb->is_active = otg->gadget->b_hnp_enable; 658 musb->is_active = musb->g.b_hnp_enable;
660 if (musb->is_active) { 659 if (musb->is_active) {
661 musb->xceiv->state = OTG_STATE_B_WAIT_ACON; 660 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
662 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n"); 661 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
@@ -672,7 +671,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
672 break; 671 break;
673 case OTG_STATE_A_HOST: 672 case OTG_STATE_A_HOST:
674 musb->xceiv->state = OTG_STATE_A_SUSPEND; 673 musb->xceiv->state = OTG_STATE_A_SUSPEND;
675 musb->is_active = otg->host->b_hnp_enable; 674 musb->is_active = musb->hcd->self.b_hnp_enable;
676 break; 675 break;
677 case OTG_STATE_B_HOST: 676 case OTG_STATE_B_HOST:
678 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */ 677 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
diff --git a/drivers/usb/musb/musb_cppi41.c b/drivers/usb/musb/musb_cppi41.c
index f88929609bac..7b8bbf53127e 100644
--- a/drivers/usb/musb/musb_cppi41.c
+++ b/drivers/usb/musb/musb_cppi41.c
@@ -39,6 +39,7 @@ struct cppi41_dma_channel {
39 u32 transferred; 39 u32 transferred;
40 u32 packet_sz; 40 u32 packet_sz;
41 struct list_head tx_check; 41 struct list_head tx_check;
42 struct work_struct dma_completion;
42}; 43};
43 44
44#define MUSB_DMA_NUM_CHANNELS 15 45#define MUSB_DMA_NUM_CHANNELS 15
@@ -112,6 +113,18 @@ static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
112 return true; 113 return true;
113} 114}
114 115
116static bool is_isoc(struct musb_hw_ep *hw_ep, bool in)
117{
118 if (in && hw_ep->in_qh) {
119 if (hw_ep->in_qh->type == USB_ENDPOINT_XFER_ISOC)
120 return true;
121 } else if (hw_ep->out_qh) {
122 if (hw_ep->out_qh->type == USB_ENDPOINT_XFER_ISOC)
123 return true;
124 }
125 return false;
126}
127
115static void cppi41_dma_callback(void *private_data); 128static void cppi41_dma_callback(void *private_data);
116 129
117static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel) 130static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
@@ -119,7 +132,8 @@ static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
119 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep; 132 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
120 struct musb *musb = hw_ep->musb; 133 struct musb *musb = hw_ep->musb;
121 134
122 if (!cppi41_channel->prog_len) { 135 if (!cppi41_channel->prog_len ||
136 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
123 137
124 /* done, complete */ 138 /* done, complete */
125 cppi41_channel->channel.actual_len = 139 cppi41_channel->channel.actual_len =
@@ -165,6 +179,32 @@ static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
165 } 179 }
166} 180}
167 181
182static void cppi_trans_done_work(struct work_struct *work)
183{
184 unsigned long flags;
185 struct cppi41_dma_channel *cppi41_channel =
186 container_of(work, struct cppi41_dma_channel, dma_completion);
187 struct cppi41_dma_controller *controller = cppi41_channel->controller;
188 struct musb *musb = controller->musb;
189 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
190 bool empty;
191
192 if (!cppi41_channel->is_tx && is_isoc(hw_ep, 1)) {
193 spin_lock_irqsave(&musb->lock, flags);
194 cppi41_trans_done(cppi41_channel);
195 spin_unlock_irqrestore(&musb->lock, flags);
196 } else {
197 empty = musb_is_tx_fifo_empty(hw_ep);
198 if (empty) {
199 spin_lock_irqsave(&musb->lock, flags);
200 cppi41_trans_done(cppi41_channel);
201 spin_unlock_irqrestore(&musb->lock, flags);
202 } else {
203 schedule_work(&cppi41_channel->dma_completion);
204 }
205 }
206}
207
168static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer) 208static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
169{ 209{
170 struct cppi41_dma_controller *controller; 210 struct cppi41_dma_controller *controller;
@@ -228,6 +268,14 @@ static void cppi41_dma_callback(void *private_data)
228 transferred < cppi41_channel->packet_sz) 268 transferred < cppi41_channel->packet_sz)
229 cppi41_channel->prog_len = 0; 269 cppi41_channel->prog_len = 0;
230 270
271 if (!cppi41_channel->is_tx) {
272 if (is_isoc(hw_ep, 1))
273 schedule_work(&cppi41_channel->dma_completion);
274 else
275 cppi41_trans_done(cppi41_channel);
276 goto out;
277 }
278
231 empty = musb_is_tx_fifo_empty(hw_ep); 279 empty = musb_is_tx_fifo_empty(hw_ep);
232 if (empty) { 280 if (empty) {
233 cppi41_trans_done(cppi41_channel); 281 cppi41_trans_done(cppi41_channel);
@@ -264,6 +312,10 @@ static void cppi41_dma_callback(void *private_data)
264 goto out; 312 goto out;
265 } 313 }
266 } 314 }
315 if (is_isoc(hw_ep, 0)) {
316 schedule_work(&cppi41_channel->dma_completion);
317 goto out;
318 }
267 list_add_tail(&cppi41_channel->tx_check, 319 list_add_tail(&cppi41_channel->tx_check,
268 &controller->early_tx_list); 320 &controller->early_tx_list);
269 if (!hrtimer_active(&controller->early_tx)) { 321 if (!hrtimer_active(&controller->early_tx)) {
@@ -448,12 +500,25 @@ static int cppi41_dma_channel_program(struct dma_channel *channel,
448 dma_addr_t dma_addr, u32 len) 500 dma_addr_t dma_addr, u32 len)
449{ 501{
450 int ret; 502 int ret;
503 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
504 int hb_mult = 0;
451 505
452 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN || 506 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
453 channel->status == MUSB_DMA_STATUS_BUSY); 507 channel->status == MUSB_DMA_STATUS_BUSY);
454 508
509 if (is_host_active(cppi41_channel->controller->musb)) {
510 if (cppi41_channel->is_tx)
511 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
512 else
513 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
514 }
515
455 channel->status = MUSB_DMA_STATUS_BUSY; 516 channel->status = MUSB_DMA_STATUS_BUSY;
456 channel->actual_len = 0; 517 channel->actual_len = 0;
518
519 if (hb_mult)
520 packet_sz = hb_mult * (packet_sz & 0x7FF);
521
457 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len); 522 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
458 if (!ret) 523 if (!ret)
459 channel->status = MUSB_DMA_STATUS_FREE; 524 channel->status = MUSB_DMA_STATUS_FREE;
@@ -607,6 +672,8 @@ static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
607 cppi41_channel->port_num = port; 672 cppi41_channel->port_num = port;
608 cppi41_channel->is_tx = is_tx; 673 cppi41_channel->is_tx = is_tx;
609 INIT_LIST_HEAD(&cppi41_channel->tx_check); 674 INIT_LIST_HEAD(&cppi41_channel->tx_check);
675 INIT_WORK(&cppi41_channel->dma_completion,
676 cppi_trans_done_work);
610 677
611 musb_dma = &cppi41_channel->channel; 678 musb_dma = &cppi41_channel->channel;
612 musb_dma->private_data = cppi41_channel; 679 musb_dma->private_data = cppi41_channel;
diff --git a/drivers/usb/musb/musb_dsps.c b/drivers/usb/musb/musb_dsps.c
index 7a109eae9b9a..3372ded5def7 100644
--- a/drivers/usb/musb/musb_dsps.c
+++ b/drivers/usb/musb/musb_dsps.c
@@ -45,6 +45,8 @@
45#include <linux/of_irq.h> 45#include <linux/of_irq.h>
46#include <linux/usb/of.h> 46#include <linux/usb/of.h>
47 47
48#include <linux/debugfs.h>
49
48#include "musb_core.h" 50#include "musb_core.h"
49 51
50static const struct of_device_id musb_dsps_of_match[]; 52static const struct of_device_id musb_dsps_of_match[];
@@ -136,6 +138,26 @@ struct dsps_glue {
136 unsigned long last_timer; /* last timer data for each instance */ 138 unsigned long last_timer; /* last timer data for each instance */
137 139
138 struct dsps_context context; 140 struct dsps_context context;
141 struct debugfs_regset32 regset;
142 struct dentry *dbgfs_root;
143};
144
145static const struct debugfs_reg32 dsps_musb_regs[] = {
146 { "revision", 0x00 },
147 { "control", 0x14 },
148 { "status", 0x18 },
149 { "eoi", 0x24 },
150 { "intr0_stat", 0x30 },
151 { "intr1_stat", 0x34 },
152 { "intr0_set", 0x38 },
153 { "intr1_set", 0x3c },
154 { "txmode", 0x70 },
155 { "rxmode", 0x74 },
156 { "autoreq", 0xd0 },
157 { "srpfixtime", 0xd4 },
158 { "tdown", 0xd8 },
159 { "phy_utmi", 0xe0 },
160 { "mode", 0xe8 },
139}; 161};
140 162
141static void dsps_musb_try_idle(struct musb *musb, unsigned long timeout) 163static void dsps_musb_try_idle(struct musb *musb, unsigned long timeout)
@@ -368,6 +390,30 @@ out:
368 return ret; 390 return ret;
369} 391}
370 392
393static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue)
394{
395 struct dentry *root;
396 struct dentry *file;
397 char buf[128];
398
399 sprintf(buf, "%s.dsps", dev_name(musb->controller));
400 root = debugfs_create_dir(buf, NULL);
401 if (!root)
402 return -ENOMEM;
403 glue->dbgfs_root = root;
404
405 glue->regset.regs = dsps_musb_regs;
406 glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs);
407 glue->regset.base = musb->ctrl_base;
408
409 file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset);
410 if (!file) {
411 debugfs_remove_recursive(root);
412 return -ENOMEM;
413 }
414 return 0;
415}
416
371static int dsps_musb_init(struct musb *musb) 417static int dsps_musb_init(struct musb *musb)
372{ 418{
373 struct device *dev = musb->controller; 419 struct device *dev = musb->controller;
@@ -377,6 +423,7 @@ static int dsps_musb_init(struct musb *musb)
377 void __iomem *reg_base; 423 void __iomem *reg_base;
378 struct resource *r; 424 struct resource *r;
379 u32 rev, val; 425 u32 rev, val;
426 int ret;
380 427
381 r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control"); 428 r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control");
382 if (!r) 429 if (!r)
@@ -410,6 +457,10 @@ static int dsps_musb_init(struct musb *musb)
410 val &= ~(1 << wrp->otg_disable); 457 val &= ~(1 << wrp->otg_disable);
411 dsps_writel(musb->ctrl_base, wrp->phy_utmi, val); 458 dsps_writel(musb->ctrl_base, wrp->phy_utmi, val);
412 459
460 ret = dsps_musb_dbg_init(musb, glue);
461 if (ret)
462 return ret;
463
413 return 0; 464 return 0;
414} 465}
415 466
@@ -616,7 +667,7 @@ static int dsps_probe(struct platform_device *pdev)
616 wrp = match->data; 667 wrp = match->data;
617 668
618 /* allocate glue */ 669 /* allocate glue */
619 glue = kzalloc(sizeof(*glue), GFP_KERNEL); 670 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
620 if (!glue) { 671 if (!glue) {
621 dev_err(&pdev->dev, "unable to allocate glue memory\n"); 672 dev_err(&pdev->dev, "unable to allocate glue memory\n");
622 return -ENOMEM; 673 return -ENOMEM;
@@ -644,7 +695,6 @@ err3:
644 pm_runtime_put(&pdev->dev); 695 pm_runtime_put(&pdev->dev);
645err2: 696err2:
646 pm_runtime_disable(&pdev->dev); 697 pm_runtime_disable(&pdev->dev);
647 kfree(glue);
648 return ret; 698 return ret;
649} 699}
650 700
@@ -657,7 +707,9 @@ static int dsps_remove(struct platform_device *pdev)
657 /* disable usbss clocks */ 707 /* disable usbss clocks */
658 pm_runtime_put(&pdev->dev); 708 pm_runtime_put(&pdev->dev);
659 pm_runtime_disable(&pdev->dev); 709 pm_runtime_disable(&pdev->dev);
660 kfree(glue); 710
711 debugfs_remove_recursive(glue->dbgfs_root);
712
661 return 0; 713 return 0;
662} 714}
663 715
diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index abb38c3833ef..eb06291a40c8 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -1694,7 +1694,8 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1694 | MUSB_RXCSR_RXPKTRDY); 1694 | MUSB_RXCSR_RXPKTRDY);
1695 musb_writew(hw_ep->regs, MUSB_RXCSR, val); 1695 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1696 1696
1697#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) 1697#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1698 defined(CONFIG_USB_TI_CPPI41_DMA)
1698 if (usb_pipeisoc(pipe)) { 1699 if (usb_pipeisoc(pipe)) {
1699 struct usb_iso_packet_descriptor *d; 1700 struct usb_iso_packet_descriptor *d;
1700 1701
@@ -1707,10 +1708,30 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1707 if (d->status != -EILSEQ && d->status != -EOVERFLOW) 1708 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1708 d->status = 0; 1709 d->status = 0;
1709 1710
1710 if (++qh->iso_idx >= urb->number_of_packets) 1711 if (++qh->iso_idx >= urb->number_of_packets) {
1711 done = true; 1712 done = true;
1712 else 1713 } else {
1714#if defined(CONFIG_USB_TI_CPPI41_DMA)
1715 struct dma_controller *c;
1716 dma_addr_t *buf;
1717 u32 length, ret;
1718
1719 c = musb->dma_controller;
1720 buf = (void *)
1721 urb->iso_frame_desc[qh->iso_idx].offset
1722 + (u32)urb->transfer_dma;
1723
1724 length =
1725 urb->iso_frame_desc[qh->iso_idx].length;
1726
1727 val |= MUSB_RXCSR_DMAENAB;
1728 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1729
1730 ret = c->channel_program(dma, qh->maxpacket,
1731 0, (u32) buf, length);
1732#endif
1713 done = false; 1733 done = false;
1734 }
1714 1735
1715 } else { 1736 } else {
1716 /* done if urb buffer is full or short packet is recd */ 1737 /* done if urb buffer is full or short packet is recd */
@@ -1750,7 +1771,8 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1750 } 1771 }
1751 1772
1752 /* we are expecting IN packets */ 1773 /* we are expecting IN packets */
1753#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) 1774#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1775 defined(CONFIG_USB_TI_CPPI41_DMA)
1754 if (dma) { 1776 if (dma) {
1755 struct dma_controller *c; 1777 struct dma_controller *c;
1756 u16 rx_count; 1778 u16 rx_count;
diff --git a/drivers/usb/phy/phy-fsm-usb.c b/drivers/usb/phy/phy-fsm-usb.c
index 7aa314ef4a8a..c47e5a6edde2 100644
--- a/drivers/usb/phy/phy-fsm-usb.c
+++ b/drivers/usb/phy/phy-fsm-usb.c
@@ -317,10 +317,12 @@ int otg_statemachine(struct otg_fsm *fsm)
317 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 317 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
318 break; 318 break;
319 case OTG_STATE_A_HOST: 319 case OTG_STATE_A_HOST:
320 if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) && 320 if (fsm->id || fsm->a_bus_drop)
321 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
322 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
321 fsm->otg->host->b_hnp_enable) 323 fsm->otg->host->b_hnp_enable)
322 otg_set_state(fsm, OTG_STATE_A_SUSPEND); 324 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
323 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop) 325 else if (!fsm->b_conn)
324 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); 326 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
325 else if (!fsm->a_vbus_vld) 327 else if (!fsm->a_vbus_vld)
326 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 328 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
@@ -346,8 +348,7 @@ int otg_statemachine(struct otg_fsm *fsm)
346 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 348 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
347 break; 349 break;
348 case OTG_STATE_A_WAIT_VFALL: 350 case OTG_STATE_A_WAIT_VFALL:
349 if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req || 351 if (fsm->a_wait_vfall_tmout)
350 (!fsm->a_sess_vld && !fsm->b_conn))
351 otg_set_state(fsm, OTG_STATE_A_IDLE); 352 otg_set_state(fsm, OTG_STATE_A_IDLE);
352 break; 353 break;
353 case OTG_STATE_A_VBUS_ERR: 354 case OTG_STATE_A_VBUS_ERR:
diff --git a/drivers/usb/phy/phy-mxs-usb.c b/drivers/usb/phy/phy-mxs-usb.c
index b42897b6474c..c42bdf0c4a1f 100644
--- a/drivers/usb/phy/phy-mxs-usb.c
+++ b/drivers/usb/phy/phy-mxs-usb.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright 2012 Freescale Semiconductor, Inc. 2 * Copyright 2012-2013 Freescale Semiconductor, Inc.
3 * Copyright (C) 2012 Marek Vasut <marex@denx.de> 3 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH 4 * on behalf of DENX Software Engineering GmbH
5 * 5 *
@@ -20,6 +20,9 @@
20#include <linux/delay.h> 20#include <linux/delay.h>
21#include <linux/err.h> 21#include <linux/err.h>
22#include <linux/io.h> 22#include <linux/io.h>
23#include <linux/of_device.h>
24#include <linux/regmap.h>
25#include <linux/mfd/syscon.h>
23 26
24#define DRIVER_NAME "mxs_phy" 27#define DRIVER_NAME "mxs_phy"
25 28
@@ -28,18 +31,134 @@
28#define HW_USBPHY_CTRL_SET 0x34 31#define HW_USBPHY_CTRL_SET 0x34
29#define HW_USBPHY_CTRL_CLR 0x38 32#define HW_USBPHY_CTRL_CLR 0x38
30 33
34#define HW_USBPHY_DEBUG_SET 0x54
35#define HW_USBPHY_DEBUG_CLR 0x58
36
37#define HW_USBPHY_IP 0x90
38#define HW_USBPHY_IP_SET 0x94
39#define HW_USBPHY_IP_CLR 0x98
40
31#define BM_USBPHY_CTRL_SFTRST BIT(31) 41#define BM_USBPHY_CTRL_SFTRST BIT(31)
32#define BM_USBPHY_CTRL_CLKGATE BIT(30) 42#define BM_USBPHY_CTRL_CLKGATE BIT(30)
43#define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
44#define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
45#define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
46#define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
47#define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
48#define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
49#define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
50#define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
33#define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15) 51#define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
34#define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14) 52#define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
35#define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1) 53#define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
36 54
55#define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
56
57#define BM_USBPHY_DEBUG_CLKGATE BIT(30)
58
59/* Anatop Registers */
60#define ANADIG_ANA_MISC0 0x150
61#define ANADIG_ANA_MISC0_SET 0x154
62#define ANADIG_ANA_MISC0_CLR 0x158
63
64#define ANADIG_USB1_VBUS_DET_STAT 0x1c0
65#define ANADIG_USB2_VBUS_DET_STAT 0x220
66
67#define ANADIG_USB1_LOOPBACK_SET 0x1e4
68#define ANADIG_USB1_LOOPBACK_CLR 0x1e8
69#define ANADIG_USB2_LOOPBACK_SET 0x244
70#define ANADIG_USB2_LOOPBACK_CLR 0x248
71
72#define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG BIT(12)
73#define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL BIT(11)
74
75#define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
76#define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
77
78#define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
79#define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
80#define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
81#define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
82
83#define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
84
85/* Do disconnection between PHY and controller without vbus */
86#define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
87
88/*
89 * The PHY will be in messy if there is a wakeup after putting
90 * bus to suspend (set portsc.suspendM) but before setting PHY to low
91 * power mode (set portsc.phcd).
92 */
93#define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
94
95/*
96 * The SOF sends too fast after resuming, it will cause disconnection
97 * between host and high speed device.
98 */
99#define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
100
101/*
102 * IC has bug fixes logic, they include
103 * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
104 * which are described at above flags, the RTL will handle it
105 * according to different versions.
106 */
107#define MXS_PHY_NEED_IP_FIX BIT(3)
108
109struct mxs_phy_data {
110 unsigned int flags;
111};
112
113static const struct mxs_phy_data imx23_phy_data = {
114 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
115};
116
117static const struct mxs_phy_data imx6q_phy_data = {
118 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
119 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
120 MXS_PHY_NEED_IP_FIX,
121};
122
123static const struct mxs_phy_data imx6sl_phy_data = {
124 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
125 MXS_PHY_NEED_IP_FIX,
126};
127
128static const struct of_device_id mxs_phy_dt_ids[] = {
129 { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
130 { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
131 { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
132 { /* sentinel */ }
133};
134MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
135
37struct mxs_phy { 136struct mxs_phy {
38 struct usb_phy phy; 137 struct usb_phy phy;
39 struct clk *clk; 138 struct clk *clk;
139 const struct mxs_phy_data *data;
140 struct regmap *regmap_anatop;
141 int port_id;
40}; 142};
41 143
42#define to_mxs_phy(p) container_of((p), struct mxs_phy, phy) 144static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
145{
146 return mxs_phy->data == &imx6q_phy_data;
147}
148
149static inline bool is_imx6sl_phy(struct mxs_phy *mxs_phy)
150{
151 return mxs_phy->data == &imx6sl_phy_data;
152}
153
154/*
155 * PHY needs some 32K cycles to switch from 32K clock to
156 * bus (such as AHB/AXI, etc) clock.
157 */
158static void mxs_phy_clock_switch_delay(void)
159{
160 usleep_range(300, 400);
161}
43 162
44static int mxs_phy_hw_init(struct mxs_phy *mxs_phy) 163static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
45{ 164{
@@ -53,19 +172,105 @@ static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
53 /* Power up the PHY */ 172 /* Power up the PHY */
54 writel(0, base + HW_USBPHY_PWD); 173 writel(0, base + HW_USBPHY_PWD);
55 174
56 /* enable FS/LS device */ 175 /*
57 writel(BM_USBPHY_CTRL_ENUTMILEVEL2 | 176 * USB PHY Ctrl Setting
58 BM_USBPHY_CTRL_ENUTMILEVEL3, 177 * - Auto clock/power on
178 * - Enable full/low speed support
179 */
180 writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
181 BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
182 BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
183 BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
184 BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
185 BM_USBPHY_CTRL_ENUTMILEVEL2 |
186 BM_USBPHY_CTRL_ENUTMILEVEL3,
59 base + HW_USBPHY_CTRL_SET); 187 base + HW_USBPHY_CTRL_SET);
60 188
189 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
190 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
191
61 return 0; 192 return 0;
62} 193}
63 194
195/* Return true if the vbus is there */
196static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
197{
198 unsigned int vbus_value;
199
200 if (mxs_phy->port_id == 0)
201 regmap_read(mxs_phy->regmap_anatop,
202 ANADIG_USB1_VBUS_DET_STAT,
203 &vbus_value);
204 else if (mxs_phy->port_id == 1)
205 regmap_read(mxs_phy->regmap_anatop,
206 ANADIG_USB2_VBUS_DET_STAT,
207 &vbus_value);
208
209 if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
210 return true;
211 else
212 return false;
213}
214
215static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
216{
217 void __iomem *base = mxs_phy->phy.io_priv;
218 u32 reg;
219
220 if (disconnect)
221 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
222 base + HW_USBPHY_DEBUG_CLR);
223
224 if (mxs_phy->port_id == 0) {
225 reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
226 : ANADIG_USB1_LOOPBACK_CLR;
227 regmap_write(mxs_phy->regmap_anatop, reg,
228 BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
229 BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
230 } else if (mxs_phy->port_id == 1) {
231 reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
232 : ANADIG_USB2_LOOPBACK_CLR;
233 regmap_write(mxs_phy->regmap_anatop, reg,
234 BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
235 BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
236 }
237
238 if (!disconnect)
239 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
240 base + HW_USBPHY_DEBUG_SET);
241
242 /* Delay some time, and let Linestate be SE0 for controller */
243 if (disconnect)
244 usleep_range(500, 1000);
245}
246
247static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
248{
249 bool vbus_is_on = false;
250
251 /* If the SoCs don't need to disconnect line without vbus, quit */
252 if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
253 return;
254
255 /* If the SoCs don't have anatop, quit */
256 if (!mxs_phy->regmap_anatop)
257 return;
258
259 vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
260
261 if (on && !vbus_is_on)
262 __mxs_phy_disconnect_line(mxs_phy, true);
263 else
264 __mxs_phy_disconnect_line(mxs_phy, false);
265
266}
267
64static int mxs_phy_init(struct usb_phy *phy) 268static int mxs_phy_init(struct usb_phy *phy)
65{ 269{
66 int ret; 270 int ret;
67 struct mxs_phy *mxs_phy = to_mxs_phy(phy); 271 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
68 272
273 mxs_phy_clock_switch_delay();
69 ret = clk_prepare_enable(mxs_phy->clk); 274 ret = clk_prepare_enable(mxs_phy->clk);
70 if (ret) 275 if (ret)
71 return ret; 276 return ret;
@@ -94,6 +299,7 @@ static int mxs_phy_suspend(struct usb_phy *x, int suspend)
94 x->io_priv + HW_USBPHY_CTRL_SET); 299 x->io_priv + HW_USBPHY_CTRL_SET);
95 clk_disable_unprepare(mxs_phy->clk); 300 clk_disable_unprepare(mxs_phy->clk);
96 } else { 301 } else {
302 mxs_phy_clock_switch_delay();
97 ret = clk_prepare_enable(mxs_phy->clk); 303 ret = clk_prepare_enable(mxs_phy->clk);
98 if (ret) 304 if (ret)
99 return ret; 305 return ret;
@@ -105,11 +311,28 @@ static int mxs_phy_suspend(struct usb_phy *x, int suspend)
105 return 0; 311 return 0;
106} 312}
107 313
314static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
315{
316 struct mxs_phy *mxs_phy = to_mxs_phy(x);
317 u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
318 BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
319 BM_USBPHY_CTRL_ENIDCHG_WKUP;
320 if (enabled) {
321 mxs_phy_disconnect_line(mxs_phy, true);
322 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
323 } else {
324 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
325 mxs_phy_disconnect_line(mxs_phy, false);
326 }
327
328 return 0;
329}
330
108static int mxs_phy_on_connect(struct usb_phy *phy, 331static int mxs_phy_on_connect(struct usb_phy *phy,
109 enum usb_device_speed speed) 332 enum usb_device_speed speed)
110{ 333{
111 dev_dbg(phy->dev, "%s speed device has connected\n", 334 dev_dbg(phy->dev, "%s device has connected\n",
112 (speed == USB_SPEED_HIGH) ? "high" : "non-high"); 335 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
113 336
114 if (speed == USB_SPEED_HIGH) 337 if (speed == USB_SPEED_HIGH)
115 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, 338 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
@@ -121,8 +344,8 @@ static int mxs_phy_on_connect(struct usb_phy *phy,
121static int mxs_phy_on_disconnect(struct usb_phy *phy, 344static int mxs_phy_on_disconnect(struct usb_phy *phy,
122 enum usb_device_speed speed) 345 enum usb_device_speed speed)
123{ 346{
124 dev_dbg(phy->dev, "%s speed device has disconnected\n", 347 dev_dbg(phy->dev, "%s device has disconnected\n",
125 (speed == USB_SPEED_HIGH) ? "high" : "non-high"); 348 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
126 349
127 if (speed == USB_SPEED_HIGH) 350 if (speed == USB_SPEED_HIGH)
128 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, 351 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
@@ -138,6 +361,9 @@ static int mxs_phy_probe(struct platform_device *pdev)
138 struct clk *clk; 361 struct clk *clk;
139 struct mxs_phy *mxs_phy; 362 struct mxs_phy *mxs_phy;
140 int ret; 363 int ret;
364 const struct of_device_id *of_id =
365 of_match_device(mxs_phy_dt_ids, &pdev->dev);
366 struct device_node *np = pdev->dev.of_node;
141 367
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 368 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 base = devm_ioremap_resource(&pdev->dev, res); 369 base = devm_ioremap_resource(&pdev->dev, res);
@@ -157,6 +383,22 @@ static int mxs_phy_probe(struct platform_device *pdev)
157 return -ENOMEM; 383 return -ENOMEM;
158 } 384 }
159 385
386 /* Some SoCs don't have anatop registers */
387 if (of_get_property(np, "fsl,anatop", NULL)) {
388 mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
389 (np, "fsl,anatop");
390 if (IS_ERR(mxs_phy->regmap_anatop)) {
391 dev_dbg(&pdev->dev,
392 "failed to find regmap for anatop\n");
393 return PTR_ERR(mxs_phy->regmap_anatop);
394 }
395 }
396
397 ret = of_alias_get_id(np, "usbphy");
398 if (ret < 0)
399 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
400 mxs_phy->port_id = ret;
401
160 mxs_phy->phy.io_priv = base; 402 mxs_phy->phy.io_priv = base;
161 mxs_phy->phy.dev = &pdev->dev; 403 mxs_phy->phy.dev = &pdev->dev;
162 mxs_phy->phy.label = DRIVER_NAME; 404 mxs_phy->phy.label = DRIVER_NAME;
@@ -166,11 +408,15 @@ static int mxs_phy_probe(struct platform_device *pdev)
166 mxs_phy->phy.notify_connect = mxs_phy_on_connect; 408 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
167 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect; 409 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
168 mxs_phy->phy.type = USB_PHY_TYPE_USB2; 410 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
411 mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
169 412
170 mxs_phy->clk = clk; 413 mxs_phy->clk = clk;
414 mxs_phy->data = of_id->data;
171 415
172 platform_set_drvdata(pdev, mxs_phy); 416 platform_set_drvdata(pdev, mxs_phy);
173 417
418 device_set_wakeup_capable(&pdev->dev, true);
419
174 ret = usb_add_phy_dev(&mxs_phy->phy); 420 ret = usb_add_phy_dev(&mxs_phy->phy);
175 if (ret) 421 if (ret)
176 return ret; 422 return ret;
@@ -187,11 +433,46 @@ static int mxs_phy_remove(struct platform_device *pdev)
187 return 0; 433 return 0;
188} 434}
189 435
190static const struct of_device_id mxs_phy_dt_ids[] = { 436#ifdef CONFIG_PM_SLEEP
191 { .compatible = "fsl,imx23-usbphy", }, 437static void mxs_phy_enable_ldo_in_suspend(struct mxs_phy *mxs_phy, bool on)
192 { /* sentinel */ } 438{
193}; 439 unsigned int reg = on ? ANADIG_ANA_MISC0_SET : ANADIG_ANA_MISC0_CLR;
194MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids); 440
441 /* If the SoCs don't have anatop, quit */
442 if (!mxs_phy->regmap_anatop)
443 return;
444
445 if (is_imx6q_phy(mxs_phy))
446 regmap_write(mxs_phy->regmap_anatop, reg,
447 BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG);
448 else if (is_imx6sl_phy(mxs_phy))
449 regmap_write(mxs_phy->regmap_anatop,
450 reg, BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL);
451}
452
453static int mxs_phy_system_suspend(struct device *dev)
454{
455 struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
456
457 if (device_may_wakeup(dev))
458 mxs_phy_enable_ldo_in_suspend(mxs_phy, true);
459
460 return 0;
461}
462
463static int mxs_phy_system_resume(struct device *dev)
464{
465 struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
466
467 if (device_may_wakeup(dev))
468 mxs_phy_enable_ldo_in_suspend(mxs_phy, false);
469
470 return 0;
471}
472#endif /* CONFIG_PM_SLEEP */
473
474static SIMPLE_DEV_PM_OPS(mxs_phy_pm, mxs_phy_system_suspend,
475 mxs_phy_system_resume);
195 476
196static struct platform_driver mxs_phy_driver = { 477static struct platform_driver mxs_phy_driver = {
197 .probe = mxs_phy_probe, 478 .probe = mxs_phy_probe,
@@ -200,6 +481,7 @@ static struct platform_driver mxs_phy_driver = {
200 .name = DRIVER_NAME, 481 .name = DRIVER_NAME,
201 .owner = THIS_MODULE, 482 .owner = THIS_MODULE,
202 .of_match_table = mxs_phy_dt_ids, 483 .of_match_table = mxs_phy_dt_ids,
484 .pm = &mxs_phy_pm,
203 }, 485 },
204}; 486};
205 487
diff --git a/drivers/usb/phy/phy-rcar-gen2-usb.c b/drivers/usb/phy/phy-rcar-gen2-usb.c
index 551e0a6c0e22..388d89f6b141 100644
--- a/drivers/usb/phy/phy-rcar-gen2-usb.c
+++ b/drivers/usb/phy/phy-rcar-gen2-usb.c
@@ -177,15 +177,15 @@ static int rcar_gen2_usb_phy_probe(struct platform_device *pdev)
177 struct clk *clk; 177 struct clk *clk;
178 int retval; 178 int retval;
179 179
180 pdata = dev_get_platdata(&pdev->dev); 180 pdata = dev_get_platdata(dev);
181 if (!pdata) { 181 if (!pdata) {
182 dev_err(dev, "No platform data\n"); 182 dev_err(dev, "No platform data\n");
183 return -EINVAL; 183 return -EINVAL;
184 } 184 }
185 185
186 clk = devm_clk_get(&pdev->dev, "usbhs"); 186 clk = devm_clk_get(dev, "usbhs");
187 if (IS_ERR(clk)) { 187 if (IS_ERR(clk)) {
188 dev_err(&pdev->dev, "Can't get the clock\n"); 188 dev_err(dev, "Can't get the clock\n");
189 return PTR_ERR(clk); 189 return PTR_ERR(clk);
190 } 190 }
191 191
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 6c0b1c513db7..353053a33f21 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -111,6 +111,13 @@ struct usb_phy {
111 int (*set_suspend)(struct usb_phy *x, 111 int (*set_suspend)(struct usb_phy *x,
112 int suspend); 112 int suspend);
113 113
114 /*
115 * Set wakeup enable for PHY, in that case, the PHY can be
116 * woken up from suspend status due to external events,
117 * like vbus change, dp/dm change and id.
118 */
119 int (*set_wakeup)(struct usb_phy *x, bool enabled);
120
114 /* notify phy connect status change */ 121 /* notify phy connect status change */
115 int (*notify_connect)(struct usb_phy *x, 122 int (*notify_connect)(struct usb_phy *x,
116 enum usb_device_speed speed); 123 enum usb_device_speed speed);
@@ -265,6 +272,15 @@ usb_phy_set_suspend(struct usb_phy *x, int suspend)
265} 272}
266 273
267static inline int 274static inline int
275usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
276{
277 if (x && x->set_wakeup)
278 return x->set_wakeup(x, enabled);
279 else
280 return 0;
281}
282
283static inline int
268usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed) 284usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
269{ 285{
270 if (x && x->notify_connect) 286 if (x && x->notify_connect)
diff --git a/include/uapi/linux/usb/functionfs.h b/include/uapi/linux/usb/functionfs.h
index d6b01283f85c..2a4b4a72a4f9 100644
--- a/include/uapi/linux/usb/functionfs.h
+++ b/include/uapi/linux/usb/functionfs.h
@@ -10,9 +10,15 @@
10 10
11enum { 11enum {
12 FUNCTIONFS_DESCRIPTORS_MAGIC = 1, 12 FUNCTIONFS_DESCRIPTORS_MAGIC = 1,
13 FUNCTIONFS_STRINGS_MAGIC = 2 13 FUNCTIONFS_STRINGS_MAGIC = 2,
14 FUNCTIONFS_DESCRIPTORS_MAGIC_V2 = 3,
14}; 15};
15 16
17enum functionfs_flags {
18 FUNCTIONFS_HAS_FS_DESC = 1,
19 FUNCTIONFS_HAS_HS_DESC = 2,
20 FUNCTIONFS_HAS_SS_DESC = 4,
21};
16 22
17#ifndef __KERNEL__ 23#ifndef __KERNEL__
18 24
@@ -29,29 +35,39 @@ struct usb_endpoint_descriptor_no_audio {
29 35
30 36
31/* 37/*
32 * All numbers must be in little endian order.
33 */
34
35struct usb_functionfs_descs_head {
36 __le32 magic;
37 __le32 length;
38 __le32 fs_count;
39 __le32 hs_count;
40} __attribute__((packed));
41
42/*
43 * Descriptors format: 38 * Descriptors format:
44 * 39 *
45 * | off | name | type | description | 40 * | off | name | type | description |
46 * |-----+-----------+--------------+--------------------------------------| 41 * |-----+-----------+--------------+--------------------------------------|
47 * | 0 | magic | LE32 | FUNCTIONFS_{FS,HS}_DESCRIPTORS_MAGIC | 42 * | 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC_V2 |
43 * | 4 | length | LE32 | length of the whole data chunk |
44 * | 8 | flags | LE32 | combination of functionfs_flags |
45 * | | fs_count | LE32 | number of full-speed descriptors |
46 * | | hs_count | LE32 | number of high-speed descriptors |
47 * | | ss_count | LE32 | number of super-speed descriptors |
48 * | | fs_descrs | Descriptor[] | list of full-speed descriptors |
49 * | | hs_descrs | Descriptor[] | list of high-speed descriptors |
50 * | | ss_descrs | Descriptor[] | list of super-speed descriptors |
51 *
52 * Depending on which flags are set, various fields may be missing in the
53 * structure. Any flags that are not recognised cause the whole block to be
54 * rejected with -ENOSYS.
55 *
56 * Legacy descriptors format:
57 *
58 * | off | name | type | description |
59 * |-----+-----------+--------------+--------------------------------------|
60 * | 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC |
48 * | 4 | length | LE32 | length of the whole data chunk | 61 * | 4 | length | LE32 | length of the whole data chunk |
49 * | 8 | fs_count | LE32 | number of full-speed descriptors | 62 * | 8 | fs_count | LE32 | number of full-speed descriptors |
50 * | 12 | hs_count | LE32 | number of high-speed descriptors | 63 * | 12 | hs_count | LE32 | number of high-speed descriptors |
51 * | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors | 64 * | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors |
52 * | | hs_descrs | Descriptor[] | list of high-speed descriptors | 65 * | | hs_descrs | Descriptor[] | list of high-speed descriptors |
53 * 66 *
54 * descs are just valid USB descriptors and have the following format: 67 * All numbers must be in little endian order.
68 *
69 * Descriptor[] is an array of valid USB descriptors which have the following
70 * format:
55 * 71 *
56 * | off | name | type | description | 72 * | off | name | type | description |
57 * |-----+-----------------+------+--------------------------| 73 * |-----+-----------------+------+--------------------------|