aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Makefile2
-rw-r--r--drivers/usb/atm/ueagle-atm.c4
-rw-r--r--drivers/usb/c67x00/c67x00-hcd.c2
-rw-r--r--drivers/usb/chipidea/Kconfig11
-rw-r--r--drivers/usb/chipidea/Makefile9
-rw-r--r--drivers/usb/chipidea/bits.h11
-rw-r--r--drivers/usb/chipidea/ci.h69
-rw-r--r--drivers/usb/chipidea/ci_hdrc_imx.c153
-rw-r--r--drivers/usb/chipidea/ci_hdrc_imx.h1
-rw-r--r--drivers/usb/chipidea/ci_hdrc_pci.c31
-rw-r--r--drivers/usb/chipidea/ci_hdrc_zevio.c2
-rw-r--r--drivers/usb/chipidea/core.c185
-rw-r--r--drivers/usb/chipidea/debug.c4
-rw-r--r--drivers/usb/chipidea/host.c74
-rw-r--r--drivers/usb/chipidea/otg.c2
-rw-r--r--drivers/usb/chipidea/otg_fsm.c389
-rw-r--r--drivers/usb/chipidea/otg_fsm.h27
-rw-r--r--drivers/usb/chipidea/udc.c52
-rw-r--r--drivers/usb/chipidea/usbmisc_imx.c129
-rw-r--r--drivers/usb/class/cdc-acm.c21
-rw-r--r--drivers/usb/class/cdc-wdm.c47
-rw-r--r--drivers/usb/common/usb-otg-fsm.c4
-rw-r--r--drivers/usb/core/devio.c2
-rw-r--r--drivers/usb/core/hub.c4
-rw-r--r--drivers/usb/core/usb.c36
-rw-r--r--drivers/usb/dwc2/Kconfig8
-rw-r--r--drivers/usb/dwc2/Makefile6
-rw-r--r--drivers/usb/dwc2/core.h2
-rw-r--r--drivers/usb/dwc2/core_intr.c3
-rw-r--r--drivers/usb/dwc2/hcd.c12
-rw-r--r--drivers/usb/dwc2/pci.c160
-rw-r--r--drivers/usb/dwc2/platform.c29
-rw-r--r--drivers/usb/dwc3/Kconfig7
-rw-r--r--drivers/usb/dwc3/core.c59
-rw-r--r--drivers/usb/dwc3/core.h2
-rw-r--r--drivers/usb/dwc3/dwc3-omap.c11
-rw-r--r--drivers/usb/dwc3/dwc3-pci.c2
-rw-r--r--drivers/usb/dwc3/gadget.c37
-rw-r--r--drivers/usb/dwc3/host.c4
-rw-r--r--drivers/usb/dwc3/platform_data.h1
-rw-r--r--drivers/usb/gadget/Kconfig17
-rw-r--r--drivers/usb/gadget/composite.c17
-rw-r--r--drivers/usb/gadget/function/Makefile2
-rw-r--r--drivers/usb/gadget/function/f_hid.c1
-rw-r--r--drivers/usb/gadget/function/f_loopback.c3
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.c109
-rw-r--r--drivers/usb/gadget/function/f_printer.c1471
-rw-r--r--drivers/usb/gadget/function/f_sourcesink.c511
-rw-r--r--drivers/usb/gadget/function/g_zero.h13
-rw-r--r--drivers/usb/gadget/function/u_printer.h37
-rw-r--r--drivers/usb/gadget/function/u_serial.c2
-rw-r--r--drivers/usb/gadget/legacy/Kconfig1
-rw-r--r--drivers/usb/gadget/legacy/printer.c1239
-rw-r--r--drivers/usb/gadget/legacy/tcm_usb_gadget.c5
-rw-r--r--drivers/usb/gadget/legacy/zero.c21
-rw-r--r--drivers/usb/gadget/udc/atmel_usba_udc.c212
-rw-r--r--drivers/usb/gadget/udc/atmel_usba_udc.h26
-rw-r--r--drivers/usb/gadget/udc/dummy_hcd.c6
-rw-r--r--drivers/usb/gadget/udc/goku_udc.c233
-rw-r--r--drivers/usb/gadget/udc/lpc32xx_udc.c15
-rw-r--r--drivers/usb/gadget/udc/net2280.c182
-rw-r--r--drivers/usb/gadget/udc/net2280.h2
-rw-r--r--drivers/usb/gadget/udc/pxa27x_udc.c134
-rw-r--r--drivers/usb/gadget/udc/udc-core.c68
-rw-r--r--drivers/usb/host/Kconfig2
-rw-r--r--drivers/usb/host/ehci-atmel.c30
-rw-r--r--drivers/usb/host/ehci-hcd.c10
-rw-r--r--drivers/usb/host/ehci-hub.c11
-rw-r--r--drivers/usb/host/ehci-orion.c18
-rw-r--r--drivers/usb/host/fhci-hub.c2
-rw-r--r--drivers/usb/host/fotg210-hcd.c4
-rw-r--r--drivers/usb/host/fusbh200-hcd.c5
-rw-r--r--drivers/usb/host/imx21-hcd.c2
-rw-r--r--drivers/usb/host/isp116x-hcd.c4
-rw-r--r--drivers/usb/host/isp1362-hcd.c2
-rw-r--r--drivers/usb/host/max3421-hcd.c2
-rw-r--r--drivers/usb/host/ohci-at91.c18
-rw-r--r--drivers/usb/host/ohci-hub.c2
-rw-r--r--drivers/usb/host/oxu210hp-hcd.c9
-rw-r--r--drivers/usb/host/r8a66597-hcd.c4
-rw-r--r--drivers/usb/host/sl811-hcd.c5
-rw-r--r--drivers/usb/host/u132-hcd.c2
-rw-r--r--drivers/usb/host/uhci-hub.c7
-rw-r--r--drivers/usb/host/whci/hcd.c3
-rw-r--r--drivers/usb/host/xhci-hub.c9
-rw-r--r--drivers/usb/host/xhci-pci.c2
-rw-r--r--drivers/usb/host/xhci-plat.c19
-rw-r--r--drivers/usb/host/xhci-ring.c8
-rw-r--r--drivers/usb/host/xhci.c100
-rw-r--r--drivers/usb/host/xhci.h2
-rw-r--r--drivers/usb/image/mdc800.c11
-rw-r--r--drivers/usb/isp1760/isp1760-core.c3
-rw-r--r--drivers/usb/isp1760/isp1760-hcd.c4
-rw-r--r--drivers/usb/isp1760/isp1760-udc.c18
-rw-r--r--drivers/usb/misc/Kconfig13
-rw-r--r--drivers/usb/misc/Makefile1
-rw-r--r--drivers/usb/misc/appledisplay.c2
-rw-r--r--drivers/usb/misc/chaoskey.c532
-rw-r--r--drivers/usb/misc/legousbtower.c6
-rw-r--r--drivers/usb/misc/usb3503.c47
-rw-r--r--drivers/usb/musb/Kconfig3
-rw-r--r--drivers/usb/musb/musb_core.c221
-rw-r--r--drivers/usb/musb/musb_core.h14
-rw-r--r--drivers/usb/musb/musb_cppi41.c84
-rw-r--r--drivers/usb/musb/musb_dsps.c105
-rw-r--r--drivers/usb/musb/musb_gadget.c40
-rw-r--r--drivers/usb/musb/musb_virthub.c4
-rw-r--r--drivers/usb/phy/Kconfig4
-rw-r--r--drivers/usb/phy/of.c2
-rw-r--r--drivers/usb/phy/phy-ab8500-usb.c6
-rw-r--r--drivers/usb/phy/phy-am335x-control.c3
-rw-r--r--drivers/usb/phy/phy-generic.c12
-rw-r--r--drivers/usb/phy/phy-msm-usb.c33
-rw-r--r--drivers/usb/phy/phy-rcar-gen2-usb.c2
-rw-r--r--drivers/usb/phy/phy.c4
-rw-r--r--drivers/usb/renesas_usbhs/common.c19
-rw-r--r--drivers/usb/renesas_usbhs/common.h7
-rw-r--r--drivers/usb/renesas_usbhs/fifo.c189
-rw-r--r--drivers/usb/renesas_usbhs/fifo.h1
-rw-r--r--drivers/usb/renesas_usbhs/mod_gadget.c22
-rw-r--r--drivers/usb/renesas_usbhs/mod_host.c2
-rw-r--r--drivers/usb/renesas_usbhs/pipe.c39
-rw-r--r--drivers/usb/renesas_usbhs/pipe.h1
-rw-r--r--drivers/usb/serial/ch341.c1
-rw-r--r--drivers/usb/serial/f81232.c558
-rw-r--r--drivers/usb/serial/ftdi_sio.c9
-rw-r--r--drivers/usb/serial/ftdi_sio_ids.h6
-rw-r--r--drivers/usb/serial/keyspan_pda.c3
-rw-r--r--drivers/usb/storage/alauda.c15
-rw-r--r--drivers/usb/storage/cypress_atacb.c17
-rw-r--r--drivers/usb/storage/isd200.c2
-rw-r--r--drivers/usb/storage/unusual_uas.h7
-rw-r--r--drivers/usb/usbip/vhci_hcd.c2
-rw-r--r--drivers/usb/wusbcore/rh.c2
134 files changed, 4948 insertions, 3351 deletions
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 2f1e2aa42b44..d8926c6cd2a8 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -5,6 +5,7 @@
5# Object files in subdirectories 5# Object files in subdirectories
6 6
7obj-$(CONFIG_USB) += core/ 7obj-$(CONFIG_USB) += core/
8obj-$(CONFIG_USB_SUPPORT) += phy/
8 9
9obj-$(CONFIG_USB_DWC3) += dwc3/ 10obj-$(CONFIG_USB_DWC3) += dwc3/
10obj-$(CONFIG_USB_DWC2) += dwc2/ 11obj-$(CONFIG_USB_DWC2) += dwc2/
@@ -48,7 +49,6 @@ obj-$(CONFIG_USB_MICROTEK) += image/
48obj-$(CONFIG_USB_SERIAL) += serial/ 49obj-$(CONFIG_USB_SERIAL) += serial/
49 50
50obj-$(CONFIG_USB) += misc/ 51obj-$(CONFIG_USB) += misc/
51obj-$(CONFIG_USB_SUPPORT) += phy/
52obj-$(CONFIG_EARLY_PRINTK_DBGP) += early/ 52obj-$(CONFIG_EARLY_PRINTK_DBGP) += early/
53 53
54obj-$(CONFIG_USB_ATM) += atm/ 54obj-$(CONFIG_USB_ATM) += atm/
diff --git a/drivers/usb/atm/ueagle-atm.c b/drivers/usb/atm/ueagle-atm.c
index 5a459377574b..888998a7fe31 100644
--- a/drivers/usb/atm/ueagle-atm.c
+++ b/drivers/usb/atm/ueagle-atm.c
@@ -952,7 +952,7 @@ static void uea_load_page_e1(struct work_struct *work)
952 int i; 952 int i;
953 953
954 /* reload firmware when reboot start and it's loaded already */ 954 /* reload firmware when reboot start and it's loaded already */
955 if (ovl == 0 && pageno == 0 && sc->dsp_firm) { 955 if (ovl == 0 && pageno == 0) {
956 release_firmware(sc->dsp_firm); 956 release_firmware(sc->dsp_firm);
957 sc->dsp_firm = NULL; 957 sc->dsp_firm = NULL;
958 } 958 }
@@ -1074,7 +1074,7 @@ static void uea_load_page_e4(struct work_struct *work)
1074 uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno); 1074 uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);
1075 1075
1076 /* reload firmware when reboot start and it's loaded already */ 1076 /* reload firmware when reboot start and it's loaded already */
1077 if (pageno == 0 && sc->dsp_firm) { 1077 if (pageno == 0) {
1078 release_firmware(sc->dsp_firm); 1078 release_firmware(sc->dsp_firm);
1079 sc->dsp_firm = NULL; 1079 sc->dsp_firm = NULL;
1080 } 1080 }
diff --git a/drivers/usb/c67x00/c67x00-hcd.c b/drivers/usb/c67x00/c67x00-hcd.c
index 20ec4eee1ac8..c2d13968da82 100644
--- a/drivers/usb/c67x00/c67x00-hcd.c
+++ b/drivers/usb/c67x00/c67x00-hcd.c
@@ -34,7 +34,7 @@
34 34
35static __u8 c67x00_hub_des[] = { 35static __u8 c67x00_hub_des[] = {
36 0x09, /* __u8 bLength; */ 36 0x09, /* __u8 bLength; */
37 0x29, /* __u8 bDescriptorType; Hub-descriptor */ 37 USB_DT_HUB, /* __u8 bDescriptorType; Hub-descriptor */
38 0x02, /* __u8 bNbrPorts; */ 38 0x02, /* __u8 bNbrPorts; */
39 0x00, /* __u16 wHubCharacteristics; */ 39 0x00, /* __u16 wHubCharacteristics; */
40 0x00, /* (per-port OC, no power switching) */ 40 0x00, /* (per-port OC, no power switching) */
diff --git a/drivers/usb/chipidea/Kconfig b/drivers/usb/chipidea/Kconfig
index 77b47d82c9a6..5ce3f1d6a6ed 100644
--- a/drivers/usb/chipidea/Kconfig
+++ b/drivers/usb/chipidea/Kconfig
@@ -10,6 +10,17 @@ config USB_CHIPIDEA
10 10
11if USB_CHIPIDEA 11if USB_CHIPIDEA
12 12
13config USB_CHIPIDEA_OF
14 tristate
15 depends on OF
16 default USB_CHIPIDEA
17
18config USB_CHIPIDEA_PCI
19 tristate
20 depends on PCI
21 depends on NOP_USB_XCEIV
22 default USB_CHIPIDEA
23
13config USB_CHIPIDEA_UDC 24config USB_CHIPIDEA_UDC
14 bool "ChipIdea device controller" 25 bool "ChipIdea device controller"
15 depends on USB_GADGET 26 depends on USB_GADGET
diff --git a/drivers/usb/chipidea/Makefile b/drivers/usb/chipidea/Makefile
index 1fc86a2ca22d..4decb12f2578 100644
--- a/drivers/usb/chipidea/Makefile
+++ b/drivers/usb/chipidea/Makefile
@@ -14,11 +14,6 @@ obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_usb2.o
14obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_msm.o 14obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_msm.o
15obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_zevio.o 15obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_zevio.o
16 16
17# PCI doesn't provide stubs, need to check 17obj-$(CONFIG_USB_CHIPIDEA_PCI) += ci_hdrc_pci.o
18ifneq ($(CONFIG_PCI),)
19 obj-$(CONFIG_USB_CHIPIDEA) += ci_hdrc_pci.o
20endif
21 18
22ifneq ($(CONFIG_OF),) 19obj-$(CONFIG_USB_CHIPIDEA_OF) += usbmisc_imx.o ci_hdrc_imx.o
23 obj-$(CONFIG_USB_CHIPIDEA) += usbmisc_imx.o ci_hdrc_imx.o
24endif
diff --git a/drivers/usb/chipidea/bits.h b/drivers/usb/chipidea/bits.h
index ca57e3dcd3d5..3cb9bda51ddf 100644
--- a/drivers/usb/chipidea/bits.h
+++ b/drivers/usb/chipidea/bits.h
@@ -15,6 +15,16 @@
15 15
16#include <linux/usb/ehci_def.h> 16#include <linux/usb/ehci_def.h>
17 17
18/*
19 * ID
20 * For 1.x revision, bit24 - bit31 are reserved
21 * For 2.x revision, bit25 - bit28 are 0x2
22 */
23#define TAG (0x1F << 16)
24#define REVISION (0xF << 21)
25#define VERSION (0xF << 25)
26#define CIVERSION (0x7 << 29)
27
18/* HCCPARAMS */ 28/* HCCPARAMS */
19#define HCCPARAMS_LEN BIT(17) 29#define HCCPARAMS_LEN BIT(17)
20 30
@@ -53,6 +63,7 @@
53#define PORTSC_HSP BIT(9) 63#define PORTSC_HSP BIT(9)
54#define PORTSC_PP BIT(12) 64#define PORTSC_PP BIT(12)
55#define PORTSC_PTC (0x0FUL << 16) 65#define PORTSC_PTC (0x0FUL << 16)
66#define PORTSC_WKCN BIT(20)
56#define PORTSC_PHCD(d) ((d) ? BIT(22) : BIT(23)) 67#define PORTSC_PHCD(d) ((d) ? BIT(22) : BIT(23))
57/* PTS and PTW for non lpm version only */ 68/* PTS and PTW for non lpm version only */
58#define PORTSC_PFSC BIT(24) 69#define PORTSC_PFSC BIT(24)
diff --git a/drivers/usb/chipidea/ci.h b/drivers/usb/chipidea/ci.h
index 65913d48f0c8..6d6200e37b71 100644
--- a/drivers/usb/chipidea/ci.h
+++ b/drivers/usb/chipidea/ci.h
@@ -29,6 +29,15 @@
29/****************************************************************************** 29/******************************************************************************
30 * REGISTERS 30 * REGISTERS
31 *****************************************************************************/ 31 *****************************************************************************/
32/* Identification Registers */
33#define ID_ID 0x0
34#define ID_HWGENERAL 0x4
35#define ID_HWHOST 0x8
36#define ID_HWDEVICE 0xc
37#define ID_HWTXBUF 0x10
38#define ID_HWRXBUF 0x14
39#define ID_SBUSCFG 0x90
40
32/* register indices */ 41/* register indices */
33enum ci_hw_regs { 42enum ci_hw_regs {
34 CAP_CAPLENGTH, 43 CAP_CAPLENGTH,
@@ -97,6 +106,18 @@ enum ci_role {
97 CI_ROLE_END, 106 CI_ROLE_END,
98}; 107};
99 108
109enum ci_revision {
110 CI_REVISION_1X = 10, /* Revision 1.x */
111 CI_REVISION_20 = 20, /* Revision 2.0 */
112 CI_REVISION_21, /* Revision 2.1 */
113 CI_REVISION_22, /* Revision 2.2 */
114 CI_REVISION_23, /* Revision 2.3 */
115 CI_REVISION_24, /* Revision 2.4 */
116 CI_REVISION_25, /* Revision 2.5 */
117 CI_REVISION_25_PLUS, /* Revision above than 2.5 */
118 CI_REVISION_UNKNOWN = 99, /* Unknown Revision */
119};
120
100/** 121/**
101 * struct ci_role_driver - host/gadget role driver 122 * struct ci_role_driver - host/gadget role driver
102 * @start: start this role 123 * @start: start this role
@@ -141,7 +162,10 @@ struct hw_bank {
141 * @role: current role 162 * @role: current role
142 * @is_otg: if the device is otg-capable 163 * @is_otg: if the device is otg-capable
143 * @fsm: otg finite state machine 164 * @fsm: otg finite state machine
144 * @fsm_timer: pointer to timer list of otg fsm 165 * @otg_fsm_hrtimer: hrtimer for otg fsm timers
166 * @hr_timeouts: time out list for active otg fsm timers
167 * @enabled_otg_timer_bits: bits of enabled otg timers
168 * @next_otg_timer: next nearest enabled timer to be expired
145 * @work: work for role changing 169 * @work: work for role changing
146 * @wq: workqueue thread 170 * @wq: workqueue thread
147 * @qh_pool: allocation pool for queue heads 171 * @qh_pool: allocation pool for queue heads
@@ -169,6 +193,10 @@ struct hw_bank {
169 * @b_sess_valid_event: indicates there is a vbus event, and handled 193 * @b_sess_valid_event: indicates there is a vbus event, and handled
170 * at ci_otg_work 194 * at ci_otg_work
171 * @imx28_write_fix: Freescale imx28 needs swp instruction for writing 195 * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
196 * @supports_runtime_pm: if runtime pm is supported
197 * @in_lpm: if the core in low power mode
198 * @wakeup_int: if wakeup interrupt occur
199 * @rev: The revision number for controller
172 */ 200 */
173struct ci_hdrc { 201struct ci_hdrc {
174 struct device *dev; 202 struct device *dev;
@@ -180,7 +208,10 @@ struct ci_hdrc {
180 bool is_otg; 208 bool is_otg;
181 struct usb_otg otg; 209 struct usb_otg otg;
182 struct otg_fsm fsm; 210 struct otg_fsm fsm;
183 struct ci_otg_fsm_timer_list *fsm_timer; 211 struct hrtimer otg_fsm_hrtimer;
212 ktime_t hr_timeouts[NUM_OTG_FSM_TIMERS];
213 unsigned enabled_otg_timer_bits;
214 enum otg_fsm_timer next_otg_timer;
184 struct work_struct work; 215 struct work_struct work;
185 struct workqueue_struct *wq; 216 struct workqueue_struct *wq;
186 217
@@ -211,6 +242,10 @@ struct ci_hdrc {
211 bool id_event; 242 bool id_event;
212 bool b_sess_valid_event; 243 bool b_sess_valid_event;
213 bool imx28_write_fix; 244 bool imx28_write_fix;
245 bool supports_runtime_pm;
246 bool in_lpm;
247 bool wakeup_int;
248 enum ci_revision rev;
214}; 249};
215 250
216static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci) 251static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
@@ -248,6 +283,36 @@ static inline void ci_role_stop(struct ci_hdrc *ci)
248} 283}
249 284
250/** 285/**
286 * hw_read_id_reg: reads from a identification register
287 * @ci: the controller
288 * @offset: offset from the beginning of identification registers region
289 * @mask: bitfield mask
290 *
291 * This function returns register contents
292 */
293static inline u32 hw_read_id_reg(struct ci_hdrc *ci, u32 offset, u32 mask)
294{
295 return ioread32(ci->hw_bank.abs + offset) & mask;
296}
297
298/**
299 * hw_write_id_reg: writes to a identification register
300 * @ci: the controller
301 * @offset: offset from the beginning of identification registers region
302 * @mask: bitfield mask
303 * @data: new value
304 */
305static inline void hw_write_id_reg(struct ci_hdrc *ci, u32 offset,
306 u32 mask, u32 data)
307{
308 if (~mask)
309 data = (ioread32(ci->hw_bank.abs + offset) & ~mask)
310 | (data & mask);
311
312 iowrite32(data, ci->hw_bank.abs + offset);
313}
314
315/**
251 * hw_read: reads from a hw register 316 * hw_read: reads from a hw register
252 * @ci: the controller 317 * @ci: the controller
253 * @reg: register index 318 * @reg: register index
diff --git a/drivers/usb/chipidea/ci_hdrc_imx.c b/drivers/usb/chipidea/ci_hdrc_imx.c
index 0f05de7c6b6c..389f0e034259 100644
--- a/drivers/usb/chipidea/ci_hdrc_imx.c
+++ b/drivers/usb/chipidea/ci_hdrc_imx.c
@@ -23,22 +23,40 @@
23#include "ci.h" 23#include "ci.h"
24#include "ci_hdrc_imx.h" 24#include "ci_hdrc_imx.h"
25 25
26#define CI_HDRC_IMX_IMX28_WRITE_FIX BIT(0)
27
28struct ci_hdrc_imx_platform_flag { 26struct ci_hdrc_imx_platform_flag {
29 unsigned int flags; 27 unsigned int flags;
28 bool runtime_pm;
30}; 29};
31 30
32static const struct ci_hdrc_imx_platform_flag imx27_usb_data = { 31static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
33}; 32};
34 33
35static const struct ci_hdrc_imx_platform_flag imx28_usb_data = { 34static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
36 .flags = CI_HDRC_IMX_IMX28_WRITE_FIX, 35 .flags = CI_HDRC_IMX28_WRITE_FIX |
36 CI_HDRC_TURN_VBUS_EARLY_ON,
37};
38
39static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
40 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
41 CI_HDRC_TURN_VBUS_EARLY_ON,
42};
43
44static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
45 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
46 CI_HDRC_TURN_VBUS_EARLY_ON,
47};
48
49static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
50 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
51 CI_HDRC_TURN_VBUS_EARLY_ON,
37}; 52};
38 53
39static const struct of_device_id ci_hdrc_imx_dt_ids[] = { 54static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
40 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data}, 55 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
41 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data}, 56 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
57 { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
58 { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
59 { .compatible = "fsl,imx6sx-usb", .data = &imx6sl_usb_data},
42 { /* sentinel */ } 60 { /* sentinel */ }
43}; 61};
44MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); 62MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
@@ -48,6 +66,8 @@ struct ci_hdrc_imx_data {
48 struct platform_device *ci_pdev; 66 struct platform_device *ci_pdev;
49 struct clk *clk; 67 struct clk *clk;
50 struct imx_usbmisc_data *usbmisc_data; 68 struct imx_usbmisc_data *usbmisc_data;
69 bool supports_runtime_pm;
70 bool in_lpm;
51}; 71};
52 72
53/* Common functions shared by usbmisc drivers */ 73/* Common functions shared by usbmisc drivers */
@@ -145,21 +165,18 @@ static int ci_hdrc_imx_probe(struct platform_device *pdev)
145 } 165 }
146 166
147 pdata.usb_phy = data->phy; 167 pdata.usb_phy = data->phy;
148 168 pdata.flags |= imx_platform_flag->flags;
149 if (imx_platform_flag->flags & CI_HDRC_IMX_IMX28_WRITE_FIX) 169 if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
150 pdata.flags |= CI_HDRC_IMX28_WRITE_FIX; 170 data->supports_runtime_pm = true;
151 171
152 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 172 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
153 if (ret) 173 if (ret)
154 goto err_clk; 174 goto err_clk;
155 175
156 if (data->usbmisc_data) { 176 ret = imx_usbmisc_init(data->usbmisc_data);
157 ret = imx_usbmisc_init(data->usbmisc_data); 177 if (ret) {
158 if (ret) { 178 dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret);
159 dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", 179 goto err_clk;
160 ret);
161 goto err_clk;
162 }
163 } 180 }
164 181
165 data->ci_pdev = ci_hdrc_add_device(&pdev->dev, 182 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
@@ -173,19 +190,20 @@ static int ci_hdrc_imx_probe(struct platform_device *pdev)
173 goto err_clk; 190 goto err_clk;
174 } 191 }
175 192
176 if (data->usbmisc_data) { 193 ret = imx_usbmisc_init_post(data->usbmisc_data);
177 ret = imx_usbmisc_init_post(data->usbmisc_data); 194 if (ret) {
178 if (ret) { 195 dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret);
179 dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", 196 goto disable_device;
180 ret);
181 goto disable_device;
182 }
183 } 197 }
184 198
185 platform_set_drvdata(pdev, data); 199 platform_set_drvdata(pdev, data);
186 200
187 pm_runtime_no_callbacks(&pdev->dev); 201 if (data->supports_runtime_pm) {
188 pm_runtime_enable(&pdev->dev); 202 pm_runtime_set_active(&pdev->dev);
203 pm_runtime_enable(&pdev->dev);
204 }
205
206 device_set_wakeup_capable(&pdev->dev, true);
189 207
190 return 0; 208 return 0;
191 209
@@ -200,14 +218,18 @@ static int ci_hdrc_imx_remove(struct platform_device *pdev)
200{ 218{
201 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); 219 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
202 220
203 pm_runtime_disable(&pdev->dev); 221 if (data->supports_runtime_pm) {
222 pm_runtime_get_sync(&pdev->dev);
223 pm_runtime_disable(&pdev->dev);
224 pm_runtime_put_noidle(&pdev->dev);
225 }
204 ci_hdrc_remove_device(data->ci_pdev); 226 ci_hdrc_remove_device(data->ci_pdev);
205 clk_disable_unprepare(data->clk); 227 clk_disable_unprepare(data->clk);
206 228
207 return 0; 229 return 0;
208} 230}
209 231
210#ifdef CONFIG_PM_SLEEP 232#ifdef CONFIG_PM
211static int imx_controller_suspend(struct device *dev) 233static int imx_controller_suspend(struct device *dev)
212{ 234{
213 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); 235 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
@@ -215,6 +237,7 @@ static int imx_controller_suspend(struct device *dev)
215 dev_dbg(dev, "at %s\n", __func__); 237 dev_dbg(dev, "at %s\n", __func__);
216 238
217 clk_disable_unprepare(data->clk); 239 clk_disable_unprepare(data->clk);
240 data->in_lpm = true;
218 241
219 return 0; 242 return 0;
220} 243}
@@ -222,25 +245,103 @@ static int imx_controller_suspend(struct device *dev)
222static int imx_controller_resume(struct device *dev) 245static int imx_controller_resume(struct device *dev)
223{ 246{
224 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); 247 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
248 int ret = 0;
225 249
226 dev_dbg(dev, "at %s\n", __func__); 250 dev_dbg(dev, "at %s\n", __func__);
227 251
228 return clk_prepare_enable(data->clk); 252 if (!data->in_lpm) {
253 WARN_ON(1);
254 return 0;
255 }
256
257 ret = clk_prepare_enable(data->clk);
258 if (ret)
259 return ret;
260
261 data->in_lpm = false;
262
263 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
264 if (ret) {
265 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
266 goto clk_disable;
267 }
268
269 return 0;
270
271clk_disable:
272 clk_disable_unprepare(data->clk);
273 return ret;
229} 274}
230 275
276#ifdef CONFIG_PM_SLEEP
231static int ci_hdrc_imx_suspend(struct device *dev) 277static int ci_hdrc_imx_suspend(struct device *dev)
232{ 278{
279 int ret;
280
281 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
282
283 if (data->in_lpm)
284 /* The core's suspend doesn't run */
285 return 0;
286
287 if (device_may_wakeup(dev)) {
288 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
289 if (ret) {
290 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
291 ret);
292 return ret;
293 }
294 }
295
233 return imx_controller_suspend(dev); 296 return imx_controller_suspend(dev);
234} 297}
235 298
236static int ci_hdrc_imx_resume(struct device *dev) 299static int ci_hdrc_imx_resume(struct device *dev)
237{ 300{
238 return imx_controller_resume(dev); 301 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
302 int ret;
303
304 ret = imx_controller_resume(dev);
305 if (!ret && data->supports_runtime_pm) {
306 pm_runtime_disable(dev);
307 pm_runtime_set_active(dev);
308 pm_runtime_enable(dev);
309 }
310
311 return ret;
239} 312}
240#endif /* CONFIG_PM_SLEEP */ 313#endif /* CONFIG_PM_SLEEP */
241 314
315static int ci_hdrc_imx_runtime_suspend(struct device *dev)
316{
317 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
318 int ret;
319
320 if (data->in_lpm) {
321 WARN_ON(1);
322 return 0;
323 }
324
325 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
326 if (ret) {
327 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
328 return ret;
329 }
330
331 return imx_controller_suspend(dev);
332}
333
334static int ci_hdrc_imx_runtime_resume(struct device *dev)
335{
336 return imx_controller_resume(dev);
337}
338
339#endif /* CONFIG_PM */
340
242static const struct dev_pm_ops ci_hdrc_imx_pm_ops = { 341static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
243 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume) 342 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
343 SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
344 ci_hdrc_imx_runtime_resume, NULL)
244}; 345};
245static struct platform_driver ci_hdrc_imx_driver = { 346static struct platform_driver ci_hdrc_imx_driver = {
246 .probe = ci_hdrc_imx_probe, 347 .probe = ci_hdrc_imx_probe,
diff --git a/drivers/usb/chipidea/ci_hdrc_imx.h b/drivers/usb/chipidea/ci_hdrc_imx.h
index 4ed828f75a1e..635717e9354a 100644
--- a/drivers/usb/chipidea/ci_hdrc_imx.h
+++ b/drivers/usb/chipidea/ci_hdrc_imx.h
@@ -22,5 +22,6 @@ struct imx_usbmisc_data {
22 22
23int imx_usbmisc_init(struct imx_usbmisc_data *); 23int imx_usbmisc_init(struct imx_usbmisc_data *);
24int imx_usbmisc_init_post(struct imx_usbmisc_data *); 24int imx_usbmisc_init_post(struct imx_usbmisc_data *);
25int imx_usbmisc_set_wakeup(struct imx_usbmisc_data *, bool);
25 26
26#endif /* __DRIVER_USB_CHIPIDEA_CI_HDRC_IMX_H */ 27#endif /* __DRIVER_USB_CHIPIDEA_CI_HDRC_IMX_H */
diff --git a/drivers/usb/chipidea/ci_hdrc_pci.c b/drivers/usb/chipidea/ci_hdrc_pci.c
index 4df669437211..773d150512fa 100644
--- a/drivers/usb/chipidea/ci_hdrc_pci.c
+++ b/drivers/usb/chipidea/ci_hdrc_pci.c
@@ -16,10 +16,16 @@
16#include <linux/interrupt.h> 16#include <linux/interrupt.h>
17#include <linux/usb/gadget.h> 17#include <linux/usb/gadget.h>
18#include <linux/usb/chipidea.h> 18#include <linux/usb/chipidea.h>
19#include <linux/usb/usb_phy_generic.h>
19 20
20/* driver name */ 21/* driver name */
21#define UDC_DRIVER_NAME "ci_hdrc_pci" 22#define UDC_DRIVER_NAME "ci_hdrc_pci"
22 23
24struct ci_hdrc_pci {
25 struct platform_device *ci;
26 struct platform_device *phy;
27};
28
23/****************************************************************************** 29/******************************************************************************
24 * PCI block 30 * PCI block
25 *****************************************************************************/ 31 *****************************************************************************/
@@ -52,7 +58,7 @@ static int ci_hdrc_pci_probe(struct pci_dev *pdev,
52 const struct pci_device_id *id) 58 const struct pci_device_id *id)
53{ 59{
54 struct ci_hdrc_platform_data *platdata = (void *)id->driver_data; 60 struct ci_hdrc_platform_data *platdata = (void *)id->driver_data;
55 struct platform_device *plat_ci; 61 struct ci_hdrc_pci *ci;
56 struct resource res[3]; 62 struct resource res[3];
57 int retval = 0, nres = 2; 63 int retval = 0, nres = 2;
58 64
@@ -61,6 +67,10 @@ static int ci_hdrc_pci_probe(struct pci_dev *pdev,
61 return -ENODEV; 67 return -ENODEV;
62 } 68 }
63 69
70 ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
71 if (!ci)
72 return -ENOMEM;
73
64 retval = pcim_enable_device(pdev); 74 retval = pcim_enable_device(pdev);
65 if (retval) 75 if (retval)
66 return retval; 76 return retval;
@@ -73,6 +83,11 @@ static int ci_hdrc_pci_probe(struct pci_dev *pdev,
73 pci_set_master(pdev); 83 pci_set_master(pdev);
74 pci_try_set_mwi(pdev); 84 pci_try_set_mwi(pdev);
75 85
86 /* register a nop PHY */
87 ci->phy = usb_phy_generic_register();
88 if (!ci->phy)
89 return -ENOMEM;
90
76 memset(res, 0, sizeof(res)); 91 memset(res, 0, sizeof(res));
77 res[0].start = pci_resource_start(pdev, 0); 92 res[0].start = pci_resource_start(pdev, 0);
78 res[0].end = pci_resource_end(pdev, 0); 93 res[0].end = pci_resource_end(pdev, 0);
@@ -80,13 +95,14 @@ static int ci_hdrc_pci_probe(struct pci_dev *pdev,
80 res[1].start = pdev->irq; 95 res[1].start = pdev->irq;
81 res[1].flags = IORESOURCE_IRQ; 96 res[1].flags = IORESOURCE_IRQ;
82 97
83 plat_ci = ci_hdrc_add_device(&pdev->dev, res, nres, platdata); 98 ci->ci = ci_hdrc_add_device(&pdev->dev, res, nres, platdata);
84 if (IS_ERR(plat_ci)) { 99 if (IS_ERR(ci->ci)) {
85 dev_err(&pdev->dev, "ci_hdrc_add_device failed!\n"); 100 dev_err(&pdev->dev, "ci_hdrc_add_device failed!\n");
86 return PTR_ERR(plat_ci); 101 usb_phy_generic_unregister(ci->phy);
102 return PTR_ERR(ci->ci);
87 } 103 }
88 104
89 pci_set_drvdata(pdev, plat_ci); 105 pci_set_drvdata(pdev, ci);
90 106
91 return 0; 107 return 0;
92} 108}
@@ -101,9 +117,10 @@ static int ci_hdrc_pci_probe(struct pci_dev *pdev,
101 */ 117 */
102static void ci_hdrc_pci_remove(struct pci_dev *pdev) 118static void ci_hdrc_pci_remove(struct pci_dev *pdev)
103{ 119{
104 struct platform_device *plat_ci = pci_get_drvdata(pdev); 120 struct ci_hdrc_pci *ci = pci_get_drvdata(pdev);
105 121
106 ci_hdrc_remove_device(plat_ci); 122 ci_hdrc_remove_device(ci->ci);
123 usb_phy_generic_unregister(ci->phy);
107} 124}
108 125
109/** 126/**
diff --git a/drivers/usb/chipidea/ci_hdrc_zevio.c b/drivers/usb/chipidea/ci_hdrc_zevio.c
index d976fc1db73a..1264de505527 100644
--- a/drivers/usb/chipidea/ci_hdrc_zevio.c
+++ b/drivers/usb/chipidea/ci_hdrc_zevio.c
@@ -18,7 +18,7 @@
18 18
19static struct ci_hdrc_platform_data ci_hdrc_zevio_platdata = { 19static struct ci_hdrc_platform_data ci_hdrc_zevio_platdata = {
20 .name = "ci_hdrc_zevio", 20 .name = "ci_hdrc_zevio",
21 .flags = CI_HDRC_REGS_SHARED, 21 .flags = CI_HDRC_REGS_SHARED | CI_HDRC_FORCE_FULLSPEED,
22 .capoffset = DEF_CAPOFFSET, 22 .capoffset = DEF_CAPOFFSET,
23}; 23};
24 24
diff --git a/drivers/usb/chipidea/core.c b/drivers/usb/chipidea/core.c
index a57dc8866fc5..74fea4fa41b1 100644
--- a/drivers/usb/chipidea/core.c
+++ b/drivers/usb/chipidea/core.c
@@ -137,6 +137,22 @@ static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
137 return 0; 137 return 0;
138} 138}
139 139
140static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
141{
142 int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
143 enum ci_revision rev = CI_REVISION_UNKNOWN;
144
145 if (ver == 0x2) {
146 rev = hw_read_id_reg(ci, ID_ID, REVISION)
147 >> __ffs(REVISION);
148 rev += CI_REVISION_20;
149 } else if (ver == 0x0) {
150 rev = CI_REVISION_1X;
151 }
152
153 return rev;
154}
155
140/** 156/**
141 * hw_read_intr_enable: returns interrupt enable register 157 * hw_read_intr_enable: returns interrupt enable register
142 * 158 *
@@ -251,8 +267,11 @@ static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
251 /* Clear all interrupts status bits*/ 267 /* Clear all interrupts status bits*/
252 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff); 268 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
253 269
254 dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n", 270 ci->rev = ci_get_revision(ci);
255 ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op); 271
272 dev_dbg(ci->dev,
273 "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
274 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
256 275
257 /* setup lock mode ? */ 276 /* setup lock mode ? */
258 277
@@ -491,6 +510,13 @@ static irqreturn_t ci_irq(int irq, void *data)
491 irqreturn_t ret = IRQ_NONE; 510 irqreturn_t ret = IRQ_NONE;
492 u32 otgsc = 0; 511 u32 otgsc = 0;
493 512
513 if (ci->in_lpm) {
514 disable_irq_nosync(irq);
515 ci->wakeup_int = true;
516 pm_runtime_get(ci->dev);
517 return IRQ_HANDLED;
518 }
519
494 if (ci->is_otg) { 520 if (ci->is_otg) {
495 otgsc = hw_read_otgsc(ci, ~0); 521 otgsc = hw_read_otgsc(ci, ~0);
496 if (ci_otg_is_fsm_mode(ci)) { 522 if (ci_otg_is_fsm_mode(ci)) {
@@ -642,8 +668,12 @@ static void ci_get_otg_capable(struct ci_hdrc *ci)
642 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS, 668 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
643 DCCPARAMS_DC | DCCPARAMS_HC) 669 DCCPARAMS_DC | DCCPARAMS_HC)
644 == (DCCPARAMS_DC | DCCPARAMS_HC)); 670 == (DCCPARAMS_DC | DCCPARAMS_HC));
645 if (ci->is_otg) 671 if (ci->is_otg) {
646 dev_dbg(ci->dev, "It is OTG capable controller\n"); 672 dev_dbg(ci->dev, "It is OTG capable controller\n");
673 /* Disable and clear all OTG irq */
674 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
675 OTGSC_INT_STATUS_BITS);
676 }
647} 677}
648 678
649static int ci_hdrc_probe(struct platform_device *pdev) 679static int ci_hdrc_probe(struct platform_device *pdev)
@@ -673,6 +703,8 @@ static int ci_hdrc_probe(struct platform_device *pdev)
673 ci->platdata = dev_get_platdata(dev); 703 ci->platdata = dev_get_platdata(dev);
674 ci->imx28_write_fix = !!(ci->platdata->flags & 704 ci->imx28_write_fix = !!(ci->platdata->flags &
675 CI_HDRC_IMX28_WRITE_FIX); 705 CI_HDRC_IMX28_WRITE_FIX);
706 ci->supports_runtime_pm = !!(ci->platdata->flags &
707 CI_HDRC_SUPPORTS_RUNTIME_PM);
676 708
677 ret = hw_device_init(ci, base); 709 ret = hw_device_init(ci, base);
678 if (ret < 0) { 710 if (ret < 0) {
@@ -740,9 +772,6 @@ static int ci_hdrc_probe(struct platform_device *pdev)
740 } 772 }
741 773
742 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) { 774 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
743 /* Disable and clear all OTG irq */
744 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
745 OTGSC_INT_STATUS_BITS);
746 ret = ci_hdrc_otg_init(ci); 775 ret = ci_hdrc_otg_init(ci);
747 if (ret) { 776 if (ret) {
748 dev_err(dev, "init otg fails, ret = %d\n", ret); 777 dev_err(dev, "init otg fails, ret = %d\n", ret);
@@ -769,11 +798,11 @@ static int ci_hdrc_probe(struct platform_device *pdev)
769 : CI_ROLE_GADGET; 798 : CI_ROLE_GADGET;
770 } 799 }
771 800
772 /* only update vbus status for peripheral */
773 if (ci->role == CI_ROLE_GADGET)
774 ci_handle_vbus_change(ci);
775
776 if (!ci_otg_is_fsm_mode(ci)) { 801 if (!ci_otg_is_fsm_mode(ci)) {
802 /* only update vbus status for peripheral */
803 if (ci->role == CI_ROLE_GADGET)
804 ci_handle_vbus_change(ci);
805
777 ret = ci_role_start(ci, ci->role); 806 ret = ci_role_start(ci, ci->role);
778 if (ret) { 807 if (ret) {
779 dev_err(dev, "can't start %s role\n", 808 dev_err(dev, "can't start %s role\n",
@@ -788,9 +817,19 @@ static int ci_hdrc_probe(struct platform_device *pdev)
788 if (ret) 817 if (ret)
789 goto stop; 818 goto stop;
790 819
820 if (ci->supports_runtime_pm) {
821 pm_runtime_set_active(&pdev->dev);
822 pm_runtime_enable(&pdev->dev);
823 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
824 pm_runtime_mark_last_busy(ci->dev);
825 pm_runtime_use_autosuspend(&pdev->dev);
826 }
827
791 if (ci_otg_is_fsm_mode(ci)) 828 if (ci_otg_is_fsm_mode(ci))
792 ci_hdrc_otg_fsm_start(ci); 829 ci_hdrc_otg_fsm_start(ci);
793 830
831 device_set_wakeup_capable(&pdev->dev, true);
832
794 ret = dbg_create_files(ci); 833 ret = dbg_create_files(ci);
795 if (!ret) 834 if (!ret)
796 return 0; 835 return 0;
@@ -807,6 +846,12 @@ static int ci_hdrc_remove(struct platform_device *pdev)
807{ 846{
808 struct ci_hdrc *ci = platform_get_drvdata(pdev); 847 struct ci_hdrc *ci = platform_get_drvdata(pdev);
809 848
849 if (ci->supports_runtime_pm) {
850 pm_runtime_get_sync(&pdev->dev);
851 pm_runtime_disable(&pdev->dev);
852 pm_runtime_put_noidle(&pdev->dev);
853 }
854
810 dbg_remove_files(ci); 855 dbg_remove_files(ci);
811 ci_role_destroy(ci); 856 ci_role_destroy(ci);
812 ci_hdrc_enter_lpm(ci, true); 857 ci_hdrc_enter_lpm(ci, true);
@@ -815,13 +860,41 @@ static int ci_hdrc_remove(struct platform_device *pdev)
815 return 0; 860 return 0;
816} 861}
817 862
818#ifdef CONFIG_PM_SLEEP 863#ifdef CONFIG_PM
864/* Prepare wakeup by SRP before suspend */
865static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
866{
867 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
868 !hw_read_otgsc(ci, OTGSC_ID)) {
869 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
870 PORTSC_PP);
871 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
872 PORTSC_WKCN);
873 }
874}
875
876/* Handle SRP when wakeup by data pulse */
877static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
878{
879 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
880 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
881 if (!hw_read_otgsc(ci, OTGSC_ID)) {
882 ci->fsm.a_srp_det = 1;
883 ci->fsm.a_bus_drop = 0;
884 } else {
885 ci->fsm.id = 1;
886 }
887 ci_otg_queue_work(ci);
888 }
889}
890
819static void ci_controller_suspend(struct ci_hdrc *ci) 891static void ci_controller_suspend(struct ci_hdrc *ci)
820{ 892{
893 disable_irq(ci->irq);
821 ci_hdrc_enter_lpm(ci, true); 894 ci_hdrc_enter_lpm(ci, true);
822 895 usb_phy_set_suspend(ci->usb_phy, 1);
823 if (ci->usb_phy) 896 ci->in_lpm = true;
824 usb_phy_set_suspend(ci->usb_phy, 1); 897 enable_irq(ci->irq);
825} 898}
826 899
827static int ci_controller_resume(struct device *dev) 900static int ci_controller_resume(struct device *dev)
@@ -830,23 +903,59 @@ static int ci_controller_resume(struct device *dev)
830 903
831 dev_dbg(dev, "at %s\n", __func__); 904 dev_dbg(dev, "at %s\n", __func__);
832 905
833 ci_hdrc_enter_lpm(ci, false); 906 if (!ci->in_lpm) {
907 WARN_ON(1);
908 return 0;
909 }
834 910
911 ci_hdrc_enter_lpm(ci, false);
835 if (ci->usb_phy) { 912 if (ci->usb_phy) {
836 usb_phy_set_suspend(ci->usb_phy, 0); 913 usb_phy_set_suspend(ci->usb_phy, 0);
837 usb_phy_set_wakeup(ci->usb_phy, false); 914 usb_phy_set_wakeup(ci->usb_phy, false);
838 hw_wait_phy_stable(); 915 hw_wait_phy_stable();
839 } 916 }
840 917
918 ci->in_lpm = false;
919 if (ci->wakeup_int) {
920 ci->wakeup_int = false;
921 pm_runtime_mark_last_busy(ci->dev);
922 pm_runtime_put_autosuspend(ci->dev);
923 enable_irq(ci->irq);
924 if (ci_otg_is_fsm_mode(ci))
925 ci_otg_fsm_wakeup_by_srp(ci);
926 }
927
841 return 0; 928 return 0;
842} 929}
843 930
931#ifdef CONFIG_PM_SLEEP
844static int ci_suspend(struct device *dev) 932static int ci_suspend(struct device *dev)
845{ 933{
846 struct ci_hdrc *ci = dev_get_drvdata(dev); 934 struct ci_hdrc *ci = dev_get_drvdata(dev);
847 935
848 if (ci->wq) 936 if (ci->wq)
849 flush_workqueue(ci->wq); 937 flush_workqueue(ci->wq);
938 /*
939 * Controller needs to be active during suspend, otherwise the core
940 * may run resume when the parent is at suspend if other driver's
941 * suspend fails, it occurs before parent's suspend has not started,
942 * but the core suspend has finished.
943 */
944 if (ci->in_lpm)
945 pm_runtime_resume(dev);
946
947 if (ci->in_lpm) {
948 WARN_ON(1);
949 return 0;
950 }
951
952 if (device_may_wakeup(dev)) {
953 if (ci_otg_is_fsm_mode(ci))
954 ci_otg_fsm_suspend_for_srp(ci);
955
956 usb_phy_set_wakeup(ci->usb_phy, true);
957 enable_irq_wake(ci->irq);
958 }
850 959
851 ci_controller_suspend(ci); 960 ci_controller_suspend(ci);
852 961
@@ -855,13 +964,57 @@ static int ci_suspend(struct device *dev)
855 964
856static int ci_resume(struct device *dev) 965static int ci_resume(struct device *dev)
857{ 966{
858 return ci_controller_resume(dev); 967 struct ci_hdrc *ci = dev_get_drvdata(dev);
968 int ret;
969
970 if (device_may_wakeup(dev))
971 disable_irq_wake(ci->irq);
972
973 ret = ci_controller_resume(dev);
974 if (ret)
975 return ret;
976
977 if (ci->supports_runtime_pm) {
978 pm_runtime_disable(dev);
979 pm_runtime_set_active(dev);
980 pm_runtime_enable(dev);
981 }
982
983 return ret;
859} 984}
860#endif /* CONFIG_PM_SLEEP */ 985#endif /* CONFIG_PM_SLEEP */
861 986
987static int ci_runtime_suspend(struct device *dev)
988{
989 struct ci_hdrc *ci = dev_get_drvdata(dev);
990
991 dev_dbg(dev, "at %s\n", __func__);
992
993 if (ci->in_lpm) {
994 WARN_ON(1);
995 return 0;
996 }
997
998 if (ci_otg_is_fsm_mode(ci))
999 ci_otg_fsm_suspend_for_srp(ci);
1000
1001 usb_phy_set_wakeup(ci->usb_phy, true);
1002 ci_controller_suspend(ci);
1003
1004 return 0;
1005}
1006
1007static int ci_runtime_resume(struct device *dev)
1008{
1009 return ci_controller_resume(dev);
1010}
1011
1012#endif /* CONFIG_PM */
862static const struct dev_pm_ops ci_pm_ops = { 1013static const struct dev_pm_ops ci_pm_ops = {
863 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume) 1014 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
1015 SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
864}; 1016};
1017
865static struct platform_driver ci_hdrc_driver = { 1018static struct platform_driver ci_hdrc_driver = {
866 .probe = ci_hdrc_probe, 1019 .probe = ci_hdrc_probe,
867 .remove = ci_hdrc_remove, 1020 .remove = ci_hdrc_remove,
diff --git a/drivers/usb/chipidea/debug.c b/drivers/usb/chipidea/debug.c
index 268e4236e84c..dfb05edcdb96 100644
--- a/drivers/usb/chipidea/debug.c
+++ b/drivers/usb/chipidea/debug.c
@@ -336,8 +336,8 @@ static int ci_registers_show(struct seq_file *s, void *unused)
336 struct ci_hdrc *ci = s->private; 336 struct ci_hdrc *ci = s->private;
337 u32 tmp_reg; 337 u32 tmp_reg;
338 338
339 if (!ci) 339 if (!ci || ci->in_lpm)
340 return 0; 340 return -EPERM;
341 341
342 /* ------ Registers ----- */ 342 /* ------ Registers ----- */
343 tmp_reg = hw_read_intr_enable(ci); 343 tmp_reg = hw_read_intr_enable(ci);
diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c
index 48731d0bab35..21fe1a314313 100644
--- a/drivers/usb/chipidea/host.c
+++ b/drivers/usb/chipidea/host.c
@@ -33,6 +33,7 @@
33#include "host.h" 33#include "host.h"
34 34
35static struct hc_driver __read_mostly ci_ehci_hc_driver; 35static struct hc_driver __read_mostly ci_ehci_hc_driver;
36static int (*orig_bus_suspend)(struct usb_hcd *hcd);
36 37
37struct ehci_ci_priv { 38struct ehci_ci_priv {
38 struct regulator *reg_vbus; 39 struct regulator *reg_vbus;
@@ -43,11 +44,10 @@ static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
43 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 44 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
44 struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv; 45 struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
45 struct device *dev = hcd->self.controller; 46 struct device *dev = hcd->self.controller;
46 struct ci_hdrc *ci = dev_get_drvdata(dev);
47 int ret = 0; 47 int ret = 0;
48 int port = HCS_N_PORTS(ehci->hcs_params); 48 int port = HCS_N_PORTS(ehci->hcs_params);
49 49
50 if (priv->reg_vbus && !ci_otg_is_fsm_mode(ci)) { 50 if (priv->reg_vbus) {
51 if (port > 1) { 51 if (port > 1) {
52 dev_warn(dev, 52 dev_warn(dev,
53 "Not support multi-port regulator control\n"); 53 "Not support multi-port regulator control\n");
@@ -113,12 +113,23 @@ static int host_start(struct ci_hdrc *ci)
113 priv = (struct ehci_ci_priv *)ehci->priv; 113 priv = (struct ehci_ci_priv *)ehci->priv;
114 priv->reg_vbus = NULL; 114 priv->reg_vbus = NULL;
115 115
116 if (ci->platdata->reg_vbus) 116 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
117 priv->reg_vbus = ci->platdata->reg_vbus; 117 if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
118 ret = regulator_enable(ci->platdata->reg_vbus);
119 if (ret) {
120 dev_err(ci->dev,
121 "Failed to enable vbus regulator, ret=%d\n",
122 ret);
123 goto put_hcd;
124 }
125 } else {
126 priv->reg_vbus = ci->platdata->reg_vbus;
127 }
128 }
118 129
119 ret = usb_add_hcd(hcd, 0, 0); 130 ret = usb_add_hcd(hcd, 0, 0);
120 if (ret) { 131 if (ret) {
121 goto put_hcd; 132 goto disable_reg;
122 } else { 133 } else {
123 struct usb_otg *otg = &ci->otg; 134 struct usb_otg *otg = &ci->otg;
124 135
@@ -133,8 +144,15 @@ static int host_start(struct ci_hdrc *ci)
133 if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING) 144 if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
134 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS); 145 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
135 146
147 if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED)
148 hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
149
136 return ret; 150 return ret;
137 151
152disable_reg:
153 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
154 (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
155 regulator_disable(ci->platdata->reg_vbus);
138put_hcd: 156put_hcd:
139 usb_put_hcd(hcd); 157 usb_put_hcd(hcd);
140 158
@@ -148,6 +166,9 @@ static void host_stop(struct ci_hdrc *ci)
148 if (hcd) { 166 if (hcd) {
149 usb_remove_hcd(hcd); 167 usb_remove_hcd(hcd);
150 usb_put_hcd(hcd); 168 usb_put_hcd(hcd);
169 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
170 (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
171 regulator_disable(ci->platdata->reg_vbus);
151 } 172 }
152} 173}
153 174
@@ -158,6 +179,47 @@ void ci_hdrc_host_destroy(struct ci_hdrc *ci)
158 host_stop(ci); 179 host_stop(ci);
159} 180}
160 181
182static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
183{
184 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
185 int port;
186 u32 tmp;
187
188 int ret = orig_bus_suspend(hcd);
189
190 if (ret)
191 return ret;
192
193 port = HCS_N_PORTS(ehci->hcs_params);
194 while (port--) {
195 u32 __iomem *reg = &ehci->regs->port_status[port];
196 u32 portsc = ehci_readl(ehci, reg);
197
198 if (portsc & PORT_CONNECT) {
199 /*
200 * For chipidea, the resume signal will be ended
201 * automatically, so for remote wakeup case, the
202 * usbcmd.rs may not be set before the resume has
203 * ended if other resume paths consumes too much
204 * time (~24ms), in that case, the SOF will not
205 * send out within 3ms after resume ends, then the
206 * high speed device will enter full speed mode.
207 */
208
209 tmp = ehci_readl(ehci, &ehci->regs->command);
210 tmp |= CMD_RUN;
211 ehci_writel(ehci, tmp, &ehci->regs->command);
212 /*
213 * It needs a short delay between set RS bit and PHCD.
214 */
215 usleep_range(150, 200);
216 break;
217 }
218 }
219
220 return 0;
221}
222
161int ci_hdrc_host_init(struct ci_hdrc *ci) 223int ci_hdrc_host_init(struct ci_hdrc *ci)
162{ 224{
163 struct ci_role_driver *rdrv; 225 struct ci_role_driver *rdrv;
@@ -176,6 +238,8 @@ int ci_hdrc_host_init(struct ci_hdrc *ci)
176 ci->roles[CI_ROLE_HOST] = rdrv; 238 ci->roles[CI_ROLE_HOST] = rdrv;
177 239
178 ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides); 240 ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
241 orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
242 ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
179 243
180 return 0; 244 return 0;
181} 245}
diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
index a048b08b9d4d..ad6c87a4653c 100644
--- a/drivers/usb/chipidea/otg.c
+++ b/drivers/usb/chipidea/otg.c
@@ -96,6 +96,7 @@ static void ci_otg_work(struct work_struct *work)
96 return; 96 return;
97 } 97 }
98 98
99 pm_runtime_get_sync(ci->dev);
99 if (ci->id_event) { 100 if (ci->id_event) {
100 ci->id_event = false; 101 ci->id_event = false;
101 ci_handle_id_switch(ci); 102 ci_handle_id_switch(ci);
@@ -104,6 +105,7 @@ static void ci_otg_work(struct work_struct *work)
104 ci_handle_vbus_change(ci); 105 ci_handle_vbus_change(ci);
105 } else 106 } else
106 dev_err(ci->dev, "unexpected event occurs at %s\n", __func__); 107 dev_err(ci->dev, "unexpected event occurs at %s\n", __func__);
108 pm_runtime_put_sync(ci->dev);
107 109
108 enable_irq(ci->irq); 110 enable_irq(ci->irq);
109} 111}
diff --git a/drivers/usb/chipidea/otg_fsm.c b/drivers/usb/chipidea/otg_fsm.c
index 562e581f6765..083acf45ad5a 100644
--- a/drivers/usb/chipidea/otg_fsm.c
+++ b/drivers/usb/chipidea/otg_fsm.c
@@ -30,22 +30,6 @@
30#include "otg.h" 30#include "otg.h"
31#include "otg_fsm.h" 31#include "otg_fsm.h"
32 32
33static struct ci_otg_fsm_timer *otg_timer_initializer
34(struct ci_hdrc *ci, void (*function)(void *, unsigned long),
35 unsigned long expires, unsigned long data)
36{
37 struct ci_otg_fsm_timer *timer;
38
39 timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
40 GFP_KERNEL);
41 if (!timer)
42 return NULL;
43 timer->function = function;
44 timer->expires = expires;
45 timer->data = data;
46 return timer;
47}
48
49/* Add for otg: interact with user space app */ 33/* Add for otg: interact with user space app */
50static ssize_t 34static ssize_t
51get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf) 35get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
@@ -204,229 +188,227 @@ static struct attribute_group inputs_attr_group = {
204}; 188};
205 189
206/* 190/*
191 * Keep this list in the same order as timers indexed
192 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
193 */
194static unsigned otg_timer_ms[] = {
195 TA_WAIT_VRISE,
196 TA_WAIT_VFALL,
197 TA_WAIT_BCON,
198 TA_AIDL_BDIS,
199 TB_ASE0_BRST,
200 TA_BIDL_ADIS,
201 TB_SE0_SRP,
202 TB_SRP_FAIL,
203 0,
204 TB_DATA_PLS,
205 TB_SSEND_SRP,
206};
207
208/*
207 * Add timer to active timer list 209 * Add timer to active timer list
208 */ 210 */
209static void ci_otg_add_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t) 211static void ci_otg_add_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
210{ 212{
211 struct ci_otg_fsm_timer *tmp_timer; 213 unsigned long flags, timer_sec, timer_nsec;
212 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
213 struct list_head *active_timers = &ci->fsm_timer->active_timers;
214 214
215 if (t >= NUM_CI_OTG_FSM_TIMERS) 215 if (t >= NUM_OTG_FSM_TIMERS)
216 return; 216 return;
217 217
218 /* 218 spin_lock_irqsave(&ci->lock, flags);
219 * Check if the timer is already in the active list, 219 timer_sec = otg_timer_ms[t] / MSEC_PER_SEC;
220 * if so update timer count 220 timer_nsec = (otg_timer_ms[t] % MSEC_PER_SEC) * NSEC_PER_MSEC;
221 */ 221 ci->hr_timeouts[t] = ktime_add(ktime_get(),
222 list_for_each_entry(tmp_timer, active_timers, list) 222 ktime_set(timer_sec, timer_nsec));
223 if (tmp_timer == timer) { 223 ci->enabled_otg_timer_bits |= (1 << t);
224 timer->count = timer->expires; 224 if ((ci->next_otg_timer == NUM_OTG_FSM_TIMERS) ||
225 return; 225 (ci->hr_timeouts[ci->next_otg_timer].tv64 >
226 } 226 ci->hr_timeouts[t].tv64)) {
227 227 ci->next_otg_timer = t;
228 timer->count = timer->expires; 228 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
229 list_add_tail(&timer->list, active_timers); 229 ci->hr_timeouts[t], NSEC_PER_MSEC,
230 230 HRTIMER_MODE_ABS);
231 /* Enable 1ms irq */ 231 }
232 if (!(hw_read_otgsc(ci, OTGSC_1MSIE))) 232 spin_unlock_irqrestore(&ci->lock, flags);
233 hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
234} 233}
235 234
236/* 235/*
237 * Remove timer from active timer list 236 * Remove timer from active timer list
238 */ 237 */
239static void ci_otg_del_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t) 238static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t)
240{ 239{
241 struct ci_otg_fsm_timer *tmp_timer, *del_tmp; 240 unsigned long flags, enabled_timer_bits;
242 struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t]; 241 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
243 struct list_head *active_timers = &ci->fsm_timer->active_timers;
244 242
245 if (t >= NUM_CI_OTG_FSM_TIMERS) 243 if ((t >= NUM_OTG_FSM_TIMERS) ||
244 !(ci->enabled_otg_timer_bits & (1 << t)))
246 return; 245 return;
247 246
248 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list) 247 spin_lock_irqsave(&ci->lock, flags);
249 if (tmp_timer == timer) 248 ci->enabled_otg_timer_bits &= ~(1 << t);
250 list_del(&timer->list); 249 if (ci->next_otg_timer == t) {
251 250 if (ci->enabled_otg_timer_bits == 0) {
252 /* Disable 1ms irq if there is no any active timer */ 251 /* No enabled timers after delete it */
253 if (list_empty(active_timers)) 252 hrtimer_cancel(&ci->otg_fsm_hrtimer);
254 hw_write_otgsc(ci, OTGSC_1MSIE, 0); 253 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
255} 254 } else {
256 255 /* Find the next timer */
257/* 256 enabled_timer_bits = ci->enabled_otg_timer_bits;
258 * Reduce timer count by 1, and find timeout conditions. 257 for_each_set_bit(cur_timer, &enabled_timer_bits,
259 * Called by otg 1ms timer interrupt 258 NUM_OTG_FSM_TIMERS) {
260 */ 259 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
261static inline int ci_otg_tick_timer(struct ci_hdrc *ci) 260 (ci->hr_timeouts[next_timer].tv64 <
262{ 261 ci->hr_timeouts[cur_timer].tv64))
263 struct ci_otg_fsm_timer *tmp_timer, *del_tmp; 262 next_timer = cur_timer;
264 struct list_head *active_timers = &ci->fsm_timer->active_timers; 263 }
265 int expired = 0;
266
267 list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list) {
268 tmp_timer->count--;
269 /* check if timer expires */
270 if (!tmp_timer->count) {
271 list_del(&tmp_timer->list);
272 tmp_timer->function(ci, tmp_timer->data);
273 expired = 1;
274 } 264 }
275 } 265 }
276 266 if (next_timer != NUM_OTG_FSM_TIMERS) {
277 /* disable 1ms irq if there is no any timer active */ 267 ci->next_otg_timer = next_timer;
278 if ((expired == 1) && list_empty(active_timers)) 268 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer,
279 hw_write_otgsc(ci, OTGSC_1MSIE, 0); 269 ci->hr_timeouts[next_timer], NSEC_PER_MSEC,
280 270 HRTIMER_MODE_ABS);
281 return expired; 271 }
272 spin_unlock_irqrestore(&ci->lock, flags);
282} 273}
283 274
284/* The timeout callback function to set time out bit */ 275/* OTG FSM timer handlers */
285static void set_tmout(void *ptr, unsigned long indicator) 276static int a_wait_vrise_tmout(struct ci_hdrc *ci)
286{ 277{
287 *(int *)indicator = 1; 278 ci->fsm.a_wait_vrise_tmout = 1;
279 return 0;
288} 280}
289 281
290static void set_tmout_and_fsm(void *ptr, unsigned long indicator) 282static int a_wait_vfall_tmout(struct ci_hdrc *ci)
291{ 283{
292 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 284 ci->fsm.a_wait_vfall_tmout = 1;
293 285 return 0;
294 set_tmout(ci, indicator);
295
296 ci_otg_queue_work(ci);
297} 286}
298 287
299static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator) 288static int a_wait_bcon_tmout(struct ci_hdrc *ci)
300{ 289{
301 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 290 ci->fsm.a_wait_bcon_tmout = 1;
302 291 return 0;
303 set_tmout(ci, indicator);
304 /* Disable port power */
305 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
306 /* Clear existing DP irq */
307 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
308 /* Enable data pulse irq */
309 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
310 ci_otg_queue_work(ci);
311} 292}
312 293
313static void b_ase0_brst_tmout_func(void *ptr, unsigned long indicator) 294static int a_aidl_bdis_tmout(struct ci_hdrc *ci)
314{ 295{
315 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 296 ci->fsm.a_aidl_bdis_tmout = 1;
316 297 return 0;
317 set_tmout(ci, indicator);
318 if (!hw_read_otgsc(ci, OTGSC_BSV))
319 ci->fsm.b_sess_vld = 0;
320
321 ci_otg_queue_work(ci);
322} 298}
323 299
324static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator) 300static int b_ase0_brst_tmout(struct ci_hdrc *ci)
325{ 301{
326 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 302 ci->fsm.b_ase0_brst_tmout = 1;
327 303 return 0;
328 set_tmout(ci, indicator);
329
330 /* only vbus fall below B_sess_vld in b_idle state */
331 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
332 ci_otg_queue_work(ci);
333} 304}
334 305
335static void b_sess_vld_tmout_func(void *ptr, unsigned long indicator) 306static int a_bidl_adis_tmout(struct ci_hdrc *ci)
336{ 307{
337 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 308 ci->fsm.a_bidl_adis_tmout = 1;
309 return 0;
310}
338 311
339 /* Check if A detached */ 312static int b_se0_srp_tmout(struct ci_hdrc *ci)
340 if (!(hw_read_otgsc(ci, OTGSC_BSV))) { 313{
341 ci->fsm.b_sess_vld = 0; 314 ci->fsm.b_se0_srp = 1;
342 ci_otg_add_timer(ci, B_SSEND_SRP); 315 return 0;
343 ci_otg_queue_work(ci);
344 }
345} 316}
346 317
347static void b_data_pulse_end(void *ptr, unsigned long indicator) 318static int b_srp_fail_tmout(struct ci_hdrc *ci)
348{ 319{
349 struct ci_hdrc *ci = (struct ci_hdrc *)ptr; 320 ci->fsm.b_srp_done = 1;
321 return 1;
322}
350 323
324static int b_data_pls_tmout(struct ci_hdrc *ci)
325{
351 ci->fsm.b_srp_done = 1; 326 ci->fsm.b_srp_done = 1;
352 ci->fsm.b_bus_req = 0; 327 ci->fsm.b_bus_req = 0;
353 if (ci->fsm.power_up) 328 if (ci->fsm.power_up)
354 ci->fsm.power_up = 0; 329 ci->fsm.power_up = 0;
355
356 hw_write_otgsc(ci, OTGSC_HABA, 0); 330 hw_write_otgsc(ci, OTGSC_HABA, 0);
331 pm_runtime_put(ci->dev);
332 return 0;
333}
357 334
358 ci_otg_queue_work(ci); 335static int b_ssend_srp_tmout(struct ci_hdrc *ci)
336{
337 ci->fsm.b_ssend_srp = 1;
338 /* only vbus fall below B_sess_vld in b_idle state */
339 if (ci->fsm.otg->state == OTG_STATE_B_IDLE)
340 return 0;
341 else
342 return 1;
343}
344
345/*
346 * Keep this list in the same order as timers indexed
347 * by enum otg_fsm_timer in include/linux/usb/otg-fsm.h
348 */
349static int (*otg_timer_handlers[])(struct ci_hdrc *) = {
350 a_wait_vrise_tmout, /* A_WAIT_VRISE */
351 a_wait_vfall_tmout, /* A_WAIT_VFALL */
352 a_wait_bcon_tmout, /* A_WAIT_BCON */
353 a_aidl_bdis_tmout, /* A_AIDL_BDIS */
354 b_ase0_brst_tmout, /* B_ASE0_BRST */
355 a_bidl_adis_tmout, /* A_BIDL_ADIS */
356 b_se0_srp_tmout, /* B_SE0_SRP */
357 b_srp_fail_tmout, /* B_SRP_FAIL */
358 NULL, /* A_WAIT_ENUM */
359 b_data_pls_tmout, /* B_DATA_PLS */
360 b_ssend_srp_tmout, /* B_SSEND_SRP */
361};
362
363/*
364 * Enable the next nearest enabled timer if have
365 */
366static enum hrtimer_restart ci_otg_hrtimer_func(struct hrtimer *t)
367{
368 struct ci_hdrc *ci = container_of(t, struct ci_hdrc, otg_fsm_hrtimer);
369 ktime_t now, *timeout;
370 unsigned long enabled_timer_bits;
371 unsigned long flags;
372 enum otg_fsm_timer cur_timer, next_timer = NUM_OTG_FSM_TIMERS;
373 int ret = -EINVAL;
374
375 spin_lock_irqsave(&ci->lock, flags);
376 enabled_timer_bits = ci->enabled_otg_timer_bits;
377 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
378
379 now = ktime_get();
380 for_each_set_bit(cur_timer, &enabled_timer_bits, NUM_OTG_FSM_TIMERS) {
381 if (now.tv64 >= ci->hr_timeouts[cur_timer].tv64) {
382 ci->enabled_otg_timer_bits &= ~(1 << cur_timer);
383 if (otg_timer_handlers[cur_timer])
384 ret = otg_timer_handlers[cur_timer](ci);
385 } else {
386 if ((next_timer == NUM_OTG_FSM_TIMERS) ||
387 (ci->hr_timeouts[cur_timer].tv64 <
388 ci->hr_timeouts[next_timer].tv64))
389 next_timer = cur_timer;
390 }
391 }
392 /* Enable the next nearest timer */
393 if (next_timer < NUM_OTG_FSM_TIMERS) {
394 timeout = &ci->hr_timeouts[next_timer];
395 hrtimer_start_range_ns(&ci->otg_fsm_hrtimer, *timeout,
396 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
397 ci->next_otg_timer = next_timer;
398 }
399 spin_unlock_irqrestore(&ci->lock, flags);
400
401 if (!ret)
402 ci_otg_queue_work(ci);
403
404 return HRTIMER_NORESTART;
359} 405}
360 406
361/* Initialize timers */ 407/* Initialize timers */
362static int ci_otg_init_timers(struct ci_hdrc *ci) 408static int ci_otg_init_timers(struct ci_hdrc *ci)
363{ 409{
364 struct otg_fsm *fsm = &ci->fsm; 410 hrtimer_init(&ci->otg_fsm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
365 411 ci->otg_fsm_hrtimer.function = ci_otg_hrtimer_func;
366 /* FSM used timers */
367 ci->fsm_timer->timer_list[A_WAIT_VRISE] =
368 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
369 (unsigned long)&fsm->a_wait_vrise_tmout);
370 if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
371 return -ENOMEM;
372
373 ci->fsm_timer->timer_list[A_WAIT_VFALL] =
374 otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
375 TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
376 if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
377 return -ENOMEM;
378
379 ci->fsm_timer->timer_list[A_WAIT_BCON] =
380 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
381 (unsigned long)&fsm->a_wait_bcon_tmout);
382 if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
383 return -ENOMEM;
384
385 ci->fsm_timer->timer_list[A_AIDL_BDIS] =
386 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
387 (unsigned long)&fsm->a_aidl_bdis_tmout);
388 if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
389 return -ENOMEM;
390
391 ci->fsm_timer->timer_list[A_BIDL_ADIS] =
392 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
393 (unsigned long)&fsm->a_bidl_adis_tmout);
394 if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
395 return -ENOMEM;
396
397 ci->fsm_timer->timer_list[B_ASE0_BRST] =
398 otg_timer_initializer(ci, &b_ase0_brst_tmout_func, TB_ASE0_BRST,
399 (unsigned long)&fsm->b_ase0_brst_tmout);
400 if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
401 return -ENOMEM;
402
403 ci->fsm_timer->timer_list[B_SE0_SRP] =
404 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
405 (unsigned long)&fsm->b_se0_srp);
406 if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
407 return -ENOMEM;
408
409 ci->fsm_timer->timer_list[B_SSEND_SRP] =
410 otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
411 (unsigned long)&fsm->b_ssend_srp);
412 if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
413 return -ENOMEM;
414
415 ci->fsm_timer->timer_list[B_SRP_FAIL] =
416 otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
417 (unsigned long)&fsm->b_srp_done);
418 if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
419 return -ENOMEM;
420
421 ci->fsm_timer->timer_list[B_DATA_PLS] =
422 otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
423 if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
424 return -ENOMEM;
425
426 ci->fsm_timer->timer_list[B_SESS_VLD] = otg_timer_initializer(ci,
427 &b_sess_vld_tmout_func, TB_SESS_VLD, 0);
428 if (ci->fsm_timer->timer_list[B_SESS_VLD] == NULL)
429 return -ENOMEM;
430 412
431 return 0; 413 return 0;
432} 414}
@@ -530,6 +512,7 @@ static void ci_otg_start_pulse(struct otg_fsm *fsm)
530 /* Hardware Assistant Data pulse */ 512 /* Hardware Assistant Data pulse */
531 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP); 513 hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
532 514
515 pm_runtime_get(ci->dev);
533 ci_otg_add_timer(ci, B_DATA_PLS); 516 ci_otg_add_timer(ci, B_DATA_PLS);
534} 517}
535 518
@@ -585,6 +568,7 @@ int ci_otg_fsm_work(struct ci_hdrc *ci)
585 ci->fsm.otg->state < OTG_STATE_A_IDLE) 568 ci->fsm.otg->state < OTG_STATE_A_IDLE)
586 return 0; 569 return 0;
587 570
571 pm_runtime_get_sync(ci->dev);
588 if (otg_statemachine(&ci->fsm)) { 572 if (otg_statemachine(&ci->fsm)) {
589 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) { 573 if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
590 /* 574 /*
@@ -596,8 +580,15 @@ int ci_otg_fsm_work(struct ci_hdrc *ci)
596 * a_idle to a_wait_vrise when power up 580 * a_idle to a_wait_vrise when power up
597 */ 581 */
598 if ((ci->fsm.id) || (ci->id_event) || 582 if ((ci->fsm.id) || (ci->id_event) ||
599 (ci->fsm.power_up)) 583 (ci->fsm.power_up)) {
600 ci_otg_queue_work(ci); 584 ci_otg_queue_work(ci);
585 } else {
586 /* Enable data pulse irq */
587 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS |
588 PORTSC_PP, 0);
589 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
590 hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
591 }
601 if (ci->id_event) 592 if (ci->id_event)
602 ci->id_event = false; 593 ci->id_event = false;
603 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) { 594 } else if (ci->fsm.otg->state == OTG_STATE_B_IDLE) {
@@ -609,8 +600,13 @@ int ci_otg_fsm_work(struct ci_hdrc *ci)
609 */ 600 */
610 ci_otg_queue_work(ci); 601 ci_otg_queue_work(ci);
611 } 602 }
603 } else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
604 pm_runtime_mark_last_busy(ci->dev);
605 pm_runtime_put_autosuspend(ci->dev);
606 return 0;
612 } 607 }
613 } 608 }
609 pm_runtime_put_sync(ci->dev);
614 return 0; 610 return 0;
615} 611}
616 612
@@ -655,7 +651,6 @@ static void ci_otg_fsm_event(struct ci_hdrc *ci)
655 fsm->a_conn = 0; 651 fsm->a_conn = 0;
656 fsm->b_bus_req = 0; 652 fsm->b_bus_req = 0;
657 ci_otg_queue_work(ci); 653 ci_otg_queue_work(ci);
658 ci_otg_add_timer(ci, B_SESS_VLD);
659 } 654 }
660 break; 655 break;
661 case OTG_STATE_A_PERIPHERAL: 656 case OTG_STATE_A_PERIPHERAL:
@@ -725,11 +720,7 @@ irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
725 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0; 720 fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
726 721
727 if (otg_int_src) { 722 if (otg_int_src) {
728 if (otg_int_src & OTGSC_1MSIS) { 723 if (otg_int_src & OTGSC_DPIS) {
729 hw_write_otgsc(ci, OTGSC_1MSIS, OTGSC_1MSIS);
730 retval = ci_otg_tick_timer(ci);
731 return IRQ_HANDLED;
732 } else if (otg_int_src & OTGSC_DPIS) {
733 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS); 724 hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
734 fsm->a_srp_det = 1; 725 fsm->a_srp_det = 1;
735 fsm->a_bus_drop = 0; 726 fsm->a_bus_drop = 0;
@@ -793,17 +784,13 @@ int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
793 784
794 mutex_init(&ci->fsm.lock); 785 mutex_init(&ci->fsm.lock);
795 786
796 ci->fsm_timer = devm_kzalloc(ci->dev,
797 sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
798 if (!ci->fsm_timer)
799 return -ENOMEM;
800
801 INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
802 retval = ci_otg_init_timers(ci); 787 retval = ci_otg_init_timers(ci);
803 if (retval) { 788 if (retval) {
804 dev_err(ci->dev, "Couldn't init OTG timers\n"); 789 dev_err(ci->dev, "Couldn't init OTG timers\n");
805 return retval; 790 return retval;
806 } 791 }
792 ci->enabled_otg_timer_bits = 0;
793 ci->next_otg_timer = NUM_OTG_FSM_TIMERS;
807 794
808 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group); 795 retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
809 if (retval < 0) { 796 if (retval < 0) {
diff --git a/drivers/usb/chipidea/otg_fsm.h b/drivers/usb/chipidea/otg_fsm.h
index 94c085f456a9..2689375ae5da 100644
--- a/drivers/usb/chipidea/otg_fsm.h
+++ b/drivers/usb/chipidea/otg_fsm.h
@@ -62,33 +62,6 @@
62/* SSEND time before SRP */ 62/* SSEND time before SRP */
63#define TB_SSEND_SRP (1500) /* minimum 1.5 sec, section:5.1.2 */ 63#define TB_SSEND_SRP (1500) /* minimum 1.5 sec, section:5.1.2 */
64 64
65#define TB_SESS_VLD (1000)
66
67enum ci_otg_fsm_timer_index {
68 /*
69 * CI specific timers, start from the end
70 * of standard and auxiliary OTG timers
71 */
72 B_DATA_PLS = NUM_OTG_FSM_TIMERS,
73 B_SSEND_SRP,
74 B_SESS_VLD,
75
76 NUM_CI_OTG_FSM_TIMERS,
77};
78
79struct ci_otg_fsm_timer {
80 unsigned long expires; /* Number of count increase to timeout */
81 unsigned long count; /* Tick counter */
82 void (*function)(void *, unsigned long); /* Timeout function */
83 unsigned long data; /* Data passed to function */
84 struct list_head list;
85};
86
87struct ci_otg_fsm_timer_list {
88 struct ci_otg_fsm_timer *timer_list[NUM_CI_OTG_FSM_TIMERS];
89 struct list_head active_timers;
90};
91
92#ifdef CONFIG_USB_OTG_FSM 65#ifdef CONFIG_USB_OTG_FSM
93 66
94int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci); 67int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci);
diff --git a/drivers/usb/chipidea/udc.c b/drivers/usb/chipidea/udc.c
index ff451048c1ac..764f668d45a9 100644
--- a/drivers/usb/chipidea/udc.c
+++ b/drivers/usb/chipidea/udc.c
@@ -86,10 +86,8 @@ static int hw_device_state(struct ci_hdrc *ci, u32 dma)
86 /* interrupt, error, port change, reset, sleep/suspend */ 86 /* interrupt, error, port change, reset, sleep/suspend */
87 hw_write(ci, OP_USBINTR, ~0, 87 hw_write(ci, OP_USBINTR, ~0,
88 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI); 88 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
89 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
90 } else { 89 } else {
91 hw_write(ci, OP_USBINTR, ~0, 0); 90 hw_write(ci, OP_USBINTR, ~0, 0);
92 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
93 } 91 }
94 return 0; 92 return 0;
95} 93}
@@ -522,6 +520,20 @@ static void free_pending_td(struct ci_hw_ep *hwep)
522 kfree(pending); 520 kfree(pending);
523} 521}
524 522
523static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
524 struct td_node *node)
525{
526 hwep->qh.ptr->td.next = node->dma;
527 hwep->qh.ptr->td.token &=
528 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
529
530 /* Synchronize before ep prime */
531 wmb();
532
533 return hw_ep_prime(ci, hwep->num, hwep->dir,
534 hwep->type == USB_ENDPOINT_XFER_CONTROL);
535}
536
525/** 537/**
526 * _hardware_dequeue: handles a request at hardware level 538 * _hardware_dequeue: handles a request at hardware level
527 * @gadget: gadget 539 * @gadget: gadget
@@ -535,6 +547,7 @@ static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
535 struct td_node *node, *tmpnode; 547 struct td_node *node, *tmpnode;
536 unsigned remaining_length; 548 unsigned remaining_length;
537 unsigned actual = hwreq->req.length; 549 unsigned actual = hwreq->req.length;
550 struct ci_hdrc *ci = hwep->ci;
538 551
539 if (hwreq->req.status != -EALREADY) 552 if (hwreq->req.status != -EALREADY)
540 return -EINVAL; 553 return -EINVAL;
@@ -544,6 +557,11 @@ static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
544 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) { 557 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
545 tmptoken = le32_to_cpu(node->ptr->token); 558 tmptoken = le32_to_cpu(node->ptr->token);
546 if ((TD_STATUS_ACTIVE & tmptoken) != 0) { 559 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
560 int n = hw_ep_bit(hwep->num, hwep->dir);
561
562 if (ci->rev == CI_REVISION_24)
563 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
564 reprime_dtd(ci, hwep, node);
547 hwreq->req.status = -EALREADY; 565 hwreq->req.status = -EALREADY;
548 return -EBUSY; 566 return -EBUSY;
549 } 567 }
@@ -929,6 +947,13 @@ __acquires(hwep->lock)
929 return retval; 947 return retval;
930} 948}
931 949
950static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
951{
952 dev_warn(&ci->gadget.dev,
953 "connect the device to an alternate port if you want HNP\n");
954 return isr_setup_status_phase(ci);
955}
956
932/** 957/**
933 * isr_setup_packet_handler: setup packet handler 958 * isr_setup_packet_handler: setup packet handler
934 * @ci: UDC descriptor 959 * @ci: UDC descriptor
@@ -1061,6 +1086,10 @@ __acquires(ci->lock)
1061 ci); 1086 ci);
1062 } 1087 }
1063 break; 1088 break;
1089 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1090 if (ci_otg_is_fsm_mode(ci))
1091 err = otg_a_alt_hnp_support(ci);
1092 break;
1064 default: 1093 default:
1065 goto delegate; 1094 goto delegate;
1066 } 1095 }
@@ -1151,10 +1180,13 @@ static int ep_enable(struct usb_ep *ep,
1151 1180
1152 /* only internal SW should enable ctrl endpts */ 1181 /* only internal SW should enable ctrl endpts */
1153 1182
1154 hwep->ep.desc = desc; 1183 if (!list_empty(&hwep->qh.queue)) {
1155
1156 if (!list_empty(&hwep->qh.queue))
1157 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n"); 1184 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1185 spin_unlock_irqrestore(hwep->lock, flags);
1186 return -EBUSY;
1187 }
1188
1189 hwep->ep.desc = desc;
1158 1190
1159 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX; 1191 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1160 hwep->num = usb_endpoint_num(desc); 1192 hwep->num = usb_endpoint_num(desc);
@@ -1474,7 +1506,9 @@ static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1474 hw_device_reset(ci); 1506 hw_device_reset(ci);
1475 hw_device_state(ci, ci->ep0out->qh.dma); 1507 hw_device_state(ci, ci->ep0out->qh.dma);
1476 usb_gadget_set_state(_gadget, USB_STATE_POWERED); 1508 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1509 usb_udc_vbus_handler(_gadget, true);
1477 } else { 1510 } else {
1511 usb_udc_vbus_handler(_gadget, false);
1478 if (ci->driver) 1512 if (ci->driver)
1479 ci->driver->disconnect(&ci->gadget); 1513 ci->driver->disconnect(&ci->gadget);
1480 hw_device_state(ci, 0); 1514 hw_device_state(ci, 0);
@@ -1540,13 +1574,16 @@ static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1540{ 1574{
1541 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1575 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1542 1576
1543 if (!ci->vbus_active) 1577 /* Data+ pullup controlled by OTG state machine in OTG fsm mode */
1544 return -EOPNOTSUPP; 1578 if (ci_otg_is_fsm_mode(ci))
1579 return 0;
1545 1580
1581 pm_runtime_get_sync(&ci->gadget.dev);
1546 if (is_on) 1582 if (is_on)
1547 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS); 1583 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1548 else 1584 else
1549 hw_write(ci, OP_USBCMD, USBCMD_RS, 0); 1585 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1586 pm_runtime_put_sync(&ci->gadget.dev);
1550 1587
1551 return 0; 1588 return 0;
1552} 1589}
@@ -1676,6 +1713,7 @@ static int ci_udc_start(struct usb_gadget *gadget,
1676 spin_lock_irqsave(&ci->lock, flags); 1713 spin_lock_irqsave(&ci->lock, flags);
1677 hw_device_reset(ci); 1714 hw_device_reset(ci);
1678 } else { 1715 } else {
1716 usb_udc_vbus_handler(&ci->gadget, false);
1679 pm_runtime_put_sync(&ci->gadget.dev); 1717 pm_runtime_put_sync(&ci->gadget.dev);
1680 return retval; 1718 return retval;
1681 } 1719 }
diff --git a/drivers/usb/chipidea/usbmisc_imx.c b/drivers/usb/chipidea/usbmisc_imx.c
index c3c6225b8acf..140945cb124f 100644
--- a/drivers/usb/chipidea/usbmisc_imx.c
+++ b/drivers/usb/chipidea/usbmisc_imx.c
@@ -11,7 +11,6 @@
11 11
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/of_platform.h> 13#include <linux/of_platform.h>
14#include <linux/clk.h>
15#include <linux/err.h> 14#include <linux/err.h>
16#include <linux/io.h> 15#include <linux/io.h>
17#include <linux/delay.h> 16#include <linux/delay.h>
@@ -56,6 +55,19 @@
56#define MX53_USB_PLL_DIV_24_MHZ 0x01 55#define MX53_USB_PLL_DIV_24_MHZ 0x01
57 56
58#define MX6_BM_OVER_CUR_DIS BIT(7) 57#define MX6_BM_OVER_CUR_DIS BIT(7)
58#define MX6_BM_WAKEUP_ENABLE BIT(10)
59#define MX6_BM_ID_WAKEUP BIT(16)
60#define MX6_BM_VBUS_WAKEUP BIT(17)
61#define MX6SX_BM_DPDM_WAKEUP_EN BIT(29)
62#define MX6_BM_WAKEUP_INTR BIT(31)
63#define MX6_USB_OTG1_PHY_CTRL 0x18
64/* For imx6dql, it is host-only controller, for later imx6, it is otg's */
65#define MX6_USB_OTG2_PHY_CTRL 0x1c
66#define MX6SX_USB_VBUS_WAKEUP_SOURCE(v) (v << 8)
67#define MX6SX_USB_VBUS_WAKEUP_SOURCE_VBUS MX6SX_USB_VBUS_WAKEUP_SOURCE(0)
68#define MX6SX_USB_VBUS_WAKEUP_SOURCE_AVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(1)
69#define MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(2)
70#define MX6SX_USB_VBUS_WAKEUP_SOURCE_SESS_END MX6SX_USB_VBUS_WAKEUP_SOURCE(3)
59 71
60#define VF610_OVER_CUR_DIS BIT(7) 72#define VF610_OVER_CUR_DIS BIT(7)
61 73
@@ -64,12 +76,13 @@ struct usbmisc_ops {
64 int (*init)(struct imx_usbmisc_data *data); 76 int (*init)(struct imx_usbmisc_data *data);
65 /* It's called once after adding a usb device */ 77 /* It's called once after adding a usb device */
66 int (*post)(struct imx_usbmisc_data *data); 78 int (*post)(struct imx_usbmisc_data *data);
79 /* It's called when we need to enable/disable usb wakeup */
80 int (*set_wakeup)(struct imx_usbmisc_data *data, bool enabled);
67}; 81};
68 82
69struct imx_usbmisc { 83struct imx_usbmisc {
70 void __iomem *base; 84 void __iomem *base;
71 spinlock_t lock; 85 spinlock_t lock;
72 struct clk *clk;
73 const struct usbmisc_ops *ops; 86 const struct usbmisc_ops *ops;
74}; 87};
75 88
@@ -204,6 +217,35 @@ static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
204 return 0; 217 return 0;
205} 218}
206 219
220static int usbmisc_imx6q_set_wakeup
221 (struct imx_usbmisc_data *data, bool enabled)
222{
223 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
224 unsigned long flags;
225 u32 val;
226 u32 wakeup_setting = (MX6_BM_WAKEUP_ENABLE |
227 MX6_BM_VBUS_WAKEUP | MX6_BM_ID_WAKEUP);
228 int ret = 0;
229
230 if (data->index > 3)
231 return -EINVAL;
232
233 spin_lock_irqsave(&usbmisc->lock, flags);
234 val = readl(usbmisc->base + data->index * 4);
235 if (enabled) {
236 val |= wakeup_setting;
237 writel(val, usbmisc->base + data->index * 4);
238 } else {
239 if (val & MX6_BM_WAKEUP_INTR)
240 pr_debug("wakeup int at ci_hdrc.%d\n", data->index);
241 val &= ~wakeup_setting;
242 writel(val, usbmisc->base + data->index * 4);
243 }
244 spin_unlock_irqrestore(&usbmisc->lock, flags);
245
246 return ret;
247}
248
207static int usbmisc_imx6q_init(struct imx_usbmisc_data *data) 249static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
208{ 250{
209 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev); 251 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
@@ -221,6 +263,36 @@ static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
221 spin_unlock_irqrestore(&usbmisc->lock, flags); 263 spin_unlock_irqrestore(&usbmisc->lock, flags);
222 } 264 }
223 265
266 usbmisc_imx6q_set_wakeup(data, false);
267
268 return 0;
269}
270
271static int usbmisc_imx6sx_init(struct imx_usbmisc_data *data)
272{
273 void __iomem *reg = NULL;
274 unsigned long flags;
275 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
276 u32 val;
277
278 usbmisc_imx6q_init(data);
279
280 if (data->index == 0 || data->index == 1) {
281 reg = usbmisc->base + MX6_USB_OTG1_PHY_CTRL + data->index * 4;
282 spin_lock_irqsave(&usbmisc->lock, flags);
283 /* Set vbus wakeup source as bvalid */
284 val = readl(reg);
285 writel(val | MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID, reg);
286 /*
287 * Disable dp/dm wakeup in device mode when vbus is
288 * not there.
289 */
290 val = readl(usbmisc->base + data->index * 4);
291 writel(val & ~MX6SX_BM_DPDM_WAKEUP_EN,
292 usbmisc->base + data->index * 4);
293 spin_unlock_irqrestore(&usbmisc->lock, flags);
294 }
295
224 return 0; 296 return 0;
225} 297}
226 298
@@ -258,6 +330,7 @@ static const struct usbmisc_ops imx53_usbmisc_ops = {
258}; 330};
259 331
260static const struct usbmisc_ops imx6q_usbmisc_ops = { 332static const struct usbmisc_ops imx6q_usbmisc_ops = {
333 .set_wakeup = usbmisc_imx6q_set_wakeup,
261 .init = usbmisc_imx6q_init, 334 .init = usbmisc_imx6q_init,
262}; 335};
263 336
@@ -265,10 +338,19 @@ static const struct usbmisc_ops vf610_usbmisc_ops = {
265 .init = usbmisc_vf610_init, 338 .init = usbmisc_vf610_init,
266}; 339};
267 340
341static const struct usbmisc_ops imx6sx_usbmisc_ops = {
342 .set_wakeup = usbmisc_imx6q_set_wakeup,
343 .init = usbmisc_imx6sx_init,
344};
345
268int imx_usbmisc_init(struct imx_usbmisc_data *data) 346int imx_usbmisc_init(struct imx_usbmisc_data *data)
269{ 347{
270 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev); 348 struct imx_usbmisc *usbmisc;
349
350 if (!data)
351 return 0;
271 352
353 usbmisc = dev_get_drvdata(data->dev);
272 if (!usbmisc->ops->init) 354 if (!usbmisc->ops->init)
273 return 0; 355 return 0;
274 return usbmisc->ops->init(data); 356 return usbmisc->ops->init(data);
@@ -277,14 +359,32 @@ EXPORT_SYMBOL_GPL(imx_usbmisc_init);
277 359
278int imx_usbmisc_init_post(struct imx_usbmisc_data *data) 360int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
279{ 361{
280 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev); 362 struct imx_usbmisc *usbmisc;
281 363
364 if (!data)
365 return 0;
366
367 usbmisc = dev_get_drvdata(data->dev);
282 if (!usbmisc->ops->post) 368 if (!usbmisc->ops->post)
283 return 0; 369 return 0;
284 return usbmisc->ops->post(data); 370 return usbmisc->ops->post(data);
285} 371}
286EXPORT_SYMBOL_GPL(imx_usbmisc_init_post); 372EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);
287 373
374int imx_usbmisc_set_wakeup(struct imx_usbmisc_data *data, bool enabled)
375{
376 struct imx_usbmisc *usbmisc;
377
378 if (!data)
379 return 0;
380
381 usbmisc = dev_get_drvdata(data->dev);
382 if (!usbmisc->ops->set_wakeup)
383 return 0;
384 return usbmisc->ops->set_wakeup(data, enabled);
385}
386EXPORT_SYMBOL_GPL(imx_usbmisc_set_wakeup);
387
288static const struct of_device_id usbmisc_imx_dt_ids[] = { 388static const struct of_device_id usbmisc_imx_dt_ids[] = {
289 { 389 {
290 .compatible = "fsl,imx25-usbmisc", 390 .compatible = "fsl,imx25-usbmisc",
@@ -314,6 +414,10 @@ static const struct of_device_id usbmisc_imx_dt_ids[] = {
314 .compatible = "fsl,vf610-usbmisc", 414 .compatible = "fsl,vf610-usbmisc",
315 .data = &vf610_usbmisc_ops, 415 .data = &vf610_usbmisc_ops,
316 }, 416 },
417 {
418 .compatible = "fsl,imx6sx-usbmisc",
419 .data = &imx6sx_usbmisc_ops,
420 },
317 { /* sentinel */ } 421 { /* sentinel */ }
318}; 422};
319MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids); 423MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
@@ -322,7 +426,6 @@ static int usbmisc_imx_probe(struct platform_device *pdev)
322{ 426{
323 struct resource *res; 427 struct resource *res;
324 struct imx_usbmisc *data; 428 struct imx_usbmisc *data;
325 int ret;
326 struct of_device_id *tmp_dev; 429 struct of_device_id *tmp_dev;
327 430
328 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 431 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
@@ -336,20 +439,6 @@ static int usbmisc_imx_probe(struct platform_device *pdev)
336 if (IS_ERR(data->base)) 439 if (IS_ERR(data->base))
337 return PTR_ERR(data->base); 440 return PTR_ERR(data->base);
338 441
339 data->clk = devm_clk_get(&pdev->dev, NULL);
340 if (IS_ERR(data->clk)) {
341 dev_err(&pdev->dev,
342 "failed to get clock, err=%ld\n", PTR_ERR(data->clk));
343 return PTR_ERR(data->clk);
344 }
345
346 ret = clk_prepare_enable(data->clk);
347 if (ret) {
348 dev_err(&pdev->dev,
349 "clk_prepare_enable failed, err=%d\n", ret);
350 return ret;
351 }
352
353 tmp_dev = (struct of_device_id *) 442 tmp_dev = (struct of_device_id *)
354 of_match_device(usbmisc_imx_dt_ids, &pdev->dev); 443 of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
355 data->ops = (const struct usbmisc_ops *)tmp_dev->data; 444 data->ops = (const struct usbmisc_ops *)tmp_dev->data;
@@ -360,8 +449,6 @@ static int usbmisc_imx_probe(struct platform_device *pdev)
360 449
361static int usbmisc_imx_remove(struct platform_device *pdev) 450static int usbmisc_imx_remove(struct platform_device *pdev)
362{ 451{
363 struct imx_usbmisc *usbmisc = dev_get_drvdata(&pdev->dev);
364 clk_disable_unprepare(usbmisc->clk);
365 return 0; 452 return 0;
366} 453}
367 454
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 683617714e7c..3e15add665e2 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -360,7 +360,7 @@ static void acm_ctrl_irq(struct urb *urb)
360 } 360 }
361exit: 361exit:
362 retval = usb_submit_urb(urb, GFP_ATOMIC); 362 retval = usb_submit_urb(urb, GFP_ATOMIC);
363 if (retval) 363 if (retval && retval != -EPERM)
364 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n", 364 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
365 __func__, retval); 365 __func__, retval);
366} 366}
@@ -417,25 +417,33 @@ static void acm_read_bulk_callback(struct urb *urb)
417 struct acm_rb *rb = urb->context; 417 struct acm_rb *rb = urb->context;
418 struct acm *acm = rb->instance; 418 struct acm *acm = rb->instance;
419 unsigned long flags; 419 unsigned long flags;
420 int status = urb->status;
420 421
421 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__, 422 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
422 rb->index, urb->actual_length); 423 rb->index, urb->actual_length);
423 set_bit(rb->index, &acm->read_urbs_free);
424 424
425 if (!acm->dev) { 425 if (!acm->dev) {
426 set_bit(rb->index, &acm->read_urbs_free);
426 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__); 427 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
427 return; 428 return;
428 } 429 }
429 430
430 if (urb->status) { 431 if (status) {
432 set_bit(rb->index, &acm->read_urbs_free);
431 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n", 433 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
432 __func__, urb->status); 434 __func__, status);
433 return; 435 return;
434 } 436 }
435 437
436 usb_mark_last_busy(acm->dev); 438 usb_mark_last_busy(acm->dev);
437 439
438 acm_process_read_urb(acm, urb); 440 acm_process_read_urb(acm, urb);
441 /*
442 * Unthrottle may run on another CPU which needs to see events
443 * in the same order. Submission has an implict barrier
444 */
445 smp_mb__before_atomic();
446 set_bit(rb->index, &acm->read_urbs_free);
439 447
440 /* throttle device if requested by tty */ 448 /* throttle device if requested by tty */
441 spin_lock_irqsave(&acm->read_lock, flags); 449 spin_lock_irqsave(&acm->read_lock, flags);
@@ -454,13 +462,14 @@ static void acm_write_bulk(struct urb *urb)
454 struct acm_wb *wb = urb->context; 462 struct acm_wb *wb = urb->context;
455 struct acm *acm = wb->instance; 463 struct acm *acm = wb->instance;
456 unsigned long flags; 464 unsigned long flags;
465 int status = urb->status;
457 466
458 if (urb->status || (urb->actual_length != urb->transfer_buffer_length)) 467 if (status || (urb->actual_length != urb->transfer_buffer_length))
459 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n", 468 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
460 __func__, 469 __func__,
461 urb->actual_length, 470 urb->actual_length,
462 urb->transfer_buffer_length, 471 urb->transfer_buffer_length,
463 urb->status); 472 status);
464 473
465 spin_lock_irqsave(&acm->write_lock, flags); 474 spin_lock_irqsave(&acm->write_lock, flags);
466 acm_write_done(acm, wb); 475 acm_write_done(acm, wb);
diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
index a051a7a2b1bd..61ea87917433 100644
--- a/drivers/usb/class/cdc-wdm.c
+++ b/drivers/usb/class/cdc-wdm.c
@@ -245,7 +245,7 @@ static void wdm_int_callback(struct urb *urb)
245 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: 245 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
246 dev_dbg(&desc->intf->dev, 246 dev_dbg(&desc->intf->dev,
247 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", 247 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
248 dr->wIndex, dr->wLength); 248 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
249 break; 249 break;
250 250
251 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 251 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
@@ -262,7 +262,9 @@ static void wdm_int_callback(struct urb *urb)
262 clear_bit(WDM_POLL_RUNNING, &desc->flags); 262 clear_bit(WDM_POLL_RUNNING, &desc->flags);
263 dev_err(&desc->intf->dev, 263 dev_err(&desc->intf->dev,
264 "unknown notification %d received: index %d len %d\n", 264 "unknown notification %d received: index %d len %d\n",
265 dr->bNotificationType, dr->wIndex, dr->wLength); 265 dr->bNotificationType,
266 le16_to_cpu(dr->wIndex),
267 le16_to_cpu(dr->wLength));
266 goto exit; 268 goto exit;
267 } 269 }
268 270
@@ -339,7 +341,7 @@ static ssize_t wdm_write
339 desc->werr = 0; 341 desc->werr = 0;
340 spin_unlock_irq(&desc->iuspin); 342 spin_unlock_irq(&desc->iuspin);
341 if (we < 0) 343 if (we < 0)
342 return -EIO; 344 return usb_translate_errors(we);
343 345
344 buf = kmalloc(count, GFP_KERNEL); 346 buf = kmalloc(count, GFP_KERNEL);
345 if (!buf) { 347 if (!buf) {
@@ -349,30 +351,25 @@ static ssize_t wdm_write
349 351
350 r = copy_from_user(buf, buffer, count); 352 r = copy_from_user(buf, buffer, count);
351 if (r > 0) { 353 if (r > 0) {
352 kfree(buf);
353 rv = -EFAULT; 354 rv = -EFAULT;
354 goto outnl; 355 goto out_free_mem;
355 } 356 }
356 357
357 /* concurrent writes and disconnect */ 358 /* concurrent writes and disconnect */
358 r = mutex_lock_interruptible(&desc->wlock); 359 r = mutex_lock_interruptible(&desc->wlock);
359 rv = -ERESTARTSYS; 360 rv = -ERESTARTSYS;
360 if (r) { 361 if (r)
361 kfree(buf); 362 goto out_free_mem;
362 goto outnl;
363 }
364 363
365 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 364 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
366 kfree(buf);
367 rv = -ENODEV; 365 rv = -ENODEV;
368 goto outnp; 366 goto out_free_mem_lock;
369 } 367 }
370 368
371 r = usb_autopm_get_interface(desc->intf); 369 r = usb_autopm_get_interface(desc->intf);
372 if (r < 0) { 370 if (r < 0) {
373 kfree(buf);
374 rv = usb_translate_errors(r); 371 rv = usb_translate_errors(r);
375 goto outnp; 372 goto out_free_mem_lock;
376 } 373 }
377 374
378 if (!(file->f_flags & O_NONBLOCK)) 375 if (!(file->f_flags & O_NONBLOCK))
@@ -386,9 +383,8 @@ static ssize_t wdm_write
386 r = -EIO; 383 r = -EIO;
387 384
388 if (r < 0) { 385 if (r < 0) {
389 kfree(buf);
390 rv = r; 386 rv = r;
391 goto out; 387 goto out_free_mem_pm;
392 } 388 }
393 389
394 req = desc->orq; 390 req = desc->orq;
@@ -408,28 +404,35 @@ static ssize_t wdm_write
408 USB_RECIP_INTERFACE); 404 USB_RECIP_INTERFACE);
409 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; 405 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
410 req->wValue = 0; 406 req->wValue = 0;
411 req->wIndex = desc->inum; 407 req->wIndex = desc->inum; /* already converted */
412 req->wLength = cpu_to_le16(count); 408 req->wLength = cpu_to_le16(count);
413 set_bit(WDM_IN_USE, &desc->flags); 409 set_bit(WDM_IN_USE, &desc->flags);
414 desc->outbuf = buf; 410 desc->outbuf = buf;
415 411
416 rv = usb_submit_urb(desc->command, GFP_KERNEL); 412 rv = usb_submit_urb(desc->command, GFP_KERNEL);
417 if (rv < 0) { 413 if (rv < 0) {
418 kfree(buf);
419 desc->outbuf = NULL; 414 desc->outbuf = NULL;
420 clear_bit(WDM_IN_USE, &desc->flags); 415 clear_bit(WDM_IN_USE, &desc->flags);
421 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); 416 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
422 rv = usb_translate_errors(rv); 417 rv = usb_translate_errors(rv);
418 goto out_free_mem_pm;
423 } else { 419 } else {
424 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", 420 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
425 req->wIndex); 421 le16_to_cpu(req->wIndex));
426 } 422 }
427out: 423
428 usb_autopm_put_interface(desc->intf); 424 usb_autopm_put_interface(desc->intf);
429outnp:
430 mutex_unlock(&desc->wlock); 425 mutex_unlock(&desc->wlock);
431outnl: 426outnl:
432 return rv < 0 ? rv : count; 427 return rv < 0 ? rv : count;
428
429out_free_mem_pm:
430 usb_autopm_put_interface(desc->intf);
431out_free_mem_lock:
432 mutex_unlock(&desc->wlock);
433out_free_mem:
434 kfree(buf);
435 return rv;
433} 436}
434 437
435/* 438/*
@@ -519,9 +522,9 @@ retry:
519 spin_lock_irq(&desc->iuspin); 522 spin_lock_irq(&desc->iuspin);
520 523
521 if (desc->rerr) { /* read completed, error happened */ 524 if (desc->rerr) { /* read completed, error happened */
525 rv = usb_translate_errors(desc->rerr);
522 desc->rerr = 0; 526 desc->rerr = 0;
523 spin_unlock_irq(&desc->iuspin); 527 spin_unlock_irq(&desc->iuspin);
524 rv = -EIO;
525 goto err; 528 goto err;
526 } 529 }
527 /* 530 /*
@@ -820,7 +823,7 @@ static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor
820 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); 823 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
821 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 824 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
822 desc->irq->wValue = 0; 825 desc->irq->wValue = 0;
823 desc->irq->wIndex = desc->inum; 826 desc->irq->wIndex = desc->inum; /* already converted */
824 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); 827 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
825 828
826 usb_fill_control_urb( 829 usb_fill_control_urb(
diff --git a/drivers/usb/common/usb-otg-fsm.c b/drivers/usb/common/usb-otg-fsm.c
index c6b35b77dab7..61d538aa2346 100644
--- a/drivers/usb/common/usb-otg-fsm.c
+++ b/drivers/usb/common/usb-otg-fsm.c
@@ -150,9 +150,9 @@ static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
150 break; 150 break;
151 case OTG_STATE_B_PERIPHERAL: 151 case OTG_STATE_B_PERIPHERAL:
152 otg_chrg_vbus(fsm, 0); 152 otg_chrg_vbus(fsm, 0);
153 otg_loc_conn(fsm, 1);
154 otg_loc_sof(fsm, 0); 153 otg_loc_sof(fsm, 0);
155 otg_set_protocol(fsm, PROTO_GADGET); 154 otg_set_protocol(fsm, PROTO_GADGET);
155 otg_loc_conn(fsm, 1);
156 break; 156 break;
157 case OTG_STATE_B_WAIT_ACON: 157 case OTG_STATE_B_WAIT_ACON:
158 otg_chrg_vbus(fsm, 0); 158 otg_chrg_vbus(fsm, 0);
@@ -213,10 +213,10 @@ static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
213 213
214 break; 214 break;
215 case OTG_STATE_A_PERIPHERAL: 215 case OTG_STATE_A_PERIPHERAL:
216 otg_loc_conn(fsm, 1);
217 otg_loc_sof(fsm, 0); 216 otg_loc_sof(fsm, 0);
218 otg_set_protocol(fsm, PROTO_GADGET); 217 otg_set_protocol(fsm, PROTO_GADGET);
219 otg_drv_vbus(fsm, 1); 218 otg_drv_vbus(fsm, 1);
219 otg_loc_conn(fsm, 1);
220 otg_add_timer(fsm, A_BIDL_ADIS); 220 otg_add_timer(fsm, A_BIDL_ADIS);
221 break; 221 break;
222 case OTG_STATE_A_WAIT_VFALL: 222 case OTG_STATE_A_WAIT_VFALL:
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 11635537c052..4b0448c26810 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -2408,7 +2408,7 @@ static int usbdev_notify(struct notifier_block *self,
2408} 2408}
2409 2409
2410static struct notifier_block usbdev_nb = { 2410static struct notifier_block usbdev_nb = {
2411 .notifier_call = usbdev_notify, 2411 .notifier_call = usbdev_notify,
2412}; 2412};
2413 2413
2414static struct cdev usb_device_cdev; 2414static struct cdev usb_device_cdev;
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index d7c3d5a35946..3b7151687776 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -3406,10 +3406,10 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3406 if (status) { 3406 if (status) {
3407 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 3407 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
3408 } else { 3408 } else {
3409 /* drive resume for at least 20 msec */ 3409 /* drive resume for USB_RESUME_TIMEOUT msec */
3410 dev_dbg(&udev->dev, "usb %sresume\n", 3410 dev_dbg(&udev->dev, "usb %sresume\n",
3411 (PMSG_IS_AUTO(msg) ? "auto-" : "")); 3411 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
3412 msleep(25); 3412 msleep(USB_RESUME_TIMEOUT);
3413 3413
3414 /* Virtual root hubs can trigger on GET_PORT_STATUS to 3414 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3415 * stop resume signaling. Then finish the resume 3415 * stop resume signaling. Then finish the resume
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index b1fb9aef0f5b..8d5b2f4113cd 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -49,6 +49,22 @@ const char *usbcore_name = "usbcore";
49 49
50static bool nousb; /* Disable USB when built into kernel image */ 50static bool nousb; /* Disable USB when built into kernel image */
51 51
52/* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */
53#ifdef MODULE
54module_param(nousb, bool, 0444);
55#else
56core_param(nousb, nousb, bool, 0444);
57#endif
58
59/*
60 * for external read access to <nousb>
61 */
62int usb_disabled(void)
63{
64 return nousb;
65}
66EXPORT_SYMBOL_GPL(usb_disabled);
67
52#ifdef CONFIG_PM 68#ifdef CONFIG_PM
53static int usb_autosuspend_delay = 2; /* Default delay value, 69static int usb_autosuspend_delay = 2; /* Default delay value,
54 * in seconds */ 70 * in seconds */
@@ -964,22 +980,6 @@ void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
964EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg); 980EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
965#endif 981#endif
966 982
967/* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */
968#ifdef MODULE
969module_param(nousb, bool, 0444);
970#else
971core_param(nousb, nousb, bool, 0444);
972#endif
973
974/*
975 * for external read access to <nousb>
976 */
977int usb_disabled(void)
978{
979 return nousb;
980}
981EXPORT_SYMBOL_GPL(usb_disabled);
982
983/* 983/*
984 * Notifications of device and interface registration 984 * Notifications of device and interface registration
985 */ 985 */
@@ -1045,7 +1045,7 @@ static void usb_debugfs_cleanup(void)
1045static int __init usb_init(void) 1045static int __init usb_init(void)
1046{ 1046{
1047 int retval; 1047 int retval;
1048 if (nousb) { 1048 if (usb_disabled()) {
1049 pr_info("%s: USB support disabled\n", usbcore_name); 1049 pr_info("%s: USB support disabled\n", usbcore_name);
1050 return 0; 1050 return 0;
1051 } 1051 }
@@ -1102,7 +1102,7 @@ out:
1102static void __exit usb_exit(void) 1102static void __exit usb_exit(void)
1103{ 1103{
1104 /* This will matter if shutdown/reboot does exitcalls. */ 1104 /* This will matter if shutdown/reboot does exitcalls. */
1105 if (nousb) 1105 if (usb_disabled())
1106 return; 1106 return;
1107 1107
1108 usb_deregister_device_driver(&usb_generic_driver); 1108 usb_deregister_device_driver(&usb_generic_driver);
diff --git a/drivers/usb/dwc2/Kconfig b/drivers/usb/dwc2/Kconfig
index 76b9ba4dc925..1bcb36ae6505 100644
--- a/drivers/usb/dwc2/Kconfig
+++ b/drivers/usb/dwc2/Kconfig
@@ -59,11 +59,13 @@ config USB_DWC2_PLATFORM
59 59
60config USB_DWC2_PCI 60config USB_DWC2_PCI
61 tristate "DWC2 PCI" 61 tristate "DWC2 PCI"
62 depends on USB_DWC2_HOST && PCI 62 depends on PCI
63 default USB_DWC2_HOST 63 default n
64 select USB_DWC2_PLATFORM
65 select NOP_USB_XCEIV
64 help 66 help
65 The Designware USB2.0 PCI interface module for controllers 67 The Designware USB2.0 PCI interface module for controllers
66 connected to a PCI bus. This is only used for host mode. 68 connected to a PCI bus.
67 69
68config USB_DWC2_DEBUG 70config USB_DWC2_DEBUG
69 bool "Enable Debugging Messages" 71 bool "Enable Debugging Messages"
diff --git a/drivers/usb/dwc2/Makefile b/drivers/usb/dwc2/Makefile
index 8f752679752a..f07b425eaff3 100644
--- a/drivers/usb/dwc2/Makefile
+++ b/drivers/usb/dwc2/Makefile
@@ -19,10 +19,8 @@ endif
19# mode. The PCI bus interface module will called dwc2_pci.ko and the platform 19# mode. The PCI bus interface module will called dwc2_pci.ko and the platform
20# interface module will be called dwc2_platform.ko. 20# interface module will be called dwc2_platform.ko.
21 21
22ifneq ($(CONFIG_USB_DWC2_PCI),) 22obj-$(CONFIG_USB_DWC2_PCI) += dwc2_pci.o
23 obj-$(CONFIG_USB_DWC2) += dwc2_pci.o 23dwc2_pci-y := pci.o
24 dwc2_pci-y := pci.o
25endif
26 24
27obj-$(CONFIG_USB_DWC2_PLATFORM) += dwc2_platform.o 25obj-$(CONFIG_USB_DWC2_PLATFORM) += dwc2_platform.o
28dwc2_platform-y := platform.o 26dwc2_platform-y := platform.o
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index f74304b12652..836c012c7707 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -593,6 +593,8 @@ struct dwc2_hsotg {
593 struct dwc2_core_params *core_params; 593 struct dwc2_core_params *core_params;
594 enum usb_otg_state op_state; 594 enum usb_otg_state op_state;
595 enum usb_dr_mode dr_mode; 595 enum usb_dr_mode dr_mode;
596 unsigned int hcd_enabled:1;
597 unsigned int gadget_enabled:1;
596 598
597 struct phy *phy; 599 struct phy *phy;
598 struct usb_phy *uphy; 600 struct usb_phy *uphy;
diff --git a/drivers/usb/dwc2/core_intr.c b/drivers/usb/dwc2/core_intr.c
index 02e3e2d4ea56..6cf047878dba 100644
--- a/drivers/usb/dwc2/core_intr.c
+++ b/drivers/usb/dwc2/core_intr.c
@@ -377,6 +377,9 @@ static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg)
377 dwc2_is_host_mode(hsotg) ? "Host" : "Device", 377 dwc2_is_host_mode(hsotg) ? "Host" : "Device",
378 dwc2_op_state_str(hsotg)); 378 dwc2_op_state_str(hsotg));
379 379
380 if (hsotg->op_state == OTG_STATE_A_HOST)
381 dwc2_hcd_disconnect(hsotg);
382
380 /* Change to L3 (OFF) state */ 383 /* Change to L3 (OFF) state */
381 hsotg->lx_state = DWC2_L3; 384 hsotg->lx_state = DWC2_L3;
382 385
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index c78c8740db1d..fbbbac2150a5 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -257,6 +257,14 @@ static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
257 */ 257 */
258 channel->qh = NULL; 258 channel->qh = NULL;
259 } 259 }
260 /* All channels have been freed, mark them available */
261 if (hsotg->core_params->uframe_sched > 0) {
262 hsotg->available_host_channels =
263 hsotg->core_params->host_channels;
264 } else {
265 hsotg->non_periodic_channels = 0;
266 hsotg->periodic_channels = 0;
267 }
260} 268}
261 269
262/** 270/**
@@ -1527,7 +1535,7 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
1527 hprt0 |= HPRT0_RES; 1535 hprt0 |= HPRT0_RES;
1528 writel(hprt0, hsotg->regs + HPRT0); 1536 writel(hprt0, hsotg->regs + HPRT0);
1529 hprt0 &= ~HPRT0_SUSP; 1537 hprt0 &= ~HPRT0_SUSP;
1530 usleep_range(100000, 150000); 1538 msleep(USB_RESUME_TIMEOUT);
1531 1539
1532 hprt0 &= ~HPRT0_RES; 1540 hprt0 &= ~HPRT0_RES;
1533 writel(hprt0, hsotg->regs + HPRT0); 1541 writel(hprt0, hsotg->regs + HPRT0);
@@ -1608,7 +1616,7 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
1608 dev_dbg(hsotg->dev, "GetHubDescriptor\n"); 1616 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
1609 hub_desc = (struct usb_hub_descriptor *)buf; 1617 hub_desc = (struct usb_hub_descriptor *)buf;
1610 hub_desc->bDescLength = 9; 1618 hub_desc->bDescLength = 9;
1611 hub_desc->bDescriptorType = 0x29; 1619 hub_desc->bDescriptorType = USB_DT_HUB;
1612 hub_desc->bNbrPorts = 1; 1620 hub_desc->bNbrPorts = 1;
1613 hub_desc->wHubCharacteristics = 1621 hub_desc->wHubCharacteristics =
1614 cpu_to_le16(HUB_CHAR_COMMON_LPSM | 1622 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
diff --git a/drivers/usb/dwc2/pci.c b/drivers/usb/dwc2/pci.c
index a4e724b0a62e..ae419615a176 100644
--- a/drivers/usb/dwc2/pci.c
+++ b/drivers/usb/dwc2/pci.c
@@ -50,113 +50,97 @@
50 50
51#include <linux/usb/hcd.h> 51#include <linux/usb/hcd.h>
52#include <linux/usb/ch11.h> 52#include <linux/usb/ch11.h>
53#include <linux/platform_device.h>
54#include <linux/usb/usb_phy_generic.h>
53 55
54#include "core.h"
55#include "hcd.h"
56
57#define PCI_VENDOR_ID_SYNOPSYS 0x16c3
58#define PCI_PRODUCT_ID_HAPS_HSOTG 0xabc0 56#define PCI_PRODUCT_ID_HAPS_HSOTG 0xabc0
59 57
60static const char dwc2_driver_name[] = "dwc2"; 58static const char dwc2_driver_name[] = "dwc2-pci";
61 59
62static const struct dwc2_core_params dwc2_module_params = { 60struct dwc2_pci_glue {
63 .otg_cap = -1, 61 struct platform_device *dwc2;
64 .otg_ver = -1, 62 struct platform_device *phy;
65 .dma_enable = -1,
66 .dma_desc_enable = 0,
67 .speed = -1,
68 .enable_dynamic_fifo = -1,
69 .en_multiple_tx_fifo = -1,
70 .host_rx_fifo_size = 1024,
71 .host_nperio_tx_fifo_size = 256,
72 .host_perio_tx_fifo_size = 1024,
73 .max_transfer_size = 65535,
74 .max_packet_count = 511,
75 .host_channels = -1,
76 .phy_type = -1,
77 .phy_utmi_width = -1,
78 .phy_ulpi_ddr = -1,
79 .phy_ulpi_ext_vbus = -1,
80 .i2c_enable = -1,
81 .ulpi_fs_ls = -1,
82 .host_support_fs_ls_low_power = -1,
83 .host_ls_low_power_phy_clk = -1,
84 .ts_dline = -1,
85 .reload_ctl = -1,
86 .ahbcfg = -1,
87 .uframe_sched = -1,
88}; 63};
89 64
90/** 65static void dwc2_pci_remove(struct pci_dev *pci)
91 * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
92 * DWC_otg driver
93 *
94 * @dev: Bus device
95 *
96 * This routine is called, for example, when the rmmod command is executed. The
97 * device may or may not be electrically present. If it is present, the driver
98 * stops device processing. Any resources used on behalf of this device are
99 * freed.
100 */
101static void dwc2_driver_remove(struct pci_dev *dev)
102{ 66{
103 struct dwc2_hsotg *hsotg = pci_get_drvdata(dev); 67 struct dwc2_pci_glue *glue = pci_get_drvdata(pci);
104 68
105 dwc2_hcd_remove(hsotg); 69 platform_device_unregister(glue->dwc2);
106 pci_disable_device(dev); 70 usb_phy_generic_unregister(glue->phy);
71 kfree(glue);
72 pci_set_drvdata(pci, NULL);
107} 73}
108 74
109/** 75static int dwc2_pci_probe(struct pci_dev *pci,
110 * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg 76 const struct pci_device_id *id)
111 * driver
112 *
113 * @dev: Bus device
114 *
115 * This routine creates the driver components required to control the device
116 * (core, HCD, and PCD) and initializes the device. The driver components are
117 * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
118 * in the device private data. This allows the driver to access the dwc2_hsotg
119 * structure on subsequent calls to driver methods for this device.
120 */
121static int dwc2_driver_probe(struct pci_dev *dev,
122 const struct pci_device_id *id)
123{ 77{
124 struct dwc2_hsotg *hsotg; 78 struct resource res[2];
125 int retval; 79 struct platform_device *dwc2;
80 struct platform_device *phy;
81 int ret;
82 struct device *dev = &pci->dev;
83 struct dwc2_pci_glue *glue;
84
85 ret = pcim_enable_device(pci);
86 if (ret) {
87 dev_err(dev, "failed to enable pci device\n");
88 return -ENODEV;
89 }
90
91 pci_set_master(pci);
126 92
127 hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL); 93 dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
128 if (!hsotg) 94 if (!dwc2) {
95 dev_err(dev, "couldn't allocate dwc2 device\n");
129 return -ENOMEM; 96 return -ENOMEM;
97 }
130 98
131 hsotg->dev = &dev->dev; 99 memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
132 hsotg->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
133 if (IS_ERR(hsotg->regs))
134 return PTR_ERR(hsotg->regs);
135 100
136 dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n", 101 res[0].start = pci_resource_start(pci, 0);
137 (unsigned long)pci_resource_start(dev, 0), hsotg->regs); 102 res[0].end = pci_resource_end(pci, 0);
103 res[0].name = "dwc2";
104 res[0].flags = IORESOURCE_MEM;
138 105
139 if (pci_enable_device(dev) < 0) 106 res[1].start = pci->irq;
140 return -ENODEV; 107 res[1].name = "dwc2";
108 res[1].flags = IORESOURCE_IRQ;
141 109
142 pci_set_master(dev); 110 ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
111 if (ret) {
112 dev_err(dev, "couldn't add resources to dwc2 device\n");
113 return ret;
114 }
143 115
144 retval = devm_request_irq(hsotg->dev, dev->irq, 116 dwc2->dev.parent = dev;
145 dwc2_handle_common_intr, IRQF_SHARED,
146 dev_name(hsotg->dev), hsotg);
147 if (retval)
148 return retval;
149 117
150 spin_lock_init(&hsotg->lock); 118 phy = usb_phy_generic_register();
151 retval = dwc2_hcd_init(hsotg, dev->irq, &dwc2_module_params); 119 if (IS_ERR(phy)) {
152 if (retval) { 120 dev_err(dev, "error registering generic PHY (%ld)\n",
153 pci_disable_device(dev); 121 PTR_ERR(phy));
154 return retval; 122 return PTR_ERR(phy);
155 } 123 }
156 124
157 pci_set_drvdata(dev, hsotg); 125 ret = platform_device_add(dwc2);
126 if (ret) {
127 dev_err(dev, "failed to register dwc2 device\n");
128 goto err;
129 }
130
131 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
132 if (!glue)
133 return -ENOMEM;
134
135 glue->phy = phy;
136 glue->dwc2 = dwc2;
137 pci_set_drvdata(pci, glue);
158 138
159 return retval; 139 return 0;
140err:
141 usb_phy_generic_unregister(phy);
142 platform_device_put(dwc2);
143 return ret;
160} 144}
161 145
162static const struct pci_device_id dwc2_pci_ids[] = { 146static const struct pci_device_id dwc2_pci_ids[] = {
@@ -174,8 +158,8 @@ MODULE_DEVICE_TABLE(pci, dwc2_pci_ids);
174static struct pci_driver dwc2_pci_driver = { 158static struct pci_driver dwc2_pci_driver = {
175 .name = dwc2_driver_name, 159 .name = dwc2_driver_name,
176 .id_table = dwc2_pci_ids, 160 .id_table = dwc2_pci_ids,
177 .probe = dwc2_driver_probe, 161 .probe = dwc2_pci_probe,
178 .remove = dwc2_driver_remove, 162 .remove = dwc2_pci_remove,
179}; 163};
180 164
181module_pci_driver(dwc2_pci_driver); 165module_pci_driver(dwc2_pci_driver);
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index ae095f009b4f..185663e0b5f4 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -121,8 +121,10 @@ static int dwc2_driver_remove(struct platform_device *dev)
121{ 121{
122 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev); 122 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
123 123
124 dwc2_hcd_remove(hsotg); 124 if (hsotg->hcd_enabled)
125 s3c_hsotg_remove(hsotg); 125 dwc2_hcd_remove(hsotg);
126 if (hsotg->gadget_enabled)
127 s3c_hsotg_remove(hsotg);
126 128
127 return 0; 129 return 0;
128} 130}
@@ -234,12 +236,23 @@ static int dwc2_driver_probe(struct platform_device *dev)
234 236
235 spin_lock_init(&hsotg->lock); 237 spin_lock_init(&hsotg->lock);
236 mutex_init(&hsotg->init_mutex); 238 mutex_init(&hsotg->init_mutex);
237 retval = dwc2_gadget_init(hsotg, irq); 239
238 if (retval) 240 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
239 return retval; 241 retval = dwc2_gadget_init(hsotg, irq);
240 retval = dwc2_hcd_init(hsotg, irq, params); 242 if (retval)
241 if (retval) 243 return retval;
242 return retval; 244 hsotg->gadget_enabled = 1;
245 }
246
247 if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
248 retval = dwc2_hcd_init(hsotg, irq, params);
249 if (retval) {
250 if (hsotg->gadget_enabled)
251 s3c_hsotg_remove(hsotg);
252 return retval;
253 }
254 hsotg->hcd_enabled = 1;
255 }
243 256
244 platform_set_drvdata(dev, hsotg); 257 platform_set_drvdata(dev, hsotg);
245 258
diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig
index edbf9c85af7e..827c4f80379f 100644
--- a/drivers/usb/dwc3/Kconfig
+++ b/drivers/usb/dwc3/Kconfig
@@ -104,11 +104,4 @@ config USB_DWC3_DEBUG
104 help 104 help
105 Say Y here to enable debugging messages on DWC3 Driver. 105 Say Y here to enable debugging messages on DWC3 Driver.
106 106
107config DWC3_HOST_USB3_LPM_ENABLE
108 bool "Enable USB3 LPM Capability"
109 depends on USB_DWC3_HOST=y || USB_DWC3_DUAL_ROLE=y
110 default n
111 help
112 Select this when you want to enable USB3 LPM with dwc3 xhci host.
113
114endif 107endif
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 9f0e209b8f6c..2bbab3d86fff 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -774,17 +774,13 @@ static int dwc3_probe(struct platform_device *pdev)
774 * since it will be requested by the xhci-plat driver. 774 * since it will be requested by the xhci-plat driver.
775 */ 775 */
776 regs = devm_ioremap_resource(dev, res); 776 regs = devm_ioremap_resource(dev, res);
777 if (IS_ERR(regs)) 777 if (IS_ERR(regs)) {
778 return PTR_ERR(regs); 778 ret = PTR_ERR(regs);
779 goto err0;
780 }
779 781
780 dwc->regs = regs; 782 dwc->regs = regs;
781 dwc->regs_size = resource_size(res); 783 dwc->regs_size = resource_size(res);
782 /*
783 * restore res->start back to its original value so that,
784 * in case the probe is deferred, we don't end up getting error in
785 * request the memory region the next time probe is called.
786 */
787 res->start -= DWC3_GLOBALS_REGS_START;
788 784
789 /* default to highest possible threshold */ 785 /* default to highest possible threshold */
790 lpm_nyet_threshold = 0xff; 786 lpm_nyet_threshold = 0xff;
@@ -808,6 +804,8 @@ static int dwc3_probe(struct platform_device *pdev)
808 "snps,is-utmi-l1-suspend"); 804 "snps,is-utmi-l1-suspend");
809 of_property_read_u8(node, "snps,hird-threshold", 805 of_property_read_u8(node, "snps,hird-threshold",
810 &hird_threshold); 806 &hird_threshold);
807 dwc->usb3_lpm_capable = of_property_read_bool(node,
808 "snps,usb3_lpm_capable");
811 809
812 dwc->needs_fifo_resize = of_property_read_bool(node, 810 dwc->needs_fifo_resize = of_property_read_bool(node,
813 "tx-fifo-resize"); 811 "tx-fifo-resize");
@@ -848,6 +846,7 @@ static int dwc3_probe(struct platform_device *pdev)
848 hird_threshold = pdata->hird_threshold; 846 hird_threshold = pdata->hird_threshold;
849 847
850 dwc->needs_fifo_resize = pdata->tx_fifo_resize; 848 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
849 dwc->usb3_lpm_capable = pdata->usb3_lpm_capable;
851 dwc->dr_mode = pdata->dr_mode; 850 dwc->dr_mode = pdata->dr_mode;
852 851
853 dwc->disable_scramble_quirk = pdata->disable_scramble_quirk; 852 dwc->disable_scramble_quirk = pdata->disable_scramble_quirk;
@@ -878,7 +877,7 @@ static int dwc3_probe(struct platform_device *pdev)
878 877
879 ret = dwc3_core_get_phy(dwc); 878 ret = dwc3_core_get_phy(dwc);
880 if (ret) 879 if (ret)
881 return ret; 880 goto err0;
882 881
883 spin_lock_init(&dwc->lock); 882 spin_lock_init(&dwc->lock);
884 platform_set_drvdata(pdev, dwc); 883 platform_set_drvdata(pdev, dwc);
@@ -899,7 +898,7 @@ static int dwc3_probe(struct platform_device *pdev)
899 if (ret) { 898 if (ret) {
900 dev_err(dwc->dev, "failed to allocate event buffers\n"); 899 dev_err(dwc->dev, "failed to allocate event buffers\n");
901 ret = -ENOMEM; 900 ret = -ENOMEM;
902 goto err0; 901 goto err1;
903 } 902 }
904 903
905 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) 904 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
@@ -913,65 +912,81 @@ static int dwc3_probe(struct platform_device *pdev)
913 ret = dwc3_core_init(dwc); 912 ret = dwc3_core_init(dwc);
914 if (ret) { 913 if (ret) {
915 dev_err(dev, "failed to initialize core\n"); 914 dev_err(dev, "failed to initialize core\n");
916 goto err0; 915 goto err1;
917 } 916 }
918 917
919 usb_phy_set_suspend(dwc->usb2_phy, 0); 918 usb_phy_set_suspend(dwc->usb2_phy, 0);
920 usb_phy_set_suspend(dwc->usb3_phy, 0); 919 usb_phy_set_suspend(dwc->usb3_phy, 0);
921 ret = phy_power_on(dwc->usb2_generic_phy); 920 ret = phy_power_on(dwc->usb2_generic_phy);
922 if (ret < 0) 921 if (ret < 0)
923 goto err1; 922 goto err2;
924 923
925 ret = phy_power_on(dwc->usb3_generic_phy); 924 ret = phy_power_on(dwc->usb3_generic_phy);
926 if (ret < 0) 925 if (ret < 0)
927 goto err_usb2phy_power; 926 goto err3;
928 927
929 ret = dwc3_event_buffers_setup(dwc); 928 ret = dwc3_event_buffers_setup(dwc);
930 if (ret) { 929 if (ret) {
931 dev_err(dwc->dev, "failed to setup event buffers\n"); 930 dev_err(dwc->dev, "failed to setup event buffers\n");
932 goto err_usb3phy_power; 931 goto err4;
933 } 932 }
934 933
935 ret = dwc3_core_init_mode(dwc); 934 ret = dwc3_core_init_mode(dwc);
936 if (ret) 935 if (ret)
937 goto err2; 936 goto err5;
938 937
939 ret = dwc3_debugfs_init(dwc); 938 ret = dwc3_debugfs_init(dwc);
940 if (ret) { 939 if (ret) {
941 dev_err(dev, "failed to initialize debugfs\n"); 940 dev_err(dev, "failed to initialize debugfs\n");
942 goto err3; 941 goto err6;
943 } 942 }
944 943
945 pm_runtime_allow(dev); 944 pm_runtime_allow(dev);
946 945
947 return 0; 946 return 0;
948 947
949err3: 948err6:
950 dwc3_core_exit_mode(dwc); 949 dwc3_core_exit_mode(dwc);
951 950
952err2: 951err5:
953 dwc3_event_buffers_cleanup(dwc); 952 dwc3_event_buffers_cleanup(dwc);
954 953
955err_usb3phy_power: 954err4:
956 phy_power_off(dwc->usb3_generic_phy); 955 phy_power_off(dwc->usb3_generic_phy);
957 956
958err_usb2phy_power: 957err3:
959 phy_power_off(dwc->usb2_generic_phy); 958 phy_power_off(dwc->usb2_generic_phy);
960 959
961err1: 960err2:
962 usb_phy_set_suspend(dwc->usb2_phy, 1); 961 usb_phy_set_suspend(dwc->usb2_phy, 1);
963 usb_phy_set_suspend(dwc->usb3_phy, 1); 962 usb_phy_set_suspend(dwc->usb3_phy, 1);
964 dwc3_core_exit(dwc); 963 dwc3_core_exit(dwc);
965 964
966err0: 965err1:
967 dwc3_free_event_buffers(dwc); 966 dwc3_free_event_buffers(dwc);
968 967
968err0:
969 /*
970 * restore res->start back to its original value so that, in case the
971 * probe is deferred, we don't end up getting error in request the
972 * memory region the next time probe is called.
973 */
974 res->start -= DWC3_GLOBALS_REGS_START;
975
969 return ret; 976 return ret;
970} 977}
971 978
972static int dwc3_remove(struct platform_device *pdev) 979static int dwc3_remove(struct platform_device *pdev)
973{ 980{
974 struct dwc3 *dwc = platform_get_drvdata(pdev); 981 struct dwc3 *dwc = platform_get_drvdata(pdev);
982 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
983
984 /*
985 * restore res->start back to its original value so that, in case the
986 * probe is deferred, we don't end up getting error in request the
987 * memory region the next time probe is called.
988 */
989 res->start -= DWC3_GLOBALS_REGS_START;
975 990
976 dwc3_debugfs_exit(dwc); 991 dwc3_debugfs_exit(dwc);
977 dwc3_core_exit_mode(dwc); 992 dwc3_core_exit_mode(dwc);
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index d201910b892f..fdab715a0631 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -689,6 +689,7 @@ struct dwc3_scratchpad_array {
689 * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround 689 * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
690 * @start_config_issued: true when StartConfig command has been issued 690 * @start_config_issued: true when StartConfig command has been issued
691 * @three_stage_setup: set if we perform a three phase setup 691 * @three_stage_setup: set if we perform a three phase setup
692 * @usb3_lpm_capable: set if hadrware supports Link Power Management
692 * @disable_scramble_quirk: set if we enable the disable scramble quirk 693 * @disable_scramble_quirk: set if we enable the disable scramble quirk
693 * @u2exit_lfps_quirk: set if we enable u2exit lfps quirk 694 * @u2exit_lfps_quirk: set if we enable u2exit lfps quirk
694 * @u2ss_inp3_quirk: set if we enable P3 OK for U2/SS Inactive quirk 695 * @u2ss_inp3_quirk: set if we enable P3 OK for U2/SS Inactive quirk
@@ -812,6 +813,7 @@ struct dwc3 {
812 unsigned setup_packet_pending:1; 813 unsigned setup_packet_pending:1;
813 unsigned start_config_issued:1; 814 unsigned start_config_issued:1;
814 unsigned three_stage_setup:1; 815 unsigned three_stage_setup:1;
816 unsigned usb3_lpm_capable:1;
815 817
816 unsigned disable_scramble_quirk:1; 818 unsigned disable_scramble_quirk:1;
817 unsigned u2exit_lfps_quirk:1; 819 unsigned u2exit_lfps_quirk:1;
diff --git a/drivers/usb/dwc3/dwc3-omap.c b/drivers/usb/dwc3/dwc3-omap.c
index 52e0c4e5e48e..edba5348be18 100644
--- a/drivers/usb/dwc3/dwc3-omap.c
+++ b/drivers/usb/dwc3/dwc3-omap.c
@@ -325,15 +325,6 @@ static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
325 return IRQ_HANDLED; 325 return IRQ_HANDLED;
326} 326}
327 327
328static int dwc3_omap_remove_core(struct device *dev, void *c)
329{
330 struct platform_device *pdev = to_platform_device(dev);
331
332 of_device_unregister(pdev);
333
334 return 0;
335}
336
337static void dwc3_omap_enable_irqs(struct dwc3_omap *omap) 328static void dwc3_omap_enable_irqs(struct dwc3_omap *omap)
338{ 329{
339 u32 reg; 330 u32 reg;
@@ -600,7 +591,7 @@ static int dwc3_omap_remove(struct platform_device *pdev)
600 if (omap->extcon_id_dev.edev) 591 if (omap->extcon_id_dev.edev)
601 extcon_unregister_interest(&omap->extcon_id_dev); 592 extcon_unregister_interest(&omap->extcon_id_dev);
602 dwc3_omap_disable_irqs(omap); 593 dwc3_omap_disable_irqs(omap);
603 device_for_each_child(&pdev->dev, NULL, dwc3_omap_remove_core); 594 of_platform_depopulate(omap->dev);
604 pm_runtime_put_sync(&pdev->dev); 595 pm_runtime_put_sync(&pdev->dev);
605 pm_runtime_disable(&pdev->dev); 596 pm_runtime_disable(&pdev->dev);
606 597
diff --git a/drivers/usb/dwc3/dwc3-pci.c b/drivers/usb/dwc3/dwc3-pci.c
index 8d950569d557..b773fb53d6a7 100644
--- a/drivers/usb/dwc3/dwc3-pci.c
+++ b/drivers/usb/dwc3/dwc3-pci.c
@@ -24,8 +24,6 @@
24 24
25#include "platform_data.h" 25#include "platform_data.h"
26 26
27/* FIXME define these in <linux/pci_ids.h> */
28#define PCI_VENDOR_ID_SYNOPSYS 0x16c3
29#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd 27#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
30#define PCI_DEVICE_ID_INTEL_BYT 0x0f37 28#define PCI_DEVICE_ID_INTEL_BYT 0x0f37
31#define PCI_DEVICE_ID_INTEL_MRFLD 0x119e 29#define PCI_DEVICE_ID_INTEL_MRFLD 0x119e
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index a03a485205c7..8946c32047e9 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -1855,32 +1855,27 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1855 unsigned int i; 1855 unsigned int i;
1856 int ret; 1856 int ret;
1857 1857
1858 req = next_request(&dep->req_queued);
1859 if (!req) {
1860 WARN_ON_ONCE(1);
1861 return 1;
1862 }
1863 i = 0;
1858 do { 1864 do {
1859 req = next_request(&dep->req_queued); 1865 slot = req->start_slot + i;
1860 if (!req) { 1866 if ((slot == DWC3_TRB_NUM - 1) &&
1861 WARN_ON_ONCE(1);
1862 return 1;
1863 }
1864 i = 0;
1865 do {
1866 slot = req->start_slot + i;
1867 if ((slot == DWC3_TRB_NUM - 1) &&
1868 usb_endpoint_xfer_isoc(dep->endpoint.desc)) 1867 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1869 slot++; 1868 slot++;
1870 slot %= DWC3_TRB_NUM; 1869 slot %= DWC3_TRB_NUM;
1871 trb = &dep->trb_pool[slot]; 1870 trb = &dep->trb_pool[slot];
1872
1873 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1874 event, status);
1875 if (ret)
1876 break;
1877 }while (++i < req->request.num_mapped_sgs);
1878
1879 dwc3_gadget_giveback(dep, req, status);
1880 1871
1872 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1873 event, status);
1881 if (ret) 1874 if (ret)
1882 break; 1875 break;
1883 } while (1); 1876 } while (++i < req->request.num_mapped_sgs);
1877
1878 dwc3_gadget_giveback(dep, req, status);
1884 1879
1885 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 1880 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1886 list_empty(&dep->req_queued)) { 1881 list_empty(&dep->req_queued)) {
diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c
index 12bfd3c5405e..c679f63783ae 100644
--- a/drivers/usb/dwc3/host.c
+++ b/drivers/usb/dwc3/host.c
@@ -49,9 +49,7 @@ int dwc3_host_init(struct dwc3 *dwc)
49 49
50 memset(&pdata, 0, sizeof(pdata)); 50 memset(&pdata, 0, sizeof(pdata));
51 51
52#ifdef CONFIG_DWC3_HOST_USB3_LPM_ENABLE 52 pdata.usb3_lpm_capable = dwc->usb3_lpm_capable;
53 pdata.usb3_lpm_capable = 1;
54#endif
55 53
56 ret = platform_device_add_data(xhci, &pdata, sizeof(pdata)); 54 ret = platform_device_add_data(xhci, &pdata, sizeof(pdata));
57 if (ret) { 55 if (ret) {
diff --git a/drivers/usb/dwc3/platform_data.h b/drivers/usb/dwc3/platform_data.h
index a3a3b6d5668c..a2bd464be828 100644
--- a/drivers/usb/dwc3/platform_data.h
+++ b/drivers/usb/dwc3/platform_data.h
@@ -24,6 +24,7 @@ struct dwc3_platform_data {
24 enum usb_device_speed maximum_speed; 24 enum usb_device_speed maximum_speed;
25 enum usb_dr_mode dr_mode; 25 enum usb_dr_mode dr_mode;
26 bool tx_fifo_resize; 26 bool tx_fifo_resize;
27 bool usb3_lpm_capable;
27 28
28 unsigned is_utmi_l1_suspend:1; 29 unsigned is_utmi_l1_suspend:1;
29 u8 hird_threshold; 30 u8 hird_threshold;
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index b454d05be583..bcf83c0a6e62 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -196,6 +196,9 @@ config USB_F_MIDI
196config USB_F_HID 196config USB_F_HID
197 tristate 197 tristate
198 198
199config USB_F_PRINTER
200 tristate
201
199choice 202choice
200 tristate "USB Gadget Drivers" 203 tristate "USB Gadget Drivers"
201 default USB_ETH 204 default USB_ETH
@@ -434,6 +437,20 @@ config USB_CONFIGFS_F_UVC
434 device. It provides a userspace API to process UVC control requests 437 device. It provides a userspace API to process UVC control requests
435 and stream video data to the host. 438 and stream video data to the host.
436 439
440config USB_CONFIGFS_F_PRINTER
441 bool "Printer function"
442 select USB_F_PRINTER
443 depends on USB_CONFIGFS
444 help
445 The Printer function channels data between the USB host and a
446 userspace program driving the print engine. The user space
447 program reads and writes the device file /dev/g_printer<X> to
448 receive or send printer data. It can use ioctl calls to
449 the device file to get or set printer status.
450
451 For more information, see Documentation/usb/gadget_printer.txt
452 which includes sample code for accessing the device file.
453
437source "drivers/usb/gadget/legacy/Kconfig" 454source "drivers/usb/gadget/legacy/Kconfig"
438 455
439endchoice 456endchoice
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 13adfd1a3f54..4e3447bbd097 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1161,11 +1161,11 @@ static struct usb_gadget_string_container *copy_gadget_strings(
1161 * This function will create a deep copy of usb_gadget_strings and usb_string 1161 * This function will create a deep copy of usb_gadget_strings and usb_string
1162 * and attach it to the cdev. The actual string (usb_string.s) will not be 1162 * and attach it to the cdev. The actual string (usb_string.s) will not be
1163 * copied but only a referenced will be made. The struct usb_gadget_strings 1163 * copied but only a referenced will be made. The struct usb_gadget_strings
1164 * array may contain multiple languges and should be NULL terminated. 1164 * array may contain multiple languages and should be NULL terminated.
1165 * The ->language pointer of each struct usb_gadget_strings has to contain the 1165 * The ->language pointer of each struct usb_gadget_strings has to contain the
1166 * same amount of entries. 1166 * same amount of entries.
1167 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first 1167 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1168 * usb_string entry of es-ES containts the translation of the first usb_string 1168 * usb_string entry of es-ES contains the translation of the first usb_string
1169 * entry of en-US. Therefore both entries become the same id assign. 1169 * entry of en-US. Therefore both entries become the same id assign.
1170 */ 1170 */
1171struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, 1171struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
@@ -1472,6 +1472,13 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1472 req->length = 0; 1472 req->length = 0;
1473 gadget->ep0->driver_data = cdev; 1473 gadget->ep0->driver_data = cdev;
1474 1474
1475 /*
1476 * Don't let non-standard requests match any of the cases below
1477 * by accident.
1478 */
1479 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1480 goto unknown;
1481
1475 switch (ctrl->bRequest) { 1482 switch (ctrl->bRequest) {
1476 1483
1477 /* we handle all standard USB descriptors */ 1484 /* we handle all standard USB descriptors */
@@ -1751,6 +1758,10 @@ unknown:
1751 * take such requests too, if that's ever needed: to work 1758 * take such requests too, if that's ever needed: to work
1752 * in config 0, etc. 1759 * in config 0, etc.
1753 */ 1760 */
1761 list_for_each_entry(f, &cdev->config->functions, list)
1762 if (f->req_match && f->req_match(f, ctrl))
1763 goto try_fun_setup;
1764 f = NULL;
1754 switch (ctrl->bRequestType & USB_RECIP_MASK) { 1765 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1755 case USB_RECIP_INTERFACE: 1766 case USB_RECIP_INTERFACE:
1756 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1767 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
@@ -1768,7 +1779,7 @@ unknown:
1768 f = NULL; 1779 f = NULL;
1769 break; 1780 break;
1770 } 1781 }
1771 1782try_fun_setup:
1772 if (f && f->setup) 1783 if (f && f->setup)
1773 value = f->setup(f, ctrl); 1784 value = f->setup(f, ctrl);
1774 else { 1785 else {
diff --git a/drivers/usb/gadget/function/Makefile b/drivers/usb/gadget/function/Makefile
index f71b1aaa0edf..bd7def576955 100644
--- a/drivers/usb/gadget/function/Makefile
+++ b/drivers/usb/gadget/function/Makefile
@@ -42,3 +42,5 @@ usb_f_midi-y := f_midi.o
42obj-$(CONFIG_USB_F_MIDI) += usb_f_midi.o 42obj-$(CONFIG_USB_F_MIDI) += usb_f_midi.o
43usb_f_hid-y := f_hid.o 43usb_f_hid-y := f_hid.o
44obj-$(CONFIG_USB_F_HID) += usb_f_hid.o 44obj-$(CONFIG_USB_F_HID) += usb_f_hid.o
45usb_f_printer-y := f_printer.o
46obj-$(CONFIG_USB_F_PRINTER) += usb_f_printer.o
diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index a2612fb79eff..13dfc9915b1d 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -908,7 +908,6 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
908 908
909 /* disable/free request and end point */ 909 /* disable/free request and end point */
910 usb_ep_disable(hidg->in_ep); 910 usb_ep_disable(hidg->in_ep);
911 usb_ep_dequeue(hidg->in_ep, hidg->req);
912 kfree(hidg->req->buf); 911 kfree(hidg->req->buf);
913 usb_ep_free_request(hidg->in_ep, hidg->req); 912 usb_ep_free_request(hidg->in_ep, hidg->req);
914 913
diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 298b46112b1a..39f49f1ad22f 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -289,8 +289,7 @@ static void disable_loopback(struct f_loopback *loop)
289 struct usb_composite_dev *cdev; 289 struct usb_composite_dev *cdev;
290 290
291 cdev = loop->function.config->cdev; 291 cdev = loop->function.config->cdev;
292 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL, NULL, 292 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
293 NULL);
294 VDBG(cdev, "%s disabled\n", loop->function.name); 293 VDBG(cdev, "%s disabled\n", loop->function.name);
295} 294}
296 295
diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 811929cd4c9e..3cc109f3c9c8 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -1085,7 +1085,7 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1085 if (!curlun) { /* Unsupported LUNs are okay */ 1085 if (!curlun) { /* Unsupported LUNs are okay */
1086 common->bad_lun_okay = 1; 1086 common->bad_lun_okay = 1;
1087 memset(buf, 0, 36); 1087 memset(buf, 0, 36);
1088 buf[0] = 0x7f; /* Unsupported, no device-type */ 1088 buf[0] = TYPE_NO_LUN; /* Unsupported, no device-type */
1089 buf[4] = 31; /* Additional length */ 1089 buf[4] = 31; /* Additional length */
1090 return 36; 1090 return 36;
1091 } 1091 }
@@ -2624,13 +2624,10 @@ static ssize_t file_store(struct device *dev, struct device_attribute *attr,
2624 return fsg_store_file(curlun, filesem, buf, count); 2624 return fsg_store_file(curlun, filesem, buf, count);
2625} 2625}
2626 2626
2627static DEVICE_ATTR_RW(ro);
2628static DEVICE_ATTR_RW(nofua); 2627static DEVICE_ATTR_RW(nofua);
2629static DEVICE_ATTR_RW(file); 2628/* mode wil be set in fsg_lun_attr_is_visible() */
2630 2629static DEVICE_ATTR(ro, 0, ro_show, ro_store);
2631static struct device_attribute dev_attr_ro_cdrom = __ATTR_RO(ro); 2630static DEVICE_ATTR(file, 0, file_show, file_store);
2632static struct device_attribute dev_attr_file_nonremovable = __ATTR_RO(file);
2633
2634 2631
2635/****************************** FSG COMMON ******************************/ 2632/****************************** FSG COMMON ******************************/
2636 2633
@@ -2745,40 +2742,10 @@ error_release:
2745} 2742}
2746EXPORT_SYMBOL_GPL(fsg_common_set_num_buffers); 2743EXPORT_SYMBOL_GPL(fsg_common_set_num_buffers);
2747 2744
2748static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
2749{
2750 device_remove_file(&lun->dev, &dev_attr_nofua);
2751 /*
2752 * device_remove_file() =>
2753 *
2754 * here the attr (e.g. dev_attr_ro) is only used to be passed to:
2755 *
2756 * sysfs_remove_file() =>
2757 *
2758 * here e.g. both dev_attr_ro_cdrom and dev_attr_ro are in
2759 * the same namespace and
2760 * from here only attr->name is passed to:
2761 *
2762 * sysfs_hash_and_remove()
2763 *
2764 * attr->name is the same for dev_attr_ro_cdrom and
2765 * dev_attr_ro
2766 * attr->name is the same for dev_attr_file and
2767 * dev_attr_file_nonremovable
2768 *
2769 * so we don't differentiate between removing e.g. dev_attr_ro_cdrom
2770 * and dev_attr_ro
2771 */
2772 device_remove_file(&lun->dev, &dev_attr_ro);
2773 device_remove_file(&lun->dev, &dev_attr_file);
2774}
2775
2776void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs) 2745void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs)
2777{ 2746{
2778 if (sysfs) { 2747 if (sysfs)
2779 fsg_common_remove_sysfs(lun);
2780 device_unregister(&lun->dev); 2748 device_unregister(&lun->dev);
2781 }
2782 fsg_lun_close(lun); 2749 fsg_lun_close(lun);
2783 kfree(lun); 2750 kfree(lun);
2784} 2751}
@@ -2877,41 +2844,35 @@ int fsg_common_set_cdev(struct fsg_common *common,
2877} 2844}
2878EXPORT_SYMBOL_GPL(fsg_common_set_cdev); 2845EXPORT_SYMBOL_GPL(fsg_common_set_cdev);
2879 2846
2880static inline int fsg_common_add_sysfs(struct fsg_common *common, 2847static struct attribute *fsg_lun_dev_attrs[] = {
2881 struct fsg_lun *lun) 2848 &dev_attr_ro.attr,
2882{ 2849 &dev_attr_file.attr,
2883 int rc; 2850 &dev_attr_nofua.attr,
2851 NULL
2852};
2884 2853
2885 rc = device_register(&lun->dev); 2854static umode_t fsg_lun_dev_is_visible(struct kobject *kobj,
2886 if (rc) { 2855 struct attribute *attr, int idx)
2887 put_device(&lun->dev); 2856{
2888 return rc; 2857 struct device *dev = kobj_to_dev(kobj);
2889 } 2858 struct fsg_lun *lun = fsg_lun_from_dev(dev);
2890 2859
2891 rc = device_create_file(&lun->dev, 2860 if (attr == &dev_attr_ro.attr)
2892 lun->cdrom 2861 return lun->cdrom ? S_IRUGO : (S_IWUSR | S_IRUGO);
2893 ? &dev_attr_ro_cdrom 2862 if (attr == &dev_attr_file.attr)
2894 : &dev_attr_ro); 2863 return lun->removable ? (S_IWUSR | S_IRUGO) : S_IRUGO;
2895 if (rc) 2864 return attr->mode;
2896 goto error; 2865}
2897 rc = device_create_file(&lun->dev,
2898 lun->removable
2899 ? &dev_attr_file
2900 : &dev_attr_file_nonremovable);
2901 if (rc)
2902 goto error;
2903 rc = device_create_file(&lun->dev, &dev_attr_nofua);
2904 if (rc)
2905 goto error;
2906 2866
2907 return 0; 2867static const struct attribute_group fsg_lun_dev_group = {
2868 .attrs = fsg_lun_dev_attrs,
2869 .is_visible = fsg_lun_dev_is_visible,
2870};
2908 2871
2909error: 2872static const struct attribute_group *fsg_lun_dev_groups[] = {
2910 /* removing nonexistent files is a no-op */ 2873 &fsg_lun_dev_group,
2911 fsg_common_remove_sysfs(lun); 2874 NULL
2912 device_unregister(&lun->dev); 2875};
2913 return rc;
2914}
2915 2876
2916int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, 2877int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
2917 unsigned int id, const char *name, 2878 unsigned int id, const char *name,
@@ -2949,13 +2910,15 @@ int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
2949 } else { 2910 } else {
2950 lun->dev.release = fsg_lun_release; 2911 lun->dev.release = fsg_lun_release;
2951 lun->dev.parent = &common->gadget->dev; 2912 lun->dev.parent = &common->gadget->dev;
2913 lun->dev.groups = fsg_lun_dev_groups;
2952 dev_set_drvdata(&lun->dev, &common->filesem); 2914 dev_set_drvdata(&lun->dev, &common->filesem);
2953 dev_set_name(&lun->dev, "%s", name); 2915 dev_set_name(&lun->dev, "%s", name);
2954 lun->name = dev_name(&lun->dev); 2916 lun->name = dev_name(&lun->dev);
2955 2917
2956 rc = fsg_common_add_sysfs(common, lun); 2918 rc = device_register(&lun->dev);
2957 if (rc) { 2919 if (rc) {
2958 pr_info("failed to register LUN%d: %d\n", id, rc); 2920 pr_info("failed to register LUN%d: %d\n", id, rc);
2921 put_device(&lun->dev);
2959 goto error_sysfs; 2922 goto error_sysfs;
2960 } 2923 }
2961 } 2924 }
@@ -2988,10 +2951,8 @@ int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
2988 return 0; 2951 return 0;
2989 2952
2990error_lun: 2953error_lun:
2991 if (common->sysfs) { 2954 if (common->sysfs)
2992 fsg_common_remove_sysfs(lun);
2993 device_unregister(&lun->dev); 2955 device_unregister(&lun->dev);
2994 }
2995 fsg_lun_close(lun); 2956 fsg_lun_close(lun);
2996 common->luns[id] = NULL; 2957 common->luns[id] = NULL;
2997error_sysfs: 2958error_sysfs:
@@ -3077,8 +3038,6 @@ static void fsg_common_release(struct kref *ref)
3077 struct fsg_lun *lun = *lun_it; 3038 struct fsg_lun *lun = *lun_it;
3078 if (!lun) 3039 if (!lun)
3079 continue; 3040 continue;
3080 if (common->sysfs)
3081 fsg_common_remove_sysfs(lun);
3082 fsg_lun_close(lun); 3041 fsg_lun_close(lun);
3083 if (common->sysfs) 3042 if (common->sysfs)
3084 device_unregister(&lun->dev); 3043 device_unregister(&lun->dev);
diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
new file mode 100644
index 000000000000..44173df27273
--- /dev/null
+++ b/drivers/usb/gadget/function/f_printer.c
@@ -0,0 +1,1471 @@
1/*
2 * f_printer.c - USB printer function driver
3 *
4 * Copied from drivers/usb/gadget/legacy/printer.c,
5 * which was:
6 *
7 * printer.c -- Printer gadget driver
8 *
9 * Copyright (C) 2003-2005 David Brownell
10 * Copyright (C) 2006 Craig W. Nadler
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/ioport.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/mutex.h>
25#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/idr.h>
28#include <linux/timer.h>
29#include <linux/list.h>
30#include <linux/interrupt.h>
31#include <linux/device.h>
32#include <linux/moduleparam.h>
33#include <linux/fs.h>
34#include <linux/poll.h>
35#include <linux/types.h>
36#include <linux/ctype.h>
37#include <linux/cdev.h>
38
39#include <asm/byteorder.h>
40#include <linux/io.h>
41#include <linux/irq.h>
42#include <linux/uaccess.h>
43#include <asm/unaligned.h>
44
45#include <linux/usb/ch9.h>
46#include <linux/usb/composite.h>
47#include <linux/usb/gadget.h>
48#include <linux/usb/g_printer.h>
49
50#include "u_printer.h"
51
52#define PNP_STRING_LEN 1024
53#define PRINTER_MINORS 4
54#define GET_DEVICE_ID 0
55#define GET_PORT_STATUS 1
56#define SOFT_RESET 2
57
58static int major, minors;
59static struct class *usb_gadget_class;
60static DEFINE_IDA(printer_ida);
61static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
62
63/*-------------------------------------------------------------------------*/
64
65struct printer_dev {
66 spinlock_t lock; /* lock this structure */
67 /* lock buffer lists during read/write calls */
68 struct mutex lock_printer_io;
69 struct usb_gadget *gadget;
70 s8 interface;
71 struct usb_ep *in_ep, *out_ep;
72
73 struct list_head rx_reqs; /* List of free RX structs */
74 struct list_head rx_reqs_active; /* List of Active RX xfers */
75 struct list_head rx_buffers; /* List of completed xfers */
76 /* wait until there is data to be read. */
77 wait_queue_head_t rx_wait;
78 struct list_head tx_reqs; /* List of free TX structs */
79 struct list_head tx_reqs_active; /* List of Active TX xfers */
80 /* Wait until there are write buffers available to use. */
81 wait_queue_head_t tx_wait;
82 /* Wait until all write buffers have been sent. */
83 wait_queue_head_t tx_flush_wait;
84 struct usb_request *current_rx_req;
85 size_t current_rx_bytes;
86 u8 *current_rx_buf;
87 u8 printer_status;
88 u8 reset_printer;
89 int minor;
90 struct cdev printer_cdev;
91 u8 printer_cdev_open;
92 wait_queue_head_t wait;
93 unsigned q_len;
94 char *pnp_string; /* We don't own memory! */
95 struct usb_function function;
96};
97
98static inline struct printer_dev *func_to_printer(struct usb_function *f)
99{
100 return container_of(f, struct printer_dev, function);
101}
102
103/*-------------------------------------------------------------------------*/
104
105/*
106 * DESCRIPTORS ... most are static, but strings and (full) configuration
107 * descriptors are built on demand.
108 */
109
110/* holds our biggest descriptor */
111#define USB_DESC_BUFSIZE 256
112#define USB_BUFSIZE 8192
113
114static struct usb_interface_descriptor intf_desc = {
115 .bLength = sizeof(intf_desc),
116 .bDescriptorType = USB_DT_INTERFACE,
117 .bNumEndpoints = 2,
118 .bInterfaceClass = USB_CLASS_PRINTER,
119 .bInterfaceSubClass = 1, /* Printer Sub-Class */
120 .bInterfaceProtocol = 2, /* Bi-Directional */
121 .iInterface = 0
122};
123
124static struct usb_endpoint_descriptor fs_ep_in_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127 .bEndpointAddress = USB_DIR_IN,
128 .bmAttributes = USB_ENDPOINT_XFER_BULK
129};
130
131static struct usb_endpoint_descriptor fs_ep_out_desc = {
132 .bLength = USB_DT_ENDPOINT_SIZE,
133 .bDescriptorType = USB_DT_ENDPOINT,
134 .bEndpointAddress = USB_DIR_OUT,
135 .bmAttributes = USB_ENDPOINT_XFER_BULK
136};
137
138static struct usb_descriptor_header *fs_printer_function[] = {
139 (struct usb_descriptor_header *) &intf_desc,
140 (struct usb_descriptor_header *) &fs_ep_in_desc,
141 (struct usb_descriptor_header *) &fs_ep_out_desc,
142 NULL
143};
144
145/*
146 * usb 2.0 devices need to expose both high speed and full speed
147 * descriptors, unless they only run at full speed.
148 */
149
150static struct usb_endpoint_descriptor hs_ep_in_desc = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 .wMaxPacketSize = cpu_to_le16(512)
155};
156
157static struct usb_endpoint_descriptor hs_ep_out_desc = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK,
161 .wMaxPacketSize = cpu_to_le16(512)
162};
163
164static struct usb_qualifier_descriptor dev_qualifier = {
165 .bLength = sizeof(dev_qualifier),
166 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
167 .bcdUSB = cpu_to_le16(0x0200),
168 .bDeviceClass = USB_CLASS_PRINTER,
169 .bNumConfigurations = 1
170};
171
172static struct usb_descriptor_header *hs_printer_function[] = {
173 (struct usb_descriptor_header *) &intf_desc,
174 (struct usb_descriptor_header *) &hs_ep_in_desc,
175 (struct usb_descriptor_header *) &hs_ep_out_desc,
176 NULL
177};
178
179/*
180 * Added endpoint descriptors for 3.0 devices
181 */
182
183static struct usb_endpoint_descriptor ss_ep_in_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 .wMaxPacketSize = cpu_to_le16(1024),
188};
189
190static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
191 .bLength = sizeof(ss_ep_in_comp_desc),
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
193};
194
195static struct usb_endpoint_descriptor ss_ep_out_desc = {
196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198 .bmAttributes = USB_ENDPOINT_XFER_BULK,
199 .wMaxPacketSize = cpu_to_le16(1024),
200};
201
202static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
203 .bLength = sizeof(ss_ep_out_comp_desc),
204 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
205};
206
207static struct usb_descriptor_header *ss_printer_function[] = {
208 (struct usb_descriptor_header *) &intf_desc,
209 (struct usb_descriptor_header *) &ss_ep_in_desc,
210 (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
211 (struct usb_descriptor_header *) &ss_ep_out_desc,
212 (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
213 NULL
214};
215
216/* maxpacket and other transfer characteristics vary by speed. */
217static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
218 struct usb_endpoint_descriptor *fs,
219 struct usb_endpoint_descriptor *hs,
220 struct usb_endpoint_descriptor *ss)
221{
222 switch (gadget->speed) {
223 case USB_SPEED_SUPER:
224 return ss;
225 case USB_SPEED_HIGH:
226 return hs;
227 default:
228 return fs;
229 }
230}
231
232/*-------------------------------------------------------------------------*/
233
234static struct usb_request *
235printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
236{
237 struct usb_request *req;
238
239 req = usb_ep_alloc_request(ep, gfp_flags);
240
241 if (req != NULL) {
242 req->length = len;
243 req->buf = kmalloc(len, gfp_flags);
244 if (req->buf == NULL) {
245 usb_ep_free_request(ep, req);
246 return NULL;
247 }
248 }
249
250 return req;
251}
252
253static void
254printer_req_free(struct usb_ep *ep, struct usb_request *req)
255{
256 if (ep != NULL && req != NULL) {
257 kfree(req->buf);
258 usb_ep_free_request(ep, req);
259 }
260}
261
262/*-------------------------------------------------------------------------*/
263
264static void rx_complete(struct usb_ep *ep, struct usb_request *req)
265{
266 struct printer_dev *dev = ep->driver_data;
267 int status = req->status;
268 unsigned long flags;
269
270 spin_lock_irqsave(&dev->lock, flags);
271
272 list_del_init(&req->list); /* Remode from Active List */
273
274 switch (status) {
275
276 /* normal completion */
277 case 0:
278 if (req->actual > 0) {
279 list_add_tail(&req->list, &dev->rx_buffers);
280 DBG(dev, "G_Printer : rx length %d\n", req->actual);
281 } else {
282 list_add(&req->list, &dev->rx_reqs);
283 }
284 break;
285
286 /* software-driven interface shutdown */
287 case -ECONNRESET: /* unlink */
288 case -ESHUTDOWN: /* disconnect etc */
289 VDBG(dev, "rx shutdown, code %d\n", status);
290 list_add(&req->list, &dev->rx_reqs);
291 break;
292
293 /* for hardware automagic (such as pxa) */
294 case -ECONNABORTED: /* endpoint reset */
295 DBG(dev, "rx %s reset\n", ep->name);
296 list_add(&req->list, &dev->rx_reqs);
297 break;
298
299 /* data overrun */
300 case -EOVERFLOW:
301 /* FALLTHROUGH */
302
303 default:
304 DBG(dev, "rx status %d\n", status);
305 list_add(&req->list, &dev->rx_reqs);
306 break;
307 }
308
309 wake_up_interruptible(&dev->rx_wait);
310 spin_unlock_irqrestore(&dev->lock, flags);
311}
312
313static void tx_complete(struct usb_ep *ep, struct usb_request *req)
314{
315 struct printer_dev *dev = ep->driver_data;
316
317 switch (req->status) {
318 default:
319 VDBG(dev, "tx err %d\n", req->status);
320 /* FALLTHROUGH */
321 case -ECONNRESET: /* unlink */
322 case -ESHUTDOWN: /* disconnect etc */
323 break;
324 case 0:
325 break;
326 }
327
328 spin_lock(&dev->lock);
329 /* Take the request struct off the active list and put it on the
330 * free list.
331 */
332 list_del_init(&req->list);
333 list_add(&req->list, &dev->tx_reqs);
334 wake_up_interruptible(&dev->tx_wait);
335 if (likely(list_empty(&dev->tx_reqs_active)))
336 wake_up_interruptible(&dev->tx_flush_wait);
337
338 spin_unlock(&dev->lock);
339}
340
341/*-------------------------------------------------------------------------*/
342
343static int
344printer_open(struct inode *inode, struct file *fd)
345{
346 struct printer_dev *dev;
347 unsigned long flags;
348 int ret = -EBUSY;
349
350 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
351
352 spin_lock_irqsave(&dev->lock, flags);
353
354 if (!dev->printer_cdev_open) {
355 dev->printer_cdev_open = 1;
356 fd->private_data = dev;
357 ret = 0;
358 /* Change the printer status to show that it's on-line. */
359 dev->printer_status |= PRINTER_SELECTED;
360 }
361
362 spin_unlock_irqrestore(&dev->lock, flags);
363
364 DBG(dev, "printer_open returned %x\n", ret);
365 return ret;
366}
367
368static int
369printer_close(struct inode *inode, struct file *fd)
370{
371 struct printer_dev *dev = fd->private_data;
372 unsigned long flags;
373
374 spin_lock_irqsave(&dev->lock, flags);
375 dev->printer_cdev_open = 0;
376 fd->private_data = NULL;
377 /* Change printer status to show that the printer is off-line. */
378 dev->printer_status &= ~PRINTER_SELECTED;
379 spin_unlock_irqrestore(&dev->lock, flags);
380
381 DBG(dev, "printer_close\n");
382
383 return 0;
384}
385
386/* This function must be called with interrupts turned off. */
387static void
388setup_rx_reqs(struct printer_dev *dev)
389{
390 struct usb_request *req;
391
392 while (likely(!list_empty(&dev->rx_reqs))) {
393 int error;
394
395 req = container_of(dev->rx_reqs.next,
396 struct usb_request, list);
397 list_del_init(&req->list);
398
399 /* The USB Host sends us whatever amount of data it wants to
400 * so we always set the length field to the full USB_BUFSIZE.
401 * If the amount of data is more than the read() caller asked
402 * for it will be stored in the request buffer until it is
403 * asked for by read().
404 */
405 req->length = USB_BUFSIZE;
406 req->complete = rx_complete;
407
408 /* here, we unlock, and only unlock, to avoid deadlock. */
409 spin_unlock(&dev->lock);
410 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
411 spin_lock(&dev->lock);
412 if (error) {
413 DBG(dev, "rx submit --> %d\n", error);
414 list_add(&req->list, &dev->rx_reqs);
415 break;
416 }
417 /* if the req is empty, then add it into dev->rx_reqs_active. */
418 else if (list_empty(&req->list))
419 list_add(&req->list, &dev->rx_reqs_active);
420 }
421}
422
423static ssize_t
424printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
425{
426 struct printer_dev *dev = fd->private_data;
427 unsigned long flags;
428 size_t size;
429 size_t bytes_copied;
430 struct usb_request *req;
431 /* This is a pointer to the current USB rx request. */
432 struct usb_request *current_rx_req;
433 /* This is the number of bytes in the current rx buffer. */
434 size_t current_rx_bytes;
435 /* This is a pointer to the current rx buffer. */
436 u8 *current_rx_buf;
437
438 if (len == 0)
439 return -EINVAL;
440
441 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
442
443 mutex_lock(&dev->lock_printer_io);
444 spin_lock_irqsave(&dev->lock, flags);
445
446 /* We will use this flag later to check if a printer reset happened
447 * after we turn interrupts back on.
448 */
449 dev->reset_printer = 0;
450
451 setup_rx_reqs(dev);
452
453 bytes_copied = 0;
454 current_rx_req = dev->current_rx_req;
455 current_rx_bytes = dev->current_rx_bytes;
456 current_rx_buf = dev->current_rx_buf;
457 dev->current_rx_req = NULL;
458 dev->current_rx_bytes = 0;
459 dev->current_rx_buf = NULL;
460
461 /* Check if there is any data in the read buffers. Please note that
462 * current_rx_bytes is the number of bytes in the current rx buffer.
463 * If it is zero then check if there are any other rx_buffers that
464 * are on the completed list. We are only out of data if all rx
465 * buffers are empty.
466 */
467 if ((current_rx_bytes == 0) &&
468 (likely(list_empty(&dev->rx_buffers)))) {
469 /* Turn interrupts back on before sleeping. */
470 spin_unlock_irqrestore(&dev->lock, flags);
471
472 /*
473 * If no data is available check if this is a NON-Blocking
474 * call or not.
475 */
476 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
477 mutex_unlock(&dev->lock_printer_io);
478 return -EAGAIN;
479 }
480
481 /* Sleep until data is available */
482 wait_event_interruptible(dev->rx_wait,
483 (likely(!list_empty(&dev->rx_buffers))));
484 spin_lock_irqsave(&dev->lock, flags);
485 }
486
487 /* We have data to return then copy it to the caller's buffer.*/
488 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
489 && len) {
490 if (current_rx_bytes == 0) {
491 req = container_of(dev->rx_buffers.next,
492 struct usb_request, list);
493 list_del_init(&req->list);
494
495 if (req->actual && req->buf) {
496 current_rx_req = req;
497 current_rx_bytes = req->actual;
498 current_rx_buf = req->buf;
499 } else {
500 list_add(&req->list, &dev->rx_reqs);
501 continue;
502 }
503 }
504
505 /* Don't leave irqs off while doing memory copies */
506 spin_unlock_irqrestore(&dev->lock, flags);
507
508 if (len > current_rx_bytes)
509 size = current_rx_bytes;
510 else
511 size = len;
512
513 size -= copy_to_user(buf, current_rx_buf, size);
514 bytes_copied += size;
515 len -= size;
516 buf += size;
517
518 spin_lock_irqsave(&dev->lock, flags);
519
520 /* We've disconnected or reset so return. */
521 if (dev->reset_printer) {
522 list_add(&current_rx_req->list, &dev->rx_reqs);
523 spin_unlock_irqrestore(&dev->lock, flags);
524 mutex_unlock(&dev->lock_printer_io);
525 return -EAGAIN;
526 }
527
528 /* If we not returning all the data left in this RX request
529 * buffer then adjust the amount of data left in the buffer.
530 * Othewise if we are done with this RX request buffer then
531 * requeue it to get any incoming data from the USB host.
532 */
533 if (size < current_rx_bytes) {
534 current_rx_bytes -= size;
535 current_rx_buf += size;
536 } else {
537 list_add(&current_rx_req->list, &dev->rx_reqs);
538 current_rx_bytes = 0;
539 current_rx_buf = NULL;
540 current_rx_req = NULL;
541 }
542 }
543
544 dev->current_rx_req = current_rx_req;
545 dev->current_rx_bytes = current_rx_bytes;
546 dev->current_rx_buf = current_rx_buf;
547
548 spin_unlock_irqrestore(&dev->lock, flags);
549 mutex_unlock(&dev->lock_printer_io);
550
551 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
552
553 if (bytes_copied)
554 return bytes_copied;
555 else
556 return -EAGAIN;
557}
558
559static ssize_t
560printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
561{
562 struct printer_dev *dev = fd->private_data;
563 unsigned long flags;
564 size_t size; /* Amount of data in a TX request. */
565 size_t bytes_copied = 0;
566 struct usb_request *req;
567
568 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
569
570 if (len == 0)
571 return -EINVAL;
572
573 mutex_lock(&dev->lock_printer_io);
574 spin_lock_irqsave(&dev->lock, flags);
575
576 /* Check if a printer reset happens while we have interrupts on */
577 dev->reset_printer = 0;
578
579 /* Check if there is any available write buffers */
580 if (likely(list_empty(&dev->tx_reqs))) {
581 /* Turn interrupts back on before sleeping. */
582 spin_unlock_irqrestore(&dev->lock, flags);
583
584 /*
585 * If write buffers are available check if this is
586 * a NON-Blocking call or not.
587 */
588 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
589 mutex_unlock(&dev->lock_printer_io);
590 return -EAGAIN;
591 }
592
593 /* Sleep until a write buffer is available */
594 wait_event_interruptible(dev->tx_wait,
595 (likely(!list_empty(&dev->tx_reqs))));
596 spin_lock_irqsave(&dev->lock, flags);
597 }
598
599 while (likely(!list_empty(&dev->tx_reqs)) && len) {
600
601 if (len > USB_BUFSIZE)
602 size = USB_BUFSIZE;
603 else
604 size = len;
605
606 req = container_of(dev->tx_reqs.next, struct usb_request,
607 list);
608 list_del_init(&req->list);
609
610 req->complete = tx_complete;
611 req->length = size;
612
613 /* Check if we need to send a zero length packet. */
614 if (len > size)
615 /* They will be more TX requests so no yet. */
616 req->zero = 0;
617 else
618 /* If the data amount is not a multiple of the
619 * maxpacket size then send a zero length packet.
620 */
621 req->zero = ((len % dev->in_ep->maxpacket) == 0);
622
623 /* Don't leave irqs off while doing memory copies */
624 spin_unlock_irqrestore(&dev->lock, flags);
625
626 if (copy_from_user(req->buf, buf, size)) {
627 list_add(&req->list, &dev->tx_reqs);
628 mutex_unlock(&dev->lock_printer_io);
629 return bytes_copied;
630 }
631
632 bytes_copied += size;
633 len -= size;
634 buf += size;
635
636 spin_lock_irqsave(&dev->lock, flags);
637
638 /* We've disconnected or reset so free the req and buffer */
639 if (dev->reset_printer) {
640 list_add(&req->list, &dev->tx_reqs);
641 spin_unlock_irqrestore(&dev->lock, flags);
642 mutex_unlock(&dev->lock_printer_io);
643 return -EAGAIN;
644 }
645
646 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
647 list_add(&req->list, &dev->tx_reqs);
648 spin_unlock_irqrestore(&dev->lock, flags);
649 mutex_unlock(&dev->lock_printer_io);
650 return -EAGAIN;
651 }
652
653 list_add(&req->list, &dev->tx_reqs_active);
654
655 }
656
657 spin_unlock_irqrestore(&dev->lock, flags);
658 mutex_unlock(&dev->lock_printer_io);
659
660 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
661
662 if (bytes_copied)
663 return bytes_copied;
664 else
665 return -EAGAIN;
666}
667
668static int
669printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
670{
671 struct printer_dev *dev = fd->private_data;
672 struct inode *inode = file_inode(fd);
673 unsigned long flags;
674 int tx_list_empty;
675
676 mutex_lock(&inode->i_mutex);
677 spin_lock_irqsave(&dev->lock, flags);
678 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
679 spin_unlock_irqrestore(&dev->lock, flags);
680
681 if (!tx_list_empty) {
682 /* Sleep until all data has been sent */
683 wait_event_interruptible(dev->tx_flush_wait,
684 (likely(list_empty(&dev->tx_reqs_active))));
685 }
686 mutex_unlock(&inode->i_mutex);
687
688 return 0;
689}
690
691static unsigned int
692printer_poll(struct file *fd, poll_table *wait)
693{
694 struct printer_dev *dev = fd->private_data;
695 unsigned long flags;
696 int status = 0;
697
698 mutex_lock(&dev->lock_printer_io);
699 spin_lock_irqsave(&dev->lock, flags);
700 setup_rx_reqs(dev);
701 spin_unlock_irqrestore(&dev->lock, flags);
702 mutex_unlock(&dev->lock_printer_io);
703
704 poll_wait(fd, &dev->rx_wait, wait);
705 poll_wait(fd, &dev->tx_wait, wait);
706
707 spin_lock_irqsave(&dev->lock, flags);
708 if (likely(!list_empty(&dev->tx_reqs)))
709 status |= POLLOUT | POLLWRNORM;
710
711 if (likely(dev->current_rx_bytes) ||
712 likely(!list_empty(&dev->rx_buffers)))
713 status |= POLLIN | POLLRDNORM;
714
715 spin_unlock_irqrestore(&dev->lock, flags);
716
717 return status;
718}
719
720static long
721printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
722{
723 struct printer_dev *dev = fd->private_data;
724 unsigned long flags;
725 int status = 0;
726
727 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
728
729 /* handle ioctls */
730
731 spin_lock_irqsave(&dev->lock, flags);
732
733 switch (code) {
734 case GADGET_GET_PRINTER_STATUS:
735 status = (int)dev->printer_status;
736 break;
737 case GADGET_SET_PRINTER_STATUS:
738 dev->printer_status = (u8)arg;
739 break;
740 default:
741 /* could not handle ioctl */
742 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
743 code);
744 status = -ENOTTY;
745 }
746
747 spin_unlock_irqrestore(&dev->lock, flags);
748
749 return status;
750}
751
752/* used after endpoint configuration */
753static const struct file_operations printer_io_operations = {
754 .owner = THIS_MODULE,
755 .open = printer_open,
756 .read = printer_read,
757 .write = printer_write,
758 .fsync = printer_fsync,
759 .poll = printer_poll,
760 .unlocked_ioctl = printer_ioctl,
761 .release = printer_close,
762 .llseek = noop_llseek,
763};
764
765/*-------------------------------------------------------------------------*/
766
767static int
768set_printer_interface(struct printer_dev *dev)
769{
770 int result = 0;
771
772 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
773 &ss_ep_in_desc);
774 dev->in_ep->driver_data = dev;
775
776 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
777 &hs_ep_out_desc, &ss_ep_out_desc);
778 dev->out_ep->driver_data = dev;
779
780 result = usb_ep_enable(dev->in_ep);
781 if (result != 0) {
782 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
783 goto done;
784 }
785
786 result = usb_ep_enable(dev->out_ep);
787 if (result != 0) {
788 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
789 goto done;
790 }
791
792done:
793 /* on error, disable any endpoints */
794 if (result != 0) {
795 (void) usb_ep_disable(dev->in_ep);
796 (void) usb_ep_disable(dev->out_ep);
797 dev->in_ep->desc = NULL;
798 dev->out_ep->desc = NULL;
799 }
800
801 /* caller is responsible for cleanup on error */
802 return result;
803}
804
805static void printer_reset_interface(struct printer_dev *dev)
806{
807 if (dev->interface < 0)
808 return;
809
810 DBG(dev, "%s\n", __func__);
811
812 if (dev->in_ep->desc)
813 usb_ep_disable(dev->in_ep);
814
815 if (dev->out_ep->desc)
816 usb_ep_disable(dev->out_ep);
817
818 dev->in_ep->desc = NULL;
819 dev->out_ep->desc = NULL;
820 dev->interface = -1;
821}
822
823/* Change our operational Interface. */
824static int set_interface(struct printer_dev *dev, unsigned number)
825{
826 int result = 0;
827
828 /* Free the current interface */
829 printer_reset_interface(dev);
830
831 result = set_printer_interface(dev);
832 if (result)
833 printer_reset_interface(dev);
834 else
835 dev->interface = number;
836
837 if (!result)
838 INFO(dev, "Using interface %x\n", number);
839
840 return result;
841}
842
843static void printer_soft_reset(struct printer_dev *dev)
844{
845 struct usb_request *req;
846
847 INFO(dev, "Received Printer Reset Request\n");
848
849 if (usb_ep_disable(dev->in_ep))
850 DBG(dev, "Failed to disable USB in_ep\n");
851 if (usb_ep_disable(dev->out_ep))
852 DBG(dev, "Failed to disable USB out_ep\n");
853
854 if (dev->current_rx_req != NULL) {
855 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
856 dev->current_rx_req = NULL;
857 }
858 dev->current_rx_bytes = 0;
859 dev->current_rx_buf = NULL;
860 dev->reset_printer = 1;
861
862 while (likely(!(list_empty(&dev->rx_buffers)))) {
863 req = container_of(dev->rx_buffers.next, struct usb_request,
864 list);
865 list_del_init(&req->list);
866 list_add(&req->list, &dev->rx_reqs);
867 }
868
869 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
870 req = container_of(dev->rx_buffers.next, struct usb_request,
871 list);
872 list_del_init(&req->list);
873 list_add(&req->list, &dev->rx_reqs);
874 }
875
876 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
877 req = container_of(dev->tx_reqs_active.next,
878 struct usb_request, list);
879 list_del_init(&req->list);
880 list_add(&req->list, &dev->tx_reqs);
881 }
882
883 if (usb_ep_enable(dev->in_ep))
884 DBG(dev, "Failed to enable USB in_ep\n");
885 if (usb_ep_enable(dev->out_ep))
886 DBG(dev, "Failed to enable USB out_ep\n");
887
888 wake_up_interruptible(&dev->rx_wait);
889 wake_up_interruptible(&dev->tx_wait);
890 wake_up_interruptible(&dev->tx_flush_wait);
891}
892
893/*-------------------------------------------------------------------------*/
894
895static bool gprinter_req_match(struct usb_function *f,
896 const struct usb_ctrlrequest *ctrl)
897{
898 struct printer_dev *dev = func_to_printer(f);
899 u16 w_index = le16_to_cpu(ctrl->wIndex);
900 u16 w_value = le16_to_cpu(ctrl->wValue);
901 u16 w_length = le16_to_cpu(ctrl->wLength);
902
903 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
904 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
905 return false;
906
907 switch (ctrl->bRequest) {
908 case GET_DEVICE_ID:
909 w_index >>= 8;
910 if (w_length <= PNP_STRING_LEN &&
911 (USB_DIR_IN & ctrl->bRequestType))
912 break;
913 return false;
914 case GET_PORT_STATUS:
915 if (!w_value && w_length == 1 &&
916 (USB_DIR_IN & ctrl->bRequestType))
917 break;
918 return false;
919 case SOFT_RESET:
920 if (!w_value && !w_length &&
921 !(USB_DIR_IN & ctrl->bRequestType))
922 break;
923 /* fall through */
924 default:
925 return false;
926 }
927 return w_index == dev->interface;
928}
929
930/*
931 * The setup() callback implements all the ep0 functionality that's not
932 * handled lower down.
933 */
934static int printer_func_setup(struct usb_function *f,
935 const struct usb_ctrlrequest *ctrl)
936{
937 struct printer_dev *dev = func_to_printer(f);
938 struct usb_composite_dev *cdev = f->config->cdev;
939 struct usb_request *req = cdev->req;
940 int value = -EOPNOTSUPP;
941 u16 wIndex = le16_to_cpu(ctrl->wIndex);
942 u16 wValue = le16_to_cpu(ctrl->wValue);
943 u16 wLength = le16_to_cpu(ctrl->wLength);
944
945 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
946 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
947
948 switch (ctrl->bRequestType&USB_TYPE_MASK) {
949 case USB_TYPE_CLASS:
950 switch (ctrl->bRequest) {
951 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
952 /* Only one printer interface is supported. */
953 if ((wIndex>>8) != dev->interface)
954 break;
955
956 value = (dev->pnp_string[0] << 8) | dev->pnp_string[1];
957 memcpy(req->buf, dev->pnp_string, value);
958 DBG(dev, "1284 PNP String: %x %s\n", value,
959 &dev->pnp_string[2]);
960 break;
961
962 case GET_PORT_STATUS: /* Get Port Status */
963 /* Only one printer interface is supported. */
964 if (wIndex != dev->interface)
965 break;
966
967 *(u8 *)req->buf = dev->printer_status;
968 value = min_t(u16, wLength, 1);
969 break;
970
971 case SOFT_RESET: /* Soft Reset */
972 /* Only one printer interface is supported. */
973 if (wIndex != dev->interface)
974 break;
975
976 printer_soft_reset(dev);
977
978 value = 0;
979 break;
980
981 default:
982 goto unknown;
983 }
984 break;
985
986 default:
987unknown:
988 VDBG(dev,
989 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
990 ctrl->bRequestType, ctrl->bRequest,
991 wValue, wIndex, wLength);
992 break;
993 }
994 /* host either stalls (value < 0) or reports success */
995 if (value >= 0) {
996 req->length = value;
997 req->zero = value < wLength;
998 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
999 if (value < 0) {
1000 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1001 req->status = 0;
1002 }
1003 }
1004 return value;
1005}
1006
1007static int printer_func_bind(struct usb_configuration *c,
1008 struct usb_function *f)
1009{
1010 struct usb_gadget *gadget = c->cdev->gadget;
1011 struct printer_dev *dev = func_to_printer(f);
1012 struct device *pdev;
1013 struct usb_composite_dev *cdev = c->cdev;
1014 struct usb_ep *in_ep;
1015 struct usb_ep *out_ep = NULL;
1016 struct usb_request *req;
1017 dev_t devt;
1018 int id;
1019 int ret;
1020 u32 i;
1021
1022 id = usb_interface_id(c, f);
1023 if (id < 0)
1024 return id;
1025 intf_desc.bInterfaceNumber = id;
1026
1027 /* finish hookup to lower layer ... */
1028 dev->gadget = gadget;
1029
1030 /* all we really need is bulk IN/OUT */
1031 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1032 if (!in_ep) {
1033autoconf_fail:
1034 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1035 cdev->gadget->name);
1036 return -ENODEV;
1037 }
1038 in_ep->driver_data = in_ep; /* claim */
1039
1040 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1041 if (!out_ep)
1042 goto autoconf_fail;
1043 out_ep->driver_data = out_ep; /* claim */
1044
1045 /* assumes that all endpoints are dual-speed */
1046 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1047 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1048 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1049 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1050
1051 ret = usb_assign_descriptors(f, fs_printer_function,
1052 hs_printer_function, ss_printer_function);
1053 if (ret)
1054 return ret;
1055
1056 dev->in_ep = in_ep;
1057 dev->out_ep = out_ep;
1058
1059 ret = -ENOMEM;
1060 for (i = 0; i < dev->q_len; i++) {
1061 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1062 if (!req)
1063 goto fail_tx_reqs;
1064 list_add(&req->list, &dev->tx_reqs);
1065 }
1066
1067 for (i = 0; i < dev->q_len; i++) {
1068 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1069 if (!req)
1070 goto fail_rx_reqs;
1071 list_add(&req->list, &dev->rx_reqs);
1072 }
1073
1074 /* Setup the sysfs files for the printer gadget. */
1075 devt = MKDEV(major, dev->minor);
1076 pdev = device_create(usb_gadget_class, NULL, devt,
1077 NULL, "g_printer%d", dev->minor);
1078 if (IS_ERR(pdev)) {
1079 ERROR(dev, "Failed to create device: g_printer\n");
1080 ret = PTR_ERR(pdev);
1081 goto fail_rx_reqs;
1082 }
1083
1084 /*
1085 * Register a character device as an interface to a user mode
1086 * program that handles the printer specific functionality.
1087 */
1088 cdev_init(&dev->printer_cdev, &printer_io_operations);
1089 dev->printer_cdev.owner = THIS_MODULE;
1090 ret = cdev_add(&dev->printer_cdev, devt, 1);
1091 if (ret) {
1092 ERROR(dev, "Failed to open char device\n");
1093 goto fail_cdev_add;
1094 }
1095
1096 return 0;
1097
1098fail_cdev_add:
1099 device_destroy(usb_gadget_class, devt);
1100
1101fail_rx_reqs:
1102 while (!list_empty(&dev->rx_reqs)) {
1103 req = container_of(dev->rx_reqs.next, struct usb_request, list);
1104 list_del(&req->list);
1105 printer_req_free(dev->out_ep, req);
1106 }
1107
1108fail_tx_reqs:
1109 while (!list_empty(&dev->tx_reqs)) {
1110 req = container_of(dev->tx_reqs.next, struct usb_request, list);
1111 list_del(&req->list);
1112 printer_req_free(dev->in_ep, req);
1113 }
1114
1115 return ret;
1116
1117}
1118
1119static int printer_func_set_alt(struct usb_function *f,
1120 unsigned intf, unsigned alt)
1121{
1122 struct printer_dev *dev = func_to_printer(f);
1123 int ret = -ENOTSUPP;
1124
1125 if (!alt)
1126 ret = set_interface(dev, intf);
1127
1128 return ret;
1129}
1130
1131static void printer_func_disable(struct usb_function *f)
1132{
1133 struct printer_dev *dev = func_to_printer(f);
1134 unsigned long flags;
1135
1136 DBG(dev, "%s\n", __func__);
1137
1138 spin_lock_irqsave(&dev->lock, flags);
1139 printer_reset_interface(dev);
1140 spin_unlock_irqrestore(&dev->lock, flags);
1141}
1142
1143static inline struct f_printer_opts
1144*to_f_printer_opts(struct config_item *item)
1145{
1146 return container_of(to_config_group(item), struct f_printer_opts,
1147 func_inst.group);
1148}
1149
1150CONFIGFS_ATTR_STRUCT(f_printer_opts);
1151CONFIGFS_ATTR_OPS(f_printer_opts);
1152
1153static void printer_attr_release(struct config_item *item)
1154{
1155 struct f_printer_opts *opts = to_f_printer_opts(item);
1156
1157 usb_put_function_instance(&opts->func_inst);
1158}
1159
1160static struct configfs_item_operations printer_item_ops = {
1161 .release = printer_attr_release,
1162 .show_attribute = f_printer_opts_attr_show,
1163 .store_attribute = f_printer_opts_attr_store,
1164};
1165
1166static ssize_t f_printer_opts_pnp_string_show(struct f_printer_opts *opts,
1167 char *page)
1168{
1169 int result;
1170
1171 mutex_lock(&opts->lock);
1172 result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2);
1173 mutex_unlock(&opts->lock);
1174
1175 return result;
1176}
1177
1178static ssize_t f_printer_opts_pnp_string_store(struct f_printer_opts *opts,
1179 const char *page, size_t len)
1180{
1181 int result, l;
1182
1183 mutex_lock(&opts->lock);
1184 result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2);
1185 l = strlen(opts->pnp_string + 2) + 2;
1186 opts->pnp_string[0] = (l >> 8) & 0xFF;
1187 opts->pnp_string[1] = l & 0xFF;
1188 mutex_unlock(&opts->lock);
1189
1190 return result;
1191}
1192
1193static struct f_printer_opts_attribute f_printer_opts_pnp_string =
1194 __CONFIGFS_ATTR(pnp_string, S_IRUGO | S_IWUSR,
1195 f_printer_opts_pnp_string_show,
1196 f_printer_opts_pnp_string_store);
1197
1198static ssize_t f_printer_opts_q_len_show(struct f_printer_opts *opts,
1199 char *page)
1200{
1201 int result;
1202
1203 mutex_lock(&opts->lock);
1204 result = sprintf(page, "%d\n", opts->q_len);
1205 mutex_unlock(&opts->lock);
1206
1207 return result;
1208}
1209
1210static ssize_t f_printer_opts_q_len_store(struct f_printer_opts *opts,
1211 const char *page, size_t len)
1212{
1213 int ret;
1214 u16 num;
1215
1216 mutex_lock(&opts->lock);
1217 if (opts->refcnt) {
1218 ret = -EBUSY;
1219 goto end;
1220 }
1221
1222 ret = kstrtou16(page, 0, &num);
1223 if (ret)
1224 goto end;
1225
1226 opts->q_len = (unsigned)num;
1227 ret = len;
1228end:
1229 mutex_unlock(&opts->lock);
1230 return ret;
1231}
1232
1233static struct f_printer_opts_attribute f_printer_opts_q_len =
1234 __CONFIGFS_ATTR(q_len, S_IRUGO | S_IWUSR, f_printer_opts_q_len_show,
1235 f_printer_opts_q_len_store);
1236
1237static struct configfs_attribute *printer_attrs[] = {
1238 &f_printer_opts_pnp_string.attr,
1239 &f_printer_opts_q_len.attr,
1240 NULL,
1241};
1242
1243static struct config_item_type printer_func_type = {
1244 .ct_item_ops = &printer_item_ops,
1245 .ct_attrs = printer_attrs,
1246 .ct_owner = THIS_MODULE,
1247};
1248
1249static inline int gprinter_get_minor(void)
1250{
1251 return ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1252}
1253
1254static inline void gprinter_put_minor(int minor)
1255{
1256 ida_simple_remove(&printer_ida, minor);
1257}
1258
1259static int gprinter_setup(int);
1260static void gprinter_cleanup(void);
1261
1262static void gprinter_free_inst(struct usb_function_instance *f)
1263{
1264 struct f_printer_opts *opts;
1265
1266 opts = container_of(f, struct f_printer_opts, func_inst);
1267
1268 mutex_lock(&printer_ida_lock);
1269
1270 gprinter_put_minor(opts->minor);
1271 if (idr_is_empty(&printer_ida.idr))
1272 gprinter_cleanup();
1273
1274 mutex_unlock(&printer_ida_lock);
1275
1276 kfree(opts);
1277}
1278
1279static struct usb_function_instance *gprinter_alloc_inst(void)
1280{
1281 struct f_printer_opts *opts;
1282 struct usb_function_instance *ret;
1283 int status = 0;
1284
1285 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1286 if (!opts)
1287 return ERR_PTR(-ENOMEM);
1288
1289 mutex_init(&opts->lock);
1290 opts->func_inst.free_func_inst = gprinter_free_inst;
1291 ret = &opts->func_inst;
1292
1293 mutex_lock(&printer_ida_lock);
1294
1295 if (idr_is_empty(&printer_ida.idr)) {
1296 status = gprinter_setup(PRINTER_MINORS);
1297 if (status) {
1298 ret = ERR_PTR(status);
1299 kfree(opts);
1300 goto unlock;
1301 }
1302 }
1303
1304 opts->minor = gprinter_get_minor();
1305 if (opts->minor < 0) {
1306 ret = ERR_PTR(opts->minor);
1307 kfree(opts);
1308 if (idr_is_empty(&printer_ida.idr))
1309 gprinter_cleanup();
1310 goto unlock;
1311 }
1312 config_group_init_type_name(&opts->func_inst.group, "",
1313 &printer_func_type);
1314
1315unlock:
1316 mutex_unlock(&printer_ida_lock);
1317 return ret;
1318}
1319
1320static void gprinter_free(struct usb_function *f)
1321{
1322 struct printer_dev *dev = func_to_printer(f);
1323 struct f_printer_opts *opts;
1324
1325 opts = container_of(f->fi, struct f_printer_opts, func_inst);
1326 kfree(dev);
1327 mutex_lock(&opts->lock);
1328 --opts->refcnt;
1329 mutex_unlock(&opts->lock);
1330}
1331
1332static void printer_func_unbind(struct usb_configuration *c,
1333 struct usb_function *f)
1334{
1335 struct printer_dev *dev;
1336 struct usb_request *req;
1337
1338 dev = func_to_printer(f);
1339
1340 device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1341
1342 /* Remove Character Device */
1343 cdev_del(&dev->printer_cdev);
1344
1345 /* we must already have been disconnected ... no i/o may be active */
1346 WARN_ON(!list_empty(&dev->tx_reqs_active));
1347 WARN_ON(!list_empty(&dev->rx_reqs_active));
1348
1349 /* Free all memory for this driver. */
1350 while (!list_empty(&dev->tx_reqs)) {
1351 req = container_of(dev->tx_reqs.next, struct usb_request,
1352 list);
1353 list_del(&req->list);
1354 printer_req_free(dev->in_ep, req);
1355 }
1356
1357 if (dev->current_rx_req != NULL)
1358 printer_req_free(dev->out_ep, dev->current_rx_req);
1359
1360 while (!list_empty(&dev->rx_reqs)) {
1361 req = container_of(dev->rx_reqs.next,
1362 struct usb_request, list);
1363 list_del(&req->list);
1364 printer_req_free(dev->out_ep, req);
1365 }
1366
1367 while (!list_empty(&dev->rx_buffers)) {
1368 req = container_of(dev->rx_buffers.next,
1369 struct usb_request, list);
1370 list_del(&req->list);
1371 printer_req_free(dev->out_ep, req);
1372 }
1373 usb_free_all_descriptors(f);
1374}
1375
1376static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1377{
1378 struct printer_dev *dev;
1379 struct f_printer_opts *opts;
1380
1381 opts = container_of(fi, struct f_printer_opts, func_inst);
1382
1383 mutex_lock(&opts->lock);
1384 if (opts->minor >= minors) {
1385 mutex_unlock(&opts->lock);
1386 return ERR_PTR(-ENOENT);
1387 }
1388
1389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1390 if (!dev) {
1391 mutex_unlock(&opts->lock);
1392 return ERR_PTR(-ENOMEM);
1393 }
1394
1395 ++opts->refcnt;
1396 dev->minor = opts->minor;
1397 dev->pnp_string = opts->pnp_string;
1398 dev->q_len = opts->q_len;
1399 mutex_unlock(&opts->lock);
1400
1401 dev->function.name = "printer";
1402 dev->function.bind = printer_func_bind;
1403 dev->function.setup = printer_func_setup;
1404 dev->function.unbind = printer_func_unbind;
1405 dev->function.set_alt = printer_func_set_alt;
1406 dev->function.disable = printer_func_disable;
1407 dev->function.req_match = gprinter_req_match;
1408 dev->function.free_func = gprinter_free;
1409
1410 INIT_LIST_HEAD(&dev->tx_reqs);
1411 INIT_LIST_HEAD(&dev->rx_reqs);
1412 INIT_LIST_HEAD(&dev->rx_buffers);
1413 INIT_LIST_HEAD(&dev->tx_reqs_active);
1414 INIT_LIST_HEAD(&dev->rx_reqs_active);
1415
1416 spin_lock_init(&dev->lock);
1417 mutex_init(&dev->lock_printer_io);
1418 init_waitqueue_head(&dev->rx_wait);
1419 init_waitqueue_head(&dev->tx_wait);
1420 init_waitqueue_head(&dev->tx_flush_wait);
1421
1422 dev->interface = -1;
1423 dev->printer_cdev_open = 0;
1424 dev->printer_status = PRINTER_NOT_ERROR;
1425 dev->current_rx_req = NULL;
1426 dev->current_rx_bytes = 0;
1427 dev->current_rx_buf = NULL;
1428
1429 return &dev->function;
1430}
1431
1432DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1433MODULE_LICENSE("GPL");
1434MODULE_AUTHOR("Craig Nadler");
1435
1436static int gprinter_setup(int count)
1437{
1438 int status;
1439 dev_t devt;
1440
1441 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1442 if (IS_ERR(usb_gadget_class)) {
1443 status = PTR_ERR(usb_gadget_class);
1444 usb_gadget_class = NULL;
1445 pr_err("unable to create usb_gadget class %d\n", status);
1446 return status;
1447 }
1448
1449 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1450 if (status) {
1451 pr_err("alloc_chrdev_region %d\n", status);
1452 class_destroy(usb_gadget_class);
1453 usb_gadget_class = NULL;
1454 return status;
1455 }
1456
1457 major = MAJOR(devt);
1458 minors = count;
1459
1460 return status;
1461}
1462
1463static void gprinter_cleanup(void)
1464{
1465 if (major) {
1466 unregister_chrdev_region(MKDEV(major, 0), minors);
1467 major = minors = 0;
1468 }
1469 class_destroy(usb_gadget_class);
1470 usb_gadget_class = NULL;
1471}
diff --git a/drivers/usb/gadget/function/f_sourcesink.c b/drivers/usb/gadget/function/f_sourcesink.c
index e3dae47baef3..3a5ae9900b1e 100644
--- a/drivers/usb/gadget/function/f_sourcesink.c
+++ b/drivers/usb/gadget/function/f_sourcesink.c
@@ -23,15 +23,6 @@
23#include "gadget_chips.h" 23#include "gadget_chips.h"
24#include "u_f.h" 24#include "u_f.h"
25 25
26#define USB_MS_TO_SS_INTERVAL(x) USB_MS_TO_HS_INTERVAL(x)
27
28enum eptype {
29 EP_CONTROL = 0,
30 EP_BULK,
31 EP_ISOC,
32 EP_INTERRUPT,
33};
34
35/* 26/*
36 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral 27 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
37 * controller drivers. 28 * controller drivers.
@@ -64,8 +55,6 @@ struct f_sourcesink {
64 struct usb_ep *out_ep; 55 struct usb_ep *out_ep;
65 struct usb_ep *iso_in_ep; 56 struct usb_ep *iso_in_ep;
66 struct usb_ep *iso_out_ep; 57 struct usb_ep *iso_out_ep;
67 struct usb_ep *int_in_ep;
68 struct usb_ep *int_out_ep;
69 int cur_alt; 58 int cur_alt;
70}; 59};
71 60
@@ -79,10 +68,6 @@ static unsigned isoc_interval;
79static unsigned isoc_maxpacket; 68static unsigned isoc_maxpacket;
80static unsigned isoc_mult; 69static unsigned isoc_mult;
81static unsigned isoc_maxburst; 70static unsigned isoc_maxburst;
82static unsigned int_interval; /* In ms */
83static unsigned int_maxpacket;
84static unsigned int_mult;
85static unsigned int_maxburst;
86static unsigned buflen; 71static unsigned buflen;
87 72
88/*-------------------------------------------------------------------------*/ 73/*-------------------------------------------------------------------------*/
@@ -107,16 +92,6 @@ static struct usb_interface_descriptor source_sink_intf_alt1 = {
107 /* .iInterface = DYNAMIC */ 92 /* .iInterface = DYNAMIC */
108}; 93};
109 94
110static struct usb_interface_descriptor source_sink_intf_alt2 = {
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113
114 .bAlternateSetting = 2,
115 .bNumEndpoints = 2,
116 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
117 /* .iInterface = DYNAMIC */
118};
119
120/* full speed support: */ 95/* full speed support: */
121 96
122static struct usb_endpoint_descriptor fs_source_desc = { 97static struct usb_endpoint_descriptor fs_source_desc = {
@@ -155,26 +130,6 @@ static struct usb_endpoint_descriptor fs_iso_sink_desc = {
155 .bInterval = 4, 130 .bInterval = 4,
156}; 131};
157 132
158static struct usb_endpoint_descriptor fs_int_source_desc = {
159 .bLength = USB_DT_ENDPOINT_SIZE,
160 .bDescriptorType = USB_DT_ENDPOINT,
161
162 .bEndpointAddress = USB_DIR_IN,
163 .bmAttributes = USB_ENDPOINT_XFER_INT,
164 .wMaxPacketSize = cpu_to_le16(64),
165 .bInterval = GZERO_INT_INTERVAL,
166};
167
168static struct usb_endpoint_descriptor fs_int_sink_desc = {
169 .bLength = USB_DT_ENDPOINT_SIZE,
170 .bDescriptorType = USB_DT_ENDPOINT,
171
172 .bEndpointAddress = USB_DIR_OUT,
173 .bmAttributes = USB_ENDPOINT_XFER_INT,
174 .wMaxPacketSize = cpu_to_le16(64),
175 .bInterval = GZERO_INT_INTERVAL,
176};
177
178static struct usb_descriptor_header *fs_source_sink_descs[] = { 133static struct usb_descriptor_header *fs_source_sink_descs[] = {
179 (struct usb_descriptor_header *) &source_sink_intf_alt0, 134 (struct usb_descriptor_header *) &source_sink_intf_alt0,
180 (struct usb_descriptor_header *) &fs_sink_desc, 135 (struct usb_descriptor_header *) &fs_sink_desc,
@@ -185,10 +140,6 @@ static struct usb_descriptor_header *fs_source_sink_descs[] = {
185 (struct usb_descriptor_header *) &fs_source_desc, 140 (struct usb_descriptor_header *) &fs_source_desc,
186 (struct usb_descriptor_header *) &fs_iso_sink_desc, 141 (struct usb_descriptor_header *) &fs_iso_sink_desc,
187 (struct usb_descriptor_header *) &fs_iso_source_desc, 142 (struct usb_descriptor_header *) &fs_iso_source_desc,
188 (struct usb_descriptor_header *) &source_sink_intf_alt2,
189#define FS_ALT_IFC_2_OFFSET 8
190 (struct usb_descriptor_header *) &fs_int_sink_desc,
191 (struct usb_descriptor_header *) &fs_int_source_desc,
192 NULL, 143 NULL,
193}; 144};
194 145
@@ -228,24 +179,6 @@ static struct usb_endpoint_descriptor hs_iso_sink_desc = {
228 .bInterval = 4, 179 .bInterval = 4,
229}; 180};
230 181
231static struct usb_endpoint_descriptor hs_int_source_desc = {
232 .bLength = USB_DT_ENDPOINT_SIZE,
233 .bDescriptorType = USB_DT_ENDPOINT,
234
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
236 .wMaxPacketSize = cpu_to_le16(1024),
237 .bInterval = USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
238};
239
240static struct usb_endpoint_descriptor hs_int_sink_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
243
244 .bmAttributes = USB_ENDPOINT_XFER_INT,
245 .wMaxPacketSize = cpu_to_le16(1024),
246 .bInterval = USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
247};
248
249static struct usb_descriptor_header *hs_source_sink_descs[] = { 182static struct usb_descriptor_header *hs_source_sink_descs[] = {
250 (struct usb_descriptor_header *) &source_sink_intf_alt0, 183 (struct usb_descriptor_header *) &source_sink_intf_alt0,
251 (struct usb_descriptor_header *) &hs_source_desc, 184 (struct usb_descriptor_header *) &hs_source_desc,
@@ -256,10 +189,6 @@ static struct usb_descriptor_header *hs_source_sink_descs[] = {
256 (struct usb_descriptor_header *) &hs_sink_desc, 189 (struct usb_descriptor_header *) &hs_sink_desc,
257 (struct usb_descriptor_header *) &hs_iso_source_desc, 190 (struct usb_descriptor_header *) &hs_iso_source_desc,
258 (struct usb_descriptor_header *) &hs_iso_sink_desc, 191 (struct usb_descriptor_header *) &hs_iso_sink_desc,
259 (struct usb_descriptor_header *) &source_sink_intf_alt2,
260#define HS_ALT_IFC_2_OFFSET 8
261 (struct usb_descriptor_header *) &hs_int_source_desc,
262 (struct usb_descriptor_header *) &hs_int_sink_desc,
263 NULL, 192 NULL,
264}; 193};
265 194
@@ -335,42 +264,6 @@ static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
335 .wBytesPerInterval = cpu_to_le16(1024), 264 .wBytesPerInterval = cpu_to_le16(1024),
336}; 265};
337 266
338static struct usb_endpoint_descriptor ss_int_source_desc = {
339 .bLength = USB_DT_ENDPOINT_SIZE,
340 .bDescriptorType = USB_DT_ENDPOINT,
341
342 .bmAttributes = USB_ENDPOINT_XFER_INT,
343 .wMaxPacketSize = cpu_to_le16(1024),
344 .bInterval = USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
345};
346
347static struct usb_ss_ep_comp_descriptor ss_int_source_comp_desc = {
348 .bLength = USB_DT_SS_EP_COMP_SIZE,
349 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
350
351 .bMaxBurst = 0,
352 .bmAttributes = 0,
353 .wBytesPerInterval = cpu_to_le16(1024),
354};
355
356static struct usb_endpoint_descriptor ss_int_sink_desc = {
357 .bLength = USB_DT_ENDPOINT_SIZE,
358 .bDescriptorType = USB_DT_ENDPOINT,
359
360 .bmAttributes = USB_ENDPOINT_XFER_INT,
361 .wMaxPacketSize = cpu_to_le16(1024),
362 .bInterval = USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
363};
364
365static struct usb_ss_ep_comp_descriptor ss_int_sink_comp_desc = {
366 .bLength = USB_DT_SS_EP_COMP_SIZE,
367 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
368
369 .bMaxBurst = 0,
370 .bmAttributes = 0,
371 .wBytesPerInterval = cpu_to_le16(1024),
372};
373
374static struct usb_descriptor_header *ss_source_sink_descs[] = { 267static struct usb_descriptor_header *ss_source_sink_descs[] = {
375 (struct usb_descriptor_header *) &source_sink_intf_alt0, 268 (struct usb_descriptor_header *) &source_sink_intf_alt0,
376 (struct usb_descriptor_header *) &ss_source_desc, 269 (struct usb_descriptor_header *) &ss_source_desc,
@@ -387,12 +280,6 @@ static struct usb_descriptor_header *ss_source_sink_descs[] = {
387 (struct usb_descriptor_header *) &ss_iso_source_comp_desc, 280 (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
388 (struct usb_descriptor_header *) &ss_iso_sink_desc, 281 (struct usb_descriptor_header *) &ss_iso_sink_desc,
389 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc, 282 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
390 (struct usb_descriptor_header *) &source_sink_intf_alt2,
391#define SS_ALT_IFC_2_OFFSET 14
392 (struct usb_descriptor_header *) &ss_int_source_desc,
393 (struct usb_descriptor_header *) &ss_int_source_comp_desc,
394 (struct usb_descriptor_header *) &ss_int_sink_desc,
395 (struct usb_descriptor_header *) &ss_int_sink_comp_desc,
396 NULL, 283 NULL,
397}; 284};
398 285
@@ -414,21 +301,6 @@ static struct usb_gadget_strings *sourcesink_strings[] = {
414}; 301};
415 302
416/*-------------------------------------------------------------------------*/ 303/*-------------------------------------------------------------------------*/
417static const char *get_ep_string(enum eptype ep_type)
418{
419 switch (ep_type) {
420 case EP_ISOC:
421 return "ISOC-";
422 case EP_INTERRUPT:
423 return "INTERRUPT-";
424 case EP_CONTROL:
425 return "CTRL-";
426 case EP_BULK:
427 return "BULK-";
428 default:
429 return "UNKNOWN-";
430 }
431}
432 304
433static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len) 305static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
434{ 306{
@@ -456,8 +328,7 @@ static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
456 328
457void disable_endpoints(struct usb_composite_dev *cdev, 329void disable_endpoints(struct usb_composite_dev *cdev,
458 struct usb_ep *in, struct usb_ep *out, 330 struct usb_ep *in, struct usb_ep *out,
459 struct usb_ep *iso_in, struct usb_ep *iso_out, 331 struct usb_ep *iso_in, struct usb_ep *iso_out)
460 struct usb_ep *int_in, struct usb_ep *int_out)
461{ 332{
462 disable_ep(cdev, in); 333 disable_ep(cdev, in);
463 disable_ep(cdev, out); 334 disable_ep(cdev, out);
@@ -465,10 +336,6 @@ void disable_endpoints(struct usb_composite_dev *cdev,
465 disable_ep(cdev, iso_in); 336 disable_ep(cdev, iso_in);
466 if (iso_out) 337 if (iso_out)
467 disable_ep(cdev, iso_out); 338 disable_ep(cdev, iso_out);
468 if (int_in)
469 disable_ep(cdev, int_in);
470 if (int_out)
471 disable_ep(cdev, int_out);
472} 339}
473 340
474static int 341static int
@@ -485,7 +352,6 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
485 return id; 352 return id;
486 source_sink_intf_alt0.bInterfaceNumber = id; 353 source_sink_intf_alt0.bInterfaceNumber = id;
487 source_sink_intf_alt1.bInterfaceNumber = id; 354 source_sink_intf_alt1.bInterfaceNumber = id;
488 source_sink_intf_alt2.bInterfaceNumber = id;
489 355
490 /* allocate bulk endpoints */ 356 /* allocate bulk endpoints */
491 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); 357 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
@@ -546,55 +412,14 @@ no_iso:
546 if (isoc_maxpacket > 1024) 412 if (isoc_maxpacket > 1024)
547 isoc_maxpacket = 1024; 413 isoc_maxpacket = 1024;
548 414
549 /* sanity check the interrupt module parameters */
550 if (int_interval < 1)
551 int_interval = 1;
552 if (int_interval > 4096)
553 int_interval = 4096;
554 if (int_mult > 2)
555 int_mult = 2;
556 if (int_maxburst > 15)
557 int_maxburst = 15;
558
559 /* fill in the FS interrupt descriptors from the module parameters */
560 fs_int_source_desc.wMaxPacketSize = int_maxpacket > 64 ?
561 64 : int_maxpacket;
562 fs_int_source_desc.bInterval = int_interval > 255 ?
563 255 : int_interval;
564 fs_int_sink_desc.wMaxPacketSize = int_maxpacket > 64 ?
565 64 : int_maxpacket;
566 fs_int_sink_desc.bInterval = int_interval > 255 ?
567 255 : int_interval;
568
569 /* allocate int endpoints */
570 ss->int_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_source_desc);
571 if (!ss->int_in_ep)
572 goto no_int;
573 ss->int_in_ep->driver_data = cdev; /* claim */
574
575 ss->int_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_sink_desc);
576 if (ss->int_out_ep) {
577 ss->int_out_ep->driver_data = cdev; /* claim */
578 } else {
579 ss->int_in_ep->driver_data = NULL;
580 ss->int_in_ep = NULL;
581no_int:
582 fs_source_sink_descs[FS_ALT_IFC_2_OFFSET] = NULL;
583 hs_source_sink_descs[HS_ALT_IFC_2_OFFSET] = NULL;
584 ss_source_sink_descs[SS_ALT_IFC_2_OFFSET] = NULL;
585 }
586
587 if (int_maxpacket > 1024)
588 int_maxpacket = 1024;
589
590 /* support high speed hardware */ 415 /* support high speed hardware */
591 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; 416 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
592 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; 417 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
593 418
594 /* 419 /*
595 * Fill in the HS isoc and interrupt descriptors from the module 420 * Fill in the HS isoc descriptors from the module parameters.
596 * parameters. We assume that the user knows what they are doing and 421 * We assume that the user knows what they are doing and won't
597 * won't give parameters that their UDC doesn't support. 422 * give parameters that their UDC doesn't support.
598 */ 423 */
599 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket; 424 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
600 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11; 425 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
@@ -607,17 +432,6 @@ no_int:
607 hs_iso_sink_desc.bInterval = isoc_interval; 432 hs_iso_sink_desc.bInterval = isoc_interval;
608 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; 433 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
609 434
610 hs_int_source_desc.wMaxPacketSize = int_maxpacket;
611 hs_int_source_desc.wMaxPacketSize |= int_mult << 11;
612 hs_int_source_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
613 hs_int_source_desc.bEndpointAddress =
614 fs_int_source_desc.bEndpointAddress;
615
616 hs_int_sink_desc.wMaxPacketSize = int_maxpacket;
617 hs_int_sink_desc.wMaxPacketSize |= int_mult << 11;
618 hs_int_sink_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
619 hs_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
620
621 /* support super speed hardware */ 435 /* support super speed hardware */
622 ss_source_desc.bEndpointAddress = 436 ss_source_desc.bEndpointAddress =
623 fs_source_desc.bEndpointAddress; 437 fs_source_desc.bEndpointAddress;
@@ -625,9 +439,9 @@ no_int:
625 fs_sink_desc.bEndpointAddress; 439 fs_sink_desc.bEndpointAddress;
626 440
627 /* 441 /*
628 * Fill in the SS isoc and interrupt descriptors from the module 442 * Fill in the SS isoc descriptors from the module parameters.
629 * parameters. We assume that the user knows what they are doing and 443 * We assume that the user knows what they are doing and won't
630 * won't give parameters that their UDC doesn't support. 444 * give parameters that their UDC doesn't support.
631 */ 445 */
632 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket; 446 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
633 ss_iso_source_desc.bInterval = isoc_interval; 447 ss_iso_source_desc.bInterval = isoc_interval;
@@ -646,37 +460,17 @@ no_int:
646 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); 460 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
647 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; 461 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
648 462
649 ss_int_source_desc.wMaxPacketSize = int_maxpacket;
650 ss_int_source_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
651 ss_int_source_comp_desc.bmAttributes = int_mult;
652 ss_int_source_comp_desc.bMaxBurst = int_maxburst;
653 ss_int_source_comp_desc.wBytesPerInterval =
654 int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
655 ss_int_source_desc.bEndpointAddress =
656 fs_int_source_desc.bEndpointAddress;
657
658 ss_int_sink_desc.wMaxPacketSize = int_maxpacket;
659 ss_int_sink_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
660 ss_int_sink_comp_desc.bmAttributes = int_mult;
661 ss_int_sink_comp_desc.bMaxBurst = int_maxburst;
662 ss_int_sink_comp_desc.wBytesPerInterval =
663 int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
664 ss_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
665
666 ret = usb_assign_descriptors(f, fs_source_sink_descs, 463 ret = usb_assign_descriptors(f, fs_source_sink_descs,
667 hs_source_sink_descs, ss_source_sink_descs); 464 hs_source_sink_descs, ss_source_sink_descs);
668 if (ret) 465 if (ret)
669 return ret; 466 return ret;
670 467
671 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s, " 468 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
672 "INT-IN/%s, INT-OUT/%s\n",
673 (gadget_is_superspeed(c->cdev->gadget) ? "super" : 469 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
674 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")), 470 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
675 f->name, ss->in_ep->name, ss->out_ep->name, 471 f->name, ss->in_ep->name, ss->out_ep->name,
676 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>", 472 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
677 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>", 473 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
678 ss->int_in_ep ? ss->int_in_ep->name : "<none>",
679 ss->int_out_ep ? ss->int_out_ep->name : "<none>");
680 return 0; 474 return 0;
681} 475}
682 476
@@ -807,15 +601,14 @@ static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
807} 601}
808 602
809static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, 603static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
810 enum eptype ep_type, int speed) 604 bool is_iso, int speed)
811{ 605{
812 struct usb_ep *ep; 606 struct usb_ep *ep;
813 struct usb_request *req; 607 struct usb_request *req;
814 int i, size, status; 608 int i, size, status;
815 609
816 for (i = 0; i < 8; i++) { 610 for (i = 0; i < 8; i++) {
817 switch (ep_type) { 611 if (is_iso) {
818 case EP_ISOC:
819 switch (speed) { 612 switch (speed) {
820 case USB_SPEED_SUPER: 613 case USB_SPEED_SUPER:
821 size = isoc_maxpacket * (isoc_mult + 1) * 614 size = isoc_maxpacket * (isoc_mult + 1) *
@@ -831,28 +624,9 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
831 } 624 }
832 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; 625 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
833 req = ss_alloc_ep_req(ep, size); 626 req = ss_alloc_ep_req(ep, size);
834 break; 627 } else {
835 case EP_INTERRUPT:
836 switch (speed) {
837 case USB_SPEED_SUPER:
838 size = int_maxpacket * (int_mult + 1) *
839 (int_maxburst + 1);
840 break;
841 case USB_SPEED_HIGH:
842 size = int_maxpacket * (int_mult + 1);
843 break;
844 default:
845 size = int_maxpacket > 1023 ?
846 1023 : int_maxpacket;
847 break;
848 }
849 ep = is_in ? ss->int_in_ep : ss->int_out_ep;
850 req = ss_alloc_ep_req(ep, size);
851 break;
852 default:
853 ep = is_in ? ss->in_ep : ss->out_ep; 628 ep = is_in ? ss->in_ep : ss->out_ep;
854 req = ss_alloc_ep_req(ep, 0); 629 req = ss_alloc_ep_req(ep, 0);
855 break;
856 } 630 }
857 631
858 if (!req) 632 if (!req)
@@ -870,12 +644,12 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
870 644
871 cdev = ss->function.config->cdev; 645 cdev = ss->function.config->cdev;
872 ERROR(cdev, "start %s%s %s --> %d\n", 646 ERROR(cdev, "start %s%s %s --> %d\n",
873 get_ep_string(ep_type), is_in ? "IN" : "OUT", 647 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
874 ep->name, status); 648 ep->name, status);
875 free_ep_req(ep, req); 649 free_ep_req(ep, req);
876 } 650 }
877 651
878 if (!(ep_type == EP_ISOC)) 652 if (!is_iso)
879 break; 653 break;
880 } 654 }
881 655
@@ -888,7 +662,7 @@ static void disable_source_sink(struct f_sourcesink *ss)
888 662
889 cdev = ss->function.config->cdev; 663 cdev = ss->function.config->cdev;
890 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep, 664 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
891 ss->iso_out_ep, ss->int_in_ep, ss->int_out_ep); 665 ss->iso_out_ep);
892 VDBG(cdev, "%s disabled\n", ss->function.name); 666 VDBG(cdev, "%s disabled\n", ss->function.name);
893} 667}
894 668
@@ -900,62 +674,6 @@ enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
900 int speed = cdev->gadget->speed; 674 int speed = cdev->gadget->speed;
901 struct usb_ep *ep; 675 struct usb_ep *ep;
902 676
903 if (alt == 2) {
904 /* Configure for periodic interrupt endpoint */
905 ep = ss->int_in_ep;
906 if (ep) {
907 result = config_ep_by_speed(cdev->gadget,
908 &(ss->function), ep);
909 if (result)
910 return result;
911
912 result = usb_ep_enable(ep);
913 if (result < 0)
914 return result;
915
916 ep->driver_data = ss;
917 result = source_sink_start_ep(ss, true, EP_INTERRUPT,
918 speed);
919 if (result < 0) {
920fail1:
921 ep = ss->int_in_ep;
922 if (ep) {
923 usb_ep_disable(ep);
924 ep->driver_data = NULL;
925 }
926 return result;
927 }
928 }
929
930 /*
931 * one interrupt endpoint reads (sinks) anything OUT (from the
932 * host)
933 */
934 ep = ss->int_out_ep;
935 if (ep) {
936 result = config_ep_by_speed(cdev->gadget,
937 &(ss->function), ep);
938 if (result)
939 goto fail1;
940
941 result = usb_ep_enable(ep);
942 if (result < 0)
943 goto fail1;
944
945 ep->driver_data = ss;
946 result = source_sink_start_ep(ss, false, EP_INTERRUPT,
947 speed);
948 if (result < 0) {
949 ep = ss->int_out_ep;
950 usb_ep_disable(ep);
951 ep->driver_data = NULL;
952 goto fail1;
953 }
954 }
955
956 goto out;
957 }
958
959 /* one bulk endpoint writes (sources) zeroes IN (to the host) */ 677 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
960 ep = ss->in_ep; 678 ep = ss->in_ep;
961 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); 679 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
@@ -966,7 +684,7 @@ fail1:
966 return result; 684 return result;
967 ep->driver_data = ss; 685 ep->driver_data = ss;
968 686
969 result = source_sink_start_ep(ss, true, EP_BULK, speed); 687 result = source_sink_start_ep(ss, true, false, speed);
970 if (result < 0) { 688 if (result < 0) {
971fail: 689fail:
972 ep = ss->in_ep; 690 ep = ss->in_ep;
@@ -985,7 +703,7 @@ fail:
985 goto fail; 703 goto fail;
986 ep->driver_data = ss; 704 ep->driver_data = ss;
987 705
988 result = source_sink_start_ep(ss, false, EP_BULK, speed); 706 result = source_sink_start_ep(ss, false, false, speed);
989 if (result < 0) { 707 if (result < 0) {
990fail2: 708fail2:
991 ep = ss->out_ep; 709 ep = ss->out_ep;
@@ -1008,7 +726,7 @@ fail2:
1008 goto fail2; 726 goto fail2;
1009 ep->driver_data = ss; 727 ep->driver_data = ss;
1010 728
1011 result = source_sink_start_ep(ss, true, EP_ISOC, speed); 729 result = source_sink_start_ep(ss, true, true, speed);
1012 if (result < 0) { 730 if (result < 0) {
1013fail3: 731fail3:
1014 ep = ss->iso_in_ep; 732 ep = ss->iso_in_ep;
@@ -1031,14 +749,13 @@ fail3:
1031 goto fail3; 749 goto fail3;
1032 ep->driver_data = ss; 750 ep->driver_data = ss;
1033 751
1034 result = source_sink_start_ep(ss, false, EP_ISOC, speed); 752 result = source_sink_start_ep(ss, false, true, speed);
1035 if (result < 0) { 753 if (result < 0) {
1036 usb_ep_disable(ep); 754 usb_ep_disable(ep);
1037 ep->driver_data = NULL; 755 ep->driver_data = NULL;
1038 goto fail3; 756 goto fail3;
1039 } 757 }
1040 } 758 }
1041
1042out: 759out:
1043 ss->cur_alt = alt; 760 ss->cur_alt = alt;
1044 761
@@ -1054,8 +771,6 @@ static int sourcesink_set_alt(struct usb_function *f,
1054 771
1055 if (ss->in_ep->driver_data) 772 if (ss->in_ep->driver_data)
1056 disable_source_sink(ss); 773 disable_source_sink(ss);
1057 else if (alt == 2 && ss->int_in_ep->driver_data)
1058 disable_source_sink(ss);
1059 return enable_source_sink(cdev, ss, alt); 774 return enable_source_sink(cdev, ss, alt);
1060} 775}
1061 776
@@ -1168,10 +883,6 @@ static struct usb_function *source_sink_alloc_func(
1168 isoc_maxpacket = ss_opts->isoc_maxpacket; 883 isoc_maxpacket = ss_opts->isoc_maxpacket;
1169 isoc_mult = ss_opts->isoc_mult; 884 isoc_mult = ss_opts->isoc_mult;
1170 isoc_maxburst = ss_opts->isoc_maxburst; 885 isoc_maxburst = ss_opts->isoc_maxburst;
1171 int_interval = ss_opts->int_interval;
1172 int_maxpacket = ss_opts->int_maxpacket;
1173 int_mult = ss_opts->int_mult;
1174 int_maxburst = ss_opts->int_maxburst;
1175 buflen = ss_opts->bulk_buflen; 886 buflen = ss_opts->bulk_buflen;
1176 887
1177 ss->function.name = "source/sink"; 888 ss->function.name = "source/sink";
@@ -1468,182 +1179,6 @@ static struct f_ss_opts_attribute f_ss_opts_bulk_buflen =
1468 f_ss_opts_bulk_buflen_show, 1179 f_ss_opts_bulk_buflen_show,
1469 f_ss_opts_bulk_buflen_store); 1180 f_ss_opts_bulk_buflen_store);
1470 1181
1471static ssize_t f_ss_opts_int_interval_show(struct f_ss_opts *opts, char *page)
1472{
1473 int result;
1474
1475 mutex_lock(&opts->lock);
1476 result = sprintf(page, "%u", opts->int_interval);
1477 mutex_unlock(&opts->lock);
1478
1479 return result;
1480}
1481
1482static ssize_t f_ss_opts_int_interval_store(struct f_ss_opts *opts,
1483 const char *page, size_t len)
1484{
1485 int ret;
1486 u32 num;
1487
1488 mutex_lock(&opts->lock);
1489 if (opts->refcnt) {
1490 ret = -EBUSY;
1491 goto end;
1492 }
1493
1494 ret = kstrtou32(page, 0, &num);
1495 if (ret)
1496 goto end;
1497
1498 if (num > 4096) {
1499 ret = -EINVAL;
1500 goto end;
1501 }
1502
1503 opts->int_interval = num;
1504 ret = len;
1505end:
1506 mutex_unlock(&opts->lock);
1507 return ret;
1508}
1509
1510static struct f_ss_opts_attribute f_ss_opts_int_interval =
1511 __CONFIGFS_ATTR(int_interval, S_IRUGO | S_IWUSR,
1512 f_ss_opts_int_interval_show,
1513 f_ss_opts_int_interval_store);
1514
1515static ssize_t f_ss_opts_int_maxpacket_show(struct f_ss_opts *opts, char *page)
1516{
1517 int result;
1518
1519 mutex_lock(&opts->lock);
1520 result = sprintf(page, "%u", opts->int_maxpacket);
1521 mutex_unlock(&opts->lock);
1522
1523 return result;
1524}
1525
1526static ssize_t f_ss_opts_int_maxpacket_store(struct f_ss_opts *opts,
1527 const char *page, size_t len)
1528{
1529 int ret;
1530 u16 num;
1531
1532 mutex_lock(&opts->lock);
1533 if (opts->refcnt) {
1534 ret = -EBUSY;
1535 goto end;
1536 }
1537
1538 ret = kstrtou16(page, 0, &num);
1539 if (ret)
1540 goto end;
1541
1542 if (num > 1024) {
1543 ret = -EINVAL;
1544 goto end;
1545 }
1546
1547 opts->int_maxpacket = num;
1548 ret = len;
1549end:
1550 mutex_unlock(&opts->lock);
1551 return ret;
1552}
1553
1554static struct f_ss_opts_attribute f_ss_opts_int_maxpacket =
1555 __CONFIGFS_ATTR(int_maxpacket, S_IRUGO | S_IWUSR,
1556 f_ss_opts_int_maxpacket_show,
1557 f_ss_opts_int_maxpacket_store);
1558
1559static ssize_t f_ss_opts_int_mult_show(struct f_ss_opts *opts, char *page)
1560{
1561 int result;
1562
1563 mutex_lock(&opts->lock);
1564 result = sprintf(page, "%u", opts->int_mult);
1565 mutex_unlock(&opts->lock);
1566
1567 return result;
1568}
1569
1570static ssize_t f_ss_opts_int_mult_store(struct f_ss_opts *opts,
1571 const char *page, size_t len)
1572{
1573 int ret;
1574 u8 num;
1575
1576 mutex_lock(&opts->lock);
1577 if (opts->refcnt) {
1578 ret = -EBUSY;
1579 goto end;
1580 }
1581
1582 ret = kstrtou8(page, 0, &num);
1583 if (ret)
1584 goto end;
1585
1586 if (num > 2) {
1587 ret = -EINVAL;
1588 goto end;
1589 }
1590
1591 opts->int_mult = num;
1592 ret = len;
1593end:
1594 mutex_unlock(&opts->lock);
1595 return ret;
1596}
1597
1598static struct f_ss_opts_attribute f_ss_opts_int_mult =
1599 __CONFIGFS_ATTR(int_mult, S_IRUGO | S_IWUSR,
1600 f_ss_opts_int_mult_show,
1601 f_ss_opts_int_mult_store);
1602
1603static ssize_t f_ss_opts_int_maxburst_show(struct f_ss_opts *opts, char *page)
1604{
1605 int result;
1606
1607 mutex_lock(&opts->lock);
1608 result = sprintf(page, "%u", opts->int_maxburst);
1609 mutex_unlock(&opts->lock);
1610
1611 return result;
1612}
1613
1614static ssize_t f_ss_opts_int_maxburst_store(struct f_ss_opts *opts,
1615 const char *page, size_t len)
1616{
1617 int ret;
1618 u8 num;
1619
1620 mutex_lock(&opts->lock);
1621 if (opts->refcnt) {
1622 ret = -EBUSY;
1623 goto end;
1624 }
1625
1626 ret = kstrtou8(page, 0, &num);
1627 if (ret)
1628 goto end;
1629
1630 if (num > 15) {
1631 ret = -EINVAL;
1632 goto end;
1633 }
1634
1635 opts->int_maxburst = num;
1636 ret = len;
1637end:
1638 mutex_unlock(&opts->lock);
1639 return ret;
1640}
1641
1642static struct f_ss_opts_attribute f_ss_opts_int_maxburst =
1643 __CONFIGFS_ATTR(int_maxburst, S_IRUGO | S_IWUSR,
1644 f_ss_opts_int_maxburst_show,
1645 f_ss_opts_int_maxburst_store);
1646
1647static struct configfs_attribute *ss_attrs[] = { 1182static struct configfs_attribute *ss_attrs[] = {
1648 &f_ss_opts_pattern.attr, 1183 &f_ss_opts_pattern.attr,
1649 &f_ss_opts_isoc_interval.attr, 1184 &f_ss_opts_isoc_interval.attr,
@@ -1651,10 +1186,6 @@ static struct configfs_attribute *ss_attrs[] = {
1651 &f_ss_opts_isoc_mult.attr, 1186 &f_ss_opts_isoc_mult.attr,
1652 &f_ss_opts_isoc_maxburst.attr, 1187 &f_ss_opts_isoc_maxburst.attr,
1653 &f_ss_opts_bulk_buflen.attr, 1188 &f_ss_opts_bulk_buflen.attr,
1654 &f_ss_opts_int_interval.attr,
1655 &f_ss_opts_int_maxpacket.attr,
1656 &f_ss_opts_int_mult.attr,
1657 &f_ss_opts_int_maxburst.attr,
1658 NULL, 1189 NULL,
1659}; 1190};
1660 1191
@@ -1684,8 +1215,6 @@ static struct usb_function_instance *source_sink_alloc_inst(void)
1684 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL; 1215 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1685 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET; 1216 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1686 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN; 1217 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1687 ss_opts->int_interval = GZERO_INT_INTERVAL;
1688 ss_opts->int_maxpacket = GZERO_INT_MAXPACKET;
1689 1218
1690 config_group_init_type_name(&ss_opts->func_inst.group, "", 1219 config_group_init_type_name(&ss_opts->func_inst.group, "",
1691 &ss_func_type); 1220 &ss_func_type);
diff --git a/drivers/usb/gadget/function/g_zero.h b/drivers/usb/gadget/function/g_zero.h
index 2ce28b9d97cc..15f180904f8a 100644
--- a/drivers/usb/gadget/function/g_zero.h
+++ b/drivers/usb/gadget/function/g_zero.h
@@ -10,8 +10,6 @@
10#define GZERO_QLEN 32 10#define GZERO_QLEN 32
11#define GZERO_ISOC_INTERVAL 4 11#define GZERO_ISOC_INTERVAL 4
12#define GZERO_ISOC_MAXPACKET 1024 12#define GZERO_ISOC_MAXPACKET 1024
13#define GZERO_INT_INTERVAL 1 /* Default interrupt interval = 1 ms */
14#define GZERO_INT_MAXPACKET 1024
15 13
16struct usb_zero_options { 14struct usb_zero_options {
17 unsigned pattern; 15 unsigned pattern;
@@ -19,10 +17,6 @@ struct usb_zero_options {
19 unsigned isoc_maxpacket; 17 unsigned isoc_maxpacket;
20 unsigned isoc_mult; 18 unsigned isoc_mult;
21 unsigned isoc_maxburst; 19 unsigned isoc_maxburst;
22 unsigned int_interval; /* In ms */
23 unsigned int_maxpacket;
24 unsigned int_mult;
25 unsigned int_maxburst;
26 unsigned bulk_buflen; 20 unsigned bulk_buflen;
27 unsigned qlen; 21 unsigned qlen;
28}; 22};
@@ -34,10 +28,6 @@ struct f_ss_opts {
34 unsigned isoc_maxpacket; 28 unsigned isoc_maxpacket;
35 unsigned isoc_mult; 29 unsigned isoc_mult;
36 unsigned isoc_maxburst; 30 unsigned isoc_maxburst;
37 unsigned int_interval; /* In ms */
38 unsigned int_maxpacket;
39 unsigned int_mult;
40 unsigned int_maxburst;
41 unsigned bulk_buflen; 31 unsigned bulk_buflen;
42 32
43 /* 33 /*
@@ -72,7 +62,6 @@ int lb_modinit(void);
72void free_ep_req(struct usb_ep *ep, struct usb_request *req); 62void free_ep_req(struct usb_ep *ep, struct usb_request *req);
73void disable_endpoints(struct usb_composite_dev *cdev, 63void disable_endpoints(struct usb_composite_dev *cdev,
74 struct usb_ep *in, struct usb_ep *out, 64 struct usb_ep *in, struct usb_ep *out,
75 struct usb_ep *iso_in, struct usb_ep *iso_out, 65 struct usb_ep *iso_in, struct usb_ep *iso_out);
76 struct usb_ep *int_in, struct usb_ep *int_out);
77 66
78#endif /* __G_ZERO_H */ 67#endif /* __G_ZERO_H */
diff --git a/drivers/usb/gadget/function/u_printer.h b/drivers/usb/gadget/function/u_printer.h
new file mode 100644
index 000000000000..0e2c49d4274e
--- /dev/null
+++ b/drivers/usb/gadget/function/u_printer.h
@@ -0,0 +1,37 @@
1/*
2 * u_printer.h
3 *
4 * Utility definitions for the printer function
5 *
6 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
8 *
9 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#ifndef U_PRINTER_H
17#define U_PRINTER_H
18
19#include <linux/usb/composite.h>
20
21#define PNP_STRING_LEN 1024
22
23struct f_printer_opts {
24 struct usb_function_instance func_inst;
25 int minor;
26 char pnp_string[PNP_STRING_LEN];
27 unsigned q_len;
28
29 /*
30 * Protect the data from concurrent access by read/write
31 * and create symlink/remove symlink
32 */
33 struct mutex lock;
34 int refcnt;
35};
36
37#endif /* U_PRINTER_H */
diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index 491082aaf103..89179ab20c10 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -912,7 +912,7 @@ static int gs_put_char(struct tty_struct *tty, unsigned char ch)
912 unsigned long flags; 912 unsigned long flags;
913 int status; 913 int status;
914 914
915 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %pf\n", 915 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
916 port->port_num, tty, ch, __builtin_return_address(0)); 916 port->port_num, tty, ch, __builtin_return_address(0));
917 917
918 spin_lock_irqsave(&port->port_lock, flags); 918 spin_lock_irqsave(&port->port_lock, flags);
diff --git a/drivers/usb/gadget/legacy/Kconfig b/drivers/usb/gadget/legacy/Kconfig
index 113c87e22117..d5a7102de696 100644
--- a/drivers/usb/gadget/legacy/Kconfig
+++ b/drivers/usb/gadget/legacy/Kconfig
@@ -301,6 +301,7 @@ config USB_MIDI_GADGET
301config USB_G_PRINTER 301config USB_G_PRINTER
302 tristate "Printer Gadget" 302 tristate "Printer Gadget"
303 select USB_LIBCOMPOSITE 303 select USB_LIBCOMPOSITE
304 select USB_F_PRINTER
304 help 305 help
305 The Printer Gadget channels data between the USB host and a 306 The Printer Gadget channels data between the USB host and a
306 userspace program driving the print engine. The user space 307 userspace program driving the print engine. The user space
diff --git a/drivers/usb/gadget/legacy/printer.c b/drivers/usb/gadget/legacy/printer.c
index 90545980542f..d5b6ee725a2a 100644
--- a/drivers/usb/gadget/legacy/printer.c
+++ b/drivers/usb/gadget/legacy/printer.c
@@ -12,29 +12,7 @@
12 12
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/ioport.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/mutex.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/timer.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/device.h>
26#include <linux/moduleparam.h>
27#include <linux/fs.h>
28#include <linux/poll.h>
29#include <linux/types.h>
30#include <linux/ctype.h>
31#include <linux/cdev.h>
32
33#include <asm/byteorder.h> 15#include <asm/byteorder.h>
34#include <linux/io.h>
35#include <linux/irq.h>
36#include <linux/uaccess.h>
37#include <asm/unaligned.h>
38 16
39#include <linux/usb/ch9.h> 17#include <linux/usb/ch9.h>
40#include <linux/usb/composite.h> 18#include <linux/usb/composite.h>
@@ -46,50 +24,12 @@
46USB_GADGET_COMPOSITE_OPTIONS(); 24USB_GADGET_COMPOSITE_OPTIONS();
47 25
48#define DRIVER_DESC "Printer Gadget" 26#define DRIVER_DESC "Printer Gadget"
49#define DRIVER_VERSION "2007 OCT 06" 27#define DRIVER_VERSION "2015 FEB 17"
50 28
51static DEFINE_MUTEX(printer_mutex);
52static const char shortname [] = "printer"; 29static const char shortname [] = "printer";
53static const char driver_desc [] = DRIVER_DESC; 30static const char driver_desc [] = DRIVER_DESC;
54 31
55static dev_t g_printer_devno; 32#include "u_printer.h"
56
57static struct class *usb_gadget_class;
58
59/*-------------------------------------------------------------------------*/
60
61struct printer_dev {
62 spinlock_t lock; /* lock this structure */
63 /* lock buffer lists during read/write calls */
64 struct mutex lock_printer_io;
65 struct usb_gadget *gadget;
66 s8 interface;
67 struct usb_ep *in_ep, *out_ep;
68
69 struct list_head rx_reqs; /* List of free RX structs */
70 struct list_head rx_reqs_active; /* List of Active RX xfers */
71 struct list_head rx_buffers; /* List of completed xfers */
72 /* wait until there is data to be read. */
73 wait_queue_head_t rx_wait;
74 struct list_head tx_reqs; /* List of free TX structs */
75 struct list_head tx_reqs_active; /* List of Active TX xfers */
76 /* Wait until there are write buffers available to use. */
77 wait_queue_head_t tx_wait;
78 /* Wait until all write buffers have been sent. */
79 wait_queue_head_t tx_flush_wait;
80 struct usb_request *current_rx_req;
81 size_t current_rx_bytes;
82 u8 *current_rx_buf;
83 u8 printer_status;
84 u8 reset_printer;
85 struct cdev printer_cdev;
86 struct device *pdev;
87 u8 printer_cdev_open;
88 wait_queue_head_t wait;
89 struct usb_function function;
90};
91
92static struct printer_dev usb_printer_gadget;
93 33
94/*-------------------------------------------------------------------------*/ 34/*-------------------------------------------------------------------------*/
95 35
@@ -120,6 +60,9 @@ module_param(qlen, uint, S_IRUGO|S_IWUSR);
120 60
121#define QLEN qlen 61#define QLEN qlen
122 62
63static struct usb_function_instance *fi_printer;
64static struct usb_function *f_printer;
65
123/*-------------------------------------------------------------------------*/ 66/*-------------------------------------------------------------------------*/
124 67
125/* 68/*
@@ -127,10 +70,6 @@ module_param(qlen, uint, S_IRUGO|S_IWUSR);
127 * descriptors are built on demand. 70 * descriptors are built on demand.
128 */ 71 */
129 72
130/* holds our biggest descriptor */
131#define USB_DESC_BUFSIZE 256
132#define USB_BUFSIZE 8192
133
134static struct usb_device_descriptor device_desc = { 73static struct usb_device_descriptor device_desc = {
135 .bLength = sizeof device_desc, 74 .bLength = sizeof device_desc,
136 .bDescriptorType = USB_DT_DEVICE, 75 .bDescriptorType = USB_DT_DEVICE,
@@ -143,108 +82,6 @@ static struct usb_device_descriptor device_desc = {
143 .bNumConfigurations = 1 82 .bNumConfigurations = 1
144}; 83};
145 84
146static struct usb_interface_descriptor intf_desc = {
147 .bLength = sizeof intf_desc,
148 .bDescriptorType = USB_DT_INTERFACE,
149 .bNumEndpoints = 2,
150 .bInterfaceClass = USB_CLASS_PRINTER,
151 .bInterfaceSubClass = 1, /* Printer Sub-Class */
152 .bInterfaceProtocol = 2, /* Bi-Directional */
153 .iInterface = 0
154};
155
156static struct usb_endpoint_descriptor fs_ep_in_desc = {
157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK
161};
162
163static struct usb_endpoint_descriptor fs_ep_out_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_OUT,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK
168};
169
170static struct usb_descriptor_header *fs_printer_function[] = {
171 (struct usb_descriptor_header *) &intf_desc,
172 (struct usb_descriptor_header *) &fs_ep_in_desc,
173 (struct usb_descriptor_header *) &fs_ep_out_desc,
174 NULL
175};
176
177/*
178 * usb 2.0 devices need to expose both high speed and full speed
179 * descriptors, unless they only run at full speed.
180 */
181
182static struct usb_endpoint_descriptor hs_ep_in_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
186 .wMaxPacketSize = cpu_to_le16(512)
187};
188
189static struct usb_endpoint_descriptor hs_ep_out_desc = {
190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bmAttributes = USB_ENDPOINT_XFER_BULK,
193 .wMaxPacketSize = cpu_to_le16(512)
194};
195
196static struct usb_qualifier_descriptor dev_qualifier = {
197 .bLength = sizeof dev_qualifier,
198 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
199 .bcdUSB = cpu_to_le16(0x0200),
200 .bDeviceClass = USB_CLASS_PRINTER,
201 .bNumConfigurations = 1
202};
203
204static struct usb_descriptor_header *hs_printer_function[] = {
205 (struct usb_descriptor_header *) &intf_desc,
206 (struct usb_descriptor_header *) &hs_ep_in_desc,
207 (struct usb_descriptor_header *) &hs_ep_out_desc,
208 NULL
209};
210
211/*
212 * Added endpoint descriptors for 3.0 devices
213 */
214
215static struct usb_endpoint_descriptor ss_ep_in_desc = {
216 .bLength = USB_DT_ENDPOINT_SIZE,
217 .bDescriptorType = USB_DT_ENDPOINT,
218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
219 .wMaxPacketSize = cpu_to_le16(1024),
220};
221
222static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
223 .bLength = sizeof(ss_ep_in_comp_desc),
224 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
225};
226
227static struct usb_endpoint_descriptor ss_ep_out_desc = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230 .bmAttributes = USB_ENDPOINT_XFER_BULK,
231 .wMaxPacketSize = cpu_to_le16(1024),
232};
233
234static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
235 .bLength = sizeof(ss_ep_out_comp_desc),
236 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
237};
238
239static struct usb_descriptor_header *ss_printer_function[] = {
240 (struct usb_descriptor_header *) &intf_desc,
241 (struct usb_descriptor_header *) &ss_ep_in_desc,
242 (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
243 (struct usb_descriptor_header *) &ss_ep_out_desc,
244 (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
245 NULL
246};
247
248static struct usb_otg_descriptor otg_descriptor = { 85static struct usb_otg_descriptor otg_descriptor = {
249 .bLength = sizeof otg_descriptor, 86 .bLength = sizeof otg_descriptor,
250 .bDescriptorType = USB_DT_OTG, 87 .bDescriptorType = USB_DT_OTG,
@@ -256,29 +93,13 @@ static const struct usb_descriptor_header *otg_desc[] = {
256 NULL, 93 NULL,
257}; 94};
258 95
259/* maxpacket and other transfer characteristics vary by speed. */
260static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
261 struct usb_endpoint_descriptor *fs,
262 struct usb_endpoint_descriptor *hs,
263 struct usb_endpoint_descriptor *ss)
264{
265 switch (gadget->speed) {
266 case USB_SPEED_SUPER:
267 return ss;
268 case USB_SPEED_HIGH:
269 return hs;
270 default:
271 return fs;
272 }
273}
274
275/*-------------------------------------------------------------------------*/ 96/*-------------------------------------------------------------------------*/
276 97
277/* descriptors that are built on-demand */ 98/* descriptors that are built on-demand */
278 99
279static char product_desc [40] = DRIVER_DESC; 100static char product_desc [40] = DRIVER_DESC;
280static char serial_num [40] = "1"; 101static char serial_num [40] = "1";
281static char pnp_string [1024] = 102static char pnp_string[PNP_STRING_LEN] =
282 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; 103 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
283 104
284/* static strings, in UTF-8 */ 105/* static strings, in UTF-8 */
@@ -299,921 +120,19 @@ static struct usb_gadget_strings *dev_strings[] = {
299 NULL, 120 NULL,
300}; 121};
301 122
302/*-------------------------------------------------------------------------*/
303
304static struct usb_request *
305printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
306{
307 struct usb_request *req;
308
309 req = usb_ep_alloc_request(ep, gfp_flags);
310
311 if (req != NULL) {
312 req->length = len;
313 req->buf = kmalloc(len, gfp_flags);
314 if (req->buf == NULL) {
315 usb_ep_free_request(ep, req);
316 return NULL;
317 }
318 }
319
320 return req;
321}
322
323static void
324printer_req_free(struct usb_ep *ep, struct usb_request *req)
325{
326 if (ep != NULL && req != NULL) {
327 kfree(req->buf);
328 usb_ep_free_request(ep, req);
329 }
330}
331
332/*-------------------------------------------------------------------------*/
333
334static void rx_complete(struct usb_ep *ep, struct usb_request *req)
335{
336 struct printer_dev *dev = ep->driver_data;
337 int status = req->status;
338 unsigned long flags;
339
340 spin_lock_irqsave(&dev->lock, flags);
341
342 list_del_init(&req->list); /* Remode from Active List */
343
344 switch (status) {
345
346 /* normal completion */
347 case 0:
348 if (req->actual > 0) {
349 list_add_tail(&req->list, &dev->rx_buffers);
350 DBG(dev, "G_Printer : rx length %d\n", req->actual);
351 } else {
352 list_add(&req->list, &dev->rx_reqs);
353 }
354 break;
355
356 /* software-driven interface shutdown */
357 case -ECONNRESET: /* unlink */
358 case -ESHUTDOWN: /* disconnect etc */
359 VDBG(dev, "rx shutdown, code %d\n", status);
360 list_add(&req->list, &dev->rx_reqs);
361 break;
362
363 /* for hardware automagic (such as pxa) */
364 case -ECONNABORTED: /* endpoint reset */
365 DBG(dev, "rx %s reset\n", ep->name);
366 list_add(&req->list, &dev->rx_reqs);
367 break;
368
369 /* data overrun */
370 case -EOVERFLOW:
371 /* FALLTHROUGH */
372
373 default:
374 DBG(dev, "rx status %d\n", status);
375 list_add(&req->list, &dev->rx_reqs);
376 break;
377 }
378
379 wake_up_interruptible(&dev->rx_wait);
380 spin_unlock_irqrestore(&dev->lock, flags);
381}
382
383static void tx_complete(struct usb_ep *ep, struct usb_request *req)
384{
385 struct printer_dev *dev = ep->driver_data;
386
387 switch (req->status) {
388 default:
389 VDBG(dev, "tx err %d\n", req->status);
390 /* FALLTHROUGH */
391 case -ECONNRESET: /* unlink */
392 case -ESHUTDOWN: /* disconnect etc */
393 break;
394 case 0:
395 break;
396 }
397
398 spin_lock(&dev->lock);
399 /* Take the request struct off the active list and put it on the
400 * free list.
401 */
402 list_del_init(&req->list);
403 list_add(&req->list, &dev->tx_reqs);
404 wake_up_interruptible(&dev->tx_wait);
405 if (likely(list_empty(&dev->tx_reqs_active)))
406 wake_up_interruptible(&dev->tx_flush_wait);
407
408 spin_unlock(&dev->lock);
409}
410
411/*-------------------------------------------------------------------------*/
412
413static int
414printer_open(struct inode *inode, struct file *fd)
415{
416 struct printer_dev *dev;
417 unsigned long flags;
418 int ret = -EBUSY;
419
420 mutex_lock(&printer_mutex);
421 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
422
423 spin_lock_irqsave(&dev->lock, flags);
424
425 if (!dev->printer_cdev_open) {
426 dev->printer_cdev_open = 1;
427 fd->private_data = dev;
428 ret = 0;
429 /* Change the printer status to show that it's on-line. */
430 dev->printer_status |= PRINTER_SELECTED;
431 }
432
433 spin_unlock_irqrestore(&dev->lock, flags);
434
435 DBG(dev, "printer_open returned %x\n", ret);
436 mutex_unlock(&printer_mutex);
437 return ret;
438}
439
440static int
441printer_close(struct inode *inode, struct file *fd)
442{
443 struct printer_dev *dev = fd->private_data;
444 unsigned long flags;
445
446 spin_lock_irqsave(&dev->lock, flags);
447 dev->printer_cdev_open = 0;
448 fd->private_data = NULL;
449 /* Change printer status to show that the printer is off-line. */
450 dev->printer_status &= ~PRINTER_SELECTED;
451 spin_unlock_irqrestore(&dev->lock, flags);
452
453 DBG(dev, "printer_close\n");
454
455 return 0;
456}
457
458/* This function must be called with interrupts turned off. */
459static void
460setup_rx_reqs(struct printer_dev *dev)
461{
462 struct usb_request *req;
463
464 while (likely(!list_empty(&dev->rx_reqs))) {
465 int error;
466
467 req = container_of(dev->rx_reqs.next,
468 struct usb_request, list);
469 list_del_init(&req->list);
470
471 /* The USB Host sends us whatever amount of data it wants to
472 * so we always set the length field to the full USB_BUFSIZE.
473 * If the amount of data is more than the read() caller asked
474 * for it will be stored in the request buffer until it is
475 * asked for by read().
476 */
477 req->length = USB_BUFSIZE;
478 req->complete = rx_complete;
479
480 /* here, we unlock, and only unlock, to avoid deadlock. */
481 spin_unlock(&dev->lock);
482 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
483 spin_lock(&dev->lock);
484 if (error) {
485 DBG(dev, "rx submit --> %d\n", error);
486 list_add(&req->list, &dev->rx_reqs);
487 break;
488 }
489 /* if the req is empty, then add it into dev->rx_reqs_active. */
490 else if (list_empty(&req->list)) {
491 list_add(&req->list, &dev->rx_reqs_active);
492 }
493 }
494}
495
496static ssize_t
497printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
498{
499 struct printer_dev *dev = fd->private_data;
500 unsigned long flags;
501 size_t size;
502 size_t bytes_copied;
503 struct usb_request *req;
504 /* This is a pointer to the current USB rx request. */
505 struct usb_request *current_rx_req;
506 /* This is the number of bytes in the current rx buffer. */
507 size_t current_rx_bytes;
508 /* This is a pointer to the current rx buffer. */
509 u8 *current_rx_buf;
510
511 if (len == 0)
512 return -EINVAL;
513
514 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
515
516 mutex_lock(&dev->lock_printer_io);
517 spin_lock_irqsave(&dev->lock, flags);
518
519 /* We will use this flag later to check if a printer reset happened
520 * after we turn interrupts back on.
521 */
522 dev->reset_printer = 0;
523
524 setup_rx_reqs(dev);
525
526 bytes_copied = 0;
527 current_rx_req = dev->current_rx_req;
528 current_rx_bytes = dev->current_rx_bytes;
529 current_rx_buf = dev->current_rx_buf;
530 dev->current_rx_req = NULL;
531 dev->current_rx_bytes = 0;
532 dev->current_rx_buf = NULL;
533
534 /* Check if there is any data in the read buffers. Please note that
535 * current_rx_bytes is the number of bytes in the current rx buffer.
536 * If it is zero then check if there are any other rx_buffers that
537 * are on the completed list. We are only out of data if all rx
538 * buffers are empty.
539 */
540 if ((current_rx_bytes == 0) &&
541 (likely(list_empty(&dev->rx_buffers)))) {
542 /* Turn interrupts back on before sleeping. */
543 spin_unlock_irqrestore(&dev->lock, flags);
544
545 /*
546 * If no data is available check if this is a NON-Blocking
547 * call or not.
548 */
549 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
550 mutex_unlock(&dev->lock_printer_io);
551 return -EAGAIN;
552 }
553
554 /* Sleep until data is available */
555 wait_event_interruptible(dev->rx_wait,
556 (likely(!list_empty(&dev->rx_buffers))));
557 spin_lock_irqsave(&dev->lock, flags);
558 }
559
560 /* We have data to return then copy it to the caller's buffer.*/
561 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
562 && len) {
563 if (current_rx_bytes == 0) {
564 req = container_of(dev->rx_buffers.next,
565 struct usb_request, list);
566 list_del_init(&req->list);
567
568 if (req->actual && req->buf) {
569 current_rx_req = req;
570 current_rx_bytes = req->actual;
571 current_rx_buf = req->buf;
572 } else {
573 list_add(&req->list, &dev->rx_reqs);
574 continue;
575 }
576 }
577
578 /* Don't leave irqs off while doing memory copies */
579 spin_unlock_irqrestore(&dev->lock, flags);
580
581 if (len > current_rx_bytes)
582 size = current_rx_bytes;
583 else
584 size = len;
585
586 size -= copy_to_user(buf, current_rx_buf, size);
587 bytes_copied += size;
588 len -= size;
589 buf += size;
590
591 spin_lock_irqsave(&dev->lock, flags);
592
593 /* We've disconnected or reset so return. */
594 if (dev->reset_printer) {
595 list_add(&current_rx_req->list, &dev->rx_reqs);
596 spin_unlock_irqrestore(&dev->lock, flags);
597 mutex_unlock(&dev->lock_printer_io);
598 return -EAGAIN;
599 }
600
601 /* If we not returning all the data left in this RX request
602 * buffer then adjust the amount of data left in the buffer.
603 * Othewise if we are done with this RX request buffer then
604 * requeue it to get any incoming data from the USB host.
605 */
606 if (size < current_rx_bytes) {
607 current_rx_bytes -= size;
608 current_rx_buf += size;
609 } else {
610 list_add(&current_rx_req->list, &dev->rx_reqs);
611 current_rx_bytes = 0;
612 current_rx_buf = NULL;
613 current_rx_req = NULL;
614 }
615 }
616
617 dev->current_rx_req = current_rx_req;
618 dev->current_rx_bytes = current_rx_bytes;
619 dev->current_rx_buf = current_rx_buf;
620
621 spin_unlock_irqrestore(&dev->lock, flags);
622 mutex_unlock(&dev->lock_printer_io);
623
624 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
625
626 if (bytes_copied)
627 return bytes_copied;
628 else
629 return -EAGAIN;
630}
631
632static ssize_t
633printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
634{
635 struct printer_dev *dev = fd->private_data;
636 unsigned long flags;
637 size_t size; /* Amount of data in a TX request. */
638 size_t bytes_copied = 0;
639 struct usb_request *req;
640
641 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
642
643 if (len == 0)
644 return -EINVAL;
645
646 mutex_lock(&dev->lock_printer_io);
647 spin_lock_irqsave(&dev->lock, flags);
648
649 /* Check if a printer reset happens while we have interrupts on */
650 dev->reset_printer = 0;
651
652 /* Check if there is any available write buffers */
653 if (likely(list_empty(&dev->tx_reqs))) {
654 /* Turn interrupts back on before sleeping. */
655 spin_unlock_irqrestore(&dev->lock, flags);
656
657 /*
658 * If write buffers are available check if this is
659 * a NON-Blocking call or not.
660 */
661 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
662 mutex_unlock(&dev->lock_printer_io);
663 return -EAGAIN;
664 }
665
666 /* Sleep until a write buffer is available */
667 wait_event_interruptible(dev->tx_wait,
668 (likely(!list_empty(&dev->tx_reqs))));
669 spin_lock_irqsave(&dev->lock, flags);
670 }
671
672 while (likely(!list_empty(&dev->tx_reqs)) && len) {
673
674 if (len > USB_BUFSIZE)
675 size = USB_BUFSIZE;
676 else
677 size = len;
678
679 req = container_of(dev->tx_reqs.next, struct usb_request,
680 list);
681 list_del_init(&req->list);
682
683 req->complete = tx_complete;
684 req->length = size;
685
686 /* Check if we need to send a zero length packet. */
687 if (len > size)
688 /* They will be more TX requests so no yet. */
689 req->zero = 0;
690 else
691 /* If the data amount is not a multple of the
692 * maxpacket size then send a zero length packet.
693 */
694 req->zero = ((len % dev->in_ep->maxpacket) == 0);
695
696 /* Don't leave irqs off while doing memory copies */
697 spin_unlock_irqrestore(&dev->lock, flags);
698
699 if (copy_from_user(req->buf, buf, size)) {
700 list_add(&req->list, &dev->tx_reqs);
701 mutex_unlock(&dev->lock_printer_io);
702 return bytes_copied;
703 }
704
705 bytes_copied += size;
706 len -= size;
707 buf += size;
708
709 spin_lock_irqsave(&dev->lock, flags);
710
711 /* We've disconnected or reset so free the req and buffer */
712 if (dev->reset_printer) {
713 list_add(&req->list, &dev->tx_reqs);
714 spin_unlock_irqrestore(&dev->lock, flags);
715 mutex_unlock(&dev->lock_printer_io);
716 return -EAGAIN;
717 }
718
719 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
720 list_add(&req->list, &dev->tx_reqs);
721 spin_unlock_irqrestore(&dev->lock, flags);
722 mutex_unlock(&dev->lock_printer_io);
723 return -EAGAIN;
724 }
725
726 list_add(&req->list, &dev->tx_reqs_active);
727
728 }
729
730 spin_unlock_irqrestore(&dev->lock, flags);
731 mutex_unlock(&dev->lock_printer_io);
732
733 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
734
735 if (bytes_copied) {
736 return bytes_copied;
737 } else {
738 return -EAGAIN;
739 }
740}
741
742static int
743printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
744{
745 struct printer_dev *dev = fd->private_data;
746 struct inode *inode = file_inode(fd);
747 unsigned long flags;
748 int tx_list_empty;
749
750 mutex_lock(&inode->i_mutex);
751 spin_lock_irqsave(&dev->lock, flags);
752 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
753 spin_unlock_irqrestore(&dev->lock, flags);
754
755 if (!tx_list_empty) {
756 /* Sleep until all data has been sent */
757 wait_event_interruptible(dev->tx_flush_wait,
758 (likely(list_empty(&dev->tx_reqs_active))));
759 }
760 mutex_unlock(&inode->i_mutex);
761
762 return 0;
763}
764
765static unsigned int
766printer_poll(struct file *fd, poll_table *wait)
767{
768 struct printer_dev *dev = fd->private_data;
769 unsigned long flags;
770 int status = 0;
771
772 mutex_lock(&dev->lock_printer_io);
773 spin_lock_irqsave(&dev->lock, flags);
774 setup_rx_reqs(dev);
775 spin_unlock_irqrestore(&dev->lock, flags);
776 mutex_unlock(&dev->lock_printer_io);
777
778 poll_wait(fd, &dev->rx_wait, wait);
779 poll_wait(fd, &dev->tx_wait, wait);
780
781 spin_lock_irqsave(&dev->lock, flags);
782 if (likely(!list_empty(&dev->tx_reqs)))
783 status |= POLLOUT | POLLWRNORM;
784
785 if (likely(dev->current_rx_bytes) ||
786 likely(!list_empty(&dev->rx_buffers)))
787 status |= POLLIN | POLLRDNORM;
788
789 spin_unlock_irqrestore(&dev->lock, flags);
790
791 return status;
792}
793
794static long
795printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
796{
797 struct printer_dev *dev = fd->private_data;
798 unsigned long flags;
799 int status = 0;
800
801 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
802
803 /* handle ioctls */
804
805 spin_lock_irqsave(&dev->lock, flags);
806
807 switch (code) {
808 case GADGET_GET_PRINTER_STATUS:
809 status = (int)dev->printer_status;
810 break;
811 case GADGET_SET_PRINTER_STATUS:
812 dev->printer_status = (u8)arg;
813 break;
814 default:
815 /* could not handle ioctl */
816 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
817 code);
818 status = -ENOTTY;
819 }
820
821 spin_unlock_irqrestore(&dev->lock, flags);
822
823 return status;
824}
825
826/* used after endpoint configuration */
827static const struct file_operations printer_io_operations = {
828 .owner = THIS_MODULE,
829 .open = printer_open,
830 .read = printer_read,
831 .write = printer_write,
832 .fsync = printer_fsync,
833 .poll = printer_poll,
834 .unlocked_ioctl = printer_ioctl,
835 .release = printer_close,
836 .llseek = noop_llseek,
837};
838
839/*-------------------------------------------------------------------------*/
840
841static int
842set_printer_interface(struct printer_dev *dev)
843{
844 int result = 0;
845
846 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
847 &ss_ep_in_desc);
848 dev->in_ep->driver_data = dev;
849
850 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
851 &hs_ep_out_desc, &ss_ep_out_desc);
852 dev->out_ep->driver_data = dev;
853
854 result = usb_ep_enable(dev->in_ep);
855 if (result != 0) {
856 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
857 goto done;
858 }
859
860 result = usb_ep_enable(dev->out_ep);
861 if (result != 0) {
862 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
863 goto done;
864 }
865
866done:
867 /* on error, disable any endpoints */
868 if (result != 0) {
869 (void) usb_ep_disable(dev->in_ep);
870 (void) usb_ep_disable(dev->out_ep);
871 dev->in_ep->desc = NULL;
872 dev->out_ep->desc = NULL;
873 }
874
875 /* caller is responsible for cleanup on error */
876 return result;
877}
878
879static void printer_reset_interface(struct printer_dev *dev)
880{
881 if (dev->interface < 0)
882 return;
883
884 DBG(dev, "%s\n", __func__);
885
886 if (dev->in_ep->desc)
887 usb_ep_disable(dev->in_ep);
888
889 if (dev->out_ep->desc)
890 usb_ep_disable(dev->out_ep);
891
892 dev->in_ep->desc = NULL;
893 dev->out_ep->desc = NULL;
894 dev->interface = -1;
895}
896
897/* Change our operational Interface. */
898static int set_interface(struct printer_dev *dev, unsigned number)
899{
900 int result = 0;
901
902 /* Free the current interface */
903 printer_reset_interface(dev);
904
905 result = set_printer_interface(dev);
906 if (result)
907 printer_reset_interface(dev);
908 else
909 dev->interface = number;
910
911 if (!result)
912 INFO(dev, "Using interface %x\n", number);
913
914 return result;
915}
916
917static void printer_soft_reset(struct printer_dev *dev)
918{
919 struct usb_request *req;
920
921 INFO(dev, "Received Printer Reset Request\n");
922
923 if (usb_ep_disable(dev->in_ep))
924 DBG(dev, "Failed to disable USB in_ep\n");
925 if (usb_ep_disable(dev->out_ep))
926 DBG(dev, "Failed to disable USB out_ep\n");
927
928 if (dev->current_rx_req != NULL) {
929 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
930 dev->current_rx_req = NULL;
931 }
932 dev->current_rx_bytes = 0;
933 dev->current_rx_buf = NULL;
934 dev->reset_printer = 1;
935
936 while (likely(!(list_empty(&dev->rx_buffers)))) {
937 req = container_of(dev->rx_buffers.next, struct usb_request,
938 list);
939 list_del_init(&req->list);
940 list_add(&req->list, &dev->rx_reqs);
941 }
942
943 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
944 req = container_of(dev->rx_buffers.next, struct usb_request,
945 list);
946 list_del_init(&req->list);
947 list_add(&req->list, &dev->rx_reqs);
948 }
949
950 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
951 req = container_of(dev->tx_reqs_active.next,
952 struct usb_request, list);
953 list_del_init(&req->list);
954 list_add(&req->list, &dev->tx_reqs);
955 }
956
957 if (usb_ep_enable(dev->in_ep))
958 DBG(dev, "Failed to enable USB in_ep\n");
959 if (usb_ep_enable(dev->out_ep))
960 DBG(dev, "Failed to enable USB out_ep\n");
961
962 wake_up_interruptible(&dev->rx_wait);
963 wake_up_interruptible(&dev->tx_wait);
964 wake_up_interruptible(&dev->tx_flush_wait);
965}
966
967/*-------------------------------------------------------------------------*/
968
969/*
970 * The setup() callback implements all the ep0 functionality that's not
971 * handled lower down.
972 */
973static int printer_func_setup(struct usb_function *f,
974 const struct usb_ctrlrequest *ctrl)
975{
976 struct printer_dev *dev = container_of(f, struct printer_dev, function);
977 struct usb_composite_dev *cdev = f->config->cdev;
978 struct usb_request *req = cdev->req;
979 int value = -EOPNOTSUPP;
980 u16 wIndex = le16_to_cpu(ctrl->wIndex);
981 u16 wValue = le16_to_cpu(ctrl->wValue);
982 u16 wLength = le16_to_cpu(ctrl->wLength);
983
984 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
985 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
986
987 switch (ctrl->bRequestType&USB_TYPE_MASK) {
988 case USB_TYPE_CLASS:
989 switch (ctrl->bRequest) {
990 case 0: /* Get the IEEE-1284 PNP String */
991 /* Only one printer interface is supported. */
992 if ((wIndex>>8) != dev->interface)
993 break;
994
995 value = (pnp_string[0]<<8)|pnp_string[1];
996 memcpy(req->buf, pnp_string, value);
997 DBG(dev, "1284 PNP String: %x %s\n", value,
998 &pnp_string[2]);
999 break;
1000
1001 case 1: /* Get Port Status */
1002 /* Only one printer interface is supported. */
1003 if (wIndex != dev->interface)
1004 break;
1005
1006 *(u8 *)req->buf = dev->printer_status;
1007 value = min(wLength, (u16) 1);
1008 break;
1009
1010 case 2: /* Soft Reset */
1011 /* Only one printer interface is supported. */
1012 if (wIndex != dev->interface)
1013 break;
1014
1015 printer_soft_reset(dev);
1016
1017 value = 0;
1018 break;
1019
1020 default:
1021 goto unknown;
1022 }
1023 break;
1024
1025 default:
1026unknown:
1027 VDBG(dev,
1028 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1029 ctrl->bRequestType, ctrl->bRequest,
1030 wValue, wIndex, wLength);
1031 break;
1032 }
1033 /* host either stalls (value < 0) or reports success */
1034 return value;
1035}
1036
1037static int __init printer_func_bind(struct usb_configuration *c,
1038 struct usb_function *f)
1039{
1040 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1041 struct usb_composite_dev *cdev = c->cdev;
1042 struct usb_ep *in_ep;
1043 struct usb_ep *out_ep = NULL;
1044 int id;
1045 int ret;
1046
1047 id = usb_interface_id(c, f);
1048 if (id < 0)
1049 return id;
1050 intf_desc.bInterfaceNumber = id;
1051
1052 /* all we really need is bulk IN/OUT */
1053 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1054 if (!in_ep) {
1055autoconf_fail:
1056 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1057 cdev->gadget->name);
1058 return -ENODEV;
1059 }
1060 in_ep->driver_data = in_ep; /* claim */
1061
1062 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1063 if (!out_ep)
1064 goto autoconf_fail;
1065 out_ep->driver_data = out_ep; /* claim */
1066
1067 /* assumes that all endpoints are dual-speed */
1068 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1069 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1070 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1071 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1072
1073 ret = usb_assign_descriptors(f, fs_printer_function,
1074 hs_printer_function, ss_printer_function);
1075 if (ret)
1076 return ret;
1077
1078 dev->in_ep = in_ep;
1079 dev->out_ep = out_ep;
1080 return 0;
1081}
1082
1083static void printer_func_unbind(struct usb_configuration *c,
1084 struct usb_function *f)
1085{
1086 usb_free_all_descriptors(f);
1087}
1088
1089static int printer_func_set_alt(struct usb_function *f,
1090 unsigned intf, unsigned alt)
1091{
1092 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1093 int ret = -ENOTSUPP;
1094
1095 if (!alt)
1096 ret = set_interface(dev, intf);
1097
1098 return ret;
1099}
1100
1101static void printer_func_disable(struct usb_function *f)
1102{
1103 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1104 unsigned long flags;
1105
1106 DBG(dev, "%s\n", __func__);
1107
1108 spin_lock_irqsave(&dev->lock, flags);
1109 printer_reset_interface(dev);
1110 spin_unlock_irqrestore(&dev->lock, flags);
1111}
1112
1113static void printer_cfg_unbind(struct usb_configuration *c)
1114{
1115 struct printer_dev *dev;
1116 struct usb_request *req;
1117
1118 dev = &usb_printer_gadget;
1119
1120 DBG(dev, "%s\n", __func__);
1121
1122 /* Remove sysfs files */
1123 device_destroy(usb_gadget_class, g_printer_devno);
1124
1125 /* Remove Character Device */
1126 cdev_del(&dev->printer_cdev);
1127
1128 /* we must already have been disconnected ... no i/o may be active */
1129 WARN_ON(!list_empty(&dev->tx_reqs_active));
1130 WARN_ON(!list_empty(&dev->rx_reqs_active));
1131
1132 /* Free all memory for this driver. */
1133 while (!list_empty(&dev->tx_reqs)) {
1134 req = container_of(dev->tx_reqs.next, struct usb_request,
1135 list);
1136 list_del(&req->list);
1137 printer_req_free(dev->in_ep, req);
1138 }
1139
1140 if (dev->current_rx_req != NULL)
1141 printer_req_free(dev->out_ep, dev->current_rx_req);
1142
1143 while (!list_empty(&dev->rx_reqs)) {
1144 req = container_of(dev->rx_reqs.next,
1145 struct usb_request, list);
1146 list_del(&req->list);
1147 printer_req_free(dev->out_ep, req);
1148 }
1149
1150 while (!list_empty(&dev->rx_buffers)) {
1151 req = container_of(dev->rx_buffers.next,
1152 struct usb_request, list);
1153 list_del(&req->list);
1154 printer_req_free(dev->out_ep, req);
1155 }
1156}
1157
1158static struct usb_configuration printer_cfg_driver = { 123static struct usb_configuration printer_cfg_driver = {
1159 .label = "printer", 124 .label = "printer",
1160 .unbind = printer_cfg_unbind,
1161 .bConfigurationValue = 1, 125 .bConfigurationValue = 1,
1162 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 126 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1163}; 127};
1164 128
1165static int __init printer_bind_config(struct usb_configuration *c) 129static int __init printer_do_config(struct usb_configuration *c)
1166{ 130{
1167 struct usb_gadget *gadget = c->cdev->gadget; 131 struct usb_gadget *gadget = c->cdev->gadget;
1168 struct printer_dev *dev; 132 int status = 0;
1169 int status = -ENOMEM;
1170 size_t len;
1171 u32 i;
1172 struct usb_request *req;
1173 133
1174 usb_ep_autoconfig_reset(gadget); 134 usb_ep_autoconfig_reset(gadget);
1175 135
1176 dev = &usb_printer_gadget;
1177
1178 dev->function.name = shortname;
1179 dev->function.bind = printer_func_bind;
1180 dev->function.setup = printer_func_setup;
1181 dev->function.unbind = printer_func_unbind;
1182 dev->function.set_alt = printer_func_set_alt;
1183 dev->function.disable = printer_func_disable;
1184
1185 status = usb_add_function(c, &dev->function);
1186 if (status)
1187 return status;
1188
1189 /* Setup the sysfs files for the printer gadget. */
1190 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1191 NULL, "g_printer");
1192 if (IS_ERR(dev->pdev)) {
1193 ERROR(dev, "Failed to create device: g_printer\n");
1194 status = PTR_ERR(dev->pdev);
1195 goto fail;
1196 }
1197
1198 /*
1199 * Register a character device as an interface to a user mode
1200 * program that handles the printer specific functionality.
1201 */
1202 cdev_init(&dev->printer_cdev, &printer_io_operations);
1203 dev->printer_cdev.owner = THIS_MODULE;
1204 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1205 if (status) {
1206 ERROR(dev, "Failed to open char device\n");
1207 goto fail;
1208 }
1209
1210 if (iPNPstring)
1211 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1212
1213 len = strlen(pnp_string);
1214 pnp_string[0] = (len >> 8) & 0xFF;
1215 pnp_string[1] = len & 0xFF;
1216
1217 usb_gadget_set_selfpowered(gadget); 136 usb_gadget_set_selfpowered(gadget);
1218 137
1219 if (gadget_is_otg(gadget)) { 138 if (gadget_is_otg(gadget)) {
@@ -1222,86 +141,64 @@ static int __init printer_bind_config(struct usb_configuration *c)
1222 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 141 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1223 } 142 }
1224 143
1225 spin_lock_init(&dev->lock); 144 f_printer = usb_get_function(fi_printer);
1226 mutex_init(&dev->lock_printer_io); 145 if (IS_ERR(f_printer))
1227 INIT_LIST_HEAD(&dev->tx_reqs); 146 return PTR_ERR(f_printer);
1228 INIT_LIST_HEAD(&dev->tx_reqs_active);
1229 INIT_LIST_HEAD(&dev->rx_reqs);
1230 INIT_LIST_HEAD(&dev->rx_reqs_active);
1231 INIT_LIST_HEAD(&dev->rx_buffers);
1232 init_waitqueue_head(&dev->rx_wait);
1233 init_waitqueue_head(&dev->tx_wait);
1234 init_waitqueue_head(&dev->tx_flush_wait);
1235
1236 dev->interface = -1;
1237 dev->printer_cdev_open = 0;
1238 dev->printer_status = PRINTER_NOT_ERROR;
1239 dev->current_rx_req = NULL;
1240 dev->current_rx_bytes = 0;
1241 dev->current_rx_buf = NULL;
1242
1243 for (i = 0; i < QLEN; i++) {
1244 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1245 if (!req) {
1246 while (!list_empty(&dev->tx_reqs)) {
1247 req = container_of(dev->tx_reqs.next,
1248 struct usb_request, list);
1249 list_del(&req->list);
1250 printer_req_free(dev->in_ep, req);
1251 }
1252 return -ENOMEM;
1253 }
1254 list_add(&req->list, &dev->tx_reqs);
1255 }
1256
1257 for (i = 0; i < QLEN; i++) {
1258 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1259 if (!req) {
1260 while (!list_empty(&dev->rx_reqs)) {
1261 req = container_of(dev->rx_reqs.next,
1262 struct usb_request, list);
1263 list_del(&req->list);
1264 printer_req_free(dev->out_ep, req);
1265 }
1266 return -ENOMEM;
1267 }
1268 list_add(&req->list, &dev->rx_reqs);
1269 }
1270
1271 /* finish hookup to lower layer ... */
1272 dev->gadget = gadget;
1273 147
1274 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); 148 status = usb_add_function(c, f_printer);
1275 return 0; 149 if (status < 0)
150 usb_put_function(f_printer);
1276 151
1277fail:
1278 printer_cfg_unbind(c);
1279 return status; 152 return status;
1280} 153}
1281 154
1282static int printer_unbind(struct usb_composite_dev *cdev)
1283{
1284 return 0;
1285}
1286
1287static int __init printer_bind(struct usb_composite_dev *cdev) 155static int __init printer_bind(struct usb_composite_dev *cdev)
1288{ 156{
1289 int ret; 157 struct f_printer_opts *opts;
158 int ret, len;
159
160 fi_printer = usb_get_function_instance("printer");
161 if (IS_ERR(fi_printer))
162 return PTR_ERR(fi_printer);
163
164 if (iPNPstring)
165 strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2);
166
167 len = strlen(pnp_string);
168 pnp_string[0] = (len >> 8) & 0xFF;
169 pnp_string[1] = len & 0xFF;
170
171 opts = container_of(fi_printer, struct f_printer_opts, func_inst);
172 opts->minor = 0;
173 memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN);
174 opts->q_len = QLEN;
1290 175
1291 ret = usb_string_ids_tab(cdev, strings); 176 ret = usb_string_ids_tab(cdev, strings);
1292 if (ret < 0) 177 if (ret < 0) {
178 usb_put_function_instance(fi_printer);
1293 return ret; 179 return ret;
180 }
1294 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id; 181 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
1295 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id; 182 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
1296 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id; 183 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
1297 184
1298 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config); 185 ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
1299 if (ret) 186 if (ret) {
187 usb_put_function_instance(fi_printer);
1300 return ret; 188 return ret;
189 }
1301 usb_composite_overwrite_options(cdev, &coverwrite); 190 usb_composite_overwrite_options(cdev, &coverwrite);
1302 return ret; 191 return ret;
1303} 192}
1304 193
194static int __exit printer_unbind(struct usb_composite_dev *cdev)
195{
196 usb_put_function(f_printer);
197 usb_put_function_instance(fi_printer);
198
199 return 0;
200}
201
1305static __refdata struct usb_composite_driver printer_driver = { 202static __refdata struct usb_composite_driver printer_driver = {
1306 .name = shortname, 203 .name = shortname,
1307 .dev = &device_desc, 204 .dev = &device_desc,
@@ -1311,47 +208,7 @@ static __refdata struct usb_composite_driver printer_driver = {
1311 .unbind = printer_unbind, 208 .unbind = printer_unbind,
1312}; 209};
1313 210
1314static int __init 211module_usb_composite_driver(printer_driver);
1315init(void)
1316{
1317 int status;
1318
1319 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1320 if (IS_ERR(usb_gadget_class)) {
1321 status = PTR_ERR(usb_gadget_class);
1322 pr_err("unable to create usb_gadget class %d\n", status);
1323 return status;
1324 }
1325
1326 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1327 "USB printer gadget");
1328 if (status) {
1329 pr_err("alloc_chrdev_region %d\n", status);
1330 class_destroy(usb_gadget_class);
1331 return status;
1332 }
1333
1334 status = usb_composite_probe(&printer_driver);
1335 if (status) {
1336 class_destroy(usb_gadget_class);
1337 unregister_chrdev_region(g_printer_devno, 1);
1338 pr_err("usb_gadget_probe_driver %x\n", status);
1339 }
1340
1341 return status;
1342}
1343module_init(init);
1344
1345static void __exit
1346cleanup(void)
1347{
1348 mutex_lock(&usb_printer_gadget.lock_printer_io);
1349 usb_composite_unregister(&printer_driver);
1350 unregister_chrdev_region(g_printer_devno, 1);
1351 class_destroy(usb_gadget_class);
1352 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1353}
1354module_exit(cleanup);
1355 212
1356MODULE_DESCRIPTION(DRIVER_DESC); 213MODULE_DESCRIPTION(DRIVER_DESC);
1357MODULE_AUTHOR("Craig Nadler"); 214MODULE_AUTHOR("Craig Nadler");
diff --git a/drivers/usb/gadget/legacy/tcm_usb_gadget.c b/drivers/usb/gadget/legacy/tcm_usb_gadget.c
index 3a494168661e..6e0a019aad54 100644
--- a/drivers/usb/gadget/legacy/tcm_usb_gadget.c
+++ b/drivers/usb/gadget/legacy/tcm_usb_gadget.c
@@ -1740,10 +1740,9 @@ static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1740 goto err_session; 1740 goto err_session;
1741 } 1741 }
1742 /* 1742 /*
1743 * Now register the TCM vHost virtual I_T Nexus as active with the 1743 * Now register the TCM vHost virtual I_T Nexus as active.
1744 * call to __transport_register_session()
1745 */ 1744 */
1746 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, 1745 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1747 tv_nexus->tvn_se_sess, tv_nexus); 1746 tv_nexus->tvn_se_sess, tv_nexus);
1748 tpg->tpg_nexus = tv_nexus; 1747 tpg->tpg_nexus = tv_nexus;
1749 mutex_unlock(&tpg->tpg_mutex); 1748 mutex_unlock(&tpg->tpg_mutex);
diff --git a/drivers/usb/gadget/legacy/zero.c b/drivers/usb/gadget/legacy/zero.c
index ff97ac93ac03..5ee95152493c 100644
--- a/drivers/usb/gadget/legacy/zero.c
+++ b/drivers/usb/gadget/legacy/zero.c
@@ -68,8 +68,6 @@ static struct usb_zero_options gzero_options = {
68 .isoc_maxpacket = GZERO_ISOC_MAXPACKET, 68 .isoc_maxpacket = GZERO_ISOC_MAXPACKET,
69 .bulk_buflen = GZERO_BULK_BUFLEN, 69 .bulk_buflen = GZERO_BULK_BUFLEN,
70 .qlen = GZERO_QLEN, 70 .qlen = GZERO_QLEN,
71 .int_interval = GZERO_INT_INTERVAL,
72 .int_maxpacket = GZERO_INT_MAXPACKET,
73}; 71};
74 72
75/*-------------------------------------------------------------------------*/ 73/*-------------------------------------------------------------------------*/
@@ -268,21 +266,6 @@ module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
268 S_IRUGO|S_IWUSR); 266 S_IRUGO|S_IWUSR);
269MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)"); 267MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
270 268
271module_param_named(int_interval, gzero_options.int_interval, uint,
272 S_IRUGO|S_IWUSR);
273MODULE_PARM_DESC(int_interval, "1 - 16");
274
275module_param_named(int_maxpacket, gzero_options.int_maxpacket, uint,
276 S_IRUGO|S_IWUSR);
277MODULE_PARM_DESC(int_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
278
279module_param_named(int_mult, gzero_options.int_mult, uint, S_IRUGO|S_IWUSR);
280MODULE_PARM_DESC(int_mult, "0 - 2 (hs/ss only)");
281
282module_param_named(int_maxburst, gzero_options.int_maxburst, uint,
283 S_IRUGO|S_IWUSR);
284MODULE_PARM_DESC(int_maxburst, "0 - 15 (ss only)");
285
286static struct usb_function *func_lb; 269static struct usb_function *func_lb;
287static struct usb_function_instance *func_inst_lb; 270static struct usb_function_instance *func_inst_lb;
288 271
@@ -318,10 +301,6 @@ static int __init zero_bind(struct usb_composite_dev *cdev)
318 ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket; 301 ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
319 ss_opts->isoc_mult = gzero_options.isoc_mult; 302 ss_opts->isoc_mult = gzero_options.isoc_mult;
320 ss_opts->isoc_maxburst = gzero_options.isoc_maxburst; 303 ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
321 ss_opts->int_interval = gzero_options.int_interval;
322 ss_opts->int_maxpacket = gzero_options.int_maxpacket;
323 ss_opts->int_mult = gzero_options.int_mult;
324 ss_opts->int_maxburst = gzero_options.int_maxburst;
325 ss_opts->bulk_buflen = gzero_options.bulk_buflen; 304 ss_opts->bulk_buflen = gzero_options.bulk_buflen;
326 305
327 func_ss = usb_get_function(func_inst_ss); 306 func_ss = usb_get_function(func_inst_ss);
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
index d79cb35dbf8a..4c01953a0869 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.c
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
@@ -152,7 +152,7 @@ static int regs_dbg_open(struct inode *inode, struct file *file)
152 152
153 spin_lock_irq(&udc->lock); 153 spin_lock_irq(&udc->lock);
154 for (i = 0; i < inode->i_size / 4; i++) 154 for (i = 0; i < inode->i_size / 4; i++)
155 data[i] = __raw_readl(udc->regs + i * 4); 155 data[i] = usba_io_readl(udc->regs + i * 4);
156 spin_unlock_irq(&udc->lock); 156 spin_unlock_irq(&udc->lock);
157 157
158 file->private_data = data; 158 file->private_data = data;
@@ -1249,7 +1249,7 @@ static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
1249 if (crq->wLength != cpu_to_le16(sizeof(status))) 1249 if (crq->wLength != cpu_to_le16(sizeof(status)))
1250 goto stall; 1250 goto stall;
1251 ep->state = DATA_STAGE_IN; 1251 ep->state = DATA_STAGE_IN;
1252 __raw_writew(status, ep->fifo); 1252 usba_io_writew(status, ep->fifo);
1253 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 1253 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
1254 break; 1254 break;
1255 } 1255 }
@@ -1739,7 +1739,72 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1739 return IRQ_HANDLED; 1739 return IRQ_HANDLED;
1740} 1740}
1741 1741
1742static irqreturn_t usba_vbus_irq(int irq, void *devid) 1742static int start_clock(struct usba_udc *udc)
1743{
1744 int ret;
1745
1746 if (udc->clocked)
1747 return 0;
1748
1749 ret = clk_prepare_enable(udc->pclk);
1750 if (ret)
1751 return ret;
1752 ret = clk_prepare_enable(udc->hclk);
1753 if (ret) {
1754 clk_disable_unprepare(udc->pclk);
1755 return ret;
1756 }
1757
1758 udc->clocked = true;
1759 return 0;
1760}
1761
1762static void stop_clock(struct usba_udc *udc)
1763{
1764 if (!udc->clocked)
1765 return;
1766
1767 clk_disable_unprepare(udc->hclk);
1768 clk_disable_unprepare(udc->pclk);
1769
1770 udc->clocked = false;
1771}
1772
1773static int usba_start(struct usba_udc *udc)
1774{
1775 unsigned long flags;
1776 int ret;
1777
1778 ret = start_clock(udc);
1779 if (ret)
1780 return ret;
1781
1782 spin_lock_irqsave(&udc->lock, flags);
1783 toggle_bias(udc, 1);
1784 usba_writel(udc, CTRL, USBA_ENABLE_MASK);
1785 usba_int_enb_set(udc, USBA_END_OF_RESET);
1786 spin_unlock_irqrestore(&udc->lock, flags);
1787
1788 return 0;
1789}
1790
1791static void usba_stop(struct usba_udc *udc)
1792{
1793 unsigned long flags;
1794
1795 spin_lock_irqsave(&udc->lock, flags);
1796 udc->gadget.speed = USB_SPEED_UNKNOWN;
1797 reset_all_endpoints(udc);
1798
1799 /* This will also disable the DP pullup */
1800 toggle_bias(udc, 0);
1801 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1802 spin_unlock_irqrestore(&udc->lock, flags);
1803
1804 stop_clock(udc);
1805}
1806
1807static irqreturn_t usba_vbus_irq_thread(int irq, void *devid)
1743{ 1808{
1744 struct usba_udc *udc = devid; 1809 struct usba_udc *udc = devid;
1745 int vbus; 1810 int vbus;
@@ -1747,35 +1812,22 @@ static irqreturn_t usba_vbus_irq(int irq, void *devid)
1747 /* debounce */ 1812 /* debounce */
1748 udelay(10); 1813 udelay(10);
1749 1814
1750 spin_lock(&udc->lock); 1815 mutex_lock(&udc->vbus_mutex);
1751
1752 /* May happen if Vbus pin toggles during probe() */
1753 if (!udc->driver)
1754 goto out;
1755 1816
1756 vbus = vbus_is_present(udc); 1817 vbus = vbus_is_present(udc);
1757 if (vbus != udc->vbus_prev) { 1818 if (vbus != udc->vbus_prev) {
1758 if (vbus) { 1819 if (vbus) {
1759 toggle_bias(udc, 1); 1820 usba_start(udc);
1760 usba_writel(udc, CTRL, USBA_ENABLE_MASK);
1761 usba_int_enb_set(udc, USBA_END_OF_RESET);
1762 } else { 1821 } else {
1763 udc->gadget.speed = USB_SPEED_UNKNOWN; 1822 usba_stop(udc);
1764 reset_all_endpoints(udc); 1823
1765 toggle_bias(udc, 0); 1824 if (udc->driver->disconnect)
1766 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1767 if (udc->driver->disconnect) {
1768 spin_unlock(&udc->lock);
1769 udc->driver->disconnect(&udc->gadget); 1825 udc->driver->disconnect(&udc->gadget);
1770 spin_lock(&udc->lock);
1771 }
1772 } 1826 }
1773 udc->vbus_prev = vbus; 1827 udc->vbus_prev = vbus;
1774 } 1828 }
1775 1829
1776out: 1830 mutex_unlock(&udc->vbus_mutex);
1777 spin_unlock(&udc->lock);
1778
1779 return IRQ_HANDLED; 1831 return IRQ_HANDLED;
1780} 1832}
1781 1833
@@ -1787,55 +1839,47 @@ static int atmel_usba_start(struct usb_gadget *gadget,
1787 unsigned long flags; 1839 unsigned long flags;
1788 1840
1789 spin_lock_irqsave(&udc->lock, flags); 1841 spin_lock_irqsave(&udc->lock, flags);
1790
1791 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED; 1842 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
1792 udc->driver = driver; 1843 udc->driver = driver;
1793 spin_unlock_irqrestore(&udc->lock, flags); 1844 spin_unlock_irqrestore(&udc->lock, flags);
1794 1845
1795 ret = clk_prepare_enable(udc->pclk); 1846 mutex_lock(&udc->vbus_mutex);
1796 if (ret)
1797 return ret;
1798 ret = clk_prepare_enable(udc->hclk);
1799 if (ret) {
1800 clk_disable_unprepare(udc->pclk);
1801 return ret;
1802 }
1803 1847
1804 udc->vbus_prev = 0;
1805 if (gpio_is_valid(udc->vbus_pin)) 1848 if (gpio_is_valid(udc->vbus_pin))
1806 enable_irq(gpio_to_irq(udc->vbus_pin)); 1849 enable_irq(gpio_to_irq(udc->vbus_pin));
1807 1850
1808 /* If Vbus is present, enable the controller and wait for reset */ 1851 /* If Vbus is present, enable the controller and wait for reset */
1809 spin_lock_irqsave(&udc->lock, flags); 1852 udc->vbus_prev = vbus_is_present(udc);
1810 if (vbus_is_present(udc) && udc->vbus_prev == 0) { 1853 if (udc->vbus_prev) {
1811 toggle_bias(udc, 1); 1854 ret = usba_start(udc);
1812 usba_writel(udc, CTRL, USBA_ENABLE_MASK); 1855 if (ret)
1813 usba_int_enb_set(udc, USBA_END_OF_RESET); 1856 goto err;
1814 } 1857 }
1815 spin_unlock_irqrestore(&udc->lock, flags);
1816 1858
1859 mutex_unlock(&udc->vbus_mutex);
1817 return 0; 1860 return 0;
1861
1862err:
1863 if (gpio_is_valid(udc->vbus_pin))
1864 disable_irq(gpio_to_irq(udc->vbus_pin));
1865
1866 mutex_unlock(&udc->vbus_mutex);
1867
1868 spin_lock_irqsave(&udc->lock, flags);
1869 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1870 udc->driver = NULL;
1871 spin_unlock_irqrestore(&udc->lock, flags);
1872 return ret;
1818} 1873}
1819 1874
1820static int atmel_usba_stop(struct usb_gadget *gadget) 1875static int atmel_usba_stop(struct usb_gadget *gadget)
1821{ 1876{
1822 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); 1877 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget);
1823 unsigned long flags;
1824 1878
1825 if (gpio_is_valid(udc->vbus_pin)) 1879 if (gpio_is_valid(udc->vbus_pin))
1826 disable_irq(gpio_to_irq(udc->vbus_pin)); 1880 disable_irq(gpio_to_irq(udc->vbus_pin));
1827 1881
1828 spin_lock_irqsave(&udc->lock, flags); 1882 usba_stop(udc);
1829 udc->gadget.speed = USB_SPEED_UNKNOWN;
1830 reset_all_endpoints(udc);
1831 spin_unlock_irqrestore(&udc->lock, flags);
1832
1833 /* This will also disable the DP pullup */
1834 toggle_bias(udc, 0);
1835 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1836
1837 clk_disable_unprepare(udc->hclk);
1838 clk_disable_unprepare(udc->pclk);
1839 1883
1840 udc->driver = NULL; 1884 udc->driver = NULL;
1841 1885
@@ -2057,6 +2101,7 @@ static int usba_udc_probe(struct platform_device *pdev)
2057 return PTR_ERR(hclk); 2101 return PTR_ERR(hclk);
2058 2102
2059 spin_lock_init(&udc->lock); 2103 spin_lock_init(&udc->lock);
2104 mutex_init(&udc->vbus_mutex);
2060 udc->pdev = pdev; 2105 udc->pdev = pdev;
2061 udc->pclk = pclk; 2106 udc->pclk = pclk;
2062 udc->hclk = hclk; 2107 udc->hclk = hclk;
@@ -2111,17 +2156,17 @@ static int usba_udc_probe(struct platform_device *pdev)
2111 2156
2112 if (gpio_is_valid(udc->vbus_pin)) { 2157 if (gpio_is_valid(udc->vbus_pin)) {
2113 if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) { 2158 if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) {
2114 ret = devm_request_irq(&pdev->dev, 2159 irq_set_status_flags(gpio_to_irq(udc->vbus_pin),
2115 gpio_to_irq(udc->vbus_pin), 2160 IRQ_NOAUTOEN);
2116 usba_vbus_irq, 0, 2161 ret = devm_request_threaded_irq(&pdev->dev,
2162 gpio_to_irq(udc->vbus_pin), NULL,
2163 usba_vbus_irq_thread, IRQF_ONESHOT,
2117 "atmel_usba_udc", udc); 2164 "atmel_usba_udc", udc);
2118 if (ret) { 2165 if (ret) {
2119 udc->vbus_pin = -ENODEV; 2166 udc->vbus_pin = -ENODEV;
2120 dev_warn(&udc->pdev->dev, 2167 dev_warn(&udc->pdev->dev,
2121 "failed to request vbus irq; " 2168 "failed to request vbus irq; "
2122 "assuming always on\n"); 2169 "assuming always on\n");
2123 } else {
2124 disable_irq(gpio_to_irq(udc->vbus_pin));
2125 } 2170 }
2126 } else { 2171 } else {
2127 /* gpio_request fail so use -EINVAL for gpio_is_valid */ 2172 /* gpio_request fail so use -EINVAL for gpio_is_valid */
@@ -2132,6 +2177,7 @@ static int usba_udc_probe(struct platform_device *pdev)
2132 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2177 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2133 if (ret) 2178 if (ret)
2134 return ret; 2179 return ret;
2180 device_init_wakeup(&pdev->dev, 1);
2135 2181
2136 usba_init_debugfs(udc); 2182 usba_init_debugfs(udc);
2137 for (i = 1; i < udc->num_ep; i++) 2183 for (i = 1; i < udc->num_ep; i++)
@@ -2147,6 +2193,7 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2147 2193
2148 udc = platform_get_drvdata(pdev); 2194 udc = platform_get_drvdata(pdev);
2149 2195
2196 device_init_wakeup(&pdev->dev, 0);
2150 usb_del_gadget_udc(&udc->gadget); 2197 usb_del_gadget_udc(&udc->gadget);
2151 2198
2152 for (i = 1; i < udc->num_ep; i++) 2199 for (i = 1; i < udc->num_ep; i++)
@@ -2156,10 +2203,65 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2156 return 0; 2203 return 0;
2157} 2204}
2158 2205
2206#ifdef CONFIG_PM
2207static int usba_udc_suspend(struct device *dev)
2208{
2209 struct usba_udc *udc = dev_get_drvdata(dev);
2210
2211 /* Not started */
2212 if (!udc->driver)
2213 return 0;
2214
2215 mutex_lock(&udc->vbus_mutex);
2216
2217 if (!device_may_wakeup(dev)) {
2218 usba_stop(udc);
2219 goto out;
2220 }
2221
2222 /*
2223 * Device may wake up. We stay clocked if we failed
2224 * to request vbus irq, assuming always on.
2225 */
2226 if (gpio_is_valid(udc->vbus_pin)) {
2227 usba_stop(udc);
2228 enable_irq_wake(gpio_to_irq(udc->vbus_pin));
2229 }
2230
2231out:
2232 mutex_unlock(&udc->vbus_mutex);
2233 return 0;
2234}
2235
2236static int usba_udc_resume(struct device *dev)
2237{
2238 struct usba_udc *udc = dev_get_drvdata(dev);
2239
2240 /* Not started */
2241 if (!udc->driver)
2242 return 0;
2243
2244 if (device_may_wakeup(dev) && gpio_is_valid(udc->vbus_pin))
2245 disable_irq_wake(gpio_to_irq(udc->vbus_pin));
2246
2247 /* If Vbus is present, enable the controller and wait for reset */
2248 mutex_lock(&udc->vbus_mutex);
2249 udc->vbus_prev = vbus_is_present(udc);
2250 if (udc->vbus_prev)
2251 usba_start(udc);
2252 mutex_unlock(&udc->vbus_mutex);
2253
2254 return 0;
2255}
2256#endif
2257
2258static SIMPLE_DEV_PM_OPS(usba_udc_pm_ops, usba_udc_suspend, usba_udc_resume);
2259
2159static struct platform_driver udc_driver = { 2260static struct platform_driver udc_driver = {
2160 .remove = __exit_p(usba_udc_remove), 2261 .remove = __exit_p(usba_udc_remove),
2161 .driver = { 2262 .driver = {
2162 .name = "atmel_usba_udc", 2263 .name = "atmel_usba_udc",
2264 .pm = &usba_udc_pm_ops,
2163 .of_match_table = of_match_ptr(atmel_udc_dt_ids), 2265 .of_match_table = of_match_ptr(atmel_udc_dt_ids),
2164 }, 2266 },
2165}; 2267};
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
index 497cd18836f3..ea448a344767 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.h
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
@@ -191,18 +191,28 @@
191 | USBA_BF(name, value)) 191 | USBA_BF(name, value))
192 192
193/* Register access macros */ 193/* Register access macros */
194#ifdef CONFIG_AVR32
195#define usba_io_readl __raw_readl
196#define usba_io_writel __raw_writel
197#define usba_io_writew __raw_writew
198#else
199#define usba_io_readl readl_relaxed
200#define usba_io_writel writel_relaxed
201#define usba_io_writew writew_relaxed
202#endif
203
194#define usba_readl(udc, reg) \ 204#define usba_readl(udc, reg) \
195 __raw_readl((udc)->regs + USBA_##reg) 205 usba_io_readl((udc)->regs + USBA_##reg)
196#define usba_writel(udc, reg, value) \ 206#define usba_writel(udc, reg, value) \
197 __raw_writel((value), (udc)->regs + USBA_##reg) 207 usba_io_writel((value), (udc)->regs + USBA_##reg)
198#define usba_ep_readl(ep, reg) \ 208#define usba_ep_readl(ep, reg) \
199 __raw_readl((ep)->ep_regs + USBA_EPT_##reg) 209 usba_io_readl((ep)->ep_regs + USBA_EPT_##reg)
200#define usba_ep_writel(ep, reg, value) \ 210#define usba_ep_writel(ep, reg, value) \
201 __raw_writel((value), (ep)->ep_regs + USBA_EPT_##reg) 211 usba_io_writel((value), (ep)->ep_regs + USBA_EPT_##reg)
202#define usba_dma_readl(ep, reg) \ 212#define usba_dma_readl(ep, reg) \
203 __raw_readl((ep)->dma_regs + USBA_DMA_##reg) 213 usba_io_readl((ep)->dma_regs + USBA_DMA_##reg)
204#define usba_dma_writel(ep, reg, value) \ 214#define usba_dma_writel(ep, reg, value) \
205 __raw_writel((value), (ep)->dma_regs + USBA_DMA_##reg) 215 usba_io_writel((value), (ep)->dma_regs + USBA_DMA_##reg)
206 216
207/* Calculate base address for a given endpoint or DMA controller */ 217/* Calculate base address for a given endpoint or DMA controller */
208#define USBA_EPT_BASE(x) (0x100 + (x) * 0x20) 218#define USBA_EPT_BASE(x) (0x100 + (x) * 0x20)
@@ -313,6 +323,9 @@ struct usba_udc {
313 /* Protect hw registers from concurrent modifications */ 323 /* Protect hw registers from concurrent modifications */
314 spinlock_t lock; 324 spinlock_t lock;
315 325
326 /* Mutex to prevent concurrent start or stop */
327 struct mutex vbus_mutex;
328
316 void __iomem *regs; 329 void __iomem *regs;
317 void __iomem *fifo; 330 void __iomem *fifo;
318 331
@@ -328,6 +341,7 @@ struct usba_udc {
328 struct clk *hclk; 341 struct clk *hclk;
329 struct usba_ep *usba_ep; 342 struct usba_ep *usba_ep;
330 bool bias_pulse_needed; 343 bool bias_pulse_needed;
344 bool clocked;
331 345
332 u16 devstatus; 346 u16 devstatus;
333 347
diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c
index 8dda48445f6f..181112c88f43 100644
--- a/drivers/usb/gadget/udc/dummy_hcd.c
+++ b/drivers/usb/gadget/udc/dummy_hcd.c
@@ -1923,7 +1923,7 @@ static inline void
1923ss_hub_descriptor(struct usb_hub_descriptor *desc) 1923ss_hub_descriptor(struct usb_hub_descriptor *desc)
1924{ 1924{
1925 memset(desc, 0, sizeof *desc); 1925 memset(desc, 0, sizeof *desc);
1926 desc->bDescriptorType = 0x2a; 1926 desc->bDescriptorType = USB_DT_SS_HUB;
1927 desc->bDescLength = 12; 1927 desc->bDescLength = 12;
1928 desc->wHubCharacteristics = cpu_to_le16( 1928 desc->wHubCharacteristics = cpu_to_le16(
1929 HUB_CHAR_INDV_PORT_LPSM | 1929 HUB_CHAR_INDV_PORT_LPSM |
@@ -1936,7 +1936,7 @@ ss_hub_descriptor(struct usb_hub_descriptor *desc)
1936static inline void hub_descriptor(struct usb_hub_descriptor *desc) 1936static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1937{ 1937{
1938 memset(desc, 0, sizeof *desc); 1938 memset(desc, 0, sizeof *desc);
1939 desc->bDescriptorType = 0x29; 1939 desc->bDescriptorType = USB_DT_HUB;
1940 desc->bDescLength = 9; 1940 desc->bDescLength = 9;
1941 desc->wHubCharacteristics = cpu_to_le16( 1941 desc->wHubCharacteristics = cpu_to_le16(
1942 HUB_CHAR_INDV_PORT_LPSM | 1942 HUB_CHAR_INDV_PORT_LPSM |
@@ -2631,7 +2631,7 @@ static int __init init(void)
2631 return -EINVAL; 2631 return -EINVAL;
2632 2632
2633 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) { 2633 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2634 pr_err("Number of emulated UDC must be in range of 1…%d\n", 2634 pr_err("Number of emulated UDC must be in range of 1...%d\n",
2635 MAX_NUM_UDC); 2635 MAX_NUM_UDC);
2636 return -EINVAL; 2636 return -EINVAL;
2637 } 2637 }
diff --git a/drivers/usb/gadget/udc/goku_udc.c b/drivers/usb/gadget/udc/goku_udc.c
index 5b9176e7202a..9e8d842e8c08 100644
--- a/drivers/usb/gadget/udc/goku_udc.c
+++ b/drivers/usb/gadget/udc/goku_udc.c
@@ -1024,35 +1024,79 @@ static const char proc_node_name [] = "driver/udc";
1024static void dump_intmask(struct seq_file *m, const char *label, u32 mask) 1024static void dump_intmask(struct seq_file *m, const char *label, u32 mask)
1025{ 1025{
1026 /* int_status is the same format ... */ 1026 /* int_status is the same format ... */
1027 seq_printf(m, 1027 seq_printf(m, "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1028 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n", 1028 label, mask,
1029 label, mask, 1029 (mask & INT_PWRDETECT) ? " power" : "",
1030 (mask & INT_PWRDETECT) ? " power" : "", 1030 (mask & INT_SYSERROR) ? " sys" : "",
1031 (mask & INT_SYSERROR) ? " sys" : "", 1031 (mask & INT_MSTRDEND) ? " in-dma" : "",
1032 (mask & INT_MSTRDEND) ? " in-dma" : "", 1032 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1033 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "", 1033
1034 1034 (mask & INT_MSTWREND) ? " out-dma" : "",
1035 (mask & INT_MSTWREND) ? " out-dma" : "", 1035 (mask & INT_MSTWRSET) ? " wrset" : "",
1036 (mask & INT_MSTWRSET) ? " wrset" : "", 1036 (mask & INT_ERR) ? " err" : "",
1037 (mask & INT_ERR) ? " err" : "", 1037 (mask & INT_SOF) ? " sof" : "",
1038 (mask & INT_SOF) ? " sof" : "", 1038
1039 1039 (mask & INT_EP3NAK) ? " ep3nak" : "",
1040 (mask & INT_EP3NAK) ? " ep3nak" : "", 1040 (mask & INT_EP2NAK) ? " ep2nak" : "",
1041 (mask & INT_EP2NAK) ? " ep2nak" : "", 1041 (mask & INT_EP1NAK) ? " ep1nak" : "",
1042 (mask & INT_EP1NAK) ? " ep1nak" : "", 1042 (mask & INT_EP3DATASET) ? " ep3" : "",
1043 (mask & INT_EP3DATASET) ? " ep3" : "", 1043
1044 1044 (mask & INT_EP2DATASET) ? " ep2" : "",
1045 (mask & INT_EP2DATASET) ? " ep2" : "", 1045 (mask & INT_EP1DATASET) ? " ep1" : "",
1046 (mask & INT_EP1DATASET) ? " ep1" : "", 1046 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1047 (mask & INT_STATUSNAK) ? " ep0snak" : "", 1047 (mask & INT_STATUS) ? " ep0status" : "",
1048 (mask & INT_STATUS) ? " ep0status" : "", 1048
1049 1049 (mask & INT_SETUP) ? " setup" : "",
1050 (mask & INT_SETUP) ? " setup" : "", 1050 (mask & INT_ENDPOINT0) ? " ep0" : "",
1051 (mask & INT_ENDPOINT0) ? " ep0" : "", 1051 (mask & INT_USBRESET) ? " reset" : "",
1052 (mask & INT_USBRESET) ? " reset" : "", 1052 (mask & INT_SUSPEND) ? " suspend" : "");
1053 (mask & INT_SUSPEND) ? " suspend" : ""); 1053}
1054
1055static const char *udc_ep_state(enum ep0state state)
1056{
1057 switch (state) {
1058 case EP0_DISCONNECT:
1059 return "ep0_disconnect";
1060 case EP0_IDLE:
1061 return "ep0_idle";
1062 case EP0_IN:
1063 return "ep0_in";
1064 case EP0_OUT:
1065 return "ep0_out";
1066 case EP0_STATUS:
1067 return "ep0_status";
1068 case EP0_STALL:
1069 return "ep0_stall";
1070 case EP0_SUSPEND:
1071 return "ep0_suspend";
1072 }
1073
1074 return "ep0_?";
1054} 1075}
1055 1076
1077static const char *udc_ep_status(u32 status)
1078{
1079 switch (status & EPxSTATUS_EP_MASK) {
1080 case EPxSTATUS_EP_READY:
1081 return "ready";
1082 case EPxSTATUS_EP_DATAIN:
1083 return "packet";
1084 case EPxSTATUS_EP_FULL:
1085 return "full";
1086 case EPxSTATUS_EP_TX_ERR: /* host will retry */
1087 return "tx_err";
1088 case EPxSTATUS_EP_RX_ERR:
1089 return "rx_err";
1090 case EPxSTATUS_EP_BUSY: /* ep0 only */
1091 return "busy";
1092 case EPxSTATUS_EP_STALL:
1093 return "stall";
1094 case EPxSTATUS_EP_INVALID: /* these "can't happen" */
1095 return "invalid";
1096 }
1097
1098 return "?";
1099}
1056 1100
1057static int udc_proc_read(struct seq_file *m, void *v) 1101static int udc_proc_read(struct seq_file *m, void *v)
1058{ 1102{
@@ -1068,29 +1112,18 @@ static int udc_proc_read(struct seq_file *m, void *v)
1068 tmp = readl(&regs->power_detect); 1112 tmp = readl(&regs->power_detect);
1069 is_usb_connected = tmp & PW_DETECT; 1113 is_usb_connected = tmp & PW_DETECT;
1070 seq_printf(m, 1114 seq_printf(m,
1071 "%s - %s\n" 1115 "%s - %s\n"
1072 "%s version: %s %s\n" 1116 "%s version: %s %s\n"
1073 "Gadget driver: %s\n" 1117 "Gadget driver: %s\n"
1074 "Host %s, %s\n" 1118 "Host %s, %s\n"
1075 "\n", 1119 "\n",
1076 pci_name(dev->pdev), driver_desc, 1120 pci_name(dev->pdev), driver_desc,
1077 driver_name, DRIVER_VERSION, dmastr(), 1121 driver_name, DRIVER_VERSION, dmastr(),
1078 dev->driver ? dev->driver->driver.name : "(none)", 1122 dev->driver ? dev->driver->driver.name : "(none)",
1079 is_usb_connected 1123 is_usb_connected
1080 ? ((tmp & PW_PULLUP) ? "full speed" : "powered") 1124 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1081 : "disconnected", 1125 : "disconnected",
1082 ({const char *state; 1126 udc_ep_state(dev->ep0state));
1083 switch(dev->ep0state){
1084 case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1085 case EP0_IDLE: state = "ep0_idle"; break;
1086 case EP0_IN: state = "ep0_in"; break;
1087 case EP0_OUT: state = "ep0_out"; break;
1088 case EP0_STATUS: state = "ep0_status"; break;
1089 case EP0_STALL: state = "ep0_stall"; break;
1090 case EP0_SUSPEND: state = "ep0_suspend"; break;
1091 default: state = "ep0_?"; break;
1092 } state; })
1093 );
1094 1127
1095 dump_intmask(m, "int_status", readl(&regs->int_status)); 1128 dump_intmask(m, "int_status", readl(&regs->int_status));
1096 dump_intmask(m, "int_enable", readl(&regs->int_enable)); 1129 dump_intmask(m, "int_enable", readl(&regs->int_enable));
@@ -1099,31 +1132,30 @@ static int udc_proc_read(struct seq_file *m, void *v)
1099 goto done; 1132 goto done;
1100 1133
1101 /* registers for (active) device and ep0 */ 1134 /* registers for (active) device and ep0 */
1102 if (seq_printf(m, "\nirqs %lu\ndataset %02x " 1135 seq_printf(m, "\nirqs %lu\ndataset %02x single.bcs %02x.%02x state %x addr %u\n",
1103 "single.bcs %02x.%02x state %x addr %u\n", 1136 dev->irqs, readl(&regs->DataSet),
1104 dev->irqs, readl(&regs->DataSet), 1137 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1105 readl(&regs->EPxSingle), readl(&regs->EPxBCS), 1138 readl(&regs->UsbState),
1106 readl(&regs->UsbState), 1139 readl(&regs->address));
1107 readl(&regs->address)) < 0) 1140 if (seq_has_overflowed(m))
1108 goto done; 1141 goto done;
1109 1142
1110 tmp = readl(&regs->dma_master); 1143 tmp = readl(&regs->dma_master);
1111 if (seq_printf(m, 1144 seq_printf(m, "dma %03X =" EIGHTBITS "%s %s\n",
1112 "dma %03X =" EIGHTBITS "%s %s\n", tmp, 1145 tmp,
1113 (tmp & MST_EOPB_DIS) ? " eopb-" : "", 1146 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1114 (tmp & MST_EOPB_ENA) ? " eopb+" : "", 1147 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1115 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "", 1148 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1116 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "", 1149 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1117 1150
1118 (tmp & MST_RD_EOPB) ? " eopb" : "", 1151 (tmp & MST_RD_EOPB) ? " eopb" : "",
1119 (tmp & MST_RD_RESET) ? " in_reset" : "", 1152 (tmp & MST_RD_RESET) ? " in_reset" : "",
1120 (tmp & MST_WR_RESET) ? " out_reset" : "", 1153 (tmp & MST_WR_RESET) ? " out_reset" : "",
1121 (tmp & MST_RD_ENA) ? " IN" : "", 1154 (tmp & MST_RD_ENA) ? " IN" : "",
1122 1155
1123 (tmp & MST_WR_ENA) ? " OUT" : "", 1156 (tmp & MST_WR_ENA) ? " OUT" : "",
1124 (tmp & MST_CONNECTION) 1157 (tmp & MST_CONNECTION) ? "ep1in/ep2out" : "ep1out/ep2in");
1125 ? "ep1in/ep2out" 1158 if (seq_has_overflowed(m))
1126 : "ep1out/ep2in") < 0)
1127 goto done; 1159 goto done;
1128 1160
1129 /* dump endpoint queues */ 1161 /* dump endpoint queues */
@@ -1135,44 +1167,23 @@ static int udc_proc_read(struct seq_file *m, void *v)
1135 continue; 1167 continue;
1136 1168
1137 tmp = readl(ep->reg_status); 1169 tmp = readl(ep->reg_status);
1138 if (seq_printf(m, 1170 seq_printf(m, "%s %s max %u %s, irqs %lu, status %02x (%s) " FOURBITS "\n",
1139 "%s %s max %u %s, irqs %lu, " 1171 ep->ep.name,
1140 "status %02x (%s) " FOURBITS "\n", 1172 ep->is_in ? "in" : "out",
1141 ep->ep.name, 1173 ep->ep.maxpacket,
1142 ep->is_in ? "in" : "out", 1174 ep->dma ? "dma" : "pio",
1143 ep->ep.maxpacket, 1175 ep->irqs,
1144 ep->dma ? "dma" : "pio", 1176 tmp, udc_ep_status(tmp),
1145 ep->irqs, 1177 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1146 tmp, ({ char *s; 1178 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1147 switch (tmp & EPxSTATUS_EP_MASK) { 1179 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1148 case EPxSTATUS_EP_READY: 1180 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : "");
1149 s = "ready"; break; 1181 if (seq_has_overflowed(m))
1150 case EPxSTATUS_EP_DATAIN:
1151 s = "packet"; break;
1152 case EPxSTATUS_EP_FULL:
1153 s = "full"; break;
1154 case EPxSTATUS_EP_TX_ERR: // host will retry
1155 s = "tx_err"; break;
1156 case EPxSTATUS_EP_RX_ERR:
1157 s = "rx_err"; break;
1158 case EPxSTATUS_EP_BUSY: /* ep0 only */
1159 s = "busy"; break;
1160 case EPxSTATUS_EP_STALL:
1161 s = "stall"; break;
1162 case EPxSTATUS_EP_INVALID: // these "can't happen"
1163 s = "invalid"; break;
1164 default:
1165 s = "?"; break;
1166 } s; }),
1167 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1168 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1169 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1170 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1171 ) < 0)
1172 goto done; 1182 goto done;
1173 1183
1174 if (list_empty(&ep->queue)) { 1184 if (list_empty(&ep->queue)) {
1175 if (seq_puts(m, "\t(nothing queued)\n") < 0) 1185 seq_puts(m, "\t(nothing queued)\n");
1186 if (seq_has_overflowed(m))
1176 goto done; 1187 goto done;
1177 continue; 1188 continue;
1178 } 1189 }
@@ -1187,10 +1198,10 @@ static int udc_proc_read(struct seq_file *m, void *v)
1187 } else 1198 } else
1188 tmp = req->req.actual; 1199 tmp = req->req.actual;
1189 1200
1190 if (seq_printf(m, 1201 seq_printf(m, "\treq %p len %u/%u buf %p\n",
1191 "\treq %p len %u/%u buf %p\n", 1202 &req->req, tmp, req->req.length,
1192 &req->req, tmp, req->req.length, 1203 req->req.buf);
1193 req->req.buf) < 0) 1204 if (seq_has_overflowed(m))
1194 goto done; 1205 goto done;
1195 } 1206 }
1196 } 1207 }
diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c
index 27fd41333f71..3b6a7852822d 100644
--- a/drivers/usb/gadget/udc/lpc32xx_udc.c
+++ b/drivers/usb/gadget/udc/lpc32xx_udc.c
@@ -1803,23 +1803,14 @@ static int lpc32xx_ep_queue(struct usb_ep *_ep,
1803 req = container_of(_req, struct lpc32xx_request, req); 1803 req = container_of(_req, struct lpc32xx_request, req);
1804 ep = container_of(_ep, struct lpc32xx_ep, ep); 1804 ep = container_of(_ep, struct lpc32xx_ep, ep);
1805 1805
1806 if (!_req || !_req->complete || !_req->buf || 1806 if (!_ep || !_req || !_req->complete || !_req->buf ||
1807 !list_empty(&req->queue)) 1807 !list_empty(&req->queue))
1808 return -EINVAL; 1808 return -EINVAL;
1809 1809
1810 udc = ep->udc; 1810 udc = ep->udc;
1811 1811
1812 if (!_ep) { 1812 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1813 dev_dbg(udc->dev, "invalid ep\n"); 1813 return -EPIPE;
1814 return -EINVAL;
1815 }
1816
1817
1818 if ((!udc) || (!udc->driver) ||
1819 (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1820 dev_dbg(udc->dev, "invalid device\n");
1821 return -EINVAL;
1822 }
1823 1814
1824 if (ep->lep) { 1815 if (ep->lep) {
1825 struct lpc32xx_usbd_dd_gad *dd; 1816 struct lpc32xx_usbd_dd_gad *dd;
diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
index d2c0bf65e345..9871b90195ad 100644
--- a/drivers/usb/gadget/udc/net2280.c
+++ b/drivers/usb/gadget/udc/net2280.c
@@ -80,6 +80,13 @@ static const char *const ep_name[] = {
80 "ep-e", "ep-f", "ep-g", "ep-h", 80 "ep-e", "ep-f", "ep-g", "ep-h",
81}; 81};
82 82
83/* Endpoint names for usb3380 advance mode */
84static const char *const ep_name_adv[] = {
85 ep0name,
86 "ep1in", "ep2out", "ep3in", "ep4out",
87 "ep1out", "ep2in", "ep3out", "ep4in",
88};
89
83/* mode 0 == ep-{a,b,c,d} 1K fifo each 90/* mode 0 == ep-{a,b,c,d} 1K fifo each
84 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable 91 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
85 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable 92 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
@@ -138,31 +145,44 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
138 u32 max, tmp; 145 u32 max, tmp;
139 unsigned long flags; 146 unsigned long flags;
140 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 }; 147 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
148 int ret = 0;
141 149
142 ep = container_of(_ep, struct net2280_ep, ep); 150 ep = container_of(_ep, struct net2280_ep, ep);
143 if (!_ep || !desc || ep->desc || _ep->name == ep0name || 151 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
144 desc->bDescriptorType != USB_DT_ENDPOINT) 152 desc->bDescriptorType != USB_DT_ENDPOINT) {
153 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
145 return -EINVAL; 154 return -EINVAL;
155 }
146 dev = ep->dev; 156 dev = ep->dev;
147 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 157 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
148 return -ESHUTDOWN; 158 ret = -ESHUTDOWN;
159 goto print_err;
160 }
149 161
150 /* erratum 0119 workaround ties up an endpoint number */ 162 /* erratum 0119 workaround ties up an endpoint number */
151 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) 163 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
152 return -EDOM; 164 ret = -EDOM;
165 goto print_err;
166 }
153 167
154 if (dev->quirks & PLX_SUPERSPEED) { 168 if (dev->quirks & PLX_SUPERSPEED) {
155 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) 169 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
156 return -EDOM; 170 ret = -EDOM;
171 goto print_err;
172 }
157 ep->is_in = !!usb_endpoint_dir_in(desc); 173 ep->is_in = !!usb_endpoint_dir_in(desc);
158 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) 174 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
159 return -EINVAL; 175 ret = -EINVAL;
176 goto print_err;
177 }
160 } 178 }
161 179
162 /* sanity check ep-e/ep-f since their fifos are small */ 180 /* sanity check ep-e/ep-f since their fifos are small */
163 max = usb_endpoint_maxp(desc) & 0x1fff; 181 max = usb_endpoint_maxp(desc) & 0x1fff;
164 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) 182 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
165 return -ERANGE; 183 ret = -ERANGE;
184 goto print_err;
185 }
166 186
167 spin_lock_irqsave(&dev->lock, flags); 187 spin_lock_irqsave(&dev->lock, flags);
168 _ep->maxpacket = max & 0x7ff; 188 _ep->maxpacket = max & 0x7ff;
@@ -192,7 +212,8 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
192 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) || 212 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
193 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) { 213 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
194 spin_unlock_irqrestore(&dev->lock, flags); 214 spin_unlock_irqrestore(&dev->lock, flags);
195 return -ERANGE; 215 ret = -ERANGE;
216 goto print_err;
196 } 217 }
197 } 218 }
198 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC); 219 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
@@ -271,7 +292,11 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
271 292
272 /* pci writes may still be posted */ 293 /* pci writes may still be posted */
273 spin_unlock_irqrestore(&dev->lock, flags); 294 spin_unlock_irqrestore(&dev->lock, flags);
274 return 0; 295 return ret;
296
297print_err:
298 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
299 return ret;
275} 300}
276 301
277static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec) 302static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
@@ -426,9 +451,10 @@ static int net2280_disable(struct usb_ep *_ep)
426 unsigned long flags; 451 unsigned long flags;
427 452
428 ep = container_of(_ep, struct net2280_ep, ep); 453 ep = container_of(_ep, struct net2280_ep, ep);
429 if (!_ep || !ep->desc || _ep->name == ep0name) 454 if (!_ep || !ep->desc || _ep->name == ep0name) {
455 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
430 return -EINVAL; 456 return -EINVAL;
431 457 }
432 spin_lock_irqsave(&ep->dev->lock, flags); 458 spin_lock_irqsave(&ep->dev->lock, flags);
433 nuke(ep); 459 nuke(ep);
434 460
@@ -458,8 +484,10 @@ static struct usb_request
458 struct net2280_ep *ep; 484 struct net2280_ep *ep;
459 struct net2280_request *req; 485 struct net2280_request *req;
460 486
461 if (!_ep) 487 if (!_ep) {
488 pr_err("%s: Invalid ep\n", __func__);
462 return NULL; 489 return NULL;
490 }
463 ep = container_of(_ep, struct net2280_ep, ep); 491 ep = container_of(_ep, struct net2280_ep, ep);
464 492
465 req = kzalloc(sizeof(*req), gfp_flags); 493 req = kzalloc(sizeof(*req), gfp_flags);
@@ -491,8 +519,11 @@ static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
491 struct net2280_request *req; 519 struct net2280_request *req;
492 520
493 ep = container_of(_ep, struct net2280_ep, ep); 521 ep = container_of(_ep, struct net2280_ep, ep);
494 if (!_ep || !_req) 522 if (!_ep || !_req) {
523 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
524 __func__, _ep, _req);
495 return; 525 return;
526 }
496 527
497 req = container_of(_req, struct net2280_request, req); 528 req = container_of(_req, struct net2280_request, req);
498 WARN_ON(!list_empty(&req->queue)); 529 WARN_ON(!list_empty(&req->queue));
@@ -896,35 +927,44 @@ net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
896 struct net2280_ep *ep; 927 struct net2280_ep *ep;
897 struct net2280 *dev; 928 struct net2280 *dev;
898 unsigned long flags; 929 unsigned long flags;
930 int ret = 0;
899 931
900 /* we always require a cpu-view buffer, so that we can 932 /* we always require a cpu-view buffer, so that we can
901 * always use pio (as fallback or whatever). 933 * always use pio (as fallback or whatever).
902 */ 934 */
903 req = container_of(_req, struct net2280_request, req);
904 if (!_req || !_req->complete || !_req->buf ||
905 !list_empty(&req->queue))
906 return -EINVAL;
907 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
908 return -EDOM;
909 ep = container_of(_ep, struct net2280_ep, ep); 935 ep = container_of(_ep, struct net2280_ep, ep);
910 if (!_ep || (!ep->desc && ep->num != 0)) 936 if (!_ep || (!ep->desc && ep->num != 0)) {
937 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
911 return -EINVAL; 938 return -EINVAL;
939 }
940 req = container_of(_req, struct net2280_request, req);
941 if (!_req || !_req->complete || !_req->buf ||
942 !list_empty(&req->queue)) {
943 ret = -EINVAL;
944 goto print_err;
945 }
946 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
947 ret = -EDOM;
948 goto print_err;
949 }
912 dev = ep->dev; 950 dev = ep->dev;
913 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 951 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
914 return -ESHUTDOWN; 952 ret = -ESHUTDOWN;
953 goto print_err;
954 }
915 955
916 /* FIXME implement PIO fallback for ZLPs with DMA */ 956 /* FIXME implement PIO fallback for ZLPs with DMA */
917 if (ep->dma && _req->length == 0) 957 if (ep->dma && _req->length == 0) {
918 return -EOPNOTSUPP; 958 ret = -EOPNOTSUPP;
959 goto print_err;
960 }
919 961
920 /* set up dma mapping in case the caller didn't */ 962 /* set up dma mapping in case the caller didn't */
921 if (ep->dma) { 963 if (ep->dma) {
922 int ret;
923
924 ret = usb_gadget_map_request(&dev->gadget, _req, 964 ret = usb_gadget_map_request(&dev->gadget, _req,
925 ep->is_in); 965 ep->is_in);
926 if (ret) 966 if (ret)
927 return ret; 967 goto print_err;
928 } 968 }
929 969
930 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n", 970 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
@@ -1013,7 +1053,11 @@ done:
1013 spin_unlock_irqrestore(&dev->lock, flags); 1053 spin_unlock_irqrestore(&dev->lock, flags);
1014 1054
1015 /* pci writes may still be posted */ 1055 /* pci writes may still be posted */
1016 return 0; 1056 return ret;
1057
1058print_err:
1059 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1060 return ret;
1017} 1061}
1018 1062
1019static inline void 1063static inline void
@@ -1134,8 +1178,11 @@ static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1134 int stopped; 1178 int stopped;
1135 1179
1136 ep = container_of(_ep, struct net2280_ep, ep); 1180 ep = container_of(_ep, struct net2280_ep, ep);
1137 if (!_ep || (!ep->desc && ep->num != 0) || !_req) 1181 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1182 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1183 __func__, _ep, _req);
1138 return -EINVAL; 1184 return -EINVAL;
1185 }
1139 1186
1140 spin_lock_irqsave(&ep->dev->lock, flags); 1187 spin_lock_irqsave(&ep->dev->lock, flags);
1141 stopped = ep->stopped; 1188 stopped = ep->stopped;
@@ -1157,6 +1204,8 @@ static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1157 } 1204 }
1158 if (&req->req != _req) { 1205 if (&req->req != _req) {
1159 spin_unlock_irqrestore(&ep->dev->lock, flags); 1206 spin_unlock_irqrestore(&ep->dev->lock, flags);
1207 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1208 __func__);
1160 return -EINVAL; 1209 return -EINVAL;
1161 } 1210 }
1162 1211
@@ -1214,20 +1263,28 @@ net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1214 int retval = 0; 1263 int retval = 0;
1215 1264
1216 ep = container_of(_ep, struct net2280_ep, ep); 1265 ep = container_of(_ep, struct net2280_ep, ep);
1217 if (!_ep || (!ep->desc && ep->num != 0)) 1266 if (!_ep || (!ep->desc && ep->num != 0)) {
1267 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1218 return -EINVAL; 1268 return -EINVAL;
1219 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1269 }
1220 return -ESHUTDOWN; 1270 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1271 retval = -ESHUTDOWN;
1272 goto print_err;
1273 }
1221 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03) 1274 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1222 == USB_ENDPOINT_XFER_ISOC) 1275 == USB_ENDPOINT_XFER_ISOC) {
1223 return -EINVAL; 1276 retval = -EINVAL;
1277 goto print_err;
1278 }
1224 1279
1225 spin_lock_irqsave(&ep->dev->lock, flags); 1280 spin_lock_irqsave(&ep->dev->lock, flags);
1226 if (!list_empty(&ep->queue)) 1281 if (!list_empty(&ep->queue)) {
1227 retval = -EAGAIN; 1282 retval = -EAGAIN;
1228 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) 1283 goto print_unlock;
1284 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1229 retval = -EAGAIN; 1285 retval = -EAGAIN;
1230 else { 1286 goto print_unlock;
1287 } else {
1231 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name, 1288 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1232 value ? "set" : "clear", 1289 value ? "set" : "clear",
1233 wedged ? "wedge" : "halt"); 1290 wedged ? "wedge" : "halt");
@@ -1251,6 +1308,12 @@ net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1251 spin_unlock_irqrestore(&ep->dev->lock, flags); 1308 spin_unlock_irqrestore(&ep->dev->lock, flags);
1252 1309
1253 return retval; 1310 return retval;
1311
1312print_unlock:
1313 spin_unlock_irqrestore(&ep->dev->lock, flags);
1314print_err:
1315 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1316 return retval;
1254} 1317}
1255 1318
1256static int net2280_set_halt(struct usb_ep *_ep, int value) 1319static int net2280_set_halt(struct usb_ep *_ep, int value)
@@ -1260,8 +1323,10 @@ static int net2280_set_halt(struct usb_ep *_ep, int value)
1260 1323
1261static int net2280_set_wedge(struct usb_ep *_ep) 1324static int net2280_set_wedge(struct usb_ep *_ep)
1262{ 1325{
1263 if (!_ep || _ep->name == ep0name) 1326 if (!_ep || _ep->name == ep0name) {
1327 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1264 return -EINVAL; 1328 return -EINVAL;
1329 }
1265 return net2280_set_halt_and_wedge(_ep, 1, 1); 1330 return net2280_set_halt_and_wedge(_ep, 1, 1);
1266} 1331}
1267 1332
@@ -1271,14 +1336,22 @@ static int net2280_fifo_status(struct usb_ep *_ep)
1271 u32 avail; 1336 u32 avail;
1272 1337
1273 ep = container_of(_ep, struct net2280_ep, ep); 1338 ep = container_of(_ep, struct net2280_ep, ep);
1274 if (!_ep || (!ep->desc && ep->num != 0)) 1339 if (!_ep || (!ep->desc && ep->num != 0)) {
1340 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1275 return -ENODEV; 1341 return -ENODEV;
1276 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1342 }
1343 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1344 dev_err(&ep->dev->pdev->dev,
1345 "%s: Invalid driver=%p or speed=%d\n",
1346 __func__, ep->dev->driver, ep->dev->gadget.speed);
1277 return -ESHUTDOWN; 1347 return -ESHUTDOWN;
1348 }
1278 1349
1279 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1); 1350 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1280 if (avail > ep->fifo_size) 1351 if (avail > ep->fifo_size) {
1352 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1281 return -EOVERFLOW; 1353 return -EOVERFLOW;
1354 }
1282 if (ep->is_in) 1355 if (ep->is_in)
1283 avail = ep->fifo_size - avail; 1356 avail = ep->fifo_size - avail;
1284 return avail; 1357 return avail;
@@ -1289,10 +1362,16 @@ static void net2280_fifo_flush(struct usb_ep *_ep)
1289 struct net2280_ep *ep; 1362 struct net2280_ep *ep;
1290 1363
1291 ep = container_of(_ep, struct net2280_ep, ep); 1364 ep = container_of(_ep, struct net2280_ep, ep);
1292 if (!_ep || (!ep->desc && ep->num != 0)) 1365 if (!_ep || (!ep->desc && ep->num != 0)) {
1366 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1293 return; 1367 return;
1294 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1368 }
1369 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1370 dev_err(&ep->dev->pdev->dev,
1371 "%s: Invalid driver=%p or speed=%d\n",
1372 __func__, ep->dev->driver, ep->dev->gadget.speed);
1295 return; 1373 return;
1374 }
1296 1375
1297 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat); 1376 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1298 (void) readl(&ep->regs->ep_rsp); 1377 (void) readl(&ep->regs->ep_rsp);
@@ -1977,7 +2056,7 @@ static void usb_reinit_338x(struct net2280 *dev)
1977 for (i = 0; i < dev->n_ep; i++) { 2056 for (i = 0; i < dev->n_ep; i++) {
1978 struct net2280_ep *ep = &dev->ep[i]; 2057 struct net2280_ep *ep = &dev->ep[i];
1979 2058
1980 ep->ep.name = ep_name[i]; 2059 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
1981 ep->dev = dev; 2060 ep->dev = dev;
1982 ep->num = i; 2061 ep->num = i;
1983 2062
@@ -1989,11 +2068,9 @@ static void usb_reinit_338x(struct net2280 *dev)
1989 ep->regs = (struct net2280_ep_regs __iomem *) 2068 ep->regs = (struct net2280_ep_regs __iomem *)
1990 (((void __iomem *)&dev->epregs[ne[i]]) + 2069 (((void __iomem *)&dev->epregs[ne[i]]) +
1991 ep_reg_addr[i]); 2070 ep_reg_addr[i]);
1992 ep->fiforegs = &dev->fiforegs[i];
1993 } else { 2071 } else {
1994 ep->cfg = &dev->epregs[i]; 2072 ep->cfg = &dev->epregs[i];
1995 ep->regs = &dev->epregs[i]; 2073 ep->regs = &dev->epregs[i];
1996 ep->fiforegs = &dev->fiforegs[i];
1997 } 2074 }
1998 2075
1999 ep->fifo_size = (i != 0) ? 2048 : 512; 2076 ep->fifo_size = (i != 0) ? 2048 : 512;
@@ -2186,7 +2263,6 @@ static int net2280_start(struct usb_gadget *_gadget,
2186 dev->ep[i].irqs = 0; 2263 dev->ep[i].irqs = 0;
2187 2264
2188 /* hook up the driver ... */ 2265 /* hook up the driver ... */
2189 dev->softconnect = 1;
2190 driver->driver.bus = NULL; 2266 driver->driver.bus = NULL;
2191 dev->driver = driver; 2267 dev->driver = driver;
2192 2268
@@ -3052,6 +3128,8 @@ next_endpoints:
3052 BIT(PCI_RETRY_ABORT_INTERRUPT)) 3128 BIT(PCI_RETRY_ABORT_INTERRUPT))
3053 3129
3054static void handle_stat1_irqs(struct net2280 *dev, u32 stat) 3130static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3131__releases(dev->lock)
3132__acquires(dev->lock)
3055{ 3133{
3056 struct net2280_ep *ep; 3134 struct net2280_ep *ep;
3057 u32 tmp, num, mask, scratch; 3135 u32 tmp, num, mask, scratch;
@@ -3373,8 +3451,6 @@ static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3373 u32 usbstat; 3451 u32 usbstat;
3374 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *) 3452 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3375 (base + 0x00b4); 3453 (base + 0x00b4);
3376 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3377 (base + 0x0500);
3378 dev->llregs = (struct usb338x_ll_regs __iomem *) 3454 dev->llregs = (struct usb338x_ll_regs __iomem *)
3379 (base + 0x0700); 3455 (base + 0x0700);
3380 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *) 3456 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
diff --git a/drivers/usb/gadget/udc/net2280.h b/drivers/usb/gadget/udc/net2280.h
index ac8d5a20a378..4dff60d34f73 100644
--- a/drivers/usb/gadget/udc/net2280.h
+++ b/drivers/usb/gadget/udc/net2280.h
@@ -96,7 +96,6 @@ struct net2280_ep {
96 struct net2280_ep_regs __iomem *regs; 96 struct net2280_ep_regs __iomem *regs;
97 struct net2280_dma_regs __iomem *dma; 97 struct net2280_dma_regs __iomem *dma;
98 struct net2280_dma *dummy; 98 struct net2280_dma *dummy;
99 struct usb338x_fifo_regs __iomem *fiforegs;
100 dma_addr_t td_dma; /* of dummy */ 99 dma_addr_t td_dma; /* of dummy */
101 struct net2280 *dev; 100 struct net2280 *dev;
102 unsigned long irqs; 101 unsigned long irqs;
@@ -181,7 +180,6 @@ struct net2280 {
181 struct net2280_dma_regs __iomem *dma; 180 struct net2280_dma_regs __iomem *dma;
182 struct net2280_dep_regs __iomem *dep; 181 struct net2280_dep_regs __iomem *dep;
183 struct net2280_ep_regs __iomem *epregs; 182 struct net2280_ep_regs __iomem *epregs;
184 struct usb338x_fifo_regs __iomem *fiforegs;
185 struct usb338x_ll_regs __iomem *llregs; 183 struct usb338x_ll_regs __iomem *llregs;
186 struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs; 184 struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs;
187 struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs; 185 struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs;
diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index 6a855fc9bd84..b51226abade6 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -93,50 +93,46 @@ static void handle_ep(struct pxa_ep *ep);
93static int state_dbg_show(struct seq_file *s, void *p) 93static int state_dbg_show(struct seq_file *s, void *p)
94{ 94{
95 struct pxa_udc *udc = s->private; 95 struct pxa_udc *udc = s->private;
96 int pos = 0, ret;
97 u32 tmp; 96 u32 tmp;
98 97
99 ret = -ENODEV;
100 if (!udc->driver) 98 if (!udc->driver)
101 goto out; 99 return -ENODEV;
102 100
103 /* basic device status */ 101 /* basic device status */
104 pos += seq_printf(s, DRIVER_DESC "\n" 102 seq_printf(s, DRIVER_DESC "\n"
105 "%s version: %s\nGadget driver: %s\n", 103 "%s version: %s\n"
106 driver_name, DRIVER_VERSION, 104 "Gadget driver: %s\n",
107 udc->driver ? udc->driver->driver.name : "(none)"); 105 driver_name, DRIVER_VERSION,
106 udc->driver ? udc->driver->driver.name : "(none)");
108 107
109 tmp = udc_readl(udc, UDCCR); 108 tmp = udc_readl(udc, UDCCR);
110 pos += seq_printf(s, 109 seq_printf(s,
111 "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), " 110 "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), con=%d,inter=%d,altinter=%d\n",
112 "con=%d,inter=%d,altinter=%d\n", tmp, 111 tmp,
113 (tmp & UDCCR_OEN) ? " oen":"", 112 (tmp & UDCCR_OEN) ? " oen":"",
114 (tmp & UDCCR_AALTHNP) ? " aalthnp":"", 113 (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
115 (tmp & UDCCR_AHNP) ? " rem" : "", 114 (tmp & UDCCR_AHNP) ? " rem" : "",
116 (tmp & UDCCR_BHNP) ? " rstir" : "", 115 (tmp & UDCCR_BHNP) ? " rstir" : "",
117 (tmp & UDCCR_DWRE) ? " dwre" : "", 116 (tmp & UDCCR_DWRE) ? " dwre" : "",
118 (tmp & UDCCR_SMAC) ? " smac" : "", 117 (tmp & UDCCR_SMAC) ? " smac" : "",
119 (tmp & UDCCR_EMCE) ? " emce" : "", 118 (tmp & UDCCR_EMCE) ? " emce" : "",
120 (tmp & UDCCR_UDR) ? " udr" : "", 119 (tmp & UDCCR_UDR) ? " udr" : "",
121 (tmp & UDCCR_UDA) ? " uda" : "", 120 (tmp & UDCCR_UDA) ? " uda" : "",
122 (tmp & UDCCR_UDE) ? " ude" : "", 121 (tmp & UDCCR_UDE) ? " ude" : "",
123 (tmp & UDCCR_ACN) >> UDCCR_ACN_S, 122 (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
124 (tmp & UDCCR_AIN) >> UDCCR_AIN_S, 123 (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
125 (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S); 124 (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S);
126 /* registers for device and ep0 */ 125 /* registers for device and ep0 */
127 pos += seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n", 126 seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n",
128 udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1)); 127 udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1));
129 pos += seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n", 128 seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n",
130 udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1)); 129 udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1));
131 pos += seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR)); 130 seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR));
132 pos += seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, " 131 seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, reconfig=%lu\n",
133 "reconfig=%lu\n", 132 udc->stats.irqs_reset, udc->stats.irqs_suspend,
134 udc->stats.irqs_reset, udc->stats.irqs_suspend, 133 udc->stats.irqs_resume, udc->stats.irqs_reconfig);
135 udc->stats.irqs_resume, udc->stats.irqs_reconfig); 134
136 135 return 0;
137 ret = 0;
138out:
139 return ret;
140} 136}
141 137
142static int queues_dbg_show(struct seq_file *s, void *p) 138static int queues_dbg_show(struct seq_file *s, void *p)
@@ -144,75 +140,67 @@ static int queues_dbg_show(struct seq_file *s, void *p)
144 struct pxa_udc *udc = s->private; 140 struct pxa_udc *udc = s->private;
145 struct pxa_ep *ep; 141 struct pxa_ep *ep;
146 struct pxa27x_request *req; 142 struct pxa27x_request *req;
147 int pos = 0, i, maxpkt, ret; 143 int i, maxpkt;
148 144
149 ret = -ENODEV;
150 if (!udc->driver) 145 if (!udc->driver)
151 goto out; 146 return -ENODEV;
152 147
153 /* dump endpoint queues */ 148 /* dump endpoint queues */
154 for (i = 0; i < NR_PXA_ENDPOINTS; i++) { 149 for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
155 ep = &udc->pxa_ep[i]; 150 ep = &udc->pxa_ep[i];
156 maxpkt = ep->fifo_size; 151 maxpkt = ep->fifo_size;
157 pos += seq_printf(s, "%-12s max_pkt=%d %s\n", 152 seq_printf(s, "%-12s max_pkt=%d %s\n",
158 EPNAME(ep), maxpkt, "pio"); 153 EPNAME(ep), maxpkt, "pio");
159 154
160 if (list_empty(&ep->queue)) { 155 if (list_empty(&ep->queue)) {
161 pos += seq_printf(s, "\t(nothing queued)\n"); 156 seq_puts(s, "\t(nothing queued)\n");
162 continue; 157 continue;
163 } 158 }
164 159
165 list_for_each_entry(req, &ep->queue, queue) { 160 list_for_each_entry(req, &ep->queue, queue) {
166 pos += seq_printf(s, "\treq %p len %d/%d buf %p\n", 161 seq_printf(s, "\treq %p len %d/%d buf %p\n",
167 &req->req, req->req.actual, 162 &req->req, req->req.actual,
168 req->req.length, req->req.buf); 163 req->req.length, req->req.buf);
169 } 164 }
170 } 165 }
171 166
172 ret = 0; 167 return 0;
173out:
174 return ret;
175} 168}
176 169
177static int eps_dbg_show(struct seq_file *s, void *p) 170static int eps_dbg_show(struct seq_file *s, void *p)
178{ 171{
179 struct pxa_udc *udc = s->private; 172 struct pxa_udc *udc = s->private;
180 struct pxa_ep *ep; 173 struct pxa_ep *ep;
181 int pos = 0, i, ret; 174 int i;
182 u32 tmp; 175 u32 tmp;
183 176
184 ret = -ENODEV;
185 if (!udc->driver) 177 if (!udc->driver)
186 goto out; 178 return -ENODEV;
187 179
188 ep = &udc->pxa_ep[0]; 180 ep = &udc->pxa_ep[0];
189 tmp = udc_ep_readl(ep, UDCCSR); 181 tmp = udc_ep_readl(ep, UDCCSR);
190 pos += seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n", tmp, 182 seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n",
191 (tmp & UDCCSR0_SA) ? " sa" : "", 183 tmp,
192 (tmp & UDCCSR0_RNE) ? " rne" : "", 184 (tmp & UDCCSR0_SA) ? " sa" : "",
193 (tmp & UDCCSR0_FST) ? " fst" : "", 185 (tmp & UDCCSR0_RNE) ? " rne" : "",
194 (tmp & UDCCSR0_SST) ? " sst" : "", 186 (tmp & UDCCSR0_FST) ? " fst" : "",
195 (tmp & UDCCSR0_DME) ? " dme" : "", 187 (tmp & UDCCSR0_SST) ? " sst" : "",
196 (tmp & UDCCSR0_IPR) ? " ipr" : "", 188 (tmp & UDCCSR0_DME) ? " dme" : "",
197 (tmp & UDCCSR0_OPC) ? " opc" : ""); 189 (tmp & UDCCSR0_IPR) ? " ipr" : "",
190 (tmp & UDCCSR0_OPC) ? " opc" : "");
198 for (i = 0; i < NR_PXA_ENDPOINTS; i++) { 191 for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
199 ep = &udc->pxa_ep[i]; 192 ep = &udc->pxa_ep[i];
200 tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR); 193 tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR);
201 pos += seq_printf(s, "%-12s: " 194 seq_printf(s, "%-12s: IN %lu(%lu reqs), OUT %lu(%lu reqs), irqs=%lu, udccr=0x%08x, udccsr=0x%03x, udcbcr=%d\n",
202 "IN %lu(%lu reqs), OUT %lu(%lu reqs), " 195 EPNAME(ep),
203 "irqs=%lu, udccr=0x%08x, udccsr=0x%03x, " 196 ep->stats.in_bytes, ep->stats.in_ops,
204 "udcbcr=%d\n", 197 ep->stats.out_bytes, ep->stats.out_ops,
205 EPNAME(ep), 198 ep->stats.irqs,
206 ep->stats.in_bytes, ep->stats.in_ops, 199 tmp, udc_ep_readl(ep, UDCCSR),
207 ep->stats.out_bytes, ep->stats.out_ops, 200 udc_ep_readl(ep, UDCBCR));
208 ep->stats.irqs,
209 tmp, udc_ep_readl(ep, UDCCSR),
210 udc_ep_readl(ep, UDCBCR));
211 } 201 }
212 202
213 ret = 0; 203 return 0;
214out:
215 return ret;
216} 204}
217 205
218static int eps_dbg_open(struct inode *inode, struct file *file) 206static int eps_dbg_open(struct inode *inode, struct file *file)
@@ -2399,7 +2387,7 @@ static struct pxa_udc memory = {
2399}; 2387};
2400 2388
2401#if defined(CONFIG_OF) 2389#if defined(CONFIG_OF)
2402static struct of_device_id udc_pxa_dt_ids[] = { 2390static const struct of_device_id udc_pxa_dt_ids[] = {
2403 { .compatible = "marvell,pxa270-udc" }, 2391 { .compatible = "marvell,pxa270-udc" },
2404 {} 2392 {}
2405}; 2393};
diff --git a/drivers/usb/gadget/udc/udc-core.c b/drivers/usb/gadget/udc/udc-core.c
index 5a81cb086b99..d69c35558f68 100644
--- a/drivers/usb/gadget/udc/udc-core.c
+++ b/drivers/usb/gadget/udc/udc-core.c
@@ -35,6 +35,8 @@
35 * @dev - the child device to the actual controller 35 * @dev - the child device to the actual controller
36 * @gadget - the gadget. For use by the class code 36 * @gadget - the gadget. For use by the class code
37 * @list - for use by the udc class driver 37 * @list - for use by the udc class driver
38 * @vbus - for udcs who care about vbus status, this value is real vbus status;
39 * for udcs who do not care about vbus status, this value is always true
38 * 40 *
39 * This represents the internal data structure which is used by the UDC-class 41 * This represents the internal data structure which is used by the UDC-class
40 * to hold information about udc driver and gadget together. 42 * to hold information about udc driver and gadget together.
@@ -44,6 +46,7 @@ struct usb_udc {
44 struct usb_gadget *gadget; 46 struct usb_gadget *gadget;
45 struct device dev; 47 struct device dev;
46 struct list_head list; 48 struct list_head list;
49 bool vbus;
47}; 50};
48 51
49static struct class *udc_class; 52static struct class *udc_class;
@@ -128,21 +131,11 @@ EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
128 131
129static void usb_gadget_state_work(struct work_struct *work) 132static void usb_gadget_state_work(struct work_struct *work)
130{ 133{
131 struct usb_gadget *gadget = work_to_gadget(work); 134 struct usb_gadget *gadget = work_to_gadget(work);
132 struct usb_udc *udc = NULL; 135 struct usb_udc *udc = gadget->udc;
133
134 mutex_lock(&udc_lock);
135 list_for_each_entry(udc, &udc_list, list)
136 if (udc->gadget == gadget)
137 goto found;
138 mutex_unlock(&udc_lock);
139
140 return;
141
142found:
143 mutex_unlock(&udc_lock);
144 136
145 sysfs_notify(&udc->dev.kobj, NULL, "state"); 137 if (udc)
138 sysfs_notify(&udc->dev.kobj, NULL, "state");
146} 139}
147 140
148void usb_gadget_set_state(struct usb_gadget *gadget, 141void usb_gadget_set_state(struct usb_gadget *gadget,
@@ -155,6 +148,34 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
155 148
156/* ------------------------------------------------------------------------- */ 149/* ------------------------------------------------------------------------- */
157 150
151static void usb_udc_connect_control(struct usb_udc *udc)
152{
153 if (udc->vbus)
154 usb_gadget_connect(udc->gadget);
155 else
156 usb_gadget_disconnect(udc->gadget);
157}
158
159/**
160 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
161 * connect or disconnect gadget
162 * @gadget: The gadget which vbus change occurs
163 * @status: The vbus status
164 *
165 * The udc driver calls it when it wants to connect or disconnect gadget
166 * according to vbus status.
167 */
168void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
169{
170 struct usb_udc *udc = gadget->udc;
171
172 if (udc) {
173 udc->vbus = status;
174 usb_udc_connect_control(udc);
175 }
176}
177EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
178
158/** 179/**
159 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs 180 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
160 * @gadget: The gadget which bus reset occurs 181 * @gadget: The gadget which bus reset occurs
@@ -278,6 +299,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
278 goto err3; 299 goto err3;
279 300
280 udc->gadget = gadget; 301 udc->gadget = gadget;
302 gadget->udc = udc;
281 303
282 mutex_lock(&udc_lock); 304 mutex_lock(&udc_lock);
283 list_add_tail(&udc->list, &udc_list); 305 list_add_tail(&udc->list, &udc_list);
@@ -287,6 +309,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
287 goto err4; 309 goto err4;
288 310
289 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); 311 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
312 udc->vbus = true;
290 313
291 mutex_unlock(&udc_lock); 314 mutex_unlock(&udc_lock);
292 315
@@ -348,21 +371,14 @@ static void usb_gadget_remove_driver(struct usb_udc *udc)
348 */ 371 */
349void usb_del_gadget_udc(struct usb_gadget *gadget) 372void usb_del_gadget_udc(struct usb_gadget *gadget)
350{ 373{
351 struct usb_udc *udc = NULL; 374 struct usb_udc *udc = gadget->udc;
352 375
353 mutex_lock(&udc_lock); 376 if (!udc)
354 list_for_each_entry(udc, &udc_list, list) 377 return;
355 if (udc->gadget == gadget)
356 goto found;
357
358 dev_err(gadget->dev.parent, "gadget not registered.\n");
359 mutex_unlock(&udc_lock);
360
361 return;
362 378
363found:
364 dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); 379 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
365 380
381 mutex_lock(&udc_lock);
366 list_del(&udc->list); 382 list_del(&udc->list);
367 mutex_unlock(&udc_lock); 383 mutex_unlock(&udc_lock);
368 384
@@ -397,7 +413,7 @@ static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *dri
397 driver->unbind(udc->gadget); 413 driver->unbind(udc->gadget);
398 goto err1; 414 goto err1;
399 } 415 }
400 usb_gadget_connect(udc->gadget); 416 usb_udc_connect_control(udc);
401 417
402 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 418 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
403 return 0; 419 return 0;
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index 5ad60e46dc2b..197a6a3e613b 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -198,7 +198,7 @@ config USB_EHCI_HCD_AT91
198 198
199config USB_EHCI_MSM 199config USB_EHCI_MSM
200 tristate "Support for Qualcomm QSD/MSM on-chip EHCI USB controller" 200 tristate "Support for Qualcomm QSD/MSM on-chip EHCI USB controller"
201 depends on ARCH_MSM || ARCH_QCOM 201 depends on ARCH_QCOM
202 select USB_EHCI_ROOT_HUB_TT 202 select USB_EHCI_ROOT_HUB_TT
203 ---help--- 203 ---help---
204 Enables support for the USB Host controller present on the 204 Enables support for the USB Host controller present on the
diff --git a/drivers/usb/host/ehci-atmel.c b/drivers/usb/host/ehci-atmel.c
index 663f7908b15c..be0964a801e8 100644
--- a/drivers/usb/host/ehci-atmel.c
+++ b/drivers/usb/host/ehci-atmel.c
@@ -34,7 +34,6 @@ static const char hcd_name[] = "ehci-atmel";
34 34
35struct atmel_ehci_priv { 35struct atmel_ehci_priv {
36 struct clk *iclk; 36 struct clk *iclk;
37 struct clk *fclk;
38 struct clk *uclk; 37 struct clk *uclk;
39 bool clocked; 38 bool clocked;
40}; 39};
@@ -51,12 +50,9 @@ static void atmel_start_clock(struct atmel_ehci_priv *atmel_ehci)
51{ 50{
52 if (atmel_ehci->clocked) 51 if (atmel_ehci->clocked)
53 return; 52 return;
54 if (IS_ENABLED(CONFIG_COMMON_CLK)) { 53
55 clk_set_rate(atmel_ehci->uclk, 48000000); 54 clk_prepare_enable(atmel_ehci->uclk);
56 clk_prepare_enable(atmel_ehci->uclk);
57 }
58 clk_prepare_enable(atmel_ehci->iclk); 55 clk_prepare_enable(atmel_ehci->iclk);
59 clk_prepare_enable(atmel_ehci->fclk);
60 atmel_ehci->clocked = true; 56 atmel_ehci->clocked = true;
61} 57}
62 58
@@ -64,10 +60,9 @@ static void atmel_stop_clock(struct atmel_ehci_priv *atmel_ehci)
64{ 60{
65 if (!atmel_ehci->clocked) 61 if (!atmel_ehci->clocked)
66 return; 62 return;
67 clk_disable_unprepare(atmel_ehci->fclk); 63
68 clk_disable_unprepare(atmel_ehci->iclk); 64 clk_disable_unprepare(atmel_ehci->iclk);
69 if (IS_ENABLED(CONFIG_COMMON_CLK)) 65 clk_disable_unprepare(atmel_ehci->uclk);
70 clk_disable_unprepare(atmel_ehci->uclk);
71 atmel_ehci->clocked = false; 66 atmel_ehci->clocked = false;
72} 67}
73 68
@@ -146,20 +141,13 @@ static int ehci_atmel_drv_probe(struct platform_device *pdev)
146 retval = -ENOENT; 141 retval = -ENOENT;
147 goto fail_request_resource; 142 goto fail_request_resource;
148 } 143 }
149 atmel_ehci->fclk = devm_clk_get(&pdev->dev, "uhpck"); 144
150 if (IS_ERR(atmel_ehci->fclk)) { 145 atmel_ehci->uclk = devm_clk_get(&pdev->dev, "usb_clk");
151 dev_err(&pdev->dev, "Error getting function clock\n"); 146 if (IS_ERR(atmel_ehci->uclk)) {
152 retval = -ENOENT; 147 dev_err(&pdev->dev, "failed to get uclk\n");
148 retval = PTR_ERR(atmel_ehci->uclk);
153 goto fail_request_resource; 149 goto fail_request_resource;
154 } 150 }
155 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
156 atmel_ehci->uclk = devm_clk_get(&pdev->dev, "usb_clk");
157 if (IS_ERR(atmel_ehci->uclk)) {
158 dev_err(&pdev->dev, "failed to get uclk\n");
159 retval = PTR_ERR(atmel_ehci->uclk);
160 goto fail_request_resource;
161 }
162 }
163 151
164 ehci = hcd_to_ehci(hcd); 152 ehci = hcd_to_ehci(hcd);
165 /* registers start at offset 0x0 */ 153 /* registers start at offset 0x0 */
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 85e56d1abd23..f4d88dfb26a7 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -792,12 +792,12 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
792 ehci->reset_done[i] == 0)) 792 ehci->reset_done[i] == 0))
793 continue; 793 continue;
794 794
795 /* start 20 msec resume signaling from this port, 795 /* start USB_RESUME_TIMEOUT msec resume signaling from
796 * and make hub_wq collect PORT_STAT_C_SUSPEND to 796 * this port, and make hub_wq collect
797 * stop that signaling. Use 5 ms extra for safety, 797 * PORT_STAT_C_SUSPEND to stop that signaling.
798 * like usb_port_resume() does.
799 */ 798 */
800 ehci->reset_done[i] = jiffies + msecs_to_jiffies(25); 799 ehci->reset_done[i] = jiffies +
800 msecs_to_jiffies(USB_RESUME_TIMEOUT);
801 set_bit(i, &ehci->resuming_ports); 801 set_bit(i, &ehci->resuming_ports);
802 ehci_dbg (ehci, "port %d remote wakeup\n", i + 1); 802 ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
803 usb_hcd_start_port_resume(&hcd->self, i); 803 usb_hcd_start_port_resume(&hcd->self, i);
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 87cf86f38b36..69208447d213 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -471,10 +471,13 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
471 ehci_writel(ehci, temp, &ehci->regs->port_status [i]); 471 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
472 } 472 }
473 473
474 /* msleep for 20ms only if code is trying to resume port */ 474 /*
475 * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume
476 * port
477 */
475 if (resume_needed) { 478 if (resume_needed) {
476 spin_unlock_irq(&ehci->lock); 479 spin_unlock_irq(&ehci->lock);
477 msleep(20); 480 msleep(USB_RESUME_TIMEOUT);
478 spin_lock_irq(&ehci->lock); 481 spin_lock_irq(&ehci->lock);
479 if (ehci->shutdown) 482 if (ehci->shutdown)
480 goto shutdown; 483 goto shutdown;
@@ -688,7 +691,7 @@ ehci_hub_descriptor (
688 int ports = HCS_N_PORTS (ehci->hcs_params); 691 int ports = HCS_N_PORTS (ehci->hcs_params);
689 u16 temp; 692 u16 temp;
690 693
691 desc->bDescriptorType = 0x29; 694 desc->bDescriptorType = USB_DT_HUB;
692 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */ 695 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
693 desc->bHubContrCurrent = 0; 696 desc->bHubContrCurrent = 0;
694 697
@@ -942,7 +945,7 @@ int ehci_hub_control(
942 temp &= ~PORT_WAKE_BITS; 945 temp &= ~PORT_WAKE_BITS;
943 ehci_writel(ehci, temp | PORT_RESUME, status_reg); 946 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
944 ehci->reset_done[wIndex] = jiffies 947 ehci->reset_done[wIndex] = jiffies
945 + msecs_to_jiffies(20); 948 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
946 set_bit(wIndex, &ehci->resuming_ports); 949 set_bit(wIndex, &ehci->resuming_ports);
947 usb_hcd_start_port_resume(&hcd->self, wIndex); 950 usb_hcd_start_port_resume(&hcd->self, wIndex);
948 break; 951 break;
diff --git a/drivers/usb/host/ehci-orion.c b/drivers/usb/host/ehci-orion.c
index f6eafecab15c..bfcbb9aa8816 100644
--- a/drivers/usb/host/ehci-orion.c
+++ b/drivers/usb/host/ehci-orion.c
@@ -29,7 +29,13 @@
29#define wrl(off, val) writel_relaxed((val), hcd->regs + (off)) 29#define wrl(off, val) writel_relaxed((val), hcd->regs + (off))
30 30
31#define USB_CMD 0x140 31#define USB_CMD 0x140
32#define USB_CMD_RUN BIT(0)
33#define USB_CMD_RESET BIT(1)
32#define USB_MODE 0x1a8 34#define USB_MODE 0x1a8
35#define USB_MODE_MASK GENMASK(1, 0)
36#define USB_MODE_DEVICE 0x2
37#define USB_MODE_HOST 0x3
38#define USB_MODE_SDIS BIT(4)
33#define USB_CAUSE 0x310 39#define USB_CAUSE 0x310
34#define USB_MASK 0x314 40#define USB_MASK 0x314
35#define USB_WINDOW_CTRL(i) (0x320 + ((i) << 4)) 41#define USB_WINDOW_CTRL(i) (0x320 + ((i) << 4))
@@ -69,8 +75,8 @@ static void orion_usb_phy_v1_setup(struct usb_hcd *hcd)
69 /* 75 /*
70 * Reset controller 76 * Reset controller
71 */ 77 */
72 wrl(USB_CMD, rdl(USB_CMD) | 0x2); 78 wrl(USB_CMD, rdl(USB_CMD) | USB_CMD_RESET);
73 while (rdl(USB_CMD) & 0x2); 79 while (rdl(USB_CMD) & USB_CMD_RESET);
74 80
75 /* 81 /*
76 * GL# USB-10: Set IPG for non start of frame packets 82 * GL# USB-10: Set IPG for non start of frame packets
@@ -112,16 +118,16 @@ static void orion_usb_phy_v1_setup(struct usb_hcd *hcd)
112 /* 118 /*
113 * Stop and reset controller 119 * Stop and reset controller
114 */ 120 */
115 wrl(USB_CMD, rdl(USB_CMD) & ~0x1); 121 wrl(USB_CMD, rdl(USB_CMD) & ~USB_CMD_RUN);
116 wrl(USB_CMD, rdl(USB_CMD) | 0x2); 122 wrl(USB_CMD, rdl(USB_CMD) | USB_CMD_RESET);
117 while (rdl(USB_CMD) & 0x2); 123 while (rdl(USB_CMD) & USB_CMD_RESET);
118 124
119 /* 125 /*
120 * GL# USB-5 Streaming disable REG_USB_MODE[4]=1 126 * GL# USB-5 Streaming disable REG_USB_MODE[4]=1
121 * TBD: This need to be done after each reset! 127 * TBD: This need to be done after each reset!
122 * GL# USB-4 Setup USB Host mode 128 * GL# USB-4 Setup USB Host mode
123 */ 129 */
124 wrl(USB_MODE, 0x13); 130 wrl(USB_MODE, USB_MODE_SDIS | USB_MODE_HOST);
125} 131}
126 132
127static void 133static void
diff --git a/drivers/usb/host/fhci-hub.c b/drivers/usb/host/fhci-hub.c
index 70116a65262c..3bacdd7befe9 100644
--- a/drivers/usb/host/fhci-hub.c
+++ b/drivers/usb/host/fhci-hub.c
@@ -30,7 +30,7 @@
30/* virtual root hub specific descriptor */ 30/* virtual root hub specific descriptor */
31static u8 root_hub_des[] = { 31static u8 root_hub_des[] = {
32 0x09, /* blength */ 32 0x09, /* blength */
33 0x29, /* bDescriptorType;hub-descriptor */ 33 USB_DT_HUB, /* bDescriptorType;hub-descriptor */
34 0x01, /* bNbrPorts */ 34 0x01, /* bNbrPorts */
35 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM, /* wHubCharacteristics */ 35 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM, /* wHubCharacteristics */
36 0x00, /* per-port power, no overcurrent */ 36 0x00, /* per-port power, no overcurrent */
diff --git a/drivers/usb/host/fotg210-hcd.c b/drivers/usb/host/fotg210-hcd.c
index 475b21fd373b..000ed80ab592 100644
--- a/drivers/usb/host/fotg210-hcd.c
+++ b/drivers/usb/host/fotg210-hcd.c
@@ -1509,7 +1509,7 @@ fotg210_hub_descriptor(
1509 int ports = HCS_N_PORTS(fotg210->hcs_params); 1509 int ports = HCS_N_PORTS(fotg210->hcs_params);
1510 u16 temp; 1510 u16 temp;
1511 1511
1512 desc->bDescriptorType = 0x29; 1512 desc->bDescriptorType = USB_DT_HUB;
1513 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */ 1513 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1514 desc->bHubContrCurrent = 0; 1514 desc->bHubContrCurrent = 0;
1515 1515
@@ -1595,7 +1595,7 @@ static int fotg210_hub_control(
1595 /* resume signaling for 20 msec */ 1595 /* resume signaling for 20 msec */
1596 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg); 1596 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
1597 fotg210->reset_done[wIndex] = jiffies 1597 fotg210->reset_done[wIndex] = jiffies
1598 + msecs_to_jiffies(20); 1598 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
1599 break; 1599 break;
1600 case USB_PORT_FEAT_C_SUSPEND: 1600 case USB_PORT_FEAT_C_SUSPEND:
1601 clear_bit(wIndex, &fotg210->port_c_suspend); 1601 clear_bit(wIndex, &fotg210->port_c_suspend);
diff --git a/drivers/usb/host/fusbh200-hcd.c b/drivers/usb/host/fusbh200-hcd.c
index a83eefefffda..00e492eaba6a 100644
--- a/drivers/usb/host/fusbh200-hcd.c
+++ b/drivers/usb/host/fusbh200-hcd.c
@@ -1467,7 +1467,7 @@ fusbh200_hub_descriptor (
1467 int ports = HCS_N_PORTS (fusbh200->hcs_params); 1467 int ports = HCS_N_PORTS (fusbh200->hcs_params);
1468 u16 temp; 1468 u16 temp;
1469 1469
1470 desc->bDescriptorType = 0x29; 1470 desc->bDescriptorType = USB_DT_HUB;
1471 desc->bPwrOn2PwrGood = 10; /* fusbh200 1.0, 2.3.9 says 20ms max */ 1471 desc->bPwrOn2PwrGood = 10; /* fusbh200 1.0, 2.3.9 says 20ms max */
1472 desc->bHubContrCurrent = 0; 1472 desc->bHubContrCurrent = 0;
1473 1473
@@ -1550,10 +1550,9 @@ static int fusbh200_hub_control (
1550 if ((temp & PORT_PE) == 0) 1550 if ((temp & PORT_PE) == 0)
1551 goto error; 1551 goto error;
1552 1552
1553 /* resume signaling for 20 msec */
1554 fusbh200_writel(fusbh200, temp | PORT_RESUME, status_reg); 1553 fusbh200_writel(fusbh200, temp | PORT_RESUME, status_reg);
1555 fusbh200->reset_done[wIndex] = jiffies 1554 fusbh200->reset_done[wIndex] = jiffies
1556 + msecs_to_jiffies(20); 1555 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
1557 break; 1556 break;
1558 case USB_PORT_FEAT_C_SUSPEND: 1557 case USB_PORT_FEAT_C_SUSPEND:
1559 clear_bit(wIndex, &fusbh200->port_c_suspend); 1558 clear_bit(wIndex, &fusbh200->port_c_suspend);
diff --git a/drivers/usb/host/imx21-hcd.c b/drivers/usb/host/imx21-hcd.c
index 6a2ad550b120..f542045dc2a6 100644
--- a/drivers/usb/host/imx21-hcd.c
+++ b/drivers/usb/host/imx21-hcd.c
@@ -1474,7 +1474,7 @@ static int get_hub_descriptor(struct usb_hcd *hcd,
1474 struct usb_hub_descriptor *desc) 1474 struct usb_hub_descriptor *desc)
1475{ 1475{
1476 struct imx21 *imx21 = hcd_to_imx21(hcd); 1476 struct imx21 *imx21 = hcd_to_imx21(hcd);
1477 desc->bDescriptorType = 0x29; /* HUB descriptor */ 1477 desc->bDescriptorType = USB_DT_HUB; /* HUB descriptor */
1478 desc->bHubContrCurrent = 0; 1478 desc->bHubContrCurrent = 0;
1479 1479
1480 desc->bNbrPorts = readl(imx21->regs + USBH_ROOTHUBA) 1480 desc->bNbrPorts = readl(imx21->regs + USBH_ROOTHUBA)
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index 113d0cc6cc43..13181dcd9820 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -943,7 +943,7 @@ static void isp116x_hub_descriptor(struct isp116x *isp116x,
943{ 943{
944 u32 reg = isp116x->rhdesca; 944 u32 reg = isp116x->rhdesca;
945 945
946 desc->bDescriptorType = 0x29; 946 desc->bDescriptorType = USB_DT_HUB;
947 desc->bDescLength = 9; 947 desc->bDescLength = 9;
948 desc->bHubContrCurrent = 0; 948 desc->bHubContrCurrent = 0;
949 desc->bNbrPorts = (u8) (reg & 0x3); 949 desc->bNbrPorts = (u8) (reg & 0x3);
@@ -1490,7 +1490,7 @@ static int isp116x_bus_resume(struct usb_hcd *hcd)
1490 spin_unlock_irq(&isp116x->lock); 1490 spin_unlock_irq(&isp116x->lock);
1491 1491
1492 hcd->state = HC_STATE_RESUMING; 1492 hcd->state = HC_STATE_RESUMING;
1493 msleep(20); 1493 msleep(USB_RESUME_TIMEOUT);
1494 1494
1495 /* Go operational */ 1495 /* Go operational */
1496 spin_lock_irq(&isp116x->lock); 1496 spin_lock_irq(&isp116x->lock);
diff --git a/drivers/usb/host/isp1362-hcd.c b/drivers/usb/host/isp1362-hcd.c
index b32ab60cad1e..6cf82ee460a6 100644
--- a/drivers/usb/host/isp1362-hcd.c
+++ b/drivers/usb/host/isp1362-hcd.c
@@ -1538,7 +1538,7 @@ static void isp1362_hub_descriptor(struct isp1362_hcd *isp1362_hcd,
1538 1538
1539 DBG(3, "%s: enter\n", __func__); 1539 DBG(3, "%s: enter\n", __func__);
1540 1540
1541 desc->bDescriptorType = 0x29; 1541 desc->bDescriptorType = USB_DT_HUB;
1542 desc->bDescLength = 9; 1542 desc->bDescLength = 9;
1543 desc->bHubContrCurrent = 0; 1543 desc->bHubContrCurrent = 0;
1544 desc->bNbrPorts = reg & 0x3; 1544 desc->bNbrPorts = reg & 0x3;
diff --git a/drivers/usb/host/max3421-hcd.c b/drivers/usb/host/max3421-hcd.c
index a98833cbfcf3..fc1fd403973a 100644
--- a/drivers/usb/host/max3421-hcd.c
+++ b/drivers/usb/host/max3421-hcd.c
@@ -1659,7 +1659,7 @@ hub_descriptor(struct usb_hub_descriptor *desc)
1659 /* 1659 /*
1660 * See Table 11-13: Hub Descriptor in USB 2.0 spec. 1660 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1661 */ 1661 */
1662 desc->bDescriptorType = 0x29; /* hub descriptor */ 1662 desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */
1663 desc->bDescLength = 9; 1663 desc->bDescLength = 9;
1664 desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | 1664 desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1665 HUB_CHAR_COMMON_OCPM); 1665 HUB_CHAR_COMMON_OCPM);
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
index 7cce85a1f7dc..15df00cceed9 100644
--- a/drivers/usb/host/ohci-at91.c
+++ b/drivers/usb/host/ohci-at91.c
@@ -39,7 +39,6 @@
39struct ohci_at91_priv { 39struct ohci_at91_priv {
40 struct clk *iclk; 40 struct clk *iclk;
41 struct clk *fclk; 41 struct clk *fclk;
42 struct clk *uclk;
43 struct clk *hclk; 42 struct clk *hclk;
44 bool clocked; 43 bool clocked;
45 bool wakeup; /* Saved wake-up state for resume */ 44 bool wakeup; /* Saved wake-up state for resume */
@@ -64,10 +63,8 @@ static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
64{ 63{
65 if (ohci_at91->clocked) 64 if (ohci_at91->clocked)
66 return; 65 return;
67 if (IS_ENABLED(CONFIG_COMMON_CLK)) { 66
68 clk_set_rate(ohci_at91->uclk, 48000000); 67 clk_set_rate(ohci_at91->fclk, 48000000);
69 clk_prepare_enable(ohci_at91->uclk);
70 }
71 clk_prepare_enable(ohci_at91->hclk); 68 clk_prepare_enable(ohci_at91->hclk);
72 clk_prepare_enable(ohci_at91->iclk); 69 clk_prepare_enable(ohci_at91->iclk);
73 clk_prepare_enable(ohci_at91->fclk); 70 clk_prepare_enable(ohci_at91->fclk);
@@ -78,11 +75,10 @@ static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
78{ 75{
79 if (!ohci_at91->clocked) 76 if (!ohci_at91->clocked)
80 return; 77 return;
78
81 clk_disable_unprepare(ohci_at91->fclk); 79 clk_disable_unprepare(ohci_at91->fclk);
82 clk_disable_unprepare(ohci_at91->iclk); 80 clk_disable_unprepare(ohci_at91->iclk);
83 clk_disable_unprepare(ohci_at91->hclk); 81 clk_disable_unprepare(ohci_at91->hclk);
84 if (IS_ENABLED(CONFIG_COMMON_CLK))
85 clk_disable_unprepare(ohci_at91->uclk);
86 ohci_at91->clocked = false; 82 ohci_at91->clocked = false;
87} 83}
88 84
@@ -191,14 +187,6 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
191 retval = PTR_ERR(ohci_at91->hclk); 187 retval = PTR_ERR(ohci_at91->hclk);
192 goto err; 188 goto err;
193 } 189 }
194 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
195 ohci_at91->uclk = devm_clk_get(dev, "usb_clk");
196 if (IS_ERR(ohci_at91->uclk)) {
197 dev_err(dev, "failed to get uclk\n");
198 retval = PTR_ERR(ohci_at91->uclk);
199 goto err;
200 }
201 }
202 190
203 board = hcd->self.controller->platform_data; 191 board = hcd->self.controller->platform_data;
204 ohci = hcd_to_ohci(hcd); 192 ohci = hcd_to_ohci(hcd);
diff --git a/drivers/usb/host/ohci-hub.c b/drivers/usb/host/ohci-hub.c
index fe2aedd8a54d..ed678c17c4ea 100644
--- a/drivers/usb/host/ohci-hub.c
+++ b/drivers/usb/host/ohci-hub.c
@@ -536,7 +536,7 @@ ohci_hub_descriptor (
536 u32 rh = roothub_a (ohci); 536 u32 rh = roothub_a (ohci);
537 u16 temp; 537 u16 temp;
538 538
539 desc->bDescriptorType = 0x29; 539 desc->bDescriptorType = USB_DT_HUB;
540 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24; 540 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
541 desc->bHubContrCurrent = 0; 541 desc->bHubContrCurrent = 0;
542 542
diff --git a/drivers/usb/host/oxu210hp-hcd.c b/drivers/usb/host/oxu210hp-hcd.c
index ef7efb278b15..6352f54e65a1 100644
--- a/drivers/usb/host/oxu210hp-hcd.c
+++ b/drivers/usb/host/oxu210hp-hcd.c
@@ -445,7 +445,7 @@ static void ehci_hub_descriptor(struct oxu_hcd *oxu,
445 int ports = HCS_N_PORTS(oxu->hcs_params); 445 int ports = HCS_N_PORTS(oxu->hcs_params);
446 u16 temp; 446 u16 temp;
447 447
448 desc->bDescriptorType = 0x29; 448 desc->bDescriptorType = USB_DT_HUB;
449 desc->bPwrOn2PwrGood = 10; /* oxu 1.0, 2.3.9 says 20ms max */ 449 desc->bPwrOn2PwrGood = 10; /* oxu 1.0, 2.3.9 says 20ms max */
450 desc->bHubContrCurrent = 0; 450 desc->bHubContrCurrent = 0;
451 451
@@ -2500,11 +2500,12 @@ static irqreturn_t oxu210_hcd_irq(struct usb_hcd *hcd)
2500 || oxu->reset_done[i] != 0) 2500 || oxu->reset_done[i] != 0)
2501 continue; 2501 continue;
2502 2502
2503 /* start 20 msec resume signaling from this port, 2503 /* start USB_RESUME_TIMEOUT resume signaling from this
2504 * and make hub_wq collect PORT_STAT_C_SUSPEND to 2504 * port, and make hub_wq collect PORT_STAT_C_SUSPEND to
2505 * stop that signaling. 2505 * stop that signaling.
2506 */ 2506 */
2507 oxu->reset_done[i] = jiffies + msecs_to_jiffies(20); 2507 oxu->reset_done[i] = jiffies +
2508 msecs_to_jiffies(USB_RESUME_TIMEOUT);
2508 oxu_dbg(oxu, "port %d remote wakeup\n", i + 1); 2509 oxu_dbg(oxu, "port %d remote wakeup\n", i + 1);
2509 mod_timer(&hcd->rh_timer, oxu->reset_done[i]); 2510 mod_timer(&hcd->rh_timer, oxu->reset_done[i]);
2510 } 2511 }
diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
index bdc82fea0a1f..4cbd0633c5c2 100644
--- a/drivers/usb/host/r8a66597-hcd.c
+++ b/drivers/usb/host/r8a66597-hcd.c
@@ -2136,7 +2136,7 @@ static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2136static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597, 2136static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2137 struct usb_hub_descriptor *desc) 2137 struct usb_hub_descriptor *desc)
2138{ 2138{
2139 desc->bDescriptorType = 0x29; 2139 desc->bDescriptorType = USB_DT_HUB;
2140 desc->bHubContrCurrent = 0; 2140 desc->bHubContrCurrent = 0;
2141 desc->bNbrPorts = r8a66597->max_root_hub; 2141 desc->bNbrPorts = r8a66597->max_root_hub;
2142 desc->bDescLength = 9; 2142 desc->bDescLength = 9;
@@ -2301,7 +2301,7 @@ static int r8a66597_bus_resume(struct usb_hcd *hcd)
2301 rh->port &= ~USB_PORT_STAT_SUSPEND; 2301 rh->port &= ~USB_PORT_STAT_SUSPEND;
2302 rh->port |= USB_PORT_STAT_C_SUSPEND << 16; 2302 rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
2303 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg); 2303 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2304 msleep(50); 2304 msleep(USB_RESUME_TIMEOUT);
2305 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg); 2305 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2306 } 2306 }
2307 2307
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index 4f4ba1ea9e9b..fd2a11473be7 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -1091,7 +1091,7 @@ sl811h_hub_descriptor (
1091) { 1091) {
1092 u16 temp = 0; 1092 u16 temp = 0;
1093 1093
1094 desc->bDescriptorType = 0x29; 1094 desc->bDescriptorType = USB_DT_HUB;
1095 desc->bHubContrCurrent = 0; 1095 desc->bHubContrCurrent = 0;
1096 1096
1097 desc->bNbrPorts = 1; 1097 desc->bNbrPorts = 1;
@@ -1259,7 +1259,7 @@ sl811h_hub_control(
1259 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1259 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1260 1260
1261 mod_timer(&sl811->timer, jiffies 1261 mod_timer(&sl811->timer, jiffies
1262 + msecs_to_jiffies(20)); 1262 + msecs_to_jiffies(USB_RESUME_TIMEOUT));
1263 break; 1263 break;
1264 case USB_PORT_FEAT_POWER: 1264 case USB_PORT_FEAT_POWER:
1265 port_power(sl811, 0); 1265 port_power(sl811, 0);
@@ -1809,7 +1809,6 @@ struct platform_driver sl811h_driver = {
1809 .resume = sl811h_resume, 1809 .resume = sl811h_resume,
1810 .driver = { 1810 .driver = {
1811 .name = (char *) hcd_name, 1811 .name = (char *) hcd_name,
1812 .owner = THIS_MODULE,
1813 }, 1812 },
1814}; 1813};
1815EXPORT_SYMBOL(sl811h_driver); 1814EXPORT_SYMBOL(sl811h_driver);
diff --git a/drivers/usb/host/u132-hcd.c b/drivers/usb/host/u132-hcd.c
index ad97e8a1ad1c..d51687780b61 100644
--- a/drivers/usb/host/u132-hcd.c
+++ b/drivers/usb/host/u132-hcd.c
@@ -2584,7 +2584,7 @@ static int u132_roothub_descriptor(struct u132 *u132,
2584 retval = u132_read_pcimem(u132, roothub.a, &rh_a); 2584 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2585 if (retval) 2585 if (retval)
2586 return retval; 2586 return retval;
2587 desc->bDescriptorType = 0x29; 2587 desc->bDescriptorType = USB_DT_HUB;
2588 desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24; 2588 desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2589 desc->bHubContrCurrent = 0; 2589 desc->bHubContrCurrent = 0;
2590 desc->bNbrPorts = u132->num_ports; 2590 desc->bNbrPorts = u132->num_ports;
diff --git a/drivers/usb/host/uhci-hub.c b/drivers/usb/host/uhci-hub.c
index 19ba5eafb31e..ece9e37e89fe 100644
--- a/drivers/usb/host/uhci-hub.c
+++ b/drivers/usb/host/uhci-hub.c
@@ -15,7 +15,7 @@
15static const __u8 root_hub_hub_des[] = 15static const __u8 root_hub_hub_des[] =
16{ 16{
17 0x09, /* __u8 bLength; */ 17 0x09, /* __u8 bLength; */
18 0x29, /* __u8 bDescriptorType; Hub-descriptor */ 18 USB_DT_HUB, /* __u8 bDescriptorType; Hub-descriptor */
19 0x02, /* __u8 bNbrPorts; */ 19 0x02, /* __u8 bNbrPorts; */
20 HUB_CHAR_NO_LPSM | /* __u16 wHubCharacteristics; */ 20 HUB_CHAR_NO_LPSM | /* __u16 wHubCharacteristics; */
21 HUB_CHAR_INDV_PORT_OCPM, /* (per-port OC, no power switching) */ 21 HUB_CHAR_INDV_PORT_OCPM, /* (per-port OC, no power switching) */
@@ -166,7 +166,7 @@ static void uhci_check_ports(struct uhci_hcd *uhci)
166 /* Port received a wakeup request */ 166 /* Port received a wakeup request */
167 set_bit(port, &uhci->resuming_ports); 167 set_bit(port, &uhci->resuming_ports);
168 uhci->ports_timeout = jiffies + 168 uhci->ports_timeout = jiffies +
169 msecs_to_jiffies(25); 169 msecs_to_jiffies(USB_RESUME_TIMEOUT);
170 usb_hcd_start_port_resume( 170 usb_hcd_start_port_resume(
171 &uhci_to_hcd(uhci)->self, port); 171 &uhci_to_hcd(uhci)->self, port);
172 172
@@ -338,7 +338,8 @@ static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
338 uhci_finish_suspend(uhci, port, port_addr); 338 uhci_finish_suspend(uhci, port, port_addr);
339 339
340 /* USB v2.0 7.1.7.5 */ 340 /* USB v2.0 7.1.7.5 */
341 uhci->ports_timeout = jiffies + msecs_to_jiffies(50); 341 uhci->ports_timeout = jiffies +
342 msecs_to_jiffies(USB_RESUME_TIMEOUT);
342 break; 343 break;
343 case USB_PORT_FEAT_POWER: 344 case USB_PORT_FEAT_POWER:
344 /* UHCI has no power switching */ 345 /* UHCI has no power switching */
diff --git a/drivers/usb/host/whci/hcd.c b/drivers/usb/host/whci/hcd.c
index d7b363a418de..43626c44683b 100644
--- a/drivers/usb/host/whci/hcd.c
+++ b/drivers/usb/host/whci/hcd.c
@@ -313,8 +313,7 @@ error_wusbhc_create:
313 uwb_rc_put(wusbhc->uwb_rc); 313 uwb_rc_put(wusbhc->uwb_rc);
314error: 314error:
315 whc_clean_up(whc); 315 whc_clean_up(whc);
316 if (usb_hcd) 316 usb_put_hcd(usb_hcd);
317 usb_put_hcd(usb_hcd);
318 return ret; 317 return ret;
319} 318}
320 319
diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index a7865c4b0498..0827d7c96527 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -387,6 +387,10 @@ static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
387 status = PORT_PLC; 387 status = PORT_PLC;
388 port_change_bit = "link state"; 388 port_change_bit = "link state";
389 break; 389 break;
390 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
391 status = PORT_CEC;
392 port_change_bit = "config error";
393 break;
390 default: 394 default:
391 /* Should never happen */ 395 /* Should never happen */
392 return; 396 return;
@@ -588,6 +592,8 @@ static u32 xhci_get_port_status(struct usb_hcd *hcd,
588 status |= USB_PORT_STAT_C_LINK_STATE << 16; 592 status |= USB_PORT_STAT_C_LINK_STATE << 16;
589 if ((raw_port_status & PORT_WRC)) 593 if ((raw_port_status & PORT_WRC))
590 status |= USB_PORT_STAT_C_BH_RESET << 16; 594 status |= USB_PORT_STAT_C_BH_RESET << 16;
595 if ((raw_port_status & PORT_CEC))
596 status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
591 } 597 }
592 598
593 if (hcd->speed != HCD_USB3) { 599 if (hcd->speed != HCD_USB3) {
@@ -1005,6 +1011,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1005 case USB_PORT_FEAT_C_OVER_CURRENT: 1011 case USB_PORT_FEAT_C_OVER_CURRENT:
1006 case USB_PORT_FEAT_C_ENABLE: 1012 case USB_PORT_FEAT_C_ENABLE:
1007 case USB_PORT_FEAT_C_PORT_LINK_STATE: 1013 case USB_PORT_FEAT_C_PORT_LINK_STATE:
1014 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
1008 xhci_clear_port_change_bit(xhci, wValue, wIndex, 1015 xhci_clear_port_change_bit(xhci, wValue, wIndex,
1009 port_array[wIndex], temp); 1016 port_array[wIndex], temp);
1010 break; 1017 break;
@@ -1069,7 +1076,7 @@ int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
1069 */ 1076 */
1070 status = bus_state->resuming_ports; 1077 status = bus_state->resuming_ports;
1071 1078
1072 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC; 1079 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
1073 1080
1074 spin_lock_irqsave(&xhci->lock, flags); 1081 spin_lock_irqsave(&xhci->lock, flags);
1075 /* For each port, did anything change? If so, set that bit in buf. */ 1082 /* For each port, did anything change? If so, set that bit in buf. */
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index fd53c9ebd662..2af32e26fafc 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -115,6 +115,7 @@ static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
115 if (pdev->vendor == PCI_VENDOR_ID_INTEL) { 115 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
116 xhci->quirks |= XHCI_LPM_SUPPORT; 116 xhci->quirks |= XHCI_LPM_SUPPORT;
117 xhci->quirks |= XHCI_INTEL_HOST; 117 xhci->quirks |= XHCI_INTEL_HOST;
118 xhci->quirks |= XHCI_AVOID_BEI;
118 } 119 }
119 if (pdev->vendor == PCI_VENDOR_ID_INTEL && 120 if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
120 pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI) { 121 pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI) {
@@ -130,7 +131,6 @@ static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
130 * PPT chipsets. 131 * PPT chipsets.
131 */ 132 */
132 xhci->quirks |= XHCI_SPURIOUS_REBOOT; 133 xhci->quirks |= XHCI_SPURIOUS_REBOOT;
133 xhci->quirks |= XHCI_AVOID_BEI;
134 } 134 }
135 if (pdev->vendor == PCI_VENDOR_ID_INTEL && 135 if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
136 pdev->device == PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI) { 136 pdev->device == PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI) {
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 0e11d61408ff..783e819139a7 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -16,6 +16,7 @@
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/of.h> 17#include <linux/of.h>
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19#include <linux/usb/phy.h>
19#include <linux/slab.h> 20#include <linux/slab.h>
20#include <linux/usb/xhci_pdriver.h> 21#include <linux/usb/xhci_pdriver.h>
21 22
@@ -155,12 +156,27 @@ static int xhci_plat_probe(struct platform_device *pdev)
155 if (HCC_MAX_PSA(xhci->hcc_params) >= 4) 156 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
156 xhci->shared_hcd->can_do_streams = 1; 157 xhci->shared_hcd->can_do_streams = 1;
157 158
159 hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
160 if (IS_ERR(hcd->usb_phy)) {
161 ret = PTR_ERR(hcd->usb_phy);
162 if (ret == -EPROBE_DEFER)
163 goto put_usb3_hcd;
164 hcd->usb_phy = NULL;
165 } else {
166 ret = usb_phy_init(hcd->usb_phy);
167 if (ret)
168 goto put_usb3_hcd;
169 }
170
158 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); 171 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
159 if (ret) 172 if (ret)
160 goto put_usb3_hcd; 173 goto disable_usb_phy;
161 174
162 return 0; 175 return 0;
163 176
177disable_usb_phy:
178 usb_phy_shutdown(hcd->usb_phy);
179
164put_usb3_hcd: 180put_usb3_hcd:
165 usb_put_hcd(xhci->shared_hcd); 181 usb_put_hcd(xhci->shared_hcd);
166 182
@@ -184,6 +200,7 @@ static int xhci_plat_remove(struct platform_device *dev)
184 struct clk *clk = xhci->clk; 200 struct clk *clk = xhci->clk;
185 201
186 usb_remove_hcd(xhci->shared_hcd); 202 usb_remove_hcd(xhci->shared_hcd);
203 usb_phy_shutdown(hcd->usb_phy);
187 usb_put_hcd(xhci->shared_hcd); 204 usb_put_hcd(xhci->shared_hcd);
188 205
189 usb_remove_hcd(hcd); 206 usb_remove_hcd(hcd);
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 5fb66db89e05..f5397a517c54 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -238,7 +238,7 @@ static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
238 238
239 /* Toggle the cycle bit after the last ring segment. */ 239 /* Toggle the cycle bit after the last ring segment. */
240 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { 240 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
241 ring->cycle_state = (ring->cycle_state ? 0 : 1); 241 ring->cycle_state ^= 1;
242 } 242 }
243 } 243 }
244 ring->enq_seg = ring->enq_seg->next; 244 ring->enq_seg = ring->enq_seg->next;
@@ -1574,7 +1574,7 @@ static void handle_port_status(struct xhci_hcd *xhci,
1574 } else { 1574 } else {
1575 xhci_dbg(xhci, "resume HS port %d\n", port_id); 1575 xhci_dbg(xhci, "resume HS port %d\n", port_id);
1576 bus_state->resume_done[faked_port_index] = jiffies + 1576 bus_state->resume_done[faked_port_index] = jiffies +
1577 msecs_to_jiffies(20); 1577 msecs_to_jiffies(USB_RESUME_TIMEOUT);
1578 set_bit(faked_port_index, &bus_state->resuming_ports); 1578 set_bit(faked_port_index, &bus_state->resuming_ports);
1579 mod_timer(&hcd->rh_timer, 1579 mod_timer(&hcd->rh_timer,
1580 bus_state->resume_done[faked_port_index]); 1580 bus_state->resume_done[faked_port_index]);
@@ -1729,7 +1729,7 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1729 if (!command) 1729 if (!command)
1730 return; 1730 return;
1731 1731
1732 ep->ep_state |= EP_HALTED | EP_RECENTLY_HALTED; 1732 ep->ep_state |= EP_HALTED;
1733 ep->stopped_stream = stream_id; 1733 ep->stopped_stream = stream_id;
1734 1734
1735 xhci_queue_reset_ep(xhci, command, slot_id, ep_index); 1735 xhci_queue_reset_ep(xhci, command, slot_id, ep_index);
@@ -2809,7 +2809,7 @@ static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2809 2809
2810 /* Toggle the cycle bit after the last ring segment. */ 2810 /* Toggle the cycle bit after the last ring segment. */
2811 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { 2811 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2812 ring->cycle_state = (ring->cycle_state ? 0 : 1); 2812 ring->cycle_state ^= 1;
2813 } 2813 }
2814 ring->enq_seg = ring->enq_seg->next; 2814 ring->enq_seg = ring->enq_seg->next;
2815 ring->enqueue = ring->enq_seg->trbs; 2815 ring->enqueue = ring->enq_seg->trbs;
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index b06d1a53652d..ec8ac1674854 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1338,12 +1338,6 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1338 goto exit; 1338 goto exit;
1339 } 1339 }
1340 1340
1341 /* Reject urb if endpoint is in soft reset, queue must stay empty */
1342 if (xhci->devs[slot_id]->eps[ep_index].ep_state & EP_CONFIG_PENDING) {
1343 xhci_warn(xhci, "Can't enqueue URB while ep is in soft reset\n");
1344 ret = -EINVAL;
1345 }
1346
1347 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) 1341 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1348 size = urb->number_of_packets; 1342 size = urb->number_of_packets;
1349 else 1343 else
@@ -2954,36 +2948,23 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
2954 } 2948 }
2955} 2949}
2956 2950
2957/* Called after clearing a halted device. USB core should have sent the control 2951/* Called when clearing halted device. The core should have sent the control
2958 * message to clear the device halt condition. The host side of the halt should 2952 * message to clear the device halt condition. The host side of the halt should
2959 * already be cleared with a reset endpoint command issued immediately when the 2953 * already be cleared with a reset endpoint command issued when the STALL tx
2960 * STALL tx event was received. 2954 * event was received.
2955 *
2956 * Context: in_interrupt
2961 */ 2957 */
2962 2958
2963void xhci_endpoint_reset(struct usb_hcd *hcd, 2959void xhci_endpoint_reset(struct usb_hcd *hcd,
2964 struct usb_host_endpoint *ep) 2960 struct usb_host_endpoint *ep)
2965{ 2961{
2966 struct xhci_hcd *xhci; 2962 struct xhci_hcd *xhci;
2967 struct usb_device *udev;
2968 struct xhci_virt_device *virt_dev;
2969 struct xhci_virt_ep *virt_ep;
2970 struct xhci_input_control_ctx *ctrl_ctx;
2971 struct xhci_command *command;
2972 unsigned int ep_index, ep_state;
2973 unsigned long flags;
2974 u32 ep_flag;
2975 2963
2976 xhci = hcd_to_xhci(hcd); 2964 xhci = hcd_to_xhci(hcd);
2977 udev = (struct usb_device *) ep->hcpriv;
2978 if (!ep->hcpriv)
2979 return;
2980 virt_dev = xhci->devs[udev->slot_id];
2981 ep_index = xhci_get_endpoint_index(&ep->desc);
2982 virt_ep = &virt_dev->eps[ep_index];
2983 ep_state = virt_ep->ep_state;
2984 2965
2985 /* 2966 /*
2986 * Implement the config ep command in xhci 4.6.8 additional note: 2967 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2987 * The Reset Endpoint Command may only be issued to endpoints in the 2968 * The Reset Endpoint Command may only be issued to endpoints in the
2988 * Halted state. If software wishes reset the Data Toggle or Sequence 2969 * Halted state. If software wishes reset the Data Toggle or Sequence
2989 * Number of an endpoint that isn't in the Halted state, then software 2970 * Number of an endpoint that isn't in the Halted state, then software
@@ -2991,72 +2972,9 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
2991 * for the target endpoint. that is in the Stopped state. 2972 * for the target endpoint. that is in the Stopped state.
2992 */ 2973 */
2993 2974
2994 if (ep_state & SET_DEQ_PENDING || ep_state & EP_RECENTLY_HALTED) { 2975 /* For now just print debug to follow the situation */
2995 virt_ep->ep_state &= ~EP_RECENTLY_HALTED; 2976 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2996 xhci_dbg(xhci, "ep recently halted, no toggle reset needed\n"); 2977 ep->desc.bEndpointAddress);
2997 return;
2998 }
2999
3000 /* Only interrupt and bulk ep's use Data toggle, USB2 spec 5.5.4-> */
3001 if (usb_endpoint_xfer_control(&ep->desc) ||
3002 usb_endpoint_xfer_isoc(&ep->desc))
3003 return;
3004
3005 ep_flag = xhci_get_endpoint_flag(&ep->desc);
3006
3007 if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
3008 return;
3009
3010 command = xhci_alloc_command(xhci, true, true, GFP_NOWAIT);
3011 if (!command) {
3012 xhci_err(xhci, "Could not allocate xHCI command structure.\n");
3013 return;
3014 }
3015
3016 spin_lock_irqsave(&xhci->lock, flags);
3017
3018 /* block ringing ep doorbell */
3019 virt_ep->ep_state |= EP_CONFIG_PENDING;
3020
3021 /*
3022 * Make sure endpoint ring is empty before resetting the toggle/seq.
3023 * Driver is required to synchronously cancel all transfer request.
3024 *
3025 * xhci 4.6.6 says we can issue a configure endpoint command on a
3026 * running endpoint ring as long as it's idle (queue empty)
3027 */
3028
3029 if (!list_empty(&virt_ep->ring->td_list)) {
3030 dev_err(&udev->dev, "EP not empty, refuse reset\n");
3031 spin_unlock_irqrestore(&xhci->lock, flags);
3032 goto cleanup;
3033 }
3034
3035 xhci_dbg(xhci, "Reset toggle/seq for slot %d, ep_index: %d\n",
3036 udev->slot_id, ep_index);
3037
3038 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
3039 if (!ctrl_ctx) {
3040 xhci_err(xhci, "Could not get input context, bad type. virt_dev: %p, in_ctx %p\n",
3041 virt_dev, virt_dev->in_ctx);
3042 spin_unlock_irqrestore(&xhci->lock, flags);
3043 goto cleanup;
3044 }
3045 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3046 virt_dev->out_ctx, ctrl_ctx,
3047 ep_flag, ep_flag);
3048 xhci_endpoint_copy(xhci, command->in_ctx, virt_dev->out_ctx, ep_index);
3049
3050 xhci_queue_configure_endpoint(xhci, command, command->in_ctx->dma,
3051 udev->slot_id, false);
3052 xhci_ring_cmd_db(xhci);
3053 spin_unlock_irqrestore(&xhci->lock, flags);
3054
3055 wait_for_completion(command->completion);
3056
3057cleanup:
3058 virt_ep->ep_state &= ~EP_CONFIG_PENDING;
3059 xhci_free_command(xhci, command);
3060} 2978}
3061 2979
3062static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, 2980static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 265ab1771d24..8e421b89632d 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -865,8 +865,6 @@ struct xhci_virt_ep {
865#define EP_HAS_STREAMS (1 << 4) 865#define EP_HAS_STREAMS (1 << 4)
866/* Transitioning the endpoint to not using streams, don't enqueue URBs */ 866/* Transitioning the endpoint to not using streams, don't enqueue URBs */
867#define EP_GETTING_NO_STREAMS (1 << 5) 867#define EP_GETTING_NO_STREAMS (1 << 5)
868#define EP_RECENTLY_HALTED (1 << 6)
869#define EP_CONFIG_PENDING (1 << 7)
870 /* ---- Related to URB cancellation ---- */ 868 /* ---- Related to URB cancellation ---- */
871 struct list_head cancelled_td_list; 869 struct list_head cancelled_td_list;
872 struct xhci_td *stopped_td; 870 struct xhci_td *stopped_td;
diff --git a/drivers/usb/image/mdc800.c b/drivers/usb/image/mdc800.c
index a62865af53cc..5cf2633cdb04 100644
--- a/drivers/usb/image/mdc800.c
+++ b/drivers/usb/image/mdc800.c
@@ -347,7 +347,8 @@ static int mdc800_usb_waitForIRQ (int mode, int msec)
347{ 347{
348 mdc800->camera_request_ready=1+mode; 348 mdc800->camera_request_ready=1+mode;
349 349
350 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000); 350 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken,
351 msecs_to_jiffies(msec));
351 mdc800->irq_woken = 0; 352 mdc800->irq_woken = 0;
352 353
353 if (mdc800->camera_request_ready>0) 354 if (mdc800->camera_request_ready>0)
@@ -743,8 +744,9 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
743 mutex_unlock(&mdc800->io_lock); 744 mutex_unlock(&mdc800->io_lock);
744 return len-left; 745 return len-left;
745 } 746 }
746 wait_event_timeout(mdc800->download_wait, mdc800->downloaded, 747 wait_event_timeout(mdc800->download_wait,
747 TO_DOWNLOAD_GET_READY*HZ/1000); 748 mdc800->downloaded,
749 msecs_to_jiffies(TO_DOWNLOAD_GET_READY));
748 mdc800->downloaded = 0; 750 mdc800->downloaded = 0;
749 if (mdc800->download_urb->status != 0) 751 if (mdc800->download_urb->status != 0)
750 { 752 {
@@ -867,7 +869,8 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
867 mutex_unlock(&mdc800->io_lock); 869 mutex_unlock(&mdc800->io_lock);
868 return -EIO; 870 return -EIO;
869 } 871 }
870 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000); 872 wait_event_timeout(mdc800->write_wait, mdc800->written,
873 msecs_to_jiffies(TO_WRITE_GET_READY));
871 mdc800->written = 0; 874 mdc800->written = 0;
872 if (mdc800->state == WORKING) 875 if (mdc800->state == WORKING)
873 { 876 {
diff --git a/drivers/usb/isp1760/isp1760-core.c b/drivers/usb/isp1760/isp1760-core.c
index b9827556455f..bfa402cf3a27 100644
--- a/drivers/usb/isp1760/isp1760-core.c
+++ b/drivers/usb/isp1760/isp1760-core.c
@@ -151,8 +151,7 @@ int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
151 } 151 }
152 152
153 if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) { 153 if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) {
154 ret = isp1760_udc_register(isp, irq, irqflags | IRQF_SHARED | 154 ret = isp1760_udc_register(isp, irq, irqflags);
155 IRQF_DISABLED);
156 if (ret < 0) { 155 if (ret < 0) {
157 isp1760_hcd_unregister(&isp->hcd); 156 isp1760_hcd_unregister(&isp->hcd);
158 return ret; 157 return ret;
diff --git a/drivers/usb/isp1760/isp1760-hcd.c b/drivers/usb/isp1760/isp1760-hcd.c
index 3cb98b1d5d29..ac31d19cc54b 100644
--- a/drivers/usb/isp1760/isp1760-hcd.c
+++ b/drivers/usb/isp1760/isp1760-hcd.c
@@ -1758,7 +1758,7 @@ static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1758 int ports = HCS_N_PORTS(priv->hcs_params); 1758 int ports = HCS_N_PORTS(priv->hcs_params);
1759 u16 temp; 1759 u16 temp;
1760 1760
1761 desc->bDescriptorType = 0x29; 1761 desc->bDescriptorType = USB_DT_HUB;
1762 /* priv 1.0, 2.3.9 says 20ms max */ 1762 /* priv 1.0, 2.3.9 says 20ms max */
1763 desc->bPwrOn2PwrGood = 10; 1763 desc->bPwrOn2PwrGood = 10;
1764 desc->bHubContrCurrent = 0; 1764 desc->bHubContrCurrent = 0;
@@ -1869,7 +1869,7 @@ static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1869 reg_write32(hcd->regs, HC_PORTSC1, 1869 reg_write32(hcd->regs, HC_PORTSC1,
1870 temp | PORT_RESUME); 1870 temp | PORT_RESUME);
1871 priv->reset_done = jiffies + 1871 priv->reset_done = jiffies +
1872 msecs_to_jiffies(20); 1872 msecs_to_jiffies(USB_RESUME_TIMEOUT);
1873 } 1873 }
1874 break; 1874 break;
1875 case USB_PORT_FEAT_C_SUSPEND: 1875 case USB_PORT_FEAT_C_SUSPEND:
diff --git a/drivers/usb/isp1760/isp1760-udc.c b/drivers/usb/isp1760/isp1760-udc.c
index 9612d7990565..3fc4fe770253 100644
--- a/drivers/usb/isp1760/isp1760-udc.c
+++ b/drivers/usb/isp1760/isp1760-udc.c
@@ -1191,6 +1191,7 @@ static int isp1760_udc_start(struct usb_gadget *gadget,
1191 struct usb_gadget_driver *driver) 1191 struct usb_gadget_driver *driver)
1192{ 1192{
1193 struct isp1760_udc *udc = gadget_to_udc(gadget); 1193 struct isp1760_udc *udc = gadget_to_udc(gadget);
1194 unsigned long flags;
1194 1195
1195 /* The hardware doesn't support low speed. */ 1196 /* The hardware doesn't support low speed. */
1196 if (driver->max_speed < USB_SPEED_FULL) { 1197 if (driver->max_speed < USB_SPEED_FULL) {
@@ -1198,17 +1199,17 @@ static int isp1760_udc_start(struct usb_gadget *gadget,
1198 return -EINVAL; 1199 return -EINVAL;
1199 } 1200 }
1200 1201
1201 spin_lock(&udc->lock); 1202 spin_lock_irqsave(&udc->lock, flags);
1202 1203
1203 if (udc->driver) { 1204 if (udc->driver) {
1204 dev_err(udc->isp->dev, "UDC already has a gadget driver\n"); 1205 dev_err(udc->isp->dev, "UDC already has a gadget driver\n");
1205 spin_unlock(&udc->lock); 1206 spin_unlock_irqrestore(&udc->lock, flags);
1206 return -EBUSY; 1207 return -EBUSY;
1207 } 1208 }
1208 1209
1209 udc->driver = driver; 1210 udc->driver = driver;
1210 1211
1211 spin_unlock(&udc->lock); 1212 spin_unlock_irqrestore(&udc->lock, flags);
1212 1213
1213 dev_dbg(udc->isp->dev, "starting UDC with driver %s\n", 1214 dev_dbg(udc->isp->dev, "starting UDC with driver %s\n",
1214 driver->function); 1215 driver->function);
@@ -1232,6 +1233,7 @@ static int isp1760_udc_start(struct usb_gadget *gadget,
1232static int isp1760_udc_stop(struct usb_gadget *gadget) 1233static int isp1760_udc_stop(struct usb_gadget *gadget)
1233{ 1234{
1234 struct isp1760_udc *udc = gadget_to_udc(gadget); 1235 struct isp1760_udc *udc = gadget_to_udc(gadget);
1236 unsigned long flags;
1235 1237
1236 dev_dbg(udc->isp->dev, "%s\n", __func__); 1238 dev_dbg(udc->isp->dev, "%s\n", __func__);
1237 1239
@@ -1239,9 +1241,9 @@ static int isp1760_udc_stop(struct usb_gadget *gadget)
1239 1241
1240 isp1760_udc_write(udc, DC_MODE, 0); 1242 isp1760_udc_write(udc, DC_MODE, 0);
1241 1243
1242 spin_lock(&udc->lock); 1244 spin_lock_irqsave(&udc->lock, flags);
1243 udc->driver = NULL; 1245 udc->driver = NULL;
1244 spin_unlock(&udc->lock); 1246 spin_unlock_irqrestore(&udc->lock, flags);
1245 1247
1246 return 0; 1248 return 0;
1247} 1249}
@@ -1411,7 +1413,7 @@ static int isp1760_udc_init(struct isp1760_udc *udc)
1411 return -ENODEV; 1413 return -ENODEV;
1412 } 1414 }
1413 1415
1414 if (chipid != 0x00011582) { 1416 if (chipid != 0x00011582 && chipid != 0x00158210) {
1415 dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid); 1417 dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid);
1416 return -ENODEV; 1418 return -ENODEV;
1417 } 1419 }
@@ -1451,8 +1453,8 @@ int isp1760_udc_register(struct isp1760_device *isp, int irq,
1451 1453
1452 sprintf(udc->irqname, "%s (udc)", devname); 1454 sprintf(udc->irqname, "%s (udc)", devname);
1453 1455
1454 ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | IRQF_DISABLED | 1456 ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | irqflags,
1455 irqflags, udc->irqname, udc); 1457 udc->irqname, udc);
1456 if (ret < 0) 1458 if (ret < 0)
1457 goto error; 1459 goto error;
1458 1460
diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index 76d77206e011..f7a7fc21be8a 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -255,3 +255,16 @@ config USB_LINK_LAYER_TEST
255 This driver is for generating specific traffic for Super Speed Link 255 This driver is for generating specific traffic for Super Speed Link
256 Layer Test Device. Say Y only when you want to conduct USB Super Speed 256 Layer Test Device. Say Y only when you want to conduct USB Super Speed
257 Link Layer Test for host controllers. 257 Link Layer Test for host controllers.
258
259config USB_CHAOSKEY
260 tristate "ChaosKey random number generator driver support"
261 depends on HW_RANDOM
262 help
263 Say Y here if you want to connect an AltusMetrum ChaosKey to
264 your computer's USB port. The ChaosKey is a hardware random
265 number generator which hooks into the kernel entropy pool to
266 ensure a large supply of entropy for /dev/random and
267 /dev/urandom and also provides direct access via /dev/chaoskeyX
268
269 To compile this driver as a module, choose M here: the
270 module will be called chaoskey.
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index 65b0402c1ca1..45fd4ac39d3e 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_USB_USS720) += uss720.o
25obj-$(CONFIG_USB_SEVSEG) += usbsevseg.o 25obj-$(CONFIG_USB_SEVSEG) += usbsevseg.o
26obj-$(CONFIG_USB_YUREX) += yurex.o 26obj-$(CONFIG_USB_YUREX) += yurex.o
27obj-$(CONFIG_USB_HSIC_USB3503) += usb3503.o 27obj-$(CONFIG_USB_HSIC_USB3503) += usb3503.o
28obj-$(CONFIG_USB_CHAOSKEY) += chaoskey.o
28 29
29obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/ 30obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/
30obj-$(CONFIG_USB_LINK_LAYER_TEST) += lvstest.o 31obj-$(CONFIG_USB_LINK_LAYER_TEST) += lvstest.o
diff --git a/drivers/usb/misc/appledisplay.c b/drivers/usb/misc/appledisplay.c
index b3d245ef46ef..a0a3827b4aff 100644
--- a/drivers/usb/misc/appledisplay.c
+++ b/drivers/usb/misc/appledisplay.c
@@ -329,7 +329,7 @@ error:
329 pdata->urbdata, pdata->urb->transfer_dma); 329 pdata->urbdata, pdata->urb->transfer_dma);
330 usb_free_urb(pdata->urb); 330 usb_free_urb(pdata->urb);
331 } 331 }
332 if (pdata->bd && !IS_ERR(pdata->bd)) 332 if (!IS_ERR(pdata->bd))
333 backlight_device_unregister(pdata->bd); 333 backlight_device_unregister(pdata->bd);
334 kfree(pdata->msgdata); 334 kfree(pdata->msgdata);
335 } 335 }
diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c
new file mode 100644
index 000000000000..3ad5d19e4d04
--- /dev/null
+++ b/drivers/usb/misc/chaoskey.c
@@ -0,0 +1,532 @@
1/*
2 * chaoskey - driver for ChaosKey device from Altus Metrum.
3 *
4 * This device provides true random numbers using a noise source based
5 * on a reverse-biased p-n junction in avalanche breakdown. More
6 * details can be found at http://chaoskey.org
7 *
8 * The driver connects to the kernel hardware RNG interface to provide
9 * entropy for /dev/random and other kernel activities. It also offers
10 * a separate /dev/ entry to allow for direct access to the random
11 * bit stream.
12 *
13 * Copyright © 2015 Keith Packard <keithp@keithp.com>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; version 2 of the License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 */
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/usb.h>
28#include <linux/wait.h>
29#include <linux/hw_random.h>
30#include <linux/mutex.h>
31#include <linux/uaccess.h>
32
33static struct usb_driver chaoskey_driver;
34static struct usb_class_driver chaoskey_class;
35static int chaoskey_rng_read(struct hwrng *rng, void *data,
36 size_t max, bool wait);
37
38#define usb_dbg(usb_if, format, arg...) \
39 dev_dbg(&(usb_if)->dev, format, ## arg)
40
41#define usb_err(usb_if, format, arg...) \
42 dev_err(&(usb_if)->dev, format, ## arg)
43
44/* Version Information */
45#define DRIVER_VERSION "v0.1"
46#define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
47#define DRIVER_DESC "Altus Metrum ChaosKey driver"
48#define DRIVER_SHORT "chaoskey"
49
50MODULE_VERSION(DRIVER_VERSION);
51MODULE_AUTHOR(DRIVER_AUTHOR);
52MODULE_DESCRIPTION(DRIVER_DESC);
53MODULE_LICENSE("GPL");
54
55#define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
56#define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
57
58#define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
59
60#define NAK_TIMEOUT (HZ) /* stall/wait timeout for device */
61
62#ifdef CONFIG_USB_DYNAMIC_MINORS
63#define USB_CHAOSKEY_MINOR_BASE 0
64#else
65
66/* IOWARRIOR_MINOR_BASE + 16, not official yet */
67#define USB_CHAOSKEY_MINOR_BASE 224
68#endif
69
70static const struct usb_device_id chaoskey_table[] = {
71 { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
72 { },
73};
74MODULE_DEVICE_TABLE(usb, chaoskey_table);
75
76/* Driver-local specific stuff */
77struct chaoskey {
78 struct usb_interface *interface;
79 char in_ep;
80 struct mutex lock;
81 struct mutex rng_lock;
82 int open; /* open count */
83 int present; /* device not disconnected */
84 int size; /* size of buf */
85 int valid; /* bytes of buf read */
86 int used; /* bytes of buf consumed */
87 char *name; /* product + serial */
88 struct hwrng hwrng; /* Embedded struct for hwrng */
89 int hwrng_registered; /* registered with hwrng API */
90 wait_queue_head_t wait_q; /* for timeouts */
91 char *buf;
92};
93
94static void chaoskey_free(struct chaoskey *dev)
95{
96 usb_dbg(dev->interface, "free");
97 kfree(dev->name);
98 kfree(dev->buf);
99 kfree(dev);
100}
101
102static int chaoskey_probe(struct usb_interface *interface,
103 const struct usb_device_id *id)
104{
105 struct usb_device *udev = interface_to_usbdev(interface);
106 struct usb_host_interface *altsetting = interface->cur_altsetting;
107 int i;
108 int in_ep = -1;
109 struct chaoskey *dev;
110 int result;
111 int size;
112
113 usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
114
115 /* Find the first bulk IN endpoint and its packet size */
116 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
117 if (usb_endpoint_is_bulk_in(&altsetting->endpoint[i].desc)) {
118 in_ep = usb_endpoint_num(&altsetting->endpoint[i].desc);
119 size = usb_endpoint_maxp(&altsetting->endpoint[i].desc);
120 break;
121 }
122 }
123
124 /* Validate endpoint and size */
125 if (in_ep == -1) {
126 usb_dbg(interface, "no IN endpoint found");
127 return -ENODEV;
128 }
129 if (size <= 0) {
130 usb_dbg(interface, "invalid size (%d)", size);
131 return -ENODEV;
132 }
133
134 if (size > CHAOSKEY_BUF_LEN) {
135 usb_dbg(interface, "size reduced from %d to %d\n",
136 size, CHAOSKEY_BUF_LEN);
137 size = CHAOSKEY_BUF_LEN;
138 }
139
140 /* Looks good, allocate and initialize */
141
142 dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
143
144 if (dev == NULL)
145 return -ENOMEM;
146
147 dev->buf = kmalloc(size, GFP_KERNEL);
148
149 if (dev->buf == NULL) {
150 kfree(dev);
151 return -ENOMEM;
152 }
153
154 /* Construct a name using the product and serial values. Each
155 * device needs a unique name for the hwrng code
156 */
157
158 if (udev->product && udev->serial) {
159 dev->name = kmalloc(strlen(udev->product) + 1 +
160 strlen(udev->serial) + 1, GFP_KERNEL);
161 if (dev->name == NULL) {
162 kfree(dev->buf);
163 kfree(dev);
164 return -ENOMEM;
165 }
166
167 strcpy(dev->name, udev->product);
168 strcat(dev->name, "-");
169 strcat(dev->name, udev->serial);
170 }
171
172 dev->interface = interface;
173
174 dev->in_ep = in_ep;
175
176 dev->size = size;
177 dev->present = 1;
178
179 init_waitqueue_head(&dev->wait_q);
180
181 mutex_init(&dev->lock);
182 mutex_init(&dev->rng_lock);
183
184 usb_set_intfdata(interface, dev);
185
186 result = usb_register_dev(interface, &chaoskey_class);
187 if (result) {
188 usb_err(interface, "Unable to allocate minor number.");
189 usb_set_intfdata(interface, NULL);
190 chaoskey_free(dev);
191 return result;
192 }
193
194 dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
195 dev->hwrng.read = chaoskey_rng_read;
196
197 /* Set the 'quality' metric. Quality is measured in units of
198 * 1/1024's of a bit ("mills"). This should be set to 1024,
199 * but there is a bug in the hwrng core which masks it with
200 * 1023.
201 *
202 * The patch that has been merged to the crypto development
203 * tree for that bug limits the value to 1024 at most, so by
204 * setting this to 1024 + 1023, we get 1023 before the fix is
205 * merged and 1024 afterwards. We'll patch this driver once
206 * both bits of code are in the same tree.
207 */
208 dev->hwrng.quality = 1024 + 1023;
209
210 dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
211 if (!dev->hwrng_registered)
212 usb_err(interface, "Unable to register with hwrng");
213
214 usb_enable_autosuspend(udev);
215
216 usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
217 return 0;
218}
219
220static void chaoskey_disconnect(struct usb_interface *interface)
221{
222 struct chaoskey *dev;
223
224 usb_dbg(interface, "disconnect");
225 dev = usb_get_intfdata(interface);
226 if (!dev) {
227 usb_dbg(interface, "disconnect failed - no dev");
228 return;
229 }
230
231 if (dev->hwrng_registered)
232 hwrng_unregister(&dev->hwrng);
233
234 usb_deregister_dev(interface, &chaoskey_class);
235
236 usb_set_intfdata(interface, NULL);
237 mutex_lock(&dev->lock);
238
239 dev->present = 0;
240
241 if (!dev->open) {
242 mutex_unlock(&dev->lock);
243 chaoskey_free(dev);
244 } else
245 mutex_unlock(&dev->lock);
246
247 usb_dbg(interface, "disconnect done");
248}
249
250static int chaoskey_open(struct inode *inode, struct file *file)
251{
252 struct chaoskey *dev;
253 struct usb_interface *interface;
254
255 /* get the interface from minor number and driver information */
256 interface = usb_find_interface(&chaoskey_driver, iminor(inode));
257 if (!interface)
258 return -ENODEV;
259
260 usb_dbg(interface, "open");
261
262 dev = usb_get_intfdata(interface);
263 if (!dev) {
264 usb_dbg(interface, "open (dev)");
265 return -ENODEV;
266 }
267
268 file->private_data = dev;
269 mutex_lock(&dev->lock);
270 ++dev->open;
271 mutex_unlock(&dev->lock);
272
273 usb_dbg(interface, "open success");
274 return 0;
275}
276
277static int chaoskey_release(struct inode *inode, struct file *file)
278{
279 struct chaoskey *dev = file->private_data;
280 struct usb_interface *interface;
281
282 if (dev == NULL)
283 return -ENODEV;
284
285 interface = dev->interface;
286
287 usb_dbg(interface, "release");
288
289 mutex_lock(&dev->lock);
290
291 usb_dbg(interface, "open count at release is %d", dev->open);
292
293 if (dev->open <= 0) {
294 usb_dbg(interface, "invalid open count (%d)", dev->open);
295 mutex_unlock(&dev->lock);
296 return -ENODEV;
297 }
298
299 --dev->open;
300
301 if (!dev->present) {
302 if (dev->open == 0) {
303 mutex_unlock(&dev->lock);
304 chaoskey_free(dev);
305 } else
306 mutex_unlock(&dev->lock);
307 } else
308 mutex_unlock(&dev->lock);
309
310 usb_dbg(interface, "release success");
311 return 0;
312}
313
314/* Fill the buffer. Called with dev->lock held
315 */
316static int _chaoskey_fill(struct chaoskey *dev)
317{
318 DEFINE_WAIT(wait);
319 int result;
320 int this_read;
321 struct usb_device *udev = interface_to_usbdev(dev->interface);
322
323 usb_dbg(dev->interface, "fill");
324
325 /* Return immediately if someone called before the buffer was
326 * empty */
327 if (dev->valid != dev->used) {
328 usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
329 dev->valid, dev->used);
330 return 0;
331 }
332
333 /* Bail if the device has been removed */
334 if (!dev->present) {
335 usb_dbg(dev->interface, "device not present");
336 return -ENODEV;
337 }
338
339 /* Make sure the device is awake */
340 result = usb_autopm_get_interface(dev->interface);
341 if (result) {
342 usb_dbg(dev->interface, "wakeup failed (result %d)", result);
343 return result;
344 }
345
346 result = usb_bulk_msg(udev,
347 usb_rcvbulkpipe(udev, dev->in_ep),
348 dev->buf, dev->size, &this_read,
349 NAK_TIMEOUT);
350
351 /* Let the device go back to sleep eventually */
352 usb_autopm_put_interface(dev->interface);
353
354 if (result == 0) {
355 dev->valid = this_read;
356 dev->used = 0;
357 }
358
359 usb_dbg(dev->interface, "bulk_msg result %d this_read %d",
360 result, this_read);
361
362 return result;
363}
364
365static ssize_t chaoskey_read(struct file *file,
366 char __user *buffer,
367 size_t count,
368 loff_t *ppos)
369{
370 struct chaoskey *dev;
371 ssize_t read_count = 0;
372 int this_time;
373 int result = 0;
374 unsigned long remain;
375
376 dev = file->private_data;
377
378 if (dev == NULL || !dev->present)
379 return -ENODEV;
380
381 usb_dbg(dev->interface, "read %zu", count);
382
383 while (count > 0) {
384
385 /* Grab the rng_lock briefly to ensure that the hwrng interface
386 * gets priority over other user access
387 */
388 result = mutex_lock_interruptible(&dev->rng_lock);
389 if (result)
390 goto bail;
391 mutex_unlock(&dev->rng_lock);
392
393 result = mutex_lock_interruptible(&dev->lock);
394 if (result)
395 goto bail;
396 if (dev->valid == dev->used) {
397 result = _chaoskey_fill(dev);
398 if (result) {
399 mutex_unlock(&dev->lock);
400 goto bail;
401 }
402
403 /* Read returned zero bytes */
404 if (dev->used == dev->valid) {
405 mutex_unlock(&dev->lock);
406 goto bail;
407 }
408 }
409
410 this_time = dev->valid - dev->used;
411 if (this_time > count)
412 this_time = count;
413
414 remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
415 if (remain) {
416 result = -EFAULT;
417
418 /* Consume the bytes that were copied so we don't leak
419 * data to user space
420 */
421 dev->used += this_time - remain;
422 mutex_unlock(&dev->lock);
423 goto bail;
424 }
425
426 count -= this_time;
427 read_count += this_time;
428 buffer += this_time;
429 dev->used += this_time;
430 mutex_unlock(&dev->lock);
431 }
432bail:
433 if (read_count) {
434 usb_dbg(dev->interface, "read %zu bytes", read_count);
435 return read_count;
436 }
437 usb_dbg(dev->interface, "empty read, result %d", result);
438 return result;
439}
440
441static int chaoskey_rng_read(struct hwrng *rng, void *data,
442 size_t max, bool wait)
443{
444 struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
445 int this_time;
446
447 usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
448
449 if (!dev->present) {
450 usb_dbg(dev->interface, "device not present");
451 return 0;
452 }
453
454 /* Hold the rng_lock until we acquire the device lock so that
455 * this operation gets priority over other user access to the
456 * device
457 */
458 mutex_lock(&dev->rng_lock);
459
460 mutex_lock(&dev->lock);
461
462 mutex_unlock(&dev->rng_lock);
463
464 /* Try to fill the buffer if empty. It doesn't actually matter
465 * if _chaoskey_fill works; we'll just return zero bytes as
466 * the buffer will still be empty
467 */
468 if (dev->valid == dev->used)
469 (void) _chaoskey_fill(dev);
470
471 this_time = dev->valid - dev->used;
472 if (this_time > max)
473 this_time = max;
474
475 memcpy(data, dev->buf, this_time);
476
477 dev->used += this_time;
478
479 mutex_unlock(&dev->lock);
480
481 usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
482 return this_time;
483}
484
485#ifdef CONFIG_PM
486static int chaoskey_suspend(struct usb_interface *interface,
487 pm_message_t message)
488{
489 usb_dbg(interface, "suspend");
490 return 0;
491}
492
493static int chaoskey_resume(struct usb_interface *interface)
494{
495 usb_dbg(interface, "resume");
496 return 0;
497}
498#else
499#define chaoskey_suspend NULL
500#define chaoskey_resume NULL
501#endif
502
503/* file operation pointers */
504static const struct file_operations chaoskey_fops = {
505 .owner = THIS_MODULE,
506 .read = chaoskey_read,
507 .open = chaoskey_open,
508 .release = chaoskey_release,
509 .llseek = default_llseek,
510};
511
512/* class driver information */
513static struct usb_class_driver chaoskey_class = {
514 .name = "chaoskey%d",
515 .fops = &chaoskey_fops,
516 .minor_base = USB_CHAOSKEY_MINOR_BASE,
517};
518
519/* usb specific object needed to register this driver with the usb subsystem */
520static struct usb_driver chaoskey_driver = {
521 .name = DRIVER_SHORT,
522 .probe = chaoskey_probe,
523 .disconnect = chaoskey_disconnect,
524 .suspend = chaoskey_suspend,
525 .resume = chaoskey_resume,
526 .reset_resume = chaoskey_resume,
527 .id_table = chaoskey_table,
528 .supports_autosuspend = 1,
529};
530
531module_usb_driver(chaoskey_driver);
532
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index 97cd9e24bd25..7771be3ac178 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -574,7 +574,7 @@ static ssize_t tower_read (struct file *file, char __user *buffer, size_t count,
574 } 574 }
575 575
576 if (read_timeout) { 576 if (read_timeout) {
577 timeout = jiffies + read_timeout * HZ / 1000; 577 timeout = jiffies + msecs_to_jiffies(read_timeout);
578 } 578 }
579 579
580 /* wait for data */ 580 /* wait for data */
@@ -592,7 +592,7 @@ static ssize_t tower_read (struct file *file, char __user *buffer, size_t count,
592 /* reset read timeout during read or write activity */ 592 /* reset read timeout during read or write activity */
593 if (read_timeout 593 if (read_timeout
594 && (dev->read_buffer_length || dev->interrupt_out_busy)) { 594 && (dev->read_buffer_length || dev->interrupt_out_busy)) {
595 timeout = jiffies + read_timeout * HZ / 1000; 595 timeout = jiffies + msecs_to_jiffies(read_timeout);
596 } 596 }
597 /* check for read timeout */ 597 /* check for read timeout */
598 if (read_timeout && time_after (jiffies, timeout)) { 598 if (read_timeout && time_after (jiffies, timeout)) {
@@ -831,7 +831,7 @@ static int tower_probe (struct usb_interface *interface, const struct usb_device
831 dev->read_buffer_length = 0; 831 dev->read_buffer_length = 0;
832 dev->read_packet_length = 0; 832 dev->read_packet_length = 0;
833 spin_lock_init (&dev->read_buffer_lock); 833 spin_lock_init (&dev->read_buffer_lock);
834 dev->packet_timeout_jiffies = packet_timeout * HZ / 1000; 834 dev->packet_timeout_jiffies = msecs_to_jiffies(packet_timeout);
835 dev->read_last_arrival = jiffies; 835 dev->read_last_arrival = jiffies;
836 836
837 init_waitqueue_head (&dev->read_wait); 837 init_waitqueue_head (&dev->read_wait);
diff --git a/drivers/usb/misc/usb3503.c b/drivers/usb/misc/usb3503.c
index 258d2f546e43..64ff5b91752d 100644
--- a/drivers/usb/misc/usb3503.c
+++ b/drivers/usb/misc/usb3503.c
@@ -186,8 +186,31 @@ static int usb3503_probe(struct usb3503 *hub)
186 hub->mode = pdata->initial_mode; 186 hub->mode = pdata->initial_mode;
187 } else if (np) { 187 } else if (np) {
188 struct clk *clk; 188 struct clk *clk;
189 u32 rate = 0;
189 hub->port_off_mask = 0; 190 hub->port_off_mask = 0;
190 191
192 if (!of_property_read_u32(np, "refclk-frequency", &rate)) {
193 switch (rate) {
194 case 38400000:
195 case 26000000:
196 case 19200000:
197 case 12000000:
198 hub->secondary_ref_clk = 0;
199 break;
200 case 24000000:
201 case 27000000:
202 case 25000000:
203 case 50000000:
204 hub->secondary_ref_clk = 1;
205 break;
206 default:
207 dev_err(dev,
208 "unsupported reference clock rate (%d)\n",
209 (int) rate);
210 return -EINVAL;
211 }
212 }
213
191 clk = devm_clk_get(dev, "refclk"); 214 clk = devm_clk_get(dev, "refclk");
192 if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) { 215 if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) {
193 dev_err(dev, "unable to request refclk (%ld)\n", 216 dev_err(dev, "unable to request refclk (%ld)\n",
@@ -196,31 +219,9 @@ static int usb3503_probe(struct usb3503 *hub)
196 } 219 }
197 220
198 if (!IS_ERR(clk)) { 221 if (!IS_ERR(clk)) {
199 u32 rate = 0;
200 hub->clk = clk; 222 hub->clk = clk;
201 223
202 if (!of_property_read_u32(np, "refclk-frequency", 224 if (rate != 0) {
203 &rate)) {
204
205 switch (rate) {
206 case 38400000:
207 case 26000000:
208 case 19200000:
209 case 12000000:
210 hub->secondary_ref_clk = 0;
211 break;
212 case 24000000:
213 case 27000000:
214 case 25000000:
215 case 50000000:
216 hub->secondary_ref_clk = 1;
217 break;
218 default:
219 dev_err(dev,
220 "unsupported reference clock rate (%d)\n",
221 (int) rate);
222 return -EINVAL;
223 }
224 err = clk_set_rate(hub->clk, rate); 225 err = clk_set_rate(hub->clk, rate);
225 if (err) { 226 if (err) {
226 dev_err(dev, 227 dev_err(dev,
diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
index 14e1628483d9..39db8b603627 100644
--- a/drivers/usb/musb/Kconfig
+++ b/drivers/usb/musb/Kconfig
@@ -79,7 +79,8 @@ config USB_MUSB_TUSB6010
79 79
80config USB_MUSB_OMAP2PLUS 80config USB_MUSB_OMAP2PLUS
81 tristate "OMAP2430 and onwards" 81 tristate "OMAP2430 and onwards"
82 depends on ARCH_OMAP2PLUS && USB && OMAP_CONTROL_PHY 82 depends on ARCH_OMAP2PLUS && USB
83 depends on OMAP_CONTROL_PHY || !OMAP_CONTROL_PHY
83 select GENERIC_PHY 84 select GENERIC_PHY
84 85
85config USB_MUSB_AM35X 86config USB_MUSB_AM35X
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 067920f2d570..3789b08ef67b 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -99,6 +99,7 @@
99#include <linux/platform_device.h> 99#include <linux/platform_device.h>
100#include <linux/io.h> 100#include <linux/io.h>
101#include <linux/dma-mapping.h> 101#include <linux/dma-mapping.h>
102#include <linux/usb.h>
102 103
103#include "musb_core.h" 104#include "musb_core.h"
104 105
@@ -507,7 +508,8 @@ void musb_hnp_stop(struct musb *musb)
507 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16); 508 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
508} 509}
509 510
510static void musb_generic_disable(struct musb *musb); 511static void musb_recover_from_babble(struct musb *musb);
512
511/* 513/*
512 * Interrupt Service Routine to record USB "global" interrupts. 514 * Interrupt Service Routine to record USB "global" interrupts.
513 * Since these do not happen often and signify things of 515 * Since these do not happen often and signify things of
@@ -534,35 +536,21 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
534 */ 536 */
535 if (int_usb & MUSB_INTR_RESUME) { 537 if (int_usb & MUSB_INTR_RESUME) {
536 handled = IRQ_HANDLED; 538 handled = IRQ_HANDLED;
537 dev_dbg(musb->controller, "RESUME (%s)\n", usb_otg_state_string(musb->xceiv->otg->state)); 539 dev_dbg(musb->controller, "RESUME (%s)\n",
540 usb_otg_state_string(musb->xceiv->otg->state));
538 541
539 if (devctl & MUSB_DEVCTL_HM) { 542 if (devctl & MUSB_DEVCTL_HM) {
540 void __iomem *mbase = musb->mregs;
541 u8 power;
542
543 switch (musb->xceiv->otg->state) { 543 switch (musb->xceiv->otg->state) {
544 case OTG_STATE_A_SUSPEND: 544 case OTG_STATE_A_SUSPEND:
545 /* remote wakeup? later, GetPortStatus 545 /* remote wakeup? later, GetPortStatus
546 * will stop RESUME signaling 546 * will stop RESUME signaling
547 */ 547 */
548 548
549 power = musb_readb(musb->mregs, MUSB_POWER);
550 if (power & MUSB_POWER_SUSPENDM) {
551 /* spurious */
552 musb->int_usb &= ~MUSB_INTR_SUSPEND;
553 dev_dbg(musb->controller, "Spurious SUSPENDM\n");
554 break;
555 }
556
557 power &= ~MUSB_POWER_SUSPENDM;
558 musb_writeb(mbase, MUSB_POWER,
559 power | MUSB_POWER_RESUME);
560
561 musb->port1_status |= 549 musb->port1_status |=
562 (USB_PORT_STAT_C_SUSPEND << 16) 550 (USB_PORT_STAT_C_SUSPEND << 16)
563 | MUSB_PORT_STAT_RESUME; 551 | MUSB_PORT_STAT_RESUME;
564 musb->rh_timer = jiffies 552 musb->rh_timer = jiffies
565 + msecs_to_jiffies(20); 553 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
566 musb->need_finish_resume = 1; 554 musb->need_finish_resume = 1;
567 555
568 musb->xceiv->otg->state = OTG_STATE_A_HOST; 556 musb->xceiv->otg->state = OTG_STATE_A_HOST;
@@ -775,10 +763,6 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
775 763
776 musb->ep0_stage = MUSB_EP0_START; 764 musb->ep0_stage = MUSB_EP0_START;
777 765
778 /* flush endpoints when transitioning from Device Mode */
779 if (is_peripheral_active(musb)) {
780 /* REVISIT HNP; just force disconnect */
781 }
782 musb->intrtxe = musb->epmask; 766 musb->intrtxe = musb->epmask;
783 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe); 767 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
784 musb->intrrxe = musb->epmask & 0xfffe; 768 musb->intrrxe = musb->epmask & 0xfffe;
@@ -879,20 +863,19 @@ b_host:
879 */ 863 */
880 if (int_usb & MUSB_INTR_RESET) { 864 if (int_usb & MUSB_INTR_RESET) {
881 handled = IRQ_HANDLED; 865 handled = IRQ_HANDLED;
882 if ((devctl & MUSB_DEVCTL_HM) != 0) { 866 if (devctl & MUSB_DEVCTL_HM) {
883 /* 867 /*
884 * Looks like non-HS BABBLE can be ignored, but 868 * When BABBLE happens what we can depends on which
885 * HS BABBLE is an error condition. For HS the solution 869 * platform MUSB is running, because some platforms
886 * is to avoid babble in the first place and fix what 870 * implemented proprietary means for 'recovering' from
887 * caused BABBLE. When HS BABBLE happens we can only 871 * Babble conditions. One such platform is AM335x. In
888 * stop the session. 872 * most cases, however, the only thing we can do is
873 * drop the session.
889 */ 874 */
890 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV)) 875 dev_err(musb->controller, "Babble\n");
891 dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl); 876
892 else { 877 if (is_host_active(musb))
893 ERR("Stopping host session -- babble\n"); 878 musb_recover_from_babble(musb);
894 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
895 }
896 } else { 879 } else {
897 dev_dbg(musb->controller, "BUS RESET as %s\n", 880 dev_dbg(musb->controller, "BUS RESET as %s\n",
898 usb_otg_state_string(musb->xceiv->otg->state)); 881 usb_otg_state_string(musb->xceiv->otg->state));
@@ -931,13 +914,6 @@ b_host:
931 } 914 }
932 } 915 }
933 916
934 /* handle babble condition */
935 if (int_usb & MUSB_INTR_BABBLE && is_host_active(musb)) {
936 musb_generic_disable(musb);
937 schedule_delayed_work(&musb->recover_work,
938 msecs_to_jiffies(100));
939 }
940
941#if 0 917#if 0
942/* REVISIT ... this would be for multiplexing periodic endpoints, or 918/* REVISIT ... this would be for multiplexing periodic endpoints, or
943 * supporting transfer phasing to prevent exceeding ISO bandwidth 919 * supporting transfer phasing to prevent exceeding ISO bandwidth
@@ -990,7 +966,7 @@ b_host:
990 966
991/*-------------------------------------------------------------------------*/ 967/*-------------------------------------------------------------------------*/
992 968
993static void musb_generic_disable(struct musb *musb) 969static void musb_disable_interrupts(struct musb *musb)
994{ 970{
995 void __iomem *mbase = musb->mregs; 971 void __iomem *mbase = musb->mregs;
996 u16 temp; 972 u16 temp;
@@ -1002,14 +978,33 @@ static void musb_generic_disable(struct musb *musb)
1002 musb->intrrxe = 0; 978 musb->intrrxe = 0;
1003 musb_writew(mbase, MUSB_INTRRXE, 0); 979 musb_writew(mbase, MUSB_INTRRXE, 0);
1004 980
1005 /* off */
1006 musb_writeb(mbase, MUSB_DEVCTL, 0);
1007
1008 /* flush pending interrupts */ 981 /* flush pending interrupts */
1009 temp = musb_readb(mbase, MUSB_INTRUSB); 982 temp = musb_readb(mbase, MUSB_INTRUSB);
1010 temp = musb_readw(mbase, MUSB_INTRTX); 983 temp = musb_readw(mbase, MUSB_INTRTX);
1011 temp = musb_readw(mbase, MUSB_INTRRX); 984 temp = musb_readw(mbase, MUSB_INTRRX);
985}
986
987static void musb_enable_interrupts(struct musb *musb)
988{
989 void __iomem *regs = musb->mregs;
990
991 /* Set INT enable registers, enable interrupts */
992 musb->intrtxe = musb->epmask;
993 musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
994 musb->intrrxe = musb->epmask & 0xfffe;
995 musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
996 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
997
998}
999
1000static void musb_generic_disable(struct musb *musb)
1001{
1002 void __iomem *mbase = musb->mregs;
1012 1003
1004 musb_disable_interrupts(musb);
1005
1006 /* off */
1007 musb_writeb(mbase, MUSB_DEVCTL, 0);
1013} 1008}
1014 1009
1015/* 1010/*
@@ -1022,13 +1017,7 @@ void musb_start(struct musb *musb)
1022 1017
1023 dev_dbg(musb->controller, "<== devctl %02x\n", devctl); 1018 dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
1024 1019
1025 /* Set INT enable registers, enable interrupts */ 1020 musb_enable_interrupts(musb);
1026 musb->intrtxe = musb->epmask;
1027 musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
1028 musb->intrrxe = musb->epmask & 0xfffe;
1029 musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
1030 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
1031
1032 musb_writeb(regs, MUSB_TESTMODE, 0); 1021 musb_writeb(regs, MUSB_TESTMODE, 0);
1033 1022
1034 /* put into basic highspeed mode and start session */ 1023 /* put into basic highspeed mode and start session */
@@ -1587,9 +1576,12 @@ static int musb_core_init(u16 musb_type, struct musb *musb)
1587irqreturn_t musb_interrupt(struct musb *musb) 1576irqreturn_t musb_interrupt(struct musb *musb)
1588{ 1577{
1589 irqreturn_t retval = IRQ_NONE; 1578 irqreturn_t retval = IRQ_NONE;
1579 unsigned long status;
1580 unsigned long epnum;
1590 u8 devctl; 1581 u8 devctl;
1591 int ep_num; 1582
1592 u32 reg; 1583 if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
1584 return IRQ_NONE;
1593 1585
1594 devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 1586 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1595 1587
@@ -1597,56 +1589,57 @@ irqreturn_t musb_interrupt(struct musb *musb)
1597 is_host_active(musb) ? "host" : "peripheral", 1589 is_host_active(musb) ? "host" : "peripheral",
1598 musb->int_usb, musb->int_tx, musb->int_rx); 1590 musb->int_usb, musb->int_tx, musb->int_rx);
1599 1591
1600 /* the core can interrupt us for multiple reasons; docs have 1592 /**
1601 * a generic interrupt flowchart to follow 1593 * According to Mentor Graphics' documentation, flowchart on page 98,
1594 * IRQ should be handled as follows:
1595 *
1596 * . Resume IRQ
1597 * . Session Request IRQ
1598 * . VBUS Error IRQ
1599 * . Suspend IRQ
1600 * . Connect IRQ
1601 * . Disconnect IRQ
1602 * . Reset/Babble IRQ
1603 * . SOF IRQ (we're not using this one)
1604 * . Endpoint 0 IRQ
1605 * . TX Endpoints
1606 * . RX Endpoints
1607 *
1608 * We will be following that flowchart in order to avoid any problems
1609 * that might arise with internal Finite State Machine.
1602 */ 1610 */
1603 if (musb->int_usb)
1604 retval |= musb_stage0_irq(musb, musb->int_usb,
1605 devctl);
1606 1611
1607 /* "stage 1" is handling endpoint irqs */ 1612 if (musb->int_usb)
1613 retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
1608 1614
1609 /* handle endpoint 0 first */
1610 if (musb->int_tx & 1) { 1615 if (musb->int_tx & 1) {
1611 if (is_host_active(musb)) 1616 if (is_host_active(musb))
1612 retval |= musb_h_ep0_irq(musb); 1617 retval |= musb_h_ep0_irq(musb);
1613 else 1618 else
1614 retval |= musb_g_ep0_irq(musb); 1619 retval |= musb_g_ep0_irq(musb);
1620
1621 /* we have just handled endpoint 0 IRQ, clear it */
1622 musb->int_tx &= ~BIT(0);
1615 } 1623 }
1616 1624
1617 /* RX on endpoints 1-15 */ 1625 status = musb->int_tx;
1618 reg = musb->int_rx >> 1;
1619 ep_num = 1;
1620 while (reg) {
1621 if (reg & 1) {
1622 /* musb_ep_select(musb->mregs, ep_num); */
1623 /* REVISIT just retval = ep->rx_irq(...) */
1624 retval = IRQ_HANDLED;
1625 if (is_host_active(musb))
1626 musb_host_rx(musb, ep_num);
1627 else
1628 musb_g_rx(musb, ep_num);
1629 }
1630 1626
1631 reg >>= 1; 1627 for_each_set_bit(epnum, &status, 16) {
1632 ep_num++; 1628 retval = IRQ_HANDLED;
1629 if (is_host_active(musb))
1630 musb_host_tx(musb, epnum);
1631 else
1632 musb_g_tx(musb, epnum);
1633 } 1633 }
1634 1634
1635 /* TX on endpoints 1-15 */ 1635 status = musb->int_rx;
1636 reg = musb->int_tx >> 1; 1636
1637 ep_num = 1; 1637 for_each_set_bit(epnum, &status, 16) {
1638 while (reg) { 1638 retval = IRQ_HANDLED;
1639 if (reg & 1) { 1639 if (is_host_active(musb))
1640 /* musb_ep_select(musb->mregs, ep_num); */ 1640 musb_host_rx(musb, epnum);
1641 /* REVISIT just retval |= ep->tx_irq(...) */ 1641 else
1642 retval = IRQ_HANDLED; 1642 musb_g_rx(musb, epnum);
1643 if (is_host_active(musb))
1644 musb_host_tx(musb, ep_num);
1645 else
1646 musb_g_tx(musb, ep_num);
1647 }
1648 reg >>= 1;
1649 ep_num++;
1650 } 1643 }
1651 1644
1652 return retval; 1645 return retval;
@@ -1825,33 +1818,44 @@ static void musb_irq_work(struct work_struct *data)
1825 } 1818 }
1826} 1819}
1827 1820
1828/* Recover from babble interrupt conditions */ 1821static void musb_recover_from_babble(struct musb *musb)
1829static void musb_recover_work(struct work_struct *data)
1830{ 1822{
1831 struct musb *musb = container_of(data, struct musb, recover_work.work); 1823 int ret;
1832 int status, ret; 1824 u8 devctl;
1833 1825
1834 ret = musb_platform_reset(musb); 1826 musb_disable_interrupts(musb);
1835 if (ret) 1827
1828 /*
1829 * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
1830 * it some slack and wait for 10us.
1831 */
1832 udelay(10);
1833
1834 ret = musb_platform_recover(musb);
1835 if (ret) {
1836 musb_enable_interrupts(musb);
1836 return; 1837 return;
1838 }
1837 1839
1838 usb_phy_vbus_off(musb->xceiv); 1840 /* drop session bit */
1839 usleep_range(100, 200); 1841 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1842 devctl &= ~MUSB_DEVCTL_SESSION;
1843 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
1840 1844
1841 usb_phy_vbus_on(musb->xceiv); 1845 /* tell usbcore about it */
1842 usleep_range(100, 200); 1846 musb_root_disconnect(musb);
1843 1847
1844 /* 1848 /*
1845 * When a babble condition occurs, the musb controller 1849 * When a babble condition occurs, the musb controller
1846 * removes the session bit and the endpoint config is lost. 1850 * removes the session bit and the endpoint config is lost.
1847 */ 1851 */
1848 if (musb->dyn_fifo) 1852 if (musb->dyn_fifo)
1849 status = ep_config_from_table(musb); 1853 ret = ep_config_from_table(musb);
1850 else 1854 else
1851 status = ep_config_from_hw(musb); 1855 ret = ep_config_from_hw(musb);
1852 1856
1853 /* start the session again */ 1857 /* restart session */
1854 if (status == 0) 1858 if (ret == 0)
1855 musb_start(musb); 1859 musb_start(musb);
1856} 1860}
1857 1861
@@ -2087,7 +2091,6 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
2087 2091
2088 /* Init IRQ workqueue before request_irq */ 2092 /* Init IRQ workqueue before request_irq */
2089 INIT_WORK(&musb->irq_work, musb_irq_work); 2093 INIT_WORK(&musb->irq_work, musb_irq_work);
2090 INIT_DELAYED_WORK(&musb->recover_work, musb_recover_work);
2091 INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset); 2094 INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
2092 INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume); 2095 INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
2093 2096
@@ -2183,7 +2186,6 @@ fail4:
2183 2186
2184fail3: 2187fail3:
2185 cancel_work_sync(&musb->irq_work); 2188 cancel_work_sync(&musb->irq_work);
2186 cancel_delayed_work_sync(&musb->recover_work);
2187 cancel_delayed_work_sync(&musb->finish_resume_work); 2189 cancel_delayed_work_sync(&musb->finish_resume_work);
2188 cancel_delayed_work_sync(&musb->deassert_reset_work); 2190 cancel_delayed_work_sync(&musb->deassert_reset_work);
2189 if (musb->dma_controller) 2191 if (musb->dma_controller)
@@ -2249,7 +2251,6 @@ static int musb_remove(struct platform_device *pdev)
2249 dma_controller_destroy(musb->dma_controller); 2251 dma_controller_destroy(musb->dma_controller);
2250 2252
2251 cancel_work_sync(&musb->irq_work); 2253 cancel_work_sync(&musb->irq_work);
2252 cancel_delayed_work_sync(&musb->recover_work);
2253 cancel_delayed_work_sync(&musb->finish_resume_work); 2254 cancel_delayed_work_sync(&musb->finish_resume_work);
2254 cancel_delayed_work_sync(&musb->deassert_reset_work); 2255 cancel_delayed_work_sync(&musb->deassert_reset_work);
2255 musb_free(musb); 2256 musb_free(musb);
@@ -2463,7 +2464,7 @@ static int musb_resume(struct device *dev)
2463 if (musb->need_finish_resume) { 2464 if (musb->need_finish_resume) {
2464 musb->need_finish_resume = 0; 2465 musb->need_finish_resume = 0;
2465 schedule_delayed_work(&musb->finish_resume_work, 2466 schedule_delayed_work(&musb->finish_resume_work,
2466 msecs_to_jiffies(20)); 2467 msecs_to_jiffies(USB_RESUME_TIMEOUT));
2467 } 2468 }
2468 2469
2469 /* 2470 /*
@@ -2506,7 +2507,7 @@ static int musb_runtime_resume(struct device *dev)
2506 if (musb->need_finish_resume) { 2507 if (musb->need_finish_resume) {
2507 musb->need_finish_resume = 0; 2508 musb->need_finish_resume = 0;
2508 schedule_delayed_work(&musb->finish_resume_work, 2509 schedule_delayed_work(&musb->finish_resume_work,
2509 msecs_to_jiffies(20)); 2510 msecs_to_jiffies(USB_RESUME_TIMEOUT));
2510 } 2511 }
2511 2512
2512 return 0; 2513 return 0;
diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h
index 5e65958f7915..3877249a8b2d 100644
--- a/drivers/usb/musb/musb_core.h
+++ b/drivers/usb/musb/musb_core.h
@@ -160,7 +160,8 @@ struct musb_io;
160 * @init: turns on clocks, sets up platform-specific registers, etc 160 * @init: turns on clocks, sets up platform-specific registers, etc
161 * @exit: undoes @init 161 * @exit: undoes @init
162 * @set_mode: forcefully changes operating mode 162 * @set_mode: forcefully changes operating mode
163 * @try_ilde: tries to idle the IP 163 * @try_idle: tries to idle the IP
164 * @recover: platform-specific babble recovery
164 * @vbus_status: returns vbus status if possible 165 * @vbus_status: returns vbus status if possible
165 * @set_vbus: forces vbus status 166 * @set_vbus: forces vbus status
166 * @adjust_channel_params: pre check for standard dma channel_program func 167 * @adjust_channel_params: pre check for standard dma channel_program func
@@ -196,7 +197,7 @@ struct musb_platform_ops {
196 void (*write_fifo)(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf); 197 void (*write_fifo)(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf);
197 int (*set_mode)(struct musb *musb, u8 mode); 198 int (*set_mode)(struct musb *musb, u8 mode);
198 void (*try_idle)(struct musb *musb, unsigned long timeout); 199 void (*try_idle)(struct musb *musb, unsigned long timeout);
199 int (*reset)(struct musb *musb); 200 int (*recover)(struct musb *musb);
200 201
201 int (*vbus_status)(struct musb *musb); 202 int (*vbus_status)(struct musb *musb);
202 void (*set_vbus)(struct musb *musb, int on); 203 void (*set_vbus)(struct musb *musb, int on);
@@ -300,7 +301,6 @@ struct musb {
300 301
301 irqreturn_t (*isr)(int, void *); 302 irqreturn_t (*isr)(int, void *);
302 struct work_struct irq_work; 303 struct work_struct irq_work;
303 struct delayed_work recover_work;
304 struct delayed_work deassert_reset_work; 304 struct delayed_work deassert_reset_work;
305 struct delayed_work finish_resume_work; 305 struct delayed_work finish_resume_work;
306 u16 hwvers; 306 u16 hwvers;
@@ -558,12 +558,12 @@ static inline void musb_platform_try_idle(struct musb *musb,
558 musb->ops->try_idle(musb, timeout); 558 musb->ops->try_idle(musb, timeout);
559} 559}
560 560
561static inline int musb_platform_reset(struct musb *musb) 561static inline int musb_platform_recover(struct musb *musb)
562{ 562{
563 if (!musb->ops->reset) 563 if (!musb->ops->recover)
564 return -EINVAL; 564 return 0;
565 565
566 return musb->ops->reset(musb); 566 return musb->ops->recover(musb);
567} 567}
568 568
569static inline int musb_platform_get_vbus_status(struct musb *musb) 569static inline int musb_platform_get_vbus_status(struct musb *musb)
diff --git a/drivers/usb/musb/musb_cppi41.c b/drivers/usb/musb/musb_cppi41.c
index be84562d021b..8bd8c5e26921 100644
--- a/drivers/usb/musb/musb_cppi41.c
+++ b/drivers/usb/musb/musb_cppi41.c
@@ -225,10 +225,12 @@ static void cppi41_dma_callback(void *private_data)
225 struct dma_channel *channel = private_data; 225 struct dma_channel *channel = private_data;
226 struct cppi41_dma_channel *cppi41_channel = channel->private_data; 226 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
227 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep; 227 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
228 struct cppi41_dma_controller *controller;
228 struct musb *musb = hw_ep->musb; 229 struct musb *musb = hw_ep->musb;
229 unsigned long flags; 230 unsigned long flags;
230 struct dma_tx_state txstate; 231 struct dma_tx_state txstate;
231 u32 transferred; 232 u32 transferred;
233 int is_hs = 0;
232 bool empty; 234 bool empty;
233 235
234 spin_lock_irqsave(&musb->lock, flags); 236 spin_lock_irqsave(&musb->lock, flags);
@@ -248,61 +250,59 @@ static void cppi41_dma_callback(void *private_data)
248 transferred < cppi41_channel->packet_sz) 250 transferred < cppi41_channel->packet_sz)
249 cppi41_channel->prog_len = 0; 251 cppi41_channel->prog_len = 0;
250 252
251 empty = musb_is_tx_fifo_empty(hw_ep); 253 if (cppi41_channel->is_tx)
252 if (empty) { 254 empty = musb_is_tx_fifo_empty(hw_ep);
255
256 if (!cppi41_channel->is_tx || empty) {
253 cppi41_trans_done(cppi41_channel); 257 cppi41_trans_done(cppi41_channel);
254 } else { 258 goto out;
255 struct cppi41_dma_controller *controller; 259 }
256 int is_hs = 0;
257 /*
258 * On AM335x it has been observed that the TX interrupt fires
259 * too early that means the TXFIFO is not yet empty but the DMA
260 * engine says that it is done with the transfer. We don't
261 * receive a FIFO empty interrupt so the only thing we can do is
262 * to poll for the bit. On HS it usually takes 2us, on FS around
263 * 110us - 150us depending on the transfer size.
264 * We spin on HS (no longer than than 25us and setup a timer on
265 * FS to check for the bit and complete the transfer.
266 */
267 controller = cppi41_channel->controller;
268 260
269 if (is_host_active(musb)) { 261 /*
270 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED) 262 * On AM335x it has been observed that the TX interrupt fires
271 is_hs = 1; 263 * too early that means the TXFIFO is not yet empty but the DMA
272 } else { 264 * engine says that it is done with the transfer. We don't
273 if (musb->g.speed == USB_SPEED_HIGH) 265 * receive a FIFO empty interrupt so the only thing we can do is
274 is_hs = 1; 266 * to poll for the bit. On HS it usually takes 2us, on FS around
275 } 267 * 110us - 150us depending on the transfer size.
276 if (is_hs) { 268 * We spin on HS (no longer than than 25us and setup a timer on
277 unsigned wait = 25; 269 * FS to check for the bit and complete the transfer.
278 270 */
279 do { 271 controller = cppi41_channel->controller;
280 empty = musb_is_tx_fifo_empty(hw_ep); 272
281 if (empty) 273 if (is_host_active(musb)) {
282 break; 274 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
283 wait--; 275 is_hs = 1;
284 if (!wait) 276 } else {
285 break; 277 if (musb->g.speed == USB_SPEED_HIGH)
286 udelay(1); 278 is_hs = 1;
287 } while (1); 279 }
280 if (is_hs) {
281 unsigned wait = 25;
288 282
283 do {
289 empty = musb_is_tx_fifo_empty(hw_ep); 284 empty = musb_is_tx_fifo_empty(hw_ep);
290 if (empty) { 285 if (empty) {
291 cppi41_trans_done(cppi41_channel); 286 cppi41_trans_done(cppi41_channel);
292 goto out; 287 goto out;
293 } 288 }
294 } 289 wait--;
295 list_add_tail(&cppi41_channel->tx_check, 290 if (!wait)
296 &controller->early_tx_list); 291 break;
297 if (!hrtimer_is_queued(&controller->early_tx)) { 292 cpu_relax();
298 unsigned long usecs = cppi41_channel->total_len / 10; 293 } while (1);
294 }
295 list_add_tail(&cppi41_channel->tx_check,
296 &controller->early_tx_list);
297 if (!hrtimer_is_queued(&controller->early_tx)) {
298 unsigned long usecs = cppi41_channel->total_len / 10;
299 299
300 hrtimer_start_range_ns(&controller->early_tx, 300 hrtimer_start_range_ns(&controller->early_tx,
301 ktime_set(0, usecs * NSEC_PER_USEC), 301 ktime_set(0, usecs * NSEC_PER_USEC),
302 20 * NSEC_PER_USEC, 302 20 * NSEC_PER_USEC,
303 HRTIMER_MODE_REL); 303 HRTIMER_MODE_REL);
304 }
305 } 304 }
305
306out: 306out:
307 spin_unlock_irqrestore(&musb->lock, flags); 307 spin_unlock_irqrestore(&musb->lock, flags);
308} 308}
diff --git a/drivers/usb/musb/musb_dsps.c b/drivers/usb/musb/musb_dsps.c
index a900c9877195..65d931a28a14 100644
--- a/drivers/usb/musb/musb_dsps.c
+++ b/drivers/usb/musb/musb_dsps.c
@@ -119,7 +119,7 @@ struct dsps_musb_wrapper {
119 unsigned iddig:5; 119 unsigned iddig:5;
120 unsigned iddig_mux:5; 120 unsigned iddig_mux:5;
121 /* miscellaneous stuff */ 121 /* miscellaneous stuff */
122 u8 poll_seconds; 122 unsigned poll_timeout;
123}; 123};
124 124
125/* 125/*
@@ -225,9 +225,8 @@ static void dsps_musb_enable(struct musb *musb)
225 225
226 dsps_writel(reg_base, wrp->epintr_set, epmask); 226 dsps_writel(reg_base, wrp->epintr_set, epmask);
227 dsps_writel(reg_base, wrp->coreintr_set, coremask); 227 dsps_writel(reg_base, wrp->coreintr_set, coremask);
228 /* Force the DRVVBUS IRQ so we can start polling for ID change. */ 228 /* start polling for ID change. */
229 dsps_writel(reg_base, wrp->coreintr_set, 229 mod_timer(&glue->timer, jiffies + msecs_to_jiffies(wrp->poll_timeout));
230 (1 << wrp->drvvbus) << wrp->usb_shift);
231 dsps_musb_try_idle(musb, 0); 230 dsps_musb_try_idle(musb, 0);
232} 231}
233 232
@@ -285,7 +284,8 @@ static void otg_timer(unsigned long _musb)
285 } 284 }
286 if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session) 285 if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session)
287 dsps_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 286 dsps_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
288 mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 287 mod_timer(&glue->timer, jiffies +
288 msecs_to_jiffies(wrp->poll_timeout));
289 break; 289 break;
290 case OTG_STATE_A_WAIT_VFALL: 290 case OTG_STATE_A_WAIT_VFALL:
291 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE; 291 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
@@ -330,28 +330,6 @@ static irqreturn_t dsps_interrupt(int irq, void *hci)
330 330
331 dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n", 331 dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n",
332 usbintr, epintr); 332 usbintr, epintr);
333 /*
334 * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
335 * DSPS IP's missing ID change IRQ. We need an ID change IRQ to
336 * switch appropriately between halves of the OTG state machine.
337 * Managing DEVCTL.SESSION per Mentor docs requires that we know its
338 * value but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
339 * Also, DRVVBUS pulses for SRP (but not at 5V) ...
340 */
341 if (is_host_active(musb) && usbintr & MUSB_INTR_BABBLE) {
342 pr_info("CAUTION: musb: Babble Interrupt Occurred\n");
343
344 /*
345 * When a babble condition occurs, the musb controller removes
346 * the session and is no longer in host mode. Hence, all
347 * devices connected to its root hub get disconnected.
348 *
349 * Hand this error down to the musb core isr, so it can
350 * recover.
351 */
352 musb->int_usb = MUSB_INTR_BABBLE | MUSB_INTR_DISCONNECT;
353 musb->int_tx = musb->int_rx = 0;
354 }
355 333
356 if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) { 334 if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) {
357 int drvvbus = dsps_readl(reg_base, wrp->status); 335 int drvvbus = dsps_readl(reg_base, wrp->status);
@@ -374,8 +352,8 @@ static irqreturn_t dsps_interrupt(int irq, void *hci)
374 */ 352 */
375 musb->int_usb &= ~MUSB_INTR_VBUSERROR; 353 musb->int_usb &= ~MUSB_INTR_VBUSERROR;
376 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL; 354 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
377 mod_timer(&glue->timer, 355 mod_timer(&glue->timer, jiffies +
378 jiffies + wrp->poll_seconds * HZ); 356 msecs_to_jiffies(wrp->poll_timeout));
379 WARNING("VBUS error workaround (delay coming)\n"); 357 WARNING("VBUS error workaround (delay coming)\n");
380 } else if (drvvbus) { 358 } else if (drvvbus) {
381 MUSB_HST_MODE(musb); 359 MUSB_HST_MODE(musb);
@@ -404,7 +382,8 @@ static irqreturn_t dsps_interrupt(int irq, void *hci)
404 /* Poll for ID change in OTG port mode */ 382 /* Poll for ID change in OTG port mode */
405 if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 383 if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
406 musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 384 musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
407 mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 385 mod_timer(&glue->timer, jiffies +
386 msecs_to_jiffies(wrp->poll_timeout));
408out: 387out:
409 spin_unlock_irqrestore(&musb->lock, flags); 388 spin_unlock_irqrestore(&musb->lock, flags);
410 389
@@ -453,7 +432,7 @@ static int dsps_musb_init(struct musb *musb)
453 musb->ctrl_base = reg_base; 432 musb->ctrl_base = reg_base;
454 433
455 /* NOP driver needs change if supporting dual instance */ 434 /* NOP driver needs change if supporting dual instance */
456 musb->xceiv = devm_usb_get_phy_by_phandle(dev, "phys", 0); 435 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "phys", 0);
457 if (IS_ERR(musb->xceiv)) 436 if (IS_ERR(musb->xceiv))
458 return PTR_ERR(musb->xceiv); 437 return PTR_ERR(musb->xceiv);
459 438
@@ -497,7 +476,7 @@ static int dsps_musb_init(struct musb *musb)
497 * logic enabled. 476 * logic enabled.
498 */ 477 */
499 val = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 478 val = dsps_readb(musb->mregs, MUSB_BABBLE_CTL);
500 if (val == MUSB_BABBLE_RCV_DISABLE) { 479 if (val & MUSB_BABBLE_RCV_DISABLE) {
501 glue->sw_babble_enabled = true; 480 glue->sw_babble_enabled = true;
502 val |= MUSB_BABBLE_SW_SESSION_CTRL; 481 val |= MUSB_BABBLE_SW_SESSION_CTRL;
503 dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, val); 482 dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, val);
@@ -571,7 +550,7 @@ static int dsps_musb_set_mode(struct musb *musb, u8 mode)
571 return 0; 550 return 0;
572} 551}
573 552
574static bool sw_babble_control(struct musb *musb) 553static bool dsps_sw_babble_control(struct musb *musb)
575{ 554{
576 u8 babble_ctl; 555 u8 babble_ctl;
577 bool session_restart = false; 556 bool session_restart = false;
@@ -622,37 +601,36 @@ static bool sw_babble_control(struct musb *musb)
622 return session_restart; 601 return session_restart;
623} 602}
624 603
625static int dsps_musb_reset(struct musb *musb) 604static int dsps_musb_recover(struct musb *musb)
626{ 605{
627 struct device *dev = musb->controller; 606 struct device *dev = musb->controller;
628 struct dsps_glue *glue = dev_get_drvdata(dev->parent); 607 struct dsps_glue *glue = dev_get_drvdata(dev->parent);
629 const struct dsps_musb_wrapper *wrp = glue->wrp; 608 int session_restart = 0;
630 int session_restart = 0, error;
631 609
632 if (glue->sw_babble_enabled) 610 if (glue->sw_babble_enabled)
633 session_restart = sw_babble_control(musb); 611 session_restart = dsps_sw_babble_control(musb);
634 /* 612 else
635 * In case of new silicon version babble condition can be recovered
636 * without resetting the MUSB. But for older silicon versions, MUSB
637 * reset is needed
638 */
639 if (session_restart || !glue->sw_babble_enabled) {
640 dev_info(musb->controller, "Restarting MUSB to recover from Babble\n");
641 dsps_writel(musb->ctrl_base, wrp->control, (1 << wrp->reset));
642 usleep_range(100, 200);
643 usb_phy_shutdown(musb->xceiv);
644 error = phy_power_off(musb->phy);
645 if (error)
646 dev_err(dev, "phy shutdown failed: %i\n", error);
647 usleep_range(100, 200);
648 usb_phy_init(musb->xceiv);
649 error = phy_power_on(musb->phy);
650 if (error)
651 dev_err(dev, "phy powerup failed: %i\n", error);
652 session_restart = 1; 613 session_restart = 1;
614
615 return session_restart ? 0 : -EPIPE;
616}
617
618/* Similar to am35x, dm81xx support only 32-bit read operation */
619static void dsps_read_fifo32(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
620{
621 void __iomem *fifo = hw_ep->fifo;
622
623 if (len >= 4) {
624 ioread32_rep(fifo, dst, len >> 2);
625 dst += len & ~0x03;
626 len &= 0x03;
653 } 627 }
654 628
655 return !session_restart; 629 /* Read any remaining 1 to 3 bytes */
630 if (len > 0) {
631 u32 val = musb_readl(fifo, 0);
632 memcpy(dst, &val, len);
633 }
656} 634}
657 635
658static struct musb_platform_ops dsps_ops = { 636static struct musb_platform_ops dsps_ops = {
@@ -665,7 +643,7 @@ static struct musb_platform_ops dsps_ops = {
665 643
666 .try_idle = dsps_musb_try_idle, 644 .try_idle = dsps_musb_try_idle,
667 .set_mode = dsps_musb_set_mode, 645 .set_mode = dsps_musb_set_mode,
668 .reset = dsps_musb_reset, 646 .recover = dsps_musb_recover,
669}; 647};
670 648
671static u64 musb_dmamask = DMA_BIT_MASK(32); 649static u64 musb_dmamask = DMA_BIT_MASK(32);
@@ -737,7 +715,6 @@ static int dsps_create_musb_pdev(struct dsps_glue *glue,
737 musb->dev.parent = dev; 715 musb->dev.parent = dev;
738 musb->dev.dma_mask = &musb_dmamask; 716 musb->dev.dma_mask = &musb_dmamask;
739 musb->dev.coherent_dma_mask = musb_dmamask; 717 musb->dev.coherent_dma_mask = musb_dmamask;
740 musb->dev.of_node = of_node_get(dn);
741 718
742 glue->musb = musb; 719 glue->musb = musb;
743 720
@@ -802,6 +779,9 @@ static int dsps_probe(struct platform_device *pdev)
802 } 779 }
803 wrp = match->data; 780 wrp = match->data;
804 781
782 if (of_device_is_compatible(pdev->dev.of_node, "ti,musb-dm816"))
783 dsps_ops.read_fifo = dsps_read_fifo32;
784
805 /* allocate glue */ 785 /* allocate glue */
806 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); 786 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
807 if (!glue) 787 if (!glue)
@@ -873,12 +853,14 @@ static const struct dsps_musb_wrapper am33xx_driver_data = {
873 .rxep_shift = 16, 853 .rxep_shift = 16,
874 .rxep_mask = 0xfffe, 854 .rxep_mask = 0xfffe,
875 .rxep_bitmap = (0xfffe << 16), 855 .rxep_bitmap = (0xfffe << 16),
876 .poll_seconds = 2, 856 .poll_timeout = 2000, /* ms */
877}; 857};
878 858
879static const struct of_device_id musb_dsps_of_match[] = { 859static const struct of_device_id musb_dsps_of_match[] = {
880 { .compatible = "ti,musb-am33xx", 860 { .compatible = "ti,musb-am33xx",
881 .data = (void *) &am33xx_driver_data, }, 861 .data = &am33xx_driver_data, },
862 { .compatible = "ti,musb-dm816",
863 .data = &am33xx_driver_data, },
882 { }, 864 { },
883}; 865};
884MODULE_DEVICE_TABLE(of, musb_dsps_of_match); 866MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
@@ -929,7 +911,8 @@ static int dsps_resume(struct device *dev)
929 dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode); 911 dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
930 if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 912 if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
931 musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 913 musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
932 mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 914 mod_timer(&glue->timer, jiffies +
915 msecs_to_jiffies(wrp->poll_timeout));
933 916
934 return 0; 917 return 0;
935} 918}
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index b2d9040c7685..4c481cd66c77 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -1876,44 +1876,6 @@ err:
1876 return retval; 1876 return retval;
1877} 1877}
1878 1878
1879static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1880{
1881 int i;
1882 struct musb_hw_ep *hw_ep;
1883
1884 /* don't disconnect if it's not connected */
1885 if (musb->g.speed == USB_SPEED_UNKNOWN)
1886 driver = NULL;
1887 else
1888 musb->g.speed = USB_SPEED_UNKNOWN;
1889
1890 /* deactivate the hardware */
1891 if (musb->softconnect) {
1892 musb->softconnect = 0;
1893 musb_pullup(musb, 0);
1894 }
1895 musb_stop(musb);
1896
1897 /* killing any outstanding requests will quiesce the driver;
1898 * then report disconnect
1899 */
1900 if (driver) {
1901 for (i = 0, hw_ep = musb->endpoints;
1902 i < musb->nr_endpoints;
1903 i++, hw_ep++) {
1904 musb_ep_select(musb->mregs, i);
1905 if (hw_ep->is_shared_fifo /* || !epnum */) {
1906 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1907 } else {
1908 if (hw_ep->max_packet_sz_tx)
1909 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1910 if (hw_ep->max_packet_sz_rx)
1911 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1912 }
1913 }
1914 }
1915}
1916
1917/* 1879/*
1918 * Unregister the gadget driver. Used by gadget drivers when 1880 * Unregister the gadget driver. Used by gadget drivers when
1919 * unregistering themselves from the controller. 1881 * unregistering themselves from the controller.
@@ -1940,7 +1902,7 @@ static int musb_gadget_stop(struct usb_gadget *g)
1940 (void) musb_gadget_vbus_draw(&musb->g, 0); 1902 (void) musb_gadget_vbus_draw(&musb->g, 0);
1941 1903
1942 musb->xceiv->otg->state = OTG_STATE_UNDEFINED; 1904 musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
1943 stop_activity(musb, NULL); 1905 musb_stop(musb);
1944 otg_set_peripheral(musb->xceiv->otg, NULL); 1906 otg_set_peripheral(musb->xceiv->otg, NULL);
1945 1907
1946 musb->is_active = 0; 1908 musb->is_active = 0;
diff --git a/drivers/usb/musb/musb_virthub.c b/drivers/usb/musb/musb_virthub.c
index 294e159f4afe..86c4b533e90b 100644
--- a/drivers/usb/musb/musb_virthub.c
+++ b/drivers/usb/musb/musb_virthub.c
@@ -136,7 +136,7 @@ void musb_port_suspend(struct musb *musb, bool do_suspend)
136 /* later, GetPortStatus will stop RESUME signaling */ 136 /* later, GetPortStatus will stop RESUME signaling */
137 musb->port1_status |= MUSB_PORT_STAT_RESUME; 137 musb->port1_status |= MUSB_PORT_STAT_RESUME;
138 schedule_delayed_work(&musb->finish_resume_work, 138 schedule_delayed_work(&musb->finish_resume_work,
139 msecs_to_jiffies(20)); 139 msecs_to_jiffies(USB_RESUME_TIMEOUT));
140 } 140 }
141} 141}
142 142
@@ -345,7 +345,7 @@ int musb_hub_control(
345 struct usb_hub_descriptor *desc = (void *)buf; 345 struct usb_hub_descriptor *desc = (void *)buf;
346 346
347 desc->bDescLength = 9; 347 desc->bDescLength = 9;
348 desc->bDescriptorType = 0x29; 348 desc->bDescriptorType = USB_DT_HUB;
349 desc->bNbrPorts = 1; 349 desc->bNbrPorts = 1;
350 desc->wHubCharacteristics = cpu_to_le16( 350 desc->wHubCharacteristics = cpu_to_le16(
351 HUB_CHAR_INDV_PORT_LPSM /* per-port power switching */ 351 HUB_CHAR_INDV_PORT_LPSM /* per-port power switching */
diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index 52d3d58252e1..2175678e674e 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -139,7 +139,7 @@ config USB_ISP1301
139 139
140config USB_MSM_OTG 140config USB_MSM_OTG
141 tristate "Qualcomm on-chip USB OTG controller support" 141 tristate "Qualcomm on-chip USB OTG controller support"
142 depends on (USB || USB_GADGET) && (ARCH_MSM || ARCH_QCOM || COMPILE_TEST) 142 depends on (USB || USB_GADGET) && (ARCH_QCOM || COMPILE_TEST)
143 depends on RESET_CONTROLLER 143 depends on RESET_CONTROLLER
144 select USB_PHY 144 select USB_PHY
145 help 145 help
@@ -202,13 +202,13 @@ config USB_RCAR_GEN2_PHY
202config USB_ULPI 202config USB_ULPI
203 bool "Generic ULPI Transceiver Driver" 203 bool "Generic ULPI Transceiver Driver"
204 depends on ARM || ARM64 204 depends on ARM || ARM64
205 select USB_ULPI_VIEWPORT
205 help 206 help
206 Enable this to support ULPI connected USB OTG transceivers which 207 Enable this to support ULPI connected USB OTG transceivers which
207 are likely found on embedded boards. 208 are likely found on embedded boards.
208 209
209config USB_ULPI_VIEWPORT 210config USB_ULPI_VIEWPORT
210 bool 211 bool
211 depends on USB_ULPI
212 help 212 help
213 Provides read/write operations to the ULPI phy register set for 213 Provides read/write operations to the ULPI phy register set for
214 controllers with a viewport register (e.g. Chipidea/ARC controllers). 214 controllers with a viewport register (e.g. Chipidea/ARC controllers).
diff --git a/drivers/usb/phy/of.c b/drivers/usb/phy/of.c
index 7ea0154da9d5..66ffa82457a8 100644
--- a/drivers/usb/phy/of.c
+++ b/drivers/usb/phy/of.c
@@ -27,7 +27,7 @@ static const char *const usbphy_modes[] = {
27 * @np: Pointer to the given device_node 27 * @np: Pointer to the given device_node
28 * 28 *
29 * The function gets phy interface string from property 'phy_type', 29 * The function gets phy interface string from property 'phy_type',
30 * and returns the correspondig enum usb_phy_interface 30 * and returns the corresponding enum usb_phy_interface
31 */ 31 */
32enum usb_phy_interface of_usb_get_phy_mode(struct device_node *np) 32enum usb_phy_interface of_usb_get_phy_mode(struct device_node *np)
33{ 33{
diff --git a/drivers/usb/phy/phy-ab8500-usb.c b/drivers/usb/phy/phy-ab8500-usb.c
index 0b1bd2369293..7225d526df04 100644
--- a/drivers/usb/phy/phy-ab8500-usb.c
+++ b/drivers/usb/phy/phy-ab8500-usb.c
@@ -277,7 +277,7 @@ static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
277 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n", 277 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
278 ret); 278 ret);
279 279
280 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000); 280 ret = regulator_set_load(ab->v_ulpi, 28000);
281 if (ret < 0) 281 if (ret < 0)
282 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n", 282 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
283 ret); 283 ret);
@@ -317,7 +317,7 @@ static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
317 ab->saved_v_ulpi, ret); 317 ab->saved_v_ulpi, ret);
318 } 318 }
319 319
320 ret = regulator_set_optimum_mode(ab->v_ulpi, 0); 320 ret = regulator_set_load(ab->v_ulpi, 0);
321 if (ret < 0) 321 if (ret < 0)
322 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n", 322 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
323 ret); 323 ret);
@@ -893,7 +893,7 @@ static int abx500_usb_link_status_update(struct ab8500_usb *ab)
893 893
894/* 894/*
895 * Disconnection Sequence: 895 * Disconnection Sequence:
896 * 1. Disconect Interrupt 896 * 1. Disconnect Interrupt
897 * 2. Disable regulators 897 * 2. Disable regulators
898 * 3. Disable AB clock 898 * 3. Disable AB clock
899 * 4. Disable the Phy 899 * 4. Disable the Phy
diff --git a/drivers/usb/phy/phy-am335x-control.c b/drivers/usb/phy/phy-am335x-control.c
index 403fab772724..7b3035ff9434 100644
--- a/drivers/usb/phy/phy-am335x-control.c
+++ b/drivers/usb/phy/phy-am335x-control.c
@@ -126,6 +126,9 @@ struct phy_control *am335x_get_phy_control(struct device *dev)
126 return NULL; 126 return NULL;
127 127
128 dev = bus_find_device(&platform_bus_type, NULL, node, match); 128 dev = bus_find_device(&platform_bus_type, NULL, node, match);
129 if (!dev)
130 return NULL;
131
129 ctrl_usb = dev_get_drvdata(dev); 132 ctrl_usb = dev_get_drvdata(dev);
130 if (!ctrl_usb) 133 if (!ctrl_usb)
131 return NULL; 134 return NULL;
diff --git a/drivers/usb/phy/phy-generic.c b/drivers/usb/phy/phy-generic.c
index 70be50b734b2..deee68eafb72 100644
--- a/drivers/usb/phy/phy-generic.c
+++ b/drivers/usb/phy/phy-generic.c
@@ -62,14 +62,14 @@ static int nop_set_suspend(struct usb_phy *x, int suspend)
62 return 0; 62 return 0;
63} 63}
64 64
65static void nop_reset_set(struct usb_phy_generic *nop, int asserted) 65static void nop_reset(struct usb_phy_generic *nop)
66{ 66{
67 if (!nop->gpiod_reset) 67 if (!nop->gpiod_reset)
68 return; 68 return;
69 69
70 gpiod_direction_output(nop->gpiod_reset, !asserted); 70 gpiod_set_value(nop->gpiod_reset, 1);
71 usleep_range(10000, 20000); 71 usleep_range(10000, 20000);
72 gpiod_set_value(nop->gpiod_reset, asserted); 72 gpiod_set_value(nop->gpiod_reset, 0);
73} 73}
74 74
75/* interface to regulator framework */ 75/* interface to regulator framework */
@@ -151,8 +151,7 @@ int usb_gen_phy_init(struct usb_phy *phy)
151 if (!IS_ERR(nop->clk)) 151 if (!IS_ERR(nop->clk))
152 clk_prepare_enable(nop->clk); 152 clk_prepare_enable(nop->clk);
153 153
154 /* De-assert RESET */ 154 nop_reset(nop);
155 nop_reset_set(nop, 0);
156 155
157 return 0; 156 return 0;
158} 157}
@@ -162,8 +161,7 @@ void usb_gen_phy_shutdown(struct usb_phy *phy)
162{ 161{
163 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev); 162 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
164 163
165 /* Assert RESET */ 164 gpiod_set_value(nop->gpiod_reset, 1);
166 nop_reset_set(nop, 1);
167 165
168 if (!IS_ERR(nop->clk)) 166 if (!IS_ERR(nop->clk))
169 clk_disable_unprepare(nop->clk); 167 clk_disable_unprepare(nop->clk);
diff --git a/drivers/usb/phy/phy-msm-usb.c b/drivers/usb/phy/phy-msm-usb.c
index 000fd892455f..c9156beeadef 100644
--- a/drivers/usb/phy/phy-msm-usb.c
+++ b/drivers/usb/phy/phy-msm-usb.c
@@ -142,27 +142,22 @@ static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
142 int ret = 0; 142 int ret = 0;
143 143
144 if (on) { 144 if (on) {
145 ret = regulator_set_optimum_mode(motg->v1p8, 145 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
146 USB_PHY_1P8_HPM_LOAD);
147 if (ret < 0) { 146 if (ret < 0) {
148 pr_err("Could not set HPM for v1p8\n"); 147 pr_err("Could not set HPM for v1p8\n");
149 return ret; 148 return ret;
150 } 149 }
151 ret = regulator_set_optimum_mode(motg->v3p3, 150 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
152 USB_PHY_3P3_HPM_LOAD);
153 if (ret < 0) { 151 if (ret < 0) {
154 pr_err("Could not set HPM for v3p3\n"); 152 pr_err("Could not set HPM for v3p3\n");
155 regulator_set_optimum_mode(motg->v1p8, 153 regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
156 USB_PHY_1P8_LPM_LOAD);
157 return ret; 154 return ret;
158 } 155 }
159 } else { 156 } else {
160 ret = regulator_set_optimum_mode(motg->v1p8, 157 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
161 USB_PHY_1P8_LPM_LOAD);
162 if (ret < 0) 158 if (ret < 0)
163 pr_err("Could not set LPM for v1p8\n"); 159 pr_err("Could not set LPM for v1p8\n");
164 ret = regulator_set_optimum_mode(motg->v3p3, 160 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
165 USB_PHY_3P3_LPM_LOAD);
166 if (ret < 0) 161 if (ret < 0)
167 pr_err("Could not set LPM for v3p3\n"); 162 pr_err("Could not set LPM for v3p3\n");
168 } 163 }
@@ -263,9 +258,7 @@ static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
263{ 258{
264 int ret; 259 int ret;
265 260
266 if (motg->pdata->link_clk_reset) 261 if (assert)
267 ret = motg->pdata->link_clk_reset(motg->clk, assert);
268 else if (assert)
269 ret = reset_control_assert(motg->link_rst); 262 ret = reset_control_assert(motg->link_rst);
270 else 263 else
271 ret = reset_control_deassert(motg->link_rst); 264 ret = reset_control_deassert(motg->link_rst);
@@ -281,9 +274,7 @@ static int msm_otg_phy_clk_reset(struct msm_otg *motg)
281{ 274{
282 int ret = 0; 275 int ret = 0;
283 276
284 if (motg->pdata->phy_clk_reset) 277 if (motg->phy_rst)
285 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
286 else if (motg->phy_rst)
287 ret = reset_control_reset(motg->phy_rst); 278 ret = reset_control_reset(motg->phy_rst);
288 279
289 if (ret) 280 if (ret)
@@ -1551,16 +1542,6 @@ static int msm_otg_probe(struct platform_device *pdev)
1551 phy = &motg->phy; 1542 phy = &motg->phy;
1552 phy->dev = &pdev->dev; 1543 phy->dev = &pdev->dev;
1553 1544
1554 if (motg->pdata->phy_clk_reset) {
1555 motg->phy_reset_clk = devm_clk_get(&pdev->dev,
1556 np ? "phy" : "usb_phy_clk");
1557
1558 if (IS_ERR(motg->phy_reset_clk)) {
1559 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
1560 return PTR_ERR(motg->phy_reset_clk);
1561 }
1562 }
1563
1564 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk"); 1545 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
1565 if (IS_ERR(motg->clk)) { 1546 if (IS_ERR(motg->clk)) {
1566 dev_err(&pdev->dev, "failed to get usb_hs_clk\n"); 1547 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
diff --git a/drivers/usb/phy/phy-rcar-gen2-usb.c b/drivers/usb/phy/phy-rcar-gen2-usb.c
index f83808413ba2..f81800b6562a 100644
--- a/drivers/usb/phy/phy-rcar-gen2-usb.c
+++ b/drivers/usb/phy/phy-rcar-gen2-usb.c
@@ -47,7 +47,7 @@ struct rcar_gen2_usb_phy_priv {
47 47
48/* USB General status register */ 48/* USB General status register */
49#define USBHS_UGSTS_REG 0x88 49#define USBHS_UGSTS_REG 0x88
50#define USBHS_UGSTS_LOCK (3 << 8) 50#define USBHS_UGSTS_LOCK (1 << 8)
51 51
52/* Enable USBHS internal phy */ 52/* Enable USBHS internal phy */
53static int __rcar_gen2_usbhs_phy_enable(void __iomem *base) 53static int __rcar_gen2_usbhs_phy_enable(void __iomem *base)
diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 2f9735b35338..d1cd6b50f520 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -81,7 +81,9 @@ static void devm_usb_phy_release(struct device *dev, void *res)
81 81
82static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) 82static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
83{ 83{
84 return res == match_data; 84 struct usb_phy **phy = res;
85
86 return *phy == match_data;
85} 87}
86 88
87/** 89/**
diff --git a/drivers/usb/renesas_usbhs/common.c b/drivers/usb/renesas_usbhs/common.c
index 4cf77d3c3bd2..0f7e850fd4aa 100644
--- a/drivers/usb/renesas_usbhs/common.c
+++ b/drivers/usb/renesas_usbhs/common.c
@@ -276,6 +276,16 @@ int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
276} 276}
277 277
278/* 278/*
279 * interrupt functions
280 */
281void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
282{
283 u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
284
285 usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
286}
287
288/*
279 * local functions 289 * local functions
280 */ 290 */
281static void usbhsc_set_buswait(struct usbhs_priv *priv) 291static void usbhsc_set_buswait(struct usbhs_priv *priv)
@@ -487,6 +497,15 @@ static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
487 if (gpio > 0) 497 if (gpio > 0)
488 dparam->enable_gpio = gpio; 498 dparam->enable_gpio = gpio;
489 499
500 switch (dparam->type) {
501 case USBHS_TYPE_R8A7790:
502 case USBHS_TYPE_R8A7791:
503 dparam->has_usb_dmac = 1;
504 break;
505 default:
506 break;
507 }
508
490 return info; 509 return info;
491} 510}
492 511
diff --git a/drivers/usb/renesas_usbhs/common.h b/drivers/usb/renesas_usbhs/common.h
index fc96e924edc4..8c5fc12ad778 100644
--- a/drivers/usb/renesas_usbhs/common.h
+++ b/drivers/usb/renesas_usbhs/common.h
@@ -193,6 +193,7 @@ struct usbhs_priv;
193#define TYPE_BULK (1 << 14) 193#define TYPE_BULK (1 << 14)
194#define TYPE_INT (2 << 14) 194#define TYPE_INT (2 << 14)
195#define TYPE_ISO (3 << 14) 195#define TYPE_ISO (3 << 14)
196#define BFRE (1 << 10) /* BRDY Interrupt Operation Spec. */
196#define DBLB (1 << 9) /* Double Buffer Mode */ 197#define DBLB (1 << 9) /* Double Buffer Mode */
197#define SHTNAK (1 << 7) /* Pipe Disable in Transfer End */ 198#define SHTNAK (1 << 7) /* Pipe Disable in Transfer End */
198#define DIR_OUT (1 << 4) /* Transfer Direction */ 199#define DIR_OUT (1 << 4) /* Transfer Direction */
@@ -216,6 +217,7 @@ struct usbhs_priv;
216#define ACLRM (1 << 9) /* Buffer Auto-Clear Mode */ 217#define ACLRM (1 << 9) /* Buffer Auto-Clear Mode */
217#define SQCLR (1 << 8) /* Toggle Bit Clear */ 218#define SQCLR (1 << 8) /* Toggle Bit Clear */
218#define SQSET (1 << 7) /* Toggle Bit Set */ 219#define SQSET (1 << 7) /* Toggle Bit Set */
220#define SQMON (1 << 6) /* Toggle Bit Check */
219#define PBUSY (1 << 5) /* Pipe Busy */ 221#define PBUSY (1 << 5) /* Pipe Busy */
220#define PID_MASK (0x3) /* Response PID */ 222#define PID_MASK (0x3) /* Response PID */
221#define PID_NAK 0 223#define PID_NAK 0
@@ -324,6 +326,11 @@ int usbhs_set_device_config(struct usbhs_priv *priv, int devnum, u16 upphub,
324 u16 hubport, u16 speed); 326 u16 hubport, u16 speed);
325 327
326/* 328/*
329 * interrupt functions
330 */
331void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit);
332
333/*
327 * data 334 * data
328 */ 335 */
329struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev); 336struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev);
diff --git a/drivers/usb/renesas_usbhs/fifo.c b/drivers/usb/renesas_usbhs/fifo.c
index d891bff39d66..8597cf9cfceb 100644
--- a/drivers/usb/renesas_usbhs/fifo.c
+++ b/drivers/usb/renesas_usbhs/fifo.c
@@ -813,7 +813,8 @@ static void xfer_work(struct work_struct *work)
813 desc->callback = usbhsf_dma_complete; 813 desc->callback = usbhsf_dma_complete;
814 desc->callback_param = pipe; 814 desc->callback_param = pipe;
815 815
816 if (dmaengine_submit(desc) < 0) { 816 pkt->cookie = dmaengine_submit(desc);
817 if (pkt->cookie < 0) {
817 dev_err(dev, "Failed to submit dma descriptor\n"); 818 dev_err(dev, "Failed to submit dma descriptor\n");
818 return; 819 return;
819 } 820 }
@@ -822,10 +823,10 @@ static void xfer_work(struct work_struct *work)
822 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero); 823 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
823 824
824 usbhs_pipe_running(pipe, 1); 825 usbhs_pipe_running(pipe, 1);
825 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
826 usbhs_pipe_enable(pipe);
827 usbhsf_dma_start(pipe, fifo); 826 usbhsf_dma_start(pipe, fifo);
827 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
828 dma_async_issue_pending(chan); 828 dma_async_issue_pending(chan);
829 usbhs_pipe_enable(pipe);
829} 830}
830 831
831/* 832/*
@@ -838,6 +839,7 @@ static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
838 struct usbhs_fifo *fifo; 839 struct usbhs_fifo *fifo;
839 int len = pkt->length - pkt->actual; 840 int len = pkt->length - pkt->actual;
840 int ret; 841 int ret;
842 uintptr_t align_mask;
841 843
842 if (usbhs_pipe_is_busy(pipe)) 844 if (usbhs_pipe_is_busy(pipe))
843 return 0; 845 return 0;
@@ -847,10 +849,14 @@ static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
847 usbhs_pipe_is_dcp(pipe)) 849 usbhs_pipe_is_dcp(pipe))
848 goto usbhsf_pio_prepare_push; 850 goto usbhsf_pio_prepare_push;
849 851
850 if (len & 0x7) /* 8byte alignment */ 852 /* check data length if this driver don't use USB-DMAC */
853 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
851 goto usbhsf_pio_prepare_push; 854 goto usbhsf_pio_prepare_push;
852 855
853 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ 856 /* check buffer alignment */
857 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
858 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
859 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
854 goto usbhsf_pio_prepare_push; 860 goto usbhsf_pio_prepare_push;
855 861
856 /* return at this time if the pipe is running */ 862 /* return at this time if the pipe is running */
@@ -924,7 +930,85 @@ struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
924/* 930/*
925 * DMA pop handler 931 * DMA pop handler
926 */ 932 */
927static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done) 933
934static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
935 int *is_done)
936{
937 return usbhsf_prepare_pop(pkt, is_done);
938}
939
940static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
941 int *is_done)
942{
943 struct usbhs_pipe *pipe = pkt->pipe;
944 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
945 struct usbhs_fifo *fifo;
946 int ret;
947
948 if (usbhs_pipe_is_busy(pipe))
949 return 0;
950
951 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
952 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
953 usbhs_pipe_is_dcp(pipe))
954 goto usbhsf_pio_prepare_pop;
955
956 fifo = usbhsf_get_dma_fifo(priv, pkt);
957 if (!fifo)
958 goto usbhsf_pio_prepare_pop;
959
960 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
961 goto usbhsf_pio_prepare_pop;
962
963 usbhs_pipe_config_change_bfre(pipe, 1);
964
965 ret = usbhsf_fifo_select(pipe, fifo, 0);
966 if (ret < 0)
967 goto usbhsf_pio_prepare_pop;
968
969 if (usbhsf_dma_map(pkt) < 0)
970 goto usbhsf_pio_prepare_pop_unselect;
971
972 /* DMA */
973
974 /*
975 * usbhs_fifo_dma_pop_handler :: prepare
976 * enabled irq to come here.
977 * but it is no longer needed for DMA. disable it.
978 */
979 usbhsf_rx_irq_ctrl(pipe, 0);
980
981 pkt->trans = pkt->length;
982
983 INIT_WORK(&pkt->work, xfer_work);
984 schedule_work(&pkt->work);
985
986 return 0;
987
988usbhsf_pio_prepare_pop_unselect:
989 usbhsf_fifo_unselect(pipe, fifo);
990usbhsf_pio_prepare_pop:
991
992 /*
993 * change handler to PIO
994 */
995 pkt->handler = &usbhs_fifo_pio_pop_handler;
996 usbhs_pipe_config_change_bfre(pipe, 0);
997
998 return pkt->handler->prepare(pkt, is_done);
999}
1000
1001static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1002{
1003 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1004
1005 if (usbhs_get_dparam(priv, has_usb_dmac))
1006 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1007 else
1008 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1009}
1010
1011static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
928{ 1012{
929 struct usbhs_pipe *pipe = pkt->pipe; 1013 struct usbhs_pipe *pipe = pkt->pipe;
930 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 1014 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
@@ -993,7 +1077,16 @@ usbhsf_pio_prepare_pop:
993 return pkt->handler->try_run(pkt, is_done); 1077 return pkt->handler->try_run(pkt, is_done);
994} 1078}
995 1079
996static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done) 1080static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1081{
1082 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1083
1084 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1085
1086 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1087}
1088
1089static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
997{ 1090{
998 struct usbhs_pipe *pipe = pkt->pipe; 1091 struct usbhs_pipe *pipe = pkt->pipe;
999 int maxp = usbhs_pipe_get_maxpacket(pipe); 1092 int maxp = usbhs_pipe_get_maxpacket(pipe);
@@ -1017,8 +1110,68 @@ static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1017 return 0; 1110 return 0;
1018} 1111}
1019 1112
1113static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1114 struct dma_chan *chan, int dtln)
1115{
1116 struct usbhs_pipe *pipe = pkt->pipe;
1117 struct dma_tx_state state;
1118 size_t received_size;
1119 int maxp = usbhs_pipe_get_maxpacket(pipe);
1120
1121 dmaengine_tx_status(chan, pkt->cookie, &state);
1122 received_size = pkt->length - state.residue;
1123
1124 if (dtln) {
1125 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1126 received_size &= ~(maxp - 1);
1127 received_size += dtln;
1128 }
1129
1130 return received_size;
1131}
1132
1133static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1134 int *is_done)
1135{
1136 struct usbhs_pipe *pipe = pkt->pipe;
1137 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1138 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1139 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1140 int rcv_len;
1141
1142 /*
1143 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1144 * cannot the BRDYSTS. So, the function clears it here because the
1145 * driver may use PIO mode next time.
1146 */
1147 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1148
1149 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1150 usbhsf_fifo_clear(pipe, fifo);
1151 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1152
1153 usbhsf_dma_stop(pipe, fifo);
1154 usbhsf_dma_unmap(pkt);
1155 usbhsf_fifo_unselect(pipe, pipe->fifo);
1156
1157 /* The driver can assume the rx transaction is always "done" */
1158 *is_done = 1;
1159
1160 return 0;
1161}
1162
1163static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1164{
1165 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1166
1167 if (usbhs_get_dparam(priv, has_usb_dmac))
1168 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1169 else
1170 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1171}
1172
1020struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = { 1173struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1021 .prepare = usbhsf_prepare_pop, 1174 .prepare = usbhsf_dma_prepare_pop,
1022 .try_run = usbhsf_dma_try_pop, 1175 .try_run = usbhsf_dma_try_pop,
1023 .dma_done = usbhsf_dma_pop_done 1176 .dma_done = usbhsf_dma_pop_done
1024}; 1177};
@@ -1069,23 +1222,29 @@ static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1069 &fifo->rx_slave); 1222 &fifo->rx_slave);
1070} 1223}
1071 1224
1072static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo) 1225static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1226 int channel)
1073{ 1227{
1074 fifo->tx_chan = dma_request_slave_channel_reason(dev, "tx"); 1228 char name[16];
1229
1230 snprintf(name, sizeof(name), "tx%d", channel);
1231 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1075 if (IS_ERR(fifo->tx_chan)) 1232 if (IS_ERR(fifo->tx_chan))
1076 fifo->tx_chan = NULL; 1233 fifo->tx_chan = NULL;
1077 fifo->rx_chan = dma_request_slave_channel_reason(dev, "rx"); 1234
1235 snprintf(name, sizeof(name), "rx%d", channel);
1236 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1078 if (IS_ERR(fifo->rx_chan)) 1237 if (IS_ERR(fifo->rx_chan))
1079 fifo->rx_chan = NULL; 1238 fifo->rx_chan = NULL;
1080} 1239}
1081 1240
1082static void usbhsf_dma_init(struct usbhs_priv *priv, 1241static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1083 struct usbhs_fifo *fifo) 1242 int channel)
1084{ 1243{
1085 struct device *dev = usbhs_priv_to_dev(priv); 1244 struct device *dev = usbhs_priv_to_dev(priv);
1086 1245
1087 if (dev->of_node) 1246 if (dev->of_node)
1088 usbhsf_dma_init_dt(dev, fifo); 1247 usbhsf_dma_init_dt(dev, fifo, channel);
1089 else 1248 else
1090 usbhsf_dma_init_pdev(fifo); 1249 usbhsf_dma_init_pdev(fifo);
1091 1250
@@ -1231,7 +1390,7 @@ do { \
1231 usbhs_get_dparam(priv, d##channel##_tx_id); \ 1390 usbhs_get_dparam(priv, d##channel##_tx_id); \
1232 fifo->rx_slave.shdma_slave.slave_id = \ 1391 fifo->rx_slave.shdma_slave.slave_id = \
1233 usbhs_get_dparam(priv, d##channel##_rx_id); \ 1392 usbhs_get_dparam(priv, d##channel##_rx_id); \
1234 usbhsf_dma_init(priv, fifo); \ 1393 usbhsf_dma_init(priv, fifo, channel); \
1235} while (0) 1394} while (0)
1236 1395
1237#define USBHS_DFIFO_INIT(priv, fifo, channel) \ 1396#define USBHS_DFIFO_INIT(priv, fifo, channel) \
diff --git a/drivers/usb/renesas_usbhs/fifo.h b/drivers/usb/renesas_usbhs/fifo.h
index f07037c1185f..04d3f8abad9e 100644
--- a/drivers/usb/renesas_usbhs/fifo.h
+++ b/drivers/usb/renesas_usbhs/fifo.h
@@ -58,6 +58,7 @@ struct usbhs_pkt {
58 struct usbhs_pkt *pkt); 58 struct usbhs_pkt *pkt);
59 struct work_struct work; 59 struct work_struct work;
60 dma_addr_t dma; 60 dma_addr_t dma;
61 dma_cookie_t cookie;
61 void *buf; 62 void *buf;
62 int length; 63 int length;
63 int trans; 64 int trans;
diff --git a/drivers/usb/renesas_usbhs/mod_gadget.c b/drivers/usb/renesas_usbhs/mod_gadget.c
index e0384af77e56..dc2aa3261202 100644
--- a/drivers/usb/renesas_usbhs/mod_gadget.c
+++ b/drivers/usb/renesas_usbhs/mod_gadget.c
@@ -119,18 +119,34 @@ struct usbhsg_recip_handle {
119/* 119/*
120 * queue push/pop 120 * queue push/pop
121 */ 121 */
122static void usbhsg_queue_pop(struct usbhsg_uep *uep, 122static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
123 struct usbhsg_request *ureq, 123 struct usbhsg_request *ureq,
124 int status) 124 int status)
125{ 125{
126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
128 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 128 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
129 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
129 130
130 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe)); 131 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
131 132
132 ureq->req.status = status; 133 ureq->req.status = status;
134 spin_unlock(usbhs_priv_to_lock(priv));
133 usb_gadget_giveback_request(&uep->ep, &ureq->req); 135 usb_gadget_giveback_request(&uep->ep, &ureq->req);
136 spin_lock(usbhs_priv_to_lock(priv));
137}
138
139static void usbhsg_queue_pop(struct usbhsg_uep *uep,
140 struct usbhsg_request *ureq,
141 int status)
142{
143 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
144 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
145 unsigned long flags;
146
147 usbhs_lock(priv, flags);
148 __usbhsg_queue_pop(uep, ureq, status);
149 usbhs_unlock(priv, flags);
134} 150}
135 151
136static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) 152static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
diff --git a/drivers/usb/renesas_usbhs/mod_host.c b/drivers/usb/renesas_usbhs/mod_host.c
index 96eead619282..bd050359926c 100644
--- a/drivers/usb/renesas_usbhs/mod_host.c
+++ b/drivers/usb/renesas_usbhs/mod_host.c
@@ -1229,7 +1229,7 @@ static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1229 break; 1229 break;
1230 1230
1231 case GetHubDescriptor: 1231 case GetHubDescriptor:
1232 desc->bDescriptorType = 0x29; 1232 desc->bDescriptorType = USB_DT_HUB;
1233 desc->bHubContrCurrent = 0; 1233 desc->bHubContrCurrent = 0;
1234 desc->bNbrPorts = roothub_id; 1234 desc->bNbrPorts = roothub_id;
1235 desc->bDescLength = 9; 1235 desc->bDescLength = 9;
diff --git a/drivers/usb/renesas_usbhs/pipe.c b/drivers/usb/renesas_usbhs/pipe.c
index 007f45abe96c..4f9c3356127a 100644
--- a/drivers/usb/renesas_usbhs/pipe.c
+++ b/drivers/usb/renesas_usbhs/pipe.c
@@ -84,6 +84,17 @@ static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
84 usbhs_bset(priv, pipe_reg, mask, val); 84 usbhs_bset(priv, pipe_reg, mask, val);
85} 85}
86 86
87static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
88 u16 dcp_reg, u16 pipe_reg)
89{
90 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
91
92 if (usbhs_pipe_is_dcp(pipe))
93 return usbhs_read(priv, dcp_reg);
94 else
95 return usbhs_read(priv, pipe_reg);
96}
97
87/* 98/*
88 * DCPCFG/PIPECFG functions 99 * DCPCFG/PIPECFG functions
89 */ 100 */
@@ -92,6 +103,11 @@ static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
92 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val); 103 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
93} 104}
94 105
106static u16 usbhsp_pipe_cfg_get(struct usbhs_pipe *pipe)
107{
108 return __usbhsp_pipe_xxx_get(pipe, DCPCFG, PIPECFG);
109}
110
95/* 111/*
96 * PIPEnTRN/PIPEnTRE functions 112 * PIPEnTRN/PIPEnTRE functions
97 */ 113 */
@@ -616,6 +632,11 @@ void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int sequence)
616 usbhsp_pipectrl_set(pipe, mask, val); 632 usbhsp_pipectrl_set(pipe, mask, val);
617} 633}
618 634
635static int usbhs_pipe_get_data_sequence(struct usbhs_pipe *pipe)
636{
637 return !!(usbhsp_pipectrl_get(pipe) & SQMON);
638}
639
619void usbhs_pipe_clear(struct usbhs_pipe *pipe) 640void usbhs_pipe_clear(struct usbhs_pipe *pipe)
620{ 641{
621 if (usbhs_pipe_is_dcp(pipe)) { 642 if (usbhs_pipe_is_dcp(pipe)) {
@@ -626,6 +647,24 @@ void usbhs_pipe_clear(struct usbhs_pipe *pipe)
626 } 647 }
627} 648}
628 649
650void usbhs_pipe_config_change_bfre(struct usbhs_pipe *pipe, int enable)
651{
652 int sequence;
653
654 if (usbhs_pipe_is_dcp(pipe))
655 return;
656
657 usbhsp_pipe_select(pipe);
658 /* check if the driver needs to change the BFRE value */
659 if (!(enable ^ !!(usbhsp_pipe_cfg_get(pipe) & BFRE)))
660 return;
661
662 sequence = usbhs_pipe_get_data_sequence(pipe);
663 usbhsp_pipe_cfg_set(pipe, BFRE, enable ? BFRE : 0);
664 usbhs_pipe_clear(pipe);
665 usbhs_pipe_data_sequence(pipe, sequence);
666}
667
629static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type) 668static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
630{ 669{
631 struct usbhs_pipe *pos, *pipe; 670 struct usbhs_pipe *pos, *pipe;
diff --git a/drivers/usb/renesas_usbhs/pipe.h b/drivers/usb/renesas_usbhs/pipe.h
index d24a05972370..b0bc7b603016 100644
--- a/drivers/usb/renesas_usbhs/pipe.h
+++ b/drivers/usb/renesas_usbhs/pipe.h
@@ -97,6 +97,7 @@ void usbhs_pipe_set_trans_count_if_bulk(struct usbhs_pipe *pipe, int len);
97void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo); 97void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo);
98void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel, 98void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
99 u16 epnum, u16 maxp); 99 u16 epnum, u16 maxp);
100void usbhs_pipe_config_change_bfre(struct usbhs_pipe *pipe, int enable);
100 101
101#define usbhs_pipe_sequence_data0(pipe) usbhs_pipe_data_sequence(pipe, 0) 102#define usbhs_pipe_sequence_data0(pipe) usbhs_pipe_data_sequence(pipe, 0)
102#define usbhs_pipe_sequence_data1(pipe) usbhs_pipe_data_sequence(pipe, 1) 103#define usbhs_pipe_sequence_data1(pipe) usbhs_pipe_data_sequence(pipe, 1)
diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index ede4f5fcfadd..c73808f095bb 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -325,7 +325,6 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
325 if (r) { 325 if (r) {
326 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", 326 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
327 __func__, r); 327 __func__, r);
328 ch341_close(port);
329 goto out; 328 goto out;
330 } 329 }
331 330
diff --git a/drivers/usb/serial/f81232.c b/drivers/usb/serial/f81232.c
index c5dc233db2d9..972f5a5fe577 100644
--- a/drivers/usb/serial/f81232.c
+++ b/drivers/usb/serial/f81232.c
@@ -19,10 +19,11 @@
19#include <linux/serial.h> 19#include <linux/serial.h>
20#include <linux/module.h> 20#include <linux/module.h>
21#include <linux/moduleparam.h> 21#include <linux/moduleparam.h>
22#include <linux/spinlock.h> 22#include <linux/mutex.h>
23#include <linux/uaccess.h> 23#include <linux/uaccess.h>
24#include <linux/usb.h> 24#include <linux/usb.h>
25#include <linux/usb/serial.h> 25#include <linux/usb/serial.h>
26#include <linux/serial_reg.h>
26 27
27static const struct usb_device_id id_table[] = { 28static const struct usb_device_id id_table[] = {
28 { USB_DEVICE(0x1934, 0x0706) }, 29 { USB_DEVICE(0x1934, 0x0706) },
@@ -30,37 +31,218 @@ static const struct usb_device_id id_table[] = {
30}; 31};
31MODULE_DEVICE_TABLE(usb, id_table); 32MODULE_DEVICE_TABLE(usb, id_table);
32 33
33#define CONTROL_DTR 0x01 34/* Maximum baudrate for F81232 */
34#define CONTROL_RTS 0x02 35#define F81232_MAX_BAUDRATE 115200
35 36
36#define UART_STATE 0x08 37/* USB Control EP parameter */
37#define UART_STATE_TRANSIENT_MASK 0x74 38#define F81232_REGISTER_REQUEST 0xa0
38#define UART_DCD 0x01 39#define F81232_GET_REGISTER 0xc0
39#define UART_DSR 0x02 40#define F81232_SET_REGISTER 0x40
40#define UART_BREAK_ERROR 0x04 41
41#define UART_RING 0x08 42#define SERIAL_BASE_ADDRESS 0x0120
42#define UART_FRAME_ERROR 0x10 43#define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
43#define UART_PARITY_ERROR 0x20 44#define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
44#define UART_OVERRUN_ERROR 0x40 45#define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
45#define UART_CTS 0x80 46#define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
47#define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
48#define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
46 49
47struct f81232_private { 50struct f81232_private {
48 spinlock_t lock; 51 struct mutex lock;
49 u8 line_control; 52 u8 modem_control;
50 u8 line_status; 53 u8 modem_status;
54 struct work_struct interrupt_work;
55 struct usb_serial_port *port;
51}; 56};
52 57
58static int calc_baud_divisor(speed_t baudrate)
59{
60 return DIV_ROUND_CLOSEST(F81232_MAX_BAUDRATE, baudrate);
61}
62
63static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
64{
65 int status;
66 u8 *tmp;
67 struct usb_device *dev = port->serial->dev;
68
69 tmp = kmalloc(sizeof(*val), GFP_KERNEL);
70 if (!tmp)
71 return -ENOMEM;
72
73 status = usb_control_msg(dev,
74 usb_rcvctrlpipe(dev, 0),
75 F81232_REGISTER_REQUEST,
76 F81232_GET_REGISTER,
77 reg,
78 0,
79 tmp,
80 sizeof(*val),
81 USB_CTRL_GET_TIMEOUT);
82 if (status != sizeof(*val)) {
83 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
84
85 if (status < 0)
86 status = usb_translate_errors(status);
87 else
88 status = -EIO;
89 } else {
90 status = 0;
91 *val = *tmp;
92 }
93
94 kfree(tmp);
95 return status;
96}
97
98static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
99{
100 int status;
101 u8 *tmp;
102 struct usb_device *dev = port->serial->dev;
103
104 tmp = kmalloc(sizeof(val), GFP_KERNEL);
105 if (!tmp)
106 return -ENOMEM;
107
108 *tmp = val;
109
110 status = usb_control_msg(dev,
111 usb_sndctrlpipe(dev, 0),
112 F81232_REGISTER_REQUEST,
113 F81232_SET_REGISTER,
114 reg,
115 0,
116 tmp,
117 sizeof(val),
118 USB_CTRL_SET_TIMEOUT);
119 if (status != sizeof(val)) {
120 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
121
122 if (status < 0)
123 status = usb_translate_errors(status);
124 else
125 status = -EIO;
126 } else {
127 status = 0;
128 }
129
130 kfree(tmp);
131 return status;
132}
133
134static void f81232_read_msr(struct usb_serial_port *port)
135{
136 int status;
137 u8 current_msr;
138 struct tty_struct *tty;
139 struct f81232_private *priv = usb_get_serial_port_data(port);
140
141 mutex_lock(&priv->lock);
142 status = f81232_get_register(port, MODEM_STATUS_REGISTER,
143 &current_msr);
144 if (status) {
145 dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
146 mutex_unlock(&priv->lock);
147 return;
148 }
149
150 if (!(current_msr & UART_MSR_ANY_DELTA)) {
151 mutex_unlock(&priv->lock);
152 return;
153 }
154
155 priv->modem_status = current_msr;
156
157 if (current_msr & UART_MSR_DCTS)
158 port->icount.cts++;
159 if (current_msr & UART_MSR_DDSR)
160 port->icount.dsr++;
161 if (current_msr & UART_MSR_TERI)
162 port->icount.rng++;
163 if (current_msr & UART_MSR_DDCD) {
164 port->icount.dcd++;
165 tty = tty_port_tty_get(&port->port);
166 if (tty) {
167 usb_serial_handle_dcd_change(port, tty,
168 current_msr & UART_MSR_DCD);
169
170 tty_kref_put(tty);
171 }
172 }
173
174 wake_up_interruptible(&port->port.delta_msr_wait);
175 mutex_unlock(&priv->lock);
176}
177
178static int f81232_set_mctrl(struct usb_serial_port *port,
179 unsigned int set, unsigned int clear)
180{
181 u8 val;
182 int status;
183 struct f81232_private *priv = usb_get_serial_port_data(port);
184
185 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
186 return 0; /* no change */
187
188 /* 'set' takes precedence over 'clear' */
189 clear &= ~set;
190
191 /* force enable interrupt with OUT2 */
192 mutex_lock(&priv->lock);
193 val = UART_MCR_OUT2 | priv->modem_control;
194
195 if (clear & TIOCM_DTR)
196 val &= ~UART_MCR_DTR;
197
198 if (clear & TIOCM_RTS)
199 val &= ~UART_MCR_RTS;
200
201 if (set & TIOCM_DTR)
202 val |= UART_MCR_DTR;
203
204 if (set & TIOCM_RTS)
205 val |= UART_MCR_RTS;
206
207 dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
208 val, priv->modem_control);
209
210 status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
211 if (status) {
212 dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
213 mutex_unlock(&priv->lock);
214 return status;
215 }
216
217 priv->modem_control = val;
218 mutex_unlock(&priv->lock);
219
220 return 0;
221}
222
53static void f81232_update_line_status(struct usb_serial_port *port, 223static void f81232_update_line_status(struct usb_serial_port *port,
54 unsigned char *data, 224 unsigned char *data,
55 unsigned int actual_length) 225 size_t actual_length)
56{ 226{
57 /* 227 struct f81232_private *priv = usb_get_serial_port_data(port);
58 * FIXME: Update port->icount, and call 228
59 * 229 if (!actual_length)
60 * wake_up_interruptible(&port->port.delta_msr_wait); 230 return;
61 * 231
62 * on MSR changes. 232 switch (data[0] & 0x07) {
63 */ 233 case 0x00: /* msr change */
234 dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
235 schedule_work(&priv->interrupt_work);
236 break;
237 case 0x02: /* tx-empty */
238 break;
239 case 0x04: /* rx data available */
240 break;
241 case 0x06: /* lsr change */
242 /* we can forget it. the LSR will read from bulk-in */
243 dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
244 break;
245 }
64} 246}
65 247
66static void f81232_read_int_callback(struct urb *urb) 248static void f81232_read_int_callback(struct urb *urb)
@@ -104,55 +286,55 @@ exit:
104static void f81232_process_read_urb(struct urb *urb) 286static void f81232_process_read_urb(struct urb *urb)
105{ 287{
106 struct usb_serial_port *port = urb->context; 288 struct usb_serial_port *port = urb->context;
107 struct f81232_private *priv = usb_get_serial_port_data(port);
108 unsigned char *data = urb->transfer_buffer; 289 unsigned char *data = urb->transfer_buffer;
109 char tty_flag = TTY_NORMAL; 290 char tty_flag;
110 unsigned long flags; 291 unsigned int i;
111 u8 line_status; 292 u8 lsr;
112 int i; 293
113 294 /*
114 /* update line status */ 295 * When opening the port we get a 1-byte packet with the current LSR,
115 spin_lock_irqsave(&priv->lock, flags); 296 * which we discard.
116 line_status = priv->line_status; 297 */
117 priv->line_status &= ~UART_STATE_TRANSIENT_MASK; 298 if ((urb->actual_length < 2) || (urb->actual_length % 2))
118 spin_unlock_irqrestore(&priv->lock, flags);
119
120 if (!urb->actual_length)
121 return; 299 return;
122 300
123 /* break takes precedence over parity, */ 301 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
124 /* which takes precedence over framing errors */ 302
125 if (line_status & UART_BREAK_ERROR) 303 for (i = 0; i < urb->actual_length; i += 2) {
126 tty_flag = TTY_BREAK; 304 tty_flag = TTY_NORMAL;
127 else if (line_status & UART_PARITY_ERROR) 305 lsr = data[i];
128 tty_flag = TTY_PARITY; 306
129 else if (line_status & UART_FRAME_ERROR) 307 if (lsr & UART_LSR_BRK_ERROR_BITS) {
130 tty_flag = TTY_FRAME; 308 if (lsr & UART_LSR_BI) {
131 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag); 309 tty_flag = TTY_BREAK;
132 310 port->icount.brk++;
133 /* overrun is special, not associated with a char */ 311 usb_serial_handle_break(port);
134 if (line_status & UART_OVERRUN_ERROR) 312 } else if (lsr & UART_LSR_PE) {
135 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 313 tty_flag = TTY_PARITY;
136 314 port->icount.parity++;
137 if (port->port.console && port->sysrq) { 315 } else if (lsr & UART_LSR_FE) {
138 for (i = 0; i < urb->actual_length; ++i) 316 tty_flag = TTY_FRAME;
139 if (!usb_serial_handle_sysrq_char(port, data[i])) 317 port->icount.frame++;
140 tty_insert_flip_char(&port->port, data[i], 318 }
141 tty_flag); 319
142 } else { 320 if (lsr & UART_LSR_OE) {
143 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, 321 port->icount.overrun++;
144 urb->actual_length); 322 tty_insert_flip_char(&port->port, 0,
323 TTY_OVERRUN);
324 }
325 }
326
327 if (port->port.console && port->sysrq) {
328 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
329 continue;
330 }
331
332 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
145 } 333 }
146 334
147 tty_flip_buffer_push(&port->port); 335 tty_flip_buffer_push(&port->port);
148} 336}
149 337
150static int set_control_lines(struct usb_device *dev, u8 value)
151{
152 /* FIXME - Stubbed out for now */
153 return 0;
154}
155
156static void f81232_break_ctl(struct tty_struct *tty, int break_state) 338static void f81232_break_ctl(struct tty_struct *tty, int break_state)
157{ 339{
158 /* FIXME - Stubbed out for now */ 340 /* FIXME - Stubbed out for now */
@@ -164,37 +346,198 @@ static void f81232_break_ctl(struct tty_struct *tty, int break_state)
164 */ 346 */
165} 347}
166 348
349static void f81232_set_baudrate(struct usb_serial_port *port, speed_t baudrate)
350{
351 u8 lcr;
352 int divisor;
353 int status = 0;
354
355 divisor = calc_baud_divisor(baudrate);
356
357 status = f81232_get_register(port, LINE_CONTROL_REGISTER,
358 &lcr); /* get LCR */
359 if (status) {
360 dev_err(&port->dev, "%s failed to get LCR: %d\n",
361 __func__, status);
362 return;
363 }
364
365 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
366 lcr | UART_LCR_DLAB); /* Enable DLAB */
367 if (status) {
368 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
369 __func__, status);
370 return;
371 }
372
373 status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
374 divisor & 0x00ff); /* low */
375 if (status) {
376 dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
377 __func__, status);
378 goto reapply_lcr;
379 }
380
381 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
382 (divisor & 0xff00) >> 8); /* high */
383 if (status) {
384 dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
385 __func__, status);
386 }
387
388reapply_lcr:
389 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
390 lcr & ~UART_LCR_DLAB);
391 if (status) {
392 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
393 __func__, status);
394 }
395}
396
397static int f81232_port_enable(struct usb_serial_port *port)
398{
399 u8 val;
400 int status;
401
402 /* fifo on, trigger8, clear TX/RX*/
403 val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
404 UART_FCR_CLEAR_XMIT;
405
406 status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
407 if (status) {
408 dev_err(&port->dev, "%s failed to set FCR: %d\n",
409 __func__, status);
410 return status;
411 }
412
413 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
414 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
415 UART_IER_MSI);
416 if (status) {
417 dev_err(&port->dev, "%s failed to set IER: %d\n",
418 __func__, status);
419 return status;
420 }
421
422 return 0;
423}
424
425static int f81232_port_disable(struct usb_serial_port *port)
426{
427 int status;
428
429 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
430 if (status) {
431 dev_err(&port->dev, "%s failed to set IER: %d\n",
432 __func__, status);
433 return status;
434 }
435
436 return 0;
437}
438
167static void f81232_set_termios(struct tty_struct *tty, 439static void f81232_set_termios(struct tty_struct *tty,
168 struct usb_serial_port *port, struct ktermios *old_termios) 440 struct usb_serial_port *port, struct ktermios *old_termios)
169{ 441{
170 /* FIXME - Stubbed out for now */ 442 u8 new_lcr = 0;
443 int status = 0;
444 speed_t baudrate;
171 445
172 /* Don't change anything if nothing has changed */ 446 /* Don't change anything if nothing has changed */
173 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) 447 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
174 return; 448 return;
175 449
176 /* Do the real work here... */ 450 if (C_BAUD(tty) == B0)
177 if (old_termios) 451 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
178 tty_termios_copy_hw(&tty->termios, old_termios); 452 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
453 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
454
455 baudrate = tty_get_baud_rate(tty);
456 if (baudrate > 0) {
457 if (baudrate > F81232_MAX_BAUDRATE) {
458 baudrate = F81232_MAX_BAUDRATE;
459 tty_encode_baud_rate(tty, baudrate, baudrate);
460 }
461 f81232_set_baudrate(port, baudrate);
462 }
463
464 if (C_PARENB(tty)) {
465 new_lcr |= UART_LCR_PARITY;
466
467 if (!C_PARODD(tty))
468 new_lcr |= UART_LCR_EPAR;
469
470 if (C_CMSPAR(tty))
471 new_lcr |= UART_LCR_SPAR;
472 }
473
474 if (C_CSTOPB(tty))
475 new_lcr |= UART_LCR_STOP;
476
477 switch (C_CSIZE(tty)) {
478 case CS5:
479 new_lcr |= UART_LCR_WLEN5;
480 break;
481 case CS6:
482 new_lcr |= UART_LCR_WLEN6;
483 break;
484 case CS7:
485 new_lcr |= UART_LCR_WLEN7;
486 break;
487 default:
488 case CS8:
489 new_lcr |= UART_LCR_WLEN8;
490 break;
491 }
492
493 status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
494 if (status) {
495 dev_err(&port->dev, "%s failed to set LCR: %d\n",
496 __func__, status);
497 }
179} 498}
180 499
181static int f81232_tiocmget(struct tty_struct *tty) 500static int f81232_tiocmget(struct tty_struct *tty)
182{ 501{
183 /* FIXME - Stubbed out for now */ 502 int r;
184 return 0; 503 struct usb_serial_port *port = tty->driver_data;
504 struct f81232_private *port_priv = usb_get_serial_port_data(port);
505 u8 mcr, msr;
506
507 /* force get current MSR changed state */
508 f81232_read_msr(port);
509
510 mutex_lock(&port_priv->lock);
511 mcr = port_priv->modem_control;
512 msr = port_priv->modem_status;
513 mutex_unlock(&port_priv->lock);
514
515 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
516 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
517 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
518 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
519 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
520 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
521
522 return r;
185} 523}
186 524
187static int f81232_tiocmset(struct tty_struct *tty, 525static int f81232_tiocmset(struct tty_struct *tty,
188 unsigned int set, unsigned int clear) 526 unsigned int set, unsigned int clear)
189{ 527{
190 /* FIXME - Stubbed out for now */ 528 struct usb_serial_port *port = tty->driver_data;
191 return 0; 529
530 return f81232_set_mctrl(port, set, clear);
192} 531}
193 532
194static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port) 533static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
195{ 534{
196 int result; 535 int result;
197 536
537 result = f81232_port_enable(port);
538 if (result)
539 return result;
540
198 /* Setup termios */ 541 /* Setup termios */
199 if (tty) 542 if (tty)
200 f81232_set_termios(tty, port, NULL); 543 f81232_set_termios(tty, port, NULL);
@@ -217,59 +560,73 @@ static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
217 560
218static void f81232_close(struct usb_serial_port *port) 561static void f81232_close(struct usb_serial_port *port)
219{ 562{
563 f81232_port_disable(port);
220 usb_serial_generic_close(port); 564 usb_serial_generic_close(port);
221 usb_kill_urb(port->interrupt_in_urb); 565 usb_kill_urb(port->interrupt_in_urb);
222} 566}
223 567
224static void f81232_dtr_rts(struct usb_serial_port *port, int on) 568static void f81232_dtr_rts(struct usb_serial_port *port, int on)
225{ 569{
226 struct f81232_private *priv = usb_get_serial_port_data(port);
227 unsigned long flags;
228 u8 control;
229
230 spin_lock_irqsave(&priv->lock, flags);
231 /* Change DTR and RTS */
232 if (on) 570 if (on)
233 priv->line_control |= (CONTROL_DTR | CONTROL_RTS); 571 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
234 else 572 else
235 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 573 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
236 control = priv->line_control;
237 spin_unlock_irqrestore(&priv->lock, flags);
238 set_control_lines(port->serial->dev, control);
239} 574}
240 575
241static int f81232_carrier_raised(struct usb_serial_port *port) 576static int f81232_carrier_raised(struct usb_serial_port *port)
242{ 577{
578 u8 msr;
243 struct f81232_private *priv = usb_get_serial_port_data(port); 579 struct f81232_private *priv = usb_get_serial_port_data(port);
244 if (priv->line_status & UART_DCD) 580
581 mutex_lock(&priv->lock);
582 msr = priv->modem_status;
583 mutex_unlock(&priv->lock);
584
585 if (msr & UART_MSR_DCD)
245 return 1; 586 return 1;
246 return 0; 587 return 0;
247} 588}
248 589
590static int f81232_get_serial_info(struct usb_serial_port *port,
591 unsigned long arg)
592{
593 struct serial_struct ser;
594
595 memset(&ser, 0, sizeof(ser));
596
597 ser.type = PORT_16550A;
598 ser.line = port->minor;
599 ser.port = port->port_number;
600 ser.baud_base = F81232_MAX_BAUDRATE;
601
602 if (copy_to_user((void __user *)arg, &ser, sizeof(ser)))
603 return -EFAULT;
604
605 return 0;
606}
607
249static int f81232_ioctl(struct tty_struct *tty, 608static int f81232_ioctl(struct tty_struct *tty,
250 unsigned int cmd, unsigned long arg) 609 unsigned int cmd, unsigned long arg)
251{ 610{
252 struct serial_struct ser;
253 struct usb_serial_port *port = tty->driver_data; 611 struct usb_serial_port *port = tty->driver_data;
254 612
255 switch (cmd) { 613 switch (cmd) {
256 case TIOCGSERIAL: 614 case TIOCGSERIAL:
257 memset(&ser, 0, sizeof ser); 615 return f81232_get_serial_info(port, arg);
258 ser.type = PORT_16654;
259 ser.line = port->minor;
260 ser.port = port->port_number;
261 ser.baud_base = 460800;
262
263 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
264 return -EFAULT;
265
266 return 0;
267 default: 616 default:
268 break; 617 break;
269 } 618 }
270 return -ENOIOCTLCMD; 619 return -ENOIOCTLCMD;
271} 620}
272 621
622static void f81232_interrupt_work(struct work_struct *work)
623{
624 struct f81232_private *priv =
625 container_of(work, struct f81232_private, interrupt_work);
626
627 f81232_read_msr(priv->port);
628}
629
273static int f81232_port_probe(struct usb_serial_port *port) 630static int f81232_port_probe(struct usb_serial_port *port)
274{ 631{
275 struct f81232_private *priv; 632 struct f81232_private *priv;
@@ -278,11 +635,13 @@ static int f81232_port_probe(struct usb_serial_port *port)
278 if (!priv) 635 if (!priv)
279 return -ENOMEM; 636 return -ENOMEM;
280 637
281 spin_lock_init(&priv->lock); 638 mutex_init(&priv->lock);
639 INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
282 640
283 usb_set_serial_port_data(port, priv); 641 usb_set_serial_port_data(port, priv);
284 642
285 port->port.drain_delay = 256; 643 port->port.drain_delay = 256;
644 priv->port = port;
286 645
287 return 0; 646 return 0;
288} 647}
@@ -308,7 +667,7 @@ static struct usb_serial_driver f81232_device = {
308 .bulk_out_size = 256, 667 .bulk_out_size = 256,
309 .open = f81232_open, 668 .open = f81232_open,
310 .close = f81232_close, 669 .close = f81232_close,
311 .dtr_rts = f81232_dtr_rts, 670 .dtr_rts = f81232_dtr_rts,
312 .carrier_raised = f81232_carrier_raised, 671 .carrier_raised = f81232_carrier_raised,
313 .ioctl = f81232_ioctl, 672 .ioctl = f81232_ioctl,
314 .break_ctl = f81232_break_ctl, 673 .break_ctl = f81232_break_ctl,
@@ -330,5 +689,6 @@ static struct usb_serial_driver * const serial_drivers[] = {
330module_usb_serial_driver(serial_drivers, id_table); 689module_usb_serial_driver(serial_drivers, id_table);
331 690
332MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver"); 691MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
333MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org"); 692MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
693MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
334MODULE_LICENSE("GPL v2"); 694MODULE_LICENSE("GPL v2");
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 3086dec0ef53..8eb68a31cab6 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -604,6 +604,7 @@ static const struct usb_device_id id_table_combined[] = {
604 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 604 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
605 { USB_DEVICE(FTDI_VID, FTDI_NT_ORIONLXM_PID), 605 { USB_DEVICE(FTDI_VID, FTDI_NT_ORIONLXM_PID),
606 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 606 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
607 { USB_DEVICE(FTDI_VID, FTDI_SYNAPSE_SS200_PID) },
607 /* 608 /*
608 * ELV devices: 609 * ELV devices:
609 */ 610 */
@@ -1883,8 +1884,12 @@ static int ftdi_8u2232c_probe(struct usb_serial *serial)
1883{ 1884{
1884 struct usb_device *udev = serial->dev; 1885 struct usb_device *udev = serial->dev;
1885 1886
1886 if ((udev->manufacturer && !strcmp(udev->manufacturer, "CALAO Systems")) || 1887 if (udev->manufacturer && !strcmp(udev->manufacturer, "CALAO Systems"))
1887 (udev->product && !strcmp(udev->product, "BeagleBone/XDS100V2"))) 1888 return ftdi_jtag_probe(serial);
1889
1890 if (udev->product &&
1891 (!strcmp(udev->product, "BeagleBone/XDS100V2") ||
1892 !strcmp(udev->product, "SNAP Connect E10")))
1888 return ftdi_jtag_probe(serial); 1893 return ftdi_jtag_probe(serial);
1889 1894
1890 return 0; 1895 return 0;
diff --git a/drivers/usb/serial/ftdi_sio_ids.h b/drivers/usb/serial/ftdi_sio_ids.h
index 56b1b55c4751..4e4f46f3c89c 100644
--- a/drivers/usb/serial/ftdi_sio_ids.h
+++ b/drivers/usb/serial/ftdi_sio_ids.h
@@ -561,6 +561,12 @@
561 */ 561 */
562#define FTDI_NT_ORIONLXM_PID 0x7c90 /* OrionLXm Substation Automation Platform */ 562#define FTDI_NT_ORIONLXM_PID 0x7c90 /* OrionLXm Substation Automation Platform */
563 563
564/*
565 * Synapse Wireless product ids (FTDI_VID)
566 * http://www.synapse-wireless.com
567 */
568#define FTDI_SYNAPSE_SS200_PID 0x9090 /* SS200 - SNAP Stick 200 */
569
564 570
565/********************************/ 571/********************************/
566/** third-party VID/PID combos **/ 572/** third-party VID/PID combos **/
diff --git a/drivers/usb/serial/keyspan_pda.c b/drivers/usb/serial/keyspan_pda.c
index dd97d8b572c3..4f7e072e4e00 100644
--- a/drivers/usb/serial/keyspan_pda.c
+++ b/drivers/usb/serial/keyspan_pda.c
@@ -61,6 +61,7 @@ struct keyspan_pda_private {
61/* For Xircom PGSDB9 and older Entrega version of the same device */ 61/* For Xircom PGSDB9 and older Entrega version of the same device */
62#define XIRCOM_VENDOR_ID 0x085a 62#define XIRCOM_VENDOR_ID 0x085a
63#define XIRCOM_FAKE_ID 0x8027 63#define XIRCOM_FAKE_ID 0x8027
64#define XIRCOM_FAKE_ID_2 0x8025 /* "PGMFHUB" serial */
64#define ENTREGA_VENDOR_ID 0x1645 65#define ENTREGA_VENDOR_ID 0x1645
65#define ENTREGA_FAKE_ID 0x8093 66#define ENTREGA_FAKE_ID 0x8093
66 67
@@ -70,6 +71,7 @@ static const struct usb_device_id id_table_combined[] = {
70#endif 71#endif
71#ifdef XIRCOM 72#ifdef XIRCOM
72 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 73 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
74 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
73 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) }, 75 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
74#endif 76#endif
75 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) }, 77 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
@@ -93,6 +95,7 @@ static const struct usb_device_id id_table_fake[] = {
93#ifdef XIRCOM 95#ifdef XIRCOM
94static const struct usb_device_id id_table_fake_xircom[] = { 96static const struct usb_device_id id_table_fake_xircom[] = {
95 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 97 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
98 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
96 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) }, 99 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
97 { } 100 { }
98}; 101};
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 62c2d9daa7d6..4b55ab66a534 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -207,7 +207,8 @@ static struct alauda_card_info alauda_card_ids[] = {
207 { 0,} 207 { 0,}
208}; 208};
209 209
210static struct alauda_card_info *alauda_card_find_id(unsigned char id) { 210static struct alauda_card_info *alauda_card_find_id(unsigned char id)
211{
211 int i; 212 int i;
212 213
213 for (i = 0; alauda_card_ids[i].id != 0; i++) 214 for (i = 0; alauda_card_ids[i].id != 0; i++)
@@ -223,7 +224,8 @@ static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
223static unsigned char parity[256]; 224static unsigned char parity[256];
224static unsigned char ecc2[256]; 225static unsigned char ecc2[256];
225 226
226static void nand_init_ecc(void) { 227static void nand_init_ecc(void)
228{
227 int i, j, a; 229 int i, j, a;
228 230
229 parity[0] = 0; 231 parity[0] = 0;
@@ -247,7 +249,8 @@ static void nand_init_ecc(void) {
247} 249}
248 250
249/* compute 3-byte ecc on 256 bytes */ 251/* compute 3-byte ecc on 256 bytes */
250static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) { 252static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
253{
251 int i, j, a; 254 int i, j, a;
252 unsigned char par = 0, bit, bits[8] = {0}; 255 unsigned char par = 0, bit, bits[8] = {0};
253 256
@@ -270,11 +273,13 @@ static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
270 ecc[2] = ecc2[par]; 273 ecc[2] = ecc2[par];
271} 274}
272 275
273static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) { 276static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
277{
274 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); 278 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
275} 279}
276 280
277static void nand_store_ecc(unsigned char *data, unsigned char *ecc) { 281static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
282{
278 memcpy(data, ecc, 3); 283 memcpy(data, ecc, 3);
279} 284}
280 285
diff --git a/drivers/usb/storage/cypress_atacb.c b/drivers/usb/storage/cypress_atacb.c
index 8514a2d82b72..b3466d1395f2 100644
--- a/drivers/usb/storage/cypress_atacb.c
+++ b/drivers/usb/storage/cypress_atacb.c
@@ -96,13 +96,13 @@ static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
96 if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */ 96 if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
97 goto invalid_fld; 97 goto invalid_fld;
98 /* check protocol */ 98 /* check protocol */
99 switch((save_cmnd[1] >> 1) & 0xf) { 99 switch ((save_cmnd[1] >> 1) & 0xf) {
100 case 3: /*no DATA */ 100 case 3: /*no DATA */
101 case 4: /* PIO in */ 101 case 4: /* PIO in */
102 case 5: /* PIO out */ 102 case 5: /* PIO out */
103 break; 103 break;
104 default: 104 default:
105 goto invalid_fld; 105 goto invalid_fld;
106 } 106 }
107 107
108 /* first build the ATACB command */ 108 /* first build the ATACB command */
@@ -132,8 +132,7 @@ static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
132 || save_cmnd[11]) 132 || save_cmnd[11])
133 goto invalid_fld; 133 goto invalid_fld;
134 } 134 }
135 } 135 } else { /* ATA12 */
136 else { /* ATA12 */
137 srb->cmnd[ 6] = save_cmnd[3]; /* features */ 136 srb->cmnd[ 6] = save_cmnd[3]; /* features */
138 srb->cmnd[ 7] = save_cmnd[4]; /* sector count */ 137 srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
139 srb->cmnd[ 8] = save_cmnd[5]; /* lba low */ 138 srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 599d8bff26c3..076178645ba4 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -737,7 +737,7 @@ static void isd200_log_config(struct us_data *us, struct isd200_info *info)
737 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2); 737 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
738 usb_stor_dbg(us, " Skip Device Boot: 0x%x\n", 738 usb_stor_dbg(us, " Skip Device Boot: 0x%x\n",
739 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT); 739 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
740 usb_stor_dbg(us, " ATA 3 State Supsend: 0x%x\n", 740 usb_stor_dbg(us, " ATA 3 State Suspend: 0x%x\n",
741 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND); 741 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
742 usb_stor_dbg(us, " Descriptor Override: 0x%x\n", 742 usb_stor_dbg(us, " Descriptor Override: 0x%x\n",
743 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE); 743 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
diff --git a/drivers/usb/storage/unusual_uas.h b/drivers/usb/storage/unusual_uas.h
index 82570425fdfe..c85ea530085f 100644
--- a/drivers/usb/storage/unusual_uas.h
+++ b/drivers/usb/storage/unusual_uas.h
@@ -113,6 +113,13 @@ UNUSUAL_DEV(0x0bc2, 0xab2a, 0x0000, 0x9999,
113 USB_SC_DEVICE, USB_PR_DEVICE, NULL, 113 USB_SC_DEVICE, USB_PR_DEVICE, NULL,
114 US_FL_NO_ATA_1X), 114 US_FL_NO_ATA_1X),
115 115
116/* Reported-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> */
117UNUSUAL_DEV(0x13fd, 0x3940, 0x0000, 0x9999,
118 "Initio Corporation",
119 "",
120 USB_SC_DEVICE, USB_PR_DEVICE, NULL,
121 US_FL_NO_ATA_1X),
122
116/* Reported-by: Tom Arild Naess <tanaess@gmail.com> */ 123/* Reported-by: Tom Arild Naess <tanaess@gmail.com> */
117UNUSUAL_DEV(0x152d, 0x0539, 0x0000, 0x9999, 124UNUSUAL_DEV(0x152d, 0x0539, 0x0000, 0x9999,
118 "JMicron", 125 "JMicron",
diff --git a/drivers/usb/usbip/vhci_hcd.c b/drivers/usb/usbip/vhci_hcd.c
index 11f6f61c2381..e9ef1eccdace 100644
--- a/drivers/usb/usbip/vhci_hcd.c
+++ b/drivers/usb/usbip/vhci_hcd.c
@@ -216,7 +216,7 @@ done:
216static inline void hub_descriptor(struct usb_hub_descriptor *desc) 216static inline void hub_descriptor(struct usb_hub_descriptor *desc)
217{ 217{
218 memset(desc, 0, sizeof(*desc)); 218 memset(desc, 0, sizeof(*desc));
219 desc->bDescriptorType = 0x29; 219 desc->bDescriptorType = USB_DT_HUB;
220 desc->bDescLength = 9; 220 desc->bDescLength = 9;
221 desc->wHubCharacteristics = __constant_cpu_to_le16( 221 desc->wHubCharacteristics = __constant_cpu_to_le16(
222 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM); 222 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
diff --git a/drivers/usb/wusbcore/rh.c b/drivers/usb/wusbcore/rh.c
index aa5af817f31c..a082fe62b1f0 100644
--- a/drivers/usb/wusbcore/rh.c
+++ b/drivers/usb/wusbcore/rh.c
@@ -182,7 +182,7 @@ static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
182 if (wLength < length) 182 if (wLength < length)
183 return -ENOSPC; 183 return -ENOSPC;
184 descr->bDescLength = 7 + 2 * temp; 184 descr->bDescLength = 7 + 2 * temp;
185 descr->bDescriptorType = 0x29; /* HUB type */ 185 descr->bDescriptorType = USB_DT_HUB; /* HUB type */
186 descr->bNbrPorts = wusbhc->ports_max; 186 descr->bNbrPorts = wusbhc->ports_max;
187 descr->wHubCharacteristics = cpu_to_le16( 187 descr->wHubCharacteristics = cpu_to_le16(
188 HUB_CHAR_COMMON_LPSM /* All ports power at once */ 188 HUB_CHAR_COMMON_LPSM /* All ports power at once */