aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig158
-rw-r--r--drivers/usb/gadget/Makefile52
-rw-r--r--drivers/usb/gadget/amd5536udc.c31
-rw-r--r--drivers/usb/gadget/amd5536udc.h2
-rw-r--r--drivers/usb/gadget/at91_udc.c21
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c16
-rw-r--r--drivers/usb/gadget/audio.c10
-rw-r--r--drivers/usb/gadget/cdc2.c10
-rw-r--r--drivers/usb/gadget/ci13xxx_msm.c134
-rw-r--r--drivers/usb/gadget/ci13xxx_pci.c176
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.c1048
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.h38
-rw-r--r--drivers/usb/gadget/composite.c238
-rw-r--r--drivers/usb/gadget/dbgp.c31
-rw-r--r--drivers/usb/gadget/dummy_hcd.c295
-rw-r--r--drivers/usb/gadget/epautoconf.c7
-rw-r--r--drivers/usb/gadget/ether.c16
-rw-r--r--drivers/usb/gadget/f_acm.c2
-rw-r--r--drivers/usb/gadget/f_audio.c7
-rw-r--r--drivers/usb/gadget/f_eem.c8
-rw-r--r--drivers/usb/gadget/f_fs.c461
-rw-r--r--drivers/usb/gadget/f_hid.c6
-rw-r--r--drivers/usb/gadget/f_loopback.c7
-rw-r--r--drivers/usb/gadget/f_mass_storage.c673
-rw-r--r--drivers/usb/gadget/f_ncm.c1407
-rw-r--r--drivers/usb/gadget/f_phonet.c15
-rw-r--r--drivers/usb/gadget/f_rndis.c3
-rw-r--r--drivers/usb/gadget/f_sourcesink.c5
-rw-r--r--drivers/usb/gadget/file_storage.c212
-rw-r--r--drivers/usb/gadget/fsl_mxc_udc.c18
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.c47
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.h8
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c476
-rw-r--r--drivers/usb/gadget/fsl_usb2_udc.h10
-rw-r--r--drivers/usb/gadget/fusb300_udc.c1744
-rw-r--r--drivers/usb/gadget/fusb300_udc.h687
-rw-r--r--drivers/usb/gadget/g_ffs.c132
-rw-r--r--drivers/usb/gadget/gadget_chips.h50
-rw-r--r--drivers/usb/gadget/gmidi.c7
-rw-r--r--drivers/usb/gadget/goku_udc.c36
-rw-r--r--drivers/usb/gadget/goku_udc.h3
-rw-r--r--drivers/usb/gadget/hid.c10
-rw-r--r--drivers/usb/gadget/imx_udc.c20
-rw-r--r--drivers/usb/gadget/imx_udc.h3
-rw-r--r--drivers/usb/gadget/inode.c26
-rw-r--r--drivers/usb/gadget/langwell_udc.c1085
-rw-r--r--drivers/usb/gadget/langwell_udc.h15
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.c2152
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.h259
-rw-r--r--drivers/usb/gadget/m66592-udc.c11
-rw-r--r--drivers/usb/gadget/mass_storage.c84
-rw-r--r--drivers/usb/gadget/multi.c36
-rw-r--r--drivers/usb/gadget/mv_udc.h294
-rw-r--r--drivers/usb/gadget/mv_udc_core.c2149
-rw-r--r--drivers/usb/gadget/mv_udc_phy.c214
-rw-r--r--drivers/usb/gadget/ncm.c248
-rw-r--r--drivers/usb/gadget/net2280.c13
-rw-r--r--drivers/usb/gadget/nokia.c13
-rw-r--r--drivers/usb/gadget/omap_udc.c29
-rw-r--r--drivers/usb/gadget/pch_udc.c3004
-rw-r--r--drivers/usb/gadget/printer.c33
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c91
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c27
-rw-r--r--drivers/usb/gadget/r8a66597-udc.c18
-rw-r--r--drivers/usb/gadget/r8a66597-udc.h2
-rw-r--r--drivers/usb/gadget/rndis.c494
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c284
-rw-r--r--drivers/usb/gadget/s3c-hsudc.c1352
-rw-r--r--drivers/usb/gadget/s3c2410_udc.c94
-rw-r--r--drivers/usb/gadget/serial.c11
-rw-r--r--drivers/usb/gadget/storage_common.c74
-rw-r--r--drivers/usb/gadget/u_audio.c10
-rw-r--r--drivers/usb/gadget/u_ether.c19
-rw-r--r--drivers/usb/gadget/u_ether.h5
-rw-r--r--drivers/usb/gadget/u_serial.c54
-rw-r--r--drivers/usb/gadget/webcam.c11
-rw-r--r--drivers/usb/gadget/zero.c5
77 files changed, 15244 insertions, 5312 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index cd27f9bde2c8..029e288805b6 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -158,6 +158,7 @@ config USB_GADGET_FSL_USB2
158 boolean "Freescale Highspeed USB DR Peripheral Controller" 158 boolean "Freescale Highspeed USB DR Peripheral Controller"
159 depends on FSL_SOC || ARCH_MXC 159 depends on FSL_SOC || ARCH_MXC
160 select USB_GADGET_DUALSPEED 160 select USB_GADGET_DUALSPEED
161 select USB_FSL_MPH_DR_OF if OF
161 help 162 help
162 Some of Freescale PowerPC processors have a High Speed 163 Some of Freescale PowerPC processors have a High Speed
163 Dual-Role(DR) USB controller, which supports device mode. 164 Dual-Role(DR) USB controller, which supports device mode.
@@ -175,15 +176,15 @@ config USB_FSL_USB2
175 default USB_GADGET 176 default USB_GADGET
176 select USB_GADGET_SELECTED 177 select USB_GADGET_SELECTED
177 178
178config USB_GADGET_LH7A40X 179config USB_GADGET_FUSB300
179 boolean "LH7A40X" 180 boolean "Faraday FUSB300 USB Peripheral Controller"
180 depends on ARCH_LH7A40X 181 select USB_GADGET_DUALSPEED
181 help 182 help
182 This driver provides USB Device Controller driver for LH7A40x 183 Faraday usb device controller FUSB300 driver
183 184
184config USB_LH7A40X 185config USB_FUSB300
185 tristate 186 tristate
186 depends on USB_GADGET_LH7A40X 187 depends on USB_GADGET_FUSB300
187 default USB_GADGET 188 default USB_GADGET
188 select USB_GADGET_SELECTED 189 select USB_GADGET_SELECTED
189 190
@@ -209,17 +210,6 @@ config USB_OMAP
209 default USB_GADGET 210 default USB_GADGET
210 select USB_GADGET_SELECTED 211 select USB_GADGET_SELECTED
211 212
212config USB_OTG
213 boolean "OTG Support"
214 depends on USB_GADGET_OMAP && ARCH_OMAP_OTG && USB_OHCI_HCD
215 help
216 The most notable feature of USB OTG is support for a
217 "Dual-Role" device, which can act as either a device
218 or a host. The initial role choice can be changed
219 later, when two dual-role devices talk to each other.
220
221 Select this only if your OMAP board has a Mini-AB connector.
222
223config USB_GADGET_PXA25X 213config USB_GADGET_PXA25X
224 boolean "PXA 25x or IXP 4xx" 214 boolean "PXA 25x or IXP 4xx"
225 depends on (ARCH_PXA && PXA25x) || ARCH_IXP4XX 215 depends on (ARCH_PXA && PXA25x) || ARCH_IXP4XX
@@ -270,6 +260,24 @@ config USB_R8A66597
270 default USB_GADGET 260 default USB_GADGET
271 select USB_GADGET_SELECTED 261 select USB_GADGET_SELECTED
272 262
263config USB_GADGET_RENESAS_USBHS
264 boolean "Renesas USBHS"
265 depends on USB_RENESAS_USBHS
266 select USB_GADGET_DUALSPEED
267 help
268 Renesas USBHS is a discrete USB host and peripheral controller
269 chip that supports both full and high speed USB 2.0 data transfers.
270 platform is able to configure endpoint (pipe) style
271
272 Say "y" to enable the gadget specific portion of the USBHS driver.
273
274
275config USB_RENESAS_USBHS_UDC
276 tristate
277 depends on USB_GADGET_RENESAS_USBHS
278 default USB_GADGET
279 select USB_GADGET_SELECTED
280
273config USB_GADGET_PXA27X 281config USB_GADGET_PXA27X
274 boolean "PXA 27x" 282 boolean "PXA 27x"
275 depends on ARCH_PXA && (PXA27x || PXA3xx) 283 depends on ARCH_PXA && (PXA27x || PXA3xx)
@@ -348,6 +356,36 @@ config USB_S3C2410_DEBUG
348 boolean "S3C2410 udc debug messages" 356 boolean "S3C2410 udc debug messages"
349 depends on USB_GADGET_S3C2410 357 depends on USB_GADGET_S3C2410
350 358
359config USB_GADGET_S3C_HSUDC
360 boolean "S3C2416, S3C2443 and S3C2450 USB Device Controller"
361 depends on ARCH_S3C2410
362 select USB_GADGET_DUALSPEED
363 help
364 Samsung's S3C2416, S3C2443 and S3C2450 is an ARM9 based SoC
365 integrated with dual speed USB 2.0 device controller. It has
366 8 endpoints, as well as endpoint zero.
367
368 This driver has been tested on S3C2416 and S3C2450 processors.
369
370config USB_S3C_HSUDC
371 tristate
372 depends on USB_GADGET_S3C_HSUDC
373 default USB_GADGET
374 select USB_GADGET_SELECTED
375
376config USB_GADGET_PXA_U2O
377 boolean "PXA9xx Processor USB2.0 controller"
378 select USB_GADGET_DUALSPEED
379 help
380 PXA9xx Processor series include a high speed USB2.0 device
381 controller, which support high speed and full speed USB peripheral.
382
383config USB_PXA_U2O
384 tristate
385 depends on USB_GADGET_PXA_U2O
386 default USB_GADGET
387 select USB_GADGET_SELECTED
388
351# 389#
352# Controllers available in both integrated and discrete versions 390# Controllers available in both integrated and discrete versions
353# 391#
@@ -424,8 +462,8 @@ config USB_FSL_QE
424 default USB_GADGET 462 default USB_GADGET
425 select USB_GADGET_SELECTED 463 select USB_GADGET_SELECTED
426 464
427config USB_GADGET_CI13XXX 465config USB_GADGET_CI13XXX_PCI
428 boolean "MIPS USB CI13xxx" 466 boolean "MIPS USB CI13xxx PCI UDC"
429 depends on PCI 467 depends on PCI
430 select USB_GADGET_DUALSPEED 468 select USB_GADGET_DUALSPEED
431 help 469 help
@@ -436,9 +474,9 @@ config USB_GADGET_CI13XXX
436 dynamically linked module called "ci13xxx_udc" and force all 474 dynamically linked module called "ci13xxx_udc" and force all
437 gadget drivers to also be dynamically linked. 475 gadget drivers to also be dynamically linked.
438 476
439config USB_CI13XXX 477config USB_CI13XXX_PCI
440 tristate 478 tristate
441 depends on USB_GADGET_CI13XXX 479 depends on USB_GADGET_CI13XXX_PCI
442 default USB_GADGET 480 default USB_GADGET
443 select USB_GADGET_SELECTED 481 select USB_GADGET_SELECTED
444 482
@@ -505,6 +543,56 @@ config USB_LANGWELL
505 default USB_GADGET 543 default USB_GADGET
506 select USB_GADGET_SELECTED 544 select USB_GADGET_SELECTED
507 545
546config USB_GADGET_EG20T
547 boolean "Intel EG20T PCH/OKI SEMICONDUCTOR ML7213 IOH UDC"
548 depends on PCI
549 select USB_GADGET_DUALSPEED
550 help
551 This is a USB device driver for EG20T PCH.
552 EG20T PCH is the platform controller hub that is used in Intel's
553 general embedded platform. EG20T PCH has USB device interface.
554 Using this interface, it is able to access system devices connected
555 to USB device.
556 This driver enables USB device function.
557 USB device is a USB peripheral controller which
558 supports both full and high speed USB 2.0 data transfers.
559 This driver supports both control transfer and bulk transfer modes.
560 This driver dose not support interrupt transfer or isochronous
561 transfer modes.
562
563 This driver also can be used for OKI SEMICONDUCTOR's ML7213 which is
564 for IVI(In-Vehicle Infotainment) use.
565 ML7213 is companion chip for Intel Atom E6xx series.
566 ML7213 is completely compatible for Intel EG20T PCH.
567
568config USB_EG20T
569 tristate
570 depends on USB_GADGET_EG20T
571 default USB_GADGET
572 select USB_GADGET_SELECTED
573
574config USB_GADGET_CI13XXX_MSM
575 boolean "MIPS USB CI13xxx for MSM"
576 depends on ARCH_MSM
577 select USB_GADGET_DUALSPEED
578 select USB_MSM_OTG
579 help
580 MSM SoC has chipidea USB controller. This driver uses
581 ci13xxx_udc core.
582 This driver depends on OTG driver for PHY initialization,
583 clock management, powering up VBUS, and power management.
584 This driver is not supported on boards like trout which
585 has an external PHY.
586
587 Say "y" to link the driver statically, or "m" to build a
588 dynamically linked module called "ci13xxx_msm" and force all
589 gadget drivers to also be dynamically linked.
590
591config USB_CI13XXX_MSM
592 tristate
593 depends on USB_GADGET_CI13XXX_MSM
594 default USB_GADGET
595 select USB_GADGET_SELECTED
508 596
509# 597#
510# LAST -- dummy/emulated controller 598# LAST -- dummy/emulated controller
@@ -544,13 +632,10 @@ config USB_DUMMY_HCD
544 632
545endchoice 633endchoice
546 634
635# Selected by UDC drivers that support high-speed operation.
547config USB_GADGET_DUALSPEED 636config USB_GADGET_DUALSPEED
548 bool 637 bool
549 depends on USB_GADGET 638 depends on USB_GADGET
550 default n
551 help
552 Means that gadget drivers should include extra descriptors
553 and code to handle dual-speed controllers.
554 639
555# 640#
556# USB Gadget Drivers 641# USB Gadget Drivers
@@ -695,6 +780,19 @@ config USB_ETH_EEM
695 If you say "y" here, the Ethernet gadget driver will use the EEM 780 If you say "y" here, the Ethernet gadget driver will use the EEM
696 protocol rather than ECM. If unsure, say "n". 781 protocol rather than ECM. If unsure, say "n".
697 782
783config USB_G_NCM
784 tristate "Network Control Model (NCM) support"
785 depends on NET
786 select CRC32
787 help
788 This driver implements USB CDC NCM subclass standard. NCM is
789 an advanced protocol for Ethernet encapsulation, allows grouping
790 of several ethernet frames into one USB transfer and diffferent
791 alignment possibilities.
792
793 Say "y" to link the driver statically, or "m" to build a
794 dynamically linked module called "g_ncm".
795
698config USB_GADGETFS 796config USB_GADGETFS
699 tristate "Gadget Filesystem (EXPERIMENTAL)" 797 tristate "Gadget Filesystem (EXPERIMENTAL)"
700 depends on EXPERIMENTAL 798 depends on EXPERIMENTAL
@@ -716,8 +814,8 @@ config USB_FUNCTIONFS
716 depends on EXPERIMENTAL 814 depends on EXPERIMENTAL
717 select USB_FUNCTIONFS_GENERIC if !(USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS) 815 select USB_FUNCTIONFS_GENERIC if !(USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS)
718 help 816 help
719 The Function Filesystem (FunctioFS) lets one create USB 817 The Function Filesystem (FunctionFS) lets one create USB
720 composite functions in user space in the same way as GadgetFS 818 composite functions in user space in the same way GadgetFS
721 lets one create USB gadgets in user space. This allows creation 819 lets one create USB gadgets in user space. This allows creation
722 of composite gadgets such that some of the functions are 820 of composite gadgets such that some of the functions are
723 implemented in kernel space (for instance Ethernet, serial or 821 implemented in kernel space (for instance Ethernet, serial or
@@ -733,14 +831,14 @@ config USB_FUNCTIONFS_ETH
733 bool "Include configuration with CDC ECM (Ethernet)" 831 bool "Include configuration with CDC ECM (Ethernet)"
734 depends on USB_FUNCTIONFS && NET 832 depends on USB_FUNCTIONFS && NET
735 help 833 help
736 Include a configuration with CDC ECM funcion (Ethernet) and the 834 Include a configuration with CDC ECM function (Ethernet) and the
737 Funcion Filesystem. 835 Function Filesystem.
738 836
739config USB_FUNCTIONFS_RNDIS 837config USB_FUNCTIONFS_RNDIS
740 bool "Include configuration with RNDIS (Ethernet)" 838 bool "Include configuration with RNDIS (Ethernet)"
741 depends on USB_FUNCTIONFS && NET 839 depends on USB_FUNCTIONFS && NET
742 help 840 help
743 Include a configuration with RNDIS funcion (Ethernet) and the Filesystem. 841 Include a configuration with RNDIS function (Ethernet) and the Filesystem.
744 842
745config USB_FUNCTIONFS_GENERIC 843config USB_FUNCTIONFS_GENERIC
746 bool "Include 'pure' configuration" 844 bool "Include 'pure' configuration"
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 27283df37d09..4fe92b18a055 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -1,9 +1,7 @@
1# 1#
2# USB peripheral controller drivers 2# USB peripheral controller drivers
3# 3#
4ifeq ($(CONFIG_USB_GADGET_DEBUG),y) 4ccflags-$(CONFIG_USB_GADGET_DEBUG) := -DDEBUG
5 EXTRA_CFLAGS += -DDEBUG
6endif
7 5
8obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o 6obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o
9obj-$(CONFIG_USB_NET2280) += net2280.o 7obj-$(CONFIG_USB_NET2280) += net2280.o
@@ -13,40 +11,44 @@ obj-$(CONFIG_USB_PXA27X) += pxa27x_udc.o
13obj-$(CONFIG_USB_IMX) += imx_udc.o 11obj-$(CONFIG_USB_IMX) += imx_udc.o
14obj-$(CONFIG_USB_GOKU) += goku_udc.o 12obj-$(CONFIG_USB_GOKU) += goku_udc.o
15obj-$(CONFIG_USB_OMAP) += omap_udc.o 13obj-$(CONFIG_USB_OMAP) += omap_udc.o
16obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
17obj-$(CONFIG_USB_S3C2410) += s3c2410_udc.o 14obj-$(CONFIG_USB_S3C2410) += s3c2410_udc.o
18obj-$(CONFIG_USB_AT91) += at91_udc.o 15obj-$(CONFIG_USB_AT91) += at91_udc.o
19obj-$(CONFIG_USB_ATMEL_USBA) += atmel_usba_udc.o 16obj-$(CONFIG_USB_ATMEL_USBA) += atmel_usba_udc.o
20obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o 17obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o
21fsl_usb2_udc-objs := fsl_udc_core.o 18fsl_usb2_udc-y := fsl_udc_core.o
22ifeq ($(CONFIG_ARCH_MXC),y) 19fsl_usb2_udc-$(CONFIG_ARCH_MXC) += fsl_mxc_udc.o
23fsl_usb2_udc-objs += fsl_mxc_udc.o
24endif
25obj-$(CONFIG_USB_M66592) += m66592-udc.o 20obj-$(CONFIG_USB_M66592) += m66592-udc.o
26obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o 21obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o
27obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o 22obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o
28obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o 23obj-$(CONFIG_USB_CI13XXX_PCI) += ci13xxx_pci.o
29obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o 24obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o
25obj-$(CONFIG_USB_S3C_HSUDC) += s3c-hsudc.o
30obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o 26obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o
27obj-$(CONFIG_USB_EG20T) += pch_udc.o
28obj-$(CONFIG_USB_PXA_U2O) += mv_udc.o
29mv_udc-y := mv_udc_core.o mv_udc_phy.o
30obj-$(CONFIG_USB_CI13XXX_MSM) += ci13xxx_msm.o
31obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
31 32
32# 33#
33# USB gadget drivers 34# USB gadget drivers
34# 35#
35g_zero-objs := zero.o 36g_zero-y := zero.o
36g_audio-objs := audio.o 37g_audio-y := audio.o
37g_ether-objs := ether.o 38g_ether-y := ether.o
38g_serial-objs := serial.o 39g_serial-y := serial.o
39g_midi-objs := gmidi.o 40g_midi-y := gmidi.o
40gadgetfs-objs := inode.o 41gadgetfs-y := inode.o
41g_file_storage-objs := file_storage.o 42g_file_storage-y := file_storage.o
42g_mass_storage-objs := mass_storage.o 43g_mass_storage-y := mass_storage.o
43g_printer-objs := printer.o 44g_printer-y := printer.o
44g_cdc-objs := cdc2.o 45g_cdc-y := cdc2.o
45g_multi-objs := multi.o 46g_multi-y := multi.o
46g_hid-objs := hid.o 47g_hid-y := hid.o
47g_dbgp-objs := dbgp.o 48g_dbgp-y := dbgp.o
48g_nokia-objs := nokia.o 49g_nokia-y := nokia.o
49g_webcam-objs := webcam.o 50g_webcam-y := webcam.o
51g_ncm-y := ncm.o
50 52
51obj-$(CONFIG_USB_ZERO) += g_zero.o 53obj-$(CONFIG_USB_ZERO) += g_zero.o
52obj-$(CONFIG_USB_AUDIO) += g_audio.o 54obj-$(CONFIG_USB_AUDIO) += g_audio.o
@@ -64,4 +66,4 @@ obj-$(CONFIG_USB_G_DBGP) += g_dbgp.o
64obj-$(CONFIG_USB_G_MULTI) += g_multi.o 66obj-$(CONFIG_USB_G_MULTI) += g_multi.o
65obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o 67obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
66obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o 68obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
67 69obj-$(CONFIG_USB_G_NCM) += g_ncm.o
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index 731150d4b1d9..95e8138cd48f 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -60,6 +60,7 @@
60#include <linux/device.h> 60#include <linux/device.h>
61#include <linux/io.h> 61#include <linux/io.h>
62#include <linux/irq.h> 62#include <linux/irq.h>
63#include <linux/prefetch.h>
63 64
64#include <asm/byteorder.h> 65#include <asm/byteorder.h>
65#include <asm/system.h> 66#include <asm/system.h>
@@ -203,7 +204,7 @@ static void print_regs(struct udc *dev)
203 DBG(dev, "DMA mode = PPBNDU (packet per buffer " 204 DBG(dev, "DMA mode = PPBNDU (packet per buffer "
204 "WITHOUT desc. update)\n"); 205 "WITHOUT desc. update)\n");
205 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBNDU"); 206 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBNDU");
206 } else if (use_dma && use_dma_ppb_du && use_dma_ppb_du) { 207 } else if (use_dma && use_dma_ppb && use_dma_ppb_du) {
207 DBG(dev, "DMA mode = PPBDU (packet per buffer " 208 DBG(dev, "DMA mode = PPBDU (packet per buffer "
208 "WITH desc. update)\n"); 209 "WITH desc. update)\n");
209 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBDU"); 210 dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBDU");
@@ -278,7 +279,7 @@ static int udc_enable_dev_setup_interrupts(struct udc *dev)
278 return 0; 279 return 0;
279} 280}
280 281
281/* Calculates fifo start of endpoint based on preceeding endpoints */ 282/* Calculates fifo start of endpoint based on preceding endpoints */
282static int udc_set_txfifo_addr(struct udc_ep *ep) 283static int udc_set_txfifo_addr(struct udc_ep *ep)
283{ 284{
284 struct udc *dev; 285 struct udc *dev;
@@ -1954,13 +1955,14 @@ static int setup_ep0(struct udc *dev)
1954} 1955}
1955 1956
1956/* Called by gadget driver to register itself */ 1957/* Called by gadget driver to register itself */
1957int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1958int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1959 int (*bind)(struct usb_gadget *))
1958{ 1960{
1959 struct udc *dev = udc; 1961 struct udc *dev = udc;
1960 int retval; 1962 int retval;
1961 u32 tmp; 1963 u32 tmp;
1962 1964
1963 if (!driver || !driver->bind || !driver->setup 1965 if (!driver || !bind || !driver->setup
1964 || driver->speed != USB_SPEED_HIGH) 1966 || driver->speed != USB_SPEED_HIGH)
1965 return -EINVAL; 1967 return -EINVAL;
1966 if (!dev) 1968 if (!dev)
@@ -1972,7 +1974,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1972 dev->driver = driver; 1974 dev->driver = driver;
1973 dev->gadget.dev.driver = &driver->driver; 1975 dev->gadget.dev.driver = &driver->driver;
1974 1976
1975 retval = driver->bind(&dev->gadget); 1977 retval = bind(&dev->gadget);
1976 1978
1977 /* Some gadget drivers use both ep0 directions. 1979 /* Some gadget drivers use both ep0 directions.
1978 * NOTE: to gadget driver, ep0 is just one endpoint... 1980 * NOTE: to gadget driver, ep0 is just one endpoint...
@@ -2000,7 +2002,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2000 2002
2001 return 0; 2003 return 0;
2002} 2004}
2003EXPORT_SYMBOL(usb_gadget_register_driver); 2005EXPORT_SYMBOL(usb_gadget_probe_driver);
2004 2006
2005/* shutdown requests and disconnect from gadget */ 2007/* shutdown requests and disconnect from gadget */
2006static void 2008static void
@@ -2136,7 +2138,7 @@ static irqreturn_t udc_data_out_isr(struct udc *dev, int ep_ix)
2136 if (use_dma) { 2138 if (use_dma) {
2137 /* BNA event ? */ 2139 /* BNA event ? */
2138 if (tmp & AMD_BIT(UDC_EPSTS_BNA)) { 2140 if (tmp & AMD_BIT(UDC_EPSTS_BNA)) {
2139 DBG(dev, "BNA ep%dout occured - DESPTR = %x \n", 2141 DBG(dev, "BNA ep%dout occurred - DESPTR = %x \n",
2140 ep->num, readl(&ep->regs->desptr)); 2142 ep->num, readl(&ep->regs->desptr));
2141 /* clear BNA */ 2143 /* clear BNA */
2142 writel(tmp | AMD_BIT(UDC_EPSTS_BNA), &ep->regs->sts); 2144 writel(tmp | AMD_BIT(UDC_EPSTS_BNA), &ep->regs->sts);
@@ -2150,7 +2152,7 @@ static irqreturn_t udc_data_out_isr(struct udc *dev, int ep_ix)
2150 } 2152 }
2151 /* HE event ? */ 2153 /* HE event ? */
2152 if (tmp & AMD_BIT(UDC_EPSTS_HE)) { 2154 if (tmp & AMD_BIT(UDC_EPSTS_HE)) {
2153 dev_err(&dev->pdev->dev, "HE ep%dout occured\n", ep->num); 2155 dev_err(&dev->pdev->dev, "HE ep%dout occurred\n", ep->num);
2154 2156
2155 /* clear HE */ 2157 /* clear HE */
2156 writel(tmp | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts); 2158 writel(tmp | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts);
@@ -2353,7 +2355,7 @@ static irqreturn_t udc_data_in_isr(struct udc *dev, int ep_ix)
2353 /* BNA ? */ 2355 /* BNA ? */
2354 if (epsts & AMD_BIT(UDC_EPSTS_BNA)) { 2356 if (epsts & AMD_BIT(UDC_EPSTS_BNA)) {
2355 dev_err(&dev->pdev->dev, 2357 dev_err(&dev->pdev->dev,
2356 "BNA ep%din occured - DESPTR = %08lx \n", 2358 "BNA ep%din occurred - DESPTR = %08lx \n",
2357 ep->num, 2359 ep->num,
2358 (unsigned long) readl(&ep->regs->desptr)); 2360 (unsigned long) readl(&ep->regs->desptr));
2359 2361
@@ -2366,7 +2368,7 @@ static irqreturn_t udc_data_in_isr(struct udc *dev, int ep_ix)
2366 /* HE event ? */ 2368 /* HE event ? */
2367 if (epsts & AMD_BIT(UDC_EPSTS_HE)) { 2369 if (epsts & AMD_BIT(UDC_EPSTS_HE)) {
2368 dev_err(&dev->pdev->dev, 2370 dev_err(&dev->pdev->dev,
2369 "HE ep%dn occured - DESPTR = %08lx \n", 2371 "HE ep%dn occurred - DESPTR = %08lx \n",
2370 ep->num, (unsigned long) readl(&ep->regs->desptr)); 2372 ep->num, (unsigned long) readl(&ep->regs->desptr));
2371 2373
2372 /* clear HE */ 2374 /* clear HE */
@@ -2383,7 +2385,7 @@ static irqreturn_t udc_data_in_isr(struct udc *dev, int ep_ix)
2383 req = list_entry(ep->queue.next, 2385 req = list_entry(ep->queue.next,
2384 struct udc_request, queue); 2386 struct udc_request, queue);
2385 /* 2387 /*
2386 * length bytes transfered 2388 * length bytes transferred
2387 * check dma done of last desc. in PPBDU mode 2389 * check dma done of last desc. in PPBDU mode
2388 */ 2390 */
2389 if (use_dma_ppb_du) { 2391 if (use_dma_ppb_du) {
@@ -2783,7 +2785,7 @@ static irqreturn_t udc_control_in_isr(struct udc *dev)
2783 /* write fifo */ 2785 /* write fifo */
2784 udc_txfifo_write(ep, &req->req); 2786 udc_txfifo_write(ep, &req->req);
2785 2787
2786 /* lengh bytes transfered */ 2788 /* lengh bytes transferred */
2787 len = req->req.length - req->req.actual; 2789 len = req->req.length - req->req.actual;
2788 if (len > ep->ep.maxpacket) 2790 if (len > ep->ep.maxpacket)
2789 len = ep->ep.maxpacket; 2791 len = ep->ep.maxpacket;
@@ -3358,7 +3360,6 @@ static int udc_probe(struct udc *dev)
3358 dev_set_name(&dev->gadget.dev, "gadget"); 3360 dev_set_name(&dev->gadget.dev, "gadget");
3359 dev->gadget.dev.release = gadget_release; 3361 dev->gadget.dev.release = gadget_release;
3360 dev->gadget.name = name; 3362 dev->gadget.name = name;
3361 dev->gadget.name = name;
3362 dev->gadget.is_dualspeed = 1; 3363 dev->gadget.is_dualspeed = 1;
3363 3364
3364 /* init registers, interrupts, ... */ 3365 /* init registers, interrupts, ... */
@@ -3382,8 +3383,10 @@ static int udc_probe(struct udc *dev)
3382 udc = dev; 3383 udc = dev;
3383 3384
3384 retval = device_register(&dev->gadget.dev); 3385 retval = device_register(&dev->gadget.dev);
3385 if (retval) 3386 if (retval) {
3387 put_device(&dev->gadget.dev);
3386 goto finished; 3388 goto finished;
3389 }
3387 3390
3388 /* timer init */ 3391 /* timer init */
3389 init_timer(&udc_timer); 3392 init_timer(&udc_timer);
diff --git a/drivers/usb/gadget/amd5536udc.h b/drivers/usb/gadget/amd5536udc.h
index 4bbabbbfc93f..1d1c7543468e 100644
--- a/drivers/usb/gadget/amd5536udc.h
+++ b/drivers/usb/gadget/amd5536udc.h
@@ -584,7 +584,7 @@ union udc_setup_data {
584 * SET and GET bitfields in u32 values 584 * SET and GET bitfields in u32 values
585 * via constants for mask/offset: 585 * via constants for mask/offset:
586 * <bit_field_stub_name> is the text between 586 * <bit_field_stub_name> is the text between
587 * UDC_ and _MASK|_OFS of appropiate 587 * UDC_ and _MASK|_OFS of appropriate
588 * constant 588 * constant
589 * 589 *
590 * set bitfield value in u32 u32Val 590 * set bitfield value in u32 u32Val
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 93ead19507b6..f4690ffcb489 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -38,6 +38,7 @@
38#include <linux/clk.h> 38#include <linux/clk.h>
39#include <linux/usb/ch9.h> 39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h> 40#include <linux/usb/gadget.h>
41#include <linux/prefetch.h>
41 42
42#include <asm/byteorder.h> 43#include <asm/byteorder.h>
43#include <mach/hardware.h> 44#include <mach/hardware.h>
@@ -826,7 +827,7 @@ done:
826 return status; 827 return status;
827} 828}
828 829
829/* reinit == restore inital software state */ 830/* reinit == restore initial software state */
830static void udc_reinit(struct at91_udc *udc) 831static void udc_reinit(struct at91_udc *udc)
831{ 832{
832 u32 i; 833 u32 i;
@@ -1266,7 +1267,6 @@ write_in:
1266 csr |= AT91_UDP_TXPKTRDY; 1267 csr |= AT91_UDP_TXPKTRDY;
1267 __raw_writel(csr, creg); 1268 __raw_writel(csr, creg);
1268 udc->req_pending = 0; 1269 udc->req_pending = 0;
1269 return;
1270} 1270}
1271 1271
1272static void handle_ep0(struct at91_udc *udc) 1272static void handle_ep0(struct at91_udc *udc)
@@ -1628,7 +1628,8 @@ static void at91_vbus_timer(unsigned long data)
1628 schedule_work(&udc->vbus_timer_work); 1628 schedule_work(&udc->vbus_timer_work);
1629} 1629}
1630 1630
1631int usb_gadget_register_driver (struct usb_gadget_driver *driver) 1631int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1632 int (*bind)(struct usb_gadget *))
1632{ 1633{
1633 struct at91_udc *udc = &controller; 1634 struct at91_udc *udc = &controller;
1634 int retval; 1635 int retval;
@@ -1636,7 +1637,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1636 1637
1637 if (!driver 1638 if (!driver
1638 || driver->speed < USB_SPEED_FULL 1639 || driver->speed < USB_SPEED_FULL
1639 || !driver->bind 1640 || !bind
1640 || !driver->setup) { 1641 || !driver->setup) {
1641 DBG("bad parameter.\n"); 1642 DBG("bad parameter.\n");
1642 return -EINVAL; 1643 return -EINVAL;
@@ -1653,9 +1654,9 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1653 udc->enabled = 1; 1654 udc->enabled = 1;
1654 udc->selfpowered = 1; 1655 udc->selfpowered = 1;
1655 1656
1656 retval = driver->bind(&udc->gadget); 1657 retval = bind(&udc->gadget);
1657 if (retval) { 1658 if (retval) {
1658 DBG("driver->bind() returned %d\n", retval); 1659 DBG("bind() returned %d\n", retval);
1659 udc->driver = NULL; 1660 udc->driver = NULL;
1660 udc->gadget.dev.driver = NULL; 1661 udc->gadget.dev.driver = NULL;
1661 dev_set_drvdata(&udc->gadget.dev, NULL); 1662 dev_set_drvdata(&udc->gadget.dev, NULL);
@@ -1671,7 +1672,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1671 DBG("bound to %s\n", driver->driver.name); 1672 DBG("bound to %s\n", driver->driver.name);
1672 return 0; 1673 return 0;
1673} 1674}
1674EXPORT_SYMBOL (usb_gadget_register_driver); 1675EXPORT_SYMBOL(usb_gadget_probe_driver);
1675 1676
1676int usb_gadget_unregister_driver (struct usb_gadget_driver *driver) 1677int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1677{ 1678{
@@ -1767,7 +1768,7 @@ static int __init at91udc_probe(struct platform_device *pdev)
1767 } 1768 }
1768 1769
1769 /* newer chips have more FIFO memory than rm9200 */ 1770 /* newer chips have more FIFO memory than rm9200 */
1770 if (cpu_is_at91sam9260()) { 1771 if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
1771 udc->ep[0].maxpacket = 64; 1772 udc->ep[0].maxpacket = 64;
1772 udc->ep[3].maxpacket = 64; 1773 udc->ep[3].maxpacket = 64;
1773 udc->ep[4].maxpacket = 512; 1774 udc->ep[4].maxpacket = 512;
@@ -1798,8 +1799,10 @@ static int __init at91udc_probe(struct platform_device *pdev)
1798 } 1799 }
1799 1800
1800 retval = device_register(&udc->gadget.dev); 1801 retval = device_register(&udc->gadget.dev);
1801 if (retval < 0) 1802 if (retval < 0) {
1803 put_device(&udc->gadget.dev);
1802 goto fail0b; 1804 goto fail0b;
1805 }
1803 1806
1804 /* don't do anything until we have both gadget driver and VBUS */ 1807 /* don't do anything until we have both gadget driver and VBUS */
1805 clk_enable(udc->iclk); 1808 clk_enable(udc->iclk);
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index d623c7bda1f6..db1a659702ba 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -1789,7 +1789,8 @@ out:
1789 return IRQ_HANDLED; 1789 return IRQ_HANDLED;
1790} 1790}
1791 1791
1792int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1792int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1793 int (*bind)(struct usb_gadget *))
1793{ 1794{
1794 struct usba_udc *udc = &the_udc; 1795 struct usba_udc *udc = &the_udc;
1795 unsigned long flags; 1796 unsigned long flags;
@@ -1812,7 +1813,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1812 clk_enable(udc->pclk); 1813 clk_enable(udc->pclk);
1813 clk_enable(udc->hclk); 1814 clk_enable(udc->hclk);
1814 1815
1815 ret = driver->bind(&udc->gadget); 1816 ret = bind(&udc->gadget);
1816 if (ret) { 1817 if (ret) {
1817 DBG(DBG_ERR, "Could not bind to driver %s: error %d\n", 1818 DBG(DBG_ERR, "Could not bind to driver %s: error %d\n",
1818 driver->driver.name, ret); 1819 driver->driver.name, ret);
@@ -1841,7 +1842,7 @@ err_driver_bind:
1841 udc->gadget.dev.driver = NULL; 1842 udc->gadget.dev.driver = NULL;
1842 return ret; 1843 return ret;
1843} 1844}
1844EXPORT_SYMBOL(usb_gadget_register_driver); 1845EXPORT_SYMBOL(usb_gadget_probe_driver);
1845 1846
1846int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1847int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1847{ 1848{
@@ -2014,6 +2015,9 @@ static int __init usba_udc_probe(struct platform_device *pdev)
2014 } else { 2015 } else {
2015 disable_irq(gpio_to_irq(udc->vbus_pin)); 2016 disable_irq(gpio_to_irq(udc->vbus_pin));
2016 } 2017 }
2018 } else {
2019 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2020 udc->vbus_pin = -EINVAL;
2017 } 2021 }
2018 } 2022 }
2019 2023
@@ -2053,8 +2057,10 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2053 usba_ep_cleanup_debugfs(&usba_ep[i]); 2057 usba_ep_cleanup_debugfs(&usba_ep[i]);
2054 usba_cleanup_debugfs(udc); 2058 usba_cleanup_debugfs(udc);
2055 2059
2056 if (gpio_is_valid(udc->vbus_pin)) 2060 if (gpio_is_valid(udc->vbus_pin)) {
2061 free_irq(gpio_to_irq(udc->vbus_pin), udc);
2057 gpio_free(udc->vbus_pin); 2062 gpio_free(udc->vbus_pin);
2063 }
2058 2064
2059 free_irq(udc->irq, udc); 2065 free_irq(udc->irq, udc);
2060 kfree(usba_ep); 2066 kfree(usba_ep);
@@ -2089,6 +2095,6 @@ static void __exit udc_exit(void)
2089module_exit(udc_exit); 2095module_exit(udc_exit);
2090 2096
2091MODULE_DESCRIPTION("Atmel USBA UDC driver"); 2097MODULE_DESCRIPTION("Atmel USBA UDC driver");
2092MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>"); 2098MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2093MODULE_LICENSE("GPL"); 2099MODULE_LICENSE("GPL");
2094MODULE_ALIAS("platform:atmel_usba_udc"); 2100MODULE_ALIAS("platform:atmel_usba_udc");
diff --git a/drivers/usb/gadget/audio.c b/drivers/usb/gadget/audio.c
index b744ccd0f34d..93b999e49ef3 100644
--- a/drivers/usb/gadget/audio.c
+++ b/drivers/usb/gadget/audio.c
@@ -89,7 +89,7 @@ static const struct usb_descriptor_header *otg_desc[] = {
89 89
90/*-------------------------------------------------------------------------*/ 90/*-------------------------------------------------------------------------*/
91 91
92static int __ref audio_do_config(struct usb_configuration *c) 92static int __init audio_do_config(struct usb_configuration *c)
93{ 93{
94 /* FIXME alloc iConfiguration string, set it in c->strings */ 94 /* FIXME alloc iConfiguration string, set it in c->strings */
95 95
@@ -105,7 +105,6 @@ static int __ref audio_do_config(struct usb_configuration *c)
105 105
106static struct usb_configuration audio_config_driver = { 106static struct usb_configuration audio_config_driver = {
107 .label = DRIVER_DESC, 107 .label = DRIVER_DESC,
108 .bind = audio_do_config,
109 .bConfigurationValue = 1, 108 .bConfigurationValue = 1,
110 /* .iConfiguration = DYNAMIC */ 109 /* .iConfiguration = DYNAMIC */
111 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 110 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -113,7 +112,7 @@ static struct usb_configuration audio_config_driver = {
113 112
114/*-------------------------------------------------------------------------*/ 113/*-------------------------------------------------------------------------*/
115 114
116static int __ref audio_bind(struct usb_composite_dev *cdev) 115static int __init audio_bind(struct usb_composite_dev *cdev)
117{ 116{
118 int gcnum; 117 int gcnum;
119 int status; 118 int status;
@@ -145,7 +144,7 @@ static int __ref audio_bind(struct usb_composite_dev *cdev)
145 strings_dev[STRING_PRODUCT_IDX].id = status; 144 strings_dev[STRING_PRODUCT_IDX].id = status;
146 device_desc.iProduct = status; 145 device_desc.iProduct = status;
147 146
148 status = usb_add_config(cdev, &audio_config_driver); 147 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
149 if (status < 0) 148 if (status < 0)
150 goto fail; 149 goto fail;
151 150
@@ -166,13 +165,12 @@ static struct usb_composite_driver audio_driver = {
166 .name = "g_audio", 165 .name = "g_audio",
167 .dev = &device_desc, 166 .dev = &device_desc,
168 .strings = audio_strings, 167 .strings = audio_strings,
169 .bind = audio_bind,
170 .unbind = __exit_p(audio_unbind), 168 .unbind = __exit_p(audio_unbind),
171}; 169};
172 170
173static int __init init(void) 171static int __init init(void)
174{ 172{
175 return usb_composite_register(&audio_driver); 173 return usb_composite_probe(&audio_driver, audio_bind);
176} 174}
177module_init(init); 175module_init(init);
178 176
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c
index 1f5ba2fd4c1f..2720ab07ef1a 100644
--- a/drivers/usb/gadget/cdc2.c
+++ b/drivers/usb/gadget/cdc2.c
@@ -129,7 +129,7 @@ static u8 hostaddr[ETH_ALEN];
129/* 129/*
130 * We _always_ have both CDC ECM and CDC ACM functions. 130 * We _always_ have both CDC ECM and CDC ACM functions.
131 */ 131 */
132static int __ref cdc_do_config(struct usb_configuration *c) 132static int __init cdc_do_config(struct usb_configuration *c)
133{ 133{
134 int status; 134 int status;
135 135
@@ -151,7 +151,6 @@ static int __ref cdc_do_config(struct usb_configuration *c)
151 151
152static struct usb_configuration cdc_config_driver = { 152static struct usb_configuration cdc_config_driver = {
153 .label = "CDC Composite (ECM + ACM)", 153 .label = "CDC Composite (ECM + ACM)",
154 .bind = cdc_do_config,
155 .bConfigurationValue = 1, 154 .bConfigurationValue = 1,
156 /* .iConfiguration = DYNAMIC */ 155 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 156 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -159,7 +158,7 @@ static struct usb_configuration cdc_config_driver = {
159 158
160/*-------------------------------------------------------------------------*/ 159/*-------------------------------------------------------------------------*/
161 160
162static int __ref cdc_bind(struct usb_composite_dev *cdev) 161static int __init cdc_bind(struct usb_composite_dev *cdev)
163{ 162{
164 int gcnum; 163 int gcnum;
165 struct usb_gadget *gadget = cdev->gadget; 164 struct usb_gadget *gadget = cdev->gadget;
@@ -218,7 +217,7 @@ static int __ref cdc_bind(struct usb_composite_dev *cdev)
218 device_desc.iProduct = status; 217 device_desc.iProduct = status;
219 218
220 /* register our configuration */ 219 /* register our configuration */
221 status = usb_add_config(cdev, &cdc_config_driver); 220 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
222 if (status < 0) 221 if (status < 0)
223 goto fail1; 222 goto fail1;
224 223
@@ -245,7 +244,6 @@ static struct usb_composite_driver cdc_driver = {
245 .name = "g_cdc", 244 .name = "g_cdc",
246 .dev = &device_desc, 245 .dev = &device_desc,
247 .strings = dev_strings, 246 .strings = dev_strings,
248 .bind = cdc_bind,
249 .unbind = __exit_p(cdc_unbind), 247 .unbind = __exit_p(cdc_unbind),
250}; 248};
251 249
@@ -255,7 +253,7 @@ MODULE_LICENSE("GPL");
255 253
256static int __init init(void) 254static int __init init(void)
257{ 255{
258 return usb_composite_register(&cdc_driver); 256 return usb_composite_probe(&cdc_driver, cdc_bind);
259} 257}
260module_init(init); 258module_init(init);
261 259
diff --git a/drivers/usb/gadget/ci13xxx_msm.c b/drivers/usb/gadget/ci13xxx_msm.c
new file mode 100644
index 000000000000..139ac9419597
--- /dev/null
+++ b/drivers/usb/gadget/ci13xxx_msm.c
@@ -0,0 +1,134 @@
1/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
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
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 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/pm_runtime.h>
22#include <linux/usb/msm_hsusb_hw.h>
23#include <linux/usb/ulpi.h>
24
25#include "ci13xxx_udc.c"
26
27#define MSM_USB_BASE (udc->regs)
28
29static irqreturn_t msm_udc_irq(int irq, void *data)
30{
31 return udc_irq();
32}
33
34static void ci13xxx_msm_notify_event(struct ci13xxx *udc, unsigned event)
35{
36 struct device *dev = udc->gadget.dev.parent;
37 int val;
38
39 switch (event) {
40 case CI13XXX_CONTROLLER_RESET_EVENT:
41 dev_dbg(dev, "CI13XXX_CONTROLLER_RESET_EVENT received\n");
42 writel(0, USB_AHBBURST);
43 writel(0, USB_AHBMODE);
44 break;
45 case CI13XXX_CONTROLLER_STOPPED_EVENT:
46 dev_dbg(dev, "CI13XXX_CONTROLLER_STOPPED_EVENT received\n");
47 /*
48 * Put the transceiver in non-driving mode. Otherwise host
49 * may not detect soft-disconnection.
50 */
51 val = otg_io_read(udc->transceiver, ULPI_FUNC_CTRL);
52 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
53 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
54 otg_io_write(udc->transceiver, val, ULPI_FUNC_CTRL);
55 break;
56 default:
57 dev_dbg(dev, "unknown ci13xxx_udc event\n");
58 break;
59 }
60}
61
62static struct ci13xxx_udc_driver ci13xxx_msm_udc_driver = {
63 .name = "ci13xxx_msm",
64 .flags = CI13XXX_REGS_SHARED |
65 CI13XXX_REQUIRE_TRANSCEIVER |
66 CI13XXX_PULLUP_ON_VBUS |
67 CI13XXX_DISABLE_STREAMING,
68
69 .notify_event = ci13xxx_msm_notify_event,
70};
71
72static int ci13xxx_msm_probe(struct platform_device *pdev)
73{
74 struct resource *res;
75 void __iomem *regs;
76 int irq;
77 int ret;
78
79 dev_dbg(&pdev->dev, "ci13xxx_msm_probe\n");
80
81 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82 if (!res) {
83 dev_err(&pdev->dev, "failed to get platform resource mem\n");
84 return -ENXIO;
85 }
86
87 regs = ioremap(res->start, resource_size(res));
88 if (!regs) {
89 dev_err(&pdev->dev, "ioremap failed\n");
90 return -ENOMEM;
91 }
92
93 ret = udc_probe(&ci13xxx_msm_udc_driver, &pdev->dev, regs);
94 if (ret < 0) {
95 dev_err(&pdev->dev, "udc_probe failed\n");
96 goto iounmap;
97 }
98
99 irq = platform_get_irq(pdev, 0);
100 if (irq < 0) {
101 dev_err(&pdev->dev, "IRQ not found\n");
102 ret = -ENXIO;
103 goto udc_remove;
104 }
105
106 ret = request_irq(irq, msm_udc_irq, IRQF_SHARED, pdev->name, pdev);
107 if (ret < 0) {
108 dev_err(&pdev->dev, "request_irq failed\n");
109 goto udc_remove;
110 }
111
112 pm_runtime_no_callbacks(&pdev->dev);
113 pm_runtime_enable(&pdev->dev);
114
115 return 0;
116
117udc_remove:
118 udc_remove();
119iounmap:
120 iounmap(regs);
121
122 return ret;
123}
124
125static struct platform_driver ci13xxx_msm_driver = {
126 .probe = ci13xxx_msm_probe,
127 .driver = { .name = "msm_hsusb", },
128};
129
130static int __init ci13xxx_msm_init(void)
131{
132 return platform_driver_register(&ci13xxx_msm_driver);
133}
134module_init(ci13xxx_msm_init);
diff --git a/drivers/usb/gadget/ci13xxx_pci.c b/drivers/usb/gadget/ci13xxx_pci.c
new file mode 100644
index 000000000000..883ab5e832d1
--- /dev/null
+++ b/drivers/usb/gadget/ci13xxx_pci.c
@@ -0,0 +1,176 @@
1/*
2 * ci13xxx_pci.c - MIPS USB IP core family device controller
3 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/pci.h>
15
16#include "ci13xxx_udc.c"
17
18/* driver name */
19#define UDC_DRIVER_NAME "ci13xxx_pci"
20
21/******************************************************************************
22 * PCI block
23 *****************************************************************************/
24/**
25 * ci13xxx_pci_irq: interrut handler
26 * @irq: irq number
27 * @pdev: USB Device Controller interrupt source
28 *
29 * This function returns IRQ_HANDLED if the IRQ has been handled
30 * This is an ISR don't trace, use attribute interface instead
31 */
32static irqreturn_t ci13xxx_pci_irq(int irq, void *pdev)
33{
34 if (irq == 0) {
35 dev_err(&((struct pci_dev *)pdev)->dev, "Invalid IRQ0 usage!");
36 return IRQ_HANDLED;
37 }
38 return udc_irq();
39}
40
41static struct ci13xxx_udc_driver ci13xxx_pci_udc_driver = {
42 .name = UDC_DRIVER_NAME,
43};
44
45/**
46 * ci13xxx_pci_probe: PCI probe
47 * @pdev: USB device controller being probed
48 * @id: PCI hotplug ID connecting controller to UDC framework
49 *
50 * This function returns an error code
51 * Allocates basic PCI resources for this USB device controller, and then
52 * invokes the udc_probe() method to start the UDC associated with it
53 */
54static int __devinit ci13xxx_pci_probe(struct pci_dev *pdev,
55 const struct pci_device_id *id)
56{
57 void __iomem *regs = NULL;
58 int retval = 0;
59
60 if (id == NULL)
61 return -EINVAL;
62
63 retval = pci_enable_device(pdev);
64 if (retval)
65 goto done;
66
67 if (!pdev->irq) {
68 dev_err(&pdev->dev, "No IRQ, check BIOS/PCI setup!");
69 retval = -ENODEV;
70 goto disable_device;
71 }
72
73 retval = pci_request_regions(pdev, UDC_DRIVER_NAME);
74 if (retval)
75 goto disable_device;
76
77 /* BAR 0 holds all the registers */
78 regs = pci_iomap(pdev, 0, 0);
79 if (!regs) {
80 dev_err(&pdev->dev, "Error mapping memory!");
81 retval = -EFAULT;
82 goto release_regions;
83 }
84 pci_set_drvdata(pdev, (__force void *)regs);
85
86 pci_set_master(pdev);
87 pci_try_set_mwi(pdev);
88
89 retval = udc_probe(&ci13xxx_pci_udc_driver, &pdev->dev, regs);
90 if (retval)
91 goto iounmap;
92
93 /* our device does not have MSI capability */
94
95 retval = request_irq(pdev->irq, ci13xxx_pci_irq, IRQF_SHARED,
96 UDC_DRIVER_NAME, pdev);
97 if (retval)
98 goto gadget_remove;
99
100 return 0;
101
102 gadget_remove:
103 udc_remove();
104 iounmap:
105 pci_iounmap(pdev, regs);
106 release_regions:
107 pci_release_regions(pdev);
108 disable_device:
109 pci_disable_device(pdev);
110 done:
111 return retval;
112}
113
114/**
115 * ci13xxx_pci_remove: PCI remove
116 * @pdev: USB Device Controller being removed
117 *
118 * Reverses the effect of ci13xxx_pci_probe(),
119 * first invoking the udc_remove() and then releases
120 * all PCI resources allocated for this USB device controller
121 */
122static void __devexit ci13xxx_pci_remove(struct pci_dev *pdev)
123{
124 free_irq(pdev->irq, pdev);
125 udc_remove();
126 pci_iounmap(pdev, (__force void __iomem *)pci_get_drvdata(pdev));
127 pci_release_regions(pdev);
128 pci_disable_device(pdev);
129}
130
131/**
132 * PCI device table
133 * PCI device structure
134 *
135 * Check "pci.h" for details
136 */
137static DEFINE_PCI_DEVICE_TABLE(ci13xxx_pci_id_table) = {
138 { PCI_DEVICE(0x153F, 0x1004) },
139 { PCI_DEVICE(0x153F, 0x1006) },
140 { 0, 0, 0, 0, 0, 0, 0 /* end: all zeroes */ }
141};
142MODULE_DEVICE_TABLE(pci, ci13xxx_pci_id_table);
143
144static struct pci_driver ci13xxx_pci_driver = {
145 .name = UDC_DRIVER_NAME,
146 .id_table = ci13xxx_pci_id_table,
147 .probe = ci13xxx_pci_probe,
148 .remove = __devexit_p(ci13xxx_pci_remove),
149};
150
151/**
152 * ci13xxx_pci_init: module init
153 *
154 * Driver load
155 */
156static int __init ci13xxx_pci_init(void)
157{
158 return pci_register_driver(&ci13xxx_pci_driver);
159}
160module_init(ci13xxx_pci_init);
161
162/**
163 * ci13xxx_pci_exit: module exit
164 *
165 * Driver unload
166 */
167static void __exit ci13xxx_pci_exit(void)
168{
169 pci_unregister_driver(&ci13xxx_pci_driver);
170}
171module_exit(ci13xxx_pci_exit);
172
173MODULE_AUTHOR("MIPS - David Lopo <dlopo@chipidea.mips.com>");
174MODULE_DESCRIPTION("MIPS CI13XXX USB Peripheral Controller");
175MODULE_LICENSE("GPL");
176MODULE_VERSION("June 2008");
diff --git a/drivers/usb/gadget/ci13xxx_udc.c b/drivers/usb/gadget/ci13xxx_udc.c
index 699695128e33..baaf87ed7685 100644
--- a/drivers/usb/gadget/ci13xxx_udc.c
+++ b/drivers/usb/gadget/ci13xxx_udc.c
@@ -22,7 +22,6 @@
22 * - ENDPT: endpoint operations (Gadget API) 22 * - ENDPT: endpoint operations (Gadget API)
23 * - GADGET: gadget operations (Gadget API) 23 * - GADGET: gadget operations (Gadget API)
24 * - BUS: bus glue code, bus abstraction layer 24 * - BUS: bus glue code, bus abstraction layer
25 * - PCI: PCI core interface and PCI resources (interrupts, memory...)
26 * 25 *
27 * Compile Options 26 * Compile Options
28 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities 27 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
@@ -60,11 +59,11 @@
60#include <linux/io.h> 59#include <linux/io.h>
61#include <linux/irq.h> 60#include <linux/irq.h>
62#include <linux/kernel.h> 61#include <linux/kernel.h>
63#include <linux/module.h>
64#include <linux/pci.h>
65#include <linux/slab.h> 62#include <linux/slab.h>
63#include <linux/pm_runtime.h>
66#include <linux/usb/ch9.h> 64#include <linux/usb/ch9.h>
67#include <linux/usb/gadget.h> 65#include <linux/usb/gadget.h>
66#include <linux/usb/otg.h>
68 67
69#include "ci13xxx_udc.h" 68#include "ci13xxx_udc.h"
70 69
@@ -75,15 +74,23 @@
75/* ctrl register bank access */ 74/* ctrl register bank access */
76static DEFINE_SPINLOCK(udc_lock); 75static DEFINE_SPINLOCK(udc_lock);
77 76
78/* driver name */
79#define UDC_DRIVER_NAME "ci13xxx_udc"
80
81/* control endpoint description */ 77/* control endpoint description */
82static const struct usb_endpoint_descriptor 78static const struct usb_endpoint_descriptor
83ctrl_endpt_desc = { 79ctrl_endpt_out_desc = {
80 .bLength = USB_DT_ENDPOINT_SIZE,
81 .bDescriptorType = USB_DT_ENDPOINT,
82
83 .bEndpointAddress = USB_DIR_OUT,
84 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
85 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
86};
87
88static const struct usb_endpoint_descriptor
89ctrl_endpt_in_desc = {
84 .bLength = USB_DT_ENDPOINT_SIZE, 90 .bLength = USB_DT_ENDPOINT_SIZE,
85 .bDescriptorType = USB_DT_ENDPOINT, 91 .bDescriptorType = USB_DT_ENDPOINT,
86 92
93 .bEndpointAddress = USB_DIR_IN,
87 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 94 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
88 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), 95 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
89}; 96};
@@ -132,6 +139,9 @@ static struct {
132 size_t size; /* bank size */ 139 size_t size; /* bank size */
133} hw_bank; 140} hw_bank;
134 141
142/* MSM specific */
143#define ABS_AHBBURST (0x0090UL)
144#define ABS_AHBMODE (0x0098UL)
135/* UDC register map */ 145/* UDC register map */
136#define ABS_CAPLENGTH (0x100UL) 146#define ABS_CAPLENGTH (0x100UL)
137#define ABS_HCCPARAMS (0x108UL) 147#define ABS_HCCPARAMS (0x108UL)
@@ -248,13 +258,7 @@ static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
248 return (reg & mask) >> ffs_nr(mask); 258 return (reg & mask) >> ffs_nr(mask);
249} 259}
250 260
251/** 261static int hw_device_init(void __iomem *base)
252 * hw_device_reset: resets chip (execute without interruption)
253 * @base: register base address
254 *
255 * This function returns an error code
256 */
257static int hw_device_reset(void __iomem *base)
258{ 262{
259 u32 reg; 263 u32 reg;
260 264
@@ -271,6 +275,28 @@ static int hw_device_reset(void __iomem *base)
271 hw_bank.size += CAP_LAST; 275 hw_bank.size += CAP_LAST;
272 hw_bank.size /= sizeof(u32); 276 hw_bank.size /= sizeof(u32);
273 277
278 reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
279 hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
280
281 if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX)
282 return -ENODEV;
283
284 /* setup lock mode ? */
285
286 /* ENDPTSETUPSTAT is '0' by default */
287
288 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
289
290 return 0;
291}
292/**
293 * hw_device_reset: resets chip (execute without interruption)
294 * @base: register base address
295 *
296 * This function returns an error code
297 */
298static int hw_device_reset(struct ci13xxx *udc)
299{
274 /* should flush & stop before reset */ 300 /* should flush & stop before reset */
275 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); 301 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
276 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0); 302 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
@@ -279,6 +305,14 @@ static int hw_device_reset(void __iomem *base)
279 while (hw_cread(CAP_USBCMD, USBCMD_RST)) 305 while (hw_cread(CAP_USBCMD, USBCMD_RST))
280 udelay(10); /* not RTOS friendly */ 306 udelay(10); /* not RTOS friendly */
281 307
308
309 if (udc->udc_driver->notify_event)
310 udc->udc_driver->notify_event(udc,
311 CI13XXX_CONTROLLER_RESET_EVENT);
312
313 if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
314 hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
315
282 /* USBMODE should be configured step by step */ 316 /* USBMODE should be configured step by step */
283 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); 317 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
284 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE); 318 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
@@ -290,18 +324,6 @@ static int hw_device_reset(void __iomem *base)
290 return -ENODEV; 324 return -ENODEV;
291 } 325 }
292 326
293 reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
294 if (reg == 0 || reg > ENDPT_MAX)
295 return -ENODEV;
296
297 hw_ep_max = reg; /* cache hw ENDPT_MAX */
298
299 /* setup lock mode ? */
300
301 /* ENDPTSETUPSTAT is '0' by default */
302
303 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
304
305 return 0; 327 return 0;
306} 328}
307 329
@@ -413,20 +435,6 @@ static int hw_ep_get_halt(int num, int dir)
413} 435}
414 436
415/** 437/**
416 * hw_ep_is_primed: test if endpoint is primed (execute without interruption)
417 * @num: endpoint number
418 * @dir: endpoint direction
419 *
420 * This function returns true if endpoint primed
421 */
422static int hw_ep_is_primed(int num, int dir)
423{
424 u32 reg = hw_cread(CAP_ENDPTPRIME, ~0) | hw_cread(CAP_ENDPTSTAT, ~0);
425
426 return test_bit(hw_ep_bit(num, dir), (void *)&reg);
427}
428
429/**
430 * hw_test_and_clear_setup_status: test & clear setup status (execute without 438 * hw_test_and_clear_setup_status: test & clear setup status (execute without
431 * interruption) 439 * interruption)
432 * @n: bit number (endpoint) 440 * @n: bit number (endpoint)
@@ -450,10 +458,6 @@ static int hw_ep_prime(int num, int dir, int is_ctrl)
450{ 458{
451 int n = hw_ep_bit(num, dir); 459 int n = hw_ep_bit(num, dir);
452 460
453 /* the caller should flush first */
454 if (hw_ep_is_primed(num, dir))
455 return -EBUSY;
456
457 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num))) 461 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
458 return -EAGAIN; 462 return -EAGAIN;
459 463
@@ -1186,16 +1190,17 @@ static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
1186 } 1190 }
1187 1191
1188 spin_lock_irqsave(udc->lock, flags); 1192 spin_lock_irqsave(udc->lock, flags);
1189 for (i = 0; i < hw_ep_max; i++) { 1193 for (i = 0; i < hw_ep_max/2; i++) {
1190 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; 1194 struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
1195 struct ci13xxx_ep *mEpTx = &udc->ci13xxx_ep[i + hw_ep_max/2];
1191 n += scnprintf(buf + n, PAGE_SIZE - n, 1196 n += scnprintf(buf + n, PAGE_SIZE - n,
1192 "EP=%02i: RX=%08X TX=%08X\n", 1197 "EP=%02i: RX=%08X TX=%08X\n",
1193 i, (u32)mEp->qh[RX].dma, (u32)mEp->qh[TX].dma); 1198 i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
1194 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) { 1199 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
1195 n += scnprintf(buf + n, PAGE_SIZE - n, 1200 n += scnprintf(buf + n, PAGE_SIZE - n,
1196 " %04X: %08X %08X\n", j, 1201 " %04X: %08X %08X\n", j,
1197 *((u32 *)mEp->qh[RX].ptr + j), 1202 *((u32 *)mEpRx->qh.ptr + j),
1198 *((u32 *)mEp->qh[TX].ptr + j)); 1203 *((u32 *)mEpTx->qh.ptr + j));
1199 } 1204 }
1200 } 1205 }
1201 spin_unlock_irqrestore(udc->lock, flags); 1206 spin_unlock_irqrestore(udc->lock, flags);
@@ -1282,7 +1287,7 @@ static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1282 unsigned long flags; 1287 unsigned long flags;
1283 struct list_head *ptr = NULL; 1288 struct list_head *ptr = NULL;
1284 struct ci13xxx_req *req = NULL; 1289 struct ci13xxx_req *req = NULL;
1285 unsigned i, j, k, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32); 1290 unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
1286 1291
1287 dbg_trace("[%s] %p\n", __func__, buf); 1292 dbg_trace("[%s] %p\n", __func__, buf);
1288 if (attr == NULL || buf == NULL) { 1293 if (attr == NULL || buf == NULL) {
@@ -1292,22 +1297,20 @@ static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1292 1297
1293 spin_lock_irqsave(udc->lock, flags); 1298 spin_lock_irqsave(udc->lock, flags);
1294 for (i = 0; i < hw_ep_max; i++) 1299 for (i = 0; i < hw_ep_max; i++)
1295 for (k = RX; k <= TX; k++) 1300 list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
1296 list_for_each(ptr, &udc->ci13xxx_ep[i].qh[k].queue) 1301 {
1297 { 1302 req = list_entry(ptr, struct ci13xxx_req, queue);
1298 req = list_entry(ptr,
1299 struct ci13xxx_req, queue);
1300 1303
1304 n += scnprintf(buf + n, PAGE_SIZE - n,
1305 "EP=%02i: TD=%08X %s\n",
1306 i % hw_ep_max/2, (u32)req->dma,
1307 ((i < hw_ep_max/2) ? "RX" : "TX"));
1308
1309 for (j = 0; j < qSize; j++)
1301 n += scnprintf(buf + n, PAGE_SIZE - n, 1310 n += scnprintf(buf + n, PAGE_SIZE - n,
1302 "EP=%02i: TD=%08X %s\n", 1311 " %04X: %08X\n", j,
1303 i, (u32)req->dma, 1312 *((u32 *)req->ptr + j));
1304 ((k == RX) ? "RX" : "TX")); 1313 }
1305
1306 for (j = 0; j < qSize; j++)
1307 n += scnprintf(buf + n, PAGE_SIZE - n,
1308 " %04X: %08X\n", j,
1309 *((u32 *)req->ptr + j));
1310 }
1311 spin_unlock_irqrestore(udc->lock, flags); 1314 spin_unlock_irqrestore(udc->lock, flags);
1312 1315
1313 return n; 1316 return n;
@@ -1413,6 +1416,8 @@ static inline u8 _usb_addr(struct ci13xxx_ep *ep)
1413static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) 1416static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1414{ 1417{
1415 unsigned i; 1418 unsigned i;
1419 int ret = 0;
1420 unsigned length = mReq->req.length;
1416 1421
1417 trace("%p, %p", mEp, mReq); 1422 trace("%p, %p", mEp, mReq);
1418 1423
@@ -1420,53 +1425,91 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1420 if (mReq->req.status == -EALREADY) 1425 if (mReq->req.status == -EALREADY)
1421 return -EALREADY; 1426 return -EALREADY;
1422 1427
1423 if (hw_ep_is_primed(mEp->num, mEp->dir))
1424 return -EBUSY;
1425
1426 mReq->req.status = -EALREADY; 1428 mReq->req.status = -EALREADY;
1427 1429 if (length && !mReq->req.dma) {
1428 if (mReq->req.length && !mReq->req.dma) {
1429 mReq->req.dma = \ 1430 mReq->req.dma = \
1430 dma_map_single(mEp->device, mReq->req.buf, 1431 dma_map_single(mEp->device, mReq->req.buf,
1431 mReq->req.length, mEp->dir ? 1432 length, mEp->dir ? DMA_TO_DEVICE :
1432 DMA_TO_DEVICE : DMA_FROM_DEVICE); 1433 DMA_FROM_DEVICE);
1433 if (mReq->req.dma == 0) 1434 if (mReq->req.dma == 0)
1434 return -ENOMEM; 1435 return -ENOMEM;
1435 1436
1436 mReq->map = 1; 1437 mReq->map = 1;
1437 } 1438 }
1438 1439
1440 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
1441 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
1442 &mReq->zdma);
1443 if (mReq->zptr == NULL) {
1444 if (mReq->map) {
1445 dma_unmap_single(mEp->device, mReq->req.dma,
1446 length, mEp->dir ? DMA_TO_DEVICE :
1447 DMA_FROM_DEVICE);
1448 mReq->req.dma = 0;
1449 mReq->map = 0;
1450 }
1451 return -ENOMEM;
1452 }
1453 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
1454 mReq->zptr->next = TD_TERMINATE;
1455 mReq->zptr->token = TD_STATUS_ACTIVE;
1456 if (!mReq->req.no_interrupt)
1457 mReq->zptr->token |= TD_IOC;
1458 }
1439 /* 1459 /*
1440 * TD configuration 1460 * TD configuration
1441 * TODO - handle requests which spawns into several TDs 1461 * TODO - handle requests which spawns into several TDs
1442 */ 1462 */
1443 memset(mReq->ptr, 0, sizeof(*mReq->ptr)); 1463 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1444 mReq->ptr->next |= TD_TERMINATE; 1464 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
1445 mReq->ptr->token = mReq->req.length << ffs_nr(TD_TOTAL_BYTES);
1446 mReq->ptr->token &= TD_TOTAL_BYTES; 1465 mReq->ptr->token &= TD_TOTAL_BYTES;
1447 mReq->ptr->token |= TD_IOC;
1448 mReq->ptr->token |= TD_STATUS_ACTIVE; 1466 mReq->ptr->token |= TD_STATUS_ACTIVE;
1467 if (mReq->zptr) {
1468 mReq->ptr->next = mReq->zdma;
1469 } else {
1470 mReq->ptr->next = TD_TERMINATE;
1471 if (!mReq->req.no_interrupt)
1472 mReq->ptr->token |= TD_IOC;
1473 }
1449 mReq->ptr->page[0] = mReq->req.dma; 1474 mReq->ptr->page[0] = mReq->req.dma;
1450 for (i = 1; i < 5; i++) 1475 for (i = 1; i < 5; i++)
1451 mReq->ptr->page[i] = 1476 mReq->ptr->page[i] =
1452 (mReq->req.dma + i * PAGE_SIZE) & ~TD_RESERVED_MASK; 1477 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
1453 1478
1454 /* 1479 if (!list_empty(&mEp->qh.queue)) {
1455 * QH configuration 1480 struct ci13xxx_req *mReqPrev;
1456 * At this point it's guaranteed exclusive access to qhead 1481 int n = hw_ep_bit(mEp->num, mEp->dir);
1457 * (endpt is not primed) so it's no need to use tripwire 1482 int tmp_stat;
1458 */ 1483
1459 mEp->qh[mEp->dir].ptr->td.next = mReq->dma; /* TERMINATE = 0 */ 1484 mReqPrev = list_entry(mEp->qh.queue.prev,
1460 mEp->qh[mEp->dir].ptr->td.token &= ~TD_STATUS; /* clear status */ 1485 struct ci13xxx_req, queue);
1461 if (mReq->req.zero == 0) 1486 if (mReqPrev->zptr)
1462 mEp->qh[mEp->dir].ptr->cap |= QH_ZLT; 1487 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
1463 else 1488 else
1464 mEp->qh[mEp->dir].ptr->cap &= ~QH_ZLT; 1489 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
1490 wmb();
1491 if (hw_cread(CAP_ENDPTPRIME, BIT(n)))
1492 goto done;
1493 do {
1494 hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
1495 tmp_stat = hw_cread(CAP_ENDPTSTAT, BIT(n));
1496 } while (!hw_cread(CAP_USBCMD, USBCMD_ATDTW));
1497 hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, 0);
1498 if (tmp_stat)
1499 goto done;
1500 }
1501
1502 /* QH configuration */
1503 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
1504 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
1505 mEp->qh.ptr->cap |= QH_ZLT;
1465 1506
1466 wmb(); /* synchronize before ep prime */ 1507 wmb(); /* synchronize before ep prime */
1467 1508
1468 return hw_ep_prime(mEp->num, mEp->dir, 1509 ret = hw_ep_prime(mEp->num, mEp->dir,
1469 mEp->type == USB_ENDPOINT_XFER_CONTROL); 1510 mEp->type == USB_ENDPOINT_XFER_CONTROL);
1511done:
1512 return ret;
1470} 1513}
1471 1514
1472/** 1515/**
@@ -1483,8 +1526,15 @@ static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1483 if (mReq->req.status != -EALREADY) 1526 if (mReq->req.status != -EALREADY)
1484 return -EINVAL; 1527 return -EINVAL;
1485 1528
1486 if (hw_ep_is_primed(mEp->num, mEp->dir)) 1529 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
1487 hw_ep_flush(mEp->num, mEp->dir); 1530 return -EBUSY;
1531
1532 if (mReq->zptr) {
1533 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
1534 return -EBUSY;
1535 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
1536 mReq->zptr = NULL;
1537 }
1488 1538
1489 mReq->req.status = 0; 1539 mReq->req.status = 0;
1490 1540
@@ -1496,9 +1546,7 @@ static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1496 } 1546 }
1497 1547
1498 mReq->req.status = mReq->ptr->token & TD_STATUS; 1548 mReq->req.status = mReq->ptr->token & TD_STATUS;
1499 if ((TD_STATUS_ACTIVE & mReq->req.status) != 0) 1549 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1500 mReq->req.status = -ECONNRESET;
1501 else if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1502 mReq->req.status = -1; 1550 mReq->req.status = -1;
1503 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0) 1551 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
1504 mReq->req.status = -1; 1552 mReq->req.status = -1;
@@ -1531,16 +1579,16 @@ __acquires(mEp->lock)
1531 1579
1532 hw_ep_flush(mEp->num, mEp->dir); 1580 hw_ep_flush(mEp->num, mEp->dir);
1533 1581
1534 while (!list_empty(&mEp->qh[mEp->dir].queue)) { 1582 while (!list_empty(&mEp->qh.queue)) {
1535 1583
1536 /* pop oldest request */ 1584 /* pop oldest request */
1537 struct ci13xxx_req *mReq = \ 1585 struct ci13xxx_req *mReq = \
1538 list_entry(mEp->qh[mEp->dir].queue.next, 1586 list_entry(mEp->qh.queue.next,
1539 struct ci13xxx_req, queue); 1587 struct ci13xxx_req, queue);
1540 list_del_init(&mReq->queue); 1588 list_del_init(&mReq->queue);
1541 mReq->req.status = -ESHUTDOWN; 1589 mReq->req.status = -ESHUTDOWN;
1542 1590
1543 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) { 1591 if (mReq->req.complete != NULL) {
1544 spin_unlock(mEp->lock); 1592 spin_unlock(mEp->lock);
1545 mReq->req.complete(&mEp->ep, &mReq->req); 1593 mReq->req.complete(&mEp->ep, &mReq->req);
1546 spin_lock(mEp->lock); 1594 spin_lock(mEp->lock);
@@ -1557,26 +1605,28 @@ __acquires(mEp->lock)
1557 * Caller must hold lock 1605 * Caller must hold lock
1558 */ 1606 */
1559static int _gadget_stop_activity(struct usb_gadget *gadget) 1607static int _gadget_stop_activity(struct usb_gadget *gadget)
1560__releases(udc->lock)
1561__acquires(udc->lock)
1562{ 1608{
1563 struct usb_ep *ep; 1609 struct usb_ep *ep;
1564 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget); 1610 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1565 struct ci13xxx_ep *mEp = container_of(gadget->ep0, 1611 unsigned long flags;
1566 struct ci13xxx_ep, ep);
1567 1612
1568 trace("%p", gadget); 1613 trace("%p", gadget);
1569 1614
1570 if (gadget == NULL) 1615 if (gadget == NULL)
1571 return -EINVAL; 1616 return -EINVAL;
1572 1617
1573 spin_unlock(udc->lock); 1618 spin_lock_irqsave(udc->lock, flags);
1619 udc->gadget.speed = USB_SPEED_UNKNOWN;
1620 udc->remote_wakeup = 0;
1621 udc->suspended = 0;
1622 spin_unlock_irqrestore(udc->lock, flags);
1574 1623
1575 /* flush all endpoints */ 1624 /* flush all endpoints */
1576 gadget_for_each_ep(ep, gadget) { 1625 gadget_for_each_ep(ep, gadget) {
1577 usb_ep_fifo_flush(ep); 1626 usb_ep_fifo_flush(ep);
1578 } 1627 }
1579 usb_ep_fifo_flush(gadget->ep0); 1628 usb_ep_fifo_flush(&udc->ep0out.ep);
1629 usb_ep_fifo_flush(&udc->ep0in.ep);
1580 1630
1581 udc->driver->disconnect(gadget); 1631 udc->driver->disconnect(gadget);
1582 1632
@@ -1584,15 +1634,12 @@ __acquires(udc->lock)
1584 gadget_for_each_ep(ep, gadget) { 1634 gadget_for_each_ep(ep, gadget) {
1585 usb_ep_disable(ep); 1635 usb_ep_disable(ep);
1586 } 1636 }
1587 usb_ep_disable(gadget->ep0);
1588 1637
1589 if (mEp->status != NULL) { 1638 if (udc->status != NULL) {
1590 usb_ep_free_request(gadget->ep0, mEp->status); 1639 usb_ep_free_request(&udc->ep0in.ep, udc->status);
1591 mEp->status = NULL; 1640 udc->status = NULL;
1592 } 1641 }
1593 1642
1594 spin_lock(udc->lock);
1595
1596 return 0; 1643 return 0;
1597} 1644}
1598 1645
@@ -1609,7 +1656,6 @@ static void isr_reset_handler(struct ci13xxx *udc)
1609__releases(udc->lock) 1656__releases(udc->lock)
1610__acquires(udc->lock) 1657__acquires(udc->lock)
1611{ 1658{
1612 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[0];
1613 int retval; 1659 int retval;
1614 1660
1615 trace("%p", udc); 1661 trace("%p", udc);
@@ -1621,6 +1667,7 @@ __acquires(udc->lock)
1621 1667
1622 dbg_event(0xFF, "BUS RST", 0); 1668 dbg_event(0xFF, "BUS RST", 0);
1623 1669
1670 spin_unlock(udc->lock);
1624 retval = _gadget_stop_activity(&udc->gadget); 1671 retval = _gadget_stop_activity(&udc->gadget);
1625 if (retval) 1672 if (retval)
1626 goto done; 1673 goto done;
@@ -1629,15 +1676,10 @@ __acquires(udc->lock)
1629 if (retval) 1676 if (retval)
1630 goto done; 1677 goto done;
1631 1678
1632 spin_unlock(udc->lock); 1679 udc->status = usb_ep_alloc_request(&udc->ep0in.ep, GFP_ATOMIC);
1633 retval = usb_ep_enable(&mEp->ep, &ctrl_endpt_desc); 1680 if (udc->status == NULL)
1634 if (!retval) { 1681 retval = -ENOMEM;
1635 mEp->status = usb_ep_alloc_request(&mEp->ep, GFP_KERNEL); 1682
1636 if (mEp->status == NULL) {
1637 usb_ep_disable(&mEp->ep);
1638 retval = -ENOMEM;
1639 }
1640 }
1641 spin_lock(udc->lock); 1683 spin_lock(udc->lock);
1642 1684
1643 done: 1685 done:
@@ -1667,16 +1709,17 @@ static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
1667 1709
1668/** 1710/**
1669 * isr_get_status_response: get_status request response 1711 * isr_get_status_response: get_status request response
1670 * @ep: endpoint 1712 * @udc: udc struct
1671 * @setup: setup request packet 1713 * @setup: setup request packet
1672 * 1714 *
1673 * This function returns an error code 1715 * This function returns an error code
1674 */ 1716 */
1675static int isr_get_status_response(struct ci13xxx_ep *mEp, 1717static int isr_get_status_response(struct ci13xxx *udc,
1676 struct usb_ctrlrequest *setup) 1718 struct usb_ctrlrequest *setup)
1677__releases(mEp->lock) 1719__releases(mEp->lock)
1678__acquires(mEp->lock) 1720__acquires(mEp->lock)
1679{ 1721{
1722 struct ci13xxx_ep *mEp = &udc->ep0in;
1680 struct usb_request *req = NULL; 1723 struct usb_request *req = NULL;
1681 gfp_t gfp_flags = GFP_ATOMIC; 1724 gfp_t gfp_flags = GFP_ATOMIC;
1682 int dir, num, retval; 1725 int dir, num, retval;
@@ -1701,7 +1744,8 @@ __acquires(mEp->lock)
1701 } 1744 }
1702 1745
1703 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) { 1746 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1704 /* TODO: D1 - Remote Wakeup; D0 - Self Powered */ 1747 /* Assume that device is bus powered for now. */
1748 *((u16 *)req->buf) = _udc->remote_wakeup << 1;
1705 retval = 0; 1749 retval = 0;
1706 } else if ((setup->bRequestType & USB_RECIP_MASK) \ 1750 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1707 == USB_RECIP_ENDPOINT) { 1751 == USB_RECIP_ENDPOINT) {
@@ -1730,28 +1774,48 @@ __acquires(mEp->lock)
1730} 1774}
1731 1775
1732/** 1776/**
1777 * isr_setup_status_complete: setup_status request complete function
1778 * @ep: endpoint
1779 * @req: request handled
1780 *
1781 * Caller must release lock. Put the port in test mode if test mode
1782 * feature is selected.
1783 */
1784static void
1785isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1786{
1787 struct ci13xxx *udc = req->context;
1788 unsigned long flags;
1789
1790 trace("%p, %p", ep, req);
1791
1792 spin_lock_irqsave(udc->lock, flags);
1793 if (udc->test_mode)
1794 hw_port_test_set(udc->test_mode);
1795 spin_unlock_irqrestore(udc->lock, flags);
1796}
1797
1798/**
1733 * isr_setup_status_phase: queues the status phase of a setup transation 1799 * isr_setup_status_phase: queues the status phase of a setup transation
1734 * @mEp: endpoint 1800 * @udc: udc struct
1735 * 1801 *
1736 * This function returns an error code 1802 * This function returns an error code
1737 */ 1803 */
1738static int isr_setup_status_phase(struct ci13xxx_ep *mEp) 1804static int isr_setup_status_phase(struct ci13xxx *udc)
1739__releases(mEp->lock) 1805__releases(mEp->lock)
1740__acquires(mEp->lock) 1806__acquires(mEp->lock)
1741{ 1807{
1742 int retval; 1808 int retval;
1809 struct ci13xxx_ep *mEp;
1743 1810
1744 trace("%p", mEp); 1811 trace("%p", udc);
1745
1746 /* mEp is always valid & configured */
1747
1748 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1749 mEp->dir = (mEp->dir == TX) ? RX : TX;
1750 1812
1751 mEp->status->no_interrupt = 1; 1813 mEp = (udc->ep0_dir == TX) ? &udc->ep0out : &udc->ep0in;
1814 udc->status->context = udc;
1815 udc->status->complete = isr_setup_status_complete;
1752 1816
1753 spin_unlock(mEp->lock); 1817 spin_unlock(mEp->lock);
1754 retval = usb_ep_queue(&mEp->ep, mEp->status, GFP_ATOMIC); 1818 retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
1755 spin_lock(mEp->lock); 1819 spin_lock(mEp->lock);
1756 1820
1757 return retval; 1821 return retval;
@@ -1768,40 +1832,37 @@ static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
1768__releases(mEp->lock) 1832__releases(mEp->lock)
1769__acquires(mEp->lock) 1833__acquires(mEp->lock)
1770{ 1834{
1771 struct ci13xxx_req *mReq; 1835 struct ci13xxx_req *mReq, *mReqTemp;
1772 int retval; 1836 struct ci13xxx_ep *mEpTemp = mEp;
1837 int uninitialized_var(retval);
1773 1838
1774 trace("%p", mEp); 1839 trace("%p", mEp);
1775 1840
1776 if (list_empty(&mEp->qh[mEp->dir].queue)) 1841 if (list_empty(&mEp->qh.queue))
1777 return -EINVAL; 1842 return -EINVAL;
1778 1843
1779 /* pop oldest request */ 1844 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
1780 mReq = list_entry(mEp->qh[mEp->dir].queue.next, 1845 queue) {
1781 struct ci13xxx_req, queue); 1846 retval = _hardware_dequeue(mEp, mReq);
1782 list_del_init(&mReq->queue); 1847 if (retval < 0)
1783 1848 break;
1784 retval = _hardware_dequeue(mEp, mReq); 1849 list_del_init(&mReq->queue);
1785 if (retval < 0) { 1850 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
1786 dbg_event(_usb_addr(mEp), "DONE", retval); 1851 if (mReq->req.complete != NULL) {
1787 goto done; 1852 spin_unlock(mEp->lock);
1788 } 1853 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
1789 1854 mReq->req.length)
1790 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval); 1855 mEpTemp = &_udc->ep0in;
1791 1856 mReq->req.complete(&mEpTemp->ep, &mReq->req);
1792 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) { 1857 spin_lock(mEp->lock);
1793 spin_unlock(mEp->lock); 1858 }
1794 mReq->req.complete(&mEp->ep, &mReq->req);
1795 spin_lock(mEp->lock);
1796 } 1859 }
1797 1860
1798 if (!list_empty(&mEp->qh[mEp->dir].queue)) { 1861 if (retval == -EBUSY)
1799 mReq = list_entry(mEp->qh[mEp->dir].queue.next, 1862 retval = 0;
1800 struct ci13xxx_req, queue); 1863 if (retval < 0)
1801 _hardware_enqueue(mEp, mReq); 1864 dbg_event(_usb_addr(mEp), "DONE", retval);
1802 }
1803 1865
1804 done:
1805 return retval; 1866 return retval;
1806} 1867}
1807 1868
@@ -1816,6 +1877,7 @@ __releases(udc->lock)
1816__acquires(udc->lock) 1877__acquires(udc->lock)
1817{ 1878{
1818 unsigned i; 1879 unsigned i;
1880 u8 tmode = 0;
1819 1881
1820 trace("%p", udc); 1882 trace("%p", udc);
1821 1883
@@ -1826,19 +1888,17 @@ __acquires(udc->lock)
1826 1888
1827 for (i = 0; i < hw_ep_max; i++) { 1889 for (i = 0; i < hw_ep_max; i++) {
1828 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; 1890 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1829 int type, num, err = -EINVAL; 1891 int type, num, dir, err = -EINVAL;
1830 struct usb_ctrlrequest req; 1892 struct usb_ctrlrequest req;
1831 1893
1832
1833 if (mEp->desc == NULL) 1894 if (mEp->desc == NULL)
1834 continue; /* not configured */ 1895 continue; /* not configured */
1835 1896
1836 if ((mEp->dir == RX && hw_test_and_clear_complete(i)) || 1897 if (hw_test_and_clear_complete(i)) {
1837 (mEp->dir == TX && hw_test_and_clear_complete(i + 16))) {
1838 err = isr_tr_complete_low(mEp); 1898 err = isr_tr_complete_low(mEp);
1839 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { 1899 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1840 if (err > 0) /* needs status phase */ 1900 if (err > 0) /* needs status phase */
1841 err = isr_setup_status_phase(mEp); 1901 err = isr_setup_status_phase(udc);
1842 if (err < 0) { 1902 if (err < 0) {
1843 dbg_event(_usb_addr(mEp), 1903 dbg_event(_usb_addr(mEp),
1844 "ERROR", err); 1904 "ERROR", err);
@@ -1859,36 +1919,56 @@ __acquires(udc->lock)
1859 continue; 1919 continue;
1860 } 1920 }
1861 1921
1922 /*
1923 * Flush data and handshake transactions of previous
1924 * setup packet.
1925 */
1926 _ep_nuke(&udc->ep0out);
1927 _ep_nuke(&udc->ep0in);
1928
1862 /* read_setup_packet */ 1929 /* read_setup_packet */
1863 do { 1930 do {
1864 hw_test_and_set_setup_guard(); 1931 hw_test_and_set_setup_guard();
1865 memcpy(&req, &mEp->qh[RX].ptr->setup, sizeof(req)); 1932 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
1866 } while (!hw_test_and_clear_setup_guard()); 1933 } while (!hw_test_and_clear_setup_guard());
1867 1934
1868 type = req.bRequestType; 1935 type = req.bRequestType;
1869 1936
1870 mEp->dir = (type & USB_DIR_IN) ? TX : RX; 1937 udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1871 1938
1872 dbg_setup(_usb_addr(mEp), &req); 1939 dbg_setup(_usb_addr(mEp), &req);
1873 1940
1874 switch (req.bRequest) { 1941 switch (req.bRequest) {
1875 case USB_REQ_CLEAR_FEATURE: 1942 case USB_REQ_CLEAR_FEATURE:
1876 if (type != (USB_DIR_OUT|USB_RECIP_ENDPOINT) && 1943 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1877 le16_to_cpu(req.wValue) != USB_ENDPOINT_HALT) 1944 le16_to_cpu(req.wValue) ==
1878 goto delegate; 1945 USB_ENDPOINT_HALT) {
1879 if (req.wLength != 0) 1946 if (req.wLength != 0)
1880 break; 1947 break;
1881 num = le16_to_cpu(req.wIndex); 1948 num = le16_to_cpu(req.wIndex);
1882 num &= USB_ENDPOINT_NUMBER_MASK; 1949 dir = num & USB_ENDPOINT_DIR_MASK;
1883 if (!udc->ci13xxx_ep[num].wedge) { 1950 num &= USB_ENDPOINT_NUMBER_MASK;
1884 spin_unlock(udc->lock); 1951 if (dir) /* TX */
1885 err = usb_ep_clear_halt( 1952 num += hw_ep_max/2;
1886 &udc->ci13xxx_ep[num].ep); 1953 if (!udc->ci13xxx_ep[num].wedge) {
1887 spin_lock(udc->lock); 1954 spin_unlock(udc->lock);
1888 if (err) 1955 err = usb_ep_clear_halt(
1956 &udc->ci13xxx_ep[num].ep);
1957 spin_lock(udc->lock);
1958 if (err)
1959 break;
1960 }
1961 err = isr_setup_status_phase(udc);
1962 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1963 le16_to_cpu(req.wValue) ==
1964 USB_DEVICE_REMOTE_WAKEUP) {
1965 if (req.wLength != 0)
1889 break; 1966 break;
1967 udc->remote_wakeup = 0;
1968 err = isr_setup_status_phase(udc);
1969 } else {
1970 goto delegate;
1890 } 1971 }
1891 err = isr_setup_status_phase(mEp);
1892 break; 1972 break;
1893 case USB_REQ_GET_STATUS: 1973 case USB_REQ_GET_STATUS:
1894 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) && 1974 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
@@ -1898,7 +1978,7 @@ __acquires(udc->lock)
1898 if (le16_to_cpu(req.wLength) != 2 || 1978 if (le16_to_cpu(req.wLength) != 2 ||
1899 le16_to_cpu(req.wValue) != 0) 1979 le16_to_cpu(req.wValue) != 0)
1900 break; 1980 break;
1901 err = isr_get_status_response(mEp, &req); 1981 err = isr_get_status_response(udc, &req);
1902 break; 1982 break;
1903 case USB_REQ_SET_ADDRESS: 1983 case USB_REQ_SET_ADDRESS:
1904 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE)) 1984 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
@@ -1909,28 +1989,59 @@ __acquires(udc->lock)
1909 err = hw_usb_set_address((u8)le16_to_cpu(req.wValue)); 1989 err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
1910 if (err) 1990 if (err)
1911 break; 1991 break;
1912 err = isr_setup_status_phase(mEp); 1992 err = isr_setup_status_phase(udc);
1913 break; 1993 break;
1914 case USB_REQ_SET_FEATURE: 1994 case USB_REQ_SET_FEATURE:
1915 if (type != (USB_DIR_OUT|USB_RECIP_ENDPOINT) && 1995 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1916 le16_to_cpu(req.wValue) != USB_ENDPOINT_HALT) 1996 le16_to_cpu(req.wValue) ==
1917 goto delegate; 1997 USB_ENDPOINT_HALT) {
1918 if (req.wLength != 0) 1998 if (req.wLength != 0)
1919 break; 1999 break;
1920 num = le16_to_cpu(req.wIndex); 2000 num = le16_to_cpu(req.wIndex);
1921 num &= USB_ENDPOINT_NUMBER_MASK; 2001 dir = num & USB_ENDPOINT_DIR_MASK;
2002 num &= USB_ENDPOINT_NUMBER_MASK;
2003 if (dir) /* TX */
2004 num += hw_ep_max/2;
1922 2005
1923 spin_unlock(udc->lock); 2006 spin_unlock(udc->lock);
1924 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep); 2007 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
1925 spin_lock(udc->lock); 2008 spin_lock(udc->lock);
1926 if (err) 2009 if (!err)
1927 break; 2010 isr_setup_status_phase(udc);
1928 err = isr_setup_status_phase(mEp); 2011 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
2012 if (req.wLength != 0)
2013 break;
2014 switch (le16_to_cpu(req.wValue)) {
2015 case USB_DEVICE_REMOTE_WAKEUP:
2016 udc->remote_wakeup = 1;
2017 err = isr_setup_status_phase(udc);
2018 break;
2019 case USB_DEVICE_TEST_MODE:
2020 tmode = le16_to_cpu(req.wIndex) >> 8;
2021 switch (tmode) {
2022 case TEST_J:
2023 case TEST_K:
2024 case TEST_SE0_NAK:
2025 case TEST_PACKET:
2026 case TEST_FORCE_EN:
2027 udc->test_mode = tmode;
2028 err = isr_setup_status_phase(
2029 udc);
2030 break;
2031 default:
2032 break;
2033 }
2034 default:
2035 goto delegate;
2036 }
2037 } else {
2038 goto delegate;
2039 }
1929 break; 2040 break;
1930 default: 2041 default:
1931delegate: 2042delegate:
1932 if (req.wLength == 0) /* no data phase */ 2043 if (req.wLength == 0) /* no data phase */
1933 mEp->dir = TX; 2044 udc->ep0_dir = TX;
1934 2045
1935 spin_unlock(udc->lock); 2046 spin_unlock(udc->lock);
1936 err = udc->driver->setup(&udc->gadget, &req); 2047 err = udc->driver->setup(&udc->gadget, &req);
@@ -1961,7 +2072,7 @@ static int ep_enable(struct usb_ep *ep,
1961 const struct usb_endpoint_descriptor *desc) 2072 const struct usb_endpoint_descriptor *desc)
1962{ 2073{
1963 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); 2074 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1964 int direction, retval = 0; 2075 int retval = 0;
1965 unsigned long flags; 2076 unsigned long flags;
1966 2077
1967 trace("%p, %p", ep, desc); 2078 trace("%p, %p", ep, desc);
@@ -1975,7 +2086,7 @@ static int ep_enable(struct usb_ep *ep,
1975 2086
1976 mEp->desc = desc; 2087 mEp->desc = desc;
1977 2088
1978 if (!list_empty(&mEp->qh[mEp->dir].queue)) 2089 if (!list_empty(&mEp->qh.queue))
1979 warn("enabling a non-empty endpoint!"); 2090 warn("enabling a non-empty endpoint!");
1980 2091
1981 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX; 2092 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
@@ -1984,30 +2095,28 @@ static int ep_enable(struct usb_ep *ep,
1984 2095
1985 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize); 2096 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize);
1986 2097
1987 direction = mEp->dir; 2098 dbg_event(_usb_addr(mEp), "ENABLE", 0);
1988 do {
1989 dbg_event(_usb_addr(mEp), "ENABLE", 0);
1990 2099
1991 mEp->qh[mEp->dir].ptr->cap = 0; 2100 mEp->qh.ptr->cap = 0;
1992 2101
1993 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) 2102 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1994 mEp->qh[mEp->dir].ptr->cap |= QH_IOS; 2103 mEp->qh.ptr->cap |= QH_IOS;
1995 else if (mEp->type == USB_ENDPOINT_XFER_ISOC) 2104 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1996 mEp->qh[mEp->dir].ptr->cap &= ~QH_MULT; 2105 mEp->qh.ptr->cap &= ~QH_MULT;
1997 else 2106 else
1998 mEp->qh[mEp->dir].ptr->cap &= ~QH_ZLT; 2107 mEp->qh.ptr->cap &= ~QH_ZLT;
1999 2108
2000 mEp->qh[mEp->dir].ptr->cap |= 2109 mEp->qh.ptr->cap |=
2001 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT; 2110 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
2002 mEp->qh[mEp->dir].ptr->td.next |= TD_TERMINATE; /* needed? */ 2111 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
2003 2112
2113 /*
2114 * Enable endpoints in the HW other than ep0 as ep0
2115 * is always enabled
2116 */
2117 if (mEp->num)
2004 retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type); 2118 retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
2005 2119
2006 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2007 mEp->dir = (mEp->dir == TX) ? RX : TX;
2008
2009 } while (mEp->dir != direction);
2010
2011 spin_unlock_irqrestore(mEp->lock, flags); 2120 spin_unlock_irqrestore(mEp->lock, flags);
2012 return retval; 2121 return retval;
2013} 2122}
@@ -2061,7 +2170,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2061{ 2170{
2062 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); 2171 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2063 struct ci13xxx_req *mReq = NULL; 2172 struct ci13xxx_req *mReq = NULL;
2064 unsigned long flags;
2065 2173
2066 trace("%p, %i", ep, gfp_flags); 2174 trace("%p, %i", ep, gfp_flags);
2067 2175
@@ -2070,8 +2178,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2070 return NULL; 2178 return NULL;
2071 } 2179 }
2072 2180
2073 spin_lock_irqsave(mEp->lock, flags);
2074
2075 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); 2181 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2076 if (mReq != NULL) { 2182 if (mReq != NULL) {
2077 INIT_LIST_HEAD(&mReq->queue); 2183 INIT_LIST_HEAD(&mReq->queue);
@@ -2086,8 +2192,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2086 2192
2087 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL); 2193 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2088 2194
2089 spin_unlock_irqrestore(mEp->lock, flags);
2090
2091 return (mReq == NULL) ? NULL : &mReq->req; 2195 return (mReq == NULL) ? NULL : &mReq->req;
2092} 2196}
2093 2197
@@ -2143,11 +2247,15 @@ static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2143 2247
2144 spin_lock_irqsave(mEp->lock, flags); 2248 spin_lock_irqsave(mEp->lock, flags);
2145 2249
2146 if (mEp->type == USB_ENDPOINT_XFER_CONTROL && 2250 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
2147 !list_empty(&mEp->qh[mEp->dir].queue)) { 2251 if (req->length)
2148 _ep_nuke(mEp); 2252 mEp = (_udc->ep0_dir == RX) ?
2149 retval = -EOVERFLOW; 2253 &_udc->ep0out : &_udc->ep0in;
2150 warn("endpoint ctrl %X nuked", _usb_addr(mEp)); 2254 if (!list_empty(&mEp->qh.queue)) {
2255 _ep_nuke(mEp);
2256 retval = -EOVERFLOW;
2257 warn("endpoint ctrl %X nuked", _usb_addr(mEp));
2258 }
2151 } 2259 }
2152 2260
2153 /* first nuke then test link, e.g. previous status has not sent */ 2261 /* first nuke then test link, e.g. previous status has not sent */
@@ -2157,8 +2265,8 @@ static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2157 goto done; 2265 goto done;
2158 } 2266 }
2159 2267
2160 if (req->length > (4 * PAGE_SIZE)) { 2268 if (req->length > (4 * CI13XXX_PAGE_SIZE)) {
2161 req->length = (4 * PAGE_SIZE); 2269 req->length = (4 * CI13XXX_PAGE_SIZE);
2162 retval = -EMSGSIZE; 2270 retval = -EMSGSIZE;
2163 warn("request length truncated"); 2271 warn("request length truncated");
2164 } 2272 }
@@ -2168,13 +2276,15 @@ static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2168 /* push request */ 2276 /* push request */
2169 mReq->req.status = -EINPROGRESS; 2277 mReq->req.status = -EINPROGRESS;
2170 mReq->req.actual = 0; 2278 mReq->req.actual = 0;
2171 list_add_tail(&mReq->queue, &mEp->qh[mEp->dir].queue);
2172 2279
2173 retval = _hardware_enqueue(mEp, mReq); 2280 retval = _hardware_enqueue(mEp, mReq);
2174 if (retval == -EALREADY || retval == -EBUSY) { 2281
2282 if (retval == -EALREADY) {
2175 dbg_event(_usb_addr(mEp), "QUEUE", retval); 2283 dbg_event(_usb_addr(mEp), "QUEUE", retval);
2176 retval = 0; 2284 retval = 0;
2177 } 2285 }
2286 if (!retval)
2287 list_add_tail(&mReq->queue, &mEp->qh.queue);
2178 2288
2179 done: 2289 done:
2180 spin_unlock_irqrestore(mEp->lock, flags); 2290 spin_unlock_irqrestore(mEp->lock, flags);
@@ -2194,22 +2304,28 @@ static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2194 2304
2195 trace("%p, %p", ep, req); 2305 trace("%p, %p", ep, req);
2196 2306
2197 if (ep == NULL || req == NULL || mEp->desc == NULL || 2307 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
2198 list_empty(&mReq->queue) || list_empty(&mEp->qh[mEp->dir].queue)) 2308 mEp->desc == NULL || list_empty(&mReq->queue) ||
2309 list_empty(&mEp->qh.queue))
2199 return -EINVAL; 2310 return -EINVAL;
2200 2311
2201 spin_lock_irqsave(mEp->lock, flags); 2312 spin_lock_irqsave(mEp->lock, flags);
2202 2313
2203 dbg_event(_usb_addr(mEp), "DEQUEUE", 0); 2314 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
2204 2315
2205 if (mReq->req.status == -EALREADY) 2316 hw_ep_flush(mEp->num, mEp->dir);
2206 _hardware_dequeue(mEp, mReq);
2207 2317
2208 /* pop request */ 2318 /* pop request */
2209 list_del_init(&mReq->queue); 2319 list_del_init(&mReq->queue);
2320 if (mReq->map) {
2321 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
2322 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2323 mReq->req.dma = 0;
2324 mReq->map = 0;
2325 }
2210 req->status = -ECONNRESET; 2326 req->status = -ECONNRESET;
2211 2327
2212 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) { 2328 if (mReq->req.complete != NULL) {
2213 spin_unlock(mEp->lock); 2329 spin_unlock(mEp->lock);
2214 mReq->req.complete(&mEp->ep, &mReq->req); 2330 mReq->req.complete(&mEp->ep, &mReq->req);
2215 spin_lock(mEp->lock); 2331 spin_lock(mEp->lock);
@@ -2240,7 +2356,7 @@ static int ep_set_halt(struct usb_ep *ep, int value)
2240#ifndef STALL_IN 2356#ifndef STALL_IN
2241 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */ 2357 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2242 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX && 2358 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2243 !list_empty(&mEp->qh[mEp->dir].queue)) { 2359 !list_empty(&mEp->qh.queue)) {
2244 spin_unlock_irqrestore(mEp->lock, flags); 2360 spin_unlock_irqrestore(mEp->lock, flags);
2245 return -EAGAIN; 2361 return -EAGAIN;
2246 } 2362 }
@@ -2332,30 +2448,104 @@ static const struct usb_ep_ops usb_ep_ops = {
2332/****************************************************************************** 2448/******************************************************************************
2333 * GADGET block 2449 * GADGET block
2334 *****************************************************************************/ 2450 *****************************************************************************/
2451static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
2452{
2453 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2454 unsigned long flags;
2455 int gadget_ready = 0;
2456
2457 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
2458 return -EOPNOTSUPP;
2459
2460 spin_lock_irqsave(udc->lock, flags);
2461 udc->vbus_active = is_active;
2462 if (udc->driver)
2463 gadget_ready = 1;
2464 spin_unlock_irqrestore(udc->lock, flags);
2465
2466 if (gadget_ready) {
2467 if (is_active) {
2468 pm_runtime_get_sync(&_gadget->dev);
2469 hw_device_reset(udc);
2470 hw_device_state(udc->ep0out.qh.dma);
2471 } else {
2472 hw_device_state(0);
2473 if (udc->udc_driver->notify_event)
2474 udc->udc_driver->notify_event(udc,
2475 CI13XXX_CONTROLLER_STOPPED_EVENT);
2476 _gadget_stop_activity(&udc->gadget);
2477 pm_runtime_put_sync(&_gadget->dev);
2478 }
2479 }
2480
2481 return 0;
2482}
2483
2484static int ci13xxx_wakeup(struct usb_gadget *_gadget)
2485{
2486 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2487 unsigned long flags;
2488 int ret = 0;
2489
2490 trace();
2491
2492 spin_lock_irqsave(udc->lock, flags);
2493 if (!udc->remote_wakeup) {
2494 ret = -EOPNOTSUPP;
2495 dbg_trace("remote wakeup feature is not enabled\n");
2496 goto out;
2497 }
2498 if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) {
2499 ret = -EINVAL;
2500 dbg_trace("port is not suspended\n");
2501 goto out;
2502 }
2503 hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR);
2504out:
2505 spin_unlock_irqrestore(udc->lock, flags);
2506 return ret;
2507}
2508
2509static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2510{
2511 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2512
2513 if (udc->transceiver)
2514 return otg_set_power(udc->transceiver, mA);
2515 return -ENOTSUPP;
2516}
2517
2335/** 2518/**
2336 * Device operations part of the API to the USB controller hardware, 2519 * Device operations part of the API to the USB controller hardware,
2337 * which don't involve endpoints (or i/o) 2520 * which don't involve endpoints (or i/o)
2338 * Check "usb_gadget.h" for details 2521 * Check "usb_gadget.h" for details
2339 */ 2522 */
2340static const struct usb_gadget_ops usb_gadget_ops; 2523static const struct usb_gadget_ops usb_gadget_ops = {
2524 .vbus_session = ci13xxx_vbus_session,
2525 .wakeup = ci13xxx_wakeup,
2526 .vbus_draw = ci13xxx_vbus_draw,
2527};
2341 2528
2342/** 2529/**
2343 * usb_gadget_register_driver: register a gadget driver 2530 * usb_gadget_probe_driver: register a gadget driver
2531 * @driver: the driver being registered
2532 * @bind: the driver's bind callback
2344 * 2533 *
2345 * Check usb_gadget_register_driver() at "usb_gadget.h" for details 2534 * Check usb_gadget_probe_driver() at <linux/usb/gadget.h> for details.
2346 * Interrupts are enabled here 2535 * Interrupts are enabled here.
2347 */ 2536 */
2348int usb_gadget_register_driver(struct usb_gadget_driver *driver) 2537int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2538 int (*bind)(struct usb_gadget *))
2349{ 2539{
2350 struct ci13xxx *udc = _udc; 2540 struct ci13xxx *udc = _udc;
2351 unsigned long i, k, flags; 2541 unsigned long flags;
2542 int i, j;
2352 int retval = -ENOMEM; 2543 int retval = -ENOMEM;
2353 2544
2354 trace("%p", driver); 2545 trace("%p", driver);
2355 2546
2356 if (driver == NULL || 2547 if (driver == NULL ||
2357 driver->bind == NULL || 2548 bind == NULL ||
2358 driver->unbind == NULL ||
2359 driver->setup == NULL || 2549 driver->setup == NULL ||
2360 driver->disconnect == NULL || 2550 driver->disconnect == NULL ||
2361 driver->suspend == NULL || 2551 driver->suspend == NULL ||
@@ -2369,13 +2559,13 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2369 /* alloc resources */ 2559 /* alloc resources */
2370 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev, 2560 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
2371 sizeof(struct ci13xxx_qh), 2561 sizeof(struct ci13xxx_qh),
2372 64, PAGE_SIZE); 2562 64, CI13XXX_PAGE_SIZE);
2373 if (udc->qh_pool == NULL) 2563 if (udc->qh_pool == NULL)
2374 return -ENOMEM; 2564 return -ENOMEM;
2375 2565
2376 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev, 2566 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
2377 sizeof(struct ci13xxx_td), 2567 sizeof(struct ci13xxx_td),
2378 64, PAGE_SIZE); 2568 64, CI13XXX_PAGE_SIZE);
2379 if (udc->td_pool == NULL) { 2569 if (udc->td_pool == NULL) {
2380 dma_pool_destroy(udc->qh_pool); 2570 dma_pool_destroy(udc->qh_pool);
2381 udc->qh_pool = NULL; 2571 udc->qh_pool = NULL;
@@ -2386,68 +2576,88 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2386 2576
2387 info("hw_ep_max = %d", hw_ep_max); 2577 info("hw_ep_max = %d", hw_ep_max);
2388 2578
2389 udc->driver = driver;
2390 udc->gadget.ops = NULL;
2391 udc->gadget.dev.driver = NULL; 2579 udc->gadget.dev.driver = NULL;
2392 2580
2393 retval = 0; 2581 retval = 0;
2394 for (i = 0; i < hw_ep_max; i++) { 2582 for (i = 0; i < hw_ep_max/2; i++) {
2395 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; 2583 for (j = RX; j <= TX; j++) {
2396 2584 int k = i + j * hw_ep_max/2;
2397 scnprintf(mEp->name, sizeof(mEp->name), "ep%i", (int)i); 2585 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
2398 2586
2399 mEp->lock = udc->lock; 2587 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
2400 mEp->device = &udc->gadget.dev; 2588 (j == TX) ? "in" : "out");
2401 mEp->td_pool = udc->td_pool; 2589
2402 2590 mEp->lock = udc->lock;
2403 mEp->ep.name = mEp->name; 2591 mEp->device = &udc->gadget.dev;
2404 mEp->ep.ops = &usb_ep_ops; 2592 mEp->td_pool = udc->td_pool;
2405 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX; 2593
2406 2594 mEp->ep.name = mEp->name;
2407 /* this allocation cannot be random */ 2595 mEp->ep.ops = &usb_ep_ops;
2408 for (k = RX; k <= TX; k++) { 2596 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
2409 INIT_LIST_HEAD(&mEp->qh[k].queue); 2597
2410 mEp->qh[k].ptr = dma_pool_alloc(udc->qh_pool, 2598 INIT_LIST_HEAD(&mEp->qh.queue);
2411 GFP_KERNEL, 2599 spin_unlock_irqrestore(udc->lock, flags);
2412 &mEp->qh[k].dma); 2600 mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
2413 if (mEp->qh[k].ptr == NULL) 2601 &mEp->qh.dma);
2602 spin_lock_irqsave(udc->lock, flags);
2603 if (mEp->qh.ptr == NULL)
2414 retval = -ENOMEM; 2604 retval = -ENOMEM;
2415 else 2605 else
2416 memset(mEp->qh[k].ptr, 0, 2606 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
2417 sizeof(*mEp->qh[k].ptr)); 2607
2418 } 2608 /* skip ep0 out and in endpoints */
2419 if (i == 0) 2609 if (i == 0)
2420 udc->gadget.ep0 = &mEp->ep; 2610 continue;
2421 else 2611
2422 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list); 2612 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2613 }
2423 } 2614 }
2424 if (retval) 2615 if (retval)
2425 goto done; 2616 goto done;
2617 spin_unlock_irqrestore(udc->lock, flags);
2618 retval = usb_ep_enable(&udc->ep0out.ep, &ctrl_endpt_out_desc);
2619 if (retval)
2620 return retval;
2621 retval = usb_ep_enable(&udc->ep0in.ep, &ctrl_endpt_in_desc);
2622 if (retval)
2623 return retval;
2624 spin_lock_irqsave(udc->lock, flags);
2426 2625
2626 udc->gadget.ep0 = &udc->ep0in.ep;
2427 /* bind gadget */ 2627 /* bind gadget */
2428 driver->driver.bus = NULL; 2628 driver->driver.bus = NULL;
2429 udc->gadget.ops = &usb_gadget_ops;
2430 udc->gadget.dev.driver = &driver->driver; 2629 udc->gadget.dev.driver = &driver->driver;
2431 2630
2432 spin_unlock_irqrestore(udc->lock, flags); 2631 spin_unlock_irqrestore(udc->lock, flags);
2433 retval = driver->bind(&udc->gadget); /* MAY SLEEP */ 2632 retval = bind(&udc->gadget); /* MAY SLEEP */
2434 spin_lock_irqsave(udc->lock, flags); 2633 spin_lock_irqsave(udc->lock, flags);
2435 2634
2436 if (retval) { 2635 if (retval) {
2437 udc->gadget.ops = NULL;
2438 udc->gadget.dev.driver = NULL; 2636 udc->gadget.dev.driver = NULL;
2439 goto done; 2637 goto done;
2440 } 2638 }
2441 2639
2442 retval = hw_device_state(udc->ci13xxx_ep[0].qh[RX].dma); 2640 udc->driver = driver;
2641 pm_runtime_get_sync(&udc->gadget.dev);
2642 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
2643 if (udc->vbus_active) {
2644 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
2645 hw_device_reset(udc);
2646 } else {
2647 pm_runtime_put_sync(&udc->gadget.dev);
2648 goto done;
2649 }
2650 }
2651
2652 retval = hw_device_state(udc->ep0out.qh.dma);
2653 if (retval)
2654 pm_runtime_put_sync(&udc->gadget.dev);
2443 2655
2444 done: 2656 done:
2445 spin_unlock_irqrestore(udc->lock, flags); 2657 spin_unlock_irqrestore(udc->lock, flags);
2446 if (retval)
2447 usb_gadget_unregister_driver(driver);
2448 return retval; 2658 return retval;
2449} 2659}
2450EXPORT_SYMBOL(usb_gadget_register_driver); 2660EXPORT_SYMBOL(usb_gadget_probe_driver);
2451 2661
2452/** 2662/**
2453 * usb_gadget_unregister_driver: unregister a gadget driver 2663 * usb_gadget_unregister_driver: unregister a gadget driver
@@ -2457,12 +2667,11 @@ EXPORT_SYMBOL(usb_gadget_register_driver);
2457int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 2667int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2458{ 2668{
2459 struct ci13xxx *udc = _udc; 2669 struct ci13xxx *udc = _udc;
2460 unsigned long i, k, flags; 2670 unsigned long i, flags;
2461 2671
2462 trace("%p", driver); 2672 trace("%p", driver);
2463 2673
2464 if (driver == NULL || 2674 if (driver == NULL ||
2465 driver->bind == NULL ||
2466 driver->unbind == NULL || 2675 driver->unbind == NULL ||
2467 driver->setup == NULL || 2676 driver->setup == NULL ||
2468 driver->disconnect == NULL || 2677 driver->disconnect == NULL ||
@@ -2473,35 +2682,35 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2473 2682
2474 spin_lock_irqsave(udc->lock, flags); 2683 spin_lock_irqsave(udc->lock, flags);
2475 2684
2476 hw_device_state(0); 2685 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
2477 2686 udc->vbus_active) {
2478 /* unbind gadget */ 2687 hw_device_state(0);
2479 if (udc->gadget.ops != NULL) { 2688 if (udc->udc_driver->notify_event)
2689 udc->udc_driver->notify_event(udc,
2690 CI13XXX_CONTROLLER_STOPPED_EVENT);
2480 _gadget_stop_activity(&udc->gadget); 2691 _gadget_stop_activity(&udc->gadget);
2692 pm_runtime_put(&udc->gadget.dev);
2693 }
2481 2694
2482 spin_unlock_irqrestore(udc->lock, flags); 2695 /* unbind gadget */
2483 driver->unbind(&udc->gadget); /* MAY SLEEP */ 2696 spin_unlock_irqrestore(udc->lock, flags);
2484 spin_lock_irqsave(udc->lock, flags); 2697 driver->unbind(&udc->gadget); /* MAY SLEEP */
2698 spin_lock_irqsave(udc->lock, flags);
2485 2699
2486 udc->gadget.ops = NULL; 2700 udc->gadget.dev.driver = NULL;
2487 udc->gadget.dev.driver = NULL;
2488 }
2489 2701
2490 /* free resources */ 2702 /* free resources */
2491 for (i = 0; i < hw_ep_max; i++) { 2703 for (i = 0; i < hw_ep_max; i++) {
2492 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; 2704 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2493 2705
2494 if (i == 0) 2706 if (!list_empty(&mEp->ep.ep_list))
2495 udc->gadget.ep0 = NULL;
2496 else if (!list_empty(&mEp->ep.ep_list))
2497 list_del_init(&mEp->ep.ep_list); 2707 list_del_init(&mEp->ep.ep_list);
2498 2708
2499 for (k = RX; k <= TX; k++) 2709 if (mEp->qh.ptr != NULL)
2500 if (mEp->qh[k].ptr != NULL) 2710 dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
2501 dma_pool_free(udc->qh_pool,
2502 mEp->qh[k].ptr, mEp->qh[k].dma);
2503 } 2711 }
2504 2712
2713 udc->gadget.ep0 = NULL;
2505 udc->driver = NULL; 2714 udc->driver = NULL;
2506 2715
2507 spin_unlock_irqrestore(udc->lock, flags); 2716 spin_unlock_irqrestore(udc->lock, flags);
@@ -2542,6 +2751,14 @@ static irqreturn_t udc_irq(void)
2542 } 2751 }
2543 2752
2544 spin_lock(udc->lock); 2753 spin_lock(udc->lock);
2754
2755 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2756 if (hw_cread(CAP_USBMODE, USBMODE_CM) !=
2757 USBMODE_CM_DEVICE) {
2758 spin_unlock(udc->lock);
2759 return IRQ_NONE;
2760 }
2761 }
2545 intr = hw_test_and_clear_intr_active(); 2762 intr = hw_test_and_clear_intr_active();
2546 if (intr) { 2763 if (intr) {
2547 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr; 2764 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
@@ -2557,6 +2774,12 @@ static irqreturn_t udc_irq(void)
2557 isr_statistics.pci++; 2774 isr_statistics.pci++;
2558 udc->gadget.speed = hw_port_is_high_speed() ? 2775 udc->gadget.speed = hw_port_is_high_speed() ?
2559 USB_SPEED_HIGH : USB_SPEED_FULL; 2776 USB_SPEED_HIGH : USB_SPEED_FULL;
2777 if (udc->suspended) {
2778 spin_unlock(udc->lock);
2779 udc->driver->resume(&udc->gadget);
2780 spin_lock(udc->lock);
2781 udc->suspended = 0;
2782 }
2560 } 2783 }
2561 if (USBi_UEI & intr) 2784 if (USBi_UEI & intr)
2562 isr_statistics.uei++; 2785 isr_statistics.uei++;
@@ -2564,8 +2787,15 @@ static irqreturn_t udc_irq(void)
2564 isr_statistics.ui++; 2787 isr_statistics.ui++;
2565 isr_tr_complete_handler(udc); 2788 isr_tr_complete_handler(udc);
2566 } 2789 }
2567 if (USBi_SLI & intr) 2790 if (USBi_SLI & intr) {
2791 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
2792 udc->suspended = 1;
2793 spin_unlock(udc->lock);
2794 udc->driver->suspend(&udc->gadget);
2795 spin_lock(udc->lock);
2796 }
2568 isr_statistics.sli++; 2797 isr_statistics.sli++;
2798 }
2569 retval = IRQ_HANDLED; 2799 retval = IRQ_HANDLED;
2570 } else { 2800 } else {
2571 isr_statistics.none++; 2801 isr_statistics.none++;
@@ -2600,14 +2830,16 @@ static void udc_release(struct device *dev)
2600 * No interrupts active, the IRQ has not been requested yet 2830 * No interrupts active, the IRQ has not been requested yet
2601 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask 2831 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2602 */ 2832 */
2603static int udc_probe(struct device *dev, void __iomem *regs, const char *name) 2833static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2834 void __iomem *regs)
2604{ 2835{
2605 struct ci13xxx *udc; 2836 struct ci13xxx *udc;
2606 int retval = 0; 2837 int retval = 0;
2607 2838
2608 trace("%p, %p, %p", dev, regs, name); 2839 trace("%p, %p, %p", dev, regs, name);
2609 2840
2610 if (dev == NULL || regs == NULL || name == NULL) 2841 if (dev == NULL || regs == NULL || driver == NULL ||
2842 driver->name == NULL)
2611 return -EINVAL; 2843 return -EINVAL;
2612 2844
2613 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL); 2845 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
@@ -2615,42 +2847,77 @@ static int udc_probe(struct device *dev, void __iomem *regs, const char *name)
2615 return -ENOMEM; 2847 return -ENOMEM;
2616 2848
2617 udc->lock = &udc_lock; 2849 udc->lock = &udc_lock;
2850 udc->regs = regs;
2851 udc->udc_driver = driver;
2618 2852
2619 retval = hw_device_reset(regs); 2853 udc->gadget.ops = &usb_gadget_ops;
2620 if (retval)
2621 goto done;
2622
2623 udc->gadget.ops = NULL;
2624 udc->gadget.speed = USB_SPEED_UNKNOWN; 2854 udc->gadget.speed = USB_SPEED_UNKNOWN;
2625 udc->gadget.is_dualspeed = 1; 2855 udc->gadget.is_dualspeed = 1;
2626 udc->gadget.is_otg = 0; 2856 udc->gadget.is_otg = 0;
2627 udc->gadget.name = name; 2857 udc->gadget.name = driver->name;
2628 2858
2629 INIT_LIST_HEAD(&udc->gadget.ep_list); 2859 INIT_LIST_HEAD(&udc->gadget.ep_list);
2630 udc->gadget.ep0 = NULL; 2860 udc->gadget.ep0 = NULL;
2631 2861
2632 dev_set_name(&udc->gadget.dev, "gadget"); 2862 dev_set_name(&udc->gadget.dev, "gadget");
2633 udc->gadget.dev.dma_mask = dev->dma_mask; 2863 udc->gadget.dev.dma_mask = dev->dma_mask;
2864 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
2634 udc->gadget.dev.parent = dev; 2865 udc->gadget.dev.parent = dev;
2635 udc->gadget.dev.release = udc_release; 2866 udc->gadget.dev.release = udc_release;
2636 2867
2868 retval = hw_device_init(regs);
2869 if (retval < 0)
2870 goto free_udc;
2871
2872 udc->transceiver = otg_get_transceiver();
2873
2874 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
2875 if (udc->transceiver == NULL) {
2876 retval = -ENODEV;
2877 goto free_udc;
2878 }
2879 }
2880
2881 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
2882 retval = hw_device_reset(udc);
2883 if (retval)
2884 goto put_transceiver;
2885 }
2886
2637 retval = device_register(&udc->gadget.dev); 2887 retval = device_register(&udc->gadget.dev);
2638 if (retval) 2888 if (retval) {
2639 goto done; 2889 put_device(&udc->gadget.dev);
2890 goto put_transceiver;
2891 }
2640 2892
2641#ifdef CONFIG_USB_GADGET_DEBUG_FILES 2893#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2642 retval = dbg_create_files(&udc->gadget.dev); 2894 retval = dbg_create_files(&udc->gadget.dev);
2643#endif 2895#endif
2644 if (retval) { 2896 if (retval)
2645 device_unregister(&udc->gadget.dev); 2897 goto unreg_device;
2646 goto done; 2898
2899 if (udc->transceiver) {
2900 retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
2901 if (retval)
2902 goto remove_dbg;
2647 } 2903 }
2904 pm_runtime_no_callbacks(&udc->gadget.dev);
2905 pm_runtime_enable(&udc->gadget.dev);
2648 2906
2649 _udc = udc; 2907 _udc = udc;
2650 return retval; 2908 return retval;
2651 2909
2652 done:
2653 err("error = %i", retval); 2910 err("error = %i", retval);
2911remove_dbg:
2912#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2913 dbg_remove_files(&udc->gadget.dev);
2914#endif
2915unreg_device:
2916 device_unregister(&udc->gadget.dev);
2917put_transceiver:
2918 if (udc->transceiver)
2919 otg_put_transceiver(udc->transceiver);
2920free_udc:
2654 kfree(udc); 2921 kfree(udc);
2655 _udc = NULL; 2922 _udc = NULL;
2656 return retval; 2923 return retval;
@@ -2670,6 +2937,10 @@ static void udc_remove(void)
2670 return; 2937 return;
2671 } 2938 }
2672 2939
2940 if (udc->transceiver) {
2941 otg_set_peripheral(udc->transceiver, &udc->gadget);
2942 otg_put_transceiver(udc->transceiver);
2943 }
2673#ifdef CONFIG_USB_GADGET_DEBUG_FILES 2944#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2674 dbg_remove_files(&udc->gadget.dev); 2945 dbg_remove_files(&udc->gadget.dev);
2675#endif 2946#endif
@@ -2678,156 +2949,3 @@ static void udc_remove(void)
2678 kfree(udc); 2949 kfree(udc);
2679 _udc = NULL; 2950 _udc = NULL;
2680} 2951}
2681
2682/******************************************************************************
2683 * PCI block
2684 *****************************************************************************/
2685/**
2686 * ci13xxx_pci_irq: interrut handler
2687 * @irq: irq number
2688 * @pdev: USB Device Controller interrupt source
2689 *
2690 * This function returns IRQ_HANDLED if the IRQ has been handled
2691 * This is an ISR don't trace, use attribute interface instead
2692 */
2693static irqreturn_t ci13xxx_pci_irq(int irq, void *pdev)
2694{
2695 if (irq == 0) {
2696 dev_err(&((struct pci_dev *)pdev)->dev, "Invalid IRQ0 usage!");
2697 return IRQ_HANDLED;
2698 }
2699 return udc_irq();
2700}
2701
2702/**
2703 * ci13xxx_pci_probe: PCI probe
2704 * @pdev: USB device controller being probed
2705 * @id: PCI hotplug ID connecting controller to UDC framework
2706 *
2707 * This function returns an error code
2708 * Allocates basic PCI resources for this USB device controller, and then
2709 * invokes the udc_probe() method to start the UDC associated with it
2710 */
2711static int __devinit ci13xxx_pci_probe(struct pci_dev *pdev,
2712 const struct pci_device_id *id)
2713{
2714 void __iomem *regs = NULL;
2715 int retval = 0;
2716
2717 if (id == NULL)
2718 return -EINVAL;
2719
2720 retval = pci_enable_device(pdev);
2721 if (retval)
2722 goto done;
2723
2724 if (!pdev->irq) {
2725 dev_err(&pdev->dev, "No IRQ, check BIOS/PCI setup!");
2726 retval = -ENODEV;
2727 goto disable_device;
2728 }
2729
2730 retval = pci_request_regions(pdev, UDC_DRIVER_NAME);
2731 if (retval)
2732 goto disable_device;
2733
2734 /* BAR 0 holds all the registers */
2735 regs = pci_iomap(pdev, 0, 0);
2736 if (!regs) {
2737 dev_err(&pdev->dev, "Error mapping memory!");
2738 retval = -EFAULT;
2739 goto release_regions;
2740 }
2741 pci_set_drvdata(pdev, (__force void *)regs);
2742
2743 pci_set_master(pdev);
2744 pci_try_set_mwi(pdev);
2745
2746 retval = udc_probe(&pdev->dev, regs, UDC_DRIVER_NAME);
2747 if (retval)
2748 goto iounmap;
2749
2750 /* our device does not have MSI capability */
2751
2752 retval = request_irq(pdev->irq, ci13xxx_pci_irq, IRQF_SHARED,
2753 UDC_DRIVER_NAME, pdev);
2754 if (retval)
2755 goto gadget_remove;
2756
2757 return 0;
2758
2759 gadget_remove:
2760 udc_remove();
2761 iounmap:
2762 pci_iounmap(pdev, regs);
2763 release_regions:
2764 pci_release_regions(pdev);
2765 disable_device:
2766 pci_disable_device(pdev);
2767 done:
2768 return retval;
2769}
2770
2771/**
2772 * ci13xxx_pci_remove: PCI remove
2773 * @pdev: USB Device Controller being removed
2774 *
2775 * Reverses the effect of ci13xxx_pci_probe(),
2776 * first invoking the udc_remove() and then releases
2777 * all PCI resources allocated for this USB device controller
2778 */
2779static void __devexit ci13xxx_pci_remove(struct pci_dev *pdev)
2780{
2781 free_irq(pdev->irq, pdev);
2782 udc_remove();
2783 pci_iounmap(pdev, (__force void __iomem *)pci_get_drvdata(pdev));
2784 pci_release_regions(pdev);
2785 pci_disable_device(pdev);
2786}
2787
2788/**
2789 * PCI device table
2790 * PCI device structure
2791 *
2792 * Check "pci.h" for details
2793 */
2794static DEFINE_PCI_DEVICE_TABLE(ci13xxx_pci_id_table) = {
2795 { PCI_DEVICE(0x153F, 0x1004) },
2796 { PCI_DEVICE(0x153F, 0x1006) },
2797 { 0, 0, 0, 0, 0, 0, 0 /* end: all zeroes */ }
2798};
2799MODULE_DEVICE_TABLE(pci, ci13xxx_pci_id_table);
2800
2801static struct pci_driver ci13xxx_pci_driver = {
2802 .name = UDC_DRIVER_NAME,
2803 .id_table = ci13xxx_pci_id_table,
2804 .probe = ci13xxx_pci_probe,
2805 .remove = __devexit_p(ci13xxx_pci_remove),
2806};
2807
2808/**
2809 * ci13xxx_pci_init: module init
2810 *
2811 * Driver load
2812 */
2813static int __init ci13xxx_pci_init(void)
2814{
2815 return pci_register_driver(&ci13xxx_pci_driver);
2816}
2817module_init(ci13xxx_pci_init);
2818
2819/**
2820 * ci13xxx_pci_exit: module exit
2821 *
2822 * Driver unload
2823 */
2824static void __exit ci13xxx_pci_exit(void)
2825{
2826 pci_unregister_driver(&ci13xxx_pci_driver);
2827}
2828module_exit(ci13xxx_pci_exit);
2829
2830MODULE_AUTHOR("MIPS - David Lopo <dlopo@chipidea.mips.com>");
2831MODULE_DESCRIPTION("MIPS CI13XXX USB Peripheral Controller");
2832MODULE_LICENSE("GPL");
2833MODULE_VERSION("June 2008");
diff --git a/drivers/usb/gadget/ci13xxx_udc.h b/drivers/usb/gadget/ci13xxx_udc.h
index 4026e9cede34..23707775cb43 100644
--- a/drivers/usb/gadget/ci13xxx_udc.h
+++ b/drivers/usb/gadget/ci13xxx_udc.h
@@ -19,7 +19,8 @@
19/****************************************************************************** 19/******************************************************************************
20 * DEFINE 20 * DEFINE
21 *****************************************************************************/ 21 *****************************************************************************/
22#define ENDPT_MAX (16) 22#define CI13XXX_PAGE_SIZE 4096ul /* page size for TD's */
23#define ENDPT_MAX (32)
23#define CTRL_PAYLOAD_MAX (64) 24#define CTRL_PAYLOAD_MAX (64)
24#define RX (0) /* similar to USB_DIR_OUT but can be used as an index */ 25#define RX (0) /* similar to USB_DIR_OUT but can be used as an index */
25#define TX (1) /* similar to USB_DIR_IN but can be used as an index */ 26#define TX (1) /* similar to USB_DIR_IN but can be used as an index */
@@ -32,6 +33,7 @@ struct ci13xxx_td {
32 /* 0 */ 33 /* 0 */
33 u32 next; 34 u32 next;
34#define TD_TERMINATE BIT(0) 35#define TD_TERMINATE BIT(0)
36#define TD_ADDR_MASK (0xFFFFFFEUL << 5)
35 /* 1 */ 37 /* 1 */
36 u32 token; 38 u32 token;
37#define TD_STATUS (0x00FFUL << 0) 39#define TD_STATUS (0x00FFUL << 0)
@@ -73,6 +75,8 @@ struct ci13xxx_req {
73 struct list_head queue; 75 struct list_head queue;
74 struct ci13xxx_td *ptr; 76 struct ci13xxx_td *ptr;
75 dma_addr_t dma; 77 dma_addr_t dma;
78 struct ci13xxx_td *zptr;
79 dma_addr_t zdma;
76}; 80};
77 81
78/* Extension of usb_ep */ 82/* Extension of usb_ep */
@@ -87,8 +91,7 @@ struct ci13xxx_ep {
87 struct list_head queue; 91 struct list_head queue;
88 struct ci13xxx_qh *ptr; 92 struct ci13xxx_qh *ptr;
89 dma_addr_t dma; 93 dma_addr_t dma;
90 } qh[2]; 94 } qh;
91 struct usb_request *status;
92 int wedge; 95 int wedge;
93 96
94 /* global resources */ 97 /* global resources */
@@ -97,17 +100,43 @@ struct ci13xxx_ep {
97 struct dma_pool *td_pool; 100 struct dma_pool *td_pool;
98}; 101};
99 102
103struct ci13xxx;
104struct ci13xxx_udc_driver {
105 const char *name;
106 unsigned long flags;
107#define CI13XXX_REGS_SHARED BIT(0)
108#define CI13XXX_REQUIRE_TRANSCEIVER BIT(1)
109#define CI13XXX_PULLUP_ON_VBUS BIT(2)
110#define CI13XXX_DISABLE_STREAMING BIT(3)
111
112#define CI13XXX_CONTROLLER_RESET_EVENT 0
113#define CI13XXX_CONTROLLER_STOPPED_EVENT 1
114 void (*notify_event) (struct ci13xxx *udc, unsigned event);
115};
116
100/* CI13XXX UDC descriptor & global resources */ 117/* CI13XXX UDC descriptor & global resources */
101struct ci13xxx { 118struct ci13xxx {
102 spinlock_t *lock; /* ctrl register bank access */ 119 spinlock_t *lock; /* ctrl register bank access */
120 void __iomem *regs; /* registers address space */
103 121
104 struct dma_pool *qh_pool; /* DMA pool for queue heads */ 122 struct dma_pool *qh_pool; /* DMA pool for queue heads */
105 struct dma_pool *td_pool; /* DMA pool for transfer descs */ 123 struct dma_pool *td_pool; /* DMA pool for transfer descs */
124 struct usb_request *status; /* ep0 status request */
106 125
107 struct usb_gadget gadget; /* USB slave device */ 126 struct usb_gadget gadget; /* USB slave device */
108 struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX]; /* extended endpts */ 127 struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX]; /* extended endpts */
128 u32 ep0_dir; /* ep0 direction */
129#define ep0out ci13xxx_ep[0]
130#define ep0in ci13xxx_ep[16]
131 u8 remote_wakeup; /* Is remote wakeup feature
132 enabled by the host? */
133 u8 suspended; /* suspended by the host */
134 u8 test_mode; /* the selected test mode */
109 135
110 struct usb_gadget_driver *driver; /* 3rd party gadget driver */ 136 struct usb_gadget_driver *driver; /* 3rd party gadget driver */
137 struct ci13xxx_udc_driver *udc_driver; /* device controller driver */
138 int vbus_active; /* is VBUS active */
139 struct otg_transceiver *transceiver; /* Transceiver struct */
111}; 140};
112 141
113/****************************************************************************** 142/******************************************************************************
@@ -130,6 +159,7 @@ struct ci13xxx {
130#define USBCMD_RS BIT(0) 159#define USBCMD_RS BIT(0)
131#define USBCMD_RST BIT(1) 160#define USBCMD_RST BIT(1)
132#define USBCMD_SUTW BIT(13) 161#define USBCMD_SUTW BIT(13)
162#define USBCMD_ATDTW BIT(14)
133 163
134/* USBSTS & USBINTR */ 164/* USBSTS & USBINTR */
135#define USBi_UI BIT(0) 165#define USBi_UI BIT(0)
@@ -143,6 +173,7 @@ struct ci13xxx {
143#define DEVICEADDR_USBADR (0x7FUL << 25) 173#define DEVICEADDR_USBADR (0x7FUL << 25)
144 174
145/* PORTSC */ 175/* PORTSC */
176#define PORTSC_FPR BIT(6)
146#define PORTSC_SUSP BIT(7) 177#define PORTSC_SUSP BIT(7)
147#define PORTSC_HSP BIT(9) 178#define PORTSC_HSP BIT(9)
148#define PORTSC_PTC (0x0FUL << 16) 179#define PORTSC_PTC (0x0FUL << 16)
@@ -157,6 +188,7 @@ struct ci13xxx {
157#define USBMODE_CM_DEVICE (0x02UL << 0) 188#define USBMODE_CM_DEVICE (0x02UL << 0)
158#define USBMODE_CM_HOST (0x03UL << 0) 189#define USBMODE_CM_HOST (0x03UL << 0)
159#define USBMODE_SLOM BIT(3) 190#define USBMODE_SLOM BIT(3)
191#define USBMODE_SDIS BIT(4)
160 192
161/* ENDPTCTRL */ 193/* ENDPTCTRL */
162#define ENDPTCTRL_RXS BIT(0) 194#define ENDPTCTRL_RXS BIT(0)
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 1160c55de7f2..5cbb1a41c223 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -24,6 +24,7 @@
24#include <linux/kernel.h> 24#include <linux/kernel.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/device.h> 26#include <linux/device.h>
27#include <linux/utsname.h>
27 28
28#include <linux/usb/composite.h> 29#include <linux/usb/composite.h>
29 30
@@ -39,8 +40,9 @@
39#define USB_BUFSIZ 1024 40#define USB_BUFSIZ 1024
40 41
41static struct usb_composite_driver *composite; 42static struct usb_composite_driver *composite;
43static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
42 44
43/* Some systems will need runtime overrides for the product identifers 45/* Some systems will need runtime overrides for the product identifiers
44 * published in the device descriptor, either numbers or strings or both. 46 * published in the device descriptor, either numbers or strings or both.
45 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). 47 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
46 */ 48 */
@@ -69,6 +71,8 @@ static char *iSerialNumber;
69module_param(iSerialNumber, charp, 0); 71module_param(iSerialNumber, charp, 0);
70MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); 72MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
71 73
74static char composite_manufacturer[50];
75
72/*-------------------------------------------------------------------------*/ 76/*-------------------------------------------------------------------------*/
73 77
74/** 78/**
@@ -201,14 +205,14 @@ int usb_function_activate(struct usb_function *function)
201 * usb_interface_id() is called from usb_function.bind() callbacks to 205 * usb_interface_id() is called from usb_function.bind() callbacks to
202 * allocate new interface IDs. The function driver will then store that 206 * allocate new interface IDs. The function driver will then store that
203 * ID in interface, association, CDC union, and other descriptors. It 207 * ID in interface, association, CDC union, and other descriptors. It
204 * will also handle any control requests targetted at that interface, 208 * will also handle any control requests targeted at that interface,
205 * particularly changing its altsetting via set_alt(). There may 209 * particularly changing its altsetting via set_alt(). There may
206 * also be class-specific or vendor-specific requests to handle. 210 * also be class-specific or vendor-specific requests to handle.
207 * 211 *
208 * All interface identifier should be allocated using this routine, to 212 * All interface identifier should be allocated using this routine, to
209 * ensure that for example different functions don't wrongly assign 213 * ensure that for example different functions don't wrongly assign
210 * different meanings to the same identifier. Note that since interface 214 * different meanings to the same identifier. Note that since interface
211 * identifers are configuration-specific, functions used in more than 215 * identifiers are configuration-specific, functions used in more than
212 * one configuration (or more than once in a given configuration) need 216 * one configuration (or more than once in a given configuration) need
213 * multiple versions of the relevant descriptors. 217 * multiple versions of the relevant descriptors.
214 * 218 *
@@ -457,12 +461,23 @@ static int set_config(struct usb_composite_dev *cdev,
457 reset_config(cdev); 461 reset_config(cdev);
458 goto done; 462 goto done;
459 } 463 }
464
465 if (result == USB_GADGET_DELAYED_STATUS) {
466 DBG(cdev,
467 "%s: interface %d (%s) requested delayed status\n",
468 __func__, tmp, f->name);
469 cdev->delayed_status++;
470 DBG(cdev, "delayed_status count %d\n",
471 cdev->delayed_status);
472 }
460 } 473 }
461 474
462 /* when we return, be sure our power usage is valid */ 475 /* when we return, be sure our power usage is valid */
463 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; 476 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
464done: 477done:
465 usb_gadget_vbus_draw(gadget, power); 478 usb_gadget_vbus_draw(gadget, power);
479 if (result >= 0 && cdev->delayed_status)
480 result = USB_GADGET_DELAYED_STATUS;
466 return result; 481 return result;
467} 482}
468 483
@@ -470,18 +485,20 @@ done:
470 * usb_add_config() - add a configuration to a device. 485 * usb_add_config() - add a configuration to a device.
471 * @cdev: wraps the USB gadget 486 * @cdev: wraps the USB gadget
472 * @config: the configuration, with bConfigurationValue assigned 487 * @config: the configuration, with bConfigurationValue assigned
488 * @bind: the configuration's bind function
473 * Context: single threaded during gadget setup 489 * Context: single threaded during gadget setup
474 * 490 *
475 * One of the main tasks of a composite driver's bind() routine is to 491 * One of the main tasks of a composite @bind() routine is to
476 * add each of the configurations it supports, using this routine. 492 * add each of the configurations it supports, using this routine.
477 * 493 *
478 * This function returns the value of the configuration's bind(), which 494 * This function returns the value of the configuration's @bind(), which
479 * is zero for success else a negative errno value. Binding configurations 495 * is zero for success else a negative errno value. Binding configurations
480 * assigns global resources including string IDs, and per-configuration 496 * assigns global resources including string IDs, and per-configuration
481 * resources such as interface IDs and endpoints. 497 * resources such as interface IDs and endpoints.
482 */ 498 */
483int usb_add_config(struct usb_composite_dev *cdev, 499int usb_add_config(struct usb_composite_dev *cdev,
484 struct usb_configuration *config) 500 struct usb_configuration *config,
501 int (*bind)(struct usb_configuration *))
485{ 502{
486 int status = -EINVAL; 503 int status = -EINVAL;
487 struct usb_configuration *c; 504 struct usb_configuration *c;
@@ -490,7 +507,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
490 config->bConfigurationValue, 507 config->bConfigurationValue,
491 config->label, config); 508 config->label, config);
492 509
493 if (!config->bConfigurationValue || !config->bind) 510 if (!config->bConfigurationValue || !bind)
494 goto done; 511 goto done;
495 512
496 /* Prevent duplicate configuration identifiers */ 513 /* Prevent duplicate configuration identifiers */
@@ -507,7 +524,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
507 INIT_LIST_HEAD(&config->functions); 524 INIT_LIST_HEAD(&config->functions);
508 config->next_interface_id = 0; 525 config->next_interface_id = 0;
509 526
510 status = config->bind(config); 527 status = bind(config);
511 if (status < 0) { 528 if (status < 0) {
512 list_del(&config->list); 529 list_del(&config->list);
513 config->cdev = NULL; 530 config->cdev = NULL;
@@ -533,7 +550,7 @@ int usb_add_config(struct usb_composite_dev *cdev,
533 } 550 }
534 } 551 }
535 552
536 /* set_alt(), or next config->bind(), sets up 553 /* set_alt(), or next bind(), sets up
537 * ep->driver_data as needed. 554 * ep->driver_data as needed.
538 */ 555 */
539 usb_ep_autoconfig_reset(cdev->gadget); 556 usb_ep_autoconfig_reset(cdev->gadget);
@@ -599,6 +616,7 @@ static int get_string(struct usb_composite_dev *cdev,
599 struct usb_configuration *c; 616 struct usb_configuration *c;
600 struct usb_function *f; 617 struct usb_function *f;
601 int len; 618 int len;
619 const char *str;
602 620
603 /* Yes, not only is USB's I18N support probably more than most 621 /* Yes, not only is USB's I18N support probably more than most
604 * folk will ever care about ... also, it's all supported here. 622 * folk will ever care about ... also, it's all supported here.
@@ -638,9 +656,29 @@ static int get_string(struct usb_composite_dev *cdev,
638 return s->bLength; 656 return s->bLength;
639 } 657 }
640 658
641 /* Otherwise, look up and return a specified string. String IDs 659 /* Otherwise, look up and return a specified string. First
642 * are device-scoped, so we look up each string table we're told 660 * check if the string has not been overridden.
643 * about. These lookups are infrequent; simpler-is-better here. 661 */
662 if (cdev->manufacturer_override == id)
663 str = iManufacturer ?: composite->iManufacturer ?:
664 composite_manufacturer;
665 else if (cdev->product_override == id)
666 str = iProduct ?: composite->iProduct;
667 else if (cdev->serial_override == id)
668 str = iSerialNumber;
669 else
670 str = NULL;
671 if (str) {
672 struct usb_gadget_strings strings = {
673 .language = language,
674 .strings = &(struct usb_string) { 0xff, str }
675 };
676 return usb_gadget_get_string(&strings, 0xff, buf);
677 }
678
679 /* String IDs are device-scoped, so we look up each string
680 * table we're told about. These lookups are infrequent;
681 * simpler-is-better here.
644 */ 682 */
645 if (composite->strings) { 683 if (composite->strings) {
646 len = lookup_string(composite->strings, buf, language, id); 684 len = lookup_string(composite->strings, buf, language, id);
@@ -786,7 +824,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
786 */ 824 */
787 req->zero = 0; 825 req->zero = 0;
788 req->complete = composite_setup_complete; 826 req->complete = composite_setup_complete;
789 req->length = USB_BUFSIZ; 827 req->length = 0;
790 gadget->ep0->driver_data = cdev; 828 gadget->ep0->driver_data = cdev;
791 829
792 switch (ctrl->bRequest) { 830 switch (ctrl->bRequest) {
@@ -860,7 +898,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
860 case USB_REQ_SET_INTERFACE: 898 case USB_REQ_SET_INTERFACE:
861 if (ctrl->bRequestType != USB_RECIP_INTERFACE) 899 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
862 goto unknown; 900 goto unknown;
863 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 901 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
864 break; 902 break;
865 f = cdev->config->interface[intf]; 903 f = cdev->config->interface[intf];
866 if (!f) 904 if (!f)
@@ -868,11 +906,19 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
868 if (w_value && !f->set_alt) 906 if (w_value && !f->set_alt)
869 break; 907 break;
870 value = f->set_alt(f, w_index, w_value); 908 value = f->set_alt(f, w_index, w_value);
909 if (value == USB_GADGET_DELAYED_STATUS) {
910 DBG(cdev,
911 "%s: interface %d (%s) requested delayed status\n",
912 __func__, intf, f->name);
913 cdev->delayed_status++;
914 DBG(cdev, "delayed_status count %d\n",
915 cdev->delayed_status);
916 }
871 break; 917 break;
872 case USB_REQ_GET_INTERFACE: 918 case USB_REQ_GET_INTERFACE:
873 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) 919 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
874 goto unknown; 920 goto unknown;
875 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 921 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
876 break; 922 break;
877 f = cdev->config->interface[intf]; 923 f = cdev->config->interface[intf];
878 if (!f) 924 if (!f)
@@ -901,6 +947,8 @@ unknown:
901 */ 947 */
902 switch (ctrl->bRequestType & USB_RECIP_MASK) { 948 switch (ctrl->bRequestType & USB_RECIP_MASK) {
903 case USB_RECIP_INTERFACE: 949 case USB_RECIP_INTERFACE:
950 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
951 break;
904 f = cdev->config->interface[intf]; 952 f = cdev->config->interface[intf];
905 break; 953 break;
906 954
@@ -929,7 +977,7 @@ unknown:
929 } 977 }
930 978
931 /* respond with data transfer before status phase? */ 979 /* respond with data transfer before status phase? */
932 if (value >= 0) { 980 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
933 req->length = value; 981 req->length = value;
934 req->zero = value < w_length; 982 req->zero = value < w_length;
935 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 983 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
@@ -938,6 +986,10 @@ unknown:
938 req->status = 0; 986 req->status = 0;
939 composite_setup_complete(gadget->ep0, req); 987 composite_setup_complete(gadget->ep0, req);
940 } 988 }
989 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
990 WARN(cdev,
991 "%s: Delayed status not supported for w_length != 0",
992 __func__);
941 } 993 }
942 994
943done: 995done:
@@ -1019,32 +1071,23 @@ composite_unbind(struct usb_gadget *gadget)
1019 kfree(cdev->req->buf); 1071 kfree(cdev->req->buf);
1020 usb_ep_free_request(gadget->ep0, cdev->req); 1072 usb_ep_free_request(gadget->ep0, cdev->req);
1021 } 1073 }
1074 device_remove_file(&gadget->dev, &dev_attr_suspended);
1022 kfree(cdev); 1075 kfree(cdev);
1023 set_gadget_data(gadget, NULL); 1076 set_gadget_data(gadget, NULL);
1024 device_remove_file(&gadget->dev, &dev_attr_suspended);
1025 composite = NULL; 1077 composite = NULL;
1026} 1078}
1027 1079
1028static void 1080static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1029string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1030{ 1081{
1031 struct usb_string *str = tab->strings; 1082 if (!*desc) {
1032 1083 int ret = usb_string_id(cdev);
1033 for (str = tab->strings; str->s; str++) { 1084 if (unlikely(ret < 0))
1034 if (str->id == id) { 1085 WARNING(cdev, "failed to override string ID\n");
1035 str->s = s; 1086 else
1036 return; 1087 *desc = ret;
1037 }
1038 } 1088 }
1039}
1040 1089
1041static void 1090 return *desc;
1042string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1043{
1044 while (*tab) {
1045 string_override_one(*tab, id, s);
1046 tab++;
1047 }
1048} 1091}
1049 1092
1050static int composite_bind(struct usb_gadget *gadget) 1093static int composite_bind(struct usb_gadget *gadget)
@@ -1074,7 +1117,13 @@ static int composite_bind(struct usb_gadget *gadget)
1074 cdev->bufsiz = USB_BUFSIZ; 1117 cdev->bufsiz = USB_BUFSIZ;
1075 cdev->driver = composite; 1118 cdev->driver = composite;
1076 1119
1077 usb_gadget_set_selfpowered(gadget); 1120 /*
1121 * As per USB compliance update, a device that is actively drawing
1122 * more than 100mA from USB must report itself as bus-powered in
1123 * the GetStatus(DEVICE) call.
1124 */
1125 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1126 usb_gadget_set_selfpowered(gadget);
1078 1127
1079 /* interface and string IDs start at zero via kzalloc. 1128 /* interface and string IDs start at zero via kzalloc.
1080 * we force endpoints to start unassigned; few controller 1129 * we force endpoints to start unassigned; few controller
@@ -1082,38 +1131,53 @@ static int composite_bind(struct usb_gadget *gadget)
1082 */ 1131 */
1083 usb_ep_autoconfig_reset(cdev->gadget); 1132 usb_ep_autoconfig_reset(cdev->gadget);
1084 1133
1085 /* standardized runtime overrides for device ID data */
1086 if (idVendor)
1087 cdev->desc.idVendor = cpu_to_le16(idVendor);
1088 if (idProduct)
1089 cdev->desc.idProduct = cpu_to_le16(idProduct);
1090 if (bcdDevice)
1091 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1092
1093 /* composite gadget needs to assign strings for whole device (like 1134 /* composite gadget needs to assign strings for whole device (like
1094 * serial number), register function drivers, potentially update 1135 * serial number), register function drivers, potentially update
1095 * power state and consumption, etc 1136 * power state and consumption, etc
1096 */ 1137 */
1097 status = composite->bind(cdev); 1138 status = composite_gadget_bind(cdev);
1098 if (status < 0) 1139 if (status < 0)
1099 goto fail; 1140 goto fail;
1100 1141
1101 cdev->desc = *composite->dev; 1142 cdev->desc = *composite->dev;
1102 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1143 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1103 1144
1104 /* strings can't be assigned before bind() allocates the 1145 /* standardized runtime overrides for device ID data */
1105 * releavnt identifiers 1146 if (idVendor)
1106 */ 1147 cdev->desc.idVendor = cpu_to_le16(idVendor);
1107 if (cdev->desc.iManufacturer && iManufacturer) 1148 if (idProduct)
1108 string_override(composite->strings, 1149 cdev->desc.idProduct = cpu_to_le16(idProduct);
1109 cdev->desc.iManufacturer, iManufacturer); 1150 if (bcdDevice)
1110 if (cdev->desc.iProduct && iProduct) 1151 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1111 string_override(composite->strings, 1152
1112 cdev->desc.iProduct, iProduct); 1153 /* string overrides */
1113 if (cdev->desc.iSerialNumber && iSerialNumber) 1154 if (iManufacturer || !cdev->desc.iManufacturer) {
1114 string_override(composite->strings, 1155 if (!iManufacturer && !composite->iManufacturer &&
1115 cdev->desc.iSerialNumber, iSerialNumber); 1156 !*composite_manufacturer)
1157 snprintf(composite_manufacturer,
1158 sizeof composite_manufacturer,
1159 "%s %s with %s",
1160 init_utsname()->sysname,
1161 init_utsname()->release,
1162 gadget->name);
1163
1164 cdev->manufacturer_override =
1165 override_id(cdev, &cdev->desc.iManufacturer);
1166 }
1167
1168 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1169 cdev->product_override =
1170 override_id(cdev, &cdev->desc.iProduct);
1171
1172 if (iSerialNumber)
1173 cdev->serial_override =
1174 override_id(cdev, &cdev->desc.iSerialNumber);
1116 1175
1176 /* has userspace failed to provide a serial number? */
1177 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1178 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1179
1180 /* finish up */
1117 status = device_create_file(&gadget->dev, &dev_attr_suspended); 1181 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1118 if (status) 1182 if (status)
1119 goto fail; 1183 goto fail;
@@ -1148,6 +1212,8 @@ composite_suspend(struct usb_gadget *gadget)
1148 composite->suspend(cdev); 1212 composite->suspend(cdev);
1149 1213
1150 cdev->suspended = 1; 1214 cdev->suspended = 1;
1215
1216 usb_gadget_vbus_draw(gadget, 2);
1151} 1217}
1152 1218
1153static void 1219static void
@@ -1155,6 +1221,7 @@ composite_resume(struct usb_gadget *gadget)
1155{ 1221{
1156 struct usb_composite_dev *cdev = get_gadget_data(gadget); 1222 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1157 struct usb_function *f; 1223 struct usb_function *f;
1224 u8 maxpower;
1158 1225
1159 /* REVISIT: should we have config level 1226 /* REVISIT: should we have config level
1160 * suspend/resume callbacks? 1227 * suspend/resume callbacks?
@@ -1167,6 +1234,11 @@ composite_resume(struct usb_gadget *gadget)
1167 if (f->resume) 1234 if (f->resume)
1168 f->resume(f); 1235 f->resume(f);
1169 } 1236 }
1237
1238 maxpower = cdev->config->bMaxPower;
1239
1240 usb_gadget_vbus_draw(gadget, maxpower ?
1241 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1170 } 1242 }
1171 1243
1172 cdev->suspended = 0; 1244 cdev->suspended = 0;
@@ -1177,7 +1249,6 @@ composite_resume(struct usb_gadget *gadget)
1177static struct usb_gadget_driver composite_driver = { 1249static struct usb_gadget_driver composite_driver = {
1178 .speed = USB_SPEED_HIGH, 1250 .speed = USB_SPEED_HIGH,
1179 1251
1180 .bind = composite_bind,
1181 .unbind = composite_unbind, 1252 .unbind = composite_unbind,
1182 1253
1183 .setup = composite_setup, 1254 .setup = composite_setup,
@@ -1192,8 +1263,12 @@ static struct usb_gadget_driver composite_driver = {
1192}; 1263};
1193 1264
1194/** 1265/**
1195 * usb_composite_register() - register a composite driver 1266 * usb_composite_probe() - register a composite driver
1196 * @driver: the driver to register 1267 * @driver: the driver to register
1268 * @bind: the callback used to allocate resources that are shared across the
1269 * whole device, such as string IDs, and add its configurations using
1270 * @usb_add_config(). This may fail by returning a negative errno
1271 * value; it should return zero on successful initialization.
1197 * Context: single threaded during gadget setup 1272 * Context: single threaded during gadget setup
1198 * 1273 *
1199 * This function is used to register drivers using the composite driver 1274 * This function is used to register drivers using the composite driver
@@ -1206,18 +1281,22 @@ static struct usb_gadget_driver composite_driver = {
1206 * while it was binding. That would usually be done in order to wait for 1281 * while it was binding. That would usually be done in order to wait for
1207 * some userspace participation. 1282 * some userspace participation.
1208 */ 1283 */
1209int usb_composite_register(struct usb_composite_driver *driver) 1284int usb_composite_probe(struct usb_composite_driver *driver,
1285 int (*bind)(struct usb_composite_dev *cdev))
1210{ 1286{
1211 if (!driver || !driver->dev || !driver->bind || composite) 1287 if (!driver || !driver->dev || !bind || composite)
1212 return -EINVAL; 1288 return -EINVAL;
1213 1289
1214 if (!driver->name) 1290 if (!driver->name)
1215 driver->name = "composite"; 1291 driver->name = "composite";
1292 if (!driver->iProduct)
1293 driver->iProduct = driver->name;
1216 composite_driver.function = (char *) driver->name; 1294 composite_driver.function = (char *) driver->name;
1217 composite_driver.driver.name = driver->name; 1295 composite_driver.driver.name = driver->name;
1218 composite = driver; 1296 composite = driver;
1297 composite_gadget_bind = bind;
1219 1298
1220 return usb_gadget_register_driver(&composite_driver); 1299 return usb_gadget_probe_driver(&composite_driver, composite_bind);
1221} 1300}
1222 1301
1223/** 1302/**
@@ -1233,3 +1312,40 @@ void usb_composite_unregister(struct usb_composite_driver *driver)
1233 return; 1312 return;
1234 usb_gadget_unregister_driver(&composite_driver); 1313 usb_gadget_unregister_driver(&composite_driver);
1235} 1314}
1315
1316/**
1317 * usb_composite_setup_continue() - Continue with the control transfer
1318 * @cdev: the composite device who's control transfer was kept waiting
1319 *
1320 * This function must be called by the USB function driver to continue
1321 * with the control transfer's data/status stage in case it had requested to
1322 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1323 * can request the composite framework to delay the setup request's data/status
1324 * stages by returning USB_GADGET_DELAYED_STATUS.
1325 */
1326void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1327{
1328 int value;
1329 struct usb_request *req = cdev->req;
1330 unsigned long flags;
1331
1332 DBG(cdev, "%s\n", __func__);
1333 spin_lock_irqsave(&cdev->lock, flags);
1334
1335 if (cdev->delayed_status == 0) {
1336 WARN(cdev, "%s: Unexpected call\n", __func__);
1337
1338 } else if (--cdev->delayed_status == 0) {
1339 DBG(cdev, "%s: Completing delayed status\n", __func__);
1340 req->length = 0;
1341 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1342 if (value < 0) {
1343 DBG(cdev, "ep_queue --> %d\n", value);
1344 req->status = 0;
1345 composite_setup_complete(cdev->gadget->ep0, req);
1346 }
1347 }
1348
1349 spin_unlock_irqrestore(&cdev->lock, flags);
1350}
1351
diff --git a/drivers/usb/gadget/dbgp.c b/drivers/usb/gadget/dbgp.c
index 0ed50a2c0a36..dbe92ee88477 100644
--- a/drivers/usb/gadget/dbgp.c
+++ b/drivers/usb/gadget/dbgp.c
@@ -261,8 +261,8 @@ static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
261 o_desc.wMaxPacketSize = 261 o_desc.wMaxPacketSize =
262 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE); 262 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
263 263
264 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress & 0x7f; 264 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
265 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress & 0x7f; 265 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
266 266
267#ifdef CONFIG_USB_G_DBGP_SERIAL 267#ifdef CONFIG_USB_G_DBGP_SERIAL
268 dbgp.serial->in = dbgp.i_ep; 268 dbgp.serial->in = dbgp.i_ep;
@@ -312,6 +312,7 @@ static int __init dbgp_bind(struct usb_gadget *gadget)
312 312
313 dbgp.req->length = DBGP_REQ_EP0_LEN; 313 dbgp.req->length = DBGP_REQ_EP0_LEN;
314 gadget->ep0->driver_data = gadget; 314 gadget->ep0->driver_data = gadget;
315 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
315 316
316#ifdef CONFIG_USB_G_DBGP_SERIAL 317#ifdef CONFIG_USB_G_DBGP_SERIAL
317 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL); 318 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
@@ -350,9 +351,9 @@ static int dbgp_setup(struct usb_gadget *gadget,
350 u8 request = ctrl->bRequest; 351 u8 request = ctrl->bRequest;
351 u16 value = le16_to_cpu(ctrl->wValue); 352 u16 value = le16_to_cpu(ctrl->wValue);
352 u16 length = le16_to_cpu(ctrl->wLength); 353 u16 length = le16_to_cpu(ctrl->wLength);
353 int err = 0; 354 int err = -EOPNOTSUPP;
354 void *data; 355 void *data = NULL;
355 u16 len; 356 u16 len = 0;
356 357
357 gadget->ep0->driver_data = gadget; 358 gadget->ep0->driver_data = gadget;
358 359
@@ -371,10 +372,9 @@ static int dbgp_setup(struct usb_gadget *gadget,
371 default: 372 default:
372 goto fail; 373 goto fail;
373 } 374 }
375 err = 0;
374 } else if (request == USB_REQ_SET_FEATURE && 376 } else if (request == USB_REQ_SET_FEATURE &&
375 value == USB_DEVICE_DEBUG_MODE) { 377 value == USB_DEVICE_DEBUG_MODE) {
376 len = 0;
377 data = NULL;
378 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n"); 378 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
379#ifdef CONFIG_USB_G_DBGP_PRINTK 379#ifdef CONFIG_USB_G_DBGP_PRINTK
380 err = dbgp_enable_ep(); 380 err = dbgp_enable_ep();
@@ -386,15 +386,13 @@ static int dbgp_setup(struct usb_gadget *gadget,
386 } else 386 } else
387 goto fail; 387 goto fail;
388 388
389 if (len >= 0) { 389 req->length = min(length, len);
390 req->length = min(length, len); 390 req->zero = len < req->length;
391 req->zero = len < req->length; 391 if (data && req->length)
392 if (data && req->length) 392 memcpy(req->buf, data, req->length);
393 memcpy(req->buf, data, req->length);
394 393
395 req->complete = dbgp_setup_complete; 394 req->complete = dbgp_setup_complete;
396 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 395 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
397 }
398 396
399fail: 397fail:
400 dev_dbg(&dbgp.gadget->dev, 398 dev_dbg(&dbgp.gadget->dev,
@@ -405,7 +403,6 @@ fail:
405static struct usb_gadget_driver dbgp_driver = { 403static struct usb_gadget_driver dbgp_driver = {
406 .function = "dbgp", 404 .function = "dbgp",
407 .speed = USB_SPEED_HIGH, 405 .speed = USB_SPEED_HIGH,
408 .bind = dbgp_bind,
409 .unbind = dbgp_unbind, 406 .unbind = dbgp_unbind,
410 .setup = dbgp_setup, 407 .setup = dbgp_setup,
411 .disconnect = dbgp_disconnect, 408 .disconnect = dbgp_disconnect,
@@ -417,7 +414,7 @@ static struct usb_gadget_driver dbgp_driver = {
417 414
418static int __init dbgp_init(void) 415static int __init dbgp_init(void)
419{ 416{
420 return usb_gadget_register_driver(&dbgp_driver); 417 return usb_gadget_probe_driver(&dbgp_driver, dbgp_bind);
421} 418}
422 419
423static void __exit dbgp_exit(void) 420static void __exit dbgp_exit(void)
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index dc6546248ed9..d3dcabc1a5fc 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -748,7 +748,8 @@ static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
748 */ 748 */
749 749
750int 750int
751usb_gadget_register_driver (struct usb_gadget_driver *driver) 751usb_gadget_probe_driver(struct usb_gadget_driver *driver,
752 int (*bind)(struct usb_gadget *))
752{ 753{
753 struct dummy *dum = the_controller; 754 struct dummy *dum = the_controller;
754 int retval, i; 755 int retval, i;
@@ -757,8 +758,7 @@ usb_gadget_register_driver (struct usb_gadget_driver *driver)
757 return -EINVAL; 758 return -EINVAL;
758 if (dum->driver) 759 if (dum->driver)
759 return -EBUSY; 760 return -EBUSY;
760 if (!driver->bind || !driver->setup 761 if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
761 || driver->speed == USB_SPEED_UNKNOWN)
762 return -EINVAL; 762 return -EINVAL;
763 763
764 /* 764 /*
@@ -796,7 +796,7 @@ usb_gadget_register_driver (struct usb_gadget_driver *driver)
796 dum->gadget.dev.driver = &driver->driver; 796 dum->gadget.dev.driver = &driver->driver;
797 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n", 797 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
798 driver->driver.name); 798 driver->driver.name);
799 retval = driver->bind(&dum->gadget); 799 retval = bind(&dum->gadget);
800 if (retval) { 800 if (retval) {
801 dum->driver = NULL; 801 dum->driver = NULL;
802 dum->gadget.dev.driver = NULL; 802 dum->gadget.dev.driver = NULL;
@@ -812,7 +812,7 @@ usb_gadget_register_driver (struct usb_gadget_driver *driver)
812 usb_hcd_poll_rh_status (dummy_to_hcd (dum)); 812 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
813 return 0; 813 return 0;
814} 814}
815EXPORT_SYMBOL (usb_gadget_register_driver); 815EXPORT_SYMBOL(usb_gadget_probe_driver);
816 816
817int 817int
818usb_gadget_unregister_driver (struct usb_gadget_driver *driver) 818usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
@@ -874,6 +874,8 @@ static int dummy_udc_probe (struct platform_device *pdev)
874 struct dummy *dum = the_controller; 874 struct dummy *dum = the_controller;
875 int rc; 875 int rc;
876 876
877 usb_get_hcd(dummy_to_hcd(dum));
878
877 dum->gadget.name = gadget_name; 879 dum->gadget.name = gadget_name;
878 dum->gadget.ops = &dummy_ops; 880 dum->gadget.ops = &dummy_ops;
879 dum->gadget.is_dualspeed = 1; 881 dum->gadget.is_dualspeed = 1;
@@ -885,15 +887,16 @@ static int dummy_udc_probe (struct platform_device *pdev)
885 dum->gadget.dev.parent = &pdev->dev; 887 dum->gadget.dev.parent = &pdev->dev;
886 dum->gadget.dev.release = dummy_gadget_release; 888 dum->gadget.dev.release = dummy_gadget_release;
887 rc = device_register (&dum->gadget.dev); 889 rc = device_register (&dum->gadget.dev);
888 if (rc < 0) 890 if (rc < 0) {
891 put_device(&dum->gadget.dev);
889 return rc; 892 return rc;
893 }
890 894
891 usb_get_hcd (dummy_to_hcd (dum));
892
893 platform_set_drvdata (pdev, dum);
894 rc = device_create_file (&dum->gadget.dev, &dev_attr_function); 895 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
895 if (rc < 0) 896 if (rc < 0)
896 device_unregister (&dum->gadget.dev); 897 device_unregister (&dum->gadget.dev);
898 else
899 platform_set_drvdata(pdev, dum);
897 return rc; 900 return rc;
898} 901}
899 902
@@ -1195,6 +1198,139 @@ static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1195#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) 1198#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1196#define Ep_InRequest (Ep_Request | USB_DIR_IN) 1199#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1197 1200
1201
1202/**
1203 * handle_control_request() - handles all control transfers
1204 * @dum: pointer to dummy (the_controller)
1205 * @urb: the urb request to handle
1206 * @setup: pointer to the setup data for a USB device control
1207 * request
1208 * @status: pointer to request handling status
1209 *
1210 * Return 0 - if the request was handled
1211 * 1 - if the request wasn't handles
1212 * error code on error
1213 */
1214static int handle_control_request(struct dummy *dum, struct urb *urb,
1215 struct usb_ctrlrequest *setup,
1216 int *status)
1217{
1218 struct dummy_ep *ep2;
1219 int ret_val = 1;
1220 unsigned w_index;
1221 unsigned w_value;
1222
1223 w_index = le16_to_cpu(setup->wIndex);
1224 w_value = le16_to_cpu(setup->wValue);
1225 switch (setup->bRequest) {
1226 case USB_REQ_SET_ADDRESS:
1227 if (setup->bRequestType != Dev_Request)
1228 break;
1229 dum->address = w_value;
1230 *status = 0;
1231 dev_dbg(udc_dev(dum), "set_address = %d\n",
1232 w_value);
1233 ret_val = 0;
1234 break;
1235 case USB_REQ_SET_FEATURE:
1236 if (setup->bRequestType == Dev_Request) {
1237 ret_val = 0;
1238 switch (w_value) {
1239 case USB_DEVICE_REMOTE_WAKEUP:
1240 break;
1241 case USB_DEVICE_B_HNP_ENABLE:
1242 dum->gadget.b_hnp_enable = 1;
1243 break;
1244 case USB_DEVICE_A_HNP_SUPPORT:
1245 dum->gadget.a_hnp_support = 1;
1246 break;
1247 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1248 dum->gadget.a_alt_hnp_support = 1;
1249 break;
1250 default:
1251 ret_val = -EOPNOTSUPP;
1252 }
1253 if (ret_val == 0) {
1254 dum->devstatus |= (1 << w_value);
1255 *status = 0;
1256 }
1257 } else if (setup->bRequestType == Ep_Request) {
1258 /* endpoint halt */
1259 ep2 = find_endpoint(dum, w_index);
1260 if (!ep2 || ep2->ep.name == ep0name) {
1261 ret_val = -EOPNOTSUPP;
1262 break;
1263 }
1264 ep2->halted = 1;
1265 ret_val = 0;
1266 *status = 0;
1267 }
1268 break;
1269 case USB_REQ_CLEAR_FEATURE:
1270 if (setup->bRequestType == Dev_Request) {
1271 ret_val = 0;
1272 switch (w_value) {
1273 case USB_DEVICE_REMOTE_WAKEUP:
1274 w_value = USB_DEVICE_REMOTE_WAKEUP;
1275 break;
1276 default:
1277 ret_val = -EOPNOTSUPP;
1278 break;
1279 }
1280 if (ret_val == 0) {
1281 dum->devstatus &= ~(1 << w_value);
1282 *status = 0;
1283 }
1284 } else if (setup->bRequestType == Ep_Request) {
1285 /* endpoint halt */
1286 ep2 = find_endpoint(dum, w_index);
1287 if (!ep2) {
1288 ret_val = -EOPNOTSUPP;
1289 break;
1290 }
1291 if (!ep2->wedged)
1292 ep2->halted = 0;
1293 ret_val = 0;
1294 *status = 0;
1295 }
1296 break;
1297 case USB_REQ_GET_STATUS:
1298 if (setup->bRequestType == Dev_InRequest
1299 || setup->bRequestType == Intf_InRequest
1300 || setup->bRequestType == Ep_InRequest) {
1301 char *buf;
1302 /*
1303 * device: remote wakeup, selfpowered
1304 * interface: nothing
1305 * endpoint: halt
1306 */
1307 buf = (char *)urb->transfer_buffer;
1308 if (urb->transfer_buffer_length > 0) {
1309 if (setup->bRequestType == Ep_InRequest) {
1310 ep2 = find_endpoint(dum, w_index);
1311 if (!ep2) {
1312 ret_val = -EOPNOTSUPP;
1313 break;
1314 }
1315 buf[0] = ep2->halted;
1316 } else if (setup->bRequestType ==
1317 Dev_InRequest) {
1318 buf[0] = (u8)dum->devstatus;
1319 } else
1320 buf[0] = 0;
1321 }
1322 if (urb->transfer_buffer_length > 1)
1323 buf[1] = 0;
1324 urb->actual_length = min_t(u32, 2,
1325 urb->transfer_buffer_length);
1326 ret_val = 0;
1327 *status = 0;
1328 }
1329 break;
1330 }
1331 return ret_val;
1332}
1333
1198/* drive both sides of the transfers; looks like irq handlers to 1334/* drive both sides of the transfers; looks like irq handlers to
1199 * both drivers except the callbacks aren't in_irq(). 1335 * both drivers except the callbacks aren't in_irq().
1200 */ 1336 */
@@ -1297,14 +1433,8 @@ restart:
1297 if (ep == &dum->ep [0] && ep->setup_stage) { 1433 if (ep == &dum->ep [0] && ep->setup_stage) {
1298 struct usb_ctrlrequest setup; 1434 struct usb_ctrlrequest setup;
1299 int value = 1; 1435 int value = 1;
1300 struct dummy_ep *ep2;
1301 unsigned w_index;
1302 unsigned w_value;
1303 1436
1304 setup = *(struct usb_ctrlrequest*) urb->setup_packet; 1437 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1305 w_index = le16_to_cpu(setup.wIndex);
1306 w_value = le16_to_cpu(setup.wValue);
1307
1308 /* paranoia, in case of stale queued data */ 1438 /* paranoia, in case of stale queued data */
1309 list_for_each_entry (req, &ep->queue, queue) { 1439 list_for_each_entry (req, &ep->queue, queue) {
1310 list_del_init (&req->queue); 1440 list_del_init (&req->queue);
@@ -1326,117 +1456,9 @@ restart:
1326 ep->last_io = jiffies; 1456 ep->last_io = jiffies;
1327 ep->setup_stage = 0; 1457 ep->setup_stage = 0;
1328 ep->halted = 0; 1458 ep->halted = 0;
1329 switch (setup.bRequest) {
1330 case USB_REQ_SET_ADDRESS:
1331 if (setup.bRequestType != Dev_Request)
1332 break;
1333 dum->address = w_value;
1334 status = 0;
1335 dev_dbg (udc_dev(dum), "set_address = %d\n",
1336 w_value);
1337 value = 0;
1338 break;
1339 case USB_REQ_SET_FEATURE:
1340 if (setup.bRequestType == Dev_Request) {
1341 value = 0;
1342 switch (w_value) {
1343 case USB_DEVICE_REMOTE_WAKEUP:
1344 break;
1345 case USB_DEVICE_B_HNP_ENABLE:
1346 dum->gadget.b_hnp_enable = 1;
1347 break;
1348 case USB_DEVICE_A_HNP_SUPPORT:
1349 dum->gadget.a_hnp_support = 1;
1350 break;
1351 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1352 dum->gadget.a_alt_hnp_support
1353 = 1;
1354 break;
1355 default:
1356 value = -EOPNOTSUPP;
1357 }
1358 if (value == 0) {
1359 dum->devstatus |=
1360 (1 << w_value);
1361 status = 0;
1362 }
1363 1459
1364 } else if (setup.bRequestType == Ep_Request) { 1460 value = handle_control_request(dum, urb, &setup,
1365 // endpoint halt 1461 &status);
1366 ep2 = find_endpoint (dum, w_index);
1367 if (!ep2 || ep2->ep.name == ep0name) {
1368 value = -EOPNOTSUPP;
1369 break;
1370 }
1371 ep2->halted = 1;
1372 value = 0;
1373 status = 0;
1374 }
1375 break;
1376 case USB_REQ_CLEAR_FEATURE:
1377 if (setup.bRequestType == Dev_Request) {
1378 switch (w_value) {
1379 case USB_DEVICE_REMOTE_WAKEUP:
1380 dum->devstatus &= ~(1 <<
1381 USB_DEVICE_REMOTE_WAKEUP);
1382 value = 0;
1383 status = 0;
1384 break;
1385 default:
1386 value = -EOPNOTSUPP;
1387 break;
1388 }
1389 } else if (setup.bRequestType == Ep_Request) {
1390 // endpoint halt
1391 ep2 = find_endpoint (dum, w_index);
1392 if (!ep2) {
1393 value = -EOPNOTSUPP;
1394 break;
1395 }
1396 if (!ep2->wedged)
1397 ep2->halted = 0;
1398 value = 0;
1399 status = 0;
1400 }
1401 break;
1402 case USB_REQ_GET_STATUS:
1403 if (setup.bRequestType == Dev_InRequest
1404 || setup.bRequestType
1405 == Intf_InRequest
1406 || setup.bRequestType
1407 == Ep_InRequest
1408 ) {
1409 char *buf;
1410
1411 // device: remote wakeup, selfpowered
1412 // interface: nothing
1413 // endpoint: halt
1414 buf = (char *)urb->transfer_buffer;
1415 if (urb->transfer_buffer_length > 0) {
1416 if (setup.bRequestType ==
1417 Ep_InRequest) {
1418 ep2 = find_endpoint (dum, w_index);
1419 if (!ep2) {
1420 value = -EOPNOTSUPP;
1421 break;
1422 }
1423 buf [0] = ep2->halted;
1424 } else if (setup.bRequestType ==
1425 Dev_InRequest) {
1426 buf [0] = (u8)
1427 dum->devstatus;
1428 } else
1429 buf [0] = 0;
1430 }
1431 if (urb->transfer_buffer_length > 1)
1432 buf [1] = 0;
1433 urb->actual_length = min_t(u32, 2,
1434 urb->transfer_buffer_length);
1435 value = 0;
1436 status = 0;
1437 }
1438 break;
1439 }
1440 1462
1441 /* gadget driver handles all other requests. block 1463 /* gadget driver handles all other requests. block
1442 * until setup() returns; no reentrancy issues etc. 1464 * until setup() returns; no reentrancy issues etc.
@@ -1572,8 +1594,8 @@ hub_descriptor (struct usb_hub_descriptor *desc)
1572 desc->bDescLength = 9; 1594 desc->bDescLength = 9;
1573 desc->wHubCharacteristics = cpu_to_le16(0x0001); 1595 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1574 desc->bNbrPorts = 1; 1596 desc->bNbrPorts = 1;
1575 desc->bitmap [0] = 0xff; 1597 desc->u.hs.DeviceRemovable[0] = 0xff;
1576 desc->bitmap [1] = 0xff; 1598 desc->u.hs.DeviceRemovable[1] = 0xff;
1577} 1599}
1578 1600
1579static int dummy_hub_control ( 1601static int dummy_hub_control (
@@ -1884,6 +1906,7 @@ static int dummy_hcd_probe(struct platform_device *pdev)
1884 if (!hcd) 1906 if (!hcd)
1885 return -ENOMEM; 1907 return -ENOMEM;
1886 the_controller = hcd_to_dummy (hcd); 1908 the_controller = hcd_to_dummy (hcd);
1909 hcd->has_tt = 1;
1887 1910
1888 retval = usb_add_hcd(hcd, 0, 0); 1911 retval = usb_add_hcd(hcd, 0, 0);
1889 if (retval != 0) { 1912 if (retval != 0) {
@@ -1974,11 +1997,29 @@ static int __init init (void)
1974 retval = platform_device_add(the_hcd_pdev); 1997 retval = platform_device_add(the_hcd_pdev);
1975 if (retval < 0) 1998 if (retval < 0)
1976 goto err_add_hcd; 1999 goto err_add_hcd;
2000 if (!the_controller) {
2001 /*
2002 * The hcd was added successfully but its probe function failed
2003 * for some reason.
2004 */
2005 retval = -EINVAL;
2006 goto err_add_udc;
2007 }
1977 retval = platform_device_add(the_udc_pdev); 2008 retval = platform_device_add(the_udc_pdev);
1978 if (retval < 0) 2009 if (retval < 0)
1979 goto err_add_udc; 2010 goto err_add_udc;
2011 if (!platform_get_drvdata(the_udc_pdev)) {
2012 /*
2013 * The udc was added successfully but its probe function failed
2014 * for some reason.
2015 */
2016 retval = -EINVAL;
2017 goto err_probe_udc;
2018 }
1980 return retval; 2019 return retval;
1981 2020
2021err_probe_udc:
2022 platform_device_del(the_udc_pdev);
1982err_add_udc: 2023err_add_udc:
1983 platform_device_del(the_hcd_pdev); 2024 platform_device_del(the_hcd_pdev);
1984err_add_hcd: 2025err_add_hcd:
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c
index 8a832488ccdd..9b7360ff5aa7 100644
--- a/drivers/usb/gadget/epautoconf.c
+++ b/drivers/usb/gadget/epautoconf.c
@@ -128,6 +128,13 @@ ep_matches (
128 } 128 }
129 } 129 }
130 130
131 /*
132 * If the protocol driver hasn't yet decided on wMaxPacketSize
133 * and wants to know the maximum possible, provide the info.
134 */
135 if (desc->wMaxPacketSize == 0)
136 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket);
137
131 /* endpoint maxpacket size is an input parameter, except for bulk 138 /* endpoint maxpacket size is an input parameter, except for bulk
132 * where it's an output parameter representing the full speed limit. 139 * where it's an output parameter representing the full speed limit.
133 * the usb spec fixes high speed bulk maxpacket at 512 bytes. 140 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 114fa024c22c..1690c9d68256 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -237,7 +237,7 @@ static u8 hostaddr[ETH_ALEN];
237 * the first one present. That's to make Microsoft's drivers happy, 237 * the first one present. That's to make Microsoft's drivers happy,
238 * and to follow DOCSIS 1.0 (cable modem standard). 238 * and to follow DOCSIS 1.0 (cable modem standard).
239 */ 239 */
240static int __ref rndis_do_config(struct usb_configuration *c) 240static int __init rndis_do_config(struct usb_configuration *c)
241{ 241{
242 /* FIXME alloc iConfiguration string, set it in c->strings */ 242 /* FIXME alloc iConfiguration string, set it in c->strings */
243 243
@@ -251,7 +251,6 @@ static int __ref rndis_do_config(struct usb_configuration *c)
251 251
252static struct usb_configuration rndis_config_driver = { 252static struct usb_configuration rndis_config_driver = {
253 .label = "RNDIS", 253 .label = "RNDIS",
254 .bind = rndis_do_config,
255 .bConfigurationValue = 2, 254 .bConfigurationValue = 2,
256 /* .iConfiguration = DYNAMIC */ 255 /* .iConfiguration = DYNAMIC */
257 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 256 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -270,7 +269,7 @@ MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
270/* 269/*
271 * We _always_ have an ECM, CDC Subset, or EEM configuration. 270 * We _always_ have an ECM, CDC Subset, or EEM configuration.
272 */ 271 */
273static int __ref eth_do_config(struct usb_configuration *c) 272static int __init eth_do_config(struct usb_configuration *c)
274{ 273{
275 /* FIXME alloc iConfiguration string, set it in c->strings */ 274 /* FIXME alloc iConfiguration string, set it in c->strings */
276 275
@@ -289,7 +288,6 @@ static int __ref eth_do_config(struct usb_configuration *c)
289 288
290static struct usb_configuration eth_config_driver = { 289static struct usb_configuration eth_config_driver = {
291 /* .label = f(hardware) */ 290 /* .label = f(hardware) */
292 .bind = eth_do_config,
293 .bConfigurationValue = 1, 291 .bConfigurationValue = 1,
294 /* .iConfiguration = DYNAMIC */ 292 /* .iConfiguration = DYNAMIC */
295 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 293 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -297,7 +295,7 @@ static struct usb_configuration eth_config_driver = {
297 295
298/*-------------------------------------------------------------------------*/ 296/*-------------------------------------------------------------------------*/
299 297
300static int __ref eth_bind(struct usb_composite_dev *cdev) 298static int __init eth_bind(struct usb_composite_dev *cdev)
301{ 299{
302 int gcnum; 300 int gcnum;
303 struct usb_gadget *gadget = cdev->gadget; 301 struct usb_gadget *gadget = cdev->gadget;
@@ -373,12 +371,13 @@ static int __ref eth_bind(struct usb_composite_dev *cdev)
373 371
374 /* register our configuration(s); RNDIS first, if it's used */ 372 /* register our configuration(s); RNDIS first, if it's used */
375 if (has_rndis()) { 373 if (has_rndis()) {
376 status = usb_add_config(cdev, &rndis_config_driver); 374 status = usb_add_config(cdev, &rndis_config_driver,
375 rndis_do_config);
377 if (status < 0) 376 if (status < 0)
378 goto fail; 377 goto fail;
379 } 378 }
380 379
381 status = usb_add_config(cdev, &eth_config_driver); 380 status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
382 if (status < 0) 381 if (status < 0)
383 goto fail; 382 goto fail;
384 383
@@ -402,7 +401,6 @@ static struct usb_composite_driver eth_driver = {
402 .name = "g_ether", 401 .name = "g_ether",
403 .dev = &device_desc, 402 .dev = &device_desc,
404 .strings = dev_strings, 403 .strings = dev_strings,
405 .bind = eth_bind,
406 .unbind = __exit_p(eth_unbind), 404 .unbind = __exit_p(eth_unbind),
407}; 405};
408 406
@@ -412,7 +410,7 @@ MODULE_LICENSE("GPL");
412 410
413static int __init init(void) 411static int __init init(void)
414{ 412{
415 return usb_composite_register(&eth_driver); 413 return usb_composite_probe(&eth_driver, eth_bind);
416} 414}
417module_init(init); 415module_init(init);
418 416
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index d47a123f15ab..bd6226cbae86 100644
--- a/drivers/usb/gadget/f_acm.c
+++ b/drivers/usb/gadget/f_acm.c
@@ -111,7 +111,7 @@ acm_iad_descriptor = {
111 .bInterfaceCount = 2, // control + data 111 .bInterfaceCount = 2, // control + data
112 .bFunctionClass = USB_CLASS_COMM, 112 .bFunctionClass = USB_CLASS_COMM,
113 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM, 113 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
114 .bFunctionProtocol = USB_CDC_PROTO_NONE, 114 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
115 /* .iFunction = DYNAMIC */ 115 /* .iFunction = DYNAMIC */
116}; 116};
117 117
diff --git a/drivers/usb/gadget/f_audio.c b/drivers/usb/gadget/f_audio.c
index 1f48ceb55a77..8ee330a2ab58 100644
--- a/drivers/usb/gadget/f_audio.c
+++ b/drivers/usb/gadget/f_audio.c
@@ -177,7 +177,7 @@ static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
177}; 177};
178 178
179/* Standard ISO OUT Endpoint Descriptor */ 179/* Standard ISO OUT Endpoint Descriptor */
180static struct usb_endpoint_descriptor as_out_ep_desc __initdata = { 180static struct usb_endpoint_descriptor as_out_ep_desc = {
181 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 181 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT, 182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT, 183 .bEndpointAddress = USB_DIR_OUT,
@@ -317,8 +317,6 @@ static void f_audio_playback_work(struct work_struct *data)
317 317
318 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); 318 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
319 f_audio_buffer_free(play_buf); 319 f_audio_buffer_free(play_buf);
320
321 return;
322} 320}
323 321
324static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) 322static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
@@ -708,6 +706,7 @@ f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
708 struct f_audio *audio = func_to_audio(f); 706 struct f_audio *audio = func_to_audio(f);
709 707
710 usb_free_descriptors(f->descriptors); 708 usb_free_descriptors(f->descriptors);
709 usb_free_descriptors(f->hs_descriptors);
711 kfree(audio); 710 kfree(audio);
712} 711}
713 712
@@ -744,7 +743,7 @@ int __init control_selector_init(struct f_audio *audio)
744} 743}
745 744
746/** 745/**
747 * audio_bind_config - add USB audio fucntion to a configuration 746 * audio_bind_config - add USB audio function to a configuration
748 * @c: the configuration to supcard the USB audio function 747 * @c: the configuration to supcard the USB audio function
749 * Context: single threaded during gadget setup 748 * Context: single threaded during gadget setup
750 * 749 *
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
index 95dd4662d6a8..b3c304290150 100644
--- a/drivers/usb/gadget/f_eem.c
+++ b/drivers/usb/gadget/f_eem.c
@@ -314,6 +314,9 @@ eem_unbind(struct usb_configuration *c, struct usb_function *f)
314 314
315static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req) 315static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
316{ 316{
317 struct sk_buff *skb = (struct sk_buff *)req->context;
318
319 dev_kfree_skb_any(skb);
317} 320}
318 321
319/* 322/*
@@ -428,10 +431,11 @@ static int eem_unwrap(struct gether *port,
428 skb_trim(skb2, len); 431 skb_trim(skb2, len);
429 put_unaligned_le16(BIT(15) | BIT(11) | len, 432 put_unaligned_le16(BIT(15) | BIT(11) | len,
430 skb_push(skb2, 2)); 433 skb_push(skb2, 2));
431 skb_copy_bits(skb, 0, req->buf, skb->len); 434 skb_copy_bits(skb2, 0, req->buf, skb2->len);
432 req->length = skb->len; 435 req->length = skb2->len;
433 req->complete = eem_cmd_complete; 436 req->complete = eem_cmd_complete;
434 req->zero = 1; 437 req->zero = 1;
438 req->context = skb2;
435 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC)) 439 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
436 DBG(cdev, "echo response queue fail\n"); 440 DBG(cdev, "echo response queue fail\n");
437 break; 441 break;
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index e4f595055208..19fffccc370d 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -1,10 +1,10 @@
1/* 1/*
2 * f_fs.c -- user mode filesystem api for usb composite funtcion controllers 2 * f_fs.c -- user mode file system API for USB composite function controllers
3 * 3 *
4 * Copyright (C) 2010 Samsung Electronics 4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com> 5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
6 * 6 *
7 * Based on inode.c (GadgetFS): 7 * Based on inode.c (GadgetFS) which was:
8 * Copyright (C) 2003-2004 David Brownell 8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies 9 * Copyright (C) 2003 Agilent Technologies
10 * 10 *
@@ -30,7 +30,6 @@
30#include <linux/blkdev.h> 30#include <linux/blkdev.h>
31#include <linux/pagemap.h> 31#include <linux/pagemap.h>
32#include <asm/unaligned.h> 32#include <asm/unaligned.h>
33#include <linux/smp_lock.h>
34 33
35#include <linux/usb/composite.h> 34#include <linux/usb/composite.h>
36#include <linux/usb/functionfs.h> 35#include <linux/usb/functionfs.h>
@@ -39,62 +38,56 @@
39#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */ 38#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
40 39
41 40
42/* Debuging *****************************************************************/ 41/* Debugging ****************************************************************/
43
44#define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
45
46#define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
47#define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
48
49#ifdef DEBUG
50# define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
51#else
52# define FDBG(...) do { } while (0)
53#endif /* DEBUG */
54
55#ifdef VERBOSE_DEBUG
56# define FVDBG FDBG
57#else
58# define FVDBG(...) do { } while (0)
59#endif /* VERBOSE_DEBUG */
60
61#define ENTER() FVDBG("%s()", __func__)
62 42
63#ifdef VERBOSE_DEBUG 43#ifdef VERBOSE_DEBUG
44# define pr_vdebug pr_debug
64# define ffs_dump_mem(prefix, ptr, len) \ 45# define ffs_dump_mem(prefix, ptr, len) \
65 print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len) 46 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
66#else 47#else
48# define pr_vdebug(...) do { } while (0)
67# define ffs_dump_mem(prefix, ptr, len) do { } while (0) 49# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
68#endif 50#endif /* VERBOSE_DEBUG */
51
52#define ENTER() pr_vdebug("%s()\n", __func__)
69 53
70 54
71/* The data structure and setup file ****************************************/ 55/* The data structure and setup file ****************************************/
72 56
73enum ffs_state { 57enum ffs_state {
74 /* Waiting for descriptors and strings. */ 58 /*
75 /* In this state no open(2), read(2) or write(2) on epfiles 59 * Waiting for descriptors and strings.
60 *
61 * In this state no open(2), read(2) or write(2) on epfiles
76 * may succeed (which should not be the problem as there 62 * may succeed (which should not be the problem as there
77 * should be no such files opened in the firts place). */ 63 * should be no such files opened in the first place).
64 */
78 FFS_READ_DESCRIPTORS, 65 FFS_READ_DESCRIPTORS,
79 FFS_READ_STRINGS, 66 FFS_READ_STRINGS,
80 67
81 /* We've got descriptors and strings. We are or have called 68 /*
69 * We've got descriptors and strings. We are or have called
82 * functionfs_ready_callback(). functionfs_bind() may have 70 * functionfs_ready_callback(). functionfs_bind() may have
83 * been called but we don't know. */ 71 * been called but we don't know.
84 /* This is the only state in which operations on epfiles may 72 *
85 * succeed. */ 73 * This is the only state in which operations on epfiles may
74 * succeed.
75 */
86 FFS_ACTIVE, 76 FFS_ACTIVE,
87 77
88 /* All endpoints have been closed. This state is also set if 78 /*
79 * All endpoints have been closed. This state is also set if
89 * we encounter an unrecoverable error. The only 80 * we encounter an unrecoverable error. The only
90 * unrecoverable error is situation when after reading strings 81 * unrecoverable error is situation when after reading strings
91 * from user space we fail to initialise EP files or 82 * from user space we fail to initialise epfiles or
92 * functionfs_ready_callback() returns with error (<0). */ 83 * functionfs_ready_callback() returns with error (<0).
93 /* In this state no open(2), read(2) or write(2) (both on ep0 84 *
85 * In this state no open(2), read(2) or write(2) (both on ep0
94 * as well as epfile) may succeed (at this point epfiles are 86 * as well as epfile) may succeed (at this point epfiles are
95 * unlinked and all closed so this is not a problem; ep0 is 87 * unlinked and all closed so this is not a problem; ep0 is
96 * also closed but ep0 file exists and so open(2) on ep0 must 88 * also closed but ep0 file exists and so open(2) on ep0 must
97 * fail). */ 89 * fail).
90 */
98 FFS_CLOSING 91 FFS_CLOSING
99}; 92};
100 93
@@ -102,14 +95,18 @@ enum ffs_state {
102enum ffs_setup_state { 95enum ffs_setup_state {
103 /* There is no setup request pending. */ 96 /* There is no setup request pending. */
104 FFS_NO_SETUP, 97 FFS_NO_SETUP,
105 /* User has read events and there was a setup request event 98 /*
99 * User has read events and there was a setup request event
106 * there. The next read/write on ep0 will handle the 100 * there. The next read/write on ep0 will handle the
107 * request. */ 101 * request.
102 */
108 FFS_SETUP_PENDING, 103 FFS_SETUP_PENDING,
109 /* There was event pending but before user space handled it 104 /*
105 * There was event pending but before user space handled it
110 * some other event was introduced which canceled existing 106 * some other event was introduced which canceled existing
111 * setup. If this state is set read/write on ep0 return 107 * setup. If this state is set read/write on ep0 return
112 * -EIDRM. This state is only set when adding event. */ 108 * -EIDRM. This state is only set when adding event.
109 */
113 FFS_SETUP_CANCELED 110 FFS_SETUP_CANCELED
114}; 111};
115 112
@@ -121,23 +118,29 @@ struct ffs_function;
121struct ffs_data { 118struct ffs_data {
122 struct usb_gadget *gadget; 119 struct usb_gadget *gadget;
123 120
124 /* Protect access read/write operations, only one read/write 121 /*
122 * Protect access read/write operations, only one read/write
125 * at a time. As a consequence protects ep0req and company. 123 * at a time. As a consequence protects ep0req and company.
126 * While setup request is being processed (queued) this is 124 * While setup request is being processed (queued) this is
127 * held. */ 125 * held.
126 */
128 struct mutex mutex; 127 struct mutex mutex;
129 128
130 /* Protect access to enpoint related structures (basically 129 /*
130 * Protect access to endpoint related structures (basically
131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for 131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
132 * endpint zero. */ 132 * endpoint zero.
133 */
133 spinlock_t eps_lock; 134 spinlock_t eps_lock;
134 135
135 /* XXX REVISIT do we need our own request? Since we are not 136 /*
136 * handling setup requests immidiatelly user space may be so 137 * XXX REVISIT do we need our own request? Since we are not
138 * handling setup requests immediately user space may be so
137 * slow that another setup will be sent to the gadget but this 139 * slow that another setup will be sent to the gadget but this
138 * time not to us but another function and then there could be 140 * time not to us but another function and then there could be
139 * a race. Is that the case? Or maybe we can use cdev->req 141 * a race. Is that the case? Or maybe we can use cdev->req
140 * after all, maybe we just need some spinlock for that? */ 142 * after all, maybe we just need some spinlock for that?
143 */
141 struct usb_request *ep0req; /* P: mutex */ 144 struct usb_request *ep0req; /* P: mutex */
142 struct completion ep0req_completion; /* P: mutex */ 145 struct completion ep0req_completion; /* P: mutex */
143 int ep0req_status; /* P: mutex */ 146 int ep0req_status; /* P: mutex */
@@ -151,7 +154,7 @@ struct ffs_data {
151 enum ffs_state state; 154 enum ffs_state state;
152 155
153 /* 156 /*
154 * Possible transations: 157 * Possible transitions:
155 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock 158 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
156 * happens only in ep0 read which is P: mutex 159 * happens only in ep0 read which is P: mutex
157 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock 160 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
@@ -184,18 +187,21 @@ struct ffs_data {
184 /* Active function */ 187 /* Active function */
185 struct ffs_function *func; 188 struct ffs_function *func;
186 189
187 /* Device name, write once when file system is mounted. 190 /*
188 * Intendet for user to read if she wants. */ 191 * Device name, write once when file system is mounted.
192 * Intended for user to read if she wants.
193 */
189 const char *dev_name; 194 const char *dev_name;
190 /* Private data for our user (ie. gadget). Managed by 195 /* Private data for our user (ie. gadget). Managed by user. */
191 * user. */
192 void *private_data; 196 void *private_data;
193 197
194 /* filled by __ffs_data_got_descs() */ 198 /* filled by __ffs_data_got_descs() */
195 /* real descriptors are 16 bytes after raw_descs (so you need 199 /*
200 * Real descriptors are 16 bytes after raw_descs (so you need
196 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the 201 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
197 * first full speed descriptor). raw_descs_length and 202 * first full speed descriptor). raw_descs_length and
198 * raw_fs_descs_length do not have those 16 bytes added. */ 203 * raw_fs_descs_length do not have those 16 bytes added.
204 */
199 const void *raw_descs; 205 const void *raw_descs;
200 unsigned raw_descs_length; 206 unsigned raw_descs_length;
201 unsigned raw_fs_descs_length; 207 unsigned raw_fs_descs_length;
@@ -212,18 +218,23 @@ struct ffs_data {
212 const void *raw_strings; 218 const void *raw_strings;
213 struct usb_gadget_strings **stringtabs; 219 struct usb_gadget_strings **stringtabs;
214 220
215 /* File system's super block, write once when file system is mounted. */ 221 /*
222 * File system's super block, write once when file system is
223 * mounted.
224 */
216 struct super_block *sb; 225 struct super_block *sb;
217 226
218 /* File permissions, written once when fs is mounted*/ 227 /* File permissions, written once when fs is mounted */
219 struct ffs_file_perms { 228 struct ffs_file_perms {
220 umode_t mode; 229 umode_t mode;
221 uid_t uid; 230 uid_t uid;
222 gid_t gid; 231 gid_t gid;
223 } file_perms; 232 } file_perms;
224 233
225 /* The endpoint files, filled by ffs_epfiles_create(), 234 /*
226 * destroyed by ffs_epfiles_destroy(). */ 235 * The endpoint files, filled by ffs_epfiles_create(),
236 * destroyed by ffs_epfiles_destroy().
237 */
227 struct ffs_epfile *epfiles; 238 struct ffs_epfile *epfiles;
228}; 239};
229 240
@@ -237,7 +248,7 @@ static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
237static void ffs_data_opened(struct ffs_data *ffs); 248static void ffs_data_opened(struct ffs_data *ffs);
238static void ffs_data_closed(struct ffs_data *ffs); 249static void ffs_data_closed(struct ffs_data *ffs);
239 250
240/* Called with ffs->mutex held; take over ownerrship of data. */ 251/* Called with ffs->mutex held; take over ownership of data. */
241static int __must_check 252static int __must_check
242__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len); 253__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
243static int __must_check 254static int __must_check
@@ -268,11 +279,9 @@ static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
268 279
269static void ffs_func_free(struct ffs_function *func); 280static void ffs_func_free(struct ffs_function *func);
270 281
271
272static void ffs_func_eps_disable(struct ffs_function *func); 282static void ffs_func_eps_disable(struct ffs_function *func);
273static int __must_check ffs_func_eps_enable(struct ffs_function *func); 283static int __must_check ffs_func_eps_enable(struct ffs_function *func);
274 284
275
276static int ffs_func_bind(struct usb_configuration *, 285static int ffs_func_bind(struct usb_configuration *,
277 struct usb_function *); 286 struct usb_function *);
278static void ffs_func_unbind(struct usb_configuration *, 287static void ffs_func_unbind(struct usb_configuration *,
@@ -289,7 +298,6 @@ static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
289static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf); 298static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
290 299
291 300
292
293/* The endpoints structures *************************************************/ 301/* The endpoints structures *************************************************/
294 302
295struct ffs_ep { 303struct ffs_ep {
@@ -322,7 +330,6 @@ struct ffs_epfile {
322 unsigned char _pad; 330 unsigned char _pad;
323}; 331};
324 332
325
326static int __must_check ffs_epfiles_create(struct ffs_data *ffs); 333static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
327static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); 334static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
328 335
@@ -349,7 +356,6 @@ static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
349 complete_all(&ffs->ep0req_completion); 356 complete_all(&ffs->ep0req_completion);
350} 357}
351 358
352
353static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len) 359static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
354{ 360{
355 struct usb_request *req = ffs->ep0req; 361 struct usb_request *req = ffs->ep0req;
@@ -362,6 +368,14 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
362 req->buf = data; 368 req->buf = data;
363 req->length = len; 369 req->length = len;
364 370
371 /*
372 * UDC layer requires to provide a buffer even for ZLP, but should
373 * not use it at all. Let's provide some poisoned pointer to catch
374 * possible bug in the driver.
375 */
376 if (req->buf == NULL)
377 req->buf = (void *)0xDEADBABE;
378
365 INIT_COMPLETION(ffs->ep0req_completion); 379 INIT_COMPLETION(ffs->ep0req_completion);
366 380
367 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); 381 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
@@ -381,17 +395,16 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
381static int __ffs_ep0_stall(struct ffs_data *ffs) 395static int __ffs_ep0_stall(struct ffs_data *ffs)
382{ 396{
383 if (ffs->ev.can_stall) { 397 if (ffs->ev.can_stall) {
384 FVDBG("ep0 stall\n"); 398 pr_vdebug("ep0 stall\n");
385 usb_ep_set_halt(ffs->gadget->ep0); 399 usb_ep_set_halt(ffs->gadget->ep0);
386 ffs->setup_state = FFS_NO_SETUP; 400 ffs->setup_state = FFS_NO_SETUP;
387 return -EL2HLT; 401 return -EL2HLT;
388 } else { 402 } else {
389 FDBG("bogus ep0 stall!\n"); 403 pr_debug("bogus ep0 stall!\n");
390 return -ESRCH; 404 return -ESRCH;
391 } 405 }
392} 406}
393 407
394
395static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, 408static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
396 size_t len, loff_t *ptr) 409 size_t len, loff_t *ptr)
397{ 410{
@@ -410,7 +423,6 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
410 if (unlikely(ret < 0)) 423 if (unlikely(ret < 0))
411 return ret; 424 return ret;
412 425
413
414 /* Check state */ 426 /* Check state */
415 switch (ffs->state) { 427 switch (ffs->state) {
416 case FFS_READ_DESCRIPTORS: 428 case FFS_READ_DESCRIPTORS:
@@ -422,14 +434,14 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
422 } 434 }
423 435
424 data = ffs_prepare_buffer(buf, len); 436 data = ffs_prepare_buffer(buf, len);
425 if (unlikely(IS_ERR(data))) { 437 if (IS_ERR(data)) {
426 ret = PTR_ERR(data); 438 ret = PTR_ERR(data);
427 break; 439 break;
428 } 440 }
429 441
430 /* Handle data */ 442 /* Handle data */
431 if (ffs->state == FFS_READ_DESCRIPTORS) { 443 if (ffs->state == FFS_READ_DESCRIPTORS) {
432 FINFO("read descriptors"); 444 pr_info("read descriptors\n");
433 ret = __ffs_data_got_descs(ffs, data, len); 445 ret = __ffs_data_got_descs(ffs, data, len);
434 if (unlikely(ret < 0)) 446 if (unlikely(ret < 0))
435 break; 447 break;
@@ -437,7 +449,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
437 ffs->state = FFS_READ_STRINGS; 449 ffs->state = FFS_READ_STRINGS;
438 ret = len; 450 ret = len;
439 } else { 451 } else {
440 FINFO("read strings"); 452 pr_info("read strings\n");
441 ret = __ffs_data_got_strings(ffs, data, len); 453 ret = __ffs_data_got_strings(ffs, data, len);
442 if (unlikely(ret < 0)) 454 if (unlikely(ret < 0))
443 break; 455 break;
@@ -462,11 +474,12 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
462 } 474 }
463 break; 475 break;
464 476
465
466 case FFS_ACTIVE: 477 case FFS_ACTIVE:
467 data = NULL; 478 data = NULL;
468 /* We're called from user space, we can use _irq 479 /*
469 * rather then _irqsave */ 480 * We're called from user space, we can use _irq
481 * rather then _irqsave
482 */
470 spin_lock_irq(&ffs->ev.waitq.lock); 483 spin_lock_irq(&ffs->ev.waitq.lock);
471 switch (FFS_SETUP_STATE(ffs)) { 484 switch (FFS_SETUP_STATE(ffs)) {
472 case FFS_SETUP_CANCELED: 485 case FFS_SETUP_CANCELED:
@@ -494,23 +507,25 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
494 spin_unlock_irq(&ffs->ev.waitq.lock); 507 spin_unlock_irq(&ffs->ev.waitq.lock);
495 508
496 data = ffs_prepare_buffer(buf, len); 509 data = ffs_prepare_buffer(buf, len);
497 if (unlikely(IS_ERR(data))) { 510 if (IS_ERR(data)) {
498 ret = PTR_ERR(data); 511 ret = PTR_ERR(data);
499 break; 512 break;
500 } 513 }
501 514
502 spin_lock_irq(&ffs->ev.waitq.lock); 515 spin_lock_irq(&ffs->ev.waitq.lock);
503 516
504 /* We are guaranteed to be still in FFS_ACTIVE state 517 /*
518 * We are guaranteed to be still in FFS_ACTIVE state
505 * but the state of setup could have changed from 519 * but the state of setup could have changed from
506 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need 520 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
507 * to check for that. If that happened we copied data 521 * to check for that. If that happened we copied data
508 * from user space in vain but it's unlikely. */ 522 * from user space in vain but it's unlikely.
509 /* For sure we are not in FFS_NO_SETUP since this is 523 *
524 * For sure we are not in FFS_NO_SETUP since this is
510 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP 525 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
511 * transition can be performed and it's protected by 526 * transition can be performed and it's protected by
512 * mutex. */ 527 * mutex.
513 528 */
514 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { 529 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
515 ret = -EIDRM; 530 ret = -EIDRM;
516done_spin: 531done_spin:
@@ -522,25 +537,22 @@ done_spin:
522 kfree(data); 537 kfree(data);
523 break; 538 break;
524 539
525
526 default: 540 default:
527 ret = -EBADFD; 541 ret = -EBADFD;
528 break; 542 break;
529 } 543 }
530 544
531
532 mutex_unlock(&ffs->mutex); 545 mutex_unlock(&ffs->mutex);
533 return ret; 546 return ret;
534} 547}
535 548
536
537
538static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, 549static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
539 size_t n) 550 size_t n)
540{ 551{
541 /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need 552 /*
542 * to release them. */ 553 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
543 554 * to release them.
555 */
544 struct usb_functionfs_event events[n]; 556 struct usb_functionfs_event events[n];
545 unsigned i = 0; 557 unsigned i = 0;
546 558
@@ -569,7 +581,6 @@ static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
569 ? -EFAULT : sizeof events; 581 ? -EFAULT : sizeof events;
570} 582}
571 583
572
573static ssize_t ffs_ep0_read(struct file *file, char __user *buf, 584static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
574 size_t len, loff_t *ptr) 585 size_t len, loff_t *ptr)
575{ 586{
@@ -589,16 +600,16 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
589 if (unlikely(ret < 0)) 600 if (unlikely(ret < 0))
590 return ret; 601 return ret;
591 602
592
593 /* Check state */ 603 /* Check state */
594 if (ffs->state != FFS_ACTIVE) { 604 if (ffs->state != FFS_ACTIVE) {
595 ret = -EBADFD; 605 ret = -EBADFD;
596 goto done_mutex; 606 goto done_mutex;
597 } 607 }
598 608
599 609 /*
600 /* We're called from user space, we can use _irq rather then 610 * We're called from user space, we can use _irq rather then
601 * _irqsave */ 611 * _irqsave
612 */
602 spin_lock_irq(&ffs->ev.waitq.lock); 613 spin_lock_irq(&ffs->ev.waitq.lock);
603 614
604 switch (FFS_SETUP_STATE(ffs)) { 615 switch (FFS_SETUP_STATE(ffs)) {
@@ -618,7 +629,8 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
618 break; 629 break;
619 } 630 }
620 631
621 if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, ffs->ev.count))) { 632 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
633 ffs->ev.count)) {
622 ret = -EINTR; 634 ret = -EINTR;
623 break; 635 break;
624 } 636 }
@@ -626,7 +638,6 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
626 return __ffs_ep0_read_events(ffs, buf, 638 return __ffs_ep0_read_events(ffs, buf,
627 min(n, (size_t)ffs->ev.count)); 639 min(n, (size_t)ffs->ev.count));
628 640
629
630 case FFS_SETUP_PENDING: 641 case FFS_SETUP_PENDING:
631 if (ffs->ev.setup.bRequestType & USB_DIR_IN) { 642 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
632 spin_unlock_irq(&ffs->ev.waitq.lock); 643 spin_unlock_irq(&ffs->ev.waitq.lock);
@@ -672,8 +683,6 @@ done_mutex:
672 return ret; 683 return ret;
673} 684}
674 685
675
676
677static int ffs_ep0_open(struct inode *inode, struct file *file) 686static int ffs_ep0_open(struct inode *inode, struct file *file)
678{ 687{
679 struct ffs_data *ffs = inode->i_private; 688 struct ffs_data *ffs = inode->i_private;
@@ -689,7 +698,6 @@ static int ffs_ep0_open(struct inode *inode, struct file *file)
689 return 0; 698 return 0;
690} 699}
691 700
692
693static int ffs_ep0_release(struct inode *inode, struct file *file) 701static int ffs_ep0_release(struct inode *inode, struct file *file)
694{ 702{
695 struct ffs_data *ffs = file->private_data; 703 struct ffs_data *ffs = file->private_data;
@@ -701,7 +709,6 @@ static int ffs_ep0_release(struct inode *inode, struct file *file)
701 return 0; 709 return 0;
702} 710}
703 711
704
705static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) 712static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706{ 713{
707 struct ffs_data *ffs = file->private_data; 714 struct ffs_data *ffs = file->private_data;
@@ -722,7 +729,6 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
722 return ret; 729 return ret;
723} 730}
724 731
725
726static const struct file_operations ffs_ep0_operations = { 732static const struct file_operations ffs_ep0_operations = {
727 .owner = THIS_MODULE, 733 .owner = THIS_MODULE,
728 .llseek = no_llseek, 734 .llseek = no_llseek,
@@ -737,7 +743,6 @@ static const struct file_operations ffs_ep0_operations = {
737 743
738/* "Normal" endpoints operations ********************************************/ 744/* "Normal" endpoints operations ********************************************/
739 745
740
741static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req) 746static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
742{ 747{
743 ENTER(); 748 ENTER();
@@ -748,7 +753,6 @@ static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
748 } 753 }
749} 754}
750 755
751
752static ssize_t ffs_epfile_io(struct file *file, 756static ssize_t ffs_epfile_io(struct file *file,
753 char __user *buf, size_t len, int read) 757 char __user *buf, size_t len, int read)
754{ 758{
@@ -778,8 +782,8 @@ first_try:
778 goto error; 782 goto error;
779 } 783 }
780 784
781 if (unlikely(wait_event_interruptible 785 if (wait_event_interruptible(epfile->wait,
782 (epfile->wait, (ep = epfile->ep)))) { 786 (ep = epfile->ep))) {
783 ret = -EINTR; 787 ret = -EINTR;
784 goto error; 788 goto error;
785 } 789 }
@@ -811,12 +815,16 @@ first_try:
811 if (unlikely(ret)) 815 if (unlikely(ret))
812 goto error; 816 goto error;
813 817
814 /* We're called from user space, we can use _irq rather then 818 /*
815 * _irqsave */ 819 * We're called from user space, we can use _irq rather then
820 * _irqsave
821 */
816 spin_lock_irq(&epfile->ffs->eps_lock); 822 spin_lock_irq(&epfile->ffs->eps_lock);
817 823
818 /* While we were acquiring mutex endpoint got disabled 824 /*
819 * or changed? */ 825 * While we were acquiring mutex endpoint got disabled
826 * or changed?
827 */
820 } while (unlikely(epfile->ep != ep)); 828 } while (unlikely(epfile->ep != ep));
821 829
822 /* Halt */ 830 /* Halt */
@@ -858,7 +866,6 @@ error:
858 return ret; 866 return ret;
859} 867}
860 868
861
862static ssize_t 869static ssize_t
863ffs_epfile_write(struct file *file, const char __user *buf, size_t len, 870ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
864 loff_t *ptr) 871 loff_t *ptr)
@@ -904,7 +911,6 @@ ffs_epfile_release(struct inode *inode, struct file *file)
904 return 0; 911 return 0;
905} 912}
906 913
907
908static long ffs_epfile_ioctl(struct file *file, unsigned code, 914static long ffs_epfile_ioctl(struct file *file, unsigned code,
909 unsigned long value) 915 unsigned long value)
910{ 916{
@@ -943,7 +949,6 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,
943 return ret; 949 return ret;
944} 950}
945 951
946
947static const struct file_operations ffs_epfile_operations = { 952static const struct file_operations ffs_epfile_operations = {
948 .owner = THIS_MODULE, 953 .owner = THIS_MODULE,
949 .llseek = no_llseek, 954 .llseek = no_llseek,
@@ -956,15 +961,13 @@ static const struct file_operations ffs_epfile_operations = {
956}; 961};
957 962
958 963
959
960/* File system and super block operations ***********************************/ 964/* File system and super block operations ***********************************/
961 965
962/* 966/*
963 * Mounting the filesystem creates a controller file, used first for 967 * Mounting the file system creates a controller file, used first for
964 * function configuration then later for event monitoring. 968 * function configuration then later for event monitoring.
965 */ 969 */
966 970
967
968static struct inode *__must_check 971static struct inode *__must_check
969ffs_sb_make_inode(struct super_block *sb, void *data, 972ffs_sb_make_inode(struct super_block *sb, void *data,
970 const struct file_operations *fops, 973 const struct file_operations *fops,
@@ -980,6 +983,7 @@ ffs_sb_make_inode(struct super_block *sb, void *data,
980 if (likely(inode)) { 983 if (likely(inode)) {
981 struct timespec current_time = CURRENT_TIME; 984 struct timespec current_time = CURRENT_TIME;
982 985
986 inode->i_ino = get_next_ino();
983 inode->i_mode = perms->mode; 987 inode->i_mode = perms->mode;
984 inode->i_uid = perms->uid; 988 inode->i_uid = perms->uid;
985 inode->i_gid = perms->gid; 989 inode->i_gid = perms->gid;
@@ -996,9 +1000,7 @@ ffs_sb_make_inode(struct super_block *sb, void *data,
996 return inode; 1000 return inode;
997} 1001}
998 1002
999
1000/* Create "regular" file */ 1003/* Create "regular" file */
1001
1002static struct inode *ffs_sb_create_file(struct super_block *sb, 1004static struct inode *ffs_sb_create_file(struct super_block *sb,
1003 const char *name, void *data, 1005 const char *name, void *data,
1004 const struct file_operations *fops, 1006 const struct file_operations *fops,
@@ -1027,9 +1029,7 @@ static struct inode *ffs_sb_create_file(struct super_block *sb,
1027 return inode; 1029 return inode;
1028} 1030}
1029 1031
1030
1031/* Super block */ 1032/* Super block */
1032
1033static const struct super_operations ffs_sb_operations = { 1033static const struct super_operations ffs_sb_operations = {
1034 .statfs = simple_statfs, 1034 .statfs = simple_statfs,
1035 .drop_inode = generic_delete_inode, 1035 .drop_inode = generic_delete_inode,
@@ -1050,7 +1050,7 @@ static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1050 1050
1051 ENTER(); 1051 ENTER();
1052 1052
1053 /* Initialize data */ 1053 /* Initialise data */
1054 ffs = ffs_data_new(); 1054 ffs = ffs_data_new();
1055 if (unlikely(!ffs)) 1055 if (unlikely(!ffs))
1056 goto enomem0; 1056 goto enomem0;
@@ -1096,7 +1096,6 @@ enomem0:
1096 return -ENOMEM; 1096 return -ENOMEM;
1097} 1097}
1098 1098
1099
1100static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts) 1099static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1101{ 1100{
1102 ENTER(); 1101 ENTER();
@@ -1116,7 +1115,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1116 /* Value limit */ 1115 /* Value limit */
1117 eq = strchr(opts, '='); 1116 eq = strchr(opts, '=');
1118 if (unlikely(!eq)) { 1117 if (unlikely(!eq)) {
1119 FERR("'=' missing in %s", opts); 1118 pr_err("'=' missing in %s\n", opts);
1120 return -EINVAL; 1119 return -EINVAL;
1121 } 1120 }
1122 *eq = 0; 1121 *eq = 0;
@@ -1124,7 +1123,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1124 /* Parse value */ 1123 /* Parse value */
1125 value = simple_strtoul(eq + 1, &end, 0); 1124 value = simple_strtoul(eq + 1, &end, 0);
1126 if (unlikely(*end != ',' && *end != 0)) { 1125 if (unlikely(*end != ',' && *end != 0)) {
1127 FERR("%s: invalid value: %s", opts, eq + 1); 1126 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1128 return -EINVAL; 1127 return -EINVAL;
1129 } 1128 }
1130 1129
@@ -1159,7 +1158,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1159 1158
1160 default: 1159 default:
1161invalid: 1160invalid:
1162 FERR("%s: invalid option", opts); 1161 pr_err("%s: invalid option\n", opts);
1163 return -EINVAL; 1162 return -EINVAL;
1164 } 1163 }
1165 1164
@@ -1172,12 +1171,11 @@ invalid:
1172 return 0; 1171 return 0;
1173} 1172}
1174 1173
1175
1176/* "mount -t functionfs dev_name /dev/function" ends up here */ 1174/* "mount -t functionfs dev_name /dev/function" ends up here */
1177 1175
1178static int 1176static struct dentry *
1179ffs_fs_get_sb(struct file_system_type *t, int flags, 1177ffs_fs_mount(struct file_system_type *t, int flags,
1180 const char *dev_name, void *opts, struct vfsmount *mnt) 1178 const char *dev_name, void *opts)
1181{ 1179{
1182 struct ffs_sb_fill_data data = { 1180 struct ffs_sb_fill_data data = {
1183 .perms = { 1181 .perms = {
@@ -1193,14 +1191,14 @@ ffs_fs_get_sb(struct file_system_type *t, int flags,
1193 1191
1194 ret = functionfs_check_dev_callback(dev_name); 1192 ret = functionfs_check_dev_callback(dev_name);
1195 if (unlikely(ret < 0)) 1193 if (unlikely(ret < 0))
1196 return ret; 1194 return ERR_PTR(ret);
1197 1195
1198 ret = ffs_fs_parse_opts(&data, opts); 1196 ret = ffs_fs_parse_opts(&data, opts);
1199 if (unlikely(ret < 0)) 1197 if (unlikely(ret < 0))
1200 return ret; 1198 return ERR_PTR(ret);
1201 1199
1202 data.dev_name = dev_name; 1200 data.dev_name = dev_name;
1203 return get_sb_single(t, flags, &data, ffs_sb_fill, mnt); 1201 return mount_single(t, flags, &data, ffs_sb_fill);
1204} 1202}
1205 1203
1206static void 1204static void
@@ -1219,15 +1217,13 @@ ffs_fs_kill_sb(struct super_block *sb)
1219static struct file_system_type ffs_fs_type = { 1217static struct file_system_type ffs_fs_type = {
1220 .owner = THIS_MODULE, 1218 .owner = THIS_MODULE,
1221 .name = "functionfs", 1219 .name = "functionfs",
1222 .get_sb = ffs_fs_get_sb, 1220 .mount = ffs_fs_mount,
1223 .kill_sb = ffs_fs_kill_sb, 1221 .kill_sb = ffs_fs_kill_sb,
1224}; 1222};
1225 1223
1226 1224
1227
1228/* Driver's main init/cleanup functions *************************************/ 1225/* Driver's main init/cleanup functions *************************************/
1229 1226
1230
1231static int functionfs_init(void) 1227static int functionfs_init(void)
1232{ 1228{
1233 int ret; 1229 int ret;
@@ -1236,9 +1232,9 @@ static int functionfs_init(void)
1236 1232
1237 ret = register_filesystem(&ffs_fs_type); 1233 ret = register_filesystem(&ffs_fs_type);
1238 if (likely(!ret)) 1234 if (likely(!ret))
1239 FINFO("file system registered"); 1235 pr_info("file system registered\n");
1240 else 1236 else
1241 FERR("failed registering file system (%d)", ret); 1237 pr_err("failed registering file system (%d)\n", ret);
1242 1238
1243 return ret; 1239 return ret;
1244} 1240}
@@ -1247,18 +1243,16 @@ static void functionfs_cleanup(void)
1247{ 1243{
1248 ENTER(); 1244 ENTER();
1249 1245
1250 FINFO("unloading"); 1246 pr_info("unloading\n");
1251 unregister_filesystem(&ffs_fs_type); 1247 unregister_filesystem(&ffs_fs_type);
1252} 1248}
1253 1249
1254 1250
1255
1256/* ffs_data and ffs_function construction and destruction code **************/ 1251/* ffs_data and ffs_function construction and destruction code **************/
1257 1252
1258static void ffs_data_clear(struct ffs_data *ffs); 1253static void ffs_data_clear(struct ffs_data *ffs);
1259static void ffs_data_reset(struct ffs_data *ffs); 1254static void ffs_data_reset(struct ffs_data *ffs);
1260 1255
1261
1262static void ffs_data_get(struct ffs_data *ffs) 1256static void ffs_data_get(struct ffs_data *ffs)
1263{ 1257{
1264 ENTER(); 1258 ENTER();
@@ -1279,7 +1273,7 @@ static void ffs_data_put(struct ffs_data *ffs)
1279 ENTER(); 1273 ENTER();
1280 1274
1281 if (unlikely(atomic_dec_and_test(&ffs->ref))) { 1275 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1282 FINFO("%s(): freeing", __func__); 1276 pr_info("%s(): freeing\n", __func__);
1283 ffs_data_clear(ffs); 1277 ffs_data_clear(ffs);
1284 BUG_ON(mutex_is_locked(&ffs->mutex) || 1278 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1285 spin_is_locked(&ffs->ev.waitq.lock) || 1279 spin_is_locked(&ffs->ev.waitq.lock) ||
@@ -1289,8 +1283,6 @@ static void ffs_data_put(struct ffs_data *ffs)
1289 } 1283 }
1290} 1284}
1291 1285
1292
1293
1294static void ffs_data_closed(struct ffs_data *ffs) 1286static void ffs_data_closed(struct ffs_data *ffs)
1295{ 1287{
1296 ENTER(); 1288 ENTER();
@@ -1303,7 +1295,6 @@ static void ffs_data_closed(struct ffs_data *ffs)
1303 ffs_data_put(ffs); 1295 ffs_data_put(ffs);
1304} 1296}
1305 1297
1306
1307static struct ffs_data *ffs_data_new(void) 1298static struct ffs_data *ffs_data_new(void)
1308{ 1299{
1309 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); 1300 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
@@ -1326,7 +1317,6 @@ static struct ffs_data *ffs_data_new(void)
1326 return ffs; 1317 return ffs;
1327} 1318}
1328 1319
1329
1330static void ffs_data_clear(struct ffs_data *ffs) 1320static void ffs_data_clear(struct ffs_data *ffs)
1331{ 1321{
1332 ENTER(); 1322 ENTER();
@@ -1344,7 +1334,6 @@ static void ffs_data_clear(struct ffs_data *ffs)
1344 kfree(ffs->stringtabs); 1334 kfree(ffs->stringtabs);
1345} 1335}
1346 1336
1347
1348static void ffs_data_reset(struct ffs_data *ffs) 1337static void ffs_data_reset(struct ffs_data *ffs)
1349{ 1338{
1350 ENTER(); 1339 ENTER();
@@ -1407,7 +1396,6 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1407 return 0; 1396 return 0;
1408} 1397}
1409 1398
1410
1411static void functionfs_unbind(struct ffs_data *ffs) 1399static void functionfs_unbind(struct ffs_data *ffs)
1412{ 1400{
1413 ENTER(); 1401 ENTER();
@@ -1420,7 +1408,6 @@ static void functionfs_unbind(struct ffs_data *ffs)
1420 } 1408 }
1421} 1409}
1422 1410
1423
1424static int ffs_epfiles_create(struct ffs_data *ffs) 1411static int ffs_epfiles_create(struct ffs_data *ffs)
1425{ 1412{
1426 struct ffs_epfile *epfile, *epfiles; 1413 struct ffs_epfile *epfile, *epfiles;
@@ -1451,7 +1438,6 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
1451 return 0; 1438 return 0;
1452} 1439}
1453 1440
1454
1455static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) 1441static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1456{ 1442{
1457 struct ffs_epfile *epfile = epfiles; 1443 struct ffs_epfile *epfile = epfiles;
@@ -1471,7 +1457,6 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1471 kfree(epfiles); 1457 kfree(epfiles);
1472} 1458}
1473 1459
1474
1475static int functionfs_bind_config(struct usb_composite_dev *cdev, 1460static int functionfs_bind_config(struct usb_composite_dev *cdev,
1476 struct usb_configuration *c, 1461 struct usb_configuration *c,
1477 struct ffs_data *ffs) 1462 struct ffs_data *ffs)
@@ -1491,7 +1476,6 @@ static int functionfs_bind_config(struct usb_composite_dev *cdev,
1491 func->function.bind = ffs_func_bind; 1476 func->function.bind = ffs_func_bind;
1492 func->function.unbind = ffs_func_unbind; 1477 func->function.unbind = ffs_func_unbind;
1493 func->function.set_alt = ffs_func_set_alt; 1478 func->function.set_alt = ffs_func_set_alt;
1494 /*func->function.get_alt = ffs_func_get_alt;*/
1495 func->function.disable = ffs_func_disable; 1479 func->function.disable = ffs_func_disable;
1496 func->function.setup = ffs_func_setup; 1480 func->function.setup = ffs_func_setup;
1497 func->function.suspend = ffs_func_suspend; 1481 func->function.suspend = ffs_func_suspend;
@@ -1516,14 +1500,15 @@ static void ffs_func_free(struct ffs_function *func)
1516 ffs_data_put(func->ffs); 1500 ffs_data_put(func->ffs);
1517 1501
1518 kfree(func->eps); 1502 kfree(func->eps);
1519 /* eps and interfaces_nums are allocated in the same chunk so 1503 /*
1504 * eps and interfaces_nums are allocated in the same chunk so
1520 * only one free is required. Descriptors are also allocated 1505 * only one free is required. Descriptors are also allocated
1521 * in the same chunk. */ 1506 * in the same chunk.
1507 */
1522 1508
1523 kfree(func); 1509 kfree(func);
1524} 1510}
1525 1511
1526
1527static void ffs_func_eps_disable(struct ffs_function *func) 1512static void ffs_func_eps_disable(struct ffs_function *func)
1528{ 1513{
1529 struct ffs_ep *ep = func->eps; 1514 struct ffs_ep *ep = func->eps;
@@ -1581,11 +1566,12 @@ static int ffs_func_eps_enable(struct ffs_function *func)
1581 1566
1582/* Parsing and building descriptors and strings *****************************/ 1567/* Parsing and building descriptors and strings *****************************/
1583 1568
1584 1569/*
1585/* This validates if data pointed by data is a valid USB descriptor as 1570 * This validates if data pointed by data is a valid USB descriptor as
1586 * well as record how many interfaces, endpoints and strings are 1571 * well as record how many interfaces, endpoints and strings are
1587 * required by given configuration. Returns address afther the 1572 * required by given configuration. Returns address after the
1588 * descriptor or NULL if data is invalid. */ 1573 * descriptor or NULL if data is invalid.
1574 */
1589 1575
1590enum ffs_entity_type { 1576enum ffs_entity_type {
1591 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT 1577 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
@@ -1607,14 +1593,14 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1607 1593
1608 /* At least two bytes are required: length and type */ 1594 /* At least two bytes are required: length and type */
1609 if (len < 2) { 1595 if (len < 2) {
1610 FVDBG("descriptor too short"); 1596 pr_vdebug("descriptor too short\n");
1611 return -EINVAL; 1597 return -EINVAL;
1612 } 1598 }
1613 1599
1614 /* If we have at least as many bytes as the descriptor takes? */ 1600 /* If we have at least as many bytes as the descriptor takes? */
1615 length = _ds->bLength; 1601 length = _ds->bLength;
1616 if (len < length) { 1602 if (len < length) {
1617 FVDBG("descriptor longer then available data"); 1603 pr_vdebug("descriptor longer then available data\n");
1618 return -EINVAL; 1604 return -EINVAL;
1619 } 1605 }
1620 1606
@@ -1622,15 +1608,15 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1622#define __entity_check_STRING(val) (val) 1608#define __entity_check_STRING(val) (val)
1623#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK) 1609#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1624#define __entity(type, val) do { \ 1610#define __entity(type, val) do { \
1625 FVDBG("entity " #type "(%02x)", (val)); \ 1611 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1626 if (unlikely(!__entity_check_ ##type(val))) { \ 1612 if (unlikely(!__entity_check_ ##type(val))) { \
1627 FVDBG("invalid entity's value"); \ 1613 pr_vdebug("invalid entity's value\n"); \
1628 return -EINVAL; \ 1614 return -EINVAL; \
1629 } \ 1615 } \
1630 ret = entity(FFS_ ##type, &val, _ds, priv); \ 1616 ret = entity(FFS_ ##type, &val, _ds, priv); \
1631 if (unlikely(ret < 0)) { \ 1617 if (unlikely(ret < 0)) { \
1632 FDBG("entity " #type "(%02x); ret = %d", \ 1618 pr_debug("entity " #type "(%02x); ret = %d\n", \
1633 (val), ret); \ 1619 (val), ret); \
1634 return ret; \ 1620 return ret; \
1635 } \ 1621 } \
1636 } while (0) 1622 } while (0)
@@ -1642,12 +1628,13 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1642 case USB_DT_STRING: 1628 case USB_DT_STRING:
1643 case USB_DT_DEVICE_QUALIFIER: 1629 case USB_DT_DEVICE_QUALIFIER:
1644 /* function can't have any of those */ 1630 /* function can't have any of those */
1645 FVDBG("descriptor reserved for gadget: %d", _ds->bDescriptorType); 1631 pr_vdebug("descriptor reserved for gadget: %d\n",
1632 _ds->bDescriptorType);
1646 return -EINVAL; 1633 return -EINVAL;
1647 1634
1648 case USB_DT_INTERFACE: { 1635 case USB_DT_INTERFACE: {
1649 struct usb_interface_descriptor *ds = (void *)_ds; 1636 struct usb_interface_descriptor *ds = (void *)_ds;
1650 FVDBG("interface descriptor"); 1637 pr_vdebug("interface descriptor\n");
1651 if (length != sizeof *ds) 1638 if (length != sizeof *ds)
1652 goto inv_length; 1639 goto inv_length;
1653 1640
@@ -1659,7 +1646,7 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1659 1646
1660 case USB_DT_ENDPOINT: { 1647 case USB_DT_ENDPOINT: {
1661 struct usb_endpoint_descriptor *ds = (void *)_ds; 1648 struct usb_endpoint_descriptor *ds = (void *)_ds;
1662 FVDBG("endpoint descriptor"); 1649 pr_vdebug("endpoint descriptor\n");
1663 if (length != USB_DT_ENDPOINT_SIZE && 1650 if (length != USB_DT_ENDPOINT_SIZE &&
1664 length != USB_DT_ENDPOINT_AUDIO_SIZE) 1651 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1665 goto inv_length; 1652 goto inv_length;
@@ -1674,7 +1661,7 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1674 1661
1675 case USB_DT_INTERFACE_ASSOCIATION: { 1662 case USB_DT_INTERFACE_ASSOCIATION: {
1676 struct usb_interface_assoc_descriptor *ds = (void *)_ds; 1663 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1677 FVDBG("interface association descriptor"); 1664 pr_vdebug("interface association descriptor\n");
1678 if (length != sizeof *ds) 1665 if (length != sizeof *ds)
1679 goto inv_length; 1666 goto inv_length;
1680 if (ds->iFunction) 1667 if (ds->iFunction)
@@ -1688,17 +1675,17 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1688 case USB_DT_SECURITY: 1675 case USB_DT_SECURITY:
1689 case USB_DT_CS_RADIO_CONTROL: 1676 case USB_DT_CS_RADIO_CONTROL:
1690 /* TODO */ 1677 /* TODO */
1691 FVDBG("unimplemented descriptor: %d", _ds->bDescriptorType); 1678 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1692 return -EINVAL; 1679 return -EINVAL;
1693 1680
1694 default: 1681 default:
1695 /* We should never be here */ 1682 /* We should never be here */
1696 FVDBG("unknown descriptor: %d", _ds->bDescriptorType); 1683 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1697 return -EINVAL; 1684 return -EINVAL;
1698 1685
1699 inv_length: 1686inv_length:
1700 FVDBG("invalid length: %d (descriptor %d)", 1687 pr_vdebug("invalid length: %d (descriptor %d)\n",
1701 _ds->bLength, _ds->bDescriptorType); 1688 _ds->bLength, _ds->bDescriptorType);
1702 return -EINVAL; 1689 return -EINVAL;
1703 } 1690 }
1704 1691
@@ -1711,7 +1698,6 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1711 return length; 1698 return length;
1712} 1699}
1713 1700
1714
1715static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len, 1701static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1716 ffs_entity_callback entity, void *priv) 1702 ffs_entity_callback entity, void *priv)
1717{ 1703{
@@ -1726,10 +1712,11 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1726 if (num == count) 1712 if (num == count)
1727 data = NULL; 1713 data = NULL;
1728 1714
1729 /* Record "descriptor" entitny */ 1715 /* Record "descriptor" entity */
1730 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); 1716 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1731 if (unlikely(ret < 0)) { 1717 if (unlikely(ret < 0)) {
1732 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num, ret); 1718 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1719 num, ret);
1733 return ret; 1720 return ret;
1734 } 1721 }
1735 1722
@@ -1738,7 +1725,7 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1738 1725
1739 ret = ffs_do_desc(data, len, entity, priv); 1726 ret = ffs_do_desc(data, len, entity, priv);
1740 if (unlikely(ret < 0)) { 1727 if (unlikely(ret < 0)) {
1741 FDBG("%s returns %d", __func__, ret); 1728 pr_debug("%s returns %d\n", __func__, ret);
1742 return ret; 1729 return ret;
1743 } 1730 }
1744 1731
@@ -1748,7 +1735,6 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1748 } 1735 }
1749} 1736}
1750 1737
1751
1752static int __ffs_data_do_entity(enum ffs_entity_type type, 1738static int __ffs_data_do_entity(enum ffs_entity_type type,
1753 u8 *valuep, struct usb_descriptor_header *desc, 1739 u8 *valuep, struct usb_descriptor_header *desc,
1754 void *priv) 1740 void *priv)
@@ -1762,16 +1748,20 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
1762 break; 1748 break;
1763 1749
1764 case FFS_INTERFACE: 1750 case FFS_INTERFACE:
1765 /* Interfaces are indexed from zero so if we 1751 /*
1752 * Interfaces are indexed from zero so if we
1766 * encountered interface "n" then there are at least 1753 * encountered interface "n" then there are at least
1767 * "n+1" interfaces. */ 1754 * "n+1" interfaces.
1755 */
1768 if (*valuep >= ffs->interfaces_count) 1756 if (*valuep >= ffs->interfaces_count)
1769 ffs->interfaces_count = *valuep + 1; 1757 ffs->interfaces_count = *valuep + 1;
1770 break; 1758 break;
1771 1759
1772 case FFS_STRING: 1760 case FFS_STRING:
1773 /* Strings are indexed from 1 (0 is magic ;) reserved 1761 /*
1774 * for languages list or some such) */ 1762 * Strings are indexed from 1 (0 is magic ;) reserved
1763 * for languages list or some such)
1764 */
1775 if (*valuep > ffs->strings_count) 1765 if (*valuep > ffs->strings_count)
1776 ffs->strings_count = *valuep; 1766 ffs->strings_count = *valuep;
1777 break; 1767 break;
@@ -1786,7 +1776,6 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
1786 return 0; 1776 return 0;
1787} 1777}
1788 1778
1789
1790static int __ffs_data_got_descs(struct ffs_data *ffs, 1779static int __ffs_data_got_descs(struct ffs_data *ffs,
1791 char *const _data, size_t len) 1780 char *const _data, size_t len)
1792{ 1781{
@@ -1849,8 +1838,6 @@ error:
1849 return ret; 1838 return ret;
1850} 1839}
1851 1840
1852
1853
1854static int __ffs_data_got_strings(struct ffs_data *ffs, 1841static int __ffs_data_got_strings(struct ffs_data *ffs,
1855 char *const _data, size_t len) 1842 char *const _data, size_t len)
1856{ 1843{
@@ -1876,17 +1863,17 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,
1876 if (unlikely(str_count < needed_count)) 1863 if (unlikely(str_count < needed_count))
1877 goto error; 1864 goto error;
1878 1865
1879 /* If we don't need any strings just return and free all 1866 /*
1880 * memory */ 1867 * If we don't need any strings just return and free all
1868 * memory.
1869 */
1881 if (!needed_count) { 1870 if (!needed_count) {
1882 kfree(_data); 1871 kfree(_data);
1883 return 0; 1872 return 0;
1884 } 1873 }
1885 1874
1886 /* Allocate */ 1875 /* Allocate everything in one chunk so there's less maintenance. */
1887 { 1876 {
1888 /* Allocate everything in one chunk so there's less
1889 * maintanance. */
1890 struct { 1877 struct {
1891 struct usb_gadget_strings *stringtabs[lang_count + 1]; 1878 struct usb_gadget_strings *stringtabs[lang_count + 1];
1892 struct usb_gadget_strings stringtab[lang_count]; 1879 struct usb_gadget_strings stringtab[lang_count];
@@ -1937,13 +1924,17 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,
1937 if (unlikely(length == len)) 1924 if (unlikely(length == len))
1938 goto error_free; 1925 goto error_free;
1939 1926
1940 /* user may provide more strings then we need, 1927 /*
1941 * if that's the case we simply ingore the 1928 * User may provide more strings then we need,
1942 * rest */ 1929 * if that's the case we simply ignore the
1930 * rest
1931 */
1943 if (likely(needed)) { 1932 if (likely(needed)) {
1944 /* s->id will be set while adding 1933 /*
1934 * s->id will be set while adding
1945 * function to configuration so for 1935 * function to configuration so for
1946 * now just leave garbage here. */ 1936 * now just leave garbage here.
1937 */
1947 s->s = data; 1938 s->s = data;
1948 --needed; 1939 --needed;
1949 ++s; 1940 ++s;
@@ -1977,8 +1968,6 @@ error:
1977} 1968}
1978 1969
1979 1970
1980
1981
1982/* Events handling and management *******************************************/ 1971/* Events handling and management *******************************************/
1983 1972
1984static void __ffs_event_add(struct ffs_data *ffs, 1973static void __ffs_event_add(struct ffs_data *ffs,
@@ -1987,29 +1976,32 @@ static void __ffs_event_add(struct ffs_data *ffs,
1987 enum usb_functionfs_event_type rem_type1, rem_type2 = type; 1976 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1988 int neg = 0; 1977 int neg = 0;
1989 1978
1990 /* Abort any unhandled setup */ 1979 /*
1991 /* We do not need to worry about some cmpxchg() changing value 1980 * Abort any unhandled setup
1981 *
1982 * We do not need to worry about some cmpxchg() changing value
1992 * of ffs->setup_state without holding the lock because when 1983 * of ffs->setup_state without holding the lock because when
1993 * state is FFS_SETUP_PENDING cmpxchg() in several places in 1984 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1994 * the source does nothing. */ 1985 * the source does nothing.
1986 */
1995 if (ffs->setup_state == FFS_SETUP_PENDING) 1987 if (ffs->setup_state == FFS_SETUP_PENDING)
1996 ffs->setup_state = FFS_SETUP_CANCELED; 1988 ffs->setup_state = FFS_SETUP_CANCELED;
1997 1989
1998 switch (type) { 1990 switch (type) {
1999 case FUNCTIONFS_RESUME: 1991 case FUNCTIONFS_RESUME:
2000 rem_type2 = FUNCTIONFS_SUSPEND; 1992 rem_type2 = FUNCTIONFS_SUSPEND;
2001 /* FALL THGOUTH */ 1993 /* FALL THROUGH */
2002 case FUNCTIONFS_SUSPEND: 1994 case FUNCTIONFS_SUSPEND:
2003 case FUNCTIONFS_SETUP: 1995 case FUNCTIONFS_SETUP:
2004 rem_type1 = type; 1996 rem_type1 = type;
2005 /* discard all similar events */ 1997 /* Discard all similar events */
2006 break; 1998 break;
2007 1999
2008 case FUNCTIONFS_BIND: 2000 case FUNCTIONFS_BIND:
2009 case FUNCTIONFS_UNBIND: 2001 case FUNCTIONFS_UNBIND:
2010 case FUNCTIONFS_DISABLE: 2002 case FUNCTIONFS_DISABLE:
2011 case FUNCTIONFS_ENABLE: 2003 case FUNCTIONFS_ENABLE:
2012 /* discard everything other then power management. */ 2004 /* Discard everything other then power management. */
2013 rem_type1 = FUNCTIONFS_SUSPEND; 2005 rem_type1 = FUNCTIONFS_SUSPEND;
2014 rem_type2 = FUNCTIONFS_RESUME; 2006 rem_type2 = FUNCTIONFS_RESUME;
2015 neg = 1; 2007 neg = 1;
@@ -2026,11 +2018,11 @@ static void __ffs_event_add(struct ffs_data *ffs,
2026 if ((*ev == rem_type1 || *ev == rem_type2) == neg) 2018 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2027 *out++ = *ev; 2019 *out++ = *ev;
2028 else 2020 else
2029 FVDBG("purging event %d", *ev); 2021 pr_vdebug("purging event %d\n", *ev);
2030 ffs->ev.count = out - ffs->ev.types; 2022 ffs->ev.count = out - ffs->ev.types;
2031 } 2023 }
2032 2024
2033 FVDBG("adding event %d", type); 2025 pr_vdebug("adding event %d\n", type);
2034 ffs->ev.types[ffs->ev.count++] = type; 2026 ffs->ev.types[ffs->ev.count++] = type;
2035 wake_up_locked(&ffs->ev.waitq); 2027 wake_up_locked(&ffs->ev.waitq);
2036} 2028}
@@ -2055,8 +2047,10 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2055 struct ffs_function *func = priv; 2047 struct ffs_function *func = priv;
2056 struct ffs_ep *ffs_ep; 2048 struct ffs_ep *ffs_ep;
2057 2049
2058 /* If hs_descriptors is not NULL then we are reading hs 2050 /*
2059 * descriptors now */ 2051 * If hs_descriptors is not NULL then we are reading hs
2052 * descriptors now
2053 */
2060 const int isHS = func->function.hs_descriptors != NULL; 2054 const int isHS = func->function.hs_descriptors != NULL;
2061 unsigned idx; 2055 unsigned idx;
2062 2056
@@ -2075,9 +2069,9 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2075 ffs_ep = func->eps + idx; 2069 ffs_ep = func->eps + idx;
2076 2070
2077 if (unlikely(ffs_ep->descs[isHS])) { 2071 if (unlikely(ffs_ep->descs[isHS])) {
2078 FVDBG("two %sspeed descriptors for EP %d", 2072 pr_vdebug("two %sspeed descriptors for EP %d\n",
2079 isHS ? "high" : "full", 2073 isHS ? "high" : "full",
2080 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 2074 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2081 return -EINVAL; 2075 return -EINVAL;
2082 } 2076 }
2083 ffs_ep->descs[isHS] = ds; 2077 ffs_ep->descs[isHS] = ds;
@@ -2091,11 +2085,11 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2091 struct usb_request *req; 2085 struct usb_request *req;
2092 struct usb_ep *ep; 2086 struct usb_ep *ep;
2093 2087
2094 FVDBG("autoconfig"); 2088 pr_vdebug("autoconfig\n");
2095 ep = usb_ep_autoconfig(func->gadget, ds); 2089 ep = usb_ep_autoconfig(func->gadget, ds);
2096 if (unlikely(!ep)) 2090 if (unlikely(!ep))
2097 return -ENOTSUPP; 2091 return -ENOTSUPP;
2098 ep->driver_data = func->eps + idx;; 2092 ep->driver_data = func->eps + idx;
2099 2093
2100 req = usb_ep_alloc_request(ep, GFP_KERNEL); 2094 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2101 if (unlikely(!req)) 2095 if (unlikely(!req))
@@ -2111,7 +2105,6 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2111 return 0; 2105 return 0;
2112} 2106}
2113 2107
2114
2115static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, 2108static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2116 struct usb_descriptor_header *desc, 2109 struct usb_descriptor_header *desc,
2117 void *priv) 2110 void *priv)
@@ -2143,8 +2136,10 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2143 break; 2136 break;
2144 2137
2145 case FFS_ENDPOINT: 2138 case FFS_ENDPOINT:
2146 /* USB_DT_ENDPOINT are handled in 2139 /*
2147 * __ffs_func_bind_do_descs(). */ 2140 * USB_DT_ENDPOINT are handled in
2141 * __ffs_func_bind_do_descs().
2142 */
2148 if (desc->bDescriptorType == USB_DT_ENDPOINT) 2143 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2149 return 0; 2144 return 0;
2150 2145
@@ -2160,7 +2155,7 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2160 break; 2155 break;
2161 } 2156 }
2162 2157
2163 FVDBG("%02x -> %02x", *valuep, newValue); 2158 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2164 *valuep = newValue; 2159 *valuep = newValue;
2165 return 0; 2160 return 0;
2166} 2161}
@@ -2211,9 +2206,11 @@ static int ffs_func_bind(struct usb_configuration *c,
2211 func->eps = data->eps; 2206 func->eps = data->eps;
2212 func->interfaces_nums = data->inums; 2207 func->interfaces_nums = data->inums;
2213 2208
2214 /* Go throught all the endpoint descriptors and allocate 2209 /*
2210 * Go through all the endpoint descriptors and allocate
2215 * endpoints first, so that later we can rewrite the endpoint 2211 * endpoints first, so that later we can rewrite the endpoint
2216 * numbers without worying that it may be described later on. */ 2212 * numbers without worrying that it may be described later on.
2213 */
2217 if (likely(full)) { 2214 if (likely(full)) {
2218 func->function.descriptors = data->fs_descs; 2215 func->function.descriptors = data->fs_descs;
2219 ret = ffs_do_descs(ffs->fs_descs_count, 2216 ret = ffs_do_descs(ffs->fs_descs_count,
@@ -2234,9 +2231,11 @@ static int ffs_func_bind(struct usb_configuration *c,
2234 __ffs_func_bind_do_descs, func); 2231 __ffs_func_bind_do_descs, func);
2235 } 2232 }
2236 2233
2237 /* Now handle interface numbers allocation and interface and 2234 /*
2238 * enpoint numbers rewritting. We can do that in one go 2235 * Now handle interface numbers allocation and interface and
2239 * now. */ 2236 * endpoint numbers rewriting. We can do that in one go
2237 * now.
2238 */
2240 ret = ffs_do_descs(ffs->fs_descs_count + 2239 ret = ffs_do_descs(ffs->fs_descs_count +
2241 (high ? ffs->hs_descs_count : 0), 2240 (high ? ffs->hs_descs_count : 0),
2242 data->raw_descs, sizeof data->raw_descs, 2241 data->raw_descs, sizeof data->raw_descs,
@@ -2274,7 +2273,6 @@ static void ffs_func_unbind(struct usb_configuration *c,
2274 ffs_func_free(func); 2273 ffs_func_free(func);
2275} 2274}
2276 2275
2277
2278static int ffs_func_set_alt(struct usb_function *f, 2276static int ffs_func_set_alt(struct usb_function *f,
2279 unsigned interface, unsigned alt) 2277 unsigned interface, unsigned alt)
2280{ 2278{
@@ -2322,20 +2320,21 @@ static int ffs_func_setup(struct usb_function *f,
2322 2320
2323 ENTER(); 2321 ENTER();
2324 2322
2325 FVDBG("creq->bRequestType = %02x", creq->bRequestType); 2323 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2326 FVDBG("creq->bRequest = %02x", creq->bRequest); 2324 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2327 FVDBG("creq->wValue = %04x", le16_to_cpu(creq->wValue)); 2325 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2328 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq->wIndex)); 2326 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2329 FVDBG("creq->wLength = %04x", le16_to_cpu(creq->wLength)); 2327 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2330 2328
2331 /* Most requests directed to interface go throught here 2329 /*
2330 * Most requests directed to interface go through here
2332 * (notable exceptions are set/get interface) so we need to 2331 * (notable exceptions are set/get interface) so we need to
2333 * handle them. All other either handled by composite or 2332 * handle them. All other either handled by composite or
2334 * passed to usb_configuration->setup() (if one is set). No 2333 * passed to usb_configuration->setup() (if one is set). No
2335 * matter, we will handle requests directed to endpoint here 2334 * matter, we will handle requests directed to endpoint here
2336 * as well (as it's straightforward) but what to do with any 2335 * as well (as it's straightforward) but what to do with any
2337 * other request? */ 2336 * other request?
2338 2337 */
2339 if (ffs->state != FFS_ACTIVE) 2338 if (ffs->state != FFS_ACTIVE)
2340 return -ENODEV; 2339 return -ENODEV;
2341 2340
@@ -2378,8 +2377,7 @@ static void ffs_func_resume(struct usb_function *f)
2378} 2377}
2379 2378
2380 2379
2381 2380/* Endpoint and interface numbers reverse mapping ***************************/
2382/* Enpoint and interface numbers reverse mapping ****************************/
2383 2381
2384static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) 2382static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2385{ 2383{
@@ -2410,7 +2408,6 @@ static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2410 : mutex_lock_interruptible(mutex); 2408 : mutex_lock_interruptible(mutex);
2411} 2409}
2412 2410
2413
2414static char *ffs_prepare_buffer(const char * __user buf, size_t len) 2411static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2415{ 2412{
2416 char *data; 2413 char *data;
@@ -2427,7 +2424,7 @@ static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2427 return ERR_PTR(-EFAULT); 2424 return ERR_PTR(-EFAULT);
2428 } 2425 }
2429 2426
2430 FVDBG("Buffer from user space:"); 2427 pr_vdebug("Buffer from user space:\n");
2431 ffs_dump_mem("", data, len); 2428 ffs_dump_mem("", data, len);
2432 2429
2433 return data; 2430 return data;
diff --git a/drivers/usb/gadget/f_hid.c b/drivers/usb/gadget/f_hid.c
index 53e120208e99..598e7e2ab80c 100644
--- a/drivers/usb/gadget/f_hid.c
+++ b/drivers/usb/gadget/f_hid.c
@@ -25,7 +25,6 @@
25#include <linux/cdev.h> 25#include <linux/cdev.h>
26#include <linux/mutex.h> 26#include <linux/mutex.h>
27#include <linux/poll.h> 27#include <linux/poll.h>
28#include <linux/smp_lock.h>
29#include <linux/uaccess.h> 28#include <linux/uaccess.h>
30#include <linux/wait.h> 29#include <linux/wait.h>
31#include <linux/usb/g_hid.h> 30#include <linux/usb/g_hid.h>
@@ -318,8 +317,6 @@ static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
318 spin_unlock(&hidg->spinlock); 317 spin_unlock(&hidg->spinlock);
319 318
320 wake_up(&hidg->read_queue); 319 wake_up(&hidg->read_queue);
321
322 return;
323} 320}
324 321
325static int hidg_setup(struct usb_function *f, 322static int hidg_setup(struct usb_function *f,
@@ -413,8 +410,6 @@ static void hidg_disable(struct usb_function *f)
413 410
414 usb_ep_disable(hidg->in_ep); 411 usb_ep_disable(hidg->in_ep);
415 hidg->in_ep->driver_data = NULL; 412 hidg->in_ep->driver_data = NULL;
416
417 return;
418} 413}
419 414
420static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 415static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
@@ -451,6 +446,7 @@ const struct file_operations f_hidg_fops = {
451 .write = f_hidg_write, 446 .write = f_hidg_write,
452 .read = f_hidg_read, 447 .read = f_hidg_read,
453 .poll = f_hidg_poll, 448 .poll = f_hidg_poll,
449 .llseek = noop_llseek,
454}; 450};
455 451
456static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f) 452static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
index 43225879c3cd..b37960f9e753 100644
--- a/drivers/usb/gadget/f_loopback.c
+++ b/drivers/usb/gadget/f_loopback.c
@@ -324,7 +324,7 @@ static void loopback_disable(struct usb_function *f)
324 324
325/*-------------------------------------------------------------------------*/ 325/*-------------------------------------------------------------------------*/
326 326
327static int __ref loopback_bind_config(struct usb_configuration *c) 327static int __init loopback_bind_config(struct usb_configuration *c)
328{ 328{
329 struct f_loopback *loop; 329 struct f_loopback *loop;
330 int status; 330 int status;
@@ -346,10 +346,9 @@ static int __ref loopback_bind_config(struct usb_configuration *c)
346 return status; 346 return status;
347} 347}
348 348
349static struct usb_configuration loopback_driver = { 349static struct usb_configuration loopback_driver = {
350 .label = "loopback", 350 .label = "loopback",
351 .strings = loopback_strings, 351 .strings = loopback_strings,
352 .bind = loopback_bind_config,
353 .bConfigurationValue = 2, 352 .bConfigurationValue = 2,
354 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 353 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
355 /* .iConfiguration = DYNAMIC */ 354 /* .iConfiguration = DYNAMIC */
@@ -382,5 +381,5 @@ int __init loopback_add(struct usb_composite_dev *cdev, bool autoresume)
382 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 381 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
383 } 382 }
384 383
385 return usb_add_config(cdev, &loopback_driver); 384 return usb_add_config(cdev, &loopback_driver, loopback_bind_config);
386} 385}
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 32cce029f65c..efb58f9f5aa9 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -37,7 +37,6 @@
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */ 38 */
39 39
40
41/* 40/*
42 * The Mass Storage Function acts as a USB Mass Storage device, 41 * The Mass Storage Function acts as a USB Mass Storage device,
43 * appearing to the host as a disk drive or as a CD-ROM drive. In 42 * appearing to the host as a disk drive or as a CD-ROM drive. In
@@ -73,6 +72,8 @@
73 * being removable. 72 * being removable.
74 * ->cdrom Flag specifying that LUN shall be reported as 73 * ->cdrom Flag specifying that LUN shall be reported as
75 * being a CD-ROM. 74 * being a CD-ROM.
75 * ->nofua Flag specifying that FUA flag in SCSI WRITE(10,12)
76 * commands for this LUN shall be ignored.
76 * 77 *
77 * lun_name_format A printf-like format for names of the LUN 78 * lun_name_format A printf-like format for names of the LUN
78 * devices. This determines how the 79 * devices. This determines how the
@@ -127,6 +128,8 @@
127 * Default true, boolean for removable media. 128 * Default true, boolean for removable media.
128 * cdrom=b[,b...] Default false, boolean for whether to emulate 129 * cdrom=b[,b...] Default false, boolean for whether to emulate
129 * a CD-ROM drive. 130 * a CD-ROM drive.
131 * nofua=b[,b...] Default false, booleans for ignore FUA flag
132 * in SCSI WRITE(10,12) commands
130 * luns=N Default N = number of filenames, number of 133 * luns=N Default N = number of filenames, number of
131 * LUNs to support. 134 * LUNs to support.
132 * stall Default determined according to the type of 135 * stall Default determined according to the type of
@@ -181,7 +184,6 @@
181 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>. 184 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
182 */ 185 */
183 186
184
185/* 187/*
186 * Driver Design 188 * Driver Design
187 * 189 *
@@ -271,7 +273,6 @@
271/* #define VERBOSE_DEBUG */ 273/* #define VERBOSE_DEBUG */
272/* #define DUMP_MSGS */ 274/* #define DUMP_MSGS */
273 275
274
275#include <linux/blkdev.h> 276#include <linux/blkdev.h>
276#include <linux/completion.h> 277#include <linux/completion.h>
277#include <linux/dcache.h> 278#include <linux/dcache.h>
@@ -292,11 +293,11 @@
292 293
293#include <linux/usb/ch9.h> 294#include <linux/usb/ch9.h>
294#include <linux/usb/gadget.h> 295#include <linux/usb/gadget.h>
296#include <linux/usb/composite.h>
295 297
296#include "gadget_chips.h" 298#include "gadget_chips.h"
297 299
298 300
299
300/*------------------------------------------------------------------------*/ 301/*------------------------------------------------------------------------*/
301 302
302#define FSG_DRIVER_DESC "Mass Storage Function" 303#define FSG_DRIVER_DESC "Mass Storage Function"
@@ -304,7 +305,6 @@
304 305
305static const char fsg_string_interface[] = "Mass Storage"; 306static const char fsg_string_interface[] = "Mass Storage";
306 307
307
308#define FSG_NO_INTR_EP 1 308#define FSG_NO_INTR_EP 1
309#define FSG_NO_DEVICE_STRINGS 1 309#define FSG_NO_DEVICE_STRINGS 1
310#define FSG_NO_OTG 1 310#define FSG_NO_OTG 1
@@ -320,28 +320,34 @@ struct fsg_common;
320 320
321/* FSF callback functions */ 321/* FSF callback functions */
322struct fsg_operations { 322struct fsg_operations {
323 /* Callback function to call when thread exits. If no 323 /*
324 * Callback function to call when thread exits. If no
324 * callback is set or it returns value lower then zero MSF 325 * callback is set or it returns value lower then zero MSF
325 * will force eject all LUNs it operates on (including those 326 * will force eject all LUNs it operates on (including those
326 * marked as non-removable or with prevent_medium_removal flag 327 * marked as non-removable or with prevent_medium_removal flag
327 * set). */ 328 * set).
329 */
328 int (*thread_exits)(struct fsg_common *common); 330 int (*thread_exits)(struct fsg_common *common);
329 331
330 /* Called prior to ejection. Negative return means error, 332 /*
333 * Called prior to ejection. Negative return means error,
331 * zero means to continue with ejection, positive means not to 334 * zero means to continue with ejection, positive means not to
332 * eject. */ 335 * eject.
336 */
333 int (*pre_eject)(struct fsg_common *common, 337 int (*pre_eject)(struct fsg_common *common,
334 struct fsg_lun *lun, int num); 338 struct fsg_lun *lun, int num);
335 /* Called after ejection. Negative return means error, zero 339 /*
336 * or positive is just a success. */ 340 * Called after ejection. Negative return means error, zero
341 * or positive is just a success.
342 */
337 int (*post_eject)(struct fsg_common *common, 343 int (*post_eject)(struct fsg_common *common,
338 struct fsg_lun *lun, int num); 344 struct fsg_lun *lun, int num);
339}; 345};
340 346
341
342/* Data shared by all the FSG instances. */ 347/* Data shared by all the FSG instances. */
343struct fsg_common { 348struct fsg_common {
344 struct usb_gadget *gadget; 349 struct usb_gadget *gadget;
350 struct usb_composite_dev *cdev;
345 struct fsg_dev *fsg, *new_fsg; 351 struct fsg_dev *fsg, *new_fsg;
346 wait_queue_head_t fsg_wait; 352 wait_queue_head_t fsg_wait;
347 353
@@ -394,14 +400,15 @@ struct fsg_common {
394 /* Gadget's private data. */ 400 /* Gadget's private data. */
395 void *private_data; 401 void *private_data;
396 402
397 /* Vendor (8 chars), product (16 chars), release (4 403 /*
398 * hexadecimal digits) and NUL byte */ 404 * Vendor (8 chars), product (16 chars), release (4
405 * hexadecimal digits) and NUL byte
406 */
399 char inquiry_string[8 + 16 + 4 + 1]; 407 char inquiry_string[8 + 16 + 4 + 1];
400 408
401 struct kref ref; 409 struct kref ref;
402}; 410};
403 411
404
405struct fsg_config { 412struct fsg_config {
406 unsigned nluns; 413 unsigned nluns;
407 struct fsg_lun_config { 414 struct fsg_lun_config {
@@ -409,6 +416,7 @@ struct fsg_config {
409 char ro; 416 char ro;
410 char removable; 417 char removable;
411 char cdrom; 418 char cdrom;
419 char nofua;
412 } luns[FSG_MAX_LUNS]; 420 } luns[FSG_MAX_LUNS];
413 421
414 const char *lun_name_format; 422 const char *lun_name_format;
@@ -426,7 +434,6 @@ struct fsg_config {
426 char can_stall; 434 char can_stall;
427}; 435};
428 436
429
430struct fsg_dev { 437struct fsg_dev {
431 struct usb_function function; 438 struct usb_function function;
432 struct usb_gadget *gadget; /* Copy of cdev->gadget */ 439 struct usb_gadget *gadget; /* Copy of cdev->gadget */
@@ -444,7 +451,6 @@ struct fsg_dev {
444 struct usb_ep *bulk_out; 451 struct usb_ep *bulk_out;
445}; 452};
446 453
447
448static inline int __fsg_is_set(struct fsg_common *common, 454static inline int __fsg_is_set(struct fsg_common *common,
449 const char *func, unsigned line) 455 const char *func, unsigned line)
450{ 456{
@@ -457,13 +463,11 @@ static inline int __fsg_is_set(struct fsg_common *common,
457 463
458#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) 464#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
459 465
460
461static inline struct fsg_dev *fsg_from_func(struct usb_function *f) 466static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
462{ 467{
463 return container_of(f, struct fsg_dev, function); 468 return container_of(f, struct fsg_dev, function);
464} 469}
465 470
466
467typedef void (*fsg_routine_t)(struct fsg_dev *); 471typedef void (*fsg_routine_t)(struct fsg_dev *);
468 472
469static int exception_in_progress(struct fsg_common *common) 473static int exception_in_progress(struct fsg_common *common)
@@ -473,7 +477,7 @@ static int exception_in_progress(struct fsg_common *common)
473 477
474/* Make bulk-out requests be divisible by the maxpacket size */ 478/* Make bulk-out requests be divisible by the maxpacket size */
475static void set_bulk_out_req_length(struct fsg_common *common, 479static void set_bulk_out_req_length(struct fsg_common *common,
476 struct fsg_buffhd *bh, unsigned int length) 480 struct fsg_buffhd *bh, unsigned int length)
477{ 481{
478 unsigned int rem; 482 unsigned int rem;
479 483
@@ -484,6 +488,7 @@ static void set_bulk_out_req_length(struct fsg_common *common,
484 bh->outreq->length = length; 488 bh->outreq->length = length;
485} 489}
486 490
491
487/*-------------------------------------------------------------------------*/ 492/*-------------------------------------------------------------------------*/
488 493
489static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) 494static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
@@ -514,14 +519,15 @@ static void wakeup_thread(struct fsg_common *common)
514 wake_up_process(common->thread_task); 519 wake_up_process(common->thread_task);
515} 520}
516 521
517
518static void raise_exception(struct fsg_common *common, enum fsg_state new_state) 522static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
519{ 523{
520 unsigned long flags; 524 unsigned long flags;
521 525
522 /* Do nothing if a higher-priority exception is already in progress. 526 /*
527 * Do nothing if a higher-priority exception is already in progress.
523 * If a lower-or-equal priority exception is in progress, preempt it 528 * If a lower-or-equal priority exception is in progress, preempt it
524 * and notify the main thread by sending it a signal. */ 529 * and notify the main thread by sending it a signal.
530 */
525 spin_lock_irqsave(&common->lock, flags); 531 spin_lock_irqsave(&common->lock, flags);
526 if (common->state <= new_state) { 532 if (common->state <= new_state) {
527 common->exception_req_tag = common->ep0_req_tag; 533 common->exception_req_tag = common->ep0_req_tag;
@@ -550,10 +556,10 @@ static int ep0_queue(struct fsg_common *common)
550 return rc; 556 return rc;
551} 557}
552 558
559
553/*-------------------------------------------------------------------------*/ 560/*-------------------------------------------------------------------------*/
554 561
555/* Bulk and interrupt endpoint completion handlers. 562/* Completion handlers. These always run in_irq. */
556 * These always run in_irq. */
557 563
558static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req) 564static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
559{ 565{
@@ -562,7 +568,7 @@ static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
562 568
563 if (req->status || req->actual != req->length) 569 if (req->status || req->actual != req->length)
564 DBG(common, "%s --> %d, %u/%u\n", __func__, 570 DBG(common, "%s --> %d, %u/%u\n", __func__,
565 req->status, req->actual, req->length); 571 req->status, req->actual, req->length);
566 if (req->status == -ECONNRESET) /* Request was cancelled */ 572 if (req->status == -ECONNRESET) /* Request was cancelled */
567 usb_ep_fifo_flush(ep); 573 usb_ep_fifo_flush(ep);
568 574
@@ -583,8 +589,7 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
583 dump_msg(common, "bulk-out", req->buf, req->actual); 589 dump_msg(common, "bulk-out", req->buf, req->actual);
584 if (req->status || req->actual != bh->bulk_out_intended_length) 590 if (req->status || req->actual != bh->bulk_out_intended_length)
585 DBG(common, "%s --> %d, %u/%u\n", __func__, 591 DBG(common, "%s --> %d, %u/%u\n", __func__,
586 req->status, req->actual, 592 req->status, req->actual, bh->bulk_out_intended_length);
587 bh->bulk_out_intended_length);
588 if (req->status == -ECONNRESET) /* Request was cancelled */ 593 if (req->status == -ECONNRESET) /* Request was cancelled */
589 usb_ep_fifo_flush(ep); 594 usb_ep_fifo_flush(ep);
590 595
@@ -597,13 +602,8 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
597 spin_unlock(&common->lock); 602 spin_unlock(&common->lock);
598} 603}
599 604
600
601/*-------------------------------------------------------------------------*/
602
603/* Ep0 class-specific handlers. These always run in_irq. */
604
605static int fsg_setup(struct usb_function *f, 605static int fsg_setup(struct usb_function *f,
606 const struct usb_ctrlrequest *ctrl) 606 const struct usb_ctrlrequest *ctrl)
607{ 607{
608 struct fsg_dev *fsg = fsg_from_func(f); 608 struct fsg_dev *fsg = fsg_from_func(f);
609 struct usb_request *req = fsg->common->ep0req; 609 struct usb_request *req = fsg->common->ep0req;
@@ -614,6 +614,11 @@ static int fsg_setup(struct usb_function *f,
614 if (!fsg_is_set(fsg->common)) 614 if (!fsg_is_set(fsg->common))
615 return -EOPNOTSUPP; 615 return -EOPNOTSUPP;
616 616
617 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */
618 req->context = NULL;
619 req->length = 0;
620 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
621
617 switch (ctrl->bRequest) { 622 switch (ctrl->bRequest) {
618 623
619 case USB_BULK_RESET_REQUEST: 624 case USB_BULK_RESET_REQUEST:
@@ -623,8 +628,10 @@ static int fsg_setup(struct usb_function *f,
623 if (w_index != fsg->interface_number || w_value != 0) 628 if (w_index != fsg->interface_number || w_value != 0)
624 return -EDOM; 629 return -EDOM;
625 630
626 /* Raise an exception to stop the current operation 631 /*
627 * and reinitialize our state. */ 632 * Raise an exception to stop the current operation
633 * and reinitialize our state.
634 */
628 DBG(fsg, "bulk reset request\n"); 635 DBG(fsg, "bulk reset request\n");
629 raise_exception(fsg->common, FSG_STATE_RESET); 636 raise_exception(fsg->common, FSG_STATE_RESET);
630 return DELAYED_STATUS; 637 return DELAYED_STATUS;
@@ -636,7 +643,7 @@ static int fsg_setup(struct usb_function *f,
636 if (w_index != fsg->interface_number || w_value != 0) 643 if (w_index != fsg->interface_number || w_value != 0)
637 return -EDOM; 644 return -EDOM;
638 VDBG(fsg, "get max LUN\n"); 645 VDBG(fsg, "get max LUN\n");
639 *(u8 *) req->buf = fsg->common->nluns - 1; 646 *(u8 *)req->buf = fsg->common->nluns - 1;
640 647
641 /* Respond with data/status */ 648 /* Respond with data/status */
642 req->length = min((u16)1, w_length); 649 req->length = min((u16)1, w_length);
@@ -644,8 +651,7 @@ static int fsg_setup(struct usb_function *f,
644 } 651 }
645 652
646 VDBG(fsg, 653 VDBG(fsg,
647 "unknown class-specific control req " 654 "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",
648 "%02x.%02x v%04x i%04x l%u\n",
649 ctrl->bRequestType, ctrl->bRequest, 655 ctrl->bRequestType, ctrl->bRequest,
650 le16_to_cpu(ctrl->wValue), w_index, w_length); 656 le16_to_cpu(ctrl->wValue), w_index, w_length);
651 return -EOPNOTSUPP; 657 return -EOPNOTSUPP;
@@ -656,11 +662,10 @@ static int fsg_setup(struct usb_function *f,
656 662
657/* All the following routines run in process context */ 663/* All the following routines run in process context */
658 664
659
660/* Use this for bulk or interrupt transfers, not ep0 */ 665/* Use this for bulk or interrupt transfers, not ep0 */
661static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, 666static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
662 struct usb_request *req, int *pbusy, 667 struct usb_request *req, int *pbusy,
663 enum fsg_buffer_state *state) 668 enum fsg_buffer_state *state)
664{ 669{
665 int rc; 670 int rc;
666 671
@@ -678,25 +683,34 @@ static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
678 683
679 /* We can't do much more than wait for a reset */ 684 /* We can't do much more than wait for a reset */
680 685
681 /* Note: currently the net2280 driver fails zero-length 686 /*
682 * submissions if DMA is enabled. */ 687 * Note: currently the net2280 driver fails zero-length
683 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP && 688 * submissions if DMA is enabled.
684 req->length == 0)) 689 */
690 if (rc != -ESHUTDOWN &&
691 !(rc == -EOPNOTSUPP && req->length == 0))
685 WARNING(fsg, "error in submission: %s --> %d\n", 692 WARNING(fsg, "error in submission: %s --> %d\n",
686 ep->name, rc); 693 ep->name, rc);
687 } 694 }
688} 695}
689 696
690#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \ 697static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
691 if (fsg_is_set(common)) \ 698{
692 start_transfer((common)->fsg, (common)->fsg->ep_name, \ 699 if (!fsg_is_set(common))
693 req, pbusy, state); \ 700 return false;
694 else 701 start_transfer(common->fsg, common->fsg->bulk_in,
695 702 bh->inreq, &bh->inreq_busy, &bh->state);
696#define START_TRANSFER(common, ep_name, req, pbusy, state) \ 703 return true;
697 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0 704}
698
699 705
706static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
707{
708 if (!fsg_is_set(common))
709 return false;
710 start_transfer(common->fsg, common->fsg->bulk_out,
711 bh->outreq, &bh->outreq_busy, &bh->state);
712 return true;
713}
700 714
701static int sleep_thread(struct fsg_common *common) 715static int sleep_thread(struct fsg_common *common)
702{ 716{
@@ -734,16 +748,20 @@ static int do_read(struct fsg_common *common)
734 unsigned int partial_page; 748 unsigned int partial_page;
735 ssize_t nread; 749 ssize_t nread;
736 750
737 /* Get the starting Logical Block Address and check that it's 751 /*
738 * not too big */ 752 * Get the starting Logical Block Address and check that it's
739 if (common->cmnd[0] == SC_READ_6) 753 * not too big.
754 */
755 if (common->cmnd[0] == READ_6)
740 lba = get_unaligned_be24(&common->cmnd[1]); 756 lba = get_unaligned_be24(&common->cmnd[1]);
741 else { 757 else {
742 lba = get_unaligned_be32(&common->cmnd[2]); 758 lba = get_unaligned_be32(&common->cmnd[2]);
743 759
744 /* We allow DPO (Disable Page Out = don't save data in the 760 /*
761 * We allow DPO (Disable Page Out = don't save data in the
745 * cache) and FUA (Force Unit Access = don't read from the 762 * cache) and FUA (Force Unit Access = don't read from the
746 * cache), but we don't implement them. */ 763 * cache), but we don't implement them.
764 */
747 if ((common->cmnd[1] & ~0x18) != 0) { 765 if ((common->cmnd[1] & ~0x18) != 0) {
748 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 766 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
749 return -EINVAL; 767 return -EINVAL;
@@ -761,22 +779,23 @@ static int do_read(struct fsg_common *common)
761 return -EIO; /* No default reply */ 779 return -EIO; /* No default reply */
762 780
763 for (;;) { 781 for (;;) {
764 782 /*
765 /* Figure out how much we need to read: 783 * Figure out how much we need to read:
766 * Try to read the remaining amount. 784 * Try to read the remaining amount.
767 * But don't read more than the buffer size. 785 * But don't read more than the buffer size.
768 * 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.
769 * Finally, if we're not at a page boundary, don't read past 787 * Finally, if we're not at a page boundary, don't read past
770 * the next page. 788 * the next page.
771 * If this means reading 0 then we were asked to read past 789 * If this means reading 0 then we were asked to read past
772 * the end of file. */ 790 * the end of file.
791 */
773 amount = min(amount_left, FSG_BUFLEN); 792 amount = min(amount_left, FSG_BUFLEN);
774 amount = min((loff_t) amount, 793 amount = min((loff_t)amount,
775 curlun->file_length - file_offset); 794 curlun->file_length - file_offset);
776 partial_page = file_offset & (PAGE_CACHE_SIZE - 1); 795 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
777 if (partial_page > 0) 796 if (partial_page > 0)
778 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE - 797 amount = min(amount, (unsigned int)PAGE_CACHE_SIZE -
779 partial_page); 798 partial_page);
780 799
781 /* Wait for the next buffer to become available */ 800 /* Wait for the next buffer to become available */
782 bh = common->next_buffhd_to_fill; 801 bh = common->next_buffhd_to_fill;
@@ -786,8 +805,10 @@ static int do_read(struct fsg_common *common)
786 return rc; 805 return rc;
787 } 806 }
788 807
789 /* If we were asked to read past the end of file, 808 /*
790 * end with an empty buffer. */ 809 * If we were asked to read past the end of file,
810 * end with an empty buffer.
811 */
791 if (amount == 0) { 812 if (amount == 0) {
792 curlun->sense_data = 813 curlun->sense_data =
793 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 814 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
@@ -801,21 +822,19 @@ static int do_read(struct fsg_common *common)
801 /* Perform the read */ 822 /* Perform the read */
802 file_offset_tmp = file_offset; 823 file_offset_tmp = file_offset;
803 nread = vfs_read(curlun->filp, 824 nread = vfs_read(curlun->filp,
804 (char __user *) bh->buf, 825 (char __user *)bh->buf,
805 amount, &file_offset_tmp); 826 amount, &file_offset_tmp);
806 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount, 827 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
807 (unsigned long long) file_offset, 828 (unsigned long long)file_offset, (int)nread);
808 (int) nread);
809 if (signal_pending(current)) 829 if (signal_pending(current))
810 return -EINTR; 830 return -EINTR;
811 831
812 if (nread < 0) { 832 if (nread < 0) {
813 LDBG(curlun, "error in file read: %d\n", 833 LDBG(curlun, "error in file read: %d\n", (int)nread);
814 (int) nread);
815 nread = 0; 834 nread = 0;
816 } else if (nread < amount) { 835 } else if (nread < amount) {
817 LDBG(curlun, "partial file read: %d/%u\n", 836 LDBG(curlun, "partial file read: %d/%u\n",
818 (int) nread, amount); 837 (int)nread, amount);
819 nread -= (nread & 511); /* Round down to a block */ 838 nread -= (nread & 511); /* Round down to a block */
820 } 839 }
821 file_offset += nread; 840 file_offset += nread;
@@ -837,10 +856,8 @@ static int do_read(struct fsg_common *common)
837 856
838 /* Send this buffer and go read some more */ 857 /* Send this buffer and go read some more */
839 bh->inreq->zero = 0; 858 bh->inreq->zero = 0;
840 START_TRANSFER_OR(common, bulk_in, bh->inreq, 859 if (!start_in_transfer(common, bh))
841 &bh->inreq_busy, &bh->state) 860 /* Don't know what to do if common->fsg is NULL */
842 /* Don't know what to do if
843 * common->fsg is NULL */
844 return -EIO; 861 return -EIO;
845 common->next_buffhd_to_fill = bh->next; 862 common->next_buffhd_to_fill = bh->next;
846 } 863 }
@@ -872,22 +889,26 @@ static int do_write(struct fsg_common *common)
872 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */ 889 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
873 spin_unlock(&curlun->filp->f_lock); 890 spin_unlock(&curlun->filp->f_lock);
874 891
875 /* Get the starting Logical Block Address and check that it's 892 /*
876 * not too big */ 893 * Get the starting Logical Block Address and check that it's
877 if (common->cmnd[0] == SC_WRITE_6) 894 * not too big
895 */
896 if (common->cmnd[0] == WRITE_6)
878 lba = get_unaligned_be24(&common->cmnd[1]); 897 lba = get_unaligned_be24(&common->cmnd[1]);
879 else { 898 else {
880 lba = get_unaligned_be32(&common->cmnd[2]); 899 lba = get_unaligned_be32(&common->cmnd[2]);
881 900
882 /* We allow DPO (Disable Page Out = don't save data in the 901 /*
902 * We allow DPO (Disable Page Out = don't save data in the
883 * cache) and FUA (Force Unit Access = write directly to the 903 * cache) and FUA (Force Unit Access = write directly to the
884 * medium). We don't implement DPO; we implement FUA by 904 * medium). We don't implement DPO; we implement FUA by
885 * performing synchronous output. */ 905 * performing synchronous output.
906 */
886 if (common->cmnd[1] & ~0x18) { 907 if (common->cmnd[1] & ~0x18) {
887 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 908 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
888 return -EINVAL; 909 return -EINVAL;
889 } 910 }
890 if (common->cmnd[1] & 0x08) { /* FUA */ 911 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */
891 spin_lock(&curlun->filp->f_lock); 912 spin_lock(&curlun->filp->f_lock);
892 curlun->filp->f_flags |= O_SYNC; 913 curlun->filp->f_flags |= O_SYNC;
893 spin_unlock(&curlun->filp->f_lock); 914 spin_unlock(&curlun->filp->f_lock);
@@ -910,7 +931,8 @@ static int do_write(struct fsg_common *common)
910 bh = common->next_buffhd_to_fill; 931 bh = common->next_buffhd_to_fill;
911 if (bh->state == BUF_STATE_EMPTY && get_some_more) { 932 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
912 933
913 /* Figure out how much we want to get: 934 /*
935 * Figure out how much we want to get:
914 * Try to get the remaining amount. 936 * Try to get the remaining amount.
915 * But don't get more than the buffer size. 937 * But don't get more than the buffer size.
916 * And don't try to go past the end of the file. 938 * And don't try to go past the end of the file.
@@ -918,14 +940,15 @@ static int do_write(struct fsg_common *common)
918 * don't go past the next page. 940 * don't go past the next page.
919 * If this means getting 0, then we were asked 941 * If this means getting 0, then we were asked
920 * to write past the end of file. 942 * to write past the end of file.
921 * Finally, round down to a block boundary. */ 943 * Finally, round down to a block boundary.
944 */
922 amount = min(amount_left_to_req, FSG_BUFLEN); 945 amount = min(amount_left_to_req, FSG_BUFLEN);
923 amount = min((loff_t) amount, curlun->file_length - 946 amount = min((loff_t)amount,
924 usb_offset); 947 curlun->file_length - usb_offset);
925 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1); 948 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
926 if (partial_page > 0) 949 if (partial_page > 0)
927 amount = min(amount, 950 amount = min(amount,
928 (unsigned int) PAGE_CACHE_SIZE - partial_page); 951 (unsigned int)PAGE_CACHE_SIZE - partial_page);
929 952
930 if (amount == 0) { 953 if (amount == 0) {
931 get_some_more = 0; 954 get_some_more = 0;
@@ -935,11 +958,13 @@ static int do_write(struct fsg_common *common)
935 curlun->info_valid = 1; 958 curlun->info_valid = 1;
936 continue; 959 continue;
937 } 960 }
938 amount -= (amount & 511); 961 amount -= amount & 511;
939 if (amount == 0) { 962 if (amount == 0) {
940 963
941 /* Why were we were asked to transfer a 964 /*
942 * partial block? */ 965 * Why were we were asked to transfer a
966 * partial block?
967 */
943 get_some_more = 0; 968 get_some_more = 0;
944 continue; 969 continue;
945 } 970 }
@@ -951,15 +976,15 @@ static int do_write(struct fsg_common *common)
951 if (amount_left_to_req == 0) 976 if (amount_left_to_req == 0)
952 get_some_more = 0; 977 get_some_more = 0;
953 978
954 /* amount is always divisible by 512, hence by 979 /*
955 * the bulk-out maxpacket size */ 980 * amount is always divisible by 512, hence by
981 * the bulk-out maxpacket size
982 */
956 bh->outreq->length = amount; 983 bh->outreq->length = amount;
957 bh->bulk_out_intended_length = amount; 984 bh->bulk_out_intended_length = amount;
958 bh->outreq->short_not_ok = 1; 985 bh->outreq->short_not_ok = 1;
959 START_TRANSFER_OR(common, bulk_out, bh->outreq, 986 if (!start_out_transfer(common, bh))
960 &bh->outreq_busy, &bh->state) 987 /* Dunno what to do if common->fsg is NULL */
961 /* Don't know what to do if
962 * common->fsg is NULL */
963 return -EIO; 988 return -EIO;
964 common->next_buffhd_to_fill = bh->next; 989 common->next_buffhd_to_fill = bh->next;
965 continue; 990 continue;
@@ -985,30 +1010,29 @@ static int do_write(struct fsg_common *common)
985 amount = bh->outreq->actual; 1010 amount = bh->outreq->actual;
986 if (curlun->file_length - file_offset < amount) { 1011 if (curlun->file_length - file_offset < amount) {
987 LERROR(curlun, 1012 LERROR(curlun,
988 "write %u @ %llu beyond end %llu\n", 1013 "write %u @ %llu beyond end %llu\n",
989 amount, (unsigned long long) file_offset, 1014 amount, (unsigned long long)file_offset,
990 (unsigned long long) curlun->file_length); 1015 (unsigned long long)curlun->file_length);
991 amount = curlun->file_length - file_offset; 1016 amount = curlun->file_length - file_offset;
992 } 1017 }
993 1018
994 /* Perform the write */ 1019 /* Perform the write */
995 file_offset_tmp = file_offset; 1020 file_offset_tmp = file_offset;
996 nwritten = vfs_write(curlun->filp, 1021 nwritten = vfs_write(curlun->filp,
997 (char __user *) bh->buf, 1022 (char __user *)bh->buf,
998 amount, &file_offset_tmp); 1023 amount, &file_offset_tmp);
999 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, 1024 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1000 (unsigned long long) file_offset, 1025 (unsigned long long)file_offset, (int)nwritten);
1001 (int) nwritten);
1002 if (signal_pending(current)) 1026 if (signal_pending(current))
1003 return -EINTR; /* Interrupted! */ 1027 return -EINTR; /* Interrupted! */
1004 1028
1005 if (nwritten < 0) { 1029 if (nwritten < 0) {
1006 LDBG(curlun, "error in file write: %d\n", 1030 LDBG(curlun, "error in file write: %d\n",
1007 (int) nwritten); 1031 (int)nwritten);
1008 nwritten = 0; 1032 nwritten = 0;
1009 } else if (nwritten < amount) { 1033 } else if (nwritten < amount) {
1010 LDBG(curlun, "partial file write: %d/%u\n", 1034 LDBG(curlun, "partial file write: %d/%u\n",
1011 (int) nwritten, amount); 1035 (int)nwritten, amount);
1012 nwritten -= (nwritten & 511); 1036 nwritten -= (nwritten & 511);
1013 /* Round down to a block */ 1037 /* Round down to a block */
1014 } 1038 }
@@ -1081,16 +1105,20 @@ static int do_verify(struct fsg_common *common)
1081 unsigned int amount; 1105 unsigned int amount;
1082 ssize_t nread; 1106 ssize_t nread;
1083 1107
1084 /* Get the starting Logical Block Address and check that it's 1108 /*
1085 * not too big */ 1109 * Get the starting Logical Block Address and check that it's
1110 * not too big.
1111 */
1086 lba = get_unaligned_be32(&common->cmnd[2]); 1112 lba = get_unaligned_be32(&common->cmnd[2]);
1087 if (lba >= curlun->num_sectors) { 1113 if (lba >= curlun->num_sectors) {
1088 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1114 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1089 return -EINVAL; 1115 return -EINVAL;
1090 } 1116 }
1091 1117
1092 /* We allow DPO (Disable Page Out = don't save data in the 1118 /*
1093 * cache) but we don't implement it. */ 1119 * We allow DPO (Disable Page Out = don't save data in the
1120 * cache) but we don't implement it.
1121 */
1094 if (common->cmnd[1] & ~0x10) { 1122 if (common->cmnd[1] & ~0x10) {
1095 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1123 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1096 return -EINVAL; 1124 return -EINVAL;
@@ -1115,16 +1143,17 @@ static int do_verify(struct fsg_common *common)
1115 1143
1116 /* Just try to read the requested blocks */ 1144 /* Just try to read the requested blocks */
1117 while (amount_left > 0) { 1145 while (amount_left > 0) {
1118 1146 /*
1119 /* Figure out how much we need to read: 1147 * Figure out how much we need to read:
1120 * Try to read the remaining amount, but not more than 1148 * Try to read the remaining amount, but not more than
1121 * the buffer size. 1149 * the buffer size.
1122 * And don't try to read past the end of the file. 1150 * And don't try to read past the end of the file.
1123 * If this means reading 0 then we were asked to read 1151 * If this means reading 0 then we were asked to read
1124 * past the end of file. */ 1152 * past the end of file.
1153 */
1125 amount = min(amount_left, FSG_BUFLEN); 1154 amount = min(amount_left, FSG_BUFLEN);
1126 amount = min((loff_t) amount, 1155 amount = min((loff_t)amount,
1127 curlun->file_length - file_offset); 1156 curlun->file_length - file_offset);
1128 if (amount == 0) { 1157 if (amount == 0) {
1129 curlun->sense_data = 1158 curlun->sense_data =
1130 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1159 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
@@ -1145,13 +1174,12 @@ static int do_verify(struct fsg_common *common)
1145 return -EINTR; 1174 return -EINTR;
1146 1175
1147 if (nread < 0) { 1176 if (nread < 0) {
1148 LDBG(curlun, "error in file verify: %d\n", 1177 LDBG(curlun, "error in file verify: %d\n", (int)nread);
1149 (int) nread);
1150 nread = 0; 1178 nread = 0;
1151 } else if (nread < amount) { 1179 } else if (nread < amount) {
1152 LDBG(curlun, "partial file verify: %d/%u\n", 1180 LDBG(curlun, "partial file verify: %d/%u\n",
1153 (int) nread, amount); 1181 (int)nread, amount);
1154 nread -= (nread & 511); /* Round down to a sector */ 1182 nread -= nread & 511; /* Round down to a sector */
1155 } 1183 }
1156 if (nread == 0) { 1184 if (nread == 0) {
1157 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1185 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
@@ -1181,7 +1209,7 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1181 return 36; 1209 return 36;
1182 } 1210 }
1183 1211
1184 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK; 1212 buf[0] = curlun->cdrom ? TYPE_ROM : TYPE_DISK;
1185 buf[1] = curlun->removable ? 0x80 : 0; 1213 buf[1] = curlun->removable ? 0x80 : 0;
1186 buf[2] = 2; /* ANSI SCSI level 2 */ 1214 buf[2] = 2; /* ANSI SCSI level 2 */
1187 buf[3] = 2; /* SCSI-2 INQUIRY data format */ 1215 buf[3] = 2; /* SCSI-2 INQUIRY data format */
@@ -1193,7 +1221,6 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1193 return 36; 1221 return 36;
1194} 1222}
1195 1223
1196
1197static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) 1224static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1198{ 1225{
1199 struct fsg_lun *curlun = common->curlun; 1226 struct fsg_lun *curlun = common->curlun;
@@ -1247,13 +1274,12 @@ static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1247 return 18; 1274 return 18;
1248} 1275}
1249 1276
1250
1251static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) 1277static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1252{ 1278{
1253 struct fsg_lun *curlun = common->curlun; 1279 struct fsg_lun *curlun = common->curlun;
1254 u32 lba = get_unaligned_be32(&common->cmnd[2]); 1280 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1255 int pmi = common->cmnd[8]; 1281 int pmi = common->cmnd[8];
1256 u8 *buf = (u8 *) bh->buf; 1282 u8 *buf = (u8 *)bh->buf;
1257 1283
1258 /* Check the PMI and LBA fields */ 1284 /* Check the PMI and LBA fields */
1259 if (pmi > 1 || (pmi == 0 && lba != 0)) { 1285 if (pmi > 1 || (pmi == 0 && lba != 0)) {
@@ -1267,13 +1293,12 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1267 return 8; 1293 return 8;
1268} 1294}
1269 1295
1270
1271static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) 1296static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1272{ 1297{
1273 struct fsg_lun *curlun = common->curlun; 1298 struct fsg_lun *curlun = common->curlun;
1274 int msf = common->cmnd[1] & 0x02; 1299 int msf = common->cmnd[1] & 0x02;
1275 u32 lba = get_unaligned_be32(&common->cmnd[2]); 1300 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1276 u8 *buf = (u8 *) bh->buf; 1301 u8 *buf = (u8 *)bh->buf;
1277 1302
1278 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ 1303 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
1279 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1304 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
@@ -1290,13 +1315,12 @@ static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1290 return 8; 1315 return 8;
1291} 1316}
1292 1317
1293
1294static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) 1318static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1295{ 1319{
1296 struct fsg_lun *curlun = common->curlun; 1320 struct fsg_lun *curlun = common->curlun;
1297 int msf = common->cmnd[1] & 0x02; 1321 int msf = common->cmnd[1] & 0x02;
1298 int start_track = common->cmnd[6]; 1322 int start_track = common->cmnd[6];
1299 u8 *buf = (u8 *) bh->buf; 1323 u8 *buf = (u8 *)bh->buf;
1300 1324
1301 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ 1325 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1302 start_track > 1) { 1326 start_track > 1) {
@@ -1318,7 +1342,6 @@ static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1318 return 20; 1342 return 20;
1319} 1343}
1320 1344
1321
1322static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) 1345static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1323{ 1346{
1324 struct fsg_lun *curlun = common->curlun; 1347 struct fsg_lun *curlun = common->curlun;
@@ -1343,16 +1366,18 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1343 changeable_values = (pc == 1); 1366 changeable_values = (pc == 1);
1344 all_pages = (page_code == 0x3f); 1367 all_pages = (page_code == 0x3f);
1345 1368
1346 /* Write the mode parameter header. Fixed values are: default 1369 /*
1370 * Write the mode parameter header. Fixed values are: default
1347 * medium type, no cache control (DPOFUA), and no block descriptors. 1371 * medium type, no cache control (DPOFUA), and no block descriptors.
1348 * The only variable value is the WriteProtect bit. We will fill in 1372 * The only variable value is the WriteProtect bit. We will fill in
1349 * the mode data length later. */ 1373 * the mode data length later.
1374 */
1350 memset(buf, 0, 8); 1375 memset(buf, 0, 8);
1351 if (mscmnd == SC_MODE_SENSE_6) { 1376 if (mscmnd == MODE_SENSE) {
1352 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */ 1377 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1353 buf += 4; 1378 buf += 4;
1354 limit = 255; 1379 limit = 255;
1355 } else { /* SC_MODE_SENSE_10 */ 1380 } else { /* MODE_SENSE_10 */
1356 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */ 1381 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1357 buf += 8; 1382 buf += 8;
1358 limit = 65535; /* Should really be FSG_BUFLEN */ 1383 limit = 65535; /* Should really be FSG_BUFLEN */
@@ -1360,8 +1385,10 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1360 1385
1361 /* No block descriptors */ 1386 /* No block descriptors */
1362 1387
1363 /* The mode pages, in numerical order. The only page we support 1388 /*
1364 * is the Caching page. */ 1389 * The mode pages, in numerical order. The only page we support
1390 * is the Caching page.
1391 */
1365 if (page_code == 0x08 || all_pages) { 1392 if (page_code == 0x08 || all_pages) {
1366 valid_page = 1; 1393 valid_page = 1;
1367 buf[0] = 0x08; /* Page code */ 1394 buf[0] = 0x08; /* Page code */
@@ -1383,8 +1410,10 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1383 buf += 12; 1410 buf += 12;
1384 } 1411 }
1385 1412
1386 /* Check that a valid page was requested and the mode data length 1413 /*
1387 * isn't too long. */ 1414 * Check that a valid page was requested and the mode data length
1415 * isn't too long.
1416 */
1388 len = buf - buf0; 1417 len = buf - buf0;
1389 if (!valid_page || len > limit) { 1418 if (!valid_page || len > limit) {
1390 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1419 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
@@ -1392,14 +1421,13 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1392 } 1421 }
1393 1422
1394 /* Store the mode data length */ 1423 /* Store the mode data length */
1395 if (mscmnd == SC_MODE_SENSE_6) 1424 if (mscmnd == MODE_SENSE)
1396 buf0[0] = len - 1; 1425 buf0[0] = len - 1;
1397 else 1426 else
1398 put_unaligned_be16(len - 2, buf0); 1427 put_unaligned_be16(len - 2, buf0);
1399 return len; 1428 return len;
1400} 1429}
1401 1430
1402
1403static int do_start_stop(struct fsg_common *common) 1431static int do_start_stop(struct fsg_common *common)
1404{ 1432{
1405 struct fsg_lun *curlun = common->curlun; 1433 struct fsg_lun *curlun = common->curlun;
@@ -1419,8 +1447,10 @@ static int do_start_stop(struct fsg_common *common)
1419 loej = common->cmnd[4] & 0x02; 1447 loej = common->cmnd[4] & 0x02;
1420 start = common->cmnd[4] & 0x01; 1448 start = common->cmnd[4] & 0x01;
1421 1449
1422 /* Our emulation doesn't support mounting; the medium is 1450 /*
1423 * available for use as soon as it is loaded. */ 1451 * Our emulation doesn't support mounting; the medium is
1452 * available for use as soon as it is loaded.
1453 */
1424 if (start) { 1454 if (start) {
1425 if (!fsg_lun_is_open(curlun)) { 1455 if (!fsg_lun_is_open(curlun)) {
1426 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 1456 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
@@ -1461,7 +1491,6 @@ static int do_start_stop(struct fsg_common *common)
1461 : 0; 1491 : 0;
1462} 1492}
1463 1493
1464
1465static int do_prevent_allow(struct fsg_common *common) 1494static int do_prevent_allow(struct fsg_common *common)
1466{ 1495{
1467 struct fsg_lun *curlun = common->curlun; 1496 struct fsg_lun *curlun = common->curlun;
@@ -1486,7 +1515,6 @@ static int do_prevent_allow(struct fsg_common *common)
1486 return 0; 1515 return 0;
1487} 1516}
1488 1517
1489
1490static int do_read_format_capacities(struct fsg_common *common, 1518static int do_read_format_capacities(struct fsg_common *common,
1491 struct fsg_buffhd *bh) 1519 struct fsg_buffhd *bh)
1492{ 1520{
@@ -1504,7 +1532,6 @@ static int do_read_format_capacities(struct fsg_common *common,
1504 return 12; 1532 return 12;
1505} 1533}
1506 1534
1507
1508static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) 1535static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1509{ 1536{
1510 struct fsg_lun *curlun = common->curlun; 1537 struct fsg_lun *curlun = common->curlun;
@@ -1563,37 +1590,6 @@ static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1563 return rc; 1590 return rc;
1564} 1591}
1565 1592
1566static int pad_with_zeros(struct fsg_dev *fsg)
1567{
1568 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1569 u32 nkeep = bh->inreq->length;
1570 u32 nsend;
1571 int rc;
1572
1573 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
1574 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1575 while (fsg->common->usb_amount_left > 0) {
1576
1577 /* Wait for the next buffer to be free */
1578 while (bh->state != BUF_STATE_EMPTY) {
1579 rc = sleep_thread(fsg->common);
1580 if (rc)
1581 return rc;
1582 }
1583
1584 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
1585 memset(bh->buf + nkeep, 0, nsend - nkeep);
1586 bh->inreq->length = nsend;
1587 bh->inreq->zero = 0;
1588 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1589 &bh->inreq_busy, &bh->state);
1590 bh = fsg->common->next_buffhd_to_fill = bh->next;
1591 fsg->common->usb_amount_left -= nsend;
1592 nkeep = 0;
1593 }
1594 return 0;
1595}
1596
1597static int throw_away_data(struct fsg_common *common) 1593static int throw_away_data(struct fsg_common *common)
1598{ 1594{
1599 struct fsg_buffhd *bh; 1595 struct fsg_buffhd *bh;
@@ -1612,7 +1608,7 @@ static int throw_away_data(struct fsg_common *common)
1612 1608
1613 /* A short packet or an error ends everything */ 1609 /* A short packet or an error ends everything */
1614 if (bh->outreq->actual != bh->outreq->length || 1610 if (bh->outreq->actual != bh->outreq->length ||
1615 bh->outreq->status != 0) { 1611 bh->outreq->status != 0) {
1616 raise_exception(common, 1612 raise_exception(common,
1617 FSG_STATE_ABORT_BULK_OUT); 1613 FSG_STATE_ABORT_BULK_OUT);
1618 return -EINTR; 1614 return -EINTR;
@@ -1626,15 +1622,15 @@ static int throw_away_data(struct fsg_common *common)
1626 && common->usb_amount_left > 0) { 1622 && common->usb_amount_left > 0) {
1627 amount = min(common->usb_amount_left, FSG_BUFLEN); 1623 amount = min(common->usb_amount_left, FSG_BUFLEN);
1628 1624
1629 /* amount is always divisible by 512, hence by 1625 /*
1630 * the bulk-out maxpacket size */ 1626 * amount is always divisible by 512, hence by
1627 * the bulk-out maxpacket size.
1628 */
1631 bh->outreq->length = amount; 1629 bh->outreq->length = amount;
1632 bh->bulk_out_intended_length = amount; 1630 bh->bulk_out_intended_length = amount;
1633 bh->outreq->short_not_ok = 1; 1631 bh->outreq->short_not_ok = 1;
1634 START_TRANSFER_OR(common, bulk_out, bh->outreq, 1632 if (!start_out_transfer(common, bh))
1635 &bh->outreq_busy, &bh->state) 1633 /* Dunno what to do if common->fsg is NULL */
1636 /* Don't know what to do if
1637 * common->fsg is NULL */
1638 return -EIO; 1634 return -EIO;
1639 common->next_buffhd_to_fill = bh->next; 1635 common->next_buffhd_to_fill = bh->next;
1640 common->usb_amount_left -= amount; 1636 common->usb_amount_left -= amount;
@@ -1649,7 +1645,6 @@ static int throw_away_data(struct fsg_common *common)
1649 return 0; 1645 return 0;
1650} 1646}
1651 1647
1652
1653static int finish_reply(struct fsg_common *common) 1648static int finish_reply(struct fsg_common *common)
1654{ 1649{
1655 struct fsg_buffhd *bh = common->next_buffhd_to_fill; 1650 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
@@ -1659,10 +1654,12 @@ static int finish_reply(struct fsg_common *common)
1659 case DATA_DIR_NONE: 1654 case DATA_DIR_NONE:
1660 break; /* Nothing to send */ 1655 break; /* Nothing to send */
1661 1656
1662 /* If we don't know whether the host wants to read or write, 1657 /*
1658 * If we don't know whether the host wants to read or write,
1663 * this must be CB or CBI with an unknown command. We mustn't 1659 * this must be CB or CBI with an unknown command. We mustn't
1664 * try to send or receive any data. So stall both bulk pipes 1660 * try to send or receive any data. So stall both bulk pipes
1665 * if we can and wait for a reset. */ 1661 * if we can and wait for a reset.
1662 */
1666 case DATA_DIR_UNKNOWN: 1663 case DATA_DIR_UNKNOWN:
1667 if (!common->can_stall) { 1664 if (!common->can_stall) {
1668 /* Nothing */ 1665 /* Nothing */
@@ -1680,37 +1677,38 @@ static int finish_reply(struct fsg_common *common)
1680 if (common->data_size == 0) { 1677 if (common->data_size == 0) {
1681 /* Nothing to send */ 1678 /* Nothing to send */
1682 1679
1680 /* Don't know what to do if common->fsg is NULL */
1681 } else if (!fsg_is_set(common)) {
1682 rc = -EIO;
1683
1683 /* If there's no residue, simply send the last buffer */ 1684 /* If there's no residue, simply send the last buffer */
1684 } else if (common->residue == 0) { 1685 } else if (common->residue == 0) {
1685 bh->inreq->zero = 0; 1686 bh->inreq->zero = 0;
1686 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1687 if (!start_in_transfer(common, bh))
1687 &bh->inreq_busy, &bh->state)
1688 return -EIO; 1688 return -EIO;
1689 common->next_buffhd_to_fill = bh->next; 1689 common->next_buffhd_to_fill = bh->next;
1690 1690
1691 /* For Bulk-only, if we're allowed to stall then send the 1691 /*
1692 * short packet and halt the bulk-in endpoint. If we can't 1692 * For Bulk-only, mark the end of the data with a short
1693 * stall, pad out the remaining data with 0's. */ 1693 * packet. If we are allowed to stall, halt the bulk-in
1694 } else if (common->can_stall) { 1694 * endpoint. (Note: This violates the Bulk-Only Transport
1695 * specification, which requires us to pad the data if we
1696 * don't halt the endpoint. Presumably nobody will mind.)
1697 */
1698 } else {
1695 bh->inreq->zero = 1; 1699 bh->inreq->zero = 1;
1696 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1700 if (!start_in_transfer(common, bh))
1697 &bh->inreq_busy, &bh->state)
1698 /* Don't know what to do if
1699 * common->fsg is NULL */
1700 rc = -EIO; 1701 rc = -EIO;
1701 common->next_buffhd_to_fill = bh->next; 1702 common->next_buffhd_to_fill = bh->next;
1702 if (common->fsg) 1703 if (common->can_stall)
1703 rc = halt_bulk_in_endpoint(common->fsg); 1704 rc = halt_bulk_in_endpoint(common->fsg);
1704 } else if (fsg_is_set(common)) {
1705 rc = pad_with_zeros(common->fsg);
1706 } else {
1707 /* Don't know what to do if common->fsg is NULL */
1708 rc = -EIO;
1709 } 1705 }
1710 break; 1706 break;
1711 1707
1712 /* We have processed all we want from the data the host has sent. 1708 /*
1713 * There may still be outstanding bulk-out requests. */ 1709 * We have processed all we want from the data the host has sent.
1710 * There may still be outstanding bulk-out requests.
1711 */
1714 case DATA_DIR_FROM_HOST: 1712 case DATA_DIR_FROM_HOST:
1715 if (common->residue == 0) { 1713 if (common->residue == 0) {
1716 /* Nothing to receive */ 1714 /* Nothing to receive */
@@ -1720,12 +1718,14 @@ static int finish_reply(struct fsg_common *common)
1720 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); 1718 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1721 rc = -EINTR; 1719 rc = -EINTR;
1722 1720
1723 /* We haven't processed all the incoming data. Even though 1721 /*
1722 * We haven't processed all the incoming data. Even though
1724 * we may be allowed to stall, doing so would cause a race. 1723 * we may be allowed to stall, doing so would cause a race.
1725 * The controller may already have ACK'ed all the remaining 1724 * The controller may already have ACK'ed all the remaining
1726 * bulk-out packets, in which case the host wouldn't see a 1725 * bulk-out packets, in which case the host wouldn't see a
1727 * STALL. Not realizing the endpoint was halted, it wouldn't 1726 * STALL. Not realizing the endpoint was halted, it wouldn't
1728 * clear the halt -- leading to problems later on. */ 1727 * clear the halt -- leading to problems later on.
1728 */
1729#if 0 1729#if 0
1730 } else if (common->can_stall) { 1730 } else if (common->can_stall) {
1731 if (fsg_is_set(common)) 1731 if (fsg_is_set(common))
@@ -1735,8 +1735,10 @@ static int finish_reply(struct fsg_common *common)
1735 rc = -EINTR; 1735 rc = -EINTR;
1736#endif 1736#endif
1737 1737
1738 /* We can't stall. Read in the excess data and throw it 1738 /*
1739 * all away. */ 1739 * We can't stall. Read in the excess data and throw it
1740 * all away.
1741 */
1740 } else { 1742 } else {
1741 rc = throw_away_data(common); 1743 rc = throw_away_data(common);
1742 } 1744 }
@@ -1745,7 +1747,6 @@ static int finish_reply(struct fsg_common *common)
1745 return rc; 1747 return rc;
1746} 1748}
1747 1749
1748
1749static int send_status(struct fsg_common *common) 1750static int send_status(struct fsg_common *common)
1750{ 1751{
1751 struct fsg_lun *curlun = common->curlun; 1752 struct fsg_lun *curlun = common->curlun;
@@ -1793,8 +1794,7 @@ static int send_status(struct fsg_common *common)
1793 1794
1794 bh->inreq->length = USB_BULK_CS_WRAP_LEN; 1795 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1795 bh->inreq->zero = 0; 1796 bh->inreq->zero = 0;
1796 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1797 if (!start_in_transfer(common, bh))
1797 &bh->inreq_busy, &bh->state)
1798 /* Don't know what to do if common->fsg is NULL */ 1798 /* Don't know what to do if common->fsg is NULL */
1799 return -EIO; 1799 return -EIO;
1800 1800
@@ -1805,11 +1805,13 @@ static int send_status(struct fsg_common *common)
1805 1805
1806/*-------------------------------------------------------------------------*/ 1806/*-------------------------------------------------------------------------*/
1807 1807
1808/* Check whether the command is properly formed and whether its data size 1808/*
1809 * and direction agree with the values we already have. */ 1809 * Check whether the command is properly formed and whether its data size
1810 * and direction agree with the values we already have.
1811 */
1810static int check_command(struct fsg_common *common, int cmnd_size, 1812static int check_command(struct fsg_common *common, int cmnd_size,
1811 enum data_direction data_dir, unsigned int mask, 1813 enum data_direction data_dir, unsigned int mask,
1812 int needs_medium, const char *name) 1814 int needs_medium, const char *name)
1813{ 1815{
1814 int i; 1816 int i;
1815 int lun = common->cmnd[1] >> 5; 1817 int lun = common->cmnd[1] >> 5;
@@ -1820,19 +1822,23 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1820 hdlen[0] = 0; 1822 hdlen[0] = 0;
1821 if (common->data_dir != DATA_DIR_UNKNOWN) 1823 if (common->data_dir != DATA_DIR_UNKNOWN)
1822 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], 1824 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1823 common->data_size); 1825 common->data_size);
1824 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", 1826 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1825 name, cmnd_size, dirletter[(int) data_dir], 1827 name, cmnd_size, dirletter[(int) data_dir],
1826 common->data_size_from_cmnd, common->cmnd_size, hdlen); 1828 common->data_size_from_cmnd, common->cmnd_size, hdlen);
1827 1829
1828 /* We can't reply at all until we know the correct data direction 1830 /*
1829 * and size. */ 1831 * We can't reply at all until we know the correct data direction
1832 * and size.
1833 */
1830 if (common->data_size_from_cmnd == 0) 1834 if (common->data_size_from_cmnd == 0)
1831 data_dir = DATA_DIR_NONE; 1835 data_dir = DATA_DIR_NONE;
1832 if (common->data_size < common->data_size_from_cmnd) { 1836 if (common->data_size < common->data_size_from_cmnd) {
1833 /* Host data size < Device data size is a phase error. 1837 /*
1838 * Host data size < Device data size is a phase error.
1834 * Carry out the command, but only transfer as much as 1839 * Carry out the command, but only transfer as much as
1835 * we are allowed. */ 1840 * we are allowed.
1841 */
1836 common->data_size_from_cmnd = common->data_size; 1842 common->data_size_from_cmnd = common->data_size;
1837 common->phase_error = 1; 1843 common->phase_error = 1;
1838 } 1844 }
@@ -1840,8 +1846,7 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1840 common->usb_amount_left = common->data_size; 1846 common->usb_amount_left = common->data_size;
1841 1847
1842 /* Conflicting data directions is a phase error */ 1848 /* Conflicting data directions is a phase error */
1843 if (common->data_dir != data_dir 1849 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {
1844 && common->data_size_from_cmnd > 0) {
1845 common->phase_error = 1; 1850 common->phase_error = 1;
1846 return -EINVAL; 1851 return -EINVAL;
1847 } 1852 }
@@ -1849,7 +1854,8 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1849 /* Verify the length of the command itself */ 1854 /* Verify the length of the command itself */
1850 if (cmnd_size != common->cmnd_size) { 1855 if (cmnd_size != common->cmnd_size) {
1851 1856
1852 /* Special case workaround: There are plenty of buggy SCSI 1857 /*
1858 * Special case workaround: There are plenty of buggy SCSI
1853 * implementations. Many have issues with cbw->Length 1859 * implementations. Many have issues with cbw->Length
1854 * field passing a wrong command size. For those cases we 1860 * field passing a wrong command size. For those cases we
1855 * always try to work around the problem by using the length 1861 * always try to work around the problem by using the length
@@ -1878,10 +1884,10 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1878 common->lun, lun); 1884 common->lun, lun);
1879 1885
1880 /* Check the LUN */ 1886 /* Check the LUN */
1881 if (common->lun >= 0 && common->lun < common->nluns) { 1887 if (common->lun < common->nluns) {
1882 curlun = &common->luns[common->lun]; 1888 curlun = &common->luns[common->lun];
1883 common->curlun = curlun; 1889 common->curlun = curlun;
1884 if (common->cmnd[0] != SC_REQUEST_SENSE) { 1890 if (common->cmnd[0] != REQUEST_SENSE) {
1885 curlun->sense_data = SS_NO_SENSE; 1891 curlun->sense_data = SS_NO_SENSE;
1886 curlun->sense_data_info = 0; 1892 curlun->sense_data_info = 0;
1887 curlun->info_valid = 0; 1893 curlun->info_valid = 0;
@@ -1891,20 +1897,24 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1891 curlun = NULL; 1897 curlun = NULL;
1892 common->bad_lun_okay = 0; 1898 common->bad_lun_okay = 0;
1893 1899
1894 /* INQUIRY and REQUEST SENSE commands are explicitly allowed 1900 /*
1895 * to use unsupported LUNs; all others may not. */ 1901 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1896 if (common->cmnd[0] != SC_INQUIRY && 1902 * to use unsupported LUNs; all others may not.
1897 common->cmnd[0] != SC_REQUEST_SENSE) { 1903 */
1904 if (common->cmnd[0] != INQUIRY &&
1905 common->cmnd[0] != REQUEST_SENSE) {
1898 DBG(common, "unsupported LUN %d\n", common->lun); 1906 DBG(common, "unsupported LUN %d\n", common->lun);
1899 return -EINVAL; 1907 return -EINVAL;
1900 } 1908 }
1901 } 1909 }
1902 1910
1903 /* If a unit attention condition exists, only INQUIRY and 1911 /*
1904 * REQUEST SENSE commands are allowed; anything else must fail. */ 1912 * If a unit attention condition exists, only INQUIRY and
1913 * REQUEST SENSE commands are allowed; anything else must fail.
1914 */
1905 if (curlun && curlun->unit_attention_data != SS_NO_SENSE && 1915 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1906 common->cmnd[0] != SC_INQUIRY && 1916 common->cmnd[0] != INQUIRY &&
1907 common->cmnd[0] != SC_REQUEST_SENSE) { 1917 common->cmnd[0] != REQUEST_SENSE) {
1908 curlun->sense_data = curlun->unit_attention_data; 1918 curlun->sense_data = curlun->unit_attention_data;
1909 curlun->unit_attention_data = SS_NO_SENSE; 1919 curlun->unit_attention_data = SS_NO_SENSE;
1910 return -EINVAL; 1920 return -EINVAL;
@@ -1930,7 +1940,6 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1930 return 0; 1940 return 0;
1931} 1941}
1932 1942
1933
1934static int do_scsi_command(struct fsg_common *common) 1943static int do_scsi_command(struct fsg_common *common)
1935{ 1944{
1936 struct fsg_buffhd *bh; 1945 struct fsg_buffhd *bh;
@@ -1955,7 +1964,7 @@ static int do_scsi_command(struct fsg_common *common)
1955 down_read(&common->filesem); /* We're using the backing file */ 1964 down_read(&common->filesem); /* We're using the backing file */
1956 switch (common->cmnd[0]) { 1965 switch (common->cmnd[0]) {
1957 1966
1958 case SC_INQUIRY: 1967 case INQUIRY:
1959 common->data_size_from_cmnd = common->cmnd[4]; 1968 common->data_size_from_cmnd = common->cmnd[4];
1960 reply = check_command(common, 6, DATA_DIR_TO_HOST, 1969 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1961 (1<<4), 0, 1970 (1<<4), 0,
@@ -1964,7 +1973,7 @@ static int do_scsi_command(struct fsg_common *common)
1964 reply = do_inquiry(common, bh); 1973 reply = do_inquiry(common, bh);
1965 break; 1974 break;
1966 1975
1967 case SC_MODE_SELECT_6: 1976 case MODE_SELECT:
1968 common->data_size_from_cmnd = common->cmnd[4]; 1977 common->data_size_from_cmnd = common->cmnd[4];
1969 reply = check_command(common, 6, DATA_DIR_FROM_HOST, 1978 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1970 (1<<1) | (1<<4), 0, 1979 (1<<1) | (1<<4), 0,
@@ -1973,7 +1982,7 @@ static int do_scsi_command(struct fsg_common *common)
1973 reply = do_mode_select(common, bh); 1982 reply = do_mode_select(common, bh);
1974 break; 1983 break;
1975 1984
1976 case SC_MODE_SELECT_10: 1985 case MODE_SELECT_10:
1977 common->data_size_from_cmnd = 1986 common->data_size_from_cmnd =
1978 get_unaligned_be16(&common->cmnd[7]); 1987 get_unaligned_be16(&common->cmnd[7]);
1979 reply = check_command(common, 10, DATA_DIR_FROM_HOST, 1988 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
@@ -1983,7 +1992,7 @@ static int do_scsi_command(struct fsg_common *common)
1983 reply = do_mode_select(common, bh); 1992 reply = do_mode_select(common, bh);
1984 break; 1993 break;
1985 1994
1986 case SC_MODE_SENSE_6: 1995 case MODE_SENSE:
1987 common->data_size_from_cmnd = common->cmnd[4]; 1996 common->data_size_from_cmnd = common->cmnd[4];
1988 reply = check_command(common, 6, DATA_DIR_TO_HOST, 1997 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1989 (1<<1) | (1<<2) | (1<<4), 0, 1998 (1<<1) | (1<<2) | (1<<4), 0,
@@ -1992,7 +2001,7 @@ static int do_scsi_command(struct fsg_common *common)
1992 reply = do_mode_sense(common, bh); 2001 reply = do_mode_sense(common, bh);
1993 break; 2002 break;
1994 2003
1995 case SC_MODE_SENSE_10: 2004 case MODE_SENSE_10:
1996 common->data_size_from_cmnd = 2005 common->data_size_from_cmnd =
1997 get_unaligned_be16(&common->cmnd[7]); 2006 get_unaligned_be16(&common->cmnd[7]);
1998 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2007 reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -2002,7 +2011,7 @@ static int do_scsi_command(struct fsg_common *common)
2002 reply = do_mode_sense(common, bh); 2011 reply = do_mode_sense(common, bh);
2003 break; 2012 break;
2004 2013
2005 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL: 2014 case ALLOW_MEDIUM_REMOVAL:
2006 common->data_size_from_cmnd = 0; 2015 common->data_size_from_cmnd = 0;
2007 reply = check_command(common, 6, DATA_DIR_NONE, 2016 reply = check_command(common, 6, DATA_DIR_NONE,
2008 (1<<4), 0, 2017 (1<<4), 0,
@@ -2011,7 +2020,7 @@ static int do_scsi_command(struct fsg_common *common)
2011 reply = do_prevent_allow(common); 2020 reply = do_prevent_allow(common);
2012 break; 2021 break;
2013 2022
2014 case SC_READ_6: 2023 case READ_6:
2015 i = common->cmnd[4]; 2024 i = common->cmnd[4];
2016 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2025 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2017 reply = check_command(common, 6, DATA_DIR_TO_HOST, 2026 reply = check_command(common, 6, DATA_DIR_TO_HOST,
@@ -2021,7 +2030,7 @@ static int do_scsi_command(struct fsg_common *common)
2021 reply = do_read(common); 2030 reply = do_read(common);
2022 break; 2031 break;
2023 2032
2024 case SC_READ_10: 2033 case READ_10:
2025 common->data_size_from_cmnd = 2034 common->data_size_from_cmnd =
2026 get_unaligned_be16(&common->cmnd[7]) << 9; 2035 get_unaligned_be16(&common->cmnd[7]) << 9;
2027 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2036 reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -2031,7 +2040,7 @@ static int do_scsi_command(struct fsg_common *common)
2031 reply = do_read(common); 2040 reply = do_read(common);
2032 break; 2041 break;
2033 2042
2034 case SC_READ_12: 2043 case READ_12:
2035 common->data_size_from_cmnd = 2044 common->data_size_from_cmnd =
2036 get_unaligned_be32(&common->cmnd[6]) << 9; 2045 get_unaligned_be32(&common->cmnd[6]) << 9;
2037 reply = check_command(common, 12, DATA_DIR_TO_HOST, 2046 reply = check_command(common, 12, DATA_DIR_TO_HOST,
@@ -2041,7 +2050,7 @@ static int do_scsi_command(struct fsg_common *common)
2041 reply = do_read(common); 2050 reply = do_read(common);
2042 break; 2051 break;
2043 2052
2044 case SC_READ_CAPACITY: 2053 case READ_CAPACITY:
2045 common->data_size_from_cmnd = 8; 2054 common->data_size_from_cmnd = 8;
2046 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2055 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2047 (0xf<<2) | (1<<8), 1, 2056 (0xf<<2) | (1<<8), 1,
@@ -2050,7 +2059,7 @@ static int do_scsi_command(struct fsg_common *common)
2050 reply = do_read_capacity(common, bh); 2059 reply = do_read_capacity(common, bh);
2051 break; 2060 break;
2052 2061
2053 case SC_READ_HEADER: 2062 case READ_HEADER:
2054 if (!common->curlun || !common->curlun->cdrom) 2063 if (!common->curlun || !common->curlun->cdrom)
2055 goto unknown_cmnd; 2064 goto unknown_cmnd;
2056 common->data_size_from_cmnd = 2065 common->data_size_from_cmnd =
@@ -2062,7 +2071,7 @@ static int do_scsi_command(struct fsg_common *common)
2062 reply = do_read_header(common, bh); 2071 reply = do_read_header(common, bh);
2063 break; 2072 break;
2064 2073
2065 case SC_READ_TOC: 2074 case READ_TOC:
2066 if (!common->curlun || !common->curlun->cdrom) 2075 if (!common->curlun || !common->curlun->cdrom)
2067 goto unknown_cmnd; 2076 goto unknown_cmnd;
2068 common->data_size_from_cmnd = 2077 common->data_size_from_cmnd =
@@ -2074,7 +2083,7 @@ static int do_scsi_command(struct fsg_common *common)
2074 reply = do_read_toc(common, bh); 2083 reply = do_read_toc(common, bh);
2075 break; 2084 break;
2076 2085
2077 case SC_READ_FORMAT_CAPACITIES: 2086 case READ_FORMAT_CAPACITIES:
2078 common->data_size_from_cmnd = 2087 common->data_size_from_cmnd =
2079 get_unaligned_be16(&common->cmnd[7]); 2088 get_unaligned_be16(&common->cmnd[7]);
2080 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2089 reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -2084,7 +2093,7 @@ static int do_scsi_command(struct fsg_common *common)
2084 reply = do_read_format_capacities(common, bh); 2093 reply = do_read_format_capacities(common, bh);
2085 break; 2094 break;
2086 2095
2087 case SC_REQUEST_SENSE: 2096 case REQUEST_SENSE:
2088 common->data_size_from_cmnd = common->cmnd[4]; 2097 common->data_size_from_cmnd = common->cmnd[4];
2089 reply = check_command(common, 6, DATA_DIR_TO_HOST, 2098 reply = check_command(common, 6, DATA_DIR_TO_HOST,
2090 (1<<4), 0, 2099 (1<<4), 0,
@@ -2093,7 +2102,7 @@ static int do_scsi_command(struct fsg_common *common)
2093 reply = do_request_sense(common, bh); 2102 reply = do_request_sense(common, bh);
2094 break; 2103 break;
2095 2104
2096 case SC_START_STOP_UNIT: 2105 case START_STOP:
2097 common->data_size_from_cmnd = 0; 2106 common->data_size_from_cmnd = 0;
2098 reply = check_command(common, 6, DATA_DIR_NONE, 2107 reply = check_command(common, 6, DATA_DIR_NONE,
2099 (1<<1) | (1<<4), 0, 2108 (1<<1) | (1<<4), 0,
@@ -2102,7 +2111,7 @@ static int do_scsi_command(struct fsg_common *common)
2102 reply = do_start_stop(common); 2111 reply = do_start_stop(common);
2103 break; 2112 break;
2104 2113
2105 case SC_SYNCHRONIZE_CACHE: 2114 case SYNCHRONIZE_CACHE:
2106 common->data_size_from_cmnd = 0; 2115 common->data_size_from_cmnd = 0;
2107 reply = check_command(common, 10, DATA_DIR_NONE, 2116 reply = check_command(common, 10, DATA_DIR_NONE,
2108 (0xf<<2) | (3<<7), 1, 2117 (0xf<<2) | (3<<7), 1,
@@ -2111,16 +2120,18 @@ static int do_scsi_command(struct fsg_common *common)
2111 reply = do_synchronize_cache(common); 2120 reply = do_synchronize_cache(common);
2112 break; 2121 break;
2113 2122
2114 case SC_TEST_UNIT_READY: 2123 case TEST_UNIT_READY:
2115 common->data_size_from_cmnd = 0; 2124 common->data_size_from_cmnd = 0;
2116 reply = check_command(common, 6, DATA_DIR_NONE, 2125 reply = check_command(common, 6, DATA_DIR_NONE,
2117 0, 1, 2126 0, 1,
2118 "TEST UNIT READY"); 2127 "TEST UNIT READY");
2119 break; 2128 break;
2120 2129
2121 /* Although optional, this command is used by MS-Windows. We 2130 /*
2122 * support a minimal version: BytChk must be 0. */ 2131 * Although optional, this command is used by MS-Windows. We
2123 case SC_VERIFY: 2132 * support a minimal version: BytChk must be 0.
2133 */
2134 case VERIFY:
2124 common->data_size_from_cmnd = 0; 2135 common->data_size_from_cmnd = 0;
2125 reply = check_command(common, 10, DATA_DIR_NONE, 2136 reply = check_command(common, 10, DATA_DIR_NONE,
2126 (1<<1) | (0xf<<2) | (3<<7), 1, 2137 (1<<1) | (0xf<<2) | (3<<7), 1,
@@ -2129,7 +2140,7 @@ static int do_scsi_command(struct fsg_common *common)
2129 reply = do_verify(common); 2140 reply = do_verify(common);
2130 break; 2141 break;
2131 2142
2132 case SC_WRITE_6: 2143 case WRITE_6:
2133 i = common->cmnd[4]; 2144 i = common->cmnd[4];
2134 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2145 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2135 reply = check_command(common, 6, DATA_DIR_FROM_HOST, 2146 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
@@ -2139,7 +2150,7 @@ static int do_scsi_command(struct fsg_common *common)
2139 reply = do_write(common); 2150 reply = do_write(common);
2140 break; 2151 break;
2141 2152
2142 case SC_WRITE_10: 2153 case WRITE_10:
2143 common->data_size_from_cmnd = 2154 common->data_size_from_cmnd =
2144 get_unaligned_be16(&common->cmnd[7]) << 9; 2155 get_unaligned_be16(&common->cmnd[7]) << 9;
2145 reply = check_command(common, 10, DATA_DIR_FROM_HOST, 2156 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
@@ -2149,7 +2160,7 @@ static int do_scsi_command(struct fsg_common *common)
2149 reply = do_write(common); 2160 reply = do_write(common);
2150 break; 2161 break;
2151 2162
2152 case SC_WRITE_12: 2163 case WRITE_12:
2153 common->data_size_from_cmnd = 2164 common->data_size_from_cmnd =
2154 get_unaligned_be32(&common->cmnd[6]) << 9; 2165 get_unaligned_be32(&common->cmnd[6]) << 9;
2155 reply = check_command(common, 12, DATA_DIR_FROM_HOST, 2166 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
@@ -2159,14 +2170,16 @@ static int do_scsi_command(struct fsg_common *common)
2159 reply = do_write(common); 2170 reply = do_write(common);
2160 break; 2171 break;
2161 2172
2162 /* Some mandatory commands that we recognize but don't implement. 2173 /*
2174 * Some mandatory commands that we recognize but don't implement.
2163 * They don't mean much in this setting. It's left as an exercise 2175 * They don't mean much in this setting. It's left as an exercise
2164 * for anyone interested to implement RESERVE and RELEASE in terms 2176 * for anyone interested to implement RESERVE and RELEASE in terms
2165 * of Posix locks. */ 2177 * of Posix locks.
2166 case SC_FORMAT_UNIT: 2178 */
2167 case SC_RELEASE: 2179 case FORMAT_UNIT:
2168 case SC_RESERVE: 2180 case RELEASE:
2169 case SC_SEND_DIAGNOSTIC: 2181 case RESERVE:
2182 case SEND_DIAGNOSTIC:
2170 /* Fall through */ 2183 /* Fall through */
2171 2184
2172 default: 2185 default:
@@ -2190,7 +2203,7 @@ unknown_cmnd:
2190 if (reply == -EINVAL) 2203 if (reply == -EINVAL)
2191 reply = 0; /* Error reply length */ 2204 reply = 0; /* Error reply length */
2192 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { 2205 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2193 reply = min((u32) reply, common->data_size_from_cmnd); 2206 reply = min((u32)reply, common->data_size_from_cmnd);
2194 bh->inreq->length = reply; 2207 bh->inreq->length = reply;
2195 bh->state = BUF_STATE_FULL; 2208 bh->state = BUF_STATE_FULL;
2196 common->residue -= reply; 2209 common->residue -= reply;
@@ -2220,7 +2233,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2220 req->actual, 2233 req->actual,
2221 le32_to_cpu(cbw->Signature)); 2234 le32_to_cpu(cbw->Signature));
2222 2235
2223 /* The Bulk-only spec says we MUST stall the IN endpoint 2236 /*
2237 * The Bulk-only spec says we MUST stall the IN endpoint
2224 * (6.6.1), so it's unavoidable. It also says we must 2238 * (6.6.1), so it's unavoidable. It also says we must
2225 * retain this state until the next reset, but there's 2239 * retain this state until the next reset, but there's
2226 * no way to tell the controller driver it should ignore 2240 * no way to tell the controller driver it should ignore
@@ -2228,7 +2242,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2228 * 2242 *
2229 * We aren't required to halt the OUT endpoint; instead 2243 * We aren't required to halt the OUT endpoint; instead
2230 * we can simply accept and discard any data received 2244 * we can simply accept and discard any data received
2231 * until the next reset. */ 2245 * until the next reset.
2246 */
2232 wedge_bulk_in_endpoint(fsg); 2247 wedge_bulk_in_endpoint(fsg);
2233 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); 2248 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2234 return -EINVAL; 2249 return -EINVAL;
@@ -2241,8 +2256,10 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2241 "cmdlen %u\n", 2256 "cmdlen %u\n",
2242 cbw->Lun, cbw->Flags, cbw->Length); 2257 cbw->Lun, cbw->Flags, cbw->Length);
2243 2258
2244 /* We can do anything we want here, so let's stall the 2259 /*
2245 * bulk pipes if we are allowed to. */ 2260 * We can do anything we want here, so let's stall the
2261 * bulk pipes if we are allowed to.
2262 */
2246 if (common->can_stall) { 2263 if (common->can_stall) {
2247 fsg_set_halt(fsg, fsg->bulk_out); 2264 fsg_set_halt(fsg, fsg->bulk_out);
2248 halt_bulk_in_endpoint(fsg); 2265 halt_bulk_in_endpoint(fsg);
@@ -2265,7 +2282,6 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2265 return 0; 2282 return 0;
2266} 2283}
2267 2284
2268
2269static int get_next_command(struct fsg_common *common) 2285static int get_next_command(struct fsg_common *common)
2270{ 2286{
2271 struct fsg_buffhd *bh; 2287 struct fsg_buffhd *bh;
@@ -2282,14 +2298,15 @@ static int get_next_command(struct fsg_common *common)
2282 /* Queue a request to read a Bulk-only CBW */ 2298 /* Queue a request to read a Bulk-only CBW */
2283 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); 2299 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2284 bh->outreq->short_not_ok = 1; 2300 bh->outreq->short_not_ok = 1;
2285 START_TRANSFER_OR(common, bulk_out, bh->outreq, 2301 if (!start_out_transfer(common, bh))
2286 &bh->outreq_busy, &bh->state)
2287 /* Don't know what to do if common->fsg is NULL */ 2302 /* Don't know what to do if common->fsg is NULL */
2288 return -EIO; 2303 return -EIO;
2289 2304
2290 /* We will drain the buffer in software, which means we 2305 /*
2306 * We will drain the buffer in software, which means we
2291 * can reuse it for the next filling. No need to advance 2307 * can reuse it for the next filling. No need to advance
2292 * next_buffhd_to_fill. */ 2308 * next_buffhd_to_fill.
2309 */
2293 2310
2294 /* Wait for the CBW to arrive */ 2311 /* Wait for the CBW to arrive */
2295 while (bh->state != BUF_STATE_FULL) { 2312 while (bh->state != BUF_STATE_FULL) {
@@ -2420,13 +2437,12 @@ reset:
2420 2437
2421/****************************** ALT CONFIGS ******************************/ 2438/****************************** ALT CONFIGS ******************************/
2422 2439
2423
2424static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 2440static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2425{ 2441{
2426 struct fsg_dev *fsg = fsg_from_func(f); 2442 struct fsg_dev *fsg = fsg_from_func(f);
2427 fsg->common->new_fsg = fsg; 2443 fsg->common->new_fsg = fsg;
2428 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); 2444 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2429 return 0; 2445 return USB_GADGET_DELAYED_STATUS;
2430} 2446}
2431 2447
2432static void fsg_disable(struct usb_function *f) 2448static void fsg_disable(struct usb_function *f)
@@ -2448,8 +2464,10 @@ static void handle_exception(struct fsg_common *common)
2448 struct fsg_lun *curlun; 2464 struct fsg_lun *curlun;
2449 unsigned int exception_req_tag; 2465 unsigned int exception_req_tag;
2450 2466
2451 /* Clear the existing signals. Anything but SIGUSR1 is converted 2467 /*
2452 * into a high-priority EXIT exception. */ 2468 * Clear the existing signals. Anything but SIGUSR1 is converted
2469 * into a high-priority EXIT exception.
2470 */
2453 for (;;) { 2471 for (;;) {
2454 int sig = 2472 int sig =
2455 dequeue_signal_lock(current, &current->blocked, &info); 2473 dequeue_signal_lock(current, &current->blocked, &info);
@@ -2493,8 +2511,10 @@ static void handle_exception(struct fsg_common *common)
2493 usb_ep_fifo_flush(common->fsg->bulk_out); 2511 usb_ep_fifo_flush(common->fsg->bulk_out);
2494 } 2512 }
2495 2513
2496 /* Reset the I/O buffer states and pointers, the SCSI 2514 /*
2497 * state, and the exception. Then invoke the handler. */ 2515 * Reset the I/O buffer states and pointers, the SCSI
2516 * state, and the exception. Then invoke the handler.
2517 */
2498 spin_lock_irq(&common->lock); 2518 spin_lock_irq(&common->lock);
2499 2519
2500 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2520 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
@@ -2532,9 +2552,11 @@ static void handle_exception(struct fsg_common *common)
2532 break; 2552 break;
2533 2553
2534 case FSG_STATE_RESET: 2554 case FSG_STATE_RESET:
2535 /* In case we were forced against our will to halt a 2555 /*
2556 * In case we were forced against our will to halt a
2536 * bulk endpoint, clear the halt now. (The SuperH UDC 2557 * bulk endpoint, clear the halt now. (The SuperH UDC
2537 * requires this.) */ 2558 * requires this.)
2559 */
2538 if (!fsg_is_set(common)) 2560 if (!fsg_is_set(common))
2539 break; 2561 break;
2540 if (test_and_clear_bit(IGNORE_BULK_OUT, 2562 if (test_and_clear_bit(IGNORE_BULK_OUT,
@@ -2544,9 +2566,11 @@ static void handle_exception(struct fsg_common *common)
2544 if (common->ep0_req_tag == exception_req_tag) 2566 if (common->ep0_req_tag == exception_req_tag)
2545 ep0_queue(common); /* Complete the status stage */ 2567 ep0_queue(common); /* Complete the status stage */
2546 2568
2547 /* Technically this should go here, but it would only be 2569 /*
2570 * Technically this should go here, but it would only be
2548 * a waste of time. Ditto for the INTERFACE_CHANGE and 2571 * a waste of time. Ditto for the INTERFACE_CHANGE and
2549 * CONFIG_CHANGE cases. */ 2572 * CONFIG_CHANGE cases.
2573 */
2550 /* for (i = 0; i < common->nluns; ++i) */ 2574 /* for (i = 0; i < common->nluns; ++i) */
2551 /* common->luns[i].unit_attention_data = */ 2575 /* common->luns[i].unit_attention_data = */
2552 /* SS_RESET_OCCURRED; */ 2576 /* SS_RESET_OCCURRED; */
@@ -2554,6 +2578,8 @@ static void handle_exception(struct fsg_common *common)
2554 2578
2555 case FSG_STATE_CONFIG_CHANGE: 2579 case FSG_STATE_CONFIG_CHANGE:
2556 do_set_interface(common, common->new_fsg); 2580 do_set_interface(common, common->new_fsg);
2581 if (common->new_fsg)
2582 usb_composite_setup_continue(common->cdev);
2557 break; 2583 break;
2558 2584
2559 case FSG_STATE_EXIT: 2585 case FSG_STATE_EXIT:
@@ -2581,8 +2607,10 @@ static int fsg_main_thread(void *common_)
2581{ 2607{
2582 struct fsg_common *common = common_; 2608 struct fsg_common *common = common_;
2583 2609
2584 /* Allow the thread to be killed by a signal, but set the signal mask 2610 /*
2585 * to block everything but INT, TERM, KILL, and USR1. */ 2611 * Allow the thread to be killed by a signal, but set the signal mask
2612 * to block everything but INT, TERM, KILL, and USR1.
2613 */
2586 allow_signal(SIGINT); 2614 allow_signal(SIGINT);
2587 allow_signal(SIGTERM); 2615 allow_signal(SIGTERM);
2588 allow_signal(SIGKILL); 2616 allow_signal(SIGKILL);
@@ -2591,9 +2619,11 @@ static int fsg_main_thread(void *common_)
2591 /* Allow the thread to be frozen */ 2619 /* Allow the thread to be frozen */
2592 set_freezable(); 2620 set_freezable();
2593 2621
2594 /* Arrange for userspace references to be interpreted as kernel 2622 /*
2623 * Arrange for userspace references to be interpreted as kernel
2595 * pointers. That way we can pass a kernel pointer to a routine 2624 * pointers. That way we can pass a kernel pointer to a routine
2596 * that expects a __user pointer and it will work okay. */ 2625 * that expects a __user pointer and it will work okay.
2626 */
2597 set_fs(get_ds()); 2627 set_fs(get_ds());
2598 2628
2599 /* The main loop */ 2629 /* The main loop */
@@ -2653,7 +2683,7 @@ static int fsg_main_thread(void *common_)
2653 up_write(&common->filesem); 2683 up_write(&common->filesem);
2654 } 2684 }
2655 2685
2656 /* Let the unbind and cleanup routines know the thread has exited */ 2686 /* Let fsg_unbind() know the thread has exited */
2657 complete_and_exit(&common->thread_notifier, 0); 2687 complete_and_exit(&common->thread_notifier, 0);
2658} 2688}
2659 2689
@@ -2662,6 +2692,7 @@ static int fsg_main_thread(void *common_)
2662 2692
2663/* Write permission is checked per LUN in store_*() functions. */ 2693/* Write permission is checked per LUN in store_*() functions. */
2664static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro); 2694static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2695static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, fsg_store_nofua);
2665static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file); 2696static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
2666 2697
2667 2698
@@ -2684,7 +2715,6 @@ static inline void fsg_common_put(struct fsg_common *common)
2684 kref_put(&common->ref, fsg_common_release); 2715 kref_put(&common->ref, fsg_common_release);
2685} 2716}
2686 2717
2687
2688static struct fsg_common *fsg_common_init(struct fsg_common *common, 2718static struct fsg_common *fsg_common_init(struct fsg_common *common,
2689 struct usb_composite_dev *cdev, 2719 struct usb_composite_dev *cdev,
2690 struct fsg_config *cfg) 2720 struct fsg_config *cfg)
@@ -2710,7 +2740,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2710 return ERR_PTR(-ENOMEM); 2740 return ERR_PTR(-ENOMEM);
2711 common->free_storage_on_release = 1; 2741 common->free_storage_on_release = 1;
2712 } else { 2742 } else {
2713 memset(common, 0, sizeof common); 2743 memset(common, 0, sizeof *common);
2714 common->free_storage_on_release = 0; 2744 common->free_storage_on_release = 0;
2715 } 2745 }
2716 2746
@@ -2720,6 +2750,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2720 common->gadget = gadget; 2750 common->gadget = gadget;
2721 common->ep0 = gadget->ep0; 2751 common->ep0 = gadget->ep0;
2722 common->ep0req = cdev->req; 2752 common->ep0req = cdev->req;
2753 common->cdev = cdev;
2723 2754
2724 /* Maybe allocate device-global string IDs, and patch descriptors */ 2755 /* Maybe allocate device-global string IDs, and patch descriptors */
2725 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { 2756 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
@@ -2730,8 +2761,10 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2730 fsg_intf_desc.iInterface = rc; 2761 fsg_intf_desc.iInterface = rc;
2731 } 2762 }
2732 2763
2733 /* Create the LUNs, open their backing files, and register the 2764 /*
2734 * LUN devices in sysfs. */ 2765 * Create the LUNs, open their backing files, and register the
2766 * LUN devices in sysfs.
2767 */
2735 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); 2768 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2736 if (unlikely(!curlun)) { 2769 if (unlikely(!curlun)) {
2737 rc = -ENOMEM; 2770 rc = -ENOMEM;
@@ -2744,6 +2777,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2744 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) { 2777 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2745 curlun->cdrom = !!lcfg->cdrom; 2778 curlun->cdrom = !!lcfg->cdrom;
2746 curlun->ro = lcfg->cdrom || lcfg->ro; 2779 curlun->ro = lcfg->cdrom || lcfg->ro;
2780 curlun->initially_ro = curlun->ro;
2747 curlun->removable = lcfg->removable; 2781 curlun->removable = lcfg->removable;
2748 curlun->dev.release = fsg_lun_release; 2782 curlun->dev.release = fsg_lun_release;
2749 curlun->dev.parent = &gadget->dev; 2783 curlun->dev.parent = &gadget->dev;
@@ -2759,6 +2793,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2759 if (rc) { 2793 if (rc) {
2760 INFO(common, "failed to register LUN%d: %d\n", i, rc); 2794 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2761 common->nluns = i; 2795 common->nluns = i;
2796 put_device(&curlun->dev);
2762 goto error_release; 2797 goto error_release;
2763 } 2798 }
2764 2799
@@ -2768,6 +2803,9 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2768 rc = device_create_file(&curlun->dev, &dev_attr_file); 2803 rc = device_create_file(&curlun->dev, &dev_attr_file);
2769 if (rc) 2804 if (rc)
2770 goto error_luns; 2805 goto error_luns;
2806 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
2807 if (rc)
2808 goto error_luns;
2771 2809
2772 if (lcfg->filename) { 2810 if (lcfg->filename) {
2773 rc = fsg_lun_open(curlun, lcfg->filename); 2811 rc = fsg_lun_open(curlun, lcfg->filename);
@@ -2781,7 +2819,6 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2781 } 2819 }
2782 common->nluns = nluns; 2820 common->nluns = nluns;
2783 2821
2784
2785 /* Data buffers cyclic list */ 2822 /* Data buffers cyclic list */
2786 bh = common->buffhds; 2823 bh = common->buffhds;
2787 i = FSG_NUM_BUFFERS; 2824 i = FSG_NUM_BUFFERS;
@@ -2798,7 +2835,6 @@ buffhds_first_it:
2798 } while (--i); 2835 } while (--i);
2799 bh->next = common->buffhds; 2836 bh->next = common->buffhds;
2800 2837
2801
2802 /* Prepare inquiryString */ 2838 /* Prepare inquiryString */
2803 if (cfg->release != 0xffff) { 2839 if (cfg->release != 0xffff) {
2804 i = cfg->release; 2840 i = cfg->release;
@@ -2812,41 +2848,35 @@ buffhds_first_it:
2812 i = 0x0399; 2848 i = 0x0399;
2813 } 2849 }
2814 } 2850 }
2815#define OR(x, y) ((x) ? (x) : (y))
2816 snprintf(common->inquiry_string, sizeof common->inquiry_string, 2851 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2817 "%-8s%-16s%04x", 2852 "%-8s%-16s%04x", cfg->vendor_name ?: "Linux",
2818 OR(cfg->vendor_name, "Linux "),
2819 /* Assume product name dependent on the first LUN */ 2853 /* Assume product name dependent on the first LUN */
2820 OR(cfg->product_name, common->luns->cdrom 2854 cfg->product_name ?: (common->luns->cdrom
2821 ? "File-Stor Gadget" 2855 ? "File-Stor Gadget"
2822 : "File-CD Gadget "), 2856 : "File-CD Gadget"),
2823 i); 2857 i);
2824 2858
2825 2859 /*
2826 /* Some peripheral controllers are known not to be able to 2860 * Some peripheral controllers are known not to be able to
2827 * halt bulk endpoints correctly. If one of them is present, 2861 * halt bulk endpoints correctly. If one of them is present,
2828 * disable stalls. 2862 * disable stalls.
2829 */ 2863 */
2830 common->can_stall = cfg->can_stall && 2864 common->can_stall = cfg->can_stall &&
2831 !(gadget_is_at91(common->gadget)); 2865 !(gadget_is_at91(common->gadget));
2832 2866
2833
2834 spin_lock_init(&common->lock); 2867 spin_lock_init(&common->lock);
2835 kref_init(&common->ref); 2868 kref_init(&common->ref);
2836 2869
2837
2838 /* Tell the thread to start working */ 2870 /* Tell the thread to start working */
2839 common->thread_task = 2871 common->thread_task =
2840 kthread_create(fsg_main_thread, common, 2872 kthread_create(fsg_main_thread, common,
2841 OR(cfg->thread_name, "file-storage")); 2873 cfg->thread_name ?: "file-storage");
2842 if (IS_ERR(common->thread_task)) { 2874 if (IS_ERR(common->thread_task)) {
2843 rc = PTR_ERR(common->thread_task); 2875 rc = PTR_ERR(common->thread_task);
2844 goto error_release; 2876 goto error_release;
2845 } 2877 }
2846 init_completion(&common->thread_notifier); 2878 init_completion(&common->thread_notifier);
2847 init_waitqueue_head(&common->fsg_wait); 2879 init_waitqueue_head(&common->fsg_wait);
2848#undef OR
2849
2850 2880
2851 /* Information */ 2881 /* Information */
2852 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); 2882 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
@@ -2880,18 +2910,15 @@ buffhds_first_it:
2880 2910
2881 return common; 2911 return common;
2882 2912
2883
2884error_luns: 2913error_luns:
2885 common->nluns = i + 1; 2914 common->nluns = i + 1;
2886error_release: 2915error_release:
2887 common->state = FSG_STATE_TERMINATED; /* The thread is dead */ 2916 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
2888 /* Call fsg_common_release() directly, ref might be not 2917 /* Call fsg_common_release() directly, ref might be not initialised. */
2889 * initialised */
2890 fsg_common_release(&common->ref); 2918 fsg_common_release(&common->ref);
2891 return ERR_PTR(rc); 2919 return ERR_PTR(rc);
2892} 2920}
2893 2921
2894
2895static void fsg_common_release(struct kref *ref) 2922static void fsg_common_release(struct kref *ref)
2896{ 2923{
2897 struct fsg_common *common = container_of(ref, struct fsg_common, ref); 2924 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
@@ -2900,9 +2927,6 @@ static void fsg_common_release(struct kref *ref)
2900 if (common->state != FSG_STATE_TERMINATED) { 2927 if (common->state != FSG_STATE_TERMINATED) {
2901 raise_exception(common, FSG_STATE_EXIT); 2928 raise_exception(common, FSG_STATE_EXIT);
2902 wait_for_completion(&common->thread_notifier); 2929 wait_for_completion(&common->thread_notifier);
2903
2904 /* The cleanup routine waits for this completion also */
2905 complete(&common->thread_notifier);
2906 } 2930 }
2907 2931
2908 if (likely(common->luns)) { 2932 if (likely(common->luns)) {
@@ -2911,6 +2935,7 @@ static void fsg_common_release(struct kref *ref)
2911 2935
2912 /* In error recovery common->nluns may be zero. */ 2936 /* In error recovery common->nluns may be zero. */
2913 for (; i; --i, ++lun) { 2937 for (; i; --i, ++lun) {
2938 device_remove_file(&lun->dev, &dev_attr_nofua);
2914 device_remove_file(&lun->dev, &dev_attr_ro); 2939 device_remove_file(&lun->dev, &dev_attr_ro);
2915 device_remove_file(&lun->dev, &dev_attr_file); 2940 device_remove_file(&lun->dev, &dev_attr_file);
2916 fsg_lun_close(lun); 2941 fsg_lun_close(lun);
@@ -2935,7 +2960,6 @@ static void fsg_common_release(struct kref *ref)
2935 2960
2936/*-------------------------------------------------------------------------*/ 2961/*-------------------------------------------------------------------------*/
2937 2962
2938
2939static void fsg_unbind(struct usb_configuration *c, struct usb_function *f) 2963static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2940{ 2964{
2941 struct fsg_dev *fsg = fsg_from_func(f); 2965 struct fsg_dev *fsg = fsg_from_func(f);
@@ -2955,7 +2979,6 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2955 kfree(fsg); 2979 kfree(fsg);
2956} 2980}
2957 2981
2958
2959static int fsg_bind(struct usb_configuration *c, struct usb_function *f) 2982static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2960{ 2983{
2961 struct fsg_dev *fsg = fsg_from_func(f); 2984 struct fsg_dev *fsg = fsg_from_func(f);
@@ -3038,11 +3061,13 @@ static int fsg_bind_config(struct usb_composite_dev *cdev,
3038 fsg->function.disable = fsg_disable; 3061 fsg->function.disable = fsg_disable;
3039 3062
3040 fsg->common = common; 3063 fsg->common = common;
3041 /* Our caller holds a reference to common structure so we 3064 /*
3065 * Our caller holds a reference to common structure so we
3042 * don't have to be worry about it being freed until we return 3066 * don't have to be worry about it being freed until we return
3043 * from this function. So instead of incrementing counter now 3067 * from this function. So instead of incrementing counter now
3044 * and decrement in error recovery we increment it only when 3068 * and decrement in error recovery we increment it only when
3045 * call to usb_add_function() was successful. */ 3069 * call to usb_add_function() was successful.
3070 */
3046 3071
3047 rc = usb_add_function(c, &fsg->function); 3072 rc = usb_add_function(c, &fsg->function);
3048 if (unlikely(rc)) 3073 if (unlikely(rc))
@@ -3053,8 +3078,7 @@ static int fsg_bind_config(struct usb_composite_dev *cdev,
3053} 3078}
3054 3079
3055static inline int __deprecated __maybe_unused 3080static inline int __deprecated __maybe_unused
3056fsg_add(struct usb_composite_dev *cdev, 3081fsg_add(struct usb_composite_dev *cdev, struct usb_configuration *c,
3057 struct usb_configuration *c,
3058 struct fsg_common *common) 3082 struct fsg_common *common)
3059{ 3083{
3060 return fsg_bind_config(cdev, c, common); 3084 return fsg_bind_config(cdev, c, common);
@@ -3063,19 +3087,19 @@ fsg_add(struct usb_composite_dev *cdev,
3063 3087
3064/************************* Module parameters *************************/ 3088/************************* Module parameters *************************/
3065 3089
3066
3067struct fsg_module_parameters { 3090struct fsg_module_parameters {
3068 char *file[FSG_MAX_LUNS]; 3091 char *file[FSG_MAX_LUNS];
3069 int ro[FSG_MAX_LUNS]; 3092 int ro[FSG_MAX_LUNS];
3070 int removable[FSG_MAX_LUNS]; 3093 int removable[FSG_MAX_LUNS];
3071 int cdrom[FSG_MAX_LUNS]; 3094 int cdrom[FSG_MAX_LUNS];
3095 int nofua[FSG_MAX_LUNS];
3072 3096
3073 unsigned int file_count, ro_count, removable_count, cdrom_count; 3097 unsigned int file_count, ro_count, removable_count, cdrom_count;
3098 unsigned int nofua_count;
3074 unsigned int luns; /* nluns */ 3099 unsigned int luns; /* nluns */
3075 int stall; /* can_stall */ 3100 int stall; /* can_stall */
3076}; 3101};
3077 3102
3078
3079#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \ 3103#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3080 module_param_array_named(prefix ## name, params.name, type, \ 3104 module_param_array_named(prefix ## name, params.name, type, \
3081 &prefix ## params.name ## _count, \ 3105 &prefix ## params.name ## _count, \
@@ -3096,12 +3120,13 @@ struct fsg_module_parameters {
3096 "true to simulate removable media"); \ 3120 "true to simulate removable media"); \
3097 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \ 3121 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3098 "true to simulate CD-ROM instead of disk"); \ 3122 "true to simulate CD-ROM instead of disk"); \
3123 _FSG_MODULE_PARAM_ARRAY(prefix, params, nofua, bool, \
3124 "true to ignore SCSI WRITE(10,12) FUA bit"); \
3099 _FSG_MODULE_PARAM(prefix, params, luns, uint, \ 3125 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3100 "number of LUNs"); \ 3126 "number of LUNs"); \
3101 _FSG_MODULE_PARAM(prefix, params, stall, bool, \ 3127 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3102 "false to prevent bulk stalls") 3128 "false to prevent bulk stalls")
3103 3129
3104
3105static void 3130static void
3106fsg_config_from_params(struct fsg_config *cfg, 3131fsg_config_from_params(struct fsg_config *cfg,
3107 const struct fsg_module_parameters *params) 3132 const struct fsg_module_parameters *params)
diff --git a/drivers/usb/gadget/f_ncm.c b/drivers/usb/gadget/f_ncm.c
new file mode 100644
index 000000000000..86902a60bcdb
--- /dev/null
+++ b/drivers/usb/gadget/f_ncm.c
@@ -0,0 +1,1407 @@
1/*
2 * f_ncm.c -- USB CDC Network (NCM) link function driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from f_ecm.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * 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 */
26
27#include <linux/kernel.h>
28#include <linux/device.h>
29#include <linux/etherdevice.h>
30#include <linux/crc32.h>
31
32#include <linux/usb/cdc.h>
33
34#include "u_ether.h"
35
36/*
37 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
38 * NCM is intended to be used with high-speed network attachments.
39 *
40 * Note that NCM requires the use of "alternate settings" for its data
41 * interface. This means that the set_alt() method has real work to do,
42 * and also means that a get_alt() method is required.
43 */
44
45/* to trigger crc/non-crc ndp signature */
46
47#define NCM_NDP_HDR_CRC_MASK 0x01000000
48#define NCM_NDP_HDR_CRC 0x01000000
49#define NCM_NDP_HDR_NOCRC 0x00000000
50
51struct ncm_ep_descs {
52 struct usb_endpoint_descriptor *in;
53 struct usb_endpoint_descriptor *out;
54 struct usb_endpoint_descriptor *notify;
55};
56
57enum ncm_notify_state {
58 NCM_NOTIFY_NONE, /* don't notify */
59 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
60 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
61};
62
63struct f_ncm {
64 struct gether port;
65 u8 ctrl_id, data_id;
66
67 char ethaddr[14];
68
69 struct ncm_ep_descs fs;
70 struct ncm_ep_descs hs;
71
72 struct usb_ep *notify;
73 struct usb_endpoint_descriptor *notify_desc;
74 struct usb_request *notify_req;
75 u8 notify_state;
76 bool is_open;
77
78 struct ndp_parser_opts *parser_opts;
79 bool is_crc;
80
81 /*
82 * for notification, it is accessed from both
83 * callback and ethernet open/close
84 */
85 spinlock_t lock;
86};
87
88static inline struct f_ncm *func_to_ncm(struct usb_function *f)
89{
90 return container_of(f, struct f_ncm, port.func);
91}
92
93/* peak (theoretical) bulk transfer rate in bits-per-second */
94static inline unsigned ncm_bitrate(struct usb_gadget *g)
95{
96 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
97 return 13 * 512 * 8 * 1000 * 8;
98 else
99 return 19 * 64 * 1 * 1000 * 8;
100}
101
102/*-------------------------------------------------------------------------*/
103
104/*
105 * We cannot group frames so use just the minimal size which ok to put
106 * one max-size ethernet frame.
107 * If the host can group frames, allow it to do that, 16K is selected,
108 * because it's used by default by the current linux host driver
109 */
110#define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
111#define NTB_OUT_SIZE 16384
112
113/*
114 * skbs of size less than that will not be aligned
115 * to NCM's dwNtbInMaxSize to save bus bandwidth
116 */
117
118#define MAX_TX_NONFIXED (512 * 3)
119
120#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
121 USB_CDC_NCM_NTB32_SUPPORTED)
122
123static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
124 .wLength = sizeof ntb_parameters,
125 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
126 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
127 .wNdpInDivisor = cpu_to_le16(4),
128 .wNdpInPayloadRemainder = cpu_to_le16(0),
129 .wNdpInAlignment = cpu_to_le16(4),
130
131 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
132 .wNdpOutDivisor = cpu_to_le16(4),
133 .wNdpOutPayloadRemainder = cpu_to_le16(0),
134 .wNdpOutAlignment = cpu_to_le16(4),
135};
136
137/*
138 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
139 * packet, to simplify cancellation; and a big transfer interval, to
140 * waste less bandwidth.
141 */
142
143#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
144#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
145
146static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata = {
147 .bLength = sizeof ncm_iad_desc,
148 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
149
150 /* .bFirstInterface = DYNAMIC, */
151 .bInterfaceCount = 2, /* control + data */
152 .bFunctionClass = USB_CLASS_COMM,
153 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
154 .bFunctionProtocol = USB_CDC_PROTO_NONE,
155 /* .iFunction = DYNAMIC */
156};
157
158/* interface descriptor: */
159
160static struct usb_interface_descriptor ncm_control_intf __initdata = {
161 .bLength = sizeof ncm_control_intf,
162 .bDescriptorType = USB_DT_INTERFACE,
163
164 /* .bInterfaceNumber = DYNAMIC */
165 .bNumEndpoints = 1,
166 .bInterfaceClass = USB_CLASS_COMM,
167 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
168 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
169 /* .iInterface = DYNAMIC */
170};
171
172static struct usb_cdc_header_desc ncm_header_desc __initdata = {
173 .bLength = sizeof ncm_header_desc,
174 .bDescriptorType = USB_DT_CS_INTERFACE,
175 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
176
177 .bcdCDC = cpu_to_le16(0x0110),
178};
179
180static struct usb_cdc_union_desc ncm_union_desc __initdata = {
181 .bLength = sizeof(ncm_union_desc),
182 .bDescriptorType = USB_DT_CS_INTERFACE,
183 .bDescriptorSubType = USB_CDC_UNION_TYPE,
184 /* .bMasterInterface0 = DYNAMIC */
185 /* .bSlaveInterface0 = DYNAMIC */
186};
187
188static struct usb_cdc_ether_desc ecm_desc __initdata = {
189 .bLength = sizeof ecm_desc,
190 .bDescriptorType = USB_DT_CS_INTERFACE,
191 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
192
193 /* this descriptor actually adds value, surprise! */
194 /* .iMACAddress = DYNAMIC */
195 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
196 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
197 .wNumberMCFilters = cpu_to_le16(0),
198 .bNumberPowerFilters = 0,
199};
200
201#define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
202
203static struct usb_cdc_ncm_desc ncm_desc __initdata = {
204 .bLength = sizeof ncm_desc,
205 .bDescriptorType = USB_DT_CS_INTERFACE,
206 .bDescriptorSubType = USB_CDC_NCM_TYPE,
207
208 .bcdNcmVersion = cpu_to_le16(0x0100),
209 /* can process SetEthernetPacketFilter */
210 .bmNetworkCapabilities = NCAPS,
211};
212
213/* the default data interface has no endpoints ... */
214
215static struct usb_interface_descriptor ncm_data_nop_intf __initdata = {
216 .bLength = sizeof ncm_data_nop_intf,
217 .bDescriptorType = USB_DT_INTERFACE,
218
219 .bInterfaceNumber = 1,
220 .bAlternateSetting = 0,
221 .bNumEndpoints = 0,
222 .bInterfaceClass = USB_CLASS_CDC_DATA,
223 .bInterfaceSubClass = 0,
224 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
225 /* .iInterface = DYNAMIC */
226};
227
228/* ... but the "real" data interface has two bulk endpoints */
229
230static struct usb_interface_descriptor ncm_data_intf __initdata = {
231 .bLength = sizeof ncm_data_intf,
232 .bDescriptorType = USB_DT_INTERFACE,
233
234 .bInterfaceNumber = 1,
235 .bAlternateSetting = 1,
236 .bNumEndpoints = 2,
237 .bInterfaceClass = USB_CLASS_CDC_DATA,
238 .bInterfaceSubClass = 0,
239 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
240 /* .iInterface = DYNAMIC */
241};
242
243/* full speed support: */
244
245static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_INT,
251 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
252 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
253};
254
255static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = {
256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258
259 .bEndpointAddress = USB_DIR_IN,
260 .bmAttributes = USB_ENDPOINT_XFER_BULK,
261};
262
263static struct usb_endpoint_descriptor fs_ncm_out_desc __initdata = {
264 .bLength = USB_DT_ENDPOINT_SIZE,
265 .bDescriptorType = USB_DT_ENDPOINT,
266
267 .bEndpointAddress = USB_DIR_OUT,
268 .bmAttributes = USB_ENDPOINT_XFER_BULK,
269};
270
271static struct usb_descriptor_header *ncm_fs_function[] __initdata = {
272 (struct usb_descriptor_header *) &ncm_iad_desc,
273 /* CDC NCM control descriptors */
274 (struct usb_descriptor_header *) &ncm_control_intf,
275 (struct usb_descriptor_header *) &ncm_header_desc,
276 (struct usb_descriptor_header *) &ncm_union_desc,
277 (struct usb_descriptor_header *) &ecm_desc,
278 (struct usb_descriptor_header *) &ncm_desc,
279 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
280 /* data interface, altsettings 0 and 1 */
281 (struct usb_descriptor_header *) &ncm_data_nop_intf,
282 (struct usb_descriptor_header *) &ncm_data_intf,
283 (struct usb_descriptor_header *) &fs_ncm_in_desc,
284 (struct usb_descriptor_header *) &fs_ncm_out_desc,
285 NULL,
286};
287
288/* high speed support: */
289
290static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata = {
291 .bLength = USB_DT_ENDPOINT_SIZE,
292 .bDescriptorType = USB_DT_ENDPOINT,
293
294 .bEndpointAddress = USB_DIR_IN,
295 .bmAttributes = USB_ENDPOINT_XFER_INT,
296 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
297 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
298};
299static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = {
300 .bLength = USB_DT_ENDPOINT_SIZE,
301 .bDescriptorType = USB_DT_ENDPOINT,
302
303 .bEndpointAddress = USB_DIR_IN,
304 .bmAttributes = USB_ENDPOINT_XFER_BULK,
305 .wMaxPacketSize = cpu_to_le16(512),
306};
307
308static struct usb_endpoint_descriptor hs_ncm_out_desc __initdata = {
309 .bLength = USB_DT_ENDPOINT_SIZE,
310 .bDescriptorType = USB_DT_ENDPOINT,
311
312 .bEndpointAddress = USB_DIR_OUT,
313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
314 .wMaxPacketSize = cpu_to_le16(512),
315};
316
317static struct usb_descriptor_header *ncm_hs_function[] __initdata = {
318 (struct usb_descriptor_header *) &ncm_iad_desc,
319 /* CDC NCM control descriptors */
320 (struct usb_descriptor_header *) &ncm_control_intf,
321 (struct usb_descriptor_header *) &ncm_header_desc,
322 (struct usb_descriptor_header *) &ncm_union_desc,
323 (struct usb_descriptor_header *) &ecm_desc,
324 (struct usb_descriptor_header *) &ncm_desc,
325 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
326 /* data interface, altsettings 0 and 1 */
327 (struct usb_descriptor_header *) &ncm_data_nop_intf,
328 (struct usb_descriptor_header *) &ncm_data_intf,
329 (struct usb_descriptor_header *) &hs_ncm_in_desc,
330 (struct usb_descriptor_header *) &hs_ncm_out_desc,
331 NULL,
332};
333
334/* string descriptors: */
335
336#define STRING_CTRL_IDX 0
337#define STRING_MAC_IDX 1
338#define STRING_DATA_IDX 2
339#define STRING_IAD_IDX 3
340
341static struct usb_string ncm_string_defs[] = {
342 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
343 [STRING_MAC_IDX].s = NULL /* DYNAMIC */,
344 [STRING_DATA_IDX].s = "CDC Network Data",
345 [STRING_IAD_IDX].s = "CDC NCM",
346 { } /* end of list */
347};
348
349static struct usb_gadget_strings ncm_string_table = {
350 .language = 0x0409, /* en-us */
351 .strings = ncm_string_defs,
352};
353
354static struct usb_gadget_strings *ncm_strings[] = {
355 &ncm_string_table,
356 NULL,
357};
358
359/*
360 * Here are options for NCM Datagram Pointer table (NDP) parser.
361 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
362 * in NDP16 offsets and sizes fields are 1 16bit word wide,
363 * in NDP32 -- 2 16bit words wide. Also signatures are different.
364 * To make the parser code the same, put the differences in the structure,
365 * and switch pointers to the structures when the format is changed.
366 */
367
368struct ndp_parser_opts {
369 u32 nth_sign;
370 u32 ndp_sign;
371 unsigned nth_size;
372 unsigned ndp_size;
373 unsigned ndplen_align;
374 /* sizes in u16 units */
375 unsigned dgram_item_len; /* index or length */
376 unsigned block_length;
377 unsigned fp_index;
378 unsigned reserved1;
379 unsigned reserved2;
380 unsigned next_fp_index;
381};
382
383#define INIT_NDP16_OPTS { \
384 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
385 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
386 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
387 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
388 .ndplen_align = 4, \
389 .dgram_item_len = 1, \
390 .block_length = 1, \
391 .fp_index = 1, \
392 .reserved1 = 0, \
393 .reserved2 = 0, \
394 .next_fp_index = 1, \
395 }
396
397
398#define INIT_NDP32_OPTS { \
399 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
400 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
401 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
402 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
403 .ndplen_align = 8, \
404 .dgram_item_len = 2, \
405 .block_length = 2, \
406 .fp_index = 2, \
407 .reserved1 = 1, \
408 .reserved2 = 2, \
409 .next_fp_index = 2, \
410 }
411
412static struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
413static struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
414
415static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
416{
417 switch (size) {
418 case 1:
419 put_unaligned_le16((u16)val, *p);
420 break;
421 case 2:
422 put_unaligned_le32((u32)val, *p);
423
424 break;
425 default:
426 BUG();
427 }
428
429 *p += size;
430}
431
432static inline unsigned get_ncm(__le16 **p, unsigned size)
433{
434 unsigned tmp;
435
436 switch (size) {
437 case 1:
438 tmp = get_unaligned_le16(*p);
439 break;
440 case 2:
441 tmp = get_unaligned_le32(*p);
442 break;
443 default:
444 BUG();
445 }
446
447 *p += size;
448 return tmp;
449}
450
451/*-------------------------------------------------------------------------*/
452
453static inline void ncm_reset_values(struct f_ncm *ncm)
454{
455 ncm->parser_opts = &ndp16_opts;
456 ncm->is_crc = false;
457 ncm->port.cdc_filter = DEFAULT_FILTER;
458
459 /* doesn't make sense for ncm, fixed size used */
460 ncm->port.header_len = 0;
461
462 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
463 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
464}
465
466/*
467 * Context: ncm->lock held
468 */
469static void ncm_do_notify(struct f_ncm *ncm)
470{
471 struct usb_request *req = ncm->notify_req;
472 struct usb_cdc_notification *event;
473 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
474 __le32 *data;
475 int status;
476
477 /* notification already in flight? */
478 if (!req)
479 return;
480
481 event = req->buf;
482 switch (ncm->notify_state) {
483 case NCM_NOTIFY_NONE:
484 return;
485
486 case NCM_NOTIFY_CONNECT:
487 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
488 if (ncm->is_open)
489 event->wValue = cpu_to_le16(1);
490 else
491 event->wValue = cpu_to_le16(0);
492 event->wLength = 0;
493 req->length = sizeof *event;
494
495 DBG(cdev, "notify connect %s\n",
496 ncm->is_open ? "true" : "false");
497 ncm->notify_state = NCM_NOTIFY_NONE;
498 break;
499
500 case NCM_NOTIFY_SPEED:
501 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
502 event->wValue = cpu_to_le16(0);
503 event->wLength = cpu_to_le16(8);
504 req->length = NCM_STATUS_BYTECOUNT;
505
506 /* SPEED_CHANGE data is up/down speeds in bits/sec */
507 data = req->buf + sizeof *event;
508 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
509 data[1] = data[0];
510
511 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
512 ncm->notify_state = NCM_NOTIFY_CONNECT;
513 break;
514 }
515 event->bmRequestType = 0xA1;
516 event->wIndex = cpu_to_le16(ncm->ctrl_id);
517
518 ncm->notify_req = NULL;
519 /*
520 * In double buffering if there is a space in FIFO,
521 * completion callback can be called right after the call,
522 * so unlocking
523 */
524 spin_unlock(&ncm->lock);
525 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
526 spin_lock(&ncm->lock);
527 if (status < 0) {
528 ncm->notify_req = req;
529 DBG(cdev, "notify --> %d\n", status);
530 }
531}
532
533/*
534 * Context: ncm->lock held
535 */
536static void ncm_notify(struct f_ncm *ncm)
537{
538 /*
539 * NOTE on most versions of Linux, host side cdc-ethernet
540 * won't listen for notifications until its netdevice opens.
541 * The first notification then sits in the FIFO for a long
542 * time, and the second one is queued.
543 *
544 * If ncm_notify() is called before the second (CONNECT)
545 * notification is sent, then it will reset to send the SPEED
546 * notificaion again (and again, and again), but it's not a problem
547 */
548 ncm->notify_state = NCM_NOTIFY_SPEED;
549 ncm_do_notify(ncm);
550}
551
552static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
553{
554 struct f_ncm *ncm = req->context;
555 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
556 struct usb_cdc_notification *event = req->buf;
557
558 spin_lock(&ncm->lock);
559 switch (req->status) {
560 case 0:
561 VDBG(cdev, "Notification %02x sent\n",
562 event->bNotificationType);
563 break;
564 case -ECONNRESET:
565 case -ESHUTDOWN:
566 ncm->notify_state = NCM_NOTIFY_NONE;
567 break;
568 default:
569 DBG(cdev, "event %02x --> %d\n",
570 event->bNotificationType, req->status);
571 break;
572 }
573 ncm->notify_req = req;
574 ncm_do_notify(ncm);
575 spin_unlock(&ncm->lock);
576}
577
578static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
579{
580 /* now for SET_NTB_INPUT_SIZE only */
581 unsigned in_size;
582 struct usb_function *f = req->context;
583 struct f_ncm *ncm = func_to_ncm(f);
584 struct usb_composite_dev *cdev = ep->driver_data;
585
586 req->context = NULL;
587 if (req->status || req->actual != req->length) {
588 DBG(cdev, "Bad control-OUT transfer\n");
589 goto invalid;
590 }
591
592 in_size = get_unaligned_le32(req->buf);
593 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
594 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
595 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
596 goto invalid;
597 }
598
599 ncm->port.fixed_in_len = in_size;
600 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
601 return;
602
603invalid:
604 usb_ep_set_halt(ep);
605 return;
606}
607
608static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
609{
610 struct f_ncm *ncm = func_to_ncm(f);
611 struct usb_composite_dev *cdev = f->config->cdev;
612 struct usb_request *req = cdev->req;
613 int value = -EOPNOTSUPP;
614 u16 w_index = le16_to_cpu(ctrl->wIndex);
615 u16 w_value = le16_to_cpu(ctrl->wValue);
616 u16 w_length = le16_to_cpu(ctrl->wLength);
617
618 /*
619 * composite driver infrastructure handles everything except
620 * CDC class messages; interface activation uses set_alt().
621 */
622 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
623 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
624 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
625 /*
626 * see 6.2.30: no data, wIndex = interface,
627 * wValue = packet filter bitmap
628 */
629 if (w_length != 0 || w_index != ncm->ctrl_id)
630 goto invalid;
631 DBG(cdev, "packet filter %02x\n", w_value);
632 /*
633 * REVISIT locking of cdc_filter. This assumes the UDC
634 * driver won't have a concurrent packet TX irq running on
635 * another CPU; or that if it does, this write is atomic...
636 */
637 ncm->port.cdc_filter = w_value;
638 value = 0;
639 break;
640 /*
641 * and optionally:
642 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
643 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
644 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
645 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
646 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
647 * case USB_CDC_GET_ETHERNET_STATISTIC:
648 */
649
650 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
651 | USB_CDC_GET_NTB_PARAMETERS:
652
653 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
654 goto invalid;
655 value = w_length > sizeof ntb_parameters ?
656 sizeof ntb_parameters : w_length;
657 memcpy(req->buf, &ntb_parameters, value);
658 VDBG(cdev, "Host asked NTB parameters\n");
659 break;
660
661 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
662 | USB_CDC_GET_NTB_INPUT_SIZE:
663
664 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
665 goto invalid;
666 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
667 value = 4;
668 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
669 ncm->port.fixed_in_len);
670 break;
671
672 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
673 | USB_CDC_SET_NTB_INPUT_SIZE:
674 {
675 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
676 goto invalid;
677 req->complete = ncm_ep0out_complete;
678 req->length = w_length;
679 req->context = f;
680
681 value = req->length;
682 break;
683 }
684
685 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
686 | USB_CDC_GET_NTB_FORMAT:
687 {
688 uint16_t format;
689
690 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
691 goto invalid;
692 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
693 put_unaligned_le16(format, req->buf);
694 value = 2;
695 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
696 break;
697 }
698
699 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
700 | USB_CDC_SET_NTB_FORMAT:
701 {
702 if (w_length != 0 || w_index != ncm->ctrl_id)
703 goto invalid;
704 switch (w_value) {
705 case 0x0000:
706 ncm->parser_opts = &ndp16_opts;
707 DBG(cdev, "NCM16 selected\n");
708 break;
709 case 0x0001:
710 ncm->parser_opts = &ndp32_opts;
711 DBG(cdev, "NCM32 selected\n");
712 break;
713 default:
714 goto invalid;
715 }
716 value = 0;
717 break;
718 }
719 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
720 | USB_CDC_GET_CRC_MODE:
721 {
722 uint16_t is_crc;
723
724 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
725 goto invalid;
726 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
727 put_unaligned_le16(is_crc, req->buf);
728 value = 2;
729 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
730 break;
731 }
732
733 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
734 | USB_CDC_SET_CRC_MODE:
735 {
736 int ndp_hdr_crc = 0;
737
738 if (w_length != 0 || w_index != ncm->ctrl_id)
739 goto invalid;
740 switch (w_value) {
741 case 0x0000:
742 ncm->is_crc = false;
743 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
744 DBG(cdev, "non-CRC mode selected\n");
745 break;
746 case 0x0001:
747 ncm->is_crc = true;
748 ndp_hdr_crc = NCM_NDP_HDR_CRC;
749 DBG(cdev, "CRC mode selected\n");
750 break;
751 default:
752 goto invalid;
753 }
754 ncm->parser_opts->ndp_sign &= ~NCM_NDP_HDR_CRC_MASK;
755 ncm->parser_opts->ndp_sign |= ndp_hdr_crc;
756 value = 0;
757 break;
758 }
759
760 /* and disabled in ncm descriptor: */
761 /* case USB_CDC_GET_NET_ADDRESS: */
762 /* case USB_CDC_SET_NET_ADDRESS: */
763 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
764 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
765
766 default:
767invalid:
768 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
769 ctrl->bRequestType, ctrl->bRequest,
770 w_value, w_index, w_length);
771 }
772
773 /* respond with data transfer or status phase? */
774 if (value >= 0) {
775 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
776 ctrl->bRequestType, ctrl->bRequest,
777 w_value, w_index, w_length);
778 req->zero = 0;
779 req->length = value;
780 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
781 if (value < 0)
782 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
783 ctrl->bRequestType, ctrl->bRequest,
784 value);
785 }
786
787 /* device either stalls (value < 0) or reports success */
788 return value;
789}
790
791
792static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
793{
794 struct f_ncm *ncm = func_to_ncm(f);
795 struct usb_composite_dev *cdev = f->config->cdev;
796
797 /* Control interface has only altsetting 0 */
798 if (intf == ncm->ctrl_id) {
799 if (alt != 0)
800 goto fail;
801
802 if (ncm->notify->driver_data) {
803 DBG(cdev, "reset ncm control %d\n", intf);
804 usb_ep_disable(ncm->notify);
805 } else {
806 DBG(cdev, "init ncm ctrl %d\n", intf);
807 ncm->notify_desc = ep_choose(cdev->gadget,
808 ncm->hs.notify,
809 ncm->fs.notify);
810 }
811 usb_ep_enable(ncm->notify, ncm->notify_desc);
812 ncm->notify->driver_data = ncm;
813
814 /* Data interface has two altsettings, 0 and 1 */
815 } else if (intf == ncm->data_id) {
816 if (alt > 1)
817 goto fail;
818
819 if (ncm->port.in_ep->driver_data) {
820 DBG(cdev, "reset ncm\n");
821 gether_disconnect(&ncm->port);
822 ncm_reset_values(ncm);
823 }
824
825 /*
826 * CDC Network only sends data in non-default altsettings.
827 * Changing altsettings resets filters, statistics, etc.
828 */
829 if (alt == 1) {
830 struct net_device *net;
831
832 if (!ncm->port.in) {
833 DBG(cdev, "init ncm\n");
834 ncm->port.in = ep_choose(cdev->gadget,
835 ncm->hs.in,
836 ncm->fs.in);
837 ncm->port.out = ep_choose(cdev->gadget,
838 ncm->hs.out,
839 ncm->fs.out);
840 }
841
842 /* TODO */
843 /* Enable zlps by default for NCM conformance;
844 * override for musb_hdrc (avoids txdma ovhead)
845 */
846 ncm->port.is_zlp_ok = !(
847 gadget_is_musbhdrc(cdev->gadget)
848 );
849 ncm->port.cdc_filter = DEFAULT_FILTER;
850 DBG(cdev, "activate ncm\n");
851 net = gether_connect(&ncm->port);
852 if (IS_ERR(net))
853 return PTR_ERR(net);
854 }
855
856 spin_lock(&ncm->lock);
857 ncm_notify(ncm);
858 spin_unlock(&ncm->lock);
859 } else
860 goto fail;
861
862 return 0;
863fail:
864 return -EINVAL;
865}
866
867/*
868 * Because the data interface supports multiple altsettings,
869 * this NCM function *MUST* implement a get_alt() method.
870 */
871static int ncm_get_alt(struct usb_function *f, unsigned intf)
872{
873 struct f_ncm *ncm = func_to_ncm(f);
874
875 if (intf == ncm->ctrl_id)
876 return 0;
877 return ncm->port.in_ep->driver_data ? 1 : 0;
878}
879
880static struct sk_buff *ncm_wrap_ntb(struct gether *port,
881 struct sk_buff *skb)
882{
883 struct f_ncm *ncm = func_to_ncm(&port->func);
884 struct sk_buff *skb2;
885 int ncb_len = 0;
886 __le16 *tmp;
887 int div = ntb_parameters.wNdpInDivisor;
888 int rem = ntb_parameters.wNdpInPayloadRemainder;
889 int pad;
890 int ndp_align = ntb_parameters.wNdpInAlignment;
891 int ndp_pad;
892 unsigned max_size = ncm->port.fixed_in_len;
893 struct ndp_parser_opts *opts = ncm->parser_opts;
894 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
895
896 ncb_len += opts->nth_size;
897 ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len;
898 ncb_len += ndp_pad;
899 ncb_len += opts->ndp_size;
900 ncb_len += 2 * 2 * opts->dgram_item_len; /* Datagram entry */
901 ncb_len += 2 * 2 * opts->dgram_item_len; /* Zero datagram entry */
902 pad = ALIGN(ncb_len, div) + rem - ncb_len;
903 ncb_len += pad;
904
905 if (ncb_len + skb->len + crc_len > max_size) {
906 dev_kfree_skb_any(skb);
907 return NULL;
908 }
909
910 skb2 = skb_copy_expand(skb, ncb_len,
911 max_size - skb->len - ncb_len - crc_len,
912 GFP_ATOMIC);
913 dev_kfree_skb_any(skb);
914 if (!skb2)
915 return NULL;
916
917 skb = skb2;
918
919 tmp = (void *) skb_push(skb, ncb_len);
920 memset(tmp, 0, ncb_len);
921
922 put_unaligned_le32(opts->nth_sign, tmp); /* dwSignature */
923 tmp += 2;
924 /* wHeaderLength */
925 put_unaligned_le16(opts->nth_size, tmp++);
926 tmp++; /* skip wSequence */
927 put_ncm(&tmp, opts->block_length, skb->len); /* (d)wBlockLength */
928 /* (d)wFpIndex */
929 /* the first pointer is right after the NTH + align */
930 put_ncm(&tmp, opts->fp_index, opts->nth_size + ndp_pad);
931
932 tmp = (void *)tmp + ndp_pad;
933
934 /* NDP */
935 put_unaligned_le32(opts->ndp_sign, tmp); /* dwSignature */
936 tmp += 2;
937 /* wLength */
938 put_unaligned_le16(ncb_len - opts->nth_size - pad, tmp++);
939
940 tmp += opts->reserved1;
941 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
942 tmp += opts->reserved2;
943
944 if (ncm->is_crc) {
945 uint32_t crc;
946
947 crc = ~crc32_le(~0,
948 skb->data + ncb_len,
949 skb->len - ncb_len);
950 put_unaligned_le32(crc, skb->data + skb->len);
951 skb_put(skb, crc_len);
952 }
953
954 /* (d)wDatagramIndex[0] */
955 put_ncm(&tmp, opts->dgram_item_len, ncb_len);
956 /* (d)wDatagramLength[0] */
957 put_ncm(&tmp, opts->dgram_item_len, skb->len - ncb_len);
958 /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
959
960 if (skb->len > MAX_TX_NONFIXED)
961 memset(skb_put(skb, max_size - skb->len),
962 0, max_size - skb->len);
963
964 return skb;
965}
966
967static int ncm_unwrap_ntb(struct gether *port,
968 struct sk_buff *skb,
969 struct sk_buff_head *list)
970{
971 struct f_ncm *ncm = func_to_ncm(&port->func);
972 __le16 *tmp = (void *) skb->data;
973 unsigned index, index2;
974 unsigned dg_len, dg_len2;
975 unsigned ndp_len;
976 struct sk_buff *skb2;
977 int ret = -EINVAL;
978 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
979 struct ndp_parser_opts *opts = ncm->parser_opts;
980 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
981 int dgram_counter;
982
983 /* dwSignature */
984 if (get_unaligned_le32(tmp) != opts->nth_sign) {
985 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
986 skb->len);
987 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
988 skb->data, 32, false);
989
990 goto err;
991 }
992 tmp += 2;
993 /* wHeaderLength */
994 if (get_unaligned_le16(tmp++) != opts->nth_size) {
995 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
996 goto err;
997 }
998 tmp++; /* skip wSequence */
999
1000 /* (d)wBlockLength */
1001 if (get_ncm(&tmp, opts->block_length) > max_size) {
1002 INFO(port->func.config->cdev, "OUT size exceeded\n");
1003 goto err;
1004 }
1005
1006 index = get_ncm(&tmp, opts->fp_index);
1007 /* NCM 3.2 */
1008 if (((index % 4) != 0) && (index < opts->nth_size)) {
1009 INFO(port->func.config->cdev, "Bad index: %x\n",
1010 index);
1011 goto err;
1012 }
1013
1014 /* walk through NDP */
1015 tmp = ((void *)skb->data) + index;
1016 if (get_unaligned_le32(tmp) != opts->ndp_sign) {
1017 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1018 goto err;
1019 }
1020 tmp += 2;
1021
1022 ndp_len = get_unaligned_le16(tmp++);
1023 /*
1024 * NCM 3.3.1
1025 * entry is 2 items
1026 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1027 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1028 */
1029 if ((ndp_len < opts->ndp_size + 2 * 2 * (opts->dgram_item_len * 2))
1030 || (ndp_len % opts->ndplen_align != 0)) {
1031 INFO(port->func.config->cdev, "Bad NDP length: %x\n", ndp_len);
1032 goto err;
1033 }
1034 tmp += opts->reserved1;
1035 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
1036 tmp += opts->reserved2;
1037
1038 ndp_len -= opts->ndp_size;
1039 index2 = get_ncm(&tmp, opts->dgram_item_len);
1040 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1041 dgram_counter = 0;
1042
1043 do {
1044 index = index2;
1045 dg_len = dg_len2;
1046 if (dg_len < 14 + crc_len) { /* ethernet header + crc */
1047 INFO(port->func.config->cdev, "Bad dgram length: %x\n",
1048 dg_len);
1049 goto err;
1050 }
1051 if (ncm->is_crc) {
1052 uint32_t crc, crc2;
1053
1054 crc = get_unaligned_le32(skb->data +
1055 index + dg_len - crc_len);
1056 crc2 = ~crc32_le(~0,
1057 skb->data + index,
1058 dg_len - crc_len);
1059 if (crc != crc2) {
1060 INFO(port->func.config->cdev, "Bad CRC\n");
1061 goto err;
1062 }
1063 }
1064
1065 index2 = get_ncm(&tmp, opts->dgram_item_len);
1066 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1067
1068 if (index2 == 0 || dg_len2 == 0) {
1069 skb2 = skb;
1070 } else {
1071 skb2 = skb_clone(skb, GFP_ATOMIC);
1072 if (skb2 == NULL)
1073 goto err;
1074 }
1075
1076 if (!skb_pull(skb2, index)) {
1077 ret = -EOVERFLOW;
1078 goto err;
1079 }
1080
1081 skb_trim(skb2, dg_len - crc_len);
1082 skb_queue_tail(list, skb2);
1083
1084 ndp_len -= 2 * (opts->dgram_item_len * 2);
1085
1086 dgram_counter++;
1087
1088 if (index2 == 0 || dg_len2 == 0)
1089 break;
1090 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); /* zero entry */
1091
1092 VDBG(port->func.config->cdev,
1093 "Parsed NTB with %d frames\n", dgram_counter);
1094 return 0;
1095err:
1096 skb_queue_purge(list);
1097 dev_kfree_skb_any(skb);
1098 return ret;
1099}
1100
1101static void ncm_disable(struct usb_function *f)
1102{
1103 struct f_ncm *ncm = func_to_ncm(f);
1104 struct usb_composite_dev *cdev = f->config->cdev;
1105
1106 DBG(cdev, "ncm deactivated\n");
1107
1108 if (ncm->port.in_ep->driver_data)
1109 gether_disconnect(&ncm->port);
1110
1111 if (ncm->notify->driver_data) {
1112 usb_ep_disable(ncm->notify);
1113 ncm->notify->driver_data = NULL;
1114 ncm->notify_desc = NULL;
1115 }
1116}
1117
1118/*-------------------------------------------------------------------------*/
1119
1120/*
1121 * Callbacks let us notify the host about connect/disconnect when the
1122 * net device is opened or closed.
1123 *
1124 * For testing, note that link states on this side include both opened
1125 * and closed variants of:
1126 *
1127 * - disconnected/unconfigured
1128 * - configured but inactive (data alt 0)
1129 * - configured and active (data alt 1)
1130 *
1131 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1132 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1133 * imply the host is actually polling the notification endpoint, and
1134 * likewise that "active" doesn't imply it's actually using the data
1135 * endpoints for traffic.
1136 */
1137
1138static void ncm_open(struct gether *geth)
1139{
1140 struct f_ncm *ncm = func_to_ncm(&geth->func);
1141
1142 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1143
1144 spin_lock(&ncm->lock);
1145 ncm->is_open = true;
1146 ncm_notify(ncm);
1147 spin_unlock(&ncm->lock);
1148}
1149
1150static void ncm_close(struct gether *geth)
1151{
1152 struct f_ncm *ncm = func_to_ncm(&geth->func);
1153
1154 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1155
1156 spin_lock(&ncm->lock);
1157 ncm->is_open = false;
1158 ncm_notify(ncm);
1159 spin_unlock(&ncm->lock);
1160}
1161
1162/*-------------------------------------------------------------------------*/
1163
1164/* ethernet function driver setup/binding */
1165
1166static int __init
1167ncm_bind(struct usb_configuration *c, struct usb_function *f)
1168{
1169 struct usb_composite_dev *cdev = c->cdev;
1170 struct f_ncm *ncm = func_to_ncm(f);
1171 int status;
1172 struct usb_ep *ep;
1173
1174 /* allocate instance-specific interface IDs */
1175 status = usb_interface_id(c, f);
1176 if (status < 0)
1177 goto fail;
1178 ncm->ctrl_id = status;
1179 ncm_iad_desc.bFirstInterface = status;
1180
1181 ncm_control_intf.bInterfaceNumber = status;
1182 ncm_union_desc.bMasterInterface0 = status;
1183
1184 status = usb_interface_id(c, f);
1185 if (status < 0)
1186 goto fail;
1187 ncm->data_id = status;
1188
1189 ncm_data_nop_intf.bInterfaceNumber = status;
1190 ncm_data_intf.bInterfaceNumber = status;
1191 ncm_union_desc.bSlaveInterface0 = status;
1192
1193 status = -ENODEV;
1194
1195 /* allocate instance-specific endpoints */
1196 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1197 if (!ep)
1198 goto fail;
1199 ncm->port.in_ep = ep;
1200 ep->driver_data = cdev; /* claim */
1201
1202 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1203 if (!ep)
1204 goto fail;
1205 ncm->port.out_ep = ep;
1206 ep->driver_data = cdev; /* claim */
1207
1208 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1209 if (!ep)
1210 goto fail;
1211 ncm->notify = ep;
1212 ep->driver_data = cdev; /* claim */
1213
1214 status = -ENOMEM;
1215
1216 /* allocate notification request and buffer */
1217 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1218 if (!ncm->notify_req)
1219 goto fail;
1220 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1221 if (!ncm->notify_req->buf)
1222 goto fail;
1223 ncm->notify_req->context = ncm;
1224 ncm->notify_req->complete = ncm_notify_complete;
1225
1226 /* copy descriptors, and track endpoint copies */
1227 f->descriptors = usb_copy_descriptors(ncm_fs_function);
1228 if (!f->descriptors)
1229 goto fail;
1230
1231 ncm->fs.in = usb_find_endpoint(ncm_fs_function,
1232 f->descriptors, &fs_ncm_in_desc);
1233 ncm->fs.out = usb_find_endpoint(ncm_fs_function,
1234 f->descriptors, &fs_ncm_out_desc);
1235 ncm->fs.notify = usb_find_endpoint(ncm_fs_function,
1236 f->descriptors, &fs_ncm_notify_desc);
1237
1238 /*
1239 * support all relevant hardware speeds... we expect that when
1240 * hardware is dual speed, all bulk-capable endpoints work at
1241 * both speeds
1242 */
1243 if (gadget_is_dualspeed(c->cdev->gadget)) {
1244 hs_ncm_in_desc.bEndpointAddress =
1245 fs_ncm_in_desc.bEndpointAddress;
1246 hs_ncm_out_desc.bEndpointAddress =
1247 fs_ncm_out_desc.bEndpointAddress;
1248 hs_ncm_notify_desc.bEndpointAddress =
1249 fs_ncm_notify_desc.bEndpointAddress;
1250
1251 /* copy descriptors, and track endpoint copies */
1252 f->hs_descriptors = usb_copy_descriptors(ncm_hs_function);
1253 if (!f->hs_descriptors)
1254 goto fail;
1255
1256 ncm->hs.in = usb_find_endpoint(ncm_hs_function,
1257 f->hs_descriptors, &hs_ncm_in_desc);
1258 ncm->hs.out = usb_find_endpoint(ncm_hs_function,
1259 f->hs_descriptors, &hs_ncm_out_desc);
1260 ncm->hs.notify = usb_find_endpoint(ncm_hs_function,
1261 f->hs_descriptors, &hs_ncm_notify_desc);
1262 }
1263
1264 /*
1265 * NOTE: all that is done without knowing or caring about
1266 * the network link ... which is unavailable to this code
1267 * until we're activated via set_alt().
1268 */
1269
1270 ncm->port.open = ncm_open;
1271 ncm->port.close = ncm_close;
1272
1273 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1274 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1275 ncm->port.in_ep->name, ncm->port.out_ep->name,
1276 ncm->notify->name);
1277 return 0;
1278
1279fail:
1280 if (f->descriptors)
1281 usb_free_descriptors(f->descriptors);
1282
1283 if (ncm->notify_req) {
1284 kfree(ncm->notify_req->buf);
1285 usb_ep_free_request(ncm->notify, ncm->notify_req);
1286 }
1287
1288 /* we might as well release our claims on endpoints */
1289 if (ncm->notify)
1290 ncm->notify->driver_data = NULL;
1291 if (ncm->port.out)
1292 ncm->port.out_ep->driver_data = NULL;
1293 if (ncm->port.in)
1294 ncm->port.in_ep->driver_data = NULL;
1295
1296 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1297
1298 return status;
1299}
1300
1301static void
1302ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1303{
1304 struct f_ncm *ncm = func_to_ncm(f);
1305
1306 DBG(c->cdev, "ncm unbind\n");
1307
1308 if (gadget_is_dualspeed(c->cdev->gadget))
1309 usb_free_descriptors(f->hs_descriptors);
1310 usb_free_descriptors(f->descriptors);
1311
1312 kfree(ncm->notify_req->buf);
1313 usb_ep_free_request(ncm->notify, ncm->notify_req);
1314
1315 ncm_string_defs[1].s = NULL;
1316 kfree(ncm);
1317}
1318
1319/**
1320 * ncm_bind_config - add CDC Network link to a configuration
1321 * @c: the configuration to support the network link
1322 * @ethaddr: a buffer in which the ethernet address of the host side
1323 * side of the link was recorded
1324 * Context: single threaded during gadget setup
1325 *
1326 * Returns zero on success, else negative errno.
1327 *
1328 * Caller must have called @gether_setup(). Caller is also responsible
1329 * for calling @gether_cleanup() before module unload.
1330 */
1331int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1332{
1333 struct f_ncm *ncm;
1334 int status;
1335
1336 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
1337 return -EINVAL;
1338
1339 /* maybe allocate device-global string IDs */
1340 if (ncm_string_defs[0].id == 0) {
1341
1342 /* control interface label */
1343 status = usb_string_id(c->cdev);
1344 if (status < 0)
1345 return status;
1346 ncm_string_defs[STRING_CTRL_IDX].id = status;
1347 ncm_control_intf.iInterface = status;
1348
1349 /* data interface label */
1350 status = usb_string_id(c->cdev);
1351 if (status < 0)
1352 return status;
1353 ncm_string_defs[STRING_DATA_IDX].id = status;
1354 ncm_data_nop_intf.iInterface = status;
1355 ncm_data_intf.iInterface = status;
1356
1357 /* MAC address */
1358 status = usb_string_id(c->cdev);
1359 if (status < 0)
1360 return status;
1361 ncm_string_defs[STRING_MAC_IDX].id = status;
1362 ecm_desc.iMACAddress = status;
1363
1364 /* IAD */
1365 status = usb_string_id(c->cdev);
1366 if (status < 0)
1367 return status;
1368 ncm_string_defs[STRING_IAD_IDX].id = status;
1369 ncm_iad_desc.iFunction = status;
1370 }
1371
1372 /* allocate and initialize one new instance */
1373 ncm = kzalloc(sizeof *ncm, GFP_KERNEL);
1374 if (!ncm)
1375 return -ENOMEM;
1376
1377 /* export host's Ethernet address in CDC format */
1378 snprintf(ncm->ethaddr, sizeof ncm->ethaddr,
1379 "%02X%02X%02X%02X%02X%02X",
1380 ethaddr[0], ethaddr[1], ethaddr[2],
1381 ethaddr[3], ethaddr[4], ethaddr[5]);
1382 ncm_string_defs[1].s = ncm->ethaddr;
1383
1384 spin_lock_init(&ncm->lock);
1385 ncm_reset_values(ncm);
1386 ncm->port.is_fixed = true;
1387
1388 ncm->port.func.name = "cdc_network";
1389 ncm->port.func.strings = ncm_strings;
1390 /* descriptors are per-instance copies */
1391 ncm->port.func.bind = ncm_bind;
1392 ncm->port.func.unbind = ncm_unbind;
1393 ncm->port.func.set_alt = ncm_set_alt;
1394 ncm->port.func.get_alt = ncm_get_alt;
1395 ncm->port.func.setup = ncm_setup;
1396 ncm->port.func.disable = ncm_disable;
1397
1398 ncm->port.wrap = ncm_wrap_ntb;
1399 ncm->port.unwrap = ncm_unwrap_ntb;
1400
1401 status = usb_add_function(c, &ncm->port.func);
1402 if (status) {
1403 ncm_string_defs[1].s = NULL;
1404 kfree(ncm);
1405 }
1406 return status;
1407}
diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c
index 3c6e1a058745..5e1495097ec3 100644
--- a/drivers/usb/gadget/f_phonet.c
+++ b/drivers/usb/gadget/f_phonet.c
@@ -346,14 +346,19 @@ static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
346 346
347 if (unlikely(!skb)) 347 if (unlikely(!skb))
348 break; 348 break;
349 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 0,
350 req->actual);
351 page = NULL;
352 349
353 if (req->actual < req->length) { /* Last fragment */ 350 if (skb->len == 0) { /* First fragment */
354 skb->protocol = htons(ETH_P_PHONET); 351 skb->protocol = htons(ETH_P_PHONET);
355 skb_reset_mac_header(skb); 352 skb_reset_mac_header(skb);
356 pskb_pull(skb, 1); 353 /* Can't use pskb_pull() on page in IRQ */
354 memcpy(skb_put(skb, 1), page_address(page), 1);
355 }
356
357 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
358 skb->len == 0, req->actual);
359 page = NULL;
360
361 if (req->actual < req->length) { /* Last fragment */
357 skb->dev = dev; 362 skb->dev = dev;
358 dev->stats.rx_packets++; 363 dev->stats.rx_packets++;
359 dev->stats.rx_bytes += skb->len; 364 dev->stats.rx_bytes += skb->len;
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index 882484a40398..fa12ec8364ef 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -420,8 +420,7 @@ rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
420 */ 420 */
421 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 421 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
422 | USB_CDC_SEND_ENCAPSULATED_COMMAND: 422 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
423 if (w_length > req->length || w_value 423 if (w_value || w_index != rndis->ctrl_id)
424 || w_index != rndis->ctrl_id)
425 goto invalid; 424 goto invalid;
426 /* read the request; process it later */ 425 /* read the request; process it later */
427 value = w_length; 426 value = w_length;
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
index 685d768f336e..e403a534dd55 100644
--- a/drivers/usb/gadget/f_sourcesink.c
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -404,7 +404,7 @@ static void sourcesink_disable(struct usb_function *f)
404 404
405/*-------------------------------------------------------------------------*/ 405/*-------------------------------------------------------------------------*/
406 406
407static int __ref sourcesink_bind_config(struct usb_configuration *c) 407static int __init sourcesink_bind_config(struct usb_configuration *c)
408{ 408{
409 struct f_sourcesink *ss; 409 struct f_sourcesink *ss;
410 int status; 410 int status;
@@ -498,7 +498,6 @@ unknown:
498static struct usb_configuration sourcesink_driver = { 498static struct usb_configuration sourcesink_driver = {
499 .label = "source/sink", 499 .label = "source/sink",
500 .strings = sourcesink_strings, 500 .strings = sourcesink_strings,
501 .bind = sourcesink_bind_config,
502 .setup = sourcesink_setup, 501 .setup = sourcesink_setup,
503 .bConfigurationValue = 3, 502 .bConfigurationValue = 3,
504 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 503 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -532,5 +531,5 @@ int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
532 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 531 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
533 } 532 }
534 533
535 return usb_add_config(cdev, &sourcesink_driver); 534 return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
536} 535}
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index a857b7ac238c..0360f56221ea 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -89,6 +89,7 @@
89 * Required if "removable" is not set, names of 89 * Required if "removable" is not set, names of
90 * the files or block devices used for 90 * the files or block devices used for
91 * backing storage 91 * backing storage
92 * serial=HHHH... Required serial number (string of hex chars)
92 * ro=b[,b...] Default false, booleans for read-only access 93 * ro=b[,b...] Default false, booleans for read-only access
93 * removable Default false, boolean for removable media 94 * removable Default false, boolean for removable media
94 * luns=N Default N = number of filenames, number of 95 * luns=N Default N = number of filenames, number of
@@ -108,12 +109,11 @@
108 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID 109 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
109 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID 110 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
110 * release=0xRRRR Override the USB release number (bcdDevice) 111 * release=0xRRRR Override the USB release number (bcdDevice)
111 * serial=HHHH... Override serial number (string of hex chars)
112 * buflen=N Default N=16384, buffer size used (will be 112 * buflen=N Default N=16384, buffer size used (will be
113 * rounded down to a multiple of 113 * rounded down to a multiple of
114 * PAGE_CACHE_SIZE) 114 * PAGE_CACHE_SIZE)
115 * 115 *
116 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "ro", 116 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
117 * "removable", "luns", "nofua", "stall", and "cdrom" options are available; 117 * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
118 * default values are used for everything else. 118 * default values are used for everything else.
119 * 119 *
@@ -273,13 +273,10 @@
273 273
274#define DRIVER_DESC "File-backed Storage Gadget" 274#define DRIVER_DESC "File-backed Storage Gadget"
275#define DRIVER_NAME "g_file_storage" 275#define DRIVER_NAME "g_file_storage"
276/* DRIVER_VERSION must be at least 6 characters long, as it is used 276#define DRIVER_VERSION "1 September 2010"
277 * to generate a fallback serial number. */
278#define DRIVER_VERSION "20 November 2008"
279 277
280static char fsg_string_manufacturer[64]; 278static char fsg_string_manufacturer[64];
281static const char fsg_string_product[] = DRIVER_DESC; 279static const char fsg_string_product[] = DRIVER_DESC;
282static char fsg_string_serial[13];
283static const char fsg_string_config[] = "Self-powered"; 280static const char fsg_string_config[] = "Self-powered";
284static const char fsg_string_interface[] = "Mass Storage"; 281static const char fsg_string_interface[] = "Mass Storage";
285 282
@@ -305,6 +302,7 @@ MODULE_LICENSE("Dual BSD/GPL");
305 302
306static struct { 303static struct {
307 char *file[FSG_MAX_LUNS]; 304 char *file[FSG_MAX_LUNS];
305 char *serial;
308 int ro[FSG_MAX_LUNS]; 306 int ro[FSG_MAX_LUNS];
309 int nofua[FSG_MAX_LUNS]; 307 int nofua[FSG_MAX_LUNS];
310 unsigned int num_filenames; 308 unsigned int num_filenames;
@@ -321,7 +319,6 @@ static struct {
321 unsigned short vendor; 319 unsigned short vendor;
322 unsigned short product; 320 unsigned short product;
323 unsigned short release; 321 unsigned short release;
324 char *serial;
325 unsigned int buflen; 322 unsigned int buflen;
326 323
327 int transport_type; 324 int transport_type;
@@ -346,6 +343,9 @@ module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
346 S_IRUGO); 343 S_IRUGO);
347MODULE_PARM_DESC(file, "names of backing files or devices"); 344MODULE_PARM_DESC(file, "names of backing files or devices");
348 345
346module_param_named(serial, mod_data.serial, charp, S_IRUGO);
347MODULE_PARM_DESC(serial, "USB serial number");
348
349module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO); 349module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
350MODULE_PARM_DESC(ro, "true to force read-only"); 350MODULE_PARM_DESC(ro, "true to force read-only");
351 351
@@ -365,9 +365,6 @@ MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
365module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO); 365module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
366MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk"); 366MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
367 367
368module_param_named(serial, mod_data.serial, charp, S_IRUGO);
369MODULE_PARM_DESC(serial, "USB serial number");
370
371/* In the non-TEST version, only the module parameters listed above 368/* In the non-TEST version, only the module parameters listed above
372 * are available. */ 369 * are available. */
373#ifdef CONFIG_USB_FILE_STORAGE_TEST 370#ifdef CONFIG_USB_FILE_STORAGE_TEST
@@ -786,7 +783,7 @@ static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
786{ 783{
787 struct usb_request *req = fsg->ep0req; 784 struct usb_request *req = fsg->ep0req;
788 static u8 cbi_reset_cmnd[6] = { 785 static u8 cbi_reset_cmnd[6] = {
789 SC_SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff}; 786 SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
790 787
791 /* Error in command transfer? */ 788 /* Error in command transfer? */
792 if (req->status || req->length != req->actual || 789 if (req->status || req->length != req->actual ||
@@ -1138,7 +1135,7 @@ static int do_read(struct fsg_dev *fsg)
1138 1135
1139 /* Get the starting Logical Block Address and check that it's 1136 /* Get the starting Logical Block Address and check that it's
1140 * not too big */ 1137 * not too big */
1141 if (fsg->cmnd[0] == SC_READ_6) 1138 if (fsg->cmnd[0] == READ_6)
1142 lba = get_unaligned_be24(&fsg->cmnd[1]); 1139 lba = get_unaligned_be24(&fsg->cmnd[1]);
1143 else { 1140 else {
1144 lba = get_unaligned_be32(&fsg->cmnd[2]); 1141 lba = get_unaligned_be32(&fsg->cmnd[2]);
@@ -1273,7 +1270,7 @@ static int do_write(struct fsg_dev *fsg)
1273 1270
1274 /* Get the starting Logical Block Address and check that it's 1271 /* Get the starting Logical Block Address and check that it's
1275 * not too big */ 1272 * not too big */
1276 if (fsg->cmnd[0] == SC_WRITE_6) 1273 if (fsg->cmnd[0] == WRITE_6)
1277 lba = get_unaligned_be24(&fsg->cmnd[1]); 1274 lba = get_unaligned_be24(&fsg->cmnd[1]);
1278 else { 1275 else {
1279 lba = get_unaligned_be32(&fsg->cmnd[2]); 1276 lba = get_unaligned_be32(&fsg->cmnd[2]);
@@ -1581,7 +1578,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1581 } 1578 }
1582 1579
1583 memset(buf, 0, 8); 1580 memset(buf, 0, 8);
1584 buf[0] = (mod_data.cdrom ? TYPE_CDROM : TYPE_DISK); 1581 buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
1585 if (mod_data.removable) 1582 if (mod_data.removable)
1586 buf[1] = 0x80; 1583 buf[1] = 0x80;
1587 buf[2] = 2; // ANSI SCSI level 2 1584 buf[2] = 2; // ANSI SCSI level 2
@@ -1750,11 +1747,11 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1750 * The only variable value is the WriteProtect bit. We will fill in 1747 * The only variable value is the WriteProtect bit. We will fill in
1751 * the mode data length later. */ 1748 * the mode data length later. */
1752 memset(buf, 0, 8); 1749 memset(buf, 0, 8);
1753 if (mscmnd == SC_MODE_SENSE_6) { 1750 if (mscmnd == MODE_SENSE) {
1754 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA 1751 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1755 buf += 4; 1752 buf += 4;
1756 limit = 255; 1753 limit = 255;
1757 } else { // SC_MODE_SENSE_10 1754 } else { // MODE_SENSE_10
1758 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA 1755 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1759 buf += 8; 1756 buf += 8;
1760 limit = 65535; // Should really be mod_data.buflen 1757 limit = 65535; // Should really be mod_data.buflen
@@ -1794,7 +1791,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1794 } 1791 }
1795 1792
1796 /* Store the mode data length */ 1793 /* Store the mode data length */
1797 if (mscmnd == SC_MODE_SENSE_6) 1794 if (mscmnd == MODE_SENSE)
1798 buf0[0] = len - 1; 1795 buf0[0] = len - 1;
1799 else 1796 else
1800 put_unaligned_be16(len - 2, buf0); 1797 put_unaligned_be16(len - 2, buf0);
@@ -1950,37 +1947,6 @@ static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1950 return rc; 1947 return rc;
1951} 1948}
1952 1949
1953static int pad_with_zeros(struct fsg_dev *fsg)
1954{
1955 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1956 u32 nkeep = bh->inreq->length;
1957 u32 nsend;
1958 int rc;
1959
1960 bh->state = BUF_STATE_EMPTY; // For the first iteration
1961 fsg->usb_amount_left = nkeep + fsg->residue;
1962 while (fsg->usb_amount_left > 0) {
1963
1964 /* Wait for the next buffer to be free */
1965 while (bh->state != BUF_STATE_EMPTY) {
1966 rc = sleep_thread(fsg);
1967 if (rc)
1968 return rc;
1969 }
1970
1971 nsend = min(fsg->usb_amount_left, (u32) mod_data.buflen);
1972 memset(bh->buf + nkeep, 0, nsend - nkeep);
1973 bh->inreq->length = nsend;
1974 bh->inreq->zero = 0;
1975 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1976 &bh->inreq_busy, &bh->state);
1977 bh = fsg->next_buffhd_to_fill = bh->next;
1978 fsg->usb_amount_left -= nsend;
1979 nkeep = 0;
1980 }
1981 return 0;
1982}
1983
1984static int throw_away_data(struct fsg_dev *fsg) 1950static int throw_away_data(struct fsg_dev *fsg)
1985{ 1951{
1986 struct fsg_buffhd *bh; 1952 struct fsg_buffhd *bh;
@@ -2085,18 +2051,20 @@ static int finish_reply(struct fsg_dev *fsg)
2085 } 2051 }
2086 } 2052 }
2087 2053
2088 /* For Bulk-only, if we're allowed to stall then send the 2054 /*
2089 * short packet and halt the bulk-in endpoint. If we can't 2055 * For Bulk-only, mark the end of the data with a short
2090 * stall, pad out the remaining data with 0's. */ 2056 * packet. If we are allowed to stall, halt the bulk-in
2057 * endpoint. (Note: This violates the Bulk-Only Transport
2058 * specification, which requires us to pad the data if we
2059 * don't halt the endpoint. Presumably nobody will mind.)
2060 */
2091 else { 2061 else {
2092 if (mod_data.can_stall) { 2062 bh->inreq->zero = 1;
2093 bh->inreq->zero = 1; 2063 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2094 start_transfer(fsg, fsg->bulk_in, bh->inreq, 2064 &bh->inreq_busy, &bh->state);
2095 &bh->inreq_busy, &bh->state); 2065 fsg->next_buffhd_to_fill = bh->next;
2096 fsg->next_buffhd_to_fill = bh->next; 2066 if (mod_data.can_stall)
2097 rc = halt_bulk_in_endpoint(fsg); 2067 rc = halt_bulk_in_endpoint(fsg);
2098 } else
2099 rc = pad_with_zeros(fsg);
2100 } 2068 }
2101 break; 2069 break;
2102 2070
@@ -2317,9 +2285,9 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2317 fsg->lun = lun; // Use LUN from the command 2285 fsg->lun = lun; // Use LUN from the command
2318 2286
2319 /* Check the LUN */ 2287 /* Check the LUN */
2320 if (fsg->lun >= 0 && fsg->lun < fsg->nluns) { 2288 if (fsg->lun < fsg->nluns) {
2321 fsg->curlun = curlun = &fsg->luns[fsg->lun]; 2289 fsg->curlun = curlun = &fsg->luns[fsg->lun];
2322 if (fsg->cmnd[0] != SC_REQUEST_SENSE) { 2290 if (fsg->cmnd[0] != REQUEST_SENSE) {
2323 curlun->sense_data = SS_NO_SENSE; 2291 curlun->sense_data = SS_NO_SENSE;
2324 curlun->sense_data_info = 0; 2292 curlun->sense_data_info = 0;
2325 curlun->info_valid = 0; 2293 curlun->info_valid = 0;
@@ -2330,8 +2298,8 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2330 2298
2331 /* INQUIRY and REQUEST SENSE commands are explicitly allowed 2299 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2332 * to use unsupported LUNs; all others may not. */ 2300 * to use unsupported LUNs; all others may not. */
2333 if (fsg->cmnd[0] != SC_INQUIRY && 2301 if (fsg->cmnd[0] != INQUIRY &&
2334 fsg->cmnd[0] != SC_REQUEST_SENSE) { 2302 fsg->cmnd[0] != REQUEST_SENSE) {
2335 DBG(fsg, "unsupported LUN %d\n", fsg->lun); 2303 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2336 return -EINVAL; 2304 return -EINVAL;
2337 } 2305 }
@@ -2340,8 +2308,8 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2340 /* If a unit attention condition exists, only INQUIRY and 2308 /* If a unit attention condition exists, only INQUIRY and
2341 * REQUEST SENSE commands are allowed; anything else must fail. */ 2309 * REQUEST SENSE commands are allowed; anything else must fail. */
2342 if (curlun && curlun->unit_attention_data != SS_NO_SENSE && 2310 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
2343 fsg->cmnd[0] != SC_INQUIRY && 2311 fsg->cmnd[0] != INQUIRY &&
2344 fsg->cmnd[0] != SC_REQUEST_SENSE) { 2312 fsg->cmnd[0] != REQUEST_SENSE) {
2345 curlun->sense_data = curlun->unit_attention_data; 2313 curlun->sense_data = curlun->unit_attention_data;
2346 curlun->unit_attention_data = SS_NO_SENSE; 2314 curlun->unit_attention_data = SS_NO_SENSE;
2347 return -EINVAL; 2315 return -EINVAL;
@@ -2391,7 +2359,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2391 down_read(&fsg->filesem); // We're using the backing file 2359 down_read(&fsg->filesem); // We're using the backing file
2392 switch (fsg->cmnd[0]) { 2360 switch (fsg->cmnd[0]) {
2393 2361
2394 case SC_INQUIRY: 2362 case INQUIRY:
2395 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2363 fsg->data_size_from_cmnd = fsg->cmnd[4];
2396 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2364 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2397 (1<<4), 0, 2365 (1<<4), 0,
@@ -2399,7 +2367,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2399 reply = do_inquiry(fsg, bh); 2367 reply = do_inquiry(fsg, bh);
2400 break; 2368 break;
2401 2369
2402 case SC_MODE_SELECT_6: 2370 case MODE_SELECT:
2403 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2371 fsg->data_size_from_cmnd = fsg->cmnd[4];
2404 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2372 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2405 (1<<1) | (1<<4), 0, 2373 (1<<1) | (1<<4), 0,
@@ -2407,7 +2375,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2407 reply = do_mode_select(fsg, bh); 2375 reply = do_mode_select(fsg, bh);
2408 break; 2376 break;
2409 2377
2410 case SC_MODE_SELECT_10: 2378 case MODE_SELECT_10:
2411 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2379 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2412 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2380 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2413 (1<<1) | (3<<7), 0, 2381 (1<<1) | (3<<7), 0,
@@ -2415,7 +2383,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2415 reply = do_mode_select(fsg, bh); 2383 reply = do_mode_select(fsg, bh);
2416 break; 2384 break;
2417 2385
2418 case SC_MODE_SENSE_6: 2386 case MODE_SENSE:
2419 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2387 fsg->data_size_from_cmnd = fsg->cmnd[4];
2420 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2388 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2421 (1<<1) | (1<<2) | (1<<4), 0, 2389 (1<<1) | (1<<2) | (1<<4), 0,
@@ -2423,7 +2391,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2423 reply = do_mode_sense(fsg, bh); 2391 reply = do_mode_sense(fsg, bh);
2424 break; 2392 break;
2425 2393
2426 case SC_MODE_SENSE_10: 2394 case MODE_SENSE_10:
2427 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2395 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2428 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2396 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2429 (1<<1) | (1<<2) | (3<<7), 0, 2397 (1<<1) | (1<<2) | (3<<7), 0,
@@ -2431,7 +2399,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2431 reply = do_mode_sense(fsg, bh); 2399 reply = do_mode_sense(fsg, bh);
2432 break; 2400 break;
2433 2401
2434 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL: 2402 case ALLOW_MEDIUM_REMOVAL:
2435 fsg->data_size_from_cmnd = 0; 2403 fsg->data_size_from_cmnd = 0;
2436 if ((reply = check_command(fsg, 6, DATA_DIR_NONE, 2404 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2437 (1<<4), 0, 2405 (1<<4), 0,
@@ -2439,7 +2407,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2439 reply = do_prevent_allow(fsg); 2407 reply = do_prevent_allow(fsg);
2440 break; 2408 break;
2441 2409
2442 case SC_READ_6: 2410 case READ_6:
2443 i = fsg->cmnd[4]; 2411 i = fsg->cmnd[4];
2444 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2412 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2445 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2413 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
@@ -2448,7 +2416,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2448 reply = do_read(fsg); 2416 reply = do_read(fsg);
2449 break; 2417 break;
2450 2418
2451 case SC_READ_10: 2419 case READ_10:
2452 fsg->data_size_from_cmnd = 2420 fsg->data_size_from_cmnd =
2453 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2421 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2454 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2422 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
@@ -2457,7 +2425,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2457 reply = do_read(fsg); 2425 reply = do_read(fsg);
2458 break; 2426 break;
2459 2427
2460 case SC_READ_12: 2428 case READ_12:
2461 fsg->data_size_from_cmnd = 2429 fsg->data_size_from_cmnd =
2462 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2430 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2463 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 2431 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
@@ -2466,7 +2434,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2466 reply = do_read(fsg); 2434 reply = do_read(fsg);
2467 break; 2435 break;
2468 2436
2469 case SC_READ_CAPACITY: 2437 case READ_CAPACITY:
2470 fsg->data_size_from_cmnd = 8; 2438 fsg->data_size_from_cmnd = 8;
2471 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2439 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2472 (0xf<<2) | (1<<8), 1, 2440 (0xf<<2) | (1<<8), 1,
@@ -2474,7 +2442,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2474 reply = do_read_capacity(fsg, bh); 2442 reply = do_read_capacity(fsg, bh);
2475 break; 2443 break;
2476 2444
2477 case SC_READ_HEADER: 2445 case READ_HEADER:
2478 if (!mod_data.cdrom) 2446 if (!mod_data.cdrom)
2479 goto unknown_cmnd; 2447 goto unknown_cmnd;
2480 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2448 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
@@ -2484,7 +2452,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2484 reply = do_read_header(fsg, bh); 2452 reply = do_read_header(fsg, bh);
2485 break; 2453 break;
2486 2454
2487 case SC_READ_TOC: 2455 case READ_TOC:
2488 if (!mod_data.cdrom) 2456 if (!mod_data.cdrom)
2489 goto unknown_cmnd; 2457 goto unknown_cmnd;
2490 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2458 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
@@ -2494,7 +2462,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2494 reply = do_read_toc(fsg, bh); 2462 reply = do_read_toc(fsg, bh);
2495 break; 2463 break;
2496 2464
2497 case SC_READ_FORMAT_CAPACITIES: 2465 case READ_FORMAT_CAPACITIES:
2498 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2466 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2499 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2467 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2500 (3<<7), 1, 2468 (3<<7), 1,
@@ -2502,7 +2470,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2502 reply = do_read_format_capacities(fsg, bh); 2470 reply = do_read_format_capacities(fsg, bh);
2503 break; 2471 break;
2504 2472
2505 case SC_REQUEST_SENSE: 2473 case REQUEST_SENSE:
2506 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2474 fsg->data_size_from_cmnd = fsg->cmnd[4];
2507 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2475 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2508 (1<<4), 0, 2476 (1<<4), 0,
@@ -2510,7 +2478,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2510 reply = do_request_sense(fsg, bh); 2478 reply = do_request_sense(fsg, bh);
2511 break; 2479 break;
2512 2480
2513 case SC_START_STOP_UNIT: 2481 case START_STOP:
2514 fsg->data_size_from_cmnd = 0; 2482 fsg->data_size_from_cmnd = 0;
2515 if ((reply = check_command(fsg, 6, DATA_DIR_NONE, 2483 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2516 (1<<1) | (1<<4), 0, 2484 (1<<1) | (1<<4), 0,
@@ -2518,7 +2486,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2518 reply = do_start_stop(fsg); 2486 reply = do_start_stop(fsg);
2519 break; 2487 break;
2520 2488
2521 case SC_SYNCHRONIZE_CACHE: 2489 case SYNCHRONIZE_CACHE:
2522 fsg->data_size_from_cmnd = 0; 2490 fsg->data_size_from_cmnd = 0;
2523 if ((reply = check_command(fsg, 10, DATA_DIR_NONE, 2491 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2524 (0xf<<2) | (3<<7), 1, 2492 (0xf<<2) | (3<<7), 1,
@@ -2526,7 +2494,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2526 reply = do_synchronize_cache(fsg); 2494 reply = do_synchronize_cache(fsg);
2527 break; 2495 break;
2528 2496
2529 case SC_TEST_UNIT_READY: 2497 case TEST_UNIT_READY:
2530 fsg->data_size_from_cmnd = 0; 2498 fsg->data_size_from_cmnd = 0;
2531 reply = check_command(fsg, 6, DATA_DIR_NONE, 2499 reply = check_command(fsg, 6, DATA_DIR_NONE,
2532 0, 1, 2500 0, 1,
@@ -2535,7 +2503,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2535 2503
2536 /* Although optional, this command is used by MS-Windows. We 2504 /* Although optional, this command is used by MS-Windows. We
2537 * support a minimal version: BytChk must be 0. */ 2505 * support a minimal version: BytChk must be 0. */
2538 case SC_VERIFY: 2506 case VERIFY:
2539 fsg->data_size_from_cmnd = 0; 2507 fsg->data_size_from_cmnd = 0;
2540 if ((reply = check_command(fsg, 10, DATA_DIR_NONE, 2508 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2541 (1<<1) | (0xf<<2) | (3<<7), 1, 2509 (1<<1) | (0xf<<2) | (3<<7), 1,
@@ -2543,7 +2511,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2543 reply = do_verify(fsg); 2511 reply = do_verify(fsg);
2544 break; 2512 break;
2545 2513
2546 case SC_WRITE_6: 2514 case WRITE_6:
2547 i = fsg->cmnd[4]; 2515 i = fsg->cmnd[4];
2548 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2516 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2549 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2517 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
@@ -2552,7 +2520,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2552 reply = do_write(fsg); 2520 reply = do_write(fsg);
2553 break; 2521 break;
2554 2522
2555 case SC_WRITE_10: 2523 case WRITE_10:
2556 fsg->data_size_from_cmnd = 2524 fsg->data_size_from_cmnd =
2557 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2525 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2558 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2526 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
@@ -2561,7 +2529,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2561 reply = do_write(fsg); 2529 reply = do_write(fsg);
2562 break; 2530 break;
2563 2531
2564 case SC_WRITE_12: 2532 case WRITE_12:
2565 fsg->data_size_from_cmnd = 2533 fsg->data_size_from_cmnd =
2566 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2534 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2567 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 2535 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
@@ -2574,10 +2542,10 @@ static int do_scsi_command(struct fsg_dev *fsg)
2574 * They don't mean much in this setting. It's left as an exercise 2542 * They don't mean much in this setting. It's left as an exercise
2575 * for anyone interested to implement RESERVE and RELEASE in terms 2543 * for anyone interested to implement RESERVE and RELEASE in terms
2576 * of Posix locks. */ 2544 * of Posix locks. */
2577 case SC_FORMAT_UNIT: 2545 case FORMAT_UNIT:
2578 case SC_RELEASE: 2546 case RELEASE:
2579 case SC_RESERVE: 2547 case RESERVE:
2580 case SC_SEND_DIAGNOSTIC: 2548 case SEND_DIAGNOSTIC:
2581 // Fall through 2549 // Fall through
2582 2550
2583 default: 2551 default:
@@ -3178,6 +3146,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3178 for (i = 0; i < fsg->nluns; ++i) { 3146 for (i = 0; i < fsg->nluns; ++i) {
3179 curlun = &fsg->luns[i]; 3147 curlun = &fsg->luns[i];
3180 if (curlun->registered) { 3148 if (curlun->registered) {
3149 device_remove_file(&curlun->dev, &dev_attr_nofua);
3181 device_remove_file(&curlun->dev, &dev_attr_ro); 3150 device_remove_file(&curlun->dev, &dev_attr_ro);
3182 device_remove_file(&curlun->dev, &dev_attr_file); 3151 device_remove_file(&curlun->dev, &dev_attr_file);
3183 fsg_lun_close(curlun); 3152 fsg_lun_close(curlun);
@@ -3213,7 +3182,6 @@ static int __init check_parameters(struct fsg_dev *fsg)
3213{ 3182{
3214 int prot; 3183 int prot;
3215 int gcnum; 3184 int gcnum;
3216 int i;
3217 3185
3218 /* Store the default values */ 3186 /* Store the default values */
3219 mod_data.transport_type = USB_PR_BULK; 3187 mod_data.transport_type = USB_PR_BULK;
@@ -3309,45 +3277,29 @@ static int __init check_parameters(struct fsg_dev *fsg)
3309 if ((*ch < '0' || *ch > '9') && 3277 if ((*ch < '0' || *ch > '9') &&
3310 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */ 3278 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3311 WARNING(fsg, 3279 WARNING(fsg,
3312 "Invalid serial string character: %c; " 3280 "Invalid serial string character: %c\n",
3313 "Failing back to default\n",
3314 *ch); 3281 *ch);
3315 goto fill_serial; 3282 goto no_serial;
3316 } 3283 }
3317 } 3284 }
3318 if (len > 126 || 3285 if (len > 126 ||
3319 (mod_data.transport_type == USB_PR_BULK && len < 12) || 3286 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3320 (mod_data.transport_type != USB_PR_BULK && len > 12)) { 3287 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
3321 WARNING(fsg, 3288 WARNING(fsg, "Invalid serial string length!\n");
3322 "Invalid serial string length; " 3289 goto no_serial;
3323 "Failing back to default\n");
3324 goto fill_serial;
3325 } 3290 }
3326 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial; 3291 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
3327 } else { 3292 } else {
3328 WARNING(fsg, 3293 WARNING(fsg, "No serial-number string provided!\n");
3329 "Userspace failed to provide serial number; " 3294 no_serial:
3330 "Failing back to default\n"); 3295 device_desc.iSerialNumber = 0;
3331fill_serial:
3332 /* Serial number not specified or invalid, make our own.
3333 * We just encode it from the driver version string,
3334 * 12 characters to comply with both CB[I] and BBB spec.
3335 * Warning : Two devices running the same kernel will have
3336 * the same fallback serial number. */
3337 for (i = 0; i < 12; i += 2) {
3338 unsigned char c = DRIVER_VERSION[i / 2];
3339
3340 if (!c)
3341 break;
3342 sprintf(&fsg_string_serial[i], "%02X", c);
3343 }
3344 } 3296 }
3345 3297
3346 return 0; 3298 return 0;
3347} 3299}
3348 3300
3349 3301
3350static int __ref fsg_bind(struct usb_gadget *gadget) 3302static int __init fsg_bind(struct usb_gadget *gadget)
3351{ 3303{
3352 struct fsg_dev *fsg = the_fsg; 3304 struct fsg_dev *fsg = the_fsg;
3353 int rc; 3305 int rc;
@@ -3411,25 +3363,28 @@ static int __ref fsg_bind(struct usb_gadget *gadget)
3411 dev_set_name(&curlun->dev,"%s-lun%d", 3363 dev_set_name(&curlun->dev,"%s-lun%d",
3412 dev_name(&gadget->dev), i); 3364 dev_name(&gadget->dev), i);
3413 3365
3414 if ((rc = device_register(&curlun->dev)) != 0) { 3366 kref_get(&fsg->ref);
3367 rc = device_register(&curlun->dev);
3368 if (rc) {
3415 INFO(fsg, "failed to register LUN%d: %d\n", i, rc); 3369 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
3416 goto out; 3370 put_device(&curlun->dev);
3417 }
3418 if ((rc = device_create_file(&curlun->dev,
3419 &dev_attr_ro)) != 0 ||
3420 (rc = device_create_file(&curlun->dev,
3421 &dev_attr_nofua)) != 0 ||
3422 (rc = device_create_file(&curlun->dev,
3423 &dev_attr_file)) != 0) {
3424 device_unregister(&curlun->dev);
3425 goto out; 3371 goto out;
3426 } 3372 }
3427 curlun->registered = 1; 3373 curlun->registered = 1;
3428 kref_get(&fsg->ref); 3374
3375 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3376 if (rc)
3377 goto out;
3378 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3379 if (rc)
3380 goto out;
3381 rc = device_create_file(&curlun->dev, &dev_attr_file);
3382 if (rc)
3383 goto out;
3429 3384
3430 if (mod_data.file[i] && *mod_data.file[i]) { 3385 if (mod_data.file[i] && *mod_data.file[i]) {
3431 if ((rc = fsg_lun_open(curlun, 3386 rc = fsg_lun_open(curlun, mod_data.file[i]);
3432 mod_data.file[i])) != 0) 3387 if (rc)
3433 goto out; 3388 goto out;
3434 } else if (!mod_data.removable) { 3389 } else if (!mod_data.removable) {
3435 ERROR(fsg, "no file given for LUN%d\n", i); 3390 ERROR(fsg, "no file given for LUN%d\n", i);
@@ -3607,7 +3562,6 @@ static struct usb_gadget_driver fsg_driver = {
3607 .speed = USB_SPEED_FULL, 3562 .speed = USB_SPEED_FULL,
3608#endif 3563#endif
3609 .function = (char *) fsg_string_product, 3564 .function = (char *) fsg_string_product,
3610 .bind = fsg_bind,
3611 .unbind = fsg_unbind, 3565 .unbind = fsg_unbind,
3612 .disconnect = fsg_disconnect, 3566 .disconnect = fsg_disconnect,
3613 .setup = fsg_setup, 3567 .setup = fsg_setup,
@@ -3649,7 +3603,7 @@ static int __init fsg_init(void)
3649 if ((rc = fsg_alloc()) != 0) 3603 if ((rc = fsg_alloc()) != 0)
3650 return rc; 3604 return rc;
3651 fsg = the_fsg; 3605 fsg = the_fsg;
3652 if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0) 3606 if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
3653 kref_put(&fsg->ref, fsg_release); 3607 kref_put(&fsg->ref, fsg_release);
3654 return rc; 3608 return rc;
3655} 3609}
diff --git a/drivers/usb/gadget/fsl_mxc_udc.c b/drivers/usb/gadget/fsl_mxc_udc.c
index eafa6d2c5ed7..43a49ecc1f36 100644
--- a/drivers/usb/gadget/fsl_mxc_udc.c
+++ b/drivers/usb/gadget/fsl_mxc_udc.c
@@ -22,6 +22,10 @@
22static struct clk *mxc_ahb_clk; 22static struct clk *mxc_ahb_clk;
23static struct clk *mxc_usb_clk; 23static struct clk *mxc_usb_clk;
24 24
25/* workaround ENGcm09152 for i.MX35 */
26#define USBPHYCTRL_OTGBASE_OFFSET 0x608
27#define USBPHYCTRL_EVDO (1 << 23)
28
25int fsl_udc_clk_init(struct platform_device *pdev) 29int fsl_udc_clk_init(struct platform_device *pdev)
26{ 30{
27 struct fsl_usb2_platform_data *pdata; 31 struct fsl_usb2_platform_data *pdata;
@@ -84,6 +88,20 @@ eenahb:
84void fsl_udc_clk_finalize(struct platform_device *pdev) 88void fsl_udc_clk_finalize(struct platform_device *pdev)
85{ 89{
86 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data; 90 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
91#if defined(CONFIG_SOC_IMX35)
92 if (cpu_is_mx35()) {
93 unsigned int v;
94
95 /* workaround ENGcm09152 for i.MX35 */
96 if (pdata->workaround & FLS_USB2_WORKAROUND_ENGCM09152) {
97 v = readl(MX35_IO_ADDRESS(MX35_USB_BASE_ADDR +
98 USBPHYCTRL_OTGBASE_OFFSET));
99 writel(v | USBPHYCTRL_EVDO,
100 MX35_IO_ADDRESS(MX35_USB_BASE_ADDR +
101 USBPHYCTRL_OTGBASE_OFFSET));
102 }
103 }
104#endif
87 105
88 /* ULPI transceivers don't need usbpll */ 106 /* ULPI transceivers don't need usbpll */
89 if (pdata->phy_mode == FSL_USB2_PHY_ULPI) { 107 if (pdata->phy_mode == FSL_USB2_PHY_ULPI) {
diff --git a/drivers/usb/gadget/fsl_qe_udc.c b/drivers/usb/gadget/fsl_qe_udc.c
index a5ea2c1d8c93..3a68e09309f7 100644
--- a/drivers/usb/gadget/fsl_qe_udc.c
+++ b/drivers/usb/gadget/fsl_qe_udc.c
@@ -1148,6 +1148,12 @@ static int qe_ep_tx(struct qe_ep *ep, struct qe_frame *frame)
1148static int txcomplete(struct qe_ep *ep, unsigned char restart) 1148static int txcomplete(struct qe_ep *ep, unsigned char restart)
1149{ 1149{
1150 if (ep->tx_req != NULL) { 1150 if (ep->tx_req != NULL) {
1151 struct qe_req *req = ep->tx_req;
1152 unsigned zlp = 0, last_len = 0;
1153
1154 last_len = min_t(unsigned, req->req.length - ep->sent,
1155 ep->ep.maxpacket);
1156
1151 if (!restart) { 1157 if (!restart) {
1152 int asent = ep->last; 1158 int asent = ep->last;
1153 ep->sent += asent; 1159 ep->sent += asent;
@@ -1156,9 +1162,18 @@ static int txcomplete(struct qe_ep *ep, unsigned char restart)
1156 ep->last = 0; 1162 ep->last = 0;
1157 } 1163 }
1158 1164
1165 /* zlp needed when req->re.zero is set */
1166 if (req->req.zero) {
1167 if (last_len == 0 ||
1168 (req->req.length % ep->ep.maxpacket) != 0)
1169 zlp = 0;
1170 else
1171 zlp = 1;
1172 } else
1173 zlp = 0;
1174
1159 /* a request already were transmitted completely */ 1175 /* a request already were transmitted completely */
1160 if ((ep->tx_req->req.length - ep->sent) <= 0) { 1176 if (((ep->tx_req->req.length - ep->sent) <= 0) && !zlp) {
1161 ep->tx_req->req.actual = (unsigned int)ep->sent;
1162 done(ep, ep->tx_req, 0); 1177 done(ep, ep->tx_req, 0);
1163 ep->tx_req = NULL; 1178 ep->tx_req = NULL;
1164 ep->last = 0; 1179 ep->last = 0;
@@ -1191,6 +1206,7 @@ static int qe_usb_senddata(struct qe_ep *ep, struct qe_frame *frame)
1191 buf = (u8 *)ep->tx_req->req.buf + ep->sent; 1206 buf = (u8 *)ep->tx_req->req.buf + ep->sent;
1192 if (buf && size) { 1207 if (buf && size) {
1193 ep->last = size; 1208 ep->last = size;
1209 ep->tx_req->req.actual += size;
1194 frame_set_data(frame, buf); 1210 frame_set_data(frame, buf);
1195 frame_set_length(frame, size); 1211 frame_set_length(frame, size);
1196 frame_set_status(frame, FRAME_OK); 1212 frame_set_status(frame, FRAME_OK);
@@ -2302,9 +2318,10 @@ static irqreturn_t qe_udc_irq(int irq, void *_udc)
2302} 2318}
2303 2319
2304/*------------------------------------------------------------------------- 2320/*-------------------------------------------------------------------------
2305 Gadget driver register and unregister. 2321 Gadget driver probe and unregister.
2306 --------------------------------------------------------------------------*/ 2322 --------------------------------------------------------------------------*/
2307int usb_gadget_register_driver(struct usb_gadget_driver *driver) 2323int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2324 int (*bind)(struct usb_gadget *))
2308{ 2325{
2309 int retval; 2326 int retval;
2310 unsigned long flags = 0; 2327 unsigned long flags = 0;
@@ -2315,8 +2332,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2315 2332
2316 if (!driver || (driver->speed != USB_SPEED_FULL 2333 if (!driver || (driver->speed != USB_SPEED_FULL
2317 && driver->speed != USB_SPEED_HIGH) 2334 && driver->speed != USB_SPEED_HIGH)
2318 || !driver->bind || !driver->disconnect 2335 || !bind || !driver->disconnect || !driver->setup)
2319 || !driver->setup)
2320 return -EINVAL; 2336 return -EINVAL;
2321 2337
2322 if (udc_controller->driver) 2338 if (udc_controller->driver)
@@ -2332,7 +2348,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2332 udc_controller->gadget.speed = (enum usb_device_speed)(driver->speed); 2348 udc_controller->gadget.speed = (enum usb_device_speed)(driver->speed);
2333 spin_unlock_irqrestore(&udc_controller->lock, flags); 2349 spin_unlock_irqrestore(&udc_controller->lock, flags);
2334 2350
2335 retval = driver->bind(&udc_controller->gadget); 2351 retval = bind(&udc_controller->gadget);
2336 if (retval) { 2352 if (retval) {
2337 dev_err(udc_controller->dev, "bind to %s --> %d", 2353 dev_err(udc_controller->dev, "bind to %s --> %d",
2338 driver->driver.name, retval); 2354 driver->driver.name, retval);
@@ -2353,7 +2369,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2353 udc_controller->gadget.name, driver->driver.name); 2369 udc_controller->gadget.name, driver->driver.name);
2354 return 0; 2370 return 0;
2355} 2371}
2356EXPORT_SYMBOL(usb_gadget_register_driver); 2372EXPORT_SYMBOL(usb_gadget_probe_driver);
2357 2373
2358int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 2374int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2359{ 2375{
@@ -2523,15 +2539,20 @@ static void qe_udc_release(struct device *dev)
2523} 2539}
2524 2540
2525/* Driver probe functions */ 2541/* Driver probe functions */
2526static int __devinit qe_udc_probe(struct platform_device *ofdev, 2542static const struct of_device_id qe_udc_match[];
2527 const struct of_device_id *match) 2543static int __devinit qe_udc_probe(struct platform_device *ofdev)
2528{ 2544{
2545 const struct of_device_id *match;
2529 struct device_node *np = ofdev->dev.of_node; 2546 struct device_node *np = ofdev->dev.of_node;
2530 struct qe_ep *ep; 2547 struct qe_ep *ep;
2531 unsigned int ret = 0; 2548 unsigned int ret = 0;
2532 unsigned int i; 2549 unsigned int i;
2533 const void *prop; 2550 const void *prop;
2534 2551
2552 match = of_match_device(qe_udc_match, &ofdev->dev);
2553 if (!match)
2554 return -EINVAL;
2555
2535 prop = of_get_property(np, "mode", NULL); 2556 prop = of_get_property(np, "mode", NULL);
2536 if (!prop || strcmp(prop, "peripheral")) 2557 if (!prop || strcmp(prop, "peripheral"))
2537 return -ENODEV; 2558 return -ENODEV;
@@ -2768,7 +2789,7 @@ static const struct of_device_id qe_udc_match[] __devinitconst = {
2768 2789
2769MODULE_DEVICE_TABLE(of, qe_udc_match); 2790MODULE_DEVICE_TABLE(of, qe_udc_match);
2770 2791
2771static struct of_platform_driver udc_driver = { 2792static struct platform_driver udc_driver = {
2772 .driver = { 2793 .driver = {
2773 .name = (char *)driver_name, 2794 .name = (char *)driver_name,
2774 .owner = THIS_MODULE, 2795 .owner = THIS_MODULE,
@@ -2786,12 +2807,12 @@ static int __init qe_udc_init(void)
2786{ 2807{
2787 printk(KERN_INFO "%s: %s, %s\n", driver_name, driver_desc, 2808 printk(KERN_INFO "%s: %s, %s\n", driver_name, driver_desc,
2788 DRIVER_VERSION); 2809 DRIVER_VERSION);
2789 return of_register_platform_driver(&udc_driver); 2810 return platform_driver_register(&udc_driver);
2790} 2811}
2791 2812
2792static void __exit qe_udc_exit(void) 2813static void __exit qe_udc_exit(void)
2793{ 2814{
2794 of_unregister_platform_driver(&udc_driver); 2815 platform_driver_unregister(&udc_driver);
2795} 2816}
2796 2817
2797module_init(qe_udc_init); 2818module_init(qe_udc_init);
diff --git a/drivers/usb/gadget/fsl_qe_udc.h b/drivers/usb/gadget/fsl_qe_udc.h
index bea5b827bebe..1da5fb03d218 100644
--- a/drivers/usb/gadget/fsl_qe_udc.h
+++ b/drivers/usb/gadget/fsl_qe_udc.h
@@ -207,15 +207,15 @@ struct qe_frame{
207 207
208/* Frame status field */ 208/* Frame status field */
209/* Receive side */ 209/* Receive side */
210#define FRAME_OK 0x00000000 /* Frame tranmitted or received OK */ 210#define FRAME_OK 0x00000000 /* Frame transmitted or received OK */
211#define FRAME_ERROR 0x80000000 /* Error occured on frame */ 211#define FRAME_ERROR 0x80000000 /* Error occurred on frame */
212#define START_FRAME_LOST 0x40000000 /* START_FRAME_LOST */ 212#define START_FRAME_LOST 0x40000000 /* START_FRAME_LOST */
213#define END_FRAME_LOST 0x20000000 /* END_FRAME_LOST */ 213#define END_FRAME_LOST 0x20000000 /* END_FRAME_LOST */
214#define RX_ER_NONOCT 0x10000000 /* Rx Non Octet Aligned Packet */ 214#define RX_ER_NONOCT 0x10000000 /* Rx Non Octet Aligned Packet */
215#define RX_ER_BITSTUFF 0x08000000 /* Frame Aborted --Received packet 215#define RX_ER_BITSTUFF 0x08000000 /* Frame Aborted --Received packet
216 with bit stuff error */ 216 with bit stuff error */
217#define RX_ER_CRC 0x04000000 /* Received packet with CRC error */ 217#define RX_ER_CRC 0x04000000 /* Received packet with CRC error */
218#define RX_ER_OVERUN 0x02000000 /* Over-run occured on reception */ 218#define RX_ER_OVERUN 0x02000000 /* Over-run occurred on reception */
219#define RX_ER_PID 0x01000000 /* Wrong PID received */ 219#define RX_ER_PID 0x01000000 /* Wrong PID received */
220/* Tranmit side */ 220/* Tranmit side */
221#define TX_ER_NAK 0x00800000 /* Received NAK handshake */ 221#define TX_ER_NAK 0x00800000 /* Received NAK handshake */
@@ -379,7 +379,7 @@ struct qe_udc {
379#define T_LSP 0x01000000 /* Low-speed transaction */ 379#define T_LSP 0x01000000 /* Low-speed transaction */
380#define T_PID 0x00c00000 /* packet id */ 380#define T_PID 0x00c00000 /* packet id */
381#define T_NAK 0x00100000 /* No ack. */ 381#define T_NAK 0x00100000 /* No ack. */
382#define T_STAL 0x00080000 /* Stall recieved */ 382#define T_STAL 0x00080000 /* Stall received */
383#define T_TO 0x00040000 /* time out */ 383#define T_TO 0x00040000 /* time out */
384#define T_UN 0x00020000 /* underrun */ 384#define T_UN 0x00020000 /* underrun */
385 385
diff --git a/drivers/usb/gadget/fsl_udc_core.c b/drivers/usb/gadget/fsl_udc_core.c
index 08a9a62a39e3..4e4833168087 100644
--- a/drivers/usb/gadget/fsl_udc_core.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -1,12 +1,13 @@
1/* 1/*
2 * Copyright (C) 2004-2007 Freescale Semicondutor, Inc. All rights reserved. 2 * Copyright (C) 2004-2007,2011 Freescale Semiconductor, Inc.
3 * All rights reserved.
3 * 4 *
4 * Author: Li Yang <leoli@freescale.com> 5 * Author: Li Yang <leoli@freescale.com>
5 * Jiang Bo <tanya.jiang@freescale.com> 6 * Jiang Bo <tanya.jiang@freescale.com>
6 * 7 *
7 * Description: 8 * Description:
8 * Freescale high-speed USB SOC DR module device controller driver. 9 * Freescale high-speed USB SOC DR module device controller driver.
9 * This can be found on MPC8349E/MPC8313E cpus. 10 * This can be found on MPC8349E/MPC8313E/MPC5121E cpus.
10 * The driver is previously named as mpc_udc. Based on bare board 11 * The driver is previously named as mpc_udc. Based on bare board
11 * code from Dave Liu and Shlomi Gridish. 12 * code from Dave Liu and Shlomi Gridish.
12 * 13 *
@@ -77,12 +78,77 @@ fsl_ep0_desc = {
77static void fsl_ep_fifo_flush(struct usb_ep *_ep); 78static void fsl_ep_fifo_flush(struct usb_ep *_ep);
78 79
79#ifdef CONFIG_PPC32 80#ifdef CONFIG_PPC32
80#define fsl_readl(addr) in_le32(addr) 81/*
81#define fsl_writel(val32, addr) out_le32(addr, val32) 82 * On some SoCs, the USB controller registers can be big or little endian,
82#else 83 * depending on the version of the chip. In order to be able to run the
84 * same kernel binary on 2 different versions of an SoC, the BE/LE decision
85 * must be made at run time. _fsl_readl and fsl_writel are pointers to the
86 * BE or LE readl() and writel() functions, and fsl_readl() and fsl_writel()
87 * call through those pointers. Platform code for SoCs that have BE USB
88 * registers should set pdata->big_endian_mmio flag.
89 *
90 * This also applies to controller-to-cpu accessors for the USB descriptors,
91 * since their endianness is also SoC dependant. Platform code for SoCs that
92 * have BE USB descriptors should set pdata->big_endian_desc flag.
93 */
94static u32 _fsl_readl_be(const unsigned __iomem *p)
95{
96 return in_be32(p);
97}
98
99static u32 _fsl_readl_le(const unsigned __iomem *p)
100{
101 return in_le32(p);
102}
103
104static void _fsl_writel_be(u32 v, unsigned __iomem *p)
105{
106 out_be32(p, v);
107}
108
109static void _fsl_writel_le(u32 v, unsigned __iomem *p)
110{
111 out_le32(p, v);
112}
113
114static u32 (*_fsl_readl)(const unsigned __iomem *p);
115static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
116
117#define fsl_readl(p) (*_fsl_readl)((p))
118#define fsl_writel(v, p) (*_fsl_writel)((v), (p))
119
120static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata)
121{
122 if (pdata->big_endian_mmio) {
123 _fsl_readl = _fsl_readl_be;
124 _fsl_writel = _fsl_writel_be;
125 } else {
126 _fsl_readl = _fsl_readl_le;
127 _fsl_writel = _fsl_writel_le;
128 }
129}
130
131static inline u32 cpu_to_hc32(const u32 x)
132{
133 return udc_controller->pdata->big_endian_desc
134 ? (__force u32)cpu_to_be32(x)
135 : (__force u32)cpu_to_le32(x);
136}
137
138static inline u32 hc32_to_cpu(const u32 x)
139{
140 return udc_controller->pdata->big_endian_desc
141 ? be32_to_cpu((__force __be32)x)
142 : le32_to_cpu((__force __le32)x);
143}
144#else /* !CONFIG_PPC32 */
145static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata) {}
146
83#define fsl_readl(addr) readl(addr) 147#define fsl_readl(addr) readl(addr)
84#define fsl_writel(val32, addr) writel(val32, addr) 148#define fsl_writel(val32, addr) writel(val32, addr)
85#endif 149#define cpu_to_hc32(x) cpu_to_le32(x)
150#define hc32_to_cpu(x) le32_to_cpu(x)
151#endif /* CONFIG_PPC32 */
86 152
87/******************************************************************** 153/********************************************************************
88 * Internal Used Function 154 * Internal Used Function
@@ -177,7 +243,8 @@ static void nuke(struct fsl_ep *ep, int status)
177 243
178static int dr_controller_setup(struct fsl_udc *udc) 244static int dr_controller_setup(struct fsl_udc *udc)
179{ 245{
180 unsigned int tmp, portctrl; 246 unsigned int tmp, portctrl, ep_num;
247 unsigned int max_no_of_ep;
181#ifndef CONFIG_ARCH_MXC 248#ifndef CONFIG_ARCH_MXC
182 unsigned int ctrl; 249 unsigned int ctrl;
183#endif 250#endif
@@ -226,9 +293,12 @@ static int dr_controller_setup(struct fsl_udc *udc)
226 293
227 /* Set the controller as device mode */ 294 /* Set the controller as device mode */
228 tmp = fsl_readl(&dr_regs->usbmode); 295 tmp = fsl_readl(&dr_regs->usbmode);
296 tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
229 tmp |= USB_MODE_CTRL_MODE_DEVICE; 297 tmp |= USB_MODE_CTRL_MODE_DEVICE;
230 /* Disable Setup Lockout */ 298 /* Disable Setup Lockout */
231 tmp |= USB_MODE_SETUP_LOCK_OFF; 299 tmp |= USB_MODE_SETUP_LOCK_OFF;
300 if (udc->pdata->es)
301 tmp |= USB_MODE_ES;
232 fsl_writel(tmp, &dr_regs->usbmode); 302 fsl_writel(tmp, &dr_regs->usbmode);
233 303
234 /* Clear the setup status */ 304 /* Clear the setup status */
@@ -242,22 +312,34 @@ static int dr_controller_setup(struct fsl_udc *udc)
242 udc->ep_qh, (int)tmp, 312 udc->ep_qh, (int)tmp,
243 fsl_readl(&dr_regs->endpointlistaddr)); 313 fsl_readl(&dr_regs->endpointlistaddr));
244 314
315 max_no_of_ep = (0x0000001F & fsl_readl(&dr_regs->dccparams));
316 for (ep_num = 1; ep_num < max_no_of_ep; ep_num++) {
317 tmp = fsl_readl(&dr_regs->endptctrl[ep_num]);
318 tmp &= ~(EPCTRL_TX_TYPE | EPCTRL_RX_TYPE);
319 tmp |= (EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT)
320 | (EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT);
321 fsl_writel(tmp, &dr_regs->endptctrl[ep_num]);
322 }
245 /* Config control enable i/o output, cpu endian register */ 323 /* Config control enable i/o output, cpu endian register */
246#ifndef CONFIG_ARCH_MXC 324#ifndef CONFIG_ARCH_MXC
247 ctrl = __raw_readl(&usb_sys_regs->control); 325 if (udc->pdata->have_sysif_regs) {
248 ctrl |= USB_CTRL_IOENB; 326 ctrl = __raw_readl(&usb_sys_regs->control);
249 __raw_writel(ctrl, &usb_sys_regs->control); 327 ctrl |= USB_CTRL_IOENB;
328 __raw_writel(ctrl, &usb_sys_regs->control);
329 }
250#endif 330#endif
251 331
252#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE) 332#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
253 /* Turn on cache snooping hardware, since some PowerPC platforms 333 /* Turn on cache snooping hardware, since some PowerPC platforms
254 * wholly rely on hardware to deal with cache coherent. */ 334 * wholly rely on hardware to deal with cache coherent. */
255 335
256 /* Setup Snooping for all the 4GB space */ 336 if (udc->pdata->have_sysif_regs) {
257 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */ 337 /* Setup Snooping for all the 4GB space */
258 __raw_writel(tmp, &usb_sys_regs->snoop1); 338 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
259 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */ 339 __raw_writel(tmp, &usb_sys_regs->snoop1);
260 __raw_writel(tmp, &usb_sys_regs->snoop2); 340 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
341 __raw_writel(tmp, &usb_sys_regs->snoop2);
342 }
261#endif 343#endif
262 344
263 return 0; 345 return 0;
@@ -287,14 +369,25 @@ static void dr_controller_run(struct fsl_udc *udc)
287 temp = fsl_readl(&dr_regs->usbcmd); 369 temp = fsl_readl(&dr_regs->usbcmd);
288 temp |= USB_CMD_RUN_STOP; 370 temp |= USB_CMD_RUN_STOP;
289 fsl_writel(temp, &dr_regs->usbcmd); 371 fsl_writel(temp, &dr_regs->usbcmd);
290
291 return;
292} 372}
293 373
294static void dr_controller_stop(struct fsl_udc *udc) 374static void dr_controller_stop(struct fsl_udc *udc)
295{ 375{
296 unsigned int tmp; 376 unsigned int tmp;
297 377
378 pr_debug("%s\n", __func__);
379
380 /* if we're in OTG mode, and the Host is currently using the port,
381 * stop now and don't rip the controller out from under the
382 * ehci driver
383 */
384 if (udc->gadget.is_otg) {
385 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
386 pr_debug("udc: Leaving early\n");
387 return;
388 }
389 }
390
298 /* disable all INTR */ 391 /* disable all INTR */
299 fsl_writel(0, &dr_regs->usbintr); 392 fsl_writel(0, &dr_regs->usbintr);
300 393
@@ -308,8 +401,6 @@ static void dr_controller_stop(struct fsl_udc *udc)
308 tmp = fsl_readl(&dr_regs->usbcmd); 401 tmp = fsl_readl(&dr_regs->usbcmd);
309 tmp &= ~USB_CMD_RUN_STOP; 402 tmp &= ~USB_CMD_RUN_STOP;
310 fsl_writel(tmp, &dr_regs->usbcmd); 403 fsl_writel(tmp, &dr_regs->usbcmd);
311
312 return;
313} 404}
314 405
315static void dr_ep_setup(unsigned char ep_num, unsigned char dir, 406static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
@@ -322,12 +413,14 @@ static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
322 if (ep_num) 413 if (ep_num)
323 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST; 414 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
324 tmp_epctrl |= EPCTRL_TX_ENABLE; 415 tmp_epctrl |= EPCTRL_TX_ENABLE;
416 tmp_epctrl &= ~EPCTRL_TX_TYPE;
325 tmp_epctrl |= ((unsigned int)(ep_type) 417 tmp_epctrl |= ((unsigned int)(ep_type)
326 << EPCTRL_TX_EP_TYPE_SHIFT); 418 << EPCTRL_TX_EP_TYPE_SHIFT);
327 } else { 419 } else {
328 if (ep_num) 420 if (ep_num)
329 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST; 421 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
330 tmp_epctrl |= EPCTRL_RX_ENABLE; 422 tmp_epctrl |= EPCTRL_RX_ENABLE;
423 tmp_epctrl &= ~EPCTRL_RX_TYPE;
331 tmp_epctrl |= ((unsigned int)(ep_type) 424 tmp_epctrl |= ((unsigned int)(ep_type)
332 << EPCTRL_RX_EP_TYPE_SHIFT); 425 << EPCTRL_RX_EP_TYPE_SHIFT);
333 } 426 }
@@ -413,11 +506,9 @@ static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
413 if (zlt) 506 if (zlt)
414 tmp |= EP_QUEUE_HEAD_ZLT_SEL; 507 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
415 508
416 p_QH->max_pkt_length = cpu_to_le32(tmp); 509 p_QH->max_pkt_length = cpu_to_hc32(tmp);
417 p_QH->next_dtd_ptr = 1; 510 p_QH->next_dtd_ptr = 1;
418 p_QH->size_ioc_int_sts = 0; 511 p_QH->size_ioc_int_sts = 0;
419
420 return;
421} 512}
422 513
423/* Setup qh structure and ep register for ep0. */ 514/* Setup qh structure and ep register for ep0. */
@@ -470,7 +561,7 @@ static int fsl_ep_enable(struct usb_ep *_ep,
470 561
471 max = le16_to_cpu(desc->wMaxPacketSize); 562 max = le16_to_cpu(desc->wMaxPacketSize);
472 563
473 /* Disable automatic zlp generation. Driver is reponsible to indicate 564 /* Disable automatic zlp generation. Driver is responsible to indicate
474 * 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
475 * request. */ 566 * request. */
476 zlt = 1; 567 zlt = 1;
@@ -552,10 +643,13 @@ static int fsl_ep_disable(struct usb_ep *_ep)
552 /* disable ep on controller */ 643 /* disable ep on controller */
553 ep_num = ep_index(ep); 644 ep_num = ep_index(ep);
554 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]); 645 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
555 if (ep_is_in(ep)) 646 if (ep_is_in(ep)) {
556 epctrl &= ~EPCTRL_TX_ENABLE; 647 epctrl &= ~(EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE);
557 else 648 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT;
558 epctrl &= ~EPCTRL_RX_ENABLE; 649 } else {
650 epctrl &= ~(EPCTRL_RX_ENABLE | EPCTRL_TX_TYPE);
651 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT;
652 }
559 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]); 653 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
560 654
561 udc = (struct fsl_udc *)ep->udc; 655 udc = (struct fsl_udc *)ep->udc;
@@ -622,7 +716,7 @@ static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
622 struct fsl_req *lastreq; 716 struct fsl_req *lastreq;
623 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue); 717 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
624 lastreq->tail->next_td_ptr = 718 lastreq->tail->next_td_ptr =
625 cpu_to_le32(req->head->td_dma & DTD_ADDR_MASK); 719 cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
626 /* Read prime bit, if 1 goto done */ 720 /* Read prime bit, if 1 goto done */
627 if (fsl_readl(&dr_regs->endpointprime) & bitmask) 721 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
628 goto out; 722 goto out;
@@ -647,14 +741,14 @@ static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
647 741
648 /* Write dQH next pointer and terminate bit to 0 */ 742 /* Write dQH next pointer and terminate bit to 0 */
649 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK; 743 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
650 dQH->next_dtd_ptr = cpu_to_le32(temp); 744 dQH->next_dtd_ptr = cpu_to_hc32(temp);
651 745
652 /* Clear active and halt bit */ 746 /* Clear active and halt bit */
653 temp = cpu_to_le32(~(EP_QUEUE_HEAD_STATUS_ACTIVE 747 temp = cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
654 | EP_QUEUE_HEAD_STATUS_HALT)); 748 | EP_QUEUE_HEAD_STATUS_HALT));
655 dQH->size_ioc_int_sts &= temp; 749 dQH->size_ioc_int_sts &= temp;
656 750
657 /* Ensure that updates to the QH will occure before priming. */ 751 /* Ensure that updates to the QH will occur before priming. */
658 wmb(); 752 wmb();
659 753
660 /* Prime endpoint by writing 1 to ENDPTPRIME */ 754 /* Prime endpoint by writing 1 to ENDPTPRIME */
@@ -688,17 +782,17 @@ static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
688 782
689 dtd->td_dma = *dma; 783 dtd->td_dma = *dma;
690 /* Clear reserved field */ 784 /* Clear reserved field */
691 swap_temp = cpu_to_le32(dtd->size_ioc_sts); 785 swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
692 swap_temp &= ~DTD_RESERVED_FIELDS; 786 swap_temp &= ~DTD_RESERVED_FIELDS;
693 dtd->size_ioc_sts = cpu_to_le32(swap_temp); 787 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
694 788
695 /* Init all of buffer page pointers */ 789 /* Init all of buffer page pointers */
696 swap_temp = (u32) (req->req.dma + req->req.actual); 790 swap_temp = (u32) (req->req.dma + req->req.actual);
697 dtd->buff_ptr0 = cpu_to_le32(swap_temp); 791 dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
698 dtd->buff_ptr1 = cpu_to_le32(swap_temp + 0x1000); 792 dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
699 dtd->buff_ptr2 = cpu_to_le32(swap_temp + 0x2000); 793 dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
700 dtd->buff_ptr3 = cpu_to_le32(swap_temp + 0x3000); 794 dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
701 dtd->buff_ptr4 = cpu_to_le32(swap_temp + 0x4000); 795 dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
702 796
703 req->req.actual += *length; 797 req->req.actual += *length;
704 798
@@ -722,7 +816,7 @@ static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
722 if (*is_last && !req->req.no_interrupt) 816 if (*is_last && !req->req.no_interrupt)
723 swap_temp |= DTD_IOC; 817 swap_temp |= DTD_IOC;
724 818
725 dtd->size_ioc_sts = cpu_to_le32(swap_temp); 819 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
726 820
727 mb(); 821 mb();
728 822
@@ -749,7 +843,7 @@ static int fsl_req_to_dtd(struct fsl_req *req)
749 is_first = 0; 843 is_first = 0;
750 req->head = dtd; 844 req->head = dtd;
751 } else { 845 } else {
752 last_dtd->next_td_ptr = cpu_to_le32(dma); 846 last_dtd->next_td_ptr = cpu_to_hc32(dma);
753 last_dtd->next_td_virt = dtd; 847 last_dtd->next_td_virt = dtd;
754 } 848 }
755 last_dtd = dtd; 849 last_dtd = dtd;
@@ -757,7 +851,7 @@ static int fsl_req_to_dtd(struct fsl_req *req)
757 req->dtd_count++; 851 req->dtd_count++;
758 } while (!is_last); 852 } while (!is_last);
759 853
760 dtd->next_td_ptr = cpu_to_le32(DTD_NEXT_TERMINATE); 854 dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
761 855
762 req->tail = dtd; 856 req->tail = dtd;
763 857
@@ -772,7 +866,6 @@ fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
772 struct fsl_req *req = container_of(_req, struct fsl_req, req); 866 struct fsl_req *req = container_of(_req, struct fsl_req, req);
773 struct fsl_udc *udc; 867 struct fsl_udc *udc;
774 unsigned long flags; 868 unsigned long flags;
775 int is_iso = 0;
776 869
777 /* catch various bogus parameters */ 870 /* catch various bogus parameters */
778 if (!_req || !req->req.complete || !req->req.buf 871 if (!_req || !req->req.complete || !req->req.buf
@@ -787,7 +880,6 @@ fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
787 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) { 880 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
788 if (req->req.length > ep->ep.maxpacket) 881 if (req->req.length > ep->ep.maxpacket)
789 return -EMSGSIZE; 882 return -EMSGSIZE;
790 is_iso = 1;
791 } 883 }
792 884
793 udc = ep->udc; 885 udc = ep->udc;
@@ -970,6 +1062,36 @@ out:
970 return status; 1062 return status;
971} 1063}
972 1064
1065static int fsl_ep_fifo_status(struct usb_ep *_ep)
1066{
1067 struct fsl_ep *ep;
1068 struct fsl_udc *udc;
1069 int size = 0;
1070 u32 bitmask;
1071 struct ep_queue_head *d_qh;
1072
1073 ep = container_of(_ep, struct fsl_ep, ep);
1074 if (!_ep || (!ep->desc && ep_index(ep) != 0))
1075 return -ENODEV;
1076
1077 udc = (struct fsl_udc *)ep->udc;
1078
1079 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1080 return -ESHUTDOWN;
1081
1082 d_qh = &ep->udc->ep_qh[ep_index(ep) * 2 + ep_is_in(ep)];
1083
1084 bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
1085 (1 << (ep_index(ep)));
1086
1087 if (fsl_readl(&dr_regs->endptstatus) & bitmask)
1088 size = (d_qh->size_ioc_int_sts & DTD_PACKET_SIZE)
1089 >> DTD_LENGTH_BIT_POS;
1090
1091 pr_debug("%s %u\n", __func__, size);
1092 return size;
1093}
1094
973static void fsl_ep_fifo_flush(struct usb_ep *_ep) 1095static void fsl_ep_fifo_flush(struct usb_ep *_ep)
974{ 1096{
975 struct fsl_ep *ep; 1097 struct fsl_ep *ep;
@@ -1022,6 +1144,7 @@ static struct usb_ep_ops fsl_ep_ops = {
1022 .dequeue = fsl_ep_dequeue, 1144 .dequeue = fsl_ep_dequeue,
1023 1145
1024 .set_halt = fsl_ep_set_halt, 1146 .set_halt = fsl_ep_set_halt,
1147 .fifo_status = fsl_ep_fifo_status,
1025 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */ 1148 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1026}; 1149};
1027 1150
@@ -1166,6 +1289,11 @@ static int ep0_prime_status(struct fsl_udc *udc, int direction)
1166 req->req.complete = NULL; 1289 req->req.complete = NULL;
1167 req->dtd_count = 0; 1290 req->dtd_count = 0;
1168 1291
1292 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1293 req->req.buf, req->req.length,
1294 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1295 req->mapped = 1;
1296
1169 if (fsl_req_to_dtd(req) == 0) 1297 if (fsl_req_to_dtd(req) == 0)
1170 fsl_queue_td(ep, req); 1298 fsl_queue_td(ep, req);
1171 else 1299 else
@@ -1236,6 +1364,7 @@ static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1236 req = udc->status_req; 1364 req = udc->status_req;
1237 /* Fill in the reqest structure */ 1365 /* Fill in the reqest structure */
1238 *((u16 *) req->req.buf) = cpu_to_le16(tmp); 1366 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1367
1239 req->ep = ep; 1368 req->ep = ep;
1240 req->req.length = 2; 1369 req->req.length = 2;
1241 req->req.status = -EINPROGRESS; 1370 req->req.status = -EINPROGRESS;
@@ -1243,6 +1372,11 @@ static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1243 req->req.complete = NULL; 1372 req->req.complete = NULL;
1244 req->dtd_count = 0; 1373 req->dtd_count = 0;
1245 1374
1375 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1376 req->req.buf, req->req.length,
1377 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1378 req->mapped = 1;
1379
1246 /* prime the data phase */ 1380 /* prime the data phase */
1247 if ((fsl_req_to_dtd(req) == 0)) 1381 if ((fsl_req_to_dtd(req) == 0))
1248 fsl_queue_td(ep, req); 1382 fsl_queue_td(ep, req);
@@ -1288,6 +1422,7 @@ static void setup_received_irq(struct fsl_udc *udc,
1288 /* Status phase from udc */ 1422 /* Status phase from udc */
1289 { 1423 {
1290 int rc = -EOPNOTSUPP; 1424 int rc = -EOPNOTSUPP;
1425 u16 ptc = 0;
1291 1426
1292 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK)) 1427 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1293 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) { 1428 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
@@ -1309,17 +1444,19 @@ static void setup_received_irq(struct fsl_udc *udc,
1309 | USB_TYPE_STANDARD)) { 1444 | USB_TYPE_STANDARD)) {
1310 /* Note: The driver has not include OTG support yet. 1445 /* Note: The driver has not include OTG support yet.
1311 * This will be set when OTG support is added */ 1446 * This will be set when OTG support is added */
1312 if (!gadget_is_otg(&udc->gadget)) 1447 if (wValue == USB_DEVICE_TEST_MODE)
1313 break; 1448 ptc = wIndex >> 8;
1314 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) 1449 else if (gadget_is_otg(&udc->gadget)) {
1315 udc->gadget.b_hnp_enable = 1; 1450 if (setup->bRequest ==
1316 else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT) 1451 USB_DEVICE_B_HNP_ENABLE)
1317 udc->gadget.a_hnp_support = 1; 1452 udc->gadget.b_hnp_enable = 1;
1318 else if (setup->bRequest == 1453 else if (setup->bRequest ==
1319 USB_DEVICE_A_ALT_HNP_SUPPORT) 1454 USB_DEVICE_A_HNP_SUPPORT)
1320 udc->gadget.a_alt_hnp_support = 1; 1455 udc->gadget.a_hnp_support = 1;
1321 else 1456 else if (setup->bRequest ==
1322 break; 1457 USB_DEVICE_A_ALT_HNP_SUPPORT)
1458 udc->gadget.a_alt_hnp_support = 1;
1459 }
1323 rc = 0; 1460 rc = 0;
1324 } else 1461 } else
1325 break; 1462 break;
@@ -1328,6 +1465,15 @@ static void setup_received_irq(struct fsl_udc *udc,
1328 if (ep0_prime_status(udc, EP_DIR_IN)) 1465 if (ep0_prime_status(udc, EP_DIR_IN))
1329 ep0stall(udc); 1466 ep0stall(udc);
1330 } 1467 }
1468 if (ptc) {
1469 u32 tmp;
1470
1471 mdelay(10);
1472 tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
1473 fsl_writel(tmp, &dr_regs->portsc1);
1474 printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
1475 }
1476
1331 return; 1477 return;
1332 } 1478 }
1333 1479
@@ -1402,6 +1548,7 @@ static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1402{ 1548{
1403 u32 temp; 1549 u32 temp;
1404 struct ep_queue_head *qh; 1550 struct ep_queue_head *qh;
1551 struct fsl_usb2_platform_data *pdata = udc->pdata;
1405 1552
1406 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT]; 1553 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1407 1554
@@ -1416,7 +1563,16 @@ static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1416 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd); 1563 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1417 1564
1418 /* Copy the setup packet to local buffer */ 1565 /* Copy the setup packet to local buffer */
1419 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8); 1566 if (pdata->le_setup_buf) {
1567 u32 *p = (u32 *)buffer_ptr;
1568 u32 *s = (u32 *)qh->setup_buffer;
1569
1570 /* Convert little endian setup buffer to CPU endian */
1571 *p++ = le32_to_cpu(*s++);
1572 *p = le32_to_cpu(*s);
1573 } else {
1574 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1575 }
1420 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW)); 1576 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1421 1577
1422 /* Clear Setup Tripwire */ 1578 /* Clear Setup Tripwire */
@@ -1440,19 +1596,19 @@ static int process_ep_req(struct fsl_udc *udc, int pipe,
1440 actual = curr_req->req.length; 1596 actual = curr_req->req.length;
1441 1597
1442 for (j = 0; j < curr_req->dtd_count; j++) { 1598 for (j = 0; j < curr_req->dtd_count; j++) {
1443 remaining_length = (le32_to_cpu(curr_td->size_ioc_sts) 1599 remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
1444 & DTD_PACKET_SIZE) 1600 & DTD_PACKET_SIZE)
1445 >> DTD_LENGTH_BIT_POS; 1601 >> DTD_LENGTH_BIT_POS;
1446 actual -= remaining_length; 1602 actual -= remaining_length;
1447 1603
1448 if ((errors = le32_to_cpu(curr_td->size_ioc_sts) & 1604 errors = hc32_to_cpu(curr_td->size_ioc_sts);
1449 DTD_ERROR_MASK)) { 1605 if (errors & DTD_ERROR_MASK) {
1450 if (errors & DTD_STATUS_HALTED) { 1606 if (errors & DTD_STATUS_HALTED) {
1451 ERR("dTD error %08x QH=%d\n", errors, pipe); 1607 ERR("dTD error %08x QH=%d\n", errors, pipe);
1452 /* Clear the errors and Halt condition */ 1608 /* Clear the errors and Halt condition */
1453 tmp = le32_to_cpu(curr_qh->size_ioc_int_sts); 1609 tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
1454 tmp &= ~errors; 1610 tmp &= ~errors;
1455 curr_qh->size_ioc_int_sts = cpu_to_le32(tmp); 1611 curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
1456 status = -EPIPE; 1612 status = -EPIPE;
1457 /* FIXME: continue with next queued TD? */ 1613 /* FIXME: continue with next queued TD? */
1458 1614
@@ -1467,10 +1623,10 @@ static int process_ep_req(struct fsl_udc *udc, int pipe,
1467 status = -EILSEQ; 1623 status = -EILSEQ;
1468 break; 1624 break;
1469 } else 1625 } else
1470 ERR("Unknown error has occured (0x%x)!\n", 1626 ERR("Unknown error has occurred (0x%x)!\n",
1471 errors); 1627 errors);
1472 1628
1473 } else if (le32_to_cpu(curr_td->size_ioc_sts) 1629 } else if (hc32_to_cpu(curr_td->size_ioc_sts)
1474 & DTD_STATUS_ACTIVE) { 1630 & DTD_STATUS_ACTIVE) {
1475 VDBG("Request not complete"); 1631 VDBG("Request not complete");
1476 status = REQ_UNCOMPLETE; 1632 status = REQ_UNCOMPLETE;
@@ -1559,6 +1715,9 @@ static void port_change_irq(struct fsl_udc *udc)
1559{ 1715{
1560 u32 speed; 1716 u32 speed;
1561 1717
1718 if (udc->bus_reset)
1719 udc->bus_reset = 0;
1720
1562 /* Bus resetting is finished */ 1721 /* Bus resetting is finished */
1563 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) { 1722 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) {
1564 /* Get the speed */ 1723 /* Get the speed */
@@ -1666,6 +1825,8 @@ static void reset_irq(struct fsl_udc *udc)
1666 1825
1667 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) { 1826 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1668 VDBG("Bus reset"); 1827 VDBG("Bus reset");
1828 /* Bus is reseting */
1829 udc->bus_reset = 1;
1669 /* Reset all the queues, include XD, dTD, EP queue 1830 /* Reset all the queues, include XD, dTD, EP queue
1670 * head and TR Queue */ 1831 * head and TR Queue */
1671 reset_queues(udc); 1832 reset_queues(udc);
@@ -1743,6 +1904,7 @@ static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1743 1904
1744 /* Reset Received */ 1905 /* Reset Received */
1745 if (irq_src & USB_STS_RESET) { 1906 if (irq_src & USB_STS_RESET) {
1907 VDBG("reset int");
1746 reset_irq(udc); 1908 reset_irq(udc);
1747 status = IRQ_HANDLED; 1909 status = IRQ_HANDLED;
1748 } 1910 }
@@ -1765,7 +1927,8 @@ static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1765 * Hook to gadget drivers 1927 * Hook to gadget drivers
1766 * Called by initialization code of gadget drivers 1928 * Called by initialization code of gadget drivers
1767*----------------------------------------------------------------*/ 1929*----------------------------------------------------------------*/
1768int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1930int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1931 int (*bind)(struct usb_gadget *))
1769{ 1932{
1770 int retval = -ENODEV; 1933 int retval = -ENODEV;
1771 unsigned long flags = 0; 1934 unsigned long flags = 0;
@@ -1775,8 +1938,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1775 1938
1776 if (!driver || (driver->speed != USB_SPEED_FULL 1939 if (!driver || (driver->speed != USB_SPEED_FULL
1777 && driver->speed != USB_SPEED_HIGH) 1940 && driver->speed != USB_SPEED_HIGH)
1778 || !driver->bind || !driver->disconnect 1941 || !bind || !driver->disconnect || !driver->setup)
1779 || !driver->setup)
1780 return -EINVAL; 1942 return -EINVAL;
1781 1943
1782 if (udc_controller->driver) 1944 if (udc_controller->driver)
@@ -1792,7 +1954,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1792 spin_unlock_irqrestore(&udc_controller->lock, flags); 1954 spin_unlock_irqrestore(&udc_controller->lock, flags);
1793 1955
1794 /* bind udc driver to gadget driver */ 1956 /* bind udc driver to gadget driver */
1795 retval = driver->bind(&udc_controller->gadget); 1957 retval = bind(&udc_controller->gadget);
1796 if (retval) { 1958 if (retval) {
1797 VDBG("bind to %s --> %d", driver->driver.name, retval); 1959 VDBG("bind to %s --> %d", driver->driver.name, retval);
1798 udc_controller->gadget.dev.driver = NULL; 1960 udc_controller->gadget.dev.driver = NULL;
@@ -1800,11 +1962,30 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1800 goto out; 1962 goto out;
1801 } 1963 }
1802 1964
1803 /* Enable DR IRQ reg and Set usbcmd reg Run bit */ 1965 if (udc_controller->transceiver) {
1804 dr_controller_run(udc_controller); 1966 /* Suspend the controller until OTG enable it */
1805 udc_controller->usb_state = USB_STATE_ATTACHED; 1967 udc_controller->stopped = 1;
1806 udc_controller->ep0_state = WAIT_FOR_SETUP; 1968 printk(KERN_INFO "Suspend udc for OTG auto detect\n");
1807 udc_controller->ep0_dir = 0; 1969
1970 /* connect to bus through transceiver */
1971 if (udc_controller->transceiver) {
1972 retval = otg_set_peripheral(udc_controller->transceiver,
1973 &udc_controller->gadget);
1974 if (retval < 0) {
1975 ERR("can't bind to transceiver\n");
1976 driver->unbind(&udc_controller->gadget);
1977 udc_controller->gadget.dev.driver = 0;
1978 udc_controller->driver = 0;
1979 return retval;
1980 }
1981 }
1982 } else {
1983 /* Enable DR IRQ reg and set USBCMD reg Run bit */
1984 dr_controller_run(udc_controller);
1985 udc_controller->usb_state = USB_STATE_ATTACHED;
1986 udc_controller->ep0_state = WAIT_FOR_SETUP;
1987 udc_controller->ep0_dir = 0;
1988 }
1808 printk(KERN_INFO "%s: bind to driver %s\n", 1989 printk(KERN_INFO "%s: bind to driver %s\n",
1809 udc_controller->gadget.name, driver->driver.name); 1990 udc_controller->gadget.name, driver->driver.name);
1810 1991
@@ -1814,7 +1995,7 @@ out:
1814 retval); 1995 retval);
1815 return retval; 1996 return retval;
1816} 1997}
1817EXPORT_SYMBOL(usb_gadget_register_driver); 1998EXPORT_SYMBOL(usb_gadget_probe_driver);
1818 1999
1819/* Disconnect from gadget driver */ 2000/* Disconnect from gadget driver */
1820int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 2001int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
@@ -2052,16 +2233,18 @@ static int fsl_proc_read(char *page, char **start, off_t off, int count,
2052 next += t; 2233 next += t;
2053 2234
2054#ifndef CONFIG_ARCH_MXC 2235#ifndef CONFIG_ARCH_MXC
2055 tmp_reg = usb_sys_regs->snoop1; 2236 if (udc->pdata->have_sysif_regs) {
2056 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg); 2237 tmp_reg = usb_sys_regs->snoop1;
2057 size -= t; 2238 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2058 next += t; 2239 size -= t;
2240 next += t;
2059 2241
2060 tmp_reg = usb_sys_regs->control; 2242 tmp_reg = usb_sys_regs->control;
2061 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n", 2243 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
2062 tmp_reg); 2244 tmp_reg);
2063 size -= t; 2245 size -= t;
2064 next += t; 2246 next += t;
2247 }
2065#endif 2248#endif
2066 2249
2067 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */ 2250 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
@@ -2190,7 +2373,6 @@ static int __init struct_udc_setup(struct fsl_udc *udc,
2190 struct fsl_req, req); 2373 struct fsl_req, req);
2191 /* allocate a small amount of memory to get valid address */ 2374 /* allocate a small amount of memory to get valid address */
2192 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL); 2375 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2193 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2194 2376
2195 udc->resume_state = USB_STATE_NOTATTACHED; 2377 udc->resume_state = USB_STATE_NOTATTACHED;
2196 udc->usb_state = USB_STATE_POWERED; 2378 udc->usb_state = USB_STATE_POWERED;
@@ -2241,6 +2423,7 @@ static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2241 */ 2423 */
2242static int __init fsl_udc_probe(struct platform_device *pdev) 2424static int __init fsl_udc_probe(struct platform_device *pdev)
2243{ 2425{
2426 struct fsl_usb2_platform_data *pdata;
2244 struct resource *res; 2427 struct resource *res;
2245 int ret = -ENODEV; 2428 int ret = -ENODEV;
2246 unsigned int i; 2429 unsigned int i;
@@ -2257,20 +2440,35 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2257 return -ENOMEM; 2440 return -ENOMEM;
2258 } 2441 }
2259 2442
2443 pdata = pdev->dev.platform_data;
2444 udc_controller->pdata = pdata;
2260 spin_lock_init(&udc_controller->lock); 2445 spin_lock_init(&udc_controller->lock);
2261 udc_controller->stopped = 1; 2446 udc_controller->stopped = 1;
2262 2447
2448#ifdef CONFIG_USB_OTG
2449 if (pdata->operating_mode == FSL_USB2_DR_OTG) {
2450 udc_controller->transceiver = otg_get_transceiver();
2451 if (!udc_controller->transceiver) {
2452 ERR("Can't find OTG driver!\n");
2453 ret = -ENODEV;
2454 goto err_kfree;
2455 }
2456 }
2457#endif
2458
2263 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2459 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2264 if (!res) { 2460 if (!res) {
2265 ret = -ENXIO; 2461 ret = -ENXIO;
2266 goto err_kfree; 2462 goto err_kfree;
2267 } 2463 }
2268 2464
2269 if (!request_mem_region(res->start, res->end - res->start + 1, 2465 if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
2270 driver_name)) { 2466 if (!request_mem_region(res->start, res->end - res->start + 1,
2271 ERR("request mem region for %s failed\n", pdev->name); 2467 driver_name)) {
2272 ret = -EBUSY; 2468 ERR("request mem region for %s failed\n", pdev->name);
2273 goto err_kfree; 2469 ret = -EBUSY;
2470 goto err_kfree;
2471 }
2274 } 2472 }
2275 2473
2276 dr_regs = ioremap(res->start, resource_size(res)); 2474 dr_regs = ioremap(res->start, resource_size(res));
@@ -2279,9 +2477,23 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2279 goto err_release_mem_region; 2477 goto err_release_mem_region;
2280 } 2478 }
2281 2479
2480 pdata->regs = (void *)dr_regs;
2481
2482 /*
2483 * do platform specific init: check the clock, grab/config pins, etc.
2484 */
2485 if (pdata->init && pdata->init(pdev)) {
2486 ret = -ENODEV;
2487 goto err_iounmap_noclk;
2488 }
2489
2490 /* Set accessors only after pdata->init() ! */
2491 fsl_set_accessors(pdata);
2492
2282#ifndef CONFIG_ARCH_MXC 2493#ifndef CONFIG_ARCH_MXC
2283 usb_sys_regs = (struct usb_sys_interface *) 2494 if (pdata->have_sysif_regs)
2284 ((u32)dr_regs + USB_DR_SYS_OFFSET); 2495 usb_sys_regs = (struct usb_sys_interface *)
2496 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2285#endif 2497#endif
2286 2498
2287 /* Initialize USB clocks */ 2499 /* Initialize USB clocks */
@@ -2321,9 +2533,11 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2321 goto err_free_irq; 2533 goto err_free_irq;
2322 } 2534 }
2323 2535
2324 /* initialize usb hw reg except for regs for EP, 2536 if (!udc_controller->transceiver) {
2325 * leave usbintr reg untouched */ 2537 /* initialize usb hw reg except for regs for EP,
2326 dr_controller_setup(udc_controller); 2538 * leave usbintr reg untouched */
2539 dr_controller_setup(udc_controller);
2540 }
2327 2541
2328 fsl_udc_clk_finalize(pdev); 2542 fsl_udc_clk_finalize(pdev);
2329 2543
@@ -2343,6 +2557,9 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2343 if (ret < 0) 2557 if (ret < 0)
2344 goto err_free_irq; 2558 goto err_free_irq;
2345 2559
2560 if (udc_controller->transceiver)
2561 udc_controller->gadget.is_otg = 1;
2562
2346 /* setup QH and epctrl for ep0 */ 2563 /* setup QH and epctrl for ep0 */
2347 ep0_setup(udc_controller); 2564 ep0_setup(udc_controller);
2348 2565
@@ -2381,11 +2598,14 @@ err_unregister:
2381err_free_irq: 2598err_free_irq:
2382 free_irq(udc_controller->irq, udc_controller); 2599 free_irq(udc_controller->irq, udc_controller);
2383err_iounmap: 2600err_iounmap:
2601 if (pdata->exit)
2602 pdata->exit(pdev);
2384 fsl_udc_clk_release(); 2603 fsl_udc_clk_release();
2385err_iounmap_noclk: 2604err_iounmap_noclk:
2386 iounmap(dr_regs); 2605 iounmap(dr_regs);
2387err_release_mem_region: 2606err_release_mem_region:
2388 release_mem_region(res->start, res->end - res->start + 1); 2607 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2608 release_mem_region(res->start, res->end - res->start + 1);
2389err_kfree: 2609err_kfree:
2390 kfree(udc_controller); 2610 kfree(udc_controller);
2391 udc_controller = NULL; 2611 udc_controller = NULL;
@@ -2398,6 +2618,7 @@ err_kfree:
2398static int __exit fsl_udc_remove(struct platform_device *pdev) 2618static int __exit fsl_udc_remove(struct platform_device *pdev)
2399{ 2619{
2400 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2620 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2621 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
2401 2622
2402 DECLARE_COMPLETION(done); 2623 DECLARE_COMPLETION(done);
2403 2624
@@ -2418,12 +2639,20 @@ static int __exit fsl_udc_remove(struct platform_device *pdev)
2418 dma_pool_destroy(udc_controller->td_pool); 2639 dma_pool_destroy(udc_controller->td_pool);
2419 free_irq(udc_controller->irq, udc_controller); 2640 free_irq(udc_controller->irq, udc_controller);
2420 iounmap(dr_regs); 2641 iounmap(dr_regs);
2421 release_mem_region(res->start, res->end - res->start + 1); 2642 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2643 release_mem_region(res->start, res->end - res->start + 1);
2422 2644
2423 device_unregister(&udc_controller->gadget.dev); 2645 device_unregister(&udc_controller->gadget.dev);
2424 /* free udc --wait for the release() finished */ 2646 /* free udc --wait for the release() finished */
2425 wait_for_completion(&done); 2647 wait_for_completion(&done);
2426 2648
2649 /*
2650 * do platform specific un-initialization:
2651 * release iomux pins, etc.
2652 */
2653 if (pdata->exit)
2654 pdata->exit(pdev);
2655
2427 return 0; 2656 return 0;
2428} 2657}
2429 2658
@@ -2454,6 +2683,62 @@ static int fsl_udc_resume(struct platform_device *pdev)
2454 return 0; 2683 return 0;
2455} 2684}
2456 2685
2686static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
2687{
2688 struct fsl_udc *udc = udc_controller;
2689 u32 mode, usbcmd;
2690
2691 mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
2692
2693 pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
2694
2695 /*
2696 * If the controller is already stopped, then this must be a
2697 * PM suspend. Remember this fact, so that we will leave the
2698 * controller stopped at PM resume time.
2699 */
2700 if (udc->stopped) {
2701 pr_debug("gadget already stopped, leaving early\n");
2702 udc->already_stopped = 1;
2703 return 0;
2704 }
2705
2706 if (mode != USB_MODE_CTRL_MODE_DEVICE) {
2707 pr_debug("gadget not in device mode, leaving early\n");
2708 return 0;
2709 }
2710
2711 /* stop the controller */
2712 usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
2713 fsl_writel(usbcmd, &dr_regs->usbcmd);
2714
2715 udc->stopped = 1;
2716
2717 pr_info("USB Gadget suspended\n");
2718
2719 return 0;
2720}
2721
2722static int fsl_udc_otg_resume(struct device *dev)
2723{
2724 pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
2725 udc_controller->stopped, udc_controller->already_stopped);
2726
2727 /*
2728 * If the controller was stopped at suspend time, then
2729 * don't resume it now.
2730 */
2731 if (udc_controller->already_stopped) {
2732 udc_controller->already_stopped = 0;
2733 pr_debug("gadget was already stopped, leaving early\n");
2734 return 0;
2735 }
2736
2737 pr_info("USB Gadget resume\n");
2738
2739 return fsl_udc_resume(NULL);
2740}
2741
2457/*------------------------------------------------------------------------- 2742/*-------------------------------------------------------------------------
2458 Register entry point for the peripheral controller driver 2743 Register entry point for the peripheral controller driver
2459--------------------------------------------------------------------------*/ 2744--------------------------------------------------------------------------*/
@@ -2466,6 +2751,9 @@ static struct platform_driver udc_driver = {
2466 .driver = { 2751 .driver = {
2467 .name = (char *)driver_name, 2752 .name = (char *)driver_name,
2468 .owner = THIS_MODULE, 2753 .owner = THIS_MODULE,
2754 /* udc suspend/resume called from OTG driver */
2755 .suspend = fsl_udc_otg_suspend,
2756 .resume = fsl_udc_otg_resume,
2469 }, 2757 },
2470}; 2758};
2471 2759
diff --git a/drivers/usb/gadget/fsl_usb2_udc.h b/drivers/usb/gadget/fsl_usb2_udc.h
index 20aeceed48c7..1d51be83fda8 100644
--- a/drivers/usb/gadget/fsl_usb2_udc.h
+++ b/drivers/usb/gadget/fsl_usb2_udc.h
@@ -15,7 +15,7 @@ struct usb_dr_device {
15 u8 res1[256]; 15 u8 res1[256];
16 u16 caplength; /* Capability Register Length */ 16 u16 caplength; /* Capability Register Length */
17 u16 hciversion; /* Host Controller Interface Version */ 17 u16 hciversion; /* Host Controller Interface Version */
18 u32 hcsparams; /* Host Controller Structual Parameters */ 18 u32 hcsparams; /* Host Controller Structural Parameters */
19 u32 hccparams; /* Host Controller Capability Parameters */ 19 u32 hccparams; /* Host Controller Capability Parameters */
20 u8 res2[20]; 20 u8 res2[20];
21 u32 dciversion; /* Device Controller Interface Version */ 21 u32 dciversion; /* Device Controller Interface Version */
@@ -52,7 +52,7 @@ struct usb_dr_host {
52 u8 res1[256]; 52 u8 res1[256];
53 u16 caplength; /* Capability Register Length */ 53 u16 caplength; /* Capability Register Length */
54 u16 hciversion; /* Host Controller Interface Version */ 54 u16 hciversion; /* Host Controller Interface Version */
55 u32 hcsparams; /* Host Controller Structual Parameters */ 55 u32 hcsparams; /* Host Controller Structural Parameters */
56 u32 hccparams; /* Host Controller Capability Parameters */ 56 u32 hccparams; /* Host Controller Capability Parameters */
57 u8 res2[20]; 57 u8 res2[20];
58 u32 dciversion; /* Device Controller Interface Version */ 58 u32 dciversion; /* Device Controller Interface Version */
@@ -275,7 +275,9 @@ struct usb_sys_interface {
275#define USB_MODE_CTRL_MODE_IDLE 0x00000000 275#define USB_MODE_CTRL_MODE_IDLE 0x00000000
276#define USB_MODE_CTRL_MODE_DEVICE 0x00000002 276#define USB_MODE_CTRL_MODE_DEVICE 0x00000002
277#define USB_MODE_CTRL_MODE_HOST 0x00000003 277#define USB_MODE_CTRL_MODE_HOST 0x00000003
278#define USB_MODE_CTRL_MODE_MASK 0x00000003
278#define USB_MODE_CTRL_MODE_RSV 0x00000001 279#define USB_MODE_CTRL_MODE_RSV 0x00000001
280#define USB_MODE_ES 0x00000004 /* Endian Select */
279#define USB_MODE_SETUP_LOCK_OFF 0x00000008 281#define USB_MODE_SETUP_LOCK_OFF 0x00000008
280#define USB_MODE_STREAM_DISABLE 0x00000010 282#define USB_MODE_STREAM_DISABLE 0x00000010
281/* Endpoint Flush Register */ 283/* Endpoint Flush Register */
@@ -461,6 +463,7 @@ struct fsl_ep {
461struct fsl_udc { 463struct fsl_udc {
462 struct usb_gadget gadget; 464 struct usb_gadget gadget;
463 struct usb_gadget_driver *driver; 465 struct usb_gadget_driver *driver;
466 struct fsl_usb2_platform_data *pdata;
464 struct completion *done; /* to make sure release() is done */ 467 struct completion *done; /* to make sure release() is done */
465 struct fsl_ep *eps; 468 struct fsl_ep *eps;
466 unsigned int max_ep; 469 unsigned int max_ep;
@@ -473,6 +476,8 @@ struct fsl_udc {
473 unsigned vbus_active:1; 476 unsigned vbus_active:1;
474 unsigned stopped:1; 477 unsigned stopped:1;
475 unsigned remote_wakeup:1; 478 unsigned remote_wakeup:1;
479 unsigned already_stopped:1;
480 unsigned big_endian_desc:1;
476 481
477 struct ep_queue_head *ep_qh; /* Endpoints Queue-Head */ 482 struct ep_queue_head *ep_qh; /* Endpoints Queue-Head */
478 struct fsl_req *status_req; /* ep0 status request */ 483 struct fsl_req *status_req; /* ep0 status request */
@@ -483,6 +488,7 @@ struct fsl_udc {
483 dma_addr_t ep_qh_dma; /* dma address of QH */ 488 dma_addr_t ep_qh_dma; /* dma address of QH */
484 489
485 u32 max_pipes; /* Device max pipes */ 490 u32 max_pipes; /* Device max pipes */
491 u32 bus_reset; /* Device is bus resetting */
486 u32 resume_state; /* USB state to resume */ 492 u32 resume_state; /* USB state to resume */
487 u32 usb_state; /* USB current state */ 493 u32 usb_state; /* USB current state */
488 u32 ep0_state; /* Endpoint zero state */ 494 u32 ep0_state; /* Endpoint zero state */
diff --git a/drivers/usb/gadget/fusb300_udc.c b/drivers/usb/gadget/fusb300_udc.c
new file mode 100644
index 000000000000..763d462454b9
--- /dev/null
+++ b/drivers/usb/gadget/fusb300_udc.c
@@ -0,0 +1,1744 @@
1/*
2 * Fusb300 UDC (USB gadget)
3 *
4 * Copyright (C) 2010 Faraday Technology Corp.
5 *
6 * Author : Yuan-hsin Chen <yhchen@faraday-tech.com>
7 *
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
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 */
22#include <linux/dma-mapping.h>
23#include <linux/err.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/platform_device.h>
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
29
30#include "fusb300_udc.h"
31
32MODULE_DESCRIPTION("FUSB300 USB gadget driver");
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
35MODULE_ALIAS("platform:fusb300_udc");
36
37#define DRIVER_VERSION "20 October 2010"
38
39static const char udc_name[] = "fusb300_udc";
40static const char * const fusb300_ep_name[] = {
41 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
42 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
43};
44
45static void done(struct fusb300_ep *ep, struct fusb300_request *req,
46 int status);
47
48static void fusb300_enable_bit(struct fusb300 *fusb300, u32 offset,
49 u32 value)
50{
51 u32 reg = ioread32(fusb300->reg + offset);
52
53 reg |= value;
54 iowrite32(reg, fusb300->reg + offset);
55}
56
57static void fusb300_disable_bit(struct fusb300 *fusb300, u32 offset,
58 u32 value)
59{
60 u32 reg = ioread32(fusb300->reg + offset);
61
62 reg &= ~value;
63 iowrite32(reg, fusb300->reg + offset);
64}
65
66
67static void fusb300_ep_setting(struct fusb300_ep *ep,
68 struct fusb300_ep_info info)
69{
70 ep->epnum = info.epnum;
71 ep->type = info.type;
72}
73
74static int fusb300_ep_release(struct fusb300_ep *ep)
75{
76 if (!ep->epnum)
77 return 0;
78 ep->epnum = 0;
79 ep->stall = 0;
80 ep->wedged = 0;
81 return 0;
82}
83
84static void fusb300_set_fifo_entry(struct fusb300 *fusb300,
85 u32 ep)
86{
87 u32 val = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
88
89 val &= ~FUSB300_EPSET1_FIFOENTRY_MSK;
90 val |= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM);
91 iowrite32(val, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
92}
93
94static void fusb300_set_start_entry(struct fusb300 *fusb300,
95 u8 ep)
96{
97 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
98 u32 start_entry = fusb300->fifo_entry_num * FUSB300_FIFO_ENTRY_NUM;
99
100 reg &= ~FUSB300_EPSET1_START_ENTRY_MSK ;
101 reg |= FUSB300_EPSET1_START_ENTRY(start_entry);
102 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
103 if (fusb300->fifo_entry_num == FUSB300_MAX_FIFO_ENTRY) {
104 fusb300->fifo_entry_num = 0;
105 fusb300->addrofs = 0;
106 pr_err("fifo entry is over the maximum number!\n");
107 } else
108 fusb300->fifo_entry_num++;
109}
110
111/* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
112static void fusb300_set_epaddrofs(struct fusb300 *fusb300,
113 struct fusb300_ep_info info)
114{
115 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
116
117 reg &= ~FUSB300_EPSET2_ADDROFS_MSK;
118 reg |= FUSB300_EPSET2_ADDROFS(fusb300->addrofs);
119 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
120 fusb300->addrofs += (info.maxpacket + 7) / 8 * FUSB300_FIFO_ENTRY_NUM;
121}
122
123static void ep_fifo_setting(struct fusb300 *fusb300,
124 struct fusb300_ep_info info)
125{
126 fusb300_set_fifo_entry(fusb300, info.epnum);
127 fusb300_set_start_entry(fusb300, info.epnum);
128 fusb300_set_epaddrofs(fusb300, info);
129}
130
131static void fusb300_set_eptype(struct fusb300 *fusb300,
132 struct fusb300_ep_info info)
133{
134 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
135
136 reg &= ~FUSB300_EPSET1_TYPE_MSK;
137 reg |= FUSB300_EPSET1_TYPE(info.type);
138 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
139}
140
141static void fusb300_set_epdir(struct fusb300 *fusb300,
142 struct fusb300_ep_info info)
143{
144 u32 reg;
145
146 if (!info.dir_in)
147 return;
148 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
149 reg &= ~FUSB300_EPSET1_DIR_MSK;
150 reg |= FUSB300_EPSET1_DIRIN;
151 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
152}
153
154static void fusb300_set_ep_active(struct fusb300 *fusb300,
155 u8 ep)
156{
157 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
158
159 reg |= FUSB300_EPSET1_ACTEN;
160 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
161}
162
163static void fusb300_set_epmps(struct fusb300 *fusb300,
164 struct fusb300_ep_info info)
165{
166 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
167
168 reg &= ~FUSB300_EPSET2_MPS_MSK;
169 reg |= FUSB300_EPSET2_MPS(info.maxpacket);
170 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
171}
172
173static void fusb300_set_interval(struct fusb300 *fusb300,
174 struct fusb300_ep_info info)
175{
176 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
177
178 reg &= ~FUSB300_EPSET1_INTERVAL(0x7);
179 reg |= FUSB300_EPSET1_INTERVAL(info.interval);
180 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
181}
182
183static void fusb300_set_bwnum(struct fusb300 *fusb300,
184 struct fusb300_ep_info info)
185{
186 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
187
188 reg &= ~FUSB300_EPSET1_BWNUM(0x3);
189 reg |= FUSB300_EPSET1_BWNUM(info.bw_num);
190 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
191}
192
193static void set_ep_reg(struct fusb300 *fusb300,
194 struct fusb300_ep_info info)
195{
196 fusb300_set_eptype(fusb300, info);
197 fusb300_set_epdir(fusb300, info);
198 fusb300_set_epmps(fusb300, info);
199
200 if (info.interval)
201 fusb300_set_interval(fusb300, info);
202
203 if (info.bw_num)
204 fusb300_set_bwnum(fusb300, info);
205
206 fusb300_set_ep_active(fusb300, info.epnum);
207}
208
209static int config_ep(struct fusb300_ep *ep,
210 const struct usb_endpoint_descriptor *desc)
211{
212 struct fusb300 *fusb300 = ep->fusb300;
213 struct fusb300_ep_info info;
214
215 ep->desc = desc;
216
217 info.interval = 0;
218 info.addrofs = 0;
219 info.bw_num = 0;
220
221 info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
222 info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
223 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
224 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
225
226 if ((info.type == USB_ENDPOINT_XFER_INT) ||
227 (info.type == USB_ENDPOINT_XFER_ISOC)) {
228 info.interval = desc->bInterval;
229 if (info.type == USB_ENDPOINT_XFER_ISOC)
230 info.bw_num = ((desc->wMaxPacketSize & 0x1800) >> 11);
231 }
232
233 ep_fifo_setting(fusb300, info);
234
235 set_ep_reg(fusb300, info);
236
237 fusb300_ep_setting(ep, info);
238
239 fusb300->ep[info.epnum] = ep;
240
241 return 0;
242}
243
244static int fusb300_enable(struct usb_ep *_ep,
245 const struct usb_endpoint_descriptor *desc)
246{
247 struct fusb300_ep *ep;
248
249 ep = container_of(_ep, struct fusb300_ep, ep);
250
251 if (ep->fusb300->reenum) {
252 ep->fusb300->fifo_entry_num = 0;
253 ep->fusb300->addrofs = 0;
254 ep->fusb300->reenum = 0;
255 }
256
257 return config_ep(ep, desc);
258}
259
260static int fusb300_disable(struct usb_ep *_ep)
261{
262 struct fusb300_ep *ep;
263 struct fusb300_request *req;
264 unsigned long flags;
265
266 ep = container_of(_ep, struct fusb300_ep, ep);
267
268 BUG_ON(!ep);
269
270 while (!list_empty(&ep->queue)) {
271 req = list_entry(ep->queue.next, struct fusb300_request, queue);
272 spin_lock_irqsave(&ep->fusb300->lock, flags);
273 done(ep, req, -ECONNRESET);
274 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
275 }
276
277 return fusb300_ep_release(ep);
278}
279
280static struct usb_request *fusb300_alloc_request(struct usb_ep *_ep,
281 gfp_t gfp_flags)
282{
283 struct fusb300_request *req;
284
285 req = kzalloc(sizeof(struct fusb300_request), gfp_flags);
286 if (!req)
287 return NULL;
288 INIT_LIST_HEAD(&req->queue);
289
290 return &req->req;
291}
292
293static void fusb300_free_request(struct usb_ep *_ep, struct usb_request *_req)
294{
295 struct fusb300_request *req;
296
297 req = container_of(_req, struct fusb300_request, req);
298 kfree(req);
299}
300
301static int enable_fifo_int(struct fusb300_ep *ep)
302{
303 struct fusb300 *fusb300 = ep->fusb300;
304
305 if (ep->epnum) {
306 fusb300_enable_bit(fusb300, FUSB300_OFFSET_IGER0,
307 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
308 } else {
309 pr_err("can't enable_fifo_int ep0\n");
310 return -EINVAL;
311 }
312
313 return 0;
314}
315
316static int disable_fifo_int(struct fusb300_ep *ep)
317{
318 struct fusb300 *fusb300 = ep->fusb300;
319
320 if (ep->epnum) {
321 fusb300_disable_bit(fusb300, FUSB300_OFFSET_IGER0,
322 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
323 } else {
324 pr_err("can't disable_fifo_int ep0\n");
325 return -EINVAL;
326 }
327
328 return 0;
329}
330
331static void fusb300_set_cxlen(struct fusb300 *fusb300, u32 length)
332{
333 u32 reg;
334
335 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
336 reg &= ~FUSB300_CSR_LEN_MSK;
337 reg |= FUSB300_CSR_LEN(length);
338 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_CSR);
339}
340
341/* write data to cx fifo */
342static void fusb300_wrcxf(struct fusb300_ep *ep,
343 struct fusb300_request *req)
344{
345 int i = 0;
346 u8 *tmp;
347 u32 data;
348 struct fusb300 *fusb300 = ep->fusb300;
349 u32 length = req->req.length - req->req.actual;
350
351 tmp = req->req.buf + req->req.actual;
352
353 if (length > SS_CTL_MAX_PACKET_SIZE) {
354 fusb300_set_cxlen(fusb300, SS_CTL_MAX_PACKET_SIZE);
355 for (i = (SS_CTL_MAX_PACKET_SIZE >> 2); i > 0; i--) {
356 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
357 *(tmp + 3) << 24;
358 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
359 tmp += 4;
360 }
361 req->req.actual += SS_CTL_MAX_PACKET_SIZE;
362 } else { /* length is less than max packet size */
363 fusb300_set_cxlen(fusb300, length);
364 for (i = length >> 2; i > 0; i--) {
365 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
366 *(tmp + 3) << 24;
367 printk(KERN_DEBUG " 0x%x\n", data);
368 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
369 tmp = tmp + 4;
370 }
371 switch (length % 4) {
372 case 1:
373 data = *tmp;
374 printk(KERN_DEBUG " 0x%x\n", data);
375 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
376 break;
377 case 2:
378 data = *tmp | *(tmp + 1) << 8;
379 printk(KERN_DEBUG " 0x%x\n", data);
380 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
381 break;
382 case 3:
383 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
384 printk(KERN_DEBUG " 0x%x\n", data);
385 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
386 break;
387 default:
388 break;
389 }
390 req->req.actual += length;
391 }
392}
393
394static void fusb300_set_epnstall(struct fusb300 *fusb300, u8 ep)
395{
396 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
397 FUSB300_EPSET0_STL);
398}
399
400static void fusb300_clear_epnstall(struct fusb300 *fusb300, u8 ep)
401{
402 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
403
404 if (reg & FUSB300_EPSET0_STL) {
405 printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep);
406 reg &= ~FUSB300_EPSET0_STL;
407 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
408 }
409}
410
411static void ep0_queue(struct fusb300_ep *ep, struct fusb300_request *req)
412{
413 if (ep->fusb300->ep0_dir) { /* if IN */
414 if (req->req.length) {
415 fusb300_wrcxf(ep, req);
416 } else
417 printk(KERN_DEBUG "%s : req->req.length = 0x%x\n",
418 __func__, req->req.length);
419 if ((req->req.length == req->req.actual) ||
420 (req->req.actual < ep->ep.maxpacket))
421 done(ep, req, 0);
422 } else { /* OUT */
423 if (!req->req.length)
424 done(ep, req, 0);
425 else
426 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER1,
427 FUSB300_IGER1_CX_OUT_INT);
428 }
429}
430
431static int fusb300_queue(struct usb_ep *_ep, struct usb_request *_req,
432 gfp_t gfp_flags)
433{
434 struct fusb300_ep *ep;
435 struct fusb300_request *req;
436 unsigned long flags;
437 int request = 0;
438
439 ep = container_of(_ep, struct fusb300_ep, ep);
440 req = container_of(_req, struct fusb300_request, req);
441
442 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
443 return -ESHUTDOWN;
444
445 spin_lock_irqsave(&ep->fusb300->lock, flags);
446
447 if (list_empty(&ep->queue))
448 request = 1;
449
450 list_add_tail(&req->queue, &ep->queue);
451
452 req->req.actual = 0;
453 req->req.status = -EINPROGRESS;
454
455 if (ep->desc == NULL) /* ep0 */
456 ep0_queue(ep, req);
457 else if (request && !ep->stall)
458 enable_fifo_int(ep);
459
460 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
461
462 return 0;
463}
464
465static int fusb300_dequeue(struct usb_ep *_ep, struct usb_request *_req)
466{
467 struct fusb300_ep *ep;
468 struct fusb300_request *req;
469 unsigned long flags;
470
471 ep = container_of(_ep, struct fusb300_ep, ep);
472 req = container_of(_req, struct fusb300_request, req);
473
474 spin_lock_irqsave(&ep->fusb300->lock, flags);
475 if (!list_empty(&ep->queue))
476 done(ep, req, -ECONNRESET);
477 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
478
479 return 0;
480}
481
482static int fusb300_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
483{
484 struct fusb300_ep *ep;
485 struct fusb300 *fusb300;
486 unsigned long flags;
487 int ret = 0;
488
489 ep = container_of(_ep, struct fusb300_ep, ep);
490
491 fusb300 = ep->fusb300;
492
493 spin_lock_irqsave(&ep->fusb300->lock, flags);
494
495 if (!list_empty(&ep->queue)) {
496 ret = -EAGAIN;
497 goto out;
498 }
499
500 if (value) {
501 fusb300_set_epnstall(fusb300, ep->epnum);
502 ep->stall = 1;
503 if (wedge)
504 ep->wedged = 1;
505 } else {
506 fusb300_clear_epnstall(fusb300, ep->epnum);
507 ep->stall = 0;
508 ep->wedged = 0;
509 }
510
511out:
512 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
513 return ret;
514}
515
516static int fusb300_set_halt(struct usb_ep *_ep, int value)
517{
518 return fusb300_set_halt_and_wedge(_ep, value, 0);
519}
520
521static int fusb300_set_wedge(struct usb_ep *_ep)
522{
523 return fusb300_set_halt_and_wedge(_ep, 1, 1);
524}
525
526static void fusb300_fifo_flush(struct usb_ep *_ep)
527{
528}
529
530static struct usb_ep_ops fusb300_ep_ops = {
531 .enable = fusb300_enable,
532 .disable = fusb300_disable,
533
534 .alloc_request = fusb300_alloc_request,
535 .free_request = fusb300_free_request,
536
537 .queue = fusb300_queue,
538 .dequeue = fusb300_dequeue,
539
540 .set_halt = fusb300_set_halt,
541 .fifo_flush = fusb300_fifo_flush,
542 .set_wedge = fusb300_set_wedge,
543};
544
545/*****************************************************************************/
546static void fusb300_clear_int(struct fusb300 *fusb300, u32 offset,
547 u32 value)
548{
549 iowrite32(value, fusb300->reg + offset);
550}
551
552static void fusb300_reset(void)
553{
554}
555
556static void fusb300_set_cxstall(struct fusb300 *fusb300)
557{
558 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
559 FUSB300_CSR_STL);
560}
561
562static void fusb300_set_cxdone(struct fusb300 *fusb300)
563{
564 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
565 FUSB300_CSR_DONE);
566}
567
568/* read data from cx fifo */
569void fusb300_rdcxf(struct fusb300 *fusb300,
570 u8 *buffer, u32 length)
571{
572 int i = 0;
573 u8 *tmp;
574 u32 data;
575
576 tmp = buffer;
577
578 for (i = (length >> 2); i > 0; i--) {
579 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
580 printk(KERN_DEBUG " 0x%x\n", data);
581 *tmp = data & 0xFF;
582 *(tmp + 1) = (data >> 8) & 0xFF;
583 *(tmp + 2) = (data >> 16) & 0xFF;
584 *(tmp + 3) = (data >> 24) & 0xFF;
585 tmp = tmp + 4;
586 }
587
588 switch (length % 4) {
589 case 1:
590 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
591 printk(KERN_DEBUG " 0x%x\n", data);
592 *tmp = data & 0xFF;
593 break;
594 case 2:
595 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
596 printk(KERN_DEBUG " 0x%x\n", data);
597 *tmp = data & 0xFF;
598 *(tmp + 1) = (data >> 8) & 0xFF;
599 break;
600 case 3:
601 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
602 printk(KERN_DEBUG " 0x%x\n", data);
603 *tmp = data & 0xFF;
604 *(tmp + 1) = (data >> 8) & 0xFF;
605 *(tmp + 2) = (data >> 16) & 0xFF;
606 break;
607 default:
608 break;
609 }
610}
611
612#if 0
613static void fusb300_dbg_fifo(struct fusb300_ep *ep,
614 u8 entry, u16 length)
615{
616 u32 reg;
617 u32 i = 0;
618 u32 j = 0;
619
620 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_GTM);
621 reg &= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
622 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG);
623 reg |= (FUSB300_GTM_TST_EP_ENTRY(entry) |
624 FUSB300_GTM_TST_EP_NUM(ep->epnum) | FUSB300_GTM_TST_FIFO_DEG);
625 iowrite32(reg, ep->fusb300->reg + FUSB300_OFFSET_GTM);
626
627 for (i = 0; i < (length >> 2); i++) {
628 if (i * 4 == 1024)
629 break;
630 reg = ioread32(ep->fusb300->reg +
631 FUSB300_OFFSET_BUFDBG_START + i * 4);
632 printk(KERN_DEBUG" 0x%-8x", reg);
633 j++;
634 if ((j % 4) == 0)
635 printk(KERN_DEBUG "\n");
636 }
637
638 if (length % 4) {
639 reg = ioread32(ep->fusb300->reg +
640 FUSB300_OFFSET_BUFDBG_START + i * 4);
641 printk(KERN_DEBUG " 0x%x\n", reg);
642 }
643
644 if ((j % 4) != 0)
645 printk(KERN_DEBUG "\n");
646
647 fusb300_disable_bit(ep->fusb300, FUSB300_OFFSET_GTM,
648 FUSB300_GTM_TST_FIFO_DEG);
649}
650
651static void fusb300_cmp_dbg_fifo(struct fusb300_ep *ep,
652 u8 entry, u16 length, u8 *golden)
653{
654 u32 reg;
655 u32 i = 0;
656 u32 golden_value;
657 u8 *tmp;
658
659 tmp = golden;
660
661 printk(KERN_DEBUG "fusb300_cmp_dbg_fifo (entry %d) : start\n", entry);
662
663 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_GTM);
664 reg &= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
665 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG);
666 reg |= (FUSB300_GTM_TST_EP_ENTRY(entry) |
667 FUSB300_GTM_TST_EP_NUM(ep->epnum) | FUSB300_GTM_TST_FIFO_DEG);
668 iowrite32(reg, ep->fusb300->reg + FUSB300_OFFSET_GTM);
669
670 for (i = 0; i < (length >> 2); i++) {
671 if (i * 4 == 1024)
672 break;
673 golden_value = *tmp | *(tmp + 1) << 8 |
674 *(tmp + 2) << 16 | *(tmp + 3) << 24;
675
676 reg = ioread32(ep->fusb300->reg +
677 FUSB300_OFFSET_BUFDBG_START + i*4);
678
679 if (reg != golden_value) {
680 printk(KERN_DEBUG "0x%x : ", (u32)(ep->fusb300->reg +
681 FUSB300_OFFSET_BUFDBG_START + i*4));
682 printk(KERN_DEBUG " golden = 0x%x, reg = 0x%x\n",
683 golden_value, reg);
684 }
685 tmp += 4;
686 }
687
688 switch (length % 4) {
689 case 1:
690 golden_value = *tmp;
691 case 2:
692 golden_value = *tmp | *(tmp + 1) << 8;
693 case 3:
694 golden_value = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
695 default:
696 break;
697
698 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_BUFDBG_START + i*4);
699 if (reg != golden_value) {
700 printk(KERN_DEBUG "0x%x:", (u32)(ep->fusb300->reg +
701 FUSB300_OFFSET_BUFDBG_START + i*4));
702 printk(KERN_DEBUG " golden = 0x%x, reg = 0x%x\n",
703 golden_value, reg);
704 }
705 }
706
707 printk(KERN_DEBUG "fusb300_cmp_dbg_fifo : end\n");
708 fusb300_disable_bit(ep->fusb300, FUSB300_OFFSET_GTM,
709 FUSB300_GTM_TST_FIFO_DEG);
710}
711#endif
712
713static void fusb300_rdfifo(struct fusb300_ep *ep,
714 struct fusb300_request *req,
715 u32 length)
716{
717 int i = 0;
718 u8 *tmp;
719 u32 data, reg;
720 struct fusb300 *fusb300 = ep->fusb300;
721
722 tmp = req->req.buf + req->req.actual;
723 req->req.actual += length;
724
725 if (req->req.actual > req->req.length)
726 printk(KERN_DEBUG "req->req.actual > req->req.length\n");
727
728 for (i = (length >> 2); i > 0; i--) {
729 data = ioread32(fusb300->reg +
730 FUSB300_OFFSET_EPPORT(ep->epnum));
731 *tmp = data & 0xFF;
732 *(tmp + 1) = (data >> 8) & 0xFF;
733 *(tmp + 2) = (data >> 16) & 0xFF;
734 *(tmp + 3) = (data >> 24) & 0xFF;
735 tmp = tmp + 4;
736 }
737
738 switch (length % 4) {
739 case 1:
740 data = ioread32(fusb300->reg +
741 FUSB300_OFFSET_EPPORT(ep->epnum));
742 *tmp = data & 0xFF;
743 break;
744 case 2:
745 data = ioread32(fusb300->reg +
746 FUSB300_OFFSET_EPPORT(ep->epnum));
747 *tmp = data & 0xFF;
748 *(tmp + 1) = (data >> 8) & 0xFF;
749 break;
750 case 3:
751 data = ioread32(fusb300->reg +
752 FUSB300_OFFSET_EPPORT(ep->epnum));
753 *tmp = data & 0xFF;
754 *(tmp + 1) = (data >> 8) & 0xFF;
755 *(tmp + 2) = (data >> 16) & 0xFF;
756 break;
757 default:
758 break;
759 }
760
761 do {
762 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
763 reg &= FUSB300_IGR1_SYNF0_EMPTY_INT;
764 if (i)
765 printk(KERN_INFO "sync fifo is not empty!\n");
766 i++;
767 } while (!reg);
768}
769
770/* write data to fifo */
771static void fusb300_wrfifo(struct fusb300_ep *ep,
772 struct fusb300_request *req)
773{
774 int i = 0;
775 u8 *tmp;
776 u32 data, reg;
777 struct fusb300 *fusb300 = ep->fusb300;
778
779 tmp = req->req.buf;
780 req->req.actual = req->req.length;
781
782 for (i = (req->req.length >> 2); i > 0; i--) {
783 data = *tmp | *(tmp + 1) << 8 |
784 *(tmp + 2) << 16 | *(tmp + 3) << 24;
785
786 iowrite32(data, fusb300->reg +
787 FUSB300_OFFSET_EPPORT(ep->epnum));
788 tmp += 4;
789 }
790
791 switch (req->req.length % 4) {
792 case 1:
793 data = *tmp;
794 iowrite32(data, fusb300->reg +
795 FUSB300_OFFSET_EPPORT(ep->epnum));
796 break;
797 case 2:
798 data = *tmp | *(tmp + 1) << 8;
799 iowrite32(data, fusb300->reg +
800 FUSB300_OFFSET_EPPORT(ep->epnum));
801 break;
802 case 3:
803 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
804 iowrite32(data, fusb300->reg +
805 FUSB300_OFFSET_EPPORT(ep->epnum));
806 break;
807 default:
808 break;
809 }
810
811 do {
812 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
813 reg &= FUSB300_IGR1_SYNF0_EMPTY_INT;
814 if (i)
815 printk(KERN_INFO"sync fifo is not empty!\n");
816 i++;
817 } while (!reg);
818}
819
820static u8 fusb300_get_epnstall(struct fusb300 *fusb300, u8 ep)
821{
822 u8 value;
823 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
824
825 value = reg & FUSB300_EPSET0_STL;
826
827 return value;
828}
829
830static u8 fusb300_get_cxstall(struct fusb300 *fusb300)
831{
832 u8 value;
833 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
834
835 value = (reg & FUSB300_CSR_STL) >> 1;
836
837 return value;
838}
839
840static void request_error(struct fusb300 *fusb300)
841{
842 fusb300_set_cxstall(fusb300);
843 printk(KERN_DEBUG "request error!!\n");
844}
845
846static void get_status(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
847__releases(fusb300->lock)
848__acquires(fusb300->lock)
849{
850 u8 ep;
851 u16 status = 0;
852 u16 w_index = ctrl->wIndex;
853
854 switch (ctrl->bRequestType & USB_RECIP_MASK) {
855 case USB_RECIP_DEVICE:
856 status = 1 << USB_DEVICE_SELF_POWERED;
857 break;
858 case USB_RECIP_INTERFACE:
859 status = 0;
860 break;
861 case USB_RECIP_ENDPOINT:
862 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
863 if (ep) {
864 if (fusb300_get_epnstall(fusb300, ep))
865 status = 1 << USB_ENDPOINT_HALT;
866 } else {
867 if (fusb300_get_cxstall(fusb300))
868 status = 0;
869 }
870 break;
871
872 default:
873 request_error(fusb300);
874 return; /* exit */
875 }
876
877 fusb300->ep0_data = cpu_to_le16(status);
878 fusb300->ep0_req->buf = &fusb300->ep0_data;
879 fusb300->ep0_req->length = 2;
880
881 spin_unlock(&fusb300->lock);
882 fusb300_queue(fusb300->gadget.ep0, fusb300->ep0_req, GFP_KERNEL);
883 spin_lock(&fusb300->lock);
884}
885
886static void set_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
887{
888 u8 ep;
889
890 switch (ctrl->bRequestType & USB_RECIP_MASK) {
891 case USB_RECIP_DEVICE:
892 fusb300_set_cxdone(fusb300);
893 break;
894 case USB_RECIP_INTERFACE:
895 fusb300_set_cxdone(fusb300);
896 break;
897 case USB_RECIP_ENDPOINT: {
898 u16 w_index = le16_to_cpu(ctrl->wIndex);
899
900 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
901 if (ep)
902 fusb300_set_epnstall(fusb300, ep);
903 else
904 fusb300_set_cxstall(fusb300);
905 fusb300_set_cxdone(fusb300);
906 }
907 break;
908 default:
909 request_error(fusb300);
910 break;
911 }
912}
913
914static void fusb300_clear_seqnum(struct fusb300 *fusb300, u8 ep)
915{
916 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
917 FUSB300_EPSET0_CLRSEQNUM);
918}
919
920static void clear_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
921{
922 struct fusb300_ep *ep =
923 fusb300->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
924
925 switch (ctrl->bRequestType & USB_RECIP_MASK) {
926 case USB_RECIP_DEVICE:
927 fusb300_set_cxdone(fusb300);
928 break;
929 case USB_RECIP_INTERFACE:
930 fusb300_set_cxdone(fusb300);
931 break;
932 case USB_RECIP_ENDPOINT:
933 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
934 if (ep->wedged) {
935 fusb300_set_cxdone(fusb300);
936 break;
937 }
938 if (ep->stall) {
939 ep->stall = 0;
940 fusb300_clear_seqnum(fusb300, ep->epnum);
941 fusb300_clear_epnstall(fusb300, ep->epnum);
942 if (!list_empty(&ep->queue))
943 enable_fifo_int(ep);
944 }
945 }
946 fusb300_set_cxdone(fusb300);
947 break;
948 default:
949 request_error(fusb300);
950 break;
951 }
952}
953
954static void fusb300_set_dev_addr(struct fusb300 *fusb300, u16 addr)
955{
956 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_DAR);
957
958 reg &= ~FUSB300_DAR_DRVADDR_MSK;
959 reg |= FUSB300_DAR_DRVADDR(addr);
960
961 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_DAR);
962}
963
964static void set_address(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
965{
966 if (ctrl->wValue >= 0x0100)
967 request_error(fusb300);
968 else {
969 fusb300_set_dev_addr(fusb300, ctrl->wValue);
970 fusb300_set_cxdone(fusb300);
971 }
972}
973
974#define UVC_COPY_DESCRIPTORS(mem, src) \
975 do { \
976 const struct usb_descriptor_header * const *__src; \
977 for (__src = src; *__src; ++__src) { \
978 memcpy(mem, *__src, (*__src)->bLength); \
979 mem += (*__src)->bLength; \
980 } \
981 } while (0)
982
983static void fusb300_ep0_complete(struct usb_ep *ep,
984 struct usb_request *req)
985{
986}
987
988static int setup_packet(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
989{
990 u8 *p = (u8 *)ctrl;
991 u8 ret = 0;
992 u8 i = 0;
993
994 fusb300_rdcxf(fusb300, p, 8);
995 fusb300->ep0_dir = ctrl->bRequestType & USB_DIR_IN;
996 fusb300->ep0_length = ctrl->wLength;
997
998 /* check request */
999 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1000 switch (ctrl->bRequest) {
1001 case USB_REQ_GET_STATUS:
1002 get_status(fusb300, ctrl);
1003 break;
1004 case USB_REQ_CLEAR_FEATURE:
1005 clear_feature(fusb300, ctrl);
1006 break;
1007 case USB_REQ_SET_FEATURE:
1008 set_feature(fusb300, ctrl);
1009 break;
1010 case USB_REQ_SET_ADDRESS:
1011 set_address(fusb300, ctrl);
1012 break;
1013 case USB_REQ_SET_CONFIGURATION:
1014 fusb300_enable_bit(fusb300, FUSB300_OFFSET_DAR,
1015 FUSB300_DAR_SETCONFG);
1016 /* clear sequence number */
1017 for (i = 1; i <= FUSB300_MAX_NUM_EP; i++)
1018 fusb300_clear_seqnum(fusb300, i);
1019 fusb300->reenum = 1;
1020 ret = 1;
1021 break;
1022 default:
1023 ret = 1;
1024 break;
1025 }
1026 } else
1027 ret = 1;
1028
1029 return ret;
1030}
1031
1032static void fusb300_set_ep_bycnt(struct fusb300_ep *ep, u32 bycnt)
1033{
1034 struct fusb300 *fusb300 = ep->fusb300;
1035 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
1036
1037 reg &= ~FUSB300_FFR_BYCNT;
1038 reg |= bycnt & FUSB300_FFR_BYCNT;
1039
1040 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
1041}
1042
1043static void done(struct fusb300_ep *ep, struct fusb300_request *req,
1044 int status)
1045{
1046 list_del_init(&req->queue);
1047
1048 /* don't modify queue heads during completion callback */
1049 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
1050 req->req.status = -ESHUTDOWN;
1051 else
1052 req->req.status = status;
1053
1054 spin_unlock(&ep->fusb300->lock);
1055 req->req.complete(&ep->ep, &req->req);
1056 spin_lock(&ep->fusb300->lock);
1057
1058 if (ep->epnum) {
1059 disable_fifo_int(ep);
1060 if (!list_empty(&ep->queue))
1061 enable_fifo_int(ep);
1062 } else
1063 fusb300_set_cxdone(ep->fusb300);
1064}
1065
1066void fusb300_fill_idma_prdtbl(struct fusb300_ep *ep,
1067 struct fusb300_request *req)
1068{
1069 u32 value;
1070 u32 reg;
1071
1072 /* wait SW owner */
1073 do {
1074 reg = ioread32(ep->fusb300->reg +
1075 FUSB300_OFFSET_EPPRD_W0(ep->epnum));
1076 reg &= FUSB300_EPPRD0_H;
1077 } while (reg);
1078
1079 iowrite32((u32) req->req.buf, ep->fusb300->reg +
1080 FUSB300_OFFSET_EPPRD_W1(ep->epnum));
1081
1082 value = FUSB300_EPPRD0_BTC(req->req.length) | FUSB300_EPPRD0_H |
1083 FUSB300_EPPRD0_F | FUSB300_EPPRD0_L | FUSB300_EPPRD0_I;
1084 iowrite32(value, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W0(ep->epnum));
1085
1086 iowrite32(0x0, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W2(ep->epnum));
1087
1088 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_EPPRDRDY,
1089 FUSB300_EPPRDR_EP_PRD_RDY(ep->epnum));
1090}
1091
1092static void fusb300_wait_idma_finished(struct fusb300_ep *ep)
1093{
1094 u32 reg;
1095
1096 do {
1097 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR1);
1098 if ((reg & FUSB300_IGR1_VBUS_CHG_INT) ||
1099 (reg & FUSB300_IGR1_WARM_RST_INT) ||
1100 (reg & FUSB300_IGR1_HOT_RST_INT) ||
1101 (reg & FUSB300_IGR1_USBRST_INT)
1102 )
1103 goto IDMA_RESET;
1104 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR0);
1105 reg &= FUSB300_IGR0_EPn_PRD_INT(ep->epnum);
1106 } while (!reg);
1107
1108 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0,
1109 FUSB300_IGR0_EPn_PRD_INT(ep->epnum));
1110IDMA_RESET:
1111 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGER0,
1112 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
1113}
1114
1115static void fusb300_set_idma(struct fusb300_ep *ep,
1116 struct fusb300_request *req)
1117{
1118 dma_addr_t d;
1119 u8 *tmp = NULL;
1120
1121 d = dma_map_single(NULL, req->req.buf, req->req.length, DMA_TO_DEVICE);
1122
1123 if (dma_mapping_error(NULL, d)) {
1124 kfree(req->req.buf);
1125 printk(KERN_DEBUG "dma_mapping_error\n");
1126 }
1127
1128 dma_sync_single_for_device(NULL, d, req->req.length, DMA_TO_DEVICE);
1129
1130 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0,
1131 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
1132
1133 tmp = req->req.buf;
1134 req->req.buf = (u8 *)d;
1135
1136 fusb300_fill_idma_prdtbl(ep, req);
1137 /* check idma is done */
1138 fusb300_wait_idma_finished(ep);
1139
1140 req->req.buf = tmp;
1141
1142 if (d)
1143 dma_unmap_single(NULL, d, req->req.length, DMA_TO_DEVICE);
1144}
1145
1146static void in_ep_fifo_handler(struct fusb300_ep *ep)
1147{
1148 struct fusb300_request *req = list_entry(ep->queue.next,
1149 struct fusb300_request, queue);
1150
1151 if (req->req.length) {
1152#if 0
1153 fusb300_set_ep_bycnt(ep, req->req.length);
1154 fusb300_wrfifo(ep, req);
1155#else
1156 fusb300_set_idma(ep, req);
1157#endif
1158 }
1159 done(ep, req, 0);
1160}
1161
1162static void out_ep_fifo_handler(struct fusb300_ep *ep)
1163{
1164 struct fusb300 *fusb300 = ep->fusb300;
1165 struct fusb300_request *req = list_entry(ep->queue.next,
1166 struct fusb300_request, queue);
1167 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
1168 u32 length = reg & FUSB300_FFR_BYCNT;
1169
1170 fusb300_rdfifo(ep, req, length);
1171
1172 /* finish out transfer */
1173 if ((req->req.length == req->req.actual) || (length < ep->ep.maxpacket))
1174 done(ep, req, 0);
1175}
1176
1177static void check_device_mode(struct fusb300 *fusb300)
1178{
1179 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_GCR);
1180
1181 switch (reg & FUSB300_GCR_DEVEN_MSK) {
1182 case FUSB300_GCR_DEVEN_SS:
1183 fusb300->gadget.speed = USB_SPEED_SUPER;
1184 break;
1185 case FUSB300_GCR_DEVEN_HS:
1186 fusb300->gadget.speed = USB_SPEED_HIGH;
1187 break;
1188 case FUSB300_GCR_DEVEN_FS:
1189 fusb300->gadget.speed = USB_SPEED_FULL;
1190 break;
1191 default:
1192 fusb300->gadget.speed = USB_SPEED_UNKNOWN;
1193 break;
1194 }
1195 printk(KERN_INFO "dev_mode = %d\n", (reg & FUSB300_GCR_DEVEN_MSK));
1196}
1197
1198
1199static void fusb300_ep0out(struct fusb300 *fusb300)
1200{
1201 struct fusb300_ep *ep = fusb300->ep[0];
1202 u32 reg;
1203
1204 if (!list_empty(&ep->queue)) {
1205 struct fusb300_request *req;
1206
1207 req = list_first_entry(&ep->queue,
1208 struct fusb300_request, queue);
1209 if (req->req.length)
1210 fusb300_rdcxf(ep->fusb300, req->req.buf,
1211 req->req.length);
1212 done(ep, req, 0);
1213 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1214 reg &= ~FUSB300_IGER1_CX_OUT_INT;
1215 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_IGER1);
1216 } else
1217 pr_err("%s : empty queue\n", __func__);
1218}
1219
1220static void fusb300_ep0in(struct fusb300 *fusb300)
1221{
1222 struct fusb300_request *req;
1223 struct fusb300_ep *ep = fusb300->ep[0];
1224
1225 if ((!list_empty(&ep->queue)) && (fusb300->ep0_dir)) {
1226 req = list_entry(ep->queue.next,
1227 struct fusb300_request, queue);
1228 if (req->req.length)
1229 fusb300_wrcxf(ep, req);
1230 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
1231 done(ep, req, 0);
1232 } else
1233 fusb300_set_cxdone(fusb300);
1234}
1235
1236static void fusb300_grp2_handler(void)
1237{
1238}
1239
1240static void fusb300_grp3_handler(void)
1241{
1242}
1243
1244static void fusb300_grp4_handler(void)
1245{
1246}
1247
1248static void fusb300_grp5_handler(void)
1249{
1250}
1251
1252static irqreturn_t fusb300_irq(int irq, void *_fusb300)
1253{
1254 struct fusb300 *fusb300 = _fusb300;
1255 u32 int_grp1 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
1256 u32 int_grp1_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1257 u32 int_grp0 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR0);
1258 u32 int_grp0_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER0);
1259 struct usb_ctrlrequest ctrl;
1260 u8 in;
1261 u32 reg;
1262 int i;
1263
1264 spin_lock(&fusb300->lock);
1265
1266 int_grp1 &= int_grp1_en;
1267 int_grp0 &= int_grp0_en;
1268
1269 if (int_grp1 & FUSB300_IGR1_WARM_RST_INT) {
1270 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1271 FUSB300_IGR1_WARM_RST_INT);
1272 printk(KERN_INFO"fusb300_warmreset\n");
1273 fusb300_reset();
1274 }
1275
1276 if (int_grp1 & FUSB300_IGR1_HOT_RST_INT) {
1277 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1278 FUSB300_IGR1_HOT_RST_INT);
1279 printk(KERN_INFO"fusb300_hotreset\n");
1280 fusb300_reset();
1281 }
1282
1283 if (int_grp1 & FUSB300_IGR1_USBRST_INT) {
1284 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1285 FUSB300_IGR1_USBRST_INT);
1286 fusb300_reset();
1287 }
1288 /* COMABT_INT has a highest priority */
1289
1290 if (int_grp1 & FUSB300_IGR1_CX_COMABT_INT) {
1291 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1292 FUSB300_IGR1_CX_COMABT_INT);
1293 printk(KERN_INFO"fusb300_ep0abt\n");
1294 }
1295
1296 if (int_grp1 & FUSB300_IGR1_VBUS_CHG_INT) {
1297 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1298 FUSB300_IGR1_VBUS_CHG_INT);
1299 printk(KERN_INFO"fusb300_vbus_change\n");
1300 }
1301
1302 if (int_grp1 & FUSB300_IGR1_U3_EXIT_FAIL_INT) {
1303 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1304 FUSB300_IGR1_U3_EXIT_FAIL_INT);
1305 }
1306
1307 if (int_grp1 & FUSB300_IGR1_U2_EXIT_FAIL_INT) {
1308 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1309 FUSB300_IGR1_U2_EXIT_FAIL_INT);
1310 }
1311
1312 if (int_grp1 & FUSB300_IGR1_U1_EXIT_FAIL_INT) {
1313 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1314 FUSB300_IGR1_U1_EXIT_FAIL_INT);
1315 }
1316
1317 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_FAIL_INT) {
1318 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1319 FUSB300_IGR1_U2_ENTRY_FAIL_INT);
1320 }
1321
1322 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_FAIL_INT) {
1323 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1324 FUSB300_IGR1_U1_ENTRY_FAIL_INT);
1325 }
1326
1327 if (int_grp1 & FUSB300_IGR1_U3_EXIT_INT) {
1328 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1329 FUSB300_IGR1_U3_EXIT_INT);
1330 printk(KERN_INFO "FUSB300_IGR1_U3_EXIT_INT\n");
1331 }
1332
1333 if (int_grp1 & FUSB300_IGR1_U2_EXIT_INT) {
1334 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1335 FUSB300_IGR1_U2_EXIT_INT);
1336 printk(KERN_INFO "FUSB300_IGR1_U2_EXIT_INT\n");
1337 }
1338
1339 if (int_grp1 & FUSB300_IGR1_U1_EXIT_INT) {
1340 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1341 FUSB300_IGR1_U1_EXIT_INT);
1342 printk(KERN_INFO "FUSB300_IGR1_U1_EXIT_INT\n");
1343 }
1344
1345 if (int_grp1 & FUSB300_IGR1_U3_ENTRY_INT) {
1346 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1347 FUSB300_IGR1_U3_ENTRY_INT);
1348 printk(KERN_INFO "FUSB300_IGR1_U3_ENTRY_INT\n");
1349 fusb300_enable_bit(fusb300, FUSB300_OFFSET_SSCR1,
1350 FUSB300_SSCR1_GO_U3_DONE);
1351 }
1352
1353 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_INT) {
1354 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1355 FUSB300_IGR1_U2_ENTRY_INT);
1356 printk(KERN_INFO "FUSB300_IGR1_U2_ENTRY_INT\n");
1357 }
1358
1359 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_INT) {
1360 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1361 FUSB300_IGR1_U1_ENTRY_INT);
1362 printk(KERN_INFO "FUSB300_IGR1_U1_ENTRY_INT\n");
1363 }
1364
1365 if (int_grp1 & FUSB300_IGR1_RESM_INT) {
1366 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1367 FUSB300_IGR1_RESM_INT);
1368 printk(KERN_INFO "fusb300_resume\n");
1369 }
1370
1371 if (int_grp1 & FUSB300_IGR1_SUSP_INT) {
1372 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1373 FUSB300_IGR1_SUSP_INT);
1374 printk(KERN_INFO "fusb300_suspend\n");
1375 }
1376
1377 if (int_grp1 & FUSB300_IGR1_HS_LPM_INT) {
1378 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1379 FUSB300_IGR1_HS_LPM_INT);
1380 printk(KERN_INFO "fusb300_HS_LPM_INT\n");
1381 }
1382
1383 if (int_grp1 & FUSB300_IGR1_DEV_MODE_CHG_INT) {
1384 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1385 FUSB300_IGR1_DEV_MODE_CHG_INT);
1386 check_device_mode(fusb300);
1387 }
1388
1389 if (int_grp1 & FUSB300_IGR1_CX_COMFAIL_INT) {
1390 fusb300_set_cxstall(fusb300);
1391 printk(KERN_INFO "fusb300_ep0fail\n");
1392 }
1393
1394 if (int_grp1 & FUSB300_IGR1_CX_SETUP_INT) {
1395 printk(KERN_INFO "fusb300_ep0setup\n");
1396 if (setup_packet(fusb300, &ctrl)) {
1397 spin_unlock(&fusb300->lock);
1398 if (fusb300->driver->setup(&fusb300->gadget, &ctrl) < 0)
1399 fusb300_set_cxstall(fusb300);
1400 spin_lock(&fusb300->lock);
1401 }
1402 }
1403
1404 if (int_grp1 & FUSB300_IGR1_CX_CMDEND_INT)
1405 printk(KERN_INFO "fusb300_cmdend\n");
1406
1407
1408 if (int_grp1 & FUSB300_IGR1_CX_OUT_INT) {
1409 printk(KERN_INFO "fusb300_cxout\n");
1410 fusb300_ep0out(fusb300);
1411 }
1412
1413 if (int_grp1 & FUSB300_IGR1_CX_IN_INT) {
1414 printk(KERN_INFO "fusb300_cxin\n");
1415 fusb300_ep0in(fusb300);
1416 }
1417
1418 if (int_grp1 & FUSB300_IGR1_INTGRP5)
1419 fusb300_grp5_handler();
1420
1421 if (int_grp1 & FUSB300_IGR1_INTGRP4)
1422 fusb300_grp4_handler();
1423
1424 if (int_grp1 & FUSB300_IGR1_INTGRP3)
1425 fusb300_grp3_handler();
1426
1427 if (int_grp1 & FUSB300_IGR1_INTGRP2)
1428 fusb300_grp2_handler();
1429
1430 if (int_grp0) {
1431 for (i = 1; i < FUSB300_MAX_NUM_EP; i++) {
1432 if (int_grp0 & FUSB300_IGR0_EPn_FIFO_INT(i)) {
1433 reg = ioread32(fusb300->reg +
1434 FUSB300_OFFSET_EPSET1(i));
1435 in = (reg & FUSB300_EPSET1_DIRIN) ? 1 : 0;
1436 if (in)
1437 in_ep_fifo_handler(fusb300->ep[i]);
1438 else
1439 out_ep_fifo_handler(fusb300->ep[i]);
1440 }
1441 }
1442 }
1443
1444 spin_unlock(&fusb300->lock);
1445
1446 return IRQ_HANDLED;
1447}
1448
1449static void fusb300_set_u2_timeout(struct fusb300 *fusb300,
1450 u32 time)
1451{
1452 u32 reg;
1453
1454 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1455 reg &= ~0xff;
1456 reg |= FUSB300_SSCR2_U2TIMEOUT(time);
1457
1458 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1459}
1460
1461static void fusb300_set_u1_timeout(struct fusb300 *fusb300,
1462 u32 time)
1463{
1464 u32 reg;
1465
1466 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1467 reg &= ~(0xff << 8);
1468 reg |= FUSB300_SSCR2_U1TIMEOUT(time);
1469
1470 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1471}
1472
1473static void init_controller(struct fusb300 *fusb300)
1474{
1475 u32 reg;
1476 u32 mask = 0;
1477 u32 val = 0;
1478
1479 /* split on */
1480 mask = val = FUSB300_AHBBCR_S0_SPLIT_ON | FUSB300_AHBBCR_S1_SPLIT_ON;
1481 reg = ioread32(fusb300->reg + FUSB300_OFFSET_AHBCR);
1482 reg &= ~mask;
1483 reg |= val;
1484 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_AHBCR);
1485
1486 /* enable high-speed LPM */
1487 mask = val = FUSB300_HSCR_HS_LPM_PERMIT;
1488 reg = ioread32(fusb300->reg + FUSB300_OFFSET_HSCR);
1489 reg &= ~mask;
1490 reg |= val;
1491 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_HSCR);
1492
1493 /*set u1 u2 timmer*/
1494 fusb300_set_u2_timeout(fusb300, 0xff);
1495 fusb300_set_u1_timeout(fusb300, 0xff);
1496
1497 /* enable all grp1 interrupt */
1498 iowrite32(0xcfffff9f, fusb300->reg + FUSB300_OFFSET_IGER1);
1499}
1500/*------------------------------------------------------------------------*/
1501static struct fusb300 *the_controller;
1502
1503int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1504 int (*bind)(struct usb_gadget *))
1505{
1506 struct fusb300 *fusb300 = the_controller;
1507 int retval;
1508
1509 if (!driver
1510 || driver->speed < USB_SPEED_FULL
1511 || !bind
1512 || !driver->setup)
1513 return -EINVAL;
1514
1515 if (!fusb300)
1516 return -ENODEV;
1517
1518 if (fusb300->driver)
1519 return -EBUSY;
1520
1521 /* hook up the driver */
1522 driver->driver.bus = NULL;
1523 fusb300->driver = driver;
1524 fusb300->gadget.dev.driver = &driver->driver;
1525
1526 retval = device_add(&fusb300->gadget.dev);
1527 if (retval) {
1528 pr_err("device_add error (%d)\n", retval);
1529 goto error;
1530 }
1531
1532 retval = bind(&fusb300->gadget);
1533 if (retval) {
1534 pr_err("bind to driver error (%d)\n", retval);
1535 device_del(&fusb300->gadget.dev);
1536 goto error;
1537 }
1538
1539 return 0;
1540
1541error:
1542 fusb300->driver = NULL;
1543 fusb300->gadget.dev.driver = NULL;
1544
1545 return retval;
1546}
1547EXPORT_SYMBOL(usb_gadget_probe_driver);
1548
1549int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1550{
1551 struct fusb300 *fusb300 = the_controller;
1552
1553 if (driver != fusb300->driver || !driver->unbind)
1554 return -EINVAL;
1555
1556 driver->unbind(&fusb300->gadget);
1557 fusb300->gadget.dev.driver = NULL;
1558
1559 init_controller(fusb300);
1560 device_del(&fusb300->gadget.dev);
1561 fusb300->driver = NULL;
1562
1563 return 0;
1564}
1565EXPORT_SYMBOL(usb_gadget_unregister_driver);
1566/*--------------------------------------------------------------------------*/
1567
1568static int fusb300_udc_pullup(struct usb_gadget *_gadget, int is_active)
1569{
1570 return 0;
1571}
1572
1573static struct usb_gadget_ops fusb300_gadget_ops = {
1574 .pullup = fusb300_udc_pullup,
1575};
1576
1577static int __exit fusb300_remove(struct platform_device *pdev)
1578{
1579 struct fusb300 *fusb300 = dev_get_drvdata(&pdev->dev);
1580
1581 iounmap(fusb300->reg);
1582 free_irq(platform_get_irq(pdev, 0), fusb300);
1583
1584 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1585 kfree(fusb300);
1586
1587 return 0;
1588}
1589
1590static int __init fusb300_probe(struct platform_device *pdev)
1591{
1592 struct resource *res, *ires, *ires1;
1593 void __iomem *reg = NULL;
1594 struct fusb300 *fusb300 = NULL;
1595 struct fusb300_ep *_ep[FUSB300_MAX_NUM_EP];
1596 int ret = 0;
1597 int i;
1598
1599 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1600 if (!res) {
1601 ret = -ENODEV;
1602 pr_err("platform_get_resource error.\n");
1603 goto clean_up;
1604 }
1605
1606 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1607 if (!ires) {
1608 ret = -ENODEV;
1609 dev_err(&pdev->dev,
1610 "platform_get_resource IORESOURCE_IRQ error.\n");
1611 goto clean_up;
1612 }
1613
1614 ires1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1615 if (!ires1) {
1616 ret = -ENODEV;
1617 dev_err(&pdev->dev,
1618 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1619 goto clean_up;
1620 }
1621
1622 reg = ioremap(res->start, resource_size(res));
1623 if (reg == NULL) {
1624 ret = -ENOMEM;
1625 pr_err("ioremap error.\n");
1626 goto clean_up;
1627 }
1628
1629 /* initialize udc */
1630 fusb300 = kzalloc(sizeof(struct fusb300), GFP_KERNEL);
1631 if (fusb300 == NULL) {
1632 pr_err("kzalloc error\n");
1633 goto clean_up;
1634 }
1635
1636 for (i = 0; i < FUSB300_MAX_NUM_EP; i++) {
1637 _ep[i] = kzalloc(sizeof(struct fusb300_ep), GFP_KERNEL);
1638 if (_ep[i] == NULL) {
1639 pr_err("_ep kzalloc error\n");
1640 goto clean_up;
1641 }
1642 fusb300->ep[i] = _ep[i];
1643 }
1644
1645 spin_lock_init(&fusb300->lock);
1646
1647 dev_set_drvdata(&pdev->dev, fusb300);
1648
1649 fusb300->gadget.ops = &fusb300_gadget_ops;
1650
1651 device_initialize(&fusb300->gadget.dev);
1652
1653 dev_set_name(&fusb300->gadget.dev, "gadget");
1654
1655 fusb300->gadget.is_dualspeed = 1;
1656 fusb300->gadget.dev.parent = &pdev->dev;
1657 fusb300->gadget.dev.dma_mask = pdev->dev.dma_mask;
1658 fusb300->gadget.dev.release = pdev->dev.release;
1659 fusb300->gadget.name = udc_name;
1660 fusb300->reg = reg;
1661
1662 ret = request_irq(ires->start, fusb300_irq, IRQF_DISABLED | IRQF_SHARED,
1663 udc_name, fusb300);
1664 if (ret < 0) {
1665 pr_err("request_irq error (%d)\n", ret);
1666 goto clean_up;
1667 }
1668
1669 ret = request_irq(ires1->start, fusb300_irq,
1670 IRQF_DISABLED | IRQF_SHARED, udc_name, fusb300);
1671 if (ret < 0) {
1672 pr_err("request_irq1 error (%d)\n", ret);
1673 goto clean_up;
1674 }
1675
1676 INIT_LIST_HEAD(&fusb300->gadget.ep_list);
1677
1678 for (i = 0; i < FUSB300_MAX_NUM_EP ; i++) {
1679 struct fusb300_ep *ep = fusb300->ep[i];
1680
1681 if (i != 0) {
1682 INIT_LIST_HEAD(&fusb300->ep[i]->ep.ep_list);
1683 list_add_tail(&fusb300->ep[i]->ep.ep_list,
1684 &fusb300->gadget.ep_list);
1685 }
1686 ep->fusb300 = fusb300;
1687 INIT_LIST_HEAD(&ep->queue);
1688 ep->ep.name = fusb300_ep_name[i];
1689 ep->ep.ops = &fusb300_ep_ops;
1690 ep->ep.maxpacket = HS_BULK_MAX_PACKET_SIZE;
1691 }
1692 fusb300->ep[0]->ep.maxpacket = HS_CTL_MAX_PACKET_SIZE;
1693 fusb300->ep[0]->epnum = 0;
1694 fusb300->gadget.ep0 = &fusb300->ep[0]->ep;
1695 INIT_LIST_HEAD(&fusb300->gadget.ep0->ep_list);
1696
1697 the_controller = fusb300;
1698
1699 fusb300->ep0_req = fusb300_alloc_request(&fusb300->ep[0]->ep,
1700 GFP_KERNEL);
1701 if (fusb300->ep0_req == NULL)
1702 goto clean_up3;
1703
1704 init_controller(fusb300);
1705 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1706
1707 return 0;
1708
1709clean_up3:
1710 free_irq(ires->start, fusb300);
1711
1712clean_up:
1713 if (fusb300) {
1714 if (fusb300->ep0_req)
1715 fusb300_free_request(&fusb300->ep[0]->ep,
1716 fusb300->ep0_req);
1717 kfree(fusb300);
1718 }
1719 if (reg)
1720 iounmap(reg);
1721
1722 return ret;
1723}
1724
1725static struct platform_driver fusb300_driver = {
1726 .remove = __exit_p(fusb300_remove),
1727 .driver = {
1728 .name = (char *) udc_name,
1729 .owner = THIS_MODULE,
1730 },
1731};
1732
1733static int __init fusb300_udc_init(void)
1734{
1735 return platform_driver_probe(&fusb300_driver, fusb300_probe);
1736}
1737
1738module_init(fusb300_udc_init);
1739
1740static void __exit fusb300_udc_cleanup(void)
1741{
1742 platform_driver_unregister(&fusb300_driver);
1743}
1744module_exit(fusb300_udc_cleanup);
diff --git a/drivers/usb/gadget/fusb300_udc.h b/drivers/usb/gadget/fusb300_udc.h
new file mode 100644
index 000000000000..f51aa2ef1f90
--- /dev/null
+++ b/drivers/usb/gadget/fusb300_udc.h
@@ -0,0 +1,687 @@
1/*
2 * Fusb300 UDC (USB gadget)
3 *
4 * Copyright (C) 2010 Faraday Technology Corp.
5 *
6 * Author : Yuan-hsin Chen <yhchen@faraday-tech.com>
7 *
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
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 */
22
23
24#ifndef __FUSB300_UDC_H__
25#define __FUSB300_UDC_H_
26
27#include <linux/kernel.h>
28
29#define FUSB300_OFFSET_GCR 0x00
30#define FUSB300_OFFSET_GTM 0x04
31#define FUSB300_OFFSET_DAR 0x08
32#define FUSB300_OFFSET_CSR 0x0C
33#define FUSB300_OFFSET_CXPORT 0x10
34#define FUSB300_OFFSET_EPSET0(n) (0x20 + (n - 1) * 0x30)
35#define FUSB300_OFFSET_EPSET1(n) (0x24 + (n - 1) * 0x30)
36#define FUSB300_OFFSET_EPSET2(n) (0x28 + (n - 1) * 0x30)
37#define FUSB300_OFFSET_EPFFR(n) (0x2c + (n - 1) * 0x30)
38#define FUSB300_OFFSET_EPSTRID(n) (0x40 + (n - 1) * 0x30)
39#define FUSB300_OFFSET_HSPTM 0x300
40#define FUSB300_OFFSET_HSCR 0x304
41#define FUSB300_OFFSET_SSCR0 0x308
42#define FUSB300_OFFSET_SSCR1 0x30C
43#define FUSB300_OFFSET_TT 0x310
44#define FUSB300_OFFSET_DEVNOTF 0x314
45#define FUSB300_OFFSET_DNC1 0x318
46#define FUSB300_OFFSET_CS 0x31C
47#define FUSB300_OFFSET_SOF 0x324
48#define FUSB300_OFFSET_EFCS 0x328
49#define FUSB300_OFFSET_IGR0 0x400
50#define FUSB300_OFFSET_IGR1 0x404
51#define FUSB300_OFFSET_IGR2 0x408
52#define FUSB300_OFFSET_IGR3 0x40C
53#define FUSB300_OFFSET_IGR4 0x410
54#define FUSB300_OFFSET_IGR5 0x414
55#define FUSB300_OFFSET_IGER0 0x420
56#define FUSB300_OFFSET_IGER1 0x424
57#define FUSB300_OFFSET_IGER2 0x428
58#define FUSB300_OFFSET_IGER3 0x42C
59#define FUSB300_OFFSET_IGER4 0x430
60#define FUSB300_OFFSET_IGER5 0x434
61#define FUSB300_OFFSET_DMAHMER 0x500
62#define FUSB300_OFFSET_EPPRDRDY 0x504
63#define FUSB300_OFFSET_DMAEPMR 0x508
64#define FUSB300_OFFSET_DMAENR 0x50C
65#define FUSB300_OFFSET_DMAAPR 0x510
66#define FUSB300_OFFSET_AHBCR 0x514
67#define FUSB300_OFFSET_EPPRD_W0(n) (0x520 + (n - 1) * 0x10)
68#define FUSB300_OFFSET_EPPRD_W1(n) (0x524 + (n - 1) * 0x10)
69#define FUSB300_OFFSET_EPPRD_W2(n) (0x528 + (n - 1) * 0x10)
70#define FUSB300_OFFSET_EPRD_PTR(n) (0x52C + (n - 1) * 0x10)
71#define FUSB300_OFFSET_BUFDBG_START 0x800
72#define FUSB300_OFFSET_BUFDBG_END 0xBFC
73#define FUSB300_OFFSET_EPPORT(n) (0x1010 + (n - 1) * 0x10)
74
75/*
76 * * Global Control Register (offset = 000H)
77 * */
78#define FUSB300_GCR_SF_RST (1 << 8)
79#define FUSB300_GCR_VBUS_STATUS (1 << 7)
80#define FUSB300_GCR_FORCE_HS_SUSP (1 << 6)
81#define FUSB300_GCR_SYNC_FIFO1_CLR (1 << 5)
82#define FUSB300_GCR_SYNC_FIFO0_CLR (1 << 4)
83#define FUSB300_GCR_FIFOCLR (1 << 3)
84#define FUSB300_GCR_GLINTEN (1 << 2)
85#define FUSB300_GCR_DEVEN_FS 0x3
86#define FUSB300_GCR_DEVEN_HS 0x2
87#define FUSB300_GCR_DEVEN_SS 0x1
88#define FUSB300_GCR_DEVDIS 0x0
89#define FUSB300_GCR_DEVEN_MSK 0x3
90
91
92/*
93 * *Global Test Mode (offset = 004H)
94 * */
95#define FUSB300_GTM_TST_DIS_SOFGEN (1 << 16)
96#define FUSB300_GTM_TST_CUR_EP_ENTRY(n) ((n & 0xF) << 12)
97#define FUSB300_GTM_TST_EP_ENTRY(n) ((n & 0xF) << 8)
98#define FUSB300_GTM_TST_EP_NUM(n) ((n & 0xF) << 4)
99#define FUSB300_GTM_TST_FIFO_DEG (1 << 1)
100#define FUSB300_GTM_TSTMODE (1 << 0)
101
102/*
103 * * Device Address Register (offset = 008H)
104 * */
105#define FUSB300_DAR_SETCONFG (1 << 7)
106#define FUSB300_DAR_DRVADDR(x) (x & 0x7F)
107#define FUSB300_DAR_DRVADDR_MSK 0x7F
108
109/*
110 * *Control Transfer Configuration and Status Register
111 * (CX_Config_Status, offset = 00CH)
112 * */
113#define FUSB300_CSR_LEN(x) ((x & 0xFFFF) << 8)
114#define FUSB300_CSR_LEN_MSK (0xFFFF << 8)
115#define FUSB300_CSR_EMP (1 << 4)
116#define FUSB300_CSR_FUL (1 << 3)
117#define FUSB300_CSR_CLR (1 << 2)
118#define FUSB300_CSR_STL (1 << 1)
119#define FUSB300_CSR_DONE (1 << 0)
120
121/*
122 * * EPn Setting 0 (EPn_SET0, offset = 020H+(n-1)*30H, n=1~15 )
123 * */
124#define FUSB300_EPSET0_CLRSEQNUM (1 << 2)
125#define FUSB300_EPSET0_EPn_TX0BYTE (1 << 1)
126#define FUSB300_EPSET0_STL (1 << 0)
127
128/*
129 * * EPn Setting 1 (EPn_SET1, offset = 024H+(n-1)*30H, n=1~15)
130 * */
131#define FUSB300_EPSET1_START_ENTRY(x) ((x & 0xFF) << 24)
132#define FUSB300_EPSET1_START_ENTRY_MSK (0xFF << 24)
133#define FUSB300_EPSET1_FIFOENTRY(x) ((x & 0x1F) << 12)
134#define FUSB300_EPSET1_FIFOENTRY_MSK (0x1f << 12)
135#define FUSB300_EPSET1_INTERVAL(x) ((x & 0x7) << 6)
136#define FUSB300_EPSET1_BWNUM(x) ((x & 0x3) << 4)
137#define FUSB300_EPSET1_TYPEISO (1 << 2)
138#define FUSB300_EPSET1_TYPEBLK (2 << 2)
139#define FUSB300_EPSET1_TYPEINT (3 << 2)
140#define FUSB300_EPSET1_TYPE(x) ((x & 0x3) << 2)
141#define FUSB300_EPSET1_TYPE_MSK (0x3 << 2)
142#define FUSB300_EPSET1_DIROUT (0 << 1)
143#define FUSB300_EPSET1_DIRIN (1 << 1)
144#define FUSB300_EPSET1_DIR(x) ((x & 0x1) << 1)
145#define FUSB300_EPSET1_DIRIN (1 << 1)
146#define FUSB300_EPSET1_DIR_MSK ((0x1) << 1)
147#define FUSB300_EPSET1_ACTDIS 0
148#define FUSB300_EPSET1_ACTEN 1
149
150/*
151 * *EPn Setting 2 (EPn_SET2, offset = 028H+(n-1)*30H, n=1~15)
152 * */
153#define FUSB300_EPSET2_ADDROFS(x) ((x & 0x7FFF) << 16)
154#define FUSB300_EPSET2_ADDROFS_MSK (0x7fff << 16)
155#define FUSB300_EPSET2_MPS(x) (x & 0x7FF)
156#define FUSB300_EPSET2_MPS_MSK 0x7FF
157
158/*
159 * * EPn FIFO Register (offset = 2cH+(n-1)*30H)
160 * */
161#define FUSB300_FFR_RST (1 << 31)
162#define FUSB300_FF_FUL (1 << 30)
163#define FUSB300_FF_EMPTY (1 << 29)
164#define FUSB300_FFR_BYCNT 0x1FFFF
165
166/*
167 * *EPn Stream ID (EPn_STR_ID, offset = 040H+(n-1)*30H, n=1~15)
168 * */
169#define FUSB300_STRID_STREN (1 << 16)
170#define FUSB300_STRID_STRID(x) (x & 0xFFFF)
171
172/*
173 * *HS PHY Test Mode (offset = 300H)
174 * */
175#define FUSB300_HSPTM_TSTPKDONE (1 << 4)
176#define FUSB300_HSPTM_TSTPKT (1 << 3)
177#define FUSB300_HSPTM_TSTSET0NAK (1 << 2)
178#define FUSB300_HSPTM_TSTKSTA (1 << 1)
179#define FUSB300_HSPTM_TSTJSTA (1 << 0)
180
181/*
182 * *HS Control Register (offset = 304H)
183 * */
184#define FUSB300_HSCR_HS_LPM_PERMIT (1 << 8)
185#define FUSB300_HSCR_HS_LPM_RMWKUP (1 << 7)
186#define FUSB300_HSCR_CAP_LPM_RMWKUP (1 << 6)
187#define FUSB300_HSCR_HS_GOSUSP (1 << 5)
188#define FUSB300_HSCR_HS_GORMWKU (1 << 4)
189#define FUSB300_HSCR_CAP_RMWKUP (1 << 3)
190#define FUSB300_HSCR_IDLECNT_0MS 0
191#define FUSB300_HSCR_IDLECNT_1MS 1
192#define FUSB300_HSCR_IDLECNT_2MS 2
193#define FUSB300_HSCR_IDLECNT_3MS 3
194#define FUSB300_HSCR_IDLECNT_4MS 4
195#define FUSB300_HSCR_IDLECNT_5MS 5
196#define FUSB300_HSCR_IDLECNT_6MS 6
197#define FUSB300_HSCR_IDLECNT_7MS 7
198
199/*
200 * * SS Controller Register 0 (offset = 308H)
201 * */
202#define FUSB300_SSCR0_MAX_INTERVAL(x) ((x & 0x7) << 4)
203#define FUSB300_SSCR0_U2_FUN_EN (1 << 1)
204#define FUSB300_SSCR0_U1_FUN_EN (1 << 0)
205
206/*
207 * * SS Controller Register 1 (offset = 30CH)
208 * */
209#define FUSB300_SSCR1_GO_U3_DONE (1 << 8)
210#define FUSB300_SSCR1_TXDEEMPH_LEVEL (1 << 7)
211#define FUSB300_SSCR1_DIS_SCRMB (1 << 6)
212#define FUSB300_SSCR1_FORCE_RECOVERY (1 << 5)
213#define FUSB300_SSCR1_U3_WAKEUP_EN (1 << 4)
214#define FUSB300_SSCR1_U2_EXIT_EN (1 << 3)
215#define FUSB300_SSCR1_U1_EXIT_EN (1 << 2)
216#define FUSB300_SSCR1_U2_ENTRY_EN (1 << 1)
217#define FUSB300_SSCR1_U1_ENTRY_EN (1 << 0)
218
219/*
220 * *SS Controller Register 2 (offset = 310H)
221 * */
222#define FUSB300_SSCR2_SS_TX_SWING (1 << 25)
223#define FUSB300_SSCR2_FORCE_LINKPM_ACCEPT (1 << 24)
224#define FUSB300_SSCR2_U2_INACT_TIMEOUT(x) ((x & 0xFF) << 16)
225#define FUSB300_SSCR2_U1TIMEOUT(x) ((x & 0xFF) << 8)
226#define FUSB300_SSCR2_U2TIMEOUT(x) (x & 0xFF)
227
228/*
229 * *SS Device Notification Control (DEV_NOTF, offset = 314H)
230 * */
231#define FUSB300_DEVNOTF_CONTEXT0(x) ((x & 0xFFFFFF) << 8)
232#define FUSB300_DEVNOTF_TYPE_DIS 0
233#define FUSB300_DEVNOTF_TYPE_FUNCWAKE 1
234#define FUSB300_DEVNOTF_TYPE_LTM 2
235#define FUSB300_DEVNOTF_TYPE_BUSINT_ADJMSG 3
236
237/*
238 * *BFM Arbiter Priority Register (BFM_ARB offset = 31CH)
239 * */
240#define FUSB300_BFMARB_ARB_M1 (1 << 3)
241#define FUSB300_BFMARB_ARB_M0 (1 << 2)
242#define FUSB300_BFMARB_ARB_S1 (1 << 1)
243#define FUSB300_BFMARB_ARB_S0 1
244
245/*
246 * *Vendor Specific IO Control Register (offset = 320H)
247 * */
248#define FUSB300_VSIC_VCTLOAD_N (1 << 8)
249#define FUSB300_VSIC_VCTL(x) (x & 0x3F)
250
251/*
252 * *SOF Mask Timer (offset = 324H)
253 * */
254#define FUSB300_SOF_MASK_TIMER_HS 0x044c
255#define FUSB300_SOF_MASK_TIMER_FS 0x2710
256
257/*
258 * *Error Flag and Control Status (offset = 328H)
259 * */
260#define FUSB300_EFCS_PM_STATE_U3 3
261#define FUSB300_EFCS_PM_STATE_U2 2
262#define FUSB300_EFCS_PM_STATE_U1 1
263#define FUSB300_EFCS_PM_STATE_U0 0
264
265/*
266 * *Interrupt Group 0 Register (offset = 400H)
267 * */
268#define FUSB300_IGR0_EP15_PRD_INT (1 << 31)
269#define FUSB300_IGR0_EP14_PRD_INT (1 << 30)
270#define FUSB300_IGR0_EP13_PRD_INT (1 << 29)
271#define FUSB300_IGR0_EP12_PRD_INT (1 << 28)
272#define FUSB300_IGR0_EP11_PRD_INT (1 << 27)
273#define FUSB300_IGR0_EP10_PRD_INT (1 << 26)
274#define FUSB300_IGR0_EP9_PRD_INT (1 << 25)
275#define FUSB300_IGR0_EP8_PRD_INT (1 << 24)
276#define FUSB300_IGR0_EP7_PRD_INT (1 << 23)
277#define FUSB300_IGR0_EP6_PRD_INT (1 << 22)
278#define FUSB300_IGR0_EP5_PRD_INT (1 << 21)
279#define FUSB300_IGR0_EP4_PRD_INT (1 << 20)
280#define FUSB300_IGR0_EP3_PRD_INT (1 << 19)
281#define FUSB300_IGR0_EP2_PRD_INT (1 << 18)
282#define FUSB300_IGR0_EP1_PRD_INT (1 << 17)
283#define FUSB300_IGR0_EPn_PRD_INT(n) (1 << (n + 16))
284
285#define FUSB300_IGR0_EP15_FIFO_INT (1 << 15)
286#define FUSB300_IGR0_EP14_FIFO_INT (1 << 14)
287#define FUSB300_IGR0_EP13_FIFO_INT (1 << 13)
288#define FUSB300_IGR0_EP12_FIFO_INT (1 << 12)
289#define FUSB300_IGR0_EP11_FIFO_INT (1 << 11)
290#define FUSB300_IGR0_EP10_FIFO_INT (1 << 10)
291#define FUSB300_IGR0_EP9_FIFO_INT (1 << 9)
292#define FUSB300_IGR0_EP8_FIFO_INT (1 << 8)
293#define FUSB300_IGR0_EP7_FIFO_INT (1 << 7)
294#define FUSB300_IGR0_EP6_FIFO_INT (1 << 6)
295#define FUSB300_IGR0_EP5_FIFO_INT (1 << 5)
296#define FUSB300_IGR0_EP4_FIFO_INT (1 << 4)
297#define FUSB300_IGR0_EP3_FIFO_INT (1 << 3)
298#define FUSB300_IGR0_EP2_FIFO_INT (1 << 2)
299#define FUSB300_IGR0_EP1_FIFO_INT (1 << 1)
300#define FUSB300_IGR0_EPn_FIFO_INT(n) (1 << n)
301
302/*
303 * *Interrupt Group 1 Register (offset = 404H)
304 * */
305#define FUSB300_IGR1_INTGRP5 (1 << 31)
306#define FUSB300_IGR1_VBUS_CHG_INT (1 << 30)
307#define FUSB300_IGR1_SYNF1_EMPTY_INT (1 << 29)
308#define FUSB300_IGR1_SYNF0_EMPTY_INT (1 << 28)
309#define FUSB300_IGR1_U3_EXIT_FAIL_INT (1 << 27)
310#define FUSB300_IGR1_U2_EXIT_FAIL_INT (1 << 26)
311#define FUSB300_IGR1_U1_EXIT_FAIL_INT (1 << 25)
312#define FUSB300_IGR1_U2_ENTRY_FAIL_INT (1 << 24)
313#define FUSB300_IGR1_U1_ENTRY_FAIL_INT (1 << 23)
314#define FUSB300_IGR1_U3_EXIT_INT (1 << 22)
315#define FUSB300_IGR1_U2_EXIT_INT (1 << 21)
316#define FUSB300_IGR1_U1_EXIT_INT (1 << 20)
317#define FUSB300_IGR1_U3_ENTRY_INT (1 << 19)
318#define FUSB300_IGR1_U2_ENTRY_INT (1 << 18)
319#define FUSB300_IGR1_U1_ENTRY_INT (1 << 17)
320#define FUSB300_IGR1_HOT_RST_INT (1 << 16)
321#define FUSB300_IGR1_WARM_RST_INT (1 << 15)
322#define FUSB300_IGR1_RESM_INT (1 << 14)
323#define FUSB300_IGR1_SUSP_INT (1 << 13)
324#define FUSB300_IGR1_HS_LPM_INT (1 << 12)
325#define FUSB300_IGR1_USBRST_INT (1 << 11)
326#define FUSB300_IGR1_DEV_MODE_CHG_INT (1 << 9)
327#define FUSB300_IGR1_CX_COMABT_INT (1 << 8)
328#define FUSB300_IGR1_CX_COMFAIL_INT (1 << 7)
329#define FUSB300_IGR1_CX_CMDEND_INT (1 << 6)
330#define FUSB300_IGR1_CX_OUT_INT (1 << 5)
331#define FUSB300_IGR1_CX_IN_INT (1 << 4)
332#define FUSB300_IGR1_CX_SETUP_INT (1 << 3)
333#define FUSB300_IGR1_INTGRP4 (1 << 2)
334#define FUSB300_IGR1_INTGRP3 (1 << 1)
335#define FUSB300_IGR1_INTGRP2 (1 << 0)
336
337/*
338 * *Interrupt Group 2 Register (offset = 408H)
339 * */
340#define FUSB300_IGR2_EP6_STR_ACCEPT_INT (1 << 29)
341#define FUSB300_IGR2_EP6_STR_RESUME_INT (1 << 28)
342#define FUSB300_IGR2_EP6_STR_REQ_INT (1 << 27)
343#define FUSB300_IGR2_EP6_STR_NOTRDY_INT (1 << 26)
344#define FUSB300_IGR2_EP6_STR_PRIME_INT (1 << 25)
345#define FUSB300_IGR2_EP5_STR_ACCEPT_INT (1 << 24)
346#define FUSB300_IGR2_EP5_STR_RESUME_INT (1 << 23)
347#define FUSB300_IGR2_EP5_STR_REQ_INT (1 << 22)
348#define FUSB300_IGR2_EP5_STR_NOTRDY_INT (1 << 21)
349#define FUSB300_IGR2_EP5_STR_PRIME_INT (1 << 20)
350#define FUSB300_IGR2_EP4_STR_ACCEPT_INT (1 << 19)
351#define FUSB300_IGR2_EP4_STR_RESUME_INT (1 << 18)
352#define FUSB300_IGR2_EP4_STR_REQ_INT (1 << 17)
353#define FUSB300_IGR2_EP4_STR_NOTRDY_INT (1 << 16)
354#define FUSB300_IGR2_EP4_STR_PRIME_INT (1 << 15)
355#define FUSB300_IGR2_EP3_STR_ACCEPT_INT (1 << 14)
356#define FUSB300_IGR2_EP3_STR_RESUME_INT (1 << 13)
357#define FUSB300_IGR2_EP3_STR_REQ_INT (1 << 12)
358#define FUSB300_IGR2_EP3_STR_NOTRDY_INT (1 << 11)
359#define FUSB300_IGR2_EP3_STR_PRIME_INT (1 << 10)
360#define FUSB300_IGR2_EP2_STR_ACCEPT_INT (1 << 9)
361#define FUSB300_IGR2_EP2_STR_RESUME_INT (1 << 8)
362#define FUSB300_IGR2_EP2_STR_REQ_INT (1 << 7)
363#define FUSB300_IGR2_EP2_STR_NOTRDY_INT (1 << 6)
364#define FUSB300_IGR2_EP2_STR_PRIME_INT (1 << 5)
365#define FUSB300_IGR2_EP1_STR_ACCEPT_INT (1 << 4)
366#define FUSB300_IGR2_EP1_STR_RESUME_INT (1 << 3)
367#define FUSB300_IGR2_EP1_STR_REQ_INT (1 << 2)
368#define FUSB300_IGR2_EP1_STR_NOTRDY_INT (1 << 1)
369#define FUSB300_IGR2_EP1_STR_PRIME_INT (1 << 0)
370
371#define FUSB300_IGR2_EP_STR_ACCEPT_INT(n) (1 << (5 * n - 1))
372#define FUSB300_IGR2_EP_STR_RESUME_INT(n) (1 << (5 * n - 2))
373#define FUSB300_IGR2_EP_STR_REQ_INT(n) (1 << (5 * n - 3))
374#define FUSB300_IGR2_EP_STR_NOTRDY_INT(n) (1 << (5 * n - 4))
375#define FUSB300_IGR2_EP_STR_PRIME_INT(n) (1 << (5 * n - 5))
376
377/*
378 * *Interrupt Group 3 Register (offset = 40CH)
379 * */
380#define FUSB300_IGR3_EP12_STR_ACCEPT_INT (1 << 29)
381#define FUSB300_IGR3_EP12_STR_RESUME_INT (1 << 28)
382#define FUSB300_IGR3_EP12_STR_REQ_INT (1 << 27)
383#define FUSB300_IGR3_EP12_STR_NOTRDY_INT (1 << 26)
384#define FUSB300_IGR3_EP12_STR_PRIME_INT (1 << 25)
385#define FUSB300_IGR3_EP11_STR_ACCEPT_INT (1 << 24)
386#define FUSB300_IGR3_EP11_STR_RESUME_INT (1 << 23)
387#define FUSB300_IGR3_EP11_STR_REQ_INT (1 << 22)
388#define FUSB300_IGR3_EP11_STR_NOTRDY_INT (1 << 21)
389#define FUSB300_IGR3_EP11_STR_PRIME_INT (1 << 20)
390#define FUSB300_IGR3_EP10_STR_ACCEPT_INT (1 << 19)
391#define FUSB300_IGR3_EP10_STR_RESUME_INT (1 << 18)
392#define FUSB300_IGR3_EP10_STR_REQ_INT (1 << 17)
393#define FUSB300_IGR3_EP10_STR_NOTRDY_INT (1 << 16)
394#define FUSB300_IGR3_EP10_STR_PRIME_INT (1 << 15)
395#define FUSB300_IGR3_EP9_STR_ACCEPT_INT (1 << 14)
396#define FUSB300_IGR3_EP9_STR_RESUME_INT (1 << 13)
397#define FUSB300_IGR3_EP9_STR_REQ_INT (1 << 12)
398#define FUSB300_IGR3_EP9_STR_NOTRDY_INT (1 << 11)
399#define FUSB300_IGR3_EP9_STR_PRIME_INT (1 << 10)
400#define FUSB300_IGR3_EP8_STR_ACCEPT_INT (1 << 9)
401#define FUSB300_IGR3_EP8_STR_RESUME_INT (1 << 8)
402#define FUSB300_IGR3_EP8_STR_REQ_INT (1 << 7)
403#define FUSB300_IGR3_EP8_STR_NOTRDY_INT (1 << 6)
404#define FUSB300_IGR3_EP8_STR_PRIME_INT (1 << 5)
405#define FUSB300_IGR3_EP7_STR_ACCEPT_INT (1 << 4)
406#define FUSB300_IGR3_EP7_STR_RESUME_INT (1 << 3)
407#define FUSB300_IGR3_EP7_STR_REQ_INT (1 << 2)
408#define FUSB300_IGR3_EP7_STR_NOTRDY_INT (1 << 1)
409#define FUSB300_IGR3_EP7_STR_PRIME_INT (1 << 0)
410
411#define FUSB300_IGR3_EP_STR_ACCEPT_INT(n) (1 << (5 * (n - 6) - 1))
412#define FUSB300_IGR3_EP_STR_RESUME_INT(n) (1 << (5 * (n - 6) - 2))
413#define FUSB300_IGR3_EP_STR_REQ_INT(n) (1 << (5 * (n - 6) - 3))
414#define FUSB300_IGR3_EP_STR_NOTRDY_INT(n) (1 << (5 * (n - 6) - 4))
415#define FUSB300_IGR3_EP_STR_PRIME_INT(n) (1 << (5 * (n - 6) - 5))
416
417/*
418 * *Interrupt Group 4 Register (offset = 410H)
419 * */
420#define FUSB300_IGR4_EP15_RX0_INT (1 << 31)
421#define FUSB300_IGR4_EP14_RX0_INT (1 << 30)
422#define FUSB300_IGR4_EP13_RX0_INT (1 << 29)
423#define FUSB300_IGR4_EP12_RX0_INT (1 << 28)
424#define FUSB300_IGR4_EP11_RX0_INT (1 << 27)
425#define FUSB300_IGR4_EP10_RX0_INT (1 << 26)
426#define FUSB300_IGR4_EP9_RX0_INT (1 << 25)
427#define FUSB300_IGR4_EP8_RX0_INT (1 << 24)
428#define FUSB300_IGR4_EP7_RX0_INT (1 << 23)
429#define FUSB300_IGR4_EP6_RX0_INT (1 << 22)
430#define FUSB300_IGR4_EP5_RX0_INT (1 << 21)
431#define FUSB300_IGR4_EP4_RX0_INT (1 << 20)
432#define FUSB300_IGR4_EP3_RX0_INT (1 << 19)
433#define FUSB300_IGR4_EP2_RX0_INT (1 << 18)
434#define FUSB300_IGR4_EP1_RX0_INT (1 << 17)
435#define FUSB300_IGR4_EP_RX0_INT(x) (1 << (x + 16))
436#define FUSB300_IGR4_EP15_STR_ACCEPT_INT (1 << 14)
437#define FUSB300_IGR4_EP15_STR_RESUME_INT (1 << 13)
438#define FUSB300_IGR4_EP15_STR_REQ_INT (1 << 12)
439#define FUSB300_IGR4_EP15_STR_NOTRDY_INT (1 << 11)
440#define FUSB300_IGR4_EP15_STR_PRIME_INT (1 << 10)
441#define FUSB300_IGR4_EP14_STR_ACCEPT_INT (1 << 9)
442#define FUSB300_IGR4_EP14_STR_RESUME_INT (1 << 8)
443#define FUSB300_IGR4_EP14_STR_REQ_INT (1 << 7)
444#define FUSB300_IGR4_EP14_STR_NOTRDY_INT (1 << 6)
445#define FUSB300_IGR4_EP14_STR_PRIME_INT (1 << 5)
446#define FUSB300_IGR4_EP13_STR_ACCEPT_INT (1 << 4)
447#define FUSB300_IGR4_EP13_STR_RESUME_INT (1 << 3)
448#define FUSB300_IGR4_EP13_STR_REQ_INT (1 << 2)
449#define FUSB300_IGR4_EP13_STR_NOTRDY_INT (1 << 1)
450#define FUSB300_IGR4_EP13_STR_PRIME_INT (1 << 0)
451
452#define FUSB300_IGR4_EP_STR_ACCEPT_INT(n) (1 << (5 * (n - 12) - 1))
453#define FUSB300_IGR4_EP_STR_RESUME_INT(n) (1 << (5 * (n - 12) - 2))
454#define FUSB300_IGR4_EP_STR_REQ_INT(n) (1 << (5 * (n - 12) - 3))
455#define FUSB300_IGR4_EP_STR_NOTRDY_INT(n) (1 << (5 * (n - 12) - 4))
456#define FUSB300_IGR4_EP_STR_PRIME_INT(n) (1 << (5 * (n - 12) - 5))
457
458/*
459 * *Interrupt Group 5 Register (offset = 414H)
460 * */
461#define FUSB300_IGR5_EP_STL_INT(n) (1 << n)
462
463/*
464 * *Interrupt Enable Group 0 Register (offset = 420H)
465 * */
466#define FUSB300_IGER0_EEP15_PRD_INT (1 << 31)
467#define FUSB300_IGER0_EEP14_PRD_INT (1 << 30)
468#define FUSB300_IGER0_EEP13_PRD_INT (1 << 29)
469#define FUSB300_IGER0_EEP12_PRD_INT (1 << 28)
470#define FUSB300_IGER0_EEP11_PRD_INT (1 << 27)
471#define FUSB300_IGER0_EEP10_PRD_INT (1 << 26)
472#define FUSB300_IGER0_EEP9_PRD_INT (1 << 25)
473#define FUSB300_IGER0_EP8_PRD_INT (1 << 24)
474#define FUSB300_IGER0_EEP7_PRD_INT (1 << 23)
475#define FUSB300_IGER0_EEP6_PRD_INT (1 << 22)
476#define FUSB300_IGER0_EEP5_PRD_INT (1 << 21)
477#define FUSB300_IGER0_EEP4_PRD_INT (1 << 20)
478#define FUSB300_IGER0_EEP3_PRD_INT (1 << 19)
479#define FUSB300_IGER0_EEP2_PRD_INT (1 << 18)
480#define FUSB300_IGER0_EEP1_PRD_INT (1 << 17)
481#define FUSB300_IGER0_EEPn_PRD_INT(n) (1 << (n + 16))
482
483#define FUSB300_IGER0_EEP15_FIFO_INT (1 << 15)
484#define FUSB300_IGER0_EEP14_FIFO_INT (1 << 14)
485#define FUSB300_IGER0_EEP13_FIFO_INT (1 << 13)
486#define FUSB300_IGER0_EEP12_FIFO_INT (1 << 12)
487#define FUSB300_IGER0_EEP11_FIFO_INT (1 << 11)
488#define FUSB300_IGER0_EEP10_FIFO_INT (1 << 10)
489#define FUSB300_IGER0_EEP9_FIFO_INT (1 << 9)
490#define FUSB300_IGER0_EEP8_FIFO_INT (1 << 8)
491#define FUSB300_IGER0_EEP7_FIFO_INT (1 << 7)
492#define FUSB300_IGER0_EEP6_FIFO_INT (1 << 6)
493#define FUSB300_IGER0_EEP5_FIFO_INT (1 << 5)
494#define FUSB300_IGER0_EEP4_FIFO_INT (1 << 4)
495#define FUSB300_IGER0_EEP3_FIFO_INT (1 << 3)
496#define FUSB300_IGER0_EEP2_FIFO_INT (1 << 2)
497#define FUSB300_IGER0_EEP1_FIFO_INT (1 << 1)
498#define FUSB300_IGER0_EEPn_FIFO_INT(n) (1 << n)
499
500/*
501 * *Interrupt Enable Group 1 Register (offset = 424H)
502 * */
503#define FUSB300_IGER1_EINT_GRP5 (1 << 31)
504#define FUSB300_IGER1_VBUS_CHG_INT (1 << 30)
505#define FUSB300_IGER1_SYNF1_EMPTY_INT (1 << 29)
506#define FUSB300_IGER1_SYNF0_EMPTY_INT (1 << 28)
507#define FUSB300_IGER1_U3_EXIT_FAIL_INT (1 << 27)
508#define FUSB300_IGER1_U2_EXIT_FAIL_INT (1 << 26)
509#define FUSB300_IGER1_U1_EXIT_FAIL_INT (1 << 25)
510#define FUSB300_IGER1_U2_ENTRY_FAIL_INT (1 << 24)
511#define FUSB300_IGER1_U1_ENTRY_FAIL_INT (1 << 23)
512#define FUSB300_IGER1_U3_EXIT_INT (1 << 22)
513#define FUSB300_IGER1_U2_EXIT_INT (1 << 21)
514#define FUSB300_IGER1_U1_EXIT_INT (1 << 20)
515#define FUSB300_IGER1_U3_ENTRY_INT (1 << 19)
516#define FUSB300_IGER1_U2_ENTRY_INT (1 << 18)
517#define FUSB300_IGER1_U1_ENTRY_INT (1 << 17)
518#define FUSB300_IGER1_HOT_RST_INT (1 << 16)
519#define FUSB300_IGER1_WARM_RST_INT (1 << 15)
520#define FUSB300_IGER1_RESM_INT (1 << 14)
521#define FUSB300_IGER1_SUSP_INT (1 << 13)
522#define FUSB300_IGER1_LPM_INT (1 << 12)
523#define FUSB300_IGER1_HS_RST_INT (1 << 11)
524#define FUSB300_IGER1_EDEV_MODE_CHG_INT (1 << 9)
525#define FUSB300_IGER1_CX_COMABT_INT (1 << 8)
526#define FUSB300_IGER1_CX_COMFAIL_INT (1 << 7)
527#define FUSB300_IGER1_CX_CMDEND_INT (1 << 6)
528#define FUSB300_IGER1_CX_OUT_INT (1 << 5)
529#define FUSB300_IGER1_CX_IN_INT (1 << 4)
530#define FUSB300_IGER1_CX_SETUP_INT (1 << 3)
531#define FUSB300_IGER1_INTGRP4 (1 << 2)
532#define FUSB300_IGER1_INTGRP3 (1 << 1)
533#define FUSB300_IGER1_INTGRP2 (1 << 0)
534
535/*
536 * *Interrupt Enable Group 2 Register (offset = 428H)
537 * */
538#define FUSB300_IGER2_EEP_STR_ACCEPT_INT(n) (1 << (5 * n - 1))
539#define FUSB300_IGER2_EEP_STR_RESUME_INT(n) (1 << (5 * n - 2))
540#define FUSB300_IGER2_EEP_STR_REQ_INT(n) (1 << (5 * n - 3))
541#define FUSB300_IGER2_EEP_STR_NOTRDY_INT(n) (1 << (5 * n - 4))
542#define FUSB300_IGER2_EEP_STR_PRIME_INT(n) (1 << (5 * n - 5))
543
544/*
545 * *Interrupt Enable Group 3 Register (offset = 42CH)
546 * */
547
548#define FUSB300_IGER3_EEP_STR_ACCEPT_INT(n) (1 << (5 * (n - 6) - 1))
549#define FUSB300_IGER3_EEP_STR_RESUME_INT(n) (1 << (5 * (n - 6) - 2))
550#define FUSB300_IGER3_EEP_STR_REQ_INT(n) (1 << (5 * (n - 6) - 3))
551#define FUSB300_IGER3_EEP_STR_NOTRDY_INT(n) (1 << (5 * (n - 6) - 4))
552#define FUSB300_IGER3_EEP_STR_PRIME_INT(n) (1 << (5 * (n - 6) - 5))
553
554/*
555 * *Interrupt Enable Group 4 Register (offset = 430H)
556 * */
557
558#define FUSB300_IGER4_EEP_RX0_INT(n) (1 << (n + 16))
559#define FUSB300_IGER4_EEP_STR_ACCEPT_INT(n) (1 << (5 * (n - 6) - 1))
560#define FUSB300_IGER4_EEP_STR_RESUME_INT(n) (1 << (5 * (n - 6) - 2))
561#define FUSB300_IGER4_EEP_STR_REQ_INT(n) (1 << (5 * (n - 6) - 3))
562#define FUSB300_IGER4_EEP_STR_NOTRDY_INT(n) (1 << (5 * (n - 6) - 4))
563#define FUSB300_IGER4_EEP_STR_PRIME_INT(n) (1 << (5 * (n - 6) - 5))
564
565/* EP PRD Ready (EP_PRD_RDY, offset = 504H) */
566
567#define FUSB300_EPPRDR_EP15_PRD_RDY (1 << 15)
568#define FUSB300_EPPRDR_EP14_PRD_RDY (1 << 14)
569#define FUSB300_EPPRDR_EP13_PRD_RDY (1 << 13)
570#define FUSB300_EPPRDR_EP12_PRD_RDY (1 << 12)
571#define FUSB300_EPPRDR_EP11_PRD_RDY (1 << 11)
572#define FUSB300_EPPRDR_EP10_PRD_RDY (1 << 10)
573#define FUSB300_EPPRDR_EP9_PRD_RDY (1 << 9)
574#define FUSB300_EPPRDR_EP8_PRD_RDY (1 << 8)
575#define FUSB300_EPPRDR_EP7_PRD_RDY (1 << 7)
576#define FUSB300_EPPRDR_EP6_PRD_RDY (1 << 6)
577#define FUSB300_EPPRDR_EP5_PRD_RDY (1 << 5)
578#define FUSB300_EPPRDR_EP4_PRD_RDY (1 << 4)
579#define FUSB300_EPPRDR_EP3_PRD_RDY (1 << 3)
580#define FUSB300_EPPRDR_EP2_PRD_RDY (1 << 2)
581#define FUSB300_EPPRDR_EP1_PRD_RDY (1 << 1)
582#define FUSB300_EPPRDR_EP_PRD_RDY(n) (1 << n)
583
584/* AHB Bus Control Register (offset = 514H) */
585#define FUSB300_AHBBCR_S1_SPLIT_ON (1 << 17)
586#define FUSB300_AHBBCR_S0_SPLIT_ON (1 << 16)
587#define FUSB300_AHBBCR_S1_1entry (0 << 12)
588#define FUSB300_AHBBCR_S1_4entry (3 << 12)
589#define FUSB300_AHBBCR_S1_8entry (5 << 12)
590#define FUSB300_AHBBCR_S1_16entry (7 << 12)
591#define FUSB300_AHBBCR_S0_1entry (0 << 8)
592#define FUSB300_AHBBCR_S0_4entry (3 << 8)
593#define FUSB300_AHBBCR_S0_8entry (5 << 8)
594#define FUSB300_AHBBCR_S0_16entry (7 << 8)
595#define FUSB300_AHBBCR_M1_BURST_SINGLE (0 << 4)
596#define FUSB300_AHBBCR_M1_BURST_INCR (1 << 4)
597#define FUSB300_AHBBCR_M1_BURST_INCR4 (3 << 4)
598#define FUSB300_AHBBCR_M1_BURST_INCR8 (5 << 4)
599#define FUSB300_AHBBCR_M1_BURST_INCR16 (7 << 4)
600#define FUSB300_AHBBCR_M0_BURST_SINGLE 0
601#define FUSB300_AHBBCR_M0_BURST_INCR 1
602#define FUSB300_AHBBCR_M0_BURST_INCR4 3
603#define FUSB300_AHBBCR_M0_BURST_INCR8 5
604#define FUSB300_AHBBCR_M0_BURST_INCR16 7
605#define FUSB300_IGER5_EEP_STL_INT(n) (1 << n)
606
607/* WORD 0 Data Structure of PRD Table */
608#define FUSB300_EPPRD0_M (1 << 30)
609#define FUSB300_EPPRD0_O (1 << 29)
610/* The finished prd */
611#define FUSB300_EPPRD0_F (1 << 28)
612#define FUSB300_EPPRD0_I (1 << 27)
613#define FUSB300_EPPRD0_A (1 << 26)
614/* To decide HW point to first prd at next time */
615#define FUSB300_EPPRD0_L (1 << 25)
616#define FUSB300_EPPRD0_H (1 << 24)
617#define FUSB300_EPPRD0_BTC(n) (n & 0xFFFFFF)
618
619/*----------------------------------------------------------------------*/
620#define FUSB300_MAX_NUM_EP 16
621
622#define FUSB300_FIFO_ENTRY_NUM 8
623#define FUSB300_MAX_FIFO_ENTRY 8
624
625#define SS_CTL_MAX_PACKET_SIZE 0x200
626#define SS_BULK_MAX_PACKET_SIZE 0x400
627#define SS_INT_MAX_PACKET_SIZE 0x400
628#define SS_ISO_MAX_PACKET_SIZE 0x400
629
630#define HS_BULK_MAX_PACKET_SIZE 0x200
631#define HS_CTL_MAX_PACKET_SIZE 0x40
632#define HS_INT_MAX_PACKET_SIZE 0x400
633#define HS_ISO_MAX_PACKET_SIZE 0x400
634
635struct fusb300_ep_info {
636 u8 epnum;
637 u8 type;
638 u8 interval;
639 u8 dir_in;
640 u16 maxpacket;
641 u16 addrofs;
642 u16 bw_num;
643};
644
645struct fusb300_request {
646
647 struct usb_request req;
648 struct list_head queue;
649};
650
651
652struct fusb300_ep {
653 struct usb_ep ep;
654 struct fusb300 *fusb300;
655
656 struct list_head queue;
657 unsigned stall:1;
658 unsigned wedged:1;
659 unsigned use_dma:1;
660
661 unsigned char epnum;
662 unsigned char type;
663 const struct usb_endpoint_descriptor *desc;
664};
665
666struct fusb300 {
667 spinlock_t lock;
668 void __iomem *reg;
669
670 unsigned long irq_trigger;
671
672 struct usb_gadget gadget;
673 struct usb_gadget_driver *driver;
674
675 struct fusb300_ep *ep[FUSB300_MAX_NUM_EP];
676
677 struct usb_request *ep0_req; /* for internal request */
678 __le16 ep0_data;
679 u32 ep0_length; /* for internal request */
680 u8 ep0_dir; /* 0/0x80 out/in */
681
682 u8 fifo_entry_num; /* next start fifo entry */
683 u32 addrofs; /* next fifo address offset */
684 u8 reenum; /* if re-enumeration */
685};
686
687#endif
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index a9474f8d5325..ebf6970a10bf 100644
--- a/drivers/usb/gadget/g_ffs.c
+++ b/drivers/usb/gadget/g_ffs.c
@@ -1,7 +1,29 @@
1/*
2 * g_ffs.c -- user mode file system API for USB composite function controllers
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
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
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
22#define pr_fmt(fmt) "g_ffs: " fmt
23
1#include <linux/module.h> 24#include <linux/module.h>
2#include <linux/utsname.h> 25#include <linux/utsname.h>
3 26
4
5/* 27/*
6 * kbuild is not very cooperative with respect to linking separately 28 * kbuild is not very cooperative with respect to linking separately
7 * compiled library objects into one module. So for now we won't use 29 * compiled library objects into one module. So for now we won't use
@@ -43,7 +65,6 @@ static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
43 65
44#include "f_fs.c" 66#include "f_fs.c"
45 67
46
47#define DRIVER_NAME "g_ffs" 68#define DRIVER_NAME "g_ffs"
48#define DRIVER_DESC "USB Function Filesystem" 69#define DRIVER_DESC "USB Function Filesystem"
49#define DRIVER_VERSION "24 Aug 2004" 70#define DRIVER_VERSION "24 Aug 2004"
@@ -52,9 +73,8 @@ MODULE_DESCRIPTION(DRIVER_DESC);
52MODULE_AUTHOR("Michal Nazarewicz"); 73MODULE_AUTHOR("Michal Nazarewicz");
53MODULE_LICENSE("GPL"); 74MODULE_LICENSE("GPL");
54 75
55 76#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
56static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */ 77#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
57static unsigned short gfs_product_id = 0xa4ac; /* XXX */
58 78
59static struct usb_device_descriptor gfs_dev_desc = { 79static struct usb_device_descriptor gfs_dev_desc = {
60 .bLength = sizeof gfs_dev_desc, 80 .bLength = sizeof gfs_dev_desc,
@@ -63,31 +83,16 @@ static struct usb_device_descriptor gfs_dev_desc = {
63 .bcdUSB = cpu_to_le16(0x0200), 83 .bcdUSB = cpu_to_le16(0x0200),
64 .bDeviceClass = USB_CLASS_PER_INTERFACE, 84 .bDeviceClass = USB_CLASS_PER_INTERFACE,
65 85
66 /* Vendor and product id can be overridden by module parameters. */ 86 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
67 /* .idVendor = cpu_to_le16(gfs_vendor_id), */ 87 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
68 /* .idProduct = cpu_to_le16(gfs_product_id), */
69 /* .bcdDevice = f(hardware) */
70 /* .iManufacturer = DYNAMIC */
71 /* .iProduct = DYNAMIC */
72 /* NO SERIAL NUMBER */
73 .bNumConfigurations = 1,
74}; 88};
75 89
76#define GFS_MODULE_PARAM_DESC(name, field) \ 90module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
77 MODULE_PARM_DESC(name, "Value of the " #field " field of the device descriptor sent to the host. Takes effect only prior to the user-space driver registering to the FunctionFS.") 91MODULE_PARM_DESC(bDeviceClass, "USB Device class");
78 92module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
79module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644); 93MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
80GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass); 94module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
81module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644); 95MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
82GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass);
83module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
84GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol);
85module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644);
86GFS_MODULE_PARAM_DESC(usb_vendor, idVendor);
87module_param_named(usb_product, gfs_product_id, ushort, 0644);
88GFS_MODULE_PARAM_DESC(usb_product, idProduct);
89
90
91 96
92static const struct usb_descriptor_header *gfs_otg_desc[] = { 97static const struct usb_descriptor_header *gfs_otg_desc[] = {
93 (const struct usb_descriptor_header *) 98 (const struct usb_descriptor_header *)
@@ -95,29 +100,18 @@ static const struct usb_descriptor_header *gfs_otg_desc[] = {
95 .bLength = sizeof(struct usb_otg_descriptor), 100 .bLength = sizeof(struct usb_otg_descriptor),
96 .bDescriptorType = USB_DT_OTG, 101 .bDescriptorType = USB_DT_OTG,
97 102
98 /* REVISIT SRP-only hardware is possible, although 103 /*
99 * it would not be called "OTG" ... */ 104 * REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
106 */
100 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, 107 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
101 }, 108 },
102 109
103 NULL 110 NULL
104}; 111};
105 112
106/* string IDs are assigned dynamically */ 113/* String IDs are assigned dynamically */
107
108enum {
109 GFS_STRING_MANUFACTURER_IDX,
110 GFS_STRING_PRODUCT_IDX,
111 GFS_STRING_FIRST_CONFIG_IDX,
112};
113
114static char gfs_manufacturer[50];
115static const char gfs_driver_desc[] = DRIVER_DESC;
116static const char gfs_short_name[] = DRIVER_NAME;
117
118static struct usb_string gfs_strings[] = { 114static struct usb_string gfs_strings[] = {
119 [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
120 [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
121#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 115#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
122 { .s = "FunctionFS + RNDIS" }, 116 { .s = "FunctionFS + RNDIS" },
123#endif 117#endif
@@ -138,8 +132,6 @@ static struct usb_gadget_strings *gfs_dev_strings[] = {
138 NULL, 132 NULL,
139}; 133};
140 134
141
142
143struct gfs_configuration { 135struct gfs_configuration {
144 struct usb_configuration c; 136 struct usb_configuration c;
145 int (*eth)(struct usb_configuration *c, u8 *ethaddr); 137 int (*eth)(struct usb_configuration *c, u8 *ethaddr);
@@ -162,24 +154,21 @@ struct gfs_configuration {
162#endif 154#endif
163}; 155};
164 156
165
166static int gfs_bind(struct usb_composite_dev *cdev); 157static int gfs_bind(struct usb_composite_dev *cdev);
167static int gfs_unbind(struct usb_composite_dev *cdev); 158static int gfs_unbind(struct usb_composite_dev *cdev);
168static int gfs_do_config(struct usb_configuration *c); 159static int gfs_do_config(struct usb_configuration *c);
169 160
170static struct usb_composite_driver gfs_driver = { 161static struct usb_composite_driver gfs_driver = {
171 .name = gfs_short_name, 162 .name = DRIVER_NAME,
172 .dev = &gfs_dev_desc, 163 .dev = &gfs_dev_desc,
173 .strings = gfs_dev_strings, 164 .strings = gfs_dev_strings,
174 .bind = gfs_bind,
175 .unbind = gfs_unbind, 165 .unbind = gfs_unbind,
166 .iProduct = DRIVER_DESC,
176}; 167};
177 168
178
179static struct ffs_data *gfs_ffs_data; 169static struct ffs_data *gfs_ffs_data;
180static unsigned long gfs_registered; 170static unsigned long gfs_registered;
181 171
182
183static int gfs_init(void) 172static int gfs_init(void)
184{ 173{
185 ENTER(); 174 ENTER();
@@ -199,7 +188,6 @@ static void gfs_exit(void)
199} 188}
200module_exit(gfs_exit); 189module_exit(gfs_exit);
201 190
202
203static int functionfs_ready_callback(struct ffs_data *ffs) 191static int functionfs_ready_callback(struct ffs_data *ffs)
204{ 192{
205 int ret; 193 int ret;
@@ -210,7 +198,7 @@ static int functionfs_ready_callback(struct ffs_data *ffs)
210 return -EBUSY; 198 return -EBUSY;
211 199
212 gfs_ffs_data = ffs; 200 gfs_ffs_data = ffs;
213 ret = usb_composite_register(&gfs_driver); 201 ret = usb_composite_probe(&gfs_driver, gfs_bind);
214 if (unlikely(ret < 0)) 202 if (unlikely(ret < 0))
215 clear_bit(0, &gfs_registered); 203 clear_bit(0, &gfs_registered);
216 return ret; 204 return ret;
@@ -224,14 +212,11 @@ static void functionfs_closed_callback(struct ffs_data *ffs)
224 usb_composite_unregister(&gfs_driver); 212 usb_composite_unregister(&gfs_driver);
225} 213}
226 214
227
228static int functionfs_check_dev_callback(const char *dev_name) 215static int functionfs_check_dev_callback(const char *dev_name)
229{ 216{
230 return 0; 217 return 0;
231} 218}
232 219
233
234
235static int gfs_bind(struct usb_composite_dev *cdev) 220static int gfs_bind(struct usb_composite_dev *cdev)
236{ 221{
237 int ret, i; 222 int ret, i;
@@ -245,20 +230,10 @@ static int gfs_bind(struct usb_composite_dev *cdev)
245 if (unlikely(ret < 0)) 230 if (unlikely(ret < 0))
246 goto error_quick; 231 goto error_quick;
247 232
248 gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id);
249 gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id);
250
251 snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
252 init_utsname()->sysname, init_utsname()->release,
253 cdev->gadget->name);
254
255 ret = usb_string_ids_tab(cdev, gfs_strings); 233 ret = usb_string_ids_tab(cdev, gfs_strings);
256 if (unlikely(ret < 0)) 234 if (unlikely(ret < 0))
257 goto error; 235 goto error;
258 236
259 gfs_dev_desc.iManufacturer = gfs_strings[GFS_STRING_MANUFACTURER_IDX].id;
260 gfs_dev_desc.iProduct = gfs_strings[GFS_STRING_PRODUCT_IDX].id;
261
262 ret = functionfs_bind(gfs_ffs_data, cdev); 237 ret = functionfs_bind(gfs_ffs_data, cdev);
263 if (unlikely(ret < 0)) 238 if (unlikely(ret < 0))
264 goto error; 239 goto error;
@@ -266,14 +241,12 @@ static int gfs_bind(struct usb_composite_dev *cdev)
266 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) { 241 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
267 struct gfs_configuration *c = gfs_configurations + i; 242 struct gfs_configuration *c = gfs_configurations + i;
268 243
269 ret = GFS_STRING_FIRST_CONFIG_IDX + i; 244 c->c.label = gfs_strings[i].s;
270 c->c.label = gfs_strings[ret].s; 245 c->c.iConfiguration = gfs_strings[i].id;
271 c->c.iConfiguration = gfs_strings[ret].id;
272 c->c.bind = gfs_do_config;
273 c->c.bConfigurationValue = 1 + i; 246 c->c.bConfigurationValue = 1 + i;
274 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER; 247 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
275 248
276 ret = usb_add_config(cdev, &c->c); 249 ret = usb_add_config(cdev, &c->c, gfs_do_config);
277 if (unlikely(ret < 0)) 250 if (unlikely(ret < 0))
278 goto error_unbind; 251 goto error_unbind;
279 } 252 }
@@ -293,13 +266,14 @@ static int gfs_unbind(struct usb_composite_dev *cdev)
293{ 266{
294 ENTER(); 267 ENTER();
295 268
296 /* We may have been called in an error recovery frem 269 /*
270 * We may have been called in an error recovery from
297 * composite_bind() after gfs_unbind() failure so we need to 271 * composite_bind() after gfs_unbind() failure so we need to
298 * check if gfs_ffs_data is not NULL since gfs_bind() handles 272 * check if gfs_ffs_data is not NULL since gfs_bind() handles
299 * all error recovery itself. I'd rather we werent called 273 * all error recovery itself. I'd rather we werent called
300 * from composite on orror recovery, but what you're gonna 274 * from composite on orror recovery, but what you're gonna
301 * do...? */ 275 * do...?
302 276 */
303 if (gfs_ffs_data) { 277 if (gfs_ffs_data) {
304 gether_cleanup(); 278 gether_cleanup();
305 functionfs_unbind(gfs_ffs_data); 279 functionfs_unbind(gfs_ffs_data);
@@ -309,7 +283,6 @@ static int gfs_unbind(struct usb_composite_dev *cdev)
309 return 0; 283 return 0;
310} 284}
311 285
312
313static int gfs_do_config(struct usb_configuration *c) 286static int gfs_do_config(struct usb_configuration *c)
314{ 287{
315 struct gfs_configuration *gc = 288 struct gfs_configuration *gc =
@@ -334,26 +307,29 @@ static int gfs_do_config(struct usb_configuration *c)
334 if (unlikely(ret < 0)) 307 if (unlikely(ret < 0))
335 return ret; 308 return ret;
336 309
337 /* After previous do_configs there may be some invalid 310 /*
311 * After previous do_configs there may be some invalid
338 * pointers in c->interface array. This happens every time 312 * pointers in c->interface array. This happens every time
339 * a user space function with fewer interfaces than a user 313 * a user space function with fewer interfaces than a user
340 * space function that was run before the new one is run. The 314 * space function that was run before the new one is run. The
341 * compasit's set_config() assumes that if there is no more 315 * compasit's set_config() assumes that if there is no more
342 * then MAX_CONFIG_INTERFACES interfaces in a configuration 316 * then MAX_CONFIG_INTERFACES interfaces in a configuration
343 * then there is a NULL pointer after the last interface in 317 * then there is a NULL pointer after the last interface in
344 * c->interface array. We need to make sure this is true. */ 318 * c->interface array. We need to make sure this is true.
319 */
345 if (c->next_interface_id < ARRAY_SIZE(c->interface)) 320 if (c->next_interface_id < ARRAY_SIZE(c->interface))
346 c->interface[c->next_interface_id] = NULL; 321 c->interface[c->next_interface_id] = NULL;
347 322
348 return 0; 323 return 0;
349} 324}
350 325
351
352#ifdef CONFIG_USB_FUNCTIONFS_ETH 326#ifdef CONFIG_USB_FUNCTIONFS_ETH
327
353static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 328static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
354{ 329{
355 return can_support_ecm(c->cdev->gadget) 330 return can_support_ecm(c->cdev->gadget)
356 ? ecm_bind_config(c, ethaddr) 331 ? ecm_bind_config(c, ethaddr)
357 : geth_bind_config(c, ethaddr); 332 : geth_bind_config(c, ethaddr);
358} 333}
334
359#endif 335#endif
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index e511fec9f26d..bcdac7c73e89 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -45,12 +45,6 @@
45#define gadget_is_goku(g) 0 45#define gadget_is_goku(g) 0
46#endif 46#endif
47 47
48#ifdef CONFIG_USB_GADGET_LH7A40X
49#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name)
50#else
51#define gadget_is_lh7a40x(g) 0
52#endif
53
54#ifdef CONFIG_USB_GADGET_OMAP 48#ifdef CONFIG_USB_GADGET_OMAP
55#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name) 49#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name)
56#else 50#else
@@ -96,7 +90,7 @@
96 90
97/* Mentor high speed "dual role" controller, in peripheral role */ 91/* Mentor high speed "dual role" controller, in peripheral role */
98#ifdef CONFIG_USB_GADGET_MUSB_HDRC 92#ifdef CONFIG_USB_GADGET_MUSB_HDRC
99#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name) 93#define gadget_is_musbhdrc(g) !strcmp("musb-hdrc", (g)->name)
100#else 94#else
101#define gadget_is_musbhdrc(g) 0 95#define gadget_is_musbhdrc(g) 0
102#endif 96#endif
@@ -120,10 +114,10 @@
120#define gadget_is_fsl_qe(g) 0 114#define gadget_is_fsl_qe(g) 0
121#endif 115#endif
122 116
123#ifdef CONFIG_USB_GADGET_CI13XXX 117#ifdef CONFIG_USB_GADGET_CI13XXX_PCI
124#define gadget_is_ci13xxx(g) (!strcmp("ci13xxx_udc", (g)->name)) 118#define gadget_is_ci13xxx_pci(g) (!strcmp("ci13xxx_pci", (g)->name))
125#else 119#else
126#define gadget_is_ci13xxx(g) 0 120#define gadget_is_ci13xxx_pci(g) 0
127#endif 121#endif
128 122
129// CONFIG_USB_GADGET_SX2 123// CONFIG_USB_GADGET_SX2
@@ -142,6 +136,29 @@
142#define gadget_is_s3c_hsotg(g) 0 136#define gadget_is_s3c_hsotg(g) 0
143#endif 137#endif
144 138
139#ifdef CONFIG_USB_S3C_HSUDC
140#define gadget_is_s3c_hsudc(g) (!strcmp("s3c-hsudc", (g)->name))
141#else
142#define gadget_is_s3c_hsudc(g) 0
143#endif
144
145#ifdef CONFIG_USB_GADGET_EG20T
146#define gadget_is_pch(g) (!strcmp("pch_udc", (g)->name))
147#else
148#define gadget_is_pch(g) 0
149#endif
150
151#ifdef CONFIG_USB_GADGET_CI13XXX_MSM
152#define gadget_is_ci13xxx_msm(g) (!strcmp("ci13xxx_msm", (g)->name))
153#else
154#define gadget_is_ci13xxx_msm(g) 0
155#endif
156
157#ifdef CONFIG_USB_GADGET_RENESAS_USBHS
158#define gadget_is_renesas_usbhs(g) (!strcmp("renesas_usbhs_udc", (g)->name))
159#else
160#define gadget_is_renesas_usbhs(g) 0
161#endif
145 162
146/** 163/**
147 * usb_gadget_controller_number - support bcdDevice id convention 164 * usb_gadget_controller_number - support bcdDevice id convention
@@ -170,8 +187,6 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
170 return 0x06; 187 return 0x06;
171 else if (gadget_is_omap(gadget)) 188 else if (gadget_is_omap(gadget))
172 return 0x08; 189 return 0x08;
173 else if (gadget_is_lh7a40x(gadget))
174 return 0x09;
175 else if (gadget_is_pxa27x(gadget)) 190 else if (gadget_is_pxa27x(gadget))
176 return 0x11; 191 return 0x11;
177 else if (gadget_is_s3c2410(gadget)) 192 else if (gadget_is_s3c2410(gadget))
@@ -192,7 +207,7 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
192 return 0x21; 207 return 0x21;
193 else if (gadget_is_fsl_qe(gadget)) 208 else if (gadget_is_fsl_qe(gadget))
194 return 0x22; 209 return 0x22;
195 else if (gadget_is_ci13xxx(gadget)) 210 else if (gadget_is_ci13xxx_pci(gadget))
196 return 0x23; 211 return 0x23;
197 else if (gadget_is_langwell(gadget)) 212 else if (gadget_is_langwell(gadget))
198 return 0x24; 213 return 0x24;
@@ -200,6 +215,15 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
200 return 0x25; 215 return 0x25;
201 else if (gadget_is_s3c_hsotg(gadget)) 216 else if (gadget_is_s3c_hsotg(gadget))
202 return 0x26; 217 return 0x26;
218 else if (gadget_is_pch(gadget))
219 return 0x27;
220 else if (gadget_is_ci13xxx_msm(gadget))
221 return 0x28;
222 else if (gadget_is_renesas_usbhs(gadget))
223 return 0x29;
224 else if (gadget_is_s3c_hsudc(gadget))
225 return 0x30;
226
203 return -ENOENT; 227 return -ENOENT;
204} 228}
205 229
diff --git a/drivers/usb/gadget/gmidi.c b/drivers/usb/gadget/gmidi.c
index 1b413a5cc3f6..47b86b99d449 100644
--- a/drivers/usb/gadget/gmidi.c
+++ b/drivers/usb/gadget/gmidi.c
@@ -67,7 +67,7 @@ MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
67module_param(id, charp, 0444); 67module_param(id, charp, 0444);
68MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter."); 68MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
69 69
70/* Some systems will want different product identifers published in the 70/* Some systems will want different product identifiers published in the
71 * device descriptor, either numbers or strings or both. These string 71 * device descriptor, either numbers or strings or both. These string
72 * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 72 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
73 */ 73 */
@@ -1157,7 +1157,7 @@ fail:
1157/* 1157/*
1158 * Creates an output endpoint, and initializes output ports. 1158 * Creates an output endpoint, and initializes output ports.
1159 */ 1159 */
1160static int __ref gmidi_bind(struct usb_gadget *gadget) 1160static int __init gmidi_bind(struct usb_gadget *gadget)
1161{ 1161{
1162 struct gmidi_device *dev; 1162 struct gmidi_device *dev;
1163 struct usb_ep *in_ep, *out_ep; 1163 struct usb_ep *in_ep, *out_ep;
@@ -1292,7 +1292,6 @@ static void gmidi_resume(struct usb_gadget *gadget)
1292static struct usb_gadget_driver gmidi_driver = { 1292static struct usb_gadget_driver gmidi_driver = {
1293 .speed = USB_SPEED_FULL, 1293 .speed = USB_SPEED_FULL,
1294 .function = (char *)longname, 1294 .function = (char *)longname,
1295 .bind = gmidi_bind,
1296 .unbind = gmidi_unbind, 1295 .unbind = gmidi_unbind,
1297 1296
1298 .setup = gmidi_setup, 1297 .setup = gmidi_setup,
@@ -1309,7 +1308,7 @@ static struct usb_gadget_driver gmidi_driver = {
1309 1308
1310static int __init gmidi_init(void) 1309static int __init gmidi_init(void)
1311{ 1310{
1312 return usb_gadget_register_driver(&gmidi_driver); 1311 return usb_gadget_probe_driver(&gmidi_driver, gmidi_bind);
1313} 1312}
1314module_init(gmidi_init); 1313module_init(gmidi_init);
1315 1314
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index 1088d08c7ed8..bf6e11c758d5 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -38,6 +38,7 @@
38#include <linux/device.h> 38#include <linux/device.h>
39#include <linux/usb/ch9.h> 39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h> 40#include <linux/usb/gadget.h>
41#include <linux/prefetch.h>
41 42
42#include <asm/byteorder.h> 43#include <asm/byteorder.h>
43#include <asm/io.h> 44#include <asm/io.h>
@@ -1343,14 +1344,15 @@ static struct goku_udc *the_controller;
1343 * disconnect is reported. then a host may connect again, or 1344 * disconnect is reported. then a host may connect again, or
1344 * the driver might get unbound. 1345 * the driver might get unbound.
1345 */ 1346 */
1346int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1347int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1348 int (*bind)(struct usb_gadget *))
1347{ 1349{
1348 struct goku_udc *dev = the_controller; 1350 struct goku_udc *dev = the_controller;
1349 int retval; 1351 int retval;
1350 1352
1351 if (!driver 1353 if (!driver
1352 || driver->speed < USB_SPEED_FULL 1354 || driver->speed < USB_SPEED_FULL
1353 || !driver->bind 1355 || !bind
1354 || !driver->disconnect 1356 || !driver->disconnect
1355 || !driver->setup) 1357 || !driver->setup)
1356 return -EINVAL; 1358 return -EINVAL;
@@ -1363,7 +1365,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1363 driver->driver.bus = NULL; 1365 driver->driver.bus = NULL;
1364 dev->driver = driver; 1366 dev->driver = driver;
1365 dev->gadget.dev.driver = &driver->driver; 1367 dev->gadget.dev.driver = &driver->driver;
1366 retval = driver->bind(&dev->gadget); 1368 retval = bind(&dev->gadget);
1367 if (retval) { 1369 if (retval) {
1368 DBG(dev, "bind to driver %s --> error %d\n", 1370 DBG(dev, "bind to driver %s --> error %d\n",
1369 driver->driver.name, retval); 1371 driver->driver.name, retval);
@@ -1380,7 +1382,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1380 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name); 1382 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1381 return 0; 1383 return 0;
1382} 1384}
1383EXPORT_SYMBOL(usb_gadget_register_driver); 1385EXPORT_SYMBOL(usb_gadget_probe_driver);
1384 1386
1385static void 1387static void
1386stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver) 1388stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
@@ -1744,7 +1746,8 @@ static void goku_remove(struct pci_dev *pdev)
1744 pci_resource_len (pdev, 0)); 1746 pci_resource_len (pdev, 0));
1745 if (dev->enabled) 1747 if (dev->enabled)
1746 pci_disable_device(pdev); 1748 pci_disable_device(pdev);
1747 device_unregister(&dev->gadget.dev); 1749 if (dev->registered)
1750 device_unregister(&dev->gadget.dev);
1748 1751
1749 pci_set_drvdata(pdev, NULL); 1752 pci_set_drvdata(pdev, NULL);
1750 dev->regs = NULL; 1753 dev->regs = NULL;
@@ -1774,7 +1777,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1774 if (!pdev->irq) { 1777 if (!pdev->irq) {
1775 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev)); 1778 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1776 retval = -ENODEV; 1779 retval = -ENODEV;
1777 goto done; 1780 goto err;
1778 } 1781 }
1779 1782
1780 /* alloc, and start init */ 1783 /* alloc, and start init */
@@ -1782,7 +1785,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1782 if (dev == NULL){ 1785 if (dev == NULL){
1783 pr_debug("enomem %s\n", pci_name(pdev)); 1786 pr_debug("enomem %s\n", pci_name(pdev));
1784 retval = -ENOMEM; 1787 retval = -ENOMEM;
1785 goto done; 1788 goto err;
1786 } 1789 }
1787 1790
1788 spin_lock_init(&dev->lock); 1791 spin_lock_init(&dev->lock);
@@ -1800,7 +1803,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1800 retval = pci_enable_device(pdev); 1803 retval = pci_enable_device(pdev);
1801 if (retval < 0) { 1804 if (retval < 0) {
1802 DBG(dev, "can't enable, %d\n", retval); 1805 DBG(dev, "can't enable, %d\n", retval);
1803 goto done; 1806 goto err;
1804 } 1807 }
1805 dev->enabled = 1; 1808 dev->enabled = 1;
1806 1809
@@ -1809,7 +1812,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1809 if (!request_mem_region(resource, len, driver_name)) { 1812 if (!request_mem_region(resource, len, driver_name)) {
1810 DBG(dev, "controller already in use\n"); 1813 DBG(dev, "controller already in use\n");
1811 retval = -EBUSY; 1814 retval = -EBUSY;
1812 goto done; 1815 goto err;
1813 } 1816 }
1814 dev->got_region = 1; 1817 dev->got_region = 1;
1815 1818
@@ -1817,7 +1820,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1817 if (base == NULL) { 1820 if (base == NULL) {
1818 DBG(dev, "can't map memory\n"); 1821 DBG(dev, "can't map memory\n");
1819 retval = -EFAULT; 1822 retval = -EFAULT;
1820 goto done; 1823 goto err;
1821 } 1824 }
1822 dev->regs = (struct goku_udc_regs __iomem *) base; 1825 dev->regs = (struct goku_udc_regs __iomem *) base;
1823 1826
@@ -1833,7 +1836,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1833 driver_name, dev) != 0) { 1836 driver_name, dev) != 0) {
1834 DBG(dev, "request interrupt %d failed\n", pdev->irq); 1837 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1835 retval = -EBUSY; 1838 retval = -EBUSY;
1836 goto done; 1839 goto err;
1837 } 1840 }
1838 dev->got_irq = 1; 1841 dev->got_irq = 1;
1839 if (use_dma) 1842 if (use_dma)
@@ -1844,13 +1847,16 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1844 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev); 1847 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1845#endif 1848#endif
1846 1849
1847 /* done */
1848 the_controller = dev; 1850 the_controller = dev;
1849 retval = device_register(&dev->gadget.dev); 1851 retval = device_register(&dev->gadget.dev);
1850 if (retval == 0) 1852 if (retval) {
1851 return 0; 1853 put_device(&dev->gadget.dev);
1854 goto err;
1855 }
1856 dev->registered = 1;
1857 return 0;
1852 1858
1853done: 1859err:
1854 if (dev) 1860 if (dev)
1855 goku_remove (pdev); 1861 goku_remove (pdev);
1856 return retval; 1862 return retval;
diff --git a/drivers/usb/gadget/goku_udc.h b/drivers/usb/gadget/goku_udc.h
index 566cb2319056..e7e0c69d3b1f 100644
--- a/drivers/usb/gadget/goku_udc.h
+++ b/drivers/usb/gadget/goku_udc.h
@@ -251,7 +251,8 @@ struct goku_udc {
251 got_region:1, 251 got_region:1,
252 req_config:1, 252 req_config:1,
253 configured:1, 253 configured:1,
254 enabled:1; 254 enabled:1,
255 registered:1;
255 256
256 /* pci state used to access those endpoints */ 257 /* pci state used to access those endpoints */
257 struct pci_dev *pdev; 258 struct pci_dev *pdev;
diff --git a/drivers/usb/gadget/hid.c b/drivers/usb/gadget/hid.c
index 735495bf8411..2523e54097bd 100644
--- a/drivers/usb/gadget/hid.c
+++ b/drivers/usb/gadget/hid.c
@@ -127,7 +127,7 @@ static struct usb_gadget_strings *dev_strings[] = {
127 127
128/****************************** Configurations ******************************/ 128/****************************** Configurations ******************************/
129 129
130static int __ref do_config(struct usb_configuration *c) 130static int __init do_config(struct usb_configuration *c)
131{ 131{
132 struct hidg_func_node *e; 132 struct hidg_func_node *e;
133 int func = 0, status = 0; 133 int func = 0, status = 0;
@@ -148,7 +148,6 @@ static int __ref do_config(struct usb_configuration *c)
148 148
149static struct usb_configuration config_driver = { 149static struct usb_configuration config_driver = {
150 .label = "HID Gadget", 150 .label = "HID Gadget",
151 .bind = do_config,
152 .bConfigurationValue = 1, 151 .bConfigurationValue = 1,
153 /* .iConfiguration = DYNAMIC */ 152 /* .iConfiguration = DYNAMIC */
154 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 153 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -156,7 +155,7 @@ static struct usb_configuration config_driver = {
156 155
157/****************************** Gadget Bind ******************************/ 156/****************************** Gadget Bind ******************************/
158 157
159static int __ref hid_bind(struct usb_composite_dev *cdev) 158static int __init hid_bind(struct usb_composite_dev *cdev)
160{ 159{
161 struct usb_gadget *gadget = cdev->gadget; 160 struct usb_gadget *gadget = cdev->gadget;
162 struct list_head *tmp; 161 struct list_head *tmp;
@@ -201,7 +200,7 @@ static int __ref hid_bind(struct usb_composite_dev *cdev)
201 device_desc.iProduct = status; 200 device_desc.iProduct = status;
202 201
203 /* register our configuration */ 202 /* register our configuration */
204 status = usb_add_config(cdev, &config_driver); 203 status = usb_add_config(cdev, &config_driver, do_config);
205 if (status < 0) 204 if (status < 0)
206 return status; 205 return status;
207 206
@@ -256,7 +255,6 @@ static struct usb_composite_driver hidg_driver = {
256 .name = "g_hid", 255 .name = "g_hid",
257 .dev = &device_desc, 256 .dev = &device_desc,
258 .strings = dev_strings, 257 .strings = dev_strings,
259 .bind = hid_bind,
260 .unbind = __exit_p(hid_unbind), 258 .unbind = __exit_p(hid_unbind),
261}; 259};
262 260
@@ -282,7 +280,7 @@ static int __init hidg_init(void)
282 if (status < 0) 280 if (status < 0)
283 return status; 281 return status;
284 282
285 status = usb_composite_register(&hidg_driver); 283 status = usb_composite_probe(&hidg_driver, hid_bind);
286 if (status < 0) 284 if (status < 0)
287 platform_driver_unregister(&hidg_plat_driver); 285 platform_driver_unregister(&hidg_plat_driver);
288 286
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index e743122fcd93..ade40066decf 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -30,6 +30,7 @@
30#include <linux/delay.h> 30#include <linux/delay.h>
31#include <linux/timer.h> 31#include <linux/timer.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/prefetch.h>
33 34
34#include <linux/usb/ch9.h> 35#include <linux/usb/ch9.h>
35#include <linux/usb/gadget.h> 36#include <linux/usb/gadget.h>
@@ -1191,13 +1192,17 @@ static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1191 return IRQ_HANDLED; 1192 return IRQ_HANDLED;
1192} 1193}
1193 1194
1195#ifndef MX1_INT_USBD0
1196#define MX1_INT_USBD0 MX1_USBD_INT0
1197#endif
1198
1194static irqreturn_t imx_udc_bulk_irq(int irq, void *dev) 1199static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1195{ 1200{
1196 struct imx_udc_struct *imx_usb = dev; 1201 struct imx_udc_struct *imx_usb = dev;
1197 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0]; 1202 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - MX1_INT_USBD0];
1198 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); 1203 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1199 1204
1200 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev); 1205 dump_ep_intr(__func__, irq - MX1_INT_USBD0, intr, imx_usb->dev);
1201 1206
1202 if (!imx_usb->driver) { 1207 if (!imx_usb->driver) {
1203 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); 1208 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
@@ -1316,17 +1321,18 @@ static struct imx_udc_struct controller = {
1316}; 1321};
1317 1322
1318/******************************************************************************* 1323/*******************************************************************************
1319 * USB gadged driver functions 1324 * USB gadget driver functions
1320 ******************************************************************************* 1325 *******************************************************************************
1321 */ 1326 */
1322int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1327int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1328 int (*bind)(struct usb_gadget *))
1323{ 1329{
1324 struct imx_udc_struct *imx_usb = &controller; 1330 struct imx_udc_struct *imx_usb = &controller;
1325 int retval; 1331 int retval;
1326 1332
1327 if (!driver 1333 if (!driver
1328 || driver->speed < USB_SPEED_FULL 1334 || driver->speed < USB_SPEED_FULL
1329 || !driver->bind 1335 || !bind
1330 || !driver->disconnect 1336 || !driver->disconnect
1331 || !driver->setup) 1337 || !driver->setup)
1332 return -EINVAL; 1338 return -EINVAL;
@@ -1342,7 +1348,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1342 retval = device_add(&imx_usb->gadget.dev); 1348 retval = device_add(&imx_usb->gadget.dev);
1343 if (retval) 1349 if (retval)
1344 goto fail; 1350 goto fail;
1345 retval = driver->bind(&imx_usb->gadget); 1351 retval = bind(&imx_usb->gadget);
1346 if (retval) { 1352 if (retval) {
1347 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n", 1353 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
1348 __func__, driver->driver.name, retval); 1354 __func__, driver->driver.name, retval);
@@ -1362,7 +1368,7 @@ fail:
1362 imx_usb->gadget.dev.driver = NULL; 1368 imx_usb->gadget.dev.driver = NULL;
1363 return retval; 1369 return retval;
1364} 1370}
1365EXPORT_SYMBOL(usb_gadget_register_driver); 1371EXPORT_SYMBOL(usb_gadget_probe_driver);
1366 1372
1367int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1373int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1368{ 1374{
diff --git a/drivers/usb/gadget/imx_udc.h b/drivers/usb/gadget/imx_udc.h
index b48ad59603d1..7136c242b4ec 100644
--- a/drivers/usb/gadget/imx_udc.h
+++ b/drivers/usb/gadget/imx_udc.h
@@ -23,9 +23,6 @@
23/* Helper macros */ 23/* Helper macros */
24#define EP_NO(ep) ((ep->bEndpointAddress) & ~USB_DIR_IN) /* IN:1, OUT:0 */ 24#define EP_NO(ep) ((ep->bEndpointAddress) & ~USB_DIR_IN) /* IN:1, OUT:0 */
25#define EP_DIR(ep) ((ep->bEndpointAddress) & USB_DIR_IN ? 1 : 0) 25#define EP_DIR(ep) ((ep->bEndpointAddress) & USB_DIR_IN ? 1 : 0)
26#define irq_to_ep(irq) (((irq) >= USBD_INT0) || ((irq) <= USBD_INT6) \
27 ? ((irq) - USBD_INT0) : (USBD_INT6)) /*should not happen*/
28#define ep_to_irq(ep) (EP_NO((ep)) + USBD_INT0)
29#define IMX_USB_NB_EP 6 26#define IMX_USB_NB_EP 6
30 27
31/* Driver structures */ 28/* Driver structures */
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index fc35406fc80c..a56876aaf76c 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -33,7 +33,6 @@
33#include <linux/sched.h> 33#include <linux/sched.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/poll.h> 35#include <linux/poll.h>
36#include <linux/smp_lock.h>
37 36
38#include <linux/device.h> 37#include <linux/device.h>
39#include <linux/moduleparam.h> 38#include <linux/moduleparam.h>
@@ -387,8 +386,10 @@ ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
387 386
388 /* halt any endpoint by doing a "wrong direction" i/o call */ 387 /* halt any endpoint by doing a "wrong direction" i/o call */
389 if (usb_endpoint_dir_in(&data->desc)) { 388 if (usb_endpoint_dir_in(&data->desc)) {
390 if (usb_endpoint_xfer_isoc(&data->desc)) 389 if (usb_endpoint_xfer_isoc(&data->desc)) {
390 mutex_unlock(&data->lock);
391 return -EINVAL; 391 return -EINVAL;
392 }
392 DBG (data->dev, "%s halt\n", data->name); 393 DBG (data->dev, "%s halt\n", data->name);
393 spin_lock_irq (&data->dev->lock); 394 spin_lock_irq (&data->dev->lock);
394 if (likely (data->ep != NULL)) 395 if (likely (data->ep != NULL))
@@ -430,8 +431,10 @@ ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
430 431
431 /* halt any endpoint by doing a "wrong direction" i/o call */ 432 /* halt any endpoint by doing a "wrong direction" i/o call */
432 if (!usb_endpoint_dir_in(&data->desc)) { 433 if (!usb_endpoint_dir_in(&data->desc)) {
433 if (usb_endpoint_xfer_isoc(&data->desc)) 434 if (usb_endpoint_xfer_isoc(&data->desc)) {
435 mutex_unlock(&data->lock);
434 return -EINVAL; 436 return -EINVAL;
437 }
435 DBG (data->dev, "%s halt\n", data->name); 438 DBG (data->dev, "%s halt\n", data->name);
436 spin_lock_irq (&data->dev->lock); 439 spin_lock_irq (&data->dev->lock);
437 if (likely (data->ep != NULL)) 440 if (likely (data->ep != NULL))
@@ -1775,7 +1778,6 @@ static struct usb_gadget_driver gadgetfs_driver = {
1775 .speed = USB_SPEED_FULL, 1778 .speed = USB_SPEED_FULL,
1776#endif 1779#endif
1777 .function = (char *) driver_desc, 1780 .function = (char *) driver_desc,
1778 .bind = gadgetfs_bind,
1779 .unbind = gadgetfs_unbind, 1781 .unbind = gadgetfs_unbind,
1780 .setup = gadgetfs_setup, 1782 .setup = gadgetfs_setup,
1781 .disconnect = gadgetfs_disconnect, 1783 .disconnect = gadgetfs_disconnect,
@@ -1798,7 +1800,6 @@ static int gadgetfs_probe (struct usb_gadget *gadget)
1798 1800
1799static struct usb_gadget_driver probe_driver = { 1801static struct usb_gadget_driver probe_driver = {
1800 .speed = USB_SPEED_HIGH, 1802 .speed = USB_SPEED_HIGH,
1801 .bind = gadgetfs_probe,
1802 .unbind = gadgetfs_nop, 1803 .unbind = gadgetfs_nop,
1803 .setup = (void *)gadgetfs_nop, 1804 .setup = (void *)gadgetfs_nop,
1804 .disconnect = gadgetfs_nop, 1805 .disconnect = gadgetfs_nop,
@@ -1908,7 +1909,7 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1908 1909
1909 /* triggers gadgetfs_bind(); then we can enumerate. */ 1910 /* triggers gadgetfs_bind(); then we can enumerate. */
1910 spin_unlock_irq (&dev->lock); 1911 spin_unlock_irq (&dev->lock);
1911 value = usb_gadget_register_driver (&gadgetfs_driver); 1912 value = usb_gadget_probe_driver(&gadgetfs_driver, gadgetfs_bind);
1912 if (value != 0) { 1913 if (value != 0) {
1913 kfree (dev->buf); 1914 kfree (dev->buf);
1914 dev->buf = NULL; 1915 dev->buf = NULL;
@@ -1994,6 +1995,7 @@ gadgetfs_make_inode (struct super_block *sb,
1994 struct inode *inode = new_inode (sb); 1995 struct inode *inode = new_inode (sb);
1995 1996
1996 if (inode) { 1997 if (inode) {
1998 inode->i_ino = get_next_ino();
1997 inode->i_mode = mode; 1999 inode->i_mode = mode;
1998 inode->i_uid = default_uid; 2000 inode->i_uid = default_uid;
1999 inode->i_gid = default_gid; 2001 inode->i_gid = default_gid;
@@ -2047,7 +2049,7 @@ gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
2047 return -ESRCH; 2049 return -ESRCH;
2048 2050
2049 /* fake probe to determine $CHIP */ 2051 /* fake probe to determine $CHIP */
2050 (void) usb_gadget_register_driver (&probe_driver); 2052 (void) usb_gadget_probe_driver(&probe_driver, gadgetfs_probe);
2051 if (!CHIP) 2053 if (!CHIP)
2052 return -ENODEV; 2054 return -ENODEV;
2053 2055
@@ -2099,11 +2101,11 @@ enomem0:
2099} 2101}
2100 2102
2101/* "mount -t gadgetfs path /dev/gadget" ends up here */ 2103/* "mount -t gadgetfs path /dev/gadget" ends up here */
2102static int 2104static struct dentry *
2103gadgetfs_get_sb (struct file_system_type *t, int flags, 2105gadgetfs_mount (struct file_system_type *t, int flags,
2104 const char *path, void *opts, struct vfsmount *mnt) 2106 const char *path, void *opts)
2105{ 2107{
2106 return get_sb_single (t, flags, opts, gadgetfs_fill_super, mnt); 2108 return mount_single (t, flags, opts, gadgetfs_fill_super);
2107} 2109}
2108 2110
2109static void 2111static void
@@ -2121,7 +2123,7 @@ gadgetfs_kill_sb (struct super_block *sb)
2121static struct file_system_type gadgetfs_type = { 2123static struct file_system_type gadgetfs_type = {
2122 .owner = THIS_MODULE, 2124 .owner = THIS_MODULE,
2123 .name = shortname, 2125 .name = shortname,
2124 .get_sb = gadgetfs_get_sb, 2126 .mount = gadgetfs_mount,
2125 .kill_sb = gadgetfs_kill_sb, 2127 .kill_sb = gadgetfs_kill_sb,
2126}; 2128};
2127 2129
diff --git a/drivers/usb/gadget/langwell_udc.c b/drivers/usb/gadget/langwell_udc.c
index c2d2a201f84b..9cee88a43a73 100644
--- a/drivers/usb/gadget/langwell_udc.c
+++ b/drivers/usb/gadget/langwell_udc.c
@@ -19,7 +19,7 @@
19 19
20 20
21/* #undef DEBUG */ 21/* #undef DEBUG */
22/* #undef VERBOSE */ 22/* #undef VERBOSE_DEBUG */
23 23
24#if defined(CONFIG_USB_LANGWELL_OTG) 24#if defined(CONFIG_USB_LANGWELL_OTG)
25#define OTG_TRANSCEIVER 25#define OTG_TRANSCEIVER
@@ -77,141 +77,110 @@ langwell_ep0_desc = {
77/*-------------------------------------------------------------------------*/ 77/*-------------------------------------------------------------------------*/
78/* debugging */ 78/* debugging */
79 79
80#ifdef DEBUG 80#ifdef VERBOSE_DEBUG
81#define DBG(dev, fmt, args...) \
82 pr_debug("%s %s: " fmt , driver_name, \
83 pci_name(dev->pdev), ## args)
84#else
85#define DBG(dev, fmt, args...) \
86 do { } while (0)
87#endif /* DEBUG */
88
89
90#ifdef VERBOSE
91#define VDBG DBG
92#else
93#define VDBG(dev, fmt, args...) \
94 do { } while (0)
95#endif /* VERBOSE */
96
97
98#define ERROR(dev, fmt, args...) \
99 pr_err("%s %s: " fmt , driver_name, \
100 pci_name(dev->pdev), ## args)
101
102#define WARNING(dev, fmt, args...) \
103 pr_warning("%s %s: " fmt , driver_name, \
104 pci_name(dev->pdev), ## args)
105
106#define INFO(dev, fmt, args...) \
107 pr_info("%s %s: " fmt , driver_name, \
108 pci_name(dev->pdev), ## args)
109
110
111#ifdef VERBOSE
112static inline void print_all_registers(struct langwell_udc *dev) 81static inline void print_all_registers(struct langwell_udc *dev)
113{ 82{
114 int i; 83 int i;
115 84
116 /* Capability Registers */ 85 /* Capability Registers */
117 printk(KERN_DEBUG "Capability Registers (offset: " 86 dev_dbg(&dev->pdev->dev,
118 "0x%04x, length: 0x%08x)\n", 87 "Capability Registers (offset: 0x%04x, length: 0x%08x)\n",
119 CAP_REG_OFFSET, 88 CAP_REG_OFFSET, (u32)sizeof(struct langwell_cap_regs));
120 (u32)sizeof(struct langwell_cap_regs)); 89 dev_dbg(&dev->pdev->dev, "caplength=0x%02x\n",
121 printk(KERN_DEBUG "caplength=0x%02x\n",
122 readb(&dev->cap_regs->caplength)); 90 readb(&dev->cap_regs->caplength));
123 printk(KERN_DEBUG "hciversion=0x%04x\n", 91 dev_dbg(&dev->pdev->dev, "hciversion=0x%04x\n",
124 readw(&dev->cap_regs->hciversion)); 92 readw(&dev->cap_regs->hciversion));
125 printk(KERN_DEBUG "hcsparams=0x%08x\n", 93 dev_dbg(&dev->pdev->dev, "hcsparams=0x%08x\n",
126 readl(&dev->cap_regs->hcsparams)); 94 readl(&dev->cap_regs->hcsparams));
127 printk(KERN_DEBUG "hccparams=0x%08x\n", 95 dev_dbg(&dev->pdev->dev, "hccparams=0x%08x\n",
128 readl(&dev->cap_regs->hccparams)); 96 readl(&dev->cap_regs->hccparams));
129 printk(KERN_DEBUG "dciversion=0x%04x\n", 97 dev_dbg(&dev->pdev->dev, "dciversion=0x%04x\n",
130 readw(&dev->cap_regs->dciversion)); 98 readw(&dev->cap_regs->dciversion));
131 printk(KERN_DEBUG "dccparams=0x%08x\n", 99 dev_dbg(&dev->pdev->dev, "dccparams=0x%08x\n",
132 readl(&dev->cap_regs->dccparams)); 100 readl(&dev->cap_regs->dccparams));
133 101
134 /* Operational Registers */ 102 /* Operational Registers */
135 printk(KERN_DEBUG "Operational Registers (offset: " 103 dev_dbg(&dev->pdev->dev,
136 "0x%04x, length: 0x%08x)\n", 104 "Operational Registers (offset: 0x%04x, length: 0x%08x)\n",
137 OP_REG_OFFSET, 105 OP_REG_OFFSET, (u32)sizeof(struct langwell_op_regs));
138 (u32)sizeof(struct langwell_op_regs)); 106 dev_dbg(&dev->pdev->dev, "extsts=0x%08x\n",
139 printk(KERN_DEBUG "extsts=0x%08x\n",
140 readl(&dev->op_regs->extsts)); 107 readl(&dev->op_regs->extsts));
141 printk(KERN_DEBUG "extintr=0x%08x\n", 108 dev_dbg(&dev->pdev->dev, "extintr=0x%08x\n",
142 readl(&dev->op_regs->extintr)); 109 readl(&dev->op_regs->extintr));
143 printk(KERN_DEBUG "usbcmd=0x%08x\n", 110 dev_dbg(&dev->pdev->dev, "usbcmd=0x%08x\n",
144 readl(&dev->op_regs->usbcmd)); 111 readl(&dev->op_regs->usbcmd));
145 printk(KERN_DEBUG "usbsts=0x%08x\n", 112 dev_dbg(&dev->pdev->dev, "usbsts=0x%08x\n",
146 readl(&dev->op_regs->usbsts)); 113 readl(&dev->op_regs->usbsts));
147 printk(KERN_DEBUG "usbintr=0x%08x\n", 114 dev_dbg(&dev->pdev->dev, "usbintr=0x%08x\n",
148 readl(&dev->op_regs->usbintr)); 115 readl(&dev->op_regs->usbintr));
149 printk(KERN_DEBUG "frindex=0x%08x\n", 116 dev_dbg(&dev->pdev->dev, "frindex=0x%08x\n",
150 readl(&dev->op_regs->frindex)); 117 readl(&dev->op_regs->frindex));
151 printk(KERN_DEBUG "ctrldssegment=0x%08x\n", 118 dev_dbg(&dev->pdev->dev, "ctrldssegment=0x%08x\n",
152 readl(&dev->op_regs->ctrldssegment)); 119 readl(&dev->op_regs->ctrldssegment));
153 printk(KERN_DEBUG "deviceaddr=0x%08x\n", 120 dev_dbg(&dev->pdev->dev, "deviceaddr=0x%08x\n",
154 readl(&dev->op_regs->deviceaddr)); 121 readl(&dev->op_regs->deviceaddr));
155 printk(KERN_DEBUG "endpointlistaddr=0x%08x\n", 122 dev_dbg(&dev->pdev->dev, "endpointlistaddr=0x%08x\n",
156 readl(&dev->op_regs->endpointlistaddr)); 123 readl(&dev->op_regs->endpointlistaddr));
157 printk(KERN_DEBUG "ttctrl=0x%08x\n", 124 dev_dbg(&dev->pdev->dev, "ttctrl=0x%08x\n",
158 readl(&dev->op_regs->ttctrl)); 125 readl(&dev->op_regs->ttctrl));
159 printk(KERN_DEBUG "burstsize=0x%08x\n", 126 dev_dbg(&dev->pdev->dev, "burstsize=0x%08x\n",
160 readl(&dev->op_regs->burstsize)); 127 readl(&dev->op_regs->burstsize));
161 printk(KERN_DEBUG "txfilltuning=0x%08x\n", 128 dev_dbg(&dev->pdev->dev, "txfilltuning=0x%08x\n",
162 readl(&dev->op_regs->txfilltuning)); 129 readl(&dev->op_regs->txfilltuning));
163 printk(KERN_DEBUG "txttfilltuning=0x%08x\n", 130 dev_dbg(&dev->pdev->dev, "txttfilltuning=0x%08x\n",
164 readl(&dev->op_regs->txttfilltuning)); 131 readl(&dev->op_regs->txttfilltuning));
165 printk(KERN_DEBUG "ic_usb=0x%08x\n", 132 dev_dbg(&dev->pdev->dev, "ic_usb=0x%08x\n",
166 readl(&dev->op_regs->ic_usb)); 133 readl(&dev->op_regs->ic_usb));
167 printk(KERN_DEBUG "ulpi_viewport=0x%08x\n", 134 dev_dbg(&dev->pdev->dev, "ulpi_viewport=0x%08x\n",
168 readl(&dev->op_regs->ulpi_viewport)); 135 readl(&dev->op_regs->ulpi_viewport));
169 printk(KERN_DEBUG "configflag=0x%08x\n", 136 dev_dbg(&dev->pdev->dev, "configflag=0x%08x\n",
170 readl(&dev->op_regs->configflag)); 137 readl(&dev->op_regs->configflag));
171 printk(KERN_DEBUG "portsc1=0x%08x\n", 138 dev_dbg(&dev->pdev->dev, "portsc1=0x%08x\n",
172 readl(&dev->op_regs->portsc1)); 139 readl(&dev->op_regs->portsc1));
173 printk(KERN_DEBUG "devlc=0x%08x\n", 140 dev_dbg(&dev->pdev->dev, "devlc=0x%08x\n",
174 readl(&dev->op_regs->devlc)); 141 readl(&dev->op_regs->devlc));
175 printk(KERN_DEBUG "otgsc=0x%08x\n", 142 dev_dbg(&dev->pdev->dev, "otgsc=0x%08x\n",
176 readl(&dev->op_regs->otgsc)); 143 readl(&dev->op_regs->otgsc));
177 printk(KERN_DEBUG "usbmode=0x%08x\n", 144 dev_dbg(&dev->pdev->dev, "usbmode=0x%08x\n",
178 readl(&dev->op_regs->usbmode)); 145 readl(&dev->op_regs->usbmode));
179 printk(KERN_DEBUG "endptnak=0x%08x\n", 146 dev_dbg(&dev->pdev->dev, "endptnak=0x%08x\n",
180 readl(&dev->op_regs->endptnak)); 147 readl(&dev->op_regs->endptnak));
181 printk(KERN_DEBUG "endptnaken=0x%08x\n", 148 dev_dbg(&dev->pdev->dev, "endptnaken=0x%08x\n",
182 readl(&dev->op_regs->endptnaken)); 149 readl(&dev->op_regs->endptnaken));
183 printk(KERN_DEBUG "endptsetupstat=0x%08x\n", 150 dev_dbg(&dev->pdev->dev, "endptsetupstat=0x%08x\n",
184 readl(&dev->op_regs->endptsetupstat)); 151 readl(&dev->op_regs->endptsetupstat));
185 printk(KERN_DEBUG "endptprime=0x%08x\n", 152 dev_dbg(&dev->pdev->dev, "endptprime=0x%08x\n",
186 readl(&dev->op_regs->endptprime)); 153 readl(&dev->op_regs->endptprime));
187 printk(KERN_DEBUG "endptflush=0x%08x\n", 154 dev_dbg(&dev->pdev->dev, "endptflush=0x%08x\n",
188 readl(&dev->op_regs->endptflush)); 155 readl(&dev->op_regs->endptflush));
189 printk(KERN_DEBUG "endptstat=0x%08x\n", 156 dev_dbg(&dev->pdev->dev, "endptstat=0x%08x\n",
190 readl(&dev->op_regs->endptstat)); 157 readl(&dev->op_regs->endptstat));
191 printk(KERN_DEBUG "endptcomplete=0x%08x\n", 158 dev_dbg(&dev->pdev->dev, "endptcomplete=0x%08x\n",
192 readl(&dev->op_regs->endptcomplete)); 159 readl(&dev->op_regs->endptcomplete));
193 160
194 for (i = 0; i < dev->ep_max / 2; i++) { 161 for (i = 0; i < dev->ep_max / 2; i++) {
195 printk(KERN_DEBUG "endptctrl[%d]=0x%08x\n", 162 dev_dbg(&dev->pdev->dev, "endptctrl[%d]=0x%08x\n",
196 i, readl(&dev->op_regs->endptctrl[i])); 163 i, readl(&dev->op_regs->endptctrl[i]));
197 } 164 }
198} 165}
199#endif /* VERBOSE */ 166#else
167
168#define print_all_registers(dev) do { } while (0)
169
170#endif /* VERBOSE_DEBUG */
200 171
201 172
202/*-------------------------------------------------------------------------*/ 173/*-------------------------------------------------------------------------*/
203 174
204#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out") 175#define is_in(ep) (((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir == \
176 USB_DIR_IN) : (usb_endpoint_dir_in((ep)->desc)))
205 177
206#define is_in(ep) (((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir == \ 178#define DIR_STRING(ep) (is_in(ep) ? "in" : "out")
207 USB_DIR_IN) : ((ep)->desc->bEndpointAddress \
208 & USB_DIR_IN) == USB_DIR_IN)
209 179
210 180
211#ifdef DEBUG 181static char *type_string(const struct usb_endpoint_descriptor *desc)
212static char *type_string(u8 bmAttributes)
213{ 182{
214 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) { 183 switch (usb_endpoint_type(desc)) {
215 case USB_ENDPOINT_XFER_BULK: 184 case USB_ENDPOINT_XFER_BULK:
216 return "bulk"; 185 return "bulk";
217 case USB_ENDPOINT_XFER_ISOC: 186 case USB_ENDPOINT_XFER_ISOC:
@@ -222,7 +191,6 @@ static char *type_string(u8 bmAttributes)
222 191
223 return "control"; 192 return "control";
224} 193}
225#endif
226 194
227 195
228/* configure endpoint control registers */ 196/* configure endpoint control registers */
@@ -233,7 +201,7 @@ static void ep_reset(struct langwell_ep *ep, unsigned char ep_num,
233 u32 endptctrl; 201 u32 endptctrl;
234 202
235 dev = ep->dev; 203 dev = ep->dev;
236 VDBG(dev, "---> %s()\n", __func__); 204 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
237 205
238 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]); 206 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
239 if (is_in) { /* TX */ 207 if (is_in) { /* TX */
@@ -250,7 +218,7 @@ static void ep_reset(struct langwell_ep *ep, unsigned char ep_num,
250 218
251 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]); 219 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
252 220
253 VDBG(dev, "<--- %s()\n", __func__); 221 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
254} 222}
255 223
256 224
@@ -260,7 +228,7 @@ static void ep0_reset(struct langwell_udc *dev)
260 struct langwell_ep *ep; 228 struct langwell_ep *ep;
261 int i; 229 int i;
262 230
263 VDBG(dev, "---> %s()\n", __func__); 231 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
264 232
265 /* ep0 in and out */ 233 /* ep0 in and out */
266 for (i = 0; i < 2; i++) { 234 for (i = 0; i < 2; i++) {
@@ -274,17 +242,18 @@ static void ep0_reset(struct langwell_udc *dev)
274 ep->dqh->dqh_ios = 1; 242 ep->dqh->dqh_ios = 1;
275 ep->dqh->dqh_mpl = EP0_MAX_PKT_SIZE; 243 ep->dqh->dqh_mpl = EP0_MAX_PKT_SIZE;
276 244
277 /* FIXME: enable ep0-in HW zero length termination select */ 245 /* enable ep0-in HW zero length termination select */
278 if (is_in(ep)) 246 if (is_in(ep))
279 ep->dqh->dqh_zlt = 0; 247 ep->dqh->dqh_zlt = 0;
280 ep->dqh->dqh_mult = 0; 248 ep->dqh->dqh_mult = 0;
281 249
250 ep->dqh->dtd_next = DTD_TERM;
251
282 /* configure ep0 control registers */ 252 /* configure ep0 control registers */
283 ep_reset(&dev->ep[0], 0, i, USB_ENDPOINT_XFER_CONTROL); 253 ep_reset(&dev->ep[0], 0, i, USB_ENDPOINT_XFER_CONTROL);
284 } 254 }
285 255
286 VDBG(dev, "<--- %s()\n", __func__); 256 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
287 return;
288} 257}
289 258
290 259
@@ -300,12 +269,12 @@ static int langwell_ep_enable(struct usb_ep *_ep,
300 struct langwell_ep *ep; 269 struct langwell_ep *ep;
301 u16 max = 0; 270 u16 max = 0;
302 unsigned long flags; 271 unsigned long flags;
303 int retval = 0; 272 int i, retval = 0;
304 unsigned char zlt, ios = 0, mult = 0; 273 unsigned char zlt, ios = 0, mult = 0;
305 274
306 ep = container_of(_ep, struct langwell_ep, ep); 275 ep = container_of(_ep, struct langwell_ep, ep);
307 dev = ep->dev; 276 dev = ep->dev;
308 VDBG(dev, "---> %s()\n", __func__); 277 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
309 278
310 if (!_ep || !desc || ep->desc 279 if (!_ep || !desc || ep->desc
311 || desc->bDescriptorType != USB_DT_ENDPOINT) 280 || desc->bDescriptorType != USB_DT_ENDPOINT)
@@ -326,7 +295,7 @@ static int langwell_ep_enable(struct usb_ep *_ep,
326 * sanity check type, direction, address, and then 295 * sanity check type, direction, address, and then
327 * initialize the endpoint capabilities fields in dQH 296 * initialize the endpoint capabilities fields in dQH
328 */ 297 */
329 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 298 switch (usb_endpoint_type(desc)) {
330 case USB_ENDPOINT_XFER_CONTROL: 299 case USB_ENDPOINT_XFER_CONTROL:
331 ios = 1; 300 ios = 1;
332 break; 301 break;
@@ -386,33 +355,36 @@ static int langwell_ep_enable(struct usb_ep *_ep,
386 355
387 spin_lock_irqsave(&dev->lock, flags); 356 spin_lock_irqsave(&dev->lock, flags);
388 357
389 /* configure endpoint capabilities in dQH */
390 ep->dqh->dqh_ios = ios;
391 ep->dqh->dqh_mpl = cpu_to_le16(max);
392 ep->dqh->dqh_zlt = zlt;
393 ep->dqh->dqh_mult = mult;
394
395 ep->ep.maxpacket = max; 358 ep->ep.maxpacket = max;
396 ep->desc = desc; 359 ep->desc = desc;
397 ep->stopped = 0; 360 ep->stopped = 0;
398 ep->ep_num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 361 ep->ep_num = usb_endpoint_num(desc);
399 362
400 /* ep_type */ 363 /* ep_type */
401 ep->ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 364 ep->ep_type = usb_endpoint_type(desc);
402 365
403 /* configure endpoint control registers */ 366 /* configure endpoint control registers */
404 ep_reset(ep, ep->ep_num, is_in(ep), ep->ep_type); 367 ep_reset(ep, ep->ep_num, is_in(ep), ep->ep_type);
405 368
406 DBG(dev, "enabled %s (ep%d%s-%s), max %04x\n", 369 /* configure endpoint capabilities in dQH */
370 i = ep->ep_num * 2 + is_in(ep);
371 ep->dqh = &dev->ep_dqh[i];
372 ep->dqh->dqh_ios = ios;
373 ep->dqh->dqh_mpl = cpu_to_le16(max);
374 ep->dqh->dqh_zlt = zlt;
375 ep->dqh->dqh_mult = mult;
376 ep->dqh->dtd_next = DTD_TERM;
377
378 dev_dbg(&dev->pdev->dev, "enabled %s (ep%d%s-%s), max %04x\n",
407 _ep->name, 379 _ep->name,
408 ep->ep_num, 380 ep->ep_num,
409 DIR_STRING(desc->bEndpointAddress), 381 DIR_STRING(ep),
410 type_string(desc->bmAttributes), 382 type_string(desc),
411 max); 383 max);
412 384
413 spin_unlock_irqrestore(&dev->lock, flags); 385 spin_unlock_irqrestore(&dev->lock, flags);
414done: 386done:
415 VDBG(dev, "<--- %s()\n", __func__); 387 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
416 return retval; 388 return retval;
417} 389}
418 390
@@ -428,7 +400,7 @@ static void done(struct langwell_ep *ep, struct langwell_request *req,
428 struct langwell_dtd *curr_dtd, *next_dtd; 400 struct langwell_dtd *curr_dtd, *next_dtd;
429 int i; 401 int i;
430 402
431 VDBG(dev, "---> %s()\n", __func__); 403 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
432 404
433 /* remove the req from ep->queue */ 405 /* remove the req from ep->queue */
434 list_del_init(&req->queue); 406 list_del_init(&req->queue);
@@ -448,7 +420,8 @@ static void done(struct langwell_ep *ep, struct langwell_request *req,
448 } 420 }
449 421
450 if (req->mapped) { 422 if (req->mapped) {
451 dma_unmap_single(&dev->pdev->dev, req->req.dma, req->req.length, 423 dma_unmap_single(&dev->pdev->dev,
424 req->req.dma, req->req.length,
452 is_in(ep) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); 425 is_in(ep) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
453 req->req.dma = DMA_ADDR_INVALID; 426 req->req.dma = DMA_ADDR_INVALID;
454 req->mapped = 0; 427 req->mapped = 0;
@@ -458,9 +431,10 @@ static void done(struct langwell_ep *ep, struct langwell_request *req,
458 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 431 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
459 432
460 if (status != -ESHUTDOWN) 433 if (status != -ESHUTDOWN)
461 DBG(dev, "complete %s, req %p, stat %d, len %u/%u\n", 434 dev_dbg(&dev->pdev->dev,
462 ep->ep.name, &req->req, status, 435 "complete %s, req %p, stat %d, len %u/%u\n",
463 req->req.actual, req->req.length); 436 ep->ep.name, &req->req, status,
437 req->req.actual, req->req.length);
464 438
465 /* don't modify queue heads during completion callback */ 439 /* don't modify queue heads during completion callback */
466 ep->stopped = 1; 440 ep->stopped = 1;
@@ -473,7 +447,7 @@ static void done(struct langwell_ep *ep, struct langwell_request *req,
473 spin_lock(&dev->lock); 447 spin_lock(&dev->lock);
474 ep->stopped = stopped; 448 ep->stopped = stopped;
475 449
476 VDBG(dev, "<--- %s()\n", __func__); 450 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
477} 451}
478 452
479 453
@@ -511,7 +485,7 @@ static int langwell_ep_disable(struct usb_ep *_ep)
511 485
512 ep = container_of(_ep, struct langwell_ep, ep); 486 ep = container_of(_ep, struct langwell_ep, ep);
513 dev = ep->dev; 487 dev = ep->dev;
514 VDBG(dev, "---> %s()\n", __func__); 488 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
515 489
516 if (!_ep || !ep->desc) 490 if (!_ep || !ep->desc)
517 return -EINVAL; 491 return -EINVAL;
@@ -535,8 +509,8 @@ static int langwell_ep_disable(struct usb_ep *_ep)
535 509
536 spin_unlock_irqrestore(&dev->lock, flags); 510 spin_unlock_irqrestore(&dev->lock, flags);
537 511
538 DBG(dev, "disabled %s\n", _ep->name); 512 dev_dbg(&dev->pdev->dev, "disabled %s\n", _ep->name);
539 VDBG(dev, "<--- %s()\n", __func__); 513 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
540 514
541 return 0; 515 return 0;
542} 516}
@@ -555,7 +529,7 @@ static struct usb_request *langwell_alloc_request(struct usb_ep *_ep,
555 529
556 ep = container_of(_ep, struct langwell_ep, ep); 530 ep = container_of(_ep, struct langwell_ep, ep);
557 dev = ep->dev; 531 dev = ep->dev;
558 VDBG(dev, "---> %s()\n", __func__); 532 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
559 533
560 req = kzalloc(sizeof(*req), gfp_flags); 534 req = kzalloc(sizeof(*req), gfp_flags);
561 if (!req) 535 if (!req)
@@ -564,8 +538,8 @@ static struct usb_request *langwell_alloc_request(struct usb_ep *_ep,
564 req->req.dma = DMA_ADDR_INVALID; 538 req->req.dma = DMA_ADDR_INVALID;
565 INIT_LIST_HEAD(&req->queue); 539 INIT_LIST_HEAD(&req->queue);
566 540
567 VDBG(dev, "alloc request for %s\n", _ep->name); 541 dev_vdbg(&dev->pdev->dev, "alloc request for %s\n", _ep->name);
568 VDBG(dev, "<--- %s()\n", __func__); 542 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
569 return &req->req; 543 return &req->req;
570} 544}
571 545
@@ -580,7 +554,7 @@ static void langwell_free_request(struct usb_ep *_ep,
580 554
581 ep = container_of(_ep, struct langwell_ep, ep); 555 ep = container_of(_ep, struct langwell_ep, ep);
582 dev = ep->dev; 556 dev = ep->dev;
583 VDBG(dev, "---> %s()\n", __func__); 557 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
584 558
585 if (!_ep || !_req) 559 if (!_ep || !_req)
586 return; 560 return;
@@ -591,8 +565,8 @@ static void langwell_free_request(struct usb_ep *_ep,
591 if (_req) 565 if (_req)
592 kfree(req); 566 kfree(req);
593 567
594 VDBG(dev, "free request for %s\n", _ep->name); 568 dev_vdbg(&dev->pdev->dev, "free request for %s\n", _ep->name);
595 VDBG(dev, "<--- %s()\n", __func__); 569 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
596} 570}
597 571
598 572
@@ -608,23 +582,24 @@ static int queue_dtd(struct langwell_ep *ep, struct langwell_request *req)
608 struct langwell_udc *dev; 582 struct langwell_udc *dev;
609 583
610 dev = ep->dev; 584 dev = ep->dev;
611 VDBG(dev, "---> %s()\n", __func__); 585 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
612 586
613 i = ep->ep_num * 2 + is_in(ep); 587 i = ep->ep_num * 2 + is_in(ep);
614 dqh = &dev->ep_dqh[i]; 588 dqh = &dev->ep_dqh[i];
615 589
616 if (ep->ep_num) 590 if (ep->ep_num)
617 VDBG(dev, "%s\n", ep->name); 591 dev_vdbg(&dev->pdev->dev, "%s\n", ep->name);
618 else 592 else
619 /* ep0 */ 593 /* ep0 */
620 VDBG(dev, "%s-%s\n", ep->name, is_in(ep) ? "in" : "out"); 594 dev_vdbg(&dev->pdev->dev, "%s-%s\n", ep->name, DIR_STRING(ep));
621 595
622 VDBG(dev, "ep_dqh[%d] addr: 0x%08x\n", i, (u32)&(dev->ep_dqh[i])); 596 dev_vdbg(&dev->pdev->dev, "ep_dqh[%d] addr: 0x%08x\n",
597 i, (u32)&(dev->ep_dqh[i]));
623 598
624 bit_mask = is_in(ep) ? 599 bit_mask = is_in(ep) ?
625 (1 << (ep->ep_num + 16)) : (1 << (ep->ep_num)); 600 (1 << (ep->ep_num + 16)) : (1 << (ep->ep_num));
626 601
627 VDBG(dev, "bit_mask = 0x%08x\n", bit_mask); 602 dev_vdbg(&dev->pdev->dev, "bit_mask = 0x%08x\n", bit_mask);
628 603
629 /* check if the pipe is empty */ 604 /* check if the pipe is empty */
630 if (!(list_empty(&ep->queue))) { 605 if (!(list_empty(&ep->queue))) {
@@ -665,14 +640,17 @@ static int queue_dtd(struct langwell_ep *ep, struct langwell_request *req)
665 /* clear active and halt bit */ 640 /* clear active and halt bit */
666 dtd_status = (u8) ~(DTD_STS_ACTIVE | DTD_STS_HALTED); 641 dtd_status = (u8) ~(DTD_STS_ACTIVE | DTD_STS_HALTED);
667 dqh->dtd_status &= dtd_status; 642 dqh->dtd_status &= dtd_status;
668 VDBG(dev, "dqh->dtd_status = 0x%x\n", dqh->dtd_status); 643 dev_vdbg(&dev->pdev->dev, "dqh->dtd_status = 0x%x\n", dqh->dtd_status);
644
645 /* ensure that updates to the dQH will occur before priming */
646 wmb();
669 647
670 /* write 1 to endptprime register to PRIME endpoint */ 648 /* write 1 to endptprime register to PRIME endpoint */
671 bit_mask = is_in(ep) ? (1 << (ep->ep_num + 16)) : (1 << ep->ep_num); 649 bit_mask = is_in(ep) ? (1 << (ep->ep_num + 16)) : (1 << ep->ep_num);
672 VDBG(dev, "endprime bit_mask = 0x%08x\n", bit_mask); 650 dev_vdbg(&dev->pdev->dev, "endprime bit_mask = 0x%08x\n", bit_mask);
673 writel(bit_mask, &dev->op_regs->endptprime); 651 writel(bit_mask, &dev->op_regs->endptprime);
674out: 652out:
675 VDBG(dev, "<--- %s()\n", __func__); 653 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
676 return 0; 654 return 0;
677} 655}
678 656
@@ -687,7 +665,7 @@ static struct langwell_dtd *build_dtd(struct langwell_request *req,
687 int i; 665 int i;
688 666
689 dev = req->ep->dev; 667 dev = req->ep->dev;
690 VDBG(dev, "---> %s()\n", __func__); 668 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
691 669
692 /* the maximum transfer length, up to 16k bytes */ 670 /* the maximum transfer length, up to 16k bytes */
693 *length = min(req->req.length - req->req.actual, 671 *length = min(req->req.length - req->req.actual,
@@ -708,7 +686,7 @@ static struct langwell_dtd *build_dtd(struct langwell_request *req,
708 686
709 /* fill in total bytes with transfer size */ 687 /* fill in total bytes with transfer size */
710 dtd->dtd_total = cpu_to_le16(*length); 688 dtd->dtd_total = cpu_to_le16(*length);
711 VDBG(dev, "dtd->dtd_total = %d\n", dtd->dtd_total); 689 dev_vdbg(&dev->pdev->dev, "dtd->dtd_total = %d\n", dtd->dtd_total);
712 690
713 /* set is_last flag if req->req.zero is set or not */ 691 /* set is_last flag if req->req.zero is set or not */
714 if (req->req.zero) { 692 if (req->req.zero) {
@@ -722,7 +700,7 @@ static struct langwell_dtd *build_dtd(struct langwell_request *req,
722 *is_last = 0; 700 *is_last = 0;
723 701
724 if (*is_last == 0) 702 if (*is_last == 0)
725 VDBG(dev, "multi-dtd request!\n"); 703 dev_vdbg(&dev->pdev->dev, "multi-dtd request!\n");
726 704
727 /* set interrupt on complete bit for the last dTD */ 705 /* set interrupt on complete bit for the last dTD */
728 if (*is_last && !req->req.no_interrupt) 706 if (*is_last && !req->req.no_interrupt)
@@ -733,10 +711,12 @@ static struct langwell_dtd *build_dtd(struct langwell_request *req,
733 711
734 /* set the active bit of status field to 1 */ 712 /* set the active bit of status field to 1 */
735 dtd->dtd_status = DTD_STS_ACTIVE; 713 dtd->dtd_status = DTD_STS_ACTIVE;
736 VDBG(dev, "dtd->dtd_status = 0x%02x\n", dtd->dtd_status); 714 dev_vdbg(&dev->pdev->dev, "dtd->dtd_status = 0x%02x\n",
715 dtd->dtd_status);
737 716
738 VDBG(dev, "length = %d, dma addr= 0x%08x\n", *length, (int)*dma); 717 dev_vdbg(&dev->pdev->dev, "length = %d, dma addr= 0x%08x\n",
739 VDBG(dev, "<--- %s()\n", __func__); 718 *length, (int)*dma);
719 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
740 return dtd; 720 return dtd;
741} 721}
742 722
@@ -751,7 +731,7 @@ static int req_to_dtd(struct langwell_request *req)
751 dma_addr_t dma; 731 dma_addr_t dma;
752 732
753 dev = req->ep->dev; 733 dev = req->ep->dev;
754 VDBG(dev, "---> %s()\n", __func__); 734 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
755 do { 735 do {
756 dtd = build_dtd(req, &count, &dma, &is_last); 736 dtd = build_dtd(req, &count, &dma, &is_last);
757 if (dtd == NULL) 737 if (dtd == NULL)
@@ -773,7 +753,7 @@ static int req_to_dtd(struct langwell_request *req)
773 753
774 req->tail = dtd; 754 req->tail = dtd;
775 755
776 VDBG(dev, "<--- %s()\n", __func__); 756 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
777 return 0; 757 return 0;
778} 758}
779 759
@@ -803,9 +783,9 @@ static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
803 783
804 dev = ep->dev; 784 dev = ep->dev;
805 req->ep = ep; 785 req->ep = ep;
806 VDBG(dev, "---> %s()\n", __func__); 786 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
807 787
808 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) { 788 if (usb_endpoint_xfer_isoc(ep->desc)) {
809 if (req->req.length > ep->ep.maxpacket) 789 if (req->req.length > ep->ep.maxpacket)
810 return -EMSGSIZE; 790 return -EMSGSIZE;
811 is_iso = 1; 791 is_iso = 1;
@@ -818,7 +798,7 @@ static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
818 if (_req->dma == DMA_ADDR_INVALID) { 798 if (_req->dma == DMA_ADDR_INVALID) {
819 /* WORKAROUND: WARN_ON(size == 0) */ 799 /* WORKAROUND: WARN_ON(size == 0) */
820 if (_req->length == 0) { 800 if (_req->length == 0) {
821 VDBG(dev, "req->length: 0->1\n"); 801 dev_vdbg(&dev->pdev->dev, "req->length: 0->1\n");
822 zlflag = 1; 802 zlflag = 1;
823 _req->length++; 803 _req->length++;
824 } 804 }
@@ -827,24 +807,25 @@ static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
827 _req->buf, _req->length, 807 _req->buf, _req->length,
828 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 808 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
829 if (zlflag && (_req->length == 1)) { 809 if (zlflag && (_req->length == 1)) {
830 VDBG(dev, "req->length: 1->0\n"); 810 dev_vdbg(&dev->pdev->dev, "req->length: 1->0\n");
831 zlflag = 0; 811 zlflag = 0;
832 _req->length = 0; 812 _req->length = 0;
833 } 813 }
834 814
835 req->mapped = 1; 815 req->mapped = 1;
836 VDBG(dev, "req->mapped = 1\n"); 816 dev_vdbg(&dev->pdev->dev, "req->mapped = 1\n");
837 } else { 817 } else {
838 dma_sync_single_for_device(&dev->pdev->dev, 818 dma_sync_single_for_device(&dev->pdev->dev,
839 _req->dma, _req->length, 819 _req->dma, _req->length,
840 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 820 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
841 req->mapped = 0; 821 req->mapped = 0;
842 VDBG(dev, "req->mapped = 0\n"); 822 dev_vdbg(&dev->pdev->dev, "req->mapped = 0\n");
843 } 823 }
844 824
845 DBG(dev, "%s queue req %p, len %u, buf %p, dma 0x%08llx\n", 825 dev_dbg(&dev->pdev->dev,
846 _ep->name, 826 "%s queue req %p, len %u, buf %p, dma 0x%08x\n",
847 _req, _req->length, _req->buf, (unsigned long long)_req->dma); 827 _ep->name,
828 _req, _req->length, _req->buf, (int)_req->dma);
848 829
849 _req->status = -EINPROGRESS; 830 _req->status = -EINPROGRESS;
850 _req->actual = 0; 831 _req->actual = 0;
@@ -866,12 +847,12 @@ static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
866 847
867 if (likely(req != NULL)) { 848 if (likely(req != NULL)) {
868 list_add_tail(&req->queue, &ep->queue); 849 list_add_tail(&req->queue, &ep->queue);
869 VDBG(dev, "list_add_tail() \n"); 850 dev_vdbg(&dev->pdev->dev, "list_add_tail()\n");
870 } 851 }
871 852
872 spin_unlock_irqrestore(&dev->lock, flags); 853 spin_unlock_irqrestore(&dev->lock, flags);
873 854
874 VDBG(dev, "<--- %s()\n", __func__); 855 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
875 return 0; 856 return 0;
876} 857}
877 858
@@ -888,7 +869,7 @@ static int langwell_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
888 869
889 ep = container_of(_ep, struct langwell_ep, ep); 870 ep = container_of(_ep, struct langwell_ep, ep);
890 dev = ep->dev; 871 dev = ep->dev;
891 VDBG(dev, "---> %s()\n", __func__); 872 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
892 873
893 if (!_ep || !ep->desc || !_req) 874 if (!_ep || !ep->desc || !_req)
894 return -EINVAL; 875 return -EINVAL;
@@ -924,7 +905,7 @@ static int langwell_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
924 905
925 /* queue head may be partially complete. */ 906 /* queue head may be partially complete. */
926 if (ep->queue.next == &req->queue) { 907 if (ep->queue.next == &req->queue) {
927 DBG(dev, "unlink (%s) dma\n", _ep->name); 908 dev_dbg(&dev->pdev->dev, "unlink (%s) dma\n", _ep->name);
928 _req->status = -ECONNRESET; 909 _req->status = -ECONNRESET;
929 langwell_ep_fifo_flush(&ep->ep); 910 langwell_ep_fifo_flush(&ep->ep);
930 911
@@ -963,7 +944,7 @@ done:
963 ep->stopped = stopped; 944 ep->stopped = stopped;
964 spin_unlock_irqrestore(&dev->lock, flags); 945 spin_unlock_irqrestore(&dev->lock, flags);
965 946
966 VDBG(dev, "<--- %s()\n", __func__); 947 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
967 return retval; 948 return retval;
968} 949}
969 950
@@ -976,7 +957,7 @@ static void ep_set_halt(struct langwell_ep *ep, int value)
976 u32 endptctrl = 0; 957 u32 endptctrl = 0;
977 int ep_num; 958 int ep_num;
978 struct langwell_udc *dev = ep->dev; 959 struct langwell_udc *dev = ep->dev;
979 VDBG(dev, "---> %s()\n", __func__); 960 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
980 961
981 ep_num = ep->ep_num; 962 ep_num = ep->ep_num;
982 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]); 963 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
@@ -1001,7 +982,7 @@ static void ep_set_halt(struct langwell_ep *ep, int value)
1001 982
1002 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]); 983 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
1003 984
1004 VDBG(dev, "<--- %s()\n", __func__); 985 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1005} 986}
1006 987
1007 988
@@ -1016,7 +997,7 @@ static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
1016 ep = container_of(_ep, struct langwell_ep, ep); 997 ep = container_of(_ep, struct langwell_ep, ep);
1017 dev = ep->dev; 998 dev = ep->dev;
1018 999
1019 VDBG(dev, "---> %s()\n", __func__); 1000 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1020 1001
1021 if (!_ep || !ep->desc) 1002 if (!_ep || !ep->desc)
1022 return -EINVAL; 1003 return -EINVAL;
@@ -1024,8 +1005,7 @@ static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
1024 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 1005 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1025 return -ESHUTDOWN; 1006 return -ESHUTDOWN;
1026 1007
1027 if (ep->desc && (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 1008 if (usb_endpoint_xfer_isoc(ep->desc))
1028 == USB_ENDPOINT_XFER_ISOC)
1029 return -EOPNOTSUPP; 1009 return -EOPNOTSUPP;
1030 1010
1031 spin_lock_irqsave(&dev->lock, flags); 1011 spin_lock_irqsave(&dev->lock, flags);
@@ -1036,7 +1016,7 @@ static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
1036 */ 1016 */
1037 if (!list_empty(&ep->queue) && is_in(ep) && value) { 1017 if (!list_empty(&ep->queue) && is_in(ep) && value) {
1038 /* IN endpoint FIFO holds bytes */ 1018 /* IN endpoint FIFO holds bytes */
1039 DBG(dev, "%s FIFO holds bytes\n", _ep->name); 1019 dev_dbg(&dev->pdev->dev, "%s FIFO holds bytes\n", _ep->name);
1040 retval = -EAGAIN; 1020 retval = -EAGAIN;
1041 goto done; 1021 goto done;
1042 } 1022 }
@@ -1050,8 +1030,9 @@ static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
1050 } 1030 }
1051done: 1031done:
1052 spin_unlock_irqrestore(&dev->lock, flags); 1032 spin_unlock_irqrestore(&dev->lock, flags);
1053 DBG(dev, "%s %s halt\n", _ep->name, value ? "set" : "clear"); 1033 dev_dbg(&dev->pdev->dev, "%s %s halt\n",
1054 VDBG(dev, "<--- %s()\n", __func__); 1034 _ep->name, value ? "set" : "clear");
1035 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1055 return retval; 1036 return retval;
1056} 1037}
1057 1038
@@ -1065,12 +1046,12 @@ static int langwell_ep_set_wedge(struct usb_ep *_ep)
1065 ep = container_of(_ep, struct langwell_ep, ep); 1046 ep = container_of(_ep, struct langwell_ep, ep);
1066 dev = ep->dev; 1047 dev = ep->dev;
1067 1048
1068 VDBG(dev, "---> %s()\n", __func__); 1049 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1069 1050
1070 if (!_ep || !ep->desc) 1051 if (!_ep || !ep->desc)
1071 return -EINVAL; 1052 return -EINVAL;
1072 1053
1073 VDBG(dev, "<--- %s()\n", __func__); 1054 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1074 return usb_ep_set_halt(_ep); 1055 return usb_ep_set_halt(_ep);
1075} 1056}
1076 1057
@@ -1086,15 +1067,16 @@ static void langwell_ep_fifo_flush(struct usb_ep *_ep)
1086 ep = container_of(_ep, struct langwell_ep, ep); 1067 ep = container_of(_ep, struct langwell_ep, ep);
1087 dev = ep->dev; 1068 dev = ep->dev;
1088 1069
1089 VDBG(dev, "---> %s()\n", __func__); 1070 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1090 1071
1091 if (!_ep || !ep->desc) { 1072 if (!_ep || !ep->desc) {
1092 VDBG(dev, "ep or ep->desc is NULL\n"); 1073 dev_vdbg(&dev->pdev->dev, "ep or ep->desc is NULL\n");
1093 VDBG(dev, "<--- %s()\n", __func__); 1074 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1094 return; 1075 return;
1095 } 1076 }
1096 1077
1097 VDBG(dev, "%s-%s fifo flush\n", _ep->name, is_in(ep) ? "in" : "out"); 1078 dev_vdbg(&dev->pdev->dev, "%s-%s fifo flush\n",
1079 _ep->name, DIR_STRING(ep));
1098 1080
1099 /* flush endpoint buffer */ 1081 /* flush endpoint buffer */
1100 if (ep->ep_num == 0) 1082 if (ep->ep_num == 0)
@@ -1110,14 +1092,14 @@ static void langwell_ep_fifo_flush(struct usb_ep *_ep)
1110 writel(flush_bit, &dev->op_regs->endptflush); 1092 writel(flush_bit, &dev->op_regs->endptflush);
1111 while (readl(&dev->op_regs->endptflush)) { 1093 while (readl(&dev->op_regs->endptflush)) {
1112 if (time_after(jiffies, timeout)) { 1094 if (time_after(jiffies, timeout)) {
1113 ERROR(dev, "ep flush timeout\n"); 1095 dev_err(&dev->pdev->dev, "ep flush timeout\n");
1114 goto done; 1096 goto done;
1115 } 1097 }
1116 cpu_relax(); 1098 cpu_relax();
1117 } 1099 }
1118 } while (readl(&dev->op_regs->endptstat) & flush_bit); 1100 } while (readl(&dev->op_regs->endptstat) & flush_bit);
1119done: 1101done:
1120 VDBG(dev, "<--- %s()\n", __func__); 1102 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1121} 1103}
1122 1104
1123 1105
@@ -1167,31 +1149,59 @@ static int langwell_get_frame(struct usb_gadget *_gadget)
1167 return -ENODEV; 1149 return -ENODEV;
1168 1150
1169 dev = container_of(_gadget, struct langwell_udc, gadget); 1151 dev = container_of(_gadget, struct langwell_udc, gadget);
1170 VDBG(dev, "---> %s()\n", __func__); 1152 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1171 1153
1172 retval = readl(&dev->op_regs->frindex) & FRINDEX_MASK; 1154 retval = readl(&dev->op_regs->frindex) & FRINDEX_MASK;
1173 1155
1174 VDBG(dev, "<--- %s()\n", __func__); 1156 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1175 return retval; 1157 return retval;
1176} 1158}
1177 1159
1178 1160
1161/* enter or exit PHY low power state */
1162static void langwell_phy_low_power(struct langwell_udc *dev, bool flag)
1163{
1164 u32 devlc;
1165 u8 devlc_byte2;
1166 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1167
1168 devlc = readl(&dev->op_regs->devlc);
1169 dev_vdbg(&dev->pdev->dev, "devlc = 0x%08x\n", devlc);
1170
1171 if (flag)
1172 devlc |= LPM_PHCD;
1173 else
1174 devlc &= ~LPM_PHCD;
1175
1176 /* FIXME: workaround for Langwell A1/A2/A3 sighting */
1177 devlc_byte2 = (devlc >> 16) & 0xff;
1178 writeb(devlc_byte2, (u8 *)&dev->op_regs->devlc + 2);
1179
1180 devlc = readl(&dev->op_regs->devlc);
1181 dev_vdbg(&dev->pdev->dev,
1182 "%s PHY low power suspend, devlc = 0x%08x\n",
1183 flag ? "enter" : "exit", devlc);
1184}
1185
1186
1179/* tries to wake up the host connected to this gadget */ 1187/* tries to wake up the host connected to this gadget */
1180static int langwell_wakeup(struct usb_gadget *_gadget) 1188static int langwell_wakeup(struct usb_gadget *_gadget)
1181{ 1189{
1182 struct langwell_udc *dev; 1190 struct langwell_udc *dev;
1183 u32 portsc1, devlc; 1191 u32 portsc1;
1184 unsigned long flags; 1192 unsigned long flags;
1185 1193
1186 if (!_gadget) 1194 if (!_gadget)
1187 return 0; 1195 return 0;
1188 1196
1189 dev = container_of(_gadget, struct langwell_udc, gadget); 1197 dev = container_of(_gadget, struct langwell_udc, gadget);
1190 VDBG(dev, "---> %s()\n", __func__); 1198 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1191 1199
1192 /* Remote Wakeup feature not enabled by host */ 1200 /* remote wakeup feature not enabled by host */
1193 if (!dev->remote_wakeup) 1201 if (!dev->remote_wakeup) {
1202 dev_info(&dev->pdev->dev, "remote wakeup is disabled\n");
1194 return -ENOTSUPP; 1203 return -ENOTSUPP;
1204 }
1195 1205
1196 spin_lock_irqsave(&dev->lock, flags); 1206 spin_lock_irqsave(&dev->lock, flags);
1197 1207
@@ -1201,27 +1211,23 @@ static int langwell_wakeup(struct usb_gadget *_gadget)
1201 return 0; 1211 return 0;
1202 } 1212 }
1203 1213
1204 /* LPM L1 to L0, remote wakeup */ 1214 /* LPM L1 to L0 or legacy remote wakeup */
1205 if (dev->lpm && dev->lpm_state == LPM_L1) { 1215 if (dev->lpm && dev->lpm_state == LPM_L1)
1206 portsc1 |= PORTS_SLP; 1216 dev_info(&dev->pdev->dev, "LPM L1 to L0 remote wakeup\n");
1207 writel(portsc1, &dev->op_regs->portsc1); 1217 else
1208 } 1218 dev_info(&dev->pdev->dev, "device remote wakeup\n");
1209
1210 /* force port resume */
1211 if (dev->usb_state == USB_STATE_SUSPENDED) {
1212 portsc1 |= PORTS_FPR;
1213 writel(portsc1, &dev->op_regs->portsc1);
1214 }
1215 1219
1216 /* exit PHY low power suspend */ 1220 /* exit PHY low power suspend */
1217 devlc = readl(&dev->op_regs->devlc); 1221 if (dev->pdev->device != 0x0829)
1218 VDBG(dev, "devlc = 0x%08x\n", devlc); 1222 langwell_phy_low_power(dev, 0);
1219 devlc &= ~LPM_PHCD; 1223
1220 writel(devlc, &dev->op_regs->devlc); 1224 /* force port resume */
1225 portsc1 |= PORTS_FPR;
1226 writel(portsc1, &dev->op_regs->portsc1);
1221 1227
1222 spin_unlock_irqrestore(&dev->lock, flags); 1228 spin_unlock_irqrestore(&dev->lock, flags);
1223 1229
1224 VDBG(dev, "<--- %s()\n", __func__); 1230 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1225 return 0; 1231 return 0;
1226} 1232}
1227 1233
@@ -1231,16 +1237,17 @@ static int langwell_vbus_session(struct usb_gadget *_gadget, int is_active)
1231{ 1237{
1232 struct langwell_udc *dev; 1238 struct langwell_udc *dev;
1233 unsigned long flags; 1239 unsigned long flags;
1234 u32 usbcmd; 1240 u32 usbcmd;
1235 1241
1236 if (!_gadget) 1242 if (!_gadget)
1237 return -ENODEV; 1243 return -ENODEV;
1238 1244
1239 dev = container_of(_gadget, struct langwell_udc, gadget); 1245 dev = container_of(_gadget, struct langwell_udc, gadget);
1240 VDBG(dev, "---> %s()\n", __func__); 1246 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1241 1247
1242 spin_lock_irqsave(&dev->lock, flags); 1248 spin_lock_irqsave(&dev->lock, flags);
1243 VDBG(dev, "VBUS status: %s\n", is_active ? "on" : "off"); 1249 dev_vdbg(&dev->pdev->dev, "VBUS status: %s\n",
1250 is_active ? "on" : "off");
1244 1251
1245 dev->vbus_active = (is_active != 0); 1252 dev->vbus_active = (is_active != 0);
1246 if (dev->driver && dev->softconnected && dev->vbus_active) { 1253 if (dev->driver && dev->softconnected && dev->vbus_active) {
@@ -1255,7 +1262,7 @@ static int langwell_vbus_session(struct usb_gadget *_gadget, int is_active)
1255 1262
1256 spin_unlock_irqrestore(&dev->lock, flags); 1263 spin_unlock_irqrestore(&dev->lock, flags);
1257 1264
1258 VDBG(dev, "<--- %s()\n", __func__); 1265 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1259 return 0; 1266 return 0;
1260} 1267}
1261 1268
@@ -1269,15 +1276,15 @@ static int langwell_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1269 return -ENODEV; 1276 return -ENODEV;
1270 1277
1271 dev = container_of(_gadget, struct langwell_udc, gadget); 1278 dev = container_of(_gadget, struct langwell_udc, gadget);
1272 VDBG(dev, "---> %s()\n", __func__); 1279 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1273 1280
1274 if (dev->transceiver) { 1281 if (dev->transceiver) {
1275 VDBG(dev, "otg_set_power\n"); 1282 dev_vdbg(&dev->pdev->dev, "otg_set_power\n");
1276 VDBG(dev, "<--- %s()\n", __func__); 1283 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1277 return otg_set_power(dev->transceiver, mA); 1284 return otg_set_power(dev->transceiver, mA);
1278 } 1285 }
1279 1286
1280 VDBG(dev, "<--- %s()\n", __func__); 1287 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1281 return -ENOTSUPP; 1288 return -ENOTSUPP;
1282} 1289}
1283 1290
@@ -1286,15 +1293,15 @@ static int langwell_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1286static int langwell_pullup(struct usb_gadget *_gadget, int is_on) 1293static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
1287{ 1294{
1288 struct langwell_udc *dev; 1295 struct langwell_udc *dev;
1289 u32 usbcmd; 1296 u32 usbcmd;
1290 unsigned long flags; 1297 unsigned long flags;
1291 1298
1292 if (!_gadget) 1299 if (!_gadget)
1293 return -ENODEV; 1300 return -ENODEV;
1294 1301
1295 dev = container_of(_gadget, struct langwell_udc, gadget); 1302 dev = container_of(_gadget, struct langwell_udc, gadget);
1296 1303
1297 VDBG(dev, "---> %s()\n", __func__); 1304 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1298 1305
1299 spin_lock_irqsave(&dev->lock, flags); 1306 spin_lock_irqsave(&dev->lock, flags);
1300 dev->softconnected = (is_on != 0); 1307 dev->softconnected = (is_on != 0);
@@ -1310,7 +1317,7 @@ static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
1310 } 1317 }
1311 spin_unlock_irqrestore(&dev->lock, flags); 1318 spin_unlock_irqrestore(&dev->lock, flags);
1312 1319
1313 VDBG(dev, "<--- %s()\n", __func__); 1320 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1314 return 0; 1321 return 0;
1315} 1322}
1316 1323
@@ -1346,12 +1353,13 @@ static const struct usb_gadget_ops langwell_ops = {
1346static int langwell_udc_reset(struct langwell_udc *dev) 1353static int langwell_udc_reset(struct langwell_udc *dev)
1347{ 1354{
1348 u32 usbcmd, usbmode, devlc, endpointlistaddr; 1355 u32 usbcmd, usbmode, devlc, endpointlistaddr;
1356 u8 devlc_byte0, devlc_byte2;
1349 unsigned long timeout; 1357 unsigned long timeout;
1350 1358
1351 if (!dev) 1359 if (!dev)
1352 return -EINVAL; 1360 return -EINVAL;
1353 1361
1354 DBG(dev, "---> %s()\n", __func__); 1362 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1355 1363
1356 /* set controller to stop state */ 1364 /* set controller to stop state */
1357 usbcmd = readl(&dev->op_regs->usbcmd); 1365 usbcmd = readl(&dev->op_regs->usbcmd);
@@ -1367,7 +1375,7 @@ static int langwell_udc_reset(struct langwell_udc *dev)
1367 timeout = jiffies + RESET_TIMEOUT; 1375 timeout = jiffies + RESET_TIMEOUT;
1368 while (readl(&dev->op_regs->usbcmd) & CMD_RST) { 1376 while (readl(&dev->op_regs->usbcmd) & CMD_RST) {
1369 if (time_after(jiffies, timeout)) { 1377 if (time_after(jiffies, timeout)) {
1370 ERROR(dev, "device reset timeout\n"); 1378 dev_err(&dev->pdev->dev, "device reset timeout\n");
1371 return -ETIMEDOUT; 1379 return -ETIMEDOUT;
1372 } 1380 }
1373 cpu_relax(); 1381 cpu_relax();
@@ -1382,7 +1390,7 @@ static int langwell_udc_reset(struct langwell_udc *dev)
1382 1390
1383 writel(usbmode, &dev->op_regs->usbmode); 1391 writel(usbmode, &dev->op_regs->usbmode);
1384 usbmode = readl(&dev->op_regs->usbmode); 1392 usbmode = readl(&dev->op_regs->usbmode);
1385 VDBG(dev, "usbmode=0x%08x\n", usbmode); 1393 dev_vdbg(&dev->pdev->dev, "usbmode=0x%08x\n", usbmode);
1386 1394
1387 /* Write-Clear setup status */ 1395 /* Write-Clear setup status */
1388 writel(0, &dev->op_regs->usbsts); 1396 writel(0, &dev->op_regs->usbsts);
@@ -1390,9 +1398,17 @@ static int langwell_udc_reset(struct langwell_udc *dev)
1390 /* if support USB LPM, ACK all LPM token */ 1398 /* if support USB LPM, ACK all LPM token */
1391 if (dev->lpm) { 1399 if (dev->lpm) {
1392 devlc = readl(&dev->op_regs->devlc); 1400 devlc = readl(&dev->op_regs->devlc);
1401 dev_vdbg(&dev->pdev->dev, "devlc = 0x%08x\n", devlc);
1402 /* FIXME: workaround for Langwell A1/A2/A3 sighting */
1393 devlc &= ~LPM_STL; /* don't STALL LPM token */ 1403 devlc &= ~LPM_STL; /* don't STALL LPM token */
1394 devlc &= ~LPM_NYT_ACK; /* ACK LPM token */ 1404 devlc &= ~LPM_NYT_ACK; /* ACK LPM token */
1395 writel(devlc, &dev->op_regs->devlc); 1405 devlc_byte0 = devlc & 0xff;
1406 devlc_byte2 = (devlc >> 16) & 0xff;
1407 writeb(devlc_byte0, (u8 *)&dev->op_regs->devlc);
1408 writeb(devlc_byte2, (u8 *)&dev->op_regs->devlc + 2);
1409 devlc = readl(&dev->op_regs->devlc);
1410 dev_vdbg(&dev->pdev->dev,
1411 "ACK LPM token, devlc = 0x%08x\n", devlc);
1396 } 1412 }
1397 1413
1398 /* fill endpointlistaddr register */ 1414 /* fill endpointlistaddr register */
@@ -1400,10 +1416,11 @@ static int langwell_udc_reset(struct langwell_udc *dev)
1400 endpointlistaddr &= ENDPOINTLISTADDR_MASK; 1416 endpointlistaddr &= ENDPOINTLISTADDR_MASK;
1401 writel(endpointlistaddr, &dev->op_regs->endpointlistaddr); 1417 writel(endpointlistaddr, &dev->op_regs->endpointlistaddr);
1402 1418
1403 VDBG(dev, "dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n", 1419 dev_vdbg(&dev->pdev->dev,
1404 dev->ep_dqh, endpointlistaddr, 1420 "dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n",
1405 readl(&dev->op_regs->endpointlistaddr)); 1421 dev->ep_dqh, endpointlistaddr,
1406 DBG(dev, "<--- %s()\n", __func__); 1422 readl(&dev->op_regs->endpointlistaddr));
1423 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1407 return 0; 1424 return 0;
1408} 1425}
1409 1426
@@ -1415,7 +1432,7 @@ static int eps_reinit(struct langwell_udc *dev)
1415 char name[14]; 1432 char name[14];
1416 int i; 1433 int i;
1417 1434
1418 VDBG(dev, "---> %s()\n", __func__); 1435 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1419 1436
1420 /* initialize ep0 */ 1437 /* initialize ep0 */
1421 ep = &dev->ep[0]; 1438 ep = &dev->ep[0];
@@ -1449,11 +1466,9 @@ static int eps_reinit(struct langwell_udc *dev)
1449 1466
1450 INIT_LIST_HEAD(&ep->queue); 1467 INIT_LIST_HEAD(&ep->queue);
1451 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list); 1468 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1452
1453 ep->dqh = &dev->ep_dqh[i];
1454 } 1469 }
1455 1470
1456 VDBG(dev, "<--- %s()\n", __func__); 1471 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1457 return 0; 1472 return 0;
1458} 1473}
1459 1474
@@ -1462,7 +1477,7 @@ static int eps_reinit(struct langwell_udc *dev)
1462static void langwell_udc_start(struct langwell_udc *dev) 1477static void langwell_udc_start(struct langwell_udc *dev)
1463{ 1478{
1464 u32 usbintr, usbcmd; 1479 u32 usbintr, usbcmd;
1465 DBG(dev, "---> %s()\n", __func__); 1480 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1466 1481
1467 /* enable interrupts */ 1482 /* enable interrupts */
1468 usbintr = INTR_ULPIE /* ULPI */ 1483 usbintr = INTR_ULPIE /* ULPI */
@@ -1485,8 +1500,7 @@ static void langwell_udc_start(struct langwell_udc *dev)
1485 usbcmd |= CMD_RUNSTOP; 1500 usbcmd |= CMD_RUNSTOP;
1486 writel(usbcmd, &dev->op_regs->usbcmd); 1501 writel(usbcmd, &dev->op_regs->usbcmd);
1487 1502
1488 DBG(dev, "<--- %s()\n", __func__); 1503 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1489 return;
1490} 1504}
1491 1505
1492 1506
@@ -1495,7 +1509,7 @@ static void langwell_udc_stop(struct langwell_udc *dev)
1495{ 1509{
1496 u32 usbcmd; 1510 u32 usbcmd;
1497 1511
1498 DBG(dev, "---> %s()\n", __func__); 1512 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1499 1513
1500 /* disable all interrupts */ 1514 /* disable all interrupts */
1501 writel(0, &dev->op_regs->usbintr); 1515 writel(0, &dev->op_regs->usbintr);
@@ -1508,8 +1522,7 @@ static void langwell_udc_stop(struct langwell_udc *dev)
1508 usbcmd &= ~CMD_RUNSTOP; 1522 usbcmd &= ~CMD_RUNSTOP;
1509 writel(usbcmd, &dev->op_regs->usbcmd); 1523 writel(usbcmd, &dev->op_regs->usbcmd);
1510 1524
1511 DBG(dev, "<--- %s()\n", __func__); 1525 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1512 return;
1513} 1526}
1514 1527
1515 1528
@@ -1518,7 +1531,7 @@ static void stop_activity(struct langwell_udc *dev,
1518 struct usb_gadget_driver *driver) 1531 struct usb_gadget_driver *driver)
1519{ 1532{
1520 struct langwell_ep *ep; 1533 struct langwell_ep *ep;
1521 DBG(dev, "---> %s()\n", __func__); 1534 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1522 1535
1523 nuke(&dev->ep[0], -ESHUTDOWN); 1536 nuke(&dev->ep[0], -ESHUTDOWN);
1524 1537
@@ -1533,7 +1546,7 @@ static void stop_activity(struct langwell_udc *dev,
1533 spin_lock(&dev->lock); 1546 spin_lock(&dev->lock);
1534 } 1547 }
1535 1548
1536 DBG(dev, "<--- %s()\n", __func__); 1549 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1537} 1550}
1538 1551
1539 1552
@@ -1659,13 +1672,15 @@ static ssize_t show_langwell_udc(struct device *_dev,
1659 "Over-current Change: %s\n" 1672 "Over-current Change: %s\n"
1660 "Port Enable/Disable Change: %s\n" 1673 "Port Enable/Disable Change: %s\n"
1661 "Port Enabled/Disabled: %s\n" 1674 "Port Enabled/Disabled: %s\n"
1662 "Current Connect Status: %s\n\n", 1675 "Current Connect Status: %s\n"
1676 "LPM Suspend Status: %s\n\n",
1663 (tmp_reg & PORTS_PR) ? "Reset" : "Not Reset", 1677 (tmp_reg & PORTS_PR) ? "Reset" : "Not Reset",
1664 (tmp_reg & PORTS_SUSP) ? "Suspend " : "Not Suspend", 1678 (tmp_reg & PORTS_SUSP) ? "Suspend " : "Not Suspend",
1665 (tmp_reg & PORTS_OCC) ? "Detected" : "No", 1679 (tmp_reg & PORTS_OCC) ? "Detected" : "No",
1666 (tmp_reg & PORTS_PEC) ? "Changed" : "Not Changed", 1680 (tmp_reg & PORTS_PEC) ? "Changed" : "Not Changed",
1667 (tmp_reg & PORTS_PE) ? "Enable" : "Not Correct", 1681 (tmp_reg & PORTS_PE) ? "Enable" : "Not Correct",
1668 (tmp_reg & PORTS_CCS) ? "Attached" : "Not Attached"); 1682 (tmp_reg & PORTS_CCS) ? "Attached" : "Not Attached",
1683 (tmp_reg & PORTS_SLP) ? "LPM L1" : "LPM L0");
1669 size -= t; 1684 size -= t;
1670 next += t; 1685 next += t;
1671 1686
@@ -1676,7 +1691,7 @@ static ssize_t show_langwell_udc(struct device *_dev,
1676 "Serial Transceiver : %d\n" 1691 "Serial Transceiver : %d\n"
1677 "Port Speed: %s\n" 1692 "Port Speed: %s\n"
1678 "Port Force Full Speed Connenct: %s\n" 1693 "Port Force Full Speed Connenct: %s\n"
1679 "PHY Low Power Suspend Clock Disable: %s\n" 1694 "PHY Low Power Suspend Clock: %s\n"
1680 "BmAttributes: %d\n\n", 1695 "BmAttributes: %d\n\n",
1681 LPM_PTS(tmp_reg), 1696 LPM_PTS(tmp_reg),
1682 (tmp_reg & LPM_STS) ? 1 : 0, 1697 (tmp_reg & LPM_STS) ? 1 : 0,
@@ -1797,6 +1812,36 @@ static ssize_t show_langwell_udc(struct device *_dev,
1797static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL); 1812static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);
1798 1813
1799 1814
1815/* device "remote_wakeup" sysfs attribute file */
1816static ssize_t store_remote_wakeup(struct device *_dev,
1817 struct device_attribute *attr, const char *buf, size_t count)
1818{
1819 struct langwell_udc *dev = the_controller;
1820 unsigned long flags;
1821 ssize_t rc = count;
1822
1823 if (count > 2)
1824 return -EINVAL;
1825
1826 if (count > 0 && buf[count-1] == '\n')
1827 ((char *) buf)[count-1] = 0;
1828
1829 if (buf[0] != '1')
1830 return -EINVAL;
1831
1832 /* force remote wakeup enabled in case gadget driver doesn't support */
1833 spin_lock_irqsave(&dev->lock, flags);
1834 dev->remote_wakeup = 1;
1835 dev->dev_status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1836 spin_unlock_irqrestore(&dev->lock, flags);
1837
1838 langwell_wakeup(&dev->gadget);
1839
1840 return rc;
1841}
1842static DEVICE_ATTR(remote_wakeup, S_IWUSR, NULL, store_remote_wakeup);
1843
1844
1800/*-------------------------------------------------------------------------*/ 1845/*-------------------------------------------------------------------------*/
1801 1846
1802/* 1847/*
@@ -1807,7 +1852,8 @@ static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);
1807 * the driver might get unbound. 1852 * the driver might get unbound.
1808 */ 1853 */
1809 1854
1810int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1855int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1856 int (*bind)(struct usb_gadget *))
1811{ 1857{
1812 struct langwell_udc *dev = the_controller; 1858 struct langwell_udc *dev = the_controller;
1813 unsigned long flags; 1859 unsigned long flags;
@@ -1816,7 +1862,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1816 if (!dev) 1862 if (!dev)
1817 return -ENODEV; 1863 return -ENODEV;
1818 1864
1819 DBG(dev, "---> %s()\n", __func__); 1865 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1820 1866
1821 if (dev->driver) 1867 if (dev->driver)
1822 return -EBUSY; 1868 return -EBUSY;
@@ -1830,9 +1876,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1830 1876
1831 spin_unlock_irqrestore(&dev->lock, flags); 1877 spin_unlock_irqrestore(&dev->lock, flags);
1832 1878
1833 retval = driver->bind(&dev->gadget); 1879 retval = bind(&dev->gadget);
1834 if (retval) { 1880 if (retval) {
1835 DBG(dev, "bind to driver %s --> %d\n", 1881 dev_dbg(&dev->pdev->dev, "bind to driver %s --> %d\n",
1836 driver->driver.name, retval); 1882 driver->driver.name, retval);
1837 dev->driver = NULL; 1883 dev->driver = NULL;
1838 dev->gadget.dev.driver = NULL; 1884 dev->gadget.dev.driver = NULL;
@@ -1851,13 +1897,13 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1851 if (dev->got_irq) 1897 if (dev->got_irq)
1852 langwell_udc_start(dev); 1898 langwell_udc_start(dev);
1853 1899
1854 VDBG(dev, "After langwell_udc_start(), print all registers:\n"); 1900 dev_vdbg(&dev->pdev->dev,
1855#ifdef VERBOSE 1901 "After langwell_udc_start(), print all registers:\n");
1856 print_all_registers(dev); 1902 print_all_registers(dev);
1857#endif
1858 1903
1859 INFO(dev, "register driver: %s\n", driver->driver.name); 1904 dev_info(&dev->pdev->dev, "register driver: %s\n",
1860 VDBG(dev, "<--- %s()\n", __func__); 1905 driver->driver.name);
1906 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1861 return 0; 1907 return 0;
1862 1908
1863err_unbind: 1909err_unbind:
@@ -1865,10 +1911,10 @@ err_unbind:
1865 dev->gadget.dev.driver = NULL; 1911 dev->gadget.dev.driver = NULL;
1866 dev->driver = NULL; 1912 dev->driver = NULL;
1867 1913
1868 DBG(dev, "<--- %s()\n", __func__); 1914 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1869 return retval; 1915 return retval;
1870} 1916}
1871EXPORT_SYMBOL(usb_gadget_register_driver); 1917EXPORT_SYMBOL(usb_gadget_probe_driver);
1872 1918
1873 1919
1874/* unregister gadget driver */ 1920/* unregister gadget driver */
@@ -1880,11 +1926,15 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1880 if (!dev) 1926 if (!dev)
1881 return -ENODEV; 1927 return -ENODEV;
1882 1928
1883 DBG(dev, "---> %s()\n", __func__); 1929 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1884 1930
1885 if (unlikely(!driver || !driver->bind || !driver->unbind)) 1931 if (unlikely(!driver || !driver->unbind))
1886 return -EINVAL; 1932 return -EINVAL;
1887 1933
1934 /* exit PHY low power suspend */
1935 if (dev->pdev->device != 0x0829)
1936 langwell_phy_low_power(dev, 0);
1937
1888 /* unbind OTG transceiver */ 1938 /* unbind OTG transceiver */
1889 if (dev->transceiver) 1939 if (dev->transceiver)
1890 (void)otg_set_peripheral(dev->transceiver, 0); 1940 (void)otg_set_peripheral(dev->transceiver, 0);
@@ -1910,8 +1960,9 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1910 1960
1911 device_remove_file(&dev->pdev->dev, &dev_attr_function); 1961 device_remove_file(&dev->pdev->dev, &dev_attr_function);
1912 1962
1913 INFO(dev, "unregistered driver '%s'\n", driver->driver.name); 1963 dev_info(&dev->pdev->dev, "unregistered driver '%s'\n",
1914 DBG(dev, "<--- %s()\n", __func__); 1964 driver->driver.name);
1965 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1915 return 0; 1966 return 0;
1916} 1967}
1917EXPORT_SYMBOL(usb_gadget_unregister_driver); 1968EXPORT_SYMBOL(usb_gadget_unregister_driver);
@@ -1930,7 +1981,7 @@ static void setup_tripwire(struct langwell_udc *dev)
1930 unsigned long timeout; 1981 unsigned long timeout;
1931 struct langwell_dqh *dqh; 1982 struct langwell_dqh *dqh;
1932 1983
1933 VDBG(dev, "---> %s()\n", __func__); 1984 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1934 1985
1935 /* ep0 OUT dQH */ 1986 /* ep0 OUT dQH */
1936 dqh = &dev->ep_dqh[EP_DIR_OUT]; 1987 dqh = &dev->ep_dqh[EP_DIR_OUT];
@@ -1943,7 +1994,7 @@ static void setup_tripwire(struct langwell_udc *dev)
1943 timeout = jiffies + SETUPSTAT_TIMEOUT; 1994 timeout = jiffies + SETUPSTAT_TIMEOUT;
1944 while (readl(&dev->op_regs->endptsetupstat)) { 1995 while (readl(&dev->op_regs->endptsetupstat)) {
1945 if (time_after(jiffies, timeout)) { 1996 if (time_after(jiffies, timeout)) {
1946 ERROR(dev, "setup_tripwire timeout\n"); 1997 dev_err(&dev->pdev->dev, "setup_tripwire timeout\n");
1947 break; 1998 break;
1948 } 1999 }
1949 cpu_relax(); 2000 cpu_relax();
@@ -1963,7 +2014,7 @@ static void setup_tripwire(struct langwell_udc *dev)
1963 usbcmd = readl(&dev->op_regs->usbcmd); 2014 usbcmd = readl(&dev->op_regs->usbcmd);
1964 writel(usbcmd & ~CMD_SUTW, &dev->op_regs->usbcmd); 2015 writel(usbcmd & ~CMD_SUTW, &dev->op_regs->usbcmd);
1965 2016
1966 VDBG(dev, "<--- %s()\n", __func__); 2017 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1967} 2018}
1968 2019
1969 2020
@@ -1972,7 +2023,7 @@ static void ep0_stall(struct langwell_udc *dev)
1972{ 2023{
1973 u32 endptctrl; 2024 u32 endptctrl;
1974 2025
1975 VDBG(dev, "---> %s()\n", __func__); 2026 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1976 2027
1977 /* set TX and RX to stall */ 2028 /* set TX and RX to stall */
1978 endptctrl = readl(&dev->op_regs->endptctrl[0]); 2029 endptctrl = readl(&dev->op_regs->endptctrl[0]);
@@ -1983,7 +2034,7 @@ static void ep0_stall(struct langwell_udc *dev)
1983 dev->ep0_state = WAIT_FOR_SETUP; 2034 dev->ep0_state = WAIT_FOR_SETUP;
1984 dev->ep0_dir = USB_DIR_OUT; 2035 dev->ep0_dir = USB_DIR_OUT;
1985 2036
1986 VDBG(dev, "<--- %s()\n", __func__); 2037 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1987} 2038}
1988 2039
1989 2040
@@ -1994,7 +2045,7 @@ static int prime_status_phase(struct langwell_udc *dev, int dir)
1994 struct langwell_ep *ep; 2045 struct langwell_ep *ep;
1995 int status = 0; 2046 int status = 0;
1996 2047
1997 VDBG(dev, "---> %s()\n", __func__); 2048 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1998 2049
1999 if (dir == EP_DIR_IN) 2050 if (dir == EP_DIR_IN)
2000 dev->ep0_dir = USB_DIR_IN; 2051 dev->ep0_dir = USB_DIR_IN;
@@ -2019,11 +2070,11 @@ static int prime_status_phase(struct langwell_udc *dev, int dir)
2019 return -ENOMEM; 2070 return -ENOMEM;
2020 2071
2021 if (status) 2072 if (status)
2022 ERROR(dev, "can't queue ep0 status request\n"); 2073 dev_err(&dev->pdev->dev, "can't queue ep0 status request\n");
2023 2074
2024 list_add_tail(&req->queue, &ep->queue); 2075 list_add_tail(&req->queue, &ep->queue);
2025 2076
2026 VDBG(dev, "<--- %s()\n", __func__); 2077 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2027 return status; 2078 return status;
2028} 2079}
2029 2080
@@ -2032,11 +2083,11 @@ static int prime_status_phase(struct langwell_udc *dev, int dir)
2032static void set_address(struct langwell_udc *dev, u16 value, 2083static void set_address(struct langwell_udc *dev, u16 value,
2033 u16 index, u16 length) 2084 u16 index, u16 length)
2034{ 2085{
2035 VDBG(dev, "---> %s()\n", __func__); 2086 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2036 2087
2037 /* save the new address to device struct */ 2088 /* save the new address to device struct */
2038 dev->dev_addr = (u8) value; 2089 dev->dev_addr = (u8) value;
2039 VDBG(dev, "dev->dev_addr = %d\n", dev->dev_addr); 2090 dev_vdbg(&dev->pdev->dev, "dev->dev_addr = %d\n", dev->dev_addr);
2040 2091
2041 /* update usb state */ 2092 /* update usb state */
2042 dev->usb_state = USB_STATE_ADDRESS; 2093 dev->usb_state = USB_STATE_ADDRESS;
@@ -2045,7 +2096,7 @@ static void set_address(struct langwell_udc *dev, u16 value,
2045 if (prime_status_phase(dev, EP_DIR_IN)) 2096 if (prime_status_phase(dev, EP_DIR_IN))
2046 ep0_stall(dev); 2097 ep0_stall(dev);
2047 2098
2048 VDBG(dev, "<--- %s()\n", __func__); 2099 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2049} 2100}
2050 2101
2051 2102
@@ -2054,7 +2105,7 @@ static struct langwell_ep *get_ep_by_windex(struct langwell_udc *dev,
2054 u16 wIndex) 2105 u16 wIndex)
2055{ 2106{
2056 struct langwell_ep *ep; 2107 struct langwell_ep *ep;
2057 VDBG(dev, "---> %s()\n", __func__); 2108 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2058 2109
2059 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0) 2110 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2060 return &dev->ep[0]; 2111 return &dev->ep[0];
@@ -2073,7 +2124,7 @@ static struct langwell_ep *get_ep_by_windex(struct langwell_udc *dev,
2073 return ep; 2124 return ep;
2074 } 2125 }
2075 2126
2076 VDBG(dev, "<--- %s()\n", __func__); 2127 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2077 return NULL; 2128 return NULL;
2078} 2129}
2079 2130
@@ -2085,7 +2136,7 @@ static int ep_is_stall(struct langwell_ep *ep)
2085 u32 endptctrl; 2136 u32 endptctrl;
2086 int retval; 2137 int retval;
2087 2138
2088 VDBG(dev, "---> %s()\n", __func__); 2139 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2089 2140
2090 endptctrl = readl(&dev->op_regs->endptctrl[ep->ep_num]); 2141 endptctrl = readl(&dev->op_regs->endptctrl[ep->ep_num]);
2091 if (is_in(ep)) 2142 if (is_in(ep))
@@ -2093,7 +2144,7 @@ static int ep_is_stall(struct langwell_ep *ep)
2093 else 2144 else
2094 retval = endptctrl & EPCTRL_RXS ? 1 : 0; 2145 retval = endptctrl & EPCTRL_RXS ? 1 : 0;
2095 2146
2096 VDBG(dev, "<--- %s()\n", __func__); 2147 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2097 return retval; 2148 return retval;
2098} 2149}
2099 2150
@@ -2107,14 +2158,13 @@ static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
2107 u16 status_data = 0; /* 16 bits cpu view status data */ 2158 u16 status_data = 0; /* 16 bits cpu view status data */
2108 int status = 0; 2159 int status = 0;
2109 2160
2110 VDBG(dev, "---> %s()\n", __func__); 2161 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2111 2162
2112 ep = &dev->ep[0]; 2163 ep = &dev->ep[0];
2113 2164
2114 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) { 2165 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
2115 /* get device status */ 2166 /* get device status */
2116 status_data = 1 << USB_DEVICE_SELF_POWERED; 2167 status_data = dev->dev_status;
2117 status_data |= dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
2118 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) { 2168 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
2119 /* get interface status */ 2169 /* get interface status */
2120 status_data = 0; 2170 status_data = 0;
@@ -2129,6 +2179,8 @@ static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
2129 status_data = ep_is_stall(epn) << USB_ENDPOINT_HALT; 2179 status_data = ep_is_stall(epn) << USB_ENDPOINT_HALT;
2130 } 2180 }
2131 2181
2182 dev_dbg(&dev->pdev->dev, "get status data: 0x%04x\n", status_data);
2183
2132 dev->ep0_dir = USB_DIR_IN; 2184 dev->ep0_dir = USB_DIR_IN;
2133 2185
2134 /* borrow the per device status_req */ 2186 /* borrow the per device status_req */
@@ -2150,18 +2202,19 @@ static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
2150 goto stall; 2202 goto stall;
2151 2203
2152 if (status) { 2204 if (status) {
2153 ERROR(dev, "response error on GET_STATUS request\n"); 2205 dev_err(&dev->pdev->dev,
2206 "response error on GET_STATUS request\n");
2154 goto stall; 2207 goto stall;
2155 } 2208 }
2156 2209
2157 list_add_tail(&req->queue, &ep->queue); 2210 list_add_tail(&req->queue, &ep->queue);
2158 dev->ep0_state = DATA_STATE_XMIT; 2211 dev->ep0_state = DATA_STATE_XMIT;
2159 2212
2160 VDBG(dev, "<--- %s()\n", __func__); 2213 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2161 return; 2214 return;
2162stall: 2215stall:
2163 ep0_stall(dev); 2216 ep0_stall(dev);
2164 VDBG(dev, "<--- %s()\n", __func__); 2217 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2165} 2218}
2166 2219
2167 2220
@@ -2172,13 +2225,14 @@ static void handle_setup_packet(struct langwell_udc *dev,
2172 u16 wValue = le16_to_cpu(setup->wValue); 2225 u16 wValue = le16_to_cpu(setup->wValue);
2173 u16 wIndex = le16_to_cpu(setup->wIndex); 2226 u16 wIndex = le16_to_cpu(setup->wIndex);
2174 u16 wLength = le16_to_cpu(setup->wLength); 2227 u16 wLength = le16_to_cpu(setup->wLength);
2228 u32 portsc1;
2175 2229
2176 VDBG(dev, "---> %s()\n", __func__); 2230 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2177 2231
2178 /* ep0 fifo flush */ 2232 /* ep0 fifo flush */
2179 nuke(&dev->ep[0], -ESHUTDOWN); 2233 nuke(&dev->ep[0], -ESHUTDOWN);
2180 2234
2181 DBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n", 2235 dev_dbg(&dev->pdev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
2182 setup->bRequestType, setup->bRequest, 2236 setup->bRequestType, setup->bRequest,
2183 wValue, wIndex, wLength); 2237 wValue, wIndex, wLength);
2184 2238
@@ -2197,7 +2251,7 @@ static void handle_setup_packet(struct langwell_udc *dev,
2197 /* We process some stardard setup requests here */ 2251 /* We process some stardard setup requests here */
2198 switch (setup->bRequest) { 2252 switch (setup->bRequest) {
2199 case USB_REQ_GET_STATUS: 2253 case USB_REQ_GET_STATUS:
2200 DBG(dev, "SETUP: USB_REQ_GET_STATUS\n"); 2254 dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_GET_STATUS\n");
2201 /* get status, DATA and STATUS phase */ 2255 /* get status, DATA and STATUS phase */
2202 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) 2256 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2203 != (USB_DIR_IN | USB_TYPE_STANDARD)) 2257 != (USB_DIR_IN | USB_TYPE_STANDARD))
@@ -2206,7 +2260,7 @@ static void handle_setup_packet(struct langwell_udc *dev,
2206 goto end; 2260 goto end;
2207 2261
2208 case USB_REQ_SET_ADDRESS: 2262 case USB_REQ_SET_ADDRESS:
2209 DBG(dev, "SETUP: USB_REQ_SET_ADDRESS\n"); 2263 dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_SET_ADDRESS\n");
2210 /* STATUS phase */ 2264 /* STATUS phase */
2211 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD 2265 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
2212 | USB_RECIP_DEVICE)) 2266 | USB_RECIP_DEVICE))
@@ -2220,9 +2274,11 @@ static void handle_setup_packet(struct langwell_udc *dev,
2220 { 2274 {
2221 int rc = -EOPNOTSUPP; 2275 int rc = -EOPNOTSUPP;
2222 if (setup->bRequest == USB_REQ_SET_FEATURE) 2276 if (setup->bRequest == USB_REQ_SET_FEATURE)
2223 DBG(dev, "SETUP: USB_REQ_SET_FEATURE\n"); 2277 dev_dbg(&dev->pdev->dev,
2278 "SETUP: USB_REQ_SET_FEATURE\n");
2224 else if (setup->bRequest == USB_REQ_CLEAR_FEATURE) 2279 else if (setup->bRequest == USB_REQ_CLEAR_FEATURE)
2225 DBG(dev, "SETUP: USB_REQ_CLEAR_FEATURE\n"); 2280 dev_dbg(&dev->pdev->dev,
2281 "SETUP: USB_REQ_CLEAR_FEATURE\n");
2226 2282
2227 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK)) 2283 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
2228 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) { 2284 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
@@ -2240,13 +2296,51 @@ static void handle_setup_packet(struct langwell_udc *dev,
2240 2296
2241 spin_unlock(&dev->lock); 2297 spin_unlock(&dev->lock);
2242 rc = langwell_ep_set_halt(&epn->ep, 2298 rc = langwell_ep_set_halt(&epn->ep,
2243 (setup->bRequest == USB_REQ_SET_FEATURE) 2299 (setup->bRequest == USB_REQ_SET_FEATURE)
2244 ? 1 : 0); 2300 ? 1 : 0);
2245 spin_lock(&dev->lock); 2301 spin_lock(&dev->lock);
2246 2302
2247 } else if ((setup->bRequestType & (USB_RECIP_MASK 2303 } else if ((setup->bRequestType & (USB_RECIP_MASK
2248 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE 2304 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
2249 | USB_TYPE_STANDARD)) { 2305 | USB_TYPE_STANDARD)) {
2306 rc = 0;
2307 switch (wValue) {
2308 case USB_DEVICE_REMOTE_WAKEUP:
2309 if (setup->bRequest == USB_REQ_SET_FEATURE) {
2310 dev->remote_wakeup = 1;
2311 dev->dev_status |= (1 << wValue);
2312 } else {
2313 dev->remote_wakeup = 0;
2314 dev->dev_status &= ~(1 << wValue);
2315 }
2316 break;
2317 case USB_DEVICE_TEST_MODE:
2318 dev_dbg(&dev->pdev->dev, "SETUP: TEST MODE\n");
2319 if ((wIndex & 0xff) ||
2320 (dev->gadget.speed != USB_SPEED_HIGH))
2321 ep0_stall(dev);
2322
2323 switch (wIndex >> 8) {
2324 case TEST_J:
2325 case TEST_K:
2326 case TEST_SE0_NAK:
2327 case TEST_PACKET:
2328 case TEST_FORCE_EN:
2329 if (prime_status_phase(dev, EP_DIR_IN))
2330 ep0_stall(dev);
2331 portsc1 = readl(&dev->op_regs->portsc1);
2332 portsc1 |= (wIndex & 0xf00) << 8;
2333 writel(portsc1, &dev->op_regs->portsc1);
2334 goto end;
2335 default:
2336 rc = -EOPNOTSUPP;
2337 }
2338 break;
2339 default:
2340 rc = -EOPNOTSUPP;
2341 break;
2342 }
2343
2250 if (!gadget_is_otg(&dev->gadget)) 2344 if (!gadget_is_otg(&dev->gadget))
2251 break; 2345 break;
2252 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) { 2346 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) {
@@ -2262,7 +2356,6 @@ static void handle_setup_packet(struct langwell_udc *dev,
2262 dev->gadget.a_alt_hnp_support = 1; 2356 dev->gadget.a_alt_hnp_support = 1;
2263 else 2357 else
2264 break; 2358 break;
2265 rc = 0;
2266 } else 2359 } else
2267 break; 2360 break;
2268 2361
@@ -2274,31 +2367,38 @@ static void handle_setup_packet(struct langwell_udc *dev,
2274 } 2367 }
2275 2368
2276 case USB_REQ_GET_DESCRIPTOR: 2369 case USB_REQ_GET_DESCRIPTOR:
2277 DBG(dev, "SETUP: USB_REQ_GET_DESCRIPTOR\n"); 2370 dev_dbg(&dev->pdev->dev,
2371 "SETUP: USB_REQ_GET_DESCRIPTOR\n");
2278 goto delegate; 2372 goto delegate;
2279 2373
2280 case USB_REQ_SET_DESCRIPTOR: 2374 case USB_REQ_SET_DESCRIPTOR:
2281 DBG(dev, "SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n"); 2375 dev_dbg(&dev->pdev->dev,
2376 "SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n");
2282 goto delegate; 2377 goto delegate;
2283 2378
2284 case USB_REQ_GET_CONFIGURATION: 2379 case USB_REQ_GET_CONFIGURATION:
2285 DBG(dev, "SETUP: USB_REQ_GET_CONFIGURATION\n"); 2380 dev_dbg(&dev->pdev->dev,
2381 "SETUP: USB_REQ_GET_CONFIGURATION\n");
2286 goto delegate; 2382 goto delegate;
2287 2383
2288 case USB_REQ_SET_CONFIGURATION: 2384 case USB_REQ_SET_CONFIGURATION:
2289 DBG(dev, "SETUP: USB_REQ_SET_CONFIGURATION\n"); 2385 dev_dbg(&dev->pdev->dev,
2386 "SETUP: USB_REQ_SET_CONFIGURATION\n");
2290 goto delegate; 2387 goto delegate;
2291 2388
2292 case USB_REQ_GET_INTERFACE: 2389 case USB_REQ_GET_INTERFACE:
2293 DBG(dev, "SETUP: USB_REQ_GET_INTERFACE\n"); 2390 dev_dbg(&dev->pdev->dev,
2391 "SETUP: USB_REQ_GET_INTERFACE\n");
2294 goto delegate; 2392 goto delegate;
2295 2393
2296 case USB_REQ_SET_INTERFACE: 2394 case USB_REQ_SET_INTERFACE:
2297 DBG(dev, "SETUP: USB_REQ_SET_INTERFACE\n"); 2395 dev_dbg(&dev->pdev->dev,
2396 "SETUP: USB_REQ_SET_INTERFACE\n");
2298 goto delegate; 2397 goto delegate;
2299 2398
2300 case USB_REQ_SYNCH_FRAME: 2399 case USB_REQ_SYNCH_FRAME:
2301 DBG(dev, "SETUP: USB_REQ_SYNCH_FRAME unsupported\n"); 2400 dev_dbg(&dev->pdev->dev,
2401 "SETUP: USB_REQ_SYNCH_FRAME unsupported\n");
2302 goto delegate; 2402 goto delegate;
2303 2403
2304 default: 2404 default:
@@ -2310,7 +2410,8 @@ delegate:
2310 /* DATA phase from gadget, STATUS phase from udc */ 2410 /* DATA phase from gadget, STATUS phase from udc */
2311 dev->ep0_dir = (setup->bRequestType & USB_DIR_IN) 2411 dev->ep0_dir = (setup->bRequestType & USB_DIR_IN)
2312 ? USB_DIR_IN : USB_DIR_OUT; 2412 ? USB_DIR_IN : USB_DIR_OUT;
2313 VDBG(dev, "dev->ep0_dir = 0x%x, wLength = %d\n", 2413 dev_vdbg(&dev->pdev->dev,
2414 "dev->ep0_dir = 0x%x, wLength = %d\n",
2314 dev->ep0_dir, wLength); 2415 dev->ep0_dir, wLength);
2315 spin_unlock(&dev->lock); 2416 spin_unlock(&dev->lock);
2316 if (dev->driver->setup(&dev->gadget, 2417 if (dev->driver->setup(&dev->gadget,
@@ -2322,7 +2423,8 @@ delegate:
2322 } else { 2423 } else {
2323 /* no DATA phase, IN STATUS phase from gadget */ 2424 /* no DATA phase, IN STATUS phase from gadget */
2324 dev->ep0_dir = USB_DIR_IN; 2425 dev->ep0_dir = USB_DIR_IN;
2325 VDBG(dev, "dev->ep0_dir = 0x%x, wLength = %d\n", 2426 dev_vdbg(&dev->pdev->dev,
2427 "dev->ep0_dir = 0x%x, wLength = %d\n",
2326 dev->ep0_dir, wLength); 2428 dev->ep0_dir, wLength);
2327 spin_unlock(&dev->lock); 2429 spin_unlock(&dev->lock);
2328 if (dev->driver->setup(&dev->gadget, 2430 if (dev->driver->setup(&dev->gadget,
@@ -2334,8 +2436,7 @@ delegate:
2334 break; 2436 break;
2335 } 2437 }
2336end: 2438end:
2337 VDBG(dev, "<--- %s()\n", __func__); 2439 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2338 return;
2339} 2440}
2340 2441
2341 2442
@@ -2359,23 +2460,27 @@ static int process_ep_req(struct langwell_udc *dev, int index,
2359 td_complete = 0; 2460 td_complete = 0;
2360 actual = curr_req->req.length; 2461 actual = curr_req->req.length;
2361 2462
2362 VDBG(dev, "---> %s()\n", __func__); 2463 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2363 2464
2364 for (i = 0; i < curr_req->dtd_count; i++) { 2465 for (i = 0; i < curr_req->dtd_count; i++) {
2365 remaining_length = le16_to_cpu(curr_dtd->dtd_total);
2366 actual -= remaining_length;
2367 2466
2368 /* command execution states by dTD */ 2467 /* command execution states by dTD */
2369 dtd_status = curr_dtd->dtd_status; 2468 dtd_status = curr_dtd->dtd_status;
2370 2469
2470 barrier();
2471 remaining_length = le16_to_cpu(curr_dtd->dtd_total);
2472 actual -= remaining_length;
2473
2371 if (!dtd_status) { 2474 if (!dtd_status) {
2372 /* transfers completed successfully */ 2475 /* transfers completed successfully */
2373 if (!remaining_length) { 2476 if (!remaining_length) {
2374 td_complete++; 2477 td_complete++;
2375 VDBG(dev, "dTD transmitted successfully\n"); 2478 dev_vdbg(&dev->pdev->dev,
2479 "dTD transmitted successfully\n");
2376 } else { 2480 } else {
2377 if (dir) { 2481 if (dir) {
2378 VDBG(dev, "TX dTD remains data\n"); 2482 dev_vdbg(&dev->pdev->dev,
2483 "TX dTD remains data\n");
2379 retval = -EPROTO; 2484 retval = -EPROTO;
2380 break; 2485 break;
2381 2486
@@ -2387,27 +2492,32 @@ static int process_ep_req(struct langwell_udc *dev, int index,
2387 } else { 2492 } else {
2388 /* transfers completed with errors */ 2493 /* transfers completed with errors */
2389 if (dtd_status & DTD_STS_ACTIVE) { 2494 if (dtd_status & DTD_STS_ACTIVE) {
2390 DBG(dev, "request not completed\n"); 2495 dev_dbg(&dev->pdev->dev,
2496 "dTD status ACTIVE dQH[%d]\n", index);
2391 retval = 1; 2497 retval = 1;
2392 return retval; 2498 return retval;
2393 } else if (dtd_status & DTD_STS_HALTED) { 2499 } else if (dtd_status & DTD_STS_HALTED) {
2394 ERROR(dev, "dTD error %08x dQH[%d]\n", 2500 dev_err(&dev->pdev->dev,
2395 dtd_status, index); 2501 "dTD error %08x dQH[%d]\n",
2502 dtd_status, index);
2396 /* clear the errors and halt condition */ 2503 /* clear the errors and halt condition */
2397 curr_dqh->dtd_status = 0; 2504 curr_dqh->dtd_status = 0;
2398 retval = -EPIPE; 2505 retval = -EPIPE;
2399 break; 2506 break;
2400 } else if (dtd_status & DTD_STS_DBE) { 2507 } else if (dtd_status & DTD_STS_DBE) {
2401 DBG(dev, "data buffer (overflow) error\n"); 2508 dev_dbg(&dev->pdev->dev,
2509 "data buffer (overflow) error\n");
2402 retval = -EPROTO; 2510 retval = -EPROTO;
2403 break; 2511 break;
2404 } else if (dtd_status & DTD_STS_TRE) { 2512 } else if (dtd_status & DTD_STS_TRE) {
2405 DBG(dev, "transaction(ISO) error\n"); 2513 dev_dbg(&dev->pdev->dev,
2514 "transaction(ISO) error\n");
2406 retval = -EILSEQ; 2515 retval = -EILSEQ;
2407 break; 2516 break;
2408 } else 2517 } else
2409 ERROR(dev, "unknown error (0x%x)!\n", 2518 dev_err(&dev->pdev->dev,
2410 dtd_status); 2519 "unknown error (0x%x)!\n",
2520 dtd_status);
2411 } 2521 }
2412 2522
2413 if (i != curr_req->dtd_count - 1) 2523 if (i != curr_req->dtd_count - 1)
@@ -2420,7 +2530,7 @@ static int process_ep_req(struct langwell_udc *dev, int index,
2420 2530
2421 curr_req->req.actual = actual; 2531 curr_req->req.actual = actual;
2422 2532
2423 VDBG(dev, "<--- %s()\n", __func__); 2533 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2424 return 0; 2534 return 0;
2425} 2535}
2426 2536
@@ -2430,7 +2540,7 @@ static void ep0_req_complete(struct langwell_udc *dev,
2430 struct langwell_ep *ep0, struct langwell_request *req) 2540 struct langwell_ep *ep0, struct langwell_request *req)
2431{ 2541{
2432 u32 new_addr; 2542 u32 new_addr;
2433 VDBG(dev, "---> %s()\n", __func__); 2543 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2434 2544
2435 if (dev->usb_state == USB_STATE_ADDRESS) { 2545 if (dev->usb_state == USB_STATE_ADDRESS) {
2436 /* set the new address */ 2546 /* set the new address */
@@ -2438,7 +2548,7 @@ static void ep0_req_complete(struct langwell_udc *dev,
2438 writel(new_addr << USBADR_SHIFT, &dev->op_regs->deviceaddr); 2548 writel(new_addr << USBADR_SHIFT, &dev->op_regs->deviceaddr);
2439 2549
2440 new_addr = USBADR(readl(&dev->op_regs->deviceaddr)); 2550 new_addr = USBADR(readl(&dev->op_regs->deviceaddr));
2441 VDBG(dev, "new_addr = %d\n", new_addr); 2551 dev_vdbg(&dev->pdev->dev, "new_addr = %d\n", new_addr);
2442 } 2552 }
2443 2553
2444 done(ep0, req, 0); 2554 done(ep0, req, 0);
@@ -2458,14 +2568,14 @@ static void ep0_req_complete(struct langwell_udc *dev,
2458 dev->ep0_state = WAIT_FOR_SETUP; 2568 dev->ep0_state = WAIT_FOR_SETUP;
2459 break; 2569 break;
2460 case WAIT_FOR_SETUP: 2570 case WAIT_FOR_SETUP:
2461 ERROR(dev, "unexpect ep0 packets\n"); 2571 dev_err(&dev->pdev->dev, "unexpect ep0 packets\n");
2462 break; 2572 break;
2463 default: 2573 default:
2464 ep0_stall(dev); 2574 ep0_stall(dev);
2465 break; 2575 break;
2466 } 2576 }
2467 2577
2468 VDBG(dev, "<--- %s()\n", __func__); 2578 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2469} 2579}
2470 2580
2471 2581
@@ -2477,16 +2587,17 @@ static void handle_trans_complete(struct langwell_udc *dev)
2477 struct langwell_ep *epn; 2587 struct langwell_ep *epn;
2478 struct langwell_request *curr_req, *temp_req; 2588 struct langwell_request *curr_req, *temp_req;
2479 2589
2480 VDBG(dev, "---> %s()\n", __func__); 2590 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2481 2591
2482 complete_bits = readl(&dev->op_regs->endptcomplete); 2592 complete_bits = readl(&dev->op_regs->endptcomplete);
2483 VDBG(dev, "endptcomplete register: 0x%08x\n", complete_bits); 2593 dev_vdbg(&dev->pdev->dev, "endptcomplete register: 0x%08x\n",
2594 complete_bits);
2484 2595
2485 /* Write-Clear the bits in endptcomplete register */ 2596 /* Write-Clear the bits in endptcomplete register */
2486 writel(complete_bits, &dev->op_regs->endptcomplete); 2597 writel(complete_bits, &dev->op_regs->endptcomplete);
2487 2598
2488 if (!complete_bits) { 2599 if (!complete_bits) {
2489 DBG(dev, "complete_bits = 0\n"); 2600 dev_dbg(&dev->pdev->dev, "complete_bits = 0\n");
2490 goto done; 2601 goto done;
2491 } 2602 }
2492 2603
@@ -2506,23 +2617,25 @@ static void handle_trans_complete(struct langwell_udc *dev)
2506 epn = &dev->ep[i]; 2617 epn = &dev->ep[i];
2507 2618
2508 if (epn->name == NULL) { 2619 if (epn->name == NULL) {
2509 WARNING(dev, "invalid endpoint\n"); 2620 dev_warn(&dev->pdev->dev, "invalid endpoint\n");
2510 continue; 2621 continue;
2511 } 2622 }
2512 2623
2513 if (i < 2) 2624 if (i < 2)
2514 /* ep0 in and out */ 2625 /* ep0 in and out */
2515 DBG(dev, "%s-%s transfer completed\n", 2626 dev_dbg(&dev->pdev->dev, "%s-%s transfer completed\n",
2516 epn->name, 2627 epn->name,
2517 is_in(epn) ? "in" : "out"); 2628 is_in(epn) ? "in" : "out");
2518 else 2629 else
2519 DBG(dev, "%s transfer completed\n", epn->name); 2630 dev_dbg(&dev->pdev->dev, "%s transfer completed\n",
2631 epn->name);
2520 2632
2521 /* process the req queue until an uncomplete request */ 2633 /* process the req queue until an uncomplete request */
2522 list_for_each_entry_safe(curr_req, temp_req, 2634 list_for_each_entry_safe(curr_req, temp_req,
2523 &epn->queue, queue) { 2635 &epn->queue, queue) {
2524 status = process_ep_req(dev, i, curr_req); 2636 status = process_ep_req(dev, i, curr_req);
2525 VDBG(dev, "%s req status: %d\n", epn->name, status); 2637 dev_vdbg(&dev->pdev->dev, "%s req status: %d\n",
2638 epn->name, status);
2526 2639
2527 if (status) 2640 if (status)
2528 break; 2641 break;
@@ -2540,8 +2653,7 @@ static void handle_trans_complete(struct langwell_udc *dev)
2540 } 2653 }
2541 } 2654 }
2542done: 2655done:
2543 VDBG(dev, "<--- %s()\n", __func__); 2656 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2544 return;
2545} 2657}
2546 2658
2547 2659
@@ -2551,14 +2663,14 @@ static void handle_port_change(struct langwell_udc *dev)
2551 u32 portsc1, devlc; 2663 u32 portsc1, devlc;
2552 u32 speed; 2664 u32 speed;
2553 2665
2554 VDBG(dev, "---> %s()\n", __func__); 2666 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2555 2667
2556 if (dev->bus_reset) 2668 if (dev->bus_reset)
2557 dev->bus_reset = 0; 2669 dev->bus_reset = 0;
2558 2670
2559 portsc1 = readl(&dev->op_regs->portsc1); 2671 portsc1 = readl(&dev->op_regs->portsc1);
2560 devlc = readl(&dev->op_regs->devlc); 2672 devlc = readl(&dev->op_regs->devlc);
2561 VDBG(dev, "portsc1 = 0x%08x, devlc = 0x%08x\n", 2673 dev_vdbg(&dev->pdev->dev, "portsc1 = 0x%08x, devlc = 0x%08x\n",
2562 portsc1, devlc); 2674 portsc1, devlc);
2563 2675
2564 /* bus reset is finished */ 2676 /* bus reset is finished */
@@ -2579,25 +2691,22 @@ static void handle_port_change(struct langwell_udc *dev)
2579 dev->gadget.speed = USB_SPEED_UNKNOWN; 2691 dev->gadget.speed = USB_SPEED_UNKNOWN;
2580 break; 2692 break;
2581 } 2693 }
2582 VDBG(dev, "speed = %d, dev->gadget.speed = %d\n", 2694 dev_vdbg(&dev->pdev->dev,
2695 "speed = %d, dev->gadget.speed = %d\n",
2583 speed, dev->gadget.speed); 2696 speed, dev->gadget.speed);
2584 } 2697 }
2585 2698
2586 /* LPM L0 to L1 */ 2699 /* LPM L0 to L1 */
2587 if (dev->lpm && dev->lpm_state == LPM_L0) 2700 if (dev->lpm && dev->lpm_state == LPM_L0)
2588 if (portsc1 & PORTS_SUSP && portsc1 & PORTS_SLP) { 2701 if (portsc1 & PORTS_SUSP && portsc1 & PORTS_SLP) {
2589 INFO(dev, "LPM L0 to L1\n"); 2702 dev_info(&dev->pdev->dev, "LPM L0 to L1\n");
2590 dev->lpm_state = LPM_L1; 2703 dev->lpm_state = LPM_L1;
2591 } 2704 }
2592 2705
2593 /* LPM L1 to L0, force resume or remote wakeup finished */ 2706 /* LPM L1 to L0, force resume or remote wakeup finished */
2594 if (dev->lpm && dev->lpm_state == LPM_L1) 2707 if (dev->lpm && dev->lpm_state == LPM_L1)
2595 if (!(portsc1 & PORTS_SUSP)) { 2708 if (!(portsc1 & PORTS_SUSP)) {
2596 if (portsc1 & PORTS_SLP) 2709 dev_info(&dev->pdev->dev, "LPM L1 to L0\n");
2597 INFO(dev, "LPM L1 to L0, force resume\n");
2598 else
2599 INFO(dev, "LPM L1 to L0, remote wakeup\n");
2600
2601 dev->lpm_state = LPM_L0; 2710 dev->lpm_state = LPM_L0;
2602 } 2711 }
2603 2712
@@ -2605,7 +2714,7 @@ static void handle_port_change(struct langwell_udc *dev)
2605 if (!dev->resume_state) 2714 if (!dev->resume_state)
2606 dev->usb_state = USB_STATE_DEFAULT; 2715 dev->usb_state = USB_STATE_DEFAULT;
2607 2716
2608 VDBG(dev, "<--- %s()\n", __func__); 2717 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2609} 2718}
2610 2719
2611 2720
@@ -2617,7 +2726,7 @@ static void handle_usb_reset(struct langwell_udc *dev)
2617 endptcomplete; 2726 endptcomplete;
2618 unsigned long timeout; 2727 unsigned long timeout;
2619 2728
2620 VDBG(dev, "---> %s()\n", __func__); 2729 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2621 2730
2622 /* Write-Clear the device address */ 2731 /* Write-Clear the device address */
2623 deviceaddr = readl(&dev->op_regs->deviceaddr); 2732 deviceaddr = readl(&dev->op_regs->deviceaddr);
@@ -2634,7 +2743,10 @@ static void handle_usb_reset(struct langwell_udc *dev)
2634 2743
2635 dev->ep0_dir = USB_DIR_OUT; 2744 dev->ep0_dir = USB_DIR_OUT;
2636 dev->ep0_state = WAIT_FOR_SETUP; 2745 dev->ep0_state = WAIT_FOR_SETUP;
2637 dev->remote_wakeup = 0; /* default to 0 on reset */ 2746
2747 /* remote wakeup reset to 0 when the device is reset */
2748 dev->remote_wakeup = 0;
2749 dev->dev_status = 1 << USB_DEVICE_SELF_POWERED;
2638 dev->gadget.b_hnp_enable = 0; 2750 dev->gadget.b_hnp_enable = 0;
2639 dev->gadget.a_hnp_support = 0; 2751 dev->gadget.a_hnp_support = 0;
2640 dev->gadget.a_alt_hnp_support = 0; 2752 dev->gadget.a_alt_hnp_support = 0;
@@ -2651,7 +2763,7 @@ static void handle_usb_reset(struct langwell_udc *dev)
2651 timeout = jiffies + PRIME_TIMEOUT; 2763 timeout = jiffies + PRIME_TIMEOUT;
2652 while (readl(&dev->op_regs->endptprime)) { 2764 while (readl(&dev->op_regs->endptprime)) {
2653 if (time_after(jiffies, timeout)) { 2765 if (time_after(jiffies, timeout)) {
2654 ERROR(dev, "USB reset timeout\n"); 2766 dev_err(&dev->pdev->dev, "USB reset timeout\n");
2655 break; 2767 break;
2656 } 2768 }
2657 cpu_relax(); 2769 cpu_relax();
@@ -2661,7 +2773,7 @@ static void handle_usb_reset(struct langwell_udc *dev)
2661 writel((u32) ~0, &dev->op_regs->endptflush); 2773 writel((u32) ~0, &dev->op_regs->endptflush);
2662 2774
2663 if (readl(&dev->op_regs->portsc1) & PORTS_PR) { 2775 if (readl(&dev->op_regs->portsc1) & PORTS_PR) {
2664 VDBG(dev, "USB bus reset\n"); 2776 dev_vdbg(&dev->pdev->dev, "USB bus reset\n");
2665 /* bus is reseting */ 2777 /* bus is reseting */
2666 dev->bus_reset = 1; 2778 dev->bus_reset = 1;
2667 2779
@@ -2669,7 +2781,7 @@ static void handle_usb_reset(struct langwell_udc *dev)
2669 stop_activity(dev, dev->driver); 2781 stop_activity(dev, dev->driver);
2670 dev->usb_state = USB_STATE_DEFAULT; 2782 dev->usb_state = USB_STATE_DEFAULT;
2671 } else { 2783 } else {
2672 VDBG(dev, "device controller reset\n"); 2784 dev_vdbg(&dev->pdev->dev, "device controller reset\n");
2673 /* controller reset */ 2785 /* controller reset */
2674 langwell_udc_reset(dev); 2786 langwell_udc_reset(dev);
2675 2787
@@ -2691,15 +2803,14 @@ static void handle_usb_reset(struct langwell_udc *dev)
2691 dev->lotg->hsm.b_hnp_enable = 0; 2803 dev->lotg->hsm.b_hnp_enable = 0;
2692#endif 2804#endif
2693 2805
2694 VDBG(dev, "<--- %s()\n", __func__); 2806 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2695} 2807}
2696 2808
2697 2809
2698/* USB bus suspend/resume interrupt */ 2810/* USB bus suspend/resume interrupt */
2699static void handle_bus_suspend(struct langwell_udc *dev) 2811static void handle_bus_suspend(struct langwell_udc *dev)
2700{ 2812{
2701 u32 devlc; 2813 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2702 DBG(dev, "---> %s()\n", __func__);
2703 2814
2704 dev->resume_state = dev->usb_state; 2815 dev->resume_state = dev->usb_state;
2705 dev->usb_state = USB_STATE_SUSPENDED; 2816 dev->usb_state = USB_STATE_SUSPENDED;
@@ -2733,33 +2844,29 @@ static void handle_bus_suspend(struct langwell_udc *dev)
2733 spin_unlock(&dev->lock); 2844 spin_unlock(&dev->lock);
2734 dev->driver->suspend(&dev->gadget); 2845 dev->driver->suspend(&dev->gadget);
2735 spin_lock(&dev->lock); 2846 spin_lock(&dev->lock);
2736 DBG(dev, "suspend %s\n", dev->driver->driver.name); 2847 dev_dbg(&dev->pdev->dev, "suspend %s\n",
2848 dev->driver->driver.name);
2737 } 2849 }
2738 } 2850 }
2739 2851
2740 /* enter PHY low power suspend */ 2852 /* enter PHY low power suspend */
2741 devlc = readl(&dev->op_regs->devlc); 2853 if (dev->pdev->device != 0x0829)
2742 VDBG(dev, "devlc = 0x%08x\n", devlc); 2854 langwell_phy_low_power(dev, 0);
2743 devlc |= LPM_PHCD;
2744 writel(devlc, &dev->op_regs->devlc);
2745 2855
2746 DBG(dev, "<--- %s()\n", __func__); 2856 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2747} 2857}
2748 2858
2749 2859
2750static void handle_bus_resume(struct langwell_udc *dev) 2860static void handle_bus_resume(struct langwell_udc *dev)
2751{ 2861{
2752 u32 devlc; 2862 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2753 DBG(dev, "---> %s()\n", __func__);
2754 2863
2755 dev->usb_state = dev->resume_state; 2864 dev->usb_state = dev->resume_state;
2756 dev->resume_state = 0; 2865 dev->resume_state = 0;
2757 2866
2758 /* exit PHY low power suspend */ 2867 /* exit PHY low power suspend */
2759 devlc = readl(&dev->op_regs->devlc); 2868 if (dev->pdev->device != 0x0829)
2760 VDBG(dev, "devlc = 0x%08x\n", devlc); 2869 langwell_phy_low_power(dev, 0);
2761 devlc &= ~LPM_PHCD;
2762 writel(devlc, &dev->op_regs->devlc);
2763 2870
2764#ifdef OTG_TRANSCEIVER 2871#ifdef OTG_TRANSCEIVER
2765 if (dev->lotg->otg.default_a == 0) 2872 if (dev->lotg->otg.default_a == 0)
@@ -2772,11 +2879,12 @@ static void handle_bus_resume(struct langwell_udc *dev)
2772 spin_unlock(&dev->lock); 2879 spin_unlock(&dev->lock);
2773 dev->driver->resume(&dev->gadget); 2880 dev->driver->resume(&dev->gadget);
2774 spin_lock(&dev->lock); 2881 spin_lock(&dev->lock);
2775 DBG(dev, "resume %s\n", dev->driver->driver.name); 2882 dev_dbg(&dev->pdev->dev, "resume %s\n",
2883 dev->driver->driver.name);
2776 } 2884 }
2777 } 2885 }
2778 2886
2779 DBG(dev, "<--- %s()\n", __func__); 2887 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2780} 2888}
2781 2889
2782 2890
@@ -2789,11 +2897,11 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2789 irq_sts, 2897 irq_sts,
2790 portsc1; 2898 portsc1;
2791 2899
2792 VDBG(dev, "---> %s()\n", __func__); 2900 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2793 2901
2794 if (dev->stopped) { 2902 if (dev->stopped) {
2795 VDBG(dev, "handle IRQ_NONE\n"); 2903 dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
2796 VDBG(dev, "<--- %s()\n", __func__); 2904 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2797 return IRQ_NONE; 2905 return IRQ_NONE;
2798 } 2906 }
2799 2907
@@ -2806,12 +2914,13 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2806 usbintr = readl(&dev->op_regs->usbintr); 2914 usbintr = readl(&dev->op_regs->usbintr);
2807 2915
2808 irq_sts = usbsts & usbintr; 2916 irq_sts = usbsts & usbintr;
2809 VDBG(dev, "usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n", 2917 dev_vdbg(&dev->pdev->dev,
2918 "usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n",
2810 usbsts, usbintr, irq_sts); 2919 usbsts, usbintr, irq_sts);
2811 2920
2812 if (!irq_sts) { 2921 if (!irq_sts) {
2813 VDBG(dev, "handle IRQ_NONE\n"); 2922 dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
2814 VDBG(dev, "<--- %s()\n", __func__); 2923 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2815 spin_unlock(&dev->lock); 2924 spin_unlock(&dev->lock);
2816 return IRQ_NONE; 2925 return IRQ_NONE;
2817 } 2926 }
@@ -2827,12 +2936,13 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2827 2936
2828 /* USB interrupt */ 2937 /* USB interrupt */
2829 if (irq_sts & STS_UI) { 2938 if (irq_sts & STS_UI) {
2830 VDBG(dev, "USB interrupt\n"); 2939 dev_vdbg(&dev->pdev->dev, "USB interrupt\n");
2831 2940
2832 /* setup packet received from ep0 */ 2941 /* setup packet received from ep0 */
2833 if (readl(&dev->op_regs->endptsetupstat) 2942 if (readl(&dev->op_regs->endptsetupstat)
2834 & EP0SETUPSTAT_MASK) { 2943 & EP0SETUPSTAT_MASK) {
2835 VDBG(dev, "USB SETUP packet received interrupt\n"); 2944 dev_vdbg(&dev->pdev->dev,
2945 "USB SETUP packet received interrupt\n");
2836 /* setup tripwire semaphone */ 2946 /* setup tripwire semaphone */
2837 setup_tripwire(dev); 2947 setup_tripwire(dev);
2838 handle_setup_packet(dev, &dev->local_setup_buff); 2948 handle_setup_packet(dev, &dev->local_setup_buff);
@@ -2840,7 +2950,8 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2840 2950
2841 /* USB transfer completion */ 2951 /* USB transfer completion */
2842 if (readl(&dev->op_regs->endptcomplete)) { 2952 if (readl(&dev->op_regs->endptcomplete)) {
2843 VDBG(dev, "USB transfer completion interrupt\n"); 2953 dev_vdbg(&dev->pdev->dev,
2954 "USB transfer completion interrupt\n");
2844 handle_trans_complete(dev); 2955 handle_trans_complete(dev);
2845 } 2956 }
2846 } 2957 }
@@ -2848,36 +2959,36 @@ static irqreturn_t langwell_irq(int irq, void *_dev)
2848 /* SOF received interrupt (for ISO transfer) */ 2959 /* SOF received interrupt (for ISO transfer) */
2849 if (irq_sts & STS_SRI) { 2960 if (irq_sts & STS_SRI) {
2850 /* FIXME */ 2961 /* FIXME */
2851 /* VDBG(dev, "SOF received interrupt\n"); */ 2962 /* dev_vdbg(&dev->pdev->dev, "SOF received interrupt\n"); */
2852 } 2963 }
2853 2964
2854 /* port change detect interrupt */ 2965 /* port change detect interrupt */
2855 if (irq_sts & STS_PCI) { 2966 if (irq_sts & STS_PCI) {
2856 VDBG(dev, "port change detect interrupt\n"); 2967 dev_vdbg(&dev->pdev->dev, "port change detect interrupt\n");
2857 handle_port_change(dev); 2968 handle_port_change(dev);
2858 } 2969 }
2859 2970
2860 /* suspend interrrupt */ 2971 /* suspend interrrupt */
2861 if (irq_sts & STS_SLI) { 2972 if (irq_sts & STS_SLI) {
2862 VDBG(dev, "suspend interrupt\n"); 2973 dev_vdbg(&dev->pdev->dev, "suspend interrupt\n");
2863 handle_bus_suspend(dev); 2974 handle_bus_suspend(dev);
2864 } 2975 }
2865 2976
2866 /* USB reset interrupt */ 2977 /* USB reset interrupt */
2867 if (irq_sts & STS_URI) { 2978 if (irq_sts & STS_URI) {
2868 VDBG(dev, "USB reset interrupt\n"); 2979 dev_vdbg(&dev->pdev->dev, "USB reset interrupt\n");
2869 handle_usb_reset(dev); 2980 handle_usb_reset(dev);
2870 } 2981 }
2871 2982
2872 /* USB error or system error interrupt */ 2983 /* USB error or system error interrupt */
2873 if (irq_sts & (STS_UEI | STS_SEI)) { 2984 if (irq_sts & (STS_UEI | STS_SEI)) {
2874 /* FIXME */ 2985 /* FIXME */
2875 WARNING(dev, "error IRQ, irq_sts: %x\n", irq_sts); 2986 dev_warn(&dev->pdev->dev, "error IRQ, irq_sts: %x\n", irq_sts);
2876 } 2987 }
2877 2988
2878 spin_unlock(&dev->lock); 2989 spin_unlock(&dev->lock);
2879 2990
2880 VDBG(dev, "<--- %s()\n", __func__); 2991 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2881 return IRQ_HANDLED; 2992 return IRQ_HANDLED;
2882} 2993}
2883 2994
@@ -2889,15 +3000,59 @@ static void gadget_release(struct device *_dev)
2889{ 3000{
2890 struct langwell_udc *dev = the_controller; 3001 struct langwell_udc *dev = the_controller;
2891 3002
2892 DBG(dev, "---> %s()\n", __func__); 3003 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2893 3004
2894 complete(dev->done); 3005 complete(dev->done);
2895 3006
2896 DBG(dev, "<--- %s()\n", __func__); 3007 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2897 kfree(dev); 3008 kfree(dev);
2898} 3009}
2899 3010
2900 3011
3012/* enable SRAM caching if SRAM detected */
3013static void sram_init(struct langwell_udc *dev)
3014{
3015 struct pci_dev *pdev = dev->pdev;
3016
3017 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3018
3019 dev->sram_addr = pci_resource_start(pdev, 1);
3020 dev->sram_size = pci_resource_len(pdev, 1);
3021 dev_info(&dev->pdev->dev, "Found private SRAM at %x size:%x\n",
3022 dev->sram_addr, dev->sram_size);
3023 dev->got_sram = 1;
3024
3025 if (pci_request_region(pdev, 1, kobject_name(&pdev->dev.kobj))) {
3026 dev_warn(&dev->pdev->dev, "SRAM request failed\n");
3027 dev->got_sram = 0;
3028 } else if (!dma_declare_coherent_memory(&pdev->dev, dev->sram_addr,
3029 dev->sram_addr, dev->sram_size, DMA_MEMORY_MAP)) {
3030 dev_warn(&dev->pdev->dev, "SRAM DMA declare failed\n");
3031 pci_release_region(pdev, 1);
3032 dev->got_sram = 0;
3033 }
3034
3035 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3036}
3037
3038
3039/* release SRAM caching */
3040static void sram_deinit(struct langwell_udc *dev)
3041{
3042 struct pci_dev *pdev = dev->pdev;
3043
3044 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3045
3046 dma_release_declared_memory(&pdev->dev);
3047 pci_release_region(pdev, 1);
3048
3049 dev->got_sram = 0;
3050
3051 dev_info(&dev->pdev->dev, "release SRAM caching\n");
3052 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3053}
3054
3055
2901/* tear down the binding between this driver and the pci device */ 3056/* tear down the binding between this driver and the pci device */
2902static void langwell_udc_remove(struct pci_dev *pdev) 3057static void langwell_udc_remove(struct pci_dev *pdev)
2903{ 3058{
@@ -2906,26 +3061,32 @@ static void langwell_udc_remove(struct pci_dev *pdev)
2906 DECLARE_COMPLETION(done); 3061 DECLARE_COMPLETION(done);
2907 3062
2908 BUG_ON(dev->driver); 3063 BUG_ON(dev->driver);
2909 DBG(dev, "---> %s()\n", __func__); 3064 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2910 3065
2911 dev->done = &done; 3066 dev->done = &done;
2912 3067
2913 /* free memory allocated in probe */ 3068#ifndef OTG_TRANSCEIVER
3069 /* free dTD dma_pool and dQH */
2914 if (dev->dtd_pool) 3070 if (dev->dtd_pool)
2915 dma_pool_destroy(dev->dtd_pool); 3071 dma_pool_destroy(dev->dtd_pool);
2916 3072
3073 if (dev->ep_dqh)
3074 dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
3075 dev->ep_dqh, dev->ep_dqh_dma);
3076
3077 /* release SRAM caching */
3078 if (dev->has_sram && dev->got_sram)
3079 sram_deinit(dev);
3080#endif
3081
2917 if (dev->status_req) { 3082 if (dev->status_req) {
2918 kfree(dev->status_req->req.buf); 3083 kfree(dev->status_req->req.buf);
2919 kfree(dev->status_req); 3084 kfree(dev->status_req);
2920 } 3085 }
2921 3086
2922 if (dev->ep_dqh)
2923 dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
2924 dev->ep_dqh, dev->ep_dqh_dma);
2925
2926 kfree(dev->ep); 3087 kfree(dev->ep);
2927 3088
2928 /* diable IRQ handler */ 3089 /* disable IRQ handler */
2929 if (dev->got_irq) 3090 if (dev->got_irq)
2930 free_irq(pdev->irq, dev); 3091 free_irq(pdev->irq, dev);
2931 3092
@@ -2949,11 +3110,12 @@ static void langwell_udc_remove(struct pci_dev *pdev)
2949 3110
2950 dev->cap_regs = NULL; 3111 dev->cap_regs = NULL;
2951 3112
2952 INFO(dev, "unbind\n"); 3113 dev_info(&dev->pdev->dev, "unbind\n");
2953 DBG(dev, "<--- %s()\n", __func__); 3114 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2954 3115
2955 device_unregister(&dev->gadget.dev); 3116 device_unregister(&dev->gadget.dev);
2956 device_remove_file(&pdev->dev, &dev_attr_langwell_udc); 3117 device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
3118 device_remove_file(&pdev->dev, &dev_attr_remote_wakeup);
2957 3119
2958#ifndef OTG_TRANSCEIVER 3120#ifndef OTG_TRANSCEIVER
2959 pci_set_drvdata(pdev, NULL); 3121 pci_set_drvdata(pdev, NULL);
@@ -2997,7 +3159,7 @@ static int langwell_udc_probe(struct pci_dev *pdev,
2997 spin_lock_init(&dev->lock); 3159 spin_lock_init(&dev->lock);
2998 3160
2999 dev->pdev = pdev; 3161 dev->pdev = pdev;
3000 DBG(dev, "---> %s()\n", __func__); 3162 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3001 3163
3002#ifdef OTG_TRANSCEIVER 3164#ifdef OTG_TRANSCEIVER
3003 /* PCI device is already enabled by otg_transceiver driver */ 3165 /* PCI device is already enabled by otg_transceiver driver */
@@ -3022,7 +3184,7 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3022 resource = pci_resource_start(pdev, 0); 3184 resource = pci_resource_start(pdev, 0);
3023 len = pci_resource_len(pdev, 0); 3185 len = pci_resource_len(pdev, 0);
3024 if (!request_mem_region(resource, len, driver_name)) { 3186 if (!request_mem_region(resource, len, driver_name)) {
3025 ERROR(dev, "controller already in use\n"); 3187 dev_err(&dev->pdev->dev, "controller already in use\n");
3026 retval = -EBUSY; 3188 retval = -EBUSY;
3027 goto error; 3189 goto error;
3028 } 3190 }
@@ -3031,33 +3193,43 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3031 base = ioremap_nocache(resource, len); 3193 base = ioremap_nocache(resource, len);
3032#endif 3194#endif
3033 if (base == NULL) { 3195 if (base == NULL) {
3034 ERROR(dev, "can't map memory\n"); 3196 dev_err(&dev->pdev->dev, "can't map memory\n");
3035 retval = -EFAULT; 3197 retval = -EFAULT;
3036 goto error; 3198 goto error;
3037 } 3199 }
3038 3200
3039 dev->cap_regs = (struct langwell_cap_regs __iomem *) base; 3201 dev->cap_regs = (struct langwell_cap_regs __iomem *) base;
3040 VDBG(dev, "dev->cap_regs: %p\n", dev->cap_regs); 3202 dev_vdbg(&dev->pdev->dev, "dev->cap_regs: %p\n", dev->cap_regs);
3041 dev->op_regs = (struct langwell_op_regs __iomem *) 3203 dev->op_regs = (struct langwell_op_regs __iomem *)
3042 (base + OP_REG_OFFSET); 3204 (base + OP_REG_OFFSET);
3043 VDBG(dev, "dev->op_regs: %p\n", dev->op_regs); 3205 dev_vdbg(&dev->pdev->dev, "dev->op_regs: %p\n", dev->op_regs);
3044 3206
3045 /* irq setup after old hardware is cleaned up */ 3207 /* irq setup after old hardware is cleaned up */
3046 if (!pdev->irq) { 3208 if (!pdev->irq) {
3047 ERROR(dev, "No IRQ. Check PCI setup!\n"); 3209 dev_err(&dev->pdev->dev, "No IRQ. Check PCI setup!\n");
3048 retval = -ENODEV; 3210 retval = -ENODEV;
3049 goto error; 3211 goto error;
3050 } 3212 }
3051 3213
3214 dev->has_sram = 1;
3215 dev->got_sram = 0;
3216 dev_vdbg(&dev->pdev->dev, "dev->has_sram: %d\n", dev->has_sram);
3217
3052#ifndef OTG_TRANSCEIVER 3218#ifndef OTG_TRANSCEIVER
3053 INFO(dev, "irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n", 3219 /* enable SRAM caching if detected */
3220 if (dev->has_sram && !dev->got_sram)
3221 sram_init(dev);
3222
3223 dev_info(&dev->pdev->dev,
3224 "irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n",
3054 pdev->irq, resource, len, base); 3225 pdev->irq, resource, len, base);
3055 /* enables bus-mastering for device dev */ 3226 /* enables bus-mastering for device dev */
3056 pci_set_master(pdev); 3227 pci_set_master(pdev);
3057 3228
3058 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED, 3229 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
3059 driver_name, dev) != 0) { 3230 driver_name, dev) != 0) {
3060 ERROR(dev, "request interrupt %d failed\n", pdev->irq); 3231 dev_err(&dev->pdev->dev,
3232 "request interrupt %d failed\n", pdev->irq);
3061 retval = -EBUSY; 3233 retval = -EBUSY;
3062 goto error; 3234 goto error;
3063 } 3235 }
@@ -3071,32 +3243,34 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3071 dev->lpm = (readl(&dev->cap_regs->hccparams) & HCC_LEN) ? 1 : 0; 3243 dev->lpm = (readl(&dev->cap_regs->hccparams) & HCC_LEN) ? 1 : 0;
3072 dev->dciversion = readw(&dev->cap_regs->dciversion); 3244 dev->dciversion = readw(&dev->cap_regs->dciversion);
3073 dev->devcap = (readl(&dev->cap_regs->dccparams) & DEVCAP) ? 1 : 0; 3245 dev->devcap = (readl(&dev->cap_regs->dccparams) & DEVCAP) ? 1 : 0;
3074 VDBG(dev, "dev->lpm: %d\n", dev->lpm); 3246 dev_vdbg(&dev->pdev->dev, "dev->lpm: %d\n", dev->lpm);
3075 VDBG(dev, "dev->dciversion: 0x%04x\n", dev->dciversion); 3247 dev_vdbg(&dev->pdev->dev, "dev->dciversion: 0x%04x\n",
3076 VDBG(dev, "dccparams: 0x%08x\n", readl(&dev->cap_regs->dccparams)); 3248 dev->dciversion);
3077 VDBG(dev, "dev->devcap: %d\n", dev->devcap); 3249 dev_vdbg(&dev->pdev->dev, "dccparams: 0x%08x\n",
3250 readl(&dev->cap_regs->dccparams));
3251 dev_vdbg(&dev->pdev->dev, "dev->devcap: %d\n", dev->devcap);
3078 if (!dev->devcap) { 3252 if (!dev->devcap) {
3079 ERROR(dev, "can't support device mode\n"); 3253 dev_err(&dev->pdev->dev, "can't support device mode\n");
3080 retval = -ENODEV; 3254 retval = -ENODEV;
3081 goto error; 3255 goto error;
3082 } 3256 }
3083 3257
3084 /* a pair of endpoints (out/in) for each address */ 3258 /* a pair of endpoints (out/in) for each address */
3085 dev->ep_max = DEN(readl(&dev->cap_regs->dccparams)) * 2; 3259 dev->ep_max = DEN(readl(&dev->cap_regs->dccparams)) * 2;
3086 VDBG(dev, "dev->ep_max: %d\n", dev->ep_max); 3260 dev_vdbg(&dev->pdev->dev, "dev->ep_max: %d\n", dev->ep_max);
3087 3261
3088 /* allocate endpoints memory */ 3262 /* allocate endpoints memory */
3089 dev->ep = kzalloc(sizeof(struct langwell_ep) * dev->ep_max, 3263 dev->ep = kzalloc(sizeof(struct langwell_ep) * dev->ep_max,
3090 GFP_KERNEL); 3264 GFP_KERNEL);
3091 if (!dev->ep) { 3265 if (!dev->ep) {
3092 ERROR(dev, "allocate endpoints memory failed\n"); 3266 dev_err(&dev->pdev->dev, "allocate endpoints memory failed\n");
3093 retval = -ENOMEM; 3267 retval = -ENOMEM;
3094 goto error; 3268 goto error;
3095 } 3269 }
3096 3270
3097 /* allocate device dQH memory */ 3271 /* allocate device dQH memory */
3098 size = dev->ep_max * sizeof(struct langwell_dqh); 3272 size = dev->ep_max * sizeof(struct langwell_dqh);
3099 VDBG(dev, "orig size = %d\n", size); 3273 dev_vdbg(&dev->pdev->dev, "orig size = %d\n", size);
3100 if (size < DQH_ALIGNMENT) 3274 if (size < DQH_ALIGNMENT)
3101 size = DQH_ALIGNMENT; 3275 size = DQH_ALIGNMENT;
3102 else if ((size % DQH_ALIGNMENT) != 0) { 3276 else if ((size % DQH_ALIGNMENT) != 0) {
@@ -3106,17 +3280,18 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3106 dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size, 3280 dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
3107 &dev->ep_dqh_dma, GFP_KERNEL); 3281 &dev->ep_dqh_dma, GFP_KERNEL);
3108 if (!dev->ep_dqh) { 3282 if (!dev->ep_dqh) {
3109 ERROR(dev, "allocate dQH memory failed\n"); 3283 dev_err(&dev->pdev->dev, "allocate dQH memory failed\n");
3110 retval = -ENOMEM; 3284 retval = -ENOMEM;
3111 goto error; 3285 goto error;
3112 } 3286 }
3113 dev->ep_dqh_size = size; 3287 dev->ep_dqh_size = size;
3114 VDBG(dev, "ep_dqh_size = %d\n", dev->ep_dqh_size); 3288 dev_vdbg(&dev->pdev->dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);
3115 3289
3116 /* initialize ep0 status request structure */ 3290 /* initialize ep0 status request structure */
3117 dev->status_req = kzalloc(sizeof(struct langwell_request), GFP_KERNEL); 3291 dev->status_req = kzalloc(sizeof(struct langwell_request), GFP_KERNEL);
3118 if (!dev->status_req) { 3292 if (!dev->status_req) {
3119 ERROR(dev, "allocate status_req memory failed\n"); 3293 dev_err(&dev->pdev->dev,
3294 "allocate status_req memory failed\n");
3120 retval = -ENOMEM; 3295 retval = -ENOMEM;
3121 goto error; 3296 goto error;
3122 } 3297 }
@@ -3129,7 +3304,10 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3129 dev->resume_state = USB_STATE_NOTATTACHED; 3304 dev->resume_state = USB_STATE_NOTATTACHED;
3130 dev->usb_state = USB_STATE_POWERED; 3305 dev->usb_state = USB_STATE_POWERED;
3131 dev->ep0_dir = USB_DIR_OUT; 3306 dev->ep0_dir = USB_DIR_OUT;
3132 dev->remote_wakeup = 0; /* default to 0 on reset */ 3307
3308 /* remote wakeup reset to 0 when the device is reset */
3309 dev->remote_wakeup = 0;
3310 dev->dev_status = 1 << USB_DEVICE_SELF_POWERED;
3133 3311
3134#ifndef OTG_TRANSCEIVER 3312#ifndef OTG_TRANSCEIVER
3135 /* reset device controller */ 3313 /* reset device controller */
@@ -3174,18 +3352,20 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3174 } 3352 }
3175 3353
3176 /* done */ 3354 /* done */
3177 INFO(dev, "%s\n", driver_desc); 3355 dev_info(&dev->pdev->dev, "%s\n", driver_desc);
3178 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base); 3356 dev_info(&dev->pdev->dev, "irq %d, pci mem %p\n", pdev->irq, base);
3179 INFO(dev, "Driver version: " DRIVER_VERSION "\n"); 3357 dev_info(&dev->pdev->dev, "Driver version: " DRIVER_VERSION "\n");
3180 INFO(dev, "Support (max) %d endpoints\n", dev->ep_max); 3358 dev_info(&dev->pdev->dev, "Support (max) %d endpoints\n", dev->ep_max);
3181 INFO(dev, "Device interface version: 0x%04x\n", dev->dciversion); 3359 dev_info(&dev->pdev->dev, "Device interface version: 0x%04x\n",
3182 INFO(dev, "Controller mode: %s\n", dev->devcap ? "Device" : "Host"); 3360 dev->dciversion);
3183 INFO(dev, "Support USB LPM: %s\n", dev->lpm ? "Yes" : "No"); 3361 dev_info(&dev->pdev->dev, "Controller mode: %s\n",
3184 3362 dev->devcap ? "Device" : "Host");
3185 VDBG(dev, "After langwell_udc_probe(), print all registers:\n"); 3363 dev_info(&dev->pdev->dev, "Support USB LPM: %s\n",
3186#ifdef VERBOSE 3364 dev->lpm ? "Yes" : "No");
3365
3366 dev_vdbg(&dev->pdev->dev,
3367 "After langwell_udc_probe(), print all registers:\n");
3187 print_all_registers(dev); 3368 print_all_registers(dev);
3188#endif
3189 3369
3190 the_controller = dev; 3370 the_controller = dev;
3191 3371
@@ -3197,12 +3377,18 @@ static int langwell_udc_probe(struct pci_dev *pdev,
3197 if (retval) 3377 if (retval)
3198 goto error; 3378 goto error;
3199 3379
3200 VDBG(dev, "<--- %s()\n", __func__); 3380 retval = device_create_file(&pdev->dev, &dev_attr_remote_wakeup);
3381 if (retval)
3382 goto error_attr1;
3383
3384 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3201 return 0; 3385 return 0;
3202 3386
3387error_attr1:
3388 device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
3203error: 3389error:
3204 if (dev) { 3390 if (dev) {
3205 DBG(dev, "<--- %s()\n", __func__); 3391 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3206 langwell_udc_remove(pdev); 3392 langwell_udc_remove(pdev);
3207 } 3393 }
3208 3394
@@ -3214,32 +3400,45 @@ error:
3214static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state) 3400static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3215{ 3401{
3216 struct langwell_udc *dev = the_controller; 3402 struct langwell_udc *dev = the_controller;
3217 u32 devlc;
3218 3403
3219 DBG(dev, "---> %s()\n", __func__); 3404 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3220 3405
3221 /* disable interrupt and set controller to stop state */ 3406 /* disable interrupt and set controller to stop state */
3222 langwell_udc_stop(dev); 3407 langwell_udc_stop(dev);
3223 3408
3224 /* diable IRQ handler */ 3409 /* disable IRQ handler */
3225 if (dev->got_irq) 3410 if (dev->got_irq)
3226 free_irq(pdev->irq, dev); 3411 free_irq(pdev->irq, dev);
3227 dev->got_irq = 0; 3412 dev->got_irq = 0;
3228 3413
3229
3230 /* save PCI state */ 3414 /* save PCI state */
3231 pci_save_state(pdev); 3415 pci_save_state(pdev);
3232 3416
3417 spin_lock_irq(&dev->lock);
3418 /* stop all usb activities */
3419 stop_activity(dev, dev->driver);
3420 spin_unlock_irq(&dev->lock);
3421
3422 /* free dTD dma_pool and dQH */
3423 if (dev->dtd_pool)
3424 dma_pool_destroy(dev->dtd_pool);
3425
3426 if (dev->ep_dqh)
3427 dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
3428 dev->ep_dqh, dev->ep_dqh_dma);
3429
3430 /* release SRAM caching */
3431 if (dev->has_sram && dev->got_sram)
3432 sram_deinit(dev);
3433
3233 /* set device power state */ 3434 /* set device power state */
3234 pci_set_power_state(pdev, PCI_D3hot); 3435 pci_set_power_state(pdev, PCI_D3hot);
3235 3436
3236 /* enter PHY low power suspend */ 3437 /* enter PHY low power suspend */
3237 devlc = readl(&dev->op_regs->devlc); 3438 if (dev->pdev->device != 0x0829)
3238 VDBG(dev, "devlc = 0x%08x\n", devlc); 3439 langwell_phy_low_power(dev, 1);
3239 devlc |= LPM_PHCD;
3240 writel(devlc, &dev->op_regs->devlc);
3241 3440
3242 DBG(dev, "<--- %s()\n", __func__); 3441 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3243 return 0; 3442 return 0;
3244} 3443}
3245 3444
@@ -3248,27 +3447,58 @@ static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3248static int langwell_udc_resume(struct pci_dev *pdev) 3447static int langwell_udc_resume(struct pci_dev *pdev)
3249{ 3448{
3250 struct langwell_udc *dev = the_controller; 3449 struct langwell_udc *dev = the_controller;
3251 u32 devlc; 3450 size_t size;
3252 3451
3253 DBG(dev, "---> %s()\n", __func__); 3452 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3254 3453
3255 /* exit PHY low power suspend */ 3454 /* exit PHY low power suspend */
3256 devlc = readl(&dev->op_regs->devlc); 3455 if (dev->pdev->device != 0x0829)
3257 VDBG(dev, "devlc = 0x%08x\n", devlc); 3456 langwell_phy_low_power(dev, 0);
3258 devlc &= ~LPM_PHCD;
3259 writel(devlc, &dev->op_regs->devlc);
3260 3457
3261 /* set device D0 power state */ 3458 /* set device D0 power state */
3262 pci_set_power_state(pdev, PCI_D0); 3459 pci_set_power_state(pdev, PCI_D0);
3263 3460
3461 /* enable SRAM caching if detected */
3462 if (dev->has_sram && !dev->got_sram)
3463 sram_init(dev);
3464
3465 /* allocate device dQH memory */
3466 size = dev->ep_max * sizeof(struct langwell_dqh);
3467 dev_vdbg(&dev->pdev->dev, "orig size = %d\n", size);
3468 if (size < DQH_ALIGNMENT)
3469 size = DQH_ALIGNMENT;
3470 else if ((size % DQH_ALIGNMENT) != 0) {
3471 size += DQH_ALIGNMENT + 1;
3472 size &= ~(DQH_ALIGNMENT - 1);
3473 }
3474 dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
3475 &dev->ep_dqh_dma, GFP_KERNEL);
3476 if (!dev->ep_dqh) {
3477 dev_err(&dev->pdev->dev, "allocate dQH memory failed\n");
3478 return -ENOMEM;
3479 }
3480 dev->ep_dqh_size = size;
3481 dev_vdbg(&dev->pdev->dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);
3482
3483 /* create dTD dma_pool resource */
3484 dev->dtd_pool = dma_pool_create("langwell_dtd",
3485 &dev->pdev->dev,
3486 sizeof(struct langwell_dtd),
3487 DTD_ALIGNMENT,
3488 DMA_BOUNDARY);
3489
3490 if (!dev->dtd_pool)
3491 return -ENOMEM;
3492
3264 /* restore PCI state */ 3493 /* restore PCI state */
3265 pci_restore_state(pdev); 3494 pci_restore_state(pdev);
3266 3495
3267 /* enable IRQ handler */ 3496 /* enable IRQ handler */
3268 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED, driver_name, dev) 3497 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
3269 != 0) { 3498 driver_name, dev) != 0) {
3270 ERROR(dev, "request interrupt %d failed\n", pdev->irq); 3499 dev_err(&dev->pdev->dev, "request interrupt %d failed\n",
3271 return -1; 3500 pdev->irq);
3501 return -EBUSY;
3272 } 3502 }
3273 dev->got_irq = 1; 3503 dev->got_irq = 1;
3274 3504
@@ -3290,7 +3520,7 @@ static int langwell_udc_resume(struct pci_dev *pdev)
3290 dev->ep0_state = WAIT_FOR_SETUP; 3520 dev->ep0_state = WAIT_FOR_SETUP;
3291 dev->ep0_dir = USB_DIR_OUT; 3521 dev->ep0_dir = USB_DIR_OUT;
3292 3522
3293 DBG(dev, "<--- %s()\n", __func__); 3523 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3294 return 0; 3524 return 0;
3295} 3525}
3296 3526
@@ -3301,15 +3531,15 @@ static void langwell_udc_shutdown(struct pci_dev *pdev)
3301 struct langwell_udc *dev = the_controller; 3531 struct langwell_udc *dev = the_controller;
3302 u32 usbmode; 3532 u32 usbmode;
3303 3533
3304 DBG(dev, "---> %s()\n", __func__); 3534 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3305 3535
3306 /* reset controller mode to IDLE */ 3536 /* reset controller mode to IDLE */
3307 usbmode = readl(&dev->op_regs->usbmode); 3537 usbmode = readl(&dev->op_regs->usbmode);
3308 DBG(dev, "usbmode = 0x%08x\n", usbmode); 3538 dev_dbg(&dev->pdev->dev, "usbmode = 0x%08x\n", usbmode);
3309 usbmode &= (~3 | MODE_IDLE); 3539 usbmode &= (~3 | MODE_IDLE);
3310 writel(usbmode, &dev->op_regs->usbmode); 3540 writel(usbmode, &dev->op_regs->usbmode);
3311 3541
3312 DBG(dev, "<--- %s()\n", __func__); 3542 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3313} 3543}
3314 3544
3315/*-------------------------------------------------------------------------*/ 3545/*-------------------------------------------------------------------------*/
@@ -3324,7 +3554,6 @@ static const struct pci_device_id pci_ids[] = { {
3324}, { /* end: all zeroes */ } 3554}, { /* end: all zeroes */ }
3325}; 3555};
3326 3556
3327
3328MODULE_DEVICE_TABLE(pci, pci_ids); 3557MODULE_DEVICE_TABLE(pci, pci_ids);
3329 3558
3330 3559
@@ -3343,12 +3572,6 @@ static struct pci_driver langwell_pci_driver = {
3343}; 3572};
3344 3573
3345 3574
3346MODULE_DESCRIPTION(DRIVER_DESC);
3347MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
3348MODULE_VERSION(DRIVER_VERSION);
3349MODULE_LICENSE("GPL");
3350
3351
3352static int __init init(void) 3575static int __init init(void)
3353{ 3576{
3354#ifdef OTG_TRANSCEIVER 3577#ifdef OTG_TRANSCEIVER
@@ -3370,3 +3593,9 @@ static void __exit cleanup(void)
3370} 3593}
3371module_exit(cleanup); 3594module_exit(cleanup);
3372 3595
3596
3597MODULE_DESCRIPTION(DRIVER_DESC);
3598MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
3599MODULE_VERSION(DRIVER_VERSION);
3600MODULE_LICENSE("GPL");
3601
diff --git a/drivers/usb/gadget/langwell_udc.h b/drivers/usb/gadget/langwell_udc.h
index 9719934e1c08..f1d9c1bb04f3 100644
--- a/drivers/usb/gadget/langwell_udc.h
+++ b/drivers/usb/gadget/langwell_udc.h
@@ -18,11 +18,7 @@
18 */ 18 */
19 19
20#include <linux/usb/langwell_udc.h> 20#include <linux/usb/langwell_udc.h>
21
22#if defined(CONFIG_USB_LANGWELL_OTG)
23#include <linux/usb/langwell_otg.h> 21#include <linux/usb/langwell_otg.h>
24#endif
25
26 22
27/*-------------------------------------------------------------------------*/ 23/*-------------------------------------------------------------------------*/
28 24
@@ -199,7 +195,9 @@ struct langwell_udc {
199 vbus_active:1, 195 vbus_active:1,
200 suspended:1, 196 suspended:1,
201 stopped:1, 197 stopped:1,
202 lpm:1; /* LPM capability */ 198 lpm:1, /* LPM capability */
199 has_sram:1, /* SRAM caching */
200 got_sram:1;
203 201
204 /* pci state used to access those endpoints */ 202 /* pci state used to access those endpoints */
205 struct pci_dev *pdev; 203 struct pci_dev *pdev;
@@ -224,5 +222,12 @@ struct langwell_udc {
224 222
225 /* make sure release() is done */ 223 /* make sure release() is done */
226 struct completion *done; 224 struct completion *done;
225
226 /* for private SRAM caching */
227 unsigned int sram_addr;
228 unsigned int sram_size;
229
230 /* device status data for get_status request */
231 u16 dev_status;
227}; 232};
228 233
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c
deleted file mode 100644
index fded3fca793b..000000000000
--- a/drivers/usb/gadget/lh7a40x_udc.c
+++ /dev/null
@@ -1,2152 +0,0 @@
1/*
2 * linux/drivers/usb/gadget/lh7a40x_udc.c
3 * Sharp LH7A40x on-chip full speed USB device controllers
4 *
5 * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6 * Copyright (C) 2004 Bo Henriksen, Nordic ID
7 *
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
10 * the Free Software Foundation; either version 2 of the License, or
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 */
23
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
27#include "lh7a40x_udc.h"
28
29//#define DEBUG printk
30//#define DEBUG_EP0 printk
31//#define DEBUG_SETUP printk
32
33#ifndef DEBUG_EP0
34# define DEBUG_EP0(fmt,args...)
35#endif
36#ifndef DEBUG_SETUP
37# define DEBUG_SETUP(fmt,args...)
38#endif
39#ifndef DEBUG
40# define NO_STATES
41# define DEBUG(fmt,args...)
42#endif
43
44#define DRIVER_DESC "LH7A40x USB Device Controller"
45#define DRIVER_VERSION __DATE__
46
47#ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
48#define _BIT(x) (1<<(x))
49#endif
50
51struct lh7a40x_udc *the_controller;
52
53static const char driver_name[] = "lh7a40x_udc";
54static const char driver_desc[] = DRIVER_DESC;
55static const char ep0name[] = "ep0-control";
56
57/*
58 Local definintions.
59*/
60
61#ifndef NO_STATES
62static char *state_names[] = {
63 "WAIT_FOR_SETUP",
64 "DATA_STATE_XMIT",
65 "DATA_STATE_NEED_ZLP",
66 "WAIT_FOR_OUT_STATUS",
67 "DATA_STATE_RECV"
68};
69#endif
70
71/*
72 Local declarations.
73*/
74static int lh7a40x_ep_enable(struct usb_ep *ep,
75 const struct usb_endpoint_descriptor *);
76static int lh7a40x_ep_disable(struct usb_ep *ep);
77static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, gfp_t);
78static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
79static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, gfp_t);
80static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
81static int lh7a40x_set_halt(struct usb_ep *ep, int);
82static int lh7a40x_fifo_status(struct usb_ep *ep);
83static void lh7a40x_fifo_flush(struct usb_ep *ep);
84static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
85static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
86
87static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
88 int status);
89static void pio_irq_enable(int bEndpointAddress);
90static void pio_irq_disable(int bEndpointAddress);
91static void stop_activity(struct lh7a40x_udc *dev,
92 struct usb_gadget_driver *driver);
93static void flush(struct lh7a40x_ep *ep);
94static void udc_enable(struct lh7a40x_udc *dev);
95static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
96
97static struct usb_ep_ops lh7a40x_ep_ops = {
98 .enable = lh7a40x_ep_enable,
99 .disable = lh7a40x_ep_disable,
100
101 .alloc_request = lh7a40x_alloc_request,
102 .free_request = lh7a40x_free_request,
103
104 .queue = lh7a40x_queue,
105 .dequeue = lh7a40x_dequeue,
106
107 .set_halt = lh7a40x_set_halt,
108 .fifo_status = lh7a40x_fifo_status,
109 .fifo_flush = lh7a40x_fifo_flush,
110};
111
112/* Inline code */
113
114static __inline__ int write_packet(struct lh7a40x_ep *ep,
115 struct lh7a40x_request *req, int max)
116{
117 u8 *buf;
118 int length, count;
119 volatile u32 *fifo = (volatile u32 *)ep->fifo;
120
121 buf = req->req.buf + req->req.actual;
122 prefetch(buf);
123
124 length = req->req.length - req->req.actual;
125 length = min(length, max);
126 req->req.actual += length;
127
128 DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
129
130 count = length;
131 while (count--) {
132 *fifo = *buf++;
133 }
134
135 return length;
136}
137
138static __inline__ void usb_set_index(u32 ep)
139{
140 *(volatile u32 *)io_p2v(USB_INDEX) = ep;
141}
142
143static __inline__ u32 usb_read(u32 port)
144{
145 return *(volatile u32 *)io_p2v(port);
146}
147
148static __inline__ void usb_write(u32 val, u32 port)
149{
150 *(volatile u32 *)io_p2v(port) = val;
151}
152
153static __inline__ void usb_set(u32 val, u32 port)
154{
155 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
156 u32 after = (*ioport) | val;
157 *ioport = after;
158}
159
160static __inline__ void usb_clear(u32 val, u32 port)
161{
162 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
163 u32 after = (*ioport) & ~val;
164 *ioport = after;
165}
166
167/*-------------------------------------------------------------------------*/
168
169#define GPIO_PORTC_DR (0x80000E08)
170#define GPIO_PORTC_DDR (0x80000E18)
171#define GPIO_PORTC_PDR (0x80000E70)
172
173/* get port C pin data register */
174#define get_portc_pdr(bit) ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
175/* get port C data direction register */
176#define get_portc_ddr(bit) ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
177/* set port C data register */
178#define set_portc_dr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
179/* set port C data direction register */
180#define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
181
182/*
183 * LPD7A404 GPIO's:
184 * Port C bit 1 = USB Port 1 Power Enable
185 * Port C bit 2 = USB Port 1 Data Carrier Detect
186 */
187#define is_usb_connected() get_portc_pdr(2)
188
189#ifdef CONFIG_USB_GADGET_DEBUG_FILES
190
191static const char proc_node_name[] = "driver/udc";
192
193static int
194udc_proc_read(char *page, char **start, off_t off, int count,
195 int *eof, void *_dev)
196{
197 char *buf = page;
198 struct lh7a40x_udc *dev = _dev;
199 char *next = buf;
200 unsigned size = count;
201 unsigned long flags;
202 int t;
203
204 if (off != 0)
205 return 0;
206
207 local_irq_save(flags);
208
209 /* basic device status */
210 t = scnprintf(next, size,
211 DRIVER_DESC "\n"
212 "%s version: %s\n"
213 "Gadget driver: %s\n"
214 "Host: %s\n\n",
215 driver_name, DRIVER_VERSION,
216 dev->driver ? dev->driver->driver.name : "(none)",
217 is_usb_connected()? "full speed" : "disconnected");
218 size -= t;
219 next += t;
220
221 t = scnprintf(next, size,
222 "GPIO:\n"
223 " Port C bit 1: %d, dir %d\n"
224 " Port C bit 2: %d, dir %d\n\n",
225 get_portc_pdr(1), get_portc_ddr(1),
226 get_portc_pdr(2), get_portc_ddr(2)
227 );
228 size -= t;
229 next += t;
230
231 t = scnprintf(next, size,
232 "DCP pullup: %d\n\n",
233 (usb_read(USB_PM) & PM_USB_DCP) != 0);
234 size -= t;
235 next += t;
236
237 local_irq_restore(flags);
238 *eof = 1;
239 return count - size;
240}
241
242#define create_proc_files() create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
243#define remove_proc_files() remove_proc_entry(proc_node_name, NULL)
244
245#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
246
247#define create_proc_files() do {} while (0)
248#define remove_proc_files() do {} while (0)
249
250#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
251
252/*
253 * udc_disable - disable USB device controller
254 */
255static void udc_disable(struct lh7a40x_udc *dev)
256{
257 DEBUG("%s, %p\n", __func__, dev);
258
259 udc_set_address(dev, 0);
260
261 /* Disable interrupts */
262 usb_write(0, USB_IN_INT_EN);
263 usb_write(0, USB_OUT_INT_EN);
264 usb_write(0, USB_INT_EN);
265
266 /* Disable the USB */
267 usb_write(0, USB_PM);
268
269#ifdef CONFIG_ARCH_LH7A404
270 /* Disable USB power */
271 set_portc_dr(1, 0);
272#endif
273
274 /* if hardware supports it, disconnect from usb */
275 /* make_usb_disappear(); */
276
277 dev->ep0state = WAIT_FOR_SETUP;
278 dev->gadget.speed = USB_SPEED_UNKNOWN;
279 dev->usb_address = 0;
280}
281
282/*
283 * udc_reinit - initialize software state
284 */
285static void udc_reinit(struct lh7a40x_udc *dev)
286{
287 u32 i;
288
289 DEBUG("%s, %p\n", __func__, dev);
290
291 /* device/ep0 records init */
292 INIT_LIST_HEAD(&dev->gadget.ep_list);
293 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
294 dev->ep0state = WAIT_FOR_SETUP;
295
296 /* basic endpoint records init */
297 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
298 struct lh7a40x_ep *ep = &dev->ep[i];
299
300 if (i != 0)
301 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
302
303 ep->desc = 0;
304 ep->stopped = 0;
305 INIT_LIST_HEAD(&ep->queue);
306 ep->pio_irqs = 0;
307 }
308
309 /* the rest was statically initialized, and is read-only */
310}
311
312#define BYTES2MAXP(x) (x / 8)
313#define MAXP2BYTES(x) (x * 8)
314
315/* until it's enabled, this UDC should be completely invisible
316 * to any USB host.
317 */
318static void udc_enable(struct lh7a40x_udc *dev)
319{
320 int ep;
321
322 DEBUG("%s, %p\n", __func__, dev);
323
324 dev->gadget.speed = USB_SPEED_UNKNOWN;
325
326#ifdef CONFIG_ARCH_LH7A404
327 /* Set Port C bit 1 & 2 as output */
328 set_portc_ddr(1, 1);
329 set_portc_ddr(2, 1);
330
331 /* Enable USB power */
332 set_portc_dr(1, 0);
333#endif
334
335 /*
336 * C.f Chapter 18.1.3.1 Initializing the USB
337 */
338
339 /* Disable the USB */
340 usb_clear(PM_USB_ENABLE, USB_PM);
341
342 /* Reset APB & I/O sides of the USB */
343 usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
344 mdelay(5);
345 usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
346
347 /* Set MAXP values for each */
348 for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
349 struct lh7a40x_ep *ep_reg = &dev->ep[ep];
350 u32 csr;
351
352 usb_set_index(ep);
353
354 switch (ep_reg->ep_type) {
355 case ep_bulk_in:
356 case ep_interrupt:
357 usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
358 ep_reg->csr2);
359 /* Fall through */
360 case ep_control:
361 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
362 USB_IN_MAXP);
363 break;
364 case ep_bulk_out:
365 usb_clear(USB_OUT_CSR2_USB_DMA_EN |
366 USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
367 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
368 USB_OUT_MAXP);
369 break;
370 }
371
372 /* Read & Write CSR1, just in case */
373 csr = usb_read(ep_reg->csr1);
374 usb_write(csr, ep_reg->csr1);
375
376 flush(ep_reg);
377 }
378
379 /* Disable interrupts */
380 usb_write(0, USB_IN_INT_EN);
381 usb_write(0, USB_OUT_INT_EN);
382 usb_write(0, USB_INT_EN);
383
384 /* Enable interrupts */
385 usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
386 usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
387 /* Dont enable rest of the interrupts */
388 /* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
389 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
390
391 /* Enable SUSPEND */
392 usb_set(PM_ENABLE_SUSPEND, USB_PM);
393
394 /* Enable the USB */
395 usb_set(PM_USB_ENABLE, USB_PM);
396
397#ifdef CONFIG_ARCH_LH7A404
398 /* NOTE: DOES NOT WORK! */
399 /* Let host detect UDC:
400 * Software must write a 0 to the PMR:DCP_CTRL bit to turn this
401 * transistor on and pull the USBDP pin HIGH.
402 */
403 /* usb_clear(PM_USB_DCP, USB_PM);
404 usb_set(PM_USB_DCP, USB_PM); */
405#endif
406}
407
408/*
409 Register entry point for the peripheral controller driver.
410*/
411int usb_gadget_register_driver(struct usb_gadget_driver *driver)
412{
413 struct lh7a40x_udc *dev = the_controller;
414 int retval;
415
416 DEBUG("%s: %s\n", __func__, driver->driver.name);
417
418 if (!driver
419 || driver->speed != USB_SPEED_FULL
420 || !driver->bind
421 || !driver->disconnect
422 || !driver->setup)
423 return -EINVAL;
424 if (!dev)
425 return -ENODEV;
426 if (dev->driver)
427 return -EBUSY;
428
429 /* first hook up the driver ... */
430 dev->driver = driver;
431 dev->gadget.dev.driver = &driver->driver;
432
433 device_add(&dev->gadget.dev);
434 retval = driver->bind(&dev->gadget);
435 if (retval) {
436 printk(KERN_WARNING "%s: bind to driver %s --> error %d\n",
437 dev->gadget.name, driver->driver.name, retval);
438 device_del(&dev->gadget.dev);
439
440 dev->driver = 0;
441 dev->gadget.dev.driver = 0;
442 return retval;
443 }
444
445 /* ... then enable host detection and ep0; and we're ready
446 * for set_configuration as well as eventual disconnect.
447 * NOTE: this shouldn't power up until later.
448 */
449 printk(KERN_WARNING "%s: registered gadget driver '%s'\n",
450 dev->gadget.name, driver->driver.name);
451
452 udc_enable(dev);
453
454 return 0;
455}
456
457EXPORT_SYMBOL(usb_gadget_register_driver);
458
459/*
460 Unregister entry point for the peripheral controller driver.
461*/
462int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
463{
464 struct lh7a40x_udc *dev = the_controller;
465 unsigned long flags;
466
467 if (!dev)
468 return -ENODEV;
469 if (!driver || driver != dev->driver || !driver->unbind)
470 return -EINVAL;
471
472 spin_lock_irqsave(&dev->lock, flags);
473 dev->driver = 0;
474 stop_activity(dev, driver);
475 spin_unlock_irqrestore(&dev->lock, flags);
476
477 driver->unbind(&dev->gadget);
478 dev->gadget.dev.driver = NULL;
479 device_del(&dev->gadget.dev);
480
481 udc_disable(dev);
482
483 DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
484 return 0;
485}
486
487EXPORT_SYMBOL(usb_gadget_unregister_driver);
488
489/*-------------------------------------------------------------------------*/
490
491/** Write request to FIFO (max write == maxp size)
492 * Return: 0 = still running, 1 = completed, negative = errno
493 * NOTE: INDEX register must be set for EP
494 */
495static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
496{
497 u32 max;
498 u32 csr;
499
500 max = le16_to_cpu(ep->desc->wMaxPacketSize);
501
502 csr = usb_read(ep->csr1);
503 DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
504
505 if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
506 unsigned count;
507 int is_last, is_short;
508
509 count = write_packet(ep, req, max);
510 usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
511
512 /* last packet is usually short (or a zlp) */
513 if (unlikely(count != max))
514 is_last = is_short = 1;
515 else {
516 if (likely(req->req.length != req->req.actual)
517 || req->req.zero)
518 is_last = 0;
519 else
520 is_last = 1;
521 /* interrupt/iso maxpacket may not fill the fifo */
522 is_short = unlikely(max < ep_maxpacket(ep));
523 }
524
525 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __func__,
526 ep->ep.name, count,
527 is_last ? "/L" : "", is_short ? "/S" : "",
528 req->req.length - req->req.actual, req);
529
530 /* requests complete when all IN data is in the FIFO */
531 if (is_last) {
532 done(ep, req, 0);
533 if (list_empty(&ep->queue)) {
534 pio_irq_disable(ep_index(ep));
535 }
536 return 1;
537 }
538 } else {
539 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
540 }
541
542 return 0;
543}
544
545/** Read to request from FIFO (max read == bytes in fifo)
546 * Return: 0 = still running, 1 = completed, negative = errno
547 * NOTE: INDEX register must be set for EP
548 */
549static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
550{
551 u32 csr;
552 u8 *buf;
553 unsigned bufferspace, count, is_short;
554 volatile u32 *fifo = (volatile u32 *)ep->fifo;
555
556 /* make sure there's a packet in the FIFO. */
557 csr = usb_read(ep->csr1);
558 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
559 DEBUG("%s: Packet NOT ready!\n", __func__);
560 return -EINVAL;
561 }
562
563 buf = req->req.buf + req->req.actual;
564 prefetchw(buf);
565 bufferspace = req->req.length - req->req.actual;
566
567 /* read all bytes from this packet */
568 count = usb_read(USB_OUT_FIFO_WC1);
569 req->req.actual += min(count, bufferspace);
570
571 is_short = (count < ep->ep.maxpacket);
572 DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
573 ep->ep.name, csr, count,
574 is_short ? "/S" : "", req, req->req.actual, req->req.length);
575
576 while (likely(count-- != 0)) {
577 u8 byte = (u8) (*fifo & 0xff);
578
579 if (unlikely(bufferspace == 0)) {
580 /* this happens when the driver's buffer
581 * is smaller than what the host sent.
582 * discard the extra data.
583 */
584 if (req->req.status != -EOVERFLOW)
585 printk(KERN_WARNING "%s overflow %d\n",
586 ep->ep.name, count);
587 req->req.status = -EOVERFLOW;
588 } else {
589 *buf++ = byte;
590 bufferspace--;
591 }
592 }
593
594 usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
595
596 /* completion */
597 if (is_short || req->req.actual == req->req.length) {
598 done(ep, req, 0);
599 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
600
601 if (list_empty(&ep->queue))
602 pio_irq_disable(ep_index(ep));
603 return 1;
604 }
605
606 /* finished that packet. the next one may be waiting... */
607 return 0;
608}
609
610/*
611 * done - retire a request; caller blocked irqs
612 * INDEX register is preserved to keep same
613 */
614static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
615{
616 unsigned int stopped = ep->stopped;
617 u32 index;
618
619 DEBUG("%s, %p\n", __func__, ep);
620 list_del_init(&req->queue);
621
622 if (likely(req->req.status == -EINPROGRESS))
623 req->req.status = status;
624 else
625 status = req->req.status;
626
627 if (status && status != -ESHUTDOWN)
628 DEBUG("complete %s req %p stat %d len %u/%u\n",
629 ep->ep.name, &req->req, status,
630 req->req.actual, req->req.length);
631
632 /* don't modify queue heads during completion callback */
633 ep->stopped = 1;
634 /* Read current index (completion may modify it) */
635 index = usb_read(USB_INDEX);
636
637 spin_unlock(&ep->dev->lock);
638 req->req.complete(&ep->ep, &req->req);
639 spin_lock(&ep->dev->lock);
640
641 /* Restore index */
642 usb_set_index(index);
643 ep->stopped = stopped;
644}
645
646/** Enable EP interrupt */
647static void pio_irq_enable(int ep)
648{
649 DEBUG("%s: %d\n", __func__, ep);
650
651 switch (ep) {
652 case 1:
653 usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
654 break;
655 case 2:
656 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
657 break;
658 case 3:
659 usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
660 break;
661 default:
662 DEBUG("Unknown endpoint: %d\n", ep);
663 break;
664 }
665}
666
667/** Disable EP interrupt */
668static void pio_irq_disable(int ep)
669{
670 DEBUG("%s: %d\n", __func__, ep);
671
672 switch (ep) {
673 case 1:
674 usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
675 break;
676 case 2:
677 usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
678 break;
679 case 3:
680 usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
681 break;
682 default:
683 DEBUG("Unknown endpoint: %d\n", ep);
684 break;
685 }
686}
687
688/*
689 * nuke - dequeue ALL requests
690 */
691void nuke(struct lh7a40x_ep *ep, int status)
692{
693 struct lh7a40x_request *req;
694
695 DEBUG("%s, %p\n", __func__, ep);
696
697 /* Flush FIFO */
698 flush(ep);
699
700 /* called with irqs blocked */
701 while (!list_empty(&ep->queue)) {
702 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
703 done(ep, req, status);
704 }
705
706 /* Disable IRQ if EP is enabled (has descriptor) */
707 if (ep->desc)
708 pio_irq_disable(ep_index(ep));
709}
710
711/*
712void nuke_all(struct lh7a40x_udc *dev)
713{
714 int n;
715 for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
716 struct lh7a40x_ep *ep = &dev->ep[n];
717 usb_set_index(n);
718 nuke(ep, 0);
719 }
720}*/
721
722/*
723static void flush_all(struct lh7a40x_udc *dev)
724{
725 int n;
726 for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
727 {
728 struct lh7a40x_ep *ep = &dev->ep[n];
729 flush(ep);
730 }
731}
732*/
733
734/** Flush EP
735 * NOTE: INDEX register must be set before this call
736 */
737static void flush(struct lh7a40x_ep *ep)
738{
739 DEBUG("%s, %p\n", __func__, ep);
740
741 switch (ep->ep_type) {
742 case ep_control:
743 /* check, by implication c.f. 15.1.2.11 */
744 break;
745
746 case ep_bulk_in:
747 case ep_interrupt:
748 /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
749 usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
750 break;
751
752 case ep_bulk_out:
753 /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
754 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
755 break;
756 }
757}
758
759/**
760 * lh7a40x_in_epn - handle IN interrupt
761 */
762static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
763{
764 u32 csr;
765 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
766 struct lh7a40x_request *req;
767
768 usb_set_index(ep_idx);
769
770 csr = usb_read(ep->csr1);
771 DEBUG("%s: %d, csr %x\n", __func__, ep_idx, csr);
772
773 if (csr & USB_IN_CSR1_SENT_STALL) {
774 DEBUG("USB_IN_CSR1_SENT_STALL\n");
775 usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
776 ep->csr1);
777 return;
778 }
779
780 if (!ep->desc) {
781 DEBUG("%s: NO EP DESC\n", __func__);
782 return;
783 }
784
785 if (list_empty(&ep->queue))
786 req = 0;
787 else
788 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
789
790 DEBUG("req: %p\n", req);
791
792 if (!req)
793 return;
794
795 write_fifo(ep, req);
796}
797
798/* ********************************************************************************************* */
799/* Bulk OUT (recv)
800 */
801
802static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
803{
804 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
805 struct lh7a40x_request *req;
806
807 DEBUG("%s: %d\n", __func__, ep_idx);
808
809 usb_set_index(ep_idx);
810
811 if (ep->desc) {
812 u32 csr;
813 csr = usb_read(ep->csr1);
814
815 while ((csr =
816 usb_read(ep->
817 csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
818 USB_OUT_CSR1_SENT_STALL)) {
819 DEBUG("%s: %x\n", __func__, csr);
820
821 if (csr & USB_OUT_CSR1_SENT_STALL) {
822 DEBUG("%s: stall sent, flush fifo\n",
823 __func__);
824 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
825 flush(ep);
826 } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
827 if (list_empty(&ep->queue))
828 req = 0;
829 else
830 req =
831 list_entry(ep->queue.next,
832 struct lh7a40x_request,
833 queue);
834
835 if (!req) {
836 printk(KERN_WARNING
837 "%s: NULL REQ %d\n",
838 __func__, ep_idx);
839 flush(ep);
840 break;
841 } else {
842 read_fifo(ep, req);
843 }
844 }
845
846 }
847
848 } else {
849 /* Throw packet away.. */
850 printk(KERN_WARNING "%s: No descriptor?!?\n", __func__);
851 flush(ep);
852 }
853}
854
855static void stop_activity(struct lh7a40x_udc *dev,
856 struct usb_gadget_driver *driver)
857{
858 int i;
859
860 /* don't disconnect drivers more than once */
861 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
862 driver = 0;
863 dev->gadget.speed = USB_SPEED_UNKNOWN;
864
865 /* prevent new request submissions, kill any outstanding requests */
866 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
867 struct lh7a40x_ep *ep = &dev->ep[i];
868 ep->stopped = 1;
869
870 usb_set_index(i);
871 nuke(ep, -ESHUTDOWN);
872 }
873
874 /* report disconnect; the driver is already quiesced */
875 if (driver) {
876 spin_unlock(&dev->lock);
877 driver->disconnect(&dev->gadget);
878 spin_lock(&dev->lock);
879 }
880
881 /* re-init driver-visible data structures */
882 udc_reinit(dev);
883}
884
885/** Handle USB RESET interrupt
886 */
887static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
888{
889#if 0 /* def CONFIG_ARCH_LH7A404 */
890 /* Does not work always... */
891
892 DEBUG("%s: %d\n", __func__, dev->usb_address);
893
894 if (!dev->usb_address) {
895 /*usb_set(USB_RESET_IO, USB_RESET);
896 mdelay(5);
897 usb_clear(USB_RESET_IO, USB_RESET); */
898 return;
899 }
900 /* Put the USB controller into reset. */
901 usb_set(USB_RESET_IO, USB_RESET);
902
903 /* Set Device ID to 0 */
904 udc_set_address(dev, 0);
905
906 /* Let PLL2 settle down */
907 mdelay(5);
908
909 /* Release the USB controller from reset */
910 usb_clear(USB_RESET_IO, USB_RESET);
911
912 /* Re-enable UDC */
913 udc_enable(dev);
914
915#endif
916 dev->gadget.speed = USB_SPEED_FULL;
917}
918
919/*
920 * lh7a40x usb client interrupt handler.
921 */
922static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev)
923{
924 struct lh7a40x_udc *dev = _dev;
925
926 DEBUG("\n\n");
927
928 spin_lock(&dev->lock);
929
930 for (;;) {
931 u32 intr_in = usb_read(USB_IN_INT);
932 u32 intr_out = usb_read(USB_OUT_INT);
933 u32 intr_int = usb_read(USB_INT);
934
935 /* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
936 u32 in_en = usb_read(USB_IN_INT_EN);
937 u32 out_en = usb_read(USB_OUT_INT_EN);
938
939 if (!intr_out && !intr_in && !intr_int)
940 break;
941
942 DEBUG("%s (on state %s)\n", __func__,
943 state_names[dev->ep0state]);
944 DEBUG("intr_out = %x\n", intr_out);
945 DEBUG("intr_in = %x\n", intr_in);
946 DEBUG("intr_int = %x\n", intr_int);
947
948 if (intr_in) {
949 usb_write(intr_in, USB_IN_INT);
950
951 if ((intr_in & USB_IN_INT_EP1)
952 && (in_en & USB_IN_INT_EP1)) {
953 DEBUG("USB_IN_INT_EP1\n");
954 lh7a40x_in_epn(dev, 1, intr_in);
955 }
956 if ((intr_in & USB_IN_INT_EP3)
957 && (in_en & USB_IN_INT_EP3)) {
958 DEBUG("USB_IN_INT_EP3\n");
959 lh7a40x_in_epn(dev, 3, intr_in);
960 }
961 if (intr_in & USB_IN_INT_EP0) {
962 DEBUG("USB_IN_INT_EP0 (control)\n");
963 lh7a40x_handle_ep0(dev, intr_in);
964 }
965 }
966
967 if (intr_out) {
968 usb_write(intr_out, USB_OUT_INT);
969
970 if ((intr_out & USB_OUT_INT_EP2)
971 && (out_en & USB_OUT_INT_EP2)) {
972 DEBUG("USB_OUT_INT_EP2\n");
973 lh7a40x_out_epn(dev, 2, intr_out);
974 }
975 }
976
977 if (intr_int) {
978 usb_write(intr_int, USB_INT);
979
980 if (intr_int & USB_INT_RESET_INT) {
981 lh7a40x_reset_intr(dev);
982 }
983
984 if (intr_int & USB_INT_RESUME_INT) {
985 DEBUG("USB resume\n");
986
987 if (dev->gadget.speed != USB_SPEED_UNKNOWN
988 && dev->driver
989 && dev->driver->resume
990 && is_usb_connected()) {
991 dev->driver->resume(&dev->gadget);
992 }
993 }
994
995 if (intr_int & USB_INT_SUSPEND_INT) {
996 DEBUG("USB suspend%s\n",
997 is_usb_connected()? "" : "+disconnect");
998 if (!is_usb_connected()) {
999 stop_activity(dev, dev->driver);
1000 } else if (dev->gadget.speed !=
1001 USB_SPEED_UNKNOWN && dev->driver
1002 && dev->driver->suspend) {
1003 dev->driver->suspend(&dev->gadget);
1004 }
1005 }
1006
1007 }
1008 }
1009
1010 spin_unlock(&dev->lock);
1011
1012 return IRQ_HANDLED;
1013}
1014
1015static int lh7a40x_ep_enable(struct usb_ep *_ep,
1016 const struct usb_endpoint_descriptor *desc)
1017{
1018 struct lh7a40x_ep *ep;
1019 struct lh7a40x_udc *dev;
1020 unsigned long flags;
1021
1022 DEBUG("%s, %p\n", __func__, _ep);
1023
1024 ep = container_of(_ep, struct lh7a40x_ep, ep);
1025 if (!_ep || !desc || ep->desc || _ep->name == ep0name
1026 || desc->bDescriptorType != USB_DT_ENDPOINT
1027 || ep->bEndpointAddress != desc->bEndpointAddress
1028 || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
1029 DEBUG("%s, bad ep or descriptor\n", __func__);
1030 return -EINVAL;
1031 }
1032
1033 /* xfer types must match, except that interrupt ~= bulk */
1034 if (ep->bmAttributes != desc->bmAttributes
1035 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1036 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1037 DEBUG("%s, %s type mismatch\n", __func__, _ep->name);
1038 return -EINVAL;
1039 }
1040
1041 /* hardware _could_ do smaller, but driver doesn't */
1042 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
1043 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
1044 || !desc->wMaxPacketSize) {
1045 DEBUG("%s, bad %s maxpacket\n", __func__, _ep->name);
1046 return -ERANGE;
1047 }
1048
1049 dev = ep->dev;
1050 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1051 DEBUG("%s, bogus device state\n", __func__);
1052 return -ESHUTDOWN;
1053 }
1054
1055 spin_lock_irqsave(&ep->dev->lock, flags);
1056
1057 ep->stopped = 0;
1058 ep->desc = desc;
1059 ep->pio_irqs = 0;
1060 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1061
1062 spin_unlock_irqrestore(&ep->dev->lock, flags);
1063
1064 /* Reset halt state (does flush) */
1065 lh7a40x_set_halt(_ep, 0);
1066
1067 DEBUG("%s: enabled %s\n", __func__, _ep->name);
1068 return 0;
1069}
1070
1071/** Disable EP
1072 * NOTE: Sets INDEX register
1073 */
1074static int lh7a40x_ep_disable(struct usb_ep *_ep)
1075{
1076 struct lh7a40x_ep *ep;
1077 unsigned long flags;
1078
1079 DEBUG("%s, %p\n", __func__, _ep);
1080
1081 ep = container_of(_ep, struct lh7a40x_ep, ep);
1082 if (!_ep || !ep->desc) {
1083 DEBUG("%s, %s not enabled\n", __func__,
1084 _ep ? ep->ep.name : NULL);
1085 return -EINVAL;
1086 }
1087
1088 spin_lock_irqsave(&ep->dev->lock, flags);
1089
1090 usb_set_index(ep_index(ep));
1091
1092 /* Nuke all pending requests (does flush) */
1093 nuke(ep, -ESHUTDOWN);
1094
1095 /* Disable ep IRQ */
1096 pio_irq_disable(ep_index(ep));
1097
1098 ep->desc = 0;
1099 ep->stopped = 1;
1100
1101 spin_unlock_irqrestore(&ep->dev->lock, flags);
1102
1103 DEBUG("%s: disabled %s\n", __func__, _ep->name);
1104 return 0;
1105}
1106
1107static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1108 gfp_t gfp_flags)
1109{
1110 struct lh7a40x_request *req;
1111
1112 DEBUG("%s, %p\n", __func__, ep);
1113
1114 req = kzalloc(sizeof(*req), gfp_flags);
1115 if (!req)
1116 return 0;
1117
1118 INIT_LIST_HEAD(&req->queue);
1119
1120 return &req->req;
1121}
1122
1123static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1124{
1125 struct lh7a40x_request *req;
1126
1127 DEBUG("%s, %p\n", __func__, ep);
1128
1129 req = container_of(_req, struct lh7a40x_request, req);
1130 WARN_ON(!list_empty(&req->queue));
1131 kfree(req);
1132}
1133
1134/** Queue one request
1135 * Kickstart transfer if needed
1136 * NOTE: Sets INDEX register
1137 */
1138static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
1139 gfp_t gfp_flags)
1140{
1141 struct lh7a40x_request *req;
1142 struct lh7a40x_ep *ep;
1143 struct lh7a40x_udc *dev;
1144 unsigned long flags;
1145
1146 DEBUG("\n\n\n%s, %p\n", __func__, _ep);
1147
1148 req = container_of(_req, struct lh7a40x_request, req);
1149 if (unlikely
1150 (!_req || !_req->complete || !_req->buf
1151 || !list_empty(&req->queue))) {
1152 DEBUG("%s, bad params\n", __func__);
1153 return -EINVAL;
1154 }
1155
1156 ep = container_of(_ep, struct lh7a40x_ep, ep);
1157 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1158 DEBUG("%s, bad ep\n", __func__);
1159 return -EINVAL;
1160 }
1161
1162 dev = ep->dev;
1163 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1164 DEBUG("%s, bogus device state %p\n", __func__, dev->driver);
1165 return -ESHUTDOWN;
1166 }
1167
1168 DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1169 _req->buf);
1170
1171 spin_lock_irqsave(&dev->lock, flags);
1172
1173 _req->status = -EINPROGRESS;
1174 _req->actual = 0;
1175
1176 /* kickstart this i/o queue? */
1177 DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1178 ep->stopped);
1179 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1180 u32 csr;
1181
1182 if (unlikely(ep_index(ep) == 0)) {
1183 /* EP0 */
1184 list_add_tail(&req->queue, &ep->queue);
1185 lh7a40x_ep0_kick(dev, ep);
1186 req = 0;
1187 } else if (ep_is_in(ep)) {
1188 /* EP1 & EP3 */
1189 usb_set_index(ep_index(ep));
1190 csr = usb_read(ep->csr1);
1191 pio_irq_enable(ep_index(ep));
1192 if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
1193 if (write_fifo(ep, req) == 1)
1194 req = 0;
1195 }
1196 } else {
1197 /* EP2 */
1198 usb_set_index(ep_index(ep));
1199 csr = usb_read(ep->csr1);
1200 pio_irq_enable(ep_index(ep));
1201 if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
1202 if (read_fifo(ep, req) == 1)
1203 req = 0;
1204 }
1205 }
1206 }
1207
1208 /* pio or dma irq handler advances the queue. */
1209 if (likely(req != 0))
1210 list_add_tail(&req->queue, &ep->queue);
1211
1212 spin_unlock_irqrestore(&dev->lock, flags);
1213
1214 return 0;
1215}
1216
1217/* dequeue JUST ONE request */
1218static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1219{
1220 struct lh7a40x_ep *ep;
1221 struct lh7a40x_request *req;
1222 unsigned long flags;
1223
1224 DEBUG("%s, %p\n", __func__, _ep);
1225
1226 ep = container_of(_ep, struct lh7a40x_ep, ep);
1227 if (!_ep || ep->ep.name == ep0name)
1228 return -EINVAL;
1229
1230 spin_lock_irqsave(&ep->dev->lock, flags);
1231
1232 /* make sure it's actually queued on this endpoint */
1233 list_for_each_entry(req, &ep->queue, queue) {
1234 if (&req->req == _req)
1235 break;
1236 }
1237 if (&req->req != _req) {
1238 spin_unlock_irqrestore(&ep->dev->lock, flags);
1239 return -EINVAL;
1240 }
1241
1242 done(ep, req, -ECONNRESET);
1243
1244 spin_unlock_irqrestore(&ep->dev->lock, flags);
1245 return 0;
1246}
1247
1248/** Halt specific EP
1249 * Return 0 if success
1250 * NOTE: Sets INDEX register to EP !
1251 */
1252static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1253{
1254 struct lh7a40x_ep *ep;
1255 unsigned long flags;
1256
1257 ep = container_of(_ep, struct lh7a40x_ep, ep);
1258 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1259 DEBUG("%s, bad ep\n", __func__);
1260 return -EINVAL;
1261 }
1262
1263 usb_set_index(ep_index(ep));
1264
1265 DEBUG("%s, ep %d, val %d\n", __func__, ep_index(ep), value);
1266
1267 spin_lock_irqsave(&ep->dev->lock, flags);
1268
1269 if (ep_index(ep) == 0) {
1270 /* EP0 */
1271 usb_set(EP0_SEND_STALL, ep->csr1);
1272 } else if (ep_is_in(ep)) {
1273 u32 csr = usb_read(ep->csr1);
1274 if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
1275 || !list_empty(&ep->queue))) {
1276 /*
1277 * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1278 * if any transfer requests are still queued, or if the controller
1279 * FIFO still holds bytes that the host hasn't collected.
1280 */
1281 spin_unlock_irqrestore(&ep->dev->lock, flags);
1282 DEBUG
1283 ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1284 (csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
1285 !list_empty(&ep->queue));
1286 return -EAGAIN;
1287 }
1288 flush(ep);
1289 if (value)
1290 usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1291 else {
1292 usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1293 usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1294 }
1295
1296 } else {
1297
1298 flush(ep);
1299 if (value)
1300 usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1301 else {
1302 usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1303 usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1304 }
1305 }
1306
1307 if (value) {
1308 ep->stopped = 1;
1309 } else {
1310 ep->stopped = 0;
1311 }
1312
1313 spin_unlock_irqrestore(&ep->dev->lock, flags);
1314
1315 DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1316
1317 return 0;
1318}
1319
1320/** Return bytes in EP FIFO
1321 * NOTE: Sets INDEX register to EP
1322 */
1323static int lh7a40x_fifo_status(struct usb_ep *_ep)
1324{
1325 u32 csr;
1326 int count = 0;
1327 struct lh7a40x_ep *ep;
1328
1329 ep = container_of(_ep, struct lh7a40x_ep, ep);
1330 if (!_ep) {
1331 DEBUG("%s, bad ep\n", __func__);
1332 return -ENODEV;
1333 }
1334
1335 DEBUG("%s, %d\n", __func__, ep_index(ep));
1336
1337 /* LPD can't report unclaimed bytes from IN fifos */
1338 if (ep_is_in(ep))
1339 return -EOPNOTSUPP;
1340
1341 usb_set_index(ep_index(ep));
1342
1343 csr = usb_read(ep->csr1);
1344 if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1345 csr & USB_OUT_CSR1_OUT_PKT_RDY) {
1346 count = usb_read(USB_OUT_FIFO_WC1);
1347 }
1348
1349 return count;
1350}
1351
1352/** Flush EP FIFO
1353 * NOTE: Sets INDEX register to EP
1354 */
1355static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1356{
1357 struct lh7a40x_ep *ep;
1358
1359 ep = container_of(_ep, struct lh7a40x_ep, ep);
1360 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1361 DEBUG("%s, bad ep\n", __func__);
1362 return;
1363 }
1364
1365 usb_set_index(ep_index(ep));
1366 flush(ep);
1367}
1368
1369/****************************************************************/
1370/* End Point 0 related functions */
1371/****************************************************************/
1372
1373/* return: 0 = still running, 1 = completed, negative = errno */
1374static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1375{
1376 u32 max;
1377 unsigned count;
1378 int is_last;
1379
1380 max = ep_maxpacket(ep);
1381
1382 DEBUG_EP0("%s\n", __func__);
1383
1384 count = write_packet(ep, req, max);
1385
1386 /* last packet is usually short (or a zlp) */
1387 if (unlikely(count != max))
1388 is_last = 1;
1389 else {
1390 if (likely(req->req.length != req->req.actual) || req->req.zero)
1391 is_last = 0;
1392 else
1393 is_last = 1;
1394 }
1395
1396 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __func__,
1397 ep->ep.name, count,
1398 is_last ? "/L" : "", req->req.length - req->req.actual, req);
1399
1400 /* requests complete when all IN data is in the FIFO */
1401 if (is_last) {
1402 done(ep, req, 0);
1403 return 1;
1404 }
1405
1406 return 0;
1407}
1408
1409static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1410 unsigned char *cp, int max)
1411{
1412 int bytes;
1413 int count = usb_read(USB_OUT_FIFO_WC1);
1414 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1415
1416 if (count > max)
1417 count = max;
1418 bytes = count;
1419 while (count--)
1420 *cp++ = *fifo & 0xFF;
1421 return bytes;
1422}
1423
1424static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1425 unsigned char *cp, int count)
1426{
1427 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1428 DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1429 while (count--)
1430 *fifo = *cp++;
1431}
1432
1433static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1434{
1435 u32 csr;
1436 u8 *buf;
1437 unsigned bufferspace, count, is_short;
1438 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1439
1440 DEBUG_EP0("%s\n", __func__);
1441
1442 csr = usb_read(USB_EP0_CSR);
1443 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1444 return 0;
1445
1446 buf = req->req.buf + req->req.actual;
1447 prefetchw(buf);
1448 bufferspace = req->req.length - req->req.actual;
1449
1450 /* read all bytes from this packet */
1451 if (likely(csr & EP0_OUT_PKT_RDY)) {
1452 count = usb_read(USB_OUT_FIFO_WC1);
1453 req->req.actual += min(count, bufferspace);
1454 } else /* zlp */
1455 count = 0;
1456
1457 is_short = (count < ep->ep.maxpacket);
1458 DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1459 ep->ep.name, csr, count,
1460 is_short ? "/S" : "", req, req->req.actual, req->req.length);
1461
1462 while (likely(count-- != 0)) {
1463 u8 byte = (u8) (*fifo & 0xff);
1464
1465 if (unlikely(bufferspace == 0)) {
1466 /* this happens when the driver's buffer
1467 * is smaller than what the host sent.
1468 * discard the extra data.
1469 */
1470 if (req->req.status != -EOVERFLOW)
1471 DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1472 count);
1473 req->req.status = -EOVERFLOW;
1474 } else {
1475 *buf++ = byte;
1476 bufferspace--;
1477 }
1478 }
1479
1480 /* completion */
1481 if (is_short || req->req.actual == req->req.length) {
1482 done(ep, req, 0);
1483 return 1;
1484 }
1485
1486 /* finished that packet. the next one may be waiting... */
1487 return 0;
1488}
1489
1490/**
1491 * udc_set_address - set the USB address for this device
1492 * @address:
1493 *
1494 * Called from control endpoint function after it decodes a set address setup packet.
1495 */
1496static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
1497{
1498 DEBUG_EP0("%s: %d\n", __func__, address);
1499 /* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
1500 dev->usb_address = address;
1501 usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
1502 usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
1503 /* usb_read(USB_FA); */
1504}
1505
1506/*
1507 * DATA_STATE_RECV (OUT_PKT_RDY)
1508 * - if error
1509 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1510 * - else
1511 * set EP0_CLR_OUT bit
1512 if last set EP0_DATA_END bit
1513 */
1514static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1515{
1516 struct lh7a40x_request *req;
1517 struct lh7a40x_ep *ep = &dev->ep[0];
1518 int ret;
1519
1520 DEBUG_EP0("%s: %x\n", __func__, csr);
1521
1522 if (list_empty(&ep->queue))
1523 req = 0;
1524 else
1525 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1526
1527 if (req) {
1528
1529 if (req->req.length == 0) {
1530 DEBUG_EP0("ZERO LENGTH OUT!\n");
1531 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1532 dev->ep0state = WAIT_FOR_SETUP;
1533 return;
1534 }
1535 ret = read_fifo_ep0(ep, req);
1536 if (ret) {
1537 /* Done! */
1538 DEBUG_EP0("%s: finished, waiting for status\n",
1539 __func__);
1540
1541 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1542 dev->ep0state = WAIT_FOR_SETUP;
1543 } else {
1544 /* Not done yet.. */
1545 DEBUG_EP0("%s: not finished\n", __func__);
1546 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1547 }
1548 } else {
1549 DEBUG_EP0("NO REQ??!\n");
1550 }
1551}
1552
1553/*
1554 * DATA_STATE_XMIT
1555 */
1556static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1557{
1558 struct lh7a40x_request *req;
1559 struct lh7a40x_ep *ep = &dev->ep[0];
1560 int ret, need_zlp = 0;
1561
1562 DEBUG_EP0("%s: %x\n", __func__, csr);
1563
1564 if (list_empty(&ep->queue))
1565 req = 0;
1566 else
1567 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1568
1569 if (!req) {
1570 DEBUG_EP0("%s: NULL REQ\n", __func__);
1571 return 0;
1572 }
1573
1574 if (req->req.length == 0) {
1575
1576 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1577 dev->ep0state = WAIT_FOR_SETUP;
1578 return 1;
1579 }
1580
1581 if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
1582 /* Next write will end with the packet size, */
1583 /* so we need Zero-length-packet */
1584 need_zlp = 1;
1585 }
1586
1587 ret = write_fifo_ep0(ep, req);
1588
1589 if (ret == 1 && !need_zlp) {
1590 /* Last packet */
1591 DEBUG_EP0("%s: finished, waiting for status\n", __func__);
1592
1593 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1594 dev->ep0state = WAIT_FOR_SETUP;
1595 } else {
1596 DEBUG_EP0("%s: not finished\n", __func__);
1597 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1598 }
1599
1600 if (need_zlp) {
1601 DEBUG_EP0("%s: Need ZLP!\n", __func__);
1602 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1603 dev->ep0state = DATA_STATE_NEED_ZLP;
1604 }
1605
1606 return 1;
1607}
1608
1609static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1610 struct usb_ctrlrequest *ctrl)
1611{
1612 struct lh7a40x_ep *ep0 = &dev->ep[0];
1613 struct lh7a40x_ep *qep;
1614 int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1615 u16 val = 0;
1616
1617 if (reqtype == USB_RECIP_INTERFACE) {
1618 /* This is not supported.
1619 * And according to the USB spec, this one does nothing..
1620 * Just return 0
1621 */
1622 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1623 } else if (reqtype == USB_RECIP_DEVICE) {
1624 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1625 val |= (1 << 0); /* Self powered */
1626 /*val |= (1<<1); *//* Remote wakeup */
1627 } else if (reqtype == USB_RECIP_ENDPOINT) {
1628 int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1629
1630 DEBUG_SETUP
1631 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1632 ep_num, ctrl->wLength);
1633
1634 if (ctrl->wLength > 2 || ep_num > 3)
1635 return -EOPNOTSUPP;
1636
1637 qep = &dev->ep[ep_num];
1638 if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1639 && ep_index(qep) != 0) {
1640 return -EOPNOTSUPP;
1641 }
1642
1643 usb_set_index(ep_index(qep));
1644
1645 /* Return status on next IN token */
1646 switch (qep->ep_type) {
1647 case ep_control:
1648 val =
1649 (usb_read(qep->csr1) & EP0_SEND_STALL) ==
1650 EP0_SEND_STALL;
1651 break;
1652 case ep_bulk_in:
1653 case ep_interrupt:
1654 val =
1655 (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1656 USB_IN_CSR1_SEND_STALL;
1657 break;
1658 case ep_bulk_out:
1659 val =
1660 (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1661 USB_OUT_CSR1_SEND_STALL;
1662 break;
1663 }
1664
1665 /* Back to EP0 index */
1666 usb_set_index(0);
1667
1668 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1669 ctrl->wIndex, val);
1670 } else {
1671 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1672 return -EOPNOTSUPP;
1673 }
1674
1675 /* Clear "out packet ready" */
1676 usb_set((EP0_CLR_OUT), USB_EP0_CSR);
1677 /* Put status to FIFO */
1678 lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
1679 /* Issue "In packet ready" */
1680 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1681
1682 return 0;
1683}
1684
1685/*
1686 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1687 * - read data packet from EP0 FIFO
1688 * - decode command
1689 * - if error
1690 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1691 * - else
1692 * set EP0_CLR_OUT | EP0_DATA_END bits
1693 */
1694static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1695{
1696 struct lh7a40x_ep *ep = &dev->ep[0];
1697 struct usb_ctrlrequest ctrl;
1698 int i, bytes, is_in;
1699
1700 DEBUG_SETUP("%s: %x\n", __func__, csr);
1701
1702 /* Nuke all previous transfers */
1703 nuke(ep, -EPROTO);
1704
1705 /* read control req from fifo (8 bytes) */
1706 bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
1707
1708 DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
1709 DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
1710 ctrl.bRequestType == USB_DIR_IN);
1711 DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
1712 DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
1713 DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
1714 DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
1715
1716 /* Set direction of EP0 */
1717 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1718 ep->bEndpointAddress |= USB_DIR_IN;
1719 is_in = 1;
1720 } else {
1721 ep->bEndpointAddress &= ~USB_DIR_IN;
1722 is_in = 0;
1723 }
1724
1725 dev->req_pending = 1;
1726
1727 /* Handle some SETUP packets ourselves */
1728 switch (ctrl.bRequest) {
1729 case USB_REQ_SET_ADDRESS:
1730 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1731 break;
1732
1733 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1734 udc_set_address(dev, ctrl.wValue);
1735 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1736 return;
1737
1738 case USB_REQ_GET_STATUS:{
1739 if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
1740 return;
1741
1742 case USB_REQ_CLEAR_FEATURE:
1743 case USB_REQ_SET_FEATURE:
1744 if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1745 struct lh7a40x_ep *qep;
1746 int ep_num = (ctrl.wIndex & 0x0f);
1747
1748 /* Support only HALT feature */
1749 if (ctrl.wValue != 0 || ctrl.wLength != 0
1750 || ep_num > 3 || ep_num < 1)
1751 break;
1752
1753 qep = &dev->ep[ep_num];
1754 spin_unlock(&dev->lock);
1755 if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1756 DEBUG_SETUP("SET_FEATURE (%d)\n",
1757 ep_num);
1758 lh7a40x_set_halt(&qep->ep, 1);
1759 } else {
1760 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1761 ep_num);
1762 lh7a40x_set_halt(&qep->ep, 0);
1763 }
1764 spin_lock(&dev->lock);
1765 usb_set_index(0);
1766
1767 /* Reply with a ZLP on next IN token */
1768 usb_set((EP0_CLR_OUT | EP0_DATA_END),
1769 USB_EP0_CSR);
1770 return;
1771 }
1772 break;
1773 }
1774
1775 default:
1776 break;
1777 }
1778
1779 if (likely(dev->driver)) {
1780 /* device-2-host (IN) or no data setup command, process immediately */
1781 spin_unlock(&dev->lock);
1782 i = dev->driver->setup(&dev->gadget, &ctrl);
1783 spin_lock(&dev->lock);
1784
1785 if (i < 0) {
1786 /* setup processing failed, force stall */
1787 DEBUG_SETUP
1788 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1789 i);
1790 usb_set_index(0);
1791 usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1792 USB_EP0_CSR);
1793
1794 /* ep->stopped = 1; */
1795 dev->ep0state = WAIT_FOR_SETUP;
1796 }
1797 }
1798}
1799
1800/*
1801 * DATA_STATE_NEED_ZLP
1802 */
1803static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1804{
1805 DEBUG_EP0("%s: %x\n", __func__, csr);
1806
1807 /* c.f. Table 15-14 */
1808 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1809 dev->ep0state = WAIT_FOR_SETUP;
1810}
1811
1812/*
1813 * handle ep0 interrupt
1814 */
1815static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1816{
1817 struct lh7a40x_ep *ep = &dev->ep[0];
1818 u32 csr;
1819
1820 /* Set index 0 */
1821 usb_set_index(0);
1822 csr = usb_read(USB_EP0_CSR);
1823
1824 DEBUG_EP0("%s: csr = %x\n", __func__, csr);
1825
1826 /*
1827 * For overview of what we should be doing see c.f. Chapter 18.1.2.4
1828 * We will follow that outline here modified by our own global state
1829 * indication which provides hints as to what we think should be
1830 * happening..
1831 */
1832
1833 /*
1834 * if SENT_STALL is set
1835 * - clear the SENT_STALL bit
1836 */
1837 if (csr & EP0_SENT_STALL) {
1838 DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __func__, csr);
1839 usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1840 nuke(ep, -ECONNABORTED);
1841 dev->ep0state = WAIT_FOR_SETUP;
1842 return;
1843 }
1844
1845 /*
1846 * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1847 * - fill EP0 FIFO
1848 * - if last packet
1849 * - set IN_PKT_RDY | DATA_END
1850 * - else
1851 * set IN_PKT_RDY
1852 */
1853 if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1854 DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1855 __func__);
1856
1857 switch (dev->ep0state) {
1858 case DATA_STATE_XMIT:
1859 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1860 lh7a40x_ep0_in(dev, csr);
1861 return;
1862 case DATA_STATE_NEED_ZLP:
1863 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1864 lh7a40x_ep0_in_zlp(dev, csr);
1865 return;
1866 default:
1867 /* Stall? */
1868 DEBUG_EP0("Odd state!! state = %s\n",
1869 state_names[dev->ep0state]);
1870 dev->ep0state = WAIT_FOR_SETUP;
1871 /* nuke(ep, 0); */
1872 /* usb_set(EP0_SEND_STALL, ep->csr1); */
1873 break;
1874 }
1875 }
1876
1877 /*
1878 * if SETUP_END is set
1879 * - abort the last transfer
1880 * - set SERVICED_SETUP_END_BIT
1881 */
1882 if (csr & EP0_SETUP_END) {
1883 DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __func__, csr);
1884
1885 usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1886
1887 nuke(ep, 0);
1888 dev->ep0state = WAIT_FOR_SETUP;
1889 }
1890
1891 /*
1892 * if EP0_OUT_PKT_RDY is set
1893 * - read data packet from EP0 FIFO
1894 * - decode command
1895 * - if error
1896 * set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1897 * - else
1898 * set SERVICED_OUT_PKT_RDY | DATA_END bits
1899 */
1900 if (csr & EP0_OUT_PKT_RDY) {
1901
1902 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __func__,
1903 csr);
1904
1905 switch (dev->ep0state) {
1906 case WAIT_FOR_SETUP:
1907 DEBUG_EP0("WAIT_FOR_SETUP\n");
1908 lh7a40x_ep0_setup(dev, csr);
1909 break;
1910
1911 case DATA_STATE_RECV:
1912 DEBUG_EP0("DATA_STATE_RECV\n");
1913 lh7a40x_ep0_out(dev, csr);
1914 break;
1915
1916 default:
1917 /* send stall? */
1918 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1919 dev->ep0state);
1920 break;
1921 }
1922 }
1923}
1924
1925static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1926{
1927 u32 csr;
1928
1929 usb_set_index(0);
1930 csr = usb_read(USB_EP0_CSR);
1931
1932 DEBUG_EP0("%s: %x\n", __func__, csr);
1933
1934 /* Clear "out packet ready" */
1935 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1936
1937 if (ep_is_in(ep)) {
1938 dev->ep0state = DATA_STATE_XMIT;
1939 lh7a40x_ep0_in(dev, csr);
1940 } else {
1941 dev->ep0state = DATA_STATE_RECV;
1942 lh7a40x_ep0_out(dev, csr);
1943 }
1944}
1945
1946/* ---------------------------------------------------------------------------
1947 * device-scoped parts of the api to the usb controller hardware
1948 * ---------------------------------------------------------------------------
1949 */
1950
1951static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1952{
1953 u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
1954 u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
1955 DEBUG("%s, %p\n", __func__, _gadget);
1956 return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1957}
1958
1959static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1960{
1961 /* host may not have enabled remote wakeup */
1962 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1963 return -EHOSTUNREACH;
1964 udc_set_mask_UDCCR(UDCCR_RSM); */
1965 return -ENOTSUPP;
1966}
1967
1968static const struct usb_gadget_ops lh7a40x_udc_ops = {
1969 .get_frame = lh7a40x_udc_get_frame,
1970 .wakeup = lh7a40x_udc_wakeup,
1971 /* current versions must always be self-powered */
1972};
1973
1974static void nop_release(struct device *dev)
1975{
1976 DEBUG("%s %s\n", __func__, dev_name(dev));
1977}
1978
1979static struct lh7a40x_udc memory = {
1980 .usb_address = 0,
1981
1982 .gadget = {
1983 .ops = &lh7a40x_udc_ops,
1984 .ep0 = &memory.ep[0].ep,
1985 .name = driver_name,
1986 .dev = {
1987 .init_name = "gadget",
1988 .release = nop_release,
1989 },
1990 },
1991
1992 /* control endpoint */
1993 .ep[0] = {
1994 .ep = {
1995 .name = ep0name,
1996 .ops = &lh7a40x_ep_ops,
1997 .maxpacket = EP0_PACKETSIZE,
1998 },
1999 .dev = &memory,
2000
2001 .bEndpointAddress = 0,
2002 .bmAttributes = 0,
2003
2004 .ep_type = ep_control,
2005 .fifo = io_p2v(USB_EP0_FIFO),
2006 .csr1 = USB_EP0_CSR,
2007 .csr2 = USB_EP0_CSR,
2008 },
2009
2010 /* first group of endpoints */
2011 .ep[1] = {
2012 .ep = {
2013 .name = "ep1in-bulk",
2014 .ops = &lh7a40x_ep_ops,
2015 .maxpacket = 64,
2016 },
2017 .dev = &memory,
2018
2019 .bEndpointAddress = USB_DIR_IN | 1,
2020 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2021
2022 .ep_type = ep_bulk_in,
2023 .fifo = io_p2v(USB_EP1_FIFO),
2024 .csr1 = USB_IN_CSR1,
2025 .csr2 = USB_IN_CSR2,
2026 },
2027
2028 .ep[2] = {
2029 .ep = {
2030 .name = "ep2out-bulk",
2031 .ops = &lh7a40x_ep_ops,
2032 .maxpacket = 64,
2033 },
2034 .dev = &memory,
2035
2036 .bEndpointAddress = 2,
2037 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2038
2039 .ep_type = ep_bulk_out,
2040 .fifo = io_p2v(USB_EP2_FIFO),
2041 .csr1 = USB_OUT_CSR1,
2042 .csr2 = USB_OUT_CSR2,
2043 },
2044
2045 .ep[3] = {
2046 .ep = {
2047 .name = "ep3in-int",
2048 .ops = &lh7a40x_ep_ops,
2049 .maxpacket = 64,
2050 },
2051 .dev = &memory,
2052
2053 .bEndpointAddress = USB_DIR_IN | 3,
2054 .bmAttributes = USB_ENDPOINT_XFER_INT,
2055
2056 .ep_type = ep_interrupt,
2057 .fifo = io_p2v(USB_EP3_FIFO),
2058 .csr1 = USB_IN_CSR1,
2059 .csr2 = USB_IN_CSR2,
2060 },
2061};
2062
2063/*
2064 * probe - binds to the platform device
2065 */
2066static int lh7a40x_udc_probe(struct platform_device *pdev)
2067{
2068 struct lh7a40x_udc *dev = &memory;
2069 int retval;
2070
2071 DEBUG("%s: %p\n", __func__, pdev);
2072
2073 spin_lock_init(&dev->lock);
2074 dev->dev = &pdev->dev;
2075
2076 device_initialize(&dev->gadget.dev);
2077 dev->gadget.dev.parent = &pdev->dev;
2078
2079 the_controller = dev;
2080 platform_set_drvdata(pdev, dev);
2081
2082 udc_disable(dev);
2083 udc_reinit(dev);
2084
2085 /* irq setup after old hardware state is cleaned up */
2086 retval =
2087 request_irq(IRQ_USBINTR, lh7a40x_udc_irq, IRQF_DISABLED, driver_name,
2088 dev);
2089 if (retval != 0) {
2090 DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2091 IRQ_USBINTR, retval);
2092 return -EBUSY;
2093 }
2094
2095 create_proc_files();
2096
2097 return retval;
2098}
2099
2100static int lh7a40x_udc_remove(struct platform_device *pdev)
2101{
2102 struct lh7a40x_udc *dev = platform_get_drvdata(pdev);
2103
2104 DEBUG("%s: %p\n", __func__, pdev);
2105
2106 if (dev->driver)
2107 return -EBUSY;
2108
2109 udc_disable(dev);
2110 remove_proc_files();
2111
2112 free_irq(IRQ_USBINTR, dev);
2113
2114 platform_set_drvdata(pdev, 0);
2115
2116 the_controller = 0;
2117
2118 return 0;
2119}
2120
2121/*-------------------------------------------------------------------------*/
2122
2123static struct platform_driver udc_driver = {
2124 .probe = lh7a40x_udc_probe,
2125 .remove = lh7a40x_udc_remove,
2126 /* FIXME power management support */
2127 /* .suspend = ... disable UDC */
2128 /* .resume = ... re-enable UDC */
2129 .driver = {
2130 .name = (char *)driver_name,
2131 .owner = THIS_MODULE,
2132 },
2133};
2134
2135static int __init udc_init(void)
2136{
2137 DEBUG("%s: %s version %s\n", __func__, driver_name, DRIVER_VERSION);
2138 return platform_driver_register(&udc_driver);
2139}
2140
2141static void __exit udc_exit(void)
2142{
2143 platform_driver_unregister(&udc_driver);
2144}
2145
2146module_init(udc_init);
2147module_exit(udc_exit);
2148
2149MODULE_DESCRIPTION(DRIVER_DESC);
2150MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2151MODULE_LICENSE("GPL");
2152MODULE_ALIAS("platform:lh7a40x_udc");
diff --git a/drivers/usb/gadget/lh7a40x_udc.h b/drivers/usb/gadget/lh7a40x_udc.h
deleted file mode 100644
index ca861203a301..000000000000
--- a/drivers/usb/gadget/lh7a40x_udc.h
+++ /dev/null
@@ -1,259 +0,0 @@
1/*
2 * linux/drivers/usb/gadget/lh7a40x_udc.h
3 * Sharp LH7A40x on-chip full speed USB device controllers
4 *
5 * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6 * Copyright (C) 2004 Bo Henriksen, Nordic ID
7 *
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
10 * the Free Software Foundation; either version 2 of the License, or
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 */
23
24#ifndef __LH7A40X_H_
25#define __LH7A40X_H_
26
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/ioport.h>
30#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/delay.h>
33#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/timer.h>
37#include <linux/list.h>
38#include <linux/interrupt.h>
39#include <linux/proc_fs.h>
40#include <linux/mm.h>
41#include <linux/device.h>
42#include <linux/dma-mapping.h>
43
44#include <asm/byteorder.h>
45#include <asm/dma.h>
46#include <asm/io.h>
47#include <asm/irq.h>
48#include <asm/system.h>
49#include <asm/unaligned.h>
50#include <mach/hardware.h>
51
52#include <linux/usb/ch9.h>
53#include <linux/usb/gadget.h>
54
55/*
56 * Memory map
57 */
58
59#define USB_FA 0x80000200 // function address register
60#define USB_PM 0x80000204 // power management register
61
62#define USB_IN_INT 0x80000208 // IN interrupt register bank (EP0-EP3)
63#define USB_OUT_INT 0x80000210 // OUT interrupt register bank (EP2)
64#define USB_INT 0x80000218 // interrupt register bank
65
66#define USB_IN_INT_EN 0x8000021C // IN interrupt enable register bank
67#define USB_OUT_INT_EN 0x80000224 // OUT interrupt enable register bank
68#define USB_INT_EN 0x8000022C // USB interrupt enable register bank
69
70#define USB_FRM_NUM1 0x80000230 // Frame number1 register
71#define USB_FRM_NUM2 0x80000234 // Frame number2 register
72#define USB_INDEX 0x80000238 // index register
73
74#define USB_IN_MAXP 0x80000240 // IN MAXP register
75#define USB_IN_CSR1 0x80000244 // IN CSR1 register/EP0 CSR register
76#define USB_EP0_CSR 0x80000244 // IN CSR1 register/EP0 CSR register
77#define USB_IN_CSR2 0x80000248 // IN CSR2 register
78#define USB_OUT_MAXP 0x8000024C // OUT MAXP register
79
80#define USB_OUT_CSR1 0x80000250 // OUT CSR1 register
81#define USB_OUT_CSR2 0x80000254 // OUT CSR2 register
82#define USB_OUT_FIFO_WC1 0x80000258 // OUT FIFO write count1 register
83#define USB_OUT_FIFO_WC2 0x8000025C // OUT FIFO write count2 register
84
85#define USB_RESET 0x8000044C // USB reset register
86
87#define USB_EP0_FIFO 0x80000280
88#define USB_EP1_FIFO 0x80000284
89#define USB_EP2_FIFO 0x80000288
90#define USB_EP3_FIFO 0x8000028c
91
92/*
93 * USB reset register
94 */
95#define USB_RESET_APB (1<<1) //resets USB APB control side WRITE
96#define USB_RESET_IO (1<<0) //resets USB IO side WRITE
97
98/*
99 * USB function address register
100 */
101#define USB_FA_ADDR_UPDATE (1<<7)
102#define USB_FA_FUNCTION_ADDR (0x7F)
103
104/*
105 * Power Management register
106 */
107#define PM_USB_DCP (1<<5)
108#define PM_USB_ENABLE (1<<4)
109#define PM_USB_RESET (1<<3)
110#define PM_UC_RESUME (1<<2)
111#define PM_SUSPEND_MODE (1<<1)
112#define PM_ENABLE_SUSPEND (1<<0)
113
114/*
115 * IN interrupt register
116 */
117#define USB_IN_INT_EP3 (1<<3)
118#define USB_IN_INT_EP1 (1<<1)
119#define USB_IN_INT_EP0 (1<<0)
120
121/*
122 * OUT interrupt register
123 */
124#define USB_OUT_INT_EP2 (1<<2)
125
126/*
127 * USB interrupt register
128 */
129#define USB_INT_RESET_INT (1<<2)
130#define USB_INT_RESUME_INT (1<<1)
131#define USB_INT_SUSPEND_INT (1<<0)
132
133/*
134 * USB interrupt enable register
135 */
136#define USB_INT_EN_USB_RESET_INTER (1<<2)
137#define USB_INT_EN_RESUME_INTER (1<<1)
138#define USB_INT_EN_SUSPEND_INTER (1<<0)
139
140/*
141 * INCSR1 register
142 */
143#define USB_IN_CSR1_CLR_DATA_TOGGLE (1<<6)
144#define USB_IN_CSR1_SENT_STALL (1<<5)
145#define USB_IN_CSR1_SEND_STALL (1<<4)
146#define USB_IN_CSR1_FIFO_FLUSH (1<<3)
147#define USB_IN_CSR1_FIFO_NOT_EMPTY (1<<1)
148#define USB_IN_CSR1_IN_PKT_RDY (1<<0)
149
150/*
151 * INCSR2 register
152 */
153#define USB_IN_CSR2_AUTO_SET (1<<7)
154#define USB_IN_CSR2_USB_DMA_EN (1<<4)
155
156/*
157 * OUT CSR1 register
158 */
159#define USB_OUT_CSR1_CLR_DATA_REG (1<<7)
160#define USB_OUT_CSR1_SENT_STALL (1<<6)
161#define USB_OUT_CSR1_SEND_STALL (1<<5)
162#define USB_OUT_CSR1_FIFO_FLUSH (1<<4)
163#define USB_OUT_CSR1_FIFO_FULL (1<<1)
164#define USB_OUT_CSR1_OUT_PKT_RDY (1<<0)
165
166/*
167 * OUT CSR2 register
168 */
169#define USB_OUT_CSR2_AUTO_CLR (1<<7)
170#define USB_OUT_CSR2_USB_DMA_EN (1<<4)
171
172/*
173 * EP0 CSR
174 */
175#define EP0_CLR_SETUP_END (1<<7) /* Clear "Setup Ends" Bit (w) */
176#define EP0_CLR_OUT (1<<6) /* Clear "Out packet ready" Bit (w) */
177#define EP0_SEND_STALL (1<<5) /* Send STALL Handshake (rw) */
178#define EP0_SETUP_END (1<<4) /* Setup Ends (r) */
179
180#define EP0_DATA_END (1<<3) /* Data end (rw) */
181#define EP0_SENT_STALL (1<<2) /* Sent Stall Handshake (r) */
182#define EP0_IN_PKT_RDY (1<<1) /* In packet ready (rw) */
183#define EP0_OUT_PKT_RDY (1<<0) /* Out packet ready (r) */
184
185/* general CSR */
186#define OUT_PKT_RDY (1<<0)
187#define IN_PKT_RDY (1<<0)
188
189/*
190 * IN/OUT MAXP register
191 */
192#define USB_OUT_MAXP_MAXP (0xF)
193#define USB_IN_MAXP_MAXP (0xF)
194
195// Max packet size
196//#define EP0_PACKETSIZE 0x10
197#define EP0_PACKETSIZE 0x8
198#define EP0_MAXPACKETSIZE 0x10
199
200#define UDC_MAX_ENDPOINTS 4
201
202#define WAIT_FOR_SETUP 0
203#define DATA_STATE_XMIT 1
204#define DATA_STATE_NEED_ZLP 2
205#define WAIT_FOR_OUT_STATUS 3
206#define DATA_STATE_RECV 4
207
208/* ********************************************************************************************* */
209/* IO
210 */
211
212typedef enum ep_type {
213 ep_control, ep_bulk_in, ep_bulk_out, ep_interrupt
214} ep_type_t;
215
216struct lh7a40x_ep {
217 struct usb_ep ep;
218 struct lh7a40x_udc *dev;
219
220 const struct usb_endpoint_descriptor *desc;
221 struct list_head queue;
222 unsigned long pio_irqs;
223
224 u8 stopped;
225 u8 bEndpointAddress;
226 u8 bmAttributes;
227
228 ep_type_t ep_type;
229 u32 fifo;
230 u32 csr1;
231 u32 csr2;
232};
233
234struct lh7a40x_request {
235 struct usb_request req;
236 struct list_head queue;
237};
238
239struct lh7a40x_udc {
240 struct usb_gadget gadget;
241 struct usb_gadget_driver *driver;
242 struct device *dev;
243 spinlock_t lock;
244
245 int ep0state;
246 struct lh7a40x_ep ep[UDC_MAX_ENDPOINTS];
247
248 unsigned char usb_address;
249
250 unsigned req_pending:1, req_std:1, req_config:1;
251};
252
253extern struct lh7a40x_udc *the_controller;
254
255#define ep_is_in(EP) (((EP)->bEndpointAddress&USB_DIR_IN)==USB_DIR_IN)
256#define ep_index(EP) ((EP)->bEndpointAddress&0xF)
257#define ep_maxpacket(EP) ((EP)->ep.maxpacket)
258
259#endif
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c
index e03058fe23cb..084aa080a2d5 100644
--- a/drivers/usb/gadget/m66592-udc.c
+++ b/drivers/usb/gadget/m66592-udc.c
@@ -258,7 +258,7 @@ static int pipe_buffer_setting(struct m66592 *m66592,
258 break; 258 break;
259 case M66592_BULK: 259 case M66592_BULK:
260 /* isochronous pipes may be used as bulk pipes */ 260 /* isochronous pipes may be used as bulk pipes */
261 if (info->pipe > M66592_BASE_PIPENUM_BULK) 261 if (info->pipe >= M66592_BASE_PIPENUM_BULK)
262 bufnum = info->pipe - M66592_BASE_PIPENUM_BULK; 262 bufnum = info->pipe - M66592_BASE_PIPENUM_BULK;
263 else 263 else
264 bufnum = info->pipe - M66592_BASE_PIPENUM_ISOC; 264 bufnum = info->pipe - M66592_BASE_PIPENUM_ISOC;
@@ -1454,14 +1454,15 @@ static struct usb_ep_ops m66592_ep_ops = {
1454/*-------------------------------------------------------------------------*/ 1454/*-------------------------------------------------------------------------*/
1455static struct m66592 *the_controller; 1455static struct m66592 *the_controller;
1456 1456
1457int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1457int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1458 int (*bind)(struct usb_gadget *))
1458{ 1459{
1459 struct m66592 *m66592 = the_controller; 1460 struct m66592 *m66592 = the_controller;
1460 int retval; 1461 int retval;
1461 1462
1462 if (!driver 1463 if (!driver
1463 || driver->speed != USB_SPEED_HIGH 1464 || driver->speed != USB_SPEED_HIGH
1464 || !driver->bind 1465 || !bind
1465 || !driver->setup) 1466 || !driver->setup)
1466 return -EINVAL; 1467 return -EINVAL;
1467 if (!m66592) 1468 if (!m66592)
@@ -1480,7 +1481,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1480 goto error; 1481 goto error;
1481 } 1482 }
1482 1483
1483 retval = driver->bind (&m66592->gadget); 1484 retval = bind(&m66592->gadget);
1484 if (retval) { 1485 if (retval) {
1485 pr_err("bind to driver error (%d)\n", retval); 1486 pr_err("bind to driver error (%d)\n", retval);
1486 device_del(&m66592->gadget.dev); 1487 device_del(&m66592->gadget.dev);
@@ -1505,7 +1506,7 @@ error:
1505 1506
1506 return retval; 1507 return retval;
1507} 1508}
1508EXPORT_SYMBOL(usb_gadget_register_driver); 1509EXPORT_SYMBOL(usb_gadget_probe_driver);
1509 1510
1510int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1511int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1511{ 1512{
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
index 585f2559484d..01822422c3e8 100644
--- a/drivers/usb/gadget/mass_storage.c
+++ b/drivers/usb/gadget/mass_storage.c
@@ -75,10 +75,6 @@ static struct usb_device_descriptor msg_device_desc = {
75 /* Vendor and product id can be overridden by module parameters. */ 75 /* Vendor and product id can be overridden by module parameters. */
76 .idVendor = cpu_to_le16(FSG_VENDOR_ID), 76 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
77 .idProduct = cpu_to_le16(FSG_PRODUCT_ID), 77 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
78 /* .bcdDevice = f(hardware) */
79 /* .iManufacturer = DYNAMIC */
80 /* .iProduct = DYNAMIC */
81 /* NO SERIAL NUMBER */
82 .bNumConfigurations = 1, 78 .bNumConfigurations = 1,
83}; 79};
84 80
@@ -86,7 +82,8 @@ static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor, 82 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG, 83 .bDescriptorType = USB_DT_OTG,
88 84
89 /* REVISIT SRP-only hardware is possible, although 85 /*
86 * REVISIT SRP-only hardware is possible, although
90 * it would not be called "OTG" ... 87 * it would not be called "OTG" ...
91 */ 88 */
92 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, 89 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
@@ -98,33 +95,6 @@ static const struct usb_descriptor_header *otg_desc[] = {
98}; 95};
99 96
100 97
101/* string IDs are assigned dynamically */
102
103#define STRING_MANUFACTURER_IDX 0
104#define STRING_PRODUCT_IDX 1
105#define STRING_CONFIGURATION_IDX 2
106
107static char manufacturer[50];
108
109static struct usb_string strings_dev[] = {
110 [STRING_MANUFACTURER_IDX].s = manufacturer,
111 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
112 [STRING_CONFIGURATION_IDX].s = "Self Powered",
113 { } /* end of list */
114};
115
116static struct usb_gadget_strings stringtab_dev = {
117 .language = 0x0409, /* en-us */
118 .strings = strings_dev,
119};
120
121static struct usb_gadget_strings *dev_strings[] = {
122 &stringtab_dev,
123 NULL,
124};
125
126
127
128/****************************** Configurations ******************************/ 98/****************************** Configurations ******************************/
129 99
130static struct fsg_module_parameters mod_data = { 100static struct fsg_module_parameters mod_data = {
@@ -132,7 +102,7 @@ static struct fsg_module_parameters mod_data = {
132}; 102};
133FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 103FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
134 104
135static unsigned long msg_registered = 0; 105static unsigned long msg_registered;
136static void msg_cleanup(void); 106static void msg_cleanup(void);
137 107
138static int msg_thread_exits(struct fsg_common *common) 108static int msg_thread_exits(struct fsg_common *common)
@@ -141,7 +111,7 @@ static int msg_thread_exits(struct fsg_common *common)
141 return 0; 111 return 0;
142} 112}
143 113
144static int __ref msg_do_config(struct usb_configuration *c) 114static int __init msg_do_config(struct usb_configuration *c)
145{ 115{
146 static const struct fsg_operations ops = { 116 static const struct fsg_operations ops = {
147 .thread_exits = msg_thread_exits, 117 .thread_exits = msg_thread_exits,
@@ -171,54 +141,23 @@ static int __ref msg_do_config(struct usb_configuration *c)
171 141
172static struct usb_configuration msg_config_driver = { 142static struct usb_configuration msg_config_driver = {
173 .label = "Linux File-Backed Storage", 143 .label = "Linux File-Backed Storage",
174 .bind = msg_do_config,
175 .bConfigurationValue = 1, 144 .bConfigurationValue = 1,
176 /* .iConfiguration = DYNAMIC */
177 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
178}; 146};
179 147
180 148
181
182/****************************** Gadget Bind ******************************/ 149/****************************** Gadget Bind ******************************/
183 150
184 151static int __init msg_bind(struct usb_composite_dev *cdev)
185static int __ref msg_bind(struct usb_composite_dev *cdev)
186{ 152{
187 struct usb_gadget *gadget = cdev->gadget;
188 int status; 153 int status;
189 154
190 /* Allocate string descriptor numbers ... note that string 155 status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
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) 156 if (status < 0)
200 return status; 157 return status;
201 strings_dev[STRING_MANUFACTURER_IDX].id = status;
202 msg_device_desc.iManufacturer = status;
203 158
204 status = usb_string_id(cdev); 159 dev_info(&cdev->gadget->dev,
205 if (status < 0) 160 DRIVER_DESC ", version: " DRIVER_VERSION "\n");
206 return status;
207 strings_dev[STRING_PRODUCT_IDX].id = status;
208 msg_device_desc.iProduct = status;
209
210 status = usb_string_id(cdev);
211 if (status < 0)
212 return status;
213 strings_dev[STRING_CONFIGURATION_IDX].id = status;
214 msg_config_driver.iConfiguration = status;
215
216 /* register our second configuration */
217 status = usb_add_config(cdev, &msg_config_driver);
218 if (status < 0)
219 return status;
220
221 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
222 set_bit(0, &msg_registered); 161 set_bit(0, &msg_registered);
223 return 0; 162 return 0;
224} 163}
@@ -226,12 +165,11 @@ static int __ref msg_bind(struct usb_composite_dev *cdev)
226 165
227/****************************** Some noise ******************************/ 166/****************************** Some noise ******************************/
228 167
229
230static struct usb_composite_driver msg_driver = { 168static struct usb_composite_driver msg_driver = {
231 .name = "g_mass_storage", 169 .name = "g_mass_storage",
232 .dev = &msg_device_desc, 170 .dev = &msg_device_desc,
233 .strings = dev_strings, 171 .iProduct = DRIVER_DESC,
234 .bind = msg_bind, 172 .needs_serial = 1,
235}; 173};
236 174
237MODULE_DESCRIPTION(DRIVER_DESC); 175MODULE_DESCRIPTION(DRIVER_DESC);
@@ -240,7 +178,7 @@ MODULE_LICENSE("GPL");
240 178
241static int __init msg_init(void) 179static int __init msg_init(void)
242{ 180{
243 return usb_composite_register(&msg_driver); 181 return usb_composite_probe(&msg_driver, msg_bind);
244} 182}
245module_init(msg_init); 183module_init(msg_init);
246 184
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
index 795d76232167..d9feced348e3 100644
--- a/drivers/usb/gadget/multi.c
+++ b/drivers/usb/gadget/multi.c
@@ -74,8 +74,8 @@ MODULE_LICENSE("GPL");
74 74
75/***************************** Device Descriptor ****************************/ 75/***************************** Device Descriptor ****************************/
76 76
77#define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */ 77#define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
78#define MULTI_PRODUCT_NUM 0xa4ab /* XXX */ 78#define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
79 79
80 80
81enum { 81enum {
@@ -121,8 +121,6 @@ static const struct usb_descriptor_header *otg_desc[] = {
121 121
122 122
123enum { 123enum {
124 MULTI_STRING_MANUFACTURER_IDX,
125 MULTI_STRING_PRODUCT_IDX,
126#ifdef CONFIG_USB_G_MULTI_RNDIS 124#ifdef CONFIG_USB_G_MULTI_RNDIS
127 MULTI_STRING_RNDIS_CONFIG_IDX, 125 MULTI_STRING_RNDIS_CONFIG_IDX,
128#endif 126#endif
@@ -131,11 +129,7 @@ enum {
131#endif 129#endif
132}; 130};
133 131
134static char manufacturer[50];
135
136static struct usb_string strings_dev[] = { 132static struct usb_string strings_dev[] = {
137 [MULTI_STRING_MANUFACTURER_IDX].s = manufacturer,
138 [MULTI_STRING_PRODUCT_IDX].s = DRIVER_DESC,
139#ifdef CONFIG_USB_G_MULTI_RNDIS 133#ifdef CONFIG_USB_G_MULTI_RNDIS
140 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS", 134 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
141#endif 135#endif
@@ -170,7 +164,7 @@ static u8 hostaddr[ETH_ALEN];
170 164
171#ifdef USB_ETH_RNDIS 165#ifdef USB_ETH_RNDIS
172 166
173static __ref int rndis_do_config(struct usb_configuration *c) 167static __init int rndis_do_config(struct usb_configuration *c)
174{ 168{
175 int ret; 169 int ret;
176 170
@@ -197,7 +191,6 @@ static __ref int rndis_do_config(struct usb_configuration *c)
197static int rndis_config_register(struct usb_composite_dev *cdev) 191static int rndis_config_register(struct usb_composite_dev *cdev)
198{ 192{
199 static struct usb_configuration config = { 193 static struct usb_configuration config = {
200 .bind = rndis_do_config,
201 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM, 194 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
202 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 195 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
203 }; 196 };
@@ -205,7 +198,7 @@ static int rndis_config_register(struct usb_composite_dev *cdev)
205 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s; 198 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
206 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id; 199 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
207 200
208 return usb_add_config(cdev, &config); 201 return usb_add_config(cdev, &config, rndis_do_config);
209} 202}
210 203
211#else 204#else
@@ -222,7 +215,7 @@ static int rndis_config_register(struct usb_composite_dev *cdev)
222 215
223#ifdef CONFIG_USB_G_MULTI_CDC 216#ifdef CONFIG_USB_G_MULTI_CDC
224 217
225static __ref int cdc_do_config(struct usb_configuration *c) 218static __init int cdc_do_config(struct usb_configuration *c)
226{ 219{
227 int ret; 220 int ret;
228 221
@@ -249,7 +242,6 @@ static __ref int cdc_do_config(struct usb_configuration *c)
249static int cdc_config_register(struct usb_composite_dev *cdev) 242static int cdc_config_register(struct usb_composite_dev *cdev)
250{ 243{
251 static struct usb_configuration config = { 244 static struct usb_configuration config = {
252 .bind = cdc_do_config,
253 .bConfigurationValue = MULTI_CDC_CONFIG_NUM, 245 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
254 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 246 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
255 }; 247 };
@@ -257,7 +249,7 @@ static int cdc_config_register(struct usb_composite_dev *cdev)
257 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s; 249 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
258 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id; 250 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
259 251
260 return usb_add_config(cdev, &config); 252 return usb_add_config(cdev, &config, cdc_do_config);
261} 253}
262 254
263#else 255#else
@@ -314,20 +306,11 @@ static int __ref multi_bind(struct usb_composite_dev *cdev)
314 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099); 306 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
315 } 307 }
316 308
317 /* allocate string descriptor numbers */ 309 /* allocate string IDs */
318 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
319 init_utsname()->sysname, init_utsname()->release,
320 gadget->name);
321
322 status = usb_string_ids_tab(cdev, strings_dev); 310 status = usb_string_ids_tab(cdev, strings_dev);
323 if (unlikely(status < 0)) 311 if (unlikely(status < 0))
324 goto fail2; 312 goto fail2;
325 313
326 device_desc.iManufacturer =
327 strings_dev[MULTI_STRING_MANUFACTURER_IDX].id;
328 device_desc.iProduct =
329 strings_dev[MULTI_STRING_PRODUCT_IDX].id;
330
331 /* register configurations */ 314 /* register configurations */
332 status = rndis_config_register(cdev); 315 status = rndis_config_register(cdev);
333 if (unlikely(status < 0)) 316 if (unlikely(status < 0))
@@ -368,14 +351,15 @@ static struct usb_composite_driver multi_driver = {
368 .name = "g_multi", 351 .name = "g_multi",
369 .dev = &device_desc, 352 .dev = &device_desc,
370 .strings = dev_strings, 353 .strings = dev_strings,
371 .bind = multi_bind,
372 .unbind = __exit_p(multi_unbind), 354 .unbind = __exit_p(multi_unbind),
355 .iProduct = DRIVER_DESC,
356 .needs_serial = 1,
373}; 357};
374 358
375 359
376static int __init multi_init(void) 360static int __init multi_init(void)
377{ 361{
378 return usb_composite_register(&multi_driver); 362 return usb_composite_probe(&multi_driver, multi_bind);
379} 363}
380module_init(multi_init); 364module_init(multi_init);
381 365
diff --git a/drivers/usb/gadget/mv_udc.h b/drivers/usb/gadget/mv_udc.h
new file mode 100644
index 000000000000..65f1f7c3bd4e
--- /dev/null
+++ b/drivers/usb/gadget/mv_udc.h
@@ -0,0 +1,294 @@
1
2#ifndef __MV_UDC_H
3#define __MV_UDC_H
4
5#define VUSBHS_MAX_PORTS 8
6
7#define DQH_ALIGNMENT 2048
8#define DTD_ALIGNMENT 64
9#define DMA_BOUNDARY 4096
10
11#define EP_DIR_IN 1
12#define EP_DIR_OUT 0
13
14#define DMA_ADDR_INVALID (~(dma_addr_t)0)
15
16#define EP0_MAX_PKT_SIZE 64
17/* ep0 transfer state */
18#define WAIT_FOR_SETUP 0
19#define DATA_STATE_XMIT 1
20#define DATA_STATE_NEED_ZLP 2
21#define WAIT_FOR_OUT_STATUS 3
22#define DATA_STATE_RECV 4
23
24#define CAPLENGTH_MASK (0xff)
25#define DCCPARAMS_DEN_MASK (0x1f)
26
27#define HCSPARAMS_PPC (0x10)
28
29/* Frame Index Register Bit Masks */
30#define USB_FRINDEX_MASKS 0x3fff
31
32/* Command Register Bit Masks */
33#define USBCMD_RUN_STOP (0x00000001)
34#define USBCMD_CTRL_RESET (0x00000002)
35#define USBCMD_SETUP_TRIPWIRE_SET (0x00002000)
36#define USBCMD_SETUP_TRIPWIRE_CLEAR (~USBCMD_SETUP_TRIPWIRE_SET)
37
38#define USBCMD_ATDTW_TRIPWIRE_SET (0x00004000)
39#define USBCMD_ATDTW_TRIPWIRE_CLEAR (~USBCMD_ATDTW_TRIPWIRE_SET)
40
41/* bit 15,3,2 are for frame list size */
42#define USBCMD_FRAME_SIZE_1024 (0x00000000) /* 000 */
43#define USBCMD_FRAME_SIZE_512 (0x00000004) /* 001 */
44#define USBCMD_FRAME_SIZE_256 (0x00000008) /* 010 */
45#define USBCMD_FRAME_SIZE_128 (0x0000000C) /* 011 */
46#define USBCMD_FRAME_SIZE_64 (0x00008000) /* 100 */
47#define USBCMD_FRAME_SIZE_32 (0x00008004) /* 101 */
48#define USBCMD_FRAME_SIZE_16 (0x00008008) /* 110 */
49#define USBCMD_FRAME_SIZE_8 (0x0000800C) /* 111 */
50
51#define EPCTRL_TX_ALL_MASK (0xFFFF0000)
52#define EPCTRL_RX_ALL_MASK (0x0000FFFF)
53
54#define EPCTRL_TX_DATA_TOGGLE_RST (0x00400000)
55#define EPCTRL_TX_EP_STALL (0x00010000)
56#define EPCTRL_RX_EP_STALL (0x00000001)
57#define EPCTRL_RX_DATA_TOGGLE_RST (0x00000040)
58#define EPCTRL_RX_ENABLE (0x00000080)
59#define EPCTRL_TX_ENABLE (0x00800000)
60#define EPCTRL_CONTROL (0x00000000)
61#define EPCTRL_ISOCHRONOUS (0x00040000)
62#define EPCTRL_BULK (0x00080000)
63#define EPCTRL_INT (0x000C0000)
64#define EPCTRL_TX_TYPE (0x000C0000)
65#define EPCTRL_RX_TYPE (0x0000000C)
66#define EPCTRL_DATA_TOGGLE_INHIBIT (0x00000020)
67#define EPCTRL_TX_EP_TYPE_SHIFT (18)
68#define EPCTRL_RX_EP_TYPE_SHIFT (2)
69
70#define EPCOMPLETE_MAX_ENDPOINTS (16)
71
72/* endpoint list address bit masks */
73#define USB_EP_LIST_ADDRESS_MASK 0xfffff800
74
75#define PORTSCX_W1C_BITS 0x2a
76#define PORTSCX_PORT_RESET 0x00000100
77#define PORTSCX_PORT_POWER 0x00001000
78#define PORTSCX_FORCE_FULL_SPEED_CONNECT 0x01000000
79#define PORTSCX_PAR_XCVR_SELECT 0xC0000000
80#define PORTSCX_PORT_FORCE_RESUME 0x00000040
81#define PORTSCX_PORT_SUSPEND 0x00000080
82#define PORTSCX_PORT_SPEED_FULL 0x00000000
83#define PORTSCX_PORT_SPEED_LOW 0x04000000
84#define PORTSCX_PORT_SPEED_HIGH 0x08000000
85#define PORTSCX_PORT_SPEED_MASK 0x0C000000
86
87/* USB MODE Register Bit Masks */
88#define USBMODE_CTRL_MODE_IDLE 0x00000000
89#define USBMODE_CTRL_MODE_DEVICE 0x00000002
90#define USBMODE_CTRL_MODE_HOST 0x00000003
91#define USBMODE_CTRL_MODE_RSV 0x00000001
92#define USBMODE_SETUP_LOCK_OFF 0x00000008
93#define USBMODE_STREAM_DISABLE 0x00000010
94
95/* USB STS Register Bit Masks */
96#define USBSTS_INT 0x00000001
97#define USBSTS_ERR 0x00000002
98#define USBSTS_PORT_CHANGE 0x00000004
99#define USBSTS_FRM_LST_ROLL 0x00000008
100#define USBSTS_SYS_ERR 0x00000010
101#define USBSTS_IAA 0x00000020
102#define USBSTS_RESET 0x00000040
103#define USBSTS_SOF 0x00000080
104#define USBSTS_SUSPEND 0x00000100
105#define USBSTS_HC_HALTED 0x00001000
106#define USBSTS_RCL 0x00002000
107#define USBSTS_PERIODIC_SCHEDULE 0x00004000
108#define USBSTS_ASYNC_SCHEDULE 0x00008000
109
110
111/* Interrupt Enable Register Bit Masks */
112#define USBINTR_INT_EN (0x00000001)
113#define USBINTR_ERR_INT_EN (0x00000002)
114#define USBINTR_PORT_CHANGE_DETECT_EN (0x00000004)
115
116#define USBINTR_ASYNC_ADV_AAE (0x00000020)
117#define USBINTR_ASYNC_ADV_AAE_ENABLE (0x00000020)
118#define USBINTR_ASYNC_ADV_AAE_DISABLE (0xFFFFFFDF)
119
120#define USBINTR_RESET_EN (0x00000040)
121#define USBINTR_SOF_UFRAME_EN (0x00000080)
122#define USBINTR_DEVICE_SUSPEND (0x00000100)
123
124#define USB_DEVICE_ADDRESS_MASK (0xfe000000)
125#define USB_DEVICE_ADDRESS_BIT_SHIFT (25)
126
127struct mv_cap_regs {
128 u32 caplength_hciversion;
129 u32 hcsparams; /* HC structural parameters */
130 u32 hccparams; /* HC Capability Parameters*/
131 u32 reserved[5];
132 u32 dciversion; /* DC version number and reserved 16 bits */
133 u32 dccparams; /* DC Capability Parameters */
134};
135
136struct mv_op_regs {
137 u32 usbcmd; /* Command register */
138 u32 usbsts; /* Status register */
139 u32 usbintr; /* Interrupt enable */
140 u32 frindex; /* Frame index */
141 u32 reserved1[1];
142 u32 deviceaddr; /* Device Address */
143 u32 eplistaddr; /* Endpoint List Address */
144 u32 ttctrl; /* HOST TT status and control */
145 u32 burstsize; /* Programmable Burst Size */
146 u32 txfilltuning; /* Host Transmit Pre-Buffer Packet Tuning */
147 u32 reserved[4];
148 u32 epnak; /* Endpoint NAK */
149 u32 epnaken; /* Endpoint NAK Enable */
150 u32 configflag; /* Configured Flag register */
151 u32 portsc[VUSBHS_MAX_PORTS]; /* Port Status/Control x, x = 1..8 */
152 u32 otgsc;
153 u32 usbmode; /* USB Host/Device mode */
154 u32 epsetupstat; /* Endpoint Setup Status */
155 u32 epprime; /* Endpoint Initialize */
156 u32 epflush; /* Endpoint De-initialize */
157 u32 epstatus; /* Endpoint Status */
158 u32 epcomplete; /* Endpoint Interrupt On Complete */
159 u32 epctrlx[16]; /* Endpoint Control, where x = 0.. 15 */
160 u32 mcr; /* Mux Control */
161 u32 isr; /* Interrupt Status */
162 u32 ier; /* Interrupt Enable */
163};
164
165struct mv_udc {
166 struct usb_gadget gadget;
167 struct usb_gadget_driver *driver;
168 spinlock_t lock;
169 struct completion *done;
170 struct platform_device *dev;
171 int irq;
172
173 struct mv_cap_regs __iomem *cap_regs;
174 struct mv_op_regs __iomem *op_regs;
175 unsigned int phy_regs;
176 unsigned int max_eps;
177 struct mv_dqh *ep_dqh;
178 size_t ep_dqh_size;
179 dma_addr_t ep_dqh_dma;
180
181 struct dma_pool *dtd_pool;
182 struct mv_ep *eps;
183
184 struct mv_dtd *dtd_head;
185 struct mv_dtd *dtd_tail;
186 unsigned int dtd_entries;
187
188 struct mv_req *status_req;
189 struct usb_ctrlrequest local_setup_buff;
190
191 unsigned int resume_state; /* USB state to resume */
192 unsigned int usb_state; /* USB current state */
193 unsigned int ep0_state; /* Endpoint zero state */
194 unsigned int ep0_dir;
195
196 unsigned int dev_addr;
197
198 int errors;
199 unsigned softconnect:1,
200 vbus_active:1,
201 remote_wakeup:1,
202 softconnected:1,
203 force_fs:1;
204 struct clk *clk;
205};
206
207/* endpoint data structure */
208struct mv_ep {
209 struct usb_ep ep;
210 struct mv_udc *udc;
211 struct list_head queue;
212 struct mv_dqh *dqh;
213 const struct usb_endpoint_descriptor *desc;
214 u32 direction;
215 char name[14];
216 unsigned stopped:1,
217 wedge:1,
218 ep_type:2,
219 ep_num:8;
220};
221
222/* request data structure */
223struct mv_req {
224 struct usb_request req;
225 struct mv_dtd *dtd, *head, *tail;
226 struct mv_ep *ep;
227 struct list_head queue;
228 unsigned dtd_count;
229 unsigned mapped:1;
230};
231
232#define EP_QUEUE_HEAD_MULT_POS 30
233#define EP_QUEUE_HEAD_ZLT_SEL 0x20000000
234#define EP_QUEUE_HEAD_MAX_PKT_LEN_POS 16
235#define EP_QUEUE_HEAD_MAX_PKT_LEN(ep_info) (((ep_info)>>16)&0x07ff)
236#define EP_QUEUE_HEAD_IOS 0x00008000
237#define EP_QUEUE_HEAD_NEXT_TERMINATE 0x00000001
238#define EP_QUEUE_HEAD_IOC 0x00008000
239#define EP_QUEUE_HEAD_MULTO 0x00000C00
240#define EP_QUEUE_HEAD_STATUS_HALT 0x00000040
241#define EP_QUEUE_HEAD_STATUS_ACTIVE 0x00000080
242#define EP_QUEUE_CURRENT_OFFSET_MASK 0x00000FFF
243#define EP_QUEUE_HEAD_NEXT_POINTER_MASK 0xFFFFFFE0
244#define EP_QUEUE_FRINDEX_MASK 0x000007FF
245#define EP_MAX_LENGTH_TRANSFER 0x4000
246
247struct mv_dqh {
248 /* Bits 16..26 Bit 15 is Interrupt On Setup */
249 u32 max_packet_length;
250 u32 curr_dtd_ptr; /* Current dTD Pointer */
251 u32 next_dtd_ptr; /* Next dTD Pointer */
252 /* Total bytes (16..30), IOC (15), INT (8), STS (0-7) */
253 u32 size_ioc_int_sts;
254 u32 buff_ptr0; /* Buffer pointer Page 0 (12-31) */
255 u32 buff_ptr1; /* Buffer pointer Page 1 (12-31) */
256 u32 buff_ptr2; /* Buffer pointer Page 2 (12-31) */
257 u32 buff_ptr3; /* Buffer pointer Page 3 (12-31) */
258 u32 buff_ptr4; /* Buffer pointer Page 4 (12-31) */
259 u32 reserved1;
260 /* 8 bytes of setup data that follows the Setup PID */
261 u8 setup_buffer[8];
262 u32 reserved2[4];
263};
264
265
266#define DTD_NEXT_TERMINATE (0x00000001)
267#define DTD_IOC (0x00008000)
268#define DTD_STATUS_ACTIVE (0x00000080)
269#define DTD_STATUS_HALTED (0x00000040)
270#define DTD_STATUS_DATA_BUFF_ERR (0x00000020)
271#define DTD_STATUS_TRANSACTION_ERR (0x00000008)
272#define DTD_RESERVED_FIELDS (0x00007F00)
273#define DTD_ERROR_MASK (0x68)
274#define DTD_ADDR_MASK (0xFFFFFFE0)
275#define DTD_PACKET_SIZE 0x7FFF0000
276#define DTD_LENGTH_BIT_POS (16)
277
278struct mv_dtd {
279 u32 dtd_next;
280 u32 size_ioc_sts;
281 u32 buff_ptr0; /* Buffer pointer Page 0 */
282 u32 buff_ptr1; /* Buffer pointer Page 1 */
283 u32 buff_ptr2; /* Buffer pointer Page 2 */
284 u32 buff_ptr3; /* Buffer pointer Page 3 */
285 u32 buff_ptr4; /* Buffer pointer Page 4 */
286 u32 scratch_ptr;
287 /* 32 bytes */
288 dma_addr_t td_dma; /* dma address for this td */
289 struct mv_dtd *next_dtd_virt;
290};
291
292extern int mv_udc_phy_init(unsigned int base);
293
294#endif
diff --git a/drivers/usb/gadget/mv_udc_core.c b/drivers/usb/gadget/mv_udc_core.c
new file mode 100644
index 000000000000..b1a8146b9d50
--- /dev/null
+++ b/drivers/usb/gadget/mv_udc_core.c
@@ -0,0 +1,2149 @@
1#include <linux/module.h>
2#include <linux/pci.h>
3#include <linux/dma-mapping.h>
4#include <linux/dmapool.h>
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/ioport.h>
8#include <linux/sched.h>
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include <linux/init.h>
12#include <linux/timer.h>
13#include <linux/list.h>
14#include <linux/interrupt.h>
15#include <linux/moduleparam.h>
16#include <linux/device.h>
17#include <linux/usb/ch9.h>
18#include <linux/usb/gadget.h>
19#include <linux/usb/otg.h>
20#include <linux/pm.h>
21#include <linux/io.h>
22#include <linux/irq.h>
23#include <linux/platform_device.h>
24#include <linux/clk.h>
25#include <asm/system.h>
26#include <asm/unaligned.h>
27
28#include "mv_udc.h"
29
30#define DRIVER_DESC "Marvell PXA USB Device Controller driver"
31#define DRIVER_VERSION "8 Nov 2010"
32
33#define ep_dir(ep) (((ep)->ep_num == 0) ? \
34 ((ep)->udc->ep0_dir) : ((ep)->direction))
35
36/* timeout value -- usec */
37#define RESET_TIMEOUT 10000
38#define FLUSH_TIMEOUT 10000
39#define EPSTATUS_TIMEOUT 10000
40#define PRIME_TIMEOUT 10000
41#define READSAFE_TIMEOUT 1000
42#define DTD_TIMEOUT 1000
43
44#define LOOPS_USEC_SHIFT 4
45#define LOOPS_USEC (1 << LOOPS_USEC_SHIFT)
46#define LOOPS(timeout) ((timeout) >> LOOPS_USEC_SHIFT)
47
48static const char driver_name[] = "mv_udc";
49static const char driver_desc[] = DRIVER_DESC;
50
51/* controller device global variable */
52static struct mv_udc *the_controller;
53int mv_usb_otgsc;
54
55static void nuke(struct mv_ep *ep, int status);
56
57/* for endpoint 0 operations */
58static const struct usb_endpoint_descriptor mv_ep0_desc = {
59 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = 0,
62 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
63 .wMaxPacketSize = EP0_MAX_PKT_SIZE,
64};
65
66static void ep0_reset(struct mv_udc *udc)
67{
68 struct mv_ep *ep;
69 u32 epctrlx;
70 int i = 0;
71
72 /* ep0 in and out */
73 for (i = 0; i < 2; i++) {
74 ep = &udc->eps[i];
75 ep->udc = udc;
76
77 /* ep0 dQH */
78 ep->dqh = &udc->ep_dqh[i];
79
80 /* configure ep0 endpoint capabilities in dQH */
81 ep->dqh->max_packet_length =
82 (EP0_MAX_PKT_SIZE << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
83 | EP_QUEUE_HEAD_IOS;
84
85 epctrlx = readl(&udc->op_regs->epctrlx[0]);
86 if (i) { /* TX */
87 epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
88 | (USB_ENDPOINT_XFER_CONTROL
89 << EPCTRL_TX_EP_TYPE_SHIFT);
90
91 } else { /* RX */
92 epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
93 | (USB_ENDPOINT_XFER_CONTROL
94 << EPCTRL_RX_EP_TYPE_SHIFT);
95 }
96
97 writel(epctrlx, &udc->op_regs->epctrlx[0]);
98 }
99}
100
101/* protocol ep0 stall, will automatically be cleared on new transaction */
102static void ep0_stall(struct mv_udc *udc)
103{
104 u32 epctrlx;
105
106 /* set TX and RX to stall */
107 epctrlx = readl(&udc->op_regs->epctrlx[0]);
108 epctrlx |= EPCTRL_RX_EP_STALL | EPCTRL_TX_EP_STALL;
109 writel(epctrlx, &udc->op_regs->epctrlx[0]);
110
111 /* update ep0 state */
112 udc->ep0_state = WAIT_FOR_SETUP;
113 udc->ep0_dir = EP_DIR_OUT;
114}
115
116static int process_ep_req(struct mv_udc *udc, int index,
117 struct mv_req *curr_req)
118{
119 struct mv_dtd *curr_dtd;
120 struct mv_dqh *curr_dqh;
121 int td_complete, actual, remaining_length;
122 int i, direction;
123 int retval = 0;
124 u32 errors;
125
126 curr_dqh = &udc->ep_dqh[index];
127 direction = index % 2;
128
129 curr_dtd = curr_req->head;
130 td_complete = 0;
131 actual = curr_req->req.length;
132
133 for (i = 0; i < curr_req->dtd_count; i++) {
134 if (curr_dtd->size_ioc_sts & DTD_STATUS_ACTIVE) {
135 dev_dbg(&udc->dev->dev, "%s, dTD not completed\n",
136 udc->eps[index].name);
137 return 1;
138 }
139
140 errors = curr_dtd->size_ioc_sts & DTD_ERROR_MASK;
141 if (!errors) {
142 remaining_length +=
143 (curr_dtd->size_ioc_sts & DTD_PACKET_SIZE)
144 >> DTD_LENGTH_BIT_POS;
145 actual -= remaining_length;
146 } else {
147 dev_info(&udc->dev->dev,
148 "complete_tr error: ep=%d %s: error = 0x%x\n",
149 index >> 1, direction ? "SEND" : "RECV",
150 errors);
151 if (errors & DTD_STATUS_HALTED) {
152 /* Clear the errors and Halt condition */
153 curr_dqh->size_ioc_int_sts &= ~errors;
154 retval = -EPIPE;
155 } else if (errors & DTD_STATUS_DATA_BUFF_ERR) {
156 retval = -EPROTO;
157 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
158 retval = -EILSEQ;
159 }
160 }
161 if (i != curr_req->dtd_count - 1)
162 curr_dtd = (struct mv_dtd *)curr_dtd->next_dtd_virt;
163 }
164 if (retval)
165 return retval;
166
167 curr_req->req.actual = actual;
168
169 return 0;
170}
171
172/*
173 * done() - retire a request; caller blocked irqs
174 * @status : request status to be set, only works when
175 * request is still in progress.
176 */
177static void done(struct mv_ep *ep, struct mv_req *req, int status)
178{
179 struct mv_udc *udc = NULL;
180 unsigned char stopped = ep->stopped;
181 struct mv_dtd *curr_td, *next_td;
182 int j;
183
184 udc = (struct mv_udc *)ep->udc;
185 /* Removed the req from fsl_ep->queue */
186 list_del_init(&req->queue);
187
188 /* req.status should be set as -EINPROGRESS in ep_queue() */
189 if (req->req.status == -EINPROGRESS)
190 req->req.status = status;
191 else
192 status = req->req.status;
193
194 /* Free dtd for the request */
195 next_td = req->head;
196 for (j = 0; j < req->dtd_count; j++) {
197 curr_td = next_td;
198 if (j != req->dtd_count - 1)
199 next_td = curr_td->next_dtd_virt;
200 dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma);
201 }
202
203 if (req->mapped) {
204 dma_unmap_single(ep->udc->gadget.dev.parent,
205 req->req.dma, req->req.length,
206 ((ep_dir(ep) == EP_DIR_IN) ?
207 DMA_TO_DEVICE : DMA_FROM_DEVICE));
208 req->req.dma = DMA_ADDR_INVALID;
209 req->mapped = 0;
210 } else
211 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
212 req->req.dma, req->req.length,
213 ((ep_dir(ep) == EP_DIR_IN) ?
214 DMA_TO_DEVICE : DMA_FROM_DEVICE));
215
216 if (status && (status != -ESHUTDOWN))
217 dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u",
218 ep->ep.name, &req->req, status,
219 req->req.actual, req->req.length);
220
221 ep->stopped = 1;
222
223 spin_unlock(&ep->udc->lock);
224 /*
225 * complete() is from gadget layer,
226 * eg fsg->bulk_in_complete()
227 */
228 if (req->req.complete)
229 req->req.complete(&ep->ep, &req->req);
230
231 spin_lock(&ep->udc->lock);
232 ep->stopped = stopped;
233}
234
235static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
236{
237 u32 tmp, epstatus, bit_pos, direction;
238 struct mv_udc *udc;
239 struct mv_dqh *dqh;
240 unsigned int loops;
241 int readsafe, retval = 0;
242
243 udc = ep->udc;
244 direction = ep_dir(ep);
245 dqh = &(udc->ep_dqh[ep->ep_num * 2 + direction]);
246 bit_pos = 1 << (((direction == EP_DIR_OUT) ? 0 : 16) + ep->ep_num);
247
248 /* check if the pipe is empty */
249 if (!(list_empty(&ep->queue))) {
250 struct mv_req *lastreq;
251 lastreq = list_entry(ep->queue.prev, struct mv_req, queue);
252 lastreq->tail->dtd_next =
253 req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
254 if (readl(&udc->op_regs->epprime) & bit_pos) {
255 loops = LOOPS(PRIME_TIMEOUT);
256 while (readl(&udc->op_regs->epprime) & bit_pos) {
257 if (loops == 0) {
258 retval = -ETIME;
259 goto done;
260 }
261 udelay(LOOPS_USEC);
262 loops--;
263 }
264 if (readl(&udc->op_regs->epstatus) & bit_pos)
265 goto done;
266 }
267 readsafe = 0;
268 loops = LOOPS(READSAFE_TIMEOUT);
269 while (readsafe == 0) {
270 if (loops == 0) {
271 retval = -ETIME;
272 goto done;
273 }
274 /* start with setting the semaphores */
275 tmp = readl(&udc->op_regs->usbcmd);
276 tmp |= USBCMD_ATDTW_TRIPWIRE_SET;
277 writel(tmp, &udc->op_regs->usbcmd);
278
279 /* read the endpoint status */
280 epstatus = readl(&udc->op_regs->epstatus) & bit_pos;
281
282 /*
283 * Reread the ATDTW semaphore bit to check if it is
284 * cleared. When hardware see a hazard, it will clear
285 * the bit or else we remain set to 1 and we can
286 * proceed with priming of endpoint if not already
287 * primed.
288 */
289 if (readl(&udc->op_regs->usbcmd)
290 & USBCMD_ATDTW_TRIPWIRE_SET) {
291 readsafe = 1;
292 }
293 loops--;
294 udelay(LOOPS_USEC);
295 }
296
297 /* Clear the semaphore */
298 tmp = readl(&udc->op_regs->usbcmd);
299 tmp &= USBCMD_ATDTW_TRIPWIRE_CLEAR;
300 writel(tmp, &udc->op_regs->usbcmd);
301
302 /* If endpoint is not active, we activate it now. */
303 if (!epstatus) {
304 if (direction == EP_DIR_IN) {
305 struct mv_dtd *curr_dtd = dma_to_virt(
306 &udc->dev->dev, dqh->curr_dtd_ptr);
307
308 loops = LOOPS(DTD_TIMEOUT);
309 while (curr_dtd->size_ioc_sts
310 & DTD_STATUS_ACTIVE) {
311 if (loops == 0) {
312 retval = -ETIME;
313 goto done;
314 }
315 loops--;
316 udelay(LOOPS_USEC);
317 }
318 }
319 /* No other transfers on the queue */
320
321 /* Write dQH next pointer and terminate bit to 0 */
322 dqh->next_dtd_ptr = req->head->td_dma
323 & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
324 dqh->size_ioc_int_sts = 0;
325
326 /*
327 * Ensure that updates to the QH will
328 * occur before priming.
329 */
330 wmb();
331
332 /* Prime the Endpoint */
333 writel(bit_pos, &udc->op_regs->epprime);
334 }
335 } else {
336 /* Write dQH next pointer and terminate bit to 0 */
337 dqh->next_dtd_ptr = req->head->td_dma
338 & EP_QUEUE_HEAD_NEXT_POINTER_MASK;;
339 dqh->size_ioc_int_sts = 0;
340
341 /* Ensure that updates to the QH will occur before priming. */
342 wmb();
343
344 /* Prime the Endpoint */
345 writel(bit_pos, &udc->op_regs->epprime);
346
347 if (direction == EP_DIR_IN) {
348 /* FIXME add status check after prime the IN ep */
349 int prime_again;
350 u32 curr_dtd_ptr = dqh->curr_dtd_ptr;
351
352 loops = LOOPS(DTD_TIMEOUT);
353 prime_again = 0;
354 while ((curr_dtd_ptr != req->head->td_dma)) {
355 curr_dtd_ptr = dqh->curr_dtd_ptr;
356 if (loops == 0) {
357 dev_err(&udc->dev->dev,
358 "failed to prime %s\n",
359 ep->name);
360 retval = -ETIME;
361 goto done;
362 }
363 loops--;
364 udelay(LOOPS_USEC);
365
366 if (loops == (LOOPS(DTD_TIMEOUT) >> 2)) {
367 if (prime_again)
368 goto done;
369 dev_info(&udc->dev->dev,
370 "prime again\n");
371 writel(bit_pos,
372 &udc->op_regs->epprime);
373 prime_again = 1;
374 }
375 }
376 }
377 }
378done:
379 return retval;;
380}
381
382static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length,
383 dma_addr_t *dma, int *is_last)
384{
385 u32 temp;
386 struct mv_dtd *dtd;
387 struct mv_udc *udc;
388
389 /* how big will this transfer be? */
390 *length = min(req->req.length - req->req.actual,
391 (unsigned)EP_MAX_LENGTH_TRANSFER);
392
393 udc = req->ep->udc;
394
395 /*
396 * Be careful that no _GFP_HIGHMEM is set,
397 * or we can not use dma_to_virt
398 */
399 dtd = dma_pool_alloc(udc->dtd_pool, GFP_KERNEL, dma);
400 if (dtd == NULL)
401 return dtd;
402
403 dtd->td_dma = *dma;
404 /* initialize buffer page pointers */
405 temp = (u32)(req->req.dma + req->req.actual);
406 dtd->buff_ptr0 = cpu_to_le32(temp);
407 temp &= ~0xFFF;
408 dtd->buff_ptr1 = cpu_to_le32(temp + 0x1000);
409 dtd->buff_ptr2 = cpu_to_le32(temp + 0x2000);
410 dtd->buff_ptr3 = cpu_to_le32(temp + 0x3000);
411 dtd->buff_ptr4 = cpu_to_le32(temp + 0x4000);
412
413 req->req.actual += *length;
414
415 /* zlp is needed if req->req.zero is set */
416 if (req->req.zero) {
417 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
418 *is_last = 1;
419 else
420 *is_last = 0;
421 } else if (req->req.length == req->req.actual)
422 *is_last = 1;
423 else
424 *is_last = 0;
425
426 /* Fill in the transfer size; set active bit */
427 temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
428
429 /* Enable interrupt for the last dtd of a request */
430 if (*is_last && !req->req.no_interrupt)
431 temp |= DTD_IOC;
432
433 dtd->size_ioc_sts = temp;
434
435 mb();
436
437 return dtd;
438}
439
440/* generate dTD linked list for a request */
441static int req_to_dtd(struct mv_req *req)
442{
443 unsigned count;
444 int is_last, is_first = 1;
445 struct mv_dtd *dtd, *last_dtd = NULL;
446 struct mv_udc *udc;
447 dma_addr_t dma;
448
449 udc = req->ep->udc;
450
451 do {
452 dtd = build_dtd(req, &count, &dma, &is_last);
453 if (dtd == NULL)
454 return -ENOMEM;
455
456 if (is_first) {
457 is_first = 0;
458 req->head = dtd;
459 } else {
460 last_dtd->dtd_next = dma;
461 last_dtd->next_dtd_virt = dtd;
462 }
463 last_dtd = dtd;
464 req->dtd_count++;
465 } while (!is_last);
466
467 /* set terminate bit to 1 for the last dTD */
468 dtd->dtd_next = DTD_NEXT_TERMINATE;
469
470 req->tail = dtd;
471
472 return 0;
473}
474
475static int mv_ep_enable(struct usb_ep *_ep,
476 const struct usb_endpoint_descriptor *desc)
477{
478 struct mv_udc *udc;
479 struct mv_ep *ep;
480 struct mv_dqh *dqh;
481 u16 max = 0;
482 u32 bit_pos, epctrlx, direction;
483 unsigned char zlt = 0, ios = 0, mult = 0;
484
485 ep = container_of(_ep, struct mv_ep, ep);
486 udc = ep->udc;
487
488 if (!_ep || !desc || ep->desc
489 || desc->bDescriptorType != USB_DT_ENDPOINT)
490 return -EINVAL;
491
492 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
493 return -ESHUTDOWN;
494
495 direction = ep_dir(ep);
496 max = le16_to_cpu(desc->wMaxPacketSize);
497
498 /*
499 * disable HW zero length termination select
500 * driver handles zero length packet through req->req.zero
501 */
502 zlt = 1;
503
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);
508
509 /* Check if the Endpoint is Primed */
510 if ((readl(&udc->op_regs->epprime) & bit_pos)
511 || (readl(&udc->op_regs->epstatus) & bit_pos)) {
512 dev_info(&udc->dev->dev,
513 "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
514 " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
515 (unsigned)ep->ep_num, direction ? "SEND" : "RECV",
516 (unsigned)readl(&udc->op_regs->epprime),
517 (unsigned)readl(&udc->op_regs->epstatus),
518 (unsigned)bit_pos);
519 goto en_done;
520 }
521 /* Set the max packet length, interrupt on Setup and Mult fields */
522 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
523 case USB_ENDPOINT_XFER_BULK:
524 zlt = 1;
525 mult = 0;
526 break;
527 case USB_ENDPOINT_XFER_CONTROL:
528 ios = 1;
529 case USB_ENDPOINT_XFER_INT:
530 mult = 0;
531 break;
532 case USB_ENDPOINT_XFER_ISOC:
533 /* Calculate transactions needed for high bandwidth iso */
534 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
535 max = max & 0x8ff; /* bit 0~10 */
536 /* 3 transactions at most */
537 if (mult > 3)
538 goto en_done;
539 break;
540 default:
541 goto en_done;
542 }
543 dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
544 | (mult << EP_QUEUE_HEAD_MULT_POS)
545 | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0)
546 | (ios ? EP_QUEUE_HEAD_IOS : 0);
547 dqh->next_dtd_ptr = 1;
548 dqh->size_ioc_int_sts = 0;
549
550 ep->ep.maxpacket = max;
551 ep->desc = desc;
552 ep->stopped = 0;
553
554 /* Enable the endpoint for Rx or Tx and set the endpoint type */
555 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
556 if (direction == EP_DIR_IN) {
557 epctrlx &= ~EPCTRL_TX_ALL_MASK;
558 epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
559 | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
560 << EPCTRL_TX_EP_TYPE_SHIFT);
561 } else {
562 epctrlx &= ~EPCTRL_RX_ALL_MASK;
563 epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
564 | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
565 << EPCTRL_RX_EP_TYPE_SHIFT);
566 }
567 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
568
569 /*
570 * Implement Guideline (GL# USB-7) The unused endpoint type must
571 * be programmed to bulk.
572 */
573 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
574 if ((epctrlx & EPCTRL_RX_ENABLE) == 0) {
575 epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
576 << EPCTRL_RX_EP_TYPE_SHIFT);
577 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
578 }
579
580 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
581 if ((epctrlx & EPCTRL_TX_ENABLE) == 0) {
582 epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
583 << EPCTRL_TX_EP_TYPE_SHIFT);
584 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
585 }
586
587 return 0;
588en_done:
589 return -EINVAL;
590}
591
592static int mv_ep_disable(struct usb_ep *_ep)
593{
594 struct mv_udc *udc;
595 struct mv_ep *ep;
596 struct mv_dqh *dqh;
597 u32 bit_pos, epctrlx, direction;
598
599 ep = container_of(_ep, struct mv_ep, ep);
600 if ((_ep == NULL) || !ep->desc)
601 return -EINVAL;
602
603 udc = ep->udc;
604
605 /* Get the endpoint queue head address */
606 dqh = ep->dqh;
607
608 direction = ep_dir(ep);
609 bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
610
611 /* Reset the max packet length and the interrupt on Setup */
612 dqh->max_packet_length = 0;
613
614 /* Disable the endpoint for Rx or Tx and reset the endpoint type */
615 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
616 epctrlx &= ~((direction == EP_DIR_IN)
617 ? (EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE)
618 : (EPCTRL_RX_ENABLE | EPCTRL_RX_TYPE));
619 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
620
621 /* nuke all pending requests (does flush) */
622 nuke(ep, -ESHUTDOWN);
623
624 ep->desc = NULL;
625 ep->stopped = 1;
626 return 0;
627}
628
629static struct usb_request *
630mv_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
631{
632 struct mv_req *req = NULL;
633
634 req = kzalloc(sizeof *req, gfp_flags);
635 if (!req)
636 return NULL;
637
638 req->req.dma = DMA_ADDR_INVALID;
639 INIT_LIST_HEAD(&req->queue);
640
641 return &req->req;
642}
643
644static void mv_free_request(struct usb_ep *_ep, struct usb_request *_req)
645{
646 struct mv_req *req = NULL;
647
648 req = container_of(_req, struct mv_req, req);
649
650 if (_req)
651 kfree(req);
652}
653
654static void mv_ep_fifo_flush(struct usb_ep *_ep)
655{
656 struct mv_udc *udc;
657 u32 bit_pos, direction;
658 struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
659 unsigned int loops;
660
661 udc = ep->udc;
662 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
670 /* Wait until flushing completed */
671 loops = LOOPS(FLUSH_TIMEOUT);
672 while (readl(&udc->op_regs->epflush) & bit_pos) {
673 /*
674 * ENDPTFLUSH bit should be cleared to indicate this
675 * operation is complete
676 */
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);
688 while (readl(&udc->op_regs->epstatus) & bit_pos) {
689 unsigned int inter_loops;
690
691 if (loops == 0) {
692 dev_err(&udc->dev->dev,
693 "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
694 (unsigned)readl(&udc->op_regs->epstatus),
695 (unsigned)bit_pos);
696 return;
697 }
698 /* Write 1 to the Flush register */
699 writel(bit_pos, &udc->op_regs->epflush);
700
701 /* Wait until flushing completed */
702 inter_loops = LOOPS(FLUSH_TIMEOUT);
703 while (readl(&udc->op_regs->epflush) & bit_pos) {
704 /*
705 * ENDPTFLUSH bit should be cleared to indicate this
706 * operation is complete
707 */
708 if (inter_loops == 0) {
709 dev_err(&udc->dev->dev,
710 "TIMEOUT for ENDPTFLUSH=0x%x,"
711 "bit_pos=0x%x\n",
712 (unsigned)readl(&udc->op_regs->epflush),
713 (unsigned)bit_pos);
714 return;
715 }
716 inter_loops--;
717 udelay(LOOPS_USEC);
718 }
719 loops--;
720 }
721}
722
723/* queues (submits) an I/O request to an endpoint */
724static int
725mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
726{
727 struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
728 struct mv_req *req = container_of(_req, struct mv_req, req);
729 struct mv_udc *udc = ep->udc;
730 unsigned long flags;
731
732 /* catch various bogus parameters */
733 if (!_req || !req->req.complete || !req->req.buf
734 || !list_empty(&req->queue)) {
735 dev_err(&udc->dev->dev, "%s, bad params", __func__);
736 return -EINVAL;
737 }
738 if (unlikely(!_ep || !ep->desc)) {
739 dev_err(&udc->dev->dev, "%s, bad ep", __func__);
740 return -EINVAL;
741 }
742 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
743 if (req->req.length > ep->ep.maxpacket)
744 return -EMSGSIZE;
745 }
746
747 udc = ep->udc;
748 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
749 return -ESHUTDOWN;
750
751 req->ep = ep;
752
753 /* map virtual address to hardware */
754 if (req->req.dma == DMA_ADDR_INVALID) {
755 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
756 req->req.buf,
757 req->req.length, ep_dir(ep)
758 ? DMA_TO_DEVICE
759 : DMA_FROM_DEVICE);
760 req->mapped = 1;
761 } else {
762 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
763 req->req.dma, req->req.length,
764 ep_dir(ep)
765 ? DMA_TO_DEVICE
766 : DMA_FROM_DEVICE);
767 req->mapped = 0;
768 }
769
770 req->req.status = -EINPROGRESS;
771 req->req.actual = 0;
772 req->dtd_count = 0;
773
774 spin_lock_irqsave(&udc->lock, flags);
775
776 /* build dtds and push them to device queue */
777 if (!req_to_dtd(req)) {
778 int retval;
779 retval = queue_dtd(ep, req);
780 if (retval) {
781 spin_unlock_irqrestore(&udc->lock, flags);
782 return retval;
783 }
784 } else {
785 spin_unlock_irqrestore(&udc->lock, flags);
786 return -ENOMEM;
787 }
788
789 /* Update ep0 state */
790 if (ep->ep_num == 0)
791 udc->ep0_state = DATA_STATE_XMIT;
792
793 /* irq handler advances the queue */
794 if (req != NULL)
795 list_add_tail(&req->queue, &ep->queue);
796 spin_unlock_irqrestore(&udc->lock, flags);
797
798 return 0;
799}
800
801/* dequeues (cancels, unlinks) an I/O request from an endpoint */
802static int mv_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
803{
804 struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
805 struct mv_req *req;
806 struct mv_udc *udc = ep->udc;
807 unsigned long flags;
808 int stopped, ret = 0;
809 u32 epctrlx;
810
811 if (!_ep || !_req)
812 return -EINVAL;
813
814 spin_lock_irqsave(&ep->udc->lock, flags);
815 stopped = ep->stopped;
816
817 /* Stop the ep before we deal with the queue */
818 ep->stopped = 1;
819 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
820 if (ep_dir(ep) == EP_DIR_IN)
821 epctrlx &= ~EPCTRL_TX_ENABLE;
822 else
823 epctrlx &= ~EPCTRL_RX_ENABLE;
824 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
825
826 /* make sure it's actually queued on this endpoint */
827 list_for_each_entry(req, &ep->queue, queue) {
828 if (&req->req == _req)
829 break;
830 }
831 if (&req->req != _req) {
832 ret = -EINVAL;
833 goto out;
834 }
835
836 /* The request is in progress, or completed but not dequeued */
837 if (ep->queue.next == &req->queue) {
838 _req->status = -ECONNRESET;
839 mv_ep_fifo_flush(_ep); /* flush current transfer */
840
841 /* The request isn't the last request in this ep queue */
842 if (req->queue.next != &ep->queue) {
843 struct mv_dqh *qh;
844 struct mv_req *next_req;
845
846 qh = ep->dqh;
847 next_req = list_entry(req->queue.next, struct mv_req,
848 queue);
849
850 /* Point the QH to the first TD of next request */
851 writel((u32) next_req->head, &qh->curr_dtd_ptr);
852 } else {
853 struct mv_dqh *qh;
854
855 qh = ep->dqh;
856 qh->next_dtd_ptr = 1;
857 qh->size_ioc_int_sts = 0;
858 }
859
860 /* The request hasn't been processed, patch up the TD chain */
861 } else {
862 struct mv_req *prev_req;
863
864 prev_req = list_entry(req->queue.prev, struct mv_req, queue);
865 writel(readl(&req->tail->dtd_next),
866 &prev_req->tail->dtd_next);
867
868 }
869
870 done(ep, req, -ECONNRESET);
871
872 /* Enable EP */
873out:
874 epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
875 if (ep_dir(ep) == EP_DIR_IN)
876 epctrlx |= EPCTRL_TX_ENABLE;
877 else
878 epctrlx |= EPCTRL_RX_ENABLE;
879 writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
880 ep->stopped = stopped;
881
882 spin_unlock_irqrestore(&ep->udc->lock, flags);
883 return ret;
884}
885
886static void ep_set_stall(struct mv_udc *udc, u8 ep_num, u8 direction, int stall)
887{
888 u32 epctrlx;
889
890 epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
891
892 if (stall) {
893 if (direction == EP_DIR_IN)
894 epctrlx |= EPCTRL_TX_EP_STALL;
895 else
896 epctrlx |= EPCTRL_RX_EP_STALL;
897 } else {
898 if (direction == EP_DIR_IN) {
899 epctrlx &= ~EPCTRL_TX_EP_STALL;
900 epctrlx |= EPCTRL_TX_DATA_TOGGLE_RST;
901 } else {
902 epctrlx &= ~EPCTRL_RX_EP_STALL;
903 epctrlx |= EPCTRL_RX_DATA_TOGGLE_RST;
904 }
905 }
906 writel(epctrlx, &udc->op_regs->epctrlx[ep_num]);
907}
908
909static int ep_is_stall(struct mv_udc *udc, u8 ep_num, u8 direction)
910{
911 u32 epctrlx;
912
913 epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
914
915 if (direction == EP_DIR_OUT)
916 return (epctrlx & EPCTRL_RX_EP_STALL) ? 1 : 0;
917 else
918 return (epctrlx & EPCTRL_TX_EP_STALL) ? 1 : 0;
919}
920
921static int mv_ep_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
922{
923 struct mv_ep *ep;
924 unsigned long flags = 0;
925 int status = 0;
926 struct mv_udc *udc;
927
928 ep = container_of(_ep, struct mv_ep, ep);
929 udc = ep->udc;
930 if (!_ep || !ep->desc) {
931 status = -EINVAL;
932 goto out;
933 }
934
935 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
936 status = -EOPNOTSUPP;
937 goto out;
938 }
939
940 /*
941 * Attempt to halt IN ep will fail if any transfer requests
942 * are still queue
943 */
944 if (halt && (ep_dir(ep) == EP_DIR_IN) && !list_empty(&ep->queue)) {
945 status = -EAGAIN;
946 goto out;
947 }
948
949 spin_lock_irqsave(&ep->udc->lock, flags);
950 ep_set_stall(udc, ep->ep_num, ep_dir(ep), halt);
951 if (halt && wedge)
952 ep->wedge = 1;
953 else if (!halt)
954 ep->wedge = 0;
955 spin_unlock_irqrestore(&ep->udc->lock, flags);
956
957 if (ep->ep_num == 0) {
958 udc->ep0_state = WAIT_FOR_SETUP;
959 udc->ep0_dir = EP_DIR_OUT;
960 }
961out:
962 return status;
963}
964
965static int mv_ep_set_halt(struct usb_ep *_ep, int halt)
966{
967 return mv_ep_set_halt_wedge(_ep, halt, 0);
968}
969
970static int mv_ep_set_wedge(struct usb_ep *_ep)
971{
972 return mv_ep_set_halt_wedge(_ep, 1, 1);
973}
974
975static struct usb_ep_ops mv_ep_ops = {
976 .enable = mv_ep_enable,
977 .disable = mv_ep_disable,
978
979 .alloc_request = mv_alloc_request,
980 .free_request = mv_free_request,
981
982 .queue = mv_ep_queue,
983 .dequeue = mv_ep_dequeue,
984
985 .set_wedge = mv_ep_set_wedge,
986 .set_halt = mv_ep_set_halt,
987 .fifo_flush = mv_ep_fifo_flush, /* flush fifo */
988};
989
990static void udc_stop(struct mv_udc *udc)
991{
992 u32 tmp;
993
994 /* Disable interrupts */
995 tmp = readl(&udc->op_regs->usbintr);
996 tmp &= ~(USBINTR_INT_EN | USBINTR_ERR_INT_EN |
997 USBINTR_PORT_CHANGE_DETECT_EN | USBINTR_RESET_EN);
998 writel(tmp, &udc->op_regs->usbintr);
999
1000 /* Reset the Run the bit in the command register to stop VUSB */
1001 tmp = readl(&udc->op_regs->usbcmd);
1002 tmp &= ~USBCMD_RUN_STOP;
1003 writel(tmp, &udc->op_regs->usbcmd);
1004}
1005
1006static void udc_start(struct mv_udc *udc)
1007{
1008 u32 usbintr;
1009
1010 usbintr = USBINTR_INT_EN | USBINTR_ERR_INT_EN
1011 | USBINTR_PORT_CHANGE_DETECT_EN
1012 | USBINTR_RESET_EN | USBINTR_DEVICE_SUSPEND;
1013 /* Enable interrupts */
1014 writel(usbintr, &udc->op_regs->usbintr);
1015
1016 /* Set the Run bit in the command register */
1017 writel(USBCMD_RUN_STOP, &udc->op_regs->usbcmd);
1018}
1019
1020static int udc_reset(struct mv_udc *udc)
1021{
1022 unsigned int loops;
1023 u32 tmp, portsc;
1024
1025 /* Stop the controller */
1026 tmp = readl(&udc->op_regs->usbcmd);
1027 tmp &= ~USBCMD_RUN_STOP;
1028 writel(tmp, &udc->op_regs->usbcmd);
1029
1030 /* Reset the controller to get default values */
1031 writel(USBCMD_CTRL_RESET, &udc->op_regs->usbcmd);
1032
1033 /* wait for reset to complete */
1034 loops = LOOPS(RESET_TIMEOUT);
1035 while (readl(&udc->op_regs->usbcmd) & USBCMD_CTRL_RESET) {
1036 if (loops == 0) {
1037 dev_err(&udc->dev->dev,
1038 "Wait for RESET completed TIMEOUT\n");
1039 return -ETIMEDOUT;
1040 }
1041 loops--;
1042 udelay(LOOPS_USEC);
1043 }
1044
1045 /* set controller to device mode */
1046 tmp = readl(&udc->op_regs->usbmode);
1047 tmp |= USBMODE_CTRL_MODE_DEVICE;
1048
1049 /* turn setup lockout off, require setup tripwire in usbcmd */
1050 tmp |= USBMODE_SETUP_LOCK_OFF | USBMODE_STREAM_DISABLE;
1051
1052 writel(tmp, &udc->op_regs->usbmode);
1053
1054 writel(0x0, &udc->op_regs->epsetupstat);
1055
1056 /* Configure the Endpoint List Address */
1057 writel(udc->ep_dqh_dma & USB_EP_LIST_ADDRESS_MASK,
1058 &udc->op_regs->eplistaddr);
1059
1060 portsc = readl(&udc->op_regs->portsc[0]);
1061 if (readl(&udc->cap_regs->hcsparams) & HCSPARAMS_PPC)
1062 portsc &= (~PORTSCX_W1C_BITS | ~PORTSCX_PORT_POWER);
1063
1064 if (udc->force_fs)
1065 portsc |= PORTSCX_FORCE_FULL_SPEED_CONNECT;
1066 else
1067 portsc &= (~PORTSCX_FORCE_FULL_SPEED_CONNECT);
1068
1069 writel(portsc, &udc->op_regs->portsc[0]);
1070
1071 tmp = readl(&udc->op_regs->epctrlx[0]);
1072 tmp &= ~(EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL);
1073 writel(tmp, &udc->op_regs->epctrlx[0]);
1074
1075 return 0;
1076}
1077
1078static int mv_udc_get_frame(struct usb_gadget *gadget)
1079{
1080 struct mv_udc *udc;
1081 u16 retval;
1082
1083 if (!gadget)
1084 return -ENODEV;
1085
1086 udc = container_of(gadget, struct mv_udc, gadget);
1087
1088 retval = readl(udc->op_regs->frindex) & USB_FRINDEX_MASKS;
1089
1090 return retval;
1091}
1092
1093/* Tries to wake up the host connected to this gadget */
1094static int mv_udc_wakeup(struct usb_gadget *gadget)
1095{
1096 struct mv_udc *udc = container_of(gadget, struct mv_udc, gadget);
1097 u32 portsc;
1098
1099 /* Remote wakeup feature not enabled by host */
1100 if (!udc->remote_wakeup)
1101 return -ENOTSUPP;
1102
1103 portsc = readl(&udc->op_regs->portsc);
1104 /* not suspended? */
1105 if (!(portsc & PORTSCX_PORT_SUSPEND))
1106 return 0;
1107 /* trigger force resume */
1108 portsc |= PORTSCX_PORT_FORCE_RESUME;
1109 writel(portsc, &udc->op_regs->portsc[0]);
1110 return 0;
1111}
1112
1113static int mv_udc_pullup(struct usb_gadget *gadget, int is_on)
1114{
1115 struct mv_udc *udc;
1116 unsigned long flags;
1117
1118 udc = container_of(gadget, struct mv_udc, gadget);
1119 spin_lock_irqsave(&udc->lock, flags);
1120
1121 udc->softconnect = (is_on != 0);
1122 if (udc->driver && udc->softconnect)
1123 udc_start(udc);
1124 else
1125 udc_stop(udc);
1126
1127 spin_unlock_irqrestore(&udc->lock, flags);
1128 return 0;
1129}
1130
1131/* device controller usb_gadget_ops structure */
1132static const struct usb_gadget_ops mv_ops = {
1133
1134 /* returns the current frame number */
1135 .get_frame = mv_udc_get_frame,
1136
1137 /* tries to wake up the host connected to this gadget */
1138 .wakeup = mv_udc_wakeup,
1139
1140 /* D+ pullup, software-controlled connect/disconnect to USB host */
1141 .pullup = mv_udc_pullup,
1142};
1143
1144static void mv_udc_testmode(struct mv_udc *udc, u16 index, bool enter)
1145{
1146 dev_info(&udc->dev->dev, "Test Mode is not support yet\n");
1147}
1148
1149static int eps_init(struct mv_udc *udc)
1150{
1151 struct mv_ep *ep;
1152 char name[14];
1153 int i;
1154
1155 /* initialize ep0 */
1156 ep = &udc->eps[0];
1157 ep->udc = udc;
1158 strncpy(ep->name, "ep0", sizeof(ep->name));
1159 ep->ep.name = ep->name;
1160 ep->ep.ops = &mv_ep_ops;
1161 ep->wedge = 0;
1162 ep->stopped = 0;
1163 ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
1164 ep->ep_num = 0;
1165 ep->desc = &mv_ep0_desc;
1166 INIT_LIST_HEAD(&ep->queue);
1167
1168 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1169
1170 /* initialize other endpoints */
1171 for (i = 2; i < udc->max_eps * 2; i++) {
1172 ep = &udc->eps[i];
1173 if (i % 2) {
1174 snprintf(name, sizeof(name), "ep%din", i / 2);
1175 ep->direction = EP_DIR_IN;
1176 } else {
1177 snprintf(name, sizeof(name), "ep%dout", i / 2);
1178 ep->direction = EP_DIR_OUT;
1179 }
1180 ep->udc = udc;
1181 strncpy(ep->name, name, sizeof(ep->name));
1182 ep->ep.name = ep->name;
1183
1184 ep->ep.ops = &mv_ep_ops;
1185 ep->stopped = 0;
1186 ep->ep.maxpacket = (unsigned short) ~0;
1187 ep->ep_num = i / 2;
1188
1189 INIT_LIST_HEAD(&ep->queue);
1190 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1191
1192 ep->dqh = &udc->ep_dqh[i];
1193 }
1194
1195 return 0;
1196}
1197
1198/* delete all endpoint requests, called with spinlock held */
1199static void nuke(struct mv_ep *ep, int status)
1200{
1201 /* called with spinlock held */
1202 ep->stopped = 1;
1203
1204 /* endpoint fifo flush */
1205 mv_ep_fifo_flush(&ep->ep);
1206
1207 while (!list_empty(&ep->queue)) {
1208 struct mv_req *req = NULL;
1209 req = list_entry(ep->queue.next, struct mv_req, queue);
1210 done(ep, req, status);
1211 }
1212}
1213
1214/* stop all USB activities */
1215static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver)
1216{
1217 struct mv_ep *ep;
1218
1219 nuke(&udc->eps[0], -ESHUTDOWN);
1220
1221 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
1222 nuke(ep, -ESHUTDOWN);
1223 }
1224
1225 /* report disconnect; the driver is already quiesced */
1226 if (driver) {
1227 spin_unlock(&udc->lock);
1228 driver->disconnect(&udc->gadget);
1229 spin_lock(&udc->lock);
1230 }
1231}
1232
1233int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1234 int (*bind)(struct usb_gadget *))
1235{
1236 struct mv_udc *udc = the_controller;
1237 int retval = 0;
1238 unsigned long flags;
1239
1240 if (!udc)
1241 return -ENODEV;
1242
1243 if (udc->driver)
1244 return -EBUSY;
1245
1246 spin_lock_irqsave(&udc->lock, flags);
1247
1248 /* hook up the driver ... */
1249 driver->driver.bus = NULL;
1250 udc->driver = driver;
1251 udc->gadget.dev.driver = &driver->driver;
1252
1253 udc->usb_state = USB_STATE_ATTACHED;
1254 udc->ep0_state = WAIT_FOR_SETUP;
1255 udc->ep0_dir = USB_DIR_OUT;
1256
1257 spin_unlock_irqrestore(&udc->lock, flags);
1258
1259 retval = bind(&udc->gadget);
1260 if (retval) {
1261 dev_err(&udc->dev->dev, "bind to driver %s --> %d\n",
1262 driver->driver.name, retval);
1263 udc->driver = NULL;
1264 udc->gadget.dev.driver = NULL;
1265 return retval;
1266 }
1267 udc_reset(udc);
1268 ep0_reset(udc);
1269 udc_start(udc);
1270
1271 return 0;
1272}
1273EXPORT_SYMBOL(usb_gadget_probe_driver);
1274
1275int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1276{
1277 struct mv_udc *udc = the_controller;
1278 unsigned long flags;
1279
1280 if (!udc)
1281 return -ENODEV;
1282
1283 udc_stop(udc);
1284
1285 spin_lock_irqsave(&udc->lock, flags);
1286
1287 /* stop all usb activities */
1288 udc->gadget.speed = USB_SPEED_UNKNOWN;
1289 stop_activity(udc, driver);
1290 spin_unlock_irqrestore(&udc->lock, flags);
1291
1292 /* unbind gadget driver */
1293 driver->unbind(&udc->gadget);
1294 udc->gadget.dev.driver = NULL;
1295 udc->driver = NULL;
1296
1297 return 0;
1298}
1299EXPORT_SYMBOL(usb_gadget_unregister_driver);
1300
1301static int
1302udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
1303{
1304 int retval = 0;
1305 struct mv_req *req;
1306 struct mv_ep *ep;
1307
1308 ep = &udc->eps[0];
1309 udc->ep0_dir = direction;
1310
1311 req = udc->status_req;
1312
1313 /* fill in the reqest structure */
1314 if (empty == false) {
1315 *((u16 *) req->req.buf) = cpu_to_le16(status);
1316 req->req.length = 2;
1317 } else
1318 req->req.length = 0;
1319
1320 req->ep = ep;
1321 req->req.status = -EINPROGRESS;
1322 req->req.actual = 0;
1323 req->req.complete = NULL;
1324 req->dtd_count = 0;
1325
1326 /* prime the data phase */
1327 if (!req_to_dtd(req))
1328 retval = queue_dtd(ep, req);
1329 else{ /* no mem */
1330 retval = -ENOMEM;
1331 goto out;
1332 }
1333
1334 if (retval) {
1335 dev_err(&udc->dev->dev, "response error on GET_STATUS request\n");
1336 goto out;
1337 }
1338
1339 list_add_tail(&req->queue, &ep->queue);
1340
1341 return 0;
1342out:
1343 return retval;
1344}
1345
1346static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1347{
1348 udc->dev_addr = (u8)setup->wValue;
1349
1350 /* update usb state */
1351 udc->usb_state = USB_STATE_ADDRESS;
1352
1353 if (udc_prime_status(udc, EP_DIR_IN, 0, true))
1354 ep0_stall(udc);
1355}
1356
1357static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
1358 struct usb_ctrlrequest *setup)
1359{
1360 u16 status;
1361 int retval;
1362
1363 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1364 != (USB_DIR_IN | USB_TYPE_STANDARD))
1365 return;
1366
1367 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1368 status = 1 << USB_DEVICE_SELF_POWERED;
1369 status |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1370 } else if ((setup->bRequestType & USB_RECIP_MASK)
1371 == USB_RECIP_INTERFACE) {
1372 /* get interface status */
1373 status = 0;
1374 } else if ((setup->bRequestType & USB_RECIP_MASK)
1375 == USB_RECIP_ENDPOINT) {
1376 u8 ep_num, direction;
1377
1378 ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
1379 direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
1380 ? EP_DIR_IN : EP_DIR_OUT;
1381 status = ep_is_stall(udc, ep_num, direction)
1382 << USB_ENDPOINT_HALT;
1383 }
1384
1385 retval = udc_prime_status(udc, EP_DIR_IN, status, false);
1386 if (retval)
1387 ep0_stall(udc);
1388}
1389
1390static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1391{
1392 u8 ep_num;
1393 u8 direction;
1394 struct mv_ep *ep;
1395
1396 if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
1397 == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
1398 switch (setup->wValue) {
1399 case USB_DEVICE_REMOTE_WAKEUP:
1400 udc->remote_wakeup = 0;
1401 break;
1402 case USB_DEVICE_TEST_MODE:
1403 mv_udc_testmode(udc, 0, false);
1404 break;
1405 default:
1406 goto out;
1407 }
1408 } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
1409 == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
1410 switch (setup->wValue) {
1411 case USB_ENDPOINT_HALT:
1412 ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
1413 direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
1414 ? EP_DIR_IN : EP_DIR_OUT;
1415 if (setup->wValue != 0 || setup->wLength != 0
1416 || ep_num > udc->max_eps)
1417 goto out;
1418 ep = &udc->eps[ep_num * 2 + direction];
1419 if (ep->wedge == 1)
1420 break;
1421 spin_unlock(&udc->lock);
1422 ep_set_stall(udc, ep_num, direction, 0);
1423 spin_lock(&udc->lock);
1424 break;
1425 default:
1426 goto out;
1427 }
1428 } else
1429 goto out;
1430
1431 if (udc_prime_status(udc, EP_DIR_IN, 0, true))
1432 ep0_stall(udc);
1433 else
1434 udc->ep0_state = DATA_STATE_XMIT;
1435out:
1436 return;
1437}
1438
1439static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
1440{
1441 u8 ep_num;
1442 u8 direction;
1443
1444 if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
1445 == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
1446 switch (setup->wValue) {
1447 case USB_DEVICE_REMOTE_WAKEUP:
1448 udc->remote_wakeup = 1;
1449 break;
1450 case USB_DEVICE_TEST_MODE:
1451 if (setup->wIndex & 0xFF
1452 && udc->gadget.speed != USB_SPEED_HIGH)
1453 goto out;
1454 if (udc->usb_state == USB_STATE_CONFIGURED
1455 || udc->usb_state == USB_STATE_ADDRESS
1456 || udc->usb_state == USB_STATE_DEFAULT)
1457 mv_udc_testmode(udc,
1458 setup->wIndex & 0xFF00, true);
1459 else
1460 goto out;
1461 break;
1462 default:
1463 goto out;
1464 }
1465 } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
1466 == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
1467 switch (setup->wValue) {
1468 case USB_ENDPOINT_HALT:
1469 ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
1470 direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
1471 ? EP_DIR_IN : EP_DIR_OUT;
1472 if (setup->wValue != 0 || setup->wLength != 0
1473 || ep_num > udc->max_eps)
1474 goto out;
1475 spin_unlock(&udc->lock);
1476 ep_set_stall(udc, ep_num, direction, 1);
1477 spin_lock(&udc->lock);
1478 break;
1479 default:
1480 goto out;
1481 }
1482 } else
1483 goto out;
1484
1485 if (udc_prime_status(udc, EP_DIR_IN, 0, true))
1486 ep0_stall(udc);
1487out:
1488 return;
1489}
1490
1491static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
1492 struct usb_ctrlrequest *setup)
1493{
1494 bool delegate = false;
1495
1496 nuke(&udc->eps[ep_num * 2 + EP_DIR_OUT], -ESHUTDOWN);
1497
1498 dev_dbg(&udc->dev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1499 setup->bRequestType, setup->bRequest,
1500 setup->wValue, setup->wIndex, setup->wLength);
1501 /* We process some stardard setup requests here */
1502 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1503 switch (setup->bRequest) {
1504 case USB_REQ_GET_STATUS:
1505 ch9getstatus(udc, ep_num, setup);
1506 break;
1507
1508 case USB_REQ_SET_ADDRESS:
1509 ch9setaddress(udc, setup);
1510 break;
1511
1512 case USB_REQ_CLEAR_FEATURE:
1513 ch9clearfeature(udc, setup);
1514 break;
1515
1516 case USB_REQ_SET_FEATURE:
1517 ch9setfeature(udc, setup);
1518 break;
1519
1520 default:
1521 delegate = true;
1522 }
1523 } else
1524 delegate = true;
1525
1526 /* delegate USB standard requests to the gadget driver */
1527 if (delegate == true) {
1528 /* USB requests handled by gadget */
1529 if (setup->wLength) {
1530 /* DATA phase from gadget, STATUS phase from udc */
1531 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1532 ? EP_DIR_IN : EP_DIR_OUT;
1533 spin_unlock(&udc->lock);
1534 if (udc->driver->setup(&udc->gadget,
1535 &udc->local_setup_buff) < 0)
1536 ep0_stall(udc);
1537 spin_lock(&udc->lock);
1538 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1539 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1540 } else {
1541 /* no DATA phase, IN STATUS phase from gadget */
1542 udc->ep0_dir = EP_DIR_IN;
1543 spin_unlock(&udc->lock);
1544 if (udc->driver->setup(&udc->gadget,
1545 &udc->local_setup_buff) < 0)
1546 ep0_stall(udc);
1547 spin_lock(&udc->lock);
1548 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1549 }
1550 }
1551}
1552
1553/* complete DATA or STATUS phase of ep0 prime status phase if needed */
1554static void ep0_req_complete(struct mv_udc *udc,
1555 struct mv_ep *ep0, struct mv_req *req)
1556{
1557 u32 new_addr;
1558
1559 if (udc->usb_state == USB_STATE_ADDRESS) {
1560 /* set the new address */
1561 new_addr = (u32)udc->dev_addr;
1562 writel(new_addr << USB_DEVICE_ADDRESS_BIT_SHIFT,
1563 &udc->op_regs->deviceaddr);
1564 }
1565
1566 done(ep0, req, 0);
1567
1568 switch (udc->ep0_state) {
1569 case DATA_STATE_XMIT:
1570 /* receive status phase */
1571 if (udc_prime_status(udc, EP_DIR_OUT, 0, true))
1572 ep0_stall(udc);
1573 break;
1574 case DATA_STATE_RECV:
1575 /* send status phase */
1576 if (udc_prime_status(udc, EP_DIR_IN, 0 , true))
1577 ep0_stall(udc);
1578 break;
1579 case WAIT_FOR_OUT_STATUS:
1580 udc->ep0_state = WAIT_FOR_SETUP;
1581 break;
1582 case WAIT_FOR_SETUP:
1583 dev_err(&udc->dev->dev, "unexpect ep0 packets\n");
1584 break;
1585 default:
1586 ep0_stall(udc);
1587 break;
1588 }
1589}
1590
1591static void get_setup_data(struct mv_udc *udc, u8 ep_num, u8 *buffer_ptr)
1592{
1593 u32 temp;
1594 struct mv_dqh *dqh;
1595
1596 dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT];
1597
1598 /* Clear bit in ENDPTSETUPSTAT */
1599 temp = readl(&udc->op_regs->epsetupstat);
1600 writel(temp | (1 << ep_num), &udc->op_regs->epsetupstat);
1601
1602 /* while a hazard exists when setup package arrives */
1603 do {
1604 /* Set Setup Tripwire */
1605 temp = readl(&udc->op_regs->usbcmd);
1606 writel(temp | USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
1607
1608 /* Copy the setup packet to local buffer */
1609 memcpy(buffer_ptr, (u8 *) dqh->setup_buffer, 8);
1610 } while (!(readl(&udc->op_regs->usbcmd) & USBCMD_SETUP_TRIPWIRE_SET));
1611
1612 /* Clear Setup Tripwire */
1613 temp = readl(&udc->op_regs->usbcmd);
1614 writel(temp & ~USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
1615}
1616
1617static void irq_process_tr_complete(struct mv_udc *udc)
1618{
1619 u32 tmp, bit_pos;
1620 int i, ep_num = 0, direction = 0;
1621 struct mv_ep *curr_ep;
1622 struct mv_req *curr_req, *temp_req;
1623 int status;
1624
1625 /*
1626 * We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
1627 * because the setup packets are to be read ASAP
1628 */
1629
1630 /* Process all Setup packet received interrupts */
1631 tmp = readl(&udc->op_regs->epsetupstat);
1632
1633 if (tmp) {
1634 for (i = 0; i < udc->max_eps; i++) {
1635 if (tmp & (1 << i)) {
1636 get_setup_data(udc, i,
1637 (u8 *)(&udc->local_setup_buff));
1638 handle_setup_packet(udc, i,
1639 &udc->local_setup_buff);
1640 }
1641 }
1642 }
1643
1644 /* Don't clear the endpoint setup status register here.
1645 * It is cleared as a setup packet is read out of the buffer
1646 */
1647
1648 /* Process non-setup transaction complete interrupts */
1649 tmp = readl(&udc->op_regs->epcomplete);
1650
1651 if (!tmp)
1652 return;
1653
1654 writel(tmp, &udc->op_regs->epcomplete);
1655
1656 for (i = 0; i < udc->max_eps * 2; i++) {
1657 ep_num = i >> 1;
1658 direction = i % 2;
1659
1660 bit_pos = 1 << (ep_num + 16 * direction);
1661
1662 if (!(bit_pos & tmp))
1663 continue;
1664
1665 if (i == 1)
1666 curr_ep = &udc->eps[0];
1667 else
1668 curr_ep = &udc->eps[i];
1669 /* process the req queue until an uncomplete request */
1670 list_for_each_entry_safe(curr_req, temp_req,
1671 &curr_ep->queue, queue) {
1672 status = process_ep_req(udc, i, curr_req);
1673 if (status)
1674 break;
1675
1676 /* write back status to req */
1677 curr_req->req.status = status;
1678
1679 /* ep0 request completion */
1680 if (ep_num == 0) {
1681 ep0_req_complete(udc, curr_ep, curr_req);
1682 break;
1683 } else {
1684 done(curr_ep, curr_req, status);
1685 }
1686 }
1687 }
1688}
1689
1690void irq_process_reset(struct mv_udc *udc)
1691{
1692 u32 tmp;
1693 unsigned int loops;
1694
1695 udc->ep0_dir = EP_DIR_OUT;
1696 udc->ep0_state = WAIT_FOR_SETUP;
1697 udc->remote_wakeup = 0; /* default to 0 on reset */
1698
1699 /* The address bits are past bit 25-31. Set the address */
1700 tmp = readl(&udc->op_regs->deviceaddr);
1701 tmp &= ~(USB_DEVICE_ADDRESS_MASK);
1702 writel(tmp, &udc->op_regs->deviceaddr);
1703
1704 /* Clear all the setup token semaphores */
1705 tmp = readl(&udc->op_regs->epsetupstat);
1706 writel(tmp, &udc->op_regs->epsetupstat);
1707
1708 /* Clear all the endpoint complete status bits */
1709 tmp = readl(&udc->op_regs->epcomplete);
1710 writel(tmp, &udc->op_regs->epcomplete);
1711
1712 /* wait until all endptprime bits cleared */
1713 loops = LOOPS(PRIME_TIMEOUT);
1714 while (readl(&udc->op_regs->epprime) & 0xFFFFFFFF) {
1715 if (loops == 0) {
1716 dev_err(&udc->dev->dev,
1717 "Timeout for ENDPTPRIME = 0x%x\n",
1718 readl(&udc->op_regs->epprime));
1719 break;
1720 }
1721 loops--;
1722 udelay(LOOPS_USEC);
1723 }
1724
1725 /* Write 1s to the Flush register */
1726 writel((u32)~0, &udc->op_regs->epflush);
1727
1728 if (readl(&udc->op_regs->portsc[0]) & PORTSCX_PORT_RESET) {
1729 dev_info(&udc->dev->dev, "usb bus reset\n");
1730 udc->usb_state = USB_STATE_DEFAULT;
1731 /* reset all the queues, stop all USB activities */
1732 stop_activity(udc, udc->driver);
1733 } else {
1734 dev_info(&udc->dev->dev, "USB reset portsc 0x%x\n",
1735 readl(&udc->op_regs->portsc));
1736
1737 /*
1738 * re-initialize
1739 * controller reset
1740 */
1741 udc_reset(udc);
1742
1743 /* reset all the queues, stop all USB activities */
1744 stop_activity(udc, udc->driver);
1745
1746 /* reset ep0 dQH and endptctrl */
1747 ep0_reset(udc);
1748
1749 /* enable interrupt and set controller to run state */
1750 udc_start(udc);
1751
1752 udc->usb_state = USB_STATE_ATTACHED;
1753 }
1754}
1755
1756static void handle_bus_resume(struct mv_udc *udc)
1757{
1758 udc->usb_state = udc->resume_state;
1759 udc->resume_state = 0;
1760
1761 /* report resume to the driver */
1762 if (udc->driver) {
1763 if (udc->driver->resume) {
1764 spin_unlock(&udc->lock);
1765 udc->driver->resume(&udc->gadget);
1766 spin_lock(&udc->lock);
1767 }
1768 }
1769}
1770
1771static void irq_process_suspend(struct mv_udc *udc)
1772{
1773 udc->resume_state = udc->usb_state;
1774 udc->usb_state = USB_STATE_SUSPENDED;
1775
1776 if (udc->driver->suspend) {
1777 spin_unlock(&udc->lock);
1778 udc->driver->suspend(&udc->gadget);
1779 spin_lock(&udc->lock);
1780 }
1781}
1782
1783static void irq_process_port_change(struct mv_udc *udc)
1784{
1785 u32 portsc;
1786
1787 portsc = readl(&udc->op_regs->portsc[0]);
1788 if (!(portsc & PORTSCX_PORT_RESET)) {
1789 /* Get the speed */
1790 u32 speed = portsc & PORTSCX_PORT_SPEED_MASK;
1791 switch (speed) {
1792 case PORTSCX_PORT_SPEED_HIGH:
1793 udc->gadget.speed = USB_SPEED_HIGH;
1794 break;
1795 case PORTSCX_PORT_SPEED_FULL:
1796 udc->gadget.speed = USB_SPEED_FULL;
1797 break;
1798 case PORTSCX_PORT_SPEED_LOW:
1799 udc->gadget.speed = USB_SPEED_LOW;
1800 break;
1801 default:
1802 udc->gadget.speed = USB_SPEED_UNKNOWN;
1803 break;
1804 }
1805 }
1806
1807 if (portsc & PORTSCX_PORT_SUSPEND) {
1808 udc->resume_state = udc->usb_state;
1809 udc->usb_state = USB_STATE_SUSPENDED;
1810 if (udc->driver->suspend) {
1811 spin_unlock(&udc->lock);
1812 udc->driver->suspend(&udc->gadget);
1813 spin_lock(&udc->lock);
1814 }
1815 }
1816
1817 if (!(portsc & PORTSCX_PORT_SUSPEND)
1818 && udc->usb_state == USB_STATE_SUSPENDED) {
1819 handle_bus_resume(udc);
1820 }
1821
1822 if (!udc->resume_state)
1823 udc->usb_state = USB_STATE_DEFAULT;
1824}
1825
1826static void irq_process_error(struct mv_udc *udc)
1827{
1828 /* Increment the error count */
1829 udc->errors++;
1830}
1831
1832static irqreturn_t mv_udc_irq(int irq, void *dev)
1833{
1834 struct mv_udc *udc = (struct mv_udc *)dev;
1835 u32 status, intr;
1836
1837 spin_lock(&udc->lock);
1838
1839 status = readl(&udc->op_regs->usbsts);
1840 intr = readl(&udc->op_regs->usbintr);
1841 status &= intr;
1842
1843 if (status == 0) {
1844 spin_unlock(&udc->lock);
1845 return IRQ_NONE;
1846 }
1847
1848 /* Clear all the interrupts occurred */
1849 writel(status, &udc->op_regs->usbsts);
1850
1851 if (status & USBSTS_ERR)
1852 irq_process_error(udc);
1853
1854 if (status & USBSTS_RESET)
1855 irq_process_reset(udc);
1856
1857 if (status & USBSTS_PORT_CHANGE)
1858 irq_process_port_change(udc);
1859
1860 if (status & USBSTS_INT)
1861 irq_process_tr_complete(udc);
1862
1863 if (status & USBSTS_SUSPEND)
1864 irq_process_suspend(udc);
1865
1866 spin_unlock(&udc->lock);
1867
1868 return IRQ_HANDLED;
1869}
1870
1871/* release device structure */
1872static void gadget_release(struct device *_dev)
1873{
1874 struct mv_udc *udc = the_controller;
1875
1876 complete(udc->done);
1877 kfree(udc);
1878}
1879
1880static int mv_udc_remove(struct platform_device *dev)
1881{
1882 struct mv_udc *udc = the_controller;
1883
1884 DECLARE_COMPLETION(done);
1885
1886 udc->done = &done;
1887
1888 /* free memory allocated in probe */
1889 if (udc->dtd_pool)
1890 dma_pool_destroy(udc->dtd_pool);
1891
1892 if (udc->ep_dqh)
1893 dma_free_coherent(&dev->dev, udc->ep_dqh_size,
1894 udc->ep_dqh, udc->ep_dqh_dma);
1895
1896 kfree(udc->eps);
1897
1898 if (udc->irq)
1899 free_irq(udc->irq, &dev->dev);
1900
1901 if (udc->cap_regs)
1902 iounmap(udc->cap_regs);
1903 udc->cap_regs = NULL;
1904
1905 if (udc->phy_regs)
1906 iounmap((void *)udc->phy_regs);
1907 udc->phy_regs = 0;
1908
1909 if (udc->status_req) {
1910 kfree(udc->status_req->req.buf);
1911 kfree(udc->status_req);
1912 }
1913
1914 device_unregister(&udc->gadget.dev);
1915
1916 /* free dev, wait for the release() finished */
1917 wait_for_completion(&done);
1918
1919 the_controller = NULL;
1920
1921 return 0;
1922}
1923
1924int mv_udc_probe(struct platform_device *dev)
1925{
1926 struct mv_udc *udc;
1927 int retval = 0;
1928 struct resource *r;
1929 size_t size;
1930
1931 udc = kzalloc(sizeof *udc, GFP_KERNEL);
1932 if (udc == NULL) {
1933 dev_err(&dev->dev, "failed to allocate memory for udc\n");
1934 retval = -ENOMEM;
1935 goto error;
1936 }
1937
1938 spin_lock_init(&udc->lock);
1939
1940 udc->dev = dev;
1941
1942 udc->clk = clk_get(&dev->dev, "U2OCLK");
1943 if (IS_ERR(udc->clk)) {
1944 retval = PTR_ERR(udc->clk);
1945 goto error;
1946 }
1947
1948 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2o");
1949 if (r == NULL) {
1950 dev_err(&dev->dev, "no I/O memory resource defined\n");
1951 retval = -ENODEV;
1952 goto error;
1953 }
1954
1955 udc->cap_regs = (struct mv_cap_regs __iomem *)
1956 ioremap(r->start, resource_size(r));
1957 if (udc->cap_regs == NULL) {
1958 dev_err(&dev->dev, "failed to map I/O memory\n");
1959 retval = -EBUSY;
1960 goto error;
1961 }
1962
1963 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2ophy");
1964 if (r == NULL) {
1965 dev_err(&dev->dev, "no phy I/O memory resource defined\n");
1966 retval = -ENODEV;
1967 goto error;
1968 }
1969
1970 udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r));
1971 if (udc->phy_regs == 0) {
1972 dev_err(&dev->dev, "failed to map phy I/O memory\n");
1973 retval = -EBUSY;
1974 goto error;
1975 }
1976
1977 /* we will acces controller register, so enable the clk */
1978 clk_enable(udc->clk);
1979 retval = mv_udc_phy_init(udc->phy_regs);
1980 if (retval) {
1981 dev_err(&dev->dev, "phy initialization error %d\n", retval);
1982 goto error;
1983 }
1984
1985 udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs
1986 + (readl(&udc->cap_regs->caplength_hciversion)
1987 & CAPLENGTH_MASK));
1988 udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
1989
1990 size = udc->max_eps * sizeof(struct mv_dqh) *2;
1991 size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
1992 udc->ep_dqh = dma_alloc_coherent(&dev->dev, size,
1993 &udc->ep_dqh_dma, GFP_KERNEL);
1994
1995 if (udc->ep_dqh == NULL) {
1996 dev_err(&dev->dev, "allocate dQH memory failed\n");
1997 retval = -ENOMEM;
1998 goto error;
1999 }
2000 udc->ep_dqh_size = size;
2001
2002 /* create dTD dma_pool resource */
2003 udc->dtd_pool = dma_pool_create("mv_dtd",
2004 &dev->dev,
2005 sizeof(struct mv_dtd),
2006 DTD_ALIGNMENT,
2007 DMA_BOUNDARY);
2008
2009 if (!udc->dtd_pool) {
2010 retval = -ENOMEM;
2011 goto error;
2012 }
2013
2014 size = udc->max_eps * sizeof(struct mv_ep) *2;
2015 udc->eps = kzalloc(size, GFP_KERNEL);
2016 if (udc->eps == NULL) {
2017 dev_err(&dev->dev, "allocate ep memory failed\n");
2018 retval = -ENOMEM;
2019 goto error;
2020 }
2021
2022 /* initialize ep0 status request structure */
2023 udc->status_req = kzalloc(sizeof(struct mv_req), GFP_KERNEL);
2024 if (!udc->status_req) {
2025 dev_err(&dev->dev, "allocate status_req memory failed\n");
2026 retval = -ENOMEM;
2027 goto error;
2028 }
2029 INIT_LIST_HEAD(&udc->status_req->queue);
2030
2031 /* allocate a small amount of memory to get valid address */
2032 udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
2033 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2034
2035 udc->resume_state = USB_STATE_NOTATTACHED;
2036 udc->usb_state = USB_STATE_POWERED;
2037 udc->ep0_dir = EP_DIR_OUT;
2038 udc->remote_wakeup = 0;
2039
2040 r = platform_get_resource(udc->dev, IORESOURCE_IRQ, 0);
2041 if (r == NULL) {
2042 dev_err(&dev->dev, "no IRQ resource defined\n");
2043 retval = -ENODEV;
2044 goto error;
2045 }
2046 udc->irq = r->start;
2047 if (request_irq(udc->irq, mv_udc_irq,
2048 IRQF_DISABLED | IRQF_SHARED, driver_name, udc)) {
2049 dev_err(&dev->dev, "Request irq %d for UDC failed\n",
2050 udc->irq);
2051 retval = -ENODEV;
2052 goto error;
2053 }
2054
2055 /* initialize gadget structure */
2056 udc->gadget.ops = &mv_ops; /* usb_gadget_ops */
2057 udc->gadget.ep0 = &udc->eps[0].ep; /* gadget ep0 */
2058 INIT_LIST_HEAD(&udc->gadget.ep_list); /* ep_list */
2059 udc->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
2060 udc->gadget.is_dualspeed = 1; /* support dual speed */
2061
2062 /* the "gadget" abstracts/virtualizes the controller */
2063 dev_set_name(&udc->gadget.dev, "gadget");
2064 udc->gadget.dev.parent = &dev->dev;
2065 udc->gadget.dev.dma_mask = dev->dev.dma_mask;
2066 udc->gadget.dev.release = gadget_release;
2067 udc->gadget.name = driver_name; /* gadget name */
2068
2069 retval = device_register(&udc->gadget.dev);
2070 if (retval)
2071 goto error;
2072
2073 eps_init(udc);
2074
2075 the_controller = udc;
2076
2077 goto out;
2078error:
2079 if (udc)
2080 mv_udc_remove(udc->dev);
2081out:
2082 return retval;
2083}
2084
2085#ifdef CONFIG_PM
2086static int mv_udc_suspend(struct device *_dev)
2087{
2088 struct mv_udc *udc = the_controller;
2089
2090 udc_stop(udc);
2091
2092 return 0;
2093}
2094
2095static int mv_udc_resume(struct device *_dev)
2096{
2097 struct mv_udc *udc = the_controller;
2098 int retval;
2099
2100 retval = mv_udc_phy_init(udc->phy_regs);
2101 if (retval) {
2102 dev_err(_dev, "phy initialization error %d\n", retval);
2103 return retval;
2104 }
2105 udc_reset(udc);
2106 ep0_reset(udc);
2107 udc_start(udc);
2108
2109 return 0;
2110}
2111
2112static const struct dev_pm_ops mv_udc_pm_ops = {
2113 .suspend = mv_udc_suspend,
2114 .resume = mv_udc_resume,
2115};
2116#endif
2117
2118static struct platform_driver udc_driver = {
2119 .probe = mv_udc_probe,
2120 .remove = __exit_p(mv_udc_remove),
2121 .driver = {
2122 .owner = THIS_MODULE,
2123 .name = "pxa-u2o",
2124#ifdef CONFIG_PM
2125 .pm = &mv_udc_pm_ops,
2126#endif
2127 },
2128};
2129
2130
2131MODULE_DESCRIPTION(DRIVER_DESC);
2132MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
2133MODULE_VERSION(DRIVER_VERSION);
2134MODULE_LICENSE("GPL");
2135
2136
2137static int __init init(void)
2138{
2139 return platform_driver_register(&udc_driver);
2140}
2141module_init(init);
2142
2143
2144static void __exit cleanup(void)
2145{
2146 platform_driver_unregister(&udc_driver);
2147}
2148module_exit(cleanup);
2149
diff --git a/drivers/usb/gadget/mv_udc_phy.c b/drivers/usb/gadget/mv_udc_phy.c
new file mode 100644
index 000000000000..d4dea97e38a5
--- /dev/null
+++ b/drivers/usb/gadget/mv_udc_phy.c
@@ -0,0 +1,214 @@
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
new file mode 100644
index 000000000000..99c179ad729d
--- /dev/null
+++ b/drivers/usb/gadget/ncm.c
@@ -0,0 +1,248 @@
1/*
2 * ncm.c -- NCM gadget driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from ether.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
11 * Copyright (C) 2008 Nokia Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify
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
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 */
27
28/* #define DEBUG */
29/* #define VERBOSE_DEBUG */
30
31#include <linux/kernel.h>
32#include <linux/utsname.h>
33
34
35#include "u_ether.h"
36
37#define DRIVER_DESC "NCM Gadget"
38
39/*-------------------------------------------------------------------------*/
40
41/*
42 * Kbuild is not very cooperative with respect to linking separately
43 * compiled library objects into one module. So for now we won't use
44 * separate compilation ... ensuring init/exit sections work to shrink
45 * the runtime footprint, and giving us at least some parts of what
46 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
47 */
48#include "composite.c"
49#include "usbstring.c"
50#include "config.c"
51#include "epautoconf.c"
52
53#include "f_ncm.c"
54#include "u_ether.c"
55
56/*-------------------------------------------------------------------------*/
57
58/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
59 * Instead: allocate your own, using normal USB-IF procedures.
60 */
61
62/* Thanks to NetChip Technologies for donating this product ID.
63 * It's for devices with only CDC Ethernet configurations.
64 */
65#define CDC_VENDOR_NUM 0x0525 /* NetChip */
66#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
67
68/*-------------------------------------------------------------------------*/
69
70static struct usb_device_descriptor device_desc = {
71 .bLength = sizeof device_desc,
72 .bDescriptorType = USB_DT_DEVICE,
73
74 .bcdUSB = cpu_to_le16 (0x0200),
75
76 .bDeviceClass = USB_CLASS_COMM,
77 .bDeviceSubClass = 0,
78 .bDeviceProtocol = 0,
79 /* .bMaxPacketSize0 = f(hardware) */
80
81 /* Vendor and product id defaults change according to what configs
82 * we support. (As does bNumConfigurations.) These values can
83 * also be overridden by module parameters.
84 */
85 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
86 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
87 /* .bcdDevice = f(hardware) */
88 /* .iManufacturer = DYNAMIC */
89 /* .iProduct = DYNAMIC */
90 /* NO SERIAL NUMBER */
91 .bNumConfigurations = 1,
92};
93
94static struct usb_otg_descriptor otg_descriptor = {
95 .bLength = sizeof otg_descriptor,
96 .bDescriptorType = USB_DT_OTG,
97
98 /* REVISIT SRP-only hardware is possible, although
99 * it would not be called "OTG" ...
100 */
101 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
102};
103
104static const struct usb_descriptor_header *otg_desc[] = {
105 (struct usb_descriptor_header *) &otg_descriptor,
106 NULL,
107};
108
109
110/* string IDs are assigned dynamically */
111
112#define STRING_MANUFACTURER_IDX 0
113#define STRING_PRODUCT_IDX 1
114
115static char manufacturer[50];
116
117static struct usb_string strings_dev[] = {
118 [STRING_MANUFACTURER_IDX].s = manufacturer,
119 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
120 { } /* end of list */
121};
122
123static struct usb_gadget_strings stringtab_dev = {
124 .language = 0x0409, /* en-us */
125 .strings = strings_dev,
126};
127
128static struct usb_gadget_strings *dev_strings[] = {
129 &stringtab_dev,
130 NULL,
131};
132
133static u8 hostaddr[ETH_ALEN];
134
135/*-------------------------------------------------------------------------*/
136
137static int __init ncm_do_config(struct usb_configuration *c)
138{
139 /* FIXME alloc iConfiguration string, set it in c->strings */
140
141 if (gadget_is_otg(c->cdev->gadget)) {
142 c->descriptors = otg_desc;
143 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
144 }
145
146 return ncm_bind_config(c, hostaddr);
147}
148
149static struct usb_configuration ncm_config_driver = {
150 /* .label = f(hardware) */
151 .label = "CDC Ethernet (NCM)",
152 .bConfigurationValue = 1,
153 /* .iConfiguration = DYNAMIC */
154 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
155};
156
157/*-------------------------------------------------------------------------*/
158
159static int __init gncm_bind(struct usb_composite_dev *cdev)
160{
161 int gcnum;
162 struct usb_gadget *gadget = cdev->gadget;
163 int status;
164
165 /* set up network link layer */
166 status = gether_setup(cdev->gadget, hostaddr);
167 if (status < 0)
168 return status;
169
170 gcnum = usb_gadget_controller_number(gadget);
171 if (gcnum >= 0)
172 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
173 else {
174 /* We assume that can_support_ecm() tells the truth;
175 * but if the controller isn't recognized at all then
176 * that assumption is a bit more likely to be wrong.
177 */
178 dev_warn(&gadget->dev,
179 "controller '%s' not recognized; trying %s\n",
180 gadget->name,
181 ncm_config_driver.label);
182 device_desc.bcdDevice =
183 cpu_to_le16(0x0300 | 0x0099);
184 }
185
186
187 /* Allocate string descriptor numbers ... note that string
188 * contents can be overridden by the composite_dev glue.
189 */
190
191 /* device descriptor strings: manufacturer, product */
192 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
193 init_utsname()->sysname, init_utsname()->release,
194 gadget->name);
195 status = usb_string_id(cdev);
196 if (status < 0)
197 goto fail;
198 strings_dev[STRING_MANUFACTURER_IDX].id = status;
199 device_desc.iManufacturer = status;
200
201 status = usb_string_id(cdev);
202 if (status < 0)
203 goto fail;
204 strings_dev[STRING_PRODUCT_IDX].id = status;
205 device_desc.iProduct = status;
206
207 status = usb_add_config(cdev, &ncm_config_driver,
208 ncm_do_config);
209 if (status < 0)
210 goto fail;
211
212 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
213
214 return 0;
215
216fail:
217 gether_cleanup();
218 return status;
219}
220
221static int __exit gncm_unbind(struct usb_composite_dev *cdev)
222{
223 gether_cleanup();
224 return 0;
225}
226
227static struct usb_composite_driver ncm_driver = {
228 .name = "g_ncm",
229 .dev = &device_desc,
230 .strings = dev_strings,
231 .unbind = __exit_p(gncm_unbind),
232};
233
234MODULE_DESCRIPTION(DRIVER_DESC);
235MODULE_AUTHOR("Yauheni Kaliuta");
236MODULE_LICENSE("GPL");
237
238static int __init init(void)
239{
240 return usb_composite_probe(&ncm_driver, gncm_bind);
241}
242module_init(init);
243
244static void __exit cleanup(void)
245{
246 usb_composite_unregister(&ncm_driver);
247}
248module_exit(cleanup);
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index 9498be87a724..476d88e1ae97 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -63,6 +63,7 @@
63#include <linux/device.h> 63#include <linux/device.h>
64#include <linux/usb/ch9.h> 64#include <linux/usb/ch9.h>
65#include <linux/usb/gadget.h> 65#include <linux/usb/gadget.h>
66#include <linux/prefetch.h>
66 67
67#include <asm/byteorder.h> 68#include <asm/byteorder.h>
68#include <asm/io.h> 69#include <asm/io.h>
@@ -117,7 +118,7 @@ module_param (fifo_mode, ushort, 0644);
117 118
118/* enable_suspend -- When enabled, the driver will respond to 119/* enable_suspend -- When enabled, the driver will respond to
119 * USB suspend requests by powering down the NET2280. Otherwise, 120 * USB suspend requests by powering down the NET2280. Otherwise,
120 * USB suspend requests will be ignored. This is acceptible for 121 * USB suspend requests will be ignored. This is acceptable for
121 * self-powered devices 122 * self-powered devices
122 */ 123 */
123static int enable_suspend = 0; 124static int enable_suspend = 0;
@@ -1929,7 +1930,8 @@ static void ep0_start (struct net2280 *dev)
1929 * disconnect is reported. then a host may connect again, or 1930 * disconnect is reported. then a host may connect again, or
1930 * the driver might get unbound. 1931 * the driver might get unbound.
1931 */ 1932 */
1932int usb_gadget_register_driver (struct usb_gadget_driver *driver) 1933int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1934 int (*bind)(struct usb_gadget *))
1933{ 1935{
1934 struct net2280 *dev = the_controller; 1936 struct net2280 *dev = the_controller;
1935 int retval; 1937 int retval;
@@ -1941,8 +1943,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1941 */ 1943 */
1942 if (!driver 1944 if (!driver
1943 || driver->speed != USB_SPEED_HIGH 1945 || driver->speed != USB_SPEED_HIGH
1944 || !driver->bind 1946 || !bind || !driver->setup)
1945 || !driver->setup)
1946 return -EINVAL; 1947 return -EINVAL;
1947 if (!dev) 1948 if (!dev)
1948 return -ENODEV; 1949 return -ENODEV;
@@ -1957,7 +1958,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1957 driver->driver.bus = NULL; 1958 driver->driver.bus = NULL;
1958 dev->driver = driver; 1959 dev->driver = driver;
1959 dev->gadget.dev.driver = &driver->driver; 1960 dev->gadget.dev.driver = &driver->driver;
1960 retval = driver->bind (&dev->gadget); 1961 retval = bind(&dev->gadget);
1961 if (retval) { 1962 if (retval) {
1962 DEBUG (dev, "bind to driver %s --> %d\n", 1963 DEBUG (dev, "bind to driver %s --> %d\n",
1963 driver->driver.name, retval); 1964 driver->driver.name, retval);
@@ -1993,7 +1994,7 @@ err_unbind:
1993 dev->driver = NULL; 1994 dev->driver = NULL;
1994 return retval; 1995 return retval;
1995} 1996}
1996EXPORT_SYMBOL (usb_gadget_register_driver); 1997EXPORT_SYMBOL(usb_gadget_probe_driver);
1997 1998
1998static void 1999static void
1999stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver) 2000stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
diff --git a/drivers/usb/gadget/nokia.c b/drivers/usb/gadget/nokia.c
index 7d6b66a85724..55ca63ad3506 100644
--- a/drivers/usb/gadget/nokia.c
+++ b/drivers/usb/gadget/nokia.c
@@ -135,7 +135,6 @@ static int __init nokia_bind_config(struct usb_configuration *c)
135 135
136static struct usb_configuration nokia_config_500ma_driver = { 136static struct usb_configuration nokia_config_500ma_driver = {
137 .label = "Bus Powered", 137 .label = "Bus Powered",
138 .bind = nokia_bind_config,
139 .bConfigurationValue = 1, 138 .bConfigurationValue = 1,
140 /* .iConfiguration = DYNAMIC */ 139 /* .iConfiguration = DYNAMIC */
141 .bmAttributes = USB_CONFIG_ATT_ONE, 140 .bmAttributes = USB_CONFIG_ATT_ONE,
@@ -144,7 +143,6 @@ static struct usb_configuration nokia_config_500ma_driver = {
144 143
145static struct usb_configuration nokia_config_100ma_driver = { 144static struct usb_configuration nokia_config_100ma_driver = {
146 .label = "Self Powered", 145 .label = "Self Powered",
147 .bind = nokia_bind_config,
148 .bConfigurationValue = 2, 146 .bConfigurationValue = 2,
149 /* .iConfiguration = DYNAMIC */ 147 /* .iConfiguration = DYNAMIC */
150 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 148 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
@@ -205,12 +203,14 @@ static int __init nokia_bind(struct usb_composite_dev *cdev)
205 goto err_usb; 203 goto err_usb;
206 } 204 }
207 205
208 /* finaly register the configuration */ 206 /* finally register the configuration */
209 status = usb_add_config(cdev, &nokia_config_500ma_driver); 207 status = usb_add_config(cdev, &nokia_config_500ma_driver,
208 nokia_bind_config);
210 if (status < 0) 209 if (status < 0)
211 goto err_usb; 210 goto err_usb;
212 211
213 status = usb_add_config(cdev, &nokia_config_100ma_driver); 212 status = usb_add_config(cdev, &nokia_config_100ma_driver,
213 nokia_bind_config);
214 if (status < 0) 214 if (status < 0)
215 goto err_usb; 215 goto err_usb;
216 216
@@ -241,13 +241,12 @@ static struct usb_composite_driver nokia_driver = {
241 .name = "g_nokia", 241 .name = "g_nokia",
242 .dev = &device_desc, 242 .dev = &device_desc,
243 .strings = dev_strings, 243 .strings = dev_strings,
244 .bind = nokia_bind,
245 .unbind = __exit_p(nokia_unbind), 244 .unbind = __exit_p(nokia_unbind),
246}; 245};
247 246
248static int __init nokia_init(void) 247static int __init nokia_init(void)
249{ 248{
250 return usb_composite_register(&nokia_driver); 249 return usb_composite_probe(&nokia_driver, nokia_bind);
251} 250}
252module_init(nokia_init); 251module_init(nokia_init);
253 252
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index f81e4f025f23..82fd24935332 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -44,6 +44,7 @@
44#include <linux/usb/otg.h> 44#include <linux/usb/otg.h>
45#include <linux/dma-mapping.h> 45#include <linux/dma-mapping.h>
46#include <linux/clk.h> 46#include <linux/clk.h>
47#include <linux/prefetch.h>
47 48
48#include <asm/byteorder.h> 49#include <asm/byteorder.h>
49#include <asm/io.h> 50#include <asm/io.h>
@@ -54,7 +55,6 @@
54 55
55#include <plat/dma.h> 56#include <plat/dma.h>
56#include <plat/usb.h> 57#include <plat/usb.h>
57#include <plat/control.h>
58 58
59#include "omap_udc.h" 59#include "omap_udc.h"
60 60
@@ -2102,7 +2102,8 @@ static inline int machine_without_vbus_sense(void)
2102 ); 2102 );
2103} 2103}
2104 2104
2105int usb_gadget_register_driver (struct usb_gadget_driver *driver) 2105int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2106 int (*bind)(struct usb_gadget *))
2106{ 2107{
2107 int status = -ENODEV; 2108 int status = -ENODEV;
2108 struct omap_ep *ep; 2109 struct omap_ep *ep;
@@ -2114,8 +2115,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2114 if (!driver 2115 if (!driver
2115 // FIXME if otg, check: driver->is_otg 2116 // FIXME if otg, check: driver->is_otg
2116 || driver->speed < USB_SPEED_FULL 2117 || driver->speed < USB_SPEED_FULL
2117 || !driver->bind 2118 || !bind || !driver->setup)
2118 || !driver->setup)
2119 return -EINVAL; 2119 return -EINVAL;
2120 2120
2121 spin_lock_irqsave(&udc->lock, flags); 2121 spin_lock_irqsave(&udc->lock, flags);
@@ -2145,7 +2145,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2145 if (udc->dc_clk != NULL) 2145 if (udc->dc_clk != NULL)
2146 omap_udc_enable_clock(1); 2146 omap_udc_enable_clock(1);
2147 2147
2148 status = driver->bind (&udc->gadget); 2148 status = bind(&udc->gadget);
2149 if (status) { 2149 if (status) {
2150 DBG("bind to %s --> %d\n", driver->driver.name, status); 2150 DBG("bind to %s --> %d\n", driver->driver.name, status);
2151 udc->gadget.dev.driver = NULL; 2151 udc->gadget.dev.driver = NULL;
@@ -2186,7 +2186,7 @@ done:
2186 omap_udc_enable_clock(0); 2186 omap_udc_enable_clock(0);
2187 return status; 2187 return status;
2188} 2188}
2189EXPORT_SYMBOL(usb_gadget_register_driver); 2189EXPORT_SYMBOL(usb_gadget_probe_driver);
2190 2190
2191int usb_gadget_unregister_driver (struct usb_gadget_driver *driver) 2191int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2192{ 2192{
@@ -2309,21 +2309,12 @@ static char *trx_mode(unsigned m, int enabled)
2309static int proc_otg_show(struct seq_file *s) 2309static int proc_otg_show(struct seq_file *s)
2310{ 2310{
2311 u32 tmp; 2311 u32 tmp;
2312 u32 trans; 2312 u32 trans = 0;
2313 char *ctrl_name; 2313 char *ctrl_name = "(UNKNOWN)";
2314 2314
2315 /* XXX This needs major revision for OMAP2+ */
2315 tmp = omap_readl(OTG_REV); 2316 tmp = omap_readl(OTG_REV);
2316 if (cpu_is_omap24xx()) { 2317 if (cpu_class_is_omap1()) {
2317 /*
2318 * REVISIT: Not clear how this works on OMAP2. trans
2319 * is ANDed to produce bits 7 and 8, which might make
2320 * sense for USB_TRANSCEIVER_CTRL on OMAP1,
2321 * but with CONTROL_DEVCONF, these bits have something to
2322 * do with the frame adjustment counter and McBSP2.
2323 */
2324 ctrl_name = "control_devconf";
2325 trans = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
2326 } else {
2327 ctrl_name = "tranceiver_ctrl"; 2318 ctrl_name = "tranceiver_ctrl";
2328 trans = omap_readw(USB_TRANSCEIVER_CTRL); 2319 trans = omap_readw(USB_TRANSCEIVER_CTRL);
2329 } 2320 }
diff --git a/drivers/usb/gadget/pch_udc.c b/drivers/usb/gadget/pch_udc.c
new file mode 100644
index 000000000000..68dbcc3e4cc2
--- /dev/null
+++ b/drivers/usb/gadget/pch_udc.c
@@ -0,0 +1,3004 @@
1/*
2 * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
3 *
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
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 */
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/delay.h>
22#include <linux/errno.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/usb/ch9.h>
26#include <linux/usb/gadget.h>
27
28/* Address offset of Registers */
29#define UDC_EP_REG_SHIFT 0x20 /* Offset to next EP */
30
31#define UDC_EPCTL_ADDR 0x00 /* Endpoint control */
32#define UDC_EPSTS_ADDR 0x04 /* Endpoint status */
33#define UDC_BUFIN_FRAMENUM_ADDR 0x08 /* buffer size in / frame number out */
34#define UDC_BUFOUT_MAXPKT_ADDR 0x0C /* buffer size out / maxpkt in */
35#define UDC_SUBPTR_ADDR 0x10 /* setup buffer pointer */
36#define UDC_DESPTR_ADDR 0x14 /* Data descriptor pointer */
37#define UDC_CONFIRM_ADDR 0x18 /* Write/Read confirmation */
38
39#define UDC_DEVCFG_ADDR 0x400 /* Device configuration */
40#define UDC_DEVCTL_ADDR 0x404 /* Device control */
41#define UDC_DEVSTS_ADDR 0x408 /* Device status */
42#define UDC_DEVIRQSTS_ADDR 0x40C /* Device irq status */
43#define UDC_DEVIRQMSK_ADDR 0x410 /* Device irq mask */
44#define UDC_EPIRQSTS_ADDR 0x414 /* Endpoint irq status */
45#define UDC_EPIRQMSK_ADDR 0x418 /* Endpoint irq mask */
46#define UDC_DEVLPM_ADDR 0x41C /* LPM control / status */
47#define UDC_CSR_BUSY_ADDR 0x4f0 /* UDC_CSR_BUSY Status register */
48#define UDC_SRST_ADDR 0x4fc /* SOFT RESET register */
49#define UDC_CSR_ADDR 0x500 /* USB_DEVICE endpoint register */
50
51/* Endpoint control register */
52/* Bit position */
53#define UDC_EPCTL_MRXFLUSH (1 << 12)
54#define UDC_EPCTL_RRDY (1 << 9)
55#define UDC_EPCTL_CNAK (1 << 8)
56#define UDC_EPCTL_SNAK (1 << 7)
57#define UDC_EPCTL_NAK (1 << 6)
58#define UDC_EPCTL_P (1 << 3)
59#define UDC_EPCTL_F (1 << 1)
60#define UDC_EPCTL_S (1 << 0)
61#define UDC_EPCTL_ET_SHIFT 4
62/* Mask patern */
63#define UDC_EPCTL_ET_MASK 0x00000030
64/* Value for ET field */
65#define UDC_EPCTL_ET_CONTROL 0
66#define UDC_EPCTL_ET_ISO 1
67#define UDC_EPCTL_ET_BULK 2
68#define UDC_EPCTL_ET_INTERRUPT 3
69
70/* Endpoint status register */
71/* Bit position */
72#define UDC_EPSTS_XFERDONE (1 << 27)
73#define UDC_EPSTS_RSS (1 << 26)
74#define UDC_EPSTS_RCS (1 << 25)
75#define UDC_EPSTS_TXEMPTY (1 << 24)
76#define UDC_EPSTS_TDC (1 << 10)
77#define UDC_EPSTS_HE (1 << 9)
78#define UDC_EPSTS_MRXFIFO_EMP (1 << 8)
79#define UDC_EPSTS_BNA (1 << 7)
80#define UDC_EPSTS_IN (1 << 6)
81#define UDC_EPSTS_OUT_SHIFT 4
82/* Mask patern */
83#define UDC_EPSTS_OUT_MASK 0x00000030
84#define UDC_EPSTS_ALL_CLR_MASK 0x1F0006F0
85/* Value for OUT field */
86#define UDC_EPSTS_OUT_SETUP 2
87#define UDC_EPSTS_OUT_DATA 1
88
89/* Device configuration register */
90/* Bit position */
91#define UDC_DEVCFG_CSR_PRG (1 << 17)
92#define UDC_DEVCFG_SP (1 << 3)
93/* SPD Valee */
94#define UDC_DEVCFG_SPD_HS 0x0
95#define UDC_DEVCFG_SPD_FS 0x1
96#define UDC_DEVCFG_SPD_LS 0x2
97
98/* Device control register */
99/* Bit position */
100#define UDC_DEVCTL_THLEN_SHIFT 24
101#define UDC_DEVCTL_BRLEN_SHIFT 16
102#define UDC_DEVCTL_CSR_DONE (1 << 13)
103#define UDC_DEVCTL_SD (1 << 10)
104#define UDC_DEVCTL_MODE (1 << 9)
105#define UDC_DEVCTL_BREN (1 << 8)
106#define UDC_DEVCTL_THE (1 << 7)
107#define UDC_DEVCTL_DU (1 << 4)
108#define UDC_DEVCTL_TDE (1 << 3)
109#define UDC_DEVCTL_RDE (1 << 2)
110#define UDC_DEVCTL_RES (1 << 0)
111
112/* Device status register */
113/* Bit position */
114#define UDC_DEVSTS_TS_SHIFT 18
115#define UDC_DEVSTS_ENUM_SPEED_SHIFT 13
116#define UDC_DEVSTS_ALT_SHIFT 8
117#define UDC_DEVSTS_INTF_SHIFT 4
118#define UDC_DEVSTS_CFG_SHIFT 0
119/* Mask patern */
120#define UDC_DEVSTS_TS_MASK 0xfffc0000
121#define UDC_DEVSTS_ENUM_SPEED_MASK 0x00006000
122#define UDC_DEVSTS_ALT_MASK 0x00000f00
123#define UDC_DEVSTS_INTF_MASK 0x000000f0
124#define UDC_DEVSTS_CFG_MASK 0x0000000f
125/* value for maximum speed for SPEED field */
126#define UDC_DEVSTS_ENUM_SPEED_FULL 1
127#define UDC_DEVSTS_ENUM_SPEED_HIGH 0
128#define UDC_DEVSTS_ENUM_SPEED_LOW 2
129#define UDC_DEVSTS_ENUM_SPEED_FULLX 3
130
131/* Device irq register */
132/* Bit position */
133#define UDC_DEVINT_RWKP (1 << 7)
134#define UDC_DEVINT_ENUM (1 << 6)
135#define UDC_DEVINT_SOF (1 << 5)
136#define UDC_DEVINT_US (1 << 4)
137#define UDC_DEVINT_UR (1 << 3)
138#define UDC_DEVINT_ES (1 << 2)
139#define UDC_DEVINT_SI (1 << 1)
140#define UDC_DEVINT_SC (1 << 0)
141/* Mask patern */
142#define UDC_DEVINT_MSK 0x7f
143
144/* Endpoint irq register */
145/* Bit position */
146#define UDC_EPINT_IN_SHIFT 0
147#define UDC_EPINT_OUT_SHIFT 16
148#define UDC_EPINT_IN_EP0 (1 << 0)
149#define UDC_EPINT_OUT_EP0 (1 << 16)
150/* Mask patern */
151#define UDC_EPINT_MSK_DISABLE_ALL 0xffffffff
152
153/* UDC_CSR_BUSY Status register */
154/* Bit position */
155#define UDC_CSR_BUSY (1 << 0)
156
157/* SOFT RESET register */
158/* Bit position */
159#define UDC_PSRST (1 << 1)
160#define UDC_SRST (1 << 0)
161
162/* USB_DEVICE endpoint register */
163/* Bit position */
164#define UDC_CSR_NE_NUM_SHIFT 0
165#define UDC_CSR_NE_DIR_SHIFT 4
166#define UDC_CSR_NE_TYPE_SHIFT 5
167#define UDC_CSR_NE_CFG_SHIFT 7
168#define UDC_CSR_NE_INTF_SHIFT 11
169#define UDC_CSR_NE_ALT_SHIFT 15
170#define UDC_CSR_NE_MAX_PKT_SHIFT 19
171/* Mask patern */
172#define UDC_CSR_NE_NUM_MASK 0x0000000f
173#define UDC_CSR_NE_DIR_MASK 0x00000010
174#define UDC_CSR_NE_TYPE_MASK 0x00000060
175#define UDC_CSR_NE_CFG_MASK 0x00000780
176#define UDC_CSR_NE_INTF_MASK 0x00007800
177#define UDC_CSR_NE_ALT_MASK 0x00078000
178#define UDC_CSR_NE_MAX_PKT_MASK 0x3ff80000
179
180#define PCH_UDC_CSR(ep) (UDC_CSR_ADDR + ep*4)
181#define PCH_UDC_EPINT(in, num)\
182 (1 << (num + (in ? UDC_EPINT_IN_SHIFT : UDC_EPINT_OUT_SHIFT)))
183
184/* Index of endpoint */
185#define UDC_EP0IN_IDX 0
186#define UDC_EP0OUT_IDX 1
187#define UDC_EPIN_IDX(ep) (ep * 2)
188#define UDC_EPOUT_IDX(ep) (ep * 2 + 1)
189#define PCH_UDC_EP0 0
190#define PCH_UDC_EP1 1
191#define PCH_UDC_EP2 2
192#define PCH_UDC_EP3 3
193
194/* Number of endpoint */
195#define PCH_UDC_EP_NUM 32 /* Total number of EPs (16 IN,16 OUT) */
196#define PCH_UDC_USED_EP_NUM 4 /* EP number of EP's really used */
197/* Length Value */
198#define PCH_UDC_BRLEN 0x0F /* Burst length */
199#define PCH_UDC_THLEN 0x1F /* Threshold length */
200/* Value of EP Buffer Size */
201#define UDC_EP0IN_BUFF_SIZE 16
202#define UDC_EPIN_BUFF_SIZE 256
203#define UDC_EP0OUT_BUFF_SIZE 16
204#define UDC_EPOUT_BUFF_SIZE 256
205/* Value of EP maximum packet size */
206#define UDC_EP0IN_MAX_PKT_SIZE 64
207#define UDC_EP0OUT_MAX_PKT_SIZE 64
208#define UDC_BULK_MAX_PKT_SIZE 512
209
210/* DMA */
211#define DMA_DIR_RX 1 /* DMA for data receive */
212#define DMA_DIR_TX 2 /* DMA for data transmit */
213#define DMA_ADDR_INVALID (~(dma_addr_t)0)
214#define UDC_DMA_MAXPACKET 65536 /* maximum packet size for DMA */
215
216/**
217 * struct pch_udc_data_dma_desc - Structure to hold DMA descriptor information
218 * for data
219 * @status: Status quadlet
220 * @reserved: Reserved
221 * @dataptr: Buffer descriptor
222 * @next: Next descriptor
223 */
224struct pch_udc_data_dma_desc {
225 u32 status;
226 u32 reserved;
227 u32 dataptr;
228 u32 next;
229};
230
231/**
232 * struct pch_udc_stp_dma_desc - Structure to hold DMA descriptor information
233 * for control data
234 * @status: Status
235 * @reserved: Reserved
236 * @data12: First setup word
237 * @data34: Second setup word
238 */
239struct pch_udc_stp_dma_desc {
240 u32 status;
241 u32 reserved;
242 struct usb_ctrlrequest request;
243} __attribute((packed));
244
245/* DMA status definitions */
246/* Buffer status */
247#define PCH_UDC_BUFF_STS 0xC0000000
248#define PCH_UDC_BS_HST_RDY 0x00000000
249#define PCH_UDC_BS_DMA_BSY 0x40000000
250#define PCH_UDC_BS_DMA_DONE 0x80000000
251#define PCH_UDC_BS_HST_BSY 0xC0000000
252/* Rx/Tx Status */
253#define PCH_UDC_RXTX_STS 0x30000000
254#define PCH_UDC_RTS_SUCC 0x00000000
255#define PCH_UDC_RTS_DESERR 0x10000000
256#define PCH_UDC_RTS_BUFERR 0x30000000
257/* Last Descriptor Indication */
258#define PCH_UDC_DMA_LAST 0x08000000
259/* Number of Rx/Tx Bytes Mask */
260#define PCH_UDC_RXTX_BYTES 0x0000ffff
261
262/**
263 * struct pch_udc_cfg_data - Structure to hold current configuration
264 * and interface information
265 * @cur_cfg: current configuration in use
266 * @cur_intf: current interface in use
267 * @cur_alt: current alt interface in use
268 */
269struct pch_udc_cfg_data {
270 u16 cur_cfg;
271 u16 cur_intf;
272 u16 cur_alt;
273};
274
275/**
276 * struct pch_udc_ep - Structure holding a PCH USB device Endpoint information
277 * @ep: embedded ep request
278 * @td_stp_phys: for setup request
279 * @td_data_phys: for data request
280 * @td_stp: for setup request
281 * @td_data: for data request
282 * @dev: reference to device struct
283 * @offset_addr: offset address of ep register
284 * @desc: for this ep
285 * @queue: queue for requests
286 * @num: endpoint number
287 * @in: endpoint is IN
288 * @halted: endpoint halted?
289 * @epsts: Endpoint status
290 */
291struct pch_udc_ep {
292 struct usb_ep ep;
293 dma_addr_t td_stp_phys;
294 dma_addr_t td_data_phys;
295 struct pch_udc_stp_dma_desc *td_stp;
296 struct pch_udc_data_dma_desc *td_data;
297 struct pch_udc_dev *dev;
298 unsigned long offset_addr;
299 const struct usb_endpoint_descriptor *desc;
300 struct list_head queue;
301 unsigned num:5,
302 in:1,
303 halted:1;
304 unsigned long epsts;
305};
306
307/**
308 * struct pch_udc_dev - Structure holding complete information
309 * of the PCH USB device
310 * @gadget: gadget driver data
311 * @driver: reference to gadget driver bound
312 * @pdev: reference to the PCI device
313 * @ep: array of endpoints
314 * @lock: protects all state
315 * @active: enabled the PCI device
316 * @stall: stall requested
317 * @prot_stall: protcol stall requested
318 * @irq_registered: irq registered with system
319 * @mem_region: device memory mapped
320 * @registered: driver regsitered with system
321 * @suspended: driver in suspended state
322 * @connected: gadget driver associated
323 * @set_cfg_not_acked: pending acknowledgement 4 setup
324 * @waiting_zlp_ack: pending acknowledgement 4 ZLP
325 * @data_requests: DMA pool for data requests
326 * @stp_requests: DMA pool for setup requests
327 * @dma_addr: DMA pool for received
328 * @ep0out_buf: Buffer for DMA
329 * @setup_data: Received setup data
330 * @phys_addr: of device memory
331 * @base_addr: for mapped device memory
332 * @irq: IRQ line for the device
333 * @cfg_data: current cfg, intf, and alt in use
334 */
335struct pch_udc_dev {
336 struct usb_gadget gadget;
337 struct usb_gadget_driver *driver;
338 struct pci_dev *pdev;
339 struct pch_udc_ep ep[PCH_UDC_EP_NUM];
340 spinlock_t lock; /* protects all state */
341 unsigned active:1,
342 stall:1,
343 prot_stall:1,
344 irq_registered:1,
345 mem_region:1,
346 registered:1,
347 suspended:1,
348 connected:1,
349 set_cfg_not_acked:1,
350 waiting_zlp_ack:1;
351 struct pci_pool *data_requests;
352 struct pci_pool *stp_requests;
353 dma_addr_t dma_addr;
354 void *ep0out_buf;
355 struct usb_ctrlrequest setup_data;
356 unsigned long phys_addr;
357 void __iomem *base_addr;
358 unsigned irq;
359 struct pch_udc_cfg_data cfg_data;
360};
361
362#define PCH_UDC_PCI_BAR 1
363#define PCI_DEVICE_ID_INTEL_EG20T_UDC 0x8808
364#define PCI_VENDOR_ID_ROHM 0x10DB
365#define PCI_DEVICE_ID_ML7213_IOH_UDC 0x801D
366
367static const char ep0_string[] = "ep0in";
368static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */
369struct pch_udc_dev *pch_udc; /* pointer to device object */
370static int speed_fs;
371module_param_named(speed_fs, speed_fs, bool, S_IRUGO);
372MODULE_PARM_DESC(speed_fs, "true for Full speed operation");
373
374/**
375 * struct pch_udc_request - Structure holding a PCH USB device request packet
376 * @req: embedded ep request
377 * @td_data_phys: phys. address
378 * @td_data: first dma desc. of chain
379 * @td_data_last: last dma desc. of chain
380 * @queue: associated queue
381 * @dma_going: DMA in progress for request
382 * @dma_mapped: DMA memory mapped for request
383 * @dma_done: DMA completed for request
384 * @chain_len: chain length
385 * @buf: Buffer memory for align adjustment
386 * @dma: DMA memory for align adjustment
387 */
388struct pch_udc_request {
389 struct usb_request req;
390 dma_addr_t td_data_phys;
391 struct pch_udc_data_dma_desc *td_data;
392 struct pch_udc_data_dma_desc *td_data_last;
393 struct list_head queue;
394 unsigned dma_going:1,
395 dma_mapped:1,
396 dma_done:1;
397 unsigned chain_len;
398 void *buf;
399 dma_addr_t dma;
400};
401
402static inline u32 pch_udc_readl(struct pch_udc_dev *dev, unsigned long reg)
403{
404 return ioread32(dev->base_addr + reg);
405}
406
407static inline void pch_udc_writel(struct pch_udc_dev *dev,
408 unsigned long val, unsigned long reg)
409{
410 iowrite32(val, dev->base_addr + reg);
411}
412
413static inline void pch_udc_bit_set(struct pch_udc_dev *dev,
414 unsigned long reg,
415 unsigned long bitmask)
416{
417 pch_udc_writel(dev, pch_udc_readl(dev, reg) | bitmask, reg);
418}
419
420static inline void pch_udc_bit_clr(struct pch_udc_dev *dev,
421 unsigned long reg,
422 unsigned long bitmask)
423{
424 pch_udc_writel(dev, pch_udc_readl(dev, reg) & ~(bitmask), reg);
425}
426
427static inline u32 pch_udc_ep_readl(struct pch_udc_ep *ep, unsigned long reg)
428{
429 return ioread32(ep->dev->base_addr + ep->offset_addr + reg);
430}
431
432static inline void pch_udc_ep_writel(struct pch_udc_ep *ep,
433 unsigned long val, unsigned long reg)
434{
435 iowrite32(val, ep->dev->base_addr + ep->offset_addr + reg);
436}
437
438static inline void pch_udc_ep_bit_set(struct pch_udc_ep *ep,
439 unsigned long reg,
440 unsigned long bitmask)
441{
442 pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) | bitmask, reg);
443}
444
445static inline void pch_udc_ep_bit_clr(struct pch_udc_ep *ep,
446 unsigned long reg,
447 unsigned long bitmask)
448{
449 pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) & ~(bitmask), reg);
450}
451
452/**
453 * pch_udc_csr_busy() - Wait till idle.
454 * @dev: Reference to pch_udc_dev structure
455 */
456static void pch_udc_csr_busy(struct pch_udc_dev *dev)
457{
458 unsigned int count = 200;
459
460 /* Wait till idle */
461 while ((pch_udc_readl(dev, UDC_CSR_BUSY_ADDR) & UDC_CSR_BUSY)
462 && --count)
463 cpu_relax();
464 if (!count)
465 dev_err(&dev->pdev->dev, "%s: wait error\n", __func__);
466}
467
468/**
469 * pch_udc_write_csr() - Write the command and status registers.
470 * @dev: Reference to pch_udc_dev structure
471 * @val: value to be written to CSR register
472 * @addr: address of CSR register
473 */
474static void pch_udc_write_csr(struct pch_udc_dev *dev, unsigned long val,
475 unsigned int ep)
476{
477 unsigned long reg = PCH_UDC_CSR(ep);
478
479 pch_udc_csr_busy(dev); /* Wait till idle */
480 pch_udc_writel(dev, val, reg);
481 pch_udc_csr_busy(dev); /* Wait till idle */
482}
483
484/**
485 * pch_udc_read_csr() - Read the command and status registers.
486 * @dev: Reference to pch_udc_dev structure
487 * @addr: address of CSR register
488 *
489 * Return codes: content of CSR register
490 */
491static u32 pch_udc_read_csr(struct pch_udc_dev *dev, unsigned int ep)
492{
493 unsigned long reg = PCH_UDC_CSR(ep);
494
495 pch_udc_csr_busy(dev); /* Wait till idle */
496 pch_udc_readl(dev, reg); /* Dummy read */
497 pch_udc_csr_busy(dev); /* Wait till idle */
498 return pch_udc_readl(dev, reg);
499}
500
501/**
502 * pch_udc_rmt_wakeup() - Initiate for remote wakeup
503 * @dev: Reference to pch_udc_dev structure
504 */
505static inline void pch_udc_rmt_wakeup(struct pch_udc_dev *dev)
506{
507 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
508 mdelay(1);
509 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
510}
511
512/**
513 * pch_udc_get_frame() - Get the current frame from device status register
514 * @dev: Reference to pch_udc_dev structure
515 * Retern current frame
516 */
517static inline int pch_udc_get_frame(struct pch_udc_dev *dev)
518{
519 u32 frame = pch_udc_readl(dev, UDC_DEVSTS_ADDR);
520 return (frame & UDC_DEVSTS_TS_MASK) >> UDC_DEVSTS_TS_SHIFT;
521}
522
523/**
524 * pch_udc_clear_selfpowered() - Clear the self power control
525 * @dev: Reference to pch_udc_regs structure
526 */
527static inline void pch_udc_clear_selfpowered(struct pch_udc_dev *dev)
528{
529 pch_udc_bit_clr(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
530}
531
532/**
533 * pch_udc_set_selfpowered() - Set the self power control
534 * @dev: Reference to pch_udc_regs structure
535 */
536static inline void pch_udc_set_selfpowered(struct pch_udc_dev *dev)
537{
538 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
539}
540
541/**
542 * pch_udc_set_disconnect() - Set the disconnect status.
543 * @dev: Reference to pch_udc_regs structure
544 */
545static inline void pch_udc_set_disconnect(struct pch_udc_dev *dev)
546{
547 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
548}
549
550/**
551 * pch_udc_clear_disconnect() - Clear the disconnect status.
552 * @dev: Reference to pch_udc_regs structure
553 */
554static void pch_udc_clear_disconnect(struct pch_udc_dev *dev)
555{
556 /* Clear the disconnect */
557 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
558 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
559 mdelay(1);
560 /* Resume USB signalling */
561 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
562}
563
564/**
565 * pch_udc_vbus_session() - set or clearr the disconnect status.
566 * @dev: Reference to pch_udc_regs structure
567 * @is_active: Parameter specifying the action
568 * 0: indicating VBUS power is ending
569 * !0: indicating VBUS power is starting
570 */
571static inline void pch_udc_vbus_session(struct pch_udc_dev *dev,
572 int is_active)
573{
574 if (is_active)
575 pch_udc_clear_disconnect(dev);
576 else
577 pch_udc_set_disconnect(dev);
578}
579
580/**
581 * pch_udc_ep_set_stall() - Set the stall of endpoint
582 * @ep: Reference to structure of type pch_udc_ep_regs
583 */
584static void pch_udc_ep_set_stall(struct pch_udc_ep *ep)
585{
586 if (ep->in) {
587 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
588 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
589 } else {
590 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
591 }
592}
593
594/**
595 * pch_udc_ep_clear_stall() - Clear the stall of endpoint
596 * @ep: Reference to structure of type pch_udc_ep_regs
597 */
598static inline void pch_udc_ep_clear_stall(struct pch_udc_ep *ep)
599{
600 /* Clear the stall */
601 pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
602 /* Clear NAK by writing CNAK */
603 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
604}
605
606/**
607 * pch_udc_ep_set_trfr_type() - Set the transfer type of endpoint
608 * @ep: Reference to structure of type pch_udc_ep_regs
609 * @type: Type of endpoint
610 */
611static inline void pch_udc_ep_set_trfr_type(struct pch_udc_ep *ep,
612 u8 type)
613{
614 pch_udc_ep_writel(ep, ((type << UDC_EPCTL_ET_SHIFT) &
615 UDC_EPCTL_ET_MASK), UDC_EPCTL_ADDR);
616}
617
618/**
619 * pch_udc_ep_set_bufsz() - Set the maximum packet size for the endpoint
620 * @ep: Reference to structure of type pch_udc_ep_regs
621 * @buf_size: The buffer word size
622 */
623static void pch_udc_ep_set_bufsz(struct pch_udc_ep *ep,
624 u32 buf_size, u32 ep_in)
625{
626 u32 data;
627 if (ep_in) {
628 data = pch_udc_ep_readl(ep, UDC_BUFIN_FRAMENUM_ADDR);
629 data = (data & 0xffff0000) | (buf_size & 0xffff);
630 pch_udc_ep_writel(ep, data, UDC_BUFIN_FRAMENUM_ADDR);
631 } else {
632 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
633 data = (buf_size << 16) | (data & 0xffff);
634 pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
635 }
636}
637
638/**
639 * pch_udc_ep_set_maxpkt() - Set the Max packet size for the endpoint
640 * @ep: Reference to structure of type pch_udc_ep_regs
641 * @pkt_size: The packet byte size
642 */
643static void pch_udc_ep_set_maxpkt(struct pch_udc_ep *ep, u32 pkt_size)
644{
645 u32 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
646 data = (data & 0xffff0000) | (pkt_size & 0xffff);
647 pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
648}
649
650/**
651 * pch_udc_ep_set_subptr() - Set the Setup buffer pointer for the endpoint
652 * @ep: Reference to structure of type pch_udc_ep_regs
653 * @addr: Address of the register
654 */
655static inline void pch_udc_ep_set_subptr(struct pch_udc_ep *ep, u32 addr)
656{
657 pch_udc_ep_writel(ep, addr, UDC_SUBPTR_ADDR);
658}
659
660/**
661 * pch_udc_ep_set_ddptr() - Set the Data descriptor pointer for the endpoint
662 * @ep: Reference to structure of type pch_udc_ep_regs
663 * @addr: Address of the register
664 */
665static inline void pch_udc_ep_set_ddptr(struct pch_udc_ep *ep, u32 addr)
666{
667 pch_udc_ep_writel(ep, addr, UDC_DESPTR_ADDR);
668}
669
670/**
671 * pch_udc_ep_set_pd() - Set the poll demand bit for the endpoint
672 * @ep: Reference to structure of type pch_udc_ep_regs
673 */
674static inline void pch_udc_ep_set_pd(struct pch_udc_ep *ep)
675{
676 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_P);
677}
678
679/**
680 * pch_udc_ep_set_rrdy() - Set the receive ready bit for the endpoint
681 * @ep: Reference to structure of type pch_udc_ep_regs
682 */
683static inline void pch_udc_ep_set_rrdy(struct pch_udc_ep *ep)
684{
685 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
686}
687
688/**
689 * pch_udc_ep_clear_rrdy() - Clear the receive ready bit for the endpoint
690 * @ep: Reference to structure of type pch_udc_ep_regs
691 */
692static inline void pch_udc_ep_clear_rrdy(struct pch_udc_ep *ep)
693{
694 pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
695}
696
697/**
698 * pch_udc_set_dma() - Set the 'TDE' or RDE bit of device control
699 * register depending on the direction specified
700 * @dev: Reference to structure of type pch_udc_regs
701 * @dir: whether Tx or Rx
702 * DMA_DIR_RX: Receive
703 * DMA_DIR_TX: Transmit
704 */
705static inline void pch_udc_set_dma(struct pch_udc_dev *dev, int dir)
706{
707 if (dir == DMA_DIR_RX)
708 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
709 else if (dir == DMA_DIR_TX)
710 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
711}
712
713/**
714 * pch_udc_clear_dma() - Clear the 'TDE' or RDE bit of device control
715 * register depending on the direction specified
716 * @dev: Reference to structure of type pch_udc_regs
717 * @dir: Whether Tx or Rx
718 * DMA_DIR_RX: Receive
719 * DMA_DIR_TX: Transmit
720 */
721static inline void pch_udc_clear_dma(struct pch_udc_dev *dev, int dir)
722{
723 if (dir == DMA_DIR_RX)
724 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
725 else if (dir == DMA_DIR_TX)
726 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
727}
728
729/**
730 * pch_udc_set_csr_done() - Set the device control register
731 * CSR done field (bit 13)
732 * @dev: reference to structure of type pch_udc_regs
733 */
734static inline void pch_udc_set_csr_done(struct pch_udc_dev *dev)
735{
736 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_CSR_DONE);
737}
738
739/**
740 * pch_udc_disable_interrupts() - Disables the specified interrupts
741 * @dev: Reference to structure of type pch_udc_regs
742 * @mask: Mask to disable interrupts
743 */
744static inline void pch_udc_disable_interrupts(struct pch_udc_dev *dev,
745 u32 mask)
746{
747 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, mask);
748}
749
750/**
751 * pch_udc_enable_interrupts() - Enable the specified interrupts
752 * @dev: Reference to structure of type pch_udc_regs
753 * @mask: Mask to enable interrupts
754 */
755static inline void pch_udc_enable_interrupts(struct pch_udc_dev *dev,
756 u32 mask)
757{
758 pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR, mask);
759}
760
761/**
762 * pch_udc_disable_ep_interrupts() - Disable endpoint interrupts
763 * @dev: Reference to structure of type pch_udc_regs
764 * @mask: Mask to disable interrupts
765 */
766static inline void pch_udc_disable_ep_interrupts(struct pch_udc_dev *dev,
767 u32 mask)
768{
769 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, mask);
770}
771
772/**
773 * pch_udc_enable_ep_interrupts() - Enable endpoint interrupts
774 * @dev: Reference to structure of type pch_udc_regs
775 * @mask: Mask to enable interrupts
776 */
777static inline void pch_udc_enable_ep_interrupts(struct pch_udc_dev *dev,
778 u32 mask)
779{
780 pch_udc_bit_clr(dev, UDC_EPIRQMSK_ADDR, mask);
781}
782
783/**
784 * pch_udc_read_device_interrupts() - Read the device interrupts
785 * @dev: Reference to structure of type pch_udc_regs
786 * Retern The device interrupts
787 */
788static inline u32 pch_udc_read_device_interrupts(struct pch_udc_dev *dev)
789{
790 return pch_udc_readl(dev, UDC_DEVIRQSTS_ADDR);
791}
792
793/**
794 * pch_udc_write_device_interrupts() - Write device interrupts
795 * @dev: Reference to structure of type pch_udc_regs
796 * @val: The value to be written to interrupt register
797 */
798static inline void pch_udc_write_device_interrupts(struct pch_udc_dev *dev,
799 u32 val)
800{
801 pch_udc_writel(dev, val, UDC_DEVIRQSTS_ADDR);
802}
803
804/**
805 * pch_udc_read_ep_interrupts() - Read the endpoint interrupts
806 * @dev: Reference to structure of type pch_udc_regs
807 * Retern The endpoint interrupt
808 */
809static inline u32 pch_udc_read_ep_interrupts(struct pch_udc_dev *dev)
810{
811 return pch_udc_readl(dev, UDC_EPIRQSTS_ADDR);
812}
813
814/**
815 * pch_udc_write_ep_interrupts() - Clear endpoint interupts
816 * @dev: Reference to structure of type pch_udc_regs
817 * @val: The value to be written to interrupt register
818 */
819static inline void pch_udc_write_ep_interrupts(struct pch_udc_dev *dev,
820 u32 val)
821{
822 pch_udc_writel(dev, val, UDC_EPIRQSTS_ADDR);
823}
824
825/**
826 * pch_udc_read_device_status() - Read the device status
827 * @dev: Reference to structure of type pch_udc_regs
828 * Retern The device status
829 */
830static inline u32 pch_udc_read_device_status(struct pch_udc_dev *dev)
831{
832 return pch_udc_readl(dev, UDC_DEVSTS_ADDR);
833}
834
835/**
836 * pch_udc_read_ep_control() - Read the endpoint control
837 * @ep: Reference to structure of type pch_udc_ep_regs
838 * Retern The endpoint control register value
839 */
840static inline u32 pch_udc_read_ep_control(struct pch_udc_ep *ep)
841{
842 return pch_udc_ep_readl(ep, UDC_EPCTL_ADDR);
843}
844
845/**
846 * pch_udc_clear_ep_control() - Clear the endpoint control register
847 * @ep: Reference to structure of type pch_udc_ep_regs
848 * Retern The endpoint control register value
849 */
850static inline void pch_udc_clear_ep_control(struct pch_udc_ep *ep)
851{
852 return pch_udc_ep_writel(ep, 0, UDC_EPCTL_ADDR);
853}
854
855/**
856 * pch_udc_read_ep_status() - Read the endpoint status
857 * @ep: Reference to structure of type pch_udc_ep_regs
858 * Retern The endpoint status
859 */
860static inline u32 pch_udc_read_ep_status(struct pch_udc_ep *ep)
861{
862 return pch_udc_ep_readl(ep, UDC_EPSTS_ADDR);
863}
864
865/**
866 * pch_udc_clear_ep_status() - Clear the endpoint status
867 * @ep: Reference to structure of type pch_udc_ep_regs
868 * @stat: Endpoint status
869 */
870static inline void pch_udc_clear_ep_status(struct pch_udc_ep *ep,
871 u32 stat)
872{
873 return pch_udc_ep_writel(ep, stat, UDC_EPSTS_ADDR);
874}
875
876/**
877 * pch_udc_ep_set_nak() - Set the bit 7 (SNAK field)
878 * of the endpoint control register
879 * @ep: Reference to structure of type pch_udc_ep_regs
880 */
881static inline void pch_udc_ep_set_nak(struct pch_udc_ep *ep)
882{
883 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_SNAK);
884}
885
886/**
887 * pch_udc_ep_clear_nak() - Set the bit 8 (CNAK field)
888 * of the endpoint control register
889 * @ep: reference to structure of type pch_udc_ep_regs
890 */
891static void pch_udc_ep_clear_nak(struct pch_udc_ep *ep)
892{
893 unsigned int loopcnt = 0;
894 struct pch_udc_dev *dev = ep->dev;
895
896 if (!(pch_udc_ep_readl(ep, UDC_EPCTL_ADDR) & UDC_EPCTL_NAK))
897 return;
898 if (!ep->in) {
899 loopcnt = 10000;
900 while (!(pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP) &&
901 --loopcnt)
902 udelay(5);
903 if (!loopcnt)
904 dev_err(&dev->pdev->dev, "%s: RxFIFO not Empty\n",
905 __func__);
906 }
907 loopcnt = 10000;
908 while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_NAK) && --loopcnt) {
909 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
910 udelay(5);
911 }
912 if (!loopcnt)
913 dev_err(&dev->pdev->dev, "%s: Clear NAK not set for ep%d%s\n",
914 __func__, ep->num, (ep->in ? "in" : "out"));
915}
916
917/**
918 * pch_udc_ep_fifo_flush() - Flush the endpoint fifo
919 * @ep: reference to structure of type pch_udc_ep_regs
920 * @dir: direction of endpoint
921 * 0: endpoint is OUT
922 * !0: endpoint is IN
923 */
924static void pch_udc_ep_fifo_flush(struct pch_udc_ep *ep, int dir)
925{
926 if (dir) { /* IN ep */
927 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
928 return;
929 }
930}
931
932/**
933 * pch_udc_ep_enable() - This api enables endpoint
934 * @regs: Reference to structure pch_udc_ep_regs
935 * @desc: endpoint descriptor
936 */
937static void pch_udc_ep_enable(struct pch_udc_ep *ep,
938 struct pch_udc_cfg_data *cfg,
939 const struct usb_endpoint_descriptor *desc)
940{
941 u32 val = 0;
942 u32 buff_size = 0;
943
944 pch_udc_ep_set_trfr_type(ep, desc->bmAttributes);
945 if (ep->in)
946 buff_size = UDC_EPIN_BUFF_SIZE;
947 else
948 buff_size = UDC_EPOUT_BUFF_SIZE;
949 pch_udc_ep_set_bufsz(ep, buff_size, ep->in);
950 pch_udc_ep_set_maxpkt(ep, le16_to_cpu(desc->wMaxPacketSize));
951 pch_udc_ep_set_nak(ep);
952 pch_udc_ep_fifo_flush(ep, ep->in);
953 /* Configure the endpoint */
954 val = ep->num << UDC_CSR_NE_NUM_SHIFT | ep->in << UDC_CSR_NE_DIR_SHIFT |
955 ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) <<
956 UDC_CSR_NE_TYPE_SHIFT) |
957 (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) |
958 (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) |
959 (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) |
960 le16_to_cpu(desc->wMaxPacketSize) << UDC_CSR_NE_MAX_PKT_SHIFT;
961
962 if (ep->in)
963 pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num));
964 else
965 pch_udc_write_csr(ep->dev, val, UDC_EPOUT_IDX(ep->num));
966}
967
968/**
969 * pch_udc_ep_disable() - This api disables endpoint
970 * @regs: Reference to structure pch_udc_ep_regs
971 */
972static void pch_udc_ep_disable(struct pch_udc_ep *ep)
973{
974 if (ep->in) {
975 /* flush the fifo */
976 pch_udc_ep_writel(ep, UDC_EPCTL_F, UDC_EPCTL_ADDR);
977 /* set NAK */
978 pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
979 pch_udc_ep_bit_set(ep, UDC_EPSTS_ADDR, UDC_EPSTS_IN);
980 } else {
981 /* set NAK */
982 pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
983 }
984 /* reset desc pointer */
985 pch_udc_ep_writel(ep, 0, UDC_DESPTR_ADDR);
986}
987
988/**
989 * pch_udc_wait_ep_stall() - Wait EP stall.
990 * @dev: Reference to pch_udc_dev structure
991 */
992static void pch_udc_wait_ep_stall(struct pch_udc_ep *ep)
993{
994 unsigned int count = 10000;
995
996 /* Wait till idle */
997 while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_S) && --count)
998 udelay(5);
999 if (!count)
1000 dev_err(&ep->dev->pdev->dev, "%s: wait error\n", __func__);
1001}
1002
1003/**
1004 * pch_udc_init() - This API initializes usb device controller
1005 * @dev: Rreference to pch_udc_regs structure
1006 */
1007static void pch_udc_init(struct pch_udc_dev *dev)
1008{
1009 if (NULL == dev) {
1010 pr_err("%s: Invalid address\n", __func__);
1011 return;
1012 }
1013 /* Soft Reset and Reset PHY */
1014 pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
1015 pch_udc_writel(dev, UDC_SRST | UDC_PSRST, UDC_SRST_ADDR);
1016 mdelay(1);
1017 pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
1018 pch_udc_writel(dev, 0x00, UDC_SRST_ADDR);
1019 mdelay(1);
1020 /* mask and clear all device interrupts */
1021 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
1022 pch_udc_bit_set(dev, UDC_DEVIRQSTS_ADDR, UDC_DEVINT_MSK);
1023
1024 /* mask and clear all ep interrupts */
1025 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1026 pch_udc_bit_set(dev, UDC_EPIRQSTS_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1027
1028 /* enable dynamic CSR programmingi, self powered and device speed */
1029 if (speed_fs)
1030 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
1031 UDC_DEVCFG_SP | UDC_DEVCFG_SPD_FS);
1032 else /* defaul high speed */
1033 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
1034 UDC_DEVCFG_SP | UDC_DEVCFG_SPD_HS);
1035 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR,
1036 (PCH_UDC_THLEN << UDC_DEVCTL_THLEN_SHIFT) |
1037 (PCH_UDC_BRLEN << UDC_DEVCTL_BRLEN_SHIFT) |
1038 UDC_DEVCTL_MODE | UDC_DEVCTL_BREN |
1039 UDC_DEVCTL_THE);
1040}
1041
1042/**
1043 * pch_udc_exit() - This API exit usb device controller
1044 * @dev: Reference to pch_udc_regs structure
1045 */
1046static void pch_udc_exit(struct pch_udc_dev *dev)
1047{
1048 /* mask all device interrupts */
1049 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
1050 /* mask all ep interrupts */
1051 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1052 /* put device in disconnected state */
1053 pch_udc_set_disconnect(dev);
1054}
1055
1056/**
1057 * pch_udc_pcd_get_frame() - This API is invoked to get the current frame number
1058 * @gadget: Reference to the gadget driver
1059 *
1060 * Return codes:
1061 * 0: Success
1062 * -EINVAL: If the gadget passed is NULL
1063 */
1064static int pch_udc_pcd_get_frame(struct usb_gadget *gadget)
1065{
1066 struct pch_udc_dev *dev;
1067
1068 if (!gadget)
1069 return -EINVAL;
1070 dev = container_of(gadget, struct pch_udc_dev, gadget);
1071 return pch_udc_get_frame(dev);
1072}
1073
1074/**
1075 * pch_udc_pcd_wakeup() - This API is invoked to initiate a remote wakeup
1076 * @gadget: Reference to the gadget driver
1077 *
1078 * Return codes:
1079 * 0: Success
1080 * -EINVAL: If the gadget passed is NULL
1081 */
1082static int pch_udc_pcd_wakeup(struct usb_gadget *gadget)
1083{
1084 struct pch_udc_dev *dev;
1085 unsigned long flags;
1086
1087 if (!gadget)
1088 return -EINVAL;
1089 dev = container_of(gadget, struct pch_udc_dev, gadget);
1090 spin_lock_irqsave(&dev->lock, flags);
1091 pch_udc_rmt_wakeup(dev);
1092 spin_unlock_irqrestore(&dev->lock, flags);
1093 return 0;
1094}
1095
1096/**
1097 * pch_udc_pcd_selfpowered() - This API is invoked to specify whether the device
1098 * is self powered or not
1099 * @gadget: Reference to the gadget driver
1100 * @value: Specifies self powered or not
1101 *
1102 * Return codes:
1103 * 0: Success
1104 * -EINVAL: If the gadget passed is NULL
1105 */
1106static int pch_udc_pcd_selfpowered(struct usb_gadget *gadget, int value)
1107{
1108 struct pch_udc_dev *dev;
1109
1110 if (!gadget)
1111 return -EINVAL;
1112 dev = container_of(gadget, struct pch_udc_dev, gadget);
1113 if (value)
1114 pch_udc_set_selfpowered(dev);
1115 else
1116 pch_udc_clear_selfpowered(dev);
1117 return 0;
1118}
1119
1120/**
1121 * pch_udc_pcd_pullup() - This API is invoked to make the device
1122 * visible/invisible to the host
1123 * @gadget: Reference to the gadget driver
1124 * @is_on: Specifies whether the pull up is made active or inactive
1125 *
1126 * Return codes:
1127 * 0: Success
1128 * -EINVAL: If the gadget passed is NULL
1129 */
1130static int pch_udc_pcd_pullup(struct usb_gadget *gadget, int is_on)
1131{
1132 struct pch_udc_dev *dev;
1133
1134 if (!gadget)
1135 return -EINVAL;
1136 dev = container_of(gadget, struct pch_udc_dev, gadget);
1137 pch_udc_vbus_session(dev, is_on);
1138 return 0;
1139}
1140
1141/**
1142 * pch_udc_pcd_vbus_session() - This API is used by a driver for an external
1143 * transceiver (or GPIO) that
1144 * detects a VBUS power session starting/ending
1145 * @gadget: Reference to the gadget driver
1146 * @is_active: specifies whether the session is starting or ending
1147 *
1148 * Return codes:
1149 * 0: Success
1150 * -EINVAL: If the gadget passed is NULL
1151 */
1152static int pch_udc_pcd_vbus_session(struct usb_gadget *gadget, int is_active)
1153{
1154 struct pch_udc_dev *dev;
1155
1156 if (!gadget)
1157 return -EINVAL;
1158 dev = container_of(gadget, struct pch_udc_dev, gadget);
1159 pch_udc_vbus_session(dev, is_active);
1160 return 0;
1161}
1162
1163/**
1164 * pch_udc_pcd_vbus_draw() - This API is used by gadget drivers during
1165 * SET_CONFIGURATION calls to
1166 * specify how much power the device can consume
1167 * @gadget: Reference to the gadget driver
1168 * @mA: specifies the current limit in 2mA unit
1169 *
1170 * Return codes:
1171 * -EINVAL: If the gadget passed is NULL
1172 * -EOPNOTSUPP:
1173 */
1174static int pch_udc_pcd_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
1175{
1176 return -EOPNOTSUPP;
1177}
1178
1179static const struct usb_gadget_ops pch_udc_ops = {
1180 .get_frame = pch_udc_pcd_get_frame,
1181 .wakeup = pch_udc_pcd_wakeup,
1182 .set_selfpowered = pch_udc_pcd_selfpowered,
1183 .pullup = pch_udc_pcd_pullup,
1184 .vbus_session = pch_udc_pcd_vbus_session,
1185 .vbus_draw = pch_udc_pcd_vbus_draw,
1186};
1187
1188/**
1189 * complete_req() - This API is invoked from the driver when processing
1190 * of a request is complete
1191 * @ep: Reference to the endpoint structure
1192 * @req: Reference to the request structure
1193 * @status: Indicates the success/failure of completion
1194 */
1195static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req,
1196 int status)
1197{
1198 struct pch_udc_dev *dev;
1199 unsigned halted = ep->halted;
1200
1201 list_del_init(&req->queue);
1202
1203 /* set new status if pending */
1204 if (req->req.status == -EINPROGRESS)
1205 req->req.status = status;
1206 else
1207 status = req->req.status;
1208
1209 dev = ep->dev;
1210 if (req->dma_mapped) {
1211 if (req->dma == DMA_ADDR_INVALID) {
1212 if (ep->in)
1213 dma_unmap_single(&dev->pdev->dev, req->req.dma,
1214 req->req.length,
1215 DMA_TO_DEVICE);
1216 else
1217 dma_unmap_single(&dev->pdev->dev, req->req.dma,
1218 req->req.length,
1219 DMA_FROM_DEVICE);
1220 req->req.dma = DMA_ADDR_INVALID;
1221 } else {
1222 if (ep->in)
1223 dma_unmap_single(&dev->pdev->dev, req->dma,
1224 req->req.length,
1225 DMA_TO_DEVICE);
1226 else {
1227 dma_unmap_single(&dev->pdev->dev, req->dma,
1228 req->req.length,
1229 DMA_FROM_DEVICE);
1230 memcpy(req->req.buf, req->buf, req->req.length);
1231 }
1232 kfree(req->buf);
1233 req->dma = DMA_ADDR_INVALID;
1234 }
1235 req->dma_mapped = 0;
1236 }
1237 ep->halted = 1;
1238 spin_unlock(&dev->lock);
1239 if (!ep->in)
1240 pch_udc_ep_clear_rrdy(ep);
1241 req->req.complete(&ep->ep, &req->req);
1242 spin_lock(&dev->lock);
1243 ep->halted = halted;
1244}
1245
1246/**
1247 * empty_req_queue() - This API empties the request queue of an endpoint
1248 * @ep: Reference to the endpoint structure
1249 */
1250static void empty_req_queue(struct pch_udc_ep *ep)
1251{
1252 struct pch_udc_request *req;
1253
1254 ep->halted = 1;
1255 while (!list_empty(&ep->queue)) {
1256 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1257 complete_req(ep, req, -ESHUTDOWN); /* Remove from list */
1258 }
1259}
1260
1261/**
1262 * pch_udc_free_dma_chain() - This function frees the DMA chain created
1263 * for the request
1264 * @dev Reference to the driver structure
1265 * @req Reference to the request to be freed
1266 *
1267 * Return codes:
1268 * 0: Success
1269 */
1270static void pch_udc_free_dma_chain(struct pch_udc_dev *dev,
1271 struct pch_udc_request *req)
1272{
1273 struct pch_udc_data_dma_desc *td = req->td_data;
1274 unsigned i = req->chain_len;
1275
1276 dma_addr_t addr2;
1277 dma_addr_t addr = (dma_addr_t)td->next;
1278 td->next = 0x00;
1279 for (; i > 1; --i) {
1280 /* do not free first desc., will be done by free for request */
1281 td = phys_to_virt(addr);
1282 addr2 = (dma_addr_t)td->next;
1283 pci_pool_free(dev->data_requests, td, addr);
1284 td->next = 0x00;
1285 addr = addr2;
1286 }
1287 req->chain_len = 1;
1288}
1289
1290/**
1291 * pch_udc_create_dma_chain() - This function creates or reinitializes
1292 * a DMA chain
1293 * @ep: Reference to the endpoint structure
1294 * @req: Reference to the request
1295 * @buf_len: The buffer length
1296 * @gfp_flags: Flags to be used while mapping the data buffer
1297 *
1298 * Return codes:
1299 * 0: success,
1300 * -ENOMEM: pci_pool_alloc invocation fails
1301 */
1302static int pch_udc_create_dma_chain(struct pch_udc_ep *ep,
1303 struct pch_udc_request *req,
1304 unsigned long buf_len,
1305 gfp_t gfp_flags)
1306{
1307 struct pch_udc_data_dma_desc *td = req->td_data, *last;
1308 unsigned long bytes = req->req.length, i = 0;
1309 dma_addr_t dma_addr;
1310 unsigned len = 1;
1311
1312 if (req->chain_len > 1)
1313 pch_udc_free_dma_chain(ep->dev, req);
1314
1315 if (req->dma == DMA_ADDR_INVALID)
1316 td->dataptr = req->req.dma;
1317 else
1318 td->dataptr = req->dma;
1319
1320 td->status = PCH_UDC_BS_HST_BSY;
1321 for (; ; bytes -= buf_len, ++len) {
1322 td->status = PCH_UDC_BS_HST_BSY | min(buf_len, bytes);
1323 if (bytes <= buf_len)
1324 break;
1325 last = td;
1326 td = pci_pool_alloc(ep->dev->data_requests, gfp_flags,
1327 &dma_addr);
1328 if (!td)
1329 goto nomem;
1330 i += buf_len;
1331 td->dataptr = req->td_data->dataptr + i;
1332 last->next = dma_addr;
1333 }
1334
1335 req->td_data_last = td;
1336 td->status |= PCH_UDC_DMA_LAST;
1337 td->next = req->td_data_phys;
1338 req->chain_len = len;
1339 return 0;
1340
1341nomem:
1342 if (len > 1) {
1343 req->chain_len = len;
1344 pch_udc_free_dma_chain(ep->dev, req);
1345 }
1346 req->chain_len = 1;
1347 return -ENOMEM;
1348}
1349
1350/**
1351 * prepare_dma() - This function creates and initializes the DMA chain
1352 * for the request
1353 * @ep: Reference to the endpoint structure
1354 * @req: Reference to the request
1355 * @gfp: Flag to be used while mapping the data buffer
1356 *
1357 * Return codes:
1358 * 0: Success
1359 * Other 0: linux error number on failure
1360 */
1361static int prepare_dma(struct pch_udc_ep *ep, struct pch_udc_request *req,
1362 gfp_t gfp)
1363{
1364 int retval;
1365
1366 /* Allocate and create a DMA chain */
1367 retval = pch_udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp);
1368 if (retval) {
1369 pr_err("%s: could not create DMA chain:%d\n", __func__, retval);
1370 return retval;
1371 }
1372 if (ep->in)
1373 req->td_data->status = (req->td_data->status &
1374 ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY;
1375 return 0;
1376}
1377
1378/**
1379 * process_zlp() - This function process zero length packets
1380 * from the gadget driver
1381 * @ep: Reference to the endpoint structure
1382 * @req: Reference to the request
1383 */
1384static void process_zlp(struct pch_udc_ep *ep, struct pch_udc_request *req)
1385{
1386 struct pch_udc_dev *dev = ep->dev;
1387
1388 /* IN zlp's are handled by hardware */
1389 complete_req(ep, req, 0);
1390
1391 /* if set_config or set_intf is waiting for ack by zlp
1392 * then set CSR_DONE
1393 */
1394 if (dev->set_cfg_not_acked) {
1395 pch_udc_set_csr_done(dev);
1396 dev->set_cfg_not_acked = 0;
1397 }
1398 /* setup command is ACK'ed now by zlp */
1399 if (!dev->stall && dev->waiting_zlp_ack) {
1400 pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX]));
1401 dev->waiting_zlp_ack = 0;
1402 }
1403}
1404
1405/**
1406 * pch_udc_start_rxrequest() - This function starts the receive requirement.
1407 * @ep: Reference to the endpoint structure
1408 * @req: Reference to the request structure
1409 */
1410static void pch_udc_start_rxrequest(struct pch_udc_ep *ep,
1411 struct pch_udc_request *req)
1412{
1413 struct pch_udc_data_dma_desc *td_data;
1414
1415 pch_udc_clear_dma(ep->dev, DMA_DIR_RX);
1416 td_data = req->td_data;
1417 /* Set the status bits for all descriptors */
1418 while (1) {
1419 td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
1420 PCH_UDC_BS_HST_RDY;
1421 if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
1422 break;
1423 td_data = phys_to_virt(td_data->next);
1424 }
1425 /* Write the descriptor pointer */
1426 pch_udc_ep_set_ddptr(ep, req->td_data_phys);
1427 req->dma_going = 1;
1428 pch_udc_enable_ep_interrupts(ep->dev, UDC_EPINT_OUT_EP0 << ep->num);
1429 pch_udc_set_dma(ep->dev, DMA_DIR_RX);
1430 pch_udc_ep_clear_nak(ep);
1431 pch_udc_ep_set_rrdy(ep);
1432}
1433
1434/**
1435 * pch_udc_pcd_ep_enable() - This API enables the endpoint. It is called
1436 * from gadget driver
1437 * @usbep: Reference to the USB endpoint structure
1438 * @desc: Reference to the USB endpoint descriptor structure
1439 *
1440 * Return codes:
1441 * 0: Success
1442 * -EINVAL:
1443 * -ESHUTDOWN:
1444 */
1445static int pch_udc_pcd_ep_enable(struct usb_ep *usbep,
1446 const struct usb_endpoint_descriptor *desc)
1447{
1448 struct pch_udc_ep *ep;
1449 struct pch_udc_dev *dev;
1450 unsigned long iflags;
1451
1452 if (!usbep || (usbep->name == ep0_string) || !desc ||
1453 (desc->bDescriptorType != USB_DT_ENDPOINT) || !desc->wMaxPacketSize)
1454 return -EINVAL;
1455
1456 ep = container_of(usbep, struct pch_udc_ep, ep);
1457 dev = ep->dev;
1458 if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN))
1459 return -ESHUTDOWN;
1460 spin_lock_irqsave(&dev->lock, iflags);
1461 ep->desc = desc;
1462 ep->halted = 0;
1463 pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc);
1464 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1465 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1466 spin_unlock_irqrestore(&dev->lock, iflags);
1467 return 0;
1468}
1469
1470/**
1471 * pch_udc_pcd_ep_disable() - This API disables endpoint and is called
1472 * from gadget driver
1473 * @usbep Reference to the USB endpoint structure
1474 *
1475 * Return codes:
1476 * 0: Success
1477 * -EINVAL:
1478 */
1479static int pch_udc_pcd_ep_disable(struct usb_ep *usbep)
1480{
1481 struct pch_udc_ep *ep;
1482 struct pch_udc_dev *dev;
1483 unsigned long iflags;
1484
1485 if (!usbep)
1486 return -EINVAL;
1487
1488 ep = container_of(usbep, struct pch_udc_ep, ep);
1489 dev = ep->dev;
1490 if ((usbep->name == ep0_string) || !ep->desc)
1491 return -EINVAL;
1492
1493 spin_lock_irqsave(&ep->dev->lock, iflags);
1494 empty_req_queue(ep);
1495 ep->halted = 1;
1496 pch_udc_ep_disable(ep);
1497 pch_udc_disable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1498 ep->desc = NULL;
1499 INIT_LIST_HEAD(&ep->queue);
1500 spin_unlock_irqrestore(&ep->dev->lock, iflags);
1501 return 0;
1502}
1503
1504/**
1505 * pch_udc_alloc_request() - This function allocates request structure.
1506 * It is called by gadget driver
1507 * @usbep: Reference to the USB endpoint structure
1508 * @gfp: Flag to be used while allocating memory
1509 *
1510 * Return codes:
1511 * NULL: Failure
1512 * Allocated address: Success
1513 */
1514static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
1515 gfp_t gfp)
1516{
1517 struct pch_udc_request *req;
1518 struct pch_udc_ep *ep;
1519 struct pch_udc_data_dma_desc *dma_desc;
1520 struct pch_udc_dev *dev;
1521
1522 if (!usbep)
1523 return NULL;
1524 ep = container_of(usbep, struct pch_udc_ep, ep);
1525 dev = ep->dev;
1526 req = kzalloc(sizeof *req, gfp);
1527 if (!req)
1528 return NULL;
1529 req->req.dma = DMA_ADDR_INVALID;
1530 req->dma = DMA_ADDR_INVALID;
1531 INIT_LIST_HEAD(&req->queue);
1532 if (!ep->dev->dma_addr)
1533 return &req->req;
1534 /* ep0 in requests are allocated from data pool here */
1535 dma_desc = pci_pool_alloc(ep->dev->data_requests, gfp,
1536 &req->td_data_phys);
1537 if (NULL == dma_desc) {
1538 kfree(req);
1539 return NULL;
1540 }
1541 /* prevent from using desc. - set HOST BUSY */
1542 dma_desc->status |= PCH_UDC_BS_HST_BSY;
1543 dma_desc->dataptr = __constant_cpu_to_le32(DMA_ADDR_INVALID);
1544 req->td_data = dma_desc;
1545 req->td_data_last = dma_desc;
1546 req->chain_len = 1;
1547 return &req->req;
1548}
1549
1550/**
1551 * pch_udc_free_request() - This function frees request structure.
1552 * It is called by gadget driver
1553 * @usbep: Reference to the USB endpoint structure
1554 * @usbreq: Reference to the USB request
1555 */
1556static void pch_udc_free_request(struct usb_ep *usbep,
1557 struct usb_request *usbreq)
1558{
1559 struct pch_udc_ep *ep;
1560 struct pch_udc_request *req;
1561 struct pch_udc_dev *dev;
1562
1563 if (!usbep || !usbreq)
1564 return;
1565 ep = container_of(usbep, struct pch_udc_ep, ep);
1566 req = container_of(usbreq, struct pch_udc_request, req);
1567 dev = ep->dev;
1568 if (!list_empty(&req->queue))
1569 dev_err(&dev->pdev->dev, "%s: %s req=0x%p queue not empty\n",
1570 __func__, usbep->name, req);
1571 if (req->td_data != NULL) {
1572 if (req->chain_len > 1)
1573 pch_udc_free_dma_chain(ep->dev, req);
1574 pci_pool_free(ep->dev->data_requests, req->td_data,
1575 req->td_data_phys);
1576 }
1577 kfree(req);
1578}
1579
1580/**
1581 * pch_udc_pcd_queue() - This function queues a request packet. It is called
1582 * by gadget driver
1583 * @usbep: Reference to the USB endpoint structure
1584 * @usbreq: Reference to the USB request
1585 * @gfp: Flag to be used while mapping the data buffer
1586 *
1587 * Return codes:
1588 * 0: Success
1589 * linux error number: Failure
1590 */
1591static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
1592 gfp_t gfp)
1593{
1594 int retval = 0;
1595 struct pch_udc_ep *ep;
1596 struct pch_udc_dev *dev;
1597 struct pch_udc_request *req;
1598 unsigned long iflags;
1599
1600 if (!usbep || !usbreq || !usbreq->complete || !usbreq->buf)
1601 return -EINVAL;
1602 ep = container_of(usbep, struct pch_udc_ep, ep);
1603 dev = ep->dev;
1604 if (!ep->desc && ep->num)
1605 return -EINVAL;
1606 req = container_of(usbreq, struct pch_udc_request, req);
1607 if (!list_empty(&req->queue))
1608 return -EINVAL;
1609 if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN))
1610 return -ESHUTDOWN;
1611 spin_lock_irqsave(&dev->lock, iflags);
1612 /* map the buffer for dma */
1613 if (usbreq->length &&
1614 ((usbreq->dma == DMA_ADDR_INVALID) || !usbreq->dma)) {
1615 if (!((unsigned long)(usbreq->buf) & 0x03)) {
1616 if (ep->in)
1617 usbreq->dma = dma_map_single(&dev->pdev->dev,
1618 usbreq->buf,
1619 usbreq->length,
1620 DMA_TO_DEVICE);
1621 else
1622 usbreq->dma = dma_map_single(&dev->pdev->dev,
1623 usbreq->buf,
1624 usbreq->length,
1625 DMA_FROM_DEVICE);
1626 } else {
1627 req->buf = kzalloc(usbreq->length, GFP_ATOMIC);
1628 if (!req->buf) {
1629 retval = -ENOMEM;
1630 goto probe_end;
1631 }
1632 if (ep->in) {
1633 memcpy(req->buf, usbreq->buf, usbreq->length);
1634 req->dma = dma_map_single(&dev->pdev->dev,
1635 req->buf,
1636 usbreq->length,
1637 DMA_TO_DEVICE);
1638 } else
1639 req->dma = dma_map_single(&dev->pdev->dev,
1640 req->buf,
1641 usbreq->length,
1642 DMA_FROM_DEVICE);
1643 }
1644 req->dma_mapped = 1;
1645 }
1646 if (usbreq->length > 0) {
1647 retval = prepare_dma(ep, req, GFP_ATOMIC);
1648 if (retval)
1649 goto probe_end;
1650 }
1651 usbreq->actual = 0;
1652 usbreq->status = -EINPROGRESS;
1653 req->dma_done = 0;
1654 if (list_empty(&ep->queue) && !ep->halted) {
1655 /* no pending transfer, so start this req */
1656 if (!usbreq->length) {
1657 process_zlp(ep, req);
1658 retval = 0;
1659 goto probe_end;
1660 }
1661 if (!ep->in) {
1662 pch_udc_start_rxrequest(ep, req);
1663 } else {
1664 /*
1665 * For IN trfr the descriptors will be programmed and
1666 * P bit will be set when
1667 * we get an IN token
1668 */
1669 pch_udc_wait_ep_stall(ep);
1670 pch_udc_ep_clear_nak(ep);
1671 pch_udc_enable_ep_interrupts(ep->dev, (1 << ep->num));
1672 }
1673 }
1674 /* Now add this request to the ep's pending requests */
1675 if (req != NULL)
1676 list_add_tail(&req->queue, &ep->queue);
1677
1678probe_end:
1679 spin_unlock_irqrestore(&dev->lock, iflags);
1680 return retval;
1681}
1682
1683/**
1684 * pch_udc_pcd_dequeue() - This function de-queues a request packet.
1685 * It is called by gadget driver
1686 * @usbep: Reference to the USB endpoint structure
1687 * @usbreq: Reference to the USB request
1688 *
1689 * Return codes:
1690 * 0: Success
1691 * linux error number: Failure
1692 */
1693static int pch_udc_pcd_dequeue(struct usb_ep *usbep,
1694 struct usb_request *usbreq)
1695{
1696 struct pch_udc_ep *ep;
1697 struct pch_udc_request *req;
1698 struct pch_udc_dev *dev;
1699 unsigned long flags;
1700 int ret = -EINVAL;
1701
1702 ep = container_of(usbep, struct pch_udc_ep, ep);
1703 dev = ep->dev;
1704 if (!usbep || !usbreq || (!ep->desc && ep->num))
1705 return ret;
1706 req = container_of(usbreq, struct pch_udc_request, req);
1707 spin_lock_irqsave(&ep->dev->lock, flags);
1708 /* make sure it's still queued on this endpoint */
1709 list_for_each_entry(req, &ep->queue, queue) {
1710 if (&req->req == usbreq) {
1711 pch_udc_ep_set_nak(ep);
1712 if (!list_empty(&req->queue))
1713 complete_req(ep, req, -ECONNRESET);
1714 ret = 0;
1715 break;
1716 }
1717 }
1718 spin_unlock_irqrestore(&ep->dev->lock, flags);
1719 return ret;
1720}
1721
1722/**
1723 * pch_udc_pcd_set_halt() - This function Sets or clear the endpoint halt
1724 * feature
1725 * @usbep: Reference to the USB endpoint structure
1726 * @halt: Specifies whether to set or clear the feature
1727 *
1728 * Return codes:
1729 * 0: Success
1730 * linux error number: Failure
1731 */
1732static int pch_udc_pcd_set_halt(struct usb_ep *usbep, int halt)
1733{
1734 struct pch_udc_ep *ep;
1735 struct pch_udc_dev *dev;
1736 unsigned long iflags;
1737 int ret;
1738
1739 if (!usbep)
1740 return -EINVAL;
1741 ep = container_of(usbep, struct pch_udc_ep, ep);
1742 dev = ep->dev;
1743 if (!ep->desc && !ep->num)
1744 return -EINVAL;
1745 if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
1746 return -ESHUTDOWN;
1747 spin_lock_irqsave(&udc_stall_spinlock, iflags);
1748 if (list_empty(&ep->queue)) {
1749 if (halt) {
1750 if (ep->num == PCH_UDC_EP0)
1751 ep->dev->stall = 1;
1752 pch_udc_ep_set_stall(ep);
1753 pch_udc_enable_ep_interrupts(ep->dev,
1754 PCH_UDC_EPINT(ep->in,
1755 ep->num));
1756 } else {
1757 pch_udc_ep_clear_stall(ep);
1758 }
1759 ret = 0;
1760 } else {
1761 ret = -EAGAIN;
1762 }
1763 spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
1764 return ret;
1765}
1766
1767/**
1768 * pch_udc_pcd_set_wedge() - This function Sets or clear the endpoint
1769 * halt feature
1770 * @usbep: Reference to the USB endpoint structure
1771 * @halt: Specifies whether to set or clear the feature
1772 *
1773 * Return codes:
1774 * 0: Success
1775 * linux error number: Failure
1776 */
1777static int pch_udc_pcd_set_wedge(struct usb_ep *usbep)
1778{
1779 struct pch_udc_ep *ep;
1780 struct pch_udc_dev *dev;
1781 unsigned long iflags;
1782 int ret;
1783
1784 if (!usbep)
1785 return -EINVAL;
1786 ep = container_of(usbep, struct pch_udc_ep, ep);
1787 dev = ep->dev;
1788 if (!ep->desc && !ep->num)
1789 return -EINVAL;
1790 if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
1791 return -ESHUTDOWN;
1792 spin_lock_irqsave(&udc_stall_spinlock, iflags);
1793 if (!list_empty(&ep->queue)) {
1794 ret = -EAGAIN;
1795 } else {
1796 if (ep->num == PCH_UDC_EP0)
1797 ep->dev->stall = 1;
1798 pch_udc_ep_set_stall(ep);
1799 pch_udc_enable_ep_interrupts(ep->dev,
1800 PCH_UDC_EPINT(ep->in, ep->num));
1801 ep->dev->prot_stall = 1;
1802 ret = 0;
1803 }
1804 spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
1805 return ret;
1806}
1807
1808/**
1809 * pch_udc_pcd_fifo_flush() - This function Flush the FIFO of specified endpoint
1810 * @usbep: Reference to the USB endpoint structure
1811 */
1812static void pch_udc_pcd_fifo_flush(struct usb_ep *usbep)
1813{
1814 struct pch_udc_ep *ep;
1815
1816 if (!usbep)
1817 return;
1818
1819 ep = container_of(usbep, struct pch_udc_ep, ep);
1820 if (ep->desc || !ep->num)
1821 pch_udc_ep_fifo_flush(ep, ep->in);
1822}
1823
1824static const struct usb_ep_ops pch_udc_ep_ops = {
1825 .enable = pch_udc_pcd_ep_enable,
1826 .disable = pch_udc_pcd_ep_disable,
1827 .alloc_request = pch_udc_alloc_request,
1828 .free_request = pch_udc_free_request,
1829 .queue = pch_udc_pcd_queue,
1830 .dequeue = pch_udc_pcd_dequeue,
1831 .set_halt = pch_udc_pcd_set_halt,
1832 .set_wedge = pch_udc_pcd_set_wedge,
1833 .fifo_status = NULL,
1834 .fifo_flush = pch_udc_pcd_fifo_flush,
1835};
1836
1837/**
1838 * pch_udc_init_setup_buff() - This function initializes the SETUP buffer
1839 * @td_stp: Reference to the SETP buffer structure
1840 */
1841static void pch_udc_init_setup_buff(struct pch_udc_stp_dma_desc *td_stp)
1842{
1843 static u32 pky_marker;
1844
1845 if (!td_stp)
1846 return;
1847 td_stp->reserved = ++pky_marker;
1848 memset(&td_stp->request, 0xFF, sizeof td_stp->request);
1849 td_stp->status = PCH_UDC_BS_HST_RDY;
1850}
1851
1852/**
1853 * pch_udc_start_next_txrequest() - This function starts
1854 * the next transmission requirement
1855 * @ep: Reference to the endpoint structure
1856 */
1857static void pch_udc_start_next_txrequest(struct pch_udc_ep *ep)
1858{
1859 struct pch_udc_request *req;
1860 struct pch_udc_data_dma_desc *td_data;
1861
1862 if (pch_udc_read_ep_control(ep) & UDC_EPCTL_P)
1863 return;
1864
1865 if (list_empty(&ep->queue))
1866 return;
1867
1868 /* next request */
1869 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1870 if (req->dma_going)
1871 return;
1872 if (!req->td_data)
1873 return;
1874 pch_udc_wait_ep_stall(ep);
1875 req->dma_going = 1;
1876 pch_udc_ep_set_ddptr(ep, 0);
1877 td_data = req->td_data;
1878 while (1) {
1879 td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
1880 PCH_UDC_BS_HST_RDY;
1881 if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
1882 break;
1883 td_data = phys_to_virt(td_data->next);
1884 }
1885 pch_udc_ep_set_ddptr(ep, req->td_data_phys);
1886 pch_udc_set_dma(ep->dev, DMA_DIR_TX);
1887 pch_udc_ep_set_pd(ep);
1888 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1889 pch_udc_ep_clear_nak(ep);
1890}
1891
1892/**
1893 * pch_udc_complete_transfer() - This function completes a transfer
1894 * @ep: Reference to the endpoint structure
1895 */
1896static void pch_udc_complete_transfer(struct pch_udc_ep *ep)
1897{
1898 struct pch_udc_request *req;
1899 struct pch_udc_dev *dev = ep->dev;
1900
1901 if (list_empty(&ep->queue))
1902 return;
1903 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1904 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
1905 PCH_UDC_BS_DMA_DONE)
1906 return;
1907 if ((req->td_data_last->status & PCH_UDC_RXTX_STS) !=
1908 PCH_UDC_RTS_SUCC) {
1909 dev_err(&dev->pdev->dev, "Invalid RXTX status (0x%08x) "
1910 "epstatus=0x%08x\n",
1911 (req->td_data_last->status & PCH_UDC_RXTX_STS),
1912 (int)(ep->epsts));
1913 return;
1914 }
1915
1916 req->req.actual = req->req.length;
1917 req->td_data_last->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
1918 req->td_data->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
1919 complete_req(ep, req, 0);
1920 req->dma_going = 0;
1921 if (!list_empty(&ep->queue)) {
1922 pch_udc_wait_ep_stall(ep);
1923 pch_udc_ep_clear_nak(ep);
1924 pch_udc_enable_ep_interrupts(ep->dev,
1925 PCH_UDC_EPINT(ep->in, ep->num));
1926 } else {
1927 pch_udc_disable_ep_interrupts(ep->dev,
1928 PCH_UDC_EPINT(ep->in, ep->num));
1929 }
1930}
1931
1932/**
1933 * pch_udc_complete_receiver() - This function completes a receiver
1934 * @ep: Reference to the endpoint structure
1935 */
1936static void pch_udc_complete_receiver(struct pch_udc_ep *ep)
1937{
1938 struct pch_udc_request *req;
1939 struct pch_udc_dev *dev = ep->dev;
1940 unsigned int count;
1941 struct pch_udc_data_dma_desc *td;
1942 dma_addr_t addr;
1943
1944 if (list_empty(&ep->queue))
1945 return;
1946 /* next request */
1947 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1948 pch_udc_clear_dma(ep->dev, DMA_DIR_RX);
1949 pch_udc_ep_set_ddptr(ep, 0);
1950 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) ==
1951 PCH_UDC_BS_DMA_DONE)
1952 td = req->td_data_last;
1953 else
1954 td = req->td_data;
1955
1956 while (1) {
1957 if ((td->status & PCH_UDC_RXTX_STS) != PCH_UDC_RTS_SUCC) {
1958 dev_err(&dev->pdev->dev, "Invalid RXTX status=0x%08x "
1959 "epstatus=0x%08x\n",
1960 (req->td_data->status & PCH_UDC_RXTX_STS),
1961 (int)(ep->epsts));
1962 return;
1963 }
1964 if ((td->status & PCH_UDC_BUFF_STS) == PCH_UDC_BS_DMA_DONE)
1965 if (td->status | PCH_UDC_DMA_LAST) {
1966 count = td->status & PCH_UDC_RXTX_BYTES;
1967 break;
1968 }
1969 if (td == req->td_data_last) {
1970 dev_err(&dev->pdev->dev, "Not complete RX descriptor");
1971 return;
1972 }
1973 addr = (dma_addr_t)td->next;
1974 td = phys_to_virt(addr);
1975 }
1976 /* on 64k packets the RXBYTES field is zero */
1977 if (!count && (req->req.length == UDC_DMA_MAXPACKET))
1978 count = UDC_DMA_MAXPACKET;
1979 req->td_data->status |= PCH_UDC_DMA_LAST;
1980 td->status |= PCH_UDC_BS_HST_BSY;
1981
1982 req->dma_going = 0;
1983 req->req.actual = count;
1984 complete_req(ep, req, 0);
1985 /* If there is a new/failed requests try that now */
1986 if (!list_empty(&ep->queue)) {
1987 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1988 pch_udc_start_rxrequest(ep, req);
1989 }
1990}
1991
1992/**
1993 * pch_udc_svc_data_in() - This function process endpoint interrupts
1994 * for IN endpoints
1995 * @dev: Reference to the device structure
1996 * @ep_num: Endpoint that generated the interrupt
1997 */
1998static void pch_udc_svc_data_in(struct pch_udc_dev *dev, int ep_num)
1999{
2000 u32 epsts;
2001 struct pch_udc_ep *ep;
2002
2003 ep = &dev->ep[UDC_EPIN_IDX(ep_num)];
2004 epsts = ep->epsts;
2005 ep->epsts = 0;
2006
2007 if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
2008 UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
2009 UDC_EPSTS_RSS | UDC_EPSTS_XFERDONE)))
2010 return;
2011 if ((epsts & UDC_EPSTS_BNA))
2012 return;
2013 if (epsts & UDC_EPSTS_HE)
2014 return;
2015 if (epsts & UDC_EPSTS_RSS) {
2016 pch_udc_ep_set_stall(ep);
2017 pch_udc_enable_ep_interrupts(ep->dev,
2018 PCH_UDC_EPINT(ep->in, ep->num));
2019 }
2020 if (epsts & UDC_EPSTS_RCS) {
2021 if (!dev->prot_stall) {
2022 pch_udc_ep_clear_stall(ep);
2023 } else {
2024 pch_udc_ep_set_stall(ep);
2025 pch_udc_enable_ep_interrupts(ep->dev,
2026 PCH_UDC_EPINT(ep->in, ep->num));
2027 }
2028 }
2029 if (epsts & UDC_EPSTS_TDC)
2030 pch_udc_complete_transfer(ep);
2031 /* On IN interrupt, provide data if we have any */
2032 if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_RSS) &&
2033 !(epsts & UDC_EPSTS_TDC) && !(epsts & UDC_EPSTS_TXEMPTY))
2034 pch_udc_start_next_txrequest(ep);
2035}
2036
2037/**
2038 * pch_udc_svc_data_out() - Handles interrupts from OUT endpoint
2039 * @dev: Reference to the device structure
2040 * @ep_num: Endpoint that generated the interrupt
2041 */
2042static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num)
2043{
2044 u32 epsts;
2045 struct pch_udc_ep *ep;
2046 struct pch_udc_request *req = NULL;
2047
2048 ep = &dev->ep[UDC_EPOUT_IDX(ep_num)];
2049 epsts = ep->epsts;
2050 ep->epsts = 0;
2051
2052 if ((epsts & UDC_EPSTS_BNA) && (!list_empty(&ep->queue))) {
2053 /* next request */
2054 req = list_entry(ep->queue.next, struct pch_udc_request,
2055 queue);
2056 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
2057 PCH_UDC_BS_DMA_DONE) {
2058 if (!req->dma_going)
2059 pch_udc_start_rxrequest(ep, req);
2060 return;
2061 }
2062 }
2063 if (epsts & UDC_EPSTS_HE)
2064 return;
2065 if (epsts & UDC_EPSTS_RSS) {
2066 pch_udc_ep_set_stall(ep);
2067 pch_udc_enable_ep_interrupts(ep->dev,
2068 PCH_UDC_EPINT(ep->in, ep->num));
2069 }
2070 if (epsts & UDC_EPSTS_RCS) {
2071 if (!dev->prot_stall) {
2072 pch_udc_ep_clear_stall(ep);
2073 } else {
2074 pch_udc_ep_set_stall(ep);
2075 pch_udc_enable_ep_interrupts(ep->dev,
2076 PCH_UDC_EPINT(ep->in, ep->num));
2077 }
2078 }
2079 if (((epsts & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2080 UDC_EPSTS_OUT_DATA) {
2081 if (ep->dev->prot_stall == 1) {
2082 pch_udc_ep_set_stall(ep);
2083 pch_udc_enable_ep_interrupts(ep->dev,
2084 PCH_UDC_EPINT(ep->in, ep->num));
2085 } else {
2086 pch_udc_complete_receiver(ep);
2087 }
2088 }
2089 if (list_empty(&ep->queue))
2090 pch_udc_set_dma(dev, DMA_DIR_RX);
2091}
2092
2093/**
2094 * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts
2095 * @dev: Reference to the device structure
2096 */
2097static void pch_udc_svc_control_in(struct pch_udc_dev *dev)
2098{
2099 u32 epsts;
2100 struct pch_udc_ep *ep;
2101 struct pch_udc_ep *ep_out;
2102
2103 ep = &dev->ep[UDC_EP0IN_IDX];
2104 ep_out = &dev->ep[UDC_EP0OUT_IDX];
2105 epsts = ep->epsts;
2106 ep->epsts = 0;
2107
2108 if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
2109 UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
2110 UDC_EPSTS_XFERDONE)))
2111 return;
2112 if ((epsts & UDC_EPSTS_BNA))
2113 return;
2114 if (epsts & UDC_EPSTS_HE)
2115 return;
2116 if ((epsts & UDC_EPSTS_TDC) && (!dev->stall)) {
2117 pch_udc_complete_transfer(ep);
2118 pch_udc_clear_dma(dev, DMA_DIR_RX);
2119 ep_out->td_data->status = (ep_out->td_data->status &
2120 ~PCH_UDC_BUFF_STS) |
2121 PCH_UDC_BS_HST_RDY;
2122 pch_udc_ep_clear_nak(ep_out);
2123 pch_udc_set_dma(dev, DMA_DIR_RX);
2124 pch_udc_ep_set_rrdy(ep_out);
2125 }
2126 /* On IN interrupt, provide data if we have any */
2127 if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_TDC) &&
2128 !(epsts & UDC_EPSTS_TXEMPTY))
2129 pch_udc_start_next_txrequest(ep);
2130}
2131
2132/**
2133 * pch_udc_svc_control_out() - Routine that handle Control
2134 * OUT endpoint interrupts
2135 * @dev: Reference to the device structure
2136 */
2137static void pch_udc_svc_control_out(struct pch_udc_dev *dev)
2138{
2139 u32 stat;
2140 int setup_supported;
2141 struct pch_udc_ep *ep;
2142
2143 ep = &dev->ep[UDC_EP0OUT_IDX];
2144 stat = ep->epsts;
2145 ep->epsts = 0;
2146
2147 /* If setup data */
2148 if (((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2149 UDC_EPSTS_OUT_SETUP) {
2150 dev->stall = 0;
2151 dev->ep[UDC_EP0IN_IDX].halted = 0;
2152 dev->ep[UDC_EP0OUT_IDX].halted = 0;
2153 dev->setup_data = ep->td_stp->request;
2154 pch_udc_init_setup_buff(ep->td_stp);
2155 pch_udc_clear_dma(dev, DMA_DIR_RX);
2156 pch_udc_ep_fifo_flush(&(dev->ep[UDC_EP0IN_IDX]),
2157 dev->ep[UDC_EP0IN_IDX].in);
2158 if ((dev->setup_data.bRequestType & USB_DIR_IN))
2159 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
2160 else /* OUT */
2161 dev->gadget.ep0 = &ep->ep;
2162 spin_unlock(&dev->lock);
2163 /* If Mass storage Reset */
2164 if ((dev->setup_data.bRequestType == 0x21) &&
2165 (dev->setup_data.bRequest == 0xFF))
2166 dev->prot_stall = 0;
2167 /* call gadget with setup data received */
2168 setup_supported = dev->driver->setup(&dev->gadget,
2169 &dev->setup_data);
2170 spin_lock(&dev->lock);
2171
2172 if (dev->setup_data.bRequestType & USB_DIR_IN) {
2173 ep->td_data->status = (ep->td_data->status &
2174 ~PCH_UDC_BUFF_STS) |
2175 PCH_UDC_BS_HST_RDY;
2176 pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
2177 }
2178 /* ep0 in returns data on IN phase */
2179 if (setup_supported >= 0 && setup_supported <
2180 UDC_EP0IN_MAX_PKT_SIZE) {
2181 pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX]));
2182 /* Gadget would have queued a request when
2183 * we called the setup */
2184 if (!(dev->setup_data.bRequestType & USB_DIR_IN)) {
2185 pch_udc_set_dma(dev, DMA_DIR_RX);
2186 pch_udc_ep_clear_nak(ep);
2187 }
2188 } else if (setup_supported < 0) {
2189 /* if unsupported request, then stall */
2190 pch_udc_ep_set_stall(&(dev->ep[UDC_EP0IN_IDX]));
2191 pch_udc_enable_ep_interrupts(ep->dev,
2192 PCH_UDC_EPINT(ep->in, ep->num));
2193 dev->stall = 0;
2194 pch_udc_set_dma(dev, DMA_DIR_RX);
2195 } else {
2196 dev->waiting_zlp_ack = 1;
2197 }
2198 } else if ((((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2199 UDC_EPSTS_OUT_DATA) && !dev->stall) {
2200 pch_udc_clear_dma(dev, DMA_DIR_RX);
2201 pch_udc_ep_set_ddptr(ep, 0);
2202 if (!list_empty(&ep->queue)) {
2203 ep->epsts = stat;
2204 pch_udc_svc_data_out(dev, PCH_UDC_EP0);
2205 }
2206 pch_udc_set_dma(dev, DMA_DIR_RX);
2207 }
2208 pch_udc_ep_set_rrdy(ep);
2209}
2210
2211
2212/**
2213 * pch_udc_postsvc_epinters() - This function enables end point interrupts
2214 * and clears NAK status
2215 * @dev: Reference to the device structure
2216 * @ep_num: End point number
2217 */
2218static void pch_udc_postsvc_epinters(struct pch_udc_dev *dev, int ep_num)
2219{
2220 struct pch_udc_ep *ep;
2221 struct pch_udc_request *req;
2222
2223 ep = &dev->ep[UDC_EPIN_IDX(ep_num)];
2224 if (!list_empty(&ep->queue)) {
2225 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
2226 pch_udc_enable_ep_interrupts(ep->dev,
2227 PCH_UDC_EPINT(ep->in, ep->num));
2228 pch_udc_ep_clear_nak(ep);
2229 }
2230}
2231
2232/**
2233 * pch_udc_read_all_epstatus() - This function read all endpoint status
2234 * @dev: Reference to the device structure
2235 * @ep_intr: Status of endpoint interrupt
2236 */
2237static void pch_udc_read_all_epstatus(struct pch_udc_dev *dev, u32 ep_intr)
2238{
2239 int i;
2240 struct pch_udc_ep *ep;
2241
2242 for (i = 0; i < PCH_UDC_USED_EP_NUM; i++) {
2243 /* IN */
2244 if (ep_intr & (0x1 << i)) {
2245 ep = &dev->ep[UDC_EPIN_IDX(i)];
2246 ep->epsts = pch_udc_read_ep_status(ep);
2247 pch_udc_clear_ep_status(ep, ep->epsts);
2248 }
2249 /* OUT */
2250 if (ep_intr & (0x10000 << i)) {
2251 ep = &dev->ep[UDC_EPOUT_IDX(i)];
2252 ep->epsts = pch_udc_read_ep_status(ep);
2253 pch_udc_clear_ep_status(ep, ep->epsts);
2254 }
2255 }
2256}
2257
2258/**
2259 * pch_udc_activate_control_ep() - This function enables the control endpoints
2260 * for traffic after a reset
2261 * @dev: Reference to the device structure
2262 */
2263static void pch_udc_activate_control_ep(struct pch_udc_dev *dev)
2264{
2265 struct pch_udc_ep *ep;
2266 u32 val;
2267
2268 /* Setup the IN endpoint */
2269 ep = &dev->ep[UDC_EP0IN_IDX];
2270 pch_udc_clear_ep_control(ep);
2271 pch_udc_ep_fifo_flush(ep, ep->in);
2272 pch_udc_ep_set_bufsz(ep, UDC_EP0IN_BUFF_SIZE, ep->in);
2273 pch_udc_ep_set_maxpkt(ep, UDC_EP0IN_MAX_PKT_SIZE);
2274 /* Initialize the IN EP Descriptor */
2275 ep->td_data = NULL;
2276 ep->td_stp = NULL;
2277 ep->td_data_phys = 0;
2278 ep->td_stp_phys = 0;
2279
2280 /* Setup the OUT endpoint */
2281 ep = &dev->ep[UDC_EP0OUT_IDX];
2282 pch_udc_clear_ep_control(ep);
2283 pch_udc_ep_fifo_flush(ep, ep->in);
2284 pch_udc_ep_set_bufsz(ep, UDC_EP0OUT_BUFF_SIZE, ep->in);
2285 pch_udc_ep_set_maxpkt(ep, UDC_EP0OUT_MAX_PKT_SIZE);
2286 val = UDC_EP0OUT_MAX_PKT_SIZE << UDC_CSR_NE_MAX_PKT_SHIFT;
2287 pch_udc_write_csr(ep->dev, val, UDC_EP0OUT_IDX);
2288
2289 /* Initialize the SETUP buffer */
2290 pch_udc_init_setup_buff(ep->td_stp);
2291 /* Write the pointer address of dma descriptor */
2292 pch_udc_ep_set_subptr(ep, ep->td_stp_phys);
2293 /* Write the pointer address of Setup descriptor */
2294 pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
2295
2296 /* Initialize the dma descriptor */
2297 ep->td_data->status = PCH_UDC_DMA_LAST;
2298 ep->td_data->dataptr = dev->dma_addr;
2299 ep->td_data->next = ep->td_data_phys;
2300
2301 pch_udc_ep_clear_nak(ep);
2302}
2303
2304
2305/**
2306 * pch_udc_svc_ur_interrupt() - This function handles a USB reset interrupt
2307 * @dev: Reference to driver structure
2308 */
2309static void pch_udc_svc_ur_interrupt(struct pch_udc_dev *dev)
2310{
2311 struct pch_udc_ep *ep;
2312 int i;
2313
2314 pch_udc_clear_dma(dev, DMA_DIR_TX);
2315 pch_udc_clear_dma(dev, DMA_DIR_RX);
2316 /* Mask all endpoint interrupts */
2317 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2318 /* clear all endpoint interrupts */
2319 pch_udc_write_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2320
2321 for (i = 0; i < PCH_UDC_EP_NUM; i++) {
2322 ep = &dev->ep[i];
2323 pch_udc_clear_ep_status(ep, UDC_EPSTS_ALL_CLR_MASK);
2324 pch_udc_clear_ep_control(ep);
2325 pch_udc_ep_set_ddptr(ep, 0);
2326 pch_udc_write_csr(ep->dev, 0x00, i);
2327 }
2328 dev->stall = 0;
2329 dev->prot_stall = 0;
2330 dev->waiting_zlp_ack = 0;
2331 dev->set_cfg_not_acked = 0;
2332
2333 /* disable ep to empty req queue. Skip the control EP's */
2334 for (i = 0; i < (PCH_UDC_USED_EP_NUM*2); i++) {
2335 ep = &dev->ep[i];
2336 pch_udc_ep_set_nak(ep);
2337 pch_udc_ep_fifo_flush(ep, ep->in);
2338 /* Complete request queue */
2339 empty_req_queue(ep);
2340 }
2341 if (dev->driver && dev->driver->disconnect)
2342 dev->driver->disconnect(&dev->gadget);
2343}
2344
2345/**
2346 * pch_udc_svc_enum_interrupt() - This function handles a USB speed enumeration
2347 * done interrupt
2348 * @dev: Reference to driver structure
2349 */
2350static void pch_udc_svc_enum_interrupt(struct pch_udc_dev *dev)
2351{
2352 u32 dev_stat, dev_speed;
2353 u32 speed = USB_SPEED_FULL;
2354
2355 dev_stat = pch_udc_read_device_status(dev);
2356 dev_speed = (dev_stat & UDC_DEVSTS_ENUM_SPEED_MASK) >>
2357 UDC_DEVSTS_ENUM_SPEED_SHIFT;
2358 switch (dev_speed) {
2359 case UDC_DEVSTS_ENUM_SPEED_HIGH:
2360 speed = USB_SPEED_HIGH;
2361 break;
2362 case UDC_DEVSTS_ENUM_SPEED_FULL:
2363 speed = USB_SPEED_FULL;
2364 break;
2365 case UDC_DEVSTS_ENUM_SPEED_LOW:
2366 speed = USB_SPEED_LOW;
2367 break;
2368 default:
2369 BUG();
2370 }
2371 dev->gadget.speed = speed;
2372 pch_udc_activate_control_ep(dev);
2373 pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 | UDC_EPINT_OUT_EP0);
2374 pch_udc_set_dma(dev, DMA_DIR_TX);
2375 pch_udc_set_dma(dev, DMA_DIR_RX);
2376 pch_udc_ep_set_rrdy(&(dev->ep[UDC_EP0OUT_IDX]));
2377}
2378
2379/**
2380 * pch_udc_svc_intf_interrupt() - This function handles a set interface
2381 * interrupt
2382 * @dev: Reference to driver structure
2383 */
2384static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev)
2385{
2386 u32 reg, dev_stat = 0;
2387 int i, ret;
2388
2389 dev_stat = pch_udc_read_device_status(dev);
2390 dev->cfg_data.cur_intf = (dev_stat & UDC_DEVSTS_INTF_MASK) >>
2391 UDC_DEVSTS_INTF_SHIFT;
2392 dev->cfg_data.cur_alt = (dev_stat & UDC_DEVSTS_ALT_MASK) >>
2393 UDC_DEVSTS_ALT_SHIFT;
2394 dev->set_cfg_not_acked = 1;
2395 /* Construct the usb request for gadget driver and inform it */
2396 memset(&dev->setup_data, 0 , sizeof dev->setup_data);
2397 dev->setup_data.bRequest = USB_REQ_SET_INTERFACE;
2398 dev->setup_data.bRequestType = USB_RECIP_INTERFACE;
2399 dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_alt);
2400 dev->setup_data.wIndex = cpu_to_le16(dev->cfg_data.cur_intf);
2401 /* programm the Endpoint Cfg registers */
2402 /* Only one end point cfg register */
2403 reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
2404 reg = (reg & ~UDC_CSR_NE_INTF_MASK) |
2405 (dev->cfg_data.cur_intf << UDC_CSR_NE_INTF_SHIFT);
2406 reg = (reg & ~UDC_CSR_NE_ALT_MASK) |
2407 (dev->cfg_data.cur_alt << UDC_CSR_NE_ALT_SHIFT);
2408 pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
2409 for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
2410 /* clear stall bits */
2411 pch_udc_ep_clear_stall(&(dev->ep[i]));
2412 dev->ep[i].halted = 0;
2413 }
2414 dev->stall = 0;
2415 spin_unlock(&dev->lock);
2416 ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
2417 spin_lock(&dev->lock);
2418}
2419
2420/**
2421 * pch_udc_svc_cfg_interrupt() - This function handles a set configuration
2422 * interrupt
2423 * @dev: Reference to driver structure
2424 */
2425static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev)
2426{
2427 int i, ret;
2428 u32 reg, dev_stat = 0;
2429
2430 dev_stat = pch_udc_read_device_status(dev);
2431 dev->set_cfg_not_acked = 1;
2432 dev->cfg_data.cur_cfg = (dev_stat & UDC_DEVSTS_CFG_MASK) >>
2433 UDC_DEVSTS_CFG_SHIFT;
2434 /* make usb request for gadget driver */
2435 memset(&dev->setup_data, 0 , sizeof dev->setup_data);
2436 dev->setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
2437 dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_cfg);
2438 /* program the NE registers */
2439 /* Only one end point cfg register */
2440 reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
2441 reg = (reg & ~UDC_CSR_NE_CFG_MASK) |
2442 (dev->cfg_data.cur_cfg << UDC_CSR_NE_CFG_SHIFT);
2443 pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
2444 for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
2445 /* clear stall bits */
2446 pch_udc_ep_clear_stall(&(dev->ep[i]));
2447 dev->ep[i].halted = 0;
2448 }
2449 dev->stall = 0;
2450
2451 /* call gadget zero with setup data received */
2452 spin_unlock(&dev->lock);
2453 ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
2454 spin_lock(&dev->lock);
2455}
2456
2457/**
2458 * pch_udc_dev_isr() - This function services device interrupts
2459 * by invoking appropriate routines.
2460 * @dev: Reference to the device structure
2461 * @dev_intr: The Device interrupt status.
2462 */
2463static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr)
2464{
2465 /* USB Reset Interrupt */
2466 if (dev_intr & UDC_DEVINT_UR)
2467 pch_udc_svc_ur_interrupt(dev);
2468 /* Enumeration Done Interrupt */
2469 if (dev_intr & UDC_DEVINT_ENUM)
2470 pch_udc_svc_enum_interrupt(dev);
2471 /* Set Interface Interrupt */
2472 if (dev_intr & UDC_DEVINT_SI)
2473 pch_udc_svc_intf_interrupt(dev);
2474 /* Set Config Interrupt */
2475 if (dev_intr & UDC_DEVINT_SC)
2476 pch_udc_svc_cfg_interrupt(dev);
2477 /* USB Suspend interrupt */
2478 if (dev_intr & UDC_DEVINT_US)
2479 dev_dbg(&dev->pdev->dev, "USB_SUSPEND\n");
2480 /* Clear the SOF interrupt, if enabled */
2481 if (dev_intr & UDC_DEVINT_SOF)
2482 dev_dbg(&dev->pdev->dev, "SOF\n");
2483 /* ES interrupt, IDLE > 3ms on the USB */
2484 if (dev_intr & UDC_DEVINT_ES)
2485 dev_dbg(&dev->pdev->dev, "ES\n");
2486 /* RWKP interrupt */
2487 if (dev_intr & UDC_DEVINT_RWKP)
2488 dev_dbg(&dev->pdev->dev, "RWKP\n");
2489}
2490
2491/**
2492 * pch_udc_isr() - This function handles interrupts from the PCH USB Device
2493 * @irq: Interrupt request number
2494 * @dev: Reference to the device structure
2495 */
2496static irqreturn_t pch_udc_isr(int irq, void *pdev)
2497{
2498 struct pch_udc_dev *dev = (struct pch_udc_dev *) pdev;
2499 u32 dev_intr, ep_intr;
2500 int i;
2501
2502 dev_intr = pch_udc_read_device_interrupts(dev);
2503 ep_intr = pch_udc_read_ep_interrupts(dev);
2504
2505 if (dev_intr)
2506 /* Clear device interrupts */
2507 pch_udc_write_device_interrupts(dev, dev_intr);
2508 if (ep_intr)
2509 /* Clear ep interrupts */
2510 pch_udc_write_ep_interrupts(dev, ep_intr);
2511 if (!dev_intr && !ep_intr)
2512 return IRQ_NONE;
2513 spin_lock(&dev->lock);
2514 if (dev_intr)
2515 pch_udc_dev_isr(dev, dev_intr);
2516 if (ep_intr) {
2517 pch_udc_read_all_epstatus(dev, ep_intr);
2518 /* Process Control In interrupts, if present */
2519 if (ep_intr & UDC_EPINT_IN_EP0) {
2520 pch_udc_svc_control_in(dev);
2521 pch_udc_postsvc_epinters(dev, 0);
2522 }
2523 /* Process Control Out interrupts, if present */
2524 if (ep_intr & UDC_EPINT_OUT_EP0)
2525 pch_udc_svc_control_out(dev);
2526 /* Process data in end point interrupts */
2527 for (i = 1; i < PCH_UDC_USED_EP_NUM; i++) {
2528 if (ep_intr & (1 << i)) {
2529 pch_udc_svc_data_in(dev, i);
2530 pch_udc_postsvc_epinters(dev, i);
2531 }
2532 }
2533 /* Process data out end point interrupts */
2534 for (i = UDC_EPINT_OUT_SHIFT + 1; i < (UDC_EPINT_OUT_SHIFT +
2535 PCH_UDC_USED_EP_NUM); i++)
2536 if (ep_intr & (1 << i))
2537 pch_udc_svc_data_out(dev, i -
2538 UDC_EPINT_OUT_SHIFT);
2539 }
2540 spin_unlock(&dev->lock);
2541 return IRQ_HANDLED;
2542}
2543
2544/**
2545 * pch_udc_setup_ep0() - This function enables control endpoint for traffic
2546 * @dev: Reference to the device structure
2547 */
2548static void pch_udc_setup_ep0(struct pch_udc_dev *dev)
2549{
2550 /* enable ep0 interrupts */
2551 pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 |
2552 UDC_EPINT_OUT_EP0);
2553 /* enable device interrupts */
2554 pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US |
2555 UDC_DEVINT_ES | UDC_DEVINT_ENUM |
2556 UDC_DEVINT_SI | UDC_DEVINT_SC);
2557}
2558
2559/**
2560 * gadget_release() - Free the gadget driver private data
2561 * @pdev reference to struct pci_dev
2562 */
2563static void gadget_release(struct device *pdev)
2564{
2565 struct pch_udc_dev *dev = dev_get_drvdata(pdev);
2566
2567 kfree(dev);
2568}
2569
2570/**
2571 * pch_udc_pcd_reinit() - This API initializes the endpoint structures
2572 * @dev: Reference to the driver structure
2573 */
2574static void pch_udc_pcd_reinit(struct pch_udc_dev *dev)
2575{
2576 const char *const ep_string[] = {
2577 ep0_string, "ep0out", "ep1in", "ep1out", "ep2in", "ep2out",
2578 "ep3in", "ep3out", "ep4in", "ep4out", "ep5in", "ep5out",
2579 "ep6in", "ep6out", "ep7in", "ep7out", "ep8in", "ep8out",
2580 "ep9in", "ep9out", "ep10in", "ep10out", "ep11in", "ep11out",
2581 "ep12in", "ep12out", "ep13in", "ep13out", "ep14in", "ep14out",
2582 "ep15in", "ep15out",
2583 };
2584 int i;
2585
2586 dev->gadget.speed = USB_SPEED_UNKNOWN;
2587 INIT_LIST_HEAD(&dev->gadget.ep_list);
2588
2589 /* Initialize the endpoints structures */
2590 memset(dev->ep, 0, sizeof dev->ep);
2591 for (i = 0; i < PCH_UDC_EP_NUM; i++) {
2592 struct pch_udc_ep *ep = &dev->ep[i];
2593 ep->dev = dev;
2594 ep->halted = 1;
2595 ep->num = i / 2;
2596 ep->in = ~i & 1;
2597 ep->ep.name = ep_string[i];
2598 ep->ep.ops = &pch_udc_ep_ops;
2599 if (ep->in)
2600 ep->offset_addr = ep->num * UDC_EP_REG_SHIFT;
2601 else
2602 ep->offset_addr = (UDC_EPINT_OUT_SHIFT + ep->num) *
2603 UDC_EP_REG_SHIFT;
2604 /* need to set ep->ep.maxpacket and set Default Configuration?*/
2605 ep->ep.maxpacket = UDC_BULK_MAX_PKT_SIZE;
2606 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
2607 INIT_LIST_HEAD(&ep->queue);
2608 }
2609 dev->ep[UDC_EP0IN_IDX].ep.maxpacket = UDC_EP0IN_MAX_PKT_SIZE;
2610 dev->ep[UDC_EP0OUT_IDX].ep.maxpacket = UDC_EP0OUT_MAX_PKT_SIZE;
2611
2612 /* remove ep0 in and out from the list. They have own pointer */
2613 list_del_init(&dev->ep[UDC_EP0IN_IDX].ep.ep_list);
2614 list_del_init(&dev->ep[UDC_EP0OUT_IDX].ep.ep_list);
2615
2616 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
2617 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2618}
2619
2620/**
2621 * pch_udc_pcd_init() - This API initializes the driver structure
2622 * @dev: Reference to the driver structure
2623 *
2624 * Return codes:
2625 * 0: Success
2626 */
2627static int pch_udc_pcd_init(struct pch_udc_dev *dev)
2628{
2629 pch_udc_init(dev);
2630 pch_udc_pcd_reinit(dev);
2631 return 0;
2632}
2633
2634/**
2635 * init_dma_pools() - create dma pools during initialization
2636 * @pdev: reference to struct pci_dev
2637 */
2638static int init_dma_pools(struct pch_udc_dev *dev)
2639{
2640 struct pch_udc_stp_dma_desc *td_stp;
2641 struct pch_udc_data_dma_desc *td_data;
2642
2643 /* DMA setup */
2644 dev->data_requests = pci_pool_create("data_requests", dev->pdev,
2645 sizeof(struct pch_udc_data_dma_desc), 0, 0);
2646 if (!dev->data_requests) {
2647 dev_err(&dev->pdev->dev, "%s: can't get request data pool\n",
2648 __func__);
2649 return -ENOMEM;
2650 }
2651
2652 /* dma desc for setup data */
2653 dev->stp_requests = pci_pool_create("setup requests", dev->pdev,
2654 sizeof(struct pch_udc_stp_dma_desc), 0, 0);
2655 if (!dev->stp_requests) {
2656 dev_err(&dev->pdev->dev, "%s: can't get setup request pool\n",
2657 __func__);
2658 return -ENOMEM;
2659 }
2660 /* setup */
2661 td_stp = pci_pool_alloc(dev->stp_requests, GFP_KERNEL,
2662 &dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
2663 if (!td_stp) {
2664 dev_err(&dev->pdev->dev,
2665 "%s: can't allocate setup dma descriptor\n", __func__);
2666 return -ENOMEM;
2667 }
2668 dev->ep[UDC_EP0OUT_IDX].td_stp = td_stp;
2669
2670 /* data: 0 packets !? */
2671 td_data = pci_pool_alloc(dev->data_requests, GFP_KERNEL,
2672 &dev->ep[UDC_EP0OUT_IDX].td_data_phys);
2673 if (!td_data) {
2674 dev_err(&dev->pdev->dev,
2675 "%s: can't allocate data dma descriptor\n", __func__);
2676 return -ENOMEM;
2677 }
2678 dev->ep[UDC_EP0OUT_IDX].td_data = td_data;
2679 dev->ep[UDC_EP0IN_IDX].td_stp = NULL;
2680 dev->ep[UDC_EP0IN_IDX].td_stp_phys = 0;
2681 dev->ep[UDC_EP0IN_IDX].td_data = NULL;
2682 dev->ep[UDC_EP0IN_IDX].td_data_phys = 0;
2683
2684 dev->ep0out_buf = kzalloc(UDC_EP0OUT_BUFF_SIZE * 4, GFP_KERNEL);
2685 if (!dev->ep0out_buf)
2686 return -ENOMEM;
2687 dev->dma_addr = dma_map_single(&dev->pdev->dev, dev->ep0out_buf,
2688 UDC_EP0OUT_BUFF_SIZE * 4,
2689 DMA_FROM_DEVICE);
2690 return 0;
2691}
2692
2693int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2694 int (*bind)(struct usb_gadget *))
2695{
2696 struct pch_udc_dev *dev = pch_udc;
2697 int retval;
2698
2699 if (!driver || (driver->speed == USB_SPEED_UNKNOWN) || !bind ||
2700 !driver->setup || !driver->unbind || !driver->disconnect) {
2701 dev_err(&dev->pdev->dev,
2702 "%s: invalid driver parameter\n", __func__);
2703 return -EINVAL;
2704 }
2705
2706 if (!dev)
2707 return -ENODEV;
2708
2709 if (dev->driver) {
2710 dev_err(&dev->pdev->dev, "%s: already bound\n", __func__);
2711 return -EBUSY;
2712 }
2713 driver->driver.bus = NULL;
2714 dev->driver = driver;
2715 dev->gadget.dev.driver = &driver->driver;
2716
2717 /* Invoke the bind routine of the gadget driver */
2718 retval = bind(&dev->gadget);
2719
2720 if (retval) {
2721 dev_err(&dev->pdev->dev, "%s: binding to %s returning %d\n",
2722 __func__, driver->driver.name, retval);
2723 dev->driver = NULL;
2724 dev->gadget.dev.driver = NULL;
2725 return retval;
2726 }
2727 /* get ready for ep0 traffic */
2728 pch_udc_setup_ep0(dev);
2729
2730 /* clear SD */
2731 pch_udc_clear_disconnect(dev);
2732
2733 dev->connected = 1;
2734 return 0;
2735}
2736EXPORT_SYMBOL(usb_gadget_probe_driver);
2737
2738int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2739{
2740 struct pch_udc_dev *dev = pch_udc;
2741
2742 if (!dev)
2743 return -ENODEV;
2744
2745 if (!driver || (driver != dev->driver)) {
2746 dev_err(&dev->pdev->dev,
2747 "%s: invalid driver parameter\n", __func__);
2748 return -EINVAL;
2749 }
2750
2751 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2752
2753 /* Assures that there are no pending requests with this driver */
2754 driver->disconnect(&dev->gadget);
2755 driver->unbind(&dev->gadget);
2756 dev->gadget.dev.driver = NULL;
2757 dev->driver = NULL;
2758 dev->connected = 0;
2759
2760 /* set SD */
2761 pch_udc_set_disconnect(dev);
2762 return 0;
2763}
2764EXPORT_SYMBOL(usb_gadget_unregister_driver);
2765
2766static void pch_udc_shutdown(struct pci_dev *pdev)
2767{
2768 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2769
2770 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2771 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2772
2773 /* disable the pullup so the host will think we're gone */
2774 pch_udc_set_disconnect(dev);
2775}
2776
2777static void pch_udc_remove(struct pci_dev *pdev)
2778{
2779 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2780
2781 /* gadget driver must not be registered */
2782 if (dev->driver)
2783 dev_err(&pdev->dev,
2784 "%s: gadget driver still bound!!!\n", __func__);
2785 /* dma pool cleanup */
2786 if (dev->data_requests)
2787 pci_pool_destroy(dev->data_requests);
2788
2789 if (dev->stp_requests) {
2790 /* cleanup DMA desc's for ep0in */
2791 if (dev->ep[UDC_EP0OUT_IDX].td_stp) {
2792 pci_pool_free(dev->stp_requests,
2793 dev->ep[UDC_EP0OUT_IDX].td_stp,
2794 dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
2795 }
2796 if (dev->ep[UDC_EP0OUT_IDX].td_data) {
2797 pci_pool_free(dev->stp_requests,
2798 dev->ep[UDC_EP0OUT_IDX].td_data,
2799 dev->ep[UDC_EP0OUT_IDX].td_data_phys);
2800 }
2801 pci_pool_destroy(dev->stp_requests);
2802 }
2803
2804 if (dev->dma_addr)
2805 dma_unmap_single(&dev->pdev->dev, dev->dma_addr,
2806 UDC_EP0OUT_BUFF_SIZE * 4, DMA_FROM_DEVICE);
2807 kfree(dev->ep0out_buf);
2808
2809 pch_udc_exit(dev);
2810
2811 if (dev->irq_registered)
2812 free_irq(pdev->irq, dev);
2813 if (dev->base_addr)
2814 iounmap(dev->base_addr);
2815 if (dev->mem_region)
2816 release_mem_region(dev->phys_addr,
2817 pci_resource_len(pdev, PCH_UDC_PCI_BAR));
2818 if (dev->active)
2819 pci_disable_device(pdev);
2820 if (dev->registered)
2821 device_unregister(&dev->gadget.dev);
2822 kfree(dev);
2823 pci_set_drvdata(pdev, NULL);
2824}
2825
2826#ifdef CONFIG_PM
2827static int pch_udc_suspend(struct pci_dev *pdev, pm_message_t state)
2828{
2829 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2830
2831 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2832 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2833
2834 pci_disable_device(pdev);
2835 pci_enable_wake(pdev, PCI_D3hot, 0);
2836
2837 if (pci_save_state(pdev)) {
2838 dev_err(&pdev->dev,
2839 "%s: could not save PCI config state\n", __func__);
2840 return -ENOMEM;
2841 }
2842 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2843 return 0;
2844}
2845
2846static int pch_udc_resume(struct pci_dev *pdev)
2847{
2848 int ret;
2849
2850 pci_set_power_state(pdev, PCI_D0);
2851 pci_restore_state(pdev);
2852 ret = pci_enable_device(pdev);
2853 if (ret) {
2854 dev_err(&pdev->dev, "%s: pci_enable_device failed\n", __func__);
2855 return ret;
2856 }
2857 pci_enable_wake(pdev, PCI_D3hot, 0);
2858 return 0;
2859}
2860#else
2861#define pch_udc_suspend NULL
2862#define pch_udc_resume NULL
2863#endif /* CONFIG_PM */
2864
2865static int pch_udc_probe(struct pci_dev *pdev,
2866 const struct pci_device_id *id)
2867{
2868 unsigned long resource;
2869 unsigned long len;
2870 int retval;
2871 struct pch_udc_dev *dev;
2872
2873 /* one udc only */
2874 if (pch_udc) {
2875 pr_err("%s: already probed\n", __func__);
2876 return -EBUSY;
2877 }
2878 /* init */
2879 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2880 if (!dev) {
2881 pr_err("%s: no memory for device structure\n", __func__);
2882 return -ENOMEM;
2883 }
2884 /* pci setup */
2885 if (pci_enable_device(pdev) < 0) {
2886 kfree(dev);
2887 pr_err("%s: pci_enable_device failed\n", __func__);
2888 return -ENODEV;
2889 }
2890 dev->active = 1;
2891 pci_set_drvdata(pdev, dev);
2892
2893 /* PCI resource allocation */
2894 resource = pci_resource_start(pdev, 1);
2895 len = pci_resource_len(pdev, 1);
2896
2897 if (!request_mem_region(resource, len, KBUILD_MODNAME)) {
2898 dev_err(&pdev->dev, "%s: pci device used already\n", __func__);
2899 retval = -EBUSY;
2900 goto finished;
2901 }
2902 dev->phys_addr = resource;
2903 dev->mem_region = 1;
2904
2905 dev->base_addr = ioremap_nocache(resource, len);
2906 if (!dev->base_addr) {
2907 pr_err("%s: device memory cannot be mapped\n", __func__);
2908 retval = -ENOMEM;
2909 goto finished;
2910 }
2911 if (!pdev->irq) {
2912 dev_err(&pdev->dev, "%s: irq not set\n", __func__);
2913 retval = -ENODEV;
2914 goto finished;
2915 }
2916 pch_udc = dev;
2917 /* initialize the hardware */
2918 if (pch_udc_pcd_init(dev))
2919 goto finished;
2920 if (request_irq(pdev->irq, pch_udc_isr, IRQF_SHARED, KBUILD_MODNAME,
2921 dev)) {
2922 dev_err(&pdev->dev, "%s: request_irq(%d) fail\n", __func__,
2923 pdev->irq);
2924 retval = -ENODEV;
2925 goto finished;
2926 }
2927 dev->irq = pdev->irq;
2928 dev->irq_registered = 1;
2929
2930 pci_set_master(pdev);
2931 pci_try_set_mwi(pdev);
2932
2933 /* device struct setup */
2934 spin_lock_init(&dev->lock);
2935 dev->pdev = pdev;
2936 dev->gadget.ops = &pch_udc_ops;
2937
2938 retval = init_dma_pools(dev);
2939 if (retval)
2940 goto finished;
2941
2942 dev_set_name(&dev->gadget.dev, "gadget");
2943 dev->gadget.dev.parent = &pdev->dev;
2944 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2945 dev->gadget.dev.release = gadget_release;
2946 dev->gadget.name = KBUILD_MODNAME;
2947 dev->gadget.is_dualspeed = 1;
2948
2949 retval = device_register(&dev->gadget.dev);
2950 if (retval)
2951 goto finished;
2952 dev->registered = 1;
2953
2954 /* Put the device in disconnected state till a driver is bound */
2955 pch_udc_set_disconnect(dev);
2956 return 0;
2957
2958finished:
2959 pch_udc_remove(pdev);
2960 return retval;
2961}
2962
2963static DEFINE_PCI_DEVICE_TABLE(pch_udc_pcidev_id) = {
2964 {
2965 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EG20T_UDC),
2966 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
2967 .class_mask = 0xffffffff,
2968 },
2969 {
2970 PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7213_IOH_UDC),
2971 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
2972 .class_mask = 0xffffffff,
2973 },
2974 { 0 },
2975};
2976
2977MODULE_DEVICE_TABLE(pci, pch_udc_pcidev_id);
2978
2979
2980static struct pci_driver pch_udc_driver = {
2981 .name = KBUILD_MODNAME,
2982 .id_table = pch_udc_pcidev_id,
2983 .probe = pch_udc_probe,
2984 .remove = pch_udc_remove,
2985 .suspend = pch_udc_suspend,
2986 .resume = pch_udc_resume,
2987 .shutdown = pch_udc_shutdown,
2988};
2989
2990static int __init pch_udc_pci_init(void)
2991{
2992 return pci_register_driver(&pch_udc_driver);
2993}
2994module_init(pch_udc_pci_init);
2995
2996static void __exit pch_udc_pci_exit(void)
2997{
2998 pci_unregister_driver(&pch_udc_driver);
2999}
3000module_exit(pch_udc_pci_exit);
3001
3002MODULE_DESCRIPTION("Intel EG20T USB Device Controller");
3003MODULE_AUTHOR("OKI SEMICONDUCTOR, <toshiharu-linux@dsn.okisemi.com>");
3004MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index cf241c371a71..271ef94668e7 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -126,36 +126,36 @@ static struct printer_dev usb_printer_gadget;
126#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */ 126#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
127#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */ 127#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
128 128
129/* Some systems will want different product identifers published in the 129/* Some systems will want different product identifiers published in the
130 * device descriptor, either numbers or strings or both. These string 130 * device descriptor, either numbers or strings or both. These string
131 * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 131 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
132 */ 132 */
133 133
134static ushort __initdata idVendor; 134static ushort idVendor;
135module_param(idVendor, ushort, S_IRUGO); 135module_param(idVendor, ushort, S_IRUGO);
136MODULE_PARM_DESC(idVendor, "USB Vendor ID"); 136MODULE_PARM_DESC(idVendor, "USB Vendor ID");
137 137
138static ushort __initdata idProduct; 138static ushort idProduct;
139module_param(idProduct, ushort, S_IRUGO); 139module_param(idProduct, ushort, S_IRUGO);
140MODULE_PARM_DESC(idProduct, "USB Product ID"); 140MODULE_PARM_DESC(idProduct, "USB Product ID");
141 141
142static ushort __initdata bcdDevice; 142static ushort bcdDevice;
143module_param(bcdDevice, ushort, S_IRUGO); 143module_param(bcdDevice, ushort, S_IRUGO);
144MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); 144MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
145 145
146static char *__initdata iManufacturer; 146static char *iManufacturer;
147module_param(iManufacturer, charp, S_IRUGO); 147module_param(iManufacturer, charp, S_IRUGO);
148MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); 148MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
149 149
150static char *__initdata iProduct; 150static char *iProduct;
151module_param(iProduct, charp, S_IRUGO); 151module_param(iProduct, charp, S_IRUGO);
152MODULE_PARM_DESC(iProduct, "USB Product string"); 152MODULE_PARM_DESC(iProduct, "USB Product string");
153 153
154static char *__initdata iSerialNum; 154static char *iSerialNum;
155module_param(iSerialNum, charp, S_IRUGO); 155module_param(iSerialNum, charp, S_IRUGO);
156MODULE_PARM_DESC(iSerialNum, "1"); 156MODULE_PARM_DESC(iSerialNum, "1");
157 157
158static char *__initdata iPNPstring; 158static char *iPNPstring;
159module_param(iPNPstring, charp, S_IRUGO); 159module_param(iPNPstring, charp, S_IRUGO);
160MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"); 160MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
161 161
@@ -884,7 +884,8 @@ static const struct file_operations printer_io_operations = {
884 .fsync = printer_fsync, 884 .fsync = printer_fsync,
885 .poll = printer_poll, 885 .poll = printer_poll,
886 .unlocked_ioctl = printer_ioctl, 886 .unlocked_ioctl = printer_ioctl,
887 .release = printer_close 887 .release = printer_close,
888 .llseek = noop_llseek,
888}; 889};
889 890
890/*-------------------------------------------------------------------------*/ 891/*-------------------------------------------------------------------------*/
@@ -1188,6 +1189,8 @@ printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1188 else if (gadget->a_alt_hnp_support) 1189 else if (gadget->a_alt_hnp_support)
1189 DBG(dev, "HNP needs a different root port\n"); 1190 DBG(dev, "HNP needs a different root port\n");
1190 value = printer_set_config(dev, wValue); 1191 value = printer_set_config(dev, wValue);
1192 if (!value)
1193 value = set_interface(dev, PRINTER_INTERFACE);
1191 break; 1194 break;
1192 case USB_REQ_GET_CONFIGURATION: 1195 case USB_REQ_GET_CONFIGURATION:
1193 if (ctrl->bRequestType != USB_DIR_IN) 1196 if (ctrl->bRequestType != USB_DIR_IN)
@@ -1347,7 +1350,7 @@ printer_unbind(struct usb_gadget *gadget)
1347 set_gadget_data(gadget, NULL); 1350 set_gadget_data(gadget, NULL);
1348} 1351}
1349 1352
1350static int __ref 1353static int __init
1351printer_bind(struct usb_gadget *gadget) 1354printer_bind(struct usb_gadget *gadget)
1352{ 1355{
1353 struct printer_dev *dev; 1356 struct printer_dev *dev;
@@ -1543,7 +1546,6 @@ static struct usb_gadget_driver printer_driver = {
1543 .speed = DEVSPEED, 1546 .speed = DEVSPEED,
1544 1547
1545 .function = (char *) driver_desc, 1548 .function = (char *) driver_desc,
1546 .bind = printer_bind,
1547 .unbind = printer_unbind, 1549 .unbind = printer_unbind,
1548 1550
1549 .setup = printer_setup, 1551 .setup = printer_setup,
@@ -1579,11 +1581,11 @@ init(void)
1579 return status; 1581 return status;
1580 } 1582 }
1581 1583
1582 status = usb_gadget_register_driver(&printer_driver); 1584 status = usb_gadget_probe_driver(&printer_driver, printer_bind);
1583 if (status) { 1585 if (status) {
1584 class_destroy(usb_gadget_class); 1586 class_destroy(usb_gadget_class);
1585 unregister_chrdev_region(g_printer_devno, 1); 1587 unregister_chrdev_region(g_printer_devno, 1);
1586 DBG(dev, "usb_gadget_register_driver %x\n", status); 1588 DBG(dev, "usb_gadget_probe_driver %x\n", status);
1587 } 1589 }
1588 1590
1589 return status; 1591 return status;
@@ -1596,13 +1598,12 @@ cleanup(void)
1596 int status; 1598 int status;
1597 1599
1598 mutex_lock(&usb_printer_gadget.lock_printer_io); 1600 mutex_lock(&usb_printer_gadget.lock_printer_io);
1599 class_destroy(usb_gadget_class);
1600 unregister_chrdev_region(g_printer_devno, 2);
1601
1602 status = usb_gadget_unregister_driver(&printer_driver); 1601 status = usb_gadget_unregister_driver(&printer_driver);
1603 if (status) 1602 if (status)
1604 ERROR(dev, "usb_gadget_unregister_driver %x\n", status); 1603 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1605 1604
1605 unregister_chrdev_region(g_printer_devno, 2);
1606 class_destroy(usb_gadget_class);
1606 mutex_unlock(&usb_printer_gadget.lock_printer_io); 1607 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1607} 1608}
1608module_exit(cleanup); 1609module_exit(cleanup);
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index be5fb34d9602..774545494cf2 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -46,6 +46,7 @@
46#include <linux/seq_file.h> 46#include <linux/seq_file.h>
47#include <linux/debugfs.h> 47#include <linux/debugfs.h>
48#include <linux/io.h> 48#include <linux/io.h>
49#include <linux/prefetch.h>
49 50
50#include <asm/byteorder.h> 51#include <asm/byteorder.h>
51#include <asm/dma.h> 52#include <asm/dma.h>
@@ -139,24 +140,6 @@ static const char ep0name [] = "ep0";
139static void pxa25x_ep_fifo_flush (struct usb_ep *ep); 140static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
140static void nuke (struct pxa25x_ep *, int status); 141static void nuke (struct pxa25x_ep *, int status);
141 142
142/* one GPIO should be used to detect VBUS from the host */
143static int is_vbus_present(void)
144{
145 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
146
147 if (gpio_is_valid(mach->gpio_vbus)) {
148 int value = gpio_get_value(mach->gpio_vbus);
149
150 if (mach->gpio_vbus_inverted)
151 return !value;
152 else
153 return !!value;
154 }
155 if (mach->udc_is_connected)
156 return mach->udc_is_connected();
157 return 1;
158}
159
160/* one GPIO should control a D+ pullup, so host sees this device (or not) */ 143/* one GPIO should control a D+ pullup, so host sees this device (or not) */
161static void pullup_off(void) 144static void pullup_off(void)
162{ 145{
@@ -1055,7 +1038,7 @@ udc_seq_show(struct seq_file *m, void *_d)
1055 "%s version: %s\nGadget driver: %s\nHost %s\n\n", 1038 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1056 driver_name, DRIVER_VERSION SIZE_STR "(pio)", 1039 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1057 dev->driver ? dev->driver->driver.name : "(none)", 1040 dev->driver ? dev->driver->driver.name : "(none)",
1058 is_vbus_present() ? "full speed" : "disconnected"); 1041 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
1059 1042
1060 /* registers for device and ep0 */ 1043 /* registers for device and ep0 */
1061 seq_printf(m, 1044 seq_printf(m,
@@ -1094,7 +1077,7 @@ udc_seq_show(struct seq_file *m, void *_d)
1094 (tmp & UDCCFR_ACM) ? " acm" : ""); 1077 (tmp & UDCCFR_ACM) ? " acm" : "");
1095 } 1078 }
1096 1079
1097 if (!is_vbus_present() || !dev->driver) 1080 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
1098 goto done; 1081 goto done;
1099 1082
1100 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n", 1083 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
@@ -1280,14 +1263,15 @@ static void udc_enable (struct pxa25x_udc *dev)
1280 * disconnect is reported. then a host may connect again, or 1263 * disconnect is reported. then a host may connect again, or
1281 * the driver might get unbound. 1264 * the driver might get unbound.
1282 */ 1265 */
1283int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1266int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1267 int (*bind)(struct usb_gadget *))
1284{ 1268{
1285 struct pxa25x_udc *dev = the_controller; 1269 struct pxa25x_udc *dev = the_controller;
1286 int retval; 1270 int retval;
1287 1271
1288 if (!driver 1272 if (!driver
1289 || driver->speed < USB_SPEED_FULL 1273 || driver->speed < USB_SPEED_FULL
1290 || !driver->bind 1274 || !bind
1291 || !driver->disconnect 1275 || !driver->disconnect
1292 || !driver->setup) 1276 || !driver->setup)
1293 return -EINVAL; 1277 return -EINVAL;
@@ -1308,7 +1292,7 @@ fail:
1308 dev->gadget.dev.driver = NULL; 1292 dev->gadget.dev.driver = NULL;
1309 return retval; 1293 return retval;
1310 } 1294 }
1311 retval = driver->bind(&dev->gadget); 1295 retval = bind(&dev->gadget);
1312 if (retval) { 1296 if (retval) {
1313 DMSG("bind to driver %s --> error %d\n", 1297 DMSG("bind to driver %s --> error %d\n",
1314 driver->driver.name, retval); 1298 driver->driver.name, retval);
@@ -1338,7 +1322,7 @@ fail:
1338bind_fail: 1322bind_fail:
1339 return retval; 1323 return retval;
1340} 1324}
1341EXPORT_SYMBOL(usb_gadget_register_driver); 1325EXPORT_SYMBOL(usb_gadget_probe_driver);
1342 1326
1343static void 1327static void
1344stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver) 1328stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
@@ -1434,14 +1418,6 @@ lubbock_vbus_irq(int irq, void *_dev)
1434 1418
1435#endif 1419#endif
1436 1420
1437static irqreturn_t udc_vbus_irq(int irq, void *_dev)
1438{
1439 struct pxa25x_udc *dev = _dev;
1440
1441 pxa25x_udc_vbus_session(&dev->gadget, is_vbus_present());
1442 return IRQ_HANDLED;
1443}
1444
1445 1421
1446/*-------------------------------------------------------------------------*/ 1422/*-------------------------------------------------------------------------*/
1447 1423
@@ -1765,12 +1741,9 @@ pxa25x_udc_irq(int irq, void *_dev)
1765 if (unlikely(udccr & UDCCR_SUSIR)) { 1741 if (unlikely(udccr & UDCCR_SUSIR)) {
1766 udc_ack_int_UDCCR(UDCCR_SUSIR); 1742 udc_ack_int_UDCCR(UDCCR_SUSIR);
1767 handled = 1; 1743 handled = 1;
1768 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present() 1744 DBG(DBG_VERBOSE, "USB suspend\n");
1769 ? "" : "+disconnect");
1770 1745
1771 if (!is_vbus_present()) 1746 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1772 stop_activity(dev, dev->driver);
1773 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
1774 && dev->driver 1747 && dev->driver
1775 && dev->driver->suspend) 1748 && dev->driver->suspend)
1776 dev->driver->suspend(&dev->gadget); 1749 dev->driver->suspend(&dev->gadget);
@@ -1785,8 +1758,7 @@ pxa25x_udc_irq(int irq, void *_dev)
1785 1758
1786 if (dev->gadget.speed != USB_SPEED_UNKNOWN 1759 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1787 && dev->driver 1760 && dev->driver
1788 && dev->driver->resume 1761 && dev->driver->resume)
1789 && is_vbus_present())
1790 dev->driver->resume(&dev->gadget); 1762 dev->driver->resume(&dev->gadget);
1791 } 1763 }
1792 1764
@@ -2136,7 +2108,7 @@ static struct pxa25x_udc memory = {
2136static int __init pxa25x_udc_probe(struct platform_device *pdev) 2108static int __init pxa25x_udc_probe(struct platform_device *pdev)
2137{ 2109{
2138 struct pxa25x_udc *dev = &memory; 2110 struct pxa25x_udc *dev = &memory;
2139 int retval, vbus_irq, irq; 2111 int retval, irq;
2140 u32 chiprev; 2112 u32 chiprev;
2141 2113
2142 /* insist on Intel/ARM/XScale */ 2114 /* insist on Intel/ARM/XScale */
@@ -2198,19 +2170,6 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2198 2170
2199 dev->transceiver = otg_get_transceiver(); 2171 dev->transceiver = otg_get_transceiver();
2200 2172
2201 if (gpio_is_valid(dev->mach->gpio_vbus)) {
2202 if ((retval = gpio_request(dev->mach->gpio_vbus,
2203 "pxa25x_udc GPIO VBUS"))) {
2204 dev_dbg(&pdev->dev,
2205 "can't get vbus gpio %d, err: %d\n",
2206 dev->mach->gpio_vbus, retval);
2207 goto err_gpio_vbus;
2208 }
2209 gpio_direction_input(dev->mach->gpio_vbus);
2210 vbus_irq = gpio_to_irq(dev->mach->gpio_vbus);
2211 } else
2212 vbus_irq = 0;
2213
2214 if (gpio_is_valid(dev->mach->gpio_pullup)) { 2173 if (gpio_is_valid(dev->mach->gpio_pullup)) {
2215 if ((retval = gpio_request(dev->mach->gpio_pullup, 2174 if ((retval = gpio_request(dev->mach->gpio_pullup,
2216 "pca25x_udc GPIO PULLUP"))) { 2175 "pca25x_udc GPIO PULLUP"))) {
@@ -2236,7 +2195,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2236 udc_disable(dev); 2195 udc_disable(dev);
2237 udc_reinit(dev); 2196 udc_reinit(dev);
2238 2197
2239 dev->vbus = !!is_vbus_present(); 2198 dev->vbus = 0;
2240 2199
2241 /* irq setup after old hardware state is cleaned up */ 2200 /* irq setup after old hardware state is cleaned up */
2242 retval = request_irq(irq, pxa25x_udc_irq, 2201 retval = request_irq(irq, pxa25x_udc_irq,
@@ -2257,7 +2216,6 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2257 if (retval != 0) { 2216 if (retval != 0) {
2258 pr_err("%s: can't get irq %i, err %d\n", 2217 pr_err("%s: can't get irq %i, err %d\n",
2259 driver_name, LUBBOCK_USB_DISC_IRQ, retval); 2218 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2260lubbock_fail0:
2261 goto err_irq_lub; 2219 goto err_irq_lub;
2262 } 2220 }
2263 retval = request_irq(LUBBOCK_USB_IRQ, 2221 retval = request_irq(LUBBOCK_USB_IRQ,
@@ -2267,39 +2225,24 @@ lubbock_fail0:
2267 if (retval != 0) { 2225 if (retval != 0) {
2268 pr_err("%s: can't get irq %i, err %d\n", 2226 pr_err("%s: can't get irq %i, err %d\n",
2269 driver_name, LUBBOCK_USB_IRQ, retval); 2227 driver_name, LUBBOCK_USB_IRQ, retval);
2270 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2271 goto lubbock_fail0; 2228 goto lubbock_fail0;
2272 } 2229 }
2273 } else 2230 } else
2274#endif 2231#endif
2275 if (vbus_irq) {
2276 retval = request_irq(vbus_irq, udc_vbus_irq,
2277 IRQF_DISABLED | IRQF_SAMPLE_RANDOM |
2278 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2279 driver_name, dev);
2280 if (retval != 0) {
2281 pr_err("%s: can't get irq %i, err %d\n",
2282 driver_name, vbus_irq, retval);
2283 goto err_vbus_irq;
2284 }
2285 }
2286 create_debug_files(dev); 2232 create_debug_files(dev);
2287 2233
2288 return 0; 2234 return 0;
2289 2235
2290 err_vbus_irq:
2291#ifdef CONFIG_ARCH_LUBBOCK 2236#ifdef CONFIG_ARCH_LUBBOCK
2237lubbock_fail0:
2292 free_irq(LUBBOCK_USB_DISC_IRQ, dev); 2238 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2293 err_irq_lub: 2239 err_irq_lub:
2294#endif
2295 free_irq(irq, dev); 2240 free_irq(irq, dev);
2241#endif
2296 err_irq1: 2242 err_irq1:
2297 if (gpio_is_valid(dev->mach->gpio_pullup)) 2243 if (gpio_is_valid(dev->mach->gpio_pullup))
2298 gpio_free(dev->mach->gpio_pullup); 2244 gpio_free(dev->mach->gpio_pullup);
2299 err_gpio_pullup: 2245 err_gpio_pullup:
2300 if (gpio_is_valid(dev->mach->gpio_vbus))
2301 gpio_free(dev->mach->gpio_vbus);
2302 err_gpio_vbus:
2303 if (dev->transceiver) { 2246 if (dev->transceiver) {
2304 otg_put_transceiver(dev->transceiver); 2247 otg_put_transceiver(dev->transceiver);
2305 dev->transceiver = NULL; 2248 dev->transceiver = NULL;
@@ -2336,10 +2279,6 @@ static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2336 free_irq(LUBBOCK_USB_IRQ, dev); 2279 free_irq(LUBBOCK_USB_IRQ, dev);
2337 } 2280 }
2338#endif 2281#endif
2339 if (gpio_is_valid(dev->mach->gpio_vbus)) {
2340 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2341 gpio_free(dev->mach->gpio_vbus);
2342 }
2343 if (gpio_is_valid(dev->mach->gpio_pullup)) 2282 if (gpio_is_valid(dev->mach->gpio_pullup))
2344 gpio_free(dev->mach->gpio_pullup); 2283 gpio_free(dev->mach->gpio_pullup);
2345 2284
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 980762453a9c..57607696735c 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -32,6 +32,7 @@
32#include <linux/irq.h> 32#include <linux/irq.h>
33#include <linux/gpio.h> 33#include <linux/gpio.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/prefetch.h>
35 36
36#include <asm/byteorder.h> 37#include <asm/byteorder.h>
37#include <mach/hardware.h> 38#include <mach/hardware.h>
@@ -602,7 +603,7 @@ static void inc_ep_stats_reqs(struct pxa_ep *ep, int is_in)
602/** 603/**
603 * inc_ep_stats_bytes - Update ep stats counts 604 * inc_ep_stats_bytes - Update ep stats counts
604 * @ep: physical endpoint 605 * @ep: physical endpoint
605 * @count: bytes transfered on endpoint 606 * @count: bytes transferred on endpoint
606 * @is_in: ep direction (USB_DIR_IN or 0) 607 * @is_in: ep direction (USB_DIR_IN or 0)
607 */ 608 */
608static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in) 609static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
@@ -877,7 +878,7 @@ static void nuke(struct pxa_ep *ep, int status)
877 * If there is less space in request than bytes received in OUT endpoint, 878 * If there is less space in request than bytes received in OUT endpoint,
878 * bytes are left in the OUT endpoint. 879 * bytes are left in the OUT endpoint.
879 * 880 *
880 * Returns how many bytes were actually transfered 881 * Returns how many bytes were actually transferred
881 */ 882 */
882static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req) 883static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
883{ 884{
@@ -914,7 +915,7 @@ static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
914 * endpoint. If there are no bytes to transfer, doesn't write anything 915 * endpoint. If there are no bytes to transfer, doesn't write anything
915 * to physical endpoint. 916 * to physical endpoint.
916 * 917 *
917 * Returns how many bytes were actually transfered. 918 * Returns how many bytes were actually transferred.
918 */ 919 */
919static int write_packet(struct pxa_ep *ep, struct pxa27x_request *req, 920static int write_packet(struct pxa_ep *ep, struct pxa27x_request *req,
920 unsigned int max) 921 unsigned int max)
@@ -991,7 +992,7 @@ static int read_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
991 * caller guarantees at least one packet buffer is ready (or a zlp). 992 * caller guarantees at least one packet buffer is ready (or a zlp).
992 * Doesn't complete the request, that's the caller's job 993 * Doesn't complete the request, that's the caller's job
993 * 994 *
994 * Returns 1 if request fully transfered, 0 if partial transfer 995 * Returns 1 if request fully transferred, 0 if partial transfer
995 */ 996 */
996static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req) 997static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
997{ 998{
@@ -1094,7 +1095,7 @@ static int read_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
1094 * Sends a request (or a part of the request) to the control endpoint (ep0 in). 1095 * Sends a request (or a part of the request) to the control endpoint (ep0 in).
1095 * If the request doesn't fit, the remaining part will be sent from irq. 1096 * If the request doesn't fit, the remaining part will be sent from irq.
1096 * The request is considered fully written only if either : 1097 * The request is considered fully written only if either :
1097 * - last write transfered all remaining bytes, but fifo was not fully filled 1098 * - last write transferred all remaining bytes, but fifo was not fully filled
1098 * - last write was a 0 length write 1099 * - last write was a 0 length write
1099 * 1100 *
1100 * Returns 1 if request fully written, 0 if request only partially sent 1101 * Returns 1 if request fully written, 0 if request only partially sent
@@ -1394,8 +1395,6 @@ static void pxa_ep_fifo_flush(struct usb_ep *_ep)
1394 } 1395 }
1395 1396
1396 spin_unlock_irqrestore(&ep->lock, flags); 1397 spin_unlock_irqrestore(&ep->lock, flags);
1397
1398 return;
1399} 1398}
1400 1399
1401/** 1400/**
@@ -1550,7 +1549,7 @@ static int pxa_udc_get_frame(struct usb_gadget *_gadget)
1550 * pxa_udc_wakeup - Force udc device out of suspend 1549 * pxa_udc_wakeup - Force udc device out of suspend
1551 * @_gadget: usb gadget 1550 * @_gadget: usb gadget
1552 * 1551 *
1553 * Returns 0 if successfull, error code otherwise 1552 * Returns 0 if successful, error code otherwise
1554 */ 1553 */
1555static int pxa_udc_wakeup(struct usb_gadget *_gadget) 1554static int pxa_udc_wakeup(struct usb_gadget *_gadget)
1556{ 1555{
@@ -1792,8 +1791,9 @@ static void udc_enable(struct pxa_udc *udc)
1792} 1791}
1793 1792
1794/** 1793/**
1795 * usb_gadget_register_driver - Register gadget driver 1794 * usb_gadget_probe_driver - Register gadget driver
1796 * @driver: gadget driver 1795 * @driver: gadget driver
1796 * @bind: bind function
1797 * 1797 *
1798 * When a driver is successfully registered, it will receive control requests 1798 * When a driver is successfully registered, it will receive control requests
1799 * including set_configuration(), which enables non-control requests. Then 1799 * including set_configuration(), which enables non-control requests. Then
@@ -1805,12 +1805,13 @@ static void udc_enable(struct pxa_udc *udc)
1805 * 1805 *
1806 * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise 1806 * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise
1807 */ 1807 */
1808int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1808int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1809 int (*bind)(struct usb_gadget *))
1809{ 1810{
1810 struct pxa_udc *udc = the_controller; 1811 struct pxa_udc *udc = the_controller;
1811 int retval; 1812 int retval;
1812 1813
1813 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind 1814 if (!driver || driver->speed < USB_SPEED_FULL || !bind
1814 || !driver->disconnect || !driver->setup) 1815 || !driver->disconnect || !driver->setup)
1815 return -EINVAL; 1816 return -EINVAL;
1816 if (!udc) 1817 if (!udc)
@@ -1828,7 +1829,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1828 dev_err(udc->dev, "device_add error %d\n", retval); 1829 dev_err(udc->dev, "device_add error %d\n", retval);
1829 goto add_fail; 1830 goto add_fail;
1830 } 1831 }
1831 retval = driver->bind(&udc->gadget); 1832 retval = bind(&udc->gadget);
1832 if (retval) { 1833 if (retval) {
1833 dev_err(udc->dev, "bind to driver %s --> error %d\n", 1834 dev_err(udc->dev, "bind to driver %s --> error %d\n",
1834 driver->driver.name, retval); 1835 driver->driver.name, retval);
@@ -1859,7 +1860,7 @@ add_fail:
1859 udc->gadget.dev.driver = NULL; 1860 udc->gadget.dev.driver = NULL;
1860 return retval; 1861 return retval;
1861} 1862}
1862EXPORT_SYMBOL(usb_gadget_register_driver); 1863EXPORT_SYMBOL(usb_gadget_probe_driver);
1863 1864
1864 1865
1865/** 1866/**
diff --git a/drivers/usb/gadget/r8a66597-udc.c b/drivers/usb/gadget/r8a66597-udc.c
index 2456ccd9965e..6dcc1f68fa60 100644
--- a/drivers/usb/gadget/r8a66597-udc.c
+++ b/drivers/usb/gadget/r8a66597-udc.c
@@ -42,6 +42,7 @@ static const char *r8a66597_ep_name[] = {
42 "ep8", "ep9", 42 "ep8", "ep9",
43}; 43};
44 44
45static void init_controller(struct r8a66597 *r8a66597);
45static void disable_controller(struct r8a66597 *r8a66597); 46static void disable_controller(struct r8a66597 *r8a66597);
46static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req); 47static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
47static void irq_packet_write(struct r8a66597_ep *ep, 48static void irq_packet_write(struct r8a66597_ep *ep,
@@ -104,6 +105,8 @@ __acquires(r8a66597->lock)
104 spin_lock(&r8a66597->lock); 105 spin_lock(&r8a66597->lock);
105 106
106 disable_controller(r8a66597); 107 disable_controller(r8a66597);
108 init_controller(r8a66597);
109 r8a66597_bset(r8a66597, VBSE, INTENB0);
107 INIT_LIST_HEAD(&r8a66597->ep[0].queue); 110 INIT_LIST_HEAD(&r8a66597->ep[0].queue);
108} 111}
109 112
@@ -255,7 +258,7 @@ static int pipe_buffer_setting(struct r8a66597 *r8a66597,
255 break; 258 break;
256 case R8A66597_BULK: 259 case R8A66597_BULK:
257 /* isochronous pipes may be used as bulk pipes */ 260 /* isochronous pipes may be used as bulk pipes */
258 if (info->pipe > R8A66597_BASE_PIPENUM_BULK) 261 if (info->pipe >= R8A66597_BASE_PIPENUM_BULK)
259 bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK; 262 bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
260 else 263 else
261 bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC; 264 bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
@@ -274,7 +277,7 @@ static int pipe_buffer_setting(struct r8a66597 *r8a66597,
274 } 277 }
275 278
276 if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) { 279 if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
277 pr_err(KERN_ERR "r8a66597 pipe memory is insufficient\n"); 280 pr_err("r8a66597 pipe memory is insufficient\n");
278 return -ENOMEM; 281 return -ENOMEM;
279 } 282 }
280 283
@@ -1080,7 +1083,9 @@ static void irq_device_state(struct r8a66597 *r8a66597)
1080 1083
1081 if (dvsq == DS_DFLT) { 1084 if (dvsq == DS_DFLT) {
1082 /* bus reset */ 1085 /* bus reset */
1086 spin_unlock(&r8a66597->lock);
1083 r8a66597->driver->disconnect(&r8a66597->gadget); 1087 r8a66597->driver->disconnect(&r8a66597->gadget);
1088 spin_lock(&r8a66597->lock);
1084 r8a66597_update_usb_speed(r8a66597); 1089 r8a66597_update_usb_speed(r8a66597);
1085 } 1090 }
1086 if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG) 1091 if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
@@ -1405,14 +1410,15 @@ static struct usb_ep_ops r8a66597_ep_ops = {
1405/*-------------------------------------------------------------------------*/ 1410/*-------------------------------------------------------------------------*/
1406static struct r8a66597 *the_controller; 1411static struct r8a66597 *the_controller;
1407 1412
1408int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1413int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1414 int (*bind)(struct usb_gadget *))
1409{ 1415{
1410 struct r8a66597 *r8a66597 = the_controller; 1416 struct r8a66597 *r8a66597 = the_controller;
1411 int retval; 1417 int retval;
1412 1418
1413 if (!driver 1419 if (!driver
1414 || driver->speed != USB_SPEED_HIGH 1420 || driver->speed != USB_SPEED_HIGH
1415 || !driver->bind 1421 || !bind
1416 || !driver->setup) 1422 || !driver->setup)
1417 return -EINVAL; 1423 return -EINVAL;
1418 if (!r8a66597) 1424 if (!r8a66597)
@@ -1431,7 +1437,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1431 goto error; 1437 goto error;
1432 } 1438 }
1433 1439
1434 retval = driver->bind(&r8a66597->gadget); 1440 retval = bind(&r8a66597->gadget);
1435 if (retval) { 1441 if (retval) {
1436 printk(KERN_ERR "bind to driver error (%d)\n", retval); 1442 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1437 device_del(&r8a66597->gadget.dev); 1443 device_del(&r8a66597->gadget.dev);
@@ -1456,7 +1462,7 @@ error:
1456 1462
1457 return retval; 1463 return retval;
1458} 1464}
1459EXPORT_SYMBOL(usb_gadget_register_driver); 1465EXPORT_SYMBOL(usb_gadget_probe_driver);
1460 1466
1461int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1467int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1462{ 1468{
diff --git a/drivers/usb/gadget/r8a66597-udc.h b/drivers/usb/gadget/r8a66597-udc.h
index f763b5190afa..5fc22e09a0f1 100644
--- a/drivers/usb/gadget/r8a66597-udc.h
+++ b/drivers/usb/gadget/r8a66597-udc.h
@@ -136,7 +136,7 @@ static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
136 int len) 136 int len)
137{ 137{
138 void __iomem *fifoaddr = r8a66597->reg + offset; 138 void __iomem *fifoaddr = r8a66597->reg + offset;
139 unsigned int data; 139 unsigned int data = 0;
140 int i; 140 int i;
141 141
142 if (r8a66597->pdata->on_chip) { 142 if (r8a66597->pdata->on_chip) {
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c
index 972d5ddd1e18..d3cdffea9c8a 100644
--- a/drivers/usb/gadget/rndis.c
+++ b/drivers/usb/gadget/rndis.c
@@ -61,17 +61,17 @@ MODULE_PARM_DESC (rndis_debug, "enable debugging");
61#define RNDIS_MAX_CONFIGS 1 61#define RNDIS_MAX_CONFIGS 1
62 62
63 63
64static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS]; 64static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
65 65
66/* Driver Version */ 66/* Driver Version */
67static const __le32 rndis_driver_version = cpu_to_le32 (1); 67static const __le32 rndis_driver_version = cpu_to_le32(1);
68 68
69/* Function Prototypes */ 69/* Function Prototypes */
70static rndis_resp_t *rndis_add_response (int configNr, u32 length); 70static rndis_resp_t *rndis_add_response(int configNr, u32 length);
71 71
72 72
73/* supported OIDs */ 73/* supported OIDs */
74static const u32 oid_supported_list [] = 74static const u32 oid_supported_list[] =
75{ 75{
76 /* the general stuff */ 76 /* the general stuff */
77 OID_GEN_SUPPORTED_LIST, 77 OID_GEN_SUPPORTED_LIST,
@@ -161,21 +161,20 @@ static const u32 oid_supported_list [] =
161 161
162 162
163/* NDIS Functions */ 163/* NDIS Functions */
164static int 164static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
165gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, 165 unsigned buf_len, rndis_resp_t *r)
166 rndis_resp_t *r)
167{ 166{
168 int retval = -ENOTSUPP; 167 int retval = -ENOTSUPP;
169 u32 length = 4; /* usually */ 168 u32 length = 4; /* usually */
170 __le32 *outbuf; 169 __le32 *outbuf;
171 int i, count; 170 int i, count;
172 rndis_query_cmplt_type *resp; 171 rndis_query_cmplt_type *resp;
173 struct net_device *net; 172 struct net_device *net;
174 struct rtnl_link_stats64 temp; 173 struct rtnl_link_stats64 temp;
175 const struct rtnl_link_stats64 *stats; 174 const struct rtnl_link_stats64 *stats;
176 175
177 if (!r) return -ENOMEM; 176 if (!r) return -ENOMEM;
178 resp = (rndis_query_cmplt_type *) r->buf; 177 resp = (rndis_query_cmplt_type *)r->buf;
179 178
180 if (!resp) return -ENOMEM; 179 if (!resp) return -ENOMEM;
181 180
@@ -191,8 +190,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
191 } 190 }
192 191
193 /* response goes here, right after the header */ 192 /* response goes here, right after the header */
194 outbuf = (__le32 *) &resp[1]; 193 outbuf = (__le32 *)&resp[1];
195 resp->InformationBufferOffset = cpu_to_le32 (16); 194 resp->InformationBufferOffset = cpu_to_le32(16);
196 195
197 net = rndis_per_dev_params[configNr].dev; 196 net = rndis_per_dev_params[configNr].dev;
198 stats = dev_get_stats(net, &temp); 197 stats = dev_get_stats(net, &temp);
@@ -204,10 +203,10 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
204 /* mandatory */ 203 /* mandatory */
205 case OID_GEN_SUPPORTED_LIST: 204 case OID_GEN_SUPPORTED_LIST:
206 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__); 205 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
207 length = sizeof (oid_supported_list); 206 length = sizeof(oid_supported_list);
208 count = length / sizeof (u32); 207 count = length / sizeof(u32);
209 for (i = 0; i < count; i++) 208 for (i = 0; i < count; i++)
210 outbuf[i] = cpu_to_le32 (oid_supported_list[i]); 209 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
211 retval = 0; 210 retval = 0;
212 break; 211 break;
213 212
@@ -220,14 +219,14 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
220 * reddite ergo quae sunt Caesaris Caesari 219 * reddite ergo quae sunt Caesaris Caesari
221 * et quae sunt Dei Deo! 220 * et quae sunt Dei Deo!
222 */ 221 */
223 *outbuf = cpu_to_le32 (0); 222 *outbuf = cpu_to_le32(0);
224 retval = 0; 223 retval = 0;
225 break; 224 break;
226 225
227 /* mandatory */ 226 /* mandatory */
228 case OID_GEN_MEDIA_SUPPORTED: 227 case OID_GEN_MEDIA_SUPPORTED:
229 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__); 228 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
230 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium); 229 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
231 retval = 0; 230 retval = 0;
232 break; 231 break;
233 232
@@ -235,16 +234,16 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
235 case OID_GEN_MEDIA_IN_USE: 234 case OID_GEN_MEDIA_IN_USE:
236 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__); 235 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
237 /* one medium, one transport... (maybe you do it better) */ 236 /* one medium, one transport... (maybe you do it better) */
238 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium); 237 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
239 retval = 0; 238 retval = 0;
240 break; 239 break;
241 240
242 /* mandatory */ 241 /* mandatory */
243 case OID_GEN_MAXIMUM_FRAME_SIZE: 242 case OID_GEN_MAXIMUM_FRAME_SIZE:
244 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__); 243 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
245 if (rndis_per_dev_params [configNr].dev) { 244 if (rndis_per_dev_params[configNr].dev) {
246 *outbuf = cpu_to_le32 ( 245 *outbuf = cpu_to_le32(
247 rndis_per_dev_params [configNr].dev->mtu); 246 rndis_per_dev_params[configNr].dev->mtu);
248 retval = 0; 247 retval = 0;
249 } 248 }
250 break; 249 break;
@@ -253,21 +252,21 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
253 case OID_GEN_LINK_SPEED: 252 case OID_GEN_LINK_SPEED:
254 if (rndis_debug > 1) 253 if (rndis_debug > 1)
255 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__); 254 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
256 if (rndis_per_dev_params [configNr].media_state 255 if (rndis_per_dev_params[configNr].media_state
257 == NDIS_MEDIA_STATE_DISCONNECTED) 256 == NDIS_MEDIA_STATE_DISCONNECTED)
258 *outbuf = cpu_to_le32 (0); 257 *outbuf = cpu_to_le32(0);
259 else 258 else
260 *outbuf = cpu_to_le32 ( 259 *outbuf = cpu_to_le32(
261 rndis_per_dev_params [configNr].speed); 260 rndis_per_dev_params[configNr].speed);
262 retval = 0; 261 retval = 0;
263 break; 262 break;
264 263
265 /* mandatory */ 264 /* mandatory */
266 case OID_GEN_TRANSMIT_BLOCK_SIZE: 265 case OID_GEN_TRANSMIT_BLOCK_SIZE:
267 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__); 266 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
268 if (rndis_per_dev_params [configNr].dev) { 267 if (rndis_per_dev_params[configNr].dev) {
269 *outbuf = cpu_to_le32 ( 268 *outbuf = cpu_to_le32(
270 rndis_per_dev_params [configNr].dev->mtu); 269 rndis_per_dev_params[configNr].dev->mtu);
271 retval = 0; 270 retval = 0;
272 } 271 }
273 break; 272 break;
@@ -275,9 +274,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
275 /* mandatory */ 274 /* mandatory */
276 case OID_GEN_RECEIVE_BLOCK_SIZE: 275 case OID_GEN_RECEIVE_BLOCK_SIZE:
277 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__); 276 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
278 if (rndis_per_dev_params [configNr].dev) { 277 if (rndis_per_dev_params[configNr].dev) {
279 *outbuf = cpu_to_le32 ( 278 *outbuf = cpu_to_le32(
280 rndis_per_dev_params [configNr].dev->mtu); 279 rndis_per_dev_params[configNr].dev->mtu);
281 retval = 0; 280 retval = 0;
282 } 281 }
283 break; 282 break;
@@ -285,18 +284,20 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
285 /* mandatory */ 284 /* mandatory */
286 case OID_GEN_VENDOR_ID: 285 case OID_GEN_VENDOR_ID:
287 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__); 286 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
288 *outbuf = cpu_to_le32 ( 287 *outbuf = cpu_to_le32(
289 rndis_per_dev_params [configNr].vendorID); 288 rndis_per_dev_params[configNr].vendorID);
290 retval = 0; 289 retval = 0;
291 break; 290 break;
292 291
293 /* mandatory */ 292 /* mandatory */
294 case OID_GEN_VENDOR_DESCRIPTION: 293 case OID_GEN_VENDOR_DESCRIPTION:
295 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__); 294 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
296 if ( rndis_per_dev_params [configNr].vendorDescr ) { 295 if (rndis_per_dev_params[configNr].vendorDescr) {
297 length = strlen (rndis_per_dev_params [configNr].vendorDescr); 296 length = strlen(rndis_per_dev_params[configNr].
298 memcpy (outbuf, 297 vendorDescr);
299 rndis_per_dev_params [configNr].vendorDescr, length); 298 memcpy(outbuf,
299 rndis_per_dev_params[configNr].vendorDescr,
300 length);
300 } else { 301 } else {
301 outbuf[0] = 0; 302 outbuf[0] = 0;
302 } 303 }
@@ -313,7 +314,7 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
313 /* mandatory */ 314 /* mandatory */
314 case OID_GEN_CURRENT_PACKET_FILTER: 315 case OID_GEN_CURRENT_PACKET_FILTER:
315 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__); 316 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
316 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter); 317 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
317 retval = 0; 318 retval = 0;
318 break; 319 break;
319 320
@@ -328,14 +329,14 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
328 case OID_GEN_MEDIA_CONNECT_STATUS: 329 case OID_GEN_MEDIA_CONNECT_STATUS:
329 if (rndis_debug > 1) 330 if (rndis_debug > 1)
330 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__); 331 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
331 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] 332 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
332 .media_state); 333 .media_state);
333 retval = 0; 334 retval = 0;
334 break; 335 break;
335 336
336 case OID_GEN_PHYSICAL_MEDIUM: 337 case OID_GEN_PHYSICAL_MEDIUM:
337 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__); 338 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
338 *outbuf = cpu_to_le32 (0); 339 *outbuf = cpu_to_le32(0);
339 retval = 0; 340 retval = 0;
340 break; 341 break;
341 342
@@ -409,10 +410,10 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
409 /* mandatory */ 410 /* mandatory */
410 case OID_802_3_PERMANENT_ADDRESS: 411 case OID_802_3_PERMANENT_ADDRESS:
411 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__); 412 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
412 if (rndis_per_dev_params [configNr].dev) { 413 if (rndis_per_dev_params[configNr].dev) {
413 length = ETH_ALEN; 414 length = ETH_ALEN;
414 memcpy (outbuf, 415 memcpy(outbuf,
415 rndis_per_dev_params [configNr].host_mac, 416 rndis_per_dev_params[configNr].host_mac,
416 length); 417 length);
417 retval = 0; 418 retval = 0;
418 } 419 }
@@ -421,9 +422,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
421 /* mandatory */ 422 /* mandatory */
422 case OID_802_3_CURRENT_ADDRESS: 423 case OID_802_3_CURRENT_ADDRESS:
423 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__); 424 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
424 if (rndis_per_dev_params [configNr].dev) { 425 if (rndis_per_dev_params[configNr].dev) {
425 length = ETH_ALEN; 426 length = ETH_ALEN;
426 memcpy (outbuf, 427 memcpy(outbuf,
427 rndis_per_dev_params [configNr].host_mac, 428 rndis_per_dev_params [configNr].host_mac,
428 length); 429 length);
429 retval = 0; 430 retval = 0;
@@ -434,7 +435,7 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
434 case OID_802_3_MULTICAST_LIST: 435 case OID_802_3_MULTICAST_LIST:
435 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__); 436 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
436 /* Multicast base address only */ 437 /* Multicast base address only */
437 *outbuf = cpu_to_le32 (0xE0000000); 438 *outbuf = cpu_to_le32(0xE0000000);
438 retval = 0; 439 retval = 0;
439 break; 440 break;
440 441
@@ -442,7 +443,7 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
442 case OID_802_3_MAXIMUM_LIST_SIZE: 443 case OID_802_3_MAXIMUM_LIST_SIZE:
443 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__); 444 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
444 /* Multicast base address only */ 445 /* Multicast base address only */
445 *outbuf = cpu_to_le32 (1); 446 *outbuf = cpu_to_le32(1);
446 retval = 0; 447 retval = 0;
447 break; 448 break;
448 449
@@ -466,14 +467,14 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
466 /* mandatory */ 467 /* mandatory */
467 case OID_802_3_XMIT_ONE_COLLISION: 468 case OID_802_3_XMIT_ONE_COLLISION:
468 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__); 469 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
469 *outbuf = cpu_to_le32 (0); 470 *outbuf = cpu_to_le32(0);
470 retval = 0; 471 retval = 0;
471 break; 472 break;
472 473
473 /* mandatory */ 474 /* mandatory */
474 case OID_802_3_XMIT_MORE_COLLISIONS: 475 case OID_802_3_XMIT_MORE_COLLISIONS:
475 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__); 476 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
476 *outbuf = cpu_to_le32 (0); 477 *outbuf = cpu_to_le32(0);
477 retval = 0; 478 retval = 0;
478 break; 479 break;
479 480
@@ -484,22 +485,22 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
484 if (retval < 0) 485 if (retval < 0)
485 length = 0; 486 length = 0;
486 487
487 resp->InformationBufferLength = cpu_to_le32 (length); 488 resp->InformationBufferLength = cpu_to_le32(length);
488 r->length = length + sizeof *resp; 489 r->length = length + sizeof(*resp);
489 resp->MessageLength = cpu_to_le32 (r->length); 490 resp->MessageLength = cpu_to_le32(r->length);
490 return retval; 491 return retval;
491} 492}
492 493
493static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len, 494static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
494 rndis_resp_t *r) 495 rndis_resp_t *r)
495{ 496{
496 rndis_set_cmplt_type *resp; 497 rndis_set_cmplt_type *resp;
497 int i, retval = -ENOTSUPP; 498 int i, retval = -ENOTSUPP;
498 struct rndis_params *params; 499 struct rndis_params *params;
499 500
500 if (!r) 501 if (!r)
501 return -ENOMEM; 502 return -ENOMEM;
502 resp = (rndis_set_cmplt_type *) r->buf; 503 resp = (rndis_set_cmplt_type *)r->buf;
503 if (!resp) 504 if (!resp)
504 return -ENOMEM; 505 return -ENOMEM;
505 506
@@ -514,7 +515,7 @@ static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
514 } 515 }
515 } 516 }
516 517
517 params = &rndis_per_dev_params [configNr]; 518 params = &rndis_per_dev_params[configNr];
518 switch (OID) { 519 switch (OID) {
519 case OID_GEN_CURRENT_PACKET_FILTER: 520 case OID_GEN_CURRENT_PACKET_FILTER:
520 521
@@ -537,11 +538,11 @@ static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
537 params->state = RNDIS_DATA_INITIALIZED; 538 params->state = RNDIS_DATA_INITIALIZED;
538 netif_carrier_on(params->dev); 539 netif_carrier_on(params->dev);
539 if (netif_running(params->dev)) 540 if (netif_running(params->dev))
540 netif_wake_queue (params->dev); 541 netif_wake_queue(params->dev);
541 } else { 542 } else {
542 params->state = RNDIS_INITIALIZED; 543 params->state = RNDIS_INITIALIZED;
543 netif_carrier_off (params->dev); 544 netif_carrier_off(params->dev);
544 netif_stop_queue (params->dev); 545 netif_stop_queue(params->dev);
545 } 546 }
546 break; 547 break;
547 548
@@ -563,48 +564,47 @@ static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
563 * Response Functions 564 * Response Functions
564 */ 565 */
565 566
566static int rndis_init_response (int configNr, rndis_init_msg_type *buf) 567static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
567{ 568{
568 rndis_init_cmplt_type *resp; 569 rndis_init_cmplt_type *resp;
569 rndis_resp_t *r; 570 rndis_resp_t *r;
570 struct rndis_params *params = rndis_per_dev_params + configNr; 571 struct rndis_params *params = rndis_per_dev_params + configNr;
571 572
572 if (!params->dev) 573 if (!params->dev)
573 return -ENOTSUPP; 574 return -ENOTSUPP;
574 575
575 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type)); 576 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
576 if (!r) 577 if (!r)
577 return -ENOMEM; 578 return -ENOMEM;
578 resp = (rndis_init_cmplt_type *) r->buf; 579 resp = (rndis_init_cmplt_type *)r->buf;
579 580
580 resp->MessageType = cpu_to_le32 ( 581 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
581 REMOTE_NDIS_INITIALIZE_CMPLT); 582 resp->MessageLength = cpu_to_le32(52);
582 resp->MessageLength = cpu_to_le32 (52);
583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
584 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS); 584 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
585 resp->MajorVersion = cpu_to_le32 (RNDIS_MAJOR_VERSION); 585 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
586 resp->MinorVersion = cpu_to_le32 (RNDIS_MINOR_VERSION); 586 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
587 resp->DeviceFlags = cpu_to_le32 (RNDIS_DF_CONNECTIONLESS); 587 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
588 resp->Medium = cpu_to_le32 (RNDIS_MEDIUM_802_3); 588 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
589 resp->MaxPacketsPerTransfer = cpu_to_le32 (1); 589 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
590 resp->MaxTransferSize = cpu_to_le32 ( 590 resp->MaxTransferSize = cpu_to_le32(
591 params->dev->mtu 591 params->dev->mtu
592 + sizeof (struct ethhdr) 592 + sizeof(struct ethhdr)
593 + sizeof (struct rndis_packet_msg_type) 593 + sizeof(struct rndis_packet_msg_type)
594 + 22); 594 + 22);
595 resp->PacketAlignmentFactor = cpu_to_le32 (0); 595 resp->PacketAlignmentFactor = cpu_to_le32(0);
596 resp->AFListOffset = cpu_to_le32 (0); 596 resp->AFListOffset = cpu_to_le32(0);
597 resp->AFListSize = cpu_to_le32 (0); 597 resp->AFListSize = cpu_to_le32(0);
598 598
599 params->resp_avail(params->v); 599 params->resp_avail(params->v);
600 return 0; 600 return 0;
601} 601}
602 602
603static int rndis_query_response (int configNr, rndis_query_msg_type *buf) 603static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
604{ 604{
605 rndis_query_cmplt_type *resp; 605 rndis_query_cmplt_type *resp;
606 rndis_resp_t *r; 606 rndis_resp_t *r;
607 struct rndis_params *params = rndis_per_dev_params + configNr; 607 struct rndis_params *params = rndis_per_dev_params + configNr;
608 608
609 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */ 609 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
610 if (!params->dev) 610 if (!params->dev)
@@ -616,47 +616,46 @@ static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
616 * rndis_query_cmplt_type followed by data. 616 * rndis_query_cmplt_type followed by data.
617 * oid_supported_list is the largest data reply 617 * oid_supported_list is the largest data reply
618 */ 618 */
619 r = rndis_add_response (configNr, 619 r = rndis_add_response(configNr,
620 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type)); 620 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
621 if (!r) 621 if (!r)
622 return -ENOMEM; 622 return -ENOMEM;
623 resp = (rndis_query_cmplt_type *) r->buf; 623 resp = (rndis_query_cmplt_type *)r->buf;
624 624
625 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT); 625 resp->MessageType = cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
626 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 626 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
627 627
628 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID), 628 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
629 le32_to_cpu(buf->InformationBufferOffset) 629 le32_to_cpu(buf->InformationBufferOffset)
630 + 8 + (u8 *) buf, 630 + 8 + (u8 *)buf,
631 le32_to_cpu(buf->InformationBufferLength), 631 le32_to_cpu(buf->InformationBufferLength),
632 r)) { 632 r)) {
633 /* OID not supported */ 633 /* OID not supported */
634 resp->Status = cpu_to_le32 ( 634 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
635 RNDIS_STATUS_NOT_SUPPORTED); 635 resp->MessageLength = cpu_to_le32(sizeof *resp);
636 resp->MessageLength = cpu_to_le32 (sizeof *resp); 636 resp->InformationBufferLength = cpu_to_le32(0);
637 resp->InformationBufferLength = cpu_to_le32 (0); 637 resp->InformationBufferOffset = cpu_to_le32(0);
638 resp->InformationBufferOffset = cpu_to_le32 (0);
639 } else 638 } else
640 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS); 639 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
641 640
642 params->resp_avail(params->v); 641 params->resp_avail(params->v);
643 return 0; 642 return 0;
644} 643}
645 644
646static int rndis_set_response (int configNr, rndis_set_msg_type *buf) 645static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
647{ 646{
648 u32 BufLength, BufOffset; 647 u32 BufLength, BufOffset;
649 rndis_set_cmplt_type *resp; 648 rndis_set_cmplt_type *resp;
650 rndis_resp_t *r; 649 rndis_resp_t *r;
651 struct rndis_params *params = rndis_per_dev_params + configNr; 650 struct rndis_params *params = rndis_per_dev_params + configNr;
652 651
653 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type)); 652 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
654 if (!r) 653 if (!r)
655 return -ENOMEM; 654 return -ENOMEM;
656 resp = (rndis_set_cmplt_type *) r->buf; 655 resp = (rndis_set_cmplt_type *)r->buf;
657 656
658 BufLength = le32_to_cpu (buf->InformationBufferLength); 657 BufLength = le32_to_cpu(buf->InformationBufferLength);
659 BufOffset = le32_to_cpu (buf->InformationBufferOffset); 658 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
660 659
661#ifdef VERBOSE_DEBUG 660#ifdef VERBOSE_DEBUG
662 pr_debug("%s: Length: %d\n", __func__, BufLength); 661 pr_debug("%s: Length: %d\n", __func__, BufLength);
@@ -670,59 +669,59 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
670 pr_debug("\n"); 669 pr_debug("\n");
671#endif 670#endif
672 671
673 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_SET_CMPLT); 672 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
674 resp->MessageLength = cpu_to_le32 (16); 673 resp->MessageLength = cpu_to_le32(16);
675 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 674 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
676 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID), 675 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
677 ((u8 *) buf) + 8 + BufOffset, BufLength, r)) 676 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
678 resp->Status = cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED); 677 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
679 else 678 else
680 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS); 679 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
681 680
682 params->resp_avail(params->v); 681 params->resp_avail(params->v);
683 return 0; 682 return 0;
684} 683}
685 684
686static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf) 685static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
687{ 686{
688 rndis_reset_cmplt_type *resp; 687 rndis_reset_cmplt_type *resp;
689 rndis_resp_t *r; 688 rndis_resp_t *r;
690 struct rndis_params *params = rndis_per_dev_params + configNr; 689 struct rndis_params *params = rndis_per_dev_params + configNr;
691 690
692 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type)); 691 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
693 if (!r) 692 if (!r)
694 return -ENOMEM; 693 return -ENOMEM;
695 resp = (rndis_reset_cmplt_type *) r->buf; 694 resp = (rndis_reset_cmplt_type *)r->buf;
696 695
697 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT); 696 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
698 resp->MessageLength = cpu_to_le32 (16); 697 resp->MessageLength = cpu_to_le32(16);
699 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS); 698 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
700 /* resent information */ 699 /* resent information */
701 resp->AddressingReset = cpu_to_le32 (1); 700 resp->AddressingReset = cpu_to_le32(1);
702 701
703 params->resp_avail(params->v); 702 params->resp_avail(params->v);
704 return 0; 703 return 0;
705} 704}
706 705
707static int rndis_keepalive_response (int configNr, 706static int rndis_keepalive_response(int configNr,
708 rndis_keepalive_msg_type *buf) 707 rndis_keepalive_msg_type *buf)
709{ 708{
710 rndis_keepalive_cmplt_type *resp; 709 rndis_keepalive_cmplt_type *resp;
711 rndis_resp_t *r; 710 rndis_resp_t *r;
712 struct rndis_params *params = rndis_per_dev_params + configNr; 711 struct rndis_params *params = rndis_per_dev_params + configNr;
713 712
714 /* host "should" check only in RNDIS_DATA_INITIALIZED state */ 713 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
715 714
716 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type)); 715 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
717 if (!r) 716 if (!r)
718 return -ENOMEM; 717 return -ENOMEM;
719 resp = (rndis_keepalive_cmplt_type *) r->buf; 718 resp = (rndis_keepalive_cmplt_type *)r->buf;
720 719
721 resp->MessageType = cpu_to_le32 ( 720 resp->MessageType = cpu_to_le32(
722 REMOTE_NDIS_KEEPALIVE_CMPLT); 721 REMOTE_NDIS_KEEPALIVE_CMPLT);
723 resp->MessageLength = cpu_to_le32 (16); 722 resp->MessageLength = cpu_to_le32(16);
724 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 723 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
725 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS); 724 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
726 725
727 params->resp_avail(params->v); 726 params->resp_avail(params->v);
728 return 0; 727 return 0;
@@ -732,86 +731,85 @@ static int rndis_keepalive_response (int configNr,
732/* 731/*
733 * Device to Host Comunication 732 * Device to Host Comunication
734 */ 733 */
735static int rndis_indicate_status_msg (int configNr, u32 status) 734static int rndis_indicate_status_msg(int configNr, u32 status)
736{ 735{
737 rndis_indicate_status_msg_type *resp; 736 rndis_indicate_status_msg_type *resp;
738 rndis_resp_t *r; 737 rndis_resp_t *r;
739 struct rndis_params *params = rndis_per_dev_params + configNr; 738 struct rndis_params *params = rndis_per_dev_params + configNr;
740 739
741 if (params->state == RNDIS_UNINITIALIZED) 740 if (params->state == RNDIS_UNINITIALIZED)
742 return -ENOTSUPP; 741 return -ENOTSUPP;
743 742
744 r = rndis_add_response (configNr, 743 r = rndis_add_response(configNr,
745 sizeof (rndis_indicate_status_msg_type)); 744 sizeof(rndis_indicate_status_msg_type));
746 if (!r) 745 if (!r)
747 return -ENOMEM; 746 return -ENOMEM;
748 resp = (rndis_indicate_status_msg_type *) r->buf; 747 resp = (rndis_indicate_status_msg_type *)r->buf;
749 748
750 resp->MessageType = cpu_to_le32 ( 749 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
751 REMOTE_NDIS_INDICATE_STATUS_MSG); 750 resp->MessageLength = cpu_to_le32(20);
752 resp->MessageLength = cpu_to_le32 (20); 751 resp->Status = cpu_to_le32(status);
753 resp->Status = cpu_to_le32 (status); 752 resp->StatusBufferLength = cpu_to_le32(0);
754 resp->StatusBufferLength = cpu_to_le32 (0); 753 resp->StatusBufferOffset = cpu_to_le32(0);
755 resp->StatusBufferOffset = cpu_to_le32 (0);
756 754
757 params->resp_avail(params->v); 755 params->resp_avail(params->v);
758 return 0; 756 return 0;
759} 757}
760 758
761int rndis_signal_connect (int configNr) 759int rndis_signal_connect(int configNr)
762{ 760{
763 rndis_per_dev_params [configNr].media_state 761 rndis_per_dev_params[configNr].media_state
764 = NDIS_MEDIA_STATE_CONNECTED; 762 = NDIS_MEDIA_STATE_CONNECTED;
765 return rndis_indicate_status_msg (configNr, 763 return rndis_indicate_status_msg(configNr,
766 RNDIS_STATUS_MEDIA_CONNECT); 764 RNDIS_STATUS_MEDIA_CONNECT);
767} 765}
768 766
769int rndis_signal_disconnect (int configNr) 767int rndis_signal_disconnect(int configNr)
770{ 768{
771 rndis_per_dev_params [configNr].media_state 769 rndis_per_dev_params[configNr].media_state
772 = NDIS_MEDIA_STATE_DISCONNECTED; 770 = NDIS_MEDIA_STATE_DISCONNECTED;
773 return rndis_indicate_status_msg (configNr, 771 return rndis_indicate_status_msg(configNr,
774 RNDIS_STATUS_MEDIA_DISCONNECT); 772 RNDIS_STATUS_MEDIA_DISCONNECT);
775} 773}
776 774
777void rndis_uninit (int configNr) 775void rndis_uninit(int configNr)
778{ 776{
779 u8 *buf; 777 u8 *buf;
780 u32 length; 778 u32 length;
781 779
782 if (configNr >= RNDIS_MAX_CONFIGS) 780 if (configNr >= RNDIS_MAX_CONFIGS)
783 return; 781 return;
784 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED; 782 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
785 783
786 /* drain the response queue */ 784 /* drain the response queue */
787 while ((buf = rndis_get_next_response(configNr, &length))) 785 while ((buf = rndis_get_next_response(configNr, &length)))
788 rndis_free_response(configNr, buf); 786 rndis_free_response(configNr, buf);
789} 787}
790 788
791void rndis_set_host_mac (int configNr, const u8 *addr) 789void rndis_set_host_mac(int configNr, const u8 *addr)
792{ 790{
793 rndis_per_dev_params [configNr].host_mac = addr; 791 rndis_per_dev_params[configNr].host_mac = addr;
794} 792}
795 793
796/* 794/*
797 * Message Parser 795 * Message Parser
798 */ 796 */
799int rndis_msg_parser (u8 configNr, u8 *buf) 797int rndis_msg_parser(u8 configNr, u8 *buf)
800{ 798{
801 u32 MsgType, MsgLength; 799 u32 MsgType, MsgLength;
802 __le32 *tmp; 800 __le32 *tmp;
803 struct rndis_params *params; 801 struct rndis_params *params;
804 802
805 if (!buf) 803 if (!buf)
806 return -ENOMEM; 804 return -ENOMEM;
807 805
808 tmp = (__le32 *) buf; 806 tmp = (__le32 *)buf;
809 MsgType = get_unaligned_le32(tmp++); 807 MsgType = get_unaligned_le32(tmp++);
810 MsgLength = get_unaligned_le32(tmp++); 808 MsgLength = get_unaligned_le32(tmp++);
811 809
812 if (configNr >= RNDIS_MAX_CONFIGS) 810 if (configNr >= RNDIS_MAX_CONFIGS)
813 return -ENOTSUPP; 811 return -ENOTSUPP;
814 params = &rndis_per_dev_params [configNr]; 812 params = &rndis_per_dev_params[configNr];
815 813
816 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for 814 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
817 * rx/tx statistics and link status, in addition to KEEPALIVE traffic 815 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
@@ -822,41 +820,41 @@ int rndis_msg_parser (u8 configNr, u8 *buf)
822 switch (MsgType) { 820 switch (MsgType) {
823 case REMOTE_NDIS_INITIALIZE_MSG: 821 case REMOTE_NDIS_INITIALIZE_MSG:
824 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", 822 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
825 __func__ ); 823 __func__);
826 params->state = RNDIS_INITIALIZED; 824 params->state = RNDIS_INITIALIZED;
827 return rndis_init_response (configNr, 825 return rndis_init_response(configNr,
828 (rndis_init_msg_type *) buf); 826 (rndis_init_msg_type *)buf);
829 827
830 case REMOTE_NDIS_HALT_MSG: 828 case REMOTE_NDIS_HALT_MSG:
831 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n", 829 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
832 __func__ ); 830 __func__);
833 params->state = RNDIS_UNINITIALIZED; 831 params->state = RNDIS_UNINITIALIZED;
834 if (params->dev) { 832 if (params->dev) {
835 netif_carrier_off (params->dev); 833 netif_carrier_off(params->dev);
836 netif_stop_queue (params->dev); 834 netif_stop_queue(params->dev);
837 } 835 }
838 return 0; 836 return 0;
839 837
840 case REMOTE_NDIS_QUERY_MSG: 838 case REMOTE_NDIS_QUERY_MSG:
841 return rndis_query_response (configNr, 839 return rndis_query_response(configNr,
842 (rndis_query_msg_type *) buf); 840 (rndis_query_msg_type *)buf);
843 841
844 case REMOTE_NDIS_SET_MSG: 842 case REMOTE_NDIS_SET_MSG:
845 return rndis_set_response (configNr, 843 return rndis_set_response(configNr,
846 (rndis_set_msg_type *) buf); 844 (rndis_set_msg_type *)buf);
847 845
848 case REMOTE_NDIS_RESET_MSG: 846 case REMOTE_NDIS_RESET_MSG:
849 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n", 847 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
850 __func__ ); 848 __func__);
851 return rndis_reset_response (configNr, 849 return rndis_reset_response(configNr,
852 (rndis_reset_msg_type *) buf); 850 (rndis_reset_msg_type *)buf);
853 851
854 case REMOTE_NDIS_KEEPALIVE_MSG: 852 case REMOTE_NDIS_KEEPALIVE_MSG:
855 /* For USB: host does this every 5 seconds */ 853 /* For USB: host does this every 5 seconds */
856 if (rndis_debug > 1) 854 if (rndis_debug > 1)
857 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", 855 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
858 __func__ ); 856 __func__);
859 return rndis_keepalive_response (configNr, 857 return rndis_keepalive_response(configNr,
860 (rndis_keepalive_msg_type *) 858 (rndis_keepalive_msg_type *)
861 buf); 859 buf);
862 860
@@ -866,7 +864,7 @@ int rndis_msg_parser (u8 configNr, u8 *buf)
866 * suspending itself. 864 * suspending itself.
867 */ 865 */
868 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n", 866 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
869 __func__ , MsgType, MsgLength); 867 __func__, MsgType, MsgLength);
870 { 868 {
871 unsigned i; 869 unsigned i;
872 for (i = 0; i < MsgLength; i += 16) { 870 for (i = 0; i < MsgLength; i += 16) {
@@ -901,10 +899,10 @@ int rndis_register(void (*resp_avail)(void *v), void *v)
901 return -EINVAL; 899 return -EINVAL;
902 900
903 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) { 901 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
904 if (!rndis_per_dev_params [i].used) { 902 if (!rndis_per_dev_params[i].used) {
905 rndis_per_dev_params [i].used = 1; 903 rndis_per_dev_params[i].used = 1;
906 rndis_per_dev_params [i].resp_avail = resp_avail; 904 rndis_per_dev_params[i].resp_avail = resp_avail;
907 rndis_per_dev_params [i].v = v; 905 rndis_per_dev_params[i].v = v;
908 pr_debug("%s: configNr = %d\n", __func__, i); 906 pr_debug("%s: configNr = %d\n", __func__, i);
909 return i; 907 return i;
910 } 908 }
@@ -914,14 +912,12 @@ int rndis_register(void (*resp_avail)(void *v), void *v)
914 return -ENODEV; 912 return -ENODEV;
915} 913}
916 914
917void rndis_deregister (int configNr) 915void rndis_deregister(int configNr)
918{ 916{
919 pr_debug("%s: \n", __func__); 917 pr_debug("%s:\n", __func__);
920 918
921 if (configNr >= RNDIS_MAX_CONFIGS) return; 919 if (configNr >= RNDIS_MAX_CONFIGS) return;
922 rndis_per_dev_params [configNr].used = 0; 920 rndis_per_dev_params[configNr].used = 0;
923
924 return;
925} 921}
926 922
927int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter) 923int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
@@ -931,76 +927,76 @@ int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
931 return -EINVAL; 927 return -EINVAL;
932 if (configNr >= RNDIS_MAX_CONFIGS) return -1; 928 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
933 929
934 rndis_per_dev_params [configNr].dev = dev; 930 rndis_per_dev_params[configNr].dev = dev;
935 rndis_per_dev_params [configNr].filter = cdc_filter; 931 rndis_per_dev_params[configNr].filter = cdc_filter;
936 932
937 return 0; 933 return 0;
938} 934}
939 935
940int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr) 936int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
941{ 937{
942 pr_debug("%s:\n", __func__); 938 pr_debug("%s:\n", __func__);
943 if (!vendorDescr) return -1; 939 if (!vendorDescr) return -1;
944 if (configNr >= RNDIS_MAX_CONFIGS) return -1; 940 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
945 941
946 rndis_per_dev_params [configNr].vendorID = vendorID; 942 rndis_per_dev_params[configNr].vendorID = vendorID;
947 rndis_per_dev_params [configNr].vendorDescr = vendorDescr; 943 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
948 944
949 return 0; 945 return 0;
950} 946}
951 947
952int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed) 948int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
953{ 949{
954 pr_debug("%s: %u %u\n", __func__, medium, speed); 950 pr_debug("%s: %u %u\n", __func__, medium, speed);
955 if (configNr >= RNDIS_MAX_CONFIGS) return -1; 951 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
956 952
957 rndis_per_dev_params [configNr].medium = medium; 953 rndis_per_dev_params[configNr].medium = medium;
958 rndis_per_dev_params [configNr].speed = speed; 954 rndis_per_dev_params[configNr].speed = speed;
959 955
960 return 0; 956 return 0;
961} 957}
962 958
963void rndis_add_hdr (struct sk_buff *skb) 959void rndis_add_hdr(struct sk_buff *skb)
964{ 960{
965 struct rndis_packet_msg_type *header; 961 struct rndis_packet_msg_type *header;
966 962
967 if (!skb) 963 if (!skb)
968 return; 964 return;
969 header = (void *) skb_push (skb, sizeof *header); 965 header = (void *)skb_push(skb, sizeof(*header));
970 memset (header, 0, sizeof *header); 966 memset(header, 0, sizeof *header);
971 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG); 967 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
972 header->MessageLength = cpu_to_le32(skb->len); 968 header->MessageLength = cpu_to_le32(skb->len);
973 header->DataOffset = cpu_to_le32 (36); 969 header->DataOffset = cpu_to_le32(36);
974 header->DataLength = cpu_to_le32(skb->len - sizeof *header); 970 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
975} 971}
976 972
977void rndis_free_response (int configNr, u8 *buf) 973void rndis_free_response(int configNr, u8 *buf)
978{ 974{
979 rndis_resp_t *r; 975 rndis_resp_t *r;
980 struct list_head *act, *tmp; 976 struct list_head *act, *tmp;
981 977
982 list_for_each_safe (act, tmp, 978 list_for_each_safe(act, tmp,
983 &(rndis_per_dev_params [configNr].resp_queue)) 979 &(rndis_per_dev_params[configNr].resp_queue))
984 { 980 {
985 r = list_entry (act, rndis_resp_t, list); 981 r = list_entry(act, rndis_resp_t, list);
986 if (r && r->buf == buf) { 982 if (r && r->buf == buf) {
987 list_del (&r->list); 983 list_del(&r->list);
988 kfree (r); 984 kfree(r);
989 } 985 }
990 } 986 }
991} 987}
992 988
993u8 *rndis_get_next_response (int configNr, u32 *length) 989u8 *rndis_get_next_response(int configNr, u32 *length)
994{ 990{
995 rndis_resp_t *r; 991 rndis_resp_t *r;
996 struct list_head *act, *tmp; 992 struct list_head *act, *tmp;
997 993
998 if (!length) return NULL; 994 if (!length) return NULL;
999 995
1000 list_for_each_safe (act, tmp, 996 list_for_each_safe(act, tmp,
1001 &(rndis_per_dev_params [configNr].resp_queue)) 997 &(rndis_per_dev_params[configNr].resp_queue))
1002 { 998 {
1003 r = list_entry (act, rndis_resp_t, list); 999 r = list_entry(act, rndis_resp_t, list);
1004 if (!r->send) { 1000 if (!r->send) {
1005 r->send = 1; 1001 r->send = 1;
1006 *length = r->length; 1002 *length = r->length;
@@ -1011,20 +1007,20 @@ u8 *rndis_get_next_response (int configNr, u32 *length)
1011 return NULL; 1007 return NULL;
1012} 1008}
1013 1009
1014static rndis_resp_t *rndis_add_response (int configNr, u32 length) 1010static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1015{ 1011{
1016 rndis_resp_t *r; 1012 rndis_resp_t *r;
1017 1013
1018 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */ 1014 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1019 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC); 1015 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
1020 if (!r) return NULL; 1016 if (!r) return NULL;
1021 1017
1022 r->buf = (u8 *) (r + 1); 1018 r->buf = (u8 *)(r + 1);
1023 r->length = length; 1019 r->length = length;
1024 r->send = 0; 1020 r->send = 0;
1025 1021
1026 list_add_tail (&r->list, 1022 list_add_tail(&r->list,
1027 &(rndis_per_dev_params [configNr].resp_queue)); 1023 &(rndis_per_dev_params[configNr].resp_queue));
1028 return r; 1024 return r;
1029} 1025}
1030 1026
@@ -1033,7 +1029,7 @@ int rndis_rm_hdr(struct gether *port,
1033 struct sk_buff_head *list) 1029 struct sk_buff_head *list)
1034{ 1030{
1035 /* tmp points to a struct rndis_packet_msg_type */ 1031 /* tmp points to a struct rndis_packet_msg_type */
1036 __le32 *tmp = (void *) skb->data; 1032 __le32 *tmp = (void *)skb->data;
1037 1033
1038 /* MessageType, MessageLength */ 1034 /* MessageType, MessageLength */
1039 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG) 1035 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
@@ -1054,7 +1050,7 @@ int rndis_rm_hdr(struct gether *port,
1054 return 0; 1050 return 0;
1055} 1051}
1056 1052
1057#ifdef CONFIG_USB_GADGET_DEBUG_FILES 1053#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1058 1054
1059static int rndis_proc_show(struct seq_file *m, void *v) 1055static int rndis_proc_show(struct seq_file *m, void *v)
1060{ 1056{
@@ -1087,7 +1083,7 @@ static int rndis_proc_show(struct seq_file *m, void *v)
1087} 1083}
1088 1084
1089static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, 1085static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1090 size_t count, loff_t *ppos) 1086 size_t count, loff_t *ppos)
1091{ 1087{
1092 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data; 1088 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
1093 u32 speed = 0; 1089 u32 speed = 0;
@@ -1109,11 +1105,11 @@ static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1109 case '8': 1105 case '8':
1110 case '9': 1106 case '9':
1111 fl_speed = 1; 1107 fl_speed = 1;
1112 speed = speed*10 + c - '0'; 1108 speed = speed * 10 + c - '0';
1113 break; 1109 break;
1114 case 'C': 1110 case 'C':
1115 case 'c': 1111 case 'c':
1116 rndis_signal_connect (p->confignr); 1112 rndis_signal_connect(p->confignr);
1117 break; 1113 break;
1118 case 'D': 1114 case 'D':
1119 case 'd': 1115 case 'd':
@@ -1145,11 +1141,11 @@ static const struct file_operations rndis_proc_fops = {
1145 .write = rndis_proc_write, 1141 .write = rndis_proc_write,
1146}; 1142};
1147 1143
1148#define NAME_TEMPLATE "driver/rndis-%03d" 1144#define NAME_TEMPLATE "driver/rndis-%03d"
1149 1145
1150static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS]; 1146static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1151 1147
1152#endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1148#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1153 1149
1154 1150
1155int rndis_init(void) 1151int rndis_init(void)
@@ -1160,42 +1156,40 @@ int rndis_init(void)
1160#ifdef CONFIG_USB_GADGET_DEBUG_FILES 1156#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1161 char name [20]; 1157 char name [20];
1162 1158
1163 sprintf (name, NAME_TEMPLATE, i); 1159 sprintf(name, NAME_TEMPLATE, i);
1164 if (!(rndis_connect_state [i] 1160 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
1165 = proc_create_data(name, 0660, NULL,
1166 &rndis_proc_fops, 1161 &rndis_proc_fops,
1167 (void *)(rndis_per_dev_params + i)))) 1162 (void *)(rndis_per_dev_params + i));
1168 { 1163 if (!rndis_connect_state[i]) {
1169 pr_debug("%s :remove entries", __func__); 1164 pr_debug("%s: remove entries", __func__);
1170 while (i) { 1165 while (i) {
1171 sprintf (name, NAME_TEMPLATE, --i); 1166 sprintf(name, NAME_TEMPLATE, --i);
1172 remove_proc_entry (name, NULL); 1167 remove_proc_entry(name, NULL);
1173 } 1168 }
1174 pr_debug("\n"); 1169 pr_debug("\n");
1175 return -EIO; 1170 return -EIO;
1176 } 1171 }
1177#endif 1172#endif
1178 rndis_per_dev_params [i].confignr = i; 1173 rndis_per_dev_params[i].confignr = i;
1179 rndis_per_dev_params [i].used = 0; 1174 rndis_per_dev_params[i].used = 0;
1180 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED; 1175 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1181 rndis_per_dev_params [i].media_state 1176 rndis_per_dev_params[i].media_state
1182 = NDIS_MEDIA_STATE_DISCONNECTED; 1177 = NDIS_MEDIA_STATE_DISCONNECTED;
1183 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue)); 1178 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1184 } 1179 }
1185 1180
1186 return 0; 1181 return 0;
1187} 1182}
1188 1183
1189void rndis_exit (void) 1184void rndis_exit(void)
1190{ 1185{
1191#ifdef CONFIG_USB_GADGET_DEBUG_FILES 1186#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1192 u8 i; 1187 u8 i;
1193 char name [20]; 1188 char name[20];
1194 1189
1195 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) { 1190 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1196 sprintf (name, NAME_TEMPLATE, i); 1191 sprintf(name, NAME_TEMPLATE, i);
1197 remove_proc_entry (name, NULL); 1192 remove_proc_entry(name, NULL);
1198 } 1193 }
1199#endif 1194#endif
1200} 1195}
1201
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index a229744a8c7d..0dfee282878a 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -1,5 +1,8 @@
1/* linux/drivers/usb/gadget/s3c-hsotg.c 1/* linux/drivers/usb/gadget/s3c-hsotg.c
2 * 2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
3 * Copyright 2008 Openmoko, Inc. 6 * Copyright 2008 Openmoko, Inc.
4 * Copyright 2008 Simtec Electronics 7 * Copyright 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk> 8 * Ben Dooks <ben@simtec.co.uk>
@@ -41,8 +44,8 @@
41/* EP0_MPS_LIMIT 44/* EP0_MPS_LIMIT
42 * 45 *
43 * Unfortunately there seems to be a limit of the amount of data that can 46 * Unfortunately there seems to be a limit of the amount of data that can
44 * be transfered by IN transactions on EP0. This is either 127 bytes or 3 47 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
45 * packets (which practially means 1 packet and 63 bytes of data) when the 48 * packets (which practically means 1 packet and 63 bytes of data) when the
46 * MPS is set to 64. 49 * MPS is set to 64.
47 * 50 *
48 * This means if we are wanting to move >127 bytes of data, we need to 51 * This means if we are wanting to move >127 bytes of data, we need to
@@ -613,11 +616,10 @@ static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
613 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1; 616 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
614 } else { 617 } else {
615 maxsize = 64+64; 618 maxsize = 64+64;
616 if (hs_ep->dir_in) { 619 if (hs_ep->dir_in)
617 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1; 620 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
618 } else { 621 else
619 maxpkt = 2; 622 maxpkt = 2;
620 }
621 } 623 }
622 624
623 /* we made the constant loading easier above by using +1 */ 625 /* we made the constant loading easier above by using +1 */
@@ -679,6 +681,14 @@ static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
679 __func__, readl(hsotg->regs + epctrl_reg), index, 681 __func__, readl(hsotg->regs + epctrl_reg), index,
680 hs_ep->dir_in ? "in" : "out"); 682 hs_ep->dir_in ? "in" : "out");
681 683
684 /* If endpoint is stalled, we will restart request later */
685 ctrl = readl(hsotg->regs + epctrl_reg);
686
687 if (ctrl & S3C_DxEPCTL_Stall) {
688 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
689 return;
690 }
691
682 length = ureq->length - ureq->actual; 692 length = ureq->length - ureq->actual;
683 693
684 if (0) 694 if (0)
@@ -731,18 +741,6 @@ static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
731 /* write size / packets */ 741 /* write size / packets */
732 writel(epsize, hsotg->regs + epsize_reg); 742 writel(epsize, hsotg->regs + epsize_reg);
733 743
734 ctrl = readl(hsotg->regs + epctrl_reg);
735
736 if (ctrl & S3C_DxEPCTL_Stall) {
737 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
738
739 /* not sure what we can do here, if it is EP0 then we should
740 * get this cleared once the endpoint has transmitted the
741 * STALL packet, otherwise it needs to be cleared by the
742 * host.
743 */
744 }
745
746 if (using_dma(hsotg)) { 744 if (using_dma(hsotg)) {
747 unsigned int dma_reg; 745 unsigned int dma_reg;
748 746
@@ -783,7 +781,7 @@ static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
783 hsotg->regs + S3C_DIEPINT(index)); 781 hsotg->regs + S3C_DIEPINT(index));
784 782
785 /* Note, trying to clear the NAK here causes problems with transmit 783 /* Note, trying to clear the NAK here causes problems with transmit
786 * on the S3C6400 ending up with the TXFIFO becomming full. */ 784 * on the S3C6400 ending up with the TXFIFO becoming full. */
787 785
788 /* check ep is enabled */ 786 /* check ep is enabled */
789 if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna)) 787 if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna))
@@ -1048,6 +1046,20 @@ static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1048static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value); 1046static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1049 1047
1050/** 1048/**
1049 * get_ep_head - return the first request on the endpoint
1050 * @hs_ep: The controller endpoint to get
1051 *
1052 * Get the first request on the endpoint.
1053 */
1054static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1055{
1056 if (list_empty(&hs_ep->queue))
1057 return NULL;
1058
1059 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1060}
1061
1062/**
1051 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE 1063 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1052 * @hsotg: The device state 1064 * @hsotg: The device state
1053 * @ctrl: USB control request 1065 * @ctrl: USB control request
@@ -1055,8 +1067,12 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1055static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg, 1067static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1056 struct usb_ctrlrequest *ctrl) 1068 struct usb_ctrlrequest *ctrl)
1057{ 1069{
1070 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1071 struct s3c_hsotg_req *hs_req;
1072 bool restart;
1058 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); 1073 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1059 struct s3c_hsotg_ep *ep; 1074 struct s3c_hsotg_ep *ep;
1075 int ret;
1060 1076
1061 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", 1077 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1062 __func__, set ? "SET" : "CLEAR"); 1078 __func__, set ? "SET" : "CLEAR");
@@ -1072,6 +1088,36 @@ static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1072 switch (le16_to_cpu(ctrl->wValue)) { 1088 switch (le16_to_cpu(ctrl->wValue)) {
1073 case USB_ENDPOINT_HALT: 1089 case USB_ENDPOINT_HALT:
1074 s3c_hsotg_ep_sethalt(&ep->ep, set); 1090 s3c_hsotg_ep_sethalt(&ep->ep, set);
1091
1092 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1093 if (ret) {
1094 dev_err(hsotg->dev,
1095 "%s: failed to send reply\n", __func__);
1096 return ret;
1097 }
1098
1099 if (!set) {
1100 /*
1101 * If we have request in progress,
1102 * then complete it
1103 */
1104 if (ep->req) {
1105 hs_req = ep->req;
1106 ep->req = NULL;
1107 list_del_init(&hs_req->queue);
1108 hs_req->req.complete(&ep->ep,
1109 &hs_req->req);
1110 }
1111
1112 /* If we have pending request, then start it */
1113 restart = !list_empty(&ep->queue);
1114 if (restart) {
1115 hs_req = get_ep_head(ep);
1116 s3c_hsotg_start_req(hsotg, ep,
1117 hs_req, false);
1118 }
1119 }
1120
1075 break; 1121 break;
1076 1122
1077 default: 1123 default:
@@ -1148,14 +1194,6 @@ static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1148 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); 1194 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1149 } 1195 }
1150 1196
1151 if (ret > 0) {
1152 if (!ep0->dir_in) {
1153 /* need to generate zlp in reply or take data */
1154 /* todo - deal with any data we might be sent? */
1155 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1156 }
1157 }
1158
1159 /* the request is either unhandlable, or is not formatted correctly 1197 /* the request is either unhandlable, or is not formatted correctly
1160 * so respond with a STALL for the status stage to indicate failure. 1198 * so respond with a STALL for the status stage to indicate failure.
1161 */ 1199 */
@@ -1176,10 +1214,10 @@ static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1176 writel(ctrl, hsotg->regs + reg); 1214 writel(ctrl, hsotg->regs + reg);
1177 1215
1178 dev_dbg(hsotg->dev, 1216 dev_dbg(hsotg->dev,
1179 "writen DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n", 1217 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1180 ctrl, reg, readl(hsotg->regs + reg)); 1218 ctrl, reg, readl(hsotg->regs + reg));
1181 1219
1182 /* don't belive we need to anything more to get the EP 1220 /* don't believe we need to anything more to get the EP
1183 * to reply with a STALL packet */ 1221 * to reply with a STALL packet */
1184 } 1222 }
1185} 1223}
@@ -1247,20 +1285,6 @@ static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1247} 1285}
1248 1286
1249/** 1287/**
1250 * get_ep_head - return the first request on the endpoint
1251 * @hs_ep: The controller endpoint to get
1252 *
1253 * Get the first request on the endpoint.
1254*/
1255static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1256{
1257 if (list_empty(&hs_ep->queue))
1258 return NULL;
1259
1260 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1261}
1262
1263/**
1264 * s3c_hsotg_complete_request - complete a request given to us 1288 * s3c_hsotg_complete_request - complete a request given to us
1265 * @hsotg: The device state. 1289 * @hsotg: The device state.
1266 * @hs_ep: The endpoint the request was on. 1290 * @hs_ep: The endpoint the request was on.
@@ -1416,7 +1440,7 @@ static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1416 * transaction. 1440 * transaction.
1417 * 1441 *
1418 * Note, since we don't write any data to the TxFIFO, then it is 1442 * Note, since we don't write any data to the TxFIFO, then it is
1419 * currently belived that we do not need to wait for any space in 1443 * currently believed that we do not need to wait for any space in
1420 * the TxFIFO. 1444 * the TxFIFO.
1421 */ 1445 */
1422static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg, 1446static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
@@ -1540,7 +1564,7 @@ static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1540 * that requires processing, so find out what is in there and do the 1564 * that requires processing, so find out what is in there and do the
1541 * appropriate read. 1565 * appropriate read.
1542 * 1566 *
1543 * The RXFIFO is a true FIFO, the packets comming out are still in packet 1567 * The RXFIFO is a true FIFO, the packets coming out are still in packet
1544 * chunks, so if you have x packets received on an endpoint you'll get x 1568 * chunks, so if you have x packets received on an endpoint you'll get x
1545 * FIFO events delivered, each with a packet's worth of data in it. 1569 * FIFO events delivered, each with a packet's worth of data in it.
1546 * 1570 *
@@ -1683,6 +1707,37 @@ bad_mps:
1683 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); 1707 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1684} 1708}
1685 1709
1710/**
1711 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1712 * @hsotg: The driver state
1713 * @idx: The index for the endpoint (0..15)
1714 */
1715static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1716{
1717 int timeout;
1718 int val;
1719
1720 writel(S3C_GRSTCTL_TxFNum(idx) | S3C_GRSTCTL_TxFFlsh,
1721 hsotg->regs + S3C_GRSTCTL);
1722
1723 /* wait until the fifo is flushed */
1724 timeout = 100;
1725
1726 while (1) {
1727 val = readl(hsotg->regs + S3C_GRSTCTL);
1728
1729 if ((val & (S3C_GRSTCTL_TxFFlsh)) == 0)
1730 break;
1731
1732 if (--timeout == 0) {
1733 dev_err(hsotg->dev,
1734 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1735 __func__, val);
1736 }
1737
1738 udelay(1);
1739 }
1740}
1686 1741
1687/** 1742/**
1688 * s3c_hsotg_trytx - check to see if anything needs transmitting 1743 * s3c_hsotg_trytx - check to see if anything needs transmitting
@@ -1775,10 +1830,12 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1775 u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx); 1830 u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx);
1776 u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx); 1831 u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx);
1777 u32 ints; 1832 u32 ints;
1778 u32 clear = 0;
1779 1833
1780 ints = readl(hsotg->regs + epint_reg); 1834 ints = readl(hsotg->regs + epint_reg);
1781 1835
1836 /* Clear endpoint interrupts */
1837 writel(ints, hsotg->regs + epint_reg);
1838
1782 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", 1839 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1783 __func__, idx, dir_in ? "in" : "out", ints); 1840 __func__, idx, dir_in ? "in" : "out", ints);
1784 1841
@@ -1801,19 +1858,28 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1801 1858
1802 s3c_hsotg_handle_outdone(hsotg, idx, false); 1859 s3c_hsotg_handle_outdone(hsotg, idx, false);
1803 } 1860 }
1804
1805 clear |= S3C_DxEPINT_XferCompl;
1806 } 1861 }
1807 1862
1808 if (ints & S3C_DxEPINT_EPDisbld) { 1863 if (ints & S3C_DxEPINT_EPDisbld) {
1809 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); 1864 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
1810 clear |= S3C_DxEPINT_EPDisbld; 1865
1866 if (dir_in) {
1867 int epctl = readl(hsotg->regs + epctl_reg);
1868
1869 s3c_hsotg_txfifo_flush(hsotg, idx);
1870
1871 if ((epctl & S3C_DxEPCTL_Stall) &&
1872 (epctl & S3C_DxEPCTL_EPType_Bulk)) {
1873 int dctl = readl(hsotg->regs + S3C_DCTL);
1874
1875 dctl |= S3C_DCTL_CGNPInNAK;
1876 writel(dctl, hsotg->regs + S3C_DCTL);
1877 }
1878 }
1811 } 1879 }
1812 1880
1813 if (ints & S3C_DxEPINT_AHBErr) { 1881 if (ints & S3C_DxEPINT_AHBErr)
1814 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); 1882 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
1815 clear |= S3C_DxEPINT_AHBErr;
1816 }
1817 1883
1818 if (ints & S3C_DxEPINT_Setup) { /* Setup or Timeout */ 1884 if (ints & S3C_DxEPINT_Setup) { /* Setup or Timeout */
1819 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); 1885 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
@@ -1829,14 +1895,10 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1829 else 1895 else
1830 s3c_hsotg_handle_outdone(hsotg, 0, true); 1896 s3c_hsotg_handle_outdone(hsotg, 0, true);
1831 } 1897 }
1832
1833 clear |= S3C_DxEPINT_Setup;
1834 } 1898 }
1835 1899
1836 if (ints & S3C_DxEPINT_Back2BackSetup) { 1900 if (ints & S3C_DxEPINT_Back2BackSetup)
1837 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); 1901 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
1838 clear |= S3C_DxEPINT_Back2BackSetup;
1839 }
1840 1902
1841 if (dir_in) { 1903 if (dir_in) {
1842 /* not sure if this is important, but we'll clear it anyway 1904 /* not sure if this is important, but we'll clear it anyway
@@ -1844,14 +1906,12 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1844 if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) { 1906 if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) {
1845 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", 1907 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1846 __func__, idx); 1908 __func__, idx);
1847 clear |= S3C_DIEPMSK_INTknTXFEmpMsk;
1848 } 1909 }
1849 1910
1850 /* this probably means something bad is happening */ 1911 /* this probably means something bad is happening */
1851 if (ints & S3C_DIEPMSK_INTknEPMisMsk) { 1912 if (ints & S3C_DIEPMSK_INTknEPMisMsk) {
1852 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", 1913 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1853 __func__, idx); 1914 __func__, idx);
1854 clear |= S3C_DIEPMSK_INTknEPMisMsk;
1855 } 1915 }
1856 1916
1857 /* FIFO has space or is empty (see GAHBCFG) */ 1917 /* FIFO has space or is empty (see GAHBCFG) */
@@ -1860,11 +1920,8 @@ static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1860 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", 1920 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1861 __func__, idx); 1921 __func__, idx);
1862 s3c_hsotg_trytx(hsotg, hs_ep); 1922 s3c_hsotg_trytx(hsotg, hs_ep);
1863 clear |= S3C_DIEPMSK_TxFIFOEmpty;
1864 } 1923 }
1865 } 1924 }
1866
1867 writel(clear, hsotg->regs + epint_reg);
1868} 1925}
1869 1926
1870/** 1927/**
@@ -2056,7 +2113,6 @@ irq_retry:
2056 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint); 2113 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2057 2114
2058 writel(otgint, hsotg->regs + S3C_GOTGINT); 2115 writel(otgint, hsotg->regs + S3C_GOTGINT);
2059 writel(S3C_GINTSTS_OTGInt, hsotg->regs + S3C_GINTSTS);
2060 } 2116 }
2061 2117
2062 if (gintsts & S3C_GINTSTS_DisconnInt) { 2118 if (gintsts & S3C_GINTSTS_DisconnInt) {
@@ -2072,8 +2128,9 @@ irq_retry:
2072 } 2128 }
2073 2129
2074 if (gintsts & S3C_GINTSTS_EnumDone) { 2130 if (gintsts & S3C_GINTSTS_EnumDone) {
2075 s3c_hsotg_irq_enumdone(hsotg);
2076 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS); 2131 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
2132
2133 s3c_hsotg_irq_enumdone(hsotg);
2077 } 2134 }
2078 2135
2079 if (gintsts & S3C_GINTSTS_ConIDStsChng) { 2136 if (gintsts & S3C_GINTSTS_ConIDStsChng) {
@@ -2101,10 +2158,6 @@ irq_retry:
2101 if (daint_in & 1) 2158 if (daint_in & 1)
2102 s3c_hsotg_epint(hsotg, ep, 1); 2159 s3c_hsotg_epint(hsotg, ep, 1);
2103 } 2160 }
2104
2105 writel(daint, hsotg->regs + S3C_DAINT);
2106 writel(gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt),
2107 hsotg->regs + S3C_GINTSTS);
2108 } 2161 }
2109 2162
2110 if (gintsts & S3C_GINTSTS_USBRst) { 2163 if (gintsts & S3C_GINTSTS_USBRst) {
@@ -2112,6 +2165,8 @@ irq_retry:
2112 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", 2165 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2113 readl(hsotg->regs + S3C_GNPTXSTS)); 2166 readl(hsotg->regs + S3C_GNPTXSTS));
2114 2167
2168 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2169
2115 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true); 2170 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2116 2171
2117 /* it seems after a reset we can end up with a situation 2172 /* it seems after a reset we can end up with a situation
@@ -2123,8 +2178,6 @@ irq_retry:
2123 s3c_hsotg_init_fifo(hsotg); 2178 s3c_hsotg_init_fifo(hsotg);
2124 2179
2125 s3c_hsotg_enqueue_setup(hsotg); 2180 s3c_hsotg_enqueue_setup(hsotg);
2126
2127 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2128 } 2181 }
2129 2182
2130 /* check both FIFOs */ 2183 /* check both FIFOs */
@@ -2138,8 +2191,6 @@ irq_retry:
2138 2191
2139 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp); 2192 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2140 s3c_hsotg_irq_fifoempty(hsotg, false); 2193 s3c_hsotg_irq_fifoempty(hsotg, false);
2141
2142 writel(S3C_GINTSTS_NPTxFEmp, hsotg->regs + S3C_GINTSTS);
2143 } 2194 }
2144 2195
2145 if (gintsts & S3C_GINTSTS_PTxFEmp) { 2196 if (gintsts & S3C_GINTSTS_PTxFEmp) {
@@ -2149,8 +2200,6 @@ irq_retry:
2149 2200
2150 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp); 2201 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2151 s3c_hsotg_irq_fifoempty(hsotg, true); 2202 s3c_hsotg_irq_fifoempty(hsotg, true);
2152
2153 writel(S3C_GINTSTS_PTxFEmp, hsotg->regs + S3C_GINTSTS);
2154 } 2203 }
2155 2204
2156 if (gintsts & S3C_GINTSTS_RxFLvl) { 2205 if (gintsts & S3C_GINTSTS_RxFLvl) {
@@ -2159,7 +2208,6 @@ irq_retry:
2159 * set. */ 2208 * set. */
2160 2209
2161 s3c_hsotg_handle_rx(hsotg); 2210 s3c_hsotg_handle_rx(hsotg);
2162 writel(S3C_GINTSTS_RxFLvl, hsotg->regs + S3C_GINTSTS);
2163 } 2211 }
2164 2212
2165 if (gintsts & S3C_GINTSTS_ModeMis) { 2213 if (gintsts & S3C_GINTSTS_ModeMis) {
@@ -2188,24 +2236,22 @@ irq_retry:
2188 2236
2189 /* these next two seem to crop-up occasionally causing the core 2237 /* these next two seem to crop-up occasionally causing the core
2190 * to shutdown the USB transfer, so try clearing them and logging 2238 * to shutdown the USB transfer, so try clearing them and logging
2191 * the occurence. */ 2239 * the occurrence. */
2192 2240
2193 if (gintsts & S3C_GINTSTS_GOUTNakEff) { 2241 if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2194 dev_info(hsotg->dev, "GOUTNakEff triggered\n"); 2242 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2195 2243
2196 s3c_hsotg_dump(hsotg);
2197
2198 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL); 2244 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
2199 writel(S3C_GINTSTS_GOUTNakEff, hsotg->regs + S3C_GINTSTS); 2245
2246 s3c_hsotg_dump(hsotg);
2200 } 2247 }
2201 2248
2202 if (gintsts & S3C_GINTSTS_GINNakEff) { 2249 if (gintsts & S3C_GINTSTS_GINNakEff) {
2203 dev_info(hsotg->dev, "GINNakEff triggered\n"); 2250 dev_info(hsotg->dev, "GINNakEff triggered\n");
2204 2251
2205 s3c_hsotg_dump(hsotg);
2206
2207 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL); 2252 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
2208 writel(S3C_GINTSTS_GINNakEff, hsotg->regs + S3C_GINTSTS); 2253
2254 s3c_hsotg_dump(hsotg);
2209 } 2255 }
2210 2256
2211 /* if we've had fifo events, we should try and go around the 2257 /* if we've had fifo events, we should try and go around the
@@ -2403,11 +2449,6 @@ static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2403 2449
2404 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); 2450 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2405 2451
2406 if (hs_req == hs_ep->req) {
2407 dev_dbg(hs->dev, "%s: already in progress\n", __func__);
2408 return -EINPROGRESS;
2409 }
2410
2411 spin_lock_irqsave(&hs_ep->lock, flags); 2452 spin_lock_irqsave(&hs_ep->lock, flags);
2412 2453
2413 if (!on_list(hs_ep, hs_req)) { 2454 if (!on_list(hs_ep, hs_req)) {
@@ -2429,6 +2470,7 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2429 unsigned long irqflags; 2470 unsigned long irqflags;
2430 u32 epreg; 2471 u32 epreg;
2431 u32 epctl; 2472 u32 epctl;
2473 u32 xfertype;
2432 2474
2433 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); 2475 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2434 2476
@@ -2439,10 +2481,17 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2439 epreg = S3C_DIEPCTL(index); 2481 epreg = S3C_DIEPCTL(index);
2440 epctl = readl(hs->regs + epreg); 2482 epctl = readl(hs->regs + epreg);
2441 2483
2442 if (value) 2484 if (value) {
2443 epctl |= S3C_DxEPCTL_Stall; 2485 epctl |= S3C_DxEPCTL_Stall + S3C_DxEPCTL_SNAK;
2444 else 2486 if (epctl & S3C_DxEPCTL_EPEna)
2487 epctl |= S3C_DxEPCTL_EPDis;
2488 } else {
2445 epctl &= ~S3C_DxEPCTL_Stall; 2489 epctl &= ~S3C_DxEPCTL_Stall;
2490 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2491 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2492 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2493 epctl |= S3C_DxEPCTL_SetD0PID;
2494 }
2446 2495
2447 writel(epctl, hs->regs + epreg); 2496 writel(epctl, hs->regs + epreg);
2448 2497
@@ -2451,8 +2500,13 @@ static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2451 2500
2452 if (value) 2501 if (value)
2453 epctl |= S3C_DxEPCTL_Stall; 2502 epctl |= S3C_DxEPCTL_Stall;
2454 else 2503 else {
2455 epctl &= ~S3C_DxEPCTL_Stall; 2504 epctl &= ~S3C_DxEPCTL_Stall;
2505 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2506 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2507 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2508 epctl |= S3C_DxEPCTL_SetD0PID;
2509 }
2456 2510
2457 writel(epctl, hs->regs + epreg); 2511 writel(epctl, hs->regs + epreg);
2458 2512
@@ -2469,7 +2523,7 @@ static struct usb_ep_ops s3c_hsotg_ep_ops = {
2469 .queue = s3c_hsotg_ep_queue, 2523 .queue = s3c_hsotg_ep_queue,
2470 .dequeue = s3c_hsotg_ep_dequeue, 2524 .dequeue = s3c_hsotg_ep_dequeue,
2471 .set_halt = s3c_hsotg_ep_sethalt, 2525 .set_halt = s3c_hsotg_ep_sethalt,
2472 /* note, don't belive we have any call for the fifo routines */ 2526 /* note, don't believe we have any call for the fifo routines */
2473}; 2527};
2474 2528
2475/** 2529/**
@@ -2491,9 +2545,9 @@ static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2491 timeout = 1000; 2545 timeout = 1000;
2492 do { 2546 do {
2493 grstctl = readl(hsotg->regs + S3C_GRSTCTL); 2547 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2494 } while (!(grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0); 2548 } while ((grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
2495 2549
2496 if (!(grstctl & S3C_GRSTCTL_CSftRst)) { 2550 if (grstctl & S3C_GRSTCTL_CSftRst) {
2497 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n"); 2551 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2498 return -EINVAL; 2552 return -EINVAL;
2499 } 2553 }
@@ -2510,20 +2564,18 @@ static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2510 return -ETIMEDOUT; 2564 return -ETIMEDOUT;
2511 } 2565 }
2512 2566
2513 if (grstctl & S3C_GRSTCTL_CSftRst)
2514 continue;
2515
2516 if (!(grstctl & S3C_GRSTCTL_AHBIdle)) 2567 if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2517 continue; 2568 continue;
2518 2569
2519 break; /* reset done */ 2570 break; /* reset done */
2520 } 2571 }
2521 2572
2522 dev_dbg(hsotg->dev, "reset successful\n"); 2573 dev_dbg(hsotg->dev, "reset successful\n");
2523 return 0; 2574 return 0;
2524} 2575}
2525 2576
2526int usb_gadget_register_driver(struct usb_gadget_driver *driver) 2577int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2578 int (*bind)(struct usb_gadget *))
2527{ 2579{
2528 struct s3c_hsotg *hsotg = our_hsotg; 2580 struct s3c_hsotg *hsotg = our_hsotg;
2529 int ret; 2581 int ret;
@@ -2543,7 +2595,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2543 dev_err(hsotg->dev, "%s: bad speed\n", __func__); 2595 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
2544 } 2596 }
2545 2597
2546 if (!driver->bind || !driver->setup) { 2598 if (!bind || !driver->setup) {
2547 dev_err(hsotg->dev, "%s: missing entry points\n", __func__); 2599 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2548 return -EINVAL; 2600 return -EINVAL;
2549 } 2601 }
@@ -2562,7 +2614,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2562 goto err; 2614 goto err;
2563 } 2615 }
2564 2616
2565 ret = driver->bind(&hsotg->gadget); 2617 ret = bind(&hsotg->gadget);
2566 if (ret) { 2618 if (ret) {
2567 dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name); 2619 dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name);
2568 2620
@@ -2587,6 +2639,12 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2587 2639
2588 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG); 2640 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG);
2589 2641
2642 /* Clear any pending OTG interrupts */
2643 writel(0xffffffff, hsotg->regs + S3C_GOTGINT);
2644
2645 /* Clear any pending interrupts */
2646 writel(0xffffffff, hsotg->regs + S3C_GINTSTS);
2647
2590 writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt | 2648 writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt |
2591 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst | 2649 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2592 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt | 2650 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
@@ -2622,9 +2680,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2622 2680
2623 writel(0, hsotg->regs + S3C_DAINTMSK); 2681 writel(0, hsotg->regs + S3C_DAINTMSK);
2624 2682
2625 dev_info(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", 2683 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2626 readl(hsotg->regs + S3C_DIEPCTL0), 2684 readl(hsotg->regs + S3C_DIEPCTL0),
2627 readl(hsotg->regs + S3C_DOEPCTL0)); 2685 readl(hsotg->regs + S3C_DOEPCTL0));
2628 2686
2629 /* enable in and out endpoint interrupts */ 2687 /* enable in and out endpoint interrupts */
2630 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt); 2688 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
@@ -2643,7 +2701,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2643 udelay(10); /* see openiboot */ 2701 udelay(10); /* see openiboot */
2644 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone); 2702 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2645 2703
2646 dev_info(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL)); 2704 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
2647 2705
2648 /* S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by 2706 /* S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2649 writing to the EPCTL register.. */ 2707 writing to the EPCTL register.. */
@@ -2663,9 +2721,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2663 2721
2664 s3c_hsotg_enqueue_setup(hsotg); 2722 s3c_hsotg_enqueue_setup(hsotg);
2665 2723
2666 dev_info(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", 2724 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2667 readl(hsotg->regs + S3C_DIEPCTL0), 2725 readl(hsotg->regs + S3C_DIEPCTL0),
2668 readl(hsotg->regs + S3C_DOEPCTL0)); 2726 readl(hsotg->regs + S3C_DOEPCTL0));
2669 2727
2670 /* clear global NAKs */ 2728 /* clear global NAKs */
2671 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK, 2729 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
@@ -2687,7 +2745,7 @@ err:
2687 hsotg->gadget.dev.driver = NULL; 2745 hsotg->gadget.dev.driver = NULL;
2688 return ret; 2746 return ret;
2689} 2747}
2690EXPORT_SYMBOL(usb_gadget_register_driver); 2748EXPORT_SYMBOL(usb_gadget_probe_driver);
2691 2749
2692int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 2750int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2693{ 2751{
@@ -2863,9 +2921,9 @@ static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2863 2921
2864 /* setup fifos */ 2922 /* setup fifos */
2865 2923
2866 dev_info(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", 2924 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2867 readl(hsotg->regs + S3C_GRXFSIZ), 2925 readl(hsotg->regs + S3C_GRXFSIZ),
2868 readl(hsotg->regs + S3C_GNPTXFSIZ)); 2926 readl(hsotg->regs + S3C_GNPTXFSIZ));
2869 2927
2870 s3c_hsotg_init_fifo(hsotg); 2928 s3c_hsotg_init_fifo(hsotg);
2871 2929
@@ -2887,6 +2945,7 @@ static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2887 2945
2888static void s3c_hsotg_dump(struct s3c_hsotg *hsotg) 2946static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2889{ 2947{
2948#ifdef DEBUG
2890 struct device *dev = hsotg->dev; 2949 struct device *dev = hsotg->dev;
2891 void __iomem *regs = hsotg->regs; 2950 void __iomem *regs = hsotg->regs;
2892 u32 val; 2951 u32 val;
@@ -2929,6 +2988,7 @@ static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2929 2988
2930 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", 2989 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
2931 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE)); 2990 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
2991#endif
2932} 2992}
2933 2993
2934 2994
@@ -3260,7 +3320,7 @@ static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3260 hsotg->clk = clk_get(&pdev->dev, "otg"); 3320 hsotg->clk = clk_get(&pdev->dev, "otg");
3261 if (IS_ERR(hsotg->clk)) { 3321 if (IS_ERR(hsotg->clk)) {
3262 dev_err(dev, "cannot get otg clock\n"); 3322 dev_err(dev, "cannot get otg clock\n");
3263 ret = -EINVAL; 3323 ret = PTR_ERR(hsotg->clk);
3264 goto err_mem; 3324 goto err_mem;
3265 } 3325 }
3266 3326
diff --git a/drivers/usb/gadget/s3c-hsudc.c b/drivers/usb/gadget/s3c-hsudc.c
new file mode 100644
index 000000000000..d5e3e1e58626
--- /dev/null
+++ b/drivers/usb/gadget/s3c-hsudc.c
@@ -0,0 +1,1352 @@
1/* linux/drivers/usb/gadget/s3c-hsudc.c
2 *
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 *
6 * S3C24XX USB 2.0 High-speed USB controller gadget driver
7 *
8 * The S3C24XX USB 2.0 high-speed USB controller supports upto 9 endpoints.
9 * Each endpoint can be configured as either in or out endpoint. Endpoints
10 * can be configured for Bulk or Interrupt transfer mode.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15*/
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/delay.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/clk.h>
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
29#include <linux/prefetch.h>
30
31#include <mach/regs-s3c2443-clock.h>
32#include <plat/udc.h>
33
34#define S3C_HSUDC_REG(x) (x)
35
36/* Non-Indexed Registers */
37#define S3C_IR S3C_HSUDC_REG(0x00) /* Index Register */
38#define S3C_EIR S3C_HSUDC_REG(0x04) /* EP Intr Status */
39#define S3C_EIR_EP0 (1<<0)
40#define S3C_EIER S3C_HSUDC_REG(0x08) /* EP Intr Enable */
41#define S3C_FAR S3C_HSUDC_REG(0x0c) /* Gadget Address */
42#define S3C_FNR S3C_HSUDC_REG(0x10) /* Frame Number */
43#define S3C_EDR S3C_HSUDC_REG(0x14) /* EP Direction */
44#define S3C_TR S3C_HSUDC_REG(0x18) /* Test Register */
45#define S3C_SSR S3C_HSUDC_REG(0x1c) /* System Status */
46#define S3C_SSR_DTZIEN_EN (0xff8f)
47#define S3C_SSR_ERR (0xff80)
48#define S3C_SSR_VBUSON (1 << 8)
49#define S3C_SSR_HSP (1 << 4)
50#define S3C_SSR_SDE (1 << 3)
51#define S3C_SSR_RESUME (1 << 2)
52#define S3C_SSR_SUSPEND (1 << 1)
53#define S3C_SSR_RESET (1 << 0)
54#define S3C_SCR S3C_HSUDC_REG(0x20) /* System Control */
55#define S3C_SCR_DTZIEN_EN (1 << 14)
56#define S3C_SCR_RRD_EN (1 << 5)
57#define S3C_SCR_SUS_EN (1 << 1)
58#define S3C_SCR_RST_EN (1 << 0)
59#define S3C_EP0SR S3C_HSUDC_REG(0x24) /* EP0 Status */
60#define S3C_EP0SR_EP0_LWO (1 << 6)
61#define S3C_EP0SR_STALL (1 << 4)
62#define S3C_EP0SR_TX_SUCCESS (1 << 1)
63#define S3C_EP0SR_RX_SUCCESS (1 << 0)
64#define S3C_EP0CR S3C_HSUDC_REG(0x28) /* EP0 Control */
65#define S3C_BR(_x) S3C_HSUDC_REG(0x60 + (_x * 4))
66
67/* Indexed Registers */
68#define S3C_ESR S3C_HSUDC_REG(0x2c) /* EPn Status */
69#define S3C_ESR_FLUSH (1 << 6)
70#define S3C_ESR_STALL (1 << 5)
71#define S3C_ESR_LWO (1 << 4)
72#define S3C_ESR_PSIF_ONE (1 << 2)
73#define S3C_ESR_PSIF_TWO (2 << 2)
74#define S3C_ESR_TX_SUCCESS (1 << 1)
75#define S3C_ESR_RX_SUCCESS (1 << 0)
76#define S3C_ECR S3C_HSUDC_REG(0x30) /* EPn Control */
77#define S3C_ECR_DUEN (1 << 7)
78#define S3C_ECR_FLUSH (1 << 6)
79#define S3C_ECR_STALL (1 << 1)
80#define S3C_ECR_IEMS (1 << 0)
81#define S3C_BRCR S3C_HSUDC_REG(0x34) /* Read Count */
82#define S3C_BWCR S3C_HSUDC_REG(0x38) /* Write Count */
83#define S3C_MPR S3C_HSUDC_REG(0x3c) /* Max Pkt Size */
84
85#define WAIT_FOR_SETUP (0)
86#define DATA_STATE_XMIT (1)
87#define DATA_STATE_RECV (2)
88
89/**
90 * struct s3c_hsudc_ep - Endpoint representation used by driver.
91 * @ep: USB gadget layer representation of device endpoint.
92 * @name: Endpoint name (as required by ep autoconfiguration).
93 * @dev: Reference to the device controller to which this EP belongs.
94 * @desc: Endpoint descriptor obtained from the gadget driver.
95 * @queue: Transfer request queue for the endpoint.
96 * @stopped: Maintains state of endpoint, set if EP is halted.
97 * @bEndpointAddress: EP address (including direction bit).
98 * @fifo: Base address of EP FIFO.
99 */
100struct s3c_hsudc_ep {
101 struct usb_ep ep;
102 char name[20];
103 struct s3c_hsudc *dev;
104 const struct usb_endpoint_descriptor *desc;
105 struct list_head queue;
106 u8 stopped;
107 u8 wedge;
108 u8 bEndpointAddress;
109 void __iomem *fifo;
110};
111
112/**
113 * struct s3c_hsudc_req - Driver encapsulation of USB gadget transfer request.
114 * @req: Reference to USB gadget transfer request.
115 * @queue: Used for inserting this request to the endpoint request queue.
116 */
117struct s3c_hsudc_req {
118 struct usb_request req;
119 struct list_head queue;
120};
121
122/**
123 * struct s3c_hsudc - Driver's abstraction of the device controller.
124 * @gadget: Instance of usb_gadget which is referenced by gadget driver.
125 * @driver: Reference to currenty active gadget driver.
126 * @dev: The device reference used by probe function.
127 * @lock: Lock to synchronize the usage of Endpoints (EP's are indexed).
128 * @regs: Remapped base address of controller's register space.
129 * @mem_rsrc: Device memory resource used for remapping device register space.
130 * irq: IRQ number used by the controller.
131 * uclk: Reference to the controller clock.
132 * ep0state: Current state of EP0.
133 * ep: List of endpoints supported by the controller.
134 */
135struct s3c_hsudc {
136 struct usb_gadget gadget;
137 struct usb_gadget_driver *driver;
138 struct device *dev;
139 struct s3c24xx_hsudc_platdata *pd;
140 spinlock_t lock;
141 void __iomem *regs;
142 struct resource *mem_rsrc;
143 int irq;
144 struct clk *uclk;
145 int ep0state;
146 struct s3c_hsudc_ep ep[];
147};
148
149#define ep_maxpacket(_ep) ((_ep)->ep.maxpacket)
150#define ep_is_in(_ep) ((_ep)->bEndpointAddress & USB_DIR_IN)
151#define ep_index(_ep) ((_ep)->bEndpointAddress & \
152 USB_ENDPOINT_NUMBER_MASK)
153
154static struct s3c_hsudc *the_controller;
155static const char driver_name[] = "s3c-udc";
156static const char ep0name[] = "ep0-control";
157
158static inline struct s3c_hsudc_req *our_req(struct usb_request *req)
159{
160 return container_of(req, struct s3c_hsudc_req, req);
161}
162
163static inline struct s3c_hsudc_ep *our_ep(struct usb_ep *ep)
164{
165 return container_of(ep, struct s3c_hsudc_ep, ep);
166}
167
168static inline struct s3c_hsudc *to_hsudc(struct usb_gadget *gadget)
169{
170 return container_of(gadget, struct s3c_hsudc, gadget);
171}
172
173static inline void set_index(struct s3c_hsudc *hsudc, int ep_addr)
174{
175 ep_addr &= USB_ENDPOINT_NUMBER_MASK;
176 writel(ep_addr, hsudc->regs + S3C_IR);
177}
178
179static inline void __orr32(void __iomem *ptr, u32 val)
180{
181 writel(readl(ptr) | val, ptr);
182}
183
184static void s3c_hsudc_init_phy(void)
185{
186 u32 cfg;
187
188 cfg = readl(S3C2443_PWRCFG) | S3C2443_PWRCFG_USBPHY;
189 writel(cfg, S3C2443_PWRCFG);
190
191 cfg = readl(S3C2443_URSTCON);
192 cfg |= (S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
193 writel(cfg, S3C2443_URSTCON);
194 mdelay(1);
195
196 cfg = readl(S3C2443_URSTCON);
197 cfg &= ~(S3C2443_URSTCON_FUNCRST | S3C2443_URSTCON_PHYRST);
198 writel(cfg, S3C2443_URSTCON);
199
200 cfg = readl(S3C2443_PHYCTRL);
201 cfg &= ~(S3C2443_PHYCTRL_CLKSEL | S3C2443_PHYCTRL_DSPORT);
202 cfg |= (S3C2443_PHYCTRL_EXTCLK | S3C2443_PHYCTRL_PLLSEL);
203 writel(cfg, S3C2443_PHYCTRL);
204
205 cfg = readl(S3C2443_PHYPWR);
206 cfg &= ~(S3C2443_PHYPWR_FSUSPEND | S3C2443_PHYPWR_PLL_PWRDN |
207 S3C2443_PHYPWR_XO_ON | S3C2443_PHYPWR_PLL_REFCLK |
208 S3C2443_PHYPWR_ANALOG_PD);
209 cfg |= S3C2443_PHYPWR_COMMON_ON;
210 writel(cfg, S3C2443_PHYPWR);
211
212 cfg = readl(S3C2443_UCLKCON);
213 cfg |= (S3C2443_UCLKCON_DETECT_VBUS | S3C2443_UCLKCON_FUNC_CLKEN |
214 S3C2443_UCLKCON_TCLKEN);
215 writel(cfg, S3C2443_UCLKCON);
216}
217
218static void s3c_hsudc_uninit_phy(void)
219{
220 u32 cfg;
221
222 cfg = readl(S3C2443_PWRCFG) & ~S3C2443_PWRCFG_USBPHY;
223 writel(cfg, S3C2443_PWRCFG);
224
225 writel(S3C2443_PHYPWR_FSUSPEND, S3C2443_PHYPWR);
226
227 cfg = readl(S3C2443_UCLKCON) & ~S3C2443_UCLKCON_FUNC_CLKEN;
228 writel(cfg, S3C2443_UCLKCON);
229}
230
231/**
232 * s3c_hsudc_complete_request - Complete a transfer request.
233 * @hsep: Endpoint to which the request belongs.
234 * @hsreq: Transfer request to be completed.
235 * @status: Transfer completion status for the transfer request.
236 */
237static void s3c_hsudc_complete_request(struct s3c_hsudc_ep *hsep,
238 struct s3c_hsudc_req *hsreq, int status)
239{
240 unsigned int stopped = hsep->stopped;
241 struct s3c_hsudc *hsudc = hsep->dev;
242
243 list_del_init(&hsreq->queue);
244 hsreq->req.status = status;
245
246 if (!ep_index(hsep)) {
247 hsudc->ep0state = WAIT_FOR_SETUP;
248 hsep->bEndpointAddress &= ~USB_DIR_IN;
249 }
250
251 hsep->stopped = 1;
252 spin_unlock(&hsudc->lock);
253 if (hsreq->req.complete != NULL)
254 hsreq->req.complete(&hsep->ep, &hsreq->req);
255 spin_lock(&hsudc->lock);
256 hsep->stopped = stopped;
257}
258
259/**
260 * s3c_hsudc_nuke_ep - Terminate all requests queued for a endpoint.
261 * @hsep: Endpoint for which queued requests have to be terminated.
262 * @status: Transfer completion status for the transfer request.
263 */
264static void s3c_hsudc_nuke_ep(struct s3c_hsudc_ep *hsep, int status)
265{
266 struct s3c_hsudc_req *hsreq;
267
268 while (!list_empty(&hsep->queue)) {
269 hsreq = list_entry(hsep->queue.next,
270 struct s3c_hsudc_req, queue);
271 s3c_hsudc_complete_request(hsep, hsreq, status);
272 }
273}
274
275/**
276 * s3c_hsudc_stop_activity - Stop activity on all endpoints.
277 * @hsudc: Device controller for which EP activity is to be stopped.
278 * @driver: Reference to the gadget driver which is currently active.
279 *
280 * All the endpoints are stopped and any pending transfer requests if any on
281 * the endpoint are terminated.
282 */
283static void s3c_hsudc_stop_activity(struct s3c_hsudc *hsudc,
284 struct usb_gadget_driver *driver)
285{
286 struct s3c_hsudc_ep *hsep;
287 int epnum;
288
289 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
290
291 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++) {
292 hsep = &hsudc->ep[epnum];
293 hsep->stopped = 1;
294 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
295 }
296
297 spin_unlock(&hsudc->lock);
298 driver->disconnect(&hsudc->gadget);
299 spin_lock(&hsudc->lock);
300}
301
302/**
303 * s3c_hsudc_read_setup_pkt - Read the received setup packet from EP0 fifo.
304 * @hsudc: Device controller from which setup packet is to be read.
305 * @buf: The buffer into which the setup packet is read.
306 *
307 * The setup packet received in the EP0 fifo is read and stored into a
308 * given buffer address.
309 */
310
311static void s3c_hsudc_read_setup_pkt(struct s3c_hsudc *hsudc, u16 *buf)
312{
313 int count;
314
315 count = readl(hsudc->regs + S3C_BRCR);
316 while (count--)
317 *buf++ = (u16)readl(hsudc->regs + S3C_BR(0));
318
319 writel(S3C_EP0SR_RX_SUCCESS, hsudc->regs + S3C_EP0SR);
320}
321
322/**
323 * s3c_hsudc_write_fifo - Write next chunk of transfer data to EP fifo.
324 * @hsep: Endpoint to which the data is to be written.
325 * @hsreq: Transfer request from which the next chunk of data is written.
326 *
327 * Write the next chunk of data from a transfer request to the endpoint FIFO.
328 * If the transfer request completes, 1 is returned, otherwise 0 is returned.
329 */
330static int s3c_hsudc_write_fifo(struct s3c_hsudc_ep *hsep,
331 struct s3c_hsudc_req *hsreq)
332{
333 u16 *buf;
334 u32 max = ep_maxpacket(hsep);
335 u32 count, length;
336 bool is_last;
337 void __iomem *fifo = hsep->fifo;
338
339 buf = hsreq->req.buf + hsreq->req.actual;
340 prefetch(buf);
341
342 length = hsreq->req.length - hsreq->req.actual;
343 length = min(length, max);
344 hsreq->req.actual += length;
345
346 writel(length, hsep->dev->regs + S3C_BWCR);
347 for (count = 0; count < length; count += 2)
348 writel(*buf++, fifo);
349
350 if (count != max) {
351 is_last = true;
352 } else {
353 if (hsreq->req.length != hsreq->req.actual || hsreq->req.zero)
354 is_last = false;
355 else
356 is_last = true;
357 }
358
359 if (is_last) {
360 s3c_hsudc_complete_request(hsep, hsreq, 0);
361 return 1;
362 }
363
364 return 0;
365}
366
367/**
368 * s3c_hsudc_read_fifo - Read the next chunk of data from EP fifo.
369 * @hsep: Endpoint from which the data is to be read.
370 * @hsreq: Transfer request to which the next chunk of data read is written.
371 *
372 * Read the next chunk of data from the endpoint FIFO and a write it to the
373 * transfer request buffer. If the transfer request completes, 1 is returned,
374 * otherwise 0 is returned.
375 */
376static int s3c_hsudc_read_fifo(struct s3c_hsudc_ep *hsep,
377 struct s3c_hsudc_req *hsreq)
378{
379 struct s3c_hsudc *hsudc = hsep->dev;
380 u32 csr, offset;
381 u16 *buf, word;
382 u32 buflen, rcnt, rlen;
383 void __iomem *fifo = hsep->fifo;
384 u32 is_short = 0;
385
386 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
387 csr = readl(hsudc->regs + offset);
388 if (!(csr & S3C_ESR_RX_SUCCESS))
389 return -EINVAL;
390
391 buf = hsreq->req.buf + hsreq->req.actual;
392 prefetchw(buf);
393 buflen = hsreq->req.length - hsreq->req.actual;
394
395 rcnt = readl(hsudc->regs + S3C_BRCR);
396 rlen = (csr & S3C_ESR_LWO) ? (rcnt * 2 - 1) : (rcnt * 2);
397
398 hsreq->req.actual += min(rlen, buflen);
399 is_short = (rlen < hsep->ep.maxpacket);
400
401 while (rcnt-- != 0) {
402 word = (u16)readl(fifo);
403 if (buflen) {
404 *buf++ = word;
405 buflen--;
406 } else {
407 hsreq->req.status = -EOVERFLOW;
408 }
409 }
410
411 writel(S3C_ESR_RX_SUCCESS, hsudc->regs + offset);
412
413 if (is_short || hsreq->req.actual == hsreq->req.length) {
414 s3c_hsudc_complete_request(hsep, hsreq, 0);
415 return 1;
416 }
417
418 return 0;
419}
420
421/**
422 * s3c_hsudc_epin_intr - Handle in-endpoint interrupt.
423 * @hsudc - Device controller for which the interrupt is to be handled.
424 * @ep_idx - Endpoint number on which an interrupt is pending.
425 *
426 * Handles interrupt for a in-endpoint. The interrupts that are handled are
427 * stall and data transmit complete interrupt.
428 */
429static void s3c_hsudc_epin_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
430{
431 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
432 struct s3c_hsudc_req *hsreq;
433 u32 csr;
434
435 csr = readl((u32)hsudc->regs + S3C_ESR);
436 if (csr & S3C_ESR_STALL) {
437 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
438 return;
439 }
440
441 if (csr & S3C_ESR_TX_SUCCESS) {
442 writel(S3C_ESR_TX_SUCCESS, hsudc->regs + S3C_ESR);
443 if (list_empty(&hsep->queue))
444 return;
445
446 hsreq = list_entry(hsep->queue.next,
447 struct s3c_hsudc_req, queue);
448 if ((s3c_hsudc_write_fifo(hsep, hsreq) == 0) &&
449 (csr & S3C_ESR_PSIF_TWO))
450 s3c_hsudc_write_fifo(hsep, hsreq);
451 }
452}
453
454/**
455 * s3c_hsudc_epout_intr - Handle out-endpoint interrupt.
456 * @hsudc - Device controller for which the interrupt is to be handled.
457 * @ep_idx - Endpoint number on which an interrupt is pending.
458 *
459 * Handles interrupt for a out-endpoint. The interrupts that are handled are
460 * stall, flush and data ready interrupt.
461 */
462static void s3c_hsudc_epout_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
463{
464 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
465 struct s3c_hsudc_req *hsreq;
466 u32 csr;
467
468 csr = readl((u32)hsudc->regs + S3C_ESR);
469 if (csr & S3C_ESR_STALL) {
470 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
471 return;
472 }
473
474 if (csr & S3C_ESR_FLUSH) {
475 __orr32(hsudc->regs + S3C_ECR, S3C_ECR_FLUSH);
476 return;
477 }
478
479 if (csr & S3C_ESR_RX_SUCCESS) {
480 if (list_empty(&hsep->queue))
481 return;
482
483 hsreq = list_entry(hsep->queue.next,
484 struct s3c_hsudc_req, queue);
485 if (((s3c_hsudc_read_fifo(hsep, hsreq)) == 0) &&
486 (csr & S3C_ESR_PSIF_TWO))
487 s3c_hsudc_read_fifo(hsep, hsreq);
488 }
489}
490
491/** s3c_hsudc_set_halt - Set or clear a endpoint halt.
492 * @_ep: Endpoint on which halt has to be set or cleared.
493 * @value: 1 for setting halt on endpoint, 0 to clear halt.
494 *
495 * Set or clear endpoint halt. If halt is set, the endpoint is stopped.
496 * If halt is cleared, for in-endpoints, if there are any pending
497 * transfer requests, transfers are started.
498 */
499static int s3c_hsudc_set_halt(struct usb_ep *_ep, int value)
500{
501 struct s3c_hsudc_ep *hsep = our_ep(_ep);
502 struct s3c_hsudc *hsudc = hsep->dev;
503 struct s3c_hsudc_req *hsreq;
504 unsigned long irqflags;
505 u32 ecr;
506 u32 offset;
507
508 if (value && ep_is_in(hsep) && !list_empty(&hsep->queue))
509 return -EAGAIN;
510
511 spin_lock_irqsave(&hsudc->lock, irqflags);
512 set_index(hsudc, ep_index(hsep));
513 offset = (ep_index(hsep)) ? S3C_ECR : S3C_EP0CR;
514 ecr = readl(hsudc->regs + offset);
515
516 if (value) {
517 ecr |= S3C_ECR_STALL;
518 if (ep_index(hsep))
519 ecr |= S3C_ECR_FLUSH;
520 hsep->stopped = 1;
521 } else {
522 ecr &= ~S3C_ECR_STALL;
523 hsep->stopped = hsep->wedge = 0;
524 }
525 writel(ecr, hsudc->regs + offset);
526
527 if (ep_is_in(hsep) && !list_empty(&hsep->queue) && !value) {
528 hsreq = list_entry(hsep->queue.next,
529 struct s3c_hsudc_req, queue);
530 if (hsreq)
531 s3c_hsudc_write_fifo(hsep, hsreq);
532 }
533
534 spin_unlock_irqrestore(&hsudc->lock, irqflags);
535 return 0;
536}
537
538/** s3c_hsudc_set_wedge - Sets the halt feature with the clear requests ignored
539 * @_ep: Endpoint on which wedge has to be set.
540 *
541 * Sets the halt feature with the clear requests ignored.
542 */
543static int s3c_hsudc_set_wedge(struct usb_ep *_ep)
544{
545 struct s3c_hsudc_ep *hsep = our_ep(_ep);
546
547 if (!hsep)
548 return -EINVAL;
549
550 hsep->wedge = 1;
551 return usb_ep_set_halt(_ep);
552}
553
554/** s3c_hsudc_handle_reqfeat - Handle set feature or clear feature requests.
555 * @_ep: Device controller on which the set/clear feature needs to be handled.
556 * @ctrl: Control request as received on the endpoint 0.
557 *
558 * Handle set feature or clear feature control requests on the control endpoint.
559 */
560static int s3c_hsudc_handle_reqfeat(struct s3c_hsudc *hsudc,
561 struct usb_ctrlrequest *ctrl)
562{
563 struct s3c_hsudc_ep *hsep;
564 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
565 u8 ep_num = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
566
567 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
568 hsep = &hsudc->ep[ep_num];
569 switch (le16_to_cpu(ctrl->wValue)) {
570 case USB_ENDPOINT_HALT:
571 if (set || (!set && !hsep->wedge))
572 s3c_hsudc_set_halt(&hsep->ep, set);
573 return 0;
574 }
575 }
576
577 return -ENOENT;
578}
579
580/**
581 * s3c_hsudc_process_req_status - Handle get status control request.
582 * @hsudc: Device controller on which get status request has be handled.
583 * @ctrl: Control request as received on the endpoint 0.
584 *
585 * Handle get status control request received on control endpoint.
586 */
587static void s3c_hsudc_process_req_status(struct s3c_hsudc *hsudc,
588 struct usb_ctrlrequest *ctrl)
589{
590 struct s3c_hsudc_ep *hsep0 = &hsudc->ep[0];
591 struct s3c_hsudc_req hsreq;
592 struct s3c_hsudc_ep *hsep;
593 __le16 reply;
594 u8 epnum;
595
596 switch (ctrl->bRequestType & USB_RECIP_MASK) {
597 case USB_RECIP_DEVICE:
598 reply = cpu_to_le16(0);
599 break;
600
601 case USB_RECIP_INTERFACE:
602 reply = cpu_to_le16(0);
603 break;
604
605 case USB_RECIP_ENDPOINT:
606 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
607 hsep = &hsudc->ep[epnum];
608 reply = cpu_to_le16(hsep->stopped ? 1 : 0);
609 break;
610 }
611
612 INIT_LIST_HEAD(&hsreq.queue);
613 hsreq.req.length = 2;
614 hsreq.req.buf = &reply;
615 hsreq.req.actual = 0;
616 hsreq.req.complete = NULL;
617 s3c_hsudc_write_fifo(hsep0, &hsreq);
618}
619
620/**
621 * s3c_hsudc_process_setup - Process control request received on endpoint 0.
622 * @hsudc: Device controller on which control request has been received.
623 *
624 * Read the control request received on endpoint 0, decode it and handle
625 * the request.
626 */
627static void s3c_hsudc_process_setup(struct s3c_hsudc *hsudc)
628{
629 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
630 struct usb_ctrlrequest ctrl = {0};
631 int ret;
632
633 s3c_hsudc_nuke_ep(hsep, -EPROTO);
634 s3c_hsudc_read_setup_pkt(hsudc, (u16 *)&ctrl);
635
636 if (ctrl.bRequestType & USB_DIR_IN) {
637 hsep->bEndpointAddress |= USB_DIR_IN;
638 hsudc->ep0state = DATA_STATE_XMIT;
639 } else {
640 hsep->bEndpointAddress &= ~USB_DIR_IN;
641 hsudc->ep0state = DATA_STATE_RECV;
642 }
643
644 switch (ctrl.bRequest) {
645 case USB_REQ_SET_ADDRESS:
646 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
647 break;
648 hsudc->ep0state = WAIT_FOR_SETUP;
649 return;
650
651 case USB_REQ_GET_STATUS:
652 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
653 break;
654 s3c_hsudc_process_req_status(hsudc, &ctrl);
655 return;
656
657 case USB_REQ_SET_FEATURE:
658 case USB_REQ_CLEAR_FEATURE:
659 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
660 break;
661 s3c_hsudc_handle_reqfeat(hsudc, &ctrl);
662 hsudc->ep0state = WAIT_FOR_SETUP;
663 return;
664 }
665
666 if (hsudc->driver) {
667 spin_unlock(&hsudc->lock);
668 ret = hsudc->driver->setup(&hsudc->gadget, &ctrl);
669 spin_lock(&hsudc->lock);
670
671 if (ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
672 hsep->bEndpointAddress &= ~USB_DIR_IN;
673 hsudc->ep0state = WAIT_FOR_SETUP;
674 }
675
676 if (ret < 0) {
677 dev_err(hsudc->dev, "setup failed, returned %d\n",
678 ret);
679 s3c_hsudc_set_halt(&hsep->ep, 1);
680 hsudc->ep0state = WAIT_FOR_SETUP;
681 hsep->bEndpointAddress &= ~USB_DIR_IN;
682 }
683 }
684}
685
686/** s3c_hsudc_handle_ep0_intr - Handle endpoint 0 interrupt.
687 * @hsudc: Device controller on which endpoint 0 interrupt has occured.
688 *
689 * Handle endpoint 0 interrupt when it occurs. EP0 interrupt could occur
690 * when a stall handshake is sent to host or data is sent/received on
691 * endpoint 0.
692 */
693static void s3c_hsudc_handle_ep0_intr(struct s3c_hsudc *hsudc)
694{
695 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
696 struct s3c_hsudc_req *hsreq;
697 u32 csr = readl(hsudc->regs + S3C_EP0SR);
698 u32 ecr;
699
700 if (csr & S3C_EP0SR_STALL) {
701 ecr = readl(hsudc->regs + S3C_EP0CR);
702 ecr &= ~(S3C_ECR_STALL | S3C_ECR_FLUSH);
703 writel(ecr, hsudc->regs + S3C_EP0CR);
704
705 writel(S3C_EP0SR_STALL, hsudc->regs + S3C_EP0SR);
706 hsep->stopped = 0;
707
708 s3c_hsudc_nuke_ep(hsep, -ECONNABORTED);
709 hsudc->ep0state = WAIT_FOR_SETUP;
710 hsep->bEndpointAddress &= ~USB_DIR_IN;
711 return;
712 }
713
714 if (csr & S3C_EP0SR_TX_SUCCESS) {
715 writel(S3C_EP0SR_TX_SUCCESS, hsudc->regs + S3C_EP0SR);
716 if (ep_is_in(hsep)) {
717 if (list_empty(&hsep->queue))
718 return;
719
720 hsreq = list_entry(hsep->queue.next,
721 struct s3c_hsudc_req, queue);
722 s3c_hsudc_write_fifo(hsep, hsreq);
723 }
724 }
725
726 if (csr & S3C_EP0SR_RX_SUCCESS) {
727 if (hsudc->ep0state == WAIT_FOR_SETUP)
728 s3c_hsudc_process_setup(hsudc);
729 else {
730 if (!ep_is_in(hsep)) {
731 if (list_empty(&hsep->queue))
732 return;
733 hsreq = list_entry(hsep->queue.next,
734 struct s3c_hsudc_req, queue);
735 s3c_hsudc_read_fifo(hsep, hsreq);
736 }
737 }
738 }
739}
740
741/**
742 * s3c_hsudc_ep_enable - Enable a endpoint.
743 * @_ep: The endpoint to be enabled.
744 * @desc: Endpoint descriptor.
745 *
746 * Enables a endpoint when called from the gadget driver. Endpoint stall if
747 * any is cleared, transfer type is configured and endpoint interrupt is
748 * enabled.
749 */
750static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
751 const struct usb_endpoint_descriptor *desc)
752{
753 struct s3c_hsudc_ep *hsep;
754 struct s3c_hsudc *hsudc;
755 unsigned long flags;
756 u32 ecr = 0;
757
758 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
759 if (!_ep || !desc || hsep->desc || _ep->name == ep0name
760 || desc->bDescriptorType != USB_DT_ENDPOINT
761 || hsep->bEndpointAddress != desc->bEndpointAddress
762 || ep_maxpacket(hsep) < le16_to_cpu(desc->wMaxPacketSize))
763 return -EINVAL;
764
765 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
766 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(hsep))
767 || !desc->wMaxPacketSize)
768 return -ERANGE;
769
770 hsudc = hsep->dev;
771 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
772 return -ESHUTDOWN;
773
774 spin_lock_irqsave(&hsudc->lock, flags);
775
776 set_index(hsudc, hsep->bEndpointAddress);
777 ecr |= ((usb_endpoint_xfer_int(desc)) ? S3C_ECR_IEMS : S3C_ECR_DUEN);
778 writel(ecr, hsudc->regs + S3C_ECR);
779
780 hsep->stopped = hsep->wedge = 0;
781 hsep->desc = desc;
782 hsep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
783
784 s3c_hsudc_set_halt(_ep, 0);
785 __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
786
787 spin_unlock_irqrestore(&hsudc->lock, flags);
788 return 0;
789}
790
791/**
792 * s3c_hsudc_ep_disable - Disable a endpoint.
793 * @_ep: The endpoint to be disabled.
794 * @desc: Endpoint descriptor.
795 *
796 * Disables a endpoint when called from the gadget driver.
797 */
798static int s3c_hsudc_ep_disable(struct usb_ep *_ep)
799{
800 struct s3c_hsudc_ep *hsep = our_ep(_ep);
801 struct s3c_hsudc *hsudc = hsep->dev;
802 unsigned long flags;
803
804 if (!_ep || !hsep->desc)
805 return -EINVAL;
806
807 spin_lock_irqsave(&hsudc->lock, flags);
808
809 set_index(hsudc, hsep->bEndpointAddress);
810 __clear_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
811
812 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
813
814 hsep->desc = 0;
815 hsep->stopped = 1;
816
817 spin_unlock_irqrestore(&hsudc->lock, flags);
818 return 0;
819}
820
821/**
822 * s3c_hsudc_alloc_request - Allocate a new request.
823 * @_ep: Endpoint for which request is allocated (not used).
824 * @gfp_flags: Flags used for the allocation.
825 *
826 * Allocates a single transfer request structure when called from gadget driver.
827 */
828static struct usb_request *s3c_hsudc_alloc_request(struct usb_ep *_ep,
829 gfp_t gfp_flags)
830{
831 struct s3c_hsudc_req *hsreq;
832
833 hsreq = kzalloc(sizeof *hsreq, gfp_flags);
834 if (!hsreq)
835 return 0;
836
837 INIT_LIST_HEAD(&hsreq->queue);
838 return &hsreq->req;
839}
840
841/**
842 * s3c_hsudc_free_request - Deallocate a request.
843 * @ep: Endpoint for which request is deallocated (not used).
844 * @_req: Request to be deallocated.
845 *
846 * Allocates a single transfer request structure when called from gadget driver.
847 */
848static void s3c_hsudc_free_request(struct usb_ep *ep, struct usb_request *_req)
849{
850 struct s3c_hsudc_req *hsreq;
851
852 hsreq = container_of(_req, struct s3c_hsudc_req, req);
853 WARN_ON(!list_empty(&hsreq->queue));
854 kfree(hsreq);
855}
856
857/**
858 * s3c_hsudc_queue - Queue a transfer request for the endpoint.
859 * @_ep: Endpoint for which the request is queued.
860 * @_req: Request to be queued.
861 * @gfp_flags: Not used.
862 *
863 * Start or enqueue a request for a endpoint when called from gadget driver.
864 */
865static int s3c_hsudc_queue(struct usb_ep *_ep, struct usb_request *_req,
866 gfp_t gfp_flags)
867{
868 struct s3c_hsudc_req *hsreq;
869 struct s3c_hsudc_ep *hsep;
870 struct s3c_hsudc *hsudc;
871 unsigned long flags;
872 u32 offset;
873 u32 csr;
874
875 hsreq = container_of(_req, struct s3c_hsudc_req, req);
876 if ((!_req || !_req->complete || !_req->buf ||
877 !list_empty(&hsreq->queue)))
878 return -EINVAL;
879
880 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
881 hsudc = hsep->dev;
882 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
883 return -ESHUTDOWN;
884
885 spin_lock_irqsave(&hsudc->lock, flags);
886 set_index(hsudc, hsep->bEndpointAddress);
887
888 _req->status = -EINPROGRESS;
889 _req->actual = 0;
890
891 if (!ep_index(hsep) && _req->length == 0) {
892 hsudc->ep0state = WAIT_FOR_SETUP;
893 s3c_hsudc_complete_request(hsep, hsreq, 0);
894 spin_unlock_irqrestore(&hsudc->lock, flags);
895 return 0;
896 }
897
898 if (list_empty(&hsep->queue) && !hsep->stopped) {
899 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
900 if (ep_is_in(hsep)) {
901 csr = readl((u32)hsudc->regs + offset);
902 if (!(csr & S3C_ESR_TX_SUCCESS) &&
903 (s3c_hsudc_write_fifo(hsep, hsreq) == 1))
904 hsreq = 0;
905 } else {
906 csr = readl((u32)hsudc->regs + offset);
907 if ((csr & S3C_ESR_RX_SUCCESS)
908 && (s3c_hsudc_read_fifo(hsep, hsreq) == 1))
909 hsreq = 0;
910 }
911 }
912
913 if (hsreq != 0)
914 list_add_tail(&hsreq->queue, &hsep->queue);
915
916 spin_unlock_irqrestore(&hsudc->lock, flags);
917 return 0;
918}
919
920/**
921 * s3c_hsudc_dequeue - Dequeue a transfer request from an endpoint.
922 * @_ep: Endpoint from which the request is dequeued.
923 * @_req: Request to be dequeued.
924 *
925 * Dequeue a request from a endpoint when called from gadget driver.
926 */
927static int s3c_hsudc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
928{
929 struct s3c_hsudc_ep *hsep = our_ep(_ep);
930 struct s3c_hsudc *hsudc = hsep->dev;
931 struct s3c_hsudc_req *hsreq;
932 unsigned long flags;
933
934 hsep = container_of(_ep, struct s3c_hsudc_ep, ep);
935 if (!_ep || hsep->ep.name == ep0name)
936 return -EINVAL;
937
938 spin_lock_irqsave(&hsudc->lock, flags);
939
940 list_for_each_entry(hsreq, &hsep->queue, queue) {
941 if (&hsreq->req == _req)
942 break;
943 }
944 if (&hsreq->req != _req) {
945 spin_unlock_irqrestore(&hsudc->lock, flags);
946 return -EINVAL;
947 }
948
949 set_index(hsudc, hsep->bEndpointAddress);
950 s3c_hsudc_complete_request(hsep, hsreq, -ECONNRESET);
951
952 spin_unlock_irqrestore(&hsudc->lock, flags);
953 return 0;
954}
955
956static struct usb_ep_ops s3c_hsudc_ep_ops = {
957 .enable = s3c_hsudc_ep_enable,
958 .disable = s3c_hsudc_ep_disable,
959 .alloc_request = s3c_hsudc_alloc_request,
960 .free_request = s3c_hsudc_free_request,
961 .queue = s3c_hsudc_queue,
962 .dequeue = s3c_hsudc_dequeue,
963 .set_halt = s3c_hsudc_set_halt,
964 .set_wedge = s3c_hsudc_set_wedge,
965};
966
967/**
968 * s3c_hsudc_initep - Initialize a endpoint to default state.
969 * @hsudc - Reference to the device controller.
970 * @hsep - Endpoint to be initialized.
971 * @epnum - Address to be assigned to the endpoint.
972 *
973 * Initialize a endpoint with default configuration.
974 */
975static void s3c_hsudc_initep(struct s3c_hsudc *hsudc,
976 struct s3c_hsudc_ep *hsep, int epnum)
977{
978 char *dir;
979
980 if ((epnum % 2) == 0) {
981 dir = "out";
982 } else {
983 dir = "in";
984 hsep->bEndpointAddress = USB_DIR_IN;
985 }
986
987 hsep->bEndpointAddress |= epnum;
988 if (epnum)
989 snprintf(hsep->name, sizeof(hsep->name), "ep%d%s", epnum, dir);
990 else
991 snprintf(hsep->name, sizeof(hsep->name), "%s", ep0name);
992
993 INIT_LIST_HEAD(&hsep->queue);
994 INIT_LIST_HEAD(&hsep->ep.ep_list);
995 if (epnum)
996 list_add_tail(&hsep->ep.ep_list, &hsudc->gadget.ep_list);
997
998 hsep->dev = hsudc;
999 hsep->ep.name = hsep->name;
1000 hsep->ep.maxpacket = epnum ? 512 : 64;
1001 hsep->ep.ops = &s3c_hsudc_ep_ops;
1002 hsep->fifo = hsudc->regs + S3C_BR(epnum);
1003 hsep->desc = 0;
1004 hsep->stopped = 0;
1005 hsep->wedge = 0;
1006
1007 set_index(hsudc, epnum);
1008 writel(hsep->ep.maxpacket, hsudc->regs + S3C_MPR);
1009}
1010
1011/**
1012 * s3c_hsudc_setup_ep - Configure all endpoints to default state.
1013 * @hsudc: Reference to device controller.
1014 *
1015 * Configures all endpoints to default state.
1016 */
1017static void s3c_hsudc_setup_ep(struct s3c_hsudc *hsudc)
1018{
1019 int epnum;
1020
1021 hsudc->ep0state = WAIT_FOR_SETUP;
1022 INIT_LIST_HEAD(&hsudc->gadget.ep_list);
1023 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++)
1024 s3c_hsudc_initep(hsudc, &hsudc->ep[epnum], epnum);
1025}
1026
1027/**
1028 * s3c_hsudc_reconfig - Reconfigure the device controller to default state.
1029 * @hsudc: Reference to device controller.
1030 *
1031 * Reconfigures the device controller registers to a default state.
1032 */
1033static void s3c_hsudc_reconfig(struct s3c_hsudc *hsudc)
1034{
1035 writel(0xAA, hsudc->regs + S3C_EDR);
1036 writel(1, hsudc->regs + S3C_EIER);
1037 writel(0, hsudc->regs + S3C_TR);
1038 writel(S3C_SCR_DTZIEN_EN | S3C_SCR_RRD_EN | S3C_SCR_SUS_EN |
1039 S3C_SCR_RST_EN, hsudc->regs + S3C_SCR);
1040 writel(0, hsudc->regs + S3C_EP0CR);
1041
1042 s3c_hsudc_setup_ep(hsudc);
1043}
1044
1045/**
1046 * s3c_hsudc_irq - Interrupt handler for device controller.
1047 * @irq: Not used.
1048 * @_dev: Reference to the device controller.
1049 *
1050 * Interrupt handler for the device controller. This handler handles controller
1051 * interrupts and endpoint interrupts.
1052 */
1053static irqreturn_t s3c_hsudc_irq(int irq, void *_dev)
1054{
1055 struct s3c_hsudc *hsudc = _dev;
1056 struct s3c_hsudc_ep *hsep;
1057 u32 ep_intr;
1058 u32 sys_status;
1059 u32 ep_idx;
1060
1061 spin_lock(&hsudc->lock);
1062
1063 sys_status = readl(hsudc->regs + S3C_SSR);
1064 ep_intr = readl(hsudc->regs + S3C_EIR) & 0x3FF;
1065
1066 if (!ep_intr && !(sys_status & S3C_SSR_DTZIEN_EN)) {
1067 spin_unlock(&hsudc->lock);
1068 return IRQ_HANDLED;
1069 }
1070
1071 if (sys_status) {
1072 if (sys_status & S3C_SSR_VBUSON)
1073 writel(S3C_SSR_VBUSON, hsudc->regs + S3C_SSR);
1074
1075 if (sys_status & S3C_SSR_ERR)
1076 writel(S3C_SSR_ERR, hsudc->regs + S3C_SSR);
1077
1078 if (sys_status & S3C_SSR_SDE) {
1079 writel(S3C_SSR_SDE, hsudc->regs + S3C_SSR);
1080 hsudc->gadget.speed = (sys_status & S3C_SSR_HSP) ?
1081 USB_SPEED_HIGH : USB_SPEED_FULL;
1082 }
1083
1084 if (sys_status & S3C_SSR_SUSPEND) {
1085 writel(S3C_SSR_SUSPEND, hsudc->regs + S3C_SSR);
1086 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1087 && hsudc->driver && hsudc->driver->suspend)
1088 hsudc->driver->suspend(&hsudc->gadget);
1089 }
1090
1091 if (sys_status & S3C_SSR_RESUME) {
1092 writel(S3C_SSR_RESUME, hsudc->regs + S3C_SSR);
1093 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1094 && hsudc->driver && hsudc->driver->resume)
1095 hsudc->driver->resume(&hsudc->gadget);
1096 }
1097
1098 if (sys_status & S3C_SSR_RESET) {
1099 writel(S3C_SSR_RESET, hsudc->regs + S3C_SSR);
1100 for (ep_idx = 0; ep_idx < hsudc->pd->epnum; ep_idx++) {
1101 hsep = &hsudc->ep[ep_idx];
1102 hsep->stopped = 1;
1103 s3c_hsudc_nuke_ep(hsep, -ECONNRESET);
1104 }
1105 s3c_hsudc_reconfig(hsudc);
1106 hsudc->ep0state = WAIT_FOR_SETUP;
1107 }
1108 }
1109
1110 if (ep_intr & S3C_EIR_EP0) {
1111 writel(S3C_EIR_EP0, hsudc->regs + S3C_EIR);
1112 set_index(hsudc, 0);
1113 s3c_hsudc_handle_ep0_intr(hsudc);
1114 }
1115
1116 ep_intr >>= 1;
1117 ep_idx = 1;
1118 while (ep_intr) {
1119 if (ep_intr & 1) {
1120 hsep = &hsudc->ep[ep_idx];
1121 set_index(hsudc, ep_idx);
1122 writel(1 << ep_idx, hsudc->regs + S3C_EIR);
1123 if (ep_is_in(hsep))
1124 s3c_hsudc_epin_intr(hsudc, ep_idx);
1125 else
1126 s3c_hsudc_epout_intr(hsudc, ep_idx);
1127 }
1128 ep_intr >>= 1;
1129 ep_idx++;
1130 }
1131
1132 spin_unlock(&hsudc->lock);
1133 return IRQ_HANDLED;
1134}
1135
1136int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1137 int (*bind)(struct usb_gadget *))
1138{
1139 struct s3c_hsudc *hsudc = the_controller;
1140 int ret;
1141
1142 if (!driver
1143 || (driver->speed != USB_SPEED_FULL &&
1144 driver->speed != USB_SPEED_HIGH)
1145 || !bind
1146 || !driver->unbind || !driver->disconnect || !driver->setup)
1147 return -EINVAL;
1148
1149 if (!hsudc)
1150 return -ENODEV;
1151
1152 if (hsudc->driver)
1153 return -EBUSY;
1154
1155 hsudc->driver = driver;
1156 hsudc->gadget.dev.driver = &driver->driver;
1157 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
1158 ret = device_add(&hsudc->gadget.dev);
1159 if (ret) {
1160 dev_err(hsudc->dev, "failed to probe gadget device");
1161 return ret;
1162 }
1163
1164 ret = bind(&hsudc->gadget);
1165 if (ret) {
1166 dev_err(hsudc->dev, "%s: bind failed\n", hsudc->gadget.name);
1167 device_del(&hsudc->gadget.dev);
1168
1169 hsudc->driver = NULL;
1170 hsudc->gadget.dev.driver = NULL;
1171 return ret;
1172 }
1173
1174 enable_irq(hsudc->irq);
1175 dev_info(hsudc->dev, "bound driver %s\n", driver->driver.name);
1176
1177 s3c_hsudc_reconfig(hsudc);
1178 s3c_hsudc_init_phy();
1179 if (hsudc->pd->gpio_init)
1180 hsudc->pd->gpio_init();
1181
1182 return 0;
1183}
1184EXPORT_SYMBOL(usb_gadget_probe_driver);
1185
1186int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1187{
1188 struct s3c_hsudc *hsudc = the_controller;
1189 unsigned long flags;
1190
1191 if (!hsudc)
1192 return -ENODEV;
1193
1194 if (!driver || driver != hsudc->driver || !driver->unbind)
1195 return -EINVAL;
1196
1197 spin_lock_irqsave(&hsudc->lock, flags);
1198 hsudc->driver = 0;
1199 s3c_hsudc_uninit_phy();
1200 if (hsudc->pd->gpio_uninit)
1201 hsudc->pd->gpio_uninit();
1202 s3c_hsudc_stop_activity(hsudc, driver);
1203 spin_unlock_irqrestore(&hsudc->lock, flags);
1204
1205 driver->unbind(&hsudc->gadget);
1206 device_del(&hsudc->gadget.dev);
1207 disable_irq(hsudc->irq);
1208
1209 dev_info(hsudc->dev, "unregistered gadget driver '%s'\n",
1210 driver->driver.name);
1211 return 0;
1212}
1213EXPORT_SYMBOL(usb_gadget_unregister_driver);
1214
1215static inline u32 s3c_hsudc_read_frameno(struct s3c_hsudc *hsudc)
1216{
1217 return readl(hsudc->regs + S3C_FNR) & 0x3FF;
1218}
1219
1220static int s3c_hsudc_gadget_getframe(struct usb_gadget *gadget)
1221{
1222 return s3c_hsudc_read_frameno(to_hsudc(gadget));
1223}
1224
1225static struct usb_gadget_ops s3c_hsudc_gadget_ops = {
1226 .get_frame = s3c_hsudc_gadget_getframe,
1227};
1228
1229static int s3c_hsudc_probe(struct platform_device *pdev)
1230{
1231 struct device *dev = &pdev->dev;
1232 struct resource *res;
1233 struct s3c_hsudc *hsudc;
1234 struct s3c24xx_hsudc_platdata *pd = pdev->dev.platform_data;
1235 int ret;
1236
1237 hsudc = kzalloc(sizeof(struct s3c_hsudc) +
1238 sizeof(struct s3c_hsudc_ep) * pd->epnum,
1239 GFP_KERNEL);
1240 if (!hsudc) {
1241 dev_err(dev, "cannot allocate memory\n");
1242 return -ENOMEM;
1243 }
1244
1245 the_controller = hsudc;
1246 platform_set_drvdata(pdev, dev);
1247 hsudc->dev = dev;
1248 hsudc->pd = pdev->dev.platform_data;
1249
1250 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1251 if (!res) {
1252 dev_err(dev, "unable to obtain driver resource data\n");
1253 ret = -ENODEV;
1254 goto err_res;
1255 }
1256
1257 hsudc->mem_rsrc = request_mem_region(res->start, resource_size(res),
1258 dev_name(&pdev->dev));
1259 if (!hsudc->mem_rsrc) {
1260 dev_err(dev, "failed to reserve register area\n");
1261 ret = -ENODEV;
1262 goto err_res;
1263 }
1264
1265 hsudc->regs = ioremap(res->start, resource_size(res));
1266 if (!hsudc->regs) {
1267 dev_err(dev, "error mapping device register area\n");
1268 ret = -EBUSY;
1269 goto err_remap;
1270 }
1271
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);
1286
1287 device_initialize(&hsudc->gadget.dev);
1288 dev_set_name(&hsudc->gadget.dev, "gadget");
1289
1290 hsudc->gadget.is_dualspeed = 1;
1291 hsudc->gadget.ops = &s3c_hsudc_gadget_ops;
1292 hsudc->gadget.name = dev_name(dev);
1293 hsudc->gadget.dev.parent = dev;
1294 hsudc->gadget.dev.dma_mask = dev->dma_mask;
1295 hsudc->gadget.ep0 = &hsudc->ep[0].ep;
1296
1297 hsudc->gadget.is_otg = 0;
1298 hsudc->gadget.is_a_peripheral = 0;
1299
1300 s3c_hsudc_setup_ep(hsudc);
1301
1302 hsudc->uclk = clk_get(&pdev->dev, "usb-device");
1303 if (IS_ERR(hsudc->uclk)) {
1304 dev_err(dev, "failed to find usb-device clock source\n");
1305 ret = PTR_ERR(hsudc->uclk);
1306 goto err_clk;
1307 }
1308 clk_enable(hsudc->uclk);
1309
1310 local_irq_disable();
1311
1312 disable_irq(hsudc->irq);
1313 local_irq_enable();
1314 return 0;
1315err_clk:
1316 free_irq(hsudc->irq, hsudc);
1317err_irq:
1318 iounmap(hsudc->regs);
1319
1320err_remap:
1321 release_resource(hsudc->mem_rsrc);
1322 kfree(hsudc->mem_rsrc);
1323
1324err_res:
1325 kfree(hsudc);
1326 return ret;
1327}
1328
1329static struct platform_driver s3c_hsudc_driver = {
1330 .driver = {
1331 .owner = THIS_MODULE,
1332 .name = "s3c-hsudc",
1333 },
1334 .probe = s3c_hsudc_probe,
1335};
1336
1337static int __init s3c_hsudc_modinit(void)
1338{
1339 return platform_driver_register(&s3c_hsudc_driver);
1340}
1341
1342static void __exit s3c_hsudc_modexit(void)
1343{
1344 platform_driver_unregister(&s3c_hsudc_driver);
1345}
1346
1347module_init(s3c_hsudc_modinit);
1348module_exit(s3c_hsudc_modexit);
1349
1350MODULE_DESCRIPTION("Samsung S3C24XX USB high-speed controller driver");
1351MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1352MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/s3c2410_udc.c b/drivers/usb/gadget/s3c2410_udc.c
index ea2b3c7ebee5..100f2635cf0a 100644
--- a/drivers/usb/gadget/s3c2410_udc.c
+++ b/drivers/usb/gadget/s3c2410_udc.c
@@ -36,6 +36,7 @@
36#include <linux/platform_device.h> 36#include <linux/platform_device.h>
37#include <linux/clk.h> 37#include <linux/clk.h>
38#include <linux/gpio.h> 38#include <linux/gpio.h>
39#include <linux/prefetch.h>
39 40
40#include <linux/debugfs.h> 41#include <linux/debugfs.h>
41#include <linux/seq_file.h> 42#include <linux/seq_file.h>
@@ -902,7 +903,7 @@ static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
902 int pwr_reg; 903 int pwr_reg;
903 int ep0csr; 904 int ep0csr;
904 int i; 905 int i;
905 u32 idx; 906 u32 idx, idx2;
906 unsigned long flags; 907 unsigned long flags;
907 908
908 spin_lock_irqsave(&dev->lock, flags); 909 spin_lock_irqsave(&dev->lock, flags);
@@ -1017,6 +1018,20 @@ static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
1017 } 1018 }
1018 } 1019 }
1019 1020
1021 /* what else causes this interrupt? a receive! who is it? */
1022 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
1023 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
1024 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
1025 udc_write(i, S3C2410_UDC_INDEX_REG);
1026
1027 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
1028 s3c2410_udc_handle_ep(&dev->ep[i]);
1029
1030 /* restore index */
1031 udc_write(idx2, S3C2410_UDC_INDEX_REG);
1032 }
1033 }
1034
1020 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD); 1035 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
1021 1036
1022 /* Restore old index */ 1037 /* Restore old index */
@@ -1467,7 +1482,9 @@ static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1467{ 1482{
1468 dprintk(DEBUG_NORMAL, "%s()\n", __func__); 1483 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1469 1484
1470 if (udc_info && udc_info->udc_command) { 1485 if (udc_info && (udc_info->udc_command ||
1486 gpio_is_valid(udc_info->pullup_pin))) {
1487
1471 if (is_on) 1488 if (is_on)
1472 s3c2410_udc_enable(udc); 1489 s3c2410_udc_enable(udc);
1473 else { 1490 else {
@@ -1544,6 +1561,32 @@ static const struct usb_gadget_ops s3c2410_ops = {
1544 .vbus_draw = s3c2410_vbus_draw, 1561 .vbus_draw = s3c2410_vbus_draw,
1545}; 1562};
1546 1563
1564static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1565{
1566 if (!udc_info)
1567 return;
1568
1569 if (udc_info->udc_command) {
1570 udc_info->udc_command(S3C2410_UDC_P_DISABLE);
1571 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1572 int value;
1573
1574 switch (cmd) {
1575 case S3C2410_UDC_P_ENABLE:
1576 value = 1;
1577 break;
1578 case S3C2410_UDC_P_DISABLE:
1579 value = 0;
1580 break;
1581 default:
1582 return;
1583 }
1584 value ^= udc_info->pullup_pin_inverted;
1585
1586 gpio_set_value(udc_info->pullup_pin, value);
1587 }
1588}
1589
1547/*------------------------- gadget driver handling---------------------------*/ 1590/*------------------------- gadget driver handling---------------------------*/
1548/* 1591/*
1549 * s3c2410_udc_disable 1592 * s3c2410_udc_disable
@@ -1565,8 +1608,7 @@ static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1565 udc_write(0x1F, S3C2410_UDC_EP_INT_REG); 1608 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1566 1609
1567 /* Good bye, cruel world */ 1610 /* Good bye, cruel world */
1568 if (udc_info && udc_info->udc_command) 1611 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1569 udc_info->udc_command(S3C2410_UDC_P_DISABLE);
1570 1612
1571 /* Set speed to unknown */ 1613 /* Set speed to unknown */
1572 dev->gadget.speed = USB_SPEED_UNKNOWN; 1614 dev->gadget.speed = USB_SPEED_UNKNOWN;
@@ -1627,20 +1669,19 @@ static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1627 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG); 1669 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1628 1670
1629 /* time to say "hello, world" */ 1671 /* time to say "hello, world" */
1630 if (udc_info && udc_info->udc_command) 1672 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1631 udc_info->udc_command(S3C2410_UDC_P_ENABLE);
1632} 1673}
1633 1674
1634/* 1675/*
1635 * usb_gadget_register_driver 1676 * usb_gadget_probe_driver
1636 */ 1677 */
1637int usb_gadget_register_driver(struct usb_gadget_driver *driver) 1678int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1679 int (*bind)(struct usb_gadget *))
1638{ 1680{
1639 struct s3c2410_udc *udc = the_controller; 1681 struct s3c2410_udc *udc = the_controller;
1640 int retval; 1682 int retval;
1641 1683
1642 dprintk(DEBUG_NORMAL, "usb_gadget_register_driver() '%s'\n", 1684 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1643 driver->driver.name);
1644 1685
1645 /* Sanity checks */ 1686 /* Sanity checks */
1646 if (!udc) 1687 if (!udc)
@@ -1649,10 +1690,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1649 if (udc->driver) 1690 if (udc->driver)
1650 return -EBUSY; 1691 return -EBUSY;
1651 1692
1652 if (!driver->bind || !driver->setup 1693 if (!bind || !driver->setup || driver->speed < USB_SPEED_FULL) {
1653 || driver->speed < USB_SPEED_FULL) {
1654 printk(KERN_ERR "Invalid driver: bind %p setup %p speed %d\n", 1694 printk(KERN_ERR "Invalid driver: bind %p setup %p speed %d\n",
1655 driver->bind, driver->setup, driver->speed); 1695 bind, driver->setup, driver->speed);
1656 return -EINVAL; 1696 return -EINVAL;
1657 } 1697 }
1658#if defined(MODULE) 1698#if defined(MODULE)
@@ -1675,7 +1715,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1675 dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n", 1715 dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n",
1676 driver->driver.name); 1716 driver->driver.name);
1677 1717
1678 if ((retval = driver->bind (&udc->gadget)) != 0) { 1718 if ((retval = bind(&udc->gadget)) != 0) {
1679 device_del(&udc->gadget.dev); 1719 device_del(&udc->gadget.dev);
1680 goto register_error; 1720 goto register_error;
1681 } 1721 }
@@ -1690,6 +1730,7 @@ register_error:
1690 udc->gadget.dev.driver = NULL; 1730 udc->gadget.dev.driver = NULL;
1691 return retval; 1731 return retval;
1692} 1732}
1733EXPORT_SYMBOL(usb_gadget_probe_driver);
1693 1734
1694/* 1735/*
1695 * usb_gadget_unregister_driver 1736 * usb_gadget_unregister_driver
@@ -1903,6 +1944,17 @@ static int s3c2410_udc_probe(struct platform_device *pdev)
1903 udc->vbus = 1; 1944 udc->vbus = 1;
1904 } 1945 }
1905 1946
1947 if (udc_info && !udc_info->udc_command &&
1948 gpio_is_valid(udc_info->pullup_pin)) {
1949
1950 retval = gpio_request_one(udc_info->pullup_pin,
1951 udc_info->vbus_pin_inverted ?
1952 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1953 "udc pullup");
1954 if (retval)
1955 goto err_vbus_irq;
1956 }
1957
1906 if (s3c2410_udc_debugfs_root) { 1958 if (s3c2410_udc_debugfs_root) {
1907 udc->regs_info = debugfs_create_file("registers", S_IRUGO, 1959 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1908 s3c2410_udc_debugfs_root, 1960 s3c2410_udc_debugfs_root,
@@ -1915,6 +1967,9 @@ static int s3c2410_udc_probe(struct platform_device *pdev)
1915 1967
1916 return 0; 1968 return 0;
1917 1969
1970err_vbus_irq:
1971 if (udc_info && udc_info->vbus_pin > 0)
1972 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1918err_gpio_claim: 1973err_gpio_claim:
1919 if (udc_info && udc_info->vbus_pin > 0) 1974 if (udc_info && udc_info->vbus_pin > 0)
1920 gpio_free(udc_info->vbus_pin); 1975 gpio_free(udc_info->vbus_pin);
@@ -1942,6 +1997,10 @@ static int s3c2410_udc_remove(struct platform_device *pdev)
1942 1997
1943 debugfs_remove(udc->regs_info); 1998 debugfs_remove(udc->regs_info);
1944 1999
2000 if (udc_info && !udc_info->udc_command &&
2001 gpio_is_valid(udc_info->pullup_pin))
2002 gpio_free(udc_info->pullup_pin);
2003
1945 if (udc_info && udc_info->vbus_pin > 0) { 2004 if (udc_info && udc_info->vbus_pin > 0) {
1946 irq = gpio_to_irq(udc_info->vbus_pin); 2005 irq = gpio_to_irq(udc_info->vbus_pin);
1947 free_irq(irq, udc); 2006 free_irq(irq, udc);
@@ -1973,16 +2032,14 @@ static int s3c2410_udc_remove(struct platform_device *pdev)
1973#ifdef CONFIG_PM 2032#ifdef CONFIG_PM
1974static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message) 2033static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1975{ 2034{
1976 if (udc_info && udc_info->udc_command) 2035 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1977 udc_info->udc_command(S3C2410_UDC_P_DISABLE);
1978 2036
1979 return 0; 2037 return 0;
1980} 2038}
1981 2039
1982static int s3c2410_udc_resume(struct platform_device *pdev) 2040static int s3c2410_udc_resume(struct platform_device *pdev)
1983{ 2041{
1984 if (udc_info && udc_info->udc_command) 2042 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1985 udc_info->udc_command(S3C2410_UDC_P_ENABLE);
1986 2043
1987 return 0; 2044 return 0;
1988} 2045}
@@ -2049,7 +2106,6 @@ static void __exit udc_exit(void)
2049} 2106}
2050 2107
2051EXPORT_SYMBOL(usb_gadget_unregister_driver); 2108EXPORT_SYMBOL(usb_gadget_unregister_driver);
2052EXPORT_SYMBOL(usb_gadget_register_driver);
2053 2109
2054module_init(udc_init); 2110module_init(udc_init);
2055module_exit(udc_exit); 2111module_exit(udc_exit);
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index b22eedbc7dc5..1ac57a973aa9 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -137,7 +137,7 @@ MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
137 137
138/*-------------------------------------------------------------------------*/ 138/*-------------------------------------------------------------------------*/
139 139
140static int __ref serial_bind_config(struct usb_configuration *c) 140static int __init serial_bind_config(struct usb_configuration *c)
141{ 141{
142 unsigned i; 142 unsigned i;
143 int status = 0; 143 int status = 0;
@@ -155,13 +155,12 @@ static int __ref serial_bind_config(struct usb_configuration *c)
155 155
156static struct usb_configuration serial_config_driver = { 156static struct usb_configuration serial_config_driver = {
157 /* .label = f(use_acm) */ 157 /* .label = f(use_acm) */
158 .bind = serial_bind_config,
159 /* .bConfigurationValue = f(use_acm) */ 158 /* .bConfigurationValue = f(use_acm) */
160 /* .iConfiguration = DYNAMIC */ 159 /* .iConfiguration = DYNAMIC */
161 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 160 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
162}; 161};
163 162
164static int __ref gs_bind(struct usb_composite_dev *cdev) 163static int __init gs_bind(struct usb_composite_dev *cdev)
165{ 164{
166 int gcnum; 165 int gcnum;
167 struct usb_gadget *gadget = cdev->gadget; 166 struct usb_gadget *gadget = cdev->gadget;
@@ -225,7 +224,8 @@ static int __ref gs_bind(struct usb_composite_dev *cdev)
225 } 224 }
226 225
227 /* register our configuration */ 226 /* register our configuration */
228 status = usb_add_config(cdev, &serial_config_driver); 227 status = usb_add_config(cdev, &serial_config_driver,
228 serial_bind_config);
229 if (status < 0) 229 if (status < 0)
230 goto fail; 230 goto fail;
231 231
@@ -242,7 +242,6 @@ static struct usb_composite_driver gserial_driver = {
242 .name = "g_serial", 242 .name = "g_serial",
243 .dev = &device_desc, 243 .dev = &device_desc,
244 .strings = dev_strings, 244 .strings = dev_strings,
245 .bind = gs_bind,
246}; 245};
247 246
248static int __init init(void) 247static int __init init(void)
@@ -271,7 +270,7 @@ static int __init init(void)
271 } 270 }
272 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; 271 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
273 272
274 return usb_composite_register(&gserial_driver); 273 return usb_composite_probe(&gserial_driver, gs_bind);
275} 274}
276module_init(init); 275module_init(init);
277 276
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 484acfb1a7c5..1fa4f705b0b4 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -26,7 +26,6 @@
26 * be defined (each of type pointer to char): 26 * be defined (each of type pointer to char):
27 * - fsg_string_manufacturer -- name of the manufacturer 27 * - fsg_string_manufacturer -- name of the manufacturer
28 * - fsg_string_product -- name of the product 28 * - fsg_string_product -- name of the product
29 * - fsg_string_serial -- product's serial
30 * - fsg_string_config -- name of the configuration 29 * - fsg_string_config -- name of the configuration
31 * - fsg_string_interface -- name of the interface 30 * - fsg_string_interface -- name of the interface
32 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS 31 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
@@ -54,6 +53,8 @@
54 */ 53 */
55 54
56 55
56#include <linux/usb/storage.h>
57#include <scsi/scsi.h>
57#include <asm/unaligned.h> 58#include <asm/unaligned.h>
58 59
59 60
@@ -153,23 +154,6 @@
153 154
154/*-------------------------------------------------------------------------*/ 155/*-------------------------------------------------------------------------*/
155 156
156/* SCSI device types */
157#define TYPE_DISK 0x00
158#define TYPE_CDROM 0x05
159
160/* USB protocol value = the transport method */
161#define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
162#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
163#define USB_PR_BULK 0x50 /* Bulk-only */
164
165/* USB subclass value = the protocol encapsulation */
166#define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
167#define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
168#define USB_SC_QIC 0x03 /* QIC-157 (tape) */
169#define USB_SC_UFI 0x04 /* UFI (floppy) */
170#define USB_SC_8070 0x05 /* SFF-8070i (removable) */
171#define USB_SC_SCSI 0x06 /* Transparent SCSI */
172
173/* Bulk-only data structures */ 157/* Bulk-only data structures */
174 158
175/* Command Block Wrapper */ 159/* Command Block Wrapper */
@@ -221,33 +205,6 @@ struct interrupt_data {
221/* Length of a SCSI Command Data Block */ 205/* Length of a SCSI Command Data Block */
222#define MAX_COMMAND_SIZE 16 206#define MAX_COMMAND_SIZE 16
223 207
224/* SCSI commands that we recognize */
225#define SC_FORMAT_UNIT 0x04
226#define SC_INQUIRY 0x12
227#define SC_MODE_SELECT_6 0x15
228#define SC_MODE_SELECT_10 0x55
229#define SC_MODE_SENSE_6 0x1a
230#define SC_MODE_SENSE_10 0x5a
231#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
232#define SC_READ_6 0x08
233#define SC_READ_10 0x28
234#define SC_READ_12 0xa8
235#define SC_READ_CAPACITY 0x25
236#define SC_READ_FORMAT_CAPACITIES 0x23
237#define SC_READ_HEADER 0x44
238#define SC_READ_TOC 0x43
239#define SC_RELEASE 0x17
240#define SC_REQUEST_SENSE 0x03
241#define SC_RESERVE 0x16
242#define SC_SEND_DIAGNOSTIC 0x1d
243#define SC_START_STOP_UNIT 0x1b
244#define SC_SYNCHRONIZE_CACHE 0x35
245#define SC_TEST_UNIT_READY 0x00
246#define SC_VERIFY 0x2f
247#define SC_WRITE_6 0x0a
248#define SC_WRITE_10 0x2a
249#define SC_WRITE_12 0xaa
250
251/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ 208/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
252#define SS_NO_SENSE 0 209#define SS_NO_SENSE 0
253#define SS_COMMUNICATION_FAILURE 0x040800 210#define SS_COMMUNICATION_FAILURE 0x040800
@@ -552,7 +509,7 @@ static struct usb_string fsg_strings[] = {
552#ifndef FSG_NO_DEVICE_STRINGS 509#ifndef FSG_NO_DEVICE_STRINGS
553 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer}, 510 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
554 {FSG_STRING_PRODUCT, fsg_string_product}, 511 {FSG_STRING_PRODUCT, fsg_string_product},
555 {FSG_STRING_SERIAL, fsg_string_serial}, 512 {FSG_STRING_SERIAL, ""},
556 {FSG_STRING_CONFIG, fsg_string_config}, 513 {FSG_STRING_CONFIG, fsg_string_config},
557#endif 514#endif
558 {FSG_STRING_INTERFACE, fsg_string_interface}, 515 {FSG_STRING_INTERFACE, fsg_string_interface},
@@ -586,7 +543,7 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
586 ro = curlun->initially_ro; 543 ro = curlun->initially_ro;
587 if (!ro) { 544 if (!ro) {
588 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); 545 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
589 if (-EROFS == PTR_ERR(filp)) 546 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
590 ro = 1; 547 ro = 1;
591 } 548 }
592 if (ro) 549 if (ro)
@@ -601,10 +558,7 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
601 558
602 if (filp->f_path.dentry) 559 if (filp->f_path.dentry)
603 inode = filp->f_path.dentry->d_inode; 560 inode = filp->f_path.dentry->d_inode;
604 if (inode && S_ISBLK(inode->i_mode)) { 561 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
605 if (bdev_read_only(inode->i_bdev))
606 ro = 1;
607 } else if (!inode || !S_ISREG(inode->i_mode)) {
608 LINFO(curlun, "invalid file type: %s\n", filename); 562 LINFO(curlun, "invalid file type: %s\n", filename);
609 goto out; 563 goto out;
610 } 564 }
@@ -754,13 +708,14 @@ static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
754static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr, 708static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
755 const char *buf, size_t count) 709 const char *buf, size_t count)
756{ 710{
757 ssize_t rc = count; 711 ssize_t rc;
758 struct fsg_lun *curlun = fsg_lun_from_dev(dev); 712 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
759 struct rw_semaphore *filesem = dev_get_drvdata(dev); 713 struct rw_semaphore *filesem = dev_get_drvdata(dev);
760 unsigned long ro; 714 unsigned ro;
761 715
762 if (strict_strtoul(buf, 2, &ro)) 716 rc = kstrtouint(buf, 2, &ro);
763 return -EINVAL; 717 if (rc)
718 return rc;
764 719
765 /* 720 /*
766 * Allow the write-enable status to change only while the 721 * Allow the write-enable status to change only while the
@@ -774,6 +729,7 @@ static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
774 curlun->ro = ro; 729 curlun->ro = ro;
775 curlun->initially_ro = ro; 730 curlun->initially_ro = ro;
776 LDBG(curlun, "read-only status set to %d\n", curlun->ro); 731 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
732 rc = count;
777 } 733 }
778 up_read(filesem); 734 up_read(filesem);
779 return rc; 735 return rc;
@@ -784,10 +740,12 @@ static ssize_t fsg_store_nofua(struct device *dev,
784 const char *buf, size_t count) 740 const char *buf, size_t count)
785{ 741{
786 struct fsg_lun *curlun = fsg_lun_from_dev(dev); 742 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
787 unsigned long nofua; 743 unsigned nofua;
744 int ret;
788 745
789 if (strict_strtoul(buf, 2, &nofua)) 746 ret = kstrtouint(buf, 2, &nofua);
790 return -EINVAL; 747 if (ret)
748 return ret;
791 749
792 /* Sync data when switching from async mode to sync */ 750 /* Sync data when switching from async mode to sync */
793 if (!nofua && curlun->nofua) 751 if (!nofua && curlun->nofua)
diff --git a/drivers/usb/gadget/u_audio.c b/drivers/usb/gadget/u_audio.c
index 7a86d2c9109c..59ffe1ecf1c9 100644
--- a/drivers/usb/gadget/u_audio.c
+++ b/drivers/usb/gadget/u_audio.c
@@ -255,6 +255,7 @@ static int gaudio_open_snd_dev(struct gaudio *card)
255 ERROR(card, "No such PCM capture device: %s\n", fn_cap); 255 ERROR(card, "No such PCM capture device: %s\n", fn_cap);
256 snd->substream = NULL; 256 snd->substream = NULL;
257 snd->card = NULL; 257 snd->card = NULL;
258 snd->filp = NULL;
258 } else { 259 } else {
259 pcm_file = snd->filp->private_data; 260 pcm_file = snd->filp->private_data;
260 snd->substream = pcm_file->substream; 261 snd->substream = pcm_file->substream;
@@ -273,17 +274,17 @@ static int gaudio_close_snd_dev(struct gaudio *gau)
273 274
274 /* Close control device */ 275 /* Close control device */
275 snd = &gau->control; 276 snd = &gau->control;
276 if (!IS_ERR(snd->filp)) 277 if (snd->filp)
277 filp_close(snd->filp, current->files); 278 filp_close(snd->filp, current->files);
278 279
279 /* Close PCM playback device and setup substream */ 280 /* Close PCM playback device and setup substream */
280 snd = &gau->playback; 281 snd = &gau->playback;
281 if (!IS_ERR(snd->filp)) 282 if (snd->filp)
282 filp_close(snd->filp, current->files); 283 filp_close(snd->filp, current->files);
283 284
284 /* Close PCM capture device and setup substream */ 285 /* Close PCM capture device and setup substream */
285 snd = &gau->capture; 286 snd = &gau->capture;
286 if (!IS_ERR(snd->filp)) 287 if (snd->filp)
287 filp_close(snd->filp, current->files); 288 filp_close(snd->filp, current->files);
288 289
289 return 0; 290 return 0;
@@ -304,8 +305,7 @@ int __init gaudio_setup(struct gaudio *card)
304 ret = gaudio_open_snd_dev(card); 305 ret = gaudio_open_snd_dev(card);
305 if (ret) 306 if (ret)
306 ERROR(card, "we need at least one control device\n"); 307 ERROR(card, "we need at least one control device\n");
307 308 else if (!the_card)
308 if (!the_card)
309 the_card = card; 309 the_card = card;
310 310
311 return ret; 311 return ret;
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index 6bb876d65252..2ac1d2147325 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -240,6 +240,9 @@ rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
240 size += out->maxpacket - 1; 240 size += out->maxpacket - 1;
241 size -= size % out->maxpacket; 241 size -= size % out->maxpacket;
242 242
243 if (dev->port_usb->is_fixed)
244 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
245
243 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); 246 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
244 if (skb == NULL) { 247 if (skb == NULL) {
245 DBG(dev, "no rx skb\n"); 248 DBG(dev, "no rx skb\n");
@@ -578,12 +581,19 @@ static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
578 req->context = skb; 581 req->context = skb;
579 req->complete = tx_complete; 582 req->complete = tx_complete;
580 583
584 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
585 if (dev->port_usb->is_fixed &&
586 length == dev->port_usb->fixed_in_len &&
587 (length % in->maxpacket) == 0)
588 req->zero = 0;
589 else
590 req->zero = 1;
591
581 /* use zlp framing on tx for strict CDC-Ether conformance, 592 /* use zlp framing on tx for strict CDC-Ether conformance,
582 * though any robust network rx path ignores extra padding. 593 * though any robust network rx path ignores extra padding.
583 * and some hardware doesn't like to write zlps. 594 * and some hardware doesn't like to write zlps.
584 */ 595 */
585 req->zero = 1; 596 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
586 if (!dev->zlp && (length % in->maxpacket) == 0)
587 length++; 597 length++;
588 598
589 req->length = length; 599 req->length = length;
@@ -797,7 +807,6 @@ int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
797 * - iff DATA transfer is active, carrier is "on" 807 * - iff DATA transfer is active, carrier is "on"
798 * - tx queueing enabled if open *and* carrier is "on" 808 * - tx queueing enabled if open *and* carrier is "on"
799 */ 809 */
800 netif_stop_queue(net);
801 netif_carrier_off(net); 810 netif_carrier_off(net);
802 811
803 dev->gadget = g; 812 dev->gadget = g;
@@ -830,11 +839,9 @@ void gether_cleanup(void)
830 return; 839 return;
831 840
832 unregister_netdev(the_dev->net); 841 unregister_netdev(the_dev->net);
842 flush_work_sync(&the_dev->work);
833 free_netdev(the_dev->net); 843 free_netdev(the_dev->net);
834 844
835 /* assuming we used keventd, it must quiesce too */
836 flush_scheduled_work();
837
838 the_dev = NULL; 845 the_dev = NULL;
839} 846}
840 847
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 3c8c0c9f9d72..b56e1e7d423c 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -62,6 +62,10 @@ struct gether {
62 62
63 /* hooks for added framing, as needed for RNDIS and EEM. */ 63 /* hooks for added framing, as needed for RNDIS and EEM. */
64 u32 header_len; 64 u32 header_len;
65 /* NCM requires fixed size bundles */
66 bool is_fixed;
67 u32 fixed_out_len;
68 u32 fixed_in_len;
65 struct sk_buff *(*wrap)(struct gether *port, 69 struct sk_buff *(*wrap)(struct gether *port,
66 struct sk_buff *skb); 70 struct sk_buff *skb);
67 int (*unwrap)(struct gether *port, 71 int (*unwrap)(struct gether *port,
@@ -103,6 +107,7 @@ static inline bool can_support_ecm(struct usb_gadget *gadget)
103/* each configuration may bind one instance of an ethernet link */ 107/* each configuration may bind one instance of an ethernet link */
104int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 108int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
105int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 109int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
110int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
106int eem_bind_config(struct usb_configuration *c); 111int eem_bind_config(struct usb_configuration *c);
107 112
108#ifdef USB_ETH_RNDIS 113#ifdef USB_ETH_RNDIS
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index 01e5354a4c20..40f7716b31fc 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -105,11 +105,15 @@ struct gs_port {
105 wait_queue_head_t close_wait; /* wait for last close */ 105 wait_queue_head_t close_wait; /* wait for last close */
106 106
107 struct list_head read_pool; 107 struct list_head read_pool;
108 int read_started;
109 int read_allocated;
108 struct list_head read_queue; 110 struct list_head read_queue;
109 unsigned n_read; 111 unsigned n_read;
110 struct tasklet_struct push; 112 struct tasklet_struct push;
111 113
112 struct list_head write_pool; 114 struct list_head write_pool;
115 int write_started;
116 int write_allocated;
113 struct gs_buf port_write_buf; 117 struct gs_buf port_write_buf;
114 wait_queue_head_t drain_wait; /* wait while writes drain */ 118 wait_queue_head_t drain_wait; /* wait while writes drain */
115 119
@@ -363,6 +367,9 @@ __acquires(&port->port_lock)
363 struct usb_request *req; 367 struct usb_request *req;
364 int len; 368 int len;
365 369
370 if (port->write_started >= QUEUE_SIZE)
371 break;
372
366 req = list_entry(pool->next, struct usb_request, list); 373 req = list_entry(pool->next, struct usb_request, list);
367 len = gs_send_packet(port, req->buf, in->maxpacket); 374 len = gs_send_packet(port, req->buf, in->maxpacket);
368 if (len == 0) { 375 if (len == 0) {
@@ -397,6 +404,8 @@ __acquires(&port->port_lock)
397 break; 404 break;
398 } 405 }
399 406
407 port->write_started++;
408
400 /* abort immediately after disconnect */ 409 /* abort immediately after disconnect */
401 if (!port->port_usb) 410 if (!port->port_usb)
402 break; 411 break;
@@ -418,7 +427,6 @@ __acquires(&port->port_lock)
418{ 427{
419 struct list_head *pool = &port->read_pool; 428 struct list_head *pool = &port->read_pool;
420 struct usb_ep *out = port->port_usb->out; 429 struct usb_ep *out = port->port_usb->out;
421 unsigned started = 0;
422 430
423 while (!list_empty(pool)) { 431 while (!list_empty(pool)) {
424 struct usb_request *req; 432 struct usb_request *req;
@@ -430,6 +438,9 @@ __acquires(&port->port_lock)
430 if (!tty) 438 if (!tty)
431 break; 439 break;
432 440
441 if (port->read_started >= QUEUE_SIZE)
442 break;
443
433 req = list_entry(pool->next, struct usb_request, list); 444 req = list_entry(pool->next, struct usb_request, list);
434 list_del(&req->list); 445 list_del(&req->list);
435 req->length = out->maxpacket; 446 req->length = out->maxpacket;
@@ -447,13 +458,13 @@ __acquires(&port->port_lock)
447 list_add(&req->list, pool); 458 list_add(&req->list, pool);
448 break; 459 break;
449 } 460 }
450 started++; 461 port->read_started++;
451 462
452 /* abort immediately after disconnect */ 463 /* abort immediately after disconnect */
453 if (!port->port_usb) 464 if (!port->port_usb)
454 break; 465 break;
455 } 466 }
456 return started; 467 return port->read_started;
457} 468}
458 469
459/* 470/*
@@ -535,6 +546,7 @@ static void gs_rx_push(unsigned long _port)
535 } 546 }
536recycle: 547recycle:
537 list_move(&req->list, &port->read_pool); 548 list_move(&req->list, &port->read_pool);
549 port->read_started--;
538 } 550 }
539 551
540 /* Push from tty to ldisc; without low_latency set this is handled by 552 /* Push from tty to ldisc; without low_latency set this is handled by
@@ -587,6 +599,7 @@ static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
587 599
588 spin_lock(&port->port_lock); 600 spin_lock(&port->port_lock);
589 list_add(&req->list, &port->write_pool); 601 list_add(&req->list, &port->write_pool);
602 port->write_started--;
590 603
591 switch (req->status) { 604 switch (req->status) {
592 default: 605 default:
@@ -608,7 +621,8 @@ static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
608 spin_unlock(&port->port_lock); 621 spin_unlock(&port->port_lock);
609} 622}
610 623
611static void gs_free_requests(struct usb_ep *ep, struct list_head *head) 624static void gs_free_requests(struct usb_ep *ep, struct list_head *head,
625 int *allocated)
612{ 626{
613 struct usb_request *req; 627 struct usb_request *req;
614 628
@@ -616,25 +630,31 @@ static void gs_free_requests(struct usb_ep *ep, struct list_head *head)
616 req = list_entry(head->next, struct usb_request, list); 630 req = list_entry(head->next, struct usb_request, list);
617 list_del(&req->list); 631 list_del(&req->list);
618 gs_free_req(ep, req); 632 gs_free_req(ep, req);
633 if (allocated)
634 (*allocated)--;
619 } 635 }
620} 636}
621 637
622static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head, 638static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
623 void (*fn)(struct usb_ep *, struct usb_request *)) 639 void (*fn)(struct usb_ep *, struct usb_request *),
640 int *allocated)
624{ 641{
625 int i; 642 int i;
626 struct usb_request *req; 643 struct usb_request *req;
644 int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE;
627 645
628 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't 646 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
629 * do quite that many this time, don't fail ... we just won't 647 * do quite that many this time, don't fail ... we just won't
630 * be as speedy as we might otherwise be. 648 * be as speedy as we might otherwise be.
631 */ 649 */
632 for (i = 0; i < QUEUE_SIZE; i++) { 650 for (i = 0; i < n; i++) {
633 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); 651 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
634 if (!req) 652 if (!req)
635 return list_empty(head) ? -ENOMEM : 0; 653 return list_empty(head) ? -ENOMEM : 0;
636 req->complete = fn; 654 req->complete = fn;
637 list_add_tail(&req->list, head); 655 list_add_tail(&req->list, head);
656 if (allocated)
657 (*allocated)++;
638 } 658 }
639 return 0; 659 return 0;
640} 660}
@@ -661,14 +681,15 @@ static int gs_start_io(struct gs_port *port)
661 * configurations may use different endpoints with a given port; 681 * configurations may use different endpoints with a given port;
662 * and high speed vs full speed changes packet sizes too. 682 * and high speed vs full speed changes packet sizes too.
663 */ 683 */
664 status = gs_alloc_requests(ep, head, gs_read_complete); 684 status = gs_alloc_requests(ep, head, gs_read_complete,
685 &port->read_allocated);
665 if (status) 686 if (status)
666 return status; 687 return status;
667 688
668 status = gs_alloc_requests(port->port_usb->in, &port->write_pool, 689 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
669 gs_write_complete); 690 gs_write_complete, &port->write_allocated);
670 if (status) { 691 if (status) {
671 gs_free_requests(ep, head); 692 gs_free_requests(ep, head, &port->read_allocated);
672 return status; 693 return status;
673 } 694 }
674 695
@@ -680,8 +701,9 @@ static int gs_start_io(struct gs_port *port)
680 if (started) { 701 if (started) {
681 tty_wakeup(port->port_tty); 702 tty_wakeup(port->port_tty);
682 } else { 703 } else {
683 gs_free_requests(ep, head); 704 gs_free_requests(ep, head, &port->read_allocated);
684 gs_free_requests(port->port_usb->in, &port->write_pool); 705 gs_free_requests(port->port_usb->in, &port->write_pool,
706 &port->write_allocated);
685 status = -EIO; 707 status = -EIO;
686 } 708 }
687 709
@@ -1315,8 +1337,12 @@ void gserial_disconnect(struct gserial *gser)
1315 spin_lock_irqsave(&port->port_lock, flags); 1337 spin_lock_irqsave(&port->port_lock, flags);
1316 if (port->open_count == 0 && !port->openclose) 1338 if (port->open_count == 0 && !port->openclose)
1317 gs_buf_free(&port->port_write_buf); 1339 gs_buf_free(&port->port_write_buf);
1318 gs_free_requests(gser->out, &port->read_pool); 1340 gs_free_requests(gser->out, &port->read_pool, NULL);
1319 gs_free_requests(gser->out, &port->read_queue); 1341 gs_free_requests(gser->out, &port->read_queue, NULL);
1320 gs_free_requests(gser->in, &port->write_pool); 1342 gs_free_requests(gser->in, &port->write_pool, NULL);
1343
1344 port->read_allocated = port->read_started =
1345 port->write_allocated = port->write_started = 0;
1346
1321 spin_unlock_irqrestore(&port->port_lock, flags); 1347 spin_unlock_irqrestore(&port->port_lock, flags);
1322} 1348}
diff --git a/drivers/usb/gadget/webcam.c b/drivers/usb/gadget/webcam.c
index de1deb7a3c63..a5a0fdb808c7 100644
--- a/drivers/usb/gadget/webcam.c
+++ b/drivers/usb/gadget/webcam.c
@@ -308,7 +308,7 @@ static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
308 * USB configuration 308 * USB configuration
309 */ 309 */
310 310
311static int __ref 311static int __init
312webcam_config_bind(struct usb_configuration *c) 312webcam_config_bind(struct usb_configuration *c)
313{ 313{
314 return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls, 314 return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls,
@@ -317,7 +317,6 @@ webcam_config_bind(struct usb_configuration *c)
317 317
318static struct usb_configuration webcam_config_driver = { 318static struct usb_configuration webcam_config_driver = {
319 .label = webcam_config_label, 319 .label = webcam_config_label,
320 .bind = webcam_config_bind,
321 .bConfigurationValue = 1, 320 .bConfigurationValue = 1,
322 .iConfiguration = 0, /* dynamic */ 321 .iConfiguration = 0, /* dynamic */
323 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 322 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
@@ -330,7 +329,7 @@ webcam_unbind(struct usb_composite_dev *cdev)
330 return 0; 329 return 0;
331} 330}
332 331
333static int __ref 332static int __init
334webcam_bind(struct usb_composite_dev *cdev) 333webcam_bind(struct usb_composite_dev *cdev)
335{ 334{
336 int ret; 335 int ret;
@@ -354,7 +353,8 @@ webcam_bind(struct usb_composite_dev *cdev)
354 webcam_config_driver.iConfiguration = ret; 353 webcam_config_driver.iConfiguration = ret;
355 354
356 /* Register our configuration. */ 355 /* Register our configuration. */
357 if ((ret = usb_add_config(cdev, &webcam_config_driver)) < 0) 356 if ((ret = usb_add_config(cdev, &webcam_config_driver,
357 webcam_config_bind)) < 0)
358 goto error; 358 goto error;
359 359
360 INFO(cdev, "Webcam Video Gadget\n"); 360 INFO(cdev, "Webcam Video Gadget\n");
@@ -373,14 +373,13 @@ static struct usb_composite_driver webcam_driver = {
373 .name = "g_webcam", 373 .name = "g_webcam",
374 .dev = &webcam_device_descriptor, 374 .dev = &webcam_device_descriptor,
375 .strings = webcam_device_strings, 375 .strings = webcam_device_strings,
376 .bind = webcam_bind,
377 .unbind = webcam_unbind, 376 .unbind = webcam_unbind,
378}; 377};
379 378
380static int __init 379static int __init
381webcam_init(void) 380webcam_init(void)
382{ 381{
383 return usb_composite_register(&webcam_driver); 382 return usb_composite_probe(&webcam_driver, webcam_bind);
384} 383}
385 384
386static void __exit 385static void __exit
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index cf353920bb1c..6d16db9d9d2d 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -264,7 +264,7 @@ static void zero_resume(struct usb_composite_dev *cdev)
264 264
265/*-------------------------------------------------------------------------*/ 265/*-------------------------------------------------------------------------*/
266 266
267static int __ref zero_bind(struct usb_composite_dev *cdev) 267static int __init zero_bind(struct usb_composite_dev *cdev)
268{ 268{
269 int gcnum; 269 int gcnum;
270 struct usb_gadget *gadget = cdev->gadget; 270 struct usb_gadget *gadget = cdev->gadget;
@@ -340,7 +340,6 @@ static struct usb_composite_driver zero_driver = {
340 .name = "zero", 340 .name = "zero",
341 .dev = &device_desc, 341 .dev = &device_desc,
342 .strings = dev_strings, 342 .strings = dev_strings,
343 .bind = zero_bind,
344 .unbind = zero_unbind, 343 .unbind = zero_unbind,
345 .suspend = zero_suspend, 344 .suspend = zero_suspend,
346 .resume = zero_resume, 345 .resume = zero_resume,
@@ -351,7 +350,7 @@ MODULE_LICENSE("GPL");
351 350
352static int __init init(void) 351static int __init init(void)
353{ 352{
354 return usb_composite_register(&zero_driver); 353 return usb_composite_probe(&zero_driver, zero_bind);
355} 354}
356module_init(init); 355module_init(init);
357 356