aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2011-11-29 04:51:07 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2011-11-29 04:51:07 -0500
commit0d2cd91bf7b1a7cc1d638296111fcc2bcf5c0bb4 (patch)
treed2ca69347816c27f9dc352581f5d0fe76811cd49 /drivers/usb
parent3d95fd6ad8d3cf582a70ed65660017114b6e4065 (diff)
parentcaca6a03d365883564885f2c1da3e88dcf65d139 (diff)
Merge commit 'v3.2-rc3' into next
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Kconfig17
-rw-r--r--drivers/usb/Makefile4
-rw-r--r--drivers/usb/c67x00/c67x00-drv.c1
-rw-r--r--drivers/usb/class/cdc-acm.c19
-rw-r--r--drivers/usb/class/cdc-wdm.c8
-rw-r--r--drivers/usb/class/usbtmc.c5
-rw-r--r--drivers/usb/core/config.c111
-rw-r--r--drivers/usb/core/devices.c4
-rw-r--r--drivers/usb/core/devio.c57
-rw-r--r--drivers/usb/core/driver.c31
-rw-r--r--drivers/usb/core/endpoint.c2
-rw-r--r--drivers/usb/core/hcd-pci.c2
-rw-r--r--drivers/usb/core/hcd.c36
-rw-r--r--drivers/usb/core/hub.c332
-rw-r--r--drivers/usb/core/message.c2
-rw-r--r--drivers/usb/core/notify.c1
-rw-r--r--drivers/usb/core/quirks.c51
-rw-r--r--drivers/usb/core/sysfs.c59
-rw-r--r--drivers/usb/core/urb.c2
-rw-r--r--drivers/usb/core/usb.c1
-rw-r--r--drivers/usb/core/usb.h7
-rw-r--r--drivers/usb/dwc3/Kconfig25
-rw-r--r--drivers/usb/dwc3/Makefile36
-rw-r--r--drivers/usb/dwc3/core.c484
-rw-r--r--drivers/usb/dwc3/core.h768
-rw-r--r--drivers/usb/dwc3/debug.h50
-rw-r--r--drivers/usb/dwc3/debugfs.c441
-rw-r--r--drivers/usb/dwc3/dwc3-omap.c401
-rw-r--r--drivers/usb/dwc3/dwc3-pci.c219
-rw-r--r--drivers/usb/dwc3/ep0.c804
-rw-r--r--drivers/usb/dwc3/gadget.c2105
-rw-r--r--drivers/usb/dwc3/gadget.h211
-rw-r--r--drivers/usb/dwc3/io.h54
-rw-r--r--drivers/usb/gadget/Kconfig56
-rw-r--r--drivers/usb/gadget/Makefile4
-rw-r--r--drivers/usb/gadget/acm_ms.c256
-rw-r--r--drivers/usb/gadget/amd5536udc.c20
-rw-r--r--drivers/usb/gadget/amd5536udc.h9
-rw-r--r--drivers/usb/gadget/at91_udc.c32
-rw-r--r--drivers/usb/gadget/at91_udc.h10
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c13
-rw-r--r--drivers/usb/gadget/cdc2.c10
-rw-r--r--drivers/usb/gadget/ci13xxx_msm.c13
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.c23
-rw-r--r--drivers/usb/gadget/composite.c38
-rw-r--r--drivers/usb/gadget/config.c9
-rw-r--r--drivers/usb/gadget/dbgp.c2
-rw-r--r--drivers/usb/gadget/dummy_hcd.c13
-rw-r--r--drivers/usb/gadget/epautoconf.c12
-rw-r--r--drivers/usb/gadget/ether.c9
-rw-r--r--drivers/usb/gadget/f_audio.c15
-rw-r--r--drivers/usb/gadget/f_ecm.c9
-rw-r--r--drivers/usb/gadget/f_eem.c9
-rw-r--r--drivers/usb/gadget/f_fs.c10
-rw-r--r--drivers/usb/gadget/f_hid.c9
-rw-r--r--drivers/usb/gadget/f_loopback.c9
-rw-r--r--drivers/usb/gadget/f_mass_storage.c202
-rw-r--r--drivers/usb/gadget/f_midi.c990
-rw-r--r--drivers/usb/gadget/f_ncm.c9
-rw-r--r--drivers/usb/gadget/f_obex.c10
-rw-r--r--drivers/usb/gadget/f_phonet.c12
-rw-r--r--drivers/usb/gadget/f_rndis.c9
-rw-r--r--drivers/usb/gadget/f_sourcesink.c10
-rw-r--r--drivers/usb/gadget/f_subset.c9
-rw-r--r--drivers/usb/gadget/f_uvc.c1
-rw-r--r--drivers/usb/gadget/f_uvc.h1
-rw-r--r--drivers/usb/gadget/file_storage.c259
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.c2
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c58
-rw-r--r--drivers/usb/gadget/fusb300_udc.c17
-rw-r--r--drivers/usb/gadget/fusb300_udc.h10
-rw-r--r--drivers/usb/gadget/g_ffs.c9
-rw-r--r--drivers/usb/gadget/gadget_chips.h3
-rw-r--r--drivers/usb/gadget/gmidi.c1293
-rw-r--r--drivers/usb/gadget/hid.c9
-rw-r--r--drivers/usb/gadget/imx_udc.c4
-rw-r--r--drivers/usb/gadget/imx_udc.h5
-rw-r--r--drivers/usb/gadget/inode.c14
-rw-r--r--drivers/usb/gadget/langwell_udc.c149
-rw-r--r--drivers/usb/gadget/langwell_udc.h12
-rw-r--r--drivers/usb/gadget/m66592-udc.c16
-rw-r--r--drivers/usb/gadget/m66592-udc.h10
-rw-r--r--drivers/usb/gadget/mass_storage.c11
-rw-r--r--drivers/usb/gadget/multi.c9
-rw-r--r--drivers/usb/gadget/mv_udc.h26
-rw-r--r--drivers/usb/gadget/mv_udc_core.c564
-rw-r--r--drivers/usb/gadget/mv_udc_phy.c214
-rw-r--r--drivers/usb/gadget/ncm.c9
-rw-r--r--drivers/usb/gadget/ndis.h6
-rw-r--r--drivers/usb/gadget/net2272.c78
-rw-r--r--drivers/usb/gadget/net2280.c89
-rw-r--r--drivers/usb/gadget/net2280.h9
-rw-r--r--drivers/usb/gadget/omap_udc.c18
-rw-r--r--drivers/usb/gadget/pch_udc.c25
-rw-r--r--drivers/usb/gadget/printer.c25
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c30
-rw-r--r--drivers/usb/gadget/pxa25x_udc.h17
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c12
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h9
-rw-r--r--drivers/usb/gadget/r8a66597-udc.c542
-rw-r--r--drivers/usb/gadget/r8a66597-udc.h73
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c10
-rw-r--r--drivers/usb/gadget/s3c-hsudc.c69
-rw-r--r--drivers/usb/gadget/s3c2410_udc.c16
-rw-r--r--drivers/usb/gadget/s3c2410_udc.h10
-rw-r--r--drivers/usb/gadget/storage_common.c185
-rw-r--r--drivers/usb/gadget/u_ether.c9
-rw-r--r--drivers/usb/gadget/u_ether.h9
-rw-r--r--drivers/usb/gadget/u_serial.c4
-rw-r--r--drivers/usb/gadget/udc-core.c31
-rw-r--r--drivers/usb/gadget/uvc.h2
-rw-r--r--drivers/usb/gadget/uvc_queue.c1
-rw-r--r--drivers/usb/gadget/uvc_v4l2.c2
-rw-r--r--drivers/usb/gadget/uvc_video.c1
-rw-r--r--drivers/usb/gadget/webcam.c2
-rw-r--r--drivers/usb/gadget/zero.c9
-rw-r--r--drivers/usb/host/Kconfig28
-rw-r--r--drivers/usb/host/Makefile4
-rw-r--r--drivers/usb/host/alchemy-common.c337
-rw-r--r--drivers/usb/host/ehci-ath79.c2
-rw-r--r--drivers/usb/host/ehci-au1xxx.c81
-rw-r--r--drivers/usb/host/ehci-dbg.c19
-rw-r--r--drivers/usb/host/ehci-fsl.c6
-rw-r--r--drivers/usb/host/ehci-hcd.c66
-rw-r--r--drivers/usb/host/ehci-hub.c10
-rw-r--r--drivers/usb/host/ehci-mxc.c2
-rw-r--r--drivers/usb/host/ehci-octeon.c2
-rw-r--r--drivers/usb/host/ehci-omap.c2
-rw-r--r--drivers/usb/host/ehci-orion.c2
-rw-r--r--drivers/usb/host/ehci-pci.c7
-rw-r--r--drivers/usb/host/ehci-ps3.c2
-rw-r--r--drivers/usb/host/ehci-pxa168.c363
-rw-r--r--drivers/usb/host/ehci-q.c31
-rw-r--r--drivers/usb/host/ehci-s5p.c4
-rw-r--r--drivers/usb/host/ehci-sched.c56
-rw-r--r--drivers/usb/host/ehci-sh.c2
-rw-r--r--drivers/usb/host/ehci-spear.c2
-rw-r--r--drivers/usb/host/ehci-tegra.c2
-rw-r--r--drivers/usb/host/ehci-vt8500.c2
-rw-r--r--drivers/usb/host/ehci-xls.c161
-rw-r--r--drivers/usb/host/ehci.h24
-rw-r--r--drivers/usb/host/fhci-hcd.c9
-rw-r--r--drivers/usb/host/fhci-sched.c19
-rw-r--r--drivers/usb/host/fsl-mph-dr-of.c1
-rw-r--r--drivers/usb/host/imx21-hcd.c2
-rw-r--r--drivers/usb/host/isp116x-hcd.c2
-rw-r--r--drivers/usb/host/isp1362-hcd.c8
-rw-r--r--drivers/usb/host/isp1760-hcd.c418
-rw-r--r--drivers/usb/host/isp1760-hcd.h5
-rw-r--r--drivers/usb/host/isp1760-if.c81
-rw-r--r--drivers/usb/host/ohci-at91.c245
-rw-r--r--drivers/usb/host/ohci-ath79.c2
-rw-r--r--drivers/usb/host/ohci-au1xxx.c112
-rw-r--r--drivers/usb/host/ohci-da8xx.c2
-rw-r--r--drivers/usb/host/ohci-ep93xx.c2
-rw-r--r--drivers/usb/host/ohci-hcd.c20
-rw-r--r--drivers/usb/host/ohci-hub.c5
-rw-r--r--drivers/usb/host/ohci-octeon.c2
-rw-r--r--drivers/usb/host/ohci-omap.c4
-rw-r--r--drivers/usb/host/ohci-omap3.c4
-rw-r--r--drivers/usb/host/ohci-pci.c26
-rw-r--r--drivers/usb/host/ohci-pnx4008.c4
-rw-r--r--drivers/usb/host/ohci-pnx8550.c2
-rw-r--r--drivers/usb/host/ohci-ppc-of.c2
-rw-r--r--drivers/usb/host/ohci-ppc-soc.c2
-rw-r--r--drivers/usb/host/ohci-ps3.c2
-rw-r--r--drivers/usb/host/ohci-pxa27x.c2
-rw-r--r--drivers/usb/host/ohci-q.c4
-rw-r--r--drivers/usb/host/ohci-s3c2410.c2
-rw-r--r--drivers/usb/host/ohci-sa1111.c2
-rw-r--r--drivers/usb/host/ohci-sh.c2
-rw-r--r--drivers/usb/host/ohci-sm501.c2
-rw-r--r--drivers/usb/host/ohci-spear.c2
-rw-r--r--drivers/usb/host/ohci-ssb.c2
-rw-r--r--drivers/usb/host/ohci-tmio.c2
-rw-r--r--drivers/usb/host/ohci-xls.c151
-rw-r--r--drivers/usb/host/ohci.h1
-rw-r--r--drivers/usb/host/pci-quirks.c58
-rw-r--r--drivers/usb/host/r8a66597-hcd.c4
-rw-r--r--drivers/usb/host/sl811-hcd.c2
-rw-r--r--drivers/usb/host/uhci-hcd.c66
-rw-r--r--drivers/usb/host/uhci-q.c6
-rw-r--r--drivers/usb/host/whci/debug.c1
-rw-r--r--drivers/usb/host/whci/hcd.c1
-rw-r--r--drivers/usb/host/xhci-ext-caps.h6
-rw-r--r--drivers/usb/host/xhci-hub.c142
-rw-r--r--drivers/usb/host/xhci-mem.c376
-rw-r--r--drivers/usb/host/xhci-pci.c108
-rw-r--r--drivers/usb/host/xhci-ring.c112
-rw-r--r--drivers/usb/host/xhci.c1185
-rw-r--r--drivers/usb/host/xhci.h187
-rw-r--r--drivers/usb/misc/adutux.c14
-rw-r--r--drivers/usb/misc/ftdi-elan.c2
-rw-r--r--drivers/usb/misc/idmouse.c2
-rw-r--r--drivers/usb/misc/iowarrior.c2
-rw-r--r--drivers/usb/misc/ldusb.c4
-rw-r--r--drivers/usb/misc/legousbtower.c4
-rw-r--r--drivers/usb/misc/usblcd.c129
-rw-r--r--drivers/usb/misc/usbled.c25
-rw-r--r--drivers/usb/misc/usbtest.c31
-rw-r--r--drivers/usb/mon/mon_bin.c3
-rw-r--r--drivers/usb/mon/mon_stat.c1
-rw-r--r--drivers/usb/mon/mon_text.c1
-rw-r--r--drivers/usb/musb/Kconfig3
-rw-r--r--drivers/usb/musb/am35x.c1
-rw-r--r--drivers/usb/musb/da8xx.c1
-rw-r--r--drivers/usb/musb/davinci.c2
-rw-r--r--drivers/usb/musb/musb_core.c3
-rw-r--r--drivers/usb/musb/musb_debugfs.c6
-rw-r--r--drivers/usb/musb/musb_gadget.c77
-rw-r--r--drivers/usb/musb/musb_gadget_ep0.c8
-rw-r--r--drivers/usb/musb/musb_host.c2
-rw-r--r--drivers/usb/musb/musbhsdma.c2
-rw-r--r--drivers/usb/otg/gpio_vbus.c1
-rw-r--r--drivers/usb/otg/isp1301_omap.c2
-rw-r--r--drivers/usb/otg/otg.c1
-rw-r--r--drivers/usb/otg/twl6030-usb.c16
-rw-r--r--drivers/usb/otg/ulpi.c1
-rw-r--r--drivers/usb/renesas_usbhs/Kconfig2
-rw-r--r--drivers/usb/renesas_usbhs/Makefile8
-rw-r--r--drivers/usb/renesas_usbhs/common.c238
-rw-r--r--drivers/usb/renesas_usbhs/common.h60
-rw-r--r--drivers/usb/renesas_usbhs/fifo.c222
-rw-r--r--drivers/usb/renesas_usbhs/fifo.h22
-rw-r--r--drivers/usb/renesas_usbhs/mod.c81
-rw-r--r--drivers/usb/renesas_usbhs/mod.h61
-rw-r--r--drivers/usb/renesas_usbhs/mod_gadget.c134
-rw-r--r--drivers/usb/renesas_usbhs/mod_host.c1327
-rw-r--r--drivers/usb/renesas_usbhs/pipe.c200
-rw-r--r--drivers/usb/renesas_usbhs/pipe.h27
-rw-r--r--drivers/usb/serial/Kconfig1
-rw-r--r--drivers/usb/serial/aircable.c1
-rw-r--r--drivers/usb/serial/ark3116.c10
-rw-r--r--drivers/usb/serial/digi_acceleport.c2
-rw-r--r--drivers/usb/serial/ftdi_sio.c105
-rw-r--r--drivers/usb/serial/ftdi_sio_ids.h7
-rw-r--r--drivers/usb/serial/io_edgeport.c4
-rw-r--r--drivers/usb/serial/ipw.c33
-rw-r--r--drivers/usb/serial/mos7720.c1
-rw-r--r--drivers/usb/serial/mos7840.c1
-rw-r--r--drivers/usb/serial/opticon.c2
-rw-r--r--drivers/usb/serial/option.c198
-rw-r--r--drivers/usb/serial/pl2303.c5
-rw-r--r--drivers/usb/serial/pl2303.h7
-rw-r--r--drivers/usb/serial/qcserial.c3
-rw-r--r--drivers/usb/serial/sierra.c2
-rw-r--r--drivers/usb/serial/symbolserial.c2
-rw-r--r--drivers/usb/serial/usb-serial.c8
-rw-r--r--drivers/usb/serial/usb_wwan.c2
-rw-r--r--drivers/usb/storage/Kconfig2
-rw-r--r--drivers/usb/storage/ene_ub6250.c3
-rw-r--r--drivers/usb/storage/option_ms.c1
-rw-r--r--drivers/usb/storage/protocol.c8
-rw-r--r--drivers/usb/storage/realtek_cr.c117
-rw-r--r--drivers/usb/storage/sierra_ms.c1
-rw-r--r--drivers/usb/storage/transport.c35
-rw-r--r--drivers/usb/storage/uas.c1
-rw-r--r--drivers/usb/storage/usb.c16
-rw-r--r--drivers/usb/usb-common.c35
-rw-r--r--drivers/usb/usb-skeleton.c2
-rw-r--r--drivers/usb/wusbcore/devconnect.c1
-rw-r--r--drivers/usb/wusbcore/mmc.c1
-rw-r--r--drivers/usb/wusbcore/rh.c1
-rw-r--r--drivers/usb/wusbcore/security.c1
-rw-r--r--drivers/usb/wusbcore/wa-hc.c3
-rw-r--r--drivers/usb/wusbcore/wa-rpipe.c1
-rw-r--r--drivers/usb/wusbcore/wa-xfer.c1
267 files changed, 15600 insertions, 4563 deletions
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index 48f1781352f1..791f11bed606 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -12,6 +12,11 @@ menuconfig USB_SUPPORT
12 12
13if USB_SUPPORT 13if USB_SUPPORT
14 14
15config USB_COMMON
16 tristate
17 default y
18 depends on USB || USB_GADGET
19
15# Host-side USB depends on having a host controller 20# Host-side USB depends on having a host controller
16# NOTE: dummy_hcd is always an option, but it's ignored here ... 21# NOTE: dummy_hcd is always an option, but it's ignored here ...
17# NOTE: SL-811 option should be board-specific ... 22# NOTE: SL-811 option should be board-specific ...
@@ -19,6 +24,7 @@ config USB_ARCH_HAS_HCD
19 boolean 24 boolean
20 default y if USB_ARCH_HAS_OHCI 25 default y if USB_ARCH_HAS_OHCI
21 default y if USB_ARCH_HAS_EHCI 26 default y if USB_ARCH_HAS_EHCI
27 default y if USB_ARCH_HAS_XHCI
22 default y if PCMCIA && !M32R # sl811_cs 28 default y if PCMCIA && !M32R # sl811_cs
23 default y if ARM # SL-811 29 default y if ARM # SL-811
24 default y if BLACKFIN # SL-811 30 default y if BLACKFIN # SL-811
@@ -54,9 +60,8 @@ config USB_ARCH_HAS_OHCI
54# some non-PCI hcds implement EHCI 60# some non-PCI hcds implement EHCI
55config USB_ARCH_HAS_EHCI 61config USB_ARCH_HAS_EHCI
56 boolean 62 boolean
57 default y if PPC_83xx 63 default y if FSL_SOC
58 default y if PPC_MPC512x 64 default y if PPC_MPC512x
59 default y if SOC_AU1200
60 default y if ARCH_IXP4XX 65 default y if ARCH_IXP4XX
61 default y if ARCH_W90X900 66 default y if ARCH_W90X900
62 default y if ARCH_AT91SAM9G45 67 default y if ARCH_AT91SAM9G45
@@ -69,6 +74,12 @@ config USB_ARCH_HAS_EHCI
69 default y if ARCH_MSM 74 default y if ARCH_MSM
70 default y if MICROBLAZE 75 default y if MICROBLAZE
71 default y if SPARC_LEON 76 default y if SPARC_LEON
77 default y if ARCH_MMP
78 default PCI
79
80# some non-PCI HCDs implement xHCI
81config USB_ARCH_HAS_XHCI
82 boolean
72 default PCI 83 default PCI
73 84
74# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface. 85# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface.
@@ -110,6 +121,8 @@ config USB
110 121
111source "drivers/usb/core/Kconfig" 122source "drivers/usb/core/Kconfig"
112 123
124source "drivers/usb/dwc3/Kconfig"
125
113source "drivers/usb/mon/Kconfig" 126source "drivers/usb/mon/Kconfig"
114 127
115source "drivers/usb/wusbcore/Kconfig" 128source "drivers/usb/wusbcore/Kconfig"
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 30ddf8dc4f72..75eca7645227 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -6,6 +6,8 @@
6 6
7obj-$(CONFIG_USB) += core/ 7obj-$(CONFIG_USB) += core/
8 8
9obj-$(CONFIG_USB_DWC3) += dwc3/
10
9obj-$(CONFIG_USB_MON) += mon/ 11obj-$(CONFIG_USB_MON) += mon/
10 12
11obj-$(CONFIG_PCI) += host/ 13obj-$(CONFIG_PCI) += host/
@@ -51,3 +53,5 @@ obj-$(CONFIG_USB_MUSB_HDRC) += musb/
51obj-$(CONFIG_USB_RENESAS_USBHS) += renesas_usbhs/ 53obj-$(CONFIG_USB_RENESAS_USBHS) += renesas_usbhs/
52obj-$(CONFIG_USB_OTG_UTILS) += otg/ 54obj-$(CONFIG_USB_OTG_UTILS) += otg/
53obj-$(CONFIG_USB_GADGET) += gadget/ 55obj-$(CONFIG_USB_GADGET) += gadget/
56
57obj-$(CONFIG_USB_COMMON) += usb-common.o
diff --git a/drivers/usb/c67x00/c67x00-drv.c b/drivers/usb/c67x00/c67x00-drv.c
index 62050f7a4f97..57ae44cd0b88 100644
--- a/drivers/usb/c67x00/c67x00-drv.c
+++ b/drivers/usb/c67x00/c67x00-drv.c
@@ -38,6 +38,7 @@
38#include <linux/io.h> 38#include <linux/io.h>
39#include <linux/list.h> 39#include <linux/list.h>
40#include <linux/slab.h> 40#include <linux/slab.h>
41#include <linux/module.h>
41#include <linux/usb.h> 42#include <linux/usb.h>
42#include <linux/usb/c67x00.h> 43#include <linux/usb/c67x00.h>
43 44
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index dac7676ce21b..e8c564a53346 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -539,7 +539,6 @@ static void acm_port_down(struct acm *acm)
539{ 539{
540 int i; 540 int i;
541 541
542 mutex_lock(&open_mutex);
543 if (acm->dev) { 542 if (acm->dev) {
544 usb_autopm_get_interface(acm->control); 543 usb_autopm_get_interface(acm->control);
545 acm_set_control(acm, acm->ctrlout = 0); 544 acm_set_control(acm, acm->ctrlout = 0);
@@ -551,14 +550,15 @@ static void acm_port_down(struct acm *acm)
551 acm->control->needs_remote_wakeup = 0; 550 acm->control->needs_remote_wakeup = 0;
552 usb_autopm_put_interface(acm->control); 551 usb_autopm_put_interface(acm->control);
553 } 552 }
554 mutex_unlock(&open_mutex);
555} 553}
556 554
557static void acm_tty_hangup(struct tty_struct *tty) 555static void acm_tty_hangup(struct tty_struct *tty)
558{ 556{
559 struct acm *acm = tty->driver_data; 557 struct acm *acm = tty->driver_data;
560 tty_port_hangup(&acm->port); 558 tty_port_hangup(&acm->port);
559 mutex_lock(&open_mutex);
561 acm_port_down(acm); 560 acm_port_down(acm);
561 mutex_unlock(&open_mutex);
562} 562}
563 563
564static void acm_tty_close(struct tty_struct *tty, struct file *filp) 564static void acm_tty_close(struct tty_struct *tty, struct file *filp)
@@ -569,8 +569,9 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
569 shutdown */ 569 shutdown */
570 if (!acm) 570 if (!acm)
571 return; 571 return;
572
573 mutex_lock(&open_mutex);
572 if (tty_port_close_start(&acm->port, tty, filp) == 0) { 574 if (tty_port_close_start(&acm->port, tty, filp) == 0) {
573 mutex_lock(&open_mutex);
574 if (!acm->dev) { 575 if (!acm->dev) {
575 tty_port_tty_set(&acm->port, NULL); 576 tty_port_tty_set(&acm->port, NULL);
576 acm_tty_unregister(acm); 577 acm_tty_unregister(acm);
@@ -582,6 +583,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
582 acm_port_down(acm); 583 acm_port_down(acm);
583 tty_port_close_end(&acm->port, tty); 584 tty_port_close_end(&acm->port, tty);
584 tty_port_tty_set(&acm->port, NULL); 585 tty_port_tty_set(&acm->port, NULL);
586 mutex_unlock(&open_mutex);
585} 587}
586 588
587static int acm_tty_write(struct tty_struct *tty, 589static int acm_tty_write(struct tty_struct *tty,
@@ -1058,11 +1060,11 @@ made_compressed_probe:
1058 goto alloc_fail; 1060 goto alloc_fail;
1059 } 1061 }
1060 1062
1061 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); 1063 ctrlsize = usb_endpoint_maxp(epctrl);
1062 readsize = le16_to_cpu(epread->wMaxPacketSize) * 1064 readsize = usb_endpoint_maxp(epread) *
1063 (quirks == SINGLE_RX_URB ? 1 : 2); 1065 (quirks == SINGLE_RX_URB ? 1 : 2);
1064 acm->combined_interfaces = combined_interfaces; 1066 acm->combined_interfaces = combined_interfaces;
1065 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20; 1067 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1066 acm->control = control_interface; 1068 acm->control = control_interface;
1067 acm->data = data_interface; 1069 acm->data = data_interface;
1068 acm->minor = minor; 1070 acm->minor = minor;
@@ -1305,7 +1307,7 @@ static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1305 struct acm *acm = usb_get_intfdata(intf); 1307 struct acm *acm = usb_get_intfdata(intf);
1306 int cnt; 1308 int cnt;
1307 1309
1308 if (message.event & PM_EVENT_AUTO) { 1310 if (PMSG_IS_AUTO(message)) {
1309 int b; 1311 int b;
1310 1312
1311 spin_lock_irq(&acm->write_lock); 1313 spin_lock_irq(&acm->write_lock);
@@ -1534,6 +1536,9 @@ static const struct usb_device_id acm_ids[] = {
1534 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */ 1536 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1535 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */ 1537 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1536 1538
1539 /* Support for Owen devices */
1540 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1541
1537 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */ 1542 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1538 1543
1539 /* Support Lego NXT using pbLua firmware */ 1544 /* Support Lego NXT using pbLua firmware */
diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
index 2b9ff518b509..efe684908c1f 100644
--- a/drivers/usb/class/cdc-wdm.c
+++ b/drivers/usb/class/cdc-wdm.c
@@ -682,7 +682,7 @@ next_desc:
682 if (!ep || !usb_endpoint_is_int_in(ep)) 682 if (!ep || !usb_endpoint_is_int_in(ep))
683 goto err; 683 goto err;
684 684
685 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize); 685 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
686 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0; 686 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
687 687
688 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 688 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
@@ -798,11 +798,11 @@ static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
798 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); 798 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
799 799
800 /* if this is an autosuspend the caller does the locking */ 800 /* if this is an autosuspend the caller does the locking */
801 if (!(message.event & PM_EVENT_AUTO)) 801 if (!PMSG_IS_AUTO(message))
802 mutex_lock(&desc->lock); 802 mutex_lock(&desc->lock);
803 spin_lock_irq(&desc->iuspin); 803 spin_lock_irq(&desc->iuspin);
804 804
805 if ((message.event & PM_EVENT_AUTO) && 805 if (PMSG_IS_AUTO(message) &&
806 (test_bit(WDM_IN_USE, &desc->flags) 806 (test_bit(WDM_IN_USE, &desc->flags)
807 || test_bit(WDM_RESPONDING, &desc->flags))) { 807 || test_bit(WDM_RESPONDING, &desc->flags))) {
808 spin_unlock_irq(&desc->iuspin); 808 spin_unlock_irq(&desc->iuspin);
@@ -815,7 +815,7 @@ static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
815 kill_urbs(desc); 815 kill_urbs(desc);
816 cancel_work_sync(&desc->rxwork); 816 cancel_work_sync(&desc->rxwork);
817 } 817 }
818 if (!(message.event & PM_EVENT_AUTO)) 818 if (!PMSG_IS_AUTO(message))
819 mutex_unlock(&desc->lock); 819 mutex_unlock(&desc->lock);
820 820
821 return rv; 821 return rv;
diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index 3f94ac34dce3..12cf5e7395a8 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -186,8 +186,7 @@ static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
186 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) 186 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
187 if (current_setting->endpoint[n].desc.bEndpointAddress == 187 if (current_setting->endpoint[n].desc.bEndpointAddress ==
188 data->bulk_in) 188 data->bulk_in)
189 max_size = le16_to_cpu(current_setting->endpoint[n]. 189 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
190 desc.wMaxPacketSize);
191 190
192 if (max_size == 0) { 191 if (max_size == 0) {
193 dev_err(dev, "Couldn't get wMaxPacketSize\n"); 192 dev_err(dev, "Couldn't get wMaxPacketSize\n");
@@ -636,7 +635,7 @@ static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
636 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) { 635 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
637 desc = &current_setting->endpoint[n].desc; 636 desc = &current_setting->endpoint[n].desc;
638 if (desc->bEndpointAddress == data->bulk_in) 637 if (desc->bEndpointAddress == data->bulk_in)
639 max_size = le16_to_cpu(desc->wMaxPacketSize); 638 max_size = usb_endpoint_maxp(desc);
640 } 639 }
641 640
642 if (max_size == 0) { 641 if (max_size == 0) {
diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 26678cadfb21..f4bdd0ce8d56 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -124,9 +124,9 @@ static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
124 124
125 if (usb_endpoint_xfer_isoc(&ep->desc)) 125 if (usb_endpoint_xfer_isoc(&ep->desc))
126 max_tx = (desc->bMaxBurst + 1) * (desc->bmAttributes + 1) * 126 max_tx = (desc->bMaxBurst + 1) * (desc->bmAttributes + 1) *
127 le16_to_cpu(ep->desc.wMaxPacketSize); 127 usb_endpoint_maxp(&ep->desc);
128 else if (usb_endpoint_xfer_int(&ep->desc)) 128 else if (usb_endpoint_xfer_int(&ep->desc))
129 max_tx = le16_to_cpu(ep->desc.wMaxPacketSize) * 129 max_tx = usb_endpoint_maxp(&ep->desc) *
130 (desc->bMaxBurst + 1); 130 (desc->bMaxBurst + 1);
131 else 131 else
132 max_tx = 999999; 132 max_tx = 999999;
@@ -241,7 +241,7 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
241 cfgno, inum, asnum, d->bEndpointAddress); 241 cfgno, inum, asnum, d->bEndpointAddress);
242 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT; 242 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
243 endpoint->desc.bInterval = 1; 243 endpoint->desc.bInterval = 1;
244 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8) 244 if (usb_endpoint_maxp(&endpoint->desc) > 8)
245 endpoint->desc.wMaxPacketSize = cpu_to_le16(8); 245 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
246 } 246 }
247 247
@@ -254,7 +254,7 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
254 && usb_endpoint_xfer_bulk(d)) { 254 && usb_endpoint_xfer_bulk(d)) {
255 unsigned maxp; 255 unsigned maxp;
256 256
257 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff; 257 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
258 if (maxp != 512) 258 if (maxp != 512)
259 dev_warn(ddev, "config %d interface %d altsetting %d " 259 dev_warn(ddev, "config %d interface %d altsetting %d "
260 "bulk endpoint 0x%X has invalid maxpacket %d\n", 260 "bulk endpoint 0x%X has invalid maxpacket %d\n",
@@ -755,3 +755,106 @@ err2:
755 dev_err(ddev, "out of memory\n"); 755 dev_err(ddev, "out of memory\n");
756 return result; 756 return result;
757} 757}
758
759void usb_release_bos_descriptor(struct usb_device *dev)
760{
761 if (dev->bos) {
762 kfree(dev->bos->desc);
763 kfree(dev->bos);
764 dev->bos = NULL;
765 }
766}
767
768/* Get BOS descriptor set */
769int usb_get_bos_descriptor(struct usb_device *dev)
770{
771 struct device *ddev = &dev->dev;
772 struct usb_bos_descriptor *bos;
773 struct usb_dev_cap_header *cap;
774 unsigned char *buffer;
775 int length, total_len, num, i;
776 int ret;
777
778 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
779 if (!bos)
780 return -ENOMEM;
781
782 /* Get BOS descriptor */
783 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
784 if (ret < USB_DT_BOS_SIZE) {
785 dev_err(ddev, "unable to get BOS descriptor\n");
786 if (ret >= 0)
787 ret = -ENOMSG;
788 kfree(bos);
789 return ret;
790 }
791
792 length = bos->bLength;
793 total_len = le16_to_cpu(bos->wTotalLength);
794 num = bos->bNumDeviceCaps;
795 kfree(bos);
796 if (total_len < length)
797 return -EINVAL;
798
799 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
800 if (!dev->bos)
801 return -ENOMEM;
802
803 /* Now let's get the whole BOS descriptor set */
804 buffer = kzalloc(total_len, GFP_KERNEL);
805 if (!buffer) {
806 ret = -ENOMEM;
807 goto err;
808 }
809 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
810
811 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
812 if (ret < total_len) {
813 dev_err(ddev, "unable to get BOS descriptor set\n");
814 if (ret >= 0)
815 ret = -ENOMSG;
816 goto err;
817 }
818 total_len -= length;
819
820 for (i = 0; i < num; i++) {
821 buffer += length;
822 cap = (struct usb_dev_cap_header *)buffer;
823 length = cap->bLength;
824
825 if (total_len < length)
826 break;
827 total_len -= length;
828
829 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
830 dev_warn(ddev, "descriptor type invalid, skip\n");
831 continue;
832 }
833
834 switch (cap->bDevCapabilityType) {
835 case USB_CAP_TYPE_WIRELESS_USB:
836 /* Wireless USB cap descriptor is handled by wusb */
837 break;
838 case USB_CAP_TYPE_EXT:
839 dev->bos->ext_cap =
840 (struct usb_ext_cap_descriptor *)buffer;
841 break;
842 case USB_SS_CAP_TYPE:
843 dev->bos->ss_cap =
844 (struct usb_ss_cap_descriptor *)buffer;
845 break;
846 case CONTAINER_ID_TYPE:
847 dev->bos->ss_id =
848 (struct usb_ss_container_id_descriptor *)buffer;
849 break;
850 default:
851 break;
852 }
853 }
854
855 return 0;
856
857err:
858 usb_release_bos_descriptor(dev);
859 return ret;
860}
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 0149c0976e9c..d95696584762 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -190,7 +190,7 @@ static char *usb_dump_endpoint_descriptor(int speed, char *start, char *end,
190 dir = usb_endpoint_dir_in(desc) ? 'I' : 'O'; 190 dir = usb_endpoint_dir_in(desc) ? 'I' : 'O';
191 191
192 if (speed == USB_SPEED_HIGH) { 192 if (speed == USB_SPEED_HIGH) {
193 switch (le16_to_cpu(desc->wMaxPacketSize) & (0x03 << 11)) { 193 switch (usb_endpoint_maxp(desc) & (0x03 << 11)) {
194 case 1 << 11: 194 case 1 << 11:
195 bandwidth = 2; break; 195 bandwidth = 2; break;
196 case 2 << 11: 196 case 2 << 11:
@@ -240,7 +240,7 @@ static char *usb_dump_endpoint_descriptor(int speed, char *start, char *end,
240 240
241 start += sprintf(start, format_endpt, desc->bEndpointAddress, dir, 241 start += sprintf(start, format_endpt, desc->bEndpointAddress, dir,
242 desc->bmAttributes, type, 242 desc->bmAttributes, type,
243 (le16_to_cpu(desc->wMaxPacketSize) & 0x07ff) * 243 (usb_endpoint_maxp(desc) & 0x07ff) *
244 bandwidth, 244 bandwidth,
245 interval, unit); 245 interval, unit);
246 return start; 246 return start;
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 37518dfdeb98..e3beaf229ee3 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -46,6 +46,7 @@
46#include <linux/cdev.h> 46#include <linux/cdev.h>
47#include <linux/notifier.h> 47#include <linux/notifier.h>
48#include <linux/security.h> 48#include <linux/security.h>
49#include <linux/user_namespace.h>
49#include <asm/uaccess.h> 50#include <asm/uaccess.h>
50#include <asm/byteorder.h> 51#include <asm/byteorder.h>
51#include <linux/moduleparam.h> 52#include <linux/moduleparam.h>
@@ -68,7 +69,7 @@ struct dev_state {
68 wait_queue_head_t wait; /* wake up if a request completed */ 69 wait_queue_head_t wait; /* wake up if a request completed */
69 unsigned int discsignr; 70 unsigned int discsignr;
70 struct pid *disc_pid; 71 struct pid *disc_pid;
71 uid_t disc_uid, disc_euid; 72 const struct cred *cred;
72 void __user *disccontext; 73 void __user *disccontext;
73 unsigned long ifclaimed; 74 unsigned long ifclaimed;
74 u32 secid; 75 u32 secid;
@@ -79,7 +80,7 @@ struct async {
79 struct list_head asynclist; 80 struct list_head asynclist;
80 struct dev_state *ps; 81 struct dev_state *ps;
81 struct pid *pid; 82 struct pid *pid;
82 uid_t uid, euid; 83 const struct cred *cred;
83 unsigned int signr; 84 unsigned int signr;
84 unsigned int ifnum; 85 unsigned int ifnum;
85 void __user *userbuffer; 86 void __user *userbuffer;
@@ -248,6 +249,7 @@ static struct async *alloc_async(unsigned int numisoframes)
248static void free_async(struct async *as) 249static void free_async(struct async *as)
249{ 250{
250 put_pid(as->pid); 251 put_pid(as->pid);
252 put_cred(as->cred);
251 kfree(as->urb->transfer_buffer); 253 kfree(as->urb->transfer_buffer);
252 kfree(as->urb->setup_packet); 254 kfree(as->urb->setup_packet);
253 usb_free_urb(as->urb); 255 usb_free_urb(as->urb);
@@ -393,9 +395,8 @@ static void async_completed(struct urb *urb)
393 struct dev_state *ps = as->ps; 395 struct dev_state *ps = as->ps;
394 struct siginfo sinfo; 396 struct siginfo sinfo;
395 struct pid *pid = NULL; 397 struct pid *pid = NULL;
396 uid_t uid = 0;
397 uid_t euid = 0;
398 u32 secid = 0; 398 u32 secid = 0;
399 const struct cred *cred = NULL;
399 int signr; 400 int signr;
400 401
401 spin_lock(&ps->lock); 402 spin_lock(&ps->lock);
@@ -407,9 +408,8 @@ static void async_completed(struct urb *urb)
407 sinfo.si_errno = as->status; 408 sinfo.si_errno = as->status;
408 sinfo.si_code = SI_ASYNCIO; 409 sinfo.si_code = SI_ASYNCIO;
409 sinfo.si_addr = as->userurb; 410 sinfo.si_addr = as->userurb;
410 pid = as->pid; 411 pid = get_pid(as->pid);
411 uid = as->uid; 412 cred = get_cred(as->cred);
412 euid = as->euid;
413 secid = as->secid; 413 secid = as->secid;
414 } 414 }
415 snoop(&urb->dev->dev, "urb complete\n"); 415 snoop(&urb->dev->dev, "urb complete\n");
@@ -422,9 +422,11 @@ static void async_completed(struct urb *urb)
422 cancel_bulk_urbs(ps, as->bulk_addr); 422 cancel_bulk_urbs(ps, as->bulk_addr);
423 spin_unlock(&ps->lock); 423 spin_unlock(&ps->lock);
424 424
425 if (signr) 425 if (signr) {
426 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid, 426 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
427 euid, secid); 427 put_pid(pid);
428 put_cred(cred);
429 }
428 430
429 wake_up(&ps->wait); 431 wake_up(&ps->wait);
430} 432}
@@ -607,9 +609,10 @@ static int findintfep(struct usb_device *dev, unsigned int ep)
607} 609}
608 610
609static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, 611static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
610 unsigned int index) 612 unsigned int request, unsigned int index)
611{ 613{
612 int ret = 0; 614 int ret = 0;
615 struct usb_host_interface *alt_setting;
613 616
614 if (ps->dev->state != USB_STATE_UNAUTHENTICATED 617 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
615 && ps->dev->state != USB_STATE_ADDRESS 618 && ps->dev->state != USB_STATE_ADDRESS
@@ -618,6 +621,19 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
618 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 621 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
619 return 0; 622 return 0;
620 623
624 /*
625 * check for the special corner case 'get_device_id' in the printer
626 * class specification, where wIndex is (interface << 8 | altsetting)
627 * instead of just interface
628 */
629 if (requesttype == 0xa1 && request == 0) {
630 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
631 index >> 8, index & 0xff);
632 if (alt_setting
633 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
634 index >>= 8;
635 }
636
621 index &= 0xff; 637 index &= 0xff;
622 switch (requesttype & USB_RECIP_MASK) { 638 switch (requesttype & USB_RECIP_MASK) {
623 case USB_RECIP_ENDPOINT: 639 case USB_RECIP_ENDPOINT:
@@ -656,7 +672,6 @@ static int usbdev_open(struct inode *inode, struct file *file)
656{ 672{
657 struct usb_device *dev = NULL; 673 struct usb_device *dev = NULL;
658 struct dev_state *ps; 674 struct dev_state *ps;
659 const struct cred *cred = current_cred();
660 int ret; 675 int ret;
661 676
662 ret = -ENOMEM; 677 ret = -ENOMEM;
@@ -706,8 +721,7 @@ static int usbdev_open(struct inode *inode, struct file *file)
706 init_waitqueue_head(&ps->wait); 721 init_waitqueue_head(&ps->wait);
707 ps->discsignr = 0; 722 ps->discsignr = 0;
708 ps->disc_pid = get_pid(task_pid(current)); 723 ps->disc_pid = get_pid(task_pid(current));
709 ps->disc_uid = cred->uid; 724 ps->cred = get_current_cred();
710 ps->disc_euid = cred->euid;
711 ps->disccontext = NULL; 725 ps->disccontext = NULL;
712 ps->ifclaimed = 0; 726 ps->ifclaimed = 0;
713 security_task_getsecid(current, &ps->secid); 727 security_task_getsecid(current, &ps->secid);
@@ -749,6 +763,7 @@ static int usbdev_release(struct inode *inode, struct file *file)
749 usb_unlock_device(dev); 763 usb_unlock_device(dev);
750 usb_put_dev(dev); 764 usb_put_dev(dev);
751 put_pid(ps->disc_pid); 765 put_pid(ps->disc_pid);
766 put_cred(ps->cred);
752 767
753 as = async_getcompleted(ps); 768 as = async_getcompleted(ps);
754 while (as) { 769 while (as) {
@@ -770,7 +785,8 @@ static int proc_control(struct dev_state *ps, void __user *arg)
770 785
771 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 786 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
772 return -EFAULT; 787 return -EFAULT;
773 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex); 788 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
789 ctrl.wIndex);
774 if (ret) 790 if (ret)
775 return ret; 791 return ret;
776 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ 792 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
@@ -1048,7 +1064,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1048 struct usb_host_endpoint *ep; 1064 struct usb_host_endpoint *ep;
1049 struct async *as; 1065 struct async *as;
1050 struct usb_ctrlrequest *dr = NULL; 1066 struct usb_ctrlrequest *dr = NULL;
1051 const struct cred *cred = current_cred();
1052 unsigned int u, totlen, isofrmlen; 1067 unsigned int u, totlen, isofrmlen;
1053 int ret, ifnum = -1; 1068 int ret, ifnum = -1;
1054 int is_in; 1069 int is_in;
@@ -1100,7 +1115,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1100 kfree(dr); 1115 kfree(dr);
1101 return -EINVAL; 1116 return -EINVAL;
1102 } 1117 }
1103 ret = check_ctrlrecip(ps, dr->bRequestType, 1118 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1104 le16_to_cpup(&dr->wIndex)); 1119 le16_to_cpup(&dr->wIndex));
1105 if (ret) { 1120 if (ret) {
1106 kfree(dr); 1121 kfree(dr);
@@ -1262,8 +1277,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1262 as->signr = uurb->signr; 1277 as->signr = uurb->signr;
1263 as->ifnum = ifnum; 1278 as->ifnum = ifnum;
1264 as->pid = get_pid(task_pid(current)); 1279 as->pid = get_pid(task_pid(current));
1265 as->uid = cred->uid; 1280 as->cred = get_current_cred();
1266 as->euid = cred->euid;
1267 security_task_getsecid(current, &as->secid); 1281 security_task_getsecid(current, &as->secid);
1268 if (!is_in && uurb->buffer_length > 0) { 1282 if (!is_in && uurb->buffer_length > 0) {
1269 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, 1283 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
@@ -1981,9 +1995,8 @@ static void usbdev_remove(struct usb_device *udev)
1981 sinfo.si_errno = EPIPE; 1995 sinfo.si_errno = EPIPE;
1982 sinfo.si_code = SI_ASYNCIO; 1996 sinfo.si_code = SI_ASYNCIO;
1983 sinfo.si_addr = ps->disccontext; 1997 sinfo.si_addr = ps->disccontext;
1984 kill_pid_info_as_uid(ps->discsignr, &sinfo, 1998 kill_pid_info_as_cred(ps->discsignr, &sinfo,
1985 ps->disc_pid, ps->disc_uid, 1999 ps->disc_pid, ps->cred, ps->secid);
1986 ps->disc_euid, ps->secid);
1987 } 2000 }
1988 } 2001 }
1989} 2002}
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 34e3da5aa72a..45887a0ff873 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -24,6 +24,7 @@
24 24
25#include <linux/device.h> 25#include <linux/device.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/export.h>
27#include <linux/usb.h> 28#include <linux/usb.h>
28#include <linux/usb/quirks.h> 29#include <linux/usb/quirks.h>
29#include <linux/usb/hcd.h> 30#include <linux/usb/hcd.h>
@@ -1046,8 +1047,7 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1046 /* Non-root devices on a full/low-speed bus must wait for their 1047 /* Non-root devices on a full/low-speed bus must wait for their
1047 * companion high-speed root hub, in case a handoff is needed. 1048 * companion high-speed root hub, in case a handoff is needed.
1048 */ 1049 */
1049 if (!(msg.event & PM_EVENT_AUTO) && udev->parent && 1050 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1050 udev->bus->hs_companion)
1051 device_pm_wait_for_dev(&udev->dev, 1051 device_pm_wait_for_dev(&udev->dev,
1052 &udev->bus->hs_companion->root_hub->dev); 1052 &udev->bus->hs_companion->root_hub->dev);
1053 1053
@@ -1075,7 +1075,7 @@ static int usb_suspend_interface(struct usb_device *udev,
1075 1075
1076 if (driver->suspend) { 1076 if (driver->suspend) {
1077 status = driver->suspend(intf, msg); 1077 status = driver->suspend(intf, msg);
1078 if (status && !(msg.event & PM_EVENT_AUTO)) 1078 if (status && !PMSG_IS_AUTO(msg))
1079 dev_err(&intf->dev, "%s error %d\n", 1079 dev_err(&intf->dev, "%s error %d\n",
1080 "suspend", status); 1080 "suspend", status);
1081 } else { 1081 } else {
@@ -1189,7 +1189,7 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1189 status = usb_suspend_interface(udev, intf, msg); 1189 status = usb_suspend_interface(udev, intf, msg);
1190 1190
1191 /* Ignore errors during system sleep transitions */ 1191 /* Ignore errors during system sleep transitions */
1192 if (!(msg.event & PM_EVENT_AUTO)) 1192 if (!PMSG_IS_AUTO(msg))
1193 status = 0; 1193 status = 0;
1194 if (status != 0) 1194 if (status != 0)
1195 break; 1195 break;
@@ -1199,7 +1199,7 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1199 status = usb_suspend_device(udev, msg); 1199 status = usb_suspend_device(udev, msg);
1200 1200
1201 /* Again, ignore errors during system sleep transitions */ 1201 /* Again, ignore errors during system sleep transitions */
1202 if (!(msg.event & PM_EVENT_AUTO)) 1202 if (!PMSG_IS_AUTO(msg))
1203 status = 0; 1203 status = 0;
1204 } 1204 }
1205 1205
@@ -1583,7 +1583,7 @@ int usb_autopm_get_interface_async(struct usb_interface *intf)
1583 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1583 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1584 __func__, atomic_read(&intf->dev.power.usage_count), 1584 __func__, atomic_read(&intf->dev.power.usage_count),
1585 status); 1585 status);
1586 if (status > 0) 1586 if (status > 0 || status == -EINPROGRESS)
1587 status = 0; 1587 status = 0;
1588 return status; 1588 return status;
1589} 1589}
@@ -1668,6 +1668,11 @@ int usb_runtime_suspend(struct device *dev)
1668 return -EAGAIN; 1668 return -EAGAIN;
1669 1669
1670 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1670 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1671
1672 /* Allow a retry if autosuspend failed temporarily */
1673 if (status == -EAGAIN || status == -EBUSY)
1674 usb_mark_last_busy(udev);
1675
1671 /* The PM core reacts badly unless the return code is 0, 1676 /* The PM core reacts badly unless the return code is 0,
1672 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error. 1677 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1673 */ 1678 */
@@ -1700,6 +1705,20 @@ int usb_runtime_idle(struct device *dev)
1700 return 0; 1705 return 0;
1701} 1706}
1702 1707
1708int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1709{
1710 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1711 int ret = -EPERM;
1712
1713 if (hcd->driver->set_usb2_hw_lpm) {
1714 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1715 if (!ret)
1716 udev->usb2_hw_lpm_enabled = enable;
1717 }
1718
1719 return ret;
1720}
1721
1703#endif /* CONFIG_USB_SUSPEND */ 1722#endif /* CONFIG_USB_SUSPEND */
1704 1723
1705struct bus_type usb_bus_type = { 1724struct bus_type usb_bus_type = {
diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c
index df502a98d0df..db7fe50c23d4 100644
--- a/drivers/usb/core/endpoint.c
+++ b/drivers/usb/core/endpoint.c
@@ -56,7 +56,7 @@ static ssize_t show_ep_wMaxPacketSize(struct device *dev,
56{ 56{
57 struct ep_device *ep = to_ep_device(dev); 57 struct ep_device *ep = to_ep_device(dev);
58 return sprintf(buf, "%04x\n", 58 return sprintf(buf, "%04x\n",
59 le16_to_cpu(ep->desc->wMaxPacketSize) & 0x07ff); 59 usb_endpoint_maxp(ep->desc) & 0x07ff);
60} 60}
61static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL); 61static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL);
62 62
diff --git a/drivers/usb/core/hcd-pci.c b/drivers/usb/core/hcd-pci.c
index ce22f4a84ed0..a004db35f6d0 100644
--- a/drivers/usb/core/hcd-pci.c
+++ b/drivers/usb/core/hcd-pci.c
@@ -242,7 +242,7 @@ int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
242 242
243 pci_set_master(dev); 243 pci_set_master(dev);
244 244
245 retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED); 245 retval = usb_add_hcd(hcd, dev->irq, IRQF_SHARED);
246 if (retval != 0) 246 if (retval != 0)
247 goto unmap_registers; 247 goto unmap_registers;
248 set_hs_companion(dev, hcd); 248 set_hs_companion(dev, hcd);
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 73cbbd85219f..13222d352a61 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -442,7 +442,11 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
442 struct usb_ctrlrequest *cmd; 442 struct usb_ctrlrequest *cmd;
443 u16 typeReq, wValue, wIndex, wLength; 443 u16 typeReq, wValue, wIndex, wLength;
444 u8 *ubuf = urb->transfer_buffer; 444 u8 *ubuf = urb->transfer_buffer;
445 u8 tbuf [sizeof (struct usb_hub_descriptor)] 445 /*
446 * tbuf should be as big as the BOS descriptor and
447 * the USB hub descriptor.
448 */
449 u8 tbuf[USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE]
446 __attribute__((aligned(4))); 450 __attribute__((aligned(4)));
447 const u8 *bufp = tbuf; 451 const u8 *bufp = tbuf;
448 unsigned len = 0; 452 unsigned len = 0;
@@ -562,6 +566,8 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
562 else /* unsupported IDs --> "protocol stall" */ 566 else /* unsupported IDs --> "protocol stall" */
563 goto error; 567 goto error;
564 break; 568 break;
569 case USB_DT_BOS << 8:
570 goto nongeneric;
565 default: 571 default:
566 goto error; 572 goto error;
567 } 573 }
@@ -596,6 +602,7 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
596 /* CLASS REQUESTS (and errors) */ 602 /* CLASS REQUESTS (and errors) */
597 603
598 default: 604 default:
605nongeneric:
599 /* non-generic request */ 606 /* non-generic request */
600 switch (typeReq) { 607 switch (typeReq) {
601 case GetHubStatus: 608 case GetHubStatus:
@@ -605,6 +612,9 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
605 case GetHubDescriptor: 612 case GetHubDescriptor:
606 len = sizeof (struct usb_hub_descriptor); 613 len = sizeof (struct usb_hub_descriptor);
607 break; 614 break;
615 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
616 /* len is returned by hub_control */
617 break;
608 } 618 }
609 status = hcd->driver->hub_control (hcd, 619 status = hcd->driver->hub_control (hcd,
610 typeReq, wValue, wIndex, 620 typeReq, wValue, wIndex,
@@ -615,7 +625,7 @@ error:
615 status = -EPIPE; 625 status = -EPIPE;
616 } 626 }
617 627
618 if (status) { 628 if (status < 0) {
619 len = 0; 629 len = 0;
620 if (status != -EPIPE) { 630 if (status != -EPIPE) {
621 dev_dbg (hcd->self.controller, 631 dev_dbg (hcd->self.controller,
@@ -624,6 +634,10 @@ error:
624 typeReq, wValue, wIndex, 634 typeReq, wValue, wIndex,
625 wLength, status); 635 wLength, status);
626 } 636 }
637 } else if (status > 0) {
638 /* hub_control may return the length of data copied. */
639 len = status;
640 status = 0;
627 } 641 }
628 if (len) { 642 if (len) {
629 if (urb->transfer_buffer_length < len) 643 if (urb->transfer_buffer_length < len)
@@ -1961,8 +1975,9 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
1961 int status; 1975 int status;
1962 int old_state = hcd->state; 1976 int old_state = hcd->state;
1963 1977
1964 dev_dbg(&rhdev->dev, "bus %s%s\n", 1978 dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
1965 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend"); 1979 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
1980 rhdev->do_remote_wakeup);
1966 if (HCD_DEAD(hcd)) { 1981 if (HCD_DEAD(hcd)) {
1967 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend"); 1982 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
1968 return 0; 1983 return 0;
@@ -1997,8 +2012,8 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1997 int status; 2012 int status;
1998 int old_state = hcd->state; 2013 int old_state = hcd->state;
1999 2014
2000 dev_dbg(&rhdev->dev, "usb %s%s\n", 2015 dev_dbg(&rhdev->dev, "usb %sresume\n",
2001 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume"); 2016 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
2002 if (HCD_DEAD(hcd)) { 2017 if (HCD_DEAD(hcd)) {
2003 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume"); 2018 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
2004 return 0; 2019 return 0;
@@ -2429,7 +2444,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
2429 * but drivers can override it in reset() if needed, along with 2444 * but drivers can override it in reset() if needed, along with
2430 * recording the overall controller's system wakeup capability. 2445 * recording the overall controller's system wakeup capability.
2431 */ 2446 */
2432 device_init_wakeup(&rhdev->dev, 1); 2447 device_set_wakeup_capable(&rhdev->dev, 1);
2433 2448
2434 /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is 2449 /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
2435 * registered. But since the controller can die at any time, 2450 * registered. But since the controller can die at any time,
@@ -2478,6 +2493,13 @@ int usb_add_hcd(struct usb_hcd *hcd,
2478 } 2493 }
2479 if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) 2494 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2480 usb_hcd_poll_rh_status(hcd); 2495 usb_hcd_poll_rh_status(hcd);
2496
2497 /*
2498 * Host controllers don't generate their own wakeup requests;
2499 * they only forward requests from the root hub. Therefore
2500 * controllers should always be enabled for remote wakeup.
2501 */
2502 device_wakeup_enable(hcd->self.controller);
2481 return retval; 2503 return retval;
2482 2504
2483error_create_attr_group: 2505error_create_attr_group:
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index a428aa080a36..79781461eec9 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -813,6 +813,12 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
813 USB_PORT_FEAT_C_PORT_LINK_STATE); 813 USB_PORT_FEAT_C_PORT_LINK_STATE);
814 } 814 }
815 815
816 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
817 hub_is_superspeed(hub->hdev)) {
818 need_debounce_delay = true;
819 clear_port_feature(hub->hdev, port1,
820 USB_PORT_FEAT_C_BH_PORT_RESET);
821 }
816 /* We can forget about a "removed" device when there's a 822 /* We can forget about a "removed" device when there's a
817 * physical disconnect or the connect status changes. 823 * physical disconnect or the connect status changes.
818 */ 824 */
@@ -1636,11 +1642,6 @@ void usb_disconnect(struct usb_device **pdev)
1636 int i; 1642 int i;
1637 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 1643 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1638 1644
1639 if (!udev) {
1640 pr_debug ("%s nodev\n", __func__);
1641 return;
1642 }
1643
1644 /* mark the device as inactive, so any further urb submissions for 1645 /* mark the device as inactive, so any further urb submissions for
1645 * this device (and any of its children) will fail immediately. 1646 * this device (and any of its children) will fail immediately.
1646 * this quiesces everything except pending urbs. 1647 * this quiesces everything except pending urbs.
@@ -2030,11 +2031,23 @@ static unsigned hub_is_wusb(struct usb_hub *hub)
2030 2031
2031#define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 2032#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2032#define HUB_SHORT_RESET_TIME 10 2033#define HUB_SHORT_RESET_TIME 10
2034#define HUB_BH_RESET_TIME 50
2033#define HUB_LONG_RESET_TIME 200 2035#define HUB_LONG_RESET_TIME 200
2034#define HUB_RESET_TIMEOUT 500 2036#define HUB_RESET_TIMEOUT 500
2035 2037
2038static int hub_port_reset(struct usb_hub *hub, int port1,
2039 struct usb_device *udev, unsigned int delay, bool warm);
2040
2041/* Is a USB 3.0 port in the Inactive state? */
2042static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
2043{
2044 return hub_is_superspeed(hub->hdev) &&
2045 (portstatus & USB_PORT_STAT_LINK_STATE) ==
2046 USB_SS_PORT_LS_SS_INACTIVE;
2047}
2048
2036static int hub_port_wait_reset(struct usb_hub *hub, int port1, 2049static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2037 struct usb_device *udev, unsigned int delay) 2050 struct usb_device *udev, unsigned int delay, bool warm)
2038{ 2051{
2039 int delay_time, ret; 2052 int delay_time, ret;
2040 u16 portstatus; 2053 u16 portstatus;
@@ -2051,28 +2064,71 @@ static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2051 if (ret < 0) 2064 if (ret < 0)
2052 return ret; 2065 return ret;
2053 2066
2054 /* Device went away? */ 2067 /*
2055 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 2068 * Some buggy devices require a warm reset to be issued even
2056 return -ENOTCONN; 2069 * when the port appears not to be connected.
2057 2070 */
2058 /* bomb out completely if the connection bounced */ 2071 if (!warm) {
2059 if ((portchange & USB_PORT_STAT_C_CONNECTION)) 2072 /*
2060 return -ENOTCONN; 2073 * Some buggy devices can cause an NEC host controller
2061 2074 * to transition to the "Error" state after a hot port
2062 /* if we`ve finished resetting, then break out of the loop */ 2075 * reset. This will show up as the port state in
2063 if (!(portstatus & USB_PORT_STAT_RESET) && 2076 * "Inactive", and the port may also report a
2064 (portstatus & USB_PORT_STAT_ENABLE)) { 2077 * disconnect. Forcing a warm port reset seems to make
2065 if (hub_is_wusb(hub)) 2078 * the device work.
2066 udev->speed = USB_SPEED_WIRELESS; 2079 *
2067 else if (hub_is_superspeed(hub->hdev)) 2080 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2068 udev->speed = USB_SPEED_SUPER; 2081 */
2069 else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 2082 if (hub_port_inactive(hub, portstatus)) {
2070 udev->speed = USB_SPEED_HIGH; 2083 int ret;
2071 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 2084
2072 udev->speed = USB_SPEED_LOW; 2085 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2073 else 2086 clear_port_feature(hub->hdev, port1,
2074 udev->speed = USB_SPEED_FULL; 2087 USB_PORT_FEAT_C_CONNECTION);
2075 return 0; 2088 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2089 clear_port_feature(hub->hdev, port1,
2090 USB_PORT_FEAT_C_PORT_LINK_STATE);
2091 if (portchange & USB_PORT_STAT_C_RESET)
2092 clear_port_feature(hub->hdev, port1,
2093 USB_PORT_FEAT_C_RESET);
2094 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2095 port1);
2096 ret = hub_port_reset(hub, port1,
2097 udev, HUB_BH_RESET_TIME,
2098 true);
2099 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2100 clear_port_feature(hub->hdev, port1,
2101 USB_PORT_FEAT_C_CONNECTION);
2102 return ret;
2103 }
2104 /* Device went away? */
2105 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2106 return -ENOTCONN;
2107
2108 /* bomb out completely if the connection bounced */
2109 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2110 return -ENOTCONN;
2111
2112 /* if we`ve finished resetting, then break out of
2113 * the loop
2114 */
2115 if (!(portstatus & USB_PORT_STAT_RESET) &&
2116 (portstatus & USB_PORT_STAT_ENABLE)) {
2117 if (hub_is_wusb(hub))
2118 udev->speed = USB_SPEED_WIRELESS;
2119 else if (hub_is_superspeed(hub->hdev))
2120 udev->speed = USB_SPEED_SUPER;
2121 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2122 udev->speed = USB_SPEED_HIGH;
2123 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2124 udev->speed = USB_SPEED_LOW;
2125 else
2126 udev->speed = USB_SPEED_FULL;
2127 return 0;
2128 }
2129 } else {
2130 if (portchange & USB_PORT_STAT_C_BH_RESET)
2131 return 0;
2076 } 2132 }
2077 2133
2078 /* switch to the long delay after two short delay failures */ 2134 /* switch to the long delay after two short delay failures */
@@ -2080,35 +2136,84 @@ static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2080 delay = HUB_LONG_RESET_TIME; 2136 delay = HUB_LONG_RESET_TIME;
2081 2137
2082 dev_dbg (hub->intfdev, 2138 dev_dbg (hub->intfdev,
2083 "port %d not reset yet, waiting %dms\n", 2139 "port %d not %sreset yet, waiting %dms\n",
2084 port1, delay); 2140 port1, warm ? "warm " : "", delay);
2085 } 2141 }
2086 2142
2087 return -EBUSY; 2143 return -EBUSY;
2088} 2144}
2089 2145
2146static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2147 struct usb_device *udev, int *status, bool warm)
2148{
2149 switch (*status) {
2150 case 0:
2151 if (!warm) {
2152 struct usb_hcd *hcd;
2153 /* TRSTRCY = 10 ms; plus some extra */
2154 msleep(10 + 40);
2155 update_devnum(udev, 0);
2156 hcd = bus_to_hcd(udev->bus);
2157 if (hcd->driver->reset_device) {
2158 *status = hcd->driver->reset_device(hcd, udev);
2159 if (*status < 0) {
2160 dev_err(&udev->dev, "Cannot reset "
2161 "HCD device state\n");
2162 break;
2163 }
2164 }
2165 }
2166 /* FALL THROUGH */
2167 case -ENOTCONN:
2168 case -ENODEV:
2169 clear_port_feature(hub->hdev,
2170 port1, USB_PORT_FEAT_C_RESET);
2171 /* FIXME need disconnect() for NOTATTACHED device */
2172 if (warm) {
2173 clear_port_feature(hub->hdev, port1,
2174 USB_PORT_FEAT_C_BH_PORT_RESET);
2175 clear_port_feature(hub->hdev, port1,
2176 USB_PORT_FEAT_C_PORT_LINK_STATE);
2177 } else {
2178 usb_set_device_state(udev, *status
2179 ? USB_STATE_NOTATTACHED
2180 : USB_STATE_DEFAULT);
2181 }
2182 break;
2183 }
2184}
2185
2186/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2090static int hub_port_reset(struct usb_hub *hub, int port1, 2187static int hub_port_reset(struct usb_hub *hub, int port1,
2091 struct usb_device *udev, unsigned int delay) 2188 struct usb_device *udev, unsigned int delay, bool warm)
2092{ 2189{
2093 int i, status; 2190 int i, status;
2094 struct usb_hcd *hcd;
2095 2191
2096 hcd = bus_to_hcd(udev->bus); 2192 if (!warm) {
2097 /* Block EHCI CF initialization during the port reset. 2193 /* Block EHCI CF initialization during the port reset.
2098 * Some companion controllers don't like it when they mix. 2194 * Some companion controllers don't like it when they mix.
2099 */ 2195 */
2100 down_read(&ehci_cf_port_reset_rwsem); 2196 down_read(&ehci_cf_port_reset_rwsem);
2197 } else {
2198 if (!hub_is_superspeed(hub->hdev)) {
2199 dev_err(hub->intfdev, "only USB3 hub support "
2200 "warm reset\n");
2201 return -EINVAL;
2202 }
2203 }
2101 2204
2102 /* Reset the port */ 2205 /* Reset the port */
2103 for (i = 0; i < PORT_RESET_TRIES; i++) { 2206 for (i = 0; i < PORT_RESET_TRIES; i++) {
2104 status = set_port_feature(hub->hdev, 2207 status = set_port_feature(hub->hdev, port1, (warm ?
2105 port1, USB_PORT_FEAT_RESET); 2208 USB_PORT_FEAT_BH_PORT_RESET :
2106 if (status) 2209 USB_PORT_FEAT_RESET));
2210 if (status) {
2107 dev_err(hub->intfdev, 2211 dev_err(hub->intfdev,
2108 "cannot reset port %d (err = %d)\n", 2212 "cannot %sreset port %d (err = %d)\n",
2109 port1, status); 2213 warm ? "warm " : "", port1, status);
2110 else { 2214 } else {
2111 status = hub_port_wait_reset(hub, port1, udev, delay); 2215 status = hub_port_wait_reset(hub, port1, udev, delay,
2216 warm);
2112 if (status && status != -ENOTCONN) 2217 if (status && status != -ENOTCONN)
2113 dev_dbg(hub->intfdev, 2218 dev_dbg(hub->intfdev,
2114 "port_wait_reset: err = %d\n", 2219 "port_wait_reset: err = %d\n",
@@ -2116,34 +2221,14 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2116 } 2221 }
2117 2222
2118 /* return on disconnect or reset */ 2223 /* return on disconnect or reset */
2119 switch (status) { 2224 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2120 case 0: 2225 hub_port_finish_reset(hub, port1, udev, &status, warm);
2121 /* TRSTRCY = 10 ms; plus some extra */
2122 msleep(10 + 40);
2123 update_devnum(udev, 0);
2124 if (hcd->driver->reset_device) {
2125 status = hcd->driver->reset_device(hcd, udev);
2126 if (status < 0) {
2127 dev_err(&udev->dev, "Cannot reset "
2128 "HCD device state\n");
2129 break;
2130 }
2131 }
2132 /* FALL THROUGH */
2133 case -ENOTCONN:
2134 case -ENODEV:
2135 clear_port_feature(hub->hdev,
2136 port1, USB_PORT_FEAT_C_RESET);
2137 /* FIXME need disconnect() for NOTATTACHED device */
2138 usb_set_device_state(udev, status
2139 ? USB_STATE_NOTATTACHED
2140 : USB_STATE_DEFAULT);
2141 goto done; 2226 goto done;
2142 } 2227 }
2143 2228
2144 dev_dbg (hub->intfdev, 2229 dev_dbg (hub->intfdev,
2145 "port %d not enabled, trying reset again...\n", 2230 "port %d not enabled, trying %sreset again...\n",
2146 port1); 2231 port1, warm ? "warm " : "");
2147 delay = HUB_LONG_RESET_TIME; 2232 delay = HUB_LONG_RESET_TIME;
2148 } 2233 }
2149 2234
@@ -2151,45 +2236,11 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2151 "Cannot enable port %i. Maybe the USB cable is bad?\n", 2236 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2152 port1); 2237 port1);
2153 2238
2154 done: 2239done:
2155 up_read(&ehci_cf_port_reset_rwsem); 2240 if (!warm)
2156 return status; 2241 up_read(&ehci_cf_port_reset_rwsem);
2157}
2158
2159/* Warm reset a USB3 protocol port */
2160static int hub_port_warm_reset(struct usb_hub *hub, int port)
2161{
2162 int ret;
2163 u16 portstatus, portchange;
2164
2165 if (!hub_is_superspeed(hub->hdev)) {
2166 dev_err(hub->intfdev, "only USB3 hub support warm reset\n");
2167 return -EINVAL;
2168 }
2169
2170 /* Warm reset the port */
2171 ret = set_port_feature(hub->hdev,
2172 port, USB_PORT_FEAT_BH_PORT_RESET);
2173 if (ret) {
2174 dev_err(hub->intfdev, "cannot warm reset port %d\n", port);
2175 return ret;
2176 }
2177
2178 msleep(20);
2179 ret = hub_port_status(hub, port, &portstatus, &portchange);
2180
2181 if (portchange & USB_PORT_STAT_C_RESET)
2182 clear_port_feature(hub->hdev, port, USB_PORT_FEAT_C_RESET);
2183
2184 if (portchange & USB_PORT_STAT_C_BH_RESET)
2185 clear_port_feature(hub->hdev, port,
2186 USB_PORT_FEAT_C_BH_PORT_RESET);
2187
2188 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2189 clear_port_feature(hub->hdev, port,
2190 USB_PORT_FEAT_C_PORT_LINK_STATE);
2191 2242
2192 return ret; 2243 return status;
2193} 2244}
2194 2245
2195/* Check if a port is power on */ 2246/* Check if a port is power on */
@@ -2324,8 +2375,6 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2324 int port1 = udev->portnum; 2375 int port1 = udev->portnum;
2325 int status; 2376 int status;
2326 2377
2327 // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2328
2329 /* enable remote wakeup when appropriate; this lets the device 2378 /* enable remote wakeup when appropriate; this lets the device
2330 * wake up the upstream hub (including maybe the root hub). 2379 * wake up the upstream hub (including maybe the root hub).
2331 * 2380 *
@@ -2342,11 +2391,15 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2342 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 2391 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2343 status); 2392 status);
2344 /* bail if autosuspend is requested */ 2393 /* bail if autosuspend is requested */
2345 if (msg.event & PM_EVENT_AUTO) 2394 if (PMSG_IS_AUTO(msg))
2346 return status; 2395 return status;
2347 } 2396 }
2348 } 2397 }
2349 2398
2399 /* disable USB2 hardware LPM */
2400 if (udev->usb2_hw_lpm_enabled == 1)
2401 usb_set_usb2_hardware_lpm(udev, 0);
2402
2350 /* see 7.1.7.6 */ 2403 /* see 7.1.7.6 */
2351 if (hub_is_superspeed(hub->hdev)) 2404 if (hub_is_superspeed(hub->hdev))
2352 status = set_port_feature(hub->hdev, 2405 status = set_port_feature(hub->hdev,
@@ -2367,12 +2420,13 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2367 USB_CTRL_SET_TIMEOUT); 2420 USB_CTRL_SET_TIMEOUT);
2368 2421
2369 /* System sleep transitions should never fail */ 2422 /* System sleep transitions should never fail */
2370 if (!(msg.event & PM_EVENT_AUTO)) 2423 if (!PMSG_IS_AUTO(msg))
2371 status = 0; 2424 status = 0;
2372 } else { 2425 } else {
2373 /* device has up to 10 msec to fully suspend */ 2426 /* device has up to 10 msec to fully suspend */
2374 dev_dbg(&udev->dev, "usb %ssuspend\n", 2427 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2375 (msg.event & PM_EVENT_AUTO ? "auto-" : "")); 2428 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2429 udev->do_remote_wakeup);
2376 usb_set_device_state(udev, USB_STATE_SUSPENDED); 2430 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2377 msleep(10); 2431 msleep(10);
2378 } 2432 }
@@ -2523,7 +2577,7 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2523 } else { 2577 } else {
2524 /* drive resume for at least 20 msec */ 2578 /* drive resume for at least 20 msec */
2525 dev_dbg(&udev->dev, "usb %sresume\n", 2579 dev_dbg(&udev->dev, "usb %sresume\n",
2526 (msg.event & PM_EVENT_AUTO ? "auto-" : "")); 2580 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
2527 msleep(25); 2581 msleep(25);
2528 2582
2529 /* Virtual root hubs can trigger on GET_PORT_STATUS to 2583 /* Virtual root hubs can trigger on GET_PORT_STATUS to
@@ -2558,7 +2612,12 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2558 if (status < 0) { 2612 if (status < 0) {
2559 dev_dbg(&udev->dev, "can't resume, status %d\n", status); 2613 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2560 hub_port_logical_disconnect(hub, port1); 2614 hub_port_logical_disconnect(hub, port1);
2615 } else {
2616 /* Try to enable USB2 hardware LPM */
2617 if (udev->usb2_hw_lpm_capable == 1)
2618 usb_set_usb2_hardware_lpm(udev, 1);
2561 } 2619 }
2620
2562 return status; 2621 return status;
2563} 2622}
2564 2623
@@ -2625,7 +2684,7 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
2625 udev = hdev->children [port1-1]; 2684 udev = hdev->children [port1-1];
2626 if (udev && udev->can_submit) { 2685 if (udev && udev->can_submit) {
2627 dev_warn(&intf->dev, "port %d nyet suspended\n", port1); 2686 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
2628 if (msg.event & PM_EVENT_AUTO) 2687 if (PMSG_IS_AUTO(msg))
2629 return -EBUSY; 2688 return -EBUSY;
2630 } 2689 }
2631 } 2690 }
@@ -2798,7 +2857,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2798 int i, j, retval; 2857 int i, j, retval;
2799 unsigned delay = HUB_SHORT_RESET_TIME; 2858 unsigned delay = HUB_SHORT_RESET_TIME;
2800 enum usb_device_speed oldspeed = udev->speed; 2859 enum usb_device_speed oldspeed = udev->speed;
2801 char *speed, *type; 2860 const char *speed;
2802 int devnum = udev->devnum; 2861 int devnum = udev->devnum;
2803 2862
2804 /* root hub ports have a slightly longer reset period 2863 /* root hub ports have a slightly longer reset period
@@ -2819,7 +2878,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2819 2878
2820 /* Reset the device; full speed may morph to high speed */ 2879 /* Reset the device; full speed may morph to high speed */
2821 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 2880 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
2822 retval = hub_port_reset(hub, port1, udev, delay); 2881 retval = hub_port_reset(hub, port1, udev, delay, false);
2823 if (retval < 0) /* error or disconnect */ 2882 if (retval < 0) /* error or disconnect */
2824 goto fail; 2883 goto fail;
2825 /* success, speed is known */ 2884 /* success, speed is known */
@@ -2858,25 +2917,16 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2858 default: 2917 default:
2859 goto fail; 2918 goto fail;
2860 } 2919 }
2861 2920
2862 type = ""; 2921 if (udev->speed == USB_SPEED_WIRELESS)
2863 switch (udev->speed) { 2922 speed = "variable speed Wireless";
2864 case USB_SPEED_LOW: speed = "low"; break; 2923 else
2865 case USB_SPEED_FULL: speed = "full"; break; 2924 speed = usb_speed_string(udev->speed);
2866 case USB_SPEED_HIGH: speed = "high"; break; 2925
2867 case USB_SPEED_SUPER:
2868 speed = "super";
2869 break;
2870 case USB_SPEED_WIRELESS:
2871 speed = "variable";
2872 type = "Wireless ";
2873 break;
2874 default: speed = "?"; break;
2875 }
2876 if (udev->speed != USB_SPEED_SUPER) 2926 if (udev->speed != USB_SPEED_SUPER)
2877 dev_info(&udev->dev, 2927 dev_info(&udev->dev,
2878 "%s %s speed %sUSB device number %d using %s\n", 2928 "%s %s USB device number %d using %s\n",
2879 (udev->config) ? "reset" : "new", speed, type, 2929 (udev->config) ? "reset" : "new", speed,
2880 devnum, udev->bus->controller->driver->name); 2930 devnum, udev->bus->controller->driver->name);
2881 2931
2882 /* Set up TT records, if needed */ 2932 /* Set up TT records, if needed */
@@ -2949,7 +2999,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2949 buf->bMaxPacketSize0; 2999 buf->bMaxPacketSize0;
2950 kfree(buf); 3000 kfree(buf);
2951 3001
2952 retval = hub_port_reset(hub, port1, udev, delay); 3002 retval = hub_port_reset(hub, port1, udev, delay, false);
2953 if (retval < 0) /* error or disconnect */ 3003 if (retval < 0) /* error or disconnect */
2954 goto fail; 3004 goto fail;
2955 if (oldspeed != udev->speed) { 3005 if (oldspeed != udev->speed) {
@@ -3023,7 +3073,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3023 i = 512; 3073 i = 512;
3024 else 3074 else
3025 i = udev->descriptor.bMaxPacketSize0; 3075 i = udev->descriptor.bMaxPacketSize0;
3026 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) { 3076 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
3027 if (udev->speed == USB_SPEED_LOW || 3077 if (udev->speed == USB_SPEED_LOW ||
3028 !(i == 8 || i == 16 || i == 32 || i == 64)) { 3078 !(i == 8 || i == 16 || i == 32 || i == 64)) {
3029 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); 3079 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
@@ -3047,6 +3097,15 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3047 goto fail; 3097 goto fail;
3048 } 3098 }
3049 3099
3100 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3101 retval = usb_get_bos_descriptor(udev);
3102 if (!retval) {
3103 if (udev->bos->ext_cap && (USB_LPM_SUPPORT &
3104 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
3105 udev->lpm_capable = 1;
3106 }
3107 }
3108
3050 retval = 0; 3109 retval = 0;
3051 /* notify HCD that we have a device connected and addressed */ 3110 /* notify HCD that we have a device connected and addressed */
3052 if (hcd->driver->update_device) 3111 if (hcd->driver->update_device)
@@ -3570,7 +3629,8 @@ static void hub_events(void)
3570 (portstatus & USB_PORT_STAT_LINK_STATE) 3629 (portstatus & USB_PORT_STAT_LINK_STATE)
3571 == USB_SS_PORT_LS_SS_INACTIVE) { 3630 == USB_SS_PORT_LS_SS_INACTIVE) {
3572 dev_dbg(hub_dev, "warm reset port %d\n", i); 3631 dev_dbg(hub_dev, "warm reset port %d\n", i);
3573 hub_port_warm_reset(hub, i); 3632 hub_port_reset(hub, i, NULL,
3633 HUB_BH_RESET_TIME, true);
3574 } 3634 }
3575 3635
3576 if (connect_change) 3636 if (connect_change)
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 0b5ec234c787..b3bdfede45e6 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -435,7 +435,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
435 435
436 len = sg->length; 436 len = sg->length;
437 if (length) { 437 if (length) {
438 len = min_t(unsigned, len, length); 438 len = min_t(size_t, len, length);
439 length -= len; 439 length -= len;
440 if (length == 0) 440 if (length == 0)
441 io->entries = i + 1; 441 io->entries = i + 1;
diff --git a/drivers/usb/core/notify.c b/drivers/usb/core/notify.c
index 7542dce3f5a1..7728c91dfa2e 100644
--- a/drivers/usb/core/notify.c
+++ b/drivers/usb/core/notify.c
@@ -10,6 +10,7 @@
10 10
11 11
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/export.h>
13#include <linux/notifier.h> 14#include <linux/notifier.h>
14#include <linux/usb.h> 15#include <linux/usb.h>
15#include <linux/mutex.h> 16#include <linux/mutex.h>
diff --git a/drivers/usb/core/quirks.c b/drivers/usb/core/quirks.c
index 81ce6a8e1d94..ecf12e15a7ef 100644
--- a/drivers/usb/core/quirks.c
+++ b/drivers/usb/core/quirks.c
@@ -38,6 +38,54 @@ static const struct usb_device_id usb_quirk_list[] = {
38 /* Creative SB Audigy 2 NX */ 38 /* Creative SB Audigy 2 NX */
39 { USB_DEVICE(0x041e, 0x3020), .driver_info = USB_QUIRK_RESET_RESUME }, 39 { USB_DEVICE(0x041e, 0x3020), .driver_info = USB_QUIRK_RESET_RESUME },
40 40
41 /* Logitech Webcam C200 */
42 { USB_DEVICE(0x046d, 0x0802), .driver_info = USB_QUIRK_RESET_RESUME },
43
44 /* Logitech Webcam C250 */
45 { USB_DEVICE(0x046d, 0x0804), .driver_info = USB_QUIRK_RESET_RESUME },
46
47 /* Logitech Webcam C300 */
48 { USB_DEVICE(0x046d, 0x0805), .driver_info = USB_QUIRK_RESET_RESUME },
49
50 /* Logitech Webcam B/C500 */
51 { USB_DEVICE(0x046d, 0x0807), .driver_info = USB_QUIRK_RESET_RESUME },
52
53 /* Logitech Webcam C600 */
54 { USB_DEVICE(0x046d, 0x0808), .driver_info = USB_QUIRK_RESET_RESUME },
55
56 /* Logitech Webcam Pro 9000 */
57 { USB_DEVICE(0x046d, 0x0809), .driver_info = USB_QUIRK_RESET_RESUME },
58
59 /* Logitech Webcam C905 */
60 { USB_DEVICE(0x046d, 0x080a), .driver_info = USB_QUIRK_RESET_RESUME },
61
62 /* Logitech Webcam C210 */
63 { USB_DEVICE(0x046d, 0x0819), .driver_info = USB_QUIRK_RESET_RESUME },
64
65 /* Logitech Webcam C260 */
66 { USB_DEVICE(0x046d, 0x081a), .driver_info = USB_QUIRK_RESET_RESUME },
67
68 /* Logitech Webcam C310 */
69 { USB_DEVICE(0x046d, 0x081b), .driver_info = USB_QUIRK_RESET_RESUME },
70
71 /* Logitech Webcam C910 */
72 { USB_DEVICE(0x046d, 0x0821), .driver_info = USB_QUIRK_RESET_RESUME },
73
74 /* Logitech Webcam C160 */
75 { USB_DEVICE(0x046d, 0x0824), .driver_info = USB_QUIRK_RESET_RESUME },
76
77 /* Logitech Webcam C270 */
78 { USB_DEVICE(0x046d, 0x0825), .driver_info = USB_QUIRK_RESET_RESUME },
79
80 /* Logitech Quickcam Pro 9000 */
81 { USB_DEVICE(0x046d, 0x0990), .driver_info = USB_QUIRK_RESET_RESUME },
82
83 /* Logitech Quickcam E3500 */
84 { USB_DEVICE(0x046d, 0x09a4), .driver_info = USB_QUIRK_RESET_RESUME },
85
86 /* Logitech Quickcam Vision Pro */
87 { USB_DEVICE(0x046d, 0x09a6), .driver_info = USB_QUIRK_RESET_RESUME },
88
41 /* Logitech Harmony 700-series */ 89 /* Logitech Harmony 700-series */
42 { USB_DEVICE(0x046d, 0xc122), .driver_info = USB_QUIRK_DELAY_INIT }, 90 { USB_DEVICE(0x046d, 0xc122), .driver_info = USB_QUIRK_DELAY_INIT },
43 91
@@ -69,6 +117,9 @@ static const struct usb_device_id usb_quirk_list[] = {
69 { USB_DEVICE(0x06a3, 0x0006), .driver_info = 117 { USB_DEVICE(0x06a3, 0x0006), .driver_info =
70 USB_QUIRK_CONFIG_INTF_STRINGS }, 118 USB_QUIRK_CONFIG_INTF_STRINGS },
71 119
120 /* Guillemot Webcam Hercules Dualpix Exchange*/
121 { USB_DEVICE(0x06f8, 0x0804), .driver_info = USB_QUIRK_RESET_RESUME },
122
72 /* M-Systems Flash Disk Pioneers */ 123 /* M-Systems Flash Disk Pioneers */
73 { USB_DEVICE(0x08ec, 0x1000), .driver_info = USB_QUIRK_RESET_RESUME }, 124 { USB_DEVICE(0x08ec, 0x1000), .driver_info = USB_QUIRK_RESET_RESUME },
74 125
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index cf05b97693ea..662c0cf3a3e1 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -412,6 +412,56 @@ set_level(struct device *dev, struct device_attribute *attr,
412 412
413static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level); 413static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
414 414
415static ssize_t
416show_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
417 char *buf)
418{
419 struct usb_device *udev = to_usb_device(dev);
420 const char *p;
421
422 if (udev->usb2_hw_lpm_enabled == 1)
423 p = "enabled";
424 else
425 p = "disabled";
426
427 return sprintf(buf, "%s\n", p);
428}
429
430static ssize_t
431set_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
433{
434 struct usb_device *udev = to_usb_device(dev);
435 bool value;
436 int ret;
437
438 usb_lock_device(udev);
439
440 ret = strtobool(buf, &value);
441
442 if (!ret)
443 ret = usb_set_usb2_hardware_lpm(udev, value);
444
445 usb_unlock_device(udev);
446
447 if (!ret)
448 return count;
449
450 return ret;
451}
452
453static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,
454 set_usb2_hardware_lpm);
455
456static struct attribute *usb2_hardware_lpm_attr[] = {
457 &dev_attr_usb2_hardware_lpm.attr,
458 NULL,
459};
460static struct attribute_group usb2_hardware_lpm_attr_group = {
461 .name = power_group_name,
462 .attrs = usb2_hardware_lpm_attr,
463};
464
415static struct attribute *power_attrs[] = { 465static struct attribute *power_attrs[] = {
416 &dev_attr_autosuspend.attr, 466 &dev_attr_autosuspend.attr,
417 &dev_attr_level.attr, 467 &dev_attr_level.attr,
@@ -428,13 +478,20 @@ static int add_power_attributes(struct device *dev)
428{ 478{
429 int rc = 0; 479 int rc = 0;
430 480
431 if (is_usb_device(dev)) 481 if (is_usb_device(dev)) {
482 struct usb_device *udev = to_usb_device(dev);
432 rc = sysfs_merge_group(&dev->kobj, &power_attr_group); 483 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
484 if (udev->usb2_hw_lpm_capable == 1)
485 rc = sysfs_merge_group(&dev->kobj,
486 &usb2_hardware_lpm_attr_group);
487 }
488
433 return rc; 489 return rc;
434} 490}
435 491
436static void remove_power_attributes(struct device *dev) 492static void remove_power_attributes(struct device *dev)
437{ 493{
494 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
438 sysfs_unmerge_group(&dev->kobj, &power_attr_group); 495 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
439} 496}
440 497
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index ae334b067c13..909625b91eb3 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -350,7 +350,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
350 dev->state < USB_STATE_CONFIGURED) 350 dev->state < USB_STATE_CONFIGURED)
351 return -ENODEV; 351 return -ENODEV;
352 352
353 max = le16_to_cpu(ep->desc.wMaxPacketSize); 353 max = usb_endpoint_maxp(&ep->desc);
354 if (max <= 0) { 354 if (max <= 0) {
355 dev_dbg(&dev->dev, 355 dev_dbg(&dev->dev,
356 "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n", 356 "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n",
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 8706fc97e60f..73cd90012ec5 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -225,6 +225,7 @@ static void usb_release_dev(struct device *dev)
225 hcd = bus_to_hcd(udev->bus); 225 hcd = bus_to_hcd(udev->bus);
226 226
227 usb_destroy_configuration(udev); 227 usb_destroy_configuration(udev);
228 usb_release_bos_descriptor(udev);
228 usb_put_hcd(hcd); 229 usb_put_hcd(hcd);
229 kfree(udev->product); 230 kfree(udev->product);
230 kfree(udev->manufacturer); 231 kfree(udev->manufacturer);
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index d44d4b7bbf17..3888778582c4 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -28,6 +28,8 @@ extern int usb_remove_device(struct usb_device *udev);
28 28
29extern int usb_get_device_descriptor(struct usb_device *dev, 29extern int usb_get_device_descriptor(struct usb_device *dev,
30 unsigned int size); 30 unsigned int size);
31extern int usb_get_bos_descriptor(struct usb_device *dev);
32extern void usb_release_bos_descriptor(struct usb_device *dev);
31extern char *usb_cache_string(struct usb_device *udev, int index); 33extern char *usb_cache_string(struct usb_device *udev, int index);
32extern int usb_set_configuration(struct usb_device *dev, int configuration); 34extern int usb_set_configuration(struct usb_device *dev, int configuration);
33extern int usb_choose_configuration(struct usb_device *udev); 35extern int usb_choose_configuration(struct usb_device *udev);
@@ -80,6 +82,7 @@ extern int usb_remote_wakeup(struct usb_device *dev);
80extern int usb_runtime_suspend(struct device *dev); 82extern int usb_runtime_suspend(struct device *dev);
81extern int usb_runtime_resume(struct device *dev); 83extern int usb_runtime_resume(struct device *dev);
82extern int usb_runtime_idle(struct device *dev); 84extern int usb_runtime_idle(struct device *dev);
85extern int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable);
83 86
84#else 87#else
85 88
@@ -94,6 +97,10 @@ static inline int usb_remote_wakeup(struct usb_device *udev)
94 return 0; 97 return 0;
95} 98}
96 99
100static inline int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
101{
102 return 0;
103}
97#endif 104#endif
98 105
99extern struct bus_type usb_bus_type; 106extern struct bus_type usb_bus_type;
diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig
new file mode 100644
index 000000000000..3c1d67d324fd
--- /dev/null
+++ b/drivers/usb/dwc3/Kconfig
@@ -0,0 +1,25 @@
1config USB_DWC3
2 tristate "DesignWare USB3 DRD Core Support"
3 depends on (USB || USB_GADGET)
4 select USB_OTG_UTILS
5 help
6 Say Y or M here if your system has a Dual Role SuperSpeed
7 USB controller based on the DesignWare USB3 IP Core.
8
9 If you choose to build this driver is a dynamically linked
10 module, the module will be called dwc3.ko.
11
12if USB_DWC3
13
14config USB_DWC3_DEBUG
15 bool "Enable Debugging Messages"
16 help
17 Say Y here to enable debugging messages on DWC3 Driver.
18
19config USB_DWC3_VERBOSE
20 bool "Enable Verbose Debugging Messages"
21 depends on USB_DWC3_DEBUG
22 help
23 Say Y here to enable verbose debugging messages on DWC3 Driver.
24
25endif
diff --git a/drivers/usb/dwc3/Makefile b/drivers/usb/dwc3/Makefile
new file mode 100644
index 000000000000..593d1dbc465b
--- /dev/null
+++ b/drivers/usb/dwc3/Makefile
@@ -0,0 +1,36 @@
1ccflags-$(CONFIG_USB_DWC3_DEBUG) := -DDEBUG
2ccflags-$(CONFIG_USB_DWC3_VERBOSE) += -DVERBOSE_DEBUG
3
4obj-$(CONFIG_USB_DWC3) += dwc3.o
5
6dwc3-y := core.o
7
8ifneq ($(CONFIG_USB_GADGET_DWC3),)
9 dwc3-y += gadget.o ep0.o
10endif
11
12ifneq ($(CONFIG_DEBUG_FS),)
13 dwc3-y += debugfs.o
14endif
15
16##
17# Platform-specific glue layers go here
18#
19# NOTICE: Make sure your glue layer doesn't depend on anything
20# which is arch-specific and that it compiles on all situations.
21#
22# We want to keep this requirement in order to be able to compile
23# the entire driver (with all its glue layers) on several architectures
24# and make sure it compiles fine. This will also help with allmodconfig
25# and allyesconfig builds.
26#
27# The only exception is the PCI glue layer, but that's only because
28# PCI doesn't provide nops if CONFIG_PCI isn't enabled.
29##
30
31obj-$(CONFIG_USB_DWC3) += dwc3-omap.o
32
33ifneq ($(CONFIG_PCI),)
34 obj-$(CONFIG_USB_DWC3) += dwc3-pci.o
35endif
36
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
new file mode 100644
index 000000000000..717ebc9ff941
--- /dev/null
+++ b/drivers/usb/dwc3/core.c
@@ -0,0 +1,484 @@
1/**
2 * core.c - DesignWare USB3 DRD Controller Core file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/ioport.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/delay.h>
50#include <linux/dma-mapping.h>
51
52#include <linux/usb/ch9.h>
53#include <linux/usb/gadget.h>
54#include <linux/module.h>
55
56#include "core.h"
57#include "gadget.h"
58#include "io.h"
59
60#include "debug.h"
61
62/**
63 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
64 * @dwc: pointer to our context structure
65 */
66static void dwc3_core_soft_reset(struct dwc3 *dwc)
67{
68 u32 reg;
69
70 /* Before Resetting PHY, put Core in Reset */
71 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
72 reg |= DWC3_GCTL_CORESOFTRESET;
73 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
74
75 /* Assert USB3 PHY reset */
76 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
77 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
78 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
79
80 /* Assert USB2 PHY reset */
81 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
82 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
83 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
84
85 mdelay(100);
86
87 /* Clear USB3 PHY reset */
88 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
89 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
90 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
91
92 /* Clear USB2 PHY reset */
93 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
94 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
95 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
96
97 /* After PHYs are stable we can take Core out of reset state */
98 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
99 reg &= ~DWC3_GCTL_CORESOFTRESET;
100 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
101}
102
103/**
104 * dwc3_free_one_event_buffer - Frees one event buffer
105 * @dwc: Pointer to our controller context structure
106 * @evt: Pointer to event buffer to be freed
107 */
108static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
109 struct dwc3_event_buffer *evt)
110{
111 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
112 kfree(evt);
113}
114
115/**
116 * dwc3_alloc_one_event_buffer - Allocated one event buffer structure
117 * @dwc: Pointer to our controller context structure
118 * @length: size of the event buffer
119 *
120 * Returns a pointer to the allocated event buffer structure on succes
121 * otherwise ERR_PTR(errno).
122 */
123static struct dwc3_event_buffer *__devinit
124dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
125{
126 struct dwc3_event_buffer *evt;
127
128 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
129 if (!evt)
130 return ERR_PTR(-ENOMEM);
131
132 evt->dwc = dwc;
133 evt->length = length;
134 evt->buf = dma_alloc_coherent(dwc->dev, length,
135 &evt->dma, GFP_KERNEL);
136 if (!evt->buf) {
137 kfree(evt);
138 return ERR_PTR(-ENOMEM);
139 }
140
141 return evt;
142}
143
144/**
145 * dwc3_free_event_buffers - frees all allocated event buffers
146 * @dwc: Pointer to our controller context structure
147 */
148static void dwc3_free_event_buffers(struct dwc3 *dwc)
149{
150 struct dwc3_event_buffer *evt;
151 int i;
152
153 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
154 evt = dwc->ev_buffs[i];
155 if (evt) {
156 dwc3_free_one_event_buffer(dwc, evt);
157 dwc->ev_buffs[i] = NULL;
158 }
159 }
160}
161
162/**
163 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
164 * @dwc: Pointer to out controller context structure
165 * @num: number of event buffers to allocate
166 * @length: size of event buffer
167 *
168 * Returns 0 on success otherwise negative errno. In error the case, dwc
169 * may contain some buffers allocated but not all which were requested.
170 */
171static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned num,
172 unsigned length)
173{
174 int i;
175
176 for (i = 0; i < num; i++) {
177 struct dwc3_event_buffer *evt;
178
179 evt = dwc3_alloc_one_event_buffer(dwc, length);
180 if (IS_ERR(evt)) {
181 dev_err(dwc->dev, "can't allocate event buffer\n");
182 return PTR_ERR(evt);
183 }
184 dwc->ev_buffs[i] = evt;
185 }
186
187 return 0;
188}
189
190/**
191 * dwc3_event_buffers_setup - setup our allocated event buffers
192 * @dwc: Pointer to out controller context structure
193 *
194 * Returns 0 on success otherwise negative errno.
195 */
196static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
197{
198 struct dwc3_event_buffer *evt;
199 int n;
200
201 for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
202 evt = dwc->ev_buffs[n];
203 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
204 evt->buf, (unsigned long long) evt->dma,
205 evt->length);
206
207 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
208 lower_32_bits(evt->dma));
209 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
210 upper_32_bits(evt->dma));
211 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
212 evt->length & 0xffff);
213 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
214 }
215
216 return 0;
217}
218
219static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
220{
221 struct dwc3_event_buffer *evt;
222 int n;
223
224 for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
225 evt = dwc->ev_buffs[n];
226 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
227 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
228 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
229 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
230 }
231}
232
233static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
234{
235 struct dwc3_hwparams *parms = &dwc->hwparams;
236
237 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
238 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
239 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
240 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
241 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
242 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
243 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
244 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
245 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
246}
247
248/**
249 * dwc3_core_init - Low-level initialization of DWC3 Core
250 * @dwc: Pointer to our controller context structure
251 *
252 * Returns 0 on success otherwise negative errno.
253 */
254static int __devinit dwc3_core_init(struct dwc3 *dwc)
255{
256 unsigned long timeout;
257 u32 reg;
258 int ret;
259
260 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
261 /* This should read as U3 followed by revision number */
262 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
263 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
264 ret = -ENODEV;
265 goto err0;
266 }
267 dwc->revision = reg & DWC3_GSNPSREV_MASK;
268
269 dwc3_core_soft_reset(dwc);
270
271 /* issue device SoftReset too */
272 timeout = jiffies + msecs_to_jiffies(500);
273 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
274 do {
275 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
276 if (!(reg & DWC3_DCTL_CSFTRST))
277 break;
278
279 if (time_after(jiffies, timeout)) {
280 dev_err(dwc->dev, "Reset Timed Out\n");
281 ret = -ETIMEDOUT;
282 goto err0;
283 }
284
285 cpu_relax();
286 } while (true);
287
288 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_NUM,
289 DWC3_EVENT_BUFFERS_SIZE);
290 if (ret) {
291 dev_err(dwc->dev, "failed to allocate event buffers\n");
292 ret = -ENOMEM;
293 goto err1;
294 }
295
296 ret = dwc3_event_buffers_setup(dwc);
297 if (ret) {
298 dev_err(dwc->dev, "failed to setup event buffers\n");
299 goto err1;
300 }
301
302 dwc3_cache_hwparams(dwc);
303
304 return 0;
305
306err1:
307 dwc3_free_event_buffers(dwc);
308
309err0:
310 return ret;
311}
312
313static void dwc3_core_exit(struct dwc3 *dwc)
314{
315 dwc3_event_buffers_cleanup(dwc);
316 dwc3_free_event_buffers(dwc);
317}
318
319#define DWC3_ALIGN_MASK (16 - 1)
320
321static int __devinit dwc3_probe(struct platform_device *pdev)
322{
323 const struct platform_device_id *id = platform_get_device_id(pdev);
324 struct resource *res;
325 struct dwc3 *dwc;
326 void __iomem *regs;
327 unsigned int features = id->driver_data;
328 int ret = -ENOMEM;
329 int irq;
330 void *mem;
331
332 mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
333 if (!mem) {
334 dev_err(&pdev->dev, "not enough memory\n");
335 goto err0;
336 }
337 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
338 dwc->mem = mem;
339
340 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 if (!res) {
342 dev_err(&pdev->dev, "missing resource\n");
343 goto err1;
344 }
345
346 res = request_mem_region(res->start, resource_size(res),
347 dev_name(&pdev->dev));
348 if (!res) {
349 dev_err(&pdev->dev, "can't request mem region\n");
350 goto err1;
351 }
352
353 regs = ioremap(res->start, resource_size(res));
354 if (!regs) {
355 dev_err(&pdev->dev, "ioremap failed\n");
356 goto err2;
357 }
358
359 irq = platform_get_irq(pdev, 0);
360 if (irq < 0) {
361 dev_err(&pdev->dev, "missing IRQ\n");
362 goto err3;
363 }
364
365 spin_lock_init(&dwc->lock);
366 platform_set_drvdata(pdev, dwc);
367
368 dwc->regs = regs;
369 dwc->regs_size = resource_size(res);
370 dwc->dev = &pdev->dev;
371 dwc->irq = irq;
372
373 pm_runtime_enable(&pdev->dev);
374 pm_runtime_get_sync(&pdev->dev);
375 pm_runtime_forbid(&pdev->dev);
376
377 ret = dwc3_core_init(dwc);
378 if (ret) {
379 dev_err(&pdev->dev, "failed to initialize core\n");
380 goto err3;
381 }
382
383 if (features & DWC3_HAS_PERIPHERAL) {
384 ret = dwc3_gadget_init(dwc);
385 if (ret) {
386 dev_err(&pdev->dev, "failed to initialized gadget\n");
387 goto err4;
388 }
389 }
390
391 ret = dwc3_debugfs_init(dwc);
392 if (ret) {
393 dev_err(&pdev->dev, "failed to initialize debugfs\n");
394 goto err5;
395 }
396
397 pm_runtime_allow(&pdev->dev);
398
399 return 0;
400
401err5:
402 if (features & DWC3_HAS_PERIPHERAL)
403 dwc3_gadget_exit(dwc);
404
405err4:
406 dwc3_core_exit(dwc);
407
408err3:
409 iounmap(regs);
410
411err2:
412 release_mem_region(res->start, resource_size(res));
413
414err1:
415 kfree(dwc->mem);
416
417err0:
418 return ret;
419}
420
421static int __devexit dwc3_remove(struct platform_device *pdev)
422{
423 const struct platform_device_id *id = platform_get_device_id(pdev);
424 struct dwc3 *dwc = platform_get_drvdata(pdev);
425 struct resource *res;
426 unsigned int features = id->driver_data;
427
428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
429
430 pm_runtime_put(&pdev->dev);
431 pm_runtime_disable(&pdev->dev);
432
433 dwc3_debugfs_exit(dwc);
434
435 if (features & DWC3_HAS_PERIPHERAL)
436 dwc3_gadget_exit(dwc);
437
438 dwc3_core_exit(dwc);
439 release_mem_region(res->start, resource_size(res));
440 iounmap(dwc->regs);
441 kfree(dwc->mem);
442
443 return 0;
444}
445
446static const struct platform_device_id dwc3_id_table[] __devinitconst = {
447 {
448 .name = "dwc3-omap",
449 .driver_data = (DWC3_HAS_PERIPHERAL
450 | DWC3_HAS_XHCI
451 | DWC3_HAS_OTG),
452 },
453 {
454 .name = "dwc3-pci",
455 .driver_data = DWC3_HAS_PERIPHERAL,
456 },
457 { }, /* Terminating Entry */
458};
459MODULE_DEVICE_TABLE(platform, dwc3_id_table);
460
461static struct platform_driver dwc3_driver = {
462 .probe = dwc3_probe,
463 .remove = __devexit_p(dwc3_remove),
464 .driver = {
465 .name = "dwc3",
466 },
467 .id_table = dwc3_id_table,
468};
469
470MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
471MODULE_LICENSE("Dual BSD/GPL");
472MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
473
474static int __devinit dwc3_init(void)
475{
476 return platform_driver_register(&dwc3_driver);
477}
478module_init(dwc3_init);
479
480static void __exit dwc3_exit(void)
481{
482 platform_driver_unregister(&dwc3_driver);
483}
484module_exit(dwc3_exit);
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
new file mode 100644
index 000000000000..29a8e1679e12
--- /dev/null
+++ b/drivers/usb/dwc3/core.h
@@ -0,0 +1,768 @@
1/**
2 * core.h - DesignWare USB3 DRD Core Header
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef __DRIVERS_USB_DWC3_CORE_H
40#define __DRIVERS_USB_DWC3_CORE_H
41
42#include <linux/device.h>
43#include <linux/spinlock.h>
44#include <linux/list.h>
45#include <linux/dma-mapping.h>
46#include <linux/mm.h>
47#include <linux/debugfs.h>
48
49#include <linux/usb/ch9.h>
50#include <linux/usb/gadget.h>
51
52/* Global constants */
53#define DWC3_ENDPOINTS_NUM 32
54
55#define DWC3_EVENT_BUFFERS_NUM 2
56#define DWC3_EVENT_BUFFERS_SIZE PAGE_SIZE
57#define DWC3_EVENT_TYPE_MASK 0xfe
58
59#define DWC3_EVENT_TYPE_DEV 0
60#define DWC3_EVENT_TYPE_CARKIT 3
61#define DWC3_EVENT_TYPE_I2C 4
62
63#define DWC3_DEVICE_EVENT_DISCONNECT 0
64#define DWC3_DEVICE_EVENT_RESET 1
65#define DWC3_DEVICE_EVENT_CONNECT_DONE 2
66#define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3
67#define DWC3_DEVICE_EVENT_WAKEUP 4
68#define DWC3_DEVICE_EVENT_EOPF 6
69#define DWC3_DEVICE_EVENT_SOF 7
70#define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9
71#define DWC3_DEVICE_EVENT_CMD_CMPL 10
72#define DWC3_DEVICE_EVENT_OVERFLOW 11
73
74#define DWC3_GEVNTCOUNT_MASK 0xfffc
75#define DWC3_GSNPSID_MASK 0xffff0000
76#define DWC3_GSNPSREV_MASK 0xffff
77
78/* Global Registers */
79#define DWC3_GSBUSCFG0 0xc100
80#define DWC3_GSBUSCFG1 0xc104
81#define DWC3_GTXTHRCFG 0xc108
82#define DWC3_GRXTHRCFG 0xc10c
83#define DWC3_GCTL 0xc110
84#define DWC3_GEVTEN 0xc114
85#define DWC3_GSTS 0xc118
86#define DWC3_GSNPSID 0xc120
87#define DWC3_GGPIO 0xc124
88#define DWC3_GUID 0xc128
89#define DWC3_GUCTL 0xc12c
90#define DWC3_GBUSERRADDR0 0xc130
91#define DWC3_GBUSERRADDR1 0xc134
92#define DWC3_GPRTBIMAP0 0xc138
93#define DWC3_GPRTBIMAP1 0xc13c
94#define DWC3_GHWPARAMS0 0xc140
95#define DWC3_GHWPARAMS1 0xc144
96#define DWC3_GHWPARAMS2 0xc148
97#define DWC3_GHWPARAMS3 0xc14c
98#define DWC3_GHWPARAMS4 0xc150
99#define DWC3_GHWPARAMS5 0xc154
100#define DWC3_GHWPARAMS6 0xc158
101#define DWC3_GHWPARAMS7 0xc15c
102#define DWC3_GDBGFIFOSPACE 0xc160
103#define DWC3_GDBGLTSSM 0xc164
104#define DWC3_GPRTBIMAP_HS0 0xc180
105#define DWC3_GPRTBIMAP_HS1 0xc184
106#define DWC3_GPRTBIMAP_FS0 0xc188
107#define DWC3_GPRTBIMAP_FS1 0xc18c
108
109#define DWC3_GUSB2PHYCFG(n) (0xc200 + (n * 0x04))
110#define DWC3_GUSB2I2CCTL(n) (0xc240 + (n * 0x04))
111
112#define DWC3_GUSB2PHYACC(n) (0xc280 + (n * 0x04))
113
114#define DWC3_GUSB3PIPECTL(n) (0xc2c0 + (n * 0x04))
115
116#define DWC3_GTXFIFOSIZ(n) (0xc300 + (n * 0x04))
117#define DWC3_GRXFIFOSIZ(n) (0xc380 + (n * 0x04))
118
119#define DWC3_GEVNTADRLO(n) (0xc400 + (n * 0x10))
120#define DWC3_GEVNTADRHI(n) (0xc404 + (n * 0x10))
121#define DWC3_GEVNTSIZ(n) (0xc408 + (n * 0x10))
122#define DWC3_GEVNTCOUNT(n) (0xc40c + (n * 0x10))
123
124#define DWC3_GHWPARAMS8 0xc600
125
126/* Device Registers */
127#define DWC3_DCFG 0xc700
128#define DWC3_DCTL 0xc704
129#define DWC3_DEVTEN 0xc708
130#define DWC3_DSTS 0xc70c
131#define DWC3_DGCMDPAR 0xc710
132#define DWC3_DGCMD 0xc714
133#define DWC3_DALEPENA 0xc720
134#define DWC3_DEPCMDPAR2(n) (0xc800 + (n * 0x10))
135#define DWC3_DEPCMDPAR1(n) (0xc804 + (n * 0x10))
136#define DWC3_DEPCMDPAR0(n) (0xc808 + (n * 0x10))
137#define DWC3_DEPCMD(n) (0xc80c + (n * 0x10))
138
139/* OTG Registers */
140#define DWC3_OCFG 0xcc00
141#define DWC3_OCTL 0xcc04
142#define DWC3_OEVTEN 0xcc08
143#define DWC3_OSTS 0xcc0C
144
145/* Bit fields */
146
147/* Global Configuration Register */
148#define DWC3_GCTL_PWRDNSCALE(n) (n << 19)
149#define DWC3_GCTL_U2RSTECN (1 << 16)
150#define DWC3_GCTL_RAMCLKSEL(x) ((x & DWC3_GCTL_CLK_MASK) << 6)
151#define DWC3_GCTL_CLK_BUS (0)
152#define DWC3_GCTL_CLK_PIPE (1)
153#define DWC3_GCTL_CLK_PIPEHALF (2)
154#define DWC3_GCTL_CLK_MASK (3)
155
156#define DWC3_GCTL_PRTCAPDIR(n) (n << 12)
157#define DWC3_GCTL_PRTCAP_HOST 1
158#define DWC3_GCTL_PRTCAP_DEVICE 2
159#define DWC3_GCTL_PRTCAP_OTG 3
160
161#define DWC3_GCTL_CORESOFTRESET (1 << 11)
162#define DWC3_GCTL_SCALEDOWN(n) (n << 4)
163#define DWC3_GCTL_DISSCRAMBLE (1 << 3)
164#define DWC3_GCTL_DSBLCLKGTNG (1 << 0)
165
166/* Global USB2 PHY Configuration Register */
167#define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31)
168#define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6)
169
170/* Global USB3 PIPE Control Register */
171#define DWC3_GUSB3PIPECTL_PHYSOFTRST (1 << 31)
172#define DWC3_GUSB3PIPECTL_SUSPHY (1 << 17)
173
174/* Global HWPARAMS1 Register */
175#define DWC3_GHWPARAMS1_EN_PWROPT(n) ((n & (3 << 24)) >> 24)
176#define DWC3_GHWPARAMS1_EN_PWROPT_NO 0
177#define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
178
179/* Device Configuration Register */
180#define DWC3_DCFG_DEVADDR(addr) ((addr) << 3)
181#define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f)
182
183#define DWC3_DCFG_SPEED_MASK (7 << 0)
184#define DWC3_DCFG_SUPERSPEED (4 << 0)
185#define DWC3_DCFG_HIGHSPEED (0 << 0)
186#define DWC3_DCFG_FULLSPEED2 (1 << 0)
187#define DWC3_DCFG_LOWSPEED (2 << 0)
188#define DWC3_DCFG_FULLSPEED1 (3 << 0)
189
190/* Device Control Register */
191#define DWC3_DCTL_RUN_STOP (1 << 31)
192#define DWC3_DCTL_CSFTRST (1 << 30)
193#define DWC3_DCTL_LSFTRST (1 << 29)
194
195#define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24)
196#define DWC3_DCTL_HIRD_THRES(n) (((n) & DWC3_DCTL_HIRD_THRES_MASK) >> 24)
197
198#define DWC3_DCTL_APPL1RES (1 << 23)
199
200#define DWC3_DCTL_INITU2ENA (1 << 12)
201#define DWC3_DCTL_ACCEPTU2ENA (1 << 11)
202#define DWC3_DCTL_INITU1ENA (1 << 10)
203#define DWC3_DCTL_ACCEPTU1ENA (1 << 9)
204#define DWC3_DCTL_TSTCTRL_MASK (0xf << 1)
205
206#define DWC3_DCTL_ULSTCHNGREQ_MASK (0x0f << 5)
207#define DWC3_DCTL_ULSTCHNGREQ(n) (((n) << 5) & DWC3_DCTL_ULSTCHNGREQ_MASK)
208
209#define DWC3_DCTL_ULSTCHNG_NO_ACTION (DWC3_DCTL_ULSTCHNGREQ(0))
210#define DWC3_DCTL_ULSTCHNG_SS_DISABLED (DWC3_DCTL_ULSTCHNGREQ(4))
211#define DWC3_DCTL_ULSTCHNG_RX_DETECT (DWC3_DCTL_ULSTCHNGREQ(5))
212#define DWC3_DCTL_ULSTCHNG_SS_INACTIVE (DWC3_DCTL_ULSTCHNGREQ(6))
213#define DWC3_DCTL_ULSTCHNG_RECOVERY (DWC3_DCTL_ULSTCHNGREQ(8))
214#define DWC3_DCTL_ULSTCHNG_COMPLIANCE (DWC3_DCTL_ULSTCHNGREQ(10))
215#define DWC3_DCTL_ULSTCHNG_LOOPBACK (DWC3_DCTL_ULSTCHNGREQ(11))
216
217/* Device Event Enable Register */
218#define DWC3_DEVTEN_VNDRDEVTSTRCVEDEN (1 << 12)
219#define DWC3_DEVTEN_EVNTOVERFLOWEN (1 << 11)
220#define DWC3_DEVTEN_CMDCMPLTEN (1 << 10)
221#define DWC3_DEVTEN_ERRTICERREN (1 << 9)
222#define DWC3_DEVTEN_SOFEN (1 << 7)
223#define DWC3_DEVTEN_EOPFEN (1 << 6)
224#define DWC3_DEVTEN_WKUPEVTEN (1 << 4)
225#define DWC3_DEVTEN_ULSTCNGEN (1 << 3)
226#define DWC3_DEVTEN_CONNECTDONEEN (1 << 2)
227#define DWC3_DEVTEN_USBRSTEN (1 << 1)
228#define DWC3_DEVTEN_DISCONNEVTEN (1 << 0)
229
230/* Device Status Register */
231#define DWC3_DSTS_PWRUPREQ (1 << 24)
232#define DWC3_DSTS_COREIDLE (1 << 23)
233#define DWC3_DSTS_DEVCTRLHLT (1 << 22)
234
235#define DWC3_DSTS_USBLNKST_MASK (0x0f << 18)
236#define DWC3_DSTS_USBLNKST(n) (((n) & DWC3_DSTS_USBLNKST_MASK) >> 18)
237
238#define DWC3_DSTS_RXFIFOEMPTY (1 << 17)
239
240#define DWC3_DSTS_SOFFN_MASK (0x3ff << 3)
241#define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3)
242
243#define DWC3_DSTS_CONNECTSPD (7 << 0)
244
245#define DWC3_DSTS_SUPERSPEED (4 << 0)
246#define DWC3_DSTS_HIGHSPEED (0 << 0)
247#define DWC3_DSTS_FULLSPEED2 (1 << 0)
248#define DWC3_DSTS_LOWSPEED (2 << 0)
249#define DWC3_DSTS_FULLSPEED1 (3 << 0)
250
251/* Device Generic Command Register */
252#define DWC3_DGCMD_SET_LMP 0x01
253#define DWC3_DGCMD_SET_PERIODIC_PAR 0x02
254#define DWC3_DGCMD_XMIT_FUNCTION 0x03
255#define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09
256#define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a
257#define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c
258#define DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK 0x10
259
260/* Device Endpoint Command Register */
261#define DWC3_DEPCMD_PARAM_SHIFT 16
262#define DWC3_DEPCMD_PARAM(x) (x << DWC3_DEPCMD_PARAM_SHIFT)
263#define DWC3_DEPCMD_GET_RSC_IDX(x) ((x >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
264#define DWC3_DEPCMD_STATUS_MASK (0x0f << 12)
265#define DWC3_DEPCMD_STATUS(x) ((x & DWC3_DEPCMD_STATUS_MASK) >> 12)
266#define DWC3_DEPCMD_HIPRI_FORCERM (1 << 11)
267#define DWC3_DEPCMD_CMDACT (1 << 10)
268#define DWC3_DEPCMD_CMDIOC (1 << 8)
269
270#define DWC3_DEPCMD_DEPSTARTCFG (0x09 << 0)
271#define DWC3_DEPCMD_ENDTRANSFER (0x08 << 0)
272#define DWC3_DEPCMD_UPDATETRANSFER (0x07 << 0)
273#define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0)
274#define DWC3_DEPCMD_CLEARSTALL (0x05 << 0)
275#define DWC3_DEPCMD_SETSTALL (0x04 << 0)
276#define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0)
277#define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0)
278#define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0)
279
280/* The EP number goes 0..31 so ep0 is always out and ep1 is always in */
281#define DWC3_DALEPENA_EP(n) (1 << n)
282
283#define DWC3_DEPCMD_TYPE_CONTROL 0
284#define DWC3_DEPCMD_TYPE_ISOC 1
285#define DWC3_DEPCMD_TYPE_BULK 2
286#define DWC3_DEPCMD_TYPE_INTR 3
287
288/* Structures */
289
290struct dwc3_trb_hw;
291
292/**
293 * struct dwc3_event_buffer - Software event buffer representation
294 * @list: a list of event buffers
295 * @buf: _THE_ buffer
296 * @length: size of this buffer
297 * @dma: dma_addr_t
298 * @dwc: pointer to DWC controller
299 */
300struct dwc3_event_buffer {
301 void *buf;
302 unsigned length;
303 unsigned int lpos;
304
305 dma_addr_t dma;
306
307 struct dwc3 *dwc;
308};
309
310#define DWC3_EP_FLAG_STALLED (1 << 0)
311#define DWC3_EP_FLAG_WEDGED (1 << 1)
312
313#define DWC3_EP_DIRECTION_TX true
314#define DWC3_EP_DIRECTION_RX false
315
316#define DWC3_TRB_NUM 32
317#define DWC3_TRB_MASK (DWC3_TRB_NUM - 1)
318
319/**
320 * struct dwc3_ep - device side endpoint representation
321 * @endpoint: usb endpoint
322 * @request_list: list of requests for this endpoint
323 * @req_queued: list of requests on this ep which have TRBs setup
324 * @trb_pool: array of transaction buffers
325 * @trb_pool_dma: dma address of @trb_pool
326 * @free_slot: next slot which is going to be used
327 * @busy_slot: first slot which is owned by HW
328 * @desc: usb_endpoint_descriptor pointer
329 * @dwc: pointer to DWC controller
330 * @flags: endpoint flags (wedged, stalled, ...)
331 * @current_trb: index of current used trb
332 * @number: endpoint number (1 - 15)
333 * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK
334 * @res_trans_idx: Resource transfer index
335 * @interval: the intervall on which the ISOC transfer is started
336 * @name: a human readable name e.g. ep1out-bulk
337 * @direction: true for TX, false for RX
338 * @stream_capable: true when streams are enabled
339 */
340struct dwc3_ep {
341 struct usb_ep endpoint;
342 struct list_head request_list;
343 struct list_head req_queued;
344
345 struct dwc3_trb_hw *trb_pool;
346 dma_addr_t trb_pool_dma;
347 u32 free_slot;
348 u32 busy_slot;
349 const struct usb_endpoint_descriptor *desc;
350 struct dwc3 *dwc;
351
352 unsigned flags;
353#define DWC3_EP_ENABLED (1 << 0)
354#define DWC3_EP_STALL (1 << 1)
355#define DWC3_EP_WEDGE (1 << 2)
356#define DWC3_EP_BUSY (1 << 4)
357#define DWC3_EP_PENDING_REQUEST (1 << 5)
358
359 /* This last one is specific to EP0 */
360#define DWC3_EP0_DIR_IN (1 << 31)
361
362 unsigned current_trb;
363
364 u8 number;
365 u8 type;
366 u8 res_trans_idx;
367 u32 interval;
368
369 char name[20];
370
371 unsigned direction:1;
372 unsigned stream_capable:1;
373};
374
375enum dwc3_phy {
376 DWC3_PHY_UNKNOWN = 0,
377 DWC3_PHY_USB3,
378 DWC3_PHY_USB2,
379};
380
381enum dwc3_ep0_next {
382 DWC3_EP0_UNKNOWN = 0,
383 DWC3_EP0_COMPLETE,
384 DWC3_EP0_NRDY_SETUP,
385 DWC3_EP0_NRDY_DATA,
386 DWC3_EP0_NRDY_STATUS,
387};
388
389enum dwc3_ep0_state {
390 EP0_UNCONNECTED = 0,
391 EP0_SETUP_PHASE,
392 EP0_DATA_PHASE,
393 EP0_STATUS_PHASE,
394};
395
396enum dwc3_link_state {
397 /* In SuperSpeed */
398 DWC3_LINK_STATE_U0 = 0x00, /* in HS, means ON */
399 DWC3_LINK_STATE_U1 = 0x01,
400 DWC3_LINK_STATE_U2 = 0x02, /* in HS, means SLEEP */
401 DWC3_LINK_STATE_U3 = 0x03, /* in HS, means SUSPEND */
402 DWC3_LINK_STATE_SS_DIS = 0x04,
403 DWC3_LINK_STATE_RX_DET = 0x05, /* in HS, means Early Suspend */
404 DWC3_LINK_STATE_SS_INACT = 0x06,
405 DWC3_LINK_STATE_POLL = 0x07,
406 DWC3_LINK_STATE_RECOV = 0x08,
407 DWC3_LINK_STATE_HRESET = 0x09,
408 DWC3_LINK_STATE_CMPLY = 0x0a,
409 DWC3_LINK_STATE_LPBK = 0x0b,
410 DWC3_LINK_STATE_MASK = 0x0f,
411};
412
413enum dwc3_device_state {
414 DWC3_DEFAULT_STATE,
415 DWC3_ADDRESS_STATE,
416 DWC3_CONFIGURED_STATE,
417};
418
419/**
420 * struct dwc3_trb - transfer request block
421 * @bpl: lower 32bit of the buffer
422 * @bph: higher 32bit of the buffer
423 * @length: buffer size (up to 16mb - 1)
424 * @pcm1: packet count m1
425 * @trbsts: trb status
426 * 0 = ok
427 * 1 = missed isoc
428 * 2 = setup pending
429 * @hwo: hardware owner of descriptor
430 * @lst: last trb
431 * @chn: chain buffers
432 * @csp: continue on short packets (only supported on isoc eps)
433 * @trbctl: trb control
434 * 1 = normal
435 * 2 = control-setup
436 * 3 = control-status-2
437 * 4 = control-status-3
438 * 5 = control-data (first trb of data stage)
439 * 6 = isochronous-first (first trb of service interval)
440 * 7 = isochronous
441 * 8 = link trb
442 * others = reserved
443 * @isp_imi: interrupt on short packet / interrupt on missed isoc
444 * @ioc: interrupt on complete
445 * @sid_sofn: Stream ID / SOF Number
446 */
447struct dwc3_trb {
448 u64 bplh;
449
450 union {
451 struct {
452 u32 length:24;
453 u32 pcm1:2;
454 u32 reserved27_26:2;
455 u32 trbsts:4;
456#define DWC3_TRB_STS_OKAY 0
457#define DWC3_TRB_STS_MISSED_ISOC 1
458#define DWC3_TRB_STS_SETUP_PENDING 2
459 };
460 u32 len_pcm;
461 };
462
463 union {
464 struct {
465 u32 hwo:1;
466 u32 lst:1;
467 u32 chn:1;
468 u32 csp:1;
469 u32 trbctl:6;
470 u32 isp_imi:1;
471 u32 ioc:1;
472 u32 reserved13_12:2;
473 u32 sid_sofn:16;
474 u32 reserved31_30:2;
475 };
476 u32 control;
477 };
478} __packed;
479
480/**
481 * struct dwc3_trb_hw - transfer request block (hw format)
482 * @bpl: DW0-3
483 * @bph: DW4-7
484 * @size: DW8-B
485 * @trl: DWC-F
486 */
487struct dwc3_trb_hw {
488 __le32 bpl;
489 __le32 bph;
490 __le32 size;
491 __le32 ctrl;
492} __packed;
493
494static inline void dwc3_trb_to_hw(struct dwc3_trb *nat, struct dwc3_trb_hw *hw)
495{
496 hw->bpl = cpu_to_le32(lower_32_bits(nat->bplh));
497 hw->bph = cpu_to_le32(upper_32_bits(nat->bplh));
498 hw->size = cpu_to_le32p(&nat->len_pcm);
499 /* HWO is written last */
500 hw->ctrl = cpu_to_le32p(&nat->control);
501}
502
503static inline void dwc3_trb_to_nat(struct dwc3_trb_hw *hw, struct dwc3_trb *nat)
504{
505 u64 bplh;
506
507 bplh = le32_to_cpup(&hw->bpl);
508 bplh |= (u64) le32_to_cpup(&hw->bph) << 32;
509 nat->bplh = bplh;
510
511 nat->len_pcm = le32_to_cpup(&hw->size);
512 nat->control = le32_to_cpup(&hw->ctrl);
513}
514
515/**
516 * dwc3_hwparams - copy of HWPARAMS registers
517 * @hwparams0 - GHWPARAMS0
518 * @hwparams1 - GHWPARAMS1
519 * @hwparams2 - GHWPARAMS2
520 * @hwparams3 - GHWPARAMS3
521 * @hwparams4 - GHWPARAMS4
522 * @hwparams5 - GHWPARAMS5
523 * @hwparams6 - GHWPARAMS6
524 * @hwparams7 - GHWPARAMS7
525 * @hwparams8 - GHWPARAMS8
526 */
527struct dwc3_hwparams {
528 u32 hwparams0;
529 u32 hwparams1;
530 u32 hwparams2;
531 u32 hwparams3;
532 u32 hwparams4;
533 u32 hwparams5;
534 u32 hwparams6;
535 u32 hwparams7;
536 u32 hwparams8;
537};
538
539/**
540 * struct dwc3 - representation of our controller
541 * @ctrl_req: usb control request which is used for ep0
542 * @ep0_trb: trb which is used for the ctrl_req
543 * @ep0_bounce: bounce buffer for ep0
544 * @setup_buf: used while precessing STD USB requests
545 * @ctrl_req_addr: dma address of ctrl_req
546 * @ep0_trb: dma address of ep0_trb
547 * @ep0_usb_req: dummy req used while handling STD USB requests
548 * @setup_buf_addr: dma address of setup_buf
549 * @ep0_bounce_addr: dma address of ep0_bounce
550 * @lock: for synchronizing
551 * @dev: pointer to our struct device
552 * @event_buffer_list: a list of event buffers
553 * @gadget: device side representation of the peripheral controller
554 * @gadget_driver: pointer to the gadget driver
555 * @regs: base address for our registers
556 * @regs_size: address space size
557 * @irq: IRQ number
558 * @revision: revision register contents
559 * @is_selfpowered: true when we are selfpowered
560 * @three_stage_setup: set if we perform a three phase setup
561 * @ep0_status_pending: ep0 status response without a req is pending
562 * @ep0_bounced: true when we used bounce buffer
563 * @ep0_expect_in: true when we expect a DATA IN transfer
564 * @start_config_issued: true when StartConfig command has been issued
565 * @ep0_next_event: hold the next expected event
566 * @ep0state: state of endpoint zero
567 * @link_state: link state
568 * @speed: device speed (super, high, full, low)
569 * @mem: points to start of memory which is used for this struct.
570 * @hwparams: copy of hwparams registers
571 * @root: debugfs root folder pointer
572 */
573struct dwc3 {
574 struct usb_ctrlrequest *ctrl_req;
575 struct dwc3_trb_hw *ep0_trb;
576 void *ep0_bounce;
577 u8 *setup_buf;
578 dma_addr_t ctrl_req_addr;
579 dma_addr_t ep0_trb_addr;
580 dma_addr_t setup_buf_addr;
581 dma_addr_t ep0_bounce_addr;
582 struct usb_request ep0_usb_req;
583 /* device lock */
584 spinlock_t lock;
585 struct device *dev;
586
587 struct dwc3_event_buffer *ev_buffs[DWC3_EVENT_BUFFERS_NUM];
588 struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM];
589
590 struct usb_gadget gadget;
591 struct usb_gadget_driver *gadget_driver;
592
593 void __iomem *regs;
594 size_t regs_size;
595
596 int irq;
597
598 u32 revision;
599
600#define DWC3_REVISION_173A 0x5533173a
601#define DWC3_REVISION_175A 0x5533175a
602#define DWC3_REVISION_180A 0x5533180a
603#define DWC3_REVISION_183A 0x5533183a
604#define DWC3_REVISION_185A 0x5533185a
605#define DWC3_REVISION_188A 0x5533188a
606#define DWC3_REVISION_190A 0x5533190a
607
608 unsigned is_selfpowered:1;
609 unsigned three_stage_setup:1;
610 unsigned ep0_status_pending:1;
611 unsigned ep0_bounced:1;
612 unsigned ep0_expect_in:1;
613 unsigned start_config_issued:1;
614
615 enum dwc3_ep0_next ep0_next_event;
616 enum dwc3_ep0_state ep0state;
617 enum dwc3_link_state link_state;
618 enum dwc3_device_state dev_state;
619
620 u8 speed;
621 void *mem;
622
623 struct dwc3_hwparams hwparams;
624 struct dentry *root;
625};
626
627/* -------------------------------------------------------------------------- */
628
629#define DWC3_TRBSTS_OK 0
630#define DWC3_TRBSTS_MISSED_ISOC 1
631#define DWC3_TRBSTS_SETUP_PENDING 2
632
633#define DWC3_TRBCTL_NORMAL 1
634#define DWC3_TRBCTL_CONTROL_SETUP 2
635#define DWC3_TRBCTL_CONTROL_STATUS2 3
636#define DWC3_TRBCTL_CONTROL_STATUS3 4
637#define DWC3_TRBCTL_CONTROL_DATA 5
638#define DWC3_TRBCTL_ISOCHRONOUS_FIRST 6
639#define DWC3_TRBCTL_ISOCHRONOUS 7
640#define DWC3_TRBCTL_LINK_TRB 8
641
642/* -------------------------------------------------------------------------- */
643
644struct dwc3_event_type {
645 u32 is_devspec:1;
646 u32 type:6;
647 u32 reserved8_31:25;
648} __packed;
649
650#define DWC3_DEPEVT_XFERCOMPLETE 0x01
651#define DWC3_DEPEVT_XFERINPROGRESS 0x02
652#define DWC3_DEPEVT_XFERNOTREADY 0x03
653#define DWC3_DEPEVT_RXTXFIFOEVT 0x04
654#define DWC3_DEPEVT_STREAMEVT 0x06
655#define DWC3_DEPEVT_EPCMDCMPLT 0x07
656
657/**
658 * struct dwc3_event_depvt - Device Endpoint Events
659 * @one_bit: indicates this is an endpoint event (not used)
660 * @endpoint_number: number of the endpoint
661 * @endpoint_event: The event we have:
662 * 0x00 - Reserved
663 * 0x01 - XferComplete
664 * 0x02 - XferInProgress
665 * 0x03 - XferNotReady
666 * 0x04 - RxTxFifoEvt (IN->Underrun, OUT->Overrun)
667 * 0x05 - Reserved
668 * 0x06 - StreamEvt
669 * 0x07 - EPCmdCmplt
670 * @reserved11_10: Reserved, don't use.
671 * @status: Indicates the status of the event. Refer to databook for
672 * more information.
673 * @parameters: Parameters of the current event. Refer to databook for
674 * more information.
675 */
676struct dwc3_event_depevt {
677 u32 one_bit:1;
678 u32 endpoint_number:5;
679 u32 endpoint_event:4;
680 u32 reserved11_10:2;
681 u32 status:4;
682#define DEPEVT_STATUS_BUSERR (1 << 0)
683#define DEPEVT_STATUS_SHORT (1 << 1)
684#define DEPEVT_STATUS_IOC (1 << 2)
685#define DEPEVT_STATUS_LST (1 << 3)
686
687/* Stream event only */
688#define DEPEVT_STREAMEVT_FOUND 1
689#define DEPEVT_STREAMEVT_NOTFOUND 2
690
691/* Control-only Status */
692#define DEPEVT_STATUS_CONTROL_SETUP 0
693#define DEPEVT_STATUS_CONTROL_DATA 1
694#define DEPEVT_STATUS_CONTROL_STATUS 2
695
696 u32 parameters:16;
697} __packed;
698
699/**
700 * struct dwc3_event_devt - Device Events
701 * @one_bit: indicates this is a non-endpoint event (not used)
702 * @device_event: indicates it's a device event. Should read as 0x00
703 * @type: indicates the type of device event.
704 * 0 - DisconnEvt
705 * 1 - USBRst
706 * 2 - ConnectDone
707 * 3 - ULStChng
708 * 4 - WkUpEvt
709 * 5 - Reserved
710 * 6 - EOPF
711 * 7 - SOF
712 * 8 - Reserved
713 * 9 - ErrticErr
714 * 10 - CmdCmplt
715 * 11 - EvntOverflow
716 * 12 - VndrDevTstRcved
717 * @reserved15_12: Reserved, not used
718 * @event_info: Information about this event
719 * @reserved31_24: Reserved, not used
720 */
721struct dwc3_event_devt {
722 u32 one_bit:1;
723 u32 device_event:7;
724 u32 type:4;
725 u32 reserved15_12:4;
726 u32 event_info:8;
727 u32 reserved31_24:8;
728} __packed;
729
730/**
731 * struct dwc3_event_gevt - Other Core Events
732 * @one_bit: indicates this is a non-endpoint event (not used)
733 * @device_event: indicates it's (0x03) Carkit or (0x04) I2C event.
734 * @phy_port_number: self-explanatory
735 * @reserved31_12: Reserved, not used.
736 */
737struct dwc3_event_gevt {
738 u32 one_bit:1;
739 u32 device_event:7;
740 u32 phy_port_number:4;
741 u32 reserved31_12:20;
742} __packed;
743
744/**
745 * union dwc3_event - representation of Event Buffer contents
746 * @raw: raw 32-bit event
747 * @type: the type of the event
748 * @depevt: Device Endpoint Event
749 * @devt: Device Event
750 * @gevt: Global Event
751 */
752union dwc3_event {
753 u32 raw;
754 struct dwc3_event_type type;
755 struct dwc3_event_depevt depevt;
756 struct dwc3_event_devt devt;
757 struct dwc3_event_gevt gevt;
758};
759
760/*
761 * DWC3 Features to be used as Driver Data
762 */
763
764#define DWC3_HAS_PERIPHERAL BIT(0)
765#define DWC3_HAS_XHCI BIT(1)
766#define DWC3_HAS_OTG BIT(3)
767
768#endif /* __DRIVERS_USB_DWC3_CORE_H */
diff --git a/drivers/usb/dwc3/debug.h b/drivers/usb/dwc3/debug.h
new file mode 100644
index 000000000000..5894ee8222af
--- /dev/null
+++ b/drivers/usb/dwc3/debug.h
@@ -0,0 +1,50 @@
1/**
2 * debug.h - DesignWare USB3 DRD Controller Debug Header
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "core.h"
40
41#ifdef CONFIG_DEBUG_FS
42extern int dwc3_debugfs_init(struct dwc3 *);
43extern void dwc3_debugfs_exit(struct dwc3 *);
44#else
45static inline int dwc3_debugfs_init(struct dwc3 *d)
46{ return 0; }
47static inline void dwc3_debugfs_exit(struct dwc3 *d)
48{ }
49#endif
50
diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c
new file mode 100644
index 000000000000..da1ad77d8d51
--- /dev/null
+++ b/drivers/usb/dwc3/debugfs.c
@@ -0,0 +1,441 @@
1/**
2 * debugfs.c - DesignWare USB3 DRD Controller DebugFS file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/slab.h>
41#include <linux/ptrace.h>
42#include <linux/types.h>
43#include <linux/spinlock.h>
44#include <linux/debugfs.h>
45#include <linux/seq_file.h>
46#include <linux/delay.h>
47
48#include <asm/uaccess.h>
49
50#include "core.h"
51#include "gadget.h"
52#include "io.h"
53
54struct dwc3_register {
55 const char *name;
56 u32 offset;
57};
58
59#define dump_register(nm) \
60{ \
61 .name = __stringify(nm), \
62 .offset = DWC3_ ##nm, \
63}
64
65static const struct dwc3_register dwc3_regs[] = {
66 dump_register(GSBUSCFG0),
67 dump_register(GSBUSCFG1),
68 dump_register(GTXTHRCFG),
69 dump_register(GRXTHRCFG),
70 dump_register(GCTL),
71 dump_register(GEVTEN),
72 dump_register(GSTS),
73 dump_register(GSNPSID),
74 dump_register(GGPIO),
75 dump_register(GUID),
76 dump_register(GUCTL),
77 dump_register(GBUSERRADDR0),
78 dump_register(GBUSERRADDR1),
79 dump_register(GPRTBIMAP0),
80 dump_register(GPRTBIMAP1),
81 dump_register(GHWPARAMS0),
82 dump_register(GHWPARAMS1),
83 dump_register(GHWPARAMS2),
84 dump_register(GHWPARAMS3),
85 dump_register(GHWPARAMS4),
86 dump_register(GHWPARAMS5),
87 dump_register(GHWPARAMS6),
88 dump_register(GHWPARAMS7),
89 dump_register(GDBGFIFOSPACE),
90 dump_register(GDBGLTSSM),
91 dump_register(GPRTBIMAP_HS0),
92 dump_register(GPRTBIMAP_HS1),
93 dump_register(GPRTBIMAP_FS0),
94 dump_register(GPRTBIMAP_FS1),
95
96 dump_register(GUSB2PHYCFG(0)),
97 dump_register(GUSB2PHYCFG(1)),
98 dump_register(GUSB2PHYCFG(2)),
99 dump_register(GUSB2PHYCFG(3)),
100 dump_register(GUSB2PHYCFG(4)),
101 dump_register(GUSB2PHYCFG(5)),
102 dump_register(GUSB2PHYCFG(6)),
103 dump_register(GUSB2PHYCFG(7)),
104 dump_register(GUSB2PHYCFG(8)),
105 dump_register(GUSB2PHYCFG(9)),
106 dump_register(GUSB2PHYCFG(10)),
107 dump_register(GUSB2PHYCFG(11)),
108 dump_register(GUSB2PHYCFG(12)),
109 dump_register(GUSB2PHYCFG(13)),
110 dump_register(GUSB2PHYCFG(14)),
111 dump_register(GUSB2PHYCFG(15)),
112
113 dump_register(GUSB2I2CCTL(0)),
114 dump_register(GUSB2I2CCTL(1)),
115 dump_register(GUSB2I2CCTL(2)),
116 dump_register(GUSB2I2CCTL(3)),
117 dump_register(GUSB2I2CCTL(4)),
118 dump_register(GUSB2I2CCTL(5)),
119 dump_register(GUSB2I2CCTL(6)),
120 dump_register(GUSB2I2CCTL(7)),
121 dump_register(GUSB2I2CCTL(8)),
122 dump_register(GUSB2I2CCTL(9)),
123 dump_register(GUSB2I2CCTL(10)),
124 dump_register(GUSB2I2CCTL(11)),
125 dump_register(GUSB2I2CCTL(12)),
126 dump_register(GUSB2I2CCTL(13)),
127 dump_register(GUSB2I2CCTL(14)),
128 dump_register(GUSB2I2CCTL(15)),
129
130 dump_register(GUSB2PHYACC(0)),
131 dump_register(GUSB2PHYACC(1)),
132 dump_register(GUSB2PHYACC(2)),
133 dump_register(GUSB2PHYACC(3)),
134 dump_register(GUSB2PHYACC(4)),
135 dump_register(GUSB2PHYACC(5)),
136 dump_register(GUSB2PHYACC(6)),
137 dump_register(GUSB2PHYACC(7)),
138 dump_register(GUSB2PHYACC(8)),
139 dump_register(GUSB2PHYACC(9)),
140 dump_register(GUSB2PHYACC(10)),
141 dump_register(GUSB2PHYACC(11)),
142 dump_register(GUSB2PHYACC(12)),
143 dump_register(GUSB2PHYACC(13)),
144 dump_register(GUSB2PHYACC(14)),
145 dump_register(GUSB2PHYACC(15)),
146
147 dump_register(GUSB3PIPECTL(0)),
148 dump_register(GUSB3PIPECTL(1)),
149 dump_register(GUSB3PIPECTL(2)),
150 dump_register(GUSB3PIPECTL(3)),
151 dump_register(GUSB3PIPECTL(4)),
152 dump_register(GUSB3PIPECTL(5)),
153 dump_register(GUSB3PIPECTL(6)),
154 dump_register(GUSB3PIPECTL(7)),
155 dump_register(GUSB3PIPECTL(8)),
156 dump_register(GUSB3PIPECTL(9)),
157 dump_register(GUSB3PIPECTL(10)),
158 dump_register(GUSB3PIPECTL(11)),
159 dump_register(GUSB3PIPECTL(12)),
160 dump_register(GUSB3PIPECTL(13)),
161 dump_register(GUSB3PIPECTL(14)),
162 dump_register(GUSB3PIPECTL(15)),
163
164 dump_register(GTXFIFOSIZ(0)),
165 dump_register(GTXFIFOSIZ(1)),
166 dump_register(GTXFIFOSIZ(2)),
167 dump_register(GTXFIFOSIZ(3)),
168 dump_register(GTXFIFOSIZ(4)),
169 dump_register(GTXFIFOSIZ(5)),
170 dump_register(GTXFIFOSIZ(6)),
171 dump_register(GTXFIFOSIZ(7)),
172 dump_register(GTXFIFOSIZ(8)),
173 dump_register(GTXFIFOSIZ(9)),
174 dump_register(GTXFIFOSIZ(10)),
175 dump_register(GTXFIFOSIZ(11)),
176 dump_register(GTXFIFOSIZ(12)),
177 dump_register(GTXFIFOSIZ(13)),
178 dump_register(GTXFIFOSIZ(14)),
179 dump_register(GTXFIFOSIZ(15)),
180 dump_register(GTXFIFOSIZ(16)),
181 dump_register(GTXFIFOSIZ(17)),
182 dump_register(GTXFIFOSIZ(18)),
183 dump_register(GTXFIFOSIZ(19)),
184 dump_register(GTXFIFOSIZ(20)),
185 dump_register(GTXFIFOSIZ(21)),
186 dump_register(GTXFIFOSIZ(22)),
187 dump_register(GTXFIFOSIZ(23)),
188 dump_register(GTXFIFOSIZ(24)),
189 dump_register(GTXFIFOSIZ(25)),
190 dump_register(GTXFIFOSIZ(26)),
191 dump_register(GTXFIFOSIZ(27)),
192 dump_register(GTXFIFOSIZ(28)),
193 dump_register(GTXFIFOSIZ(29)),
194 dump_register(GTXFIFOSIZ(30)),
195 dump_register(GTXFIFOSIZ(31)),
196
197 dump_register(GRXFIFOSIZ(0)),
198 dump_register(GRXFIFOSIZ(1)),
199 dump_register(GRXFIFOSIZ(2)),
200 dump_register(GRXFIFOSIZ(3)),
201 dump_register(GRXFIFOSIZ(4)),
202 dump_register(GRXFIFOSIZ(5)),
203 dump_register(GRXFIFOSIZ(6)),
204 dump_register(GRXFIFOSIZ(7)),
205 dump_register(GRXFIFOSIZ(8)),
206 dump_register(GRXFIFOSIZ(9)),
207 dump_register(GRXFIFOSIZ(10)),
208 dump_register(GRXFIFOSIZ(11)),
209 dump_register(GRXFIFOSIZ(12)),
210 dump_register(GRXFIFOSIZ(13)),
211 dump_register(GRXFIFOSIZ(14)),
212 dump_register(GRXFIFOSIZ(15)),
213 dump_register(GRXFIFOSIZ(16)),
214 dump_register(GRXFIFOSIZ(17)),
215 dump_register(GRXFIFOSIZ(18)),
216 dump_register(GRXFIFOSIZ(19)),
217 dump_register(GRXFIFOSIZ(20)),
218 dump_register(GRXFIFOSIZ(21)),
219 dump_register(GRXFIFOSIZ(22)),
220 dump_register(GRXFIFOSIZ(23)),
221 dump_register(GRXFIFOSIZ(24)),
222 dump_register(GRXFIFOSIZ(25)),
223 dump_register(GRXFIFOSIZ(26)),
224 dump_register(GRXFIFOSIZ(27)),
225 dump_register(GRXFIFOSIZ(28)),
226 dump_register(GRXFIFOSIZ(29)),
227 dump_register(GRXFIFOSIZ(30)),
228 dump_register(GRXFIFOSIZ(31)),
229
230 dump_register(GEVNTADRLO(0)),
231 dump_register(GEVNTADRHI(0)),
232 dump_register(GEVNTSIZ(0)),
233 dump_register(GEVNTCOUNT(0)),
234
235 dump_register(GHWPARAMS8),
236 dump_register(DCFG),
237 dump_register(DCTL),
238 dump_register(DEVTEN),
239 dump_register(DSTS),
240 dump_register(DGCMDPAR),
241 dump_register(DGCMD),
242 dump_register(DALEPENA),
243
244 dump_register(DEPCMDPAR2(0)),
245 dump_register(DEPCMDPAR2(1)),
246 dump_register(DEPCMDPAR2(2)),
247 dump_register(DEPCMDPAR2(3)),
248 dump_register(DEPCMDPAR2(4)),
249 dump_register(DEPCMDPAR2(5)),
250 dump_register(DEPCMDPAR2(6)),
251 dump_register(DEPCMDPAR2(7)),
252 dump_register(DEPCMDPAR2(8)),
253 dump_register(DEPCMDPAR2(9)),
254 dump_register(DEPCMDPAR2(10)),
255 dump_register(DEPCMDPAR2(11)),
256 dump_register(DEPCMDPAR2(12)),
257 dump_register(DEPCMDPAR2(13)),
258 dump_register(DEPCMDPAR2(14)),
259 dump_register(DEPCMDPAR2(15)),
260 dump_register(DEPCMDPAR2(16)),
261 dump_register(DEPCMDPAR2(17)),
262 dump_register(DEPCMDPAR2(18)),
263 dump_register(DEPCMDPAR2(19)),
264 dump_register(DEPCMDPAR2(20)),
265 dump_register(DEPCMDPAR2(21)),
266 dump_register(DEPCMDPAR2(22)),
267 dump_register(DEPCMDPAR2(23)),
268 dump_register(DEPCMDPAR2(24)),
269 dump_register(DEPCMDPAR2(25)),
270 dump_register(DEPCMDPAR2(26)),
271 dump_register(DEPCMDPAR2(27)),
272 dump_register(DEPCMDPAR2(28)),
273 dump_register(DEPCMDPAR2(29)),
274 dump_register(DEPCMDPAR2(30)),
275 dump_register(DEPCMDPAR2(31)),
276
277 dump_register(DEPCMDPAR1(0)),
278 dump_register(DEPCMDPAR1(1)),
279 dump_register(DEPCMDPAR1(2)),
280 dump_register(DEPCMDPAR1(3)),
281 dump_register(DEPCMDPAR1(4)),
282 dump_register(DEPCMDPAR1(5)),
283 dump_register(DEPCMDPAR1(6)),
284 dump_register(DEPCMDPAR1(7)),
285 dump_register(DEPCMDPAR1(8)),
286 dump_register(DEPCMDPAR1(9)),
287 dump_register(DEPCMDPAR1(10)),
288 dump_register(DEPCMDPAR1(11)),
289 dump_register(DEPCMDPAR1(12)),
290 dump_register(DEPCMDPAR1(13)),
291 dump_register(DEPCMDPAR1(14)),
292 dump_register(DEPCMDPAR1(15)),
293 dump_register(DEPCMDPAR1(16)),
294 dump_register(DEPCMDPAR1(17)),
295 dump_register(DEPCMDPAR1(18)),
296 dump_register(DEPCMDPAR1(19)),
297 dump_register(DEPCMDPAR1(20)),
298 dump_register(DEPCMDPAR1(21)),
299 dump_register(DEPCMDPAR1(22)),
300 dump_register(DEPCMDPAR1(23)),
301 dump_register(DEPCMDPAR1(24)),
302 dump_register(DEPCMDPAR1(25)),
303 dump_register(DEPCMDPAR1(26)),
304 dump_register(DEPCMDPAR1(27)),
305 dump_register(DEPCMDPAR1(28)),
306 dump_register(DEPCMDPAR1(29)),
307 dump_register(DEPCMDPAR1(30)),
308 dump_register(DEPCMDPAR1(31)),
309
310 dump_register(DEPCMDPAR0(0)),
311 dump_register(DEPCMDPAR0(1)),
312 dump_register(DEPCMDPAR0(2)),
313 dump_register(DEPCMDPAR0(3)),
314 dump_register(DEPCMDPAR0(4)),
315 dump_register(DEPCMDPAR0(5)),
316 dump_register(DEPCMDPAR0(6)),
317 dump_register(DEPCMDPAR0(7)),
318 dump_register(DEPCMDPAR0(8)),
319 dump_register(DEPCMDPAR0(9)),
320 dump_register(DEPCMDPAR0(10)),
321 dump_register(DEPCMDPAR0(11)),
322 dump_register(DEPCMDPAR0(12)),
323 dump_register(DEPCMDPAR0(13)),
324 dump_register(DEPCMDPAR0(14)),
325 dump_register(DEPCMDPAR0(15)),
326 dump_register(DEPCMDPAR0(16)),
327 dump_register(DEPCMDPAR0(17)),
328 dump_register(DEPCMDPAR0(18)),
329 dump_register(DEPCMDPAR0(19)),
330 dump_register(DEPCMDPAR0(20)),
331 dump_register(DEPCMDPAR0(21)),
332 dump_register(DEPCMDPAR0(22)),
333 dump_register(DEPCMDPAR0(23)),
334 dump_register(DEPCMDPAR0(24)),
335 dump_register(DEPCMDPAR0(25)),
336 dump_register(DEPCMDPAR0(26)),
337 dump_register(DEPCMDPAR0(27)),
338 dump_register(DEPCMDPAR0(28)),
339 dump_register(DEPCMDPAR0(29)),
340 dump_register(DEPCMDPAR0(30)),
341 dump_register(DEPCMDPAR0(31)),
342
343 dump_register(DEPCMD(0)),
344 dump_register(DEPCMD(1)),
345 dump_register(DEPCMD(2)),
346 dump_register(DEPCMD(3)),
347 dump_register(DEPCMD(4)),
348 dump_register(DEPCMD(5)),
349 dump_register(DEPCMD(6)),
350 dump_register(DEPCMD(7)),
351 dump_register(DEPCMD(8)),
352 dump_register(DEPCMD(9)),
353 dump_register(DEPCMD(10)),
354 dump_register(DEPCMD(11)),
355 dump_register(DEPCMD(12)),
356 dump_register(DEPCMD(13)),
357 dump_register(DEPCMD(14)),
358 dump_register(DEPCMD(15)),
359 dump_register(DEPCMD(16)),
360 dump_register(DEPCMD(17)),
361 dump_register(DEPCMD(18)),
362 dump_register(DEPCMD(19)),
363 dump_register(DEPCMD(20)),
364 dump_register(DEPCMD(21)),
365 dump_register(DEPCMD(22)),
366 dump_register(DEPCMD(23)),
367 dump_register(DEPCMD(24)),
368 dump_register(DEPCMD(25)),
369 dump_register(DEPCMD(26)),
370 dump_register(DEPCMD(27)),
371 dump_register(DEPCMD(28)),
372 dump_register(DEPCMD(29)),
373 dump_register(DEPCMD(30)),
374 dump_register(DEPCMD(31)),
375
376 dump_register(OCFG),
377 dump_register(OCTL),
378 dump_register(OEVTEN),
379 dump_register(OSTS),
380};
381
382static int dwc3_regdump_show(struct seq_file *s, void *unused)
383{
384 struct dwc3 *dwc = s->private;
385 int i;
386
387 seq_printf(s, "DesignWare USB3 Core Register Dump\n");
388
389 for (i = 0; i < ARRAY_SIZE(dwc3_regs); i++) {
390 seq_printf(s, "%-20s : %08x\n", dwc3_regs[i].name,
391 dwc3_readl(dwc->regs, dwc3_regs[i].offset));
392 }
393
394 return 0;
395}
396
397static int dwc3_regdump_open(struct inode *inode, struct file *file)
398{
399 return single_open(file, dwc3_regdump_show, inode->i_private);
400}
401
402static const struct file_operations dwc3_regdump_fops = {
403 .open = dwc3_regdump_open,
404 .read = seq_read,
405 .release = single_release,
406};
407
408int __devinit dwc3_debugfs_init(struct dwc3 *dwc)
409{
410 struct dentry *root;
411 struct dentry *file;
412 int ret;
413
414 root = debugfs_create_dir(dev_name(dwc->dev), NULL);
415 if (IS_ERR(root)){
416 ret = PTR_ERR(root);
417 goto err0;
418 }
419
420 dwc->root = root;
421
422 file = debugfs_create_file("regdump", S_IRUGO, root, dwc,
423 &dwc3_regdump_fops);
424 if (IS_ERR(file)) {
425 ret = PTR_ERR(file);
426 goto err1;
427 }
428 return 0;
429
430err1:
431 debugfs_remove_recursive(root);
432
433err0:
434 return ret;
435}
436
437void __devexit dwc3_debugfs_exit(struct dwc3 *dwc)
438{
439 debugfs_remove_recursive(dwc->root);
440 dwc->root = NULL;
441}
diff --git a/drivers/usb/dwc3/dwc3-omap.c b/drivers/usb/dwc3/dwc3-omap.c
new file mode 100644
index 000000000000..062552b5fc8a
--- /dev/null
+++ b/drivers/usb/dwc3/dwc3-omap.c
@@ -0,0 +1,401 @@
1/**
2 * dwc3-omap.c - OMAP Specific Glue layer
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/slab.h>
42#include <linux/interrupt.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/platform_data/dwc3-omap.h>
46#include <linux/dma-mapping.h>
47#include <linux/ioport.h>
48#include <linux/io.h>
49#include <linux/module.h>
50
51#include "io.h"
52
53/*
54 * All these registers belong to OMAP's Wrapper around the
55 * DesignWare USB3 Core.
56 */
57
58#define USBOTGSS_REVISION 0x0000
59#define USBOTGSS_SYSCONFIG 0x0010
60#define USBOTGSS_IRQ_EOI 0x0020
61#define USBOTGSS_IRQSTATUS_RAW_0 0x0024
62#define USBOTGSS_IRQSTATUS_0 0x0028
63#define USBOTGSS_IRQENABLE_SET_0 0x002c
64#define USBOTGSS_IRQENABLE_CLR_0 0x0030
65#define USBOTGSS_IRQSTATUS_RAW_1 0x0034
66#define USBOTGSS_IRQSTATUS_1 0x0038
67#define USBOTGSS_IRQENABLE_SET_1 0x003c
68#define USBOTGSS_IRQENABLE_CLR_1 0x0040
69#define USBOTGSS_UTMI_OTG_CTRL 0x0080
70#define USBOTGSS_UTMI_OTG_STATUS 0x0084
71#define USBOTGSS_MMRAM_OFFSET 0x0100
72#define USBOTGSS_FLADJ 0x0104
73#define USBOTGSS_DEBUG_CFG 0x0108
74#define USBOTGSS_DEBUG_DATA 0x010c
75
76/* SYSCONFIG REGISTER */
77#define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
78#define USBOTGSS_SYSCONFIG_STANDBYMODE(x) ((x) << 4)
79
80#define USBOTGSS_STANDBYMODE_FORCE_STANDBY 0
81#define USBOTGSS_STANDBYMODE_NO_STANDBY 1
82#define USBOTGSS_STANDBYMODE_SMART_STANDBY 2
83#define USBOTGSS_STANDBYMODE_SMART_WAKEUP 3
84
85#define USBOTGSS_STANDBYMODE_MASK (0x03 << 4)
86
87#define USBOTGSS_SYSCONFIG_IDLEMODE(x) ((x) << 2)
88
89#define USBOTGSS_IDLEMODE_FORCE_IDLE 0
90#define USBOTGSS_IDLEMODE_NO_IDLE 1
91#define USBOTGSS_IDLEMODE_SMART_IDLE 2
92#define USBOTGSS_IDLEMODE_SMART_WAKEUP 3
93
94#define USBOTGSS_IDLEMODE_MASK (0x03 << 2)
95
96/* IRQ_EOI REGISTER */
97#define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
98
99/* IRQS0 BITS */
100#define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
101
102/* IRQ1 BITS */
103#define USBOTGSS_IRQ1_DMADISABLECLR (1 << 17)
104#define USBOTGSS_IRQ1_OEVT (1 << 16)
105#define USBOTGSS_IRQ1_DRVVBUS_RISE (1 << 13)
106#define USBOTGSS_IRQ1_CHRGVBUS_RISE (1 << 12)
107#define USBOTGSS_IRQ1_DISCHRGVBUS_RISE (1 << 11)
108#define USBOTGSS_IRQ1_IDPULLUP_RISE (1 << 8)
109#define USBOTGSS_IRQ1_DRVVBUS_FALL (1 << 5)
110#define USBOTGSS_IRQ1_CHRGVBUS_FALL (1 << 4)
111#define USBOTGSS_IRQ1_DISCHRGVBUS_FALL (1 << 3)
112#define USBOTGSS_IRQ1_IDPULLUP_FALL (1 << 0)
113
114/* UTMI_OTG_CTRL REGISTER */
115#define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS (1 << 5)
116#define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS (1 << 4)
117#define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS (1 << 3)
118#define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP (1 << 0)
119
120/* UTMI_OTG_STATUS REGISTER */
121#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE (1 << 31)
122#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT (1 << 9)
123#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
124#define USBOTGSS_UTMI_OTG_STATUS_IDDIG (1 << 4)
125#define USBOTGSS_UTMI_OTG_STATUS_SESSEND (1 << 3)
126#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID (1 << 2)
127#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
128
129struct dwc3_omap {
130 /* device lock */
131 spinlock_t lock;
132
133 struct platform_device *dwc3;
134 struct device *dev;
135
136 int irq;
137 void __iomem *base;
138
139 void *context;
140 u32 resource_size;
141
142 u32 dma_status:1;
143};
144
145static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
146{
147 struct dwc3_omap *omap = _omap;
148 u32 reg;
149
150 spin_lock(&omap->lock);
151
152 reg = dwc3_readl(omap->base, USBOTGSS_IRQSTATUS_1);
153
154 if (reg & USBOTGSS_IRQ1_DMADISABLECLR) {
155 dev_dbg(omap->dev, "DMA Disable was Cleared\n");
156 omap->dma_status = false;
157 }
158
159 if (reg & USBOTGSS_IRQ1_OEVT)
160 dev_dbg(omap->dev, "OTG Event\n");
161
162 if (reg & USBOTGSS_IRQ1_DRVVBUS_RISE)
163 dev_dbg(omap->dev, "DRVVBUS Rise\n");
164
165 if (reg & USBOTGSS_IRQ1_CHRGVBUS_RISE)
166 dev_dbg(omap->dev, "CHRGVBUS Rise\n");
167
168 if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_RISE)
169 dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
170
171 if (reg & USBOTGSS_IRQ1_IDPULLUP_RISE)
172 dev_dbg(omap->dev, "IDPULLUP Rise\n");
173
174 if (reg & USBOTGSS_IRQ1_DRVVBUS_FALL)
175 dev_dbg(omap->dev, "DRVVBUS Fall\n");
176
177 if (reg & USBOTGSS_IRQ1_CHRGVBUS_FALL)
178 dev_dbg(omap->dev, "CHRGVBUS Fall\n");
179
180 if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_FALL)
181 dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
182
183 if (reg & USBOTGSS_IRQ1_IDPULLUP_FALL)
184 dev_dbg(omap->dev, "IDPULLUP Fall\n");
185
186 dwc3_writel(omap->base, USBOTGSS_IRQSTATUS_1, reg);
187
188 reg = dwc3_readl(omap->base, USBOTGSS_IRQSTATUS_0);
189 dwc3_writel(omap->base, USBOTGSS_IRQSTATUS_0, reg);
190
191 spin_unlock(&omap->lock);
192
193 return IRQ_HANDLED;
194}
195
196static int __devinit dwc3_omap_probe(struct platform_device *pdev)
197{
198 struct dwc3_omap_data *pdata = pdev->dev.platform_data;
199 struct platform_device *dwc3;
200 struct dwc3_omap *omap;
201 struct resource *res;
202
203 int ret = -ENOMEM;
204 int irq;
205
206 u32 reg;
207
208 void __iomem *base;
209 void *context;
210
211 omap = kzalloc(sizeof(*omap), GFP_KERNEL);
212 if (!omap) {
213 dev_err(&pdev->dev, "not enough memory\n");
214 goto err0;
215 }
216
217 platform_set_drvdata(pdev, omap);
218
219 irq = platform_get_irq(pdev, 1);
220 if (irq < 0) {
221 dev_err(&pdev->dev, "missing IRQ resource\n");
222 ret = -EINVAL;
223 goto err1;
224 }
225
226 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
227 if (!res) {
228 dev_err(&pdev->dev, "missing memory base resource\n");
229 ret = -EINVAL;
230 goto err1;
231 }
232
233 base = ioremap_nocache(res->start, resource_size(res));
234 if (!base) {
235 dev_err(&pdev->dev, "ioremap failed\n");
236 goto err1;
237 }
238
239 dwc3 = platform_device_alloc("dwc3-omap", -1);
240 if (!dwc3) {
241 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
242 goto err2;
243 }
244
245 context = kzalloc(resource_size(res), GFP_KERNEL);
246 if (!context) {
247 dev_err(&pdev->dev, "couldn't allocate dwc3 context memory\n");
248 goto err3;
249 }
250
251 spin_lock_init(&omap->lock);
252 dma_set_coherent_mask(&dwc3->dev, pdev->dev.coherent_dma_mask);
253
254 dwc3->dev.parent = &pdev->dev;
255 dwc3->dev.dma_mask = pdev->dev.dma_mask;
256 dwc3->dev.dma_parms = pdev->dev.dma_parms;
257 omap->resource_size = resource_size(res);
258 omap->context = context;
259 omap->dev = &pdev->dev;
260 omap->irq = irq;
261 omap->base = base;
262 omap->dwc3 = dwc3;
263
264 reg = dwc3_readl(omap->base, USBOTGSS_UTMI_OTG_STATUS);
265
266 if (!pdata) {
267 dev_dbg(&pdev->dev, "missing platform data\n");
268 } else {
269 switch (pdata->utmi_mode) {
270 case DWC3_OMAP_UTMI_MODE_SW:
271 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
272 break;
273 case DWC3_OMAP_UTMI_MODE_HW:
274 reg &= ~USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
275 break;
276 default:
277 dev_dbg(&pdev->dev, "UNKNOWN utmi mode %d\n",
278 pdata->utmi_mode);
279 }
280 }
281
282 dwc3_writel(omap->base, USBOTGSS_UTMI_OTG_STATUS, reg);
283
284 /* check the DMA Status */
285 reg = dwc3_readl(omap->base, USBOTGSS_SYSCONFIG);
286 omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
287
288 /* Set No-Idle and No-Standby */
289 reg &= ~(USBOTGSS_STANDBYMODE_MASK
290 | USBOTGSS_IDLEMODE_MASK);
291
292 reg |= (USBOTGSS_SYSCONFIG_STANDBYMODE(USBOTGSS_STANDBYMODE_NO_STANDBY)
293 | USBOTGSS_SYSCONFIG_IDLEMODE(USBOTGSS_IDLEMODE_NO_IDLE));
294
295 dwc3_writel(omap->base, USBOTGSS_SYSCONFIG, reg);
296
297 ret = request_irq(omap->irq, dwc3_omap_interrupt, 0,
298 "dwc3-omap", omap);
299 if (ret) {
300 dev_err(&pdev->dev, "failed to request IRQ #%d --> %d\n",
301 omap->irq, ret);
302 goto err4;
303 }
304
305 /* enable all IRQs */
306 reg = USBOTGSS_IRQO_COREIRQ_ST;
307 dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_0, reg);
308
309 reg = (USBOTGSS_IRQ1_OEVT |
310 USBOTGSS_IRQ1_DRVVBUS_RISE |
311 USBOTGSS_IRQ1_CHRGVBUS_RISE |
312 USBOTGSS_IRQ1_DISCHRGVBUS_RISE |
313 USBOTGSS_IRQ1_IDPULLUP_RISE |
314 USBOTGSS_IRQ1_DRVVBUS_FALL |
315 USBOTGSS_IRQ1_CHRGVBUS_FALL |
316 USBOTGSS_IRQ1_DISCHRGVBUS_FALL |
317 USBOTGSS_IRQ1_IDPULLUP_FALL);
318
319 dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_1, reg);
320
321 ret = platform_device_add_resources(dwc3, pdev->resource,
322 pdev->num_resources);
323 if (ret) {
324 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
325 goto err5;
326 }
327
328 ret = platform_device_add(dwc3);
329 if (ret) {
330 dev_err(&pdev->dev, "failed to register dwc3 device\n");
331 goto err5;
332 }
333
334 return 0;
335
336err5:
337 free_irq(omap->irq, omap);
338
339err4:
340 kfree(omap->context);
341
342err3:
343 platform_device_put(dwc3);
344
345err2:
346 iounmap(base);
347
348err1:
349 kfree(omap);
350
351err0:
352 return ret;
353}
354
355static int __devexit dwc3_omap_remove(struct platform_device *pdev)
356{
357 struct dwc3_omap *omap = platform_get_drvdata(pdev);
358
359 platform_device_unregister(omap->dwc3);
360
361 free_irq(omap->irq, omap);
362 iounmap(omap->base);
363
364 kfree(omap->context);
365 kfree(omap);
366
367 return 0;
368}
369
370static const struct of_device_id of_dwc3_matach[] = {
371 {
372 "ti,dwc3",
373 },
374 { },
375};
376MODULE_DEVICE_TABLE(of, of_dwc3_matach);
377
378static struct platform_driver dwc3_omap_driver = {
379 .probe = dwc3_omap_probe,
380 .remove = __devexit_p(dwc3_omap_remove),
381 .driver = {
382 .name = "omap-dwc3",
383 .of_match_table = of_dwc3_matach,
384 },
385};
386
387MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
388MODULE_LICENSE("Dual BSD/GPL");
389MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");
390
391static int __devinit dwc3_omap_init(void)
392{
393 return platform_driver_register(&dwc3_omap_driver);
394}
395module_init(dwc3_omap_init);
396
397static void __exit dwc3_omap_exit(void)
398{
399 platform_driver_unregister(&dwc3_omap_driver);
400}
401module_exit(dwc3_omap_exit);
diff --git a/drivers/usb/dwc3/dwc3-pci.c b/drivers/usb/dwc3/dwc3-pci.c
new file mode 100644
index 000000000000..f77c00042685
--- /dev/null
+++ b/drivers/usb/dwc3/dwc3-pci.c
@@ -0,0 +1,219 @@
1/**
2 * dwc3-pci.c - PCI Specific glue layer
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/pci.h>
43#include <linux/platform_device.h>
44
45/* FIXME define these in <linux/pci_ids.h> */
46#define PCI_VENDOR_ID_SYNOPSYS 0x16c3
47#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
48
49#define DWC3_PCI_DEVS_POSSIBLE 32
50
51struct dwc3_pci {
52 struct device *dev;
53 struct platform_device *dwc3;
54};
55
56static DECLARE_BITMAP(dwc3_pci_devs, DWC3_PCI_DEVS_POSSIBLE);
57
58static int dwc3_pci_get_device_id(struct dwc3_pci *glue)
59{
60 int id;
61
62again:
63 id = find_first_zero_bit(dwc3_pci_devs, DWC3_PCI_DEVS_POSSIBLE);
64 if (id < DWC3_PCI_DEVS_POSSIBLE) {
65 int old;
66
67 old = test_and_set_bit(id, dwc3_pci_devs);
68 if (old)
69 goto again;
70 } else {
71 dev_err(glue->dev, "no space for new device\n");
72 id = -ENOMEM;
73 }
74
75 return 0;
76}
77
78static void dwc3_pci_put_device_id(struct dwc3_pci *glue, int id)
79{
80 int ret;
81
82 if (id < 0)
83 return;
84
85 ret = test_bit(id, dwc3_pci_devs);
86 WARN(!ret, "Device: %s\nID %d not in use\n",
87 dev_driver_string(glue->dev), id);
88 clear_bit(id, dwc3_pci_devs);
89}
90
91static int __devinit dwc3_pci_probe(struct pci_dev *pci,
92 const struct pci_device_id *id)
93{
94 struct resource res[2];
95 struct platform_device *dwc3;
96 struct dwc3_pci *glue;
97 int ret = -ENOMEM;
98 int devid;
99
100 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
101 if (!glue) {
102 dev_err(&pci->dev, "not enough memory\n");
103 goto err0;
104 }
105
106 glue->dev = &pci->dev;
107
108 ret = pci_enable_device(pci);
109 if (ret) {
110 dev_err(&pci->dev, "failed to enable pci device\n");
111 goto err1;
112 }
113
114 pci_set_power_state(pci, PCI_D0);
115 pci_set_master(pci);
116
117 devid = dwc3_pci_get_device_id(glue);
118 if (devid < 0)
119 goto err2;
120
121 dwc3 = platform_device_alloc("dwc3-pci", devid);
122 if (!dwc3) {
123 dev_err(&pci->dev, "couldn't allocate dwc3 device\n");
124 goto err3;
125 }
126
127 memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
128
129 res[0].start = pci_resource_start(pci, 0);
130 res[0].end = pci_resource_end(pci, 0);
131 res[0].name = "dwc_usb3";
132 res[0].flags = IORESOURCE_MEM;
133
134 res[1].start = pci->irq;
135 res[1].name = "dwc_usb3";
136 res[1].flags = IORESOURCE_IRQ;
137
138 ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
139 if (ret) {
140 dev_err(&pci->dev, "couldn't add resources to dwc3 device\n");
141 goto err4;
142 }
143
144 pci_set_drvdata(pci, glue);
145
146 dma_set_coherent_mask(&dwc3->dev, pci->dev.coherent_dma_mask);
147
148 dwc3->dev.dma_mask = pci->dev.dma_mask;
149 dwc3->dev.dma_parms = pci->dev.dma_parms;
150 dwc3->dev.parent = &pci->dev;
151 glue->dwc3 = dwc3;
152
153 ret = platform_device_add(dwc3);
154 if (ret) {
155 dev_err(&pci->dev, "failed to register dwc3 device\n");
156 goto err4;
157 }
158
159 return 0;
160
161err4:
162 pci_set_drvdata(pci, NULL);
163 platform_device_put(dwc3);
164
165err3:
166 dwc3_pci_put_device_id(glue, devid);
167
168err2:
169 pci_disable_device(pci);
170
171err1:
172 kfree(pci);
173
174err0:
175 return ret;
176}
177
178static void __devexit dwc3_pci_remove(struct pci_dev *pci)
179{
180 struct dwc3_pci *glue = pci_get_drvdata(pci);
181
182 dwc3_pci_put_device_id(glue, glue->dwc3->id);
183 platform_device_unregister(glue->dwc3);
184 pci_set_drvdata(pci, NULL);
185 pci_disable_device(pci);
186 kfree(glue);
187}
188
189static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
190 {
191 PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
192 PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
193 },
194 { } /* Terminating Entry */
195};
196MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
197
198static struct pci_driver dwc3_pci_driver = {
199 .name = "pci-dwc3",
200 .id_table = dwc3_pci_id_table,
201 .probe = dwc3_pci_probe,
202 .remove = __devexit_p(dwc3_pci_remove),
203};
204
205MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
206MODULE_LICENSE("Dual BSD/GPL");
207MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
208
209static int __devinit dwc3_pci_init(void)
210{
211 return pci_register_driver(&dwc3_pci_driver);
212}
213module_init(dwc3_pci_init);
214
215static void __exit dwc3_pci_exit(void)
216{
217 pci_unregister_driver(&dwc3_pci_driver);
218}
219module_exit(dwc3_pci_exit);
diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c
new file mode 100644
index 000000000000..69a4e43ddf59
--- /dev/null
+++ b/drivers/usb/dwc3/ep0.c
@@ -0,0 +1,804 @@
1/**
2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/platform_device.h>
43#include <linux/pm_runtime.h>
44#include <linux/interrupt.h>
45#include <linux/io.h>
46#include <linux/list.h>
47#include <linux/dma-mapping.h>
48
49#include <linux/usb/ch9.h>
50#include <linux/usb/gadget.h>
51
52#include "core.h"
53#include "gadget.h"
54#include "io.h"
55
56static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
57 const struct dwc3_event_depevt *event);
58
59static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
60{
61 switch (state) {
62 case EP0_UNCONNECTED:
63 return "Unconnected";
64 case EP0_SETUP_PHASE:
65 return "Setup Phase";
66 case EP0_DATA_PHASE:
67 return "Data Phase";
68 case EP0_STATUS_PHASE:
69 return "Status Phase";
70 default:
71 return "UNKNOWN";
72 }
73}
74
75static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
76 u32 len, u32 type)
77{
78 struct dwc3_gadget_ep_cmd_params params;
79 struct dwc3_trb_hw *trb_hw;
80 struct dwc3_trb trb;
81 struct dwc3_ep *dep;
82
83 int ret;
84
85 dep = dwc->eps[epnum];
86 if (dep->flags & DWC3_EP_BUSY) {
87 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
88 return 0;
89 }
90
91 trb_hw = dwc->ep0_trb;
92 memset(&trb, 0, sizeof(trb));
93
94 trb.trbctl = type;
95 trb.bplh = buf_dma;
96 trb.length = len;
97
98 trb.hwo = 1;
99 trb.lst = 1;
100 trb.ioc = 1;
101 trb.isp_imi = 1;
102
103 dwc3_trb_to_hw(&trb, trb_hw);
104
105 memset(&params, 0, sizeof(params));
106 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
107 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
108
109 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
110 DWC3_DEPCMD_STARTTRANSFER, &params);
111 if (ret < 0) {
112 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
113 return ret;
114 }
115
116 dep->flags |= DWC3_EP_BUSY;
117 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
118 dep->number);
119
120 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
121
122 return 0;
123}
124
125static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
126 struct dwc3_request *req)
127{
128 int ret = 0;
129
130 req->request.actual = 0;
131 req->request.status = -EINPROGRESS;
132 req->epnum = dep->number;
133
134 list_add_tail(&req->list, &dep->request_list);
135
136 /*
137 * Gadget driver might not be quick enough to queue a request
138 * before we get a Transfer Not Ready event on this endpoint.
139 *
140 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
141 * flag is set, it's telling us that as soon as Gadget queues the
142 * required request, we should kick the transfer here because the
143 * IRQ we were waiting for is long gone.
144 */
145 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
146 struct dwc3 *dwc = dep->dwc;
147 unsigned direction;
148 u32 type;
149
150 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
151
152 if (dwc->ep0state == EP0_STATUS_PHASE) {
153 type = dwc->three_stage_setup
154 ? DWC3_TRBCTL_CONTROL_STATUS3
155 : DWC3_TRBCTL_CONTROL_STATUS2;
156 } else if (dwc->ep0state == EP0_DATA_PHASE) {
157 type = DWC3_TRBCTL_CONTROL_DATA;
158 } else {
159 /* should never happen */
160 WARN_ON(1);
161 return 0;
162 }
163
164 ret = dwc3_ep0_start_trans(dwc, direction,
165 req->request.dma, req->request.length, type);
166 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
167 DWC3_EP0_DIR_IN);
168 }
169
170 return ret;
171}
172
173int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
174 gfp_t gfp_flags)
175{
176 struct dwc3_request *req = to_dwc3_request(request);
177 struct dwc3_ep *dep = to_dwc3_ep(ep);
178 struct dwc3 *dwc = dep->dwc;
179
180 unsigned long flags;
181
182 int ret;
183
184 spin_lock_irqsave(&dwc->lock, flags);
185 if (!dep->desc) {
186 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
187 request, dep->name);
188 ret = -ESHUTDOWN;
189 goto out;
190 }
191
192 /* we share one TRB for ep0/1 */
193 if (!list_empty(&dwc->eps[0]->request_list) ||
194 !list_empty(&dwc->eps[1]->request_list) ||
195 dwc->ep0_status_pending) {
196 ret = -EBUSY;
197 goto out;
198 }
199
200 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
201 request, dep->name, request->length,
202 dwc3_ep0_state_string(dwc->ep0state));
203
204 ret = __dwc3_gadget_ep0_queue(dep, req);
205
206out:
207 spin_unlock_irqrestore(&dwc->lock, flags);
208
209 return ret;
210}
211
212static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
213{
214 struct dwc3_ep *dep = dwc->eps[0];
215
216 /* stall is always issued on EP0 */
217 __dwc3_gadget_ep_set_halt(dwc->eps[0], 1);
218 dwc->eps[0]->flags = DWC3_EP_ENABLED;
219
220 if (!list_empty(&dep->request_list)) {
221 struct dwc3_request *req;
222
223 req = next_request(&dep->request_list);
224 dwc3_gadget_giveback(dep, req, -ECONNRESET);
225 }
226
227 dwc->ep0state = EP0_SETUP_PHASE;
228 dwc3_ep0_out_start(dwc);
229}
230
231void dwc3_ep0_out_start(struct dwc3 *dwc)
232{
233 int ret;
234
235 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
236 DWC3_TRBCTL_CONTROL_SETUP);
237 WARN_ON(ret < 0);
238}
239
240static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
241{
242 struct dwc3_ep *dep;
243 u32 windex = le16_to_cpu(wIndex_le);
244 u32 epnum;
245
246 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
247 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
248 epnum |= 1;
249
250 dep = dwc->eps[epnum];
251 if (dep->flags & DWC3_EP_ENABLED)
252 return dep;
253
254 return NULL;
255}
256
257static void dwc3_ep0_send_status_response(struct dwc3 *dwc)
258{
259 dwc3_ep0_start_trans(dwc, 1, dwc->setup_buf_addr,
260 dwc->ep0_usb_req.length,
261 DWC3_TRBCTL_CONTROL_DATA);
262}
263
264/*
265 * ch 9.4.5
266 */
267static int dwc3_ep0_handle_status(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
268{
269 struct dwc3_ep *dep;
270 u32 recip;
271 u16 usb_status = 0;
272 __le16 *response_pkt;
273
274 recip = ctrl->bRequestType & USB_RECIP_MASK;
275 switch (recip) {
276 case USB_RECIP_DEVICE:
277 /*
278 * We are self-powered. U1/U2/LTM will be set later
279 * once we handle this states. RemoteWakeup is 0 on SS
280 */
281 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
282 break;
283
284 case USB_RECIP_INTERFACE:
285 /*
286 * Function Remote Wake Capable D0
287 * Function Remote Wakeup D1
288 */
289 break;
290
291 case USB_RECIP_ENDPOINT:
292 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
293 if (!dep)
294 return -EINVAL;
295
296 if (dep->flags & DWC3_EP_STALL)
297 usb_status = 1 << USB_ENDPOINT_HALT;
298 break;
299 default:
300 return -EINVAL;
301 };
302
303 response_pkt = (__le16 *) dwc->setup_buf;
304 *response_pkt = cpu_to_le16(usb_status);
305 dwc->ep0_usb_req.length = sizeof(*response_pkt);
306 dwc->ep0_status_pending = 1;
307
308 return 0;
309}
310
311static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
312 struct usb_ctrlrequest *ctrl, int set)
313{
314 struct dwc3_ep *dep;
315 u32 recip;
316 u32 wValue;
317 u32 wIndex;
318 u32 reg;
319 int ret;
320 u32 mode;
321
322 wValue = le16_to_cpu(ctrl->wValue);
323 wIndex = le16_to_cpu(ctrl->wIndex);
324 recip = ctrl->bRequestType & USB_RECIP_MASK;
325 switch (recip) {
326 case USB_RECIP_DEVICE:
327
328 /*
329 * 9.4.1 says only only for SS, in AddressState only for
330 * default control pipe
331 */
332 switch (wValue) {
333 case USB_DEVICE_U1_ENABLE:
334 case USB_DEVICE_U2_ENABLE:
335 case USB_DEVICE_LTM_ENABLE:
336 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
337 return -EINVAL;
338 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
339 return -EINVAL;
340 }
341
342 /* XXX add U[12] & LTM */
343 switch (wValue) {
344 case USB_DEVICE_REMOTE_WAKEUP:
345 break;
346 case USB_DEVICE_U1_ENABLE:
347 break;
348 case USB_DEVICE_U2_ENABLE:
349 break;
350 case USB_DEVICE_LTM_ENABLE:
351 break;
352
353 case USB_DEVICE_TEST_MODE:
354 if ((wIndex & 0xff) != 0)
355 return -EINVAL;
356 if (!set)
357 return -EINVAL;
358
359 mode = wIndex >> 8;
360 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
361 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
362
363 switch (mode) {
364 case TEST_J:
365 case TEST_K:
366 case TEST_SE0_NAK:
367 case TEST_PACKET:
368 case TEST_FORCE_EN:
369 reg |= mode << 1;
370 break;
371 default:
372 return -EINVAL;
373 }
374 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
375 break;
376 default:
377 return -EINVAL;
378 }
379 break;
380
381 case USB_RECIP_INTERFACE:
382 switch (wValue) {
383 case USB_INTRF_FUNC_SUSPEND:
384 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
385 /* XXX enable Low power suspend */
386 ;
387 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
388 /* XXX enable remote wakeup */
389 ;
390 break;
391 default:
392 return -EINVAL;
393 }
394 break;
395
396 case USB_RECIP_ENDPOINT:
397 switch (wValue) {
398 case USB_ENDPOINT_HALT:
399
400 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
401 if (!dep)
402 return -EINVAL;
403 ret = __dwc3_gadget_ep_set_halt(dep, set);
404 if (ret)
405 return -EINVAL;
406 break;
407 default:
408 return -EINVAL;
409 }
410 break;
411
412 default:
413 return -EINVAL;
414 };
415
416 return 0;
417}
418
419static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
420{
421 u32 addr;
422 u32 reg;
423
424 addr = le16_to_cpu(ctrl->wValue);
425 if (addr > 127)
426 return -EINVAL;
427
428 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
429 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
430 reg |= DWC3_DCFG_DEVADDR(addr);
431 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
432
433 if (addr)
434 dwc->dev_state = DWC3_ADDRESS_STATE;
435 else
436 dwc->dev_state = DWC3_DEFAULT_STATE;
437
438 return 0;
439}
440
441static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
442{
443 int ret;
444
445 spin_unlock(&dwc->lock);
446 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
447 spin_lock(&dwc->lock);
448 return ret;
449}
450
451static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
452{
453 u32 cfg;
454 int ret;
455
456 dwc->start_config_issued = false;
457 cfg = le16_to_cpu(ctrl->wValue);
458
459 switch (dwc->dev_state) {
460 case DWC3_DEFAULT_STATE:
461 return -EINVAL;
462 break;
463
464 case DWC3_ADDRESS_STATE:
465 ret = dwc3_ep0_delegate_req(dwc, ctrl);
466 /* if the cfg matches and the cfg is non zero */
467 if (!ret && cfg)
468 dwc->dev_state = DWC3_CONFIGURED_STATE;
469 break;
470
471 case DWC3_CONFIGURED_STATE:
472 ret = dwc3_ep0_delegate_req(dwc, ctrl);
473 if (!cfg)
474 dwc->dev_state = DWC3_ADDRESS_STATE;
475 break;
476 }
477 return 0;
478}
479
480static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
481{
482 int ret;
483
484 switch (ctrl->bRequest) {
485 case USB_REQ_GET_STATUS:
486 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
487 ret = dwc3_ep0_handle_status(dwc, ctrl);
488 break;
489 case USB_REQ_CLEAR_FEATURE:
490 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
491 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
492 break;
493 case USB_REQ_SET_FEATURE:
494 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
495 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
496 break;
497 case USB_REQ_SET_ADDRESS:
498 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
499 ret = dwc3_ep0_set_address(dwc, ctrl);
500 break;
501 case USB_REQ_SET_CONFIGURATION:
502 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
503 ret = dwc3_ep0_set_config(dwc, ctrl);
504 break;
505 default:
506 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
507 ret = dwc3_ep0_delegate_req(dwc, ctrl);
508 break;
509 };
510
511 return ret;
512}
513
514static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
515 const struct dwc3_event_depevt *event)
516{
517 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
518 int ret;
519 u32 len;
520
521 if (!dwc->gadget_driver)
522 goto err;
523
524 len = le16_to_cpu(ctrl->wLength);
525 if (!len) {
526 dwc->three_stage_setup = false;
527 dwc->ep0_expect_in = false;
528 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
529 } else {
530 dwc->three_stage_setup = true;
531 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
532 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
533 }
534
535 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
536 ret = dwc3_ep0_std_request(dwc, ctrl);
537 else
538 ret = dwc3_ep0_delegate_req(dwc, ctrl);
539
540 if (ret >= 0)
541 return;
542
543err:
544 dwc3_ep0_stall_and_restart(dwc);
545}
546
547static void dwc3_ep0_complete_data(struct dwc3 *dwc,
548 const struct dwc3_event_depevt *event)
549{
550 struct dwc3_request *r = NULL;
551 struct usb_request *ur;
552 struct dwc3_trb trb;
553 struct dwc3_ep *dep;
554 u32 transferred;
555 u8 epnum;
556
557 epnum = event->endpoint_number;
558 dep = dwc->eps[epnum];
559
560 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
561
562 if (!dwc->ep0_status_pending) {
563 r = next_request(&dwc->eps[0]->request_list);
564 ur = &r->request;
565 } else {
566 ur = &dwc->ep0_usb_req;
567 dwc->ep0_status_pending = 0;
568 }
569
570 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
571
572 if (dwc->ep0_bounced) {
573 struct dwc3_ep *ep0 = dwc->eps[0];
574
575 transferred = min_t(u32, ur->length,
576 ep0->endpoint.maxpacket - trb.length);
577 memcpy(ur->buf, dwc->ep0_bounce, transferred);
578 dwc->ep0_bounced = false;
579 } else {
580 transferred = ur->length - trb.length;
581 ur->actual += transferred;
582 }
583
584 if ((epnum & 1) && ur->actual < ur->length) {
585 /* for some reason we did not get everything out */
586
587 dwc3_ep0_stall_and_restart(dwc);
588 } else {
589 /*
590 * handle the case where we have to send a zero packet. This
591 * seems to be case when req.length > maxpacket. Could it be?
592 */
593 if (r)
594 dwc3_gadget_giveback(dep, r, 0);
595 }
596}
597
598static void dwc3_ep0_complete_req(struct dwc3 *dwc,
599 const struct dwc3_event_depevt *event)
600{
601 struct dwc3_request *r;
602 struct dwc3_ep *dep;
603
604 dep = dwc->eps[0];
605
606 if (!list_empty(&dep->request_list)) {
607 r = next_request(&dep->request_list);
608
609 dwc3_gadget_giveback(dep, r, 0);
610 }
611
612 dwc->ep0state = EP0_SETUP_PHASE;
613 dwc3_ep0_out_start(dwc);
614}
615
616static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
617 const struct dwc3_event_depevt *event)
618{
619 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
620
621 dep->flags &= ~DWC3_EP_BUSY;
622
623 switch (dwc->ep0state) {
624 case EP0_SETUP_PHASE:
625 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
626 dwc3_ep0_inspect_setup(dwc, event);
627 break;
628
629 case EP0_DATA_PHASE:
630 dev_vdbg(dwc->dev, "Data Phase\n");
631 dwc3_ep0_complete_data(dwc, event);
632 break;
633
634 case EP0_STATUS_PHASE:
635 dev_vdbg(dwc->dev, "Status Phase\n");
636 dwc3_ep0_complete_req(dwc, event);
637 break;
638 default:
639 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
640 }
641}
642
643static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
644 const struct dwc3_event_depevt *event)
645{
646 dwc->ep0state = EP0_SETUP_PHASE;
647 dwc3_ep0_out_start(dwc);
648}
649
650static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
651 const struct dwc3_event_depevt *event)
652{
653 struct dwc3_ep *dep;
654 struct dwc3_request *req;
655 int ret;
656
657 dep = dwc->eps[0];
658 dwc->ep0state = EP0_DATA_PHASE;
659
660 if (dwc->ep0_status_pending) {
661 dwc3_ep0_send_status_response(dwc);
662 return;
663 }
664
665 if (list_empty(&dep->request_list)) {
666 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
667 dep->flags |= DWC3_EP_PENDING_REQUEST;
668
669 if (event->endpoint_number)
670 dep->flags |= DWC3_EP0_DIR_IN;
671 return;
672 }
673
674 req = next_request(&dep->request_list);
675 req->direction = !!event->endpoint_number;
676
677 dwc->ep0state = EP0_DATA_PHASE;
678 if (req->request.length == 0) {
679 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
680 dwc->ctrl_req_addr, 0,
681 DWC3_TRBCTL_CONTROL_DATA);
682 } else if ((req->request.length % dep->endpoint.maxpacket)
683 && (event->endpoint_number == 0)) {
684 dwc3_map_buffer_to_dma(req);
685
686 WARN_ON(req->request.length > dep->endpoint.maxpacket);
687
688 dwc->ep0_bounced = true;
689
690 /*
691 * REVISIT in case request length is bigger than EP0
692 * wMaxPacketSize, we will need two chained TRBs to handle
693 * the transfer.
694 */
695 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
696 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
697 DWC3_TRBCTL_CONTROL_DATA);
698 } else {
699 dwc3_map_buffer_to_dma(req);
700
701 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
702 req->request.dma, req->request.length,
703 DWC3_TRBCTL_CONTROL_DATA);
704 }
705
706 WARN_ON(ret < 0);
707}
708
709static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
710 const struct dwc3_event_depevt *event)
711{
712 u32 type;
713 int ret;
714
715 dwc->ep0state = EP0_STATUS_PHASE;
716
717 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
718 : DWC3_TRBCTL_CONTROL_STATUS2;
719
720 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
721 dwc->ctrl_req_addr, 0, type);
722
723 WARN_ON(ret < 0);
724}
725
726static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
727 const struct dwc3_event_depevt *event)
728{
729 switch (event->status) {
730 case DEPEVT_STATUS_CONTROL_SETUP:
731 dev_vdbg(dwc->dev, "Control Setup\n");
732 dwc3_ep0_do_control_setup(dwc, event);
733 break;
734
735 case DEPEVT_STATUS_CONTROL_DATA:
736 dev_vdbg(dwc->dev, "Control Data\n");
737
738 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
739 dev_vdbg(dwc->dev, "Expected %d got %d\n",
740 dwc->ep0_next_event,
741 DWC3_EP0_NRDY_DATA);
742
743 dwc3_ep0_stall_and_restart(dwc);
744 return;
745 }
746
747 /*
748 * One of the possible error cases is when Host _does_
749 * request for Data Phase, but it does so on the wrong
750 * direction.
751 *
752 * Here, we already know ep0_next_event is DATA (see above),
753 * so we only need to check for direction.
754 */
755 if (dwc->ep0_expect_in != event->endpoint_number) {
756 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
757 dwc3_ep0_stall_and_restart(dwc);
758 return;
759 }
760
761 dwc3_ep0_do_control_data(dwc, event);
762 break;
763
764 case DEPEVT_STATUS_CONTROL_STATUS:
765 dev_vdbg(dwc->dev, "Control Status\n");
766
767 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
768 dev_vdbg(dwc->dev, "Expected %d got %d\n",
769 dwc->ep0_next_event,
770 DWC3_EP0_NRDY_STATUS);
771
772 dwc3_ep0_stall_and_restart(dwc);
773 return;
774 }
775 dwc3_ep0_do_control_status(dwc, event);
776 }
777}
778
779void dwc3_ep0_interrupt(struct dwc3 *dwc,
780 const const struct dwc3_event_depevt *event)
781{
782 u8 epnum = event->endpoint_number;
783
784 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
785 dwc3_ep_event_string(event->endpoint_event),
786 epnum >> 1, (epnum & 1) ? "in" : "out",
787 dwc3_ep0_state_string(dwc->ep0state));
788
789 switch (event->endpoint_event) {
790 case DWC3_DEPEVT_XFERCOMPLETE:
791 dwc3_ep0_xfer_complete(dwc, event);
792 break;
793
794 case DWC3_DEPEVT_XFERNOTREADY:
795 dwc3_ep0_xfernotready(dwc, event);
796 break;
797
798 case DWC3_DEPEVT_XFERINPROGRESS:
799 case DWC3_DEPEVT_RXTXFIFOEVT:
800 case DWC3_DEPEVT_STREAMEVT:
801 case DWC3_DEPEVT_EPCMDCMPLT:
802 break;
803 }
804}
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
new file mode 100644
index 000000000000..25dbd8614e72
--- /dev/null
+++ b/drivers/usb/dwc3/gadget.c
@@ -0,0 +1,2105 @@
1/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
52
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
56
57#define DMA_ADDR_INVALID (~(dma_addr_t)0)
58
59void dwc3_map_buffer_to_dma(struct dwc3_request *req)
60{
61 struct dwc3 *dwc = req->dep->dwc;
62
63 if (req->request.length == 0) {
64 /* req->request.dma = dwc->setup_buf_addr; */
65 return;
66 }
67
68 if (req->request.dma == DMA_ADDR_INVALID) {
69 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
70 req->request.length, req->direction
71 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
72 req->mapped = true;
73 }
74}
75
76void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
77{
78 struct dwc3 *dwc = req->dep->dwc;
79
80 if (req->request.length == 0) {
81 req->request.dma = DMA_ADDR_INVALID;
82 return;
83 }
84
85 if (req->mapped) {
86 dma_unmap_single(dwc->dev, req->request.dma,
87 req->request.length, req->direction
88 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
89 req->mapped = 0;
90 req->request.dma = DMA_ADDR_INVALID;
91 }
92}
93
94void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
95 int status)
96{
97 struct dwc3 *dwc = dep->dwc;
98
99 if (req->queued) {
100 dep->busy_slot++;
101 /*
102 * Skip LINK TRB. We can't use req->trb and check for
103 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
104 * completed (not the LINK TRB).
105 */
106 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
107 usb_endpoint_xfer_isoc(dep->desc))
108 dep->busy_slot++;
109 }
110 list_del(&req->list);
111
112 if (req->request.status == -EINPROGRESS)
113 req->request.status = status;
114
115 dwc3_unmap_buffer_from_dma(req);
116
117 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
118 req, dep->name, req->request.actual,
119 req->request.length, status);
120
121 spin_unlock(&dwc->lock);
122 req->request.complete(&req->dep->endpoint, &req->request);
123 spin_lock(&dwc->lock);
124}
125
126static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
127{
128 switch (cmd) {
129 case DWC3_DEPCMD_DEPSTARTCFG:
130 return "Start New Configuration";
131 case DWC3_DEPCMD_ENDTRANSFER:
132 return "End Transfer";
133 case DWC3_DEPCMD_UPDATETRANSFER:
134 return "Update Transfer";
135 case DWC3_DEPCMD_STARTTRANSFER:
136 return "Start Transfer";
137 case DWC3_DEPCMD_CLEARSTALL:
138 return "Clear Stall";
139 case DWC3_DEPCMD_SETSTALL:
140 return "Set Stall";
141 case DWC3_DEPCMD_GETSEQNUMBER:
142 return "Get Data Sequence Number";
143 case DWC3_DEPCMD_SETTRANSFRESOURCE:
144 return "Set Endpoint Transfer Resource";
145 case DWC3_DEPCMD_SETEPCONFIG:
146 return "Set Endpoint Configuration";
147 default:
148 return "UNKNOWN command";
149 }
150}
151
152int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
153 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
154{
155 struct dwc3_ep *dep = dwc->eps[ep];
156 u32 timeout = 500;
157 u32 reg;
158
159 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
160 dep->name,
161 dwc3_gadget_ep_cmd_string(cmd), params->param0,
162 params->param1, params->param2);
163
164 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
165 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
166 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
167
168 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
169 do {
170 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
171 if (!(reg & DWC3_DEPCMD_CMDACT)) {
172 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
173 DWC3_DEPCMD_STATUS(reg));
174 return 0;
175 }
176
177 /*
178 * We can't sleep here, because it is also called from
179 * interrupt context.
180 */
181 timeout--;
182 if (!timeout)
183 return -ETIMEDOUT;
184
185 udelay(1);
186 } while (1);
187}
188
189static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
190 struct dwc3_trb_hw *trb)
191{
192 u32 offset = (char *) trb - (char *) dep->trb_pool;
193
194 return dep->trb_pool_dma + offset;
195}
196
197static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
198{
199 struct dwc3 *dwc = dep->dwc;
200
201 if (dep->trb_pool)
202 return 0;
203
204 if (dep->number == 0 || dep->number == 1)
205 return 0;
206
207 dep->trb_pool = dma_alloc_coherent(dwc->dev,
208 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
209 &dep->trb_pool_dma, GFP_KERNEL);
210 if (!dep->trb_pool) {
211 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
212 dep->name);
213 return -ENOMEM;
214 }
215
216 return 0;
217}
218
219static void dwc3_free_trb_pool(struct dwc3_ep *dep)
220{
221 struct dwc3 *dwc = dep->dwc;
222
223 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
224 dep->trb_pool, dep->trb_pool_dma);
225
226 dep->trb_pool = NULL;
227 dep->trb_pool_dma = 0;
228}
229
230static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
231{
232 struct dwc3_gadget_ep_cmd_params params;
233 u32 cmd;
234
235 memset(&params, 0x00, sizeof(params));
236
237 if (dep->number != 1) {
238 cmd = DWC3_DEPCMD_DEPSTARTCFG;
239 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
240 if (dep->number > 1) {
241 if (dwc->start_config_issued)
242 return 0;
243 dwc->start_config_issued = true;
244 cmd |= DWC3_DEPCMD_PARAM(2);
245 }
246
247 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
248 }
249
250 return 0;
251}
252
253static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
254 const struct usb_endpoint_descriptor *desc)
255{
256 struct dwc3_gadget_ep_cmd_params params;
257
258 memset(&params, 0x00, sizeof(params));
259
260 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
261 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
262 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
263
264 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
265 | DWC3_DEPCFG_XFER_NOT_READY_EN;
266
267 if (usb_endpoint_xfer_bulk(desc) && dep->endpoint.max_streams) {
268 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
269 | DWC3_DEPCFG_STREAM_EVENT_EN;
270 dep->stream_capable = true;
271 }
272
273 if (usb_endpoint_xfer_isoc(desc))
274 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
275
276 /*
277 * We are doing 1:1 mapping for endpoints, meaning
278 * Physical Endpoints 2 maps to Logical Endpoint 2 and
279 * so on. We consider the direction bit as part of the physical
280 * endpoint number. So USB endpoint 0x81 is 0x03.
281 */
282 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
283
284 /*
285 * We must use the lower 16 TX FIFOs even though
286 * HW might have more
287 */
288 if (dep->direction)
289 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
290
291 if (desc->bInterval) {
292 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
293 dep->interval = 1 << (desc->bInterval - 1);
294 }
295
296 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
297 DWC3_DEPCMD_SETEPCONFIG, &params);
298}
299
300static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
301{
302 struct dwc3_gadget_ep_cmd_params params;
303
304 memset(&params, 0x00, sizeof(params));
305
306 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
307
308 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
309 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
310}
311
312/**
313 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
314 * @dep: endpoint to be initialized
315 * @desc: USB Endpoint Descriptor
316 *
317 * Caller should take care of locking
318 */
319static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
320 const struct usb_endpoint_descriptor *desc)
321{
322 struct dwc3 *dwc = dep->dwc;
323 u32 reg;
324 int ret = -ENOMEM;
325
326 if (!(dep->flags & DWC3_EP_ENABLED)) {
327 ret = dwc3_gadget_start_config(dwc, dep);
328 if (ret)
329 return ret;
330 }
331
332 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
333 if (ret)
334 return ret;
335
336 if (!(dep->flags & DWC3_EP_ENABLED)) {
337 struct dwc3_trb_hw *trb_st_hw;
338 struct dwc3_trb_hw *trb_link_hw;
339 struct dwc3_trb trb_link;
340
341 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
342 if (ret)
343 return ret;
344
345 dep->desc = desc;
346 dep->type = usb_endpoint_type(desc);
347 dep->flags |= DWC3_EP_ENABLED;
348
349 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
350 reg |= DWC3_DALEPENA_EP(dep->number);
351 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
352
353 if (!usb_endpoint_xfer_isoc(desc))
354 return 0;
355
356 memset(&trb_link, 0, sizeof(trb_link));
357
358 /* Link TRB for ISOC. The HWO but is never reset */
359 trb_st_hw = &dep->trb_pool[0];
360
361 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
362 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
363 trb_link.hwo = true;
364
365 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
366 dwc3_trb_to_hw(&trb_link, trb_link_hw);
367 }
368
369 return 0;
370}
371
372static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
373static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
374{
375 struct dwc3_request *req;
376
377 if (!list_empty(&dep->req_queued))
378 dwc3_stop_active_transfer(dwc, dep->number);
379
380 while (!list_empty(&dep->request_list)) {
381 req = next_request(&dep->request_list);
382
383 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
384 }
385}
386
387/**
388 * __dwc3_gadget_ep_disable - Disables a HW endpoint
389 * @dep: the endpoint to disable
390 *
391 * This function also removes requests which are currently processed ny the
392 * hardware and those which are not yet scheduled.
393 * Caller should take care of locking.
394 */
395static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
396{
397 struct dwc3 *dwc = dep->dwc;
398 u32 reg;
399
400 dwc3_remove_requests(dwc, dep);
401
402 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
403 reg &= ~DWC3_DALEPENA_EP(dep->number);
404 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
405
406 dep->stream_capable = false;
407 dep->desc = NULL;
408 dep->type = 0;
409 dep->flags = 0;
410
411 return 0;
412}
413
414/* -------------------------------------------------------------------------- */
415
416static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
417 const struct usb_endpoint_descriptor *desc)
418{
419 return -EINVAL;
420}
421
422static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
423{
424 return -EINVAL;
425}
426
427/* -------------------------------------------------------------------------- */
428
429static int dwc3_gadget_ep_enable(struct usb_ep *ep,
430 const struct usb_endpoint_descriptor *desc)
431{
432 struct dwc3_ep *dep;
433 struct dwc3 *dwc;
434 unsigned long flags;
435 int ret;
436
437 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
438 pr_debug("dwc3: invalid parameters\n");
439 return -EINVAL;
440 }
441
442 if (!desc->wMaxPacketSize) {
443 pr_debug("dwc3: missing wMaxPacketSize\n");
444 return -EINVAL;
445 }
446
447 dep = to_dwc3_ep(ep);
448 dwc = dep->dwc;
449
450 switch (usb_endpoint_type(desc)) {
451 case USB_ENDPOINT_XFER_CONTROL:
452 strncat(dep->name, "-control", sizeof(dep->name));
453 break;
454 case USB_ENDPOINT_XFER_ISOC:
455 strncat(dep->name, "-isoc", sizeof(dep->name));
456 break;
457 case USB_ENDPOINT_XFER_BULK:
458 strncat(dep->name, "-bulk", sizeof(dep->name));
459 break;
460 case USB_ENDPOINT_XFER_INT:
461 strncat(dep->name, "-int", sizeof(dep->name));
462 break;
463 default:
464 dev_err(dwc->dev, "invalid endpoint transfer type\n");
465 }
466
467 if (dep->flags & DWC3_EP_ENABLED) {
468 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
469 dep->name);
470 return 0;
471 }
472
473 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
474
475 spin_lock_irqsave(&dwc->lock, flags);
476 ret = __dwc3_gadget_ep_enable(dep, desc);
477 spin_unlock_irqrestore(&dwc->lock, flags);
478
479 return ret;
480}
481
482static int dwc3_gadget_ep_disable(struct usb_ep *ep)
483{
484 struct dwc3_ep *dep;
485 struct dwc3 *dwc;
486 unsigned long flags;
487 int ret;
488
489 if (!ep) {
490 pr_debug("dwc3: invalid parameters\n");
491 return -EINVAL;
492 }
493
494 dep = to_dwc3_ep(ep);
495 dwc = dep->dwc;
496
497 if (!(dep->flags & DWC3_EP_ENABLED)) {
498 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
499 dep->name);
500 return 0;
501 }
502
503 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
504 dep->number >> 1,
505 (dep->number & 1) ? "in" : "out");
506
507 spin_lock_irqsave(&dwc->lock, flags);
508 ret = __dwc3_gadget_ep_disable(dep);
509 spin_unlock_irqrestore(&dwc->lock, flags);
510
511 return ret;
512}
513
514static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
515 gfp_t gfp_flags)
516{
517 struct dwc3_request *req;
518 struct dwc3_ep *dep = to_dwc3_ep(ep);
519 struct dwc3 *dwc = dep->dwc;
520
521 req = kzalloc(sizeof(*req), gfp_flags);
522 if (!req) {
523 dev_err(dwc->dev, "not enough memory\n");
524 return NULL;
525 }
526
527 req->epnum = dep->number;
528 req->dep = dep;
529 req->request.dma = DMA_ADDR_INVALID;
530
531 return &req->request;
532}
533
534static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
535 struct usb_request *request)
536{
537 struct dwc3_request *req = to_dwc3_request(request);
538
539 kfree(req);
540}
541
542/*
543 * dwc3_prepare_trbs - setup TRBs from requests
544 * @dep: endpoint for which requests are being prepared
545 * @starting: true if the endpoint is idle and no requests are queued.
546 *
547 * The functions goes through the requests list and setups TRBs for the
548 * transfers. The functions returns once there are not more TRBs available or
549 * it run out of requests.
550 */
551static struct dwc3_request *dwc3_prepare_trbs(struct dwc3_ep *dep,
552 bool starting)
553{
554 struct dwc3_request *req, *n, *ret = NULL;
555 struct dwc3_trb_hw *trb_hw;
556 struct dwc3_trb trb;
557 u32 trbs_left;
558
559 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
560
561 /* the first request must not be queued */
562 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
563 /*
564 * if busy & slot are equal than it is either full or empty. If we are
565 * starting to proceed requests then we are empty. Otherwise we ar
566 * full and don't do anything
567 */
568 if (!trbs_left) {
569 if (!starting)
570 return NULL;
571 trbs_left = DWC3_TRB_NUM;
572 /*
573 * In case we start from scratch, we queue the ISOC requests
574 * starting from slot 1. This is done because we use ring
575 * buffer and have no LST bit to stop us. Instead, we place
576 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
577 * after the first request so we start at slot 1 and have
578 * 7 requests proceed before we hit the first IOC.
579 * Other transfer types don't use the ring buffer and are
580 * processed from the first TRB until the last one. Since we
581 * don't wrap around we have to start at the beginning.
582 */
583 if (usb_endpoint_xfer_isoc(dep->desc)) {
584 dep->busy_slot = 1;
585 dep->free_slot = 1;
586 } else {
587 dep->busy_slot = 0;
588 dep->free_slot = 0;
589 }
590 }
591
592 /* The last TRB is a link TRB, not used for xfer */
593 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
594 return NULL;
595
596 list_for_each_entry_safe(req, n, &dep->request_list, list) {
597 unsigned int last_one = 0;
598 unsigned int cur_slot;
599
600 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
601 cur_slot = dep->free_slot;
602 dep->free_slot++;
603
604 /* Skip the LINK-TRB on ISOC */
605 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
606 usb_endpoint_xfer_isoc(dep->desc))
607 continue;
608
609 dwc3_gadget_move_request_queued(req);
610 memset(&trb, 0, sizeof(trb));
611 trbs_left--;
612
613 /* Is our TRB pool empty? */
614 if (!trbs_left)
615 last_one = 1;
616 /* Is this the last request? */
617 if (list_empty(&dep->request_list))
618 last_one = 1;
619
620 /*
621 * FIXME we shouldn't need to set LST bit always but we are
622 * facing some weird problem with the Hardware where it doesn't
623 * complete even though it has been previously started.
624 *
625 * While we're debugging the problem, as a workaround to
626 * multiple TRBs handling, use only one TRB at a time.
627 */
628 last_one = 1;
629
630 req->trb = trb_hw;
631 if (!ret)
632 ret = req;
633
634 trb.bplh = req->request.dma;
635
636 if (usb_endpoint_xfer_isoc(dep->desc)) {
637 trb.isp_imi = true;
638 trb.csp = true;
639 } else {
640 trb.lst = last_one;
641 }
642
643 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
644 trb.sid_sofn = req->request.stream_id;
645
646 switch (usb_endpoint_type(dep->desc)) {
647 case USB_ENDPOINT_XFER_CONTROL:
648 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
649 break;
650
651 case USB_ENDPOINT_XFER_ISOC:
652 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
653
654 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
655 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
656 trb.ioc = last_one;
657 break;
658
659 case USB_ENDPOINT_XFER_BULK:
660 case USB_ENDPOINT_XFER_INT:
661 trb.trbctl = DWC3_TRBCTL_NORMAL;
662 break;
663 default:
664 /*
665 * This is only possible with faulty memory because we
666 * checked it already :)
667 */
668 BUG();
669 }
670
671 trb.length = req->request.length;
672 trb.hwo = true;
673
674 dwc3_trb_to_hw(&trb, trb_hw);
675 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
676
677 if (last_one)
678 break;
679 }
680
681 return ret;
682}
683
684static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
685 int start_new)
686{
687 struct dwc3_gadget_ep_cmd_params params;
688 struct dwc3_request *req;
689 struct dwc3 *dwc = dep->dwc;
690 int ret;
691 u32 cmd;
692
693 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
694 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
695 return -EBUSY;
696 }
697 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
698
699 /*
700 * If we are getting here after a short-out-packet we don't enqueue any
701 * new requests as we try to set the IOC bit only on the last request.
702 */
703 if (start_new) {
704 if (list_empty(&dep->req_queued))
705 dwc3_prepare_trbs(dep, start_new);
706
707 /* req points to the first request which will be sent */
708 req = next_request(&dep->req_queued);
709 } else {
710 /*
711 * req points to the first request where HWO changed
712 * from 0 to 1
713 */
714 req = dwc3_prepare_trbs(dep, start_new);
715 }
716 if (!req) {
717 dep->flags |= DWC3_EP_PENDING_REQUEST;
718 return 0;
719 }
720
721 memset(&params, 0, sizeof(params));
722 params.param0 = upper_32_bits(req->trb_dma);
723 params.param1 = lower_32_bits(req->trb_dma);
724
725 if (start_new)
726 cmd = DWC3_DEPCMD_STARTTRANSFER;
727 else
728 cmd = DWC3_DEPCMD_UPDATETRANSFER;
729
730 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
731 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
732 if (ret < 0) {
733 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
734
735 /*
736 * FIXME we need to iterate over the list of requests
737 * here and stop, unmap, free and del each of the linked
738 * requests instead of we do now.
739 */
740 dwc3_unmap_buffer_from_dma(req);
741 list_del(&req->list);
742 return ret;
743 }
744
745 dep->flags |= DWC3_EP_BUSY;
746 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
747 dep->number);
748 if (!dep->res_trans_idx)
749 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
750 return 0;
751}
752
753static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
754{
755 req->request.actual = 0;
756 req->request.status = -EINPROGRESS;
757 req->direction = dep->direction;
758 req->epnum = dep->number;
759
760 /*
761 * We only add to our list of requests now and
762 * start consuming the list once we get XferNotReady
763 * IRQ.
764 *
765 * That way, we avoid doing anything that we don't need
766 * to do now and defer it until the point we receive a
767 * particular token from the Host side.
768 *
769 * This will also avoid Host cancelling URBs due to too
770 * many NACKs.
771 */
772 dwc3_map_buffer_to_dma(req);
773 list_add_tail(&req->list, &dep->request_list);
774
775 /*
776 * There is one special case: XferNotReady with
777 * empty list of requests. We need to kick the
778 * transfer here in that situation, otherwise
779 * we will be NAKing forever.
780 *
781 * If we get XferNotReady before gadget driver
782 * has a chance to queue a request, we will ACK
783 * the IRQ but won't be able to receive the data
784 * until the next request is queued. The following
785 * code is handling exactly that.
786 */
787 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
788 int ret;
789 int start_trans;
790
791 start_trans = 1;
792 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
793 dep->flags & DWC3_EP_BUSY)
794 start_trans = 0;
795
796 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
797 if (ret && ret != -EBUSY) {
798 struct dwc3 *dwc = dep->dwc;
799
800 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
801 dep->name);
802 }
803 };
804
805 return 0;
806}
807
808static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
809 gfp_t gfp_flags)
810{
811 struct dwc3_request *req = to_dwc3_request(request);
812 struct dwc3_ep *dep = to_dwc3_ep(ep);
813 struct dwc3 *dwc = dep->dwc;
814
815 unsigned long flags;
816
817 int ret;
818
819 if (!dep->desc) {
820 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
821 request, ep->name);
822 return -ESHUTDOWN;
823 }
824
825 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
826 request, ep->name, request->length);
827
828 spin_lock_irqsave(&dwc->lock, flags);
829 ret = __dwc3_gadget_ep_queue(dep, req);
830 spin_unlock_irqrestore(&dwc->lock, flags);
831
832 return ret;
833}
834
835static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
836 struct usb_request *request)
837{
838 struct dwc3_request *req = to_dwc3_request(request);
839 struct dwc3_request *r = NULL;
840
841 struct dwc3_ep *dep = to_dwc3_ep(ep);
842 struct dwc3 *dwc = dep->dwc;
843
844 unsigned long flags;
845 int ret = 0;
846
847 spin_lock_irqsave(&dwc->lock, flags);
848
849 list_for_each_entry(r, &dep->request_list, list) {
850 if (r == req)
851 break;
852 }
853
854 if (r != req) {
855 list_for_each_entry(r, &dep->req_queued, list) {
856 if (r == req)
857 break;
858 }
859 if (r == req) {
860 /* wait until it is processed */
861 dwc3_stop_active_transfer(dwc, dep->number);
862 goto out0;
863 }
864 dev_err(dwc->dev, "request %p was not queued to %s\n",
865 request, ep->name);
866 ret = -EINVAL;
867 goto out0;
868 }
869
870 /* giveback the request */
871 dwc3_gadget_giveback(dep, req, -ECONNRESET);
872
873out0:
874 spin_unlock_irqrestore(&dwc->lock, flags);
875
876 return ret;
877}
878
879int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
880{
881 struct dwc3_gadget_ep_cmd_params params;
882 struct dwc3 *dwc = dep->dwc;
883 int ret;
884
885 memset(&params, 0x00, sizeof(params));
886
887 if (value) {
888 if (dep->number == 0 || dep->number == 1) {
889 /*
890 * Whenever EP0 is stalled, we will restart
891 * the state machine, thus moving back to
892 * Setup Phase
893 */
894 dwc->ep0state = EP0_SETUP_PHASE;
895 }
896
897 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
898 DWC3_DEPCMD_SETSTALL, &params);
899 if (ret)
900 dev_err(dwc->dev, "failed to %s STALL on %s\n",
901 value ? "set" : "clear",
902 dep->name);
903 else
904 dep->flags |= DWC3_EP_STALL;
905 } else {
906 if (dep->flags & DWC3_EP_WEDGE)
907 return 0;
908
909 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
910 DWC3_DEPCMD_CLEARSTALL, &params);
911 if (ret)
912 dev_err(dwc->dev, "failed to %s STALL on %s\n",
913 value ? "set" : "clear",
914 dep->name);
915 else
916 dep->flags &= ~DWC3_EP_STALL;
917 }
918
919 return ret;
920}
921
922static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
923{
924 struct dwc3_ep *dep = to_dwc3_ep(ep);
925 struct dwc3 *dwc = dep->dwc;
926
927 unsigned long flags;
928
929 int ret;
930
931 spin_lock_irqsave(&dwc->lock, flags);
932
933 if (usb_endpoint_xfer_isoc(dep->desc)) {
934 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
935 ret = -EINVAL;
936 goto out;
937 }
938
939 ret = __dwc3_gadget_ep_set_halt(dep, value);
940out:
941 spin_unlock_irqrestore(&dwc->lock, flags);
942
943 return ret;
944}
945
946static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
947{
948 struct dwc3_ep *dep = to_dwc3_ep(ep);
949
950 dep->flags |= DWC3_EP_WEDGE;
951
952 return dwc3_gadget_ep_set_halt(ep, 1);
953}
954
955/* -------------------------------------------------------------------------- */
956
957static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
958 .bLength = USB_DT_ENDPOINT_SIZE,
959 .bDescriptorType = USB_DT_ENDPOINT,
960 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
961};
962
963static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
964 .enable = dwc3_gadget_ep0_enable,
965 .disable = dwc3_gadget_ep0_disable,
966 .alloc_request = dwc3_gadget_ep_alloc_request,
967 .free_request = dwc3_gadget_ep_free_request,
968 .queue = dwc3_gadget_ep0_queue,
969 .dequeue = dwc3_gadget_ep_dequeue,
970 .set_halt = dwc3_gadget_ep_set_halt,
971 .set_wedge = dwc3_gadget_ep_set_wedge,
972};
973
974static const struct usb_ep_ops dwc3_gadget_ep_ops = {
975 .enable = dwc3_gadget_ep_enable,
976 .disable = dwc3_gadget_ep_disable,
977 .alloc_request = dwc3_gadget_ep_alloc_request,
978 .free_request = dwc3_gadget_ep_free_request,
979 .queue = dwc3_gadget_ep_queue,
980 .dequeue = dwc3_gadget_ep_dequeue,
981 .set_halt = dwc3_gadget_ep_set_halt,
982 .set_wedge = dwc3_gadget_ep_set_wedge,
983};
984
985/* -------------------------------------------------------------------------- */
986
987static int dwc3_gadget_get_frame(struct usb_gadget *g)
988{
989 struct dwc3 *dwc = gadget_to_dwc(g);
990 u32 reg;
991
992 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
993 return DWC3_DSTS_SOFFN(reg);
994}
995
996static int dwc3_gadget_wakeup(struct usb_gadget *g)
997{
998 struct dwc3 *dwc = gadget_to_dwc(g);
999
1000 unsigned long timeout;
1001 unsigned long flags;
1002
1003 u32 reg;
1004
1005 int ret = 0;
1006
1007 u8 link_state;
1008 u8 speed;
1009
1010 spin_lock_irqsave(&dwc->lock, flags);
1011
1012 /*
1013 * According to the Databook Remote wakeup request should
1014 * be issued only when the device is in early suspend state.
1015 *
1016 * We can check that via USB Link State bits in DSTS register.
1017 */
1018 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1019
1020 speed = reg & DWC3_DSTS_CONNECTSPD;
1021 if (speed == DWC3_DSTS_SUPERSPEED) {
1022 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1023 ret = -EINVAL;
1024 goto out;
1025 }
1026
1027 link_state = DWC3_DSTS_USBLNKST(reg);
1028
1029 switch (link_state) {
1030 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1031 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1032 break;
1033 default:
1034 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1035 link_state);
1036 ret = -EINVAL;
1037 goto out;
1038 }
1039
1040 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1041
1042 /*
1043 * Switch link state to Recovery. In HS/FS/LS this means
1044 * RemoteWakeup Request
1045 */
1046 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1047 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1048
1049 /* wait for at least 2000us */
1050 usleep_range(2000, 2500);
1051
1052 /* write zeroes to Link Change Request */
1053 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1054 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1055
1056 /* pool until Link State change to ON */
1057 timeout = jiffies + msecs_to_jiffies(100);
1058
1059 while (!(time_after(jiffies, timeout))) {
1060 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1061
1062 /* in HS, means ON */
1063 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1064 break;
1065 }
1066
1067 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1068 dev_err(dwc->dev, "failed to send remote wakeup\n");
1069 ret = -EINVAL;
1070 }
1071
1072out:
1073 spin_unlock_irqrestore(&dwc->lock, flags);
1074
1075 return ret;
1076}
1077
1078static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1079 int is_selfpowered)
1080{
1081 struct dwc3 *dwc = gadget_to_dwc(g);
1082
1083 dwc->is_selfpowered = !!is_selfpowered;
1084
1085 return 0;
1086}
1087
1088static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1089{
1090 u32 reg;
1091 u32 timeout = 500;
1092
1093 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1094 if (is_on)
1095 reg |= DWC3_DCTL_RUN_STOP;
1096 else
1097 reg &= ~DWC3_DCTL_RUN_STOP;
1098
1099 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1100
1101 do {
1102 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1103 if (is_on) {
1104 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1105 break;
1106 } else {
1107 if (reg & DWC3_DSTS_DEVCTRLHLT)
1108 break;
1109 }
1110 timeout--;
1111 if (!timeout)
1112 break;
1113 udelay(1);
1114 } while (1);
1115
1116 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1117 dwc->gadget_driver
1118 ? dwc->gadget_driver->function : "no-function",
1119 is_on ? "connect" : "disconnect");
1120}
1121
1122static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1123{
1124 struct dwc3 *dwc = gadget_to_dwc(g);
1125 unsigned long flags;
1126
1127 is_on = !!is_on;
1128
1129 spin_lock_irqsave(&dwc->lock, flags);
1130 dwc3_gadget_run_stop(dwc, is_on);
1131 spin_unlock_irqrestore(&dwc->lock, flags);
1132
1133 return 0;
1134}
1135
1136static int dwc3_gadget_start(struct usb_gadget *g,
1137 struct usb_gadget_driver *driver)
1138{
1139 struct dwc3 *dwc = gadget_to_dwc(g);
1140 struct dwc3_ep *dep;
1141 unsigned long flags;
1142 int ret = 0;
1143 u32 reg;
1144
1145 spin_lock_irqsave(&dwc->lock, flags);
1146
1147 if (dwc->gadget_driver) {
1148 dev_err(dwc->dev, "%s is already bound to %s\n",
1149 dwc->gadget.name,
1150 dwc->gadget_driver->driver.name);
1151 ret = -EBUSY;
1152 goto err0;
1153 }
1154
1155 dwc->gadget_driver = driver;
1156 dwc->gadget.dev.driver = &driver->driver;
1157
1158 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1159
1160 reg &= ~DWC3_GCTL_SCALEDOWN(3);
1161 reg &= ~DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG);
1162 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1163 reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
1164
1165 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams0)) {
1166 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
1167 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
1168 break;
1169 default:
1170 dev_dbg(dwc->dev, "No power optimization available\n");
1171 }
1172
1173 /*
1174 * WORKAROUND: DWC3 revisions <1.90a have a bug
1175 * when The device fails to connect at SuperSpeed
1176 * and falls back to high-speed mode which causes
1177 * the device to enter in a Connect/Disconnect loop
1178 */
1179 if (dwc->revision < DWC3_REVISION_190A)
1180 reg |= DWC3_GCTL_U2RSTECN;
1181
1182 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1183
1184 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1185 reg &= ~(DWC3_DCFG_SPEED_MASK);
1186 reg |= DWC3_DCFG_SUPERSPEED;
1187 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1188
1189 dwc->start_config_issued = false;
1190
1191 /* Start with SuperSpeed Default */
1192 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1193
1194 dep = dwc->eps[0];
1195 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1196 if (ret) {
1197 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1198 goto err0;
1199 }
1200
1201 dep = dwc->eps[1];
1202 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1203 if (ret) {
1204 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1205 goto err1;
1206 }
1207
1208 /* begin to receive SETUP packets */
1209 dwc->ep0state = EP0_SETUP_PHASE;
1210 dwc3_ep0_out_start(dwc);
1211
1212 spin_unlock_irqrestore(&dwc->lock, flags);
1213
1214 return 0;
1215
1216err1:
1217 __dwc3_gadget_ep_disable(dwc->eps[0]);
1218
1219err0:
1220 spin_unlock_irqrestore(&dwc->lock, flags);
1221
1222 return ret;
1223}
1224
1225static int dwc3_gadget_stop(struct usb_gadget *g,
1226 struct usb_gadget_driver *driver)
1227{
1228 struct dwc3 *dwc = gadget_to_dwc(g);
1229 unsigned long flags;
1230
1231 spin_lock_irqsave(&dwc->lock, flags);
1232
1233 __dwc3_gadget_ep_disable(dwc->eps[0]);
1234 __dwc3_gadget_ep_disable(dwc->eps[1]);
1235
1236 dwc->gadget_driver = NULL;
1237 dwc->gadget.dev.driver = NULL;
1238
1239 spin_unlock_irqrestore(&dwc->lock, flags);
1240
1241 return 0;
1242}
1243static const struct usb_gadget_ops dwc3_gadget_ops = {
1244 .get_frame = dwc3_gadget_get_frame,
1245 .wakeup = dwc3_gadget_wakeup,
1246 .set_selfpowered = dwc3_gadget_set_selfpowered,
1247 .pullup = dwc3_gadget_pullup,
1248 .udc_start = dwc3_gadget_start,
1249 .udc_stop = dwc3_gadget_stop,
1250};
1251
1252/* -------------------------------------------------------------------------- */
1253
1254static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1255{
1256 struct dwc3_ep *dep;
1257 u8 epnum;
1258
1259 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1260
1261 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1262 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1263 if (!dep) {
1264 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1265 epnum);
1266 return -ENOMEM;
1267 }
1268
1269 dep->dwc = dwc;
1270 dep->number = epnum;
1271 dwc->eps[epnum] = dep;
1272
1273 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1274 (epnum & 1) ? "in" : "out");
1275 dep->endpoint.name = dep->name;
1276 dep->direction = (epnum & 1);
1277
1278 if (epnum == 0 || epnum == 1) {
1279 dep->endpoint.maxpacket = 512;
1280 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1281 if (!epnum)
1282 dwc->gadget.ep0 = &dep->endpoint;
1283 } else {
1284 int ret;
1285
1286 dep->endpoint.maxpacket = 1024;
1287 dep->endpoint.max_streams = 15;
1288 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1289 list_add_tail(&dep->endpoint.ep_list,
1290 &dwc->gadget.ep_list);
1291
1292 ret = dwc3_alloc_trb_pool(dep);
1293 if (ret) {
1294 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1295 return ret;
1296 }
1297 }
1298 INIT_LIST_HEAD(&dep->request_list);
1299 INIT_LIST_HEAD(&dep->req_queued);
1300 }
1301
1302 return 0;
1303}
1304
1305static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1306{
1307 struct dwc3_ep *dep;
1308 u8 epnum;
1309
1310 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1311 dep = dwc->eps[epnum];
1312 dwc3_free_trb_pool(dep);
1313
1314 if (epnum != 0 && epnum != 1)
1315 list_del(&dep->endpoint.ep_list);
1316
1317 kfree(dep);
1318 }
1319}
1320
1321static void dwc3_gadget_release(struct device *dev)
1322{
1323 dev_dbg(dev, "%s\n", __func__);
1324}
1325
1326/* -------------------------------------------------------------------------- */
1327static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1328 const struct dwc3_event_depevt *event, int status)
1329{
1330 struct dwc3_request *req;
1331 struct dwc3_trb trb;
1332 unsigned int count;
1333 unsigned int s_pkt = 0;
1334
1335 do {
1336 req = next_request(&dep->req_queued);
1337 if (!req)
1338 break;
1339
1340 dwc3_trb_to_nat(req->trb, &trb);
1341
1342 if (trb.hwo && status != -ESHUTDOWN)
1343 /*
1344 * We continue despite the error. There is not much we
1345 * can do. If we don't clean in up we loop for ever. If
1346 * we skip the TRB than it gets overwritten reused after
1347 * a while since we use them in a ring buffer. a BUG()
1348 * would help. Lets hope that if this occures, someone
1349 * fixes the root cause instead of looking away :)
1350 */
1351 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1352 dep->name, req->trb);
1353 count = trb.length;
1354
1355 if (dep->direction) {
1356 if (count) {
1357 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1358 dep->name);
1359 status = -ECONNRESET;
1360 }
1361 } else {
1362 if (count && (event->status & DEPEVT_STATUS_SHORT))
1363 s_pkt = 1;
1364 }
1365
1366 /*
1367 * We assume here we will always receive the entire data block
1368 * which we should receive. Meaning, if we program RX to
1369 * receive 4K but we receive only 2K, we assume that's all we
1370 * should receive and we simply bounce the request back to the
1371 * gadget driver for further processing.
1372 */
1373 req->request.actual += req->request.length - count;
1374 dwc3_gadget_giveback(dep, req, status);
1375 if (s_pkt)
1376 break;
1377 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1378 break;
1379 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1380 break;
1381 } while (1);
1382
1383 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1384 return 0;
1385 return 1;
1386}
1387
1388static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1389 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1390 int start_new)
1391{
1392 unsigned status = 0;
1393 int clean_busy;
1394
1395 if (event->status & DEPEVT_STATUS_BUSERR)
1396 status = -ECONNRESET;
1397
1398 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1399 if (clean_busy) {
1400 dep->flags &= ~DWC3_EP_BUSY;
1401 dep->res_trans_idx = 0;
1402 }
1403}
1404
1405static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1406 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1407{
1408 u32 uf;
1409
1410 if (list_empty(&dep->request_list)) {
1411 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1412 dep->name);
1413 return;
1414 }
1415
1416 if (event->parameters) {
1417 u32 mask;
1418
1419 mask = ~(dep->interval - 1);
1420 uf = event->parameters & mask;
1421 /* 4 micro frames in the future */
1422 uf += dep->interval * 4;
1423 } else {
1424 uf = 0;
1425 }
1426
1427 __dwc3_gadget_kick_transfer(dep, uf, 1);
1428}
1429
1430static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1431 const struct dwc3_event_depevt *event)
1432{
1433 struct dwc3 *dwc = dep->dwc;
1434 struct dwc3_event_depevt mod_ev = *event;
1435
1436 /*
1437 * We were asked to remove one requests. It is possible that this
1438 * request and a few other were started together and have the same
1439 * transfer index. Since we stopped the complete endpoint we don't
1440 * know how many requests were already completed (and not yet)
1441 * reported and how could be done (later). We purge them all until
1442 * the end of the list.
1443 */
1444 mod_ev.status = DEPEVT_STATUS_LST;
1445 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1446 dep->flags &= ~DWC3_EP_BUSY;
1447 /* pending requets are ignored and are queued on XferNotReady */
1448}
1449
1450static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1451 const struct dwc3_event_depevt *event)
1452{
1453 u32 param = event->parameters;
1454 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1455
1456 switch (cmd_type) {
1457 case DWC3_DEPCMD_ENDTRANSFER:
1458 dwc3_process_ep_cmd_complete(dep, event);
1459 break;
1460 case DWC3_DEPCMD_STARTTRANSFER:
1461 dep->res_trans_idx = param & 0x7f;
1462 break;
1463 default:
1464 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1465 __func__, cmd_type);
1466 break;
1467 };
1468}
1469
1470static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1471 const struct dwc3_event_depevt *event)
1472{
1473 struct dwc3_ep *dep;
1474 u8 epnum = event->endpoint_number;
1475
1476 dep = dwc->eps[epnum];
1477
1478 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1479 dwc3_ep_event_string(event->endpoint_event));
1480
1481 if (epnum == 0 || epnum == 1) {
1482 dwc3_ep0_interrupt(dwc, event);
1483 return;
1484 }
1485
1486 switch (event->endpoint_event) {
1487 case DWC3_DEPEVT_XFERCOMPLETE:
1488 if (usb_endpoint_xfer_isoc(dep->desc)) {
1489 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1490 dep->name);
1491 return;
1492 }
1493
1494 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1495 break;
1496 case DWC3_DEPEVT_XFERINPROGRESS:
1497 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1498 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1499 dep->name);
1500 return;
1501 }
1502
1503 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1504 break;
1505 case DWC3_DEPEVT_XFERNOTREADY:
1506 if (usb_endpoint_xfer_isoc(dep->desc)) {
1507 dwc3_gadget_start_isoc(dwc, dep, event);
1508 } else {
1509 int ret;
1510
1511 dev_vdbg(dwc->dev, "%s: reason %s\n",
1512 dep->name, event->status
1513 ? "Transfer Active"
1514 : "Transfer Not Active");
1515
1516 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1517 if (!ret || ret == -EBUSY)
1518 return;
1519
1520 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1521 dep->name);
1522 }
1523
1524 break;
1525 case DWC3_DEPEVT_STREAMEVT:
1526 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1527 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1528 dep->name);
1529 return;
1530 }
1531
1532 switch (event->status) {
1533 case DEPEVT_STREAMEVT_FOUND:
1534 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1535 event->parameters);
1536
1537 break;
1538 case DEPEVT_STREAMEVT_NOTFOUND:
1539 /* FALLTHROUGH */
1540 default:
1541 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1542 }
1543 break;
1544 case DWC3_DEPEVT_RXTXFIFOEVT:
1545 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1546 break;
1547 case DWC3_DEPEVT_EPCMDCMPLT:
1548 dwc3_ep_cmd_compl(dep, event);
1549 break;
1550 }
1551}
1552
1553static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1554{
1555 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1556 spin_unlock(&dwc->lock);
1557 dwc->gadget_driver->disconnect(&dwc->gadget);
1558 spin_lock(&dwc->lock);
1559 }
1560}
1561
1562static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1563{
1564 struct dwc3_ep *dep;
1565 struct dwc3_gadget_ep_cmd_params params;
1566 u32 cmd;
1567 int ret;
1568
1569 dep = dwc->eps[epnum];
1570
1571 WARN_ON(!dep->res_trans_idx);
1572 if (dep->res_trans_idx) {
1573 cmd = DWC3_DEPCMD_ENDTRANSFER;
1574 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1575 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1576 memset(&params, 0, sizeof(params));
1577 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1578 WARN_ON_ONCE(ret);
1579 dep->res_trans_idx = 0;
1580 }
1581}
1582
1583static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1584{
1585 u32 epnum;
1586
1587 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1588 struct dwc3_ep *dep;
1589
1590 dep = dwc->eps[epnum];
1591 if (!(dep->flags & DWC3_EP_ENABLED))
1592 continue;
1593
1594 dwc3_remove_requests(dwc, dep);
1595 }
1596}
1597
1598static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1599{
1600 u32 epnum;
1601
1602 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1603 struct dwc3_ep *dep;
1604 struct dwc3_gadget_ep_cmd_params params;
1605 int ret;
1606
1607 dep = dwc->eps[epnum];
1608
1609 if (!(dep->flags & DWC3_EP_STALL))
1610 continue;
1611
1612 dep->flags &= ~DWC3_EP_STALL;
1613
1614 memset(&params, 0, sizeof(params));
1615 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1616 DWC3_DEPCMD_CLEARSTALL, &params);
1617 WARN_ON_ONCE(ret);
1618 }
1619}
1620
1621static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1622{
1623 dev_vdbg(dwc->dev, "%s\n", __func__);
1624#if 0
1625 XXX
1626 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1627 enable it before we can disable it.
1628
1629 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1630 reg &= ~DWC3_DCTL_INITU1ENA;
1631 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1632
1633 reg &= ~DWC3_DCTL_INITU2ENA;
1634 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1635#endif
1636
1637 dwc3_stop_active_transfers(dwc);
1638 dwc3_disconnect_gadget(dwc);
1639 dwc->start_config_issued = false;
1640
1641 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1642}
1643
1644static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1645{
1646 u32 reg;
1647
1648 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1649
1650 if (on)
1651 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1652 else
1653 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1654
1655 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1656}
1657
1658static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1659{
1660 u32 reg;
1661
1662 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1663
1664 if (on)
1665 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1666 else
1667 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1668
1669 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1670}
1671
1672static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1673{
1674 u32 reg;
1675
1676 dev_vdbg(dwc->dev, "%s\n", __func__);
1677
1678 /* Enable PHYs */
1679 dwc3_gadget_usb2_phy_power(dwc, true);
1680 dwc3_gadget_usb3_phy_power(dwc, true);
1681
1682 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1683 dwc3_disconnect_gadget(dwc);
1684
1685 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1686 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1687 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1688
1689 dwc3_stop_active_transfers(dwc);
1690 dwc3_clear_stall_all_ep(dwc);
1691 dwc->start_config_issued = false;
1692
1693 /* Reset device address to zero */
1694 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1695 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1696 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1697}
1698
1699static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1700{
1701 u32 reg;
1702 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1703
1704 /*
1705 * We change the clock only at SS but I dunno why I would want to do
1706 * this. Maybe it becomes part of the power saving plan.
1707 */
1708
1709 if (speed != DWC3_DSTS_SUPERSPEED)
1710 return;
1711
1712 /*
1713 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1714 * each time on Connect Done.
1715 */
1716 if (!usb30_clock)
1717 return;
1718
1719 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1720 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1721 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1722}
1723
1724static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1725{
1726 switch (speed) {
1727 case USB_SPEED_SUPER:
1728 dwc3_gadget_usb2_phy_power(dwc, false);
1729 break;
1730 case USB_SPEED_HIGH:
1731 case USB_SPEED_FULL:
1732 case USB_SPEED_LOW:
1733 dwc3_gadget_usb3_phy_power(dwc, false);
1734 break;
1735 }
1736}
1737
1738static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1739{
1740 struct dwc3_gadget_ep_cmd_params params;
1741 struct dwc3_ep *dep;
1742 int ret;
1743 u32 reg;
1744 u8 speed;
1745
1746 dev_vdbg(dwc->dev, "%s\n", __func__);
1747
1748 memset(&params, 0x00, sizeof(params));
1749
1750 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1751 speed = reg & DWC3_DSTS_CONNECTSPD;
1752 dwc->speed = speed;
1753
1754 dwc3_update_ram_clk_sel(dwc, speed);
1755
1756 switch (speed) {
1757 case DWC3_DCFG_SUPERSPEED:
1758 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1759 dwc->gadget.ep0->maxpacket = 512;
1760 dwc->gadget.speed = USB_SPEED_SUPER;
1761 break;
1762 case DWC3_DCFG_HIGHSPEED:
1763 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1764 dwc->gadget.ep0->maxpacket = 64;
1765 dwc->gadget.speed = USB_SPEED_HIGH;
1766 break;
1767 case DWC3_DCFG_FULLSPEED2:
1768 case DWC3_DCFG_FULLSPEED1:
1769 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1770 dwc->gadget.ep0->maxpacket = 64;
1771 dwc->gadget.speed = USB_SPEED_FULL;
1772 break;
1773 case DWC3_DCFG_LOWSPEED:
1774 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1775 dwc->gadget.ep0->maxpacket = 8;
1776 dwc->gadget.speed = USB_SPEED_LOW;
1777 break;
1778 }
1779
1780 /* Disable unneded PHY */
1781 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1782
1783 dep = dwc->eps[0];
1784 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1785 if (ret) {
1786 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1787 return;
1788 }
1789
1790 dep = dwc->eps[1];
1791 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1792 if (ret) {
1793 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1794 return;
1795 }
1796
1797 /*
1798 * Configure PHY via GUSB3PIPECTLn if required.
1799 *
1800 * Update GTXFIFOSIZn
1801 *
1802 * In both cases reset values should be sufficient.
1803 */
1804}
1805
1806static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1807{
1808 dev_vdbg(dwc->dev, "%s\n", __func__);
1809
1810 /*
1811 * TODO take core out of low power mode when that's
1812 * implemented.
1813 */
1814
1815 dwc->gadget_driver->resume(&dwc->gadget);
1816}
1817
1818static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1819 unsigned int evtinfo)
1820{
1821 /* The fith bit says SuperSpeed yes or no. */
1822 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1823
1824 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
1825}
1826
1827static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1828 const struct dwc3_event_devt *event)
1829{
1830 switch (event->type) {
1831 case DWC3_DEVICE_EVENT_DISCONNECT:
1832 dwc3_gadget_disconnect_interrupt(dwc);
1833 break;
1834 case DWC3_DEVICE_EVENT_RESET:
1835 dwc3_gadget_reset_interrupt(dwc);
1836 break;
1837 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1838 dwc3_gadget_conndone_interrupt(dwc);
1839 break;
1840 case DWC3_DEVICE_EVENT_WAKEUP:
1841 dwc3_gadget_wakeup_interrupt(dwc);
1842 break;
1843 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1844 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1845 break;
1846 case DWC3_DEVICE_EVENT_EOPF:
1847 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1848 break;
1849 case DWC3_DEVICE_EVENT_SOF:
1850 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1851 break;
1852 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1853 dev_vdbg(dwc->dev, "Erratic Error\n");
1854 break;
1855 case DWC3_DEVICE_EVENT_CMD_CMPL:
1856 dev_vdbg(dwc->dev, "Command Complete\n");
1857 break;
1858 case DWC3_DEVICE_EVENT_OVERFLOW:
1859 dev_vdbg(dwc->dev, "Overflow\n");
1860 break;
1861 default:
1862 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1863 }
1864}
1865
1866static void dwc3_process_event_entry(struct dwc3 *dwc,
1867 const union dwc3_event *event)
1868{
1869 /* Endpoint IRQ, handle it and return early */
1870 if (event->type.is_devspec == 0) {
1871 /* depevt */
1872 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1873 }
1874
1875 switch (event->type.type) {
1876 case DWC3_EVENT_TYPE_DEV:
1877 dwc3_gadget_interrupt(dwc, &event->devt);
1878 break;
1879 /* REVISIT what to do with Carkit and I2C events ? */
1880 default:
1881 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1882 }
1883}
1884
1885static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1886{
1887 struct dwc3_event_buffer *evt;
1888 int left;
1889 u32 count;
1890
1891 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1892 count &= DWC3_GEVNTCOUNT_MASK;
1893 if (!count)
1894 return IRQ_NONE;
1895
1896 evt = dwc->ev_buffs[buf];
1897 left = count;
1898
1899 while (left > 0) {
1900 union dwc3_event event;
1901
1902 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1903 dwc3_process_event_entry(dwc, &event);
1904 /*
1905 * XXX we wrap around correctly to the next entry as almost all
1906 * entries are 4 bytes in size. There is one entry which has 12
1907 * bytes which is a regular entry followed by 8 bytes data. ATM
1908 * I don't know how things are organized if were get next to the
1909 * a boundary so I worry about that once we try to handle that.
1910 */
1911 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1912 left -= 4;
1913
1914 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1915 }
1916
1917 return IRQ_HANDLED;
1918}
1919
1920static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1921{
1922 struct dwc3 *dwc = _dwc;
1923 int i;
1924 irqreturn_t ret = IRQ_NONE;
1925
1926 spin_lock(&dwc->lock);
1927
1928 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1929 irqreturn_t status;
1930
1931 status = dwc3_process_event_buf(dwc, i);
1932 if (status == IRQ_HANDLED)
1933 ret = status;
1934 }
1935
1936 spin_unlock(&dwc->lock);
1937
1938 return ret;
1939}
1940
1941/**
1942 * dwc3_gadget_init - Initializes gadget related registers
1943 * @dwc: Pointer to out controller context structure
1944 *
1945 * Returns 0 on success otherwise negative errno.
1946 */
1947int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1948{
1949 u32 reg;
1950 int ret;
1951 int irq;
1952
1953 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1954 &dwc->ctrl_req_addr, GFP_KERNEL);
1955 if (!dwc->ctrl_req) {
1956 dev_err(dwc->dev, "failed to allocate ctrl request\n");
1957 ret = -ENOMEM;
1958 goto err0;
1959 }
1960
1961 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
1962 &dwc->ep0_trb_addr, GFP_KERNEL);
1963 if (!dwc->ep0_trb) {
1964 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
1965 ret = -ENOMEM;
1966 goto err1;
1967 }
1968
1969 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
1970 sizeof(*dwc->setup_buf) * 2,
1971 &dwc->setup_buf_addr, GFP_KERNEL);
1972 if (!dwc->setup_buf) {
1973 dev_err(dwc->dev, "failed to allocate setup buffer\n");
1974 ret = -ENOMEM;
1975 goto err2;
1976 }
1977
1978 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
1979 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
1980 if (!dwc->ep0_bounce) {
1981 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
1982 ret = -ENOMEM;
1983 goto err3;
1984 }
1985
1986 dev_set_name(&dwc->gadget.dev, "gadget");
1987
1988 dwc->gadget.ops = &dwc3_gadget_ops;
1989 dwc->gadget.is_dualspeed = true;
1990 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1991 dwc->gadget.dev.parent = dwc->dev;
1992
1993 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
1994
1995 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
1996 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
1997 dwc->gadget.dev.release = dwc3_gadget_release;
1998 dwc->gadget.name = "dwc3-gadget";
1999
2000 /*
2001 * REVISIT: Here we should clear all pending IRQs to be
2002 * sure we're starting from a well known location.
2003 */
2004
2005 ret = dwc3_gadget_init_endpoints(dwc);
2006 if (ret)
2007 goto err4;
2008
2009 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2010
2011 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2012 "dwc3", dwc);
2013 if (ret) {
2014 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2015 irq, ret);
2016 goto err5;
2017 }
2018
2019 /* Enable all but Start and End of Frame IRQs */
2020 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2021 DWC3_DEVTEN_EVNTOVERFLOWEN |
2022 DWC3_DEVTEN_CMDCMPLTEN |
2023 DWC3_DEVTEN_ERRTICERREN |
2024 DWC3_DEVTEN_WKUPEVTEN |
2025 DWC3_DEVTEN_ULSTCNGEN |
2026 DWC3_DEVTEN_CONNECTDONEEN |
2027 DWC3_DEVTEN_USBRSTEN |
2028 DWC3_DEVTEN_DISCONNEVTEN);
2029 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2030
2031 ret = device_register(&dwc->gadget.dev);
2032 if (ret) {
2033 dev_err(dwc->dev, "failed to register gadget device\n");
2034 put_device(&dwc->gadget.dev);
2035 goto err6;
2036 }
2037
2038 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2039 if (ret) {
2040 dev_err(dwc->dev, "failed to register udc\n");
2041 goto err7;
2042 }
2043
2044 return 0;
2045
2046err7:
2047 device_unregister(&dwc->gadget.dev);
2048
2049err6:
2050 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2051 free_irq(irq, dwc);
2052
2053err5:
2054 dwc3_gadget_free_endpoints(dwc);
2055
2056err4:
2057 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2058 dwc->ep0_bounce_addr);
2059
2060err3:
2061 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2062 dwc->setup_buf, dwc->setup_buf_addr);
2063
2064err2:
2065 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2066 dwc->ep0_trb, dwc->ep0_trb_addr);
2067
2068err1:
2069 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2070 dwc->ctrl_req, dwc->ctrl_req_addr);
2071
2072err0:
2073 return ret;
2074}
2075
2076void dwc3_gadget_exit(struct dwc3 *dwc)
2077{
2078 int irq;
2079 int i;
2080
2081 usb_del_gadget_udc(&dwc->gadget);
2082 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2083
2084 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2085 free_irq(irq, dwc);
2086
2087 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2088 __dwc3_gadget_ep_disable(dwc->eps[i]);
2089
2090 dwc3_gadget_free_endpoints(dwc);
2091
2092 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2093 dwc->ep0_bounce_addr);
2094
2095 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2096 dwc->setup_buf, dwc->setup_buf_addr);
2097
2098 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2099 dwc->ep0_trb, dwc->ep0_trb_addr);
2100
2101 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2102 dwc->ctrl_req, dwc->ctrl_req_addr);
2103
2104 device_unregister(&dwc->gadget.dev);
2105}
diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h
new file mode 100644
index 000000000000..71145a449d99
--- /dev/null
+++ b/drivers/usb/dwc3/gadget.h
@@ -0,0 +1,211 @@
1/**
2 * gadget.h - DesignWare USB3 DRD Gadget Header
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef __DRIVERS_USB_DWC3_GADGET_H
40#define __DRIVERS_USB_DWC3_GADGET_H
41
42#include <linux/list.h>
43#include <linux/usb/gadget.h>
44#include "io.h"
45
46struct dwc3;
47#define to_dwc3_ep(ep) (container_of(ep, struct dwc3_ep, endpoint))
48#define gadget_to_dwc(g) (container_of(g, struct dwc3, gadget))
49
50/* DEPCFG parameter 1 */
51#define DWC3_DEPCFG_INT_NUM(n) ((n) << 0)
52#define DWC3_DEPCFG_XFER_COMPLETE_EN (1 << 8)
53#define DWC3_DEPCFG_XFER_IN_PROGRESS_EN (1 << 9)
54#define DWC3_DEPCFG_XFER_NOT_READY_EN (1 << 10)
55#define DWC3_DEPCFG_FIFO_ERROR_EN (1 << 11)
56#define DWC3_DEPCFG_STREAM_EVENT_EN (1 << 13)
57#define DWC3_DEPCFG_BINTERVAL_M1(n) ((n) << 16)
58#define DWC3_DEPCFG_STREAM_CAPABLE (1 << 24)
59#define DWC3_DEPCFG_EP_NUMBER(n) ((n) << 25)
60#define DWC3_DEPCFG_BULK_BASED (1 << 30)
61#define DWC3_DEPCFG_FIFO_BASED (1 << 31)
62
63/* DEPCFG parameter 0 */
64#define DWC3_DEPCFG_EP_TYPE(n) ((n) << 1)
65#define DWC3_DEPCFG_MAX_PACKET_SIZE(n) ((n) << 3)
66#define DWC3_DEPCFG_FIFO_NUMBER(n) ((n) << 17)
67#define DWC3_DEPCFG_BURST_SIZE(n) ((n) << 22)
68#define DWC3_DEPCFG_DATA_SEQ_NUM(n) ((n) << 26)
69#define DWC3_DEPCFG_IGN_SEQ_NUM (1 << 31)
70
71/* DEPXFERCFG parameter 0 */
72#define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffff)
73
74struct dwc3_gadget_ep_cmd_params {
75 u32 param2;
76 u32 param1;
77 u32 param0;
78};
79
80/* -------------------------------------------------------------------------- */
81
82struct dwc3_request {
83 struct usb_request request;
84 struct list_head list;
85 struct dwc3_ep *dep;
86
87 u8 epnum;
88 struct dwc3_trb_hw *trb;
89 dma_addr_t trb_dma;
90
91 unsigned direction:1;
92 unsigned mapped:1;
93 unsigned queued:1;
94};
95#define to_dwc3_request(r) (container_of(r, struct dwc3_request, request))
96
97static inline struct dwc3_request *next_request(struct list_head *list)
98{
99 if (list_empty(list))
100 return NULL;
101
102 return list_first_entry(list, struct dwc3_request, list);
103}
104
105static inline void dwc3_gadget_move_request_queued(struct dwc3_request *req)
106{
107 struct dwc3_ep *dep = req->dep;
108
109 req->queued = true;
110 list_move_tail(&req->list, &dep->req_queued);
111}
112
113#if defined(CONFIG_USB_GADGET_DWC3) || defined(CONFIG_USB_GADGET_DWC3_MODULE)
114int dwc3_gadget_init(struct dwc3 *dwc);
115void dwc3_gadget_exit(struct dwc3 *dwc);
116#else
117static inline int dwc3_gadget_init(struct dwc3 *dwc) { return 0; }
118static inline void dwc3_gadget_exit(struct dwc3 *dwc) { }
119static inline int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
120 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
121{
122 return 0;
123}
124#endif
125
126void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
127 int status);
128
129void dwc3_ep0_interrupt(struct dwc3 *dwc, const struct dwc3_event_depevt *event);
130void dwc3_ep0_out_start(struct dwc3 *dwc);
131int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
132 gfp_t gfp_flags);
133int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value);
134int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
135 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params);
136void dwc3_map_buffer_to_dma(struct dwc3_request *req);
137void dwc3_unmap_buffer_from_dma(struct dwc3_request *req);
138
139/**
140 * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW
141 * @dwc: DesignWare USB3 Pointer
142 * @number: DWC endpoint number
143 *
144 * Caller should take care of locking
145 */
146static inline u32 dwc3_gadget_ep_get_transfer_index(struct dwc3 *dwc, u8 number)
147{
148 u32 res_id;
149
150 res_id = dwc3_readl(dwc->regs, DWC3_DEPCMD(number));
151
152 return DWC3_DEPCMD_GET_RSC_IDX(res_id);
153}
154
155/**
156 * dwc3_gadget_event_string - returns event name
157 * @event: the event code
158 */
159static inline const char *dwc3_gadget_event_string(u8 event)
160{
161 switch (event) {
162 case DWC3_DEVICE_EVENT_DISCONNECT:
163 return "Disconnect";
164 case DWC3_DEVICE_EVENT_RESET:
165 return "Reset";
166 case DWC3_DEVICE_EVENT_CONNECT_DONE:
167 return "Connection Done";
168 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
169 return "Link Status Change";
170 case DWC3_DEVICE_EVENT_WAKEUP:
171 return "WakeUp";
172 case DWC3_DEVICE_EVENT_EOPF:
173 return "End-Of-Frame";
174 case DWC3_DEVICE_EVENT_SOF:
175 return "Start-Of-Frame";
176 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
177 return "Erratic Error";
178 case DWC3_DEVICE_EVENT_CMD_CMPL:
179 return "Command Complete";
180 case DWC3_DEVICE_EVENT_OVERFLOW:
181 return "Overflow";
182 }
183
184 return "UNKNOWN";
185}
186
187/**
188 * dwc3_ep_event_string - returns event name
189 * @event: then event code
190 */
191static inline const char *dwc3_ep_event_string(u8 event)
192{
193 switch (event) {
194 case DWC3_DEPEVT_XFERCOMPLETE:
195 return "Transfer Complete";
196 case DWC3_DEPEVT_XFERINPROGRESS:
197 return "Transfer In-Progress";
198 case DWC3_DEPEVT_XFERNOTREADY:
199 return "Transfer Not Ready";
200 case DWC3_DEPEVT_RXTXFIFOEVT:
201 return "FIFO";
202 case DWC3_DEPEVT_STREAMEVT:
203 return "Stream";
204 case DWC3_DEPEVT_EPCMDCMPLT:
205 return "Endpoint Command Complete";
206 }
207
208 return "UNKNOWN";
209}
210
211#endif /* __DRIVERS_USB_DWC3_GADGET_H */
diff --git a/drivers/usb/dwc3/io.h b/drivers/usb/dwc3/io.h
new file mode 100644
index 000000000000..bc957db1ea4b
--- /dev/null
+++ b/drivers/usb/dwc3/io.h
@@ -0,0 +1,54 @@
1/**
2 * io.h - DesignWare USB3 DRD IO Header
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef __DRIVERS_USB_DWC3_IO_H
40#define __DRIVERS_USB_DWC3_IO_H
41
42#include <asm/io.h>
43
44static inline u32 dwc3_readl(void __iomem *base, u32 offset)
45{
46 return readl(base + offset);
47}
48
49static inline void dwc3_writel(void __iomem *base, u32 offset, u32 value)
50{
51 writel(value, base + offset);
52}
53
54#endif /* __DRIVERS_USB_DWC3_IO_H */
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 5a084b9cfa3c..23a447373c51 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -96,6 +96,22 @@ config USB_GADGET_VBUS_DRAW
96 This value will be used except for system-specific gadget 96 This value will be used except for system-specific gadget
97 drivers that have more specific information. 97 drivers that have more specific information.
98 98
99config USB_GADGET_STORAGE_NUM_BUFFERS
100 int "Number of storage pipeline buffers"
101 range 2 4
102 default 2
103 help
104 Usually 2 buffers are enough to establish a good buffering
105 pipeline. The number may be increased in order to compensate
106 for a bursty VFS behaviour. For instance there may be CPU wake up
107 latencies that makes the VFS to appear bursty in a system with
108 an CPU on-demand governor. Especially if DMA is doing IO to
109 offload the CPU. In this case the CPU will go into power
110 save often and spin up occasionally to move data within VFS.
111 If selecting USB_GADGET_DEBUG_FILES this value may be set by
112 a module parameter as well.
113 If unsure, say 2.
114
99# 115#
100# USB Peripheral Controller Support 116# USB Peripheral Controller Support
101# 117#
@@ -255,12 +271,11 @@ config USB_S3C_HSOTG
255 integrated into the S3C64XX series SoC. 271 integrated into the S3C64XX series SoC.
256 272
257config USB_IMX 273config USB_IMX
258 tristate "Freescale IMX USB Peripheral Controller" 274 tristate "Freescale i.MX1 USB Peripheral Controller"
259 depends on ARCH_MX1 275 depends on ARCH_MXC
260 help 276 help
261 Freescale's IMX series include an integrated full speed 277 Freescale's i.MX1 includes an integrated full speed
262 USB 1.1 device controller. The controller in the IMX series 278 USB 1.1 device controller.
263 is register-compatible.
264 279
265 It has Six fixed-function endpoints, as well as endpoint 280 It has Six fixed-function endpoints, as well as endpoint
266 zero (for control transfers). 281 zero (for control transfers).
@@ -303,6 +318,18 @@ config USB_PXA_U2O
303 PXA9xx Processor series include a high speed USB2.0 device 318 PXA9xx Processor series include a high speed USB2.0 device
304 controller, which support high speed and full speed USB peripheral. 319 controller, which support high speed and full speed USB peripheral.
305 320
321config USB_GADGET_DWC3
322 tristate "DesignWare USB3.0 (DRD) Controller"
323 depends on USB_DWC3
324 select USB_GADGET_DUALSPEED
325 select USB_GADGET_SUPERSPEED
326 help
327 DesignWare USB3.0 controller is a SuperSpeed USB3.0 Controller
328 which can be configured for peripheral-only, host-only, hub-only
329 and Dual-Role operation. This Controller was first integrated into
330 the OMAP5 series of processors. More information about the OMAP5
331 version of this controller, refer to http://www.ti.com/omap5.
332
306# 333#
307# Controllers available in both integrated and discrete versions 334# Controllers available in both integrated and discrete versions
308# 335#
@@ -442,7 +469,7 @@ config USB_LANGWELL
442 gadget drivers to also be dynamically linked. 469 gadget drivers to also be dynamically linked.
443 470
444config USB_EG20T 471config USB_EG20T
445 tristate "Intel EG20T PCH/OKI SEMICONDUCTOR ML7213 IOH UDC" 472 tristate "Intel EG20T PCH/LAPIS Semiconductor IOH(ML7213/ML7831) UDC"
446 depends on PCI 473 depends on PCI
447 select USB_GADGET_DUALSPEED 474 select USB_GADGET_DUALSPEED
448 help 475 help
@@ -458,10 +485,11 @@ config USB_EG20T
458 This driver dose not support interrupt transfer or isochronous 485 This driver dose not support interrupt transfer or isochronous
459 transfer modes. 486 transfer modes.
460 487
461 This driver also can be used for OKI SEMICONDUCTOR's ML7213 which is 488 This driver also can be used for LAPIS Semiconductor's ML7213 which is
462 for IVI(In-Vehicle Infotainment) use. 489 for IVI(In-Vehicle Infotainment) use.
463 ML7213 is companion chip for Intel Atom E6xx series. 490 ML7831 is for general purpose use.
464 ML7213 is completely compatible for Intel EG20T PCH. 491 ML7213/ML7831 is companion chip for Intel Atom E6xx series.
492 ML7213/ML7831 is completely compatible for Intel EG20T PCH.
465 493
466config USB_CI13XXX_MSM 494config USB_CI13XXX_MSM
467 tristate "MIPS USB CI13xxx for MSM" 495 tristate "MIPS USB CI13xxx for MSM"
@@ -846,6 +874,16 @@ config USB_G_NOKIA
846 It's only really useful for N900 hardware. If you're building 874 It's only really useful for N900 hardware. If you're building
847 a kernel for N900, say Y or M here. If unsure, say N. 875 a kernel for N900, say Y or M here. If unsure, say N.
848 876
877config USB_G_ACM_MS
878 tristate "CDC Composite Device (ACM and mass storage)"
879 depends on BLOCK
880 help
881 This driver provides two functions in one configuration:
882 a mass storage, and a CDC ACM (serial port) link.
883
884 Say "y" to link the driver statically, or "m" to build a
885 dynamically linked module called "g_acm_ms".
886
849config USB_G_MULTI 887config USB_G_MULTI
850 tristate "Multifunction Composite Gadget (EXPERIMENTAL)" 888 tristate "Multifunction Composite Gadget (EXPERIMENTAL)"
851 depends on BLOCK && NET 889 depends on BLOCK && NET
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 9ba725af4a08..b54ac6190890 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -28,7 +28,7 @@ obj-$(CONFIG_USB_S3C_HSUDC) += s3c-hsudc.o
28obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o 28obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o
29obj-$(CONFIG_USB_EG20T) += pch_udc.o 29obj-$(CONFIG_USB_EG20T) += pch_udc.o
30obj-$(CONFIG_USB_PXA_U2O) += mv_udc.o 30obj-$(CONFIG_USB_PXA_U2O) += mv_udc.o
31mv_udc-y := mv_udc_core.o mv_udc_phy.o 31mv_udc-y := mv_udc_core.o
32obj-$(CONFIG_USB_CI13XXX_MSM) += ci13xxx_msm.o 32obj-$(CONFIG_USB_CI13XXX_MSM) += ci13xxx_msm.o
33obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o 33obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
34 34
@@ -51,6 +51,7 @@ g_dbgp-y := dbgp.o
51g_nokia-y := nokia.o 51g_nokia-y := nokia.o
52g_webcam-y := webcam.o 52g_webcam-y := webcam.o
53g_ncm-y := ncm.o 53g_ncm-y := ncm.o
54g_acm_ms-y := acm_ms.o
54 55
55obj-$(CONFIG_USB_ZERO) += g_zero.o 56obj-$(CONFIG_USB_ZERO) += g_zero.o
56obj-$(CONFIG_USB_AUDIO) += g_audio.o 57obj-$(CONFIG_USB_AUDIO) += g_audio.o
@@ -69,3 +70,4 @@ obj-$(CONFIG_USB_G_MULTI) += g_multi.o
69obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o 70obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
70obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o 71obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
71obj-$(CONFIG_USB_G_NCM) += g_ncm.o 72obj-$(CONFIG_USB_G_NCM) += g_ncm.o
73obj-$(CONFIG_USB_G_ACM_MS) += g_acm_ms.o
diff --git a/drivers/usb/gadget/acm_ms.c b/drivers/usb/gadget/acm_ms.c
new file mode 100644
index 000000000000..fdb7aec3bd0c
--- /dev/null
+++ b/drivers/usb/gadget/acm_ms.c
@@ -0,0 +1,256 @@
1/*
2 * acm_ms.c -- Composite driver, with ACM and mass storage support
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: David Brownell
7 * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de>
8 *
9 * Heavily based on multi.c and cdc2.c
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 as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/kernel.h>
18#include <linux/utsname.h>
19
20#include "u_serial.h"
21
22#define DRIVER_DESC "Composite Gadget (ACM + MS)"
23#define DRIVER_VERSION "2011/10/10"
24
25/*-------------------------------------------------------------------------*/
26
27/*
28 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
29 * Instead: allocate your own, using normal USB-IF procedures.
30 */
31#define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */
32#define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/
33
34/*-------------------------------------------------------------------------*/
35
36/*
37 * Kbuild is not very cooperative with respect to linking separately
38 * compiled library objects into one module. So for now we won't use
39 * separate compilation ... ensuring init/exit sections work to shrink
40 * the runtime footprint, and giving us at least some parts of what
41 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
42 */
43
44#include "composite.c"
45#include "usbstring.c"
46#include "config.c"
47#include "epautoconf.c"
48#include "u_serial.c"
49#include "f_acm.c"
50#include "f_mass_storage.c"
51
52/*-------------------------------------------------------------------------*/
53
54static struct usb_device_descriptor device_desc = {
55 .bLength = sizeof device_desc,
56 .bDescriptorType = USB_DT_DEVICE,
57
58 .bcdUSB = cpu_to_le16(0x0200),
59
60 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
61 .bDeviceSubClass = 2,
62 .bDeviceProtocol = 1,
63
64 /* .bMaxPacketSize0 = f(hardware) */
65
66 /* Vendor and product id can be overridden by module parameters. */
67 .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
68 .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
69 /* .bcdDevice = f(hardware) */
70 /* .iManufacturer = DYNAMIC */
71 /* .iProduct = DYNAMIC */
72 /* NO SERIAL NUMBER */
73 /*.bNumConfigurations = DYNAMIC*/
74};
75
76static struct usb_otg_descriptor otg_descriptor = {
77 .bLength = sizeof otg_descriptor,
78 .bDescriptorType = USB_DT_OTG,
79
80 /*
81 * REVISIT SRP-only hardware is possible, although
82 * it would not be called "OTG" ...
83 */
84 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
85};
86
87static const struct usb_descriptor_header *otg_desc[] = {
88 (struct usb_descriptor_header *) &otg_descriptor,
89 NULL,
90};
91
92
93/* string IDs are assigned dynamically */
94
95#define STRING_MANUFACTURER_IDX 0
96#define STRING_PRODUCT_IDX 1
97
98static char manufacturer[50];
99
100static struct usb_string strings_dev[] = {
101 [STRING_MANUFACTURER_IDX].s = manufacturer,
102 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
103 { } /* end of list */
104};
105
106static struct usb_gadget_strings stringtab_dev = {
107 .language = 0x0409, /* en-us */
108 .strings = strings_dev,
109};
110
111static struct usb_gadget_strings *dev_strings[] = {
112 &stringtab_dev,
113 NULL,
114};
115
116/****************************** Configurations ******************************/
117
118static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
119FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
120
121static struct fsg_common fsg_common;
122
123/*-------------------------------------------------------------------------*/
124
125/*
126 * We _always_ have both ACM and mass storage functions.
127 */
128static int __init acm_ms_do_config(struct usb_configuration *c)
129{
130 int status;
131
132 if (gadget_is_otg(c->cdev->gadget)) {
133 c->descriptors = otg_desc;
134 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
135 }
136
137
138 status = acm_bind_config(c, 0);
139 if (status < 0)
140 return status;
141
142 status = fsg_bind_config(c->cdev, c, &fsg_common);
143 if (status < 0)
144 return status;
145
146 return 0;
147}
148
149static struct usb_configuration acm_ms_config_driver = {
150 .label = DRIVER_DESC,
151 .bConfigurationValue = 1,
152 /* .iConfiguration = DYNAMIC */
153 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
154};
155
156/*-------------------------------------------------------------------------*/
157
158static int __init acm_ms_bind(struct usb_composite_dev *cdev)
159{
160 int gcnum;
161 struct usb_gadget *gadget = cdev->gadget;
162 int status;
163 void *retp;
164
165 /* set up serial link layer */
166 status = gserial_setup(cdev->gadget, 1);
167 if (status < 0)
168 return status;
169
170 /* set up mass storage function */
171 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
172 if (IS_ERR(retp)) {
173 status = PTR_ERR(retp);
174 goto fail0;
175 }
176
177 /* set bcdDevice */
178 gcnum = usb_gadget_controller_number(gadget);
179 if (gcnum >= 0) {
180 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
181 } else {
182 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
183 gadget->name,
184 acm_ms_config_driver.label);
185 device_desc.bcdDevice =
186 cpu_to_le16(0x0300 | 0x0099);
187 }
188
189 /*
190 * Allocate string descriptor numbers ... note that string
191 * contents can be overridden by the composite_dev glue.
192 */
193
194 /* device descriptor strings: manufacturer, product */
195 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
196 init_utsname()->sysname, init_utsname()->release,
197 gadget->name);
198 status = usb_string_id(cdev);
199 if (status < 0)
200 goto fail1;
201 strings_dev[STRING_MANUFACTURER_IDX].id = status;
202 device_desc.iManufacturer = status;
203
204 status = usb_string_id(cdev);
205 if (status < 0)
206 goto fail1;
207 strings_dev[STRING_PRODUCT_IDX].id = status;
208 device_desc.iProduct = status;
209
210 /* register our configuration */
211 status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
212 if (status < 0)
213 goto fail1;
214
215 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
216 DRIVER_DESC);
217 fsg_common_put(&fsg_common);
218 return 0;
219
220 /* error recovery */
221fail1:
222 fsg_common_put(&fsg_common);
223fail0:
224 gserial_cleanup();
225 return status;
226}
227
228static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
229{
230 gserial_cleanup();
231
232 return 0;
233}
234
235static struct usb_composite_driver acm_ms_driver = {
236 .name = "g_acm_ms",
237 .dev = &device_desc,
238 .strings = dev_strings,
239 .unbind = __exit_p(acm_ms_unbind),
240};
241
242MODULE_DESCRIPTION(DRIVER_DESC);
243MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
244MODULE_LICENSE("GPL v2");
245
246static int __init init(void)
247{
248 return usb_composite_probe(&acm_ms_driver, acm_ms_bind);
249}
250module_init(init);
251
252static void __exit cleanup(void)
253{
254 usb_composite_unregister(&acm_ms_driver);
255}
256module_exit(cleanup);
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index 70f2b376c86d..4730016d7cd4 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22/* 13/*
@@ -354,7 +345,7 @@ udc_ep_enable(struct usb_ep *usbep, const struct usb_endpoint_descriptor *desc)
354 writel(tmp, &dev->ep[ep->num].regs->ctl); 345 writel(tmp, &dev->ep[ep->num].regs->ctl);
355 346
356 /* set max packet size */ 347 /* set max packet size */
357 maxpacket = le16_to_cpu(desc->wMaxPacketSize); 348 maxpacket = usb_endpoint_maxp(desc);
358 tmp = readl(&dev->ep[ep->num].regs->bufout_maxpkt); 349 tmp = readl(&dev->ep[ep->num].regs->bufout_maxpkt);
359 tmp = AMD_ADDBITS(tmp, maxpacket, UDC_EP_MAX_PKT_SIZE); 350 tmp = AMD_ADDBITS(tmp, maxpacket, UDC_EP_MAX_PKT_SIZE);
360 ep->ep.maxpacket = maxpacket; 351 ep->ep.maxpacket = maxpacket;
@@ -3014,13 +3005,8 @@ __acquires(dev->lock)
3014 3005
3015 /* link up all endpoints */ 3006 /* link up all endpoints */
3016 udc_setup_endpoints(dev); 3007 udc_setup_endpoints(dev);
3017 if (dev->gadget.speed == USB_SPEED_HIGH) { 3008 dev_info(&dev->pdev->dev, "Connect: %s\n",
3018 dev_info(&dev->pdev->dev, "Connect: speed = %s\n", 3009 usb_speed_string(dev->gadget.speed));
3019 "high");
3020 } else if (dev->gadget.speed == USB_SPEED_FULL) {
3021 dev_info(&dev->pdev->dev, "Connect: speed = %s\n",
3022 "full");
3023 }
3024 3010
3025 /* init ep 0 */ 3011 /* init ep 0 */
3026 activate_control_endpoints(dev); 3012 activate_control_endpoints(dev);
diff --git a/drivers/usb/gadget/amd5536udc.h b/drivers/usb/gadget/amd5536udc.h
index 1d1c7543468e..f87e29c65325 100644
--- a/drivers/usb/gadget/amd5536udc.h
+++ b/drivers/usb/gadget/amd5536udc.h
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22#ifndef AMD5536UDC_H 13#ifndef AMD5536UDC_H
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index ddb118a76807..8efe0fa9228d 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -9,16 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */ 12 */
23 13
24#undef VERBOSE_DEBUG 14#undef VERBOSE_DEBUG
@@ -460,7 +450,7 @@ static void nuke(struct at91_ep *ep, int status)
460{ 450{
461 struct at91_request *req; 451 struct at91_request *req;
462 452
463 // terminer chaque requete dans la queue 453 /* terminate any request in the queue */
464 ep->stopped = 1; 454 ep->stopped = 1;
465 if (list_empty(&ep->queue)) 455 if (list_empty(&ep->queue))
466 return; 456 return;
@@ -487,7 +477,7 @@ static int at91_ep_enable(struct usb_ep *_ep,
487 || !desc || ep->desc 477 || !desc || ep->desc
488 || _ep->name == ep0name 478 || _ep->name == ep0name
489 || desc->bDescriptorType != USB_DT_ENDPOINT 479 || desc->bDescriptorType != USB_DT_ENDPOINT
490 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0 480 || (maxpacket = usb_endpoint_maxp(desc)) == 0
491 || maxpacket > ep->maxpacket) { 481 || maxpacket > ep->maxpacket) {
492 DBG("bad ep or descriptor\n"); 482 DBG("bad ep or descriptor\n");
493 return -EINVAL; 483 return -EINVAL;
@@ -788,7 +778,7 @@ static const struct usb_ep_ops at91_ep_ops = {
788 .queue = at91_ep_queue, 778 .queue = at91_ep_queue,
789 .dequeue = at91_ep_dequeue, 779 .dequeue = at91_ep_dequeue,
790 .set_halt = at91_ep_set_halt, 780 .set_halt = at91_ep_set_halt,
791 // there's only imprecise fifo status reporting 781 /* there's only imprecise fifo status reporting */
792}; 782};
793 783
794/*-------------------------------------------------------------------------*/ 784/*-------------------------------------------------------------------------*/
@@ -846,7 +836,7 @@ static void udc_reinit(struct at91_udc *udc)
846 ep->fifo_bank = 0; 836 ep->fifo_bank = 0;
847 ep->ep.maxpacket = ep->maxpacket; 837 ep->ep.maxpacket = ep->maxpacket;
848 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i); 838 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
849 // initialiser une queue par endpoint 839 /* initialize one queue per endpoint */
850 INIT_LIST_HEAD(&ep->queue); 840 INIT_LIST_HEAD(&ep->queue);
851 } 841 }
852} 842}
@@ -952,7 +942,7 @@ static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
952 struct at91_udc *udc = to_udc(gadget); 942 struct at91_udc *udc = to_udc(gadget);
953 unsigned long flags; 943 unsigned long flags;
954 944
955 // VDBG("vbus %s\n", is_active ? "on" : "off"); 945 /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
956 spin_lock_irqsave(&udc->lock, flags); 946 spin_lock_irqsave(&udc->lock, flags);
957 udc->vbus = (is_active != 0); 947 udc->vbus = (is_active != 0);
958 if (udc->driver) 948 if (udc->driver)
@@ -1003,7 +993,7 @@ static const struct usb_gadget_ops at91_udc_ops = {
1003 * VBUS-powered devices may also also want to support bigger 993 * VBUS-powered devices may also also want to support bigger
1004 * power budgets after an appropriate SET_CONFIGURATION. 994 * power budgets after an appropriate SET_CONFIGURATION.
1005 */ 995 */
1006 // .vbus_power = at91_vbus_power, 996 /* .vbus_power = at91_vbus_power, */
1007}; 997};
1008 998
1009/*-------------------------------------------------------------------------*/ 999/*-------------------------------------------------------------------------*/
@@ -1072,7 +1062,7 @@ static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1072 ep->is_in = 0; 1062 ep->is_in = 0;
1073 } 1063 }
1074 } else { 1064 } else {
1075 // REVISIT this happens sometimes under load; why?? 1065 /* REVISIT this happens sometimes under load; why?? */
1076 ERR("SETUP len %d, csr %08x\n", rxcount, csr); 1066 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1077 status = -EINVAL; 1067 status = -EINVAL;
1078 } 1068 }
@@ -1451,7 +1441,7 @@ static irqreturn_t at91_udc_irq (int irq, void *_udc)
1451 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP); 1441 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1452 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM); 1442 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1453 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP); 1443 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1454 // VDBG("bus suspend\n"); 1444 /* VDBG("bus suspend\n"); */
1455 if (udc->suspended) 1445 if (udc->suspended)
1456 continue; 1446 continue;
1457 udc->suspended = 1; 1447 udc->suspended = 1;
@@ -1473,7 +1463,7 @@ static irqreturn_t at91_udc_irq (int irq, void *_udc)
1473 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM); 1463 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1474 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP); 1464 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1475 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM); 1465 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1476 // VDBG("bus resume\n"); 1466 /* VDBG("bus resume\n"); */
1477 if (!udc->suspended) 1467 if (!udc->suspended)
1478 continue; 1468 continue;
1479 udc->suspended = 0; 1469 udc->suspended = 0;
@@ -1820,7 +1810,7 @@ static int __init at91udc_probe(struct platform_device *pdev)
1820 /* request UDC and maybe VBUS irqs */ 1810 /* request UDC and maybe VBUS irqs */
1821 udc->udp_irq = platform_get_irq(pdev, 0); 1811 udc->udp_irq = platform_get_irq(pdev, 0);
1822 retval = request_irq(udc->udp_irq, at91_udc_irq, 1812 retval = request_irq(udc->udp_irq, at91_udc_irq,
1823 IRQF_DISABLED, driver_name, udc); 1813 0, driver_name, udc);
1824 if (retval < 0) { 1814 if (retval < 0) {
1825 DBG("request irq %d failed\n", udc->udp_irq); 1815 DBG("request irq %d failed\n", udc->udp_irq);
1826 goto fail1; 1816 goto fail1;
@@ -1848,7 +1838,7 @@ static int __init at91udc_probe(struct platform_device *pdev)
1848 jiffies + VBUS_POLL_TIMEOUT); 1838 jiffies + VBUS_POLL_TIMEOUT);
1849 } else { 1839 } else {
1850 if (request_irq(udc->board.vbus_pin, at91_vbus_irq, 1840 if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
1851 IRQF_DISABLED, driver_name, udc)) { 1841 0, driver_name, udc)) {
1852 DBG("request vbus irq %d failed\n", 1842 DBG("request vbus irq %d failed\n",
1853 udc->board.vbus_pin); 1843 udc->board.vbus_pin);
1854 retval = -EBUSY; 1844 retval = -EBUSY;
diff --git a/drivers/usb/gadget/at91_udc.h b/drivers/usb/gadget/at91_udc.h
index 108ca54f9092..3c0315b86ace 100644
--- a/drivers/usb/gadget/at91_udc.h
+++ b/drivers/usb/gadget/at91_udc.h
@@ -7,16 +7,6 @@
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */ 10 */
21 11
22#ifndef AT91_UDC_H 12#ifndef AT91_UDC_H
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 5b1665eb1bef..271a9d873608 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -527,7 +527,7 @@ usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
527 527
528 DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc); 528 DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc);
529 529
530 maxpacket = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff; 530 maxpacket = usb_endpoint_maxp(desc) & 0x7ff;
531 531
532 if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index) 532 if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index)
533 || ep->index == 0 533 || ep->index == 0
@@ -571,7 +571,7 @@ usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
571 * Bits 11:12 specify number of _additional_ 571 * Bits 11:12 specify number of _additional_
572 * transactions per microframe. 572 * transactions per microframe.
573 */ 573 */
574 nr_trans = ((le16_to_cpu(desc->wMaxPacketSize) >> 11) & 3) + 1; 574 nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1;
575 if (nr_trans > 3) 575 if (nr_trans > 3)
576 return -EINVAL; 576 return -EINVAL;
577 577
@@ -1718,13 +1718,12 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1718 spin_lock(&udc->lock); 1718 spin_lock(&udc->lock);
1719 } 1719 }
1720 1720
1721 if (status & USBA_HIGH_SPEED) { 1721 if (status & USBA_HIGH_SPEED)
1722 DBG(DBG_BUS, "High-speed bus reset detected\n");
1723 udc->gadget.speed = USB_SPEED_HIGH; 1722 udc->gadget.speed = USB_SPEED_HIGH;
1724 } else { 1723 else
1725 DBG(DBG_BUS, "Full-speed bus reset detected\n");
1726 udc->gadget.speed = USB_SPEED_FULL; 1724 udc->gadget.speed = USB_SPEED_FULL;
1727 } 1725 DBG(DBG_BUS, "%s bus reset detected\n",
1726 usb_speed_string(udc->gadget.speed));
1728 1727
1729 ep0 = &usba_ep[0]; 1728 ep0 = &usba_ep[0];
1730 ep0->desc = &usba_ep0_desc; 1729 ep0->desc = &usba_ep0_desc;
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c
index b1c1afbb8750..725550f06fab 100644
--- a/drivers/usb/gadget/cdc2.c
+++ b/drivers/usb/gadget/cdc2.c
@@ -8,19 +8,11 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22#include <linux/kernel.h> 13#include <linux/kernel.h>
23#include <linux/utsname.h> 14#include <linux/utsname.h>
15#include <linux/module.h>
24 16
25#include "u_ether.h" 17#include "u_ether.h"
26#include "u_serial.h" 18#include "u_serial.h"
diff --git a/drivers/usb/gadget/ci13xxx_msm.c b/drivers/usb/gadget/ci13xxx_msm.c
index 470981ad6f77..1fc612914c52 100644
--- a/drivers/usb/gadget/ci13xxx_msm.c
+++ b/drivers/usb/gadget/ci13xxx_msm.c
@@ -3,17 +3,6 @@
3 * This program is free software; you can redistribute it and/or modify 3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and 4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation. 5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 */ 6 */
18 7
19#include <linux/module.h> 8#include <linux/module.h>
@@ -133,3 +122,5 @@ static int __init ci13xxx_msm_init(void)
133 return platform_driver_register(&ci13xxx_msm_driver); 122 return platform_driver_register(&ci13xxx_msm_driver);
134} 123}
135module_init(ci13xxx_msm_init); 124module_init(ci13xxx_msm_init);
125
126MODULE_LICENSE("GPL v2");
diff --git a/drivers/usb/gadget/ci13xxx_udc.c b/drivers/usb/gadget/ci13xxx_udc.c
index 1265a8502ea0..9a0c3979ff43 100644
--- a/drivers/usb/gadget/ci13xxx_udc.c
+++ b/drivers/usb/gadget/ci13xxx_udc.c
@@ -71,6 +71,9 @@
71/****************************************************************************** 71/******************************************************************************
72 * DEFINE 72 * DEFINE
73 *****************************************************************************/ 73 *****************************************************************************/
74
75#define DMA_ADDR_INVALID (~(dma_addr_t)0)
76
74/* ctrl register bank access */ 77/* ctrl register bank access */
75static DEFINE_SPINLOCK(udc_lock); 78static DEFINE_SPINLOCK(udc_lock);
76 79
@@ -1434,7 +1437,7 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1434 return -EALREADY; 1437 return -EALREADY;
1435 1438
1436 mReq->req.status = -EALREADY; 1439 mReq->req.status = -EALREADY;
1437 if (length && !mReq->req.dma) { 1440 if (length && mReq->req.dma == DMA_ADDR_INVALID) {
1438 mReq->req.dma = \ 1441 mReq->req.dma = \
1439 dma_map_single(mEp->device, mReq->req.buf, 1442 dma_map_single(mEp->device, mReq->req.buf,
1440 length, mEp->dir ? DMA_TO_DEVICE : 1443 length, mEp->dir ? DMA_TO_DEVICE :
@@ -1453,7 +1456,7 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1453 dma_unmap_single(mEp->device, mReq->req.dma, 1456 dma_unmap_single(mEp->device, mReq->req.dma,
1454 length, mEp->dir ? DMA_TO_DEVICE : 1457 length, mEp->dir ? DMA_TO_DEVICE :
1455 DMA_FROM_DEVICE); 1458 DMA_FROM_DEVICE);
1456 mReq->req.dma = 0; 1459 mReq->req.dma = DMA_ADDR_INVALID;
1457 mReq->map = 0; 1460 mReq->map = 0;
1458 } 1461 }
1459 return -ENOMEM; 1462 return -ENOMEM;
@@ -1549,7 +1552,7 @@ static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1549 if (mReq->map) { 1552 if (mReq->map) {
1550 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length, 1553 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
1551 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1554 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1552 mReq->req.dma = 0; 1555 mReq->req.dma = DMA_ADDR_INVALID;
1553 mReq->map = 0; 1556 mReq->map = 0;
1554 } 1557 }
1555 1558
@@ -1610,7 +1613,6 @@ __acquires(mEp->lock)
1610 * @gadget: gadget 1613 * @gadget: gadget
1611 * 1614 *
1612 * This function returns an error code 1615 * This function returns an error code
1613 * Caller must hold lock
1614 */ 1616 */
1615static int _gadget_stop_activity(struct usb_gadget *gadget) 1617static int _gadget_stop_activity(struct usb_gadget *gadget)
1616{ 1618{
@@ -2101,7 +2103,7 @@ static int ep_enable(struct usb_ep *ep,
2101 mEp->num = usb_endpoint_num(desc); 2103 mEp->num = usb_endpoint_num(desc);
2102 mEp->type = usb_endpoint_type(desc); 2104 mEp->type = usb_endpoint_type(desc);
2103 2105
2104 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize); 2106 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
2105 2107
2106 dbg_event(_usb_addr(mEp), "ENABLE", 0); 2108 dbg_event(_usb_addr(mEp), "ENABLE", 0);
2107 2109
@@ -2189,6 +2191,7 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2189 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); 2191 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2190 if (mReq != NULL) { 2192 if (mReq != NULL) {
2191 INIT_LIST_HEAD(&mReq->queue); 2193 INIT_LIST_HEAD(&mReq->queue);
2194 mReq->req.dma = DMA_ADDR_INVALID;
2192 2195
2193 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, 2196 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
2194 &mReq->dma); 2197 &mReq->dma);
@@ -2328,7 +2331,7 @@ static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2328 if (mReq->map) { 2331 if (mReq->map) {
2329 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length, 2332 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
2330 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 2333 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2331 mReq->req.dma = 0; 2334 mReq->req.dma = DMA_ADDR_INVALID;
2332 mReq->map = 0; 2335 mReq->map = 0;
2333 } 2336 }
2334 req->status = -ECONNRESET; 2337 req->status = -ECONNRESET;
@@ -2500,12 +2503,12 @@ static int ci13xxx_wakeup(struct usb_gadget *_gadget)
2500 spin_lock_irqsave(udc->lock, flags); 2503 spin_lock_irqsave(udc->lock, flags);
2501 if (!udc->remote_wakeup) { 2504 if (!udc->remote_wakeup) {
2502 ret = -EOPNOTSUPP; 2505 ret = -EOPNOTSUPP;
2503 dbg_trace("remote wakeup feature is not enabled\n"); 2506 trace("remote wakeup feature is not enabled\n");
2504 goto out; 2507 goto out;
2505 } 2508 }
2506 if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) { 2509 if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) {
2507 ret = -EINVAL; 2510 ret = -EINVAL;
2508 dbg_trace("port is not suspended\n"); 2511 trace("port is not suspended\n");
2509 goto out; 2512 goto out;
2510 } 2513 }
2511 hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR); 2514 hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR);
@@ -2703,7 +2706,9 @@ static int ci13xxx_stop(struct usb_gadget_driver *driver)
2703 if (udc->udc_driver->notify_event) 2706 if (udc->udc_driver->notify_event)
2704 udc->udc_driver->notify_event(udc, 2707 udc->udc_driver->notify_event(udc,
2705 CI13XXX_CONTROLLER_STOPPED_EVENT); 2708 CI13XXX_CONTROLLER_STOPPED_EVENT);
2709 spin_unlock_irqrestore(udc->lock, flags);
2706 _gadget_stop_activity(&udc->gadget); 2710 _gadget_stop_activity(&udc->gadget);
2711 spin_lock_irqsave(udc->lock, flags);
2707 pm_runtime_put(&udc->gadget.dev); 2712 pm_runtime_put(&udc->gadget.dev);
2708 } 2713 }
2709 2714
@@ -2850,7 +2855,7 @@ static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2850 struct ci13xxx *udc; 2855 struct ci13xxx *udc;
2851 int retval = 0; 2856 int retval = 0;
2852 2857
2853 trace("%p, %p, %p", dev, regs, name); 2858 trace("%p, %p, %p", dev, regs, driver->name);
2854 2859
2855 if (dev == NULL || regs == NULL || driver == NULL || 2860 if (dev == NULL || regs == NULL || driver == NULL ||
2856 driver->name == NULL) 2861 driver->name == NULL)
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index aef47414f5d5..f71b0787983f 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -7,15 +7,6 @@
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 10 */
20 11
21/* #define VERBOSE_DEBUG */ 12/* #define VERBOSE_DEBUG */
@@ -23,6 +14,7 @@
23#include <linux/kallsyms.h> 14#include <linux/kallsyms.h>
24#include <linux/kernel.h> 15#include <linux/kernel.h>
25#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/module.h>
26#include <linux/device.h> 18#include <linux/device.h>
27#include <linux/utsname.h> 19#include <linux/utsname.h>
28 20
@@ -164,7 +156,7 @@ int config_ep_by_speed(struct usb_gadget *g,
164 156
165ep_found: 157ep_found:
166 /* commit results */ 158 /* commit results */
167 _ep->maxpacket = le16_to_cpu(chosen_desc->wMaxPacketSize); 159 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
168 _ep->desc = chosen_desc; 160 _ep->desc = chosen_desc;
169 _ep->comp_desc = NULL; 161 _ep->comp_desc = NULL;
170 _ep->maxburst = 0; 162 _ep->maxburst = 0;
@@ -551,9 +543,9 @@ static int bos_desc(struct usb_composite_dev *cdev)
551 if (cdev->gadget->ops->get_config_params) 543 if (cdev->gadget->ops->get_config_params)
552 cdev->gadget->ops->get_config_params(&dcd_config_params); 544 cdev->gadget->ops->get_config_params(&dcd_config_params);
553 else { 545 else {
554 dcd_config_params.bU1devExitLat = USB_DEFULT_U1_DEV_EXIT_LAT; 546 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
555 dcd_config_params.bU2DevExitLat = 547 dcd_config_params.bU2DevExitLat =
556 cpu_to_le16(USB_DEFULT_U2_DEV_EXIT_LAT); 548 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
557 } 549 }
558 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; 550 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
559 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; 551 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
@@ -626,25 +618,9 @@ static int set_config(struct usb_composite_dev *cdev,
626 result = 0; 618 result = 0;
627 } 619 }
628 620
629 INFO(cdev, "%s speed config #%d: %s\n", 621 INFO(cdev, "%s config #%d: %s\n",
630 ({ char *speed; 622 usb_speed_string(gadget->speed),
631 switch (gadget->speed) { 623 number, c ? c->label : "unconfigured");
632 case USB_SPEED_LOW:
633 speed = "low";
634 break;
635 case USB_SPEED_FULL:
636 speed = "full";
637 break;
638 case USB_SPEED_HIGH:
639 speed = "high";
640 break;
641 case USB_SPEED_SUPER:
642 speed = "super";
643 break;
644 default:
645 speed = "?";
646 break;
647 } ; speed; }), number, c ? c->label : "unconfigured");
648 624
649 if (!c) 625 if (!c)
650 goto done; 626 goto done;
diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c
index b2c001334876..7542a72ce51a 100644
--- a/drivers/usb/gadget/config.c
+++ b/drivers/usb/gadget/config.c
@@ -7,15 +7,6 @@
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 10 */
20 11
21#include <linux/errno.h> 12#include <linux/errno.h>
diff --git a/drivers/usb/gadget/dbgp.c b/drivers/usb/gadget/dbgp.c
index 8beefdd36787..6256420089f3 100644
--- a/drivers/usb/gadget/dbgp.c
+++ b/drivers/usb/gadget/dbgp.c
@@ -4,12 +4,12 @@
4 * Copyright (C) 2010 Stephane Duverger 4 * Copyright (C) 2010 Stephane Duverger
5 * 5 *
6 * Released under the GPLv2. 6 * Released under the GPLv2.
7 *
8 */ 7 */
9 8
10/* verbose messages */ 9/* verbose messages */
11#include <linux/kernel.h> 10#include <linux/kernel.h>
12#include <linux/device.h> 11#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/usb/ch9.h> 13#include <linux/usb/ch9.h>
14#include <linux/usb/gadget.h> 14#include <linux/usb/gadget.h>
15 15
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index e755a9d267fc..ab8f1b488d54 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -10,15 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 13 */
23 14
24 15
@@ -439,7 +430,7 @@ dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
439 * maximum packet size. 430 * maximum packet size.
440 * For SS devices the wMaxPacketSize is limited by 1024. 431 * For SS devices the wMaxPacketSize is limited by 1024.
441 */ 432 */
442 max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff; 433 max = usb_endpoint_maxp(desc) & 0x7ff;
443 434
444 /* drivers must not request bad settings, since lower levels 435 /* drivers must not request bad settings, since lower levels
445 * (hardware or its drivers) may not check. some endpoints 436 * (hardware or its drivers) may not check. some endpoints
@@ -1277,7 +1268,7 @@ static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1277 int tmp; 1268 int tmp;
1278 1269
1279 /* high bandwidth mode */ 1270 /* high bandwidth mode */
1280 tmp = le16_to_cpu(ep->desc->wMaxPacketSize); 1271 tmp = usb_endpoint_maxp(ep->desc);
1281 tmp = (tmp >> 11) & 0x03; 1272 tmp = (tmp >> 11) & 0x03;
1282 tmp *= 8 /* applies to entire frame */; 1273 tmp *= 8 /* applies to entire frame */;
1283 limit += limit * tmp; 1274 limit += limit * tmp;
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c
index 7a7e6b7e1fd6..596a0b464e61 100644
--- a/drivers/usb/gadget/epautoconf.c
+++ b/drivers/usb/gadget/epautoconf.c
@@ -7,16 +7,6 @@
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */ 10 */
21 11
22#include <linux/kernel.h> 12#include <linux/kernel.h>
@@ -158,7 +148,7 @@ ep_matches (
158 * where it's an output parameter representing the full speed limit. 148 * where it's an output parameter representing the full speed limit.
159 * the usb spec fixes high speed bulk maxpacket at 512 bytes. 149 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
160 */ 150 */
161 max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); 151 max = 0x7ff & usb_endpoint_maxp(desc);
162 switch (type) { 152 switch (type) {
163 case USB_ENDPOINT_XFER_INT: 153 case USB_ENDPOINT_XFER_INT:
164 /* INT: limit 64 bytes full speed, 1024 high/super speed */ 154 /* INT: limit 64 bytes full speed, 1024 high/super speed */
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index aafc84f33e26..0cd764d59351 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23/* #define VERBOSE_DEBUG */ 14/* #define VERBOSE_DEBUG */
diff --git a/drivers/usb/gadget/f_audio.c b/drivers/usb/gadget/f_audio.c
index a9a4eade7e80..ec7ffcd0d0cd 100644
--- a/drivers/usb/gadget/f_audio.c
+++ b/drivers/usb/gadget/f_audio.c
@@ -460,7 +460,7 @@ static int audio_set_endpoint_req(struct usb_function *f,
460 460
461 switch (ctrl->bRequest) { 461 switch (ctrl->bRequest) {
462 case UAC_SET_CUR: 462 case UAC_SET_CUR:
463 value = 0; 463 value = len;
464 break; 464 break;
465 465
466 case UAC_SET_MIN: 466 case UAC_SET_MIN:
@@ -499,7 +499,7 @@ static int audio_get_endpoint_req(struct usb_function *f,
499 case UAC_GET_MIN: 499 case UAC_GET_MIN:
500 case UAC_GET_MAX: 500 case UAC_GET_MAX:
501 case UAC_GET_RES: 501 case UAC_GET_RES:
502 value = 3; 502 value = len;
503 break; 503 break;
504 case UAC_GET_MEM: 504 case UAC_GET_MEM:
505 break; 505 break;
@@ -681,17 +681,18 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
681 681
682 status = -ENOMEM; 682 status = -ENOMEM;
683 683
684 /* supcard all relevant hardware speeds... we expect that when 684 /* copy descriptors, and track endpoint copies */
685 f->descriptors = usb_copy_descriptors(f_audio_desc);
686
687 /*
688 * support all relevant hardware speeds... we expect that when
685 * hardware is dual speed, all bulk-capable endpoints work at 689 * hardware is dual speed, all bulk-capable endpoints work at
686 * both speeds 690 * both speeds
687 */ 691 */
688
689 /* copy descriptors, and track endpoint copies */
690 if (gadget_is_dualspeed(c->cdev->gadget)) { 692 if (gadget_is_dualspeed(c->cdev->gadget)) {
691 c->highspeed = true; 693 c->highspeed = true;
692 f->hs_descriptors = usb_copy_descriptors(f_audio_desc); 694 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
693 } else 695 }
694 f->descriptors = usb_copy_descriptors(f_audio_desc);
695 696
696 return 0; 697 return 0;
697 698
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
index 3691a0cb9465..11c07cb7d337 100644
--- a/drivers/usb/gadget/f_ecm.c
+++ b/drivers/usb/gadget/f_ecm.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22/* #define VERBOSE_DEBUG */ 13/* #define VERBOSE_DEBUG */
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
index 046c6d0e6960..1a7b2dd7d408 100644
--- a/drivers/usb/gadget/f_eem.c
+++ b/drivers/usb/gadget/f_eem.c
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23#include <linux/kernel.h> 14#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index c161a9aaeb7e..acb38004eec0 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -12,15 +12,6 @@
12 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or 13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. 14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */ 15 */
25 16
26 17
@@ -29,6 +20,7 @@
29 20
30#include <linux/blkdev.h> 21#include <linux/blkdev.h>
31#include <linux/pagemap.h> 22#include <linux/pagemap.h>
23#include <linux/export.h>
32#include <asm/unaligned.h> 24#include <asm/unaligned.h>
33 25
34#include <linux/usb/composite.h> 26#include <linux/usb/composite.h>
diff --git a/drivers/usb/gadget/f_hid.c b/drivers/usb/gadget/f_hid.c
index 83a266bdb40e..b2113420b806 100644
--- a/drivers/usb/gadget/f_hid.c
+++ b/drivers/usb/gadget/f_hid.c
@@ -7,15 +7,6 @@
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 10 */
20 11
21#include <linux/kernel.h> 12#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
index ca660d40b11a..6d87f288df4e 100644
--- a/drivers/usb/gadget/f_loopback.c
+++ b/drivers/usb/gadget/f_loopback.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22/* #define VERBOSE_DEBUG */ 13/* #define VERBOSE_DEBUG */
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 5b9339582007..c39d58860fa0 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -112,8 +112,7 @@
112 * is not loaded (an empty string as "filename" in the fsg_config 112 * is not loaded (an empty string as "filename" in the fsg_config
113 * structure causes error). The CD-ROM emulation includes a single 113 * structure causes error). The CD-ROM emulation includes a single
114 * data track and no audio tracks; hence there need be only one 114 * data track and no audio tracks; hence there need be only one
115 * backing file per LUN. Note also that the CD-ROM block length is 115 * backing file per LUN.
116 * set to 512 rather than the more common value 2048.
117 * 116 *
118 * 117 *
119 * MSF includes support for module parameters. If gadget using it 118 * MSF includes support for module parameters. If gadget using it
@@ -363,7 +362,7 @@ struct fsg_common {
363 362
364 struct fsg_buffhd *next_buffhd_to_fill; 363 struct fsg_buffhd *next_buffhd_to_fill;
365 struct fsg_buffhd *next_buffhd_to_drain; 364 struct fsg_buffhd *next_buffhd_to_drain;
366 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; 365 struct fsg_buffhd *buffhds;
367 366
368 int cmnd_size; 367 int cmnd_size;
369 u8 cmnd[MAX_COMMAND_SIZE]; 368 u8 cmnd[MAX_COMMAND_SIZE];
@@ -625,7 +624,8 @@ static int fsg_setup(struct usb_function *f,
625 if (ctrl->bRequestType != 624 if (ctrl->bRequestType !=
626 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE)) 625 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
627 break; 626 break;
628 if (w_index != fsg->interface_number || w_value != 0) 627 if (w_index != fsg->interface_number || w_value != 0 ||
628 w_length != 0)
629 return -EDOM; 629 return -EDOM;
630 630
631 /* 631 /*
@@ -640,7 +640,8 @@ static int fsg_setup(struct usb_function *f,
640 if (ctrl->bRequestType != 640 if (ctrl->bRequestType !=
641 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE)) 641 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
642 break; 642 break;
643 if (w_index != fsg->interface_number || w_value != 0) 643 if (w_index != fsg->interface_number || w_value != 0 ||
644 w_length != 1)
644 return -EDOM; 645 return -EDOM;
645 VDBG(fsg, "get max LUN\n"); 646 VDBG(fsg, "get max LUN\n");
646 *(u8 *)req->buf = fsg->common->nluns - 1; 647 *(u8 *)req->buf = fsg->common->nluns - 1;
@@ -745,7 +746,6 @@ static int do_read(struct fsg_common *common)
745 u32 amount_left; 746 u32 amount_left;
746 loff_t file_offset, file_offset_tmp; 747 loff_t file_offset, file_offset_tmp;
747 unsigned int amount; 748 unsigned int amount;
748 unsigned int partial_page;
749 ssize_t nread; 749 ssize_t nread;
750 750
751 /* 751 /*
@@ -771,7 +771,7 @@ static int do_read(struct fsg_common *common)
771 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 771 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
772 return -EINVAL; 772 return -EINVAL;
773 } 773 }
774 file_offset = ((loff_t) lba) << 9; 774 file_offset = ((loff_t) lba) << curlun->blkbits;
775 775
776 /* Carry out the file reads */ 776 /* Carry out the file reads */
777 amount_left = common->data_size_from_cmnd; 777 amount_left = common->data_size_from_cmnd;
@@ -784,18 +784,10 @@ static int do_read(struct fsg_common *common)
784 * Try to read the remaining amount. 784 * Try to read the remaining amount.
785 * But don't read more than the buffer size. 785 * But don't read more than the buffer size.
786 * And don't try to read past the end of the file. 786 * And don't try to read past the end of the file.
787 * Finally, if we're not at a page boundary, don't read past
788 * the next page.
789 * If this means reading 0 then we were asked to read past
790 * the end of file.
791 */ 787 */
792 amount = min(amount_left, FSG_BUFLEN); 788 amount = min(amount_left, FSG_BUFLEN);
793 amount = min((loff_t)amount, 789 amount = min((loff_t)amount,
794 curlun->file_length - file_offset); 790 curlun->file_length - file_offset);
795 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
796 if (partial_page > 0)
797 amount = min(amount, (unsigned int)PAGE_CACHE_SIZE -
798 partial_page);
799 791
800 /* Wait for the next buffer to become available */ 792 /* Wait for the next buffer to become available */
801 bh = common->next_buffhd_to_fill; 793 bh = common->next_buffhd_to_fill;
@@ -812,7 +804,8 @@ static int do_read(struct fsg_common *common)
812 if (amount == 0) { 804 if (amount == 0) {
813 curlun->sense_data = 805 curlun->sense_data =
814 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 806 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
815 curlun->sense_data_info = file_offset >> 9; 807 curlun->sense_data_info =
808 file_offset >> curlun->blkbits;
816 curlun->info_valid = 1; 809 curlun->info_valid = 1;
817 bh->inreq->length = 0; 810 bh->inreq->length = 0;
818 bh->state = BUF_STATE_FULL; 811 bh->state = BUF_STATE_FULL;
@@ -835,18 +828,25 @@ static int do_read(struct fsg_common *common)
835 } else if (nread < amount) { 828 } else if (nread < amount) {
836 LDBG(curlun, "partial file read: %d/%u\n", 829 LDBG(curlun, "partial file read: %d/%u\n",
837 (int)nread, amount); 830 (int)nread, amount);
838 nread -= (nread & 511); /* Round down to a block */ 831 nread = round_down(nread, curlun->blksize);
839 } 832 }
840 file_offset += nread; 833 file_offset += nread;
841 amount_left -= nread; 834 amount_left -= nread;
842 common->residue -= nread; 835 common->residue -= nread;
836
837 /*
838 * Except at the end of the transfer, nread will be
839 * equal to the buffer size, which is divisible by the
840 * bulk-in maxpacket size.
841 */
843 bh->inreq->length = nread; 842 bh->inreq->length = nread;
844 bh->state = BUF_STATE_FULL; 843 bh->state = BUF_STATE_FULL;
845 844
846 /* If an error occurred, report it and its position */ 845 /* If an error occurred, report it and its position */
847 if (nread < amount) { 846 if (nread < amount) {
848 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 847 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
849 curlun->sense_data_info = file_offset >> 9; 848 curlun->sense_data_info =
849 file_offset >> curlun->blkbits;
850 curlun->info_valid = 1; 850 curlun->info_valid = 1;
851 break; 851 break;
852 } 852 }
@@ -877,7 +877,6 @@ static int do_write(struct fsg_common *common)
877 u32 amount_left_to_req, amount_left_to_write; 877 u32 amount_left_to_req, amount_left_to_write;
878 loff_t usb_offset, file_offset, file_offset_tmp; 878 loff_t usb_offset, file_offset, file_offset_tmp;
879 unsigned int amount; 879 unsigned int amount;
880 unsigned int partial_page;
881 ssize_t nwritten; 880 ssize_t nwritten;
882 int rc; 881 int rc;
883 882
@@ -921,7 +920,7 @@ static int do_write(struct fsg_common *common)
921 920
922 /* Carry out the file writes */ 921 /* Carry out the file writes */
923 get_some_more = 1; 922 get_some_more = 1;
924 file_offset = usb_offset = ((loff_t) lba) << 9; 923 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
925 amount_left_to_req = common->data_size_from_cmnd; 924 amount_left_to_req = common->data_size_from_cmnd;
926 amount_left_to_write = common->data_size_from_cmnd; 925 amount_left_to_write = common->data_size_from_cmnd;
927 926
@@ -933,41 +932,21 @@ static int do_write(struct fsg_common *common)
933 932
934 /* 933 /*
935 * Figure out how much we want to get: 934 * Figure out how much we want to get:
936 * Try to get the remaining amount. 935 * Try to get the remaining amount,
937 * But don't get more than the buffer size. 936 * but not more than the buffer size.
938 * And don't try to go past the end of the file.
939 * If we're not at a page boundary,
940 * don't go past the next page.
941 * If this means getting 0, then we were asked
942 * to write past the end of file.
943 * Finally, round down to a block boundary.
944 */ 937 */
945 amount = min(amount_left_to_req, FSG_BUFLEN); 938 amount = min(amount_left_to_req, FSG_BUFLEN);
946 amount = min((loff_t)amount, 939
947 curlun->file_length - usb_offset); 940 /* Beyond the end of the backing file? */
948 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1); 941 if (usb_offset >= curlun->file_length) {
949 if (partial_page > 0)
950 amount = min(amount,
951 (unsigned int)PAGE_CACHE_SIZE - partial_page);
952
953 if (amount == 0) {
954 get_some_more = 0; 942 get_some_more = 0;
955 curlun->sense_data = 943 curlun->sense_data =
956 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 944 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
957 curlun->sense_data_info = usb_offset >> 9; 945 curlun->sense_data_info =
946 usb_offset >> curlun->blkbits;
958 curlun->info_valid = 1; 947 curlun->info_valid = 1;
959 continue; 948 continue;
960 } 949 }
961 amount -= amount & 511;
962 if (amount == 0) {
963
964 /*
965 * Why were we were asked to transfer a
966 * partial block?
967 */
968 get_some_more = 0;
969 continue;
970 }
971 950
972 /* Get the next buffer */ 951 /* Get the next buffer */
973 usb_offset += amount; 952 usb_offset += amount;
@@ -977,12 +956,11 @@ static int do_write(struct fsg_common *common)
977 get_some_more = 0; 956 get_some_more = 0;
978 957
979 /* 958 /*
980 * amount is always divisible by 512, hence by 959 * Except at the end of the transfer, amount will be
981 * the bulk-out maxpacket size 960 * equal to the buffer size, which is divisible by
961 * the bulk-out maxpacket size.
982 */ 962 */
983 bh->outreq->length = amount; 963 set_bulk_out_req_length(common, bh, amount);
984 bh->bulk_out_intended_length = amount;
985 bh->outreq->short_not_ok = 1;
986 if (!start_out_transfer(common, bh)) 964 if (!start_out_transfer(common, bh))
987 /* Dunno what to do if common->fsg is NULL */ 965 /* Dunno what to do if common->fsg is NULL */
988 return -EIO; 966 return -EIO;
@@ -1002,7 +980,8 @@ static int do_write(struct fsg_common *common)
1002 /* Did something go wrong with the transfer? */ 980 /* Did something go wrong with the transfer? */
1003 if (bh->outreq->status != 0) { 981 if (bh->outreq->status != 0) {
1004 curlun->sense_data = SS_COMMUNICATION_FAILURE; 982 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1005 curlun->sense_data_info = file_offset >> 9; 983 curlun->sense_data_info =
984 file_offset >> curlun->blkbits;
1006 curlun->info_valid = 1; 985 curlun->info_valid = 1;
1007 break; 986 break;
1008 } 987 }
@@ -1016,6 +995,16 @@ static int do_write(struct fsg_common *common)
1016 amount = curlun->file_length - file_offset; 995 amount = curlun->file_length - file_offset;
1017 } 996 }
1018 997
998 /* Don't accept excess data. The spec doesn't say
999 * what to do in this case. We'll ignore the error.
1000 */
1001 amount = min(amount, bh->bulk_out_intended_length);
1002
1003 /* Don't write a partial block */
1004 amount = round_down(amount, curlun->blksize);
1005 if (amount == 0)
1006 goto empty_write;
1007
1019 /* Perform the write */ 1008 /* Perform the write */
1020 file_offset_tmp = file_offset; 1009 file_offset_tmp = file_offset;
1021 nwritten = vfs_write(curlun->filp, 1010 nwritten = vfs_write(curlun->filp,
@@ -1033,8 +1022,7 @@ static int do_write(struct fsg_common *common)
1033 } else if (nwritten < amount) { 1022 } else if (nwritten < amount) {
1034 LDBG(curlun, "partial file write: %d/%u\n", 1023 LDBG(curlun, "partial file write: %d/%u\n",
1035 (int)nwritten, amount); 1024 (int)nwritten, amount);
1036 nwritten -= (nwritten & 511); 1025 nwritten = round_down(nwritten, curlun->blksize);
1037 /* Round down to a block */
1038 } 1026 }
1039 file_offset += nwritten; 1027 file_offset += nwritten;
1040 amount_left_to_write -= nwritten; 1028 amount_left_to_write -= nwritten;
@@ -1043,13 +1031,15 @@ static int do_write(struct fsg_common *common)
1043 /* If an error occurred, report it and its position */ 1031 /* If an error occurred, report it and its position */
1044 if (nwritten < amount) { 1032 if (nwritten < amount) {
1045 curlun->sense_data = SS_WRITE_ERROR; 1033 curlun->sense_data = SS_WRITE_ERROR;
1046 curlun->sense_data_info = file_offset >> 9; 1034 curlun->sense_data_info =
1035 file_offset >> curlun->blkbits;
1047 curlun->info_valid = 1; 1036 curlun->info_valid = 1;
1048 break; 1037 break;
1049 } 1038 }
1050 1039
1040 empty_write:
1051 /* Did the host decide to stop early? */ 1041 /* Did the host decide to stop early? */
1052 if (bh->outreq->actual != bh->outreq->length) { 1042 if (bh->outreq->actual < bh->bulk_out_intended_length) {
1053 common->short_packet_received = 1; 1043 common->short_packet_received = 1;
1054 break; 1044 break;
1055 } 1045 }
@@ -1129,8 +1119,8 @@ static int do_verify(struct fsg_common *common)
1129 return -EIO; /* No default reply */ 1119 return -EIO; /* No default reply */
1130 1120
1131 /* Prepare to carry out the file verify */ 1121 /* Prepare to carry out the file verify */
1132 amount_left = verification_length << 9; 1122 amount_left = verification_length << curlun->blkbits;
1133 file_offset = ((loff_t) lba) << 9; 1123 file_offset = ((loff_t) lba) << curlun->blkbits;
1134 1124
1135 /* Write out all the dirty buffers before invalidating them */ 1125 /* Write out all the dirty buffers before invalidating them */
1136 fsg_lun_fsync_sub(curlun); 1126 fsg_lun_fsync_sub(curlun);
@@ -1148,8 +1138,6 @@ static int do_verify(struct fsg_common *common)
1148 * Try to read the remaining amount, but not more than 1138 * Try to read the remaining amount, but not more than
1149 * the buffer size. 1139 * the buffer size.
1150 * And don't try to read past the end of the file. 1140 * And don't try to read past the end of the file.
1151 * If this means reading 0 then we were asked to read
1152 * past the end of file.
1153 */ 1141 */
1154 amount = min(amount_left, FSG_BUFLEN); 1142 amount = min(amount_left, FSG_BUFLEN);
1155 amount = min((loff_t)amount, 1143 amount = min((loff_t)amount,
@@ -1157,7 +1145,8 @@ static int do_verify(struct fsg_common *common)
1157 if (amount == 0) { 1145 if (amount == 0) {
1158 curlun->sense_data = 1146 curlun->sense_data =
1159 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1147 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1160 curlun->sense_data_info = file_offset >> 9; 1148 curlun->sense_data_info =
1149 file_offset >> curlun->blkbits;
1161 curlun->info_valid = 1; 1150 curlun->info_valid = 1;
1162 break; 1151 break;
1163 } 1152 }
@@ -1179,11 +1168,12 @@ static int do_verify(struct fsg_common *common)
1179 } else if (nread < amount) { 1168 } else if (nread < amount) {
1180 LDBG(curlun, "partial file verify: %d/%u\n", 1169 LDBG(curlun, "partial file verify: %d/%u\n",
1181 (int)nread, amount); 1170 (int)nread, amount);
1182 nread -= nread & 511; /* Round down to a sector */ 1171 nread = round_down(nread, curlun->blksize);
1183 } 1172 }
1184 if (nread == 0) { 1173 if (nread == 0) {
1185 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1174 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1186 curlun->sense_data_info = file_offset >> 9; 1175 curlun->sense_data_info =
1176 file_offset >> curlun->blkbits;
1187 curlun->info_valid = 1; 1177 curlun->info_valid = 1;
1188 break; 1178 break;
1189 } 1179 }
@@ -1289,7 +1279,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1289 1279
1290 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]); 1280 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1291 /* Max logical block */ 1281 /* Max logical block */
1292 put_unaligned_be32(512, &buf[4]); /* Block length */ 1282 put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
1293 return 8; 1283 return 8;
1294} 1284}
1295 1285
@@ -1527,7 +1517,7 @@ static int do_read_format_capacities(struct fsg_common *common,
1527 1517
1528 put_unaligned_be32(curlun->num_sectors, &buf[0]); 1518 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1529 /* Number of blocks */ 1519 /* Number of blocks */
1530 put_unaligned_be32(512, &buf[4]); /* Block length */ 1520 put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
1531 buf[4] = 0x02; /* Current capacity */ 1521 buf[4] = 0x02; /* Current capacity */
1532 return 12; 1522 return 12;
1533} 1523}
@@ -1607,7 +1597,7 @@ static int throw_away_data(struct fsg_common *common)
1607 common->next_buffhd_to_drain = bh->next; 1597 common->next_buffhd_to_drain = bh->next;
1608 1598
1609 /* A short packet or an error ends everything */ 1599 /* A short packet or an error ends everything */
1610 if (bh->outreq->actual != bh->outreq->length || 1600 if (bh->outreq->actual < bh->bulk_out_intended_length ||
1611 bh->outreq->status != 0) { 1601 bh->outreq->status != 0) {
1612 raise_exception(common, 1602 raise_exception(common,
1613 FSG_STATE_ABORT_BULK_OUT); 1603 FSG_STATE_ABORT_BULK_OUT);
@@ -1623,12 +1613,11 @@ static int throw_away_data(struct fsg_common *common)
1623 amount = min(common->usb_amount_left, FSG_BUFLEN); 1613 amount = min(common->usb_amount_left, FSG_BUFLEN);
1624 1614
1625 /* 1615 /*
1626 * amount is always divisible by 512, hence by 1616 * Except at the end of the transfer, amount will be
1617 * equal to the buffer size, which is divisible by
1627 * the bulk-out maxpacket size. 1618 * the bulk-out maxpacket size.
1628 */ 1619 */
1629 bh->outreq->length = amount; 1620 set_bulk_out_req_length(common, bh, amount);
1630 bh->bulk_out_intended_length = amount;
1631 bh->outreq->short_not_ok = 1;
1632 if (!start_out_transfer(common, bh)) 1621 if (!start_out_transfer(common, bh))
1633 /* Dunno what to do if common->fsg is NULL */ 1622 /* Dunno what to do if common->fsg is NULL */
1634 return -EIO; 1623 return -EIO;
@@ -2022,7 +2011,8 @@ static int do_scsi_command(struct fsg_common *common)
2022 2011
2023 case READ_6: 2012 case READ_6:
2024 i = common->cmnd[4]; 2013 i = common->cmnd[4];
2025 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2014 common->data_size_from_cmnd = (i == 0 ? 256 : i) <<
2015 common->curlun->blkbits;
2026 reply = check_command(common, 6, DATA_DIR_TO_HOST, 2016 reply = check_command(common, 6, DATA_DIR_TO_HOST,
2027 (7<<1) | (1<<4), 1, 2017 (7<<1) | (1<<4), 1,
2028 "READ(6)"); 2018 "READ(6)");
@@ -2032,7 +2022,8 @@ static int do_scsi_command(struct fsg_common *common)
2032 2022
2033 case READ_10: 2023 case READ_10:
2034 common->data_size_from_cmnd = 2024 common->data_size_from_cmnd =
2035 get_unaligned_be16(&common->cmnd[7]) << 9; 2025 get_unaligned_be16(&common->cmnd[7]) <<
2026 common->curlun->blkbits;
2036 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2027 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2037 (1<<1) | (0xf<<2) | (3<<7), 1, 2028 (1<<1) | (0xf<<2) | (3<<7), 1,
2038 "READ(10)"); 2029 "READ(10)");
@@ -2042,7 +2033,8 @@ static int do_scsi_command(struct fsg_common *common)
2042 2033
2043 case READ_12: 2034 case READ_12:
2044 common->data_size_from_cmnd = 2035 common->data_size_from_cmnd =
2045 get_unaligned_be32(&common->cmnd[6]) << 9; 2036 get_unaligned_be32(&common->cmnd[6]) <<
2037 common->curlun->blkbits;
2046 reply = check_command(common, 12, DATA_DIR_TO_HOST, 2038 reply = check_command(common, 12, DATA_DIR_TO_HOST,
2047 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2039 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2048 "READ(12)"); 2040 "READ(12)");
@@ -2142,7 +2134,8 @@ static int do_scsi_command(struct fsg_common *common)
2142 2134
2143 case WRITE_6: 2135 case WRITE_6:
2144 i = common->cmnd[4]; 2136 i = common->cmnd[4];
2145 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2137 common->data_size_from_cmnd = (i == 0 ? 256 : i) <<
2138 common->curlun->blkbits;
2146 reply = check_command(common, 6, DATA_DIR_FROM_HOST, 2139 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
2147 (7<<1) | (1<<4), 1, 2140 (7<<1) | (1<<4), 1,
2148 "WRITE(6)"); 2141 "WRITE(6)");
@@ -2152,7 +2145,8 @@ static int do_scsi_command(struct fsg_common *common)
2152 2145
2153 case WRITE_10: 2146 case WRITE_10:
2154 common->data_size_from_cmnd = 2147 common->data_size_from_cmnd =
2155 get_unaligned_be16(&common->cmnd[7]) << 9; 2148 get_unaligned_be16(&common->cmnd[7]) <<
2149 common->curlun->blkbits;
2156 reply = check_command(common, 10, DATA_DIR_FROM_HOST, 2150 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
2157 (1<<1) | (0xf<<2) | (3<<7), 1, 2151 (1<<1) | (0xf<<2) | (3<<7), 1,
2158 "WRITE(10)"); 2152 "WRITE(10)");
@@ -2162,7 +2156,8 @@ static int do_scsi_command(struct fsg_common *common)
2162 2156
2163 case WRITE_12: 2157 case WRITE_12:
2164 common->data_size_from_cmnd = 2158 common->data_size_from_cmnd =
2165 get_unaligned_be32(&common->cmnd[6]) << 9; 2159 get_unaligned_be32(&common->cmnd[6]) <<
2160 common->curlun->blkbits;
2166 reply = check_command(common, 12, DATA_DIR_FROM_HOST, 2161 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
2167 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2162 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2168 "WRITE(12)"); 2163 "WRITE(12)");
@@ -2297,7 +2292,6 @@ static int get_next_command(struct fsg_common *common)
2297 2292
2298 /* Queue a request to read a Bulk-only CBW */ 2293 /* Queue a request to read a Bulk-only CBW */
2299 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); 2294 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2300 bh->outreq->short_not_ok = 1;
2301 if (!start_out_transfer(common, bh)) 2295 if (!start_out_transfer(common, bh))
2302 /* Don't know what to do if common->fsg is NULL */ 2296 /* Don't know what to do if common->fsg is NULL */
2303 return -EIO; 2297 return -EIO;
@@ -2348,7 +2342,7 @@ reset:
2348 if (common->fsg) { 2342 if (common->fsg) {
2349 fsg = common->fsg; 2343 fsg = common->fsg;
2350 2344
2351 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2345 for (i = 0; i < fsg_num_buffers; ++i) {
2352 struct fsg_buffhd *bh = &common->buffhds[i]; 2346 struct fsg_buffhd *bh = &common->buffhds[i];
2353 2347
2354 if (bh->inreq) { 2348 if (bh->inreq) {
@@ -2401,12 +2395,11 @@ reset:
2401 goto reset; 2395 goto reset;
2402 fsg->bulk_out->driver_data = common; 2396 fsg->bulk_out->driver_data = common;
2403 fsg->bulk_out_enabled = 1; 2397 fsg->bulk_out_enabled = 1;
2404 common->bulk_out_maxpacket = 2398 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc);
2405 le16_to_cpu(fsg->bulk_out->desc->wMaxPacketSize);
2406 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); 2399 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2407 2400
2408 /* Allocate the requests */ 2401 /* Allocate the requests */
2409 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2402 for (i = 0; i < fsg_num_buffers; ++i) {
2410 struct fsg_buffhd *bh = &common->buffhds[i]; 2403 struct fsg_buffhd *bh = &common->buffhds[i];
2411 2404
2412 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); 2405 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
@@ -2475,7 +2468,7 @@ static void handle_exception(struct fsg_common *common)
2475 2468
2476 /* Cancel all the pending transfers */ 2469 /* Cancel all the pending transfers */
2477 if (likely(common->fsg)) { 2470 if (likely(common->fsg)) {
2478 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2471 for (i = 0; i < fsg_num_buffers; ++i) {
2479 bh = &common->buffhds[i]; 2472 bh = &common->buffhds[i];
2480 if (bh->inreq_busy) 2473 if (bh->inreq_busy)
2481 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); 2474 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
@@ -2487,7 +2480,7 @@ static void handle_exception(struct fsg_common *common)
2487 /* Wait until everything is idle */ 2480 /* Wait until everything is idle */
2488 for (;;) { 2481 for (;;) {
2489 int num_active = 0; 2482 int num_active = 0;
2490 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2483 for (i = 0; i < fsg_num_buffers; ++i) {
2491 bh = &common->buffhds[i]; 2484 bh = &common->buffhds[i];
2492 num_active += bh->inreq_busy + bh->outreq_busy; 2485 num_active += bh->inreq_busy + bh->outreq_busy;
2493 } 2486 }
@@ -2510,7 +2503,7 @@ static void handle_exception(struct fsg_common *common)
2510 */ 2503 */
2511 spin_lock_irq(&common->lock); 2504 spin_lock_irq(&common->lock);
2512 2505
2513 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2506 for (i = 0; i < fsg_num_buffers; ++i) {
2514 bh = &common->buffhds[i]; 2507 bh = &common->buffhds[i];
2515 bh->state = BUF_STATE_EMPTY; 2508 bh->state = BUF_STATE_EMPTY;
2516 } 2509 }
@@ -2719,6 +2712,10 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2719 int nluns, i, rc; 2712 int nluns, i, rc;
2720 char *pathbuf; 2713 char *pathbuf;
2721 2714
2715 rc = fsg_num_buffers_validate();
2716 if (rc != 0)
2717 return ERR_PTR(rc);
2718
2722 /* Find out how many LUNs there should be */ 2719 /* Find out how many LUNs there should be */
2723 nluns = cfg->nluns; 2720 nluns = cfg->nluns;
2724 if (nluns < 1 || nluns > FSG_MAX_LUNS) { 2721 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
@@ -2737,6 +2734,14 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2737 common->free_storage_on_release = 0; 2734 common->free_storage_on_release = 0;
2738 } 2735 }
2739 2736
2737 common->buffhds = kcalloc(fsg_num_buffers,
2738 sizeof *(common->buffhds), GFP_KERNEL);
2739 if (!common->buffhds) {
2740 if (common->free_storage_on_release)
2741 kfree(common);
2742 return ERR_PTR(-ENOMEM);
2743 }
2744
2740 common->ops = cfg->ops; 2745 common->ops = cfg->ops;
2741 common->private_data = cfg->private_data; 2746 common->private_data = cfg->private_data;
2742 2747
@@ -2814,7 +2819,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2814 2819
2815 /* Data buffers cyclic list */ 2820 /* Data buffers cyclic list */
2816 bh = common->buffhds; 2821 bh = common->buffhds;
2817 i = FSG_NUM_BUFFERS; 2822 i = fsg_num_buffers;
2818 goto buffhds_first_it; 2823 goto buffhds_first_it;
2819 do { 2824 do {
2820 bh->next = bh + 1; 2825 bh->next = bh + 1;
@@ -2940,12 +2945,13 @@ static void fsg_common_release(struct kref *ref)
2940 2945
2941 { 2946 {
2942 struct fsg_buffhd *bh = common->buffhds; 2947 struct fsg_buffhd *bh = common->buffhds;
2943 unsigned i = FSG_NUM_BUFFERS; 2948 unsigned i = fsg_num_buffers;
2944 do { 2949 do {
2945 kfree(bh->buf); 2950 kfree(bh->buf);
2946 } while (++bh, --i); 2951 } while (++bh, --i);
2947 } 2952 }
2948 2953
2954 kfree(common->buffhds);
2949 if (common->free_storage_on_release) 2955 if (common->free_storage_on_release)
2950 kfree(common); 2956 kfree(common);
2951} 2957}
@@ -3019,6 +3025,28 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
3019 } 3025 }
3020 } 3026 }
3021 3027
3028 if (gadget_is_superspeed(gadget)) {
3029 unsigned max_burst;
3030
3031 /* Calculate bMaxBurst, we know packet size is 1024 */
3032 max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15);
3033
3034 fsg_ss_bulk_in_desc.bEndpointAddress =
3035 fsg_fs_bulk_in_desc.bEndpointAddress;
3036 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
3037
3038 fsg_ss_bulk_out_desc.bEndpointAddress =
3039 fsg_fs_bulk_out_desc.bEndpointAddress;
3040 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
3041
3042 f->ss_descriptors = usb_copy_descriptors(fsg_ss_function);
3043 if (unlikely(!f->ss_descriptors)) {
3044 usb_free_descriptors(f->hs_descriptors);
3045 usb_free_descriptors(f->descriptors);
3046 return -ENOMEM;
3047 }
3048 }
3049
3022 return 0; 3050 return 0;
3023 3051
3024autoconf_fail: 3052autoconf_fail:
diff --git a/drivers/usb/gadget/f_midi.c b/drivers/usb/gadget/f_midi.c
new file mode 100644
index 000000000000..3797b3d6c622
--- /dev/null
+++ b/drivers/usb/gadget/f_midi.c
@@ -0,0 +1,990 @@
1/*
2 * f_midi.c -- USB MIDI class function driver
3 *
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
7 *
8 * Rewritten for the composite framework
9 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
10 *
11 * Based on drivers/usb/gadget/f_audio.c,
12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
13 * Copyright (C) 2008 Analog Devices, Inc
14 *
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
17 * Ben Williamson <ben.williamson@greyinnovation.com>
18 *
19 * Licensed under the GPL-2 or later.
20 */
21
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/utsname.h>
25#include <linux/device.h>
26
27#include <sound/core.h>
28#include <sound/initval.h>
29#include <sound/rawmidi.h>
30
31#include <linux/usb/ch9.h>
32#include <linux/usb/gadget.h>
33#include <linux/usb/audio.h>
34#include <linux/usb/midi.h>
35
36MODULE_AUTHOR("Ben Williamson");
37MODULE_LICENSE("GPL v2");
38
39static const char f_midi_shortname[] = "f_midi";
40static const char f_midi_longname[] = "MIDI Gadget";
41
42/*
43 * We can only handle 16 cables on one single endpoint, as cable numbers are
44 * stored in 4-bit fields. And as the interface currently only holds one
45 * single endpoint, this is the maximum number of ports we can allow.
46 */
47#define MAX_PORTS 16
48
49/*
50 * This is a gadget, and the IN/OUT naming is from the host's perspective.
51 * USB -> OUT endpoint -> rawmidi
52 * USB <- IN endpoint <- rawmidi
53 */
54struct gmidi_in_port {
55 struct f_midi *midi;
56 int active;
57 uint8_t cable;
58 uint8_t state;
59#define STATE_UNKNOWN 0
60#define STATE_1PARAM 1
61#define STATE_2PARAM_1 2
62#define STATE_2PARAM_2 3
63#define STATE_SYSEX_0 4
64#define STATE_SYSEX_1 5
65#define STATE_SYSEX_2 6
66 uint8_t data[2];
67};
68
69struct f_midi {
70 struct usb_function func;
71 struct usb_gadget *gadget;
72 struct usb_ep *in_ep, *out_ep;
73 struct snd_card *card;
74 struct snd_rawmidi *rmidi;
75
76 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
77 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
78 struct gmidi_in_port *in_port[MAX_PORTS];
79
80 unsigned long out_triggered;
81 struct tasklet_struct tasklet;
82 unsigned int in_ports;
83 unsigned int out_ports;
84 int index;
85 char *id;
86 unsigned int buflen, qlen;
87};
88
89static inline struct f_midi *func_to_midi(struct usb_function *f)
90{
91 return container_of(f, struct f_midi, func);
92}
93
94static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
95
96DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
97DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
98DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
99
100/* B.3.1 Standard AC Interface Descriptor */
101static struct usb_interface_descriptor ac_interface_desc __initdata = {
102 .bLength = USB_DT_INTERFACE_SIZE,
103 .bDescriptorType = USB_DT_INTERFACE,
104 /* .bInterfaceNumber = DYNAMIC */
105 /* .bNumEndpoints = DYNAMIC */
106 .bInterfaceClass = USB_CLASS_AUDIO,
107 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
108 /* .iInterface = DYNAMIC */
109};
110
111/* B.3.2 Class-Specific AC Interface Descriptor */
112static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata = {
113 .bLength = UAC_DT_AC_HEADER_SIZE(1),
114 .bDescriptorType = USB_DT_CS_INTERFACE,
115 .bDescriptorSubtype = USB_MS_HEADER,
116 .bcdADC = cpu_to_le16(0x0100),
117 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
118 .bInCollection = 1,
119 /* .baInterfaceNr = DYNAMIC */
120};
121
122/* B.4.1 Standard MS Interface Descriptor */
123static struct usb_interface_descriptor ms_interface_desc __initdata = {
124 .bLength = USB_DT_INTERFACE_SIZE,
125 .bDescriptorType = USB_DT_INTERFACE,
126 /* .bInterfaceNumber = DYNAMIC */
127 .bNumEndpoints = 2,
128 .bInterfaceClass = USB_CLASS_AUDIO,
129 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
130 /* .iInterface = DYNAMIC */
131};
132
133/* B.4.2 Class-Specific MS Interface Descriptor */
134static struct usb_ms_header_descriptor ms_header_desc __initdata = {
135 .bLength = USB_DT_MS_HEADER_SIZE,
136 .bDescriptorType = USB_DT_CS_INTERFACE,
137 .bDescriptorSubtype = USB_MS_HEADER,
138 .bcdMSC = cpu_to_le16(0x0100),
139 /* .wTotalLength = DYNAMIC */
140};
141
142/* B.5.1 Standard Bulk OUT Endpoint Descriptor */
143static struct usb_endpoint_descriptor bulk_out_desc = {
144 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
146 .bEndpointAddress = USB_DIR_OUT,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148};
149
150/* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
151static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
152 /* .bLength = DYNAMIC */
153 .bDescriptorType = USB_DT_CS_ENDPOINT,
154 .bDescriptorSubtype = USB_MS_GENERAL,
155 /* .bNumEmbMIDIJack = DYNAMIC */
156 /* .baAssocJackID = DYNAMIC */
157};
158
159/* B.6.1 Standard Bulk IN Endpoint Descriptor */
160static struct usb_endpoint_descriptor bulk_in_desc = {
161 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165};
166
167/* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
168static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
169 /* .bLength = DYNAMIC */
170 .bDescriptorType = USB_DT_CS_ENDPOINT,
171 .bDescriptorSubtype = USB_MS_GENERAL,
172 /* .bNumEmbMIDIJack = DYNAMIC */
173 /* .baAssocJackID = DYNAMIC */
174};
175
176/* string IDs are assigned dynamically */
177
178#define STRING_FUNC_IDX 0
179
180static struct usb_string midi_string_defs[] = {
181 [STRING_FUNC_IDX].s = "MIDI function",
182 { } /* end of list */
183};
184
185static struct usb_gadget_strings midi_stringtab = {
186 .language = 0x0409, /* en-us */
187 .strings = midi_string_defs,
188};
189
190static struct usb_gadget_strings *midi_strings[] = {
191 &midi_stringtab,
192 NULL,
193};
194
195static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
196{
197 struct usb_request *req;
198
199 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
200 if (req) {
201 req->length = length;
202 req->buf = kmalloc(length, GFP_ATOMIC);
203 if (!req->buf) {
204 usb_ep_free_request(ep, req);
205 req = NULL;
206 }
207 }
208 return req;
209}
210
211static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
212{
213 kfree(req->buf);
214 usb_ep_free_request(ep, req);
215}
216
217static const uint8_t f_midi_cin_length[] = {
218 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
219};
220
221/*
222 * Receives a chunk of MIDI data.
223 */
224static void f_midi_read_data(struct usb_ep *ep, int cable,
225 uint8_t *data, int length)
226{
227 struct f_midi *midi = ep->driver_data;
228 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
229
230 if (!substream)
231 /* Nobody is listening - throw it on the floor. */
232 return;
233
234 if (!test_bit(cable, &midi->out_triggered))
235 return;
236
237 snd_rawmidi_receive(substream, data, length);
238}
239
240static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
241{
242 unsigned int i;
243 u8 *buf = req->buf;
244
245 for (i = 0; i + 3 < req->actual; i += 4)
246 if (buf[i] != 0) {
247 int cable = buf[i] >> 4;
248 int length = f_midi_cin_length[buf[i] & 0x0f];
249 f_midi_read_data(ep, cable, &buf[i + 1], length);
250 }
251}
252
253static void
254f_midi_complete(struct usb_ep *ep, struct usb_request *req)
255{
256 struct f_midi *midi = ep->driver_data;
257 struct usb_composite_dev *cdev = midi->func.config->cdev;
258 int status = req->status;
259
260 switch (status) {
261 case 0: /* normal completion */
262 if (ep == midi->out_ep) {
263 /* We received stuff. req is queued again, below */
264 f_midi_handle_out_data(ep, req);
265 } else if (ep == midi->in_ep) {
266 /* Our transmit completed. See if there's more to go.
267 * f_midi_transmit eats req, don't queue it again. */
268 f_midi_transmit(midi, req);
269 return;
270 }
271 break;
272
273 /* this endpoint is normally active while we're configured */
274 case -ECONNABORTED: /* hardware forced ep reset */
275 case -ECONNRESET: /* request dequeued */
276 case -ESHUTDOWN: /* disconnect from host */
277 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
278 req->actual, req->length);
279 if (ep == midi->out_ep)
280 f_midi_handle_out_data(ep, req);
281
282 free_ep_req(ep, req);
283 return;
284
285 case -EOVERFLOW: /* buffer overrun on read means that
286 * we didn't provide a big enough buffer.
287 */
288 default:
289 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
290 status, req->actual, req->length);
291 break;
292 case -EREMOTEIO: /* short read */
293 break;
294 }
295
296 status = usb_ep_queue(ep, req, GFP_ATOMIC);
297 if (status) {
298 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
299 ep->name, req->length, status);
300 usb_ep_set_halt(ep);
301 /* FIXME recover later ... somehow */
302 }
303}
304
305static int f_midi_start_ep(struct f_midi *midi,
306 struct usb_function *f,
307 struct usb_ep *ep)
308{
309 int err;
310 struct usb_composite_dev *cdev = f->config->cdev;
311
312 if (ep->driver_data)
313 usb_ep_disable(ep);
314
315 err = config_ep_by_speed(midi->gadget, f, ep);
316 if (err) {
317 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
318 return err;
319 }
320
321 err = usb_ep_enable(ep);
322 if (err) {
323 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
324 return err;
325 }
326
327 ep->driver_data = midi;
328
329 return 0;
330}
331
332static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
333{
334 struct f_midi *midi = func_to_midi(f);
335 struct usb_composite_dev *cdev = f->config->cdev;
336 unsigned i;
337 int err;
338
339 err = f_midi_start_ep(midi, f, midi->in_ep);
340 if (err)
341 return err;
342
343 err = f_midi_start_ep(midi, f, midi->out_ep);
344 if (err)
345 return err;
346
347 if (midi->out_ep->driver_data)
348 usb_ep_disable(midi->out_ep);
349
350 err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
351 if (err) {
352 ERROR(cdev, "can't configure %s: %d\n",
353 midi->out_ep->name, err);
354 return err;
355 }
356
357 err = usb_ep_enable(midi->out_ep);
358 if (err) {
359 ERROR(cdev, "can't start %s: %d\n",
360 midi->out_ep->name, err);
361 return err;
362 }
363
364 midi->out_ep->driver_data = midi;
365
366 /* allocate a bunch of read buffers and queue them all at once. */
367 for (i = 0; i < midi->qlen && err == 0; i++) {
368 struct usb_request *req =
369 alloc_ep_req(midi->out_ep, midi->buflen);
370 if (req == NULL)
371 return -ENOMEM;
372
373 req->complete = f_midi_complete;
374 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
375 if (err) {
376 ERROR(midi, "%s queue req: %d\n",
377 midi->out_ep->name, err);
378 }
379 }
380
381 return 0;
382}
383
384static void f_midi_disable(struct usb_function *f)
385{
386 struct f_midi *midi = func_to_midi(f);
387 struct usb_composite_dev *cdev = f->config->cdev;
388
389 DBG(cdev, "disable\n");
390
391 /*
392 * just disable endpoints, forcing completion of pending i/o.
393 * all our completion handlers free their requests in this case.
394 */
395 usb_ep_disable(midi->in_ep);
396 usb_ep_disable(midi->out_ep);
397}
398
399static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
400{
401 struct usb_composite_dev *cdev = f->config->cdev;
402 struct f_midi *midi = func_to_midi(f);
403 struct snd_card *card;
404
405 DBG(cdev, "unbind\n");
406
407 /* just to be sure */
408 f_midi_disable(f);
409
410 card = midi->card;
411 midi->card = NULL;
412 if (card)
413 snd_card_free(card);
414
415 kfree(midi->id);
416 midi->id = NULL;
417
418 usb_free_descriptors(f->descriptors);
419 kfree(midi);
420}
421
422static int f_midi_snd_free(struct snd_device *device)
423{
424 return 0;
425}
426
427static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
428 uint8_t p1, uint8_t p2, uint8_t p3)
429{
430 unsigned length = req->length;
431 u8 *buf = (u8 *)req->buf + length;
432
433 buf[0] = p0;
434 buf[1] = p1;
435 buf[2] = p2;
436 buf[3] = p3;
437 req->length = length + 4;
438}
439
440/*
441 * Converts MIDI commands to USB MIDI packets.
442 */
443static void f_midi_transmit_byte(struct usb_request *req,
444 struct gmidi_in_port *port, uint8_t b)
445{
446 uint8_t p0 = port->cable << 4;
447
448 if (b >= 0xf8) {
449 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
450 } else if (b >= 0xf0) {
451 switch (b) {
452 case 0xf0:
453 port->data[0] = b;
454 port->state = STATE_SYSEX_1;
455 break;
456 case 0xf1:
457 case 0xf3:
458 port->data[0] = b;
459 port->state = STATE_1PARAM;
460 break;
461 case 0xf2:
462 port->data[0] = b;
463 port->state = STATE_2PARAM_1;
464 break;
465 case 0xf4:
466 case 0xf5:
467 port->state = STATE_UNKNOWN;
468 break;
469 case 0xf6:
470 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
471 port->state = STATE_UNKNOWN;
472 break;
473 case 0xf7:
474 switch (port->state) {
475 case STATE_SYSEX_0:
476 f_midi_transmit_packet(req,
477 p0 | 0x05, 0xf7, 0, 0);
478 break;
479 case STATE_SYSEX_1:
480 f_midi_transmit_packet(req,
481 p0 | 0x06, port->data[0], 0xf7, 0);
482 break;
483 case STATE_SYSEX_2:
484 f_midi_transmit_packet(req,
485 p0 | 0x07, port->data[0],
486 port->data[1], 0xf7);
487 break;
488 }
489 port->state = STATE_UNKNOWN;
490 break;
491 }
492 } else if (b >= 0x80) {
493 port->data[0] = b;
494 if (b >= 0xc0 && b <= 0xdf)
495 port->state = STATE_1PARAM;
496 else
497 port->state = STATE_2PARAM_1;
498 } else { /* b < 0x80 */
499 switch (port->state) {
500 case STATE_1PARAM:
501 if (port->data[0] < 0xf0) {
502 p0 |= port->data[0] >> 4;
503 } else {
504 p0 |= 0x02;
505 port->state = STATE_UNKNOWN;
506 }
507 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
508 break;
509 case STATE_2PARAM_1:
510 port->data[1] = b;
511 port->state = STATE_2PARAM_2;
512 break;
513 case STATE_2PARAM_2:
514 if (port->data[0] < 0xf0) {
515 p0 |= port->data[0] >> 4;
516 port->state = STATE_2PARAM_1;
517 } else {
518 p0 |= 0x03;
519 port->state = STATE_UNKNOWN;
520 }
521 f_midi_transmit_packet(req,
522 p0, port->data[0], port->data[1], b);
523 break;
524 case STATE_SYSEX_0:
525 port->data[0] = b;
526 port->state = STATE_SYSEX_1;
527 break;
528 case STATE_SYSEX_1:
529 port->data[1] = b;
530 port->state = STATE_SYSEX_2;
531 break;
532 case STATE_SYSEX_2:
533 f_midi_transmit_packet(req,
534 p0 | 0x04, port->data[0], port->data[1], b);
535 port->state = STATE_SYSEX_0;
536 break;
537 }
538 }
539}
540
541static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
542{
543 struct usb_ep *ep = midi->in_ep;
544 int i;
545
546 if (!ep)
547 return;
548
549 if (!req)
550 req = alloc_ep_req(ep, midi->buflen);
551
552 if (!req) {
553 ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
554 return;
555 }
556 req->length = 0;
557 req->complete = f_midi_complete;
558
559 for (i = 0; i < MAX_PORTS; i++) {
560 struct gmidi_in_port *port = midi->in_port[i];
561 struct snd_rawmidi_substream *substream = midi->in_substream[i];
562
563 if (!port || !port->active || !substream)
564 continue;
565
566 while (req->length + 3 < midi->buflen) {
567 uint8_t b;
568 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
569 port->active = 0;
570 break;
571 }
572 f_midi_transmit_byte(req, port, b);
573 }
574 }
575
576 if (req->length > 0)
577 usb_ep_queue(ep, req, GFP_ATOMIC);
578 else
579 free_ep_req(ep, req);
580}
581
582static void f_midi_in_tasklet(unsigned long data)
583{
584 struct f_midi *midi = (struct f_midi *) data;
585 f_midi_transmit(midi, NULL);
586}
587
588static int f_midi_in_open(struct snd_rawmidi_substream *substream)
589{
590 struct f_midi *midi = substream->rmidi->private_data;
591
592 if (!midi->in_port[substream->number])
593 return -EINVAL;
594
595 VDBG(midi, "%s()\n", __func__);
596 midi->in_substream[substream->number] = substream;
597 midi->in_port[substream->number]->state = STATE_UNKNOWN;
598 return 0;
599}
600
601static int f_midi_in_close(struct snd_rawmidi_substream *substream)
602{
603 struct f_midi *midi = substream->rmidi->private_data;
604
605 VDBG(midi, "%s()\n", __func__);
606 return 0;
607}
608
609static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
610{
611 struct f_midi *midi = substream->rmidi->private_data;
612
613 if (!midi->in_port[substream->number])
614 return;
615
616 VDBG(midi, "%s() %d\n", __func__, up);
617 midi->in_port[substream->number]->active = up;
618 if (up)
619 tasklet_hi_schedule(&midi->tasklet);
620}
621
622static int f_midi_out_open(struct snd_rawmidi_substream *substream)
623{
624 struct f_midi *midi = substream->rmidi->private_data;
625
626 if (substream->number >= MAX_PORTS)
627 return -EINVAL;
628
629 VDBG(midi, "%s()\n", __func__);
630 midi->out_substream[substream->number] = substream;
631 return 0;
632}
633
634static int f_midi_out_close(struct snd_rawmidi_substream *substream)
635{
636 struct f_midi *midi = substream->rmidi->private_data;
637
638 VDBG(midi, "%s()\n", __func__);
639 return 0;
640}
641
642static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
643{
644 struct f_midi *midi = substream->rmidi->private_data;
645
646 VDBG(midi, "%s()\n", __func__);
647
648 if (up)
649 set_bit(substream->number, &midi->out_triggered);
650 else
651 clear_bit(substream->number, &midi->out_triggered);
652}
653
654static struct snd_rawmidi_ops gmidi_in_ops = {
655 .open = f_midi_in_open,
656 .close = f_midi_in_close,
657 .trigger = f_midi_in_trigger,
658};
659
660static struct snd_rawmidi_ops gmidi_out_ops = {
661 .open = f_midi_out_open,
662 .close = f_midi_out_close,
663 .trigger = f_midi_out_trigger
664};
665
666/* register as a sound "card" */
667static int f_midi_register_card(struct f_midi *midi)
668{
669 struct snd_card *card;
670 struct snd_rawmidi *rmidi;
671 int err;
672 static struct snd_device_ops ops = {
673 .dev_free = f_midi_snd_free,
674 };
675
676 err = snd_card_create(midi->index, midi->id, THIS_MODULE, 0, &card);
677 if (err < 0) {
678 ERROR(midi, "snd_card_create() failed\n");
679 goto fail;
680 }
681 midi->card = card;
682
683 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
684 if (err < 0) {
685 ERROR(midi, "snd_device_new() failed: error %d\n", err);
686 goto fail;
687 }
688
689 strcpy(card->driver, f_midi_longname);
690 strcpy(card->longname, f_midi_longname);
691 strcpy(card->shortname, f_midi_shortname);
692
693 /* Set up rawmidi */
694 snd_component_add(card, "MIDI");
695 err = snd_rawmidi_new(card, card->longname, 0,
696 midi->out_ports, midi->in_ports, &rmidi);
697 if (err < 0) {
698 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
699 goto fail;
700 }
701 midi->rmidi = rmidi;
702 strcpy(rmidi->name, card->shortname);
703 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
704 SNDRV_RAWMIDI_INFO_INPUT |
705 SNDRV_RAWMIDI_INFO_DUPLEX;
706 rmidi->private_data = midi;
707
708 /*
709 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
710 * It's an upside-down world being a gadget.
711 */
712 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
713 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
714
715 snd_card_set_dev(card, &midi->gadget->dev);
716
717 /* register it - we're ready to go */
718 err = snd_card_register(card);
719 if (err < 0) {
720 ERROR(midi, "snd_card_register() failed\n");
721 goto fail;
722 }
723
724 VDBG(midi, "%s() finished ok\n", __func__);
725 return 0;
726
727fail:
728 if (midi->card) {
729 snd_card_free(midi->card);
730 midi->card = NULL;
731 }
732 return err;
733}
734
735/* MIDI function driver setup/binding */
736
737static int __init
738f_midi_bind(struct usb_configuration *c, struct usb_function *f)
739{
740 struct usb_descriptor_header **midi_function;
741 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
742 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
743 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
744 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
745 struct usb_composite_dev *cdev = c->cdev;
746 struct f_midi *midi = func_to_midi(f);
747 int status, n, jack = 1, i = 0;
748
749 /* maybe allocate device-global string ID */
750 if (midi_string_defs[0].id == 0) {
751 status = usb_string_id(c->cdev);
752 if (status < 0)
753 goto fail;
754 midi_string_defs[0].id = status;
755 }
756
757 /* We have two interfaces, AudioControl and MIDIStreaming */
758 status = usb_interface_id(c, f);
759 if (status < 0)
760 goto fail;
761 ac_interface_desc.bInterfaceNumber = status;
762
763 status = usb_interface_id(c, f);
764 if (status < 0)
765 goto fail;
766 ms_interface_desc.bInterfaceNumber = status;
767 ac_header_desc.baInterfaceNr[0] = status;
768
769 status = -ENODEV;
770
771 /* allocate instance-specific endpoints */
772 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
773 if (!midi->in_ep)
774 goto fail;
775 midi->in_ep->driver_data = cdev; /* claim */
776
777 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
778 if (!midi->out_ep)
779 goto fail;
780 midi->out_ep->driver_data = cdev; /* claim */
781
782 /* allocate temporary function list */
783 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(midi_function),
784 GFP_KERNEL);
785 if (!midi_function) {
786 status = -ENOMEM;
787 goto fail;
788 }
789
790 /*
791 * construct the function's descriptor set. As the number of
792 * input and output MIDI ports is configurable, we have to do
793 * it that way.
794 */
795
796 /* add the headers - these are always the same */
797 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
798 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
799 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
800
801 /* calculate the header's wTotalLength */
802 n = USB_DT_MS_HEADER_SIZE
803 + (midi->in_ports + midi->out_ports) *
804 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
805 ms_header_desc.wTotalLength = cpu_to_le16(n);
806
807 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
808
809 /* configure the external IN jacks, each linked to an embedded OUT jack */
810 for (n = 0; n < midi->in_ports; n++) {
811 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
812 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
813
814 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
815 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
816 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
817 in_ext->bJackType = USB_MS_EXTERNAL;
818 in_ext->bJackID = jack++;
819 in_ext->iJack = 0;
820 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
821
822 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
823 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
824 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
825 out_emb->bJackType = USB_MS_EMBEDDED;
826 out_emb->bJackID = jack++;
827 out_emb->bNrInputPins = 1;
828 out_emb->pins[0].baSourcePin = 1;
829 out_emb->pins[0].baSourceID = in_ext->bJackID;
830 out_emb->iJack = 0;
831 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
832
833 /* link it to the endpoint */
834 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
835 }
836
837 /* configure the external OUT jacks, each linked to an embedded IN jack */
838 for (n = 0; n < midi->out_ports; n++) {
839 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
840 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
841
842 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
843 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
844 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
845 in_emb->bJackType = USB_MS_EMBEDDED;
846 in_emb->bJackID = jack++;
847 in_emb->iJack = 0;
848 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
849
850 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
851 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
852 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
853 out_ext->bJackType = USB_MS_EXTERNAL;
854 out_ext->bJackID = jack++;
855 out_ext->bNrInputPins = 1;
856 out_ext->iJack = 0;
857 out_ext->pins[0].baSourceID = in_emb->bJackID;
858 out_ext->pins[0].baSourcePin = 1;
859 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
860
861 /* link it to the endpoint */
862 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
863 }
864
865 /* configure the endpoint descriptors ... */
866 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
867 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
868
869 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
870 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
871
872 /* ... and add them to the list */
873 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
874 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
875 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
876 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
877 midi_function[i++] = NULL;
878
879 /*
880 * support all relevant hardware speeds... we expect that when
881 * hardware is dual speed, all bulk-capable endpoints work at
882 * both speeds
883 */
884 /* copy descriptors, and track endpoint copies */
885 if (gadget_is_dualspeed(c->cdev->gadget)) {
886 c->highspeed = true;
887 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
888 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
889 f->hs_descriptors = usb_copy_descriptors(midi_function);
890 } else {
891 f->descriptors = usb_copy_descriptors(midi_function);
892 }
893
894 kfree(midi_function);
895
896 return 0;
897
898fail:
899 /* we might as well release our claims on endpoints */
900 if (midi->out_ep)
901 midi->out_ep->driver_data = NULL;
902 if (midi->in_ep)
903 midi->in_ep->driver_data = NULL;
904
905 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
906
907 return status;
908}
909
910/**
911 * f_midi_bind_config - add USB MIDI function to a configuration
912 * @c: the configuration to supcard the USB audio function
913 * @index: the soundcard index to use for the ALSA device creation
914 * @id: the soundcard id to use for the ALSA device creation
915 * @buflen: the buffer length to use
916 * @qlen the number of read requests to pre-allocate
917 * Context: single threaded during gadget setup
918 *
919 * Returns zero on success, else negative errno.
920 */
921int __init f_midi_bind_config(struct usb_configuration *c,
922 int index, char *id,
923 unsigned int in_ports,
924 unsigned int out_ports,
925 unsigned int buflen,
926 unsigned int qlen)
927{
928 struct f_midi *midi;
929 int status, i;
930
931 /* sanity check */
932 if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
933 return -EINVAL;
934
935 /* allocate and initialize one new instance */
936 midi = kzalloc(sizeof *midi, GFP_KERNEL);
937 if (!midi) {
938 status = -ENOMEM;
939 goto fail;
940 }
941
942 for (i = 0; i < in_ports; i++) {
943 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
944 if (!port) {
945 status = -ENOMEM;
946 goto setup_fail;
947 }
948
949 port->midi = midi;
950 port->active = 0;
951 port->cable = i;
952 midi->in_port[i] = port;
953 }
954
955 midi->gadget = c->cdev->gadget;
956 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
957
958 /* set up ALSA midi devices */
959 midi->in_ports = in_ports;
960 midi->out_ports = out_ports;
961 status = f_midi_register_card(midi);
962 if (status < 0)
963 goto setup_fail;
964
965 midi->func.name = "gmidi function";
966 midi->func.strings = midi_strings;
967 midi->func.bind = f_midi_bind;
968 midi->func.unbind = f_midi_unbind;
969 midi->func.set_alt = f_midi_set_alt;
970 midi->func.disable = f_midi_disable;
971
972 midi->id = kstrdup(id, GFP_KERNEL);
973 midi->index = index;
974 midi->buflen = buflen;
975 midi->qlen = qlen;
976
977 status = usb_add_function(c, &midi->func);
978 if (status)
979 goto setup_fail;
980
981 return 0;
982
983setup_fail:
984 for (--i; i >= 0; i--)
985 kfree(midi->in_port[i]);
986 kfree(midi);
987fail:
988 return status;
989}
990
diff --git a/drivers/usb/gadget/f_ncm.c b/drivers/usb/gadget/f_ncm.c
index ae69ed7e6b99..aab8eded045b 100644
--- a/drivers/usb/gadget/f_ncm.c
+++ b/drivers/usb/gadget/f_ncm.c
@@ -13,15 +13,6 @@
13 * it under the terms of the GNU General Public License as published by 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 14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version. 15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */ 16 */
26 17
27#include <linux/kernel.h> 18#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/f_obex.c b/drivers/usb/gadget/f_obex.c
index 394502abeb96..5f400f66aa9b 100644
--- a/drivers/usb/gadget/f_obex.c
+++ b/drivers/usb/gadget/f_obex.c
@@ -10,15 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 13 */
23 14
24/* #define VERBOSE_DEBUG */ 15/* #define VERBOSE_DEBUG */
@@ -26,6 +17,7 @@
26#include <linux/slab.h> 17#include <linux/slab.h>
27#include <linux/kernel.h> 18#include <linux/kernel.h>
28#include <linux/device.h> 19#include <linux/device.h>
20#include <linux/module.h>
29 21
30#include "u_serial.h" 22#include "u_serial.h"
31#include "gadget_chips.h" 23#include "gadget_chips.h"
diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c
index 8f3eab1af885..16a509ae517b 100644
--- a/drivers/usb/gadget/f_phonet.c
+++ b/drivers/usb/gadget/f_phonet.c
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or 8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License 9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation. 10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */ 11 */
22 12
23#include <linux/mm.h> 13#include <linux/mm.h>
@@ -356,7 +346,7 @@ static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
356 } 346 }
357 347
358 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 348 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
359 skb->len == 0, req->actual); 349 skb->len <= 1, req->actual);
360 page = NULL; 350 page = NULL;
361 351
362 if (req->actual < req->length) { /* Last fragment */ 352 if (req->actual < req->length) { /* Last fragment */
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index 3ea4666be3d0..704d1d94f72a 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -11,15 +11,6 @@
11 * it under the terms of the GNU General Public License as published by 11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. 13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */ 14 */
24 15
25/* #define VERBOSE_DEBUG */ 16/* #define VERBOSE_DEBUG */
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
index e18b4f520951..7aa7ac82c02c 100644
--- a/drivers/usb/gadget/f_sourcesink.c
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22/* #define VERBOSE_DEBUG */ 13/* #define VERBOSE_DEBUG */
@@ -24,6 +15,7 @@
24#include <linux/slab.h> 15#include <linux/slab.h>
25#include <linux/kernel.h> 16#include <linux/kernel.h>
26#include <linux/device.h> 17#include <linux/device.h>
18#include <linux/module.h>
27 19
28#include "g_zero.h" 20#include "g_zero.h"
29#include "gadget_chips.h" 21#include "gadget_chips.h"
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c
index 3dc53754ab60..c1540648125a 100644
--- a/drivers/usb/gadget/f_subset.c
+++ b/drivers/usb/gadget/f_subset.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22#include <linux/slab.h> 13#include <linux/slab.h>
diff --git a/drivers/usb/gadget/f_uvc.c b/drivers/usb/gadget/f_uvc.c
index 7a8b9aa4aea5..2022fe492148 100644
--- a/drivers/usb/gadget/f_uvc.c
+++ b/drivers/usb/gadget/f_uvc.c
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#include <linux/kernel.h> 13#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/f_uvc.h b/drivers/usb/gadget/f_uvc.h
index e18a6636c283..abf832935134 100644
--- a/drivers/usb/gadget/f_uvc.h
+++ b/drivers/usb/gadget/f_uvc.h
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#ifndef _F_UVC_H_ 13#ifndef _F_UVC_H_
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 639e14a2fd15..11b5196284ae 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -69,8 +69,7 @@
69 * each LUN would be settable independently as a disk drive or a CD-ROM 69 * each LUN would be settable independently as a disk drive or a CD-ROM
70 * drive, but currently all LUNs have to be the same type. The CD-ROM 70 * drive, but currently all LUNs have to be the same type. The CD-ROM
71 * emulation includes a single data track and no audio tracks; hence there 71 * emulation includes a single data track and no audio tracks; hence there
72 * need be only one backing file per LUN. Note also that the CD-ROM block 72 * need be only one backing file per LUN.
73 * length is set to 512 rather than the more common value 2048.
74 * 73 *
75 * Requirements are modest; only a bulk-in and a bulk-out endpoint are 74 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
76 * needed (an interrupt-out endpoint is also needed for CBI). The memory 75 * needed (an interrupt-out endpoint is also needed for CBI). The memory
@@ -244,6 +243,7 @@
244#include <linux/kref.h> 243#include <linux/kref.h>
245#include <linux/kthread.h> 244#include <linux/kthread.h>
246#include <linux/limits.h> 245#include <linux/limits.h>
246#include <linux/module.h>
247#include <linux/rwsem.h> 247#include <linux/rwsem.h>
248#include <linux/slab.h> 248#include <linux/slab.h>
249#include <linux/spinlock.h> 249#include <linux/spinlock.h>
@@ -461,7 +461,6 @@ struct fsg_dev {
461 461
462 struct fsg_buffhd *next_buffhd_to_fill; 462 struct fsg_buffhd *next_buffhd_to_fill;
463 struct fsg_buffhd *next_buffhd_to_drain; 463 struct fsg_buffhd *next_buffhd_to_drain;
464 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
465 464
466 int thread_wakeup_needed; 465 int thread_wakeup_needed;
467 struct completion thread_notifier; 466 struct completion thread_notifier;
@@ -488,6 +487,8 @@ struct fsg_dev {
488 unsigned int nluns; 487 unsigned int nluns;
489 struct fsg_lun *luns; 488 struct fsg_lun *luns;
490 struct fsg_lun *curlun; 489 struct fsg_lun *curlun;
490 /* Must be the last entry */
491 struct fsg_buffhd buffhds[];
491}; 492};
492 493
493typedef void (*fsg_routine_t)(struct fsg_dev *); 494typedef void (*fsg_routine_t)(struct fsg_dev *);
@@ -586,7 +587,19 @@ dev_qualifier = {
586 .bNumConfigurations = 1, 587 .bNumConfigurations = 1,
587}; 588};
588 589
590static int populate_bos(struct fsg_dev *fsg, u8 *buf)
591{
592 memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
593 buf += USB_DT_BOS_SIZE;
594
595 memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
596 buf += USB_DT_USB_EXT_CAP_SIZE;
589 597
598 memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
599
600 return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
601 + USB_DT_USB_EXT_CAP_SIZE;
602}
590 603
591/* 604/*
592 * Config descriptors must agree with the code that sets configurations 605 * Config descriptors must agree with the code that sets configurations
@@ -846,7 +859,7 @@ static int class_setup_req(struct fsg_dev *fsg,
846 if (ctrl->bRequestType != (USB_DIR_OUT | 859 if (ctrl->bRequestType != (USB_DIR_OUT |
847 USB_TYPE_CLASS | USB_RECIP_INTERFACE)) 860 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
848 break; 861 break;
849 if (w_index != 0 || w_value != 0) { 862 if (w_index != 0 || w_value != 0 || w_length != 0) {
850 value = -EDOM; 863 value = -EDOM;
851 break; 864 break;
852 } 865 }
@@ -862,7 +875,7 @@ static int class_setup_req(struct fsg_dev *fsg,
862 if (ctrl->bRequestType != (USB_DIR_IN | 875 if (ctrl->bRequestType != (USB_DIR_IN |
863 USB_TYPE_CLASS | USB_RECIP_INTERFACE)) 876 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
864 break; 877 break;
865 if (w_index != 0 || w_value != 0) { 878 if (w_index != 0 || w_value != 0 || w_length != 1) {
866 value = -EDOM; 879 value = -EDOM;
867 break; 880 break;
868 } 881 }
@@ -935,7 +948,8 @@ static int standard_setup_req(struct fsg_dev *fsg,
935 break; 948 break;
936 case USB_DT_DEVICE_QUALIFIER: 949 case USB_DT_DEVICE_QUALIFIER:
937 VDBG(fsg, "get device qualifier\n"); 950 VDBG(fsg, "get device qualifier\n");
938 if (!gadget_is_dualspeed(fsg->gadget)) 951 if (!gadget_is_dualspeed(fsg->gadget) ||
952 fsg->gadget->speed == USB_SPEED_SUPER)
939 break; 953 break;
940 /* 954 /*
941 * Assume ep0 uses the same maxpacket value for both 955 * Assume ep0 uses the same maxpacket value for both
@@ -948,7 +962,8 @@ static int standard_setup_req(struct fsg_dev *fsg,
948 962
949 case USB_DT_OTHER_SPEED_CONFIG: 963 case USB_DT_OTHER_SPEED_CONFIG:
950 VDBG(fsg, "get other-speed config descriptor\n"); 964 VDBG(fsg, "get other-speed config descriptor\n");
951 if (!gadget_is_dualspeed(fsg->gadget)) 965 if (!gadget_is_dualspeed(fsg->gadget) ||
966 fsg->gadget->speed == USB_SPEED_SUPER)
952 break; 967 break;
953 goto get_config; 968 goto get_config;
954 case USB_DT_CONFIG: 969 case USB_DT_CONFIG:
@@ -967,7 +982,15 @@ get_config:
967 value = usb_gadget_get_string(&fsg_stringtab, 982 value = usb_gadget_get_string(&fsg_stringtab,
968 w_value & 0xff, req->buf); 983 w_value & 0xff, req->buf);
969 break; 984 break;
985
986 case USB_DT_BOS:
987 VDBG(fsg, "get bos descriptor\n");
988
989 if (gadget_is_superspeed(fsg->gadget))
990 value = populate_bos(fsg, req->buf);
991 break;
970 } 992 }
993
971 break; 994 break;
972 995
973 /* One config, two speeds */ 996 /* One config, two speeds */
@@ -1136,7 +1159,6 @@ static int do_read(struct fsg_dev *fsg)
1136 u32 amount_left; 1159 u32 amount_left;
1137 loff_t file_offset, file_offset_tmp; 1160 loff_t file_offset, file_offset_tmp;
1138 unsigned int amount; 1161 unsigned int amount;
1139 unsigned int partial_page;
1140 ssize_t nread; 1162 ssize_t nread;
1141 1163
1142 /* Get the starting Logical Block Address and check that it's 1164 /* Get the starting Logical Block Address and check that it's
@@ -1158,7 +1180,7 @@ static int do_read(struct fsg_dev *fsg)
1158 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1180 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1159 return -EINVAL; 1181 return -EINVAL;
1160 } 1182 }
1161 file_offset = ((loff_t) lba) << 9; 1183 file_offset = ((loff_t) lba) << curlun->blkbits;
1162 1184
1163 /* Carry out the file reads */ 1185 /* Carry out the file reads */
1164 amount_left = fsg->data_size_from_cmnd; 1186 amount_left = fsg->data_size_from_cmnd;
@@ -1171,17 +1193,10 @@ static int do_read(struct fsg_dev *fsg)
1171 * Try to read the remaining amount. 1193 * Try to read the remaining amount.
1172 * But don't read more than the buffer size. 1194 * But don't read more than the buffer size.
1173 * And don't try to read past the end of the file. 1195 * And don't try to read past the end of the file.
1174 * Finally, if we're not at a page boundary, don't read past 1196 */
1175 * the next page.
1176 * If this means reading 0 then we were asked to read past
1177 * the end of file. */
1178 amount = min((unsigned int) amount_left, mod_data.buflen); 1197 amount = min((unsigned int) amount_left, mod_data.buflen);
1179 amount = min((loff_t) amount, 1198 amount = min((loff_t) amount,
1180 curlun->file_length - file_offset); 1199 curlun->file_length - file_offset);
1181 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
1182 if (partial_page > 0)
1183 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
1184 partial_page);
1185 1200
1186 /* Wait for the next buffer to become available */ 1201 /* Wait for the next buffer to become available */
1187 bh = fsg->next_buffhd_to_fill; 1202 bh = fsg->next_buffhd_to_fill;
@@ -1196,7 +1211,7 @@ static int do_read(struct fsg_dev *fsg)
1196 if (amount == 0) { 1211 if (amount == 0) {
1197 curlun->sense_data = 1212 curlun->sense_data =
1198 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1213 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1199 curlun->sense_data_info = file_offset >> 9; 1214 curlun->sense_data_info = file_offset >> curlun->blkbits;
1200 curlun->info_valid = 1; 1215 curlun->info_valid = 1;
1201 bh->inreq->length = 0; 1216 bh->inreq->length = 0;
1202 bh->state = BUF_STATE_FULL; 1217 bh->state = BUF_STATE_FULL;
@@ -1221,18 +1236,23 @@ static int do_read(struct fsg_dev *fsg)
1221 } else if (nread < amount) { 1236 } else if (nread < amount) {
1222 LDBG(curlun, "partial file read: %d/%u\n", 1237 LDBG(curlun, "partial file read: %d/%u\n",
1223 (int) nread, amount); 1238 (int) nread, amount);
1224 nread -= (nread & 511); // Round down to a block 1239 nread = round_down(nread, curlun->blksize);
1225 } 1240 }
1226 file_offset += nread; 1241 file_offset += nread;
1227 amount_left -= nread; 1242 amount_left -= nread;
1228 fsg->residue -= nread; 1243 fsg->residue -= nread;
1244
1245 /* Except at the end of the transfer, nread will be
1246 * equal to the buffer size, which is divisible by the
1247 * bulk-in maxpacket size.
1248 */
1229 bh->inreq->length = nread; 1249 bh->inreq->length = nread;
1230 bh->state = BUF_STATE_FULL; 1250 bh->state = BUF_STATE_FULL;
1231 1251
1232 /* If an error occurred, report it and its position */ 1252 /* If an error occurred, report it and its position */
1233 if (nread < amount) { 1253 if (nread < amount) {
1234 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1254 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1235 curlun->sense_data_info = file_offset >> 9; 1255 curlun->sense_data_info = file_offset >> curlun->blkbits;
1236 curlun->info_valid = 1; 1256 curlun->info_valid = 1;
1237 break; 1257 break;
1238 } 1258 }
@@ -1262,7 +1282,6 @@ static int do_write(struct fsg_dev *fsg)
1262 u32 amount_left_to_req, amount_left_to_write; 1282 u32 amount_left_to_req, amount_left_to_write;
1263 loff_t usb_offset, file_offset, file_offset_tmp; 1283 loff_t usb_offset, file_offset, file_offset_tmp;
1264 unsigned int amount; 1284 unsigned int amount;
1265 unsigned int partial_page;
1266 ssize_t nwritten; 1285 ssize_t nwritten;
1267 int rc; 1286 int rc;
1268 1287
@@ -1303,7 +1322,7 @@ static int do_write(struct fsg_dev *fsg)
1303 1322
1304 /* Carry out the file writes */ 1323 /* Carry out the file writes */
1305 get_some_more = 1; 1324 get_some_more = 1;
1306 file_offset = usb_offset = ((loff_t) lba) << 9; 1325 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
1307 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd; 1326 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1308 1327
1309 while (amount_left_to_write > 0) { 1328 while (amount_left_to_write > 0) {
@@ -1313,38 +1332,20 @@ static int do_write(struct fsg_dev *fsg)
1313 if (bh->state == BUF_STATE_EMPTY && get_some_more) { 1332 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1314 1333
1315 /* Figure out how much we want to get: 1334 /* Figure out how much we want to get:
1316 * Try to get the remaining amount. 1335 * Try to get the remaining amount,
1317 * But don't get more than the buffer size. 1336 * but not more than the buffer size.
1318 * And don't try to go past the end of the file. 1337 */
1319 * If we're not at a page boundary,
1320 * don't go past the next page.
1321 * If this means getting 0, then we were asked
1322 * to write past the end of file.
1323 * Finally, round down to a block boundary. */
1324 amount = min(amount_left_to_req, mod_data.buflen); 1338 amount = min(amount_left_to_req, mod_data.buflen);
1325 amount = min((loff_t) amount, curlun->file_length - 1339
1326 usb_offset); 1340 /* Beyond the end of the backing file? */
1327 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1); 1341 if (usb_offset >= curlun->file_length) {
1328 if (partial_page > 0)
1329 amount = min(amount,
1330 (unsigned int) PAGE_CACHE_SIZE - partial_page);
1331
1332 if (amount == 0) {
1333 get_some_more = 0; 1342 get_some_more = 0;
1334 curlun->sense_data = 1343 curlun->sense_data =
1335 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1344 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1336 curlun->sense_data_info = usb_offset >> 9; 1345 curlun->sense_data_info = usb_offset >> curlun->blkbits;
1337 curlun->info_valid = 1; 1346 curlun->info_valid = 1;
1338 continue; 1347 continue;
1339 } 1348 }
1340 amount -= (amount & 511);
1341 if (amount == 0) {
1342
1343 /* Why were we were asked to transfer a
1344 * partial block? */
1345 get_some_more = 0;
1346 continue;
1347 }
1348 1349
1349 /* Get the next buffer */ 1350 /* Get the next buffer */
1350 usb_offset += amount; 1351 usb_offset += amount;
@@ -1353,11 +1354,11 @@ static int do_write(struct fsg_dev *fsg)
1353 if (amount_left_to_req == 0) 1354 if (amount_left_to_req == 0)
1354 get_some_more = 0; 1355 get_some_more = 0;
1355 1356
1356 /* amount is always divisible by 512, hence by 1357 /* Except at the end of the transfer, amount will be
1357 * the bulk-out maxpacket size */ 1358 * equal to the buffer size, which is divisible by
1358 bh->outreq->length = bh->bulk_out_intended_length = 1359 * the bulk-out maxpacket size.
1359 amount; 1360 */
1360 bh->outreq->short_not_ok = 1; 1361 set_bulk_out_req_length(fsg, bh, amount);
1361 start_transfer(fsg, fsg->bulk_out, bh->outreq, 1362 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1362 &bh->outreq_busy, &bh->state); 1363 &bh->outreq_busy, &bh->state);
1363 fsg->next_buffhd_to_fill = bh->next; 1364 fsg->next_buffhd_to_fill = bh->next;
@@ -1376,7 +1377,7 @@ static int do_write(struct fsg_dev *fsg)
1376 /* Did something go wrong with the transfer? */ 1377 /* Did something go wrong with the transfer? */
1377 if (bh->outreq->status != 0) { 1378 if (bh->outreq->status != 0) {
1378 curlun->sense_data = SS_COMMUNICATION_FAILURE; 1379 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1379 curlun->sense_data_info = file_offset >> 9; 1380 curlun->sense_data_info = file_offset >> curlun->blkbits;
1380 curlun->info_valid = 1; 1381 curlun->info_valid = 1;
1381 break; 1382 break;
1382 } 1383 }
@@ -1390,6 +1391,16 @@ static int do_write(struct fsg_dev *fsg)
1390 amount = curlun->file_length - file_offset; 1391 amount = curlun->file_length - file_offset;
1391 } 1392 }
1392 1393
1394 /* Don't accept excess data. The spec doesn't say
1395 * what to do in this case. We'll ignore the error.
1396 */
1397 amount = min(amount, bh->bulk_out_intended_length);
1398
1399 /* Don't write a partial block */
1400 amount = round_down(amount, curlun->blksize);
1401 if (amount == 0)
1402 goto empty_write;
1403
1393 /* Perform the write */ 1404 /* Perform the write */
1394 file_offset_tmp = file_offset; 1405 file_offset_tmp = file_offset;
1395 nwritten = vfs_write(curlun->filp, 1406 nwritten = vfs_write(curlun->filp,
@@ -1408,8 +1419,7 @@ static int do_write(struct fsg_dev *fsg)
1408 } else if (nwritten < amount) { 1419 } else if (nwritten < amount) {
1409 LDBG(curlun, "partial file write: %d/%u\n", 1420 LDBG(curlun, "partial file write: %d/%u\n",
1410 (int) nwritten, amount); 1421 (int) nwritten, amount);
1411 nwritten -= (nwritten & 511); 1422 nwritten = round_down(nwritten, curlun->blksize);
1412 // Round down to a block
1413 } 1423 }
1414 file_offset += nwritten; 1424 file_offset += nwritten;
1415 amount_left_to_write -= nwritten; 1425 amount_left_to_write -= nwritten;
@@ -1418,13 +1428,14 @@ static int do_write(struct fsg_dev *fsg)
1418 /* If an error occurred, report it and its position */ 1428 /* If an error occurred, report it and its position */
1419 if (nwritten < amount) { 1429 if (nwritten < amount) {
1420 curlun->sense_data = SS_WRITE_ERROR; 1430 curlun->sense_data = SS_WRITE_ERROR;
1421 curlun->sense_data_info = file_offset >> 9; 1431 curlun->sense_data_info = file_offset >> curlun->blkbits;
1422 curlun->info_valid = 1; 1432 curlun->info_valid = 1;
1423 break; 1433 break;
1424 } 1434 }
1425 1435
1436 empty_write:
1426 /* Did the host decide to stop early? */ 1437 /* Did the host decide to stop early? */
1427 if (bh->outreq->actual != bh->outreq->length) { 1438 if (bh->outreq->actual < bh->bulk_out_intended_length) {
1428 fsg->short_packet_received = 1; 1439 fsg->short_packet_received = 1;
1429 break; 1440 break;
1430 } 1441 }
@@ -1500,8 +1511,8 @@ static int do_verify(struct fsg_dev *fsg)
1500 return -EIO; // No default reply 1511 return -EIO; // No default reply
1501 1512
1502 /* Prepare to carry out the file verify */ 1513 /* Prepare to carry out the file verify */
1503 amount_left = verification_length << 9; 1514 amount_left = verification_length << curlun->blkbits;
1504 file_offset = ((loff_t) lba) << 9; 1515 file_offset = ((loff_t) lba) << curlun->blkbits;
1505 1516
1506 /* Write out all the dirty buffers before invalidating them */ 1517 /* Write out all the dirty buffers before invalidating them */
1507 fsg_lun_fsync_sub(curlun); 1518 fsg_lun_fsync_sub(curlun);
@@ -1519,15 +1530,14 @@ static int do_verify(struct fsg_dev *fsg)
1519 * Try to read the remaining amount, but not more than 1530 * Try to read the remaining amount, but not more than
1520 * the buffer size. 1531 * the buffer size.
1521 * And don't try to read past the end of the file. 1532 * And don't try to read past the end of the file.
1522 * If this means reading 0 then we were asked to read 1533 */
1523 * past the end of file. */
1524 amount = min((unsigned int) amount_left, mod_data.buflen); 1534 amount = min((unsigned int) amount_left, mod_data.buflen);
1525 amount = min((loff_t) amount, 1535 amount = min((loff_t) amount,
1526 curlun->file_length - file_offset); 1536 curlun->file_length - file_offset);
1527 if (amount == 0) { 1537 if (amount == 0) {
1528 curlun->sense_data = 1538 curlun->sense_data =
1529 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1539 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1530 curlun->sense_data_info = file_offset >> 9; 1540 curlun->sense_data_info = file_offset >> curlun->blkbits;
1531 curlun->info_valid = 1; 1541 curlun->info_valid = 1;
1532 break; 1542 break;
1533 } 1543 }
@@ -1550,11 +1560,11 @@ static int do_verify(struct fsg_dev *fsg)
1550 } else if (nread < amount) { 1560 } else if (nread < amount) {
1551 LDBG(curlun, "partial file verify: %d/%u\n", 1561 LDBG(curlun, "partial file verify: %d/%u\n",
1552 (int) nread, amount); 1562 (int) nread, amount);
1553 nread -= (nread & 511); // Round down to a sector 1563 nread = round_down(nread, curlun->blksize);
1554 } 1564 }
1555 if (nread == 0) { 1565 if (nread == 0) {
1556 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1566 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1557 curlun->sense_data_info = file_offset >> 9; 1567 curlun->sense_data_info = file_offset >> curlun->blkbits;
1558 curlun->info_valid = 1; 1568 curlun->info_valid = 1;
1559 break; 1569 break;
1560 } 1570 }
@@ -1668,7 +1678,7 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1668 1678
1669 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]); 1679 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1670 /* Max logical block */ 1680 /* Max logical block */
1671 put_unaligned_be32(512, &buf[4]); /* Block length */ 1681 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
1672 return 8; 1682 return 8;
1673} 1683}
1674 1684
@@ -1890,7 +1900,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg,
1890 1900
1891 put_unaligned_be32(curlun->num_sectors, &buf[0]); 1901 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1892 /* Number of blocks */ 1902 /* Number of blocks */
1893 put_unaligned_be32(512, &buf[4]); /* Block length */ 1903 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
1894 buf[4] = 0x02; /* Current capacity */ 1904 buf[4] = 0x02; /* Current capacity */
1895 return 12; 1905 return 12;
1896} 1906}
@@ -1969,7 +1979,7 @@ static int throw_away_data(struct fsg_dev *fsg)
1969 fsg->next_buffhd_to_drain = bh->next; 1979 fsg->next_buffhd_to_drain = bh->next;
1970 1980
1971 /* A short packet or an error ends everything */ 1981 /* A short packet or an error ends everything */
1972 if (bh->outreq->actual != bh->outreq->length || 1982 if (bh->outreq->actual < bh->bulk_out_intended_length ||
1973 bh->outreq->status != 0) { 1983 bh->outreq->status != 0) {
1974 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); 1984 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1975 return -EINTR; 1985 return -EINTR;
@@ -1983,11 +1993,11 @@ static int throw_away_data(struct fsg_dev *fsg)
1983 amount = min(fsg->usb_amount_left, 1993 amount = min(fsg->usb_amount_left,
1984 (u32) mod_data.buflen); 1994 (u32) mod_data.buflen);
1985 1995
1986 /* amount is always divisible by 512, hence by 1996 /* Except at the end of the transfer, amount will be
1987 * the bulk-out maxpacket size */ 1997 * equal to the buffer size, which is divisible by
1988 bh->outreq->length = bh->bulk_out_intended_length = 1998 * the bulk-out maxpacket size.
1989 amount; 1999 */
1990 bh->outreq->short_not_ok = 1; 2000 set_bulk_out_req_length(fsg, bh, amount);
1991 start_transfer(fsg, fsg->bulk_out, bh->outreq, 2001 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1992 &bh->outreq_busy, &bh->state); 2002 &bh->outreq_busy, &bh->state);
1993 fsg->next_buffhd_to_fill = bh->next; 2003 fsg->next_buffhd_to_fill = bh->next;
@@ -2415,7 +2425,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2415 2425
2416 case READ_6: 2426 case READ_6:
2417 i = fsg->cmnd[4]; 2427 i = fsg->cmnd[4];
2418 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2428 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
2419 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2429 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2420 (7<<1) | (1<<4), 1, 2430 (7<<1) | (1<<4), 1,
2421 "READ(6)")) == 0) 2431 "READ(6)")) == 0)
@@ -2424,7 +2434,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2424 2434
2425 case READ_10: 2435 case READ_10:
2426 fsg->data_size_from_cmnd = 2436 fsg->data_size_from_cmnd =
2427 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2437 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
2428 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2438 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2429 (1<<1) | (0xf<<2) | (3<<7), 1, 2439 (1<<1) | (0xf<<2) | (3<<7), 1,
2430 "READ(10)")) == 0) 2440 "READ(10)")) == 0)
@@ -2433,7 +2443,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2433 2443
2434 case READ_12: 2444 case READ_12:
2435 fsg->data_size_from_cmnd = 2445 fsg->data_size_from_cmnd =
2436 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2446 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
2437 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 2447 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2438 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2448 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2439 "READ(12)")) == 0) 2449 "READ(12)")) == 0)
@@ -2519,7 +2529,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2519 2529
2520 case WRITE_6: 2530 case WRITE_6:
2521 i = fsg->cmnd[4]; 2531 i = fsg->cmnd[4];
2522 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2532 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
2523 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2533 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2524 (7<<1) | (1<<4), 1, 2534 (7<<1) | (1<<4), 1,
2525 "WRITE(6)")) == 0) 2535 "WRITE(6)")) == 0)
@@ -2528,7 +2538,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2528 2538
2529 case WRITE_10: 2539 case WRITE_10:
2530 fsg->data_size_from_cmnd = 2540 fsg->data_size_from_cmnd =
2531 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2541 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
2532 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2542 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2533 (1<<1) | (0xf<<2) | (3<<7), 1, 2543 (1<<1) | (0xf<<2) | (3<<7), 1,
2534 "WRITE(10)")) == 0) 2544 "WRITE(10)")) == 0)
@@ -2537,7 +2547,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2537 2547
2538 case WRITE_12: 2548 case WRITE_12:
2539 fsg->data_size_from_cmnd = 2549 fsg->data_size_from_cmnd =
2540 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2550 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
2541 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 2551 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2542 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2552 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2543 "WRITE(12)")) == 0) 2553 "WRITE(12)")) == 0)
@@ -2666,7 +2676,6 @@ static int get_next_command(struct fsg_dev *fsg)
2666 2676
2667 /* Queue a request to read a Bulk-only CBW */ 2677 /* Queue a request to read a Bulk-only CBW */
2668 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN); 2678 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
2669 bh->outreq->short_not_ok = 1;
2670 start_transfer(fsg, fsg->bulk_out, bh->outreq, 2679 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2671 &bh->outreq_busy, &bh->state); 2680 &bh->outreq_busy, &bh->state);
2672 2681
@@ -2752,7 +2761,7 @@ static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2752 2761
2753reset: 2762reset:
2754 /* Deallocate the requests */ 2763 /* Deallocate the requests */
2755 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2764 for (i = 0; i < fsg_num_buffers; ++i) {
2756 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2765 struct fsg_buffhd *bh = &fsg->buffhds[i];
2757 2766
2758 if (bh->inreq) { 2767 if (bh->inreq) {
@@ -2791,29 +2800,32 @@ reset:
2791 2800
2792 /* Enable the endpoints */ 2801 /* Enable the endpoints */
2793 d = fsg_ep_desc(fsg->gadget, 2802 d = fsg_ep_desc(fsg->gadget,
2794 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); 2803 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
2804 &fsg_ss_bulk_in_desc);
2795 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) 2805 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2796 goto reset; 2806 goto reset;
2797 fsg->bulk_in_enabled = 1; 2807 fsg->bulk_in_enabled = 1;
2798 2808
2799 d = fsg_ep_desc(fsg->gadget, 2809 d = fsg_ep_desc(fsg->gadget,
2800 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); 2810 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
2811 &fsg_ss_bulk_out_desc);
2801 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) 2812 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2802 goto reset; 2813 goto reset;
2803 fsg->bulk_out_enabled = 1; 2814 fsg->bulk_out_enabled = 1;
2804 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize); 2815 fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
2805 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); 2816 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2806 2817
2807 if (transport_is_cbi()) { 2818 if (transport_is_cbi()) {
2808 d = fsg_ep_desc(fsg->gadget, 2819 d = fsg_ep_desc(fsg->gadget,
2809 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc); 2820 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
2821 &fsg_ss_intr_in_desc);
2810 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0) 2822 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2811 goto reset; 2823 goto reset;
2812 fsg->intr_in_enabled = 1; 2824 fsg->intr_in_enabled = 1;
2813 } 2825 }
2814 2826
2815 /* Allocate the requests */ 2827 /* Allocate the requests */
2816 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2828 for (i = 0; i < fsg_num_buffers; ++i) {
2817 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2829 struct fsg_buffhd *bh = &fsg->buffhds[i];
2818 2830
2819 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) 2831 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
@@ -2862,17 +2874,10 @@ static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2862 fsg->config = new_config; 2874 fsg->config = new_config;
2863 if ((rc = do_set_interface(fsg, 0)) != 0) 2875 if ((rc = do_set_interface(fsg, 0)) != 0)
2864 fsg->config = 0; // Reset on errors 2876 fsg->config = 0; // Reset on errors
2865 else { 2877 else
2866 char *speed; 2878 INFO(fsg, "%s config #%d\n",
2867 2879 usb_speed_string(fsg->gadget->speed),
2868 switch (fsg->gadget->speed) { 2880 fsg->config);
2869 case USB_SPEED_LOW: speed = "low"; break;
2870 case USB_SPEED_FULL: speed = "full"; break;
2871 case USB_SPEED_HIGH: speed = "high"; break;
2872 default: speed = "?"; break;
2873 }
2874 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2875 }
2876 } 2881 }
2877 return rc; 2882 return rc;
2878} 2883}
@@ -2909,7 +2914,7 @@ static void handle_exception(struct fsg_dev *fsg)
2909 /* Cancel all the pending transfers */ 2914 /* Cancel all the pending transfers */
2910 if (fsg->intreq_busy) 2915 if (fsg->intreq_busy)
2911 usb_ep_dequeue(fsg->intr_in, fsg->intreq); 2916 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2912 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2917 for (i = 0; i < fsg_num_buffers; ++i) {
2913 bh = &fsg->buffhds[i]; 2918 bh = &fsg->buffhds[i];
2914 if (bh->inreq_busy) 2919 if (bh->inreq_busy)
2915 usb_ep_dequeue(fsg->bulk_in, bh->inreq); 2920 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
@@ -2920,7 +2925,7 @@ static void handle_exception(struct fsg_dev *fsg)
2920 /* Wait until everything is idle */ 2925 /* Wait until everything is idle */
2921 for (;;) { 2926 for (;;) {
2922 num_active = fsg->intreq_busy; 2927 num_active = fsg->intreq_busy;
2923 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2928 for (i = 0; i < fsg_num_buffers; ++i) {
2924 bh = &fsg->buffhds[i]; 2929 bh = &fsg->buffhds[i];
2925 num_active += bh->inreq_busy + bh->outreq_busy; 2930 num_active += bh->inreq_busy + bh->outreq_busy;
2926 } 2931 }
@@ -2942,7 +2947,7 @@ static void handle_exception(struct fsg_dev *fsg)
2942 * state, and the exception. Then invoke the handler. */ 2947 * state, and the exception. Then invoke the handler. */
2943 spin_lock_irq(&fsg->lock); 2948 spin_lock_irq(&fsg->lock);
2944 2949
2945 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2950 for (i = 0; i < fsg_num_buffers; ++i) {
2946 bh = &fsg->buffhds[i]; 2951 bh = &fsg->buffhds[i];
2947 bh->state = BUF_STATE_EMPTY; 2952 bh->state = BUF_STATE_EMPTY;
2948 } 2953 }
@@ -3149,6 +3154,15 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3149 DBG(fsg, "unbind\n"); 3154 DBG(fsg, "unbind\n");
3150 clear_bit(REGISTERED, &fsg->atomic_bitflags); 3155 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3151 3156
3157 /* If the thread isn't already dead, tell it to exit now */
3158 if (fsg->state != FSG_STATE_TERMINATED) {
3159 raise_exception(fsg, FSG_STATE_EXIT);
3160 wait_for_completion(&fsg->thread_notifier);
3161
3162 /* The cleanup routine waits for this completion also */
3163 complete(&fsg->thread_notifier);
3164 }
3165
3152 /* Unregister the sysfs attribute files and the LUNs */ 3166 /* Unregister the sysfs attribute files and the LUNs */
3153 for (i = 0; i < fsg->nluns; ++i) { 3167 for (i = 0; i < fsg->nluns; ++i) {
3154 curlun = &fsg->luns[i]; 3168 curlun = &fsg->luns[i];
@@ -3162,17 +3176,8 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3162 } 3176 }
3163 } 3177 }
3164 3178
3165 /* If the thread isn't already dead, tell it to exit now */
3166 if (fsg->state != FSG_STATE_TERMINATED) {
3167 raise_exception(fsg, FSG_STATE_EXIT);
3168 wait_for_completion(&fsg->thread_notifier);
3169
3170 /* The cleanup routine waits for this completion also */
3171 complete(&fsg->thread_notifier);
3172 }
3173
3174 /* Free the data buffers */ 3179 /* Free the data buffers */
3175 for (i = 0; i < FSG_NUM_BUFFERS; ++i) 3180 for (i = 0; i < fsg_num_buffers; ++i)
3176 kfree(fsg->buffhds[i].buf); 3181 kfree(fsg->buffhds[i].buf);
3177 3182
3178 /* Free the request and buffer for endpoint 0 */ 3183 /* Free the request and buffer for endpoint 0 */
@@ -3445,6 +3450,24 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3445 fsg_fs_intr_in_desc.bEndpointAddress; 3450 fsg_fs_intr_in_desc.bEndpointAddress;
3446 } 3451 }
3447 3452
3453 if (gadget_is_superspeed(gadget)) {
3454 unsigned max_burst;
3455
3456 fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
3457
3458 /* Calculate bMaxBurst, we know packet size is 1024 */
3459 max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
3460
3461 /* Assume endpoint addresses are the same for both speeds */
3462 fsg_ss_bulk_in_desc.bEndpointAddress =
3463 fsg_fs_bulk_in_desc.bEndpointAddress;
3464 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
3465
3466 fsg_ss_bulk_out_desc.bEndpointAddress =
3467 fsg_fs_bulk_out_desc.bEndpointAddress;
3468 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
3469 }
3470
3448 if (gadget_is_otg(gadget)) 3471 if (gadget_is_otg(gadget))
3449 fsg_otg_desc.bmAttributes |= USB_OTG_HNP; 3472 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
3450 3473
@@ -3460,7 +3483,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3460 req->complete = ep0_complete; 3483 req->complete = ep0_complete;
3461 3484
3462 /* Allocate the data buffers */ 3485 /* Allocate the data buffers */
3463 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 3486 for (i = 0; i < fsg_num_buffers; ++i) {
3464 struct fsg_buffhd *bh = &fsg->buffhds[i]; 3487 struct fsg_buffhd *bh = &fsg->buffhds[i];
3465 3488
3466 /* Allocate for the bulk-in endpoint. We assume that 3489 /* Allocate for the bulk-in endpoint. We assume that
@@ -3471,7 +3494,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3471 goto out; 3494 goto out;
3472 bh->next = bh + 1; 3495 bh->next = bh + 1;
3473 } 3496 }
3474 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0]; 3497 fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
3475 3498
3476 /* This should reflect the actual gadget power source */ 3499 /* This should reflect the actual gadget power source */
3477 usb_gadget_set_selfpowered(gadget); 3500 usb_gadget_set_selfpowered(gadget);
@@ -3561,11 +3584,7 @@ static void fsg_resume(struct usb_gadget *gadget)
3561/*-------------------------------------------------------------------------*/ 3584/*-------------------------------------------------------------------------*/
3562 3585
3563static struct usb_gadget_driver fsg_driver = { 3586static struct usb_gadget_driver fsg_driver = {
3564#ifdef CONFIG_USB_GADGET_DUALSPEED 3587 .speed = USB_SPEED_SUPER,
3565 .speed = USB_SPEED_HIGH,
3566#else
3567 .speed = USB_SPEED_FULL,
3568#endif
3569 .function = (char *) fsg_string_product, 3588 .function = (char *) fsg_string_product,
3570 .unbind = fsg_unbind, 3589 .unbind = fsg_unbind,
3571 .disconnect = fsg_disconnect, 3590 .disconnect = fsg_disconnect,
@@ -3587,7 +3606,9 @@ static int __init fsg_alloc(void)
3587{ 3606{
3588 struct fsg_dev *fsg; 3607 struct fsg_dev *fsg;
3589 3608
3590 fsg = kzalloc(sizeof *fsg, GFP_KERNEL); 3609 fsg = kzalloc(sizeof *fsg +
3610 fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
3611
3591 if (!fsg) 3612 if (!fsg)
3592 return -ENOMEM; 3613 return -ENOMEM;
3593 spin_lock_init(&fsg->lock); 3614 spin_lock_init(&fsg->lock);
@@ -3605,6 +3626,10 @@ static int __init fsg_init(void)
3605 int rc; 3626 int rc;
3606 struct fsg_dev *fsg; 3627 struct fsg_dev *fsg;
3607 3628
3629 rc = fsg_num_buffers_validate();
3630 if (rc != 0)
3631 return rc;
3632
3608 if ((rc = fsg_alloc()) != 0) 3633 if ((rc = fsg_alloc()) != 0)
3609 return rc; 3634 return rc;
3610 fsg = the_fsg; 3635 fsg = the_fsg;
diff --git a/drivers/usb/gadget/fsl_qe_udc.c b/drivers/usb/gadget/fsl_qe_udc.c
index 3bf872e1ad39..2a03e4de11c1 100644
--- a/drivers/usb/gadget/fsl_qe_udc.c
+++ b/drivers/usb/gadget/fsl_qe_udc.c
@@ -540,7 +540,7 @@ static int qe_ep_init(struct qe_udc *udc,
540 int reval = 0; 540 int reval = 0;
541 u16 max = 0; 541 u16 max = 0;
542 542
543 max = le16_to_cpu(desc->wMaxPacketSize); 543 max = usb_endpoint_maxp(desc);
544 544
545 /* check the max package size validate for this endpoint */ 545 /* check the max package size validate for this endpoint */
546 /* Refer to USB2.0 spec table 9-13, 546 /* Refer to USB2.0 spec table 9-13,
diff --git a/drivers/usb/gadget/fsl_udc_core.c b/drivers/usb/gadget/fsl_udc_core.c
index de24a4233c25..b3b3d83b7c33 100644
--- a/drivers/usb/gadget/fsl_udc_core.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -559,7 +559,7 @@ static int fsl_ep_enable(struct usb_ep *_ep,
559 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN)) 559 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
560 return -ESHUTDOWN; 560 return -ESHUTDOWN;
561 561
562 max = le16_to_cpu(desc->wMaxPacketSize); 562 max = usb_endpoint_maxp(desc);
563 563
564 /* Disable automatic zlp generation. Driver is responsible to indicate 564 /* Disable automatic zlp generation. Driver is responsible to indicate
565 * explicitly through req->req.zero. This is needed to enable multi-td 565 * explicitly through req->req.zero. This is needed to enable multi-td
@@ -1715,34 +1715,31 @@ static void dtd_complete_irq(struct fsl_udc *udc)
1715 } 1715 }
1716} 1716}
1717 1717
1718static inline enum usb_device_speed portscx_device_speed(u32 reg)
1719{
1720 switch (reg & PORTSCX_PORT_SPEED_MASK) {
1721 case PORTSCX_PORT_SPEED_HIGH:
1722 return USB_SPEED_HIGH;
1723 case PORTSCX_PORT_SPEED_FULL:
1724 return USB_SPEED_FULL;
1725 case PORTSCX_PORT_SPEED_LOW:
1726 return USB_SPEED_LOW;
1727 default:
1728 return USB_SPEED_UNKNOWN;
1729 }
1730}
1731
1718/* Process a port change interrupt */ 1732/* Process a port change interrupt */
1719static void port_change_irq(struct fsl_udc *udc) 1733static void port_change_irq(struct fsl_udc *udc)
1720{ 1734{
1721 u32 speed;
1722
1723 if (udc->bus_reset) 1735 if (udc->bus_reset)
1724 udc->bus_reset = 0; 1736 udc->bus_reset = 0;
1725 1737
1726 /* Bus resetting is finished */ 1738 /* Bus resetting is finished */
1727 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) { 1739 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
1728 /* Get the speed */ 1740 /* Get the speed */
1729 speed = (fsl_readl(&dr_regs->portsc1) 1741 udc->gadget.speed =
1730 & PORTSCX_PORT_SPEED_MASK); 1742 portscx_device_speed(fsl_readl(&dr_regs->portsc1));
1731 switch (speed) {
1732 case PORTSCX_PORT_SPEED_HIGH:
1733 udc->gadget.speed = USB_SPEED_HIGH;
1734 break;
1735 case PORTSCX_PORT_SPEED_FULL:
1736 udc->gadget.speed = USB_SPEED_FULL;
1737 break;
1738 case PORTSCX_PORT_SPEED_LOW:
1739 udc->gadget.speed = USB_SPEED_LOW;
1740 break;
1741 default:
1742 udc->gadget.speed = USB_SPEED_UNKNOWN;
1743 break;
1744 }
1745 }
1746 1743
1747 /* Update USB state */ 1744 /* Update USB state */
1748 if (!udc->resume_state) 1745 if (!udc->resume_state)
@@ -2167,20 +2164,8 @@ static int fsl_proc_read(char *page, char **start, off_t off, int count,
2167 default: 2164 default:
2168 s = "None"; break; 2165 s = "None"; break;
2169 } 2166 }
2170 s;} ), ( { 2167 s;} ),
2171 char *s; 2168 usb_speed_string(portscx_device_speed(tmp_reg)),
2172 switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
2173 case PORTSCX_PORT_SPEED_FULL:
2174 s = "Full Speed"; break;
2175 case PORTSCX_PORT_SPEED_LOW:
2176 s = "Low Speed"; break;
2177 case PORTSCX_PORT_SPEED_HIGH:
2178 s = "High Speed"; break;
2179 default:
2180 s = "Undefined"; break;
2181 }
2182 s;
2183 } ),
2184 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ? 2169 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
2185 "Normal PHY mode" : "Low power mode", 2170 "Normal PHY mode" : "Low power mode",
2186 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" : 2171 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
@@ -2495,8 +2480,7 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2495 2480
2496#ifndef CONFIG_ARCH_MXC 2481#ifndef CONFIG_ARCH_MXC
2497 if (pdata->have_sysif_regs) 2482 if (pdata->have_sysif_regs)
2498 usb_sys_regs = (struct usb_sys_interface *) 2483 usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
2499 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2500#endif 2484#endif
2501 2485
2502 /* Initialize USB clocks */ 2486 /* Initialize USB clocks */
diff --git a/drivers/usb/gadget/fusb300_udc.c b/drivers/usb/gadget/fusb300_udc.c
index 4ec888f90002..74da206c8406 100644
--- a/drivers/usb/gadget/fusb300_udc.c
+++ b/drivers/usb/gadget/fusb300_udc.c
@@ -8,21 +8,12 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22#include <linux/dma-mapping.h> 12#include <linux/dma-mapping.h>
23#include <linux/err.h> 13#include <linux/err.h>
24#include <linux/interrupt.h> 14#include <linux/interrupt.h>
25#include <linux/io.h> 15#include <linux/io.h>
16#include <linux/module.h>
26#include <linux/platform_device.h> 17#include <linux/platform_device.h>
27#include <linux/usb/ch9.h> 18#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h> 19#include <linux/usb/gadget.h>
@@ -220,7 +211,7 @@ static int config_ep(struct fusb300_ep *ep,
220 211
221 info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 212 info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
222 info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; 213 info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
223 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 214 info.maxpacket = usb_endpoint_maxp(desc);
224 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 215 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
225 216
226 if ((info.type == USB_ENDPOINT_XFER_INT) || 217 if ((info.type == USB_ENDPOINT_XFER_INT) ||
@@ -1479,7 +1470,7 @@ static int __init fusb300_probe(struct platform_device *pdev)
1479 fusb300->gadget.name = udc_name; 1470 fusb300->gadget.name = udc_name;
1480 fusb300->reg = reg; 1471 fusb300->reg = reg;
1481 1472
1482 ret = request_irq(ires->start, fusb300_irq, IRQF_DISABLED | IRQF_SHARED, 1473 ret = request_irq(ires->start, fusb300_irq, IRQF_SHARED,
1483 udc_name, fusb300); 1474 udc_name, fusb300);
1484 if (ret < 0) { 1475 if (ret < 0) {
1485 pr_err("request_irq error (%d)\n", ret); 1476 pr_err("request_irq error (%d)\n", ret);
@@ -1487,7 +1478,7 @@ static int __init fusb300_probe(struct platform_device *pdev)
1487 } 1478 }
1488 1479
1489 ret = request_irq(ires1->start, fusb300_irq, 1480 ret = request_irq(ires1->start, fusb300_irq,
1490 IRQF_DISABLED | IRQF_SHARED, udc_name, fusb300); 1481 IRQF_SHARED, udc_name, fusb300);
1491 if (ret < 0) { 1482 if (ret < 0) {
1492 pr_err("request_irq1 error (%d)\n", ret); 1483 pr_err("request_irq1 error (%d)\n", ret);
1493 goto clean_up; 1484 goto clean_up;
diff --git a/drivers/usb/gadget/fusb300_udc.h b/drivers/usb/gadget/fusb300_udc.h
index f51aa2ef1f90..92745bd03064 100644
--- a/drivers/usb/gadget/fusb300_udc.h
+++ b/drivers/usb/gadget/fusb300_udc.h
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22 12
23 13
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index 704c2800ac00..0519d77915ec 100644
--- a/drivers/usb/gadget/g_ffs.c
+++ b/drivers/usb/gadget/g_ffs.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22#define pr_fmt(fmt) "g_ffs: " fmt 13#define pr_fmt(fmt) "g_ffs: " fmt
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index f3a83cd0ef50..a8855d0b7f3b 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -31,6 +31,7 @@
31#define gadget_is_ci13xxx_msm(g) (!strcmp("ci13xxx_msm", (g)->name)) 31#define gadget_is_ci13xxx_msm(g) (!strcmp("ci13xxx_msm", (g)->name))
32#define gadget_is_ci13xxx_pci(g) (!strcmp("ci13xxx_pci", (g)->name)) 32#define gadget_is_ci13xxx_pci(g) (!strcmp("ci13xxx_pci", (g)->name))
33#define gadget_is_dummy(g) (!strcmp("dummy_udc", (g)->name)) 33#define gadget_is_dummy(g) (!strcmp("dummy_udc", (g)->name))
34#define gadget_is_dwc3(g) (!strcmp("dwc3-gadget", (g)->name))
34#define gadget_is_fsl_qe(g) (!strcmp("fsl_qe_udc", (g)->name)) 35#define gadget_is_fsl_qe(g) (!strcmp("fsl_qe_udc", (g)->name))
35#define gadget_is_fsl_usb2(g) (!strcmp("fsl-usb2-udc", (g)->name)) 36#define gadget_is_fsl_usb2(g) (!strcmp("fsl-usb2-udc", (g)->name))
36#define gadget_is_goku(g) (!strcmp("goku_udc", (g)->name)) 37#define gadget_is_goku(g) (!strcmp("goku_udc", (g)->name))
@@ -115,6 +116,8 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
115 return 0x30; 116 return 0x30;
116 else if (gadget_is_net2272(gadget)) 117 else if (gadget_is_net2272(gadget))
117 return 0x31; 118 return 0x31;
119 else if (gadget_is_dwc3(gadget))
120 return 0x32;
118 121
119 return -ENOENT; 122 return -ENOENT;
120} 123}
diff --git a/drivers/usb/gadget/gmidi.c b/drivers/usb/gadget/gmidi.c
index 8b9220e128a7..681bd038b1d8 100644
--- a/drivers/usb/gadget/gmidi.c
+++ b/drivers/usb/gadget/gmidi.c
@@ -23,6 +23,7 @@
23#include <linux/kernel.h> 23#include <linux/kernel.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/utsname.h> 25#include <linux/utsname.h>
26#include <linux/module.h>
26#include <linux/device.h> 27#include <linux/device.h>
27 28
28#include <sound/core.h> 29#include <sound/core.h>
@@ -36,134 +37,43 @@
36 37
37#include "gadget_chips.h" 38#include "gadget_chips.h"
38 39
39 40#include "composite.c"
40/*
41 * Kbuild is not very cooperative with respect to linking separately
42 * compiled library objects into one module. So for now we won't use
43 * separate compilation ... ensuring init/exit sections work to shrink
44 * the runtime footprint, and giving us at least some parts of what
45 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
46 */
47#include "usbstring.c" 41#include "usbstring.c"
48#include "config.c" 42#include "config.c"
49#include "epautoconf.c" 43#include "epautoconf.c"
44#include "f_midi.c"
50 45
51/*-------------------------------------------------------------------------*/ 46/*-------------------------------------------------------------------------*/
52 47
53
54MODULE_AUTHOR("Ben Williamson"); 48MODULE_AUTHOR("Ben Williamson");
55MODULE_LICENSE("GPL v2"); 49MODULE_LICENSE("GPL v2");
56 50
57#define DRIVER_VERSION "25 Jul 2006"
58
59static const char shortname[] = "g_midi"; 51static const char shortname[] = "g_midi";
60static const char longname[] = "MIDI Gadget"; 52static const char longname[] = "MIDI Gadget";
61 53
62static int index = SNDRV_DEFAULT_IDX1; 54static int index = SNDRV_DEFAULT_IDX1;
63static char *id = SNDRV_DEFAULT_STR1; 55module_param(index, int, S_IRUGO);
64
65module_param(index, int, 0444);
66MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter."); 56MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
67module_param(id, charp, 0444);
68MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
69
70/* Some systems will want different product identifiers published in the
71 * device descriptor, either numbers or strings or both. These string
72 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
73 */
74
75static ushort idVendor;
76module_param(idVendor, ushort, S_IRUGO);
77MODULE_PARM_DESC(idVendor, "USB Vendor ID");
78
79static ushort idProduct;
80module_param(idProduct, ushort, S_IRUGO);
81MODULE_PARM_DESC(idProduct, "USB Product ID");
82
83static ushort bcdDevice;
84module_param(bcdDevice, ushort, S_IRUGO);
85MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
86
87static char *iManufacturer;
88module_param(iManufacturer, charp, S_IRUGO);
89MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
90
91static char *iProduct;
92module_param(iProduct, charp, S_IRUGO);
93MODULE_PARM_DESC(iProduct, "USB Product string");
94
95static char *iSerialNumber;
96module_param(iSerialNumber, charp, S_IRUGO);
97MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
98
99/*
100 * this version autoconfigures as much as possible,
101 * which is reasonable for most "bulk-only" drivers.
102 */
103static const char *EP_IN_NAME;
104static const char *EP_OUT_NAME;
105 57
58static char *id = SNDRV_DEFAULT_STR1;
59module_param(id, charp, S_IRUGO);
60MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
106 61
107/* big enough to hold our biggest descriptor */ 62static unsigned int buflen = 256;
108#define USB_BUFSIZ 256
109
110
111/* This is a gadget, and the IN/OUT naming is from the host's perspective.
112 USB -> OUT endpoint -> rawmidi
113 USB <- IN endpoint <- rawmidi */
114struct gmidi_in_port {
115 struct gmidi_device* dev;
116 int active;
117 uint8_t cable; /* cable number << 4 */
118 uint8_t state;
119#define STATE_UNKNOWN 0
120#define STATE_1PARAM 1
121#define STATE_2PARAM_1 2
122#define STATE_2PARAM_2 3
123#define STATE_SYSEX_0 4
124#define STATE_SYSEX_1 5
125#define STATE_SYSEX_2 6
126 uint8_t data[2];
127};
128
129struct gmidi_device {
130 spinlock_t lock;
131 struct usb_gadget *gadget;
132 struct usb_request *req; /* for control responses */
133 u8 config;
134 struct usb_ep *in_ep, *out_ep;
135 struct snd_card *card;
136 struct snd_rawmidi *rmidi;
137 struct snd_rawmidi_substream *in_substream;
138 struct snd_rawmidi_substream *out_substream;
139
140 /* For the moment we only support one port in
141 each direction, but in_port is kept as a
142 separate struct so we can have more later. */
143 struct gmidi_in_port in_port;
144 unsigned long out_triggered;
145 struct tasklet_struct tasklet;
146};
147
148static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req);
149
150
151#define DBG(d, fmt, args...) \
152 dev_dbg(&(d)->gadget->dev , fmt , ## args)
153#define VDBG(d, fmt, args...) \
154 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
155#define ERROR(d, fmt, args...) \
156 dev_err(&(d)->gadget->dev , fmt , ## args)
157#define INFO(d, fmt, args...) \
158 dev_info(&(d)->gadget->dev , fmt , ## args)
159
160
161static unsigned buflen = 256;
162static unsigned qlen = 32;
163
164module_param(buflen, uint, S_IRUGO); 63module_param(buflen, uint, S_IRUGO);
64MODULE_PARM_DESC(buflen, "MIDI buffer length");
65
66static unsigned int qlen = 32;
165module_param(qlen, uint, S_IRUGO); 67module_param(qlen, uint, S_IRUGO);
68MODULE_PARM_DESC(qlen, "USB read request queue length");
166 69
70static unsigned int in_ports = 1;
71module_param(in_ports, uint, S_IRUGO);
72MODULE_PARM_DESC(in_ports, "Number of MIDI input ports");
73
74static unsigned int out_ports = 1;
75module_param(out_ports, uint, S_IRUGO);
76MODULE_PARM_DESC(out_ports, "Number of MIDI output ports");
167 77
168/* Thanks to Grey Innovation for donating this product ID. 78/* Thanks to Grey Innovation for donating this product ID.
169 * 79 *
@@ -173,1149 +83,124 @@ module_param(qlen, uint, S_IRUGO);
173#define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */ 83#define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
174#define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */ 84#define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
175 85
86/* string IDs are assigned dynamically */
176 87
177/* 88#define STRING_MANUFACTURER_IDX 0
178 * DESCRIPTORS ... most are static, but strings and (full) 89#define STRING_PRODUCT_IDX 1
179 * configuration descriptors are built on demand. 90#define STRING_DESCRIPTION_IDX 2
180 */
181
182#define STRING_MANUFACTURER 25
183#define STRING_PRODUCT 42
184#define STRING_SERIAL 101
185#define STRING_MIDI_GADGET 250
186 91
187/* We only have the one configuration, it's number 1. */
188#define GMIDI_CONFIG 1
189
190/* We have two interfaces- AudioControl and MIDIStreaming */
191#define GMIDI_AC_INTERFACE 0
192#define GMIDI_MS_INTERFACE 1
193#define GMIDI_NUM_INTERFACES 2
194
195DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
196DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
197DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
198
199/* B.1 Device Descriptor */
200static struct usb_device_descriptor device_desc = { 92static struct usb_device_descriptor device_desc = {
201 .bLength = USB_DT_DEVICE_SIZE, 93 .bLength = USB_DT_DEVICE_SIZE,
202 .bDescriptorType = USB_DT_DEVICE, 94 .bDescriptorType = USB_DT_DEVICE,
203 .bcdUSB = cpu_to_le16(0x0200), 95 .bcdUSB = __constant_cpu_to_le16(0x0200),
204 .bDeviceClass = USB_CLASS_PER_INTERFACE, 96 .bDeviceClass = USB_CLASS_PER_INTERFACE,
205 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM), 97 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
206 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM), 98 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
207 .iManufacturer = STRING_MANUFACTURER, 99 /* .iManufacturer = DYNAMIC */
208 .iProduct = STRING_PRODUCT, 100 /* .iProduct = DYNAMIC */
209 .bNumConfigurations = 1, 101 .bNumConfigurations = 1,
210}; 102};
211 103
212/* B.2 Configuration Descriptor */ 104static struct usb_string strings_dev[] = {
213static struct usb_config_descriptor config_desc = { 105 [STRING_MANUFACTURER_IDX].s = "Grey Innovation",
214 .bLength = USB_DT_CONFIG_SIZE, 106 [STRING_PRODUCT_IDX].s = "MIDI Gadget",
215 .bDescriptorType = USB_DT_CONFIG, 107 [STRING_DESCRIPTION_IDX].s = "MIDI",
216 /* compute wTotalLength on the fly */ 108 { } /* end of list */
217 .bNumInterfaces = GMIDI_NUM_INTERFACES,
218 .bConfigurationValue = GMIDI_CONFIG,
219 .iConfiguration = STRING_MIDI_GADGET,
220 /*
221 * FIXME: When embedding this driver in a device,
222 * these need to be set to reflect the actual
223 * power properties of the device. Is it selfpowered?
224 */
225 .bmAttributes = USB_CONFIG_ATT_ONE,
226 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
227};
228
229/* B.3.1 Standard AC Interface Descriptor */
230static const struct usb_interface_descriptor ac_interface_desc = {
231 .bLength = USB_DT_INTERFACE_SIZE,
232 .bDescriptorType = USB_DT_INTERFACE,
233 .bInterfaceNumber = GMIDI_AC_INTERFACE,
234 .bNumEndpoints = 0,
235 .bInterfaceClass = USB_CLASS_AUDIO,
236 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
237 .iInterface = STRING_MIDI_GADGET,
238};
239
240/* B.3.2 Class-Specific AC Interface Descriptor */
241static const struct uac1_ac_header_descriptor_1 ac_header_desc = {
242 .bLength = UAC_DT_AC_HEADER_SIZE(1),
243 .bDescriptorType = USB_DT_CS_INTERFACE,
244 .bDescriptorSubtype = USB_MS_HEADER,
245 .bcdADC = cpu_to_le16(0x0100),
246 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
247 .bInCollection = 1,
248 .baInterfaceNr = {
249 [0] = GMIDI_MS_INTERFACE,
250 }
251};
252
253/* B.4.1 Standard MS Interface Descriptor */
254static const struct usb_interface_descriptor ms_interface_desc = {
255 .bLength = USB_DT_INTERFACE_SIZE,
256 .bDescriptorType = USB_DT_INTERFACE,
257 .bInterfaceNumber = GMIDI_MS_INTERFACE,
258 .bNumEndpoints = 2,
259 .bInterfaceClass = USB_CLASS_AUDIO,
260 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
261 .iInterface = STRING_MIDI_GADGET,
262}; 109};
263 110
264/* B.4.2 Class-Specific MS Interface Descriptor */ 111static struct usb_gadget_strings stringtab_dev = {
265static const struct usb_ms_header_descriptor ms_header_desc = {
266 .bLength = USB_DT_MS_HEADER_SIZE,
267 .bDescriptorType = USB_DT_CS_INTERFACE,
268 .bDescriptorSubtype = USB_MS_HEADER,
269 .bcdMSC = cpu_to_le16(0x0100),
270 .wTotalLength = cpu_to_le16(USB_DT_MS_HEADER_SIZE
271 + 2*USB_DT_MIDI_IN_SIZE
272 + 2*USB_DT_MIDI_OUT_SIZE(1)),
273};
274
275#define JACK_IN_EMB 1
276#define JACK_IN_EXT 2
277#define JACK_OUT_EMB 3
278#define JACK_OUT_EXT 4
279
280/* B.4.3 MIDI IN Jack Descriptors */
281static const struct usb_midi_in_jack_descriptor jack_in_emb_desc = {
282 .bLength = USB_DT_MIDI_IN_SIZE,
283 .bDescriptorType = USB_DT_CS_INTERFACE,
284 .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
285 .bJackType = USB_MS_EMBEDDED,
286 .bJackID = JACK_IN_EMB,
287};
288
289static const struct usb_midi_in_jack_descriptor jack_in_ext_desc = {
290 .bLength = USB_DT_MIDI_IN_SIZE,
291 .bDescriptorType = USB_DT_CS_INTERFACE,
292 .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
293 .bJackType = USB_MS_EXTERNAL,
294 .bJackID = JACK_IN_EXT,
295};
296
297/* B.4.4 MIDI OUT Jack Descriptors */
298static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc = {
299 .bLength = USB_DT_MIDI_OUT_SIZE(1),
300 .bDescriptorType = USB_DT_CS_INTERFACE,
301 .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK,
302 .bJackType = USB_MS_EMBEDDED,
303 .bJackID = JACK_OUT_EMB,
304 .bNrInputPins = 1,
305 .pins = {
306 [0] = {
307 .baSourceID = JACK_IN_EXT,
308 .baSourcePin = 1,
309 }
310 }
311};
312
313static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc = {
314 .bLength = USB_DT_MIDI_OUT_SIZE(1),
315 .bDescriptorType = USB_DT_CS_INTERFACE,
316 .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK,
317 .bJackType = USB_MS_EXTERNAL,
318 .bJackID = JACK_OUT_EXT,
319 .bNrInputPins = 1,
320 .pins = {
321 [0] = {
322 .baSourceID = JACK_IN_EMB,
323 .baSourcePin = 1,
324 }
325 }
326};
327
328/* B.5.1 Standard Bulk OUT Endpoint Descriptor */
329static struct usb_endpoint_descriptor bulk_out_desc = {
330 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
331 .bDescriptorType = USB_DT_ENDPOINT,
332 .bEndpointAddress = USB_DIR_OUT,
333 .bmAttributes = USB_ENDPOINT_XFER_BULK,
334};
335
336/* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
337static const struct usb_ms_endpoint_descriptor_1 ms_out_desc = {
338 .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
339 .bDescriptorType = USB_DT_CS_ENDPOINT,
340 .bDescriptorSubtype = USB_MS_GENERAL,
341 .bNumEmbMIDIJack = 1,
342 .baAssocJackID = {
343 [0] = JACK_IN_EMB,
344 }
345};
346
347/* B.6.1 Standard Bulk IN Endpoint Descriptor */
348static struct usb_endpoint_descriptor bulk_in_desc = {
349 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
350 .bDescriptorType = USB_DT_ENDPOINT,
351 .bEndpointAddress = USB_DIR_IN,
352 .bmAttributes = USB_ENDPOINT_XFER_BULK,
353};
354
355/* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
356static const struct usb_ms_endpoint_descriptor_1 ms_in_desc = {
357 .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
358 .bDescriptorType = USB_DT_CS_ENDPOINT,
359 .bDescriptorSubtype = USB_MS_GENERAL,
360 .bNumEmbMIDIJack = 1,
361 .baAssocJackID = {
362 [0] = JACK_OUT_EMB,
363 }
364};
365
366static const struct usb_descriptor_header *gmidi_function [] = {
367 (struct usb_descriptor_header *)&ac_interface_desc,
368 (struct usb_descriptor_header *)&ac_header_desc,
369 (struct usb_descriptor_header *)&ms_interface_desc,
370
371 (struct usb_descriptor_header *)&ms_header_desc,
372 (struct usb_descriptor_header *)&jack_in_emb_desc,
373 (struct usb_descriptor_header *)&jack_in_ext_desc,
374 (struct usb_descriptor_header *)&jack_out_emb_desc,
375 (struct usb_descriptor_header *)&jack_out_ext_desc,
376 /* If you add more jacks, update ms_header_desc.wTotalLength */
377
378 (struct usb_descriptor_header *)&bulk_out_desc,
379 (struct usb_descriptor_header *)&ms_out_desc,
380 (struct usb_descriptor_header *)&bulk_in_desc,
381 (struct usb_descriptor_header *)&ms_in_desc,
382 NULL,
383};
384
385static char manufacturer[50];
386static char product_desc[40] = "MIDI Gadget";
387static char serial_number[20];
388
389/* static strings, in UTF-8 */
390static struct usb_string strings [] = {
391 { STRING_MANUFACTURER, manufacturer, },
392 { STRING_PRODUCT, product_desc, },
393 { STRING_SERIAL, serial_number, },
394 { STRING_MIDI_GADGET, longname, },
395 { } /* end of list */
396};
397
398static struct usb_gadget_strings stringtab = {
399 .language = 0x0409, /* en-us */ 112 .language = 0x0409, /* en-us */
400 .strings = strings, 113 .strings = strings_dev,
401}; 114};
402 115
403static int config_buf(struct usb_gadget *gadget, 116static struct usb_gadget_strings *dev_strings[] = {
404 u8 *buf, u8 type, unsigned index) 117 &stringtab_dev,
405{ 118 NULL,
406 int len;
407
408 /* only one configuration */
409 if (index != 0) {
410 return -EINVAL;
411 }
412 len = usb_gadget_config_buf(&config_desc,
413 buf, USB_BUFSIZ, gmidi_function);
414 if (len < 0) {
415 return len;
416 }
417 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
418 return len;
419}
420
421static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
422{
423 struct usb_request *req;
424
425 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
426 if (req) {
427 req->length = length;
428 req->buf = kmalloc(length, GFP_ATOMIC);
429 if (!req->buf) {
430 usb_ep_free_request(ep, req);
431 req = NULL;
432 }
433 }
434 return req;
435}
436
437static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
438{
439 kfree(req->buf);
440 usb_ep_free_request(ep, req);
441}
442
443static const uint8_t gmidi_cin_length[] = {
444 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
445}; 119};
446 120
447/* 121static int __exit midi_unbind(struct usb_composite_dev *dev)
448 * Receives a chunk of MIDI data.
449 */
450static void gmidi_read_data(struct usb_ep *ep, int cable,
451 uint8_t *data, int length)
452{
453 struct gmidi_device *dev = ep->driver_data;
454 /* cable is ignored, because for now we only have one. */
455
456 if (!dev->out_substream) {
457 /* Nobody is listening - throw it on the floor. */
458 return;
459 }
460 if (!test_bit(dev->out_substream->number, &dev->out_triggered)) {
461 return;
462 }
463 snd_rawmidi_receive(dev->out_substream, data, length);
464}
465
466static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
467{
468 unsigned i;
469 u8 *buf = req->buf;
470
471 for (i = 0; i + 3 < req->actual; i += 4) {
472 if (buf[i] != 0) {
473 int cable = buf[i] >> 4;
474 int length = gmidi_cin_length[buf[i] & 0x0f];
475 gmidi_read_data(ep, cable, &buf[i + 1], length);
476 }
477 }
478}
479
480static void gmidi_complete(struct usb_ep *ep, struct usb_request *req)
481{
482 struct gmidi_device *dev = ep->driver_data;
483 int status = req->status;
484
485 switch (status) {
486 case 0: /* normal completion */
487 if (ep == dev->out_ep) {
488 /* we received stuff.
489 req is queued again, below */
490 gmidi_handle_out_data(ep, req);
491 } else if (ep == dev->in_ep) {
492 /* our transmit completed.
493 see if there's more to go.
494 gmidi_transmit eats req, don't queue it again. */
495 gmidi_transmit(dev, req);
496 return;
497 }
498 break;
499
500 /* this endpoint is normally active while we're configured */
501 case -ECONNABORTED: /* hardware forced ep reset */
502 case -ECONNRESET: /* request dequeued */
503 case -ESHUTDOWN: /* disconnect from host */
504 VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
505 req->actual, req->length);
506 if (ep == dev->out_ep) {
507 gmidi_handle_out_data(ep, req);
508 }
509 free_ep_req(ep, req);
510 return;
511
512 case -EOVERFLOW: /* buffer overrun on read means that
513 * we didn't provide a big enough
514 * buffer.
515 */
516 default:
517 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
518 status, req->actual, req->length);
519 break;
520 case -EREMOTEIO: /* short read */
521 break;
522 }
523
524 status = usb_ep_queue(ep, req, GFP_ATOMIC);
525 if (status) {
526 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
527 ep->name, req->length, status);
528 usb_ep_set_halt(ep);
529 /* FIXME recover later ... somehow */
530 }
531}
532
533static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags)
534{
535 int err = 0;
536 struct usb_request *req;
537 struct usb_ep *ep;
538 unsigned i;
539
540 dev->in_ep->desc = &bulk_in_desc;
541 err = usb_ep_enable(dev->in_ep);
542 if (err) {
543 ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
544 goto fail;
545 }
546 dev->in_ep->driver_data = dev;
547
548 dev->out_ep->desc = &bulk_out_desc;
549 err = usb_ep_enable(dev->out_ep);
550 if (err) {
551 ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
552 goto fail;
553 }
554 dev->out_ep->driver_data = dev;
555
556 /* allocate a bunch of read buffers and queue them all at once. */
557 ep = dev->out_ep;
558 for (i = 0; i < qlen && err == 0; i++) {
559 req = alloc_ep_req(ep, buflen);
560 if (req) {
561 req->complete = gmidi_complete;
562 err = usb_ep_queue(ep, req, GFP_ATOMIC);
563 if (err) {
564 DBG(dev, "%s queue req: %d\n", ep->name, err);
565 }
566 } else {
567 err = -ENOMEM;
568 }
569 }
570fail:
571 /* caller is responsible for cleanup on error */
572 return err;
573}
574
575
576static void gmidi_reset_config(struct gmidi_device *dev)
577{
578 if (dev->config == 0) {
579 return;
580 }
581
582 DBG(dev, "reset config\n");
583
584 /* just disable endpoints, forcing completion of pending i/o.
585 * all our completion handlers free their requests in this case.
586 */
587 usb_ep_disable(dev->in_ep);
588 usb_ep_disable(dev->out_ep);
589 dev->config = 0;
590}
591
592/* change our operational config. this code must agree with the code
593 * that returns config descriptors, and altsetting code.
594 *
595 * it's also responsible for power management interactions. some
596 * configurations might not work with our current power sources.
597 *
598 * note that some device controller hardware will constrain what this
599 * code can do, perhaps by disallowing more than one configuration or
600 * by limiting configuration choices (like the pxa2xx).
601 */
602static int
603gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
604{
605 int result = 0;
606 struct usb_gadget *gadget = dev->gadget;
607
608#if 0
609 /* FIXME */
610 /* Hacking this bit out fixes a bug where on receipt of two
611 USB_REQ_SET_CONFIGURATION messages, we end up with no
612 buffered OUT requests waiting for data. This is clearly
613 hiding a bug elsewhere, because if the config didn't
614 change then we really shouldn't do anything. */
615 /* Having said that, when we do "change" from config 1
616 to config 1, we at least gmidi_reset_config() which
617 clears out any requests on endpoints, so it's not like
618 we leak or anything. */
619 if (number == dev->config) {
620 return 0;
621 }
622#endif
623
624 gmidi_reset_config(dev);
625
626 switch (number) {
627 case GMIDI_CONFIG:
628 result = set_gmidi_config(dev, gfp_flags);
629 break;
630 default:
631 result = -EINVAL;
632 /* FALL THROUGH */
633 case 0:
634 return result;
635 }
636
637 if (!result && (!dev->in_ep || !dev->out_ep)) {
638 result = -ENODEV;
639 }
640 if (result) {
641 gmidi_reset_config(dev);
642 } else {
643 char *speed;
644
645 switch (gadget->speed) {
646 case USB_SPEED_LOW: speed = "low"; break;
647 case USB_SPEED_FULL: speed = "full"; break;
648 case USB_SPEED_HIGH: speed = "high"; break;
649 default: speed = "?"; break;
650 }
651
652 dev->config = number;
653 INFO(dev, "%s speed\n", speed);
654 }
655 return result;
656}
657
658
659static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req)
660{
661 if (req->status || req->actual != req->length) {
662 DBG((struct gmidi_device *) ep->driver_data,
663 "setup complete --> %d, %d/%d\n",
664 req->status, req->actual, req->length);
665 }
666}
667
668/*
669 * The setup() callback implements all the ep0 functionality that's
670 * not handled lower down, in hardware or the hardware driver (like
671 * device and endpoint feature flags, and their status). It's all
672 * housekeeping for the gadget function we're implementing. Most of
673 * the work is in config-specific setup.
674 */
675static int gmidi_setup(struct usb_gadget *gadget,
676 const struct usb_ctrlrequest *ctrl)
677{
678 struct gmidi_device *dev = get_gadget_data(gadget);
679 struct usb_request *req = dev->req;
680 int value = -EOPNOTSUPP;
681 u16 w_index = le16_to_cpu(ctrl->wIndex);
682 u16 w_value = le16_to_cpu(ctrl->wValue);
683 u16 w_length = le16_to_cpu(ctrl->wLength);
684
685 /* usually this stores reply data in the pre-allocated ep0 buffer,
686 * but config change events will reconfigure hardware.
687 */
688 req->zero = 0;
689 switch (ctrl->bRequest) {
690
691 case USB_REQ_GET_DESCRIPTOR:
692 if (ctrl->bRequestType != USB_DIR_IN) {
693 goto unknown;
694 }
695 switch (w_value >> 8) {
696
697 case USB_DT_DEVICE:
698 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
699 value = min(w_length, (u16) sizeof(device_desc));
700 memcpy(req->buf, &device_desc, value);
701 break;
702 case USB_DT_CONFIG:
703 value = config_buf(gadget, req->buf,
704 w_value >> 8,
705 w_value & 0xff);
706 if (value >= 0) {
707 value = min(w_length, (u16)value);
708 }
709 break;
710
711 case USB_DT_STRING:
712 /* wIndex == language code.
713 * this driver only handles one language, you can
714 * add string tables for other languages, using
715 * any UTF-8 characters
716 */
717 value = usb_gadget_get_string(&stringtab,
718 w_value & 0xff, req->buf);
719 if (value >= 0) {
720 value = min(w_length, (u16)value);
721 }
722 break;
723 }
724 break;
725
726 /* currently two configs, two speeds */
727 case USB_REQ_SET_CONFIGURATION:
728 if (ctrl->bRequestType != 0) {
729 goto unknown;
730 }
731 if (gadget->a_hnp_support) {
732 DBG(dev, "HNP available\n");
733 } else if (gadget->a_alt_hnp_support) {
734 DBG(dev, "HNP needs a different root port\n");
735 } else {
736 VDBG(dev, "HNP inactive\n");
737 }
738 spin_lock(&dev->lock);
739 value = gmidi_set_config(dev, w_value, GFP_ATOMIC);
740 spin_unlock(&dev->lock);
741 break;
742 case USB_REQ_GET_CONFIGURATION:
743 if (ctrl->bRequestType != USB_DIR_IN) {
744 goto unknown;
745 }
746 *(u8 *)req->buf = dev->config;
747 value = min(w_length, (u16)1);
748 break;
749
750 /* until we add altsetting support, or other interfaces,
751 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
752 * and already killed pending endpoint I/O.
753 */
754 case USB_REQ_SET_INTERFACE:
755 if (ctrl->bRequestType != USB_RECIP_INTERFACE) {
756 goto unknown;
757 }
758 spin_lock(&dev->lock);
759 if (dev->config && w_index < GMIDI_NUM_INTERFACES
760 && w_value == 0)
761 {
762 u8 config = dev->config;
763
764 /* resets interface configuration, forgets about
765 * previous transaction state (queued bufs, etc)
766 * and re-inits endpoint state (toggle etc)
767 * no response queued, just zero status == success.
768 * if we had more than one interface we couldn't
769 * use this "reset the config" shortcut.
770 */
771 gmidi_reset_config(dev);
772 gmidi_set_config(dev, config, GFP_ATOMIC);
773 value = 0;
774 }
775 spin_unlock(&dev->lock);
776 break;
777 case USB_REQ_GET_INTERFACE:
778 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) {
779 goto unknown;
780 }
781 if (!dev->config) {
782 break;
783 }
784 if (w_index >= GMIDI_NUM_INTERFACES) {
785 value = -EDOM;
786 break;
787 }
788 *(u8 *)req->buf = 0;
789 value = min(w_length, (u16)1);
790 break;
791
792 default:
793unknown:
794 VDBG(dev, "unknown control req%02x.%02x v%04x i%04x l%d\n",
795 ctrl->bRequestType, ctrl->bRequest,
796 w_value, w_index, w_length);
797 }
798
799 /* respond with data transfer before status phase? */
800 if (value >= 0) {
801 req->length = value;
802 req->zero = value < w_length;
803 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
804 if (value < 0) {
805 DBG(dev, "ep_queue --> %d\n", value);
806 req->status = 0;
807 gmidi_setup_complete(gadget->ep0, req);
808 }
809 }
810
811 /* device either stalls (value < 0) or reports success */
812 return value;
813}
814
815static void gmidi_disconnect(struct usb_gadget *gadget)
816{
817 struct gmidi_device *dev = get_gadget_data(gadget);
818 unsigned long flags;
819
820 spin_lock_irqsave(&dev->lock, flags);
821 gmidi_reset_config(dev);
822
823 /* a more significant application might have some non-usb
824 * activities to quiesce here, saving resources like power
825 * or pushing the notification up a network stack.
826 */
827 spin_unlock_irqrestore(&dev->lock, flags);
828
829 /* next we may get setup() calls to enumerate new connections;
830 * or an unbind() during shutdown (including removing module).
831 */
832}
833
834static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget)
835{
836 struct gmidi_device *dev = get_gadget_data(gadget);
837 struct snd_card *card;
838
839 DBG(dev, "unbind\n");
840
841 card = dev->card;
842 dev->card = NULL;
843 if (card) {
844 snd_card_free(card);
845 }
846
847 /* we've already been disconnected ... no i/o is active */
848 if (dev->req) {
849 dev->req->length = USB_BUFSIZ;
850 free_ep_req(gadget->ep0, dev->req);
851 }
852 kfree(dev);
853 set_gadget_data(gadget, NULL);
854}
855
856static int gmidi_snd_free(struct snd_device *device)
857{ 122{
858 return 0; 123 return 0;
859} 124}
860 125
861static void gmidi_transmit_packet(struct usb_request *req, uint8_t p0, 126static struct usb_configuration midi_config = {
862 uint8_t p1, uint8_t p2, uint8_t p3) 127 .label = "MIDI Gadget",
863{ 128 .bConfigurationValue = 1,
864 unsigned length = req->length; 129 /* .iConfiguration = DYNAMIC */
865 u8 *buf = (u8 *)req->buf + length; 130 .bmAttributes = USB_CONFIG_ATT_ONE,
866 131 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
867 buf[0] = p0;
868 buf[1] = p1;
869 buf[2] = p2;
870 buf[3] = p3;
871 req->length = length + 4;
872}
873
874/*
875 * Converts MIDI commands to USB MIDI packets.
876 */
877static void gmidi_transmit_byte(struct usb_request *req,
878 struct gmidi_in_port *port, uint8_t b)
879{
880 uint8_t p0 = port->cable;
881
882 if (b >= 0xf8) {
883 gmidi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
884 } else if (b >= 0xf0) {
885 switch (b) {
886 case 0xf0:
887 port->data[0] = b;
888 port->state = STATE_SYSEX_1;
889 break;
890 case 0xf1:
891 case 0xf3:
892 port->data[0] = b;
893 port->state = STATE_1PARAM;
894 break;
895 case 0xf2:
896 port->data[0] = b;
897 port->state = STATE_2PARAM_1;
898 break;
899 case 0xf4:
900 case 0xf5:
901 port->state = STATE_UNKNOWN;
902 break;
903 case 0xf6:
904 gmidi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
905 port->state = STATE_UNKNOWN;
906 break;
907 case 0xf7:
908 switch (port->state) {
909 case STATE_SYSEX_0:
910 gmidi_transmit_packet(req,
911 p0 | 0x05, 0xf7, 0, 0);
912 break;
913 case STATE_SYSEX_1:
914 gmidi_transmit_packet(req,
915 p0 | 0x06, port->data[0], 0xf7, 0);
916 break;
917 case STATE_SYSEX_2:
918 gmidi_transmit_packet(req,
919 p0 | 0x07, port->data[0],
920 port->data[1], 0xf7);
921 break;
922 }
923 port->state = STATE_UNKNOWN;
924 break;
925 }
926 } else if (b >= 0x80) {
927 port->data[0] = b;
928 if (b >= 0xc0 && b <= 0xdf)
929 port->state = STATE_1PARAM;
930 else
931 port->state = STATE_2PARAM_1;
932 } else { /* b < 0x80 */
933 switch (port->state) {
934 case STATE_1PARAM:
935 if (port->data[0] < 0xf0) {
936 p0 |= port->data[0] >> 4;
937 } else {
938 p0 |= 0x02;
939 port->state = STATE_UNKNOWN;
940 }
941 gmidi_transmit_packet(req, p0, port->data[0], b, 0);
942 break;
943 case STATE_2PARAM_1:
944 port->data[1] = b;
945 port->state = STATE_2PARAM_2;
946 break;
947 case STATE_2PARAM_2:
948 if (port->data[0] < 0xf0) {
949 p0 |= port->data[0] >> 4;
950 port->state = STATE_2PARAM_1;
951 } else {
952 p0 |= 0x03;
953 port->state = STATE_UNKNOWN;
954 }
955 gmidi_transmit_packet(req,
956 p0, port->data[0], port->data[1], b);
957 break;
958 case STATE_SYSEX_0:
959 port->data[0] = b;
960 port->state = STATE_SYSEX_1;
961 break;
962 case STATE_SYSEX_1:
963 port->data[1] = b;
964 port->state = STATE_SYSEX_2;
965 break;
966 case STATE_SYSEX_2:
967 gmidi_transmit_packet(req,
968 p0 | 0x04, port->data[0], port->data[1], b);
969 port->state = STATE_SYSEX_0;
970 break;
971 }
972 }
973}
974
975static void gmidi_transmit(struct gmidi_device *dev, struct usb_request *req)
976{
977 struct usb_ep *ep = dev->in_ep;
978 struct gmidi_in_port *port = &dev->in_port;
979
980 if (!ep) {
981 return;
982 }
983 if (!req) {
984 req = alloc_ep_req(ep, buflen);
985 }
986 if (!req) {
987 ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n");
988 return;
989 }
990 req->length = 0;
991 req->complete = gmidi_complete;
992
993 if (port->active) {
994 while (req->length + 3 < buflen) {
995 uint8_t b;
996 if (snd_rawmidi_transmit(dev->in_substream, &b, 1)
997 != 1)
998 {
999 port->active = 0;
1000 break;
1001 }
1002 gmidi_transmit_byte(req, port, b);
1003 }
1004 }
1005 if (req->length > 0) {
1006 usb_ep_queue(ep, req, GFP_ATOMIC);
1007 } else {
1008 free_ep_req(ep, req);
1009 }
1010}
1011
1012static void gmidi_in_tasklet(unsigned long data)
1013{
1014 struct gmidi_device *dev = (struct gmidi_device *)data;
1015
1016 gmidi_transmit(dev, NULL);
1017}
1018
1019static int gmidi_in_open(struct snd_rawmidi_substream *substream)
1020{
1021 struct gmidi_device *dev = substream->rmidi->private_data;
1022
1023 VDBG(dev, "gmidi_in_open\n");
1024 dev->in_substream = substream;
1025 dev->in_port.state = STATE_UNKNOWN;
1026 return 0;
1027}
1028
1029static int gmidi_in_close(struct snd_rawmidi_substream *substream)
1030{
1031 struct gmidi_device *dev = substream->rmidi->private_data;
1032
1033 VDBG(dev, "gmidi_in_close\n");
1034 return 0;
1035}
1036
1037static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
1038{
1039 struct gmidi_device *dev = substream->rmidi->private_data;
1040
1041 VDBG(dev, "gmidi_in_trigger %d\n", up);
1042 dev->in_port.active = up;
1043 if (up) {
1044 tasklet_hi_schedule(&dev->tasklet);
1045 }
1046}
1047
1048static int gmidi_out_open(struct snd_rawmidi_substream *substream)
1049{
1050 struct gmidi_device *dev = substream->rmidi->private_data;
1051
1052 VDBG(dev, "gmidi_out_open\n");
1053 dev->out_substream = substream;
1054 return 0;
1055}
1056
1057static int gmidi_out_close(struct snd_rawmidi_substream *substream)
1058{
1059 struct gmidi_device *dev = substream->rmidi->private_data;
1060
1061 VDBG(dev, "gmidi_out_close\n");
1062 return 0;
1063}
1064
1065static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up)
1066{
1067 struct gmidi_device *dev = substream->rmidi->private_data;
1068
1069 VDBG(dev, "gmidi_out_trigger %d\n", up);
1070 if (up) {
1071 set_bit(substream->number, &dev->out_triggered);
1072 } else {
1073 clear_bit(substream->number, &dev->out_triggered);
1074 }
1075}
1076
1077static struct snd_rawmidi_ops gmidi_in_ops = {
1078 .open = gmidi_in_open,
1079 .close = gmidi_in_close,
1080 .trigger = gmidi_in_trigger,
1081}; 132};
1082 133
1083static struct snd_rawmidi_ops gmidi_out_ops = { 134static int __init midi_bind_config(struct usb_configuration *c)
1084 .open = gmidi_out_open,
1085 .close = gmidi_out_close,
1086 .trigger = gmidi_out_trigger
1087};
1088
1089/* register as a sound "card" */
1090static int gmidi_register_card(struct gmidi_device *dev)
1091{ 135{
1092 struct snd_card *card; 136 return f_midi_bind_config(c, index, id,
1093 struct snd_rawmidi *rmidi; 137 in_ports, out_ports,
1094 int err; 138 buflen, qlen);
1095 int out_ports = 1;
1096 int in_ports = 1;
1097 static struct snd_device_ops ops = {
1098 .dev_free = gmidi_snd_free,
1099 };
1100
1101 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
1102 if (err < 0) {
1103 ERROR(dev, "snd_card_create failed\n");
1104 goto fail;
1105 }
1106 dev->card = card;
1107
1108 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops);
1109 if (err < 0) {
1110 ERROR(dev, "snd_device_new failed: error %d\n", err);
1111 goto fail;
1112 }
1113
1114 strcpy(card->driver, longname);
1115 strcpy(card->longname, longname);
1116 strcpy(card->shortname, shortname);
1117
1118 /* Set up rawmidi */
1119 dev->in_port.dev = dev;
1120 dev->in_port.active = 0;
1121 snd_component_add(card, "MIDI");
1122 err = snd_rawmidi_new(card, "USB MIDI Gadget", 0,
1123 out_ports, in_ports, &rmidi);
1124 if (err < 0) {
1125 ERROR(dev, "snd_rawmidi_new failed: error %d\n", err);
1126 goto fail;
1127 }
1128 dev->rmidi = rmidi;
1129 strcpy(rmidi->name, card->shortname);
1130 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1131 SNDRV_RAWMIDI_INFO_INPUT |
1132 SNDRV_RAWMIDI_INFO_DUPLEX;
1133 rmidi->private_data = dev;
1134
1135 /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
1136 It's an upside-down world being a gadget. */
1137 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
1138 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
1139
1140 snd_card_set_dev(card, &dev->gadget->dev);
1141
1142 /* register it - we're ready to go */
1143 err = snd_card_register(card);
1144 if (err < 0) {
1145 ERROR(dev, "snd_card_register failed\n");
1146 goto fail;
1147 }
1148
1149 VDBG(dev, "gmidi_register_card finished ok\n");
1150 return 0;
1151
1152fail:
1153 if (dev->card) {
1154 snd_card_free(dev->card);
1155 dev->card = NULL;
1156 }
1157 return err;
1158} 139}
1159 140
1160/* 141static int __init midi_bind(struct usb_composite_dev *cdev)
1161 * Creates an output endpoint, and initializes output ports.
1162 */
1163static int __init gmidi_bind(struct usb_gadget *gadget)
1164{ 142{
1165 struct gmidi_device *dev; 143 struct usb_gadget *gadget = cdev->gadget;
1166 struct usb_ep *in_ep, *out_ep; 144 int gcnum, status;
1167 int gcnum, err = 0;
1168 145
1169 /* support optional vendor/distro customization */ 146 status = usb_string_id(cdev);
1170 if (idVendor) { 147 if (status < 0)
1171 if (!idProduct) { 148 return status;
1172 pr_err("idVendor needs idProduct!\n"); 149 strings_dev[STRING_MANUFACTURER_IDX].id = status;
1173 return -ENODEV; 150 device_desc.iManufacturer = status;
1174 }
1175 device_desc.idVendor = cpu_to_le16(idVendor);
1176 device_desc.idProduct = cpu_to_le16(idProduct);
1177 if (bcdDevice) {
1178 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1179 }
1180 }
1181 if (iManufacturer) {
1182 strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
1183 } else {
1184 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1185 init_utsname()->sysname, init_utsname()->release,
1186 gadget->name);
1187 }
1188 if (iProduct) {
1189 strlcpy(product_desc, iProduct, sizeof(product_desc));
1190 }
1191 if (iSerialNumber) {
1192 device_desc.iSerialNumber = STRING_SERIAL,
1193 strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
1194 }
1195 151
1196 /* Bulk-only drivers like this one SHOULD be able to 152 status = usb_string_id(cdev);
1197 * autoconfigure on any sane usb controller driver, 153 if (status < 0)
1198 * but there may also be important quirks to address. 154 return status;
1199 */ 155 strings_dev[STRING_PRODUCT_IDX].id = status;
1200 usb_ep_autoconfig_reset(gadget); 156 device_desc.iProduct = status;
1201 in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
1202 if (!in_ep) {
1203autoconf_fail:
1204 pr_err("%s: can't autoconfigure on %s\n",
1205 shortname, gadget->name);
1206 return -ENODEV;
1207 }
1208 EP_IN_NAME = in_ep->name;
1209 in_ep->driver_data = in_ep; /* claim */
1210 157
1211 out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc); 158 /* config description */
1212 if (!out_ep) { 159 status = usb_string_id(cdev);
1213 goto autoconf_fail; 160 if (status < 0)
1214 } 161 return status;
1215 EP_OUT_NAME = out_ep->name; 162 strings_dev[STRING_DESCRIPTION_IDX].id = status;
1216 out_ep->driver_data = out_ep; /* claim */ 163
164 midi_config.iConfiguration = status;
1217 165
1218 gcnum = usb_gadget_controller_number(gadget); 166 gcnum = usb_gadget_controller_number(gadget);
1219 if (gcnum >= 0) { 167 if (gcnum < 0) {
1220 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1221 } else {
1222 /* gmidi is so simple (no altsettings) that 168 /* gmidi is so simple (no altsettings) that
1223 * it SHOULD NOT have problems with bulk-capable hardware. 169 * it SHOULD NOT have problems with bulk-capable hardware.
1224 * so warn about unrecognized controllers, don't panic. 170 * so warn about unrecognized controllers, don't panic.
1225 */ 171 */
1226 pr_warning("%s: controller '%s' not recognized\n", 172 pr_warning("%s: controller '%s' not recognized\n",
1227 shortname, gadget->name); 173 __func__, gadget->name);
1228 device_desc.bcdDevice = cpu_to_le16(0x9999); 174 device_desc.bcdDevice = cpu_to_le16(0x9999);
175 } else {
176 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1229 } 177 }
1230 178
179 status = usb_add_config(cdev, &midi_config, midi_bind_config);
180 if (status < 0)
181 return status;
1231 182
1232 /* ok, we made sense of the hardware ... */ 183 pr_info("%s\n", longname);
1233 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1234 if (!dev) {
1235 return -ENOMEM;
1236 }
1237 spin_lock_init(&dev->lock);
1238 dev->gadget = gadget;
1239 dev->in_ep = in_ep;
1240 dev->out_ep = out_ep;
1241 set_gadget_data(gadget, dev);
1242 tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev);
1243
1244 /* preallocate control response and buffer */
1245 dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
1246 if (!dev->req) {
1247 err = -ENOMEM;
1248 goto fail;
1249 }
1250
1251 dev->req->complete = gmidi_setup_complete;
1252
1253 gadget->ep0->driver_data = dev;
1254
1255 INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
1256 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
1257 EP_OUT_NAME, EP_IN_NAME);
1258
1259 /* register as an ALSA sound card */
1260 err = gmidi_register_card(dev);
1261 if (err < 0) {
1262 goto fail;
1263 }
1264
1265 VDBG(dev, "gmidi_bind finished ok\n");
1266 return 0; 184 return 0;
1267
1268fail:
1269 gmidi_unbind(gadget);
1270 return err;
1271}
1272
1273
1274static void gmidi_suspend(struct usb_gadget *gadget)
1275{
1276 struct gmidi_device *dev = get_gadget_data(gadget);
1277
1278 if (gadget->speed == USB_SPEED_UNKNOWN) {
1279 return;
1280 }
1281
1282 DBG(dev, "suspend\n");
1283}
1284
1285static void gmidi_resume(struct usb_gadget *gadget)
1286{
1287 struct gmidi_device *dev = get_gadget_data(gadget);
1288
1289 DBG(dev, "resume\n");
1290} 185}
1291 186
1292 187static struct usb_composite_driver midi_driver = {
1293static struct usb_gadget_driver gmidi_driver = { 188 .name = (char *) longname,
1294 .speed = USB_SPEED_FULL, 189 .dev = &device_desc,
1295 .function = (char *)longname, 190 .strings = dev_strings,
1296 .unbind = gmidi_unbind, 191 .max_speed = USB_SPEED_HIGH,
1297 192 .unbind = __exit_p(midi_unbind),
1298 .setup = gmidi_setup,
1299 .disconnect = gmidi_disconnect,
1300
1301 .suspend = gmidi_suspend,
1302 .resume = gmidi_resume,
1303
1304 .driver = {
1305 .name = (char *)shortname,
1306 .owner = THIS_MODULE,
1307 },
1308}; 193};
1309 194
1310static int __init gmidi_init(void) 195static int __init midi_init(void)
1311{ 196{
1312 return usb_gadget_probe_driver(&gmidi_driver, gmidi_bind); 197 return usb_composite_probe(&midi_driver, midi_bind);
1313} 198}
1314module_init(gmidi_init); 199module_init(midi_init);
1315 200
1316static void __exit gmidi_cleanup(void) 201static void __exit midi_cleanup(void)
1317{ 202{
1318 usb_gadget_unregister_driver(&gmidi_driver); 203 usb_composite_unregister(&midi_driver);
1319} 204}
1320module_exit(gmidi_cleanup); 205module_exit(midi_cleanup);
1321 206
diff --git a/drivers/usb/gadget/hid.c b/drivers/usb/gadget/hid.c
index 9fb575034a0e..f888c3ede860 100644
--- a/drivers/usb/gadget/hid.c
+++ b/drivers/usb/gadget/hid.c
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23 14
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index 692fd9b2248b..2d978c0e7ced 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -689,7 +689,7 @@ static int imx_ep_enable(struct usb_ep *usb_ep,
689 return -EINVAL; 689 return -EINVAL;
690 } 690 }
691 691
692 if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) { 692 if (imx_ep->fifosize < usb_endpoint_maxp(desc)) {
693 D_ERR(imx_usb->dev, 693 D_ERR(imx_usb->dev,
694 "<%s> bad %s maxpacket\n", __func__, usb_ep->name); 694 "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
695 return -ERANGE; 695 return -ERANGE;
@@ -1478,7 +1478,7 @@ static int __init imx_udc_probe(struct platform_device *pdev)
1478 1478
1479 for (i = 0; i < IMX_USB_NB_EP + 1; i++) { 1479 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1480 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i), 1480 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
1481 IRQF_DISABLED, driver_name, imx_usb); 1481 0, driver_name, imx_usb);
1482 if (ret) { 1482 if (ret) {
1483 dev_err(&pdev->dev, "can't get irq %i, err %d\n", 1483 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
1484 imx_usb->usbd_int[i], ret); 1484 imx_usb->usbd_int[i], ret);
diff --git a/drivers/usb/gadget/imx_udc.h b/drivers/usb/gadget/imx_udc.h
index 7136c242b4ec..d118fb777840 100644
--- a/drivers/usb/gadget/imx_udc.h
+++ b/drivers/usb/gadget/imx_udc.h
@@ -8,11 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */ 11 */
17 12
18#ifndef __LINUX_USB_GADGET_IMX_H 13#ifndef __LINUX_USB_GADGET_IMX_H
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 1b240990448f..6ccae2707e59 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22 13
@@ -1739,8 +1730,9 @@ static void
1739gadgetfs_disconnect (struct usb_gadget *gadget) 1730gadgetfs_disconnect (struct usb_gadget *gadget)
1740{ 1731{
1741 struct dev_data *dev = get_gadget_data (gadget); 1732 struct dev_data *dev = get_gadget_data (gadget);
1733 unsigned long flags;
1742 1734
1743 spin_lock (&dev->lock); 1735 spin_lock_irqsave (&dev->lock, flags);
1744 if (dev->state == STATE_DEV_UNCONNECTED) 1736 if (dev->state == STATE_DEV_UNCONNECTED)
1745 goto exit; 1737 goto exit;
1746 dev->state = STATE_DEV_UNCONNECTED; 1738 dev->state = STATE_DEV_UNCONNECTED;
@@ -1749,7 +1741,7 @@ gadgetfs_disconnect (struct usb_gadget *gadget)
1749 next_event (dev, GADGETFS_DISCONNECT); 1741 next_event (dev, GADGETFS_DISCONNECT);
1750 ep0_readable (dev); 1742 ep0_readable (dev);
1751exit: 1743exit:
1752 spin_unlock (&dev->lock); 1744 spin_unlock_irqrestore (&dev->lock, flags);
1753} 1745}
1754 1746
1755static void 1747static void
diff --git a/drivers/usb/gadget/langwell_udc.c b/drivers/usb/gadget/langwell_udc.c
index a06e2c27b435..c9fa3bf5b377 100644
--- a/drivers/usb/gadget/langwell_udc.c
+++ b/drivers/usb/gadget/langwell_udc.c
@@ -5,16 +5,6 @@
5 * This program is free software; you can redistribute it and/or modify it 5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License, 6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation. 7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */ 8 */
19 9
20 10
@@ -60,9 +50,6 @@ static const char driver_name[] = "langwell_udc";
60static const char driver_desc[] = DRIVER_DESC; 50static const char driver_desc[] = DRIVER_DESC;
61 51
62 52
63/* controller device global variable */
64static struct langwell_udc *the_controller;
65
66/* for endpoint 0 operations */ 53/* for endpoint 0 operations */
67static const struct usb_endpoint_descriptor 54static const struct usb_endpoint_descriptor
68langwell_ep0_desc = { 55langwell_ep0_desc = {
@@ -283,7 +270,7 @@ static int langwell_ep_enable(struct usb_ep *_ep,
283 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 270 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
284 return -ESHUTDOWN; 271 return -ESHUTDOWN;
285 272
286 max = le16_to_cpu(desc->wMaxPacketSize); 273 max = usb_endpoint_maxp(desc);
287 274
288 /* 275 /*
289 * disable HW zero length termination select 276 * disable HW zero length termination select
@@ -1321,9 +1308,12 @@ static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
1321 return 0; 1308 return 0;
1322} 1309}
1323 1310
1324static int langwell_start(struct usb_gadget_driver *driver, 1311static int langwell_start(struct usb_gadget *g,
1325 int (*bind)(struct usb_gadget *)); 1312 struct usb_gadget_driver *driver);
1326static int langwell_stop(struct usb_gadget_driver *driver); 1313
1314static int langwell_stop(struct usb_gadget *g,
1315 struct usb_gadget_driver *driver);
1316
1327/* device controller usb_gadget_ops structure */ 1317/* device controller usb_gadget_ops structure */
1328static const struct usb_gadget_ops langwell_ops = { 1318static const struct usb_gadget_ops langwell_ops = {
1329 1319
@@ -1345,8 +1335,8 @@ static const struct usb_gadget_ops langwell_ops = {
1345 /* D+ pullup, software-controlled connect/disconnect to USB host */ 1335 /* D+ pullup, software-controlled connect/disconnect to USB host */
1346 .pullup = langwell_pullup, 1336 .pullup = langwell_pullup,
1347 1337
1348 .start = langwell_start, 1338 .udc_start = langwell_start,
1349 .stop = langwell_stop, 1339 .udc_stop = langwell_stop,
1350}; 1340};
1351 1341
1352 1342
@@ -1561,7 +1551,7 @@ static void stop_activity(struct langwell_udc *dev,
1561static ssize_t show_function(struct device *_dev, 1551static ssize_t show_function(struct device *_dev,
1562 struct device_attribute *attr, char *buf) 1552 struct device_attribute *attr, char *buf)
1563{ 1553{
1564 struct langwell_udc *dev = the_controller; 1554 struct langwell_udc *dev = dev_get_drvdata(_dev);
1565 1555
1566 if (!dev->driver || !dev->driver->function 1556 if (!dev->driver || !dev->driver->function
1567 || strlen(dev->driver->function) > PAGE_SIZE) 1557 || strlen(dev->driver->function) > PAGE_SIZE)
@@ -1572,11 +1562,25 @@ static ssize_t show_function(struct device *_dev,
1572static DEVICE_ATTR(function, S_IRUGO, show_function, NULL); 1562static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
1573 1563
1574 1564
1565static inline enum usb_device_speed lpm_device_speed(u32 reg)
1566{
1567 switch (LPM_PSPD(reg)) {
1568 case LPM_SPEED_HIGH:
1569 return USB_SPEED_HIGH;
1570 case LPM_SPEED_FULL:
1571 return USB_SPEED_FULL;
1572 case LPM_SPEED_LOW:
1573 return USB_SPEED_LOW;
1574 default:
1575 return USB_SPEED_UNKNOWN;
1576 }
1577}
1578
1575/* device "langwell_udc" sysfs attribute file */ 1579/* device "langwell_udc" sysfs attribute file */
1576static ssize_t show_langwell_udc(struct device *_dev, 1580static ssize_t show_langwell_udc(struct device *_dev,
1577 struct device_attribute *attr, char *buf) 1581 struct device_attribute *attr, char *buf)
1578{ 1582{
1579 struct langwell_udc *dev = the_controller; 1583 struct langwell_udc *dev = dev_get_drvdata(_dev);
1580 struct langwell_request *req; 1584 struct langwell_request *req;
1581 struct langwell_ep *ep = NULL; 1585 struct langwell_ep *ep = NULL;
1582 char *next; 1586 char *next;
@@ -1700,20 +1704,7 @@ static ssize_t show_langwell_udc(struct device *_dev,
1700 "BmAttributes: %d\n\n", 1704 "BmAttributes: %d\n\n",
1701 LPM_PTS(tmp_reg), 1705 LPM_PTS(tmp_reg),
1702 (tmp_reg & LPM_STS) ? 1 : 0, 1706 (tmp_reg & LPM_STS) ? 1 : 0,
1703 ({ 1707 usb_speed_string(lpm_device_speed(tmp_reg)),
1704 char *s;
1705 switch (LPM_PSPD(tmp_reg)) {
1706 case LPM_SPEED_FULL:
1707 s = "Full Speed"; break;
1708 case LPM_SPEED_LOW:
1709 s = "Low Speed"; break;
1710 case LPM_SPEED_HIGH:
1711 s = "High Speed"; break;
1712 default:
1713 s = "Unknown Speed"; break;
1714 }
1715 s;
1716 }),
1717 (tmp_reg & LPM_PFSC) ? "Force Full Speed" : "Not Force", 1708 (tmp_reg & LPM_PFSC) ? "Force Full Speed" : "Not Force",
1718 (tmp_reg & LPM_PHCD) ? "Disabled" : "Enabled", 1709 (tmp_reg & LPM_PHCD) ? "Disabled" : "Enabled",
1719 LPM_BA(tmp_reg)); 1710 LPM_BA(tmp_reg));
@@ -1821,7 +1812,7 @@ static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);
1821static ssize_t store_remote_wakeup(struct device *_dev, 1812static ssize_t store_remote_wakeup(struct device *_dev,
1822 struct device_attribute *attr, const char *buf, size_t count) 1813 struct device_attribute *attr, const char *buf, size_t count)
1823{ 1814{
1824 struct langwell_udc *dev = the_controller; 1815 struct langwell_udc *dev = dev_get_drvdata(_dev);
1825 unsigned long flags; 1816 unsigned long flags;
1826 ssize_t rc = count; 1817 ssize_t rc = count;
1827 1818
@@ -1857,21 +1848,15 @@ static DEVICE_ATTR(remote_wakeup, S_IWUSR, NULL, store_remote_wakeup);
1857 * the driver might get unbound. 1848 * the driver might get unbound.
1858 */ 1849 */
1859 1850
1860static int langwell_start(struct usb_gadget_driver *driver, 1851static int langwell_start(struct usb_gadget *g,
1861 int (*bind)(struct usb_gadget *)) 1852 struct usb_gadget_driver *driver)
1862{ 1853{
1863 struct langwell_udc *dev = the_controller; 1854 struct langwell_udc *dev = gadget_to_langwell(g);
1864 unsigned long flags; 1855 unsigned long flags;
1865 int retval; 1856 int retval;
1866 1857
1867 if (!dev)
1868 return -ENODEV;
1869
1870 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 1858 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1871 1859
1872 if (dev->driver)
1873 return -EBUSY;
1874
1875 spin_lock_irqsave(&dev->lock, flags); 1860 spin_lock_irqsave(&dev->lock, flags);
1876 1861
1877 /* hook up the driver ... */ 1862 /* hook up the driver ... */
@@ -1881,18 +1866,9 @@ static int langwell_start(struct usb_gadget_driver *driver,
1881 1866
1882 spin_unlock_irqrestore(&dev->lock, flags); 1867 spin_unlock_irqrestore(&dev->lock, flags);
1883 1868
1884 retval = bind(&dev->gadget);
1885 if (retval) {
1886 dev_dbg(&dev->pdev->dev, "bind to driver %s --> %d\n",
1887 driver->driver.name, retval);
1888 dev->driver = NULL;
1889 dev->gadget.dev.driver = NULL;
1890 return retval;
1891 }
1892
1893 retval = device_create_file(&dev->pdev->dev, &dev_attr_function); 1869 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
1894 if (retval) 1870 if (retval)
1895 goto err_unbind; 1871 goto err;
1896 1872
1897 dev->usb_state = USB_STATE_ATTACHED; 1873 dev->usb_state = USB_STATE_ATTACHED;
1898 dev->ep0_state = WAIT_FOR_SETUP; 1874 dev->ep0_state = WAIT_FOR_SETUP;
@@ -1909,31 +1885,27 @@ static int langwell_start(struct usb_gadget_driver *driver,
1909 dev_info(&dev->pdev->dev, "register driver: %s\n", 1885 dev_info(&dev->pdev->dev, "register driver: %s\n",
1910 driver->driver.name); 1886 driver->driver.name);
1911 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__); 1887 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1888
1912 return 0; 1889 return 0;
1913 1890
1914err_unbind: 1891err:
1915 driver->unbind(&dev->gadget);
1916 dev->gadget.dev.driver = NULL; 1892 dev->gadget.dev.driver = NULL;
1917 dev->driver = NULL; 1893 dev->driver = NULL;
1918 1894
1919 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__); 1895 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1896
1920 return retval; 1897 return retval;
1921} 1898}
1922 1899
1923/* unregister gadget driver */ 1900/* unregister gadget driver */
1924static int langwell_stop(struct usb_gadget_driver *driver) 1901static int langwell_stop(struct usb_gadget *g,
1902 struct usb_gadget_driver *driver)
1925{ 1903{
1926 struct langwell_udc *dev = the_controller; 1904 struct langwell_udc *dev = gadget_to_langwell(g);
1927 unsigned long flags; 1905 unsigned long flags;
1928 1906
1929 if (!dev)
1930 return -ENODEV;
1931
1932 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 1907 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1933 1908
1934 if (unlikely(!driver || !driver->unbind))
1935 return -EINVAL;
1936
1937 /* exit PHY low power suspend */ 1909 /* exit PHY low power suspend */
1938 if (dev->pdev->device != 0x0829) 1910 if (dev->pdev->device != 0x0829)
1939 langwell_phy_low_power(dev, 0); 1911 langwell_phy_low_power(dev, 0);
@@ -1956,8 +1928,6 @@ static int langwell_stop(struct usb_gadget_driver *driver)
1956 stop_activity(dev, driver); 1928 stop_activity(dev, driver);
1957 spin_unlock_irqrestore(&dev->lock, flags); 1929 spin_unlock_irqrestore(&dev->lock, flags);
1958 1930
1959 /* unbind gadget driver */
1960 driver->unbind(&dev->gadget);
1961 dev->gadget.dev.driver = NULL; 1931 dev->gadget.dev.driver = NULL;
1962 dev->driver = NULL; 1932 dev->driver = NULL;
1963 1933
@@ -1966,6 +1936,7 @@ static int langwell_stop(struct usb_gadget_driver *driver)
1966 dev_info(&dev->pdev->dev, "unregistered driver '%s'\n", 1936 dev_info(&dev->pdev->dev, "unregistered driver '%s'\n",
1967 driver->driver.name); 1937 driver->driver.name);
1968 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__); 1938 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1939
1969 return 0; 1940 return 0;
1970} 1941}
1971 1942
@@ -2657,12 +2628,10 @@ done:
2657 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__); 2628 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2658} 2629}
2659 2630
2660
2661/* port change detect interrupt handler */ 2631/* port change detect interrupt handler */
2662static void handle_port_change(struct langwell_udc *dev) 2632static void handle_port_change(struct langwell_udc *dev)
2663{ 2633{
2664 u32 portsc1, devlc; 2634 u32 portsc1, devlc;
2665 u32 speed;
2666 2635
2667 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__); 2636 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2668 2637
@@ -2677,24 +2646,9 @@ static void handle_port_change(struct langwell_udc *dev)
2677 /* bus reset is finished */ 2646 /* bus reset is finished */
2678 if (!(portsc1 & PORTS_PR)) { 2647 if (!(portsc1 & PORTS_PR)) {
2679 /* get the speed */ 2648 /* get the speed */
2680 speed = LPM_PSPD(devlc); 2649 dev->gadget.speed = lpm_device_speed(devlc);
2681 switch (speed) { 2650 dev_vdbg(&dev->pdev->dev, "dev->gadget.speed = %d\n",
2682 case LPM_SPEED_HIGH: 2651 dev->gadget.speed);
2683 dev->gadget.speed = USB_SPEED_HIGH;
2684 break;
2685 case LPM_SPEED_FULL:
2686 dev->gadget.speed = USB_SPEED_FULL;
2687 break;
2688 case LPM_SPEED_LOW:
2689 dev->gadget.speed = USB_SPEED_LOW;
2690 break;
2691 default:
2692 dev->gadget.speed = USB_SPEED_UNKNOWN;
2693 break;
2694 }
2695 dev_vdbg(&dev->pdev->dev,
2696 "speed = %d, dev->gadget.speed = %d\n",
2697 speed, dev->gadget.speed);
2698 } 2652 }
2699 2653
2700 /* LPM L0 to L1 */ 2654 /* LPM L0 to L1 */
@@ -2969,7 +2923,7 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2969 handle_port_change(dev); 2923 handle_port_change(dev);
2970 } 2924 }
2971 2925
2972 /* suspend interrrupt */ 2926 /* suspend interrupt */
2973 if (irq_sts & STS_SLI) { 2927 if (irq_sts & STS_SLI) {
2974 dev_vdbg(&dev->pdev->dev, "suspend interrupt\n"); 2928 dev_vdbg(&dev->pdev->dev, "suspend interrupt\n");
2975 handle_bus_suspend(dev); 2929 handle_bus_suspend(dev);
@@ -2999,7 +2953,7 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2999/* release device structure */ 2953/* release device structure */
3000static void gadget_release(struct device *_dev) 2954static void gadget_release(struct device *_dev)
3001{ 2955{
3002 struct langwell_udc *dev = the_controller; 2956 struct langwell_udc *dev = dev_get_drvdata(_dev);
3003 2957
3004 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 2958 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3005 2959
@@ -3057,7 +3011,7 @@ static void sram_deinit(struct langwell_udc *dev)
3057/* tear down the binding between this driver and the pci device */ 3011/* tear down the binding between this driver and the pci device */
3058static void langwell_udc_remove(struct pci_dev *pdev) 3012static void langwell_udc_remove(struct pci_dev *pdev)
3059{ 3013{
3060 struct langwell_udc *dev = the_controller; 3014 struct langwell_udc *dev = pci_get_drvdata(pdev);
3061 3015
3062 DECLARE_COMPLETION(done); 3016 DECLARE_COMPLETION(done);
3063 3017
@@ -3124,8 +3078,6 @@ static void langwell_udc_remove(struct pci_dev *pdev)
3124 3078
3125 /* free dev, wait for the release() finished */ 3079 /* free dev, wait for the release() finished */
3126 wait_for_completion(&done); 3080 wait_for_completion(&done);
3127
3128 the_controller = NULL;
3129} 3081}
3130 3082
3131 3083
@@ -3144,11 +3096,6 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3144 size_t size; 3096 size_t size;
3145 int retval; 3097 int retval;
3146 3098
3147 if (the_controller) {
3148 dev_warn(&pdev->dev, "ignoring\n");
3149 return -EBUSY;
3150 }
3151
3152 /* alloc, and start init */ 3099 /* alloc, and start init */
3153 dev = kzalloc(sizeof *dev, GFP_KERNEL); 3100 dev = kzalloc(sizeof *dev, GFP_KERNEL);
3154 if (dev == NULL) { 3101 if (dev == NULL) {
@@ -3368,8 +3315,6 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3368 "After langwell_udc_probe(), print all registers:\n"); 3315 "After langwell_udc_probe(), print all registers:\n");
3369 print_all_registers(dev); 3316 print_all_registers(dev);
3370 3317
3371 the_controller = dev;
3372
3373 retval = device_register(&dev->gadget.dev); 3318 retval = device_register(&dev->gadget.dev);
3374 if (retval) 3319 if (retval)
3375 goto error; 3320 goto error;
@@ -3404,7 +3349,7 @@ error:
3404/* device controller suspend */ 3349/* device controller suspend */
3405static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state) 3350static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3406{ 3351{
3407 struct langwell_udc *dev = the_controller; 3352 struct langwell_udc *dev = pci_get_drvdata(pdev);
3408 3353
3409 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 3354 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3410 3355
@@ -3452,7 +3397,7 @@ static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3452/* device controller resume */ 3397/* device controller resume */
3453static int langwell_udc_resume(struct pci_dev *pdev) 3398static int langwell_udc_resume(struct pci_dev *pdev)
3454{ 3399{
3455 struct langwell_udc *dev = the_controller; 3400 struct langwell_udc *dev = pci_get_drvdata(pdev);
3456 size_t size; 3401 size_t size;
3457 3402
3458 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 3403 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
@@ -3534,7 +3479,7 @@ static int langwell_udc_resume(struct pci_dev *pdev)
3534/* pci driver shutdown */ 3479/* pci driver shutdown */
3535static void langwell_udc_shutdown(struct pci_dev *pdev) 3480static void langwell_udc_shutdown(struct pci_dev *pdev)
3536{ 3481{
3537 struct langwell_udc *dev = the_controller; 3482 struct langwell_udc *dev = pci_get_drvdata(pdev);
3538 u32 usbmode; 3483 u32 usbmode;
3539 3484
3540 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__); 3485 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
diff --git a/drivers/usb/gadget/langwell_udc.h b/drivers/usb/gadget/langwell_udc.h
index f1d9c1bb04f3..ef79e242b7b0 100644
--- a/drivers/usb/gadget/langwell_udc.h
+++ b/drivers/usb/gadget/langwell_udc.h
@@ -5,16 +5,6 @@
5 * This program is free software; you can redistribute it and/or modify it 5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License, 6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation. 7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */ 8 */
19 9
20#include <linux/usb/langwell_udc.h> 10#include <linux/usb/langwell_udc.h>
@@ -231,3 +221,5 @@ struct langwell_udc {
231 u16 dev_status; 221 u16 dev_status;
232}; 222};
233 223
224#define gadget_to_langwell(g) container_of((g), struct langwell_udc, gadget)
225
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c
index 491f825ed5c9..91d0af2a24a8 100644
--- a/drivers/usb/gadget/m66592-udc.c
+++ b/drivers/usb/gadget/m66592-udc.c
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22 12
23#include <linux/module.h> 13#include <linux/module.h>
@@ -370,7 +360,7 @@ static void m66592_ep_setting(struct m66592 *m66592, struct m66592_ep *ep,
370 360
371 ep->pipectr = get_pipectr_addr(pipenum); 361 ep->pipectr = get_pipectr_addr(pipenum);
372 ep->pipenum = pipenum; 362 ep->pipenum = pipenum;
373 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 363 ep->ep.maxpacket = usb_endpoint_maxp(desc);
374 m66592->pipenum2ep[pipenum] = ep; 364 m66592->pipenum2ep[pipenum] = ep;
375 m66592->epaddr2ep[desc->bEndpointAddress&USB_ENDPOINT_NUMBER_MASK] = ep; 365 m66592->epaddr2ep[desc->bEndpointAddress&USB_ENDPOINT_NUMBER_MASK] = ep;
376 INIT_LIST_HEAD(&ep->queue); 366 INIT_LIST_HEAD(&ep->queue);
@@ -447,7 +437,7 @@ static int alloc_pipe_config(struct m66592_ep *ep,
447 ep->type = info.type; 437 ep->type = info.type;
448 438
449 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 439 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
450 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 440 info.maxpacket = usb_endpoint_maxp(desc);
451 info.interval = desc->bInterval; 441 info.interval = desc->bInterval;
452 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 442 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
453 info.dir_in = 1; 443 info.dir_in = 1;
@@ -1674,7 +1664,7 @@ static int __init m66592_probe(struct platform_device *pdev)
1674 m66592->timer.data = (unsigned long)m66592; 1664 m66592->timer.data = (unsigned long)m66592;
1675 m66592->reg = reg; 1665 m66592->reg = reg;
1676 1666
1677 ret = request_irq(ires->start, m66592_irq, IRQF_DISABLED | IRQF_SHARED, 1667 ret = request_irq(ires->start, m66592_irq, IRQF_SHARED,
1678 udc_name, m66592); 1668 udc_name, m66592);
1679 if (ret < 0) { 1669 if (ret < 0) {
1680 pr_err("request_irq error (%d)\n", ret); 1670 pr_err("request_irq error (%d)\n", ret);
diff --git a/drivers/usb/gadget/m66592-udc.h b/drivers/usb/gadget/m66592-udc.h
index 7b93d579af37..9d9f7e39f037 100644
--- a/drivers/usb/gadget/m66592-udc.h
+++ b/drivers/usb/gadget/m66592-udc.h
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22 12
23#ifndef __M66592_UDC_H__ 13#ifndef __M66592_UDC_H__
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
index d3eb27427c58..e24f72f82a47 100644
--- a/drivers/usb/gadget/mass_storage.c
+++ b/drivers/usb/gadget/mass_storage.c
@@ -10,15 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 13 */
23 14
24 15
@@ -169,7 +160,7 @@ static struct usb_composite_driver msg_driver = {
169 .name = "g_mass_storage", 160 .name = "g_mass_storage",
170 .dev = &msg_device_desc, 161 .dev = &msg_device_desc,
171 .iProduct = DRIVER_DESC, 162 .iProduct = DRIVER_DESC,
172 .max_speed = USB_SPEED_HIGH, 163 .max_speed = USB_SPEED_SUPER,
173 .needs_serial = 1, 164 .needs_serial = 1,
174}; 165};
175 166
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
index 8c7b74717d85..7e7f515b8b19 100644
--- a/drivers/usb/gadget/multi.c
+++ b/drivers/usb/gadget/multi.c
@@ -10,15 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 13 */
23 14
24 15
diff --git a/drivers/usb/gadget/mv_udc.h b/drivers/usb/gadget/mv_udc.h
index 65f1f7c3bd4e..daa75c12f336 100644
--- a/drivers/usb/gadget/mv_udc.h
+++ b/drivers/usb/gadget/mv_udc.h
@@ -1,3 +1,11 @@
1/*
2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 */
1 9
2#ifndef __MV_UDC_H 10#ifndef __MV_UDC_H
3#define __MV_UDC_H 11#define __MV_UDC_H
@@ -194,14 +202,25 @@ struct mv_udc {
194 unsigned int ep0_dir; 202 unsigned int ep0_dir;
195 203
196 unsigned int dev_addr; 204 unsigned int dev_addr;
205 unsigned int test_mode;
197 206
198 int errors; 207 int errors;
199 unsigned softconnect:1, 208 unsigned softconnect:1,
200 vbus_active:1, 209 vbus_active:1,
201 remote_wakeup:1, 210 remote_wakeup:1,
202 softconnected:1, 211 softconnected:1,
203 force_fs:1; 212 force_fs:1,
204 struct clk *clk; 213 clock_gating:1,
214 active:1;
215
216 struct work_struct vbus_work;
217 struct workqueue_struct *qwork;
218
219 struct mv_usb_platform_data *pdata;
220
221 /* some SOC has mutiple clock sources for USB*/
222 unsigned int clknum;
223 struct clk *clk[0];
205}; 224};
206 225
207/* endpoint data structure */ 226/* endpoint data structure */
@@ -225,6 +244,7 @@ struct mv_req {
225 struct mv_dtd *dtd, *head, *tail; 244 struct mv_dtd *dtd, *head, *tail;
226 struct mv_ep *ep; 245 struct mv_ep *ep;
227 struct list_head queue; 246 struct list_head queue;
247 unsigned int test_mode;
228 unsigned dtd_count; 248 unsigned dtd_count;
229 unsigned mapped:1; 249 unsigned mapped:1;
230}; 250};
@@ -289,6 +309,4 @@ struct mv_dtd {
289 struct mv_dtd *next_dtd_virt; 309 struct mv_dtd *next_dtd_virt;
290}; 310};
291 311
292extern int mv_udc_phy_init(unsigned int base);
293
294#endif 312#endif
diff --git a/drivers/usb/gadget/mv_udc_core.c b/drivers/usb/gadget/mv_udc_core.c
index ce1ac2bcb314..892412103dd8 100644
--- a/drivers/usb/gadget/mv_udc_core.c
+++ b/drivers/usb/gadget/mv_udc_core.c
@@ -1,3 +1,14 @@
1/*
2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
3 * Author: Chao Xie <chao.xie@marvell.com>
4 * Neil Zhang <zhangwm@marvell.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
1#include <linux/module.h> 12#include <linux/module.h>
2#include <linux/pci.h> 13#include <linux/pci.h>
3#include <linux/dma-mapping.h> 14#include <linux/dma-mapping.h>
@@ -22,6 +33,7 @@
22#include <linux/irq.h> 33#include <linux/irq.h>
23#include <linux/platform_device.h> 34#include <linux/platform_device.h>
24#include <linux/clk.h> 35#include <linux/clk.h>
36#include <linux/platform_data/mv_usb.h>
25#include <asm/system.h> 37#include <asm/system.h>
26#include <asm/unaligned.h> 38#include <asm/unaligned.h>
27 39
@@ -45,6 +57,8 @@
45#define LOOPS_USEC (1 << LOOPS_USEC_SHIFT) 57#define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
46#define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT) 58#define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
47 59
60static DECLARE_COMPLETION(release_done);
61
48static const char driver_name[] = "mv_udc"; 62static const char driver_name[] = "mv_udc";
49static const char driver_desc[] = DRIVER_DESC; 63static const char driver_desc[] = DRIVER_DESC;
50 64
@@ -53,6 +67,7 @@ static struct mv_udc *the_controller;
53int mv_usb_otgsc; 67int mv_usb_otgsc;
54 68
55static void nuke(struct mv_ep *ep, int status); 69static void nuke(struct mv_ep *ep, int status);
70static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver);
56 71
57/* for endpoint 0 operations */ 72/* for endpoint 0 operations */
58static const struct usb_endpoint_descriptor mv_ep0_desc = { 73static const struct usb_endpoint_descriptor mv_ep0_desc = {
@@ -82,14 +97,16 @@ static void ep0_reset(struct mv_udc *udc)
82 (EP0_MAX_PKT_SIZE << EP_QUEUE_HEAD_MAX_PKT_LEN_POS) 97 (EP0_MAX_PKT_SIZE << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
83 | EP_QUEUE_HEAD_IOS; 98 | EP_QUEUE_HEAD_IOS;
84 99
100 ep->dqh->next_dtd_ptr = EP_QUEUE_HEAD_NEXT_TERMINATE;
101
85 epctrlx = readl(&udc->op_regs->epctrlx[0]); 102 epctrlx = readl(&udc->op_regs->epctrlx[0]);
86 if (i) { /* TX */ 103 if (i) { /* TX */
87 epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST 104 epctrlx |= EPCTRL_TX_ENABLE
88 | (USB_ENDPOINT_XFER_CONTROL 105 | (USB_ENDPOINT_XFER_CONTROL
89 << EPCTRL_TX_EP_TYPE_SHIFT); 106 << EPCTRL_TX_EP_TYPE_SHIFT);
90 107
91 } else { /* RX */ 108 } else { /* RX */
92 epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST 109 epctrlx |= EPCTRL_RX_ENABLE
93 | (USB_ENDPOINT_XFER_CONTROL 110 | (USB_ENDPOINT_XFER_CONTROL
94 << EPCTRL_RX_EP_TYPE_SHIFT); 111 << EPCTRL_RX_EP_TYPE_SHIFT);
95 } 112 }
@@ -122,6 +139,7 @@ static int process_ep_req(struct mv_udc *udc, int index,
122 int i, direction; 139 int i, direction;
123 int retval = 0; 140 int retval = 0;
124 u32 errors; 141 u32 errors;
142 u32 bit_pos;
125 143
126 curr_dqh = &udc->ep_dqh[index]; 144 curr_dqh = &udc->ep_dqh[index];
127 direction = index % 2; 145 direction = index % 2;
@@ -139,10 +157,20 @@ static int process_ep_req(struct mv_udc *udc, int index,
139 157
140 errors = curr_dtd->size_ioc_sts & DTD_ERROR_MASK; 158 errors = curr_dtd->size_ioc_sts & DTD_ERROR_MASK;
141 if (!errors) { 159 if (!errors) {
142 remaining_length += 160 remaining_length =
143 (curr_dtd->size_ioc_sts & DTD_PACKET_SIZE) 161 (curr_dtd->size_ioc_sts & DTD_PACKET_SIZE)
144 >> DTD_LENGTH_BIT_POS; 162 >> DTD_LENGTH_BIT_POS;
145 actual -= remaining_length; 163 actual -= remaining_length;
164
165 if (remaining_length) {
166 if (direction) {
167 dev_dbg(&udc->dev->dev,
168 "TX dTD remains data\n");
169 retval = -EPROTO;
170 break;
171 } else
172 break;
173 }
146 } else { 174 } else {
147 dev_info(&udc->dev->dev, 175 dev_info(&udc->dev->dev,
148 "complete_tr error: ep=%d %s: error = 0x%x\n", 176 "complete_tr error: ep=%d %s: error = 0x%x\n",
@@ -164,6 +192,20 @@ static int process_ep_req(struct mv_udc *udc, int index,
164 if (retval) 192 if (retval)
165 return retval; 193 return retval;
166 194
195 if (direction == EP_DIR_OUT)
196 bit_pos = 1 << curr_req->ep->ep_num;
197 else
198 bit_pos = 1 << (16 + curr_req->ep->ep_num);
199
200 while ((curr_dqh->curr_dtd_ptr == curr_dtd->td_dma)) {
201 if (curr_dtd->dtd_next == EP_QUEUE_HEAD_NEXT_TERMINATE) {
202 while (readl(&udc->op_regs->epstatus) & bit_pos)
203 udelay(1);
204 break;
205 }
206 udelay(1);
207 }
208
167 curr_req->req.actual = actual; 209 curr_req->req.actual = actual;
168 210
169 return 0; 211 return 0;
@@ -335,7 +377,7 @@ static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
335 } else { 377 } else {
336 /* Write dQH next pointer and terminate bit to 0 */ 378 /* Write dQH next pointer and terminate bit to 0 */
337 dqh->next_dtd_ptr = req->head->td_dma 379 dqh->next_dtd_ptr = req->head->td_dma
338 & EP_QUEUE_HEAD_NEXT_POINTER_MASK;; 380 & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
339 dqh->size_ioc_int_sts = 0; 381 dqh->size_ioc_int_sts = 0;
340 382
341 /* Ensure that updates to the QH will occur before priming. */ 383 /* Ensure that updates to the QH will occur before priming. */
@@ -376,7 +418,7 @@ static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
376 } 418 }
377 } 419 }
378done: 420done:
379 return retval;; 421 return retval;
380} 422}
381 423
382static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length, 424static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length,
@@ -481,6 +523,7 @@ static int mv_ep_enable(struct usb_ep *_ep,
481 u16 max = 0; 523 u16 max = 0;
482 u32 bit_pos, epctrlx, direction; 524 u32 bit_pos, epctrlx, direction;
483 unsigned char zlt = 0, ios = 0, mult = 0; 525 unsigned char zlt = 0, ios = 0, mult = 0;
526 unsigned long flags;
484 527
485 ep = container_of(_ep, struct mv_ep, ep); 528 ep = container_of(_ep, struct mv_ep, ep);
486 udc = ep->udc; 529 udc = ep->udc;
@@ -493,7 +536,7 @@ static int mv_ep_enable(struct usb_ep *_ep,
493 return -ESHUTDOWN; 536 return -ESHUTDOWN;
494 537
495 direction = ep_dir(ep); 538 direction = ep_dir(ep);
496 max = le16_to_cpu(desc->wMaxPacketSize); 539 max = usb_endpoint_maxp(desc);
497 540
498 /* 541 /*
499 * disable HW zero length termination select 542 * disable HW zero length termination select
@@ -501,9 +544,6 @@ static int mv_ep_enable(struct usb_ep *_ep,
501 */ 544 */
502 zlt = 1; 545 zlt = 1;
503 546
504 /* Get the endpoint queue head address */
505 dqh = (struct mv_dqh *)ep->dqh;
506
507 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num); 547 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
508 548
509 /* Check if the Endpoint is Primed */ 549 /* Check if the Endpoint is Primed */
@@ -532,7 +572,7 @@ static int mv_ep_enable(struct usb_ep *_ep,
532 case USB_ENDPOINT_XFER_ISOC: 572 case USB_ENDPOINT_XFER_ISOC:
533 /* Calculate transactions needed for high bandwidth iso */ 573 /* Calculate transactions needed for high bandwidth iso */
534 mult = (unsigned char)(1 + ((max >> 11) & 0x03)); 574 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
535 max = max & 0x8ff; /* bit 0~10 */ 575 max = max & 0x7ff; /* bit 0~10 */
536 /* 3 transactions at most */ 576 /* 3 transactions at most */
537 if (mult > 3) 577 if (mult > 3)
538 goto en_done; 578 goto en_done;
@@ -540,6 +580,10 @@ static int mv_ep_enable(struct usb_ep *_ep,
540 default: 580 default:
541 goto en_done; 581 goto en_done;
542 } 582 }
583
584 spin_lock_irqsave(&udc->lock, flags);
585 /* Get the endpoint queue head address */
586 dqh = ep->dqh;
543 dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS) 587 dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
544 | (mult << EP_QUEUE_HEAD_MULT_POS) 588 | (mult << EP_QUEUE_HEAD_MULT_POS)
545 | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0) 589 | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0)
@@ -572,18 +616,20 @@ static int mv_ep_enable(struct usb_ep *_ep,
572 */ 616 */
573 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]); 617 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
574 if ((epctrlx & EPCTRL_RX_ENABLE) == 0) { 618 if ((epctrlx & EPCTRL_RX_ENABLE) == 0) {
575 epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 619 epctrlx |= (USB_ENDPOINT_XFER_BULK
576 << EPCTRL_RX_EP_TYPE_SHIFT); 620 << EPCTRL_RX_EP_TYPE_SHIFT);
577 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]); 621 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
578 } 622 }
579 623
580 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]); 624 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
581 if ((epctrlx & EPCTRL_TX_ENABLE) == 0) { 625 if ((epctrlx & EPCTRL_TX_ENABLE) == 0) {
582 epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 626 epctrlx |= (USB_ENDPOINT_XFER_BULK
583 << EPCTRL_TX_EP_TYPE_SHIFT); 627 << EPCTRL_TX_EP_TYPE_SHIFT);
584 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]); 628 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
585 } 629 }
586 630
631 spin_unlock_irqrestore(&udc->lock, flags);
632
587 return 0; 633 return 0;
588en_done: 634en_done:
589 return -EINVAL; 635 return -EINVAL;
@@ -595,6 +641,7 @@ static int mv_ep_disable(struct usb_ep *_ep)
595 struct mv_ep *ep; 641 struct mv_ep *ep;
596 struct mv_dqh *dqh; 642 struct mv_dqh *dqh;
597 u32 bit_pos, epctrlx, direction; 643 u32 bit_pos, epctrlx, direction;
644 unsigned long flags;
598 645
599 ep = container_of(_ep, struct mv_ep, ep); 646 ep = container_of(_ep, struct mv_ep, ep);
600 if ((_ep == NULL) || !ep->desc) 647 if ((_ep == NULL) || !ep->desc)
@@ -605,6 +652,8 @@ static int mv_ep_disable(struct usb_ep *_ep)
605 /* Get the endpoint queue head address */ 652 /* Get the endpoint queue head address */
606 dqh = ep->dqh; 653 dqh = ep->dqh;
607 654
655 spin_lock_irqsave(&udc->lock, flags);
656
608 direction = ep_dir(ep); 657 direction = ep_dir(ep);
609 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num); 658 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
610 659
@@ -623,6 +672,9 @@ static int mv_ep_disable(struct usb_ep *_ep)
623 672
624 ep->desc = NULL; 673 ep->desc = NULL;
625 ep->stopped = 1; 674 ep->stopped = 1;
675
676 spin_unlock_irqrestore(&udc->lock, flags);
677
626 return 0; 678 return 0;
627} 679}
628 680
@@ -655,37 +707,28 @@ static void mv_ep_fifo_flush(struct usb_ep *_ep)
655{ 707{
656 struct mv_udc *udc; 708 struct mv_udc *udc;
657 u32 bit_pos, direction; 709 u32 bit_pos, direction;
658 struct mv_ep *ep = container_of(_ep, struct mv_ep, ep); 710 struct mv_ep *ep;
659 unsigned int loops; 711 unsigned int loops;
660 712
713 if (!_ep)
714 return;
715
716 ep = container_of(_ep, struct mv_ep, ep);
717 if (!ep->desc)
718 return;
719
661 udc = ep->udc; 720 udc = ep->udc;
662 direction = ep_dir(ep); 721 direction = ep_dir(ep);
663 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
664 /*
665 * Flushing will halt the pipe
666 * Write 1 to the Flush register
667 */
668 writel(bit_pos, &udc->op_regs->epflush);
669 722
670 /* Wait until flushing completed */ 723 if (ep->ep_num == 0)
671 loops = LOOPS(FLUSH_TIMEOUT); 724 bit_pos = (1 << 16) | 1;
672 while (readl(&udc->op_regs->epflush) & bit_pos) { 725 else if (direction == EP_DIR_OUT)
673 /* 726 bit_pos = 1 << ep->ep_num;
674 * ENDPTFLUSH bit should be cleared to indicate this 727 else
675 * operation is complete 728 bit_pos = 1 << (16 + ep->ep_num);
676 */ 729
677 if (loops == 0) {
678 dev_err(&udc->dev->dev,
679 "TIMEOUT for ENDPTFLUSH=0x%x, bit_pos=0x%x\n",
680 (unsigned)readl(&udc->op_regs->epflush),
681 (unsigned)bit_pos);
682 return;
683 }
684 loops--;
685 udelay(LOOPS_USEC);
686 }
687 loops = LOOPS(EPSTATUS_TIMEOUT); 730 loops = LOOPS(EPSTATUS_TIMEOUT);
688 while (readl(&udc->op_regs->epstatus) & bit_pos) { 731 do {
689 unsigned int inter_loops; 732 unsigned int inter_loops;
690 733
691 if (loops == 0) { 734 if (loops == 0) {
@@ -700,7 +743,7 @@ static void mv_ep_fifo_flush(struct usb_ep *_ep)
700 743
701 /* Wait until flushing completed */ 744 /* Wait until flushing completed */
702 inter_loops = LOOPS(FLUSH_TIMEOUT); 745 inter_loops = LOOPS(FLUSH_TIMEOUT);
703 while (readl(&udc->op_regs->epflush) & bit_pos) { 746 while (readl(&udc->op_regs->epflush)) {
704 /* 747 /*
705 * ENDPTFLUSH bit should be cleared to indicate this 748 * ENDPTFLUSH bit should be cleared to indicate this
706 * operation is complete 749 * operation is complete
@@ -717,7 +760,7 @@ static void mv_ep_fifo_flush(struct usb_ep *_ep)
717 udelay(LOOPS_USEC); 760 udelay(LOOPS_USEC);
718 } 761 }
719 loops--; 762 loops--;
720 } 763 } while (readl(&udc->op_regs->epstatus) & bit_pos);
721} 764}
722 765
723/* queues (submits) an I/O request to an endpoint */ 766/* queues (submits) an I/O request to an endpoint */
@@ -987,6 +1030,22 @@ static struct usb_ep_ops mv_ep_ops = {
987 .fifo_flush = mv_ep_fifo_flush, /* flush fifo */ 1030 .fifo_flush = mv_ep_fifo_flush, /* flush fifo */
988}; 1031};
989 1032
1033static void udc_clock_enable(struct mv_udc *udc)
1034{
1035 unsigned int i;
1036
1037 for (i = 0; i < udc->clknum; i++)
1038 clk_enable(udc->clk[i]);
1039}
1040
1041static void udc_clock_disable(struct mv_udc *udc)
1042{
1043 unsigned int i;
1044
1045 for (i = 0; i < udc->clknum; i++)
1046 clk_disable(udc->clk[i]);
1047}
1048
990static void udc_stop(struct mv_udc *udc) 1049static void udc_stop(struct mv_udc *udc)
991{ 1050{
992 u32 tmp; 1051 u32 tmp;
@@ -1075,6 +1134,40 @@ static int udc_reset(struct mv_udc *udc)
1075 return 0; 1134 return 0;
1076} 1135}
1077 1136
1137static int mv_udc_enable(struct mv_udc *udc)
1138{
1139 int retval;
1140
1141 if (udc->clock_gating == 0 || udc->active)
1142 return 0;
1143
1144 dev_dbg(&udc->dev->dev, "enable udc\n");
1145 udc_clock_enable(udc);
1146 if (udc->pdata->phy_init) {
1147 retval = udc->pdata->phy_init(udc->phy_regs);
1148 if (retval) {
1149 dev_err(&udc->dev->dev,
1150 "init phy error %d\n", retval);
1151 udc_clock_disable(udc);
1152 return retval;
1153 }
1154 }
1155 udc->active = 1;
1156
1157 return 0;
1158}
1159
1160static void mv_udc_disable(struct mv_udc *udc)
1161{
1162 if (udc->clock_gating && udc->active) {
1163 dev_dbg(&udc->dev->dev, "disable udc\n");
1164 if (udc->pdata->phy_deinit)
1165 udc->pdata->phy_deinit(udc->phy_regs);
1166 udc_clock_disable(udc);
1167 udc->active = 0;
1168 }
1169}
1170
1078static int mv_udc_get_frame(struct usb_gadget *gadget) 1171static int mv_udc_get_frame(struct usb_gadget *gadget)
1079{ 1172{
1080 struct mv_udc *udc; 1173 struct mv_udc *udc;
@@ -1110,22 +1203,68 @@ static int mv_udc_wakeup(struct usb_gadget *gadget)
1110 return 0; 1203 return 0;
1111} 1204}
1112 1205
1206static int mv_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1207{
1208 struct mv_udc *udc;
1209 unsigned long flags;
1210 int retval = 0;
1211
1212 udc = container_of(gadget, struct mv_udc, gadget);
1213 spin_lock_irqsave(&udc->lock, flags);
1214
1215 dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
1216 __func__, udc->softconnect, udc->vbus_active);
1217
1218 udc->vbus_active = (is_active != 0);
1219 if (udc->driver && udc->softconnect && udc->vbus_active) {
1220 retval = mv_udc_enable(udc);
1221 if (retval == 0) {
1222 /* Clock is disabled, need re-init registers */
1223 udc_reset(udc);
1224 ep0_reset(udc);
1225 udc_start(udc);
1226 }
1227 } else if (udc->driver && udc->softconnect) {
1228 /* stop all the transfer in queue*/
1229 stop_activity(udc, udc->driver);
1230 udc_stop(udc);
1231 mv_udc_disable(udc);
1232 }
1233
1234 spin_unlock_irqrestore(&udc->lock, flags);
1235 return retval;
1236}
1237
1113static int mv_udc_pullup(struct usb_gadget *gadget, int is_on) 1238static int mv_udc_pullup(struct usb_gadget *gadget, int is_on)
1114{ 1239{
1115 struct mv_udc *udc; 1240 struct mv_udc *udc;
1116 unsigned long flags; 1241 unsigned long flags;
1242 int retval = 0;
1117 1243
1118 udc = container_of(gadget, struct mv_udc, gadget); 1244 udc = container_of(gadget, struct mv_udc, gadget);
1119 spin_lock_irqsave(&udc->lock, flags); 1245 spin_lock_irqsave(&udc->lock, flags);
1120 1246
1247 dev_dbg(&udc->dev->dev, "%s: softconnect %d, vbus_active %d\n",
1248 __func__, udc->softconnect, udc->vbus_active);
1249
1121 udc->softconnect = (is_on != 0); 1250 udc->softconnect = (is_on != 0);
1122 if (udc->driver && udc->softconnect) 1251 if (udc->driver && udc->softconnect && udc->vbus_active) {
1123 udc_start(udc); 1252 retval = mv_udc_enable(udc);
1124 else 1253 if (retval == 0) {
1254 /* Clock is disabled, need re-init registers */
1255 udc_reset(udc);
1256 ep0_reset(udc);
1257 udc_start(udc);
1258 }
1259 } else if (udc->driver && udc->vbus_active) {
1260 /* stop all the transfer in queue*/
1261 stop_activity(udc, udc->driver);
1125 udc_stop(udc); 1262 udc_stop(udc);
1263 mv_udc_disable(udc);
1264 }
1126 1265
1127 spin_unlock_irqrestore(&udc->lock, flags); 1266 spin_unlock_irqrestore(&udc->lock, flags);
1128 return 0; 1267 return retval;
1129} 1268}
1130 1269
1131static int mv_udc_start(struct usb_gadget_driver *driver, 1270static int mv_udc_start(struct usb_gadget_driver *driver,
@@ -1140,17 +1279,15 @@ static const struct usb_gadget_ops mv_ops = {
1140 /* tries to wake up the host connected to this gadget */ 1279 /* tries to wake up the host connected to this gadget */
1141 .wakeup = mv_udc_wakeup, 1280 .wakeup = mv_udc_wakeup,
1142 1281
1282 /* notify controller that VBUS is powered or not */
1283 .vbus_session = mv_udc_vbus_session,
1284
1143 /* D+ pullup, software-controlled connect/disconnect to USB host */ 1285 /* D+ pullup, software-controlled connect/disconnect to USB host */
1144 .pullup = mv_udc_pullup, 1286 .pullup = mv_udc_pullup,
1145 .start = mv_udc_start, 1287 .start = mv_udc_start,
1146 .stop = mv_udc_stop, 1288 .stop = mv_udc_stop,
1147}; 1289};
1148 1290
1149static void mv_udc_testmode(struct mv_udc *udc, u16 index, bool enter)
1150{
1151 dev_info(&udc->dev->dev, "Test Mode is not support yet\n");
1152}
1153
1154static int eps_init(struct mv_udc *udc) 1291static int eps_init(struct mv_udc *udc)
1155{ 1292{
1156 struct mv_ep *ep; 1293 struct mv_ep *ep;
@@ -1257,7 +1394,7 @@ static int mv_udc_start(struct usb_gadget_driver *driver,
1257 1394
1258 udc->usb_state = USB_STATE_ATTACHED; 1395 udc->usb_state = USB_STATE_ATTACHED;
1259 udc->ep0_state = WAIT_FOR_SETUP; 1396 udc->ep0_state = WAIT_FOR_SETUP;
1260 udc->ep0_dir = USB_DIR_OUT; 1397 udc->ep0_dir = EP_DIR_OUT;
1261 1398
1262 spin_unlock_irqrestore(&udc->lock, flags); 1399 spin_unlock_irqrestore(&udc->lock, flags);
1263 1400
@@ -1269,9 +1406,13 @@ static int mv_udc_start(struct usb_gadget_driver *driver,
1269 udc->gadget.dev.driver = NULL; 1406 udc->gadget.dev.driver = NULL;
1270 return retval; 1407 return retval;
1271 } 1408 }
1272 udc_reset(udc); 1409
1273 ep0_reset(udc); 1410 /* pullup is always on */
1274 udc_start(udc); 1411 mv_udc_pullup(&udc->gadget, 1);
1412
1413 /* When boot with cable attached, there will be no vbus irq occurred */
1414 if (udc->qwork)
1415 queue_work(udc->qwork, &udc->vbus_work);
1275 1416
1276 return 0; 1417 return 0;
1277} 1418}
@@ -1284,13 +1425,16 @@ static int mv_udc_stop(struct usb_gadget_driver *driver)
1284 if (!udc) 1425 if (!udc)
1285 return -ENODEV; 1426 return -ENODEV;
1286 1427
1287 udc_stop(udc);
1288
1289 spin_lock_irqsave(&udc->lock, flags); 1428 spin_lock_irqsave(&udc->lock, flags);
1290 1429
1430 mv_udc_enable(udc);
1431 udc_stop(udc);
1432
1291 /* stop all usb activities */ 1433 /* stop all usb activities */
1292 udc->gadget.speed = USB_SPEED_UNKNOWN; 1434 udc->gadget.speed = USB_SPEED_UNKNOWN;
1293 stop_activity(udc, driver); 1435 stop_activity(udc, driver);
1436 mv_udc_disable(udc);
1437
1294 spin_unlock_irqrestore(&udc->lock, flags); 1438 spin_unlock_irqrestore(&udc->lock, flags);
1295 1439
1296 /* unbind gadget driver */ 1440 /* unbind gadget driver */
@@ -1301,6 +1445,31 @@ static int mv_udc_stop(struct usb_gadget_driver *driver)
1301 return 0; 1445 return 0;
1302} 1446}
1303 1447
1448static void mv_set_ptc(struct mv_udc *udc, u32 mode)
1449{
1450 u32 portsc;
1451
1452 portsc = readl(&udc->op_regs->portsc[0]);
1453 portsc |= mode << 16;
1454 writel(portsc, &udc->op_regs->portsc[0]);
1455}
1456
1457static void prime_status_complete(struct usb_ep *ep, struct usb_request *_req)
1458{
1459 struct mv_udc *udc = the_controller;
1460 struct mv_req *req = container_of(_req, struct mv_req, req);
1461 unsigned long flags;
1462
1463 dev_info(&udc->dev->dev, "switch to test mode %d\n", req->test_mode);
1464
1465 spin_lock_irqsave(&udc->lock, flags);
1466 if (req->test_mode) {
1467 mv_set_ptc(udc, req->test_mode);
1468 req->test_mode = 0;
1469 }
1470 spin_unlock_irqrestore(&udc->lock, flags);
1471}
1472
1304static int 1473static int
1305udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty) 1474udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
1306{ 1475{
@@ -1310,6 +1479,7 @@ udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
1310 1479
1311 ep = &udc->eps[0]; 1480 ep = &udc->eps[0];
1312 udc->ep0_dir = direction; 1481 udc->ep0_dir = direction;
1482 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1313 1483
1314 req = udc->status_req; 1484 req = udc->status_req;
1315 1485
@@ -1323,9 +1493,21 @@ udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
1323 req->ep = ep; 1493 req->ep = ep;
1324 req->req.status = -EINPROGRESS; 1494 req->req.status = -EINPROGRESS;
1325 req->req.actual = 0; 1495 req->req.actual = 0;
1326 req->req.complete = NULL; 1496 if (udc->test_mode) {
1497 req->req.complete = prime_status_complete;
1498 req->test_mode = udc->test_mode;
1499 udc->test_mode = 0;
1500 } else
1501 req->req.complete = NULL;
1327 req->dtd_count = 0; 1502 req->dtd_count = 0;
1328 1503
1504 if (req->req.dma == DMA_ADDR_INVALID) {
1505 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1506 req->req.buf, req->req.length,
1507 ep_dir(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1508 req->mapped = 1;
1509 }
1510
1329 /* prime the data phase */ 1511 /* prime the data phase */
1330 if (!req_to_dtd(req)) 1512 if (!req_to_dtd(req))
1331 retval = queue_dtd(ep, req); 1513 retval = queue_dtd(ep, req);
@@ -1346,6 +1528,17 @@ out:
1346 return retval; 1528 return retval;
1347} 1529}
1348 1530
1531static void mv_udc_testmode(struct mv_udc *udc, u16 index)
1532{
1533 if (index <= TEST_FORCE_EN) {
1534 udc->test_mode = index;
1535 if (udc_prime_status(udc, EP_DIR_IN, 0, true))
1536 ep0_stall(udc);
1537 } else
1538 dev_err(&udc->dev->dev,
1539 "This test mode(%d) is not supported\n", index);
1540}
1541
1349static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup) 1542static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1350{ 1543{
1351 udc->dev_addr = (u8)setup->wValue; 1544 udc->dev_addr = (u8)setup->wValue;
@@ -1360,7 +1553,7 @@ static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1360static void ch9getstatus(struct mv_udc *udc, u8 ep_num, 1553static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
1361 struct usb_ctrlrequest *setup) 1554 struct usb_ctrlrequest *setup)
1362{ 1555{
1363 u16 status; 1556 u16 status = 0;
1364 int retval; 1557 int retval;
1365 1558
1366 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) 1559 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
@@ -1388,6 +1581,8 @@ static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
1388 retval = udc_prime_status(udc, EP_DIR_IN, status, false); 1581 retval = udc_prime_status(udc, EP_DIR_IN, status, false);
1389 if (retval) 1582 if (retval)
1390 ep0_stall(udc); 1583 ep0_stall(udc);
1584 else
1585 udc->ep0_state = DATA_STATE_XMIT;
1391} 1586}
1392 1587
1393static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup) 1588static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
@@ -1402,9 +1597,6 @@ static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1402 case USB_DEVICE_REMOTE_WAKEUP: 1597 case USB_DEVICE_REMOTE_WAKEUP:
1403 udc->remote_wakeup = 0; 1598 udc->remote_wakeup = 0;
1404 break; 1599 break;
1405 case USB_DEVICE_TEST_MODE:
1406 mv_udc_testmode(udc, 0, false);
1407 break;
1408 default: 1600 default:
1409 goto out; 1601 goto out;
1410 } 1602 }
@@ -1433,8 +1625,6 @@ static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1433 1625
1434 if (udc_prime_status(udc, EP_DIR_IN, 0, true)) 1626 if (udc_prime_status(udc, EP_DIR_IN, 0, true))
1435 ep0_stall(udc); 1627 ep0_stall(udc);
1436 else
1437 udc->ep0_state = DATA_STATE_XMIT;
1438out: 1628out:
1439 return; 1629 return;
1440} 1630}
@@ -1452,16 +1642,16 @@ static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1452 break; 1642 break;
1453 case USB_DEVICE_TEST_MODE: 1643 case USB_DEVICE_TEST_MODE:
1454 if (setup->wIndex & 0xFF 1644 if (setup->wIndex & 0xFF
1455 && udc->gadget.speed != USB_SPEED_HIGH) 1645 || udc->gadget.speed != USB_SPEED_HIGH)
1456 goto out; 1646 ep0_stall(udc);
1457 if (udc->usb_state == USB_STATE_CONFIGURED 1647
1458 || udc->usb_state == USB_STATE_ADDRESS 1648 if (udc->usb_state != USB_STATE_CONFIGURED
1459 || udc->usb_state == USB_STATE_DEFAULT) 1649 && udc->usb_state != USB_STATE_ADDRESS
1460 mv_udc_testmode(udc, 1650 && udc->usb_state != USB_STATE_DEFAULT)
1461 setup->wIndex & 0xFF00, true); 1651 ep0_stall(udc);
1462 else 1652
1463 goto out; 1653 mv_udc_testmode(udc, (setup->wIndex >> 8));
1464 break; 1654 goto out;
1465 default: 1655 default:
1466 goto out; 1656 goto out;
1467 } 1657 }
@@ -1599,8 +1789,7 @@ static void get_setup_data(struct mv_udc *udc, u8 ep_num, u8 *buffer_ptr)
1599 dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT]; 1789 dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT];
1600 1790
1601 /* Clear bit in ENDPTSETUPSTAT */ 1791 /* Clear bit in ENDPTSETUPSTAT */
1602 temp = readl(&udc->op_regs->epsetupstat); 1792 writel((1 << ep_num), &udc->op_regs->epsetupstat);
1603 writel(temp | (1 << ep_num), &udc->op_regs->epsetupstat);
1604 1793
1605 /* while a hazard exists when setup package arrives */ 1794 /* while a hazard exists when setup package arrives */
1606 do { 1795 do {
@@ -1871,23 +2060,57 @@ static irqreturn_t mv_udc_irq(int irq, void *dev)
1871 return IRQ_HANDLED; 2060 return IRQ_HANDLED;
1872} 2061}
1873 2062
2063static irqreturn_t mv_udc_vbus_irq(int irq, void *dev)
2064{
2065 struct mv_udc *udc = (struct mv_udc *)dev;
2066
2067 /* polling VBUS and init phy may cause too much time*/
2068 if (udc->qwork)
2069 queue_work(udc->qwork, &udc->vbus_work);
2070
2071 return IRQ_HANDLED;
2072}
2073
2074static void mv_udc_vbus_work(struct work_struct *work)
2075{
2076 struct mv_udc *udc;
2077 unsigned int vbus;
2078
2079 udc = container_of(work, struct mv_udc, vbus_work);
2080 if (!udc->pdata->vbus)
2081 return;
2082
2083 vbus = udc->pdata->vbus->poll();
2084 dev_info(&udc->dev->dev, "vbus is %d\n", vbus);
2085
2086 if (vbus == VBUS_HIGH)
2087 mv_udc_vbus_session(&udc->gadget, 1);
2088 else if (vbus == VBUS_LOW)
2089 mv_udc_vbus_session(&udc->gadget, 0);
2090}
2091
1874/* release device structure */ 2092/* release device structure */
1875static void gadget_release(struct device *_dev) 2093static void gadget_release(struct device *_dev)
1876{ 2094{
1877 struct mv_udc *udc = the_controller; 2095 struct mv_udc *udc = the_controller;
1878 2096
1879 complete(udc->done); 2097 complete(udc->done);
1880 kfree(udc);
1881} 2098}
1882 2099
1883static int mv_udc_remove(struct platform_device *dev) 2100static int __devexit mv_udc_remove(struct platform_device *dev)
1884{ 2101{
1885 struct mv_udc *udc = the_controller; 2102 struct mv_udc *udc = the_controller;
1886 DECLARE_COMPLETION(done); 2103 int clk_i;
1887 2104
1888 usb_del_gadget_udc(&udc->gadget); 2105 usb_del_gadget_udc(&udc->gadget);
1889 2106
1890 udc->done = &done; 2107 if (udc->qwork) {
2108 flush_workqueue(udc->qwork);
2109 destroy_workqueue(udc->qwork);
2110 }
2111
2112 if (udc->pdata && udc->pdata->vbus && udc->clock_gating)
2113 free_irq(udc->pdata->vbus->irq, &dev->dev);
1891 2114
1892 /* free memory allocated in probe */ 2115 /* free memory allocated in probe */
1893 if (udc->dtd_pool) 2116 if (udc->dtd_pool)
@@ -1902,6 +2125,8 @@ static int mv_udc_remove(struct platform_device *dev)
1902 if (udc->irq) 2125 if (udc->irq)
1903 free_irq(udc->irq, &dev->dev); 2126 free_irq(udc->irq, &dev->dev);
1904 2127
2128 mv_udc_disable(udc);
2129
1905 if (udc->cap_regs) 2130 if (udc->cap_regs)
1906 iounmap(udc->cap_regs); 2131 iounmap(udc->cap_regs);
1907 udc->cap_regs = NULL; 2132 udc->cap_regs = NULL;
@@ -1915,45 +2140,62 @@ static int mv_udc_remove(struct platform_device *dev)
1915 kfree(udc->status_req); 2140 kfree(udc->status_req);
1916 } 2141 }
1917 2142
2143 for (clk_i = 0; clk_i <= udc->clknum; clk_i++)
2144 clk_put(udc->clk[clk_i]);
2145
1918 device_unregister(&udc->gadget.dev); 2146 device_unregister(&udc->gadget.dev);
1919 2147
1920 /* free dev, wait for the release() finished */ 2148 /* free dev, wait for the release() finished */
1921 wait_for_completion(&done); 2149 wait_for_completion(udc->done);
2150 kfree(udc);
1922 2151
1923 the_controller = NULL; 2152 the_controller = NULL;
1924 2153
1925 return 0; 2154 return 0;
1926} 2155}
1927 2156
1928int mv_udc_probe(struct platform_device *dev) 2157static int __devinit mv_udc_probe(struct platform_device *dev)
1929{ 2158{
2159 struct mv_usb_platform_data *pdata = dev->dev.platform_data;
1930 struct mv_udc *udc; 2160 struct mv_udc *udc;
1931 int retval = 0; 2161 int retval = 0;
2162 int clk_i = 0;
1932 struct resource *r; 2163 struct resource *r;
1933 size_t size; 2164 size_t size;
1934 2165
1935 udc = kzalloc(sizeof *udc, GFP_KERNEL); 2166 if (pdata == NULL) {
2167 dev_err(&dev->dev, "missing platform_data\n");
2168 return -ENODEV;
2169 }
2170
2171 size = sizeof(*udc) + sizeof(struct clk *) * pdata->clknum;
2172 udc = kzalloc(size, GFP_KERNEL);
1936 if (udc == NULL) { 2173 if (udc == NULL) {
1937 dev_err(&dev->dev, "failed to allocate memory for udc\n"); 2174 dev_err(&dev->dev, "failed to allocate memory for udc\n");
1938 retval = -ENOMEM; 2175 return -ENOMEM;
1939 goto error;
1940 } 2176 }
1941 2177
2178 the_controller = udc;
2179 udc->done = &release_done;
2180 udc->pdata = dev->dev.platform_data;
1942 spin_lock_init(&udc->lock); 2181 spin_lock_init(&udc->lock);
1943 2182
1944 udc->dev = dev; 2183 udc->dev = dev;
1945 2184
1946 udc->clk = clk_get(&dev->dev, "U2OCLK"); 2185 udc->clknum = pdata->clknum;
1947 if (IS_ERR(udc->clk)) { 2186 for (clk_i = 0; clk_i < udc->clknum; clk_i++) {
1948 retval = PTR_ERR(udc->clk); 2187 udc->clk[clk_i] = clk_get(&dev->dev, pdata->clkname[clk_i]);
1949 goto error; 2188 if (IS_ERR(udc->clk[clk_i])) {
2189 retval = PTR_ERR(udc->clk[clk_i]);
2190 goto err_put_clk;
2191 }
1950 } 2192 }
1951 2193
1952 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2o"); 2194 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "capregs");
1953 if (r == NULL) { 2195 if (r == NULL) {
1954 dev_err(&dev->dev, "no I/O memory resource defined\n"); 2196 dev_err(&dev->dev, "no I/O memory resource defined\n");
1955 retval = -ENODEV; 2197 retval = -ENODEV;
1956 goto error; 2198 goto err_put_clk;
1957 } 2199 }
1958 2200
1959 udc->cap_regs = (struct mv_cap_regs __iomem *) 2201 udc->cap_regs = (struct mv_cap_regs __iomem *)
@@ -1961,29 +2203,31 @@ int mv_udc_probe(struct platform_device *dev)
1961 if (udc->cap_regs == NULL) { 2203 if (udc->cap_regs == NULL) {
1962 dev_err(&dev->dev, "failed to map I/O memory\n"); 2204 dev_err(&dev->dev, "failed to map I/O memory\n");
1963 retval = -EBUSY; 2205 retval = -EBUSY;
1964 goto error; 2206 goto err_put_clk;
1965 } 2207 }
1966 2208
1967 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2ophy"); 2209 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "phyregs");
1968 if (r == NULL) { 2210 if (r == NULL) {
1969 dev_err(&dev->dev, "no phy I/O memory resource defined\n"); 2211 dev_err(&dev->dev, "no phy I/O memory resource defined\n");
1970 retval = -ENODEV; 2212 retval = -ENODEV;
1971 goto error; 2213 goto err_iounmap_capreg;
1972 } 2214 }
1973 2215
1974 udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r)); 2216 udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r));
1975 if (udc->phy_regs == 0) { 2217 if (udc->phy_regs == 0) {
1976 dev_err(&dev->dev, "failed to map phy I/O memory\n"); 2218 dev_err(&dev->dev, "failed to map phy I/O memory\n");
1977 retval = -EBUSY; 2219 retval = -EBUSY;
1978 goto error; 2220 goto err_iounmap_capreg;
1979 } 2221 }
1980 2222
1981 /* we will acces controller register, so enable the clk */ 2223 /* we will acces controller register, so enable the clk */
1982 clk_enable(udc->clk); 2224 udc_clock_enable(udc);
1983 retval = mv_udc_phy_init(udc->phy_regs); 2225 if (pdata->phy_init) {
1984 if (retval) { 2226 retval = pdata->phy_init(udc->phy_regs);
1985 dev_err(&dev->dev, "phy initialization error %d\n", retval); 2227 if (retval) {
1986 goto error; 2228 dev_err(&dev->dev, "phy init error %d\n", retval);
2229 goto err_iounmap_phyreg;
2230 }
1987 } 2231 }
1988 2232
1989 udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs 2233 udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs
@@ -1991,6 +2235,13 @@ int mv_udc_probe(struct platform_device *dev)
1991 & CAPLENGTH_MASK)); 2235 & CAPLENGTH_MASK));
1992 udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK; 2236 udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
1993 2237
2238 /*
2239 * some platform will use usb to download image, it may not disconnect
2240 * usb gadget before loading kernel. So first stop udc here.
2241 */
2242 udc_stop(udc);
2243 writel(0xFFFFFFFF, &udc->op_regs->usbsts);
2244
1994 size = udc->max_eps * sizeof(struct mv_dqh) *2; 2245 size = udc->max_eps * sizeof(struct mv_dqh) *2;
1995 size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1); 2246 size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
1996 udc->ep_dqh = dma_alloc_coherent(&dev->dev, size, 2247 udc->ep_dqh = dma_alloc_coherent(&dev->dev, size,
@@ -1999,7 +2250,7 @@ int mv_udc_probe(struct platform_device *dev)
1999 if (udc->ep_dqh == NULL) { 2250 if (udc->ep_dqh == NULL) {
2000 dev_err(&dev->dev, "allocate dQH memory failed\n"); 2251 dev_err(&dev->dev, "allocate dQH memory failed\n");
2001 retval = -ENOMEM; 2252 retval = -ENOMEM;
2002 goto error; 2253 goto err_disable_clock;
2003 } 2254 }
2004 udc->ep_dqh_size = size; 2255 udc->ep_dqh_size = size;
2005 2256
@@ -2012,7 +2263,7 @@ int mv_udc_probe(struct platform_device *dev)
2012 2263
2013 if (!udc->dtd_pool) { 2264 if (!udc->dtd_pool) {
2014 retval = -ENOMEM; 2265 retval = -ENOMEM;
2015 goto error; 2266 goto err_free_dma;
2016 } 2267 }
2017 2268
2018 size = udc->max_eps * sizeof(struct mv_ep) *2; 2269 size = udc->max_eps * sizeof(struct mv_ep) *2;
@@ -2020,7 +2271,7 @@ int mv_udc_probe(struct platform_device *dev)
2020 if (udc->eps == NULL) { 2271 if (udc->eps == NULL) {
2021 dev_err(&dev->dev, "allocate ep memory failed\n"); 2272 dev_err(&dev->dev, "allocate ep memory failed\n");
2022 retval = -ENOMEM; 2273 retval = -ENOMEM;
2023 goto error; 2274 goto err_destroy_dma;
2024 } 2275 }
2025 2276
2026 /* initialize ep0 status request structure */ 2277 /* initialize ep0 status request structure */
@@ -2028,13 +2279,13 @@ int mv_udc_probe(struct platform_device *dev)
2028 if (!udc->status_req) { 2279 if (!udc->status_req) {
2029 dev_err(&dev->dev, "allocate status_req memory failed\n"); 2280 dev_err(&dev->dev, "allocate status_req memory failed\n");
2030 retval = -ENOMEM; 2281 retval = -ENOMEM;
2031 goto error; 2282 goto err_free_eps;
2032 } 2283 }
2033 INIT_LIST_HEAD(&udc->status_req->queue); 2284 INIT_LIST_HEAD(&udc->status_req->queue);
2034 2285
2035 /* allocate a small amount of memory to get valid address */ 2286 /* allocate a small amount of memory to get valid address */
2036 udc->status_req->req.buf = kzalloc(8, GFP_KERNEL); 2287 udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
2037 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf); 2288 udc->status_req->req.dma = DMA_ADDR_INVALID;
2038 2289
2039 udc->resume_state = USB_STATE_NOTATTACHED; 2290 udc->resume_state = USB_STATE_NOTATTACHED;
2040 udc->usb_state = USB_STATE_POWERED; 2291 udc->usb_state = USB_STATE_POWERED;
@@ -2045,15 +2296,15 @@ int mv_udc_probe(struct platform_device *dev)
2045 if (r == NULL) { 2296 if (r == NULL) {
2046 dev_err(&dev->dev, "no IRQ resource defined\n"); 2297 dev_err(&dev->dev, "no IRQ resource defined\n");
2047 retval = -ENODEV; 2298 retval = -ENODEV;
2048 goto error; 2299 goto err_free_status_req;
2049 } 2300 }
2050 udc->irq = r->start; 2301 udc->irq = r->start;
2051 if (request_irq(udc->irq, mv_udc_irq, 2302 if (request_irq(udc->irq, mv_udc_irq,
2052 IRQF_DISABLED | IRQF_SHARED, driver_name, udc)) { 2303 IRQF_SHARED, driver_name, udc)) {
2053 dev_err(&dev->dev, "Request irq %d for UDC failed\n", 2304 dev_err(&dev->dev, "Request irq %d for UDC failed\n",
2054 udc->irq); 2305 udc->irq);
2055 retval = -ENODEV; 2306 retval = -ENODEV;
2056 goto error; 2307 goto err_free_status_req;
2057 } 2308 }
2058 2309
2059 /* initialize gadget structure */ 2310 /* initialize gadget structure */
@@ -2072,18 +2323,82 @@ int mv_udc_probe(struct platform_device *dev)
2072 2323
2073 retval = device_register(&udc->gadget.dev); 2324 retval = device_register(&udc->gadget.dev);
2074 if (retval) 2325 if (retval)
2075 goto error; 2326 goto err_free_irq;
2076 2327
2077 eps_init(udc); 2328 eps_init(udc);
2078 2329
2079 the_controller = udc; 2330 /* VBUS detect: we can disable/enable clock on demand.*/
2331 if (pdata->vbus) {
2332 udc->clock_gating = 1;
2333 retval = request_threaded_irq(pdata->vbus->irq, NULL,
2334 mv_udc_vbus_irq, IRQF_ONESHOT, "vbus", udc);
2335 if (retval) {
2336 dev_info(&dev->dev,
2337 "Can not request irq for VBUS, "
2338 "disable clock gating\n");
2339 udc->clock_gating = 0;
2340 }
2341
2342 udc->qwork = create_singlethread_workqueue("mv_udc_queue");
2343 if (!udc->qwork) {
2344 dev_err(&dev->dev, "cannot create workqueue\n");
2345 retval = -ENOMEM;
2346 goto err_unregister;
2347 }
2348
2349 INIT_WORK(&udc->vbus_work, mv_udc_vbus_work);
2350 }
2351
2352 /*
2353 * When clock gating is supported, we can disable clk and phy.
2354 * If not, it means that VBUS detection is not supported, we
2355 * have to enable vbus active all the time to let controller work.
2356 */
2357 if (udc->clock_gating) {
2358 if (udc->pdata->phy_deinit)
2359 udc->pdata->phy_deinit(udc->phy_regs);
2360 udc_clock_disable(udc);
2361 } else
2362 udc->vbus_active = 1;
2080 2363
2081 retval = usb_add_gadget_udc(&dev->dev, &udc->gadget); 2364 retval = usb_add_gadget_udc(&dev->dev, &udc->gadget);
2082 if (!retval) 2365 if (retval)
2083 return retval; 2366 goto err_unregister;
2084error: 2367
2085 if (udc) 2368 dev_info(&dev->dev, "successful probe UDC device %s clock gating.\n",
2086 mv_udc_remove(udc->dev); 2369 udc->clock_gating ? "with" : "without");
2370
2371 return 0;
2372
2373err_unregister:
2374 if (udc->pdata && udc->pdata->vbus && udc->clock_gating)
2375 free_irq(pdata->vbus->irq, &dev->dev);
2376 device_unregister(&udc->gadget.dev);
2377err_free_irq:
2378 free_irq(udc->irq, &dev->dev);
2379err_free_status_req:
2380 kfree(udc->status_req->req.buf);
2381 kfree(udc->status_req);
2382err_free_eps:
2383 kfree(udc->eps);
2384err_destroy_dma:
2385 dma_pool_destroy(udc->dtd_pool);
2386err_free_dma:
2387 dma_free_coherent(&dev->dev, udc->ep_dqh_size,
2388 udc->ep_dqh, udc->ep_dqh_dma);
2389err_disable_clock:
2390 if (udc->pdata->phy_deinit)
2391 udc->pdata->phy_deinit(udc->phy_regs);
2392 udc_clock_disable(udc);
2393err_iounmap_phyreg:
2394 iounmap((void *)udc->phy_regs);
2395err_iounmap_capreg:
2396 iounmap(udc->cap_regs);
2397err_put_clk:
2398 for (clk_i--; clk_i >= 0; clk_i--)
2399 clk_put(udc->clk[clk_i]);
2400 the_controller = NULL;
2401 kfree(udc);
2087 return retval; 2402 return retval;
2088} 2403}
2089 2404
@@ -2102,11 +2417,16 @@ static int mv_udc_resume(struct device *_dev)
2102 struct mv_udc *udc = the_controller; 2417 struct mv_udc *udc = the_controller;
2103 int retval; 2418 int retval;
2104 2419
2105 retval = mv_udc_phy_init(udc->phy_regs); 2420 if (udc->pdata->phy_init) {
2106 if (retval) { 2421 retval = udc->pdata->phy_init(udc->phy_regs);
2107 dev_err(_dev, "phy initialization error %d\n", retval); 2422 if (retval) {
2108 return retval; 2423 dev_err(&udc->dev->dev,
2424 "init phy error %d when resume back\n",
2425 retval);
2426 return retval;
2427 }
2109 } 2428 }
2429
2110 udc_reset(udc); 2430 udc_reset(udc);
2111 ep0_reset(udc); 2431 ep0_reset(udc);
2112 udc_start(udc); 2432 udc_start(udc);
@@ -2120,9 +2440,21 @@ static const struct dev_pm_ops mv_udc_pm_ops = {
2120}; 2440};
2121#endif 2441#endif
2122 2442
2443static void mv_udc_shutdown(struct platform_device *dev)
2444{
2445 struct mv_udc *udc = the_controller;
2446 u32 mode;
2447
2448 /* reset controller mode to IDLE */
2449 mode = readl(&udc->op_regs->usbmode);
2450 mode &= ~3;
2451 writel(mode, &udc->op_regs->usbmode);
2452}
2453
2123static struct platform_driver udc_driver = { 2454static struct platform_driver udc_driver = {
2124 .probe = mv_udc_probe, 2455 .probe = mv_udc_probe,
2125 .remove = __exit_p(mv_udc_remove), 2456 .remove = __exit_p(mv_udc_remove),
2457 .shutdown = mv_udc_shutdown,
2126 .driver = { 2458 .driver = {
2127 .owner = THIS_MODULE, 2459 .owner = THIS_MODULE,
2128 .name = "pxa-u2o", 2460 .name = "pxa-u2o",
diff --git a/drivers/usb/gadget/mv_udc_phy.c b/drivers/usb/gadget/mv_udc_phy.c
deleted file mode 100644
index d4dea97e38a5..000000000000
--- a/drivers/usb/gadget/mv_udc_phy.c
+++ /dev/null
@@ -1,214 +0,0 @@
1#include <linux/delay.h>
2#include <linux/timer.h>
3#include <linux/io.h>
4#include <linux/errno.h>
5
6#include <mach/cputype.h>
7
8#ifdef CONFIG_ARCH_MMP
9
10#define UTMI_REVISION 0x0
11#define UTMI_CTRL 0x4
12#define UTMI_PLL 0x8
13#define UTMI_TX 0xc
14#define UTMI_RX 0x10
15#define UTMI_IVREF 0x14
16#define UTMI_T0 0x18
17#define UTMI_T1 0x1c
18#define UTMI_T2 0x20
19#define UTMI_T3 0x24
20#define UTMI_T4 0x28
21#define UTMI_T5 0x2c
22#define UTMI_RESERVE 0x30
23#define UTMI_USB_INT 0x34
24#define UTMI_DBG_CTL 0x38
25#define UTMI_OTG_ADDON 0x3c
26
27/* For UTMICTRL Register */
28#define UTMI_CTRL_USB_CLK_EN (1 << 31)
29/* pxa168 */
30#define UTMI_CTRL_SUSPEND_SET1 (1 << 30)
31#define UTMI_CTRL_SUSPEND_SET2 (1 << 29)
32#define UTMI_CTRL_RXBUF_PDWN (1 << 24)
33#define UTMI_CTRL_TXBUF_PDWN (1 << 11)
34
35#define UTMI_CTRL_INPKT_DELAY_SHIFT 30
36#define UTMI_CTRL_INPKT_DELAY_SOF_SHIFT 28
37#define UTMI_CTRL_PU_REF_SHIFT 20
38#define UTMI_CTRL_ARC_PULLDN_SHIFT 12
39#define UTMI_CTRL_PLL_PWR_UP_SHIFT 1
40#define UTMI_CTRL_PWR_UP_SHIFT 0
41/* For UTMI_PLL Register */
42#define UTMI_PLL_CLK_BLK_EN_SHIFT 24
43#define UTMI_PLL_FBDIV_SHIFT 4
44#define UTMI_PLL_REFDIV_SHIFT 0
45#define UTMI_PLL_FBDIV_MASK 0x00000FF0
46#define UTMI_PLL_REFDIV_MASK 0x0000000F
47#define UTMI_PLL_ICP_MASK 0x00007000
48#define UTMI_PLL_KVCO_MASK 0x00031000
49#define UTMI_PLL_PLLCALI12_SHIFT 29
50#define UTMI_PLL_PLLCALI12_MASK (0x3 << 29)
51#define UTMI_PLL_PLLVDD18_SHIFT 27
52#define UTMI_PLL_PLLVDD18_MASK (0x3 << 27)
53#define UTMI_PLL_PLLVDD12_SHIFT 25
54#define UTMI_PLL_PLLVDD12_MASK (0x3 << 25)
55#define UTMI_PLL_KVCO_SHIFT 15
56#define UTMI_PLL_ICP_SHIFT 12
57/* For UTMI_TX Register */
58#define UTMI_TX_REG_EXT_FS_RCAL_SHIFT 27
59#define UTMI_TX_REG_EXT_FS_RCAL_MASK (0xf << 27)
60#define UTMI_TX_REG_EXT_FS_RCAL_EN_MASK 26
61#define UTMI_TX_REG_EXT_FS_RCAL_EN (0x1 << 26)
62#define UTMI_TX_LOW_VDD_EN_SHIFT 11
63#define UTMI_TX_IMPCAL_VTH_SHIFT 14
64#define UTMI_TX_IMPCAL_VTH_MASK (0x7 << 14)
65#define UTMI_TX_CK60_PHSEL_SHIFT 17
66#define UTMI_TX_CK60_PHSEL_MASK (0xf << 17)
67#define UTMI_TX_TXVDD12_SHIFT 22
68#define UTMI_TX_TXVDD12_MASK (0x3 << 22)
69#define UTMI_TX_AMP_SHIFT 0
70#define UTMI_TX_AMP_MASK (0x7 << 0)
71/* For UTMI_RX Register */
72#define UTMI_RX_SQ_THRESH_SHIFT 4
73#define UTMI_RX_SQ_THRESH_MASK (0xf << 4)
74#define UTMI_REG_SQ_LENGTH_SHIFT 15
75#define UTMI_REG_SQ_LENGTH_MASK (0x3 << 15)
76
77#define REG_RCAL_START 0x00001000
78#define VCOCAL_START 0x00200000
79#define KVCO_EXT 0x00400000
80#define PLL_READY 0x00800000
81#define CLK_BLK_EN 0x01000000
82#endif
83
84static unsigned int u2o_read(unsigned int base, unsigned int offset)
85{
86 return readl(base + offset);
87}
88
89static void u2o_set(unsigned int base, unsigned int offset, unsigned int value)
90{
91 unsigned int reg;
92
93 reg = readl(base + offset);
94 reg |= value;
95 writel(reg, base + offset);
96 readl(base + offset);
97}
98
99static void u2o_clear(unsigned int base, unsigned int offset,
100 unsigned int value)
101{
102 unsigned int reg;
103
104 reg = readl(base + offset);
105 reg &= ~value;
106 writel(reg, base + offset);
107 readl(base + offset);
108}
109
110static void u2o_write(unsigned int base, unsigned int offset,
111 unsigned int value)
112{
113 writel(value, base + offset);
114 readl(base + offset);
115}
116
117#ifdef CONFIG_ARCH_MMP
118int mv_udc_phy_init(unsigned int base)
119{
120 unsigned long timeout;
121
122 /* Initialize the USB PHY power */
123 if (cpu_is_pxa910()) {
124 u2o_set(base, UTMI_CTRL, (1 << UTMI_CTRL_INPKT_DELAY_SOF_SHIFT)
125 | (1 << UTMI_CTRL_PU_REF_SHIFT));
126 }
127
128 u2o_set(base, UTMI_CTRL, 1 << UTMI_CTRL_PLL_PWR_UP_SHIFT);
129 u2o_set(base, UTMI_CTRL, 1 << UTMI_CTRL_PWR_UP_SHIFT);
130
131 /* UTMI_PLL settings */
132 u2o_clear(base, UTMI_PLL, UTMI_PLL_PLLVDD18_MASK
133 | UTMI_PLL_PLLVDD12_MASK | UTMI_PLL_PLLCALI12_MASK
134 | UTMI_PLL_FBDIV_MASK | UTMI_PLL_REFDIV_MASK
135 | UTMI_PLL_ICP_MASK | UTMI_PLL_KVCO_MASK);
136
137 u2o_set(base, UTMI_PLL, (0xee << UTMI_PLL_FBDIV_SHIFT)
138 | (0xb << UTMI_PLL_REFDIV_SHIFT)
139 | (3 << UTMI_PLL_PLLVDD18_SHIFT)
140 | (3 << UTMI_PLL_PLLVDD12_SHIFT)
141 | (3 << UTMI_PLL_PLLCALI12_SHIFT)
142 | (1 << UTMI_PLL_ICP_SHIFT) | (3 << UTMI_PLL_KVCO_SHIFT));
143
144 /* UTMI_TX */
145 u2o_clear(base, UTMI_TX, UTMI_TX_REG_EXT_FS_RCAL_EN_MASK
146 | UTMI_TX_TXVDD12_MASK
147 | UTMI_TX_CK60_PHSEL_MASK | UTMI_TX_IMPCAL_VTH_MASK
148 | UTMI_TX_REG_EXT_FS_RCAL_MASK | UTMI_TX_AMP_MASK);
149 u2o_set(base, UTMI_TX, (3 << UTMI_TX_TXVDD12_SHIFT)
150 | (4 << UTMI_TX_CK60_PHSEL_SHIFT)
151 | (4 << UTMI_TX_IMPCAL_VTH_SHIFT)
152 | (8 << UTMI_TX_REG_EXT_FS_RCAL_SHIFT)
153 | (3 << UTMI_TX_AMP_SHIFT));
154
155 /* UTMI_RX */
156 u2o_clear(base, UTMI_RX, UTMI_RX_SQ_THRESH_MASK
157 | UTMI_REG_SQ_LENGTH_MASK);
158 if (cpu_is_pxa168())
159 u2o_set(base, UTMI_RX, (7 << UTMI_RX_SQ_THRESH_SHIFT)
160 | (2 << UTMI_REG_SQ_LENGTH_SHIFT));
161 else
162 u2o_set(base, UTMI_RX, (0x7 << UTMI_RX_SQ_THRESH_SHIFT)
163 | (2 << UTMI_REG_SQ_LENGTH_SHIFT));
164
165 /* UTMI_IVREF */
166 if (cpu_is_pxa168())
167 /*
168 * fixing Microsoft Altair board interface with NEC hub issue -
169 * Set UTMI_IVREF from 0x4a3 to 0x4bf
170 */
171 u2o_write(base, UTMI_IVREF, 0x4bf);
172
173 /* calibrate */
174 timeout = jiffies + 100;
175 while ((u2o_read(base, UTMI_PLL) & PLL_READY) == 0) {
176 if (time_after(jiffies, timeout))
177 return -ETIME;
178 cpu_relax();
179 }
180
181 /* toggle VCOCAL_START bit of UTMI_PLL */
182 udelay(200);
183 u2o_set(base, UTMI_PLL, VCOCAL_START);
184 udelay(40);
185 u2o_clear(base, UTMI_PLL, VCOCAL_START);
186
187 /* toggle REG_RCAL_START bit of UTMI_TX */
188 udelay(200);
189 u2o_set(base, UTMI_TX, REG_RCAL_START);
190 udelay(40);
191 u2o_clear(base, UTMI_TX, REG_RCAL_START);
192 udelay(200);
193
194 /* make sure phy is ready */
195 timeout = jiffies + 100;
196 while ((u2o_read(base, UTMI_PLL) & PLL_READY) == 0) {
197 if (time_after(jiffies, timeout))
198 return -ETIME;
199 cpu_relax();
200 }
201
202 if (cpu_is_pxa168()) {
203 u2o_set(base, UTMI_RESERVE, 1 << 5);
204 /* Turn on UTMI PHY OTG extension */
205 u2o_write(base, UTMI_OTG_ADDON, 1);
206 }
207 return 0;
208}
209#else
210int mv_udc_phy_init(unsigned int base)
211{
212 return 0;
213}
214#endif
diff --git a/drivers/usb/gadget/ncm.c b/drivers/usb/gadget/ncm.c
index 62ee5087dcac..89530034dff1 100644
--- a/drivers/usb/gadget/ncm.c
+++ b/drivers/usb/gadget/ncm.c
@@ -14,15 +14,6 @@
14 * it under the terms of the GNU General Public License as published by 14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or 15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version. 16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */ 17 */
27 18
28/* #define DEBUG */ 19/* #define DEBUG */
diff --git a/drivers/usb/gadget/ndis.h b/drivers/usb/gadget/ndis.h
index df886cec5ef4..b0e52fc277b4 100644
--- a/drivers/usb/gadget/ndis.h
+++ b/drivers/usb/gadget/ndis.h
@@ -10,12 +10,6 @@
10 * 10 *
11 * This source code is offered for use in the public domain. You may 11 * This source code is offered for use in the public domain. You may
12 * use, modify or distribute it freely. 12 * use, modify or distribute it freely.
13 *
14 * This code is distributed in the hope that it will be useful but
15 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
16 * DISCLAIMED. This includes but is not limited to warranties of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 */ 13 */
20 14
21#ifndef _LINUX_NDIS_H 15#ifndef _LINUX_NDIS_H
diff --git a/drivers/usb/gadget/net2272.c b/drivers/usb/gadget/net2272.c
index ab98ea926a11..d1b76368472f 100644
--- a/drivers/usb/gadget/net2272.c
+++ b/drivers/usb/gadget/net2272.c
@@ -204,7 +204,7 @@ net2272_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
204 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 204 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
205 return -ESHUTDOWN; 205 return -ESHUTDOWN;
206 206
207 max = le16_to_cpu(desc->wMaxPacketSize) & 0x1fff; 207 max = usb_endpoint_maxp(desc) & 0x1fff;
208 208
209 spin_lock_irqsave(&dev->lock, flags); 209 spin_lock_irqsave(&dev->lock, flags);
210 _ep->maxpacket = max & 0x7fff; 210 _ep->maxpacket = max & 0x7fff;
@@ -1172,17 +1172,18 @@ net2272_pullup(struct usb_gadget *_gadget, int is_on)
1172 return 0; 1172 return 0;
1173} 1173}
1174 1174
1175static int net2272_start(struct usb_gadget_driver *driver, 1175static int net2272_start(struct usb_gadget *_gadget,
1176 int (*bind)(struct usb_gadget *)); 1176 struct usb_gadget_driver *driver);
1177static int net2272_stop(struct usb_gadget_driver *driver); 1177static int net2272_stop(struct usb_gadget *_gadget,
1178 struct usb_gadget_driver *driver);
1178 1179
1179static const struct usb_gadget_ops net2272_ops = { 1180static const struct usb_gadget_ops net2272_ops = {
1180 .get_frame = net2272_get_frame, 1181 .get_frame = net2272_get_frame,
1181 .wakeup = net2272_wakeup, 1182 .wakeup = net2272_wakeup,
1182 .set_selfpowered = net2272_set_selfpowered, 1183 .set_selfpowered = net2272_set_selfpowered,
1183 .pullup = net2272_pullup, 1184 .pullup = net2272_pullup,
1184 .start = net2272_start, 1185 .udc_start = net2272_start,
1185 .stop = net2272_stop, 1186 .udc_stop = net2272_stop,
1186}; 1187};
1187 1188
1188/*---------------------------------------------------------------------------*/ 1189/*---------------------------------------------------------------------------*/
@@ -1356,8 +1357,6 @@ net2272_set_fifo_mode(struct net2272 *dev, int mode)
1356 1357
1357/*---------------------------------------------------------------------------*/ 1358/*---------------------------------------------------------------------------*/
1358 1359
1359static struct net2272 *the_controller;
1360
1361static void 1360static void
1362net2272_usb_reset(struct net2272 *dev) 1361net2272_usb_reset(struct net2272 *dev)
1363{ 1362{
@@ -1453,20 +1452,17 @@ net2272_ep0_start(struct net2272 *dev)
1453 * disconnect is reported. then a host may connect again, or 1452 * disconnect is reported. then a host may connect again, or
1454 * the driver might get unbound. 1453 * the driver might get unbound.
1455 */ 1454 */
1456static int net2272_start(struct usb_gadget_driver *driver, 1455static int net2272_start(struct usb_gadget *_gadget,
1457 int (*bind)(struct usb_gadget *)) 1456 struct usb_gadget_driver *driver)
1458{ 1457{
1459 struct net2272 *dev = the_controller; 1458 struct net2272 *dev;
1460 int ret;
1461 unsigned i; 1459 unsigned i;
1462 1460
1463 if (!driver || !bind || !driver->unbind || !driver->setup || 1461 if (!driver || !driver->unbind || !driver->setup ||
1464 driver->speed != USB_SPEED_HIGH) 1462 driver->speed != USB_SPEED_HIGH)
1465 return -EINVAL; 1463 return -EINVAL;
1466 if (!dev) 1464
1467 return -ENODEV; 1465 dev = container_of(_gadget, struct net2272, gadget);
1468 if (dev->driver)
1469 return -EBUSY;
1470 1466
1471 for (i = 0; i < 4; ++i) 1467 for (i = 0; i < 4; ++i)
1472 dev->ep[i].irqs = 0; 1468 dev->ep[i].irqs = 0;
@@ -1475,14 +1471,6 @@ static int net2272_start(struct usb_gadget_driver *driver,
1475 driver->driver.bus = NULL; 1471 driver->driver.bus = NULL;
1476 dev->driver = driver; 1472 dev->driver = driver;
1477 dev->gadget.dev.driver = &driver->driver; 1473 dev->gadget.dev.driver = &driver->driver;
1478 ret = bind(&dev->gadget);
1479 if (ret) {
1480 dev_dbg(dev->dev, "bind to driver %s --> %d\n",
1481 driver->driver.name, ret);
1482 dev->driver = NULL;
1483 dev->gadget.dev.driver = NULL;
1484 return ret;
1485 }
1486 1474
1487 /* ... then enable host detection and ep0; and we're ready 1475 /* ... then enable host detection and ep0; and we're ready
1488 * for set_configuration as well as eventual disconnect. 1476 * for set_configuration as well as eventual disconnect.
@@ -1510,33 +1498,21 @@ stop_activity(struct net2272 *dev, struct usb_gadget_driver *driver)
1510 for (i = 0; i < 4; ++i) 1498 for (i = 0; i < 4; ++i)
1511 net2272_dequeue_all(&dev->ep[i]); 1499 net2272_dequeue_all(&dev->ep[i]);
1512 1500
1513 /* report disconnect; the driver is already quiesced */
1514 if (driver) {
1515 spin_unlock(&dev->lock);
1516 driver->disconnect(&dev->gadget);
1517 spin_lock(&dev->lock);
1518
1519 }
1520 net2272_usb_reinit(dev); 1501 net2272_usb_reinit(dev);
1521} 1502}
1522 1503
1523static int net2272_stop(struct usb_gadget_driver *driver) 1504static int net2272_stop(struct usb_gadget *_gadget,
1505 struct usb_gadget_driver *driver)
1524{ 1506{
1525 struct net2272 *dev = the_controller; 1507 struct net2272 *dev;
1526 unsigned long flags; 1508 unsigned long flags;
1527 1509
1528 if (!dev) 1510 dev = container_of(_gadget, struct net2272, gadget);
1529 return -ENODEV;
1530 if (!driver || driver != dev->driver)
1531 return -EINVAL;
1532 1511
1533 spin_lock_irqsave(&dev->lock, flags); 1512 spin_lock_irqsave(&dev->lock, flags);
1534 stop_activity(dev, driver); 1513 stop_activity(dev, driver);
1535 spin_unlock_irqrestore(&dev->lock, flags); 1514 spin_unlock_irqrestore(&dev->lock, flags);
1536 1515
1537 net2272_pullup(&dev->gadget, 0);
1538
1539 driver->unbind(&dev->gadget);
1540 dev->gadget.dev.driver = NULL; 1516 dev->gadget.dev.driver = NULL;
1541 dev->driver = NULL; 1517 dev->driver = NULL;
1542 1518
@@ -1764,8 +1740,8 @@ net2272_handle_stat0_irqs(struct net2272 *dev, u8 stat)
1764 dev->gadget.speed = USB_SPEED_HIGH; 1740 dev->gadget.speed = USB_SPEED_HIGH;
1765 else 1741 else
1766 dev->gadget.speed = USB_SPEED_FULL; 1742 dev->gadget.speed = USB_SPEED_FULL;
1767 dev_dbg(dev->dev, "%s speed\n", 1743 dev_dbg(dev->dev, "%s\n",
1768 (dev->gadget.speed == USB_SPEED_HIGH) ? "high" : "full"); 1744 usb_speed_string(dev->gadget.speed));
1769 } 1745 }
1770 1746
1771 ep = &dev->ep[0]; 1747 ep = &dev->ep[0];
@@ -2238,7 +2214,6 @@ net2272_remove(struct net2272 *dev)
2238 device_remove_file(dev->dev, &dev_attr_registers); 2214 device_remove_file(dev->dev, &dev_attr_registers);
2239 2215
2240 dev_info(dev->dev, "unbind\n"); 2216 dev_info(dev->dev, "unbind\n");
2241 the_controller = NULL;
2242} 2217}
2243 2218
2244static struct net2272 * __devinit 2219static struct net2272 * __devinit
@@ -2246,11 +2221,6 @@ net2272_probe_init(struct device *dev, unsigned int irq)
2246{ 2221{
2247 struct net2272 *ret; 2222 struct net2272 *ret;
2248 2223
2249 if (the_controller) {
2250 dev_warn(dev, "ignoring\n");
2251 return ERR_PTR(-EBUSY);
2252 }
2253
2254 if (!irq) { 2224 if (!irq) {
2255 dev_dbg(dev, "No IRQ!\n"); 2225 dev_dbg(dev, "No IRQ!\n");
2256 return ERR_PTR(-ENODEV); 2226 return ERR_PTR(-ENODEV);
@@ -2307,8 +2277,6 @@ net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2307 dma_mode_string()); 2277 dma_mode_string());
2308 dev_info(dev->dev, "version: %s\n", driver_vers); 2278 dev_info(dev->dev, "version: %s\n", driver_vers);
2309 2279
2310 the_controller = dev;
2311
2312 ret = device_register(&dev->gadget.dev); 2280 ret = device_register(&dev->gadget.dev);
2313 if (ret) 2281 if (ret)
2314 goto err_irq; 2282 goto err_irq;
@@ -2684,8 +2652,6 @@ net2272_plat_probe(struct platform_device *pdev)
2684 dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n", 2652 dev_info(&pdev->dev, "running in 16-bit, %sbyte swap local bus mode\n",
2685 (net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no "); 2653 (net2272_read(dev, LOCCTL) & (1 << BYTE_SWAP)) ? "" : "no ");
2686 2654
2687 the_controller = dev;
2688
2689 return 0; 2655 return 0;
2690 2656
2691 err_io: 2657 err_io:
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index 3dd40b4e675c..7f1bc9a73cda 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -33,15 +33,6 @@
33 * it under the terms of the GNU General Public License as published by 33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or 34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version. 35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 */ 36 */
46 37
47#undef DEBUG /* messages on error and most fault paths */ 38#undef DEBUG /* messages on error and most fault paths */
@@ -169,7 +160,7 @@ net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
169 return -EDOM; 160 return -EDOM;
170 161
171 /* sanity check ep-e/ep-f since their fifos are small */ 162 /* sanity check ep-e/ep-f since their fifos are small */
172 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff; 163 max = usb_endpoint_maxp (desc) & 0x1fff;
173 if (ep->num > 4 && max > 64) 164 if (ep->num > 4 && max > 64)
174 return -ERANGE; 165 return -ERANGE;
175 166
@@ -1410,17 +1401,18 @@ static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1410 return 0; 1401 return 0;
1411} 1402}
1412 1403
1413static int net2280_start(struct usb_gadget_driver *driver, 1404static int net2280_start(struct usb_gadget *_gadget,
1414 int (*bind)(struct usb_gadget *)); 1405 struct usb_gadget_driver *driver);
1415static int net2280_stop(struct usb_gadget_driver *driver); 1406static int net2280_stop(struct usb_gadget *_gadget,
1407 struct usb_gadget_driver *driver);
1416 1408
1417static const struct usb_gadget_ops net2280_ops = { 1409static const struct usb_gadget_ops net2280_ops = {
1418 .get_frame = net2280_get_frame, 1410 .get_frame = net2280_get_frame,
1419 .wakeup = net2280_wakeup, 1411 .wakeup = net2280_wakeup,
1420 .set_selfpowered = net2280_set_selfpowered, 1412 .set_selfpowered = net2280_set_selfpowered,
1421 .pullup = net2280_pullup, 1413 .pullup = net2280_pullup,
1422 .start = net2280_start, 1414 .udc_start = net2280_start,
1423 .stop = net2280_stop, 1415 .udc_stop = net2280_stop,
1424}; 1416};
1425 1417
1426/*-------------------------------------------------------------------------*/ 1418/*-------------------------------------------------------------------------*/
@@ -1640,7 +1632,7 @@ show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1640 default: 1632 default:
1641 val = "iso"; break; 1633 val = "iso"; break;
1642 }; val; }), 1634 }; val; }),
1643 le16_to_cpu (d->wMaxPacketSize) & 0x1fff, 1635 usb_endpoint_maxp (d) & 0x1fff,
1644 ep->dma ? "dma" : "pio", ep->fifo_size 1636 ep->dma ? "dma" : "pio", ep->fifo_size
1645 ); 1637 );
1646 } else /* ep0 should only have one transfer queued */ 1638 } else /* ep0 should only have one transfer queued */
@@ -1753,8 +1745,6 @@ static void set_fifo_mode (struct net2280 *dev, int mode)
1753 * perhaps to bind specific drivers to specific devices. 1745 * perhaps to bind specific drivers to specific devices.
1754 */ 1746 */
1755 1747
1756static struct net2280 *the_controller;
1757
1758static void usb_reset (struct net2280 *dev) 1748static void usb_reset (struct net2280 *dev)
1759{ 1749{
1760 u32 tmp; 1750 u32 tmp;
@@ -1880,10 +1870,10 @@ static void ep0_start (struct net2280 *dev)
1880 * disconnect is reported. then a host may connect again, or 1870 * disconnect is reported. then a host may connect again, or
1881 * the driver might get unbound. 1871 * the driver might get unbound.
1882 */ 1872 */
1883static int net2280_start(struct usb_gadget_driver *driver, 1873static int net2280_start(struct usb_gadget *_gadget,
1884 int (*bind)(struct usb_gadget *)) 1874 struct usb_gadget_driver *driver)
1885{ 1875{
1886 struct net2280 *dev = the_controller; 1876 struct net2280 *dev;
1887 int retval; 1877 int retval;
1888 unsigned i; 1878 unsigned i;
1889 1879
@@ -1891,14 +1881,11 @@ static int net2280_start(struct usb_gadget_driver *driver,
1891 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE) 1881 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1892 * "must not be used in normal operation" 1882 * "must not be used in normal operation"
1893 */ 1883 */
1894 if (!driver 1884 if (!driver || driver->speed != USB_SPEED_HIGH
1895 || driver->speed != USB_SPEED_HIGH 1885 || !driver->setup)
1896 || !bind || !driver->setup)
1897 return -EINVAL; 1886 return -EINVAL;
1898 if (!dev) 1887
1899 return -ENODEV; 1888 dev = container_of (_gadget, struct net2280, gadget);
1900 if (dev->driver)
1901 return -EBUSY;
1902 1889
1903 for (i = 0; i < 7; i++) 1890 for (i = 0; i < 7; i++)
1904 dev->ep [i].irqs = 0; 1891 dev->ep [i].irqs = 0;
@@ -1908,14 +1895,6 @@ static int net2280_start(struct usb_gadget_driver *driver,
1908 driver->driver.bus = NULL; 1895 driver->driver.bus = NULL;
1909 dev->driver = driver; 1896 dev->driver = driver;
1910 dev->gadget.dev.driver = &driver->driver; 1897 dev->gadget.dev.driver = &driver->driver;
1911 retval = bind(&dev->gadget);
1912 if (retval) {
1913 DEBUG (dev, "bind to driver %s --> %d\n",
1914 driver->driver.name, retval);
1915 dev->driver = NULL;
1916 dev->gadget.dev.driver = NULL;
1917 return retval;
1918 }
1919 1898
1920 retval = device_create_file (&dev->pdev->dev, &dev_attr_function); 1899 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1921 if (retval) goto err_unbind; 1900 if (retval) goto err_unbind;
@@ -1961,33 +1940,21 @@ stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1961 for (i = 0; i < 7; i++) 1940 for (i = 0; i < 7; i++)
1962 nuke (&dev->ep [i]); 1941 nuke (&dev->ep [i]);
1963 1942
1964 /* report disconnect; the driver is already quiesced */
1965 if (driver) {
1966 spin_unlock (&dev->lock);
1967 driver->disconnect (&dev->gadget);
1968 spin_lock (&dev->lock);
1969 }
1970
1971 usb_reinit (dev); 1943 usb_reinit (dev);
1972} 1944}
1973 1945
1974static int net2280_stop(struct usb_gadget_driver *driver) 1946static int net2280_stop(struct usb_gadget *_gadget,
1947 struct usb_gadget_driver *driver)
1975{ 1948{
1976 struct net2280 *dev = the_controller; 1949 struct net2280 *dev;
1977 unsigned long flags; 1950 unsigned long flags;
1978 1951
1979 if (!dev) 1952 dev = container_of (_gadget, struct net2280, gadget);
1980 return -ENODEV;
1981 if (!driver || driver != dev->driver || !driver->unbind)
1982 return -EINVAL;
1983 1953
1984 spin_lock_irqsave (&dev->lock, flags); 1954 spin_lock_irqsave (&dev->lock, flags);
1985 stop_activity (dev, driver); 1955 stop_activity (dev, driver);
1986 spin_unlock_irqrestore (&dev->lock, flags); 1956 spin_unlock_irqrestore (&dev->lock, flags);
1987 1957
1988 net2280_pullup (&dev->gadget, 0);
1989
1990 driver->unbind (&dev->gadget);
1991 dev->gadget.dev.driver = NULL; 1958 dev->gadget.dev.driver = NULL;
1992 dev->driver = NULL; 1959 dev->driver = NULL;
1993 1960
@@ -2266,9 +2233,7 @@ static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2266 else 2233 else
2267 dev->gadget.speed = USB_SPEED_FULL; 2234 dev->gadget.speed = USB_SPEED_FULL;
2268 net2280_led_speed (dev, dev->gadget.speed); 2235 net2280_led_speed (dev, dev->gadget.speed);
2269 DEBUG (dev, "%s speed\n", 2236 DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
2270 (dev->gadget.speed == USB_SPEED_HIGH)
2271 ? "high" : "full");
2272 } 2237 }
2273 2238
2274 ep = &dev->ep [0]; 2239 ep = &dev->ep [0];
@@ -2481,7 +2446,7 @@ static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2481 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED); 2446 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2482 2447
2483 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set. 2448 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2484 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and 2449 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2485 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT 2450 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2486 * only indicates a change in the reset state). 2451 * only indicates a change in the reset state).
2487 */ 2452 */
@@ -2709,8 +2674,6 @@ static void net2280_remove (struct pci_dev *pdev)
2709 pci_set_drvdata (pdev, NULL); 2674 pci_set_drvdata (pdev, NULL);
2710 2675
2711 INFO (dev, "unbind\n"); 2676 INFO (dev, "unbind\n");
2712
2713 the_controller = NULL;
2714} 2677}
2715 2678
2716/* wrap this driver around the specified device, but 2679/* wrap this driver around the specified device, but
@@ -2724,14 +2687,6 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2724 void __iomem *base = NULL; 2687 void __iomem *base = NULL;
2725 int retval, i; 2688 int retval, i;
2726 2689
2727 /* if you want to support more than one controller in a system,
2728 * usb_gadget_driver_{register,unregister}() must change.
2729 */
2730 if (the_controller) {
2731 dev_warn (&pdev->dev, "ignoring\n");
2732 return -EBUSY;
2733 }
2734
2735 /* alloc, and start init */ 2690 /* alloc, and start init */
2736 dev = kzalloc (sizeof *dev, GFP_KERNEL); 2691 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2737 if (dev == NULL){ 2692 if (dev == NULL){
@@ -2858,8 +2813,6 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2858 use_dma 2813 use_dma
2859 ? (use_dma_chaining ? "chaining" : "enabled") 2814 ? (use_dma_chaining ? "chaining" : "enabled")
2860 : "disabled"); 2815 : "disabled");
2861 the_controller = dev;
2862
2863 retval = device_register (&dev->gadget.dev); 2816 retval = device_register (&dev->gadget.dev);
2864 if (retval) goto done; 2817 if (retval) goto done;
2865 retval = device_create_file (&pdev->dev, &dev_attr_registers); 2818 retval = device_create_file (&pdev->dev, &dev_attr_registers);
diff --git a/drivers/usb/gadget/net2280.h b/drivers/usb/gadget/net2280.h
index c36852263d93..a844be0d683a 100644
--- a/drivers/usb/gadget/net2280.h
+++ b/drivers/usb/gadget/net2280.h
@@ -11,15 +11,6 @@
11 * it under the terms of the GNU General Public License as published by 11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. 13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */ 14 */
24 15
25#include <linux/usb/net2280.h> 16#include <linux/usb/net2280.h>
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index 740c7daed279..788989a10223 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -10,15 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 13 */
23 14
24#undef DEBUG 15#undef DEBUG
@@ -166,15 +157,14 @@ static int omap_ep_enable(struct usb_ep *_ep,
166 if (!_ep || !desc || ep->desc 157 if (!_ep || !desc || ep->desc
167 || desc->bDescriptorType != USB_DT_ENDPOINT 158 || desc->bDescriptorType != USB_DT_ENDPOINT
168 || ep->bEndpointAddress != desc->bEndpointAddress 159 || ep->bEndpointAddress != desc->bEndpointAddress
169 || ep->maxpacket < le16_to_cpu 160 || ep->maxpacket < usb_endpoint_maxp(desc)) {
170 (desc->wMaxPacketSize)) {
171 DBG("%s, bad ep or descriptor\n", __func__); 161 DBG("%s, bad ep or descriptor\n", __func__);
172 return -EINVAL; 162 return -EINVAL;
173 } 163 }
174 maxp = le16_to_cpu (desc->wMaxPacketSize); 164 maxp = usb_endpoint_maxp(desc);
175 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK 165 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
176 && maxp != ep->maxpacket) 166 && maxp != ep->maxpacket)
177 || le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket 167 || usb_endpoint_maxp(desc) > ep->maxpacket
178 || !desc->wMaxPacketSize) { 168 || !desc->wMaxPacketSize) {
179 DBG("%s, bad %s maxpacket\n", __func__, _ep->name); 169 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
180 return -ERANGE; 170 return -ERANGE;
@@ -2968,7 +2958,7 @@ known:
2968 } 2958 }
2969#ifdef USE_ISO 2959#ifdef USE_ISO
2970 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq, 2960 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
2971 IRQF_DISABLED, "omap_udc iso", udc); 2961 0, "omap_udc iso", udc);
2972 if (status != 0) { 2962 if (status != 0) {
2973 ERR("can't get irq %d, err %d\n", 2963 ERR("can't get irq %d, err %d\n",
2974 (int) pdev->resource[3].start, status); 2964 (int) pdev->resource[3].start, status);
diff --git a/drivers/usb/gadget/pch_udc.c b/drivers/usb/gadget/pch_udc.c
index f96615ab6b77..5048a0c07640 100644
--- a/drivers/usb/gadget/pch_udc.c
+++ b/drivers/usb/gadget/pch_udc.c
@@ -1,18 +1,9 @@
1/* 1/*
2 * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD. 2 * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */ 7 */
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18#include <linux/kernel.h> 9#include <linux/kernel.h>
@@ -363,6 +354,7 @@ struct pch_udc_dev {
363#define PCI_DEVICE_ID_INTEL_EG20T_UDC 0x8808 354#define PCI_DEVICE_ID_INTEL_EG20T_UDC 0x8808
364#define PCI_VENDOR_ID_ROHM 0x10DB 355#define PCI_VENDOR_ID_ROHM 0x10DB
365#define PCI_DEVICE_ID_ML7213_IOH_UDC 0x801D 356#define PCI_DEVICE_ID_ML7213_IOH_UDC 0x801D
357#define PCI_DEVICE_ID_ML7831_IOH_UDC 0x8808
366 358
367static const char ep0_string[] = "ep0in"; 359static const char ep0_string[] = "ep0in";
368static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */ 360static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */
@@ -947,7 +939,7 @@ static void pch_udc_ep_enable(struct pch_udc_ep *ep,
947 else 939 else
948 buff_size = UDC_EPOUT_BUFF_SIZE; 940 buff_size = UDC_EPOUT_BUFF_SIZE;
949 pch_udc_ep_set_bufsz(ep, buff_size, ep->in); 941 pch_udc_ep_set_bufsz(ep, buff_size, ep->in);
950 pch_udc_ep_set_maxpkt(ep, le16_to_cpu(desc->wMaxPacketSize)); 942 pch_udc_ep_set_maxpkt(ep, usb_endpoint_maxp(desc));
951 pch_udc_ep_set_nak(ep); 943 pch_udc_ep_set_nak(ep);
952 pch_udc_ep_fifo_flush(ep, ep->in); 944 pch_udc_ep_fifo_flush(ep, ep->in);
953 /* Configure the endpoint */ 945 /* Configure the endpoint */
@@ -957,7 +949,7 @@ static void pch_udc_ep_enable(struct pch_udc_ep *ep,
957 (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) | 949 (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) |
958 (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) | 950 (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) |
959 (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) | 951 (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) |
960 le16_to_cpu(desc->wMaxPacketSize) << UDC_CSR_NE_MAX_PKT_SHIFT; 952 usb_endpoint_maxp(desc) << UDC_CSR_NE_MAX_PKT_SHIFT;
961 953
962 if (ep->in) 954 if (ep->in)
963 pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num)); 955 pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num));
@@ -1466,7 +1458,7 @@ static int pch_udc_pcd_ep_enable(struct usb_ep *usbep,
1466 ep->desc = desc; 1458 ep->desc = desc;
1467 ep->halted = 0; 1459 ep->halted = 0;
1468 pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc); 1460 pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc);
1469 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 1461 ep->ep.maxpacket = usb_endpoint_maxp(desc);
1470 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); 1462 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1471 spin_unlock_irqrestore(&dev->lock, iflags); 1463 spin_unlock_irqrestore(&dev->lock, iflags);
1472 return 0; 1464 return 0;
@@ -2979,6 +2971,11 @@ static DEFINE_PCI_DEVICE_TABLE(pch_udc_pcidev_id) = {
2979 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe, 2971 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
2980 .class_mask = 0xffffffff, 2972 .class_mask = 0xffffffff,
2981 }, 2973 },
2974 {
2975 PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7831_IOH_UDC),
2976 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
2977 .class_mask = 0xffffffff,
2978 },
2982 { 0 }, 2979 { 0 },
2983}; 2980};
2984 2981
@@ -3008,5 +3005,5 @@ static void __exit pch_udc_pci_exit(void)
3008module_exit(pch_udc_pci_exit); 3005module_exit(pch_udc_pci_exit);
3009 3006
3010MODULE_DESCRIPTION("Intel EG20T USB Device Controller"); 3007MODULE_DESCRIPTION("Intel EG20T USB Device Controller");
3011MODULE_AUTHOR("OKI SEMICONDUCTOR, <toshiharu-linux@dsn.okisemi.com>"); 3008MODULE_AUTHOR("LAPIS Semiconductor, <tomoya-linux@dsn.lapis-semi.com>");
3012MODULE_LICENSE("GPL"); 3009MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index a341dde6f9c3..65a8834f274b 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22#include <linux/module.h> 13#include <linux/module.h>
@@ -971,23 +962,15 @@ printer_set_config(struct printer_dev *dev, unsigned number)
971 usb_gadget_vbus_draw(dev->gadget, 962 usb_gadget_vbus_draw(dev->gadget,
972 dev->gadget->is_otg ? 8 : 100); 963 dev->gadget->is_otg ? 8 : 100);
973 } else { 964 } else {
974 char *speed;
975 unsigned power; 965 unsigned power;
976 966
977 power = 2 * config_desc.bMaxPower; 967 power = 2 * config_desc.bMaxPower;
978 usb_gadget_vbus_draw(dev->gadget, power); 968 usb_gadget_vbus_draw(dev->gadget, power);
979 969
980 switch (gadget->speed) {
981 case USB_SPEED_FULL: speed = "full"; break;
982#ifdef CONFIG_USB_GADGET_DUALSPEED
983 case USB_SPEED_HIGH: speed = "high"; break;
984#endif
985 default: speed = "?"; break;
986 }
987
988 dev->config = number; 970 dev->config = number;
989 INFO(dev, "%s speed config #%d: %d mA, %s\n", 971 INFO(dev, "%s config #%d: %d mA, %s\n",
990 speed, number, power, driver_desc); 972 usb_speed_string(gadget->speed),
973 number, power, driver_desc);
991 } 974 }
992 return result; 975 return result;
993} 976}
@@ -1611,7 +1594,7 @@ cleanup(void)
1611 if (status) 1594 if (status)
1612 ERROR(dev, "usb_gadget_unregister_driver %x\n", status); 1595 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1613 1596
1614 unregister_chrdev_region(g_printer_devno, 2); 1597 unregister_chrdev_region(g_printer_devno, 1);
1615 class_destroy(usb_gadget_class); 1598 class_destroy(usb_gadget_class);
1616 mutex_unlock(&usb_printer_gadget.lock_printer_io); 1599 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1617} 1600}
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index e4e59b4de25d..c090a7e3ecf8 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -11,16 +11,6 @@
11 * it under the terms of the GNU General Public License as published by 11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. 13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */ 14 */
25 15
26/* #define VERBOSE_DEBUG */ 16/* #define VERBOSE_DEBUG */
@@ -232,8 +222,7 @@ static int pxa25x_ep_enable (struct usb_ep *_ep,
232 if (!_ep || !desc || ep->desc || _ep->name == ep0name 222 if (!_ep || !desc || ep->desc || _ep->name == ep0name
233 || desc->bDescriptorType != USB_DT_ENDPOINT 223 || desc->bDescriptorType != USB_DT_ENDPOINT
234 || ep->bEndpointAddress != desc->bEndpointAddress 224 || ep->bEndpointAddress != desc->bEndpointAddress
235 || ep->fifo_size < le16_to_cpu 225 || ep->fifo_size < usb_endpoint_maxp (desc)) {
236 (desc->wMaxPacketSize)) {
237 DMSG("%s, bad ep or descriptor\n", __func__); 226 DMSG("%s, bad ep or descriptor\n", __func__);
238 return -EINVAL; 227 return -EINVAL;
239 } 228 }
@@ -248,7 +237,7 @@ static int pxa25x_ep_enable (struct usb_ep *_ep,
248 237
249 /* hardware _could_ do smaller, but driver doesn't */ 238 /* hardware _could_ do smaller, but driver doesn't */
250 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK 239 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
251 && le16_to_cpu (desc->wMaxPacketSize) 240 && usb_endpoint_maxp (desc)
252 != BULK_FIFO_SIZE) 241 != BULK_FIFO_SIZE)
253 || !desc->wMaxPacketSize) { 242 || !desc->wMaxPacketSize) {
254 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name); 243 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
@@ -264,7 +253,7 @@ static int pxa25x_ep_enable (struct usb_ep *_ep,
264 ep->desc = desc; 253 ep->desc = desc;
265 ep->stopped = 0; 254 ep->stopped = 0;
266 ep->pio_irqs = 0; 255 ep->pio_irqs = 0;
267 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize); 256 ep->ep.maxpacket = usb_endpoint_maxp (desc);
268 257
269 /* flush fifo (mostly for OUT buffers) */ 258 /* flush fifo (mostly for OUT buffers) */
270 pxa25x_ep_fifo_flush (_ep); 259 pxa25x_ep_fifo_flush (_ep);
@@ -401,7 +390,7 @@ write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
401{ 390{
402 unsigned max; 391 unsigned max;
403 392
404 max = le16_to_cpu(ep->desc->wMaxPacketSize); 393 max = usb_endpoint_maxp(ep->desc);
405 do { 394 do {
406 unsigned count; 395 unsigned count;
407 int is_last, is_short; 396 int is_last, is_short;
@@ -671,8 +660,7 @@ pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
671 * we can report per-packet status. that also helps with dma. 660 * we can report per-packet status. that also helps with dma.
672 */ 661 */
673 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC 662 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
674 && req->req.length > le16_to_cpu 663 && req->req.length > usb_endpoint_maxp (ep->desc)))
675 (ep->desc->wMaxPacketSize)))
676 return -EMSGSIZE; 664 return -EMSGSIZE;
677 665
678 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n", 666 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
@@ -1105,7 +1093,7 @@ udc_seq_show(struct seq_file *m, void *_d)
1105 tmp = *dev->ep [i].reg_udccs; 1093 tmp = *dev->ep [i].reg_udccs;
1106 seq_printf(m, 1094 seq_printf(m,
1107 "%s max %d %s udccs %02x irqs %lu\n", 1095 "%s max %d %s udccs %02x irqs %lu\n",
1108 ep->ep.name, le16_to_cpu(desc->wMaxPacketSize), 1096 ep->ep.name, usb_endpoint_maxp(desc),
1109 "pio", tmp, ep->pio_irqs); 1097 "pio", tmp, ep->pio_irqs);
1110 /* TODO translate all five groups of udccs bits! */ 1098 /* TODO translate all five groups of udccs bits! */
1111 1099
@@ -2202,7 +2190,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2202 2190
2203 /* irq setup after old hardware state is cleaned up */ 2191 /* irq setup after old hardware state is cleaned up */
2204 retval = request_irq(irq, pxa25x_udc_irq, 2192 retval = request_irq(irq, pxa25x_udc_irq,
2205 IRQF_DISABLED, driver_name, dev); 2193 0, driver_name, dev);
2206 if (retval != 0) { 2194 if (retval != 0) {
2207 pr_err("%s: can't get irq %d, err %d\n", 2195 pr_err("%s: can't get irq %d, err %d\n",
2208 driver_name, irq, retval); 2196 driver_name, irq, retval);
@@ -2214,7 +2202,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2214 if (machine_is_lubbock()) { 2202 if (machine_is_lubbock()) {
2215 retval = request_irq(LUBBOCK_USB_DISC_IRQ, 2203 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2216 lubbock_vbus_irq, 2204 lubbock_vbus_irq,
2217 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, 2205 IRQF_SAMPLE_RANDOM,
2218 driver_name, dev); 2206 driver_name, dev);
2219 if (retval != 0) { 2207 if (retval != 0) {
2220 pr_err("%s: can't get irq %i, err %d\n", 2208 pr_err("%s: can't get irq %i, err %d\n",
@@ -2223,7 +2211,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2223 } 2211 }
2224 retval = request_irq(LUBBOCK_USB_IRQ, 2212 retval = request_irq(LUBBOCK_USB_IRQ,
2225 lubbock_vbus_irq, 2213 lubbock_vbus_irq,
2226 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, 2214 IRQF_SAMPLE_RANDOM,
2227 driver_name, dev); 2215 driver_name, dev);
2228 if (retval != 0) { 2216 if (retval != 0) {
2229 pr_err("%s: can't get irq %i, err %d\n", 2217 pr_err("%s: can't get irq %i, err %d\n",
diff --git a/drivers/usb/gadget/pxa25x_udc.h b/drivers/usb/gadget/pxa25x_udc.h
index f572c5617462..8eaf4e43726b 100644
--- a/drivers/usb/gadget/pxa25x_udc.h
+++ b/drivers/usb/gadget/pxa25x_udc.h
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23#ifndef __LINUX_USB_GADGET_PXA25X_H 14#ifndef __LINUX_USB_GADGET_PXA25X_H
@@ -161,8 +152,6 @@ static struct pxa25x_udc *the_controller;
161 152
162#ifdef DEBUG 153#ifdef DEBUG
163 154
164static int is_vbus_present(void);
165
166static const char *state_name[] = { 155static const char *state_name[] = {
167 "EP0_IDLE", 156 "EP0_IDLE",
168 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE", 157 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
@@ -214,8 +203,7 @@ dump_state(struct pxa25x_udc *dev)
214 u32 tmp; 203 u32 tmp;
215 unsigned i; 204 unsigned i;
216 205
217 DMSG("%s %s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n", 206 DMSG("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
218 is_vbus_present() ? "host " : "disconnected",
219 state_name[dev->ep0state], 207 state_name[dev->ep0state],
220 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL); 208 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
221 dump_udccr("udccr"); 209 dump_udccr("udccr");
@@ -232,9 +220,6 @@ dump_state(struct pxa25x_udc *dev)
232 } else 220 } else
233 DMSG("ep0 driver '%s'\n", dev->driver->driver.name); 221 DMSG("ep0 driver '%s'\n", dev->driver->driver.name);
234 222
235 if (!is_vbus_present())
236 return;
237
238 dump_udccs0 ("udccs0"); 223 dump_udccs0 ("udccs0");
239 DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n", 224 DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n",
240 dev->stats.write.bytes, dev->stats.write.ops, 225 dev->stats.write.bytes, dev->stats.write.ops,
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 85b68c75dc9d..18b6b091f2a6 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -8,16 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */ 11 */
22#include <linux/module.h> 12#include <linux/module.h>
23#include <linux/kernel.h> 13#include <linux/kernel.h>
@@ -1439,7 +1429,7 @@ static int pxa_ep_enable(struct usb_ep *_ep,
1439 return -EINVAL; 1429 return -EINVAL;
1440 } 1430 }
1441 1431
1442 if (ep->fifo_size < le16_to_cpu(desc->wMaxPacketSize)) { 1432 if (ep->fifo_size < usb_endpoint_maxp(desc)) {
1443 ep_err(ep, "bad maxpacket\n"); 1433 ep_err(ep, "bad maxpacket\n");
1444 return -ERANGE; 1434 return -ERANGE;
1445 } 1435 }
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index b01696eab068..7f4e8f424e80 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23#ifndef __LINUX_USB_GADGET_PXA27X_H 14#ifndef __LINUX_USB_GADGET_PXA27X_H
diff --git a/drivers/usb/gadget/r8a66597-udc.c b/drivers/usb/gadget/r8a66597-udc.c
index 50991e5bd5e8..24f84b210ce1 100644
--- a/drivers/usb/gadget/r8a66597-udc.c
+++ b/drivers/usb/gadget/r8a66597-udc.c
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22 12
23#include <linux/module.h> 13#include <linux/module.h>
@@ -28,13 +18,14 @@
28#include <linux/clk.h> 18#include <linux/clk.h>
29#include <linux/err.h> 19#include <linux/err.h>
30#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/dma-mapping.h>
31 22
32#include <linux/usb/ch9.h> 23#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h> 24#include <linux/usb/gadget.h>
34 25
35#include "r8a66597-udc.h" 26#include "r8a66597-udc.h"
36 27
37#define DRIVER_VERSION "2009-08-18" 28#define DRIVER_VERSION "2011-09-26"
38 29
39static const char udc_name[] = "r8a66597_udc"; 30static const char udc_name[] = "r8a66597_udc";
40static const char *r8a66597_ep_name[] = { 31static const char *r8a66597_ep_name[] = {
@@ -115,13 +106,15 @@ static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
115 u16 pid = 0; 106 u16 pid = 0;
116 unsigned long offset; 107 unsigned long offset;
117 108
118 if (pipenum == 0) 109 if (pipenum == 0) {
119 pid = r8a66597_read(r8a66597, DCPCTR) & PID; 110 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
120 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 111 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
121 offset = get_pipectr_addr(pipenum); 112 offset = get_pipectr_addr(pipenum);
122 pid = r8a66597_read(r8a66597, offset) & PID; 113 pid = r8a66597_read(r8a66597, offset) & PID;
123 } else 114 } else {
124 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 115 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
116 pipenum);
117 }
125 118
126 return pid; 119 return pid;
127} 120}
@@ -131,13 +124,15 @@ static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
131{ 124{
132 unsigned long offset; 125 unsigned long offset;
133 126
134 if (pipenum == 0) 127 if (pipenum == 0) {
135 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR); 128 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
136 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 129 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
137 offset = get_pipectr_addr(pipenum); 130 offset = get_pipectr_addr(pipenum);
138 r8a66597_mdfy(r8a66597, pid, PID, offset); 131 r8a66597_mdfy(r8a66597, pid, PID, offset);
139 } else 132 } else {
140 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 133 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
134 pipenum);
135 }
141} 136}
142 137
143static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum) 138static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
@@ -160,13 +155,15 @@ static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
160 u16 ret = 0; 155 u16 ret = 0;
161 unsigned long offset; 156 unsigned long offset;
162 157
163 if (pipenum == 0) 158 if (pipenum == 0) {
164 ret = r8a66597_read(r8a66597, DCPCTR); 159 ret = r8a66597_read(r8a66597, DCPCTR);
165 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 160 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
166 offset = get_pipectr_addr(pipenum); 161 offset = get_pipectr_addr(pipenum);
167 ret = r8a66597_read(r8a66597, offset); 162 ret = r8a66597_read(r8a66597, offset);
168 } else 163 } else {
169 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 164 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
165 pipenum);
166 }
170 167
171 return ret; 168 return ret;
172} 169}
@@ -177,13 +174,63 @@ static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
177 174
178 pipe_stop(r8a66597, pipenum); 175 pipe_stop(r8a66597, pipenum);
179 176
180 if (pipenum == 0) 177 if (pipenum == 0) {
181 r8a66597_bset(r8a66597, SQCLR, DCPCTR); 178 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
182 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 179 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
183 offset = get_pipectr_addr(pipenum); 180 offset = get_pipectr_addr(pipenum);
184 r8a66597_bset(r8a66597, SQCLR, offset); 181 r8a66597_bset(r8a66597, SQCLR, offset);
185 } else 182 } else {
186 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum); 183 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
184 pipenum);
185 }
186}
187
188static void control_reg_sqset(struct r8a66597 *r8a66597, u16 pipenum)
189{
190 unsigned long offset;
191
192 pipe_stop(r8a66597, pipenum);
193
194 if (pipenum == 0) {
195 r8a66597_bset(r8a66597, SQSET, DCPCTR);
196 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
197 offset = get_pipectr_addr(pipenum);
198 r8a66597_bset(r8a66597, SQSET, offset);
199 } else {
200 dev_err(r8a66597_to_dev(r8a66597),
201 "unexpect pipe num(%d)\n", pipenum);
202 }
203}
204
205static u16 control_reg_sqmon(struct r8a66597 *r8a66597, u16 pipenum)
206{
207 unsigned long offset;
208
209 if (pipenum == 0) {
210 return r8a66597_read(r8a66597, DCPCTR) & SQMON;
211 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
212 offset = get_pipectr_addr(pipenum);
213 return r8a66597_read(r8a66597, offset) & SQMON;
214 } else {
215 dev_err(r8a66597_to_dev(r8a66597),
216 "unexpect pipe num(%d)\n", pipenum);
217 }
218
219 return 0;
220}
221
222static u16 save_usb_toggle(struct r8a66597 *r8a66597, u16 pipenum)
223{
224 return control_reg_sqmon(r8a66597, pipenum);
225}
226
227static void restore_usb_toggle(struct r8a66597 *r8a66597, u16 pipenum,
228 u16 toggle)
229{
230 if (toggle)
231 control_reg_sqset(r8a66597, pipenum);
232 else
233 control_reg_sqclr(r8a66597, pipenum);
187} 234}
188 235
189static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum) 236static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
@@ -222,18 +269,51 @@ static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
222 return MBW_16; 269 return MBW_16;
223} 270}
224 271
272static void r8a66597_change_curpipe(struct r8a66597 *r8a66597, u16 pipenum,
273 u16 isel, u16 fifosel)
274{
275 u16 tmp, mask, loop;
276 int i = 0;
277
278 if (!pipenum) {
279 mask = ISEL | CURPIPE;
280 loop = isel;
281 } else {
282 mask = CURPIPE;
283 loop = pipenum;
284 }
285 r8a66597_mdfy(r8a66597, loop, mask, fifosel);
286
287 do {
288 tmp = r8a66597_read(r8a66597, fifosel);
289 if (i++ > 1000000) {
290 dev_err(r8a66597_to_dev(r8a66597),
291 "r8a66597: register%x, loop %x "
292 "is timeout\n", fifosel, loop);
293 break;
294 }
295 ndelay(1);
296 } while ((tmp & mask) != loop);
297}
298
225static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum) 299static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
226{ 300{
227 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum]; 301 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
228 302
229 if (ep->use_dma) 303 if (ep->use_dma)
230 return; 304 r8a66597_bclr(r8a66597, DREQE, ep->fifosel);
231 305
232 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel); 306 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
233 307
234 ndelay(450); 308 ndelay(450);
235 309
236 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel); 310 if (r8a66597_is_sudmac(r8a66597) && ep->use_dma)
311 r8a66597_bclr(r8a66597, mbw_value(r8a66597), ep->fifosel);
312 else
313 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
314
315 if (ep->use_dma)
316 r8a66597_bset(r8a66597, DREQE, ep->fifosel);
237} 317}
238 318
239static int pipe_buffer_setting(struct r8a66597 *r8a66597, 319static int pipe_buffer_setting(struct r8a66597 *r8a66597,
@@ -297,17 +377,18 @@ static void pipe_buffer_release(struct r8a66597 *r8a66597,
297 if (info->pipe == 0) 377 if (info->pipe == 0)
298 return; 378 return;
299 379
300 if (is_bulk_pipe(info->pipe)) 380 if (is_bulk_pipe(info->pipe)) {
301 r8a66597->bulk--; 381 r8a66597->bulk--;
302 else if (is_interrupt_pipe(info->pipe)) 382 } else if (is_interrupt_pipe(info->pipe)) {
303 r8a66597->interrupt--; 383 r8a66597->interrupt--;
304 else if (is_isoc_pipe(info->pipe)) { 384 } else if (is_isoc_pipe(info->pipe)) {
305 r8a66597->isochronous--; 385 r8a66597->isochronous--;
306 if (info->type == R8A66597_BULK) 386 if (info->type == R8A66597_BULK)
307 r8a66597->bulk--; 387 r8a66597->bulk--;
308 } else 388 } else {
309 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n", 389 dev_err(r8a66597_to_dev(r8a66597),
310 info->pipe); 390 "ep_release: unexpect pipenum (%d)\n", info->pipe);
391 }
311} 392}
312 393
313static void pipe_initialize(struct r8a66597_ep *ep) 394static void pipe_initialize(struct r8a66597_ep *ep)
@@ -337,11 +418,17 @@ static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
337 ep->fifoaddr = CFIFO; 418 ep->fifoaddr = CFIFO;
338 ep->fifosel = CFIFOSEL; 419 ep->fifosel = CFIFOSEL;
339 ep->fifoctr = CFIFOCTR; 420 ep->fifoctr = CFIFOCTR;
340 ep->fifotrn = 0;
341 421
342 ep->pipectr = get_pipectr_addr(pipenum); 422 ep->pipectr = get_pipectr_addr(pipenum);
423 if (is_bulk_pipe(pipenum) || is_isoc_pipe(pipenum)) {
424 ep->pipetre = get_pipetre_addr(pipenum);
425 ep->pipetrn = get_pipetrn_addr(pipenum);
426 } else {
427 ep->pipetre = 0;
428 ep->pipetrn = 0;
429 }
343 ep->pipenum = pipenum; 430 ep->pipenum = pipenum;
344 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 431 ep->ep.maxpacket = usb_endpoint_maxp(desc);
345 r8a66597->pipenum2ep[pipenum] = ep; 432 r8a66597->pipenum2ep[pipenum] = ep;
346 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK] 433 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
347 = ep; 434 = ep;
@@ -381,7 +468,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
381 case USB_ENDPOINT_XFER_BULK: 468 case USB_ENDPOINT_XFER_BULK:
382 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) { 469 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
383 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) { 470 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
384 printk(KERN_ERR "bulk pipe is insufficient\n"); 471 dev_err(r8a66597_to_dev(r8a66597),
472 "bulk pipe is insufficient\n");
385 return -ENODEV; 473 return -ENODEV;
386 } else { 474 } else {
387 info.pipe = R8A66597_BASE_PIPENUM_ISOC 475 info.pipe = R8A66597_BASE_PIPENUM_ISOC
@@ -397,7 +485,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
397 break; 485 break;
398 case USB_ENDPOINT_XFER_INT: 486 case USB_ENDPOINT_XFER_INT:
399 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) { 487 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
400 printk(KERN_ERR "interrupt pipe is insufficient\n"); 488 dev_err(r8a66597_to_dev(r8a66597),
489 "interrupt pipe is insufficient\n");
401 return -ENODEV; 490 return -ENODEV;
402 } 491 }
403 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt; 492 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
@@ -406,7 +495,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
406 break; 495 break;
407 case USB_ENDPOINT_XFER_ISOC: 496 case USB_ENDPOINT_XFER_ISOC:
408 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) { 497 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
409 printk(KERN_ERR "isochronous pipe is insufficient\n"); 498 dev_err(r8a66597_to_dev(r8a66597),
499 "isochronous pipe is insufficient\n");
410 return -ENODEV; 500 return -ENODEV;
411 } 501 }
412 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous; 502 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
@@ -414,13 +504,13 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
414 counter = &r8a66597->isochronous; 504 counter = &r8a66597->isochronous;
415 break; 505 break;
416 default: 506 default:
417 printk(KERN_ERR "unexpect xfer type\n"); 507 dev_err(r8a66597_to_dev(r8a66597), "unexpect xfer type\n");
418 return -EINVAL; 508 return -EINVAL;
419 } 509 }
420 ep->type = info.type; 510 ep->type = info.type;
421 511
422 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 512 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
423 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 513 info.maxpacket = usb_endpoint_maxp(desc);
424 info.interval = desc->bInterval; 514 info.interval = desc->bInterval;
425 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 515 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
426 info.dir_in = 1; 516 info.dir_in = 1;
@@ -429,7 +519,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
429 519
430 ret = pipe_buffer_setting(r8a66597, &info); 520 ret = pipe_buffer_setting(r8a66597, &info);
431 if (ret < 0) { 521 if (ret < 0) {
432 printk(KERN_ERR "pipe_buffer_setting fail\n"); 522 dev_err(r8a66597_to_dev(r8a66597),
523 "pipe_buffer_setting fail\n");
433 return ret; 524 return ret;
434 } 525 }
435 526
@@ -495,6 +586,124 @@ static void start_ep0_write(struct r8a66597_ep *ep,
495 } 586 }
496} 587}
497 588
589static void disable_fifosel(struct r8a66597 *r8a66597, u16 pipenum,
590 u16 fifosel)
591{
592 u16 tmp;
593
594 tmp = r8a66597_read(r8a66597, fifosel) & CURPIPE;
595 if (tmp == pipenum)
596 r8a66597_change_curpipe(r8a66597, 0, 0, fifosel);
597}
598
599static void change_bfre_mode(struct r8a66597 *r8a66597, u16 pipenum,
600 int enable)
601{
602 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
603 u16 tmp, toggle;
604
605 /* check current BFRE bit */
606 r8a66597_write(r8a66597, pipenum, PIPESEL);
607 tmp = r8a66597_read(r8a66597, PIPECFG) & R8A66597_BFRE;
608 if ((enable && tmp) || (!enable && !tmp))
609 return;
610
611 /* change BFRE bit */
612 pipe_stop(r8a66597, pipenum);
613 disable_fifosel(r8a66597, pipenum, CFIFOSEL);
614 disable_fifosel(r8a66597, pipenum, D0FIFOSEL);
615 disable_fifosel(r8a66597, pipenum, D1FIFOSEL);
616
617 toggle = save_usb_toggle(r8a66597, pipenum);
618
619 r8a66597_write(r8a66597, pipenum, PIPESEL);
620 if (enable)
621 r8a66597_bset(r8a66597, R8A66597_BFRE, PIPECFG);
622 else
623 r8a66597_bclr(r8a66597, R8A66597_BFRE, PIPECFG);
624
625 /* initialize for internal BFRE flag */
626 r8a66597_bset(r8a66597, ACLRM, ep->pipectr);
627 r8a66597_bclr(r8a66597, ACLRM, ep->pipectr);
628
629 restore_usb_toggle(r8a66597, pipenum, toggle);
630}
631
632static int sudmac_alloc_channel(struct r8a66597 *r8a66597,
633 struct r8a66597_ep *ep,
634 struct r8a66597_request *req)
635{
636 struct r8a66597_dma *dma;
637
638 if (!r8a66597_is_sudmac(r8a66597))
639 return -ENODEV;
640
641 /* Check transfer type */
642 if (!is_bulk_pipe(ep->pipenum))
643 return -EIO;
644
645 if (r8a66597->dma.used)
646 return -EBUSY;
647
648 /* set SUDMAC parameters */
649 dma = &r8a66597->dma;
650 dma->used = 1;
651 if (ep->desc->bEndpointAddress & USB_DIR_IN) {
652 dma->dir = 1;
653 } else {
654 dma->dir = 0;
655 change_bfre_mode(r8a66597, ep->pipenum, 1);
656 }
657
658 /* set r8a66597_ep paramters */
659 ep->use_dma = 1;
660 ep->dma = dma;
661 ep->fifoaddr = D0FIFO;
662 ep->fifosel = D0FIFOSEL;
663 ep->fifoctr = D0FIFOCTR;
664
665 /* dma mapping */
666 req->req.dma = dma_map_single(r8a66597_to_dev(ep->r8a66597),
667 req->req.buf, req->req.length,
668 dma->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
669
670 return 0;
671}
672
673static void sudmac_free_channel(struct r8a66597 *r8a66597,
674 struct r8a66597_ep *ep,
675 struct r8a66597_request *req)
676{
677 if (!r8a66597_is_sudmac(r8a66597))
678 return;
679
680 dma_unmap_single(r8a66597_to_dev(ep->r8a66597),
681 req->req.dma, req->req.length,
682 ep->dma->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
683
684 r8a66597_bclr(r8a66597, DREQE, ep->fifosel);
685 r8a66597_change_curpipe(r8a66597, 0, 0, ep->fifosel);
686
687 ep->dma->used = 0;
688 ep->use_dma = 0;
689 ep->fifoaddr = CFIFO;
690 ep->fifosel = CFIFOSEL;
691 ep->fifoctr = CFIFOCTR;
692}
693
694static void sudmac_start(struct r8a66597 *r8a66597, struct r8a66597_ep *ep,
695 struct r8a66597_request *req)
696{
697 BUG_ON(req->req.length == 0);
698
699 r8a66597_sudmac_write(r8a66597, LBA_WAIT, CH0CFG);
700 r8a66597_sudmac_write(r8a66597, req->req.dma, CH0BA);
701 r8a66597_sudmac_write(r8a66597, req->req.length, CH0BBC);
702 r8a66597_sudmac_write(r8a66597, CH0ENDE, DINTCTRL);
703
704 r8a66597_sudmac_write(r8a66597, DEN, CH0DEN);
705}
706
498static void start_packet_write(struct r8a66597_ep *ep, 707static void start_packet_write(struct r8a66597_ep *ep,
499 struct r8a66597_request *req) 708 struct r8a66597_request *req)
500{ 709{
@@ -505,11 +714,29 @@ static void start_packet_write(struct r8a66597_ep *ep,
505 disable_irq_empty(r8a66597, ep->pipenum); 714 disable_irq_empty(r8a66597, ep->pipenum);
506 pipe_start(r8a66597, ep->pipenum); 715 pipe_start(r8a66597, ep->pipenum);
507 716
508 tmp = r8a66597_read(r8a66597, ep->fifoctr); 717 if (req->req.length == 0) {
509 if (unlikely((tmp & FRDY) == 0)) 718 transfer_complete(ep, req, 0);
510 pipe_irq_enable(r8a66597, ep->pipenum); 719 } else {
511 else 720 r8a66597_write(r8a66597, ~(1 << ep->pipenum), BRDYSTS);
512 irq_packet_write(ep, req); 721 if (sudmac_alloc_channel(r8a66597, ep, req) < 0) {
722 /* PIO mode */
723 pipe_change(r8a66597, ep->pipenum);
724 disable_irq_empty(r8a66597, ep->pipenum);
725 pipe_start(r8a66597, ep->pipenum);
726 tmp = r8a66597_read(r8a66597, ep->fifoctr);
727 if (unlikely((tmp & FRDY) == 0))
728 pipe_irq_enable(r8a66597, ep->pipenum);
729 else
730 irq_packet_write(ep, req);
731 } else {
732 /* DMA mode */
733 pipe_change(r8a66597, ep->pipenum);
734 disable_irq_nrdy(r8a66597, ep->pipenum);
735 pipe_start(r8a66597, ep->pipenum);
736 enable_irq_nrdy(r8a66597, ep->pipenum);
737 sudmac_start(r8a66597, ep, req);
738 }
739 }
513} 740}
514 741
515static void start_packet_read(struct r8a66597_ep *ep, 742static void start_packet_read(struct r8a66597_ep *ep,
@@ -524,17 +751,26 @@ static void start_packet_read(struct r8a66597_ep *ep,
524 pipe_start(r8a66597, pipenum); 751 pipe_start(r8a66597, pipenum);
525 pipe_irq_enable(r8a66597, pipenum); 752 pipe_irq_enable(r8a66597, pipenum);
526 } else { 753 } else {
527 if (ep->use_dma) { 754 pipe_stop(r8a66597, pipenum);
528 r8a66597_bset(r8a66597, TRCLR, ep->fifosel); 755 if (ep->pipetre) {
529 pipe_change(r8a66597, pipenum); 756 enable_irq_nrdy(r8a66597, pipenum);
530 r8a66597_bset(r8a66597, TRENB, ep->fifosel); 757 r8a66597_write(r8a66597, TRCLR, ep->pipetre);
531 r8a66597_write(r8a66597, 758 r8a66597_write(r8a66597,
532 (req->req.length + ep->ep.maxpacket - 1) 759 DIV_ROUND_UP(req->req.length, ep->ep.maxpacket),
533 / ep->ep.maxpacket, 760 ep->pipetrn);
534 ep->fifotrn); 761 r8a66597_bset(r8a66597, TRENB, ep->pipetre);
762 }
763
764 if (sudmac_alloc_channel(r8a66597, ep, req) < 0) {
765 /* PIO mode */
766 change_bfre_mode(r8a66597, ep->pipenum, 0);
767 pipe_start(r8a66597, pipenum); /* trigger once */
768 pipe_irq_enable(r8a66597, pipenum);
769 } else {
770 pipe_change(r8a66597, pipenum);
771 sudmac_start(r8a66597, ep, req);
772 pipe_start(r8a66597, pipenum); /* trigger once */
535 } 773 }
536 pipe_start(r8a66597, pipenum); /* trigger once */
537 pipe_irq_enable(r8a66597, pipenum);
538 } 774 }
539} 775}
540 776
@@ -564,7 +800,8 @@ static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
564 control_end(ep->r8a66597, 0); 800 control_end(ep->r8a66597, 0);
565 break; 801 break;
566 default: 802 default:
567 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq); 803 dev_err(r8a66597_to_dev(ep->r8a66597),
804 "start_ep0: unexpect ctsq(%x)\n", ctsq);
568 break; 805 break;
569 } 806 }
570} 807}
@@ -690,6 +927,9 @@ __acquires(r8a66597->lock)
690 if (!list_empty(&ep->queue)) 927 if (!list_empty(&ep->queue))
691 restart = 1; 928 restart = 1;
692 929
930 if (ep->use_dma)
931 sudmac_free_channel(ep->r8a66597, ep, req);
932
693 spin_unlock(&ep->r8a66597->lock); 933 spin_unlock(&ep->r8a66597->lock);
694 req->req.complete(&ep->ep, &req->req); 934 req->req.complete(&ep->ep, &req->req);
695 spin_lock(&ep->r8a66597->lock); 935 spin_lock(&ep->r8a66597->lock);
@@ -718,7 +958,8 @@ static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
718 do { 958 do {
719 tmp = r8a66597_read(r8a66597, ep->fifoctr); 959 tmp = r8a66597_read(r8a66597, ep->fifoctr);
720 if (i++ > 100000) { 960 if (i++ > 100000) {
721 printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus" 961 dev_err(r8a66597_to_dev(r8a66597),
962 "pipe0 is busy. maybe cpu i/o bus "
722 "conflict. please power off this controller."); 963 "conflict. please power off this controller.");
723 return; 964 return;
724 } 965 }
@@ -733,7 +974,7 @@ static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
733 /* write fifo */ 974 /* write fifo */
734 if (req->req.buf) { 975 if (req->req.buf) {
735 if (size > 0) 976 if (size > 0)
736 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size); 977 r8a66597_write_fifo(r8a66597, ep, buf, size);
737 if ((size == 0) || ((size % ep->ep.maxpacket) != 0)) 978 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
738 r8a66597_bset(r8a66597, BVAL, ep->fifoctr); 979 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
739 } 980 }
@@ -769,7 +1010,8 @@ static void irq_packet_write(struct r8a66597_ep *ep,
769 if (unlikely((tmp & FRDY) == 0)) { 1010 if (unlikely((tmp & FRDY) == 0)) {
770 pipe_stop(r8a66597, pipenum); 1011 pipe_stop(r8a66597, pipenum);
771 pipe_irq_disable(r8a66597, pipenum); 1012 pipe_irq_disable(r8a66597, pipenum);
772 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum); 1013 dev_err(r8a66597_to_dev(r8a66597),
1014 "write fifo not ready. pipnum=%d\n", pipenum);
773 return; 1015 return;
774 } 1016 }
775 1017
@@ -780,7 +1022,7 @@ static void irq_packet_write(struct r8a66597_ep *ep,
780 1022
781 /* write fifo */ 1023 /* write fifo */
782 if (req->req.buf) { 1024 if (req->req.buf) {
783 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size); 1025 r8a66597_write_fifo(r8a66597, ep, buf, size);
784 if ((size == 0) 1026 if ((size == 0)
785 || ((size % ep->ep.maxpacket) != 0) 1027 || ((size % ep->ep.maxpacket) != 0)
786 || ((bufsize != ep->ep.maxpacket) 1028 || ((bufsize != ep->ep.maxpacket)
@@ -819,7 +1061,7 @@ static void irq_packet_read(struct r8a66597_ep *ep,
819 req->req.status = -EPIPE; 1061 req->req.status = -EPIPE;
820 pipe_stop(r8a66597, pipenum); 1062 pipe_stop(r8a66597, pipenum);
821 pipe_irq_disable(r8a66597, pipenum); 1063 pipe_irq_disable(r8a66597, pipenum);
822 printk(KERN_ERR "read fifo not ready"); 1064 dev_err(r8a66597_to_dev(r8a66597), "read fifo not ready");
823 return; 1065 return;
824 } 1066 }
825 1067
@@ -1095,7 +1337,7 @@ static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1095 break; 1337 break;
1096 default: 1338 default:
1097 r8a66597->gadget.speed = USB_SPEED_UNKNOWN; 1339 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1098 printk(KERN_ERR "USB speed unknown\n"); 1340 dev_err(r8a66597_to_dev(r8a66597), "USB speed unknown\n");
1099 } 1341 }
1100} 1342}
1101 1343
@@ -1158,11 +1400,71 @@ __acquires(r8a66597->lock)
1158 control_end(r8a66597, 0); 1400 control_end(r8a66597, 0);
1159 break; 1401 break;
1160 default: 1402 default:
1161 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq); 1403 dev_err(r8a66597_to_dev(r8a66597),
1404 "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1162 break; 1405 break;
1163 } 1406 }
1164} 1407}
1165 1408
1409static void sudmac_finish(struct r8a66597 *r8a66597, struct r8a66597_ep *ep)
1410{
1411 u16 pipenum;
1412 struct r8a66597_request *req;
1413 u32 len;
1414 int i = 0;
1415
1416 pipenum = ep->pipenum;
1417 pipe_change(r8a66597, pipenum);
1418
1419 while (!(r8a66597_read(r8a66597, ep->fifoctr) & FRDY)) {
1420 udelay(1);
1421 if (unlikely(i++ >= 10000)) { /* timeout = 10 msec */
1422 dev_err(r8a66597_to_dev(r8a66597),
1423 "%s: FRDY was not set (%d)\n",
1424 __func__, pipenum);
1425 return;
1426 }
1427 }
1428
1429 r8a66597_bset(r8a66597, BCLR, ep->fifoctr);
1430 req = get_request_from_ep(ep);
1431
1432 /* prepare parameters */
1433 len = r8a66597_sudmac_read(r8a66597, CH0CBC);
1434 req->req.actual += len;
1435
1436 /* clear */
1437 r8a66597_sudmac_write(r8a66597, CH0STCLR, DSTSCLR);
1438
1439 /* check transfer finish */
1440 if ((!req->req.zero && (req->req.actual == req->req.length))
1441 || (len % ep->ep.maxpacket)) {
1442 if (ep->dma->dir) {
1443 disable_irq_ready(r8a66597, pipenum);
1444 enable_irq_empty(r8a66597, pipenum);
1445 } else {
1446 /* Clear the interrupt flag for next transfer */
1447 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
1448 transfer_complete(ep, req, 0);
1449 }
1450 }
1451}
1452
1453static void r8a66597_sudmac_irq(struct r8a66597 *r8a66597)
1454{
1455 u32 irqsts;
1456 struct r8a66597_ep *ep;
1457 u16 pipenum;
1458
1459 irqsts = r8a66597_sudmac_read(r8a66597, DINTSTS);
1460 if (irqsts & CH0ENDS) {
1461 r8a66597_sudmac_write(r8a66597, CH0ENDC, DINTSTSCLR);
1462 pipenum = (r8a66597_read(r8a66597, D0FIFOSEL) & CURPIPE);
1463 ep = r8a66597->pipenum2ep[pipenum];
1464 sudmac_finish(r8a66597, ep);
1465 }
1466}
1467
1166static irqreturn_t r8a66597_irq(int irq, void *_r8a66597) 1468static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1167{ 1469{
1168 struct r8a66597 *r8a66597 = _r8a66597; 1470 struct r8a66597 *r8a66597 = _r8a66597;
@@ -1173,6 +1475,9 @@ static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1173 u16 savepipe; 1475 u16 savepipe;
1174 u16 mask0; 1476 u16 mask0;
1175 1477
1478 if (r8a66597_is_sudmac(r8a66597))
1479 r8a66597_sudmac_irq(r8a66597);
1480
1176 spin_lock(&r8a66597->lock); 1481 spin_lock(&r8a66597->lock);
1177 1482
1178 intsts0 = r8a66597_read(r8a66597, INTSTS0); 1483 intsts0 = r8a66597_read(r8a66597, INTSTS0);
@@ -1413,6 +1718,8 @@ static void r8a66597_fifo_flush(struct usb_ep *_ep)
1413 if (list_empty(&ep->queue) && !ep->busy) { 1718 if (list_empty(&ep->queue) && !ep->busy) {
1414 pipe_stop(ep->r8a66597, ep->pipenum); 1719 pipe_stop(ep->r8a66597, ep->pipenum);
1415 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr); 1720 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1721 r8a66597_write(ep->r8a66597, ACLRM, ep->pipectr);
1722 r8a66597_write(ep->r8a66597, 0, ep->pipectr);
1416 } 1723 }
1417 spin_unlock_irqrestore(&ep->r8a66597->lock, flags); 1724 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1418} 1725}
@@ -1433,41 +1740,20 @@ static struct usb_ep_ops r8a66597_ep_ops = {
1433}; 1740};
1434 1741
1435/*-------------------------------------------------------------------------*/ 1742/*-------------------------------------------------------------------------*/
1436static struct r8a66597 *the_controller; 1743static int r8a66597_start(struct usb_gadget *gadget,
1437 1744 struct usb_gadget_driver *driver)
1438static int r8a66597_start(struct usb_gadget_driver *driver,
1439 int (*bind)(struct usb_gadget *))
1440{ 1745{
1441 struct r8a66597 *r8a66597 = the_controller; 1746 struct r8a66597 *r8a66597 = gadget_to_r8a66597(gadget);
1442 int retval;
1443 1747
1444 if (!driver 1748 if (!driver
1445 || driver->speed != USB_SPEED_HIGH 1749 || driver->speed != USB_SPEED_HIGH
1446 || !bind
1447 || !driver->setup) 1750 || !driver->setup)
1448 return -EINVAL; 1751 return -EINVAL;
1449 if (!r8a66597) 1752 if (!r8a66597)
1450 return -ENODEV; 1753 return -ENODEV;
1451 if (r8a66597->driver)
1452 return -EBUSY;
1453 1754
1454 /* hook up the driver */ 1755 /* hook up the driver */
1455 driver->driver.bus = NULL;
1456 r8a66597->driver = driver; 1756 r8a66597->driver = driver;
1457 r8a66597->gadget.dev.driver = &driver->driver;
1458
1459 retval = device_add(&r8a66597->gadget.dev);
1460 if (retval) {
1461 printk(KERN_ERR "device_add error (%d)\n", retval);
1462 goto error;
1463 }
1464
1465 retval = bind(&r8a66597->gadget);
1466 if (retval) {
1467 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1468 device_del(&r8a66597->gadget.dev);
1469 goto error;
1470 }
1471 1757
1472 init_controller(r8a66597); 1758 init_controller(r8a66597);
1473 r8a66597_bset(r8a66597, VBSE, INTENB0); 1759 r8a66597_bset(r8a66597, VBSE, INTENB0);
@@ -1481,32 +1767,19 @@ static int r8a66597_start(struct usb_gadget_driver *driver,
1481 } 1767 }
1482 1768
1483 return 0; 1769 return 0;
1484
1485error:
1486 r8a66597->driver = NULL;
1487 r8a66597->gadget.dev.driver = NULL;
1488
1489 return retval;
1490} 1770}
1491 1771
1492static int r8a66597_stop(struct usb_gadget_driver *driver) 1772static int r8a66597_stop(struct usb_gadget *gadget,
1773 struct usb_gadget_driver *driver)
1493{ 1774{
1494 struct r8a66597 *r8a66597 = the_controller; 1775 struct r8a66597 *r8a66597 = gadget_to_r8a66597(gadget);
1495 unsigned long flags; 1776 unsigned long flags;
1496 1777
1497 if (driver != r8a66597->driver || !driver->unbind)
1498 return -EINVAL;
1499
1500 spin_lock_irqsave(&r8a66597->lock, flags); 1778 spin_lock_irqsave(&r8a66597->lock, flags);
1501 if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1502 r8a66597_usb_disconnect(r8a66597);
1503 r8a66597_bclr(r8a66597, VBSE, INTENB0); 1779 r8a66597_bclr(r8a66597, VBSE, INTENB0);
1504 disable_controller(r8a66597); 1780 disable_controller(r8a66597);
1505 spin_unlock_irqrestore(&r8a66597->lock, flags); 1781 spin_unlock_irqrestore(&r8a66597->lock, flags);
1506 1782
1507 driver->unbind(&r8a66597->gadget);
1508
1509 device_del(&r8a66597->gadget.dev);
1510 r8a66597->driver = NULL; 1783 r8a66597->driver = NULL;
1511 return 0; 1784 return 0;
1512} 1785}
@@ -1535,8 +1808,8 @@ static int r8a66597_pullup(struct usb_gadget *gadget, int is_on)
1535 1808
1536static struct usb_gadget_ops r8a66597_gadget_ops = { 1809static struct usb_gadget_ops r8a66597_gadget_ops = {
1537 .get_frame = r8a66597_get_frame, 1810 .get_frame = r8a66597_get_frame,
1538 .start = r8a66597_start, 1811 .udc_start = r8a66597_start,
1539 .stop = r8a66597_stop, 1812 .udc_stop = r8a66597_stop,
1540 .pullup = r8a66597_pullup, 1813 .pullup = r8a66597_pullup,
1541}; 1814};
1542 1815
@@ -1547,6 +1820,8 @@ static int __exit r8a66597_remove(struct platform_device *pdev)
1547 usb_del_gadget_udc(&r8a66597->gadget); 1820 usb_del_gadget_udc(&r8a66597->gadget);
1548 del_timer_sync(&r8a66597->timer); 1821 del_timer_sync(&r8a66597->timer);
1549 iounmap(r8a66597->reg); 1822 iounmap(r8a66597->reg);
1823 if (r8a66597->pdata->sudmac)
1824 iounmap(r8a66597->sudmac_reg);
1550 free_irq(platform_get_irq(pdev, 0), r8a66597); 1825 free_irq(platform_get_irq(pdev, 0), r8a66597);
1551 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req); 1826 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1552#ifdef CONFIG_HAVE_CLK 1827#ifdef CONFIG_HAVE_CLK
@@ -1555,6 +1830,7 @@ static int __exit r8a66597_remove(struct platform_device *pdev)
1555 clk_put(r8a66597->clk); 1830 clk_put(r8a66597->clk);
1556 } 1831 }
1557#endif 1832#endif
1833 device_unregister(&r8a66597->gadget.dev);
1558 kfree(r8a66597); 1834 kfree(r8a66597);
1559 return 0; 1835 return 0;
1560} 1836}
@@ -1563,6 +1839,26 @@ static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1563{ 1839{
1564} 1840}
1565 1841
1842static int __init r8a66597_sudmac_ioremap(struct r8a66597 *r8a66597,
1843 struct platform_device *pdev)
1844{
1845 struct resource *res;
1846
1847 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sudmac");
1848 if (!res) {
1849 dev_err(&pdev->dev, "platform_get_resource error(sudmac).\n");
1850 return -ENODEV;
1851 }
1852
1853 r8a66597->sudmac_reg = ioremap(res->start, resource_size(res));
1854 if (r8a66597->sudmac_reg == NULL) {
1855 dev_err(&pdev->dev, "ioremap error(sudmac).\n");
1856 return -ENOMEM;
1857 }
1858
1859 return 0;
1860}
1861
1566static int __init r8a66597_probe(struct platform_device *pdev) 1862static int __init r8a66597_probe(struct platform_device *pdev)
1567{ 1863{
1568#ifdef CONFIG_HAVE_CLK 1864#ifdef CONFIG_HAVE_CLK
@@ -1579,7 +1875,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1579 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1875 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1580 if (!res) { 1876 if (!res) {
1581 ret = -ENODEV; 1877 ret = -ENODEV;
1582 printk(KERN_ERR "platform_get_resource error.\n"); 1878 dev_err(&pdev->dev, "platform_get_resource error.\n");
1583 goto clean_up; 1879 goto clean_up;
1584 } 1880 }
1585 1881
@@ -1589,14 +1885,14 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1589 1885
1590 if (irq < 0) { 1886 if (irq < 0) {
1591 ret = -ENODEV; 1887 ret = -ENODEV;
1592 printk(KERN_ERR "platform_get_irq error.\n"); 1888 dev_err(&pdev->dev, "platform_get_irq error.\n");
1593 goto clean_up; 1889 goto clean_up;
1594 } 1890 }
1595 1891
1596 reg = ioremap(res->start, resource_size(res)); 1892 reg = ioremap(res->start, resource_size(res));
1597 if (reg == NULL) { 1893 if (reg == NULL) {
1598 ret = -ENOMEM; 1894 ret = -ENOMEM;
1599 printk(KERN_ERR "ioremap error.\n"); 1895 dev_err(&pdev->dev, "ioremap error.\n");
1600 goto clean_up; 1896 goto clean_up;
1601 } 1897 }
1602 1898
@@ -1604,7 +1900,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1604 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL); 1900 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1605 if (r8a66597 == NULL) { 1901 if (r8a66597 == NULL) {
1606 ret = -ENOMEM; 1902 ret = -ENOMEM;
1607 printk(KERN_ERR "kzalloc error\n"); 1903 dev_err(&pdev->dev, "kzalloc error\n");
1608 goto clean_up; 1904 goto clean_up;
1609 } 1905 }
1610 1906
@@ -1614,13 +1910,17 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1614 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW; 1910 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1615 1911
1616 r8a66597->gadget.ops = &r8a66597_gadget_ops; 1912 r8a66597->gadget.ops = &r8a66597_gadget_ops;
1617 device_initialize(&r8a66597->gadget.dev);
1618 dev_set_name(&r8a66597->gadget.dev, "gadget"); 1913 dev_set_name(&r8a66597->gadget.dev, "gadget");
1619 r8a66597->gadget.is_dualspeed = 1; 1914 r8a66597->gadget.is_dualspeed = 1;
1620 r8a66597->gadget.dev.parent = &pdev->dev; 1915 r8a66597->gadget.dev.parent = &pdev->dev;
1621 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask; 1916 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1622 r8a66597->gadget.dev.release = pdev->dev.release; 1917 r8a66597->gadget.dev.release = pdev->dev.release;
1623 r8a66597->gadget.name = udc_name; 1918 r8a66597->gadget.name = udc_name;
1919 ret = device_register(&r8a66597->gadget.dev);
1920 if (ret < 0) {
1921 dev_err(&pdev->dev, "device_register failed\n");
1922 goto clean_up;
1923 }
1624 1924
1625 init_timer(&r8a66597->timer); 1925 init_timer(&r8a66597->timer);
1626 r8a66597->timer.function = r8a66597_timer; 1926 r8a66597->timer.function = r8a66597_timer;
@@ -1635,18 +1935,23 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1635 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", 1935 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1636 clk_name); 1936 clk_name);
1637 ret = PTR_ERR(r8a66597->clk); 1937 ret = PTR_ERR(r8a66597->clk);
1638 goto clean_up; 1938 goto clean_up_dev;
1639 } 1939 }
1640 clk_enable(r8a66597->clk); 1940 clk_enable(r8a66597->clk);
1641 } 1941 }
1642#endif 1942#endif
1943 if (r8a66597->pdata->sudmac) {
1944 ret = r8a66597_sudmac_ioremap(r8a66597, pdev);
1945 if (ret < 0)
1946 goto clean_up2;
1947 }
1643 1948
1644 disable_controller(r8a66597); /* make sure controller is disabled */ 1949 disable_controller(r8a66597); /* make sure controller is disabled */
1645 1950
1646 ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED, 1951 ret = request_irq(irq, r8a66597_irq, IRQF_SHARED,
1647 udc_name, r8a66597); 1952 udc_name, r8a66597);
1648 if (ret < 0) { 1953 if (ret < 0) {
1649 printk(KERN_ERR "request_irq error (%d)\n", ret); 1954 dev_err(&pdev->dev, "request_irq error (%d)\n", ret);
1650 goto clean_up2; 1955 goto clean_up2;
1651 } 1956 }
1652 1957
@@ -1672,13 +1977,10 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1672 r8a66597->ep[0].fifoaddr = CFIFO; 1977 r8a66597->ep[0].fifoaddr = CFIFO;
1673 r8a66597->ep[0].fifosel = CFIFOSEL; 1978 r8a66597->ep[0].fifosel = CFIFOSEL;
1674 r8a66597->ep[0].fifoctr = CFIFOCTR; 1979 r8a66597->ep[0].fifoctr = CFIFOCTR;
1675 r8a66597->ep[0].fifotrn = 0;
1676 r8a66597->ep[0].pipectr = get_pipectr_addr(0); 1980 r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1677 r8a66597->pipenum2ep[0] = &r8a66597->ep[0]; 1981 r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1678 r8a66597->epaddr2ep[0] = &r8a66597->ep[0]; 1982 r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1679 1983
1680 the_controller = r8a66597;
1681
1682 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep, 1984 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1683 GFP_KERNEL); 1985 GFP_KERNEL);
1684 if (r8a66597->ep0_req == NULL) 1986 if (r8a66597->ep0_req == NULL)
@@ -1702,9 +2004,13 @@ clean_up2:
1702 clk_disable(r8a66597->clk); 2004 clk_disable(r8a66597->clk);
1703 clk_put(r8a66597->clk); 2005 clk_put(r8a66597->clk);
1704 } 2006 }
2007clean_up_dev:
1705#endif 2008#endif
2009 device_unregister(&r8a66597->gadget.dev);
1706clean_up: 2010clean_up:
1707 if (r8a66597) { 2011 if (r8a66597) {
2012 if (r8a66597->sudmac_reg)
2013 iounmap(r8a66597->sudmac_reg);
1708 if (r8a66597->ep0_req) 2014 if (r8a66597->ep0_req)
1709 r8a66597_free_request(&r8a66597->ep[0].ep, 2015 r8a66597_free_request(&r8a66597->ep[0].ep,
1710 r8a66597->ep0_req); 2016 r8a66597->ep0_req);
diff --git a/drivers/usb/gadget/r8a66597-udc.h b/drivers/usb/gadget/r8a66597-udc.h
index 503f766c23a7..8e3de61cd4b8 100644
--- a/drivers/usb/gadget/r8a66597-udc.h
+++ b/drivers/usb/gadget/r8a66597-udc.h
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */ 11 */
22 12
23#ifndef __R8A66597_H__ 13#ifndef __R8A66597_H__
@@ -53,6 +43,7 @@
53 ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \ 43 ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \
54 (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC))) 44 (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC)))
55 45
46#define r8a66597_is_sudmac(r8a66597) (r8a66597->pdata->sudmac)
56struct r8a66597_pipe_info { 47struct r8a66597_pipe_info {
57 u16 pipe; 48 u16 pipe;
58 u16 epnum; 49 u16 epnum;
@@ -70,6 +61,7 @@ struct r8a66597_request {
70struct r8a66597_ep { 61struct r8a66597_ep {
71 struct usb_ep ep; 62 struct usb_ep ep;
72 struct r8a66597 *r8a66597; 63 struct r8a66597 *r8a66597;
64 struct r8a66597_dma *dma;
73 65
74 struct list_head queue; 66 struct list_head queue;
75 unsigned busy:1; 67 unsigned busy:1;
@@ -85,13 +77,20 @@ struct r8a66597_ep {
85 unsigned char fifoaddr; 77 unsigned char fifoaddr;
86 unsigned char fifosel; 78 unsigned char fifosel;
87 unsigned char fifoctr; 79 unsigned char fifoctr;
88 unsigned char fifotrn;
89 unsigned char pipectr; 80 unsigned char pipectr;
81 unsigned char pipetre;
82 unsigned char pipetrn;
83};
84
85struct r8a66597_dma {
86 unsigned used:1;
87 unsigned dir:1; /* 1 = IN(write), 0 = OUT(read) */
90}; 88};
91 89
92struct r8a66597 { 90struct r8a66597 {
93 spinlock_t lock; 91 spinlock_t lock;
94 void __iomem *reg; 92 void __iomem *reg;
93 void __iomem *sudmac_reg;
95 94
96#ifdef CONFIG_HAVE_CLK 95#ifdef CONFIG_HAVE_CLK
97 struct clk *clk; 96 struct clk *clk;
@@ -104,6 +103,7 @@ struct r8a66597 {
104 struct r8a66597_ep ep[R8A66597_MAX_NUM_PIPE]; 103 struct r8a66597_ep ep[R8A66597_MAX_NUM_PIPE];
105 struct r8a66597_ep *pipenum2ep[R8A66597_MAX_NUM_PIPE]; 104 struct r8a66597_ep *pipenum2ep[R8A66597_MAX_NUM_PIPE];
106 struct r8a66597_ep *epaddr2ep[16]; 105 struct r8a66597_ep *epaddr2ep[16];
106 struct r8a66597_dma dma;
107 107
108 struct timer_list timer; 108 struct timer_list timer;
109 struct usb_request *ep0_req; /* for internal request */ 109 struct usb_request *ep0_req; /* for internal request */
@@ -124,6 +124,7 @@ struct r8a66597 {
124#define gadget_to_r8a66597(_gadget) \ 124#define gadget_to_r8a66597(_gadget) \
125 container_of(_gadget, struct r8a66597, gadget) 125 container_of(_gadget, struct r8a66597, gadget)
126#define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget) 126#define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
127#define r8a66597_to_dev(r8a66597) (r8a66597->gadget.dev.parent)
127 128
128static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset) 129static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
129{ 130{
@@ -182,12 +183,27 @@ static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
182 iowrite16(val, r8a66597->reg + offset); 183 iowrite16(val, r8a66597->reg + offset);
183} 184}
184 185
186static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
187 u16 val, u16 pat, unsigned long offset)
188{
189 u16 tmp;
190 tmp = r8a66597_read(r8a66597, offset);
191 tmp = tmp & (~pat);
192 tmp = tmp | val;
193 r8a66597_write(r8a66597, tmp, offset);
194}
195
196#define r8a66597_bclr(r8a66597, val, offset) \
197 r8a66597_mdfy(r8a66597, 0, val, offset)
198#define r8a66597_bset(r8a66597, val, offset) \
199 r8a66597_mdfy(r8a66597, val, 0, offset)
200
185static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597, 201static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
186 unsigned long offset, 202 struct r8a66597_ep *ep,
187 unsigned char *buf, 203 unsigned char *buf,
188 int len) 204 int len)
189{ 205{
190 void __iomem *fifoaddr = r8a66597->reg + offset; 206 void __iomem *fifoaddr = r8a66597->reg + ep->fifoaddr;
191 int adj = 0; 207 int adj = 0;
192 int i; 208 int i;
193 209
@@ -215,18 +231,12 @@ static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
215 adj = 0x01; /* 16-bit wide */ 231 adj = 0x01; /* 16-bit wide */
216 } 232 }
217 233
234 if (r8a66597->pdata->wr0_shorted_to_wr1)
235 r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
218 for (i = 0; i < len; i++) 236 for (i = 0; i < len; i++)
219 iowrite8(buf[i], fifoaddr + adj - (i & adj)); 237 iowrite8(buf[i], fifoaddr + adj - (i & adj));
220} 238 if (r8a66597->pdata->wr0_shorted_to_wr1)
221 239 r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
222static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
223 u16 val, u16 pat, unsigned long offset)
224{
225 u16 tmp;
226 tmp = r8a66597_read(r8a66597, offset);
227 tmp = tmp & (~pat);
228 tmp = tmp | val;
229 r8a66597_write(r8a66597, tmp, offset);
230} 240}
231 241
232static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata) 242static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
@@ -251,12 +261,21 @@ static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
251 return clock; 261 return clock;
252} 262}
253 263
254#define r8a66597_bclr(r8a66597, val, offset) \ 264static inline u32 r8a66597_sudmac_read(struct r8a66597 *r8a66597,
255 r8a66597_mdfy(r8a66597, 0, val, offset) 265 unsigned long offset)
256#define r8a66597_bset(r8a66597, val, offset) \ 266{
257 r8a66597_mdfy(r8a66597, val, 0, offset) 267 return ioread32(r8a66597->sudmac_reg + offset);
268}
269
270static inline void r8a66597_sudmac_write(struct r8a66597 *r8a66597, u32 val,
271 unsigned long offset)
272{
273 iowrite32(val, r8a66597->sudmac_reg + offset);
274}
258 275
259#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2) 276#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
277#define get_pipetre_addr(pipenum) (PIPE1TRE + (pipenum - 1) * 4)
278#define get_pipetrn_addr(pipenum) (PIPE1TRN + (pipenum - 1) * 4)
260 279
261#define enable_irq_ready(r8a66597, pipenum) \ 280#define enable_irq_ready(r8a66597, pipenum) \
262 enable_pipe_irq(r8a66597, pipenum, BRDYENB) 281 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 8bdee67ce09a..a552453dc946 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -1951,30 +1951,26 @@ static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
1951 case S3C_DSTS_EnumSpd_FS: 1951 case S3C_DSTS_EnumSpd_FS:
1952 case S3C_DSTS_EnumSpd_FS48: 1952 case S3C_DSTS_EnumSpd_FS48:
1953 hsotg->gadget.speed = USB_SPEED_FULL; 1953 hsotg->gadget.speed = USB_SPEED_FULL;
1954 dev_info(hsotg->dev, "new device is full-speed\n");
1955
1956 ep0_mps = EP0_MPS_LIMIT; 1954 ep0_mps = EP0_MPS_LIMIT;
1957 ep_mps = 64; 1955 ep_mps = 64;
1958 break; 1956 break;
1959 1957
1960 case S3C_DSTS_EnumSpd_HS: 1958 case S3C_DSTS_EnumSpd_HS:
1961 dev_info(hsotg->dev, "new device is high-speed\n");
1962 hsotg->gadget.speed = USB_SPEED_HIGH; 1959 hsotg->gadget.speed = USB_SPEED_HIGH;
1963
1964 ep0_mps = EP0_MPS_LIMIT; 1960 ep0_mps = EP0_MPS_LIMIT;
1965 ep_mps = 512; 1961 ep_mps = 512;
1966 break; 1962 break;
1967 1963
1968 case S3C_DSTS_EnumSpd_LS: 1964 case S3C_DSTS_EnumSpd_LS:
1969 hsotg->gadget.speed = USB_SPEED_LOW; 1965 hsotg->gadget.speed = USB_SPEED_LOW;
1970 dev_info(hsotg->dev, "new device is low-speed\n");
1971
1972 /* note, we don't actually support LS in this driver at the 1966 /* note, we don't actually support LS in this driver at the
1973 * moment, and the documentation seems to imply that it isn't 1967 * moment, and the documentation seems to imply that it isn't
1974 * supported by the PHYs on some of the devices. 1968 * supported by the PHYs on some of the devices.
1975 */ 1969 */
1976 break; 1970 break;
1977 } 1971 }
1972 dev_info(hsotg->dev, "new device is %s\n",
1973 usb_speed_string(hsotg->gadget.speed));
1978 1974
1979 /* we should now know the maximum packet size for an 1975 /* we should now know the maximum packet size for an
1980 * endpoint, so set the endpoints to a default value. */ 1976 * endpoint, so set the endpoints to a default value. */
@@ -2297,7 +2293,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2297 return -EINVAL; 2293 return -EINVAL;
2298 } 2294 }
2299 2295
2300 mps = le16_to_cpu(desc->wMaxPacketSize); 2296 mps = usb_endpoint_maxp(desc);
2301 2297
2302 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */ 2298 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2303 2299
diff --git a/drivers/usb/gadget/s3c-hsudc.c b/drivers/usb/gadget/s3c-hsudc.c
index 3fa717c5f4bc..8d54f893cefe 100644
--- a/drivers/usb/gadget/s3c-hsudc.c
+++ b/drivers/usb/gadget/s3c-hsudc.c
@@ -26,6 +26,7 @@
26#include <linux/clk.h> 26#include <linux/clk.h>
27#include <linux/usb/ch9.h> 27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h> 28#include <linux/usb/gadget.h>
29#include <linux/usb/otg.h>
29#include <linux/prefetch.h> 30#include <linux/prefetch.h>
30 31
31#include <mach/regs-s3c2443-clock.h> 32#include <mach/regs-s3c2443-clock.h>
@@ -137,6 +138,7 @@ struct s3c_hsudc {
137 struct usb_gadget_driver *driver; 138 struct usb_gadget_driver *driver;
138 struct device *dev; 139 struct device *dev;
139 struct s3c24xx_hsudc_platdata *pd; 140 struct s3c24xx_hsudc_platdata *pd;
141 struct otg_transceiver *transceiver;
140 spinlock_t lock; 142 spinlock_t lock;
141 void __iomem *regs; 143 void __iomem *regs;
142 struct resource *mem_rsrc; 144 struct resource *mem_rsrc;
@@ -759,11 +761,11 @@ static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
759 if (!_ep || !desc || hsep->desc || _ep->name == ep0name 761 if (!_ep || !desc || hsep->desc || _ep->name == ep0name
760 || desc->bDescriptorType != USB_DT_ENDPOINT 762 || desc->bDescriptorType != USB_DT_ENDPOINT
761 || hsep->bEndpointAddress != desc->bEndpointAddress 763 || hsep->bEndpointAddress != desc->bEndpointAddress
762 || ep_maxpacket(hsep) < le16_to_cpu(desc->wMaxPacketSize)) 764 || ep_maxpacket(hsep) < usb_endpoint_maxp(desc))
763 return -EINVAL; 765 return -EINVAL;
764 766
765 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK 767 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
766 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(hsep)) 768 && usb_endpoint_maxp(desc) != ep_maxpacket(hsep))
767 || !desc->wMaxPacketSize) 769 || !desc->wMaxPacketSize)
768 return -ERANGE; 770 return -ERANGE;
769 771
@@ -779,7 +781,7 @@ static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
779 781
780 hsep->stopped = hsep->wedge = 0; 782 hsep->stopped = hsep->wedge = 0;
781 hsep->desc = desc; 783 hsep->desc = desc;
782 hsep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 784 hsep->ep.maxpacket = usb_endpoint_maxp(desc);
783 785
784 s3c_hsudc_set_halt(_ep, 0); 786 s3c_hsudc_set_halt(_ep, 0);
785 __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER); 787 __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
@@ -1171,6 +1173,22 @@ static int s3c_hsudc_start(struct usb_gadget_driver *driver,
1171 return ret; 1173 return ret;
1172 } 1174 }
1173 1175
1176 /* connect to bus through transceiver */
1177 if (hsudc->transceiver) {
1178 ret = otg_set_peripheral(hsudc->transceiver, &hsudc->gadget);
1179 if (ret) {
1180 dev_err(hsudc->dev, "%s: can't bind to transceiver\n",
1181 hsudc->gadget.name);
1182 driver->unbind(&hsudc->gadget);
1183
1184 device_del(&hsudc->gadget.dev);
1185
1186 hsudc->driver = NULL;
1187 hsudc->gadget.dev.driver = NULL;
1188 return ret;
1189 }
1190 }
1191
1174 enable_irq(hsudc->irq); 1192 enable_irq(hsudc->irq);
1175 dev_info(hsudc->dev, "bound driver %s\n", driver->driver.name); 1193 dev_info(hsudc->dev, "bound driver %s\n", driver->driver.name);
1176 1194
@@ -1201,6 +1219,9 @@ static int s3c_hsudc_stop(struct usb_gadget_driver *driver)
1201 s3c_hsudc_stop_activity(hsudc, driver); 1219 s3c_hsudc_stop_activity(hsudc, driver);
1202 spin_unlock_irqrestore(&hsudc->lock, flags); 1220 spin_unlock_irqrestore(&hsudc->lock, flags);
1203 1221
1222 if (hsudc->transceiver)
1223 (void) otg_set_peripheral(hsudc->transceiver, NULL);
1224
1204 driver->unbind(&hsudc->gadget); 1225 driver->unbind(&hsudc->gadget);
1205 device_del(&hsudc->gadget.dev); 1226 device_del(&hsudc->gadget.dev);
1206 disable_irq(hsudc->irq); 1227 disable_irq(hsudc->irq);
@@ -1220,10 +1241,24 @@ static int s3c_hsudc_gadget_getframe(struct usb_gadget *gadget)
1220 return s3c_hsudc_read_frameno(to_hsudc(gadget)); 1241 return s3c_hsudc_read_frameno(to_hsudc(gadget));
1221} 1242}
1222 1243
1244static int s3c_hsudc_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1245{
1246 struct s3c_hsudc *hsudc = the_controller;
1247
1248 if (!hsudc)
1249 return -ENODEV;
1250
1251 if (hsudc->transceiver)
1252 return otg_set_power(hsudc->transceiver, mA);
1253
1254 return -EOPNOTSUPP;
1255}
1256
1223static struct usb_gadget_ops s3c_hsudc_gadget_ops = { 1257static struct usb_gadget_ops s3c_hsudc_gadget_ops = {
1224 .get_frame = s3c_hsudc_gadget_getframe, 1258 .get_frame = s3c_hsudc_gadget_getframe,
1225 .start = s3c_hsudc_start, 1259 .start = s3c_hsudc_start,
1226 .stop = s3c_hsudc_stop, 1260 .stop = s3c_hsudc_stop,
1261 .vbus_draw = s3c_hsudc_vbus_draw,
1227}; 1262};
1228 1263
1229static int s3c_hsudc_probe(struct platform_device *pdev) 1264static int s3c_hsudc_probe(struct platform_device *pdev)
@@ -1247,6 +1282,8 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1247 hsudc->dev = dev; 1282 hsudc->dev = dev;
1248 hsudc->pd = pdev->dev.platform_data; 1283 hsudc->pd = pdev->dev.platform_data;
1249 1284
1285 hsudc->transceiver = otg_get_transceiver();
1286
1250 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1287 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1251 if (!res) { 1288 if (!res) {
1252 dev_err(dev, "unable to obtain driver resource data\n"); 1289 dev_err(dev, "unable to obtain driver resource data\n");
@@ -1269,19 +1306,6 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1269 goto err_remap; 1306 goto err_remap;
1270 } 1307 }
1271 1308
1272 ret = platform_get_irq(pdev, 0);
1273 if (ret < 0) {
1274 dev_err(dev, "unable to obtain IRQ number\n");
1275 goto err_irq;
1276 }
1277 hsudc->irq = ret;
1278
1279 ret = request_irq(hsudc->irq, s3c_hsudc_irq, 0, driver_name, hsudc);
1280 if (ret < 0) {
1281 dev_err(dev, "irq request failed\n");
1282 goto err_irq;
1283 }
1284
1285 spin_lock_init(&hsudc->lock); 1309 spin_lock_init(&hsudc->lock);
1286 1310
1287 device_initialize(&hsudc->gadget.dev); 1311 device_initialize(&hsudc->gadget.dev);
@@ -1299,6 +1323,19 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1299 1323
1300 s3c_hsudc_setup_ep(hsudc); 1324 s3c_hsudc_setup_ep(hsudc);
1301 1325
1326 ret = platform_get_irq(pdev, 0);
1327 if (ret < 0) {
1328 dev_err(dev, "unable to obtain IRQ number\n");
1329 goto err_irq;
1330 }
1331 hsudc->irq = ret;
1332
1333 ret = request_irq(hsudc->irq, s3c_hsudc_irq, 0, driver_name, hsudc);
1334 if (ret < 0) {
1335 dev_err(dev, "irq request failed\n");
1336 goto err_irq;
1337 }
1338
1302 hsudc->uclk = clk_get(&pdev->dev, "usb-device"); 1339 hsudc->uclk = clk_get(&pdev->dev, "usb-device");
1303 if (IS_ERR(hsudc->uclk)) { 1340 if (IS_ERR(hsudc->uclk)) {
1304 dev_err(dev, "failed to find usb-device clock source\n"); 1341 dev_err(dev, "failed to find usb-device clock source\n");
diff --git a/drivers/usb/gadget/s3c2410_udc.c b/drivers/usb/gadget/s3c2410_udc.c
index 8d31848aab09..b8643771fa80 100644
--- a/drivers/usb/gadget/s3c2410_udc.c
+++ b/drivers/usb/gadget/s3c2410_udc.c
@@ -10,16 +10,6 @@
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */ 13 */
24 14
25#include <linux/module.h> 15#include <linux/module.h>
@@ -1082,7 +1072,7 @@ static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1082 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 1072 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1083 return -ESHUTDOWN; 1073 return -ESHUTDOWN;
1084 1074
1085 max = le16_to_cpu(desc->wMaxPacketSize) & 0x1fff; 1075 max = usb_endpoint_maxp(desc) & 0x1fff;
1086 1076
1087 local_irq_save (flags); 1077 local_irq_save (flags);
1088 _ep->maxpacket = max & 0x7ff; 1078 _ep->maxpacket = max & 0x7ff;
@@ -1903,7 +1893,7 @@ static int s3c2410_udc_probe(struct platform_device *pdev)
1903 1893
1904 /* irq setup after old hardware state is cleaned up */ 1894 /* irq setup after old hardware state is cleaned up */
1905 retval = request_irq(IRQ_USBD, s3c2410_udc_irq, 1895 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1906 IRQF_DISABLED, gadget_name, udc); 1896 0, gadget_name, udc);
1907 1897
1908 if (retval != 0) { 1898 if (retval != 0) {
1909 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval); 1899 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
@@ -1927,7 +1917,7 @@ static int s3c2410_udc_probe(struct platform_device *pdev)
1927 } 1917 }
1928 1918
1929 retval = request_irq(irq, s3c2410_udc_vbus_irq, 1919 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1930 IRQF_DISABLED | IRQF_TRIGGER_RISING 1920 IRQF_TRIGGER_RISING
1931 | IRQF_TRIGGER_FALLING | IRQF_SHARED, 1921 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1932 gadget_name, udc); 1922 gadget_name, udc);
1933 1923
diff --git a/drivers/usb/gadget/s3c2410_udc.h b/drivers/usb/gadget/s3c2410_udc.h
index 9e0bece4f241..a48f619cb1cc 100644
--- a/drivers/usb/gadget/s3c2410_udc.h
+++ b/drivers/usb/gadget/s3c2410_udc.h
@@ -9,16 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */ 12 */
23 13
24#ifndef _S3C2410_UDC_H 14#ifndef _S3C2410_UDC_H
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index d3dd227a2bfc..c7f291a331df 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23 14
@@ -52,6 +43,12 @@
52 * characters rather then a pointer to void. 43 * characters rather then a pointer to void.
53 */ 44 */
54 45
46/*
47 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
48 * sets the number of pipeline buffers (length of the fsg_buffhd array).
49 * The valid range of num_buffers is: num >= 2 && num <= 4.
50 */
51
55 52
56#include <linux/usb/storage.h> 53#include <linux/usb/storage.h>
57#include <scsi/scsi.h> 54#include <scsi/scsi.h>
@@ -247,6 +244,8 @@ struct fsg_lun {
247 u32 sense_data_info; 244 u32 sense_data_info;
248 u32 unit_attention_data; 245 u32 unit_attention_data;
249 246
247 unsigned int blkbits; /* Bits of logical block size of bound block device */
248 unsigned int blksize; /* logical block size of bound block device */
250 struct device dev; 249 struct device dev;
251}; 250};
252 251
@@ -262,8 +261,31 @@ static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
262#define EP0_BUFSIZE 256 261#define EP0_BUFSIZE 256
263#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ 262#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
264 263
265/* Number of buffers we will use. 2 is enough for double-buffering */ 264#ifdef CONFIG_USB_GADGET_DEBUG_FILES
266#define FSG_NUM_BUFFERS 2 265
266static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
267module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
268MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
269
270#else
271
272/*
273 * Number of buffers we will use.
274 * 2 is usually enough for good buffering pipeline
275 */
276#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
277
278#endif /* CONFIG_USB_DEBUG */
279
280/* check if fsg_num_buffers is within a valid range */
281static inline int fsg_num_buffers_validate(void)
282{
283 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
284 return 0;
285 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
286 fsg_num_buffers, 2 ,4);
287 return -EINVAL;
288}
267 289
268/* Default size of buffer length. */ 290/* Default size of buffer length. */
269#define FSG_BUFLEN ((u32)16384) 291#define FSG_BUFLEN ((u32)16384)
@@ -493,12 +515,128 @@ static struct usb_descriptor_header *fsg_hs_function[] = {
493 NULL, 515 NULL,
494}; 516};
495 517
518static struct usb_endpoint_descriptor
519fsg_ss_bulk_in_desc = {
520 .bLength = USB_DT_ENDPOINT_SIZE,
521 .bDescriptorType = USB_DT_ENDPOINT,
522
523 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
524 .bmAttributes = USB_ENDPOINT_XFER_BULK,
525 .wMaxPacketSize = cpu_to_le16(1024),
526};
527
528static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
529 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
530 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
531
532 /*.bMaxBurst = DYNAMIC, */
533};
534
535static struct usb_endpoint_descriptor
536fsg_ss_bulk_out_desc = {
537 .bLength = USB_DT_ENDPOINT_SIZE,
538 .bDescriptorType = USB_DT_ENDPOINT,
539
540 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
541 .bmAttributes = USB_ENDPOINT_XFER_BULK,
542 .wMaxPacketSize = cpu_to_le16(1024),
543};
544
545static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
546 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
547 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
548
549 /*.bMaxBurst = DYNAMIC, */
550};
551
552#ifndef FSG_NO_INTR_EP
553
554static struct usb_endpoint_descriptor
555fsg_ss_intr_in_desc = {
556 .bLength = USB_DT_ENDPOINT_SIZE,
557 .bDescriptorType = USB_DT_ENDPOINT,
558
559 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
560 .bmAttributes = USB_ENDPOINT_XFER_INT,
561 .wMaxPacketSize = cpu_to_le16(2),
562 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
563};
564
565static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = {
566 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
567 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
568
569 .wBytesPerInterval = cpu_to_le16(2),
570};
571
572#ifndef FSG_NO_OTG
573# define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
574#else
575# define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
576#endif
577
578#endif
579
580static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = {
581 .bLength = USB_DT_USB_EXT_CAP_SIZE,
582 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
583 .bDevCapabilityType = USB_CAP_TYPE_EXT,
584
585 .bmAttributes = cpu_to_le32(USB_LPM_SUPPORT),
586};
587
588static __maybe_unused struct usb_ss_cap_descriptor fsg_ss_cap_desc = {
589 .bLength = USB_DT_USB_SS_CAP_SIZE,
590 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
591 .bDevCapabilityType = USB_SS_CAP_TYPE,
592
593 /* .bmAttributes = LTM is not supported yet */
594
595 .wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION
596 | USB_FULL_SPEED_OPERATION
597 | USB_HIGH_SPEED_OPERATION
598 | USB_5GBPS_OPERATION),
599 .bFunctionalitySupport = USB_LOW_SPEED_OPERATION,
600 .bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT,
601 .bU2DevExitLat = USB_DEFAULT_U2_DEV_EXIT_LAT,
602};
603
604static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = {
605 .bLength = USB_DT_BOS_SIZE,
606 .bDescriptorType = USB_DT_BOS,
607
608 .wTotalLength = USB_DT_BOS_SIZE
609 + USB_DT_USB_EXT_CAP_SIZE
610 + USB_DT_USB_SS_CAP_SIZE,
611
612 .bNumDeviceCaps = 2,
613};
614
615static struct usb_descriptor_header *fsg_ss_function[] = {
616#ifndef FSG_NO_OTG
617 (struct usb_descriptor_header *) &fsg_otg_desc,
618#endif
619 (struct usb_descriptor_header *) &fsg_intf_desc,
620 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
621 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
622 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
623 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
624#ifndef FSG_NO_INTR_EP
625 (struct usb_descriptor_header *) &fsg_ss_intr_in_desc,
626 (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc,
627#endif
628 NULL,
629};
630
496/* Maxpacket and other transfer characteristics vary by speed. */ 631/* Maxpacket and other transfer characteristics vary by speed. */
497static __maybe_unused struct usb_endpoint_descriptor * 632static __maybe_unused struct usb_endpoint_descriptor *
498fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, 633fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
499 struct usb_endpoint_descriptor *hs) 634 struct usb_endpoint_descriptor *hs,
635 struct usb_endpoint_descriptor *ss)
500{ 636{
501 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 637 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
638 return ss;
639 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
502 return hs; 640 return hs;
503 return fs; 641 return fs;
504} 642}
@@ -580,13 +718,24 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
580 rc = (int) size; 718 rc = (int) size;
581 goto out; 719 goto out;
582 } 720 }
583 num_sectors = size >> 9; /* File size in 512-byte blocks */ 721
722 if (curlun->cdrom) {
723 curlun->blksize = 2048;
724 curlun->blkbits = 11;
725 } else if (inode->i_bdev) {
726 curlun->blksize = bdev_logical_block_size(inode->i_bdev);
727 curlun->blkbits = blksize_bits(curlun->blksize);
728 } else {
729 curlun->blksize = 512;
730 curlun->blkbits = 9;
731 }
732
733 num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */
584 min_sectors = 1; 734 min_sectors = 1;
585 if (curlun->cdrom) { 735 if (curlun->cdrom) {
586 num_sectors &= ~3; /* Reduce to a multiple of 2048 */ 736 min_sectors = 300; /* Smallest track is 300 frames */
587 min_sectors = 300*4; /* Smallest track is 300 frames */ 737 if (num_sectors >= 256*60*75) {
588 if (num_sectors >= 256*60*75*4) { 738 num_sectors = 256*60*75 - 1;
589 num_sectors = (256*60*75 - 1) * 4;
590 LINFO(curlun, "file too big: %s\n", filename); 739 LINFO(curlun, "file too big: %s\n", filename);
591 LINFO(curlun, "using only first %d blocks\n", 740 LINFO(curlun, "using only first %d blocks\n",
592 (int) num_sectors); 741 (int) num_sectors);
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index dfed4c1d96c0..29c854bbca44 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23/* #define VERBOSE_DEBUG */ 14/* #define VERBOSE_DEBUG */
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index c966440ddd70..8012357e98aa 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -9,15 +9,6 @@
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */ 12 */
22 13
23#ifndef __U_ETHER_H 14#ifndef __U_ETHER_H
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index a8aa46962d81..6597a6813e43 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -25,6 +25,7 @@
25#include <linux/tty.h> 25#include <linux/tty.h>
26#include <linux/tty_flip.h> 26#include <linux/tty_flip.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/export.h>
28 29
29#include "u_serial.h" 30#include "u_serial.h"
30 31
@@ -552,9 +553,8 @@ recycle:
552 /* Push from tty to ldisc; without low_latency set this is handled by 553 /* Push from tty to ldisc; without low_latency set this is handled by
553 * a workqueue, so we won't get callbacks and can hold port_lock 554 * a workqueue, so we won't get callbacks and can hold port_lock
554 */ 555 */
555 if (tty && do_push) { 556 if (tty && do_push)
556 tty_flip_buffer_push(tty); 557 tty_flip_buffer_push(tty);
557 }
558 558
559 559
560 /* We want our data queue to become empty ASAP, keeping data 560 /* We want our data queue to become empty ASAP, keeping data
diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
index 05ba47214361..6939e17f4580 100644
--- a/drivers/usb/gadget/udc-core.c
+++ b/drivers/usb/gadget/udc-core.c
@@ -210,10 +210,10 @@ static void usb_gadget_remove_driver(struct usb_udc *udc)
210 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 210 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
211 211
212 if (udc_is_newstyle(udc)) { 212 if (udc_is_newstyle(udc)) {
213 usb_gadget_disconnect(udc->gadget); 213 udc->driver->disconnect(udc->gadget);
214 udc->driver->unbind(udc->gadget); 214 udc->driver->unbind(udc->gadget);
215 usb_gadget_udc_stop(udc->gadget, udc->driver); 215 usb_gadget_udc_stop(udc->gadget, udc->driver);
216 216 usb_gadget_disconnect(udc->gadget);
217 } else { 217 } else {
218 usb_gadget_stop(udc->gadget, udc->driver); 218 usb_gadget_stop(udc->gadget, udc->driver);
219 } 219 }
@@ -344,7 +344,7 @@ EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
344static ssize_t usb_udc_srp_store(struct device *dev, 344static ssize_t usb_udc_srp_store(struct device *dev,
345 struct device_attribute *attr, const char *buf, size_t n) 345 struct device_attribute *attr, const char *buf, size_t n)
346{ 346{
347 struct usb_udc *udc = dev_get_drvdata(dev); 347 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
348 348
349 if (sysfs_streq(buf, "1")) 349 if (sysfs_streq(buf, "1"))
350 usb_gadget_wakeup(udc->gadget); 350 usb_gadget_wakeup(udc->gadget);
@@ -356,7 +356,7 @@ static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
356static ssize_t usb_udc_softconn_store(struct device *dev, 356static ssize_t usb_udc_softconn_store(struct device *dev,
357 struct device_attribute *attr, const char *buf, size_t n) 357 struct device_attribute *attr, const char *buf, size_t n)
358{ 358{
359 struct usb_udc *udc = dev_get_drvdata(dev); 359 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
360 360
361 if (sysfs_streq(buf, "connect")) { 361 if (sysfs_streq(buf, "connect")) {
362 usb_gadget_connect(udc->gadget); 362 usb_gadget_connect(udc->gadget);
@@ -375,25 +375,10 @@ static ssize_t usb_udc_speed_show(struct device *dev,
375 struct device_attribute *attr, char *buf) 375 struct device_attribute *attr, char *buf)
376{ 376{
377 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 377 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
378 struct usb_gadget *gadget = udc->gadget; 378 return snprintf(buf, PAGE_SIZE, "%s\n",
379 379 usb_speed_string(udc->gadget->speed));
380 switch (gadget->speed) {
381 case USB_SPEED_LOW:
382 return snprintf(buf, PAGE_SIZE, "low-speed\n");
383 case USB_SPEED_FULL:
384 return snprintf(buf, PAGE_SIZE, "full-speed\n");
385 case USB_SPEED_HIGH:
386 return snprintf(buf, PAGE_SIZE, "high-speed\n");
387 case USB_SPEED_WIRELESS:
388 return snprintf(buf, PAGE_SIZE, "wireless\n");
389 case USB_SPEED_SUPER:
390 return snprintf(buf, PAGE_SIZE, "super-speed\n");
391 case USB_SPEED_UNKNOWN: /* FALLTHROUGH */
392 default:
393 return snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
394 }
395} 380}
396static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL); 381static DEVICE_ATTR(speed, S_IRUGO, usb_udc_speed_show, NULL);
397 382
398#define USB_UDC_ATTR(name) \ 383#define USB_UDC_ATTR(name) \
399ssize_t usb_udc_##name##_show(struct device *dev, \ 384ssize_t usb_udc_##name##_show(struct device *dev, \
@@ -404,7 +389,7 @@ ssize_t usb_udc_##name##_show(struct device *dev, \
404 \ 389 \
405 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ 390 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
406} \ 391} \
407static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL) 392static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
408 393
409static USB_UDC_ATTR(is_dualspeed); 394static USB_UDC_ATTR(is_dualspeed);
410static USB_UDC_ATTR(is_otg); 395static USB_UDC_ATTR(is_otg);
diff --git a/drivers/usb/gadget/uvc.h b/drivers/usb/gadget/uvc.h
index 5b7919460fd2..bc78c606c12b 100644
--- a/drivers/usb/gadget/uvc.h
+++ b/drivers/usb/gadget/uvc.h
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#ifndef _UVC_GADGET_H_ 13#ifndef _UVC_GADGET_H_
@@ -56,6 +55,7 @@ struct uvc_event
56#include <linux/usb.h> /* For usb_endpoint_* */ 55#include <linux/usb.h> /* For usb_endpoint_* */
57#include <linux/usb/gadget.h> 56#include <linux/usb/gadget.h>
58#include <linux/videodev2.h> 57#include <linux/videodev2.h>
58#include <linux/version.h>
59#include <media/v4l2-fh.h> 59#include <media/v4l2-fh.h>
60 60
61#include "uvc_queue.h" 61#include "uvc_queue.h"
diff --git a/drivers/usb/gadget/uvc_queue.c b/drivers/usb/gadget/uvc_queue.c
index aa0ad34e0f1f..d776adb2da67 100644
--- a/drivers/usb/gadget/uvc_queue.c
+++ b/drivers/usb/gadget/uvc_queue.c
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#include <linux/kernel.h> 13#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/uvc_v4l2.c b/drivers/usb/gadget/uvc_v4l2.c
index 52f8f9e513af..f6e083b50191 100644
--- a/drivers/usb/gadget/uvc_v4l2.c
+++ b/drivers/usb/gadget/uvc_v4l2.c
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#include <linux/kernel.h> 13#include <linux/kernel.h>
@@ -16,7 +15,6 @@
16#include <linux/errno.h> 15#include <linux/errno.h>
17#include <linux/list.h> 16#include <linux/list.h>
18#include <linux/mutex.h> 17#include <linux/mutex.h>
19#include <linux/version.h>
20#include <linux/videodev2.h> 18#include <linux/videodev2.h>
21#include <linux/vmalloc.h> 19#include <linux/vmalloc.h>
22#include <linux/wait.h> 20#include <linux/wait.h>
diff --git a/drivers/usb/gadget/uvc_video.c b/drivers/usb/gadget/uvc_video.c
index b08f35438d70..b0e53a8ea4f7 100644
--- a/drivers/usb/gadget/uvc_video.c
+++ b/drivers/usb/gadget/uvc_video.c
@@ -8,7 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
13 12
14#include <linux/kernel.h> 13#include <linux/kernel.h>
diff --git a/drivers/usb/gadget/webcam.c b/drivers/usb/gadget/webcam.c
index df6882de50bf..668fe128f2ef 100644
--- a/drivers/usb/gadget/webcam.c
+++ b/drivers/usb/gadget/webcam.c
@@ -8,8 +8,8 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 */ 11 */
12
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/usb/video.h> 15#include <linux/usb/video.h>
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index 00e2fd2d4791..20697cc132d1 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -8,15 +8,6 @@
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 11 */
21 12
22 13
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index ab085f12d570..060e0e2b1ae6 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -19,7 +19,7 @@ config USB_C67X00_HCD
19 19
20config USB_XHCI_HCD 20config USB_XHCI_HCD
21 tristate "xHCI HCD (USB 3.0) support (EXPERIMENTAL)" 21 tristate "xHCI HCD (USB 3.0) support (EXPERIMENTAL)"
22 depends on USB && PCI && EXPERIMENTAL 22 depends on USB && USB_ARCH_HAS_XHCI && EXPERIMENTAL
23 ---help--- 23 ---help---
24 The eXtensible Host Controller Interface (xHCI) is standard for USB 3.0 24 The eXtensible Host Controller Interface (xHCI) is standard for USB 3.0
25 "SuperSpeed" host controller hardware. 25 "SuperSpeed" host controller hardware.
@@ -515,6 +515,19 @@ config USB_R8A66597_HCD
515 To compile this driver as a module, choose M here: the 515 To compile this driver as a module, choose M here: the
516 module will be called r8a66597-hcd. 516 module will be called r8a66597-hcd.
517 517
518config USB_RENESAS_USBHS_HCD
519 tristate "Renesas USBHS HCD support"
520 depends on USB
521 depends on USB_RENESAS_USBHS
522 help
523 The Renesas USBHS is a USB 2.0 host and peripheral controller.
524
525 Enable this option if your board has this chip, and you want
526 to use it as a host controller. If unsure, say N.
527
528 To compile this driver as a module, choose M here: the
529 module will be called renesas-usbhs.
530
518config USB_WHCI_HCD 531config USB_WHCI_HCD
519 tristate "Wireless USB Host Controller Interface (WHCI) driver (EXPERIMENTAL)" 532 tristate "Wireless USB Host Controller Interface (WHCI) driver (EXPERIMENTAL)"
520 depends on EXPERIMENTAL 533 depends on EXPERIMENTAL
@@ -544,11 +557,11 @@ config USB_HWA_HCD
544 will be called "hwa-hc". 557 will be called "hwa-hc".
545 558
546config USB_IMX21_HCD 559config USB_IMX21_HCD
547 tristate "iMX21 HCD support" 560 tristate "i.MX21 HCD support"
548 depends on USB && ARM && MACH_MX21 561 depends on USB && ARM && ARCH_MXC
549 help 562 help
550 This driver enables support for the on-chip USB host in the 563 This driver enables support for the on-chip USB host in the
551 iMX21 processor. 564 i.MX21 processor.
552 565
553 To compile this driver as a module, choose M here: the 566 To compile this driver as a module, choose M here: the
554 module will be called "imx21-hcd". 567 module will be called "imx21-hcd".
@@ -578,3 +591,10 @@ config USB_OCTEON_OHCI
578config USB_OCTEON2_COMMON 591config USB_OCTEON2_COMMON
579 bool 592 bool
580 default y if USB_OCTEON_EHCI || USB_OCTEON_OHCI 593 default y if USB_OCTEON_EHCI || USB_OCTEON_OHCI
594
595config USB_PXA168_EHCI
596 bool "Marvell PXA168 on-chip EHCI HCD support"
597 depends on USB_EHCI_HCD && ARCH_MMP
598 help
599 Enable support for Marvell PXA168 SoC's on-chip EHCI
600 host controller
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index 624a362f2fee..7ca290fcb070 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -11,8 +11,9 @@ fhci-y += fhci-mem.o fhci-tds.o fhci-sched.o
11 11
12fhci-$(CONFIG_FHCI_DEBUG) += fhci-dbg.o 12fhci-$(CONFIG_FHCI_DEBUG) += fhci-dbg.o
13 13
14xhci-hcd-y := xhci.o xhci-mem.o xhci-pci.o 14xhci-hcd-y := xhci.o xhci-mem.o
15xhci-hcd-y += xhci-ring.o xhci-hub.o xhci-dbg.o 15xhci-hcd-y += xhci-ring.o xhci-hub.o xhci-dbg.o
16xhci-hcd-$(CONFIG_PCI) += xhci-pci.o
16 17
17obj-$(CONFIG_USB_WHCI_HCD) += whci/ 18obj-$(CONFIG_USB_WHCI_HCD) += whci/
18 19
@@ -35,3 +36,4 @@ obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
35obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o 36obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
36obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o 37obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
37obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o 38obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
39obj-$(CONFIG_MIPS_ALCHEMY) += alchemy-common.o
diff --git a/drivers/usb/host/alchemy-common.c b/drivers/usb/host/alchemy-common.c
new file mode 100644
index 000000000000..b4192c964d0d
--- /dev/null
+++ b/drivers/usb/host/alchemy-common.c
@@ -0,0 +1,337 @@
1/*
2 * USB block power/access management abstraction.
3 *
4 * Au1000+: The OHCI block control register is at the far end of the OHCI memory
5 * area. Au1550 has OHCI on different base address. No need to handle
6 * UDC here.
7 * Au1200: one register to control access and clocks to O/EHCI, UDC and OTG
8 * as well as the PHY for EHCI and UDC.
9 *
10 */
11
12#include <linux/init.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/syscore_ops.h>
17#include <asm/mach-au1x00/au1000.h>
18
19/* control register offsets */
20#define AU1000_OHCICFG 0x7fffc
21#define AU1550_OHCICFG 0x07ffc
22#define AU1200_USBCFG 0x04
23
24/* Au1000 USB block config bits */
25#define USBHEN_RD (1 << 4) /* OHCI reset-done indicator */
26#define USBHEN_CE (1 << 3) /* OHCI block clock enable */
27#define USBHEN_E (1 << 2) /* OHCI block enable */
28#define USBHEN_C (1 << 1) /* OHCI block coherency bit */
29#define USBHEN_BE (1 << 0) /* OHCI Big-Endian */
30
31/* Au1200 USB config bits */
32#define USBCFG_PFEN (1 << 31) /* prefetch enable (undoc) */
33#define USBCFG_RDCOMB (1 << 30) /* read combining (undoc) */
34#define USBCFG_UNKNOWN (5 << 20) /* unknown, leave this way */
35#define USBCFG_SSD (1 << 23) /* serial short detect en */
36#define USBCFG_PPE (1 << 19) /* HS PHY PLL */
37#define USBCFG_UCE (1 << 18) /* UDC clock enable */
38#define USBCFG_ECE (1 << 17) /* EHCI clock enable */
39#define USBCFG_OCE (1 << 16) /* OHCI clock enable */
40#define USBCFG_FLA(x) (((x) & 0x3f) << 8)
41#define USBCFG_UCAM (1 << 7) /* coherent access (undoc) */
42#define USBCFG_GME (1 << 6) /* OTG mem access */
43#define USBCFG_DBE (1 << 5) /* UDC busmaster enable */
44#define USBCFG_DME (1 << 4) /* UDC mem enable */
45#define USBCFG_EBE (1 << 3) /* EHCI busmaster enable */
46#define USBCFG_EME (1 << 2) /* EHCI mem enable */
47#define USBCFG_OBE (1 << 1) /* OHCI busmaster enable */
48#define USBCFG_OME (1 << 0) /* OHCI mem enable */
49#define USBCFG_INIT_AU1200 (USBCFG_PFEN | USBCFG_RDCOMB | USBCFG_UNKNOWN |\
50 USBCFG_SSD | USBCFG_FLA(0x20) | USBCFG_UCAM | \
51 USBCFG_GME | USBCFG_DBE | USBCFG_DME | \
52 USBCFG_EBE | USBCFG_EME | USBCFG_OBE | \
53 USBCFG_OME)
54
55
56static DEFINE_SPINLOCK(alchemy_usb_lock);
57
58
59static inline void __au1200_ohci_control(void __iomem *base, int enable)
60{
61 unsigned long r = __raw_readl(base + AU1200_USBCFG);
62 if (enable) {
63 __raw_writel(r | USBCFG_OCE, base + AU1200_USBCFG);
64 wmb();
65 udelay(2000);
66 } else {
67 __raw_writel(r & ~USBCFG_OCE, base + AU1200_USBCFG);
68 wmb();
69 udelay(1000);
70 }
71}
72
73static inline void __au1200_ehci_control(void __iomem *base, int enable)
74{
75 unsigned long r = __raw_readl(base + AU1200_USBCFG);
76 if (enable) {
77 __raw_writel(r | USBCFG_ECE | USBCFG_PPE, base + AU1200_USBCFG);
78 wmb();
79 udelay(1000);
80 } else {
81 if (!(r & USBCFG_UCE)) /* UDC also off? */
82 r &= ~USBCFG_PPE; /* yes: disable HS PHY PLL */
83 __raw_writel(r & ~USBCFG_ECE, base + AU1200_USBCFG);
84 wmb();
85 udelay(1000);
86 }
87}
88
89static inline void __au1200_udc_control(void __iomem *base, int enable)
90{
91 unsigned long r = __raw_readl(base + AU1200_USBCFG);
92 if (enable) {
93 __raw_writel(r | USBCFG_UCE | USBCFG_PPE, base + AU1200_USBCFG);
94 wmb();
95 } else {
96 if (!(r & USBCFG_ECE)) /* EHCI also off? */
97 r &= ~USBCFG_PPE; /* yes: disable HS PHY PLL */
98 __raw_writel(r & ~USBCFG_UCE, base + AU1200_USBCFG);
99 wmb();
100 }
101}
102
103static inline int au1200_coherency_bug(void)
104{
105#if defined(CONFIG_DMA_COHERENT)
106 /* Au1200 AB USB does not support coherent memory */
107 if (!(read_c0_prid() & 0xff)) {
108 printk(KERN_INFO "Au1200 USB: this is chip revision AB !!\n");
109 printk(KERN_INFO "Au1200 USB: update your board or re-configure"
110 " the kernel\n");
111 return -ENODEV;
112 }
113#endif
114 return 0;
115}
116
117static inline int au1200_usb_control(int block, int enable)
118{
119 void __iomem *base =
120 (void __iomem *)KSEG1ADDR(AU1200_USB_CTL_PHYS_ADDR);
121 int ret = 0;
122
123 switch (block) {
124 case ALCHEMY_USB_OHCI0:
125 ret = au1200_coherency_bug();
126 if (ret && enable)
127 goto out;
128 __au1200_ohci_control(base, enable);
129 break;
130 case ALCHEMY_USB_UDC0:
131 __au1200_udc_control(base, enable);
132 break;
133 case ALCHEMY_USB_EHCI0:
134 ret = au1200_coherency_bug();
135 if (ret && enable)
136 goto out;
137 __au1200_ehci_control(base, enable);
138 break;
139 default:
140 ret = -ENODEV;
141 }
142out:
143 return ret;
144}
145
146
147/* initialize USB block(s) to a known working state */
148static inline void au1200_usb_init(void)
149{
150 void __iomem *base =
151 (void __iomem *)KSEG1ADDR(AU1200_USB_CTL_PHYS_ADDR);
152 __raw_writel(USBCFG_INIT_AU1200, base + AU1200_USBCFG);
153 wmb();
154 udelay(1000);
155}
156
157static inline void au1000_usb_init(unsigned long rb, int reg)
158{
159 void __iomem *base = (void __iomem *)KSEG1ADDR(rb + reg);
160 unsigned long r = __raw_readl(base);
161
162#if defined(__BIG_ENDIAN)
163 r |= USBHEN_BE;
164#endif
165 r |= USBHEN_C;
166
167 __raw_writel(r, base);
168 wmb();
169 udelay(1000);
170}
171
172
173static inline void __au1xx0_ohci_control(int enable, unsigned long rb, int creg)
174{
175 void __iomem *base = (void __iomem *)KSEG1ADDR(rb);
176 unsigned long r = __raw_readl(base + creg);
177
178 if (enable) {
179 __raw_writel(r | USBHEN_CE, base + creg);
180 wmb();
181 udelay(1000);
182 __raw_writel(r | USBHEN_CE | USBHEN_E, base + creg);
183 wmb();
184 udelay(1000);
185
186 /* wait for reset complete (read reg twice: au1500 erratum) */
187 while (__raw_readl(base + creg),
188 !(__raw_readl(base + creg) & USBHEN_RD))
189 udelay(1000);
190 } else {
191 __raw_writel(r & ~(USBHEN_CE | USBHEN_E), base + creg);
192 wmb();
193 }
194}
195
196static inline int au1000_usb_control(int block, int enable, unsigned long rb,
197 int creg)
198{
199 int ret = 0;
200
201 switch (block) {
202 case ALCHEMY_USB_OHCI0:
203 __au1xx0_ohci_control(enable, rb, creg);
204 break;
205 default:
206 ret = -ENODEV;
207 }
208 return ret;
209}
210
211/*
212 * alchemy_usb_control - control Alchemy on-chip USB blocks
213 * @block: USB block to target
214 * @enable: set 1 to enable a block, 0 to disable
215 */
216int alchemy_usb_control(int block, int enable)
217{
218 unsigned long flags;
219 int ret;
220
221 spin_lock_irqsave(&alchemy_usb_lock, flags);
222 switch (alchemy_get_cputype()) {
223 case ALCHEMY_CPU_AU1000:
224 case ALCHEMY_CPU_AU1500:
225 case ALCHEMY_CPU_AU1100:
226 ret = au1000_usb_control(block, enable,
227 AU1000_USB_OHCI_PHYS_ADDR, AU1000_OHCICFG);
228 break;
229 case ALCHEMY_CPU_AU1550:
230 ret = au1000_usb_control(block, enable,
231 AU1550_USB_OHCI_PHYS_ADDR, AU1550_OHCICFG);
232 break;
233 case ALCHEMY_CPU_AU1200:
234 ret = au1200_usb_control(block, enable);
235 break;
236 default:
237 ret = -ENODEV;
238 }
239 spin_unlock_irqrestore(&alchemy_usb_lock, flags);
240 return ret;
241}
242EXPORT_SYMBOL_GPL(alchemy_usb_control);
243
244
245static unsigned long alchemy_usb_pmdata[2];
246
247static void au1000_usb_pm(unsigned long br, int creg, int susp)
248{
249 void __iomem *base = (void __iomem *)KSEG1ADDR(br);
250
251 if (susp) {
252 alchemy_usb_pmdata[0] = __raw_readl(base + creg);
253 /* There appears to be some undocumented reset register.... */
254 __raw_writel(0, base + 0x04);
255 wmb();
256 __raw_writel(0, base + creg);
257 wmb();
258 } else {
259 __raw_writel(alchemy_usb_pmdata[0], base + creg);
260 wmb();
261 }
262}
263
264static void au1200_usb_pm(int susp)
265{
266 void __iomem *base =
267 (void __iomem *)KSEG1ADDR(AU1200_USB_OTG_PHYS_ADDR);
268 if (susp) {
269 /* save OTG_CAP/MUX registers which indicate port routing */
270 /* FIXME: write an OTG driver to do that */
271 alchemy_usb_pmdata[0] = __raw_readl(base + 0x00);
272 alchemy_usb_pmdata[1] = __raw_readl(base + 0x04);
273 } else {
274 /* restore access to all MMIO areas */
275 au1200_usb_init();
276
277 /* restore OTG_CAP/MUX registers */
278 __raw_writel(alchemy_usb_pmdata[0], base + 0x00);
279 __raw_writel(alchemy_usb_pmdata[1], base + 0x04);
280 wmb();
281 }
282}
283
284static void alchemy_usb_pm(int susp)
285{
286 switch (alchemy_get_cputype()) {
287 case ALCHEMY_CPU_AU1000:
288 case ALCHEMY_CPU_AU1500:
289 case ALCHEMY_CPU_AU1100:
290 au1000_usb_pm(AU1000_USB_OHCI_PHYS_ADDR, AU1000_OHCICFG, susp);
291 break;
292 case ALCHEMY_CPU_AU1550:
293 au1000_usb_pm(AU1550_USB_OHCI_PHYS_ADDR, AU1550_OHCICFG, susp);
294 break;
295 case ALCHEMY_CPU_AU1200:
296 au1200_usb_pm(susp);
297 break;
298 }
299}
300
301static int alchemy_usb_suspend(void)
302{
303 alchemy_usb_pm(1);
304 return 0;
305}
306
307static void alchemy_usb_resume(void)
308{
309 alchemy_usb_pm(0);
310}
311
312static struct syscore_ops alchemy_usb_pm_ops = {
313 .suspend = alchemy_usb_suspend,
314 .resume = alchemy_usb_resume,
315};
316
317static int __init alchemy_usb_init(void)
318{
319 switch (alchemy_get_cputype()) {
320 case ALCHEMY_CPU_AU1000:
321 case ALCHEMY_CPU_AU1500:
322 case ALCHEMY_CPU_AU1100:
323 au1000_usb_init(AU1000_USB_OHCI_PHYS_ADDR, AU1000_OHCICFG);
324 break;
325 case ALCHEMY_CPU_AU1550:
326 au1000_usb_init(AU1550_USB_OHCI_PHYS_ADDR, AU1550_OHCICFG);
327 break;
328 case ALCHEMY_CPU_AU1200:
329 au1200_usb_init();
330 break;
331 }
332
333 register_syscore_ops(&alchemy_usb_pm_ops);
334
335 return 0;
336}
337arch_initcall(alchemy_usb_init);
diff --git a/drivers/usb/host/ehci-ath79.c b/drivers/usb/host/ehci-ath79.c
index 4d2e88d04dab..afb6743cf094 100644
--- a/drivers/usb/host/ehci-ath79.c
+++ b/drivers/usb/host/ehci-ath79.c
@@ -163,7 +163,7 @@ static int ehci_ath79_probe(struct platform_device *pdev)
163 goto err_release_region; 163 goto err_release_region;
164 } 164 }
165 165
166 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 166 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
167 if (ret) 167 if (ret)
168 goto err_iounmap; 168 goto err_iounmap;
169 169
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
index 42ae57409908..18bafa99fe57 100644
--- a/drivers/usb/host/ehci-au1xxx.c
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -14,61 +14,9 @@
14#include <linux/platform_device.h> 14#include <linux/platform_device.h>
15#include <asm/mach-au1x00/au1000.h> 15#include <asm/mach-au1x00/au1000.h>
16 16
17#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
18#define USB_MCFG_PFEN (1<<31)
19#define USB_MCFG_RDCOMB (1<<30)
20#define USB_MCFG_SSDEN (1<<23)
21#define USB_MCFG_PHYPLLEN (1<<19)
22#define USB_MCFG_UCECLKEN (1<<18)
23#define USB_MCFG_EHCCLKEN (1<<17)
24#ifdef CONFIG_DMA_COHERENT
25#define USB_MCFG_UCAM (1<<7)
26#else
27#define USB_MCFG_UCAM (0)
28#endif
29#define USB_MCFG_EBMEN (1<<3)
30#define USB_MCFG_EMEMEN (1<<2)
31
32#define USBH_ENABLE_CE (USB_MCFG_PHYPLLEN | USB_MCFG_EHCCLKEN)
33#define USBH_ENABLE_INIT (USB_MCFG_PFEN | USB_MCFG_RDCOMB | \
34 USBH_ENABLE_CE | USB_MCFG_SSDEN | \
35 USB_MCFG_UCAM | USB_MCFG_EBMEN | \
36 USB_MCFG_EMEMEN)
37
38#define USBH_DISABLE (USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
39 17
40extern int usb_disabled(void); 18extern int usb_disabled(void);
41 19
42static void au1xxx_start_ehc(void)
43{
44 /* enable clock to EHCI block and HS PHY PLL*/
45 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_CE, USB_HOST_CONFIG);
46 au_sync();
47 udelay(1000);
48
49 /* enable EHCI mmio */
50 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
51 au_sync();
52 udelay(1000);
53}
54
55static void au1xxx_stop_ehc(void)
56{
57 unsigned long c;
58
59 /* Disable mem */
60 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_DISABLE, USB_HOST_CONFIG);
61 au_sync();
62 udelay(1000);
63
64 /* Disable EHC clock. If the HS PHY is unused disable it too. */
65 c = au_readl(USB_HOST_CONFIG) & ~USB_MCFG_EHCCLKEN;
66 if (!(c & USB_MCFG_UCECLKEN)) /* UDC disabled? */
67 c &= ~USB_MCFG_PHYPLLEN; /* yes: disable HS PHY PLL */
68 au_writel(c, USB_HOST_CONFIG);
69 au_sync();
70}
71
72static int au1xxx_ehci_setup(struct usb_hcd *hcd) 20static int au1xxx_ehci_setup(struct usb_hcd *hcd)
73{ 21{
74 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 22 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
@@ -136,16 +84,6 @@ static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
136 if (usb_disabled()) 84 if (usb_disabled())
137 return -ENODEV; 85 return -ENODEV;
138 86
139#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
140 /* Au1200 AB USB does not support coherent memory */
141 if (!(read_c0_prid() & 0xff)) {
142 printk(KERN_INFO "%s: this is chip revision AB!\n", pdev->name);
143 printk(KERN_INFO "%s: update your board or re-configure"
144 " the kernel\n", pdev->name);
145 return -ENODEV;
146 }
147#endif
148
149 if (pdev->resource[1].flags != IORESOURCE_IRQ) { 87 if (pdev->resource[1].flags != IORESOURCE_IRQ) {
150 pr_debug("resource[1] is not IORESOURCE_IRQ"); 88 pr_debug("resource[1] is not IORESOURCE_IRQ");
151 return -ENOMEM; 89 return -ENOMEM;
@@ -171,7 +109,11 @@ static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
171 goto err2; 109 goto err2;
172 } 110 }
173 111
174 au1xxx_start_ehc(); 112 if (alchemy_usb_control(ALCHEMY_USB_EHCI0, 1)) {
113 printk(KERN_INFO "%s: controller init failed!\n", pdev->name);
114 ret = -ENODEV;
115 goto err3;
116 }
175 117
176 ehci = hcd_to_ehci(hcd); 118 ehci = hcd_to_ehci(hcd);
177 ehci->caps = hcd->regs; 119 ehci->caps = hcd->regs;
@@ -181,13 +123,14 @@ static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
181 ehci->hcs_params = readl(&ehci->caps->hcs_params); 123 ehci->hcs_params = readl(&ehci->caps->hcs_params);
182 124
183 ret = usb_add_hcd(hcd, pdev->resource[1].start, 125 ret = usb_add_hcd(hcd, pdev->resource[1].start,
184 IRQF_DISABLED | IRQF_SHARED); 126 IRQF_SHARED);
185 if (ret == 0) { 127 if (ret == 0) {
186 platform_set_drvdata(pdev, hcd); 128 platform_set_drvdata(pdev, hcd);
187 return ret; 129 return ret;
188 } 130 }
189 131
190 au1xxx_stop_ehc(); 132 alchemy_usb_control(ALCHEMY_USB_EHCI0, 0);
133err3:
191 iounmap(hcd->regs); 134 iounmap(hcd->regs);
192err2: 135err2:
193 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 136 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
@@ -201,10 +144,10 @@ static int ehci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
201 struct usb_hcd *hcd = platform_get_drvdata(pdev); 144 struct usb_hcd *hcd = platform_get_drvdata(pdev);
202 145
203 usb_remove_hcd(hcd); 146 usb_remove_hcd(hcd);
147 alchemy_usb_control(ALCHEMY_USB_EHCI0, 0);
204 iounmap(hcd->regs); 148 iounmap(hcd->regs);
205 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 149 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
206 usb_put_hcd(hcd); 150 usb_put_hcd(hcd);
207 au1xxx_stop_ehc();
208 platform_set_drvdata(pdev, NULL); 151 platform_set_drvdata(pdev, NULL);
209 152
210 return 0; 153 return 0;
@@ -236,7 +179,7 @@ static int ehci_hcd_au1xxx_drv_suspend(struct device *dev)
236 // could save FLADJ in case of Vaux power loss 179 // could save FLADJ in case of Vaux power loss
237 // ... we'd only use it to handle clock skew 180 // ... we'd only use it to handle clock skew
238 181
239 au1xxx_stop_ehc(); 182 alchemy_usb_control(ALCHEMY_USB_EHCI0, 0);
240 183
241 return rc; 184 return rc;
242} 185}
@@ -246,7 +189,7 @@ static int ehci_hcd_au1xxx_drv_resume(struct device *dev)
246 struct usb_hcd *hcd = dev_get_drvdata(dev); 189 struct usb_hcd *hcd = dev_get_drvdata(dev);
247 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 190 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
248 191
249 au1xxx_start_ehc(); 192 alchemy_usb_control(ALCHEMY_USB_EHCI0, 1);
250 193
251 // maybe restore FLADJ 194 // maybe restore FLADJ
252 195
@@ -293,7 +236,7 @@ static int ehci_hcd_au1xxx_drv_resume(struct device *dev)
293 /* here we "know" root ports should always stay powered */ 236 /* here we "know" root ports should always stay powered */
294 ehci_port_power(ehci, 1); 237 ehci_port_power(ehci, 1);
295 238
296 hcd->state = HC_STATE_SUSPENDED; 239 ehci->rh_state = EHCI_RH_SUSPENDED;
297 240
298 return 0; 241 return 0;
299} 242}
diff --git a/drivers/usb/host/ehci-dbg.c b/drivers/usb/host/ehci-dbg.c
index 40a844c1dbb4..d6d74d2e09f4 100644
--- a/drivers/usb/host/ehci-dbg.c
+++ b/drivers/usb/host/ehci-dbg.c
@@ -697,6 +697,19 @@ static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
697} 697}
698#undef DBG_SCHED_LIMIT 698#undef DBG_SCHED_LIMIT
699 699
700static const char *rh_state_string(struct ehci_hcd *ehci)
701{
702 switch (ehci->rh_state) {
703 case EHCI_RH_HALTED:
704 return "halted";
705 case EHCI_RH_SUSPENDED:
706 return "suspended";
707 case EHCI_RH_RUNNING:
708 return "running";
709 }
710 return "?";
711}
712
700static ssize_t fill_registers_buffer(struct debug_buffer *buf) 713static ssize_t fill_registers_buffer(struct debug_buffer *buf)
701{ 714{
702 struct usb_hcd *hcd; 715 struct usb_hcd *hcd;
@@ -730,11 +743,11 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
730 temp = scnprintf (next, size, 743 temp = scnprintf (next, size,
731 "bus %s, device %s\n" 744 "bus %s, device %s\n"
732 "%s\n" 745 "%s\n"
733 "EHCI %x.%02x, hcd state %d\n", 746 "EHCI %x.%02x, rh state %s\n",
734 hcd->self.controller->bus->name, 747 hcd->self.controller->bus->name,
735 dev_name(hcd->self.controller), 748 dev_name(hcd->self.controller),
736 hcd->product_desc, 749 hcd->product_desc,
737 i >> 8, i & 0x0ff, hcd->state); 750 i >> 8, i & 0x0ff, rh_state_string(ehci));
738 size -= temp; 751 size -= temp;
739 next += temp; 752 next += temp;
740 753
@@ -808,7 +821,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
808 next += temp; 821 next += temp;
809 822
810 temp = scnprintf (next, size, "uframe %04x\n", 823 temp = scnprintf (next, size, "uframe %04x\n",
811 ehci_readl(ehci, &ehci->regs->frame_index)); 824 ehci_read_frame_index(ehci));
812 size -= temp; 825 size -= temp;
813 next += temp; 826 next += temp;
814 827
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index 34a3140d1e5f..e90344a17631 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -134,7 +134,7 @@ static int usb_hcd_fsl_probe(const struct hc_driver *driver,
134 134
135 /* Don't need to set host mode here. It will be done by tdi_reset() */ 135 /* Don't need to set host mode here. It will be done by tdi_reset() */
136 136
137 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 137 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
138 if (retval != 0) 138 if (retval != 0)
139 goto err4; 139 goto err4;
140 140
@@ -392,7 +392,7 @@ static int ehci_fsl_mpc512x_drv_suspend(struct device *dev)
392 392
393 dev_dbg(dev, "suspending...\n"); 393 dev_dbg(dev, "suspending...\n");
394 394
395 hcd->state = HC_STATE_SUSPENDED; 395 ehci->rh_state = EHCI_RH_SUSPENDED;
396 dev->power.power_state = PMSG_SUSPEND; 396 dev->power.power_state = PMSG_SUSPEND;
397 397
398 /* ignore non-host interrupts */ 398 /* ignore non-host interrupts */
@@ -481,7 +481,7 @@ static int ehci_fsl_mpc512x_drv_resume(struct device *dev)
481 ehci_writel(ehci, pdata->pm_portsc, &ehci->regs->port_status[0]); 481 ehci_writel(ehci, pdata->pm_portsc, &ehci->regs->port_status[0]);
482 482
483 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 483 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
484 hcd->state = HC_STATE_RUNNING; 484 ehci->rh_state = EHCI_RH_RUNNING;
485 dev->power.power_state = PMSG_ON; 485 dev->power.power_state = PMSG_ON;
486 486
487 tmp = ehci_readl(ehci, &ehci->regs->command); 487 tmp = ehci_readl(ehci, &ehci->regs->command);
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index f72ae0b6ee7f..3ff9f82f7263 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -95,7 +95,7 @@ static const char hcd_name [] = "ehci_hcd";
95#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */ 95#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
96#define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */ 96#define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
97#define EHCI_SHRINK_JIFFIES (DIV_ROUND_UP(HZ, 200) + 1) 97#define EHCI_SHRINK_JIFFIES (DIV_ROUND_UP(HZ, 200) + 1)
98 /* 200-ms async qh unlink delay */ 98 /* 5-ms async qh unlink delay */
99 99
100/* Initial IRQ latency: faster than hw default */ 100/* Initial IRQ latency: faster than hw default */
101static int log2_irq_thresh = 0; // 0 to 6 101static int log2_irq_thresh = 0; // 0 to 6
@@ -238,7 +238,7 @@ static int handshake_on_error_set_halt(struct ehci_hcd *ehci, void __iomem *ptr,
238 error = handshake(ehci, ptr, mask, done, usec); 238 error = handshake(ehci, ptr, mask, done, usec);
239 if (error) { 239 if (error) {
240 ehci_halt(ehci); 240 ehci_halt(ehci);
241 ehci_to_hcd(ehci)->state = HC_STATE_HALT; 241 ehci->rh_state = EHCI_RH_HALTED;
242 ehci_err(ehci, "force halt; handshake %p %08x %08x -> %d\n", 242 ehci_err(ehci, "force halt; handshake %p %08x %08x -> %d\n",
243 ptr, mask, done, error); 243 ptr, mask, done, error);
244 } 244 }
@@ -278,7 +278,7 @@ static int ehci_reset (struct ehci_hcd *ehci)
278 command |= CMD_RESET; 278 command |= CMD_RESET;
279 dbg_cmd (ehci, "reset", command); 279 dbg_cmd (ehci, "reset", command);
280 ehci_writel(ehci, command, &ehci->regs->command); 280 ehci_writel(ehci, command, &ehci->regs->command);
281 ehci_to_hcd(ehci)->state = HC_STATE_HALT; 281 ehci->rh_state = EHCI_RH_HALTED;
282 ehci->next_statechange = jiffies; 282 ehci->next_statechange = jiffies;
283 retval = handshake (ehci, &ehci->regs->command, 283 retval = handshake (ehci, &ehci->regs->command,
284 CMD_RESET, 0, 250 * 1000); 284 CMD_RESET, 0, 250 * 1000);
@@ -307,7 +307,7 @@ static void ehci_quiesce (struct ehci_hcd *ehci)
307 u32 temp; 307 u32 temp;
308 308
309#ifdef DEBUG 309#ifdef DEBUG
310 if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) 310 if (ehci->rh_state != EHCI_RH_RUNNING)
311 BUG (); 311 BUG ();
312#endif 312#endif
313 313
@@ -356,7 +356,7 @@ static void ehci_iaa_watchdog(unsigned long param)
356 */ 356 */
357 if (ehci->reclaim 357 if (ehci->reclaim
358 && !timer_pending(&ehci->iaa_watchdog) 358 && !timer_pending(&ehci->iaa_watchdog)
359 && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { 359 && ehci->rh_state == EHCI_RH_RUNNING) {
360 u32 cmd, status; 360 u32 cmd, status;
361 361
362 /* If we get here, IAA is *REALLY* late. It's barely 362 /* If we get here, IAA is *REALLY* late. It's barely
@@ -496,7 +496,7 @@ static void ehci_work (struct ehci_hcd *ehci)
496 * misplace IRQs, and should let us run completely without IRQs. 496 * misplace IRQs, and should let us run completely without IRQs.
497 * such lossage has been observed on both VT6202 and VT8235. 497 * such lossage has been observed on both VT6202 and VT8235.
498 */ 498 */
499 if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && 499 if (ehci->rh_state == EHCI_RH_RUNNING &&
500 (ehci->async->qh_next.ptr != NULL || 500 (ehci->async->qh_next.ptr != NULL ||
501 ehci->periodic_sched != 0)) 501 ehci->periodic_sched != 0))
502 timer_action (ehci, TIMER_IO_WATCHDOG); 502 timer_action (ehci, TIMER_IO_WATCHDOG);
@@ -516,7 +516,7 @@ static void ehci_stop (struct usb_hcd *hcd)
516 del_timer_sync(&ehci->iaa_watchdog); 516 del_timer_sync(&ehci->iaa_watchdog);
517 517
518 spin_lock_irq(&ehci->lock); 518 spin_lock_irq(&ehci->lock);
519 if (HC_IS_RUNNING (hcd->state)) 519 if (ehci->rh_state == EHCI_RH_RUNNING)
520 ehci_quiesce (ehci); 520 ehci_quiesce (ehci);
521 521
522 ehci_silence_controller(ehci); 522 ehci_silence_controller(ehci);
@@ -741,7 +741,7 @@ static int ehci_run (struct usb_hcd *hcd)
741 * be started before the port switching actions could complete. 741 * be started before the port switching actions could complete.
742 */ 742 */
743 down_write(&ehci_cf_port_reset_rwsem); 743 down_write(&ehci_cf_port_reset_rwsem);
744 hcd->state = HC_STATE_RUNNING; 744 ehci->rh_state = EHCI_RH_RUNNING;
745 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag); 745 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
746 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */ 746 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
747 msleep(5); 747 msleep(5);
@@ -768,6 +768,35 @@ static int ehci_run (struct usb_hcd *hcd)
768 return 0; 768 return 0;
769} 769}
770 770
771static int __maybe_unused ehci_setup (struct usb_hcd *hcd)
772{
773 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
774 int retval;
775
776 ehci->regs = (void __iomem *)ehci->caps +
777 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
778 dbg_hcs_params(ehci, "reset");
779 dbg_hcc_params(ehci, "reset");
780
781 /* cache this readonly data; minimize chip reads */
782 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
783
784 ehci->sbrn = HCD_USB2;
785
786 retval = ehci_halt(ehci);
787 if (retval)
788 return retval;
789
790 /* data structure init */
791 retval = ehci_init(hcd);
792 if (retval)
793 return retval;
794
795 ehci_reset(ehci);
796
797 return 0;
798}
799
771/*-------------------------------------------------------------------------*/ 800/*-------------------------------------------------------------------------*/
772 801
773static irqreturn_t ehci_irq (struct usb_hcd *hcd) 802static irqreturn_t ehci_irq (struct usb_hcd *hcd)
@@ -788,7 +817,7 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
788 817
789 /* Shared IRQ? */ 818 /* Shared IRQ? */
790 masked_status = status & INTR_MASK; 819 masked_status = status & INTR_MASK;
791 if (!masked_status || unlikely(hcd->state == HC_STATE_HALT)) { 820 if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
792 spin_unlock(&ehci->lock); 821 spin_unlock(&ehci->lock);
793 return IRQ_NONE; 822 return IRQ_NONE;
794 } 823 }
@@ -952,7 +981,7 @@ static int ehci_urb_enqueue (
952static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh) 981static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
953{ 982{
954 /* failfast */ 983 /* failfast */
955 if (!HC_IS_RUNNING(ehci_to_hcd(ehci)->state) && ehci->reclaim) 984 if (ehci->rh_state != EHCI_RH_RUNNING && ehci->reclaim)
956 end_unlink_async(ehci); 985 end_unlink_async(ehci);
957 986
958 /* If the QH isn't linked then there's nothing we can do 987 /* If the QH isn't linked then there's nothing we can do
@@ -1079,7 +1108,7 @@ rescan:
1079 goto idle_timeout; 1108 goto idle_timeout;
1080 } 1109 }
1081 1110
1082 if (!HC_IS_RUNNING (hcd->state)) 1111 if (ehci->rh_state != EHCI_RH_RUNNING)
1083 qh->qh_state = QH_STATE_IDLE; 1112 qh->qh_state = QH_STATE_IDLE;
1084 switch (qh->qh_state) { 1113 switch (qh->qh_state) {
1085 case QH_STATE_LINKED: 1114 case QH_STATE_LINKED:
@@ -1166,8 +1195,7 @@ ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1166static int ehci_get_frame (struct usb_hcd *hcd) 1195static int ehci_get_frame (struct usb_hcd *hcd)
1167{ 1196{
1168 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 1197 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
1169 return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) % 1198 return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
1170 ehci->periodic_size;
1171} 1199}
1172 1200
1173/*-------------------------------------------------------------------------*/ 1201/*-------------------------------------------------------------------------*/
@@ -1196,7 +1224,7 @@ MODULE_LICENSE ("GPL");
1196#define PLATFORM_DRIVER ehci_hcd_sh_driver 1224#define PLATFORM_DRIVER ehci_hcd_sh_driver
1197#endif 1225#endif
1198 1226
1199#ifdef CONFIG_SOC_AU1200 1227#ifdef CONFIG_MIPS_ALCHEMY
1200#include "ehci-au1xxx.c" 1228#include "ehci-au1xxx.c"
1201#define PLATFORM_DRIVER ehci_hcd_au1xxx_driver 1229#define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
1202#endif 1230#endif
@@ -1291,6 +1319,16 @@ MODULE_LICENSE ("GPL");
1291#define PLATFORM_DRIVER ehci_grlib_driver 1319#define PLATFORM_DRIVER ehci_grlib_driver
1292#endif 1320#endif
1293 1321
1322#ifdef CONFIG_USB_PXA168_EHCI
1323#include "ehci-pxa168.c"
1324#define PLATFORM_DRIVER ehci_pxa168_driver
1325#endif
1326
1327#ifdef CONFIG_NLM_XLR
1328#include "ehci-xls.c"
1329#define PLATFORM_DRIVER ehci_xls_driver
1330#endif
1331
1294#if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \ 1332#if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
1295 !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \ 1333 !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \
1296 !defined(XILINX_OF_PLATFORM_DRIVER) 1334 !defined(XILINX_OF_PLATFORM_DRIVER)
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 4c32cb19b405..77bbb2357e47 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -236,10 +236,8 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
236 } 236 }
237 237
238 /* stop schedules, clean any completed work */ 238 /* stop schedules, clean any completed work */
239 if (HC_IS_RUNNING(hcd->state)) { 239 if (ehci->rh_state == EHCI_RH_RUNNING)
240 ehci_quiesce (ehci); 240 ehci_quiesce (ehci);
241 hcd->state = HC_STATE_QUIESCING;
242 }
243 ehci->command = ehci_readl(ehci, &ehci->regs->command); 241 ehci->command = ehci_readl(ehci, &ehci->regs->command);
244 ehci_work(ehci); 242 ehci_work(ehci);
245 243
@@ -313,7 +311,7 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
313 311
314 /* turn off now-idle HC */ 312 /* turn off now-idle HC */
315 ehci_halt (ehci); 313 ehci_halt (ehci);
316 hcd->state = HC_STATE_SUSPENDED; 314 ehci->rh_state = EHCI_RH_SUSPENDED;
317 315
318 if (ehci->reclaim) 316 if (ehci->reclaim)
319 end_unlink_async(ehci); 317 end_unlink_async(ehci);
@@ -382,6 +380,7 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
382 380
383 /* restore CMD_RUN, framelist size, and irq threshold */ 381 /* restore CMD_RUN, framelist size, and irq threshold */
384 ehci_writel(ehci, ehci->command, &ehci->regs->command); 382 ehci_writel(ehci, ehci->command, &ehci->regs->command);
383 ehci->rh_state = EHCI_RH_RUNNING;
385 384
386 /* Some controller/firmware combinations need a delay during which 385 /* Some controller/firmware combinations need a delay during which
387 * they set up the port statuses. See Bugzilla #8190. */ 386 * they set up the port statuses. See Bugzilla #8190. */
@@ -451,7 +450,6 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
451 } 450 }
452 451
453 ehci->next_statechange = jiffies + msecs_to_jiffies(5); 452 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
454 hcd->state = HC_STATE_RUNNING;
455 453
456 /* Now we can safely re-enable irqs */ 454 /* Now we can safely re-enable irqs */
457 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable); 455 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
@@ -563,7 +561,7 @@ ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
563 u32 ppcd = 0; 561 u32 ppcd = 0;
564 562
565 /* if !USB_SUSPEND, root hub timers won't get shut down ... */ 563 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
566 if (!HC_IS_RUNNING(hcd->state)) 564 if (ehci->rh_state != EHCI_RH_RUNNING)
567 return 0; 565 return 0;
568 566
569 /* init status to no-changes */ 567 /* init status to no-changes */
diff --git a/drivers/usb/host/ehci-mxc.c b/drivers/usb/host/ehci-mxc.c
index 555a73c864b5..55978fcfa4b2 100644
--- a/drivers/usb/host/ehci-mxc.c
+++ b/drivers/usb/host/ehci-mxc.c
@@ -236,7 +236,7 @@ static int ehci_mxc_drv_probe(struct platform_device *pdev)
236 priv->hcd = hcd; 236 priv->hcd = hcd;
237 platform_set_drvdata(pdev, priv); 237 platform_set_drvdata(pdev, priv);
238 238
239 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 239 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
240 if (ret) 240 if (ret)
241 goto err_add; 241 goto err_add;
242 242
diff --git a/drivers/usb/host/ehci-octeon.c b/drivers/usb/host/ehci-octeon.c
index c3ba3ed5f3a6..ba1f51361134 100644
--- a/drivers/usb/host/ehci-octeon.c
+++ b/drivers/usb/host/ehci-octeon.c
@@ -155,7 +155,7 @@ static int ehci_octeon_drv_probe(struct platform_device *pdev)
155 /* cache this readonly data; minimize chip reads */ 155 /* cache this readonly data; minimize chip reads */
156 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params); 156 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
157 157
158 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 158 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
159 if (ret) { 159 if (ret) {
160 dev_dbg(&pdev->dev, "failed to add hcd with err %d\n", ret); 160 dev_dbg(&pdev->dev, "failed to add hcd with err %d\n", ret);
161 goto err3; 161 goto err3;
diff --git a/drivers/usb/host/ehci-omap.c b/drivers/usb/host/ehci-omap.c
index 45240321ca09..e39b0297bad1 100644
--- a/drivers/usb/host/ehci-omap.c
+++ b/drivers/usb/host/ehci-omap.c
@@ -228,7 +228,7 @@ static int ehci_hcd_omap_probe(struct platform_device *pdev)
228 /* cache this readonly data; minimize chip reads */ 228 /* cache this readonly data; minimize chip reads */
229 omap_ehci->hcs_params = readl(&omap_ehci->caps->hcs_params); 229 omap_ehci->hcs_params = readl(&omap_ehci->caps->hcs_params);
230 230
231 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 231 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
232 if (ret) { 232 if (ret) {
233 dev_err(dev, "failed to add hcd with err %d\n", ret); 233 dev_err(dev, "failed to add hcd with err %d\n", ret);
234 goto err_add_hcd; 234 goto err_add_hcd;
diff --git a/drivers/usb/host/ehci-orion.c b/drivers/usb/host/ehci-orion.c
index 395bdb0248d5..a68a2a5c4b83 100644
--- a/drivers/usb/host/ehci-orion.c
+++ b/drivers/usb/host/ehci-orion.c
@@ -277,7 +277,7 @@ static int __devinit ehci_orion_drv_probe(struct platform_device *pdev)
277 printk(KERN_WARNING "Orion ehci -USB phy version isn't supported.\n"); 277 printk(KERN_WARNING "Orion ehci -USB phy version isn't supported.\n");
278 } 278 }
279 279
280 err = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED); 280 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
281 if (err) 281 if (err)
282 goto err4; 282 goto err4;
283 283
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index 1102ce65a3a9..f4b627d343ac 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -224,6 +224,11 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
224 pci_dev_put(p_smbus); 224 pci_dev_put(p_smbus);
225 } 225 }
226 break; 226 break;
227 case PCI_VENDOR_ID_NETMOS:
228 /* MosChip frame-index-register bug */
229 ehci_info(ehci, "applying MosChip frame-index workaround\n");
230 ehci->frame_index_bug = 1;
231 break;
227 } 232 }
228 233
229 /* optional debug port, normally in the first BAR */ 234 /* optional debug port, normally in the first BAR */
@@ -439,7 +444,7 @@ static int ehci_pci_resume(struct usb_hcd *hcd, bool hibernated)
439 /* here we "know" root ports should always stay powered */ 444 /* here we "know" root ports should always stay powered */
440 ehci_port_power(ehci, 1); 445 ehci_port_power(ehci, 1);
441 446
442 hcd->state = HC_STATE_SUSPENDED; 447 ehci->rh_state = EHCI_RH_SUSPENDED;
443 return 0; 448 return 0;
444} 449}
445#endif 450#endif
diff --git a/drivers/usb/host/ehci-ps3.c b/drivers/usb/host/ehci-ps3.c
index 64626a777d61..2dc32da75cfc 100644
--- a/drivers/usb/host/ehci-ps3.c
+++ b/drivers/usb/host/ehci-ps3.c
@@ -167,7 +167,7 @@ static int __devinit ps3_ehci_probe(struct ps3_system_bus_device *dev)
167 167
168 ps3_system_bus_set_drvdata(dev, hcd); 168 ps3_system_bus_set_drvdata(dev, hcd);
169 169
170 result = usb_add_hcd(hcd, virq, IRQF_DISABLED); 170 result = usb_add_hcd(hcd, virq, 0);
171 171
172 if (result) { 172 if (result) {
173 dev_dbg(&dev->core, "%s:%d: usb_add_hcd failed (%d)\n", 173 dev_dbg(&dev->core, "%s:%d: usb_add_hcd failed (%d)\n",
diff --git a/drivers/usb/host/ehci-pxa168.c b/drivers/usb/host/ehci-pxa168.c
new file mode 100644
index 000000000000..ac0c16e8f539
--- /dev/null
+++ b/drivers/usb/host/ehci-pxa168.c
@@ -0,0 +1,363 @@
1/*
2 * drivers/usb/host/ehci-pxa168.c
3 *
4 * Tanmay Upadhyay <tanmay.upadhyay@einfochips.com>
5 *
6 * Based on drivers/usb/host/ehci-orion.c
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/clk.h>
17#include <mach/pxa168.h>
18
19#define USB_PHY_CTRL_REG 0x4
20#define USB_PHY_PLL_REG 0x8
21#define USB_PHY_TX_REG 0xc
22
23#define FBDIV_SHIFT 4
24
25#define ICP_SHIFT 12
26#define ICP_15 2
27#define ICP_20 3
28#define ICP_25 4
29
30#define KVCO_SHIFT 15
31
32#define PLLCALI12_SHIFT 25
33#define CALI12_VDD 0
34#define CALI12_09 1
35#define CALI12_10 2
36#define CALI12_11 3
37
38#define PLLVDD12_SHIFT 27
39#define VDD12_VDD 0
40#define VDD12_10 1
41#define VDD12_11 2
42#define VDD12_12 3
43
44#define PLLVDD18_SHIFT 29
45#define VDD18_19 0
46#define VDD18_20 1
47#define VDD18_21 2
48#define VDD18_22 3
49
50
51#define PLL_READY (1 << 23)
52#define VCOCAL_START (1 << 21)
53#define REG_RCAL_START (1 << 12)
54
55struct pxa168_usb_drv_data {
56 struct ehci_hcd ehci;
57 struct clk *pxa168_usb_clk;
58 struct resource *usb_phy_res;
59 void __iomem *usb_phy_reg_base;
60};
61
62static int ehci_pxa168_setup(struct usb_hcd *hcd)
63{
64 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
65 int retval;
66
67 ehci_reset(ehci);
68 retval = ehci_halt(ehci);
69 if (retval)
70 return retval;
71
72 /*
73 * data structure init
74 */
75 retval = ehci_init(hcd);
76 if (retval)
77 return retval;
78
79 hcd->has_tt = 1;
80
81 ehci_port_power(ehci, 0);
82
83 return retval;
84}
85
86static const struct hc_driver ehci_pxa168_hc_driver = {
87 .description = hcd_name,
88 .product_desc = "Marvell PXA168 EHCI",
89 .hcd_priv_size = sizeof(struct pxa168_usb_drv_data),
90
91 /*
92 * generic hardware linkage
93 */
94 .irq = ehci_irq,
95 .flags = HCD_MEMORY | HCD_USB2,
96
97 /*
98 * basic lifecycle operations
99 */
100 .reset = ehci_pxa168_setup,
101 .start = ehci_run,
102 .stop = ehci_stop,
103 .shutdown = ehci_shutdown,
104
105 /*
106 * managing i/o requests and associated device resources
107 */
108 .urb_enqueue = ehci_urb_enqueue,
109 .urb_dequeue = ehci_urb_dequeue,
110 .endpoint_disable = ehci_endpoint_disable,
111 .endpoint_reset = ehci_endpoint_reset,
112
113 /*
114 * scheduling support
115 */
116 .get_frame_number = ehci_get_frame,
117
118 /*
119 * root hub support
120 */
121 .hub_status_data = ehci_hub_status_data,
122 .hub_control = ehci_hub_control,
123 .bus_suspend = ehci_bus_suspend,
124 .bus_resume = ehci_bus_resume,
125 .relinquish_port = ehci_relinquish_port,
126 .port_handed_over = ehci_port_handed_over,
127
128 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
129};
130
131static int pxa168_usb_phy_init(struct platform_device *pdev)
132{
133 struct resource *res;
134 void __iomem *usb_phy_reg_base;
135 struct pxa168_usb_pdata *pdata;
136 struct pxa168_usb_drv_data *drv_data;
137 struct usb_hcd *hcd = platform_get_drvdata(pdev);
138 unsigned long reg_val;
139 int pll_retry_cont = 10000, err = 0;
140
141 drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;
142 pdata = (struct pxa168_usb_pdata *)pdev->dev.platform_data;
143
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
145 if (!res) {
146 dev_err(&pdev->dev,
147 "Found HC with no PHY register addr. Check %s setup!\n",
148 dev_name(&pdev->dev));
149 return -ENODEV;
150 }
151
152 if (!request_mem_region(res->start, resource_size(res),
153 ehci_pxa168_hc_driver.description)) {
154 dev_dbg(&pdev->dev, "controller already in use\n");
155 return -EBUSY;
156 }
157
158 usb_phy_reg_base = ioremap(res->start, resource_size(res));
159 if (usb_phy_reg_base == NULL) {
160 dev_dbg(&pdev->dev, "error mapping memory\n");
161 err = -EFAULT;
162 goto err1;
163 }
164 drv_data->usb_phy_reg_base = usb_phy_reg_base;
165 drv_data->usb_phy_res = res;
166
167 /* If someone wants to init USB phy in board specific way */
168 if (pdata && pdata->phy_init)
169 return pdata->phy_init(usb_phy_reg_base);
170
171 /* Power up the PHY and PLL */
172 writel(readl(usb_phy_reg_base + USB_PHY_CTRL_REG) | 0x3,
173 usb_phy_reg_base + USB_PHY_CTRL_REG);
174
175 /* Configure PHY PLL */
176 reg_val = readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~(0x7e03ffff);
177 reg_val |= (VDD18_22 << PLLVDD18_SHIFT | VDD12_12 << PLLVDD12_SHIFT |
178 CALI12_11 << PLLCALI12_SHIFT | 3 << KVCO_SHIFT |
179 ICP_15 << ICP_SHIFT | 0xee << FBDIV_SHIFT | 0xb);
180 writel(reg_val, usb_phy_reg_base + USB_PHY_PLL_REG);
181
182 /* Make sure PHY PLL is ready */
183 while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
184 if (!(pll_retry_cont--)) {
185 dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
186 err = -EIO;
187 goto err2;
188 }
189 }
190
191 /* Toggle VCOCAL_START bit of U2PLL for PLL calibration */
192 udelay(200);
193 writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) | VCOCAL_START,
194 usb_phy_reg_base + USB_PHY_PLL_REG);
195 udelay(40);
196 writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~VCOCAL_START,
197 usb_phy_reg_base + USB_PHY_PLL_REG);
198
199 /* Toggle REG_RCAL_START bit of U2PTX for impedance calibration */
200 udelay(400);
201 writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) | REG_RCAL_START,
202 usb_phy_reg_base + USB_PHY_TX_REG);
203 udelay(40);
204 writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) & ~REG_RCAL_START,
205 usb_phy_reg_base + USB_PHY_TX_REG);
206
207 /* Make sure PHY PLL is ready again */
208 pll_retry_cont = 0;
209 while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
210 if (!(pll_retry_cont--)) {
211 dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
212 err = -EIO;
213 goto err2;
214 }
215 }
216
217 return 0;
218err2:
219 iounmap(usb_phy_reg_base);
220err1:
221 release_mem_region(res->start, resource_size(res));
222 return err;
223}
224
225static int __devinit ehci_pxa168_drv_probe(struct platform_device *pdev)
226{
227 struct resource *res;
228 struct usb_hcd *hcd;
229 struct ehci_hcd *ehci;
230 struct pxa168_usb_drv_data *drv_data;
231 void __iomem *regs;
232 int irq, err = 0;
233
234 if (usb_disabled())
235 return -ENODEV;
236
237 pr_debug("Initializing pxa168-SoC USB Host Controller\n");
238
239 irq = platform_get_irq(pdev, 0);
240 if (irq <= 0) {
241 dev_err(&pdev->dev,
242 "Found HC with no IRQ. Check %s setup!\n",
243 dev_name(&pdev->dev));
244 err = -ENODEV;
245 goto err1;
246 }
247
248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 if (!res) {
250 dev_err(&pdev->dev,
251 "Found HC with no register addr. Check %s setup!\n",
252 dev_name(&pdev->dev));
253 err = -ENODEV;
254 goto err1;
255 }
256
257 if (!request_mem_region(res->start, resource_size(res),
258 ehci_pxa168_hc_driver.description)) {
259 dev_dbg(&pdev->dev, "controller already in use\n");
260 err = -EBUSY;
261 goto err1;
262 }
263
264 regs = ioremap(res->start, resource_size(res));
265 if (regs == NULL) {
266 dev_dbg(&pdev->dev, "error mapping memory\n");
267 err = -EFAULT;
268 goto err2;
269 }
270
271 hcd = usb_create_hcd(&ehci_pxa168_hc_driver,
272 &pdev->dev, dev_name(&pdev->dev));
273 if (!hcd) {
274 err = -ENOMEM;
275 goto err3;
276 }
277
278 drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;
279
280 /* Enable USB clock */
281 drv_data->pxa168_usb_clk = clk_get(&pdev->dev, "PXA168-USBCLK");
282 if (IS_ERR(drv_data->pxa168_usb_clk)) {
283 dev_err(&pdev->dev, "Couldn't get USB clock\n");
284 err = PTR_ERR(drv_data->pxa168_usb_clk);
285 goto err4;
286 }
287 clk_enable(drv_data->pxa168_usb_clk);
288
289 err = pxa168_usb_phy_init(pdev);
290 if (err) {
291 dev_err(&pdev->dev, "USB PHY initialization failed\n");
292 goto err5;
293 }
294
295 hcd->rsrc_start = res->start;
296 hcd->rsrc_len = resource_size(res);
297 hcd->regs = regs;
298
299 ehci = hcd_to_ehci(hcd);
300 ehci->caps = hcd->regs + 0x100;
301 ehci->regs = hcd->regs + 0x100 +
302 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
303 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
304 hcd->has_tt = 1;
305 ehci->sbrn = 0x20;
306
307 err = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
308 if (err)
309 goto err5;
310
311 return 0;
312
313err5:
314 clk_disable(drv_data->pxa168_usb_clk);
315 clk_put(drv_data->pxa168_usb_clk);
316err4:
317 usb_put_hcd(hcd);
318err3:
319 iounmap(regs);
320err2:
321 release_mem_region(res->start, resource_size(res));
322err1:
323 dev_err(&pdev->dev, "init %s fail, %d\n",
324 dev_name(&pdev->dev), err);
325
326 return err;
327}
328
329static int __exit ehci_pxa168_drv_remove(struct platform_device *pdev)
330{
331 struct usb_hcd *hcd = platform_get_drvdata(pdev);
332 struct pxa168_usb_drv_data *drv_data =
333 (struct pxa168_usb_drv_data *)hcd->hcd_priv;
334
335 usb_remove_hcd(hcd);
336
337 /* Power down PHY & PLL */
338 writel(readl(drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG) & (~0x3),
339 drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG);
340
341 clk_disable(drv_data->pxa168_usb_clk);
342 clk_put(drv_data->pxa168_usb_clk);
343
344 iounmap(hcd->regs);
345 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
346
347 iounmap(drv_data->usb_phy_reg_base);
348 release_mem_region(drv_data->usb_phy_res->start,
349 resource_size(drv_data->usb_phy_res));
350
351 usb_put_hcd(hcd);
352
353 return 0;
354}
355
356MODULE_ALIAS("platform:pxa168-ehci");
357
358static struct platform_driver ehci_pxa168_driver = {
359 .probe = ehci_pxa168_drv_probe,
360 .remove = __exit_p(ehci_pxa168_drv_remove),
361 .shutdown = usb_hcd_platform_shutdown,
362 .driver.name = "pxa168-ehci",
363};
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 0917e3a32465..4e4066c35a09 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -111,8 +111,6 @@ qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
111 } 111 }
112 } 112 }
113 113
114 /* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
115 wmb ();
116 hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING); 114 hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
117} 115}
118 116
@@ -153,7 +151,7 @@ static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
153 spin_lock_irqsave(&ehci->lock, flags); 151 spin_lock_irqsave(&ehci->lock, flags);
154 qh->clearing_tt = 0; 152 qh->clearing_tt = 0;
155 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list) 153 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
156 && HC_IS_RUNNING(hcd->state)) 154 && ehci->rh_state == EHCI_RH_RUNNING)
157 qh_link_async(ehci, qh); 155 qh_link_async(ehci, qh);
158 spin_unlock_irqrestore(&ehci->lock, flags); 156 spin_unlock_irqrestore(&ehci->lock, flags);
159} 157}
@@ -425,7 +423,7 @@ qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
425 423
426 /* stop scanning when we reach qtds the hc is using */ 424 /* stop scanning when we reach qtds the hc is using */
427 } else if (likely (!stopped 425 } else if (likely (!stopped
428 && HC_IS_RUNNING (ehci_to_hcd(ehci)->state))) { 426 && ehci->rh_state == EHCI_RH_RUNNING)) {
429 break; 427 break;
430 428
431 /* scan the whole queue for unlinks whenever it stops */ 429 /* scan the whole queue for unlinks whenever it stops */
@@ -433,7 +431,7 @@ qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
433 stopped = 1; 431 stopped = 1;
434 432
435 /* cancel everything if we halt, suspend, etc */ 433 /* cancel everything if we halt, suspend, etc */
436 if (!HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) 434 if (ehci->rh_state != EHCI_RH_RUNNING)
437 last_status = -ESHUTDOWN; 435 last_status = -ESHUTDOWN;
438 436
439 /* this qtd is active; skip it unless a previous qtd 437 /* this qtd is active; skip it unless a previous qtd
@@ -724,7 +722,8 @@ qh_urb_transaction (
724 722
725 /* 723 /*
726 * control requests may need a terminating data "status" ack; 724 * control requests may need a terminating data "status" ack;
727 * bulk ones may need a terminating short packet (zero length). 725 * other OUT ones may need a terminating short packet
726 * (zero length).
728 */ 727 */
729 if (likely (urb->transfer_buffer_length != 0)) { 728 if (likely (urb->transfer_buffer_length != 0)) {
730 int one_more = 0; 729 int one_more = 0;
@@ -733,7 +732,7 @@ qh_urb_transaction (
733 one_more = 1; 732 one_more = 1;
734 token ^= 0x0100; /* "in" <--> "out" */ 733 token ^= 0x0100; /* "in" <--> "out" */
735 token |= QTD_TOGGLE; /* force DATA1 */ 734 token |= QTD_TOGGLE; /* force DATA1 */
736 } else if (usb_pipebulk (urb->pipe) 735 } else if (usb_pipeout(urb->pipe)
737 && (urb->transfer_flags & URB_ZERO_PACKET) 736 && (urb->transfer_flags & URB_ZERO_PACKET)
738 && !(urb->transfer_buffer_length % maxpacket)) { 737 && !(urb->transfer_buffer_length % maxpacket)) {
739 one_more = 1; 738 one_more = 1;
@@ -977,9 +976,8 @@ static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
977 /* in case a clear of CMD_ASE didn't take yet */ 976 /* in case a clear of CMD_ASE didn't take yet */
978 (void)handshake(ehci, &ehci->regs->status, 977 (void)handshake(ehci, &ehci->regs->status,
979 STS_ASS, 0, 150); 978 STS_ASS, 0, 150);
980 cmd |= CMD_ASE | CMD_RUN; 979 cmd |= CMD_ASE;
981 ehci_writel(ehci, cmd, &ehci->regs->command); 980 ehci_writel(ehci, cmd, &ehci->regs->command);
982 ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
983 /* posted write need not be known to HC yet ... */ 981 /* posted write need not be known to HC yet ... */
984 } 982 }
985 } 983 }
@@ -1058,7 +1056,7 @@ static struct ehci_qh *qh_append_tds (
1058 */ 1056 */
1059 token = qtd->hw_token; 1057 token = qtd->hw_token;
1060 qtd->hw_token = HALT_BIT(ehci); 1058 qtd->hw_token = HALT_BIT(ehci);
1061 wmb (); 1059
1062 dummy = qh->dummy; 1060 dummy = qh->dummy;
1063 1061
1064 dma = dummy->qtd_dma; 1062 dma = dummy->qtd_dma;
@@ -1168,14 +1166,13 @@ static void end_unlink_async (struct ehci_hcd *ehci)
1168 1166
1169 qh_completions (ehci, qh); 1167 qh_completions (ehci, qh);
1170 1168
1171 if (!list_empty (&qh->qtd_list) 1169 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
1172 && HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
1173 qh_link_async (ehci, qh); 1170 qh_link_async (ehci, qh);
1174 else { 1171 } else {
1175 /* it's not free to turn the async schedule on/off; leave it 1172 /* it's not free to turn the async schedule on/off; leave it
1176 * active but idle for a while once it empties. 1173 * active but idle for a while once it empties.
1177 */ 1174 */
1178 if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) 1175 if (ehci->rh_state == EHCI_RH_RUNNING
1179 && ehci->async->qh_next.qh == NULL) 1176 && ehci->async->qh_next.qh == NULL)
1180 timer_action (ehci, TIMER_ASYNC_OFF); 1177 timer_action (ehci, TIMER_ASYNC_OFF);
1181 } 1178 }
@@ -1211,7 +1208,7 @@ static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
1211 /* stop async schedule right now? */ 1208 /* stop async schedule right now? */
1212 if (unlikely (qh == ehci->async)) { 1209 if (unlikely (qh == ehci->async)) {
1213 /* can't get here without STS_ASS set */ 1210 /* can't get here without STS_ASS set */
1214 if (ehci_to_hcd(ehci)->state != HC_STATE_HALT 1211 if (ehci->rh_state != EHCI_RH_HALTED
1215 && !ehci->reclaim) { 1212 && !ehci->reclaim) {
1216 /* ... and CMD_IAAD clear */ 1213 /* ... and CMD_IAAD clear */
1217 ehci_writel(ehci, cmd & ~CMD_ASE, 1214 ehci_writel(ehci, cmd & ~CMD_ASE,
@@ -1237,7 +1234,7 @@ static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
1237 wmb (); 1234 wmb ();
1238 1235
1239 /* If the controller isn't running, we don't have to wait for it */ 1236 /* If the controller isn't running, we don't have to wait for it */
1240 if (unlikely(!HC_IS_RUNNING(ehci_to_hcd(ehci)->state))) { 1237 if (unlikely(ehci->rh_state != EHCI_RH_RUNNING)) {
1241 /* if (unlikely (qh->reclaim != 0)) 1238 /* if (unlikely (qh->reclaim != 0))
1242 * this will recurse, probably not much 1239 * this will recurse, probably not much
1243 */ 1240 */
@@ -1260,7 +1257,7 @@ static void scan_async (struct ehci_hcd *ehci)
1260 enum ehci_timer_action action = TIMER_IO_WATCHDOG; 1257 enum ehci_timer_action action = TIMER_IO_WATCHDOG;
1261 1258
1262 timer_action_done (ehci, TIMER_ASYNC_SHRINK); 1259 timer_action_done (ehci, TIMER_ASYNC_SHRINK);
1263 stopped = !HC_IS_RUNNING(ehci_to_hcd(ehci)->state); 1260 stopped = (ehci->rh_state != EHCI_RH_RUNNING);
1264 1261
1265 ehci->qh_scan_next = ehci->async->qh_next.qh; 1262 ehci->qh_scan_next = ehci->async->qh_next.qh;
1266 while (ehci->qh_scan_next) { 1263 while (ehci->qh_scan_next) {
diff --git a/drivers/usb/host/ehci-s5p.c b/drivers/usb/host/ehci-s5p.c
index 9e77f1c8bdbd..024b65c4990d 100644
--- a/drivers/usb/host/ehci-s5p.c
+++ b/drivers/usb/host/ehci-s5p.c
@@ -136,7 +136,7 @@ static int __devinit s5p_ehci_probe(struct platform_device *pdev)
136 /* cache this readonly data; minimize chip reads */ 136 /* cache this readonly data; minimize chip reads */
137 ehci->hcs_params = readl(&ehci->caps->hcs_params); 137 ehci->hcs_params = readl(&ehci->caps->hcs_params);
138 138
139 err = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 139 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
140 if (err) { 140 if (err) {
141 dev_err(&pdev->dev, "Failed to add USB HCD\n"); 141 dev_err(&pdev->dev, "Failed to add USB HCD\n");
142 goto fail; 142 goto fail;
@@ -270,7 +270,7 @@ static int s5p_ehci_resume(struct device *dev)
270 /* here we "know" root ports should always stay powered */ 270 /* here we "know" root ports should always stay powered */
271 ehci_port_power(ehci, 1); 271 ehci_port_power(ehci, 1);
272 272
273 hcd->state = HC_STATE_SUSPENDED; 273 ehci->rh_state = EHCI_RH_SUSPENDED;
274 274
275 return 0; 275 return 0;
276} 276}
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index 2abf8543f083..56a32033adb3 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -36,6 +36,27 @@
36 36
37static int ehci_get_frame (struct usb_hcd *hcd); 37static int ehci_get_frame (struct usb_hcd *hcd);
38 38
39#ifdef CONFIG_PCI
40
41static unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
42{
43 unsigned uf;
44
45 /*
46 * The MosChip MCS9990 controller updates its microframe counter
47 * a little before the frame counter, and occasionally we will read
48 * the invalid intermediate value. Avoid problems by checking the
49 * microframe number (the low-order 3 bits); if they are 0 then
50 * re-read the register to get the correct value.
51 */
52 uf = ehci_readl(ehci, &ehci->regs->frame_index);
53 if (unlikely(ehci->frame_index_bug && ((uf & 7) == 0)))
54 uf = ehci_readl(ehci, &ehci->regs->frame_index);
55 return uf;
56}
57
58#endif
59
39/*-------------------------------------------------------------------------*/ 60/*-------------------------------------------------------------------------*/
40 61
41/* 62/*
@@ -479,10 +500,9 @@ static int enable_periodic (struct ehci_hcd *ehci)
479 cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE; 500 cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
480 ehci_writel(ehci, cmd, &ehci->regs->command); 501 ehci_writel(ehci, cmd, &ehci->regs->command);
481 /* posted write ... PSS happens later */ 502 /* posted write ... PSS happens later */
482 ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
483 503
484 /* make sure ehci_work scans these */ 504 /* make sure ehci_work scans these */
485 ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index) 505 ehci->next_uframe = ehci_read_frame_index(ehci)
486 % (ehci->periodic_size << 3); 506 % (ehci->periodic_size << 3);
487 if (unlikely(ehci->broken_periodic)) 507 if (unlikely(ehci->broken_periodic))
488 ehci->last_periodic_enable = ktime_get_real(); 508 ehci->last_periodic_enable = ktime_get_real();
@@ -677,7 +697,7 @@ static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
677 697
678 /* reschedule QH iff another request is queued */ 698 /* reschedule QH iff another request is queued */
679 if (!list_empty(&qh->qtd_list) && 699 if (!list_empty(&qh->qtd_list) &&
680 HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { 700 ehci->rh_state == EHCI_RH_RUNNING) {
681 rc = qh_schedule(ehci, qh); 701 rc = qh_schedule(ehci, qh);
682 702
683 /* An error here likely indicates handshake failure 703 /* An error here likely indicates handshake failure
@@ -1409,7 +1429,7 @@ iso_stream_schedule (
1409 goto fail; 1429 goto fail;
1410 } 1430 }
1411 1431
1412 now = ehci_readl(ehci, &ehci->regs->frame_index) & (mod - 1); 1432 now = ehci_read_frame_index(ehci) & (mod - 1);
1413 1433
1414 /* Typical case: reuse current schedule, stream is still active. 1434 /* Typical case: reuse current schedule, stream is still active.
1415 * Hopefully there are no gaps from the host falling behind 1435 * Hopefully there are no gaps from the host falling behind
@@ -1459,10 +1479,15 @@ iso_stream_schedule (
1459 1479
1460 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */ 1480 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1461 1481
1462 /* find a uframe slot with enough bandwidth */ 1482 /* find a uframe slot with enough bandwidth.
1463 next = start + period; 1483 * Early uframes are more precious because full-speed
1464 for (; start < next; start++) { 1484 * iso IN transfers can't use late uframes,
1465 1485 * and therefore they should be allocated last.
1486 */
1487 next = start;
1488 start += period;
1489 do {
1490 start--;
1466 /* check schedule: enough space? */ 1491 /* check schedule: enough space? */
1467 if (stream->highspeed) { 1492 if (stream->highspeed) {
1468 if (itd_slot_ok(ehci, mod, start, 1493 if (itd_slot_ok(ehci, mod, start,
@@ -1475,7 +1500,7 @@ iso_stream_schedule (
1475 start, sched, period)) 1500 start, sched, period))
1476 break; 1501 break;
1477 } 1502 }
1478 } 1503 } while (start > next);
1479 1504
1480 /* no room in the schedule */ 1505 /* no room in the schedule */
1481 if (start == next) { 1506 if (start == next) {
@@ -2275,8 +2300,8 @@ scan_periodic (struct ehci_hcd *ehci)
2275 * Touches as few pages as possible: cache-friendly. 2300 * Touches as few pages as possible: cache-friendly.
2276 */ 2301 */
2277 now_uframe = ehci->next_uframe; 2302 now_uframe = ehci->next_uframe;
2278 if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { 2303 if (ehci->rh_state == EHCI_RH_RUNNING) {
2279 clock = ehci_readl(ehci, &ehci->regs->frame_index); 2304 clock = ehci_read_frame_index(ehci);
2280 clock_frame = (clock >> 3) & (ehci->periodic_size - 1); 2305 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
2281 } else { 2306 } else {
2282 clock = now_uframe + mod - 1; 2307 clock = now_uframe + mod - 1;
@@ -2310,7 +2335,7 @@ restart:
2310 union ehci_shadow temp; 2335 union ehci_shadow temp;
2311 int live; 2336 int live;
2312 2337
2313 live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state); 2338 live = (ehci->rh_state == EHCI_RH_RUNNING);
2314 switch (hc32_to_cpu(ehci, type)) { 2339 switch (hc32_to_cpu(ehci, type)) {
2315 case Q_TYPE_QH: 2340 case Q_TYPE_QH:
2316 /* handle any completions */ 2341 /* handle any completions */
@@ -2435,7 +2460,7 @@ restart:
2435 * We can't advance our scan without collecting the ISO 2460 * We can't advance our scan without collecting the ISO
2436 * transfers that are still pending in this frame. 2461 * transfers that are still pending in this frame.
2437 */ 2462 */
2438 if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { 2463 if (incomplete && ehci->rh_state == EHCI_RH_RUNNING) {
2439 ehci->next_uframe = now_uframe; 2464 ehci->next_uframe = now_uframe;
2440 break; 2465 break;
2441 } 2466 }
@@ -2451,12 +2476,11 @@ restart:
2451 if (now_uframe == clock) { 2476 if (now_uframe == clock) {
2452 unsigned now; 2477 unsigned now;
2453 2478
2454 if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) 2479 if (ehci->rh_state != EHCI_RH_RUNNING
2455 || ehci->periodic_sched == 0) 2480 || ehci->periodic_sched == 0)
2456 break; 2481 break;
2457 ehci->next_uframe = now_uframe; 2482 ehci->next_uframe = now_uframe;
2458 now = ehci_readl(ehci, &ehci->regs->frame_index) & 2483 now = ehci_read_frame_index(ehci) & (mod - 1);
2459 (mod - 1);
2460 if (now_uframe == now) 2484 if (now_uframe == now)
2461 break; 2485 break;
2462 2486
diff --git a/drivers/usb/host/ehci-sh.c b/drivers/usb/host/ehci-sh.c
index 86a95bb80a61..9d9cf47d80da 100644
--- a/drivers/usb/host/ehci-sh.c
+++ b/drivers/usb/host/ehci-sh.c
@@ -168,7 +168,7 @@ static int ehci_hcd_sh_probe(struct platform_device *pdev)
168 clk_enable(priv->fclk); 168 clk_enable(priv->fclk);
169 clk_enable(priv->iclk); 169 clk_enable(priv->iclk);
170 170
171 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 171 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
172 if (ret != 0) { 172 if (ret != 0) {
173 dev_err(&pdev->dev, "Failed to add hcd"); 173 dev_err(&pdev->dev, "Failed to add hcd");
174 goto fail_add_hcd; 174 goto fail_add_hcd;
diff --git a/drivers/usb/host/ehci-spear.c b/drivers/usb/host/ehci-spear.c
index dbf1e4ef3c17..b115b0b76e33 100644
--- a/drivers/usb/host/ehci-spear.c
+++ b/drivers/usb/host/ehci-spear.c
@@ -154,7 +154,7 @@ static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
154 ehci->clk = usbh_clk; 154 ehci->clk = usbh_clk;
155 155
156 spear_start_ehci(ehci); 156 spear_start_ehci(ehci);
157 retval = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED); 157 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
158 if (retval) 158 if (retval)
159 goto fail_add_hcd; 159 goto fail_add_hcd;
160 160
diff --git a/drivers/usb/host/ehci-tegra.c b/drivers/usb/host/ehci-tegra.c
index 02b2bfd49a10..db9d1b4bfbdc 100644
--- a/drivers/usb/host/ehci-tegra.c
+++ b/drivers/usb/host/ehci-tegra.c
@@ -674,7 +674,7 @@ static int tegra_ehci_probe(struct platform_device *pdev)
674 } 674 }
675#endif 675#endif
676 676
677 err = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 677 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
678 if (err) { 678 if (err) {
679 dev_err(&pdev->dev, "Failed to add USB HCD\n"); 679 dev_err(&pdev->dev, "Failed to add USB HCD\n");
680 goto fail; 680 goto fail;
diff --git a/drivers/usb/host/ehci-vt8500.c b/drivers/usb/host/ehci-vt8500.c
index 47d749631bc7..54d1ab8aec49 100644
--- a/drivers/usb/host/ehci-vt8500.c
+++ b/drivers/usb/host/ehci-vt8500.c
@@ -133,7 +133,7 @@ static int vt8500_ehci_drv_probe(struct platform_device *pdev)
133 ehci_port_power(ehci, 1); 133 ehci_port_power(ehci, 1);
134 134
135 ret = usb_add_hcd(hcd, pdev->resource[1].start, 135 ret = usb_add_hcd(hcd, pdev->resource[1].start,
136 IRQF_DISABLED | IRQF_SHARED); 136 IRQF_SHARED);
137 if (ret == 0) { 137 if (ret == 0) {
138 platform_set_drvdata(pdev, hcd); 138 platform_set_drvdata(pdev, hcd);
139 return ret; 139 return ret;
diff --git a/drivers/usb/host/ehci-xls.c b/drivers/usb/host/ehci-xls.c
new file mode 100644
index 000000000000..b4fb511d24bc
--- /dev/null
+++ b/drivers/usb/host/ehci-xls.c
@@ -0,0 +1,161 @@
1/*
2 * EHCI HCD for Netlogic XLS processors.
3 *
4 * (C) Copyright 2011 Netlogic Microsystems Inc.
5 *
6 * Based on various ehci-*.c drivers
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
11 */
12
13#include <linux/platform_device.h>
14
15static int ehci_xls_setup(struct usb_hcd *hcd)
16{
17 int retval;
18 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
19
20 ehci->caps = hcd->regs;
21 ehci->regs = hcd->regs +
22 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
23 dbg_hcs_params(ehci, "reset");
24 dbg_hcc_params(ehci, "reset");
25
26 /* cache this readonly data; minimize chip reads */
27 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
28
29 retval = ehci_halt(ehci);
30 if (retval)
31 return retval;
32
33 /* data structure init */
34 retval = ehci_init(hcd);
35 if (retval)
36 return retval;
37
38 ehci_reset(ehci);
39
40 return retval;
41}
42
43int ehci_xls_probe_internal(const struct hc_driver *driver,
44 struct platform_device *pdev)
45{
46 struct usb_hcd *hcd;
47 struct resource *res;
48 int retval, irq;
49
50 /* Get our IRQ from an earlier registered Platform Resource */
51 irq = platform_get_irq(pdev, 0);
52 if (irq < 0) {
53 dev_err(&pdev->dev, "Found HC with no IRQ. Check %s setup!\n",
54 dev_name(&pdev->dev));
55 return -ENODEV;
56 }
57
58 /* Get our Memory Handle */
59 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
60 if (!res) {
61 dev_err(&pdev->dev, "Error: MMIO Handle %s setup!\n",
62 dev_name(&pdev->dev));
63 return -ENODEV;
64 }
65 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
66 if (!hcd) {
67 retval = -ENOMEM;
68 goto err1;
69 }
70
71 hcd->rsrc_start = res->start;
72 hcd->rsrc_len = res->end - res->start + 1;
73
74 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
75 driver->description)) {
76 dev_dbg(&pdev->dev, "controller already in use\n");
77 retval = -EBUSY;
78 goto err2;
79 }
80 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
81
82 if (hcd->regs == NULL) {
83 dev_dbg(&pdev->dev, "error mapping memory\n");
84 retval = -EFAULT;
85 goto err3;
86 }
87
88 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
89 if (retval != 0)
90 goto err4;
91 return retval;
92
93err4:
94 iounmap(hcd->regs);
95err3:
96 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
97err2:
98 usb_put_hcd(hcd);
99err1:
100 dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev),
101 retval);
102 return retval;
103}
104
105static struct hc_driver ehci_xls_hc_driver = {
106 .description = hcd_name,
107 .product_desc = "XLS EHCI Host Controller",
108 .hcd_priv_size = sizeof(struct ehci_hcd),
109 .irq = ehci_irq,
110 .flags = HCD_USB2 | HCD_MEMORY,
111 .reset = ehci_xls_setup,
112 .start = ehci_run,
113 .stop = ehci_stop,
114 .shutdown = ehci_shutdown,
115
116 .urb_enqueue = ehci_urb_enqueue,
117 .urb_dequeue = ehci_urb_dequeue,
118 .endpoint_disable = ehci_endpoint_disable,
119 .endpoint_reset = ehci_endpoint_reset,
120
121 .get_frame_number = ehci_get_frame,
122
123 .hub_status_data = ehci_hub_status_data,
124 .hub_control = ehci_hub_control,
125 .bus_suspend = ehci_bus_suspend,
126 .bus_resume = ehci_bus_resume,
127 .relinquish_port = ehci_relinquish_port,
128 .port_handed_over = ehci_port_handed_over,
129
130 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
131};
132
133static int ehci_xls_probe(struct platform_device *pdev)
134{
135 if (usb_disabled())
136 return -ENODEV;
137
138 return ehci_xls_probe_internal(&ehci_xls_hc_driver, pdev);
139}
140
141static int ehci_xls_remove(struct platform_device *pdev)
142{
143 struct usb_hcd *hcd = platform_get_drvdata(pdev);
144
145 usb_remove_hcd(hcd);
146 iounmap(hcd->regs);
147 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
148 usb_put_hcd(hcd);
149 return 0;
150}
151
152MODULE_ALIAS("ehci-xls");
153
154static struct platform_driver ehci_xls_driver = {
155 .probe = ehci_xls_probe,
156 .remove = ehci_xls_remove,
157 .shutdown = usb_hcd_platform_shutdown,
158 .driver = {
159 .name = "ehci-xls",
160 },
161};
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index cc7d337ec355..0a5fda73b3f2 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -62,6 +62,12 @@ struct ehci_stats {
62 62
63#define EHCI_MAX_ROOT_PORTS 15 /* see HCS_N_PORTS */ 63#define EHCI_MAX_ROOT_PORTS 15 /* see HCS_N_PORTS */
64 64
65enum ehci_rh_state {
66 EHCI_RH_HALTED,
67 EHCI_RH_SUSPENDED,
68 EHCI_RH_RUNNING
69};
70
65struct ehci_hcd { /* one per controller */ 71struct ehci_hcd { /* one per controller */
66 /* glue to PCI and HCD framework */ 72 /* glue to PCI and HCD framework */
67 struct ehci_caps __iomem *caps; 73 struct ehci_caps __iomem *caps;
@@ -70,6 +76,7 @@ struct ehci_hcd { /* one per controller */
70 76
71 __u32 hcs_params; /* cached register copy */ 77 __u32 hcs_params; /* cached register copy */
72 spinlock_t lock; 78 spinlock_t lock;
79 enum ehci_rh_state rh_state;
73 80
74 /* async schedule support */ 81 /* async schedule support */
75 struct ehci_qh *async; 82 struct ehci_qh *async;
@@ -139,6 +146,7 @@ struct ehci_hcd { /* one per controller */
139 unsigned fs_i_thresh:1; /* Intel iso scheduling */ 146 unsigned fs_i_thresh:1; /* Intel iso scheduling */
140 unsigned use_dummy_qh:1; /* AMD Frame List table quirk*/ 147 unsigned use_dummy_qh:1; /* AMD Frame List table quirk*/
141 unsigned has_synopsys_hc_bug:1; /* Synopsys HC */ 148 unsigned has_synopsys_hc_bug:1; /* Synopsys HC */
149 unsigned frame_index_bug:1; /* MosChip (AKA NetMos) */
142 150
143 /* required for usb32 quirk */ 151 /* required for usb32 quirk */
144 #define OHCI_CTRL_HCFS (3 << 6) 152 #define OHCI_CTRL_HCFS (3 << 6)
@@ -740,6 +748,22 @@ static inline u32 hc32_to_cpup (const struct ehci_hcd *ehci, const __hc32 *x)
740 748
741/*-------------------------------------------------------------------------*/ 749/*-------------------------------------------------------------------------*/
742 750
751#ifdef CONFIG_PCI
752
753/* For working around the MosChip frame-index-register bug */
754static unsigned ehci_read_frame_index(struct ehci_hcd *ehci);
755
756#else
757
758static inline unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
759{
760 return ehci_readl(ehci, &ehci->regs->frame_index);
761}
762
763#endif
764
765/*-------------------------------------------------------------------------*/
766
743#ifndef DEBUG 767#ifndef DEBUG
744#define STUB_DEBUG_FILES 768#define STUB_DEBUG_FILES
745#endif /* DEBUG */ 769#endif /* DEBUG */
diff --git a/drivers/usb/host/fhci-hcd.c b/drivers/usb/host/fhci-hcd.c
index 572ea53b0226..4ed6d19f2a54 100644
--- a/drivers/usb/host/fhci-hcd.c
+++ b/drivers/usb/host/fhci-hcd.c
@@ -621,12 +621,15 @@ static int __devinit of_fhci_probe(struct platform_device *ofdev)
621 goto err_pram; 621 goto err_pram;
622 } 622 }
623 623
624 pram_addr = cpm_muram_alloc_fixed(iprop[2], FHCI_PRAM_SIZE); 624 pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
625 if (IS_ERR_VALUE(pram_addr)) { 625 if (IS_ERR_VALUE(pram_addr)) {
626 dev_err(dev, "failed to allocate usb pram\n"); 626 dev_err(dev, "failed to allocate usb pram\n");
627 ret = -ENOMEM; 627 ret = -ENOMEM;
628 goto err_pram; 628 goto err_pram;
629 } 629 }
630
631 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
632 QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
630 fhci->pram = cpm_muram_addr(pram_addr); 633 fhci->pram = cpm_muram_addr(pram_addr);
631 634
632 /* GPIOs and pins */ 635 /* GPIOs and pins */
@@ -686,7 +689,7 @@ static int __devinit of_fhci_probe(struct platform_device *ofdev)
686 } 689 }
687 690
688 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq, 691 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
689 IRQF_DISABLED, "qe timer (usb)", hcd); 692 0, "qe timer (usb)", hcd);
690 if (ret) { 693 if (ret) {
691 dev_err(dev, "failed to request timer irq"); 694 dev_err(dev, "failed to request timer irq");
692 goto err_timer_irq; 695 goto err_timer_irq;
@@ -745,7 +748,7 @@ static int __devinit of_fhci_probe(struct platform_device *ofdev)
745 out_be16(&fhci->regs->usb_event, 0xffff); 748 out_be16(&fhci->regs->usb_event, 0xffff);
746 out_be16(&fhci->regs->usb_mask, 0); 749 out_be16(&fhci->regs->usb_mask, 0);
747 750
748 ret = usb_add_hcd(hcd, usb_irq, IRQF_DISABLED); 751 ret = usb_add_hcd(hcd, usb_irq, 0);
749 if (ret < 0) 752 if (ret < 0)
750 goto err_add_hcd; 753 goto err_add_hcd;
751 754
diff --git a/drivers/usb/host/fhci-sched.c b/drivers/usb/host/fhci-sched.c
index a42ef380e917..2df851b4bc7c 100644
--- a/drivers/usb/host/fhci-sched.c
+++ b/drivers/usb/host/fhci-sched.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Freescale QUICC Engine USB Host Controller Driver 2 * Freescale QUICC Engine USB Host Controller Driver
3 * 3 *
4 * Copyright (c) Freescale Semicondutor, Inc. 2006. 4 * Copyright (c) Freescale Semicondutor, Inc. 2006, 2011.
5 * Shlomi Gridish <gridish@freescale.com> 5 * Shlomi Gridish <gridish@freescale.com>
6 * Jerry Huang <Chang-Ming.Huang@freescale.com> 6 * Jerry Huang <Chang-Ming.Huang@freescale.com>
7 * Copyright (c) Logic Product Development, Inc. 2007 7 * Copyright (c) Logic Product Development, Inc. 2007
@@ -810,9 +810,11 @@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
810 ed->dev_addr = usb_pipedevice(urb->pipe); 810 ed->dev_addr = usb_pipedevice(urb->pipe);
811 ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe, 811 ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
812 usb_pipeout(urb->pipe)); 812 usb_pipeout(urb->pipe));
813 /* setup stage */
813 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP, 814 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
814 USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true); 815 USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
815 816
817 /* data stage */
816 if (data_len > 0) { 818 if (data_len > 0) {
817 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, 819 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
818 usb_pipeout(urb->pipe) ? FHCI_TA_OUT : 820 usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
@@ -820,9 +822,18 @@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
820 USB_TD_TOGGLE_DATA1, data, data_len, 0, 0, 822 USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
821 true); 823 true);
822 } 824 }
823 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, 825
824 usb_pipeout(urb->pipe) ? FHCI_TA_IN : FHCI_TA_OUT, 826 /* status stage */
825 USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true); 827 if (data_len > 0)
828 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
829 (usb_pipeout(urb->pipe) ? FHCI_TA_IN :
830 FHCI_TA_OUT),
831 USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
832 else
833 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
834 FHCI_TA_IN,
835 USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
836
826 urb_state = US_CTRL_SETUP; 837 urb_state = US_CTRL_SETUP;
827 break; 838 break;
828 case FHCI_TF_ISO: 839 case FHCI_TF_ISO:
diff --git a/drivers/usb/host/fsl-mph-dr-of.c b/drivers/usb/host/fsl-mph-dr-of.c
index 79a66d622f9c..9037035ad1e4 100644
--- a/drivers/usb/host/fsl-mph-dr-of.c
+++ b/drivers/usb/host/fsl-mph-dr-of.c
@@ -16,6 +16,7 @@
16#include <linux/io.h> 16#include <linux/io.h>
17#include <linux/of_platform.h> 17#include <linux/of_platform.h>
18#include <linux/clk.h> 18#include <linux/clk.h>
19#include <linux/module.h>
19 20
20struct fsl_usb2_dev_data { 21struct fsl_usb2_dev_data {
21 char *dr_mode; /* controller mode */ 22 char *dr_mode; /* controller mode */
diff --git a/drivers/usb/host/imx21-hcd.c b/drivers/usb/host/imx21-hcd.c
index af05718bdc73..2ee18cfa1efe 100644
--- a/drivers/usb/host/imx21-hcd.c
+++ b/drivers/usb/host/imx21-hcd.c
@@ -1891,7 +1891,7 @@ static int imx21_probe(struct platform_device *pdev)
1891 dev_info(imx21->dev, "Hardware HC revision: 0x%02X\n", 1891 dev_info(imx21->dev, "Hardware HC revision: 0x%02X\n",
1892 (readl(imx21->regs + USBOTG_HWMODE) >> 16) & 0xFF); 1892 (readl(imx21->regs + USBOTG_HWMODE) >> 16) & 0xFF);
1893 1893
1894 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 1894 ret = usb_add_hcd(hcd, irq, 0);
1895 if (ret != 0) { 1895 if (ret != 0) {
1896 dev_err(imx21->dev, "usb_add_hcd() returned %d\n", ret); 1896 dev_err(imx21->dev, "usb_add_hcd() returned %d\n", ret);
1897 goto failed_add_hcd; 1897 goto failed_add_hcd;
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index baae4ccd16ac..d91e5f211a76 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -1639,7 +1639,7 @@ static int __devinit isp116x_probe(struct platform_device *pdev)
1639 goto err6; 1639 goto err6;
1640 } 1640 }
1641 1641
1642 ret = usb_add_hcd(hcd, irq, irqflags | IRQF_DISABLED); 1642 ret = usb_add_hcd(hcd, irq, irqflags);
1643 if (ret) 1643 if (ret)
1644 goto err6; 1644 goto err6;
1645 1645
diff --git a/drivers/usb/host/isp1362-hcd.c b/drivers/usb/host/isp1362-hcd.c
index 9c37dad3e816..e5fd8aa57af1 100644
--- a/drivers/usb/host/isp1362-hcd.c
+++ b/drivers/usb/host/isp1362-hcd.c
@@ -2358,7 +2358,7 @@ static int isp1362_hc_reset(struct usb_hcd *hcd)
2358 unsigned long flags; 2358 unsigned long flags;
2359 int clkrdy = 0; 2359 int clkrdy = 0;
2360 2360
2361 pr_info("%s:\n", __func__); 2361 pr_debug("%s:\n", __func__);
2362 2362
2363 if (isp1362_hcd->board && isp1362_hcd->board->reset) { 2363 if (isp1362_hcd->board && isp1362_hcd->board->reset) {
2364 isp1362_hcd->board->reset(hcd->self.controller, 1); 2364 isp1362_hcd->board->reset(hcd->self.controller, 1);
@@ -2395,7 +2395,7 @@ static void isp1362_hc_stop(struct usb_hcd *hcd)
2395 unsigned long flags; 2395 unsigned long flags;
2396 u32 tmp; 2396 u32 tmp;
2397 2397
2398 pr_info("%s:\n", __func__); 2398 pr_debug("%s:\n", __func__);
2399 2399
2400 del_timer_sync(&hcd->rh_timer); 2400 del_timer_sync(&hcd->rh_timer);
2401 2401
@@ -2523,7 +2523,7 @@ static int isp1362_hc_start(struct usb_hcd *hcd)
2523 u16 chipid; 2523 u16 chipid;
2524 unsigned long flags; 2524 unsigned long flags;
2525 2525
2526 pr_info("%s:\n", __func__); 2526 pr_debug("%s:\n", __func__);
2527 2527
2528 spin_lock_irqsave(&isp1362_hcd->lock, flags); 2528 spin_lock_irqsave(&isp1362_hcd->lock, flags);
2529 chipid = isp1362_read_reg16(isp1362_hcd, HCCHIPID); 2529 chipid = isp1362_read_reg16(isp1362_hcd, HCCHIPID);
@@ -2773,7 +2773,7 @@ static int __devinit isp1362_probe(struct platform_device *pdev)
2773 if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL) 2773 if (irq_res->flags & IORESOURCE_IRQ_LOWLEVEL)
2774 irq_flags |= IRQF_TRIGGER_LOW; 2774 irq_flags |= IRQF_TRIGGER_LOW;
2775 2775
2776 retval = usb_add_hcd(hcd, irq, irq_flags | IRQF_DISABLED | IRQF_SHARED); 2776 retval = usb_add_hcd(hcd, irq, irq_flags | IRQF_SHARED);
2777 if (retval != 0) 2777 if (retval != 0)
2778 goto err6; 2778 goto err6;
2779 pr_info("%s, irq %d\n", hcd->product_desc, irq); 2779 pr_info("%s, irq %d\n", hcd->product_desc, irq);
diff --git a/drivers/usb/host/isp1760-hcd.c b/drivers/usb/host/isp1760-hcd.c
index 840beda66dd9..27dfab80ed8f 100644
--- a/drivers/usb/host/isp1760-hcd.c
+++ b/drivers/usb/host/isp1760-hcd.c
@@ -21,8 +21,10 @@
21#include <linux/uaccess.h> 21#include <linux/uaccess.h>
22#include <linux/io.h> 22#include <linux/io.h>
23#include <linux/mm.h> 23#include <linux/mm.h>
24#include <linux/timer.h>
24#include <asm/unaligned.h> 25#include <asm/unaligned.h>
25#include <asm/cacheflush.h> 26#include <asm/cacheflush.h>
27#include <linux/gpio.h>
26 28
27#include "isp1760-hcd.h" 29#include "isp1760-hcd.h"
28 30
@@ -39,7 +41,6 @@ struct isp1760_hcd {
39 int int_done_map; 41 int int_done_map;
40 struct memory_chunk memory_pool[BLOCKS]; 42 struct memory_chunk memory_pool[BLOCKS];
41 struct list_head controlqhs, bulkqhs, interruptqhs; 43 struct list_head controlqhs, bulkqhs, interruptqhs;
42 int active_ptds;
43 44
44 /* periodic schedule support */ 45 /* periodic schedule support */
45#define DEFAULT_I_TDPS 1024 46#define DEFAULT_I_TDPS 1024
@@ -48,6 +49,8 @@ struct isp1760_hcd {
48 unsigned long reset_done; 49 unsigned long reset_done;
49 unsigned long next_statechange; 50 unsigned long next_statechange;
50 unsigned int devflags; 51 unsigned int devflags;
52
53 int rst_gpio;
51}; 54};
52 55
53static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd) 56static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
@@ -114,6 +117,7 @@ struct isp1760_qh {
114 u32 toggle; 117 u32 toggle;
115 u32 ping; 118 u32 ping;
116 int slot; 119 int slot;
120 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
117}; 121};
118 122
119struct urb_listitem { 123struct urb_listitem {
@@ -432,6 +436,18 @@ static int isp1760_hc_setup(struct usb_hcd *hcd)
432 int result; 436 int result;
433 u32 scratch, hwmode; 437 u32 scratch, hwmode;
434 438
439 /* low-level chip reset */
440 if (gpio_is_valid(priv->rst_gpio)) {
441 unsigned int rst_lvl;
442
443 rst_lvl = (priv->devflags &
444 ISP1760_FLAG_RESET_ACTIVE_HIGH) ? 1 : 0;
445
446 gpio_set_value(priv->rst_gpio, rst_lvl);
447 mdelay(50);
448 gpio_set_value(priv->rst_gpio, !rst_lvl);
449 }
450
435 /* Setup HW Mode Control: This assumes a level active-low interrupt */ 451 /* Setup HW Mode Control: This assumes a level active-low interrupt */
436 hwmode = HW_DATA_BUS_32BIT; 452 hwmode = HW_DATA_BUS_32BIT;
437 453
@@ -489,10 +505,6 @@ static int isp1760_hc_setup(struct usb_hcd *hcd)
489 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ? 505 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
490 "analog" : "digital"); 506 "analog" : "digital");
491 507
492 /* This is weird: at the first plug-in of a device there seems to be
493 one packet queued that never gets returned? */
494 priv->active_ptds = -1;
495
496 /* ATL reset */ 508 /* ATL reset */
497 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET); 509 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
498 mdelay(10); 510 mdelay(10);
@@ -514,83 +526,6 @@ static int isp1760_hc_setup(struct usb_hcd *hcd)
514 return priv_init(hcd); 526 return priv_init(hcd);
515} 527}
516 528
517static void isp1760_init_maps(struct usb_hcd *hcd)
518{
519 /*set last maps, for iso its only 1, else 32 tds bitmap*/
520 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
521 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
522 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
523
524 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
525 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
526 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
527
528 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
529 ATL_BUF_FILL | INT_BUF_FILL);
530}
531
532static void isp1760_enable_interrupts(struct usb_hcd *hcd)
533{
534 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
535 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
536 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
537 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
538 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
539 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
540 /* step 23 passed */
541}
542
543static int isp1760_run(struct usb_hcd *hcd)
544{
545 int retval;
546 u32 temp;
547 u32 command;
548 u32 chipid;
549
550 hcd->uses_new_polling = 1;
551
552 hcd->state = HC_STATE_RUNNING;
553 isp1760_enable_interrupts(hcd);
554 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
555 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
556
557 command = reg_read32(hcd->regs, HC_USBCMD);
558 command &= ~(CMD_LRESET|CMD_RESET);
559 command |= CMD_RUN;
560 reg_write32(hcd->regs, HC_USBCMD, command);
561
562 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
563 if (retval)
564 return retval;
565
566 /*
567 * XXX
568 * Spec says to write FLAG_CF as last config action, priv code grabs
569 * the semaphore while doing so.
570 */
571 down_write(&ehci_cf_port_reset_rwsem);
572 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
573
574 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
575 up_write(&ehci_cf_port_reset_rwsem);
576 if (retval)
577 return retval;
578
579 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
580 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
581 chipid & 0xffff, chipid >> 16);
582
583 /* PTD Register Init Part 2, Step 28 */
584 /* enable INTs */
585 isp1760_init_maps(hcd);
586
587 /* GRR this is run-once init(), being done every time the HC starts.
588 * So long as they're part of class devices, we can't do it init()
589 * since the class device isn't created that early.
590 */
591 return 0;
592}
593
594static u32 base_to_chip(u32 base) 529static u32 base_to_chip(u32 base)
595{ 530{
596 return ((base - 0x400) >> 3); 531 return ((base - 0x400) >> 3);
@@ -813,28 +748,29 @@ static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
813 WARN_ON(slots[slot].qh); 748 WARN_ON(slots[slot].qh);
814 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC); 749 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
815 750
816 slots[slot].qtd = qtd;
817 slots[slot].qh = qh;
818 qh->slot = slot;
819 qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
820 interrupt routine may preempt and expects this value. */
821 ptd_write(hcd->regs, ptd_offset, slot, ptd);
822 priv->active_ptds++;
823
824 /* Make sure done map has not triggered from some unlinked transfer */ 751 /* Make sure done map has not triggered from some unlinked transfer */
825 if (ptd_offset == ATL_PTD_OFFSET) { 752 if (ptd_offset == ATL_PTD_OFFSET) {
826 priv->atl_done_map |= reg_read32(hcd->regs, 753 priv->atl_done_map |= reg_read32(hcd->regs,
827 HC_ATL_PTD_DONEMAP_REG); 754 HC_ATL_PTD_DONEMAP_REG);
828 priv->atl_done_map &= ~(1 << qh->slot); 755 priv->atl_done_map &= ~(1 << slot);
756 } else {
757 priv->int_done_map |= reg_read32(hcd->regs,
758 HC_INT_PTD_DONEMAP_REG);
759 priv->int_done_map &= ~(1 << slot);
760 }
761
762 qh->slot = slot;
763 qtd->status = QTD_XFER_STARTED;
764 slots[slot].timestamp = jiffies;
765 slots[slot].qtd = qtd;
766 slots[slot].qh = qh;
767 ptd_write(hcd->regs, ptd_offset, slot, ptd);
829 768
769 if (ptd_offset == ATL_PTD_OFFSET) {
830 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG); 770 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
831 skip_map &= ~(1 << qh->slot); 771 skip_map &= ~(1 << qh->slot);
832 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map); 772 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
833 } else { 773 } else {
834 priv->int_done_map |= reg_read32(hcd->regs,
835 HC_INT_PTD_DONEMAP_REG);
836 priv->int_done_map &= ~(1 << qh->slot);
837
838 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG); 774 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
839 skip_map &= ~(1 << qh->slot); 775 skip_map &= ~(1 << qh->slot);
840 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map); 776 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
@@ -858,10 +794,7 @@ static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
858 if (qtd->status < QTD_XFER_COMPLETE) 794 if (qtd->status < QTD_XFER_COMPLETE)
859 break; 795 break;
860 796
861 if (list_is_last(&qtd->qtd_list, &qh->qtd_list)) 797 last_qtd = last_qtd_of_urb(qtd, qh);
862 last_qtd = 1;
863 else
864 last_qtd = qtd->urb != qtd_next->urb;
865 798
866 if ((!last_qtd) && (qtd->status == QTD_RETIRE)) 799 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
867 qtd_next->status = QTD_RETIRE; 800 qtd_next->status = QTD_RETIRE;
@@ -902,7 +835,7 @@ static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
902 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep, 835 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
903 GFP_ATOMIC); 836 GFP_ATOMIC);
904 if (unlikely(!urb_listitem)) 837 if (unlikely(!urb_listitem))
905 break; 838 break; /* Try again on next call */
906 urb_listitem->urb = qtd->urb; 839 urb_listitem->urb = qtd->urb;
907 list_add_tail(&urb_listitem->urb_list, urb_list); 840 list_add_tail(&urb_listitem->urb_list, urb_list);
908 } 841 }
@@ -928,6 +861,10 @@ static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
928 return; 861 return;
929 } 862 }
930 863
864 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
865 if (qh->tt_buffer_dirty)
866 return;
867
931 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd, 868 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
932 qtd_list)->urb->pipe)) { 869 qtd_list)->urb->pipe)) {
933 ptd_offset = INT_PTD_OFFSET; 870 ptd_offset = INT_PTD_OFFSET;
@@ -1168,11 +1105,9 @@ static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1168 return PTD_STATE_QTD_DONE; 1105 return PTD_STATE_QTD_DONE;
1169} 1106}
1170 1107
1171static irqreturn_t isp1760_irq(struct usb_hcd *hcd) 1108static void handle_done_ptds(struct usb_hcd *hcd)
1172{ 1109{
1173 struct isp1760_hcd *priv = hcd_to_priv(hcd); 1110 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1174 u32 imask;
1175 irqreturn_t irqret = IRQ_NONE;
1176 struct ptd ptd; 1111 struct ptd ptd;
1177 struct isp1760_qh *qh; 1112 struct isp1760_qh *qh;
1178 int slot; 1113 int slot;
@@ -1181,27 +1116,14 @@ static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1181 u32 ptd_offset; 1116 u32 ptd_offset;
1182 struct isp1760_qtd *qtd; 1117 struct isp1760_qtd *qtd;
1183 int modified; 1118 int modified;
1184 static int last_active_ptds; 1119 int skip_map;
1185 int int_skip_map, atl_skip_map;
1186
1187 spin_lock(&priv->lock);
1188
1189 if (!(hcd->state & HC_STATE_RUNNING))
1190 goto leave;
1191
1192 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1193 if (unlikely(!imask))
1194 goto leave;
1195 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1196 1120
1197 int_skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG); 1121 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1198 atl_skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG); 1122 priv->int_done_map &= ~skip_map;
1199 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG); 1123 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1200 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG); 1124 priv->atl_done_map &= ~skip_map;
1201 priv->int_done_map &= ~int_skip_map;
1202 priv->atl_done_map &= ~atl_skip_map;
1203 1125
1204 modified = priv->int_done_map | priv->atl_done_map; 1126 modified = priv->int_done_map || priv->atl_done_map;
1205 1127
1206 while (priv->int_done_map || priv->atl_done_map) { 1128 while (priv->int_done_map || priv->atl_done_map) {
1207 if (priv->int_done_map) { 1129 if (priv->int_done_map) {
@@ -1240,7 +1162,6 @@ static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1240 slots[slot].qtd = NULL; 1162 slots[slot].qtd = NULL;
1241 qh = slots[slot].qh; 1163 qh = slots[slot].qh;
1242 slots[slot].qh = NULL; 1164 slots[slot].qh = NULL;
1243 priv->active_ptds--;
1244 qh->slot = -1; 1165 qh->slot = -1;
1245 1166
1246 WARN_ON(qtd->status != QTD_XFER_STARTED); 1167 WARN_ON(qtd->status != QTD_XFER_STARTED);
@@ -1281,6 +1202,15 @@ static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1281 1202
1282 case PTD_STATE_URB_RETIRE: 1203 case PTD_STATE_URB_RETIRE:
1283 qtd->status = QTD_RETIRE; 1204 qtd->status = QTD_RETIRE;
1205 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1206 (qtd->urb->status != -EPIPE) &&
1207 (qtd->urb->status != -EREMOTEIO)) {
1208 qh->tt_buffer_dirty = 1;
1209 if (usb_hub_clear_tt_buffer(qtd->urb))
1210 /* Clear failed; let's hope things work
1211 anyway */
1212 qh->tt_buffer_dirty = 0;
1213 }
1284 qtd = NULL; 1214 qtd = NULL;
1285 qh->toggle = 0; 1215 qh->toggle = 0;
1286 qh->ping = 0; 1216 qh->ping = 0;
@@ -1311,22 +1241,28 @@ static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1311 1241
1312 if (modified) 1242 if (modified)
1313 schedule_ptds(hcd); 1243 schedule_ptds(hcd);
1244}
1314 1245
1315 /* ISP1760 Errata 2 explains that interrupts may be missed (or not 1246static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1316 happen?) if two USB devices are running simultaneously. Perhaps 1247{
1317 this happens when a PTD is finished during interrupt handling; 1248 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1318 enable SOF interrupts if PTDs are still scheduled when exiting this 1249 u32 imask;
1319 interrupt handler, just to be safe. */ 1250 irqreturn_t irqret = IRQ_NONE;
1320 1251
1321 if (priv->active_ptds != last_active_ptds) { 1252 spin_lock(&priv->lock);
1322 if (priv->active_ptds > 0) 1253
1323 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, 1254 if (!(hcd->state & HC_STATE_RUNNING))
1324 INTERRUPT_ENABLE_SOT_MASK); 1255 goto leave;
1325 else 1256
1326 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, 1257 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1327 INTERRUPT_ENABLE_MASK); 1258 if (unlikely(!imask))
1328 last_active_ptds = priv->active_ptds; 1259 goto leave;
1329 } 1260 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1261
1262 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1263 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1264
1265 handle_done_ptds(hcd);
1330 1266
1331 irqret = IRQ_HANDLED; 1267 irqret = IRQ_HANDLED;
1332leave: 1268leave:
@@ -1335,6 +1271,138 @@ leave:
1335 return irqret; 1271 return irqret;
1336} 1272}
1337 1273
1274/*
1275 * Workaround for problem described in chip errata 2:
1276 *
1277 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1278 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1279 * ATL done interrupts (the "instead of" might be important since it seems
1280 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1281 * to set the PTD's done bit in addition to not generating an interrupt!).
1282 *
1283 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1284 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1285 *
1286 * If we use SOF interrupts only, we get latency between ptd completion and the
1287 * actual handling. This is very noticeable in testusb runs which takes several
1288 * minutes longer without ATL interrupts.
1289 *
1290 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1291 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1292 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1293 * completed and its done map bit is set.
1294 *
1295 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1296 * not to cause too much lag when this HW bug occurs, while still hopefully
1297 * ensuring that the check does not falsely trigger.
1298 */
1299#define SLOT_TIMEOUT 300
1300#define SLOT_CHECK_PERIOD 200
1301static struct timer_list errata2_timer;
1302
1303void errata2_function(unsigned long data)
1304{
1305 struct usb_hcd *hcd = (struct usb_hcd *) data;
1306 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1307 int slot;
1308 struct ptd ptd;
1309 unsigned long spinflags;
1310
1311 spin_lock_irqsave(&priv->lock, spinflags);
1312
1313 for (slot = 0; slot < 32; slot++)
1314 if (priv->atl_slots[slot].qh && time_after(jiffies,
1315 priv->atl_slots[slot].timestamp +
1316 SLOT_TIMEOUT * HZ / 1000)) {
1317 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1318 if (!FROM_DW0_VALID(ptd.dw0) &&
1319 !FROM_DW3_ACTIVE(ptd.dw3))
1320 priv->atl_done_map |= 1 << slot;
1321 }
1322
1323 if (priv->atl_done_map)
1324 handle_done_ptds(hcd);
1325
1326 spin_unlock_irqrestore(&priv->lock, spinflags);
1327
1328 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1329 add_timer(&errata2_timer);
1330}
1331
1332static int isp1760_run(struct usb_hcd *hcd)
1333{
1334 int retval;
1335 u32 temp;
1336 u32 command;
1337 u32 chipid;
1338
1339 hcd->uses_new_polling = 1;
1340
1341 hcd->state = HC_STATE_RUNNING;
1342
1343 /* Set PTD interrupt AND & OR maps */
1344 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1345 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1346 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1347 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1348 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1349 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1350 /* step 23 passed */
1351
1352 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1353 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1354
1355 command = reg_read32(hcd->regs, HC_USBCMD);
1356 command &= ~(CMD_LRESET|CMD_RESET);
1357 command |= CMD_RUN;
1358 reg_write32(hcd->regs, HC_USBCMD, command);
1359
1360 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1361 if (retval)
1362 return retval;
1363
1364 /*
1365 * XXX
1366 * Spec says to write FLAG_CF as last config action, priv code grabs
1367 * the semaphore while doing so.
1368 */
1369 down_write(&ehci_cf_port_reset_rwsem);
1370 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1371
1372 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1373 up_write(&ehci_cf_port_reset_rwsem);
1374 if (retval)
1375 return retval;
1376
1377 init_timer(&errata2_timer);
1378 errata2_timer.function = errata2_function;
1379 errata2_timer.data = (unsigned long) hcd;
1380 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1381 add_timer(&errata2_timer);
1382
1383 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1384 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1385 chipid & 0xffff, chipid >> 16);
1386
1387 /* PTD Register Init Part 2, Step 28 */
1388
1389 /* Setup registers controlling PTD checking */
1390 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1391 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1392 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1393 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1394 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1395 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1396 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1397 ATL_BUF_FILL | INT_BUF_FILL);
1398
1399 /* GRR this is run-once init(), being done every time the HC starts.
1400 * So long as they're part of class devices, we can't do it init()
1401 * since the class device isn't created that early.
1402 */
1403 return 0;
1404}
1405
1338static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len) 1406static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
1339{ 1407{
1340 qtd->data_buffer = databuffer; 1408 qtd->data_buffer = databuffer;
@@ -1503,7 +1571,6 @@ static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1503 packetize_urb(hcd, urb, &new_qtds, mem_flags); 1571 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1504 if (list_empty(&new_qtds)) 1572 if (list_empty(&new_qtds))
1505 return -ENOMEM; 1573 return -ENOMEM;
1506 urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
1507 1574
1508 retval = 0; 1575 retval = 0;
1509 spin_lock_irqsave(&priv->lock, spinflags); 1576 spin_lock_irqsave(&priv->lock, spinflags);
@@ -1531,6 +1598,7 @@ static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1531 qh = qh_alloc(GFP_ATOMIC); 1598 qh = qh_alloc(GFP_ATOMIC);
1532 if (!qh) { 1599 if (!qh) {
1533 retval = -ENOMEM; 1600 retval = -ENOMEM;
1601 usb_hcd_unlink_urb_from_ep(hcd, urb);
1534 goto out; 1602 goto out;
1535 } 1603 }
1536 list_add_tail(&qh->qh_list, ep_queue); 1604 list_add_tail(&qh->qh_list, ep_queue);
@@ -1570,7 +1638,41 @@ static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1570 } 1638 }
1571 1639
1572 qh->slot = -1; 1640 qh->slot = -1;
1573 priv->active_ptds--; 1641}
1642
1643/*
1644 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1645 * any active transfer belonging to the urb in the process.
1646 */
1647static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1648 struct isp1760_qtd *qtd)
1649{
1650 struct urb *urb;
1651 int urb_was_running;
1652
1653 urb = qtd->urb;
1654 urb_was_running = 0;
1655 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
1656 if (qtd->urb != urb)
1657 break;
1658
1659 if (qtd->status >= QTD_XFER_STARTED)
1660 urb_was_running = 1;
1661 if (last_qtd_of_urb(qtd, qh) &&
1662 (qtd->status >= QTD_XFER_COMPLETE))
1663 urb_was_running = 0;
1664
1665 if (qtd->status == QTD_XFER_STARTED)
1666 kill_transfer(hcd, urb, qh);
1667 qtd->status = QTD_RETIRE;
1668 }
1669
1670 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
1671 qh->tt_buffer_dirty = 1;
1672 if (usb_hub_clear_tt_buffer(urb))
1673 /* Clear failed; let's hope things work anyway */
1674 qh->tt_buffer_dirty = 0;
1675 }
1574} 1676}
1575 1677
1576static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, 1678static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
@@ -1595,9 +1697,8 @@ static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1595 1697
1596 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) 1698 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1597 if (qtd->urb == urb) { 1699 if (qtd->urb == urb) {
1598 if (qtd->status == QTD_XFER_STARTED) 1700 dequeue_urb_from_qtd(hcd, qh, qtd);
1599 kill_transfer(hcd, urb, qh); 1701 break;
1600 qtd->status = QTD_RETIRE;
1601 } 1702 }
1602 1703
1603 urb->status = status; 1704 urb->status = status;
@@ -1622,12 +1723,11 @@ static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1622 if (!qh) 1723 if (!qh)
1623 goto out; 1724 goto out;
1624 1725
1625 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) { 1726 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1626 if (qtd->status == QTD_XFER_STARTED) 1727 if (qtd->status != QTD_RETIRE) {
1627 kill_transfer(hcd, qtd->urb, qh); 1728 dequeue_urb_from_qtd(hcd, qh, qtd);
1628 qtd->status = QTD_RETIRE; 1729 qtd->urb->status = -ECONNRESET;
1629 qtd->urb->status = -ECONNRESET; 1730 }
1630 }
1631 1731
1632 ep->hcpriv = NULL; 1732 ep->hcpriv = NULL;
1633 /* Cannot free qh here since it will be parsed by schedule_ptds() */ 1733 /* Cannot free qh here since it will be parsed by schedule_ptds() */
@@ -2021,6 +2121,8 @@ static void isp1760_stop(struct usb_hcd *hcd)
2021 struct isp1760_hcd *priv = hcd_to_priv(hcd); 2121 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2022 u32 temp; 2122 u32 temp;
2023 2123
2124 del_timer(&errata2_timer);
2125
2024 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1, 2126 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2025 NULL, 0); 2127 NULL, 0);
2026 mdelay(20); 2128 mdelay(20);
@@ -2048,6 +2150,23 @@ static void isp1760_shutdown(struct usb_hcd *hcd)
2048 reg_write32(hcd->regs, HC_USBCMD, command); 2150 reg_write32(hcd->regs, HC_USBCMD, command);
2049} 2151}
2050 2152
2153static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2154 struct usb_host_endpoint *ep)
2155{
2156 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2157 struct isp1760_qh *qh = ep->hcpriv;
2158 unsigned long spinflags;
2159
2160 if (!qh)
2161 return;
2162
2163 spin_lock_irqsave(&priv->lock, spinflags);
2164 qh->tt_buffer_dirty = 0;
2165 schedule_ptds(hcd);
2166 spin_unlock_irqrestore(&priv->lock, spinflags);
2167}
2168
2169
2051static const struct hc_driver isp1760_hc_driver = { 2170static const struct hc_driver isp1760_hc_driver = {
2052 .description = "isp1760-hcd", 2171 .description = "isp1760-hcd",
2053 .product_desc = "NXP ISP1760 USB Host Controller", 2172 .product_desc = "NXP ISP1760 USB Host Controller",
@@ -2064,6 +2183,7 @@ static const struct hc_driver isp1760_hc_driver = {
2064 .get_frame_number = isp1760_get_frame, 2183 .get_frame_number = isp1760_get_frame,
2065 .hub_status_data = isp1760_hub_status_data, 2184 .hub_status_data = isp1760_hub_status_data,
2066 .hub_control = isp1760_hub_control, 2185 .hub_control = isp1760_hub_control,
2186 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
2067}; 2187};
2068 2188
2069int __init init_kmem_once(void) 2189int __init init_kmem_once(void)
@@ -2102,6 +2222,7 @@ void deinit_kmem_cache(void)
2102 2222
2103struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len, 2223struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2104 int irq, unsigned long irqflags, 2224 int irq, unsigned long irqflags,
2225 int rst_gpio,
2105 struct device *dev, const char *busname, 2226 struct device *dev, const char *busname,
2106 unsigned int devflags) 2227 unsigned int devflags)
2107{ 2228{
@@ -2121,6 +2242,7 @@ struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2121 2242
2122 priv = hcd_to_priv(hcd); 2243 priv = hcd_to_priv(hcd);
2123 priv->devflags = devflags; 2244 priv->devflags = devflags;
2245 priv->rst_gpio = rst_gpio;
2124 init_memory(priv); 2246 init_memory(priv);
2125 hcd->regs = ioremap(res_start, res_len); 2247 hcd->regs = ioremap(res_start, res_len);
2126 if (!hcd->regs) { 2248 if (!hcd->regs) {
diff --git a/drivers/usb/host/isp1760-hcd.h b/drivers/usb/host/isp1760-hcd.h
index 014a7dfadf91..33dc79ccaa6b 100644
--- a/drivers/usb/host/isp1760-hcd.h
+++ b/drivers/usb/host/isp1760-hcd.h
@@ -4,6 +4,7 @@
4/* exports for if */ 4/* exports for if */
5struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len, 5struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
6 int irq, unsigned long irqflags, 6 int irq, unsigned long irqflags,
7 int rst_gpio,
7 struct device *dev, const char *busname, 8 struct device *dev, const char *busname,
8 unsigned int devflags); 9 unsigned int devflags);
9int init_kmem_once(void); 10int init_kmem_once(void);
@@ -73,7 +74,6 @@ void deinit_kmem_cache(void);
73#define HC_EOT_INT (1 << 3) 74#define HC_EOT_INT (1 << 3)
74#define HC_SOT_INT (1 << 1) 75#define HC_SOT_INT (1 << 1)
75#define INTERRUPT_ENABLE_MASK (HC_INTL_INT | HC_ATL_INT) 76#define INTERRUPT_ENABLE_MASK (HC_INTL_INT | HC_ATL_INT)
76#define INTERRUPT_ENABLE_SOT_MASK (HC_SOT_INT)
77 77
78#define HC_ISO_IRQ_MASK_OR_REG 0x318 78#define HC_ISO_IRQ_MASK_OR_REG 0x318
79#define HC_INT_IRQ_MASK_OR_REG 0x31C 79#define HC_INT_IRQ_MASK_OR_REG 0x31C
@@ -107,6 +107,7 @@ struct ptd {
107struct slotinfo { 107struct slotinfo {
108 struct isp1760_qh *qh; 108 struct isp1760_qh *qh;
109 struct isp1760_qtd *qtd; 109 struct isp1760_qtd *qtd;
110 unsigned long timestamp;
110}; 111};
111 112
112 113
@@ -126,6 +127,7 @@ typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
126#define ISP1760_FLAG_ISP1761 0x00000040 /* Chip is ISP1761 */ 127#define ISP1760_FLAG_ISP1761 0x00000040 /* Chip is ISP1761 */
127#define ISP1760_FLAG_INTR_POL_HIGH 0x00000080 /* Interrupt polarity active high */ 128#define ISP1760_FLAG_INTR_POL_HIGH 0x00000080 /* Interrupt polarity active high */
128#define ISP1760_FLAG_INTR_EDGE_TRIG 0x00000100 /* Interrupt edge triggered */ 129#define ISP1760_FLAG_INTR_EDGE_TRIG 0x00000100 /* Interrupt edge triggered */
130#define ISP1760_FLAG_RESET_ACTIVE_HIGH 0x80000000 /* RESET GPIO active high */
129 131
130/* chip memory management */ 132/* chip memory management */
131struct memory_chunk { 133struct memory_chunk {
@@ -188,6 +190,7 @@ struct memory_chunk {
188#define DW3_BABBLE_BIT (1 << 29) 190#define DW3_BABBLE_BIT (1 << 29)
189#define DW3_HALT_BIT (1 << 30) 191#define DW3_HALT_BIT (1 << 30)
190#define DW3_ACTIVE_BIT (1 << 31) 192#define DW3_ACTIVE_BIT (1 << 31)
193#define FROM_DW3_ACTIVE(x) (((x) >> 31) & 0x01)
191 194
192#define INT_UNDERRUN (1 << 2) 195#define INT_UNDERRUN (1 << 2)
193#define INT_BABBLE (1 << 1) 196#define INT_BABBLE (1 << 1)
diff --git a/drivers/usb/host/isp1760-if.c b/drivers/usb/host/isp1760-if.c
index 7ee30056f373..a7dc1e1d45f2 100644
--- a/drivers/usb/host/isp1760-if.c
+++ b/drivers/usb/host/isp1760-if.c
@@ -11,25 +11,35 @@
11 11
12#include <linux/usb.h> 12#include <linux/usb.h>
13#include <linux/io.h> 13#include <linux/io.h>
14#include <linux/module.h>
14#include <linux/platform_device.h> 15#include <linux/platform_device.h>
15#include <linux/usb/isp1760.h> 16#include <linux/usb/isp1760.h>
16#include <linux/usb/hcd.h> 17#include <linux/usb/hcd.h>
17 18
18#include "isp1760-hcd.h" 19#include "isp1760-hcd.h"
19 20
20#ifdef CONFIG_PPC_OF 21#ifdef CONFIG_OF
22#include <linux/slab.h>
21#include <linux/of.h> 23#include <linux/of.h>
22#include <linux/of_platform.h> 24#include <linux/of_platform.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
27#include <linux/of_gpio.h>
23#endif 28#endif
24 29
25#ifdef CONFIG_PCI 30#ifdef CONFIG_PCI
26#include <linux/pci.h> 31#include <linux/pci.h>
27#endif 32#endif
28 33
29#ifdef CONFIG_PPC_OF 34#ifdef CONFIG_OF
35struct isp1760 {
36 struct usb_hcd *hcd;
37 int rst_gpio;
38};
39
30static int of_isp1760_probe(struct platform_device *dev) 40static int of_isp1760_probe(struct platform_device *dev)
31{ 41{
32 struct usb_hcd *hcd; 42 struct isp1760 *drvdata;
33 struct device_node *dp = dev->dev.of_node; 43 struct device_node *dp = dev->dev.of_node;
34 struct resource *res; 44 struct resource *res;
35 struct resource memory; 45 struct resource memory;
@@ -39,6 +49,11 @@ static int of_isp1760_probe(struct platform_device *dev)
39 int ret; 49 int ret;
40 const unsigned int *prop; 50 const unsigned int *prop;
41 unsigned int devflags = 0; 51 unsigned int devflags = 0;
52 enum of_gpio_flags gpio_flags;
53
54 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
55 if (!drvdata)
56 return -ENOMEM;
42 57
43 ret = of_address_to_resource(dp, 0, &memory); 58 ret = of_address_to_resource(dp, 0, &memory);
44 if (ret) 59 if (ret)
@@ -78,32 +93,57 @@ static int of_isp1760_probe(struct platform_device *dev)
78 if (of_get_property(dp, "dreq-polarity", NULL) != NULL) 93 if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
79 devflags |= ISP1760_FLAG_DREQ_POL_HIGH; 94 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
80 95
81 hcd = isp1760_register(memory.start, res_len, virq, 96 drvdata->rst_gpio = of_get_gpio_flags(dp, 0, &gpio_flags);
82 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev), 97 if (gpio_is_valid(drvdata->rst_gpio)) {
83 devflags); 98 ret = gpio_request(drvdata->rst_gpio, dev_name(&dev->dev));
84 if (IS_ERR(hcd)) { 99 if (!ret) {
85 ret = PTR_ERR(hcd); 100 if (!(gpio_flags & OF_GPIO_ACTIVE_LOW)) {
86 goto release_reg; 101 devflags |= ISP1760_FLAG_RESET_ACTIVE_HIGH;
102 gpio_direction_output(drvdata->rst_gpio, 0);
103 } else {
104 gpio_direction_output(drvdata->rst_gpio, 1);
105 }
106 } else {
107 drvdata->rst_gpio = ret;
108 }
87 } 109 }
88 110
89 dev_set_drvdata(&dev->dev, hcd); 111 drvdata->hcd = isp1760_register(memory.start, res_len, virq,
112 IRQF_SHARED, drvdata->rst_gpio,
113 &dev->dev, dev_name(&dev->dev),
114 devflags);
115 if (IS_ERR(drvdata->hcd)) {
116 ret = PTR_ERR(drvdata->hcd);
117 goto free_gpio;
118 }
119
120 dev_set_drvdata(&dev->dev, drvdata);
90 return ret; 121 return ret;
91 122
123free_gpio:
124 if (gpio_is_valid(drvdata->rst_gpio))
125 gpio_free(drvdata->rst_gpio);
92release_reg: 126release_reg:
93 release_mem_region(memory.start, res_len); 127 release_mem_region(memory.start, res_len);
128 kfree(drvdata);
94 return ret; 129 return ret;
95} 130}
96 131
97static int of_isp1760_remove(struct platform_device *dev) 132static int of_isp1760_remove(struct platform_device *dev)
98{ 133{
99 struct usb_hcd *hcd = dev_get_drvdata(&dev->dev); 134 struct isp1760 *drvdata = dev_get_drvdata(&dev->dev);
100 135
101 dev_set_drvdata(&dev->dev, NULL); 136 dev_set_drvdata(&dev->dev, NULL);
102 137
103 usb_remove_hcd(hcd); 138 usb_remove_hcd(drvdata->hcd);
104 iounmap(hcd->regs); 139 iounmap(drvdata->hcd->regs);
105 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 140 release_mem_region(drvdata->hcd->rsrc_start, drvdata->hcd->rsrc_len);
106 usb_put_hcd(hcd); 141 usb_put_hcd(drvdata->hcd);
142
143 if (gpio_is_valid(drvdata->rst_gpio))
144 gpio_free(drvdata->rst_gpio);
145
146 kfree(drvdata);
107 return 0; 147 return 0;
108} 148}
109 149
@@ -240,7 +280,7 @@ static int __devinit isp1761_pci_probe(struct pci_dev *dev,
240 280
241 dev->dev.dma_mask = NULL; 281 dev->dev.dma_mask = NULL;
242 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq, 282 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
243 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev), 283 IRQF_SHARED, -ENOENT, &dev->dev, dev_name(&dev->dev),
244 devflags); 284 devflags);
245 if (IS_ERR(hcd)) { 285 if (IS_ERR(hcd)) {
246 ret_status = -ENODEV; 286 ret_status = -ENODEV;
@@ -313,7 +353,7 @@ static int __devinit isp1760_plat_probe(struct platform_device *pdev)
313 resource_size_t mem_size; 353 resource_size_t mem_size;
314 struct isp1760_platform_data *priv = pdev->dev.platform_data; 354 struct isp1760_platform_data *priv = pdev->dev.platform_data;
315 unsigned int devflags = 0; 355 unsigned int devflags = 0;
316 unsigned long irqflags = IRQF_SHARED | IRQF_DISABLED; 356 unsigned long irqflags = IRQF_SHARED;
317 357
318 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 358 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
319 if (!mem_res) { 359 if (!mem_res) {
@@ -351,7 +391,8 @@ static int __devinit isp1760_plat_probe(struct platform_device *pdev)
351 } 391 }
352 392
353 hcd = isp1760_register(mem_res->start, mem_size, irq_res->start, 393 hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
354 irqflags, &pdev->dev, dev_name(&pdev->dev), devflags); 394 irqflags, -ENOENT,
395 &pdev->dev, dev_name(&pdev->dev), devflags);
355 if (IS_ERR(hcd)) { 396 if (IS_ERR(hcd)) {
356 pr_warning("isp1760: Failed to register the HCD device\n"); 397 pr_warning("isp1760: Failed to register the HCD device\n");
357 ret = -ENODEV; 398 ret = -ENODEV;
@@ -396,7 +437,7 @@ static int __init isp1760_init(void)
396 ret = platform_driver_register(&isp1760_plat_driver); 437 ret = platform_driver_register(&isp1760_plat_driver);
397 if (!ret) 438 if (!ret)
398 any_ret = 0; 439 any_ret = 0;
399#ifdef CONFIG_PPC_OF 440#ifdef CONFIG_OF
400 ret = platform_driver_register(&isp1760_of_driver); 441 ret = platform_driver_register(&isp1760_of_driver);
401 if (!ret) 442 if (!ret)
402 any_ret = 0; 443 any_ret = 0;
@@ -416,7 +457,7 @@ module_init(isp1760_init);
416static void __exit isp1760_exit(void) 457static void __exit isp1760_exit(void)
417{ 458{
418 platform_driver_unregister(&isp1760_plat_driver); 459 platform_driver_unregister(&isp1760_plat_driver);
419#ifdef CONFIG_PPC_OF 460#ifdef CONFIG_OF
420 platform_driver_unregister(&isp1760_of_driver); 461 platform_driver_unregister(&isp1760_of_driver);
421#endif 462#endif
422#ifdef CONFIG_PCI 463#ifdef CONFIG_PCI
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
index 944291e10f97..95a9fec38e89 100644
--- a/drivers/usb/host/ohci-at91.c
+++ b/drivers/usb/host/ohci-at91.c
@@ -35,8 +35,7 @@ extern int usb_disabled(void);
35 35
36static void at91_start_clock(void) 36static void at91_start_clock(void)
37{ 37{
38 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) 38 clk_enable(hclk);
39 clk_enable(hclk);
40 clk_enable(iclk); 39 clk_enable(iclk);
41 clk_enable(fclk); 40 clk_enable(fclk);
42 clocked = 1; 41 clocked = 1;
@@ -46,8 +45,7 @@ static void at91_stop_clock(void)
46{ 45{
47 clk_disable(fclk); 46 clk_disable(fclk);
48 clk_disable(iclk); 47 clk_disable(iclk);
49 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) 48 clk_disable(hclk);
50 clk_disable(hclk);
51 clocked = 0; 49 clocked = 0;
52} 50}
53 51
@@ -142,8 +140,7 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
142 140
143 iclk = clk_get(&pdev->dev, "ohci_clk"); 141 iclk = clk_get(&pdev->dev, "ohci_clk");
144 fclk = clk_get(&pdev->dev, "uhpck"); 142 fclk = clk_get(&pdev->dev, "uhpck");
145 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) 143 hclk = clk_get(&pdev->dev, "hclk");
146 hclk = clk_get(&pdev->dev, "hck0");
147 144
148 at91_start_hc(pdev); 145 at91_start_hc(pdev);
149 ohci_hcd_init(hcd_to_ohci(hcd)); 146 ohci_hcd_init(hcd_to_ohci(hcd));
@@ -155,8 +152,7 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
155 /* Error handling */ 152 /* Error handling */
156 at91_stop_hc(pdev); 153 at91_stop_hc(pdev);
157 154
158 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) 155 clk_put(hclk);
159 clk_put(hclk);
160 clk_put(fclk); 156 clk_put(fclk);
161 clk_put(iclk); 157 clk_put(iclk);
162 158
@@ -192,8 +188,7 @@ static void usb_hcd_at91_remove(struct usb_hcd *hcd,
192 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 188 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
193 usb_put_hcd(hcd); 189 usb_put_hcd(hcd);
194 190
195 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) 191 clk_put(hclk);
196 clk_put(hclk);
197 clk_put(fclk); 192 clk_put(fclk);
198 clk_put(iclk); 193 clk_put(iclk);
199 fclk = iclk = hclk = NULL; 194 fclk = iclk = hclk = NULL;
@@ -223,6 +218,162 @@ ohci_at91_start (struct usb_hcd *hcd)
223 return 0; 218 return 0;
224} 219}
225 220
221static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
222{
223 if (port < 0 || port >= 2)
224 return;
225
226 if (pdata->vbus_pin[port] <= 0)
227 return;
228
229 gpio_set_value(pdata->vbus_pin[port], !pdata->vbus_pin_inverted ^ enable);
230}
231
232static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
233{
234 if (port < 0 || port >= 2)
235 return -EINVAL;
236
237 if (pdata->vbus_pin[port] <= 0)
238 return -EINVAL;
239
240 return gpio_get_value(pdata->vbus_pin[port]) ^ !pdata->vbus_pin_inverted;
241}
242
243/*
244 * Update the status data from the hub with the over-current indicator change.
245 */
246static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
247{
248 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
249 int length = ohci_hub_status_data(hcd, buf);
250 int port;
251
252 for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
253 if (pdata->overcurrent_changed[port]) {
254 if (! length)
255 length = 1;
256 buf[0] |= 1 << (port + 1);
257 }
258 }
259
260 return length;
261}
262
263/*
264 * Look at the control requests to the root hub and see if we need to override.
265 */
266static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
267 u16 wIndex, char *buf, u16 wLength)
268{
269 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
270 struct usb_hub_descriptor *desc;
271 int ret = -EINVAL;
272 u32 *data = (u32 *)buf;
273
274 dev_dbg(hcd->self.controller,
275 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
276 hcd, typeReq, wValue, wIndex, buf, wLength);
277
278 switch (typeReq) {
279 case SetPortFeature:
280 if (wValue == USB_PORT_FEAT_POWER) {
281 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
282 ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
283 goto out;
284 }
285 break;
286
287 case ClearPortFeature:
288 switch (wValue) {
289 case USB_PORT_FEAT_C_OVER_CURRENT:
290 dev_dbg(hcd->self.controller,
291 "ClearPortFeature: C_OVER_CURRENT\n");
292
293 if (wIndex == 1 || wIndex == 2) {
294 pdata->overcurrent_changed[wIndex-1] = 0;
295 pdata->overcurrent_status[wIndex-1] = 0;
296 }
297
298 goto out;
299
300 case USB_PORT_FEAT_OVER_CURRENT:
301 dev_dbg(hcd->self.controller,
302 "ClearPortFeature: OVER_CURRENT\n");
303
304 if (wIndex == 1 || wIndex == 2) {
305 pdata->overcurrent_status[wIndex-1] = 0;
306 }
307
308 goto out;
309
310 case USB_PORT_FEAT_POWER:
311 dev_dbg(hcd->self.controller,
312 "ClearPortFeature: POWER\n");
313
314 if (wIndex == 1 || wIndex == 2) {
315 ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
316 return 0;
317 }
318 }
319 break;
320 }
321
322 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
323 if (ret)
324 goto out;
325
326 switch (typeReq) {
327 case GetHubDescriptor:
328
329 /* update the hub's descriptor */
330
331 desc = (struct usb_hub_descriptor *)buf;
332
333 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
334 desc->wHubCharacteristics);
335
336 /* remove the old configurations for power-switching, and
337 * over-current protection, and insert our new configuration
338 */
339
340 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
341 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
342
343 if (pdata->overcurrent_supported) {
344 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
345 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
346 }
347
348 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
349 desc->wHubCharacteristics);
350
351 return ret;
352
353 case GetPortStatus:
354 /* check port status */
355
356 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
357
358 if (wIndex == 1 || wIndex == 2) {
359 if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
360 *data &= ~cpu_to_le32(RH_PS_PPS);
361 }
362
363 if (pdata->overcurrent_changed[wIndex-1]) {
364 *data |= cpu_to_le32(RH_PS_OCIC);
365 }
366
367 if (pdata->overcurrent_status[wIndex-1]) {
368 *data |= cpu_to_le32(RH_PS_POCI);
369 }
370 }
371 }
372
373 out:
374 return ret;
375}
376
226/*-------------------------------------------------------------------------*/ 377/*-------------------------------------------------------------------------*/
227 378
228static const struct hc_driver ohci_at91_hc_driver = { 379static const struct hc_driver ohci_at91_hc_driver = {
@@ -258,8 +409,8 @@ static const struct hc_driver ohci_at91_hc_driver = {
258 /* 409 /*
259 * root hub support 410 * root hub support
260 */ 411 */
261 .hub_status_data = ohci_hub_status_data, 412 .hub_status_data = ohci_at91_hub_status_data,
262 .hub_control = ohci_hub_control, 413 .hub_control = ohci_at91_hub_control,
263#ifdef CONFIG_PM 414#ifdef CONFIG_PM
264 .bus_suspend = ohci_bus_suspend, 415 .bus_suspend = ohci_bus_suspend,
265 .bus_resume = ohci_bus_resume, 416 .bus_resume = ohci_bus_resume,
@@ -269,22 +420,71 @@ static const struct hc_driver ohci_at91_hc_driver = {
269 420
270/*-------------------------------------------------------------------------*/ 421/*-------------------------------------------------------------------------*/
271 422
423static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
424{
425 struct platform_device *pdev = data;
426 struct at91_usbh_data *pdata = pdev->dev.platform_data;
427 int val, gpio, port;
428
429 /* From the GPIO notifying the over-current situation, find
430 * out the corresponding port */
431 gpio = irq_to_gpio(irq);
432 for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
433 if (pdata->overcurrent_pin[port] == gpio)
434 break;
435 }
436
437 if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
438 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
439 return IRQ_HANDLED;
440 }
441
442 val = gpio_get_value(gpio);
443
444 /* When notified of an over-current situation, disable power
445 on the corresponding port, and mark this port in
446 over-current. */
447 if (! val) {
448 ohci_at91_usb_set_power(pdata, port, 0);
449 pdata->overcurrent_status[port] = 1;
450 pdata->overcurrent_changed[port] = 1;
451 }
452
453 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
454 val ? "exited" : "notified");
455
456 return IRQ_HANDLED;
457}
458
459/*-------------------------------------------------------------------------*/
460
272static int ohci_hcd_at91_drv_probe(struct platform_device *pdev) 461static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
273{ 462{
274 struct at91_usbh_data *pdata = pdev->dev.platform_data; 463 struct at91_usbh_data *pdata = pdev->dev.platform_data;
275 int i; 464 int i;
276 465
277 if (pdata) { 466 if (pdata) {
278 /* REVISIT make the driver support per-port power switching,
279 * and also overcurrent detection. Here we assume the ports
280 * are always powered while this driver is active, and use
281 * active-low power switches.
282 */
283 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) { 467 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
284 if (pdata->vbus_pin[i] <= 0) 468 if (pdata->vbus_pin[i] <= 0)
285 continue; 469 continue;
286 gpio_request(pdata->vbus_pin[i], "ohci_vbus"); 470 gpio_request(pdata->vbus_pin[i], "ohci_vbus");
287 gpio_direction_output(pdata->vbus_pin[i], 0); 471 ohci_at91_usb_set_power(pdata, i, 1);
472 }
473
474 for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
475 int ret;
476
477 if (pdata->overcurrent_pin[i] <= 0)
478 continue;
479 gpio_request(pdata->overcurrent_pin[i], "ohci_overcurrent");
480
481 ret = request_irq(gpio_to_irq(pdata->overcurrent_pin[i]),
482 ohci_hcd_at91_overcurrent_irq,
483 IRQF_SHARED, "ohci_overcurrent", pdev);
484 if (ret) {
485 gpio_free(pdata->overcurrent_pin[i]);
486 dev_warn(& pdev->dev, "cannot get GPIO IRQ for overcurrent\n");
487 }
288 } 488 }
289 } 489 }
290 490
@@ -301,9 +501,16 @@ static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
301 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) { 501 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
302 if (pdata->vbus_pin[i] <= 0) 502 if (pdata->vbus_pin[i] <= 0)
303 continue; 503 continue;
304 gpio_direction_output(pdata->vbus_pin[i], 1); 504 ohci_at91_usb_set_power(pdata, i, 0);
305 gpio_free(pdata->vbus_pin[i]); 505 gpio_free(pdata->vbus_pin[i]);
306 } 506 }
507
508 for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
509 if (pdata->overcurrent_pin[i] <= 0)
510 continue;
511 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
512 gpio_free(pdata->overcurrent_pin[i]);
513 }
307 } 514 }
308 515
309 device_init_wakeup(&pdev->dev, 0); 516 device_init_wakeup(&pdev->dev, 0);
diff --git a/drivers/usb/host/ohci-ath79.c b/drivers/usb/host/ohci-ath79.c
index c620c50f6770..18d574d6958b 100644
--- a/drivers/usb/host/ohci-ath79.c
+++ b/drivers/usb/host/ohci-ath79.c
@@ -111,7 +111,7 @@ static int ohci_ath79_probe(struct platform_device *pdev)
111 111
112 ohci_hcd_init(hcd_to_ohci(hcd)); 112 ohci_hcd_init(hcd_to_ohci(hcd));
113 113
114 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 114 ret = usb_add_hcd(hcd, irq, 0);
115 if (ret) 115 if (ret)
116 goto err_stop_hcd; 116 goto err_stop_hcd;
117 117
diff --git a/drivers/usb/host/ohci-au1xxx.c b/drivers/usb/host/ohci-au1xxx.c
index 958d985f2951..9b66df8278f3 100644
--- a/drivers/usb/host/ohci-au1xxx.c
+++ b/drivers/usb/host/ohci-au1xxx.c
@@ -23,92 +23,9 @@
23 23
24#include <asm/mach-au1x00/au1000.h> 24#include <asm/mach-au1x00/au1000.h>
25 25
26#ifndef CONFIG_SOC_AU1200
27
28#define USBH_ENABLE_BE (1<<0)
29#define USBH_ENABLE_C (1<<1)
30#define USBH_ENABLE_E (1<<2)
31#define USBH_ENABLE_CE (1<<3)
32#define USBH_ENABLE_RD (1<<4)
33
34#ifdef __LITTLE_ENDIAN
35#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C)
36#elif defined(__BIG_ENDIAN)
37#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C | \
38 USBH_ENABLE_BE)
39#else
40#error not byte order defined
41#endif
42
43#else /* Au1200 */
44
45#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
46#define USB_MCFG_PFEN (1<<31)
47#define USB_MCFG_RDCOMB (1<<30)
48#define USB_MCFG_SSDEN (1<<23)
49#define USB_MCFG_OHCCLKEN (1<<16)
50#ifdef CONFIG_DMA_COHERENT
51#define USB_MCFG_UCAM (1<<7)
52#else
53#define USB_MCFG_UCAM (0)
54#endif
55#define USB_MCFG_OBMEN (1<<1)
56#define USB_MCFG_OMEMEN (1<<0)
57
58#define USBH_ENABLE_CE USB_MCFG_OHCCLKEN
59
60#define USBH_ENABLE_INIT (USB_MCFG_PFEN | USB_MCFG_RDCOMB | \
61 USBH_ENABLE_CE | USB_MCFG_SSDEN | \
62 USB_MCFG_UCAM | \
63 USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
64
65#define USBH_DISABLE (USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
66
67#endif /* Au1200 */
68 26
69extern int usb_disabled(void); 27extern int usb_disabled(void);
70 28
71static void au1xxx_start_ohc(void)
72{
73 /* enable host controller */
74#ifndef CONFIG_SOC_AU1200
75 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG);
76 au_sync();
77 udelay(1000);
78
79 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
80 au_sync();
81 udelay(1000);
82
83 /* wait for reset complete (read register twice; see au1500 errata) */
84 while (au_readl(USB_HOST_CONFIG),
85 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD))
86 udelay(1000);
87
88#else /* Au1200 */
89 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_CE, USB_HOST_CONFIG);
90 au_sync();
91 udelay(1000);
92
93 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
94 au_sync();
95 udelay(2000);
96#endif /* Au1200 */
97}
98
99static void au1xxx_stop_ohc(void)
100{
101#ifdef CONFIG_SOC_AU1200
102 /* Disable mem */
103 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_DISABLE, USB_HOST_CONFIG);
104 au_sync();
105 udelay(1000);
106#endif
107 /* Disable clock */
108 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
109 au_sync();
110}
111
112static int __devinit ohci_au1xxx_start(struct usb_hcd *hcd) 29static int __devinit ohci_au1xxx_start(struct usb_hcd *hcd)
113{ 30{
114 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 31 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
@@ -178,17 +95,6 @@ static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
178 if (usb_disabled()) 95 if (usb_disabled())
179 return -ENODEV; 96 return -ENODEV;
180 97
181#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
182 /* Au1200 AB USB does not support coherent memory */
183 if (!(read_c0_prid() & 0xff)) {
184 printk(KERN_INFO "%s: this is chip revision AB !!\n",
185 pdev->name);
186 printk(KERN_INFO "%s: update your board or re-configure "
187 "the kernel\n", pdev->name);
188 return -ENODEV;
189 }
190#endif
191
192 if (pdev->resource[1].flags != IORESOURCE_IRQ) { 98 if (pdev->resource[1].flags != IORESOURCE_IRQ) {
193 pr_debug("resource[1] is not IORESOURCE_IRQ\n"); 99 pr_debug("resource[1] is not IORESOURCE_IRQ\n");
194 return -ENOMEM; 100 return -ENOMEM;
@@ -214,17 +120,23 @@ static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
214 goto err2; 120 goto err2;
215 } 121 }
216 122
217 au1xxx_start_ohc(); 123 if (alchemy_usb_control(ALCHEMY_USB_OHCI0, 1)) {
124 printk(KERN_INFO "%s: controller init failed!\n", pdev->name);
125 ret = -ENODEV;
126 goto err3;
127 }
128
218 ohci_hcd_init(hcd_to_ohci(hcd)); 129 ohci_hcd_init(hcd_to_ohci(hcd));
219 130
220 ret = usb_add_hcd(hcd, pdev->resource[1].start, 131 ret = usb_add_hcd(hcd, pdev->resource[1].start,
221 IRQF_DISABLED | IRQF_SHARED); 132 IRQF_SHARED);
222 if (ret == 0) { 133 if (ret == 0) {
223 platform_set_drvdata(pdev, hcd); 134 platform_set_drvdata(pdev, hcd);
224 return ret; 135 return ret;
225 } 136 }
226 137
227 au1xxx_stop_ohc(); 138 alchemy_usb_control(ALCHEMY_USB_OHCI0, 0);
139err3:
228 iounmap(hcd->regs); 140 iounmap(hcd->regs);
229err2: 141err2:
230 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 142 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
@@ -238,7 +150,7 @@ static int ohci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
238 struct usb_hcd *hcd = platform_get_drvdata(pdev); 150 struct usb_hcd *hcd = platform_get_drvdata(pdev);
239 151
240 usb_remove_hcd(hcd); 152 usb_remove_hcd(hcd);
241 au1xxx_stop_ohc(); 153 alchemy_usb_control(ALCHEMY_USB_OHCI0, 0);
242 iounmap(hcd->regs); 154 iounmap(hcd->regs);
243 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 155 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
244 usb_put_hcd(hcd); 156 usb_put_hcd(hcd);
@@ -275,7 +187,7 @@ static int ohci_hcd_au1xxx_drv_suspend(struct device *dev)
275 187
276 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 188 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
277 189
278 au1xxx_stop_ohc(); 190 alchemy_usb_control(ALCHEMY_USB_OHCI0, 0);
279bail: 191bail:
280 spin_unlock_irqrestore(&ohci->lock, flags); 192 spin_unlock_irqrestore(&ohci->lock, flags);
281 193
@@ -286,7 +198,7 @@ static int ohci_hcd_au1xxx_drv_resume(struct device *dev)
286{ 198{
287 struct usb_hcd *hcd = dev_get_drvdata(dev); 199 struct usb_hcd *hcd = dev_get_drvdata(dev);
288 200
289 au1xxx_start_ohc(); 201 alchemy_usb_control(ALCHEMY_USB_OHCI0, 1);
290 202
291 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 203 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
292 ohci_finish_controller_resume(hcd); 204 ohci_finish_controller_resume(hcd);
diff --git a/drivers/usb/host/ohci-da8xx.c b/drivers/usb/host/ohci-da8xx.c
index 6aca2c4453f7..843509778a33 100644
--- a/drivers/usb/host/ohci-da8xx.c
+++ b/drivers/usb/host/ohci-da8xx.c
@@ -344,7 +344,7 @@ static int usb_hcd_da8xx_probe(const struct hc_driver *driver,
344 error = -ENODEV; 344 error = -ENODEV;
345 goto err4; 345 goto err4;
346 } 346 }
347 error = usb_add_hcd(hcd, irq, IRQF_DISABLED); 347 error = usb_add_hcd(hcd, irq, 0);
348 if (error) 348 if (error)
349 goto err4; 349 goto err4;
350 350
diff --git a/drivers/usb/host/ohci-ep93xx.c b/drivers/usb/host/ohci-ep93xx.c
index 4e681613e7ae..dc45d489d00e 100644
--- a/drivers/usb/host/ohci-ep93xx.c
+++ b/drivers/usb/host/ohci-ep93xx.c
@@ -81,7 +81,7 @@ static int usb_hcd_ep93xx_probe(const struct hc_driver *driver,
81 81
82 ohci_hcd_init(hcd_to_ohci(hcd)); 82 ohci_hcd_init(hcd_to_ohci(hcd));
83 83
84 retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED); 84 retval = usb_add_hcd(hcd, pdev->resource[1].start, 0);
85 if (retval == 0) 85 if (retval == 0)
86 return retval; 86 return retval;
87 87
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index f9cf3f04b742..b2639191549e 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -389,17 +389,14 @@ ohci_shutdown (struct usb_hcd *hcd)
389 struct ohci_hcd *ohci; 389 struct ohci_hcd *ohci;
390 390
391 ohci = hcd_to_ohci (hcd); 391 ohci = hcd_to_ohci (hcd);
392 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable); 392 ohci_writel(ohci, (u32) ~0, &ohci->regs->intrdisable);
393 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
394 393
395 /* If the SHUTDOWN quirk is set, don't put the controller in RESET */ 394 /* Software reset, after which the controller goes into SUSPEND */
396 ohci->hc_control &= (ohci->flags & OHCI_QUIRK_SHUTDOWN ? 395 ohci_writel(ohci, OHCI_HCR, &ohci->regs->cmdstatus);
397 OHCI_CTRL_RWC | OHCI_CTRL_HCFS : 396 ohci_readl(ohci, &ohci->regs->cmdstatus); /* flush the writes */
398 OHCI_CTRL_RWC); 397 udelay(10);
399 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
400 398
401 /* flush the writes */ 399 ohci_writel(ohci, ohci->fminterval, &ohci->regs->fminterval);
402 (void) ohci_readl (ohci, &ohci->regs->control);
403} 400}
404 401
405static int check_ed(struct ohci_hcd *ohci, struct ed *ed) 402static int check_ed(struct ohci_hcd *ohci, struct ed *ed)
@@ -1114,6 +1111,11 @@ MODULE_LICENSE ("GPL");
1114#define PLATFORM_DRIVER ohci_hcd_ath79_driver 1111#define PLATFORM_DRIVER ohci_hcd_ath79_driver
1115#endif 1112#endif
1116 1113
1114#ifdef CONFIG_NLM_XLR
1115#include "ohci-xls.c"
1116#define PLATFORM_DRIVER ohci_xls_driver
1117#endif
1118
1117#if !defined(PCI_DRIVER) && \ 1119#if !defined(PCI_DRIVER) && \
1118 !defined(PLATFORM_DRIVER) && \ 1120 !defined(PLATFORM_DRIVER) && \
1119 !defined(OMAP1_PLATFORM_DRIVER) && \ 1121 !defined(OMAP1_PLATFORM_DRIVER) && \
diff --git a/drivers/usb/host/ohci-hub.c b/drivers/usb/host/ohci-hub.c
index 9154615292db..2f00040fc408 100644
--- a/drivers/usb/host/ohci-hub.c
+++ b/drivers/usb/host/ohci-hub.c
@@ -356,10 +356,7 @@ static void ohci_finish_controller_resume(struct usb_hcd *hcd)
356 msleep(20); 356 msleep(20);
357 } 357 }
358 358
359 /* Does the root hub have a port wakeup pending? */ 359 usb_hcd_resume_root_hub(hcd);
360 if (ohci_readl(ohci, &ohci->regs->intrstatus) &
361 (OHCI_INTR_RD | OHCI_INTR_RHSC))
362 usb_hcd_resume_root_hub(hcd);
363} 360}
364 361
365/* Carry out polling-, autostop-, and autoresume-related state changes */ 362/* Carry out polling-, autostop-, and autoresume-related state changes */
diff --git a/drivers/usb/host/ohci-octeon.c b/drivers/usb/host/ohci-octeon.c
index d8b45647d1dc..d469bf9b9e54 100644
--- a/drivers/usb/host/ohci-octeon.c
+++ b/drivers/usb/host/ohci-octeon.c
@@ -164,7 +164,7 @@ static int ohci_octeon_drv_probe(struct platform_device *pdev)
164 164
165 ohci_hcd_init(ohci); 165 ohci_hcd_init(ohci);
166 166
167 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 167 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
168 if (ret) { 168 if (ret) {
169 dev_dbg(&pdev->dev, "failed to add hcd with err %d\n", ret); 169 dev_dbg(&pdev->dev, "failed to add hcd with err %d\n", ret);
170 goto err3; 170 goto err3;
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index 5645f70b9214..e4b8782cc6e2 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -14,7 +14,7 @@
14 * This file is licenced under the GPL. 14 * This file is licenced under the GPL.
15 */ 15 */
16 16
17#include <linux/signal.h> /* IRQF_DISABLED */ 17#include <linux/signal.h>
18#include <linux/jiffies.h> 18#include <linux/jiffies.h>
19#include <linux/platform_device.h> 19#include <linux/platform_device.h>
20#include <linux/clk.h> 20#include <linux/clk.h>
@@ -363,7 +363,7 @@ static int usb_hcd_omap_probe (const struct hc_driver *driver,
363 retval = -ENXIO; 363 retval = -ENXIO;
364 goto err3; 364 goto err3;
365 } 365 }
366 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED); 366 retval = usb_add_hcd(hcd, irq, 0);
367 if (retval) 367 if (retval)
368 goto err3; 368 goto err3;
369 369
diff --git a/drivers/usb/host/ohci-omap3.c b/drivers/usb/host/ohci-omap3.c
index 6048f2f64f73..516ebc4d6cc2 100644
--- a/drivers/usb/host/ohci-omap3.c
+++ b/drivers/usb/host/ohci-omap3.c
@@ -149,7 +149,7 @@ static int __devinit ohci_hcd_omap3_probe(struct platform_device *pdev)
149 149
150 res = platform_get_resource_byname(pdev, 150 res = platform_get_resource_byname(pdev,
151 IORESOURCE_MEM, "ohci"); 151 IORESOURCE_MEM, "ohci");
152 if (!ret) { 152 if (!res) {
153 dev_err(dev, "UHH OHCI get resource failed\n"); 153 dev_err(dev, "UHH OHCI get resource failed\n");
154 return -ENOMEM; 154 return -ENOMEM;
155 } 155 }
@@ -180,7 +180,7 @@ static int __devinit ohci_hcd_omap3_probe(struct platform_device *pdev)
180 180
181 ohci_hcd_init(hcd_to_ohci(hcd)); 181 ohci_hcd_init(hcd_to_ohci(hcd));
182 182
183 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 183 ret = usb_add_hcd(hcd, irq, 0);
184 if (ret) { 184 if (ret) {
185 dev_dbg(dev, "failed to add hcd with err %d\n", ret); 185 dev_dbg(dev, "failed to add hcd with err %d\n", ret);
186 goto err_add_hcd; 186 goto err_add_hcd;
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index ad8166c681e2..bc01b064585a 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -175,28 +175,6 @@ static int ohci_quirk_amd700(struct usb_hcd *hcd)
175 return 0; 175 return 0;
176} 176}
177 177
178/* nVidia controllers continue to drive Reset signalling on the bus
179 * even after system shutdown, wasting power. This flag tells the
180 * shutdown routine to leave the controller OPERATIONAL instead of RESET.
181 */
182static int ohci_quirk_nvidia_shutdown(struct usb_hcd *hcd)
183{
184 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
185 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
186
187 /* Evidently nVidia fixed their later hardware; this is a guess at
188 * the changeover point.
189 */
190#define PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_USB 0x026d
191
192 if (pdev->device < PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_USB) {
193 ohci->flags |= OHCI_QUIRK_SHUTDOWN;
194 ohci_dbg(ohci, "enabled nVidia shutdown quirk\n");
195 }
196
197 return 0;
198}
199
200static void sb800_prefetch(struct ohci_hcd *ohci, int on) 178static void sb800_prefetch(struct ohci_hcd *ohci, int on)
201{ 179{
202 struct pci_dev *pdev; 180 struct pci_dev *pdev;
@@ -260,10 +238,6 @@ static const struct pci_device_id ohci_pci_quirks[] = {
260 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4399), 238 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4399),
261 .driver_data = (unsigned long)ohci_quirk_amd700, 239 .driver_data = (unsigned long)ohci_quirk_amd700,
262 }, 240 },
263 {
264 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
265 .driver_data = (unsigned long) ohci_quirk_nvidia_shutdown,
266 },
267 241
268 /* FIXME for some of the early AMD 760 southbridges, OHCI 242 /* FIXME for some of the early AMD 760 southbridges, OHCI
269 * won't work at all. blacklist them. 243 * won't work at all. blacklist them.
diff --git a/drivers/usb/host/ohci-pnx4008.c b/drivers/usb/host/ohci-pnx4008.c
index 653d6a60edb5..0013db7bdf92 100644
--- a/drivers/usb/host/ohci-pnx4008.c
+++ b/drivers/usb/host/ohci-pnx4008.c
@@ -26,7 +26,7 @@
26 26
27#include <mach/platform.h> 27#include <mach/platform.h>
28#include <mach/irqs.h> 28#include <mach/irqs.h>
29#include <mach/gpio.h> 29#include <asm/gpio.h>
30 30
31#define USB_CTRL IO_ADDRESS(PNX4008_PWRMAN_BASE + 0x64) 31#define USB_CTRL IO_ADDRESS(PNX4008_PWRMAN_BASE + 0x64)
32 32
@@ -398,7 +398,7 @@ static int __devinit usb_hcd_pnx4008_probe(struct platform_device *pdev)
398 ohci_hcd_init(ohci); 398 ohci_hcd_init(ohci);
399 399
400 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq); 400 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
401 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 401 ret = usb_add_hcd(hcd, irq, 0);
402 if (ret == 0) 402 if (ret == 0)
403 return ret; 403 return ret;
404 404
diff --git a/drivers/usb/host/ohci-pnx8550.c b/drivers/usb/host/ohci-pnx8550.c
index 28467e288a93..f13d08f94d6b 100644
--- a/drivers/usb/host/ohci-pnx8550.c
+++ b/drivers/usb/host/ohci-pnx8550.c
@@ -107,7 +107,7 @@ int usb_hcd_pnx8550_probe (const struct hc_driver *driver,
107 107
108 ohci_hcd_init(hcd_to_ohci(hcd)); 108 ohci_hcd_init(hcd_to_ohci(hcd));
109 109
110 retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED); 110 retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
111 if (retval == 0) 111 if (retval == 0)
112 return retval; 112 return retval;
113 113
diff --git a/drivers/usb/host/ohci-ppc-of.c b/drivers/usb/host/ohci-ppc-of.c
index 0c12f4e14dcd..d24cc89de16f 100644
--- a/drivers/usb/host/ohci-ppc-of.c
+++ b/drivers/usb/host/ohci-ppc-of.c
@@ -143,7 +143,7 @@ static int __devinit ohci_hcd_ppc_of_probe(struct platform_device *op)
143 143
144 ohci_hcd_init(ohci); 144 ohci_hcd_init(ohci);
145 145
146 rv = usb_add_hcd(hcd, irq, IRQF_DISABLED); 146 rv = usb_add_hcd(hcd, irq, 0);
147 if (rv == 0) 147 if (rv == 0)
148 return 0; 148 return 0;
149 149
diff --git a/drivers/usb/host/ohci-ppc-soc.c b/drivers/usb/host/ohci-ppc-soc.c
index c0f595c44487..1514b7067470 100644
--- a/drivers/usb/host/ohci-ppc-soc.c
+++ b/drivers/usb/host/ohci-ppc-soc.c
@@ -80,7 +80,7 @@ static int usb_hcd_ppc_soc_probe(const struct hc_driver *driver,
80#endif 80#endif
81 ohci_hcd_init(ohci); 81 ohci_hcd_init(ohci);
82 82
83 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED); 83 retval = usb_add_hcd(hcd, irq, 0);
84 if (retval == 0) 84 if (retval == 0)
85 return retval; 85 return retval;
86 86
diff --git a/drivers/usb/host/ohci-ps3.c b/drivers/usb/host/ohci-ps3.c
index 700950455f4d..6fd4fa1f19bb 100644
--- a/drivers/usb/host/ohci-ps3.c
+++ b/drivers/usb/host/ohci-ps3.c
@@ -164,7 +164,7 @@ static int __devinit ps3_ohci_probe(struct ps3_system_bus_device *dev)
164 164
165 ps3_system_bus_set_drvdata(dev, hcd); 165 ps3_system_bus_set_drvdata(dev, hcd);
166 166
167 result = usb_add_hcd(hcd, virq, IRQF_DISABLED); 167 result = usb_add_hcd(hcd, virq, 0);
168 168
169 if (result) { 169 if (result) {
170 dev_dbg(&dev->core, "%s:%d: usb_add_hcd failed (%d)\n", 170 dev_dbg(&dev->core, "%s:%d: usb_add_hcd failed (%d)\n",
diff --git a/drivers/usb/host/ohci-pxa27x.c b/drivers/usb/host/ohci-pxa27x.c
index 80be5472783a..29dfefe1c726 100644
--- a/drivers/usb/host/ohci-pxa27x.c
+++ b/drivers/usb/host/ohci-pxa27x.c
@@ -359,7 +359,7 @@ int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device
359 359
360 ohci_hcd_init(hcd_to_ohci(hcd)); 360 ohci_hcd_init(hcd_to_ohci(hcd));
361 361
362 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED); 362 retval = usb_add_hcd(hcd, irq, 0);
363 if (retval == 0) 363 if (retval == 0)
364 return retval; 364 return retval;
365 365
diff --git a/drivers/usb/host/ohci-q.c b/drivers/usb/host/ohci-q.c
index dd24fc115e48..15dc51ded61a 100644
--- a/drivers/usb/host/ohci-q.c
+++ b/drivers/usb/host/ohci-q.c
@@ -428,7 +428,7 @@ static struct ed *ed_get (
428 ed->type = usb_pipetype(pipe); 428 ed->type = usb_pipetype(pipe);
429 429
430 info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7; 430 info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
431 info |= le16_to_cpu(ep->desc.wMaxPacketSize) << 16; 431 info |= usb_endpoint_maxp(&ep->desc) << 16;
432 if (udev->speed == USB_SPEED_LOW) 432 if (udev->speed == USB_SPEED_LOW)
433 info |= ED_LOWSPEED; 433 info |= ED_LOWSPEED;
434 /* only control transfers store pids in tds */ 434 /* only control transfers store pids in tds */
@@ -444,7 +444,7 @@ static struct ed *ed_get (
444 ed->load = usb_calc_bus_time ( 444 ed->load = usb_calc_bus_time (
445 udev->speed, !is_out, 445 udev->speed, !is_out,
446 ed->type == PIPE_ISOCHRONOUS, 446 ed->type == PIPE_ISOCHRONOUS,
447 le16_to_cpu(ep->desc.wMaxPacketSize)) 447 usb_endpoint_maxp(&ep->desc))
448 / 1000; 448 / 1000;
449 } 449 }
450 } 450 }
diff --git a/drivers/usb/host/ohci-s3c2410.c b/drivers/usb/host/ohci-s3c2410.c
index 7c9a4d55526b..a1877c47601e 100644
--- a/drivers/usb/host/ohci-s3c2410.c
+++ b/drivers/usb/host/ohci-s3c2410.c
@@ -384,7 +384,7 @@ static int usb_hcd_s3c2410_probe(const struct hc_driver *driver,
384 384
385 ohci_hcd_init(hcd_to_ohci(hcd)); 385 ohci_hcd_init(hcd_to_ohci(hcd));
386 386
387 retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED); 387 retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
388 if (retval != 0) 388 if (retval != 0)
389 goto err_ioremap; 389 goto err_ioremap;
390 390
diff --git a/drivers/usb/host/ohci-sa1111.c b/drivers/usb/host/ohci-sa1111.c
index 4204d9720d23..4bde4f9821ba 100644
--- a/drivers/usb/host/ohci-sa1111.c
+++ b/drivers/usb/host/ohci-sa1111.c
@@ -143,7 +143,7 @@ int usb_hcd_sa1111_probe (const struct hc_driver *driver,
143 sa1111_start_hc(dev); 143 sa1111_start_hc(dev);
144 ohci_hcd_init(hcd_to_ohci(hcd)); 144 ohci_hcd_init(hcd_to_ohci(hcd));
145 145
146 retval = usb_add_hcd(hcd, dev->irq[1], IRQF_DISABLED); 146 retval = usb_add_hcd(hcd, dev->irq[1], 0);
147 if (retval == 0) 147 if (retval == 0)
148 return retval; 148 return retval;
149 149
diff --git a/drivers/usb/host/ohci-sh.c b/drivers/usb/host/ohci-sh.c
index 14cecb52a9fe..afc4eb6bb9d0 100644
--- a/drivers/usb/host/ohci-sh.c
+++ b/drivers/usb/host/ohci-sh.c
@@ -109,7 +109,7 @@ static int ohci_hcd_sh_probe(struct platform_device *pdev)
109 hcd->regs = (void __iomem *)res->start; 109 hcd->regs = (void __iomem *)res->start;
110 hcd->rsrc_start = res->start; 110 hcd->rsrc_start = res->start;
111 hcd->rsrc_len = resource_size(res); 111 hcd->rsrc_len = resource_size(res);
112 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 112 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
113 if (ret != 0) { 113 if (ret != 0) {
114 err("Failed to add hcd"); 114 err("Failed to add hcd");
115 usb_put_hcd(hcd); 115 usb_put_hcd(hcd);
diff --git a/drivers/usb/host/ohci-sm501.c b/drivers/usb/host/ohci-sm501.c
index 78918ca0da23..968cea2b6d4e 100644
--- a/drivers/usb/host/ohci-sm501.c
+++ b/drivers/usb/host/ohci-sm501.c
@@ -165,7 +165,7 @@ static int ohci_hcd_sm501_drv_probe(struct platform_device *pdev)
165 165
166 ohci_hcd_init(hcd_to_ohci(hcd)); 166 ohci_hcd_init(hcd_to_ohci(hcd));
167 167
168 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED); 168 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
169 if (retval) 169 if (retval)
170 goto err5; 170 goto err5;
171 171
diff --git a/drivers/usb/host/ohci-spear.c b/drivers/usb/host/ohci-spear.c
index 4fd4bea9ac7a..69874654f3b5 100644
--- a/drivers/usb/host/ohci-spear.c
+++ b/drivers/usb/host/ohci-spear.c
@@ -152,7 +152,7 @@ static int spear_ohci_hcd_drv_probe(struct platform_device *pdev)
152 spear_start_ohci(ohci_p); 152 spear_start_ohci(ohci_p);
153 ohci_hcd_init(hcd_to_ohci(hcd)); 153 ohci_hcd_init(hcd_to_ohci(hcd));
154 154
155 retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), IRQF_DISABLED); 155 retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
156 if (retval == 0) 156 if (retval == 0)
157 return retval; 157 return retval;
158 158
diff --git a/drivers/usb/host/ohci-ssb.c b/drivers/usb/host/ohci-ssb.c
index c4aea3b8315e..5ba18595d6f7 100644
--- a/drivers/usb/host/ohci-ssb.c
+++ b/drivers/usb/host/ohci-ssb.c
@@ -169,7 +169,7 @@ static int ssb_ohci_attach(struct ssb_device *dev)
169 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); 169 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
170 if (!hcd->regs) 170 if (!hcd->regs)
171 goto err_put_hcd; 171 goto err_put_hcd;
172 err = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED); 172 err = usb_add_hcd(hcd, dev->irq, IRQF_SHARED);
173 if (err) 173 if (err)
174 goto err_iounmap; 174 goto err_iounmap;
175 175
diff --git a/drivers/usb/host/ohci-tmio.c b/drivers/usb/host/ohci-tmio.c
index 57ad1271fc9b..06331d931171 100644
--- a/drivers/usb/host/ohci-tmio.c
+++ b/drivers/usb/host/ohci-tmio.c
@@ -244,7 +244,7 @@ static int __devinit ohci_hcd_tmio_drv_probe(struct platform_device *dev)
244 ohci = hcd_to_ohci(hcd); 244 ohci = hcd_to_ohci(hcd);
245 ohci_hcd_init(ohci); 245 ohci_hcd_init(ohci);
246 246
247 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 247 ret = usb_add_hcd(hcd, irq, 0);
248 if (ret) 248 if (ret)
249 goto err_add_hcd; 249 goto err_add_hcd;
250 250
diff --git a/drivers/usb/host/ohci-xls.c b/drivers/usb/host/ohci-xls.c
new file mode 100644
index 000000000000..a3a9c6f45b91
--- /dev/null
+++ b/drivers/usb/host/ohci-xls.c
@@ -0,0 +1,151 @@
1/*
2 * OHCI HCD for Netlogic XLS processors.
3 *
4 * (C) Copyright 2011 Netlogic Microsystems Inc.
5 *
6 * Based on ohci-au1xxx.c, and other Linux OHCI drivers.
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
11 */
12
13#include <linux/platform_device.h>
14#include <linux/signal.h>
15
16static int ohci_xls_probe_internal(const struct hc_driver *driver,
17 struct platform_device *dev)
18{
19 struct resource *res;
20 struct usb_hcd *hcd;
21 int retval, irq;
22
23 /* Get our IRQ from an earlier registered Platform Resource */
24 irq = platform_get_irq(dev, 0);
25 if (irq < 0) {
26 dev_err(&dev->dev, "Found HC with no IRQ\n");
27 return -ENODEV;
28 }
29
30 /* Get our Memory Handle */
31 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
32 if (!res) {
33 dev_err(&dev->dev, "MMIO Handle incorrect!\n");
34 return -ENODEV;
35 }
36
37 hcd = usb_create_hcd(driver, &dev->dev, "XLS");
38 if (!hcd) {
39 retval = -ENOMEM;
40 goto err1;
41 }
42 hcd->rsrc_start = res->start;
43 hcd->rsrc_len = res->end - res->start + 1;
44
45 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
46 driver->description)) {
47 dev_dbg(&dev->dev, "Controller already in use\n");
48 retval = -EBUSY;
49 goto err2;
50 }
51
52 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
53 if (hcd->regs == NULL) {
54 dev_dbg(&dev->dev, "error mapping memory\n");
55 retval = -EFAULT;
56 goto err3;
57 }
58
59 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
60 if (retval != 0)
61 goto err4;
62 return retval;
63
64err4:
65 iounmap(hcd->regs);
66err3:
67 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
68err2:
69 usb_put_hcd(hcd);
70err1:
71 dev_err(&dev->dev, "init fail, %d\n", retval);
72 return retval;
73}
74
75static int ohci_xls_reset(struct usb_hcd *hcd)
76{
77 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
78
79 ohci_hcd_init(ohci);
80 return ohci_init(ohci);
81}
82
83static int __devinit ohci_xls_start(struct usb_hcd *hcd)
84{
85 struct ohci_hcd *ohci;
86 int ret;
87
88 ohci = hcd_to_ohci(hcd);
89 ret = ohci_run(ohci);
90 if (ret < 0) {
91 err("can't start %s", hcd->self.bus_name);
92 ohci_stop(hcd);
93 return ret;
94 }
95 return 0;
96}
97
98static struct hc_driver ohci_xls_hc_driver = {
99 .description = hcd_name,
100 .product_desc = "XLS OHCI Host Controller",
101 .hcd_priv_size = sizeof(struct ohci_hcd),
102 .irq = ohci_irq,
103 .flags = HCD_MEMORY | HCD_USB11,
104 .reset = ohci_xls_reset,
105 .start = ohci_xls_start,
106 .stop = ohci_stop,
107 .shutdown = ohci_shutdown,
108 .urb_enqueue = ohci_urb_enqueue,
109 .urb_dequeue = ohci_urb_dequeue,
110 .endpoint_disable = ohci_endpoint_disable,
111 .get_frame_number = ohci_get_frame,
112 .hub_status_data = ohci_hub_status_data,
113 .hub_control = ohci_hub_control,
114#ifdef CONFIG_PM
115 .bus_suspend = ohci_bus_suspend,
116 .bus_resume = ohci_bus_resume,
117#endif
118 .start_port_reset = ohci_start_port_reset,
119};
120
121static int ohci_xls_probe(struct platform_device *dev)
122{
123 int ret;
124
125 pr_debug("In ohci_xls_probe");
126 if (usb_disabled())
127 return -ENODEV;
128 ret = ohci_xls_probe_internal(&ohci_xls_hc_driver, dev);
129 return ret;
130}
131
132static int ohci_xls_remove(struct platform_device *dev)
133{
134 struct usb_hcd *hcd = platform_get_drvdata(dev);
135
136 usb_remove_hcd(hcd);
137 iounmap(hcd->regs);
138 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
139 usb_put_hcd(hcd);
140 return 0;
141}
142
143static struct platform_driver ohci_xls_driver = {
144 .probe = ohci_xls_probe,
145 .remove = ohci_xls_remove,
146 .shutdown = usb_hcd_platform_shutdown,
147 .driver = {
148 .name = "ohci-xls-0",
149 .owner = THIS_MODULE,
150 },
151};
diff --git a/drivers/usb/host/ohci.h b/drivers/usb/host/ohci.h
index 35e5fd640ce7..0795b934d00c 100644
--- a/drivers/usb/host/ohci.h
+++ b/drivers/usb/host/ohci.h
@@ -403,7 +403,6 @@ struct ohci_hcd {
403#define OHCI_QUIRK_HUB_POWER 0x100 /* distrust firmware power/oc setup */ 403#define OHCI_QUIRK_HUB_POWER 0x100 /* distrust firmware power/oc setup */
404#define OHCI_QUIRK_AMD_PLL 0x200 /* AMD PLL quirk*/ 404#define OHCI_QUIRK_AMD_PLL 0x200 /* AMD PLL quirk*/
405#define OHCI_QUIRK_AMD_PREFETCH 0x400 /* pre-fetch for ISO transfer */ 405#define OHCI_QUIRK_AMD_PREFETCH 0x400 /* pre-fetch for ISO transfer */
406#define OHCI_QUIRK_SHUTDOWN 0x800 /* nVidia power bug */
407 // there are also chip quirks/bugs in init logic 406 // there are also chip quirks/bugs in init logic
408 407
409 struct work_struct nec_work; /* Worker for NEC quirk */ 408 struct work_struct nec_work; /* Worker for NEC quirk */
diff --git a/drivers/usb/host/pci-quirks.c b/drivers/usb/host/pci-quirks.c
index 629a96813fd6..caf87428ca43 100644
--- a/drivers/usb/host/pci-quirks.c
+++ b/drivers/usb/host/pci-quirks.c
@@ -13,6 +13,7 @@
13#include <linux/pci.h> 13#include <linux/pci.h>
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/delay.h> 15#include <linux/delay.h>
16#include <linux/export.h>
16#include <linux/acpi.h> 17#include <linux/acpi.h>
17#include <linux/dmi.h> 18#include <linux/dmi.h>
18#include "pci-quirks.h" 19#include "pci-quirks.h"
@@ -36,6 +37,7 @@
36#define OHCI_INTRENABLE 0x10 37#define OHCI_INTRENABLE 0x10
37#define OHCI_INTRDISABLE 0x14 38#define OHCI_INTRDISABLE 0x14
38#define OHCI_FMINTERVAL 0x34 39#define OHCI_FMINTERVAL 0x34
40#define OHCI_HCFS (3 << 6) /* hc functional state */
39#define OHCI_HCR (1 << 0) /* host controller reset */ 41#define OHCI_HCR (1 << 0) /* host controller reset */
40#define OHCI_OCR (1 << 3) /* ownership change request */ 42#define OHCI_OCR (1 << 3) /* ownership change request */
41#define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */ 43#define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */
@@ -465,6 +467,8 @@ static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
465{ 467{
466 void __iomem *base; 468 void __iomem *base;
467 u32 control; 469 u32 control;
470 u32 fminterval;
471 int cnt;
468 472
469 if (!mmio_resource_enabled(pdev, 0)) 473 if (!mmio_resource_enabled(pdev, 0))
470 return; 474 return;
@@ -497,41 +501,32 @@ static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
497 } 501 }
498#endif 502#endif
499 503
500 /* reset controller, preserving RWC (and possibly IR) */ 504 /* disable interrupts */
501 writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL); 505 writel((u32) ~0, base + OHCI_INTRDISABLE);
502 readl(base + OHCI_CONTROL);
503 506
504 /* Some NVIDIA controllers stop working if kept in RESET for too long */ 507 /* Reset the USB bus, if the controller isn't already in RESET */
505 if (pdev->vendor == PCI_VENDOR_ID_NVIDIA) { 508 if (control & OHCI_HCFS) {
506 u32 fminterval; 509 /* Go into RESET, preserving RWC (and possibly IR) */
507 int cnt; 510 writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
511 readl(base + OHCI_CONTROL);
508 512
509 /* drive reset for at least 50 ms (7.1.7.5) */ 513 /* drive bus reset for at least 50 ms (7.1.7.5) */
510 msleep(50); 514 msleep(50);
515 }
511 516
512 /* software reset of the controller, preserving HcFmInterval */ 517 /* software reset of the controller, preserving HcFmInterval */
513 fminterval = readl(base + OHCI_FMINTERVAL); 518 fminterval = readl(base + OHCI_FMINTERVAL);
514 writel(OHCI_HCR, base + OHCI_CMDSTATUS); 519 writel(OHCI_HCR, base + OHCI_CMDSTATUS);
515 520
516 /* reset requires max 10 us delay */ 521 /* reset requires max 10 us delay */
517 for (cnt = 30; cnt > 0; --cnt) { /* ... allow extra time */ 522 for (cnt = 30; cnt > 0; --cnt) { /* ... allow extra time */
518 if ((readl(base + OHCI_CMDSTATUS) & OHCI_HCR) == 0) 523 if ((readl(base + OHCI_CMDSTATUS) & OHCI_HCR) == 0)
519 break; 524 break;
520 udelay(1); 525 udelay(1);
521 }
522 writel(fminterval, base + OHCI_FMINTERVAL);
523
524 /* Now we're in the SUSPEND state with all devices reset
525 * and wakeups and interrupts disabled
526 */
527 } 526 }
527 writel(fminterval, base + OHCI_FMINTERVAL);
528 528
529 /* 529 /* Now the controller is safely in SUSPEND and nothing can wake it up */
530 * disable interrupts
531 */
532 writel(~(u32)0, base + OHCI_INTRDISABLE);
533 writel(~(u32)0, base + OHCI_INTRSTATUS);
534
535 iounmap(base); 530 iounmap(base);
536} 531}
537 532
@@ -626,7 +621,7 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
626 void __iomem *base, *op_reg_base; 621 void __iomem *base, *op_reg_base;
627 u32 hcc_params, cap, val; 622 u32 hcc_params, cap, val;
628 u8 offset, cap_length; 623 u8 offset, cap_length;
629 int wait_time, delta, count = 256/4; 624 int wait_time, count = 256/4;
630 625
631 if (!mmio_resource_enabled(pdev, 0)) 626 if (!mmio_resource_enabled(pdev, 0))
632 return; 627 return;
@@ -672,11 +667,10 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
672 writel(val, op_reg_base + EHCI_USBCMD); 667 writel(val, op_reg_base + EHCI_USBCMD);
673 668
674 wait_time = 2000; 669 wait_time = 2000;
675 delta = 100;
676 do { 670 do {
677 writel(0x3f, op_reg_base + EHCI_USBSTS); 671 writel(0x3f, op_reg_base + EHCI_USBSTS);
678 udelay(delta); 672 udelay(100);
679 wait_time -= delta; 673 wait_time -= 100;
680 val = readl(op_reg_base + EHCI_USBSTS); 674 val = readl(op_reg_base + EHCI_USBSTS);
681 if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) { 675 if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
682 break; 676 break;
diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
index 40a0d8b03ad7..e84ca1928dbf 100644
--- a/drivers/usb/host/r8a66597-hcd.c
+++ b/drivers/usb/host/r8a66597-hcd.c
@@ -959,7 +959,7 @@ static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
959 info.pipenum = get_empty_pipenum(r8a66597, ep); 959 info.pipenum = get_empty_pipenum(r8a66597, ep);
960 info.address = get_urb_to_r8a66597_addr(r8a66597, urb); 960 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
961 info.epnum = usb_endpoint_num(ep); 961 info.epnum = usb_endpoint_num(ep);
962 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize); 962 info.maxpacket = usb_endpoint_maxp(ep);
963 info.type = get_r8a66597_type(usb_endpoint_type(ep)); 963 info.type = get_r8a66597_type(usb_endpoint_type(ep));
964 info.bufnum = get_bufnum(info.pipenum); 964 info.bufnum = get_bufnum(info.pipenum);
965 info.buf_bsize = get_buf_bsize(info.pipenum); 965 info.buf_bsize = get_buf_bsize(info.pipenum);
@@ -2519,7 +2519,7 @@ static int __devinit r8a66597_probe(struct platform_device *pdev)
2519 hcd->rsrc_start = res->start; 2519 hcd->rsrc_start = res->start;
2520 hcd->has_tt = 1; 2520 hcd->has_tt = 1;
2521 2521
2522 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger); 2522 ret = usb_add_hcd(hcd, irq, irq_trigger);
2523 if (ret != 0) { 2523 if (ret != 0) {
2524 dev_err(&pdev->dev, "Failed to add hcd\n"); 2524 dev_err(&pdev->dev, "Failed to add hcd\n");
2525 goto clean_up3; 2525 goto clean_up3;
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index 1a996245ab98..961d6638d8f9 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -1729,7 +1729,7 @@ sl811h_probe(struct platform_device *dev)
1729 * Use resource IRQ flags if set by platform device setup. 1729 * Use resource IRQ flags if set by platform device setup.
1730 */ 1730 */
1731 irqflags |= IRQF_SHARED; 1731 irqflags |= IRQF_SHARED;
1732 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | irqflags); 1732 retval = usb_add_hcd(hcd, irq, irqflags);
1733 if (retval != 0) 1733 if (retval != 0)
1734 goto err6; 1734 goto err6;
1735 1735
diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
index fba99b120588..c8ae199cfbb8 100644
--- a/drivers/usb/host/uhci-hcd.c
+++ b/drivers/usb/host/uhci-hcd.c
@@ -294,50 +294,50 @@ __acquires(uhci->lock)
294 * and that remote wakeups should be enabled. 294 * and that remote wakeups should be enabled.
295 */ 295 */
296 egsm_enable = USBCMD_EGSM; 296 egsm_enable = USBCMD_EGSM;
297 uhci->RD_enable = 1;
298 int_enable = USBINTR_RESUME; 297 int_enable = USBINTR_RESUME;
299 wakeup_enable = 1; 298 wakeup_enable = 1;
300 299
301 /* In auto-stop mode wakeups must always be detected, but 300 /*
302 * Resume-Detect interrupts may be prohibited. (In the absence 301 * In auto-stop mode, we must be able to detect new connections.
303 * of CONFIG_PM, they are always disallowed.) 302 * The user can force us to poll by disabling remote wakeup;
303 * otherwise we will use the EGSM/RD mechanism.
304 */ 304 */
305 if (auto_stop) { 305 if (auto_stop) {
306 if (!device_may_wakeup(&rhdev->dev)) 306 if (!device_may_wakeup(&rhdev->dev))
307 int_enable = 0; 307 egsm_enable = int_enable = 0;
308 }
308 309
309 /* In bus-suspend mode wakeups may be disabled, but if they are
310 * allowed then so are Resume-Detect interrupts.
311 */
312 } else {
313#ifdef CONFIG_PM 310#ifdef CONFIG_PM
311 /*
312 * In bus-suspend mode, we use the wakeup setting specified
313 * for the root hub.
314 */
315 else {
314 if (!rhdev->do_remote_wakeup) 316 if (!rhdev->do_remote_wakeup)
315 wakeup_enable = 0; 317 wakeup_enable = 0;
316#endif
317 } 318 }
319#endif
318 320
319 /* EGSM causes the root hub to echo a 'K' signal (resume) out any 321 /*
320 * port which requests a remote wakeup. According to the USB spec, 322 * UHCI doesn't distinguish between wakeup requests from downstream
321 * every hub is supposed to do this. But if we are ignoring 323 * devices and local connect/disconnect events. There's no way to
322 * remote-wakeup requests anyway then there's no point to it. 324 * enable one without the other; both are controlled by EGSM. Thus
323 * We also shouldn't enable EGSM if it's broken. 325 * if wakeups are disallowed then EGSM must be turned off -- in which
324 */ 326 * case remote wakeup requests from downstream during system sleep
325 if (!wakeup_enable || global_suspend_mode_is_broken(uhci)) 327 * will be lost.
326 egsm_enable = 0; 328 *
327 329 * In addition, if EGSM is broken then we can't use it. Likewise,
328 /* If we're ignoring wakeup events then there's no reason to 330 * if Resume-Detect interrupts are broken then we can't use them.
329 * enable Resume-Detect interrupts. We also shouldn't enable
330 * them if they are broken or disallowed.
331 * 331 *
332 * This logic may lead us to enabling RD but not EGSM. The UHCI 332 * Finally, neither EGSM nor RD is useful by itself. Without EGSM,
333 * spec foolishly says that RD works only when EGSM is on, but 333 * the RD status bit will never get set. Without RD, the controller
334 * there's no harm in enabling it anyway -- perhaps some chips 334 * won't generate interrupts to tell the system about wakeup events.
335 * will implement it!
336 */ 335 */
337 if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) || 336 if (!wakeup_enable || global_suspend_mode_is_broken(uhci) ||
338 !int_enable) 337 resume_detect_interrupts_are_broken(uhci))
339 uhci->RD_enable = int_enable = 0; 338 egsm_enable = int_enable = 0;
340 339
340 uhci->RD_enable = !!int_enable;
341 uhci_writew(uhci, int_enable, USBINTR); 341 uhci_writew(uhci, int_enable, USBINTR);
342 uhci_writew(uhci, egsm_enable | USBCMD_CF, USBCMD); 342 uhci_writew(uhci, egsm_enable | USBCMD_CF, USBCMD);
343 mb(); 343 mb();
@@ -364,10 +364,12 @@ __acquires(uhci->lock)
364 uhci->rh_state = new_state; 364 uhci->rh_state = new_state;
365 uhci->is_stopped = UHCI_IS_STOPPED; 365 uhci->is_stopped = UHCI_IS_STOPPED;
366 366
367 /* If interrupts don't work and remote wakeup is enabled then 367 /*
368 * the suspended root hub needs to be polled. 368 * If remote wakeup is enabled but either EGSM or RD interrupts
369 * doesn't work, then we won't get an interrupt when a wakeup event
370 * occurs. Thus the suspended root hub needs to be polled.
369 */ 371 */
370 if (!int_enable && wakeup_enable) 372 if (wakeup_enable && (!int_enable || !egsm_enable))
371 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags); 373 set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
372 else 374 else
373 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags); 375 clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 84ed28b34f93..f6ca80ee4cec 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -280,7 +280,7 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
280 qh->load = usb_calc_bus_time(udev->speed, 280 qh->load = usb_calc_bus_time(udev->speed,
281 usb_endpoint_dir_in(&hep->desc), 281 usb_endpoint_dir_in(&hep->desc),
282 qh->type == USB_ENDPOINT_XFER_ISOC, 282 qh->type == USB_ENDPOINT_XFER_ISOC,
283 le16_to_cpu(hep->desc.wMaxPacketSize)) 283 usb_endpoint_maxp(&hep->desc))
284 / 1000 + 1; 284 / 1000 + 1;
285 285
286 } else { /* Skeleton QH */ 286 } else { /* Skeleton QH */
@@ -792,7 +792,7 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
792{ 792{
793 struct uhci_td *td; 793 struct uhci_td *td;
794 unsigned long destination, status; 794 unsigned long destination, status;
795 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); 795 int maxsze = usb_endpoint_maxp(&qh->hep->desc);
796 int len = urb->transfer_buffer_length; 796 int len = urb->transfer_buffer_length;
797 dma_addr_t data = urb->transfer_dma; 797 dma_addr_t data = urb->transfer_dma;
798 __hc32 *plink; 798 __hc32 *plink;
@@ -918,7 +918,7 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
918{ 918{
919 struct uhci_td *td; 919 struct uhci_td *td;
920 unsigned long destination, status; 920 unsigned long destination, status;
921 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); 921 int maxsze = usb_endpoint_maxp(&qh->hep->desc);
922 int len = urb->transfer_buffer_length; 922 int len = urb->transfer_buffer_length;
923 int this_sg_len; 923 int this_sg_len;
924 dma_addr_t data; 924 dma_addr_t data;
diff --git a/drivers/usb/host/whci/debug.c b/drivers/usb/host/whci/debug.c
index 767af265e002..ba61dae9e4d2 100644
--- a/drivers/usb/host/whci/debug.c
+++ b/drivers/usb/host/whci/debug.c
@@ -19,6 +19,7 @@
19#include <linux/kernel.h> 19#include <linux/kernel.h>
20#include <linux/debugfs.h> 20#include <linux/debugfs.h>
21#include <linux/seq_file.h> 21#include <linux/seq_file.h>
22#include <linux/export.h>
22 23
23#include "../../wusbcore/wusbhc.h" 24#include "../../wusbcore/wusbhc.h"
24 25
diff --git a/drivers/usb/host/whci/hcd.c b/drivers/usb/host/whci/hcd.c
index 9546f6cd01f0..1e141f755b26 100644
--- a/drivers/usb/host/whci/hcd.c
+++ b/drivers/usb/host/whci/hcd.c
@@ -17,6 +17,7 @@
17 */ 17 */
18#include <linux/kernel.h> 18#include <linux/kernel.h>
19#include <linux/init.h> 19#include <linux/init.h>
20#include <linux/module.h>
20#include <linux/uwb/umc.h> 21#include <linux/uwb/umc.h>
21 22
22#include "../../wusbcore/wusbhc.h" 23#include "../../wusbcore/wusbhc.h"
diff --git a/drivers/usb/host/xhci-ext-caps.h b/drivers/usb/host/xhci-ext-caps.h
index ce5c9e51748e..c7f33123d4c0 100644
--- a/drivers/usb/host/xhci-ext-caps.h
+++ b/drivers/usb/host/xhci-ext-caps.h
@@ -65,6 +65,12 @@
65/* bits 1:2, 5:12, and 17:19 need to be preserved; bits 21:28 should be zero */ 65/* bits 1:2, 5:12, and 17:19 need to be preserved; bits 21:28 should be zero */
66#define XHCI_LEGACY_DISABLE_SMI ((0x3 << 1) + (0xff << 5) + (0x7 << 17)) 66#define XHCI_LEGACY_DISABLE_SMI ((0x3 << 1) + (0xff << 5) + (0x7 << 17))
67 67
68/* USB 2.0 xHCI 0.96 L1C capability - section 7.2.2.1.3.2 */
69#define XHCI_L1C (1 << 16)
70
71/* USB 2.0 xHCI 1.0 hardware LMP capability - section 7.2.2.1.3.2 */
72#define XHCI_HLC (1 << 19)
73
68/* command register values to disable interrupts and halt the HC */ 74/* command register values to disable interrupts and halt the HC */
69/* start/stop HC execution - do not write unless HC is halted*/ 75/* start/stop HC execution - do not write unless HC is halted*/
70#define XHCI_CMD_RUN (1 << 0) 76#define XHCI_CMD_RUN (1 << 0)
diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index 1e96d1f1fe6b..430e88fd3f6c 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -20,6 +20,7 @@
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */ 21 */
22 22
23#include <linux/gfp.h>
23#include <asm/unaligned.h> 24#include <asm/unaligned.h>
24 25
25#include "xhci.h" 26#include "xhci.h"
@@ -28,6 +29,25 @@
28#define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \ 29#define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
29 PORT_RC | PORT_PLC | PORT_PE) 30 PORT_RC | PORT_PLC | PORT_PE)
30 31
32/* usb 1.1 root hub device descriptor */
33static u8 usb_bos_descriptor [] = {
34 USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
35 USB_DT_BOS, /* __u8 bDescriptorType */
36 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
37 0x1, /* __u8 bNumDeviceCaps */
38 /* First device capability */
39 USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
40 USB_DT_DEVICE_CAPABILITY, /* Device Capability */
41 USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
42 0x00, /* bmAttributes, LTM off by default */
43 USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
44 0x03, /* bFunctionalitySupport,
45 USB 3.0 speed only */
46 0x00, /* bU1DevExitLat, set later. */
47 0x00, 0x00 /* __le16 bU2DevExitLat, set later. */
48};
49
50
31static void xhci_common_hub_descriptor(struct xhci_hcd *xhci, 51static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
32 struct usb_hub_descriptor *desc, int ports) 52 struct usb_hub_descriptor *desc, int ports)
33{ 53{
@@ -232,7 +252,7 @@ int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
232 continue; 252 continue;
233 speed = xhci->devs[i]->udev->speed; 253 speed = xhci->devs[i]->udev->speed;
234 if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3)) 254 if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
235 && xhci->devs[i]->port == port) { 255 && xhci->devs[i]->fake_port == port) {
236 slot_id = i; 256 slot_id = i;
237 break; 257 break;
238 } 258 }
@@ -392,13 +412,39 @@ static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
392 return max_ports; 412 return max_ports;
393} 413}
394 414
415void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
416 int port_id, u32 link_state)
417{
418 u32 temp;
419
420 temp = xhci_readl(xhci, port_array[port_id]);
421 temp = xhci_port_state_to_neutral(temp);
422 temp &= ~PORT_PLS_MASK;
423 temp |= PORT_LINK_STROBE | link_state;
424 xhci_writel(xhci, temp, port_array[port_id]);
425}
426
427/* Test and clear port RWC bit */
428void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
429 int port_id, u32 port_bit)
430{
431 u32 temp;
432
433 temp = xhci_readl(xhci, port_array[port_id]);
434 if (temp & port_bit) {
435 temp = xhci_port_state_to_neutral(temp);
436 temp |= port_bit;
437 xhci_writel(xhci, temp, port_array[port_id]);
438 }
439}
440
395int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 441int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
396 u16 wIndex, char *buf, u16 wLength) 442 u16 wIndex, char *buf, u16 wLength)
397{ 443{
398 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 444 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
399 int max_ports; 445 int max_ports;
400 unsigned long flags; 446 unsigned long flags;
401 u32 temp, temp1, status; 447 u32 temp, status;
402 int retval = 0; 448 int retval = 0;
403 __le32 __iomem **port_array; 449 __le32 __iomem **port_array;
404 int slot_id; 450 int slot_id;
@@ -429,6 +475,21 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
429 xhci_hub_descriptor(hcd, xhci, 475 xhci_hub_descriptor(hcd, xhci,
430 (struct usb_hub_descriptor *) buf); 476 (struct usb_hub_descriptor *) buf);
431 break; 477 break;
478 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
479 if ((wValue & 0xff00) != (USB_DT_BOS << 8))
480 goto error;
481
482 if (hcd->speed != HCD_USB3)
483 goto error;
484
485 memcpy(buf, &usb_bos_descriptor,
486 USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
487 temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
488 buf[12] = HCS_U1_LATENCY(temp);
489 put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
490
491 spin_unlock_irqrestore(&xhci->lock, flags);
492 return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
432 case GetPortStatus: 493 case GetPortStatus:
433 if (!wIndex || wIndex > max_ports) 494 if (!wIndex || wIndex > max_ports)
434 goto error; 495 goto error;
@@ -472,11 +533,8 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
472 xhci_dbg(xhci, "Resume USB2 port %d\n", 533 xhci_dbg(xhci, "Resume USB2 port %d\n",
473 wIndex + 1); 534 wIndex + 1);
474 bus_state->resume_done[wIndex] = 0; 535 bus_state->resume_done[wIndex] = 0;
475 temp1 = xhci_port_state_to_neutral(temp); 536 xhci_set_link_state(xhci, port_array, wIndex,
476 temp1 &= ~PORT_PLS_MASK; 537 XDEV_U0);
477 temp1 |= PORT_LINK_STROBE | XDEV_U0;
478 xhci_writel(xhci, temp1, port_array[wIndex]);
479
480 xhci_dbg(xhci, "set port %d resume\n", 538 xhci_dbg(xhci, "set port %d resume\n",
481 wIndex + 1); 539 wIndex + 1);
482 slot_id = xhci_find_slot_id_by_port(hcd, xhci, 540 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
@@ -551,10 +609,19 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
551 switch (wValue) { 609 switch (wValue) {
552 case USB_PORT_FEAT_SUSPEND: 610 case USB_PORT_FEAT_SUSPEND:
553 temp = xhci_readl(xhci, port_array[wIndex]); 611 temp = xhci_readl(xhci, port_array[wIndex]);
612 if ((temp & PORT_PLS_MASK) != XDEV_U0) {
613 /* Resume the port to U0 first */
614 xhci_set_link_state(xhci, port_array, wIndex,
615 XDEV_U0);
616 spin_unlock_irqrestore(&xhci->lock, flags);
617 msleep(10);
618 spin_lock_irqsave(&xhci->lock, flags);
619 }
554 /* In spec software should not attempt to suspend 620 /* In spec software should not attempt to suspend
555 * a port unless the port reports that it is in the 621 * a port unless the port reports that it is in the
556 * enabled (PED = ‘1’,PLS < ‘3’) state. 622 * enabled (PED = ‘1’,PLS < ‘3’) state.
557 */ 623 */
624 temp = xhci_readl(xhci, port_array[wIndex]);
558 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) 625 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
559 || (temp & PORT_PLS_MASK) >= XDEV_U3) { 626 || (temp & PORT_PLS_MASK) >= XDEV_U3) {
560 xhci_warn(xhci, "USB core suspending device " 627 xhci_warn(xhci, "USB core suspending device "
@@ -573,10 +640,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
573 xhci_stop_device(xhci, slot_id, 1); 640 xhci_stop_device(xhci, slot_id, 1);
574 spin_lock_irqsave(&xhci->lock, flags); 641 spin_lock_irqsave(&xhci->lock, flags);
575 642
576 temp = xhci_port_state_to_neutral(temp); 643 xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
577 temp &= ~PORT_PLS_MASK;
578 temp |= PORT_LINK_STROBE | XDEV_U3;
579 xhci_writel(xhci, temp, port_array[wIndex]);
580 644
581 spin_unlock_irqrestore(&xhci->lock, flags); 645 spin_unlock_irqrestore(&xhci->lock, flags);
582 msleep(10); /* wait device to enter */ 646 msleep(10); /* wait device to enter */
@@ -610,10 +674,8 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
610 } 674 }
611 } 675 }
612 676
613 temp = xhci_port_state_to_neutral(temp); 677 xhci_set_link_state(xhci, port_array, wIndex,
614 temp &= ~PORT_PLS_MASK; 678 link_state);
615 temp |= PORT_LINK_STROBE | link_state;
616 xhci_writel(xhci, temp, port_array[wIndex]);
617 679
618 spin_unlock_irqrestore(&xhci->lock, flags); 680 spin_unlock_irqrestore(&xhci->lock, flags);
619 msleep(20); /* wait device to enter */ 681 msleep(20); /* wait device to enter */
@@ -677,24 +739,13 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
677 if ((temp & PORT_PE) == 0) 739 if ((temp & PORT_PE) == 0)
678 goto error; 740 goto error;
679 741
680 temp = xhci_port_state_to_neutral(temp); 742 xhci_set_link_state(xhci, port_array, wIndex,
681 temp &= ~PORT_PLS_MASK; 743 XDEV_RESUME);
682 temp |= PORT_LINK_STROBE | XDEV_RESUME; 744 spin_unlock_irqrestore(&xhci->lock, flags);
683 xhci_writel(xhci, temp,
684 port_array[wIndex]);
685
686 spin_unlock_irqrestore(&xhci->lock,
687 flags);
688 msleep(20); 745 msleep(20);
689 spin_lock_irqsave(&xhci->lock, flags); 746 spin_lock_irqsave(&xhci->lock, flags);
690 747 xhci_set_link_state(xhci, port_array, wIndex,
691 temp = xhci_readl(xhci, 748 XDEV_U0);
692 port_array[wIndex]);
693 temp = xhci_port_state_to_neutral(temp);
694 temp &= ~PORT_PLS_MASK;
695 temp |= PORT_LINK_STROBE | XDEV_U0;
696 xhci_writel(xhci, temp,
697 port_array[wIndex]);
698 } 749 }
699 bus_state->port_c_suspend |= 1 << wIndex; 750 bus_state->port_c_suspend |= 1 << wIndex;
700 751
@@ -761,7 +812,7 @@ int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
761 memset(buf, 0, retval); 812 memset(buf, 0, retval);
762 status = 0; 813 status = 0;
763 814
764 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC; 815 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
765 816
766 spin_lock_irqsave(&xhci->lock, flags); 817 spin_lock_irqsave(&xhci->lock, flags);
767 /* For each port, did anything change? If so, set that bit in buf. */ 818 /* For each port, did anything change? If so, set that bit in buf. */
@@ -910,25 +961,18 @@ int xhci_bus_resume(struct usb_hcd *hcd)
910 if (test_bit(port_index, &bus_state->bus_suspended) && 961 if (test_bit(port_index, &bus_state->bus_suspended) &&
911 (temp & PORT_PLS_MASK)) { 962 (temp & PORT_PLS_MASK)) {
912 if (DEV_SUPERSPEED(temp)) { 963 if (DEV_SUPERSPEED(temp)) {
913 temp = xhci_port_state_to_neutral(temp); 964 xhci_set_link_state(xhci, port_array,
914 temp &= ~PORT_PLS_MASK; 965 port_index, XDEV_U0);
915 temp |= PORT_LINK_STROBE | XDEV_U0;
916 xhci_writel(xhci, temp, port_array[port_index]);
917 } else { 966 } else {
918 temp = xhci_port_state_to_neutral(temp); 967 xhci_set_link_state(xhci, port_array,
919 temp &= ~PORT_PLS_MASK; 968 port_index, XDEV_RESUME);
920 temp |= PORT_LINK_STROBE | XDEV_RESUME;
921 xhci_writel(xhci, temp, port_array[port_index]);
922 969
923 spin_unlock_irqrestore(&xhci->lock, flags); 970 spin_unlock_irqrestore(&xhci->lock, flags);
924 msleep(20); 971 msleep(20);
925 spin_lock_irqsave(&xhci->lock, flags); 972 spin_lock_irqsave(&xhci->lock, flags);
926 973
927 temp = xhci_readl(xhci, port_array[port_index]); 974 xhci_set_link_state(xhci, port_array,
928 temp = xhci_port_state_to_neutral(temp); 975 port_index, XDEV_U0);
929 temp &= ~PORT_PLS_MASK;
930 temp |= PORT_LINK_STROBE | XDEV_U0;
931 xhci_writel(xhci, temp, port_array[port_index]);
932 } 976 }
933 /* wait for the port to enter U0 and report port link 977 /* wait for the port to enter U0 and report port link
934 * state change. 978 * state change.
@@ -938,12 +982,8 @@ int xhci_bus_resume(struct usb_hcd *hcd)
938 spin_lock_irqsave(&xhci->lock, flags); 982 spin_lock_irqsave(&xhci->lock, flags);
939 983
940 /* Clear PLC */ 984 /* Clear PLC */
941 temp = xhci_readl(xhci, port_array[port_index]); 985 xhci_test_and_clear_bit(xhci, port_array, port_index,
942 if (temp & PORT_PLC) { 986 PORT_PLC);
943 temp = xhci_port_state_to_neutral(temp);
944 temp |= PORT_PLC;
945 xhci_writel(xhci, temp, port_array[port_index]);
946 }
947 987
948 slot_id = xhci_find_slot_id_by_port(hcd, 988 slot_id = xhci_find_slot_id_by_port(hcd,
949 xhci, port_index + 1); 989 xhci, port_index + 1);
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index d446886b22b0..0e4b25fa3bcd 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -61,8 +61,6 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flag
61 61
62static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg) 62static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
63{ 63{
64 if (!seg)
65 return;
66 if (seg->trbs) { 64 if (seg->trbs) {
67 xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n", 65 xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
68 seg->trbs, (unsigned long long)seg->dma); 66 seg->trbs, (unsigned long long)seg->dma);
@@ -81,7 +79,7 @@ static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
81 * related flags, such as End TRB, Toggle Cycle, and no snoop. 79 * related flags, such as End TRB, Toggle Cycle, and no snoop.
82 */ 80 */
83static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev, 81static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
84 struct xhci_segment *next, bool link_trbs) 82 struct xhci_segment *next, bool link_trbs, bool isoc)
85{ 83{
86 u32 val; 84 u32 val;
87 85
@@ -97,7 +95,9 @@ static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
97 val &= ~TRB_TYPE_BITMASK; 95 val &= ~TRB_TYPE_BITMASK;
98 val |= TRB_TYPE(TRB_LINK); 96 val |= TRB_TYPE(TRB_LINK);
99 /* Always set the chain bit with 0.95 hardware */ 97 /* Always set the chain bit with 0.95 hardware */
100 if (xhci_link_trb_quirk(xhci)) 98 /* Set chain bit for isoc rings on AMD 0.96 host */
99 if (xhci_link_trb_quirk(xhci) ||
100 (isoc && (xhci->quirks & XHCI_AMD_0x96_HOST)))
101 val |= TRB_CHAIN; 101 val |= TRB_CHAIN;
102 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val); 102 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
103 } 103 }
@@ -112,18 +112,20 @@ void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
112 struct xhci_segment *seg; 112 struct xhci_segment *seg;
113 struct xhci_segment *first_seg; 113 struct xhci_segment *first_seg;
114 114
115 if (!ring || !ring->first_seg) 115 if (!ring)
116 return; 116 return;
117 first_seg = ring->first_seg; 117 if (ring->first_seg) {
118 seg = first_seg->next; 118 first_seg = ring->first_seg;
119 xhci_dbg(xhci, "Freeing ring at %p\n", ring); 119 seg = first_seg->next;
120 while (seg != first_seg) { 120 xhci_dbg(xhci, "Freeing ring at %p\n", ring);
121 struct xhci_segment *next = seg->next; 121 while (seg != first_seg) {
122 xhci_segment_free(xhci, seg); 122 struct xhci_segment *next = seg->next;
123 seg = next; 123 xhci_segment_free(xhci, seg);
124 seg = next;
125 }
126 xhci_segment_free(xhci, first_seg);
127 ring->first_seg = NULL;
124 } 128 }
125 xhci_segment_free(xhci, first_seg);
126 ring->first_seg = NULL;
127 kfree(ring); 129 kfree(ring);
128} 130}
129 131
@@ -152,7 +154,7 @@ static void xhci_initialize_ring_info(struct xhci_ring *ring)
152 * See section 4.9.1 and figures 15 and 16. 154 * See section 4.9.1 and figures 15 and 16.
153 */ 155 */
154static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci, 156static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
155 unsigned int num_segs, bool link_trbs, gfp_t flags) 157 unsigned int num_segs, bool link_trbs, bool isoc, gfp_t flags)
156{ 158{
157 struct xhci_ring *ring; 159 struct xhci_ring *ring;
158 struct xhci_segment *prev; 160 struct xhci_segment *prev;
@@ -178,12 +180,12 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
178 next = xhci_segment_alloc(xhci, flags); 180 next = xhci_segment_alloc(xhci, flags);
179 if (!next) 181 if (!next)
180 goto fail; 182 goto fail;
181 xhci_link_segments(xhci, prev, next, link_trbs); 183 xhci_link_segments(xhci, prev, next, link_trbs, isoc);
182 184
183 prev = next; 185 prev = next;
184 num_segs--; 186 num_segs--;
185 } 187 }
186 xhci_link_segments(xhci, prev, ring->first_seg, link_trbs); 188 xhci_link_segments(xhci, prev, ring->first_seg, link_trbs, isoc);
187 189
188 if (link_trbs) { 190 if (link_trbs) {
189 /* See section 4.9.2.1 and 6.4.4.1 */ 191 /* See section 4.9.2.1 and 6.4.4.1 */
@@ -229,14 +231,14 @@ void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
229 * pointers to the beginning of the ring. 231 * pointers to the beginning of the ring.
230 */ 232 */
231static void xhci_reinit_cached_ring(struct xhci_hcd *xhci, 233static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
232 struct xhci_ring *ring) 234 struct xhci_ring *ring, bool isoc)
233{ 235{
234 struct xhci_segment *seg = ring->first_seg; 236 struct xhci_segment *seg = ring->first_seg;
235 do { 237 do {
236 memset(seg->trbs, 0, 238 memset(seg->trbs, 0,
237 sizeof(union xhci_trb)*TRBS_PER_SEGMENT); 239 sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
238 /* All endpoint rings have link TRBs */ 240 /* All endpoint rings have link TRBs */
239 xhci_link_segments(xhci, seg, seg->next, 1); 241 xhci_link_segments(xhci, seg, seg->next, 1, isoc);
240 seg = seg->next; 242 seg = seg->next;
241 } while (seg != ring->first_seg); 243 } while (seg != ring->first_seg);
242 xhci_initialize_ring_info(ring); 244 xhci_initialize_ring_info(ring);
@@ -315,7 +317,7 @@ static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
315 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 317 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
316 318
317 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE) 319 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
318 pci_free_consistent(pdev, 320 dma_free_coherent(&pdev->dev,
319 sizeof(struct xhci_stream_ctx)*num_stream_ctxs, 321 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
320 stream_ctx, dma); 322 stream_ctx, dma);
321 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE) 323 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
@@ -343,9 +345,9 @@ static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
343 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 345 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
344 346
345 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE) 347 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
346 return pci_alloc_consistent(pdev, 348 return dma_alloc_coherent(&pdev->dev,
347 sizeof(struct xhci_stream_ctx)*num_stream_ctxs, 349 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
348 dma); 350 dma, mem_flags);
349 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE) 351 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
350 return dma_pool_alloc(xhci->small_streams_pool, 352 return dma_pool_alloc(xhci->small_streams_pool,
351 mem_flags, dma); 353 mem_flags, dma);
@@ -540,7 +542,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
540 */ 542 */
541 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 543 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
542 stream_info->stream_rings[cur_stream] = 544 stream_info->stream_rings[cur_stream] =
543 xhci_ring_alloc(xhci, 1, true, mem_flags); 545 xhci_ring_alloc(xhci, 1, true, false, mem_flags);
544 cur_ring = stream_info->stream_rings[cur_stream]; 546 cur_ring = stream_info->stream_rings[cur_stream];
545 if (!cur_ring) 547 if (!cur_ring)
546 goto cleanup_rings; 548 goto cleanup_rings;
@@ -687,11 +689,103 @@ static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
687 ep->xhci = xhci; 689 ep->xhci = xhci;
688} 690}
689 691
690/* All the xhci_tds in the ring's TD list should be freed at this point */ 692static void xhci_free_tt_info(struct xhci_hcd *xhci,
693 struct xhci_virt_device *virt_dev,
694 int slot_id)
695{
696 struct list_head *tt;
697 struct list_head *tt_list_head;
698 struct list_head *tt_next;
699 struct xhci_tt_bw_info *tt_info;
700
701 /* If the device never made it past the Set Address stage,
702 * it may not have the real_port set correctly.
703 */
704 if (virt_dev->real_port == 0 ||
705 virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
706 xhci_dbg(xhci, "Bad real port.\n");
707 return;
708 }
709
710 tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts);
711 if (list_empty(tt_list_head))
712 return;
713
714 list_for_each(tt, tt_list_head) {
715 tt_info = list_entry(tt, struct xhci_tt_bw_info, tt_list);
716 if (tt_info->slot_id == slot_id)
717 break;
718 }
719 /* Cautionary measure in case the hub was disconnected before we
720 * stored the TT information.
721 */
722 if (tt_info->slot_id != slot_id)
723 return;
724
725 tt_next = tt->next;
726 tt_info = list_entry(tt, struct xhci_tt_bw_info,
727 tt_list);
728 /* Multi-TT hubs will have more than one entry */
729 do {
730 list_del(tt);
731 kfree(tt_info);
732 tt = tt_next;
733 if (list_empty(tt_list_head))
734 break;
735 tt_next = tt->next;
736 tt_info = list_entry(tt, struct xhci_tt_bw_info,
737 tt_list);
738 } while (tt_info->slot_id == slot_id);
739}
740
741int xhci_alloc_tt_info(struct xhci_hcd *xhci,
742 struct xhci_virt_device *virt_dev,
743 struct usb_device *hdev,
744 struct usb_tt *tt, gfp_t mem_flags)
745{
746 struct xhci_tt_bw_info *tt_info;
747 unsigned int num_ports;
748 int i, j;
749
750 if (!tt->multi)
751 num_ports = 1;
752 else
753 num_ports = hdev->maxchild;
754
755 for (i = 0; i < num_ports; i++, tt_info++) {
756 struct xhci_interval_bw_table *bw_table;
757
758 tt_info = kzalloc(sizeof(*tt_info), mem_flags);
759 if (!tt_info)
760 goto free_tts;
761 INIT_LIST_HEAD(&tt_info->tt_list);
762 list_add(&tt_info->tt_list,
763 &xhci->rh_bw[virt_dev->real_port - 1].tts);
764 tt_info->slot_id = virt_dev->udev->slot_id;
765 if (tt->multi)
766 tt_info->ttport = i+1;
767 bw_table = &tt_info->bw_table;
768 for (j = 0; j < XHCI_MAX_INTERVAL; j++)
769 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
770 }
771 return 0;
772
773free_tts:
774 xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
775 return -ENOMEM;
776}
777
778
779/* All the xhci_tds in the ring's TD list should be freed at this point.
780 * Should be called with xhci->lock held if there is any chance the TT lists
781 * will be manipulated by the configure endpoint, allocate device, or update
782 * hub functions while this function is removing the TT entries from the list.
783 */
691void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) 784void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
692{ 785{
693 struct xhci_virt_device *dev; 786 struct xhci_virt_device *dev;
694 int i; 787 int i;
788 int old_active_eps = 0;
695 789
696 /* Slot ID 0 is reserved */ 790 /* Slot ID 0 is reserved */
697 if (slot_id == 0 || !xhci->devs[slot_id]) 791 if (slot_id == 0 || !xhci->devs[slot_id])
@@ -702,13 +796,29 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
702 if (!dev) 796 if (!dev)
703 return; 797 return;
704 798
799 if (dev->tt_info)
800 old_active_eps = dev->tt_info->active_eps;
801
705 for (i = 0; i < 31; ++i) { 802 for (i = 0; i < 31; ++i) {
706 if (dev->eps[i].ring) 803 if (dev->eps[i].ring)
707 xhci_ring_free(xhci, dev->eps[i].ring); 804 xhci_ring_free(xhci, dev->eps[i].ring);
708 if (dev->eps[i].stream_info) 805 if (dev->eps[i].stream_info)
709 xhci_free_stream_info(xhci, 806 xhci_free_stream_info(xhci,
710 dev->eps[i].stream_info); 807 dev->eps[i].stream_info);
808 /* Endpoints on the TT/root port lists should have been removed
809 * when usb_disable_device() was called for the device.
810 * We can't drop them anyway, because the udev might have gone
811 * away by this point, and we can't tell what speed it was.
812 */
813 if (!list_empty(&dev->eps[i].bw_endpoint_list))
814 xhci_warn(xhci, "Slot %u endpoint %u "
815 "not removed from BW list!\n",
816 slot_id, i);
711 } 817 }
818 /* If this is a hub, free the TT(s) from the TT list */
819 xhci_free_tt_info(xhci, dev, slot_id);
820 /* If necessary, update the number of active TTs on this root port */
821 xhci_update_tt_active_eps(xhci, dev, old_active_eps);
712 822
713 if (dev->ring_cache) { 823 if (dev->ring_cache) {
714 for (i = 0; i < dev->num_rings_cached; i++) 824 for (i = 0; i < dev->num_rings_cached; i++)
@@ -762,10 +872,11 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
762 for (i = 0; i < 31; i++) { 872 for (i = 0; i < 31; i++) {
763 xhci_init_endpoint_timer(xhci, &dev->eps[i]); 873 xhci_init_endpoint_timer(xhci, &dev->eps[i]);
764 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list); 874 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
875 INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
765 } 876 }
766 877
767 /* Allocate endpoint 0 ring */ 878 /* Allocate endpoint 0 ring */
768 dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags); 879 dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, false, flags);
769 if (!dev->eps[0].ring) 880 if (!dev->eps[0].ring)
770 goto fail; 881 goto fail;
771 882
@@ -871,7 +982,6 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
871 struct xhci_virt_device *dev; 982 struct xhci_virt_device *dev;
872 struct xhci_ep_ctx *ep0_ctx; 983 struct xhci_ep_ctx *ep0_ctx;
873 struct xhci_slot_ctx *slot_ctx; 984 struct xhci_slot_ctx *slot_ctx;
874 struct xhci_input_control_ctx *ctrl_ctx;
875 u32 port_num; 985 u32 port_num;
876 struct usb_device *top_dev; 986 struct usb_device *top_dev;
877 987
@@ -883,12 +993,8 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
883 return -EINVAL; 993 return -EINVAL;
884 } 994 }
885 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0); 995 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
886 ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
887 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx); 996 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
888 997
889 /* 2) New slot context and endpoint 0 context are valid*/
890 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
891
892 /* 3) Only the control endpoint is valid - one endpoint context */ 998 /* 3) Only the control endpoint is valid - one endpoint context */
893 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route); 999 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
894 switch (udev->speed) { 1000 switch (udev->speed) {
@@ -921,9 +1027,40 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
921 for (top_dev = udev; top_dev->parent && top_dev->parent->parent; 1027 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
922 top_dev = top_dev->parent) 1028 top_dev = top_dev->parent)
923 /* Found device below root hub */; 1029 /* Found device below root hub */;
924 dev->port = top_dev->portnum; 1030 dev->fake_port = top_dev->portnum;
1031 dev->real_port = port_num;
925 xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num); 1032 xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num);
926 xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->port); 1033 xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port);
1034
1035 /* Find the right bandwidth table that this device will be a part of.
1036 * If this is a full speed device attached directly to a root port (or a
1037 * decendent of one), it counts as a primary bandwidth domain, not a
1038 * secondary bandwidth domain under a TT. An xhci_tt_info structure
1039 * will never be created for the HS root hub.
1040 */
1041 if (!udev->tt || !udev->tt->hub->parent) {
1042 dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table;
1043 } else {
1044 struct xhci_root_port_bw_info *rh_bw;
1045 struct xhci_tt_bw_info *tt_bw;
1046
1047 rh_bw = &xhci->rh_bw[port_num - 1];
1048 /* Find the right TT. */
1049 list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
1050 if (tt_bw->slot_id != udev->tt->hub->slot_id)
1051 continue;
1052
1053 if (!dev->udev->tt->multi ||
1054 (udev->tt->multi &&
1055 tt_bw->ttport == dev->udev->ttport)) {
1056 dev->bw_table = &tt_bw->bw_table;
1057 dev->tt_info = tt_bw;
1058 break;
1059 }
1060 }
1061 if (!dev->tt_info)
1062 xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
1063 }
927 1064
928 /* Is this a LS/FS device under an external HS hub? */ 1065 /* Is this a LS/FS device under an external HS hub? */
929 if (udev->tt && udev->tt->hub->parent) { 1066 if (udev->tt && udev->tt->hub->parent) {
@@ -1141,8 +1278,8 @@ static u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
1141 if (udev->speed == USB_SPEED_SUPER) 1278 if (udev->speed == USB_SPEED_SUPER)
1142 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); 1279 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1143 1280
1144 max_packet = GET_MAX_PACKET(le16_to_cpu(ep->desc.wMaxPacketSize)); 1281 max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&ep->desc));
1145 max_burst = (le16_to_cpu(ep->desc.wMaxPacketSize) & 0x1800) >> 11; 1282 max_burst = (usb_endpoint_maxp(&ep->desc) & 0x1800) >> 11;
1146 /* A 0 in max burst means 1 transfer per ESIT */ 1283 /* A 0 in max burst means 1 transfer per ESIT */
1147 return max_packet * (max_burst + 1); 1284 return max_packet * (max_burst + 1);
1148} 1285}
@@ -1175,10 +1312,10 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1175 */ 1312 */
1176 if (usb_endpoint_xfer_isoc(&ep->desc)) 1313 if (usb_endpoint_xfer_isoc(&ep->desc))
1177 virt_dev->eps[ep_index].new_ring = 1314 virt_dev->eps[ep_index].new_ring =
1178 xhci_ring_alloc(xhci, 8, true, mem_flags); 1315 xhci_ring_alloc(xhci, 8, true, true, mem_flags);
1179 else 1316 else
1180 virt_dev->eps[ep_index].new_ring = 1317 virt_dev->eps[ep_index].new_ring =
1181 xhci_ring_alloc(xhci, 1, true, mem_flags); 1318 xhci_ring_alloc(xhci, 1, true, false, mem_flags);
1182 if (!virt_dev->eps[ep_index].new_ring) { 1319 if (!virt_dev->eps[ep_index].new_ring) {
1183 /* Attempt to use the ring cache */ 1320 /* Attempt to use the ring cache */
1184 if (virt_dev->num_rings_cached == 0) 1321 if (virt_dev->num_rings_cached == 0)
@@ -1187,7 +1324,8 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1187 virt_dev->ring_cache[virt_dev->num_rings_cached]; 1324 virt_dev->ring_cache[virt_dev->num_rings_cached];
1188 virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL; 1325 virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
1189 virt_dev->num_rings_cached--; 1326 virt_dev->num_rings_cached--;
1190 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring); 1327 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring,
1328 usb_endpoint_xfer_isoc(&ep->desc) ? true : false);
1191 } 1329 }
1192 virt_dev->eps[ep_index].skip = false; 1330 virt_dev->eps[ep_index].skip = false;
1193 ep_ring = virt_dev->eps[ep_index].new_ring; 1331 ep_ring = virt_dev->eps[ep_index].new_ring;
@@ -1211,7 +1349,7 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1211 /* Set the max packet size and max burst */ 1349 /* Set the max packet size and max burst */
1212 switch (udev->speed) { 1350 switch (udev->speed) {
1213 case USB_SPEED_SUPER: 1351 case USB_SPEED_SUPER:
1214 max_packet = le16_to_cpu(ep->desc.wMaxPacketSize); 1352 max_packet = usb_endpoint_maxp(&ep->desc);
1215 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet)); 1353 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet));
1216 /* dig out max burst from ep companion desc */ 1354 /* dig out max burst from ep companion desc */
1217 max_packet = ep->ss_ep_comp.bMaxBurst; 1355 max_packet = ep->ss_ep_comp.bMaxBurst;
@@ -1223,14 +1361,14 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
1223 */ 1361 */
1224 if (usb_endpoint_xfer_isoc(&ep->desc) || 1362 if (usb_endpoint_xfer_isoc(&ep->desc) ||
1225 usb_endpoint_xfer_int(&ep->desc)) { 1363 usb_endpoint_xfer_int(&ep->desc)) {
1226 max_burst = (le16_to_cpu(ep->desc.wMaxPacketSize) 1364 max_burst = (usb_endpoint_maxp(&ep->desc)
1227 & 0x1800) >> 11; 1365 & 0x1800) >> 11;
1228 ep_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(max_burst)); 1366 ep_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(max_burst));
1229 } 1367 }
1230 /* Fall through */ 1368 /* Fall through */
1231 case USB_SPEED_FULL: 1369 case USB_SPEED_FULL:
1232 case USB_SPEED_LOW: 1370 case USB_SPEED_LOW:
1233 max_packet = GET_MAX_PACKET(le16_to_cpu(ep->desc.wMaxPacketSize)); 1371 max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&ep->desc));
1234 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet)); 1372 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet));
1235 break; 1373 break;
1236 default: 1374 default:
@@ -1286,6 +1424,70 @@ void xhci_endpoint_zero(struct xhci_hcd *xhci,
1286 */ 1424 */
1287} 1425}
1288 1426
1427void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
1428{
1429 bw_info->ep_interval = 0;
1430 bw_info->mult = 0;
1431 bw_info->num_packets = 0;
1432 bw_info->max_packet_size = 0;
1433 bw_info->type = 0;
1434 bw_info->max_esit_payload = 0;
1435}
1436
1437void xhci_update_bw_info(struct xhci_hcd *xhci,
1438 struct xhci_container_ctx *in_ctx,
1439 struct xhci_input_control_ctx *ctrl_ctx,
1440 struct xhci_virt_device *virt_dev)
1441{
1442 struct xhci_bw_info *bw_info;
1443 struct xhci_ep_ctx *ep_ctx;
1444 unsigned int ep_type;
1445 int i;
1446
1447 for (i = 1; i < 31; ++i) {
1448 bw_info = &virt_dev->eps[i].bw_info;
1449
1450 /* We can't tell what endpoint type is being dropped, but
1451 * unconditionally clearing the bandwidth info for non-periodic
1452 * endpoints should be harmless because the info will never be
1453 * set in the first place.
1454 */
1455 if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
1456 /* Dropped endpoint */
1457 xhci_clear_endpoint_bw_info(bw_info);
1458 continue;
1459 }
1460
1461 if (EP_IS_ADDED(ctrl_ctx, i)) {
1462 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
1463 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
1464
1465 /* Ignore non-periodic endpoints */
1466 if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
1467 ep_type != ISOC_IN_EP &&
1468 ep_type != INT_IN_EP)
1469 continue;
1470
1471 /* Added or changed endpoint */
1472 bw_info->ep_interval = CTX_TO_EP_INTERVAL(
1473 le32_to_cpu(ep_ctx->ep_info));
1474 /* Number of packets and mult are zero-based in the
1475 * input context, but we want one-based for the
1476 * interval table.
1477 */
1478 bw_info->mult = CTX_TO_EP_MULT(
1479 le32_to_cpu(ep_ctx->ep_info)) + 1;
1480 bw_info->num_packets = CTX_TO_MAX_BURST(
1481 le32_to_cpu(ep_ctx->ep_info2)) + 1;
1482 bw_info->max_packet_size = MAX_PACKET_DECODED(
1483 le32_to_cpu(ep_ctx->ep_info2));
1484 bw_info->type = ep_type;
1485 bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
1486 le32_to_cpu(ep_ctx->tx_info));
1487 }
1488 }
1489}
1490
1289/* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy. 1491/* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
1290 * Useful when you want to change one particular aspect of the endpoint and then 1492 * Useful when you want to change one particular aspect of the endpoint and then
1291 * issue a configure endpoint command. 1493 * issue a configure endpoint command.
@@ -1344,10 +1546,9 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1344 if (!xhci->scratchpad) 1546 if (!xhci->scratchpad)
1345 goto fail_sp; 1547 goto fail_sp;
1346 1548
1347 xhci->scratchpad->sp_array = 1549 xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
1348 pci_alloc_consistent(to_pci_dev(dev),
1349 num_sp * sizeof(u64), 1550 num_sp * sizeof(u64),
1350 &xhci->scratchpad->sp_dma); 1551 &xhci->scratchpad->sp_dma, flags);
1351 if (!xhci->scratchpad->sp_array) 1552 if (!xhci->scratchpad->sp_array)
1352 goto fail_sp2; 1553 goto fail_sp2;
1353 1554
@@ -1364,8 +1565,8 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1364 xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma); 1565 xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
1365 for (i = 0; i < num_sp; i++) { 1566 for (i = 0; i < num_sp; i++) {
1366 dma_addr_t dma; 1567 dma_addr_t dma;
1367 void *buf = pci_alloc_consistent(to_pci_dev(dev), 1568 void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma,
1368 xhci->page_size, &dma); 1569 flags);
1369 if (!buf) 1570 if (!buf)
1370 goto fail_sp5; 1571 goto fail_sp5;
1371 1572
@@ -1378,7 +1579,7 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1378 1579
1379 fail_sp5: 1580 fail_sp5:
1380 for (i = i - 1; i >= 0; i--) { 1581 for (i = i - 1; i >= 0; i--) {
1381 pci_free_consistent(to_pci_dev(dev), xhci->page_size, 1582 dma_free_coherent(dev, xhci->page_size,
1382 xhci->scratchpad->sp_buffers[i], 1583 xhci->scratchpad->sp_buffers[i],
1383 xhci->scratchpad->sp_dma_buffers[i]); 1584 xhci->scratchpad->sp_dma_buffers[i]);
1384 } 1585 }
@@ -1388,7 +1589,7 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1388 kfree(xhci->scratchpad->sp_buffers); 1589 kfree(xhci->scratchpad->sp_buffers);
1389 1590
1390 fail_sp3: 1591 fail_sp3:
1391 pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64), 1592 dma_free_coherent(dev, num_sp * sizeof(u64),
1392 xhci->scratchpad->sp_array, 1593 xhci->scratchpad->sp_array,
1393 xhci->scratchpad->sp_dma); 1594 xhci->scratchpad->sp_dma);
1394 1595
@@ -1412,13 +1613,13 @@ static void scratchpad_free(struct xhci_hcd *xhci)
1412 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); 1613 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
1413 1614
1414 for (i = 0; i < num_sp; i++) { 1615 for (i = 0; i < num_sp; i++) {
1415 pci_free_consistent(pdev, xhci->page_size, 1616 dma_free_coherent(&pdev->dev, xhci->page_size,
1416 xhci->scratchpad->sp_buffers[i], 1617 xhci->scratchpad->sp_buffers[i],
1417 xhci->scratchpad->sp_dma_buffers[i]); 1618 xhci->scratchpad->sp_dma_buffers[i]);
1418 } 1619 }
1419 kfree(xhci->scratchpad->sp_dma_buffers); 1620 kfree(xhci->scratchpad->sp_dma_buffers);
1420 kfree(xhci->scratchpad->sp_buffers); 1621 kfree(xhci->scratchpad->sp_buffers);
1421 pci_free_consistent(pdev, num_sp * sizeof(u64), 1622 dma_free_coherent(&pdev->dev, num_sp * sizeof(u64),
1422 xhci->scratchpad->sp_array, 1623 xhci->scratchpad->sp_array,
1423 xhci->scratchpad->sp_dma); 1624 xhci->scratchpad->sp_dma);
1424 kfree(xhci->scratchpad); 1625 kfree(xhci->scratchpad);
@@ -1463,18 +1664,10 @@ struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
1463 1664
1464void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv) 1665void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv)
1465{ 1666{
1466 int last; 1667 if (urb_priv) {
1467 1668 kfree(urb_priv->td[0]);
1468 if (!urb_priv) 1669 kfree(urb_priv);
1469 return;
1470
1471 last = urb_priv->length - 1;
1472 if (last >= 0) {
1473 int i;
1474 for (i = 0; i <= last; i++)
1475 kfree(urb_priv->td[i]);
1476 } 1670 }
1477 kfree(urb_priv);
1478} 1671}
1479 1672
1480void xhci_free_command(struct xhci_hcd *xhci, 1673void xhci_free_command(struct xhci_hcd *xhci,
@@ -1489,6 +1682,8 @@ void xhci_free_command(struct xhci_hcd *xhci,
1489void xhci_mem_cleanup(struct xhci_hcd *xhci) 1682void xhci_mem_cleanup(struct xhci_hcd *xhci)
1490{ 1683{
1491 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 1684 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
1685 struct dev_info *dev_info, *next;
1686 unsigned long flags;
1492 int size; 1687 int size;
1493 int i; 1688 int i;
1494 1689
@@ -1500,7 +1695,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
1500 } 1695 }
1501 size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries); 1696 size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
1502 if (xhci->erst.entries) 1697 if (xhci->erst.entries)
1503 pci_free_consistent(pdev, size, 1698 dma_free_coherent(&pdev->dev, size,
1504 xhci->erst.entries, xhci->erst.erst_dma_addr); 1699 xhci->erst.entries, xhci->erst.erst_dma_addr);
1505 xhci->erst.entries = NULL; 1700 xhci->erst.entries = NULL;
1506 xhci_dbg(xhci, "Freed ERST\n"); 1701 xhci_dbg(xhci, "Freed ERST\n");
@@ -1540,17 +1735,25 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
1540 1735
1541 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr); 1736 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
1542 if (xhci->dcbaa) 1737 if (xhci->dcbaa)
1543 pci_free_consistent(pdev, sizeof(*xhci->dcbaa), 1738 dma_free_coherent(&pdev->dev, sizeof(*xhci->dcbaa),
1544 xhci->dcbaa, xhci->dcbaa->dma); 1739 xhci->dcbaa, xhci->dcbaa->dma);
1545 xhci->dcbaa = NULL; 1740 xhci->dcbaa = NULL;
1546 1741
1547 scratchpad_free(xhci); 1742 scratchpad_free(xhci);
1548 1743
1744 spin_lock_irqsave(&xhci->lock, flags);
1745 list_for_each_entry_safe(dev_info, next, &xhci->lpm_failed_devs, list) {
1746 list_del(&dev_info->list);
1747 kfree(dev_info);
1748 }
1749 spin_unlock_irqrestore(&xhci->lock, flags);
1750
1549 xhci->num_usb2_ports = 0; 1751 xhci->num_usb2_ports = 0;
1550 xhci->num_usb3_ports = 0; 1752 xhci->num_usb3_ports = 0;
1551 kfree(xhci->usb2_ports); 1753 kfree(xhci->usb2_ports);
1552 kfree(xhci->usb3_ports); 1754 kfree(xhci->usb3_ports);
1553 kfree(xhci->port_array); 1755 kfree(xhci->port_array);
1756 kfree(xhci->rh_bw);
1554 1757
1555 xhci->page_size = 0; 1758 xhci->page_size = 0;
1556 xhci->page_shift = 0; 1759 xhci->page_shift = 0;
@@ -1762,6 +1965,23 @@ static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
1762 if (port_offset == 0 || (port_offset + port_count - 1) > num_ports) 1965 if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
1763 /* WTF? "Valid values are ‘1’ to MaxPorts" */ 1966 /* WTF? "Valid values are ‘1’ to MaxPorts" */
1764 return; 1967 return;
1968
1969 /* Check the host's USB2 LPM capability */
1970 if ((xhci->hci_version == 0x96) && (major_revision != 0x03) &&
1971 (temp & XHCI_L1C)) {
1972 xhci_dbg(xhci, "xHCI 0.96: support USB2 software lpm\n");
1973 xhci->sw_lpm_support = 1;
1974 }
1975
1976 if ((xhci->hci_version >= 0x100) && (major_revision != 0x03)) {
1977 xhci_dbg(xhci, "xHCI 1.0: support USB2 software lpm\n");
1978 xhci->sw_lpm_support = 1;
1979 if (temp & XHCI_HLC) {
1980 xhci_dbg(xhci, "xHCI 1.0: support USB2 hardware lpm\n");
1981 xhci->hw_lpm_support = 1;
1982 }
1983 }
1984
1765 port_offset--; 1985 port_offset--;
1766 for (i = port_offset; i < (port_offset + port_count); i++) { 1986 for (i = port_offset; i < (port_offset + port_count); i++) {
1767 /* Duplicate entry. Ignore the port if the revisions differ. */ 1987 /* Duplicate entry. Ignore the port if the revisions differ. */
@@ -1806,7 +2026,7 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
1806 __le32 __iomem *addr; 2026 __le32 __iomem *addr;
1807 u32 offset; 2027 u32 offset;
1808 unsigned int num_ports; 2028 unsigned int num_ports;
1809 int i, port_index; 2029 int i, j, port_index;
1810 2030
1811 addr = &xhci->cap_regs->hcc_params; 2031 addr = &xhci->cap_regs->hcc_params;
1812 offset = XHCI_HCC_EXT_CAPS(xhci_readl(xhci, addr)); 2032 offset = XHCI_HCC_EXT_CAPS(xhci_readl(xhci, addr));
@@ -1821,6 +2041,18 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
1821 if (!xhci->port_array) 2041 if (!xhci->port_array)
1822 return -ENOMEM; 2042 return -ENOMEM;
1823 2043
2044 xhci->rh_bw = kzalloc(sizeof(*xhci->rh_bw)*num_ports, flags);
2045 if (!xhci->rh_bw)
2046 return -ENOMEM;
2047 for (i = 0; i < num_ports; i++) {
2048 struct xhci_interval_bw_table *bw_table;
2049
2050 INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
2051 bw_table = &xhci->rh_bw[i].bw_table;
2052 for (j = 0; j < XHCI_MAX_INTERVAL; j++)
2053 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
2054 }
2055
1824 /* 2056 /*
1825 * For whatever reason, the first capability offset is from the 2057 * For whatever reason, the first capability offset is from the
1826 * capability register base, not from the HCCPARAMS register. 2058 * capability register base, not from the HCCPARAMS register.
@@ -1959,8 +2191,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
1959 * Section 5.4.8 - doorbell array must be 2191 * Section 5.4.8 - doorbell array must be
1960 * "physically contiguous and 64-byte (cache line) aligned". 2192 * "physically contiguous and 64-byte (cache line) aligned".
1961 */ 2193 */
1962 xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev), 2194 xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
1963 sizeof(*xhci->dcbaa), &dma); 2195 GFP_KERNEL);
1964 if (!xhci->dcbaa) 2196 if (!xhci->dcbaa)
1965 goto fail; 2197 goto fail;
1966 memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa)); 2198 memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
@@ -1994,14 +2226,14 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
1994 dma_pool_create("xHCI 1KB stream ctx arrays", 2226 dma_pool_create("xHCI 1KB stream ctx arrays",
1995 dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0); 2227 dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
1996 /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE 2228 /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
1997 * will be allocated with pci_alloc_consistent() 2229 * will be allocated with dma_alloc_coherent()
1998 */ 2230 */
1999 2231
2000 if (!xhci->small_streams_pool || !xhci->medium_streams_pool) 2232 if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
2001 goto fail; 2233 goto fail;
2002 2234
2003 /* Set up the command ring to have one segments for now. */ 2235 /* Set up the command ring to have one segments for now. */
2004 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags); 2236 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, false, flags);
2005 if (!xhci->cmd_ring) 2237 if (!xhci->cmd_ring)
2006 goto fail; 2238 goto fail;
2007 xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring); 2239 xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
@@ -2032,14 +2264,16 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
2032 * the event ring segment table (ERST). Section 4.9.3. 2264 * the event ring segment table (ERST). Section 4.9.3.
2033 */ 2265 */
2034 xhci_dbg(xhci, "// Allocating event ring\n"); 2266 xhci_dbg(xhci, "// Allocating event ring\n");
2035 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags); 2267 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, false,
2268 flags);
2036 if (!xhci->event_ring) 2269 if (!xhci->event_ring)
2037 goto fail; 2270 goto fail;
2038 if (xhci_check_trb_in_td_math(xhci, flags) < 0) 2271 if (xhci_check_trb_in_td_math(xhci, flags) < 0)
2039 goto fail; 2272 goto fail;
2040 2273
2041 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev), 2274 xhci->erst.entries = dma_alloc_coherent(dev,
2042 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma); 2275 sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS, &dma,
2276 GFP_KERNEL);
2043 if (!xhci->erst.entries) 2277 if (!xhci->erst.entries)
2044 goto fail; 2278 goto fail;
2045 xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n", 2279 xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
@@ -2102,6 +2336,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
2102 if (xhci_setup_port_arrays(xhci, flags)) 2336 if (xhci_setup_port_arrays(xhci, flags))
2103 goto fail; 2337 goto fail;
2104 2338
2339 INIT_LIST_HEAD(&xhci->lpm_failed_devs);
2340
2105 return 0; 2341 return 0;
2106 2342
2107fail: 2343fail:
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index cb16de213f64..ef98b38626fb 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -22,6 +22,7 @@
22 22
23#include <linux/pci.h> 23#include <linux/pci.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/module.h>
25 26
26#include "xhci.h" 27#include "xhci.h"
27 28
@@ -51,61 +52,9 @@ static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
51 return 0; 52 return 0;
52} 53}
53 54
54/* called during probe() after chip reset completes */ 55static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
55static int xhci_pci_setup(struct usb_hcd *hcd)
56{ 56{
57 struct xhci_hcd *xhci; 57 struct pci_dev *pdev = to_pci_dev(dev);
58 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
59 int retval;
60 u32 temp;
61
62 hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 2;
63
64 if (usb_hcd_is_primary_hcd(hcd)) {
65 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
66 if (!xhci)
67 return -ENOMEM;
68 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
69 xhci->main_hcd = hcd;
70 /* Mark the first roothub as being USB 2.0.
71 * The xHCI driver will register the USB 3.0 roothub.
72 */
73 hcd->speed = HCD_USB2;
74 hcd->self.root_hub->speed = USB_SPEED_HIGH;
75 /*
76 * USB 2.0 roothub under xHCI has an integrated TT,
77 * (rate matching hub) as opposed to having an OHCI/UHCI
78 * companion controller.
79 */
80 hcd->has_tt = 1;
81 } else {
82 /* xHCI private pointer was set in xhci_pci_probe for the second
83 * registered roothub.
84 */
85 xhci = hcd_to_xhci(hcd);
86 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
87 if (HCC_64BIT_ADDR(temp)) {
88 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
89 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
90 } else {
91 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
92 }
93 return 0;
94 }
95
96 xhci->cap_regs = hcd->regs;
97 xhci->op_regs = hcd->regs +
98 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
99 xhci->run_regs = hcd->regs +
100 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
101 /* Cache read-only capability registers */
102 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
103 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
104 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
105 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
106 xhci->hci_version = HC_VERSION(xhci->hcc_params);
107 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
108 xhci_print_registers(xhci);
109 58
110 /* Look for vendor-specific quirks */ 59 /* Look for vendor-specific quirks */
111 if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC && 60 if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
@@ -128,6 +77,9 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
128 if (pdev->vendor == PCI_VENDOR_ID_NEC) 77 if (pdev->vendor == PCI_VENDOR_ID_NEC)
129 xhci->quirks |= XHCI_NEC_HOST; 78 xhci->quirks |= XHCI_NEC_HOST;
130 79
80 if (pdev->vendor == PCI_VENDOR_ID_AMD && xhci->hci_version == 0x96)
81 xhci->quirks |= XHCI_AMD_0x96_HOST;
82
131 /* AMD PLL quirk */ 83 /* AMD PLL quirk */
132 if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_find_chipset_info()) 84 if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_find_chipset_info())
133 xhci->quirks |= XHCI_AMD_PLL_FIX; 85 xhci->quirks |= XHCI_AMD_PLL_FIX;
@@ -136,39 +88,29 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
136 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; 88 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
137 xhci->quirks |= XHCI_EP_LIMIT_QUIRK; 89 xhci->quirks |= XHCI_EP_LIMIT_QUIRK;
138 xhci->limit_active_eps = 64; 90 xhci->limit_active_eps = 64;
91 xhci->quirks |= XHCI_SW_BW_CHECKING;
139 } 92 }
140 if (pdev->vendor == PCI_VENDOR_ID_ETRON && 93 if (pdev->vendor == PCI_VENDOR_ID_ETRON &&
141 pdev->device == PCI_DEVICE_ID_ASROCK_P67) { 94 pdev->device == PCI_DEVICE_ID_ASROCK_P67) {
142 xhci->quirks |= XHCI_RESET_ON_RESUME; 95 xhci->quirks |= XHCI_RESET_ON_RESUME;
143 xhci_dbg(xhci, "QUIRK: Resetting on resume\n"); 96 xhci_dbg(xhci, "QUIRK: Resetting on resume\n");
144 } 97 }
98}
145 99
146 /* Make sure the HC is halted. */ 100/* called during probe() after chip reset completes */
147 retval = xhci_halt(xhci); 101static int xhci_pci_setup(struct usb_hcd *hcd)
148 if (retval) 102{
149 goto error; 103 struct xhci_hcd *xhci;
104 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
105 int retval;
150 106
151 xhci_dbg(xhci, "Resetting HCD\n"); 107 retval = xhci_gen_setup(hcd, xhci_pci_quirks);
152 /* Reset the internal HC memory state and registers. */
153 retval = xhci_reset(xhci);
154 if (retval) 108 if (retval)
155 goto error; 109 return retval;
156 xhci_dbg(xhci, "Reset complete\n");
157
158 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
159 if (HCC_64BIT_ADDR(temp)) {
160 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
161 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
162 } else {
163 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
164 }
165 110
166 xhci_dbg(xhci, "Calling HCD init\n"); 111 xhci = hcd_to_xhci(hcd);
167 /* Initialize HCD and host controller data structures. */ 112 if (!usb_hcd_is_primary_hcd(hcd))
168 retval = xhci_init(hcd); 113 return 0;
169 if (retval)
170 goto error;
171 xhci_dbg(xhci, "Called HCD init\n");
172 114
173 pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn); 115 pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
174 xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn); 116 xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
@@ -178,7 +120,6 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
178 if (!retval) 120 if (!retval)
179 return retval; 121 return retval;
180 122
181error:
182 kfree(xhci); 123 kfree(xhci);
183 return retval; 124 return retval;
184} 125}
@@ -222,7 +163,7 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
222 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci; 163 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
223 164
224 retval = usb_add_hcd(xhci->shared_hcd, dev->irq, 165 retval = usb_add_hcd(xhci->shared_hcd, dev->irq,
225 IRQF_DISABLED | IRQF_SHARED); 166 IRQF_SHARED);
226 if (retval) 167 if (retval)
227 goto put_usb3_hcd; 168 goto put_usb3_hcd;
228 /* Roothub already marked as USB 3.0 speed */ 169 /* Roothub already marked as USB 3.0 speed */
@@ -344,6 +285,11 @@ static const struct hc_driver xhci_pci_hc_driver = {
344 .hub_status_data = xhci_hub_status_data, 285 .hub_status_data = xhci_hub_status_data,
345 .bus_suspend = xhci_bus_suspend, 286 .bus_suspend = xhci_bus_suspend,
346 .bus_resume = xhci_bus_resume, 287 .bus_resume = xhci_bus_resume,
288 /*
289 * call back when device connected and addressed
290 */
291 .update_device = xhci_update_device,
292 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
347}; 293};
348 294
349/*-------------------------------------------------------------------------*/ 295/*-------------------------------------------------------------------------*/
@@ -375,12 +321,12 @@ static struct pci_driver xhci_pci_driver = {
375#endif 321#endif
376}; 322};
377 323
378int xhci_register_pci(void) 324int __init xhci_register_pci(void)
379{ 325{
380 return pci_register_driver(&xhci_pci_driver); 326 return pci_register_driver(&xhci_pci_driver);
381} 327}
382 328
383void xhci_unregister_pci(void) 329void __exit xhci_unregister_pci(void)
384{ 330{
385 pci_unregister_driver(&xhci_pci_driver); 331 pci_unregister_driver(&xhci_pci_driver);
386} 332}
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 54139a2f06ce..9f1d4b15d818 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -185,7 +185,7 @@ static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer
185 * prepare_transfer()? 185 * prepare_transfer()?
186 */ 186 */
187static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, 187static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
188 bool consumer, bool more_trbs_coming) 188 bool consumer, bool more_trbs_coming, bool isoc)
189{ 189{
190 u32 chain; 190 u32 chain;
191 union xhci_trb *next; 191 union xhci_trb *next;
@@ -212,11 +212,13 @@ static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
212 if (!chain && !more_trbs_coming) 212 if (!chain && !more_trbs_coming)
213 break; 213 break;
214 214
215 /* If we're not dealing with 0.95 hardware, 215 /* If we're not dealing with 0.95 hardware or
216 * isoc rings on AMD 0.96 host,
216 * carry over the chain bit of the previous TRB 217 * carry over the chain bit of the previous TRB
217 * (which may mean the chain bit is cleared). 218 * (which may mean the chain bit is cleared).
218 */ 219 */
219 if (!xhci_link_trb_quirk(xhci)) { 220 if (!(isoc && (xhci->quirks & XHCI_AMD_0x96_HOST))
221 && !xhci_link_trb_quirk(xhci)) {
220 next->link.control &= 222 next->link.control &=
221 cpu_to_le32(~TRB_CHAIN); 223 cpu_to_le32(~TRB_CHAIN);
222 next->link.control |= 224 next->link.control |=
@@ -814,23 +816,24 @@ void xhci_stop_endpoint_command_watchdog(unsigned long arg)
814 struct xhci_ring *ring; 816 struct xhci_ring *ring;
815 struct xhci_td *cur_td; 817 struct xhci_td *cur_td;
816 int ret, i, j; 818 int ret, i, j;
819 unsigned long flags;
817 820
818 ep = (struct xhci_virt_ep *) arg; 821 ep = (struct xhci_virt_ep *) arg;
819 xhci = ep->xhci; 822 xhci = ep->xhci;
820 823
821 spin_lock(&xhci->lock); 824 spin_lock_irqsave(&xhci->lock, flags);
822 825
823 ep->stop_cmds_pending--; 826 ep->stop_cmds_pending--;
824 if (xhci->xhc_state & XHCI_STATE_DYING) { 827 if (xhci->xhc_state & XHCI_STATE_DYING) {
825 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked " 828 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
826 "xHCI as DYING, exiting.\n"); 829 "xHCI as DYING, exiting.\n");
827 spin_unlock(&xhci->lock); 830 spin_unlock_irqrestore(&xhci->lock, flags);
828 return; 831 return;
829 } 832 }
830 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) { 833 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
831 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, " 834 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
832 "exiting.\n"); 835 "exiting.\n");
833 spin_unlock(&xhci->lock); 836 spin_unlock_irqrestore(&xhci->lock, flags);
834 return; 837 return;
835 } 838 }
836 839
@@ -842,11 +845,11 @@ void xhci_stop_endpoint_command_watchdog(unsigned long arg)
842 xhci->xhc_state |= XHCI_STATE_DYING; 845 xhci->xhc_state |= XHCI_STATE_DYING;
843 /* Disable interrupts from the host controller and start halting it */ 846 /* Disable interrupts from the host controller and start halting it */
844 xhci_quiesce(xhci); 847 xhci_quiesce(xhci);
845 spin_unlock(&xhci->lock); 848 spin_unlock_irqrestore(&xhci->lock, flags);
846 849
847 ret = xhci_halt(xhci); 850 ret = xhci_halt(xhci);
848 851
849 spin_lock(&xhci->lock); 852 spin_lock_irqsave(&xhci->lock, flags);
850 if (ret < 0) { 853 if (ret < 0) {
851 /* This is bad; the host is not responding to commands and it's 854 /* This is bad; the host is not responding to commands and it's
852 * not allowing itself to be halted. At least interrupts are 855 * not allowing itself to be halted. At least interrupts are
@@ -894,7 +897,7 @@ void xhci_stop_endpoint_command_watchdog(unsigned long arg)
894 } 897 }
895 } 898 }
896 } 899 }
897 spin_unlock(&xhci->lock); 900 spin_unlock_irqrestore(&xhci->lock, flags);
898 xhci_dbg(xhci, "Calling usb_hc_died()\n"); 901 xhci_dbg(xhci, "Calling usb_hc_died()\n");
899 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd); 902 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
900 xhci_dbg(xhci, "xHCI host controller is dead.\n"); 903 xhci_dbg(xhci, "xHCI host controller is dead.\n");
@@ -1329,10 +1332,8 @@ static void handle_port_status(struct xhci_hcd *xhci,
1329 1332
1330 if (DEV_SUPERSPEED(temp)) { 1333 if (DEV_SUPERSPEED(temp)) {
1331 xhci_dbg(xhci, "resume SS port %d\n", port_id); 1334 xhci_dbg(xhci, "resume SS port %d\n", port_id);
1332 temp = xhci_port_state_to_neutral(temp); 1335 xhci_set_link_state(xhci, port_array, faked_port_index,
1333 temp &= ~PORT_PLS_MASK; 1336 XDEV_U0);
1334 temp |= PORT_LINK_STROBE | XDEV_U0;
1335 xhci_writel(xhci, temp, port_array[faked_port_index]);
1336 slot_id = xhci_find_slot_id_by_port(hcd, xhci, 1337 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1337 faked_port_index); 1338 faked_port_index);
1338 if (!slot_id) { 1339 if (!slot_id) {
@@ -1342,10 +1343,8 @@ static void handle_port_status(struct xhci_hcd *xhci,
1342 xhci_ring_device(xhci, slot_id); 1343 xhci_ring_device(xhci, slot_id);
1343 xhci_dbg(xhci, "resume SS port %d finished\n", port_id); 1344 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
1344 /* Clear PORT_PLC */ 1345 /* Clear PORT_PLC */
1345 temp = xhci_readl(xhci, port_array[faked_port_index]); 1346 xhci_test_and_clear_bit(xhci, port_array,
1346 temp = xhci_port_state_to_neutral(temp); 1347 faked_port_index, PORT_PLC);
1347 temp |= PORT_PLC;
1348 xhci_writel(xhci, temp, port_array[faked_port_index]);
1349 } else { 1348 } else {
1350 xhci_dbg(xhci, "resume HS port %d\n", port_id); 1349 xhci_dbg(xhci, "resume HS port %d\n", port_id);
1351 bus_state->resume_done[faked_port_index] = jiffies + 1350 bus_state->resume_done[faked_port_index] = jiffies +
@@ -1356,6 +1355,10 @@ static void handle_port_status(struct xhci_hcd *xhci,
1356 } 1355 }
1357 } 1356 }
1358 1357
1358 if (hcd->speed != HCD_USB3)
1359 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1360 PORT_PLC);
1361
1359cleanup: 1362cleanup:
1360 /* Update event ring dequeue pointer before dropping the lock */ 1363 /* Update event ring dequeue pointer before dropping the lock */
1361 inc_deq(xhci, xhci->event_ring, true); 1364 inc_deq(xhci, xhci->event_ring, true);
@@ -1934,8 +1937,10 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1934 int status = -EINPROGRESS; 1937 int status = -EINPROGRESS;
1935 struct urb_priv *urb_priv; 1938 struct urb_priv *urb_priv;
1936 struct xhci_ep_ctx *ep_ctx; 1939 struct xhci_ep_ctx *ep_ctx;
1940 struct list_head *tmp;
1937 u32 trb_comp_code; 1941 u32 trb_comp_code;
1938 int ret = 0; 1942 int ret = 0;
1943 int td_num = 0;
1939 1944
1940 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 1945 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
1941 xdev = xhci->devs[slot_id]; 1946 xdev = xhci->devs[slot_id];
@@ -1957,6 +1962,12 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1957 return -ENODEV; 1962 return -ENODEV;
1958 } 1963 }
1959 1964
1965 /* Count current td numbers if ep->skip is set */
1966 if (ep->skip) {
1967 list_for_each(tmp, &ep_ring->td_list)
1968 td_num++;
1969 }
1970
1960 event_dma = le64_to_cpu(event->buffer); 1971 event_dma = le64_to_cpu(event->buffer);
1961 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 1972 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
1962 /* Look for common error cases */ 1973 /* Look for common error cases */
@@ -2068,7 +2079,18 @@ static int handle_tx_event(struct xhci_hcd *xhci,
2068 goto cleanup; 2079 goto cleanup;
2069 } 2080 }
2070 2081
2082 /* We've skipped all the TDs on the ep ring when ep->skip set */
2083 if (ep->skip && td_num == 0) {
2084 ep->skip = false;
2085 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2086 "Clear skip flag.\n");
2087 ret = 0;
2088 goto cleanup;
2089 }
2090
2071 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list); 2091 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
2092 if (ep->skip)
2093 td_num--;
2072 2094
2073 /* Is this a TRB in the currently executing TD? */ 2095 /* Is this a TRB in the currently executing TD? */
2074 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue, 2096 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
@@ -2173,7 +2195,8 @@ cleanup:
2173 if ((urb->actual_length != urb->transfer_buffer_length && 2195 if ((urb->actual_length != urb->transfer_buffer_length &&
2174 (urb->transfer_flags & 2196 (urb->transfer_flags &
2175 URB_SHORT_NOT_OK)) || 2197 URB_SHORT_NOT_OK)) ||
2176 status != 0) 2198 (status != 0 &&
2199 !usb_endpoint_xfer_isoc(&urb->ep->desc)))
2177 xhci_dbg(xhci, "Giveback URB %p, len = %d, " 2200 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2178 "expected = %x, status = %d\n", 2201 "expected = %x, status = %d\n",
2179 urb, urb->actual_length, 2202 urb, urb->actual_length,
@@ -2390,7 +2413,7 @@ irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2390 * prepare_transfer()? 2413 * prepare_transfer()?
2391 */ 2414 */
2392static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, 2415static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
2393 bool consumer, bool more_trbs_coming, 2416 bool consumer, bool more_trbs_coming, bool isoc,
2394 u32 field1, u32 field2, u32 field3, u32 field4) 2417 u32 field1, u32 field2, u32 field3, u32 field4)
2395{ 2418{
2396 struct xhci_generic_trb *trb; 2419 struct xhci_generic_trb *trb;
@@ -2400,7 +2423,7 @@ static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
2400 trb->field[1] = cpu_to_le32(field2); 2423 trb->field[1] = cpu_to_le32(field2);
2401 trb->field[2] = cpu_to_le32(field3); 2424 trb->field[2] = cpu_to_le32(field3);
2402 trb->field[3] = cpu_to_le32(field4); 2425 trb->field[3] = cpu_to_le32(field4);
2403 inc_enq(xhci, ring, consumer, more_trbs_coming); 2426 inc_enq(xhci, ring, consumer, more_trbs_coming, isoc);
2404} 2427}
2405 2428
2406/* 2429/*
@@ -2408,7 +2431,7 @@ static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
2408 * FIXME allocate segments if the ring is full. 2431 * FIXME allocate segments if the ring is full.
2409 */ 2432 */
2410static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, 2433static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2411 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) 2434 u32 ep_state, unsigned int num_trbs, bool isoc, gfp_t mem_flags)
2412{ 2435{
2413 /* Make sure the endpoint has been added to xHC schedule */ 2436 /* Make sure the endpoint has been added to xHC schedule */
2414 switch (ep_state) { 2437 switch (ep_state) {
@@ -2450,10 +2473,11 @@ static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2450 next = ring->enqueue; 2473 next = ring->enqueue;
2451 2474
2452 while (last_trb(xhci, ring, ring->enq_seg, next)) { 2475 while (last_trb(xhci, ring, ring->enq_seg, next)) {
2453 /* If we're not dealing with 0.95 hardware, 2476 /* If we're not dealing with 0.95 hardware or isoc rings
2454 * clear the chain bit. 2477 * on AMD 0.96 host, clear the chain bit.
2455 */ 2478 */
2456 if (!xhci_link_trb_quirk(xhci)) 2479 if (!xhci_link_trb_quirk(xhci) && !(isoc &&
2480 (xhci->quirks & XHCI_AMD_0x96_HOST)))
2457 next->link.control &= cpu_to_le32(~TRB_CHAIN); 2481 next->link.control &= cpu_to_le32(~TRB_CHAIN);
2458 else 2482 else
2459 next->link.control |= cpu_to_le32(TRB_CHAIN); 2483 next->link.control |= cpu_to_le32(TRB_CHAIN);
@@ -2486,6 +2510,7 @@ static int prepare_transfer(struct xhci_hcd *xhci,
2486 unsigned int num_trbs, 2510 unsigned int num_trbs,
2487 struct urb *urb, 2511 struct urb *urb,
2488 unsigned int td_index, 2512 unsigned int td_index,
2513 bool isoc,
2489 gfp_t mem_flags) 2514 gfp_t mem_flags)
2490{ 2515{
2491 int ret; 2516 int ret;
@@ -2503,7 +2528,7 @@ static int prepare_transfer(struct xhci_hcd *xhci,
2503 2528
2504 ret = prepare_ring(xhci, ep_ring, 2529 ret = prepare_ring(xhci, ep_ring,
2505 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK, 2530 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
2506 num_trbs, mem_flags); 2531 num_trbs, isoc, mem_flags);
2507 if (ret) 2532 if (ret)
2508 return ret; 2533 return ret;
2509 2534
@@ -2692,7 +2717,7 @@ static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
2692 * running_total. 2717 * running_total.
2693 */ 2718 */
2694 packets_transferred = (running_total + trb_buff_len) / 2719 packets_transferred = (running_total + trb_buff_len) /
2695 le16_to_cpu(urb->ep->desc.wMaxPacketSize); 2720 usb_endpoint_maxp(&urb->ep->desc);
2696 2721
2697 return xhci_td_remainder(total_packet_count - packets_transferred); 2722 return xhci_td_remainder(total_packet_count - packets_transferred);
2698} 2723}
@@ -2722,11 +2747,11 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2722 num_trbs = count_sg_trbs_needed(xhci, urb); 2747 num_trbs = count_sg_trbs_needed(xhci, urb);
2723 num_sgs = urb->num_sgs; 2748 num_sgs = urb->num_sgs;
2724 total_packet_count = roundup(urb->transfer_buffer_length, 2749 total_packet_count = roundup(urb->transfer_buffer_length,
2725 le16_to_cpu(urb->ep->desc.wMaxPacketSize)); 2750 usb_endpoint_maxp(&urb->ep->desc));
2726 2751
2727 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id], 2752 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
2728 ep_index, urb->stream_id, 2753 ep_index, urb->stream_id,
2729 num_trbs, urb, 0, mem_flags); 2754 num_trbs, urb, 0, false, mem_flags);
2730 if (trb_buff_len < 0) 2755 if (trb_buff_len < 0)
2731 return trb_buff_len; 2756 return trb_buff_len;
2732 2757
@@ -2821,7 +2846,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2821 more_trbs_coming = true; 2846 more_trbs_coming = true;
2822 else 2847 else
2823 more_trbs_coming = false; 2848 more_trbs_coming = false;
2824 queue_trb(xhci, ep_ring, false, more_trbs_coming, 2849 queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
2825 lower_32_bits(addr), 2850 lower_32_bits(addr),
2826 upper_32_bits(addr), 2851 upper_32_bits(addr),
2827 length_field, 2852 length_field,
@@ -2912,7 +2937,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2912 2937
2913 ret = prepare_transfer(xhci, xhci->devs[slot_id], 2938 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2914 ep_index, urb->stream_id, 2939 ep_index, urb->stream_id,
2915 num_trbs, urb, 0, mem_flags); 2940 num_trbs, urb, 0, false, mem_flags);
2916 if (ret < 0) 2941 if (ret < 0)
2917 return ret; 2942 return ret;
2918 2943
@@ -2929,7 +2954,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2929 2954
2930 running_total = 0; 2955 running_total = 0;
2931 total_packet_count = roundup(urb->transfer_buffer_length, 2956 total_packet_count = roundup(urb->transfer_buffer_length,
2932 le16_to_cpu(urb->ep->desc.wMaxPacketSize)); 2957 usb_endpoint_maxp(&urb->ep->desc));
2933 /* How much data is in the first TRB? */ 2958 /* How much data is in the first TRB? */
2934 addr = (u64) urb->transfer_dma; 2959 addr = (u64) urb->transfer_dma;
2935 trb_buff_len = TRB_MAX_BUFF_SIZE - 2960 trb_buff_len = TRB_MAX_BUFF_SIZE -
@@ -2984,7 +3009,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2984 more_trbs_coming = true; 3009 more_trbs_coming = true;
2985 else 3010 else
2986 more_trbs_coming = false; 3011 more_trbs_coming = false;
2987 queue_trb(xhci, ep_ring, false, more_trbs_coming, 3012 queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
2988 lower_32_bits(addr), 3013 lower_32_bits(addr),
2989 upper_32_bits(addr), 3014 upper_32_bits(addr),
2990 length_field, 3015 length_field,
@@ -3044,7 +3069,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3044 num_trbs++; 3069 num_trbs++;
3045 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3070 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3046 ep_index, urb->stream_id, 3071 ep_index, urb->stream_id,
3047 num_trbs, urb, 0, mem_flags); 3072 num_trbs, urb, 0, false, mem_flags);
3048 if (ret < 0) 3073 if (ret < 0)
3049 return ret; 3074 return ret;
3050 3075
@@ -3077,7 +3102,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3077 } 3102 }
3078 } 3103 }
3079 3104
3080 queue_trb(xhci, ep_ring, false, true, 3105 queue_trb(xhci, ep_ring, false, true, false,
3081 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, 3106 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3082 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, 3107 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3083 TRB_LEN(8) | TRB_INTR_TARGET(0), 3108 TRB_LEN(8) | TRB_INTR_TARGET(0),
@@ -3097,7 +3122,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3097 if (urb->transfer_buffer_length > 0) { 3122 if (urb->transfer_buffer_length > 0) {
3098 if (setup->bRequestType & USB_DIR_IN) 3123 if (setup->bRequestType & USB_DIR_IN)
3099 field |= TRB_DIR_IN; 3124 field |= TRB_DIR_IN;
3100 queue_trb(xhci, ep_ring, false, true, 3125 queue_trb(xhci, ep_ring, false, true, false,
3101 lower_32_bits(urb->transfer_dma), 3126 lower_32_bits(urb->transfer_dma),
3102 upper_32_bits(urb->transfer_dma), 3127 upper_32_bits(urb->transfer_dma),
3103 length_field, 3128 length_field,
@@ -3113,7 +3138,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3113 field = 0; 3138 field = 0;
3114 else 3139 else
3115 field = TRB_DIR_IN; 3140 field = TRB_DIR_IN;
3116 queue_trb(xhci, ep_ring, false, false, 3141 queue_trb(xhci, ep_ring, false, false, false,
3117 0, 3142 0,
3118 0, 3143 0,
3119 TRB_INTR_TARGET(0), 3144 TRB_INTR_TARGET(0),
@@ -3250,7 +3275,7 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3250 td_len = urb->iso_frame_desc[i].length; 3275 td_len = urb->iso_frame_desc[i].length;
3251 td_remain_len = td_len; 3276 td_remain_len = td_len;
3252 total_packet_count = roundup(td_len, 3277 total_packet_count = roundup(td_len,
3253 le16_to_cpu(urb->ep->desc.wMaxPacketSize)); 3278 usb_endpoint_maxp(&urb->ep->desc));
3254 /* A zero-length transfer still involves at least one packet. */ 3279 /* A zero-length transfer still involves at least one packet. */
3255 if (total_packet_count == 0) 3280 if (total_packet_count == 0)
3256 total_packet_count++; 3281 total_packet_count++;
@@ -3262,7 +3287,8 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3262 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i); 3287 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3263 3288
3264 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, 3289 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
3265 urb->stream_id, trbs_per_td, urb, i, mem_flags); 3290 urb->stream_id, trbs_per_td, urb, i, true,
3291 mem_flags);
3266 if (ret < 0) { 3292 if (ret < 0) {
3267 if (i == 0) 3293 if (i == 0)
3268 return ret; 3294 return ret;
@@ -3332,7 +3358,7 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3332 remainder | 3358 remainder |
3333 TRB_INTR_TARGET(0); 3359 TRB_INTR_TARGET(0);
3334 3360
3335 queue_trb(xhci, ep_ring, false, more_trbs_coming, 3361 queue_trb(xhci, ep_ring, false, more_trbs_coming, true,
3336 lower_32_bits(addr), 3362 lower_32_bits(addr),
3337 upper_32_bits(addr), 3363 upper_32_bits(addr),
3338 length_field, 3364 length_field,
@@ -3414,7 +3440,7 @@ int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3414 * Do not insert any td of the urb to the ring if the check failed. 3440 * Do not insert any td of the urb to the ring if the check failed.
3415 */ 3441 */
3416 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK, 3442 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
3417 num_trbs, mem_flags); 3443 num_trbs, true, mem_flags);
3418 if (ret) 3444 if (ret)
3419 return ret; 3445 return ret;
3420 3446
@@ -3473,7 +3499,7 @@ static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3473 reserved_trbs++; 3499 reserved_trbs++;
3474 3500
3475 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, 3501 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
3476 reserved_trbs, GFP_ATOMIC); 3502 reserved_trbs, false, GFP_ATOMIC);
3477 if (ret < 0) { 3503 if (ret < 0) {
3478 xhci_err(xhci, "ERR: No room for command on command ring\n"); 3504 xhci_err(xhci, "ERR: No room for command on command ring\n");
3479 if (command_must_succeed) 3505 if (command_must_succeed)
@@ -3481,8 +3507,8 @@ static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3481 "unfailable commands failed.\n"); 3507 "unfailable commands failed.\n");
3482 return ret; 3508 return ret;
3483 } 3509 }
3484 queue_trb(xhci, xhci->cmd_ring, false, false, field1, field2, field3, 3510 queue_trb(xhci, xhci->cmd_ring, false, false, false, field1, field2,
3485 field4 | xhci->cmd_ring->cycle_state); 3511 field3, field4 | xhci->cmd_ring->cycle_state);
3486 return 0; 3512 return 0;
3487} 3513}
3488 3514
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 3a0f695138f4..aa94c0195791 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -175,28 +175,19 @@ int xhci_reset(struct xhci_hcd *xhci)
175 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000); 175 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
176} 176}
177 177
178/* 178#ifdef CONFIG_PCI
179 * Free IRQs 179static int xhci_free_msi(struct xhci_hcd *xhci)
180 * free all IRQs request
181 */
182static void xhci_free_irq(struct xhci_hcd *xhci)
183{ 180{
184 int i; 181 int i;
185 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
186 182
187 /* return if using legacy interrupt */ 183 if (!xhci->msix_entries)
188 if (xhci_to_hcd(xhci)->irq >= 0) 184 return -EINVAL;
189 return;
190
191 if (xhci->msix_entries) {
192 for (i = 0; i < xhci->msix_count; i++)
193 if (xhci->msix_entries[i].vector)
194 free_irq(xhci->msix_entries[i].vector,
195 xhci_to_hcd(xhci));
196 } else if (pdev->irq >= 0)
197 free_irq(pdev->irq, xhci_to_hcd(xhci));
198 185
199 return; 186 for (i = 0; i < xhci->msix_count; i++)
187 if (xhci->msix_entries[i].vector)
188 free_irq(xhci->msix_entries[i].vector,
189 xhci_to_hcd(xhci));
190 return 0;
200} 191}
201 192
202/* 193/*
@@ -224,6 +215,28 @@ static int xhci_setup_msi(struct xhci_hcd *xhci)
224} 215}
225 216
226/* 217/*
218 * Free IRQs
219 * free all IRQs request
220 */
221static void xhci_free_irq(struct xhci_hcd *xhci)
222{
223 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
224 int ret;
225
226 /* return if using legacy interrupt */
227 if (xhci_to_hcd(xhci)->irq >= 0)
228 return;
229
230 ret = xhci_free_msi(xhci);
231 if (!ret)
232 return;
233 if (pdev->irq >= 0)
234 free_irq(pdev->irq, xhci_to_hcd(xhci));
235
236 return;
237}
238
239/*
227 * Set up MSI-X 240 * Set up MSI-X
228 */ 241 */
229static int xhci_setup_msix(struct xhci_hcd *xhci) 242static int xhci_setup_msix(struct xhci_hcd *xhci)
@@ -302,6 +315,72 @@ static void xhci_cleanup_msix(struct xhci_hcd *xhci)
302 return; 315 return;
303} 316}
304 317
318static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
319{
320 int i;
321
322 if (xhci->msix_entries) {
323 for (i = 0; i < xhci->msix_count; i++)
324 synchronize_irq(xhci->msix_entries[i].vector);
325 }
326}
327
328static int xhci_try_enable_msi(struct usb_hcd *hcd)
329{
330 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
331 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
332 int ret;
333
334 /*
335 * Some Fresco Logic host controllers advertise MSI, but fail to
336 * generate interrupts. Don't even try to enable MSI.
337 */
338 if (xhci->quirks & XHCI_BROKEN_MSI)
339 return 0;
340
341 /* unregister the legacy interrupt */
342 if (hcd->irq)
343 free_irq(hcd->irq, hcd);
344 hcd->irq = -1;
345
346 ret = xhci_setup_msix(xhci);
347 if (ret)
348 /* fall back to msi*/
349 ret = xhci_setup_msi(xhci);
350
351 if (!ret)
352 /* hcd->irq is -1, we have MSI */
353 return 0;
354
355 /* fall back to legacy interrupt*/
356 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
357 hcd->irq_descr, hcd);
358 if (ret) {
359 xhci_err(xhci, "request interrupt %d failed\n",
360 pdev->irq);
361 return ret;
362 }
363 hcd->irq = pdev->irq;
364 return 0;
365}
366
367#else
368
369static int xhci_try_enable_msi(struct usb_hcd *hcd)
370{
371 return 0;
372}
373
374static void xhci_cleanup_msix(struct xhci_hcd *xhci)
375{
376}
377
378static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
379{
380}
381
382#endif
383
305/* 384/*
306 * Initialize memory for HCD and xHC (one-time init). 385 * Initialize memory for HCD and xHC (one-time init).
307 * 386 *
@@ -316,7 +395,7 @@ int xhci_init(struct usb_hcd *hcd)
316 395
317 xhci_dbg(xhci, "xhci_init\n"); 396 xhci_dbg(xhci, "xhci_init\n");
318 spin_lock_init(&xhci->lock); 397 spin_lock_init(&xhci->lock);
319 if (link_quirk) { 398 if (xhci->hci_version == 0x95 && link_quirk) {
320 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n"); 399 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
321 xhci->quirks |= XHCI_LINK_TRB_QUIRK; 400 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
322 } else { 401 } else {
@@ -413,9 +492,8 @@ int xhci_run(struct usb_hcd *hcd)
413{ 492{
414 u32 temp; 493 u32 temp;
415 u64 temp_64; 494 u64 temp_64;
416 u32 ret; 495 int ret;
417 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 496 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
418 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
419 497
420 /* Start the xHCI host controller running only after the USB 2.0 roothub 498 /* Start the xHCI host controller running only after the USB 2.0 roothub
421 * is setup. 499 * is setup.
@@ -426,34 +504,10 @@ int xhci_run(struct usb_hcd *hcd)
426 return xhci_run_finished(xhci); 504 return xhci_run_finished(xhci);
427 505
428 xhci_dbg(xhci, "xhci_run\n"); 506 xhci_dbg(xhci, "xhci_run\n");
429 /* unregister the legacy interrupt */
430 if (hcd->irq)
431 free_irq(hcd->irq, hcd);
432 hcd->irq = -1;
433 507
434 /* Some Fresco Logic host controllers advertise MSI, but fail to 508 ret = xhci_try_enable_msi(hcd);
435 * generate interrupts. Don't even try to enable MSI.
436 */
437 if (xhci->quirks & XHCI_BROKEN_MSI)
438 goto legacy_irq;
439
440 ret = xhci_setup_msix(xhci);
441 if (ret) 509 if (ret)
442 /* fall back to msi*/ 510 return ret;
443 ret = xhci_setup_msi(xhci);
444
445 if (ret) {
446legacy_irq:
447 /* fall back to legacy interrupt*/
448 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
449 hcd->irq_descr, hcd);
450 if (ret) {
451 xhci_err(xhci, "request interrupt %d failed\n",
452 pdev->irq);
453 return ret;
454 }
455 hcd->irq = pdev->irq;
456 }
457 511
458#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 512#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
459 init_timer(&xhci->event_ring_timer); 513 init_timer(&xhci->event_ring_timer);
@@ -694,7 +748,6 @@ int xhci_suspend(struct xhci_hcd *xhci)
694 int rc = 0; 748 int rc = 0;
695 struct usb_hcd *hcd = xhci_to_hcd(xhci); 749 struct usb_hcd *hcd = xhci_to_hcd(xhci);
696 u32 command; 750 u32 command;
697 int i;
698 751
699 spin_lock_irq(&xhci->lock); 752 spin_lock_irq(&xhci->lock);
700 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 753 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
@@ -730,10 +783,7 @@ int xhci_suspend(struct xhci_hcd *xhci)
730 783
731 /* step 5: remove core well power */ 784 /* step 5: remove core well power */
732 /* synchronize irq when using MSI-X */ 785 /* synchronize irq when using MSI-X */
733 if (xhci->msix_entries) { 786 xhci_msix_sync_irqs(xhci);
734 for (i = 0; i < xhci->msix_count; i++)
735 synchronize_irq(xhci->msix_entries[i].vector);
736 }
737 787
738 return rc; 788 return rc;
739} 789}
@@ -749,7 +799,7 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
749 u32 command, temp = 0; 799 u32 command, temp = 0;
750 struct usb_hcd *hcd = xhci_to_hcd(xhci); 800 struct usb_hcd *hcd = xhci_to_hcd(xhci);
751 struct usb_hcd *secondary_hcd; 801 struct usb_hcd *secondary_hcd;
752 int retval; 802 int retval = 0;
753 803
754 /* Wait a bit if either of the roothubs need to settle from the 804 /* Wait a bit if either of the roothubs need to settle from the
755 * transition into bus suspend. 805 * transition into bus suspend.
@@ -759,6 +809,9 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
759 xhci->bus_state[1].next_statechange)) 809 xhci->bus_state[1].next_statechange))
760 msleep(100); 810 msleep(100);
761 811
812 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
813 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
814
762 spin_lock_irq(&xhci->lock); 815 spin_lock_irq(&xhci->lock);
763 if (xhci->quirks & XHCI_RESET_ON_RESUME) 816 if (xhci->quirks & XHCI_RESET_ON_RESUME)
764 hibernated = true; 817 hibernated = true;
@@ -828,20 +881,13 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
828 return retval; 881 return retval;
829 xhci_dbg(xhci, "Start the primary HCD\n"); 882 xhci_dbg(xhci, "Start the primary HCD\n");
830 retval = xhci_run(hcd->primary_hcd); 883 retval = xhci_run(hcd->primary_hcd);
831 if (retval)
832 goto failed_restart;
833
834 xhci_dbg(xhci, "Start the secondary HCD\n");
835 retval = xhci_run(secondary_hcd);
836 if (!retval) { 884 if (!retval) {
837 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 885 xhci_dbg(xhci, "Start the secondary HCD\n");
838 set_bit(HCD_FLAG_HW_ACCESSIBLE, 886 retval = xhci_run(secondary_hcd);
839 &xhci->shared_hcd->flags);
840 } 887 }
841failed_restart:
842 hcd->state = HC_STATE_SUSPENDED; 888 hcd->state = HC_STATE_SUSPENDED;
843 xhci->shared_hcd->state = HC_STATE_SUSPENDED; 889 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
844 return retval; 890 goto done;
845 } 891 }
846 892
847 /* step 4: set Run/Stop bit */ 893 /* step 4: set Run/Stop bit */
@@ -860,11 +906,14 @@ failed_restart:
860 * Running endpoints by ringing their doorbells 906 * Running endpoints by ringing their doorbells
861 */ 907 */
862 908
863 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
864 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
865
866 spin_unlock_irq(&xhci->lock); 909 spin_unlock_irq(&xhci->lock);
867 return 0; 910
911 done:
912 if (retval == 0) {
913 usb_hcd_resume_root_hub(hcd);
914 usb_hcd_resume_root_hub(xhci->shared_hcd);
915 }
916 return retval;
868} 917}
869#endif /* CONFIG_PM */ 918#endif /* CONFIG_PM */
870 919
@@ -945,8 +994,7 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
945 return -ENODEV; 994 return -ENODEV;
946 995
947 if (check_virt_dev) { 996 if (check_virt_dev) {
948 if (!udev->slot_id || !xhci->devs 997 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
949 || !xhci->devs[udev->slot_id]) {
950 printk(KERN_DEBUG "xHCI %s called with unaddressed " 998 printk(KERN_DEBUG "xHCI %s called with unaddressed "
951 "device\n", func); 999 "device\n", func);
952 return -EINVAL; 1000 return -EINVAL;
@@ -987,7 +1035,7 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
987 out_ctx = xhci->devs[slot_id]->out_ctx; 1035 out_ctx = xhci->devs[slot_id]->out_ctx;
988 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1036 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
989 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); 1037 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
990 max_packet_size = le16_to_cpu(urb->dev->ep0.desc.wMaxPacketSize); 1038 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
991 if (hw_max_packet_size != max_packet_size) { 1039 if (hw_max_packet_size != max_packet_size) {
992 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n"); 1040 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
993 xhci_dbg(xhci, "Max packet size in usb_device = %d\n", 1041 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
@@ -1035,6 +1083,7 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1035int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) 1083int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1036{ 1084{
1037 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 1085 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1086 struct xhci_td *buffer;
1038 unsigned long flags; 1087 unsigned long flags;
1039 int ret = 0; 1088 int ret = 0;
1040 unsigned int slot_id, ep_index; 1089 unsigned int slot_id, ep_index;
@@ -1065,13 +1114,15 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1065 if (!urb_priv) 1114 if (!urb_priv)
1066 return -ENOMEM; 1115 return -ENOMEM;
1067 1116
1117 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1118 if (!buffer) {
1119 kfree(urb_priv);
1120 return -ENOMEM;
1121 }
1122
1068 for (i = 0; i < size; i++) { 1123 for (i = 0; i < size; i++) {
1069 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags); 1124 urb_priv->td[i] = buffer;
1070 if (!urb_priv->td[i]) { 1125 buffer++;
1071 urb_priv->length = i;
1072 xhci_urb_free_priv(xhci, urb_priv);
1073 return -ENOMEM;
1074 }
1075 } 1126 }
1076 1127
1077 urb_priv->length = size; 1128 urb_priv->length = size;
@@ -1747,6 +1798,564 @@ static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
1747 xhci->num_active_eps); 1798 xhci->num_active_eps);
1748} 1799}
1749 1800
1801unsigned int xhci_get_block_size(struct usb_device *udev)
1802{
1803 switch (udev->speed) {
1804 case USB_SPEED_LOW:
1805 case USB_SPEED_FULL:
1806 return FS_BLOCK;
1807 case USB_SPEED_HIGH:
1808 return HS_BLOCK;
1809 case USB_SPEED_SUPER:
1810 return SS_BLOCK;
1811 case USB_SPEED_UNKNOWN:
1812 case USB_SPEED_WIRELESS:
1813 default:
1814 /* Should never happen */
1815 return 1;
1816 }
1817}
1818
1819unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
1820{
1821 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
1822 return LS_OVERHEAD;
1823 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
1824 return FS_OVERHEAD;
1825 return HS_OVERHEAD;
1826}
1827
1828/* If we are changing a LS/FS device under a HS hub,
1829 * make sure (if we are activating a new TT) that the HS bus has enough
1830 * bandwidth for this new TT.
1831 */
1832static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
1833 struct xhci_virt_device *virt_dev,
1834 int old_active_eps)
1835{
1836 struct xhci_interval_bw_table *bw_table;
1837 struct xhci_tt_bw_info *tt_info;
1838
1839 /* Find the bandwidth table for the root port this TT is attached to. */
1840 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
1841 tt_info = virt_dev->tt_info;
1842 /* If this TT already had active endpoints, the bandwidth for this TT
1843 * has already been added. Removing all periodic endpoints (and thus
1844 * making the TT enactive) will only decrease the bandwidth used.
1845 */
1846 if (old_active_eps)
1847 return 0;
1848 if (old_active_eps == 0 && tt_info->active_eps != 0) {
1849 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
1850 return -ENOMEM;
1851 return 0;
1852 }
1853 /* Not sure why we would have no new active endpoints...
1854 *
1855 * Maybe because of an Evaluate Context change for a hub update or a
1856 * control endpoint 0 max packet size change?
1857 * FIXME: skip the bandwidth calculation in that case.
1858 */
1859 return 0;
1860}
1861
1862static int xhci_check_ss_bw(struct xhci_hcd *xhci,
1863 struct xhci_virt_device *virt_dev)
1864{
1865 unsigned int bw_reserved;
1866
1867 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
1868 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
1869 return -ENOMEM;
1870
1871 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
1872 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
1873 return -ENOMEM;
1874
1875 return 0;
1876}
1877
1878/*
1879 * This algorithm is a very conservative estimate of the worst-case scheduling
1880 * scenario for any one interval. The hardware dynamically schedules the
1881 * packets, so we can't tell which microframe could be the limiting factor in
1882 * the bandwidth scheduling. This only takes into account periodic endpoints.
1883 *
1884 * Obviously, we can't solve an NP complete problem to find the minimum worst
1885 * case scenario. Instead, we come up with an estimate that is no less than
1886 * the worst case bandwidth used for any one microframe, but may be an
1887 * over-estimate.
1888 *
1889 * We walk the requirements for each endpoint by interval, starting with the
1890 * smallest interval, and place packets in the schedule where there is only one
1891 * possible way to schedule packets for that interval. In order to simplify
1892 * this algorithm, we record the largest max packet size for each interval, and
1893 * assume all packets will be that size.
1894 *
1895 * For interval 0, we obviously must schedule all packets for each interval.
1896 * The bandwidth for interval 0 is just the amount of data to be transmitted
1897 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
1898 * the number of packets).
1899 *
1900 * For interval 1, we have two possible microframes to schedule those packets
1901 * in. For this algorithm, if we can schedule the same number of packets for
1902 * each possible scheduling opportunity (each microframe), we will do so. The
1903 * remaining number of packets will be saved to be transmitted in the gaps in
1904 * the next interval's scheduling sequence.
1905 *
1906 * As we move those remaining packets to be scheduled with interval 2 packets,
1907 * we have to double the number of remaining packets to transmit. This is
1908 * because the intervals are actually powers of 2, and we would be transmitting
1909 * the previous interval's packets twice in this interval. We also have to be
1910 * sure that when we look at the largest max packet size for this interval, we
1911 * also look at the largest max packet size for the remaining packets and take
1912 * the greater of the two.
1913 *
1914 * The algorithm continues to evenly distribute packets in each scheduling
1915 * opportunity, and push the remaining packets out, until we get to the last
1916 * interval. Then those packets and their associated overhead are just added
1917 * to the bandwidth used.
1918 */
1919static int xhci_check_bw_table(struct xhci_hcd *xhci,
1920 struct xhci_virt_device *virt_dev,
1921 int old_active_eps)
1922{
1923 unsigned int bw_reserved;
1924 unsigned int max_bandwidth;
1925 unsigned int bw_used;
1926 unsigned int block_size;
1927 struct xhci_interval_bw_table *bw_table;
1928 unsigned int packet_size = 0;
1929 unsigned int overhead = 0;
1930 unsigned int packets_transmitted = 0;
1931 unsigned int packets_remaining = 0;
1932 unsigned int i;
1933
1934 if (virt_dev->udev->speed == USB_SPEED_SUPER)
1935 return xhci_check_ss_bw(xhci, virt_dev);
1936
1937 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
1938 max_bandwidth = HS_BW_LIMIT;
1939 /* Convert percent of bus BW reserved to blocks reserved */
1940 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
1941 } else {
1942 max_bandwidth = FS_BW_LIMIT;
1943 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
1944 }
1945
1946 bw_table = virt_dev->bw_table;
1947 /* We need to translate the max packet size and max ESIT payloads into
1948 * the units the hardware uses.
1949 */
1950 block_size = xhci_get_block_size(virt_dev->udev);
1951
1952 /* If we are manipulating a LS/FS device under a HS hub, double check
1953 * that the HS bus has enough bandwidth if we are activing a new TT.
1954 */
1955 if (virt_dev->tt_info) {
1956 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
1957 virt_dev->real_port);
1958 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
1959 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
1960 "newly activated TT.\n");
1961 return -ENOMEM;
1962 }
1963 xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
1964 virt_dev->tt_info->slot_id,
1965 virt_dev->tt_info->ttport);
1966 } else {
1967 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
1968 virt_dev->real_port);
1969 }
1970
1971 /* Add in how much bandwidth will be used for interval zero, or the
1972 * rounded max ESIT payload + number of packets * largest overhead.
1973 */
1974 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
1975 bw_table->interval_bw[0].num_packets *
1976 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
1977
1978 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
1979 unsigned int bw_added;
1980 unsigned int largest_mps;
1981 unsigned int interval_overhead;
1982
1983 /*
1984 * How many packets could we transmit in this interval?
1985 * If packets didn't fit in the previous interval, we will need
1986 * to transmit that many packets twice within this interval.
1987 */
1988 packets_remaining = 2 * packets_remaining +
1989 bw_table->interval_bw[i].num_packets;
1990
1991 /* Find the largest max packet size of this or the previous
1992 * interval.
1993 */
1994 if (list_empty(&bw_table->interval_bw[i].endpoints))
1995 largest_mps = 0;
1996 else {
1997 struct xhci_virt_ep *virt_ep;
1998 struct list_head *ep_entry;
1999
2000 ep_entry = bw_table->interval_bw[i].endpoints.next;
2001 virt_ep = list_entry(ep_entry,
2002 struct xhci_virt_ep, bw_endpoint_list);
2003 /* Convert to blocks, rounding up */
2004 largest_mps = DIV_ROUND_UP(
2005 virt_ep->bw_info.max_packet_size,
2006 block_size);
2007 }
2008 if (largest_mps > packet_size)
2009 packet_size = largest_mps;
2010
2011 /* Use the larger overhead of this or the previous interval. */
2012 interval_overhead = xhci_get_largest_overhead(
2013 &bw_table->interval_bw[i]);
2014 if (interval_overhead > overhead)
2015 overhead = interval_overhead;
2016
2017 /* How many packets can we evenly distribute across
2018 * (1 << (i + 1)) possible scheduling opportunities?
2019 */
2020 packets_transmitted = packets_remaining >> (i + 1);
2021
2022 /* Add in the bandwidth used for those scheduled packets */
2023 bw_added = packets_transmitted * (overhead + packet_size);
2024
2025 /* How many packets do we have remaining to transmit? */
2026 packets_remaining = packets_remaining % (1 << (i + 1));
2027
2028 /* What largest max packet size should those packets have? */
2029 /* If we've transmitted all packets, don't carry over the
2030 * largest packet size.
2031 */
2032 if (packets_remaining == 0) {
2033 packet_size = 0;
2034 overhead = 0;
2035 } else if (packets_transmitted > 0) {
2036 /* Otherwise if we do have remaining packets, and we've
2037 * scheduled some packets in this interval, take the
2038 * largest max packet size from endpoints with this
2039 * interval.
2040 */
2041 packet_size = largest_mps;
2042 overhead = interval_overhead;
2043 }
2044 /* Otherwise carry over packet_size and overhead from the last
2045 * time we had a remainder.
2046 */
2047 bw_used += bw_added;
2048 if (bw_used > max_bandwidth) {
2049 xhci_warn(xhci, "Not enough bandwidth. "
2050 "Proposed: %u, Max: %u\n",
2051 bw_used, max_bandwidth);
2052 return -ENOMEM;
2053 }
2054 }
2055 /*
2056 * Ok, we know we have some packets left over after even-handedly
2057 * scheduling interval 15. We don't know which microframes they will
2058 * fit into, so we over-schedule and say they will be scheduled every
2059 * microframe.
2060 */
2061 if (packets_remaining > 0)
2062 bw_used += overhead + packet_size;
2063
2064 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2065 unsigned int port_index = virt_dev->real_port - 1;
2066
2067 /* OK, we're manipulating a HS device attached to a
2068 * root port bandwidth domain. Include the number of active TTs
2069 * in the bandwidth used.
2070 */
2071 bw_used += TT_HS_OVERHEAD *
2072 xhci->rh_bw[port_index].num_active_tts;
2073 }
2074
2075 xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2076 "Available: %u " "percent\n",
2077 bw_used, max_bandwidth, bw_reserved,
2078 (max_bandwidth - bw_used - bw_reserved) * 100 /
2079 max_bandwidth);
2080
2081 bw_used += bw_reserved;
2082 if (bw_used > max_bandwidth) {
2083 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2084 bw_used, max_bandwidth);
2085 return -ENOMEM;
2086 }
2087
2088 bw_table->bw_used = bw_used;
2089 return 0;
2090}
2091
2092static bool xhci_is_async_ep(unsigned int ep_type)
2093{
2094 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2095 ep_type != ISOC_IN_EP &&
2096 ep_type != INT_IN_EP);
2097}
2098
2099static bool xhci_is_sync_in_ep(unsigned int ep_type)
2100{
2101 return (ep_type == ISOC_IN_EP || ep_type != INT_IN_EP);
2102}
2103
2104static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2105{
2106 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2107
2108 if (ep_bw->ep_interval == 0)
2109 return SS_OVERHEAD_BURST +
2110 (ep_bw->mult * ep_bw->num_packets *
2111 (SS_OVERHEAD + mps));
2112 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2113 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2114 1 << ep_bw->ep_interval);
2115
2116}
2117
2118void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2119 struct xhci_bw_info *ep_bw,
2120 struct xhci_interval_bw_table *bw_table,
2121 struct usb_device *udev,
2122 struct xhci_virt_ep *virt_ep,
2123 struct xhci_tt_bw_info *tt_info)
2124{
2125 struct xhci_interval_bw *interval_bw;
2126 int normalized_interval;
2127
2128 if (xhci_is_async_ep(ep_bw->type))
2129 return;
2130
2131 if (udev->speed == USB_SPEED_SUPER) {
2132 if (xhci_is_sync_in_ep(ep_bw->type))
2133 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2134 xhci_get_ss_bw_consumed(ep_bw);
2135 else
2136 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2137 xhci_get_ss_bw_consumed(ep_bw);
2138 return;
2139 }
2140
2141 /* SuperSpeed endpoints never get added to intervals in the table, so
2142 * this check is only valid for HS/FS/LS devices.
2143 */
2144 if (list_empty(&virt_ep->bw_endpoint_list))
2145 return;
2146 /* For LS/FS devices, we need to translate the interval expressed in
2147 * microframes to frames.
2148 */
2149 if (udev->speed == USB_SPEED_HIGH)
2150 normalized_interval = ep_bw->ep_interval;
2151 else
2152 normalized_interval = ep_bw->ep_interval - 3;
2153
2154 if (normalized_interval == 0)
2155 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2156 interval_bw = &bw_table->interval_bw[normalized_interval];
2157 interval_bw->num_packets -= ep_bw->num_packets;
2158 switch (udev->speed) {
2159 case USB_SPEED_LOW:
2160 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2161 break;
2162 case USB_SPEED_FULL:
2163 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2164 break;
2165 case USB_SPEED_HIGH:
2166 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2167 break;
2168 case USB_SPEED_SUPER:
2169 case USB_SPEED_UNKNOWN:
2170 case USB_SPEED_WIRELESS:
2171 /* Should never happen because only LS/FS/HS endpoints will get
2172 * added to the endpoint list.
2173 */
2174 return;
2175 }
2176 if (tt_info)
2177 tt_info->active_eps -= 1;
2178 list_del_init(&virt_ep->bw_endpoint_list);
2179}
2180
2181static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2182 struct xhci_bw_info *ep_bw,
2183 struct xhci_interval_bw_table *bw_table,
2184 struct usb_device *udev,
2185 struct xhci_virt_ep *virt_ep,
2186 struct xhci_tt_bw_info *tt_info)
2187{
2188 struct xhci_interval_bw *interval_bw;
2189 struct xhci_virt_ep *smaller_ep;
2190 int normalized_interval;
2191
2192 if (xhci_is_async_ep(ep_bw->type))
2193 return;
2194
2195 if (udev->speed == USB_SPEED_SUPER) {
2196 if (xhci_is_sync_in_ep(ep_bw->type))
2197 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2198 xhci_get_ss_bw_consumed(ep_bw);
2199 else
2200 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2201 xhci_get_ss_bw_consumed(ep_bw);
2202 return;
2203 }
2204
2205 /* For LS/FS devices, we need to translate the interval expressed in
2206 * microframes to frames.
2207 */
2208 if (udev->speed == USB_SPEED_HIGH)
2209 normalized_interval = ep_bw->ep_interval;
2210 else
2211 normalized_interval = ep_bw->ep_interval - 3;
2212
2213 if (normalized_interval == 0)
2214 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2215 interval_bw = &bw_table->interval_bw[normalized_interval];
2216 interval_bw->num_packets += ep_bw->num_packets;
2217 switch (udev->speed) {
2218 case USB_SPEED_LOW:
2219 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2220 break;
2221 case USB_SPEED_FULL:
2222 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2223 break;
2224 case USB_SPEED_HIGH:
2225 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2226 break;
2227 case USB_SPEED_SUPER:
2228 case USB_SPEED_UNKNOWN:
2229 case USB_SPEED_WIRELESS:
2230 /* Should never happen because only LS/FS/HS endpoints will get
2231 * added to the endpoint list.
2232 */
2233 return;
2234 }
2235
2236 if (tt_info)
2237 tt_info->active_eps += 1;
2238 /* Insert the endpoint into the list, largest max packet size first. */
2239 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2240 bw_endpoint_list) {
2241 if (ep_bw->max_packet_size >=
2242 smaller_ep->bw_info.max_packet_size) {
2243 /* Add the new ep before the smaller endpoint */
2244 list_add_tail(&virt_ep->bw_endpoint_list,
2245 &smaller_ep->bw_endpoint_list);
2246 return;
2247 }
2248 }
2249 /* Add the new endpoint at the end of the list. */
2250 list_add_tail(&virt_ep->bw_endpoint_list,
2251 &interval_bw->endpoints);
2252}
2253
2254void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2255 struct xhci_virt_device *virt_dev,
2256 int old_active_eps)
2257{
2258 struct xhci_root_port_bw_info *rh_bw_info;
2259 if (!virt_dev->tt_info)
2260 return;
2261
2262 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2263 if (old_active_eps == 0 &&
2264 virt_dev->tt_info->active_eps != 0) {
2265 rh_bw_info->num_active_tts += 1;
2266 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
2267 } else if (old_active_eps != 0 &&
2268 virt_dev->tt_info->active_eps == 0) {
2269 rh_bw_info->num_active_tts -= 1;
2270 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
2271 }
2272}
2273
2274static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2275 struct xhci_virt_device *virt_dev,
2276 struct xhci_container_ctx *in_ctx)
2277{
2278 struct xhci_bw_info ep_bw_info[31];
2279 int i;
2280 struct xhci_input_control_ctx *ctrl_ctx;
2281 int old_active_eps = 0;
2282
2283 if (virt_dev->tt_info)
2284 old_active_eps = virt_dev->tt_info->active_eps;
2285
2286 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2287
2288 for (i = 0; i < 31; i++) {
2289 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2290 continue;
2291
2292 /* Make a copy of the BW info in case we need to revert this */
2293 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2294 sizeof(ep_bw_info[i]));
2295 /* Drop the endpoint from the interval table if the endpoint is
2296 * being dropped or changed.
2297 */
2298 if (EP_IS_DROPPED(ctrl_ctx, i))
2299 xhci_drop_ep_from_interval_table(xhci,
2300 &virt_dev->eps[i].bw_info,
2301 virt_dev->bw_table,
2302 virt_dev->udev,
2303 &virt_dev->eps[i],
2304 virt_dev->tt_info);
2305 }
2306 /* Overwrite the information stored in the endpoints' bw_info */
2307 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2308 for (i = 0; i < 31; i++) {
2309 /* Add any changed or added endpoints to the interval table */
2310 if (EP_IS_ADDED(ctrl_ctx, i))
2311 xhci_add_ep_to_interval_table(xhci,
2312 &virt_dev->eps[i].bw_info,
2313 virt_dev->bw_table,
2314 virt_dev->udev,
2315 &virt_dev->eps[i],
2316 virt_dev->tt_info);
2317 }
2318
2319 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2320 /* Ok, this fits in the bandwidth we have.
2321 * Update the number of active TTs.
2322 */
2323 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2324 return 0;
2325 }
2326
2327 /* We don't have enough bandwidth for this, revert the stored info. */
2328 for (i = 0; i < 31; i++) {
2329 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2330 continue;
2331
2332 /* Drop the new copies of any added or changed endpoints from
2333 * the interval table.
2334 */
2335 if (EP_IS_ADDED(ctrl_ctx, i)) {
2336 xhci_drop_ep_from_interval_table(xhci,
2337 &virt_dev->eps[i].bw_info,
2338 virt_dev->bw_table,
2339 virt_dev->udev,
2340 &virt_dev->eps[i],
2341 virt_dev->tt_info);
2342 }
2343 /* Revert the endpoint back to its old information */
2344 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2345 sizeof(ep_bw_info[i]));
2346 /* Add any changed or dropped endpoints back into the table */
2347 if (EP_IS_DROPPED(ctrl_ctx, i))
2348 xhci_add_ep_to_interval_table(xhci,
2349 &virt_dev->eps[i].bw_info,
2350 virt_dev->bw_table,
2351 virt_dev->udev,
2352 &virt_dev->eps[i],
2353 virt_dev->tt_info);
2354 }
2355 return -ENOMEM;
2356}
2357
2358
1750/* Issue a configure endpoint command or evaluate context command 2359/* Issue a configure endpoint command or evaluate context command
1751 * and wait for it to finish. 2360 * and wait for it to finish.
1752 */ 2361 */
@@ -1765,17 +2374,30 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1765 2374
1766 spin_lock_irqsave(&xhci->lock, flags); 2375 spin_lock_irqsave(&xhci->lock, flags);
1767 virt_dev = xhci->devs[udev->slot_id]; 2376 virt_dev = xhci->devs[udev->slot_id];
1768 if (command) { 2377
2378 if (command)
1769 in_ctx = command->in_ctx; 2379 in_ctx = command->in_ctx;
1770 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) && 2380 else
1771 xhci_reserve_host_resources(xhci, in_ctx)) { 2381 in_ctx = virt_dev->in_ctx;
1772 spin_unlock_irqrestore(&xhci->lock, flags);
1773 xhci_warn(xhci, "Not enough host resources, "
1774 "active endpoint contexts = %u\n",
1775 xhci->num_active_eps);
1776 return -ENOMEM;
1777 }
1778 2382
2383 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2384 xhci_reserve_host_resources(xhci, in_ctx)) {
2385 spin_unlock_irqrestore(&xhci->lock, flags);
2386 xhci_warn(xhci, "Not enough host resources, "
2387 "active endpoint contexts = %u\n",
2388 xhci->num_active_eps);
2389 return -ENOMEM;
2390 }
2391 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2392 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2393 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2394 xhci_free_host_resources(xhci, in_ctx);
2395 spin_unlock_irqrestore(&xhci->lock, flags);
2396 xhci_warn(xhci, "Not enough bandwidth\n");
2397 return -ENOMEM;
2398 }
2399
2400 if (command) {
1779 cmd_completion = command->completion; 2401 cmd_completion = command->completion;
1780 cmd_status = &command->status; 2402 cmd_status = &command->status;
1781 command->command_trb = xhci->cmd_ring->enqueue; 2403 command->command_trb = xhci->cmd_ring->enqueue;
@@ -1789,15 +2411,6 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1789 2411
1790 list_add_tail(&command->cmd_list, &virt_dev->cmd_list); 2412 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1791 } else { 2413 } else {
1792 in_ctx = virt_dev->in_ctx;
1793 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
1794 xhci_reserve_host_resources(xhci, in_ctx)) {
1795 spin_unlock_irqrestore(&xhci->lock, flags);
1796 xhci_warn(xhci, "Not enough host resources, "
1797 "active endpoint contexts = %u\n",
1798 xhci->num_active_eps);
1799 return -ENOMEM;
1800 }
1801 cmd_completion = &virt_dev->cmd_completion; 2414 cmd_completion = &virt_dev->cmd_completion;
1802 cmd_status = &virt_dev->cmd_status; 2415 cmd_status = &virt_dev->cmd_status;
1803 } 2416 }
@@ -1888,6 +2501,12 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1888 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 2501 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
1889 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); 2502 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
1890 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); 2503 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
2504
2505 /* Don't issue the command if there's no endpoints to update. */
2506 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2507 ctrl_ctx->drop_flags == 0)
2508 return 0;
2509
1891 xhci_dbg(xhci, "New Input Control Context:\n"); 2510 xhci_dbg(xhci, "New Input Control Context:\n");
1892 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 2511 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1893 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2512 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
@@ -2525,6 +3144,7 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
2525 int timeleft; 3144 int timeleft;
2526 int last_freed_endpoint; 3145 int last_freed_endpoint;
2527 struct xhci_slot_ctx *slot_ctx; 3146 struct xhci_slot_ctx *slot_ctx;
3147 int old_active_eps = 0;
2528 3148
2529 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__); 3149 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
2530 if (ret <= 0) 3150 if (ret <= 0)
@@ -2666,7 +3286,18 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
2666 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); 3286 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2667 last_freed_endpoint = i; 3287 last_freed_endpoint = i;
2668 } 3288 }
2669 } 3289 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3290 xhci_drop_ep_from_interval_table(xhci,
3291 &virt_dev->eps[i].bw_info,
3292 virt_dev->bw_table,
3293 udev,
3294 &virt_dev->eps[i],
3295 virt_dev->tt_info);
3296 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
3297 }
3298 /* If necessary, update the number of active TTs on this root port */
3299 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3300
2670 xhci_dbg(xhci, "Output context after successful reset device cmd:\n"); 3301 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2671 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint); 3302 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2672 ret = 0; 3303 ret = 0;
@@ -2704,6 +3335,11 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2704 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); 3335 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2705 } 3336 }
2706 3337
3338 if (udev->usb2_hw_lpm_enabled) {
3339 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3340 udev->usb2_hw_lpm_enabled = 0;
3341 }
3342
2707 spin_lock_irqsave(&xhci->lock, flags); 3343 spin_lock_irqsave(&xhci->lock, flags);
2708 /* Don't disable the slot if the host controller is dead. */ 3344 /* Don't disable the slot if the host controller is dead. */
2709 state = xhci_readl(xhci, &xhci->op_regs->status); 3345 state = xhci_readl(xhci, &xhci->op_regs->status);
@@ -2867,6 +3503,10 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2867 /* Otherwise, update the control endpoint ring enqueue pointer. */ 3503 /* Otherwise, update the control endpoint ring enqueue pointer. */
2868 else 3504 else
2869 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); 3505 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
3506 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3507 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3508 ctrl_ctx->drop_flags = 0;
3509
2870 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); 3510 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
2871 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); 3511 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
2872 3512
@@ -2889,7 +3529,7 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2889 * command on a timeout. 3529 * command on a timeout.
2890 */ 3530 */
2891 if (timeleft <= 0) { 3531 if (timeleft <= 0) {
2892 xhci_warn(xhci, "%s while waiting for a slot\n", 3532 xhci_warn(xhci, "%s while waiting for address device command\n",
2893 timeleft == 0 ? "Timeout" : "Signal"); 3533 timeleft == 0 ? "Timeout" : "Signal");
2894 /* FIXME cancel the address device command */ 3534 /* FIXME cancel the address device command */
2895 return -ETIME; 3535 return -ETIME;
@@ -2948,7 +3588,6 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2948 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK) 3588 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
2949 + 1; 3589 + 1;
2950 /* Zero the input context control for later use */ 3590 /* Zero the input context control for later use */
2951 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2952 ctrl_ctx->add_flags = 0; 3591 ctrl_ctx->add_flags = 0;
2953 ctrl_ctx->drop_flags = 0; 3592 ctrl_ctx->drop_flags = 0;
2954 3593
@@ -2957,6 +3596,254 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2957 return 0; 3596 return 0;
2958} 3597}
2959 3598
3599#ifdef CONFIG_USB_SUSPEND
3600
3601/* BESL to HIRD Encoding array for USB2 LPM */
3602static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3603 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3604
3605/* Calculate HIRD/BESL for USB2 PORTPMSC*/
3606static int xhci_calculate_hird_besl(int u2del, bool use_besl)
3607{
3608 int hird;
3609
3610 if (use_besl) {
3611 for (hird = 0; hird < 16; hird++) {
3612 if (xhci_besl_encoding[hird] >= u2del)
3613 break;
3614 }
3615 } else {
3616 if (u2del <= 50)
3617 hird = 0;
3618 else
3619 hird = (u2del - 51) / 75 + 1;
3620
3621 if (hird > 15)
3622 hird = 15;
3623 }
3624
3625 return hird;
3626}
3627
3628static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
3629 struct usb_device *udev)
3630{
3631 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3632 struct dev_info *dev_info;
3633 __le32 __iomem **port_array;
3634 __le32 __iomem *addr, *pm_addr;
3635 u32 temp, dev_id;
3636 unsigned int port_num;
3637 unsigned long flags;
3638 int u2del, hird;
3639 int ret;
3640
3641 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
3642 !udev->lpm_capable)
3643 return -EINVAL;
3644
3645 /* we only support lpm for non-hub device connected to root hub yet */
3646 if (!udev->parent || udev->parent->parent ||
3647 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3648 return -EINVAL;
3649
3650 spin_lock_irqsave(&xhci->lock, flags);
3651
3652 /* Look for devices in lpm_failed_devs list */
3653 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
3654 le16_to_cpu(udev->descriptor.idProduct);
3655 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
3656 if (dev_info->dev_id == dev_id) {
3657 ret = -EINVAL;
3658 goto finish;
3659 }
3660 }
3661
3662 port_array = xhci->usb2_ports;
3663 port_num = udev->portnum - 1;
3664
3665 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
3666 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
3667 ret = -EINVAL;
3668 goto finish;
3669 }
3670
3671 /*
3672 * Test USB 2.0 software LPM.
3673 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
3674 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
3675 * in the June 2011 errata release.
3676 */
3677 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
3678 /*
3679 * Set L1 Device Slot and HIRD/BESL.
3680 * Check device's USB 2.0 extension descriptor to determine whether
3681 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
3682 */
3683 pm_addr = port_array[port_num] + 1;
3684 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3685 if (le32_to_cpu(udev->bos->ext_cap->bmAttributes) & (1 << 2))
3686 hird = xhci_calculate_hird_besl(u2del, 1);
3687 else
3688 hird = xhci_calculate_hird_besl(u2del, 0);
3689
3690 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
3691 xhci_writel(xhci, temp, pm_addr);
3692
3693 /* Set port link state to U2(L1) */
3694 addr = port_array[port_num];
3695 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
3696
3697 /* wait for ACK */
3698 spin_unlock_irqrestore(&xhci->lock, flags);
3699 msleep(10);
3700 spin_lock_irqsave(&xhci->lock, flags);
3701
3702 /* Check L1 Status */
3703 ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
3704 if (ret != -ETIMEDOUT) {
3705 /* enter L1 successfully */
3706 temp = xhci_readl(xhci, addr);
3707 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
3708 port_num, temp);
3709 ret = 0;
3710 } else {
3711 temp = xhci_readl(xhci, pm_addr);
3712 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
3713 port_num, temp & PORT_L1S_MASK);
3714 ret = -EINVAL;
3715 }
3716
3717 /* Resume the port */
3718 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
3719
3720 spin_unlock_irqrestore(&xhci->lock, flags);
3721 msleep(10);
3722 spin_lock_irqsave(&xhci->lock, flags);
3723
3724 /* Clear PLC */
3725 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
3726
3727 /* Check PORTSC to make sure the device is in the right state */
3728 if (!ret) {
3729 temp = xhci_readl(xhci, addr);
3730 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
3731 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
3732 (temp & PORT_PLS_MASK) != XDEV_U0) {
3733 xhci_dbg(xhci, "port L1 resume fail\n");
3734 ret = -EINVAL;
3735 }
3736 }
3737
3738 if (ret) {
3739 /* Insert dev to lpm_failed_devs list */
3740 xhci_warn(xhci, "device LPM test failed, may disconnect and "
3741 "re-enumerate\n");
3742 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
3743 if (!dev_info) {
3744 ret = -ENOMEM;
3745 goto finish;
3746 }
3747 dev_info->dev_id = dev_id;
3748 INIT_LIST_HEAD(&dev_info->list);
3749 list_add(&dev_info->list, &xhci->lpm_failed_devs);
3750 } else {
3751 xhci_ring_device(xhci, udev->slot_id);
3752 }
3753
3754finish:
3755 spin_unlock_irqrestore(&xhci->lock, flags);
3756 return ret;
3757}
3758
3759int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
3760 struct usb_device *udev, int enable)
3761{
3762 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3763 __le32 __iomem **port_array;
3764 __le32 __iomem *pm_addr;
3765 u32 temp;
3766 unsigned int port_num;
3767 unsigned long flags;
3768 int u2del, hird;
3769
3770 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
3771 !udev->lpm_capable)
3772 return -EPERM;
3773
3774 if (!udev->parent || udev->parent->parent ||
3775 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3776 return -EPERM;
3777
3778 if (udev->usb2_hw_lpm_capable != 1)
3779 return -EPERM;
3780
3781 spin_lock_irqsave(&xhci->lock, flags);
3782
3783 port_array = xhci->usb2_ports;
3784 port_num = udev->portnum - 1;
3785 pm_addr = port_array[port_num] + 1;
3786 temp = xhci_readl(xhci, pm_addr);
3787
3788 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
3789 enable ? "enable" : "disable", port_num);
3790
3791 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3792 if (le32_to_cpu(udev->bos->ext_cap->bmAttributes) & (1 << 2))
3793 hird = xhci_calculate_hird_besl(u2del, 1);
3794 else
3795 hird = xhci_calculate_hird_besl(u2del, 0);
3796
3797 if (enable) {
3798 temp &= ~PORT_HIRD_MASK;
3799 temp |= PORT_HIRD(hird) | PORT_RWE;
3800 xhci_writel(xhci, temp, pm_addr);
3801 temp = xhci_readl(xhci, pm_addr);
3802 temp |= PORT_HLE;
3803 xhci_writel(xhci, temp, pm_addr);
3804 } else {
3805 temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
3806 xhci_writel(xhci, temp, pm_addr);
3807 }
3808
3809 spin_unlock_irqrestore(&xhci->lock, flags);
3810 return 0;
3811}
3812
3813int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
3814{
3815 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3816 int ret;
3817
3818 ret = xhci_usb2_software_lpm_test(hcd, udev);
3819 if (!ret) {
3820 xhci_dbg(xhci, "software LPM test succeed\n");
3821 if (xhci->hw_lpm_support == 1) {
3822 udev->usb2_hw_lpm_capable = 1;
3823 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
3824 if (!ret)
3825 udev->usb2_hw_lpm_enabled = 1;
3826 }
3827 }
3828
3829 return 0;
3830}
3831
3832#else
3833
3834int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
3835 struct usb_device *udev, int enable)
3836{
3837 return 0;
3838}
3839
3840int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
3841{
3842 return 0;
3843}
3844
3845#endif /* CONFIG_USB_SUSPEND */
3846
2960/* Once a hub descriptor is fetched for a device, we need to update the xHC's 3847/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2961 * internal data structures for the device. 3848 * internal data structures for the device.
2962 */ 3849 */
@@ -2988,6 +3875,14 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2988 } 3875 }
2989 3876
2990 spin_lock_irqsave(&xhci->lock, flags); 3877 spin_lock_irqsave(&xhci->lock, flags);
3878 if (hdev->speed == USB_SPEED_HIGH &&
3879 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
3880 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
3881 xhci_free_command(xhci, config_cmd);
3882 spin_unlock_irqrestore(&xhci->lock, flags);
3883 return -ENOMEM;
3884 }
3885
2991 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); 3886 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2992 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx); 3887 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2993 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 3888 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
@@ -3051,22 +3946,108 @@ int xhci_get_frame(struct usb_hcd *hcd)
3051 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3; 3946 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
3052} 3947}
3053 3948
3949int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
3950{
3951 struct xhci_hcd *xhci;
3952 struct device *dev = hcd->self.controller;
3953 int retval;
3954 u32 temp;
3955
3956 hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 2;
3957
3958 if (usb_hcd_is_primary_hcd(hcd)) {
3959 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
3960 if (!xhci)
3961 return -ENOMEM;
3962 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
3963 xhci->main_hcd = hcd;
3964 /* Mark the first roothub as being USB 2.0.
3965 * The xHCI driver will register the USB 3.0 roothub.
3966 */
3967 hcd->speed = HCD_USB2;
3968 hcd->self.root_hub->speed = USB_SPEED_HIGH;
3969 /*
3970 * USB 2.0 roothub under xHCI has an integrated TT,
3971 * (rate matching hub) as opposed to having an OHCI/UHCI
3972 * companion controller.
3973 */
3974 hcd->has_tt = 1;
3975 } else {
3976 /* xHCI private pointer was set in xhci_pci_probe for the second
3977 * registered roothub.
3978 */
3979 xhci = hcd_to_xhci(hcd);
3980 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
3981 if (HCC_64BIT_ADDR(temp)) {
3982 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
3983 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
3984 } else {
3985 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
3986 }
3987 return 0;
3988 }
3989
3990 xhci->cap_regs = hcd->regs;
3991 xhci->op_regs = hcd->regs +
3992 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
3993 xhci->run_regs = hcd->regs +
3994 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
3995 /* Cache read-only capability registers */
3996 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
3997 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
3998 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
3999 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4000 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4001 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4002 xhci_print_registers(xhci);
4003
4004 get_quirks(dev, xhci);
4005
4006 /* Make sure the HC is halted. */
4007 retval = xhci_halt(xhci);
4008 if (retval)
4009 goto error;
4010
4011 xhci_dbg(xhci, "Resetting HCD\n");
4012 /* Reset the internal HC memory state and registers. */
4013 retval = xhci_reset(xhci);
4014 if (retval)
4015 goto error;
4016 xhci_dbg(xhci, "Reset complete\n");
4017
4018 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4019 if (HCC_64BIT_ADDR(temp)) {
4020 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4021 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4022 } else {
4023 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4024 }
4025
4026 xhci_dbg(xhci, "Calling HCD init\n");
4027 /* Initialize HCD and host controller data structures. */
4028 retval = xhci_init(hcd);
4029 if (retval)
4030 goto error;
4031 xhci_dbg(xhci, "Called HCD init\n");
4032 return 0;
4033error:
4034 kfree(xhci);
4035 return retval;
4036}
4037
3054MODULE_DESCRIPTION(DRIVER_DESC); 4038MODULE_DESCRIPTION(DRIVER_DESC);
3055MODULE_AUTHOR(DRIVER_AUTHOR); 4039MODULE_AUTHOR(DRIVER_AUTHOR);
3056MODULE_LICENSE("GPL"); 4040MODULE_LICENSE("GPL");
3057 4041
3058static int __init xhci_hcd_init(void) 4042static int __init xhci_hcd_init(void)
3059{ 4043{
3060#ifdef CONFIG_PCI 4044 int retval;
3061 int retval = 0;
3062 4045
3063 retval = xhci_register_pci(); 4046 retval = xhci_register_pci();
3064
3065 if (retval < 0) { 4047 if (retval < 0) {
3066 printk(KERN_DEBUG "Problem registering PCI driver."); 4048 printk(KERN_DEBUG "Problem registering PCI driver.");
3067 return retval; 4049 return retval;
3068 } 4050 }
3069#endif
3070 /* 4051 /*
3071 * Check the compiler generated sizes of structures that must be laid 4052 * Check the compiler generated sizes of structures that must be laid
3072 * out in specific ways for hardware access. 4053 * out in specific ways for hardware access.
@@ -3091,8 +4072,6 @@ module_init(xhci_hcd_init);
3091 4072
3092static void __exit xhci_hcd_cleanup(void) 4073static void __exit xhci_hcd_cleanup(void)
3093{ 4074{
3094#ifdef CONFIG_PCI
3095 xhci_unregister_pci(); 4075 xhci_unregister_pci();
3096#endif
3097} 4076}
3098module_exit(xhci_hcd_cleanup); 4077module_exit(xhci_hcd_cleanup);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index cae8e23308bf..3c8fbd2772ea 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -272,6 +272,7 @@ struct xhci_op_regs {
272 */ 272 */
273#define PORT_PLS_MASK (0xf << 5) 273#define PORT_PLS_MASK (0xf << 5)
274#define XDEV_U0 (0x0 << 5) 274#define XDEV_U0 (0x0 << 5)
275#define XDEV_U2 (0x2 << 5)
275#define XDEV_U3 (0x3 << 5) 276#define XDEV_U3 (0x3 << 5)
276#define XDEV_RESUME (0xf << 5) 277#define XDEV_RESUME (0xf << 5)
277/* true: port has power (see HCC_PPC) */ 278/* true: port has power (see HCC_PPC) */
@@ -362,7 +363,13 @@ struct xhci_op_regs {
362/* Bits 24:31 for port testing */ 363/* Bits 24:31 for port testing */
363 364
364/* USB2 Protocol PORTSPMSC */ 365/* USB2 Protocol PORTSPMSC */
365#define PORT_RWE (1 << 0x3) 366#define PORT_L1S_MASK 7
367#define PORT_L1S_SUCCESS 1
368#define PORT_RWE (1 << 3)
369#define PORT_HIRD(p) (((p) & 0xf) << 4)
370#define PORT_HIRD_MASK (0xf << 4)
371#define PORT_L1DS(p) (((p) & 0xff) << 8)
372#define PORT_HLE (1 << 16)
366 373
367/** 374/**
368 * struct xhci_intr_reg - Interrupt Register Set 375 * struct xhci_intr_reg - Interrupt Register Set
@@ -611,11 +618,13 @@ struct xhci_ep_ctx {
611#define EP_STATE_ERROR 4 618#define EP_STATE_ERROR 4
612/* Mult - Max number of burtst within an interval, in EP companion desc. */ 619/* Mult - Max number of burtst within an interval, in EP companion desc. */
613#define EP_MULT(p) (((p) & 0x3) << 8) 620#define EP_MULT(p) (((p) & 0x3) << 8)
621#define CTX_TO_EP_MULT(p) (((p) >> 8) & 0x3)
614/* bits 10:14 are Max Primary Streams */ 622/* bits 10:14 are Max Primary Streams */
615/* bit 15 is Linear Stream Array */ 623/* bit 15 is Linear Stream Array */
616/* Interval - period between requests to an endpoint - 125u increments. */ 624/* Interval - period between requests to an endpoint - 125u increments. */
617#define EP_INTERVAL(p) (((p) & 0xff) << 16) 625#define EP_INTERVAL(p) (((p) & 0xff) << 16)
618#define EP_INTERVAL_TO_UFRAMES(p) (1 << (((p) >> 16) & 0xff)) 626#define EP_INTERVAL_TO_UFRAMES(p) (1 << (((p) >> 16) & 0xff))
627#define CTX_TO_EP_INTERVAL(p) (((p) >> 16) & 0xff)
619#define EP_MAXPSTREAMS_MASK (0x1f << 10) 628#define EP_MAXPSTREAMS_MASK (0x1f << 10)
620#define EP_MAXPSTREAMS(p) (((p) << 10) & EP_MAXPSTREAMS_MASK) 629#define EP_MAXPSTREAMS(p) (((p) << 10) & EP_MAXPSTREAMS_MASK)
621/* Endpoint is set up with a Linear Stream Array (vs. Secondary Stream Array) */ 630/* Endpoint is set up with a Linear Stream Array (vs. Secondary Stream Array) */
@@ -640,6 +649,7 @@ struct xhci_ep_ctx {
640/* bit 6 reserved */ 649/* bit 6 reserved */
641/* bit 7 is Host Initiate Disable - for disabling stream selection */ 650/* bit 7 is Host Initiate Disable - for disabling stream selection */
642#define MAX_BURST(p) (((p)&0xff) << 8) 651#define MAX_BURST(p) (((p)&0xff) << 8)
652#define CTX_TO_MAX_BURST(p) (((p) >> 8) & 0xff)
643#define MAX_PACKET(p) (((p)&0xffff) << 16) 653#define MAX_PACKET(p) (((p)&0xffff) << 16)
644#define MAX_PACKET_MASK (0xffff << 16) 654#define MAX_PACKET_MASK (0xffff << 16)
645#define MAX_PACKET_DECODED(p) (((p) >> 16) & 0xffff) 655#define MAX_PACKET_DECODED(p) (((p) >> 16) & 0xffff)
@@ -652,6 +662,7 @@ struct xhci_ep_ctx {
652/* tx_info bitmasks */ 662/* tx_info bitmasks */
653#define AVG_TRB_LENGTH_FOR_EP(p) ((p) & 0xffff) 663#define AVG_TRB_LENGTH_FOR_EP(p) ((p) & 0xffff)
654#define MAX_ESIT_PAYLOAD_FOR_EP(p) (((p) & 0xffff) << 16) 664#define MAX_ESIT_PAYLOAD_FOR_EP(p) (((p) & 0xffff) << 16)
665#define CTX_TO_MAX_ESIT_PAYLOAD(p) (((p) >> 16) & 0xffff)
655 666
656/* deq bitmasks */ 667/* deq bitmasks */
657#define EP_CTX_CYCLE_MASK (1 << 0) 668#define EP_CTX_CYCLE_MASK (1 << 0)
@@ -670,6 +681,11 @@ struct xhci_input_control_ctx {
670 __le32 rsvd2[6]; 681 __le32 rsvd2[6];
671}; 682};
672 683
684#define EP_IS_ADDED(ctrl_ctx, i) \
685 (le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))
686#define EP_IS_DROPPED(ctrl_ctx, i) \
687 (le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1)))
688
673/* Represents everything that is needed to issue a command on the command ring. 689/* Represents everything that is needed to issue a command on the command ring.
674 * It's useful to pre-allocate these for commands that cannot fail due to 690 * It's useful to pre-allocate these for commands that cannot fail due to
675 * out-of-memory errors, like freeing streams. 691 * out-of-memory errors, like freeing streams.
@@ -731,6 +747,67 @@ struct xhci_stream_info {
731#define SMALL_STREAM_ARRAY_SIZE 256 747#define SMALL_STREAM_ARRAY_SIZE 256
732#define MEDIUM_STREAM_ARRAY_SIZE 1024 748#define MEDIUM_STREAM_ARRAY_SIZE 1024
733 749
750/* Some Intel xHCI host controllers need software to keep track of the bus
751 * bandwidth. Keep track of endpoint info here. Each root port is allocated
752 * the full bus bandwidth. We must also treat TTs (including each port under a
753 * multi-TT hub) as a separate bandwidth domain. The direct memory interface
754 * (DMI) also limits the total bandwidth (across all domains) that can be used.
755 */
756struct xhci_bw_info {
757 /* ep_interval is zero-based */
758 unsigned int ep_interval;
759 /* mult and num_packets are one-based */
760 unsigned int mult;
761 unsigned int num_packets;
762 unsigned int max_packet_size;
763 unsigned int max_esit_payload;
764 unsigned int type;
765};
766
767/* "Block" sizes in bytes the hardware uses for different device speeds.
768 * The logic in this part of the hardware limits the number of bits the hardware
769 * can use, so must represent bandwidth in a less precise manner to mimic what
770 * the scheduler hardware computes.
771 */
772#define FS_BLOCK 1
773#define HS_BLOCK 4
774#define SS_BLOCK 16
775#define DMI_BLOCK 32
776
777/* Each device speed has a protocol overhead (CRC, bit stuffing, etc) associated
778 * with each byte transferred. SuperSpeed devices have an initial overhead to
779 * set up bursts. These are in blocks, see above. LS overhead has already been
780 * translated into FS blocks.
781 */
782#define DMI_OVERHEAD 8
783#define DMI_OVERHEAD_BURST 4
784#define SS_OVERHEAD 8
785#define SS_OVERHEAD_BURST 32
786#define HS_OVERHEAD 26
787#define FS_OVERHEAD 20
788#define LS_OVERHEAD 128
789/* The TTs need to claim roughly twice as much bandwidth (94 bytes per
790 * microframe ~= 24Mbps) of the HS bus as the devices can actually use because
791 * of overhead associated with split transfers crossing microframe boundaries.
792 * 31 blocks is pure protocol overhead.
793 */
794#define TT_HS_OVERHEAD (31 + 94)
795#define TT_DMI_OVERHEAD (25 + 12)
796
797/* Bandwidth limits in blocks */
798#define FS_BW_LIMIT 1285
799#define TT_BW_LIMIT 1320
800#define HS_BW_LIMIT 1607
801#define SS_BW_LIMIT_IN 3906
802#define DMI_BW_LIMIT_IN 3906
803#define SS_BW_LIMIT_OUT 3906
804#define DMI_BW_LIMIT_OUT 3906
805
806/* Percentage of bus bandwidth reserved for non-periodic transfers */
807#define FS_BW_RESERVED 10
808#define HS_BW_RESERVED 20
809#define SS_BW_RESERVED 10
810
734struct xhci_virt_ep { 811struct xhci_virt_ep {
735 struct xhci_ring *ring; 812 struct xhci_ring *ring;
736 /* Related to endpoints that are configured to use stream IDs only */ 813 /* Related to endpoints that are configured to use stream IDs only */
@@ -772,8 +849,39 @@ struct xhci_virt_ep {
772 * process the missed tds on the endpoint ring. 849 * process the missed tds on the endpoint ring.
773 */ 850 */
774 bool skip; 851 bool skip;
852 /* Bandwidth checking storage */
853 struct xhci_bw_info bw_info;
854 struct list_head bw_endpoint_list;
855};
856
857enum xhci_overhead_type {
858 LS_OVERHEAD_TYPE = 0,
859 FS_OVERHEAD_TYPE,
860 HS_OVERHEAD_TYPE,
861};
862
863struct xhci_interval_bw {
864 unsigned int num_packets;
865 /* Sorted by max packet size.
866 * Head of the list is the greatest max packet size.
867 */
868 struct list_head endpoints;
869 /* How many endpoints of each speed are present. */
870 unsigned int overhead[3];
871};
872
873#define XHCI_MAX_INTERVAL 16
874
875struct xhci_interval_bw_table {
876 unsigned int interval0_esit_payload;
877 struct xhci_interval_bw interval_bw[XHCI_MAX_INTERVAL];
878 /* Includes reserved bandwidth for async endpoints */
879 unsigned int bw_used;
880 unsigned int ss_bw_in;
881 unsigned int ss_bw_out;
775}; 882};
776 883
884
777struct xhci_virt_device { 885struct xhci_virt_device {
778 struct usb_device *udev; 886 struct usb_device *udev;
779 /* 887 /*
@@ -798,7 +906,32 @@ struct xhci_virt_device {
798 /* Status of the last command issued for this device */ 906 /* Status of the last command issued for this device */
799 u32 cmd_status; 907 u32 cmd_status;
800 struct list_head cmd_list; 908 struct list_head cmd_list;
801 u8 port; 909 u8 fake_port;
910 u8 real_port;
911 struct xhci_interval_bw_table *bw_table;
912 struct xhci_tt_bw_info *tt_info;
913};
914
915/*
916 * For each roothub, keep track of the bandwidth information for each periodic
917 * interval.
918 *
919 * If a high speed hub is attached to the roothub, each TT associated with that
920 * hub is a separate bandwidth domain. The interval information for the
921 * endpoints on the devices under that TT will appear in the TT structure.
922 */
923struct xhci_root_port_bw_info {
924 struct list_head tts;
925 unsigned int num_active_tts;
926 struct xhci_interval_bw_table bw_table;
927};
928
929struct xhci_tt_bw_info {
930 struct list_head tt_list;
931 int slot_id;
932 int ttport;
933 struct xhci_interval_bw_table bw_table;
934 int active_eps;
802}; 935};
803 936
804 937
@@ -1198,6 +1331,12 @@ struct s3_save {
1198 u64 erst_dequeue; 1331 u64 erst_dequeue;
1199}; 1332};
1200 1333
1334/* Use for lpm */
1335struct dev_info {
1336 u32 dev_id;
1337 struct list_head list;
1338};
1339
1201struct xhci_bus_state { 1340struct xhci_bus_state {
1202 unsigned long bus_suspended; 1341 unsigned long bus_suspended;
1203 unsigned long next_statechange; 1342 unsigned long next_statechange;
@@ -1261,12 +1400,16 @@ struct xhci_hcd {
1261 struct xhci_erst erst; 1400 struct xhci_erst erst;
1262 /* Scratchpad */ 1401 /* Scratchpad */
1263 struct xhci_scratchpad *scratchpad; 1402 struct xhci_scratchpad *scratchpad;
1403 /* Store LPM test failed devices' information */
1404 struct list_head lpm_failed_devs;
1264 1405
1265 /* slot enabling and address device helpers */ 1406 /* slot enabling and address device helpers */
1266 struct completion addr_dev; 1407 struct completion addr_dev;
1267 int slot_id; 1408 int slot_id;
1268 /* Internal mirror of the HW's dcbaa */ 1409 /* Internal mirror of the HW's dcbaa */
1269 struct xhci_virt_device *devs[MAX_HC_SLOTS]; 1410 struct xhci_virt_device *devs[MAX_HC_SLOTS];
1411 /* For keeping track of bandwidth domains per roothub. */
1412 struct xhci_root_port_bw_info *rh_bw;
1270 1413
1271 /* DMA pools */ 1414 /* DMA pools */
1272 struct dma_pool *device_pool; 1415 struct dma_pool *device_pool;
@@ -1318,6 +1461,8 @@ struct xhci_hcd {
1318#define XHCI_EP_LIMIT_QUIRK (1 << 5) 1461#define XHCI_EP_LIMIT_QUIRK (1 << 5)
1319#define XHCI_BROKEN_MSI (1 << 6) 1462#define XHCI_BROKEN_MSI (1 << 6)
1320#define XHCI_RESET_ON_RESUME (1 << 7) 1463#define XHCI_RESET_ON_RESUME (1 << 7)
1464#define XHCI_SW_BW_CHECKING (1 << 8)
1465#define XHCI_AMD_0x96_HOST (1 << 9)
1321 unsigned int num_active_eps; 1466 unsigned int num_active_eps;
1322 unsigned int limit_active_eps; 1467 unsigned int limit_active_eps;
1323 /* There are two roothubs to keep track of bus suspend info for */ 1468 /* There are two roothubs to keep track of bus suspend info for */
@@ -1330,6 +1475,10 @@ struct xhci_hcd {
1330 /* Array of pointers to USB 2.0 PORTSC registers */ 1475 /* Array of pointers to USB 2.0 PORTSC registers */
1331 __le32 __iomem **usb2_ports; 1476 __le32 __iomem **usb2_ports;
1332 unsigned int num_usb2_ports; 1477 unsigned int num_usb2_ports;
1478 /* support xHCI 0.96 spec USB2 software LPM */
1479 unsigned sw_lpm_support:1;
1480 /* support xHCI 1.0 spec USB2 hardware LPM */
1481 unsigned hw_lpm_support:1;
1333}; 1482};
1334 1483
1335/* convert between an HCD pointer and the corresponding EHCI_HCD */ 1484/* convert between an HCD pointer and the corresponding EHCI_HCD */
@@ -1401,9 +1550,7 @@ static inline void xhci_write_64(struct xhci_hcd *xhci,
1401 1550
1402static inline int xhci_link_trb_quirk(struct xhci_hcd *xhci) 1551static inline int xhci_link_trb_quirk(struct xhci_hcd *xhci)
1403{ 1552{
1404 u32 temp = xhci_readl(xhci, &xhci->cap_regs->hc_capbase); 1553 return xhci->quirks & XHCI_LINK_TRB_QUIRK;
1405 return ((HC_VERSION(temp) == 0x95) &&
1406 (xhci->quirks & XHCI_LINK_TRB_QUIRK));
1407} 1554}
1408 1555
1409/* xHCI debugging */ 1556/* xHCI debugging */
@@ -1438,6 +1585,20 @@ unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
1438unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index); 1585unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index);
1439unsigned int xhci_last_valid_endpoint(u32 added_ctxs); 1586unsigned int xhci_last_valid_endpoint(u32 added_ctxs);
1440void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep); 1587void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
1588void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
1589 struct xhci_bw_info *ep_bw,
1590 struct xhci_interval_bw_table *bw_table,
1591 struct usb_device *udev,
1592 struct xhci_virt_ep *virt_ep,
1593 struct xhci_tt_bw_info *tt_info);
1594void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
1595 struct xhci_virt_device *virt_dev,
1596 int old_active_eps);
1597void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info);
1598void xhci_update_bw_info(struct xhci_hcd *xhci,
1599 struct xhci_container_ctx *in_ctx,
1600 struct xhci_input_control_ctx *ctrl_ctx,
1601 struct xhci_virt_device *virt_dev);
1441void xhci_endpoint_copy(struct xhci_hcd *xhci, 1602void xhci_endpoint_copy(struct xhci_hcd *xhci,
1442 struct xhci_container_ctx *in_ctx, 1603 struct xhci_container_ctx *in_ctx,
1443 struct xhci_container_ctx *out_ctx, 1604 struct xhci_container_ctx *out_ctx,
@@ -1483,9 +1644,13 @@ void xhci_free_command(struct xhci_hcd *xhci,
1483/* xHCI PCI glue */ 1644/* xHCI PCI glue */
1484int xhci_register_pci(void); 1645int xhci_register_pci(void);
1485void xhci_unregister_pci(void); 1646void xhci_unregister_pci(void);
1647#else
1648static inline int xhci_register_pci(void) { return 0; }
1649static inline void xhci_unregister_pci(void) {}
1486#endif 1650#endif
1487 1651
1488/* xHCI host controller glue */ 1652/* xHCI host controller glue */
1653typedef void (*xhci_get_quirks_t)(struct device *, struct xhci_hcd *);
1489void xhci_quiesce(struct xhci_hcd *xhci); 1654void xhci_quiesce(struct xhci_hcd *xhci);
1490int xhci_halt(struct xhci_hcd *xhci); 1655int xhci_halt(struct xhci_hcd *xhci);
1491int xhci_reset(struct xhci_hcd *xhci); 1656int xhci_reset(struct xhci_hcd *xhci);
@@ -1493,6 +1658,7 @@ int xhci_init(struct usb_hcd *hcd);
1493int xhci_run(struct usb_hcd *hcd); 1658int xhci_run(struct usb_hcd *hcd);
1494void xhci_stop(struct usb_hcd *hcd); 1659void xhci_stop(struct usb_hcd *hcd);
1495void xhci_shutdown(struct usb_hcd *hcd); 1660void xhci_shutdown(struct usb_hcd *hcd);
1661int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks);
1496 1662
1497#ifdef CONFIG_PM 1663#ifdef CONFIG_PM
1498int xhci_suspend(struct xhci_hcd *xhci); 1664int xhci_suspend(struct xhci_hcd *xhci);
@@ -1507,6 +1673,10 @@ irqreturn_t xhci_irq(struct usb_hcd *hcd);
1507irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd); 1673irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd);
1508int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev); 1674int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev);
1509void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev); 1675void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev);
1676int xhci_alloc_tt_info(struct xhci_hcd *xhci,
1677 struct xhci_virt_device *virt_dev,
1678 struct usb_device *hdev,
1679 struct usb_tt *tt, gfp_t mem_flags);
1510int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, 1680int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1511 struct usb_host_endpoint **eps, unsigned int num_eps, 1681 struct usb_host_endpoint **eps, unsigned int num_eps,
1512 unsigned int num_streams, gfp_t mem_flags); 1682 unsigned int num_streams, gfp_t mem_flags);
@@ -1514,6 +1684,9 @@ int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1514 struct usb_host_endpoint **eps, unsigned int num_eps, 1684 struct usb_host_endpoint **eps, unsigned int num_eps,
1515 gfp_t mem_flags); 1685 gfp_t mem_flags);
1516int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev); 1686int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev);
1687int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev);
1688int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
1689 struct usb_device *udev, int enable);
1517int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev, 1690int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
1518 struct usb_tt *tt, gfp_t mem_flags); 1691 struct usb_tt *tt, gfp_t mem_flags);
1519int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags); 1692int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
@@ -1572,6 +1745,10 @@ void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id,
1572 unsigned int ep_index, unsigned int stream_id); 1745 unsigned int ep_index, unsigned int stream_id);
1573 1746
1574/* xHCI roothub code */ 1747/* xHCI roothub code */
1748void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
1749 int port_id, u32 link_state);
1750void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
1751 int port_id, u32 port_bit);
1575int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, 1752int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,
1576 char *buf, u16 wLength); 1753 char *buf, u16 wLength);
1577int xhci_hub_status_data(struct usb_hcd *hcd, char *buf); 1754int xhci_hub_status_data(struct usb_hcd *hcd, char *buf);
diff --git a/drivers/usb/misc/adutux.c b/drivers/usb/misc/adutux.c
index a6afd15f6a46..fe858711651c 100644
--- a/drivers/usb/misc/adutux.c
+++ b/drivers/usb/misc/adutux.c
@@ -213,7 +213,7 @@ static void adu_interrupt_in_callback(struct urb *urb)
213 213
214 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { 214 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
215 if (dev->read_buffer_length < 215 if (dev->read_buffer_length <
216 (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) - 216 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
217 (urb->actual_length)) { 217 (urb->actual_length)) {
218 memcpy (dev->read_buffer_primary + 218 memcpy (dev->read_buffer_primary +
219 dev->read_buffer_length, 219 dev->read_buffer_length,
@@ -315,7 +315,7 @@ static int adu_open(struct inode *inode, struct file *file)
315 usb_rcvintpipe(dev->udev, 315 usb_rcvintpipe(dev->udev,
316 dev->interrupt_in_endpoint->bEndpointAddress), 316 dev->interrupt_in_endpoint->bEndpointAddress),
317 dev->interrupt_in_buffer, 317 dev->interrupt_in_buffer,
318 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 318 usb_endpoint_maxp(dev->interrupt_in_endpoint),
319 adu_interrupt_in_callback, dev, 319 adu_interrupt_in_callback, dev,
320 dev->interrupt_in_endpoint->bInterval); 320 dev->interrupt_in_endpoint->bInterval);
321 dev->read_urb_finished = 0; 321 dev->read_urb_finished = 0;
@@ -483,7 +483,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
483 usb_rcvintpipe(dev->udev, 483 usb_rcvintpipe(dev->udev,
484 dev->interrupt_in_endpoint->bEndpointAddress), 484 dev->interrupt_in_endpoint->bEndpointAddress),
485 dev->interrupt_in_buffer, 485 dev->interrupt_in_buffer,
486 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 486 usb_endpoint_maxp(dev->interrupt_in_endpoint),
487 adu_interrupt_in_callback, 487 adu_interrupt_in_callback,
488 dev, 488 dev,
489 dev->interrupt_in_endpoint->bInterval); 489 dev->interrupt_in_endpoint->bInterval);
@@ -536,7 +536,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
536 usb_rcvintpipe(dev->udev, 536 usb_rcvintpipe(dev->udev,
537 dev->interrupt_in_endpoint->bEndpointAddress), 537 dev->interrupt_in_endpoint->bEndpointAddress),
538 dev->interrupt_in_buffer, 538 dev->interrupt_in_buffer,
539 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 539 usb_endpoint_maxp(dev->interrupt_in_endpoint),
540 adu_interrupt_in_callback, 540 adu_interrupt_in_callback,
541 dev, 541 dev,
542 dev->interrupt_in_endpoint->bInterval); 542 dev->interrupt_in_endpoint->bInterval);
@@ -622,7 +622,7 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
622 dbg(4," %s : sending, count = %Zd", __func__, count); 622 dbg(4," %s : sending, count = %Zd", __func__, count);
623 623
624 /* write the data into interrupt_out_buffer from userspace */ 624 /* write the data into interrupt_out_buffer from userspace */
625 buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 625 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
626 bytes_to_write = count > buffer_size ? buffer_size : count; 626 bytes_to_write = count > buffer_size ? buffer_size : count;
627 dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", 627 dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
628 __func__, buffer_size, count, bytes_to_write); 628 __func__, buffer_size, count, bytes_to_write);
@@ -752,8 +752,8 @@ static int adu_probe(struct usb_interface *interface,
752 goto error; 752 goto error;
753 } 753 }
754 754
755 in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 755 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
756 out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 756 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
757 757
758 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 758 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
759 if (!dev->read_buffer_primary) { 759 if (!dev->read_buffer_primary) {
diff --git a/drivers/usb/misc/ftdi-elan.c b/drivers/usb/misc/ftdi-elan.c
index 2f41089cd854..2dbe600fbc11 100644
--- a/drivers/usb/misc/ftdi-elan.c
+++ b/drivers/usb/misc/ftdi-elan.c
@@ -2777,7 +2777,7 @@ static int ftdi_elan_probe(struct usb_interface *interface,
2777 endpoint = &iface_desc->endpoint[i].desc; 2777 endpoint = &iface_desc->endpoint[i].desc;
2778 if (!ftdi->bulk_in_endpointAddr && 2778 if (!ftdi->bulk_in_endpointAddr &&
2779 usb_endpoint_is_bulk_in(endpoint)) { 2779 usb_endpoint_is_bulk_in(endpoint)) {
2780 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 2780 buffer_size = usb_endpoint_maxp(endpoint);
2781 ftdi->bulk_in_size = buffer_size; 2781 ftdi->bulk_in_size = buffer_size;
2782 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress; 2782 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2783 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); 2783 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
diff --git a/drivers/usb/misc/idmouse.c b/drivers/usb/misc/idmouse.c
index c6184b4d1695..515b67fffab1 100644
--- a/drivers/usb/misc/idmouse.c
+++ b/drivers/usb/misc/idmouse.c
@@ -359,7 +359,7 @@ static int idmouse_probe(struct usb_interface *interface,
359 endpoint = &iface_desc->endpoint[0].desc; 359 endpoint = &iface_desc->endpoint[0].desc;
360 if (!dev->bulk_in_endpointAddr && usb_endpoint_is_bulk_in(endpoint)) { 360 if (!dev->bulk_in_endpointAddr && usb_endpoint_is_bulk_in(endpoint)) {
361 /* we found a bulk in endpoint */ 361 /* we found a bulk in endpoint */
362 dev->orig_bi_size = le16_to_cpu(endpoint->wMaxPacketSize); 362 dev->orig_bi_size = usb_endpoint_maxp(endpoint);
363 dev->bulk_in_size = 0x200; /* works _much_ faster */ 363 dev->bulk_in_size = 0x200; /* works _much_ faster */
364 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; 364 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
365 dev->bulk_in_buffer = 365 dev->bulk_in_buffer =
diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index a2190b983f52..81457904d6ba 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -803,7 +803,7 @@ static int iowarrior_probe(struct usb_interface *interface,
803 dev->int_out_endpoint = endpoint; 803 dev->int_out_endpoint = endpoint;
804 } 804 }
805 /* we have to check the report_size often, so remember it in the endianess suitable for our machine */ 805 /* we have to check the report_size often, so remember it in the endianess suitable for our machine */
806 dev->report_size = le16_to_cpu(dev->int_in_endpoint->wMaxPacketSize); 806 dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint);
807 if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) && 807 if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) &&
808 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56)) 808 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56))
809 /* IOWarrior56 has wMaxPacketSize different from report size */ 809 /* IOWarrior56 has wMaxPacketSize different from report size */
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
index cb4096201e29..48c166f0d764 100644
--- a/drivers/usb/misc/ldusb.c
+++ b/drivers/usb/misc/ldusb.c
@@ -721,7 +721,7 @@ static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *
721 if (dev->interrupt_out_endpoint == NULL) 721 if (dev->interrupt_out_endpoint == NULL)
722 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); 722 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
723 723
724 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 724 dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
725 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL); 725 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
726 if (!dev->ring_buffer) { 726 if (!dev->ring_buffer) {
727 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n"); 727 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
@@ -737,7 +737,7 @@ static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *
737 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); 737 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
738 goto error; 738 goto error;
739 } 739 }
740 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) : 740 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) :
741 udev->descriptor.bMaxPacketSize0; 741 udev->descriptor.bMaxPacketSize0;
742 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL); 742 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
743 if (!dev->interrupt_out_buffer) { 743 if (!dev->interrupt_out_buffer) {
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index 6482c6e2e6bd..a989356f693e 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -409,7 +409,7 @@ static int tower_open (struct inode *inode, struct file *file)
409 dev->udev, 409 dev->udev,
410 usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress), 410 usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
411 dev->interrupt_in_buffer, 411 dev->interrupt_in_buffer,
412 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 412 usb_endpoint_maxp(dev->interrupt_in_endpoint),
413 tower_interrupt_in_callback, 413 tower_interrupt_in_callback,
414 dev, 414 dev,
415 dev->interrupt_in_interval); 415 dev->interrupt_in_interval);
@@ -928,7 +928,7 @@ static int tower_probe (struct usb_interface *interface, const struct usb_device
928 err("Couldn't allocate read_buffer"); 928 err("Couldn't allocate read_buffer");
929 goto error; 929 goto error;
930 } 930 }
931 dev->interrupt_in_buffer = kmalloc (le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), GFP_KERNEL); 931 dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
932 if (!dev->interrupt_in_buffer) { 932 if (!dev->interrupt_in_buffer) {
933 err("Couldn't allocate interrupt_in_buffer"); 933 err("Couldn't allocate interrupt_in_buffer");
934 goto error; 934 goto error;
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index 51648154bb44..1871cdf10da3 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -18,7 +18,7 @@
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/errno.h> 19#include <linux/errno.h>
20#include <linux/mutex.h> 20#include <linux/mutex.h>
21#include <asm/uaccess.h> 21#include <linux/uaccess.h>
22#include <linux/usb.h> 22#include <linux/usb.h>
23 23
24#define DRIVER_VERSION "USBLCD Driver Version 1.05" 24#define DRIVER_VERSION "USBLCD Driver Version 1.05"
@@ -34,22 +34,29 @@ static const struct usb_device_id id_table[] = {
34 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, }, 34 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
35 { }, 35 { },
36}; 36};
37MODULE_DEVICE_TABLE (usb, id_table); 37MODULE_DEVICE_TABLE(usb, id_table);
38 38
39static DEFINE_MUTEX(open_disc_mutex); 39static DEFINE_MUTEX(open_disc_mutex);
40 40
41 41
42struct usb_lcd { 42struct usb_lcd {
43 struct usb_device * udev; /* init: probe_lcd */ 43 struct usb_device *udev; /* init: probe_lcd */
44 struct usb_interface * interface; /* the interface for this device */ 44 struct usb_interface *interface; /* the interface for
45 unsigned char * bulk_in_buffer; /* the buffer to receive data */ 45 this device */
46 size_t bulk_in_size; /* the size of the receive buffer */ 46 unsigned char *bulk_in_buffer; /* the buffer to receive
47 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ 47 data */
48 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ 48 size_t bulk_in_size; /* the size of the
49 receive buffer */
50 __u8 bulk_in_endpointAddr; /* the address of the
51 bulk in endpoint */
52 __u8 bulk_out_endpointAddr; /* the address of the
53 bulk out endpoint */
49 struct kref kref; 54 struct kref kref;
50 struct semaphore limit_sem; /* to stop writes at full throttle from 55 struct semaphore limit_sem; /* to stop writes at
51 * using up all RAM */ 56 full throttle from
52 struct usb_anchor submitted; /* URBs to wait for before suspend */ 57 using up all RAM */
58 struct usb_anchor submitted; /* URBs to wait for
59 before suspend */
53}; 60};
54#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref) 61#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
55 62
@@ -63,8 +70,8 @@ static void lcd_delete(struct kref *kref)
63 struct usb_lcd *dev = to_lcd_dev(kref); 70 struct usb_lcd *dev = to_lcd_dev(kref);
64 71
65 usb_put_dev(dev->udev); 72 usb_put_dev(dev->udev);
66 kfree (dev->bulk_in_buffer); 73 kfree(dev->bulk_in_buffer);
67 kfree (dev); 74 kfree(dev);
68} 75}
69 76
70 77
@@ -80,7 +87,7 @@ static int lcd_open(struct inode *inode, struct file *file)
80 interface = usb_find_interface(&lcd_driver, subminor); 87 interface = usb_find_interface(&lcd_driver, subminor);
81 if (!interface) { 88 if (!interface) {
82 mutex_unlock(&lcd_mutex); 89 mutex_unlock(&lcd_mutex);
83 err ("USBLCD: %s - error, can't find device for minor %d", 90 err("USBLCD: %s - error, can't find device for minor %d",
84 __func__, subminor); 91 __func__, subminor);
85 return -ENODEV; 92 return -ENODEV;
86 } 93 }
@@ -126,7 +133,8 @@ static int lcd_release(struct inode *inode, struct file *file)
126 return 0; 133 return 0;
127} 134}
128 135
129static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, loff_t *ppos) 136static ssize_t lcd_read(struct file *file, char __user * buffer,
137 size_t count, loff_t *ppos)
130{ 138{
131 struct usb_lcd *dev; 139 struct usb_lcd *dev;
132 int retval = 0; 140 int retval = 0;
@@ -135,8 +143,9 @@ static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, l
135 dev = file->private_data; 143 dev = file->private_data;
136 144
137 /* do a blocking bulk read to get data from the device */ 145 /* do a blocking bulk read to get data from the device */
138 retval = usb_bulk_msg(dev->udev, 146 retval = usb_bulk_msg(dev->udev,
139 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), 147 usb_rcvbulkpipe(dev->udev,
148 dev->bulk_in_endpointAddr),
140 dev->bulk_in_buffer, 149 dev->bulk_in_buffer,
141 min(dev->bulk_in_size, count), 150 min(dev->bulk_in_size, count),
142 &bytes_read, 10000); 151 &bytes_read, 10000);
@@ -161,23 +170,23 @@ static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
161 dev = file->private_data; 170 dev = file->private_data;
162 if (dev == NULL) 171 if (dev == NULL)
163 return -ENODEV; 172 return -ENODEV;
164 173
165 switch (cmd) { 174 switch (cmd) {
166 case IOCTL_GET_HARD_VERSION: 175 case IOCTL_GET_HARD_VERSION:
167 mutex_lock(&lcd_mutex); 176 mutex_lock(&lcd_mutex);
168 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice); 177 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
169 sprintf(buf,"%1d%1d.%1d%1d", 178 sprintf(buf, "%1d%1d.%1d%1d",
170 (bcdDevice & 0xF000)>>12, 179 (bcdDevice & 0xF000)>>12,
171 (bcdDevice & 0xF00)>>8, 180 (bcdDevice & 0xF00)>>8,
172 (bcdDevice & 0xF0)>>4, 181 (bcdDevice & 0xF0)>>4,
173 (bcdDevice & 0xF)); 182 (bcdDevice & 0xF));
174 mutex_unlock(&lcd_mutex); 183 mutex_unlock(&lcd_mutex);
175 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0) 184 if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
176 return -EFAULT; 185 return -EFAULT;
177 break; 186 break;
178 case IOCTL_GET_DRV_VERSION: 187 case IOCTL_GET_DRV_VERSION:
179 sprintf(buf,DRIVER_VERSION); 188 sprintf(buf, DRIVER_VERSION);
180 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0) 189 if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
181 return -EFAULT; 190 return -EFAULT;
182 break; 191 break;
183 default: 192 default:
@@ -199,7 +208,7 @@ static void lcd_write_bulk_callback(struct urb *urb)
199 if (status && 208 if (status &&
200 !(status == -ENOENT || 209 !(status == -ENOENT ||
201 status == -ECONNRESET || 210 status == -ECONNRESET ||
202 status == -ESHUTDOWN)) { 211 status == -ESHUTDOWN)) {
203 dbg("USBLCD: %s - nonzero write bulk status received: %d", 212 dbg("USBLCD: %s - nonzero write bulk status received: %d",
204 __func__, status); 213 __func__, status);
205 } 214 }
@@ -210,15 +219,16 @@ static void lcd_write_bulk_callback(struct urb *urb)
210 up(&dev->limit_sem); 219 up(&dev->limit_sem);
211} 220}
212 221
213static ssize_t lcd_write(struct file *file, const char __user * user_buffer, size_t count, loff_t *ppos) 222static ssize_t lcd_write(struct file *file, const char __user * user_buffer,
223 size_t count, loff_t *ppos)
214{ 224{
215 struct usb_lcd *dev; 225 struct usb_lcd *dev;
216 int retval = 0, r; 226 int retval = 0, r;
217 struct urb *urb = NULL; 227 struct urb *urb = NULL;
218 char *buf = NULL; 228 char *buf = NULL;
219 229
220 dev = file->private_data; 230 dev = file->private_data;
221 231
222 /* verify that we actually have some data to write */ 232 /* verify that we actually have some data to write */
223 if (count == 0) 233 if (count == 0)
224 goto exit; 234 goto exit;
@@ -233,34 +243,38 @@ static ssize_t lcd_write(struct file *file, const char __user * user_buffer, siz
233 retval = -ENOMEM; 243 retval = -ENOMEM;
234 goto err_no_buf; 244 goto err_no_buf;
235 } 245 }
236 246
237 buf = usb_alloc_coherent(dev->udev, count, GFP_KERNEL, &urb->transfer_dma); 247 buf = usb_alloc_coherent(dev->udev, count, GFP_KERNEL,
248 &urb->transfer_dma);
238 if (!buf) { 249 if (!buf) {
239 retval = -ENOMEM; 250 retval = -ENOMEM;
240 goto error; 251 goto error;
241 } 252 }
242 253
243 if (copy_from_user(buf, user_buffer, count)) { 254 if (copy_from_user(buf, user_buffer, count)) {
244 retval = -EFAULT; 255 retval = -EFAULT;
245 goto error; 256 goto error;
246 } 257 }
247 258
248 /* initialize the urb properly */ 259 /* initialize the urb properly */
249 usb_fill_bulk_urb(urb, dev->udev, 260 usb_fill_bulk_urb(urb, dev->udev,
250 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), 261 usb_sndbulkpipe(dev->udev,
262 dev->bulk_out_endpointAddr),
251 buf, count, lcd_write_bulk_callback, dev); 263 buf, count, lcd_write_bulk_callback, dev);
252 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 264 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
253 265
254 usb_anchor_urb(urb, &dev->submitted); 266 usb_anchor_urb(urb, &dev->submitted);
255 267
256 /* send the data out the bulk port */ 268 /* send the data out the bulk port */
257 retval = usb_submit_urb(urb, GFP_KERNEL); 269 retval = usb_submit_urb(urb, GFP_KERNEL);
258 if (retval) { 270 if (retval) {
259 err("USBLCD: %s - failed submitting write urb, error %d", __func__, retval); 271 err("USBLCD: %s - failed submitting write urb, error %d",
272 __func__, retval);
260 goto error_unanchor; 273 goto error_unanchor;
261 } 274 }
262 275
263 /* release our reference to this urb, the USB core will eventually free it entirely */ 276 /* release our reference to this urb,
277 the USB core will eventually free it entirely */
264 usb_free_urb(urb); 278 usb_free_urb(urb);
265 279
266exit: 280exit:
@@ -276,13 +290,13 @@ err_no_buf:
276} 290}
277 291
278static const struct file_operations lcd_fops = { 292static const struct file_operations lcd_fops = {
279 .owner = THIS_MODULE, 293 .owner = THIS_MODULE,
280 .read = lcd_read, 294 .read = lcd_read,
281 .write = lcd_write, 295 .write = lcd_write,
282 .open = lcd_open, 296 .open = lcd_open,
283 .unlocked_ioctl = lcd_ioctl, 297 .unlocked_ioctl = lcd_ioctl,
284 .release = lcd_release, 298 .release = lcd_release,
285 .llseek = noop_llseek, 299 .llseek = noop_llseek,
286}; 300};
287 301
288/* 302/*
@@ -290,12 +304,13 @@ static const struct file_operations lcd_fops = {
290 * and to have the device registered with the driver core 304 * and to have the device registered with the driver core
291 */ 305 */
292static struct usb_class_driver lcd_class = { 306static struct usb_class_driver lcd_class = {
293 .name = "lcd%d", 307 .name = "lcd%d",
294 .fops = &lcd_fops, 308 .fops = &lcd_fops,
295 .minor_base = USBLCD_MINOR, 309 .minor_base = USBLCD_MINOR,
296}; 310};
297 311
298static int lcd_probe(struct usb_interface *interface, const struct usb_device_id *id) 312static int lcd_probe(struct usb_interface *interface,
313 const struct usb_device_id *id)
299{ 314{
300 struct usb_lcd *dev = NULL; 315 struct usb_lcd *dev = NULL;
301 struct usb_host_interface *iface_desc; 316 struct usb_host_interface *iface_desc;
@@ -322,7 +337,7 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
322 retval = -ENODEV; 337 retval = -ENODEV;
323 goto error; 338 goto error;
324 } 339 }
325 340
326 /* set up the endpoint information */ 341 /* set up the endpoint information */
327 /* use only the first bulk-in and bulk-out endpoints */ 342 /* use only the first bulk-in and bulk-out endpoints */
328 iface_desc = interface->cur_altsetting; 343 iface_desc = interface->cur_altsetting;
@@ -332,7 +347,7 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
332 if (!dev->bulk_in_endpointAddr && 347 if (!dev->bulk_in_endpointAddr &&
333 usb_endpoint_is_bulk_in(endpoint)) { 348 usb_endpoint_is_bulk_in(endpoint)) {
334 /* we found a bulk in endpoint */ 349 /* we found a bulk in endpoint */
335 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 350 buffer_size = usb_endpoint_maxp(endpoint);
336 dev->bulk_in_size = buffer_size; 351 dev->bulk_in_size = buffer_size;
337 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; 352 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
338 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); 353 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -369,7 +384,7 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
369 384
370 dev_info(&interface->dev, "USBLCD Version %1d%1d.%1d%1d found " 385 dev_info(&interface->dev, "USBLCD Version %1d%1d.%1d%1d found "
371 "at address %d\n", (i & 0xF000)>>12, (i & 0xF00)>>8, 386 "at address %d\n", (i & 0xF000)>>12, (i & 0xF00)>>8,
372 (i & 0xF0)>>4,(i & 0xF), dev->udev->devnum); 387 (i & 0xF0)>>4, (i & 0xF), dev->udev->devnum);
373 388
374 /* let the user know what node this device is now attached to */ 389 /* let the user know what node this device is now attached to */
375 dev_info(&interface->dev, "USB LCD device now attached to USBLCD-%d\n", 390 dev_info(&interface->dev, "USB LCD device now attached to USBLCD-%d\n",
@@ -401,7 +416,7 @@ static int lcd_suspend(struct usb_interface *intf, pm_message_t message)
401 return 0; 416 return 0;
402} 417}
403 418
404static int lcd_resume (struct usb_interface *intf) 419static int lcd_resume(struct usb_interface *intf)
405{ 420{
406 return 0; 421 return 0;
407} 422}
@@ -409,16 +424,16 @@ static int lcd_resume (struct usb_interface *intf)
409static void lcd_disconnect(struct usb_interface *interface) 424static void lcd_disconnect(struct usb_interface *interface)
410{ 425{
411 struct usb_lcd *dev; 426 struct usb_lcd *dev;
412 int minor = interface->minor; 427 int minor = interface->minor;
413 428
414 mutex_lock(&open_disc_mutex); 429 mutex_lock(&open_disc_mutex);
415 dev = usb_get_intfdata(interface); 430 dev = usb_get_intfdata(interface);
416 usb_set_intfdata(interface, NULL); 431 usb_set_intfdata(interface, NULL);
417 mutex_unlock(&open_disc_mutex); 432 mutex_unlock(&open_disc_mutex);
418 433
419 /* give back our minor */ 434 /* give back our minor */
420 usb_deregister_dev(interface, &lcd_class); 435 usb_deregister_dev(interface, &lcd_class);
421 436
422 /* decrement our usage count */ 437 /* decrement our usage count */
423 kref_put(&dev->kref, lcd_delete); 438 kref_put(&dev->kref, lcd_delete);
424 439
@@ -438,7 +453,7 @@ static struct usb_driver lcd_driver = {
438static int __init usb_lcd_init(void) 453static int __init usb_lcd_init(void)
439{ 454{
440 int result; 455 int result;
441 456
442 result = usb_register(&lcd_driver); 457 result = usb_register(&lcd_driver);
443 if (result) 458 if (result)
444 err("usb_register failed. Error number %d", result); 459 err("usb_register failed. Error number %d", result);
diff --git a/drivers/usb/misc/usbled.c b/drivers/usb/misc/usbled.c
index 1616ad1793a4..43f84e50d514 100644
--- a/drivers/usb/misc/usbled.c
+++ b/drivers/usb/misc/usbled.c
@@ -33,10 +33,10 @@ static const struct usb_device_id id_table[] = {
33 .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER }, 33 .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER },
34 { }, 34 { },
35}; 35};
36MODULE_DEVICE_TABLE (usb, id_table); 36MODULE_DEVICE_TABLE(usb, id_table);
37 37
38struct usb_led { 38struct usb_led {
39 struct usb_device * udev; 39 struct usb_device *udev;
40 unsigned char blue; 40 unsigned char blue;
41 unsigned char red; 41 unsigned char red;
42 unsigned char green; 42 unsigned char green;
@@ -113,14 +113,16 @@ static void change_color(struct usb_led *led)
113} 113}
114 114
115#define show_set(value) \ 115#define show_set(value) \
116static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ 116static ssize_t show_##value(struct device *dev, struct device_attribute *attr,\
117 char *buf) \
117{ \ 118{ \
118 struct usb_interface *intf = to_usb_interface(dev); \ 119 struct usb_interface *intf = to_usb_interface(dev); \
119 struct usb_led *led = usb_get_intfdata(intf); \ 120 struct usb_led *led = usb_get_intfdata(intf); \
120 \ 121 \
121 return sprintf(buf, "%d\n", led->value); \ 122 return sprintf(buf, "%d\n", led->value); \
122} \ 123} \
123static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ 124static ssize_t set_##value(struct device *dev, struct device_attribute *attr,\
125 const char *buf, size_t count) \
124{ \ 126{ \
125 struct usb_interface *intf = to_usb_interface(dev); \ 127 struct usb_interface *intf = to_usb_interface(dev); \
126 struct usb_led *led = usb_get_intfdata(intf); \ 128 struct usb_led *led = usb_get_intfdata(intf); \
@@ -135,7 +137,8 @@ show_set(blue);
135show_set(red); 137show_set(red);
136show_set(green); 138show_set(green);
137 139
138static int led_probe(struct usb_interface *interface, const struct usb_device_id *id) 140static int led_probe(struct usb_interface *interface,
141 const struct usb_device_id *id)
139{ 142{
140 struct usb_device *udev = interface_to_usbdev(interface); 143 struct usb_device *udev = interface_to_usbdev(interface);
141 struct usb_led *dev = NULL; 144 struct usb_led *dev = NULL;
@@ -150,7 +153,7 @@ static int led_probe(struct usb_interface *interface, const struct usb_device_id
150 dev->udev = usb_get_dev(udev); 153 dev->udev = usb_get_dev(udev);
151 dev->type = id->driver_info; 154 dev->type = id->driver_info;
152 155
153 usb_set_intfdata (interface, dev); 156 usb_set_intfdata(interface, dev);
154 157
155 retval = device_create_file(&interface->dev, &dev_attr_blue); 158 retval = device_create_file(&interface->dev, &dev_attr_blue);
156 if (retval) 159 if (retval)
@@ -194,7 +197,7 @@ error:
194 device_remove_file(&interface->dev, &dev_attr_blue); 197 device_remove_file(&interface->dev, &dev_attr_blue);
195 device_remove_file(&interface->dev, &dev_attr_red); 198 device_remove_file(&interface->dev, &dev_attr_red);
196 device_remove_file(&interface->dev, &dev_attr_green); 199 device_remove_file(&interface->dev, &dev_attr_green);
197 usb_set_intfdata (interface, NULL); 200 usb_set_intfdata(interface, NULL);
198 usb_put_dev(dev->udev); 201 usb_put_dev(dev->udev);
199 kfree(dev); 202 kfree(dev);
200error_mem: 203error_mem:
@@ -205,14 +208,14 @@ static void led_disconnect(struct usb_interface *interface)
205{ 208{
206 struct usb_led *dev; 209 struct usb_led *dev;
207 210
208 dev = usb_get_intfdata (interface); 211 dev = usb_get_intfdata(interface);
209 212
210 device_remove_file(&interface->dev, &dev_attr_blue); 213 device_remove_file(&interface->dev, &dev_attr_blue);
211 device_remove_file(&interface->dev, &dev_attr_red); 214 device_remove_file(&interface->dev, &dev_attr_red);
212 device_remove_file(&interface->dev, &dev_attr_green); 215 device_remove_file(&interface->dev, &dev_attr_green);
213 216
214 /* first remove the files, then set the pointer to NULL */ 217 /* first remove the files, then set the pointer to NULL */
215 usb_set_intfdata (interface, NULL); 218 usb_set_intfdata(interface, NULL);
216 219
217 usb_put_dev(dev->udev); 220 usb_put_dev(dev->udev);
218 221
@@ -243,8 +246,8 @@ static void __exit usb_led_exit(void)
243 usb_deregister(&led_driver); 246 usb_deregister(&led_driver);
244} 247}
245 248
246module_init (usb_led_init); 249module_init(usb_led_init);
247module_exit (usb_led_exit); 250module_exit(usb_led_exit);
248 251
249MODULE_AUTHOR(DRIVER_AUTHOR); 252MODULE_AUTHOR(DRIVER_AUTHOR);
250MODULE_DESCRIPTION(DRIVER_DESC); 253MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index bb10846affc3..bd6d00802eab 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -359,8 +359,10 @@ static int simple_io(
359 urb->context = &completion; 359 urb->context = &completion;
360 while (retval == 0 && iterations-- > 0) { 360 while (retval == 0 && iterations-- > 0) {
361 init_completion(&completion); 361 init_completion(&completion);
362 if (usb_pipeout(urb->pipe)) 362 if (usb_pipeout(urb->pipe)) {
363 simple_fill_buf(urb); 363 simple_fill_buf(urb);
364 urb->transfer_flags |= URB_ZERO_PACKET;
365 }
364 retval = usb_submit_urb(urb, GFP_KERNEL); 366 retval = usb_submit_urb(urb, GFP_KERNEL);
365 if (retval != 0) 367 if (retval != 0)
366 break; 368 break;
@@ -1583,8 +1585,8 @@ static struct urb *iso_alloc_urb(
1583 1585
1584 if (bytes < 0 || !desc) 1586 if (bytes < 0 || !desc)
1585 return NULL; 1587 return NULL;
1586 maxp = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); 1588 maxp = 0x7ff & usb_endpoint_maxp(desc);
1587 maxp *= 1 + (0x3 & (le16_to_cpu(desc->wMaxPacketSize) >> 11)); 1589 maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
1588 packets = DIV_ROUND_UP(bytes, maxp); 1590 packets = DIV_ROUND_UP(bytes, maxp);
1589 1591
1590 urb = usb_alloc_urb(packets, GFP_KERNEL); 1592 urb = usb_alloc_urb(packets, GFP_KERNEL);
@@ -1654,7 +1656,7 @@ test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
1654 "... iso period %d %sframes, wMaxPacket %04x\n", 1656 "... iso period %d %sframes, wMaxPacket %04x\n",
1655 1 << (desc->bInterval - 1), 1657 1 << (desc->bInterval - 1),
1656 (udev->speed == USB_SPEED_HIGH) ? "micro" : "", 1658 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
1657 le16_to_cpu(desc->wMaxPacketSize)); 1659 usb_endpoint_maxp(desc));
1658 1660
1659 for (i = 0; i < param->sglen; i++) { 1661 for (i = 0; i < param->sglen; i++) {
1660 urbs[i] = iso_alloc_urb(udev, pipe, desc, 1662 urbs[i] = iso_alloc_urb(udev, pipe, desc,
@@ -2298,25 +2300,8 @@ usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
2298 2300
2299 usb_set_intfdata(intf, dev); 2301 usb_set_intfdata(intf, dev);
2300 dev_info(&intf->dev, "%s\n", info->name); 2302 dev_info(&intf->dev, "%s\n", info->name);
2301 dev_info(&intf->dev, "%s speed {control%s%s%s%s%s} tests%s\n", 2303 dev_info(&intf->dev, "%s {control%s%s%s%s%s} tests%s\n",
2302 ({ char *tmp; 2304 usb_speed_string(udev->speed),
2303 switch (udev->speed) {
2304 case USB_SPEED_LOW:
2305 tmp = "low";
2306 break;
2307 case USB_SPEED_FULL:
2308 tmp = "full";
2309 break;
2310 case USB_SPEED_HIGH:
2311 tmp = "high";
2312 break;
2313 case USB_SPEED_SUPER:
2314 tmp = "super";
2315 break;
2316 default:
2317 tmp = "unknown";
2318 break;
2319 }; tmp; }),
2320 info->ctrl_out ? " in/out" : "", 2305 info->ctrl_out ? " in/out" : "",
2321 rtest, wtest, 2306 rtest, wtest,
2322 irtest, iwtest, 2307 irtest, iwtest,
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index a09dbd243eb3..91cd85076a44 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -11,6 +11,7 @@
11#include <linux/types.h> 11#include <linux/types.h>
12#include <linux/fs.h> 12#include <linux/fs.h>
13#include <linux/cdev.h> 13#include <linux/cdev.h>
14#include <linux/export.h>
14#include <linux/usb.h> 15#include <linux/usb.h>
15#include <linux/poll.h> 16#include <linux/poll.h>
16#include <linux/compat.h> 17#include <linux/compat.h>
@@ -1101,7 +1102,7 @@ static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg
1101 nevents = mon_bin_queued(rp); 1102 nevents = mon_bin_queued(rp);
1102 1103
1103 sp = (struct mon_bin_stats __user *)arg; 1104 sp = (struct mon_bin_stats __user *)arg;
1104 if (put_user(rp->cnt_lost, &sp->dropped)) 1105 if (put_user(ndropped, &sp->dropped))
1105 return -EFAULT; 1106 return -EFAULT;
1106 if (put_user(nevents, &sp->queued)) 1107 if (put_user(nevents, &sp->queued))
1107 return -EFAULT; 1108 return -EFAULT;
diff --git a/drivers/usb/mon/mon_stat.c b/drivers/usb/mon/mon_stat.c
index e5ce42bd316e..ebd6189a5014 100644
--- a/drivers/usb/mon/mon_stat.c
+++ b/drivers/usb/mon/mon_stat.c
@@ -9,6 +9,7 @@
9 9
10#include <linux/kernel.h> 10#include <linux/kernel.h>
11#include <linux/slab.h> 11#include <linux/slab.h>
12#include <linux/export.h>
12#include <linux/usb.h> 13#include <linux/usb.h>
13#include <linux/fs.h> 14#include <linux/fs.h>
14#include <asm/uaccess.h> 15#include <asm/uaccess.h>
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 1c3afcc11bd9..ad408251d955 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -9,6 +9,7 @@
9#include <linux/usb.h> 9#include <linux/usb.h>
10#include <linux/slab.h> 10#include <linux/slab.h>
11#include <linux/time.h> 11#include <linux/time.h>
12#include <linux/export.h>
12#include <linux/mutex.h> 13#include <linux/mutex.h>
13#include <linux/debugfs.h> 14#include <linux/debugfs.h>
14#include <linux/scatterlist.h> 15#include <linux/scatterlist.h>
diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
index fc34b8b11910..07a03460a598 100644
--- a/drivers/usb/musb/Kconfig
+++ b/drivers/usb/musb/Kconfig
@@ -11,6 +11,7 @@ config USB_MUSB_HDRC
11 select TWL4030_USB if MACH_OMAP_3430SDP 11 select TWL4030_USB if MACH_OMAP_3430SDP
12 select TWL6030_USB if MACH_OMAP_4430SDP || MACH_OMAP4_PANDA 12 select TWL6030_USB if MACH_OMAP_4430SDP || MACH_OMAP4_PANDA
13 select USB_OTG_UTILS 13 select USB_OTG_UTILS
14 select USB_GADGET_DUALSPEED
14 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)' 15 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
15 help 16 help
16 Say Y here if your system has a dual role high speed USB 17 Say Y here if your system has a dual role high speed USB
@@ -60,7 +61,7 @@ config USB_MUSB_BLACKFIN
60 61
61config USB_MUSB_UX500 62config USB_MUSB_UX500
62 tristate "U8500 and U5500" 63 tristate "U8500 and U5500"
63 depends on (ARCH_U8500 && AB8500_USB) || (ARCH_U5500) 64 depends on (ARCH_U8500 && AB8500_USB)
64 65
65endchoice 66endchoice
66 67
diff --git a/drivers/usb/musb/am35x.c b/drivers/usb/musb/am35x.c
index 08f1d0b662a3..e233d2b7d335 100644
--- a/drivers/usb/musb/am35x.c
+++ b/drivers/usb/musb/am35x.c
@@ -27,6 +27,7 @@
27 */ 27 */
28 28
29#include <linux/init.h> 29#include <linux/init.h>
30#include <linux/module.h>
30#include <linux/clk.h> 31#include <linux/clk.h>
31#include <linux/io.h> 32#include <linux/io.h>
32#include <linux/platform_device.h> 33#include <linux/platform_device.h>
diff --git a/drivers/usb/musb/da8xx.c b/drivers/usb/musb/da8xx.c
index 4da7492ddbdb..2613bfdb09b6 100644
--- a/drivers/usb/musb/da8xx.c
+++ b/drivers/usb/musb/da8xx.c
@@ -27,6 +27,7 @@
27 */ 27 */
28 28
29#include <linux/init.h> 29#include <linux/init.h>
30#include <linux/module.h>
30#include <linux/clk.h> 31#include <linux/clk.h>
31#include <linux/io.h> 32#include <linux/io.h>
32#include <linux/platform_device.h> 33#include <linux/platform_device.h>
diff --git a/drivers/usb/musb/davinci.c b/drivers/usb/musb/davinci.c
index 8bdf25a8b023..f9a3f62a83b5 100644
--- a/drivers/usb/musb/davinci.c
+++ b/drivers/usb/musb/davinci.c
@@ -35,7 +35,7 @@
35 35
36#include <mach/hardware.h> 36#include <mach/hardware.h>
37#include <mach/memory.h> 37#include <mach/memory.h>
38#include <mach/gpio.h> 38#include <asm/gpio.h>
39#include <mach/cputype.h> 39#include <mach/cputype.h>
40 40
41#include <asm/mach-types.h> 41#include <asm/mach-types.h>
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 20a28731c338..c1fa12ec7a9a 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -1477,8 +1477,7 @@ static int __init musb_core_init(u16 musb_type, struct musb *musb)
1477/*-------------------------------------------------------------------------*/ 1477/*-------------------------------------------------------------------------*/
1478 1478
1479#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \ 1479#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
1480 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \ 1480 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500)
1481 defined(CONFIG_ARCH_U5500)
1482 1481
1483static irqreturn_t generic_interrupt(int irq, void *__hci) 1482static irqreturn_t generic_interrupt(int irq, void *__hci)
1484{ 1483{
diff --git a/drivers/usb/musb/musb_debugfs.c b/drivers/usb/musb/musb_debugfs.c
index b0176e4569e0..61f4ee466df7 100644
--- a/drivers/usb/musb/musb_debugfs.c
+++ b/drivers/usb/musb/musb_debugfs.c
@@ -41,12 +41,6 @@
41#include <linux/debugfs.h> 41#include <linux/debugfs.h>
42#include <linux/seq_file.h> 42#include <linux/seq_file.h>
43 43
44#ifdef CONFIG_ARM
45#include <mach/hardware.h>
46#include <mach/memory.h>
47#include <asm/mach-types.h>
48#endif
49
50#include <asm/uaccess.h> 44#include <asm/uaccess.h>
51 45
52#include "musb_core.h" 46#include "musb_core.h"
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index e81820370d6f..d51043acfe1a 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -634,6 +634,7 @@ static void rxstate(struct musb *musb, struct musb_request *req)
634 u16 len; 634 u16 len;
635 u16 csr = musb_readw(epio, MUSB_RXCSR); 635 u16 csr = musb_readw(epio, MUSB_RXCSR);
636 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 636 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
637 u8 use_mode_1;
637 638
638 if (hw_ep->is_shared_fifo) 639 if (hw_ep->is_shared_fifo)
639 musb_ep = &hw_ep->ep_in; 640 musb_ep = &hw_ep->ep_in;
@@ -683,6 +684,18 @@ static void rxstate(struct musb *musb, struct musb_request *req)
683 684
684 if (csr & MUSB_RXCSR_RXPKTRDY) { 685 if (csr & MUSB_RXCSR_RXPKTRDY) {
685 len = musb_readw(epio, MUSB_RXCOUNT); 686 len = musb_readw(epio, MUSB_RXCOUNT);
687
688 /*
689 * Enable Mode 1 on RX transfers only when short_not_ok flag
690 * is set. Currently short_not_ok flag is set only from
691 * file_storage and f_mass_storage drivers
692 */
693
694 if (request->short_not_ok && len == musb_ep->packet_sz)
695 use_mode_1 = 1;
696 else
697 use_mode_1 = 0;
698
686 if (request->actual < request->length) { 699 if (request->actual < request->length) {
687#ifdef CONFIG_USB_INVENTRA_DMA 700#ifdef CONFIG_USB_INVENTRA_DMA
688 if (is_buffer_mapped(req)) { 701 if (is_buffer_mapped(req)) {
@@ -704,7 +717,7 @@ static void rxstate(struct musb *musb, struct musb_request *req)
704 * most these gadgets, end of is signified either by a short packet, 717 * most these gadgets, end of is signified either by a short packet,
705 * or filling the last byte of the buffer. (Sending extra data in 718 * or filling the last byte of the buffer. (Sending extra data in
706 * that last pckate should trigger an overflow fault.) But in mode 1, 719 * that last pckate should trigger an overflow fault.) But in mode 1,
707 * we don't get DMA completion interrrupt for short packets. 720 * we don't get DMA completion interrupt for short packets.
708 * 721 *
709 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), 722 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
710 * to get endpoint interrupt on every DMA req, but that didn't seem 723 * to get endpoint interrupt on every DMA req, but that didn't seem
@@ -714,37 +727,41 @@ static void rxstate(struct musb *musb, struct musb_request *req)
714 * then becomes usable as a runtime "use mode 1" hint... 727 * then becomes usable as a runtime "use mode 1" hint...
715 */ 728 */
716 729
717 csr |= MUSB_RXCSR_DMAENAB; 730 /* Experimental: Mode1 works with mass storage use cases */
718#ifdef USE_MODE1 731 if (use_mode_1) {
719 csr |= MUSB_RXCSR_AUTOCLEAR;
720 /* csr |= MUSB_RXCSR_DMAMODE; */
721
722 /* this special sequence (enabling and then
723 * disabling MUSB_RXCSR_DMAMODE) is required
724 * to get DMAReq to activate
725 */
726 musb_writew(epio, MUSB_RXCSR,
727 csr | MUSB_RXCSR_DMAMODE);
728#else
729 if (!musb_ep->hb_mult &&
730 musb_ep->hw_ep->rx_double_buffered)
731 csr |= MUSB_RXCSR_AUTOCLEAR; 732 csr |= MUSB_RXCSR_AUTOCLEAR;
732#endif 733 musb_writew(epio, MUSB_RXCSR, csr);
733 musb_writew(epio, MUSB_RXCSR, csr); 734 csr |= MUSB_RXCSR_DMAENAB;
735 musb_writew(epio, MUSB_RXCSR, csr);
736
737 /*
738 * this special sequence (enabling and then
739 * disabling MUSB_RXCSR_DMAMODE) is required
740 * to get DMAReq to activate
741 */
742 musb_writew(epio, MUSB_RXCSR,
743 csr | MUSB_RXCSR_DMAMODE);
744 musb_writew(epio, MUSB_RXCSR, csr);
745
746 } else {
747 if (!musb_ep->hb_mult &&
748 musb_ep->hw_ep->rx_double_buffered)
749 csr |= MUSB_RXCSR_AUTOCLEAR;
750 csr |= MUSB_RXCSR_DMAENAB;
751 musb_writew(epio, MUSB_RXCSR, csr);
752 }
734 753
735 if (request->actual < request->length) { 754 if (request->actual < request->length) {
736 int transfer_size = 0; 755 int transfer_size = 0;
737#ifdef USE_MODE1 756 if (use_mode_1) {
738 transfer_size = min(request->length - request->actual, 757 transfer_size = min(request->length - request->actual,
739 channel->max_len); 758 channel->max_len);
740#else
741 transfer_size = min(request->length - request->actual,
742 (unsigned)len);
743#endif
744 if (transfer_size <= musb_ep->packet_sz)
745 musb_ep->dma->desired_mode = 0;
746 else
747 musb_ep->dma->desired_mode = 1; 759 musb_ep->dma->desired_mode = 1;
760 } else {
761 transfer_size = min(request->length - request->actual,
762 (unsigned)len);
763 musb_ep->dma->desired_mode = 0;
764 }
748 765
749 use_dma = c->channel_program( 766 use_dma = c->channel_program(
750 channel, 767 channel,
@@ -1020,7 +1037,7 @@ static int musb_gadget_enable(struct usb_ep *ep,
1020 goto fail; 1037 goto fail;
1021 1038
1022 /* REVISIT this rules out high bandwidth periodic transfers */ 1039 /* REVISIT this rules out high bandwidth periodic transfers */
1023 tmp = le16_to_cpu(desc->wMaxPacketSize); 1040 tmp = usb_endpoint_maxp(desc);
1024 if (tmp & ~0x07ff) { 1041 if (tmp & ~0x07ff) {
1025 int ok; 1042 int ok;
1026 1043
@@ -1982,10 +1999,6 @@ static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1982 nuke(&hw_ep->ep_out, -ESHUTDOWN); 1999 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1983 } 2000 }
1984 } 2001 }
1985
1986 spin_unlock(&musb->lock);
1987 driver->disconnect(&musb->g);
1988 spin_lock(&musb->lock);
1989 } 2002 }
1990} 2003}
1991 2004
diff --git a/drivers/usb/musb/musb_gadget_ep0.c b/drivers/usb/musb/musb_gadget_ep0.c
index 9378b359c1f0..6a0d0467ec74 100644
--- a/drivers/usb/musb/musb_gadget_ep0.c
+++ b/drivers/usb/musb/musb_gadget_ep0.c
@@ -679,6 +679,14 @@ irqreturn_t musb_g_ep0_irq(struct musb *musb)
679 musb_readb(mbase, MUSB_FADDR), 679 musb_readb(mbase, MUSB_FADDR),
680 decode_ep0stage(musb->ep0_state)); 680 decode_ep0stage(musb->ep0_state));
681 681
682 if (csr & MUSB_CSR0_P_DATAEND) {
683 /*
684 * If DATAEND is set we should not call the callback,
685 * hence the status stage is not complete.
686 */
687 return IRQ_HANDLED;
688 }
689
682 /* I sent a stall.. need to acknowledge it now.. */ 690 /* I sent a stall.. need to acknowledge it now.. */
683 if (csr & MUSB_CSR0_P_SENTSTALL) { 691 if (csr & MUSB_CSR0_P_SENTSTALL) {
684 musb_writew(regs, MUSB_CSR0, 692 musb_writew(regs, MUSB_CSR0,
diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index 8b2473fa0f47..60ddba8066ea 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -1932,7 +1932,7 @@ static int musb_urb_enqueue(
1932 INIT_LIST_HEAD(&qh->ring); 1932 INIT_LIST_HEAD(&qh->ring);
1933 qh->is_ready = 1; 1933 qh->is_ready = 1;
1934 1934
1935 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize); 1935 qh->maxpacket = usb_endpoint_maxp(epd);
1936 qh->type = usb_endpoint_type(epd); 1936 qh->type = usb_endpoint_type(epd);
1937 1937
1938 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier. 1938 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
diff --git a/drivers/usb/musb/musbhsdma.c b/drivers/usb/musb/musbhsdma.c
index f70c5a577736..57a608584e16 100644
--- a/drivers/usb/musb/musbhsdma.c
+++ b/drivers/usb/musb/musbhsdma.c
@@ -408,7 +408,7 @@ dma_controller_create(struct musb *musb, void __iomem *base)
408 controller->controller.channel_program = dma_channel_program; 408 controller->controller.channel_program = dma_channel_program;
409 controller->controller.channel_abort = dma_channel_abort; 409 controller->controller.channel_abort = dma_channel_abort;
410 410
411 if (request_irq(irq, dma_controller_irq, IRQF_DISABLED, 411 if (request_irq(irq, dma_controller_irq, 0,
412 dev_name(musb->controller), &controller->controller)) { 412 dev_name(musb->controller), &controller->controller)) {
413 dev_err(dev, "request_irq %d failed!\n", irq); 413 dev_err(dev, "request_irq %d failed!\n", irq);
414 dma_controller_destroy(&controller->controller); 414 dma_controller_destroy(&controller->controller);
diff --git a/drivers/usb/otg/gpio_vbus.c b/drivers/usb/otg/gpio_vbus.c
index 52733d9959b4..fb644c107ded 100644
--- a/drivers/usb/otg/gpio_vbus.c
+++ b/drivers/usb/otg/gpio_vbus.c
@@ -11,6 +11,7 @@
11#include <linux/kernel.h> 11#include <linux/kernel.h>
12#include <linux/platform_device.h> 12#include <linux/platform_device.h>
13#include <linux/gpio.h> 13#include <linux/gpio.h>
14#include <linux/module.h>
14#include <linux/slab.h> 15#include <linux/slab.h>
15#include <linux/interrupt.h> 16#include <linux/interrupt.h>
16#include <linux/usb.h> 17#include <linux/usb.h>
diff --git a/drivers/usb/otg/isp1301_omap.c b/drivers/usb/otg/isp1301_omap.c
index ca9b690a7e40..8c86787c2f09 100644
--- a/drivers/usb/otg/isp1301_omap.c
+++ b/drivers/usb/otg/isp1301_omap.c
@@ -899,7 +899,7 @@ static int otg_bind(struct isp1301 *isp)
899 899
900 if (otg_dev) 900 if (otg_dev)
901 status = request_irq(otg_dev->resource[1].start, omap_otg_irq, 901 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
902 IRQF_DISABLED, DRIVER_NAME, isp); 902 0, DRIVER_NAME, isp);
903 else 903 else
904 status = -ENODEV; 904 status = -ENODEV;
905 905
diff --git a/drivers/usb/otg/otg.c b/drivers/usb/otg/otg.c
index fb7adeff9ffa..307c27bc51eb 100644
--- a/drivers/usb/otg/otg.c
+++ b/drivers/usb/otg/otg.c
@@ -10,6 +10,7 @@
10 */ 10 */
11 11
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/export.h>
13#include <linux/device.h> 14#include <linux/device.h>
14 15
15#include <linux/usb/otg.h> 16#include <linux/usb/otg.h>
diff --git a/drivers/usb/otg/twl6030-usb.c b/drivers/usb/otg/twl6030-usb.c
index b4d2c0972b3d..ed2b26cfe814 100644
--- a/drivers/usb/otg/twl6030-usb.c
+++ b/drivers/usb/otg/twl6030-usb.c
@@ -137,22 +137,6 @@ static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
137 return ret; 137 return ret;
138} 138}
139 139
140/*-------------------------------------------------------------------------*/
141static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
142{
143 struct twl6030_usb *twl;
144 struct device *dev;
145 struct twl4030_usb_data *pdata;
146
147 twl = xceiv_to_twl(x);
148 dev = twl->dev;
149 pdata = dev->platform_data;
150
151 pdata->phy_set_clock(twl->dev, on);
152
153 return 0;
154}
155
156static int twl6030_phy_init(struct otg_transceiver *x) 140static int twl6030_phy_init(struct otg_transceiver *x)
157{ 141{
158 struct twl6030_usb *twl; 142 struct twl6030_usb *twl;
diff --git a/drivers/usb/otg/ulpi.c b/drivers/usb/otg/ulpi.c
index 770d799d5afb..0b0466728fdc 100644
--- a/drivers/usb/otg/ulpi.c
+++ b/drivers/usb/otg/ulpi.c
@@ -25,6 +25,7 @@
25 25
26#include <linux/kernel.h> 26#include <linux/kernel.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/export.h>
28#include <linux/usb.h> 29#include <linux/usb.h>
29#include <linux/usb/otg.h> 30#include <linux/usb/otg.h>
30#include <linux/usb/ulpi.h> 31#include <linux/usb/ulpi.h>
diff --git a/drivers/usb/renesas_usbhs/Kconfig b/drivers/usb/renesas_usbhs/Kconfig
index 286cbf1ca7de..6f4afa436381 100644
--- a/drivers/usb/renesas_usbhs/Kconfig
+++ b/drivers/usb/renesas_usbhs/Kconfig
@@ -4,7 +4,7 @@
4 4
5config USB_RENESAS_USBHS 5config USB_RENESAS_USBHS
6 tristate 'Renesas USBHS controller' 6 tristate 'Renesas USBHS controller'
7 depends on SUPERH || ARCH_SHMOBILE 7 depends on USB && USB_GADGET
8 default n 8 default n
9 help 9 help
10 Renesas USBHS is a discrete USB host and peripheral controller chip 10 Renesas USBHS is a discrete USB host and peripheral controller chip
diff --git a/drivers/usb/renesas_usbhs/Makefile b/drivers/usb/renesas_usbhs/Makefile
index ce08345fa15a..bc8aef4311a1 100644
--- a/drivers/usb/renesas_usbhs/Makefile
+++ b/drivers/usb/renesas_usbhs/Makefile
@@ -6,4 +6,10 @@ obj-$(CONFIG_USB_RENESAS_USBHS) += renesas_usbhs.o
6 6
7renesas_usbhs-y := common.o mod.o pipe.o fifo.o 7renesas_usbhs-y := common.o mod.o pipe.o fifo.o
8 8
9renesas_usbhs-$(CONFIG_USB_RENESAS_USBHS_UDC) += mod_gadget.o 9ifneq ($(CONFIG_USB_RENESAS_USBHS_HCD),)
10 renesas_usbhs-y += mod_host.o
11endif
12
13ifneq ($(CONFIG_USB_RENESAS_USBHS_UDC),)
14 renesas_usbhs-y += mod_gadget.o
15endif
diff --git a/drivers/usb/renesas_usbhs/common.c b/drivers/usb/renesas_usbhs/common.c
index d8239e5efa66..08c679c0dde5 100644
--- a/drivers/usb/renesas_usbhs/common.c
+++ b/drivers/usb/renesas_usbhs/common.c
@@ -61,8 +61,8 @@
61 */ 61 */
62#define usbhs_platform_call(priv, func, args...)\ 62#define usbhs_platform_call(priv, func, args...)\
63 (!(priv) ? -ENODEV : \ 63 (!(priv) ? -ENODEV : \
64 !((priv)->pfunc->func) ? 0 : \ 64 !((priv)->pfunc.func) ? 0 : \
65 (priv)->pfunc->func(args)) 65 (priv)->pfunc.func(args))
66 66
67/* 67/*
68 * common functions 68 * common functions
@@ -114,6 +114,10 @@ void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
114{ 114{
115 u16 mask = DCFM | DRPD | DPRPU; 115 u16 mask = DCFM | DRPD | DPRPU;
116 u16 val = DCFM | DRPD; 116 u16 val = DCFM | DRPD;
117 int has_otg = usbhs_get_dparam(priv, has_otg);
118
119 if (has_otg)
120 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
117 121
118 /* 122 /*
119 * if enable 123 * if enable
@@ -147,19 +151,133 @@ int usbhs_frame_get_num(struct usbhs_priv *priv)
147} 151}
148 152
149/* 153/*
154 * usb request functions
155 */
156void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
157{
158 u16 val;
159
160 val = usbhs_read(priv, USBREQ);
161 req->bRequest = (val >> 8) & 0xFF;
162 req->bRequestType = (val >> 0) & 0xFF;
163
164 req->wValue = usbhs_read(priv, USBVAL);
165 req->wIndex = usbhs_read(priv, USBINDX);
166 req->wLength = usbhs_read(priv, USBLENG);
167}
168
169void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
170{
171 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
172 usbhs_write(priv, USBVAL, req->wValue);
173 usbhs_write(priv, USBINDX, req->wIndex);
174 usbhs_write(priv, USBLENG, req->wLength);
175
176 usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
177}
178
179/*
180 * bus/vbus functions
181 */
182void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
183{
184 u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
185
186 if (status != USBRST) {
187 struct device *dev = usbhs_priv_to_dev(priv);
188 dev_err(dev, "usbhs should be reset\n");
189 }
190
191 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
192}
193
194void usbhs_bus_send_reset(struct usbhs_priv *priv)
195{
196 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
197}
198
199int usbhs_bus_get_speed(struct usbhs_priv *priv)
200{
201 u16 dvstctr = usbhs_read(priv, DVSTCTR);
202
203 switch (RHST & dvstctr) {
204 case RHST_LOW_SPEED:
205 return USB_SPEED_LOW;
206 case RHST_FULL_SPEED:
207 return USB_SPEED_FULL;
208 case RHST_HIGH_SPEED:
209 return USB_SPEED_HIGH;
210 }
211
212 return USB_SPEED_UNKNOWN;
213}
214
215int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
216{
217 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
218
219 return usbhs_platform_call(priv, set_vbus, pdev, enable);
220}
221
222static void usbhsc_bus_init(struct usbhs_priv *priv)
223{
224 usbhs_write(priv, DVSTCTR, 0);
225
226 usbhs_vbus_ctrl(priv, 0);
227}
228
229/*
230 * device configuration
231 */
232int usbhs_set_device_speed(struct usbhs_priv *priv, int devnum,
233 u16 upphub, u16 hubport, u16 speed)
234{
235 struct device *dev = usbhs_priv_to_dev(priv);
236 u16 usbspd = 0;
237 u32 reg = DEVADD0 + (2 * devnum);
238
239 if (devnum > 10) {
240 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
241 return -EIO;
242 }
243
244 if (upphub > 0xA) {
245 dev_err(dev, "unsupported hub number %d\n", upphub);
246 return -EIO;
247 }
248
249 switch (speed) {
250 case USB_SPEED_LOW:
251 usbspd = USBSPD_SPEED_LOW;
252 break;
253 case USB_SPEED_FULL:
254 usbspd = USBSPD_SPEED_FULL;
255 break;
256 case USB_SPEED_HIGH:
257 usbspd = USBSPD_SPEED_HIGH;
258 break;
259 default:
260 dev_err(dev, "unsupported speed %d\n", speed);
261 return -EIO;
262 }
263
264 usbhs_write(priv, reg, UPPHUB(upphub) |
265 HUBPORT(hubport)|
266 USBSPD(usbspd));
267
268 return 0;
269}
270
271/*
150 * local functions 272 * local functions
151 */ 273 */
152static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable) 274static void usbhsc_set_buswait(struct usbhs_priv *priv)
153{ 275{
154 int wait = usbhs_get_dparam(priv, buswait_bwait); 276 int wait = usbhs_get_dparam(priv, buswait_bwait);
155 u16 data = 0;
156 277
157 if (enable) { 278 /* set bus wait if platform have */
158 /* set bus wait if platform have */ 279 if (wait)
159 if (wait) 280 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
160 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
161 }
162 usbhs_write(priv, DVSTCTR, data);
163} 281}
164 282
165/* 283/*
@@ -191,10 +309,8 @@ static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
191 309
192 /* USB on */ 310 /* USB on */
193 usbhs_sys_clock_ctrl(priv, enable); 311 usbhs_sys_clock_ctrl(priv, enable);
194 usbhsc_bus_ctrl(priv, enable);
195 } else { 312 } else {
196 /* USB off */ 313 /* USB off */
197 usbhsc_bus_ctrl(priv, enable);
198 usbhs_sys_clock_ctrl(priv, enable); 314 usbhs_sys_clock_ctrl(priv, enable);
199 315
200 /* disable PM */ 316 /* disable PM */
@@ -203,13 +319,10 @@ static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
203} 319}
204 320
205/* 321/*
206 * notify hotplug 322 * hotplug
207 */ 323 */
208static void usbhsc_notify_hotplug(struct work_struct *work) 324static void usbhsc_hotplug(struct usbhs_priv *priv)
209{ 325{
210 struct usbhs_priv *priv = container_of(work,
211 struct usbhs_priv,
212 notify_hotplug_work.work);
213 struct platform_device *pdev = usbhs_priv_to_pdev(priv); 326 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
214 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 327 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
215 int id; 328 int id;
@@ -237,6 +350,10 @@ static void usbhsc_notify_hotplug(struct work_struct *work)
237 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 350 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
238 usbhsc_power_ctrl(priv, enable); 351 usbhsc_power_ctrl(priv, enable);
239 352
353 /* bus init */
354 usbhsc_set_buswait(priv);
355 usbhsc_bus_init(priv);
356
240 /* module start */ 357 /* module start */
241 usbhs_mod_call(priv, start, priv); 358 usbhs_mod_call(priv, start, priv);
242 359
@@ -246,6 +363,9 @@ static void usbhsc_notify_hotplug(struct work_struct *work)
246 /* module stop */ 363 /* module stop */
247 usbhs_mod_call(priv, stop, priv); 364 usbhs_mod_call(priv, stop, priv);
248 365
366 /* bus init */
367 usbhsc_bus_init(priv);
368
249 /* power off */ 369 /* power off */
250 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 370 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
251 usbhsc_power_ctrl(priv, enable); 371 usbhsc_power_ctrl(priv, enable);
@@ -257,6 +377,17 @@ static void usbhsc_notify_hotplug(struct work_struct *work)
257 } 377 }
258} 378}
259 379
380/*
381 * notify hotplug
382 */
383static void usbhsc_notify_hotplug(struct work_struct *work)
384{
385 struct usbhs_priv *priv = container_of(work,
386 struct usbhs_priv,
387 notify_hotplug_work.work);
388 usbhsc_hotplug(priv);
389}
390
260int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) 391int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
261{ 392{
262 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 393 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
@@ -274,7 +405,7 @@ int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
274/* 405/*
275 * platform functions 406 * platform functions
276 */ 407 */
277static int __devinit usbhs_probe(struct platform_device *pdev) 408static int usbhs_probe(struct platform_device *pdev)
278{ 409{
279 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data; 410 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
280 struct renesas_usbhs_driver_callback *dfunc; 411 struct renesas_usbhs_driver_callback *dfunc;
@@ -315,24 +446,28 @@ static int __devinit usbhs_probe(struct platform_device *pdev)
315 /* 446 /*
316 * care platform info 447 * care platform info
317 */ 448 */
318 priv->pfunc = &info->platform_callback; 449 memcpy(&priv->pfunc,
319 priv->dparam = &info->driver_param; 450 &info->platform_callback,
451 sizeof(struct renesas_usbhs_platform_callback));
452 memcpy(&priv->dparam,
453 &info->driver_param,
454 sizeof(struct renesas_usbhs_driver_param));
320 455
321 /* set driver callback functions for platform */ 456 /* set driver callback functions for platform */
322 dfunc = &info->driver_callback; 457 dfunc = &info->driver_callback;
323 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; 458 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
324 459
325 /* set default param if platform doesn't have */ 460 /* set default param if platform doesn't have */
326 if (!priv->dparam->pipe_type) { 461 if (!priv->dparam.pipe_type) {
327 priv->dparam->pipe_type = usbhsc_default_pipe_type; 462 priv->dparam.pipe_type = usbhsc_default_pipe_type;
328 priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type); 463 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
329 } 464 }
330 if (!priv->dparam->pio_dma_border) 465 if (!priv->dparam.pio_dma_border)
331 priv->dparam->pio_dma_border = 64; /* 64byte */ 466 priv->dparam.pio_dma_border = 64; /* 64byte */
332 467
333 /* FIXME */ 468 /* FIXME */
334 /* runtime power control ? */ 469 /* runtime power control ? */
335 if (priv->pfunc->get_vbus) 470 if (priv->pfunc.get_vbus)
336 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL); 471 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
337 472
338 /* 473 /*
@@ -443,9 +578,60 @@ static int __devexit usbhs_remove(struct platform_device *pdev)
443 return 0; 578 return 0;
444} 579}
445 580
581static int usbhsc_suspend(struct device *dev)
582{
583 struct usbhs_priv *priv = dev_get_drvdata(dev);
584 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
585
586 if (mod) {
587 usbhs_mod_call(priv, stop, priv);
588 usbhs_mod_change(priv, -1);
589 }
590
591 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
592 usbhsc_power_ctrl(priv, 0);
593
594 return 0;
595}
596
597static int usbhsc_resume(struct device *dev)
598{
599 struct usbhs_priv *priv = dev_get_drvdata(dev);
600 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
601
602 usbhs_platform_call(priv, phy_reset, pdev);
603
604 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
605 usbhsc_power_ctrl(priv, 1);
606
607 usbhsc_hotplug(priv);
608
609 return 0;
610}
611
612static int usbhsc_runtime_nop(struct device *dev)
613{
614 /* Runtime PM callback shared between ->runtime_suspend()
615 * and ->runtime_resume(). Simply returns success.
616 *
617 * This driver re-initializes all registers after
618 * pm_runtime_get_sync() anyway so there is no need
619 * to save and restore registers here.
620 */
621 return 0;
622}
623
624static const struct dev_pm_ops usbhsc_pm_ops = {
625 .suspend = usbhsc_suspend,
626 .resume = usbhsc_resume,
627 .runtime_suspend = usbhsc_runtime_nop,
628 .runtime_resume = usbhsc_runtime_nop,
629};
630
446static struct platform_driver renesas_usbhs_driver = { 631static struct platform_driver renesas_usbhs_driver = {
447 .driver = { 632 .driver = {
448 .name = "renesas_usbhs", 633 .name = "renesas_usbhs",
634 .pm = &usbhsc_pm_ops,
449 }, 635 },
450 .probe = usbhs_probe, 636 .probe = usbhs_probe,
451 .remove = __devexit_p(usbhs_remove), 637 .remove = __devexit_p(usbhs_remove),
diff --git a/drivers/usb/renesas_usbhs/common.h b/drivers/usb/renesas_usbhs/common.h
index b410463a1212..8729da5c3be6 100644
--- a/drivers/usb/renesas_usbhs/common.h
+++ b/drivers/usb/renesas_usbhs/common.h
@@ -90,6 +90,17 @@ struct usbhs_priv;
90#define PIPE9TRN 0x00BA 90#define PIPE9TRN 0x00BA
91#define PIPEATRE 0x00BC 91#define PIPEATRE 0x00BC
92#define PIPEATRN 0x00BE 92#define PIPEATRN 0x00BE
93#define DEVADD0 0x00D0 /* Device address n configuration */
94#define DEVADD1 0x00D2
95#define DEVADD2 0x00D4
96#define DEVADD3 0x00D6
97#define DEVADD4 0x00D8
98#define DEVADD5 0x00DA
99#define DEVADD6 0x00DC
100#define DEVADD7 0x00DE
101#define DEVADD8 0x00E0
102#define DEVADD9 0x00E2
103#define DEVADDA 0x00E4
93 104
94/* SYSCFG */ 105/* SYSCFG */
95#define SCKE (1 << 10) /* USB Module Clock Enable */ 106#define SCKE (1 << 10) /* USB Module Clock Enable */
@@ -102,6 +113,8 @@ struct usbhs_priv;
102/* DVSTCTR */ 113/* DVSTCTR */
103#define EXTLP (1 << 10) /* Controls the EXTLP pin output state */ 114#define EXTLP (1 << 10) /* Controls the EXTLP pin output state */
104#define PWEN (1 << 9) /* Controls the PWEN pin output state */ 115#define PWEN (1 << 9) /* Controls the PWEN pin output state */
116#define USBRST (1 << 6) /* Bus Reset Output */
117#define UACT (1 << 4) /* USB Bus Enable */
105#define RHST (0x7) /* Reset Handshake */ 118#define RHST (0x7) /* Reset Handshake */
106#define RHST_LOW_SPEED 1 /* Low-speed connection */ 119#define RHST_LOW_SPEED 1 /* Low-speed connection */
107#define RHST_FULL_SPEED 2 /* Full-speed connection */ 120#define RHST_FULL_SPEED 2 /* Full-speed connection */
@@ -159,6 +172,15 @@ struct usbhs_priv;
159#define NODATA_STATUS_STAGE 5 /* Control write NoData status stage */ 172#define NODATA_STATUS_STAGE 5 /* Control write NoData status stage */
160#define SEQUENCE_ERROR 6 /* Control transfer sequence error */ 173#define SEQUENCE_ERROR 6 /* Control transfer sequence error */
161 174
175/* INTSTS1 */
176#define OVRCR (1 << 15) /* OVRCR Interrupt Status */
177#define BCHG (1 << 14) /* USB Bus Change Interrupt Status */
178#define DTCH (1 << 12) /* USB Disconnection Detect Interrupt Status */
179#define ATTCH (1 << 11) /* ATTCH Interrupt Status */
180#define EOFERR (1 << 6) /* EOF Error Detect Interrupt Status */
181#define SIGN (1 << 5) /* Setup Transaction Error Interrupt Status */
182#define SACK (1 << 4) /* Setup Transaction ACK Response Interrupt Status */
183
162/* PIPECFG */ 184/* PIPECFG */
163/* DCPCFG */ 185/* DCPCFG */
164#define TYPE_NONE (0 << 14) /* Transfer Type */ 186#define TYPE_NONE (0 << 14) /* Transfer Type */
@@ -183,9 +205,11 @@ struct usbhs_priv;
183/* PIPEnCTR */ 205/* PIPEnCTR */
184/* DCPCTR */ 206/* DCPCTR */
185#define BSTS (1 << 15) /* Buffer Status */ 207#define BSTS (1 << 15) /* Buffer Status */
208#define SUREQ (1 << 14) /* Sending SETUP Token */
186#define CSSTS (1 << 12) /* CSSTS Status */ 209#define CSSTS (1 << 12) /* CSSTS Status */
187#define SQCLR (1 << 8) /* Toggle Bit Clear */
188#define ACLRM (1 << 9) /* Buffer Auto-Clear Mode */ 210#define ACLRM (1 << 9) /* Buffer Auto-Clear Mode */
211#define SQCLR (1 << 8) /* Toggle Bit Clear */
212#define SQSET (1 << 7) /* Toggle Bit Set */
189#define PBUSY (1 << 5) /* Pipe Busy */ 213#define PBUSY (1 << 5) /* Pipe Busy */
190#define PID_MASK (0x3) /* Response PID */ 214#define PID_MASK (0x3) /* Response PID */
191#define PID_NAK 0 215#define PID_NAK 0
@@ -202,6 +226,14 @@ struct usbhs_priv;
202/* FRMNUM */ 226/* FRMNUM */
203#define FRNM_MASK (0x7FF) 227#define FRNM_MASK (0x7FF)
204 228
229/* DEVADDn */
230#define UPPHUB(x) (((x) & 0xF) << 11) /* HUB Register */
231#define HUBPORT(x) (((x) & 0x7) << 8) /* HUB Port for Target Device */
232#define USBSPD(x) (((x) & 0x3) << 6) /* Device Transfer Rate */
233#define USBSPD_SPEED_LOW 0x1
234#define USBSPD_SPEED_FULL 0x2
235#define USBSPD_SPEED_HIGH 0x3
236
205/* 237/*
206 * struct 238 * struct
207 */ 239 */
@@ -210,8 +242,8 @@ struct usbhs_priv {
210 void __iomem *base; 242 void __iomem *base;
211 unsigned int irq; 243 unsigned int irq;
212 244
213 struct renesas_usbhs_platform_callback *pfunc; 245 struct renesas_usbhs_platform_callback pfunc;
214 struct renesas_usbhs_driver_param *dparam; 246 struct renesas_usbhs_driver_param dparam;
215 247
216 struct delayed_work notify_hotplug_work; 248 struct delayed_work notify_hotplug_work;
217 struct platform_device *pdev; 249 struct platform_device *pdev;
@@ -258,15 +290,35 @@ void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable);
258void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable); 290void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable);
259 291
260/* 292/*
293 * usb request
294 */
295void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req);
296void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req);
297
298/*
299 * bus
300 */
301void usbhs_bus_send_sof_enable(struct usbhs_priv *priv);
302void usbhs_bus_send_reset(struct usbhs_priv *priv);
303int usbhs_bus_get_speed(struct usbhs_priv *priv);
304int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable);
305
306/*
261 * frame 307 * frame
262 */ 308 */
263int usbhs_frame_get_num(struct usbhs_priv *priv); 309int usbhs_frame_get_num(struct usbhs_priv *priv);
264 310
265/* 311/*
312 * device config
313 */
314int usbhs_set_device_speed(struct usbhs_priv *priv, int devnum, u16 upphub,
315 u16 hubport, u16 speed);
316
317/*
266 * data 318 * data
267 */ 319 */
268struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev); 320struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev);
269#define usbhs_get_dparam(priv, param) (priv->dparam->param) 321#define usbhs_get_dparam(priv, param) (priv->dparam.param)
270#define usbhs_priv_to_pdev(priv) (priv->pdev) 322#define usbhs_priv_to_pdev(priv) (priv->pdev)
271#define usbhs_priv_to_dev(priv) (&priv->pdev->dev) 323#define usbhs_priv_to_dev(priv) (&priv->pdev->dev)
272#define usbhs_priv_to_lock(priv) (&priv->lock) 324#define usbhs_priv_to_lock(priv) (&priv->lock)
diff --git a/drivers/usb/renesas_usbhs/fifo.c b/drivers/usb/renesas_usbhs/fifo.c
index a34430f55fb7..ffdf5d15085e 100644
--- a/drivers/usb/renesas_usbhs/fifo.c
+++ b/drivers/usb/renesas_usbhs/fifo.c
@@ -54,35 +54,45 @@ static struct usbhs_pkt_handle usbhsf_null_handler = {
54}; 54};
55 55
56void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, 56void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
57 struct usbhs_pkt_handle *handler, 57 void (*done)(struct usbhs_priv *priv,
58 struct usbhs_pkt *pkt),
58 void *buf, int len, int zero) 59 void *buf, int len, int zero)
59{ 60{
60 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 61 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
61 struct device *dev = usbhs_priv_to_dev(priv); 62 struct device *dev = usbhs_priv_to_dev(priv);
62 unsigned long flags; 63 unsigned long flags;
63 64
65 if (!done) {
66 dev_err(dev, "no done function\n");
67 return;
68 }
69
64 /******************** spin lock ********************/ 70 /******************** spin lock ********************/
65 usbhs_lock(priv, flags); 71 usbhs_lock(priv, flags);
66 72
67 if (!handler) { 73 if (!pipe->handler) {
68 dev_err(dev, "no handler function\n"); 74 dev_err(dev, "no handler function\n");
69 handler = &usbhsf_null_handler; 75 pipe->handler = &usbhsf_null_handler;
70 } 76 }
71 77
72 list_del_init(&pkt->node); 78 list_del_init(&pkt->node);
73 list_add_tail(&pkt->node, &pipe->list); 79 list_add_tail(&pkt->node, &pipe->list);
74 80
81 /*
82 * each pkt must hold own handler.
83 * because handler might be changed by its situation.
84 * dma handler -> pio handler.
85 */
75 pkt->pipe = pipe; 86 pkt->pipe = pipe;
76 pkt->buf = buf; 87 pkt->buf = buf;
77 pkt->handler = handler; 88 pkt->handler = pipe->handler;
78 pkt->length = len; 89 pkt->length = len;
79 pkt->zero = zero; 90 pkt->zero = zero;
80 pkt->actual = 0; 91 pkt->actual = 0;
92 pkt->done = done;
81 93
82 usbhs_unlock(priv, flags); 94 usbhs_unlock(priv, flags);
83 /******************** spin unlock ******************/ 95 /******************** spin unlock ******************/
84
85 usbhs_pkt_start(pipe);
86} 96}
87 97
88static void __usbhsf_pkt_del(struct usbhs_pkt *pkt) 98static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
@@ -118,10 +128,15 @@ struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
118 return pkt; 128 return pkt;
119} 129}
120 130
121int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type) 131enum {
132 USBHSF_PKT_PREPARE,
133 USBHSF_PKT_TRY_RUN,
134 USBHSF_PKT_DMA_DONE,
135};
136
137static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
122{ 138{
123 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 139 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
124 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
125 struct usbhs_pkt *pkt; 140 struct usbhs_pkt *pkt;
126 struct device *dev = usbhs_priv_to_dev(priv); 141 struct device *dev = usbhs_priv_to_dev(priv);
127 int (*func)(struct usbhs_pkt *pkt, int *is_done); 142 int (*func)(struct usbhs_pkt *pkt, int *is_done);
@@ -161,13 +176,18 @@ __usbhs_pkt_handler_end:
161 /******************** spin unlock ******************/ 176 /******************** spin unlock ******************/
162 177
163 if (is_done) { 178 if (is_done) {
164 info->done(pkt); 179 pkt->done(priv, pkt);
165 usbhs_pkt_start(pipe); 180 usbhs_pkt_start(pipe);
166 } 181 }
167 182
168 return ret; 183 return ret;
169} 184}
170 185
186void usbhs_pkt_start(struct usbhs_pipe *pipe)
187{
188 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
189}
190
171/* 191/*
172 * irq enable/disable function 192 * irq enable/disable function
173 */ 193 */
@@ -276,9 +296,13 @@ static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
276 usbhsf_fifo_is_busy(fifo)) 296 usbhsf_fifo_is_busy(fifo))
277 return -EBUSY; 297 return -EBUSY;
278 298
279 if (usbhs_pipe_is_dcp(pipe)) 299 if (usbhs_pipe_is_dcp(pipe)) {
280 base |= (1 == write) << 5; /* ISEL */ 300 base |= (1 == write) << 5; /* ISEL */
281 301
302 if (usbhs_mod_is_host(priv))
303 usbhs_dcp_dir_for_host(pipe, write);
304 }
305
282 /* "base" will be used below */ 306 /* "base" will be used below */
283 usbhs_write(priv, fifo->sel, base | MBW_32); 307 usbhs_write(priv, fifo->sel, base | MBW_32);
284 308
@@ -297,6 +321,151 @@ static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
297} 321}
298 322
299/* 323/*
324 * DCP status stage
325 */
326static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
327{
328 struct usbhs_pipe *pipe = pkt->pipe;
329 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
330 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
331 struct device *dev = usbhs_priv_to_dev(priv);
332 int ret;
333
334 usbhs_pipe_disable(pipe);
335
336 ret = usbhsf_fifo_select(pipe, fifo, 1);
337 if (ret < 0) {
338 dev_err(dev, "%s() faile\n", __func__);
339 return ret;
340 }
341
342 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
343
344 usbhsf_fifo_clear(pipe, fifo);
345 usbhsf_send_terminator(pipe, fifo);
346
347 usbhsf_fifo_unselect(pipe, fifo);
348
349 usbhsf_tx_irq_ctrl(pipe, 1);
350 usbhs_pipe_enable(pipe);
351
352 return ret;
353}
354
355static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
356{
357 struct usbhs_pipe *pipe = pkt->pipe;
358 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
359 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
360 struct device *dev = usbhs_priv_to_dev(priv);
361 int ret;
362
363 usbhs_pipe_disable(pipe);
364
365 ret = usbhsf_fifo_select(pipe, fifo, 0);
366 if (ret < 0) {
367 dev_err(dev, "%s() fail\n", __func__);
368 return ret;
369 }
370
371 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
372 usbhsf_fifo_clear(pipe, fifo);
373
374 usbhsf_fifo_unselect(pipe, fifo);
375
376 usbhsf_rx_irq_ctrl(pipe, 1);
377 usbhs_pipe_enable(pipe);
378
379 return ret;
380
381}
382
383static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
384{
385 struct usbhs_pipe *pipe = pkt->pipe;
386
387 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
388 usbhsf_tx_irq_ctrl(pipe, 0);
389 else
390 usbhsf_rx_irq_ctrl(pipe, 0);
391
392 pkt->actual = pkt->length;
393 *is_done = 1;
394
395 return 0;
396}
397
398struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
399 .prepare = usbhs_dcp_dir_switch_to_write,
400 .try_run = usbhs_dcp_dir_switch_done,
401};
402
403struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
404 .prepare = usbhs_dcp_dir_switch_to_read,
405 .try_run = usbhs_dcp_dir_switch_done,
406};
407
408/*
409 * DCP data stage (push)
410 */
411static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
412{
413 struct usbhs_pipe *pipe = pkt->pipe;
414
415 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
416
417 /*
418 * change handler to PIO push
419 */
420 pkt->handler = &usbhs_fifo_pio_push_handler;
421
422 return pkt->handler->prepare(pkt, is_done);
423}
424
425struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
426 .prepare = usbhsf_dcp_data_stage_try_push,
427};
428
429/*
430 * DCP data stage (pop)
431 */
432static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
433 int *is_done)
434{
435 struct usbhs_pipe *pipe = pkt->pipe;
436 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
437 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
438
439 if (usbhs_pipe_is_busy(pipe))
440 return 0;
441
442 /*
443 * prepare pop for DCP should
444 * - change DCP direction,
445 * - clear fifo
446 * - DATA1
447 */
448 usbhs_pipe_disable(pipe);
449
450 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
451
452 usbhsf_fifo_select(pipe, fifo, 0);
453 usbhsf_fifo_clear(pipe, fifo);
454 usbhsf_fifo_unselect(pipe, fifo);
455
456 /*
457 * change handler to PIO pop
458 */
459 pkt->handler = &usbhs_fifo_pio_pop_handler;
460
461 return pkt->handler->prepare(pkt, is_done);
462}
463
464struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
465 .prepare = usbhsf_dcp_data_stage_prepare_pop,
466};
467
468/*
300 * PIO push handler 469 * PIO push handler
301 */ 470 */
302static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done) 471static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
@@ -452,6 +621,20 @@ static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
452 total_len = len; 621 total_len = len;
453 622
454 /* 623 /*
624 * update actual length first here to decide disable pipe.
625 * if this pipe keeps BUF status and all data were popped,
626 * then, next interrupt/token will be issued again
627 */
628 pkt->actual += total_len;
629
630 if ((pkt->actual == pkt->length) || /* receive all data */
631 (total_len < maxp)) { /* short packet */
632 *is_done = 1;
633 usbhsf_rx_irq_ctrl(pipe, 0);
634 usbhs_pipe_disable(pipe); /* disable pipe first */
635 }
636
637 /*
455 * Buffer clear if Zero-Length packet 638 * Buffer clear if Zero-Length packet
456 * 639 *
457 * see 640 * see
@@ -481,16 +664,7 @@ static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
481 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; 664 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
482 } 665 }
483 666
484 pkt->actual += total_len;
485
486usbhs_fifo_read_end: 667usbhs_fifo_read_end:
487 if ((pkt->actual == pkt->length) || /* receive all data */
488 (total_len < maxp)) { /* short packet */
489 *is_done = 1;
490 usbhsf_rx_irq_ctrl(pipe, 0);
491 usbhs_pipe_disable(pipe);
492 }
493
494 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n", 668 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
495 usbhs_pipe_number(pipe), 669 usbhs_pipe_number(pipe),
496 pkt->length, pkt->actual, *is_done, pkt->zero); 670 pkt->length, pkt->actual, *is_done, pkt->zero);
@@ -646,7 +820,7 @@ static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
646 if (len % 4) /* 32bit alignment */ 820 if (len % 4) /* 32bit alignment */
647 goto usbhsf_pio_prepare_push; 821 goto usbhsf_pio_prepare_push;
648 822
649 if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ 823 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
650 goto usbhsf_pio_prepare_push; 824 goto usbhsf_pio_prepare_push;
651 825
652 /* get enable DMA fifo */ 826 /* get enable DMA fifo */
@@ -723,7 +897,7 @@ static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
723 if (!fifo) 897 if (!fifo)
724 goto usbhsf_pio_prepare_pop; 898 goto usbhsf_pio_prepare_pop;
725 899
726 if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */ 900 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
727 goto usbhsf_pio_prepare_pop; 901 goto usbhsf_pio_prepare_pop;
728 902
729 ret = usbhsf_fifo_select(pipe, fifo, 0); 903 ret = usbhsf_fifo_select(pipe, fifo, 0);
@@ -884,7 +1058,7 @@ static int usbhsf_irq_empty(struct usbhs_priv *priv,
884 if (!(irq_state->bempsts & (1 << i))) 1058 if (!(irq_state->bempsts & (1 << i)))
885 continue; 1059 continue;
886 1060
887 ret = usbhs_pkt_run(pipe); 1061 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
888 if (ret < 0) 1062 if (ret < 0)
889 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret); 1063 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
890 } 1064 }
@@ -914,7 +1088,7 @@ static int usbhsf_irq_ready(struct usbhs_priv *priv,
914 if (!(irq_state->brdysts & (1 << i))) 1088 if (!(irq_state->brdysts & (1 << i)))
915 continue; 1089 continue;
916 1090
917 ret = usbhs_pkt_run(pipe); 1091 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
918 if (ret < 0) 1092 if (ret < 0)
919 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret); 1093 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
920 } 1094 }
@@ -929,7 +1103,7 @@ static void usbhsf_dma_complete(void *arg)
929 struct device *dev = usbhs_priv_to_dev(priv); 1103 struct device *dev = usbhs_priv_to_dev(priv);
930 int ret; 1104 int ret;
931 1105
932 ret = usbhs_pkt_dmadone(pipe); 1106 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
933 if (ret < 0) 1107 if (ret < 0)
934 dev_err(dev, "dma_complete run_error %d : %d\n", 1108 dev_err(dev, "dma_complete run_error %d : %d\n",
935 usbhs_pipe_number(pipe), ret); 1109 usbhs_pipe_number(pipe), ret);
diff --git a/drivers/usb/renesas_usbhs/fifo.h b/drivers/usb/renesas_usbhs/fifo.h
index ed6d8e56c13c..32a7b246b28d 100644
--- a/drivers/usb/renesas_usbhs/fifo.h
+++ b/drivers/usb/renesas_usbhs/fifo.h
@@ -51,6 +51,8 @@ struct usbhs_pkt {
51 struct list_head node; 51 struct list_head node;
52 struct usbhs_pipe *pipe; 52 struct usbhs_pipe *pipe;
53 struct usbhs_pkt_handle *handler; 53 struct usbhs_pkt_handle *handler;
54 void (*done)(struct usbhs_priv *priv,
55 struct usbhs_pkt *pkt);
54 dma_addr_t dma; 56 dma_addr_t dma;
55 void *buf; 57 void *buf;
56 int length; 58 int length;
@@ -76,12 +78,6 @@ void usbhs_fifo_quit(struct usbhs_priv *priv);
76/* 78/*
77 * packet info 79 * packet info
78 */ 80 */
79enum {
80 USBHSF_PKT_PREPARE,
81 USBHSF_PKT_TRY_RUN,
82 USBHSF_PKT_DMA_DONE,
83};
84
85extern struct usbhs_pkt_handle usbhs_fifo_pio_push_handler; 81extern struct usbhs_pkt_handle usbhs_fifo_pio_push_handler;
86extern struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler; 82extern struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler;
87extern struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler; 83extern struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler;
@@ -89,16 +85,18 @@ extern struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler;
89extern struct usbhs_pkt_handle usbhs_fifo_dma_push_handler; 85extern struct usbhs_pkt_handle usbhs_fifo_dma_push_handler;
90extern struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler; 86extern struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler;
91 87
88extern struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler;
89extern struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler;
90
91extern struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler;
92extern struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler;
92 93
93void usbhs_pkt_init(struct usbhs_pkt *pkt); 94void usbhs_pkt_init(struct usbhs_pkt *pkt);
94void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt, 95void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
95 struct usbhs_pkt_handle *handler, 96 void (*done)(struct usbhs_priv *priv,
97 struct usbhs_pkt *pkt),
96 void *buf, int len, int zero); 98 void *buf, int len, int zero);
97struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt); 99struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt);
98int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type); 100void usbhs_pkt_start(struct usbhs_pipe *pipe);
99
100#define usbhs_pkt_start(p) __usbhs_pkt_handler(p, USBHSF_PKT_PREPARE)
101#define usbhs_pkt_run(p) __usbhs_pkt_handler(p, USBHSF_PKT_TRY_RUN)
102#define usbhs_pkt_dmadone(p) __usbhs_pkt_handler(p, USBHSF_PKT_DMA_DONE)
103 101
104#endif /* RENESAS_USB_FIFO_H */ 102#endif /* RENESAS_USB_FIFO_H */
diff --git a/drivers/usb/renesas_usbhs/mod.c b/drivers/usb/renesas_usbhs/mod.c
index a577f8f4064c..053f86d70009 100644
--- a/drivers/usb/renesas_usbhs/mod.c
+++ b/drivers/usb/renesas_usbhs/mod.c
@@ -58,7 +58,7 @@ void usbhs_mod_autonomy_mode(struct usbhs_priv *priv)
58 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); 58 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
59 59
60 info->irq_vbus = usbhsm_autonomy_irq_vbus; 60 info->irq_vbus = usbhsm_autonomy_irq_vbus;
61 priv->pfunc->get_vbus = usbhsm_autonomy_get_vbus; 61 priv->pfunc.get_vbus = usbhsm_autonomy_get_vbus;
62 62
63 usbhs_irq_callback_update(priv, NULL); 63 usbhs_irq_callback_update(priv, NULL);
64} 64}
@@ -93,8 +93,9 @@ struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
93 return ret; 93 return ret;
94} 94}
95 95
96int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod) 96int usbhs_mod_is_host(struct usbhs_priv *priv)
97{ 97{
98 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
98 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); 99 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
99 100
100 if (!mod) 101 if (!mod)
@@ -139,13 +140,17 @@ int usbhs_mod_probe(struct usbhs_priv *priv)
139 /* 140 /*
140 * install host/gadget driver 141 * install host/gadget driver
141 */ 142 */
142 ret = usbhs_mod_gadget_probe(priv); 143 ret = usbhs_mod_host_probe(priv);
143 if (ret < 0) 144 if (ret < 0)
144 return ret; 145 return ret;
145 146
147 ret = usbhs_mod_gadget_probe(priv);
148 if (ret < 0)
149 goto mod_init_host_err;
150
146 /* irq settings */ 151 /* irq settings */
147 ret = request_irq(priv->irq, usbhs_interrupt, 152 ret = request_irq(priv->irq, usbhs_interrupt,
148 IRQF_DISABLED, dev_name(dev), priv); 153 0, dev_name(dev), priv);
149 if (ret) { 154 if (ret) {
150 dev_err(dev, "irq request err\n"); 155 dev_err(dev, "irq request err\n");
151 goto mod_init_gadget_err; 156 goto mod_init_gadget_err;
@@ -155,12 +160,15 @@ int usbhs_mod_probe(struct usbhs_priv *priv)
155 160
156mod_init_gadget_err: 161mod_init_gadget_err:
157 usbhs_mod_gadget_remove(priv); 162 usbhs_mod_gadget_remove(priv);
163mod_init_host_err:
164 usbhs_mod_host_remove(priv);
158 165
159 return ret; 166 return ret;
160} 167}
161 168
162void usbhs_mod_remove(struct usbhs_priv *priv) 169void usbhs_mod_remove(struct usbhs_priv *priv)
163{ 170{
171 usbhs_mod_host_remove(priv);
164 usbhs_mod_gadget_remove(priv); 172 usbhs_mod_gadget_remove(priv);
165 free_irq(priv->irq, priv); 173 free_irq(priv->irq, priv);
166} 174}
@@ -168,20 +176,6 @@ void usbhs_mod_remove(struct usbhs_priv *priv)
168/* 176/*
169 * status functions 177 * status functions
170 */ 178 */
171int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
172{
173 switch (irq_state->dvstctr & RHST) {
174 case RHST_LOW_SPEED:
175 return USB_SPEED_LOW;
176 case RHST_FULL_SPEED:
177 return USB_SPEED_FULL;
178 case RHST_HIGH_SPEED:
179 return USB_SPEED_HIGH;
180 }
181
182 return USB_SPEED_UNKNOWN;
183}
184
185int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state) 179int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
186{ 180{
187 int state = irq_state->intsts0 & DVSQ_MASK; 181 int state = irq_state->intsts0 & DVSQ_MASK;
@@ -221,8 +215,6 @@ static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
221 state->intsts0 = usbhs_read(priv, INTSTS0); 215 state->intsts0 = usbhs_read(priv, INTSTS0);
222 state->intsts1 = usbhs_read(priv, INTSTS1); 216 state->intsts1 = usbhs_read(priv, INTSTS1);
223 217
224 state->dvstctr = usbhs_read(priv, DVSTCTR);
225
226 /* mask */ 218 /* mask */
227 if (mod) { 219 if (mod) {
228 state->brdysts = usbhs_read(priv, BRDYSTS); 220 state->brdysts = usbhs_read(priv, BRDYSTS);
@@ -269,6 +261,8 @@ static irqreturn_t usbhs_interrupt(int irq, void *data)
269 * see also 261 * see also
270 * usbhs_irq_setting_update 262 * usbhs_irq_setting_update
271 */ 263 */
264
265 /* INTSTS0 */
272 if (irq_state.intsts0 & VBINT) 266 if (irq_state.intsts0 & VBINT)
273 usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state); 267 usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state);
274 268
@@ -284,15 +278,38 @@ static irqreturn_t usbhs_interrupt(int irq, void *data)
284 if (irq_state.intsts0 & BRDY) 278 if (irq_state.intsts0 & BRDY)
285 usbhs_mod_call(priv, irq_ready, priv, &irq_state); 279 usbhs_mod_call(priv, irq_ready, priv, &irq_state);
286 280
281 /* INTSTS1 */
282 if (irq_state.intsts1 & ATTCH)
283 usbhs_mod_call(priv, irq_attch, priv, &irq_state);
284
285 if (irq_state.intsts1 & DTCH)
286 usbhs_mod_call(priv, irq_dtch, priv, &irq_state);
287
288 if (irq_state.intsts1 & SIGN)
289 usbhs_mod_call(priv, irq_sign, priv, &irq_state);
290
291 if (irq_state.intsts1 & SACK)
292 usbhs_mod_call(priv, irq_sack, priv, &irq_state);
293
287 return IRQ_HANDLED; 294 return IRQ_HANDLED;
288} 295}
289 296
290void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod) 297void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
291{ 298{
292 u16 intenb0 = 0; 299 u16 intenb0 = 0;
300 u16 intenb1 = 0;
293 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); 301 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
294 302
303 /*
304 * BEMPENB/BRDYENB are picky.
305 * below method is required
306 *
307 * - clear INTSTS0
308 * - update BEMPENB/BRDYENB
309 * - update INTSTS0
310 */
295 usbhs_write(priv, INTENB0, 0); 311 usbhs_write(priv, INTENB0, 0);
312 usbhs_write(priv, INTENB1, 0);
296 313
297 usbhs_write(priv, BEMPENB, 0); 314 usbhs_write(priv, BEMPENB, 0);
298 usbhs_write(priv, BRDYENB, 0); 315 usbhs_write(priv, BRDYENB, 0);
@@ -310,6 +327,9 @@ void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
310 intenb0 |= VBSE; 327 intenb0 |= VBSE;
311 328
312 if (mod) { 329 if (mod) {
330 /*
331 * INTSTS0
332 */
313 if (mod->irq_ctrl_stage) 333 if (mod->irq_ctrl_stage)
314 intenb0 |= CTRE; 334 intenb0 |= CTRE;
315 335
@@ -322,7 +342,26 @@ void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
322 usbhs_write(priv, BRDYENB, mod->irq_brdysts); 342 usbhs_write(priv, BRDYENB, mod->irq_brdysts);
323 intenb0 |= BRDYE; 343 intenb0 |= BRDYE;
324 } 344 }
345
346 /*
347 * INTSTS1
348 */
349 if (mod->irq_attch)
350 intenb1 |= ATTCHE;
351
352 if (mod->irq_attch)
353 intenb1 |= DTCHE;
354
355 if (mod->irq_sign)
356 intenb1 |= SIGNE;
357
358 if (mod->irq_sack)
359 intenb1 |= SACKE;
325 } 360 }
326 361
327 usbhs_write(priv, INTENB0, intenb0); 362 if (intenb0)
363 usbhs_write(priv, INTENB0, intenb0);
364
365 if (intenb1)
366 usbhs_write(priv, INTENB1, intenb1);
328} 367}
diff --git a/drivers/usb/renesas_usbhs/mod.h b/drivers/usb/renesas_usbhs/mod.h
index 5c845a28a21c..6c6875533f01 100644
--- a/drivers/usb/renesas_usbhs/mod.h
+++ b/drivers/usb/renesas_usbhs/mod.h
@@ -30,7 +30,6 @@ struct usbhs_irq_state {
30 u16 brdysts; 30 u16 brdysts;
31 u16 nrdysts; 31 u16 nrdysts;
32 u16 bempsts; 32 u16 bempsts;
33 u16 dvstctr;
34}; 33};
35 34
36struct usbhs_mod { 35struct usbhs_mod {
@@ -42,26 +41,48 @@ struct usbhs_mod {
42 int (*start)(struct usbhs_priv *priv); 41 int (*start)(struct usbhs_priv *priv);
43 int (*stop)(struct usbhs_priv *priv); 42 int (*stop)(struct usbhs_priv *priv);
44 43
45 /* INTSTS0 :: DVST (DVSQ) */ 44 /*
45 * INTSTS0
46 */
47
48 /* DVST (DVSQ) */
46 int (*irq_dev_state)(struct usbhs_priv *priv, 49 int (*irq_dev_state)(struct usbhs_priv *priv,
47 struct usbhs_irq_state *irq_state); 50 struct usbhs_irq_state *irq_state);
48 51
49 /* INTSTS0 :: CTRT (CTSQ) */ 52 /* CTRT (CTSQ) */
50 int (*irq_ctrl_stage)(struct usbhs_priv *priv, 53 int (*irq_ctrl_stage)(struct usbhs_priv *priv,
51 struct usbhs_irq_state *irq_state); 54 struct usbhs_irq_state *irq_state);
52 55
53 /* INTSTS0 :: BEMP */ 56 /* BEMP / BEMPSTS */
54 /* BEMPSTS */
55 int (*irq_empty)(struct usbhs_priv *priv, 57 int (*irq_empty)(struct usbhs_priv *priv,
56 struct usbhs_irq_state *irq_state); 58 struct usbhs_irq_state *irq_state);
57 u16 irq_bempsts; 59 u16 irq_bempsts;
58 60
59 /* INTSTS0 :: BRDY */ 61 /* BRDY / BRDYSTS */
60 /* BRDYSTS */
61 int (*irq_ready)(struct usbhs_priv *priv, 62 int (*irq_ready)(struct usbhs_priv *priv,
62 struct usbhs_irq_state *irq_state); 63 struct usbhs_irq_state *irq_state);
63 u16 irq_brdysts; 64 u16 irq_brdysts;
64 65
66 /*
67 * INTSTS1
68 */
69
70 /* ATTCHE */
71 int (*irq_attch)(struct usbhs_priv *priv,
72 struct usbhs_irq_state *irq_state);
73
74 /* DTCHE */
75 int (*irq_dtch)(struct usbhs_priv *priv,
76 struct usbhs_irq_state *irq_state);
77
78 /* SIGN */
79 int (*irq_sign)(struct usbhs_priv *priv,
80 struct usbhs_irq_state *irq_state);
81
82 /* SACK */
83 int (*irq_sack)(struct usbhs_priv *priv,
84 struct usbhs_irq_state *irq_state);
85
65 struct usbhs_priv *priv; 86 struct usbhs_priv *priv;
66}; 87};
67 88
@@ -89,7 +110,7 @@ struct usbhs_mod_info {
89struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id); 110struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id);
90struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv); 111struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv);
91void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *usb, int id); 112void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *usb, int id);
92int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod); 113int usbhs_mod_is_host(struct usbhs_priv *priv);
93int usbhs_mod_change(struct usbhs_priv *priv, int id); 114int usbhs_mod_change(struct usbhs_priv *priv, int id);
94int usbhs_mod_probe(struct usbhs_priv *priv); 115int usbhs_mod_probe(struct usbhs_priv *priv);
95void usbhs_mod_remove(struct usbhs_priv *priv); 116void usbhs_mod_remove(struct usbhs_priv *priv);
@@ -99,7 +120,6 @@ void usbhs_mod_autonomy_mode(struct usbhs_priv *priv);
99/* 120/*
100 * status functions 121 * status functions
101 */ 122 */
102int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state);
103int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state); 123int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state);
104int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state); 124int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state);
105 125
@@ -119,11 +139,26 @@ void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod);
119 }) 139 })
120 140
121/* 141/*
122 * gadget control 142 * host / gadget control
123 */ 143 */
124#ifdef CONFIG_USB_RENESAS_USBHS_UDC 144#if defined(CONFIG_USB_RENESAS_USBHS_HCD) || \
125extern int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv); 145 defined(CONFIG_USB_RENESAS_USBHS_HCD_MODULE)
126extern void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv); 146extern int usbhs_mod_host_probe(struct usbhs_priv *priv);
147extern int usbhs_mod_host_remove(struct usbhs_priv *priv);
148#else
149static inline int usbhs_mod_host_probe(struct usbhs_priv *priv)
150{
151 return 0;
152}
153static inline void usbhs_mod_host_remove(struct usbhs_priv *priv)
154{
155}
156#endif
157
158#if defined(CONFIG_USB_RENESAS_USBHS_UDC) || \
159 defined(CONFIG_USB_RENESAS_USBHS_UDC_MODULE)
160extern int usbhs_mod_gadget_probe(struct usbhs_priv *priv);
161extern void usbhs_mod_gadget_remove(struct usbhs_priv *priv);
127#else 162#else
128static inline int usbhs_mod_gadget_probe(struct usbhs_priv *priv) 163static inline int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
129{ 164{
diff --git a/drivers/usb/renesas_usbhs/mod_gadget.c b/drivers/usb/renesas_usbhs/mod_gadget.c
index cb2d451d511e..d9717e0bc1ff 100644
--- a/drivers/usb/renesas_usbhs/mod_gadget.c
+++ b/drivers/usb/renesas_usbhs/mod_gadget.c
@@ -39,7 +39,6 @@ struct usbhsg_uep {
39 char ep_name[EP_NAME_SIZE]; 39 char ep_name[EP_NAME_SIZE];
40 40
41 struct usbhsg_gpriv *gpriv; 41 struct usbhsg_gpriv *gpriv;
42 struct usbhs_pkt_handle *handler;
43}; 42};
44 43
45struct usbhsg_gpriv { 44struct usbhsg_gpriv {
@@ -128,25 +127,6 @@ LIST_HEAD(the_controller_link);
128/* 127/*
129 * queue push/pop 128 * queue push/pop
130 */ 129 */
131static void usbhsg_queue_push(struct usbhsg_uep *uep,
132 struct usbhsg_request *ureq)
133{
134 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
135 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
136 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
137 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
138 struct usb_request *req = &ureq->req;
139
140 req->actual = 0;
141 req->status = -EINPROGRESS;
142 usbhs_pkt_push(pipe, pkt, uep->handler,
143 req->buf, req->length, req->zero);
144
145 dev_dbg(dev, "pipe %d : queue push (%d)\n",
146 usbhs_pipe_number(pipe),
147 req->length);
148}
149
150static void usbhsg_queue_pop(struct usbhsg_uep *uep, 130static void usbhsg_queue_pop(struct usbhsg_uep *uep,
151 struct usbhsg_request *ureq, 131 struct usbhsg_request *ureq,
152 int status) 132 int status)
@@ -161,7 +141,7 @@ static void usbhsg_queue_pop(struct usbhsg_uep *uep,
161 ureq->req.complete(&uep->ep, &ureq->req); 141 ureq->req.complete(&uep->ep, &ureq->req);
162} 142}
163 143
164static void usbhsg_queue_done(struct usbhs_pkt *pkt) 144static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
165{ 145{
166 struct usbhs_pipe *pipe = pkt->pipe; 146 struct usbhs_pipe *pipe = pkt->pipe;
167 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); 147 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
@@ -172,6 +152,26 @@ static void usbhsg_queue_done(struct usbhs_pkt *pkt)
172 usbhsg_queue_pop(uep, ureq, 0); 152 usbhsg_queue_pop(uep, ureq, 0);
173} 153}
174 154
155static void usbhsg_queue_push(struct usbhsg_uep *uep,
156 struct usbhsg_request *ureq)
157{
158 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
159 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
160 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
161 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
162 struct usb_request *req = &ureq->req;
163
164 req->actual = 0;
165 req->status = -EINPROGRESS;
166 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
167 req->buf, req->length, req->zero);
168 usbhs_pkt_start(pipe);
169
170 dev_dbg(dev, "pipe %d : queue push (%d)\n",
171 usbhs_pipe_number(pipe),
172 req->length);
173}
174
175/* 175/*
176 * dma map/unmap 176 * dma map/unmap
177 */ 177 */
@@ -265,7 +265,7 @@ static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
265 265
266 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { 266 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
267 usbhs_pipe_disable(pipe); 267 usbhs_pipe_disable(pipe);
268 usbhs_pipe_clear_sequence(pipe); 268 usbhs_pipe_sequence_data0(pipe);
269 usbhs_pipe_enable(pipe); 269 usbhs_pipe_enable(pipe);
270 } 270 }
271 271
@@ -355,7 +355,7 @@ static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
355 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 355 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
356 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 356 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
357 357
358 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state); 358 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
359 359
360 dev_dbg(dev, "state = %x : speed : %d\n", 360 dev_dbg(dev, "state = %x : speed : %d\n",
361 usbhs_status_get_device_state(irq_state), 361 usbhs_status_get_device_state(irq_state),
@@ -389,13 +389,13 @@ static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
389 389
390 switch (stage) { 390 switch (stage) {
391 case READ_DATA_STAGE: 391 case READ_DATA_STAGE:
392 dcp->handler = &usbhs_fifo_pio_push_handler; 392 pipe->handler = &usbhs_fifo_pio_push_handler;
393 break; 393 break;
394 case WRITE_DATA_STAGE: 394 case WRITE_DATA_STAGE:
395 dcp->handler = &usbhs_fifo_pio_pop_handler; 395 pipe->handler = &usbhs_fifo_pio_pop_handler;
396 break; 396 break;
397 case NODATA_STATUS_STAGE: 397 case NODATA_STATUS_STAGE:
398 dcp->handler = &usbhs_ctrl_stage_end_handler; 398 pipe->handler = &usbhs_ctrl_stage_end_handler;
399 break; 399 break;
400 default: 400 default:
401 return ret; 401 return ret;
@@ -479,24 +479,31 @@ static int usbhsg_ep_enable(struct usb_ep *ep,
479 */ 479 */
480 if (uep->pipe) { 480 if (uep->pipe) {
481 usbhs_pipe_clear(uep->pipe); 481 usbhs_pipe_clear(uep->pipe);
482 usbhs_pipe_clear_sequence(uep->pipe); 482 usbhs_pipe_sequence_data0(uep->pipe);
483 return 0; 483 return 0;
484 } 484 }
485 485
486 pipe = usbhs_pipe_malloc(priv, desc); 486 pipe = usbhs_pipe_malloc(priv,
487 usb_endpoint_type(desc),
488 usb_endpoint_dir_in(desc));
487 if (pipe) { 489 if (pipe) {
488 uep->pipe = pipe; 490 uep->pipe = pipe;
489 pipe->mod_private = uep; 491 pipe->mod_private = uep;
490 492
493 /* set epnum / maxp */
494 usbhs_pipe_config_update(pipe, 0,
495 usb_endpoint_num(desc),
496 usb_endpoint_maxp(desc));
497
491 /* 498 /*
492 * usbhs_fifo_dma_push/pop_handler try to 499 * usbhs_fifo_dma_push/pop_handler try to
493 * use dmaengine if possible. 500 * use dmaengine if possible.
494 * It will use pio handler if impossible. 501 * It will use pio handler if impossible.
495 */ 502 */
496 if (usb_endpoint_dir_in(desc)) 503 if (usb_endpoint_dir_in(desc))
497 uep->handler = &usbhs_fifo_dma_push_handler; 504 pipe->handler = &usbhs_fifo_dma_push_handler;
498 else 505 else
499 uep->handler = &usbhs_fifo_dma_pop_handler; 506 pipe->handler = &usbhs_fifo_dma_pop_handler;
500 507
501 ret = 0; 508 ret = 0;
502 } 509 }
@@ -659,7 +666,6 @@ static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
659 * pipe initialize and enable DCP 666 * pipe initialize and enable DCP
660 */ 667 */
661 usbhs_pipe_init(priv, 668 usbhs_pipe_init(priv,
662 usbhsg_queue_done,
663 usbhsg_dma_map_ctrl); 669 usbhsg_dma_map_ctrl);
664 usbhs_fifo_init(priv); 670 usbhs_fifo_init(priv);
665 usbhsg_uep_init(gpriv); 671 usbhsg_uep_init(gpriv);
@@ -667,6 +673,7 @@ static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
667 /* dcp init */ 673 /* dcp init */
668 dcp->pipe = usbhs_dcp_malloc(priv); 674 dcp->pipe = usbhs_dcp_malloc(priv);
669 dcp->pipe->mod_private = dcp; 675 dcp->pipe->mod_private = dcp;
676 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
670 677
671 /* 678 /*
672 * system config enble 679 * system config enble
@@ -730,10 +737,6 @@ static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
730 737
731 usbhsg_pipe_disable(dcp); 738 usbhsg_pipe_disable(dcp);
732 739
733 if (gpriv->driver &&
734 gpriv->driver->disconnect)
735 gpriv->driver->disconnect(&gpriv->gadget);
736
737 dev_dbg(dev, "stop gadget\n"); 740 dev_dbg(dev, "stop gadget\n");
738 741
739 return 0; 742 return 0;
@@ -744,31 +747,19 @@ static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
744 * linux usb function 747 * linux usb function
745 * 748 *
746 */ 749 */
747static int usbhsg_gadget_start(struct usb_gadget_driver *driver, 750static int usbhsg_gadget_start(struct usb_gadget *gadget,
748 int (*bind)(struct usb_gadget *)) 751 struct usb_gadget_driver *driver)
749{ 752{
750 struct usbhsg_gpriv *gpriv; 753 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
751 struct usbhs_priv *priv; 754 struct usbhs_priv *priv;
752 struct device *dev; 755 struct device *dev;
753 int ret; 756 int ret;
754 757
755 if (!bind || 758 if (!driver ||
756 !driver ||
757 !driver->setup || 759 !driver->setup ||
758 driver->speed != USB_SPEED_HIGH) 760 driver->speed != USB_SPEED_HIGH)
759 return -EINVAL; 761 return -EINVAL;
760 762
761 /*
762 * find unused controller
763 */
764 usbhsg_for_each_controller(gpriv) {
765 if (!gpriv->driver)
766 goto find_unused_controller;
767 }
768 return -ENODEV;
769
770find_unused_controller:
771
772 dev = usbhsg_gpriv_to_dev(gpriv); 763 dev = usbhsg_gpriv_to_dev(gpriv);
773 priv = usbhsg_gpriv_to_priv(gpriv); 764 priv = usbhsg_gpriv_to_priv(gpriv);
774 765
@@ -782,19 +773,8 @@ find_unused_controller:
782 goto add_fail; 773 goto add_fail;
783 } 774 }
784 775
785 ret = bind(&gpriv->gadget);
786 if (ret) {
787 dev_err(dev, "bind to driver %s error %d\n",
788 driver->driver.name, ret);
789 goto bind_fail;
790 }
791
792 dev_dbg(dev, "bind %s\n", driver->driver.name);
793
794 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); 776 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
795 777
796bind_fail:
797 device_del(&gpriv->gadget.dev);
798add_fail: 778add_fail:
799 gpriv->driver = NULL; 779 gpriv->driver = NULL;
800 gpriv->gadget.dev.driver = NULL; 780 gpriv->gadget.dev.driver = NULL;
@@ -802,9 +782,10 @@ add_fail:
802 return ret; 782 return ret;
803} 783}
804 784
805static int usbhsg_gadget_stop(struct usb_gadget_driver *driver) 785static int usbhsg_gadget_stop(struct usb_gadget *gadget,
786 struct usb_gadget_driver *driver)
806{ 787{
807 struct usbhsg_gpriv *gpriv; 788 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
808 struct usbhs_priv *priv; 789 struct usbhs_priv *priv;
809 struct device *dev; 790 struct device *dev;
810 791
@@ -812,17 +793,6 @@ static int usbhsg_gadget_stop(struct usb_gadget_driver *driver)
812 !driver->unbind) 793 !driver->unbind)
813 return -EINVAL; 794 return -EINVAL;
814 795
815 /*
816 * find controller
817 */
818 usbhsg_for_each_controller(gpriv) {
819 if (gpriv->driver == driver)
820 goto find_matching_controller;
821 }
822 return -ENODEV;
823
824find_matching_controller:
825
826 dev = usbhsg_gpriv_to_dev(gpriv); 796 dev = usbhsg_gpriv_to_dev(gpriv);
827 priv = usbhsg_gpriv_to_priv(gpriv); 797 priv = usbhsg_gpriv_to_priv(gpriv);
828 798
@@ -830,12 +800,6 @@ find_matching_controller:
830 device_del(&gpriv->gadget.dev); 800 device_del(&gpriv->gadget.dev);
831 gpriv->driver = NULL; 801 gpriv->driver = NULL;
832 802
833 if (driver->disconnect)
834 driver->disconnect(&gpriv->gadget);
835
836 driver->unbind(&gpriv->gadget);
837 dev_dbg(dev, "unbind %s\n", driver->driver.name);
838
839 return 0; 803 return 0;
840} 804}
841 805
@@ -852,8 +816,8 @@ static int usbhsg_get_frame(struct usb_gadget *gadget)
852 816
853static struct usb_gadget_ops usbhsg_gadget_ops = { 817static struct usb_gadget_ops usbhsg_gadget_ops = {
854 .get_frame = usbhsg_get_frame, 818 .get_frame = usbhsg_get_frame,
855 .start = usbhsg_gadget_start, 819 .udc_start = usbhsg_gadget_start,
856 .stop = usbhsg_gadget_stop, 820 .udc_stop = usbhsg_gadget_stop,
857}; 821};
858 822
859static int usbhsg_start(struct usbhs_priv *priv) 823static int usbhsg_start(struct usbhs_priv *priv)
@@ -866,7 +830,7 @@ static int usbhsg_stop(struct usbhs_priv *priv)
866 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); 830 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
867} 831}
868 832
869int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv) 833int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
870{ 834{
871 struct usbhsg_gpriv *gpriv; 835 struct usbhsg_gpriv *gpriv;
872 struct usbhsg_uep *uep; 836 struct usbhsg_uep *uep;
@@ -963,7 +927,7 @@ usbhs_mod_gadget_probe_err_gpriv:
963 return ret; 927 return ret;
964} 928}
965 929
966void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv) 930void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
967{ 931{
968 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 932 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
969 933
diff --git a/drivers/usb/renesas_usbhs/mod_host.c b/drivers/usb/renesas_usbhs/mod_host.c
new file mode 100644
index 000000000000..bade761a1e52
--- /dev/null
+++ b/drivers/usb/renesas_usbhs/mod_host.c
@@ -0,0 +1,1327 @@
1/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/io.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/usb.h>
23#include <linux/usb/hcd.h>
24#include "common.h"
25
26/*
27 *** HARDWARE LIMITATION ***
28 *
29 * 1) renesas_usbhs has a limited number of controllable devices.
30 * it can control only 9 devices in generally.
31 * see DEVADDn / DCPMAXP / PIPEMAXP.
32 *
33 * 2) renesas_usbhs pipe number is limited.
34 * the pipe will be re-used for each devices.
35 * so, software should control DATA0/1 sequence of each devices.
36 */
37
38
39/*
40 * image of mod_host
41 *
42 * +--------+
43 * | udev 0 | --> it is used when set address
44 * +--------+
45 *
46 * +--------+ pipes are reused for each uep.
47 * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
48 * +--------+ | | target device was changed
49 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
51 * +- [uep 2 (bulk)] --|---|---+ +--------------+
52 * | | | | pipe1 (isoc) |
53 * +--------+ | | | +--------------+
54 * | udev 2 |-+- [uep 0 (dcp) ] --+ +-- |------> | pipe2 (bulk) |
55 * +--------+ | | | | +--------------+
56 * +- [uep 1 (int) ] --|-+ | +------> | pipe3 (bulk) |
57 * | | | | +--------------+
58 * +--------+ | +-|---|------> | pipe4 (int) |
59 * | udev 3 |-+- [uep 0 (dcp) ] --+ | | +--------------+
60 * +--------+ | | | | .... |
61 * +- [uep 1 (bulk)] ------+ | | .... |
62 * | |
63 * +- [uep 2 (bulk)]-----------+
64 */
65
66
67/*
68 * struct
69 */
70struct usbhsh_pipe_info {
71 unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */
72};
73
74struct usbhsh_request {
75 struct urb *urb;
76 struct usbhs_pkt pkt;
77 struct list_head ureq_link; /* see hpriv :: ureq_link_xxx */
78};
79
80struct usbhsh_device {
81 struct usb_device *usbv;
82 struct list_head ep_list_head; /* list of usbhsh_ep */
83};
84
85struct usbhsh_ep {
86 struct usbhs_pipe *pipe;
87 struct usbhsh_device *udev; /* attached udev */
88 struct list_head ep_list; /* list to usbhsh_device */
89
90 int maxp;
91};
92
93#define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
94#define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
95struct usbhsh_hpriv {
96 struct usbhs_mod mod;
97 struct usbhs_pipe *dcp;
98
99 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
100
101 struct usbhsh_pipe_info *pipe_info;
102 int pipe_size;
103
104 u32 port_stat; /* USB_PORT_STAT_xxx */
105
106 struct completion setup_ack_done;
107
108 /* see usbhsh_req_alloc/free */
109 struct list_head ureq_link_active;
110 struct list_head ureq_link_free;
111};
112
113
114static const char usbhsh_hcd_name[] = "renesas_usbhs host";
115
116/*
117 * macro
118 */
119#define usbhsh_priv_to_hpriv(priv) \
120 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
121
122#define __usbhsh_for_each_hpipe(start, pos, h, i) \
123 for (i = start, pos = (h)->hpipe + i; \
124 i < (h)->hpipe_size; \
125 i++, pos = (h)->hpipe + i)
126
127#define usbhsh_for_each_hpipe(pos, hpriv, i) \
128 __usbhsh_for_each_hpipe(1, pos, hpriv, i)
129
130#define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \
131 __usbhsh_for_each_hpipe(0, pos, hpriv, i)
132
133#define __usbhsh_for_each_udev(start, pos, h, i) \
134 for (i = start, pos = (h)->udev + i; \
135 i < USBHSH_DEVICE_MAX; \
136 i++, pos = (h)->udev + i)
137
138#define usbhsh_for_each_udev(pos, hpriv, i) \
139 __usbhsh_for_each_udev(1, pos, hpriv, i)
140
141#define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
142 __usbhsh_for_each_udev(0, pos, hpriv, i)
143
144#define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
145#define usbhsh_hcd_to_dev(h) ((h)->self.controller)
146
147#define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
148#define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
149#define usbhsh_hpriv_to_hcd(h) \
150 container_of((void *)h, struct usb_hcd, hcd_priv)
151
152#define usbhsh_ep_to_uep(u) ((u)->hcpriv)
153#define usbhsh_uep_to_pipe(u) ((u)->pipe)
154#define usbhsh_uep_to_udev(u) ((u)->udev)
155#define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
156#define usbhsh_urb_to_usbv(u) ((u)->dev)
157
158#define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
159
160#define usbhsh_udev_to_usbv(h) ((h)->usbv)
161
162#define usbhsh_pipe_info(p) ((p)->mod_private)
163
164#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
165#define usbhsh_device_nth(h, d) ((h)->udev + d)
166#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
167
168#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
169#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
170#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
171#define usbhsh_port_stat_get(h) ((h)->port_stat)
172
173#define usbhsh_pkt_to_req(p) \
174 container_of((void *)p, struct usbhsh_request, pkt)
175
176/*
177 * req alloc/free
178 */
179static void usbhsh_req_list_init(struct usbhsh_hpriv *hpriv)
180{
181 INIT_LIST_HEAD(&hpriv->ureq_link_active);
182 INIT_LIST_HEAD(&hpriv->ureq_link_free);
183}
184
185static void usbhsh_req_list_quit(struct usbhsh_hpriv *hpriv)
186{
187 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
188 struct device *dev = usbhsh_hcd_to_dev(hcd);
189 struct usbhsh_request *ureq, *next;
190
191 /* kfree all active ureq */
192 list_for_each_entry_safe(ureq, next,
193 &hpriv->ureq_link_active,
194 ureq_link) {
195 dev_err(dev, "active ureq (%p) is force freed\n", ureq);
196 kfree(ureq);
197 }
198
199 /* kfree all free ureq */
200 list_for_each_entry_safe(ureq, next, &hpriv->ureq_link_free, ureq_link)
201 kfree(ureq);
202}
203
204static struct usbhsh_request *usbhsh_req_alloc(struct usbhsh_hpriv *hpriv,
205 struct urb *urb,
206 gfp_t mem_flags)
207{
208 struct usbhsh_request *ureq;
209 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
210 struct device *dev = usbhs_priv_to_dev(priv);
211
212 if (list_empty(&hpriv->ureq_link_free)) {
213 /*
214 * create new one if there is no free ureq
215 */
216 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
217 if (ureq)
218 INIT_LIST_HEAD(&ureq->ureq_link);
219 } else {
220 /*
221 * reuse "free" ureq if exist
222 */
223 ureq = list_entry(hpriv->ureq_link_free.next,
224 struct usbhsh_request,
225 ureq_link);
226 if (ureq)
227 list_del_init(&ureq->ureq_link);
228 }
229
230 if (!ureq) {
231 dev_err(dev, "ureq alloc fail\n");
232 return NULL;
233 }
234
235 usbhs_pkt_init(&ureq->pkt);
236
237 /*
238 * push it to "active" list
239 */
240 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_active);
241 ureq->urb = urb;
242
243 return ureq;
244}
245
246static void usbhsh_req_free(struct usbhsh_hpriv *hpriv,
247 struct usbhsh_request *ureq)
248{
249 struct usbhs_pkt *pkt = &ureq->pkt;
250
251 usbhs_pkt_init(pkt);
252
253 /*
254 * removed from "active" list,
255 * and push it to "free" list
256 */
257 ureq->urb = NULL;
258 list_del_init(&ureq->ureq_link);
259 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_free);
260}
261
262/*
263 * device control
264 */
265
266static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
267{
268 return !list_empty(&udev->ep_list_head);
269}
270
271static struct usbhsh_device *usbhsh_device_alloc(struct usbhsh_hpriv *hpriv,
272 struct urb *urb)
273{
274 struct usbhsh_device *udev = NULL;
275 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
276 struct device *dev = usbhsh_hcd_to_dev(hcd);
277 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
278 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
279 int i;
280
281 /*
282 * device 0
283 */
284 if (0 == usb_pipedevice(urb->pipe)) {
285 udev = usbhsh_device0(hpriv);
286 goto usbhsh_device_find;
287 }
288
289 /*
290 * find unused device
291 */
292 usbhsh_for_each_udev(udev, hpriv, i) {
293 if (usbhsh_udev_to_usbv(udev))
294 continue;
295 goto usbhsh_device_find;
296 }
297
298 dev_err(dev, "no free usbhsh_device\n");
299
300 return NULL;
301
302usbhsh_device_find:
303 if (usbhsh_device_has_endpoint(udev))
304 dev_warn(dev, "udev have old endpoint\n");
305
306 /* uep will be attached */
307 INIT_LIST_HEAD(&udev->ep_list_head);
308
309 /*
310 * usbhsh_usbv_to_udev()
311 * usbhsh_udev_to_usbv()
312 * will be enable
313 */
314 dev_set_drvdata(&usbv->dev, udev);
315 udev->usbv = usbv;
316
317 /* set device config */
318 usbhs_set_device_speed(priv,
319 usbhsh_device_number(hpriv, udev),
320 usbhsh_device_number(hpriv, udev),
321 0, /* FIXME no parent */
322 usbv->speed);
323
324 dev_dbg(dev, "%s [%d](%p)\n", __func__,
325 usbhsh_device_number(hpriv, udev), udev);
326
327 return udev;
328}
329
330static void usbhsh_device_free(struct usbhsh_hpriv *hpriv,
331 struct usbhsh_device *udev)
332{
333 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
334 struct device *dev = usbhsh_hcd_to_dev(hcd);
335 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
336
337 dev_dbg(dev, "%s [%d](%p)\n", __func__,
338 usbhsh_device_number(hpriv, udev), udev);
339
340 if (usbhsh_device_has_endpoint(udev))
341 dev_warn(dev, "udev still have endpoint\n");
342
343 /*
344 * usbhsh_usbv_to_udev()
345 * usbhsh_udev_to_usbv()
346 * will be disable
347 */
348 dev_set_drvdata(&usbv->dev, NULL);
349 udev->usbv = NULL;
350}
351
352/*
353 * end-point control
354 */
355struct usbhsh_ep *usbhsh_endpoint_alloc(struct usbhsh_hpriv *hpriv,
356 struct usbhsh_device *udev,
357 struct usb_host_endpoint *ep,
358 int dir_in_req,
359 gfp_t mem_flags)
360{
361 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
362 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
363 struct usbhsh_ep *uep;
364 struct usbhsh_pipe_info *info;
365 struct usbhs_pipe *pipe, *best_pipe;
366 struct device *dev = usbhsh_hcd_to_dev(hcd);
367 struct usb_endpoint_descriptor *desc = &ep->desc;
368 int type, i, dir_in;
369 unsigned int min_usr;
370
371 dir_in_req = !!dir_in_req;
372
373 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
374 if (!uep) {
375 dev_err(dev, "usbhsh_ep alloc fail\n");
376 return NULL;
377 }
378
379 if (usb_endpoint_xfer_control(desc)) {
380 best_pipe = usbhsh_hpriv_to_dcp(hpriv);
381 goto usbhsh_endpoint_alloc_find_pipe;
382 }
383
384 /*
385 * find best pipe for endpoint
386 * see
387 * HARDWARE LIMITATION
388 */
389 type = usb_endpoint_type(desc);
390 min_usr = ~0;
391 best_pipe = NULL;
392 usbhs_for_each_pipe(pipe, priv, i) {
393 if (!usbhs_pipe_type_is(pipe, type))
394 continue;
395
396 dir_in = !!usbhs_pipe_is_dir_in(pipe);
397 if (0 != (dir_in - dir_in_req))
398 continue;
399
400 info = usbhsh_pipe_info(pipe);
401
402 if (min_usr > info->usr_cnt) {
403 min_usr = info->usr_cnt;
404 best_pipe = pipe;
405 }
406 }
407
408 if (unlikely(!best_pipe)) {
409 dev_err(dev, "couldn't find best pipe\n");
410 kfree(uep);
411 return NULL;
412 }
413usbhsh_endpoint_alloc_find_pipe:
414 /*
415 * init uep
416 */
417 uep->pipe = best_pipe;
418 uep->maxp = usb_endpoint_maxp(desc);
419 usbhsh_uep_to_udev(uep) = udev;
420 usbhsh_ep_to_uep(ep) = uep;
421
422 /*
423 * update pipe user count
424 */
425 info = usbhsh_pipe_info(best_pipe);
426 info->usr_cnt++;
427
428 /* init this endpoint, and attach it to udev */
429 INIT_LIST_HEAD(&uep->ep_list);
430 list_add_tail(&uep->ep_list, &udev->ep_list_head);
431
432 /*
433 * usbhs_pipe_config_update() should be called after
434 * usbhs_device_config()
435 * see
436 * DCPMAXP/PIPEMAXP
437 */
438 usbhs_pipe_sequence_data0(uep->pipe);
439 usbhs_pipe_config_update(uep->pipe,
440 usbhsh_device_number(hpriv, udev),
441 usb_endpoint_num(desc),
442 uep->maxp);
443
444 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
445 usbhsh_device_number(hpriv, udev),
446 usbhs_pipe_name(uep->pipe), uep);
447
448 return uep;
449}
450
451void usbhsh_endpoint_free(struct usbhsh_hpriv *hpriv,
452 struct usb_host_endpoint *ep)
453{
454 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
455 struct device *dev = usbhs_priv_to_dev(priv);
456 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
457 struct usbhsh_pipe_info *info;
458
459 if (!uep)
460 return;
461
462 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
463 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
464 usbhs_pipe_name(uep->pipe), uep);
465
466 info = usbhsh_pipe_info(uep->pipe);
467 info->usr_cnt--;
468
469 /* remove this endpoint from udev */
470 list_del_init(&uep->ep_list);
471
472 usbhsh_uep_to_udev(uep) = NULL;
473 usbhsh_ep_to_uep(ep) = NULL;
474
475 kfree(uep);
476}
477
478/*
479 * queue push/pop
480 */
481static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
482{
483 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt);
484 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
485 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
486 struct urb *urb = ureq->urb;
487 struct device *dev = usbhs_priv_to_dev(priv);
488
489 dev_dbg(dev, "%s\n", __func__);
490
491 if (!urb) {
492 dev_warn(dev, "pkt doesn't have urb\n");
493 return;
494 }
495
496 urb->actual_length = pkt->actual;
497 usbhsh_req_free(hpriv, ureq);
498 usbhsh_urb_to_ureq(urb) = NULL;
499
500 usb_hcd_unlink_urb_from_ep(hcd, urb);
501 usb_hcd_giveback_urb(hcd, urb, 0);
502}
503
504static int usbhsh_queue_push(struct usb_hcd *hcd,
505 struct usbhs_pipe *pipe,
506 struct urb *urb)
507{
508 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
509 struct usbhs_pkt *pkt = &ureq->pkt;
510 struct device *dev = usbhsh_hcd_to_dev(hcd);
511 void *buf;
512 int len;
513
514 if (usb_pipeisoc(urb->pipe)) {
515 dev_err(dev, "pipe iso is not supported now\n");
516 return -EIO;
517 }
518
519 if (usb_pipein(urb->pipe))
520 pipe->handler = &usbhs_fifo_pio_pop_handler;
521 else
522 pipe->handler = &usbhs_fifo_pio_push_handler;
523
524 buf = (void *)(urb->transfer_buffer + urb->actual_length);
525 len = urb->transfer_buffer_length - urb->actual_length;
526
527 dev_dbg(dev, "%s\n", __func__);
528 usbhs_pkt_push(pipe, pkt, usbhsh_queue_done,
529 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
530 usbhs_pkt_start(pipe);
531
532 return 0;
533}
534
535/*
536 * DCP setup stage
537 */
538static int usbhsh_is_request_address(struct urb *urb)
539{
540 struct usb_ctrlrequest *cmd;
541
542 cmd = (struct usb_ctrlrequest *)urb->setup_packet;
543
544 if ((DeviceOutRequest == cmd->bRequestType << 8) &&
545 (USB_REQ_SET_ADDRESS == cmd->bRequest))
546 return 1;
547 else
548 return 0;
549}
550
551static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
552 struct urb *urb,
553 struct usbhs_pipe *pipe)
554{
555 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
556 struct usb_ctrlrequest req;
557 struct device *dev = usbhs_priv_to_dev(priv);
558
559 /*
560 * wait setup packet ACK
561 * see
562 * usbhsh_irq_setup_ack()
563 * usbhsh_irq_setup_err()
564 */
565 init_completion(&hpriv->setup_ack_done);
566
567 /* copy original request */
568 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
569
570 /*
571 * renesas_usbhs can not use original usb address.
572 * see HARDWARE LIMITATION.
573 * modify usb address here.
574 */
575 if (usbhsh_is_request_address(urb)) {
576 /* FIXME */
577 req.wValue = 1;
578 dev_dbg(dev, "create new address - %d\n", req.wValue);
579 }
580
581 /* set request */
582 usbhs_usbreq_set_val(priv, &req);
583
584 /*
585 * wait setup packet ACK
586 */
587 wait_for_completion(&hpriv->setup_ack_done);
588
589 dev_dbg(dev, "%s done\n", __func__);
590}
591
592/*
593 * DCP data stage
594 */
595static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
596 struct usbhs_pkt *pkt)
597{
598 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt);
599 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
600 struct urb *urb = ureq->urb;
601
602 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
603
604 usbhsh_req_free(hpriv, ureq);
605 usbhsh_urb_to_ureq(urb) = NULL;
606}
607
608static void usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
609 struct urb *urb,
610 struct usbhs_pipe *pipe)
611{
612 struct usbhsh_request *ureq;
613 struct usbhs_pkt *pkt;
614
615 /*
616 * FIXME
617 *
618 * data stage uses ureq which is connected to urb
619 * see usbhsh_urb_enqueue() :: alloc new request.
620 * it will be freed in usbhsh_data_stage_packet_done()
621 */
622 ureq = usbhsh_urb_to_ureq(urb);
623 pkt = &ureq->pkt;
624
625 if (usb_pipein(urb->pipe))
626 pipe->handler = &usbhs_dcp_data_stage_in_handler;
627 else
628 pipe->handler = &usbhs_dcp_data_stage_out_handler;
629
630 usbhs_pkt_push(pipe, pkt,
631 usbhsh_data_stage_packet_done,
632 urb->transfer_buffer,
633 urb->transfer_buffer_length,
634 (urb->transfer_flags & URB_ZERO_PACKET));
635}
636
637/*
638 * DCP status stage
639 */
640static void usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
641 struct urb *urb,
642 struct usbhs_pipe *pipe)
643{
644 struct usbhsh_request *ureq;
645 struct usbhs_pkt *pkt;
646
647 /*
648 * FIXME
649 *
650 * status stage uses allocated ureq.
651 * it will be freed on usbhsh_queue_done()
652 */
653 ureq = usbhsh_req_alloc(hpriv, urb, GFP_KERNEL);
654 pkt = &ureq->pkt;
655
656 if (usb_pipein(urb->pipe))
657 pipe->handler = &usbhs_dcp_status_stage_in_handler;
658 else
659 pipe->handler = &usbhs_dcp_status_stage_out_handler;
660
661 usbhs_pkt_push(pipe, pkt,
662 usbhsh_queue_done,
663 NULL,
664 urb->transfer_buffer_length,
665 0);
666}
667
668static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
669 struct usbhsh_hpriv *hpriv,
670 struct usbhs_pipe *pipe,
671 struct urb *urb)
672{
673 struct device *dev = usbhsh_hcd_to_dev(hcd);
674
675 dev_dbg(dev, "%s\n", __func__);
676
677 /*
678 * setup stage
679 *
680 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
681 */
682 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
683
684 /*
685 * data stage
686 *
687 * It is pushed only when urb has buffer.
688 */
689 if (urb->transfer_buffer_length)
690 usbhsh_data_stage_packet_push(hpriv, urb, pipe);
691
692 /*
693 * status stage
694 */
695 usbhsh_status_stage_packet_push(hpriv, urb, pipe);
696
697 /*
698 * start pushed packets
699 */
700 usbhs_pkt_start(pipe);
701
702 return 0;
703}
704
705/*
706 * dma map functions
707 */
708static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
709{
710 return 0;
711}
712
713/*
714 * for hc_driver
715 */
716static int usbhsh_host_start(struct usb_hcd *hcd)
717{
718 return 0;
719}
720
721static void usbhsh_host_stop(struct usb_hcd *hcd)
722{
723}
724
725static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
726 struct urb *urb,
727 gfp_t mem_flags)
728{
729 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
730 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
731 struct device *dev = usbhs_priv_to_dev(priv);
732 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
733 struct usb_host_endpoint *ep = urb->ep;
734 struct usbhsh_request *ureq;
735 struct usbhsh_device *udev, *new_udev = NULL;
736 struct usbhs_pipe *pipe;
737 struct usbhsh_ep *uep;
738 int is_dir_in = usb_pipein(urb->pipe);
739
740 int ret;
741
742 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
743
744 ret = usb_hcd_link_urb_to_ep(hcd, urb);
745 if (ret)
746 goto usbhsh_urb_enqueue_error_not_linked;
747
748 /*
749 * get udev
750 */
751 udev = usbhsh_usbv_to_udev(usbv);
752 if (!udev) {
753 new_udev = usbhsh_device_alloc(hpriv, urb);
754 if (!new_udev)
755 goto usbhsh_urb_enqueue_error_not_linked;
756
757 udev = new_udev;
758 }
759
760 /*
761 * get uep
762 */
763 uep = usbhsh_ep_to_uep(ep);
764 if (!uep) {
765 uep = usbhsh_endpoint_alloc(hpriv, udev, ep,
766 is_dir_in, mem_flags);
767 if (!uep)
768 goto usbhsh_urb_enqueue_error_free_device;
769 }
770 pipe = usbhsh_uep_to_pipe(uep);
771
772 /*
773 * alloc new request
774 */
775 ureq = usbhsh_req_alloc(hpriv, urb, mem_flags);
776 if (unlikely(!ureq)) {
777 ret = -ENOMEM;
778 goto usbhsh_urb_enqueue_error_free_endpoint;
779 }
780 usbhsh_urb_to_ureq(urb) = ureq;
781
782 /*
783 * push packet
784 */
785 if (usb_pipecontrol(urb->pipe))
786 usbhsh_dcp_queue_push(hcd, hpriv, pipe, urb);
787 else
788 usbhsh_queue_push(hcd, pipe, urb);
789
790 return 0;
791
792usbhsh_urb_enqueue_error_free_endpoint:
793 usbhsh_endpoint_free(hpriv, ep);
794usbhsh_urb_enqueue_error_free_device:
795 if (new_udev)
796 usbhsh_device_free(hpriv, new_udev);
797usbhsh_urb_enqueue_error_not_linked:
798
799 dev_dbg(dev, "%s error\n", __func__);
800
801 return ret;
802}
803
804static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
805{
806 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
807 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
808
809 if (ureq) {
810 usbhsh_req_free(hpriv, ureq);
811 usbhsh_urb_to_ureq(urb) = NULL;
812 }
813
814 return 0;
815}
816
817static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
818 struct usb_host_endpoint *ep)
819{
820 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
821 struct usbhsh_device *udev;
822 struct usbhsh_hpriv *hpriv;
823
824 /*
825 * this function might be called manytimes by same hcd/ep
826 * in-endpoitn == out-endpoint if ep == dcp.
827 */
828 if (!uep)
829 return;
830
831 udev = usbhsh_uep_to_udev(uep);
832 hpriv = usbhsh_hcd_to_hpriv(hcd);
833
834 usbhsh_endpoint_free(hpriv, ep);
835 ep->hcpriv = NULL;
836
837 /*
838 * if there is no endpoint,
839 * free device
840 */
841 if (!usbhsh_device_has_endpoint(udev))
842 usbhsh_device_free(hpriv, udev);
843}
844
845static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
846{
847 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
848 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
849 struct device *dev = usbhs_priv_to_dev(priv);
850 int roothub_id = 1; /* only 1 root hub */
851
852 /*
853 * does port stat was changed ?
854 * check USB_PORT_STAT_C_xxx << 16
855 */
856 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
857 *buf = (1 << roothub_id);
858 else
859 *buf = 0;
860
861 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
862
863 return !!(*buf);
864}
865
866static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
867 u16 typeReq, u16 wValue,
868 u16 wIndex, char *buf, u16 wLength)
869{
870 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
871 struct device *dev = usbhs_priv_to_dev(priv);
872
873 switch (wValue) {
874 case C_HUB_OVER_CURRENT:
875 case C_HUB_LOCAL_POWER:
876 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
877 return 0;
878 }
879
880 return -EPIPE;
881}
882
883static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
884 u16 typeReq, u16 wValue,
885 u16 wIndex, char *buf, u16 wLength)
886{
887 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
888 struct device *dev = usbhs_priv_to_dev(priv);
889 int enable = (typeReq == SetPortFeature);
890 int speed, i, timeout = 128;
891 int roothub_id = 1; /* only 1 root hub */
892
893 /* common error */
894 if (wIndex > roothub_id || wLength != 0)
895 return -EPIPE;
896
897 /* check wValue */
898 switch (wValue) {
899 case USB_PORT_FEAT_POWER:
900 usbhs_vbus_ctrl(priv, enable);
901 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
902 break;
903
904 case USB_PORT_FEAT_ENABLE:
905 case USB_PORT_FEAT_SUSPEND:
906 case USB_PORT_FEAT_C_ENABLE:
907 case USB_PORT_FEAT_C_SUSPEND:
908 case USB_PORT_FEAT_C_CONNECTION:
909 case USB_PORT_FEAT_C_OVER_CURRENT:
910 case USB_PORT_FEAT_C_RESET:
911 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
912 break;
913
914 case USB_PORT_FEAT_RESET:
915 if (!enable)
916 break;
917
918 usbhsh_port_stat_clear(hpriv,
919 USB_PORT_STAT_HIGH_SPEED |
920 USB_PORT_STAT_LOW_SPEED);
921
922 usbhs_bus_send_reset(priv);
923 msleep(20);
924 usbhs_bus_send_sof_enable(priv);
925
926 for (i = 0; i < timeout ; i++) {
927 switch (usbhs_bus_get_speed(priv)) {
928 case USB_SPEED_LOW:
929 speed = USB_PORT_STAT_LOW_SPEED;
930 goto got_usb_bus_speed;
931 case USB_SPEED_HIGH:
932 speed = USB_PORT_STAT_HIGH_SPEED;
933 goto got_usb_bus_speed;
934 case USB_SPEED_FULL:
935 speed = 0;
936 goto got_usb_bus_speed;
937 }
938
939 msleep(20);
940 }
941 return -EPIPE;
942
943got_usb_bus_speed:
944 usbhsh_port_stat_set(hpriv, speed);
945 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
946
947 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
948 __func__, speed);
949
950 /* status change is not needed */
951 return 0;
952
953 default:
954 return -EPIPE;
955 }
956
957 /* set/clear status */
958 if (enable)
959 usbhsh_port_stat_set(hpriv, (1 << wValue));
960 else
961 usbhsh_port_stat_clear(hpriv, (1 << wValue));
962
963 return 0;
964}
965
966static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
967 u16 typeReq, u16 wValue,
968 u16 wIndex, char *buf, u16 wLength)
969{
970 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
971 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
972 struct device *dev = usbhs_priv_to_dev(priv);
973 int roothub_id = 1; /* only 1 root hub */
974
975 switch (typeReq) {
976 case GetHubStatus:
977 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
978
979 *buf = 0x00;
980 break;
981
982 case GetPortStatus:
983 if (wIndex != roothub_id)
984 return -EPIPE;
985
986 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
987 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
988 break;
989
990 case GetHubDescriptor:
991 desc->bDescriptorType = 0x29;
992 desc->bHubContrCurrent = 0;
993 desc->bNbrPorts = roothub_id;
994 desc->bDescLength = 9;
995 desc->bPwrOn2PwrGood = 0;
996 desc->wHubCharacteristics = cpu_to_le16(0x0011);
997 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
998 desc->u.hs.DeviceRemovable[1] = ~0;
999 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1000 break;
1001 }
1002
1003 return 0;
1004}
1005
1006static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1007 u16 wIndex, char *buf, u16 wLength)
1008{
1009 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1010 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1011 struct device *dev = usbhs_priv_to_dev(priv);
1012 int ret = -EPIPE;
1013
1014 switch (typeReq) {
1015
1016 /* Hub Feature */
1017 case ClearHubFeature:
1018 case SetHubFeature:
1019 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1020 wValue, wIndex, buf, wLength);
1021 break;
1022
1023 /* Port Feature */
1024 case SetPortFeature:
1025 case ClearPortFeature:
1026 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1027 wValue, wIndex, buf, wLength);
1028 break;
1029
1030 /* Get status */
1031 case GetHubStatus:
1032 case GetPortStatus:
1033 case GetHubDescriptor:
1034 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1035 wValue, wIndex, buf, wLength);
1036 break;
1037 }
1038
1039 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1040 typeReq, ret, usbhsh_port_stat_get(hpriv));
1041
1042 return ret;
1043}
1044
1045static struct hc_driver usbhsh_driver = {
1046 .description = usbhsh_hcd_name,
1047 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1048
1049 /*
1050 * generic hardware linkage
1051 */
1052 .flags = HCD_USB2,
1053
1054 .start = usbhsh_host_start,
1055 .stop = usbhsh_host_stop,
1056
1057 /*
1058 * managing i/o requests and associated device resources
1059 */
1060 .urb_enqueue = usbhsh_urb_enqueue,
1061 .urb_dequeue = usbhsh_urb_dequeue,
1062 .endpoint_disable = usbhsh_endpoint_disable,
1063
1064 /*
1065 * root hub
1066 */
1067 .hub_status_data = usbhsh_hub_status_data,
1068 .hub_control = usbhsh_hub_control,
1069};
1070
1071/*
1072 * interrupt functions
1073 */
1074static int usbhsh_irq_attch(struct usbhs_priv *priv,
1075 struct usbhs_irq_state *irq_state)
1076{
1077 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1078 struct device *dev = usbhs_priv_to_dev(priv);
1079
1080 dev_dbg(dev, "device attached\n");
1081
1082 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1083 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1084
1085 return 0;
1086}
1087
1088static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1089 struct usbhs_irq_state *irq_state)
1090{
1091 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1092 struct device *dev = usbhs_priv_to_dev(priv);
1093
1094 dev_dbg(dev, "device detached\n");
1095
1096 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1097 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1098
1099 return 0;
1100}
1101
1102static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1103 struct usbhs_irq_state *irq_state)
1104{
1105 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1106 struct device *dev = usbhs_priv_to_dev(priv);
1107
1108 dev_dbg(dev, "setup packet OK\n");
1109
1110 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1111
1112 return 0;
1113}
1114
1115static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1116 struct usbhs_irq_state *irq_state)
1117{
1118 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1119 struct device *dev = usbhs_priv_to_dev(priv);
1120
1121 dev_dbg(dev, "setup packet Err\n");
1122
1123 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1124
1125 return 0;
1126}
1127
1128/*
1129 * module start/stop
1130 */
1131static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1132{
1133 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1134 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1135 struct usbhs_pipe *pipe;
1136 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1137 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1138 int old_type, dir_in, i;
1139
1140 /* init all pipe */
1141 old_type = USB_ENDPOINT_XFER_CONTROL;
1142 for (i = 0; i < pipe_size; i++) {
1143 pipe_info[i].usr_cnt = 0;
1144
1145 /*
1146 * data "output" will be finished as soon as possible,
1147 * but there is no guaranty at data "input" case.
1148 *
1149 * "input" needs "standby" pipe.
1150 * So, "input" direction pipe > "output" direction pipe
1151 * is good idea.
1152 *
1153 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1154 * and the other will be input direction here.
1155 *
1156 * ex)
1157 * ...
1158 * USB_ENDPOINT_XFER_ISOC -> dir out
1159 * USB_ENDPOINT_XFER_ISOC -> dir in
1160 * USB_ENDPOINT_XFER_BULK -> dir out
1161 * USB_ENDPOINT_XFER_BULK -> dir in
1162 * USB_ENDPOINT_XFER_BULK -> dir in
1163 * ...
1164 */
1165 dir_in = (pipe_type[i] == old_type);
1166 old_type = pipe_type[i];
1167
1168 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1169 pipe = usbhs_dcp_malloc(priv);
1170 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1171 } else {
1172 pipe = usbhs_pipe_malloc(priv,
1173 pipe_type[i],
1174 dir_in);
1175 }
1176
1177 pipe->mod_private = pipe_info + i;
1178 }
1179}
1180
1181static int usbhsh_start(struct usbhs_priv *priv)
1182{
1183 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1184 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1185 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1186 struct device *dev = usbhs_priv_to_dev(priv);
1187 int ret;
1188
1189 /* add hcd */
1190 ret = usb_add_hcd(hcd, 0, 0);
1191 if (ret < 0)
1192 return 0;
1193
1194 /*
1195 * pipe initialize and enable DCP
1196 */
1197 usbhs_pipe_init(priv,
1198 usbhsh_dma_map_ctrl);
1199 usbhs_fifo_init(priv);
1200 usbhsh_pipe_init_for_host(priv);
1201
1202 /*
1203 * system config enble
1204 * - HI speed
1205 * - host
1206 * - usb module
1207 */
1208 usbhs_sys_hispeed_ctrl(priv, 1);
1209 usbhs_sys_host_ctrl(priv, 1);
1210 usbhs_sys_usb_ctrl(priv, 1);
1211
1212 /*
1213 * enable irq callback
1214 */
1215 mod->irq_attch = usbhsh_irq_attch;
1216 mod->irq_dtch = usbhsh_irq_dtch;
1217 mod->irq_sack = usbhsh_irq_setup_ack;
1218 mod->irq_sign = usbhsh_irq_setup_err;
1219 usbhs_irq_callback_update(priv, mod);
1220
1221 dev_dbg(dev, "start host\n");
1222
1223 return ret;
1224}
1225
1226static int usbhsh_stop(struct usbhs_priv *priv)
1227{
1228 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1229 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1230 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1231 struct device *dev = usbhs_priv_to_dev(priv);
1232
1233 /*
1234 * disable irq callback
1235 */
1236 mod->irq_attch = NULL;
1237 mod->irq_dtch = NULL;
1238 mod->irq_sack = NULL;
1239 mod->irq_sign = NULL;
1240 usbhs_irq_callback_update(priv, mod);
1241
1242 usb_remove_hcd(hcd);
1243
1244 /* disable sys */
1245 usbhs_sys_hispeed_ctrl(priv, 0);
1246 usbhs_sys_host_ctrl(priv, 0);
1247 usbhs_sys_usb_ctrl(priv, 0);
1248
1249 dev_dbg(dev, "quit host\n");
1250
1251 return 0;
1252}
1253
1254int usbhs_mod_host_probe(struct usbhs_priv *priv)
1255{
1256 struct usbhsh_hpriv *hpriv;
1257 struct usb_hcd *hcd;
1258 struct usbhsh_pipe_info *pipe_info;
1259 struct usbhsh_device *udev;
1260 struct device *dev = usbhs_priv_to_dev(priv);
1261 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1262 int i;
1263
1264 /* initialize hcd */
1265 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1266 if (!hcd) {
1267 dev_err(dev, "Failed to create hcd\n");
1268 return -ENOMEM;
1269 }
1270
1271 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1272 if (!pipe_info) {
1273 dev_err(dev, "Could not allocate pipe_info\n");
1274 goto usbhs_mod_host_probe_err;
1275 }
1276
1277 /*
1278 * CAUTION
1279 *
1280 * There is no guarantee that it is possible to access usb module here.
1281 * Don't accesses to it.
1282 * The accesse will be enable after "usbhsh_start"
1283 */
1284
1285 hpriv = usbhsh_hcd_to_hpriv(hcd);
1286
1287 /*
1288 * register itself
1289 */
1290 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1291
1292 /* init hpriv */
1293 hpriv->mod.name = "host";
1294 hpriv->mod.start = usbhsh_start;
1295 hpriv->mod.stop = usbhsh_stop;
1296 hpriv->pipe_info = pipe_info;
1297 hpriv->pipe_size = pipe_size;
1298 usbhsh_req_list_init(hpriv);
1299 usbhsh_port_stat_init(hpriv);
1300
1301 /* init all device */
1302 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1303 udev->usbv = NULL;
1304 INIT_LIST_HEAD(&udev->ep_list_head);
1305 }
1306
1307 dev_info(dev, "host probed\n");
1308
1309 return 0;
1310
1311usbhs_mod_host_probe_err:
1312 usb_put_hcd(hcd);
1313
1314 return -ENOMEM;
1315}
1316
1317int usbhs_mod_host_remove(struct usbhs_priv *priv)
1318{
1319 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1320 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1321
1322 usbhsh_req_list_quit(hpriv);
1323
1324 usb_put_hcd(hcd);
1325
1326 return 0;
1327}
diff --git a/drivers/usb/renesas_usbhs/pipe.c b/drivers/usb/renesas_usbhs/pipe.c
index 1b14cae45704..c74389ce2177 100644
--- a/drivers/usb/renesas_usbhs/pipe.c
+++ b/drivers/usb/renesas_usbhs/pipe.c
@@ -29,9 +29,6 @@
29#define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f) 29#define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f)
30#define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0) 30#define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0)
31 31
32#define usbhsp_type(p) ((p)->pipe_type)
33#define usbhsp_type_is(p, t) ((p)->pipe_type == t)
34
35/* 32/*
36 * for debug 33 * for debug
37 */ 34 */
@@ -42,28 +39,9 @@ static char *usbhsp_pipe_name[] = {
42 [USB_ENDPOINT_XFER_ISOC] = "ISO", 39 [USB_ENDPOINT_XFER_ISOC] = "ISO",
43}; 40};
44 41
45/* 42char *usbhs_pipe_name(struct usbhs_pipe *pipe)
46 * usb request functions
47 */
48void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
49{ 43{
50 u16 val; 44 return usbhsp_pipe_name[usbhs_pipe_type(pipe)];
51
52 val = usbhs_read(priv, USBREQ);
53 req->bRequest = (val >> 8) & 0xFF;
54 req->bRequestType = (val >> 0) & 0xFF;
55
56 req->wValue = usbhs_read(priv, USBVAL);
57 req->wIndex = usbhs_read(priv, USBINDX);
58 req->wLength = usbhs_read(priv, USBLENG);
59}
60
61void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
62{
63 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
64 usbhs_write(priv, USBVAL, req->wValue);
65 usbhs_write(priv, USBINDX, req->wIndex);
66 usbhs_write(priv, USBLENG, req->wLength);
67} 45}
68 46
69/* 47/*
@@ -106,17 +84,6 @@ static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
106 usbhs_bset(priv, pipe_reg, mask, val); 84 usbhs_bset(priv, pipe_reg, mask, val);
107} 85}
108 86
109static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
110 u16 dcp_reg, u16 pipe_reg)
111{
112 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
113
114 if (usbhs_pipe_is_dcp(pipe))
115 return usbhs_read(priv, dcp_reg);
116 else
117 return usbhs_read(priv, pipe_reg);
118}
119
120/* 87/*
121 * DCPCFG/PIPECFG functions 88 * DCPCFG/PIPECFG functions
122 */ 89 */
@@ -144,11 +111,6 @@ static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
144 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val); 111 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
145} 112}
146 113
147static u16 usbhsp_pipe_maxp_get(struct usbhs_pipe *pipe)
148{
149 return __usbhsp_pipe_xxx_get(pipe, DCPMAXP, PIPEMAXP);
150}
151
152/* 114/*
153 * pipe control functions 115 * pipe control functions
154 */ 116 */
@@ -303,16 +265,16 @@ static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
303 /* 265 /*
304 * only ISO / BULK pipe can use double buffer 266 * only ISO / BULK pipe can use double buffer
305 */ 267 */
306 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) || 268 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
307 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC)) 269 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
308 return 1; 270 return 1;
309 271
310 return 0; 272 return 0;
311} 273}
312 274
313static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe, 275static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
314 const struct usb_endpoint_descriptor *desc, 276 int is_host,
315 int is_host) 277 int dir_in)
316{ 278{
317 u16 type = 0; 279 u16 type = 0;
318 u16 bfre = 0; 280 u16 bfre = 0;
@@ -341,40 +303,40 @@ static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
341 */ 303 */
342 304
343 /* TYPE */ 305 /* TYPE */
344 type = type_array[usbhsp_type(pipe)]; 306 type = type_array[usbhs_pipe_type(pipe)];
345 307
346 /* BFRE */ 308 /* BFRE */
347 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || 309 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
348 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 310 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
349 bfre = 0; /* FIXME */ 311 bfre = 0; /* FIXME */
350 312
351 /* DBLB */ 313 /* DBLB */
352 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || 314 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
353 usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 315 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
354 dblb = (is_double) ? DBLB : 0; 316 dblb = (is_double) ? DBLB : 0;
355 317
356 /* CNTMD */ 318 /* CNTMD */
357 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 319 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
358 cntmd = 0; /* FIXME */ 320 cntmd = 0; /* FIXME */
359 321
360 /* DIR */ 322 /* DIR */
361 if (usb_endpoint_dir_in(desc)) 323 if (dir_in)
362 usbhsp_flags_set(pipe, IS_DIR_HOST); 324 usbhsp_flags_set(pipe, IS_DIR_HOST);
363 325
364 if ((is_host && usb_endpoint_dir_out(desc)) || 326 if ((is_host && !dir_in) ||
365 (!is_host && usb_endpoint_dir_in(desc))) 327 (!is_host && dir_in))
366 dir |= DIR_OUT; 328 dir |= DIR_OUT;
367 329
368 if (!dir) 330 if (!dir)
369 usbhsp_flags_set(pipe, IS_DIR_IN); 331 usbhsp_flags_set(pipe, IS_DIR_IN);
370 332
371 /* SHTNAK */ 333 /* SHTNAK */
372 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) && 334 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
373 !dir) 335 !dir)
374 shtnak = SHTNAK; 336 shtnak = SHTNAK;
375 337
376 /* EPNUM */ 338 /* EPNUM */
377 epnum = 0xF & usb_endpoint_num(desc); 339 epnum = 0; /* see usbhs_pipe_config_update() */
378 340
379 return type | 341 return type |
380 bfre | 342 bfre |
@@ -385,19 +347,7 @@ static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
385 epnum; 347 epnum;
386} 348}
387 349
388static u16 usbhsp_setup_pipemaxp(struct usbhs_pipe *pipe, 350static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
389 const struct usb_endpoint_descriptor *desc,
390 int is_host)
391{
392 /* host should set DEVSEL */
393
394 /* reutn MXPS */
395 return PIPE_MAXP_MASK & le16_to_cpu(desc->wMaxPacketSize);
396}
397
398static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
399 const struct usb_endpoint_descriptor *desc,
400 int is_host)
401{ 351{
402 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 352 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
403 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 353 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
@@ -441,9 +391,9 @@ static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
441 * INT : 64 byte 391 * INT : 64 byte
442 * ISOC: 512 byte 392 * ISOC: 512 byte
443 */ 393 */
444 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL)) 394 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
445 buff_size = 256; 395 buff_size = 256;
446 else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) 396 else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
447 buff_size = 64; 397 buff_size = 64;
448 else 398 else
449 buff_size = 512; 399 buff_size = 512;
@@ -453,7 +403,7 @@ static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
453 403
454 /* BUFNMB has been reserved for INT pipe 404 /* BUFNMB has been reserved for INT pipe
455 * see above */ 405 * see above */
456 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) { 406 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
457 bufnmb = pipe_num - 2; 407 bufnmb = pipe_num - 2;
458 } else { 408 } else {
459 bufnmb = info->bufnmb_last; 409 bufnmb = info->bufnmb_last;
@@ -473,16 +423,42 @@ static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
473 (0xff & bufnmb) << 0; 423 (0xff & bufnmb) << 0;
474} 424}
475 425
426void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
427 u16 epnum, u16 maxp)
428{
429 if (devsel > 0xA) {
430 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
431 struct device *dev = usbhs_priv_to_dev(priv);
432
433 dev_err(dev, "devsel error %d\n", devsel);
434
435 devsel = 0;
436 }
437
438 usbhsp_pipe_barrier(pipe);
439
440 pipe->maxp = maxp;
441
442 usbhsp_pipe_select(pipe);
443 usbhsp_pipe_maxp_set(pipe, 0xFFFF,
444 (devsel << 12) |
445 maxp);
446
447 if (!usbhs_pipe_is_dcp(pipe))
448 usbhsp_pipe_cfg_set(pipe, 0x000F, epnum);
449}
450
476/* 451/*
477 * pipe control 452 * pipe control
478 */ 453 */
479int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe) 454int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
480{ 455{
481 u16 mask = usbhs_pipe_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK; 456 /*
482 457 * see
483 usbhsp_pipe_select(pipe); 458 * usbhs_pipe_config_update()
484 459 * usbhs_dcp_malloc()
485 return (int)(usbhsp_pipe_maxp_get(pipe) & mask); 460 */
461 return pipe->maxp;
486} 462}
487 463
488int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe) 464int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
@@ -495,9 +471,12 @@ int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
495 return usbhsp_flags_has(pipe, IS_DIR_HOST); 471 return usbhsp_flags_has(pipe, IS_DIR_HOST);
496} 472}
497 473
498void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe) 474void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int data)
499{ 475{
500 usbhsp_pipectrl_set(pipe, SQCLR, SQCLR); 476 u16 mask = (SQCLR | SQSET);
477 u16 val = (data) ? SQSET : SQCLR;
478
479 usbhsp_pipectrl_set(pipe, mask, val);
501} 480}
502 481
503void usbhs_pipe_clear(struct usbhs_pipe *pipe) 482void usbhs_pipe_clear(struct usbhs_pipe *pipe)
@@ -516,7 +495,7 @@ static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
516 */ 495 */
517 pipe = NULL; 496 pipe = NULL;
518 usbhs_for_each_pipe_with_dcp(pos, priv, i) { 497 usbhs_for_each_pipe_with_dcp(pos, priv, i) {
519 if (!usbhsp_type_is(pos, type)) 498 if (!usbhs_pipe_type_is(pos, type))
520 continue; 499 continue;
521 if (usbhsp_flags_has(pos, IS_USED)) 500 if (usbhsp_flags_has(pos, IS_USED))
522 continue; 501 continue;
@@ -538,19 +517,12 @@ static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
538} 517}
539 518
540void usbhs_pipe_init(struct usbhs_priv *priv, 519void usbhs_pipe_init(struct usbhs_priv *priv,
541 void (*done)(struct usbhs_pkt *pkt),
542 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map)) 520 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
543{ 521{
544 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 522 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
545 struct device *dev = usbhs_priv_to_dev(priv);
546 struct usbhs_pipe *pipe; 523 struct usbhs_pipe *pipe;
547 int i; 524 int i;
548 525
549 if (!done) {
550 dev_err(dev, "no done function\n");
551 return;
552 }
553
554 /* 526 /*
555 * FIXME 527 * FIXME
556 * 528 *
@@ -565,7 +537,7 @@ void usbhs_pipe_init(struct usbhs_priv *priv,
565 */ 537 */
566 info->bufnmb_last = 4; 538 info->bufnmb_last = 4;
567 usbhs_for_each_pipe_with_dcp(pipe, priv, i) { 539 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
568 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) 540 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
569 info->bufnmb_last++; 541 info->bufnmb_last++;
570 542
571 usbhsp_flags_init(pipe); 543 usbhsp_flags_init(pipe);
@@ -577,24 +549,23 @@ void usbhs_pipe_init(struct usbhs_priv *priv,
577 usbhs_pipe_clear(pipe); 549 usbhs_pipe_clear(pipe);
578 } 550 }
579 551
580 info->done = done;
581 info->dma_map_ctrl = dma_map_ctrl; 552 info->dma_map_ctrl = dma_map_ctrl;
582} 553}
583 554
584struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv, 555struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
585 const struct usb_endpoint_descriptor *desc) 556 int endpoint_type,
557 int dir_in)
586{ 558{
587 struct device *dev = usbhs_priv_to_dev(priv); 559 struct device *dev = usbhs_priv_to_dev(priv);
588 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
589 struct usbhs_pipe *pipe; 560 struct usbhs_pipe *pipe;
590 int is_host = usbhs_mod_is_host(priv, mod); 561 int is_host = usbhs_mod_is_host(priv);
591 int ret; 562 int ret;
592 u16 pipecfg, pipebuf, pipemaxp; 563 u16 pipecfg, pipebuf;
593 564
594 pipe = usbhsp_get_pipe(priv, usb_endpoint_type(desc)); 565 pipe = usbhsp_get_pipe(priv, endpoint_type);
595 if (!pipe) { 566 if (!pipe) {
596 dev_err(dev, "can't get pipe (%s)\n", 567 dev_err(dev, "can't get pipe (%s)\n",
597 usbhsp_pipe_name[usb_endpoint_type(desc)]); 568 usbhsp_pipe_name[endpoint_type]);
598 return NULL; 569 return NULL;
599 } 570 }
600 571
@@ -609,22 +580,25 @@ struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
609 return NULL; 580 return NULL;
610 } 581 }
611 582
612 pipecfg = usbhsp_setup_pipecfg(pipe, desc, is_host); 583 pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
613 pipebuf = usbhsp_setup_pipebuff(pipe, desc, is_host); 584 pipebuf = usbhsp_setup_pipebuff(pipe);
614 pipemaxp = usbhsp_setup_pipemaxp(pipe, desc, is_host);
615 585
616 usbhsp_pipe_select(pipe); 586 usbhsp_pipe_select(pipe);
617 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg); 587 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
618 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf); 588 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
619 usbhsp_pipe_maxp_set(pipe, 0xFFFF, pipemaxp);
620 589
621 usbhs_pipe_clear_sequence(pipe); 590 usbhs_pipe_sequence_data0(pipe);
622 591
623 dev_dbg(dev, "enable pipe %d : %s (%s)\n", 592 dev_dbg(dev, "enable pipe %d : %s (%s)\n",
624 usbhs_pipe_number(pipe), 593 usbhs_pipe_number(pipe),
625 usbhsp_pipe_name[usb_endpoint_type(desc)], 594 usbhs_pipe_name(pipe),
626 usbhs_pipe_is_dir_in(pipe) ? "in" : "out"); 595 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
627 596
597 /*
598 * epnum / maxp are still not set to this pipe.
599 * call usbhs_pipe_config_update() after this function !!
600 */
601
628 return pipe; 602 return pipe;
629} 603}
630 604
@@ -651,25 +625,31 @@ struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
651 if (!pipe) 625 if (!pipe)
652 return NULL; 626 return NULL;
653 627
628 INIT_LIST_HEAD(&pipe->list);
629
654 /* 630 /*
655 * dcpcfg : default 631 * call usbhs_pipe_config_update() after this function !!
656 * dcpmaxp : default
657 * pipebuf : nothing to do
658 */ 632 */
659 633
660 usbhsp_pipe_select(pipe);
661 usbhs_pipe_clear_sequence(pipe);
662 INIT_LIST_HEAD(&pipe->list);
663
664 return pipe; 634 return pipe;
665} 635}
666 636
667void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe) 637void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
668{ 638{
639 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
640
669 WARN_ON(!usbhs_pipe_is_dcp(pipe)); 641 WARN_ON(!usbhs_pipe_is_dcp(pipe));
670 642
671 usbhs_pipe_enable(pipe); 643 usbhs_pipe_enable(pipe);
672 usbhsp_pipectrl_set(pipe, CCPL, CCPL); 644
645 if (!usbhs_mod_is_host(priv)) /* funconly */
646 usbhsp_pipectrl_set(pipe, CCPL, CCPL);
647}
648
649void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out)
650{
651 usbhsp_pipe_cfg_set(pipe, DIR_OUT,
652 dir_out ? DIR_OUT : 0);
673} 653}
674 654
675/* 655/*
@@ -703,7 +683,9 @@ int usbhs_pipe_probe(struct usbhs_priv *priv)
703 */ 683 */
704 usbhs_for_each_pipe_with_dcp(pipe, priv, i) { 684 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
705 pipe->priv = priv; 685 pipe->priv = priv;
706 usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK; 686
687 usbhs_pipe_type(pipe) =
688 pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
707 689
708 dev_dbg(dev, "pipe %x\t: %s\n", 690 dev_dbg(dev, "pipe %x\t: %s\n",
709 i, usbhsp_pipe_name[pipe_type[i]]); 691 i, usbhsp_pipe_name[pipe_type[i]]);
diff --git a/drivers/usb/renesas_usbhs/pipe.h b/drivers/usb/renesas_usbhs/pipe.h
index 41534cb0e734..6334fc644cc0 100644
--- a/drivers/usb/renesas_usbhs/pipe.h
+++ b/drivers/usb/renesas_usbhs/pipe.h
@@ -30,11 +30,15 @@ struct usbhs_pipe {
30 struct usbhs_fifo *fifo; 30 struct usbhs_fifo *fifo;
31 struct list_head list; 31 struct list_head list;
32 32
33 int maxp;
34
33 u32 flags; 35 u32 flags;
34#define USBHS_PIPE_FLAGS_IS_USED (1 << 0) 36#define USBHS_PIPE_FLAGS_IS_USED (1 << 0)
35#define USBHS_PIPE_FLAGS_IS_DIR_IN (1 << 1) 37#define USBHS_PIPE_FLAGS_IS_DIR_IN (1 << 1)
36#define USBHS_PIPE_FLAGS_IS_DIR_HOST (1 << 2) 38#define USBHS_PIPE_FLAGS_IS_DIR_HOST (1 << 2)
37 39
40 struct usbhs_pkt_handle *handler;
41
38 void *mod_private; 42 void *mod_private;
39}; 43};
40 44
@@ -43,7 +47,6 @@ struct usbhs_pipe_info {
43 int size; /* array size of "pipe" */ 47 int size; /* array size of "pipe" */
44 int bufnmb_last; /* FIXME : driver needs good allocator */ 48 int bufnmb_last; /* FIXME : driver needs good allocator */
45 49
46 void (*done)(struct usbhs_pkt *pkt);
47 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map); 50 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map);
48}; 51};
49 52
@@ -67,32 +70,30 @@ struct usbhs_pipe_info {
67#define usbhs_priv_to_pipeinfo(pr) (&(pr)->pipe_info) 70#define usbhs_priv_to_pipeinfo(pr) (&(pr)->pipe_info)
68 71
69/* 72/*
70 * usb request
71 */
72void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req);
73void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req);
74
75/*
76 * pipe control 73 * pipe control
77 */ 74 */
75char *usbhs_pipe_name(struct usbhs_pipe *pipe);
78struct usbhs_pipe 76struct usbhs_pipe
79*usbhs_pipe_malloc(struct usbhs_priv *priv, 77*usbhs_pipe_malloc(struct usbhs_priv *priv, int endpoint_type, int dir_in);
80 const struct usb_endpoint_descriptor *desc);
81int usbhs_pipe_probe(struct usbhs_priv *priv); 78int usbhs_pipe_probe(struct usbhs_priv *priv);
82void usbhs_pipe_remove(struct usbhs_priv *priv); 79void usbhs_pipe_remove(struct usbhs_priv *priv);
83int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe); 80int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe);
84int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe); 81int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe);
85void usbhs_pipe_init(struct usbhs_priv *priv, 82void usbhs_pipe_init(struct usbhs_priv *priv,
86 void (*done)(struct usbhs_pkt *pkt),
87 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map)); 83 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map));
88int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe); 84int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe);
89void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe);
90void usbhs_pipe_clear(struct usbhs_pipe *pipe); 85void usbhs_pipe_clear(struct usbhs_pipe *pipe);
91int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe); 86int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe);
92void usbhs_pipe_enable(struct usbhs_pipe *pipe); 87void usbhs_pipe_enable(struct usbhs_pipe *pipe);
93void usbhs_pipe_disable(struct usbhs_pipe *pipe); 88void usbhs_pipe_disable(struct usbhs_pipe *pipe);
94void usbhs_pipe_stall(struct usbhs_pipe *pipe); 89void usbhs_pipe_stall(struct usbhs_pipe *pipe);
95void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo); 90void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo);
91void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
92 u16 epnum, u16 maxp);
93
94#define usbhs_pipe_sequence_data0(pipe) usbhs_pipe_data_sequence(pipe, 0)
95#define usbhs_pipe_sequence_data1(pipe) usbhs_pipe_data_sequence(pipe, 1)
96void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int data);
96 97
97#define usbhs_pipe_to_priv(p) ((p)->priv) 98#define usbhs_pipe_to_priv(p) ((p)->priv)
98#define usbhs_pipe_number(p) (int)((p) - (p)->priv->pipe_info.pipe) 99#define usbhs_pipe_number(p) (int)((p) - (p)->priv->pipe_info.pipe)
@@ -100,10 +101,14 @@ void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo);
100#define usbhs_pipe_to_fifo(p) ((p)->fifo) 101#define usbhs_pipe_to_fifo(p) ((p)->fifo)
101#define usbhs_pipe_is_busy(p) usbhs_pipe_to_fifo(p) 102#define usbhs_pipe_is_busy(p) usbhs_pipe_to_fifo(p)
102 103
104#define usbhs_pipe_type(p) ((p)->pipe_type)
105#define usbhs_pipe_type_is(p, t) ((p)->pipe_type == t)
106
103/* 107/*
104 * dcp control 108 * dcp control
105 */ 109 */
106struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv); 110struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv);
107void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe); 111void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe);
112void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out);
108 113
109#endif /* RENESAS_USB_PIPE_H */ 114#endif /* RENESAS_USB_PIPE_H */
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index b71e309116a3..677f577c0243 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -252,6 +252,7 @@ config USB_SERIAL_GARMIN
252 252
253config USB_SERIAL_IPW 253config USB_SERIAL_IPW
254 tristate "USB IPWireless (3G UMTS TDD) Driver" 254 tristate "USB IPWireless (3G UMTS TDD) Driver"
255 select USB_SERIAL_WWAN
255 help 256 help
256 Say Y here if you want to use a IPWireless USB modem such as 257 Say Y here if you want to use a IPWireless USB modem such as
257 the ones supplied by Axity3G/Sentech South Africa. 258 the ones supplied by Axity3G/Sentech South Africa.
diff --git a/drivers/usb/serial/aircable.c b/drivers/usb/serial/aircable.c
index aba201cb872c..b43d07df4c44 100644
--- a/drivers/usb/serial/aircable.c
+++ b/drivers/usb/serial/aircable.c
@@ -47,6 +47,7 @@
47#include <asm/unaligned.h> 47#include <asm/unaligned.h>
48#include <linux/tty.h> 48#include <linux/tty.h>
49#include <linux/slab.h> 49#include <linux/slab.h>
50#include <linux/module.h>
50#include <linux/tty_flip.h> 51#include <linux/tty_flip.h>
51#include <linux/usb.h> 52#include <linux/usb.h>
52#include <linux/usb/serial.h> 53#include <linux/usb/serial.h>
diff --git a/drivers/usb/serial/ark3116.c b/drivers/usb/serial/ark3116.c
index 5cdb9d912275..18e875b92e00 100644
--- a/drivers/usb/serial/ark3116.c
+++ b/drivers/usb/serial/ark3116.c
@@ -42,7 +42,7 @@ static int debug;
42 * Version information 42 * Version information
43 */ 43 */
44 44
45#define DRIVER_VERSION "v0.6" 45#define DRIVER_VERSION "v0.7"
46#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>" 46#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
47#define DRIVER_DESC "USB ARK3116 serial/IrDA driver" 47#define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
48#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA" 48#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
@@ -380,10 +380,6 @@ static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
380 goto err_out; 380 goto err_out;
381 } 381 }
382 382
383 /* setup termios */
384 if (tty)
385 ark3116_set_termios(tty, port, NULL);
386
387 /* remove any data still left: also clears error state */ 383 /* remove any data still left: also clears error state */
388 ark3116_read_reg(serial, UART_RX, buf); 384 ark3116_read_reg(serial, UART_RX, buf);
389 385
@@ -406,6 +402,10 @@ static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
406 /* enable DMA */ 402 /* enable DMA */
407 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT); 403 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
408 404
405 /* setup termios */
406 if (tty)
407 ark3116_set_termios(tty, port, NULL);
408
409err_out: 409err_out:
410 kfree(buf); 410 kfree(buf);
411 return result; 411 return result;
diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c
index 86fbba6336c9..e92cbefc0f88 100644
--- a/drivers/usb/serial/digi_acceleport.c
+++ b/drivers/usb/serial/digi_acceleport.c
@@ -227,7 +227,7 @@
227* - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to 227* - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
228* recheck the condition they are sleeping on. This is defensive, 228* recheck the condition they are sleeping on. This is defensive,
229* in case a wake up is lost. 229* in case a wake up is lost.
230* - Following Documentation/DocBook/kernel-locking.pdf no spin locks 230* - Following Documentation/DocBook/kernel-locking.tmpl no spin locks
231* are held when calling copy_to/from_user or printk. 231* are held when calling copy_to/from_user or printk.
232*/ 232*/
233 233
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 5fc13e717911..bd4298bb6750 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -73,6 +73,7 @@ struct ftdi_private {
73 */ 73 */
74 int flags; /* some ASYNC_xxxx flags are supported */ 74 int flags; /* some ASYNC_xxxx flags are supported */
75 unsigned long last_dtr_rts; /* saved modem control outputs */ 75 unsigned long last_dtr_rts; /* saved modem control outputs */
76 struct async_icount icount;
76 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ 77 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
77 char prev_status, diff_status; /* Used for TIOCMIWAIT */ 78 char prev_status, diff_status; /* Used for TIOCMIWAIT */
78 char transmit_empty; /* If transmitter is empty or not */ 79 char transmit_empty; /* If transmitter is empty or not */
@@ -207,6 +208,8 @@ static struct usb_device_id id_table_combined [] = {
207 { USB_DEVICE(FTDI_VID, FTDI_XF_640_PID) }, 208 { USB_DEVICE(FTDI_VID, FTDI_XF_640_PID) },
208 { USB_DEVICE(FTDI_VID, FTDI_XF_642_PID) }, 209 { USB_DEVICE(FTDI_VID, FTDI_XF_642_PID) },
209 { USB_DEVICE(FTDI_VID, FTDI_DSS20_PID) }, 210 { USB_DEVICE(FTDI_VID, FTDI_DSS20_PID) },
211 { USB_DEVICE(FTDI_VID, FTDI_URBAN_0_PID) },
212 { USB_DEVICE(FTDI_VID, FTDI_URBAN_1_PID) },
210 { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) }, 213 { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },
211 { USB_DEVICE(FTDI_VID, FTDI_VNHCPCUSB_D_PID) }, 214 { USB_DEVICE(FTDI_VID, FTDI_VNHCPCUSB_D_PID) },
212 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_0_PID) }, 215 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_0_PID) },
@@ -745,6 +748,8 @@ static struct usb_device_id id_table_combined [] = {
745 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 748 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
746 { USB_DEVICE(FTDI_VID, LMI_LM3S_EVAL_BOARD_PID), 749 { USB_DEVICE(FTDI_VID, LMI_LM3S_EVAL_BOARD_PID),
747 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 750 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
751 { USB_DEVICE(FTDI_VID, LMI_LM3S_ICDI_BOARD_PID),
752 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
748 { USB_DEVICE(FTDI_VID, FTDI_TURTELIZER_PID), 753 { USB_DEVICE(FTDI_VID, FTDI_TURTELIZER_PID),
749 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 754 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
750 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID_USB60F) }, 755 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID_USB60F) },
@@ -885,6 +890,8 @@ static void ftdi_set_termios(struct tty_struct *tty,
885static int ftdi_tiocmget(struct tty_struct *tty); 890static int ftdi_tiocmget(struct tty_struct *tty);
886static int ftdi_tiocmset(struct tty_struct *tty, 891static int ftdi_tiocmset(struct tty_struct *tty,
887 unsigned int set, unsigned int clear); 892 unsigned int set, unsigned int clear);
893static int ftdi_get_icount(struct tty_struct *tty,
894 struct serial_icounter_struct *icount);
888static int ftdi_ioctl(struct tty_struct *tty, 895static int ftdi_ioctl(struct tty_struct *tty,
889 unsigned int cmd, unsigned long arg); 896 unsigned int cmd, unsigned long arg);
890static void ftdi_break_ctl(struct tty_struct *tty, int break_state); 897static void ftdi_break_ctl(struct tty_struct *tty, int break_state);
@@ -919,6 +926,7 @@ static struct usb_serial_driver ftdi_sio_device = {
919 .prepare_write_buffer = ftdi_prepare_write_buffer, 926 .prepare_write_buffer = ftdi_prepare_write_buffer,
920 .tiocmget = ftdi_tiocmget, 927 .tiocmget = ftdi_tiocmget,
921 .tiocmset = ftdi_tiocmset, 928 .tiocmset = ftdi_tiocmset,
929 .get_icount = ftdi_get_icount,
922 .ioctl = ftdi_ioctl, 930 .ioctl = ftdi_ioctl,
923 .set_termios = ftdi_set_termios, 931 .set_termios = ftdi_set_termios,
924 .break_ctl = ftdi_break_ctl, 932 .break_ctl = ftdi_break_ctl,
@@ -1486,7 +1494,7 @@ static void ftdi_set_max_packet_size(struct usb_serial_port *port)
1486 } 1494 }
1487 1495
1488 /* set max packet size based on descriptor */ 1496 /* set max packet size based on descriptor */
1489 priv->max_packet_size = le16_to_cpu(ep_desc->wMaxPacketSize); 1497 priv->max_packet_size = usb_endpoint_maxp(ep_desc);
1490 1498
1491 dev_info(&udev->dev, "Setting MaxPacketSize %d\n", priv->max_packet_size); 1499 dev_info(&udev->dev, "Setting MaxPacketSize %d\n", priv->max_packet_size);
1492} 1500}
@@ -1646,6 +1654,7 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
1646 1654
1647 kref_init(&priv->kref); 1655 kref_init(&priv->kref);
1648 mutex_init(&priv->cfg_lock); 1656 mutex_init(&priv->cfg_lock);
1657 memset(&priv->icount, 0x00, sizeof(priv->icount));
1649 init_waitqueue_head(&priv->delta_msr_wait); 1658 init_waitqueue_head(&priv->delta_msr_wait);
1650 1659
1651 priv->flags = ASYNC_LOW_LATENCY; 1660 priv->flags = ASYNC_LOW_LATENCY;
@@ -1909,6 +1918,7 @@ static int ftdi_prepare_write_buffer(struct usb_serial_port *port,
1909 c = kfifo_out(&port->write_fifo, &buffer[i + 1], len); 1918 c = kfifo_out(&port->write_fifo, &buffer[i + 1], len);
1910 if (!c) 1919 if (!c)
1911 break; 1920 break;
1921 priv->icount.tx += c;
1912 buffer[i] = (c << 2) + 1; 1922 buffer[i] = (c << 2) + 1;
1913 count += c + 1; 1923 count += c + 1;
1914 } 1924 }
@@ -1916,6 +1926,7 @@ static int ftdi_prepare_write_buffer(struct usb_serial_port *port,
1916 } else { 1926 } else {
1917 count = kfifo_out_locked(&port->write_fifo, dest, size, 1927 count = kfifo_out_locked(&port->write_fifo, dest, size,
1918 &port->lock); 1928 &port->lock);
1929 priv->icount.tx += count;
1919 } 1930 }
1920 1931
1921 return count; 1932 return count;
@@ -1943,6 +1954,14 @@ static int ftdi_process_packet(struct tty_struct *tty,
1943 N.B. packet may be processed more than once, but differences 1954 N.B. packet may be processed more than once, but differences
1944 are only processed once. */ 1955 are only processed once. */
1945 status = packet[0] & FTDI_STATUS_B0_MASK; 1956 status = packet[0] & FTDI_STATUS_B0_MASK;
1957 if (status & FTDI_RS0_CTS)
1958 priv->icount.cts++;
1959 if (status & FTDI_RS0_DSR)
1960 priv->icount.dsr++;
1961 if (status & FTDI_RS0_RI)
1962 priv->icount.rng++;
1963 if (status & FTDI_RS0_RLSD)
1964 priv->icount.dcd++;
1946 if (status != priv->prev_status) { 1965 if (status != priv->prev_status) {
1947 priv->diff_status |= status ^ priv->prev_status; 1966 priv->diff_status |= status ^ priv->prev_status;
1948 wake_up_interruptible(&priv->delta_msr_wait); 1967 wake_up_interruptible(&priv->delta_msr_wait);
@@ -1955,15 +1974,20 @@ static int ftdi_process_packet(struct tty_struct *tty,
1955 * over framing errors */ 1974 * over framing errors */
1956 if (packet[1] & FTDI_RS_BI) { 1975 if (packet[1] & FTDI_RS_BI) {
1957 flag = TTY_BREAK; 1976 flag = TTY_BREAK;
1977 priv->icount.brk++;
1958 usb_serial_handle_break(port); 1978 usb_serial_handle_break(port);
1959 } else if (packet[1] & FTDI_RS_PE) { 1979 } else if (packet[1] & FTDI_RS_PE) {
1960 flag = TTY_PARITY; 1980 flag = TTY_PARITY;
1981 priv->icount.parity++;
1961 } else if (packet[1] & FTDI_RS_FE) { 1982 } else if (packet[1] & FTDI_RS_FE) {
1962 flag = TTY_FRAME; 1983 flag = TTY_FRAME;
1984 priv->icount.frame++;
1963 } 1985 }
1964 /* Overrun is special, not associated with a char */ 1986 /* Overrun is special, not associated with a char */
1965 if (packet[1] & FTDI_RS_OE) 1987 if (packet[1] & FTDI_RS_OE) {
1988 priv->icount.overrun++;
1966 tty_insert_flip_char(tty, 0, TTY_OVERRUN); 1989 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1990 }
1967 } 1991 }
1968 1992
1969 /* save if the transmitter is empty or not */ 1993 /* save if the transmitter is empty or not */
@@ -1975,6 +1999,7 @@ static int ftdi_process_packet(struct tty_struct *tty,
1975 len -= 2; 1999 len -= 2;
1976 if (!len) 2000 if (!len)
1977 return 0; /* status only */ 2001 return 0; /* status only */
2002 priv->icount.rx += len;
1978 ch = packet + 2; 2003 ch = packet + 2;
1979 2004
1980 if (port->port.console && port->sysrq) { 2005 if (port->port.console && port->sysrq) {
@@ -2079,13 +2104,19 @@ static void ftdi_set_termios(struct tty_struct *tty,
2079 2104
2080 cflag = termios->c_cflag; 2105 cflag = termios->c_cflag;
2081 2106
2082 /* FIXME -For this cut I don't care if the line is really changing or 2107 if (old_termios->c_cflag == termios->c_cflag
2083 not - so just do the change regardless - should be able to 2108 && old_termios->c_ispeed == termios->c_ispeed
2084 compare old_termios and tty->termios */ 2109 && old_termios->c_ospeed == termios->c_ospeed)
2110 goto no_c_cflag_changes;
2111
2085 /* NOTE These routines can get interrupted by 2112 /* NOTE These routines can get interrupted by
2086 ftdi_sio_read_bulk_callback - need to examine what this means - 2113 ftdi_sio_read_bulk_callback - need to examine what this means -
2087 don't see any problems yet */ 2114 don't see any problems yet */
2088 2115
2116 if ((old_termios->c_cflag & (CSIZE|PARODD|PARENB|CMSPAR|CSTOPB)) ==
2117 (termios->c_cflag & (CSIZE|PARODD|PARENB|CMSPAR|CSTOPB)))
2118 goto no_data_parity_stop_changes;
2119
2089 /* Set number of data bits, parity, stop bits */ 2120 /* Set number of data bits, parity, stop bits */
2090 2121
2091 urb_value = 0; 2122 urb_value = 0;
@@ -2126,6 +2157,7 @@ static void ftdi_set_termios(struct tty_struct *tty,
2126 } 2157 }
2127 2158
2128 /* Now do the baudrate */ 2159 /* Now do the baudrate */
2160no_data_parity_stop_changes:
2129 if ((cflag & CBAUD) == B0) { 2161 if ((cflag & CBAUD) == B0) {
2130 /* Disable flow control */ 2162 /* Disable flow control */
2131 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 2163 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
@@ -2153,6 +2185,7 @@ static void ftdi_set_termios(struct tty_struct *tty,
2153 2185
2154 /* Set flow control */ 2186 /* Set flow control */
2155 /* Note device also supports DTR/CD (ugh) and Xon/Xoff in hardware */ 2187 /* Note device also supports DTR/CD (ugh) and Xon/Xoff in hardware */
2188no_c_cflag_changes:
2156 if (cflag & CRTSCTS) { 2189 if (cflag & CRTSCTS) {
2157 dbg("%s Setting to CRTSCTS flow control", __func__); 2190 dbg("%s Setting to CRTSCTS flow control", __func__);
2158 if (usb_control_msg(dev, 2191 if (usb_control_msg(dev,
@@ -2277,11 +2310,34 @@ static int ftdi_tiocmset(struct tty_struct *tty,
2277 return update_mctrl(port, set, clear); 2310 return update_mctrl(port, set, clear);
2278} 2311}
2279 2312
2313static int ftdi_get_icount(struct tty_struct *tty,
2314 struct serial_icounter_struct *icount)
2315{
2316 struct usb_serial_port *port = tty->driver_data;
2317 struct ftdi_private *priv = usb_get_serial_port_data(port);
2318 struct async_icount *ic = &priv->icount;
2319
2320 icount->cts = ic->cts;
2321 icount->dsr = ic->dsr;
2322 icount->rng = ic->rng;
2323 icount->dcd = ic->dcd;
2324 icount->tx = ic->tx;
2325 icount->rx = ic->rx;
2326 icount->frame = ic->frame;
2327 icount->parity = ic->parity;
2328 icount->overrun = ic->overrun;
2329 icount->brk = ic->brk;
2330 icount->buf_overrun = ic->buf_overrun;
2331 return 0;
2332}
2333
2280static int ftdi_ioctl(struct tty_struct *tty, 2334static int ftdi_ioctl(struct tty_struct *tty,
2281 unsigned int cmd, unsigned long arg) 2335 unsigned int cmd, unsigned long arg)
2282{ 2336{
2283 struct usb_serial_port *port = tty->driver_data; 2337 struct usb_serial_port *port = tty->driver_data;
2284 struct ftdi_private *priv = usb_get_serial_port_data(port); 2338 struct ftdi_private *priv = usb_get_serial_port_data(port);
2339 struct async_icount cnow;
2340 struct async_icount cprev;
2285 2341
2286 dbg("%s cmd 0x%04x", __func__, cmd); 2342 dbg("%s cmd 0x%04x", __func__, cmd);
2287 2343
@@ -2301,41 +2357,30 @@ static int ftdi_ioctl(struct tty_struct *tty,
2301 * - mask passed in arg for lines of interest 2357 * - mask passed in arg for lines of interest
2302 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 2358 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2303 * Caller should use TIOCGICOUNT to see which one it was. 2359 * Caller should use TIOCGICOUNT to see which one it was.
2304 * (except that the driver doesn't support it !)
2305 * 2360 *
2306 * This code is borrowed from linux/drivers/char/serial.c 2361 * This code is borrowed from linux/drivers/char/serial.c
2307 */ 2362 */
2308 case TIOCMIWAIT: 2363 case TIOCMIWAIT:
2309 while (priv != NULL) { 2364 cprev = priv->icount;
2365 while (1) {
2310 interruptible_sleep_on(&priv->delta_msr_wait); 2366 interruptible_sleep_on(&priv->delta_msr_wait);
2311 /* see if a signal did it */ 2367 /* see if a signal did it */
2312 if (signal_pending(current)) 2368 if (signal_pending(current))
2313 return -ERESTARTSYS; 2369 return -ERESTARTSYS;
2314 else { 2370 cnow = priv->icount;
2315 char diff = priv->diff_status; 2371 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2316 2372 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2317 if (diff == 0) 2373 return -EIO; /* no change => error */
2318 return -EIO; /* no change => error */ 2374 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2319 2375 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2320 /* Consume all events */ 2376 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2321 priv->diff_status = 0; 2377 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2322 2378 return 0;
2323 /* Return 0 if caller wanted to know about
2324 these bits */
2325 if (((arg & TIOCM_RNG) && (diff & FTDI_RS0_RI)) ||
2326 ((arg & TIOCM_DSR) && (diff & FTDI_RS0_DSR)) ||
2327 ((arg & TIOCM_CD) && (diff & FTDI_RS0_RLSD)) ||
2328 ((arg & TIOCM_CTS) && (diff & FTDI_RS0_CTS))) {
2329 return 0;
2330 }
2331 /*
2332 * Otherwise caller can't care less about what
2333 * happened,and so we continue to wait for more
2334 * events.
2335 */
2336 } 2379 }
2380 cprev = cnow;
2337 } 2381 }
2338 return 0; 2382 /* not reached */
2383 break;
2339 case TIOCSERGETLSR: 2384 case TIOCSERGETLSR:
2340 return get_lsr_info(port, (struct serial_struct __user *)arg); 2385 return get_lsr_info(port, (struct serial_struct __user *)arg);
2341 break; 2386 break;
diff --git a/drivers/usb/serial/ftdi_sio_ids.h b/drivers/usb/serial/ftdi_sio_ids.h
index bf5227ad3ef7..571fa96b49c7 100644
--- a/drivers/usb/serial/ftdi_sio_ids.h
+++ b/drivers/usb/serial/ftdi_sio_ids.h
@@ -54,6 +54,7 @@
54/* FTDI 2332C Dual channel device, side A=245 FIFO (JTAG), Side B=RS232 UART */ 54/* FTDI 2332C Dual channel device, side A=245 FIFO (JTAG), Side B=RS232 UART */
55#define LMI_LM3S_DEVEL_BOARD_PID 0xbcd8 55#define LMI_LM3S_DEVEL_BOARD_PID 0xbcd8
56#define LMI_LM3S_EVAL_BOARD_PID 0xbcd9 56#define LMI_LM3S_EVAL_BOARD_PID 0xbcd9
57#define LMI_LM3S_ICDI_BOARD_PID 0xbcda
57 58
58#define FTDI_TURTELIZER_PID 0xBDC8 /* JTAG/RS-232 adapter by egnite GmbH */ 59#define FTDI_TURTELIZER_PID 0xBDC8 /* JTAG/RS-232 adapter by egnite GmbH */
59 60
@@ -420,9 +421,11 @@
420#define PROTEGO_SPECIAL_4 0xFC73 /* special/unknown device */ 421#define PROTEGO_SPECIAL_4 0xFC73 /* special/unknown device */
421 422
422/* 423/*
423 * DSS-20 Sync Station for Sony Ericsson P800 424 * Sony Ericsson product ids
424 */ 425 */
425#define FTDI_DSS20_PID 0xFC82 426#define FTDI_DSS20_PID 0xFC82 /* DSS-20 Sync Station for Sony Ericsson P800 */
427#define FTDI_URBAN_0_PID 0xFC8A /* Sony Ericsson Urban, uart #0 */
428#define FTDI_URBAN_1_PID 0xFC8B /* Sony Ericsson Urban, uart #1 */
426 429
427/* www.irtrans.de device */ 430/* www.irtrans.de device */
428#define FTDI_IRTRANS_PID 0xFC60 /* Product Id */ 431#define FTDI_IRTRANS_PID 0xFC60 /* Product Id */
diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c
index abf095be5753..2ee807523f53 100644
--- a/drivers/usb/serial/io_edgeport.c
+++ b/drivers/usb/serial/io_edgeport.c
@@ -3042,7 +3042,7 @@ static int edge_startup(struct usb_serial *serial)
3042 3042
3043 endpoint = &serial->interface->altsetting[0]. 3043 endpoint = &serial->interface->altsetting[0].
3044 endpoint[i].desc; 3044 endpoint[i].desc;
3045 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 3045 buffer_size = usb_endpoint_maxp(endpoint);
3046 if (!interrupt_in_found && 3046 if (!interrupt_in_found &&
3047 (usb_endpoint_is_int_in(endpoint))) { 3047 (usb_endpoint_is_int_in(endpoint))) {
3048 /* we found a interrupt in endpoint */ 3048 /* we found a interrupt in endpoint */
@@ -3107,7 +3107,7 @@ static int edge_startup(struct usb_serial *serial)
3107 usb_rcvbulkpipe(dev, 3107 usb_rcvbulkpipe(dev,
3108 endpoint->bEndpointAddress), 3108 endpoint->bEndpointAddress),
3109 edge_serial->bulk_in_buffer, 3109 edge_serial->bulk_in_buffer,
3110 le16_to_cpu(endpoint->wMaxPacketSize), 3110 usb_endpoint_maxp(endpoint),
3111 edge_bulk_in_callback, 3111 edge_bulk_in_callback,
3112 edge_serial); 3112 edge_serial);
3113 bulk_in_found = true; 3113 bulk_in_found = true;
diff --git a/drivers/usb/serial/ipw.c b/drivers/usb/serial/ipw.c
index ca77e88836bd..5170799d6e94 100644
--- a/drivers/usb/serial/ipw.c
+++ b/drivers/usb/serial/ipw.c
@@ -47,6 +47,7 @@
47#include <linux/usb.h> 47#include <linux/usb.h>
48#include <linux/usb/serial.h> 48#include <linux/usb/serial.h>
49#include <linux/uaccess.h> 49#include <linux/uaccess.h>
50#include "usb-wwan.h"
50 51
51/* 52/*
52 * Version Information 53 * Version Information
@@ -185,7 +186,7 @@ static int ipw_open(struct tty_struct *tty, struct usb_serial_port *port)
185 186
186 /*--2: Start reading from the device */ 187 /*--2: Start reading from the device */
187 dbg("%s: setting up bulk read callback", __func__); 188 dbg("%s: setting up bulk read callback", __func__);
188 usb_serial_generic_open(tty, port); 189 usb_wwan_open(tty, port);
189 190
190 /*--3: Tell the modem to open the floodgates on the rx bulk channel */ 191 /*--3: Tell the modem to open the floodgates on the rx bulk channel */
191 dbg("%s:asking modem for RxRead (RXBULK_ON)", __func__); 192 dbg("%s:asking modem for RxRead (RXBULK_ON)", __func__);
@@ -219,6 +220,29 @@ static int ipw_open(struct tty_struct *tty, struct usb_serial_port *port)
219 return 0; 220 return 0;
220} 221}
221 222
223/* fake probe - only to allocate data structures */
224static int ipw_probe(struct usb_serial *serial, const struct usb_device_id *id)
225{
226 struct usb_wwan_intf_private *data;
227
228 data = kzalloc(sizeof(struct usb_wwan_intf_private), GFP_KERNEL);
229 if (!data)
230 return -ENOMEM;
231
232 spin_lock_init(&data->susp_lock);
233 usb_set_serial_data(serial, data);
234 return 0;
235}
236
237static void ipw_release(struct usb_serial *serial)
238{
239 struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
240
241 usb_wwan_release(serial);
242 usb_set_serial_data(serial, NULL);
243 kfree(data);
244}
245
222static void ipw_dtr_rts(struct usb_serial_port *port, int on) 246static void ipw_dtr_rts(struct usb_serial_port *port, int on)
223{ 247{
224 struct usb_device *dev = port->serial->dev; 248 struct usb_device *dev = port->serial->dev;
@@ -285,7 +309,7 @@ static void ipw_close(struct usb_serial_port *port)
285 dev_err(&port->dev, 309 dev_err(&port->dev,
286 "Disabling bulk RxRead failed (error = %d)\n", result); 310 "Disabling bulk RxRead failed (error = %d)\n", result);
287 311
288 usb_serial_generic_close(port); 312 usb_wwan_close(port);
289} 313}
290 314
291static struct usb_serial_driver ipw_device = { 315static struct usb_serial_driver ipw_device = {
@@ -297,9 +321,14 @@ static struct usb_serial_driver ipw_device = {
297 .usb_driver = &usb_ipw_driver, 321 .usb_driver = &usb_ipw_driver,
298 .id_table = usb_ipw_ids, 322 .id_table = usb_ipw_ids,
299 .num_ports = 1, 323 .num_ports = 1,
324 .disconnect = usb_wwan_disconnect,
300 .open = ipw_open, 325 .open = ipw_open,
301 .close = ipw_close, 326 .close = ipw_close,
327 .probe = ipw_probe,
328 .attach = usb_wwan_startup,
329 .release = ipw_release,
302 .dtr_rts = ipw_dtr_rts, 330 .dtr_rts = ipw_dtr_rts,
331 .write = usb_wwan_write,
303}; 332};
304 333
305 334
diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
index 40abedbc5943..3524a105d042 100644
--- a/drivers/usb/serial/mos7720.c
+++ b/drivers/usb/serial/mos7720.c
@@ -2006,7 +2006,6 @@ static int mos7720_ioctl(struct tty_struct *tty,
2006 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number); 2006 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2007 return get_lsr_info(tty, mos7720_port, 2007 return get_lsr_info(tty, mos7720_port,
2008 (unsigned int __user *)arg); 2008 (unsigned int __user *)arg);
2009 return 0;
2010 2009
2011 /* FIXME: These should be using the mode methods */ 2010 /* FIXME: These should be using the mode methods */
2012 case TIOCMBIS: 2011 case TIOCMBIS:
diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index 7b50aa122752..c72abd524983 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -2263,7 +2263,6 @@ static int mos7840_ioctl(struct tty_struct *tty,
2263 case TIOCSERGETLSR: 2263 case TIOCSERGETLSR:
2264 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number); 2264 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2265 return mos7840_get_lsr_info(tty, argp); 2265 return mos7840_get_lsr_info(tty, argp);
2266 return 0;
2267 2266
2268 case TIOCGSERIAL: 2267 case TIOCGSERIAL:
2269 dbg("%s (%d) TIOCGSERIAL", __func__, port->number); 2268 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c
index 96423f3c8ef3..c248a9147439 100644
--- a/drivers/usb/serial/opticon.c
+++ b/drivers/usb/serial/opticon.c
@@ -523,7 +523,7 @@ static int opticon_startup(struct usb_serial *serial)
523 goto error; 523 goto error;
524 } 524 }
525 525
526 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2; 526 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
527 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); 527 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
528 if (!priv->bulk_in_buffer) { 528 if (!priv->bulk_in_buffer) {
529 dev_err(&priv->udev->dev, "out of memory\n"); 529 dev_err(&priv->udev->dev, "out of memory\n");
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index fe22e90bc879..d865878c9f97 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -156,6 +156,7 @@ static void option_instat_callback(struct urb *urb);
156#define HUAWEI_PRODUCT_K4511 0x14CC 156#define HUAWEI_PRODUCT_K4511 0x14CC
157#define HUAWEI_PRODUCT_ETS1220 0x1803 157#define HUAWEI_PRODUCT_ETS1220 0x1803
158#define HUAWEI_PRODUCT_E353 0x1506 158#define HUAWEI_PRODUCT_E353 0x1506
159#define HUAWEI_PRODUCT_E173S 0x1C05
159 160
160#define QUANTA_VENDOR_ID 0x0408 161#define QUANTA_VENDOR_ID 0x0408
161#define QUANTA_PRODUCT_Q101 0xEA02 162#define QUANTA_PRODUCT_Q101 0xEA02
@@ -316,6 +317,9 @@ static void option_instat_callback(struct urb *urb);
316#define ZTE_PRODUCT_AC8710 0xfff1 317#define ZTE_PRODUCT_AC8710 0xfff1
317#define ZTE_PRODUCT_AC2726 0xfff5 318#define ZTE_PRODUCT_AC2726 0xfff5
318#define ZTE_PRODUCT_AC8710T 0xffff 319#define ZTE_PRODUCT_AC8710T 0xffff
320#define ZTE_PRODUCT_MC2718 0xffe8
321#define ZTE_PRODUCT_AD3812 0xffeb
322#define ZTE_PRODUCT_MC2716 0xffed
319 323
320#define BENQ_VENDOR_ID 0x04a5 324#define BENQ_VENDOR_ID 0x04a5
321#define BENQ_PRODUCT_H10 0x4068 325#define BENQ_PRODUCT_H10 0x4068
@@ -468,6 +472,10 @@ static void option_instat_callback(struct urb *urb);
468#define YUGA_PRODUCT_CLU528 0x260D 472#define YUGA_PRODUCT_CLU528 0x260D
469#define YUGA_PRODUCT_CLU526 0x260F 473#define YUGA_PRODUCT_CLU526 0x260F
470 474
475/* Viettel products */
476#define VIETTEL_VENDOR_ID 0x2262
477#define VIETTEL_PRODUCT_VT1000 0x0002
478
471/* some devices interfaces need special handling due to a number of reasons */ 479/* some devices interfaces need special handling due to a number of reasons */
472enum option_blacklist_reason { 480enum option_blacklist_reason {
473 OPTION_BLACKLIST_NONE = 0, 481 OPTION_BLACKLIST_NONE = 0,
@@ -475,31 +483,66 @@ enum option_blacklist_reason {
475 OPTION_BLACKLIST_RESERVED_IF = 2 483 OPTION_BLACKLIST_RESERVED_IF = 2
476}; 484};
477 485
486#define MAX_BL_NUM 8
478struct option_blacklist_info { 487struct option_blacklist_info {
479 const u32 infolen; /* number of interface numbers on blacklist */ 488 /* bitfield of interface numbers for OPTION_BLACKLIST_SENDSETUP */
480 const u8 *ifaceinfo; /* pointer to the array holding the numbers */ 489 const unsigned long sendsetup;
481 enum option_blacklist_reason reason; 490 /* bitfield of interface numbers for OPTION_BLACKLIST_RESERVED_IF */
491 const unsigned long reserved;
482}; 492};
483 493
484static const u8 four_g_w14_no_sendsetup[] = { 0, 1 };
485static const struct option_blacklist_info four_g_w14_blacklist = { 494static const struct option_blacklist_info four_g_w14_blacklist = {
486 .infolen = ARRAY_SIZE(four_g_w14_no_sendsetup), 495 .sendsetup = BIT(0) | BIT(1),
487 .ifaceinfo = four_g_w14_no_sendsetup,
488 .reason = OPTION_BLACKLIST_SENDSETUP
489}; 496};
490 497
491static const u8 alcatel_x200_no_sendsetup[] = { 0, 1 };
492static const struct option_blacklist_info alcatel_x200_blacklist = { 498static const struct option_blacklist_info alcatel_x200_blacklist = {
493 .infolen = ARRAY_SIZE(alcatel_x200_no_sendsetup), 499 .sendsetup = BIT(0) | BIT(1),
494 .ifaceinfo = alcatel_x200_no_sendsetup, 500};
495 .reason = OPTION_BLACKLIST_SENDSETUP 501
502static const struct option_blacklist_info zte_0037_blacklist = {
503 .sendsetup = BIT(0) | BIT(1),
496}; 504};
497 505
498static const u8 zte_k3765_z_no_sendsetup[] = { 0, 1, 2 };
499static const struct option_blacklist_info zte_k3765_z_blacklist = { 506static const struct option_blacklist_info zte_k3765_z_blacklist = {
500 .infolen = ARRAY_SIZE(zte_k3765_z_no_sendsetup), 507 .sendsetup = BIT(0) | BIT(1) | BIT(2),
501 .ifaceinfo = zte_k3765_z_no_sendsetup, 508 .reserved = BIT(4),
502 .reason = OPTION_BLACKLIST_SENDSETUP 509};
510
511static const struct option_blacklist_info zte_ad3812_z_blacklist = {
512 .sendsetup = BIT(0) | BIT(1) | BIT(2),
513};
514
515static const struct option_blacklist_info zte_mc2718_z_blacklist = {
516 .sendsetup = BIT(1) | BIT(2) | BIT(3) | BIT(4),
517};
518
519static const struct option_blacklist_info zte_mc2716_z_blacklist = {
520 .sendsetup = BIT(1) | BIT(2) | BIT(3),
521};
522
523static const struct option_blacklist_info huawei_cdc12_blacklist = {
524 .reserved = BIT(1) | BIT(2),
525};
526
527static const struct option_blacklist_info net_intf1_blacklist = {
528 .reserved = BIT(1),
529};
530
531static const struct option_blacklist_info net_intf3_blacklist = {
532 .reserved = BIT(3),
533};
534
535static const struct option_blacklist_info net_intf4_blacklist = {
536 .reserved = BIT(4),
537};
538
539static const struct option_blacklist_info net_intf5_blacklist = {
540 .reserved = BIT(5),
541};
542
543static const struct option_blacklist_info zte_mf626_blacklist = {
544 .sendsetup = BIT(0) | BIT(1),
545 .reserved = BIT(4),
503}; 546};
504 547
505static const struct usb_device_id option_ids[] = { 548static const struct usb_device_id option_ids[] = {
@@ -599,12 +642,16 @@ static const struct usb_device_id option_ids[] = {
599 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143D, 0xff, 0xff, 0xff) }, 642 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143D, 0xff, 0xff, 0xff) },
600 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143E, 0xff, 0xff, 0xff) }, 643 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143E, 0xff, 0xff, 0xff) },
601 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143F, 0xff, 0xff, 0xff) }, 644 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E143F, 0xff, 0xff, 0xff) },
602 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K4505, 0xff, 0xff, 0xff) }, 645 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E173S, 0xff, 0xff, 0xff) },
603 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3765, 0xff, 0xff, 0xff) }, 646 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K4505, 0xff, 0xff, 0xff),
647 .driver_info = (kernel_ulong_t) &huawei_cdc12_blacklist },
648 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3765, 0xff, 0xff, 0xff),
649 .driver_info = (kernel_ulong_t) &huawei_cdc12_blacklist },
604 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_ETS1220, 0xff, 0xff, 0xff) }, 650 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_ETS1220, 0xff, 0xff, 0xff) },
605 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E14AC, 0xff, 0xff, 0xff) }, 651 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E14AC, 0xff, 0xff, 0xff) },
606 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3806, 0xff, 0xff, 0xff) }, 652 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3806, 0xff, 0xff, 0xff) },
607 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K4605, 0xff, 0xff, 0xff) }, 653 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K4605, 0xff, 0xff, 0xff),
654 .driver_info = (kernel_ulong_t) &huawei_cdc12_blacklist },
608 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3770, 0xff, 0x02, 0x31) }, 655 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3770, 0xff, 0x02, 0x31) },
609 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3770, 0xff, 0x02, 0x32) }, 656 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3770, 0xff, 0x02, 0x32) },
610 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3771, 0xff, 0x02, 0x31) }, 657 { USB_DEVICE_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_K3771, 0xff, 0x02, 0x31) },
@@ -705,7 +752,8 @@ static const struct usb_device_id option_ids[] = {
705 { USB_DEVICE(TELIT_VENDOR_ID, TELIT_PRODUCT_UC864E) }, 752 { USB_DEVICE(TELIT_VENDOR_ID, TELIT_PRODUCT_UC864E) },
706 { USB_DEVICE(TELIT_VENDOR_ID, TELIT_PRODUCT_UC864G) }, 753 { USB_DEVICE(TELIT_VENDOR_ID, TELIT_PRODUCT_UC864G) },
707 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF622, 0xff, 0xff, 0xff) }, /* ZTE WCDMA products */ 754 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF622, 0xff, 0xff, 0xff) }, /* ZTE WCDMA products */
708 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0002, 0xff, 0xff, 0xff) }, 755 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0002, 0xff, 0xff, 0xff),
756 .driver_info = (kernel_ulong_t)&net_intf1_blacklist },
709 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0003, 0xff, 0xff, 0xff) }, 757 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0003, 0xff, 0xff, 0xff) },
710 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0004, 0xff, 0xff, 0xff) }, 758 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0004, 0xff, 0xff, 0xff) },
711 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0005, 0xff, 0xff, 0xff) }, 759 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0005, 0xff, 0xff, 0xff) },
@@ -720,51 +768,62 @@ static const struct usb_device_id option_ids[] = {
720 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x000f, 0xff, 0xff, 0xff) }, 768 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x000f, 0xff, 0xff, 0xff) },
721 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0010, 0xff, 0xff, 0xff) }, 769 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0010, 0xff, 0xff, 0xff) },
722 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0011, 0xff, 0xff, 0xff) }, 770 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0011, 0xff, 0xff, 0xff) },
723 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0012, 0xff, 0xff, 0xff) }, 771 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0012, 0xff, 0xff, 0xff),
772 .driver_info = (kernel_ulong_t)&net_intf1_blacklist },
724 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0013, 0xff, 0xff, 0xff) }, 773 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0013, 0xff, 0xff, 0xff) },
725 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0014, 0xff, 0xff, 0xff) }, 774 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0014, 0xff, 0xff, 0xff) },
726 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF628, 0xff, 0xff, 0xff) }, 775 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF628, 0xff, 0xff, 0xff) },
727 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0016, 0xff, 0xff, 0xff) }, 776 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0016, 0xff, 0xff, 0xff) },
728 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0017, 0xff, 0xff, 0xff) }, 777 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0017, 0xff, 0xff, 0xff),
778 .driver_info = (kernel_ulong_t)&net_intf3_blacklist },
729 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0018, 0xff, 0xff, 0xff) }, 779 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0018, 0xff, 0xff, 0xff) },
730 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0019, 0xff, 0xff, 0xff) }, 780 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0019, 0xff, 0xff, 0xff) },
731 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0020, 0xff, 0xff, 0xff) }, 781 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0020, 0xff, 0xff, 0xff) },
732 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0021, 0xff, 0xff, 0xff) }, 782 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0021, 0xff, 0xff, 0xff),
783 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
733 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0022, 0xff, 0xff, 0xff) }, 784 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0022, 0xff, 0xff, 0xff) },
734 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0023, 0xff, 0xff, 0xff) }, 785 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0023, 0xff, 0xff, 0xff) },
735 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0024, 0xff, 0xff, 0xff) }, 786 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0024, 0xff, 0xff, 0xff) },
736 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0025, 0xff, 0xff, 0xff) }, 787 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0025, 0xff, 0xff, 0xff),
788 .driver_info = (kernel_ulong_t)&net_intf1_blacklist },
737 /* { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0026, 0xff, 0xff, 0xff) }, */ 789 /* { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0026, 0xff, 0xff, 0xff) }, */
738 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0028, 0xff, 0xff, 0xff) }, 790 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0028, 0xff, 0xff, 0xff) },
739 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0029, 0xff, 0xff, 0xff) }, 791 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0029, 0xff, 0xff, 0xff) },
740 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0030, 0xff, 0xff, 0xff) }, 792 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0030, 0xff, 0xff, 0xff) },
741 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF626, 0xff, 793 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MF626, 0xff,
742 0xff, 0xff), .driver_info = (kernel_ulong_t)&four_g_w14_blacklist }, 794 0xff, 0xff), .driver_info = (kernel_ulong_t)&zte_mf626_blacklist },
743 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0032, 0xff, 0xff, 0xff) }, 795 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0032, 0xff, 0xff, 0xff) },
744 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0033, 0xff, 0xff, 0xff) }, 796 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0033, 0xff, 0xff, 0xff) },
745 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0034, 0xff, 0xff, 0xff) }, 797 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0034, 0xff, 0xff, 0xff) },
746 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0037, 0xff, 0xff, 0xff) }, 798 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0037, 0xff, 0xff, 0xff),
799 .driver_info = (kernel_ulong_t)&zte_0037_blacklist },
747 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0038, 0xff, 0xff, 0xff) }, 800 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0038, 0xff, 0xff, 0xff) },
748 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0039, 0xff, 0xff, 0xff) }, 801 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0039, 0xff, 0xff, 0xff) },
749 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0040, 0xff, 0xff, 0xff) }, 802 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0040, 0xff, 0xff, 0xff) },
750 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0042, 0xff, 0xff, 0xff) }, 803 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0042, 0xff, 0xff, 0xff),
804 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
751 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0043, 0xff, 0xff, 0xff) }, 805 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0043, 0xff, 0xff, 0xff) },
752 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0044, 0xff, 0xff, 0xff) }, 806 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0044, 0xff, 0xff, 0xff) },
753 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0048, 0xff, 0xff, 0xff) }, 807 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0048, 0xff, 0xff, 0xff) },
754 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0049, 0xff, 0xff, 0xff) }, 808 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0049, 0xff, 0xff, 0xff),
809 .driver_info = (kernel_ulong_t)&net_intf5_blacklist },
755 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0050, 0xff, 0xff, 0xff) }, 810 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0050, 0xff, 0xff, 0xff) },
756 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0051, 0xff, 0xff, 0xff) }, 811 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0051, 0xff, 0xff, 0xff) },
757 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0052, 0xff, 0xff, 0xff) }, 812 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0052, 0xff, 0xff, 0xff),
813 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
758 /* { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0053, 0xff, 0xff, 0xff) }, */ 814 /* { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0053, 0xff, 0xff, 0xff) }, */
759 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0054, 0xff, 0xff, 0xff) }, 815 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0054, 0xff, 0xff, 0xff) },
760 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0055, 0xff, 0xff, 0xff) }, 816 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0055, 0xff, 0xff, 0xff),
817 .driver_info = (kernel_ulong_t)&net_intf1_blacklist },
761 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0056, 0xff, 0xff, 0xff) }, 818 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0056, 0xff, 0xff, 0xff) },
762 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0057, 0xff, 0xff, 0xff) }, 819 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0057, 0xff, 0xff, 0xff) },
763 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0058, 0xff, 0xff, 0xff) }, 820 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0058, 0xff, 0xff, 0xff),
821 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
764 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0059, 0xff, 0xff, 0xff) }, 822 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0059, 0xff, 0xff, 0xff) },
765 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0061, 0xff, 0xff, 0xff) }, 823 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0061, 0xff, 0xff, 0xff) },
766 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0062, 0xff, 0xff, 0xff) }, 824 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0062, 0xff, 0xff, 0xff) },
767 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0063, 0xff, 0xff, 0xff) }, 825 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0063, 0xff, 0xff, 0xff),
826 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
768 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0064, 0xff, 0xff, 0xff) }, 827 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0064, 0xff, 0xff, 0xff) },
769 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0065, 0xff, 0xff, 0xff) }, 828 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0065, 0xff, 0xff, 0xff) },
770 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0066, 0xff, 0xff, 0xff) }, 829 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0066, 0xff, 0xff, 0xff) },
@@ -779,11 +838,13 @@ static const struct usb_device_id option_ids[] = {
779 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0083, 0xff, 0xff, 0xff) }, 838 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0083, 0xff, 0xff, 0xff) },
780 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0086, 0xff, 0xff, 0xff) }, 839 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0086, 0xff, 0xff, 0xff) },
781 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0087, 0xff, 0xff, 0xff) }, 840 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0087, 0xff, 0xff, 0xff) },
782 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0104, 0xff, 0xff, 0xff) }, 841 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0104, 0xff, 0xff, 0xff),
842 .driver_info = (kernel_ulong_t)&net_intf4_blacklist },
783 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0105, 0xff, 0xff, 0xff) }, 843 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0105, 0xff, 0xff, 0xff) },
784 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0106, 0xff, 0xff, 0xff) }, 844 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0106, 0xff, 0xff, 0xff) },
785 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0108, 0xff, 0xff, 0xff) }, 845 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0108, 0xff, 0xff, 0xff) },
786 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0113, 0xff, 0xff, 0xff) }, 846 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0113, 0xff, 0xff, 0xff),
847 .driver_info = (kernel_ulong_t)&net_intf5_blacklist },
787 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0117, 0xff, 0xff, 0xff) }, 848 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0117, 0xff, 0xff, 0xff) },
788 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0118, 0xff, 0xff, 0xff) }, 849 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0118, 0xff, 0xff, 0xff) },
789 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0121, 0xff, 0xff, 0xff) }, 850 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0121, 0xff, 0xff, 0xff) },
@@ -1003,6 +1064,12 @@ static const struct usb_device_id option_ids[] = {
1003 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710, 0xff, 0xff, 0xff) }, 1064 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710, 0xff, 0xff, 0xff) },
1004 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC2726, 0xff, 0xff, 0xff) }, 1065 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC2726, 0xff, 0xff, 0xff) },
1005 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710T, 0xff, 0xff, 0xff) }, 1066 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710T, 0xff, 0xff, 0xff) },
1067 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MC2718, 0xff, 0xff, 0xff),
1068 .driver_info = (kernel_ulong_t)&zte_mc2718_z_blacklist },
1069 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AD3812, 0xff, 0xff, 0xff),
1070 .driver_info = (kernel_ulong_t)&zte_ad3812_z_blacklist },
1071 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_MC2716, 0xff, 0xff, 0xff),
1072 .driver_info = (kernel_ulong_t)&zte_mc2716_z_blacklist },
1006 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_H10) }, 1073 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_H10) },
1007 { USB_DEVICE(DLINK_VENDOR_ID, DLINK_PRODUCT_DWM_652) }, 1074 { USB_DEVICE(DLINK_VENDOR_ID, DLINK_PRODUCT_DWM_652) },
1008 { USB_DEVICE(ALINK_VENDOR_ID, DLINK_PRODUCT_DWM_652_U5) }, /* Yes, ALINK_VENDOR_ID */ 1075 { USB_DEVICE(ALINK_VENDOR_ID, DLINK_PRODUCT_DWM_652_U5) }, /* Yes, ALINK_VENDOR_ID */
@@ -1101,6 +1168,7 @@ static const struct usb_device_id option_ids[] = {
1101 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU516) }, 1168 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU516) },
1102 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU528) }, 1169 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU528) },
1103 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU526) }, 1170 { USB_DEVICE(YUGA_VENDOR_ID, YUGA_PRODUCT_CLU526) },
1171 { USB_DEVICE_AND_INTERFACE_INFO(VIETTEL_VENDOR_ID, VIETTEL_PRODUCT_VT1000, 0xff, 0xff, 0xff) },
1104 { } /* Terminating entry */ 1172 { } /* Terminating entry */
1105}; 1173};
1106MODULE_DEVICE_TABLE(usb, option_ids); 1174MODULE_DEVICE_TABLE(usb, option_ids);
@@ -1214,10 +1282,35 @@ static void __exit option_exit(void)
1214module_init(option_init); 1282module_init(option_init);
1215module_exit(option_exit); 1283module_exit(option_exit);
1216 1284
1285static bool is_blacklisted(const u8 ifnum, enum option_blacklist_reason reason,
1286 const struct option_blacklist_info *blacklist)
1287{
1288 unsigned long num;
1289 const unsigned long *intf_list;
1290
1291 if (blacklist) {
1292 if (reason == OPTION_BLACKLIST_SENDSETUP)
1293 intf_list = &blacklist->sendsetup;
1294 else if (reason == OPTION_BLACKLIST_RESERVED_IF)
1295 intf_list = &blacklist->reserved;
1296 else {
1297 BUG_ON(reason);
1298 return false;
1299 }
1300
1301 for_each_set_bit(num, intf_list, MAX_BL_NUM + 1) {
1302 if (num == ifnum)
1303 return true;
1304 }
1305 }
1306 return false;
1307}
1308
1217static int option_probe(struct usb_serial *serial, 1309static int option_probe(struct usb_serial *serial,
1218 const struct usb_device_id *id) 1310 const struct usb_device_id *id)
1219{ 1311{
1220 struct usb_wwan_intf_private *data; 1312 struct usb_wwan_intf_private *data;
1313
1221 /* D-Link DWM 652 still exposes CD-Rom emulation interface in modem mode */ 1314 /* D-Link DWM 652 still exposes CD-Rom emulation interface in modem mode */
1222 if (serial->dev->descriptor.idVendor == DLINK_VENDOR_ID && 1315 if (serial->dev->descriptor.idVendor == DLINK_VENDOR_ID &&
1223 serial->dev->descriptor.idProduct == DLINK_PRODUCT_DWM_652 && 1316 serial->dev->descriptor.idProduct == DLINK_PRODUCT_DWM_652 &&
@@ -1230,14 +1323,14 @@ static int option_probe(struct usb_serial *serial,
1230 serial->interface->cur_altsetting->desc.bInterfaceClass != 0xff) 1323 serial->interface->cur_altsetting->desc.bInterfaceClass != 0xff)
1231 return -ENODEV; 1324 return -ENODEV;
1232 1325
1233 /* Don't bind network interfaces on Huawei K3765, K4505 & K4605 */ 1326 /* Don't bind reserved interfaces (like network ones) which often have
1234 if (serial->dev->descriptor.idVendor == HUAWEI_VENDOR_ID && 1327 * the same class/subclass/protocol as the serial interfaces. Look at
1235 (serial->dev->descriptor.idProduct == HUAWEI_PRODUCT_K3765 || 1328 * the Windows driver .INF files for reserved interface numbers.
1236 serial->dev->descriptor.idProduct == HUAWEI_PRODUCT_K4505 || 1329 */
1237 serial->dev->descriptor.idProduct == HUAWEI_PRODUCT_K4605) && 1330 if (is_blacklisted(
1238 (serial->interface->cur_altsetting->desc.bInterfaceNumber == 1 || 1331 serial->interface->cur_altsetting->desc.bInterfaceNumber,
1239 serial->interface->cur_altsetting->desc.bInterfaceNumber == 2)) 1332 OPTION_BLACKLIST_RESERVED_IF,
1240 return -ENODEV; 1333 (const struct option_blacklist_info *) id->driver_info))
1241 1334
1242 /* Don't bind network interface on Samsung GT-B3730, it is handled by a separate module */ 1335 /* Don't bind network interface on Samsung GT-B3730, it is handled by a separate module */
1243 if (serial->dev->descriptor.idVendor == SAMSUNG_VENDOR_ID && 1336 if (serial->dev->descriptor.idVendor == SAMSUNG_VENDOR_ID &&
@@ -1246,7 +1339,6 @@ static int option_probe(struct usb_serial *serial,
1246 return -ENODEV; 1339 return -ENODEV;
1247 1340
1248 data = serial->private = kzalloc(sizeof(struct usb_wwan_intf_private), GFP_KERNEL); 1341 data = serial->private = kzalloc(sizeof(struct usb_wwan_intf_private), GFP_KERNEL);
1249
1250 if (!data) 1342 if (!data)
1251 return -ENOMEM; 1343 return -ENOMEM;
1252 data->send_setup = option_send_setup; 1344 data->send_setup = option_send_setup;
@@ -1255,23 +1347,6 @@ static int option_probe(struct usb_serial *serial,
1255 return 0; 1347 return 0;
1256} 1348}
1257 1349
1258static enum option_blacklist_reason is_blacklisted(const u8 ifnum,
1259 const struct option_blacklist_info *blacklist)
1260{
1261 const u8 *info;
1262 int i;
1263
1264 if (blacklist) {
1265 info = blacklist->ifaceinfo;
1266
1267 for (i = 0; i < blacklist->infolen; i++) {
1268 if (info[i] == ifnum)
1269 return blacklist->reason;
1270 }
1271 }
1272 return OPTION_BLACKLIST_NONE;
1273}
1274
1275static void option_instat_callback(struct urb *urb) 1350static void option_instat_callback(struct urb *urb)
1276{ 1351{
1277 int err; 1352 int err;
@@ -1343,9 +1418,8 @@ static int option_send_setup(struct usb_serial_port *port)
1343 int val = 0; 1418 int val = 0;
1344 dbg("%s", __func__); 1419 dbg("%s", __func__);
1345 1420
1346 if (is_blacklisted(ifNum, 1421 if (is_blacklisted(ifNum, OPTION_BLACKLIST_SENDSETUP,
1347 (struct option_blacklist_info *) intfdata->private) 1422 (struct option_blacklist_info *) intfdata->private)) {
1348 == OPTION_BLACKLIST_SENDSETUP) {
1349 dbg("No send_setup on blacklisted interface #%d\n", ifNum); 1423 dbg("No send_setup on blacklisted interface #%d\n", ifNum);
1350 return -EIO; 1424 return -EIO;
1351 } 1425 }
diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index 1d33260de014..fc2d66f7f4eb 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -91,7 +91,7 @@ static const struct usb_device_id id_table[] = {
91 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) }, 91 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
92 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) }, 92 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
93 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) }, 93 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
94 { USB_DEVICE(WINCHIPHEAD_VENDOR_ID, WINCHIPHEAD_USBSER_PRODUCT_ID) }, 94 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
95 { } /* Terminating entry */ 95 { } /* Terminating entry */
96}; 96};
97 97
@@ -360,9 +360,6 @@ static void pl2303_set_termios(struct tty_struct *tty,
360 tmp >>= 2; 360 tmp >>= 2;
361 buf[1] <<= 1; 361 buf[1] <<= 1;
362 } 362 }
363 if (tmp > 256) {
364 tmp %= 256;
365 }
366 buf[0] = tmp; 363 buf[0] = tmp;
367 } 364 }
368 } 365 }
diff --git a/drivers/usb/serial/pl2303.h b/drivers/usb/serial/pl2303.h
index ca0d237683b3..c38b8c00c06f 100644
--- a/drivers/usb/serial/pl2303.h
+++ b/drivers/usb/serial/pl2303.h
@@ -145,6 +145,7 @@
145#define ADLINK_VENDOR_ID 0x0b63 145#define ADLINK_VENDOR_ID 0x0b63
146#define ADLINK_ND6530_PRODUCT_ID 0x6530 146#define ADLINK_ND6530_PRODUCT_ID 0x6530
147 147
148/* WinChipHead USB->RS 232 adapter */ 148/* SMART USB Serial Adapter */
149#define WINCHIPHEAD_VENDOR_ID 0x4348 149#define SMART_VENDOR_ID 0x0b8c
150#define WINCHIPHEAD_USBSER_PRODUCT_ID 0x5523 150#define SMART_PRODUCT_ID 0x2303
151
diff --git a/drivers/usb/serial/qcserial.c b/drivers/usb/serial/qcserial.c
index aeccc7f0a93c..aa9367f5b421 100644
--- a/drivers/usb/serial/qcserial.c
+++ b/drivers/usb/serial/qcserial.c
@@ -13,6 +13,7 @@
13 13
14#include <linux/tty.h> 14#include <linux/tty.h>
15#include <linux/tty_flip.h> 15#include <linux/tty_flip.h>
16#include <linux/module.h>
16#include <linux/usb.h> 17#include <linux/usb.h>
17#include <linux/usb/serial.h> 18#include <linux/usb/serial.h>
18#include <linux/slab.h> 19#include <linux/slab.h>
@@ -28,6 +29,7 @@ static const struct usb_device_id id_table[] = {
28 {USB_DEVICE(0x05c6, 0x9212)}, /* Acer Gobi Modem Device */ 29 {USB_DEVICE(0x05c6, 0x9212)}, /* Acer Gobi Modem Device */
29 {USB_DEVICE(0x03f0, 0x1f1d)}, /* HP un2400 Gobi Modem Device */ 30 {USB_DEVICE(0x03f0, 0x1f1d)}, /* HP un2400 Gobi Modem Device */
30 {USB_DEVICE(0x03f0, 0x201d)}, /* HP un2400 Gobi QDL Device */ 31 {USB_DEVICE(0x03f0, 0x201d)}, /* HP un2400 Gobi QDL Device */
32 {USB_DEVICE(0x03f0, 0x371d)}, /* HP un2430 Mobile Broadband Module */
31 {USB_DEVICE(0x04da, 0x250d)}, /* Panasonic Gobi Modem device */ 33 {USB_DEVICE(0x04da, 0x250d)}, /* Panasonic Gobi Modem device */
32 {USB_DEVICE(0x04da, 0x250c)}, /* Panasonic Gobi QDL device */ 34 {USB_DEVICE(0x04da, 0x250c)}, /* Panasonic Gobi QDL device */
33 {USB_DEVICE(0x413c, 0x8172)}, /* Dell Gobi Modem device */ 35 {USB_DEVICE(0x413c, 0x8172)}, /* Dell Gobi Modem device */
@@ -84,6 +86,7 @@ static const struct usb_device_id id_table[] = {
84 {USB_DEVICE(0x16d8, 0x8002)}, /* CMDTech Gobi 2000 Modem device (VU922) */ 86 {USB_DEVICE(0x16d8, 0x8002)}, /* CMDTech Gobi 2000 Modem device (VU922) */
85 {USB_DEVICE(0x05c6, 0x9204)}, /* Gobi 2000 QDL device */ 87 {USB_DEVICE(0x05c6, 0x9204)}, /* Gobi 2000 QDL device */
86 {USB_DEVICE(0x05c6, 0x9205)}, /* Gobi 2000 Modem device */ 88 {USB_DEVICE(0x05c6, 0x9205)}, /* Gobi 2000 Modem device */
89 {USB_DEVICE(0x1199, 0x9013)}, /* Sierra Wireless Gobi 3000 Modem device (MC8355) */
87 { } /* Terminating entry */ 90 { } /* Terminating entry */
88}; 91};
89MODULE_DEVICE_TABLE(usb, id_table); 92MODULE_DEVICE_TABLE(usb, id_table);
diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c
index d5d136a53b61..b18179bda0d8 100644
--- a/drivers/usb/serial/sierra.c
+++ b/drivers/usb/serial/sierra.c
@@ -1009,7 +1009,7 @@ static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
1009 struct sierra_intf_private *intfdata; 1009 struct sierra_intf_private *intfdata;
1010 int b; 1010 int b;
1011 1011
1012 if (message.event & PM_EVENT_AUTO) { 1012 if (PMSG_IS_AUTO(message)) {
1013 intfdata = serial->private; 1013 intfdata = serial->private;
1014 spin_lock_irq(&intfdata->susp_lock); 1014 spin_lock_irq(&intfdata->susp_lock);
1015 b = intfdata->in_flight; 1015 b = intfdata->in_flight;
diff --git a/drivers/usb/serial/symbolserial.c b/drivers/usb/serial/symbolserial.c
index d9457bd4fe10..7096f799b071 100644
--- a/drivers/usb/serial/symbolserial.c
+++ b/drivers/usb/serial/symbolserial.c
@@ -226,7 +226,7 @@ static int symbol_startup(struct usb_serial *serial)
226 goto error; 226 goto error;
227 } 227 }
228 228
229 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2; 229 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
230 priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL); 230 priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
231 if (!priv->int_buffer) { 231 if (!priv->int_buffer) {
232 dev_err(&priv->udev->dev, "out of memory\n"); 232 dev_err(&priv->udev->dev, "out of memory\n");
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 1c031309ab25..cc274fdf2627 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -912,7 +912,7 @@ int usb_serial_probe(struct usb_interface *interface,
912 goto probe_error; 912 goto probe_error;
913 } 913 }
914 buffer_size = max_t(int, serial->type->bulk_in_size, 914 buffer_size = max_t(int, serial->type->bulk_in_size,
915 le16_to_cpu(endpoint->wMaxPacketSize)); 915 usb_endpoint_maxp(endpoint));
916 port->bulk_in_size = buffer_size; 916 port->bulk_in_size = buffer_size;
917 port->bulk_in_endpointAddress = endpoint->bEndpointAddress; 917 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
918 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); 918 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -942,7 +942,7 @@ int usb_serial_probe(struct usb_interface *interface,
942 goto probe_error; 942 goto probe_error;
943 buffer_size = serial->type->bulk_out_size; 943 buffer_size = serial->type->bulk_out_size;
944 if (!buffer_size) 944 if (!buffer_size)
945 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 945 buffer_size = usb_endpoint_maxp(endpoint);
946 port->bulk_out_size = buffer_size; 946 port->bulk_out_size = buffer_size;
947 port->bulk_out_endpointAddress = endpoint->bEndpointAddress; 947 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
948 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL); 948 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -990,7 +990,7 @@ int usb_serial_probe(struct usb_interface *interface,
990 "No free urbs available\n"); 990 "No free urbs available\n");
991 goto probe_error; 991 goto probe_error;
992 } 992 }
993 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 993 buffer_size = usb_endpoint_maxp(endpoint);
994 port->interrupt_in_endpointAddress = 994 port->interrupt_in_endpointAddress =
995 endpoint->bEndpointAddress; 995 endpoint->bEndpointAddress;
996 port->interrupt_in_buffer = kmalloc(buffer_size, 996 port->interrupt_in_buffer = kmalloc(buffer_size,
@@ -1021,7 +1021,7 @@ int usb_serial_probe(struct usb_interface *interface,
1021 "No free urbs available\n"); 1021 "No free urbs available\n");
1022 goto probe_error; 1022 goto probe_error;
1023 } 1023 }
1024 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 1024 buffer_size = usb_endpoint_maxp(endpoint);
1025 port->interrupt_out_size = buffer_size; 1025 port->interrupt_out_size = buffer_size;
1026 port->interrupt_out_endpointAddress = 1026 port->interrupt_out_endpointAddress =
1027 endpoint->bEndpointAddress; 1027 endpoint->bEndpointAddress;
diff --git a/drivers/usb/serial/usb_wwan.c b/drivers/usb/serial/usb_wwan.c
index e4fad5e643d7..d555ca9567b8 100644
--- a/drivers/usb/serial/usb_wwan.c
+++ b/drivers/usb/serial/usb_wwan.c
@@ -651,7 +651,7 @@ int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
651 651
652 dbg("%s entered", __func__); 652 dbg("%s entered", __func__);
653 653
654 if (message.event & PM_EVENT_AUTO) { 654 if (PMSG_IS_AUTO(message)) {
655 spin_lock_irq(&intfdata->susp_lock); 655 spin_lock_irq(&intfdata->susp_lock);
656 b = intfdata->in_flight; 656 b = intfdata->in_flight;
657 spin_unlock_irq(&intfdata->susp_lock); 657 spin_unlock_irq(&intfdata->susp_lock);
diff --git a/drivers/usb/storage/Kconfig b/drivers/usb/storage/Kconfig
index bedc4b9f2ac4..fe2d803a6347 100644
--- a/drivers/usb/storage/Kconfig
+++ b/drivers/usb/storage/Kconfig
@@ -42,7 +42,7 @@ config USB_STORAGE_REALTEK
42 42
43config REALTEK_AUTOPM 43config REALTEK_AUTOPM
44 bool "Realtek Card Reader autosuspend support" 44 bool "Realtek Card Reader autosuspend support"
45 depends on USB_STORAGE_REALTEK && CONFIG_PM_RUNTIME 45 depends on USB_STORAGE_REALTEK && PM_RUNTIME
46 default y 46 default y
47 47
48config USB_STORAGE_DATAFAB 48config USB_STORAGE_DATAFAB
diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
index 4dca3ef0668c..9fbe742343c6 100644
--- a/drivers/usb/storage/ene_ub6250.c
+++ b/drivers/usb/storage/ene_ub6250.c
@@ -1762,10 +1762,9 @@ static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1762 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1); 1762 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1763 } else { 1763 } else {
1764 void *buf; 1764 void *buf;
1765 int offset; 1765 int offset = 0;
1766 u16 PhyBlockAddr; 1766 u16 PhyBlockAddr;
1767 u8 PageNum; 1767 u8 PageNum;
1768 u32 result;
1769 u16 len, oldphy, newphy; 1768 u16 len, oldphy, newphy;
1770 1769
1771 buf = kmalloc(blenByte, GFP_KERNEL); 1770 buf = kmalloc(blenByte, GFP_KERNEL);
diff --git a/drivers/usb/storage/option_ms.c b/drivers/usb/storage/option_ms.c
index 89460181d122..e0f76bb05915 100644
--- a/drivers/usb/storage/option_ms.c
+++ b/drivers/usb/storage/option_ms.c
@@ -22,6 +22,7 @@
22 22
23#include <linux/usb.h> 23#include <linux/usb.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/module.h>
25 26
26#include "usb.h" 27#include "usb.h"
27#include "transport.h" 28#include "transport.h"
diff --git a/drivers/usb/storage/protocol.c b/drivers/usb/storage/protocol.c
index fc310f75eada..82dd834709c7 100644
--- a/drivers/usb/storage/protocol.c
+++ b/drivers/usb/storage/protocol.c
@@ -43,6 +43,7 @@
43 */ 43 */
44 44
45#include <linux/highmem.h> 45#include <linux/highmem.h>
46#include <linux/export.h>
46#include <scsi/scsi.h> 47#include <scsi/scsi.h>
47#include <scsi/scsi_cmnd.h> 48#include <scsi/scsi_cmnd.h>
48 49
@@ -58,7 +59,9 @@
58 59
59void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 60void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
60{ 61{
61 /* Pad the SCSI command with zeros out to 12 bytes 62 /*
63 * Pad the SCSI command with zeros out to 12 bytes. If the
64 * command already is 12 bytes or longer, leave it alone.
62 * 65 *
63 * NOTE: This only works because a scsi_cmnd struct field contains 66 * NOTE: This only works because a scsi_cmnd struct field contains
64 * a unsigned char cmnd[16], so we know we have storage available 67 * a unsigned char cmnd[16], so we know we have storage available
@@ -66,9 +69,6 @@ void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
66 for (; srb->cmd_len<12; srb->cmd_len++) 69 for (; srb->cmd_len<12; srb->cmd_len++)
67 srb->cmnd[srb->cmd_len] = 0; 70 srb->cmnd[srb->cmd_len] = 0;
68 71
69 /* set command length to 12 bytes */
70 srb->cmd_len = 12;
71
72 /* send the command to the transport layer */ 72 /* send the command to the transport layer */
73 usb_stor_invoke_transport(srb, us); 73 usb_stor_invoke_transport(srb, us);
74} 74}
diff --git a/drivers/usb/storage/realtek_cr.c b/drivers/usb/storage/realtek_cr.c
index 34adc4b42ceb..0ce5f79197e7 100644
--- a/drivers/usb/storage/realtek_cr.c
+++ b/drivers/usb/storage/realtek_cr.c
@@ -25,7 +25,6 @@
25#include <linux/kthread.h> 25#include <linux/kthread.h>
26#include <linux/sched.h> 26#include <linux/sched.h>
27#include <linux/kernel.h> 27#include <linux/kernel.h>
28#include <linux/version.h>
29 28
30#include <scsi/scsi.h> 29#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h> 30#include <scsi/scsi_cmnd.h>
@@ -293,6 +292,52 @@ static int rts51x_bulk_transport(struct us_data *us, u8 lun,
293 return USB_STOR_TRANSPORT_ERROR; 292 return USB_STOR_TRANSPORT_ERROR;
294} 293}
295 294
295static int rts51x_bulk_transport_special(struct us_data *us, u8 lun,
296 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
297 enum dma_data_direction dir, int *act_len)
298{
299 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
300 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
301 int result;
302 unsigned int cswlen;
303 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
304
305 /* set up the command wrapper */
306 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
307 bcb->DataTransferLength = cpu_to_le32(buf_len);
308 bcb->Flags = (dir == DMA_FROM_DEVICE) ? 1 << 7 : 0;
309 bcb->Tag = ++us->tag;
310 bcb->Lun = lun;
311 bcb->Length = cmd_len;
312
313 /* copy the command payload */
314 memset(bcb->CDB, 0, sizeof(bcb->CDB));
315 memcpy(bcb->CDB, cmd, bcb->Length);
316
317 /* send it to out endpoint */
318 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
319 bcb, cbwlen, NULL);
320 if (result != USB_STOR_XFER_GOOD)
321 return USB_STOR_TRANSPORT_ERROR;
322
323 /* DATA STAGE */
324 /* send/receive data payload, if there is any */
325
326 if (buf && buf_len) {
327 unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
328 us->recv_bulk_pipe : us->send_bulk_pipe;
329 result = usb_stor_bulk_transfer_buf(us, pipe,
330 buf, buf_len, NULL);
331 if (result == USB_STOR_XFER_ERROR)
332 return USB_STOR_TRANSPORT_ERROR;
333 }
334
335 /* get CSW for device status */
336 result = usb_bulk_msg(us->pusb_dev, us->recv_bulk_pipe, bcs,
337 US_BULK_CS_WRAP_LEN, &cswlen, 250);
338 return result;
339}
340
296/* Determine what the maximum LUN supported is */ 341/* Determine what the maximum LUN supported is */
297static int rts51x_get_max_lun(struct us_data *us) 342static int rts51x_get_max_lun(struct us_data *us)
298{ 343{
@@ -320,6 +365,11 @@ static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
320{ 365{
321 int retval; 366 int retval;
322 u8 cmnd[12] = { 0 }; 367 u8 cmnd[12] = { 0 };
368 u8 *buf;
369
370 buf = kmalloc(len, GFP_NOIO);
371 if (buf == NULL)
372 return USB_STOR_TRANSPORT_ERROR;
323 373
324 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len); 374 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
325 375
@@ -331,10 +381,14 @@ static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
331 cmnd[5] = (u8) len; 381 cmnd[5] = (u8) len;
332 382
333 retval = rts51x_bulk_transport(us, 0, cmnd, 12, 383 retval = rts51x_bulk_transport(us, 0, cmnd, 12,
334 data, len, DMA_FROM_DEVICE, NULL); 384 buf, len, DMA_FROM_DEVICE, NULL);
335 if (retval != USB_STOR_TRANSPORT_GOOD) 385 if (retval != USB_STOR_TRANSPORT_GOOD) {
386 kfree(buf);
336 return -EIO; 387 return -EIO;
388 }
337 389
390 memcpy(data, buf, len);
391 kfree(buf);
338 return 0; 392 return 0;
339} 393}
340 394
@@ -342,6 +396,12 @@ static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
342{ 396{
343 int retval; 397 int retval;
344 u8 cmnd[12] = { 0 }; 398 u8 cmnd[12] = { 0 };
399 u8 *buf;
400
401 buf = kmalloc(len, GFP_NOIO);
402 if (buf == NULL)
403 return USB_STOR_TRANSPORT_ERROR;
404 memcpy(buf, data, len);
345 405
346 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len); 406 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
347 407
@@ -353,7 +413,8 @@ static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
353 cmnd[5] = (u8) len; 413 cmnd[5] = (u8) len;
354 414
355 retval = rts51x_bulk_transport(us, 0, cmnd, 12, 415 retval = rts51x_bulk_transport(us, 0, cmnd, 12,
356 data, len, DMA_TO_DEVICE, NULL); 416 buf, len, DMA_TO_DEVICE, NULL);
417 kfree(buf);
357 if (retval != USB_STOR_TRANSPORT_GOOD) 418 if (retval != USB_STOR_TRANSPORT_GOOD)
358 return -EIO; 419 return -EIO;
359 420
@@ -365,6 +426,11 @@ static int rts51x_read_status(struct us_data *us,
365{ 426{
366 int retval; 427 int retval;
367 u8 cmnd[12] = { 0 }; 428 u8 cmnd[12] = { 0 };
429 u8 *buf;
430
431 buf = kmalloc(len, GFP_NOIO);
432 if (buf == NULL)
433 return USB_STOR_TRANSPORT_ERROR;
368 434
369 US_DEBUGP("%s, lun = %d\n", __func__, lun); 435 US_DEBUGP("%s, lun = %d\n", __func__, lun);
370 436
@@ -372,10 +438,14 @@ static int rts51x_read_status(struct us_data *us,
372 cmnd[1] = 0x09; 438 cmnd[1] = 0x09;
373 439
374 retval = rts51x_bulk_transport(us, lun, cmnd, 12, 440 retval = rts51x_bulk_transport(us, lun, cmnd, 12,
375 status, len, DMA_FROM_DEVICE, actlen); 441 buf, len, DMA_FROM_DEVICE, actlen);
376 if (retval != USB_STOR_TRANSPORT_GOOD) 442 if (retval != USB_STOR_TRANSPORT_GOOD) {
443 kfree(buf);
377 return -EIO; 444 return -EIO;
445 }
378 446
447 memcpy(status, buf, len);
448 kfree(buf);
379 return 0; 449 return 0;
380} 450}
381 451
@@ -434,6 +504,29 @@ static int enable_oscillator(struct us_data *us)
434 return 0; 504 return 0;
435} 505}
436 506
507static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
508{
509 int retval;
510 u16 addr = 0xFE47;
511 u8 cmnd[12] = {0};
512
513 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __FUNCTION__, addr, len);
514
515 cmnd[0] = 0xF0;
516 cmnd[1] = 0x0E;
517 cmnd[2] = (u8)(addr >> 8);
518 cmnd[3] = (u8)addr;
519 cmnd[4] = (u8)(len >> 8);
520 cmnd[5] = (u8)len;
521
522 retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, data, len, DMA_TO_DEVICE, NULL);
523 if (retval != USB_STOR_TRANSPORT_GOOD) {
524 return -EIO;
525 }
526
527 return 0;
528}
529
437static int do_config_autodelink(struct us_data *us, int enable, int force) 530static int do_config_autodelink(struct us_data *us, int enable, int force)
438{ 531{
439 int retval; 532 int retval;
@@ -454,7 +547,8 @@ static int do_config_autodelink(struct us_data *us, int enable, int force)
454 547
455 US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value); 548 US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
456 549
457 retval = rts51x_write_mem(us, 0xFE47, &value, 1); 550 /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
551 retval = __do_config_autodelink(us, &value, 1);
458 if (retval < 0) 552 if (retval < 0)
459 return -EIO; 553 return -EIO;
460 554
@@ -486,7 +580,8 @@ static int config_autodelink_after_power_on(struct us_data *us)
486 580
487 SET_BIT(value, 7); 581 SET_BIT(value, 7);
488 582
489 retval = rts51x_write_mem(us, 0xFE47, &value, 1); 583 /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
584 retval = __do_config_autodelink(us, &value, 1);
490 if (retval < 0) 585 if (retval < 0)
491 return -EIO; 586 return -EIO;
492 587
@@ -507,7 +602,8 @@ static int config_autodelink_after_power_on(struct us_data *us)
507 CLR_BIT(value, 7); 602 CLR_BIT(value, 7);
508 } 603 }
509 604
510 retval = rts51x_write_mem(us, 0xFE47, &value, 1); 605 /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
606 retval = __do_config_autodelink(us, &value, 1);
511 if (retval < 0) 607 if (retval < 0)
512 return -EIO; 608 return -EIO;
513 609
@@ -584,7 +680,8 @@ static int config_autodelink_before_power_down(struct us_data *us)
584 if (CHECK_ID(chip, 0x0138, 0x3882)) 680 if (CHECK_ID(chip, 0x0138, 0x3882))
585 SET_BIT(value, 2); 681 SET_BIT(value, 2);
586 682
587 retval = rts51x_write_mem(us, 0xFE47, &value, 1); 683 /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
684 retval = __do_config_autodelink(us, &value, 1);
588 if (retval < 0) 685 if (retval < 0)
589 return -EIO; 686 return -EIO;
590 } 687 }
diff --git a/drivers/usb/storage/sierra_ms.c b/drivers/usb/storage/sierra_ms.c
index 1deca07c8265..37539c89e3ba 100644
--- a/drivers/usb/storage/sierra_ms.c
+++ b/drivers/usb/storage/sierra_ms.c
@@ -3,6 +3,7 @@
3#include <scsi/scsi_cmnd.h> 3#include <scsi/scsi_cmnd.h>
4#include <scsi/scsi_device.h> 4#include <scsi/scsi_device.h>
5#include <linux/usb.h> 5#include <linux/usb.h>
6#include <linux/module.h>
6#include <linux/slab.h> 7#include <linux/slab.h>
7 8
8#include "usb.h" 9#include "usb.h"
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index e8ae21b2d387..0e5c91c6187f 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -46,6 +46,7 @@
46#include <linux/sched.h> 46#include <linux/sched.h>
47#include <linux/gfp.h> 47#include <linux/gfp.h>
48#include <linux/errno.h> 48#include <linux/errno.h>
49#include <linux/export.h>
49 50
50#include <linux/usb/quirks.h> 51#include <linux/usb/quirks.h>
51 52
@@ -691,6 +692,9 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
691 int temp_result; 692 int temp_result;
692 struct scsi_eh_save ses; 693 struct scsi_eh_save ses;
693 int sense_size = US_SENSE_SIZE; 694 int sense_size = US_SENSE_SIZE;
695 struct scsi_sense_hdr sshdr;
696 const u8 *scdd;
697 u8 fm_ili;
694 698
695 /* device supports and needs bigger sense buffer */ 699 /* device supports and needs bigger sense buffer */
696 if (us->fflags & US_FL_SANE_SENSE) 700 if (us->fflags & US_FL_SANE_SENSE)
@@ -774,32 +778,30 @@ Retry_Sense:
774 srb->sense_buffer[7] = (US_SENSE_SIZE - 8); 778 srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
775 } 779 }
776 780
781 scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
782 &sshdr);
783
777 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result); 784 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
778 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n", 785 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
779 srb->sense_buffer[0], 786 sshdr.response_code, sshdr.sense_key,
780 srb->sense_buffer[2] & 0xf, 787 sshdr.asc, sshdr.ascq);
781 srb->sense_buffer[12],
782 srb->sense_buffer[13]);
783#ifdef CONFIG_USB_STORAGE_DEBUG 788#ifdef CONFIG_USB_STORAGE_DEBUG
784 usb_stor_show_sense( 789 usb_stor_show_sense(sshdr.sense_key, sshdr.asc, sshdr.ascq);
785 srb->sense_buffer[2] & 0xf,
786 srb->sense_buffer[12],
787 srb->sense_buffer[13]);
788#endif 790#endif
789 791
790 /* set the result so the higher layers expect this data */ 792 /* set the result so the higher layers expect this data */
791 srb->result = SAM_STAT_CHECK_CONDITION; 793 srb->result = SAM_STAT_CHECK_CONDITION;
792 794
795 scdd = scsi_sense_desc_find(srb->sense_buffer,
796 SCSI_SENSE_BUFFERSIZE, 4);
797 fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
798
793 /* We often get empty sense data. This could indicate that 799 /* We often get empty sense data. This could indicate that
794 * everything worked or that there was an unspecified 800 * everything worked or that there was an unspecified
795 * problem. We have to decide which. 801 * problem. We have to decide which.
796 */ 802 */
797 if ( /* Filemark 0, ignore EOM, ILI 0, no sense */ 803 if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
798 (srb->sense_buffer[2] & 0xaf) == 0 && 804 fm_ili == 0) {
799 /* No ASC or ASCQ */
800 srb->sense_buffer[12] == 0 &&
801 srb->sense_buffer[13] == 0) {
802
803 /* If things are really okay, then let's show that. 805 /* If things are really okay, then let's show that.
804 * Zero out the sense buffer so the higher layers 806 * Zero out the sense buffer so the higher layers
805 * won't realize we did an unsolicited auto-sense. 807 * won't realize we did an unsolicited auto-sense.
@@ -814,7 +816,10 @@ Retry_Sense:
814 */ 816 */
815 } else { 817 } else {
816 srb->result = DID_ERROR << 16; 818 srb->result = DID_ERROR << 16;
817 srb->sense_buffer[2] = HARDWARE_ERROR; 819 if ((sshdr.response_code & 0x72) == 0x72)
820 srb->sense_buffer[1] = HARDWARE_ERROR;
821 else
822 srb->sense_buffer[2] = HARDWARE_ERROR;
818 } 823 }
819 } 824 }
820 } 825 }
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 23f0dd9c36d4..1d10d5b8204c 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -11,6 +11,7 @@
11#include <linux/blkdev.h> 11#include <linux/blkdev.h>
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/types.h> 13#include <linux/types.h>
14#include <linux/module.h>
14#include <linux/usb.h> 15#include <linux/usb.h>
15#include <linux/usb/storage.h> 16#include <linux/usb/storage.h>
16 17
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 0ca095820f3e..c325e69415a1 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -831,12 +831,22 @@ static int usb_stor_scan_thread(void * __us)
831 831
832 dev_dbg(dev, "device found\n"); 832 dev_dbg(dev, "device found\n");
833 833
834 set_freezable(); 834 set_freezable_with_signal();
835 /* Wait for the timeout to expire or for a disconnect */ 835 /*
836 * Wait for the timeout to expire or for a disconnect
837 *
838 * We can't freeze in this thread or we risk causing khubd to
839 * fail to freeze, but we can't be non-freezable either. Nor can
840 * khubd freeze while waiting for scanning to complete as it may
841 * hold the device lock, causing a hang when suspending devices.
842 * So we request a fake signal when freezing and use
843 * interruptible sleep to kick us out of our wait early when
844 * freezing happens.
845 */
836 if (delay_use > 0) { 846 if (delay_use > 0) {
837 dev_dbg(dev, "waiting for device to settle " 847 dev_dbg(dev, "waiting for device to settle "
838 "before scanning\n"); 848 "before scanning\n");
839 wait_event_freezable_timeout(us->delay_wait, 849 wait_event_interruptible_timeout(us->delay_wait,
840 test_bit(US_FLIDX_DONT_SCAN, &us->dflags), 850 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
841 delay_use * HZ); 851 delay_use * HZ);
842 } 852 }
diff --git a/drivers/usb/usb-common.c b/drivers/usb/usb-common.c
new file mode 100644
index 000000000000..d29503e954ab
--- /dev/null
+++ b/drivers/usb/usb-common.c
@@ -0,0 +1,35 @@
1/*
2 * Provides code common for host and device side USB.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2.
7 *
8 * If either host side (ie. CONFIG_USB=y) or device side USB stack
9 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
10 * compiled-in as well. Otherwise, if either of the two stacks is
11 * compiled as module, this file is compiled as module as well.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/usb/ch9.h>
17
18const char *usb_speed_string(enum usb_device_speed speed)
19{
20 static const char *const names[] = {
21 [USB_SPEED_UNKNOWN] = "UNKNOWN",
22 [USB_SPEED_LOW] = "low-speed",
23 [USB_SPEED_FULL] = "full-speed",
24 [USB_SPEED_HIGH] = "high-speed",
25 [USB_SPEED_WIRELESS] = "wireless",
26 [USB_SPEED_SUPER] = "super-speed",
27 };
28
29 if (speed < 0 || speed >= ARRAY_SIZE(names))
30 speed = USB_SPEED_UNKNOWN;
31 return names[speed];
32}
33EXPORT_SYMBOL_GPL(usb_speed_string);
34
35MODULE_LICENSE("GPL");
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index e24ce3123071..32d6fc953904 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -555,7 +555,7 @@ static int skel_probe(struct usb_interface *interface,
555 if (!dev->bulk_in_endpointAddr && 555 if (!dev->bulk_in_endpointAddr &&
556 usb_endpoint_is_bulk_in(endpoint)) { 556 usb_endpoint_is_bulk_in(endpoint)) {
557 /* we found a bulk in endpoint */ 557 /* we found a bulk in endpoint */
558 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 558 buffer_size = usb_endpoint_maxp(endpoint);
559 dev->bulk_in_size = buffer_size; 559 dev->bulk_in_size = buffer_size;
560 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; 560 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
561 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); 561 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
diff --git a/drivers/usb/wusbcore/devconnect.c b/drivers/usb/wusbcore/devconnect.c
index 7ec24e46b34b..231009af65a3 100644
--- a/drivers/usb/wusbcore/devconnect.c
+++ b/drivers/usb/wusbcore/devconnect.c
@@ -90,6 +90,7 @@
90#include <linux/ctype.h> 90#include <linux/ctype.h>
91#include <linux/slab.h> 91#include <linux/slab.h>
92#include <linux/workqueue.h> 92#include <linux/workqueue.h>
93#include <linux/export.h>
93#include "wusbhc.h" 94#include "wusbhc.h"
94 95
95static void wusbhc_devconnect_acked_work(struct work_struct *work); 96static void wusbhc_devconnect_acked_work(struct work_struct *work);
diff --git a/drivers/usb/wusbcore/mmc.c b/drivers/usb/wusbcore/mmc.c
index 0a57ff0a0b0c..b8c72583c040 100644
--- a/drivers/usb/wusbcore/mmc.c
+++ b/drivers/usb/wusbcore/mmc.c
@@ -38,6 +38,7 @@
38 */ 38 */
39#include <linux/usb/wusb.h> 39#include <linux/usb/wusb.h>
40#include <linux/slab.h> 40#include <linux/slab.h>
41#include <linux/export.h>
41#include "wusbhc.h" 42#include "wusbhc.h"
42 43
43/* Initialize the MMCIEs handling mechanism */ 44/* Initialize the MMCIEs handling mechanism */
diff --git a/drivers/usb/wusbcore/rh.c b/drivers/usb/wusbcore/rh.c
index 39de3900ad20..59ff254dfb6f 100644
--- a/drivers/usb/wusbcore/rh.c
+++ b/drivers/usb/wusbcore/rh.c
@@ -70,6 +70,7 @@
70 * wusbhc_rh_start_port_reset() ??? unimplemented 70 * wusbhc_rh_start_port_reset() ??? unimplemented
71 */ 71 */
72#include <linux/slab.h> 72#include <linux/slab.h>
73#include <linux/export.h>
73#include "wusbhc.h" 74#include "wusbhc.h"
74 75
75/* 76/*
diff --git a/drivers/usb/wusbcore/security.c b/drivers/usb/wusbcore/security.c
index b60799b811c1..371f61733f05 100644
--- a/drivers/usb/wusbcore/security.c
+++ b/drivers/usb/wusbcore/security.c
@@ -26,6 +26,7 @@
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/usb/ch9.h> 27#include <linux/usb/ch9.h>
28#include <linux/random.h> 28#include <linux/random.h>
29#include <linux/export.h>
29#include "wusbhc.h" 30#include "wusbhc.h"
30 31
31static void wusbhc_set_gtk_callback(struct urb *urb); 32static void wusbhc_set_gtk_callback(struct urb *urb);
diff --git a/drivers/usb/wusbcore/wa-hc.c b/drivers/usb/wusbcore/wa-hc.c
index 59a748a0e5da..9e4a92461688 100644
--- a/drivers/usb/wusbcore/wa-hc.c
+++ b/drivers/usb/wusbcore/wa-hc.c
@@ -23,6 +23,7 @@
23 * FIXME: docs 23 * FIXME: docs
24 */ 24 */
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/module.h>
26#include "wusbhc.h" 27#include "wusbhc.h"
27#include "wa-hc.h" 28#include "wa-hc.h"
28 29
@@ -43,7 +44,7 @@ int wa_create(struct wahc *wa, struct usb_interface *iface)
43 /* Fill up Data Transfer EP pointers */ 44 /* Fill up Data Transfer EP pointers */
44 wa->dti_epd = &iface->cur_altsetting->endpoint[1].desc; 45 wa->dti_epd = &iface->cur_altsetting->endpoint[1].desc;
45 wa->dto_epd = &iface->cur_altsetting->endpoint[2].desc; 46 wa->dto_epd = &iface->cur_altsetting->endpoint[2].desc;
46 wa->xfer_result_size = le16_to_cpu(wa->dti_epd->wMaxPacketSize); 47 wa->xfer_result_size = usb_endpoint_maxp(wa->dti_epd);
47 wa->xfer_result = kmalloc(wa->xfer_result_size, GFP_KERNEL); 48 wa->xfer_result = kmalloc(wa->xfer_result_size, GFP_KERNEL);
48 if (wa->xfer_result == NULL) 49 if (wa->xfer_result == NULL)
49 goto error_xfer_result_alloc; 50 goto error_xfer_result_alloc;
diff --git a/drivers/usb/wusbcore/wa-rpipe.c b/drivers/usb/wusbcore/wa-rpipe.c
index 2acc7f504c51..f0d546c5a089 100644
--- a/drivers/usb/wusbcore/wa-rpipe.c
+++ b/drivers/usb/wusbcore/wa-rpipe.c
@@ -61,6 +61,7 @@
61#include <linux/atomic.h> 61#include <linux/atomic.h>
62#include <linux/bitmap.h> 62#include <linux/bitmap.h>
63#include <linux/slab.h> 63#include <linux/slab.h>
64#include <linux/export.h>
64 65
65#include "wusbhc.h" 66#include "wusbhc.h"
66#include "wa-hc.h" 67#include "wa-hc.h"
diff --git a/drivers/usb/wusbcore/wa-xfer.c b/drivers/usb/wusbcore/wa-xfer.c
index 419334568be6..57c01ab09ad8 100644
--- a/drivers/usb/wusbcore/wa-xfer.c
+++ b/drivers/usb/wusbcore/wa-xfer.c
@@ -84,6 +84,7 @@
84#include <linux/slab.h> 84#include <linux/slab.h>
85#include <linux/hash.h> 85#include <linux/hash.h>
86#include <linux/ratelimit.h> 86#include <linux/ratelimit.h>
87#include <linux/export.h>
87 88
88#include "wa-hc.h" 89#include "wa-hc.h"
89#include "wusbhc.h" 90#include "wusbhc.h"