aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2010-12-16 13:05:06 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2010-12-16 13:05:06 -0500
commit36facadd9ea98f8415d0dbb63e0763b7ee9d3911 (patch)
tree99dea00b332ed852f2b0a4923b581dd723f03634 /drivers/usb/gadget
parent2faa83e2a519abea1055d156ce1b42b8fa57e87b (diff)
parent0b83ae960cd7d4a5ee02786ecf41ab45688999bf (diff)
Merge branch 'usb-next' into musb-merge
* usb-next: (132 commits) USB: uas: Use GFP_NOIO instead of GFP_KERNEL in I/O submission path USB: uas: Ensure we only bind to a UAS interface USB: uas: Rename sense pipe and sense urb to status pipe and status urb USB: uas: Use kzalloc instead of kmalloc USB: uas: Fix up the Sense IU usb: musb: core: kill unneeded #include's DA8xx: assign name to MUSB IRQ resource usb: gadget: g_ncm added usb: gadget: f_ncm.c added usb: gadget: u_ether: prepare for NCM usb: pch_udc: Fix setup transfers with data out usb: pch_udc: Fix compile error, warnings and checkpatch warnings usb: add ab8500 usb transceiver driver USB: gadget: Implement runtime PM for MSM bus glue driver USB: gadget: Implement runtime PM for ci13xxx gadget USB: gadget: Add USB controller driver for MSM SoC USB: gadget: Introduce ci13xxx_udc_driver struct USB: gadget: Initialize ci13xxx gadget device's coherent DMA mask USB: gadget: Fix "scheduling while atomic" bugs in ci13xxx_udc USB: gadget: Separate out PCI bus code from ci13xxx_udc ...
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig77
-rw-r--r--drivers/usb/gadget/Makefile8
-rw-r--r--drivers/usb/gadget/amd5536udc.c1
-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.c384
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.h19
-rw-r--r--drivers/usb/gadget/composite.c10
-rw-r--r--drivers/usb/gadget/dummy_hcd.c251
-rw-r--r--drivers/usb/gadget/f_fs.c437
-rw-r--r--drivers/usb/gadget/f_mass_storage.c524
-rw-r--r--drivers/usb/gadget/f_ncm.c1407
-rw-r--r--drivers/usb/gadget/file_storage.c29
-rw-r--r--drivers/usb/gadget/g_ffs.c41
-rw-r--r--drivers/usb/gadget/gadget_chips.h25
-rw-r--r--drivers/usb/gadget/imx_udc.c8
-rw-r--r--drivers/usb/gadget/imx_udc.h3
-rw-r--r--drivers/usb/gadget/langwell_udc.c23
-rw-r--r--drivers/usb/gadget/mass_storage.c2
-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/pch_udc.c2947
-rw-r--r--drivers/usb/gadget/u_audio.c10
-rw-r--r--drivers/usb/gadget/u_ether.c14
-rw-r--r--drivers/usb/gadget/u_ether.h5
27 files changed, 8580 insertions, 860 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 607d0db4a988..1dc9739277b4 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -338,6 +338,19 @@ config USB_S3C2410_DEBUG
338 boolean "S3C2410 udc debug messages" 338 boolean "S3C2410 udc debug messages"
339 depends on USB_GADGET_S3C2410 339 depends on USB_GADGET_S3C2410
340 340
341config USB_GADGET_PXA_U2O
342 boolean "PXA9xx Processor USB2.0 controller"
343 select USB_GADGET_DUALSPEED
344 help
345 PXA9xx Processor series include a high speed USB2.0 device
346 controller, which support high speed and full speed USB peripheral.
347
348config USB_PXA_U2O
349 tristate
350 depends on USB_GADGET_PXA_U2O
351 default USB_GADGET
352 select USB_GADGET_SELECTED
353
341# 354#
342# Controllers available in both integrated and discrete versions 355# Controllers available in both integrated and discrete versions
343# 356#
@@ -414,8 +427,8 @@ config USB_FSL_QE
414 default USB_GADGET 427 default USB_GADGET
415 select USB_GADGET_SELECTED 428 select USB_GADGET_SELECTED
416 429
417config USB_GADGET_CI13XXX 430config USB_GADGET_CI13XXX_PCI
418 boolean "MIPS USB CI13xxx" 431 boolean "MIPS USB CI13xxx PCI UDC"
419 depends on PCI 432 depends on PCI
420 select USB_GADGET_DUALSPEED 433 select USB_GADGET_DUALSPEED
421 help 434 help
@@ -426,9 +439,9 @@ config USB_GADGET_CI13XXX
426 dynamically linked module called "ci13xxx_udc" and force all 439 dynamically linked module called "ci13xxx_udc" and force all
427 gadget drivers to also be dynamically linked. 440 gadget drivers to also be dynamically linked.
428 441
429config USB_CI13XXX 442config USB_CI13XXX_PCI
430 tristate 443 tristate
431 depends on USB_GADGET_CI13XXX 444 depends on USB_GADGET_CI13XXX_PCI
432 default USB_GADGET 445 default USB_GADGET
433 select USB_GADGET_SELECTED 446 select USB_GADGET_SELECTED
434 447
@@ -495,6 +508,49 @@ config USB_LANGWELL
495 default USB_GADGET 508 default USB_GADGET
496 select USB_GADGET_SELECTED 509 select USB_GADGET_SELECTED
497 510
511config USB_GADGET_EG20T
512 boolean "Intel EG20T(Topcliff) USB Device controller"
513 depends on PCI
514 select USB_GADGET_DUALSPEED
515 help
516 This is a USB device driver for EG20T PCH.
517 EG20T PCH is the platform controller hub that is used in Intel's
518 general embedded platform. EG20T PCH has USB device interface.
519 Using this interface, it is able to access system devices connected
520 to USB device.
521 This driver enables USB device function.
522 USB device is a USB peripheral controller which
523 supports both full and high speed USB 2.0 data transfers.
524 This driver supports both control transfer and bulk transfer modes.
525 This driver dose not support interrupt transfer or isochronous
526 transfer modes.
527
528config USB_EG20T
529 tristate
530 depends on USB_GADGET_EG20T
531 default USB_GADGET
532 select USB_GADGET_SELECTED
533
534config USB_GADGET_CI13XXX_MSM
535 boolean "MIPS USB CI13xxx for MSM"
536 depends on ARCH_MSM
537 select USB_GADGET_DUALSPEED
538 select USB_MSM_OTG_72K
539 help
540 MSM SoC has chipidea USB controller. This driver uses
541 ci13xxx_udc core.
542 This driver depends on OTG driver for PHY initialization,
543 clock management, powering up VBUS, and power management.
544
545 Say "y" to link the driver statically, or "m" to build a
546 dynamically linked module called "ci13xxx_msm" and force all
547 gadget drivers to also be dynamically linked.
548
549config USB_CI13XXX_MSM
550 tristate
551 depends on USB_GADGET_CI13XXX_MSM
552 default USB_GADGET
553 select USB_GADGET_SELECTED
498 554
499# 555#
500# LAST -- dummy/emulated controller 556# LAST -- dummy/emulated controller
@@ -685,6 +741,19 @@ config USB_ETH_EEM
685 If you say "y" here, the Ethernet gadget driver will use the EEM 741 If you say "y" here, the Ethernet gadget driver will use the EEM
686 protocol rather than ECM. If unsure, say "n". 742 protocol rather than ECM. If unsure, say "n".
687 743
744config USB_G_NCM
745 tristate "Network Control Model (NCM) support"
746 depends on NET
747 select CRC32
748 help
749 This driver implements USB CDC NCM subclass standard. NCM is
750 an advanced protocol for Ethernet encapsulation, allows grouping
751 of several ethernet frames into one USB transfer and diffferent
752 alignment possibilities.
753
754 Say "y" to link the driver statically, or "m" to build a
755 dynamically linked module called "g_ncm".
756
688config USB_GADGETFS 757config USB_GADGETFS
689 tristate "Gadget Filesystem (EXPERIMENTAL)" 758 tristate "Gadget Filesystem (EXPERIMENTAL)"
690 depends on EXPERIMENTAL 759 depends on EXPERIMENTAL
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 5780db42417b..55f5e8ae5924 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -21,9 +21,13 @@ fsl_usb2_udc-$(CONFIG_ARCH_MXC) += fsl_mxc_udc.o
21obj-$(CONFIG_USB_M66592) += m66592-udc.o 21obj-$(CONFIG_USB_M66592) += m66592-udc.o
22obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o 22obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o
23obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o 23obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o
24obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o 24obj-$(CONFIG_USB_CI13XXX_PCI) += ci13xxx_pci.o
25obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o 25obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o
26obj-$(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
27 31
28# 32#
29# USB gadget drivers 33# USB gadget drivers
@@ -43,6 +47,7 @@ g_hid-y := hid.o
43g_dbgp-y := dbgp.o 47g_dbgp-y := dbgp.o
44g_nokia-y := nokia.o 48g_nokia-y := nokia.o
45g_webcam-y := webcam.o 49g_webcam-y := webcam.o
50g_ncm-y := ncm.o
46 51
47obj-$(CONFIG_USB_ZERO) += g_zero.o 52obj-$(CONFIG_USB_ZERO) += g_zero.o
48obj-$(CONFIG_USB_AUDIO) += g_audio.o 53obj-$(CONFIG_USB_AUDIO) += g_audio.o
@@ -60,3 +65,4 @@ obj-$(CONFIG_USB_G_DBGP) += g_dbgp.o
60obj-$(CONFIG_USB_G_MULTI) += g_multi.o 65obj-$(CONFIG_USB_G_MULTI) += g_multi.o
61obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o 66obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
62obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o 67obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
68obj-$(CONFIG_USB_G_NCM) += g_ncm.o
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index 9034e0344723..f8dd7269d79c 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -3359,7 +3359,6 @@ static int udc_probe(struct udc *dev)
3359 dev_set_name(&dev->gadget.dev, "gadget"); 3359 dev_set_name(&dev->gadget.dev, "gadget");
3360 dev->gadget.dev.release = gadget_release; 3360 dev->gadget.dev.release = gadget_release;
3361 dev->gadget.name = name; 3361 dev->gadget.name = name;
3362 dev->gadget.name = name;
3363 dev->gadget.is_dualspeed = 1; 3362 dev->gadget.is_dualspeed = 1;
3364 3363
3365 /* init registers, interrupts, ... */ 3364 /* init registers, interrupts, ... */
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 98b36fc88c77..f200e472e476 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,9 +74,6 @@
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_desc = {
@@ -132,6 +128,9 @@ static struct {
132 size_t size; /* bank size */ 128 size_t size; /* bank size */
133} hw_bank; 129} hw_bank;
134 130
131/* MSM specific */
132#define ABS_AHBBURST (0x0090UL)
133#define ABS_AHBMODE (0x0098UL)
135/* UDC register map */ 134/* UDC register map */
136#define ABS_CAPLENGTH (0x100UL) 135#define ABS_CAPLENGTH (0x100UL)
137#define ABS_HCCPARAMS (0x108UL) 136#define ABS_HCCPARAMS (0x108UL)
@@ -248,13 +247,7 @@ static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
248 return (reg & mask) >> ffs_nr(mask); 247 return (reg & mask) >> ffs_nr(mask);
249} 248}
250 249
251/** 250static 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{ 251{
259 u32 reg; 252 u32 reg;
260 253
@@ -271,6 +264,28 @@ static int hw_device_reset(void __iomem *base)
271 hw_bank.size += CAP_LAST; 264 hw_bank.size += CAP_LAST;
272 hw_bank.size /= sizeof(u32); 265 hw_bank.size /= sizeof(u32);
273 266
267 reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
268 if (reg == 0 || reg > ENDPT_MAX)
269 return -ENODEV;
270
271 hw_ep_max = reg; /* cache hw ENDPT_MAX */
272
273 /* setup lock mode ? */
274
275 /* ENDPTSETUPSTAT is '0' by default */
276
277 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
278
279 return 0;
280}
281/**
282 * hw_device_reset: resets chip (execute without interruption)
283 * @base: register base address
284 *
285 * This function returns an error code
286 */
287static int hw_device_reset(struct ci13xxx *udc)
288{
274 /* should flush & stop before reset */ 289 /* should flush & stop before reset */
275 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); 290 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
276 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0); 291 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
@@ -279,6 +294,14 @@ static int hw_device_reset(void __iomem *base)
279 while (hw_cread(CAP_USBCMD, USBCMD_RST)) 294 while (hw_cread(CAP_USBCMD, USBCMD_RST))
280 udelay(10); /* not RTOS friendly */ 295 udelay(10); /* not RTOS friendly */
281 296
297
298 if (udc->udc_driver->notify_event)
299 udc->udc_driver->notify_event(udc,
300 CI13XXX_CONTROLLER_RESET_EVENT);
301
302 if (udc->udc_driver->flags && CI13XXX_DISABLE_STREAMING)
303 hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
304
282 /* USBMODE should be configured step by step */ 305 /* USBMODE should be configured step by step */
283 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); 306 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
284 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE); 307 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
@@ -290,18 +313,6 @@ static int hw_device_reset(void __iomem *base)
290 return -ENODEV; 313 return -ENODEV;
291 } 314 }
292 315
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; 316 return 0;
306} 317}
307 318
@@ -1557,8 +1568,6 @@ __acquires(mEp->lock)
1557 * Caller must hold lock 1568 * Caller must hold lock
1558 */ 1569 */
1559static int _gadget_stop_activity(struct usb_gadget *gadget) 1570static int _gadget_stop_activity(struct usb_gadget *gadget)
1560__releases(udc->lock)
1561__acquires(udc->lock)
1562{ 1571{
1563 struct usb_ep *ep; 1572 struct usb_ep *ep;
1564 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget); 1573 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
@@ -1570,8 +1579,6 @@ __acquires(udc->lock)
1570 if (gadget == NULL) 1579 if (gadget == NULL)
1571 return -EINVAL; 1580 return -EINVAL;
1572 1581
1573 spin_unlock(udc->lock);
1574
1575 /* flush all endpoints */ 1582 /* flush all endpoints */
1576 gadget_for_each_ep(ep, gadget) { 1583 gadget_for_each_ep(ep, gadget) {
1577 usb_ep_fifo_flush(ep); 1584 usb_ep_fifo_flush(ep);
@@ -1591,8 +1598,6 @@ __acquires(udc->lock)
1591 mEp->status = NULL; 1598 mEp->status = NULL;
1592 } 1599 }
1593 1600
1594 spin_lock(udc->lock);
1595
1596 return 0; 1601 return 0;
1597} 1602}
1598 1603
@@ -1621,6 +1626,7 @@ __acquires(udc->lock)
1621 1626
1622 dbg_event(0xFF, "BUS RST", 0); 1627 dbg_event(0xFF, "BUS RST", 0);
1623 1628
1629 spin_unlock(udc->lock);
1624 retval = _gadget_stop_activity(&udc->gadget); 1630 retval = _gadget_stop_activity(&udc->gadget);
1625 if (retval) 1631 if (retval)
1626 goto done; 1632 goto done;
@@ -1629,10 +1635,9 @@ __acquires(udc->lock)
1629 if (retval) 1635 if (retval)
1630 goto done; 1636 goto done;
1631 1637
1632 spin_unlock(udc->lock);
1633 retval = usb_ep_enable(&mEp->ep, &ctrl_endpt_desc); 1638 retval = usb_ep_enable(&mEp->ep, &ctrl_endpt_desc);
1634 if (!retval) { 1639 if (!retval) {
1635 mEp->status = usb_ep_alloc_request(&mEp->ep, GFP_KERNEL); 1640 mEp->status = usb_ep_alloc_request(&mEp->ep, GFP_ATOMIC);
1636 if (mEp->status == NULL) { 1641 if (mEp->status == NULL) {
1637 usb_ep_disable(&mEp->ep); 1642 usb_ep_disable(&mEp->ep);
1638 retval = -ENOMEM; 1643 retval = -ENOMEM;
@@ -2061,7 +2066,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2061{ 2066{
2062 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); 2067 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2063 struct ci13xxx_req *mReq = NULL; 2068 struct ci13xxx_req *mReq = NULL;
2064 unsigned long flags;
2065 2069
2066 trace("%p, %i", ep, gfp_flags); 2070 trace("%p, %i", ep, gfp_flags);
2067 2071
@@ -2070,8 +2074,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2070 return NULL; 2074 return NULL;
2071 } 2075 }
2072 2076
2073 spin_lock_irqsave(mEp->lock, flags);
2074
2075 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); 2077 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2076 if (mReq != NULL) { 2078 if (mReq != NULL) {
2077 INIT_LIST_HEAD(&mReq->queue); 2079 INIT_LIST_HEAD(&mReq->queue);
@@ -2086,8 +2088,6 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2086 2088
2087 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL); 2089 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2088 2090
2089 spin_unlock_irqrestore(mEp->lock, flags);
2090
2091 return (mReq == NULL) ? NULL : &mReq->req; 2091 return (mReq == NULL) ? NULL : &mReq->req;
2092} 2092}
2093 2093
@@ -2332,12 +2332,47 @@ static const struct usb_ep_ops usb_ep_ops = {
2332/****************************************************************************** 2332/******************************************************************************
2333 * GADGET block 2333 * GADGET block
2334 *****************************************************************************/ 2334 *****************************************************************************/
2335static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
2336{
2337 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2338 unsigned long flags;
2339 int gadget_ready = 0;
2340
2341 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
2342 return -EOPNOTSUPP;
2343
2344 spin_lock_irqsave(udc->lock, flags);
2345 udc->vbus_active = is_active;
2346 if (udc->driver)
2347 gadget_ready = 1;
2348 spin_unlock_irqrestore(udc->lock, flags);
2349
2350 if (gadget_ready) {
2351 if (is_active) {
2352 pm_runtime_get_sync(&_gadget->dev);
2353 hw_device_reset(udc);
2354 hw_device_state(udc->ci13xxx_ep[0].qh[RX].dma);
2355 } else {
2356 hw_device_state(0);
2357 if (udc->udc_driver->notify_event)
2358 udc->udc_driver->notify_event(udc,
2359 CI13XXX_CONTROLLER_STOPPED_EVENT);
2360 _gadget_stop_activity(&udc->gadget);
2361 pm_runtime_put_sync(&_gadget->dev);
2362 }
2363 }
2364
2365 return 0;
2366}
2367
2335/** 2368/**
2336 * Device operations part of the API to the USB controller hardware, 2369 * Device operations part of the API to the USB controller hardware,
2337 * which don't involve endpoints (or i/o) 2370 * which don't involve endpoints (or i/o)
2338 * Check "usb_gadget.h" for details 2371 * Check "usb_gadget.h" for details
2339 */ 2372 */
2340static const struct usb_gadget_ops usb_gadget_ops; 2373static const struct usb_gadget_ops usb_gadget_ops = {
2374 .vbus_session = ci13xxx_vbus_session,
2375};
2341 2376
2342/** 2377/**
2343 * usb_gadget_probe_driver: register a gadget driver 2378 * usb_gadget_probe_driver: register a gadget driver
@@ -2390,7 +2425,6 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2390 info("hw_ep_max = %d", hw_ep_max); 2425 info("hw_ep_max = %d", hw_ep_max);
2391 2426
2392 udc->driver = driver; 2427 udc->driver = driver;
2393 udc->gadget.ops = NULL;
2394 udc->gadget.dev.driver = NULL; 2428 udc->gadget.dev.driver = NULL;
2395 2429
2396 retval = 0; 2430 retval = 0;
@@ -2410,9 +2444,11 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2410 /* this allocation cannot be random */ 2444 /* this allocation cannot be random */
2411 for (k = RX; k <= TX; k++) { 2445 for (k = RX; k <= TX; k++) {
2412 INIT_LIST_HEAD(&mEp->qh[k].queue); 2446 INIT_LIST_HEAD(&mEp->qh[k].queue);
2447 spin_unlock_irqrestore(udc->lock, flags);
2413 mEp->qh[k].ptr = dma_pool_alloc(udc->qh_pool, 2448 mEp->qh[k].ptr = dma_pool_alloc(udc->qh_pool,
2414 GFP_KERNEL, 2449 GFP_KERNEL,
2415 &mEp->qh[k].dma); 2450 &mEp->qh[k].dma);
2451 spin_lock_irqsave(udc->lock, flags);
2416 if (mEp->qh[k].ptr == NULL) 2452 if (mEp->qh[k].ptr == NULL)
2417 retval = -ENOMEM; 2453 retval = -ENOMEM;
2418 else 2454 else
@@ -2429,7 +2465,6 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2429 2465
2430 /* bind gadget */ 2466 /* bind gadget */
2431 driver->driver.bus = NULL; 2467 driver->driver.bus = NULL;
2432 udc->gadget.ops = &usb_gadget_ops;
2433 udc->gadget.dev.driver = &driver->driver; 2468 udc->gadget.dev.driver = &driver->driver;
2434 2469
2435 spin_unlock_irqrestore(udc->lock, flags); 2470 spin_unlock_irqrestore(udc->lock, flags);
@@ -2437,12 +2472,24 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2437 spin_lock_irqsave(udc->lock, flags); 2472 spin_lock_irqsave(udc->lock, flags);
2438 2473
2439 if (retval) { 2474 if (retval) {
2440 udc->gadget.ops = NULL;
2441 udc->gadget.dev.driver = NULL; 2475 udc->gadget.dev.driver = NULL;
2442 goto done; 2476 goto done;
2443 } 2477 }
2444 2478
2479 pm_runtime_get_sync(&udc->gadget.dev);
2480 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
2481 if (udc->vbus_active) {
2482 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
2483 hw_device_reset(udc);
2484 } else {
2485 pm_runtime_put_sync(&udc->gadget.dev);
2486 goto done;
2487 }
2488 }
2489
2445 retval = hw_device_state(udc->ci13xxx_ep[0].qh[RX].dma); 2490 retval = hw_device_state(udc->ci13xxx_ep[0].qh[RX].dma);
2491 if (retval)
2492 pm_runtime_put_sync(&udc->gadget.dev);
2446 2493
2447 done: 2494 done:
2448 spin_unlock_irqrestore(udc->lock, flags); 2495 spin_unlock_irqrestore(udc->lock, flags);
@@ -2475,19 +2522,22 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2475 2522
2476 spin_lock_irqsave(udc->lock, flags); 2523 spin_lock_irqsave(udc->lock, flags);
2477 2524
2478 hw_device_state(0); 2525 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
2479 2526 udc->vbus_active) {
2480 /* unbind gadget */ 2527 hw_device_state(0);
2481 if (udc->gadget.ops != NULL) { 2528 if (udc->udc_driver->notify_event)
2529 udc->udc_driver->notify_event(udc,
2530 CI13XXX_CONTROLLER_STOPPED_EVENT);
2482 _gadget_stop_activity(&udc->gadget); 2531 _gadget_stop_activity(&udc->gadget);
2532 pm_runtime_put(&udc->gadget.dev);
2533 }
2483 2534
2484 spin_unlock_irqrestore(udc->lock, flags); 2535 /* unbind gadget */
2485 driver->unbind(&udc->gadget); /* MAY SLEEP */ 2536 spin_unlock_irqrestore(udc->lock, flags);
2486 spin_lock_irqsave(udc->lock, flags); 2537 driver->unbind(&udc->gadget); /* MAY SLEEP */
2538 spin_lock_irqsave(udc->lock, flags);
2487 2539
2488 udc->gadget.ops = NULL; 2540 udc->gadget.dev.driver = NULL;
2489 udc->gadget.dev.driver = NULL;
2490 }
2491 2541
2492 /* free resources */ 2542 /* free resources */
2493 for (i = 0; i < hw_ep_max; i++) { 2543 for (i = 0; i < hw_ep_max; i++) {
@@ -2544,6 +2594,14 @@ static irqreturn_t udc_irq(void)
2544 } 2594 }
2545 2595
2546 spin_lock(udc->lock); 2596 spin_lock(udc->lock);
2597
2598 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2599 if (hw_cread(CAP_USBMODE, USBMODE_CM) !=
2600 USBMODE_CM_DEVICE) {
2601 spin_unlock(udc->lock);
2602 return IRQ_NONE;
2603 }
2604 }
2547 intr = hw_test_and_clear_intr_active(); 2605 intr = hw_test_and_clear_intr_active();
2548 if (intr) { 2606 if (intr) {
2549 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr; 2607 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
@@ -2602,14 +2660,16 @@ static void udc_release(struct device *dev)
2602 * No interrupts active, the IRQ has not been requested yet 2660 * No interrupts active, the IRQ has not been requested yet
2603 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask 2661 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2604 */ 2662 */
2605static int udc_probe(struct device *dev, void __iomem *regs, const char *name) 2663static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2664 void __iomem *regs)
2606{ 2665{
2607 struct ci13xxx *udc; 2666 struct ci13xxx *udc;
2608 int retval = 0; 2667 int retval = 0;
2609 2668
2610 trace("%p, %p, %p", dev, regs, name); 2669 trace("%p, %p, %p", dev, regs, name);
2611 2670
2612 if (dev == NULL || regs == NULL || name == NULL) 2671 if (dev == NULL || regs == NULL || driver == NULL ||
2672 driver->name == NULL)
2613 return -EINVAL; 2673 return -EINVAL;
2614 2674
2615 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL); 2675 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
@@ -2617,42 +2677,77 @@ static int udc_probe(struct device *dev, void __iomem *regs, const char *name)
2617 return -ENOMEM; 2677 return -ENOMEM;
2618 2678
2619 udc->lock = &udc_lock; 2679 udc->lock = &udc_lock;
2680 udc->regs = regs;
2681 udc->udc_driver = driver;
2620 2682
2621 retval = hw_device_reset(regs); 2683 udc->gadget.ops = &usb_gadget_ops;
2622 if (retval)
2623 goto done;
2624
2625 udc->gadget.ops = NULL;
2626 udc->gadget.speed = USB_SPEED_UNKNOWN; 2684 udc->gadget.speed = USB_SPEED_UNKNOWN;
2627 udc->gadget.is_dualspeed = 1; 2685 udc->gadget.is_dualspeed = 1;
2628 udc->gadget.is_otg = 0; 2686 udc->gadget.is_otg = 0;
2629 udc->gadget.name = name; 2687 udc->gadget.name = driver->name;
2630 2688
2631 INIT_LIST_HEAD(&udc->gadget.ep_list); 2689 INIT_LIST_HEAD(&udc->gadget.ep_list);
2632 udc->gadget.ep0 = NULL; 2690 udc->gadget.ep0 = NULL;
2633 2691
2634 dev_set_name(&udc->gadget.dev, "gadget"); 2692 dev_set_name(&udc->gadget.dev, "gadget");
2635 udc->gadget.dev.dma_mask = dev->dma_mask; 2693 udc->gadget.dev.dma_mask = dev->dma_mask;
2694 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
2636 udc->gadget.dev.parent = dev; 2695 udc->gadget.dev.parent = dev;
2637 udc->gadget.dev.release = udc_release; 2696 udc->gadget.dev.release = udc_release;
2638 2697
2698 retval = hw_device_init(regs);
2699 if (retval < 0)
2700 goto free_udc;
2701
2702 udc->transceiver = otg_get_transceiver();
2703
2704 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
2705 if (udc->transceiver == NULL) {
2706 retval = -ENODEV;
2707 goto free_udc;
2708 }
2709 }
2710
2711 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
2712 retval = hw_device_reset(udc);
2713 if (retval)
2714 goto put_transceiver;
2715 }
2716
2639 retval = device_register(&udc->gadget.dev); 2717 retval = device_register(&udc->gadget.dev);
2640 if (retval) 2718 if (retval) {
2641 goto done; 2719 put_device(&udc->gadget.dev);
2720 goto put_transceiver;
2721 }
2642 2722
2643#ifdef CONFIG_USB_GADGET_DEBUG_FILES 2723#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2644 retval = dbg_create_files(&udc->gadget.dev); 2724 retval = dbg_create_files(&udc->gadget.dev);
2645#endif 2725#endif
2646 if (retval) { 2726 if (retval)
2647 device_unregister(&udc->gadget.dev); 2727 goto unreg_device;
2648 goto done; 2728
2729 if (udc->transceiver) {
2730 retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
2731 if (retval)
2732 goto remove_dbg;
2649 } 2733 }
2734 pm_runtime_no_callbacks(&udc->gadget.dev);
2735 pm_runtime_enable(&udc->gadget.dev);
2650 2736
2651 _udc = udc; 2737 _udc = udc;
2652 return retval; 2738 return retval;
2653 2739
2654 done:
2655 err("error = %i", retval); 2740 err("error = %i", retval);
2741remove_dbg:
2742#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2743 dbg_remove_files(&udc->gadget.dev);
2744#endif
2745unreg_device:
2746 device_unregister(&udc->gadget.dev);
2747put_transceiver:
2748 if (udc->transceiver)
2749 otg_put_transceiver(udc->transceiver);
2750free_udc:
2656 kfree(udc); 2751 kfree(udc);
2657 _udc = NULL; 2752 _udc = NULL;
2658 return retval; 2753 return retval;
@@ -2672,6 +2767,10 @@ static void udc_remove(void)
2672 return; 2767 return;
2673 } 2768 }
2674 2769
2770 if (udc->transceiver) {
2771 otg_set_peripheral(udc->transceiver, &udc->gadget);
2772 otg_put_transceiver(udc->transceiver);
2773 }
2675#ifdef CONFIG_USB_GADGET_DEBUG_FILES 2774#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2676 dbg_remove_files(&udc->gadget.dev); 2775 dbg_remove_files(&udc->gadget.dev);
2677#endif 2776#endif
@@ -2680,156 +2779,3 @@ static void udc_remove(void)
2680 kfree(udc); 2779 kfree(udc);
2681 _udc = NULL; 2780 _udc = NULL;
2682} 2781}
2683
2684/******************************************************************************
2685 * PCI block
2686 *****************************************************************************/
2687/**
2688 * ci13xxx_pci_irq: interrut handler
2689 * @irq: irq number
2690 * @pdev: USB Device Controller interrupt source
2691 *
2692 * This function returns IRQ_HANDLED if the IRQ has been handled
2693 * This is an ISR don't trace, use attribute interface instead
2694 */
2695static irqreturn_t ci13xxx_pci_irq(int irq, void *pdev)
2696{
2697 if (irq == 0) {
2698 dev_err(&((struct pci_dev *)pdev)->dev, "Invalid IRQ0 usage!");
2699 return IRQ_HANDLED;
2700 }
2701 return udc_irq();
2702}
2703
2704/**
2705 * ci13xxx_pci_probe: PCI probe
2706 * @pdev: USB device controller being probed
2707 * @id: PCI hotplug ID connecting controller to UDC framework
2708 *
2709 * This function returns an error code
2710 * Allocates basic PCI resources for this USB device controller, and then
2711 * invokes the udc_probe() method to start the UDC associated with it
2712 */
2713static int __devinit ci13xxx_pci_probe(struct pci_dev *pdev,
2714 const struct pci_device_id *id)
2715{
2716 void __iomem *regs = NULL;
2717 int retval = 0;
2718
2719 if (id == NULL)
2720 return -EINVAL;
2721
2722 retval = pci_enable_device(pdev);
2723 if (retval)
2724 goto done;
2725
2726 if (!pdev->irq) {
2727 dev_err(&pdev->dev, "No IRQ, check BIOS/PCI setup!");
2728 retval = -ENODEV;
2729 goto disable_device;
2730 }
2731
2732 retval = pci_request_regions(pdev, UDC_DRIVER_NAME);
2733 if (retval)
2734 goto disable_device;
2735
2736 /* BAR 0 holds all the registers */
2737 regs = pci_iomap(pdev, 0, 0);
2738 if (!regs) {
2739 dev_err(&pdev->dev, "Error mapping memory!");
2740 retval = -EFAULT;
2741 goto release_regions;
2742 }
2743 pci_set_drvdata(pdev, (__force void *)regs);
2744
2745 pci_set_master(pdev);
2746 pci_try_set_mwi(pdev);
2747
2748 retval = udc_probe(&pdev->dev, regs, UDC_DRIVER_NAME);
2749 if (retval)
2750 goto iounmap;
2751
2752 /* our device does not have MSI capability */
2753
2754 retval = request_irq(pdev->irq, ci13xxx_pci_irq, IRQF_SHARED,
2755 UDC_DRIVER_NAME, pdev);
2756 if (retval)
2757 goto gadget_remove;
2758
2759 return 0;
2760
2761 gadget_remove:
2762 udc_remove();
2763 iounmap:
2764 pci_iounmap(pdev, regs);
2765 release_regions:
2766 pci_release_regions(pdev);
2767 disable_device:
2768 pci_disable_device(pdev);
2769 done:
2770 return retval;
2771}
2772
2773/**
2774 * ci13xxx_pci_remove: PCI remove
2775 * @pdev: USB Device Controller being removed
2776 *
2777 * Reverses the effect of ci13xxx_pci_probe(),
2778 * first invoking the udc_remove() and then releases
2779 * all PCI resources allocated for this USB device controller
2780 */
2781static void __devexit ci13xxx_pci_remove(struct pci_dev *pdev)
2782{
2783 free_irq(pdev->irq, pdev);
2784 udc_remove();
2785 pci_iounmap(pdev, (__force void __iomem *)pci_get_drvdata(pdev));
2786 pci_release_regions(pdev);
2787 pci_disable_device(pdev);
2788}
2789
2790/**
2791 * PCI device table
2792 * PCI device structure
2793 *
2794 * Check "pci.h" for details
2795 */
2796static DEFINE_PCI_DEVICE_TABLE(ci13xxx_pci_id_table) = {
2797 { PCI_DEVICE(0x153F, 0x1004) },
2798 { PCI_DEVICE(0x153F, 0x1006) },
2799 { 0, 0, 0, 0, 0, 0, 0 /* end: all zeroes */ }
2800};
2801MODULE_DEVICE_TABLE(pci, ci13xxx_pci_id_table);
2802
2803static struct pci_driver ci13xxx_pci_driver = {
2804 .name = UDC_DRIVER_NAME,
2805 .id_table = ci13xxx_pci_id_table,
2806 .probe = ci13xxx_pci_probe,
2807 .remove = __devexit_p(ci13xxx_pci_remove),
2808};
2809
2810/**
2811 * ci13xxx_pci_init: module init
2812 *
2813 * Driver load
2814 */
2815static int __init ci13xxx_pci_init(void)
2816{
2817 return pci_register_driver(&ci13xxx_pci_driver);
2818}
2819module_init(ci13xxx_pci_init);
2820
2821/**
2822 * ci13xxx_pci_exit: module exit
2823 *
2824 * Driver unload
2825 */
2826static void __exit ci13xxx_pci_exit(void)
2827{
2828 pci_unregister_driver(&ci13xxx_pci_driver);
2829}
2830module_exit(ci13xxx_pci_exit);
2831
2832MODULE_AUTHOR("MIPS - David Lopo <dlopo@chipidea.mips.com>");
2833MODULE_DESCRIPTION("MIPS CI13XXX USB Peripheral Controller");
2834MODULE_LICENSE("GPL");
2835MODULE_VERSION("June 2008");
diff --git a/drivers/usb/gadget/ci13xxx_udc.h b/drivers/usb/gadget/ci13xxx_udc.h
index 4026e9cede34..4fd19313e238 100644
--- a/drivers/usb/gadget/ci13xxx_udc.h
+++ b/drivers/usb/gadget/ci13xxx_udc.h
@@ -97,9 +97,24 @@ struct ci13xxx_ep {
97 struct dma_pool *td_pool; 97 struct dma_pool *td_pool;
98}; 98};
99 99
100struct ci13xxx;
101struct ci13xxx_udc_driver {
102 const char *name;
103 unsigned long flags;
104#define CI13XXX_REGS_SHARED BIT(0)
105#define CI13XXX_REQUIRE_TRANSCEIVER BIT(1)
106#define CI13XXX_PULLUP_ON_VBUS BIT(2)
107#define CI13XXX_DISABLE_STREAMING BIT(3)
108
109#define CI13XXX_CONTROLLER_RESET_EVENT 0
110#define CI13XXX_CONTROLLER_STOPPED_EVENT 1
111 void (*notify_event) (struct ci13xxx *udc, unsigned event);
112};
113
100/* CI13XXX UDC descriptor & global resources */ 114/* CI13XXX UDC descriptor & global resources */
101struct ci13xxx { 115struct ci13xxx {
102 spinlock_t *lock; /* ctrl register bank access */ 116 spinlock_t *lock; /* ctrl register bank access */
117 void __iomem *regs; /* registers address space */
103 118
104 struct dma_pool *qh_pool; /* DMA pool for queue heads */ 119 struct dma_pool *qh_pool; /* DMA pool for queue heads */
105 struct dma_pool *td_pool; /* DMA pool for transfer descs */ 120 struct dma_pool *td_pool; /* DMA pool for transfer descs */
@@ -108,6 +123,9 @@ struct ci13xxx {
108 struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX]; /* extended endpts */ 123 struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX]; /* extended endpts */
109 124
110 struct usb_gadget_driver *driver; /* 3rd party gadget driver */ 125 struct usb_gadget_driver *driver; /* 3rd party gadget driver */
126 struct ci13xxx_udc_driver *udc_driver; /* device controller driver */
127 int vbus_active; /* is VBUS active */
128 struct otg_transceiver *transceiver; /* Transceiver struct */
111}; 129};
112 130
113/****************************************************************************** 131/******************************************************************************
@@ -157,6 +175,7 @@ struct ci13xxx {
157#define USBMODE_CM_DEVICE (0x02UL << 0) 175#define USBMODE_CM_DEVICE (0x02UL << 0)
158#define USBMODE_CM_HOST (0x03UL << 0) 176#define USBMODE_CM_HOST (0x03UL << 0)
159#define USBMODE_SLOM BIT(3) 177#define USBMODE_SLOM BIT(3)
178#define USBMODE_SDIS BIT(4)
160 179
161/* ENDPTCTRL */ 180/* ENDPTCTRL */
162#define ENDPTCTRL_RXS BIT(0) 181#define ENDPTCTRL_RXS BIT(0)
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 7b5cc16e4a0b..21dc0da36ab7 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1126,7 +1126,7 @@ static int composite_bind(struct usb_gadget *gadget)
1126 cdev->desc = *composite->dev; 1126 cdev->desc = *composite->dev;
1127 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1127 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1128 1128
1129 /* stirng overrides */ 1129 /* string overrides */
1130 if (iManufacturer || !cdev->desc.iManufacturer) { 1130 if (iManufacturer || !cdev->desc.iManufacturer) {
1131 if (!iManufacturer && !composite->iManufacturer && 1131 if (!iManufacturer && !composite->iManufacturer &&
1132 !*composite_manufacturer) 1132 !*composite_manufacturer)
@@ -1188,6 +1188,8 @@ composite_suspend(struct usb_gadget *gadget)
1188 composite->suspend(cdev); 1188 composite->suspend(cdev);
1189 1189
1190 cdev->suspended = 1; 1190 cdev->suspended = 1;
1191
1192 usb_gadget_vbus_draw(gadget, 2);
1191} 1193}
1192 1194
1193static void 1195static void
@@ -1195,6 +1197,7 @@ composite_resume(struct usb_gadget *gadget)
1195{ 1197{
1196 struct usb_composite_dev *cdev = get_gadget_data(gadget); 1198 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1197 struct usb_function *f; 1199 struct usb_function *f;
1200 u8 maxpower;
1198 1201
1199 /* REVISIT: should we have config level 1202 /* REVISIT: should we have config level
1200 * suspend/resume callbacks? 1203 * suspend/resume callbacks?
@@ -1207,6 +1210,11 @@ composite_resume(struct usb_gadget *gadget)
1207 if (f->resume) 1210 if (f->resume)
1208 f->resume(f); 1211 f->resume(f);
1209 } 1212 }
1213
1214 maxpower = cdev->config->bMaxPower;
1215
1216 usb_gadget_vbus_draw(gadget, maxpower ?
1217 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1210 } 1218 }
1211 1219
1212 cdev->suspended = 0; 1220 cdev->suspended = 0;
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 1d2a2abbfa80..13b9f47feecd 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -1197,6 +1197,139 @@ static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1197#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) 1197#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1198#define Ep_InRequest (Ep_Request | USB_DIR_IN) 1198#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1199 1199
1200
1201/**
1202 * handle_control_request() - handles all control transfers
1203 * @dum: pointer to dummy (the_controller)
1204 * @urb: the urb request to handle
1205 * @setup: pointer to the setup data for a USB device control
1206 * request
1207 * @status: pointer to request handling status
1208 *
1209 * Return 0 - if the request was handled
1210 * 1 - if the request wasn't handles
1211 * error code on error
1212 */
1213static int handle_control_request(struct dummy *dum, struct urb *urb,
1214 struct usb_ctrlrequest *setup,
1215 int *status)
1216{
1217 struct dummy_ep *ep2;
1218 int ret_val = 1;
1219 unsigned w_index;
1220 unsigned w_value;
1221
1222 w_index = le16_to_cpu(setup->wIndex);
1223 w_value = le16_to_cpu(setup->wValue);
1224 switch (setup->bRequest) {
1225 case USB_REQ_SET_ADDRESS:
1226 if (setup->bRequestType != Dev_Request)
1227 break;
1228 dum->address = w_value;
1229 *status = 0;
1230 dev_dbg(udc_dev(dum), "set_address = %d\n",
1231 w_value);
1232 ret_val = 0;
1233 break;
1234 case USB_REQ_SET_FEATURE:
1235 if (setup->bRequestType == Dev_Request) {
1236 ret_val = 0;
1237 switch (w_value) {
1238 case USB_DEVICE_REMOTE_WAKEUP:
1239 break;
1240 case USB_DEVICE_B_HNP_ENABLE:
1241 dum->gadget.b_hnp_enable = 1;
1242 break;
1243 case USB_DEVICE_A_HNP_SUPPORT:
1244 dum->gadget.a_hnp_support = 1;
1245 break;
1246 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1247 dum->gadget.a_alt_hnp_support = 1;
1248 break;
1249 default:
1250 ret_val = -EOPNOTSUPP;
1251 }
1252 if (ret_val == 0) {
1253 dum->devstatus |= (1 << w_value);
1254 *status = 0;
1255 }
1256 } else if (setup->bRequestType == Ep_Request) {
1257 /* endpoint halt */
1258 ep2 = find_endpoint(dum, w_index);
1259 if (!ep2 || ep2->ep.name == ep0name) {
1260 ret_val = -EOPNOTSUPP;
1261 break;
1262 }
1263 ep2->halted = 1;
1264 ret_val = 0;
1265 *status = 0;
1266 }
1267 break;
1268 case USB_REQ_CLEAR_FEATURE:
1269 if (setup->bRequestType == Dev_Request) {
1270 ret_val = 0;
1271 switch (w_value) {
1272 case USB_DEVICE_REMOTE_WAKEUP:
1273 w_value = USB_DEVICE_REMOTE_WAKEUP;
1274 break;
1275 default:
1276 ret_val = -EOPNOTSUPP;
1277 break;
1278 }
1279 if (ret_val == 0) {
1280 dum->devstatus &= ~(1 << w_value);
1281 *status = 0;
1282 }
1283 } else if (setup->bRequestType == Ep_Request) {
1284 /* endpoint halt */
1285 ep2 = find_endpoint(dum, w_index);
1286 if (!ep2) {
1287 ret_val = -EOPNOTSUPP;
1288 break;
1289 }
1290 if (!ep2->wedged)
1291 ep2->halted = 0;
1292 ret_val = 0;
1293 *status = 0;
1294 }
1295 break;
1296 case USB_REQ_GET_STATUS:
1297 if (setup->bRequestType == Dev_InRequest
1298 || setup->bRequestType == Intf_InRequest
1299 || setup->bRequestType == Ep_InRequest) {
1300 char *buf;
1301 /*
1302 * device: remote wakeup, selfpowered
1303 * interface: nothing
1304 * endpoint: halt
1305 */
1306 buf = (char *)urb->transfer_buffer;
1307 if (urb->transfer_buffer_length > 0) {
1308 if (setup->bRequestType == Ep_InRequest) {
1309 ep2 = find_endpoint(dum, w_index);
1310 if (!ep2) {
1311 ret_val = -EOPNOTSUPP;
1312 break;
1313 }
1314 buf[0] = ep2->halted;
1315 } else if (setup->bRequestType ==
1316 Dev_InRequest) {
1317 buf[0] = (u8)dum->devstatus;
1318 } else
1319 buf[0] = 0;
1320 }
1321 if (urb->transfer_buffer_length > 1)
1322 buf[1] = 0;
1323 urb->actual_length = min_t(u32, 2,
1324 urb->transfer_buffer_length);
1325 ret_val = 0;
1326 *status = 0;
1327 }
1328 break;
1329 }
1330 return ret_val;
1331}
1332
1200/* drive both sides of the transfers; looks like irq handlers to 1333/* drive both sides of the transfers; looks like irq handlers to
1201 * both drivers except the callbacks aren't in_irq(). 1334 * both drivers except the callbacks aren't in_irq().
1202 */ 1335 */
@@ -1299,14 +1432,8 @@ restart:
1299 if (ep == &dum->ep [0] && ep->setup_stage) { 1432 if (ep == &dum->ep [0] && ep->setup_stage) {
1300 struct usb_ctrlrequest setup; 1433 struct usb_ctrlrequest setup;
1301 int value = 1; 1434 int value = 1;
1302 struct dummy_ep *ep2;
1303 unsigned w_index;
1304 unsigned w_value;
1305 1435
1306 setup = *(struct usb_ctrlrequest*) urb->setup_packet; 1436 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1307 w_index = le16_to_cpu(setup.wIndex);
1308 w_value = le16_to_cpu(setup.wValue);
1309
1310 /* paranoia, in case of stale queued data */ 1437 /* paranoia, in case of stale queued data */
1311 list_for_each_entry (req, &ep->queue, queue) { 1438 list_for_each_entry (req, &ep->queue, queue) {
1312 list_del_init (&req->queue); 1439 list_del_init (&req->queue);
@@ -1328,117 +1455,9 @@ restart:
1328 ep->last_io = jiffies; 1455 ep->last_io = jiffies;
1329 ep->setup_stage = 0; 1456 ep->setup_stage = 0;
1330 ep->halted = 0; 1457 ep->halted = 0;
1331 switch (setup.bRequest) {
1332 case USB_REQ_SET_ADDRESS:
1333 if (setup.bRequestType != Dev_Request)
1334 break;
1335 dum->address = w_value;
1336 status = 0;
1337 dev_dbg (udc_dev(dum), "set_address = %d\n",
1338 w_value);
1339 value = 0;
1340 break;
1341 case USB_REQ_SET_FEATURE:
1342 if (setup.bRequestType == Dev_Request) {
1343 value = 0;
1344 switch (w_value) {
1345 case USB_DEVICE_REMOTE_WAKEUP:
1346 break;
1347 case USB_DEVICE_B_HNP_ENABLE:
1348 dum->gadget.b_hnp_enable = 1;
1349 break;
1350 case USB_DEVICE_A_HNP_SUPPORT:
1351 dum->gadget.a_hnp_support = 1;
1352 break;
1353 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1354 dum->gadget.a_alt_hnp_support
1355 = 1;
1356 break;
1357 default:
1358 value = -EOPNOTSUPP;
1359 }
1360 if (value == 0) {
1361 dum->devstatus |=
1362 (1 << w_value);
1363 status = 0;
1364 }
1365 1458
1366 } else if (setup.bRequestType == Ep_Request) { 1459 value = handle_control_request(dum, urb, &setup,
1367 // endpoint halt 1460 &status);
1368 ep2 = find_endpoint (dum, w_index);
1369 if (!ep2 || ep2->ep.name == ep0name) {
1370 value = -EOPNOTSUPP;
1371 break;
1372 }
1373 ep2->halted = 1;
1374 value = 0;
1375 status = 0;
1376 }
1377 break;
1378 case USB_REQ_CLEAR_FEATURE:
1379 if (setup.bRequestType == Dev_Request) {
1380 switch (w_value) {
1381 case USB_DEVICE_REMOTE_WAKEUP:
1382 dum->devstatus &= ~(1 <<
1383 USB_DEVICE_REMOTE_WAKEUP);
1384 value = 0;
1385 status = 0;
1386 break;
1387 default:
1388 value = -EOPNOTSUPP;
1389 break;
1390 }
1391 } else if (setup.bRequestType == Ep_Request) {
1392 // endpoint halt
1393 ep2 = find_endpoint (dum, w_index);
1394 if (!ep2) {
1395 value = -EOPNOTSUPP;
1396 break;
1397 }
1398 if (!ep2->wedged)
1399 ep2->halted = 0;
1400 value = 0;
1401 status = 0;
1402 }
1403 break;
1404 case USB_REQ_GET_STATUS:
1405 if (setup.bRequestType == Dev_InRequest
1406 || setup.bRequestType
1407 == Intf_InRequest
1408 || setup.bRequestType
1409 == Ep_InRequest
1410 ) {
1411 char *buf;
1412
1413 // device: remote wakeup, selfpowered
1414 // interface: nothing
1415 // endpoint: halt
1416 buf = (char *)urb->transfer_buffer;
1417 if (urb->transfer_buffer_length > 0) {
1418 if (setup.bRequestType ==
1419 Ep_InRequest) {
1420 ep2 = find_endpoint (dum, w_index);
1421 if (!ep2) {
1422 value = -EOPNOTSUPP;
1423 break;
1424 }
1425 buf [0] = ep2->halted;
1426 } else if (setup.bRequestType ==
1427 Dev_InRequest) {
1428 buf [0] = (u8)
1429 dum->devstatus;
1430 } else
1431 buf [0] = 0;
1432 }
1433 if (urb->transfer_buffer_length > 1)
1434 buf [1] = 0;
1435 urb->actual_length = min_t(u32, 2,
1436 urb->transfer_buffer_length);
1437 value = 0;
1438 status = 0;
1439 }
1440 break;
1441 }
1442 1461
1443 /* gadget driver handles all other requests. block 1462 /* gadget driver handles all other requests. block
1444 * until setup() returns; no reentrancy issues etc. 1463 * until setup() returns; no reentrancy issues etc.
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index 484c5ba5450e..1499f9e4afa8 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 *
@@ -38,62 +38,56 @@
38#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */ 38#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
39 39
40 40
41/* Debuging *****************************************************************/ 41/* Debugging ****************************************************************/
42
43#define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
44
45#define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
46#define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
47
48#ifdef DEBUG
49# define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
50#else
51# define FDBG(...) do { } while (0)
52#endif /* DEBUG */
53
54#ifdef VERBOSE_DEBUG
55# define FVDBG FDBG
56#else
57# define FVDBG(...) do { } while (0)
58#endif /* VERBOSE_DEBUG */
59
60#define ENTER() FVDBG("%s()", __func__)
61 42
62#ifdef VERBOSE_DEBUG 43#ifdef VERBOSE_DEBUG
44# define pr_vdebug pr_debug
63# define ffs_dump_mem(prefix, ptr, len) \ 45# define ffs_dump_mem(prefix, ptr, len) \
64 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)
65#else 47#else
48# define pr_vdebug(...) do { } while (0)
66# define ffs_dump_mem(prefix, ptr, len) do { } while (0) 49# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
67#endif 50#endif /* VERBOSE_DEBUG */
51
52#define ENTER() pr_vdebug("%s()\n", __func__)
68 53
69 54
70/* The data structure and setup file ****************************************/ 55/* The data structure and setup file ****************************************/
71 56
72enum ffs_state { 57enum ffs_state {
73 /* Waiting for descriptors and strings. */ 58 /*
74 /* 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
75 * may succeed (which should not be the problem as there 62 * may succeed (which should not be the problem as there
76 * should be no such files opened in the firts place). */ 63 * should be no such files opened in the first place).
64 */
77 FFS_READ_DESCRIPTORS, 65 FFS_READ_DESCRIPTORS,
78 FFS_READ_STRINGS, 66 FFS_READ_STRINGS,
79 67
80 /* We've got descriptors and strings. We are or have called 68 /*
69 * We've got descriptors and strings. We are or have called
81 * functionfs_ready_callback(). functionfs_bind() may have 70 * functionfs_ready_callback(). functionfs_bind() may have
82 * been called but we don't know. */ 71 * been called but we don't know.
83 /* This is the only state in which operations on epfiles may 72 *
84 * succeed. */ 73 * This is the only state in which operations on epfiles may
74 * succeed.
75 */
85 FFS_ACTIVE, 76 FFS_ACTIVE,
86 77
87 /* All endpoints have been closed. This state is also set if 78 /*
79 * All endpoints have been closed. This state is also set if
88 * we encounter an unrecoverable error. The only 80 * we encounter an unrecoverable error. The only
89 * unrecoverable error is situation when after reading strings 81 * unrecoverable error is situation when after reading strings
90 * from user space we fail to initialise EP files or 82 * from user space we fail to initialise epfiles or
91 * functionfs_ready_callback() returns with error (<0). */ 83 * functionfs_ready_callback() returns with error (<0).
92 /* 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
93 * as well as epfile) may succeed (at this point epfiles are 86 * as well as epfile) may succeed (at this point epfiles are
94 * 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
95 * 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
96 * fail). */ 89 * fail).
90 */
97 FFS_CLOSING 91 FFS_CLOSING
98}; 92};
99 93
@@ -101,14 +95,18 @@ enum ffs_state {
101enum ffs_setup_state { 95enum ffs_setup_state {
102 /* There is no setup request pending. */ 96 /* There is no setup request pending. */
103 FFS_NO_SETUP, 97 FFS_NO_SETUP,
104 /* User has read events and there was a setup request event 98 /*
99 * User has read events and there was a setup request event
105 * there. The next read/write on ep0 will handle the 100 * there. The next read/write on ep0 will handle the
106 * request. */ 101 * request.
102 */
107 FFS_SETUP_PENDING, 103 FFS_SETUP_PENDING,
108 /* There was event pending but before user space handled it 104 /*
105 * There was event pending but before user space handled it
109 * some other event was introduced which canceled existing 106 * some other event was introduced which canceled existing
110 * setup. If this state is set read/write on ep0 return 107 * setup. If this state is set read/write on ep0 return
111 * -EIDRM. This state is only set when adding event. */ 108 * -EIDRM. This state is only set when adding event.
109 */
112 FFS_SETUP_CANCELED 110 FFS_SETUP_CANCELED
113}; 111};
114 112
@@ -120,23 +118,29 @@ struct ffs_function;
120struct ffs_data { 118struct ffs_data {
121 struct usb_gadget *gadget; 119 struct usb_gadget *gadget;
122 120
123 /* Protect access read/write operations, only one read/write 121 /*
122 * Protect access read/write operations, only one read/write
124 * at a time. As a consequence protects ep0req and company. 123 * at a time. As a consequence protects ep0req and company.
125 * While setup request is being processed (queued) this is 124 * While setup request is being processed (queued) this is
126 * held. */ 125 * held.
126 */
127 struct mutex mutex; 127 struct mutex mutex;
128 128
129 /* Protect access to enpoint related structures (basically 129 /*
130 * Protect access to endpoint related structures (basically
130 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for 131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
131 * endpint zero. */ 132 * endpoint zero.
133 */
132 spinlock_t eps_lock; 134 spinlock_t eps_lock;
133 135
134 /* XXX REVISIT do we need our own request? Since we are not 136 /*
135 * 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
136 * 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
137 * 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
138 * 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
139 * after all, maybe we just need some spinlock for that? */ 142 * after all, maybe we just need some spinlock for that?
143 */
140 struct usb_request *ep0req; /* P: mutex */ 144 struct usb_request *ep0req; /* P: mutex */
141 struct completion ep0req_completion; /* P: mutex */ 145 struct completion ep0req_completion; /* P: mutex */
142 int ep0req_status; /* P: mutex */ 146 int ep0req_status; /* P: mutex */
@@ -150,7 +154,7 @@ struct ffs_data {
150 enum ffs_state state; 154 enum ffs_state state;
151 155
152 /* 156 /*
153 * Possible transations: 157 * Possible transitions:
154 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock 158 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
155 * happens only in ep0 read which is P: mutex 159 * happens only in ep0 read which is P: mutex
156 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock 160 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
@@ -183,18 +187,21 @@ struct ffs_data {
183 /* Active function */ 187 /* Active function */
184 struct ffs_function *func; 188 struct ffs_function *func;
185 189
186 /* Device name, write once when file system is mounted. 190 /*
187 * 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 */
188 const char *dev_name; 194 const char *dev_name;
189 /* Private data for our user (ie. gadget). Managed by 195 /* Private data for our user (ie. gadget). Managed by user. */
190 * user. */
191 void *private_data; 196 void *private_data;
192 197
193 /* filled by __ffs_data_got_descs() */ 198 /* filled by __ffs_data_got_descs() */
194 /* real descriptors are 16 bytes after raw_descs (so you need 199 /*
200 * Real descriptors are 16 bytes after raw_descs (so you need
195 * 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
196 * first full speed descriptor). raw_descs_length and 202 * first full speed descriptor). raw_descs_length and
197 * raw_fs_descs_length do not have those 16 bytes added. */ 203 * raw_fs_descs_length do not have those 16 bytes added.
204 */
198 const void *raw_descs; 205 const void *raw_descs;
199 unsigned raw_descs_length; 206 unsigned raw_descs_length;
200 unsigned raw_fs_descs_length; 207 unsigned raw_fs_descs_length;
@@ -211,18 +218,23 @@ struct ffs_data {
211 const void *raw_strings; 218 const void *raw_strings;
212 struct usb_gadget_strings **stringtabs; 219 struct usb_gadget_strings **stringtabs;
213 220
214 /* 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 */
215 struct super_block *sb; 225 struct super_block *sb;
216 226
217 /* File permissions, written once when fs is mounted*/ 227 /* File permissions, written once when fs is mounted */
218 struct ffs_file_perms { 228 struct ffs_file_perms {
219 umode_t mode; 229 umode_t mode;
220 uid_t uid; 230 uid_t uid;
221 gid_t gid; 231 gid_t gid;
222 } file_perms; 232 } file_perms;
223 233
224 /* The endpoint files, filled by ffs_epfiles_create(), 234 /*
225 * destroyed by ffs_epfiles_destroy(). */ 235 * The endpoint files, filled by ffs_epfiles_create(),
236 * destroyed by ffs_epfiles_destroy().
237 */
226 struct ffs_epfile *epfiles; 238 struct ffs_epfile *epfiles;
227}; 239};
228 240
@@ -236,7 +248,7 @@ static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
236static void ffs_data_opened(struct ffs_data *ffs); 248static void ffs_data_opened(struct ffs_data *ffs);
237static void ffs_data_closed(struct ffs_data *ffs); 249static void ffs_data_closed(struct ffs_data *ffs);
238 250
239/* Called with ffs->mutex held; take over ownerrship of data. */ 251/* Called with ffs->mutex held; take over ownership of data. */
240static int __must_check 252static int __must_check
241__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);
242static int __must_check 254static int __must_check
@@ -267,11 +279,9 @@ static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
267 279
268static void ffs_func_free(struct ffs_function *func); 280static void ffs_func_free(struct ffs_function *func);
269 281
270
271static void ffs_func_eps_disable(struct ffs_function *func); 282static void ffs_func_eps_disable(struct ffs_function *func);
272static int __must_check ffs_func_eps_enable(struct ffs_function *func); 283static int __must_check ffs_func_eps_enable(struct ffs_function *func);
273 284
274
275static int ffs_func_bind(struct usb_configuration *, 285static int ffs_func_bind(struct usb_configuration *,
276 struct usb_function *); 286 struct usb_function *);
277static void ffs_func_unbind(struct usb_configuration *, 287static void ffs_func_unbind(struct usb_configuration *,
@@ -288,7 +298,6 @@ static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
288static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf); 298static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
289 299
290 300
291
292/* The endpoints structures *************************************************/ 301/* The endpoints structures *************************************************/
293 302
294struct ffs_ep { 303struct ffs_ep {
@@ -321,7 +330,6 @@ struct ffs_epfile {
321 unsigned char _pad; 330 unsigned char _pad;
322}; 331};
323 332
324
325static int __must_check ffs_epfiles_create(struct ffs_data *ffs); 333static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
326static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); 334static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
327 335
@@ -348,7 +356,6 @@ static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
348 complete_all(&ffs->ep0req_completion); 356 complete_all(&ffs->ep0req_completion);
349} 357}
350 358
351
352static 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)
353{ 360{
354 struct usb_request *req = ffs->ep0req; 361 struct usb_request *req = ffs->ep0req;
@@ -380,17 +387,16 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
380static int __ffs_ep0_stall(struct ffs_data *ffs) 387static int __ffs_ep0_stall(struct ffs_data *ffs)
381{ 388{
382 if (ffs->ev.can_stall) { 389 if (ffs->ev.can_stall) {
383 FVDBG("ep0 stall\n"); 390 pr_vdebug("ep0 stall\n");
384 usb_ep_set_halt(ffs->gadget->ep0); 391 usb_ep_set_halt(ffs->gadget->ep0);
385 ffs->setup_state = FFS_NO_SETUP; 392 ffs->setup_state = FFS_NO_SETUP;
386 return -EL2HLT; 393 return -EL2HLT;
387 } else { 394 } else {
388 FDBG("bogus ep0 stall!\n"); 395 pr_debug("bogus ep0 stall!\n");
389 return -ESRCH; 396 return -ESRCH;
390 } 397 }
391} 398}
392 399
393
394static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, 400static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
395 size_t len, loff_t *ptr) 401 size_t len, loff_t *ptr)
396{ 402{
@@ -409,7 +415,6 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
409 if (unlikely(ret < 0)) 415 if (unlikely(ret < 0))
410 return ret; 416 return ret;
411 417
412
413 /* Check state */ 418 /* Check state */
414 switch (ffs->state) { 419 switch (ffs->state) {
415 case FFS_READ_DESCRIPTORS: 420 case FFS_READ_DESCRIPTORS:
@@ -421,14 +426,14 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
421 } 426 }
422 427
423 data = ffs_prepare_buffer(buf, len); 428 data = ffs_prepare_buffer(buf, len);
424 if (unlikely(IS_ERR(data))) { 429 if (IS_ERR(data)) {
425 ret = PTR_ERR(data); 430 ret = PTR_ERR(data);
426 break; 431 break;
427 } 432 }
428 433
429 /* Handle data */ 434 /* Handle data */
430 if (ffs->state == FFS_READ_DESCRIPTORS) { 435 if (ffs->state == FFS_READ_DESCRIPTORS) {
431 FINFO("read descriptors"); 436 pr_info("read descriptors\n");
432 ret = __ffs_data_got_descs(ffs, data, len); 437 ret = __ffs_data_got_descs(ffs, data, len);
433 if (unlikely(ret < 0)) 438 if (unlikely(ret < 0))
434 break; 439 break;
@@ -436,7 +441,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
436 ffs->state = FFS_READ_STRINGS; 441 ffs->state = FFS_READ_STRINGS;
437 ret = len; 442 ret = len;
438 } else { 443 } else {
439 FINFO("read strings"); 444 pr_info("read strings\n");
440 ret = __ffs_data_got_strings(ffs, data, len); 445 ret = __ffs_data_got_strings(ffs, data, len);
441 if (unlikely(ret < 0)) 446 if (unlikely(ret < 0))
442 break; 447 break;
@@ -461,11 +466,12 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
461 } 466 }
462 break; 467 break;
463 468
464
465 case FFS_ACTIVE: 469 case FFS_ACTIVE:
466 data = NULL; 470 data = NULL;
467 /* We're called from user space, we can use _irq 471 /*
468 * rather then _irqsave */ 472 * We're called from user space, we can use _irq
473 * rather then _irqsave
474 */
469 spin_lock_irq(&ffs->ev.waitq.lock); 475 spin_lock_irq(&ffs->ev.waitq.lock);
470 switch (FFS_SETUP_STATE(ffs)) { 476 switch (FFS_SETUP_STATE(ffs)) {
471 case FFS_SETUP_CANCELED: 477 case FFS_SETUP_CANCELED:
@@ -493,23 +499,25 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
493 spin_unlock_irq(&ffs->ev.waitq.lock); 499 spin_unlock_irq(&ffs->ev.waitq.lock);
494 500
495 data = ffs_prepare_buffer(buf, len); 501 data = ffs_prepare_buffer(buf, len);
496 if (unlikely(IS_ERR(data))) { 502 if (IS_ERR(data)) {
497 ret = PTR_ERR(data); 503 ret = PTR_ERR(data);
498 break; 504 break;
499 } 505 }
500 506
501 spin_lock_irq(&ffs->ev.waitq.lock); 507 spin_lock_irq(&ffs->ev.waitq.lock);
502 508
503 /* We are guaranteed to be still in FFS_ACTIVE state 509 /*
510 * We are guaranteed to be still in FFS_ACTIVE state
504 * but the state of setup could have changed from 511 * but the state of setup could have changed from
505 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need 512 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
506 * to check for that. If that happened we copied data 513 * to check for that. If that happened we copied data
507 * from user space in vain but it's unlikely. */ 514 * from user space in vain but it's unlikely.
508 /* For sure we are not in FFS_NO_SETUP since this is 515 *
516 * For sure we are not in FFS_NO_SETUP since this is
509 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP 517 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
510 * transition can be performed and it's protected by 518 * transition can be performed and it's protected by
511 * mutex. */ 519 * mutex.
512 520 */
513 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { 521 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
514 ret = -EIDRM; 522 ret = -EIDRM;
515done_spin: 523done_spin:
@@ -521,25 +529,22 @@ done_spin:
521 kfree(data); 529 kfree(data);
522 break; 530 break;
523 531
524
525 default: 532 default:
526 ret = -EBADFD; 533 ret = -EBADFD;
527 break; 534 break;
528 } 535 }
529 536
530
531 mutex_unlock(&ffs->mutex); 537 mutex_unlock(&ffs->mutex);
532 return ret; 538 return ret;
533} 539}
534 540
535
536
537static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, 541static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
538 size_t n) 542 size_t n)
539{ 543{
540 /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need 544 /*
541 * to release them. */ 545 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
542 546 * to release them.
547 */
543 struct usb_functionfs_event events[n]; 548 struct usb_functionfs_event events[n];
544 unsigned i = 0; 549 unsigned i = 0;
545 550
@@ -568,7 +573,6 @@ static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
568 ? -EFAULT : sizeof events; 573 ? -EFAULT : sizeof events;
569} 574}
570 575
571
572static ssize_t ffs_ep0_read(struct file *file, char __user *buf, 576static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
573 size_t len, loff_t *ptr) 577 size_t len, loff_t *ptr)
574{ 578{
@@ -588,16 +592,16 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
588 if (unlikely(ret < 0)) 592 if (unlikely(ret < 0))
589 return ret; 593 return ret;
590 594
591
592 /* Check state */ 595 /* Check state */
593 if (ffs->state != FFS_ACTIVE) { 596 if (ffs->state != FFS_ACTIVE) {
594 ret = -EBADFD; 597 ret = -EBADFD;
595 goto done_mutex; 598 goto done_mutex;
596 } 599 }
597 600
598 601 /*
599 /* We're called from user space, we can use _irq rather then 602 * We're called from user space, we can use _irq rather then
600 * _irqsave */ 603 * _irqsave
604 */
601 spin_lock_irq(&ffs->ev.waitq.lock); 605 spin_lock_irq(&ffs->ev.waitq.lock);
602 606
603 switch (FFS_SETUP_STATE(ffs)) { 607 switch (FFS_SETUP_STATE(ffs)) {
@@ -617,7 +621,8 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
617 break; 621 break;
618 } 622 }
619 623
620 if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, ffs->ev.count))) { 624 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
625 ffs->ev.count)) {
621 ret = -EINTR; 626 ret = -EINTR;
622 break; 627 break;
623 } 628 }
@@ -625,7 +630,6 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
625 return __ffs_ep0_read_events(ffs, buf, 630 return __ffs_ep0_read_events(ffs, buf,
626 min(n, (size_t)ffs->ev.count)); 631 min(n, (size_t)ffs->ev.count));
627 632
628
629 case FFS_SETUP_PENDING: 633 case FFS_SETUP_PENDING:
630 if (ffs->ev.setup.bRequestType & USB_DIR_IN) { 634 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
631 spin_unlock_irq(&ffs->ev.waitq.lock); 635 spin_unlock_irq(&ffs->ev.waitq.lock);
@@ -671,8 +675,6 @@ done_mutex:
671 return ret; 675 return ret;
672} 676}
673 677
674
675
676static int ffs_ep0_open(struct inode *inode, struct file *file) 678static int ffs_ep0_open(struct inode *inode, struct file *file)
677{ 679{
678 struct ffs_data *ffs = inode->i_private; 680 struct ffs_data *ffs = inode->i_private;
@@ -688,7 +690,6 @@ static int ffs_ep0_open(struct inode *inode, struct file *file)
688 return 0; 690 return 0;
689} 691}
690 692
691
692static int ffs_ep0_release(struct inode *inode, struct file *file) 693static int ffs_ep0_release(struct inode *inode, struct file *file)
693{ 694{
694 struct ffs_data *ffs = file->private_data; 695 struct ffs_data *ffs = file->private_data;
@@ -700,7 +701,6 @@ static int ffs_ep0_release(struct inode *inode, struct file *file)
700 return 0; 701 return 0;
701} 702}
702 703
703
704static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) 704static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
705{ 705{
706 struct ffs_data *ffs = file->private_data; 706 struct ffs_data *ffs = file->private_data;
@@ -721,7 +721,6 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
721 return ret; 721 return ret;
722} 722}
723 723
724
725static const struct file_operations ffs_ep0_operations = { 724static const struct file_operations ffs_ep0_operations = {
726 .owner = THIS_MODULE, 725 .owner = THIS_MODULE,
727 .llseek = no_llseek, 726 .llseek = no_llseek,
@@ -736,7 +735,6 @@ static const struct file_operations ffs_ep0_operations = {
736 735
737/* "Normal" endpoints operations ********************************************/ 736/* "Normal" endpoints operations ********************************************/
738 737
739
740static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req) 738static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
741{ 739{
742 ENTER(); 740 ENTER();
@@ -747,7 +745,6 @@ static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
747 } 745 }
748} 746}
749 747
750
751static ssize_t ffs_epfile_io(struct file *file, 748static ssize_t ffs_epfile_io(struct file *file,
752 char __user *buf, size_t len, int read) 749 char __user *buf, size_t len, int read)
753{ 750{
@@ -777,8 +774,8 @@ first_try:
777 goto error; 774 goto error;
778 } 775 }
779 776
780 if (unlikely(wait_event_interruptible 777 if (wait_event_interruptible(epfile->wait,
781 (epfile->wait, (ep = epfile->ep)))) { 778 (ep = epfile->ep))) {
782 ret = -EINTR; 779 ret = -EINTR;
783 goto error; 780 goto error;
784 } 781 }
@@ -810,12 +807,16 @@ first_try:
810 if (unlikely(ret)) 807 if (unlikely(ret))
811 goto error; 808 goto error;
812 809
813 /* We're called from user space, we can use _irq rather then 810 /*
814 * _irqsave */ 811 * We're called from user space, we can use _irq rather then
812 * _irqsave
813 */
815 spin_lock_irq(&epfile->ffs->eps_lock); 814 spin_lock_irq(&epfile->ffs->eps_lock);
816 815
817 /* While we were acquiring mutex endpoint got disabled 816 /*
818 * or changed? */ 817 * While we were acquiring mutex endpoint got disabled
818 * or changed?
819 */
819 } while (unlikely(epfile->ep != ep)); 820 } while (unlikely(epfile->ep != ep));
820 821
821 /* Halt */ 822 /* Halt */
@@ -857,7 +858,6 @@ error:
857 return ret; 858 return ret;
858} 859}
859 860
860
861static ssize_t 861static ssize_t
862ffs_epfile_write(struct file *file, const char __user *buf, size_t len, 862ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
863 loff_t *ptr) 863 loff_t *ptr)
@@ -903,7 +903,6 @@ ffs_epfile_release(struct inode *inode, struct file *file)
903 return 0; 903 return 0;
904} 904}
905 905
906
907static long ffs_epfile_ioctl(struct file *file, unsigned code, 906static long ffs_epfile_ioctl(struct file *file, unsigned code,
908 unsigned long value) 907 unsigned long value)
909{ 908{
@@ -942,7 +941,6 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,
942 return ret; 941 return ret;
943} 942}
944 943
945
946static const struct file_operations ffs_epfile_operations = { 944static const struct file_operations ffs_epfile_operations = {
947 .owner = THIS_MODULE, 945 .owner = THIS_MODULE,
948 .llseek = no_llseek, 946 .llseek = no_llseek,
@@ -955,15 +953,13 @@ static const struct file_operations ffs_epfile_operations = {
955}; 953};
956 954
957 955
958
959/* File system and super block operations ***********************************/ 956/* File system and super block operations ***********************************/
960 957
961/* 958/*
962 * Mounting the filesystem creates a controller file, used first for 959 * Mounting the file system creates a controller file, used first for
963 * function configuration then later for event monitoring. 960 * function configuration then later for event monitoring.
964 */ 961 */
965 962
966
967static struct inode *__must_check 963static struct inode *__must_check
968ffs_sb_make_inode(struct super_block *sb, void *data, 964ffs_sb_make_inode(struct super_block *sb, void *data,
969 const struct file_operations *fops, 965 const struct file_operations *fops,
@@ -996,9 +992,7 @@ ffs_sb_make_inode(struct super_block *sb, void *data,
996 return inode; 992 return inode;
997} 993}
998 994
999
1000/* Create "regular" file */ 995/* Create "regular" file */
1001
1002static struct inode *ffs_sb_create_file(struct super_block *sb, 996static struct inode *ffs_sb_create_file(struct super_block *sb,
1003 const char *name, void *data, 997 const char *name, void *data,
1004 const struct file_operations *fops, 998 const struct file_operations *fops,
@@ -1027,9 +1021,7 @@ static struct inode *ffs_sb_create_file(struct super_block *sb,
1027 return inode; 1021 return inode;
1028} 1022}
1029 1023
1030
1031/* Super block */ 1024/* Super block */
1032
1033static const struct super_operations ffs_sb_operations = { 1025static const struct super_operations ffs_sb_operations = {
1034 .statfs = simple_statfs, 1026 .statfs = simple_statfs,
1035 .drop_inode = generic_delete_inode, 1027 .drop_inode = generic_delete_inode,
@@ -1050,7 +1042,7 @@ static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1050 1042
1051 ENTER(); 1043 ENTER();
1052 1044
1053 /* Initialize data */ 1045 /* Initialise data */
1054 ffs = ffs_data_new(); 1046 ffs = ffs_data_new();
1055 if (unlikely(!ffs)) 1047 if (unlikely(!ffs))
1056 goto enomem0; 1048 goto enomem0;
@@ -1096,7 +1088,6 @@ enomem0:
1096 return -ENOMEM; 1088 return -ENOMEM;
1097} 1089}
1098 1090
1099
1100static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts) 1091static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1101{ 1092{
1102 ENTER(); 1093 ENTER();
@@ -1116,7 +1107,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1116 /* Value limit */ 1107 /* Value limit */
1117 eq = strchr(opts, '='); 1108 eq = strchr(opts, '=');
1118 if (unlikely(!eq)) { 1109 if (unlikely(!eq)) {
1119 FERR("'=' missing in %s", opts); 1110 pr_err("'=' missing in %s\n", opts);
1120 return -EINVAL; 1111 return -EINVAL;
1121 } 1112 }
1122 *eq = 0; 1113 *eq = 0;
@@ -1124,7 +1115,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1124 /* Parse value */ 1115 /* Parse value */
1125 value = simple_strtoul(eq + 1, &end, 0); 1116 value = simple_strtoul(eq + 1, &end, 0);
1126 if (unlikely(*end != ',' && *end != 0)) { 1117 if (unlikely(*end != ',' && *end != 0)) {
1127 FERR("%s: invalid value: %s", opts, eq + 1); 1118 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1128 return -EINVAL; 1119 return -EINVAL;
1129 } 1120 }
1130 1121
@@ -1159,7 +1150,7 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1159 1150
1160 default: 1151 default:
1161invalid: 1152invalid:
1162 FERR("%s: invalid option", opts); 1153 pr_err("%s: invalid option\n", opts);
1163 return -EINVAL; 1154 return -EINVAL;
1164 } 1155 }
1165 1156
@@ -1172,7 +1163,6 @@ invalid:
1172 return 0; 1163 return 0;
1173} 1164}
1174 1165
1175
1176/* "mount -t functionfs dev_name /dev/function" ends up here */ 1166/* "mount -t functionfs dev_name /dev/function" ends up here */
1177 1167
1178static struct dentry * 1168static struct dentry *
@@ -1224,10 +1214,8 @@ static struct file_system_type ffs_fs_type = {
1224}; 1214};
1225 1215
1226 1216
1227
1228/* Driver's main init/cleanup functions *************************************/ 1217/* Driver's main init/cleanup functions *************************************/
1229 1218
1230
1231static int functionfs_init(void) 1219static int functionfs_init(void)
1232{ 1220{
1233 int ret; 1221 int ret;
@@ -1236,9 +1224,9 @@ static int functionfs_init(void)
1236 1224
1237 ret = register_filesystem(&ffs_fs_type); 1225 ret = register_filesystem(&ffs_fs_type);
1238 if (likely(!ret)) 1226 if (likely(!ret))
1239 FINFO("file system registered"); 1227 pr_info("file system registered\n");
1240 else 1228 else
1241 FERR("failed registering file system (%d)", ret); 1229 pr_err("failed registering file system (%d)\n", ret);
1242 1230
1243 return ret; 1231 return ret;
1244} 1232}
@@ -1247,18 +1235,16 @@ static void functionfs_cleanup(void)
1247{ 1235{
1248 ENTER(); 1236 ENTER();
1249 1237
1250 FINFO("unloading"); 1238 pr_info("unloading\n");
1251 unregister_filesystem(&ffs_fs_type); 1239 unregister_filesystem(&ffs_fs_type);
1252} 1240}
1253 1241
1254 1242
1255
1256/* ffs_data and ffs_function construction and destruction code **************/ 1243/* ffs_data and ffs_function construction and destruction code **************/
1257 1244
1258static void ffs_data_clear(struct ffs_data *ffs); 1245static void ffs_data_clear(struct ffs_data *ffs);
1259static void ffs_data_reset(struct ffs_data *ffs); 1246static void ffs_data_reset(struct ffs_data *ffs);
1260 1247
1261
1262static void ffs_data_get(struct ffs_data *ffs) 1248static void ffs_data_get(struct ffs_data *ffs)
1263{ 1249{
1264 ENTER(); 1250 ENTER();
@@ -1279,7 +1265,7 @@ static void ffs_data_put(struct ffs_data *ffs)
1279 ENTER(); 1265 ENTER();
1280 1266
1281 if (unlikely(atomic_dec_and_test(&ffs->ref))) { 1267 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1282 FINFO("%s(): freeing", __func__); 1268 pr_info("%s(): freeing\n", __func__);
1283 ffs_data_clear(ffs); 1269 ffs_data_clear(ffs);
1284 BUG_ON(mutex_is_locked(&ffs->mutex) || 1270 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1285 spin_is_locked(&ffs->ev.waitq.lock) || 1271 spin_is_locked(&ffs->ev.waitq.lock) ||
@@ -1289,8 +1275,6 @@ static void ffs_data_put(struct ffs_data *ffs)
1289 } 1275 }
1290} 1276}
1291 1277
1292
1293
1294static void ffs_data_closed(struct ffs_data *ffs) 1278static void ffs_data_closed(struct ffs_data *ffs)
1295{ 1279{
1296 ENTER(); 1280 ENTER();
@@ -1303,7 +1287,6 @@ static void ffs_data_closed(struct ffs_data *ffs)
1303 ffs_data_put(ffs); 1287 ffs_data_put(ffs);
1304} 1288}
1305 1289
1306
1307static struct ffs_data *ffs_data_new(void) 1290static struct ffs_data *ffs_data_new(void)
1308{ 1291{
1309 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); 1292 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
@@ -1326,7 +1309,6 @@ static struct ffs_data *ffs_data_new(void)
1326 return ffs; 1309 return ffs;
1327} 1310}
1328 1311
1329
1330static void ffs_data_clear(struct ffs_data *ffs) 1312static void ffs_data_clear(struct ffs_data *ffs)
1331{ 1313{
1332 ENTER(); 1314 ENTER();
@@ -1344,7 +1326,6 @@ static void ffs_data_clear(struct ffs_data *ffs)
1344 kfree(ffs->stringtabs); 1326 kfree(ffs->stringtabs);
1345} 1327}
1346 1328
1347
1348static void ffs_data_reset(struct ffs_data *ffs) 1329static void ffs_data_reset(struct ffs_data *ffs)
1349{ 1330{
1350 ENTER(); 1331 ENTER();
@@ -1407,7 +1388,6 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1407 return 0; 1388 return 0;
1408} 1389}
1409 1390
1410
1411static void functionfs_unbind(struct ffs_data *ffs) 1391static void functionfs_unbind(struct ffs_data *ffs)
1412{ 1392{
1413 ENTER(); 1393 ENTER();
@@ -1420,7 +1400,6 @@ static void functionfs_unbind(struct ffs_data *ffs)
1420 } 1400 }
1421} 1401}
1422 1402
1423
1424static int ffs_epfiles_create(struct ffs_data *ffs) 1403static int ffs_epfiles_create(struct ffs_data *ffs)
1425{ 1404{
1426 struct ffs_epfile *epfile, *epfiles; 1405 struct ffs_epfile *epfile, *epfiles;
@@ -1451,7 +1430,6 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
1451 return 0; 1430 return 0;
1452} 1431}
1453 1432
1454
1455static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) 1433static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1456{ 1434{
1457 struct ffs_epfile *epfile = epfiles; 1435 struct ffs_epfile *epfile = epfiles;
@@ -1471,7 +1449,6 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1471 kfree(epfiles); 1449 kfree(epfiles);
1472} 1450}
1473 1451
1474
1475static int functionfs_bind_config(struct usb_composite_dev *cdev, 1452static int functionfs_bind_config(struct usb_composite_dev *cdev,
1476 struct usb_configuration *c, 1453 struct usb_configuration *c,
1477 struct ffs_data *ffs) 1454 struct ffs_data *ffs)
@@ -1491,7 +1468,6 @@ static int functionfs_bind_config(struct usb_composite_dev *cdev,
1491 func->function.bind = ffs_func_bind; 1468 func->function.bind = ffs_func_bind;
1492 func->function.unbind = ffs_func_unbind; 1469 func->function.unbind = ffs_func_unbind;
1493 func->function.set_alt = ffs_func_set_alt; 1470 func->function.set_alt = ffs_func_set_alt;
1494 /*func->function.get_alt = ffs_func_get_alt;*/
1495 func->function.disable = ffs_func_disable; 1471 func->function.disable = ffs_func_disable;
1496 func->function.setup = ffs_func_setup; 1472 func->function.setup = ffs_func_setup;
1497 func->function.suspend = ffs_func_suspend; 1473 func->function.suspend = ffs_func_suspend;
@@ -1516,14 +1492,15 @@ static void ffs_func_free(struct ffs_function *func)
1516 ffs_data_put(func->ffs); 1492 ffs_data_put(func->ffs);
1517 1493
1518 kfree(func->eps); 1494 kfree(func->eps);
1519 /* eps and interfaces_nums are allocated in the same chunk so 1495 /*
1496 * eps and interfaces_nums are allocated in the same chunk so
1520 * only one free is required. Descriptors are also allocated 1497 * only one free is required. Descriptors are also allocated
1521 * in the same chunk. */ 1498 * in the same chunk.
1499 */
1522 1500
1523 kfree(func); 1501 kfree(func);
1524} 1502}
1525 1503
1526
1527static void ffs_func_eps_disable(struct ffs_function *func) 1504static void ffs_func_eps_disable(struct ffs_function *func)
1528{ 1505{
1529 struct ffs_ep *ep = func->eps; 1506 struct ffs_ep *ep = func->eps;
@@ -1581,11 +1558,12 @@ static int ffs_func_eps_enable(struct ffs_function *func)
1581 1558
1582/* Parsing and building descriptors and strings *****************************/ 1559/* Parsing and building descriptors and strings *****************************/
1583 1560
1584 1561/*
1585/* This validates if data pointed by data is a valid USB descriptor as 1562 * This validates if data pointed by data is a valid USB descriptor as
1586 * well as record how many interfaces, endpoints and strings are 1563 * well as record how many interfaces, endpoints and strings are
1587 * required by given configuration. Returns address afther the 1564 * required by given configuration. Returns address after the
1588 * descriptor or NULL if data is invalid. */ 1565 * descriptor or NULL if data is invalid.
1566 */
1589 1567
1590enum ffs_entity_type { 1568enum ffs_entity_type {
1591 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT 1569 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
@@ -1607,14 +1585,14 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1607 1585
1608 /* At least two bytes are required: length and type */ 1586 /* At least two bytes are required: length and type */
1609 if (len < 2) { 1587 if (len < 2) {
1610 FVDBG("descriptor too short"); 1588 pr_vdebug("descriptor too short\n");
1611 return -EINVAL; 1589 return -EINVAL;
1612 } 1590 }
1613 1591
1614 /* If we have at least as many bytes as the descriptor takes? */ 1592 /* If we have at least as many bytes as the descriptor takes? */
1615 length = _ds->bLength; 1593 length = _ds->bLength;
1616 if (len < length) { 1594 if (len < length) {
1617 FVDBG("descriptor longer then available data"); 1595 pr_vdebug("descriptor longer then available data\n");
1618 return -EINVAL; 1596 return -EINVAL;
1619 } 1597 }
1620 1598
@@ -1622,15 +1600,15 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1622#define __entity_check_STRING(val) (val) 1600#define __entity_check_STRING(val) (val)
1623#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK) 1601#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1624#define __entity(type, val) do { \ 1602#define __entity(type, val) do { \
1625 FVDBG("entity " #type "(%02x)", (val)); \ 1603 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1626 if (unlikely(!__entity_check_ ##type(val))) { \ 1604 if (unlikely(!__entity_check_ ##type(val))) { \
1627 FVDBG("invalid entity's value"); \ 1605 pr_vdebug("invalid entity's value\n"); \
1628 return -EINVAL; \ 1606 return -EINVAL; \
1629 } \ 1607 } \
1630 ret = entity(FFS_ ##type, &val, _ds, priv); \ 1608 ret = entity(FFS_ ##type, &val, _ds, priv); \
1631 if (unlikely(ret < 0)) { \ 1609 if (unlikely(ret < 0)) { \
1632 FDBG("entity " #type "(%02x); ret = %d", \ 1610 pr_debug("entity " #type "(%02x); ret = %d\n", \
1633 (val), ret); \ 1611 (val), ret); \
1634 return ret; \ 1612 return ret; \
1635 } \ 1613 } \
1636 } while (0) 1614 } while (0)
@@ -1642,12 +1620,13 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1642 case USB_DT_STRING: 1620 case USB_DT_STRING:
1643 case USB_DT_DEVICE_QUALIFIER: 1621 case USB_DT_DEVICE_QUALIFIER:
1644 /* function can't have any of those */ 1622 /* function can't have any of those */
1645 FVDBG("descriptor reserved for gadget: %d", _ds->bDescriptorType); 1623 pr_vdebug("descriptor reserved for gadget: %d\n",
1624 _ds->bDescriptorType);
1646 return -EINVAL; 1625 return -EINVAL;
1647 1626
1648 case USB_DT_INTERFACE: { 1627 case USB_DT_INTERFACE: {
1649 struct usb_interface_descriptor *ds = (void *)_ds; 1628 struct usb_interface_descriptor *ds = (void *)_ds;
1650 FVDBG("interface descriptor"); 1629 pr_vdebug("interface descriptor\n");
1651 if (length != sizeof *ds) 1630 if (length != sizeof *ds)
1652 goto inv_length; 1631 goto inv_length;
1653 1632
@@ -1659,7 +1638,7 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1659 1638
1660 case USB_DT_ENDPOINT: { 1639 case USB_DT_ENDPOINT: {
1661 struct usb_endpoint_descriptor *ds = (void *)_ds; 1640 struct usb_endpoint_descriptor *ds = (void *)_ds;
1662 FVDBG("endpoint descriptor"); 1641 pr_vdebug("endpoint descriptor\n");
1663 if (length != USB_DT_ENDPOINT_SIZE && 1642 if (length != USB_DT_ENDPOINT_SIZE &&
1664 length != USB_DT_ENDPOINT_AUDIO_SIZE) 1643 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1665 goto inv_length; 1644 goto inv_length;
@@ -1674,7 +1653,7 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1674 1653
1675 case USB_DT_INTERFACE_ASSOCIATION: { 1654 case USB_DT_INTERFACE_ASSOCIATION: {
1676 struct usb_interface_assoc_descriptor *ds = (void *)_ds; 1655 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1677 FVDBG("interface association descriptor"); 1656 pr_vdebug("interface association descriptor\n");
1678 if (length != sizeof *ds) 1657 if (length != sizeof *ds)
1679 goto inv_length; 1658 goto inv_length;
1680 if (ds->iFunction) 1659 if (ds->iFunction)
@@ -1688,17 +1667,17 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1688 case USB_DT_SECURITY: 1667 case USB_DT_SECURITY:
1689 case USB_DT_CS_RADIO_CONTROL: 1668 case USB_DT_CS_RADIO_CONTROL:
1690 /* TODO */ 1669 /* TODO */
1691 FVDBG("unimplemented descriptor: %d", _ds->bDescriptorType); 1670 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1692 return -EINVAL; 1671 return -EINVAL;
1693 1672
1694 default: 1673 default:
1695 /* We should never be here */ 1674 /* We should never be here */
1696 FVDBG("unknown descriptor: %d", _ds->bDescriptorType); 1675 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1697 return -EINVAL; 1676 return -EINVAL;
1698 1677
1699 inv_length: 1678inv_length:
1700 FVDBG("invalid length: %d (descriptor %d)", 1679 pr_vdebug("invalid length: %d (descriptor %d)\n",
1701 _ds->bLength, _ds->bDescriptorType); 1680 _ds->bLength, _ds->bDescriptorType);
1702 return -EINVAL; 1681 return -EINVAL;
1703 } 1682 }
1704 1683
@@ -1711,7 +1690,6 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
1711 return length; 1690 return length;
1712} 1691}
1713 1692
1714
1715static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len, 1693static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1716 ffs_entity_callback entity, void *priv) 1694 ffs_entity_callback entity, void *priv)
1717{ 1695{
@@ -1726,10 +1704,11 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1726 if (num == count) 1704 if (num == count)
1727 data = NULL; 1705 data = NULL;
1728 1706
1729 /* Record "descriptor" entitny */ 1707 /* Record "descriptor" entity */
1730 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); 1708 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1731 if (unlikely(ret < 0)) { 1709 if (unlikely(ret < 0)) {
1732 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num, ret); 1710 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1711 num, ret);
1733 return ret; 1712 return ret;
1734 } 1713 }
1735 1714
@@ -1738,7 +1717,7 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1738 1717
1739 ret = ffs_do_desc(data, len, entity, priv); 1718 ret = ffs_do_desc(data, len, entity, priv);
1740 if (unlikely(ret < 0)) { 1719 if (unlikely(ret < 0)) {
1741 FDBG("%s returns %d", __func__, ret); 1720 pr_debug("%s returns %d\n", __func__, ret);
1742 return ret; 1721 return ret;
1743 } 1722 }
1744 1723
@@ -1748,7 +1727,6 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1748 } 1727 }
1749} 1728}
1750 1729
1751
1752static int __ffs_data_do_entity(enum ffs_entity_type type, 1730static int __ffs_data_do_entity(enum ffs_entity_type type,
1753 u8 *valuep, struct usb_descriptor_header *desc, 1731 u8 *valuep, struct usb_descriptor_header *desc,
1754 void *priv) 1732 void *priv)
@@ -1762,16 +1740,20 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
1762 break; 1740 break;
1763 1741
1764 case FFS_INTERFACE: 1742 case FFS_INTERFACE:
1765 /* Interfaces are indexed from zero so if we 1743 /*
1744 * Interfaces are indexed from zero so if we
1766 * encountered interface "n" then there are at least 1745 * encountered interface "n" then there are at least
1767 * "n+1" interfaces. */ 1746 * "n+1" interfaces.
1747 */
1768 if (*valuep >= ffs->interfaces_count) 1748 if (*valuep >= ffs->interfaces_count)
1769 ffs->interfaces_count = *valuep + 1; 1749 ffs->interfaces_count = *valuep + 1;
1770 break; 1750 break;
1771 1751
1772 case FFS_STRING: 1752 case FFS_STRING:
1773 /* Strings are indexed from 1 (0 is magic ;) reserved 1753 /*
1774 * for languages list or some such) */ 1754 * Strings are indexed from 1 (0 is magic ;) reserved
1755 * for languages list or some such)
1756 */
1775 if (*valuep > ffs->strings_count) 1757 if (*valuep > ffs->strings_count)
1776 ffs->strings_count = *valuep; 1758 ffs->strings_count = *valuep;
1777 break; 1759 break;
@@ -1786,7 +1768,6 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
1786 return 0; 1768 return 0;
1787} 1769}
1788 1770
1789
1790static int __ffs_data_got_descs(struct ffs_data *ffs, 1771static int __ffs_data_got_descs(struct ffs_data *ffs,
1791 char *const _data, size_t len) 1772 char *const _data, size_t len)
1792{ 1773{
@@ -1849,8 +1830,6 @@ error:
1849 return ret; 1830 return ret;
1850} 1831}
1851 1832
1852
1853
1854static int __ffs_data_got_strings(struct ffs_data *ffs, 1833static int __ffs_data_got_strings(struct ffs_data *ffs,
1855 char *const _data, size_t len) 1834 char *const _data, size_t len)
1856{ 1835{
@@ -1876,17 +1855,17 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,
1876 if (unlikely(str_count < needed_count)) 1855 if (unlikely(str_count < needed_count))
1877 goto error; 1856 goto error;
1878 1857
1879 /* If we don't need any strings just return and free all 1858 /*
1880 * memory */ 1859 * If we don't need any strings just return and free all
1860 * memory.
1861 */
1881 if (!needed_count) { 1862 if (!needed_count) {
1882 kfree(_data); 1863 kfree(_data);
1883 return 0; 1864 return 0;
1884 } 1865 }
1885 1866
1886 /* Allocate */ 1867 /* Allocate everything in one chunk so there's less maintenance. */
1887 { 1868 {
1888 /* Allocate everything in one chunk so there's less
1889 * maintanance. */
1890 struct { 1869 struct {
1891 struct usb_gadget_strings *stringtabs[lang_count + 1]; 1870 struct usb_gadget_strings *stringtabs[lang_count + 1];
1892 struct usb_gadget_strings stringtab[lang_count]; 1871 struct usb_gadget_strings stringtab[lang_count];
@@ -1937,13 +1916,17 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,
1937 if (unlikely(length == len)) 1916 if (unlikely(length == len))
1938 goto error_free; 1917 goto error_free;
1939 1918
1940 /* user may provide more strings then we need, 1919 /*
1941 * if that's the case we simply ingore the 1920 * User may provide more strings then we need,
1942 * rest */ 1921 * if that's the case we simply ignore the
1922 * rest
1923 */
1943 if (likely(needed)) { 1924 if (likely(needed)) {
1944 /* s->id will be set while adding 1925 /*
1926 * s->id will be set while adding
1945 * function to configuration so for 1927 * function to configuration so for
1946 * now just leave garbage here. */ 1928 * now just leave garbage here.
1929 */
1947 s->s = data; 1930 s->s = data;
1948 --needed; 1931 --needed;
1949 ++s; 1932 ++s;
@@ -1977,8 +1960,6 @@ error:
1977} 1960}
1978 1961
1979 1962
1980
1981
1982/* Events handling and management *******************************************/ 1963/* Events handling and management *******************************************/
1983 1964
1984static void __ffs_event_add(struct ffs_data *ffs, 1965static void __ffs_event_add(struct ffs_data *ffs,
@@ -1987,29 +1968,32 @@ static void __ffs_event_add(struct ffs_data *ffs,
1987 enum usb_functionfs_event_type rem_type1, rem_type2 = type; 1968 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1988 int neg = 0; 1969 int neg = 0;
1989 1970
1990 /* Abort any unhandled setup */ 1971 /*
1991 /* We do not need to worry about some cmpxchg() changing value 1972 * Abort any unhandled setup
1973 *
1974 * We do not need to worry about some cmpxchg() changing value
1992 * of ffs->setup_state without holding the lock because when 1975 * of ffs->setup_state without holding the lock because when
1993 * state is FFS_SETUP_PENDING cmpxchg() in several places in 1976 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1994 * the source does nothing. */ 1977 * the source does nothing.
1978 */
1995 if (ffs->setup_state == FFS_SETUP_PENDING) 1979 if (ffs->setup_state == FFS_SETUP_PENDING)
1996 ffs->setup_state = FFS_SETUP_CANCELED; 1980 ffs->setup_state = FFS_SETUP_CANCELED;
1997 1981
1998 switch (type) { 1982 switch (type) {
1999 case FUNCTIONFS_RESUME: 1983 case FUNCTIONFS_RESUME:
2000 rem_type2 = FUNCTIONFS_SUSPEND; 1984 rem_type2 = FUNCTIONFS_SUSPEND;
2001 /* FALL THGOUTH */ 1985 /* FALL THROUGH */
2002 case FUNCTIONFS_SUSPEND: 1986 case FUNCTIONFS_SUSPEND:
2003 case FUNCTIONFS_SETUP: 1987 case FUNCTIONFS_SETUP:
2004 rem_type1 = type; 1988 rem_type1 = type;
2005 /* discard all similar events */ 1989 /* Discard all similar events */
2006 break; 1990 break;
2007 1991
2008 case FUNCTIONFS_BIND: 1992 case FUNCTIONFS_BIND:
2009 case FUNCTIONFS_UNBIND: 1993 case FUNCTIONFS_UNBIND:
2010 case FUNCTIONFS_DISABLE: 1994 case FUNCTIONFS_DISABLE:
2011 case FUNCTIONFS_ENABLE: 1995 case FUNCTIONFS_ENABLE:
2012 /* discard everything other then power management. */ 1996 /* Discard everything other then power management. */
2013 rem_type1 = FUNCTIONFS_SUSPEND; 1997 rem_type1 = FUNCTIONFS_SUSPEND;
2014 rem_type2 = FUNCTIONFS_RESUME; 1998 rem_type2 = FUNCTIONFS_RESUME;
2015 neg = 1; 1999 neg = 1;
@@ -2026,11 +2010,11 @@ static void __ffs_event_add(struct ffs_data *ffs,
2026 if ((*ev == rem_type1 || *ev == rem_type2) == neg) 2010 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2027 *out++ = *ev; 2011 *out++ = *ev;
2028 else 2012 else
2029 FVDBG("purging event %d", *ev); 2013 pr_vdebug("purging event %d\n", *ev);
2030 ffs->ev.count = out - ffs->ev.types; 2014 ffs->ev.count = out - ffs->ev.types;
2031 } 2015 }
2032 2016
2033 FVDBG("adding event %d", type); 2017 pr_vdebug("adding event %d\n", type);
2034 ffs->ev.types[ffs->ev.count++] = type; 2018 ffs->ev.types[ffs->ev.count++] = type;
2035 wake_up_locked(&ffs->ev.waitq); 2019 wake_up_locked(&ffs->ev.waitq);
2036} 2020}
@@ -2055,8 +2039,10 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2055 struct ffs_function *func = priv; 2039 struct ffs_function *func = priv;
2056 struct ffs_ep *ffs_ep; 2040 struct ffs_ep *ffs_ep;
2057 2041
2058 /* If hs_descriptors is not NULL then we are reading hs 2042 /*
2059 * descriptors now */ 2043 * If hs_descriptors is not NULL then we are reading hs
2044 * descriptors now
2045 */
2060 const int isHS = func->function.hs_descriptors != NULL; 2046 const int isHS = func->function.hs_descriptors != NULL;
2061 unsigned idx; 2047 unsigned idx;
2062 2048
@@ -2075,9 +2061,9 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2075 ffs_ep = func->eps + idx; 2061 ffs_ep = func->eps + idx;
2076 2062
2077 if (unlikely(ffs_ep->descs[isHS])) { 2063 if (unlikely(ffs_ep->descs[isHS])) {
2078 FVDBG("two %sspeed descriptors for EP %d", 2064 pr_vdebug("two %sspeed descriptors for EP %d\n",
2079 isHS ? "high" : "full", 2065 isHS ? "high" : "full",
2080 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 2066 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2081 return -EINVAL; 2067 return -EINVAL;
2082 } 2068 }
2083 ffs_ep->descs[isHS] = ds; 2069 ffs_ep->descs[isHS] = ds;
@@ -2091,11 +2077,11 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2091 struct usb_request *req; 2077 struct usb_request *req;
2092 struct usb_ep *ep; 2078 struct usb_ep *ep;
2093 2079
2094 FVDBG("autoconfig"); 2080 pr_vdebug("autoconfig\n");
2095 ep = usb_ep_autoconfig(func->gadget, ds); 2081 ep = usb_ep_autoconfig(func->gadget, ds);
2096 if (unlikely(!ep)) 2082 if (unlikely(!ep))
2097 return -ENOTSUPP; 2083 return -ENOTSUPP;
2098 ep->driver_data = func->eps + idx;; 2084 ep->driver_data = func->eps + idx;
2099 2085
2100 req = usb_ep_alloc_request(ep, GFP_KERNEL); 2086 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2101 if (unlikely(!req)) 2087 if (unlikely(!req))
@@ -2111,7 +2097,6 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2111 return 0; 2097 return 0;
2112} 2098}
2113 2099
2114
2115static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, 2100static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2116 struct usb_descriptor_header *desc, 2101 struct usb_descriptor_header *desc,
2117 void *priv) 2102 void *priv)
@@ -2143,8 +2128,10 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2143 break; 2128 break;
2144 2129
2145 case FFS_ENDPOINT: 2130 case FFS_ENDPOINT:
2146 /* USB_DT_ENDPOINT are handled in 2131 /*
2147 * __ffs_func_bind_do_descs(). */ 2132 * USB_DT_ENDPOINT are handled in
2133 * __ffs_func_bind_do_descs().
2134 */
2148 if (desc->bDescriptorType == USB_DT_ENDPOINT) 2135 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2149 return 0; 2136 return 0;
2150 2137
@@ -2160,7 +2147,7 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2160 break; 2147 break;
2161 } 2148 }
2162 2149
2163 FVDBG("%02x -> %02x", *valuep, newValue); 2150 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2164 *valuep = newValue; 2151 *valuep = newValue;
2165 return 0; 2152 return 0;
2166} 2153}
@@ -2211,9 +2198,11 @@ static int ffs_func_bind(struct usb_configuration *c,
2211 func->eps = data->eps; 2198 func->eps = data->eps;
2212 func->interfaces_nums = data->inums; 2199 func->interfaces_nums = data->inums;
2213 2200
2214 /* Go throught all the endpoint descriptors and allocate 2201 /*
2202 * Go through all the endpoint descriptors and allocate
2215 * endpoints first, so that later we can rewrite the endpoint 2203 * endpoints first, so that later we can rewrite the endpoint
2216 * numbers without worying that it may be described later on. */ 2204 * numbers without worrying that it may be described later on.
2205 */
2217 if (likely(full)) { 2206 if (likely(full)) {
2218 func->function.descriptors = data->fs_descs; 2207 func->function.descriptors = data->fs_descs;
2219 ret = ffs_do_descs(ffs->fs_descs_count, 2208 ret = ffs_do_descs(ffs->fs_descs_count,
@@ -2234,9 +2223,11 @@ static int ffs_func_bind(struct usb_configuration *c,
2234 __ffs_func_bind_do_descs, func); 2223 __ffs_func_bind_do_descs, func);
2235 } 2224 }
2236 2225
2237 /* Now handle interface numbers allocation and interface and 2226 /*
2238 * enpoint numbers rewritting. We can do that in one go 2227 * Now handle interface numbers allocation and interface and
2239 * now. */ 2228 * endpoint numbers rewriting. We can do that in one go
2229 * now.
2230 */
2240 ret = ffs_do_descs(ffs->fs_descs_count + 2231 ret = ffs_do_descs(ffs->fs_descs_count +
2241 (high ? ffs->hs_descs_count : 0), 2232 (high ? ffs->hs_descs_count : 0),
2242 data->raw_descs, sizeof data->raw_descs, 2233 data->raw_descs, sizeof data->raw_descs,
@@ -2274,7 +2265,6 @@ static void ffs_func_unbind(struct usb_configuration *c,
2274 ffs_func_free(func); 2265 ffs_func_free(func);
2275} 2266}
2276 2267
2277
2278static int ffs_func_set_alt(struct usb_function *f, 2268static int ffs_func_set_alt(struct usb_function *f,
2279 unsigned interface, unsigned alt) 2269 unsigned interface, unsigned alt)
2280{ 2270{
@@ -2322,20 +2312,21 @@ static int ffs_func_setup(struct usb_function *f,
2322 2312
2323 ENTER(); 2313 ENTER();
2324 2314
2325 FVDBG("creq->bRequestType = %02x", creq->bRequestType); 2315 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2326 FVDBG("creq->bRequest = %02x", creq->bRequest); 2316 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2327 FVDBG("creq->wValue = %04x", le16_to_cpu(creq->wValue)); 2317 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2328 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq->wIndex)); 2318 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2329 FVDBG("creq->wLength = %04x", le16_to_cpu(creq->wLength)); 2319 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2330 2320
2331 /* Most requests directed to interface go throught here 2321 /*
2322 * Most requests directed to interface go through here
2332 * (notable exceptions are set/get interface) so we need to 2323 * (notable exceptions are set/get interface) so we need to
2333 * handle them. All other either handled by composite or 2324 * handle them. All other either handled by composite or
2334 * passed to usb_configuration->setup() (if one is set). No 2325 * passed to usb_configuration->setup() (if one is set). No
2335 * matter, we will handle requests directed to endpoint here 2326 * matter, we will handle requests directed to endpoint here
2336 * as well (as it's straightforward) but what to do with any 2327 * as well (as it's straightforward) but what to do with any
2337 * other request? */ 2328 * other request?
2338 2329 */
2339 if (ffs->state != FFS_ACTIVE) 2330 if (ffs->state != FFS_ACTIVE)
2340 return -ENODEV; 2331 return -ENODEV;
2341 2332
@@ -2378,8 +2369,7 @@ static void ffs_func_resume(struct usb_function *f)
2378} 2369}
2379 2370
2380 2371
2381 2372/* Endpoint and interface numbers reverse mapping ***************************/
2382/* Enpoint and interface numbers reverse mapping ****************************/
2383 2373
2384static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) 2374static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2385{ 2375{
@@ -2410,7 +2400,6 @@ static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2410 : mutex_lock_interruptible(mutex); 2400 : mutex_lock_interruptible(mutex);
2411} 2401}
2412 2402
2413
2414static char *ffs_prepare_buffer(const char * __user buf, size_t len) 2403static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2415{ 2404{
2416 char *data; 2405 char *data;
@@ -2427,7 +2416,7 @@ static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2427 return ERR_PTR(-EFAULT); 2416 return ERR_PTR(-EFAULT);
2428 } 2417 }
2429 2418
2430 FVDBG("Buffer from user space:"); 2419 pr_vdebug("Buffer from user space:\n");
2431 ffs_dump_mem("", data, len); 2420 ffs_dump_mem("", data, len);
2432 2421
2433 return data; 2422 return data;
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 838286b1cd14..b5dbb2308f56 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
@@ -185,7 +184,6 @@
185 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>. 184 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
186 */ 185 */
187 186
188
189/* 187/*
190 * Driver Design 188 * Driver Design
191 * 189 *
@@ -275,7 +273,6 @@
275/* #define VERBOSE_DEBUG */ 273/* #define VERBOSE_DEBUG */
276/* #define DUMP_MSGS */ 274/* #define DUMP_MSGS */
277 275
278
279#include <linux/blkdev.h> 276#include <linux/blkdev.h>
280#include <linux/completion.h> 277#include <linux/completion.h>
281#include <linux/dcache.h> 278#include <linux/dcache.h>
@@ -300,7 +297,6 @@
300#include "gadget_chips.h" 297#include "gadget_chips.h"
301 298
302 299
303
304/*------------------------------------------------------------------------*/ 300/*------------------------------------------------------------------------*/
305 301
306#define FSG_DRIVER_DESC "Mass Storage Function" 302#define FSG_DRIVER_DESC "Mass Storage Function"
@@ -308,7 +304,6 @@
308 304
309static const char fsg_string_interface[] = "Mass Storage"; 305static const char fsg_string_interface[] = "Mass Storage";
310 306
311
312#define FSG_NO_INTR_EP 1 307#define FSG_NO_INTR_EP 1
313#define FSG_NO_DEVICE_STRINGS 1 308#define FSG_NO_DEVICE_STRINGS 1
314#define FSG_NO_OTG 1 309#define FSG_NO_OTG 1
@@ -324,25 +319,30 @@ struct fsg_common;
324 319
325/* FSF callback functions */ 320/* FSF callback functions */
326struct fsg_operations { 321struct fsg_operations {
327 /* Callback function to call when thread exits. If no 322 /*
323 * Callback function to call when thread exits. If no
328 * callback is set or it returns value lower then zero MSF 324 * callback is set or it returns value lower then zero MSF
329 * will force eject all LUNs it operates on (including those 325 * will force eject all LUNs it operates on (including those
330 * marked as non-removable or with prevent_medium_removal flag 326 * marked as non-removable or with prevent_medium_removal flag
331 * set). */ 327 * set).
328 */
332 int (*thread_exits)(struct fsg_common *common); 329 int (*thread_exits)(struct fsg_common *common);
333 330
334 /* Called prior to ejection. Negative return means error, 331 /*
332 * Called prior to ejection. Negative return means error,
335 * zero means to continue with ejection, positive means not to 333 * zero means to continue with ejection, positive means not to
336 * eject. */ 334 * eject.
335 */
337 int (*pre_eject)(struct fsg_common *common, 336 int (*pre_eject)(struct fsg_common *common,
338 struct fsg_lun *lun, int num); 337 struct fsg_lun *lun, int num);
339 /* Called after ejection. Negative return means error, zero 338 /*
340 * or positive is just a success. */ 339 * Called after ejection. Negative return means error, zero
340 * or positive is just a success.
341 */
341 int (*post_eject)(struct fsg_common *common, 342 int (*post_eject)(struct fsg_common *common,
342 struct fsg_lun *lun, int num); 343 struct fsg_lun *lun, int num);
343}; 344};
344 345
345
346/* Data shared by all the FSG instances. */ 346/* Data shared by all the FSG instances. */
347struct fsg_common { 347struct fsg_common {
348 struct usb_gadget *gadget; 348 struct usb_gadget *gadget;
@@ -398,14 +398,15 @@ struct fsg_common {
398 /* Gadget's private data. */ 398 /* Gadget's private data. */
399 void *private_data; 399 void *private_data;
400 400
401 /* Vendor (8 chars), product (16 chars), release (4 401 /*
402 * hexadecimal digits) and NUL byte */ 402 * Vendor (8 chars), product (16 chars), release (4
403 * hexadecimal digits) and NUL byte
404 */
403 char inquiry_string[8 + 16 + 4 + 1]; 405 char inquiry_string[8 + 16 + 4 + 1];
404 406
405 struct kref ref; 407 struct kref ref;
406}; 408};
407 409
408
409struct fsg_config { 410struct fsg_config {
410 unsigned nluns; 411 unsigned nluns;
411 struct fsg_lun_config { 412 struct fsg_lun_config {
@@ -431,7 +432,6 @@ struct fsg_config {
431 char can_stall; 432 char can_stall;
432}; 433};
433 434
434
435struct fsg_dev { 435struct fsg_dev {
436 struct usb_function function; 436 struct usb_function function;
437 struct usb_gadget *gadget; /* Copy of cdev->gadget */ 437 struct usb_gadget *gadget; /* Copy of cdev->gadget */
@@ -449,7 +449,6 @@ struct fsg_dev {
449 struct usb_ep *bulk_out; 449 struct usb_ep *bulk_out;
450}; 450};
451 451
452
453static inline int __fsg_is_set(struct fsg_common *common, 452static inline int __fsg_is_set(struct fsg_common *common,
454 const char *func, unsigned line) 453 const char *func, unsigned line)
455{ 454{
@@ -462,13 +461,11 @@ static inline int __fsg_is_set(struct fsg_common *common,
462 461
463#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) 462#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
464 463
465
466static inline struct fsg_dev *fsg_from_func(struct usb_function *f) 464static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
467{ 465{
468 return container_of(f, struct fsg_dev, function); 466 return container_of(f, struct fsg_dev, function);
469} 467}
470 468
471
472typedef void (*fsg_routine_t)(struct fsg_dev *); 469typedef void (*fsg_routine_t)(struct fsg_dev *);
473 470
474static int exception_in_progress(struct fsg_common *common) 471static int exception_in_progress(struct fsg_common *common)
@@ -478,7 +475,7 @@ static int exception_in_progress(struct fsg_common *common)
478 475
479/* Make bulk-out requests be divisible by the maxpacket size */ 476/* Make bulk-out requests be divisible by the maxpacket size */
480static void set_bulk_out_req_length(struct fsg_common *common, 477static void set_bulk_out_req_length(struct fsg_common *common,
481 struct fsg_buffhd *bh, unsigned int length) 478 struct fsg_buffhd *bh, unsigned int length)
482{ 479{
483 unsigned int rem; 480 unsigned int rem;
484 481
@@ -489,6 +486,7 @@ static void set_bulk_out_req_length(struct fsg_common *common,
489 bh->outreq->length = length; 486 bh->outreq->length = length;
490} 487}
491 488
489
492/*-------------------------------------------------------------------------*/ 490/*-------------------------------------------------------------------------*/
493 491
494static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) 492static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
@@ -519,14 +517,15 @@ static void wakeup_thread(struct fsg_common *common)
519 wake_up_process(common->thread_task); 517 wake_up_process(common->thread_task);
520} 518}
521 519
522
523static void raise_exception(struct fsg_common *common, enum fsg_state new_state) 520static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
524{ 521{
525 unsigned long flags; 522 unsigned long flags;
526 523
527 /* Do nothing if a higher-priority exception is already in progress. 524 /*
525 * Do nothing if a higher-priority exception is already in progress.
528 * If a lower-or-equal priority exception is in progress, preempt it 526 * If a lower-or-equal priority exception is in progress, preempt it
529 * and notify the main thread by sending it a signal. */ 527 * and notify the main thread by sending it a signal.
528 */
530 spin_lock_irqsave(&common->lock, flags); 529 spin_lock_irqsave(&common->lock, flags);
531 if (common->state <= new_state) { 530 if (common->state <= new_state) {
532 common->exception_req_tag = common->ep0_req_tag; 531 common->exception_req_tag = common->ep0_req_tag;
@@ -555,10 +554,10 @@ static int ep0_queue(struct fsg_common *common)
555 return rc; 554 return rc;
556} 555}
557 556
557
558/*-------------------------------------------------------------------------*/ 558/*-------------------------------------------------------------------------*/
559 559
560/* Bulk and interrupt endpoint completion handlers. 560/* Completion handlers. These always run in_irq. */
561 * These always run in_irq. */
562 561
563static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req) 562static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
564{ 563{
@@ -567,7 +566,7 @@ static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
567 566
568 if (req->status || req->actual != req->length) 567 if (req->status || req->actual != req->length)
569 DBG(common, "%s --> %d, %u/%u\n", __func__, 568 DBG(common, "%s --> %d, %u/%u\n", __func__,
570 req->status, req->actual, req->length); 569 req->status, req->actual, req->length);
571 if (req->status == -ECONNRESET) /* Request was cancelled */ 570 if (req->status == -ECONNRESET) /* Request was cancelled */
572 usb_ep_fifo_flush(ep); 571 usb_ep_fifo_flush(ep);
573 572
@@ -588,8 +587,7 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
588 dump_msg(common, "bulk-out", req->buf, req->actual); 587 dump_msg(common, "bulk-out", req->buf, req->actual);
589 if (req->status || req->actual != bh->bulk_out_intended_length) 588 if (req->status || req->actual != bh->bulk_out_intended_length)
590 DBG(common, "%s --> %d, %u/%u\n", __func__, 589 DBG(common, "%s --> %d, %u/%u\n", __func__,
591 req->status, req->actual, 590 req->status, req->actual, bh->bulk_out_intended_length);
592 bh->bulk_out_intended_length);
593 if (req->status == -ECONNRESET) /* Request was cancelled */ 591 if (req->status == -ECONNRESET) /* Request was cancelled */
594 usb_ep_fifo_flush(ep); 592 usb_ep_fifo_flush(ep);
595 593
@@ -602,13 +600,8 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
602 spin_unlock(&common->lock); 600 spin_unlock(&common->lock);
603} 601}
604 602
605
606/*-------------------------------------------------------------------------*/
607
608/* Ep0 class-specific handlers. These always run in_irq. */
609
610static int fsg_setup(struct usb_function *f, 603static int fsg_setup(struct usb_function *f,
611 const struct usb_ctrlrequest *ctrl) 604 const struct usb_ctrlrequest *ctrl)
612{ 605{
613 struct fsg_dev *fsg = fsg_from_func(f); 606 struct fsg_dev *fsg = fsg_from_func(f);
614 struct usb_request *req = fsg->common->ep0req; 607 struct usb_request *req = fsg->common->ep0req;
@@ -628,8 +621,10 @@ static int fsg_setup(struct usb_function *f,
628 if (w_index != fsg->interface_number || w_value != 0) 621 if (w_index != fsg->interface_number || w_value != 0)
629 return -EDOM; 622 return -EDOM;
630 623
631 /* Raise an exception to stop the current operation 624 /*
632 * and reinitialize our state. */ 625 * Raise an exception to stop the current operation
626 * and reinitialize our state.
627 */
633 DBG(fsg, "bulk reset request\n"); 628 DBG(fsg, "bulk reset request\n");
634 raise_exception(fsg->common, FSG_STATE_RESET); 629 raise_exception(fsg->common, FSG_STATE_RESET);
635 return DELAYED_STATUS; 630 return DELAYED_STATUS;
@@ -641,7 +636,7 @@ static int fsg_setup(struct usb_function *f,
641 if (w_index != fsg->interface_number || w_value != 0) 636 if (w_index != fsg->interface_number || w_value != 0)
642 return -EDOM; 637 return -EDOM;
643 VDBG(fsg, "get max LUN\n"); 638 VDBG(fsg, "get max LUN\n");
644 *(u8 *) req->buf = fsg->common->nluns - 1; 639 *(u8 *)req->buf = fsg->common->nluns - 1;
645 640
646 /* Respond with data/status */ 641 /* Respond with data/status */
647 req->length = min((u16)1, w_length); 642 req->length = min((u16)1, w_length);
@@ -649,8 +644,7 @@ static int fsg_setup(struct usb_function *f,
649 } 644 }
650 645
651 VDBG(fsg, 646 VDBG(fsg,
652 "unknown class-specific control req " 647 "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",
653 "%02x.%02x v%04x i%04x l%u\n",
654 ctrl->bRequestType, ctrl->bRequest, 648 ctrl->bRequestType, ctrl->bRequest,
655 le16_to_cpu(ctrl->wValue), w_index, w_length); 649 le16_to_cpu(ctrl->wValue), w_index, w_length);
656 return -EOPNOTSUPP; 650 return -EOPNOTSUPP;
@@ -661,11 +655,10 @@ static int fsg_setup(struct usb_function *f,
661 655
662/* All the following routines run in process context */ 656/* All the following routines run in process context */
663 657
664
665/* Use this for bulk or interrupt transfers, not ep0 */ 658/* Use this for bulk or interrupt transfers, not ep0 */
666static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, 659static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
667 struct usb_request *req, int *pbusy, 660 struct usb_request *req, int *pbusy,
668 enum fsg_buffer_state *state) 661 enum fsg_buffer_state *state)
669{ 662{
670 int rc; 663 int rc;
671 664
@@ -683,25 +676,34 @@ static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
683 676
684 /* We can't do much more than wait for a reset */ 677 /* We can't do much more than wait for a reset */
685 678
686 /* Note: currently the net2280 driver fails zero-length 679 /*
687 * submissions if DMA is enabled. */ 680 * Note: currently the net2280 driver fails zero-length
688 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP && 681 * submissions if DMA is enabled.
689 req->length == 0)) 682 */
683 if (rc != -ESHUTDOWN &&
684 !(rc == -EOPNOTSUPP && req->length == 0))
690 WARNING(fsg, "error in submission: %s --> %d\n", 685 WARNING(fsg, "error in submission: %s --> %d\n",
691 ep->name, rc); 686 ep->name, rc);
692 } 687 }
693} 688}
694 689
695#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \ 690static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
696 if (fsg_is_set(common)) \ 691{
697 start_transfer((common)->fsg, (common)->fsg->ep_name, \ 692 if (!fsg_is_set(common))
698 req, pbusy, state); \ 693 return false;
699 else 694 start_transfer(common->fsg, common->fsg->bulk_in,
700 695 bh->inreq, &bh->inreq_busy, &bh->state);
701#define START_TRANSFER(common, ep_name, req, pbusy, state) \ 696 return true;
702 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0 697}
703
704 698
699static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
700{
701 if (!fsg_is_set(common))
702 return false;
703 start_transfer(common->fsg, common->fsg->bulk_out,
704 bh->outreq, &bh->outreq_busy, &bh->state);
705 return true;
706}
705 707
706static int sleep_thread(struct fsg_common *common) 708static int sleep_thread(struct fsg_common *common)
707{ 709{
@@ -739,16 +741,20 @@ static int do_read(struct fsg_common *common)
739 unsigned int partial_page; 741 unsigned int partial_page;
740 ssize_t nread; 742 ssize_t nread;
741 743
742 /* Get the starting Logical Block Address and check that it's 744 /*
743 * not too big */ 745 * Get the starting Logical Block Address and check that it's
746 * not too big.
747 */
744 if (common->cmnd[0] == READ_6) 748 if (common->cmnd[0] == READ_6)
745 lba = get_unaligned_be24(&common->cmnd[1]); 749 lba = get_unaligned_be24(&common->cmnd[1]);
746 else { 750 else {
747 lba = get_unaligned_be32(&common->cmnd[2]); 751 lba = get_unaligned_be32(&common->cmnd[2]);
748 752
749 /* We allow DPO (Disable Page Out = don't save data in the 753 /*
754 * We allow DPO (Disable Page Out = don't save data in the
750 * cache) and FUA (Force Unit Access = don't read from the 755 * cache) and FUA (Force Unit Access = don't read from the
751 * cache), but we don't implement them. */ 756 * cache), but we don't implement them.
757 */
752 if ((common->cmnd[1] & ~0x18) != 0) { 758 if ((common->cmnd[1] & ~0x18) != 0) {
753 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 759 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
754 return -EINVAL; 760 return -EINVAL;
@@ -766,22 +772,23 @@ static int do_read(struct fsg_common *common)
766 return -EIO; /* No default reply */ 772 return -EIO; /* No default reply */
767 773
768 for (;;) { 774 for (;;) {
769 775 /*
770 /* Figure out how much we need to read: 776 * Figure out how much we need to read:
771 * Try to read the remaining amount. 777 * Try to read the remaining amount.
772 * But don't read more than the buffer size. 778 * But don't read more than the buffer size.
773 * And don't try to read past the end of the file. 779 * And don't try to read past the end of the file.
774 * Finally, if we're not at a page boundary, don't read past 780 * Finally, if we're not at a page boundary, don't read past
775 * the next page. 781 * the next page.
776 * If this means reading 0 then we were asked to read past 782 * If this means reading 0 then we were asked to read past
777 * the end of file. */ 783 * the end of file.
784 */
778 amount = min(amount_left, FSG_BUFLEN); 785 amount = min(amount_left, FSG_BUFLEN);
779 amount = min((loff_t) amount, 786 amount = min((loff_t)amount,
780 curlun->file_length - file_offset); 787 curlun->file_length - file_offset);
781 partial_page = file_offset & (PAGE_CACHE_SIZE - 1); 788 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
782 if (partial_page > 0) 789 if (partial_page > 0)
783 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE - 790 amount = min(amount, (unsigned int)PAGE_CACHE_SIZE -
784 partial_page); 791 partial_page);
785 792
786 /* Wait for the next buffer to become available */ 793 /* Wait for the next buffer to become available */
787 bh = common->next_buffhd_to_fill; 794 bh = common->next_buffhd_to_fill;
@@ -791,8 +798,10 @@ static int do_read(struct fsg_common *common)
791 return rc; 798 return rc;
792 } 799 }
793 800
794 /* If we were asked to read past the end of file, 801 /*
795 * end with an empty buffer. */ 802 * If we were asked to read past the end of file,
803 * end with an empty buffer.
804 */
796 if (amount == 0) { 805 if (amount == 0) {
797 curlun->sense_data = 806 curlun->sense_data =
798 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 807 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
@@ -806,21 +815,19 @@ static int do_read(struct fsg_common *common)
806 /* Perform the read */ 815 /* Perform the read */
807 file_offset_tmp = file_offset; 816 file_offset_tmp = file_offset;
808 nread = vfs_read(curlun->filp, 817 nread = vfs_read(curlun->filp,
809 (char __user *) bh->buf, 818 (char __user *)bh->buf,
810 amount, &file_offset_tmp); 819 amount, &file_offset_tmp);
811 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount, 820 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
812 (unsigned long long) file_offset, 821 (unsigned long long)file_offset, (int)nread);
813 (int) nread);
814 if (signal_pending(current)) 822 if (signal_pending(current))
815 return -EINTR; 823 return -EINTR;
816 824
817 if (nread < 0) { 825 if (nread < 0) {
818 LDBG(curlun, "error in file read: %d\n", 826 LDBG(curlun, "error in file read: %d\n", (int)nread);
819 (int) nread);
820 nread = 0; 827 nread = 0;
821 } else if (nread < amount) { 828 } else if (nread < amount) {
822 LDBG(curlun, "partial file read: %d/%u\n", 829 LDBG(curlun, "partial file read: %d/%u\n",
823 (int) nread, amount); 830 (int)nread, amount);
824 nread -= (nread & 511); /* Round down to a block */ 831 nread -= (nread & 511); /* Round down to a block */
825 } 832 }
826 file_offset += nread; 833 file_offset += nread;
@@ -842,10 +849,8 @@ static int do_read(struct fsg_common *common)
842 849
843 /* Send this buffer and go read some more */ 850 /* Send this buffer and go read some more */
844 bh->inreq->zero = 0; 851 bh->inreq->zero = 0;
845 START_TRANSFER_OR(common, bulk_in, bh->inreq, 852 if (!start_in_transfer(common, bh))
846 &bh->inreq_busy, &bh->state) 853 /* Don't know what to do if common->fsg is NULL */
847 /* Don't know what to do if
848 * common->fsg is NULL */
849 return -EIO; 854 return -EIO;
850 common->next_buffhd_to_fill = bh->next; 855 common->next_buffhd_to_fill = bh->next;
851 } 856 }
@@ -877,17 +882,21 @@ static int do_write(struct fsg_common *common)
877 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */ 882 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
878 spin_unlock(&curlun->filp->f_lock); 883 spin_unlock(&curlun->filp->f_lock);
879 884
880 /* Get the starting Logical Block Address and check that it's 885 /*
881 * not too big */ 886 * Get the starting Logical Block Address and check that it's
887 * not too big
888 */
882 if (common->cmnd[0] == WRITE_6) 889 if (common->cmnd[0] == WRITE_6)
883 lba = get_unaligned_be24(&common->cmnd[1]); 890 lba = get_unaligned_be24(&common->cmnd[1]);
884 else { 891 else {
885 lba = get_unaligned_be32(&common->cmnd[2]); 892 lba = get_unaligned_be32(&common->cmnd[2]);
886 893
887 /* We allow DPO (Disable Page Out = don't save data in the 894 /*
895 * We allow DPO (Disable Page Out = don't save data in the
888 * cache) and FUA (Force Unit Access = write directly to the 896 * cache) and FUA (Force Unit Access = write directly to the
889 * medium). We don't implement DPO; we implement FUA by 897 * medium). We don't implement DPO; we implement FUA by
890 * performing synchronous output. */ 898 * performing synchronous output.
899 */
891 if (common->cmnd[1] & ~0x18) { 900 if (common->cmnd[1] & ~0x18) {
892 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 901 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
893 return -EINVAL; 902 return -EINVAL;
@@ -915,7 +924,8 @@ static int do_write(struct fsg_common *common)
915 bh = common->next_buffhd_to_fill; 924 bh = common->next_buffhd_to_fill;
916 if (bh->state == BUF_STATE_EMPTY && get_some_more) { 925 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
917 926
918 /* Figure out how much we want to get: 927 /*
928 * Figure out how much we want to get:
919 * Try to get the remaining amount. 929 * Try to get the remaining amount.
920 * But don't get more than the buffer size. 930 * But don't get more than the buffer size.
921 * And don't try to go past the end of the file. 931 * And don't try to go past the end of the file.
@@ -923,14 +933,15 @@ static int do_write(struct fsg_common *common)
923 * don't go past the next page. 933 * don't go past the next page.
924 * If this means getting 0, then we were asked 934 * If this means getting 0, then we were asked
925 * to write past the end of file. 935 * to write past the end of file.
926 * Finally, round down to a block boundary. */ 936 * Finally, round down to a block boundary.
937 */
927 amount = min(amount_left_to_req, FSG_BUFLEN); 938 amount = min(amount_left_to_req, FSG_BUFLEN);
928 amount = min((loff_t) amount, curlun->file_length - 939 amount = min((loff_t)amount,
929 usb_offset); 940 curlun->file_length - usb_offset);
930 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1); 941 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
931 if (partial_page > 0) 942 if (partial_page > 0)
932 amount = min(amount, 943 amount = min(amount,
933 (unsigned int) PAGE_CACHE_SIZE - partial_page); 944 (unsigned int)PAGE_CACHE_SIZE - partial_page);
934 945
935 if (amount == 0) { 946 if (amount == 0) {
936 get_some_more = 0; 947 get_some_more = 0;
@@ -940,11 +951,13 @@ static int do_write(struct fsg_common *common)
940 curlun->info_valid = 1; 951 curlun->info_valid = 1;
941 continue; 952 continue;
942 } 953 }
943 amount -= (amount & 511); 954 amount -= amount & 511;
944 if (amount == 0) { 955 if (amount == 0) {
945 956
946 /* Why were we were asked to transfer a 957 /*
947 * partial block? */ 958 * Why were we were asked to transfer a
959 * partial block?
960 */
948 get_some_more = 0; 961 get_some_more = 0;
949 continue; 962 continue;
950 } 963 }
@@ -956,15 +969,15 @@ static int do_write(struct fsg_common *common)
956 if (amount_left_to_req == 0) 969 if (amount_left_to_req == 0)
957 get_some_more = 0; 970 get_some_more = 0;
958 971
959 /* amount is always divisible by 512, hence by 972 /*
960 * the bulk-out maxpacket size */ 973 * amount is always divisible by 512, hence by
974 * the bulk-out maxpacket size
975 */
961 bh->outreq->length = amount; 976 bh->outreq->length = amount;
962 bh->bulk_out_intended_length = amount; 977 bh->bulk_out_intended_length = amount;
963 bh->outreq->short_not_ok = 1; 978 bh->outreq->short_not_ok = 1;
964 START_TRANSFER_OR(common, bulk_out, bh->outreq, 979 if (!start_out_transfer(common, bh))
965 &bh->outreq_busy, &bh->state) 980 /* Dunno what to do if common->fsg is NULL */
966 /* Don't know what to do if
967 * common->fsg is NULL */
968 return -EIO; 981 return -EIO;
969 common->next_buffhd_to_fill = bh->next; 982 common->next_buffhd_to_fill = bh->next;
970 continue; 983 continue;
@@ -990,30 +1003,29 @@ static int do_write(struct fsg_common *common)
990 amount = bh->outreq->actual; 1003 amount = bh->outreq->actual;
991 if (curlun->file_length - file_offset < amount) { 1004 if (curlun->file_length - file_offset < amount) {
992 LERROR(curlun, 1005 LERROR(curlun,
993 "write %u @ %llu beyond end %llu\n", 1006 "write %u @ %llu beyond end %llu\n",
994 amount, (unsigned long long) file_offset, 1007 amount, (unsigned long long)file_offset,
995 (unsigned long long) curlun->file_length); 1008 (unsigned long long)curlun->file_length);
996 amount = curlun->file_length - file_offset; 1009 amount = curlun->file_length - file_offset;
997 } 1010 }
998 1011
999 /* Perform the write */ 1012 /* Perform the write */
1000 file_offset_tmp = file_offset; 1013 file_offset_tmp = file_offset;
1001 nwritten = vfs_write(curlun->filp, 1014 nwritten = vfs_write(curlun->filp,
1002 (char __user *) bh->buf, 1015 (char __user *)bh->buf,
1003 amount, &file_offset_tmp); 1016 amount, &file_offset_tmp);
1004 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, 1017 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1005 (unsigned long long) file_offset, 1018 (unsigned long long)file_offset, (int)nwritten);
1006 (int) nwritten);
1007 if (signal_pending(current)) 1019 if (signal_pending(current))
1008 return -EINTR; /* Interrupted! */ 1020 return -EINTR; /* Interrupted! */
1009 1021
1010 if (nwritten < 0) { 1022 if (nwritten < 0) {
1011 LDBG(curlun, "error in file write: %d\n", 1023 LDBG(curlun, "error in file write: %d\n",
1012 (int) nwritten); 1024 (int)nwritten);
1013 nwritten = 0; 1025 nwritten = 0;
1014 } else if (nwritten < amount) { 1026 } else if (nwritten < amount) {
1015 LDBG(curlun, "partial file write: %d/%u\n", 1027 LDBG(curlun, "partial file write: %d/%u\n",
1016 (int) nwritten, amount); 1028 (int)nwritten, amount);
1017 nwritten -= (nwritten & 511); 1029 nwritten -= (nwritten & 511);
1018 /* Round down to a block */ 1030 /* Round down to a block */
1019 } 1031 }
@@ -1086,16 +1098,20 @@ static int do_verify(struct fsg_common *common)
1086 unsigned int amount; 1098 unsigned int amount;
1087 ssize_t nread; 1099 ssize_t nread;
1088 1100
1089 /* Get the starting Logical Block Address and check that it's 1101 /*
1090 * not too big */ 1102 * Get the starting Logical Block Address and check that it's
1103 * not too big.
1104 */
1091 lba = get_unaligned_be32(&common->cmnd[2]); 1105 lba = get_unaligned_be32(&common->cmnd[2]);
1092 if (lba >= curlun->num_sectors) { 1106 if (lba >= curlun->num_sectors) {
1093 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1107 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1094 return -EINVAL; 1108 return -EINVAL;
1095 } 1109 }
1096 1110
1097 /* We allow DPO (Disable Page Out = don't save data in the 1111 /*
1098 * cache) but we don't implement it. */ 1112 * We allow DPO (Disable Page Out = don't save data in the
1113 * cache) but we don't implement it.
1114 */
1099 if (common->cmnd[1] & ~0x10) { 1115 if (common->cmnd[1] & ~0x10) {
1100 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1116 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1101 return -EINVAL; 1117 return -EINVAL;
@@ -1120,16 +1136,17 @@ static int do_verify(struct fsg_common *common)
1120 1136
1121 /* Just try to read the requested blocks */ 1137 /* Just try to read the requested blocks */
1122 while (amount_left > 0) { 1138 while (amount_left > 0) {
1123 1139 /*
1124 /* Figure out how much we need to read: 1140 * Figure out how much we need to read:
1125 * Try to read the remaining amount, but not more than 1141 * Try to read the remaining amount, but not more than
1126 * the buffer size. 1142 * the buffer size.
1127 * And don't try to read past the end of the file. 1143 * And don't try to read past the end of the file.
1128 * If this means reading 0 then we were asked to read 1144 * If this means reading 0 then we were asked to read
1129 * past the end of file. */ 1145 * past the end of file.
1146 */
1130 amount = min(amount_left, FSG_BUFLEN); 1147 amount = min(amount_left, FSG_BUFLEN);
1131 amount = min((loff_t) amount, 1148 amount = min((loff_t)amount,
1132 curlun->file_length - file_offset); 1149 curlun->file_length - file_offset);
1133 if (amount == 0) { 1150 if (amount == 0) {
1134 curlun->sense_data = 1151 curlun->sense_data =
1135 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1152 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
@@ -1150,13 +1167,12 @@ static int do_verify(struct fsg_common *common)
1150 return -EINTR; 1167 return -EINTR;
1151 1168
1152 if (nread < 0) { 1169 if (nread < 0) {
1153 LDBG(curlun, "error in file verify: %d\n", 1170 LDBG(curlun, "error in file verify: %d\n", (int)nread);
1154 (int) nread);
1155 nread = 0; 1171 nread = 0;
1156 } else if (nread < amount) { 1172 } else if (nread < amount) {
1157 LDBG(curlun, "partial file verify: %d/%u\n", 1173 LDBG(curlun, "partial file verify: %d/%u\n",
1158 (int) nread, amount); 1174 (int)nread, amount);
1159 nread -= (nread & 511); /* Round down to a sector */ 1175 nread -= nread & 511; /* Round down to a sector */
1160 } 1176 }
1161 if (nread == 0) { 1177 if (nread == 0) {
1162 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1178 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
@@ -1198,7 +1214,6 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1198 return 36; 1214 return 36;
1199} 1215}
1200 1216
1201
1202static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) 1217static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1203{ 1218{
1204 struct fsg_lun *curlun = common->curlun; 1219 struct fsg_lun *curlun = common->curlun;
@@ -1252,13 +1267,12 @@ static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1252 return 18; 1267 return 18;
1253} 1268}
1254 1269
1255
1256static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) 1270static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1257{ 1271{
1258 struct fsg_lun *curlun = common->curlun; 1272 struct fsg_lun *curlun = common->curlun;
1259 u32 lba = get_unaligned_be32(&common->cmnd[2]); 1273 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1260 int pmi = common->cmnd[8]; 1274 int pmi = common->cmnd[8];
1261 u8 *buf = (u8 *) bh->buf; 1275 u8 *buf = (u8 *)bh->buf;
1262 1276
1263 /* Check the PMI and LBA fields */ 1277 /* Check the PMI and LBA fields */
1264 if (pmi > 1 || (pmi == 0 && lba != 0)) { 1278 if (pmi > 1 || (pmi == 0 && lba != 0)) {
@@ -1272,13 +1286,12 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1272 return 8; 1286 return 8;
1273} 1287}
1274 1288
1275
1276static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) 1289static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1277{ 1290{
1278 struct fsg_lun *curlun = common->curlun; 1291 struct fsg_lun *curlun = common->curlun;
1279 int msf = common->cmnd[1] & 0x02; 1292 int msf = common->cmnd[1] & 0x02;
1280 u32 lba = get_unaligned_be32(&common->cmnd[2]); 1293 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1281 u8 *buf = (u8 *) bh->buf; 1294 u8 *buf = (u8 *)bh->buf;
1282 1295
1283 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ 1296 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
1284 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1297 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
@@ -1295,13 +1308,12 @@ static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1295 return 8; 1308 return 8;
1296} 1309}
1297 1310
1298
1299static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) 1311static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1300{ 1312{
1301 struct fsg_lun *curlun = common->curlun; 1313 struct fsg_lun *curlun = common->curlun;
1302 int msf = common->cmnd[1] & 0x02; 1314 int msf = common->cmnd[1] & 0x02;
1303 int start_track = common->cmnd[6]; 1315 int start_track = common->cmnd[6];
1304 u8 *buf = (u8 *) bh->buf; 1316 u8 *buf = (u8 *)bh->buf;
1305 1317
1306 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ 1318 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1307 start_track > 1) { 1319 start_track > 1) {
@@ -1323,7 +1335,6 @@ static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1323 return 20; 1335 return 20;
1324} 1336}
1325 1337
1326
1327static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) 1338static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1328{ 1339{
1329 struct fsg_lun *curlun = common->curlun; 1340 struct fsg_lun *curlun = common->curlun;
@@ -1348,10 +1359,12 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1348 changeable_values = (pc == 1); 1359 changeable_values = (pc == 1);
1349 all_pages = (page_code == 0x3f); 1360 all_pages = (page_code == 0x3f);
1350 1361
1351 /* Write the mode parameter header. Fixed values are: default 1362 /*
1363 * Write the mode parameter header. Fixed values are: default
1352 * medium type, no cache control (DPOFUA), and no block descriptors. 1364 * medium type, no cache control (DPOFUA), and no block descriptors.
1353 * The only variable value is the WriteProtect bit. We will fill in 1365 * The only variable value is the WriteProtect bit. We will fill in
1354 * the mode data length later. */ 1366 * the mode data length later.
1367 */
1355 memset(buf, 0, 8); 1368 memset(buf, 0, 8);
1356 if (mscmnd == MODE_SENSE) { 1369 if (mscmnd == MODE_SENSE) {
1357 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */ 1370 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
@@ -1365,8 +1378,10 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1365 1378
1366 /* No block descriptors */ 1379 /* No block descriptors */
1367 1380
1368 /* The mode pages, in numerical order. The only page we support 1381 /*
1369 * is the Caching page. */ 1382 * The mode pages, in numerical order. The only page we support
1383 * is the Caching page.
1384 */
1370 if (page_code == 0x08 || all_pages) { 1385 if (page_code == 0x08 || all_pages) {
1371 valid_page = 1; 1386 valid_page = 1;
1372 buf[0] = 0x08; /* Page code */ 1387 buf[0] = 0x08; /* Page code */
@@ -1388,8 +1403,10 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1388 buf += 12; 1403 buf += 12;
1389 } 1404 }
1390 1405
1391 /* Check that a valid page was requested and the mode data length 1406 /*
1392 * isn't too long. */ 1407 * Check that a valid page was requested and the mode data length
1408 * isn't too long.
1409 */
1393 len = buf - buf0; 1410 len = buf - buf0;
1394 if (!valid_page || len > limit) { 1411 if (!valid_page || len > limit) {
1395 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1412 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
@@ -1404,7 +1421,6 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1404 return len; 1421 return len;
1405} 1422}
1406 1423
1407
1408static int do_start_stop(struct fsg_common *common) 1424static int do_start_stop(struct fsg_common *common)
1409{ 1425{
1410 struct fsg_lun *curlun = common->curlun; 1426 struct fsg_lun *curlun = common->curlun;
@@ -1424,8 +1440,10 @@ static int do_start_stop(struct fsg_common *common)
1424 loej = common->cmnd[4] & 0x02; 1440 loej = common->cmnd[4] & 0x02;
1425 start = common->cmnd[4] & 0x01; 1441 start = common->cmnd[4] & 0x01;
1426 1442
1427 /* Our emulation doesn't support mounting; the medium is 1443 /*
1428 * available for use as soon as it is loaded. */ 1444 * Our emulation doesn't support mounting; the medium is
1445 * available for use as soon as it is loaded.
1446 */
1429 if (start) { 1447 if (start) {
1430 if (!fsg_lun_is_open(curlun)) { 1448 if (!fsg_lun_is_open(curlun)) {
1431 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 1449 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
@@ -1466,7 +1484,6 @@ static int do_start_stop(struct fsg_common *common)
1466 : 0; 1484 : 0;
1467} 1485}
1468 1486
1469
1470static int do_prevent_allow(struct fsg_common *common) 1487static int do_prevent_allow(struct fsg_common *common)
1471{ 1488{
1472 struct fsg_lun *curlun = common->curlun; 1489 struct fsg_lun *curlun = common->curlun;
@@ -1491,7 +1508,6 @@ static int do_prevent_allow(struct fsg_common *common)
1491 return 0; 1508 return 0;
1492} 1509}
1493 1510
1494
1495static int do_read_format_capacities(struct fsg_common *common, 1511static int do_read_format_capacities(struct fsg_common *common,
1496 struct fsg_buffhd *bh) 1512 struct fsg_buffhd *bh)
1497{ 1513{
@@ -1509,7 +1525,6 @@ static int do_read_format_capacities(struct fsg_common *common,
1509 return 12; 1525 return 12;
1510} 1526}
1511 1527
1512
1513static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) 1528static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1514{ 1529{
1515 struct fsg_lun *curlun = common->curlun; 1530 struct fsg_lun *curlun = common->curlun;
@@ -1591,7 +1606,7 @@ static int pad_with_zeros(struct fsg_dev *fsg)
1591 bh->inreq->length = nsend; 1606 bh->inreq->length = nsend;
1592 bh->inreq->zero = 0; 1607 bh->inreq->zero = 0;
1593 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1608 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1594 &bh->inreq_busy, &bh->state); 1609 &bh->inreq_busy, &bh->state);
1595 bh = fsg->common->next_buffhd_to_fill = bh->next; 1610 bh = fsg->common->next_buffhd_to_fill = bh->next;
1596 fsg->common->usb_amount_left -= nsend; 1611 fsg->common->usb_amount_left -= nsend;
1597 nkeep = 0; 1612 nkeep = 0;
@@ -1617,7 +1632,7 @@ static int throw_away_data(struct fsg_common *common)
1617 1632
1618 /* A short packet or an error ends everything */ 1633 /* A short packet or an error ends everything */
1619 if (bh->outreq->actual != bh->outreq->length || 1634 if (bh->outreq->actual != bh->outreq->length ||
1620 bh->outreq->status != 0) { 1635 bh->outreq->status != 0) {
1621 raise_exception(common, 1636 raise_exception(common,
1622 FSG_STATE_ABORT_BULK_OUT); 1637 FSG_STATE_ABORT_BULK_OUT);
1623 return -EINTR; 1638 return -EINTR;
@@ -1631,15 +1646,15 @@ static int throw_away_data(struct fsg_common *common)
1631 && common->usb_amount_left > 0) { 1646 && common->usb_amount_left > 0) {
1632 amount = min(common->usb_amount_left, FSG_BUFLEN); 1647 amount = min(common->usb_amount_left, FSG_BUFLEN);
1633 1648
1634 /* amount is always divisible by 512, hence by 1649 /*
1635 * the bulk-out maxpacket size */ 1650 * amount is always divisible by 512, hence by
1651 * the bulk-out maxpacket size.
1652 */
1636 bh->outreq->length = amount; 1653 bh->outreq->length = amount;
1637 bh->bulk_out_intended_length = amount; 1654 bh->bulk_out_intended_length = amount;
1638 bh->outreq->short_not_ok = 1; 1655 bh->outreq->short_not_ok = 1;
1639 START_TRANSFER_OR(common, bulk_out, bh->outreq, 1656 if (!start_out_transfer(common, bh))
1640 &bh->outreq_busy, &bh->state) 1657 /* Dunno what to do if common->fsg is NULL */
1641 /* Don't know what to do if
1642 * common->fsg is NULL */
1643 return -EIO; 1658 return -EIO;
1644 common->next_buffhd_to_fill = bh->next; 1659 common->next_buffhd_to_fill = bh->next;
1645 common->usb_amount_left -= amount; 1660 common->usb_amount_left -= amount;
@@ -1654,7 +1669,6 @@ static int throw_away_data(struct fsg_common *common)
1654 return 0; 1669 return 0;
1655} 1670}
1656 1671
1657
1658static int finish_reply(struct fsg_common *common) 1672static int finish_reply(struct fsg_common *common)
1659{ 1673{
1660 struct fsg_buffhd *bh = common->next_buffhd_to_fill; 1674 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
@@ -1664,10 +1678,12 @@ static int finish_reply(struct fsg_common *common)
1664 case DATA_DIR_NONE: 1678 case DATA_DIR_NONE:
1665 break; /* Nothing to send */ 1679 break; /* Nothing to send */
1666 1680
1667 /* If we don't know whether the host wants to read or write, 1681 /*
1682 * If we don't know whether the host wants to read or write,
1668 * this must be CB or CBI with an unknown command. We mustn't 1683 * this must be CB or CBI with an unknown command. We mustn't
1669 * try to send or receive any data. So stall both bulk pipes 1684 * try to send or receive any data. So stall both bulk pipes
1670 * if we can and wait for a reset. */ 1685 * if we can and wait for a reset.
1686 */
1671 case DATA_DIR_UNKNOWN: 1687 case DATA_DIR_UNKNOWN:
1672 if (!common->can_stall) { 1688 if (!common->can_stall) {
1673 /* Nothing */ 1689 /* Nothing */
@@ -1688,18 +1704,18 @@ static int finish_reply(struct fsg_common *common)
1688 /* If there's no residue, simply send the last buffer */ 1704 /* If there's no residue, simply send the last buffer */
1689 } else if (common->residue == 0) { 1705 } else if (common->residue == 0) {
1690 bh->inreq->zero = 0; 1706 bh->inreq->zero = 0;
1691 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1707 if (!start_in_transfer(common, bh))
1692 &bh->inreq_busy, &bh->state)
1693 return -EIO; 1708 return -EIO;
1694 common->next_buffhd_to_fill = bh->next; 1709 common->next_buffhd_to_fill = bh->next;
1695 1710
1696 /* For Bulk-only, if we're allowed to stall then send the 1711 /*
1712 * For Bulk-only, if we're allowed to stall then send the
1697 * short packet and halt the bulk-in endpoint. If we can't 1713 * short packet and halt the bulk-in endpoint. If we can't
1698 * stall, pad out the remaining data with 0's. */ 1714 * stall, pad out the remaining data with 0's.
1715 */
1699 } else if (common->can_stall) { 1716 } else if (common->can_stall) {
1700 bh->inreq->zero = 1; 1717 bh->inreq->zero = 1;
1701 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1718 if (!start_in_transfer(common, bh))
1702 &bh->inreq_busy, &bh->state)
1703 /* Don't know what to do if 1719 /* Don't know what to do if
1704 * common->fsg is NULL */ 1720 * common->fsg is NULL */
1705 rc = -EIO; 1721 rc = -EIO;
@@ -1714,8 +1730,10 @@ static int finish_reply(struct fsg_common *common)
1714 } 1730 }
1715 break; 1731 break;
1716 1732
1717 /* We have processed all we want from the data the host has sent. 1733 /*
1718 * There may still be outstanding bulk-out requests. */ 1734 * We have processed all we want from the data the host has sent.
1735 * There may still be outstanding bulk-out requests.
1736 */
1719 case DATA_DIR_FROM_HOST: 1737 case DATA_DIR_FROM_HOST:
1720 if (common->residue == 0) { 1738 if (common->residue == 0) {
1721 /* Nothing to receive */ 1739 /* Nothing to receive */
@@ -1725,12 +1743,14 @@ static int finish_reply(struct fsg_common *common)
1725 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); 1743 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1726 rc = -EINTR; 1744 rc = -EINTR;
1727 1745
1728 /* We haven't processed all the incoming data. Even though 1746 /*
1747 * We haven't processed all the incoming data. Even though
1729 * we may be allowed to stall, doing so would cause a race. 1748 * we may be allowed to stall, doing so would cause a race.
1730 * The controller may already have ACK'ed all the remaining 1749 * The controller may already have ACK'ed all the remaining
1731 * bulk-out packets, in which case the host wouldn't see a 1750 * bulk-out packets, in which case the host wouldn't see a
1732 * STALL. Not realizing the endpoint was halted, it wouldn't 1751 * STALL. Not realizing the endpoint was halted, it wouldn't
1733 * clear the halt -- leading to problems later on. */ 1752 * clear the halt -- leading to problems later on.
1753 */
1734#if 0 1754#if 0
1735 } else if (common->can_stall) { 1755 } else if (common->can_stall) {
1736 if (fsg_is_set(common)) 1756 if (fsg_is_set(common))
@@ -1740,8 +1760,10 @@ static int finish_reply(struct fsg_common *common)
1740 rc = -EINTR; 1760 rc = -EINTR;
1741#endif 1761#endif
1742 1762
1743 /* We can't stall. Read in the excess data and throw it 1763 /*
1744 * all away. */ 1764 * We can't stall. Read in the excess data and throw it
1765 * all away.
1766 */
1745 } else { 1767 } else {
1746 rc = throw_away_data(common); 1768 rc = throw_away_data(common);
1747 } 1769 }
@@ -1750,7 +1772,6 @@ static int finish_reply(struct fsg_common *common)
1750 return rc; 1772 return rc;
1751} 1773}
1752 1774
1753
1754static int send_status(struct fsg_common *common) 1775static int send_status(struct fsg_common *common)
1755{ 1776{
1756 struct fsg_lun *curlun = common->curlun; 1777 struct fsg_lun *curlun = common->curlun;
@@ -1798,8 +1819,7 @@ static int send_status(struct fsg_common *common)
1798 1819
1799 bh->inreq->length = USB_BULK_CS_WRAP_LEN; 1820 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1800 bh->inreq->zero = 0; 1821 bh->inreq->zero = 0;
1801 START_TRANSFER_OR(common, bulk_in, bh->inreq, 1822 if (!start_in_transfer(common, bh))
1802 &bh->inreq_busy, &bh->state)
1803 /* Don't know what to do if common->fsg is NULL */ 1823 /* Don't know what to do if common->fsg is NULL */
1804 return -EIO; 1824 return -EIO;
1805 1825
@@ -1810,11 +1830,13 @@ static int send_status(struct fsg_common *common)
1810 1830
1811/*-------------------------------------------------------------------------*/ 1831/*-------------------------------------------------------------------------*/
1812 1832
1813/* Check whether the command is properly formed and whether its data size 1833/*
1814 * and direction agree with the values we already have. */ 1834 * Check whether the command is properly formed and whether its data size
1835 * and direction agree with the values we already have.
1836 */
1815static int check_command(struct fsg_common *common, int cmnd_size, 1837static int check_command(struct fsg_common *common, int cmnd_size,
1816 enum data_direction data_dir, unsigned int mask, 1838 enum data_direction data_dir, unsigned int mask,
1817 int needs_medium, const char *name) 1839 int needs_medium, const char *name)
1818{ 1840{
1819 int i; 1841 int i;
1820 int lun = common->cmnd[1] >> 5; 1842 int lun = common->cmnd[1] >> 5;
@@ -1825,19 +1847,23 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1825 hdlen[0] = 0; 1847 hdlen[0] = 0;
1826 if (common->data_dir != DATA_DIR_UNKNOWN) 1848 if (common->data_dir != DATA_DIR_UNKNOWN)
1827 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], 1849 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1828 common->data_size); 1850 common->data_size);
1829 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", 1851 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1830 name, cmnd_size, dirletter[(int) data_dir], 1852 name, cmnd_size, dirletter[(int) data_dir],
1831 common->data_size_from_cmnd, common->cmnd_size, hdlen); 1853 common->data_size_from_cmnd, common->cmnd_size, hdlen);
1832 1854
1833 /* We can't reply at all until we know the correct data direction 1855 /*
1834 * and size. */ 1856 * We can't reply at all until we know the correct data direction
1857 * and size.
1858 */
1835 if (common->data_size_from_cmnd == 0) 1859 if (common->data_size_from_cmnd == 0)
1836 data_dir = DATA_DIR_NONE; 1860 data_dir = DATA_DIR_NONE;
1837 if (common->data_size < common->data_size_from_cmnd) { 1861 if (common->data_size < common->data_size_from_cmnd) {
1838 /* Host data size < Device data size is a phase error. 1862 /*
1863 * Host data size < Device data size is a phase error.
1839 * Carry out the command, but only transfer as much as 1864 * Carry out the command, but only transfer as much as
1840 * we are allowed. */ 1865 * we are allowed.
1866 */
1841 common->data_size_from_cmnd = common->data_size; 1867 common->data_size_from_cmnd = common->data_size;
1842 common->phase_error = 1; 1868 common->phase_error = 1;
1843 } 1869 }
@@ -1845,8 +1871,7 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1845 common->usb_amount_left = common->data_size; 1871 common->usb_amount_left = common->data_size;
1846 1872
1847 /* Conflicting data directions is a phase error */ 1873 /* Conflicting data directions is a phase error */
1848 if (common->data_dir != data_dir 1874 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {
1849 && common->data_size_from_cmnd > 0) {
1850 common->phase_error = 1; 1875 common->phase_error = 1;
1851 return -EINVAL; 1876 return -EINVAL;
1852 } 1877 }
@@ -1854,7 +1879,8 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1854 /* Verify the length of the command itself */ 1879 /* Verify the length of the command itself */
1855 if (cmnd_size != common->cmnd_size) { 1880 if (cmnd_size != common->cmnd_size) {
1856 1881
1857 /* Special case workaround: There are plenty of buggy SCSI 1882 /*
1883 * Special case workaround: There are plenty of buggy SCSI
1858 * implementations. Many have issues with cbw->Length 1884 * implementations. Many have issues with cbw->Length
1859 * field passing a wrong command size. For those cases we 1885 * field passing a wrong command size. For those cases we
1860 * always try to work around the problem by using the length 1886 * always try to work around the problem by using the length
@@ -1896,8 +1922,10 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1896 curlun = NULL; 1922 curlun = NULL;
1897 common->bad_lun_okay = 0; 1923 common->bad_lun_okay = 0;
1898 1924
1899 /* INQUIRY and REQUEST SENSE commands are explicitly allowed 1925 /*
1900 * to use unsupported LUNs; all others may not. */ 1926 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1927 * to use unsupported LUNs; all others may not.
1928 */
1901 if (common->cmnd[0] != INQUIRY && 1929 if (common->cmnd[0] != INQUIRY &&
1902 common->cmnd[0] != REQUEST_SENSE) { 1930 common->cmnd[0] != REQUEST_SENSE) {
1903 DBG(common, "unsupported LUN %d\n", common->lun); 1931 DBG(common, "unsupported LUN %d\n", common->lun);
@@ -1905,11 +1933,13 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1905 } 1933 }
1906 } 1934 }
1907 1935
1908 /* If a unit attention condition exists, only INQUIRY and 1936 /*
1909 * REQUEST SENSE commands are allowed; anything else must fail. */ 1937 * If a unit attention condition exists, only INQUIRY and
1938 * REQUEST SENSE commands are allowed; anything else must fail.
1939 */
1910 if (curlun && curlun->unit_attention_data != SS_NO_SENSE && 1940 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1911 common->cmnd[0] != INQUIRY && 1941 common->cmnd[0] != INQUIRY &&
1912 common->cmnd[0] != REQUEST_SENSE) { 1942 common->cmnd[0] != REQUEST_SENSE) {
1913 curlun->sense_data = curlun->unit_attention_data; 1943 curlun->sense_data = curlun->unit_attention_data;
1914 curlun->unit_attention_data = SS_NO_SENSE; 1944 curlun->unit_attention_data = SS_NO_SENSE;
1915 return -EINVAL; 1945 return -EINVAL;
@@ -1935,7 +1965,6 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1935 return 0; 1965 return 0;
1936} 1966}
1937 1967
1938
1939static int do_scsi_command(struct fsg_common *common) 1968static int do_scsi_command(struct fsg_common *common)
1940{ 1969{
1941 struct fsg_buffhd *bh; 1970 struct fsg_buffhd *bh;
@@ -2123,8 +2152,10 @@ static int do_scsi_command(struct fsg_common *common)
2123 "TEST UNIT READY"); 2152 "TEST UNIT READY");
2124 break; 2153 break;
2125 2154
2126 /* Although optional, this command is used by MS-Windows. We 2155 /*
2127 * support a minimal version: BytChk must be 0. */ 2156 * Although optional, this command is used by MS-Windows. We
2157 * support a minimal version: BytChk must be 0.
2158 */
2128 case VERIFY: 2159 case VERIFY:
2129 common->data_size_from_cmnd = 0; 2160 common->data_size_from_cmnd = 0;
2130 reply = check_command(common, 10, DATA_DIR_NONE, 2161 reply = check_command(common, 10, DATA_DIR_NONE,
@@ -2164,10 +2195,12 @@ static int do_scsi_command(struct fsg_common *common)
2164 reply = do_write(common); 2195 reply = do_write(common);
2165 break; 2196 break;
2166 2197
2167 /* Some mandatory commands that we recognize but don't implement. 2198 /*
2199 * Some mandatory commands that we recognize but don't implement.
2168 * They don't mean much in this setting. It's left as an exercise 2200 * They don't mean much in this setting. It's left as an exercise
2169 * for anyone interested to implement RESERVE and RELEASE in terms 2201 * for anyone interested to implement RESERVE and RELEASE in terms
2170 * of Posix locks. */ 2202 * of Posix locks.
2203 */
2171 case FORMAT_UNIT: 2204 case FORMAT_UNIT:
2172 case RELEASE: 2205 case RELEASE:
2173 case RESERVE: 2206 case RESERVE:
@@ -2195,7 +2228,7 @@ unknown_cmnd:
2195 if (reply == -EINVAL) 2228 if (reply == -EINVAL)
2196 reply = 0; /* Error reply length */ 2229 reply = 0; /* Error reply length */
2197 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { 2230 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2198 reply = min((u32) reply, common->data_size_from_cmnd); 2231 reply = min((u32)reply, common->data_size_from_cmnd);
2199 bh->inreq->length = reply; 2232 bh->inreq->length = reply;
2200 bh->state = BUF_STATE_FULL; 2233 bh->state = BUF_STATE_FULL;
2201 common->residue -= reply; 2234 common->residue -= reply;
@@ -2225,7 +2258,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2225 req->actual, 2258 req->actual,
2226 le32_to_cpu(cbw->Signature)); 2259 le32_to_cpu(cbw->Signature));
2227 2260
2228 /* The Bulk-only spec says we MUST stall the IN endpoint 2261 /*
2262 * The Bulk-only spec says we MUST stall the IN endpoint
2229 * (6.6.1), so it's unavoidable. It also says we must 2263 * (6.6.1), so it's unavoidable. It also says we must
2230 * retain this state until the next reset, but there's 2264 * retain this state until the next reset, but there's
2231 * no way to tell the controller driver it should ignore 2265 * no way to tell the controller driver it should ignore
@@ -2233,7 +2267,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2233 * 2267 *
2234 * We aren't required to halt the OUT endpoint; instead 2268 * We aren't required to halt the OUT endpoint; instead
2235 * we can simply accept and discard any data received 2269 * we can simply accept and discard any data received
2236 * until the next reset. */ 2270 * until the next reset.
2271 */
2237 wedge_bulk_in_endpoint(fsg); 2272 wedge_bulk_in_endpoint(fsg);
2238 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); 2273 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2239 return -EINVAL; 2274 return -EINVAL;
@@ -2246,8 +2281,10 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2246 "cmdlen %u\n", 2281 "cmdlen %u\n",
2247 cbw->Lun, cbw->Flags, cbw->Length); 2282 cbw->Lun, cbw->Flags, cbw->Length);
2248 2283
2249 /* We can do anything we want here, so let's stall the 2284 /*
2250 * bulk pipes if we are allowed to. */ 2285 * We can do anything we want here, so let's stall the
2286 * bulk pipes if we are allowed to.
2287 */
2251 if (common->can_stall) { 2288 if (common->can_stall) {
2252 fsg_set_halt(fsg, fsg->bulk_out); 2289 fsg_set_halt(fsg, fsg->bulk_out);
2253 halt_bulk_in_endpoint(fsg); 2290 halt_bulk_in_endpoint(fsg);
@@ -2270,7 +2307,6 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2270 return 0; 2307 return 0;
2271} 2308}
2272 2309
2273
2274static int get_next_command(struct fsg_common *common) 2310static int get_next_command(struct fsg_common *common)
2275{ 2311{
2276 struct fsg_buffhd *bh; 2312 struct fsg_buffhd *bh;
@@ -2287,14 +2323,15 @@ static int get_next_command(struct fsg_common *common)
2287 /* Queue a request to read a Bulk-only CBW */ 2323 /* Queue a request to read a Bulk-only CBW */
2288 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); 2324 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2289 bh->outreq->short_not_ok = 1; 2325 bh->outreq->short_not_ok = 1;
2290 START_TRANSFER_OR(common, bulk_out, bh->outreq, 2326 if (!start_out_transfer(common, bh))
2291 &bh->outreq_busy, &bh->state)
2292 /* Don't know what to do if common->fsg is NULL */ 2327 /* Don't know what to do if common->fsg is NULL */
2293 return -EIO; 2328 return -EIO;
2294 2329
2295 /* We will drain the buffer in software, which means we 2330 /*
2331 * We will drain the buffer in software, which means we
2296 * can reuse it for the next filling. No need to advance 2332 * can reuse it for the next filling. No need to advance
2297 * next_buffhd_to_fill. */ 2333 * next_buffhd_to_fill.
2334 */
2298 2335
2299 /* Wait for the CBW to arrive */ 2336 /* Wait for the CBW to arrive */
2300 while (bh->state != BUF_STATE_FULL) { 2337 while (bh->state != BUF_STATE_FULL) {
@@ -2425,7 +2462,6 @@ reset:
2425 2462
2426/****************************** ALT CONFIGS ******************************/ 2463/****************************** ALT CONFIGS ******************************/
2427 2464
2428
2429static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 2465static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2430{ 2466{
2431 struct fsg_dev *fsg = fsg_from_func(f); 2467 struct fsg_dev *fsg = fsg_from_func(f);
@@ -2453,8 +2489,10 @@ static void handle_exception(struct fsg_common *common)
2453 struct fsg_lun *curlun; 2489 struct fsg_lun *curlun;
2454 unsigned int exception_req_tag; 2490 unsigned int exception_req_tag;
2455 2491
2456 /* Clear the existing signals. Anything but SIGUSR1 is converted 2492 /*
2457 * into a high-priority EXIT exception. */ 2493 * Clear the existing signals. Anything but SIGUSR1 is converted
2494 * into a high-priority EXIT exception.
2495 */
2458 for (;;) { 2496 for (;;) {
2459 int sig = 2497 int sig =
2460 dequeue_signal_lock(current, &current->blocked, &info); 2498 dequeue_signal_lock(current, &current->blocked, &info);
@@ -2498,8 +2536,10 @@ static void handle_exception(struct fsg_common *common)
2498 usb_ep_fifo_flush(common->fsg->bulk_out); 2536 usb_ep_fifo_flush(common->fsg->bulk_out);
2499 } 2537 }
2500 2538
2501 /* Reset the I/O buffer states and pointers, the SCSI 2539 /*
2502 * state, and the exception. Then invoke the handler. */ 2540 * Reset the I/O buffer states and pointers, the SCSI
2541 * state, and the exception. Then invoke the handler.
2542 */
2503 spin_lock_irq(&common->lock); 2543 spin_lock_irq(&common->lock);
2504 2544
2505 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2545 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
@@ -2537,9 +2577,11 @@ static void handle_exception(struct fsg_common *common)
2537 break; 2577 break;
2538 2578
2539 case FSG_STATE_RESET: 2579 case FSG_STATE_RESET:
2540 /* In case we were forced against our will to halt a 2580 /*
2581 * In case we were forced against our will to halt a
2541 * bulk endpoint, clear the halt now. (The SuperH UDC 2582 * bulk endpoint, clear the halt now. (The SuperH UDC
2542 * requires this.) */ 2583 * requires this.)
2584 */
2543 if (!fsg_is_set(common)) 2585 if (!fsg_is_set(common))
2544 break; 2586 break;
2545 if (test_and_clear_bit(IGNORE_BULK_OUT, 2587 if (test_and_clear_bit(IGNORE_BULK_OUT,
@@ -2549,9 +2591,11 @@ static void handle_exception(struct fsg_common *common)
2549 if (common->ep0_req_tag == exception_req_tag) 2591 if (common->ep0_req_tag == exception_req_tag)
2550 ep0_queue(common); /* Complete the status stage */ 2592 ep0_queue(common); /* Complete the status stage */
2551 2593
2552 /* Technically this should go here, but it would only be 2594 /*
2595 * Technically this should go here, but it would only be
2553 * a waste of time. Ditto for the INTERFACE_CHANGE and 2596 * a waste of time. Ditto for the INTERFACE_CHANGE and
2554 * CONFIG_CHANGE cases. */ 2597 * CONFIG_CHANGE cases.
2598 */
2555 /* for (i = 0; i < common->nluns; ++i) */ 2599 /* for (i = 0; i < common->nluns; ++i) */
2556 /* common->luns[i].unit_attention_data = */ 2600 /* common->luns[i].unit_attention_data = */
2557 /* SS_RESET_OCCURRED; */ 2601 /* SS_RESET_OCCURRED; */
@@ -2586,8 +2630,10 @@ static int fsg_main_thread(void *common_)
2586{ 2630{
2587 struct fsg_common *common = common_; 2631 struct fsg_common *common = common_;
2588 2632
2589 /* Allow the thread to be killed by a signal, but set the signal mask 2633 /*
2590 * to block everything but INT, TERM, KILL, and USR1. */ 2634 * Allow the thread to be killed by a signal, but set the signal mask
2635 * to block everything but INT, TERM, KILL, and USR1.
2636 */
2591 allow_signal(SIGINT); 2637 allow_signal(SIGINT);
2592 allow_signal(SIGTERM); 2638 allow_signal(SIGTERM);
2593 allow_signal(SIGKILL); 2639 allow_signal(SIGKILL);
@@ -2596,9 +2642,11 @@ static int fsg_main_thread(void *common_)
2596 /* Allow the thread to be frozen */ 2642 /* Allow the thread to be frozen */
2597 set_freezable(); 2643 set_freezable();
2598 2644
2599 /* Arrange for userspace references to be interpreted as kernel 2645 /*
2646 * Arrange for userspace references to be interpreted as kernel
2600 * pointers. That way we can pass a kernel pointer to a routine 2647 * pointers. That way we can pass a kernel pointer to a routine
2601 * that expects a __user pointer and it will work okay. */ 2648 * that expects a __user pointer and it will work okay.
2649 */
2602 set_fs(get_ds()); 2650 set_fs(get_ds());
2603 2651
2604 /* The main loop */ 2652 /* The main loop */
@@ -2658,7 +2706,7 @@ static int fsg_main_thread(void *common_)
2658 up_write(&common->filesem); 2706 up_write(&common->filesem);
2659 } 2707 }
2660 2708
2661 /* Let the unbind and cleanup routines know the thread has exited */ 2709 /* Let fsg_unbind() know the thread has exited */
2662 complete_and_exit(&common->thread_notifier, 0); 2710 complete_and_exit(&common->thread_notifier, 0);
2663} 2711}
2664 2712
@@ -2690,7 +2738,6 @@ static inline void fsg_common_put(struct fsg_common *common)
2690 kref_put(&common->ref, fsg_common_release); 2738 kref_put(&common->ref, fsg_common_release);
2691} 2739}
2692 2740
2693
2694static struct fsg_common *fsg_common_init(struct fsg_common *common, 2741static struct fsg_common *fsg_common_init(struct fsg_common *common,
2695 struct usb_composite_dev *cdev, 2742 struct usb_composite_dev *cdev,
2696 struct fsg_config *cfg) 2743 struct fsg_config *cfg)
@@ -2736,8 +2783,10 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2736 fsg_intf_desc.iInterface = rc; 2783 fsg_intf_desc.iInterface = rc;
2737 } 2784 }
2738 2785
2739 /* Create the LUNs, open their backing files, and register the 2786 /*
2740 * LUN devices in sysfs. */ 2787 * Create the LUNs, open their backing files, and register the
2788 * LUN devices in sysfs.
2789 */
2741 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); 2790 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2742 if (unlikely(!curlun)) { 2791 if (unlikely(!curlun)) {
2743 rc = -ENOMEM; 2792 rc = -ENOMEM;
@@ -2765,6 +2814,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2765 if (rc) { 2814 if (rc) {
2766 INFO(common, "failed to register LUN%d: %d\n", i, rc); 2815 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2767 common->nluns = i; 2816 common->nluns = i;
2817 put_device(&curlun->dev);
2768 goto error_release; 2818 goto error_release;
2769 } 2819 }
2770 2820
@@ -2790,7 +2840,6 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2790 } 2840 }
2791 common->nluns = nluns; 2841 common->nluns = nluns;
2792 2842
2793
2794 /* Data buffers cyclic list */ 2843 /* Data buffers cyclic list */
2795 bh = common->buffhds; 2844 bh = common->buffhds;
2796 i = FSG_NUM_BUFFERS; 2845 i = FSG_NUM_BUFFERS;
@@ -2807,7 +2856,6 @@ buffhds_first_it:
2807 } while (--i); 2856 } while (--i);
2808 bh->next = common->buffhds; 2857 bh->next = common->buffhds;
2809 2858
2810
2811 /* Prepare inquiryString */ 2859 /* Prepare inquiryString */
2812 if (cfg->release != 0xffff) { 2860 if (cfg->release != 0xffff) {
2813 i = cfg->release; 2861 i = cfg->release;
@@ -2821,41 +2869,35 @@ buffhds_first_it:
2821 i = 0x0399; 2869 i = 0x0399;
2822 } 2870 }
2823 } 2871 }
2824#define OR(x, y) ((x) ? (x) : (y))
2825 snprintf(common->inquiry_string, sizeof common->inquiry_string, 2872 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2826 "%-8s%-16s%04x", 2873 "%-8s%-16s%04x", cfg->vendor_name ?: "Linux",
2827 OR(cfg->vendor_name, "Linux "),
2828 /* Assume product name dependent on the first LUN */ 2874 /* Assume product name dependent on the first LUN */
2829 OR(cfg->product_name, common->luns->cdrom 2875 cfg->product_name ?: (common->luns->cdrom
2830 ? "File-Stor Gadget" 2876 ? "File-Stor Gadget"
2831 : "File-CD Gadget "), 2877 : "File-CD Gadget"),
2832 i); 2878 i);
2833 2879
2834 2880 /*
2835 /* Some peripheral controllers are known not to be able to 2881 * Some peripheral controllers are known not to be able to
2836 * halt bulk endpoints correctly. If one of them is present, 2882 * halt bulk endpoints correctly. If one of them is present,
2837 * disable stalls. 2883 * disable stalls.
2838 */ 2884 */
2839 common->can_stall = cfg->can_stall && 2885 common->can_stall = cfg->can_stall &&
2840 !(gadget_is_at91(common->gadget)); 2886 !(gadget_is_at91(common->gadget));
2841 2887
2842
2843 spin_lock_init(&common->lock); 2888 spin_lock_init(&common->lock);
2844 kref_init(&common->ref); 2889 kref_init(&common->ref);
2845 2890
2846
2847 /* Tell the thread to start working */ 2891 /* Tell the thread to start working */
2848 common->thread_task = 2892 common->thread_task =
2849 kthread_create(fsg_main_thread, common, 2893 kthread_create(fsg_main_thread, common,
2850 OR(cfg->thread_name, "file-storage")); 2894 cfg->thread_name ?: "file-storage");
2851 if (IS_ERR(common->thread_task)) { 2895 if (IS_ERR(common->thread_task)) {
2852 rc = PTR_ERR(common->thread_task); 2896 rc = PTR_ERR(common->thread_task);
2853 goto error_release; 2897 goto error_release;
2854 } 2898 }
2855 init_completion(&common->thread_notifier); 2899 init_completion(&common->thread_notifier);
2856 init_waitqueue_head(&common->fsg_wait); 2900 init_waitqueue_head(&common->fsg_wait);
2857#undef OR
2858
2859 2901
2860 /* Information */ 2902 /* Information */
2861 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); 2903 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
@@ -2889,18 +2931,15 @@ buffhds_first_it:
2889 2931
2890 return common; 2932 return common;
2891 2933
2892
2893error_luns: 2934error_luns:
2894 common->nluns = i + 1; 2935 common->nluns = i + 1;
2895error_release: 2936error_release:
2896 common->state = FSG_STATE_TERMINATED; /* The thread is dead */ 2937 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
2897 /* Call fsg_common_release() directly, ref might be not 2938 /* Call fsg_common_release() directly, ref might be not initialised. */
2898 * initialised */
2899 fsg_common_release(&common->ref); 2939 fsg_common_release(&common->ref);
2900 return ERR_PTR(rc); 2940 return ERR_PTR(rc);
2901} 2941}
2902 2942
2903
2904static void fsg_common_release(struct kref *ref) 2943static void fsg_common_release(struct kref *ref)
2905{ 2944{
2906 struct fsg_common *common = container_of(ref, struct fsg_common, ref); 2945 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
@@ -2909,9 +2948,6 @@ static void fsg_common_release(struct kref *ref)
2909 if (common->state != FSG_STATE_TERMINATED) { 2948 if (common->state != FSG_STATE_TERMINATED) {
2910 raise_exception(common, FSG_STATE_EXIT); 2949 raise_exception(common, FSG_STATE_EXIT);
2911 wait_for_completion(&common->thread_notifier); 2950 wait_for_completion(&common->thread_notifier);
2912
2913 /* The cleanup routine waits for this completion also */
2914 complete(&common->thread_notifier);
2915 } 2951 }
2916 2952
2917 if (likely(common->luns)) { 2953 if (likely(common->luns)) {
@@ -2945,7 +2981,6 @@ static void fsg_common_release(struct kref *ref)
2945 2981
2946/*-------------------------------------------------------------------------*/ 2982/*-------------------------------------------------------------------------*/
2947 2983
2948
2949static void fsg_unbind(struct usb_configuration *c, struct usb_function *f) 2984static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2950{ 2985{
2951 struct fsg_dev *fsg = fsg_from_func(f); 2986 struct fsg_dev *fsg = fsg_from_func(f);
@@ -2965,7 +3000,6 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2965 kfree(fsg); 3000 kfree(fsg);
2966} 3001}
2967 3002
2968
2969static int fsg_bind(struct usb_configuration *c, struct usb_function *f) 3003static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2970{ 3004{
2971 struct fsg_dev *fsg = fsg_from_func(f); 3005 struct fsg_dev *fsg = fsg_from_func(f);
@@ -3048,11 +3082,13 @@ static int fsg_bind_config(struct usb_composite_dev *cdev,
3048 fsg->function.disable = fsg_disable; 3082 fsg->function.disable = fsg_disable;
3049 3083
3050 fsg->common = common; 3084 fsg->common = common;
3051 /* Our caller holds a reference to common structure so we 3085 /*
3086 * Our caller holds a reference to common structure so we
3052 * don't have to be worry about it being freed until we return 3087 * don't have to be worry about it being freed until we return
3053 * from this function. So instead of incrementing counter now 3088 * from this function. So instead of incrementing counter now
3054 * and decrement in error recovery we increment it only when 3089 * and decrement in error recovery we increment it only when
3055 * call to usb_add_function() was successful. */ 3090 * call to usb_add_function() was successful.
3091 */
3056 3092
3057 rc = usb_add_function(c, &fsg->function); 3093 rc = usb_add_function(c, &fsg->function);
3058 if (unlikely(rc)) 3094 if (unlikely(rc))
@@ -3063,8 +3099,7 @@ static int fsg_bind_config(struct usb_composite_dev *cdev,
3063} 3099}
3064 3100
3065static inline int __deprecated __maybe_unused 3101static inline int __deprecated __maybe_unused
3066fsg_add(struct usb_composite_dev *cdev, 3102fsg_add(struct usb_composite_dev *cdev, struct usb_configuration *c,
3067 struct usb_configuration *c,
3068 struct fsg_common *common) 3103 struct fsg_common *common)
3069{ 3104{
3070 return fsg_bind_config(cdev, c, common); 3105 return fsg_bind_config(cdev, c, common);
@@ -3073,7 +3108,6 @@ fsg_add(struct usb_composite_dev *cdev,
3073 3108
3074/************************* Module parameters *************************/ 3109/************************* Module parameters *************************/
3075 3110
3076
3077struct fsg_module_parameters { 3111struct fsg_module_parameters {
3078 char *file[FSG_MAX_LUNS]; 3112 char *file[FSG_MAX_LUNS];
3079 int ro[FSG_MAX_LUNS]; 3113 int ro[FSG_MAX_LUNS];
@@ -3087,7 +3121,6 @@ struct fsg_module_parameters {
3087 int stall; /* can_stall */ 3121 int stall; /* can_stall */
3088}; 3122};
3089 3123
3090
3091#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \ 3124#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3092 module_param_array_named(prefix ## name, params.name, type, \ 3125 module_param_array_named(prefix ## name, params.name, type, \
3093 &prefix ## params.name ## _count, \ 3126 &prefix ## params.name ## _count, \
@@ -3115,7 +3148,6 @@ struct fsg_module_parameters {
3115 _FSG_MODULE_PARAM(prefix, params, stall, bool, \ 3148 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3116 "false to prevent bulk stalls") 3149 "false to prevent bulk stalls")
3117 3150
3118
3119static void 3151static void
3120fsg_config_from_params(struct fsg_config *cfg, 3152fsg_config_from_params(struct fsg_config *cfg,
3121 const struct fsg_module_parameters *params) 3153 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..130eee678c8b
--- /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 alligned
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/file_storage.c b/drivers/usb/gadget/file_storage.c
index d4fdf65fb925..a6eacb59571b 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -3392,25 +3392,28 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3392 dev_set_name(&curlun->dev,"%s-lun%d", 3392 dev_set_name(&curlun->dev,"%s-lun%d",
3393 dev_name(&gadget->dev), i); 3393 dev_name(&gadget->dev), i);
3394 3394
3395 if ((rc = device_register(&curlun->dev)) != 0) { 3395 kref_get(&fsg->ref);
3396 rc = device_register(&curlun->dev);
3397 if (rc) {
3396 INFO(fsg, "failed to register LUN%d: %d\n", i, rc); 3398 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
3397 goto out; 3399 put_device(&curlun->dev);
3398 }
3399 if ((rc = device_create_file(&curlun->dev,
3400 &dev_attr_ro)) != 0 ||
3401 (rc = device_create_file(&curlun->dev,
3402 &dev_attr_nofua)) != 0 ||
3403 (rc = device_create_file(&curlun->dev,
3404 &dev_attr_file)) != 0) {
3405 device_unregister(&curlun->dev);
3406 goto out; 3400 goto out;
3407 } 3401 }
3408 curlun->registered = 1; 3402 curlun->registered = 1;
3409 kref_get(&fsg->ref); 3403
3404 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3405 if (rc)
3406 goto out;
3407 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3408 if (rc)
3409 goto out;
3410 rc = device_create_file(&curlun->dev, &dev_attr_file);
3411 if (rc)
3412 goto out;
3410 3413
3411 if (mod_data.file[i] && *mod_data.file[i]) { 3414 if (mod_data.file[i] && *mod_data.file[i]) {
3412 if ((rc = fsg_lun_open(curlun, 3415 rc = fsg_lun_open(curlun, mod_data.file[i]);
3413 mod_data.file[i])) != 0) 3416 if (rc)
3414 goto out; 3417 goto out;
3415 } else if (!mod_data.removable) { 3418 } else if (!mod_data.removable) {
3416 ERROR(fsg, "no file given for LUN%d\n", i); 3419 ERROR(fsg, "no file given for LUN%d\n", i);
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index af75e3620849..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"
@@ -73,8 +94,6 @@ MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
73module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644); 94module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
74MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol"); 95MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
75 96
76
77
78static const struct usb_descriptor_header *gfs_otg_desc[] = { 97static const struct usb_descriptor_header *gfs_otg_desc[] = {
79 (const struct usb_descriptor_header *) 98 (const struct usb_descriptor_header *)
80 &(const struct usb_otg_descriptor) { 99 &(const struct usb_otg_descriptor) {
@@ -91,8 +110,7 @@ static const struct usb_descriptor_header *gfs_otg_desc[] = {
91 NULL 110 NULL
92}; 111};
93 112
94/* string IDs are assigned dynamically */ 113/* String IDs are assigned dynamically */
95
96static struct usb_string gfs_strings[] = { 114static struct usb_string gfs_strings[] = {
97#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 115#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
98 { .s = "FunctionFS + RNDIS" }, 116 { .s = "FunctionFS + RNDIS" },
@@ -114,8 +132,6 @@ static struct usb_gadget_strings *gfs_dev_strings[] = {
114 NULL, 132 NULL,
115}; 133};
116 134
117
118
119struct gfs_configuration { 135struct gfs_configuration {
120 struct usb_configuration c; 136 struct usb_configuration c;
121 int (*eth)(struct usb_configuration *c, u8 *ethaddr); 137 int (*eth)(struct usb_configuration *c, u8 *ethaddr);
@@ -138,7 +154,6 @@ struct gfs_configuration {
138#endif 154#endif
139}; 155};
140 156
141
142static int gfs_bind(struct usb_composite_dev *cdev); 157static int gfs_bind(struct usb_composite_dev *cdev);
143static int gfs_unbind(struct usb_composite_dev *cdev); 158static int gfs_unbind(struct usb_composite_dev *cdev);
144static int gfs_do_config(struct usb_configuration *c); 159static int gfs_do_config(struct usb_configuration *c);
@@ -151,11 +166,9 @@ static struct usb_composite_driver gfs_driver = {
151 .iProduct = DRIVER_DESC, 166 .iProduct = DRIVER_DESC,
152}; 167};
153 168
154
155static struct ffs_data *gfs_ffs_data; 169static struct ffs_data *gfs_ffs_data;
156static unsigned long gfs_registered; 170static unsigned long gfs_registered;
157 171
158
159static int gfs_init(void) 172static int gfs_init(void)
160{ 173{
161 ENTER(); 174 ENTER();
@@ -175,7 +188,6 @@ static void gfs_exit(void)
175} 188}
176module_exit(gfs_exit); 189module_exit(gfs_exit);
177 190
178
179static int functionfs_ready_callback(struct ffs_data *ffs) 191static int functionfs_ready_callback(struct ffs_data *ffs)
180{ 192{
181 int ret; 193 int ret;
@@ -200,14 +212,11 @@ static void functionfs_closed_callback(struct ffs_data *ffs)
200 usb_composite_unregister(&gfs_driver); 212 usb_composite_unregister(&gfs_driver);
201} 213}
202 214
203
204static int functionfs_check_dev_callback(const char *dev_name) 215static int functionfs_check_dev_callback(const char *dev_name)
205{ 216{
206 return 0; 217 return 0;
207} 218}
208 219
209
210
211static int gfs_bind(struct usb_composite_dev *cdev) 220static int gfs_bind(struct usb_composite_dev *cdev)
212{ 221{
213 int ret, i; 222 int ret, i;
@@ -274,7 +283,6 @@ static int gfs_unbind(struct usb_composite_dev *cdev)
274 return 0; 283 return 0;
275} 284}
276 285
277
278static int gfs_do_config(struct usb_configuration *c) 286static int gfs_do_config(struct usb_configuration *c)
279{ 287{
280 struct gfs_configuration *gc = 288 struct gfs_configuration *gc =
@@ -315,7 +323,6 @@ static int gfs_do_config(struct usb_configuration *c)
315 return 0; 323 return 0;
316} 324}
317 325
318
319#ifdef CONFIG_USB_FUNCTIONFS_ETH 326#ifdef CONFIG_USB_FUNCTIONFS_ETH
320 327
321static 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])
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index e511fec9f26d..5c2720d64ffa 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -96,7 +96,7 @@
96 96
97/* Mentor high speed "dual role" controller, in peripheral role */ 97/* Mentor high speed "dual role" controller, in peripheral role */
98#ifdef CONFIG_USB_GADGET_MUSB_HDRC 98#ifdef CONFIG_USB_GADGET_MUSB_HDRC
99#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name) 99#define gadget_is_musbhdrc(g) !strcmp("musb-hdrc", (g)->name)
100#else 100#else
101#define gadget_is_musbhdrc(g) 0 101#define gadget_is_musbhdrc(g) 0
102#endif 102#endif
@@ -120,10 +120,10 @@
120#define gadget_is_fsl_qe(g) 0 120#define gadget_is_fsl_qe(g) 0
121#endif 121#endif
122 122
123#ifdef CONFIG_USB_GADGET_CI13XXX 123#ifdef CONFIG_USB_GADGET_CI13XXX_PCI
124#define gadget_is_ci13xxx(g) (!strcmp("ci13xxx_udc", (g)->name)) 124#define gadget_is_ci13xxx_pci(g) (!strcmp("ci13xxx_pci", (g)->name))
125#else 125#else
126#define gadget_is_ci13xxx(g) 0 126#define gadget_is_ci13xxx_pci(g) 0
127#endif 127#endif
128 128
129// CONFIG_USB_GADGET_SX2 129// CONFIG_USB_GADGET_SX2
@@ -142,6 +142,17 @@
142#define gadget_is_s3c_hsotg(g) 0 142#define gadget_is_s3c_hsotg(g) 0
143#endif 143#endif
144 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
145 156
146/** 157/**
147 * usb_gadget_controller_number - support bcdDevice id convention 158 * usb_gadget_controller_number - support bcdDevice id convention
@@ -192,7 +203,7 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
192 return 0x21; 203 return 0x21;
193 else if (gadget_is_fsl_qe(gadget)) 204 else if (gadget_is_fsl_qe(gadget))
194 return 0x22; 205 return 0x22;
195 else if (gadget_is_ci13xxx(gadget)) 206 else if (gadget_is_ci13xxx_pci(gadget))
196 return 0x23; 207 return 0x23;
197 else if (gadget_is_langwell(gadget)) 208 else if (gadget_is_langwell(gadget))
198 return 0x24; 209 return 0x24;
@@ -200,6 +211,10 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
200 return 0x25; 211 return 0x25;
201 else if (gadget_is_s3c_hsotg(gadget)) 212 else if (gadget_is_s3c_hsotg(gadget))
202 return 0x26; 213 return 0x26;
214 else if (gadget_is_pch(gadget))
215 return 0x27;
216 else if (gadget_is_ci13xxx_msm(gadget))
217 return 0x28;
203 return -ENOENT; 218 return -ENOENT;
204} 219}
205 220
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index ed0266462c57..1210534822d6 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -1191,13 +1191,17 @@ static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1191 return IRQ_HANDLED; 1191 return IRQ_HANDLED;
1192} 1192}
1193 1193
1194#ifndef MX1_INT_USBD0
1195#define MX1_INT_USBD0 MX1_USBD_INT0
1196#endif
1197
1194static irqreturn_t imx_udc_bulk_irq(int irq, void *dev) 1198static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1195{ 1199{
1196 struct imx_udc_struct *imx_usb = dev; 1200 struct imx_udc_struct *imx_usb = dev;
1197 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0]; 1201 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))); 1202 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1199 1203
1200 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev); 1204 dump_ep_intr(__func__, irq - MX1_INT_USBD0, intr, imx_usb->dev);
1201 1205
1202 if (!imx_usb->driver) { 1206 if (!imx_usb->driver) {
1203 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); 1207 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
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/langwell_udc.c b/drivers/usb/gadget/langwell_udc.c
index b8ec954c0692..777972454e3e 100644
--- a/drivers/usb/gadget/langwell_udc.c
+++ b/drivers/usb/gadget/langwell_udc.c
@@ -2225,6 +2225,7 @@ static void handle_setup_packet(struct langwell_udc *dev,
2225 u16 wValue = le16_to_cpu(setup->wValue); 2225 u16 wValue = le16_to_cpu(setup->wValue);
2226 u16 wIndex = le16_to_cpu(setup->wIndex); 2226 u16 wIndex = le16_to_cpu(setup->wIndex);
2227 u16 wLength = le16_to_cpu(setup->wLength); 2227 u16 wLength = le16_to_cpu(setup->wLength);
2228 u32 portsc1;
2228 2229
2229 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__); 2230 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2230 2231
@@ -2313,6 +2314,28 @@ static void handle_setup_packet(struct langwell_udc *dev,
2313 dev->dev_status &= ~(1 << wValue); 2314 dev->dev_status &= ~(1 << wValue);
2314 } 2315 }
2315 break; 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;
2316 default: 2339 default:
2317 rc = -EOPNOTSUPP; 2340 rc = -EOPNOTSUPP;
2318 break; 2341 break;
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
index 0769179dbdb0..01822422c3e8 100644
--- a/drivers/usb/gadget/mass_storage.c
+++ b/drivers/usb/gadget/mass_storage.c
@@ -102,7 +102,7 @@ static struct fsg_module_parameters mod_data = {
102}; 102};
103FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 103FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
104 104
105static unsigned long msg_registered = 0; 105static unsigned long msg_registered;
106static void msg_cleanup(void); 106static void msg_cleanup(void);
107 107
108static int msg_thread_exits(struct fsg_common *common) 108static int msg_thread_exits(struct fsg_common *common)
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..d5468a7f38e0
--- /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 * occure 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 occure 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 occured */
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 platform_device *_dev, pm_message_t state)
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 platform_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 goto error;
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/pch_udc.c b/drivers/usb/gadget/pch_udc.c
new file mode 100644
index 000000000000..0c8dd81dddca
--- /dev/null
+++ b/drivers/usb/gadget/pch_udc.c
@@ -0,0 +1,2947 @@
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 64
202#define UDC_EPIN_BUFF_SIZE 512
203#define UDC_EP0OUT_BUFF_SIZE 64
204#define UDC_EPOUT_BUFF_SIZE 512
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 unsigned long ep0out_buf[64];
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
365static const char ep0_string[] = "ep0in";
366static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */
367struct pch_udc_dev *pch_udc; /* pointer to device object */
368
369static int speed_fs;
370module_param_named(speed_fs, speed_fs, bool, S_IRUGO);
371MODULE_PARM_DESC(speed_fs, "true for Full speed operation");
372
373/**
374 * struct pch_udc_request - Structure holding a PCH USB device request packet
375 * @req: embedded ep request
376 * @td_data_phys: phys. address
377 * @td_data: first dma desc. of chain
378 * @td_data_last: last dma desc. of chain
379 * @queue: associated queue
380 * @dma_going: DMA in progress for request
381 * @dma_mapped: DMA memory mapped for request
382 * @dma_done: DMA completed for request
383 * @chain_len: chain length
384 */
385struct pch_udc_request {
386 struct usb_request req;
387 dma_addr_t td_data_phys;
388 struct pch_udc_data_dma_desc *td_data;
389 struct pch_udc_data_dma_desc *td_data_last;
390 struct list_head queue;
391 unsigned dma_going:1,
392 dma_mapped:1,
393 dma_done:1;
394 unsigned chain_len;
395};
396
397static inline u32 pch_udc_readl(struct pch_udc_dev *dev, unsigned long reg)
398{
399 return ioread32(dev->base_addr + reg);
400}
401
402static inline void pch_udc_writel(struct pch_udc_dev *dev,
403 unsigned long val, unsigned long reg)
404{
405 iowrite32(val, dev->base_addr + reg);
406}
407
408static inline void pch_udc_bit_set(struct pch_udc_dev *dev,
409 unsigned long reg,
410 unsigned long bitmask)
411{
412 pch_udc_writel(dev, pch_udc_readl(dev, reg) | bitmask, reg);
413}
414
415static inline void pch_udc_bit_clr(struct pch_udc_dev *dev,
416 unsigned long reg,
417 unsigned long bitmask)
418{
419 pch_udc_writel(dev, pch_udc_readl(dev, reg) & ~(bitmask), reg);
420}
421
422static inline u32 pch_udc_ep_readl(struct pch_udc_ep *ep, unsigned long reg)
423{
424 return ioread32(ep->dev->base_addr + ep->offset_addr + reg);
425}
426
427static inline void pch_udc_ep_writel(struct pch_udc_ep *ep,
428 unsigned long val, unsigned long reg)
429{
430 iowrite32(val, ep->dev->base_addr + ep->offset_addr + reg);
431}
432
433static inline void pch_udc_ep_bit_set(struct pch_udc_ep *ep,
434 unsigned long reg,
435 unsigned long bitmask)
436{
437 pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) | bitmask, reg);
438}
439
440static inline void pch_udc_ep_bit_clr(struct pch_udc_ep *ep,
441 unsigned long reg,
442 unsigned long bitmask)
443{
444 pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) & ~(bitmask), reg);
445}
446
447/**
448 * pch_udc_csr_busy() - Wait till idle.
449 * @dev: Reference to pch_udc_dev structure
450 */
451static void pch_udc_csr_busy(struct pch_udc_dev *dev)
452{
453 unsigned int count = 200;
454
455 /* Wait till idle */
456 while ((pch_udc_readl(dev, UDC_CSR_BUSY_ADDR) & UDC_CSR_BUSY)
457 && --count)
458 cpu_relax();
459 if (!count)
460 dev_err(&dev->pdev->dev, "%s: wait error\n", __func__);
461}
462
463/**
464 * pch_udc_write_csr() - Write the command and status registers.
465 * @dev: Reference to pch_udc_dev structure
466 * @val: value to be written to CSR register
467 * @addr: address of CSR register
468 */
469static void pch_udc_write_csr(struct pch_udc_dev *dev, unsigned long val,
470 unsigned int ep)
471{
472 unsigned long reg = PCH_UDC_CSR(ep);
473
474 pch_udc_csr_busy(dev); /* Wait till idle */
475 pch_udc_writel(dev, val, reg);
476 pch_udc_csr_busy(dev); /* Wait till idle */
477}
478
479/**
480 * pch_udc_read_csr() - Read the command and status registers.
481 * @dev: Reference to pch_udc_dev structure
482 * @addr: address of CSR register
483 *
484 * Return codes: content of CSR register
485 */
486static u32 pch_udc_read_csr(struct pch_udc_dev *dev, unsigned int ep)
487{
488 unsigned long reg = PCH_UDC_CSR(ep);
489
490 pch_udc_csr_busy(dev); /* Wait till idle */
491 pch_udc_readl(dev, reg); /* Dummy read */
492 pch_udc_csr_busy(dev); /* Wait till idle */
493 return pch_udc_readl(dev, reg);
494}
495
496/**
497 * pch_udc_rmt_wakeup() - Initiate for remote wakeup
498 * @dev: Reference to pch_udc_dev structure
499 */
500static inline void pch_udc_rmt_wakeup(struct pch_udc_dev *dev)
501{
502 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
503 mdelay(1);
504 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
505}
506
507/**
508 * pch_udc_get_frame() - Get the current frame from device status register
509 * @dev: Reference to pch_udc_dev structure
510 * Retern current frame
511 */
512static inline int pch_udc_get_frame(struct pch_udc_dev *dev)
513{
514 u32 frame = pch_udc_readl(dev, UDC_DEVSTS_ADDR);
515 return (frame & UDC_DEVSTS_TS_MASK) >> UDC_DEVSTS_TS_SHIFT;
516}
517
518/**
519 * pch_udc_clear_selfpowered() - Clear the self power control
520 * @dev: Reference to pch_udc_regs structure
521 */
522static inline void pch_udc_clear_selfpowered(struct pch_udc_dev *dev)
523{
524 pch_udc_bit_clr(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
525}
526
527/**
528 * pch_udc_set_selfpowered() - Set the self power control
529 * @dev: Reference to pch_udc_regs structure
530 */
531static inline void pch_udc_set_selfpowered(struct pch_udc_dev *dev)
532{
533 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
534}
535
536/**
537 * pch_udc_set_disconnect() - Set the disconnect status.
538 * @dev: Reference to pch_udc_regs structure
539 */
540static inline void pch_udc_set_disconnect(struct pch_udc_dev *dev)
541{
542 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
543}
544
545/**
546 * pch_udc_clear_disconnect() - Clear the disconnect status.
547 * @dev: Reference to pch_udc_regs structure
548 */
549static void pch_udc_clear_disconnect(struct pch_udc_dev *dev)
550{
551 /* Clear the disconnect */
552 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
553 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
554 mdelay(1);
555 /* Resume USB signalling */
556 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
557}
558
559/**
560 * pch_udc_vbus_session() - set or clearr the disconnect status.
561 * @dev: Reference to pch_udc_regs structure
562 * @is_active: Parameter specifying the action
563 * 0: indicating VBUS power is ending
564 * !0: indicating VBUS power is starting
565 */
566static inline void pch_udc_vbus_session(struct pch_udc_dev *dev,
567 int is_active)
568{
569 if (is_active)
570 pch_udc_clear_disconnect(dev);
571 else
572 pch_udc_set_disconnect(dev);
573}
574
575/**
576 * pch_udc_ep_set_stall() - Set the stall of endpoint
577 * @ep: Reference to structure of type pch_udc_ep_regs
578 */
579static void pch_udc_ep_set_stall(struct pch_udc_ep *ep)
580{
581 if (ep->in) {
582 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
583 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
584 } else {
585 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
586 }
587}
588
589/**
590 * pch_udc_ep_clear_stall() - Clear the stall of endpoint
591 * @ep: Reference to structure of type pch_udc_ep_regs
592 */
593static inline void pch_udc_ep_clear_stall(struct pch_udc_ep *ep)
594{
595 /* Clear the stall */
596 pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
597 /* Clear NAK by writing CNAK */
598 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
599}
600
601/**
602 * pch_udc_ep_set_trfr_type() - Set the transfer type of endpoint
603 * @ep: Reference to structure of type pch_udc_ep_regs
604 * @type: Type of endpoint
605 */
606static inline void pch_udc_ep_set_trfr_type(struct pch_udc_ep *ep,
607 u8 type)
608{
609 pch_udc_ep_writel(ep, ((type << UDC_EPCTL_ET_SHIFT) &
610 UDC_EPCTL_ET_MASK), UDC_EPCTL_ADDR);
611}
612
613/**
614 * pch_udc_ep_set_bufsz() - Set the maximum packet size for the endpoint
615 * @ep: Reference to structure of type pch_udc_ep_regs
616 * @buf_size: The buffer size
617 */
618static void pch_udc_ep_set_bufsz(struct pch_udc_ep *ep,
619 u32 buf_size, u32 ep_in)
620{
621 u32 data;
622 if (ep_in) {
623 data = pch_udc_ep_readl(ep, UDC_BUFIN_FRAMENUM_ADDR);
624 data = (data & 0xffff0000) | (buf_size & 0xffff);
625 pch_udc_ep_writel(ep, data, UDC_BUFIN_FRAMENUM_ADDR);
626 } else {
627 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
628 data = (buf_size << 16) | (data & 0xffff);
629 pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
630 }
631}
632
633/**
634 * pch_udc_ep_set_maxpkt() - Set the Max packet size for the endpoint
635 * @ep: Reference to structure of type pch_udc_ep_regs
636 * @pkt_size: The packet size
637 */
638static void pch_udc_ep_set_maxpkt(struct pch_udc_ep *ep, u32 pkt_size)
639{
640 u32 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
641 data = (data & 0xffff0000) | (pkt_size & 0xffff);
642 pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
643}
644
645/**
646 * pch_udc_ep_set_subptr() - Set the Setup buffer pointer for the endpoint
647 * @ep: Reference to structure of type pch_udc_ep_regs
648 * @addr: Address of the register
649 */
650static inline void pch_udc_ep_set_subptr(struct pch_udc_ep *ep, u32 addr)
651{
652 pch_udc_ep_writel(ep, addr, UDC_SUBPTR_ADDR);
653}
654
655/**
656 * pch_udc_ep_set_ddptr() - Set the Data descriptor pointer for the endpoint
657 * @ep: Reference to structure of type pch_udc_ep_regs
658 * @addr: Address of the register
659 */
660static inline void pch_udc_ep_set_ddptr(struct pch_udc_ep *ep, u32 addr)
661{
662 pch_udc_ep_writel(ep, addr, UDC_DESPTR_ADDR);
663}
664
665/**
666 * pch_udc_ep_set_pd() - Set the poll demand bit for the endpoint
667 * @ep: Reference to structure of type pch_udc_ep_regs
668 */
669static inline void pch_udc_ep_set_pd(struct pch_udc_ep *ep)
670{
671 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_P);
672}
673
674/**
675 * pch_udc_ep_set_rrdy() - Set the receive ready bit for the endpoint
676 * @ep: Reference to structure of type pch_udc_ep_regs
677 */
678static inline void pch_udc_ep_set_rrdy(struct pch_udc_ep *ep)
679{
680 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
681}
682
683/**
684 * pch_udc_ep_clear_rrdy() - Clear the receive ready bit for the endpoint
685 * @ep: Reference to structure of type pch_udc_ep_regs
686 */
687static inline void pch_udc_ep_clear_rrdy(struct pch_udc_ep *ep)
688{
689 pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
690}
691
692/**
693 * pch_udc_set_dma() - Set the 'TDE' or RDE bit of device control
694 * register depending on the direction specified
695 * @dev: Reference to structure of type pch_udc_regs
696 * @dir: whether Tx or Rx
697 * DMA_DIR_RX: Receive
698 * DMA_DIR_TX: Transmit
699 */
700static inline void pch_udc_set_dma(struct pch_udc_dev *dev, int dir)
701{
702 if (dir == DMA_DIR_RX)
703 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
704 else if (dir == DMA_DIR_TX)
705 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
706}
707
708/**
709 * pch_udc_clear_dma() - Clear the 'TDE' or RDE bit of device control
710 * register depending on the direction specified
711 * @dev: Reference to structure of type pch_udc_regs
712 * @dir: Whether Tx or Rx
713 * DMA_DIR_RX: Receive
714 * DMA_DIR_TX: Transmit
715 */
716static inline void pch_udc_clear_dma(struct pch_udc_dev *dev, int dir)
717{
718 if (dir == DMA_DIR_RX)
719 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
720 else if (dir == DMA_DIR_TX)
721 pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
722}
723
724/**
725 * pch_udc_set_csr_done() - Set the device control register
726 * CSR done field (bit 13)
727 * @dev: reference to structure of type pch_udc_regs
728 */
729static inline void pch_udc_set_csr_done(struct pch_udc_dev *dev)
730{
731 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_CSR_DONE);
732}
733
734/**
735 * pch_udc_disable_interrupts() - Disables the specified interrupts
736 * @dev: Reference to structure of type pch_udc_regs
737 * @mask: Mask to disable interrupts
738 */
739static inline void pch_udc_disable_interrupts(struct pch_udc_dev *dev,
740 u32 mask)
741{
742 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, mask);
743}
744
745/**
746 * pch_udc_enable_interrupts() - Enable the specified interrupts
747 * @dev: Reference to structure of type pch_udc_regs
748 * @mask: Mask to enable interrupts
749 */
750static inline void pch_udc_enable_interrupts(struct pch_udc_dev *dev,
751 u32 mask)
752{
753 pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR, mask);
754}
755
756/**
757 * pch_udc_disable_ep_interrupts() - Disable endpoint interrupts
758 * @dev: Reference to structure of type pch_udc_regs
759 * @mask: Mask to disable interrupts
760 */
761static inline void pch_udc_disable_ep_interrupts(struct pch_udc_dev *dev,
762 u32 mask)
763{
764 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, mask);
765}
766
767/**
768 * pch_udc_enable_ep_interrupts() - Enable endpoint interrupts
769 * @dev: Reference to structure of type pch_udc_regs
770 * @mask: Mask to enable interrupts
771 */
772static inline void pch_udc_enable_ep_interrupts(struct pch_udc_dev *dev,
773 u32 mask)
774{
775 pch_udc_bit_clr(dev, UDC_EPIRQMSK_ADDR, mask);
776}
777
778/**
779 * pch_udc_read_device_interrupts() - Read the device interrupts
780 * @dev: Reference to structure of type pch_udc_regs
781 * Retern The device interrupts
782 */
783static inline u32 pch_udc_read_device_interrupts(struct pch_udc_dev *dev)
784{
785 return pch_udc_readl(dev, UDC_DEVIRQSTS_ADDR);
786}
787
788/**
789 * pch_udc_write_device_interrupts() - Write device interrupts
790 * @dev: Reference to structure of type pch_udc_regs
791 * @val: The value to be written to interrupt register
792 */
793static inline void pch_udc_write_device_interrupts(struct pch_udc_dev *dev,
794 u32 val)
795{
796 pch_udc_writel(dev, val, UDC_DEVIRQSTS_ADDR);
797}
798
799/**
800 * pch_udc_read_ep_interrupts() - Read the endpoint interrupts
801 * @dev: Reference to structure of type pch_udc_regs
802 * Retern The endpoint interrupt
803 */
804static inline u32 pch_udc_read_ep_interrupts(struct pch_udc_dev *dev)
805{
806 return pch_udc_readl(dev, UDC_EPIRQSTS_ADDR);
807}
808
809/**
810 * pch_udc_write_ep_interrupts() - Clear endpoint interupts
811 * @dev: Reference to structure of type pch_udc_regs
812 * @val: The value to be written to interrupt register
813 */
814static inline void pch_udc_write_ep_interrupts(struct pch_udc_dev *dev,
815 u32 val)
816{
817 pch_udc_writel(dev, val, UDC_EPIRQSTS_ADDR);
818}
819
820/**
821 * pch_udc_read_device_status() - Read the device status
822 * @dev: Reference to structure of type pch_udc_regs
823 * Retern The device status
824 */
825static inline u32 pch_udc_read_device_status(struct pch_udc_dev *dev)
826{
827 return pch_udc_readl(dev, UDC_DEVSTS_ADDR);
828}
829
830/**
831 * pch_udc_read_ep_control() - Read the endpoint control
832 * @ep: Reference to structure of type pch_udc_ep_regs
833 * Retern The endpoint control register value
834 */
835static inline u32 pch_udc_read_ep_control(struct pch_udc_ep *ep)
836{
837 return pch_udc_ep_readl(ep, UDC_EPCTL_ADDR);
838}
839
840/**
841 * pch_udc_clear_ep_control() - Clear the endpoint control register
842 * @ep: Reference to structure of type pch_udc_ep_regs
843 * Retern The endpoint control register value
844 */
845static inline void pch_udc_clear_ep_control(struct pch_udc_ep *ep)
846{
847 return pch_udc_ep_writel(ep, 0, UDC_EPCTL_ADDR);
848}
849
850/**
851 * pch_udc_read_ep_status() - Read the endpoint status
852 * @ep: Reference to structure of type pch_udc_ep_regs
853 * Retern The endpoint status
854 */
855static inline u32 pch_udc_read_ep_status(struct pch_udc_ep *ep)
856{
857 return pch_udc_ep_readl(ep, UDC_EPSTS_ADDR);
858}
859
860/**
861 * pch_udc_clear_ep_status() - Clear the endpoint status
862 * @ep: Reference to structure of type pch_udc_ep_regs
863 * @stat: Endpoint status
864 */
865static inline void pch_udc_clear_ep_status(struct pch_udc_ep *ep,
866 u32 stat)
867{
868 return pch_udc_ep_writel(ep, stat, UDC_EPSTS_ADDR);
869}
870
871/**
872 * pch_udc_ep_set_nak() - Set the bit 7 (SNAK field)
873 * of the endpoint control register
874 * @ep: Reference to structure of type pch_udc_ep_regs
875 */
876static inline void pch_udc_ep_set_nak(struct pch_udc_ep *ep)
877{
878 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_SNAK);
879}
880
881/**
882 * pch_udc_ep_clear_nak() - Set the bit 8 (CNAK field)
883 * of the endpoint control register
884 * @ep: reference to structure of type pch_udc_ep_regs
885 */
886static void pch_udc_ep_clear_nak(struct pch_udc_ep *ep)
887{
888 unsigned int loopcnt = 0;
889 struct pch_udc_dev *dev = ep->dev;
890
891 if (!(pch_udc_ep_readl(ep, UDC_EPCTL_ADDR) & UDC_EPCTL_NAK))
892 return;
893 if (!ep->in) {
894 loopcnt = 10000;
895 while (!(pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP) &&
896 --loopcnt)
897 udelay(5);
898 if (!loopcnt)
899 dev_err(&dev->pdev->dev, "%s: RxFIFO not Empty\n",
900 __func__);
901 }
902 loopcnt = 10000;
903 while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_NAK) && --loopcnt) {
904 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
905 udelay(5);
906 }
907 if (!loopcnt)
908 dev_err(&dev->pdev->dev, "%s: Clear NAK not set for ep%d%s\n",
909 __func__, ep->num, (ep->in ? "in" : "out"));
910}
911
912/**
913 * pch_udc_ep_fifo_flush() - Flush the endpoint fifo
914 * @ep: reference to structure of type pch_udc_ep_regs
915 * @dir: direction of endpoint
916 * 0: endpoint is OUT
917 * !0: endpoint is IN
918 */
919static void pch_udc_ep_fifo_flush(struct pch_udc_ep *ep, int dir)
920{
921 unsigned int loopcnt = 0;
922 struct pch_udc_dev *dev = ep->dev;
923
924 if (dir) { /* IN ep */
925 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
926 return;
927 }
928
929 if (pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP)
930 return;
931 pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_MRXFLUSH);
932 /* Wait for RxFIFO Empty */
933 loopcnt = 10000;
934 while (!(pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP) &&
935 --loopcnt)
936 udelay(5);
937 if (!loopcnt)
938 dev_err(&dev->pdev->dev, "RxFIFO not Empty\n");
939 pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_MRXFLUSH);
940}
941
942/**
943 * pch_udc_ep_enable() - This api enables endpoint
944 * @regs: Reference to structure pch_udc_ep_regs
945 * @desc: endpoint descriptor
946 */
947static void pch_udc_ep_enable(struct pch_udc_ep *ep,
948 struct pch_udc_cfg_data *cfg,
949 const struct usb_endpoint_descriptor *desc)
950{
951 u32 val = 0;
952 u32 buff_size = 0;
953
954 pch_udc_ep_set_trfr_type(ep, desc->bmAttributes);
955 if (ep->in)
956 buff_size = UDC_EPIN_BUFF_SIZE;
957 else
958 buff_size = UDC_EPOUT_BUFF_SIZE;
959 pch_udc_ep_set_bufsz(ep, buff_size, ep->in);
960 pch_udc_ep_set_maxpkt(ep, le16_to_cpu(desc->wMaxPacketSize));
961 pch_udc_ep_set_nak(ep);
962 pch_udc_ep_fifo_flush(ep, ep->in);
963 /* Configure the endpoint */
964 val = ep->num << UDC_CSR_NE_NUM_SHIFT | ep->in << UDC_CSR_NE_DIR_SHIFT |
965 ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) <<
966 UDC_CSR_NE_TYPE_SHIFT) |
967 (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) |
968 (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) |
969 (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) |
970 le16_to_cpu(desc->wMaxPacketSize) << UDC_CSR_NE_MAX_PKT_SHIFT;
971
972 if (ep->in)
973 pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num));
974 else
975 pch_udc_write_csr(ep->dev, val, UDC_EPOUT_IDX(ep->num));
976}
977
978/**
979 * pch_udc_ep_disable() - This api disables endpoint
980 * @regs: Reference to structure pch_udc_ep_regs
981 */
982static void pch_udc_ep_disable(struct pch_udc_ep *ep)
983{
984 if (ep->in) {
985 /* flush the fifo */
986 pch_udc_ep_writel(ep, UDC_EPCTL_F, UDC_EPCTL_ADDR);
987 /* set NAK */
988 pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
989 pch_udc_ep_bit_set(ep, UDC_EPSTS_ADDR, UDC_EPSTS_IN);
990 } else {
991 /* set NAK */
992 pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
993 }
994 /* reset desc pointer */
995 pch_udc_ep_writel(ep, 0, UDC_DESPTR_ADDR);
996}
997
998/**
999 * pch_udc_wait_ep_stall() - Wait EP stall.
1000 * @dev: Reference to pch_udc_dev structure
1001 */
1002static void pch_udc_wait_ep_stall(struct pch_udc_ep *ep)
1003{
1004 unsigned int count = 10000;
1005
1006 /* Wait till idle */
1007 while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_S) && --count)
1008 udelay(5);
1009 if (!count)
1010 dev_err(&ep->dev->pdev->dev, "%s: wait error\n", __func__);
1011}
1012
1013/**
1014 * pch_udc_init() - This API initializes usb device controller
1015 * @dev: Rreference to pch_udc_regs structure
1016 */
1017static void pch_udc_init(struct pch_udc_dev *dev)
1018{
1019 if (NULL == dev) {
1020 pr_err("%s: Invalid address\n", __func__);
1021 return;
1022 }
1023 /* Soft Reset and Reset PHY */
1024 pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
1025 pch_udc_writel(dev, UDC_SRST | UDC_PSRST, UDC_SRST_ADDR);
1026 mdelay(1);
1027 pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
1028 pch_udc_writel(dev, 0x00, UDC_SRST_ADDR);
1029 mdelay(1);
1030 /* mask and clear all device interrupts */
1031 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
1032 pch_udc_bit_set(dev, UDC_DEVIRQSTS_ADDR, UDC_DEVINT_MSK);
1033
1034 /* mask and clear all ep interrupts */
1035 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1036 pch_udc_bit_set(dev, UDC_EPIRQSTS_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1037
1038 /* enable dynamic CSR programmingi, self powered and device speed */
1039 if (speed_fs)
1040 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
1041 UDC_DEVCFG_SP | UDC_DEVCFG_SPD_FS);
1042 else /* defaul high speed */
1043 pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
1044 UDC_DEVCFG_SP | UDC_DEVCFG_SPD_HS);
1045 pch_udc_bit_set(dev, UDC_DEVCTL_ADDR,
1046 (PCH_UDC_THLEN << UDC_DEVCTL_THLEN_SHIFT) |
1047 (PCH_UDC_BRLEN << UDC_DEVCTL_BRLEN_SHIFT) |
1048 UDC_DEVCTL_MODE | UDC_DEVCTL_BREN |
1049 UDC_DEVCTL_THE);
1050}
1051
1052/**
1053 * pch_udc_exit() - This API exit usb device controller
1054 * @dev: Reference to pch_udc_regs structure
1055 */
1056static void pch_udc_exit(struct pch_udc_dev *dev)
1057{
1058 /* mask all device interrupts */
1059 pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
1060 /* mask all ep interrupts */
1061 pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
1062 /* put device in disconnected state */
1063 pch_udc_set_disconnect(dev);
1064}
1065
1066/**
1067 * pch_udc_pcd_get_frame() - This API is invoked to get the current frame number
1068 * @gadget: Reference to the gadget driver
1069 *
1070 * Return codes:
1071 * 0: Success
1072 * -EINVAL: If the gadget passed is NULL
1073 */
1074static int pch_udc_pcd_get_frame(struct usb_gadget *gadget)
1075{
1076 struct pch_udc_dev *dev;
1077
1078 if (!gadget)
1079 return -EINVAL;
1080 dev = container_of(gadget, struct pch_udc_dev, gadget);
1081 return pch_udc_get_frame(dev);
1082}
1083
1084/**
1085 * pch_udc_pcd_wakeup() - This API is invoked to initiate a remote wakeup
1086 * @gadget: Reference to the gadget driver
1087 *
1088 * Return codes:
1089 * 0: Success
1090 * -EINVAL: If the gadget passed is NULL
1091 */
1092static int pch_udc_pcd_wakeup(struct usb_gadget *gadget)
1093{
1094 struct pch_udc_dev *dev;
1095 unsigned long flags;
1096
1097 if (!gadget)
1098 return -EINVAL;
1099 dev = container_of(gadget, struct pch_udc_dev, gadget);
1100 spin_lock_irqsave(&dev->lock, flags);
1101 pch_udc_rmt_wakeup(dev);
1102 spin_unlock_irqrestore(&dev->lock, flags);
1103 return 0;
1104}
1105
1106/**
1107 * pch_udc_pcd_selfpowered() - This API is invoked to specify whether the device
1108 * is self powered or not
1109 * @gadget: Reference to the gadget driver
1110 * @value: Specifies self powered or not
1111 *
1112 * Return codes:
1113 * 0: Success
1114 * -EINVAL: If the gadget passed is NULL
1115 */
1116static int pch_udc_pcd_selfpowered(struct usb_gadget *gadget, int value)
1117{
1118 struct pch_udc_dev *dev;
1119
1120 if (!gadget)
1121 return -EINVAL;
1122 dev = container_of(gadget, struct pch_udc_dev, gadget);
1123 if (value)
1124 pch_udc_set_selfpowered(dev);
1125 else
1126 pch_udc_clear_selfpowered(dev);
1127 return 0;
1128}
1129
1130/**
1131 * pch_udc_pcd_pullup() - This API is invoked to make the device
1132 * visible/invisible to the host
1133 * @gadget: Reference to the gadget driver
1134 * @is_on: Specifies whether the pull up is made active or inactive
1135 *
1136 * Return codes:
1137 * 0: Success
1138 * -EINVAL: If the gadget passed is NULL
1139 */
1140static int pch_udc_pcd_pullup(struct usb_gadget *gadget, int is_on)
1141{
1142 struct pch_udc_dev *dev;
1143
1144 if (!gadget)
1145 return -EINVAL;
1146 dev = container_of(gadget, struct pch_udc_dev, gadget);
1147 pch_udc_vbus_session(dev, is_on);
1148 return 0;
1149}
1150
1151/**
1152 * pch_udc_pcd_vbus_session() - This API is used by a driver for an external
1153 * transceiver (or GPIO) that
1154 * detects a VBUS power session starting/ending
1155 * @gadget: Reference to the gadget driver
1156 * @is_active: specifies whether the session is starting or ending
1157 *
1158 * Return codes:
1159 * 0: Success
1160 * -EINVAL: If the gadget passed is NULL
1161 */
1162static int pch_udc_pcd_vbus_session(struct usb_gadget *gadget, int is_active)
1163{
1164 struct pch_udc_dev *dev;
1165
1166 if (!gadget)
1167 return -EINVAL;
1168 dev = container_of(gadget, struct pch_udc_dev, gadget);
1169 pch_udc_vbus_session(dev, is_active);
1170 return 0;
1171}
1172
1173/**
1174 * pch_udc_pcd_vbus_draw() - This API is used by gadget drivers during
1175 * SET_CONFIGURATION calls to
1176 * specify how much power the device can consume
1177 * @gadget: Reference to the gadget driver
1178 * @mA: specifies the current limit in 2mA unit
1179 *
1180 * Return codes:
1181 * -EINVAL: If the gadget passed is NULL
1182 * -EOPNOTSUPP:
1183 */
1184static int pch_udc_pcd_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
1185{
1186 return -EOPNOTSUPP;
1187}
1188
1189static const struct usb_gadget_ops pch_udc_ops = {
1190 .get_frame = pch_udc_pcd_get_frame,
1191 .wakeup = pch_udc_pcd_wakeup,
1192 .set_selfpowered = pch_udc_pcd_selfpowered,
1193 .pullup = pch_udc_pcd_pullup,
1194 .vbus_session = pch_udc_pcd_vbus_session,
1195 .vbus_draw = pch_udc_pcd_vbus_draw,
1196};
1197
1198/**
1199 * complete_req() - This API is invoked from the driver when processing
1200 * of a request is complete
1201 * @ep: Reference to the endpoint structure
1202 * @req: Reference to the request structure
1203 * @status: Indicates the success/failure of completion
1204 */
1205static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req,
1206 int status)
1207{
1208 struct pch_udc_dev *dev;
1209 unsigned halted = ep->halted;
1210
1211 list_del_init(&req->queue);
1212
1213 /* set new status if pending */
1214 if (req->req.status == -EINPROGRESS)
1215 req->req.status = status;
1216 else
1217 status = req->req.status;
1218
1219 dev = ep->dev;
1220 if (req->dma_mapped) {
1221 if (ep->in)
1222 pci_unmap_single(dev->pdev, req->req.dma,
1223 req->req.length, PCI_DMA_TODEVICE);
1224 else
1225 pci_unmap_single(dev->pdev, req->req.dma,
1226 req->req.length, PCI_DMA_FROMDEVICE);
1227 req->dma_mapped = 0;
1228 req->req.dma = DMA_ADDR_INVALID;
1229 }
1230 ep->halted = 1;
1231 spin_unlock(&dev->lock);
1232 if (!ep->in)
1233 pch_udc_ep_clear_rrdy(ep);
1234 req->req.complete(&ep->ep, &req->req);
1235 spin_lock(&dev->lock);
1236 ep->halted = halted;
1237}
1238
1239/**
1240 * empty_req_queue() - This API empties the request queue of an endpoint
1241 * @ep: Reference to the endpoint structure
1242 */
1243static void empty_req_queue(struct pch_udc_ep *ep)
1244{
1245 struct pch_udc_request *req;
1246
1247 ep->halted = 1;
1248 while (!list_empty(&ep->queue)) {
1249 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1250 complete_req(ep, req, -ESHUTDOWN); /* Remove from list */
1251 }
1252}
1253
1254/**
1255 * pch_udc_free_dma_chain() - This function frees the DMA chain created
1256 * for the request
1257 * @dev Reference to the driver structure
1258 * @req Reference to the request to be freed
1259 *
1260 * Return codes:
1261 * 0: Success
1262 */
1263static void pch_udc_free_dma_chain(struct pch_udc_dev *dev,
1264 struct pch_udc_request *req)
1265{
1266 struct pch_udc_data_dma_desc *td = req->td_data;
1267 unsigned i = req->chain_len;
1268
1269 for (; i > 1; --i) {
1270 dma_addr_t addr = (dma_addr_t)td->next;
1271 /* do not free first desc., will be done by free for request */
1272 td = phys_to_virt(addr);
1273 pci_pool_free(dev->data_requests, td, addr);
1274 }
1275}
1276
1277/**
1278 * pch_udc_create_dma_chain() - This function creates or reinitializes
1279 * a DMA chain
1280 * @ep: Reference to the endpoint structure
1281 * @req: Reference to the request
1282 * @buf_len: The buffer length
1283 * @gfp_flags: Flags to be used while mapping the data buffer
1284 *
1285 * Return codes:
1286 * 0: success,
1287 * -ENOMEM: pci_pool_alloc invocation fails
1288 */
1289static int pch_udc_create_dma_chain(struct pch_udc_ep *ep,
1290 struct pch_udc_request *req,
1291 unsigned long buf_len,
1292 gfp_t gfp_flags)
1293{
1294 struct pch_udc_data_dma_desc *td = req->td_data, *last;
1295 unsigned long bytes = req->req.length, i = 0;
1296 dma_addr_t dma_addr;
1297 unsigned len = 1;
1298
1299 if (req->chain_len > 1)
1300 pch_udc_free_dma_chain(ep->dev, req);
1301
1302 for (; ; bytes -= buf_len, ++len) {
1303 if (ep->in)
1304 td->status = PCH_UDC_BS_HST_BSY | min(buf_len, bytes);
1305 else
1306 td->status = PCH_UDC_BS_HST_BSY;
1307
1308 if (bytes <= buf_len)
1309 break;
1310
1311 last = td;
1312 td = pci_pool_alloc(ep->dev->data_requests, gfp_flags,
1313 &dma_addr);
1314 if (!td)
1315 goto nomem;
1316
1317 i += buf_len;
1318 td->dataptr = req->req.dma + i;
1319 last->next = dma_addr;
1320 }
1321
1322 req->td_data_last = td;
1323 td->status |= PCH_UDC_DMA_LAST;
1324 td->next = req->td_data_phys;
1325 req->chain_len = len;
1326 return 0;
1327
1328nomem:
1329 if (len > 1) {
1330 req->chain_len = len;
1331 pch_udc_free_dma_chain(ep->dev, req);
1332 }
1333 req->chain_len = 1;
1334 return -ENOMEM;
1335}
1336
1337/**
1338 * prepare_dma() - This function creates and initializes the DMA chain
1339 * for the request
1340 * @ep: Reference to the endpoint structure
1341 * @req: Reference to the request
1342 * @gfp: Flag to be used while mapping the data buffer
1343 *
1344 * Return codes:
1345 * 0: Success
1346 * Other 0: linux error number on failure
1347 */
1348static int prepare_dma(struct pch_udc_ep *ep, struct pch_udc_request *req,
1349 gfp_t gfp)
1350{
1351 int retval;
1352
1353 req->td_data->dataptr = req->req.dma;
1354 req->td_data->status |= PCH_UDC_DMA_LAST;
1355 /* Allocate and create a DMA chain */
1356 retval = pch_udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp);
1357 if (retval) {
1358 pr_err("%s: could not create DMA chain: %d\n",
1359 __func__, retval);
1360 return retval;
1361 }
1362 if (!ep->in)
1363 return 0;
1364 if (req->req.length <= ep->ep.maxpacket)
1365 req->td_data->status = PCH_UDC_DMA_LAST | PCH_UDC_BS_HST_BSY |
1366 req->req.length;
1367 /* if bytes < max packet then tx bytes must
1368 * be written in packet per buffer mode
1369 */
1370 if ((req->req.length < ep->ep.maxpacket) || !ep->num)
1371 req->td_data->status = (req->td_data->status &
1372 ~PCH_UDC_RXTX_BYTES) | req->req.length;
1373 req->td_data->status = (req->td_data->status &
1374 ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_BSY;
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 ep->td_data = req->td_data;
1418 /* Set the status bits for all descriptors */
1419 while (1) {
1420 td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
1421 PCH_UDC_BS_HST_RDY;
1422 if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
1423 break;
1424 td_data = phys_to_virt(td_data->next);
1425 }
1426 /* Write the descriptor pointer */
1427 pch_udc_ep_set_ddptr(ep, req->td_data_phys);
1428 req->dma_going = 1;
1429 pch_udc_enable_ep_interrupts(ep->dev, UDC_EPINT_OUT_EP0 << ep->num);
1430 pch_udc_set_dma(ep->dev, DMA_DIR_RX);
1431 pch_udc_ep_clear_nak(ep);
1432 pch_udc_ep_set_rrdy(ep);
1433}
1434
1435/**
1436 * pch_udc_pcd_ep_enable() - This API enables the endpoint. It is called
1437 * from gadget driver
1438 * @usbep: Reference to the USB endpoint structure
1439 * @desc: Reference to the USB endpoint descriptor structure
1440 *
1441 * Return codes:
1442 * 0: Success
1443 * -EINVAL:
1444 * -ESHUTDOWN:
1445 */
1446static int pch_udc_pcd_ep_enable(struct usb_ep *usbep,
1447 const struct usb_endpoint_descriptor *desc)
1448{
1449 struct pch_udc_ep *ep;
1450 struct pch_udc_dev *dev;
1451 unsigned long iflags;
1452
1453 if (!usbep || (usbep->name == ep0_string) || !desc ||
1454 (desc->bDescriptorType != USB_DT_ENDPOINT) || !desc->wMaxPacketSize)
1455 return -EINVAL;
1456
1457 ep = container_of(usbep, struct pch_udc_ep, ep);
1458 dev = ep->dev;
1459 if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN))
1460 return -ESHUTDOWN;
1461 spin_lock_irqsave(&dev->lock, iflags);
1462 ep->desc = desc;
1463 ep->halted = 0;
1464 pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc);
1465 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1466 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1467 spin_unlock_irqrestore(&dev->lock, iflags);
1468 return 0;
1469}
1470
1471/**
1472 * pch_udc_pcd_ep_disable() - This API disables endpoint and is called
1473 * from gadget driver
1474 * @usbep Reference to the USB endpoint structure
1475 *
1476 * Return codes:
1477 * 0: Success
1478 * -EINVAL:
1479 */
1480static int pch_udc_pcd_ep_disable(struct usb_ep *usbep)
1481{
1482 struct pch_udc_ep *ep;
1483 struct pch_udc_dev *dev;
1484 unsigned long iflags;
1485
1486 if (!usbep)
1487 return -EINVAL;
1488
1489 ep = container_of(usbep, struct pch_udc_ep, ep);
1490 dev = ep->dev;
1491 if ((usbep->name == ep0_string) || !ep->desc)
1492 return -EINVAL;
1493
1494 spin_lock_irqsave(&ep->dev->lock, iflags);
1495 empty_req_queue(ep);
1496 ep->halted = 1;
1497 pch_udc_ep_disable(ep);
1498 pch_udc_disable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1499 ep->desc = NULL;
1500 INIT_LIST_HEAD(&ep->queue);
1501 spin_unlock_irqrestore(&ep->dev->lock, iflags);
1502 return 0;
1503}
1504
1505/**
1506 * pch_udc_alloc_request() - This function allocates request structure.
1507 * It is called by gadget driver
1508 * @usbep: Reference to the USB endpoint structure
1509 * @gfp: Flag to be used while allocating memory
1510 *
1511 * Return codes:
1512 * NULL: Failure
1513 * Allocated address: Success
1514 */
1515static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
1516 gfp_t gfp)
1517{
1518 struct pch_udc_request *req;
1519 struct pch_udc_ep *ep;
1520 struct pch_udc_data_dma_desc *dma_desc;
1521 struct pch_udc_dev *dev;
1522
1523 if (!usbep)
1524 return NULL;
1525 ep = container_of(usbep, struct pch_udc_ep, ep);
1526 dev = ep->dev;
1527 req = kzalloc(sizeof *req, gfp);
1528 if (!req)
1529 return NULL;
1530 req->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(&ep->dev->lock, iflags);
1612 /* map the buffer for dma */
1613 if (usbreq->length &&
1614 ((usbreq->dma == DMA_ADDR_INVALID) || !usbreq->dma)) {
1615 if (ep->in)
1616 usbreq->dma = pci_map_single(dev->pdev, usbreq->buf,
1617 usbreq->length, PCI_DMA_TODEVICE);
1618 else
1619 usbreq->dma = pci_map_single(dev->pdev, usbreq->buf,
1620 usbreq->length, PCI_DMA_FROMDEVICE);
1621 req->dma_mapped = 1;
1622 }
1623 if (usbreq->length > 0) {
1624 retval = prepare_dma(ep, req, gfp);
1625 if (retval)
1626 goto probe_end;
1627 }
1628 usbreq->actual = 0;
1629 usbreq->status = -EINPROGRESS;
1630 req->dma_done = 0;
1631 if (list_empty(&ep->queue) && !ep->halted) {
1632 /* no pending transfer, so start this req */
1633 if (!usbreq->length) {
1634 process_zlp(ep, req);
1635 retval = 0;
1636 goto probe_end;
1637 }
1638 if (!ep->in) {
1639 pch_udc_start_rxrequest(ep, req);
1640 } else {
1641 /*
1642 * For IN trfr the descriptors will be programmed and
1643 * P bit will be set when
1644 * we get an IN token
1645 */
1646 pch_udc_wait_ep_stall(ep);
1647 pch_udc_ep_clear_nak(ep);
1648 pch_udc_enable_ep_interrupts(ep->dev, (1 << ep->num));
1649 pch_udc_set_dma(dev, DMA_DIR_TX);
1650 }
1651 }
1652 /* Now add this request to the ep's pending requests */
1653 if (req != NULL)
1654 list_add_tail(&req->queue, &ep->queue);
1655
1656probe_end:
1657 spin_unlock_irqrestore(&dev->lock, iflags);
1658 return retval;
1659}
1660
1661/**
1662 * pch_udc_pcd_dequeue() - This function de-queues a request packet.
1663 * It is called by gadget driver
1664 * @usbep: Reference to the USB endpoint structure
1665 * @usbreq: Reference to the USB request
1666 *
1667 * Return codes:
1668 * 0: Success
1669 * linux error number: Failure
1670 */
1671static int pch_udc_pcd_dequeue(struct usb_ep *usbep,
1672 struct usb_request *usbreq)
1673{
1674 struct pch_udc_ep *ep;
1675 struct pch_udc_request *req;
1676 struct pch_udc_dev *dev;
1677 unsigned long flags;
1678 int ret = -EINVAL;
1679
1680 ep = container_of(usbep, struct pch_udc_ep, ep);
1681 dev = ep->dev;
1682 if (!usbep || !usbreq || (!ep->desc && ep->num))
1683 return ret;
1684 req = container_of(usbreq, struct pch_udc_request, req);
1685 spin_lock_irqsave(&ep->dev->lock, flags);
1686 /* make sure it's still queued on this endpoint */
1687 list_for_each_entry(req, &ep->queue, queue) {
1688 if (&req->req == usbreq) {
1689 pch_udc_ep_set_nak(ep);
1690 if (!list_empty(&req->queue))
1691 complete_req(ep, req, -ECONNRESET);
1692 ret = 0;
1693 break;
1694 }
1695 }
1696 spin_unlock_irqrestore(&ep->dev->lock, flags);
1697 return ret;
1698}
1699
1700/**
1701 * pch_udc_pcd_set_halt() - This function Sets or clear the endpoint halt
1702 * feature
1703 * @usbep: Reference to the USB endpoint structure
1704 * @halt: Specifies whether to set or clear the feature
1705 *
1706 * Return codes:
1707 * 0: Success
1708 * linux error number: Failure
1709 */
1710static int pch_udc_pcd_set_halt(struct usb_ep *usbep, int halt)
1711{
1712 struct pch_udc_ep *ep;
1713 struct pch_udc_dev *dev;
1714 unsigned long iflags;
1715 int ret;
1716
1717 if (!usbep)
1718 return -EINVAL;
1719 ep = container_of(usbep, struct pch_udc_ep, ep);
1720 dev = ep->dev;
1721 if (!ep->desc && !ep->num)
1722 return -EINVAL;
1723 if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
1724 return -ESHUTDOWN;
1725 spin_lock_irqsave(&udc_stall_spinlock, iflags);
1726 if (list_empty(&ep->queue)) {
1727 if (halt) {
1728 if (ep->num == PCH_UDC_EP0)
1729 ep->dev->stall = 1;
1730 pch_udc_ep_set_stall(ep);
1731 pch_udc_enable_ep_interrupts(ep->dev,
1732 PCH_UDC_EPINT(ep->in,
1733 ep->num));
1734 } else {
1735 pch_udc_ep_clear_stall(ep);
1736 }
1737 ret = 0;
1738 } else {
1739 ret = -EAGAIN;
1740 }
1741 spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
1742 return ret;
1743}
1744
1745/**
1746 * pch_udc_pcd_set_wedge() - This function Sets or clear the endpoint
1747 * halt feature
1748 * @usbep: Reference to the USB endpoint structure
1749 * @halt: Specifies whether to set or clear the feature
1750 *
1751 * Return codes:
1752 * 0: Success
1753 * linux error number: Failure
1754 */
1755static int pch_udc_pcd_set_wedge(struct usb_ep *usbep)
1756{
1757 struct pch_udc_ep *ep;
1758 struct pch_udc_dev *dev;
1759 unsigned long iflags;
1760 int ret;
1761
1762 if (!usbep)
1763 return -EINVAL;
1764 ep = container_of(usbep, struct pch_udc_ep, ep);
1765 dev = ep->dev;
1766 if (!ep->desc && !ep->num)
1767 return -EINVAL;
1768 if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
1769 return -ESHUTDOWN;
1770 spin_lock_irqsave(&udc_stall_spinlock, iflags);
1771 if (!list_empty(&ep->queue)) {
1772 ret = -EAGAIN;
1773 } else {
1774 if (ep->num == PCH_UDC_EP0)
1775 ep->dev->stall = 1;
1776 pch_udc_ep_set_stall(ep);
1777 pch_udc_enable_ep_interrupts(ep->dev,
1778 PCH_UDC_EPINT(ep->in, ep->num));
1779 ep->dev->prot_stall = 1;
1780 ret = 0;
1781 }
1782 spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
1783 return ret;
1784}
1785
1786/**
1787 * pch_udc_pcd_fifo_flush() - This function Flush the FIFO of specified endpoint
1788 * @usbep: Reference to the USB endpoint structure
1789 */
1790static void pch_udc_pcd_fifo_flush(struct usb_ep *usbep)
1791{
1792 struct pch_udc_ep *ep;
1793
1794 if (!usbep)
1795 return;
1796
1797 ep = container_of(usbep, struct pch_udc_ep, ep);
1798 if (ep->desc || !ep->num)
1799 pch_udc_ep_fifo_flush(ep, ep->in);
1800}
1801
1802static const struct usb_ep_ops pch_udc_ep_ops = {
1803 .enable = pch_udc_pcd_ep_enable,
1804 .disable = pch_udc_pcd_ep_disable,
1805 .alloc_request = pch_udc_alloc_request,
1806 .free_request = pch_udc_free_request,
1807 .queue = pch_udc_pcd_queue,
1808 .dequeue = pch_udc_pcd_dequeue,
1809 .set_halt = pch_udc_pcd_set_halt,
1810 .set_wedge = pch_udc_pcd_set_wedge,
1811 .fifo_status = NULL,
1812 .fifo_flush = pch_udc_pcd_fifo_flush,
1813};
1814
1815/**
1816 * pch_udc_init_setup_buff() - This function initializes the SETUP buffer
1817 * @td_stp: Reference to the SETP buffer structure
1818 */
1819static void pch_udc_init_setup_buff(struct pch_udc_stp_dma_desc *td_stp)
1820{
1821 static u32 pky_marker;
1822
1823 if (!td_stp)
1824 return;
1825 td_stp->reserved = ++pky_marker;
1826 memset(&td_stp->request, 0xFF, sizeof td_stp->request);
1827 td_stp->status = PCH_UDC_BS_HST_RDY;
1828}
1829
1830/**
1831 * pch_udc_start_next_txrequest() - This function starts
1832 * the next transmission requirement
1833 * @ep: Reference to the endpoint structure
1834 */
1835static void pch_udc_start_next_txrequest(struct pch_udc_ep *ep)
1836{
1837 struct pch_udc_request *req;
1838 struct pch_udc_data_dma_desc *td_data;
1839
1840 if (pch_udc_read_ep_control(ep) & UDC_EPCTL_P)
1841 return;
1842
1843 if (list_empty(&ep->queue))
1844 return;
1845
1846 /* next request */
1847 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1848 if (req->dma_going)
1849 return;
1850 if (!req->td_data)
1851 return;
1852 pch_udc_wait_ep_stall(ep);
1853 req->dma_going = 1;
1854 pch_udc_ep_set_ddptr(ep, 0);
1855 td_data = req->td_data;
1856 while (1) {
1857 td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
1858 PCH_UDC_BS_HST_RDY;
1859 if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
1860 break;
1861 td_data = phys_to_virt(td_data->next);
1862 }
1863 pch_udc_ep_set_ddptr(ep, req->td_data_phys);
1864 pch_udc_set_dma(ep->dev, DMA_DIR_TX);
1865 pch_udc_ep_set_pd(ep);
1866 pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
1867 pch_udc_ep_clear_nak(ep);
1868}
1869
1870/**
1871 * pch_udc_complete_transfer() - This function completes a transfer
1872 * @ep: Reference to the endpoint structure
1873 */
1874static void pch_udc_complete_transfer(struct pch_udc_ep *ep)
1875{
1876 struct pch_udc_request *req;
1877 struct pch_udc_dev *dev = ep->dev;
1878
1879 if (list_empty(&ep->queue))
1880 return;
1881 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1882 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
1883 PCH_UDC_BS_DMA_DONE)
1884 return;
1885 if ((req->td_data_last->status & PCH_UDC_RXTX_STS) !=
1886 PCH_UDC_RTS_SUCC) {
1887 dev_err(&dev->pdev->dev, "Invalid RXTX status (0x%08x) "
1888 "epstatus=0x%08x\n",
1889 (req->td_data_last->status & PCH_UDC_RXTX_STS),
1890 (int)(ep->epsts));
1891 return;
1892 }
1893
1894 req->req.actual = req->req.length;
1895 req->td_data_last->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
1896 req->td_data->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
1897 complete_req(ep, req, 0);
1898 req->dma_going = 0;
1899 if (!list_empty(&ep->queue)) {
1900 pch_udc_wait_ep_stall(ep);
1901 pch_udc_ep_clear_nak(ep);
1902 pch_udc_enable_ep_interrupts(ep->dev,
1903 PCH_UDC_EPINT(ep->in, ep->num));
1904 } else {
1905 pch_udc_disable_ep_interrupts(ep->dev,
1906 PCH_UDC_EPINT(ep->in, ep->num));
1907 }
1908}
1909
1910/**
1911 * pch_udc_complete_receiver() - This function completes a receiver
1912 * @ep: Reference to the endpoint structure
1913 */
1914static void pch_udc_complete_receiver(struct pch_udc_ep *ep)
1915{
1916 struct pch_udc_request *req;
1917 struct pch_udc_dev *dev = ep->dev;
1918 unsigned int count;
1919
1920 if (list_empty(&ep->queue))
1921 return;
1922
1923 /* next request */
1924 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1925 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
1926 PCH_UDC_BS_DMA_DONE)
1927 return;
1928 pch_udc_clear_dma(ep->dev, DMA_DIR_RX);
1929 if ((req->td_data_last->status & PCH_UDC_RXTX_STS) !=
1930 PCH_UDC_RTS_SUCC) {
1931 dev_err(&dev->pdev->dev, "Invalid RXTX status (0x%08x) "
1932 "epstatus=0x%08x\n",
1933 (req->td_data_last->status & PCH_UDC_RXTX_STS),
1934 (int)(ep->epsts));
1935 return;
1936 }
1937 count = req->td_data_last->status & PCH_UDC_RXTX_BYTES;
1938
1939 /* on 64k packets the RXBYTES field is zero */
1940 if (!count && (req->req.length == UDC_DMA_MAXPACKET))
1941 count = UDC_DMA_MAXPACKET;
1942 req->td_data->status |= PCH_UDC_DMA_LAST;
1943 req->td_data_last->status |= PCH_UDC_BS_HST_BSY;
1944
1945 req->dma_going = 0;
1946 req->req.actual = count;
1947 complete_req(ep, req, 0);
1948 /* If there is a new/failed requests try that now */
1949 if (!list_empty(&ep->queue)) {
1950 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
1951 pch_udc_start_rxrequest(ep, req);
1952 }
1953}
1954
1955/**
1956 * pch_udc_svc_data_in() - This function process endpoint interrupts
1957 * for IN endpoints
1958 * @dev: Reference to the device structure
1959 * @ep_num: Endpoint that generated the interrupt
1960 */
1961static void pch_udc_svc_data_in(struct pch_udc_dev *dev, int ep_num)
1962{
1963 u32 epsts;
1964 struct pch_udc_ep *ep;
1965
1966 ep = &dev->ep[2*ep_num];
1967 epsts = ep->epsts;
1968 ep->epsts = 0;
1969
1970 if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
1971 UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
1972 UDC_EPSTS_RSS | UDC_EPSTS_XFERDONE)))
1973 return;
1974 if ((epsts & UDC_EPSTS_BNA))
1975 return;
1976 if (epsts & UDC_EPSTS_HE)
1977 return;
1978 if (epsts & UDC_EPSTS_RSS) {
1979 pch_udc_ep_set_stall(ep);
1980 pch_udc_enable_ep_interrupts(ep->dev,
1981 PCH_UDC_EPINT(ep->in, ep->num));
1982 }
1983 if (epsts & UDC_EPSTS_RCS) {
1984 if (!dev->prot_stall) {
1985 pch_udc_ep_clear_stall(ep);
1986 } else {
1987 pch_udc_ep_set_stall(ep);
1988 pch_udc_enable_ep_interrupts(ep->dev,
1989 PCH_UDC_EPINT(ep->in, ep->num));
1990 }
1991 }
1992 if (epsts & UDC_EPSTS_TDC)
1993 pch_udc_complete_transfer(ep);
1994 /* On IN interrupt, provide data if we have any */
1995 if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_RSS) &&
1996 !(epsts & UDC_EPSTS_TDC) && !(epsts & UDC_EPSTS_TXEMPTY))
1997 pch_udc_start_next_txrequest(ep);
1998}
1999
2000/**
2001 * pch_udc_svc_data_out() - Handles interrupts from OUT endpoint
2002 * @dev: Reference to the device structure
2003 * @ep_num: Endpoint that generated the interrupt
2004 */
2005static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num)
2006{
2007 u32 epsts;
2008 struct pch_udc_ep *ep;
2009 struct pch_udc_request *req = NULL;
2010
2011 ep = &dev->ep[2*ep_num + 1];
2012 epsts = ep->epsts;
2013 ep->epsts = 0;
2014
2015 if ((epsts & UDC_EPSTS_BNA) && (!list_empty(&ep->queue))) {
2016 /* next request */
2017 req = list_entry(ep->queue.next, struct pch_udc_request,
2018 queue);
2019 if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
2020 PCH_UDC_BS_DMA_DONE) {
2021 if (!req->dma_going)
2022 pch_udc_start_rxrequest(ep, req);
2023 return;
2024 }
2025 }
2026 if (epsts & UDC_EPSTS_HE)
2027 return;
2028 if (epsts & UDC_EPSTS_RSS)
2029 pch_udc_ep_set_stall(ep);
2030 pch_udc_enable_ep_interrupts(ep->dev,
2031 PCH_UDC_EPINT(ep->in, ep->num));
2032 if (epsts & UDC_EPSTS_RCS) {
2033 if (!dev->prot_stall) {
2034 pch_udc_ep_clear_stall(ep);
2035 } else {
2036 pch_udc_ep_set_stall(ep);
2037 pch_udc_enable_ep_interrupts(ep->dev,
2038 PCH_UDC_EPINT(ep->in, ep->num));
2039 }
2040 }
2041 if (((epsts & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2042 UDC_EPSTS_OUT_DATA) {
2043 if (ep->dev->prot_stall == 1) {
2044 pch_udc_ep_set_stall(ep);
2045 pch_udc_enable_ep_interrupts(ep->dev,
2046 PCH_UDC_EPINT(ep->in, ep->num));
2047 } else {
2048 pch_udc_complete_receiver(ep);
2049 }
2050 }
2051 if (list_empty(&ep->queue))
2052 pch_udc_set_dma(dev, DMA_DIR_RX);
2053}
2054
2055/**
2056 * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts
2057 * @dev: Reference to the device structure
2058 */
2059static void pch_udc_svc_control_in(struct pch_udc_dev *dev)
2060{
2061 u32 epsts;
2062 struct pch_udc_ep *ep;
2063
2064 ep = &dev->ep[UDC_EP0IN_IDX];
2065 epsts = ep->epsts;
2066 ep->epsts = 0;
2067
2068 if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
2069 UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
2070 UDC_EPSTS_XFERDONE)))
2071 return;
2072 if ((epsts & UDC_EPSTS_BNA))
2073 return;
2074 if (epsts & UDC_EPSTS_HE)
2075 return;
2076 if ((epsts & UDC_EPSTS_TDC) && (!dev->stall))
2077 pch_udc_complete_transfer(ep);
2078 /* On IN interrupt, provide data if we have any */
2079 if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_TDC) &&
2080 !(epsts & UDC_EPSTS_TXEMPTY))
2081 pch_udc_start_next_txrequest(ep);
2082}
2083
2084/**
2085 * pch_udc_svc_control_out() - Routine that handle Control
2086 * OUT endpoint interrupts
2087 * @dev: Reference to the device structure
2088 */
2089static void pch_udc_svc_control_out(struct pch_udc_dev *dev)
2090{
2091 u32 stat;
2092 int setup_supported;
2093 struct pch_udc_ep *ep;
2094
2095 ep = &dev->ep[UDC_EP0OUT_IDX];
2096 stat = ep->epsts;
2097 ep->epsts = 0;
2098
2099 /* If setup data */
2100 if (((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2101 UDC_EPSTS_OUT_SETUP) {
2102 dev->stall = 0;
2103 dev->ep[UDC_EP0IN_IDX].halted = 0;
2104 dev->ep[UDC_EP0OUT_IDX].halted = 0;
2105 /* In data not ready */
2106 pch_udc_ep_set_nak(&(dev->ep[UDC_EP0IN_IDX]));
2107 dev->setup_data = ep->td_stp->request;
2108 pch_udc_init_setup_buff(ep->td_stp);
2109 pch_udc_clear_dma(dev, DMA_DIR_TX);
2110 pch_udc_ep_fifo_flush(&(dev->ep[UDC_EP0IN_IDX]),
2111 dev->ep[UDC_EP0IN_IDX].in);
2112 if ((dev->setup_data.bRequestType & USB_DIR_IN))
2113 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
2114 else /* OUT */
2115 dev->gadget.ep0 = &ep->ep;
2116 spin_unlock(&dev->lock);
2117 /* If Mass storage Reset */
2118 if ((dev->setup_data.bRequestType == 0x21) &&
2119 (dev->setup_data.bRequest == 0xFF))
2120 dev->prot_stall = 0;
2121 /* call gadget with setup data received */
2122 setup_supported = dev->driver->setup(&dev->gadget,
2123 &dev->setup_data);
2124 spin_lock(&dev->lock);
2125 /* ep0 in returns data on IN phase */
2126 if (setup_supported >= 0 && setup_supported <
2127 UDC_EP0IN_MAX_PKT_SIZE) {
2128 pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX]));
2129 /* Gadget would have queued a request when
2130 * we called the setup */
2131 pch_udc_set_dma(dev, DMA_DIR_RX);
2132 pch_udc_ep_clear_nak(ep);
2133 } else if (setup_supported < 0) {
2134 /* if unsupported request, then stall */
2135 pch_udc_ep_set_stall(&(dev->ep[UDC_EP0IN_IDX]));
2136 pch_udc_enable_ep_interrupts(ep->dev,
2137 PCH_UDC_EPINT(ep->in, ep->num));
2138 dev->stall = 0;
2139 pch_udc_set_dma(dev, DMA_DIR_RX);
2140 } else {
2141 dev->waiting_zlp_ack = 1;
2142 }
2143 } else if ((((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
2144 UDC_EPSTS_OUT_DATA) && !dev->stall) {
2145 if (list_empty(&ep->queue)) {
2146 dev_err(&dev->pdev->dev, "%s: No request\n", __func__);
2147 ep->td_data->status = (ep->td_data->status &
2148 ~PCH_UDC_BUFF_STS) |
2149 PCH_UDC_BS_HST_RDY;
2150 pch_udc_set_dma(dev, DMA_DIR_RX);
2151 } else {
2152 /* control write */
2153 /* next function will pickuo an clear the status */
2154 ep->epsts = stat;
2155
2156 pch_udc_svc_data_out(dev, 0);
2157 /* re-program desc. pointer for possible ZLPs */
2158 pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
2159 pch_udc_set_dma(dev, DMA_DIR_RX);
2160 }
2161 }
2162 pch_udc_ep_set_rrdy(ep);
2163}
2164
2165
2166/**
2167 * pch_udc_postsvc_epinters() - This function enables end point interrupts
2168 * and clears NAK status
2169 * @dev: Reference to the device structure
2170 * @ep_num: End point number
2171 */
2172static void pch_udc_postsvc_epinters(struct pch_udc_dev *dev, int ep_num)
2173{
2174 struct pch_udc_ep *ep;
2175 struct pch_udc_request *req;
2176
2177 ep = &dev->ep[2*ep_num];
2178 if (!list_empty(&ep->queue)) {
2179 req = list_entry(ep->queue.next, struct pch_udc_request, queue);
2180 pch_udc_enable_ep_interrupts(ep->dev,
2181 PCH_UDC_EPINT(ep->in, ep->num));
2182 pch_udc_ep_clear_nak(ep);
2183 }
2184}
2185
2186/**
2187 * pch_udc_read_all_epstatus() - This function read all endpoint status
2188 * @dev: Reference to the device structure
2189 * @ep_intr: Status of endpoint interrupt
2190 */
2191static void pch_udc_read_all_epstatus(struct pch_udc_dev *dev, u32 ep_intr)
2192{
2193 int i;
2194 struct pch_udc_ep *ep;
2195
2196 for (i = 0; i < PCH_UDC_USED_EP_NUM; i++) {
2197 /* IN */
2198 if (ep_intr & (0x1 << i)) {
2199 ep = &dev->ep[2*i];
2200 ep->epsts = pch_udc_read_ep_status(ep);
2201 pch_udc_clear_ep_status(ep, ep->epsts);
2202 }
2203 /* OUT */
2204 if (ep_intr & (0x10000 << i)) {
2205 ep = &dev->ep[2*i+1];
2206 ep->epsts = pch_udc_read_ep_status(ep);
2207 pch_udc_clear_ep_status(ep, ep->epsts);
2208 }
2209 }
2210}
2211
2212/**
2213 * pch_udc_activate_control_ep() - This function enables the control endpoints
2214 * for traffic after a reset
2215 * @dev: Reference to the device structure
2216 */
2217static void pch_udc_activate_control_ep(struct pch_udc_dev *dev)
2218{
2219 struct pch_udc_ep *ep;
2220 u32 val;
2221
2222 /* Setup the IN endpoint */
2223 ep = &dev->ep[UDC_EP0IN_IDX];
2224 pch_udc_clear_ep_control(ep);
2225 pch_udc_ep_fifo_flush(ep, ep->in);
2226 pch_udc_ep_set_bufsz(ep, UDC_EP0IN_BUFF_SIZE, ep->in);
2227 pch_udc_ep_set_maxpkt(ep, UDC_EP0IN_MAX_PKT_SIZE);
2228 /* Initialize the IN EP Descriptor */
2229 ep->td_data = NULL;
2230 ep->td_stp = NULL;
2231 ep->td_data_phys = 0;
2232 ep->td_stp_phys = 0;
2233
2234 /* Setup the OUT endpoint */
2235 ep = &dev->ep[UDC_EP0OUT_IDX];
2236 pch_udc_clear_ep_control(ep);
2237 pch_udc_ep_fifo_flush(ep, ep->in);
2238 pch_udc_ep_set_bufsz(ep, UDC_EP0OUT_BUFF_SIZE, ep->in);
2239 pch_udc_ep_set_maxpkt(ep, UDC_EP0OUT_MAX_PKT_SIZE);
2240 val = UDC_EP0OUT_MAX_PKT_SIZE << UDC_CSR_NE_MAX_PKT_SHIFT;
2241 pch_udc_write_csr(ep->dev, val, UDC_EP0OUT_IDX);
2242
2243 /* Initialize the SETUP buffer */
2244 pch_udc_init_setup_buff(ep->td_stp);
2245 /* Write the pointer address of dma descriptor */
2246 pch_udc_ep_set_subptr(ep, ep->td_stp_phys);
2247 /* Write the pointer address of Setup descriptor */
2248 pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
2249
2250 /* Initialize the dma descriptor */
2251 ep->td_data->status = PCH_UDC_DMA_LAST;
2252 ep->td_data->dataptr = dev->dma_addr;
2253 ep->td_data->next = ep->td_data_phys;
2254
2255 pch_udc_ep_clear_nak(ep);
2256}
2257
2258
2259/**
2260 * pch_udc_svc_ur_interrupt() - This function handles a USB reset interrupt
2261 * @dev: Reference to driver structure
2262 */
2263static void pch_udc_svc_ur_interrupt(struct pch_udc_dev *dev)
2264{
2265 struct pch_udc_ep *ep;
2266 int i;
2267
2268 pch_udc_clear_dma(dev, DMA_DIR_TX);
2269 pch_udc_clear_dma(dev, DMA_DIR_RX);
2270 /* Mask all endpoint interrupts */
2271 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2272 /* clear all endpoint interrupts */
2273 pch_udc_write_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2274
2275 for (i = 0; i < PCH_UDC_EP_NUM; i++) {
2276 ep = &dev->ep[i];
2277 pch_udc_clear_ep_status(ep, UDC_EPSTS_ALL_CLR_MASK);
2278 pch_udc_clear_ep_control(ep);
2279 pch_udc_ep_set_ddptr(ep, 0);
2280 pch_udc_write_csr(ep->dev, 0x00, i);
2281 }
2282 dev->stall = 0;
2283 dev->prot_stall = 0;
2284 dev->waiting_zlp_ack = 0;
2285 dev->set_cfg_not_acked = 0;
2286
2287 /* disable ep to empty req queue. Skip the control EP's */
2288 for (i = 0; i < (PCH_UDC_USED_EP_NUM*2); i++) {
2289 ep = &dev->ep[i];
2290 pch_udc_ep_set_nak(ep);
2291 pch_udc_ep_fifo_flush(ep, ep->in);
2292 /* Complete request queue */
2293 empty_req_queue(ep);
2294 }
2295 if (dev->driver && dev->driver->disconnect)
2296 dev->driver->disconnect(&dev->gadget);
2297}
2298
2299/**
2300 * pch_udc_svc_enum_interrupt() - This function handles a USB speed enumeration
2301 * done interrupt
2302 * @dev: Reference to driver structure
2303 */
2304static void pch_udc_svc_enum_interrupt(struct pch_udc_dev *dev)
2305{
2306 u32 dev_stat, dev_speed;
2307 u32 speed = USB_SPEED_FULL;
2308
2309 dev_stat = pch_udc_read_device_status(dev);
2310 dev_speed = (dev_stat & UDC_DEVSTS_ENUM_SPEED_MASK) >>
2311 UDC_DEVSTS_ENUM_SPEED_SHIFT;
2312 switch (dev_speed) {
2313 case UDC_DEVSTS_ENUM_SPEED_HIGH:
2314 speed = USB_SPEED_HIGH;
2315 break;
2316 case UDC_DEVSTS_ENUM_SPEED_FULL:
2317 speed = USB_SPEED_FULL;
2318 break;
2319 case UDC_DEVSTS_ENUM_SPEED_LOW:
2320 speed = USB_SPEED_LOW;
2321 break;
2322 default:
2323 BUG();
2324 }
2325 dev->gadget.speed = speed;
2326 pch_udc_activate_control_ep(dev);
2327 pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 | UDC_EPINT_OUT_EP0);
2328 pch_udc_set_dma(dev, DMA_DIR_TX);
2329 pch_udc_set_dma(dev, DMA_DIR_RX);
2330 pch_udc_ep_set_rrdy(&(dev->ep[UDC_EP0OUT_IDX]));
2331}
2332
2333/**
2334 * pch_udc_svc_intf_interrupt() - This function handles a set interface
2335 * interrupt
2336 * @dev: Reference to driver structure
2337 */
2338static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev)
2339{
2340 u32 reg, dev_stat = 0;
2341 int i, ret;
2342
2343 dev_stat = pch_udc_read_device_status(dev);
2344 dev->cfg_data.cur_intf = (dev_stat & UDC_DEVSTS_INTF_MASK) >>
2345 UDC_DEVSTS_INTF_SHIFT;
2346 dev->cfg_data.cur_alt = (dev_stat & UDC_DEVSTS_ALT_MASK) >>
2347 UDC_DEVSTS_ALT_SHIFT;
2348 dev->set_cfg_not_acked = 1;
2349 /* Construct the usb request for gadget driver and inform it */
2350 memset(&dev->setup_data, 0 , sizeof dev->setup_data);
2351 dev->setup_data.bRequest = USB_REQ_SET_INTERFACE;
2352 dev->setup_data.bRequestType = USB_RECIP_INTERFACE;
2353 dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_alt);
2354 dev->setup_data.wIndex = cpu_to_le16(dev->cfg_data.cur_intf);
2355 /* programm the Endpoint Cfg registers */
2356 /* Only one end point cfg register */
2357 reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
2358 reg = (reg & ~UDC_CSR_NE_INTF_MASK) |
2359 (dev->cfg_data.cur_intf << UDC_CSR_NE_INTF_SHIFT);
2360 reg = (reg & ~UDC_CSR_NE_ALT_MASK) |
2361 (dev->cfg_data.cur_alt << UDC_CSR_NE_ALT_SHIFT);
2362 pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
2363 for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
2364 /* clear stall bits */
2365 pch_udc_ep_clear_stall(&(dev->ep[i]));
2366 dev->ep[i].halted = 0;
2367 }
2368 dev->stall = 0;
2369 spin_unlock(&dev->lock);
2370 ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
2371 spin_lock(&dev->lock);
2372}
2373
2374/**
2375 * pch_udc_svc_cfg_interrupt() - This function handles a set configuration
2376 * interrupt
2377 * @dev: Reference to driver structure
2378 */
2379static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev)
2380{
2381 int i, ret;
2382 u32 reg, dev_stat = 0;
2383
2384 dev_stat = pch_udc_read_device_status(dev);
2385 dev->set_cfg_not_acked = 1;
2386 dev->cfg_data.cur_cfg = (dev_stat & UDC_DEVSTS_CFG_MASK) >>
2387 UDC_DEVSTS_CFG_SHIFT;
2388 /* make usb request for gadget driver */
2389 memset(&dev->setup_data, 0 , sizeof dev->setup_data);
2390 dev->setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
2391 dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_cfg);
2392 /* program the NE registers */
2393 /* Only one end point cfg register */
2394 reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
2395 reg = (reg & ~UDC_CSR_NE_CFG_MASK) |
2396 (dev->cfg_data.cur_cfg << UDC_CSR_NE_CFG_SHIFT);
2397 pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
2398 for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
2399 /* clear stall bits */
2400 pch_udc_ep_clear_stall(&(dev->ep[i]));
2401 dev->ep[i].halted = 0;
2402 }
2403 dev->stall = 0;
2404
2405 /* call gadget zero with setup data received */
2406 spin_unlock(&dev->lock);
2407 ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
2408 spin_lock(&dev->lock);
2409}
2410
2411/**
2412 * pch_udc_dev_isr() - This function services device interrupts
2413 * by invoking appropriate routines.
2414 * @dev: Reference to the device structure
2415 * @dev_intr: The Device interrupt status.
2416 */
2417static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr)
2418{
2419 /* USB Reset Interrupt */
2420 if (dev_intr & UDC_DEVINT_UR)
2421 pch_udc_svc_ur_interrupt(dev);
2422 /* Enumeration Done Interrupt */
2423 if (dev_intr & UDC_DEVINT_ENUM)
2424 pch_udc_svc_enum_interrupt(dev);
2425 /* Set Interface Interrupt */
2426 if (dev_intr & UDC_DEVINT_SI)
2427 pch_udc_svc_intf_interrupt(dev);
2428 /* Set Config Interrupt */
2429 if (dev_intr & UDC_DEVINT_SC)
2430 pch_udc_svc_cfg_interrupt(dev);
2431 /* USB Suspend interrupt */
2432 if (dev_intr & UDC_DEVINT_US)
2433 dev_dbg(&dev->pdev->dev, "USB_SUSPEND\n");
2434 /* Clear the SOF interrupt, if enabled */
2435 if (dev_intr & UDC_DEVINT_SOF)
2436 dev_dbg(&dev->pdev->dev, "SOF\n");
2437 /* ES interrupt, IDLE > 3ms on the USB */
2438 if (dev_intr & UDC_DEVINT_ES)
2439 dev_dbg(&dev->pdev->dev, "ES\n");
2440 /* RWKP interrupt */
2441 if (dev_intr & UDC_DEVINT_RWKP)
2442 dev_dbg(&dev->pdev->dev, "RWKP\n");
2443}
2444
2445/**
2446 * pch_udc_isr() - This function handles interrupts from the PCH USB Device
2447 * @irq: Interrupt request number
2448 * @dev: Reference to the device structure
2449 */
2450static irqreturn_t pch_udc_isr(int irq, void *pdev)
2451{
2452 struct pch_udc_dev *dev = (struct pch_udc_dev *) pdev;
2453 u32 dev_intr, ep_intr;
2454 int i;
2455
2456 dev_intr = pch_udc_read_device_interrupts(dev);
2457 ep_intr = pch_udc_read_ep_interrupts(dev);
2458
2459 if (dev_intr)
2460 /* Clear device interrupts */
2461 pch_udc_write_device_interrupts(dev, dev_intr);
2462 if (ep_intr)
2463 /* Clear ep interrupts */
2464 pch_udc_write_ep_interrupts(dev, ep_intr);
2465 if (!dev_intr && !ep_intr)
2466 return IRQ_NONE;
2467 spin_lock(&dev->lock);
2468 if (dev_intr)
2469 pch_udc_dev_isr(dev, dev_intr);
2470 if (ep_intr) {
2471 pch_udc_read_all_epstatus(dev, ep_intr);
2472 /* Process Control In interrupts, if present */
2473 if (ep_intr & UDC_EPINT_IN_EP0) {
2474 pch_udc_svc_control_in(dev);
2475 pch_udc_postsvc_epinters(dev, 0);
2476 }
2477 /* Process Control Out interrupts, if present */
2478 if (ep_intr & UDC_EPINT_OUT_EP0)
2479 pch_udc_svc_control_out(dev);
2480 /* Process data in end point interrupts */
2481 for (i = 1; i < PCH_UDC_USED_EP_NUM; i++) {
2482 if (ep_intr & (1 << i)) {
2483 pch_udc_svc_data_in(dev, i);
2484 pch_udc_postsvc_epinters(dev, i);
2485 }
2486 }
2487 /* Process data out end point interrupts */
2488 for (i = UDC_EPINT_OUT_SHIFT + 1; i < (UDC_EPINT_OUT_SHIFT +
2489 PCH_UDC_USED_EP_NUM); i++)
2490 if (ep_intr & (1 << i))
2491 pch_udc_svc_data_out(dev, i -
2492 UDC_EPINT_OUT_SHIFT);
2493 }
2494 spin_unlock(&dev->lock);
2495 return IRQ_HANDLED;
2496}
2497
2498/**
2499 * pch_udc_setup_ep0() - This function enables control endpoint for traffic
2500 * @dev: Reference to the device structure
2501 */
2502static void pch_udc_setup_ep0(struct pch_udc_dev *dev)
2503{
2504 /* enable ep0 interrupts */
2505 pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 |
2506 UDC_EPINT_OUT_EP0);
2507 /* enable device interrupts */
2508 pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US |
2509 UDC_DEVINT_ES | UDC_DEVINT_ENUM |
2510 UDC_DEVINT_SI | UDC_DEVINT_SC);
2511}
2512
2513/**
2514 * gadget_release() - Free the gadget driver private data
2515 * @pdev reference to struct pci_dev
2516 */
2517static void gadget_release(struct device *pdev)
2518{
2519 struct pch_udc_dev *dev = dev_get_drvdata(pdev);
2520
2521 kfree(dev);
2522}
2523
2524/**
2525 * pch_udc_pcd_reinit() - This API initializes the endpoint structures
2526 * @dev: Reference to the driver structure
2527 */
2528static void pch_udc_pcd_reinit(struct pch_udc_dev *dev)
2529{
2530 const char *const ep_string[] = {
2531 ep0_string, "ep0out", "ep1in", "ep1out", "ep2in", "ep2out",
2532 "ep3in", "ep3out", "ep4in", "ep4out", "ep5in", "ep5out",
2533 "ep6in", "ep6out", "ep7in", "ep7out", "ep8in", "ep8out",
2534 "ep9in", "ep9out", "ep10in", "ep10out", "ep11in", "ep11out",
2535 "ep12in", "ep12out", "ep13in", "ep13out", "ep14in", "ep14out",
2536 "ep15in", "ep15out",
2537 };
2538 int i;
2539
2540 dev->gadget.speed = USB_SPEED_UNKNOWN;
2541 INIT_LIST_HEAD(&dev->gadget.ep_list);
2542
2543 /* Initialize the endpoints structures */
2544 memset(dev->ep, 0, sizeof dev->ep);
2545 for (i = 0; i < PCH_UDC_EP_NUM; i++) {
2546 struct pch_udc_ep *ep = &dev->ep[i];
2547 ep->dev = dev;
2548 ep->halted = 1;
2549 ep->num = i / 2;
2550 ep->in = ~i & 1;
2551 ep->ep.name = ep_string[i];
2552 ep->ep.ops = &pch_udc_ep_ops;
2553 if (ep->in)
2554 ep->offset_addr = ep->num * UDC_EP_REG_SHIFT;
2555 else
2556 ep->offset_addr = (UDC_EPINT_OUT_SHIFT + ep->num) *
2557 UDC_EP_REG_SHIFT;
2558 /* need to set ep->ep.maxpacket and set Default Configuration?*/
2559 ep->ep.maxpacket = UDC_BULK_MAX_PKT_SIZE;
2560 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
2561 INIT_LIST_HEAD(&ep->queue);
2562 }
2563 dev->ep[UDC_EP0IN_IDX].ep.maxpacket = UDC_EP0IN_MAX_PKT_SIZE;
2564 dev->ep[UDC_EP0OUT_IDX].ep.maxpacket = UDC_EP0OUT_MAX_PKT_SIZE;
2565
2566 dev->dma_addr = pci_map_single(dev->pdev, dev->ep0out_buf, 256,
2567 PCI_DMA_FROMDEVICE);
2568
2569 /* remove ep0 in and out from the list. They have own pointer */
2570 list_del_init(&dev->ep[UDC_EP0IN_IDX].ep.ep_list);
2571 list_del_init(&dev->ep[UDC_EP0OUT_IDX].ep.ep_list);
2572
2573 dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
2574 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2575}
2576
2577/**
2578 * pch_udc_pcd_init() - This API initializes the driver structure
2579 * @dev: Reference to the driver structure
2580 *
2581 * Return codes:
2582 * 0: Success
2583 */
2584static int pch_udc_pcd_init(struct pch_udc_dev *dev)
2585{
2586 pch_udc_init(dev);
2587 pch_udc_pcd_reinit(dev);
2588 return 0;
2589}
2590
2591/**
2592 * init_dma_pools() - create dma pools during initialization
2593 * @pdev: reference to struct pci_dev
2594 */
2595static int init_dma_pools(struct pch_udc_dev *dev)
2596{
2597 struct pch_udc_stp_dma_desc *td_stp;
2598 struct pch_udc_data_dma_desc *td_data;
2599
2600 /* DMA setup */
2601 dev->data_requests = pci_pool_create("data_requests", dev->pdev,
2602 sizeof(struct pch_udc_data_dma_desc), 0, 0);
2603 if (!dev->data_requests) {
2604 dev_err(&dev->pdev->dev, "%s: can't get request data pool\n",
2605 __func__);
2606 return -ENOMEM;
2607 }
2608
2609 /* dma desc for setup data */
2610 dev->stp_requests = pci_pool_create("setup requests", dev->pdev,
2611 sizeof(struct pch_udc_stp_dma_desc), 0, 0);
2612 if (!dev->stp_requests) {
2613 dev_err(&dev->pdev->dev, "%s: can't get setup request pool\n",
2614 __func__);
2615 return -ENOMEM;
2616 }
2617 /* setup */
2618 td_stp = pci_pool_alloc(dev->stp_requests, GFP_KERNEL,
2619 &dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
2620 if (!td_stp) {
2621 dev_err(&dev->pdev->dev,
2622 "%s: can't allocate setup dma descriptor\n", __func__);
2623 return -ENOMEM;
2624 }
2625 dev->ep[UDC_EP0OUT_IDX].td_stp = td_stp;
2626
2627 /* data: 0 packets !? */
2628 td_data = pci_pool_alloc(dev->data_requests, GFP_KERNEL,
2629 &dev->ep[UDC_EP0OUT_IDX].td_data_phys);
2630 if (!td_data) {
2631 dev_err(&dev->pdev->dev,
2632 "%s: can't allocate data dma descriptor\n", __func__);
2633 return -ENOMEM;
2634 }
2635 dev->ep[UDC_EP0OUT_IDX].td_data = td_data;
2636 dev->ep[UDC_EP0IN_IDX].td_stp = NULL;
2637 dev->ep[UDC_EP0IN_IDX].td_stp_phys = 0;
2638 dev->ep[UDC_EP0IN_IDX].td_data = NULL;
2639 dev->ep[UDC_EP0IN_IDX].td_data_phys = 0;
2640 return 0;
2641}
2642
2643int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2644 int (*bind)(struct usb_gadget *))
2645{
2646 struct pch_udc_dev *dev = pch_udc;
2647 int retval;
2648
2649 if (!driver || (driver->speed == USB_SPEED_UNKNOWN) || !bind ||
2650 !driver->setup || !driver->unbind || !driver->disconnect) {
2651 dev_err(&dev->pdev->dev,
2652 "%s: invalid driver parameter\n", __func__);
2653 return -EINVAL;
2654 }
2655
2656 if (!dev)
2657 return -ENODEV;
2658
2659 if (dev->driver) {
2660 dev_err(&dev->pdev->dev, "%s: already bound\n", __func__);
2661 return -EBUSY;
2662 }
2663 driver->driver.bus = NULL;
2664 dev->driver = driver;
2665 dev->gadget.dev.driver = &driver->driver;
2666
2667 /* Invoke the bind routine of the gadget driver */
2668 retval = bind(&dev->gadget);
2669
2670 if (retval) {
2671 dev_err(&dev->pdev->dev, "%s: binding to %s returning %d\n",
2672 __func__, driver->driver.name, retval);
2673 dev->driver = NULL;
2674 dev->gadget.dev.driver = NULL;
2675 return retval;
2676 }
2677 /* get ready for ep0 traffic */
2678 pch_udc_setup_ep0(dev);
2679
2680 /* clear SD */
2681 pch_udc_clear_disconnect(dev);
2682
2683 dev->connected = 1;
2684 return 0;
2685}
2686EXPORT_SYMBOL(usb_gadget_probe_driver);
2687
2688int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2689{
2690 struct pch_udc_dev *dev = pch_udc;
2691
2692 if (!dev)
2693 return -ENODEV;
2694
2695 if (!driver || (driver != dev->driver)) {
2696 dev_err(&dev->pdev->dev,
2697 "%s: invalid driver parameter\n", __func__);
2698 return -EINVAL;
2699 }
2700
2701 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2702
2703 /* Assues that there are no pending requets with this driver */
2704 driver->unbind(&dev->gadget);
2705 dev->gadget.dev.driver = NULL;
2706 dev->driver = NULL;
2707 dev->connected = 0;
2708
2709 /* set SD */
2710 pch_udc_set_disconnect(dev);
2711 return 0;
2712}
2713EXPORT_SYMBOL(usb_gadget_unregister_driver);
2714
2715static void pch_udc_shutdown(struct pci_dev *pdev)
2716{
2717 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2718
2719 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2720 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2721
2722 /* disable the pullup so the host will think we're gone */
2723 pch_udc_set_disconnect(dev);
2724}
2725
2726static void pch_udc_remove(struct pci_dev *pdev)
2727{
2728 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2729
2730 /* gadget driver must not be registered */
2731 if (dev->driver)
2732 dev_err(&pdev->dev,
2733 "%s: gadget driver still bound!!!\n", __func__);
2734 /* dma pool cleanup */
2735 if (dev->data_requests)
2736 pci_pool_destroy(dev->data_requests);
2737
2738 if (dev->stp_requests) {
2739 /* cleanup DMA desc's for ep0in */
2740 if (dev->ep[UDC_EP0OUT_IDX].td_stp) {
2741 pci_pool_free(dev->stp_requests,
2742 dev->ep[UDC_EP0OUT_IDX].td_stp,
2743 dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
2744 }
2745 if (dev->ep[UDC_EP0OUT_IDX].td_data) {
2746 pci_pool_free(dev->stp_requests,
2747 dev->ep[UDC_EP0OUT_IDX].td_data,
2748 dev->ep[UDC_EP0OUT_IDX].td_data_phys);
2749 }
2750 pci_pool_destroy(dev->stp_requests);
2751 }
2752
2753 pch_udc_exit(dev);
2754
2755 if (dev->irq_registered)
2756 free_irq(pdev->irq, dev);
2757 if (dev->base_addr)
2758 iounmap(dev->base_addr);
2759 if (dev->mem_region)
2760 release_mem_region(dev->phys_addr,
2761 pci_resource_len(pdev, PCH_UDC_PCI_BAR));
2762 if (dev->active)
2763 pci_disable_device(pdev);
2764 if (dev->registered)
2765 device_unregister(&dev->gadget.dev);
2766 kfree(dev);
2767 pci_set_drvdata(pdev, NULL);
2768}
2769
2770#ifdef CONFIG_PM
2771static int pch_udc_suspend(struct pci_dev *pdev, pm_message_t state)
2772{
2773 struct pch_udc_dev *dev = pci_get_drvdata(pdev);
2774
2775 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
2776 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
2777
2778 pci_disable_device(pdev);
2779 pci_enable_wake(pdev, PCI_D3hot, 0);
2780
2781 if (pci_save_state(pdev)) {
2782 dev_err(&pdev->dev,
2783 "%s: could not save PCI config state\n", __func__);
2784 return -ENOMEM;
2785 }
2786 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2787 return 0;
2788}
2789
2790static int pch_udc_resume(struct pci_dev *pdev)
2791{
2792 int ret;
2793
2794 pci_set_power_state(pdev, PCI_D0);
2795 ret = pci_restore_state(pdev);
2796 if (ret) {
2797 dev_err(&pdev->dev, "%s: pci_restore_state failed\n", __func__);
2798 return ret;
2799 }
2800 ret = pci_enable_device(pdev);
2801 if (ret) {
2802 dev_err(&pdev->dev, "%s: pci_enable_device failed\n", __func__);
2803 return ret;
2804 }
2805 pci_enable_wake(pdev, PCI_D3hot, 0);
2806 return 0;
2807}
2808#else
2809#define pch_udc_suspend NULL
2810#define pch_udc_resume NULL
2811#endif /* CONFIG_PM */
2812
2813static int pch_udc_probe(struct pci_dev *pdev,
2814 const struct pci_device_id *id)
2815{
2816 unsigned long resource;
2817 unsigned long len;
2818 int retval;
2819 struct pch_udc_dev *dev;
2820
2821 /* one udc only */
2822 if (pch_udc) {
2823 pr_err("%s: already probed\n", __func__);
2824 return -EBUSY;
2825 }
2826 /* init */
2827 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2828 if (!dev) {
2829 pr_err("%s: no memory for device structure\n", __func__);
2830 return -ENOMEM;
2831 }
2832 /* pci setup */
2833 if (pci_enable_device(pdev) < 0) {
2834 kfree(dev);
2835 pr_err("%s: pci_enable_device failed\n", __func__);
2836 return -ENODEV;
2837 }
2838 dev->active = 1;
2839 pci_set_drvdata(pdev, dev);
2840
2841 /* PCI resource allocation */
2842 resource = pci_resource_start(pdev, 1);
2843 len = pci_resource_len(pdev, 1);
2844
2845 if (!request_mem_region(resource, len, KBUILD_MODNAME)) {
2846 dev_err(&pdev->dev, "%s: pci device used already\n", __func__);
2847 retval = -EBUSY;
2848 goto finished;
2849 }
2850 dev->phys_addr = resource;
2851 dev->mem_region = 1;
2852
2853 dev->base_addr = ioremap_nocache(resource, len);
2854 if (!dev->base_addr) {
2855 pr_err("%s: device memory cannot be mapped\n", __func__);
2856 retval = -ENOMEM;
2857 goto finished;
2858 }
2859 if (!pdev->irq) {
2860 dev_err(&pdev->dev, "%s: irq not set\n", __func__);
2861 retval = -ENODEV;
2862 goto finished;
2863 }
2864 pch_udc = dev;
2865 /* initialize the hardware */
2866 if (pch_udc_pcd_init(dev))
2867 goto finished;
2868 if (request_irq(pdev->irq, pch_udc_isr, IRQF_SHARED, KBUILD_MODNAME,
2869 dev)) {
2870 dev_err(&pdev->dev, "%s: request_irq(%d) fail\n", __func__,
2871 pdev->irq);
2872 retval = -ENODEV;
2873 goto finished;
2874 }
2875 dev->irq = pdev->irq;
2876 dev->irq_registered = 1;
2877
2878 pci_set_master(pdev);
2879 pci_try_set_mwi(pdev);
2880
2881 /* device struct setup */
2882 spin_lock_init(&dev->lock);
2883 dev->pdev = pdev;
2884 dev->gadget.ops = &pch_udc_ops;
2885
2886 retval = init_dma_pools(dev);
2887 if (retval)
2888 goto finished;
2889
2890 dev_set_name(&dev->gadget.dev, "gadget");
2891 dev->gadget.dev.parent = &pdev->dev;
2892 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2893 dev->gadget.dev.release = gadget_release;
2894 dev->gadget.name = KBUILD_MODNAME;
2895 dev->gadget.is_dualspeed = 1;
2896
2897 retval = device_register(&dev->gadget.dev);
2898 if (retval)
2899 goto finished;
2900 dev->registered = 1;
2901
2902 /* Put the device in disconnected state till a driver is bound */
2903 pch_udc_set_disconnect(dev);
2904 return 0;
2905
2906finished:
2907 pch_udc_remove(pdev);
2908 return retval;
2909}
2910
2911static DEFINE_PCI_DEVICE_TABLE(pch_udc_pcidev_id) = {
2912 {
2913 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EG20T_UDC),
2914 .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
2915 .class_mask = 0xffffffff,
2916 },
2917 { 0 },
2918};
2919
2920MODULE_DEVICE_TABLE(pci, pch_udc_pcidev_id);
2921
2922
2923static struct pci_driver pch_udc_driver = {
2924 .name = KBUILD_MODNAME,
2925 .id_table = pch_udc_pcidev_id,
2926 .probe = pch_udc_probe,
2927 .remove = pch_udc_remove,
2928 .suspend = pch_udc_suspend,
2929 .resume = pch_udc_resume,
2930 .shutdown = pch_udc_shutdown,
2931};
2932
2933static int __init pch_udc_pci_init(void)
2934{
2935 return pci_register_driver(&pch_udc_driver);
2936}
2937module_init(pch_udc_pci_init);
2938
2939static void __exit pch_udc_pci_exit(void)
2940{
2941 pci_unregister_driver(&pch_udc_driver);
2942}
2943module_exit(pch_udc_pci_exit);
2944
2945MODULE_DESCRIPTION("Intel EG20T USB Device Controller");
2946MODULE_AUTHOR("OKI SEMICONDUCTOR, <toshiharu-linux@dsn.okisemi.com>");
2947MODULE_LICENSE("GPL");
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 fbe86ca95802..e3454fe46b47 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(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;
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