aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-09-14 14:37:50 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-09-14 14:37:50 -0400
commit81522637485dd6ec9de4279c9714d58f884b6091 (patch)
tree74657b5881c08b1e6a7042482e593897e678afe2
parent54a2ec67f1db62a763f57b7f8f2e82874f5f358b (diff)
parente6be244a83211f3a9daaf5e29ee97fe0bf1efe5a (diff)
Merge tag 'usb-for-v4.9' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb into usb-next
Felipe writes: usb: patches for v4.9 merge window This time around we have 92 non-merge commits. Most of the changes are in drivers/usb/gadget (40.3%) with drivers/usb/gadget/function being the most active directory (27.2%). As for UDC drivers, only dwc3 (26.5%) and dwc2 (12.7%) have really been active. The most important changes for dwc3 are better support for scatterlist and, again, throughput improvements. While on dwc2 got some minor stability fixes related to soft reset and FIFO usage. Felipe Tonello has done some good work fixing up our f_midi gadget and Tal Shorer has implemented a nice API change for our ULPI bus. Apart from these, we have our usual set of non-critical fixes, spelling fixes, build warning fixes, etc.
-rw-r--r--Documentation/devicetree/bindings/phy/mxs-usb-phy.txt10
-rw-r--r--Documentation/devicetree/bindings/usb/dwc2.txt5
-rw-r--r--Documentation/devicetree/bindings/usb/dwc3-cavium.txt28
-rw-r--r--Documentation/devicetree/bindings/usb/dwc3.txt8
-rw-r--r--Documentation/devicetree/bindings/usb/generic.txt6
-rw-r--r--Documentation/devicetree/bindings/usb/renesas_usbhs.txt1
-rw-r--r--Documentation/devicetree/bindings/usb/rockchip,dwc3.txt59
-rw-r--r--drivers/usb/common/ulpi.c11
-rw-r--r--drivers/usb/dwc2/core.c126
-rw-r--r--drivers/usb/dwc2/core.h8
-rw-r--r--drivers/usb/dwc2/gadget.c86
-rw-r--r--drivers/usb/dwc2/hw.h1
-rw-r--r--drivers/usb/dwc3/core.c122
-rw-r--r--drivers/usb/dwc3/core.h35
-rw-r--r--drivers/usb/dwc3/dwc3-of-simple.c48
-rw-r--r--drivers/usb/dwc3/gadget.c278
-rw-r--r--drivers/usb/dwc3/ulpi.c10
-rw-r--r--drivers/usb/gadget/Kconfig39
-rw-r--r--drivers/usb/gadget/composite.c16
-rw-r--r--drivers/usb/gadget/configfs.c5
-rw-r--r--drivers/usb/gadget/function/f_fs.c45
-rw-r--r--drivers/usb/gadget/function/f_hid.c28
-rw-r--r--drivers/usb/gadget/function/f_loopback.c14
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.c28
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.h1
-rw-r--r--drivers/usb/gadget/function/f_midi.c234
-rw-r--r--drivers/usb/gadget/function/f_ncm.c84
-rw-r--r--drivers/usb/gadget/function/f_printer.c6
-rw-r--r--drivers/usb/gadget/function/f_sourcesink.c6
-rw-r--r--drivers/usb/gadget/function/f_uvc.c7
-rw-r--r--drivers/usb/gadget/function/storage_common.c24
-rw-r--r--drivers/usb/gadget/function/storage_common.h10
-rw-r--r--drivers/usb/gadget/function/u_ether.c15
-rw-r--r--drivers/usb/gadget/function/u_ether.h1
-rw-r--r--drivers/usb/gadget/legacy/gmidi.c2
-rw-r--r--drivers/usb/gadget/u_f.c6
-rw-r--r--drivers/usb/gadget/u_f.h17
-rw-r--r--drivers/usb/gadget/udc/core.c4
-rw-r--r--drivers/usb/gadget/udc/goku_udc.c2
-rw-r--r--drivers/usb/gadget/udc/net2280.c80
-rw-r--r--drivers/usb/gadget/udc/pxa27x_udc.c51
-rw-r--r--drivers/usb/phy/phy-ab8500-usb.c8
-rw-r--r--drivers/usb/phy/phy-generic.c9
-rw-r--r--drivers/usb/phy/phy-mxs-usb.c61
-rw-r--r--drivers/usb/renesas_usbhs/common.c4
-rw-r--r--drivers/usb/renesas_usbhs/mod_gadget.c2
-rw-r--r--include/linux/ulpi/driver.h2
-rw-r--r--include/linux/ulpi/interface.h9
-rw-r--r--include/linux/usb/composite.h3
-rw-r--r--include/linux/usb/gadget.h30
-rw-r--r--include/uapi/linux/usb/functionfs.h2
51 files changed, 1198 insertions, 499 deletions
diff --git a/Documentation/devicetree/bindings/phy/mxs-usb-phy.txt b/Documentation/devicetree/bindings/phy/mxs-usb-phy.txt
index 379b84a567cc..1d25b04cd05e 100644
--- a/Documentation/devicetree/bindings/phy/mxs-usb-phy.txt
+++ b/Documentation/devicetree/bindings/phy/mxs-usb-phy.txt
@@ -12,6 +12,16 @@ Required properties:
12- interrupts: Should contain phy interrupt 12- interrupts: Should contain phy interrupt
13- fsl,anatop: phandle for anatop register, it is only for imx6 SoC series 13- fsl,anatop: phandle for anatop register, it is only for imx6 SoC series
14 14
15Optional properties:
16- fsl,tx-cal-45-dn-ohms: Integer [30-55]. Resistance (in ohms) of switchable
17 high-speed trimming resistor connected in parallel with the 45 ohm resistor
18 that terminates the DN output signal. Default: 45
19- fsl,tx-cal-45-dp-ohms: Integer [30-55]. Resistance (in ohms) of switchable
20 high-speed trimming resistor connected in parallel with the 45 ohm resistor
21 that terminates the DP output signal. Default: 45
22- fsl,tx-d-cal: Integer [79-119]. Current trimming value (as a percentage) of
23 the 17.78mA TX reference current. Default: 100
24
15Example: 25Example:
16usbphy1: usbphy@020c9000 { 26usbphy1: usbphy@020c9000 {
17 compatible = "fsl,imx6q-usbphy", "fsl,imx23-usbphy"; 27 compatible = "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
diff --git a/Documentation/devicetree/bindings/usb/dwc2.txt b/Documentation/devicetree/bindings/usb/dwc2.txt
index 20a68bf2b4e7..7d16ebfaa5a1 100644
--- a/Documentation/devicetree/bindings/usb/dwc2.txt
+++ b/Documentation/devicetree/bindings/usb/dwc2.txt
@@ -26,7 +26,10 @@ Refer to phy/phy-bindings.txt for generic phy consumer properties
26- g-use-dma: enable dma usage in gadget driver. 26- g-use-dma: enable dma usage in gadget driver.
27- g-rx-fifo-size: size of rx fifo size in gadget mode. 27- g-rx-fifo-size: size of rx fifo size in gadget mode.
28- g-np-tx-fifo-size: size of non-periodic tx fifo size in gadget mode. 28- g-np-tx-fifo-size: size of non-periodic tx fifo size in gadget mode.
29- g-tx-fifo-size: size of periodic tx fifo per endpoint (except ep0) in gadget mode. 29
30Deprecated properties:
31- g-tx-fifo-size: size of periodic tx fifo per endpoint (except ep0)
32 in gadget mode.
30 33
31Example: 34Example:
32 35
diff --git a/Documentation/devicetree/bindings/usb/dwc3-cavium.txt b/Documentation/devicetree/bindings/usb/dwc3-cavium.txt
new file mode 100644
index 000000000000..710b782ccf65
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/dwc3-cavium.txt
@@ -0,0 +1,28 @@
1Cavium SuperSpeed DWC3 USB SoC controller
2
3Required properties:
4- compatible: Should contain "cavium,octeon-7130-usb-uctl"
5
6Required child node:
7A child node must exist to represent the core DWC3 IP block. The name of
8the node is not important. The content of the node is defined in dwc3.txt.
9
10Example device node:
11
12 uctl@1180069000000 {
13 compatible = "cavium,octeon-7130-usb-uctl";
14 reg = <0x00011800 0x69000000 0x00000000 0x00000100>;
15 ranges;
16 #address-cells = <0x00000002>;
17 #size-cells = <0x00000002>;
18 refclk-frequency = <0x05f5e100>;
19 refclk-type-ss = "dlmc_ref_clk0";
20 refclk-type-hs = "dlmc_ref_clk0";
21 power = <0x00000002 0x00000002 0x00000001>;
22 xhci@1690000000000 {
23 compatible = "cavium,octeon-7130-xhci", "synopsys,dwc3";
24 reg = <0x00016900 0x00000000 0x00000010 0x00000000>;
25 interrupt-parent = <0x00000010>;
26 interrupts = <0x00000009 0x00000004>;
27 };
28 };
diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt
index 7d7ce089b003..e3e6983288e3 100644
--- a/Documentation/devicetree/bindings/usb/dwc3.txt
+++ b/Documentation/devicetree/bindings/usb/dwc3.txt
@@ -13,7 +13,8 @@ Optional properties:
13 in the array is expected to be a handle to the USB2/HS PHY and 13 in the array is expected to be a handle to the USB2/HS PHY and
14 the second element is expected to be a handle to the USB3/SS PHY 14 the second element is expected to be a handle to the USB3/SS PHY
15 - phys: from the *Generic PHY* bindings 15 - phys: from the *Generic PHY* bindings
16 - phy-names: from the *Generic PHY* bindings 16 - phy-names: from the *Generic PHY* bindings; supported names are "usb2-phy"
17 or "usb3-phy".
17 - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable 18 - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable
18 - snps,disable_scramble_quirk: true when SW should disable data scrambling. 19 - snps,disable_scramble_quirk: true when SW should disable data scrambling.
19 Only really useful for FPGA builds. 20 Only really useful for FPGA builds.
@@ -39,6 +40,11 @@ Optional properties:
39 disabling the suspend signal to the PHY. 40 disabling the suspend signal to the PHY.
40 - snps,dis_rxdet_inp3_quirk: when set core will disable receiver detection 41 - snps,dis_rxdet_inp3_quirk: when set core will disable receiver detection
41 in PHY P3 power state. 42 in PHY P3 power state.
43 - snps,dis-u2-freeclk-exists-quirk: when set, clear the u2_freeclk_exists
44 in GUSB2PHYCFG, specify that USB2 PHY doesn't provide
45 a free-running PHY clock.
46 - snps,dis-del-phy-power-chg-quirk: when set core will change PHY power
47 from P0 to P1/P2/P3 without delay.
42 - snps,is-utmi-l1-suspend: true when DWC3 asserts output signal 48 - snps,is-utmi-l1-suspend: true when DWC3 asserts output signal
43 utmi_l1_suspend_n, false when asserts utmi_sleep_n 49 utmi_l1_suspend_n, false when asserts utmi_sleep_n
44 - snps,hird-threshold: HIRD threshold 50 - snps,hird-threshold: HIRD threshold
diff --git a/Documentation/devicetree/bindings/usb/generic.txt b/Documentation/devicetree/bindings/usb/generic.txt
index bba825711873..bfadeb1c3bab 100644
--- a/Documentation/devicetree/bindings/usb/generic.txt
+++ b/Documentation/devicetree/bindings/usb/generic.txt
@@ -11,6 +11,11 @@ Optional properties:
11 "peripheral" and "otg". In case this attribute isn't 11 "peripheral" and "otg". In case this attribute isn't
12 passed via DT, USB DRD controllers should default to 12 passed via DT, USB DRD controllers should default to
13 OTG. 13 OTG.
14 - phy_type: tells USB controllers that we want to configure the core to support
15 a UTMI+ PHY with an 8- or 16-bit interface if UTMI+ is
16 selected. Valid arguments are "utmi" and "utmi_wide".
17 In case this isn't passed via DT, USB controllers should
18 default to HW capability.
14 - otg-rev: tells usb driver the release number of the OTG and EH supplement 19 - otg-rev: tells usb driver the release number of the OTG and EH supplement
15 with which the device and its descriptors are compliant, 20 with which the device and its descriptors are compliant,
16 in binary-coded decimal (i.e. 2.0 is 0200H). This 21 in binary-coded decimal (i.e. 2.0 is 0200H). This
@@ -34,6 +39,7 @@ dwc3@4a030000 {
34 usb-phy = <&usb2_phy>, <&usb3,phy>; 39 usb-phy = <&usb2_phy>, <&usb3,phy>;
35 maximum-speed = "super-speed"; 40 maximum-speed = "super-speed";
36 dr_mode = "otg"; 41 dr_mode = "otg";
42 phy_type = "utmi_wide";
37 otg-rev = <0x0200>; 43 otg-rev = <0x0200>;
38 adp-disable; 44 adp-disable;
39}; 45};
diff --git a/Documentation/devicetree/bindings/usb/renesas_usbhs.txt b/Documentation/devicetree/bindings/usb/renesas_usbhs.txt
index b6040563e51a..9e18e000339e 100644
--- a/Documentation/devicetree/bindings/usb/renesas_usbhs.txt
+++ b/Documentation/devicetree/bindings/usb/renesas_usbhs.txt
@@ -9,6 +9,7 @@ Required properties:
9 - "renesas,usbhs-r8a7793" for r8a7793 (R-Car M2-N) compatible device 9 - "renesas,usbhs-r8a7793" for r8a7793 (R-Car M2-N) compatible device
10 - "renesas,usbhs-r8a7794" for r8a7794 (R-Car E2) compatible device 10 - "renesas,usbhs-r8a7794" for r8a7794 (R-Car E2) compatible device
11 - "renesas,usbhs-r8a7795" for r8a7795 (R-Car H3) compatible device 11 - "renesas,usbhs-r8a7795" for r8a7795 (R-Car H3) compatible device
12 - "renesas,usbhs-r8a7796" for r8a7796 (R-Car M3-W) compatible device
12 - "renesas,rcar-gen2-usbhs" for R-Car Gen2 compatible device 13 - "renesas,rcar-gen2-usbhs" for R-Car Gen2 compatible device
13 - "renesas,rcar-gen3-usbhs" for R-Car Gen3 compatible device 14 - "renesas,rcar-gen3-usbhs" for R-Car Gen3 compatible device
14 15
diff --git a/Documentation/devicetree/bindings/usb/rockchip,dwc3.txt b/Documentation/devicetree/bindings/usb/rockchip,dwc3.txt
new file mode 100644
index 000000000000..0536a938e3ab
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/rockchip,dwc3.txt
@@ -0,0 +1,59 @@
1Rockchip SuperSpeed DWC3 USB SoC controller
2
3Required properties:
4- compatible: should contain "rockchip,rk3399-dwc3" for rk3399 SoC
5- clocks: A list of phandle + clock-specifier pairs for the
6 clocks listed in clock-names
7- clock-names: Should contain the following:
8 "ref_clk" Controller reference clk, have to be 24 MHz
9 "suspend_clk" Controller suspend clk, have to be 24 MHz or 32 KHz
10 "bus_clk" Master/Core clock, have to be >= 62.5 MHz for SS
11 operation and >= 30MHz for HS operation
12 "grf_clk" Controller grf clk
13
14Required child node:
15A child node must exist to represent the core DWC3 IP block. The name of
16the node is not important. The content of the node is defined in dwc3.txt.
17
18Phy documentation is provided in the following places:
19Documentation/devicetree/bindings/phy/rockchip,dwc3-usb-phy.txt
20
21Example device nodes:
22
23 usbdrd3_0: usb@fe800000 {
24 compatible = "rockchip,rk3399-dwc3";
25 clocks = <&cru SCLK_USB3OTG0_REF>, <&cru SCLK_USB3OTG0_SUSPEND>,
26 <&cru ACLK_USB3OTG0>, <&cru ACLK_USB3_GRF>;
27 clock-names = "ref_clk", "suspend_clk",
28 "bus_clk", "grf_clk";
29 #address-cells = <2>;
30 #size-cells = <2>;
31 ranges;
32 status = "disabled";
33 usbdrd_dwc3_0: dwc3@fe800000 {
34 compatible = "snps,dwc3";
35 reg = <0x0 0xfe800000 0x0 0x100000>;
36 interrupts = <GIC_SPI 105 IRQ_TYPE_LEVEL_HIGH>;
37 dr_mode = "otg";
38 status = "disabled";
39 };
40 };
41
42 usbdrd3_1: usb@fe900000 {
43 compatible = "rockchip,rk3399-dwc3";
44 clocks = <&cru SCLK_USB3OTG1_REF>, <&cru SCLK_USB3OTG1_SUSPEND>,
45 <&cru ACLK_USB3OTG1>, <&cru ACLK_USB3_GRF>;
46 clock-names = "ref_clk", "suspend_clk",
47 "bus_clk", "grf_clk";
48 #address-cells = <2>;
49 #size-cells = <2>;
50 ranges;
51 status = "disabled";
52 usbdrd_dwc3_1: dwc3@fe900000 {
53 compatible = "snps,dwc3";
54 reg = <0x0 0xfe900000 0x0 0x100000>;
55 interrupts = <GIC_SPI 110 IRQ_TYPE_LEVEL_HIGH>;
56 dr_mode = "otg";
57 status = "disabled";
58 };
59 };
diff --git a/drivers/usb/common/ulpi.c b/drivers/usb/common/ulpi.c
index e04a34e7a341..8b317702d761 100644
--- a/drivers/usb/common/ulpi.c
+++ b/drivers/usb/common/ulpi.c
@@ -21,13 +21,13 @@
21 21
22int ulpi_read(struct ulpi *ulpi, u8 addr) 22int ulpi_read(struct ulpi *ulpi, u8 addr)
23{ 23{
24 return ulpi->ops->read(ulpi->ops, addr); 24 return ulpi->ops->read(ulpi->dev.parent, addr);
25} 25}
26EXPORT_SYMBOL_GPL(ulpi_read); 26EXPORT_SYMBOL_GPL(ulpi_read);
27 27
28int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val) 28int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
29{ 29{
30 return ulpi->ops->write(ulpi->ops, addr, val); 30 return ulpi->ops->write(ulpi->dev.parent, addr, val);
31} 31}
32EXPORT_SYMBOL_GPL(ulpi_write); 32EXPORT_SYMBOL_GPL(ulpi_write);
33 33
@@ -157,6 +157,8 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
157{ 157{
158 int ret; 158 int ret;
159 159
160 ulpi->dev.parent = dev; /* needed early for ops */
161
160 /* Test the interface */ 162 /* Test the interface */
161 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa); 163 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
162 if (ret < 0) 164 if (ret < 0)
@@ -175,7 +177,6 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
175 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW); 177 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
176 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8; 178 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
177 179
178 ulpi->dev.parent = dev;
179 ulpi->dev.bus = &ulpi_bus; 180 ulpi->dev.bus = &ulpi_bus;
180 ulpi->dev.type = &ulpi_dev_type; 181 ulpi->dev.type = &ulpi_dev_type;
181 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev)); 182 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
@@ -202,7 +203,8 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
202 * Allocates and registers a ULPI device and an interface for it. Called from 203 * Allocates and registers a ULPI device and an interface for it. Called from
203 * the USB controller that provides the ULPI interface. 204 * the USB controller that provides the ULPI interface.
204 */ 205 */
205struct ulpi *ulpi_register_interface(struct device *dev, struct ulpi_ops *ops) 206struct ulpi *ulpi_register_interface(struct device *dev,
207 const struct ulpi_ops *ops)
206{ 208{
207 struct ulpi *ulpi; 209 struct ulpi *ulpi;
208 int ret; 210 int ret;
@@ -212,7 +214,6 @@ struct ulpi *ulpi_register_interface(struct device *dev, struct ulpi_ops *ops)
212 return ERR_PTR(-ENOMEM); 214 return ERR_PTR(-ENOMEM);
213 215
214 ulpi->ops = ops; 216 ulpi->ops = ops;
215 ops->dev = dev;
216 217
217 ret = ulpi_register(dev, ulpi); 218 ret = ulpi_register(dev, ulpi);
218 if (ret) { 219 if (ret) {
diff --git a/drivers/usb/dwc2/core.c b/drivers/usb/dwc2/core.c
index 4135a5ff67ca..fa9b26b91507 100644
--- a/drivers/usb/dwc2/core.c
+++ b/drivers/usb/dwc2/core.c
@@ -238,6 +238,77 @@ int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
238 return ret; 238 return ret;
239} 239}
240 240
241/**
242 * dwc2_wait_for_mode() - Waits for the controller mode.
243 * @hsotg: Programming view of the DWC_otg controller.
244 * @host_mode: If true, waits for host mode, otherwise device mode.
245 */
246static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
247 bool host_mode)
248{
249 ktime_t start;
250 ktime_t end;
251 unsigned int timeout = 110;
252
253 dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
254 host_mode ? "host" : "device");
255
256 start = ktime_get();
257
258 while (1) {
259 s64 ms;
260
261 if (dwc2_is_host_mode(hsotg) == host_mode) {
262 dev_vdbg(hsotg->dev, "%s mode set\n",
263 host_mode ? "Host" : "Device");
264 break;
265 }
266
267 end = ktime_get();
268 ms = ktime_to_ms(ktime_sub(end, start));
269
270 if (ms >= (s64)timeout) {
271 dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
272 __func__, host_mode ? "host" : "device");
273 break;
274 }
275
276 usleep_range(1000, 2000);
277 }
278}
279
280/**
281 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
282 * filter is enabled.
283 */
284static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
285{
286 u32 gsnpsid;
287 u32 ghwcfg4;
288
289 if (!dwc2_hw_is_otg(hsotg))
290 return false;
291
292 /* Check if core configuration includes the IDDIG filter. */
293 ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
294 if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
295 return false;
296
297 /*
298 * Check if the IDDIG debounce filter is bypassed. Available
299 * in core version >= 3.10a.
300 */
301 gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
302 if (gsnpsid >= DWC2_CORE_REV_3_10a) {
303 u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
304
305 if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
306 return false;
307 }
308
309 return true;
310}
311
241/* 312/*
242 * Do core a soft reset of the core. Be careful with this because it 313 * Do core a soft reset of the core. Be careful with this because it
243 * resets all the internal state machines of the core. 314 * resets all the internal state machines of the core.
@@ -246,9 +317,30 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
246{ 317{
247 u32 greset; 318 u32 greset;
248 int count = 0; 319 int count = 0;
320 bool wait_for_host_mode = false;
249 321
250 dev_vdbg(hsotg->dev, "%s()\n", __func__); 322 dev_vdbg(hsotg->dev, "%s()\n", __func__);
251 323
324 /*
325 * If the current mode is host, either due to the force mode
326 * bit being set (which persists after core reset) or the
327 * connector id pin, a core soft reset will temporarily reset
328 * the mode to device. A delay from the IDDIG debounce filter
329 * will occur before going back to host mode.
330 *
331 * Determine whether we will go back into host mode after a
332 * reset and account for this delay after the reset.
333 */
334 if (dwc2_iddig_filter_enabled(hsotg)) {
335 u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
336 u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
337
338 if (!(gotgctl & GOTGCTL_CONID_B) ||
339 (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
340 wait_for_host_mode = true;
341 }
342 }
343
252 /* Core Soft Reset */ 344 /* Core Soft Reset */
253 greset = dwc2_readl(hsotg->regs + GRSTCTL); 345 greset = dwc2_readl(hsotg->regs + GRSTCTL);
254 greset |= GRSTCTL_CSFTRST; 346 greset |= GRSTCTL_CSFTRST;
@@ -277,6 +369,9 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
277 } 369 }
278 } while (!(greset & GRSTCTL_AHBIDLE)); 370 } while (!(greset & GRSTCTL_AHBIDLE));
279 371
372 if (wait_for_host_mode)
373 dwc2_wait_for_mode(hsotg, true);
374
280 return 0; 375 return 0;
281} 376}
282 377
@@ -300,9 +395,9 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
300 * Checks are done in this function to determine whether doing a force 395 * Checks are done in this function to determine whether doing a force
301 * would be valid or not. 396 * would be valid or not.
302 * 397 *
303 * If a force is done, it requires a 25ms delay to take effect. 398 * If a force is done, it requires a IDDIG debounce filter delay if
304 * 399 * the filter is configured and enabled. We poll the current mode of
305 * Returns true if the mode was forced. 400 * the controller to account for this delay.
306 */ 401 */
307static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host) 402static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
308{ 403{
@@ -337,12 +432,18 @@ static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
337 gusbcfg |= set; 432 gusbcfg |= set;
338 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG); 433 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
339 434
340 msleep(25); 435 dwc2_wait_for_mode(hsotg, host);
341 return true; 436 return true;
342} 437}
343 438
344/* 439/**
345 * Clears the force mode bits. 440 * dwc2_clear_force_mode() - Clears the force mode bits.
441 *
442 * After clearing the bits, wait up to 100 ms to account for any
443 * potential IDDIG filter delay. We can't know if we expect this delay
444 * or not because the value of the connector ID status is affected by
445 * the force mode. We only need to call this once during probe if
446 * dr_mode == OTG.
346 */ 447 */
347static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg) 448static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
348{ 449{
@@ -353,11 +454,8 @@ static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
353 gusbcfg &= ~GUSBCFG_FORCEDEVMODE; 454 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
354 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG); 455 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
355 456
356 /* 457 if (dwc2_iddig_filter_enabled(hsotg))
357 * NOTE: This long sleep is _very_ important, otherwise the core will 458 usleep_range(100000, 110000);
358 * not stay in host mode after a connector ID change!
359 */
360 msleep(25);
361} 459}
362 460
363/* 461/*
@@ -380,12 +478,6 @@ void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
380 __func__, hsotg->dr_mode); 478 __func__, hsotg->dr_mode);
381 break; 479 break;
382 } 480 }
383
384 /*
385 * NOTE: This is required for some rockchip soc based
386 * platforms.
387 */
388 msleep(50);
389} 481}
390 482
391/* 483/*
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index d64551243789..aad4107ef927 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -259,13 +259,6 @@ enum dwc2_lx_state {
259 DWC2_L3, /* Off state */ 259 DWC2_L3, /* Off state */
260}; 260};
261 261
262/*
263 * Gadget periodic tx fifo sizes as used by legacy driver
264 * EP0 is not included
265 */
266#define DWC2_G_P_LEGACY_TX_FIFO_SIZE {256, 256, 256, 256, 768, 768, 768, \
267 768, 0, 0, 0, 0, 0, 0, 0}
268
269/* Gadget ep0 states */ 262/* Gadget ep0 states */
270enum dwc2_ep0_state { 263enum dwc2_ep0_state {
271 DWC2_EP0_SETUP, 264 DWC2_EP0_SETUP,
@@ -890,6 +883,7 @@ struct dwc2_hsotg {
890#define DWC2_CORE_REV_2_92a 0x4f54292a 883#define DWC2_CORE_REV_2_92a 0x4f54292a
891#define DWC2_CORE_REV_2_94a 0x4f54294a 884#define DWC2_CORE_REV_2_94a 0x4f54294a
892#define DWC2_CORE_REV_3_00a 0x4f54300a 885#define DWC2_CORE_REV_3_00a 0x4f54300a
886#define DWC2_CORE_REV_3_10a 0x4f54310a
893 887
894#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) 888#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
895 union dwc2_hcd_internal_flags { 889 union dwc2_hcd_internal_flags {
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 2ff03ae08e14..4cd6403a7566 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -186,9 +186,10 @@ static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
186 */ 186 */
187static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) 187static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
188{ 188{
189 unsigned int ep; 189 unsigned int fifo;
190 unsigned int addr; 190 unsigned int addr;
191 int timeout; 191 int timeout;
192 u32 dptxfsizn;
192 u32 val; 193 u32 val;
193 194
194 /* Reset fifo map if not correctly cleared during previous session */ 195 /* Reset fifo map if not correctly cleared during previous session */
@@ -216,16 +217,16 @@ static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
216 * them to endpoints dynamically according to maxpacket size value of 217 * them to endpoints dynamically according to maxpacket size value of
217 * given endpoint. 218 * given endpoint.
218 */ 219 */
219 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { 220 for (fifo = 1; fifo < MAX_EPS_CHANNELS; fifo++) {
220 if (!hsotg->g_tx_fifo_sz[ep]) 221 dptxfsizn = dwc2_readl(hsotg->regs + DPTXFSIZN(fifo));
221 continue; 222
222 val = addr; 223 val = (dptxfsizn & FIFOSIZE_DEPTH_MASK) | addr;
223 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT; 224 addr += dptxfsizn >> FIFOSIZE_DEPTH_SHIFT;
224 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem, 225
225 "insufficient fifo memory"); 226 if (addr > hsotg->fifo_mem)
226 addr += hsotg->g_tx_fifo_sz[ep]; 227 break;
227 228
228 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep)); 229 dwc2_writel(val, hsotg->regs + DPTXFSIZN(fifo));
229 } 230 }
230 231
231 /* 232 /*
@@ -388,7 +389,8 @@ static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
388 return -ENOSPC; 389 return -ENOSPC;
389 } 390 }
390 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { 391 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
391 can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index)); 392 can_write = dwc2_readl(hsotg->regs +
393 DTXFSTS(hs_ep->fifo_index));
392 394
393 can_write &= 0xffff; 395 can_write &= 0xffff;
394 can_write *= 4; 396 can_write *= 4;
@@ -2432,7 +2434,7 @@ static void kill_all_requests(struct dwc2_hsotg *hsotg,
2432 2434
2433 if (!hsotg->dedicated_fifos) 2435 if (!hsotg->dedicated_fifos)
2434 return; 2436 return;
2435 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4; 2437 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
2436 if (size < ep->fifo_size) 2438 if (size < ep->fifo_size)
2437 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); 2439 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
2438} 2440}
@@ -3041,22 +3043,11 @@ static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
3041 break; 3043 break;
3042 } 3044 }
3043 3045
3044 /* If fifo is already allocated for this ep */
3045 if (hs_ep->fifo_index) {
3046 size = hs_ep->ep.maxpacket * hs_ep->mc;
3047 /* If bigger fifo is required deallocate current one */
3048 if (size > hs_ep->fifo_size) {
3049 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3050 hs_ep->fifo_index = 0;
3051 hs_ep->fifo_size = 0;
3052 }
3053 }
3054
3055 /* 3046 /*
3056 * if the hardware has dedicated fifos, we must give each IN EP 3047 * if the hardware has dedicated fifos, we must give each IN EP
3057 * a unique tx-fifo even if it is non-periodic. 3048 * a unique tx-fifo even if it is non-periodic.
3058 */ 3049 */
3059 if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) { 3050 if (dir_in && hsotg->dedicated_fifos) {
3060 u32 fifo_index = 0; 3051 u32 fifo_index = 0;
3061 u32 fifo_size = UINT_MAX; 3052 u32 fifo_size = UINT_MAX;
3062 size = hs_ep->ep.maxpacket*hs_ep->mc; 3053 size = hs_ep->ep.maxpacket*hs_ep->mc;
@@ -3129,10 +3120,6 @@ static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
3129 3120
3130 spin_lock_irqsave(&hsotg->lock, flags); 3121 spin_lock_irqsave(&hsotg->lock, flags);
3131 3122
3132 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
3133 hs_ep->fifo_index = 0;
3134 hs_ep->fifo_size = 0;
3135
3136 ctrl = dwc2_readl(hsotg->regs + epctrl_reg); 3123 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
3137 ctrl &= ~DXEPCTL_EPENA; 3124 ctrl &= ~DXEPCTL_EPENA;
3138 ctrl &= ~DXEPCTL_USBACTEP; 3125 ctrl &= ~DXEPCTL_USBACTEP;
@@ -3147,6 +3134,10 @@ static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
3147 /* terminate all requests with shutdown */ 3134 /* terminate all requests with shutdown */
3148 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); 3135 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3149 3136
3137 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3138 hs_ep->fifo_index = 0;
3139 hs_ep->fifo_size = 0;
3140
3150 spin_unlock_irqrestore(&hsotg->lock, flags); 3141 spin_unlock_irqrestore(&hsotg->lock, flags);
3151 return 0; 3142 return 0;
3152} 3143}
@@ -3475,8 +3466,11 @@ static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
3475 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); 3466 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
3476 3467
3477 spin_lock_irqsave(&hsotg->lock, flags); 3468 spin_lock_irqsave(&hsotg->lock, flags);
3478 dwc2_hsotg_init(hsotg); 3469 if (dwc2_hw_is_device(hsotg)) {
3479 dwc2_hsotg_core_init_disconnected(hsotg, false); 3470 dwc2_hsotg_init(hsotg);
3471 dwc2_hsotg_core_init_disconnected(hsotg, false);
3472 }
3473
3480 hsotg->enabled = 0; 3474 hsotg->enabled = 0;
3481 spin_unlock_irqrestore(&hsotg->lock, flags); 3475 spin_unlock_irqrestore(&hsotg->lock, flags);
3482 3476
@@ -3813,36 +3807,10 @@ static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
3813static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) 3807static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
3814{ 3808{
3815 struct device_node *np = hsotg->dev->of_node; 3809 struct device_node *np = hsotg->dev->of_node;
3816 u32 len = 0;
3817 u32 i = 0;
3818 3810
3819 /* Enable dma if requested in device tree */ 3811 /* Enable dma if requested in device tree */
3820 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma"); 3812 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
3821 3813
3822 /*
3823 * Register TX periodic fifo size per endpoint.
3824 * EP0 is excluded since it has no fifo configuration.
3825 */
3826 if (!of_find_property(np, "g-tx-fifo-size", &len))
3827 goto rx_fifo;
3828
3829 len /= sizeof(u32);
3830
3831 /* Read tx fifo sizes other than ep0 */
3832 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3833 &hsotg->g_tx_fifo_sz[1], len))
3834 goto rx_fifo;
3835
3836 /* Add ep0 */
3837 len++;
3838
3839 /* Make remaining TX fifos unavailable */
3840 if (len < MAX_EPS_CHANNELS) {
3841 for (i = len; i < MAX_EPS_CHANNELS; i++)
3842 hsotg->g_tx_fifo_sz[i] = 0;
3843 }
3844
3845rx_fifo:
3846 /* Register RX fifo size */ 3814 /* Register RX fifo size */
3847 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz); 3815 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3848 3816
@@ -3864,13 +3832,10 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
3864 struct device *dev = hsotg->dev; 3832 struct device *dev = hsotg->dev;
3865 int epnum; 3833 int epnum;
3866 int ret; 3834 int ret;
3867 int i;
3868 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
3869 3835
3870 /* Initialize to legacy fifo configuration values */ 3836 /* Initialize to legacy fifo configuration values */
3871 hsotg->g_rx_fifo_sz = 2048; 3837 hsotg->g_rx_fifo_sz = 2048;
3872 hsotg->g_np_g_tx_fifo_sz = 1024; 3838 hsotg->g_np_g_tx_fifo_sz = 1024;
3873 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3874 /* Device tree specific probe */ 3839 /* Device tree specific probe */
3875 dwc2_hsotg_of_probe(hsotg); 3840 dwc2_hsotg_of_probe(hsotg);
3876 3841
@@ -3888,9 +3853,6 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
3888 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", 3853 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3889 hsotg->g_np_g_tx_fifo_sz); 3854 hsotg->g_np_g_tx_fifo_sz);
3890 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz); 3855 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3891 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3892 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3893 hsotg->g_tx_fifo_sz[i]);
3894 3856
3895 hsotg->gadget.max_speed = USB_SPEED_HIGH; 3857 hsotg->gadget.max_speed = USB_SPEED_HIGH;
3896 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; 3858 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
diff --git a/drivers/usb/dwc2/hw.h b/drivers/usb/dwc2/hw.h
index efc3bcde2822..91058441e62a 100644
--- a/drivers/usb/dwc2/hw.h
+++ b/drivers/usb/dwc2/hw.h
@@ -48,6 +48,7 @@
48#define GOTGCTL_ASESVLD (1 << 18) 48#define GOTGCTL_ASESVLD (1 << 18)
49#define GOTGCTL_DBNC_SHORT (1 << 17) 49#define GOTGCTL_DBNC_SHORT (1 << 17)
50#define GOTGCTL_CONID_B (1 << 16) 50#define GOTGCTL_CONID_B (1 << 16)
51#define GOTGCTL_DBNCE_FLTR_BYPASS (1 << 15)
51#define GOTGCTL_DEVHNPEN (1 << 11) 52#define GOTGCTL_DEVHNPEN (1 << 11)
52#define GOTGCTL_HSTSETHNPEN (1 << 10) 53#define GOTGCTL_HSTSETHNPEN (1 << 10)
53#define GOTGCTL_HNPREQ (1 << 9) 54#define GOTGCTL_HNPREQ (1 << 9)
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 35d092456bec..7287a763cd0c 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -49,6 +49,57 @@
49 49
50#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */ 50#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
51 51
52/**
53 * dwc3_get_dr_mode - Validates and sets dr_mode
54 * @dwc: pointer to our context structure
55 */
56static int dwc3_get_dr_mode(struct dwc3 *dwc)
57{
58 enum usb_dr_mode mode;
59 struct device *dev = dwc->dev;
60 unsigned int hw_mode;
61
62 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
63 dwc->dr_mode = USB_DR_MODE_OTG;
64
65 mode = dwc->dr_mode;
66 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
67
68 switch (hw_mode) {
69 case DWC3_GHWPARAMS0_MODE_GADGET:
70 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
71 dev_err(dev,
72 "Controller does not support host mode.\n");
73 return -EINVAL;
74 }
75 mode = USB_DR_MODE_PERIPHERAL;
76 break;
77 case DWC3_GHWPARAMS0_MODE_HOST:
78 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
79 dev_err(dev,
80 "Controller does not support device mode.\n");
81 return -EINVAL;
82 }
83 mode = USB_DR_MODE_HOST;
84 break;
85 default:
86 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
87 mode = USB_DR_MODE_HOST;
88 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
89 mode = USB_DR_MODE_PERIPHERAL;
90 }
91
92 if (mode != dwc->dr_mode) {
93 dev_warn(dev,
94 "Configuration mismatch. dr_mode forced to %s\n",
95 mode == USB_DR_MODE_HOST ? "host" : "gadget");
96
97 dwc->dr_mode = mode;
98 }
99
100 return 0;
101}
102
52void dwc3_set_mode(struct dwc3 *dwc, u32 mode) 103void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
53{ 104{
54 u32 reg; 105 u32 reg;
@@ -448,6 +499,9 @@ static int dwc3_phy_setup(struct dwc3 *dwc)
448 if (dwc->dis_u3_susphy_quirk) 499 if (dwc->dis_u3_susphy_quirk)
449 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; 500 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
450 501
502 if (dwc->dis_del_phy_power_chg_quirk)
503 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
504
451 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 505 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
452 506
453 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 507 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
@@ -485,6 +539,23 @@ static int dwc3_phy_setup(struct dwc3 *dwc)
485 break; 539 break;
486 } 540 }
487 541
542 switch (dwc->hsphy_mode) {
543 case USBPHY_INTERFACE_MODE_UTMI:
544 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
545 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
546 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
547 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
548 break;
549 case USBPHY_INTERFACE_MODE_UTMIW:
550 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
551 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
552 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
553 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
554 break;
555 default:
556 break;
557 }
558
488 /* 559 /*
489 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to 560 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
490 * '0' during coreConsultant configuration. So default value will 561 * '0' during coreConsultant configuration. So default value will
@@ -500,6 +571,9 @@ static int dwc3_phy_setup(struct dwc3 *dwc)
500 if (dwc->dis_enblslpm_quirk) 571 if (dwc->dis_enblslpm_quirk)
501 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 572 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
502 573
574 if (dwc->dis_u2_freeclk_exists_quirk)
575 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
576
503 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 577 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
504 578
505 return 0; 579 return 0;
@@ -666,6 +740,32 @@ static int dwc3_core_init(struct dwc3 *dwc)
666 goto err4; 740 goto err4;
667 } 741 }
668 742
743 switch (dwc->dr_mode) {
744 case USB_DR_MODE_PERIPHERAL:
745 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
746 break;
747 case USB_DR_MODE_HOST:
748 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
749 break;
750 case USB_DR_MODE_OTG:
751 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
752 break;
753 default:
754 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
755 break;
756 }
757
758 /*
759 * ENDXFER polling is available on version 3.10a and later of
760 * the DWC_usb3 controller. It is NOT available in the
761 * DWC_usb31 controller.
762 */
763 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
764 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
765 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
766 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
767 }
768
669 return 0; 769 return 0;
670 770
671err4: 771err4:
@@ -763,7 +863,6 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
763 863
764 switch (dwc->dr_mode) { 864 switch (dwc->dr_mode) {
765 case USB_DR_MODE_PERIPHERAL: 865 case USB_DR_MODE_PERIPHERAL:
766 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
767 ret = dwc3_gadget_init(dwc); 866 ret = dwc3_gadget_init(dwc);
768 if (ret) { 867 if (ret) {
769 if (ret != -EPROBE_DEFER) 868 if (ret != -EPROBE_DEFER)
@@ -772,7 +871,6 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
772 } 871 }
773 break; 872 break;
774 case USB_DR_MODE_HOST: 873 case USB_DR_MODE_HOST:
775 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
776 ret = dwc3_host_init(dwc); 874 ret = dwc3_host_init(dwc);
777 if (ret) { 875 if (ret) {
778 if (ret != -EPROBE_DEFER) 876 if (ret != -EPROBE_DEFER)
@@ -781,7 +879,6 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
781 } 879 }
782 break; 880 break;
783 case USB_DR_MODE_OTG: 881 case USB_DR_MODE_OTG:
784 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
785 ret = dwc3_host_init(dwc); 882 ret = dwc3_host_init(dwc);
786 if (ret) { 883 if (ret) {
787 if (ret != -EPROBE_DEFER) 884 if (ret != -EPROBE_DEFER)
@@ -888,6 +985,7 @@ static int dwc3_probe(struct platform_device *pdev)
888 985
889 dwc->maximum_speed = usb_get_maximum_speed(dev); 986 dwc->maximum_speed = usb_get_maximum_speed(dev);
890 dwc->dr_mode = usb_get_dr_mode(dev); 987 dwc->dr_mode = usb_get_dr_mode(dev);
988 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
891 989
892 dwc->has_lpm_erratum = device_property_read_bool(dev, 990 dwc->has_lpm_erratum = device_property_read_bool(dev,
893 "snps,has-lpm-erratum"); 991 "snps,has-lpm-erratum");
@@ -924,6 +1022,10 @@ static int dwc3_probe(struct platform_device *pdev)
924 "snps,dis_enblslpm_quirk"); 1022 "snps,dis_enblslpm_quirk");
925 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev, 1023 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
926 "snps,dis_rxdet_inp3_quirk"); 1024 "snps,dis_rxdet_inp3_quirk");
1025 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1026 "snps,dis-u2-freeclk-exists-quirk");
1027 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1028 "snps,dis-del-phy-power-chg-quirk");
927 1029
928 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev, 1030 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
929 "snps,tx_de_emphasis_quirk"); 1031 "snps,tx_de_emphasis_quirk");
@@ -972,17 +1074,9 @@ static int dwc3_probe(struct platform_device *pdev)
972 goto err2; 1074 goto err2;
973 } 1075 }
974 1076
975 if (IS_ENABLED(CONFIG_USB_DWC3_HOST) && 1077 ret = dwc3_get_dr_mode(dwc);
976 (dwc->dr_mode == USB_DR_MODE_OTG || 1078 if (ret)
977 dwc->dr_mode == USB_DR_MODE_UNKNOWN)) 1079 goto err3;
978 dwc->dr_mode = USB_DR_MODE_HOST;
979 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
980 (dwc->dr_mode == USB_DR_MODE_OTG ||
981 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
982 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
983
984 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
985 dwc->dr_mode = USB_DR_MODE_OTG;
986 1080
987 ret = dwc3_alloc_scratch_buffers(dwc); 1081 ret = dwc3_alloc_scratch_buffers(dwc);
988 if (ret) 1082 if (ret)
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index 45d6de5107c7..6b60e42626a2 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -109,6 +109,7 @@
109#define DWC3_GPRTBIMAP_HS1 0xc184 109#define DWC3_GPRTBIMAP_HS1 0xc184
110#define DWC3_GPRTBIMAP_FS0 0xc188 110#define DWC3_GPRTBIMAP_FS0 0xc188
111#define DWC3_GPRTBIMAP_FS1 0xc18c 111#define DWC3_GPRTBIMAP_FS1 0xc18c
112#define DWC3_GUCTL2 0xc19c
112 113
113#define DWC3_VER_NUMBER 0xc1a0 114#define DWC3_VER_NUMBER 0xc1a0
114#define DWC3_VER_TYPE 0xc1a4 115#define DWC3_VER_TYPE 0xc1a4
@@ -199,9 +200,18 @@
199 200
200/* Global USB2 PHY Configuration Register */ 201/* Global USB2 PHY Configuration Register */
201#define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31) 202#define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31)
203#define DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS (1 << 30)
202#define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6) 204#define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6)
203#define DWC3_GUSB2PHYCFG_ULPI_UTMI (1 << 4) 205#define DWC3_GUSB2PHYCFG_ULPI_UTMI (1 << 4)
204#define DWC3_GUSB2PHYCFG_ENBLSLPM (1 << 8) 206#define DWC3_GUSB2PHYCFG_ENBLSLPM (1 << 8)
207#define DWC3_GUSB2PHYCFG_PHYIF(n) (n << 3)
208#define DWC3_GUSB2PHYCFG_PHYIF_MASK DWC3_GUSB2PHYCFG_PHYIF(1)
209#define DWC3_GUSB2PHYCFG_USBTRDTIM(n) (n << 10)
210#define DWC3_GUSB2PHYCFG_USBTRDTIM_MASK DWC3_GUSB2PHYCFG_USBTRDTIM(0xf)
211#define USBTRDTIM_UTMI_8_BIT 9
212#define USBTRDTIM_UTMI_16_BIT 5
213#define UTMI_PHYIF_16_BIT 1
214#define UTMI_PHYIF_8_BIT 0
205 215
206/* Global USB2 PHY Vendor Control Register */ 216/* Global USB2 PHY Vendor Control Register */
207#define DWC3_GUSB2PHYACC_NEWREGREQ (1 << 25) 217#define DWC3_GUSB2PHYACC_NEWREGREQ (1 << 25)
@@ -235,7 +245,10 @@
235#define DWC3_GEVNTSIZ_SIZE(n) ((n) & 0xffff) 245#define DWC3_GEVNTSIZ_SIZE(n) ((n) & 0xffff)
236 246
237/* Global HWPARAMS0 Register */ 247/* Global HWPARAMS0 Register */
238#define DWC3_GHWPARAMS0_USB3_MODE(n) ((n) & 0x3) 248#define DWC3_GHWPARAMS0_MODE(n) ((n) & 0x3)
249#define DWC3_GHWPARAMS0_MODE_GADGET 0
250#define DWC3_GHWPARAMS0_MODE_HOST 1
251#define DWC3_GHWPARAMS0_MODE_DRD 2
239#define DWC3_GHWPARAMS0_MBUS_TYPE(n) (((n) >> 3) & 0x7) 252#define DWC3_GHWPARAMS0_MBUS_TYPE(n) (((n) >> 3) & 0x7)
240#define DWC3_GHWPARAMS0_SBUS_TYPE(n) (((n) >> 6) & 0x3) 253#define DWC3_GHWPARAMS0_SBUS_TYPE(n) (((n) >> 6) & 0x3)
241#define DWC3_GHWPARAMS0_MDWIDTH(n) (((n) >> 8) & 0xff) 254#define DWC3_GHWPARAMS0_MDWIDTH(n) (((n) >> 8) & 0xff)
@@ -279,6 +292,9 @@
279#define DWC3_GFLADJ_30MHZ_SDBND_SEL (1 << 7) 292#define DWC3_GFLADJ_30MHZ_SDBND_SEL (1 << 7)
280#define DWC3_GFLADJ_30MHZ_MASK 0x3f 293#define DWC3_GFLADJ_30MHZ_MASK 0x3f
281 294
295/* Global User Control Register 2 */
296#define DWC3_GUCTL2_RST_ACTBITLATER (1 << 14)
297
282/* Device Configuration Register */ 298/* Device Configuration Register */
283#define DWC3_DCFG_DEVADDR(addr) ((addr) << 3) 299#define DWC3_DCFG_DEVADDR(addr) ((addr) << 3)
284#define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f) 300#define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f)
@@ -685,6 +701,8 @@ struct dwc3_hwparams {
685 * @request: struct usb_request to be transferred 701 * @request: struct usb_request to be transferred
686 * @list: a list_head used for request queueing 702 * @list: a list_head used for request queueing
687 * @dep: struct dwc3_ep owning this request 703 * @dep: struct dwc3_ep owning this request
704 * @sg: pointer to first incomplete sg
705 * @num_pending_sgs: counter to pending sgs
688 * @first_trb_index: index to first trb used by this request 706 * @first_trb_index: index to first trb used by this request
689 * @epnum: endpoint number to which this request refers 707 * @epnum: endpoint number to which this request refers
690 * @trb: pointer to struct dwc3_trb 708 * @trb: pointer to struct dwc3_trb
@@ -697,7 +715,9 @@ struct dwc3_request {
697 struct usb_request request; 715 struct usb_request request;
698 struct list_head list; 716 struct list_head list;
699 struct dwc3_ep *dep; 717 struct dwc3_ep *dep;
718 struct scatterlist *sg;
700 719
720 unsigned num_pending_sgs;
701 u8 first_trb_index; 721 u8 first_trb_index;
702 u8 epnum; 722 u8 epnum;
703 struct dwc3_trb *trb; 723 struct dwc3_trb *trb;
@@ -743,6 +763,9 @@ struct dwc3_scratchpad_array {
743 * @maximum_speed: maximum speed requested (mainly for testing purposes) 763 * @maximum_speed: maximum speed requested (mainly for testing purposes)
744 * @revision: revision register contents 764 * @revision: revision register contents
745 * @dr_mode: requested mode of operation 765 * @dr_mode: requested mode of operation
766 * @hsphy_mode: UTMI phy mode, one of following:
767 * - USBPHY_INTERFACE_MODE_UTMI
768 * - USBPHY_INTERFACE_MODE_UTMIW
746 * @usb2_phy: pointer to USB2 PHY 769 * @usb2_phy: pointer to USB2 PHY
747 * @usb3_phy: pointer to USB3 PHY 770 * @usb3_phy: pointer to USB3 PHY
748 * @usb2_generic_phy: pointer to USB2 PHY 771 * @usb2_generic_phy: pointer to USB2 PHY
@@ -799,6 +822,11 @@ struct dwc3_scratchpad_array {
799 * @dis_u2_susphy_quirk: set if we disable usb2 suspend phy 822 * @dis_u2_susphy_quirk: set if we disable usb2 suspend phy
800 * @dis_enblslpm_quirk: set if we clear enblslpm in GUSB2PHYCFG, 823 * @dis_enblslpm_quirk: set if we clear enblslpm in GUSB2PHYCFG,
801 * disabling the suspend signal to the PHY. 824 * disabling the suspend signal to the PHY.
825 * @dis_u2_freeclk_exists_quirk : set if we clear u2_freeclk_exists
826 * in GUSB2PHYCFG, specify that USB2 PHY doesn't
827 * provide a free-running PHY clock.
828 * @dis_del_phy_power_chg_quirk: set if we disable delay phy power
829 * change quirk.
802 * @tx_de_emphasis_quirk: set if we enable Tx de-emphasis quirk 830 * @tx_de_emphasis_quirk: set if we enable Tx de-emphasis quirk
803 * @tx_de_emphasis: Tx de-emphasis value 831 * @tx_de_emphasis: Tx de-emphasis value
804 * 0 - -6dB de-emphasis 832 * 0 - -6dB de-emphasis
@@ -845,6 +873,7 @@ struct dwc3 {
845 size_t regs_size; 873 size_t regs_size;
846 874
847 enum usb_dr_mode dr_mode; 875 enum usb_dr_mode dr_mode;
876 enum usb_phy_interface hsphy_mode;
848 877
849 u32 fladj; 878 u32 fladj;
850 u32 irq_gadget; 879 u32 irq_gadget;
@@ -880,6 +909,8 @@ struct dwc3 {
880#define DWC3_REVISION_260A 0x5533260a 909#define DWC3_REVISION_260A 0x5533260a
881#define DWC3_REVISION_270A 0x5533270a 910#define DWC3_REVISION_270A 0x5533270a
882#define DWC3_REVISION_280A 0x5533280a 911#define DWC3_REVISION_280A 0x5533280a
912#define DWC3_REVISION_300A 0x5533300a
913#define DWC3_REVISION_310A 0x5533310a
883 914
884/* 915/*
885 * NOTICE: we're using bit 31 as a "is usb 3.1" flag. This is really 916 * NOTICE: we're using bit 31 as a "is usb 3.1" flag. This is really
@@ -942,6 +973,8 @@ struct dwc3 {
942 unsigned dis_u2_susphy_quirk:1; 973 unsigned dis_u2_susphy_quirk:1;
943 unsigned dis_enblslpm_quirk:1; 974 unsigned dis_enblslpm_quirk:1;
944 unsigned dis_rxdet_inp3_quirk:1; 975 unsigned dis_rxdet_inp3_quirk:1;
976 unsigned dis_u2_freeclk_exists_quirk:1;
977 unsigned dis_del_phy_power_chg_quirk:1;
945 978
946 unsigned tx_de_emphasis_quirk:1; 979 unsigned tx_de_emphasis_quirk:1;
947 unsigned tx_de_emphasis:2; 980 unsigned tx_de_emphasis:2;
diff --git a/drivers/usb/dwc3/dwc3-of-simple.c b/drivers/usb/dwc3/dwc3-of-simple.c
index e56d59b19a0e..fe414e7a9c78 100644
--- a/drivers/usb/dwc3/dwc3-of-simple.c
+++ b/drivers/usb/dwc3/dwc3-of-simple.c
@@ -36,36 +36,25 @@ struct dwc3_of_simple {
36 int num_clocks; 36 int num_clocks;
37}; 37};
38 38
39static int dwc3_of_simple_probe(struct platform_device *pdev) 39static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
40{ 40{
41 struct dwc3_of_simple *simple; 41 struct device *dev = simple->dev;
42 struct device *dev = &pdev->dev;
43 struct device_node *np = dev->of_node; 42 struct device_node *np = dev->of_node;
44
45 unsigned int count;
46 int ret;
47 int i; 43 int i;
48 44
49 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL); 45 simple->num_clocks = count;
50 if (!simple)
51 return -ENOMEM;
52 46
53 count = of_clk_get_parent_count(np);
54 if (!count) 47 if (!count)
55 return -ENOENT; 48 return 0;
56
57 simple->num_clocks = count;
58 49
59 simple->clks = devm_kcalloc(dev, simple->num_clocks, 50 simple->clks = devm_kcalloc(dev, simple->num_clocks,
60 sizeof(struct clk *), GFP_KERNEL); 51 sizeof(struct clk *), GFP_KERNEL);
61 if (!simple->clks) 52 if (!simple->clks)
62 return -ENOMEM; 53 return -ENOMEM;
63 54
64 platform_set_drvdata(pdev, simple);
65 simple->dev = dev;
66
67 for (i = 0; i < simple->num_clocks; i++) { 55 for (i = 0; i < simple->num_clocks; i++) {
68 struct clk *clk; 56 struct clk *clk;
57 int ret;
69 58
70 clk = of_clk_get(np, i); 59 clk = of_clk_get(np, i);
71 if (IS_ERR(clk)) { 60 if (IS_ERR(clk)) {
@@ -88,6 +77,29 @@ static int dwc3_of_simple_probe(struct platform_device *pdev)
88 simple->clks[i] = clk; 77 simple->clks[i] = clk;
89 } 78 }
90 79
80 return 0;
81}
82
83static int dwc3_of_simple_probe(struct platform_device *pdev)
84{
85 struct dwc3_of_simple *simple;
86 struct device *dev = &pdev->dev;
87 struct device_node *np = dev->of_node;
88
89 int ret;
90 int i;
91
92 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
93 if (!simple)
94 return -ENOMEM;
95
96 platform_set_drvdata(pdev, simple);
97 simple->dev = dev;
98
99 ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
100 if (ret)
101 return ret;
102
91 ret = of_platform_populate(np, NULL, NULL, dev); 103 ret = of_platform_populate(np, NULL, NULL, dev);
92 if (ret) { 104 if (ret) {
93 for (i = 0; i < simple->num_clocks; i++) { 105 for (i = 0; i < simple->num_clocks; i++) {
@@ -112,7 +124,7 @@ static int dwc3_of_simple_remove(struct platform_device *pdev)
112 int i; 124 int i;
113 125
114 for (i = 0; i < simple->num_clocks; i++) { 126 for (i = 0; i < simple->num_clocks; i++) {
115 clk_unprepare(simple->clks[i]); 127 clk_disable_unprepare(simple->clks[i]);
116 clk_put(simple->clks[i]); 128 clk_put(simple->clks[i]);
117 } 129 }
118 130
@@ -162,7 +174,9 @@ static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
162 174
163static const struct of_device_id of_dwc3_simple_match[] = { 175static const struct of_device_id of_dwc3_simple_match[] = {
164 { .compatible = "qcom,dwc3" }, 176 { .compatible = "qcom,dwc3" },
177 { .compatible = "rockchip,rk3399-dwc3" },
165 { .compatible = "xlnx,zynqmp-dwc3" }, 178 { .compatible = "xlnx,zynqmp-dwc3" },
179 { .compatible = "cavium,octeon-7130-usb-uctl" },
166 { /* Sentinel */ } 180 { /* Sentinel */ }
167}; 181};
168MODULE_DEVICE_TABLE(of, of_dwc3_simple_match); 182MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 122e64df2f4d..07cc8929f271 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -174,15 +174,8 @@ void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
174 int status) 174 int status)
175{ 175{
176 struct dwc3 *dwc = dep->dwc; 176 struct dwc3 *dwc = dep->dwc;
177 int i;
178 177
179 if (req->started) { 178 req->started = false;
180 i = 0;
181 do {
182 dwc3_ep_inc_deq(dep);
183 } while(++i < req->request.num_mapped_sgs);
184 req->started = false;
185 }
186 list_del(&req->list); 179 list_del(&req->list);
187 req->trb = NULL; 180 req->trb = NULL;
188 181
@@ -348,7 +341,8 @@ static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
348 * IN transfers due to a mishandled error condition. Synopsys 341 * IN transfers due to a mishandled error condition. Synopsys
349 * STAR 9000614252. 342 * STAR 9000614252.
350 */ 343 */
351 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A)) 344 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
345 (dwc->gadget.speed >= USB_SPEED_SUPER))
352 cmd |= DWC3_DEPCMD_CLEARPENDIN; 346 cmd |= DWC3_DEPCMD_CLEARPENDIN;
353 347
354 memset(&params, 0, sizeof(params)); 348 memset(&params, 0, sizeof(params));
@@ -490,7 +484,8 @@ static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
490 params.param0 |= DWC3_DEPCFG_ACTION_INIT; 484 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
491 } 485 }
492 486
493 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; 487 if (usb_endpoint_xfer_control(desc))
488 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
494 489
495 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) 490 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
496 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; 491 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
@@ -764,6 +759,8 @@ static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
764 kfree(req); 759 kfree(req);
765} 760}
766 761
762static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
763
767/** 764/**
768 * dwc3_prepare_one_trb - setup one TRB from one request 765 * dwc3_prepare_one_trb - setup one TRB from one request
769 * @dep: endpoint for which this request is prepared 766 * @dep: endpoint for which this request is prepared
@@ -771,15 +768,13 @@ static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
771 */ 768 */
772static void dwc3_prepare_one_trb(struct dwc3_ep *dep, 769static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
773 struct dwc3_request *req, dma_addr_t dma, 770 struct dwc3_request *req, dma_addr_t dma,
774 unsigned length, unsigned last, unsigned chain, unsigned node) 771 unsigned length, unsigned chain, unsigned node)
775{ 772{
776 struct dwc3_trb *trb; 773 struct dwc3_trb *trb;
777 774
778 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s", 775 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
779 dep->name, req, (unsigned long long) dma, 776 dep->name, req, (unsigned long long) dma,
780 length, last ? " last" : "", 777 length, chain ? " chain" : "");
781 chain ? " chain" : "");
782
783 778
784 trb = &dep->trb_pool[dep->trb_enqueue]; 779 trb = &dep->trb_pool[dep->trb_enqueue];
785 780
@@ -826,12 +821,10 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
826 /* always enable Continue on Short Packet */ 821 /* always enable Continue on Short Packet */
827 trb->ctrl |= DWC3_TRB_CTRL_CSP; 822 trb->ctrl |= DWC3_TRB_CTRL_CSP;
828 823
829 if (!req->request.no_interrupt && !chain) 824 if ((!req->request.no_interrupt && !chain) ||
825 (dwc3_calc_trbs_left(dep) == 0))
830 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI; 826 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
831 827
832 if (last && !usb_endpoint_xfer_isoc(dep->endpoint.desc))
833 trb->ctrl |= DWC3_TRB_CTRL_LST;
834
835 if (chain) 828 if (chain)
836 trb->ctrl |= DWC3_TRB_CTRL_CHN; 829 trb->ctrl |= DWC3_TRB_CTRL_CHN;
837 830
@@ -856,12 +849,12 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
856 */ 849 */
857static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) 850static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
858{ 851{
859 if (!index) 852 u8 tmp = index;
860 index = DWC3_TRB_NUM - 2;
861 else
862 index = dep->trb_enqueue - 1;
863 853
864 return &dep->trb_pool[index]; 854 if (!tmp)
855 tmp = DWC3_TRB_NUM - 1;
856
857 return &dep->trb_pool[tmp - 1];
865} 858}
866 859
867static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 860static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
@@ -894,65 +887,42 @@ static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
894} 887}
895 888
896static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, 889static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
897 struct dwc3_request *req, unsigned int trbs_left, 890 struct dwc3_request *req)
898 unsigned int more_coming)
899{ 891{
900 struct usb_request *request = &req->request; 892 struct scatterlist *sg = req->sg;
901 struct scatterlist *sg = request->sg;
902 struct scatterlist *s; 893 struct scatterlist *s;
903 unsigned int last = false;
904 unsigned int length; 894 unsigned int length;
905 dma_addr_t dma; 895 dma_addr_t dma;
906 int i; 896 int i;
907 897
908 for_each_sg(sg, s, request->num_mapped_sgs, i) { 898 for_each_sg(sg, s, req->num_pending_sgs, i) {
909 unsigned chain = true; 899 unsigned chain = true;
910 900
911 length = sg_dma_len(s); 901 length = sg_dma_len(s);
912 dma = sg_dma_address(s); 902 dma = sg_dma_address(s);
913 903
914 if (sg_is_last(s)) { 904 if (sg_is_last(s))
915 if (usb_endpoint_xfer_int(dep->endpoint.desc) ||
916 !more_coming)
917 last = true;
918
919 chain = false;
920 }
921
922 if (!trbs_left--)
923 last = true;
924
925 if (last)
926 chain = false; 905 chain = false;
927 906
928 dwc3_prepare_one_trb(dep, req, dma, length, 907 dwc3_prepare_one_trb(dep, req, dma, length,
929 last, chain, i); 908 chain, i);
930 909
931 if (last) 910 if (!dwc3_calc_trbs_left(dep))
932 break; 911 break;
933 } 912 }
934} 913}
935 914
936static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, 915static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
937 struct dwc3_request *req, unsigned int trbs_left, 916 struct dwc3_request *req)
938 unsigned int more_coming)
939{ 917{
940 unsigned int last = false;
941 unsigned int length; 918 unsigned int length;
942 dma_addr_t dma; 919 dma_addr_t dma;
943 920
944 dma = req->request.dma; 921 dma = req->request.dma;
945 length = req->request.length; 922 length = req->request.length;
946 923
947 if (!trbs_left)
948 last = true;
949
950 /* Is this the last request? */
951 if (usb_endpoint_xfer_int(dep->endpoint.desc) || !more_coming)
952 last = true;
953
954 dwc3_prepare_one_trb(dep, req, dma, length, 924 dwc3_prepare_one_trb(dep, req, dma, length,
955 last, false, 0); 925 false, 0);
956} 926}
957 927
958/* 928/*
@@ -966,26 +936,19 @@ static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
966static void dwc3_prepare_trbs(struct dwc3_ep *dep) 936static void dwc3_prepare_trbs(struct dwc3_ep *dep)
967{ 937{
968 struct dwc3_request *req, *n; 938 struct dwc3_request *req, *n;
969 unsigned int more_coming;
970 u32 trbs_left;
971 939
972 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); 940 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
973 941
974 trbs_left = dwc3_calc_trbs_left(dep); 942 if (!dwc3_calc_trbs_left(dep))
975 if (!trbs_left)
976 return; 943 return;
977 944
978 more_coming = dep->allocated_requests - dep->queued_requests;
979
980 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 945 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
981 if (req->request.num_mapped_sgs > 0) 946 if (req->num_pending_sgs > 0)
982 dwc3_prepare_one_trb_sg(dep, req, trbs_left--, 947 dwc3_prepare_one_trb_sg(dep, req);
983 more_coming);
984 else 948 else
985 dwc3_prepare_one_trb_linear(dep, req, trbs_left--, 949 dwc3_prepare_one_trb_linear(dep, req);
986 more_coming);
987 950
988 if (!trbs_left) 951 if (!dwc3_calc_trbs_left(dep))
989 return; 952 return;
990 } 953 }
991} 954}
@@ -1101,93 +1064,29 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1101 1064
1102 trace_dwc3_ep_queue(req); 1065 trace_dwc3_ep_queue(req);
1103 1066
1104 /*
1105 * We only add to our list of requests now and
1106 * start consuming the list once we get XferNotReady
1107 * IRQ.
1108 *
1109 * That way, we avoid doing anything that we don't need
1110 * to do now and defer it until the point we receive a
1111 * particular token from the Host side.
1112 *
1113 * This will also avoid Host cancelling URBs due to too
1114 * many NAKs.
1115 */
1116 ret = usb_gadget_map_request(&dwc->gadget, &req->request, 1067 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1117 dep->direction); 1068 dep->direction);
1118 if (ret) 1069 if (ret)
1119 return ret; 1070 return ret;
1120 1071
1121 list_add_tail(&req->list, &dep->pending_list); 1072 req->sg = req->request.sg;
1122 1073 req->num_pending_sgs = req->request.num_mapped_sgs;
1123 /*
1124 * If there are no pending requests and the endpoint isn't already
1125 * busy, we will just start the request straight away.
1126 *
1127 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1128 * little bit faster.
1129 */
1130 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1131 !usb_endpoint_xfer_int(dep->endpoint.desc)) {
1132 ret = __dwc3_gadget_kick_transfer(dep, 0);
1133 goto out;
1134 }
1135
1136 /*
1137 * There are a few special cases:
1138 *
1139 * 1. XferNotReady with empty list of requests. We need to kick the
1140 * transfer here in that situation, otherwise we will be NAKing
1141 * forever. If we get XferNotReady before gadget driver has a
1142 * chance to queue a request, we will ACK the IRQ but won't be
1143 * able to receive the data until the next request is queued.
1144 * The following code is handling exactly that.
1145 *
1146 */
1147 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1148 /*
1149 * If xfernotready is already elapsed and it is a case
1150 * of isoc transfer, then issue END TRANSFER, so that
1151 * you can receive xfernotready again and can have
1152 * notion of current microframe.
1153 */
1154 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1155 if (list_empty(&dep->started_list)) {
1156 dwc3_stop_active_transfer(dwc, dep->number, true);
1157 dep->flags = DWC3_EP_ENABLED;
1158 }
1159 return 0;
1160 }
1161
1162 ret = __dwc3_gadget_kick_transfer(dep, 0);
1163 if (!ret)
1164 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1165 1074
1166 goto out; 1075 list_add_tail(&req->list, &dep->pending_list);
1167 }
1168 1076
1169 /*
1170 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1171 * kick the transfer here after queuing a request, otherwise the
1172 * core may not see the modified TRB(s).
1173 */
1174 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 1077 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1175 (dep->flags & DWC3_EP_BUSY) && 1078 dep->flags & DWC3_EP_PENDING_REQUEST) {
1176 !(dep->flags & DWC3_EP_MISSED_ISOC)) { 1079 if (list_empty(&dep->started_list)) {
1177 WARN_ON_ONCE(!dep->resource_index); 1080 dwc3_stop_active_transfer(dwc, dep->number, true);
1178 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index); 1081 dep->flags = DWC3_EP_ENABLED;
1179 goto out; 1082 }
1083 return 0;
1180 } 1084 }
1181 1085
1182 /* 1086 if (!dwc3_calc_trbs_left(dep))
1183 * 4. Stream Capable Bulk Endpoints. We need to start the transfer 1087 return 0;
1184 * right away, otherwise host will not know we have streams to be
1185 * handled.
1186 */
1187 if (dep->stream_capable)
1188 ret = __dwc3_gadget_kick_transfer(dep, 0);
1189 1088
1190out: 1089 ret = __dwc3_gadget_kick_transfer(dep, 0);
1191 if (ret && ret != -EBUSY) 1090 if (ret && ret != -EBUSY)
1192 dwc3_trace(trace_dwc3_gadget, 1091 dwc3_trace(trace_dwc3_gadget,
1193 "%s: failed to kick transfers", 1092 "%s: failed to kick transfers",
@@ -1963,6 +1862,7 @@ static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1963 unsigned int trb_status; 1862 unsigned int trb_status;
1964 1863
1965 dep->queued_requests--; 1864 dep->queued_requests--;
1865 dwc3_ep_inc_deq(dep);
1966 trace_dwc3_complete_trb(dep, trb); 1866 trace_dwc3_complete_trb(dep, trb);
1967 1867
1968 /* 1868 /*
@@ -1982,6 +1882,7 @@ static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1982 return 1; 1882 return 1;
1983 1883
1984 count = trb->size & DWC3_TRB_SIZE_MASK; 1884 count = trb->size & DWC3_TRB_SIZE_MASK;
1885 req->request.actual += count;
1985 1886
1986 if (dep->direction) { 1887 if (dep->direction) {
1987 if (count) { 1888 if (count) {
@@ -2021,48 +1922,51 @@ static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2021 1922
2022 if (s_pkt && !chain) 1923 if (s_pkt && !chain)
2023 return 1; 1924 return 1;
2024 if ((event->status & DEPEVT_STATUS_LST) && 1925
2025 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2026 DWC3_TRB_CTRL_HWO)))
2027 return 1;
2028 if ((event->status & DEPEVT_STATUS_IOC) && 1926 if ((event->status & DEPEVT_STATUS_IOC) &&
2029 (trb->ctrl & DWC3_TRB_CTRL_IOC)) 1927 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2030 return 1; 1928 return 1;
1929
2031 return 0; 1930 return 0;
2032} 1931}
2033 1932
2034static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, 1933static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2035 const struct dwc3_event_depevt *event, int status) 1934 const struct dwc3_event_depevt *event, int status)
2036{ 1935{
2037 struct dwc3_request *req; 1936 struct dwc3_request *req, *n;
2038 struct dwc3_trb *trb; 1937 struct dwc3_trb *trb;
2039 unsigned int slot; 1938 bool ioc = false;
2040 unsigned int i;
2041 int count = 0;
2042 int ret; 1939 int ret;
2043 1940
2044 do { 1941 list_for_each_entry_safe(req, n, &dep->started_list, list) {
1942 unsigned length;
1943 unsigned actual;
2045 int chain; 1944 int chain;
2046 1945
2047 req = next_request(&dep->started_list); 1946 length = req->request.length;
2048 if (WARN_ON_ONCE(!req)) 1947 chain = req->num_pending_sgs > 0;
2049 return 1; 1948 if (chain) {
2050 1949 struct scatterlist *sg = req->sg;
2051 chain = req->request.num_mapped_sgs > 0; 1950 struct scatterlist *s;
2052 i = 0; 1951 unsigned int pending = req->num_pending_sgs;
2053 do { 1952 unsigned int i;
2054 slot = req->first_trb_index + i;
2055 if (slot == DWC3_TRB_NUM - 1)
2056 slot++;
2057 slot %= DWC3_TRB_NUM;
2058 trb = &dep->trb_pool[slot];
2059 count += trb->size & DWC3_TRB_SIZE_MASK;
2060 1953
1954 for_each_sg(sg, s, pending, i) {
1955 trb = &dep->trb_pool[dep->trb_dequeue];
1956
1957 req->sg = sg_next(s);
1958 req->num_pending_sgs--;
1959
1960 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1961 event, status, chain);
1962 if (ret)
1963 break;
1964 }
1965 } else {
1966 trb = &dep->trb_pool[dep->trb_dequeue];
2061 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb, 1967 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2062 event, status, chain); 1968 event, status, chain);
2063 if (ret) 1969 }
2064 break;
2065 } while (++i < req->request.num_mapped_sgs);
2066 1970
2067 /* 1971 /*
2068 * We assume here we will always receive the entire data block 1972 * We assume here we will always receive the entire data block
@@ -2071,12 +1975,21 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2071 * should receive and we simply bounce the request back to the 1975 * should receive and we simply bounce the request back to the
2072 * gadget driver for further processing. 1976 * gadget driver for further processing.
2073 */ 1977 */
2074 req->request.actual += req->request.length - count; 1978 actual = length - req->request.actual;
1979 req->request.actual = actual;
1980
1981 if (ret && chain && (actual < length) && req->num_pending_sgs)
1982 return __dwc3_gadget_kick_transfer(dep, 0);
1983
2075 dwc3_gadget_giveback(dep, req, status); 1984 dwc3_gadget_giveback(dep, req, status);
2076 1985
2077 if (ret) 1986 if (ret) {
1987 if ((event->status & DEPEVT_STATUS_IOC) &&
1988 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1989 ioc = true;
2078 break; 1990 break;
2079 } while (1); 1991 }
1992 }
2080 1993
2081 /* 1994 /*
2082 * Our endpoint might get disabled by another thread during 1995 * Our endpoint might get disabled by another thread during
@@ -2103,10 +2016,9 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2103 return 1; 2016 return 1;
2104 } 2017 }
2105 2018
2106 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 2019 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2107 if ((event->status & DEPEVT_STATUS_IOC) && 2020 return 0;
2108 (trb->ctrl & DWC3_TRB_CTRL_IOC)) 2021
2109 return 0;
2110 return 1; 2022 return 1;
2111} 2023}
2112 2024
@@ -2322,6 +2234,18 @@ static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2322 * 2234 *
2323 * - Issue EndTransfer WITH CMDIOC bit set 2235 * - Issue EndTransfer WITH CMDIOC bit set
2324 * - Wait 100us 2236 * - Wait 100us
2237 *
2238 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2239 * supports a mode to work around the above limitation. The
2240 * software can poll the CMDACT bit in the DEPCMD register
2241 * after issuing a EndTransfer command. This mode is enabled
2242 * by writing GUCTL2[14]. This polling is already done in the
2243 * dwc3_send_gadget_ep_cmd() function so if the mode is
2244 * enabled, the EndTransfer command will have completed upon
2245 * returning from this function and we don't need to delay for
2246 * 100us.
2247 *
2248 * This mode is NOT available on the DWC_usb31 IP.
2325 */ 2249 */
2326 2250
2327 cmd = DWC3_DEPCMD_ENDTRANSFER; 2251 cmd = DWC3_DEPCMD_ENDTRANSFER;
@@ -2333,7 +2257,9 @@ static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2333 WARN_ON_ONCE(ret); 2257 WARN_ON_ONCE(ret);
2334 dep->resource_index = 0; 2258 dep->resource_index = 0;
2335 dep->flags &= ~DWC3_EP_BUSY; 2259 dep->flags &= ~DWC3_EP_BUSY;
2336 udelay(100); 2260
2261 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2262 udelay(100);
2337} 2263}
2338 2264
2339static void dwc3_stop_active_transfers(struct dwc3 *dwc) 2265static void dwc3_stop_active_transfers(struct dwc3 *dwc)
diff --git a/drivers/usb/dwc3/ulpi.c b/drivers/usb/dwc3/ulpi.c
index ec004c6d76f2..bd86f84f3790 100644
--- a/drivers/usb/dwc3/ulpi.c
+++ b/drivers/usb/dwc3/ulpi.c
@@ -35,9 +35,9 @@ static int dwc3_ulpi_busyloop(struct dwc3 *dwc)
35 return -ETIMEDOUT; 35 return -ETIMEDOUT;
36} 36}
37 37
38static int dwc3_ulpi_read(struct ulpi_ops *ops, u8 addr) 38static int dwc3_ulpi_read(struct device *dev, u8 addr)
39{ 39{
40 struct dwc3 *dwc = dev_get_drvdata(ops->dev); 40 struct dwc3 *dwc = dev_get_drvdata(dev);
41 u32 reg; 41 u32 reg;
42 int ret; 42 int ret;
43 43
@@ -53,9 +53,9 @@ static int dwc3_ulpi_read(struct ulpi_ops *ops, u8 addr)
53 return DWC3_GUSB2PHYACC_DATA(reg); 53 return DWC3_GUSB2PHYACC_DATA(reg);
54} 54}
55 55
56static int dwc3_ulpi_write(struct ulpi_ops *ops, u8 addr, u8 val) 56static int dwc3_ulpi_write(struct device *dev, u8 addr, u8 val)
57{ 57{
58 struct dwc3 *dwc = dev_get_drvdata(ops->dev); 58 struct dwc3 *dwc = dev_get_drvdata(dev);
59 u32 reg; 59 u32 reg;
60 60
61 reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_ULPI_ADDR(addr); 61 reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_ULPI_ADDR(addr);
@@ -65,7 +65,7 @@ static int dwc3_ulpi_write(struct ulpi_ops *ops, u8 addr, u8 val)
65 return dwc3_ulpi_busyloop(dwc); 65 return dwc3_ulpi_busyloop(dwc);
66} 66}
67 67
68static struct ulpi_ops dwc3_ulpi_ops = { 68static const struct ulpi_ops dwc3_ulpi_ops = {
69 .read = dwc3_ulpi_read, 69 .read = dwc3_ulpi_read,
70 .write = dwc3_ulpi_write, 70 .write = dwc3_ulpi_write,
71}; 71};
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 3c3f31ceece7..8ad203296079 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -209,25 +209,6 @@ config USB_F_PRINTER
209config USB_F_TCM 209config USB_F_TCM
210 tristate 210 tristate
211 211
212choice
213 tristate "USB Gadget Drivers"
214 default USB_ETH
215 help
216 A Linux "Gadget Driver" talks to the USB Peripheral Controller
217 driver through the abstract "gadget" API. Some other operating
218 systems call these "client" drivers, of which "class drivers"
219 are a subset (implementing a USB device class specification).
220 A gadget driver implements one or more USB functions using
221 the peripheral hardware.
222
223 Gadget drivers are hardware-neutral, or "platform independent",
224 except that they sometimes must understand quirks or limitations
225 of the particular controllers they work with. For example, when
226 a controller doesn't support alternate configurations or provide
227 enough of the right types of endpoints, the gadget driver might
228 not be able work with that controller, or might need to implement
229 a less common variant of a device class protocol.
230
231# this first set of drivers all depend on bulk-capable hardware. 212# this first set of drivers all depend on bulk-capable hardware.
232 213
233config USB_CONFIGFS 214config USB_CONFIGFS
@@ -439,6 +420,7 @@ config USB_CONFIGFS_F_HID
439config USB_CONFIGFS_F_UVC 420config USB_CONFIGFS_F_UVC
440 bool "USB Webcam function" 421 bool "USB Webcam function"
441 depends on USB_CONFIGFS 422 depends on USB_CONFIGFS
423 depends on VIDEO_V4L2
442 depends on VIDEO_DEV 424 depends on VIDEO_DEV
443 select VIDEOBUF2_VMALLOC 425 select VIDEOBUF2_VMALLOC
444 select USB_F_UVC 426 select USB_F_UVC
@@ -475,6 +457,25 @@ config USB_CONFIGFS_F_TCM
475 Both protocols can work on USB2.0 and USB3.0. 457 Both protocols can work on USB2.0 and USB3.0.
476 UAS utilizes the USB 3.0 feature called streams support. 458 UAS utilizes the USB 3.0 feature called streams support.
477 459
460choice
461 tristate "USB Gadget Drivers"
462 default USB_ETH
463 help
464 A Linux "Gadget Driver" talks to the USB Peripheral Controller
465 driver through the abstract "gadget" API. Some other operating
466 systems call these "client" drivers, of which "class drivers"
467 are a subset (implementing a USB device class specification).
468 A gadget driver implements one or more USB functions using
469 the peripheral hardware.
470
471 Gadget drivers are hardware-neutral, or "platform independent",
472 except that they sometimes must understand quirks or limitations
473 of the particular controllers they work with. For example, when
474 a controller doesn't support alternate configurations or provide
475 enough of the right types of endpoints, the gadget driver might
476 not be able work with that controller, or might need to implement
477 a less common variant of a device class protocol.
478
478source "drivers/usb/gadget/legacy/Kconfig" 479source "drivers/usb/gadget/legacy/Kconfig"
479 480
480endchoice 481endchoice
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 5ebe6af7976e..32176f779861 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1893,17 +1893,21 @@ unknown:
1893 /* functions always handle their interfaces and endpoints... 1893 /* functions always handle their interfaces and endpoints...
1894 * punt other recipients (other, WUSB, ...) to the current 1894 * punt other recipients (other, WUSB, ...) to the current
1895 * configuration code. 1895 * configuration code.
1896 *
1897 * REVISIT it could make sense to let the composite device
1898 * take such requests too, if that's ever needed: to work
1899 * in config 0, etc.
1900 */ 1896 */
1901 if (cdev->config) { 1897 if (cdev->config) {
1902 list_for_each_entry(f, &cdev->config->functions, list) 1898 list_for_each_entry(f, &cdev->config->functions, list)
1903 if (f->req_match && f->req_match(f, ctrl)) 1899 if (f->req_match &&
1900 f->req_match(f, ctrl, false))
1904 goto try_fun_setup; 1901 goto try_fun_setup;
1905 f = NULL; 1902 } else {
1903 struct usb_configuration *c;
1904 list_for_each_entry(c, &cdev->configs, list)
1905 list_for_each_entry(f, &c->functions, list)
1906 if (f->req_match &&
1907 f->req_match(f, ctrl, true))
1908 goto try_fun_setup;
1906 } 1909 }
1910 f = NULL;
1907 1911
1908 switch (ctrl->bRequestType & USB_RECIP_MASK) { 1912 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1909 case USB_RECIP_INTERFACE: 1913 case USB_RECIP_INTERFACE:
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index f9237fe2be05..3984787f8e97 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1211,8 +1211,9 @@ static void purge_configs_funcs(struct gadget_info *gi)
1211 1211
1212 list_move_tail(&f->list, &cfg->func_list); 1212 list_move_tail(&f->list, &cfg->func_list);
1213 if (f->unbind) { 1213 if (f->unbind) {
1214 dev_err(&gi->cdev.gadget->dev, "unbind function" 1214 dev_dbg(&gi->cdev.gadget->dev,
1215 " '%s'/%p\n", f->name, f); 1215 "unbind function '%s'/%p\n",
1216 f->name, f);
1216 f->unbind(c, f); 1217 f->unbind(c, f);
1217 } 1218 }
1218 } 1219 }
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 5c8429f23a89..0aeed85bb5cb 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -98,6 +98,9 @@ static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
98static void ffs_func_disable(struct usb_function *); 98static void ffs_func_disable(struct usb_function *);
99static int ffs_func_setup(struct usb_function *, 99static int ffs_func_setup(struct usb_function *,
100 const struct usb_ctrlrequest *); 100 const struct usb_ctrlrequest *);
101static bool ffs_func_req_match(struct usb_function *,
102 const struct usb_ctrlrequest *,
103 bool config0);
101static void ffs_func_suspend(struct usb_function *); 104static void ffs_func_suspend(struct usb_function *);
102static void ffs_func_resume(struct usb_function *); 105static void ffs_func_resume(struct usb_function *);
103 106
@@ -2243,7 +2246,9 @@ static int __ffs_data_got_descs(struct ffs_data *ffs,
2243 FUNCTIONFS_HAS_SS_DESC | 2246 FUNCTIONFS_HAS_SS_DESC |
2244 FUNCTIONFS_HAS_MS_OS_DESC | 2247 FUNCTIONFS_HAS_MS_OS_DESC |
2245 FUNCTIONFS_VIRTUAL_ADDR | 2248 FUNCTIONFS_VIRTUAL_ADDR |
2246 FUNCTIONFS_EVENTFD)) { 2249 FUNCTIONFS_EVENTFD |
2250 FUNCTIONFS_ALL_CTRL_RECIP |
2251 FUNCTIONFS_CONFIG0_SETUP)) {
2247 ret = -ENOSYS; 2252 ret = -ENOSYS;
2248 goto error; 2253 goto error;
2249 } 2254 }
@@ -3094,8 +3099,9 @@ static int ffs_func_setup(struct usb_function *f,
3094 * handle them. All other either handled by composite or 3099 * handle them. All other either handled by composite or
3095 * passed to usb_configuration->setup() (if one is set). No 3100 * passed to usb_configuration->setup() (if one is set). No
3096 * matter, we will handle requests directed to endpoint here 3101 * matter, we will handle requests directed to endpoint here
3097 * as well (as it's straightforward) but what to do with any 3102 * as well (as it's straightforward). Other request recipient
3098 * other request? 3103 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
3104 * is being used.
3099 */ 3105 */
3100 if (ffs->state != FFS_ACTIVE) 3106 if (ffs->state != FFS_ACTIVE)
3101 return -ENODEV; 3107 return -ENODEV;
@@ -3116,7 +3122,10 @@ static int ffs_func_setup(struct usb_function *f,
3116 break; 3122 break;
3117 3123
3118 default: 3124 default:
3119 return -EOPNOTSUPP; 3125 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
3126 ret = le16_to_cpu(creq->wIndex);
3127 else
3128 return -EOPNOTSUPP;
3120 } 3129 }
3121 3130
3122 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); 3131 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
@@ -3128,6 +3137,28 @@ static int ffs_func_setup(struct usb_function *f,
3128 return 0; 3137 return 0;
3129} 3138}
3130 3139
3140static bool ffs_func_req_match(struct usb_function *f,
3141 const struct usb_ctrlrequest *creq,
3142 bool config0)
3143{
3144 struct ffs_function *func = ffs_func_from_usb(f);
3145
3146 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
3147 return false;
3148
3149 switch (creq->bRequestType & USB_RECIP_MASK) {
3150 case USB_RECIP_INTERFACE:
3151 return ffs_func_revmap_intf(func,
3152 le16_to_cpu(creq->wIndex) >= 0);
3153 case USB_RECIP_ENDPOINT:
3154 return ffs_func_revmap_ep(func,
3155 le16_to_cpu(creq->wIndex) >= 0);
3156 default:
3157 return (bool) (func->ffs->user_flags &
3158 FUNCTIONFS_ALL_CTRL_RECIP);
3159 }
3160}
3161
3131static void ffs_func_suspend(struct usb_function *f) 3162static void ffs_func_suspend(struct usb_function *f)
3132{ 3163{
3133 ENTER(); 3164 ENTER();
@@ -3378,6 +3409,7 @@ static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3378 func->function.set_alt = ffs_func_set_alt; 3409 func->function.set_alt = ffs_func_set_alt;
3379 func->function.disable = ffs_func_disable; 3410 func->function.disable = ffs_func_disable;
3380 func->function.setup = ffs_func_setup; 3411 func->function.setup = ffs_func_setup;
3412 func->function.req_match = ffs_func_req_match;
3381 func->function.suspend = ffs_func_suspend; 3413 func->function.suspend = ffs_func_suspend;
3382 func->function.resume = ffs_func_resume; 3414 func->function.resume = ffs_func_resume;
3383 func->function.free_func = ffs_free; 3415 func->function.free_func = ffs_free;
@@ -3470,6 +3502,11 @@ static void _ffs_free_dev(struct ffs_dev *dev)
3470 list_del(&dev->entry); 3502 list_del(&dev->entry);
3471 if (dev->name_allocated) 3503 if (dev->name_allocated)
3472 kfree(dev->name); 3504 kfree(dev->name);
3505
3506 /* Clear the private_data pointer to stop incorrect dev access */
3507 if (dev->ffs_data)
3508 dev->ffs_data->private_data = NULL;
3509
3473 kfree(dev); 3510 kfree(dev);
3474 if (list_empty(&ffs_devices)) 3511 if (list_empty(&ffs_devices))
3475 functionfs_cleanup(); 3512 functionfs_cleanup();
diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index 51980c50546d..e2966f87c860 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -365,7 +365,7 @@ static int f_hidg_open(struct inode *inode, struct file *fd)
365static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 365static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
366 unsigned length) 366 unsigned length)
367{ 367{
368 return alloc_ep_req(ep, length, length); 368 return alloc_ep_req(ep, length);
369} 369}
370 370
371static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 371static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
@@ -617,14 +617,10 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
617 617
618 /* preallocate request and buffer */ 618 /* preallocate request and buffer */
619 status = -ENOMEM; 619 status = -ENOMEM;
620 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL); 620 hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length);
621 if (!hidg->req) 621 if (!hidg->req)
622 goto fail; 622 goto fail;
623 623
624 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
625 if (!hidg->req->buf)
626 goto fail;
627
628 /* set descriptor dynamic values */ 624 /* set descriptor dynamic values */
629 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 625 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
630 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 626 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
@@ -677,11 +673,8 @@ fail_free_descs:
677 usb_free_all_descriptors(f); 673 usb_free_all_descriptors(f);
678fail: 674fail:
679 ERROR(f->config->cdev, "hidg_bind FAILED\n"); 675 ERROR(f->config->cdev, "hidg_bind FAILED\n");
680 if (hidg->req != NULL) { 676 if (hidg->req != NULL)
681 kfree(hidg->req->buf); 677 free_ep_req(hidg->in_ep, hidg->req);
682 if (hidg->in_ep != NULL)
683 usb_ep_free_request(hidg->in_ep, hidg->req);
684 }
685 678
686 return status; 679 return status;
687} 680}
@@ -809,11 +802,21 @@ end:
809 802
810CONFIGFS_ATTR(f_hid_opts_, report_desc); 803CONFIGFS_ATTR(f_hid_opts_, report_desc);
811 804
805static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
806{
807 struct f_hid_opts *opts = to_f_hid_opts(item);
808
809 return sprintf(page, "%d:%d\n", major, opts->minor);
810}
811
812CONFIGFS_ATTR_RO(f_hid_opts_, dev);
813
812static struct configfs_attribute *hid_attrs[] = { 814static struct configfs_attribute *hid_attrs[] = {
813 &f_hid_opts_attr_subclass, 815 &f_hid_opts_attr_subclass,
814 &f_hid_opts_attr_protocol, 816 &f_hid_opts_attr_protocol,
815 &f_hid_opts_attr_report_length, 817 &f_hid_opts_attr_report_length,
816 &f_hid_opts_attr_report_desc, 818 &f_hid_opts_attr_report_desc,
819 &f_hid_opts_attr_dev,
817 NULL, 820 NULL,
818}; 821};
819 822
@@ -910,8 +913,7 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
910 913
911 /* disable/free request and end point */ 914 /* disable/free request and end point */
912 usb_ep_disable(hidg->in_ep); 915 usb_ep_disable(hidg->in_ep);
913 kfree(hidg->req->buf); 916 free_ep_req(hidg->in_ep, hidg->req);
914 usb_ep_free_request(hidg->in_ep, hidg->req);
915 917
916 usb_free_all_descriptors(f); 918 usb_free_all_descriptors(f);
917} 919}
diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 3a9f8f9c77bd..e70093835e14 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -308,9 +308,7 @@ static void disable_loopback(struct f_loopback *loop)
308 308
309static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len) 309static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
310{ 310{
311 struct f_loopback *loop = ep->driver_data; 311 return alloc_ep_req(ep, len);
312
313 return alloc_ep_req(ep, len, loop->buflen);
314} 312}
315 313
316static int alloc_requests(struct usb_composite_dev *cdev, 314static int alloc_requests(struct usb_composite_dev *cdev,
@@ -333,7 +331,7 @@ static int alloc_requests(struct usb_composite_dev *cdev,
333 if (!in_req) 331 if (!in_req)
334 goto fail; 332 goto fail;
335 333
336 out_req = lb_alloc_ep_req(loop->out_ep, 0); 334 out_req = lb_alloc_ep_req(loop->out_ep, loop->buflen);
337 if (!out_req) 335 if (!out_req)
338 goto fail_in; 336 goto fail_in;
339 337
@@ -593,13 +591,9 @@ DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
593 591
594int __init lb_modinit(void) 592int __init lb_modinit(void)
595{ 593{
596 int ret; 594 return usb_function_register(&Loopbackusb_func);
597
598 ret = usb_function_register(&Loopbackusb_func);
599 if (ret)
600 return ret;
601 return ret;
602} 595}
596
603void __exit lb_modexit(void) 597void __exit lb_modexit(void)
604{ 598{
605 usb_function_unregister(&Loopbackusb_func); 599 usb_function_unregister(&Loopbackusb_func);
diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 2505117e88e8..8f3659b65f53 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -311,11 +311,7 @@ struct fsg_common {
311 /* Gadget's private data. */ 311 /* Gadget's private data. */
312 void *private_data; 312 void *private_data;
313 313
314 /* 314 char inquiry_string[INQUIRY_STRING_LEN];
315 * Vendor (8 chars), product (16 chars), release (4
316 * hexadecimal digits) and NUL byte
317 */
318 char inquiry_string[8 + 16 + 4 + 1];
319 315
320 struct kref ref; 316 struct kref ref;
321}; 317};
@@ -1107,7 +1103,12 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1107 buf[5] = 0; /* No special options */ 1103 buf[5] = 0; /* No special options */
1108 buf[6] = 0; 1104 buf[6] = 0;
1109 buf[7] = 0; 1105 buf[7] = 0;
1110 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string); 1106 if (curlun->inquiry_string[0])
1107 memcpy(buf + 8, curlun->inquiry_string,
1108 sizeof(curlun->inquiry_string));
1109 else
1110 memcpy(buf + 8, common->inquiry_string,
1111 sizeof(common->inquiry_string));
1111 return 36; 1112 return 36;
1112} 1113}
1113 1114
@@ -3209,12 +3210,27 @@ static ssize_t fsg_lun_opts_nofua_store(struct config_item *item,
3209 3210
3210CONFIGFS_ATTR(fsg_lun_opts_, nofua); 3211CONFIGFS_ATTR(fsg_lun_opts_, nofua);
3211 3212
3213static ssize_t fsg_lun_opts_inquiry_string_show(struct config_item *item,
3214 char *page)
3215{
3216 return fsg_show_inquiry_string(to_fsg_lun_opts(item)->lun, page);
3217}
3218
3219static ssize_t fsg_lun_opts_inquiry_string_store(struct config_item *item,
3220 const char *page, size_t len)
3221{
3222 return fsg_store_inquiry_string(to_fsg_lun_opts(item)->lun, page, len);
3223}
3224
3225CONFIGFS_ATTR(fsg_lun_opts_, inquiry_string);
3226
3212static struct configfs_attribute *fsg_lun_attrs[] = { 3227static struct configfs_attribute *fsg_lun_attrs[] = {
3213 &fsg_lun_opts_attr_file, 3228 &fsg_lun_opts_attr_file,
3214 &fsg_lun_opts_attr_ro, 3229 &fsg_lun_opts_attr_ro,
3215 &fsg_lun_opts_attr_removable, 3230 &fsg_lun_opts_attr_removable,
3216 &fsg_lun_opts_attr_cdrom, 3231 &fsg_lun_opts_attr_cdrom,
3217 &fsg_lun_opts_attr_nofua, 3232 &fsg_lun_opts_attr_nofua,
3233 &fsg_lun_opts_attr_inquiry_string,
3218 NULL, 3234 NULL,
3219}; 3235};
3220 3236
diff --git a/drivers/usb/gadget/function/f_mass_storage.h b/drivers/usb/gadget/function/f_mass_storage.h
index b6a9918eaefb..d3902313b8ac 100644
--- a/drivers/usb/gadget/function/f_mass_storage.h
+++ b/drivers/usb/gadget/function/f_mass_storage.h
@@ -100,6 +100,7 @@ struct fsg_lun_config {
100 char removable; 100 char removable;
101 char cdrom; 101 char cdrom;
102 char nofua; 102 char nofua;
103 char inquiry_string[INQUIRY_STRING_LEN];
103}; 104};
104 105
105struct fsg_config { 106struct fsg_config {
diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
index 58fc199a18ec..a5719f271bf0 100644
--- a/drivers/usb/gadget/function/f_midi.c
+++ b/drivers/usb/gadget/function/f_midi.c
@@ -51,6 +51,19 @@ static const char f_midi_longname[] = "MIDI Gadget";
51 */ 51 */
52#define MAX_PORTS 16 52#define MAX_PORTS 16
53 53
54/* MIDI message states */
55enum {
56 STATE_INITIAL = 0, /* pseudo state */
57 STATE_1PARAM,
58 STATE_2PARAM_1,
59 STATE_2PARAM_2,
60 STATE_SYSEX_0,
61 STATE_SYSEX_1,
62 STATE_SYSEX_2,
63 STATE_REAL_TIME,
64 STATE_FINISHED, /* pseudo state */
65};
66
54/* 67/*
55 * This is a gadget, and the IN/OUT naming is from the host's perspective. 68 * This is a gadget, and the IN/OUT naming is from the host's perspective.
56 * USB -> OUT endpoint -> rawmidi 69 * USB -> OUT endpoint -> rawmidi
@@ -61,13 +74,6 @@ struct gmidi_in_port {
61 int active; 74 int active;
62 uint8_t cable; 75 uint8_t cable;
63 uint8_t state; 76 uint8_t state;
64#define STATE_UNKNOWN 0
65#define STATE_1PARAM 1
66#define STATE_2PARAM_1 2
67#define STATE_2PARAM_2 3
68#define STATE_SYSEX_0 4
69#define STATE_SYSEX_1 5
70#define STATE_SYSEX_2 6
71 uint8_t data[2]; 77 uint8_t data[2];
72}; 78};
73 79
@@ -205,7 +211,7 @@ static struct usb_gadget_strings *midi_strings[] = {
205static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep, 211static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
206 unsigned length) 212 unsigned length)
207{ 213{
208 return alloc_ep_req(ep, length, length); 214 return alloc_ep_req(ep, length);
209} 215}
210 216
211static const uint8_t f_midi_cin_length[] = { 217static const uint8_t f_midi_cin_length[] = {
@@ -299,6 +305,19 @@ f_midi_complete(struct usb_ep *ep, struct usb_request *req)
299 } 305 }
300} 306}
301 307
308static void f_midi_drop_out_substreams(struct f_midi *midi)
309{
310 unsigned int i;
311
312 for (i = 0; i < midi->in_ports; i++) {
313 struct gmidi_in_port *port = midi->in_ports_array + i;
314 struct snd_rawmidi_substream *substream = port->substream;
315
316 if (port->active && substream)
317 snd_rawmidi_drop_output(substream);
318 }
319}
320
302static int f_midi_start_ep(struct f_midi *midi, 321static int f_midi_start_ep(struct f_midi *midi,
303 struct usb_function *f, 322 struct usb_function *f,
304 struct usb_ep *ep) 323 struct usb_ep *ep)
@@ -360,9 +379,8 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
360 /* allocate a bunch of read buffers and queue them all at once. */ 379 /* allocate a bunch of read buffers and queue them all at once. */
361 for (i = 0; i < midi->qlen && err == 0; i++) { 380 for (i = 0; i < midi->qlen && err == 0; i++) {
362 struct usb_request *req = 381 struct usb_request *req =
363 midi_alloc_ep_req(midi->out_ep, 382 midi_alloc_ep_req(midi->out_ep, midi->buflen);
364 max_t(unsigned, midi->buflen, 383
365 bulk_out_desc.wMaxPacketSize));
366 if (req == NULL) 384 if (req == NULL)
367 return -ENOMEM; 385 return -ENOMEM;
368 386
@@ -397,6 +415,8 @@ static void f_midi_disable(struct usb_function *f)
397 /* release IN requests */ 415 /* release IN requests */
398 while (kfifo_get(&midi->in_req_fifo, &req)) 416 while (kfifo_get(&midi->in_req_fifo, &req))
399 free_ep_req(midi->in_ep, req); 417 free_ep_req(midi->in_ep, req);
418
419 f_midi_drop_out_substreams(midi);
400} 420}
401 421
402static int f_midi_snd_free(struct snd_device *device) 422static int f_midi_snd_free(struct snd_device *device)
@@ -404,130 +424,166 @@ static int f_midi_snd_free(struct snd_device *device)
404 return 0; 424 return 0;
405} 425}
406 426
407static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
408 uint8_t p1, uint8_t p2, uint8_t p3)
409{
410 unsigned length = req->length;
411 u8 *buf = (u8 *)req->buf + length;
412
413 buf[0] = p0;
414 buf[1] = p1;
415 buf[2] = p2;
416 buf[3] = p3;
417 req->length = length + 4;
418}
419
420/* 427/*
421 * Converts MIDI commands to USB MIDI packets. 428 * Converts MIDI commands to USB MIDI packets.
422 */ 429 */
423static void f_midi_transmit_byte(struct usb_request *req, 430static void f_midi_transmit_byte(struct usb_request *req,
424 struct gmidi_in_port *port, uint8_t b) 431 struct gmidi_in_port *port, uint8_t b)
425{ 432{
426 uint8_t p0 = port->cable << 4; 433 uint8_t p[4] = { port->cable << 4, 0, 0, 0 };
434 uint8_t next_state = STATE_INITIAL;
435
436 switch (b) {
437 case 0xf8 ... 0xff:
438 /* System Real-Time Messages */
439 p[0] |= 0x0f;
440 p[1] = b;
441 next_state = port->state;
442 port->state = STATE_REAL_TIME;
443 break;
427 444
428 if (b >= 0xf8) { 445 case 0xf7:
429 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0); 446 /* End of SysEx */
430 } else if (b >= 0xf0) { 447 switch (port->state) {
448 case STATE_SYSEX_0:
449 p[0] |= 0x05;
450 p[1] = 0xf7;
451 next_state = STATE_FINISHED;
452 break;
453 case STATE_SYSEX_1:
454 p[0] |= 0x06;
455 p[1] = port->data[0];
456 p[2] = 0xf7;
457 next_state = STATE_FINISHED;
458 break;
459 case STATE_SYSEX_2:
460 p[0] |= 0x07;
461 p[1] = port->data[0];
462 p[2] = port->data[1];
463 p[3] = 0xf7;
464 next_state = STATE_FINISHED;
465 break;
466 default:
467 /* Ignore byte */
468 next_state = port->state;
469 port->state = STATE_INITIAL;
470 }
471 break;
472
473 case 0xf0 ... 0xf6:
474 /* System Common Messages */
475 port->data[0] = port->data[1] = 0;
476 port->state = STATE_INITIAL;
431 switch (b) { 477 switch (b) {
432 case 0xf0: 478 case 0xf0:
433 port->data[0] = b; 479 port->data[0] = b;
434 port->state = STATE_SYSEX_1; 480 port->data[1] = 0;
481 next_state = STATE_SYSEX_1;
435 break; 482 break;
436 case 0xf1: 483 case 0xf1:
437 case 0xf3: 484 case 0xf3:
438 port->data[0] = b; 485 port->data[0] = b;
439 port->state = STATE_1PARAM; 486 next_state = STATE_1PARAM;
440 break; 487 break;
441 case 0xf2: 488 case 0xf2:
442 port->data[0] = b; 489 port->data[0] = b;
443 port->state = STATE_2PARAM_1; 490 next_state = STATE_2PARAM_1;
444 break; 491 break;
445 case 0xf4: 492 case 0xf4:
446 case 0xf5: 493 case 0xf5:
447 port->state = STATE_UNKNOWN; 494 next_state = STATE_INITIAL;
448 break; 495 break;
449 case 0xf6: 496 case 0xf6:
450 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0); 497 p[0] |= 0x05;
451 port->state = STATE_UNKNOWN; 498 p[1] = 0xf6;
452 break; 499 next_state = STATE_FINISHED;
453 case 0xf7:
454 switch (port->state) {
455 case STATE_SYSEX_0:
456 f_midi_transmit_packet(req,
457 p0 | 0x05, 0xf7, 0, 0);
458 break;
459 case STATE_SYSEX_1:
460 f_midi_transmit_packet(req,
461 p0 | 0x06, port->data[0], 0xf7, 0);
462 break;
463 case STATE_SYSEX_2:
464 f_midi_transmit_packet(req,
465 p0 | 0x07, port->data[0],
466 port->data[1], 0xf7);
467 break;
468 }
469 port->state = STATE_UNKNOWN;
470 break; 500 break;
471 } 501 }
472 } else if (b >= 0x80) { 502 break;
503
504 case 0x80 ... 0xef:
505 /*
506 * Channel Voice Messages, Channel Mode Messages
507 * and Control Change Messages.
508 */
473 port->data[0] = b; 509 port->data[0] = b;
510 port->data[1] = 0;
511 port->state = STATE_INITIAL;
474 if (b >= 0xc0 && b <= 0xdf) 512 if (b >= 0xc0 && b <= 0xdf)
475 port->state = STATE_1PARAM; 513 next_state = STATE_1PARAM;
476 else 514 else
477 port->state = STATE_2PARAM_1; 515 next_state = STATE_2PARAM_1;
478 } else { /* b < 0x80 */ 516 break;
517
518 case 0x00 ... 0x7f:
519 /* Message parameters */
479 switch (port->state) { 520 switch (port->state) {
480 case STATE_1PARAM: 521 case STATE_1PARAM:
481 if (port->data[0] < 0xf0) { 522 if (port->data[0] < 0xf0)
482 p0 |= port->data[0] >> 4; 523 p[0] |= port->data[0] >> 4;
483 } else { 524 else
484 p0 |= 0x02; 525 p[0] |= 0x02;
485 port->state = STATE_UNKNOWN; 526
486 } 527 p[1] = port->data[0];
487 f_midi_transmit_packet(req, p0, port->data[0], b, 0); 528 p[2] = b;
529 /* This is to allow Running State Messages */
530 next_state = STATE_1PARAM;
488 break; 531 break;
489 case STATE_2PARAM_1: 532 case STATE_2PARAM_1:
490 port->data[1] = b; 533 port->data[1] = b;
491 port->state = STATE_2PARAM_2; 534 next_state = STATE_2PARAM_2;
492 break; 535 break;
493 case STATE_2PARAM_2: 536 case STATE_2PARAM_2:
494 if (port->data[0] < 0xf0) { 537 if (port->data[0] < 0xf0)
495 p0 |= port->data[0] >> 4; 538 p[0] |= port->data[0] >> 4;
496 port->state = STATE_2PARAM_1; 539 else
497 } else { 540 p[0] |= 0x03;
498 p0 |= 0x03; 541
499 port->state = STATE_UNKNOWN; 542 p[1] = port->data[0];
500 } 543 p[2] = port->data[1];
501 f_midi_transmit_packet(req, 544 p[3] = b;
502 p0, port->data[0], port->data[1], b); 545 /* This is to allow Running State Messages */
546 next_state = STATE_2PARAM_1;
503 break; 547 break;
504 case STATE_SYSEX_0: 548 case STATE_SYSEX_0:
505 port->data[0] = b; 549 port->data[0] = b;
506 port->state = STATE_SYSEX_1; 550 next_state = STATE_SYSEX_1;
507 break; 551 break;
508 case STATE_SYSEX_1: 552 case STATE_SYSEX_1:
509 port->data[1] = b; 553 port->data[1] = b;
510 port->state = STATE_SYSEX_2; 554 next_state = STATE_SYSEX_2;
511 break; 555 break;
512 case STATE_SYSEX_2: 556 case STATE_SYSEX_2:
513 f_midi_transmit_packet(req, 557 p[0] |= 0x04;
514 p0 | 0x04, port->data[0], port->data[1], b); 558 p[1] = port->data[0];
515 port->state = STATE_SYSEX_0; 559 p[2] = port->data[1];
560 p[3] = b;
561 next_state = STATE_SYSEX_0;
516 break; 562 break;
517 } 563 }
564 break;
518 } 565 }
519}
520 566
521static void f_midi_drop_out_substreams(struct f_midi *midi) 567 /* States where we have to write into the USB request */
522{ 568 if (next_state == STATE_FINISHED ||
523 unsigned int i; 569 port->state == STATE_SYSEX_2 ||
570 port->state == STATE_1PARAM ||
571 port->state == STATE_2PARAM_2 ||
572 port->state == STATE_REAL_TIME) {
524 573
525 for (i = 0; i < midi->in_ports; i++) { 574 unsigned int length = req->length;
526 struct gmidi_in_port *port = midi->in_ports_array + i; 575 u8 *buf = (u8 *)req->buf + length;
527 struct snd_rawmidi_substream *substream = port->substream; 576
528 if (port->active && substream) 577 memcpy(buf, p, sizeof(p));
529 snd_rawmidi_drop_output(substream); 578 req->length = length + sizeof(p);
579
580 if (next_state == STATE_FINISHED) {
581 next_state = STATE_INITIAL;
582 port->data[0] = port->data[1] = 0;
583 }
530 } 584 }
585
586 port->state = next_state;
531} 587}
532 588
533static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep) 589static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)
@@ -642,7 +698,7 @@ static int f_midi_in_open(struct snd_rawmidi_substream *substream)
642 VDBG(midi, "%s()\n", __func__); 698 VDBG(midi, "%s()\n", __func__);
643 port = midi->in_ports_array + substream->number; 699 port = midi->in_ports_array + substream->number;
644 port->substream = substream; 700 port->substream = substream;
645 port->state = STATE_UNKNOWN; 701 port->state = STATE_INITIAL;
646 return 0; 702 return 0;
647} 703}
648 704
@@ -1123,7 +1179,7 @@ static struct usb_function_instance *f_midi_alloc_inst(void)
1123 opts->func_inst.free_func_inst = f_midi_free_inst; 1179 opts->func_inst.free_func_inst = f_midi_free_inst;
1124 opts->index = SNDRV_DEFAULT_IDX1; 1180 opts->index = SNDRV_DEFAULT_IDX1;
1125 opts->id = SNDRV_DEFAULT_STR1; 1181 opts->id = SNDRV_DEFAULT_STR1;
1126 opts->buflen = 256; 1182 opts->buflen = 512;
1127 opts->qlen = 32; 1183 opts->qlen = 32;
1128 opts->in_ports = 1; 1184 opts->in_ports = 1;
1129 opts->out_ports = 1; 1185 opts->out_ports = 1;
diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
index 97f0a9bc84df..639603722709 100644
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -90,7 +90,9 @@ static inline struct f_ncm *func_to_ncm(struct usb_function *f)
90/* peak (theoretical) bulk transfer rate in bits-per-second */ 90/* peak (theoretical) bulk transfer rate in bits-per-second */
91static inline unsigned ncm_bitrate(struct usb_gadget *g) 91static inline unsigned ncm_bitrate(struct usb_gadget *g)
92{ 92{
93 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 93 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
94 return 13 * 1024 * 8 * 1000 * 8;
95 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
94 return 13 * 512 * 8 * 1000 * 8; 96 return 13 * 512 * 8 * 1000 * 8;
95 else 97 else
96 return 19 * 64 * 1 * 1000 * 8; 98 return 19 * 64 * 1 * 1000 * 8;
@@ -333,6 +335,76 @@ static struct usb_descriptor_header *ncm_hs_function[] = {
333 NULL, 335 NULL,
334}; 336};
335 337
338
339/* super speed support: */
340
341static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
344
345 .bEndpointAddress = USB_DIR_IN,
346 .bmAttributes = USB_ENDPOINT_XFER_INT,
347 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
348 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
349};
350
351static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
352 .bLength = sizeof(ss_ncm_notify_comp_desc),
353 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
354
355 /* the following 3 values can be tweaked if necessary */
356 /* .bMaxBurst = 0, */
357 /* .bmAttributes = 0, */
358 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
359};
360
361static struct usb_endpoint_descriptor ss_ncm_in_desc = {
362 .bLength = USB_DT_ENDPOINT_SIZE,
363 .bDescriptorType = USB_DT_ENDPOINT,
364
365 .bEndpointAddress = USB_DIR_IN,
366 .bmAttributes = USB_ENDPOINT_XFER_BULK,
367 .wMaxPacketSize = cpu_to_le16(1024),
368};
369
370static struct usb_endpoint_descriptor ss_ncm_out_desc = {
371 .bLength = USB_DT_ENDPOINT_SIZE,
372 .bDescriptorType = USB_DT_ENDPOINT,
373
374 .bEndpointAddress = USB_DIR_OUT,
375 .bmAttributes = USB_ENDPOINT_XFER_BULK,
376 .wMaxPacketSize = cpu_to_le16(1024),
377};
378
379static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
380 .bLength = sizeof(ss_ncm_bulk_comp_desc),
381 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
382
383 /* the following 2 values can be tweaked if necessary */
384 /* .bMaxBurst = 0, */
385 /* .bmAttributes = 0, */
386};
387
388static struct usb_descriptor_header *ncm_ss_function[] = {
389 (struct usb_descriptor_header *) &ncm_iad_desc,
390 /* CDC NCM control descriptors */
391 (struct usb_descriptor_header *) &ncm_control_intf,
392 (struct usb_descriptor_header *) &ncm_header_desc,
393 (struct usb_descriptor_header *) &ncm_union_desc,
394 (struct usb_descriptor_header *) &ecm_desc,
395 (struct usb_descriptor_header *) &ncm_desc,
396 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
397 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
398 /* data interface, altsettings 0 and 1 */
399 (struct usb_descriptor_header *) &ncm_data_nop_intf,
400 (struct usb_descriptor_header *) &ncm_data_intf,
401 (struct usb_descriptor_header *) &ss_ncm_in_desc,
402 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
403 (struct usb_descriptor_header *) &ss_ncm_out_desc,
404 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
405 NULL,
406};
407
336/* string descriptors: */ 408/* string descriptors: */
337 409
338#define STRING_CTRL_IDX 0 410#define STRING_CTRL_IDX 0
@@ -852,6 +924,8 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
852 */ 924 */
853 ncm->port.is_zlp_ok = 925 ncm->port.is_zlp_ok =
854 gadget_is_zlp_supported(cdev->gadget); 926 gadget_is_zlp_supported(cdev->gadget);
927 ncm->port.no_skb_reserve =
928 gadget_avoids_skb_reserve(cdev->gadget);
855 ncm->port.cdc_filter = DEFAULT_FILTER; 929 ncm->port.cdc_filter = DEFAULT_FILTER;
856 DBG(cdev, "activate ncm\n"); 930 DBG(cdev, "activate ncm\n");
857 net = gether_connect(&ncm->port); 931 net = gether_connect(&ncm->port);
@@ -1431,8 +1505,13 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1431 hs_ncm_notify_desc.bEndpointAddress = 1505 hs_ncm_notify_desc.bEndpointAddress =
1432 fs_ncm_notify_desc.bEndpointAddress; 1506 fs_ncm_notify_desc.bEndpointAddress;
1433 1507
1508 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1509 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1510 ss_ncm_notify_desc.bEndpointAddress =
1511 fs_ncm_notify_desc.bEndpointAddress;
1512
1434 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function, 1513 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1435 NULL, NULL); 1514 ncm_ss_function, NULL);
1436 if (status) 1515 if (status)
1437 goto fail; 1516 goto fail;
1438 1517
@@ -1450,6 +1529,7 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1450 ncm->task_timer.function = ncm_tx_timeout; 1529 ncm->task_timer.function = ncm_tx_timeout;
1451 1530
1452 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n", 1531 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1532 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1453 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", 1533 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1454 ncm->port.in_ep->name, ncm->port.out_ep->name, 1534 ncm->port.in_ep->name, ncm->port.out_ep->name,
1455 ncm->notify->name); 1535 ncm->notify->name);
diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
index 64706a789580..0de36cda6e41 100644
--- a/drivers/usb/gadget/function/f_printer.c
+++ b/drivers/usb/gadget/function/f_printer.c
@@ -889,13 +889,17 @@ static void printer_soft_reset(struct printer_dev *dev)
889/*-------------------------------------------------------------------------*/ 889/*-------------------------------------------------------------------------*/
890 890
891static bool gprinter_req_match(struct usb_function *f, 891static bool gprinter_req_match(struct usb_function *f,
892 const struct usb_ctrlrequest *ctrl) 892 const struct usb_ctrlrequest *ctrl,
893 bool config0)
893{ 894{
894 struct printer_dev *dev = func_to_printer(f); 895 struct printer_dev *dev = func_to_printer(f);
895 u16 w_index = le16_to_cpu(ctrl->wIndex); 896 u16 w_index = le16_to_cpu(ctrl->wIndex);
896 u16 w_value = le16_to_cpu(ctrl->wValue); 897 u16 w_value = le16_to_cpu(ctrl->wValue);
897 u16 w_length = le16_to_cpu(ctrl->wLength); 898 u16 w_length = le16_to_cpu(ctrl->wLength);
898 899
900 if (config0)
901 return false;
902
899 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE || 903 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
900 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) 904 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
901 return false; 905 return false;
diff --git a/drivers/usb/gadget/function/f_sourcesink.c b/drivers/usb/gadget/function/f_sourcesink.c
index df0189ddfdd5..8784fa12ea2c 100644
--- a/drivers/usb/gadget/function/f_sourcesink.c
+++ b/drivers/usb/gadget/function/f_sourcesink.c
@@ -293,9 +293,7 @@ static struct usb_gadget_strings *sourcesink_strings[] = {
293 293
294static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len) 294static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
295{ 295{
296 struct f_sourcesink *ss = ep->driver_data; 296 return alloc_ep_req(ep, len);
297
298 return alloc_ep_req(ep, len, ss->buflen);
299} 297}
300 298
301static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) 299static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
@@ -606,7 +604,7 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
606 } else { 604 } else {
607 ep = is_in ? ss->in_ep : ss->out_ep; 605 ep = is_in ? ss->in_ep : ss->out_ep;
608 qlen = ss->bulk_qlen; 606 qlen = ss->bulk_qlen;
609 size = 0; 607 size = ss->buflen;
610 } 608 }
611 609
612 for (i = 0; i < qlen; i++) { 610 for (i = 0; i < qlen; i++) {
diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
index 29b41b5dee04..27ed51b5082f 100644
--- a/drivers/usb/gadget/function/f_uvc.c
+++ b/drivers/usb/gadget/function/f_uvc.c
@@ -258,6 +258,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); 258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
259 v4l2_event_queue(&uvc->vdev, &v4l2_event); 259 v4l2_event_queue(&uvc->vdev, &v4l2_event);
260 260
261 /* Pass additional setup data to userspace */
262 if (uvc->event_setup_out && uvc->event_length) {
263 uvc->control_req->length = uvc->event_length;
264 return usb_ep_queue(uvc->func.config->cdev->gadget->ep0,
265 uvc->control_req, GFP_ATOMIC);
266 }
267
261 return 0; 268 return 0;
262} 269}
263 270
diff --git a/drivers/usb/gadget/function/storage_common.c b/drivers/usb/gadget/function/storage_common.c
index 990df221c629..8fbf6861690d 100644
--- a/drivers/usb/gadget/function/storage_common.c
+++ b/drivers/usb/gadget/function/storage_common.c
@@ -369,6 +369,12 @@ ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
369} 369}
370EXPORT_SYMBOL_GPL(fsg_show_removable); 370EXPORT_SYMBOL_GPL(fsg_show_removable);
371 371
372ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf)
373{
374 return sprintf(buf, "%s\n", curlun->inquiry_string);
375}
376EXPORT_SYMBOL_GPL(fsg_show_inquiry_string);
377
372/* 378/*
373 * The caller must hold fsg->filesem for reading when calling this function. 379 * The caller must hold fsg->filesem for reading when calling this function.
374 */ 380 */
@@ -499,4 +505,22 @@ ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
499} 505}
500EXPORT_SYMBOL_GPL(fsg_store_removable); 506EXPORT_SYMBOL_GPL(fsg_store_removable);
501 507
508ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
509 size_t count)
510{
511 const size_t len = min(count, sizeof(curlun->inquiry_string));
512
513 if (len == 0 || buf[0] == '\n') {
514 curlun->inquiry_string[0] = 0;
515 } else {
516 snprintf(curlun->inquiry_string,
517 sizeof(curlun->inquiry_string), "%-28s", buf);
518 if (curlun->inquiry_string[len-1] == '\n')
519 curlun->inquiry_string[len-1] = ' ';
520 }
521
522 return count;
523}
524EXPORT_SYMBOL_GPL(fsg_store_inquiry_string);
525
502MODULE_LICENSE("GPL"); 526MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/function/storage_common.h b/drivers/usb/gadget/function/storage_common.h
index c3544e61da66..e69848994cb4 100644
--- a/drivers/usb/gadget/function/storage_common.h
+++ b/drivers/usb/gadget/function/storage_common.h
@@ -88,6 +88,12 @@ do { \
88#define ASC(x) ((u8) ((x) >> 8)) 88#define ASC(x) ((u8) ((x) >> 8))
89#define ASCQ(x) ((u8) (x)) 89#define ASCQ(x) ((u8) (x))
90 90
91/*
92 * Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
93 * byte
94 */
95#define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
96
91struct fsg_lun { 97struct fsg_lun {
92 struct file *filp; 98 struct file *filp;
93 loff_t file_length; 99 loff_t file_length;
@@ -112,6 +118,7 @@ struct fsg_lun {
112 struct device dev; 118 struct device dev;
113 const char *name; /* "lun.name" */ 119 const char *name; /* "lun.name" */
114 const char **name_pfx; /* "function.name" */ 120 const char **name_pfx; /* "function.name" */
121 char inquiry_string[INQUIRY_STRING_LEN];
115}; 122};
116 123
117static inline bool fsg_lun_is_open(struct fsg_lun *curlun) 124static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
@@ -210,6 +217,7 @@ ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
210ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf); 217ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
211ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, 218ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
212 char *buf); 219 char *buf);
220ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
213ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf); 221ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
214ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf); 222ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
215ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, 223ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
@@ -221,5 +229,7 @@ ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
221 const char *buf, size_t count); 229 const char *buf, size_t count);
222ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf, 230ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
223 size_t count); 231 size_t count);
232ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
233 size_t count);
224 234
225#endif /* USB_STORAGE_COMMON_H */ 235#endif /* USB_STORAGE_COMMON_H */
diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/function/u_ether.c
index 5f562c1ec795..8cb08033b7c1 100644
--- a/drivers/usb/gadget/function/u_ether.c
+++ b/drivers/usb/gadget/function/u_ether.c
@@ -82,6 +82,7 @@ struct eth_dev {
82#define WORK_RX_MEMORY 0 82#define WORK_RX_MEMORY 0
83 83
84 bool zlp; 84 bool zlp;
85 bool no_skb_reserve;
85 u8 host_mac[ETH_ALEN]; 86 u8 host_mac[ETH_ALEN];
86 u8 dev_mac[ETH_ALEN]; 87 u8 dev_mac[ETH_ALEN];
87}; 88};
@@ -233,7 +234,8 @@ rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
233 * but on at least one, checksumming fails otherwise. Note: 234 * but on at least one, checksumming fails otherwise. Note:
234 * RNDIS headers involve variable numbers of LE32 values. 235 * RNDIS headers involve variable numbers of LE32 values.
235 */ 236 */
236 skb_reserve(skb, NET_IP_ALIGN); 237 if (likely(!dev->no_skb_reserve))
238 skb_reserve(skb, NET_IP_ALIGN);
237 239
238 req->buf = skb->data; 240 req->buf = skb->data;
239 req->length = size; 241 req->length = size;
@@ -551,14 +553,16 @@ static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
551 spin_lock_irqsave(&dev->lock, flags); 553 spin_lock_irqsave(&dev->lock, flags);
552 if (dev->port_usb) 554 if (dev->port_usb)
553 skb = dev->wrap(dev->port_usb, skb); 555 skb = dev->wrap(dev->port_usb, skb);
554 spin_unlock_irqrestore(&dev->lock, flags);
555 if (!skb) { 556 if (!skb) {
556 /* Multi frame CDC protocols may store the frame for 557 /* Multi frame CDC protocols may store the frame for
557 * later which is not a dropped frame. 558 * later which is not a dropped frame.
558 */ 559 */
559 if (dev->port_usb && 560 if (dev->port_usb &&
560 dev->port_usb->supports_multi_frame) 561 dev->port_usb->supports_multi_frame) {
562 spin_unlock_irqrestore(&dev->lock, flags);
561 goto multiframe; 563 goto multiframe;
564 }
565 spin_unlock_irqrestore(&dev->lock, flags);
562 goto drop; 566 goto drop;
563 } 567 }
564 } 568 }
@@ -569,12 +573,14 @@ static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
569 req->complete = tx_complete; 573 req->complete = tx_complete;
570 574
571 /* NCM requires no zlp if transfer is dwNtbInMaxSize */ 575 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
572 if (dev->port_usb->is_fixed && 576 if (dev->port_usb &&
577 dev->port_usb->is_fixed &&
573 length == dev->port_usb->fixed_in_len && 578 length == dev->port_usb->fixed_in_len &&
574 (length % in->maxpacket) == 0) 579 (length % in->maxpacket) == 0)
575 req->zero = 0; 580 req->zero = 0;
576 else 581 else
577 req->zero = 1; 582 req->zero = 1;
583 spin_unlock_irqrestore(&dev->lock, flags);
578 584
579 /* use zlp framing on tx for strict CDC-Ether conformance, 585 /* use zlp framing on tx for strict CDC-Ether conformance,
580 * though any robust network rx path ignores extra padding. 586 * though any robust network rx path ignores extra padding.
@@ -1063,6 +1069,7 @@ struct net_device *gether_connect(struct gether *link)
1063 1069
1064 if (result == 0) { 1070 if (result == 0) {
1065 dev->zlp = link->is_zlp_ok; 1071 dev->zlp = link->is_zlp_ok;
1072 dev->no_skb_reserve = link->no_skb_reserve;
1066 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult)); 1073 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
1067 1074
1068 dev->header_len = link->header_len; 1075 dev->header_len = link->header_len;
diff --git a/drivers/usb/gadget/function/u_ether.h b/drivers/usb/gadget/function/u_ether.h
index c77145bd6b5b..81d94a7ae4b4 100644
--- a/drivers/usb/gadget/function/u_ether.h
+++ b/drivers/usb/gadget/function/u_ether.h
@@ -64,6 +64,7 @@ struct gether {
64 struct usb_ep *out_ep; 64 struct usb_ep *out_ep;
65 65
66 bool is_zlp_ok; 66 bool is_zlp_ok;
67 bool no_skb_reserve;
67 68
68 u16 cdc_filter; 69 u16 cdc_filter;
69 70
diff --git a/drivers/usb/gadget/legacy/gmidi.c b/drivers/usb/gadget/legacy/gmidi.c
index fc2ac150f5ff..0bf39c3ccdb1 100644
--- a/drivers/usb/gadget/legacy/gmidi.c
+++ b/drivers/usb/gadget/legacy/gmidi.c
@@ -47,7 +47,7 @@ static char *id = SNDRV_DEFAULT_STR1;
47module_param(id, charp, S_IRUGO); 47module_param(id, charp, S_IRUGO);
48MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter."); 48MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
49 49
50static unsigned int buflen = 256; 50static unsigned int buflen = 512;
51module_param(buflen, uint, S_IRUGO); 51module_param(buflen, uint, S_IRUGO);
52MODULE_PARM_DESC(buflen, "MIDI buffer length"); 52MODULE_PARM_DESC(buflen, "MIDI buffer length");
53 53
diff --git a/drivers/usb/gadget/u_f.c b/drivers/usb/gadget/u_f.c
index 4bc7eea8bfc8..18839732c840 100644
--- a/drivers/usb/gadget/u_f.c
+++ b/drivers/usb/gadget/u_f.c
@@ -12,14 +12,16 @@
12 */ 12 */
13 13
14#include "u_f.h" 14#include "u_f.h"
15#include <linux/usb/ch9.h>
15 16
16struct usb_request *alloc_ep_req(struct usb_ep *ep, int len, int default_len) 17struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len)
17{ 18{
18 struct usb_request *req; 19 struct usb_request *req;
19 20
20 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 21 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
21 if (req) { 22 if (req) {
22 req->length = len ?: default_len; 23 req->length = usb_endpoint_dir_out(ep->desc) ?
24 usb_ep_align(ep, len) : len;
23 req->buf = kmalloc(req->length, GFP_ATOMIC); 25 req->buf = kmalloc(req->length, GFP_ATOMIC);
24 if (!req->buf) { 26 if (!req->buf) {
25 usb_ep_free_request(ep, req); 27 usb_ep_free_request(ep, req);
diff --git a/drivers/usb/gadget/u_f.h b/drivers/usb/gadget/u_f.h
index 4247cc098a89..7d53a4773d1a 100644
--- a/drivers/usb/gadget/u_f.h
+++ b/drivers/usb/gadget/u_f.h
@@ -47,8 +47,21 @@
47struct usb_ep; 47struct usb_ep;
48struct usb_request; 48struct usb_request;
49 49
50/* Requests allocated via alloc_ep_req() must be freed by free_ep_req(). */ 50/**
51struct usb_request *alloc_ep_req(struct usb_ep *ep, int len, int default_len); 51 * alloc_ep_req - returns a usb_request allocated by the gadget driver and
52 * allocates the request's buffer.
53 *
54 * @ep: the endpoint to allocate a usb_request
55 * @len: usb_requests's buffer suggested size
56 *
57 * In case @ep direction is OUT, the @len will be aligned to ep's
58 * wMaxPacketSize. In order to avoid memory leaks or drops, *always* use
59 * usb_requests's length (req->length) to refer to the allocated buffer size.
60 * Requests allocated via alloc_ep_req() *must* be freed by free_ep_req().
61 */
62struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
63
64/* Frees a usb_request previously allocated by alloc_ep_req() */
52static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req) 65static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
53{ 66{
54 kfree(req->buf); 67 kfree(req->buf);
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 40c04bb25f2f..9483489080f6 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -107,10 +107,8 @@ int usb_ep_enable(struct usb_ep *ep)
107 goto out; 107 goto out;
108 108
109 ret = ep->ops->enable(ep, ep->desc); 109 ret = ep->ops->enable(ep, ep->desc);
110 if (ret) { 110 if (ret)
111 ret = ret;
112 goto out; 111 goto out;
113 }
114 112
115 ep->enabled = true; 113 ep->enabled = true;
116 114
diff --git a/drivers/usb/gadget/udc/goku_udc.c b/drivers/usb/gadget/udc/goku_udc.c
index 1400415fe67a..5107987bd353 100644
--- a/drivers/usb/gadget/udc/goku_udc.c
+++ b/drivers/usb/gadget/udc/goku_udc.c
@@ -1838,6 +1838,8 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1838err: 1838err:
1839 if (dev) 1839 if (dev)
1840 goku_remove (pdev); 1840 goku_remove (pdev);
1841 /* gadget_release is not registered yet, kfree explicitly */
1842 kfree(dev);
1841 return retval; 1843 return retval;
1842} 1844}
1843 1845
diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
index 614ab951a4ae..61c938c36d88 100644
--- a/drivers/usb/gadget/udc/net2280.c
+++ b/drivers/usb/gadget/udc/net2280.c
@@ -589,7 +589,7 @@ static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
589 589
590 ep = container_of(_ep, struct net2280_ep, ep); 590 ep = container_of(_ep, struct net2280_ep, ep);
591 if (!_ep || !_req) { 591 if (!_ep || !_req) {
592 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n", 592 dev_err(&ep->dev->pdev->dev, "%s: Invalid ep=%p or req=%p\n",
593 __func__, _ep, _req); 593 __func__, _ep, _req);
594 return; 594 return;
595 } 595 }
@@ -1137,8 +1137,10 @@ dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1137 done(ep, req, status); 1137 done(ep, req, status);
1138} 1138}
1139 1139
1140static void scan_dma_completions(struct net2280_ep *ep) 1140static int scan_dma_completions(struct net2280_ep *ep)
1141{ 1141{
1142 int num_completed = 0;
1143
1142 /* only look at descriptors that were "naturally" retired, 1144 /* only look at descriptors that were "naturally" retired,
1143 * so fifo and list head state won't matter 1145 * so fifo and list head state won't matter
1144 */ 1146 */
@@ -1166,6 +1168,7 @@ static void scan_dma_completions(struct net2280_ep *ep)
1166 break; 1168 break;
1167 /* single transfer mode */ 1169 /* single transfer mode */
1168 dma_done(ep, req, tmp, 0); 1170 dma_done(ep, req, tmp, 0);
1171 num_completed++;
1169 break; 1172 break;
1170 } else if (!ep->is_in && 1173 } else if (!ep->is_in &&
1171 (req->req.length % ep->ep.maxpacket) && 1174 (req->req.length % ep->ep.maxpacket) &&
@@ -1194,7 +1197,10 @@ static void scan_dma_completions(struct net2280_ep *ep)
1194 } 1197 }
1195 } 1198 }
1196 dma_done(ep, req, tmp, 0); 1199 dma_done(ep, req, tmp, 0);
1200 num_completed++;
1197 } 1201 }
1202
1203 return num_completed;
1198} 1204}
1199 1205
1200static void restart_dma(struct net2280_ep *ep) 1206static void restart_dma(struct net2280_ep *ep)
@@ -1567,6 +1573,44 @@ static struct usb_ep *net2280_match_ep(struct usb_gadget *_gadget,
1567 return ep; 1573 return ep;
1568 } 1574 }
1569 1575
1576 /* USB3380: Only first four endpoints have DMA channels. Allocate
1577 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1578 * endpoints use DMA hw endpoints.
1579 */
1580 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1581 usb_endpoint_dir_in(desc)) {
1582 ep = gadget_find_ep_by_name(_gadget, "ep2in");
1583 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1584 return ep;
1585 ep = gadget_find_ep_by_name(_gadget, "ep4in");
1586 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1587 return ep;
1588 } else if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1589 !usb_endpoint_dir_in(desc)) {
1590 ep = gadget_find_ep_by_name(_gadget, "ep1out");
1591 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1592 return ep;
1593 ep = gadget_find_ep_by_name(_gadget, "ep3out");
1594 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1595 return ep;
1596 } else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1597 usb_endpoint_dir_in(desc)) {
1598 ep = gadget_find_ep_by_name(_gadget, "ep1in");
1599 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1600 return ep;
1601 ep = gadget_find_ep_by_name(_gadget, "ep3in");
1602 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1603 return ep;
1604 } else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1605 !usb_endpoint_dir_in(desc)) {
1606 ep = gadget_find_ep_by_name(_gadget, "ep2out");
1607 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1608 return ep;
1609 ep = gadget_find_ep_by_name(_gadget, "ep4out");
1610 if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1611 return ep;
1612 }
1613
1570 /* USB3380: use same address for usb and hardware endpoints */ 1614 /* USB3380: use same address for usb and hardware endpoints */
1571 snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc), 1615 snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
1572 usb_endpoint_dir_in(desc) ? "in" : "out"); 1616 usb_endpoint_dir_in(desc) ? "in" : "out");
@@ -2547,8 +2591,11 @@ static void handle_ep_small(struct net2280_ep *ep)
2547 /* manual DMA queue advance after short OUT */ 2591 /* manual DMA queue advance after short OUT */
2548 if (likely(ep->dma)) { 2592 if (likely(ep->dma)) {
2549 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) { 2593 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2550 u32 count; 2594 struct net2280_request *stuck_req = NULL;
2551 int stopped = ep->stopped; 2595 int stopped = ep->stopped;
2596 int num_completed;
2597 int stuck = 0;
2598 u32 count;
2552 2599
2553 /* TRANSFERRED works around OUT_DONE erratum 0112. 2600 /* TRANSFERRED works around OUT_DONE erratum 0112.
2554 * we expect (N <= maxpacket) bytes; host wrote M. 2601 * we expect (N <= maxpacket) bytes; host wrote M.
@@ -2560,7 +2607,7 @@ static void handle_ep_small(struct net2280_ep *ep)
2560 /* any preceding dma transfers must finish. 2607 /* any preceding dma transfers must finish.
2561 * dma handles (M >= N), may empty the queue 2608 * dma handles (M >= N), may empty the queue
2562 */ 2609 */
2563 scan_dma_completions(ep); 2610 num_completed = scan_dma_completions(ep);
2564 if (unlikely(list_empty(&ep->queue) || 2611 if (unlikely(list_empty(&ep->queue) ||
2565 ep->out_overflow)) { 2612 ep->out_overflow)) {
2566 req = NULL; 2613 req = NULL;
@@ -2580,6 +2627,31 @@ static void handle_ep_small(struct net2280_ep *ep)
2580 req = NULL; 2627 req = NULL;
2581 break; 2628 break;
2582 } 2629 }
2630
2631 /* Escape loop if no dma transfers completed
2632 * after few retries.
2633 */
2634 if (num_completed == 0) {
2635 if (stuck_req == req &&
2636 readl(&ep->dma->dmadesc) !=
2637 req->td_dma && stuck++ > 5) {
2638 count = readl(
2639 &ep->dma->dmacount);
2640 count &= DMA_BYTE_COUNT_MASK;
2641 req = NULL;
2642 ep_dbg(ep->dev, "%s escape stuck %d, count %u\n",
2643 ep->ep.name, stuck,
2644 count);
2645 break;
2646 } else if (stuck_req != req) {
2647 stuck_req = req;
2648 stuck = 0;
2649 }
2650 } else {
2651 stuck_req = NULL;
2652 stuck = 0;
2653 }
2654
2583 udelay(1); 2655 udelay(1);
2584 } 2656 }
2585 2657
diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index ad140aa00132..7fa60f5b7ae4 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -33,6 +33,7 @@
33#include <linux/usb.h> 33#include <linux/usb.h>
34#include <linux/usb/ch9.h> 34#include <linux/usb/ch9.h>
35#include <linux/usb/gadget.h> 35#include <linux/usb/gadget.h>
36#include <linux/usb/phy.h>
36 37
37#include "pxa27x_udc.h" 38#include "pxa27x_udc.h"
38 39
@@ -1655,6 +1656,37 @@ static int pxa_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1655 return -EOPNOTSUPP; 1656 return -EOPNOTSUPP;
1656} 1657}
1657 1658
1659/**
1660 * pxa_udc_phy_event - Called by phy upon VBus event
1661 * @nb: notifier block
1662 * @action: phy action, is vbus connect or disconnect
1663 * @data: the usb_gadget structure in pxa_udc
1664 *
1665 * Called by the USB Phy when a cable connect or disconnect is sensed.
1666 *
1667 * Returns 0
1668 */
1669static int pxa_udc_phy_event(struct notifier_block *nb, unsigned long action,
1670 void *data)
1671{
1672 struct usb_gadget *gadget = data;
1673
1674 switch (action) {
1675 case USB_EVENT_VBUS:
1676 usb_gadget_vbus_connect(gadget);
1677 return NOTIFY_OK;
1678 case USB_EVENT_NONE:
1679 usb_gadget_vbus_disconnect(gadget);
1680 return NOTIFY_OK;
1681 default:
1682 return NOTIFY_DONE;
1683 }
1684}
1685
1686static struct notifier_block pxa27x_udc_phy = {
1687 .notifier_call = pxa_udc_phy_event,
1688};
1689
1658static int pxa27x_udc_start(struct usb_gadget *g, 1690static int pxa27x_udc_start(struct usb_gadget *g,
1659 struct usb_gadget_driver *driver); 1691 struct usb_gadget_driver *driver);
1660static int pxa27x_udc_stop(struct usb_gadget *g); 1692static int pxa27x_udc_stop(struct usb_gadget *g);
@@ -2432,7 +2464,14 @@ static int pxa_udc_probe(struct platform_device *pdev)
2432 return udc->irq; 2464 return udc->irq;
2433 2465
2434 udc->dev = &pdev->dev; 2466 udc->dev = &pdev->dev;
2435 udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2); 2467 if (of_have_populated_dt()) {
2468 udc->transceiver =
2469 devm_usb_get_phy_by_phandle(udc->dev, "phys", 0);
2470 if (IS_ERR(udc->transceiver))
2471 return PTR_ERR(udc->transceiver);
2472 } else {
2473 udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
2474 }
2436 2475
2437 if (IS_ERR(udc->gpiod)) { 2476 if (IS_ERR(udc->gpiod)) {
2438 dev_err(&pdev->dev, "Couldn't find or request D+ gpio : %ld\n", 2477 dev_err(&pdev->dev, "Couldn't find or request D+ gpio : %ld\n",
@@ -2465,14 +2504,20 @@ static int pxa_udc_probe(struct platform_device *pdev)
2465 goto err; 2504 goto err;
2466 } 2505 }
2467 2506
2507 if (!IS_ERR_OR_NULL(udc->transceiver))
2508 usb_register_notifier(udc->transceiver, &pxa27x_udc_phy);
2468 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2509 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2469 if (retval) 2510 if (retval)
2470 goto err; 2511 goto err_add_gadget;
2471 2512
2472 pxa_init_debugfs(udc); 2513 pxa_init_debugfs(udc);
2473 if (should_enable_udc(udc)) 2514 if (should_enable_udc(udc))
2474 udc_enable(udc); 2515 udc_enable(udc);
2475 return 0; 2516 return 0;
2517
2518err_add_gadget:
2519 if (!IS_ERR_OR_NULL(udc->transceiver))
2520 usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
2476err: 2521err:
2477 clk_unprepare(udc->clk); 2522 clk_unprepare(udc->clk);
2478 return retval; 2523 return retval;
@@ -2489,6 +2534,8 @@ static int pxa_udc_remove(struct platform_device *_dev)
2489 usb_del_gadget_udc(&udc->gadget); 2534 usb_del_gadget_udc(&udc->gadget);
2490 pxa_cleanup_debugfs(udc); 2535 pxa_cleanup_debugfs(udc);
2491 2536
2537 if (!IS_ERR_OR_NULL(udc->transceiver))
2538 usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
2492 usb_put_phy(udc->transceiver); 2539 usb_put_phy(udc->transceiver);
2493 2540
2494 udc->transceiver = NULL; 2541 udc->transceiver = NULL;
diff --git a/drivers/usb/phy/phy-ab8500-usb.c b/drivers/usb/phy/phy-ab8500-usb.c
index 0c912d3950a5..a03caf4b1327 100644
--- a/drivers/usb/phy/phy-ab8500-usb.c
+++ b/drivers/usb/phy/phy-ab8500-usb.c
@@ -1248,7 +1248,7 @@ static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
1248 err = abx500_set_register_interruptible(ab->dev, 1248 err = abx500_set_register_interruptible(ab->dev,
1249 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78); 1249 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
1250 if (err < 0) 1250 if (err < 0)
1251 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n", 1251 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1252 err); 1252 err);
1253 1253
1254 /* Switch to normal mode/disable Bank 0x12 access */ 1254 /* Switch to normal mode/disable Bank 0x12 access */
@@ -1290,7 +1290,7 @@ static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
1290 0xFC, 0x80); 1290 0xFC, 0x80);
1291 1291
1292 if (err < 0) 1292 if (err < 0)
1293 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n", 1293 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1294 err); 1294 err);
1295 1295
1296 /* Switch to normal mode/disable Bank 0x12 access */ 1296 /* Switch to normal mode/disable Bank 0x12 access */
@@ -1321,7 +1321,7 @@ static void ab8500_usb_set_ab8540_tuning_values(struct ab8500_usb *ab)
1321 err = abx500_set_register_interruptible(ab->dev, 1321 err = abx500_set_register_interruptible(ab->dev,
1322 AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90); 1322 AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90);
1323 if (err < 0) 1323 if (err < 0)
1324 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester ret=%d\n", 1324 dev_err(ab->dev, "Failed to set PHY_TUNE3 register ret=%d\n",
1325 err); 1325 err);
1326} 1326}
1327 1327
@@ -1351,7 +1351,7 @@ static void ab8500_usb_set_ab9540_tuning_values(struct ab8500_usb *ab)
1351 err = abx500_set_register_interruptible(ab->dev, 1351 err = abx500_set_register_interruptible(ab->dev,
1352 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x80); 1352 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x80);
1353 if (err < 0) 1353 if (err < 0)
1354 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n", 1354 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1355 err); 1355 err);
1356 1356
1357 /* Switch to normal mode/disable Bank 0x12 access */ 1357 /* Switch to normal mode/disable Bank 0x12 access */
diff --git a/drivers/usb/phy/phy-generic.c b/drivers/usb/phy/phy-generic.c
index 427efb5eebae..8311ba2968cd 100644
--- a/drivers/usb/phy/phy-generic.c
+++ b/drivers/usb/phy/phy-generic.c
@@ -118,8 +118,6 @@ static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
118 status = USB_EVENT_VBUS; 118 status = USB_EVENT_VBUS;
119 otg->state = OTG_STATE_B_PERIPHERAL; 119 otg->state = OTG_STATE_B_PERIPHERAL;
120 nop->phy.last_event = status; 120 nop->phy.last_event = status;
121 if (otg->gadget)
122 usb_gadget_vbus_connect(otg->gadget);
123 121
124 /* drawing a "unit load" is *always* OK, except for OTG */ 122 /* drawing a "unit load" is *always* OK, except for OTG */
125 nop_set_vbus_draw(nop, 100); 123 nop_set_vbus_draw(nop, 100);
@@ -129,8 +127,6 @@ static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
129 } else { 127 } else {
130 nop_set_vbus_draw(nop, 0); 128 nop_set_vbus_draw(nop, 0);
131 129
132 if (otg->gadget)
133 usb_gadget_vbus_disconnect(otg->gadget);
134 status = USB_EVENT_NONE; 130 status = USB_EVENT_NONE;
135 otg->state = OTG_STATE_B_IDLE; 131 otg->state = OTG_STATE_B_IDLE;
136 nop->phy.last_event = status; 132 nop->phy.last_event = status;
@@ -191,7 +187,8 @@ static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
191 187
192 otg->gadget = gadget; 188 otg->gadget = gadget;
193 if (otg->state == OTG_STATE_B_PERIPHERAL) 189 if (otg->state == OTG_STATE_B_PERIPHERAL)
194 usb_gadget_vbus_connect(gadget); 190 atomic_notifier_call_chain(&otg->usb_phy->notifier,
191 USB_EVENT_VBUS, otg->gadget);
195 else 192 else
196 otg->state = OTG_STATE_B_IDLE; 193 otg->state = OTG_STATE_B_IDLE;
197 return 0; 194 return 0;
@@ -326,6 +323,8 @@ static int usb_phy_generic_probe(struct platform_device *pdev)
326 gpiod_to_irq(nop->gpiod_vbus), err); 323 gpiod_to_irq(nop->gpiod_vbus), err);
327 return err; 324 return err;
328 } 325 }
326 nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ?
327 OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE;
329 } 328 }
330 329
331 nop->phy.init = usb_gen_phy_init; 330 nop->phy.init = usb_gen_phy_init;
diff --git a/drivers/usb/phy/phy-mxs-usb.c b/drivers/usb/phy/phy-mxs-usb.c
index 00bfea01be65..0e2f1a36d315 100644
--- a/drivers/usb/phy/phy-mxs-usb.c
+++ b/drivers/usb/phy/phy-mxs-usb.c
@@ -27,6 +27,7 @@
27#define DRIVER_NAME "mxs_phy" 27#define DRIVER_NAME "mxs_phy"
28 28
29#define HW_USBPHY_PWD 0x00 29#define HW_USBPHY_PWD 0x00
30#define HW_USBPHY_TX 0x10
30#define HW_USBPHY_CTRL 0x30 31#define HW_USBPHY_CTRL 0x30
31#define HW_USBPHY_CTRL_SET 0x34 32#define HW_USBPHY_CTRL_SET 0x34
32#define HW_USBPHY_CTRL_CLR 0x38 33#define HW_USBPHY_CTRL_CLR 0x38
@@ -38,6 +39,10 @@
38#define HW_USBPHY_IP_SET 0x94 39#define HW_USBPHY_IP_SET 0x94
39#define HW_USBPHY_IP_CLR 0x98 40#define HW_USBPHY_IP_CLR 0x98
40 41
42#define GM_USBPHY_TX_TXCAL45DP(x) (((x) & 0xf) << 16)
43#define GM_USBPHY_TX_TXCAL45DN(x) (((x) & 0xf) << 8)
44#define GM_USBPHY_TX_D_CAL(x) (((x) & 0xf) << 0)
45
41#define BM_USBPHY_CTRL_SFTRST BIT(31) 46#define BM_USBPHY_CTRL_SFTRST BIT(31)
42#define BM_USBPHY_CTRL_CLKGATE BIT(30) 47#define BM_USBPHY_CTRL_CLKGATE BIT(30)
43#define BM_USBPHY_CTRL_OTG_ID_VALUE BIT(27) 48#define BM_USBPHY_CTRL_OTG_ID_VALUE BIT(27)
@@ -115,6 +120,12 @@
115 */ 120 */
116#define MXS_PHY_NEED_IP_FIX BIT(3) 121#define MXS_PHY_NEED_IP_FIX BIT(3)
117 122
123/* Minimum and maximum values for device tree entries */
124#define MXS_PHY_TX_CAL45_MIN 30
125#define MXS_PHY_TX_CAL45_MAX 55
126#define MXS_PHY_TX_D_CAL_MIN 79
127#define MXS_PHY_TX_D_CAL_MAX 119
128
118struct mxs_phy_data { 129struct mxs_phy_data {
119 unsigned int flags; 130 unsigned int flags;
120}; 131};
@@ -164,6 +175,8 @@ struct mxs_phy {
164 const struct mxs_phy_data *data; 175 const struct mxs_phy_data *data;
165 struct regmap *regmap_anatop; 176 struct regmap *regmap_anatop;
166 int port_id; 177 int port_id;
178 u32 tx_reg_set;
179 u32 tx_reg_mask;
167}; 180};
168 181
169static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy) 182static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
@@ -185,6 +198,20 @@ static void mxs_phy_clock_switch_delay(void)
185 usleep_range(300, 400); 198 usleep_range(300, 400);
186} 199}
187 200
201static void mxs_phy_tx_init(struct mxs_phy *mxs_phy)
202{
203 void __iomem *base = mxs_phy->phy.io_priv;
204 u32 phytx;
205
206 /* Update TX register if there is anything to write */
207 if (mxs_phy->tx_reg_mask) {
208 phytx = readl(base + HW_USBPHY_TX);
209 phytx &= ~mxs_phy->tx_reg_mask;
210 phytx |= mxs_phy->tx_reg_set;
211 writel(phytx, base + HW_USBPHY_TX);
212 }
213}
214
188static int mxs_phy_hw_init(struct mxs_phy *mxs_phy) 215static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
189{ 216{
190 int ret; 217 int ret;
@@ -214,6 +241,8 @@ static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
214 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX) 241 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
215 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET); 242 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
216 243
244 mxs_phy_tx_init(mxs_phy);
245
217 return 0; 246 return 0;
218} 247}
219 248
@@ -459,6 +488,7 @@ static int mxs_phy_probe(struct platform_device *pdev)
459 int ret; 488 int ret;
460 const struct of_device_id *of_id; 489 const struct of_device_id *of_id;
461 struct device_node *np = pdev->dev.of_node; 490 struct device_node *np = pdev->dev.of_node;
491 u32 val;
462 492
463 of_id = of_match_device(mxs_phy_dt_ids, &pdev->dev); 493 of_id = of_match_device(mxs_phy_dt_ids, &pdev->dev);
464 if (!of_id) 494 if (!of_id)
@@ -491,6 +521,37 @@ static int mxs_phy_probe(struct platform_device *pdev)
491 } 521 }
492 } 522 }
493 523
524 /* Precompute which bits of the TX register are to be updated, if any */
525 if (!of_property_read_u32(np, "fsl,tx-cal-45-dn-ohms", &val) &&
526 val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
527 /* Scale to a 4-bit value */
528 val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
529 / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
530 mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DN(~0);
531 mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DN(val);
532 }
533
534 if (!of_property_read_u32(np, "fsl,tx-cal-45-dp-ohms", &val) &&
535 val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
536 /* Scale to a 4-bit value. */
537 val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
538 / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
539 mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DP(~0);
540 mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DP(val);
541 }
542
543 if (!of_property_read_u32(np, "fsl,tx-d-cal", &val) &&
544 val >= MXS_PHY_TX_D_CAL_MIN && val <= MXS_PHY_TX_D_CAL_MAX) {
545 /* Scale to a 4-bit value. Round up the values and heavily
546 * weight the rounding by adding 2/3 of the denominator.
547 */
548 val = ((MXS_PHY_TX_D_CAL_MAX - val) * 0xF
549 + (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN) * 2/3)
550 / (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN);
551 mxs_phy->tx_reg_mask |= GM_USBPHY_TX_D_CAL(~0);
552 mxs_phy->tx_reg_set |= GM_USBPHY_TX_D_CAL(val);
553 }
554
494 ret = of_alias_get_id(np, "usbphy"); 555 ret = of_alias_get_id(np, "usbphy");
495 if (ret < 0) 556 if (ret < 0)
496 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret); 557 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
diff --git a/drivers/usb/renesas_usbhs/common.c b/drivers/usb/renesas_usbhs/common.c
index ac67bab9124c..012a37aa3e0d 100644
--- a/drivers/usb/renesas_usbhs/common.c
+++ b/drivers/usb/renesas_usbhs/common.c
@@ -482,6 +482,10 @@ static const struct of_device_id usbhs_of_match[] = {
482 .data = (void *)USBHS_TYPE_RCAR_GEN3, 482 .data = (void *)USBHS_TYPE_RCAR_GEN3,
483 }, 483 },
484 { 484 {
485 .compatible = "renesas,usbhs-r8a7796",
486 .data = (void *)USBHS_TYPE_RCAR_GEN3,
487 },
488 {
485 .compatible = "renesas,rcar-gen2-usbhs", 489 .compatible = "renesas,rcar-gen2-usbhs",
486 .data = (void *)USBHS_TYPE_RCAR_GEN2, 490 .data = (void *)USBHS_TYPE_RCAR_GEN2,
487 }, 491 },
diff --git a/drivers/usb/renesas_usbhs/mod_gadget.c b/drivers/usb/renesas_usbhs/mod_gadget.c
index 86b37a8eedd4..5bc7a6138855 100644
--- a/drivers/usb/renesas_usbhs/mod_gadget.c
+++ b/drivers/usb/renesas_usbhs/mod_gadget.c
@@ -1102,6 +1102,8 @@ int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1102 gpriv->gadget.name = "renesas_usbhs_udc"; 1102 gpriv->gadget.name = "renesas_usbhs_udc";
1103 gpriv->gadget.ops = &usbhsg_gadget_ops; 1103 gpriv->gadget.ops = &usbhsg_gadget_ops;
1104 gpriv->gadget.max_speed = USB_SPEED_HIGH; 1104 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1105 gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv,
1106 has_usb_dmac);
1105 1107
1106 INIT_LIST_HEAD(&gpriv->gadget.ep_list); 1108 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1107 1109
diff --git a/include/linux/ulpi/driver.h b/include/linux/ulpi/driver.h
index 80b36ca12e80..a7af21a55248 100644
--- a/include/linux/ulpi/driver.h
+++ b/include/linux/ulpi/driver.h
@@ -15,7 +15,7 @@ struct ulpi_ops;
15 */ 15 */
16struct ulpi { 16struct ulpi {
17 struct ulpi_device_id id; 17 struct ulpi_device_id id;
18 struct ulpi_ops *ops; 18 const struct ulpi_ops *ops;
19 struct device dev; 19 struct device dev;
20}; 20};
21 21
diff --git a/include/linux/ulpi/interface.h b/include/linux/ulpi/interface.h
index 4de8ab491038..a2011a919eb6 100644
--- a/include/linux/ulpi/interface.h
+++ b/include/linux/ulpi/interface.h
@@ -4,20 +4,19 @@
4#include <linux/types.h> 4#include <linux/types.h>
5 5
6struct ulpi; 6struct ulpi;
7struct device;
7 8
8/** 9/**
9 * struct ulpi_ops - ULPI register access 10 * struct ulpi_ops - ULPI register access
10 * @dev: the interface provider
11 * @read: read operation for ULPI register access 11 * @read: read operation for ULPI register access
12 * @write: write operation for ULPI register access 12 * @write: write operation for ULPI register access
13 */ 13 */
14struct ulpi_ops { 14struct ulpi_ops {
15 struct device *dev; 15 int (*read)(struct device *dev, u8 addr);
16 int (*read)(struct ulpi_ops *ops, u8 addr); 16 int (*write)(struct device *dev, u8 addr, u8 val);
17 int (*write)(struct ulpi_ops *ops, u8 addr, u8 val);
18}; 17};
19 18
20struct ulpi *ulpi_register_interface(struct device *, struct ulpi_ops *); 19struct ulpi *ulpi_register_interface(struct device *, const struct ulpi_ops *);
21void ulpi_unregister_interface(struct ulpi *); 20void ulpi_unregister_interface(struct ulpi *);
22 21
23#endif /* __LINUX_ULPI_INTERFACE_H */ 22#endif /* __LINUX_ULPI_INTERFACE_H */
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 2b81b24eb5aa..4616a49a1c2e 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -220,7 +220,8 @@ struct usb_function {
220 int (*setup)(struct usb_function *, 220 int (*setup)(struct usb_function *,
221 const struct usb_ctrlrequest *); 221 const struct usb_ctrlrequest *);
222 bool (*req_match)(struct usb_function *, 222 bool (*req_match)(struct usb_function *,
223 const struct usb_ctrlrequest *); 223 const struct usb_ctrlrequest *,
224 bool config0);
224 void (*suspend)(struct usb_function *); 225 void (*suspend)(struct usb_function *);
225 void (*resume)(struct usb_function *); 226 void (*resume)(struct usb_function *);
226 227
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 612dbdfa388e..8e81f9eb95e4 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -346,6 +346,8 @@ struct usb_gadget_ops {
346 * or B-Peripheral wants to take host role. 346 * or B-Peripheral wants to take host role.
347 * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to 347 * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
348 * MaxPacketSize. 348 * MaxPacketSize.
349 * @quirk_avoids_skb_reserve: udc/platform wants to avoid skb_reserve() in
350 * u_ether.c to improve performance.
349 * @is_selfpowered: if the gadget is self-powered. 351 * @is_selfpowered: if the gadget is self-powered.
350 * @deactivated: True if gadget is deactivated - in deactivated state it cannot 352 * @deactivated: True if gadget is deactivated - in deactivated state it cannot
351 * be connected. 353 * be connected.
@@ -398,6 +400,7 @@ struct usb_gadget {
398 unsigned quirk_altset_not_supp:1; 400 unsigned quirk_altset_not_supp:1;
399 unsigned quirk_stall_not_supp:1; 401 unsigned quirk_stall_not_supp:1;
400 unsigned quirk_zlp_not_supp:1; 402 unsigned quirk_zlp_not_supp:1;
403 unsigned quirk_avoids_skb_reserve:1;
401 unsigned is_selfpowered:1; 404 unsigned is_selfpowered:1;
402 unsigned deactivated:1; 405 unsigned deactivated:1;
403 unsigned connected:1; 406 unsigned connected:1;
@@ -418,8 +421,20 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
418 list_for_each_entry(tmp, &(gadget)->ep_list, ep_list) 421 list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
419 422
420/** 423/**
424 * usb_ep_align - returns @len aligned to ep's maxpacketsize.
425 * @ep: the endpoint whose maxpacketsize is used to align @len
426 * @len: buffer size's length to align to @ep's maxpacketsize
427 *
428 * This helper is used to align buffer's size to an ep's maxpacketsize.
429 */
430static inline size_t usb_ep_align(struct usb_ep *ep, size_t len)
431{
432 return round_up(len, (size_t)le16_to_cpu(ep->desc->wMaxPacketSize));
433}
434
435/**
421 * usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget 436 * usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget
422 * requires quirk_ep_out_aligned_size, otherwise reguens len. 437 * requires quirk_ep_out_aligned_size, otherwise returns len.
423 * @g: controller to check for quirk 438 * @g: controller to check for quirk
424 * @ep: the endpoint whose maxpacketsize is used to align @len 439 * @ep: the endpoint whose maxpacketsize is used to align @len
425 * @len: buffer size's length to align to @ep's maxpacketsize 440 * @len: buffer size's length to align to @ep's maxpacketsize
@@ -430,8 +445,7 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
430static inline size_t 445static inline size_t
431usb_ep_align_maybe(struct usb_gadget *g, struct usb_ep *ep, size_t len) 446usb_ep_align_maybe(struct usb_gadget *g, struct usb_ep *ep, size_t len)
432{ 447{
433 return !g->quirk_ep_out_aligned_size ? len : 448 return g->quirk_ep_out_aligned_size ? usb_ep_align(ep, len) : len;
434 round_up(len, (size_t)ep->desc->wMaxPacketSize);
435} 449}
436 450
437/** 451/**
@@ -463,6 +477,16 @@ static inline int gadget_is_zlp_supported(struct usb_gadget *g)
463} 477}
464 478
465/** 479/**
480 * gadget_avoids_skb_reserve - return true iff the hardware would like to avoid
481 * skb_reserve to improve performance.
482 * @g: controller to check for quirk
483 */
484static inline int gadget_avoids_skb_reserve(struct usb_gadget *g)
485{
486 return g->quirk_avoids_skb_reserve;
487}
488
489/**
466 * gadget_is_dualspeed - return true iff the hardware handles high speed 490 * gadget_is_dualspeed - return true iff the hardware handles high speed
467 * @g: controller that might support both high and full speeds 491 * @g: controller that might support both high and full speeds
468 */ 492 */
diff --git a/include/uapi/linux/usb/functionfs.h b/include/uapi/linux/usb/functionfs.h
index 108dd7997014..acc63697a0cc 100644
--- a/include/uapi/linux/usb/functionfs.h
+++ b/include/uapi/linux/usb/functionfs.h
@@ -21,6 +21,8 @@ enum functionfs_flags {
21 FUNCTIONFS_HAS_MS_OS_DESC = 8, 21 FUNCTIONFS_HAS_MS_OS_DESC = 8,
22 FUNCTIONFS_VIRTUAL_ADDR = 16, 22 FUNCTIONFS_VIRTUAL_ADDR = 16,
23 FUNCTIONFS_EVENTFD = 32, 23 FUNCTIONFS_EVENTFD = 32,
24 FUNCTIONFS_ALL_CTRL_RECIP = 64,
25 FUNCTIONFS_CONFIG0_SETUP = 128,
24}; 26};
25 27
26/* Descriptor of an non-audio endpoint */ 28/* Descriptor of an non-audio endpoint */