aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/DocBook/gadget.tmpl38
-rw-r--r--Documentation/usb/gadget_serial.txt35
-rw-r--r--Documentation/usb/persist.txt7
-rw-r--r--Documentation/usb/uhci.txt165
-rw-r--r--drivers/hid/usbhid/hid-core.c2
-rw-r--r--drivers/usb/atm/cxacru.c1
-rw-r--r--drivers/usb/atm/speedtch.c1
-rw-r--r--drivers/usb/class/cdc-acm.c177
-rw-r--r--drivers/usb/class/cdc-acm.h5
-rw-r--r--drivers/usb/class/cdc-wdm.c127
-rw-r--r--drivers/usb/core/devices.c4
-rw-r--r--drivers/usb/core/devio.c94
-rw-r--r--drivers/usb/core/driver.c168
-rw-r--r--drivers/usb/core/endpoint.c4
-rw-r--r--drivers/usb/core/file.c10
-rw-r--r--drivers/usb/core/hcd.c6
-rw-r--r--drivers/usb/core/hcd.h4
-rw-r--r--drivers/usb/core/hub.c536
-rw-r--r--drivers/usb/core/inode.c16
-rw-r--r--drivers/usb/core/message.c22
-rw-r--r--drivers/usb/core/usb.c7
-rw-r--r--drivers/usb/core/usb.h19
-rw-r--r--drivers/usb/gadget/Kconfig14
-rw-r--r--drivers/usb/gadget/Makefile13
-rw-r--r--drivers/usb/gadget/amd5536udc.c2
-rw-r--r--drivers/usb/gadget/at91_udc.c13
-rw-r--r--drivers/usb/gadget/cdc2.c246
-rw-r--r--drivers/usb/gadget/composite.c1041
-rw-r--r--drivers/usb/gadget/config.c76
-rw-r--r--drivers/usb/gadget/dummy_hcd.c4
-rw-r--r--drivers/usb/gadget/epautoconf.c1
-rw-r--r--drivers/usb/gadget/ether.c2631
-rw-r--r--drivers/usb/gadget/f_acm.c589
-rw-r--r--drivers/usb/gadget/f_ecm.c833
-rw-r--r--drivers/usb/gadget/f_loopback.c381
-rw-r--r--drivers/usb/gadget/f_rndis.c827
-rw-r--r--drivers/usb/gadget/f_serial.c296
-rw-r--r--drivers/usb/gadget/f_sourcesink.c587
-rw-r--r--drivers/usb/gadget/f_subset.c423
-rw-r--r--drivers/usb/gadget/file_storage.c4
-rw-r--r--drivers/usb/gadget/fsl_usb2_udc.c2
-rw-r--r--drivers/usb/gadget/g_zero.h25
-rw-r--r--drivers/usb/gadget/gadget_chips.h23
-rw-r--r--drivers/usb/gadget/goku_udc.c2
-rw-r--r--drivers/usb/gadget/inode.c23
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.c2
-rw-r--r--drivers/usb/gadget/m66592-udc.c2
-rw-r--r--drivers/usb/gadget/ndis.h10
-rw-r--r--drivers/usb/gadget/net2280.c2
-rw-r--r--drivers/usb/gadget/omap_udc.c2
-rw-r--r--drivers/usb/gadget/printer.c7
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c2
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c1
-rw-r--r--drivers/usb/gadget/rndis.c421
-rw-r--r--drivers/usb/gadget/rndis.h9
-rw-r--r--drivers/usb/gadget/serial.c2315
-rw-r--r--drivers/usb/gadget/u_ether.c967
-rw-r--r--drivers/usb/gadget/u_ether.h127
-rw-r--r--drivers/usb/gadget/u_serial.c1246
-rw-r--r--drivers/usb/gadget/u_serial.h58
-rw-r--r--drivers/usb/gadget/zero.c1162
-rw-r--r--drivers/usb/host/ehci-au1xxx.c369
-rw-r--r--drivers/usb/host/ehci-dbg.c4
-rw-r--r--drivers/usb/host/ehci-fsl.c17
-rw-r--r--drivers/usb/host/ehci-hcd.c2
-rw-r--r--drivers/usb/host/ehci-ixp4xx.c8
-rw-r--r--drivers/usb/host/ehci-orion.c8
-rw-r--r--drivers/usb/host/ehci-ps3.c2
-rw-r--r--drivers/usb/host/ehci-q.c17
-rw-r--r--drivers/usb/host/ehci.h5
-rw-r--r--drivers/usb/host/isp116x-hcd.c25
-rw-r--r--drivers/usb/host/isp1760-hcd.c81
-rw-r--r--drivers/usb/host/isp1760-hcd.h20
-rw-r--r--drivers/usb/host/isp1760-if.c37
-rw-r--r--drivers/usb/host/ohci-at91.c9
-rw-r--r--drivers/usb/host/ohci-au1xxx.c327
-rw-r--r--drivers/usb/host/ohci-dbg.c2
-rw-r--r--drivers/usb/host/ohci-hcd.c2
-rw-r--r--drivers/usb/host/ohci-omap.c2
-rw-r--r--drivers/usb/host/ohci-pnx4008.c28
-rw-r--r--drivers/usb/host/ohci-ppc-of.c2
-rw-r--r--drivers/usb/host/ohci-ps3.c2
-rw-r--r--drivers/usb/host/ohci-q.c3
-rw-r--r--drivers/usb/host/ohci-sm501.c2
-rw-r--r--drivers/usb/host/ohci-ssb.c2
-rw-r--r--drivers/usb/host/r8a66597-hcd.c44
-rw-r--r--drivers/usb/host/sl811-hcd.c2
-rw-r--r--drivers/usb/host/u132-hcd.c2
-rw-r--r--drivers/usb/host/uhci-hub.c2
-rw-r--r--drivers/usb/misc/auerswald.c10
-rw-r--r--drivers/usb/misc/emi62.c2
-rw-r--r--drivers/usb/misc/ftdi-elan.c24
-rw-r--r--drivers/usb/misc/iowarrior.c8
-rw-r--r--drivers/usb/misc/rio500.c8
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.c13
-rw-r--r--drivers/usb/misc/usblcd.c6
-rw-r--r--drivers/usb/mon/mon_stat.c14
-rw-r--r--drivers/usb/serial/Kconfig8
-rw-r--r--drivers/usb/serial/Makefile1
-rw-r--r--drivers/usb/serial/airprime.c353
-rw-r--r--drivers/usb/serial/cp2101.c13
-rw-r--r--drivers/usb/serial/digi_acceleport.c3
-rw-r--r--drivers/usb/serial/io_ti.c1860
-rw-r--r--drivers/usb/serial/ipaq.c3
-rw-r--r--drivers/usb/serial/ir-usb.c490
-rw-r--r--drivers/usb/serial/keyspan.h5
-rw-r--r--drivers/usb/serial/keyspan_pda.c2
-rw-r--r--drivers/usb/serial/kl5kusb105.c2
-rw-r--r--drivers/usb/serial/option.c2
-rw-r--r--drivers/usb/serial/usb-serial.c13
-rw-r--r--drivers/usb/serial/usb_debug.c9
-rw-r--r--drivers/usb/storage/datafab.c2
-rw-r--r--drivers/usb/storage/debug.c2
-rw-r--r--drivers/usb/storage/debug.h2
-rw-r--r--drivers/usb/storage/dpcm.c2
-rw-r--r--drivers/usb/storage/dpcm.h2
-rw-r--r--drivers/usb/storage/freecom.c2
-rw-r--r--drivers/usb/storage/freecom.h2
-rw-r--r--drivers/usb/storage/initializers.c2
-rw-r--r--drivers/usb/storage/initializers.h2
-rw-r--r--drivers/usb/storage/isd200.c8
-rw-r--r--drivers/usb/storage/jumpshot.c2
-rw-r--r--drivers/usb/storage/protocol.c2
-rw-r--r--drivers/usb/storage/protocol.h2
-rw-r--r--drivers/usb/storage/scsiglue.c53
-rw-r--r--drivers/usb/storage/scsiglue.h2
-rw-r--r--drivers/usb/storage/sddr09.c1
-rw-r--r--drivers/usb/storage/sddr09.h2
-rw-r--r--drivers/usb/storage/sddr55.c2
-rw-r--r--drivers/usb/storage/sddr55.h2
-rw-r--r--drivers/usb/storage/shuttle_usbat.c2
-rw-r--r--drivers/usb/storage/shuttle_usbat.h2
-rw-r--r--drivers/usb/storage/transport.c83
-rw-r--r--drivers/usb/storage/transport.h2
-rw-r--r--drivers/usb/storage/unusual_devs.h13
-rw-r--r--drivers/usb/storage/usb.c111
-rw-r--r--drivers/usb/storage/usb.h25
-rw-r--r--include/linux/usb.h12
-rw-r--r--include/linux/usb/composite.h338
-rw-r--r--include/linux/usb/gadget.h27
-rw-r--r--include/linux/usb/irda.h151
-rw-r--r--include/linux/usbdevice_fs.h2
142 files changed, 11623 insertions, 8951 deletions
diff --git a/Documentation/DocBook/gadget.tmpl b/Documentation/DocBook/gadget.tmpl
index 5a8ffa761e09..ea3bc9565e6a 100644
--- a/Documentation/DocBook/gadget.tmpl
+++ b/Documentation/DocBook/gadget.tmpl
@@ -524,6 +524,44 @@ These utilities include endpoint autoconfiguration.
524<!-- !Edrivers/usb/gadget/epautoconf.c --> 524<!-- !Edrivers/usb/gadget/epautoconf.c -->
525</sect1> 525</sect1>
526 526
527<sect1 id="composite"><title>Composite Device Framework</title>
528
529<para>The core API is sufficient for writing drivers for composite
530USB devices (with more than one function in a given configuration),
531and also multi-configuration devices (also more than one function,
532but not necessarily sharing a given configuration).
533There is however an optional framework which makes it easier to
534reuse and combine functions.
535</para>
536
537<para>Devices using this framework provide a <emphasis>struct
538usb_composite_driver</emphasis>, which in turn provides one or
539more <emphasis>struct usb_configuration</emphasis> instances.
540Each such configuration includes at least one
541<emphasis>struct usb_function</emphasis>, which packages a user
542visible role such as "network link" or "mass storage device".
543Management functions may also exist, such as "Device Firmware
544Upgrade".
545</para>
546
547!Iinclude/linux/usb/composite.h
548!Edrivers/usb/gadget/composite.c
549
550</sect1>
551
552<sect1 id="functions"><title>Composite Device Functions</title>
553
554<para>At this writing, a few of the current gadget drivers have
555been converted to this framework.
556Near-term plans include converting all of them, except for "gadgetfs".
557</para>
558
559!Edrivers/usb/gadget/f_acm.c
560!Edrivers/usb/gadget/f_serial.c
561
562</sect1>
563
564
527</chapter> 565</chapter>
528 566
529<chapter id="controllers"><title>Peripheral Controller Drivers</title> 567<chapter id="controllers"><title>Peripheral Controller Drivers</title>
diff --git a/Documentation/usb/gadget_serial.txt b/Documentation/usb/gadget_serial.txt
index 815f5c2301ff..9b22bd14c348 100644
--- a/Documentation/usb/gadget_serial.txt
+++ b/Documentation/usb/gadget_serial.txt
@@ -1,6 +1,7 @@
1 1
2 Linux Gadget Serial Driver v2.0 2 Linux Gadget Serial Driver v2.0
3 11/20/2004 3 11/20/2004
4 (updated 8-May-2008 for v2.3)
4 5
5 6
6License and Disclaimer 7License and Disclaimer
@@ -31,7 +32,7 @@ Prerequisites
31------------- 32-------------
32Versions of the gadget serial driver are available for the 33Versions of the gadget serial driver are available for the
332.4 Linux kernels, but this document assumes you are using 342.4 Linux kernels, but this document assumes you are using
34version 2.0 or later of the gadget serial driver in a 2.6 35version 2.3 or later of the gadget serial driver in a 2.6
35Linux kernel. 36Linux kernel.
36 37
37This document assumes that you are familiar with Linux and 38This document assumes that you are familiar with Linux and
@@ -40,6 +41,12 @@ standard utilities, use minicom and HyperTerminal, and work with
40USB and serial devices. It also assumes you configure the Linux 41USB and serial devices. It also assumes you configure the Linux
41gadget and usb drivers as modules. 42gadget and usb drivers as modules.
42 43
44With version 2.3 of the driver, major and minor device nodes are
45no longer statically defined. Your Linux based system should mount
46sysfs in /sys, and use "mdev" (in Busybox) or "udev" to make the
47/dev nodes matching the sysfs /sys/class/tty files.
48
49
43 50
44Overview 51Overview
45-------- 52--------
@@ -104,15 +111,8 @@ driver. All this are listed under "USB Gadget Support" when
104configuring the kernel. Then rebuild and install the kernel or 111configuring the kernel. Then rebuild and install the kernel or
105modules. 112modules.
106 113
107The gadget serial driver uses major number 127, for now. So you
108will need to create a device node for it, like this:
109
110 mknod /dev/ttygserial c 127 0
111
112You only need to do this once.
113
114Then you must load the gadget serial driver. To load it as an 114Then you must load the gadget serial driver. To load it as an
115ACM device, do this: 115ACM device (recommended for interoperability), do this:
116 116
117 modprobe g_serial use_acm=1 117 modprobe g_serial use_acm=1
118 118
@@ -125,6 +125,23 @@ controller driver. This must be done each time you reboot the gadget
125side Linux system. You can add this to the start up scripts, if 125side Linux system. You can add this to the start up scripts, if
126desired. 126desired.
127 127
128Your system should use mdev (from busybox) or udev to make the
129device nodes. After this gadget driver has been set up you should
130then see a /dev/ttyGS0 node:
131
132 # ls -l /dev/ttyGS0 | cat
133 crw-rw---- 1 root root 253, 0 May 8 14:10 /dev/ttyGS0
134 #
135
136Note that the major number (253, above) is system-specific. If
137you need to create /dev nodes by hand, the right numbers to use
138will be in the /sys/class/tty/ttyGS0/dev file.
139
140When you link this gadget driver early, perhaps even statically,
141you may want to set up an /etc/inittab entry to run "getty" on it.
142The /dev/ttyGS0 line should work like most any other serial port.
143
144
128If gadget serial is loaded as an ACM device you will want to use 145If gadget serial is loaded as an ACM device you will want to use
129either the Windows or Linux ACM driver on the host side. If gadget 146either the Windows or Linux ACM driver on the host side. If gadget
130serial is loaded as a bulk in/out device, you will want to use the 147serial is loaded as a bulk in/out device, you will want to use the
diff --git a/Documentation/usb/persist.txt b/Documentation/usb/persist.txt
index d56cb1a11550..074b159b77c2 100644
--- a/Documentation/usb/persist.txt
+++ b/Documentation/usb/persist.txt
@@ -81,8 +81,11 @@ re-enumeration shows that the device now attached to that port has the
81same descriptors as before, including the Vendor and Product IDs, then 81same descriptors as before, including the Vendor and Product IDs, then
82the kernel continues to use the same device structure. In effect, the 82the kernel continues to use the same device structure. In effect, the
83kernel treats the device as though it had merely been reset instead of 83kernel treats the device as though it had merely been reset instead of
84unplugged. The same thing happens if the host controller is in the 84unplugged.
85expected state but a USB device was unplugged and then replugged. 85
86The same thing happens if the host controller is in the expected state
87but a USB device was unplugged and then replugged, or if a USB device
88fails to carry out a normal resume.
86 89
87If no device is now attached to the port, or if the descriptors are 90If no device is now attached to the port, or if the descriptors are
88different from what the kernel remembers, then the treatment is what 91different from what the kernel remembers, then the treatment is what
diff --git a/Documentation/usb/uhci.txt b/Documentation/usb/uhci.txt
deleted file mode 100644
index 2f25952c86c6..000000000000
--- a/Documentation/usb/uhci.txt
+++ /dev/null
@@ -1,165 +0,0 @@
1Specification and Internals for the New UHCI Driver (Whitepaper...)
2
3 brought to you by
4
5 Georg Acher, acher@in.tum.de (executive slave) (base guitar)
6 Deti Fliegl, deti@fliegl.de (executive slave) (lead voice)
7 Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader)
8
9 $Id: README.uhci,v 1.1 1999/12/14 14:03:02 fliegl Exp $
10
11This document and the new uhci sources can be found on
12 http://hotswap.in.tum.de/usb
13
141. General issues
15
161.1 Why a new UHCI driver, we already have one?!?
17
18Correct, but its internal structure got more and more mixed up by the (still
19ongoing) efforts to get isochronous transfers (ISO) to work.
20Since there is an increasing need for reliable ISO-transfers (especially
21for USB-audio needed by TS and for a DAB-USB-Receiver build by GA and DF),
22this state was a bit unsatisfying in our opinion, so we've decided (based
23on knowledge and experiences with the old UHCI driver) to start
24from scratch with a new approach, much simpler but at the same time more
25powerful.
26It is inspired by the way Win98/Win2000 handles USB requests via URBs,
27but it's definitely 100% free of MS-code and doesn't crash while
28unplugging an used ISO-device like Win98 ;-)
29Some code for HW setup and root hub management was taken from the
30original UHCI driver, but heavily modified to fit into the new code.
31The invention of the basic concept, and major coding were completed in two
32days (and nights) on the 16th and 17th of October 1999, now known as the
33great USB-October-Revolution started by GA, DF, and TS ;-)
34
35Since the concept is in no way UHCI dependent, we hope that it will also be
36transferred to the OHCI-driver, so both drivers share a common API.
37
381.2. Advantages and disadvantages
39
40+ All USB transfer types work now!
41+ Asynchronous operation
42+ Simple, but powerful interface (only two calls for start and cancel)
43+ Easy migration to the new API, simplified by a compatibility API
44+ Simple usage of ISO transfers
45+ Automatic linking of requests
46+ ISO transfers allow variable length for each frame and striping
47+ No CPU dependent and non-portable atomic memory access, no asm()-inlines
48+ Tested on x86 and Alpha
49
50- Rewriting for ISO transfers needed
51
521.3. Is there some compatibility to the old API?
53
54Yes, but only for control, bulk and interrupt transfers. We've implemented
55some wrapper calls for these transfer types. The usbcore works fine with
56these wrappers. For ISO there's no compatibility, because the old ISO-API
57and its semantics were unnecessary complicated in our opinion.
58
591.4. What's really working?
60
61As said above, CTRL and BULK already work fine even with the wrappers,
62so legacy code wouldn't notice the change.
63Regarding to Thomas, ISO transfers now run stable with USB audio.
64INT transfers (e.g. mouse driver) work fine, too.
65
661.5. Are there any bugs?
67
68No ;-)
69Hm...
70Well, of course this implementation needs extensive testing on all available
71hardware, but we believe that any fixes shouldn't harm the overall concept.
72
731.6. What should be done next?
74
75A large part of the request handling seems to be identical for UHCI and
76OHCI, so it would be a good idea to extract the common parts and have only
77the HW specific stuff in uhci.c. Furthermore, all other USB device drivers
78should need URBification, if they use isochronous or interrupt transfers.
79One thing missing in the current implementation (and the old UHCI driver)
80is fair queueing for BULK transfers. Since this would need (in principle)
81the alteration of already constructed TD chains (to switch from depth to
82breadth execution), another way has to be found. Maybe some simple
83heuristics work with the same effect.
84
85---------------------------------------------------------------------------
86
872. Internal structure and mechanisms
88
89To get quickly familiar with the internal structures, here's a short
90description how the new UHCI driver works. However, the ultimate source of
91truth is only uhci.c!
92
932.1. Descriptor structure (QHs and TDs)
94
95During initialization, the following skeleton is allocated in init_skel:
96
97 framespecific | common chain
98
99framelist[]
100[ 0 ]-----> TD --> TD -------\
101[ 1 ]-----> TD --> TD --------> TD ----> QH -------> QH -------> QH ---> NULL
102 ... TD --> TD -------/
103[1023]-----> TD --> TD ------/
104
105 ^^ ^^ ^^ ^^ ^^ ^^
106 1024 TDs for 7 TDs for 1 TD for Start of Start of End Chain
107 ISO INT (2-128ms) 1ms-INT CTRL Chain BULK Chain
108
109For each CTRL or BULK transfer a new QH is allocated and the containing data
110transfers are appended as (vertical) TDs. After building the whole QH with its
111dangling TDs, the QH is inserted before the BULK Chain QH (for CTRL) or
112before the End Chain QH (for BULK). Since only the QH->next pointers are
113affected, no atomic memory operation is required. The three QHs in the
114common chain are never equipped with TDs!
115
116For ISO or INT, the TD for each frame is simply inserted into the appropriate
117ISO/INT-TD-chain for the desired frame. The 7 skeleton INT-TDs are scattered
118among the 1024 frames similar to the old UHCI driver.
119
120For CTRL/BULK/ISO, the last TD in the transfer has the IOC-bit set. For INT,
121every TD (there is only one...) has the IOC-bit set.
122
123Besides the data for the UHCI controller (2 or 4 32bit words), the descriptors
124are double-linked through the .vertical and .horizontal elements in the
125SW data of the descriptor (using the double-linked list structures and
126operations), but SW-linking occurs only in closed domains, i.e. for each of
127the 1024 ISO-chains and the 8 INT-chains there is a closed cycle. This
128simplifies all insertions and unlinking operations and avoids costly
129bus_to_virt()-calls.
130
1312.2. URB structure and linking to QH/TDs
132
133During assembly of the QH and TDs of the requested action, these descriptors
134are stored in urb->urb_list, so the allocated QH/TD descriptors are bound to
135this URB.
136If the assembly was successful and the descriptors were added to the HW chain,
137the corresponding URB is inserted into a global URB list for this controller.
138This list stores all pending URBs.
139
1402.3. Interrupt processing
141
142Since UHCI provides no means to directly detect completed transactions, the
143following is done in each UHCI interrupt (uhci_interrupt()):
144
145For each URB in the pending queue (process_urb()), the ACTIVE-flag of the
146associated TDs are processed (depending on the transfer type
147process_{transfer|interrupt|iso}()). If the TDs are not active anymore,
148they indicate the completion of the transaction and the status is calculated.
149Inactive QH/TDs are removed from the HW chain (since the host controller
150already removed the TDs from the QH, no atomic access is needed) and
151eventually the URB is marked as completed (OK or errors) and removed from the
152pending queue. Then the next linked URB is submitted. After (or immediately
153before) that, the completion handler is called.
154
1552.4. Unlinking URBs
156
157First, all QH/TDs stored in the URB are unlinked from the HW chain.
158To ensure that the host controller really left a vertical TD chain, we
159wait for one frame. After that, the TDs are physically destroyed.
160
1612.5. URB linking and the consequences
162
163Since URBs can be linked and the corresponding submit_urb is called in
164the UHCI-interrupt, all work associated with URB/QH/TD assembly has to be
165interrupt save. This forces kmalloc to use GFP_ATOMIC in the interrupt.
diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
index 01427c51c7cc..27fe4d8912cb 100644
--- a/drivers/hid/usbhid/hid-core.c
+++ b/drivers/hid/usbhid/hid-core.c
@@ -122,7 +122,7 @@ static void hid_reset(struct work_struct *work)
122 dev_dbg(&usbhid->intf->dev, "resetting device\n"); 122 dev_dbg(&usbhid->intf->dev, "resetting device\n");
123 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf); 123 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
124 if (rc_lock >= 0) { 124 if (rc_lock >= 0) {
125 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf); 125 rc = usb_reset_device(hid_to_usb_dev(hid));
126 if (rc_lock) 126 if (rc_lock)
127 usb_unlock_device(hid_to_usb_dev(hid)); 127 usb_unlock_device(hid_to_usb_dev(hid));
128 } 128 }
diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c
index 90583d6a5949..507a9bd0d77c 100644
--- a/drivers/usb/atm/cxacru.c
+++ b/drivers/usb/atm/cxacru.c
@@ -1052,7 +1052,6 @@ static int cxacru_bind(struct usbatm_data *usbatm_instance,
1052 1052
1053 instance->usbatm = usbatm_instance; 1053 instance->usbatm = usbatm_instance;
1054 instance->modem_type = (struct cxacru_modem_type *) id->driver_info; 1054 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1055 memset(instance->card_info, 0, sizeof(instance->card_info));
1056 1055
1057 mutex_init(&instance->poll_state_serialize); 1056 mutex_init(&instance->poll_state_serialize);
1058 instance->poll_state = CXPOLL_STOPPED; 1057 instance->poll_state = CXPOLL_STOPPED;
diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c
index 7d27c9cf3c43..76fce44c2f9a 100644
--- a/drivers/usb/atm/speedtch.c
+++ b/drivers/usb/atm/speedtch.c
@@ -829,7 +829,6 @@ static int speedtch_bind(struct usbatm_data *usbatm,
829 if (use_isoc) { 829 if (use_isoc) {
830 const struct usb_host_interface *desc = data_intf->cur_altsetting; 830 const struct usb_host_interface *desc = data_intf->cur_altsetting;
831 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in; 831 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
832 int i;
833 832
834 use_isoc = 0; /* fall back to bulk if endpoint not found */ 833 use_isoc = 0; /* fall back to bulk if endpoint not found */
835 834
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index c3201affa0b6..95ae6377d7e5 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -159,12 +159,34 @@ static void acm_write_done(struct acm *acm, struct acm_wb *wb)
159 spin_lock_irqsave(&acm->write_lock, flags); 159 spin_lock_irqsave(&acm->write_lock, flags);
160 acm->write_ready = 1; 160 acm->write_ready = 1;
161 wb->use = 0; 161 wb->use = 0;
162 acm->transmitting--;
162 spin_unlock_irqrestore(&acm->write_lock, flags); 163 spin_unlock_irqrestore(&acm->write_lock, flags);
163} 164}
164 165
165/* 166/*
166 * Poke write. 167 * Poke write.
168 *
169 * the caller is responsible for locking
167 */ 170 */
171
172static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
173{
174 int rc;
175
176 acm->transmitting++;
177
178 wb->urb->transfer_buffer = wb->buf;
179 wb->urb->transfer_dma = wb->dmah;
180 wb->urb->transfer_buffer_length = wb->len;
181 wb->urb->dev = acm->dev;
182
183 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
184 dbg("usb_submit_urb(write bulk) failed: %d", rc);
185 acm_write_done(acm, wb);
186 }
187 return rc;
188}
189
168static int acm_write_start(struct acm *acm, int wbn) 190static int acm_write_start(struct acm *acm, int wbn)
169{ 191{
170 unsigned long flags; 192 unsigned long flags;
@@ -182,26 +204,31 @@ static int acm_write_start(struct acm *acm, int wbn)
182 return 0; /* A white lie */ 204 return 0; /* A white lie */
183 } 205 }
184 206
207 wb = &acm->wb[wbn];
208 if(acm_wb_is_avail(acm) <= 1)
209 acm->write_ready = 0;
210
211 dbg("%s susp_count: %d", __func__, acm->susp_count);
212 if (acm->susp_count) {
213 acm->old_ready = acm->write_ready;
214 acm->delayed_wb = wb;
215 acm->write_ready = 0;
216 schedule_work(&acm->waker);
217 spin_unlock_irqrestore(&acm->write_lock, flags);
218 return 0; /* A white lie */
219 }
220 usb_mark_last_busy(acm->dev);
221
185 if (!acm_wb_is_used(acm, wbn)) { 222 if (!acm_wb_is_used(acm, wbn)) {
186 spin_unlock_irqrestore(&acm->write_lock, flags); 223 spin_unlock_irqrestore(&acm->write_lock, flags);
187 return 0; 224 return 0;
188 } 225 }
189 wb = &acm->wb[wbn];
190 226
191 if(acm_wb_is_avail(acm) <= 1) 227 rc = acm_start_wb(acm, wb);
192 acm->write_ready = 0;
193 spin_unlock_irqrestore(&acm->write_lock, flags); 228 spin_unlock_irqrestore(&acm->write_lock, flags);
194 229
195 wb->urb->transfer_buffer = wb->buf;
196 wb->urb->transfer_dma = wb->dmah;
197 wb->urb->transfer_buffer_length = wb->len;
198 wb->urb->dev = acm->dev;
199
200 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
201 dbg("usb_submit_urb(write bulk) failed: %d", rc);
202 acm_write_done(acm, wb);
203 }
204 return rc; 230 return rc;
231
205} 232}
206/* 233/*
207 * attributes exported through sysfs 234 * attributes exported through sysfs
@@ -304,6 +331,7 @@ static void acm_ctrl_irq(struct urb *urb)
304 break; 331 break;
305 } 332 }
306exit: 333exit:
334 usb_mark_last_busy(acm->dev);
307 retval = usb_submit_urb (urb, GFP_ATOMIC); 335 retval = usb_submit_urb (urb, GFP_ATOMIC);
308 if (retval) 336 if (retval)
309 err ("%s - usb_submit_urb failed with result %d", 337 err ("%s - usb_submit_urb failed with result %d",
@@ -320,8 +348,11 @@ static void acm_read_bulk(struct urb *urb)
320 348
321 dbg("Entering acm_read_bulk with status %d", status); 349 dbg("Entering acm_read_bulk with status %d", status);
322 350
323 if (!ACM_READY(acm)) 351 if (!ACM_READY(acm)) {
352 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
324 return; 353 return;
354 }
355 usb_mark_last_busy(acm->dev);
325 356
326 if (status) 357 if (status)
327 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status); 358 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
@@ -331,6 +362,7 @@ static void acm_read_bulk(struct urb *urb)
331 362
332 if (likely(status == 0)) { 363 if (likely(status == 0)) {
333 spin_lock(&acm->read_lock); 364 spin_lock(&acm->read_lock);
365 acm->processing++;
334 list_add_tail(&rcv->list, &acm->spare_read_urbs); 366 list_add_tail(&rcv->list, &acm->spare_read_urbs);
335 list_add_tail(&buf->list, &acm->filled_read_bufs); 367 list_add_tail(&buf->list, &acm->filled_read_bufs);
336 spin_unlock(&acm->read_lock); 368 spin_unlock(&acm->read_lock);
@@ -343,7 +375,8 @@ static void acm_read_bulk(struct urb *urb)
343 /* nevertheless the tasklet must be kicked unconditionally 375 /* nevertheless the tasklet must be kicked unconditionally
344 so the queue cannot dry up */ 376 so the queue cannot dry up */
345 } 377 }
346 tasklet_schedule(&acm->urb_task); 378 if (likely(!acm->susp_count))
379 tasklet_schedule(&acm->urb_task);
347} 380}
348 381
349static void acm_rx_tasklet(unsigned long _acm) 382static void acm_rx_tasklet(unsigned long _acm)
@@ -354,16 +387,23 @@ static void acm_rx_tasklet(unsigned long _acm)
354 struct acm_ru *rcv; 387 struct acm_ru *rcv;
355 unsigned long flags; 388 unsigned long flags;
356 unsigned char throttled; 389 unsigned char throttled;
390
357 dbg("Entering acm_rx_tasklet"); 391 dbg("Entering acm_rx_tasklet");
358 392
359 if (!ACM_READY(acm)) 393 if (!ACM_READY(acm))
394 {
395 dbg("acm_rx_tasklet: ACM not ready");
360 return; 396 return;
397 }
361 398
362 spin_lock_irqsave(&acm->throttle_lock, flags); 399 spin_lock_irqsave(&acm->throttle_lock, flags);
363 throttled = acm->throttle; 400 throttled = acm->throttle;
364 spin_unlock_irqrestore(&acm->throttle_lock, flags); 401 spin_unlock_irqrestore(&acm->throttle_lock, flags);
365 if (throttled) 402 if (throttled)
403 {
404 dbg("acm_rx_tasklet: throttled");
366 return; 405 return;
406 }
367 407
368next_buffer: 408next_buffer:
369 spin_lock_irqsave(&acm->read_lock, flags); 409 spin_lock_irqsave(&acm->read_lock, flags);
@@ -403,6 +443,7 @@ urbs:
403 while (!list_empty(&acm->spare_read_bufs)) { 443 while (!list_empty(&acm->spare_read_bufs)) {
404 spin_lock_irqsave(&acm->read_lock, flags); 444 spin_lock_irqsave(&acm->read_lock, flags);
405 if (list_empty(&acm->spare_read_urbs)) { 445 if (list_empty(&acm->spare_read_urbs)) {
446 acm->processing = 0;
406 spin_unlock_irqrestore(&acm->read_lock, flags); 447 spin_unlock_irqrestore(&acm->read_lock, flags);
407 return; 448 return;
408 } 449 }
@@ -425,18 +466,23 @@ urbs:
425 rcv->urb->transfer_dma = buf->dma; 466 rcv->urb->transfer_dma = buf->dma;
426 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 467 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
427 468
428 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
429
430 /* This shouldn't kill the driver as unsuccessful URBs are returned to the 469 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
431 free-urbs-pool and resubmited ASAP */ 470 free-urbs-pool and resubmited ASAP */
432 if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) { 471 spin_lock_irqsave(&acm->read_lock, flags);
472 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
433 list_add(&buf->list, &acm->spare_read_bufs); 473 list_add(&buf->list, &acm->spare_read_bufs);
434 spin_lock_irqsave(&acm->read_lock, flags);
435 list_add(&rcv->list, &acm->spare_read_urbs); 474 list_add(&rcv->list, &acm->spare_read_urbs);
475 acm->processing = 0;
436 spin_unlock_irqrestore(&acm->read_lock, flags); 476 spin_unlock_irqrestore(&acm->read_lock, flags);
437 return; 477 return;
478 } else {
479 spin_unlock_irqrestore(&acm->read_lock, flags);
480 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
438 } 481 }
439 } 482 }
483 spin_lock_irqsave(&acm->read_lock, flags);
484 acm->processing = 0;
485 spin_unlock_irqrestore(&acm->read_lock, flags);
440} 486}
441 487
442/* data interface wrote those outgoing bytes */ 488/* data interface wrote those outgoing bytes */
@@ -463,6 +509,27 @@ static void acm_softint(struct work_struct *work)
463 tty_wakeup(acm->tty); 509 tty_wakeup(acm->tty);
464} 510}
465 511
512static void acm_waker(struct work_struct *waker)
513{
514 struct acm *acm = container_of(waker, struct acm, waker);
515 long flags;
516 int rv;
517
518 rv = usb_autopm_get_interface(acm->control);
519 if (rv < 0) {
520 err("Autopm failure in %s", __func__);
521 return;
522 }
523 if (acm->delayed_wb) {
524 acm_start_wb(acm, acm->delayed_wb);
525 acm->delayed_wb = NULL;
526 }
527 spin_lock_irqsave(&acm->write_lock, flags);
528 acm->write_ready = acm->old_ready;
529 spin_unlock_irqrestore(&acm->write_lock, flags);
530 usb_autopm_put_interface(acm->control);
531}
532
466/* 533/*
467 * TTY handlers 534 * TTY handlers
468 */ 535 */
@@ -492,6 +559,8 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
492 559
493 if (usb_autopm_get_interface(acm->control) < 0) 560 if (usb_autopm_get_interface(acm->control) < 0)
494 goto early_bail; 561 goto early_bail;
562 else
563 acm->control->needs_remote_wakeup = 1;
495 564
496 mutex_lock(&acm->mutex); 565 mutex_lock(&acm->mutex);
497 if (acm->used++) { 566 if (acm->used++) {
@@ -509,6 +578,7 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
509 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) && 578 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
510 (acm->ctrl_caps & USB_CDC_CAP_LINE)) 579 (acm->ctrl_caps & USB_CDC_CAP_LINE))
511 goto full_bailout; 580 goto full_bailout;
581 usb_autopm_put_interface(acm->control);
512 582
513 INIT_LIST_HEAD(&acm->spare_read_urbs); 583 INIT_LIST_HEAD(&acm->spare_read_urbs);
514 INIT_LIST_HEAD(&acm->spare_read_bufs); 584 INIT_LIST_HEAD(&acm->spare_read_bufs);
@@ -570,12 +640,14 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
570 mutex_lock(&open_mutex); 640 mutex_lock(&open_mutex);
571 if (!--acm->used) { 641 if (!--acm->used) {
572 if (acm->dev) { 642 if (acm->dev) {
643 usb_autopm_get_interface(acm->control);
573 acm_set_control(acm, acm->ctrlout = 0); 644 acm_set_control(acm, acm->ctrlout = 0);
574 usb_kill_urb(acm->ctrlurb); 645 usb_kill_urb(acm->ctrlurb);
575 for (i = 0; i < ACM_NW; i++) 646 for (i = 0; i < ACM_NW; i++)
576 usb_kill_urb(acm->wb[i].urb); 647 usb_kill_urb(acm->wb[i].urb);
577 for (i = 0; i < nr; i++) 648 for (i = 0; i < nr; i++)
578 usb_kill_urb(acm->ru[i].urb); 649 usb_kill_urb(acm->ru[i].urb);
650 acm->control->needs_remote_wakeup = 0;
579 usb_autopm_put_interface(acm->control); 651 usb_autopm_put_interface(acm->control);
580 } else 652 } else
581 acm_tty_unregister(acm); 653 acm_tty_unregister(acm);
@@ -766,7 +838,7 @@ static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios
766 * USB probe and disconnect routines. 838 * USB probe and disconnect routines.
767 */ 839 */
768 840
769/* Little helper: write buffers free */ 841/* Little helpers: write/read buffers free */
770static void acm_write_buffers_free(struct acm *acm) 842static void acm_write_buffers_free(struct acm *acm)
771{ 843{
772 int i; 844 int i;
@@ -777,6 +849,15 @@ static void acm_write_buffers_free(struct acm *acm)
777 } 849 }
778} 850}
779 851
852static void acm_read_buffers_free(struct acm *acm)
853{
854 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
855 int i, n = acm->rx_buflimit;
856
857 for (i = 0; i < n; i++)
858 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
859}
860
780/* Little helper: write buffers allocate */ 861/* Little helper: write buffers allocate */
781static int acm_write_buffers_alloc(struct acm *acm) 862static int acm_write_buffers_alloc(struct acm *acm)
782{ 863{
@@ -987,6 +1068,7 @@ skip_normal_probe:
987 acm->urb_task.func = acm_rx_tasklet; 1068 acm->urb_task.func = acm_rx_tasklet;
988 acm->urb_task.data = (unsigned long) acm; 1069 acm->urb_task.data = (unsigned long) acm;
989 INIT_WORK(&acm->work, acm_softint); 1070 INIT_WORK(&acm->work, acm_softint);
1071 INIT_WORK(&acm->waker, acm_waker);
990 spin_lock_init(&acm->throttle_lock); 1072 spin_lock_init(&acm->throttle_lock);
991 spin_lock_init(&acm->write_lock); 1073 spin_lock_init(&acm->write_lock);
992 spin_lock_init(&acm->read_lock); 1074 spin_lock_init(&acm->read_lock);
@@ -1098,8 +1180,7 @@ alloc_fail8:
1098 for (i = 0; i < ACM_NW; i++) 1180 for (i = 0; i < ACM_NW; i++)
1099 usb_free_urb(acm->wb[i].urb); 1181 usb_free_urb(acm->wb[i].urb);
1100alloc_fail7: 1182alloc_fail7:
1101 for (i = 0; i < num_rx_buf; i++) 1183 acm_read_buffers_free(acm);
1102 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1103 for (i = 0; i < num_rx_buf; i++) 1184 for (i = 0; i < num_rx_buf; i++)
1104 usb_free_urb(acm->ru[i].urb); 1185 usb_free_urb(acm->ru[i].urb);
1105 usb_free_urb(acm->ctrlurb); 1186 usb_free_urb(acm->ctrlurb);
@@ -1116,6 +1197,7 @@ alloc_fail:
1116static void stop_data_traffic(struct acm *acm) 1197static void stop_data_traffic(struct acm *acm)
1117{ 1198{
1118 int i; 1199 int i;
1200 dbg("Entering stop_data_traffic");
1119 1201
1120 tasklet_disable(&acm->urb_task); 1202 tasklet_disable(&acm->urb_task);
1121 1203
@@ -1128,21 +1210,16 @@ static void stop_data_traffic(struct acm *acm)
1128 tasklet_enable(&acm->urb_task); 1210 tasklet_enable(&acm->urb_task);
1129 1211
1130 cancel_work_sync(&acm->work); 1212 cancel_work_sync(&acm->work);
1213 cancel_work_sync(&acm->waker);
1131} 1214}
1132 1215
1133static void acm_disconnect(struct usb_interface *intf) 1216static void acm_disconnect(struct usb_interface *intf)
1134{ 1217{
1135 struct acm *acm = usb_get_intfdata(intf); 1218 struct acm *acm = usb_get_intfdata(intf);
1136 struct usb_device *usb_dev = interface_to_usbdev(intf); 1219 struct usb_device *usb_dev = interface_to_usbdev(intf);
1137 int i;
1138
1139 if (!acm || !acm->dev) {
1140 dbg("disconnect on nonexisting interface");
1141 return;
1142 }
1143 1220
1144 mutex_lock(&open_mutex); 1221 mutex_lock(&open_mutex);
1145 if (!usb_get_intfdata(intf)) { 1222 if (!acm || !acm->dev) {
1146 mutex_unlock(&open_mutex); 1223 mutex_unlock(&open_mutex);
1147 return; 1224 return;
1148 } 1225 }
@@ -1161,10 +1238,10 @@ static void acm_disconnect(struct usb_interface *intf)
1161 1238
1162 acm_write_buffers_free(acm); 1239 acm_write_buffers_free(acm);
1163 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1240 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1164 for (i = 0; i < acm->rx_buflimit; i++) 1241 acm_read_buffers_free(acm);
1165 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1166 1242
1167 usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf); 1243 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1244 acm->data : acm->control);
1168 1245
1169 if (!acm->used) { 1246 if (!acm->used) {
1170 acm_tty_unregister(acm); 1247 acm_tty_unregister(acm);
@@ -1178,11 +1255,31 @@ static void acm_disconnect(struct usb_interface *intf)
1178 tty_hangup(acm->tty); 1255 tty_hangup(acm->tty);
1179} 1256}
1180 1257
1258#ifdef CONFIG_PM
1181static int acm_suspend(struct usb_interface *intf, pm_message_t message) 1259static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1182{ 1260{
1183 struct acm *acm = usb_get_intfdata(intf); 1261 struct acm *acm = usb_get_intfdata(intf);
1262 int cnt;
1263
1264 if (acm->dev->auto_pm) {
1265 int b;
1266
1267 spin_lock_irq(&acm->read_lock);
1268 spin_lock(&acm->write_lock);
1269 b = acm->processing + acm->transmitting;
1270 spin_unlock(&acm->write_lock);
1271 spin_unlock_irq(&acm->read_lock);
1272 if (b)
1273 return -EBUSY;
1274 }
1275
1276 spin_lock_irq(&acm->read_lock);
1277 spin_lock(&acm->write_lock);
1278 cnt = acm->susp_count++;
1279 spin_unlock(&acm->write_lock);
1280 spin_unlock_irq(&acm->read_lock);
1184 1281
1185 if (acm->susp_count++) 1282 if (cnt)
1186 return 0; 1283 return 0;
1187 /* 1284 /*
1188 we treat opened interfaces differently, 1285 we treat opened interfaces differently,
@@ -1201,15 +1298,21 @@ static int acm_resume(struct usb_interface *intf)
1201{ 1298{
1202 struct acm *acm = usb_get_intfdata(intf); 1299 struct acm *acm = usb_get_intfdata(intf);
1203 int rv = 0; 1300 int rv = 0;
1301 int cnt;
1302
1303 spin_lock_irq(&acm->read_lock);
1304 acm->susp_count -= 1;
1305 cnt = acm->susp_count;
1306 spin_unlock_irq(&acm->read_lock);
1204 1307
1205 if (--acm->susp_count) 1308 if (cnt)
1206 return 0; 1309 return 0;
1207 1310
1208 mutex_lock(&acm->mutex); 1311 mutex_lock(&acm->mutex);
1209 if (acm->used) { 1312 if (acm->used) {
1210 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); 1313 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1211 if (rv < 0) 1314 if (rv < 0)
1212 goto err_out; 1315 goto err_out;
1213 1316
1214 tasklet_schedule(&acm->urb_task); 1317 tasklet_schedule(&acm->urb_task);
1215 } 1318 }
@@ -1218,6 +1321,8 @@ err_out:
1218 mutex_unlock(&acm->mutex); 1321 mutex_unlock(&acm->mutex);
1219 return rv; 1322 return rv;
1220} 1323}
1324
1325#endif /* CONFIG_PM */
1221/* 1326/*
1222 * USB driver structure. 1327 * USB driver structure.
1223 */ 1328 */
@@ -1273,10 +1378,14 @@ static struct usb_driver acm_driver = {
1273 .name = "cdc_acm", 1378 .name = "cdc_acm",
1274 .probe = acm_probe, 1379 .probe = acm_probe,
1275 .disconnect = acm_disconnect, 1380 .disconnect = acm_disconnect,
1381#ifdef CONFIG_PM
1276 .suspend = acm_suspend, 1382 .suspend = acm_suspend,
1277 .resume = acm_resume, 1383 .resume = acm_resume,
1384#endif
1278 .id_table = acm_ids, 1385 .id_table = acm_ids,
1386#ifdef CONFIG_PM
1279 .supports_autosuspend = 1, 1387 .supports_autosuspend = 1,
1388#endif
1280}; 1389};
1281 1390
1282/* 1391/*
diff --git a/drivers/usb/class/cdc-acm.h b/drivers/usb/class/cdc-acm.h
index 046e064b033a..85c3aaaab7c5 100644
--- a/drivers/usb/class/cdc-acm.h
+++ b/drivers/usb/class/cdc-acm.h
@@ -107,10 +107,14 @@ struct acm {
107 struct list_head filled_read_bufs; 107 struct list_head filled_read_bufs;
108 int write_used; /* number of non-empty write buffers */ 108 int write_used; /* number of non-empty write buffers */
109 int write_ready; /* write urb is not running */ 109 int write_ready; /* write urb is not running */
110 int old_ready;
111 int processing;
112 int transmitting;
110 spinlock_t write_lock; 113 spinlock_t write_lock;
111 struct mutex mutex; 114 struct mutex mutex;
112 struct usb_cdc_line_coding line; /* bits, stop, parity */ 115 struct usb_cdc_line_coding line; /* bits, stop, parity */
113 struct work_struct work; /* work queue entry for line discipline waking up */ 116 struct work_struct work; /* work queue entry for line discipline waking up */
117 struct work_struct waker;
114 struct tasklet_struct urb_task; /* rx processing */ 118 struct tasklet_struct urb_task; /* rx processing */
115 spinlock_t throttle_lock; /* synchronize throtteling and read callback */ 119 spinlock_t throttle_lock; /* synchronize throtteling and read callback */
116 unsigned int ctrlin; /* input control lines (DCD, DSR, RI, break, overruns) */ 120 unsigned int ctrlin; /* input control lines (DCD, DSR, RI, break, overruns) */
@@ -123,6 +127,7 @@ struct acm {
123 unsigned char clocal; /* termios CLOCAL */ 127 unsigned char clocal; /* termios CLOCAL */
124 unsigned int ctrl_caps; /* control capabilities from the class specific header */ 128 unsigned int ctrl_caps; /* control capabilities from the class specific header */
125 unsigned int susp_count; /* number of suspended interfaces */ 129 unsigned int susp_count; /* number of suspended interfaces */
130 struct acm_wb *delayed_wb; /* write queued for a device about to be woken */
126}; 131};
127 132
128#define CDC_DATA_INTERFACE_TYPE 0x0a 133#define CDC_DATA_INTERFACE_TYPE 0x0a
diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
index 731db051070a..7e8e1235e4e5 100644
--- a/drivers/usb/class/cdc-wdm.c
+++ b/drivers/usb/class/cdc-wdm.c
@@ -28,8 +28,9 @@
28/* 28/*
29 * Version Information 29 * Version Information
30 */ 30 */
31#define DRIVER_VERSION "v0.02" 31#define DRIVER_VERSION "v0.03"
32#define DRIVER_AUTHOR "Oliver Neukum" 32#define DRIVER_AUTHOR "Oliver Neukum"
33#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
33 34
34static struct usb_device_id wdm_ids[] = { 35static struct usb_device_id wdm_ids[] = {
35 { 36 {
@@ -87,6 +88,7 @@ struct wdm_device {
87 dma_addr_t ihandle; 88 dma_addr_t ihandle;
88 struct mutex wlock; 89 struct mutex wlock;
89 struct mutex rlock; 90 struct mutex rlock;
91 struct mutex plock;
90 wait_queue_head_t wait; 92 wait_queue_head_t wait;
91 struct work_struct rxwork; 93 struct work_struct rxwork;
92 int werr; 94 int werr;
@@ -205,7 +207,7 @@ static void wdm_int_callback(struct urb *urb)
205 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 207 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
206 req->wValue = 0; 208 req->wValue = 0;
207 req->wIndex = desc->inum; 209 req->wIndex = desc->inum;
208 req->wLength = cpu_to_le16(desc->bMaxPacketSize0); 210 req->wLength = cpu_to_le16(desc->wMaxCommand);
209 211
210 usb_fill_control_urb( 212 usb_fill_control_urb(
211 desc->response, 213 desc->response,
@@ -214,7 +216,7 @@ static void wdm_int_callback(struct urb *urb)
214 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), 216 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
215 (unsigned char *)req, 217 (unsigned char *)req,
216 desc->inbuf, 218 desc->inbuf,
217 desc->bMaxPacketSize0, 219 desc->wMaxCommand,
218 wdm_in_callback, 220 wdm_in_callback,
219 desc 221 desc
220 ); 222 );
@@ -247,6 +249,7 @@ exit:
247 249
248static void kill_urbs(struct wdm_device *desc) 250static void kill_urbs(struct wdm_device *desc)
249{ 251{
252 /* the order here is essential */
250 usb_kill_urb(desc->command); 253 usb_kill_urb(desc->command);
251 usb_kill_urb(desc->validity); 254 usb_kill_urb(desc->validity);
252 usb_kill_urb(desc->response); 255 usb_kill_urb(desc->response);
@@ -266,7 +269,7 @@ static void cleanup(struct wdm_device *desc)
266 desc->sbuf, 269 desc->sbuf,
267 desc->validity->transfer_dma); 270 desc->validity->transfer_dma);
268 usb_buffer_free(interface_to_usbdev(desc->intf), 271 usb_buffer_free(interface_to_usbdev(desc->intf),
269 desc->wMaxPacketSize, 272 desc->wMaxCommand,
270 desc->inbuf, 273 desc->inbuf,
271 desc->response->transfer_dma); 274 desc->response->transfer_dma);
272 kfree(desc->orq); 275 kfree(desc->orq);
@@ -299,6 +302,9 @@ static ssize_t wdm_write
299 if (r) 302 if (r)
300 goto outnl; 303 goto outnl;
301 304
305 r = usb_autopm_get_interface(desc->intf);
306 if (r < 0)
307 goto outnp;
302 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, 308 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
303 &desc->flags)); 309 &desc->flags));
304 if (r < 0) 310 if (r < 0)
@@ -347,11 +353,14 @@ static ssize_t wdm_write
347 if (rv < 0) { 353 if (rv < 0) {
348 kfree(buf); 354 kfree(buf);
349 clear_bit(WDM_IN_USE, &desc->flags); 355 clear_bit(WDM_IN_USE, &desc->flags);
356 err("Tx URB error: %d", rv);
350 } else { 357 } else {
351 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", 358 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
352 req->wIndex); 359 req->wIndex);
353 } 360 }
354out: 361out:
362 usb_autopm_put_interface(desc->intf);
363outnp:
355 mutex_unlock(&desc->wlock); 364 mutex_unlock(&desc->wlock);
356outnl: 365outnl:
357 return rv < 0 ? rv : count; 366 return rv < 0 ? rv : count;
@@ -376,6 +385,11 @@ retry:
376 rv = wait_event_interruptible(desc->wait, 385 rv = wait_event_interruptible(desc->wait,
377 test_bit(WDM_READ, &desc->flags)); 386 test_bit(WDM_READ, &desc->flags));
378 387
388 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
389 rv = -ENODEV;
390 goto err;
391 }
392 usb_mark_last_busy(interface_to_usbdev(desc->intf));
379 if (rv < 0) { 393 if (rv < 0) {
380 rv = -ERESTARTSYS; 394 rv = -ERESTARTSYS;
381 goto err; 395 goto err;
@@ -418,6 +432,9 @@ retry:
418 desc->ubuf[i] = desc->ubuf[i + cntr]; 432 desc->ubuf[i] = desc->ubuf[i + cntr];
419 433
420 desc->length -= cntr; 434 desc->length -= cntr;
435 /* in case we had outstanding data */
436 if (!desc->length)
437 clear_bit(WDM_READ, &desc->flags);
421 rv = cntr; 438 rv = cntr;
422 439
423err: 440err:
@@ -480,18 +497,28 @@ static int wdm_open(struct inode *inode, struct file *file)
480 if (test_bit(WDM_DISCONNECTING, &desc->flags)) 497 if (test_bit(WDM_DISCONNECTING, &desc->flags))
481 goto out; 498 goto out;
482 499
483 desc->count++; 500 ;
484 file->private_data = desc; 501 file->private_data = desc;
485 502
486 rv = usb_submit_urb(desc->validity, GFP_KERNEL); 503 rv = usb_autopm_get_interface(desc->intf);
487
488 if (rv < 0) { 504 if (rv < 0) {
489 desc->count--; 505 err("Error autopm - %d", rv);
490 err("Error submitting int urb - %d", rv);
491 goto out; 506 goto out;
492 } 507 }
493 rv = 0; 508 intf->needs_remote_wakeup = 1;
494 509
510 mutex_lock(&desc->plock);
511 if (!desc->count++) {
512 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
513 if (rv < 0) {
514 desc->count--;
515 err("Error submitting int urb - %d", rv);
516 }
517 } else {
518 rv = 0;
519 }
520 mutex_unlock(&desc->plock);
521 usb_autopm_put_interface(desc->intf);
495out: 522out:
496 mutex_unlock(&wdm_mutex); 523 mutex_unlock(&wdm_mutex);
497 return rv; 524 return rv;
@@ -502,10 +529,15 @@ static int wdm_release(struct inode *inode, struct file *file)
502 struct wdm_device *desc = file->private_data; 529 struct wdm_device *desc = file->private_data;
503 530
504 mutex_lock(&wdm_mutex); 531 mutex_lock(&wdm_mutex);
532 mutex_lock(&desc->plock);
505 desc->count--; 533 desc->count--;
534 mutex_unlock(&desc->plock);
535
506 if (!desc->count) { 536 if (!desc->count) {
507 dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); 537 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
508 kill_urbs(desc); 538 kill_urbs(desc);
539 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
540 desc->intf->needs_remote_wakeup = 0;
509 } 541 }
510 mutex_unlock(&wdm_mutex); 542 mutex_unlock(&wdm_mutex);
511 return 0; 543 return 0;
@@ -597,6 +629,7 @@ next_desc:
597 goto out; 629 goto out;
598 mutex_init(&desc->wlock); 630 mutex_init(&desc->wlock);
599 mutex_init(&desc->rlock); 631 mutex_init(&desc->rlock);
632 mutex_init(&desc->plock);
600 spin_lock_init(&desc->iuspin); 633 spin_lock_init(&desc->iuspin);
601 init_waitqueue_head(&desc->wait); 634 init_waitqueue_head(&desc->wait);
602 desc->wMaxCommand = maxcom; 635 desc->wMaxCommand = maxcom;
@@ -698,6 +731,7 @@ static void wdm_disconnect(struct usb_interface *intf)
698 spin_lock_irqsave(&desc->iuspin, flags); 731 spin_lock_irqsave(&desc->iuspin, flags);
699 set_bit(WDM_DISCONNECTING, &desc->flags); 732 set_bit(WDM_DISCONNECTING, &desc->flags);
700 set_bit(WDM_READ, &desc->flags); 733 set_bit(WDM_READ, &desc->flags);
734 /* to terminate pending flushes */
701 clear_bit(WDM_IN_USE, &desc->flags); 735 clear_bit(WDM_IN_USE, &desc->flags);
702 spin_unlock_irqrestore(&desc->iuspin, flags); 736 spin_unlock_irqrestore(&desc->iuspin, flags);
703 cancel_work_sync(&desc->rxwork); 737 cancel_work_sync(&desc->rxwork);
@@ -708,11 +742,81 @@ static void wdm_disconnect(struct usb_interface *intf)
708 mutex_unlock(&wdm_mutex); 742 mutex_unlock(&wdm_mutex);
709} 743}
710 744
745static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
746{
747 struct wdm_device *desc = usb_get_intfdata(intf);
748 int rv = 0;
749
750 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
751
752 mutex_lock(&desc->plock);
753#ifdef CONFIG_PM
754 if (interface_to_usbdev(desc->intf)->auto_pm && test_bit(WDM_IN_USE, &desc->flags)) {
755 rv = -EBUSY;
756 } else {
757#endif
758 cancel_work_sync(&desc->rxwork);
759 kill_urbs(desc);
760#ifdef CONFIG_PM
761 }
762#endif
763 mutex_unlock(&desc->plock);
764
765 return rv;
766}
767
768static int recover_from_urb_loss(struct wdm_device *desc)
769{
770 int rv = 0;
771
772 if (desc->count) {
773 rv = usb_submit_urb(desc->validity, GFP_NOIO);
774 if (rv < 0)
775 err("Error resume submitting int urb - %d", rv);
776 }
777 return rv;
778}
779static int wdm_resume(struct usb_interface *intf)
780{
781 struct wdm_device *desc = usb_get_intfdata(intf);
782 int rv;
783
784 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
785 mutex_lock(&desc->plock);
786 rv = recover_from_urb_loss(desc);
787 mutex_unlock(&desc->plock);
788 return rv;
789}
790
791static int wdm_pre_reset(struct usb_interface *intf)
792{
793 struct wdm_device *desc = usb_get_intfdata(intf);
794
795 mutex_lock(&desc->plock);
796 return 0;
797}
798
799static int wdm_post_reset(struct usb_interface *intf)
800{
801 struct wdm_device *desc = usb_get_intfdata(intf);
802 int rv;
803
804 rv = recover_from_urb_loss(desc);
805 mutex_unlock(&desc->plock);
806 return 0;
807}
808
711static struct usb_driver wdm_driver = { 809static struct usb_driver wdm_driver = {
712 .name = "cdc_wdm", 810 .name = "cdc_wdm",
713 .probe = wdm_probe, 811 .probe = wdm_probe,
714 .disconnect = wdm_disconnect, 812 .disconnect = wdm_disconnect,
813 .suspend = wdm_suspend,
814 .resume = wdm_resume,
815 .reset_resume = wdm_resume,
816 .pre_reset = wdm_pre_reset,
817 .post_reset = wdm_post_reset,
715 .id_table = wdm_ids, 818 .id_table = wdm_ids,
819 .supports_autosuspend = 1,
716}; 820};
717 821
718/* --- low level module stuff --- */ 822/* --- low level module stuff --- */
@@ -735,6 +839,5 @@ module_init(wdm_init);
735module_exit(wdm_exit); 839module_exit(wdm_exit);
736 840
737MODULE_AUTHOR(DRIVER_AUTHOR); 841MODULE_AUTHOR(DRIVER_AUTHOR);
738MODULE_DESCRIPTION("USB Abstract Control Model driver for " 842MODULE_DESCRIPTION(DRIVER_DESC);
739 "USB WCM Device Management");
740MODULE_LICENSE("GPL"); 843MODULE_LICENSE("GPL");
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 83d9dc379d96..6ec38175a817 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -46,8 +46,6 @@
46 * 2000-07-05: Ashley Montanaro <ashley@compsoc.man.ac.uk> 46 * 2000-07-05: Ashley Montanaro <ashley@compsoc.man.ac.uk>
47 * Converted file reading routine to dump to buffer once 47 * Converted file reading routine to dump to buffer once
48 * per device, not per bus 48 * per device, not per bus
49 *
50 * $Id: devices.c,v 1.5 2000/01/11 13:58:21 tom Exp $
51 */ 49 */
52 50
53#include <linux/fs.h> 51#include <linux/fs.h>
@@ -63,8 +61,6 @@
63#include "usb.h" 61#include "usb.h"
64#include "hcd.h" 62#include "hcd.h"
65 63
66#define MAX_TOPO_LEVEL 6
67
68/* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */ 64/* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */
69#define ALLOW_SERIAL_NUMBER 65#define ALLOW_SERIAL_NUMBER
70 66
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 9218cca21043..54a350ccd033 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -19,8 +19,6 @@
19 * along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 21 *
22 * $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $
23 *
24 * This file implements the usbfs/x/y files, where 22 * This file implements the usbfs/x/y files, where
25 * x is the bus number and y the device number. 23 * x is the bus number and y the device number.
26 * 24 *
@@ -61,6 +59,22 @@
61/* Mutual exclusion for removal, open, and release */ 59/* Mutual exclusion for removal, open, and release */
62DEFINE_MUTEX(usbfs_mutex); 60DEFINE_MUTEX(usbfs_mutex);
63 61
62struct dev_state {
63 struct list_head list; /* state list */
64 struct usb_device *dev;
65 struct file *file;
66 spinlock_t lock; /* protects the async urb lists */
67 struct list_head async_pending;
68 struct list_head async_completed;
69 wait_queue_head_t wait; /* wake up if a request completed */
70 unsigned int discsignr;
71 struct pid *disc_pid;
72 uid_t disc_uid, disc_euid;
73 void __user *disccontext;
74 unsigned long ifclaimed;
75 u32 secid;
76};
77
64struct async { 78struct async {
65 struct list_head asynclist; 79 struct list_head asynclist;
66 struct dev_state *ps; 80 struct dev_state *ps;
@@ -536,23 +550,19 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
536 return ret; 550 return ret;
537} 551}
538 552
539static int __match_minor(struct device *dev, void *data) 553static int match_devt(struct device *dev, void *data)
540{ 554{
541 int minor = *((int *)data); 555 return dev->devt == (dev_t) (unsigned long) data;
542
543 if (dev->devt == MKDEV(USB_DEVICE_MAJOR, minor))
544 return 1;
545 return 0;
546} 556}
547 557
548static struct usb_device *usbdev_lookup_by_minor(int minor) 558static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
549{ 559{
550 struct device *dev; 560 struct device *dev;
551 561
552 dev = bus_find_device(&usb_bus_type, NULL, &minor, __match_minor); 562 dev = bus_find_device(&usb_bus_type, NULL,
563 (void *) (unsigned long) devt, match_devt);
553 if (!dev) 564 if (!dev)
554 return NULL; 565 return NULL;
555 put_device(dev);
556 return container_of(dev, struct usb_device, dev); 566 return container_of(dev, struct usb_device, dev);
557} 567}
558 568
@@ -575,21 +585,27 @@ static int usbdev_open(struct inode *inode, struct file *file)
575 goto out; 585 goto out;
576 586
577 ret = -ENOENT; 587 ret = -ENOENT;
588
578 /* usbdev device-node */ 589 /* usbdev device-node */
579 if (imajor(inode) == USB_DEVICE_MAJOR) 590 if (imajor(inode) == USB_DEVICE_MAJOR)
580 dev = usbdev_lookup_by_minor(iminor(inode)); 591 dev = usbdev_lookup_by_devt(inode->i_rdev);
581#ifdef CONFIG_USB_DEVICEFS 592#ifdef CONFIG_USB_DEVICEFS
582 /* procfs file */ 593 /* procfs file */
583 if (!dev) 594 if (!dev) {
584 dev = inode->i_private; 595 dev = inode->i_private;
596 if (dev && dev->usbfs_dentry &&
597 dev->usbfs_dentry->d_inode == inode)
598 usb_get_dev(dev);
599 else
600 dev = NULL;
601 }
585#endif 602#endif
586 if (!dev) 603 if (!dev || dev->state == USB_STATE_NOTATTACHED)
587 goto out; 604 goto out;
588 ret = usb_autoresume_device(dev); 605 ret = usb_autoresume_device(dev);
589 if (ret) 606 if (ret)
590 goto out; 607 goto out;
591 608
592 usb_get_dev(dev);
593 ret = 0; 609 ret = 0;
594 ps->dev = dev; 610 ps->dev = dev;
595 ps->file = file; 611 ps->file = file;
@@ -609,8 +625,10 @@ static int usbdev_open(struct inode *inode, struct file *file)
609 list_add_tail(&ps->list, &dev->filelist); 625 list_add_tail(&ps->list, &dev->filelist);
610 file->private_data = ps; 626 file->private_data = ps;
611 out: 627 out:
612 if (ret) 628 if (ret) {
613 kfree(ps); 629 kfree(ps);
630 usb_put_dev(dev);
631 }
614 mutex_unlock(&usbfs_mutex); 632 mutex_unlock(&usbfs_mutex);
615 unlock_kernel(); 633 unlock_kernel();
616 return ret; 634 return ret;
@@ -874,7 +892,7 @@ static int proc_connectinfo(struct dev_state *ps, void __user *arg)
874 892
875static int proc_resetdevice(struct dev_state *ps) 893static int proc_resetdevice(struct dev_state *ps)
876{ 894{
877 return usb_reset_composite_device(ps->dev, NULL); 895 return usb_reset_device(ps->dev);
878} 896}
879 897
880static int proc_setintf(struct dev_state *ps, void __user *arg) 898static int proc_setintf(struct dev_state *ps, void __user *arg)
@@ -1682,25 +1700,49 @@ const struct file_operations usbdev_file_operations = {
1682 .release = usbdev_release, 1700 .release = usbdev_release,
1683}; 1701};
1684 1702
1703void usb_fs_classdev_common_remove(struct usb_device *udev)
1704{
1705 struct dev_state *ps;
1706 struct siginfo sinfo;
1707
1708 while (!list_empty(&udev->filelist)) {
1709 ps = list_entry(udev->filelist.next, struct dev_state, list);
1710 destroy_all_async(ps);
1711 wake_up_all(&ps->wait);
1712 list_del_init(&ps->list);
1713 if (ps->discsignr) {
1714 sinfo.si_signo = ps->discsignr;
1715 sinfo.si_errno = EPIPE;
1716 sinfo.si_code = SI_ASYNCIO;
1717 sinfo.si_addr = ps->disccontext;
1718 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1719 ps->disc_pid, ps->disc_uid,
1720 ps->disc_euid, ps->secid);
1721 }
1722 }
1723}
1724
1685#ifdef CONFIG_USB_DEVICE_CLASS 1725#ifdef CONFIG_USB_DEVICE_CLASS
1686static struct class *usb_classdev_class; 1726static struct class *usb_classdev_class;
1687 1727
1688static int usb_classdev_add(struct usb_device *dev) 1728static int usb_classdev_add(struct usb_device *dev)
1689{ 1729{
1690 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1); 1730 struct device *cldev;
1691 1731
1692 dev->usb_classdev = device_create(usb_classdev_class, &dev->dev, 1732 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1693 MKDEV(USB_DEVICE_MAJOR, minor), 1733 "usbdev%d.%d", dev->bus->busnum,
1694 "usbdev%d.%d", dev->bus->busnum, dev->devnum); 1734 dev->devnum);
1695 if (IS_ERR(dev->usb_classdev)) 1735 if (IS_ERR(cldev))
1696 return PTR_ERR(dev->usb_classdev); 1736 return PTR_ERR(cldev);
1697 1737 dev->usb_classdev = cldev;
1698 return 0; 1738 return 0;
1699} 1739}
1700 1740
1701static void usb_classdev_remove(struct usb_device *dev) 1741static void usb_classdev_remove(struct usb_device *dev)
1702{ 1742{
1703 device_unregister(dev->usb_classdev); 1743 if (dev->usb_classdev)
1744 device_unregister(dev->usb_classdev);
1745 usb_fs_classdev_common_remove(dev);
1704} 1746}
1705 1747
1706static int usb_classdev_notify(struct notifier_block *self, 1748static int usb_classdev_notify(struct notifier_block *self,
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 1e56f1cfa6dc..ddb54e14a5c5 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -201,6 +201,7 @@ static int usb_probe_interface(struct device *dev)
201 201
202 intf = to_usb_interface(dev); 202 intf = to_usb_interface(dev);
203 udev = interface_to_usbdev(intf); 203 udev = interface_to_usbdev(intf);
204 intf->needs_binding = 0;
204 205
205 if (udev->authorized == 0) { 206 if (udev->authorized == 0) {
206 dev_err(&intf->dev, "Device is not authorized for usage\n"); 207 dev_err(&intf->dev, "Device is not authorized for usage\n");
@@ -257,15 +258,16 @@ static int usb_unbind_interface(struct device *dev)
257 udev = interface_to_usbdev(intf); 258 udev = interface_to_usbdev(intf);
258 error = usb_autoresume_device(udev); 259 error = usb_autoresume_device(udev);
259 260
260 /* release all urbs for this interface */ 261 /* Terminate all URBs for this interface unless the driver
261 usb_disable_interface(interface_to_usbdev(intf), intf); 262 * supports "soft" unbinding.
263 */
264 if (!driver->soft_unbind)
265 usb_disable_interface(udev, intf);
262 266
263 driver->disconnect(intf); 267 driver->disconnect(intf);
264 268
265 /* reset other interface state */ 269 /* reset other interface state */
266 usb_set_interface(interface_to_usbdev(intf), 270 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
267 intf->altsetting[0].desc.bInterfaceNumber,
268 0);
269 usb_set_intfdata(intf, NULL); 271 usb_set_intfdata(intf, NULL);
270 272
271 intf->condition = USB_INTERFACE_UNBOUND; 273 intf->condition = USB_INTERFACE_UNBOUND;
@@ -310,6 +312,7 @@ int usb_driver_claim_interface(struct usb_driver *driver,
310 312
311 dev->driver = &driver->drvwrap.driver; 313 dev->driver = &driver->drvwrap.driver;
312 usb_set_intfdata(iface, priv); 314 usb_set_intfdata(iface, priv);
315 iface->needs_binding = 0;
313 316
314 usb_pm_lock(udev); 317 usb_pm_lock(udev);
315 iface->condition = USB_INTERFACE_BOUND; 318 iface->condition = USB_INTERFACE_BOUND;
@@ -586,7 +589,7 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
586 struct usb_device *usb_dev; 589 struct usb_device *usb_dev;
587 590
588 /* driver is often null here; dev_dbg() would oops */ 591 /* driver is often null here; dev_dbg() would oops */
589 pr_debug("usb %s: uevent\n", dev->bus_id); 592 pr_debug("usb %s: uevent\n", dev_name(dev));
590 593
591 if (is_usb_device(dev)) 594 if (is_usb_device(dev))
592 usb_dev = to_usb_device(dev); 595 usb_dev = to_usb_device(dev);
@@ -596,11 +599,11 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
596 } 599 }
597 600
598 if (usb_dev->devnum < 0) { 601 if (usb_dev->devnum < 0) {
599 pr_debug("usb %s: already deleted?\n", dev->bus_id); 602 pr_debug("usb %s: already deleted?\n", dev_name(dev));
600 return -ENODEV; 603 return -ENODEV;
601 } 604 }
602 if (!usb_dev->bus) { 605 if (!usb_dev->bus) {
603 pr_debug("usb %s: bus removed?\n", dev->bus_id); 606 pr_debug("usb %s: bus removed?\n", dev_name(dev));
604 return -ENODEV; 607 return -ENODEV;
605 } 608 }
606 609
@@ -771,6 +774,104 @@ void usb_deregister(struct usb_driver *driver)
771} 774}
772EXPORT_SYMBOL_GPL(usb_deregister); 775EXPORT_SYMBOL_GPL(usb_deregister);
773 776
777
778/* Forced unbinding of a USB interface driver, either because
779 * it doesn't support pre_reset/post_reset/reset_resume or
780 * because it doesn't support suspend/resume.
781 *
782 * The caller must hold @intf's device's lock, but not its pm_mutex
783 * and not @intf->dev.sem.
784 */
785void usb_forced_unbind_intf(struct usb_interface *intf)
786{
787 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
788
789 dev_dbg(&intf->dev, "forced unbind\n");
790 usb_driver_release_interface(driver, intf);
791
792 /* Mark the interface for later rebinding */
793 intf->needs_binding = 1;
794}
795
796/* Delayed forced unbinding of a USB interface driver and scan
797 * for rebinding.
798 *
799 * The caller must hold @intf's device's lock, but not its pm_mutex
800 * and not @intf->dev.sem.
801 *
802 * FIXME: The caller must block system sleep transitions.
803 */
804void usb_rebind_intf(struct usb_interface *intf)
805{
806 int rc;
807
808 /* Delayed unbind of an existing driver */
809 if (intf->dev.driver) {
810 struct usb_driver *driver =
811 to_usb_driver(intf->dev.driver);
812
813 dev_dbg(&intf->dev, "forced unbind\n");
814 usb_driver_release_interface(driver, intf);
815 }
816
817 /* Try to rebind the interface */
818 intf->needs_binding = 0;
819 rc = device_attach(&intf->dev);
820 if (rc < 0)
821 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
822}
823
824#define DO_UNBIND 0
825#define DO_REBIND 1
826
827/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
828 * or rebind interfaces that have been unbound, according to @action.
829 *
830 * The caller must hold @udev's device lock.
831 * FIXME: For rebinds, the caller must block system sleep transitions.
832 */
833static void do_unbind_rebind(struct usb_device *udev, int action)
834{
835 struct usb_host_config *config;
836 int i;
837 struct usb_interface *intf;
838 struct usb_driver *drv;
839
840 config = udev->actconfig;
841 if (config) {
842 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
843 intf = config->interface[i];
844 switch (action) {
845 case DO_UNBIND:
846 if (intf->dev.driver) {
847 drv = to_usb_driver(intf->dev.driver);
848 if (!drv->suspend || !drv->resume)
849 usb_forced_unbind_intf(intf);
850 }
851 break;
852 case DO_REBIND:
853 if (intf->needs_binding) {
854
855 /* FIXME: The next line is needed because we are going to probe
856 * the interface, but as far as the PM core is concerned the
857 * interface is still suspended. The problem wouldn't exist
858 * if we could rebind the interface during the interface's own
859 * resume() call, but at the time the usb_device isn't locked!
860 *
861 * The real solution will be to carry this out during the device's
862 * complete() callback. Until that is implemented, we have to
863 * use this hack.
864 */
865// intf->dev.power.sleeping = 0;
866
867 usb_rebind_intf(intf);
868 }
869 break;
870 }
871 }
872 }
873}
874
774#ifdef CONFIG_PM 875#ifdef CONFIG_PM
775 876
776/* Caller has locked udev's pm_mutex */ 877/* Caller has locked udev's pm_mutex */
@@ -805,8 +906,6 @@ static int usb_resume_device(struct usb_device *udev)
805 906
806 if (udev->state == USB_STATE_NOTATTACHED) 907 if (udev->state == USB_STATE_NOTATTACHED)
807 goto done; 908 goto done;
808 if (udev->state != USB_STATE_SUSPENDED && !udev->reset_resume)
809 goto done;
810 909
811 /* Can't resume it if it doesn't have a driver. */ 910 /* Can't resume it if it doesn't have a driver. */
812 if (udev->dev.driver == NULL) { 911 if (udev->dev.driver == NULL) {
@@ -842,7 +941,7 @@ static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
842 goto done; 941 goto done;
843 driver = to_usb_driver(intf->dev.driver); 942 driver = to_usb_driver(intf->dev.driver);
844 943
845 if (driver->suspend && driver->resume) { 944 if (driver->suspend) {
846 status = driver->suspend(intf, msg); 945 status = driver->suspend(intf, msg);
847 if (status == 0) 946 if (status == 0)
848 mark_quiesced(intf); 947 mark_quiesced(intf);
@@ -850,12 +949,10 @@ static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
850 dev_err(&intf->dev, "%s error %d\n", 949 dev_err(&intf->dev, "%s error %d\n",
851 "suspend", status); 950 "suspend", status);
852 } else { 951 } else {
853 /* 952 /* Later we will unbind the driver and reprobe */
854 * FIXME else if there's no suspend method, disconnect... 953 intf->needs_binding = 1;
855 * Not possible if auto_pm is set... 954 dev_warn(&intf->dev, "no %s for driver %s?\n",
856 */ 955 "suspend", driver->name);
857 dev_warn(&intf->dev, "no suspend for driver %s?\n",
858 driver->name);
859 mark_quiesced(intf); 956 mark_quiesced(intf);
860 } 957 }
861 958
@@ -879,10 +976,12 @@ static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
879 goto done; 976 goto done;
880 977
881 /* Can't resume it if it doesn't have a driver. */ 978 /* Can't resume it if it doesn't have a driver. */
882 if (intf->condition == USB_INTERFACE_UNBOUND) { 979 if (intf->condition == USB_INTERFACE_UNBOUND)
883 status = -ENOTCONN; 980 goto done;
981
982 /* Don't resume if the interface is marked for rebinding */
983 if (intf->needs_binding)
884 goto done; 984 goto done;
885 }
886 driver = to_usb_driver(intf->dev.driver); 985 driver = to_usb_driver(intf->dev.driver);
887 986
888 if (reset_resume) { 987 if (reset_resume) {
@@ -892,7 +991,7 @@ static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
892 dev_err(&intf->dev, "%s error %d\n", 991 dev_err(&intf->dev, "%s error %d\n",
893 "reset_resume", status); 992 "reset_resume", status);
894 } else { 993 } else {
895 /* status = -EOPNOTSUPP; */ 994 intf->needs_binding = 1;
896 dev_warn(&intf->dev, "no %s for driver %s?\n", 995 dev_warn(&intf->dev, "no %s for driver %s?\n",
897 "reset_resume", driver->name); 996 "reset_resume", driver->name);
898 } 997 }
@@ -903,7 +1002,7 @@ static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
903 dev_err(&intf->dev, "%s error %d\n", 1002 dev_err(&intf->dev, "%s error %d\n",
904 "resume", status); 1003 "resume", status);
905 } else { 1004 } else {
906 /* status = -EOPNOTSUPP; */ 1005 intf->needs_binding = 1;
907 dev_warn(&intf->dev, "no %s for driver %s?\n", 1006 dev_warn(&intf->dev, "no %s for driver %s?\n",
908 "resume", driver->name); 1007 "resume", driver->name);
909 } 1008 }
@@ -911,11 +1010,10 @@ static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
911 1010
912done: 1011done:
913 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1012 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
914 if (status == 0) 1013 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
915 mark_active(intf); 1014 mark_active(intf);
916 1015
917 /* FIXME: Unbind the driver and reprobe if the resume failed 1016 /* Later we will unbind the driver and/or reprobe, if necessary */
918 * (not possible if auto_pm is set) */
919 return status; 1017 return status;
920} 1018}
921 1019
@@ -1173,11 +1271,8 @@ static int usb_resume_both(struct usb_device *udev)
1173 * then we're stuck. */ 1271 * then we're stuck. */
1174 status = usb_resume_device(udev); 1272 status = usb_resume_device(udev);
1175 } 1273 }
1176 } else { 1274 } else if (udev->reset_resume)
1177
1178 /* Needed for reset-resume */
1179 status = usb_resume_device(udev); 1275 status = usb_resume_device(udev);
1180 }
1181 1276
1182 if (status == 0 && udev->actconfig) { 1277 if (status == 0 && udev->actconfig) {
1183 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1278 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
@@ -1474,6 +1569,7 @@ int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1474{ 1569{
1475 int status; 1570 int status;
1476 1571
1572 do_unbind_rebind(udev, DO_UNBIND);
1477 usb_pm_lock(udev); 1573 usb_pm_lock(udev);
1478 udev->auto_pm = 0; 1574 udev->auto_pm = 0;
1479 status = usb_suspend_both(udev, msg); 1575 status = usb_suspend_both(udev, msg);
@@ -1501,6 +1597,7 @@ int usb_external_resume_device(struct usb_device *udev)
1501 status = usb_resume_both(udev); 1597 status = usb_resume_both(udev);
1502 udev->last_busy = jiffies; 1598 udev->last_busy = jiffies;
1503 usb_pm_unlock(udev); 1599 usb_pm_unlock(udev);
1600 do_unbind_rebind(udev, DO_REBIND);
1504 1601
1505 /* Now that the device is awake, we can start trying to autosuspend 1602 /* Now that the device is awake, we can start trying to autosuspend
1506 * it again. */ 1603 * it again. */
@@ -1542,14 +1639,11 @@ static int usb_resume(struct device *dev)
1542 udev = to_usb_device(dev); 1639 udev = to_usb_device(dev);
1543 1640
1544 /* If udev->skip_sys_resume is set then udev was already suspended 1641 /* If udev->skip_sys_resume is set then udev was already suspended
1545 * when the system suspend started, so we don't want to resume 1642 * when the system sleep started, so we don't want to resume it
1546 * udev during this system wakeup. However a reset-resume counts 1643 * during this system wakeup.
1547 * as a wakeup event, so allow a reset-resume to occur if remote 1644 */
1548 * wakeup is enabled. */ 1645 if (udev->skip_sys_resume)
1549 if (udev->skip_sys_resume) { 1646 return 0;
1550 if (!(udev->reset_resume && udev->do_remote_wakeup))
1551 return -EHOSTUNREACH;
1552 }
1553 return usb_external_resume_device(udev); 1647 return usb_external_resume_device(udev);
1554} 1648}
1555 1649
diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c
index fae55a31e26d..22912136fc14 100644
--- a/drivers/usb/core/endpoint.c
+++ b/drivers/usb/core/endpoint.c
@@ -296,7 +296,7 @@ int usb_create_ep_files(struct device *parent,
296 retval = endpoint_get_minor(ep_dev); 296 retval = endpoint_get_minor(ep_dev);
297 if (retval) { 297 if (retval) {
298 dev_err(parent, "can not allocate minor number for %s\n", 298 dev_err(parent, "can not allocate minor number for %s\n",
299 ep_dev->dev.bus_id); 299 dev_name(&ep_dev->dev));
300 goto error_register; 300 goto error_register;
301 } 301 }
302 302
@@ -307,7 +307,7 @@ int usb_create_ep_files(struct device *parent,
307 ep_dev->dev.class = ep_class->class; 307 ep_dev->dev.class = ep_class->class;
308 ep_dev->dev.parent = parent; 308 ep_dev->dev.parent = parent;
309 ep_dev->dev.release = ep_device_release; 309 ep_dev->dev.release = ep_device_release;
310 snprintf(ep_dev->dev.bus_id, BUS_ID_SIZE, "usbdev%d.%d_ep%02x", 310 dev_set_name(&ep_dev->dev, "usbdev%d.%d_ep%02x",
311 udev->bus->busnum, udev->devnum, 311 udev->bus->busnum, udev->devnum,
312 endpoint->desc.bEndpointAddress); 312 endpoint->desc.bEndpointAddress);
313 313
diff --git a/drivers/usb/core/file.c b/drivers/usb/core/file.c
index c6a95395e52a..913fb8667899 100644
--- a/drivers/usb/core/file.c
+++ b/drivers/usb/core/file.c
@@ -150,7 +150,7 @@ int usb_register_dev(struct usb_interface *intf,
150 int retval = -EINVAL; 150 int retval = -EINVAL;
151 int minor_base = class_driver->minor_base; 151 int minor_base = class_driver->minor_base;
152 int minor = 0; 152 int minor = 0;
153 char name[BUS_ID_SIZE]; 153 char name[20];
154 char *temp; 154 char *temp;
155 155
156#ifdef CONFIG_USB_DYNAMIC_MINORS 156#ifdef CONFIG_USB_DYNAMIC_MINORS
@@ -190,9 +190,9 @@ int usb_register_dev(struct usb_interface *intf,
190 intf->minor = minor; 190 intf->minor = minor;
191 191
192 /* create a usb class device for this usb interface */ 192 /* create a usb class device for this usb interface */
193 snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base); 193 snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
194 temp = strrchr(name, '/'); 194 temp = strrchr(name, '/');
195 if (temp && (temp[1] != 0x00)) 195 if (temp && (temp[1] != '\0'))
196 ++temp; 196 ++temp;
197 else 197 else
198 temp = name; 198 temp = name;
@@ -227,7 +227,7 @@ void usb_deregister_dev(struct usb_interface *intf,
227 struct usb_class_driver *class_driver) 227 struct usb_class_driver *class_driver)
228{ 228{
229 int minor_base = class_driver->minor_base; 229 int minor_base = class_driver->minor_base;
230 char name[BUS_ID_SIZE]; 230 char name[20];
231 231
232#ifdef CONFIG_USB_DYNAMIC_MINORS 232#ifdef CONFIG_USB_DYNAMIC_MINORS
233 minor_base = 0; 233 minor_base = 0;
@@ -242,7 +242,7 @@ void usb_deregister_dev(struct usb_interface *intf,
242 usb_minors[intf->minor] = NULL; 242 usb_minors[intf->minor] = NULL;
243 up_write(&minor_rwsem); 243 up_write(&minor_rwsem);
244 244
245 snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base); 245 snprintf(name, sizeof(name), class_driver->name, intf->minor - minor_base);
246 device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor)); 246 device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
247 intf->usb_dev = NULL; 247 intf->usb_dev = NULL;
248 intf->minor = -1; 248 intf->minor = -1;
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 42a436478b78..f7bfd72ef115 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -900,14 +900,14 @@ static int register_root_hub(struct usb_hcd *hcd)
900 if (retval != sizeof usb_dev->descriptor) { 900 if (retval != sizeof usb_dev->descriptor) {
901 mutex_unlock(&usb_bus_list_lock); 901 mutex_unlock(&usb_bus_list_lock);
902 dev_dbg (parent_dev, "can't read %s device descriptor %d\n", 902 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
903 usb_dev->dev.bus_id, retval); 903 dev_name(&usb_dev->dev), retval);
904 return (retval < 0) ? retval : -EMSGSIZE; 904 return (retval < 0) ? retval : -EMSGSIZE;
905 } 905 }
906 906
907 retval = usb_new_device (usb_dev); 907 retval = usb_new_device (usb_dev);
908 if (retval) { 908 if (retval) {
909 dev_err (parent_dev, "can't register root hub for %s, %d\n", 909 dev_err (parent_dev, "can't register root hub for %s, %d\n",
910 usb_dev->dev.bus_id, retval); 910 dev_name(&usb_dev->dev), retval);
911 } 911 }
912 mutex_unlock(&usb_bus_list_lock); 912 mutex_unlock(&usb_bus_list_lock);
913 913
@@ -1764,7 +1764,7 @@ EXPORT_SYMBOL_GPL (usb_hc_died);
1764 * If memory is unavailable, returns NULL. 1764 * If memory is unavailable, returns NULL.
1765 */ 1765 */
1766struct usb_hcd *usb_create_hcd (const struct hc_driver *driver, 1766struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1767 struct device *dev, char *bus_name) 1767 struct device *dev, const char *bus_name)
1768{ 1768{
1769 struct usb_hcd *hcd; 1769 struct usb_hcd *hcd;
1770 1770
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index b9de1569b39e..5b0b59b0d89b 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -21,6 +21,8 @@
21 21
22#include <linux/rwsem.h> 22#include <linux/rwsem.h>
23 23
24#define MAX_TOPO_LEVEL 6
25
24/* This file contains declarations of usbcore internals that are mostly 26/* This file contains declarations of usbcore internals that are mostly
25 * used or exposed by Host Controller Drivers. 27 * used or exposed by Host Controller Drivers.
26 */ 28 */
@@ -235,7 +237,7 @@ extern void usb_hcd_disable_endpoint(struct usb_device *udev,
235extern int usb_hcd_get_frame_number(struct usb_device *udev); 237extern int usb_hcd_get_frame_number(struct usb_device *udev);
236 238
237extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver, 239extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
238 struct device *dev, char *bus_name); 240 struct device *dev, const char *bus_name);
239extern struct usb_hcd *usb_get_hcd(struct usb_hcd *hcd); 241extern struct usb_hcd *usb_get_hcd(struct usb_hcd *hcd);
240extern void usb_put_hcd(struct usb_hcd *hcd); 242extern void usb_put_hcd(struct usb_hcd *hcd);
241extern int usb_add_hcd(struct usb_hcd *hcd, 243extern int usb_add_hcd(struct usb_hcd *hcd,
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 4cfe32a16c37..107e1d25ddec 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -72,7 +72,6 @@ struct usb_hub {
72 72
73 unsigned limited_power:1; 73 unsigned limited_power:1;
74 unsigned quiescing:1; 74 unsigned quiescing:1;
75 unsigned activating:1;
76 unsigned disconnected:1; 75 unsigned disconnected:1;
77 76
78 unsigned has_indicators:1; 77 unsigned has_indicators:1;
@@ -131,6 +130,12 @@ MODULE_PARM_DESC(use_both_schemes,
131DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 130DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
132EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 131EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
133 132
133#define HUB_DEBOUNCE_TIMEOUT 1500
134#define HUB_DEBOUNCE_STEP 25
135#define HUB_DEBOUNCE_STABLE 100
136
137
138static int usb_reset_and_verify_device(struct usb_device *udev);
134 139
135static inline char *portspeed(int portstatus) 140static inline char *portspeed(int portstatus)
136{ 141{
@@ -535,37 +540,6 @@ static void hub_power_on(struct usb_hub *hub)
535 msleep(max(pgood_delay, (unsigned) 100)); 540 msleep(max(pgood_delay, (unsigned) 100));
536} 541}
537 542
538static void hub_quiesce(struct usb_hub *hub)
539{
540 /* (nonblocking) khubd and related activity won't re-trigger */
541 hub->quiescing = 1;
542 hub->activating = 0;
543
544 /* (blocking) stop khubd and related activity */
545 usb_kill_urb(hub->urb);
546 if (hub->has_indicators)
547 cancel_delayed_work_sync(&hub->leds);
548 if (hub->tt.hub)
549 cancel_work_sync(&hub->tt.kevent);
550}
551
552static void hub_activate(struct usb_hub *hub)
553{
554 int status;
555
556 hub->quiescing = 0;
557 hub->activating = 1;
558
559 status = usb_submit_urb(hub->urb, GFP_NOIO);
560 if (status < 0)
561 dev_err(hub->intfdev, "activate --> %d\n", status);
562 if (hub->has_indicators && blinkenlights)
563 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
564
565 /* scan all ports ASAP */
566 kick_khubd(hub);
567}
568
569static int hub_hub_status(struct usb_hub *hub, 543static int hub_hub_status(struct usb_hub *hub,
570 u16 *status, u16 *change) 544 u16 *status, u16 *change)
571{ 545{
@@ -624,136 +598,149 @@ static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
624 kick_khubd(hub); 598 kick_khubd(hub);
625} 599}
626 600
627/* caller has locked the hub device */ 601enum hub_activation_type {
628static void hub_stop(struct usb_hub *hub) 602 HUB_INIT, HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME
603};
604
605static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
629{ 606{
630 struct usb_device *hdev = hub->hdev; 607 struct usb_device *hdev = hub->hdev;
631 int i; 608 int port1;
609 int status;
610 bool need_debounce_delay = false;
632 611
633 /* Disconnect all the children */ 612 /* After a resume, port power should still be on.
634 for (i = 0; i < hdev->maxchild; ++i) { 613 * For any other type of activation, turn it on.
635 if (hdev->children[i]) 614 */
636 usb_disconnect(&hdev->children[i]); 615 if (type != HUB_RESUME)
637 } 616 hub_power_on(hub);
638 hub_quiesce(hub);
639}
640 617
641#define HUB_RESET 1 618 /* Check each port and set hub->change_bits to let khubd know
642#define HUB_RESUME 2 619 * which ports need attention.
643#define HUB_RESET_RESUME 3 620 */
621 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
622 struct usb_device *udev = hdev->children[port1-1];
623 u16 portstatus, portchange;
644 624
645#ifdef CONFIG_PM 625 portstatus = portchange = 0;
626 status = hub_port_status(hub, port1, &portstatus, &portchange);
627 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
628 dev_dbg(hub->intfdev,
629 "port %d: status %04x change %04x\n",
630 port1, portstatus, portchange);
646 631
647/* Try to identify which devices need USB-PERSIST handling */ 632 /* After anything other than HUB_RESUME (i.e., initialization
648static int persistent_device(struct usb_device *udev) 633 * or any sort of reset), every port should be disabled.
649{ 634 * Unconnected ports should likewise be disabled (paranoia),
650 int i; 635 * and so should ports for which we have no usb_device.
651 int retval; 636 */
652 struct usb_host_config *actconfig; 637 if ((portstatus & USB_PORT_STAT_ENABLE) && (
638 type != HUB_RESUME ||
639 !(portstatus & USB_PORT_STAT_CONNECTION) ||
640 !udev ||
641 udev->state == USB_STATE_NOTATTACHED)) {
642 clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
643 portstatus &= ~USB_PORT_STAT_ENABLE;
644 }
653 645
654 /* Explicitly not marked persistent? */ 646 /* Clear status-change flags; we'll debounce later */
655 if (!udev->persist_enabled) 647 if (portchange & USB_PORT_STAT_C_CONNECTION) {
656 return 0; 648 need_debounce_delay = true;
649 clear_port_feature(hub->hdev, port1,
650 USB_PORT_FEAT_C_CONNECTION);
651 }
652 if (portchange & USB_PORT_STAT_C_ENABLE) {
653 need_debounce_delay = true;
654 clear_port_feature(hub->hdev, port1,
655 USB_PORT_FEAT_C_ENABLE);
656 }
657 657
658 /* No active config? */ 658 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
659 actconfig = udev->actconfig; 659 /* Tell khubd to disconnect the device or
660 if (!actconfig) 660 * check for a new connection
661 return 0; 661 */
662 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
663 set_bit(port1, hub->change_bits);
664
665 } else if (portstatus & USB_PORT_STAT_ENABLE) {
666 /* The power session apparently survived the resume.
667 * If there was an overcurrent or suspend change
668 * (i.e., remote wakeup request), have khubd
669 * take care of it.
670 */
671 if (portchange)
672 set_bit(port1, hub->change_bits);
662 673
663 /* FIXME! We should check whether it's open here or not! */ 674 } else if (udev->persist_enabled) {
675#ifdef CONFIG_PM
676 udev->reset_resume = 1;
677#endif
678 set_bit(port1, hub->change_bits);
664 679
665 /* 680 } else {
666 * Check that all the interface drivers have a 681 /* The power session is gone; tell khubd */
667 * 'reset_resume' entrypoint 682 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
683 set_bit(port1, hub->change_bits);
684 }
685 }
686
687 /* If no port-status-change flags were set, we don't need any
688 * debouncing. If flags were set we can try to debounce the
689 * ports all at once right now, instead of letting khubd do them
690 * one at a time later on.
691 *
692 * If any port-status changes do occur during this delay, khubd
693 * will see them later and handle them normally.
668 */ 694 */
669 retval = 0; 695 if (need_debounce_delay)
670 for (i = 0; i < actconfig->desc.bNumInterfaces; i++) { 696 msleep(HUB_DEBOUNCE_STABLE);
671 struct usb_interface *intf;
672 struct usb_driver *driver;
673 697
674 intf = actconfig->interface[i]; 698 hub->quiescing = 0;
675 if (!intf->dev.driver)
676 continue;
677 driver = to_usb_driver(intf->dev.driver);
678 if (!driver->reset_resume)
679 return 0;
680 /*
681 * We have at least one driver, and that one
682 * has a reset_resume method.
683 */
684 retval = 1;
685 }
686 return retval;
687}
688 699
689static void hub_restart(struct usb_hub *hub, int type) 700 status = usb_submit_urb(hub->urb, GFP_NOIO);
690{ 701 if (status < 0)
691 struct usb_device *hdev = hub->hdev; 702 dev_err(hub->intfdev, "activate --> %d\n", status);
692 int port1; 703 if (hub->has_indicators && blinkenlights)
704 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
693 705
694 /* Check each of the children to see if they require 706 /* Scan all ports that need attention */
695 * USB-PERSIST handling or disconnection. Also check 707 kick_khubd(hub);
696 * each unoccupied port to make sure it is still disabled. 708}
697 */
698 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
699 struct usb_device *udev = hdev->children[port1-1];
700 int status = 0;
701 u16 portstatus, portchange;
702 709
703 if (!udev || udev->state == USB_STATE_NOTATTACHED) { 710enum hub_quiescing_type {
704 if (type != HUB_RESET) { 711 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
705 status = hub_port_status(hub, port1, 712};
706 &portstatus, &portchange);
707 if (status == 0 && (portstatus &
708 USB_PORT_STAT_ENABLE))
709 clear_port_feature(hdev, port1,
710 USB_PORT_FEAT_ENABLE);
711 }
712 continue;
713 }
714 713
715 /* Was the power session lost while we were suspended? */ 714static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
716 status = hub_port_status(hub, port1, &portstatus, &portchange); 715{
716 struct usb_device *hdev = hub->hdev;
717 int i;
717 718
718 /* If the device is gone, khubd will handle it later */ 719 /* khubd and related activity won't re-trigger */
719 if (status == 0 && !(portstatus & USB_PORT_STAT_CONNECTION)) 720 hub->quiescing = 1;
720 continue;
721 721
722 /* For "USB_PERSIST"-enabled children we must 722 if (type != HUB_SUSPEND) {
723 * mark the child device for reset-resume and 723 /* Disconnect all the children */
724 * turn off the various status changes to prevent 724 for (i = 0; i < hdev->maxchild; ++i) {
725 * khubd from disconnecting it later. 725 if (hdev->children[i])
726 */ 726 usb_disconnect(&hdev->children[i]);
727 if (status == 0 && !(portstatus & USB_PORT_STAT_ENABLE) &&
728 persistent_device(udev)) {
729 if (portchange & USB_PORT_STAT_C_ENABLE)
730 clear_port_feature(hub->hdev, port1,
731 USB_PORT_FEAT_C_ENABLE);
732 if (portchange & USB_PORT_STAT_C_CONNECTION)
733 clear_port_feature(hub->hdev, port1,
734 USB_PORT_FEAT_C_CONNECTION);
735 udev->reset_resume = 1;
736 } 727 }
737
738 /* Otherwise for a reset_resume we must disconnect the child,
739 * but as we may not lock the child device here
740 * we have to do a "logical" disconnect.
741 */
742 else if (type == HUB_RESET_RESUME)
743 hub_port_logical_disconnect(hub, port1);
744 } 728 }
745 729
746 hub_activate(hub); 730 /* Stop khubd and related activity */
731 usb_kill_urb(hub->urb);
732 if (hub->has_indicators)
733 cancel_delayed_work_sync(&hub->leds);
734 if (hub->tt.hub)
735 cancel_work_sync(&hub->tt.kevent);
747} 736}
748 737
749#endif /* CONFIG_PM */
750
751/* caller has locked the hub device */ 738/* caller has locked the hub device */
752static int hub_pre_reset(struct usb_interface *intf) 739static int hub_pre_reset(struct usb_interface *intf)
753{ 740{
754 struct usb_hub *hub = usb_get_intfdata(intf); 741 struct usb_hub *hub = usb_get_intfdata(intf);
755 742
756 hub_stop(hub); 743 hub_quiesce(hub, HUB_PRE_RESET);
757 return 0; 744 return 0;
758} 745}
759 746
@@ -762,8 +749,7 @@ static int hub_post_reset(struct usb_interface *intf)
762{ 749{
763 struct usb_hub *hub = usb_get_intfdata(intf); 750 struct usb_hub *hub = usb_get_intfdata(intf);
764 751
765 hub_power_on(hub); 752 hub_activate(hub, HUB_POST_RESET);
766 hub_activate(hub);
767 return 0; 753 return 0;
768} 754}
769 755
@@ -1009,8 +995,7 @@ static int hub_configure(struct usb_hub *hub,
1009 if (hub->has_indicators && blinkenlights) 995 if (hub->has_indicators && blinkenlights)
1010 hub->indicator [0] = INDICATOR_CYCLE; 996 hub->indicator [0] = INDICATOR_CYCLE;
1011 997
1012 hub_power_on(hub); 998 hub_activate(hub, HUB_INIT);
1013 hub_activate(hub);
1014 return 0; 999 return 0;
1015 1000
1016fail: 1001fail:
@@ -1042,7 +1027,7 @@ static void hub_disconnect(struct usb_interface *intf)
1042 1027
1043 /* Disconnect all children and quiesce the hub */ 1028 /* Disconnect all children and quiesce the hub */
1044 hub->error = 0; 1029 hub->error = 0;
1045 hub_stop(hub); 1030 hub_quiesce(hub, HUB_DISCONNECT);
1046 1031
1047 usb_set_intfdata (intf, NULL); 1032 usb_set_intfdata (intf, NULL);
1048 1033
@@ -1068,6 +1053,12 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1068 desc = intf->cur_altsetting; 1053 desc = intf->cur_altsetting;
1069 hdev = interface_to_usbdev(intf); 1054 hdev = interface_to_usbdev(intf);
1070 1055
1056 if (hdev->level == MAX_TOPO_LEVEL) {
1057 dev_err(&intf->dev, "Unsupported bus topology: "
1058 "hub nested too deep\n");
1059 return -E2BIG;
1060 }
1061
1071#ifdef CONFIG_USB_OTG_BLACKLIST_HUB 1062#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1072 if (hdev->parent) { 1063 if (hdev->parent) {
1073 dev_warn(&intf->dev, "ignoring external hub\n"); 1064 dev_warn(&intf->dev, "ignoring external hub\n");
@@ -1814,6 +1805,51 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
1814 1805
1815#ifdef CONFIG_PM 1806#ifdef CONFIG_PM
1816 1807
1808#define MASK_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
1809 USB_PORT_STAT_SUSPEND)
1810#define WANT_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
1811
1812/* Determine whether the device on a port is ready for a normal resume,
1813 * is ready for a reset-resume, or should be disconnected.
1814 */
1815static int check_port_resume_type(struct usb_device *udev,
1816 struct usb_hub *hub, int port1,
1817 int status, unsigned portchange, unsigned portstatus)
1818{
1819 /* Is the device still present? */
1820 if (status || (portstatus & MASK_BITS) != WANT_BITS) {
1821 if (status >= 0)
1822 status = -ENODEV;
1823 }
1824
1825 /* Can't do a normal resume if the port isn't enabled,
1826 * so try a reset-resume instead.
1827 */
1828 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
1829 if (udev->persist_enabled)
1830 udev->reset_resume = 1;
1831 else
1832 status = -ENODEV;
1833 }
1834
1835 if (status) {
1836 dev_dbg(hub->intfdev,
1837 "port %d status %04x.%04x after resume, %d\n",
1838 port1, portchange, portstatus, status);
1839 } else if (udev->reset_resume) {
1840
1841 /* Late port handoff can set status-change bits */
1842 if (portchange & USB_PORT_STAT_C_CONNECTION)
1843 clear_port_feature(hub->hdev, port1,
1844 USB_PORT_FEAT_C_CONNECTION);
1845 if (portchange & USB_PORT_STAT_C_ENABLE)
1846 clear_port_feature(hub->hdev, port1,
1847 USB_PORT_FEAT_C_ENABLE);
1848 }
1849
1850 return status;
1851}
1852
1817#ifdef CONFIG_USB_SUSPEND 1853#ifdef CONFIG_USB_SUSPEND
1818 1854
1819/* 1855/*
@@ -1943,7 +1979,8 @@ static int finish_port_resume(struct usb_device *udev)
1943 * resumed. 1979 * resumed.
1944 */ 1980 */
1945 if (udev->reset_resume) 1981 if (udev->reset_resume)
1946 status = usb_reset_device(udev); 1982 retry_reset_resume:
1983 status = usb_reset_and_verify_device(udev);
1947 1984
1948 /* 10.5.4.5 says be sure devices in the tree are still there. 1985 /* 10.5.4.5 says be sure devices in the tree are still there.
1949 * For now let's assume the device didn't go crazy on resume, 1986 * For now let's assume the device didn't go crazy on resume,
@@ -1954,6 +1991,13 @@ static int finish_port_resume(struct usb_device *udev)
1954 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 1991 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
1955 if (status >= 0) 1992 if (status >= 0)
1956 status = (status > 0 ? 0 : -ENODEV); 1993 status = (status > 0 ? 0 : -ENODEV);
1994
1995 /* If a normal resume failed, try doing a reset-resume */
1996 if (status && !udev->reset_resume && udev->persist_enabled) {
1997 dev_dbg(&udev->dev, "retry with reset-resume\n");
1998 udev->reset_resume = 1;
1999 goto retry_reset_resume;
2000 }
1957 } 2001 }
1958 2002
1959 if (status) { 2003 if (status) {
@@ -2002,7 +2046,7 @@ static int finish_port_resume(struct usb_device *udev)
2002 * to it will be lost. Using the USB_PERSIST facility, the device can be 2046 * to it will be lost. Using the USB_PERSIST facility, the device can be
2003 * made to appear as if it had not disconnected. 2047 * made to appear as if it had not disconnected.
2004 * 2048 *
2005 * This facility can be dangerous. Although usb_reset_device() makes 2049 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
2006 * every effort to insure that the same device is present after the 2050 * every effort to insure that the same device is present after the
2007 * reset as before, it cannot provide a 100% guarantee. Furthermore it's 2051 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2008 * quite possible for a device to remain unaltered but its media to be 2052 * quite possible for a device to remain unaltered but its media to be
@@ -2018,7 +2062,6 @@ int usb_port_resume(struct usb_device *udev)
2018 int port1 = udev->portnum; 2062 int port1 = udev->portnum;
2019 int status; 2063 int status;
2020 u16 portchange, portstatus; 2064 u16 portchange, portstatus;
2021 unsigned mask_flags, want_flags;
2022 2065
2023 /* Skip the initial Clear-Suspend step for a remote wakeup */ 2066 /* Skip the initial Clear-Suspend step for a remote wakeup */
2024 status = hub_port_status(hub, port1, &portstatus, &portchange); 2067 status = hub_port_status(hub, port1, &portstatus, &portchange);
@@ -2047,35 +2090,23 @@ int usb_port_resume(struct usb_device *udev)
2047 */ 2090 */
2048 status = hub_port_status(hub, port1, &portstatus, &portchange); 2091 status = hub_port_status(hub, port1, &portstatus, &portchange);
2049 2092
2050 SuspendCleared: 2093 /* TRSMRCY = 10 msec */
2051 if (udev->reset_resume) 2094 msleep(10);
2052 want_flags = USB_PORT_STAT_POWER 2095 }
2053 | USB_PORT_STAT_CONNECTION;
2054 else
2055 want_flags = USB_PORT_STAT_POWER
2056 | USB_PORT_STAT_CONNECTION
2057 | USB_PORT_STAT_ENABLE;
2058 mask_flags = want_flags | USB_PORT_STAT_SUSPEND;
2059 2096
2060 if (status < 0 || (portstatus & mask_flags) != want_flags) { 2097 SuspendCleared:
2061 dev_dbg(hub->intfdev, 2098 if (status == 0) {
2062 "port %d status %04x.%04x after resume, %d\n", 2099 if (portchange & USB_PORT_STAT_C_SUSPEND)
2063 port1, portchange, portstatus, status); 2100 clear_port_feature(hub->hdev, port1,
2064 if (status >= 0) 2101 USB_PORT_FEAT_C_SUSPEND);
2065 status = -ENODEV;
2066 } else {
2067 if (portchange & USB_PORT_STAT_C_SUSPEND)
2068 clear_port_feature(hub->hdev, port1,
2069 USB_PORT_FEAT_C_SUSPEND);
2070 /* TRSMRCY = 10 msec */
2071 msleep(10);
2072 }
2073 } 2102 }
2074 2103
2075 clear_bit(port1, hub->busy_bits); 2104 clear_bit(port1, hub->busy_bits);
2076 if (!hub->hdev->parent && !hub->busy_bits[0]) 2105 if (!hub->hdev->parent && !hub->busy_bits[0])
2077 usb_enable_root_hub_irq(hub->hdev->bus); 2106 usb_enable_root_hub_irq(hub->hdev->bus);
2078 2107
2108 status = check_port_resume_type(udev,
2109 hub, port1, status, portchange, portstatus);
2079 if (status == 0) 2110 if (status == 0)
2080 status = finish_port_resume(udev); 2111 status = finish_port_resume(udev);
2081 if (status < 0) { 2112 if (status < 0) {
@@ -2085,17 +2116,16 @@ int usb_port_resume(struct usb_device *udev)
2085 return status; 2116 return status;
2086} 2117}
2087 2118
2119/* caller has locked udev */
2088static int remote_wakeup(struct usb_device *udev) 2120static int remote_wakeup(struct usb_device *udev)
2089{ 2121{
2090 int status = 0; 2122 int status = 0;
2091 2123
2092 usb_lock_device(udev);
2093 if (udev->state == USB_STATE_SUSPENDED) { 2124 if (udev->state == USB_STATE_SUSPENDED) {
2094 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 2125 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
2095 usb_mark_last_busy(udev); 2126 usb_mark_last_busy(udev);
2096 status = usb_external_resume_device(udev); 2127 status = usb_external_resume_device(udev);
2097 } 2128 }
2098 usb_unlock_device(udev);
2099 return status; 2129 return status;
2100} 2130}
2101 2131
@@ -2108,14 +2138,25 @@ int usb_port_suspend(struct usb_device *udev)
2108 return 0; 2138 return 0;
2109} 2139}
2110 2140
2141/* However we may need to do a reset-resume */
2142
2111int usb_port_resume(struct usb_device *udev) 2143int usb_port_resume(struct usb_device *udev)
2112{ 2144{
2113 int status = 0; 2145 struct usb_hub *hub = hdev_to_hub(udev->parent);
2146 int port1 = udev->portnum;
2147 int status;
2148 u16 portchange, portstatus;
2114 2149
2115 /* However we may need to do a reset-resume */ 2150 status = hub_port_status(hub, port1, &portstatus, &portchange);
2116 if (udev->reset_resume) { 2151 status = check_port_resume_type(udev,
2152 hub, port1, status, portchange, portstatus);
2153
2154 if (status) {
2155 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2156 hub_port_logical_disconnect(hub, port1);
2157 } else if (udev->reset_resume) {
2117 dev_dbg(&udev->dev, "reset-resume\n"); 2158 dev_dbg(&udev->dev, "reset-resume\n");
2118 status = usb_reset_device(udev); 2159 status = usb_reset_and_verify_device(udev);
2119 } 2160 }
2120 return status; 2161 return status;
2121} 2162}
@@ -2149,7 +2190,7 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
2149 dev_dbg(&intf->dev, "%s\n", __func__); 2190 dev_dbg(&intf->dev, "%s\n", __func__);
2150 2191
2151 /* stop khubd and related activity */ 2192 /* stop khubd and related activity */
2152 hub_quiesce(hub); 2193 hub_quiesce(hub, HUB_SUSPEND);
2153 return 0; 2194 return 0;
2154} 2195}
2155 2196
@@ -2158,7 +2199,7 @@ static int hub_resume(struct usb_interface *intf)
2158 struct usb_hub *hub = usb_get_intfdata(intf); 2199 struct usb_hub *hub = usb_get_intfdata(intf);
2159 2200
2160 dev_dbg(&intf->dev, "%s\n", __func__); 2201 dev_dbg(&intf->dev, "%s\n", __func__);
2161 hub_restart(hub, HUB_RESUME); 2202 hub_activate(hub, HUB_RESUME);
2162 return 0; 2203 return 0;
2163} 2204}
2164 2205
@@ -2167,8 +2208,7 @@ static int hub_reset_resume(struct usb_interface *intf)
2167 struct usb_hub *hub = usb_get_intfdata(intf); 2208 struct usb_hub *hub = usb_get_intfdata(intf);
2168 2209
2169 dev_dbg(&intf->dev, "%s\n", __func__); 2210 dev_dbg(&intf->dev, "%s\n", __func__);
2170 hub_power_on(hub); 2211 hub_activate(hub, HUB_RESET_RESUME);
2171 hub_restart(hub, HUB_RESET_RESUME);
2172 return 0; 2212 return 0;
2173} 2213}
2174 2214
@@ -2218,11 +2258,6 @@ static inline int remote_wakeup(struct usb_device *udev)
2218 * every 25ms for transient disconnects. When the port status has been 2258 * every 25ms for transient disconnects. When the port status has been
2219 * unchanged for 100ms it returns the port status. 2259 * unchanged for 100ms it returns the port status.
2220 */ 2260 */
2221
2222#define HUB_DEBOUNCE_TIMEOUT 1500
2223#define HUB_DEBOUNCE_STEP 25
2224#define HUB_DEBOUNCE_STABLE 100
2225
2226static int hub_port_debounce(struct usb_hub *hub, int port1) 2261static int hub_port_debounce(struct usb_hub *hub, int port1)
2227{ 2262{
2228 int ret; 2263 int ret;
@@ -2302,7 +2337,7 @@ static int hub_set_address(struct usb_device *udev, int devnum)
2302 * Returns device in USB_STATE_ADDRESS, except on error. 2337 * Returns device in USB_STATE_ADDRESS, except on error.
2303 * 2338 *
2304 * If this is called for an already-existing device (as part of 2339 * If this is called for an already-existing device (as part of
2305 * usb_reset_device), the caller must own the device lock. For a 2340 * usb_reset_and_verify_device), the caller must own the device lock. For a
2306 * newly detected device that is not accessible through any global 2341 * newly detected device that is not accessible through any global
2307 * pointers, it's not necessary to lock the device. 2342 * pointers, it's not necessary to lock the device.
2308 */ 2343 */
@@ -2619,7 +2654,7 @@ hub_power_remaining (struct usb_hub *hub)
2619 * This routine is called when: 2654 * This routine is called when:
2620 * a port connection-change occurs; 2655 * a port connection-change occurs;
2621 * a port enable-change occurs (often caused by EMI); 2656 * a port enable-change occurs (often caused by EMI);
2622 * usb_reset_device() encounters changed descriptors (as from 2657 * usb_reset_and_verify_device() encounters changed descriptors (as from
2623 * a firmware download) 2658 * a firmware download)
2624 * caller already locked the hub 2659 * caller already locked the hub
2625 */ 2660 */
@@ -2629,9 +2664,11 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2629 struct usb_device *hdev = hub->hdev; 2664 struct usb_device *hdev = hub->hdev;
2630 struct device *hub_dev = hub->intfdev; 2665 struct device *hub_dev = hub->intfdev;
2631 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 2666 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
2632 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2667 unsigned wHubCharacteristics =
2668 le16_to_cpu(hub->descriptor->wHubCharacteristics);
2669 struct usb_device *udev;
2633 int status, i; 2670 int status, i;
2634 2671
2635 dev_dbg (hub_dev, 2672 dev_dbg (hub_dev,
2636 "port %d, status %04x, change %04x, %s\n", 2673 "port %d, status %04x, change %04x, %s\n",
2637 port1, portstatus, portchange, portspeed (portstatus)); 2674 port1, portstatus, portchange, portspeed (portstatus));
@@ -2640,30 +2677,73 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2640 set_port_led(hub, port1, HUB_LED_AUTO); 2677 set_port_led(hub, port1, HUB_LED_AUTO);
2641 hub->indicator[port1-1] = INDICATOR_AUTO; 2678 hub->indicator[port1-1] = INDICATOR_AUTO;
2642 } 2679 }
2643
2644 /* Disconnect any existing devices under this port */
2645 if (hdev->children[port1-1])
2646 usb_disconnect(&hdev->children[port1-1]);
2647 clear_bit(port1, hub->change_bits);
2648 2680
2649#ifdef CONFIG_USB_OTG 2681#ifdef CONFIG_USB_OTG
2650 /* during HNP, don't repeat the debounce */ 2682 /* during HNP, don't repeat the debounce */
2651 if (hdev->bus->is_b_host) 2683 if (hdev->bus->is_b_host)
2652 portchange &= ~USB_PORT_STAT_C_CONNECTION; 2684 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
2685 USB_PORT_STAT_C_ENABLE);
2653#endif 2686#endif
2654 2687
2655 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2688 /* Try to use the debounce delay for protection against
2689 * port-enable changes caused, for example, by EMI.
2690 */
2691 if (portchange & (USB_PORT_STAT_C_CONNECTION |
2692 USB_PORT_STAT_C_ENABLE)) {
2656 status = hub_port_debounce(hub, port1); 2693 status = hub_port_debounce(hub, port1);
2657 if (status < 0) { 2694 if (status < 0) {
2658 if (printk_ratelimit()) 2695 if (printk_ratelimit())
2659 dev_err (hub_dev, "connect-debounce failed, " 2696 dev_err (hub_dev, "connect-debounce failed, "
2660 "port %d disabled\n", port1); 2697 "port %d disabled\n", port1);
2661 goto done; 2698 portstatus &= ~USB_PORT_STAT_CONNECTION;
2699 } else {
2700 portstatus = status;
2701 }
2702 }
2703
2704 /* Try to resuscitate an existing device */
2705 udev = hdev->children[port1-1];
2706 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
2707 udev->state != USB_STATE_NOTATTACHED) {
2708
2709 usb_lock_device(udev);
2710 if (portstatus & USB_PORT_STAT_ENABLE) {
2711 status = 0; /* Nothing to do */
2712 } else if (!udev->persist_enabled) {
2713 status = -ENODEV; /* Mustn't resuscitate */
2714
2715#ifdef CONFIG_USB_SUSPEND
2716 } else if (udev->state == USB_STATE_SUSPENDED) {
2717 /* For a suspended device, treat this as a
2718 * remote wakeup event.
2719 */
2720 if (udev->do_remote_wakeup)
2721 status = remote_wakeup(udev);
2722
2723 /* Otherwise leave it be; devices can't tell the
2724 * difference between suspended and disabled.
2725 */
2726 else
2727 status = 0;
2728#endif
2729
2730 } else {
2731 status = usb_reset_device(udev);
2732 }
2733 usb_unlock_device(udev);
2734
2735 if (status == 0) {
2736 clear_bit(port1, hub->change_bits);
2737 return;
2662 } 2738 }
2663 portstatus = status;
2664 } 2739 }
2665 2740
2666 /* Return now if nothing is connected */ 2741 /* Disconnect any existing devices under this port */
2742 if (udev)
2743 usb_disconnect(&hdev->children[port1-1]);
2744 clear_bit(port1, hub->change_bits);
2745
2746 /* Return now if debouncing failed or nothing is connected */
2667 if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 2747 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2668 2748
2669 /* maybe switch power back on (e.g. root hub was reset) */ 2749 /* maybe switch power back on (e.g. root hub was reset) */
@@ -2677,7 +2757,6 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2677 } 2757 }
2678 2758
2679 for (i = 0; i < SET_CONFIG_TRIES; i++) { 2759 for (i = 0; i < SET_CONFIG_TRIES; i++) {
2680 struct usb_device *udev;
2681 2760
2682 /* reallocate for each attempt, since references 2761 /* reallocate for each attempt, since references
2683 * to the previous one can escape in various ways 2762 * to the previous one can escape in various ways
@@ -2858,7 +2937,7 @@ static void hub_events(void)
2858 /* If the hub has died, clean up after it */ 2937 /* If the hub has died, clean up after it */
2859 if (hdev->state == USB_STATE_NOTATTACHED) { 2938 if (hdev->state == USB_STATE_NOTATTACHED) {
2860 hub->error = -ENODEV; 2939 hub->error = -ENODEV;
2861 hub_stop(hub); 2940 hub_quiesce(hub, HUB_DISCONNECT);
2862 goto loop; 2941 goto loop;
2863 } 2942 }
2864 2943
@@ -2877,7 +2956,7 @@ static void hub_events(void)
2877 dev_dbg (hub_dev, "resetting for error %d\n", 2956 dev_dbg (hub_dev, "resetting for error %d\n",
2878 hub->error); 2957 hub->error);
2879 2958
2880 ret = usb_reset_composite_device(hdev, intf); 2959 ret = usb_reset_device(hdev);
2881 if (ret) { 2960 if (ret) {
2882 dev_dbg (hub_dev, 2961 dev_dbg (hub_dev,
2883 "error resetting hub: %d\n", ret); 2962 "error resetting hub: %d\n", ret);
@@ -2894,7 +2973,7 @@ static void hub_events(void)
2894 continue; 2973 continue;
2895 connect_change = test_bit(i, hub->change_bits); 2974 connect_change = test_bit(i, hub->change_bits);
2896 if (!test_and_clear_bit(i, hub->event_bits) && 2975 if (!test_and_clear_bit(i, hub->event_bits) &&
2897 !connect_change && !hub->activating) 2976 !connect_change)
2898 continue; 2977 continue;
2899 2978
2900 ret = hub_port_status(hub, i, 2979 ret = hub_port_status(hub, i,
@@ -2902,11 +2981,6 @@ static void hub_events(void)
2902 if (ret < 0) 2981 if (ret < 0)
2903 continue; 2982 continue;
2904 2983
2905 if (hub->activating && !hdev->children[i-1] &&
2906 (portstatus &
2907 USB_PORT_STAT_CONNECTION))
2908 connect_change = 1;
2909
2910 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2984 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2911 clear_port_feature(hdev, i, 2985 clear_port_feature(hdev, i,
2912 USB_PORT_FEAT_C_CONNECTION); 2986 USB_PORT_FEAT_C_CONNECTION);
@@ -2941,11 +3015,16 @@ static void hub_events(void)
2941 } 3015 }
2942 3016
2943 if (portchange & USB_PORT_STAT_C_SUSPEND) { 3017 if (portchange & USB_PORT_STAT_C_SUSPEND) {
3018 struct usb_device *udev;
3019
2944 clear_port_feature(hdev, i, 3020 clear_port_feature(hdev, i,
2945 USB_PORT_FEAT_C_SUSPEND); 3021 USB_PORT_FEAT_C_SUSPEND);
2946 if (hdev->children[i-1]) { 3022 udev = hdev->children[i-1];
3023 if (udev) {
3024 usb_lock_device(udev);
2947 ret = remote_wakeup(hdev-> 3025 ret = remote_wakeup(hdev->
2948 children[i-1]); 3026 children[i-1]);
3027 usb_unlock_device(udev);
2949 if (ret < 0) 3028 if (ret < 0)
2950 connect_change = 1; 3029 connect_change = 1;
2951 } else { 3030 } else {
@@ -3002,8 +3081,6 @@ static void hub_events(void)
3002 } 3081 }
3003 } 3082 }
3004 3083
3005 hub->activating = 0;
3006
3007 /* If this is a root hub, tell the HCD it's okay to 3084 /* If this is a root hub, tell the HCD it's okay to
3008 * re-enable port-change interrupts now. */ 3085 * re-enable port-change interrupts now. */
3009 if (!hdev->parent && !hub->busy_bits[0]) 3086 if (!hdev->parent && !hub->busy_bits[0])
@@ -3172,12 +3249,12 @@ static int descriptors_changed(struct usb_device *udev,
3172} 3249}
3173 3250
3174/** 3251/**
3175 * usb_reset_device - perform a USB port reset to reinitialize a device 3252 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
3176 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 3253 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
3177 * 3254 *
3178 * WARNING - don't use this routine to reset a composite device 3255 * WARNING - don't use this routine to reset a composite device
3179 * (one with multiple interfaces owned by separate drivers)! 3256 * (one with multiple interfaces owned by separate drivers)!
3180 * Use usb_reset_composite_device() instead. 3257 * Use usb_reset_device() instead.
3181 * 3258 *
3182 * Do a port reset, reassign the device's address, and establish its 3259 * Do a port reset, reassign the device's address, and establish its
3183 * former operating configuration. If the reset fails, or the device's 3260 * former operating configuration. If the reset fails, or the device's
@@ -3201,7 +3278,7 @@ static int descriptors_changed(struct usb_device *udev,
3201 * holding the device lock because these tasks should always call 3278 * holding the device lock because these tasks should always call
3202 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. 3279 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
3203 */ 3280 */
3204int usb_reset_device(struct usb_device *udev) 3281static int usb_reset_and_verify_device(struct usb_device *udev)
3205{ 3282{
3206 struct usb_device *parent_hdev = udev->parent; 3283 struct usb_device *parent_hdev = udev->parent;
3207 struct usb_hub *parent_hub; 3284 struct usb_hub *parent_hub;
@@ -3289,26 +3366,28 @@ re_enumerate:
3289 hub_port_logical_disconnect(parent_hub, port1); 3366 hub_port_logical_disconnect(parent_hub, port1);
3290 return -ENODEV; 3367 return -ENODEV;
3291} 3368}
3292EXPORT_SYMBOL_GPL(usb_reset_device);
3293 3369
3294/** 3370/**
3295 * usb_reset_composite_device - warn interface drivers and perform a USB port reset 3371 * usb_reset_device - warn interface drivers and perform a USB port reset
3296 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 3372 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
3297 * @iface: interface bound to the driver making the request (optional)
3298 * 3373 *
3299 * Warns all drivers bound to registered interfaces (using their pre_reset 3374 * Warns all drivers bound to registered interfaces (using their pre_reset
3300 * method), performs the port reset, and then lets the drivers know that 3375 * method), performs the port reset, and then lets the drivers know that
3301 * the reset is over (using their post_reset method). 3376 * the reset is over (using their post_reset method).
3302 * 3377 *
3303 * Return value is the same as for usb_reset_device(). 3378 * Return value is the same as for usb_reset_and_verify_device().
3304 * 3379 *
3305 * The caller must own the device lock. For example, it's safe to use 3380 * The caller must own the device lock. For example, it's safe to use
3306 * this from a driver probe() routine after downloading new firmware. 3381 * this from a driver probe() routine after downloading new firmware.
3307 * For calls that might not occur during probe(), drivers should lock 3382 * For calls that might not occur during probe(), drivers should lock
3308 * the device using usb_lock_device_for_reset(). 3383 * the device using usb_lock_device_for_reset().
3384 *
3385 * If an interface is currently being probed or disconnected, we assume
3386 * its driver knows how to handle resets. For all other interfaces,
3387 * if the driver doesn't have pre_reset and post_reset methods then
3388 * we attempt to unbind it and rebind afterward.
3309 */ 3389 */
3310int usb_reset_composite_device(struct usb_device *udev, 3390int usb_reset_device(struct usb_device *udev)
3311 struct usb_interface *iface)
3312{ 3391{
3313 int ret; 3392 int ret;
3314 int i; 3393 int i;
@@ -3324,40 +3403,47 @@ int usb_reset_composite_device(struct usb_device *udev,
3324 /* Prevent autosuspend during the reset */ 3403 /* Prevent autosuspend during the reset */
3325 usb_autoresume_device(udev); 3404 usb_autoresume_device(udev);
3326 3405
3327 if (iface && iface->condition != USB_INTERFACE_BINDING)
3328 iface = NULL;
3329
3330 if (config) { 3406 if (config) {
3331 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 3407 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3332 struct usb_interface *cintf = config->interface[i]; 3408 struct usb_interface *cintf = config->interface[i];
3333 struct usb_driver *drv; 3409 struct usb_driver *drv;
3410 int unbind = 0;
3334 3411
3335 if (cintf->dev.driver) { 3412 if (cintf->dev.driver) {
3336 drv = to_usb_driver(cintf->dev.driver); 3413 drv = to_usb_driver(cintf->dev.driver);
3337 if (drv->pre_reset) 3414 if (drv->pre_reset && drv->post_reset)
3338 (drv->pre_reset)(cintf); 3415 unbind = (drv->pre_reset)(cintf);
3339 /* FIXME: Unbind if pre_reset returns an error or isn't defined */ 3416 else if (cintf->condition ==
3417 USB_INTERFACE_BOUND)
3418 unbind = 1;
3419 if (unbind)
3420 usb_forced_unbind_intf(cintf);
3340 } 3421 }
3341 } 3422 }
3342 } 3423 }
3343 3424
3344 ret = usb_reset_device(udev); 3425 ret = usb_reset_and_verify_device(udev);
3345 3426
3346 if (config) { 3427 if (config) {
3347 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 3428 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3348 struct usb_interface *cintf = config->interface[i]; 3429 struct usb_interface *cintf = config->interface[i];
3349 struct usb_driver *drv; 3430 struct usb_driver *drv;
3431 int rebind = cintf->needs_binding;
3350 3432
3351 if (cintf->dev.driver) { 3433 if (!rebind && cintf->dev.driver) {
3352 drv = to_usb_driver(cintf->dev.driver); 3434 drv = to_usb_driver(cintf->dev.driver);
3353 if (drv->post_reset) 3435 if (drv->post_reset)
3354 (drv->post_reset)(cintf); 3436 rebind = (drv->post_reset)(cintf);
3355 /* FIXME: Unbind if post_reset returns an error or isn't defined */ 3437 else if (cintf->condition ==
3438 USB_INTERFACE_BOUND)
3439 rebind = 1;
3356 } 3440 }
3441 if (rebind)
3442 usb_rebind_intf(cintf);
3357 } 3443 }
3358 } 3444 }
3359 3445
3360 usb_autosuspend_device(udev); 3446 usb_autosuspend_device(udev);
3361 return ret; 3447 return ret;
3362} 3448}
3363EXPORT_SYMBOL_GPL(usb_reset_composite_device); 3449EXPORT_SYMBOL_GPL(usb_reset_device);
diff --git a/drivers/usb/core/inode.c b/drivers/usb/core/inode.c
index 1d253dd4ea81..db410e92c80d 100644
--- a/drivers/usb/core/inode.c
+++ b/drivers/usb/core/inode.c
@@ -712,25 +712,11 @@ static void usbfs_add_device(struct usb_device *dev)
712 712
713static void usbfs_remove_device(struct usb_device *dev) 713static void usbfs_remove_device(struct usb_device *dev)
714{ 714{
715 struct dev_state *ds;
716 struct siginfo sinfo;
717
718 if (dev->usbfs_dentry) { 715 if (dev->usbfs_dentry) {
719 fs_remove_file (dev->usbfs_dentry); 716 fs_remove_file (dev->usbfs_dentry);
720 dev->usbfs_dentry = NULL; 717 dev->usbfs_dentry = NULL;
721 } 718 }
722 while (!list_empty(&dev->filelist)) { 719 usb_fs_classdev_common_remove(dev);
723 ds = list_entry(dev->filelist.next, struct dev_state, list);
724 wake_up_all(&ds->wait);
725 list_del_init(&ds->list);
726 if (ds->discsignr) {
727 sinfo.si_signo = ds->discsignr;
728 sinfo.si_errno = EPIPE;
729 sinfo.si_code = SI_ASYNCIO;
730 sinfo.si_addr = ds->disccontext;
731 kill_pid_info_as_uid(ds->discsignr, &sinfo, ds->disc_pid, ds->disc_uid, ds->disc_euid, ds->secid);
732 }
733 }
734} 720}
735 721
736static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev) 722static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index fe47d145255a..2fcc06eb5e60 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -400,7 +400,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
400 if (usb_pipein(pipe)) 400 if (usb_pipein(pipe))
401 urb_flags |= URB_SHORT_NOT_OK; 401 urb_flags |= URB_SHORT_NOT_OK;
402 402
403 for (i = 0; i < io->entries; i++) { 403 for_each_sg(sg, sg, io->entries, i) {
404 unsigned len; 404 unsigned len;
405 405
406 io->urbs[i] = usb_alloc_urb(0, mem_flags); 406 io->urbs[i] = usb_alloc_urb(0, mem_flags);
@@ -434,17 +434,17 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
434 * to prevent stale pointers and to help spot bugs. 434 * to prevent stale pointers and to help spot bugs.
435 */ 435 */
436 if (dma) { 436 if (dma) {
437 io->urbs[i]->transfer_dma = sg_dma_address(sg + i); 437 io->urbs[i]->transfer_dma = sg_dma_address(sg);
438 len = sg_dma_len(sg + i); 438 len = sg_dma_len(sg);
439#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU) 439#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
440 io->urbs[i]->transfer_buffer = NULL; 440 io->urbs[i]->transfer_buffer = NULL;
441#else 441#else
442 io->urbs[i]->transfer_buffer = sg_virt(&sg[i]); 442 io->urbs[i]->transfer_buffer = sg_virt(sg);
443#endif 443#endif
444 } else { 444 } else {
445 /* hc may use _only_ transfer_buffer */ 445 /* hc may use _only_ transfer_buffer */
446 io->urbs[i]->transfer_buffer = sg_virt(&sg[i]); 446 io->urbs[i]->transfer_buffer = sg_virt(sg);
447 len = sg[i].length; 447 len = sg->length;
448 } 448 }
449 449
450 if (length) { 450 if (length) {
@@ -1090,7 +1090,7 @@ void usb_disable_device(struct usb_device *dev, int skip_ep0)
1090 if (!device_is_registered(&interface->dev)) 1090 if (!device_is_registered(&interface->dev))
1091 continue; 1091 continue;
1092 dev_dbg(&dev->dev, "unregistering interface %s\n", 1092 dev_dbg(&dev->dev, "unregistering interface %s\n",
1093 interface->dev.bus_id); 1093 dev_name(&interface->dev));
1094 device_del(&interface->dev); 1094 device_del(&interface->dev);
1095 usb_remove_sysfs_intf_files(interface); 1095 usb_remove_sysfs_intf_files(interface);
1096 } 1096 }
@@ -1476,7 +1476,7 @@ static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1476 * 1476 *
1477 * This call is synchronous. The calling context must be able to sleep, 1477 * This call is synchronous. The calling context must be able to sleep,
1478 * must own the device lock, and must not hold the driver model's USB 1478 * must own the device lock, and must not hold the driver model's USB
1479 * bus mutex; usb device driver probe() methods cannot use this routine. 1479 * bus mutex; usb interface driver probe() methods cannot use this routine.
1480 * 1480 *
1481 * Returns zero on success, or else the status code returned by the 1481 * Returns zero on success, or else the status code returned by the
1482 * underlying call that failed. On successful completion, each interface 1482 * underlying call that failed. On successful completion, each interface
@@ -1611,7 +1611,7 @@ free_interfaces:
1611 intf->dev.dma_mask = dev->dev.dma_mask; 1611 intf->dev.dma_mask = dev->dev.dma_mask;
1612 device_initialize(&intf->dev); 1612 device_initialize(&intf->dev);
1613 mark_quiesced(intf); 1613 mark_quiesced(intf);
1614 sprintf(&intf->dev.bus_id[0], "%d-%s:%d.%d", 1614 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1615 dev->bus->busnum, dev->devpath, 1615 dev->bus->busnum, dev->devpath,
1616 configuration, alt->desc.bInterfaceNumber); 1616 configuration, alt->desc.bInterfaceNumber);
1617 } 1617 }
@@ -1631,12 +1631,12 @@ free_interfaces:
1631 1631
1632 dev_dbg(&dev->dev, 1632 dev_dbg(&dev->dev,
1633 "adding %s (config #%d, interface %d)\n", 1633 "adding %s (config #%d, interface %d)\n",
1634 intf->dev.bus_id, configuration, 1634 dev_name(&intf->dev), configuration,
1635 intf->cur_altsetting->desc.bInterfaceNumber); 1635 intf->cur_altsetting->desc.bInterfaceNumber);
1636 ret = device_add(&intf->dev); 1636 ret = device_add(&intf->dev);
1637 if (ret != 0) { 1637 if (ret != 0) {
1638 dev_err(&dev->dev, "device_add(%s) --> %d\n", 1638 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1639 intf->dev.bus_id, ret); 1639 dev_name(&intf->dev), ret);
1640 continue; 1640 continue;
1641 } 1641 }
1642 usb_create_sysfs_intf_files(intf); 1642 usb_create_sysfs_intf_files(intf);
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 325774375837..84fcaa6a21ec 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -308,7 +308,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
308 * by location for diagnostics, tools, driver model, etc. The 308 * by location for diagnostics, tools, driver model, etc. The
309 * string is a path along hub ports, from the root. Each device's 309 * string is a path along hub ports, from the root. Each device's
310 * dev->devpath will be stable until USB is re-cabled, and hubs 310 * dev->devpath will be stable until USB is re-cabled, and hubs
311 * are often labeled with these port numbers. The bus_id isn't 311 * are often labeled with these port numbers. The name isn't
312 * as stable: bus->busnum changes easily from modprobe order, 312 * as stable: bus->busnum changes easily from modprobe order,
313 * cardbus or pci hotplugging, and so on. 313 * cardbus or pci hotplugging, and so on.
314 */ 314 */
@@ -316,7 +316,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
316 dev->devpath[0] = '0'; 316 dev->devpath[0] = '0';
317 317
318 dev->dev.parent = bus->controller; 318 dev->dev.parent = bus->controller;
319 sprintf(&dev->dev.bus_id[0], "usb%d", bus->busnum); 319 dev_set_name(&dev->dev, "usb%d", bus->busnum);
320 root_hub = 1; 320 root_hub = 1;
321 } else { 321 } else {
322 /* match any labeling on the hubs; it's one-based */ 322 /* match any labeling on the hubs; it's one-based */
@@ -328,8 +328,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
328 "%s.%d", parent->devpath, port1); 328 "%s.%d", parent->devpath, port1);
329 329
330 dev->dev.parent = &parent->dev; 330 dev->dev.parent = &parent->dev;
331 sprintf(&dev->dev.bus_id[0], "%d-%s", 331 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
332 bus->busnum, dev->devpath);
333 332
334 /* hub driver sets up TT records */ 333 /* hub driver sets up TT records */
335 } 334 }
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 1a8bc21c335e..d9a6e16dbf84 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -29,6 +29,8 @@ extern int usb_choose_configuration(struct usb_device *udev);
29extern void usb_kick_khubd(struct usb_device *dev); 29extern void usb_kick_khubd(struct usb_device *dev);
30extern int usb_match_device(struct usb_device *dev, 30extern int usb_match_device(struct usb_device *dev,
31 const struct usb_device_id *id); 31 const struct usb_device_id *id);
32extern void usb_forced_unbind_intf(struct usb_interface *intf);
33extern void usb_rebind_intf(struct usb_interface *intf);
32 34
33extern int usb_hub_init(void); 35extern int usb_hub_init(void);
34extern void usb_hub_cleanup(void); 36extern void usb_hub_cleanup(void);
@@ -140,26 +142,11 @@ extern struct usb_driver usbfs_driver;
140extern const struct file_operations usbfs_devices_fops; 142extern const struct file_operations usbfs_devices_fops;
141extern const struct file_operations usbdev_file_operations; 143extern const struct file_operations usbdev_file_operations;
142extern void usbfs_conn_disc_event(void); 144extern void usbfs_conn_disc_event(void);
145extern void usb_fs_classdev_common_remove(struct usb_device *udev);
143 146
144extern int usb_devio_init(void); 147extern int usb_devio_init(void);
145extern void usb_devio_cleanup(void); 148extern void usb_devio_cleanup(void);
146 149
147struct dev_state {
148 struct list_head list; /* state list */
149 struct usb_device *dev;
150 struct file *file;
151 spinlock_t lock; /* protects the async urb lists */
152 struct list_head async_pending;
153 struct list_head async_completed;
154 wait_queue_head_t wait; /* wake up if a request completed */
155 unsigned int discsignr;
156 struct pid *disc_pid;
157 uid_t disc_uid, disc_euid;
158 void __user *disccontext;
159 unsigned long ifclaimed;
160 u32 secid;
161};
162
163/* internal notify stuff */ 150/* internal notify stuff */
164extern void usb_notify_add_device(struct usb_device *udev); 151extern void usb_notify_add_device(struct usb_device *udev);
165extern void usb_notify_remove_device(struct usb_device *udev); 152extern void usb_notify_remove_device(struct usb_device *udev);
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index d6bab0d5f453..c6a8c6b1116a 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -586,6 +586,20 @@ config USB_G_PRINTER
586 For more information, see Documentation/usb/gadget_printer.txt 586 For more information, see Documentation/usb/gadget_printer.txt
587 which includes sample code for accessing the device file. 587 which includes sample code for accessing the device file.
588 588
589config USB_CDC_COMPOSITE
590 tristate "CDC Composite Device (Ethernet and ACM)"
591 depends on NET
592 help
593 This driver provides two functions in one configuration:
594 a CDC Ethernet (ECM) link, and a CDC ACM (serial port) link.
595
596 This driver requires four bulk and two interrupt endpoints,
597 plus the ability to handle altsettings. Not all peripheral
598 controllers are that capable.
599
600 Say "y" to link the driver statically, or "m" to build a
601 dynamically linked module.
602
589# put drivers that need isochronous transfer support (for audio 603# put drivers that need isochronous transfer support (for audio
590# or video class gadget drivers), or specific hardware, here. 604# or video class gadget drivers), or specific hardware, here.
591 605
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index e258afd25faf..fcb5cb9094d9 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -22,18 +22,22 @@ obj-$(CONFIG_USB_M66592) += m66592-udc.o
22# 22#
23# USB gadget drivers 23# USB gadget drivers
24# 24#
25g_zero-objs := zero.o usbstring.o config.o epautoconf.o 25C_UTILS = composite.o usbstring.o config.o epautoconf.o
26g_ether-objs := ether.o usbstring.o config.o epautoconf.o 26
27g_serial-objs := serial.o usbstring.o config.o epautoconf.o 27g_zero-objs := zero.o f_sourcesink.o f_loopback.o $(C_UTILS)
28g_ether-objs := ether.o u_ether.o f_subset.o f_ecm.o $(C_UTILS)
29g_serial-objs := serial.o u_serial.o f_acm.o f_serial.o $(C_UTILS)
28g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o 30g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o
29gadgetfs-objs := inode.o 31gadgetfs-objs := inode.o
30g_file_storage-objs := file_storage.o usbstring.o config.o \ 32g_file_storage-objs := file_storage.o usbstring.o config.o \
31 epautoconf.o 33 epautoconf.o
32g_printer-objs := printer.o usbstring.o config.o \ 34g_printer-objs := printer.o usbstring.o config.o \
33 epautoconf.o 35 epautoconf.o
36g_cdc-objs := cdc2.o u_ether.o f_ecm.o \
37 u_serial.o f_acm.o $(C_UTILS)
34 38
35ifeq ($(CONFIG_USB_ETH_RNDIS),y) 39ifeq ($(CONFIG_USB_ETH_RNDIS),y)
36 g_ether-objs += rndis.o 40 g_ether-objs += f_rndis.o rndis.o
37endif 41endif
38 42
39obj-$(CONFIG_USB_ZERO) += g_zero.o 43obj-$(CONFIG_USB_ZERO) += g_zero.o
@@ -43,4 +47,5 @@ obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
43obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 47obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
44obj-$(CONFIG_USB_G_PRINTER) += g_printer.o 48obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
45obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o 49obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
50obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
46 51
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index f261d2a9a5f0..1500e1b3c302 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -3342,7 +3342,7 @@ static int udc_probe(struct udc *dev)
3342 spin_lock_init(&dev->lock); 3342 spin_lock_init(&dev->lock);
3343 dev->gadget.ops = &udc_ops; 3343 dev->gadget.ops = &udc_ops;
3344 3344
3345 strcpy(dev->gadget.dev.bus_id, "gadget"); 3345 dev_set_name(&dev->gadget.dev, "gadget");
3346 dev->gadget.dev.release = gadget_release; 3346 dev->gadget.dev.release = gadget_release;
3347 dev->gadget.name = name; 3347 dev->gadget.name = name;
3348 dev->gadget.name = name; 3348 dev->gadget.name = name;
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index b6b2a0a5ba37..e2d8a5d86c40 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -1687,6 +1687,19 @@ static int __init at91udc_probe(struct platform_device *pdev)
1687 udc->board.pullup_active_low); 1687 udc->board.pullup_active_low);
1688 } 1688 }
1689 1689
1690 /* newer chips have more FIFO memory than rm9200 */
1691 if (cpu_is_at91sam9260()) {
1692 udc->ep[0].maxpacket = 64;
1693 udc->ep[3].maxpacket = 64;
1694 udc->ep[4].maxpacket = 512;
1695 udc->ep[5].maxpacket = 512;
1696 } else if (cpu_is_at91sam9261()) {
1697 udc->ep[3].maxpacket = 64;
1698 } else if (cpu_is_at91sam9263()) {
1699 udc->ep[0].maxpacket = 64;
1700 udc->ep[3].maxpacket = 64;
1701 }
1702
1690 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1); 1703 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
1691 if (!udc->udp_baseaddr) { 1704 if (!udc->udp_baseaddr) {
1692 retval = -ENOMEM; 1705 retval = -ENOMEM;
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c
new file mode 100644
index 000000000000..d490d0289507
--- /dev/null
+++ b/drivers/usb/gadget/cdc2.c
@@ -0,0 +1,246 @@
1/*
2 * cdc2.c -- CDC Composite driver, with ECM and ACM support
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
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#include <linux/kernel.h>
23#include <linux/utsname.h>
24
25#include "u_ether.h"
26#include "u_serial.h"
27
28
29#define DRIVER_DESC "CDC Composite Gadget"
30#define DRIVER_VERSION "King Kamehameha Day 2008"
31
32/*-------------------------------------------------------------------------*/
33
34/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
35 * Instead: allocate your own, using normal USB-IF procedures.
36 */
37
38/* Thanks to NetChip Technologies for donating this product ID.
39 * It's for devices with only this composite CDC configuration.
40 */
41#define CDC_VENDOR_NUM 0x0525 /* NetChip */
42#define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
43
44/*-------------------------------------------------------------------------*/
45
46static struct usb_device_descriptor device_desc = {
47 .bLength = sizeof device_desc,
48 .bDescriptorType = USB_DT_DEVICE,
49
50 .bcdUSB = __constant_cpu_to_le16(0x0200),
51
52 .bDeviceClass = USB_CLASS_COMM,
53 .bDeviceSubClass = 0,
54 .bDeviceProtocol = 0,
55 /* .bMaxPacketSize0 = f(hardware) */
56
57 /* Vendor and product id can be overridden by module parameters. */
58 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
59 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
60 /* .bcdDevice = f(hardware) */
61 /* .iManufacturer = DYNAMIC */
62 /* .iProduct = DYNAMIC */
63 /* NO SERIAL NUMBER */
64 .bNumConfigurations = 1,
65};
66
67static struct usb_otg_descriptor otg_descriptor = {
68 .bLength = sizeof otg_descriptor,
69 .bDescriptorType = USB_DT_OTG,
70
71 /* REVISIT SRP-only hardware is possible, although
72 * it would not be called "OTG" ...
73 */
74 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
75};
76
77static const struct usb_descriptor_header *otg_desc[] = {
78 (struct usb_descriptor_header *) &otg_descriptor,
79 NULL,
80};
81
82
83/* string IDs are assigned dynamically */
84
85#define STRING_MANUFACTURER_IDX 0
86#define STRING_PRODUCT_IDX 1
87
88static char manufacturer[50];
89
90static struct usb_string strings_dev[] = {
91 [STRING_MANUFACTURER_IDX].s = manufacturer,
92 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
93 { } /* end of list */
94};
95
96static struct usb_gadget_strings stringtab_dev = {
97 .language = 0x0409, /* en-us */
98 .strings = strings_dev,
99};
100
101static struct usb_gadget_strings *dev_strings[] = {
102 &stringtab_dev,
103 NULL,
104};
105
106static u8 hostaddr[ETH_ALEN];
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 * We _always_ have both CDC ECM and CDC ACM functions.
112 */
113static int __init cdc_do_config(struct usb_configuration *c)
114{
115 int status;
116
117 if (gadget_is_otg(c->cdev->gadget)) {
118 c->descriptors = otg_desc;
119 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
120 }
121
122 status = ecm_bind_config(c, hostaddr);
123 if (status < 0)
124 return status;
125
126 status = acm_bind_config(c, 0);
127 if (status < 0)
128 return status;
129
130 return 0;
131}
132
133static struct usb_configuration cdc_config_driver = {
134 .label = "CDC Composite (ECM + ACM)",
135 .bind = cdc_do_config,
136 .bConfigurationValue = 1,
137 /* .iConfiguration = DYNAMIC */
138 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
139 .bMaxPower = 1, /* 2 mA, minimal */
140};
141
142/*-------------------------------------------------------------------------*/
143
144static int __init cdc_bind(struct usb_composite_dev *cdev)
145{
146 int gcnum;
147 struct usb_gadget *gadget = cdev->gadget;
148 int status;
149
150 if (!can_support_ecm(cdev->gadget)) {
151 ERROR(cdev, "controller '%s' not usable\n", gadget->name);
152 return -EINVAL;
153 }
154
155 /* set up network link layer */
156 status = gether_setup(cdev->gadget, hostaddr);
157 if (status < 0)
158 return status;
159
160 /* set up serial link layer */
161 status = gserial_setup(cdev->gadget, 1);
162 if (status < 0)
163 goto fail0;
164
165 gcnum = usb_gadget_controller_number(gadget);
166 if (gcnum >= 0)
167 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
168 else {
169 /* We assume that can_support_ecm() tells the truth;
170 * but if the controller isn't recognized at all then
171 * that assumption is a bit more likely to be wrong.
172 */
173 WARN(cdev, "controller '%s' not recognized; trying %s\n",
174 gadget->name,
175 cdc_config_driver.label);
176 device_desc.bcdDevice =
177 __constant_cpu_to_le16(0x0300 | 0x0099);
178 }
179
180
181 /* Allocate string descriptor numbers ... note that string
182 * contents can be overridden by the composite_dev glue.
183 */
184
185 /* device descriptor strings: manufacturer, product */
186 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
187 init_utsname()->sysname, init_utsname()->release,
188 gadget->name);
189 status = usb_string_id(cdev);
190 if (status < 0)
191 goto fail1;
192 strings_dev[STRING_MANUFACTURER_IDX].id = status;
193 device_desc.iManufacturer = status;
194
195 status = usb_string_id(cdev);
196 if (status < 0)
197 goto fail1;
198 strings_dev[STRING_PRODUCT_IDX].id = status;
199 device_desc.iProduct = status;
200
201 /* register our configuration */
202 status = usb_add_config(cdev, &cdc_config_driver);
203 if (status < 0)
204 goto fail1;
205
206 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC);
207
208 return 0;
209
210fail1:
211 gserial_cleanup();
212fail0:
213 gether_cleanup();
214 return status;
215}
216
217static int __exit cdc_unbind(struct usb_composite_dev *cdev)
218{
219 gserial_cleanup();
220 gether_cleanup();
221 return 0;
222}
223
224static struct usb_composite_driver cdc_driver = {
225 .name = "g_cdc",
226 .dev = &device_desc,
227 .strings = dev_strings,
228 .bind = cdc_bind,
229 .unbind = __exit_p(cdc_unbind),
230};
231
232MODULE_DESCRIPTION(DRIVER_DESC);
233MODULE_AUTHOR("David Brownell");
234MODULE_LICENSE("GPL");
235
236static int __init init(void)
237{
238 return usb_composite_register(&cdc_driver);
239}
240module_init(init);
241
242static void __exit cleanup(void)
243{
244 usb_composite_unregister(&cdc_driver);
245}
246module_exit(cleanup);
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
new file mode 100644
index 000000000000..85c876c1f150
--- /dev/null
+++ b/drivers/usb/gadget/composite.c
@@ -0,0 +1,1041 @@
1/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/* #define VERBOSE_DEBUG */
22
23#include <linux/kallsyms.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/device.h>
27
28#include <linux/usb/composite.h>
29
30
31/*
32 * The code in this file is utility code, used to build a gadget driver
33 * from one or more "function" drivers, one or more "configuration"
34 * objects, and a "usb_composite_driver" by gluing them together along
35 * with the relevant device-wide data.
36 */
37
38/* big enough to hold our biggest descriptor */
39#define USB_BUFSIZ 512
40
41static struct usb_composite_driver *composite;
42
43/* Some systems will need runtime overrides for the product identifers
44 * published in the device descriptor, either numbers or strings or both.
45 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
46 */
47
48static ushort idVendor;
49module_param(idVendor, ushort, 0);
50MODULE_PARM_DESC(idVendor, "USB Vendor ID");
51
52static ushort idProduct;
53module_param(idProduct, ushort, 0);
54MODULE_PARM_DESC(idProduct, "USB Product ID");
55
56static ushort bcdDevice;
57module_param(bcdDevice, ushort, 0);
58MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
59
60static char *iManufacturer;
61module_param(iManufacturer, charp, 0);
62MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
63
64static char *iProduct;
65module_param(iProduct, charp, 0);
66MODULE_PARM_DESC(iProduct, "USB Product string");
67
68static char *iSerialNumber;
69module_param(iSerialNumber, charp, 0);
70MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
71
72/*-------------------------------------------------------------------------*/
73
74/**
75 * usb_add_function() - add a function to a configuration
76 * @config: the configuration
77 * @function: the function being added
78 * Context: single threaded during gadget setup
79 *
80 * After initialization, each configuration must have one or more
81 * functions added to it. Adding a function involves calling its @bind()
82 * method to allocate resources such as interface and string identifiers
83 * and endpoints.
84 *
85 * This function returns the value of the function's bind(), which is
86 * zero for success else a negative errno value.
87 */
88int __init usb_add_function(struct usb_configuration *config,
89 struct usb_function *function)
90{
91 int value = -EINVAL;
92
93 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
94 function->name, function,
95 config->label, config);
96
97 if (!function->set_alt || !function->disable)
98 goto done;
99
100 function->config = config;
101 list_add_tail(&function->list, &config->functions);
102
103 /* REVISIT *require* function->bind? */
104 if (function->bind) {
105 value = function->bind(config, function);
106 if (value < 0) {
107 list_del(&function->list);
108 function->config = NULL;
109 }
110 } else
111 value = 0;
112
113 /* We allow configurations that don't work at both speeds.
114 * If we run into a lowspeed Linux system, treat it the same
115 * as full speed ... it's the function drivers that will need
116 * to avoid bulk and ISO transfers.
117 */
118 if (!config->fullspeed && function->descriptors)
119 config->fullspeed = true;
120 if (!config->highspeed && function->hs_descriptors)
121 config->highspeed = true;
122
123done:
124 if (value)
125 DBG(config->cdev, "adding '%s'/%p --> %d\n",
126 function->name, function, value);
127 return value;
128}
129
130/**
131 * usb_interface_id() - allocate an unused interface ID
132 * @config: configuration associated with the interface
133 * @function: function handling the interface
134 * Context: single threaded during gadget setup
135 *
136 * usb_interface_id() is called from usb_function.bind() callbacks to
137 * allocate new interface IDs. The function driver will then store that
138 * ID in interface, association, CDC union, and other descriptors. It
139 * will also handle any control requests targetted at that interface,
140 * particularly changing its altsetting via set_alt(). There may
141 * also be class-specific or vendor-specific requests to handle.
142 *
143 * All interface identifier should be allocated using this routine, to
144 * ensure that for example different functions don't wrongly assign
145 * different meanings to the same identifier. Note that since interface
146 * identifers are configuration-specific, functions used in more than
147 * one configuration (or more than once in a given configuration) need
148 * multiple versions of the relevant descriptors.
149 *
150 * Returns the interface ID which was allocated; or -ENODEV if no
151 * more interface IDs can be allocated.
152 */
153int __init usb_interface_id(struct usb_configuration *config,
154 struct usb_function *function)
155{
156 unsigned id = config->next_interface_id;
157
158 if (id < MAX_CONFIG_INTERFACES) {
159 config->interface[id] = function;
160 config->next_interface_id = id + 1;
161 return id;
162 }
163 return -ENODEV;
164}
165
166static int config_buf(struct usb_configuration *config,
167 enum usb_device_speed speed, void *buf, u8 type)
168{
169 struct usb_config_descriptor *c = buf;
170 void *next = buf + USB_DT_CONFIG_SIZE;
171 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
172 struct usb_function *f;
173 int status;
174
175 /* write the config descriptor */
176 c = buf;
177 c->bLength = USB_DT_CONFIG_SIZE;
178 c->bDescriptorType = type;
179 /* wTotalLength is written later */
180 c->bNumInterfaces = config->next_interface_id;
181 c->bConfigurationValue = config->bConfigurationValue;
182 c->iConfiguration = config->iConfiguration;
183 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
184 c->bMaxPower = config->bMaxPower;
185
186 /* There may be e.g. OTG descriptors */
187 if (config->descriptors) {
188 status = usb_descriptor_fillbuf(next, len,
189 config->descriptors);
190 if (status < 0)
191 return status;
192 len -= status;
193 next += status;
194 }
195
196 /* add each function's descriptors */
197 list_for_each_entry(f, &config->functions, list) {
198 struct usb_descriptor_header **descriptors;
199
200 if (speed == USB_SPEED_HIGH)
201 descriptors = f->hs_descriptors;
202 else
203 descriptors = f->descriptors;
204 if (!descriptors)
205 continue;
206 status = usb_descriptor_fillbuf(next, len,
207 (const struct usb_descriptor_header **) descriptors);
208 if (status < 0)
209 return status;
210 len -= status;
211 next += status;
212 }
213
214 len = next - buf;
215 c->wTotalLength = cpu_to_le16(len);
216 return len;
217}
218
219static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
220{
221 struct usb_gadget *gadget = cdev->gadget;
222 struct usb_configuration *c;
223 u8 type = w_value >> 8;
224 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
225
226 if (gadget_is_dualspeed(gadget)) {
227 int hs = 0;
228
229 if (gadget->speed == USB_SPEED_HIGH)
230 hs = 1;
231 if (type == USB_DT_OTHER_SPEED_CONFIG)
232 hs = !hs;
233 if (hs)
234 speed = USB_SPEED_HIGH;
235
236 }
237
238 /* This is a lookup by config *INDEX* */
239 w_value &= 0xff;
240 list_for_each_entry(c, &cdev->configs, list) {
241 /* ignore configs that won't work at this speed */
242 if (speed == USB_SPEED_HIGH) {
243 if (!c->highspeed)
244 continue;
245 } else {
246 if (!c->fullspeed)
247 continue;
248 }
249 if (w_value == 0)
250 return config_buf(c, speed, cdev->req->buf, type);
251 w_value--;
252 }
253 return -EINVAL;
254}
255
256static int count_configs(struct usb_composite_dev *cdev, unsigned type)
257{
258 struct usb_gadget *gadget = cdev->gadget;
259 struct usb_configuration *c;
260 unsigned count = 0;
261 int hs = 0;
262
263 if (gadget_is_dualspeed(gadget)) {
264 if (gadget->speed == USB_SPEED_HIGH)
265 hs = 1;
266 if (type == USB_DT_DEVICE_QUALIFIER)
267 hs = !hs;
268 }
269 list_for_each_entry(c, &cdev->configs, list) {
270 /* ignore configs that won't work at this speed */
271 if (hs) {
272 if (!c->highspeed)
273 continue;
274 } else {
275 if (!c->fullspeed)
276 continue;
277 }
278 count++;
279 }
280 return count;
281}
282
283static void device_qual(struct usb_composite_dev *cdev)
284{
285 struct usb_qualifier_descriptor *qual = cdev->req->buf;
286
287 qual->bLength = sizeof(*qual);
288 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
289 /* POLICY: same bcdUSB and device type info at both speeds */
290 qual->bcdUSB = cdev->desc.bcdUSB;
291 qual->bDeviceClass = cdev->desc.bDeviceClass;
292 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
293 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
294 /* ASSUME same EP0 fifo size at both speeds */
295 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
296 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
297 qual->bRESERVED = 0;
298}
299
300/*-------------------------------------------------------------------------*/
301
302static void reset_config(struct usb_composite_dev *cdev)
303{
304 struct usb_function *f;
305
306 DBG(cdev, "reset config\n");
307
308 list_for_each_entry(f, &cdev->config->functions, list) {
309 if (f->disable)
310 f->disable(f);
311 }
312 cdev->config = NULL;
313}
314
315static int set_config(struct usb_composite_dev *cdev,
316 const struct usb_ctrlrequest *ctrl, unsigned number)
317{
318 struct usb_gadget *gadget = cdev->gadget;
319 struct usb_configuration *c = NULL;
320 int result = -EINVAL;
321 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
322 int tmp;
323
324 if (cdev->config)
325 reset_config(cdev);
326
327 if (number) {
328 list_for_each_entry(c, &cdev->configs, list) {
329 if (c->bConfigurationValue == number) {
330 result = 0;
331 break;
332 }
333 }
334 if (result < 0)
335 goto done;
336 } else
337 result = 0;
338
339 INFO(cdev, "%s speed config #%d: %s\n",
340 ({ char *speed;
341 switch (gadget->speed) {
342 case USB_SPEED_LOW: speed = "low"; break;
343 case USB_SPEED_FULL: speed = "full"; break;
344 case USB_SPEED_HIGH: speed = "high"; break;
345 default: speed = "?"; break;
346 } ; speed; }), number, c ? c->label : "unconfigured");
347
348 if (!c)
349 goto done;
350
351 cdev->config = c;
352
353 /* Initialize all interfaces by setting them to altsetting zero. */
354 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
355 struct usb_function *f = c->interface[tmp];
356
357 if (!f)
358 break;
359
360 result = f->set_alt(f, tmp, 0);
361 if (result < 0) {
362 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
363 tmp, f->name, f, result);
364
365 reset_config(cdev);
366 goto done;
367 }
368 }
369
370 /* when we return, be sure our power usage is valid */
371 power = 2 * c->bMaxPower;
372done:
373 usb_gadget_vbus_draw(gadget, power);
374 return result;
375}
376
377/**
378 * usb_add_config() - add a configuration to a device.
379 * @cdev: wraps the USB gadget
380 * @config: the configuration, with bConfigurationValue assigned
381 * Context: single threaded during gadget setup
382 *
383 * One of the main tasks of a composite driver's bind() routine is to
384 * add each of the configurations it supports, using this routine.
385 *
386 * This function returns the value of the configuration's bind(), which
387 * is zero for success else a negative errno value. Binding configurations
388 * assigns global resources including string IDs, and per-configuration
389 * resources such as interface IDs and endpoints.
390 */
391int __init usb_add_config(struct usb_composite_dev *cdev,
392 struct usb_configuration *config)
393{
394 int status = -EINVAL;
395 struct usb_configuration *c;
396
397 DBG(cdev, "adding config #%u '%s'/%p\n",
398 config->bConfigurationValue,
399 config->label, config);
400
401 if (!config->bConfigurationValue || !config->bind)
402 goto done;
403
404 /* Prevent duplicate configuration identifiers */
405 list_for_each_entry(c, &cdev->configs, list) {
406 if (c->bConfigurationValue == config->bConfigurationValue) {
407 status = -EBUSY;
408 goto done;
409 }
410 }
411
412 config->cdev = cdev;
413 list_add_tail(&config->list, &cdev->configs);
414
415 INIT_LIST_HEAD(&config->functions);
416 config->next_interface_id = 0;
417
418 status = config->bind(config);
419 if (status < 0) {
420 list_del(&config->list);
421 config->cdev = NULL;
422 } else {
423 unsigned i;
424
425 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
426 config->bConfigurationValue, config,
427 config->highspeed ? " high" : "",
428 config->fullspeed
429 ? (gadget_is_dualspeed(cdev->gadget)
430 ? " full"
431 : " full/low")
432 : "");
433
434 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
435 struct usb_function *f = config->interface[i];
436
437 if (!f)
438 continue;
439 DBG(cdev, " interface %d = %s/%p\n",
440 i, f->name, f);
441 }
442 }
443
444 /* set_alt(), or next config->bind(), sets up
445 * ep->driver_data as needed.
446 */
447 usb_ep_autoconfig_reset(cdev->gadget);
448
449done:
450 if (status)
451 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
452 config->bConfigurationValue, status);
453 return status;
454}
455
456/*-------------------------------------------------------------------------*/
457
458/* We support strings in multiple languages ... string descriptor zero
459 * says which languages are supported. The typical case will be that
460 * only one language (probably English) is used, with I18N handled on
461 * the host side.
462 */
463
464static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
465{
466 const struct usb_gadget_strings *s;
467 u16 language;
468 __le16 *tmp;
469
470 while (*sp) {
471 s = *sp;
472 language = cpu_to_le16(s->language);
473 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
474 if (*tmp == language)
475 goto repeat;
476 }
477 *tmp++ = language;
478repeat:
479 sp++;
480 }
481}
482
483static int lookup_string(
484 struct usb_gadget_strings **sp,
485 void *buf,
486 u16 language,
487 int id
488)
489{
490 struct usb_gadget_strings *s;
491 int value;
492
493 while (*sp) {
494 s = *sp++;
495 if (s->language != language)
496 continue;
497 value = usb_gadget_get_string(s, id, buf);
498 if (value > 0)
499 return value;
500 }
501 return -EINVAL;
502}
503
504static int get_string(struct usb_composite_dev *cdev,
505 void *buf, u16 language, int id)
506{
507 struct usb_configuration *c;
508 struct usb_function *f;
509 int len;
510
511 /* Yes, not only is USB's I18N support probably more than most
512 * folk will ever care about ... also, it's all supported here.
513 * (Except for UTF8 support for Unicode's "Astral Planes".)
514 */
515
516 /* 0 == report all available language codes */
517 if (id == 0) {
518 struct usb_string_descriptor *s = buf;
519 struct usb_gadget_strings **sp;
520
521 memset(s, 0, 256);
522 s->bDescriptorType = USB_DT_STRING;
523
524 sp = composite->strings;
525 if (sp)
526 collect_langs(sp, s->wData);
527
528 list_for_each_entry(c, &cdev->configs, list) {
529 sp = c->strings;
530 if (sp)
531 collect_langs(sp, s->wData);
532
533 list_for_each_entry(f, &c->functions, list) {
534 sp = f->strings;
535 if (sp)
536 collect_langs(sp, s->wData);
537 }
538 }
539
540 for (len = 0; s->wData[len] && len <= 126; len++)
541 continue;
542 if (!len)
543 return -EINVAL;
544
545 s->bLength = 2 * (len + 1);
546 return s->bLength;
547 }
548
549 /* Otherwise, look up and return a specified string. String IDs
550 * are device-scoped, so we look up each string table we're told
551 * about. These lookups are infrequent; simpler-is-better here.
552 */
553 if (composite->strings) {
554 len = lookup_string(composite->strings, buf, language, id);
555 if (len > 0)
556 return len;
557 }
558 list_for_each_entry(c, &cdev->configs, list) {
559 if (c->strings) {
560 len = lookup_string(c->strings, buf, language, id);
561 if (len > 0)
562 return len;
563 }
564 list_for_each_entry(f, &c->functions, list) {
565 if (!f->strings)
566 continue;
567 len = lookup_string(f->strings, buf, language, id);
568 if (len > 0)
569 return len;
570 }
571 }
572 return -EINVAL;
573}
574
575/**
576 * usb_string_id() - allocate an unused string ID
577 * @cdev: the device whose string descriptor IDs are being allocated
578 * Context: single threaded during gadget setup
579 *
580 * @usb_string_id() is called from bind() callbacks to allocate
581 * string IDs. Drivers for functions, configurations, or gadgets will
582 * then store that ID in the appropriate descriptors and string table.
583 *
584 * All string identifier should be allocated using this routine, to
585 * ensure that for example different functions don't wrongly assign
586 * different meanings to the same identifier.
587 */
588int __init usb_string_id(struct usb_composite_dev *cdev)
589{
590 if (cdev->next_string_id < 254) {
591 /* string id 0 is reserved */
592 cdev->next_string_id++;
593 return cdev->next_string_id;
594 }
595 return -ENODEV;
596}
597
598/*-------------------------------------------------------------------------*/
599
600static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
601{
602 if (req->status || req->actual != req->length)
603 DBG((struct usb_composite_dev *) ep->driver_data,
604 "setup complete --> %d, %d/%d\n",
605 req->status, req->actual, req->length);
606}
607
608/*
609 * The setup() callback implements all the ep0 functionality that's
610 * not handled lower down, in hardware or the hardware driver(like
611 * device and endpoint feature flags, and their status). It's all
612 * housekeeping for the gadget function we're implementing. Most of
613 * the work is in config and function specific setup.
614 */
615static int
616composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
617{
618 struct usb_composite_dev *cdev = get_gadget_data(gadget);
619 struct usb_request *req = cdev->req;
620 int value = -EOPNOTSUPP;
621 u16 w_index = le16_to_cpu(ctrl->wIndex);
622 u16 w_value = le16_to_cpu(ctrl->wValue);
623 u16 w_length = le16_to_cpu(ctrl->wLength);
624 struct usb_function *f = NULL;
625
626 /* partial re-init of the response message; the function or the
627 * gadget might need to intercept e.g. a control-OUT completion
628 * when we delegate to it.
629 */
630 req->zero = 0;
631 req->complete = composite_setup_complete;
632 req->length = USB_BUFSIZ;
633 gadget->ep0->driver_data = cdev;
634
635 switch (ctrl->bRequest) {
636
637 /* we handle all standard USB descriptors */
638 case USB_REQ_GET_DESCRIPTOR:
639 if (ctrl->bRequestType != USB_DIR_IN)
640 goto unknown;
641 switch (w_value >> 8) {
642
643 case USB_DT_DEVICE:
644 cdev->desc.bNumConfigurations =
645 count_configs(cdev, USB_DT_DEVICE);
646 value = min(w_length, (u16) sizeof cdev->desc);
647 memcpy(req->buf, &cdev->desc, value);
648 break;
649 case USB_DT_DEVICE_QUALIFIER:
650 if (!gadget_is_dualspeed(gadget))
651 break;
652 device_qual(cdev);
653 value = min_t(int, w_length,
654 sizeof(struct usb_qualifier_descriptor));
655 break;
656 case USB_DT_OTHER_SPEED_CONFIG:
657 if (!gadget_is_dualspeed(gadget))
658 break;
659 /* FALLTHROUGH */
660 case USB_DT_CONFIG:
661 value = config_desc(cdev, w_value);
662 if (value >= 0)
663 value = min(w_length, (u16) value);
664 break;
665 case USB_DT_STRING:
666 value = get_string(cdev, req->buf,
667 w_index, w_value & 0xff);
668 if (value >= 0)
669 value = min(w_length, (u16) value);
670 break;
671 }
672 break;
673
674 /* any number of configs can work */
675 case USB_REQ_SET_CONFIGURATION:
676 if (ctrl->bRequestType != 0)
677 goto unknown;
678 if (gadget_is_otg(gadget)) {
679 if (gadget->a_hnp_support)
680 DBG(cdev, "HNP available\n");
681 else if (gadget->a_alt_hnp_support)
682 DBG(cdev, "HNP on another port\n");
683 else
684 VDBG(cdev, "HNP inactive\n");
685 }
686 spin_lock(&cdev->lock);
687 value = set_config(cdev, ctrl, w_value);
688 spin_unlock(&cdev->lock);
689 break;
690 case USB_REQ_GET_CONFIGURATION:
691 if (ctrl->bRequestType != USB_DIR_IN)
692 goto unknown;
693 if (cdev->config)
694 *(u8 *)req->buf = cdev->config->bConfigurationValue;
695 else
696 *(u8 *)req->buf = 0;
697 value = min(w_length, (u16) 1);
698 break;
699
700 /* function drivers must handle get/set altsetting; if there's
701 * no get() method, we know only altsetting zero works.
702 */
703 case USB_REQ_SET_INTERFACE:
704 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
705 goto unknown;
706 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
707 break;
708 f = cdev->config->interface[w_index];
709 if (!f)
710 break;
711 if (w_value && !f->get_alt)
712 break;
713 value = f->set_alt(f, w_index, w_value);
714 break;
715 case USB_REQ_GET_INTERFACE:
716 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
717 goto unknown;
718 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
719 break;
720 f = cdev->config->interface[w_index];
721 if (!f)
722 break;
723 /* lots of interfaces only need altsetting zero... */
724 value = f->get_alt ? f->get_alt(f, w_index) : 0;
725 if (value < 0)
726 break;
727 *((u8 *)req->buf) = value;
728 value = min(w_length, (u16) 1);
729 break;
730 default:
731unknown:
732 VDBG(cdev,
733 "non-core control req%02x.%02x v%04x i%04x l%d\n",
734 ctrl->bRequestType, ctrl->bRequest,
735 w_value, w_index, w_length);
736
737 /* functions always handle their interfaces ... punt other
738 * recipients (endpoint, other, WUSB, ...) to the current
739 * configuration code.
740 *
741 * REVISIT it could make sense to let the composite device
742 * take such requests too, if that's ever needed: to work
743 * in config 0, etc.
744 */
745 if ((ctrl->bRequestType & USB_RECIP_MASK)
746 == USB_RECIP_INTERFACE) {
747 f = cdev->config->interface[w_index];
748 if (f && f->setup)
749 value = f->setup(f, ctrl);
750 else
751 f = NULL;
752 }
753 if (value < 0 && !f) {
754 struct usb_configuration *c;
755
756 c = cdev->config;
757 if (c && c->setup)
758 value = c->setup(c, ctrl);
759 }
760
761 goto done;
762 }
763
764 /* respond with data transfer before status phase? */
765 if (value >= 0) {
766 req->length = value;
767 req->zero = value < w_length;
768 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
769 if (value < 0) {
770 DBG(cdev, "ep_queue --> %d\n", value);
771 req->status = 0;
772 composite_setup_complete(gadget->ep0, req);
773 }
774 }
775
776done:
777 /* device either stalls (value < 0) or reports success */
778 return value;
779}
780
781static void composite_disconnect(struct usb_gadget *gadget)
782{
783 struct usb_composite_dev *cdev = get_gadget_data(gadget);
784 unsigned long flags;
785
786 /* REVISIT: should we have config and device level
787 * disconnect callbacks?
788 */
789 spin_lock_irqsave(&cdev->lock, flags);
790 if (cdev->config)
791 reset_config(cdev);
792 spin_unlock_irqrestore(&cdev->lock, flags);
793}
794
795/*-------------------------------------------------------------------------*/
796
797static void /* __init_or_exit */
798composite_unbind(struct usb_gadget *gadget)
799{
800 struct usb_composite_dev *cdev = get_gadget_data(gadget);
801
802 /* composite_disconnect() must already have been called
803 * by the underlying peripheral controller driver!
804 * so there's no i/o concurrency that could affect the
805 * state protected by cdev->lock.
806 */
807 WARN_ON(cdev->config);
808
809 while (!list_empty(&cdev->configs)) {
810 struct usb_configuration *c;
811
812 c = list_first_entry(&cdev->configs,
813 struct usb_configuration, list);
814 while (!list_empty(&c->functions)) {
815 struct usb_function *f;
816
817 f = list_first_entry(&c->functions,
818 struct usb_function, list);
819 list_del(&f->list);
820 if (f->unbind) {
821 DBG(cdev, "unbind function '%s'/%p\n",
822 f->name, f);
823 f->unbind(c, f);
824 /* may free memory for "f" */
825 }
826 }
827 list_del(&c->list);
828 if (c->unbind) {
829 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
830 c->unbind(c);
831 /* may free memory for "c" */
832 }
833 }
834 if (composite->unbind)
835 composite->unbind(cdev);
836
837 if (cdev->req) {
838 kfree(cdev->req->buf);
839 usb_ep_free_request(gadget->ep0, cdev->req);
840 }
841 kfree(cdev);
842 set_gadget_data(gadget, NULL);
843 composite = NULL;
844}
845
846static void __init
847string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
848{
849 struct usb_string *str = tab->strings;
850
851 for (str = tab->strings; str->s; str++) {
852 if (str->id == id) {
853 str->s = s;
854 return;
855 }
856 }
857}
858
859static void __init
860string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
861{
862 while (*tab) {
863 string_override_one(*tab, id, s);
864 tab++;
865 }
866}
867
868static int __init composite_bind(struct usb_gadget *gadget)
869{
870 struct usb_composite_dev *cdev;
871 int status = -ENOMEM;
872
873 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
874 if (!cdev)
875 return status;
876
877 spin_lock_init(&cdev->lock);
878 cdev->gadget = gadget;
879 set_gadget_data(gadget, cdev);
880 INIT_LIST_HEAD(&cdev->configs);
881
882 /* preallocate control response and buffer */
883 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
884 if (!cdev->req)
885 goto fail;
886 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
887 if (!cdev->req->buf)
888 goto fail;
889 cdev->req->complete = composite_setup_complete;
890 gadget->ep0->driver_data = cdev;
891
892 cdev->bufsiz = USB_BUFSIZ;
893 cdev->driver = composite;
894
895 usb_gadget_set_selfpowered(gadget);
896
897 /* interface and string IDs start at zero via kzalloc.
898 * we force endpoints to start unassigned; few controller
899 * drivers will zero ep->driver_data.
900 */
901 usb_ep_autoconfig_reset(cdev->gadget);
902
903 /* composite gadget needs to assign strings for whole device (like
904 * serial number), register function drivers, potentially update
905 * power state and consumption, etc
906 */
907 status = composite->bind(cdev);
908 if (status < 0)
909 goto fail;
910
911 cdev->desc = *composite->dev;
912 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
913
914 /* standardized runtime overrides for device ID data */
915 if (idVendor)
916 cdev->desc.idVendor = cpu_to_le16(idVendor);
917 if (idProduct)
918 cdev->desc.idProduct = cpu_to_le16(idProduct);
919 if (bcdDevice)
920 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
921
922 /* strings can't be assigned before bind() allocates the
923 * releavnt identifiers
924 */
925 if (cdev->desc.iManufacturer && iManufacturer)
926 string_override(composite->strings,
927 cdev->desc.iManufacturer, iManufacturer);
928 if (cdev->desc.iProduct && iProduct)
929 string_override(composite->strings,
930 cdev->desc.iProduct, iProduct);
931 if (cdev->desc.iSerialNumber && iSerialNumber)
932 string_override(composite->strings,
933 cdev->desc.iSerialNumber, iSerialNumber);
934
935 INFO(cdev, "%s ready\n", composite->name);
936 return 0;
937
938fail:
939 composite_unbind(gadget);
940 return status;
941}
942
943/*-------------------------------------------------------------------------*/
944
945static void
946composite_suspend(struct usb_gadget *gadget)
947{
948 struct usb_composite_dev *cdev = get_gadget_data(gadget);
949 struct usb_function *f;
950
951 /* REVISIT: should we have config and device level
952 * suspend/resume callbacks?
953 */
954 DBG(cdev, "suspend\n");
955 if (cdev->config) {
956 list_for_each_entry(f, &cdev->config->functions, list) {
957 if (f->suspend)
958 f->suspend(f);
959 }
960 }
961}
962
963static void
964composite_resume(struct usb_gadget *gadget)
965{
966 struct usb_composite_dev *cdev = get_gadget_data(gadget);
967 struct usb_function *f;
968
969 /* REVISIT: should we have config and device level
970 * suspend/resume callbacks?
971 */
972 DBG(cdev, "resume\n");
973 if (cdev->config) {
974 list_for_each_entry(f, &cdev->config->functions, list) {
975 if (f->resume)
976 f->resume(f);
977 }
978 }
979}
980
981/*-------------------------------------------------------------------------*/
982
983static struct usb_gadget_driver composite_driver = {
984 .speed = USB_SPEED_HIGH,
985
986 .bind = composite_bind,
987 .unbind = __exit_p(composite_unbind),
988
989 .setup = composite_setup,
990 .disconnect = composite_disconnect,
991
992 .suspend = composite_suspend,
993 .resume = composite_resume,
994
995 .driver = {
996 .owner = THIS_MODULE,
997 },
998};
999
1000/**
1001 * usb_composite_register() - register a composite driver
1002 * @driver: the driver to register
1003 * Context: single threaded during gadget setup
1004 *
1005 * This function is used to register drivers using the composite driver
1006 * framework. The return value is zero, or a negative errno value.
1007 * Those values normally come from the driver's @bind method, which does
1008 * all the work of setting up the driver to match the hardware.
1009 *
1010 * On successful return, the gadget is ready to respond to requests from
1011 * the host, unless one of its components invokes usb_gadget_disconnect()
1012 * while it was binding. That would usually be done in order to wait for
1013 * some userspace participation.
1014 */
1015int __init usb_composite_register(struct usb_composite_driver *driver)
1016{
1017 if (!driver || !driver->dev || !driver->bind || composite)
1018 return -EINVAL;
1019
1020 if (!driver->name)
1021 driver->name = "composite";
1022 composite_driver.function = (char *) driver->name;
1023 composite_driver.driver.name = driver->name;
1024 composite = driver;
1025
1026 return usb_gadget_register_driver(&composite_driver);
1027}
1028
1029/**
1030 * usb_composite_unregister() - unregister a composite driver
1031 * @driver: the driver to unregister
1032 *
1033 * This function is used to unregister drivers using the composite
1034 * driver framework.
1035 */
1036void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1037{
1038 if (composite != driver)
1039 return;
1040 usb_gadget_unregister_driver(&composite_driver);
1041}
diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c
index a4e54b2743f0..1ca1c326392a 100644
--- a/drivers/usb/gadget/config.c
+++ b/drivers/usb/gadget/config.c
@@ -96,7 +96,7 @@ int usb_gadget_config_buf(
96 /* config descriptor first */ 96 /* config descriptor first */
97 if (length < USB_DT_CONFIG_SIZE || !desc) 97 if (length < USB_DT_CONFIG_SIZE || !desc)
98 return -EINVAL; 98 return -EINVAL;
99 *cp = *config; 99 *cp = *config;
100 100
101 /* then interface/endpoint/class/vendor/... */ 101 /* then interface/endpoint/class/vendor/... */
102 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, 102 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
@@ -115,3 +115,77 @@ int usb_gadget_config_buf(
115 return len; 115 return len;
116} 116}
117 117
118/**
119 * usb_copy_descriptors - copy a vector of USB descriptors
120 * @src: null-terminated vector to copy
121 * Context: initialization code, which may sleep
122 *
123 * This makes a copy of a vector of USB descriptors. Its primary use
124 * is to support usb_function objects which can have multiple copies,
125 * each needing different descriptors. Functions may have static
126 * tables of descriptors, which are used as templates and customized
127 * with identifiers (for interfaces, strings, endpoints, and more)
128 * as needed by a given function instance.
129 */
130struct usb_descriptor_header **__init
131usb_copy_descriptors(struct usb_descriptor_header **src)
132{
133 struct usb_descriptor_header **tmp;
134 unsigned bytes;
135 unsigned n_desc;
136 void *mem;
137 struct usb_descriptor_header **ret;
138
139 /* count descriptors and their sizes; then add vector size */
140 for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
141 bytes += (*tmp)->bLength;
142 bytes += (n_desc + 1) * sizeof(*tmp);
143
144 mem = kmalloc(bytes, GFP_KERNEL);
145 if (!mem)
146 return NULL;
147
148 /* fill in pointers starting at "tmp",
149 * to descriptors copied starting at "mem";
150 * and return "ret"
151 */
152 tmp = mem;
153 ret = mem;
154 mem += (n_desc + 1) * sizeof(*tmp);
155 while (*src) {
156 memcpy(mem, *src, (*src)->bLength);
157 *tmp = mem;
158 tmp++;
159 mem += (*src)->bLength;
160 src++;
161 }
162 *tmp = NULL;
163
164 return ret;
165}
166
167/**
168 * usb_find_endpoint - find a copy of an endpoint descriptor
169 * @src: original vector of descriptors
170 * @copy: copy of @src
171 * @ep: endpoint descriptor found in @src
172 *
173 * This returns the copy of the @match descriptor made for @copy. Its
174 * intended use is to help remembering the endpoint descriptor to use
175 * when enabling a given endpoint.
176 */
177struct usb_endpoint_descriptor *__init
178usb_find_endpoint(
179 struct usb_descriptor_header **src,
180 struct usb_descriptor_header **copy,
181 struct usb_endpoint_descriptor *match
182)
183{
184 while (*src) {
185 if (*src == (void *) match)
186 return (void *)*copy;
187 src++;
188 copy++;
189 }
190 return NULL;
191}
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 42036192a03c..21d1406af9ee 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -862,7 +862,7 @@ static int dummy_udc_probe (struct platform_device *pdev)
862 /* maybe claim OTG support, though we won't complete HNP */ 862 /* maybe claim OTG support, though we won't complete HNP */
863 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0); 863 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
864 864
865 strcpy (dum->gadget.dev.bus_id, "gadget"); 865 dev_set_name(&dum->gadget.dev, "gadget");
866 dum->gadget.dev.parent = &pdev->dev; 866 dum->gadget.dev.parent = &pdev->dev;
867 dum->gadget.dev.release = dummy_gadget_release; 867 dum->gadget.dev.release = dummy_gadget_release;
868 rc = device_register (&dum->gadget.dev); 868 rc = device_register (&dum->gadget.dev);
@@ -1865,7 +1865,7 @@ static int dummy_hcd_probe(struct platform_device *pdev)
1865 1865
1866 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); 1866 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1867 1867
1868 hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id); 1868 hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
1869 if (!hcd) 1869 if (!hcd)
1870 return -ENOMEM; 1870 return -ENOMEM;
1871 the_controller = hcd_to_dummy (hcd); 1871 the_controller = hcd_to_dummy (hcd);
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c
index 8bdad221fa91..9462e30192d8 100644
--- a/drivers/usb/gadget/epautoconf.c
+++ b/drivers/usb/gadget/epautoconf.c
@@ -159,6 +159,7 @@ ep_matches (
159 /* MATCH!! */ 159 /* MATCH!! */
160 160
161 /* report address */ 161 /* report address */
162 desc->bEndpointAddress &= USB_DIR_IN;
162 if (isdigit (ep->name [2])) { 163 if (isdigit (ep->name [2])) {
163 u8 num = simple_strtol (&ep->name [2], NULL, 10); 164 u8 num = simple_strtol (&ep->name [2], NULL, 10);
164 desc->bEndpointAddress |= num; 165 desc->bEndpointAddress |= num;
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 4ce3950b997f..d7aaaa29b1e1 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -1,8 +1,9 @@
1/* 1/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options 2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3 * 3 *
4 * Copyright (C) 2003-2005 David Brownell 4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger 5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
@@ -23,18 +24,9 @@
23 24
24#include <linux/kernel.h> 25#include <linux/kernel.h>
25#include <linux/utsname.h> 26#include <linux/utsname.h>
26#include <linux/device.h>
27#include <linux/ctype.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30 27
31#include <linux/usb/ch9.h> 28#include "u_ether.h"
32#include <linux/usb/cdc.h>
33#include <linux/usb/gadget.h>
34 29
35#include "gadget_chips.h"
36
37/*-------------------------------------------------------------------------*/
38 30
39/* 31/*
40 * Ethernet gadget driver -- with CDC and non-CDC options 32 * Ethernet gadget driver -- with CDC and non-CDC options
@@ -46,7 +38,11 @@
46 * this USB-IF standard as its open-systems interoperability solution; 38 * this USB-IF standard as its open-systems interoperability solution;
47 * most host side USB stacks (except from Microsoft) support it. 39 * most host side USB stacks (except from Microsoft) support it.
48 * 40 *
49 * There's some hardware that can't talk CDC. We make that hardware 41 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
42 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
43 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
44 *
45 * There's some hardware that can't talk CDC ECM. We make that hardware
50 * implement a "minimalist" vendor-agnostic CDC core: same framing, but 46 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
51 * link-level setup only requires activating the configuration. Only the 47 * link-level setup only requires activating the configuration. Only the
52 * endpoint descriptors, and product/vendor IDs, are relevant; no control 48 * endpoint descriptors, and product/vendor IDs, are relevant; no control
@@ -64,70 +60,40 @@
64 * A third option is also in use. Rather than CDC Ethernet, or something 60 * A third option is also in use. Rather than CDC Ethernet, or something
65 * simpler, Microsoft pushes their own approach: RNDIS. The published 61 * simpler, Microsoft pushes their own approach: RNDIS. The published
66 * RNDIS specs are ambiguous and appear to be incomplete, and are also 62 * RNDIS specs are ambiguous and appear to be incomplete, and are also
67 * needlessly complex. 63 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
68 */ 64 */
69 65
70#define DRIVER_DESC "Ethernet Gadget" 66#define DRIVER_DESC "Ethernet Gadget"
71#define DRIVER_VERSION "May Day 2005" 67#define DRIVER_VERSION "Memorial Day 2008"
72
73static const char shortname [] = "ether";
74static const char driver_desc [] = DRIVER_DESC;
75
76#define RX_EXTRA 20 /* guard against rx overflows */
77
78#include "rndis.h"
79 68
80#ifndef CONFIG_USB_ETH_RNDIS 69#ifdef CONFIG_USB_ETH_RNDIS
81#define rndis_uninit(x) do{}while(0) 70#define PREFIX "RNDIS/"
82#define rndis_deregister(c) do{}while(0) 71#else
83#define rndis_exit() do{}while(0) 72#define PREFIX ""
84#endif 73#endif
85 74
86/* CDC and RNDIS support the same host-chosen outgoing packet filters. */ 75/*
87#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ 76 * This driver aims for interoperability by using CDC ECM unless
88 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ 77 *
89 |USB_CDC_PACKET_TYPE_PROMISCUOUS \ 78 * can_support_ecm()
90 |USB_CDC_PACKET_TYPE_DIRECTED) 79 *
91 80 * returns false, in which case it supports the CDC Subset. By default,
92 81 * that returns true; most hardware has no problems with CDC ECM, that's
93/*-------------------------------------------------------------------------*/ 82 * a good default. Previous versions of this driver had no default; this
94 83 * version changes that, removing overhead for new controller support.
95struct eth_dev {
96 spinlock_t lock;
97 struct usb_gadget *gadget;
98 struct usb_request *req; /* for control responses */
99 struct usb_request *stat_req; /* for cdc & rndis status */
100
101 u8 config;
102 struct usb_ep *in_ep, *out_ep, *status_ep;
103 const struct usb_endpoint_descriptor
104 *in, *out, *status;
105
106 spinlock_t req_lock;
107 struct list_head tx_reqs, rx_reqs;
108
109 struct net_device *net;
110 struct net_device_stats stats;
111 atomic_t tx_qlen;
112
113 struct work_struct work;
114 unsigned zlp:1;
115 unsigned cdc:1;
116 unsigned rndis:1;
117 unsigned suspended:1;
118 u16 cdc_filter;
119 unsigned long todo;
120#define WORK_RX_MEMORY 0
121 int rndis_config;
122 u8 host_mac [ETH_ALEN];
123};
124
125/* This version autoconfigures as much as possible at run-time.
126 * 84 *
127 * It also ASSUMES a self-powered device, without remote wakeup, 85 * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
128 * although remote wakeup support would make sense.
129 */ 86 */
130 87
88static inline bool has_rndis(void)
89{
90#ifdef CONFIG_USB_ETH_RNDIS
91 return true;
92#else
93 return false;
94#endif
95}
96
131/*-------------------------------------------------------------------------*/ 97/*-------------------------------------------------------------------------*/
132 98
133/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 99/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
@@ -137,8 +103,8 @@ struct eth_dev {
137/* Thanks to NetChip Technologies for donating this product ID. 103/* Thanks to NetChip Technologies for donating this product ID.
138 * It's for devices with only CDC Ethernet configurations. 104 * It's for devices with only CDC Ethernet configurations.
139 */ 105 */
140#define CDC_VENDOR_NUM 0x0525 /* NetChip */ 106#define CDC_VENDOR_NUM 0x0525 /* NetChip */
141#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ 107#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
142 108
143/* For hardware that can't talk CDC, we use the same vendor ID that 109/* For hardware that can't talk CDC, we use the same vendor ID that
144 * ARM Linux has used for ethernet-over-usb, both with sa1100 and 110 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
@@ -162,274 +128,9 @@ struct eth_dev {
162#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ 128#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
163#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ 129#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
164 130
165
166/* Some systems will want different product identifers published in the
167 * device descriptor, either numbers or strings or both. These string
168 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
169 */
170
171static ushort idVendor;
172module_param(idVendor, ushort, S_IRUGO);
173MODULE_PARM_DESC(idVendor, "USB Vendor ID");
174
175static ushort idProduct;
176module_param(idProduct, ushort, S_IRUGO);
177MODULE_PARM_DESC(idProduct, "USB Product ID");
178
179static ushort bcdDevice;
180module_param(bcdDevice, ushort, S_IRUGO);
181MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
182
183static char *iManufacturer;
184module_param(iManufacturer, charp, S_IRUGO);
185MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
186
187static char *iProduct;
188module_param(iProduct, charp, S_IRUGO);
189MODULE_PARM_DESC(iProduct, "USB Product string");
190
191static char *iSerialNumber;
192module_param(iSerialNumber, charp, S_IRUGO);
193MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
194
195/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
196static char *dev_addr;
197module_param(dev_addr, charp, S_IRUGO);
198MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
199
200/* this address is invisible to ifconfig */
201static char *host_addr;
202module_param(host_addr, charp, S_IRUGO);
203MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
204
205
206/*-------------------------------------------------------------------------*/
207
208/* Include CDC support if we could run on CDC-capable hardware. */
209
210#ifdef CONFIG_USB_GADGET_NET2280
211#define DEV_CONFIG_CDC
212#endif
213
214#ifdef CONFIG_USB_GADGET_DUMMY_HCD
215#define DEV_CONFIG_CDC
216#endif
217
218#ifdef CONFIG_USB_GADGET_GOKU
219#define DEV_CONFIG_CDC
220#endif
221
222#ifdef CONFIG_USB_GADGET_LH7A40X
223#define DEV_CONFIG_CDC
224#endif
225
226#ifdef CONFIG_USB_GADGET_MQ11XX
227#define DEV_CONFIG_CDC
228#endif
229
230#ifdef CONFIG_USB_GADGET_OMAP
231#define DEV_CONFIG_CDC
232#endif
233
234#ifdef CONFIG_USB_GADGET_N9604
235#define DEV_CONFIG_CDC
236#endif
237
238#ifdef CONFIG_USB_GADGET_S3C2410
239#define DEV_CONFIG_CDC
240#endif
241
242#ifdef CONFIG_USB_GADGET_AT91
243#define DEV_CONFIG_CDC
244#endif
245
246#ifdef CONFIG_USB_GADGET_MUSBHSFC
247#define DEV_CONFIG_CDC
248#endif
249
250#ifdef CONFIG_USB_GADGET_MUSB_HDRC
251#define DEV_CONFIG_CDC
252#endif
253
254#ifdef CONFIG_USB_GADGET_ATMEL_USBA
255#define DEV_CONFIG_CDC
256#endif
257
258#ifdef CONFIG_USB_GADGET_FSL_USB2
259#define DEV_CONFIG_CDC
260#endif
261
262/* For CDC-incapable hardware, choose the simple cdc subset.
263 * Anything that talks bulk (without notable bugs) can do this.
264 */
265#ifdef CONFIG_USB_GADGET_PXA25X
266#define DEV_CONFIG_SUBSET
267#endif
268
269#ifdef CONFIG_USB_GADGET_PXA27X
270#define DEV_CONFIG_SUBSET
271#endif
272
273#ifdef CONFIG_USB_GADGET_SUPERH
274#define DEV_CONFIG_SUBSET
275#endif
276
277#ifdef CONFIG_USB_GADGET_SA1100
278/* use non-CDC for backwards compatibility */
279#define DEV_CONFIG_SUBSET
280#endif
281
282#ifdef CONFIG_USB_GADGET_M66592
283#define DEV_CONFIG_CDC
284#endif
285
286#ifdef CONFIG_USB_GADGET_AMD5536UDC
287#define DEV_CONFIG_CDC
288#endif
289
290
291/*-------------------------------------------------------------------------*/
292
293/* "main" config is either CDC, or its simple subset */
294static inline int is_cdc(struct eth_dev *dev)
295{
296#if !defined(DEV_CONFIG_SUBSET)
297 return 1; /* only cdc possible */
298#elif !defined (DEV_CONFIG_CDC)
299 return 0; /* only subset possible */
300#else
301 return dev->cdc; /* depends on what hardware we found */
302#endif
303}
304
305/* "secondary" RNDIS config may sometimes be activated */
306static inline int rndis_active(struct eth_dev *dev)
307{
308#ifdef CONFIG_USB_ETH_RNDIS
309 return dev->rndis;
310#else
311 return 0;
312#endif
313}
314
315#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
316#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
317
318
319
320#define DEFAULT_QLEN 2 /* double buffering by default */
321
322/* peak bulk transfer bits-per-second */
323#define HS_BPS (13 * 512 * 8 * 1000 * 8)
324#define FS_BPS (19 * 64 * 1 * 1000 * 8)
325
326#ifdef CONFIG_USB_GADGET_DUALSPEED
327#define DEVSPEED USB_SPEED_HIGH
328
329static unsigned qmult = 5;
330module_param (qmult, uint, S_IRUGO|S_IWUSR);
331
332
333/* for dual-speed hardware, use deeper queues at highspeed */
334#define qlen(gadget) \
335 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
336
337static inline int BITRATE(struct usb_gadget *g)
338{
339 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
340}
341
342#else /* full speed (low speed doesn't do bulk) */
343
344#define qmult 1
345
346#define DEVSPEED USB_SPEED_FULL
347
348#define qlen(gadget) DEFAULT_QLEN
349
350static inline int BITRATE(struct usb_gadget *g)
351{
352 return FS_BPS;
353}
354#endif
355
356
357/*-------------------------------------------------------------------------*/
358
359#define xprintk(d,level,fmt,args...) \
360 printk(level "%s: " fmt , (d)->net->name , ## args)
361
362#ifdef DEBUG
363#undef DEBUG
364#define DEBUG(dev,fmt,args...) \
365 xprintk(dev , KERN_DEBUG , fmt , ## args)
366#else
367#define DEBUG(dev,fmt,args...) \
368 do { } while (0)
369#endif /* DEBUG */
370
371#ifdef VERBOSE_DEBUG
372#define VDEBUG DEBUG
373#else
374#define VDEBUG(dev,fmt,args...) \
375 do { } while (0)
376#endif /* DEBUG */
377
378#define ERROR(dev,fmt,args...) \
379 xprintk(dev , KERN_ERR , fmt , ## args)
380#define WARN(dev,fmt,args...) \
381 xprintk(dev , KERN_WARNING , fmt , ## args)
382#define INFO(dev,fmt,args...) \
383 xprintk(dev , KERN_INFO , fmt , ## args)
384
385/*-------------------------------------------------------------------------*/ 131/*-------------------------------------------------------------------------*/
386 132
387/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly 133static struct usb_device_descriptor device_desc = {
388 * ep0 implementation: descriptors, config management, setup().
389 * also optional class-specific notification interrupt transfer.
390 */
391
392/*
393 * DESCRIPTORS ... most are static, but strings and (full) configuration
394 * descriptors are built on demand. For now we do either full CDC, or
395 * our simple subset, with RNDIS as an optional second configuration.
396 *
397 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
398 * the class descriptors match a modem (they're ignored; it's really just
399 * Ethernet functionality), they don't need the NOP altsetting, and the
400 * status transfer endpoint isn't optional.
401 */
402
403#define STRING_MANUFACTURER 1
404#define STRING_PRODUCT 2
405#define STRING_ETHADDR 3
406#define STRING_DATA 4
407#define STRING_CONTROL 5
408#define STRING_RNDIS_CONTROL 6
409#define STRING_CDC 7
410#define STRING_SUBSET 8
411#define STRING_RNDIS 9
412#define STRING_SERIALNUMBER 10
413
414/* holds our biggest descriptor (or RNDIS response) */
415#define USB_BUFSIZ 256
416
417/*
418 * This device advertises one configuration, eth_config, unless RNDIS
419 * is enabled (rndis_config) on hardware supporting at least two configs.
420 *
421 * NOTE: Controllers like superh_udc should probably be able to use
422 * an RNDIS-only configuration.
423 *
424 * FIXME define some higher-powered configurations to make it easier
425 * to recharge batteries ...
426 */
427
428#define DEV_CONFIG_VALUE 1 /* cdc or subset */
429#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
430
431static struct usb_device_descriptor
432device_desc = {
433 .bLength = sizeof device_desc, 134 .bLength = sizeof device_desc,
434 .bDescriptorType = USB_DT_DEVICE, 135 .bDescriptorType = USB_DT_DEVICE,
435 136
@@ -438,2220 +139,234 @@ device_desc = {
438 .bDeviceClass = USB_CLASS_COMM, 139 .bDeviceClass = USB_CLASS_COMM,
439 .bDeviceSubClass = 0, 140 .bDeviceSubClass = 0,
440 .bDeviceProtocol = 0, 141 .bDeviceProtocol = 0,
142 /* .bMaxPacketSize0 = f(hardware) */
441 143
144 /* Vendor and product id defaults change according to what configs
145 * we support. (As does bNumConfigurations.) These values can
146 * also be overridden by module parameters.
147 */
442 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), 148 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
443 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), 149 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
444 .iManufacturer = STRING_MANUFACTURER, 150 /* .bcdDevice = f(hardware) */
445 .iProduct = STRING_PRODUCT, 151 /* .iManufacturer = DYNAMIC */
152 /* .iProduct = DYNAMIC */
153 /* NO SERIAL NUMBER */
446 .bNumConfigurations = 1, 154 .bNumConfigurations = 1,
447}; 155};
448 156
449static struct usb_otg_descriptor 157static struct usb_otg_descriptor otg_descriptor = {
450otg_descriptor = {
451 .bLength = sizeof otg_descriptor, 158 .bLength = sizeof otg_descriptor,
452 .bDescriptorType = USB_DT_OTG, 159 .bDescriptorType = USB_DT_OTG,
453 160
454 .bmAttributes = USB_OTG_SRP, 161 /* REVISIT SRP-only hardware is possible, although
455}; 162 * it would not be called "OTG" ...
456 163 */
457static struct usb_config_descriptor 164 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
458eth_config = {
459 .bLength = sizeof eth_config,
460 .bDescriptorType = USB_DT_CONFIG,
461
462 /* compute wTotalLength on the fly */
463 .bNumInterfaces = 2,
464 .bConfigurationValue = DEV_CONFIG_VALUE,
465 .iConfiguration = STRING_CDC,
466 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
467 .bMaxPower = 50,
468};
469
470#ifdef CONFIG_USB_ETH_RNDIS
471static struct usb_config_descriptor
472rndis_config = {
473 .bLength = sizeof rndis_config,
474 .bDescriptorType = USB_DT_CONFIG,
475
476 /* compute wTotalLength on the fly */
477 .bNumInterfaces = 2,
478 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
479 .iConfiguration = STRING_RNDIS,
480 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
481 .bMaxPower = 50,
482};
483#endif
484
485/*
486 * Compared to the simple CDC subset, the full CDC Ethernet model adds
487 * three class descriptors, two interface descriptors, optional status
488 * endpoint. Both have a "data" interface and two bulk endpoints.
489 * There are also differences in how control requests are handled.
490 *
491 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
492 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
493 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
494 * work around those bugs) are unlikely to ever come from MSFT, you may
495 * wish to avoid using RNDIS.
496 *
497 * MCCI offers an alternative to RNDIS if you need to connect to Windows
498 * but have hardware that can't support CDC Ethernet. We add descriptors
499 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
500 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
501 * get those drivers from MCCI, or bundled with various products.
502 */
503
504#ifdef DEV_CONFIG_CDC
505static struct usb_interface_descriptor
506control_intf = {
507 .bLength = sizeof control_intf,
508 .bDescriptorType = USB_DT_INTERFACE,
509
510 .bInterfaceNumber = 0,
511 /* status endpoint is optional; this may be patched later */
512 .bNumEndpoints = 1,
513 .bInterfaceClass = USB_CLASS_COMM,
514 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
515 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
516 .iInterface = STRING_CONTROL,
517};
518#endif
519
520#ifdef CONFIG_USB_ETH_RNDIS
521static const struct usb_interface_descriptor
522rndis_control_intf = {
523 .bLength = sizeof rndis_control_intf,
524 .bDescriptorType = USB_DT_INTERFACE,
525
526 .bInterfaceNumber = 0,
527 .bNumEndpoints = 1,
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
530 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
531 .iInterface = STRING_RNDIS_CONTROL,
532};
533#endif
534
535static const struct usb_cdc_header_desc header_desc = {
536 .bLength = sizeof header_desc,
537 .bDescriptorType = USB_DT_CS_INTERFACE,
538 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
539
540 .bcdCDC = __constant_cpu_to_le16 (0x0110),
541};
542
543#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
544
545static const struct usb_cdc_union_desc union_desc = {
546 .bLength = sizeof union_desc,
547 .bDescriptorType = USB_DT_CS_INTERFACE,
548 .bDescriptorSubType = USB_CDC_UNION_TYPE,
549
550 .bMasterInterface0 = 0, /* index of control interface */
551 .bSlaveInterface0 = 1, /* index of DATA interface */
552};
553
554#endif /* CDC || RNDIS */
555
556#ifdef CONFIG_USB_ETH_RNDIS
557
558static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
559 .bLength = sizeof call_mgmt_descriptor,
560 .bDescriptorType = USB_DT_CS_INTERFACE,
561 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
562
563 .bmCapabilities = 0x00,
564 .bDataInterface = 0x01,
565};
566
567static const struct usb_cdc_acm_descriptor acm_descriptor = {
568 .bLength = sizeof acm_descriptor,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_ACM_TYPE,
571
572 .bmCapabilities = 0x00,
573};
574
575#endif
576
577#ifndef DEV_CONFIG_CDC
578
579/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
580 * ways: data endpoints live in the control interface, there's no data
581 * interface, and it's not used to talk to a cell phone radio.
582 */
583
584static const struct usb_cdc_mdlm_desc mdlm_desc = {
585 .bLength = sizeof mdlm_desc,
586 .bDescriptorType = USB_DT_CS_INTERFACE,
587 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
588
589 .bcdVersion = __constant_cpu_to_le16(0x0100),
590 .bGUID = {
591 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
592 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
593 },
594};
595
596/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
597 * can't really use its struct. All we do here is say that we're using
598 * the submode of "SAFE" which directly matches the CDC Subset.
599 */
600static const u8 mdlm_detail_desc[] = {
601 6,
602 USB_DT_CS_INTERFACE,
603 USB_CDC_MDLM_DETAIL_TYPE,
604
605 0, /* "SAFE" */
606 0, /* network control capabilities (none) */
607 0, /* network data capabilities ("raw" encapsulation) */
608};
609
610#endif
611
612static const struct usb_cdc_ether_desc ether_desc = {
613 .bLength = sizeof ether_desc,
614 .bDescriptorType = USB_DT_CS_INTERFACE,
615 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
616
617 /* this descriptor actually adds value, surprise! */
618 .iMACAddress = STRING_ETHADDR,
619 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
620 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
621 .wNumberMCFilters = __constant_cpu_to_le16 (0),
622 .bNumberPowerFilters = 0,
623};
624
625
626#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
627
628/* include the status endpoint if we can, even where it's optional.
629 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
630 * packet, to simplify cancellation; and a big transfer interval, to
631 * waste less bandwidth.
632 *
633 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
634 * if they ignore the connect/disconnect notifications that real aether
635 * can provide. more advanced cdc configurations might want to support
636 * encapsulated commands (vendor-specific, using control-OUT).
637 *
638 * RNDIS requires the status endpoint, since it uses that encapsulation
639 * mechanism for its funky RPC scheme.
640 */
641
642#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
643#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
644
645static struct usb_endpoint_descriptor
646fs_status_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_INT,
652 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
653 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
654};
655#endif
656
657#ifdef DEV_CONFIG_CDC
658
659/* the default data interface has no endpoints ... */
660
661static const struct usb_interface_descriptor
662data_nop_intf = {
663 .bLength = sizeof data_nop_intf,
664 .bDescriptorType = USB_DT_INTERFACE,
665
666 .bInterfaceNumber = 1,
667 .bAlternateSetting = 0,
668 .bNumEndpoints = 0,
669 .bInterfaceClass = USB_CLASS_CDC_DATA,
670 .bInterfaceSubClass = 0,
671 .bInterfaceProtocol = 0,
672};
673
674/* ... but the "real" data interface has two bulk endpoints */
675
676static const struct usb_interface_descriptor
677data_intf = {
678 .bLength = sizeof data_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
680
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 1,
683 .bNumEndpoints = 2,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687 .iInterface = STRING_DATA,
688};
689
690#endif
691
692#ifdef CONFIG_USB_ETH_RNDIS
693
694/* RNDIS doesn't activate by changing to the "real" altsetting */
695
696static const struct usb_interface_descriptor
697rndis_data_intf = {
698 .bLength = sizeof rndis_data_intf,
699 .bDescriptorType = USB_DT_INTERFACE,
700
701 .bInterfaceNumber = 1,
702 .bAlternateSetting = 0,
703 .bNumEndpoints = 2,
704 .bInterfaceClass = USB_CLASS_CDC_DATA,
705 .bInterfaceSubClass = 0,
706 .bInterfaceProtocol = 0,
707 .iInterface = STRING_DATA,
708};
709
710#endif
711
712#ifdef DEV_CONFIG_SUBSET
713
714/*
715 * "Simple" CDC-subset option is a simple vendor-neutral model that most
716 * full speed controllers can handle: one interface, two bulk endpoints.
717 *
718 * To assist host side drivers, we fancy it up a bit, and add descriptors
719 * so some host side drivers will understand it as a "SAFE" variant.
720 */
721
722static const struct usb_interface_descriptor
723subset_data_intf = {
724 .bLength = sizeof subset_data_intf,
725 .bDescriptorType = USB_DT_INTERFACE,
726
727 .bInterfaceNumber = 0,
728 .bAlternateSetting = 0,
729 .bNumEndpoints = 2,
730 .bInterfaceClass = USB_CLASS_COMM,
731 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
732 .bInterfaceProtocol = 0,
733 .iInterface = STRING_DATA,
734};
735
736#endif /* SUBSET */
737
738
739static struct usb_endpoint_descriptor
740fs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
743
744 .bEndpointAddress = USB_DIR_IN,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
746};
747
748static struct usb_endpoint_descriptor
749fs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
752
753 .bEndpointAddress = USB_DIR_OUT,
754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
755}; 165};
756 166
757static const struct usb_descriptor_header *fs_eth_function [11] = { 167static const struct usb_descriptor_header *otg_desc[] = {
758 (struct usb_descriptor_header *) &otg_descriptor, 168 (struct usb_descriptor_header *) &otg_descriptor,
759#ifdef DEV_CONFIG_CDC
760 /* "cdc" mode descriptors */
761 (struct usb_descriptor_header *) &control_intf,
762 (struct usb_descriptor_header *) &header_desc,
763 (struct usb_descriptor_header *) &union_desc,
764 (struct usb_descriptor_header *) &ether_desc,
765 /* NOTE: status endpoint may need to be removed */
766 (struct usb_descriptor_header *) &fs_status_desc,
767 /* data interface, with altsetting */
768 (struct usb_descriptor_header *) &data_nop_intf,
769 (struct usb_descriptor_header *) &data_intf,
770 (struct usb_descriptor_header *) &fs_source_desc,
771 (struct usb_descriptor_header *) &fs_sink_desc,
772 NULL, 169 NULL,
773#endif /* DEV_CONFIG_CDC */
774}; 170};
775 171
776static inline void __init fs_subset_descriptors(void)
777{
778#ifdef DEV_CONFIG_SUBSET
779 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
780 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
781 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
782 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
783 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
784 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
785 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
786 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
787 fs_eth_function[8] = NULL;
788#else
789 fs_eth_function[1] = NULL;
790#endif
791}
792 172
793#ifdef CONFIG_USB_ETH_RNDIS 173/* string IDs are assigned dynamically */
794static const struct usb_descriptor_header *fs_rndis_function [] = {
795 (struct usb_descriptor_header *) &otg_descriptor,
796 /* control interface matches ACM, not Ethernet */
797 (struct usb_descriptor_header *) &rndis_control_intf,
798 (struct usb_descriptor_header *) &header_desc,
799 (struct usb_descriptor_header *) &call_mgmt_descriptor,
800 (struct usb_descriptor_header *) &acm_descriptor,
801 (struct usb_descriptor_header *) &union_desc,
802 (struct usb_descriptor_header *) &fs_status_desc,
803 /* data interface has no altsetting */
804 (struct usb_descriptor_header *) &rndis_data_intf,
805 (struct usb_descriptor_header *) &fs_source_desc,
806 (struct usb_descriptor_header *) &fs_sink_desc,
807 NULL,
808};
809#endif
810
811/*
812 * usb 2.0 devices need to expose both high speed and full speed
813 * descriptors, unless they only run at full speed.
814 */
815
816#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
817static struct usb_endpoint_descriptor
818hs_status_desc = {
819 .bLength = USB_DT_ENDPOINT_SIZE,
820 .bDescriptorType = USB_DT_ENDPOINT,
821
822 .bmAttributes = USB_ENDPOINT_XFER_INT,
823 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
824 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
825};
826#endif /* DEV_CONFIG_CDC */
827
828static struct usb_endpoint_descriptor
829hs_source_desc = {
830 .bLength = USB_DT_ENDPOINT_SIZE,
831 .bDescriptorType = USB_DT_ENDPOINT,
832
833 .bmAttributes = USB_ENDPOINT_XFER_BULK,
834 .wMaxPacketSize = __constant_cpu_to_le16 (512),
835};
836
837static struct usb_endpoint_descriptor
838hs_sink_desc = {
839 .bLength = USB_DT_ENDPOINT_SIZE,
840 .bDescriptorType = USB_DT_ENDPOINT,
841
842 .bmAttributes = USB_ENDPOINT_XFER_BULK,
843 .wMaxPacketSize = __constant_cpu_to_le16 (512),
844};
845 174
846static struct usb_qualifier_descriptor 175#define STRING_MANUFACTURER_IDX 0
847dev_qualifier = { 176#define STRING_PRODUCT_IDX 1
848 .bLength = sizeof dev_qualifier,
849 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
850 177
851 .bcdUSB = __constant_cpu_to_le16 (0x0200), 178static char manufacturer[50];
852 .bDeviceClass = USB_CLASS_COMM,
853 179
854 .bNumConfigurations = 1, 180static struct usb_string strings_dev[] = {
181 [STRING_MANUFACTURER_IDX].s = manufacturer,
182 [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
183 { } /* end of list */
855}; 184};
856 185
857static const struct usb_descriptor_header *hs_eth_function [11] = { 186static struct usb_gadget_strings stringtab_dev = {
858 (struct usb_descriptor_header *) &otg_descriptor, 187 .language = 0x0409, /* en-us */
859#ifdef DEV_CONFIG_CDC 188 .strings = strings_dev,
860 /* "cdc" mode descriptors */
861 (struct usb_descriptor_header *) &control_intf,
862 (struct usb_descriptor_header *) &header_desc,
863 (struct usb_descriptor_header *) &union_desc,
864 (struct usb_descriptor_header *) &ether_desc,
865 /* NOTE: status endpoint may need to be removed */
866 (struct usb_descriptor_header *) &hs_status_desc,
867 /* data interface, with altsetting */
868 (struct usb_descriptor_header *) &data_nop_intf,
869 (struct usb_descriptor_header *) &data_intf,
870 (struct usb_descriptor_header *) &hs_source_desc,
871 (struct usb_descriptor_header *) &hs_sink_desc,
872 NULL,
873#endif /* DEV_CONFIG_CDC */
874}; 189};
875 190
876static inline void __init hs_subset_descriptors(void) 191static struct usb_gadget_strings *dev_strings[] = {
877{ 192 &stringtab_dev,
878#ifdef DEV_CONFIG_SUBSET
879 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
880 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
881 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
882 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
883 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
884 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
885 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
886 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
887 hs_eth_function[8] = NULL;
888#else
889 hs_eth_function[1] = NULL;
890#endif
891}
892
893#ifdef CONFIG_USB_ETH_RNDIS
894static const struct usb_descriptor_header *hs_rndis_function [] = {
895 (struct usb_descriptor_header *) &otg_descriptor,
896 /* control interface matches ACM, not Ethernet */
897 (struct usb_descriptor_header *) &rndis_control_intf,
898 (struct usb_descriptor_header *) &header_desc,
899 (struct usb_descriptor_header *) &call_mgmt_descriptor,
900 (struct usb_descriptor_header *) &acm_descriptor,
901 (struct usb_descriptor_header *) &union_desc,
902 (struct usb_descriptor_header *) &hs_status_desc,
903 /* data interface has no altsetting */
904 (struct usb_descriptor_header *) &rndis_data_intf,
905 (struct usb_descriptor_header *) &hs_source_desc,
906 (struct usb_descriptor_header *) &hs_sink_desc,
907 NULL, 193 NULL,
908}; 194};
909#endif
910
911
912/* maxpacket and other transfer characteristics vary by speed. */
913static inline struct usb_endpoint_descriptor *
914ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
915 struct usb_endpoint_descriptor *fs)
916{
917 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
918 return hs;
919 return fs;
920}
921 195
196static u8 hostaddr[ETH_ALEN];
922 197
923/*-------------------------------------------------------------------------*/ 198/*-------------------------------------------------------------------------*/
924 199
925/* descriptors that are built on-demand */
926
927static char manufacturer [50];
928static char product_desc [40] = DRIVER_DESC;
929static char serial_number [20];
930
931/* address that the host will use ... usually assigned at random */
932static char ethaddr [2 * ETH_ALEN + 1];
933
934/* static strings, in UTF-8 */
935static struct usb_string strings [] = {
936 { STRING_MANUFACTURER, manufacturer, },
937 { STRING_PRODUCT, product_desc, },
938 { STRING_SERIALNUMBER, serial_number, },
939 { STRING_DATA, "Ethernet Data", },
940 { STRING_ETHADDR, ethaddr, },
941#ifdef DEV_CONFIG_CDC
942 { STRING_CDC, "CDC Ethernet", },
943 { STRING_CONTROL, "CDC Communications Control", },
944#endif
945#ifdef DEV_CONFIG_SUBSET
946 { STRING_SUBSET, "CDC Ethernet Subset", },
947#endif
948#ifdef CONFIG_USB_ETH_RNDIS
949 { STRING_RNDIS, "RNDIS", },
950 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
951#endif
952 { } /* end of list */
953};
954
955static struct usb_gadget_strings stringtab = {
956 .language = 0x0409, /* en-us */
957 .strings = strings,
958};
959
960/* 200/*
961 * one config, two interfaces: control, data. 201 * We may not have an RNDIS configuration, but if we do it needs to be
962 * complications: class descriptors, and an altsetting. 202 * the first one present. That's to make Microsoft's drivers happy,
963 */ 203 * and to follow DOCSIS 1.0 (cable modem standard).
964static int
965config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
966{
967 int len;
968 const struct usb_config_descriptor *config;
969 const struct usb_descriptor_header **function;
970 int hs = 0;
971
972 if (gadget_is_dualspeed(g)) {
973 hs = (g->speed == USB_SPEED_HIGH);
974 if (type == USB_DT_OTHER_SPEED_CONFIG)
975 hs = !hs;
976 }
977#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
978
979 if (index >= device_desc.bNumConfigurations)
980 return -EINVAL;
981
982#ifdef CONFIG_USB_ETH_RNDIS
983 /* list the RNDIS config first, to make Microsoft's drivers
984 * happy. DOCSIS 1.0 needs this too.
985 */
986 if (device_desc.bNumConfigurations == 2 && index == 0) {
987 config = &rndis_config;
988 function = which_fn (rndis);
989 } else
990#endif
991 {
992 config = &eth_config;
993 function = which_fn (eth);
994 }
995
996 /* for now, don't advertise srp-only devices */
997 if (!is_otg)
998 function++;
999
1000 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1001 if (len < 0)
1002 return len;
1003 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1004 return len;
1005}
1006
1007/*-------------------------------------------------------------------------*/
1008
1009static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1010static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1011
1012static int
1013set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1014{
1015 int result = 0;
1016 struct usb_gadget *gadget = dev->gadget;
1017
1018#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1019 /* status endpoint used for RNDIS and (optionally) CDC */
1020 if (!subset_active(dev) && dev->status_ep) {
1021 dev->status = ep_desc (gadget, &hs_status_desc,
1022 &fs_status_desc);
1023 dev->status_ep->driver_data = dev;
1024
1025 result = usb_ep_enable (dev->status_ep, dev->status);
1026 if (result != 0) {
1027 DEBUG (dev, "enable %s --> %d\n",
1028 dev->status_ep->name, result);
1029 goto done;
1030 }
1031 }
1032#endif
1033
1034 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1035 dev->in_ep->driver_data = dev;
1036
1037 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1038 dev->out_ep->driver_data = dev;
1039
1040 /* With CDC, the host isn't allowed to use these two data
1041 * endpoints in the default altsetting for the interface.
1042 * so we don't activate them yet. Reset from SET_INTERFACE.
1043 *
1044 * Strictly speaking RNDIS should work the same: activation is
1045 * a side effect of setting a packet filter. Deactivation is
1046 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1047 */
1048 if (!cdc_active(dev)) {
1049 result = usb_ep_enable (dev->in_ep, dev->in);
1050 if (result != 0) {
1051 DEBUG(dev, "enable %s --> %d\n",
1052 dev->in_ep->name, result);
1053 goto done;
1054 }
1055
1056 result = usb_ep_enable (dev->out_ep, dev->out);
1057 if (result != 0) {
1058 DEBUG (dev, "enable %s --> %d\n",
1059 dev->out_ep->name, result);
1060 goto done;
1061 }
1062 }
1063
1064done:
1065 if (result == 0)
1066 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1067
1068 /* on error, disable any endpoints */
1069 if (result < 0) {
1070 if (!subset_active(dev) && dev->status_ep)
1071 (void) usb_ep_disable (dev->status_ep);
1072 dev->status = NULL;
1073 (void) usb_ep_disable (dev->in_ep);
1074 (void) usb_ep_disable (dev->out_ep);
1075 dev->in = NULL;
1076 dev->out = NULL;
1077 }
1078
1079 /* activate non-CDC configs right away
1080 * this isn't strictly according to the RNDIS spec
1081 */
1082 else if (!cdc_active (dev)) {
1083 netif_carrier_on (dev->net);
1084 if (netif_running (dev->net)) {
1085 spin_unlock (&dev->lock);
1086 eth_start (dev, GFP_ATOMIC);
1087 spin_lock (&dev->lock);
1088 }
1089 }
1090
1091 if (result == 0)
1092 DEBUG (dev, "qlen %d\n", qlen (gadget));
1093
1094 /* caller is responsible for cleanup on error */
1095 return result;
1096}
1097
1098static void eth_reset_config (struct eth_dev *dev)
1099{
1100 struct usb_request *req;
1101
1102 if (dev->config == 0)
1103 return;
1104
1105 DEBUG (dev, "%s\n", __func__);
1106
1107 netif_stop_queue (dev->net);
1108 netif_carrier_off (dev->net);
1109 rndis_uninit(dev->rndis_config);
1110
1111 /* disable endpoints, forcing (synchronous) completion of
1112 * pending i/o. then free the requests.
1113 */
1114 if (dev->in) {
1115 usb_ep_disable (dev->in_ep);
1116 spin_lock(&dev->req_lock);
1117 while (likely (!list_empty (&dev->tx_reqs))) {
1118 req = container_of (dev->tx_reqs.next,
1119 struct usb_request, list);
1120 list_del (&req->list);
1121
1122 spin_unlock(&dev->req_lock);
1123 usb_ep_free_request (dev->in_ep, req);
1124 spin_lock(&dev->req_lock);
1125 }
1126 spin_unlock(&dev->req_lock);
1127 }
1128 if (dev->out) {
1129 usb_ep_disable (dev->out_ep);
1130 spin_lock(&dev->req_lock);
1131 while (likely (!list_empty (&dev->rx_reqs))) {
1132 req = container_of (dev->rx_reqs.next,
1133 struct usb_request, list);
1134 list_del (&req->list);
1135
1136 spin_unlock(&dev->req_lock);
1137 usb_ep_free_request (dev->out_ep, req);
1138 spin_lock(&dev->req_lock);
1139 }
1140 spin_unlock(&dev->req_lock);
1141 }
1142
1143 if (dev->status) {
1144 usb_ep_disable (dev->status_ep);
1145 }
1146 dev->rndis = 0;
1147 dev->cdc_filter = 0;
1148 dev->config = 0;
1149}
1150
1151/* change our operational config. must agree with the code
1152 * that returns config descriptors, and altsetting code.
1153 */ 204 */
1154static int 205static int __init rndis_do_config(struct usb_configuration *c)
1155eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1156{ 206{
1157 int result = 0; 207 /* FIXME alloc iConfiguration string, set it in c->strings */
1158 struct usb_gadget *gadget = dev->gadget;
1159
1160 if (gadget_is_sa1100 (gadget)
1161 && dev->config
1162 && atomic_read (&dev->tx_qlen) != 0) {
1163 /* tx fifo is full, but we can't clear it...*/
1164 INFO (dev, "can't change configurations\n");
1165 return -ESPIPE;
1166 }
1167 eth_reset_config (dev);
1168
1169 switch (number) {
1170 case DEV_CONFIG_VALUE:
1171 result = set_ether_config (dev, gfp_flags);
1172 break;
1173#ifdef CONFIG_USB_ETH_RNDIS
1174 case DEV_RNDIS_CONFIG_VALUE:
1175 dev->rndis = 1;
1176 result = set_ether_config (dev, gfp_flags);
1177 break;
1178#endif
1179 default:
1180 result = -EINVAL;
1181 /* FALL THROUGH */
1182 case 0:
1183 break;
1184 }
1185
1186 if (result) {
1187 if (number)
1188 eth_reset_config (dev);
1189 usb_gadget_vbus_draw(dev->gadget,
1190 gadget_is_otg(dev->gadget) ? 8 : 100);
1191 } else {
1192 char *speed;
1193 unsigned power;
1194
1195 power = 2 * eth_config.bMaxPower;
1196 usb_gadget_vbus_draw(dev->gadget, power);
1197 208
1198 switch (gadget->speed) { 209 if (gadget_is_otg(c->cdev->gadget)) {
1199 case USB_SPEED_FULL: speed = "full"; break; 210 c->descriptors = otg_desc;
1200#ifdef CONFIG_USB_GADGET_DUALSPEED 211 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1201 case USB_SPEED_HIGH: speed = "high"; break;
1202#endif
1203 default: speed = "?"; break;
1204 }
1205
1206 dev->config = number;
1207 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1208 speed, number, power, driver_desc,
1209 rndis_active(dev)
1210 ? "RNDIS"
1211 : (cdc_active(dev)
1212 ? "CDC Ethernet"
1213 : "CDC Ethernet Subset"));
1214 } 212 }
1215 return result;
1216}
1217
1218/*-------------------------------------------------------------------------*/
1219
1220#ifdef DEV_CONFIG_CDC
1221 213
1222/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 214 return rndis_bind_config(c, hostaddr);
1223 * only to notify the host about link status changes (which we support) or
1224 * report completion of some encapsulated command (as used in RNDIS). Since
1225 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1226 * command mechanism; and only one status request is ever queued.
1227 */
1228
1229static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1230{
1231 struct usb_cdc_notification *event = req->buf;
1232 int value = req->status;
1233 struct eth_dev *dev = ep->driver_data;
1234
1235 /* issue the second notification if host reads the first */
1236 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1237 && value == 0) {
1238 __le32 *data = req->buf + sizeof *event;
1239
1240 event->bmRequestType = 0xA1;
1241 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1242 event->wValue = __constant_cpu_to_le16 (0);
1243 event->wIndex = __constant_cpu_to_le16 (1);
1244 event->wLength = __constant_cpu_to_le16 (8);
1245
1246 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1247 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1248
1249 req->length = STATUS_BYTECOUNT;
1250 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1251 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1252 if (value == 0)
1253 return;
1254 } else if (value != -ECONNRESET)
1255 DEBUG (dev, "event %02x --> %d\n",
1256 event->bNotificationType, value);
1257 req->context = NULL;
1258} 215}
1259 216
1260static void issue_start_status (struct eth_dev *dev) 217static struct usb_configuration rndis_config_driver = {
1261{ 218 .label = "RNDIS",
1262 struct usb_request *req = dev->stat_req; 219 .bind = rndis_do_config,
1263 struct usb_cdc_notification *event; 220 .bConfigurationValue = 2,
1264 int value; 221 /* .iConfiguration = DYNAMIC */
1265 222 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
1266 DEBUG (dev, "%s, flush old status first\n", __func__); 223 .bMaxPower = 1, /* 2 mA, minimal */
1267 224};
1268 /* flush old status
1269 *
1270 * FIXME ugly idiom, maybe we'd be better with just
1271 * a "cancel the whole queue" primitive since any
1272 * unlink-one primitive has way too many error modes.
1273 * here, we "know" toggle is already clear...
1274 *
1275 * FIXME iff req->context != null just dequeue it
1276 */
1277 usb_ep_disable (dev->status_ep);
1278 usb_ep_enable (dev->status_ep, dev->status);
1279
1280 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1281 * a SPEED_CHANGE. could be useful in some configs.
1282 */
1283 event = req->buf;
1284 event->bmRequestType = 0xA1;
1285 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1286 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1287 event->wIndex = __constant_cpu_to_le16 (1);
1288 event->wLength = 0;
1289
1290 req->length = sizeof *event;
1291 req->complete = eth_status_complete;
1292 req->context = dev;
1293
1294 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1295 if (value < 0)
1296 DEBUG (dev, "status buf queue --> %d\n", value);
1297}
1298
1299#endif
1300 225
1301/*-------------------------------------------------------------------------*/ 226/*-------------------------------------------------------------------------*/
1302 227
1303static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1304{
1305 if (req->status || req->actual != req->length)
1306 DEBUG ((struct eth_dev *) ep->driver_data,
1307 "setup complete --> %d, %d/%d\n",
1308 req->status, req->actual, req->length);
1309}
1310
1311#ifdef CONFIG_USB_ETH_RNDIS
1312
1313static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1314{
1315 if (req->status || req->actual != req->length)
1316 DEBUG ((struct eth_dev *) ep->driver_data,
1317 "rndis response complete --> %d, %d/%d\n",
1318 req->status, req->actual, req->length);
1319
1320 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1321}
1322
1323static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1324{
1325 struct eth_dev *dev = ep->driver_data;
1326 int status;
1327
1328 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1329 spin_lock(&dev->lock);
1330 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1331 if (status < 0)
1332 ERROR(dev, "%s: rndis parse error %d\n", __func__, status);
1333 spin_unlock(&dev->lock);
1334}
1335
1336#endif /* RNDIS */
1337
1338/* 228/*
1339 * The setup() callback implements all the ep0 functionality that's not 229 * We _always_ have an ECM or CDC Subset configuration.
1340 * handled lower down. CDC has a number of less-common features:
1341 *
1342 * - two interfaces: control, and ethernet data
1343 * - Ethernet data interface has two altsettings: default, and active
1344 * - class-specific descriptors for the control interface
1345 * - class-specific control requests
1346 */ 230 */
1347static int 231static int __init eth_do_config(struct usb_configuration *c)
1348eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1349{ 232{
1350 struct eth_dev *dev = get_gadget_data (gadget); 233 /* FIXME alloc iConfiguration string, set it in c->strings */
1351 struct usb_request *req = dev->req;
1352 int value = -EOPNOTSUPP;
1353 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1354 u16 wValue = le16_to_cpu(ctrl->wValue);
1355 u16 wLength = le16_to_cpu(ctrl->wLength);
1356
1357 /* descriptors just go into the pre-allocated ep0 buffer,
1358 * while config change events may enable network traffic.
1359 */
1360 req->complete = eth_setup_complete;
1361 switch (ctrl->bRequest) {
1362
1363 case USB_REQ_GET_DESCRIPTOR:
1364 if (ctrl->bRequestType != USB_DIR_IN)
1365 break;
1366 switch (wValue >> 8) {
1367
1368 case USB_DT_DEVICE:
1369 value = min (wLength, (u16) sizeof device_desc);
1370 memcpy (req->buf, &device_desc, value);
1371 break;
1372 case USB_DT_DEVICE_QUALIFIER:
1373 if (!gadget_is_dualspeed(gadget))
1374 break;
1375 value = min (wLength, (u16) sizeof dev_qualifier);
1376 memcpy (req->buf, &dev_qualifier, value);
1377 break;
1378
1379 case USB_DT_OTHER_SPEED_CONFIG:
1380 if (!gadget_is_dualspeed(gadget))
1381 break;
1382 // FALLTHROUGH
1383 case USB_DT_CONFIG:
1384 value = config_buf(gadget, req->buf,
1385 wValue >> 8,
1386 wValue & 0xff,
1387 gadget_is_otg(gadget));
1388 if (value >= 0)
1389 value = min (wLength, (u16) value);
1390 break;
1391
1392 case USB_DT_STRING:
1393 value = usb_gadget_get_string (&stringtab,
1394 wValue & 0xff, req->buf);
1395 if (value >= 0)
1396 value = min (wLength, (u16) value);
1397 break;
1398 }
1399 break;
1400
1401 case USB_REQ_SET_CONFIGURATION:
1402 if (ctrl->bRequestType != 0)
1403 break;
1404 if (gadget->a_hnp_support)
1405 DEBUG (dev, "HNP available\n");
1406 else if (gadget->a_alt_hnp_support)
1407 DEBUG (dev, "HNP needs a different root port\n");
1408 spin_lock (&dev->lock);
1409 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1410 spin_unlock (&dev->lock);
1411 break;
1412 case USB_REQ_GET_CONFIGURATION:
1413 if (ctrl->bRequestType != USB_DIR_IN)
1414 break;
1415 *(u8 *)req->buf = dev->config;
1416 value = min (wLength, (u16) 1);
1417 break;
1418
1419 case USB_REQ_SET_INTERFACE:
1420 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1421 || !dev->config
1422 || wIndex > 1)
1423 break;
1424 if (!cdc_active(dev) && wIndex != 0)
1425 break;
1426 spin_lock (&dev->lock);
1427
1428 /* PXA hardware partially handles SET_INTERFACE;
1429 * we need to kluge around that interference.
1430 */
1431 if (gadget_is_pxa (gadget)) {
1432 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1433 GFP_ATOMIC);
1434 goto done_set_intf;
1435 }
1436
1437#ifdef DEV_CONFIG_CDC
1438 switch (wIndex) {
1439 case 0: /* control/master intf */
1440 if (wValue != 0)
1441 break;
1442 if (dev->status) {
1443 usb_ep_disable (dev->status_ep);
1444 usb_ep_enable (dev->status_ep, dev->status);
1445 }
1446 value = 0;
1447 break;
1448 case 1: /* data intf */
1449 if (wValue > 1)
1450 break;
1451 usb_ep_disable (dev->in_ep);
1452 usb_ep_disable (dev->out_ep);
1453
1454 /* CDC requires the data transfers not be done from
1455 * the default interface setting ... also, setting
1456 * the non-default interface resets filters etc.
1457 */
1458 if (wValue == 1) {
1459 if (!cdc_active (dev))
1460 break;
1461 usb_ep_enable (dev->in_ep, dev->in);
1462 usb_ep_enable (dev->out_ep, dev->out);
1463 dev->cdc_filter = DEFAULT_FILTER;
1464 netif_carrier_on (dev->net);
1465 if (dev->status)
1466 issue_start_status (dev);
1467 if (netif_running (dev->net)) {
1468 spin_unlock (&dev->lock);
1469 eth_start (dev, GFP_ATOMIC);
1470 spin_lock (&dev->lock);
1471 }
1472 } else {
1473 netif_stop_queue (dev->net);
1474 netif_carrier_off (dev->net);
1475 }
1476 value = 0;
1477 break;
1478 }
1479#else
1480 /* FIXME this is wrong, as is the assumption that
1481 * all non-PXA hardware talks real CDC ...
1482 */
1483 dev_warn (&gadget->dev, "set_interface ignored!\n");
1484#endif /* DEV_CONFIG_CDC */
1485
1486done_set_intf:
1487 spin_unlock (&dev->lock);
1488 break;
1489 case USB_REQ_GET_INTERFACE:
1490 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1491 || !dev->config
1492 || wIndex > 1)
1493 break;
1494 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1495 break;
1496
1497 /* for CDC, iff carrier is on, data interface is active. */
1498 if (rndis_active(dev) || wIndex != 1)
1499 *(u8 *)req->buf = 0;
1500 else
1501 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1502 value = min (wLength, (u16) 1);
1503 break;
1504
1505#ifdef DEV_CONFIG_CDC
1506 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1507 /* see 6.2.30: no data, wIndex = interface,
1508 * wValue = packet filter bitmap
1509 */
1510 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1511 || !cdc_active(dev)
1512 || wLength != 0
1513 || wIndex > 1)
1514 break;
1515 DEBUG (dev, "packet filter %02x\n", wValue);
1516 dev->cdc_filter = wValue;
1517 value = 0;
1518 break;
1519
1520 /* and potentially:
1521 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1522 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1523 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1524 * case USB_CDC_GET_ETHERNET_STATISTIC:
1525 */
1526
1527#endif /* DEV_CONFIG_CDC */
1528 234
1529#ifdef CONFIG_USB_ETH_RNDIS 235 if (gadget_is_otg(c->cdev->gadget)) {
1530 /* RNDIS uses the CDC command encapsulation mechanism to implement 236 c->descriptors = otg_desc;
1531 * an RPC scheme, with much getting/setting of attributes by OID. 237 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1532 */
1533 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1534 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1535 || !rndis_active(dev)
1536 || wLength > USB_BUFSIZ
1537 || wValue
1538 || rndis_control_intf.bInterfaceNumber
1539 != wIndex)
1540 break;
1541 /* read the request, then process it */
1542 value = wLength;
1543 req->complete = rndis_command_complete;
1544 /* later, rndis_control_ack () sends a notification */
1545 break;
1546
1547 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1548 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1549 == ctrl->bRequestType
1550 && rndis_active(dev)
1551 // && wLength >= 0x0400
1552 && !wValue
1553 && rndis_control_intf.bInterfaceNumber
1554 == wIndex) {
1555 u8 *buf;
1556 u32 n;
1557
1558 /* return the result */
1559 buf = rndis_get_next_response(dev->rndis_config, &n);
1560 if (buf) {
1561 memcpy(req->buf, buf, n);
1562 req->complete = rndis_response_complete;
1563 rndis_free_response(dev->rndis_config, buf);
1564 value = n;
1565 }
1566 /* else stalls ... spec says to avoid that */
1567 }
1568 break;
1569#endif /* RNDIS */
1570
1571 default:
1572 VDEBUG (dev,
1573 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1574 ctrl->bRequestType, ctrl->bRequest,
1575 wValue, wIndex, wLength);
1576 } 238 }
1577 239
1578 /* respond with data transfer before status phase? */ 240 if (can_support_ecm(c->cdev->gadget))
1579 if (value >= 0) { 241 return ecm_bind_config(c, hostaddr);
1580 req->length = value;
1581 req->zero = value < wLength
1582 && (value % gadget->ep0->maxpacket) == 0;
1583 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1584 if (value < 0) {
1585 DEBUG (dev, "ep_queue --> %d\n", value);
1586 req->status = 0;
1587 eth_setup_complete (gadget->ep0, req);
1588 }
1589 }
1590
1591 /* host either stalls (value < 0) or reports success */
1592 return value;
1593}
1594
1595static void
1596eth_disconnect (struct usb_gadget *gadget)
1597{
1598 struct eth_dev *dev = get_gadget_data (gadget);
1599 unsigned long flags;
1600
1601 spin_lock_irqsave (&dev->lock, flags);
1602 netif_stop_queue (dev->net);
1603 netif_carrier_off (dev->net);
1604 eth_reset_config (dev);
1605 spin_unlock_irqrestore (&dev->lock, flags);
1606
1607 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1608
1609 /* next we may get setup() calls to enumerate new connections;
1610 * or an unbind() during shutdown (including removing module).
1611 */
1612}
1613
1614/*-------------------------------------------------------------------------*/
1615
1616/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1617
1618static int eth_change_mtu (struct net_device *net, int new_mtu)
1619{
1620 struct eth_dev *dev = netdev_priv(net);
1621
1622 if (dev->rndis)
1623 return -EBUSY;
1624
1625 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1626 return -ERANGE;
1627 /* no zero-length packet read wanted after mtu-sized packets */
1628 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1629 return -EDOM;
1630 net->mtu = new_mtu;
1631 return 0;
1632}
1633
1634static struct net_device_stats *eth_get_stats (struct net_device *net)
1635{
1636 return &((struct eth_dev *)netdev_priv(net))->stats;
1637}
1638
1639static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1640{
1641 struct eth_dev *dev = netdev_priv(net);
1642 strlcpy(p->driver, shortname, sizeof p->driver);
1643 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1644 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1645 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1646}
1647
1648static u32 eth_get_link(struct net_device *net)
1649{
1650 struct eth_dev *dev = netdev_priv(net);
1651 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1652}
1653
1654static struct ethtool_ops ops = {
1655 .get_drvinfo = eth_get_drvinfo,
1656 .get_link = eth_get_link
1657};
1658
1659static void defer_kevent (struct eth_dev *dev, int flag)
1660{
1661 if (test_and_set_bit (flag, &dev->todo))
1662 return;
1663 if (!schedule_work (&dev->work))
1664 ERROR (dev, "kevent %d may have been dropped\n", flag);
1665 else 242 else
1666 DEBUG (dev, "kevent %d scheduled\n", flag); 243 return geth_bind_config(c, hostaddr);
1667}
1668
1669static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1670
1671static int
1672rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1673{
1674 struct sk_buff *skb;
1675 int retval = -ENOMEM;
1676 size_t size;
1677
1678 /* Padding up to RX_EXTRA handles minor disagreements with host.
1679 * Normally we use the USB "terminate on short read" convention;
1680 * so allow up to (N*maxpacket), since that memory is normally
1681 * already allocated. Some hardware doesn't deal well with short
1682 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1683 * byte off the end (to force hardware errors on overflow).
1684 *
1685 * RNDIS uses internal framing, and explicitly allows senders to
1686 * pad to end-of-packet. That's potentially nice for speed,
1687 * but means receivers can't recover synch on their own.
1688 */
1689 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1690 size += dev->out_ep->maxpacket - 1;
1691 if (rndis_active(dev))
1692 size += sizeof (struct rndis_packet_msg_type);
1693 size -= size % dev->out_ep->maxpacket;
1694
1695 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
1696 if (skb == NULL) {
1697 DEBUG (dev, "no rx skb\n");
1698 goto enomem;
1699 }
1700
1701 /* Some platforms perform better when IP packets are aligned,
1702 * but on at least one, checksumming fails otherwise. Note:
1703 * RNDIS headers involve variable numbers of LE32 values.
1704 */
1705 skb_reserve(skb, NET_IP_ALIGN);
1706
1707 req->buf = skb->data;
1708 req->length = size;
1709 req->complete = rx_complete;
1710 req->context = skb;
1711
1712 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1713 if (retval == -ENOMEM)
1714enomem:
1715 defer_kevent (dev, WORK_RX_MEMORY);
1716 if (retval) {
1717 DEBUG (dev, "rx submit --> %d\n", retval);
1718 if (skb)
1719 dev_kfree_skb_any(skb);
1720 spin_lock(&dev->req_lock);
1721 list_add (&req->list, &dev->rx_reqs);
1722 spin_unlock(&dev->req_lock);
1723 }
1724 return retval;
1725}
1726
1727static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1728{
1729 struct sk_buff *skb = req->context;
1730 struct eth_dev *dev = ep->driver_data;
1731 int status = req->status;
1732
1733 switch (status) {
1734
1735 /* normal completion */
1736 case 0:
1737 skb_put (skb, req->actual);
1738 /* we know MaxPacketsPerTransfer == 1 here */
1739 if (rndis_active(dev))
1740 status = rndis_rm_hdr (skb);
1741 if (status < 0
1742 || ETH_HLEN > skb->len
1743 || skb->len > ETH_FRAME_LEN) {
1744 dev->stats.rx_errors++;
1745 dev->stats.rx_length_errors++;
1746 DEBUG (dev, "rx length %d\n", skb->len);
1747 break;
1748 }
1749
1750 skb->protocol = eth_type_trans (skb, dev->net);
1751 dev->stats.rx_packets++;
1752 dev->stats.rx_bytes += skb->len;
1753
1754 /* no buffer copies needed, unless hardware can't
1755 * use skb buffers.
1756 */
1757 status = netif_rx (skb);
1758 skb = NULL;
1759 break;
1760
1761 /* software-driven interface shutdown */
1762 case -ECONNRESET: // unlink
1763 case -ESHUTDOWN: // disconnect etc
1764 VDEBUG (dev, "rx shutdown, code %d\n", status);
1765 goto quiesce;
1766
1767 /* for hardware automagic (such as pxa) */
1768 case -ECONNABORTED: // endpoint reset
1769 DEBUG (dev, "rx %s reset\n", ep->name);
1770 defer_kevent (dev, WORK_RX_MEMORY);
1771quiesce:
1772 dev_kfree_skb_any (skb);
1773 goto clean;
1774
1775 /* data overrun */
1776 case -EOVERFLOW:
1777 dev->stats.rx_over_errors++;
1778 // FALLTHROUGH
1779
1780 default:
1781 dev->stats.rx_errors++;
1782 DEBUG (dev, "rx status %d\n", status);
1783 break;
1784 }
1785
1786 if (skb)
1787 dev_kfree_skb_any (skb);
1788 if (!netif_running (dev->net)) {
1789clean:
1790 spin_lock(&dev->req_lock);
1791 list_add (&req->list, &dev->rx_reqs);
1792 spin_unlock(&dev->req_lock);
1793 req = NULL;
1794 }
1795 if (req)
1796 rx_submit (dev, req, GFP_ATOMIC);
1797}
1798
1799static int prealloc (struct list_head *list, struct usb_ep *ep,
1800 unsigned n, gfp_t gfp_flags)
1801{
1802 unsigned i;
1803 struct usb_request *req;
1804
1805 if (!n)
1806 return -ENOMEM;
1807
1808 /* queue/recycle up to N requests */
1809 i = n;
1810 list_for_each_entry (req, list, list) {
1811 if (i-- == 0)
1812 goto extra;
1813 }
1814 while (i--) {
1815 req = usb_ep_alloc_request (ep, gfp_flags);
1816 if (!req)
1817 return list_empty (list) ? -ENOMEM : 0;
1818 list_add (&req->list, list);
1819 }
1820 return 0;
1821
1822extra:
1823 /* free extras */
1824 for (;;) {
1825 struct list_head *next;
1826
1827 next = req->list.next;
1828 list_del (&req->list);
1829 usb_ep_free_request (ep, req);
1830
1831 if (next == list)
1832 break;
1833
1834 req = container_of (next, struct usb_request, list);
1835 }
1836 return 0;
1837}
1838
1839static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1840{
1841 int status;
1842
1843 spin_lock(&dev->req_lock);
1844 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1845 if (status < 0)
1846 goto fail;
1847 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1848 if (status < 0)
1849 goto fail;
1850 goto done;
1851fail:
1852 DEBUG (dev, "can't alloc requests\n");
1853done:
1854 spin_unlock(&dev->req_lock);
1855 return status;
1856}
1857
1858static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1859{
1860 struct usb_request *req;
1861 unsigned long flags;
1862
1863 /* fill unused rxq slots with some skb */
1864 spin_lock_irqsave(&dev->req_lock, flags);
1865 while (!list_empty (&dev->rx_reqs)) {
1866 req = container_of (dev->rx_reqs.next,
1867 struct usb_request, list);
1868 list_del_init (&req->list);
1869 spin_unlock_irqrestore(&dev->req_lock, flags);
1870
1871 if (rx_submit (dev, req, gfp_flags) < 0) {
1872 defer_kevent (dev, WORK_RX_MEMORY);
1873 return;
1874 }
1875
1876 spin_lock_irqsave(&dev->req_lock, flags);
1877 }
1878 spin_unlock_irqrestore(&dev->req_lock, flags);
1879}
1880
1881static void eth_work (struct work_struct *work)
1882{
1883 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1884
1885 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1886 if (netif_running (dev->net))
1887 rx_fill (dev, GFP_KERNEL);
1888 }
1889
1890 if (dev->todo)
1891 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1892}
1893
1894static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1895{
1896 struct sk_buff *skb = req->context;
1897 struct eth_dev *dev = ep->driver_data;
1898
1899 switch (req->status) {
1900 default:
1901 dev->stats.tx_errors++;
1902 VDEBUG (dev, "tx err %d\n", req->status);
1903 /* FALLTHROUGH */
1904 case -ECONNRESET: // unlink
1905 case -ESHUTDOWN: // disconnect etc
1906 break;
1907 case 0:
1908 dev->stats.tx_bytes += skb->len;
1909 }
1910 dev->stats.tx_packets++;
1911
1912 spin_lock(&dev->req_lock);
1913 list_add (&req->list, &dev->tx_reqs);
1914 spin_unlock(&dev->req_lock);
1915 dev_kfree_skb_any (skb);
1916
1917 atomic_dec (&dev->tx_qlen);
1918 if (netif_carrier_ok (dev->net))
1919 netif_wake_queue (dev->net);
1920}
1921
1922static inline int eth_is_promisc (struct eth_dev *dev)
1923{
1924 /* no filters for the CDC subset; always promisc */
1925 if (subset_active (dev))
1926 return 1;
1927 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1928}
1929
1930static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1931{
1932 struct eth_dev *dev = netdev_priv(net);
1933 int length = skb->len;
1934 int retval;
1935 struct usb_request *req = NULL;
1936 unsigned long flags;
1937
1938 /* apply outgoing CDC or RNDIS filters */
1939 if (!eth_is_promisc (dev)) {
1940 u8 *dest = skb->data;
1941
1942 if (is_multicast_ether_addr(dest)) {
1943 u16 type;
1944
1945 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1946 * SET_ETHERNET_MULTICAST_FILTERS requests
1947 */
1948 if (is_broadcast_ether_addr(dest))
1949 type = USB_CDC_PACKET_TYPE_BROADCAST;
1950 else
1951 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1952 if (!(dev->cdc_filter & type)) {
1953 dev_kfree_skb_any (skb);
1954 return 0;
1955 }
1956 }
1957 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1958 }
1959
1960 spin_lock_irqsave(&dev->req_lock, flags);
1961 /*
1962 * this freelist can be empty if an interrupt triggered disconnect()
1963 * and reconfigured the gadget (shutting down this queue) after the
1964 * network stack decided to xmit but before we got the spinlock.
1965 */
1966 if (list_empty(&dev->tx_reqs)) {
1967 spin_unlock_irqrestore(&dev->req_lock, flags);
1968 return 1;
1969 }
1970
1971 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1972 list_del (&req->list);
1973
1974 /* temporarily stop TX queue when the freelist empties */
1975 if (list_empty (&dev->tx_reqs))
1976 netif_stop_queue (net);
1977 spin_unlock_irqrestore(&dev->req_lock, flags);
1978
1979 /* no buffer copies needed, unless the network stack did it
1980 * or the hardware can't use skb buffers.
1981 * or there's not enough space for any RNDIS headers we need
1982 */
1983 if (rndis_active(dev)) {
1984 struct sk_buff *skb_rndis;
1985
1986 skb_rndis = skb_realloc_headroom (skb,
1987 sizeof (struct rndis_packet_msg_type));
1988 if (!skb_rndis)
1989 goto drop;
1990
1991 dev_kfree_skb_any (skb);
1992 skb = skb_rndis;
1993 rndis_add_hdr (skb);
1994 length = skb->len;
1995 }
1996 req->buf = skb->data;
1997 req->context = skb;
1998 req->complete = tx_complete;
1999
2000 /* use zlp framing on tx for strict CDC-Ether conformance,
2001 * though any robust network rx path ignores extra padding.
2002 * and some hardware doesn't like to write zlps.
2003 */
2004 req->zero = 1;
2005 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2006 length++;
2007
2008 req->length = length;
2009
2010 /* throttle highspeed IRQ rate back slightly */
2011 if (gadget_is_dualspeed(dev->gadget))
2012 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2013 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
2014 : 0;
2015
2016 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2017 switch (retval) {
2018 default:
2019 DEBUG (dev, "tx queue err %d\n", retval);
2020 break;
2021 case 0:
2022 net->trans_start = jiffies;
2023 atomic_inc (&dev->tx_qlen);
2024 }
2025
2026 if (retval) {
2027drop:
2028 dev->stats.tx_dropped++;
2029 dev_kfree_skb_any (skb);
2030 spin_lock_irqsave(&dev->req_lock, flags);
2031 if (list_empty (&dev->tx_reqs))
2032 netif_start_queue (net);
2033 list_add (&req->list, &dev->tx_reqs);
2034 spin_unlock_irqrestore(&dev->req_lock, flags);
2035 }
2036 return 0;
2037}
2038
2039/*-------------------------------------------------------------------------*/
2040
2041#ifdef CONFIG_USB_ETH_RNDIS
2042
2043/* The interrupt endpoint is used in RNDIS to notify the host when messages
2044 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2045 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2046 * REMOTE_NDIS_KEEPALIVE_MSG.
2047 *
2048 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2049 * normally just one notification will be queued.
2050 */
2051
2052static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2053static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2054
2055static void
2056rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2057{
2058 struct eth_dev *dev = ep->driver_data;
2059
2060 if (req->status || req->actual != req->length)
2061 DEBUG (dev,
2062 "rndis control ack complete --> %d, %d/%d\n",
2063 req->status, req->actual, req->length);
2064 req->context = NULL;
2065
2066 if (req != dev->stat_req)
2067 eth_req_free(ep, req);
2068}
2069
2070static int rndis_control_ack (struct net_device *net)
2071{
2072 struct eth_dev *dev = netdev_priv(net);
2073 int length;
2074 struct usb_request *resp = dev->stat_req;
2075
2076 /* in case RNDIS calls this after disconnect */
2077 if (!dev->status) {
2078 DEBUG (dev, "status ENODEV\n");
2079 return -ENODEV;
2080 }
2081
2082 /* in case queue length > 1 */
2083 if (resp->context) {
2084 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2085 if (!resp)
2086 return -ENOMEM;
2087 }
2088
2089 /* Send RNDIS RESPONSE_AVAILABLE notification;
2090 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2091 */
2092 resp->length = 8;
2093 resp->complete = rndis_control_ack_complete;
2094 resp->context = dev;
2095
2096 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2097 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2098
2099 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2100 if (length < 0) {
2101 resp->status = 0;
2102 rndis_control_ack_complete (dev->status_ep, resp);
2103 }
2104
2105 return 0;
2106}
2107
2108#else
2109
2110#define rndis_control_ack NULL
2111
2112#endif /* RNDIS */
2113
2114static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2115{
2116 DEBUG (dev, "%s\n", __func__);
2117
2118 /* fill the rx queue */
2119 rx_fill (dev, gfp_flags);
2120
2121 /* and open the tx floodgates */
2122 atomic_set (&dev->tx_qlen, 0);
2123 netif_wake_queue (dev->net);
2124 if (rndis_active(dev)) {
2125 rndis_set_param_medium (dev->rndis_config,
2126 NDIS_MEDIUM_802_3,
2127 BITRATE(dev->gadget)/100);
2128 (void) rndis_signal_connect (dev->rndis_config);
2129 }
2130}
2131
2132static int eth_open (struct net_device *net)
2133{
2134 struct eth_dev *dev = netdev_priv(net);
2135
2136 DEBUG (dev, "%s\n", __func__);
2137 if (netif_carrier_ok (dev->net))
2138 eth_start (dev, GFP_KERNEL);
2139 return 0;
2140} 244}
2141 245
2142static int eth_stop (struct net_device *net) 246static struct usb_configuration eth_config_driver = {
2143{ 247 /* .label = f(hardware) */
2144 struct eth_dev *dev = netdev_priv(net); 248 .bind = eth_do_config,
2145 249 .bConfigurationValue = 1,
2146 VDEBUG (dev, "%s\n", __func__); 250 /* .iConfiguration = DYNAMIC */
2147 netif_stop_queue (net); 251 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
2148 252 .bMaxPower = 1, /* 2 mA, minimal */
2149 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", 253};
2150 dev->stats.rx_packets, dev->stats.tx_packets,
2151 dev->stats.rx_errors, dev->stats.tx_errors
2152 );
2153
2154 /* ensure there are no more active requests */
2155 if (dev->config) {
2156 usb_ep_disable (dev->in_ep);
2157 usb_ep_disable (dev->out_ep);
2158 if (netif_carrier_ok (dev->net)) {
2159 DEBUG (dev, "host still using in/out endpoints\n");
2160 // FIXME idiom may leave toggle wrong here
2161 usb_ep_enable (dev->in_ep, dev->in);
2162 usb_ep_enable (dev->out_ep, dev->out);
2163 }
2164 if (dev->status_ep) {
2165 usb_ep_disable (dev->status_ep);
2166 usb_ep_enable (dev->status_ep, dev->status);
2167 }
2168 }
2169
2170 if (rndis_active(dev)) {
2171 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
2172 (void) rndis_signal_disconnect (dev->rndis_config);
2173 }
2174
2175 return 0;
2176}
2177 254
2178/*-------------------------------------------------------------------------*/ 255/*-------------------------------------------------------------------------*/
2179 256
2180static struct usb_request * 257static int __init eth_bind(struct usb_composite_dev *cdev)
2181eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2182{ 258{
2183 struct usb_request *req; 259 int gcnum;
2184 260 struct usb_gadget *gadget = cdev->gadget;
2185 req = usb_ep_alloc_request (ep, gfp_flags); 261 int status;
2186 if (!req)
2187 return NULL;
2188
2189 req->buf = kmalloc (size, gfp_flags);
2190 if (!req->buf) {
2191 usb_ep_free_request (ep, req);
2192 req = NULL;
2193 }
2194 return req;
2195}
2196
2197static void
2198eth_req_free (struct usb_ep *ep, struct usb_request *req)
2199{
2200 kfree (req->buf);
2201 usb_ep_free_request (ep, req);
2202}
2203
2204
2205static void /* __init_or_exit */
2206eth_unbind (struct usb_gadget *gadget)
2207{
2208 struct eth_dev *dev = get_gadget_data (gadget);
2209
2210 DEBUG (dev, "unbind\n");
2211 rndis_deregister (dev->rndis_config);
2212 rndis_exit ();
2213
2214 /* we've already been disconnected ... no i/o is active */
2215 if (dev->req) {
2216 eth_req_free (gadget->ep0, dev->req);
2217 dev->req = NULL;
2218 }
2219 if (dev->stat_req) {
2220 eth_req_free (dev->status_ep, dev->stat_req);
2221 dev->stat_req = NULL;
2222 }
2223
2224 unregister_netdev (dev->net);
2225 free_netdev(dev->net);
2226
2227 /* assuming we used keventd, it must quiesce too */
2228 flush_scheduled_work ();
2229 set_gadget_data (gadget, NULL);
2230}
2231
2232static u8 __init nibble (unsigned char c)
2233{
2234 if (likely (isdigit (c)))
2235 return c - '0';
2236 c = toupper (c);
2237 if (likely (isxdigit (c)))
2238 return 10 + c - 'A';
2239 return 0;
2240}
2241 262
2242static int __init get_ether_addr(const char *str, u8 *dev_addr) 263 /* set up network link layer */
2243{ 264 status = gether_setup(cdev->gadget, hostaddr);
2244 if (str) { 265 if (status < 0)
2245 unsigned i; 266 return status;
2246 267
2247 for (i = 0; i < 6; i++) { 268 /* set up main config label and device descriptor */
2248 unsigned char num; 269 if (can_support_ecm(cdev->gadget)) {
270 /* ECM */
271 eth_config_driver.label = "CDC Ethernet (ECM)";
272 } else {
273 /* CDC Subset */
274 eth_config_driver.label = "CDC Subset/SAFE";
2249 275
2250 if((*str == '.') || (*str == ':')) 276 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM),
2251 str++; 277 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM),
2252 num = nibble(*str++) << 4; 278 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2253 num |= (nibble(*str++));
2254 dev_addr [i] = num;
2255 }
2256 if (is_valid_ether_addr (dev_addr))
2257 return 0;
2258 } 279 }
2259 random_ether_addr(dev_addr);
2260 return 1;
2261}
2262
2263static int __init
2264eth_bind (struct usb_gadget *gadget)
2265{
2266 struct eth_dev *dev;
2267 struct net_device *net;
2268 u8 cdc = 1, zlp = 1, rndis = 1;
2269 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2270 int status = -ENOMEM;
2271 int gcnum;
2272
2273 /* these flags are only ever cleared; compiler take note */
2274#ifndef DEV_CONFIG_CDC
2275 cdc = 0;
2276#endif
2277#ifndef CONFIG_USB_ETH_RNDIS
2278 rndis = 0;
2279#endif
2280 280
2281 /* Because most host side USB stacks handle CDC Ethernet, that 281 if (has_rndis()) {
2282 * standard protocol is _strongly_ preferred for interop purposes. 282 /* RNDIS plus ECM-or-Subset */
2283 * (By everyone except Microsoft.) 283 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM),
2284 */ 284 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM),
2285 if (gadget_is_pxa (gadget)) { 285 device_desc.bNumConfigurations = 2;
2286 /* pxa doesn't support altsettings */
2287 cdc = 0;
2288 } else if (gadget_is_musbhdrc(gadget)) {
2289 /* reduce tx dma overhead by avoiding special cases */
2290 zlp = 0;
2291 } else if (gadget_is_sh(gadget)) {
2292 /* sh doesn't support multiple interfaces or configs */
2293 cdc = 0;
2294 rndis = 0;
2295 } else if (gadget_is_sa1100 (gadget)) {
2296 /* hardware can't write zlps */
2297 zlp = 0;
2298 /* sa1100 CAN do CDC, without status endpoint ... we use
2299 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2300 */
2301 cdc = 0;
2302 } 286 }
2303 287
2304 gcnum = usb_gadget_controller_number (gadget); 288 gcnum = usb_gadget_controller_number(gadget);
2305 if (gcnum >= 0) 289 if (gcnum >= 0)
2306 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum); 290 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
2307 else { 291 else {
2308 /* can't assume CDC works. don't want to default to 292 /* We assume that can_support_ecm() tells the truth;
2309 * anything less functional on CDC-capable hardware, 293 * but if the controller isn't recognized at all then
2310 * so we fail in this case. 294 * that assumption is a bit more likely to be wrong.
2311 */ 295 */
2312 dev_err (&gadget->dev, 296 WARN(cdev, "controller '%s' not recognized; trying %s\n",
2313 "controller '%s' not recognized\n", 297 gadget->name,
2314 gadget->name); 298 eth_config_driver.label);
2315 return -ENODEV; 299 device_desc.bcdDevice =
2316 } 300 __constant_cpu_to_le16(0x0300 | 0x0099);
2317 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2318 init_utsname()->sysname, init_utsname()->release,
2319 gadget->name);
2320
2321 /* If there's an RNDIS configuration, that's what Windows wants to
2322 * be using ... so use these product IDs here and in the "linux.inf"
2323 * needed to install MSFT drivers. Current Linux kernels will use
2324 * the second configuration if it's CDC Ethernet, and need some help
2325 * to choose the right configuration otherwise.
2326 */
2327 if (rndis) {
2328 device_desc.idVendor =
2329 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2330 device_desc.idProduct =
2331 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2332 snprintf (product_desc, sizeof product_desc,
2333 "RNDIS/%s", driver_desc);
2334
2335 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2336 * drivers aren't widely available. (That may be improved by
2337 * supporting one submode of the "SAFE" variant of MDLM.)
2338 */
2339 } else if (!cdc) {
2340 device_desc.idVendor =
2341 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2342 device_desc.idProduct =
2343 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2344 }
2345
2346 /* support optional vendor/distro customization */
2347 if (idVendor) {
2348 if (!idProduct) {
2349 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2350 return -ENODEV;
2351 }
2352 device_desc.idVendor = cpu_to_le16(idVendor);
2353 device_desc.idProduct = cpu_to_le16(idProduct);
2354 if (bcdDevice)
2355 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2356 }
2357 if (iManufacturer)
2358 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2359 if (iProduct)
2360 strlcpy (product_desc, iProduct, sizeof product_desc);
2361 if (iSerialNumber) {
2362 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2363 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2364 }
2365
2366 /* all we really need is bulk IN/OUT */
2367 usb_ep_autoconfig_reset (gadget);
2368 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2369 if (!in_ep) {
2370autoconf_fail:
2371 dev_err (&gadget->dev,
2372 "can't autoconfigure on %s\n",
2373 gadget->name);
2374 return -ENODEV;
2375 }
2376 in_ep->driver_data = in_ep; /* claim */
2377
2378 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2379 if (!out_ep)
2380 goto autoconf_fail;
2381 out_ep->driver_data = out_ep; /* claim */
2382
2383#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2384 /* CDC Ethernet control interface doesn't require a status endpoint.
2385 * Since some hosts expect one, try to allocate one anyway.
2386 */
2387 if (cdc || rndis) {
2388 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2389 if (status_ep) {
2390 status_ep->driver_data = status_ep; /* claim */
2391 } else if (rndis) {
2392 dev_err (&gadget->dev,
2393 "can't run RNDIS on %s\n",
2394 gadget->name);
2395 return -ENODEV;
2396#ifdef DEV_CONFIG_CDC
2397 /* pxa25x only does CDC subset; often used with RNDIS */
2398 } else if (cdc) {
2399 control_intf.bNumEndpoints = 0;
2400 /* FIXME remove endpoint from descriptor list */
2401#endif
2402 }
2403 }
2404#endif
2405
2406 /* one config: cdc, else minimal subset */
2407 if (!cdc) {
2408 eth_config.bNumInterfaces = 1;
2409 eth_config.iConfiguration = STRING_SUBSET;
2410
2411 /* use functions to set these up, in case we're built to work
2412 * with multiple controllers and must override CDC Ethernet.
2413 */
2414 fs_subset_descriptors();
2415 hs_subset_descriptors();
2416 }
2417
2418 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2419 usb_gadget_set_selfpowered (gadget);
2420
2421 /* For now RNDIS is always a second config */
2422 if (rndis)
2423 device_desc.bNumConfigurations = 2;
2424
2425 if (gadget_is_dualspeed(gadget)) {
2426 if (rndis)
2427 dev_qualifier.bNumConfigurations = 2;
2428 else if (!cdc)
2429 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2430
2431 /* assumes ep0 uses the same value for both speeds ... */
2432 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2433
2434 /* and that all endpoints are dual-speed */
2435 hs_source_desc.bEndpointAddress =
2436 fs_source_desc.bEndpointAddress;
2437 hs_sink_desc.bEndpointAddress =
2438 fs_sink_desc.bEndpointAddress;
2439#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2440 if (status_ep)
2441 hs_status_desc.bEndpointAddress =
2442 fs_status_desc.bEndpointAddress;
2443#endif
2444 } 301 }
2445 302
2446 if (gadget_is_otg(gadget)) {
2447 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2448 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2449 eth_config.bMaxPower = 4;
2450#ifdef CONFIG_USB_ETH_RNDIS
2451 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2452 rndis_config.bMaxPower = 4;
2453#endif
2454 }
2455
2456 net = alloc_etherdev (sizeof *dev);
2457 if (!net)
2458 return status;
2459 dev = netdev_priv(net);
2460 spin_lock_init (&dev->lock);
2461 spin_lock_init (&dev->req_lock);
2462 INIT_WORK (&dev->work, eth_work);
2463 INIT_LIST_HEAD (&dev->tx_reqs);
2464 INIT_LIST_HEAD (&dev->rx_reqs);
2465
2466 /* network device setup */
2467 dev->net = net;
2468 strcpy (net->name, "usb%d");
2469 dev->cdc = cdc;
2470 dev->zlp = zlp;
2471 303
2472 dev->in_ep = in_ep; 304 /* Allocate string descriptor numbers ... note that string
2473 dev->out_ep = out_ep; 305 * contents can be overridden by the composite_dev glue.
2474 dev->status_ep = status_ep;
2475
2476 /* Module params for these addresses should come from ID proms.
2477 * The host side address is used with CDC and RNDIS, and commonly
2478 * ends up in a persistent config database. It's not clear if
2479 * host side code for the SAFE thing cares -- its original BLAN
2480 * thing didn't, Sharp never assigned those addresses on Zaurii.
2481 */ 306 */
2482 if (get_ether_addr(dev_addr, net->dev_addr))
2483 dev_warn(&gadget->dev,
2484 "using random %s ethernet address\n", "self");
2485 if (get_ether_addr(host_addr, dev->host_mac))
2486 dev_warn(&gadget->dev,
2487 "using random %s ethernet address\n", "host");
2488 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2489 dev->host_mac [0], dev->host_mac [1],
2490 dev->host_mac [2], dev->host_mac [3],
2491 dev->host_mac [4], dev->host_mac [5]);
2492
2493 if (rndis) {
2494 status = rndis_init();
2495 if (status < 0) {
2496 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2497 status);
2498 goto fail;
2499 }
2500 }
2501 307
2502 net->change_mtu = eth_change_mtu; 308 /* device descriptor strings: manufacturer, product */
2503 net->get_stats = eth_get_stats; 309 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
2504 net->hard_start_xmit = eth_start_xmit; 310 init_utsname()->sysname, init_utsname()->release,
2505 net->open = eth_open; 311 gadget->name);
2506 net->stop = eth_stop; 312 status = usb_string_id(cdev);
2507 // watchdog_timeo, tx_timeout ... 313 if (status < 0)
2508 // set_multicast_list 314 goto fail;
2509 SET_ETHTOOL_OPS(net, &ops); 315 strings_dev[STRING_MANUFACTURER_IDX].id = status;
316 device_desc.iManufacturer = status;
2510 317
2511 /* preallocate control message data and buffer */ 318 status = usb_string_id(cdev);
2512 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL); 319 if (status < 0)
2513 if (!dev->req)
2514 goto fail; 320 goto fail;
2515 dev->req->complete = eth_setup_complete; 321 strings_dev[STRING_PRODUCT_IDX].id = status;
322 device_desc.iProduct = status;
2516 323
2517 /* ... and maybe likewise for status transfer */ 324 /* register our configuration(s); RNDIS first, if it's used */
2518#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 325 if (has_rndis()) {
2519 if (dev->status_ep) { 326 status = usb_add_config(cdev, &rndis_config_driver);
2520 dev->stat_req = eth_req_alloc (dev->status_ep, 327 if (status < 0)
2521 STATUS_BYTECOUNT, GFP_KERNEL);
2522 if (!dev->stat_req) {
2523 eth_req_free (gadget->ep0, dev->req);
2524 goto fail; 328 goto fail;
2525 }
2526 dev->stat_req->context = NULL;
2527 } 329 }
2528#endif
2529
2530 /* finish hookup to lower layer ... */
2531 dev->gadget = gadget;
2532 set_gadget_data (gadget, dev);
2533 gadget->ep0->driver_data = dev;
2534 330
2535 /* two kinds of host-initiated state changes: 331 status = usb_add_config(cdev, &eth_config_driver);
2536 * - iff DATA transfer is active, carrier is "on"
2537 * - tx queueing enabled if open *and* carrier is "on"
2538 */
2539 netif_stop_queue (dev->net);
2540 netif_carrier_off (dev->net);
2541
2542 SET_NETDEV_DEV (dev->net, &gadget->dev);
2543 status = register_netdev (dev->net);
2544 if (status < 0) 332 if (status < 0)
2545 goto fail1; 333 goto fail;
2546
2547 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2548 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2549 out_ep->name, in_ep->name,
2550 status_ep ? " STATUS " : "",
2551 status_ep ? status_ep->name : ""
2552 );
2553 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2554 net->dev_addr [0], net->dev_addr [1],
2555 net->dev_addr [2], net->dev_addr [3],
2556 net->dev_addr [4], net->dev_addr [5]);
2557
2558 if (cdc || rndis)
2559 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2560 dev->host_mac [0], dev->host_mac [1],
2561 dev->host_mac [2], dev->host_mac [3],
2562 dev->host_mac [4], dev->host_mac [5]);
2563
2564 if (rndis) {
2565 u32 vendorID = 0;
2566
2567 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2568
2569 dev->rndis_config = rndis_register (rndis_control_ack);
2570 if (dev->rndis_config < 0) {
2571fail0:
2572 unregister_netdev (dev->net);
2573 status = -ENODEV;
2574 goto fail;
2575 }
2576 334
2577 /* these set up a lot of the OIDs that RNDIS needs */ 335 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC);
2578 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2579 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2580 &dev->stats, &dev->cdc_filter))
2581 goto fail0;
2582 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2583 manufacturer))
2584 goto fail0;
2585 if (rndis_set_param_medium(dev->rndis_config,
2586 NDIS_MEDIUM_802_3, 0))
2587 goto fail0;
2588 INFO (dev, "RNDIS ready\n");
2589 }
2590 336
2591 return status; 337 return 0;
2592 338
2593fail1:
2594 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2595fail: 339fail:
2596 eth_unbind (gadget); 340 gether_cleanup();
2597 return status; 341 return status;
2598} 342}
2599 343
2600/*-------------------------------------------------------------------------*/ 344static int __exit eth_unbind(struct usb_composite_dev *cdev)
2601
2602static void
2603eth_suspend (struct usb_gadget *gadget)
2604{
2605 struct eth_dev *dev = get_gadget_data (gadget);
2606
2607 DEBUG (dev, "suspend\n");
2608 dev->suspended = 1;
2609}
2610
2611static void
2612eth_resume (struct usb_gadget *gadget)
2613{ 345{
2614 struct eth_dev *dev = get_gadget_data (gadget); 346 gether_cleanup();
2615 347 return 0;
2616 DEBUG (dev, "resume\n");
2617 dev->suspended = 0;
2618} 348}
2619 349
2620/*-------------------------------------------------------------------------*/ 350static struct usb_composite_driver eth_driver = {
2621 351 .name = "g_ether",
2622static struct usb_gadget_driver eth_driver = { 352 .dev = &device_desc,
2623 .speed = DEVSPEED, 353 .strings = dev_strings,
2624
2625 .function = (char *) driver_desc,
2626 .bind = eth_bind, 354 .bind = eth_bind,
2627 .unbind = eth_unbind, 355 .unbind = __exit_p(eth_unbind),
2628
2629 .setup = eth_setup,
2630 .disconnect = eth_disconnect,
2631
2632 .suspend = eth_suspend,
2633 .resume = eth_resume,
2634
2635 .driver = {
2636 .name = (char *) shortname,
2637 .owner = THIS_MODULE,
2638 },
2639}; 356};
2640 357
2641MODULE_DESCRIPTION (DRIVER_DESC); 358MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
2642MODULE_AUTHOR ("David Brownell, Benedikt Spanger"); 359MODULE_AUTHOR("David Brownell, Benedikt Spanger");
2643MODULE_LICENSE ("GPL"); 360MODULE_LICENSE("GPL");
2644 361
2645 362static int __init init(void)
2646static int __init init (void)
2647{ 363{
2648 return usb_gadget_register_driver (&eth_driver); 364 return usb_composite_register(&eth_driver);
2649} 365}
2650module_init (init); 366module_init(init);
2651 367
2652static void __exit cleanup (void) 368static void __exit cleanup(void)
2653{ 369{
2654 usb_gadget_unregister_driver (&eth_driver); 370 usb_composite_unregister(&eth_driver);
2655} 371}
2656module_exit (cleanup); 372module_exit(cleanup);
2657
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
new file mode 100644
index 000000000000..d8faccf27895
--- /dev/null
+++ b/drivers/usb/gadget/f_acm.c
@@ -0,0 +1,589 @@
1/*
2 * f_acm.c -- USB CDC serial (ACM) function driver
3 *
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
7 *
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
11 */
12
13/* #define VERBOSE_DEBUG */
14
15#include <linux/kernel.h>
16#include <linux/device.h>
17
18#include "u_serial.h"
19#include "gadget_chips.h"
20
21
22/*
23 * This CDC ACM function support just wraps control functions and
24 * notifications around the generic serial-over-usb code.
25 *
26 * Because CDC ACM is standardized by the USB-IF, many host operating
27 * systems have drivers for it. Accordingly, ACM is the preferred
28 * interop solution for serial-port type connections. The control
29 * models are often not necessary, and in any case don't do much in
30 * this bare-bones implementation.
31 *
32 * Note that even MS-Windows has some support for ACM. However, that
33 * support is somewhat broken because when you use ACM in a composite
34 * device, having multiple interfaces confuses the poor OS. It doesn't
35 * seem to understand CDC Union descriptors. The new "association"
36 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
37 */
38
39struct acm_ep_descs {
40 struct usb_endpoint_descriptor *in;
41 struct usb_endpoint_descriptor *out;
42 struct usb_endpoint_descriptor *notify;
43};
44
45struct f_acm {
46 struct gserial port;
47 u8 ctrl_id, data_id;
48 u8 port_num;
49
50 struct usb_descriptor_header **fs_function;
51 struct acm_ep_descs fs;
52 struct usb_descriptor_header **hs_function;
53 struct acm_ep_descs hs;
54
55 struct usb_ep *notify;
56 struct usb_endpoint_descriptor *notify_desc;
57
58 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
59 u16 port_handshake_bits;
60#define RS232_RTS (1 << 1) /* unused with full duplex */
61#define RS232_DTR (1 << 0) /* host is ready for data r/w */
62};
63
64static inline struct f_acm *func_to_acm(struct usb_function *f)
65{
66 return container_of(f, struct f_acm, port.func);
67}
68
69/*-------------------------------------------------------------------------*/
70
71/* notification endpoint uses smallish and infrequent fixed-size messages */
72
73#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
74#define GS_NOTIFY_MAXPACKET 8
75
76/* interface and class descriptors: */
77
78static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
79 .bLength = USB_DT_INTERFACE_SIZE,
80 .bDescriptorType = USB_DT_INTERFACE,
81 /* .bInterfaceNumber = DYNAMIC */
82 .bNumEndpoints = 1,
83 .bInterfaceClass = USB_CLASS_COMM,
84 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
85 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
86 /* .iInterface = DYNAMIC */
87};
88
89static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
90 .bLength = USB_DT_INTERFACE_SIZE,
91 .bDescriptorType = USB_DT_INTERFACE,
92 /* .bInterfaceNumber = DYNAMIC */
93 .bNumEndpoints = 2,
94 .bInterfaceClass = USB_CLASS_CDC_DATA,
95 .bInterfaceSubClass = 0,
96 .bInterfaceProtocol = 0,
97 /* .iInterface = DYNAMIC */
98};
99
100static struct usb_cdc_header_desc acm_header_desc __initdata = {
101 .bLength = sizeof(acm_header_desc),
102 .bDescriptorType = USB_DT_CS_INTERFACE,
103 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
104 .bcdCDC = __constant_cpu_to_le16(0x0110),
105};
106
107static struct usb_cdc_call_mgmt_descriptor
108acm_call_mgmt_descriptor __initdata = {
109 .bLength = sizeof(acm_call_mgmt_descriptor),
110 .bDescriptorType = USB_DT_CS_INTERFACE,
111 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
112 .bmCapabilities = 0,
113 /* .bDataInterface = DYNAMIC */
114};
115
116static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
117 .bLength = sizeof(acm_descriptor),
118 .bDescriptorType = USB_DT_CS_INTERFACE,
119 .bDescriptorSubType = USB_CDC_ACM_TYPE,
120 .bmCapabilities = (1 << 1),
121};
122
123static struct usb_cdc_union_desc acm_union_desc __initdata = {
124 .bLength = sizeof(acm_union_desc),
125 .bDescriptorType = USB_DT_CS_INTERFACE,
126 .bDescriptorSubType = USB_CDC_UNION_TYPE,
127 /* .bMasterInterface0 = DYNAMIC */
128 /* .bSlaveInterface0 = DYNAMIC */
129};
130
131/* full speed support: */
132
133static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_INT,
138 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
139 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
140};
141
142static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = {
143 .bLength = USB_DT_ENDPOINT_SIZE,
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147};
148
149static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_OUT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154};
155
156static struct usb_descriptor_header *acm_fs_function[] __initdata = {
157 (struct usb_descriptor_header *) &acm_control_interface_desc,
158 (struct usb_descriptor_header *) &acm_header_desc,
159 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
160 (struct usb_descriptor_header *) &acm_descriptor,
161 (struct usb_descriptor_header *) &acm_union_desc,
162 (struct usb_descriptor_header *) &acm_fs_notify_desc,
163 (struct usb_descriptor_header *) &acm_data_interface_desc,
164 (struct usb_descriptor_header *) &acm_fs_in_desc,
165 (struct usb_descriptor_header *) &acm_fs_out_desc,
166 NULL,
167};
168
169/* high speed support: */
170
171static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT,
174 .bEndpointAddress = USB_DIR_IN,
175 .bmAttributes = USB_ENDPOINT_XFER_INT,
176 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
177 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
178};
179
180static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = {
181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bmAttributes = USB_ENDPOINT_XFER_BULK,
184 .wMaxPacketSize = __constant_cpu_to_le16(512),
185};
186
187static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
190 .bmAttributes = USB_ENDPOINT_XFER_BULK,
191 .wMaxPacketSize = __constant_cpu_to_le16(512),
192};
193
194static struct usb_descriptor_header *acm_hs_function[] __initdata = {
195 (struct usb_descriptor_header *) &acm_control_interface_desc,
196 (struct usb_descriptor_header *) &acm_header_desc,
197 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
198 (struct usb_descriptor_header *) &acm_descriptor,
199 (struct usb_descriptor_header *) &acm_union_desc,
200 (struct usb_descriptor_header *) &acm_hs_notify_desc,
201 (struct usb_descriptor_header *) &acm_data_interface_desc,
202 (struct usb_descriptor_header *) &acm_hs_in_desc,
203 (struct usb_descriptor_header *) &acm_hs_out_desc,
204 NULL,
205};
206
207/* string descriptors: */
208
209#define ACM_CTRL_IDX 0
210#define ACM_DATA_IDX 1
211
212/* static strings, in UTF-8 */
213static struct usb_string acm_string_defs[] = {
214 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
215 [ACM_DATA_IDX].s = "CDC ACM Data",
216 { /* ZEROES END LIST */ },
217};
218
219static struct usb_gadget_strings acm_string_table = {
220 .language = 0x0409, /* en-us */
221 .strings = acm_string_defs,
222};
223
224static struct usb_gadget_strings *acm_strings[] = {
225 &acm_string_table,
226 NULL,
227};
228
229/*-------------------------------------------------------------------------*/
230
231/* ACM control ... data handling is delegated to tty library code.
232 * The main task of this function is to activate and deactivate
233 * that code based on device state; track parameters like line
234 * speed, handshake state, and so on; and issue notifications.
235 */
236
237static void acm_complete_set_line_coding(struct usb_ep *ep,
238 struct usb_request *req)
239{
240 struct f_acm *acm = ep->driver_data;
241 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
242
243 if (req->status != 0) {
244 DBG(cdev, "acm ttyGS%d completion, err %d\n",
245 acm->port_num, req->status);
246 return;
247 }
248
249 /* normal completion */
250 if (req->actual != sizeof(acm->port_line_coding)) {
251 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
252 acm->port_num, req->actual);
253 usb_ep_set_halt(ep);
254 } else {
255 struct usb_cdc_line_coding *value = req->buf;
256
257 /* REVISIT: we currently just remember this data.
258 * If we change that, (a) validate it first, then
259 * (b) update whatever hardware needs updating,
260 * (c) worry about locking. This is information on
261 * the order of 9600-8-N-1 ... most of which means
262 * nothing unless we control a real RS232 line.
263 */
264 acm->port_line_coding = *value;
265 }
266}
267
268static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
269{
270 struct f_acm *acm = func_to_acm(f);
271 struct usb_composite_dev *cdev = f->config->cdev;
272 struct usb_request *req = cdev->req;
273 int value = -EOPNOTSUPP;
274 u16 w_index = le16_to_cpu(ctrl->wIndex);
275 u16 w_value = le16_to_cpu(ctrl->wValue);
276 u16 w_length = le16_to_cpu(ctrl->wLength);
277
278 /* composite driver infrastructure handles everything except
279 * CDC class messages; interface activation uses set_alt().
280 */
281 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
282
283 /* SET_LINE_CODING ... just read and save what the host sends */
284 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
285 | USB_CDC_REQ_SET_LINE_CODING:
286 if (w_length != sizeof(struct usb_cdc_line_coding)
287 || w_index != acm->ctrl_id)
288 goto invalid;
289
290 value = w_length;
291 cdev->gadget->ep0->driver_data = acm;
292 req->complete = acm_complete_set_line_coding;
293 break;
294
295 /* GET_LINE_CODING ... return what host sent, or initial value */
296 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
297 | USB_CDC_REQ_GET_LINE_CODING:
298 if (w_index != acm->ctrl_id)
299 goto invalid;
300
301 value = min_t(unsigned, w_length,
302 sizeof(struct usb_cdc_line_coding));
303 memcpy(req->buf, &acm->port_line_coding, value);
304 break;
305
306 /* SET_CONTROL_LINE_STATE ... save what the host sent */
307 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
308 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
309 if (w_index != acm->ctrl_id)
310 goto invalid;
311
312 value = 0;
313
314 /* FIXME we should not allow data to flow until the
315 * host sets the RS232_DTR bit; and when it clears
316 * that bit, we should return to that no-flow state.
317 */
318 acm->port_handshake_bits = w_value;
319 break;
320
321 default:
322invalid:
323 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
324 ctrl->bRequestType, ctrl->bRequest,
325 w_value, w_index, w_length);
326 }
327
328 /* respond with data transfer or status phase? */
329 if (value >= 0) {
330 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
331 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
332 w_value, w_index, w_length);
333 req->zero = 0;
334 req->length = value;
335 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
336 if (value < 0)
337 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
338 acm->port_num, value);
339 }
340
341 /* device either stalls (value < 0) or reports success */
342 return value;
343}
344
345static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
346{
347 struct f_acm *acm = func_to_acm(f);
348 struct usb_composite_dev *cdev = f->config->cdev;
349
350 /* we know alt == 0, so this is an activation or a reset */
351
352 if (intf == acm->ctrl_id) {
353 /* REVISIT this may need more work when we start to
354 * send notifications ...
355 */
356 if (acm->notify->driver_data) {
357 VDBG(cdev, "reset acm control interface %d\n", intf);
358 usb_ep_disable(acm->notify);
359 } else {
360 VDBG(cdev, "init acm ctrl interface %d\n", intf);
361 acm->notify_desc = ep_choose(cdev->gadget,
362 acm->hs.notify,
363 acm->fs.notify);
364 }
365 usb_ep_enable(acm->notify, acm->notify_desc);
366 acm->notify->driver_data = acm;
367
368 } else if (intf == acm->data_id) {
369 if (acm->port.in->driver_data) {
370 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
371 gserial_disconnect(&acm->port);
372 } else {
373 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
374 acm->port.in_desc = ep_choose(cdev->gadget,
375 acm->hs.in, acm->fs.in);
376 acm->port.out_desc = ep_choose(cdev->gadget,
377 acm->hs.out, acm->fs.out);
378 }
379 gserial_connect(&acm->port, acm->port_num);
380
381 } else
382 return -EINVAL;
383
384 return 0;
385}
386
387static void acm_disable(struct usb_function *f)
388{
389 struct f_acm *acm = func_to_acm(f);
390 struct usb_composite_dev *cdev = f->config->cdev;
391
392 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
393 gserial_disconnect(&acm->port);
394 usb_ep_disable(acm->notify);
395 acm->notify->driver_data = NULL;
396}
397
398/*-------------------------------------------------------------------------*/
399
400/* ACM function driver setup/binding */
401static int __init
402acm_bind(struct usb_configuration *c, struct usb_function *f)
403{
404 struct usb_composite_dev *cdev = c->cdev;
405 struct f_acm *acm = func_to_acm(f);
406 int status;
407 struct usb_ep *ep;
408
409 /* allocate instance-specific interface IDs, and patch descriptors */
410 status = usb_interface_id(c, f);
411 if (status < 0)
412 goto fail;
413 acm->ctrl_id = status;
414
415 acm_control_interface_desc.bInterfaceNumber = status;
416 acm_union_desc .bMasterInterface0 = status;
417
418 status = usb_interface_id(c, f);
419 if (status < 0)
420 goto fail;
421 acm->data_id = status;
422
423 acm_data_interface_desc.bInterfaceNumber = status;
424 acm_union_desc.bSlaveInterface0 = status;
425 acm_call_mgmt_descriptor.bDataInterface = status;
426
427 status = -ENODEV;
428
429 /* allocate instance-specific endpoints */
430 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
431 if (!ep)
432 goto fail;
433 acm->port.in = ep;
434 ep->driver_data = cdev; /* claim */
435
436 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
437 if (!ep)
438 goto fail;
439 acm->port.out = ep;
440 ep->driver_data = cdev; /* claim */
441
442 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
443 if (!ep)
444 goto fail;
445 acm->notify = ep;
446 ep->driver_data = cdev; /* claim */
447
448 /* copy descriptors, and track endpoint copies */
449 f->descriptors = usb_copy_descriptors(acm_fs_function);
450
451 acm->fs.in = usb_find_endpoint(acm_fs_function,
452 f->descriptors, &acm_fs_in_desc);
453 acm->fs.out = usb_find_endpoint(acm_fs_function,
454 f->descriptors, &acm_fs_out_desc);
455 acm->fs.notify = usb_find_endpoint(acm_fs_function,
456 f->descriptors, &acm_fs_notify_desc);
457
458 /* support all relevant hardware speeds... we expect that when
459 * hardware is dual speed, all bulk-capable endpoints work at
460 * both speeds
461 */
462 if (gadget_is_dualspeed(c->cdev->gadget)) {
463 acm_hs_in_desc.bEndpointAddress =
464 acm_fs_in_desc.bEndpointAddress;
465 acm_hs_out_desc.bEndpointAddress =
466 acm_fs_out_desc.bEndpointAddress;
467 acm_hs_notify_desc.bEndpointAddress =
468 acm_fs_notify_desc.bEndpointAddress;
469
470 /* copy descriptors, and track endpoint copies */
471 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
472
473 acm->hs.in = usb_find_endpoint(acm_hs_function,
474 f->hs_descriptors, &acm_hs_in_desc);
475 acm->hs.out = usb_find_endpoint(acm_hs_function,
476 f->hs_descriptors, &acm_hs_out_desc);
477 acm->hs.notify = usb_find_endpoint(acm_hs_function,
478 f->hs_descriptors, &acm_hs_notify_desc);
479 }
480
481 /* FIXME provide a callback for triggering notifications */
482
483 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
484 acm->port_num,
485 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
486 acm->port.in->name, acm->port.out->name,
487 acm->notify->name);
488 return 0;
489
490fail:
491 /* we might as well release our claims on endpoints */
492 if (acm->notify)
493 acm->notify->driver_data = NULL;
494 if (acm->port.out)
495 acm->port.out->driver_data = NULL;
496 if (acm->port.in)
497 acm->port.in->driver_data = NULL;
498
499 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
500
501 return status;
502}
503
504static void
505acm_unbind(struct usb_configuration *c, struct usb_function *f)
506{
507 if (gadget_is_dualspeed(c->cdev->gadget))
508 usb_free_descriptors(f->hs_descriptors);
509 usb_free_descriptors(f->descriptors);
510 kfree(func_to_acm(f));
511}
512
513/* Some controllers can't support CDC ACM ... */
514static inline bool can_support_cdc(struct usb_configuration *c)
515{
516 /* SH3 doesn't support multiple interfaces */
517 if (gadget_is_sh(c->cdev->gadget))
518 return false;
519
520 /* sa1100 doesn't have a third interrupt endpoint */
521 if (gadget_is_sa1100(c->cdev->gadget))
522 return false;
523
524 /* everything else is *probably* fine ... */
525 return true;
526}
527
528/**
529 * acm_bind_config - add a CDC ACM function to a configuration
530 * @c: the configuration to support the CDC ACM instance
531 * @port_num: /dev/ttyGS* port this interface will use
532 * Context: single threaded during gadget setup
533 *
534 * Returns zero on success, else negative errno.
535 *
536 * Caller must have called @gserial_setup() with enough ports to
537 * handle all the ones it binds. Caller is also responsible
538 * for calling @gserial_cleanup() before module unload.
539 */
540int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
541{
542 struct f_acm *acm;
543 int status;
544
545 if (!can_support_cdc(c))
546 return -EINVAL;
547
548 /* REVISIT might want instance-specific strings to help
549 * distinguish instances ...
550 */
551
552 /* maybe allocate device-global string IDs, and patch descriptors */
553 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
554 status = usb_string_id(c->cdev);
555 if (status < 0)
556 return status;
557 acm_string_defs[ACM_CTRL_IDX].id = status;
558
559 acm_control_interface_desc.iInterface = status;
560
561 status = usb_string_id(c->cdev);
562 if (status < 0)
563 return status;
564 acm_string_defs[ACM_DATA_IDX].id = status;
565
566 acm_data_interface_desc.iInterface = status;
567 }
568
569 /* allocate and initialize one new instance */
570 acm = kzalloc(sizeof *acm, GFP_KERNEL);
571 if (!acm)
572 return -ENOMEM;
573
574 acm->port_num = port_num;
575
576 acm->port.func.name = "acm";
577 acm->port.func.strings = acm_strings;
578 /* descriptors are per-instance copies */
579 acm->port.func.bind = acm_bind;
580 acm->port.func.unbind = acm_unbind;
581 acm->port.func.set_alt = acm_set_alt;
582 acm->port.func.setup = acm_setup;
583 acm->port.func.disable = acm_disable;
584
585 status = usb_add_function(c, &acm->port.func);
586 if (status)
587 kfree(acm);
588 return status;
589}
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
new file mode 100644
index 000000000000..0822e9d7693a
--- /dev/null
+++ b/drivers/usb/gadget/f_ecm.c
@@ -0,0 +1,833 @@
1/*
2 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
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 VERBOSE_DEBUG */
23
24#include <linux/kernel.h>
25#include <linux/device.h>
26#include <linux/etherdevice.h>
27
28#include "u_ether.h"
29
30
31/*
32 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
33 * Ethernet link. The data transfer model is simple (packets sent and
34 * received over bulk endpoints using normal short packet termination),
35 * and the control model exposes various data and optional notifications.
36 *
37 * ECM is well standardized and (except for Microsoft) supported by most
38 * operating systems with USB host support. It's the preferred interop
39 * solution for Ethernet over USB, at least for firmware based solutions.
40 * (Hardware solutions tend to be more minimalist.) A newer and simpler
41 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
42 *
43 * Note that ECM requires the use of "alternate settings" for its data
44 * interface. This means that the set_alt() method has real work to do,
45 * and also means that a get_alt() method is required.
46 */
47
48struct ecm_ep_descs {
49 struct usb_endpoint_descriptor *in;
50 struct usb_endpoint_descriptor *out;
51 struct usb_endpoint_descriptor *notify;
52};
53
54enum ecm_notify_state {
55 ECM_NOTIFY_NONE, /* don't notify */
56 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
57 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
58};
59
60struct f_ecm {
61 struct gether port;
62 u8 ctrl_id, data_id;
63
64 char ethaddr[14];
65
66 struct usb_descriptor_header **fs_function;
67 struct ecm_ep_descs fs;
68 struct usb_descriptor_header **hs_function;
69 struct ecm_ep_descs hs;
70
71 struct usb_ep *notify;
72 struct usb_endpoint_descriptor *notify_desc;
73 struct usb_request *notify_req;
74 u8 notify_state;
75 bool is_open;
76
77 /* FIXME is_open needs some irq-ish locking
78 * ... possibly the same as port.ioport
79 */
80};
81
82static inline struct f_ecm *func_to_ecm(struct usb_function *f)
83{
84 return container_of(f, struct f_ecm, port.func);
85}
86
87/* peak (theoretical) bulk transfer rate in bits-per-second */
88static inline unsigned bitrate(struct usb_gadget *g)
89{
90 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 return 13 * 512 * 8 * 1000 * 8;
92 else
93 return 19 * 64 * 1 * 1000 * 8;
94}
95
96/*-------------------------------------------------------------------------*/
97
98/*
99 * Include the status endpoint if we can, even though it's optional.
100 *
101 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
102 * packet, to simplify cancellation; and a big transfer interval, to
103 * waste less bandwidth.
104 *
105 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
106 * if they ignore the connect/disconnect notifications that real aether
107 * can provide. More advanced cdc configurations might want to support
108 * encapsulated commands (vendor-specific, using control-OUT).
109 */
110
111#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
112#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
113
114
115/* interface descriptor: */
116
117static struct usb_interface_descriptor ecm_control_intf __initdata = {
118 .bLength = sizeof ecm_control_intf,
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 /* .bInterfaceNumber = DYNAMIC */
122 /* status endpoint is optional; this could be patched later */
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_COMM,
125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
126 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
127 /* .iInterface = DYNAMIC */
128};
129
130static struct usb_cdc_header_desc header_desc __initdata = {
131 .bLength = sizeof header_desc,
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
134
135 .bcdCDC = __constant_cpu_to_le16(0x0110),
136};
137
138static struct usb_cdc_union_desc ecm_union_desc __initdata = {
139 .bLength = sizeof(ecm_union_desc),
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_UNION_TYPE,
142 /* .bMasterInterface0 = DYNAMIC */
143 /* .bSlaveInterface0 = DYNAMIC */
144};
145
146static struct usb_cdc_ether_desc ether_desc __initdata = {
147 .bLength = sizeof ether_desc,
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
150
151 /* this descriptor actually adds value, surprise! */
152 /* .iMACAddress = DYNAMIC */
153 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
154 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
155 .wNumberMCFilters = __constant_cpu_to_le16(0),
156 .bNumberPowerFilters = 0,
157};
158
159/* the default data interface has no endpoints ... */
160
161static struct usb_interface_descriptor ecm_data_nop_intf __initdata = {
162 .bLength = sizeof ecm_data_nop_intf,
163 .bDescriptorType = USB_DT_INTERFACE,
164
165 .bInterfaceNumber = 1,
166 .bAlternateSetting = 0,
167 .bNumEndpoints = 0,
168 .bInterfaceClass = USB_CLASS_CDC_DATA,
169 .bInterfaceSubClass = 0,
170 .bInterfaceProtocol = 0,
171 /* .iInterface = DYNAMIC */
172};
173
174/* ... but the "real" data interface has two bulk endpoints */
175
176static struct usb_interface_descriptor ecm_data_intf __initdata = {
177 .bLength = sizeof ecm_data_intf,
178 .bDescriptorType = USB_DT_INTERFACE,
179
180 .bInterfaceNumber = 1,
181 .bAlternateSetting = 1,
182 .bNumEndpoints = 2,
183 .bInterfaceClass = USB_CLASS_CDC_DATA,
184 .bInterfaceSubClass = 0,
185 .bInterfaceProtocol = 0,
186 /* .iInterface = DYNAMIC */
187};
188
189/* full speed support: */
190
191static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_INT,
197 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
198 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
199};
200
201static struct usb_endpoint_descriptor fs_in_desc __initdata = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207};
208
209static struct usb_endpoint_descriptor fs_out_desc __initdata = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
215};
216
217static struct usb_descriptor_header *eth_fs_function[] __initdata = {
218 /* CDC ECM control descriptors */
219 (struct usb_descriptor_header *) &ecm_control_intf,
220 (struct usb_descriptor_header *) &header_desc,
221 (struct usb_descriptor_header *) &ecm_union_desc,
222 (struct usb_descriptor_header *) &ether_desc,
223 /* NOTE: status endpoint might need to be removed */
224 (struct usb_descriptor_header *) &fs_notify_desc,
225 /* data interface, altsettings 0 and 1 */
226 (struct usb_descriptor_header *) &ecm_data_nop_intf,
227 (struct usb_descriptor_header *) &ecm_data_intf,
228 (struct usb_descriptor_header *) &fs_in_desc,
229 (struct usb_descriptor_header *) &fs_out_desc,
230 NULL,
231};
232
233/* high speed support: */
234
235static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
241 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
243};
244static struct usb_endpoint_descriptor hs_in_desc __initdata = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
247
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 .wMaxPacketSize = __constant_cpu_to_le16(512),
251};
252
253static struct usb_endpoint_descriptor hs_out_desc __initdata = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259 .wMaxPacketSize = __constant_cpu_to_le16(512),
260};
261
262static struct usb_descriptor_header *eth_hs_function[] __initdata = {
263 /* CDC ECM control descriptors */
264 (struct usb_descriptor_header *) &ecm_control_intf,
265 (struct usb_descriptor_header *) &header_desc,
266 (struct usb_descriptor_header *) &ecm_union_desc,
267 (struct usb_descriptor_header *) &ether_desc,
268 /* NOTE: status endpoint might need to be removed */
269 (struct usb_descriptor_header *) &hs_notify_desc,
270 /* data interface, altsettings 0 and 1 */
271 (struct usb_descriptor_header *) &ecm_data_nop_intf,
272 (struct usb_descriptor_header *) &ecm_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
275 NULL,
276};
277
278/* string descriptors: */
279
280static struct usb_string ecm_string_defs[] = {
281 [0].s = "CDC Ethernet Control Model (ECM)",
282 [1].s = NULL /* DYNAMIC */,
283 [2].s = "CDC Ethernet Data",
284 { } /* end of list */
285};
286
287static struct usb_gadget_strings ecm_string_table = {
288 .language = 0x0409, /* en-us */
289 .strings = ecm_string_defs,
290};
291
292static struct usb_gadget_strings *ecm_strings[] = {
293 &ecm_string_table,
294 NULL,
295};
296
297/*-------------------------------------------------------------------------*/
298
299static void ecm_do_notify(struct f_ecm *ecm)
300{
301 struct usb_request *req = ecm->notify_req;
302 struct usb_cdc_notification *event;
303 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
304 __le32 *data;
305 int status;
306
307 /* notification already in flight? */
308 if (!req)
309 return;
310
311 event = req->buf;
312 switch (ecm->notify_state) {
313 case ECM_NOTIFY_NONE:
314 return;
315
316 case ECM_NOTIFY_CONNECT:
317 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
318 if (ecm->is_open)
319 event->wValue = cpu_to_le16(1);
320 else
321 event->wValue = cpu_to_le16(0);
322 event->wLength = 0;
323 req->length = sizeof *event;
324
325 DBG(cdev, "notify connect %s\n",
326 ecm->is_open ? "true" : "false");
327 ecm->notify_state = ECM_NOTIFY_SPEED;
328 break;
329
330 case ECM_NOTIFY_SPEED:
331 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
332 event->wValue = cpu_to_le16(0);
333 event->wLength = cpu_to_le16(8);
334 req->length = STATUS_BYTECOUNT;
335
336 /* SPEED_CHANGE data is up/down speeds in bits/sec */
337 data = req->buf + sizeof *event;
338 data[0] = cpu_to_le32(bitrate(cdev->gadget));
339 data[1] = data[0];
340
341 DBG(cdev, "notify speed %d\n", bitrate(cdev->gadget));
342 ecm->notify_state = ECM_NOTIFY_NONE;
343 break;
344 }
345 event->bmRequestType = 0xA1;
346 event->wIndex = cpu_to_le16(ecm->ctrl_id);
347
348 ecm->notify_req = NULL;
349 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
350 if (status < 0) {
351 ecm->notify_req = req;
352 DBG(cdev, "notify --> %d\n", status);
353 }
354}
355
356static void ecm_notify(struct f_ecm *ecm)
357{
358 /* NOTE on most versions of Linux, host side cdc-ethernet
359 * won't listen for notifications until its netdevice opens.
360 * The first notification then sits in the FIFO for a long
361 * time, and the second one is queued.
362 */
363 ecm->notify_state = ECM_NOTIFY_CONNECT;
364 ecm_do_notify(ecm);
365}
366
367static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
368{
369 struct f_ecm *ecm = req->context;
370 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
371 struct usb_cdc_notification *event = req->buf;
372
373 switch (req->status) {
374 case 0:
375 /* no fault */
376 break;
377 case -ECONNRESET:
378 case -ESHUTDOWN:
379 ecm->notify_state = ECM_NOTIFY_NONE;
380 break;
381 default:
382 DBG(cdev, "event %02x --> %d\n",
383 event->bNotificationType, req->status);
384 break;
385 }
386 ecm->notify_req = req;
387 ecm_do_notify(ecm);
388}
389
390static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
391{
392 struct f_ecm *ecm = func_to_ecm(f);
393 struct usb_composite_dev *cdev = f->config->cdev;
394 struct usb_request *req = cdev->req;
395 int value = -EOPNOTSUPP;
396 u16 w_index = le16_to_cpu(ctrl->wIndex);
397 u16 w_value = le16_to_cpu(ctrl->wValue);
398 u16 w_length = le16_to_cpu(ctrl->wLength);
399
400 /* composite driver infrastructure handles everything except
401 * CDC class messages; interface activation uses set_alt().
402 */
403 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
404 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
405 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
406 /* see 6.2.30: no data, wIndex = interface,
407 * wValue = packet filter bitmap
408 */
409 if (w_length != 0 || w_index != ecm->ctrl_id)
410 goto invalid;
411 DBG(cdev, "packet filter %02x\n", w_value);
412 /* REVISIT locking of cdc_filter. This assumes the UDC
413 * driver won't have a concurrent packet TX irq running on
414 * another CPU; or that if it does, this write is atomic...
415 */
416 ecm->port.cdc_filter = w_value;
417 value = 0;
418 break;
419
420 /* and optionally:
421 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
422 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
423 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
424 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
425 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
426 * case USB_CDC_GET_ETHERNET_STATISTIC:
427 */
428
429 default:
430invalid:
431 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
432 ctrl->bRequestType, ctrl->bRequest,
433 w_value, w_index, w_length);
434 }
435
436 /* respond with data transfer or status phase? */
437 if (value >= 0) {
438 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
439 ctrl->bRequestType, ctrl->bRequest,
440 w_value, w_index, w_length);
441 req->zero = 0;
442 req->length = value;
443 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
444 if (value < 0)
445 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
446 ctrl->bRequestType, ctrl->bRequest,
447 value);
448 }
449
450 /* device either stalls (value < 0) or reports success */
451 return value;
452}
453
454
455static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
456{
457 struct f_ecm *ecm = func_to_ecm(f);
458 struct usb_composite_dev *cdev = f->config->cdev;
459
460 /* Control interface has only altsetting 0 */
461 if (intf == ecm->ctrl_id) {
462 if (alt != 0)
463 goto fail;
464
465 if (ecm->notify->driver_data) {
466 VDBG(cdev, "reset ecm control %d\n", intf);
467 usb_ep_disable(ecm->notify);
468 } else {
469 VDBG(cdev, "init ecm ctrl %d\n", intf);
470 ecm->notify_desc = ep_choose(cdev->gadget,
471 ecm->hs.notify,
472 ecm->fs.notify);
473 }
474 usb_ep_enable(ecm->notify, ecm->notify_desc);
475 ecm->notify->driver_data = ecm;
476
477 /* Data interface has two altsettings, 0 and 1 */
478 } else if (intf == ecm->data_id) {
479 if (alt > 1)
480 goto fail;
481
482 if (ecm->port.in_ep->driver_data) {
483 DBG(cdev, "reset ecm\n");
484 gether_disconnect(&ecm->port);
485 }
486
487 if (!ecm->port.in) {
488 DBG(cdev, "init ecm\n");
489 ecm->port.in = ep_choose(cdev->gadget,
490 ecm->hs.in, ecm->fs.in);
491 ecm->port.out = ep_choose(cdev->gadget,
492 ecm->hs.out, ecm->fs.out);
493 }
494
495 /* CDC Ethernet only sends data in non-default altsettings.
496 * Changing altsettings resets filters, statistics, etc.
497 */
498 if (alt == 1) {
499 struct net_device *net;
500
501 /* Enable zlps by default for ECM conformance;
502 * override for musb_hdrc (avoids txdma ovhead)
503 * and sa1100 (can't).
504 */
505 ecm->port.is_zlp_ok = !(
506 gadget_is_sa1100(cdev->gadget)
507 || gadget_is_musbhdrc(cdev->gadget)
508 );
509 ecm->port.cdc_filter = DEFAULT_FILTER;
510 DBG(cdev, "activate ecm\n");
511 net = gether_connect(&ecm->port);
512 if (IS_ERR(net))
513 return PTR_ERR(net);
514 }
515
516 /* NOTE this can be a minor disagreement with the ECM spec,
517 * which says speed notifications will "always" follow
518 * connection notifications. But we allow one connect to
519 * follow another (if the first is in flight), and instead
520 * just guarantee that a speed notification is always sent.
521 */
522 ecm_notify(ecm);
523 } else
524 goto fail;
525
526 return 0;
527fail:
528 return -EINVAL;
529}
530
531/* Because the data interface supports multiple altsettings,
532 * this ECM function *MUST* implement a get_alt() method.
533 */
534static int ecm_get_alt(struct usb_function *f, unsigned intf)
535{
536 struct f_ecm *ecm = func_to_ecm(f);
537
538 if (intf == ecm->ctrl_id)
539 return 0;
540 return ecm->port.in_ep->driver_data ? 1 : 0;
541}
542
543static void ecm_disable(struct usb_function *f)
544{
545 struct f_ecm *ecm = func_to_ecm(f);
546 struct usb_composite_dev *cdev = f->config->cdev;
547
548 DBG(cdev, "ecm deactivated\n");
549
550 if (ecm->port.in_ep->driver_data)
551 gether_disconnect(&ecm->port);
552
553 if (ecm->notify->driver_data) {
554 usb_ep_disable(ecm->notify);
555 ecm->notify->driver_data = NULL;
556 ecm->notify_desc = NULL;
557 }
558}
559
560/*-------------------------------------------------------------------------*/
561
562/*
563 * Callbacks let us notify the host about connect/disconnect when the
564 * net device is opened or closed.
565 *
566 * For testing, note that link states on this side include both opened
567 * and closed variants of:
568 *
569 * - disconnected/unconfigured
570 * - configured but inactive (data alt 0)
571 * - configured and active (data alt 1)
572 *
573 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
574 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
575 * imply the host is actually polling the notification endpoint, and
576 * likewise that "active" doesn't imply it's actually using the data
577 * endpoints for traffic.
578 */
579
580static void ecm_open(struct gether *geth)
581{
582 struct f_ecm *ecm = func_to_ecm(&geth->func);
583
584 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
585
586 ecm->is_open = true;
587 ecm_notify(ecm);
588}
589
590static void ecm_close(struct gether *geth)
591{
592 struct f_ecm *ecm = func_to_ecm(&geth->func);
593
594 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
595
596 ecm->is_open = false;
597 ecm_notify(ecm);
598}
599
600/*-------------------------------------------------------------------------*/
601
602/* ethernet function driver setup/binding */
603
604static int __init
605ecm_bind(struct usb_configuration *c, struct usb_function *f)
606{
607 struct usb_composite_dev *cdev = c->cdev;
608 struct f_ecm *ecm = func_to_ecm(f);
609 int status;
610 struct usb_ep *ep;
611
612 /* allocate instance-specific interface IDs */
613 status = usb_interface_id(c, f);
614 if (status < 0)
615 goto fail;
616 ecm->ctrl_id = status;
617
618 ecm_control_intf.bInterfaceNumber = status;
619 ecm_union_desc.bMasterInterface0 = status;
620
621 status = usb_interface_id(c, f);
622 if (status < 0)
623 goto fail;
624 ecm->data_id = status;
625
626 ecm_data_nop_intf.bInterfaceNumber = status;
627 ecm_data_intf.bInterfaceNumber = status;
628 ecm_union_desc.bSlaveInterface0 = status;
629
630 status = -ENODEV;
631
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
634 if (!ep)
635 goto fail;
636 ecm->port.in_ep = ep;
637 ep->driver_data = cdev; /* claim */
638
639 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
640 if (!ep)
641 goto fail;
642 ecm->port.out_ep = ep;
643 ep->driver_data = cdev; /* claim */
644
645 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
646 * don't treat it that way. It's simpler, and some newer CDC
647 * profiles (wireless handsets) no longer treat it as optional.
648 */
649 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
650 if (!ep)
651 goto fail;
652 ecm->notify = ep;
653 ep->driver_data = cdev; /* claim */
654
655 status = -ENOMEM;
656
657 /* allocate notification request and buffer */
658 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
659 if (!ecm->notify_req)
660 goto fail;
661 ecm->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
662 if (!ecm->notify_req->buf)
663 goto fail;
664 ecm->notify_req->context = ecm;
665 ecm->notify_req->complete = ecm_notify_complete;
666
667 /* copy descriptors, and track endpoint copies */
668 f->descriptors = usb_copy_descriptors(eth_fs_function);
669 if (!f->descriptors)
670 goto fail;
671
672 ecm->fs.in = usb_find_endpoint(eth_fs_function,
673 f->descriptors, &fs_in_desc);
674 ecm->fs.out = usb_find_endpoint(eth_fs_function,
675 f->descriptors, &fs_out_desc);
676 ecm->fs.notify = usb_find_endpoint(eth_fs_function,
677 f->descriptors, &fs_notify_desc);
678
679 /* support all relevant hardware speeds... we expect that when
680 * hardware is dual speed, all bulk-capable endpoints work at
681 * both speeds
682 */
683 if (gadget_is_dualspeed(c->cdev->gadget)) {
684 hs_in_desc.bEndpointAddress =
685 fs_in_desc.bEndpointAddress;
686 hs_out_desc.bEndpointAddress =
687 fs_out_desc.bEndpointAddress;
688 hs_notify_desc.bEndpointAddress =
689 fs_notify_desc.bEndpointAddress;
690
691 /* copy descriptors, and track endpoint copies */
692 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
693 if (!f->hs_descriptors)
694 goto fail;
695
696 ecm->hs.in = usb_find_endpoint(eth_hs_function,
697 f->hs_descriptors, &hs_in_desc);
698 ecm->hs.out = usb_find_endpoint(eth_hs_function,
699 f->hs_descriptors, &hs_out_desc);
700 ecm->hs.notify = usb_find_endpoint(eth_hs_function,
701 f->hs_descriptors, &hs_notify_desc);
702 }
703
704 /* NOTE: all that is done without knowing or caring about
705 * the network link ... which is unavailable to this code
706 * until we're activated via set_alt().
707 */
708
709 ecm->port.open = ecm_open;
710 ecm->port.close = ecm_close;
711
712 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
713 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
714 ecm->port.in_ep->name, ecm->port.out_ep->name,
715 ecm->notify->name);
716 return 0;
717
718fail:
719 if (f->descriptors)
720 usb_free_descriptors(f->descriptors);
721
722 if (ecm->notify_req) {
723 kfree(ecm->notify_req->buf);
724 usb_ep_free_request(ecm->notify, ecm->notify_req);
725 }
726
727 /* we might as well release our claims on endpoints */
728 if (ecm->notify)
729 ecm->notify->driver_data = NULL;
730 if (ecm->port.out)
731 ecm->port.out_ep->driver_data = NULL;
732 if (ecm->port.in)
733 ecm->port.in_ep->driver_data = NULL;
734
735 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
736
737 return status;
738}
739
740static void
741ecm_unbind(struct usb_configuration *c, struct usb_function *f)
742{
743 struct f_ecm *ecm = func_to_ecm(f);
744
745 DBG(c->cdev, "ecm unbind\n");
746
747 if (gadget_is_dualspeed(c->cdev->gadget))
748 usb_free_descriptors(f->hs_descriptors);
749 usb_free_descriptors(f->descriptors);
750
751 kfree(ecm->notify_req->buf);
752 usb_ep_free_request(ecm->notify, ecm->notify_req);
753
754 ecm_string_defs[1].s = NULL;
755 kfree(ecm);
756}
757
758/**
759 * ecm_bind_config - add CDC Ethernet network link to a configuration
760 * @c: the configuration to support the network link
761 * @ethaddr: a buffer in which the ethernet address of the host side
762 * side of the link was recorded
763 * Context: single threaded during gadget setup
764 *
765 * Returns zero on success, else negative errno.
766 *
767 * Caller must have called @gether_setup(). Caller is also responsible
768 * for calling @gether_cleanup() before module unload.
769 */
770int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
771{
772 struct f_ecm *ecm;
773 int status;
774
775 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
776 return -EINVAL;
777
778 /* maybe allocate device-global string IDs */
779 if (ecm_string_defs[0].id == 0) {
780
781 /* control interface label */
782 status = usb_string_id(c->cdev);
783 if (status < 0)
784 return status;
785 ecm_string_defs[0].id = status;
786 ecm_control_intf.iInterface = status;
787
788 /* data interface label */
789 status = usb_string_id(c->cdev);
790 if (status < 0)
791 return status;
792 ecm_string_defs[2].id = status;
793 ecm_data_intf.iInterface = status;
794
795 /* MAC address */
796 status = usb_string_id(c->cdev);
797 if (status < 0)
798 return status;
799 ecm_string_defs[1].id = status;
800 ether_desc.iMACAddress = status;
801 }
802
803 /* allocate and initialize one new instance */
804 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
805 if (!ecm)
806 return -ENOMEM;
807
808 /* export host's Ethernet address in CDC format */
809 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
810 "%02X%02X%02X%02X%02X%02X",
811 ethaddr[0], ethaddr[1], ethaddr[2],
812 ethaddr[3], ethaddr[4], ethaddr[5]);
813 ecm_string_defs[1].s = ecm->ethaddr;
814
815 ecm->port.cdc_filter = DEFAULT_FILTER;
816
817 ecm->port.func.name = "cdc_ethernet";
818 ecm->port.func.strings = ecm_strings;
819 /* descriptors are per-instance copies */
820 ecm->port.func.bind = ecm_bind;
821 ecm->port.func.unbind = ecm_unbind;
822 ecm->port.func.set_alt = ecm_set_alt;
823 ecm->port.func.get_alt = ecm_get_alt;
824 ecm->port.func.setup = ecm_setup;
825 ecm->port.func.disable = ecm_disable;
826
827 status = usb_add_function(c, &ecm->port.func);
828 if (status) {
829 ecm_string_defs[1].s = NULL;
830 kfree(ecm);
831 }
832 return status;
833}
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
new file mode 100644
index 000000000000..eda4cde72c82
--- /dev/null
+++ b/drivers/usb/gadget/f_loopback.c
@@ -0,0 +1,381 @@
1/*
2 * f_loopback.c - USB peripheral loopback configuration driver
3 *
4 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
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 VERBOSE_DEBUG */
23
24#include <linux/kernel.h>
25#include <linux/utsname.h>
26#include <linux/device.h>
27
28#include "g_zero.h"
29#include "gadget_chips.h"
30
31
32/*
33 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
34 *
35 * This takes messages of various sizes written OUT to a device, and loops
36 * them back so they can be read IN from it. It has been used by certain
37 * test applications. It supports limited testing of data queueing logic.
38 *
39 *
40 * This is currently packaged as a configuration driver, which can't be
41 * combined with other functions to make composite devices. However, it
42 * can be combined with other independent configurations.
43 */
44struct f_loopback {
45 struct usb_function function;
46
47 struct usb_ep *in_ep;
48 struct usb_ep *out_ep;
49};
50
51static inline struct f_loopback *func_to_loop(struct usb_function *f)
52{
53 return container_of(f, struct f_loopback, function);
54}
55
56static unsigned qlen = 32;
57module_param(qlen, uint, 0);
58MODULE_PARM_DESC(qlenn, "depth of loopback queue");
59
60/*-------------------------------------------------------------------------*/
61
62static struct usb_interface_descriptor loopback_intf = {
63 .bLength = sizeof loopback_intf,
64 .bDescriptorType = USB_DT_INTERFACE,
65
66 .bNumEndpoints = 2,
67 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
68 /* .iInterface = DYNAMIC */
69};
70
71/* full speed support: */
72
73static struct usb_endpoint_descriptor fs_source_desc = {
74 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
76
77 .bEndpointAddress = USB_DIR_IN,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
79};
80
81static struct usb_endpoint_descriptor fs_sink_desc = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
84
85 .bEndpointAddress = USB_DIR_OUT,
86 .bmAttributes = USB_ENDPOINT_XFER_BULK,
87};
88
89static struct usb_descriptor_header *fs_loopback_descs[] = {
90 (struct usb_descriptor_header *) &loopback_intf,
91 (struct usb_descriptor_header *) &fs_sink_desc,
92 (struct usb_descriptor_header *) &fs_source_desc,
93 NULL,
94};
95
96/* high speed support: */
97
98static struct usb_endpoint_descriptor hs_source_desc = {
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
103 .wMaxPacketSize = __constant_cpu_to_le16(512),
104};
105
106static struct usb_endpoint_descriptor hs_sink_desc = {
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109
110 .bmAttributes = USB_ENDPOINT_XFER_BULK,
111 .wMaxPacketSize = __constant_cpu_to_le16(512),
112};
113
114static struct usb_descriptor_header *hs_loopback_descs[] = {
115 (struct usb_descriptor_header *) &loopback_intf,
116 (struct usb_descriptor_header *) &hs_source_desc,
117 (struct usb_descriptor_header *) &hs_sink_desc,
118 NULL,
119};
120
121/* function-specific strings: */
122
123static struct usb_string strings_loopback[] = {
124 [0].s = "loop input to output",
125 { } /* end of list */
126};
127
128static struct usb_gadget_strings stringtab_loop = {
129 .language = 0x0409, /* en-us */
130 .strings = strings_loopback,
131};
132
133static struct usb_gadget_strings *loopback_strings[] = {
134 &stringtab_loop,
135 NULL,
136};
137
138/*-------------------------------------------------------------------------*/
139
140static int __init
141loopback_bind(struct usb_configuration *c, struct usb_function *f)
142{
143 struct usb_composite_dev *cdev = c->cdev;
144 struct f_loopback *loop = func_to_loop(f);
145 int id;
146
147 /* allocate interface ID(s) */
148 id = usb_interface_id(c, f);
149 if (id < 0)
150 return id;
151 loopback_intf.bInterfaceNumber = id;
152
153 /* allocate endpoints */
154
155 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
156 if (!loop->in_ep) {
157autoconf_fail:
158 ERROR(cdev, "%s: can't autoconfigure on %s\n",
159 f->name, cdev->gadget->name);
160 return -ENODEV;
161 }
162 loop->in_ep->driver_data = cdev; /* claim */
163
164 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
165 if (!loop->out_ep)
166 goto autoconf_fail;
167 loop->out_ep->driver_data = cdev; /* claim */
168
169 /* support high speed hardware */
170 if (gadget_is_dualspeed(c->cdev->gadget)) {
171 hs_source_desc.bEndpointAddress =
172 fs_source_desc.bEndpointAddress;
173 hs_sink_desc.bEndpointAddress =
174 fs_sink_desc.bEndpointAddress;
175 f->hs_descriptors = hs_loopback_descs;
176 }
177
178 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
179 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
180 f->name, loop->in_ep->name, loop->out_ep->name);
181 return 0;
182}
183
184static void
185loopback_unbind(struct usb_configuration *c, struct usb_function *f)
186{
187 kfree(func_to_loop(f));
188}
189
190static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
191{
192 struct f_loopback *loop = ep->driver_data;
193 struct usb_composite_dev *cdev = loop->function.config->cdev;
194 int status = req->status;
195
196 switch (status) {
197
198 case 0: /* normal completion? */
199 if (ep == loop->out_ep) {
200 /* loop this OUT packet back IN to the host */
201 req->zero = (req->actual < req->length);
202 req->length = req->actual;
203 status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
204 if (status == 0)
205 return;
206
207 /* "should never get here" */
208 ERROR(cdev, "can't loop %s to %s: %d\n",
209 ep->name, loop->in_ep->name,
210 status);
211 }
212
213 /* queue the buffer for some later OUT packet */
214 req->length = buflen;
215 status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
216 if (status == 0)
217 return;
218
219 /* "should never get here" */
220 /* FALLTHROUGH */
221
222 default:
223 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
224 status, req->actual, req->length);
225 /* FALLTHROUGH */
226
227 /* NOTE: since this driver doesn't maintain an explicit record
228 * of requests it submitted (just maintains qlen count), we
229 * rely on the hardware driver to clean up on disconnect or
230 * endpoint disable.
231 */
232 case -ECONNABORTED: /* hardware forced ep reset */
233 case -ECONNRESET: /* request dequeued */
234 case -ESHUTDOWN: /* disconnect from host */
235 free_ep_req(ep, req);
236 return;
237 }
238}
239
240static void disable_loopback(struct f_loopback *loop)
241{
242 struct usb_composite_dev *cdev;
243
244 cdev = loop->function.config->cdev;
245 disable_endpoints(cdev, loop->in_ep, loop->out_ep);
246 VDBG(cdev, "%s disabled\n", loop->function.name);
247}
248
249static int
250enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
251{
252 int result = 0;
253 const struct usb_endpoint_descriptor *src, *sink;
254 struct usb_ep *ep;
255 struct usb_request *req;
256 unsigned i;
257
258 src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc);
259 sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc);
260
261 /* one endpoint writes data back IN to the host */
262 ep = loop->in_ep;
263 result = usb_ep_enable(ep, src);
264 if (result < 0)
265 return result;
266 ep->driver_data = loop;
267
268 /* one endpoint just reads OUT packets */
269 ep = loop->out_ep;
270 result = usb_ep_enable(ep, sink);
271 if (result < 0) {
272fail0:
273 ep = loop->in_ep;
274 usb_ep_disable(ep);
275 ep->driver_data = NULL;
276 return result;
277 }
278 ep->driver_data = loop;
279
280 /* allocate a bunch of read buffers and queue them all at once.
281 * we buffer at most 'qlen' transfers; fewer if any need more
282 * than 'buflen' bytes each.
283 */
284 for (i = 0; i < qlen && result == 0; i++) {
285 req = alloc_ep_req(ep);
286 if (req) {
287 req->complete = loopback_complete;
288 result = usb_ep_queue(ep, req, GFP_ATOMIC);
289 if (result)
290 ERROR(cdev, "%s queue req --> %d\n",
291 ep->name, result);
292 } else {
293 usb_ep_disable(ep);
294 ep->driver_data = NULL;
295 result = -ENOMEM;
296 goto fail0;
297 }
298 }
299
300 DBG(cdev, "%s enabled\n", loop->function.name);
301 return result;
302}
303
304static int loopback_set_alt(struct usb_function *f,
305 unsigned intf, unsigned alt)
306{
307 struct f_loopback *loop = func_to_loop(f);
308 struct usb_composite_dev *cdev = f->config->cdev;
309
310 /* we know alt is zero */
311 if (loop->in_ep->driver_data)
312 disable_loopback(loop);
313 return enable_loopback(cdev, loop);
314}
315
316static void loopback_disable(struct usb_function *f)
317{
318 struct f_loopback *loop = func_to_loop(f);
319
320 disable_loopback(loop);
321}
322
323/*-------------------------------------------------------------------------*/
324
325static int __init loopback_bind_config(struct usb_configuration *c)
326{
327 struct f_loopback *loop;
328 int status;
329
330 loop = kzalloc(sizeof *loop, GFP_KERNEL);
331 if (!loop)
332 return -ENOMEM;
333
334 loop->function.name = "loopback";
335 loop->function.descriptors = fs_loopback_descs;
336 loop->function.bind = loopback_bind;
337 loop->function.unbind = loopback_unbind;
338 loop->function.set_alt = loopback_set_alt;
339 loop->function.disable = loopback_disable;
340
341 status = usb_add_function(c, &loop->function);
342 if (status)
343 kfree(loop);
344 return status;
345}
346
347static struct usb_configuration loopback_driver = {
348 .label = "loopback",
349 .strings = loopback_strings,
350 .bind = loopback_bind_config,
351 .bConfigurationValue = 2,
352 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
353 .bMaxPower = 1, /* 2 mA, minimal */
354 /* .iConfiguration = DYNAMIC */
355};
356
357/**
358 * loopback_add - add a loopback testing configuration to a device
359 * @cdev: the device to support the loopback configuration
360 */
361int __init loopback_add(struct usb_composite_dev *cdev)
362{
363 int id;
364
365 /* allocate string ID(s) */
366 id = usb_string_id(cdev);
367 if (id < 0)
368 return id;
369 strings_loopback[0].id = id;
370
371 loopback_intf.iInterface = id;
372 loopback_driver.iConfiguration = id;
373
374 /* support OTG systems */
375 if (gadget_is_otg(cdev->gadget)) {
376 loopback_driver.descriptors = otg_desc;
377 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
378 }
379
380 return usb_add_config(cdev, &loopback_driver);
381}
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
new file mode 100644
index 000000000000..61652f0f13fd
--- /dev/null
+++ b/drivers/usb/gadget/f_rndis.c
@@ -0,0 +1,827 @@
1/*
2 * f_rndis.c -- RNDIS link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/* #define VERBOSE_DEBUG */
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include <asm/atomic.h>
30
31#include "u_ether.h"
32#include "rndis.h"
33
34
35/*
36 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
37 * been promoted instead of the standard CDC Ethernet. The published RNDIS
38 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
39 * ActiveSync have even worse status in terms of specification.
40 *
41 * In short: it's a protocol controlled by (and for) Microsoft, not for an
42 * Open ecosystem or markets. Linux supports it *only* because Microsoft
43 * doesn't support the CDC Ethernet standard.
44 *
45 * The RNDIS data transfer model is complex, with multiple Ethernet packets
46 * per USB message, and out of band data. The control model is built around
47 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
48 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
49 * useless (they're ignored). RNDIS expects to be the only function in its
50 * configuration, so it's no real help if you need composite devices; and
51 * it expects to be the first configuration too.
52 *
53 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
54 * discount the fluff that its RPC can be made to deliver: it doesn't need
55 * a NOP altsetting for the data interface. That lets it work on some of the
56 * "so smart it's stupid" hardware which takes over configuration changes
57 * from the software, and adds restrictions like "no altsettings".
58 *
59 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
60 * have all sorts of contrary-to-specification oddities that can prevent
61 * them from working sanely. Since bugfixes (or accurate specs, letting
62 * Linux work around those bugs) are unlikely to ever come from MSFT, you
63 * may want to avoid using RNDIS on purely operational grounds.
64 *
65 * Omissions from the RNDIS 1.0 specification include:
66 *
67 * - Power management ... references data that's scattered around lots
68 * of other documentation, which is incorrect/incomplete there too.
69 *
70 * - There are various undocumented protocol requirements, like the need
71 * to send garbage in some control-OUT messages.
72 *
73 * - MS-Windows drivers sometimes emit undocumented requests.
74 */
75
76struct rndis_ep_descs {
77 struct usb_endpoint_descriptor *in;
78 struct usb_endpoint_descriptor *out;
79 struct usb_endpoint_descriptor *notify;
80};
81
82struct f_rndis {
83 struct gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 int config;
87
88 struct usb_descriptor_header **fs_function;
89 struct rndis_ep_descs fs;
90 struct usb_descriptor_header **hs_function;
91 struct rndis_ep_descs hs;
92
93 struct usb_ep *notify;
94 struct usb_endpoint_descriptor *notify_desc;
95 struct usb_request *notify_req;
96 atomic_t notify_count;
97};
98
99static inline struct f_rndis *func_to_rndis(struct usb_function *f)
100{
101 return container_of(f, struct f_rndis, port.func);
102}
103
104/* peak (theoretical) bulk transfer rate in bits-per-second */
105static unsigned int bitrate(struct usb_gadget *g)
106{
107 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
108 return 13 * 512 * 8 * 1000 * 8;
109 else
110 return 19 * 64 * 1 * 1000 * 8;
111}
112
113/*-------------------------------------------------------------------------*/
114
115/*
116 */
117
118#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
119#define STATUS_BYTECOUNT 8 /* 8 bytes data */
120
121
122/* interface descriptor: */
123
124static struct usb_interface_descriptor rndis_control_intf __initdata = {
125 .bLength = sizeof rndis_control_intf,
126 .bDescriptorType = USB_DT_INTERFACE,
127
128 /* .bInterfaceNumber = DYNAMIC */
129 /* status endpoint is optional; this could be patched later */
130 .bNumEndpoints = 1,
131 .bInterfaceClass = USB_CLASS_COMM,
132 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
133 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
134 /* .iInterface = DYNAMIC */
135};
136
137static struct usb_cdc_header_desc header_desc __initdata = {
138 .bLength = sizeof header_desc,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
141
142 .bcdCDC = __constant_cpu_to_le16(0x0110),
143};
144
145static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
146 .bLength = sizeof call_mgmt_descriptor,
147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
149
150 .bmCapabilities = 0x00,
151 .bDataInterface = 0x01,
152};
153
154static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
155 .bLength = sizeof acm_descriptor,
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_ACM_TYPE,
158
159 .bmCapabilities = 0x00,
160};
161
162static struct usb_cdc_union_desc rndis_union_desc __initdata = {
163 .bLength = sizeof(rndis_union_desc),
164 .bDescriptorType = USB_DT_CS_INTERFACE,
165 .bDescriptorSubType = USB_CDC_UNION_TYPE,
166 /* .bMasterInterface0 = DYNAMIC */
167 /* .bSlaveInterface0 = DYNAMIC */
168};
169
170/* the data interface has two bulk endpoints */
171
172static struct usb_interface_descriptor rndis_data_intf __initdata = {
173 .bLength = sizeof rndis_data_intf,
174 .bDescriptorType = USB_DT_INTERFACE,
175
176 /* .bInterfaceNumber = DYNAMIC */
177 .bAlternateSetting = 1,
178 .bNumEndpoints = 2,
179 .bInterfaceClass = USB_CLASS_CDC_DATA,
180 .bInterfaceSubClass = 0,
181 .bInterfaceProtocol = 0,
182 /* .iInterface = DYNAMIC */
183};
184
185/* full speed support: */
186
187static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
190
191 .bEndpointAddress = USB_DIR_IN,
192 .bmAttributes = USB_ENDPOINT_XFER_INT,
193 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
194 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
195};
196
197static struct usb_endpoint_descriptor fs_in_desc __initdata = {
198 .bLength = USB_DT_ENDPOINT_SIZE,
199 .bDescriptorType = USB_DT_ENDPOINT,
200
201 .bEndpointAddress = USB_DIR_IN,
202 .bmAttributes = USB_ENDPOINT_XFER_BULK,
203};
204
205static struct usb_endpoint_descriptor fs_out_desc __initdata = {
206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
208
209 .bEndpointAddress = USB_DIR_OUT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211};
212
213static struct usb_descriptor_header *eth_fs_function[] __initdata = {
214 /* control interface matches ACM, not Ethernet */
215 (struct usb_descriptor_header *) &rndis_control_intf,
216 (struct usb_descriptor_header *) &header_desc,
217 (struct usb_descriptor_header *) &call_mgmt_descriptor,
218 (struct usb_descriptor_header *) &acm_descriptor,
219 (struct usb_descriptor_header *) &rndis_union_desc,
220 (struct usb_descriptor_header *) &fs_notify_desc,
221 /* data interface has no altsetting */
222 (struct usb_descriptor_header *) &rndis_data_intf,
223 (struct usb_descriptor_header *) &fs_in_desc,
224 (struct usb_descriptor_header *) &fs_out_desc,
225 NULL,
226};
227
228/* high speed support: */
229
230static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
236 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
237 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
238};
239static struct usb_endpoint_descriptor hs_in_desc __initdata = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
242
243 .bEndpointAddress = USB_DIR_IN,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = __constant_cpu_to_le16(512),
246};
247
248static struct usb_endpoint_descriptor hs_out_desc __initdata = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251
252 .bEndpointAddress = USB_DIR_OUT,
253 .bmAttributes = USB_ENDPOINT_XFER_BULK,
254 .wMaxPacketSize = __constant_cpu_to_le16(512),
255};
256
257static struct usb_descriptor_header *eth_hs_function[] __initdata = {
258 /* control interface matches ACM, not Ethernet */
259 (struct usb_descriptor_header *) &rndis_control_intf,
260 (struct usb_descriptor_header *) &header_desc,
261 (struct usb_descriptor_header *) &call_mgmt_descriptor,
262 (struct usb_descriptor_header *) &acm_descriptor,
263 (struct usb_descriptor_header *) &rndis_union_desc,
264 (struct usb_descriptor_header *) &hs_notify_desc,
265 /* data interface has no altsetting */
266 (struct usb_descriptor_header *) &rndis_data_intf,
267 (struct usb_descriptor_header *) &hs_in_desc,
268 (struct usb_descriptor_header *) &hs_out_desc,
269 NULL,
270};
271
272/* string descriptors: */
273
274static struct usb_string rndis_string_defs[] = {
275 [0].s = "RNDIS Communications Control",
276 [1].s = "RNDIS Ethernet Data",
277 { } /* end of list */
278};
279
280static struct usb_gadget_strings rndis_string_table = {
281 .language = 0x0409, /* en-us */
282 .strings = rndis_string_defs,
283};
284
285static struct usb_gadget_strings *rndis_strings[] = {
286 &rndis_string_table,
287 NULL,
288};
289
290/*-------------------------------------------------------------------------*/
291
292static struct sk_buff *rndis_add_header(struct sk_buff *skb)
293{
294 skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
295 if (skb)
296 rndis_add_hdr(skb);
297 return skb;
298}
299
300static void rndis_response_available(void *_rndis)
301{
302 struct f_rndis *rndis = _rndis;
303 struct usb_request *req = rndis->notify_req;
304 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
305 __le32 *data = req->buf;
306 int status;
307
308 if (atomic_inc_return(&rndis->notify_count))
309 return;
310
311 /* Send RNDIS RESPONSE_AVAILABLE notification; a
312 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
313 *
314 * This is the only notification defined by RNDIS.
315 */
316 data[0] = cpu_to_le32(1);
317 data[1] = cpu_to_le32(0);
318
319 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
320 if (status) {
321 atomic_dec(&rndis->notify_count);
322 DBG(cdev, "notify/0 --> %d\n", status);
323 }
324}
325
326static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
327{
328 struct f_rndis *rndis = req->context;
329 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
330 int status = req->status;
331
332 /* after TX:
333 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
334 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
335 */
336 switch (status) {
337 case -ECONNRESET:
338 case -ESHUTDOWN:
339 /* connection gone */
340 atomic_set(&rndis->notify_count, 0);
341 break;
342 default:
343 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
344 ep->name, status,
345 req->actual, req->length);
346 /* FALLTHROUGH */
347 case 0:
348 if (ep != rndis->notify)
349 break;
350
351 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
352 * notifications by resending until we're done
353 */
354 if (atomic_dec_and_test(&rndis->notify_count))
355 break;
356 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
357 if (status) {
358 atomic_dec(&rndis->notify_count);
359 DBG(cdev, "notify/1 --> %d\n", status);
360 }
361 break;
362 }
363}
364
365static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
366{
367 struct f_rndis *rndis = req->context;
368 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
369 int status;
370
371 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
372// spin_lock(&dev->lock);
373 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
374 if (status < 0)
375 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
376 status, req->actual, req->length);
377// spin_unlock(&dev->lock);
378}
379
380static int
381rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
382{
383 struct f_rndis *rndis = func_to_rndis(f);
384 struct usb_composite_dev *cdev = f->config->cdev;
385 struct usb_request *req = cdev->req;
386 int value = -EOPNOTSUPP;
387 u16 w_index = le16_to_cpu(ctrl->wIndex);
388 u16 w_value = le16_to_cpu(ctrl->wValue);
389 u16 w_length = le16_to_cpu(ctrl->wLength);
390
391 /* composite driver infrastructure handles everything except
392 * CDC class messages; interface activation uses set_alt().
393 */
394 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
395
396 /* RNDIS uses the CDC command encapsulation mechanism to implement
397 * an RPC scheme, with much getting/setting of attributes by OID.
398 */
399 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
400 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
401 if (w_length > req->length || w_value
402 || w_index != rndis->ctrl_id)
403 goto invalid;
404 /* read the request; process it later */
405 value = w_length;
406 req->complete = rndis_command_complete;
407 req->context = rndis;
408 /* later, rndis_response_available() sends a notification */
409 break;
410
411 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
412 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
413 if (w_value || w_index != rndis->ctrl_id)
414 goto invalid;
415 else {
416 u8 *buf;
417 u32 n;
418
419 /* return the result */
420 buf = rndis_get_next_response(rndis->config, &n);
421 if (buf) {
422 memcpy(req->buf, buf, n);
423 req->complete = rndis_response_complete;
424 rndis_free_response(rndis->config, buf);
425 value = n;
426 }
427 /* else stalls ... spec says to avoid that */
428 }
429 break;
430
431 default:
432invalid:
433 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
434 ctrl->bRequestType, ctrl->bRequest,
435 w_value, w_index, w_length);
436 }
437
438 /* respond with data transfer or status phase? */
439 if (value >= 0) {
440 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
441 ctrl->bRequestType, ctrl->bRequest,
442 w_value, w_index, w_length);
443 req->zero = 0;
444 req->length = value;
445 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
446 if (value < 0)
447 ERROR(cdev, "rndis response on err %d\n", value);
448 }
449
450 /* device either stalls (value < 0) or reports success */
451 return value;
452}
453
454
455static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
456{
457 struct f_rndis *rndis = func_to_rndis(f);
458 struct usb_composite_dev *cdev = f->config->cdev;
459
460 /* we know alt == 0 */
461
462 if (intf == rndis->ctrl_id) {
463 if (rndis->notify->driver_data) {
464 VDBG(cdev, "reset rndis control %d\n", intf);
465 usb_ep_disable(rndis->notify);
466 } else {
467 VDBG(cdev, "init rndis ctrl %d\n", intf);
468 rndis->notify_desc = ep_choose(cdev->gadget,
469 rndis->hs.notify,
470 rndis->fs.notify);
471 }
472 usb_ep_enable(rndis->notify, rndis->notify_desc);
473 rndis->notify->driver_data = rndis;
474
475 } else if (intf == rndis->data_id) {
476 struct net_device *net;
477
478 if (rndis->port.in_ep->driver_data) {
479 DBG(cdev, "reset rndis\n");
480 gether_disconnect(&rndis->port);
481 } else {
482 DBG(cdev, "init rndis\n");
483 rndis->port.in = ep_choose(cdev->gadget,
484 rndis->hs.in, rndis->fs.in);
485 rndis->port.out = ep_choose(cdev->gadget,
486 rndis->hs.out, rndis->fs.out);
487 }
488
489 /* Avoid ZLPs; they can be troublesome. */
490 rndis->port.is_zlp_ok = false;
491
492 /* RNDIS should be in the "RNDIS uninitialized" state,
493 * either never activated or after rndis_uninit().
494 *
495 * We don't want data to flow here until a nonzero packet
496 * filter is set, at which point it enters "RNDIS data
497 * initialized" state ... but we do want the endpoints
498 * to be activated. It's a strange little state.
499 *
500 * REVISIT the RNDIS gadget code has done this wrong for a
501 * very long time. We need another call to the link layer
502 * code -- gether_updown(...bool) maybe -- to do it right.
503 */
504 rndis->port.cdc_filter = 0;
505
506 DBG(cdev, "RNDIS RX/TX early activation ... \n");
507 net = gether_connect(&rndis->port);
508 if (IS_ERR(net))
509 return PTR_ERR(net);
510
511 rndis_set_param_dev(rndis->config, net,
512 &rndis->port.cdc_filter);
513 } else
514 goto fail;
515
516 return 0;
517fail:
518 return -EINVAL;
519}
520
521static void rndis_disable(struct usb_function *f)
522{
523 struct f_rndis *rndis = func_to_rndis(f);
524 struct usb_composite_dev *cdev = f->config->cdev;
525
526 if (!rndis->notify->driver_data)
527 return;
528
529 DBG(cdev, "rndis deactivated\n");
530
531 rndis_uninit(rndis->config);
532 gether_disconnect(&rndis->port);
533
534 usb_ep_disable(rndis->notify);
535 rndis->notify->driver_data = NULL;
536}
537
538/*-------------------------------------------------------------------------*/
539
540/*
541 * This isn't quite the same mechanism as CDC Ethernet, since the
542 * notification scheme passes less data, but the same set of link
543 * states must be tested. A key difference is that altsettings are
544 * not used to tell whether the link should send packets or not.
545 */
546
547static void rndis_open(struct gether *geth)
548{
549 struct f_rndis *rndis = func_to_rndis(&geth->func);
550 struct usb_composite_dev *cdev = geth->func.config->cdev;
551
552 DBG(cdev, "%s\n", __func__);
553
554 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
555 bitrate(cdev->gadget) / 100);
556 rndis_signal_connect(rndis->config);
557}
558
559static void rndis_close(struct gether *geth)
560{
561 struct f_rndis *rndis = func_to_rndis(&geth->func);
562
563 DBG(geth->func.config->cdev, "%s\n", __func__);
564
565 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
566 rndis_signal_disconnect(rndis->config);
567}
568
569/*-------------------------------------------------------------------------*/
570
571/* ethernet function driver setup/binding */
572
573static int __init
574rndis_bind(struct usb_configuration *c, struct usb_function *f)
575{
576 struct usb_composite_dev *cdev = c->cdev;
577 struct f_rndis *rndis = func_to_rndis(f);
578 int status;
579 struct usb_ep *ep;
580
581 /* allocate instance-specific interface IDs */
582 status = usb_interface_id(c, f);
583 if (status < 0)
584 goto fail;
585 rndis->ctrl_id = status;
586
587 rndis_control_intf.bInterfaceNumber = status;
588 rndis_union_desc.bMasterInterface0 = status;
589
590 status = usb_interface_id(c, f);
591 if (status < 0)
592 goto fail;
593 rndis->data_id = status;
594
595 rndis_data_intf.bInterfaceNumber = status;
596 rndis_union_desc.bSlaveInterface0 = status;
597
598 status = -ENODEV;
599
600 /* allocate instance-specific endpoints */
601 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
602 if (!ep)
603 goto fail;
604 rndis->port.in_ep = ep;
605 ep->driver_data = cdev; /* claim */
606
607 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
608 if (!ep)
609 goto fail;
610 rndis->port.out_ep = ep;
611 ep->driver_data = cdev; /* claim */
612
613 /* NOTE: a status/notification endpoint is, strictly speaking,
614 * optional. We don't treat it that way though! It's simpler,
615 * and some newer profiles don't treat it as optional.
616 */
617 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
618 if (!ep)
619 goto fail;
620 rndis->notify = ep;
621 ep->driver_data = cdev; /* claim */
622
623 status = -ENOMEM;
624
625 /* allocate notification request and buffer */
626 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
627 if (!rndis->notify_req)
628 goto fail;
629 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
630 if (!rndis->notify_req->buf)
631 goto fail;
632 rndis->notify_req->length = STATUS_BYTECOUNT;
633 rndis->notify_req->context = rndis;
634 rndis->notify_req->complete = rndis_response_complete;
635
636 /* copy descriptors, and track endpoint copies */
637 f->descriptors = usb_copy_descriptors(eth_fs_function);
638 if (!f->descriptors)
639 goto fail;
640
641 rndis->fs.in = usb_find_endpoint(eth_fs_function,
642 f->descriptors, &fs_in_desc);
643 rndis->fs.out = usb_find_endpoint(eth_fs_function,
644 f->descriptors, &fs_out_desc);
645 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
646 f->descriptors, &fs_notify_desc);
647
648 /* support all relevant hardware speeds... we expect that when
649 * hardware is dual speed, all bulk-capable endpoints work at
650 * both speeds
651 */
652 if (gadget_is_dualspeed(c->cdev->gadget)) {
653 hs_in_desc.bEndpointAddress =
654 fs_in_desc.bEndpointAddress;
655 hs_out_desc.bEndpointAddress =
656 fs_out_desc.bEndpointAddress;
657
658 /* copy descriptors, and track endpoint copies */
659 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
660
661 if (!f->hs_descriptors)
662 goto fail;
663
664 rndis->hs.in = usb_find_endpoint(eth_hs_function,
665 f->hs_descriptors, &hs_in_desc);
666 rndis->hs.out = usb_find_endpoint(eth_hs_function,
667 f->hs_descriptors, &hs_out_desc);
668 }
669
670 rndis->port.open = rndis_open;
671 rndis->port.close = rndis_close;
672
673 status = rndis_register(rndis_response_available, rndis);
674 if (status < 0)
675 goto fail;
676 rndis->config = status;
677
678 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
679 rndis_set_host_mac(rndis->config, rndis->ethaddr);
680
681#if 0
682// FIXME
683 if (rndis_set_param_vendor(rndis->config, vendorID,
684 manufacturer))
685 goto fail0;
686#endif
687
688 /* NOTE: all that is done without knowing or caring about
689 * the network link ... which is unavailable to this code
690 * until we're activated via set_alt().
691 */
692
693 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
694 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
695 rndis->port.in_ep->name, rndis->port.out_ep->name,
696 rndis->notify->name);
697 return 0;
698
699fail:
700 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
701 usb_free_descriptors(f->hs_descriptors);
702 if (f->descriptors)
703 usb_free_descriptors(f->descriptors);
704
705 if (rndis->notify_req) {
706 kfree(rndis->notify_req->buf);
707 usb_ep_free_request(rndis->notify, rndis->notify_req);
708 }
709
710 /* we might as well release our claims on endpoints */
711 if (rndis->notify)
712 rndis->notify->driver_data = NULL;
713 if (rndis->port.out)
714 rndis->port.out_ep->driver_data = NULL;
715 if (rndis->port.in)
716 rndis->port.in_ep->driver_data = NULL;
717
718 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
719
720 return status;
721}
722
723static void
724rndis_unbind(struct usb_configuration *c, struct usb_function *f)
725{
726 struct f_rndis *rndis = func_to_rndis(f);
727
728 rndis_deregister(rndis->config);
729 rndis_exit();
730
731 if (gadget_is_dualspeed(c->cdev->gadget))
732 usb_free_descriptors(f->hs_descriptors);
733 usb_free_descriptors(f->descriptors);
734
735 kfree(rndis->notify_req->buf);
736 usb_ep_free_request(rndis->notify, rndis->notify_req);
737
738 kfree(rndis);
739}
740
741/* Some controllers can't support RNDIS ... */
742static inline bool can_support_rndis(struct usb_configuration *c)
743{
744 /* only two endpoints on sa1100 */
745 if (gadget_is_sa1100(c->cdev->gadget))
746 return false;
747
748 /* everything else is *presumably* fine */
749 return true;
750}
751
752/**
753 * rndis_bind_config - add RNDIS network link to a configuration
754 * @c: the configuration to support the network link
755 * @ethaddr: a buffer in which the ethernet address of the host side
756 * side of the link was recorded
757 * Context: single threaded during gadget setup
758 *
759 * Returns zero on success, else negative errno.
760 *
761 * Caller must have called @gether_setup(). Caller is also responsible
762 * for calling @gether_cleanup() before module unload.
763 */
764int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
765{
766 struct f_rndis *rndis;
767 int status;
768
769 if (!can_support_rndis(c) || !ethaddr)
770 return -EINVAL;
771
772 /* maybe allocate device-global string IDs */
773 if (rndis_string_defs[0].id == 0) {
774
775 /* ... and setup RNDIS itself */
776 status = rndis_init();
777 if (status < 0)
778 return status;
779
780 /* control interface label */
781 status = usb_string_id(c->cdev);
782 if (status < 0)
783 return status;
784 rndis_string_defs[0].id = status;
785 rndis_control_intf.iInterface = status;
786
787 /* data interface label */
788 status = usb_string_id(c->cdev);
789 if (status < 0)
790 return status;
791 rndis_string_defs[1].id = status;
792 rndis_data_intf.iInterface = status;
793 }
794
795 /* allocate and initialize one new instance */
796 status = -ENOMEM;
797 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
798 if (!rndis)
799 goto fail;
800
801 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
802
803 /* RNDIS activates when the host changes this filter */
804 rndis->port.cdc_filter = 0;
805
806 /* RNDIS has special (and complex) framing */
807 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
808 rndis->port.wrap = rndis_add_header;
809 rndis->port.unwrap = rndis_rm_hdr;
810
811 rndis->port.func.name = "rndis";
812 rndis->port.func.strings = rndis_strings;
813 /* descriptors are per-instance copies */
814 rndis->port.func.bind = rndis_bind;
815 rndis->port.func.unbind = rndis_unbind;
816 rndis->port.func.set_alt = rndis_set_alt;
817 rndis->port.func.setup = rndis_setup;
818 rndis->port.func.disable = rndis_disable;
819
820 status = usb_add_function(c, &rndis->port.func);
821 if (status) {
822 kfree(rndis);
823fail:
824 rndis_exit();
825 }
826 return status;
827}
diff --git a/drivers/usb/gadget/f_serial.c b/drivers/usb/gadget/f_serial.c
new file mode 100644
index 000000000000..1b6bde9aaed5
--- /dev/null
+++ b/drivers/usb/gadget/f_serial.c
@@ -0,0 +1,296 @@
1/*
2 * f_serial.c - generic USB serial function driver
3 *
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
7 *
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15
16#include "u_serial.h"
17#include "gadget_chips.h"
18
19
20/*
21 * This function packages a simple "generic serial" port with no real
22 * control mechanisms, just raw data transfer over two bulk endpoints.
23 *
24 * Because it's not standardized, this isn't as interoperable as the
25 * CDC ACM driver. However, for many purposes it's just as functional
26 * if you can arrange appropriate host side drivers.
27 */
28
29struct gser_descs {
30 struct usb_endpoint_descriptor *in;
31 struct usb_endpoint_descriptor *out;
32};
33
34struct f_gser {
35 struct gserial port;
36 u8 data_id;
37 u8 port_num;
38
39 struct usb_descriptor_header **fs_function;
40 struct gser_descs fs;
41 struct usb_descriptor_header **hs_function;
42 struct gser_descs hs;
43};
44
45static inline struct f_gser *func_to_gser(struct usb_function *f)
46{
47 return container_of(f, struct f_gser, port.func);
48}
49
50/*-------------------------------------------------------------------------*/
51
52/* interface descriptor: */
53
54static struct usb_interface_descriptor gser_interface_desc __initdata = {
55 .bLength = USB_DT_INTERFACE_SIZE,
56 .bDescriptorType = USB_DT_INTERFACE,
57 /* .bInterfaceNumber = DYNAMIC */
58 .bNumEndpoints = 2,
59 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
60 .bInterfaceSubClass = 0,
61 .bInterfaceProtocol = 0,
62 /* .iInterface = DYNAMIC */
63};
64
65/* full speed support: */
66
67static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = {
68 .bLength = USB_DT_ENDPOINT_SIZE,
69 .bDescriptorType = USB_DT_ENDPOINT,
70 .bEndpointAddress = USB_DIR_IN,
71 .bmAttributes = USB_ENDPOINT_XFER_BULK,
72};
73
74static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = {
75 .bLength = USB_DT_ENDPOINT_SIZE,
76 .bDescriptorType = USB_DT_ENDPOINT,
77 .bEndpointAddress = USB_DIR_OUT,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
79};
80
81static struct usb_descriptor_header *gser_fs_function[] __initdata = {
82 (struct usb_descriptor_header *) &gser_interface_desc,
83 (struct usb_descriptor_header *) &gser_fs_in_desc,
84 (struct usb_descriptor_header *) &gser_fs_out_desc,
85 NULL,
86};
87
88/* high speed support: */
89
90static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = {
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93 .bmAttributes = USB_ENDPOINT_XFER_BULK,
94 .wMaxPacketSize = __constant_cpu_to_le16(512),
95};
96
97static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = {
98 .bLength = USB_DT_ENDPOINT_SIZE,
99 .bDescriptorType = USB_DT_ENDPOINT,
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = __constant_cpu_to_le16(512),
102};
103
104static struct usb_descriptor_header *gser_hs_function[] __initdata = {
105 (struct usb_descriptor_header *) &gser_interface_desc,
106 (struct usb_descriptor_header *) &gser_hs_in_desc,
107 (struct usb_descriptor_header *) &gser_hs_out_desc,
108 NULL,
109};
110
111/* string descriptors: */
112
113static struct usb_string gser_string_defs[] = {
114 [0].s = "Generic Serial",
115 { } /* end of list */
116};
117
118static struct usb_gadget_strings gser_string_table = {
119 .language = 0x0409, /* en-us */
120 .strings = gser_string_defs,
121};
122
123static struct usb_gadget_strings *gser_strings[] = {
124 &gser_string_table,
125 NULL,
126};
127
128/*-------------------------------------------------------------------------*/
129
130static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
131{
132 struct f_gser *gser = func_to_gser(f);
133 struct usb_composite_dev *cdev = f->config->cdev;
134
135 /* we know alt == 0, so this is an activation or a reset */
136
137 if (gser->port.in->driver_data) {
138 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
139 gserial_disconnect(&gser->port);
140 } else {
141 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
142 gser->port.in_desc = ep_choose(cdev->gadget,
143 gser->hs.in, gser->fs.in);
144 gser->port.out_desc = ep_choose(cdev->gadget,
145 gser->hs.out, gser->fs.out);
146 }
147 gserial_connect(&gser->port, gser->port_num);
148 return 0;
149}
150
151static void gser_disable(struct usb_function *f)
152{
153 struct f_gser *gser = func_to_gser(f);
154 struct usb_composite_dev *cdev = f->config->cdev;
155
156 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
157 gserial_disconnect(&gser->port);
158}
159
160/*-------------------------------------------------------------------------*/
161
162/* serial function driver setup/binding */
163
164static int __init
165gser_bind(struct usb_configuration *c, struct usb_function *f)
166{
167 struct usb_composite_dev *cdev = c->cdev;
168 struct f_gser *gser = func_to_gser(f);
169 int status;
170 struct usb_ep *ep;
171
172 /* allocate instance-specific interface IDs */
173 status = usb_interface_id(c, f);
174 if (status < 0)
175 goto fail;
176 gser->data_id = status;
177 gser_interface_desc.bInterfaceNumber = status;
178
179 status = -ENODEV;
180
181 /* allocate instance-specific endpoints */
182 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
183 if (!ep)
184 goto fail;
185 gser->port.in = ep;
186 ep->driver_data = cdev; /* claim */
187
188 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
189 if (!ep)
190 goto fail;
191 gser->port.out = ep;
192 ep->driver_data = cdev; /* claim */
193
194 /* copy descriptors, and track endpoint copies */
195 f->descriptors = usb_copy_descriptors(gser_fs_function);
196
197 gser->fs.in = usb_find_endpoint(gser_fs_function,
198 f->descriptors, &gser_fs_in_desc);
199 gser->fs.out = usb_find_endpoint(gser_fs_function,
200 f->descriptors, &gser_fs_out_desc);
201
202
203 /* support all relevant hardware speeds... we expect that when
204 * hardware is dual speed, all bulk-capable endpoints work at
205 * both speeds
206 */
207 if (gadget_is_dualspeed(c->cdev->gadget)) {
208 gser_hs_in_desc.bEndpointAddress =
209 gser_fs_in_desc.bEndpointAddress;
210 gser_hs_out_desc.bEndpointAddress =
211 gser_fs_out_desc.bEndpointAddress;
212
213 /* copy descriptors, and track endpoint copies */
214 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
215
216 gser->hs.in = usb_find_endpoint(gser_hs_function,
217 f->hs_descriptors, &gser_hs_in_desc);
218 gser->hs.out = usb_find_endpoint(gser_hs_function,
219 f->hs_descriptors, &gser_hs_out_desc);
220 }
221
222 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
223 gser->port_num,
224 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
225 gser->port.in->name, gser->port.out->name);
226 return 0;
227
228fail:
229 /* we might as well release our claims on endpoints */
230 if (gser->port.out)
231 gser->port.out->driver_data = NULL;
232 if (gser->port.in)
233 gser->port.in->driver_data = NULL;
234
235 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
236
237 return status;
238}
239
240static void
241gser_unbind(struct usb_configuration *c, struct usb_function *f)
242{
243 if (gadget_is_dualspeed(c->cdev->gadget))
244 usb_free_descriptors(f->hs_descriptors);
245 usb_free_descriptors(f->descriptors);
246 kfree(func_to_gser(f));
247}
248
249/**
250 * gser_bind_config - add a generic serial function to a configuration
251 * @c: the configuration to support the serial instance
252 * @port_num: /dev/ttyGS* port this interface will use
253 * Context: single threaded during gadget setup
254 *
255 * Returns zero on success, else negative errno.
256 *
257 * Caller must have called @gserial_setup() with enough ports to
258 * handle all the ones it binds. Caller is also responsible
259 * for calling @gserial_cleanup() before module unload.
260 */
261int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
262{
263 struct f_gser *gser;
264 int status;
265
266 /* REVISIT might want instance-specific strings to help
267 * distinguish instances ...
268 */
269
270 /* maybe allocate device-global string ID */
271 if (gser_string_defs[0].id == 0) {
272 status = usb_string_id(c->cdev);
273 if (status < 0)
274 return status;
275 gser_string_defs[0].id = status;
276 }
277
278 /* allocate and initialize one new instance */
279 gser = kzalloc(sizeof *gser, GFP_KERNEL);
280 if (!gser)
281 return -ENOMEM;
282
283 gser->port_num = port_num;
284
285 gser->port.func.name = "gser";
286 gser->port.func.strings = gser_strings;
287 gser->port.func.bind = gser_bind;
288 gser->port.func.unbind = gser_unbind;
289 gser->port.func.set_alt = gser_set_alt;
290 gser->port.func.disable = gser_disable;
291
292 status = usb_add_function(c, &gser->port.func);
293 if (status)
294 kfree(gser);
295 return status;
296}
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
new file mode 100644
index 000000000000..f18c3a14d72a
--- /dev/null
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -0,0 +1,587 @@
1/*
2 * f_sourcesink.c - USB peripheral source/sink configuration driver
3 *
4 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
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 VERBOSE_DEBUG */
23
24#include <linux/kernel.h>
25#include <linux/utsname.h>
26#include <linux/device.h>
27
28#include "g_zero.h"
29#include "gadget_chips.h"
30
31
32/*
33 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
34 * controller drivers.
35 *
36 * This just sinks bulk packets OUT to the peripheral and sources them IN
37 * to the host, optionally with specific data patterns for integrity tests.
38 * As such it supports basic functionality and load tests.
39 *
40 * In terms of control messaging, this supports all the standard requests
41 * plus two that support control-OUT tests. If the optional "autoresume"
42 * mode is enabled, it provides good functional coverage for the "USBCV"
43 * test harness from USB-IF.
44 *
45 * Note that because this doesn't queue more than one request at a time,
46 * some other function must be used to test queueing logic. The network
47 * link (g_ether) is the best overall option for that, since its TX and RX
48 * queues are relatively independent, will receive a range of packet sizes,
49 * and can often be made to run out completely. Those issues are important
50 * when stress testing peripheral controller drivers.
51 *
52 *
53 * This is currently packaged as a configuration driver, which can't be
54 * combined with other functions to make composite devices. However, it
55 * can be combined with other independent configurations.
56 */
57struct f_sourcesink {
58 struct usb_function function;
59
60 struct usb_ep *in_ep;
61 struct usb_ep *out_ep;
62 struct timer_list resume;
63};
64
65static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
66{
67 return container_of(f, struct f_sourcesink, function);
68}
69
70static unsigned autoresume;
71module_param(autoresume, uint, 0);
72MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
73
74static unsigned pattern;
75module_param(pattern, uint, 0);
76MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
77
78/*-------------------------------------------------------------------------*/
79
80static struct usb_interface_descriptor source_sink_intf = {
81 .bLength = sizeof source_sink_intf,
82 .bDescriptorType = USB_DT_INTERFACE,
83
84 .bNumEndpoints = 2,
85 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
86 /* .iInterface = DYNAMIC */
87};
88
89/* full speed support: */
90
91static struct usb_endpoint_descriptor fs_source_desc = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94
95 .bEndpointAddress = USB_DIR_IN,
96 .bmAttributes = USB_ENDPOINT_XFER_BULK,
97};
98
99static struct usb_endpoint_descriptor fs_sink_desc = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102
103 .bEndpointAddress = USB_DIR_OUT,
104 .bmAttributes = USB_ENDPOINT_XFER_BULK,
105};
106
107static struct usb_descriptor_header *fs_source_sink_descs[] = {
108 (struct usb_descriptor_header *) &source_sink_intf,
109 (struct usb_descriptor_header *) &fs_sink_desc,
110 (struct usb_descriptor_header *) &fs_source_desc,
111 NULL,
112};
113
114/* high speed support: */
115
116static struct usb_endpoint_descriptor hs_source_desc = {
117 .bLength = USB_DT_ENDPOINT_SIZE,
118 .bDescriptorType = USB_DT_ENDPOINT,
119
120 .bmAttributes = USB_ENDPOINT_XFER_BULK,
121 .wMaxPacketSize = __constant_cpu_to_le16(512),
122};
123
124static struct usb_endpoint_descriptor hs_sink_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127
128 .bmAttributes = USB_ENDPOINT_XFER_BULK,
129 .wMaxPacketSize = __constant_cpu_to_le16(512),
130};
131
132static struct usb_descriptor_header *hs_source_sink_descs[] = {
133 (struct usb_descriptor_header *) &source_sink_intf,
134 (struct usb_descriptor_header *) &hs_source_desc,
135 (struct usb_descriptor_header *) &hs_sink_desc,
136 NULL,
137};
138
139/* function-specific strings: */
140
141static struct usb_string strings_sourcesink[] = {
142 [0].s = "source and sink data",
143 { } /* end of list */
144};
145
146static struct usb_gadget_strings stringtab_sourcesink = {
147 .language = 0x0409, /* en-us */
148 .strings = strings_sourcesink,
149};
150
151static struct usb_gadget_strings *sourcesink_strings[] = {
152 &stringtab_sourcesink,
153 NULL,
154};
155
156/*-------------------------------------------------------------------------*/
157
158static void sourcesink_autoresume(unsigned long _c)
159{
160 struct usb_composite_dev *cdev = (void *)_c;
161 struct usb_gadget *g = cdev->gadget;
162
163 /* Normally the host would be woken up for something
164 * more significant than just a timer firing; likely
165 * because of some direct user request.
166 */
167 if (g->speed != USB_SPEED_UNKNOWN) {
168 int status = usb_gadget_wakeup(g);
169 DBG(cdev, "%s --> %d\n", __func__, status);
170 }
171}
172
173static int __init
174sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
175{
176 struct usb_composite_dev *cdev = c->cdev;
177 struct f_sourcesink *ss = func_to_ss(f);
178 int id;
179
180 /* allocate interface ID(s) */
181 id = usb_interface_id(c, f);
182 if (id < 0)
183 return id;
184 source_sink_intf.bInterfaceNumber = id;
185
186 /* allocate endpoints */
187 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
188 if (!ss->in_ep) {
189autoconf_fail:
190 ERROR(cdev, "%s: can't autoconfigure on %s\n",
191 f->name, cdev->gadget->name);
192 return -ENODEV;
193 }
194 ss->in_ep->driver_data = cdev; /* claim */
195
196 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
197 if (!ss->out_ep)
198 goto autoconf_fail;
199 ss->out_ep->driver_data = cdev; /* claim */
200
201 setup_timer(&ss->resume, sourcesink_autoresume,
202 (unsigned long) c->cdev);
203
204 /* support high speed hardware */
205 if (gadget_is_dualspeed(c->cdev->gadget)) {
206 hs_source_desc.bEndpointAddress =
207 fs_source_desc.bEndpointAddress;
208 hs_sink_desc.bEndpointAddress =
209 fs_sink_desc.bEndpointAddress;
210 f->hs_descriptors = hs_source_sink_descs;
211 }
212
213 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
214 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
215 f->name, ss->in_ep->name, ss->out_ep->name);
216 return 0;
217}
218
219static void
220sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
221{
222 kfree(func_to_ss(f));
223}
224
225/* optionally require specific source/sink data patterns */
226static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
227{
228 unsigned i;
229 u8 *buf = req->buf;
230 struct usb_composite_dev *cdev = ss->function.config->cdev;
231
232 for (i = 0; i < req->actual; i++, buf++) {
233 switch (pattern) {
234
235 /* all-zeroes has no synchronization issues */
236 case 0:
237 if (*buf == 0)
238 continue;
239 break;
240
241 /* "mod63" stays in sync with short-terminated transfers,
242 * OR otherwise when host and gadget agree on how large
243 * each usb transfer request should be. Resync is done
244 * with set_interface or set_config. (We *WANT* it to
245 * get quickly out of sync if controllers or their drivers
246 * stutter for any reason, including buffer duplcation...)
247 */
248 case 1:
249 if (*buf == (u8)(i % 63))
250 continue;
251 break;
252 }
253 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
254 usb_ep_set_halt(ss->out_ep);
255 return -EINVAL;
256 }
257 return 0;
258}
259
260static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
261{
262 unsigned i;
263 u8 *buf = req->buf;
264
265 switch (pattern) {
266 case 0:
267 memset(req->buf, 0, req->length);
268 break;
269 case 1:
270 for (i = 0; i < req->length; i++)
271 *buf++ = (u8) (i % 63);
272 break;
273 }
274}
275
276static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
277{
278 struct f_sourcesink *ss = ep->driver_data;
279 struct usb_composite_dev *cdev = ss->function.config->cdev;
280 int status = req->status;
281
282 switch (status) {
283
284 case 0: /* normal completion? */
285 if (ep == ss->out_ep) {
286 check_read_data(ss, req);
287 memset(req->buf, 0x55, req->length);
288 } else
289 reinit_write_data(ep, req);
290 break;
291
292 /* this endpoint is normally active while we're configured */
293 case -ECONNABORTED: /* hardware forced ep reset */
294 case -ECONNRESET: /* request dequeued */
295 case -ESHUTDOWN: /* disconnect from host */
296 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
297 req->actual, req->length);
298 if (ep == ss->out_ep)
299 check_read_data(ss, req);
300 free_ep_req(ep, req);
301 return;
302
303 case -EOVERFLOW: /* buffer overrun on read means that
304 * we didn't provide a big enough
305 * buffer.
306 */
307 default:
308#if 1
309 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
310 status, req->actual, req->length);
311#endif
312 case -EREMOTEIO: /* short read */
313 break;
314 }
315
316 status = usb_ep_queue(ep, req, GFP_ATOMIC);
317 if (status) {
318 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
319 ep->name, req->length, status);
320 usb_ep_set_halt(ep);
321 /* FIXME recover later ... somehow */
322 }
323}
324
325static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
326{
327 struct usb_ep *ep;
328 struct usb_request *req;
329 int status;
330
331 ep = is_in ? ss->in_ep : ss->out_ep;
332 req = alloc_ep_req(ep);
333 if (!req)
334 return -ENOMEM;
335
336 req->complete = source_sink_complete;
337 if (is_in)
338 reinit_write_data(ep, req);
339 else
340 memset(req->buf, 0x55, req->length);
341
342 status = usb_ep_queue(ep, req, GFP_ATOMIC);
343 if (status) {
344 struct usb_composite_dev *cdev;
345
346 cdev = ss->function.config->cdev;
347 ERROR(cdev, "start %s %s --> %d\n",
348 is_in ? "IN" : "OUT",
349 ep->name, status);
350 free_ep_req(ep, req);
351 }
352
353 return status;
354}
355
356static void disable_source_sink(struct f_sourcesink *ss)
357{
358 struct usb_composite_dev *cdev;
359
360 cdev = ss->function.config->cdev;
361 disable_endpoints(cdev, ss->in_ep, ss->out_ep);
362 del_timer(&ss->resume);
363 VDBG(cdev, "%s disabled\n", ss->function.name);
364}
365
366static int
367enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
368{
369 int result = 0;
370 const struct usb_endpoint_descriptor *src, *sink;
371 struct usb_ep *ep;
372
373 src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc);
374 sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc);
375
376 /* one endpoint writes (sources) zeroes IN (to the host) */
377 ep = ss->in_ep;
378 result = usb_ep_enable(ep, src);
379 if (result < 0)
380 return result;
381 ep->driver_data = ss;
382
383 result = source_sink_start_ep(ss, true);
384 if (result < 0) {
385fail:
386 ep = ss->in_ep;
387 usb_ep_disable(ep);
388 ep->driver_data = NULL;
389 return result;
390 }
391
392 /* one endpoint reads (sinks) anything OUT (from the host) */
393 ep = ss->out_ep;
394 result = usb_ep_enable(ep, sink);
395 if (result < 0)
396 goto fail;
397 ep->driver_data = ss;
398
399 result = source_sink_start_ep(ss, false);
400 if (result < 0) {
401 usb_ep_disable(ep);
402 ep->driver_data = NULL;
403 goto fail;
404 }
405
406 DBG(cdev, "%s enabled\n", ss->function.name);
407 return result;
408}
409
410static int sourcesink_set_alt(struct usb_function *f,
411 unsigned intf, unsigned alt)
412{
413 struct f_sourcesink *ss = func_to_ss(f);
414 struct usb_composite_dev *cdev = f->config->cdev;
415
416 /* we know alt is zero */
417 if (ss->in_ep->driver_data)
418 disable_source_sink(ss);
419 return enable_source_sink(cdev, ss);
420}
421
422static void sourcesink_disable(struct usb_function *f)
423{
424 struct f_sourcesink *ss = func_to_ss(f);
425
426 disable_source_sink(ss);
427}
428
429static void sourcesink_suspend(struct usb_function *f)
430{
431 struct f_sourcesink *ss = func_to_ss(f);
432 struct usb_composite_dev *cdev = f->config->cdev;
433
434 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
435 return;
436
437 if (autoresume) {
438 mod_timer(&ss->resume, jiffies + (HZ * autoresume));
439 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
440 } else
441 DBG(cdev, "%s\n", __func__);
442}
443
444static void sourcesink_resume(struct usb_function *f)
445{
446 struct f_sourcesink *ss = func_to_ss(f);
447 struct usb_composite_dev *cdev = f->config->cdev;
448
449 DBG(cdev, "%s\n", __func__);
450 del_timer(&ss->resume);
451}
452
453/*-------------------------------------------------------------------------*/
454
455static int __init sourcesink_bind_config(struct usb_configuration *c)
456{
457 struct f_sourcesink *ss;
458 int status;
459
460 ss = kzalloc(sizeof *ss, GFP_KERNEL);
461 if (!ss)
462 return -ENOMEM;
463
464 ss->function.name = "source/sink";
465 ss->function.descriptors = fs_source_sink_descs;
466 ss->function.bind = sourcesink_bind;
467 ss->function.unbind = sourcesink_unbind;
468 ss->function.set_alt = sourcesink_set_alt;
469 ss->function.disable = sourcesink_disable;
470 ss->function.suspend = sourcesink_suspend;
471 ss->function.resume = sourcesink_resume;
472
473 status = usb_add_function(c, &ss->function);
474 if (status)
475 kfree(ss);
476 return status;
477}
478
479static int sourcesink_setup(struct usb_configuration *c,
480 const struct usb_ctrlrequest *ctrl)
481{
482 struct usb_request *req = c->cdev->req;
483 int value = -EOPNOTSUPP;
484 u16 w_index = le16_to_cpu(ctrl->wIndex);
485 u16 w_value = le16_to_cpu(ctrl->wValue);
486 u16 w_length = le16_to_cpu(ctrl->wLength);
487
488 /* composite driver infrastructure handles everything except
489 * the two control test requests.
490 */
491 switch (ctrl->bRequest) {
492
493 /*
494 * These are the same vendor-specific requests supported by
495 * Intel's USB 2.0 compliance test devices. We exceed that
496 * device spec by allowing multiple-packet requests.
497 *
498 * NOTE: the Control-OUT data stays in req->buf ... better
499 * would be copying it into a scratch buffer, so that other
500 * requests may safely intervene.
501 */
502 case 0x5b: /* control WRITE test -- fill the buffer */
503 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
504 goto unknown;
505 if (w_value || w_index)
506 break;
507 /* just read that many bytes into the buffer */
508 if (w_length > req->length)
509 break;
510 value = w_length;
511 break;
512 case 0x5c: /* control READ test -- return the buffer */
513 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
514 goto unknown;
515 if (w_value || w_index)
516 break;
517 /* expect those bytes are still in the buffer; send back */
518 if (w_length > req->length)
519 break;
520 value = w_length;
521 break;
522
523 default:
524unknown:
525 VDBG(c->cdev,
526 "unknown control req%02x.%02x v%04x i%04x l%d\n",
527 ctrl->bRequestType, ctrl->bRequest,
528 w_value, w_index, w_length);
529 }
530
531 /* respond with data transfer or status phase? */
532 if (value >= 0) {
533 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
534 ctrl->bRequestType, ctrl->bRequest,
535 w_value, w_index, w_length);
536 req->zero = 0;
537 req->length = value;
538 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
539 if (value < 0)
540 ERROR(c->cdev, "source/sinkc response, err %d\n",
541 value);
542 }
543
544 /* device either stalls (value < 0) or reports success */
545 return value;
546}
547
548static struct usb_configuration sourcesink_driver = {
549 .label = "source/sink",
550 .strings = sourcesink_strings,
551 .bind = sourcesink_bind_config,
552 .setup = sourcesink_setup,
553 .bConfigurationValue = 3,
554 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
555 .bMaxPower = 1, /* 2 mA, minimal */
556 /* .iConfiguration = DYNAMIC */
557};
558
559/**
560 * sourcesink_add - add a source/sink testing configuration to a device
561 * @cdev: the device to support the configuration
562 */
563int __init sourcesink_add(struct usb_composite_dev *cdev)
564{
565 int id;
566
567 /* allocate string ID(s) */
568 id = usb_string_id(cdev);
569 if (id < 0)
570 return id;
571 strings_sourcesink[0].id = id;
572
573 source_sink_intf.iInterface = id;
574 sourcesink_driver.iConfiguration = id;
575
576 /* support autoresume for remote wakeup testing */
577 if (autoresume)
578 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
579
580 /* support OTG systems */
581 if (gadget_is_otg(cdev->gadget)) {
582 sourcesink_driver.descriptors = otg_desc;
583 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
584 }
585
586 return usb_add_config(cdev, &sourcesink_driver);
587}
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c
new file mode 100644
index 000000000000..afeab9a0523f
--- /dev/null
+++ b/drivers/usb/gadget/f_subset.c
@@ -0,0 +1,423 @@
1/*
2 * f_subset.c -- "CDC Subset" Ethernet link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
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#include <linux/kernel.h>
23#include <linux/device.h>
24#include <linux/etherdevice.h>
25
26#include "u_ether.h"
27
28
29/*
30 * This function packages a simple "CDC Subset" Ethernet port with no real
31 * control mechanisms; just raw data transfer over two bulk endpoints.
32 * The data transfer model is exactly that of CDC Ethernet, which is
33 * why we call it the "CDC Subset".
34 *
35 * Because it's not standardized, this has some interoperability issues.
36 * They mostly relate to driver binding, since the data transfer model is
37 * so simple (CDC Ethernet). The original versions of this protocol used
38 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
39 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
40 * daughtercards with USB peripheral connectors. (It was used more often
41 * with other boards, using the Itsy identifiers.) Linux hosts recognized
42 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
43 * and one interface.
44 *
45 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
46 * "SAFE", which happens to have a mode which is identical to the "CDC
47 * Subset" in terms of data transfer and lack of control model. This was
48 * adopted by later Sharp Zaurus models, and by some other software which
49 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
50 *
51 * Because Microsoft's RNDIS drivers are far from robust, we added a few
52 * descriptors to the CDC Subset code, making this code look like a SAFE
53 * implementation. This lets you use MCCI's host side MS-Windows drivers
54 * if you get fed up with RNDIS. It also makes it easier for composite
55 * drivers to work, since they can use class based binding instead of
56 * caring about specific product and vendor IDs.
57 */
58
59struct geth_descs {
60 struct usb_endpoint_descriptor *in;
61 struct usb_endpoint_descriptor *out;
62};
63
64struct f_gether {
65 struct gether port;
66
67 char ethaddr[14];
68
69 struct usb_descriptor_header **fs_function;
70 struct geth_descs fs;
71 struct usb_descriptor_header **hs_function;
72 struct geth_descs hs;
73};
74
75static inline struct f_gether *func_to_geth(struct usb_function *f)
76{
77 return container_of(f, struct f_gether, port.func);
78}
79
80/*-------------------------------------------------------------------------*/
81
82/*
83 * "Simple" CDC-subset option is a simple vendor-neutral model that most
84 * full speed controllers can handle: one interface, two bulk endpoints.
85 * To assist host side drivers, we fancy it up a bit, and add descriptors so
86 * some host side drivers will understand it as a "SAFE" variant.
87 *
88 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
89 * Data endpoints live in the control interface, there's no data interface.
90 * And it's not used to talk to a cell phone radio.
91 */
92
93/* interface descriptor: */
94
95static struct usb_interface_descriptor subset_data_intf __initdata = {
96 .bLength = sizeof subset_data_intf,
97 .bDescriptorType = USB_DT_INTERFACE,
98
99 /* .bInterfaceNumber = DYNAMIC */
100 .bAlternateSetting = 0,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = USB_CLASS_COMM,
103 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
104 .bInterfaceProtocol = 0,
105 /* .iInterface = DYNAMIC */
106};
107
108static struct usb_cdc_header_desc header_desc __initdata = {
109 .bLength = sizeof header_desc,
110 .bDescriptorType = USB_DT_CS_INTERFACE,
111 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
112
113 .bcdCDC = __constant_cpu_to_le16(0x0110),
114};
115
116static struct usb_cdc_mdlm_desc mdlm_desc __initdata = {
117 .bLength = sizeof mdlm_desc,
118 .bDescriptorType = USB_DT_CS_INTERFACE,
119 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
120
121 .bcdVersion = __constant_cpu_to_le16(0x0100),
122 .bGUID = {
123 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
124 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
125 },
126};
127
128/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
129 * can't really use its struct. All we do here is say that we're using
130 * the submode of "SAFE" which directly matches the CDC Subset.
131 */
132static u8 mdlm_detail_desc[] __initdata = {
133 6,
134 USB_DT_CS_INTERFACE,
135 USB_CDC_MDLM_DETAIL_TYPE,
136
137 0, /* "SAFE" */
138 0, /* network control capabilities (none) */
139 0, /* network data capabilities ("raw" encapsulation) */
140};
141
142static struct usb_cdc_ether_desc ether_desc __initdata = {
143 .bLength = sizeof ether_desc,
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
146
147 /* this descriptor actually adds value, surprise! */
148 /* .iMACAddress = DYNAMIC */
149 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
150 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
151 .wNumberMCFilters = __constant_cpu_to_le16(0),
152 .bNumberPowerFilters = 0,
153};
154
155/* full speed support: */
156
157static struct usb_endpoint_descriptor fs_in_desc __initdata = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160
161 .bEndpointAddress = USB_DIR_IN,
162 .bmAttributes = USB_ENDPOINT_XFER_BULK,
163};
164
165static struct usb_endpoint_descriptor fs_out_desc __initdata = {
166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
168
169 .bEndpointAddress = USB_DIR_OUT,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171};
172
173static struct usb_descriptor_header *fs_eth_function[] __initdata = {
174 (struct usb_descriptor_header *) &subset_data_intf,
175 (struct usb_descriptor_header *) &header_desc,
176 (struct usb_descriptor_header *) &mdlm_desc,
177 (struct usb_descriptor_header *) &mdlm_detail_desc,
178 (struct usb_descriptor_header *) &ether_desc,
179 (struct usb_descriptor_header *) &fs_in_desc,
180 (struct usb_descriptor_header *) &fs_out_desc,
181 NULL,
182};
183
184/* high speed support: */
185
186static struct usb_endpoint_descriptor hs_in_desc __initdata = {
187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
189
190 .bmAttributes = USB_ENDPOINT_XFER_BULK,
191 .wMaxPacketSize = __constant_cpu_to_le16(512),
192};
193
194static struct usb_endpoint_descriptor hs_out_desc __initdata = {
195 .bLength = USB_DT_ENDPOINT_SIZE,
196 .bDescriptorType = USB_DT_ENDPOINT,
197
198 .bmAttributes = USB_ENDPOINT_XFER_BULK,
199 .wMaxPacketSize = __constant_cpu_to_le16(512),
200};
201
202static struct usb_descriptor_header *hs_eth_function[] __initdata = {
203 (struct usb_descriptor_header *) &subset_data_intf,
204 (struct usb_descriptor_header *) &header_desc,
205 (struct usb_descriptor_header *) &mdlm_desc,
206 (struct usb_descriptor_header *) &mdlm_detail_desc,
207 (struct usb_descriptor_header *) &ether_desc,
208 (struct usb_descriptor_header *) &hs_in_desc,
209 (struct usb_descriptor_header *) &hs_out_desc,
210 NULL,
211};
212
213/* string descriptors: */
214
215static struct usb_string geth_string_defs[] = {
216 [0].s = "CDC Ethernet Subset/SAFE",
217 [1].s = NULL /* DYNAMIC */,
218 { } /* end of list */
219};
220
221static struct usb_gadget_strings geth_string_table = {
222 .language = 0x0409, /* en-us */
223 .strings = geth_string_defs,
224};
225
226static struct usb_gadget_strings *geth_strings[] = {
227 &geth_string_table,
228 NULL,
229};
230
231/*-------------------------------------------------------------------------*/
232
233static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
234{
235 struct f_gether *geth = func_to_geth(f);
236 struct usb_composite_dev *cdev = f->config->cdev;
237 struct net_device *net;
238
239 /* we know alt == 0, so this is an activation or a reset */
240
241 if (geth->port.in_ep->driver_data) {
242 DBG(cdev, "reset cdc subset\n");
243 gether_disconnect(&geth->port);
244 }
245
246 DBG(cdev, "init + activate cdc subset\n");
247 geth->port.in = ep_choose(cdev->gadget,
248 geth->hs.in, geth->fs.in);
249 geth->port.out = ep_choose(cdev->gadget,
250 geth->hs.out, geth->fs.out);
251
252 net = gether_connect(&geth->port);
253 return IS_ERR(net) ? PTR_ERR(net) : 0;
254}
255
256static void geth_disable(struct usb_function *f)
257{
258 struct f_gether *geth = func_to_geth(f);
259 struct usb_composite_dev *cdev = f->config->cdev;
260
261 DBG(cdev, "net deactivated\n");
262 gether_disconnect(&geth->port);
263}
264
265/*-------------------------------------------------------------------------*/
266
267/* serial function driver setup/binding */
268
269static int __init
270geth_bind(struct usb_configuration *c, struct usb_function *f)
271{
272 struct usb_composite_dev *cdev = c->cdev;
273 struct f_gether *geth = func_to_geth(f);
274 int status;
275 struct usb_ep *ep;
276
277 /* allocate instance-specific interface IDs */
278 status = usb_interface_id(c, f);
279 if (status < 0)
280 goto fail;
281 subset_data_intf.bInterfaceNumber = status;
282
283 status = -ENODEV;
284
285 /* allocate instance-specific endpoints */
286 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
287 if (!ep)
288 goto fail;
289 geth->port.in_ep = ep;
290 ep->driver_data = cdev; /* claim */
291
292 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
293 if (!ep)
294 goto fail;
295 geth->port.out_ep = ep;
296 ep->driver_data = cdev; /* claim */
297
298 /* copy descriptors, and track endpoint copies */
299 f->descriptors = usb_copy_descriptors(fs_eth_function);
300
301 geth->fs.in = usb_find_endpoint(fs_eth_function,
302 f->descriptors, &fs_in_desc);
303 geth->fs.out = usb_find_endpoint(fs_eth_function,
304 f->descriptors, &fs_out_desc);
305
306
307 /* support all relevant hardware speeds... we expect that when
308 * hardware is dual speed, all bulk-capable endpoints work at
309 * both speeds
310 */
311 if (gadget_is_dualspeed(c->cdev->gadget)) {
312 hs_in_desc.bEndpointAddress =
313 fs_in_desc.bEndpointAddress;
314 hs_out_desc.bEndpointAddress =
315 fs_out_desc.bEndpointAddress;
316
317 /* copy descriptors, and track endpoint copies */
318 f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
319
320 geth->hs.in = usb_find_endpoint(hs_eth_function,
321 f->hs_descriptors, &hs_in_desc);
322 geth->hs.out = usb_find_endpoint(hs_eth_function,
323 f->hs_descriptors, &hs_out_desc);
324 }
325
326 /* NOTE: all that is done without knowing or caring about
327 * the network link ... which is unavailable to this code
328 * until we're activated via set_alt().
329 */
330
331 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
332 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
333 geth->port.in_ep->name, geth->port.out_ep->name);
334 return 0;
335
336fail:
337 /* we might as well release our claims on endpoints */
338 if (geth->port.out)
339 geth->port.out_ep->driver_data = NULL;
340 if (geth->port.in)
341 geth->port.in_ep->driver_data = NULL;
342
343 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
344
345 return status;
346}
347
348static void
349geth_unbind(struct usb_configuration *c, struct usb_function *f)
350{
351 if (gadget_is_dualspeed(c->cdev->gadget))
352 usb_free_descriptors(f->hs_descriptors);
353 usb_free_descriptors(f->descriptors);
354 geth_string_defs[1].s = NULL;
355 kfree(func_to_geth(f));
356}
357
358/**
359 * geth_bind_config - add CDC Subset network link to a configuration
360 * @c: the configuration to support the network link
361 * @ethaddr: a buffer in which the ethernet address of the host side
362 * side of the link was recorded
363 * Context: single threaded during gadget setup
364 *
365 * Returns zero on success, else negative errno.
366 *
367 * Caller must have called @gether_setup(). Caller is also responsible
368 * for calling @gether_cleanup() before module unload.
369 */
370int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
371{
372 struct f_gether *geth;
373 int status;
374
375 if (!ethaddr)
376 return -EINVAL;
377
378 /* maybe allocate device-global string IDs */
379 if (geth_string_defs[0].id == 0) {
380
381 /* interface label */
382 status = usb_string_id(c->cdev);
383 if (status < 0)
384 return status;
385 geth_string_defs[0].id = status;
386 subset_data_intf.iInterface = status;
387
388 /* MAC address */
389 status = usb_string_id(c->cdev);
390 if (status < 0)
391 return status;
392 geth_string_defs[1].id = status;
393 ether_desc.iMACAddress = status;
394 }
395
396 /* allocate and initialize one new instance */
397 geth = kzalloc(sizeof *geth, GFP_KERNEL);
398 if (!geth)
399 return -ENOMEM;
400
401 /* export host's Ethernet address in CDC format */
402 snprintf(geth->ethaddr, sizeof geth->ethaddr,
403 "%02X%02X%02X%02X%02X%02X",
404 ethaddr[0], ethaddr[1], ethaddr[2],
405 ethaddr[3], ethaddr[4], ethaddr[5]);
406 geth_string_defs[1].s = geth->ethaddr;
407
408 geth->port.cdc_filter = DEFAULT_FILTER;
409
410 geth->port.func.name = "cdc_subset";
411 geth->port.func.strings = geth_strings;
412 geth->port.func.bind = geth_bind;
413 geth->port.func.unbind = geth_unbind;
414 geth->port.func.set_alt = geth_set_alt;
415 geth->port.func.disable = geth_disable;
416
417 status = usb_add_function(c, &geth->port.func);
418 if (status) {
419 geth_string_defs[1].s = NULL;
420 kfree(geth);
421 }
422 return status;
423}
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 47bb9f09a1aa..15c24edbb61a 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -3867,8 +3867,8 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3867 curlun->dev.parent = &gadget->dev; 3867 curlun->dev.parent = &gadget->dev;
3868 curlun->dev.driver = &fsg_driver.driver; 3868 curlun->dev.driver = &fsg_driver.driver;
3869 dev_set_drvdata(&curlun->dev, fsg); 3869 dev_set_drvdata(&curlun->dev, fsg);
3870 snprintf(curlun->dev.bus_id, BUS_ID_SIZE, 3870 dev_set_name(&curlun->dev,"%s-lun%d",
3871 "%s-lun%d", gadget->dev.bus_id, i); 3871 dev_name(&gadget->dev), i);
3872 3872
3873 if ((rc = device_register(&curlun->dev)) != 0) { 3873 if ((rc = device_register(&curlun->dev)) != 0) {
3874 INFO(fsg, "failed to register LUN%d: %d\n", i, rc); 3874 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
diff --git a/drivers/usb/gadget/fsl_usb2_udc.c b/drivers/usb/gadget/fsl_usb2_udc.c
index 18687543d7fa..1695382f30fe 100644
--- a/drivers/usb/gadget/fsl_usb2_udc.c
+++ b/drivers/usb/gadget/fsl_usb2_udc.c
@@ -2331,7 +2331,7 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2331 udc_controller->gadget.name = driver_name; 2331 udc_controller->gadget.name = driver_name;
2332 2332
2333 /* Setup gadget.dev and register with kernel */ 2333 /* Setup gadget.dev and register with kernel */
2334 strcpy(udc_controller->gadget.dev.bus_id, "gadget"); 2334 dev_set_name(&udc_controller->gadget.dev, "gadget");
2335 udc_controller->gadget.dev.release = fsl_udc_release; 2335 udc_controller->gadget.dev.release = fsl_udc_release;
2336 udc_controller->gadget.dev.parent = &pdev->dev; 2336 udc_controller->gadget.dev.parent = &pdev->dev;
2337 ret = device_register(&udc_controller->gadget.dev); 2337 ret = device_register(&udc_controller->gadget.dev);
diff --git a/drivers/usb/gadget/g_zero.h b/drivers/usb/gadget/g_zero.h
new file mode 100644
index 000000000000..dd2f16ad5a88
--- /dev/null
+++ b/drivers/usb/gadget/g_zero.h
@@ -0,0 +1,25 @@
1/*
2 * This header declares the utility functions used by "Gadget Zero", plus
3 * interfaces to its two single-configuration function drivers.
4 */
5
6#ifndef __G_ZERO_H
7#define __G_ZERO_H
8
9#include <linux/usb/composite.h>
10
11/* global state */
12extern unsigned buflen;
13extern const struct usb_descriptor_header *otg_desc[];
14
15/* common utilities */
16struct usb_request *alloc_ep_req(struct usb_ep *ep);
17void free_ep_req(struct usb_ep *ep, struct usb_request *req);
18void disable_endpoints(struct usb_composite_dev *cdev,
19 struct usb_ep *in, struct usb_ep *out);
20
21/* configuration-specific linkup */
22int sourcesink_add(struct usb_composite_dev *cdev);
23int loopback_add(struct usb_composite_dev *cdev);
24
25#endif /* __G_ZERO_H */
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index ca5149ea7312..5246e8fef2b2 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -214,3 +214,26 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
214 return 0x21; 214 return 0x21;
215 return -ENOENT; 215 return -ENOENT;
216} 216}
217
218
219/**
220 * gadget_supports_altsettings - return true if altsettings work
221 * @gadget: the gadget in question
222 */
223static inline bool gadget_supports_altsettings(struct usb_gadget *gadget)
224{
225 /* PXA 21x/25x/26x has no altsettings at all */
226 if (gadget_is_pxa(gadget))
227 return false;
228
229 /* PXA 27x and 3xx have *broken* altsetting support */
230 if (gadget_is_pxa27x(gadget))
231 return false;
232
233 /* SH3 hardware just doesn't do altsettings */
234 if (gadget_is_sh(gadget))
235 return false;
236
237 /* Everything else is *presumably* fine ... */
238 return true;
239}
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index be6613afedbf..48f1c63b7013 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -1790,7 +1790,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1790 dev->gadget.ops = &goku_ops; 1790 dev->gadget.ops = &goku_ops;
1791 1791
1792 /* the "gadget" abstracts/virtualizes the controller */ 1792 /* the "gadget" abstracts/virtualizes the controller */
1793 strcpy(dev->gadget.dev.bus_id, "gadget"); 1793 dev_set_name(&dev->gadget.dev, "gadget");
1794 dev->gadget.dev.parent = &pdev->dev; 1794 dev->gadget.dev.parent = &pdev->dev;
1795 dev->gadget.dev.dma_mask = pdev->dev.dma_mask; 1795 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1796 dev->gadget.dev.release = gadget_release; 1796 dev->gadget.dev.release = gadget_release;
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index f132a9219e11..04692d59fc1c 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -32,6 +32,7 @@
32#include <asm/uaccess.h> 32#include <asm/uaccess.h>
33#include <linux/slab.h> 33#include <linux/slab.h>
34#include <linux/poll.h> 34#include <linux/poll.h>
35#include <linux/smp_lock.h>
35 36
36#include <linux/device.h> 37#include <linux/device.h>
37#include <linux/moduleparam.h> 38#include <linux/moduleparam.h>
@@ -483,8 +484,7 @@ ep_release (struct inode *inode, struct file *fd)
483 return 0; 484 return 0;
484} 485}
485 486
486static int ep_ioctl (struct inode *inode, struct file *fd, 487static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
487 unsigned code, unsigned long value)
488{ 488{
489 struct ep_data *data = fd->private_data; 489 struct ep_data *data = fd->private_data;
490 int status; 490 int status;
@@ -740,7 +740,7 @@ static const struct file_operations ep_io_operations = {
740 740
741 .read = ep_read, 741 .read = ep_read,
742 .write = ep_write, 742 .write = ep_write,
743 .ioctl = ep_ioctl, 743 .unlocked_ioctl = ep_ioctl,
744 .release = ep_release, 744 .release = ep_release,
745 745
746 .aio_read = ep_aio_read, 746 .aio_read = ep_aio_read,
@@ -1294,15 +1294,18 @@ out:
1294 return mask; 1294 return mask;
1295} 1295}
1296 1296
1297static int dev_ioctl (struct inode *inode, struct file *fd, 1297static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1298 unsigned code, unsigned long value)
1299{ 1298{
1300 struct dev_data *dev = fd->private_data; 1299 struct dev_data *dev = fd->private_data;
1301 struct usb_gadget *gadget = dev->gadget; 1300 struct usb_gadget *gadget = dev->gadget;
1301 long ret = -ENOTTY;
1302 1302
1303 if (gadget->ops->ioctl) 1303 if (gadget->ops->ioctl) {
1304 return gadget->ops->ioctl (gadget, code, value); 1304 lock_kernel();
1305 return -ENOTTY; 1305 ret = gadget->ops->ioctl (gadget, code, value);
1306 unlock_kernel();
1307 }
1308 return ret;
1306} 1309}
1307 1310
1308/* used after device configuration */ 1311/* used after device configuration */
@@ -1314,7 +1317,7 @@ static const struct file_operations ep0_io_operations = {
1314 .write = ep0_write, 1317 .write = ep0_write,
1315 .fasync = ep0_fasync, 1318 .fasync = ep0_fasync,
1316 .poll = ep0_poll, 1319 .poll = ep0_poll,
1317 .ioctl = dev_ioctl, 1320 .unlocked_ioctl = dev_ioctl,
1318 .release = dev_release, 1321 .release = dev_release,
1319}; 1322};
1320 1323
@@ -1964,7 +1967,7 @@ static const struct file_operations dev_init_operations = {
1964 .open = dev_open, 1967 .open = dev_open,
1965 .write = dev_config, 1968 .write = dev_config,
1966 .fasync = ep0_fasync, 1969 .fasync = ep0_fasync,
1967 .ioctl = dev_ioctl, 1970 .unlocked_ioctl = dev_ioctl,
1968 .release = dev_release, 1971 .release = dev_release,
1969}; 1972};
1970 1973
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c
index 825abd2621b3..c6e7df04c69a 100644
--- a/drivers/usb/gadget/lh7a40x_udc.c
+++ b/drivers/usb/gadget/lh7a40x_udc.c
@@ -1970,7 +1970,7 @@ static const struct usb_gadget_ops lh7a40x_udc_ops = {
1970 1970
1971static void nop_release(struct device *dev) 1971static void nop_release(struct device *dev)
1972{ 1972{
1973 DEBUG("%s %s\n", __func__, dev->bus_id); 1973 DEBUG("%s %s\n", __func__, dev_name(dev));
1974} 1974}
1975 1975
1976static struct lh7a40x_udc memory = { 1976static struct lh7a40x_udc memory = {
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c
index ee6b35fa870f..8da7535c0c70 100644
--- a/drivers/usb/gadget/m66592-udc.c
+++ b/drivers/usb/gadget/m66592-udc.c
@@ -1593,7 +1593,7 @@ static int __init m66592_probe(struct platform_device *pdev)
1593 1593
1594 m66592->gadget.ops = &m66592_gadget_ops; 1594 m66592->gadget.ops = &m66592_gadget_ops;
1595 device_initialize(&m66592->gadget.dev); 1595 device_initialize(&m66592->gadget.dev);
1596 strcpy(m66592->gadget.dev.bus_id, "gadget"); 1596 dev_set_name(&m66592->gadget, "gadget");
1597 m66592->gadget.is_dualspeed = 1; 1597 m66592->gadget.is_dualspeed = 1;
1598 m66592->gadget.dev.parent = &pdev->dev; 1598 m66592->gadget.dev.parent = &pdev->dev;
1599 m66592->gadget.dev.dma_mask = pdev->dev.dma_mask; 1599 m66592->gadget.dev.dma_mask = pdev->dev.dma_mask;
diff --git a/drivers/usb/gadget/ndis.h b/drivers/usb/gadget/ndis.h
index 09e3ee4eeae1..df886cec5ef4 100644
--- a/drivers/usb/gadget/ndis.h
+++ b/drivers/usb/gadget/ndis.h
@@ -1,11 +1,11 @@
1/* 1/*
2 * ndis.h 2 * ndis.h
3 * 3 *
4 * ntddndis.h modified by Benedikt Spranger <b.spranger@pengutronix.de> 4 * ntddndis.h modified by Benedikt Spranger <b.spranger@pengutronix.de>
5 * 5 *
6 * Thanks to the cygwin development team, 6 * Thanks to the cygwin development team,
7 * espacially to Casper S. Hornstrup <chorns@users.sourceforge.net> 7 * espacially to Casper S. Hornstrup <chorns@users.sourceforge.net>
8 * 8 *
9 * THIS SOFTWARE IS NOT COPYRIGHTED 9 * THIS SOFTWARE IS NOT COPYRIGHTED
10 * 10 *
11 * This source code is offered for use in the public domain. You may 11 * This source code is offered for use in the public domain. You may
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index e01862300169..b67ab677af72 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -2768,7 +2768,7 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2768 dev->gadget.is_dualspeed = 1; 2768 dev->gadget.is_dualspeed = 1;
2769 2769
2770 /* the "gadget" abstracts/virtualizes the controller */ 2770 /* the "gadget" abstracts/virtualizes the controller */
2771 strcpy (dev->gadget.dev.bus_id, "gadget"); 2771 dev_set_name(&dev->gadget.dev, "gadget");
2772 dev->gadget.dev.parent = &pdev->dev; 2772 dev->gadget.dev.parent = &pdev->dev;
2773 dev->gadget.dev.dma_mask = pdev->dev.dma_mask; 2773 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2774 dev->gadget.dev.release = gadget_release; 2774 dev->gadget.dev.release = gadget_release;
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index 03a7f49d207d..4b79a8509e84 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -2686,7 +2686,7 @@ omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2686 udc->gadget.name = driver_name; 2686 udc->gadget.name = driver_name;
2687 2687
2688 device_initialize(&udc->gadget.dev); 2688 device_initialize(&udc->gadget.dev);
2689 strcpy (udc->gadget.dev.bus_id, "gadget"); 2689 dev_set_name(&udc->gadget.dev, "gadget");
2690 udc->gadget.dev.release = omap_udc_release; 2690 udc->gadget.dev.release = omap_udc_release;
2691 udc->gadget.dev.parent = &odev->dev; 2691 udc->gadget.dev.parent = &odev->dev;
2692 if (use_dma) 2692 if (use_dma)
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index ec8f2eb041ca..9caaec3ae9a3 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -828,9 +828,8 @@ printer_poll(struct file *fd, poll_table *wait)
828 return status; 828 return status;
829} 829}
830 830
831static int 831static long
832printer_ioctl(struct inode *inode, struct file *fd, unsigned int code, 832printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
833 unsigned long arg)
834{ 833{
835 struct printer_dev *dev = fd->private_data; 834 struct printer_dev *dev = fd->private_data;
836 unsigned long flags; 835 unsigned long flags;
@@ -869,7 +868,7 @@ static struct file_operations printer_io_operations = {
869 .write = printer_write, 868 .write = printer_write,
870 .fsync = printer_fsync, 869 .fsync = printer_fsync,
871 .poll = printer_poll, 870 .poll = printer_poll,
872 .ioctl = printer_ioctl, 871 .unlocked_ioctl = printer_ioctl,
873 .release = printer_close 872 .release = printer_close
874}; 873};
875 874
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index 031dceb93023..fbd6289977c8 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -1818,7 +1818,7 @@ pxa25x_udc_irq(int irq, void *_dev)
1818 1818
1819static void nop_release (struct device *dev) 1819static void nop_release (struct device *dev)
1820{ 1820{
1821 DMSG("%s %s\n", __func__, dev->bus_id); 1821 DMSG("%s %s\n", __func__, dev_name(dev));
1822} 1822}
1823 1823
1824/* this uses load-time allocation and initialization (instead of 1824/* this uses load-time allocation and initialization (instead of
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 9c0e82ec5c43..9d447d8cfc0c 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -1575,7 +1575,6 @@ static void udc_enable(struct pxa_udc *udc)
1575{ 1575{
1576 udc_writel(udc, UDCICR0, 0); 1576 udc_writel(udc, UDCICR0, 0);
1577 udc_writel(udc, UDCICR1, 0); 1577 udc_writel(udc, UDCICR1, 0);
1578 udc_writel(udc, UP2OCR, UP2OCR_HXOE);
1579 udc_clear_mask_UDCCR(udc, UDCCR_UDE); 1578 udc_clear_mask_UDCCR(udc, UDCCR_UDE);
1580 1579
1581 clk_enable(udc->clk); 1580 clk_enable(udc->clk);
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c
index d0677f5d3cd5..7228e8562236 100644
--- a/drivers/usb/gadget/rndis.c
+++ b/drivers/usb/gadget/rndis.c
@@ -1,8 +1,6 @@
1/* 1/*
2 * RNDIS MSG parser 2 * RNDIS MSG parser
3 * 3 *
4 * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
5 *
6 * Authors: Benedikt Spranger, Pengutronix 4 * Authors: Benedikt Spranger, Pengutronix
7 * Robert Schwebel, Pengutronix 5 * Robert Schwebel, Pengutronix
8 * 6 *
@@ -30,6 +28,7 @@
30#include <linux/init.h> 28#include <linux/init.h>
31#include <linux/list.h> 29#include <linux/list.h>
32#include <linux/proc_fs.h> 30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
33#include <linux/netdevice.h> 32#include <linux/netdevice.h>
34 33
35#include <asm/io.h> 34#include <asm/io.h>
@@ -38,9 +37,7 @@
38#include <asm/unaligned.h> 37#include <asm/unaligned.h>
39 38
40 39
41#undef RNDIS_PM 40#undef VERBOSE_DEBUG
42#undef RNDIS_WAKEUP
43#undef VERBOSE
44 41
45#include "rndis.h" 42#include "rndis.h"
46 43
@@ -96,9 +93,6 @@ static const u32 oid_supported_list [] =
96 OID_GEN_MAXIMUM_TOTAL_SIZE, 93 OID_GEN_MAXIMUM_TOTAL_SIZE,
97 OID_GEN_MEDIA_CONNECT_STATUS, 94 OID_GEN_MEDIA_CONNECT_STATUS,
98 OID_GEN_PHYSICAL_MEDIUM, 95 OID_GEN_PHYSICAL_MEDIUM,
99#if 0
100 OID_GEN_RNDIS_CONFIG_PARAMETER,
101#endif
102 96
103 /* the statistical stuff */ 97 /* the statistical stuff */
104 OID_GEN_XMIT_OK, 98 OID_GEN_XMIT_OK,
@@ -146,7 +140,14 @@ static const u32 oid_supported_list [] =
146#endif /* RNDIS_OPTIONAL_STATS */ 140#endif /* RNDIS_OPTIONAL_STATS */
147 141
148#ifdef RNDIS_PM 142#ifdef RNDIS_PM
149 /* PM and wakeup are mandatory for USB: */ 143 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
144 * don't say what they mean ... and the NDIS specs are often
145 * confusing and/or ambiguous in this context. (That is, more
146 * so than their specs for the other OIDs.)
147 *
148 * FIXME someone who knows what these should do, please
149 * implement them!
150 */
150 151
151 /* power management */ 152 /* power management */
152 OID_PNP_CAPABILITIES, 153 OID_PNP_CAPABILITIES,
@@ -173,6 +174,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
173 __le32 *outbuf; 174 __le32 *outbuf;
174 int i, count; 175 int i, count;
175 rndis_query_cmplt_type *resp; 176 rndis_query_cmplt_type *resp;
177 struct net_device *net;
178 struct net_device_stats *stats;
176 179
177 if (!r) return -ENOMEM; 180 if (!r) return -ENOMEM;
178 resp = (rndis_query_cmplt_type *) r->buf; 181 resp = (rndis_query_cmplt_type *) r->buf;
@@ -194,6 +197,12 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
194 outbuf = (__le32 *) &resp[1]; 197 outbuf = (__le32 *) &resp[1];
195 resp->InformationBufferOffset = __constant_cpu_to_le32 (16); 198 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
196 199
200 net = rndis_per_dev_params[configNr].dev;
201 if (net->get_stats)
202 stats = net->get_stats(net);
203 else
204 stats = NULL;
205
197 switch (OID) { 206 switch (OID) {
198 207
199 /* general oids (table 4-1) */ 208 /* general oids (table 4-1) */
@@ -350,11 +359,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
350 case OID_GEN_XMIT_OK: 359 case OID_GEN_XMIT_OK:
351 if (rndis_debug > 1) 360 if (rndis_debug > 1)
352 DBG("%s: OID_GEN_XMIT_OK\n", __func__); 361 DBG("%s: OID_GEN_XMIT_OK\n", __func__);
353 if (rndis_per_dev_params [configNr].stats) { 362 if (stats) {
354 *outbuf = cpu_to_le32 ( 363 *outbuf = cpu_to_le32(stats->tx_packets
355 rndis_per_dev_params [configNr].stats->tx_packets - 364 - stats->tx_errors - stats->tx_dropped);
356 rndis_per_dev_params [configNr].stats->tx_errors -
357 rndis_per_dev_params [configNr].stats->tx_dropped);
358 retval = 0; 365 retval = 0;
359 } 366 }
360 break; 367 break;
@@ -363,11 +370,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
363 case OID_GEN_RCV_OK: 370 case OID_GEN_RCV_OK:
364 if (rndis_debug > 1) 371 if (rndis_debug > 1)
365 DBG("%s: OID_GEN_RCV_OK\n", __func__); 372 DBG("%s: OID_GEN_RCV_OK\n", __func__);
366 if (rndis_per_dev_params [configNr].stats) { 373 if (stats) {
367 *outbuf = cpu_to_le32 ( 374 *outbuf = cpu_to_le32(stats->rx_packets
368 rndis_per_dev_params [configNr].stats->rx_packets - 375 - stats->rx_errors - stats->rx_dropped);
369 rndis_per_dev_params [configNr].stats->rx_errors -
370 rndis_per_dev_params [configNr].stats->rx_dropped);
371 retval = 0; 376 retval = 0;
372 } 377 }
373 break; 378 break;
@@ -376,9 +381,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
376 case OID_GEN_XMIT_ERROR: 381 case OID_GEN_XMIT_ERROR:
377 if (rndis_debug > 1) 382 if (rndis_debug > 1)
378 DBG("%s: OID_GEN_XMIT_ERROR\n", __func__); 383 DBG("%s: OID_GEN_XMIT_ERROR\n", __func__);
379 if (rndis_per_dev_params [configNr].stats) { 384 if (stats) {
380 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] 385 *outbuf = cpu_to_le32(stats->tx_errors);
381 .stats->tx_errors);
382 retval = 0; 386 retval = 0;
383 } 387 }
384 break; 388 break;
@@ -387,9 +391,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
387 case OID_GEN_RCV_ERROR: 391 case OID_GEN_RCV_ERROR:
388 if (rndis_debug > 1) 392 if (rndis_debug > 1)
389 DBG("%s: OID_GEN_RCV_ERROR\n", __func__); 393 DBG("%s: OID_GEN_RCV_ERROR\n", __func__);
390 if (rndis_per_dev_params [configNr].stats) { 394 if (stats) {
391 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] 395 *outbuf = cpu_to_le32(stats->rx_errors);
392 .stats->rx_errors);
393 retval = 0; 396 retval = 0;
394 } 397 }
395 break; 398 break;
@@ -397,150 +400,12 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
397 /* mandatory */ 400 /* mandatory */
398 case OID_GEN_RCV_NO_BUFFER: 401 case OID_GEN_RCV_NO_BUFFER:
399 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__); 402 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
400 if (rndis_per_dev_params [configNr].stats) { 403 if (stats) {
401 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] 404 *outbuf = cpu_to_le32(stats->rx_dropped);
402 .stats->rx_dropped);
403 retval = 0;
404 }
405 break;
406
407#ifdef RNDIS_OPTIONAL_STATS
408 case OID_GEN_DIRECTED_BYTES_XMIT:
409 DBG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__);
410 /*
411 * Aunt Tilly's size of shoes
412 * minus antarctica count of penguins
413 * divided by weight of Alpha Centauri
414 */
415 if (rndis_per_dev_params [configNr].stats) {
416 *outbuf = cpu_to_le32 (
417 (rndis_per_dev_params [configNr]
418 .stats->tx_packets -
419 rndis_per_dev_params [configNr]
420 .stats->tx_errors -
421 rndis_per_dev_params [configNr]
422 .stats->tx_dropped)
423 * 123);
424 retval = 0;
425 }
426 break;
427
428 case OID_GEN_DIRECTED_FRAMES_XMIT:
429 DBG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__);
430 /* dito */
431 if (rndis_per_dev_params [configNr].stats) {
432 *outbuf = cpu_to_le32 (
433 (rndis_per_dev_params [configNr]
434 .stats->tx_packets -
435 rndis_per_dev_params [configNr]
436 .stats->tx_errors -
437 rndis_per_dev_params [configNr]
438 .stats->tx_dropped)
439 / 123);
440 retval = 0;
441 }
442 break;
443
444 case OID_GEN_MULTICAST_BYTES_XMIT:
445 DBG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__);
446 if (rndis_per_dev_params [configNr].stats) {
447 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
448 .stats->multicast*1234);
449 retval = 0;
450 }
451 break;
452
453 case OID_GEN_MULTICAST_FRAMES_XMIT:
454 DBG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__);
455 if (rndis_per_dev_params [configNr].stats) {
456 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
457 .stats->multicast);
458 retval = 0;
459 }
460 break;
461
462 case OID_GEN_BROADCAST_BYTES_XMIT:
463 DBG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__);
464 if (rndis_per_dev_params [configNr].stats) {
465 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
466 .stats->tx_packets/42*255);
467 retval = 0;
468 }
469 break;
470
471 case OID_GEN_BROADCAST_FRAMES_XMIT:
472 DBG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__);
473 if (rndis_per_dev_params [configNr].stats) {
474 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
475 .stats->tx_packets/42);
476 retval = 0;
477 }
478 break;
479
480 case OID_GEN_DIRECTED_BYTES_RCV:
481 DBG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__);
482 *outbuf = __constant_cpu_to_le32 (0);
483 retval = 0;
484 break;
485
486 case OID_GEN_DIRECTED_FRAMES_RCV:
487 DBG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__);
488 *outbuf = __constant_cpu_to_le32 (0);
489 retval = 0;
490 break;
491
492 case OID_GEN_MULTICAST_BYTES_RCV:
493 DBG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__);
494 if (rndis_per_dev_params [configNr].stats) {
495 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
496 .stats->multicast * 1111);
497 retval = 0;
498 }
499 break;
500
501 case OID_GEN_MULTICAST_FRAMES_RCV:
502 DBG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__);
503 if (rndis_per_dev_params [configNr].stats) {
504 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
505 .stats->multicast);
506 retval = 0;
507 }
508 break;
509
510 case OID_GEN_BROADCAST_BYTES_RCV:
511 DBG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__);
512 if (rndis_per_dev_params [configNr].stats) {
513 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
514 .stats->rx_packets/42*255);
515 retval = 0;
516 }
517 break;
518
519 case OID_GEN_BROADCAST_FRAMES_RCV:
520 DBG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__);
521 if (rndis_per_dev_params [configNr].stats) {
522 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
523 .stats->rx_packets/42);
524 retval = 0; 405 retval = 0;
525 } 406 }
526 break; 407 break;
527 408
528 case OID_GEN_RCV_CRC_ERROR:
529 DBG("%s: OID_GEN_RCV_CRC_ERROR\n", __func__);
530 if (rndis_per_dev_params [configNr].stats) {
531 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
532 .stats->rx_crc_errors);
533 retval = 0;
534 }
535 break;
536
537 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
538 DBG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__);
539 *outbuf = __constant_cpu_to_le32 (0);
540 retval = 0;
541 break;
542#endif /* RNDIS_OPTIONAL_STATS */
543
544 /* ieee802.3 OIDs (table 4-3) */ 409 /* ieee802.3 OIDs (table 4-3) */
545 410
546 /* mandatory */ 411 /* mandatory */
@@ -592,9 +457,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
592 /* mandatory */ 457 /* mandatory */
593 case OID_802_3_RCV_ERROR_ALIGNMENT: 458 case OID_802_3_RCV_ERROR_ALIGNMENT:
594 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__); 459 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
595 if (rndis_per_dev_params [configNr].stats) { 460 if (stats) {
596 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] 461 *outbuf = cpu_to_le32(stats->rx_frame_errors);
597 .stats->rx_frame_errors);
598 retval = 0; 462 retval = 0;
599 } 463 }
600 break; 464 break;
@@ -613,64 +477,6 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
613 retval = 0; 477 retval = 0;
614 break; 478 break;
615 479
616#ifdef RNDIS_OPTIONAL_STATS
617 case OID_802_3_XMIT_DEFERRED:
618 DBG("%s: OID_802_3_XMIT_DEFERRED\n", __func__);
619 /* TODO */
620 break;
621
622 case OID_802_3_XMIT_MAX_COLLISIONS:
623 DBG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__);
624 /* TODO */
625 break;
626
627 case OID_802_3_RCV_OVERRUN:
628 DBG("%s: OID_802_3_RCV_OVERRUN\n", __func__);
629 /* TODO */
630 break;
631
632 case OID_802_3_XMIT_UNDERRUN:
633 DBG("%s: OID_802_3_XMIT_UNDERRUN\n", __func__);
634 /* TODO */
635 break;
636
637 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
638 DBG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__);
639 /* TODO */
640 break;
641
642 case OID_802_3_XMIT_TIMES_CRS_LOST:
643 DBG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__);
644 /* TODO */
645 break;
646
647 case OID_802_3_XMIT_LATE_COLLISIONS:
648 DBG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__);
649 /* TODO */
650 break;
651#endif /* RNDIS_OPTIONAL_STATS */
652
653#ifdef RNDIS_PM
654 /* power management OIDs (table 4-5) */
655 case OID_PNP_CAPABILITIES:
656 DBG("%s: OID_PNP_CAPABILITIES\n", __func__);
657
658 /* for now, no wakeup capabilities */
659 length = sizeof (struct NDIS_PNP_CAPABILITIES);
660 memset(outbuf, 0, length);
661 retval = 0;
662 break;
663 case OID_PNP_QUERY_POWER:
664 DBG("%s: OID_PNP_QUERY_POWER D%d\n", __func__,
665 get_unaligned_le32(buf) - 1);
666 /* only suspend is a real power state, and
667 * it can't be entered by OID_PNP_SET_POWER...
668 */
669 length = 0;
670 retval = 0;
671 break;
672#endif
673
674 default: 480 default:
675 pr_warning("%s: query unknown OID 0x%08X\n", 481 pr_warning("%s: query unknown OID 0x%08X\n",
676 __func__, OID); 482 __func__, OID);
@@ -726,9 +532,6 @@ static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
726 * what makes the packet flow start and stop, like 532 * what makes the packet flow start and stop, like
727 * activating the CDC Ethernet altsetting. 533 * activating the CDC Ethernet altsetting.
728 */ 534 */
729#ifdef RNDIS_PM
730update_linkstate:
731#endif
732 retval = 0; 535 retval = 0;
733 if (*params->filter) { 536 if (*params->filter) {
734 params->state = RNDIS_DATA_INITIALIZED; 537 params->state = RNDIS_DATA_INITIALIZED;
@@ -747,49 +550,6 @@ update_linkstate:
747 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__); 550 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__);
748 retval = 0; 551 retval = 0;
749 break; 552 break;
750#if 0
751 case OID_GEN_RNDIS_CONFIG_PARAMETER:
752 {
753 struct rndis_config_parameter *param;
754 param = (struct rndis_config_parameter *) buf;
755 DBG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
756 __func__,
757 min(cpu_to_le32(param->ParameterNameLength),80),
758 buf + param->ParameterNameOffset);
759 retval = 0;
760 }
761 break;
762#endif
763
764#ifdef RNDIS_PM
765 case OID_PNP_SET_POWER:
766 /* The only real power state is USB suspend, and RNDIS requests
767 * can't enter it; this one isn't really about power. After
768 * resuming, Windows forces a reset, and then SET_POWER D0.
769 * FIXME ... then things go batty; Windows wedges itself.
770 */
771 i = get_unaligned_le32(buf);
772 DBG("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1);
773 switch (i) {
774 case NdisDeviceStateD0:
775 *params->filter = params->saved_filter;
776 goto update_linkstate;
777 case NdisDeviceStateD3:
778 case NdisDeviceStateD2:
779 case NdisDeviceStateD1:
780 params->saved_filter = *params->filter;
781 retval = 0;
782 break;
783 }
784 break;
785
786#ifdef RNDIS_WAKEUP
787 // no wakeup support advertised, so wakeup OIDs always fail:
788 // - OID_PNP_ENABLE_WAKE_UP
789 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
790#endif
791
792#endif /* RNDIS_PM */
793 553
794 default: 554 default:
795 pr_warning("%s: set unknown OID 0x%08X, size %d\n", 555 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
@@ -807,8 +567,10 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
807{ 567{
808 rndis_init_cmplt_type *resp; 568 rndis_init_cmplt_type *resp;
809 rndis_resp_t *r; 569 rndis_resp_t *r;
570 struct rndis_params *params = rndis_per_dev_params + configNr;
810 571
811 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP; 572 if (!params->dev)
573 return -ENOTSUPP;
812 574
813 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type)); 575 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
814 if (!r) 576 if (!r)
@@ -826,7 +588,7 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
826 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3); 588 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
827 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1); 589 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
828 resp->MaxTransferSize = cpu_to_le32 ( 590 resp->MaxTransferSize = cpu_to_le32 (
829 rndis_per_dev_params [configNr].dev->mtu 591 params->dev->mtu
830 + sizeof (struct ethhdr) 592 + sizeof (struct ethhdr)
831 + sizeof (struct rndis_packet_msg_type) 593 + sizeof (struct rndis_packet_msg_type)
832 + 22); 594 + 22);
@@ -834,10 +596,7 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
834 resp->AFListOffset = __constant_cpu_to_le32 (0); 596 resp->AFListOffset = __constant_cpu_to_le32 (0);
835 resp->AFListSize = __constant_cpu_to_le32 (0); 597 resp->AFListSize = __constant_cpu_to_le32 (0);
836 598
837 if (rndis_per_dev_params [configNr].ack) 599 params->resp_avail(params->v);
838 rndis_per_dev_params [configNr].ack (
839 rndis_per_dev_params [configNr].dev);
840
841 return 0; 600 return 0;
842} 601}
843 602
@@ -845,9 +604,11 @@ static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
845{ 604{
846 rndis_query_cmplt_type *resp; 605 rndis_query_cmplt_type *resp;
847 rndis_resp_t *r; 606 rndis_resp_t *r;
607 struct rndis_params *params = rndis_per_dev_params + configNr;
848 608
849 // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); 609 // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID));
850 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP; 610 if (!params->dev)
611 return -ENOTSUPP;
851 612
852 /* 613 /*
853 * we need more memory: 614 * we need more memory:
@@ -878,9 +639,7 @@ static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
878 } else 639 } else
879 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); 640 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
880 641
881 if (rndis_per_dev_params [configNr].ack) 642 params->resp_avail(params->v);
882 rndis_per_dev_params [configNr].ack (
883 rndis_per_dev_params [configNr].dev);
884 return 0; 643 return 0;
885} 644}
886 645
@@ -889,6 +648,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
889 u32 BufLength, BufOffset; 648 u32 BufLength, BufOffset;
890 rndis_set_cmplt_type *resp; 649 rndis_set_cmplt_type *resp;
891 rndis_resp_t *r; 650 rndis_resp_t *r;
651 struct rndis_params *params = rndis_per_dev_params + configNr;
892 652
893 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type)); 653 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
894 if (!r) 654 if (!r)
@@ -898,7 +658,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
898 BufLength = le32_to_cpu (buf->InformationBufferLength); 658 BufLength = le32_to_cpu (buf->InformationBufferLength);
899 BufOffset = le32_to_cpu (buf->InformationBufferOffset); 659 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
900 660
901#ifdef VERBOSE 661#ifdef VERBOSE_DEBUG
902 DBG("%s: Length: %d\n", __func__, BufLength); 662 DBG("%s: Length: %d\n", __func__, BufLength);
903 DBG("%s: Offset: %d\n", __func__, BufOffset); 663 DBG("%s: Offset: %d\n", __func__, BufOffset);
904 DBG("%s: InfoBuffer: ", __func__); 664 DBG("%s: InfoBuffer: ", __func__);
@@ -919,10 +679,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
919 else 679 else
920 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); 680 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
921 681
922 if (rndis_per_dev_params [configNr].ack) 682 params->resp_avail(params->v);
923 rndis_per_dev_params [configNr].ack (
924 rndis_per_dev_params [configNr].dev);
925
926 return 0; 683 return 0;
927} 684}
928 685
@@ -930,6 +687,7 @@ static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
930{ 687{
931 rndis_reset_cmplt_type *resp; 688 rndis_reset_cmplt_type *resp;
932 rndis_resp_t *r; 689 rndis_resp_t *r;
690 struct rndis_params *params = rndis_per_dev_params + configNr;
933 691
934 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type)); 692 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
935 if (!r) 693 if (!r)
@@ -942,10 +700,7 @@ static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
942 /* resent information */ 700 /* resent information */
943 resp->AddressingReset = __constant_cpu_to_le32 (1); 701 resp->AddressingReset = __constant_cpu_to_le32 (1);
944 702
945 if (rndis_per_dev_params [configNr].ack) 703 params->resp_avail(params->v);
946 rndis_per_dev_params [configNr].ack (
947 rndis_per_dev_params [configNr].dev);
948
949 return 0; 704 return 0;
950} 705}
951 706
@@ -954,6 +709,7 @@ static int rndis_keepalive_response (int configNr,
954{ 709{
955 rndis_keepalive_cmplt_type *resp; 710 rndis_keepalive_cmplt_type *resp;
956 rndis_resp_t *r; 711 rndis_resp_t *r;
712 struct rndis_params *params = rndis_per_dev_params + configNr;
957 713
958 /* host "should" check only in RNDIS_DATA_INITIALIZED state */ 714 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
959 715
@@ -968,10 +724,7 @@ static int rndis_keepalive_response (int configNr,
968 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 724 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
969 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); 725 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
970 726
971 if (rndis_per_dev_params [configNr].ack) 727 params->resp_avail(params->v);
972 rndis_per_dev_params [configNr].ack (
973 rndis_per_dev_params [configNr].dev);
974
975 return 0; 728 return 0;
976} 729}
977 730
@@ -983,8 +736,9 @@ static int rndis_indicate_status_msg (int configNr, u32 status)
983{ 736{
984 rndis_indicate_status_msg_type *resp; 737 rndis_indicate_status_msg_type *resp;
985 rndis_resp_t *r; 738 rndis_resp_t *r;
739 struct rndis_params *params = rndis_per_dev_params + configNr;
986 740
987 if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED) 741 if (params->state == RNDIS_UNINITIALIZED)
988 return -ENOTSUPP; 742 return -ENOTSUPP;
989 743
990 r = rndis_add_response (configNr, 744 r = rndis_add_response (configNr,
@@ -1000,9 +754,7 @@ static int rndis_indicate_status_msg (int configNr, u32 status)
1000 resp->StatusBufferLength = __constant_cpu_to_le32 (0); 754 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1001 resp->StatusBufferOffset = __constant_cpu_to_le32 (0); 755 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
1002 756
1003 if (rndis_per_dev_params [configNr].ack) 757 params->resp_avail(params->v);
1004 rndis_per_dev_params [configNr].ack (
1005 rndis_per_dev_params [configNr].dev);
1006 return 0; 758 return 0;
1007} 759}
1008 760
@@ -1029,7 +781,6 @@ void rndis_uninit (int configNr)
1029 781
1030 if (configNr >= RNDIS_MAX_CONFIGS) 782 if (configNr >= RNDIS_MAX_CONFIGS)
1031 return; 783 return;
1032 rndis_per_dev_params [configNr].used = 0;
1033 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED; 784 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
1034 785
1035 /* drain the response queue */ 786 /* drain the response queue */
@@ -1142,21 +893,25 @@ int rndis_msg_parser (u8 configNr, u8 *buf)
1142 return -ENOTSUPP; 893 return -ENOTSUPP;
1143} 894}
1144 895
1145int rndis_register (int (* rndis_control_ack) (struct net_device *)) 896int rndis_register(void (*resp_avail)(void *v), void *v)
1146{ 897{
1147 u8 i; 898 u8 i;
1148 899
900 if (!resp_avail)
901 return -EINVAL;
902
1149 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) { 903 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1150 if (!rndis_per_dev_params [i].used) { 904 if (!rndis_per_dev_params [i].used) {
1151 rndis_per_dev_params [i].used = 1; 905 rndis_per_dev_params [i].used = 1;
1152 rndis_per_dev_params [i].ack = rndis_control_ack; 906 rndis_per_dev_params [i].resp_avail = resp_avail;
907 rndis_per_dev_params [i].v = v;
1153 DBG("%s: configNr = %d\n", __func__, i); 908 DBG("%s: configNr = %d\n", __func__, i);
1154 return i; 909 return i;
1155 } 910 }
1156 } 911 }
1157 DBG("failed\n"); 912 DBG("failed\n");
1158 913
1159 return -1; 914 return -ENODEV;
1160} 915}
1161 916
1162void rndis_deregister (int configNr) 917void rndis_deregister (int configNr)
@@ -1169,16 +924,14 @@ void rndis_deregister (int configNr)
1169 return; 924 return;
1170} 925}
1171 926
1172int rndis_set_param_dev (u8 configNr, struct net_device *dev, 927int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
1173 struct net_device_stats *stats,
1174 u16 *cdc_filter)
1175{ 928{
1176 DBG("%s:\n", __func__ ); 929 DBG("%s:\n", __func__ );
1177 if (!dev || !stats) return -1; 930 if (!dev)
931 return -EINVAL;
1178 if (configNr >= RNDIS_MAX_CONFIGS) return -1; 932 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1179 933
1180 rndis_per_dev_params [configNr].dev = dev; 934 rndis_per_dev_params [configNr].dev = dev;
1181 rndis_per_dev_params [configNr].stats = stats;
1182 rndis_per_dev_params [configNr].filter = cdc_filter; 935 rndis_per_dev_params [configNr].filter = cdc_filter;
1183 936
1184 return 0; 937 return 0;
@@ -1296,14 +1049,11 @@ int rndis_rm_hdr(struct sk_buff *skb)
1296 1049
1297#ifdef CONFIG_USB_GADGET_DEBUG_FILES 1050#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1298 1051
1299static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof, 1052static int rndis_proc_show(struct seq_file *m, void *v)
1300 void *data)
1301{ 1053{
1302 char *out = page; 1054 rndis_params *param = m->private;
1303 int len;
1304 rndis_params *param = (rndis_params *) data;
1305 1055
1306 out += snprintf (out, count, 1056 seq_printf(m,
1307 "Config Nr. %d\n" 1057 "Config Nr. %d\n"
1308 "used : %s\n" 1058 "used : %s\n"
1309 "state : %s\n" 1059 "state : %s\n"
@@ -1326,25 +1076,13 @@ static int rndis_proc_read (char *page, char **start, off_t off, int count, int
1326 (param->media_state) ? 0 : param->speed*100, 1076 (param->media_state) ? 0 : param->speed*100,
1327 (param->media_state) ? "disconnected" : "connected", 1077 (param->media_state) ? "disconnected" : "connected",
1328 param->vendorID, param->vendorDescr); 1078 param->vendorID, param->vendorDescr);
1329 1079 return 0;
1330 len = out - page;
1331 len -= off;
1332
1333 if (len < count) {
1334 *eof = 1;
1335 if (len <= 0)
1336 return 0;
1337 } else
1338 len = count;
1339
1340 *start = page + off;
1341 return len;
1342} 1080}
1343 1081
1344static int rndis_proc_write (struct file *file, const char __user *buffer, 1082static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1345 unsigned long count, void *data) 1083 size_t count, loff_t *ppos)
1346{ 1084{
1347 rndis_params *p = data; 1085 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
1348 u32 speed = 0; 1086 u32 speed = 0;
1349 int i, fl_speed = 0; 1087 int i, fl_speed = 0;
1350 1088
@@ -1386,6 +1124,20 @@ static int rndis_proc_write (struct file *file, const char __user *buffer,
1386 return count; 1124 return count;
1387} 1125}
1388 1126
1127static int rndis_proc_open(struct inode *inode, struct file *file)
1128{
1129 return single_open(file, rndis_proc_show, PDE(inode)->data);
1130}
1131
1132static const struct file_operations rndis_proc_fops = {
1133 .owner = THIS_MODULE,
1134 .open = rndis_proc_open,
1135 .read = seq_read,
1136 .llseek = seq_lseek,
1137 .release = single_release,
1138 .write = rndis_proc_write,
1139};
1140
1389#define NAME_TEMPLATE "driver/rndis-%03d" 1141#define NAME_TEMPLATE "driver/rndis-%03d"
1390 1142
1391static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS]; 1143static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
@@ -1403,7 +1155,9 @@ int __init rndis_init (void)
1403 1155
1404 sprintf (name, NAME_TEMPLATE, i); 1156 sprintf (name, NAME_TEMPLATE, i);
1405 if (!(rndis_connect_state [i] 1157 if (!(rndis_connect_state [i]
1406 = create_proc_entry (name, 0660, NULL))) 1158 = proc_create_data(name, 0660, NULL,
1159 &rndis_proc_fops,
1160 (void *)(rndis_per_dev_params + i))))
1407 { 1161 {
1408 DBG("%s :remove entries", __func__); 1162 DBG("%s :remove entries", __func__);
1409 while (i) { 1163 while (i) {
@@ -1413,11 +1167,6 @@ int __init rndis_init (void)
1413 DBG("\n"); 1167 DBG("\n");
1414 return -EIO; 1168 return -EIO;
1415 } 1169 }
1416
1417 rndis_connect_state [i]->write_proc = rndis_proc_write;
1418 rndis_connect_state [i]->read_proc = rndis_proc_read;
1419 rndis_connect_state [i]->data = (void *)
1420 (rndis_per_dev_params + i);
1421#endif 1170#endif
1422 rndis_per_dev_params [i].confignr = i; 1171 rndis_per_dev_params [i].confignr = i;
1423 rndis_per_dev_params [i].used = 0; 1172 rndis_per_dev_params [i].used = 0;
diff --git a/drivers/usb/gadget/rndis.h b/drivers/usb/gadget/rndis.h
index 397b149f3ca7..aac61dfe0f03 100644
--- a/drivers/usb/gadget/rndis.h
+++ b/drivers/usb/gadget/rndis.h
@@ -1,8 +1,6 @@
1/* 1/*
2 * RNDIS Definitions for Remote NDIS 2 * RNDIS Definitions for Remote NDIS
3 * 3 *
4 * Version: $Id: rndis.h,v 1.15 2004/03/25 21:33:46 robert Exp $
5 *
6 * Authors: Benedikt Spranger, Pengutronix 4 * Authors: Benedikt Spranger, Pengutronix
7 * Robert Schwebel, Pengutronix 5 * Robert Schwebel, Pengutronix
8 * 6 *
@@ -235,20 +233,19 @@ typedef struct rndis_params
235 const u8 *host_mac; 233 const u8 *host_mac;
236 u16 *filter; 234 u16 *filter;
237 struct net_device *dev; 235 struct net_device *dev;
238 struct net_device_stats *stats;
239 236
240 u32 vendorID; 237 u32 vendorID;
241 const char *vendorDescr; 238 const char *vendorDescr;
242 int (*ack) (struct net_device *); 239 void (*resp_avail)(void *v);
240 void *v;
243 struct list_head resp_queue; 241 struct list_head resp_queue;
244} rndis_params; 242} rndis_params;
245 243
246/* RNDIS Message parser and other useless functions */ 244/* RNDIS Message parser and other useless functions */
247int rndis_msg_parser (u8 configNr, u8 *buf); 245int rndis_msg_parser (u8 configNr, u8 *buf);
248int rndis_register (int (*rndis_control_ack) (struct net_device *)); 246int rndis_register(void (*resp_avail)(void *v), void *v);
249void rndis_deregister (int configNr); 247void rndis_deregister (int configNr);
250int rndis_set_param_dev (u8 configNr, struct net_device *dev, 248int rndis_set_param_dev (u8 configNr, struct net_device *dev,
251 struct net_device_stats *stats,
252 u16 *cdc_filter); 249 u16 *cdc_filter);
253int rndis_set_param_vendor (u8 configNr, u32 vendorID, 250int rndis_set_param_vendor (u8 configNr, u32 vendorID,
254 const char *vendorDescr); 251 const char *vendorDescr);
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index fa019fa73334..b3699afff002 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -1,15 +1,9 @@
1/* 1/*
2 * g_serial.c -- USB gadget serial driver 2 * serial.c -- USB gadget serial driver
3 * 3 *
4 * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com) 4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * 5 * Copyright (C) 2008 by David Brownell
6 * This code is based in part on the Gadget Zero driver, which 6 * Copyright (C) 2008 by Nokia Corporation
7 * is Copyright (C) 2003 by David Brownell, all rights reserved.
8 *
9 * This code also borrows from usbserial.c, which is
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
12 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
13 * 7 *
14 * This software is distributed under the terms of the GNU General 8 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation, 9 * Public License ("GPL") as published by the Free Software Foundation,
@@ -22,2254 +16,237 @@
22#include <linux/tty.h> 16#include <linux/tty.h>
23#include <linux/tty_flip.h> 17#include <linux/tty_flip.h>
24 18
25#include <linux/usb/ch9.h> 19#include "u_serial.h"
26#include <linux/usb/cdc.h>
27#include <linux/usb/gadget.h>
28
29#include "gadget_chips.h" 20#include "gadget_chips.h"
30 21
31 22
32/* Defines */ 23/* Defines */
33 24
34#define GS_VERSION_STR "v2.2" 25#define GS_VERSION_STR "v2.4"
35#define GS_VERSION_NUM 0x2200 26#define GS_VERSION_NUM 0x2400
36 27
37#define GS_LONG_NAME "Gadget Serial" 28#define GS_LONG_NAME "Gadget Serial"
38#define GS_SHORT_NAME "g_serial" 29#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
39
40#define GS_MAJOR 127
41#define GS_MINOR_START 0
42
43/* REVISIT only one port is supported for now;
44 * see gs_{send,recv}_packet() ... no multiplexing,
45 * and no support for multiple ACM devices.
46 */
47#define GS_NUM_PORTS 1
48
49#define GS_NUM_CONFIGS 1
50#define GS_NO_CONFIG_ID 0
51#define GS_BULK_CONFIG_ID 1
52#define GS_ACM_CONFIG_ID 2
53
54#define GS_MAX_NUM_INTERFACES 2
55#define GS_BULK_INTERFACE_ID 0
56#define GS_CONTROL_INTERFACE_ID 0
57#define GS_DATA_INTERFACE_ID 1
58
59#define GS_MAX_DESC_LEN 256
60
61#define GS_DEFAULT_READ_Q_SIZE 32
62#define GS_DEFAULT_WRITE_Q_SIZE 32
63
64#define GS_DEFAULT_WRITE_BUF_SIZE 8192
65#define GS_TMP_BUF_SIZE 8192
66
67#define GS_CLOSE_TIMEOUT 15
68
69#define GS_DEFAULT_USE_ACM 0
70
71/* 9600-8-N-1 ... matches init_termios.c_cflag and defaults
72 * expected by "usbser.sys" on MS-Windows.
73 */
74#define GS_DEFAULT_DTE_RATE 9600
75#define GS_DEFAULT_DATA_BITS 8
76#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
77#define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
78
79/* maxpacket and other transfer characteristics vary by speed. */
80static inline struct usb_endpoint_descriptor *
81choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
82 struct usb_endpoint_descriptor *fs)
83{
84 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
85 return hs;
86 return fs;
87}
88
89
90/* debug settings */
91#ifdef DEBUG
92static int debug = 1;
93#else
94#define debug 0
95#endif
96
97#define gs_debug(format, arg...) \
98 do { if (debug) pr_debug(format, ## arg); } while (0)
99#define gs_debug_level(level, format, arg...) \
100 do { if (debug >= level) pr_debug(format, ## arg); } while (0)
101 30
31/*-------------------------------------------------------------------------*/
102 32
103/* Thanks to NetChip Technologies for donating this product ID. 33/* Thanks to NetChip Technologies for donating this product ID.
104 * 34*
105 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 35* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
106 * Instead: allocate your own, using normal USB-IF procedures. 36* Instead: allocate your own, using normal USB-IF procedures.
107 */ 37*/
108#define GS_VENDOR_ID 0x0525 /* NetChip */ 38#define GS_VENDOR_ID 0x0525 /* NetChip */
109#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ 39#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
110#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ 40#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
111 41
112#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ 42/* string IDs are assigned dynamically */
113#define GS_NOTIFY_MAXPACKET 8
114 43
44#define STRING_MANUFACTURER_IDX 0
45#define STRING_PRODUCT_IDX 1
46#define STRING_DESCRIPTION_IDX 2
115 47
116/* circular buffer */ 48static char manufacturer[50];
117struct gs_buf {
118 unsigned int buf_size;
119 char *buf_buf;
120 char *buf_get;
121 char *buf_put;
122};
123 49
124/* the port structure holds info for each port, one for each minor number */ 50static struct usb_string strings_dev[] = {
125struct gs_port { 51 [STRING_MANUFACTURER_IDX].s = manufacturer,
126 struct gs_dev *port_dev; /* pointer to device struct */ 52 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
127 struct tty_struct *port_tty; /* pointer to tty struct */ 53 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
128 spinlock_t port_lock; 54 { } /* end of list */
129 int port_num;
130 int port_open_count;
131 int port_in_use; /* open/close in progress */
132 wait_queue_head_t port_write_wait;/* waiting to write */
133 struct gs_buf *port_write_buf;
134 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
135 u16 port_handshake_bits;
136#define RS232_RTS (1 << 1)
137#define RS232_DTE (1 << 0)
138}; 55};
139 56
140/* the device structure holds info for the USB device */ 57static struct usb_gadget_strings stringtab_dev = {
141struct gs_dev { 58 .language = 0x0409, /* en-us */
142 struct usb_gadget *dev_gadget; /* gadget device pointer */ 59 .strings = strings_dev,
143 spinlock_t dev_lock; /* lock for set/reset config */
144 int dev_config; /* configuration number */
145 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
146 struct usb_ep *dev_in_ep; /* address of in endpoint */
147 struct usb_ep *dev_out_ep; /* address of out endpoint */
148 struct usb_endpoint_descriptor /* descriptor of notify ep */
149 *dev_notify_ep_desc;
150 struct usb_endpoint_descriptor /* descriptor of in endpoint */
151 *dev_in_ep_desc;
152 struct usb_endpoint_descriptor /* descriptor of out endpoint */
153 *dev_out_ep_desc;
154 struct usb_request *dev_ctrl_req; /* control request */
155 struct list_head dev_req_list; /* list of write requests */
156 int dev_sched_port; /* round robin port scheduled */
157 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
158}; 60};
159 61
160 62static struct usb_gadget_strings *dev_strings[] = {
161/* Functions */ 63 &stringtab_dev,
162 64 NULL,
163/* tty driver internals */
164static int gs_send(struct gs_dev *dev);
165static int gs_send_packet(struct gs_dev *dev, char *packet,
166 unsigned int size);
167static int gs_recv_packet(struct gs_dev *dev, char *packet,
168 unsigned int size);
169static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
170static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
171
172/* gadget driver internals */
173static int gs_set_config(struct gs_dev *dev, unsigned config);
174static void gs_reset_config(struct gs_dev *dev);
175static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
176 u8 type, unsigned int index, int is_otg);
177
178static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
179 gfp_t kmalloc_flags);
180static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
181
182static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
183static void gs_free_ports(struct gs_dev *dev);
184
185/* circular buffer */
186static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
187static void gs_buf_free(struct gs_buf *gb);
188static void gs_buf_clear(struct gs_buf *gb);
189static unsigned int gs_buf_data_avail(struct gs_buf *gb);
190static unsigned int gs_buf_space_avail(struct gs_buf *gb);
191static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
192 unsigned int count);
193static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
194 unsigned int count);
195
196
197/* Globals */
198
199static struct gs_dev *gs_device;
200
201static struct mutex gs_open_close_lock[GS_NUM_PORTS];
202
203
204/*-------------------------------------------------------------------------*/
205
206/* USB descriptors */
207
208#define GS_MANUFACTURER_STR_ID 1
209#define GS_PRODUCT_STR_ID 2
210#define GS_SERIAL_STR_ID 3
211#define GS_BULK_CONFIG_STR_ID 4
212#define GS_ACM_CONFIG_STR_ID 5
213#define GS_CONTROL_STR_ID 6
214#define GS_DATA_STR_ID 7
215
216/* static strings, in UTF-8 */
217static char manufacturer[50];
218static struct usb_string gs_strings[] = {
219 { GS_MANUFACTURER_STR_ID, manufacturer },
220 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
221 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
222 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
223 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
224 { GS_DATA_STR_ID, "Gadget Serial Data" },
225 { } /* end of list */
226};
227
228static struct usb_gadget_strings gs_string_table = {
229 .language = 0x0409, /* en-us */
230 .strings = gs_strings,
231}; 65};
232 66
233static struct usb_device_descriptor gs_device_desc = { 67static struct usb_device_descriptor device_desc = {
234 .bLength = USB_DT_DEVICE_SIZE, 68 .bLength = USB_DT_DEVICE_SIZE,
235 .bDescriptorType = USB_DT_DEVICE, 69 .bDescriptorType = USB_DT_DEVICE,
236 .bcdUSB = __constant_cpu_to_le16(0x0200), 70 .bcdUSB = __constant_cpu_to_le16(0x0200),
71 /* .bDeviceClass = f(use_acm) */
237 .bDeviceSubClass = 0, 72 .bDeviceSubClass = 0,
238 .bDeviceProtocol = 0, 73 .bDeviceProtocol = 0,
74 /* .bMaxPacketSize0 = f(hardware) */
239 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), 75 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
240 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID), 76 /* .idProduct = f(use_acm) */
241 .iManufacturer = GS_MANUFACTURER_STR_ID, 77 /* .bcdDevice = f(hardware) */
242 .iProduct = GS_PRODUCT_STR_ID, 78 /* .iManufacturer = DYNAMIC */
243 .bNumConfigurations = GS_NUM_CONFIGS, 79 /* .iProduct = DYNAMIC */
80 .bNumConfigurations = 1,
244}; 81};
245 82
246static struct usb_otg_descriptor gs_otg_descriptor = { 83static struct usb_otg_descriptor otg_descriptor = {
247 .bLength = sizeof(gs_otg_descriptor), 84 .bLength = sizeof otg_descriptor,
248 .bDescriptorType = USB_DT_OTG, 85 .bDescriptorType = USB_DT_OTG,
249 .bmAttributes = USB_OTG_SRP,
250};
251
252static struct usb_config_descriptor gs_bulk_config_desc = {
253 .bLength = USB_DT_CONFIG_SIZE,
254 .bDescriptorType = USB_DT_CONFIG,
255 /* .wTotalLength computed dynamically */
256 .bNumInterfaces = 1,
257 .bConfigurationValue = GS_BULK_CONFIG_ID,
258 .iConfiguration = GS_BULK_CONFIG_STR_ID,
259 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
260 .bMaxPower = 1,
261};
262
263static struct usb_config_descriptor gs_acm_config_desc = {
264 .bLength = USB_DT_CONFIG_SIZE,
265 .bDescriptorType = USB_DT_CONFIG,
266 /* .wTotalLength computed dynamically */
267 .bNumInterfaces = 2,
268 .bConfigurationValue = GS_ACM_CONFIG_ID,
269 .iConfiguration = GS_ACM_CONFIG_STR_ID,
270 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
271 .bMaxPower = 1,
272};
273
274static const struct usb_interface_descriptor gs_bulk_interface_desc = {
275 .bLength = USB_DT_INTERFACE_SIZE,
276 .bDescriptorType = USB_DT_INTERFACE,
277 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
278 .bNumEndpoints = 2,
279 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
280 .bInterfaceSubClass = 0,
281 .bInterfaceProtocol = 0,
282 .iInterface = GS_DATA_STR_ID,
283};
284
285static const struct usb_interface_descriptor gs_control_interface_desc = {
286 .bLength = USB_DT_INTERFACE_SIZE,
287 .bDescriptorType = USB_DT_INTERFACE,
288 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
289 .bNumEndpoints = 1,
290 .bInterfaceClass = USB_CLASS_COMM,
291 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
292 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
293 .iInterface = GS_CONTROL_STR_ID,
294};
295
296static const struct usb_interface_descriptor gs_data_interface_desc = {
297 .bLength = USB_DT_INTERFACE_SIZE,
298 .bDescriptorType = USB_DT_INTERFACE,
299 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
300 .bNumEndpoints = 2,
301 .bInterfaceClass = USB_CLASS_CDC_DATA,
302 .bInterfaceSubClass = 0,
303 .bInterfaceProtocol = 0,
304 .iInterface = GS_DATA_STR_ID,
305};
306
307static const struct usb_cdc_header_desc gs_header_desc = {
308 .bLength = sizeof(gs_header_desc),
309 .bDescriptorType = USB_DT_CS_INTERFACE,
310 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
311 .bcdCDC = __constant_cpu_to_le16(0x0110),
312};
313
314static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
315 .bLength = sizeof(gs_call_mgmt_descriptor),
316 .bDescriptorType = USB_DT_CS_INTERFACE,
317 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
318 .bmCapabilities = 0,
319 .bDataInterface = 1, /* index of data interface */
320};
321
322static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
323 .bLength = sizeof(gs_acm_descriptor),
324 .bDescriptorType = USB_DT_CS_INTERFACE,
325 .bDescriptorSubType = USB_CDC_ACM_TYPE,
326 .bmCapabilities = (1 << 1),
327};
328
329static const struct usb_cdc_union_desc gs_union_desc = {
330 .bLength = sizeof(gs_union_desc),
331 .bDescriptorType = USB_DT_CS_INTERFACE,
332 .bDescriptorSubType = USB_CDC_UNION_TYPE,
333 .bMasterInterface0 = 0, /* index of control interface */
334 .bSlaveInterface0 = 1, /* index of data interface */
335};
336
337static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
338 .bLength = USB_DT_ENDPOINT_SIZE,
339 .bDescriptorType = USB_DT_ENDPOINT,
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
343 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
344};
345
346static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
347 .bLength = USB_DT_ENDPOINT_SIZE,
348 .bDescriptorType = USB_DT_ENDPOINT,
349 .bEndpointAddress = USB_DIR_IN,
350 .bmAttributes = USB_ENDPOINT_XFER_BULK,
351};
352
353static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
354 .bLength = USB_DT_ENDPOINT_SIZE,
355 .bDescriptorType = USB_DT_ENDPOINT,
356 .bEndpointAddress = USB_DIR_OUT,
357 .bmAttributes = USB_ENDPOINT_XFER_BULK,
358};
359
360static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
361 (struct usb_descriptor_header *) &gs_otg_descriptor,
362 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
363 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
364 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
365 NULL,
366};
367
368static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
369 (struct usb_descriptor_header *) &gs_otg_descriptor,
370 (struct usb_descriptor_header *) &gs_control_interface_desc,
371 (struct usb_descriptor_header *) &gs_header_desc,
372 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
373 (struct usb_descriptor_header *) &gs_acm_descriptor,
374 (struct usb_descriptor_header *) &gs_union_desc,
375 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
376 (struct usb_descriptor_header *) &gs_data_interface_desc,
377 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
378 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
379 NULL,
380};
381 86
382static struct usb_endpoint_descriptor gs_highspeed_notify_desc = { 87 /* REVISIT SRP-only hardware is possible, although
383 .bLength = USB_DT_ENDPOINT_SIZE, 88 * it would not be called "OTG" ...
384 .bDescriptorType = USB_DT_ENDPOINT, 89 */
385 .bEndpointAddress = USB_DIR_IN, 90 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
386 .bmAttributes = USB_ENDPOINT_XFER_INT,
387 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
388 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
389};
390
391static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
392 .bLength = USB_DT_ENDPOINT_SIZE,
393 .bDescriptorType = USB_DT_ENDPOINT,
394 .bmAttributes = USB_ENDPOINT_XFER_BULK,
395 .wMaxPacketSize = __constant_cpu_to_le16(512),
396};
397
398static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
399 .bLength = USB_DT_ENDPOINT_SIZE,
400 .bDescriptorType = USB_DT_ENDPOINT,
401 .bmAttributes = USB_ENDPOINT_XFER_BULK,
402 .wMaxPacketSize = __constant_cpu_to_le16(512),
403};
404
405static struct usb_qualifier_descriptor gs_qualifier_desc = {
406 .bLength = sizeof(struct usb_qualifier_descriptor),
407 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
408 .bcdUSB = __constant_cpu_to_le16 (0x0200),
409 /* assumes ep0 uses the same value for both speeds ... */
410 .bNumConfigurations = GS_NUM_CONFIGS,
411};
412
413static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
414 (struct usb_descriptor_header *) &gs_otg_descriptor,
415 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
416 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
417 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
418 NULL,
419}; 91};
420 92
421static const struct usb_descriptor_header *gs_acm_highspeed_function[] = { 93static const struct usb_descriptor_header *otg_desc[] = {
422 (struct usb_descriptor_header *) &gs_otg_descriptor, 94 (struct usb_descriptor_header *) &otg_descriptor,
423 (struct usb_descriptor_header *) &gs_control_interface_desc,
424 (struct usb_descriptor_header *) &gs_header_desc,
425 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
426 (struct usb_descriptor_header *) &gs_acm_descriptor,
427 (struct usb_descriptor_header *) &gs_union_desc,
428 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
429 (struct usb_descriptor_header *) &gs_data_interface_desc,
430 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
431 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
432 NULL, 95 NULL,
433}; 96};
434 97
435
436/*-------------------------------------------------------------------------*/ 98/*-------------------------------------------------------------------------*/
437 99
438/* Module */ 100/* Module */
439MODULE_DESCRIPTION(GS_LONG_NAME); 101MODULE_DESCRIPTION(GS_VERSION_NAME);
440MODULE_AUTHOR("Al Borchers"); 102MODULE_AUTHOR("Al Borchers");
103MODULE_AUTHOR("David Brownell");
441MODULE_LICENSE("GPL"); 104MODULE_LICENSE("GPL");
442 105
443#ifdef DEBUG 106static int use_acm = true;
444module_param(debug, int, S_IRUGO|S_IWUSR); 107module_param(use_acm, bool, 0);
445MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); 108MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
446#endif
447
448static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
449module_param(read_q_size, uint, S_IRUGO);
450MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
451
452static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
453module_param(write_q_size, uint, S_IRUGO);
454MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
455 109
456static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE; 110static unsigned n_ports = 1;
457module_param(write_buf_size, uint, S_IRUGO); 111module_param(n_ports, uint, 0);
458MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); 112MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
459
460static unsigned int use_acm = GS_DEFAULT_USE_ACM;
461module_param(use_acm, uint, S_IRUGO);
462MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
463 113
464/*-------------------------------------------------------------------------*/ 114/*-------------------------------------------------------------------------*/
465 115
466/* TTY Driver */ 116static int __init serial_bind_config(struct usb_configuration *c)
467
468/*
469 * gs_open
470 */
471static int gs_open(struct tty_struct *tty, struct file *file)
472{
473 int port_num;
474 unsigned long flags;
475 struct gs_port *port;
476 struct gs_dev *dev;
477 struct gs_buf *buf;
478 struct mutex *mtx;
479 int ret;
480
481 port_num = tty->index;
482
483 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
484
485 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
486 pr_err("gs_open: (%d,%p,%p) invalid port number\n",
487 port_num, tty, file);
488 return -ENODEV;
489 }
490
491 dev = gs_device;
492
493 if (dev == NULL) {
494 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
495 port_num, tty, file);
496 return -ENODEV;
497 }
498
499 mtx = &gs_open_close_lock[port_num];
500 if (mutex_lock_interruptible(mtx)) {
501 pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
502 port_num, tty, file);
503 return -ERESTARTSYS;
504 }
505
506 spin_lock_irqsave(&dev->dev_lock, flags);
507
508 if (dev->dev_config == GS_NO_CONFIG_ID) {
509 pr_err("gs_open: (%d,%p,%p) device is not connected\n",
510 port_num, tty, file);
511 ret = -ENODEV;
512 goto exit_unlock_dev;
513 }
514
515 port = dev->dev_port[port_num];
516
517 if (port == NULL) {
518 pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
519 port_num, tty, file);
520 ret = -ENODEV;
521 goto exit_unlock_dev;
522 }
523
524 spin_lock(&port->port_lock);
525 spin_unlock(&dev->dev_lock);
526
527 if (port->port_dev == NULL) {
528 pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
529 port_num, tty, file);
530 ret = -EIO;
531 goto exit_unlock_port;
532 }
533
534 if (port->port_open_count > 0) {
535 ++port->port_open_count;
536 gs_debug("gs_open: (%d,%p,%p) already open\n",
537 port_num, tty, file);
538 ret = 0;
539 goto exit_unlock_port;
540 }
541
542 tty->driver_data = NULL;
543
544 /* mark port as in use, we can drop port lock and sleep if necessary */
545 port->port_in_use = 1;
546
547 /* allocate write buffer on first open */
548 if (port->port_write_buf == NULL) {
549 spin_unlock_irqrestore(&port->port_lock, flags);
550 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
551 spin_lock_irqsave(&port->port_lock, flags);
552
553 /* might have been disconnected while asleep, check */
554 if (port->port_dev == NULL) {
555 pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
556 port_num, tty, file);
557 port->port_in_use = 0;
558 ret = -EIO;
559 goto exit_unlock_port;
560 }
561
562 if ((port->port_write_buf=buf) == NULL) {
563 pr_err("gs_open: (%d,%p,%p) cannot allocate "
564 "port write buffer\n",
565 port_num, tty, file);
566 port->port_in_use = 0;
567 ret = -ENOMEM;
568 goto exit_unlock_port;
569 }
570
571 }
572
573 /* wait for carrier detect (not implemented) */
574
575 /* might have been disconnected while asleep, check */
576 if (port->port_dev == NULL) {
577 pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
578 port_num, tty, file);
579 port->port_in_use = 0;
580 ret = -EIO;
581 goto exit_unlock_port;
582 }
583
584 tty->driver_data = port;
585 port->port_tty = tty;
586 port->port_open_count = 1;
587 port->port_in_use = 0;
588
589 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
590
591 ret = 0;
592
593exit_unlock_port:
594 spin_unlock_irqrestore(&port->port_lock, flags);
595 mutex_unlock(mtx);
596 return ret;
597
598exit_unlock_dev:
599 spin_unlock_irqrestore(&dev->dev_lock, flags);
600 mutex_unlock(mtx);
601 return ret;
602
603}
604
605/*
606 * gs_close
607 */
608
609static int gs_write_finished_event_safely(struct gs_port *p)
610{
611 int cond;
612
613 spin_lock_irq(&(p)->port_lock);
614 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf);
615 spin_unlock_irq(&(p)->port_lock);
616 return cond;
617}
618
619static void gs_close(struct tty_struct *tty, struct file *file)
620{
621 struct gs_port *port = tty->driver_data;
622 struct mutex *mtx;
623
624 if (port == NULL) {
625 pr_err("gs_close: NULL port pointer\n");
626 return;
627 }
628
629 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
630
631 mtx = &gs_open_close_lock[port->port_num];
632 mutex_lock(mtx);
633
634 spin_lock_irq(&port->port_lock);
635
636 if (port->port_open_count == 0) {
637 pr_err("gs_close: (%d,%p,%p) port is already closed\n",
638 port->port_num, tty, file);
639 goto exit;
640 }
641
642 if (port->port_open_count > 1) {
643 --port->port_open_count;
644 goto exit;
645 }
646
647 /* free disconnected port on final close */
648 if (port->port_dev == NULL) {
649 kfree(port);
650 goto exit;
651 }
652
653 /* mark port as closed but in use, we can drop port lock */
654 /* and sleep if necessary */
655 port->port_in_use = 1;
656 port->port_open_count = 0;
657
658 /* wait for write buffer to drain, or */
659 /* at most GS_CLOSE_TIMEOUT seconds */
660 if (gs_buf_data_avail(port->port_write_buf) > 0) {
661 spin_unlock_irq(&port->port_lock);
662 wait_event_interruptible_timeout(port->port_write_wait,
663 gs_write_finished_event_safely(port),
664 GS_CLOSE_TIMEOUT * HZ);
665 spin_lock_irq(&port->port_lock);
666 }
667
668 /* free disconnected port on final close */
669 /* (might have happened during the above sleep) */
670 if (port->port_dev == NULL) {
671 kfree(port);
672 goto exit;
673 }
674
675 gs_buf_clear(port->port_write_buf);
676
677 tty->driver_data = NULL;
678 port->port_tty = NULL;
679 port->port_in_use = 0;
680
681 gs_debug("gs_close: (%d,%p,%p) completed\n",
682 port->port_num, tty, file);
683
684exit:
685 spin_unlock_irq(&port->port_lock);
686 mutex_unlock(mtx);
687}
688
689/*
690 * gs_write
691 */
692static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
693{ 117{
694 unsigned long flags; 118 unsigned i;
695 struct gs_port *port = tty->driver_data; 119 int status = 0;
696 int ret;
697
698 if (port == NULL) {
699 pr_err("gs_write: NULL port pointer\n");
700 return -EIO;
701 }
702
703 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
704 count);
705
706 if (count == 0)
707 return 0;
708
709 spin_lock_irqsave(&port->port_lock, flags);
710
711 if (port->port_dev == NULL) {
712 pr_err("gs_write: (%d,%p) port is not connected\n",
713 port->port_num, tty);
714 ret = -EIO;
715 goto exit;
716 }
717
718 if (port->port_open_count == 0) {
719 pr_err("gs_write: (%d,%p) port is closed\n",
720 port->port_num, tty);
721 ret = -EBADF;
722 goto exit;
723 }
724
725 count = gs_buf_put(port->port_write_buf, buf, count);
726
727 spin_unlock_irqrestore(&port->port_lock, flags);
728
729 gs_send(gs_device);
730
731 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
732 count);
733 120
734 return count; 121 for (i = 0; i < n_ports && status == 0; i++) {
735 122 if (use_acm)
736exit: 123 status = acm_bind_config(c, i);
737 spin_unlock_irqrestore(&port->port_lock, flags); 124 else
738 return ret; 125 status = gser_bind_config(c, i);
739}
740
741/*
742 * gs_put_char
743 */
744static int gs_put_char(struct tty_struct *tty, unsigned char ch)
745{
746 unsigned long flags;
747 struct gs_port *port = tty->driver_data;
748 int ret = 0;
749
750 if (port == NULL) {
751 pr_err("gs_put_char: NULL port pointer\n");
752 return 0;
753 }
754
755 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
756 port->port_num, tty, ch, __builtin_return_address(0));
757
758 spin_lock_irqsave(&port->port_lock, flags);
759
760 if (port->port_dev == NULL) {
761 pr_err("gs_put_char: (%d,%p) port is not connected\n",
762 port->port_num, tty);
763 goto exit;
764 }
765
766 if (port->port_open_count == 0) {
767 pr_err("gs_put_char: (%d,%p) port is closed\n",
768 port->port_num, tty);
769 goto exit;
770 }
771
772 ret = gs_buf_put(port->port_write_buf, &ch, 1);
773
774exit:
775 spin_unlock_irqrestore(&port->port_lock, flags);
776 return ret;
777}
778
779/*
780 * gs_flush_chars
781 */
782static void gs_flush_chars(struct tty_struct *tty)
783{
784 unsigned long flags;
785 struct gs_port *port = tty->driver_data;
786
787 if (port == NULL) {
788 pr_err("gs_flush_chars: NULL port pointer\n");
789 return;
790 }
791
792 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
793
794 spin_lock_irqsave(&port->port_lock, flags);
795
796 if (port->port_dev == NULL) {
797 pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
798 port->port_num, tty);
799 goto exit;
800 }
801
802 if (port->port_open_count == 0) {
803 pr_err("gs_flush_chars: (%d,%p) port is closed\n",
804 port->port_num, tty);
805 goto exit;
806 }
807
808 spin_unlock_irqrestore(&port->port_lock, flags);
809
810 gs_send(gs_device);
811
812 return;
813
814exit:
815 spin_unlock_irqrestore(&port->port_lock, flags);
816}
817
818/*
819 * gs_write_room
820 */
821static int gs_write_room(struct tty_struct *tty)
822{
823
824 int room = 0;
825 unsigned long flags;
826 struct gs_port *port = tty->driver_data;
827
828
829 if (port == NULL)
830 return 0;
831
832 spin_lock_irqsave(&port->port_lock, flags);
833
834 if (port->port_dev != NULL && port->port_open_count > 0
835 && port->port_write_buf != NULL)
836 room = gs_buf_space_avail(port->port_write_buf);
837
838 spin_unlock_irqrestore(&port->port_lock, flags);
839
840 gs_debug("gs_write_room: (%d,%p) room=%d\n",
841 port->port_num, tty, room);
842
843 return room;
844}
845
846/*
847 * gs_chars_in_buffer
848 */
849static int gs_chars_in_buffer(struct tty_struct *tty)
850{
851 int chars = 0;
852 unsigned long flags;
853 struct gs_port *port = tty->driver_data;
854
855 if (port == NULL)
856 return 0;
857
858 spin_lock_irqsave(&port->port_lock, flags);
859
860 if (port->port_dev != NULL && port->port_open_count > 0
861 && port->port_write_buf != NULL)
862 chars = gs_buf_data_avail(port->port_write_buf);
863
864 spin_unlock_irqrestore(&port->port_lock, flags);
865
866 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
867 port->port_num, tty, chars);
868
869 return chars;
870}
871
872/*
873 * gs_throttle
874 */
875static void gs_throttle(struct tty_struct *tty)
876{
877}
878
879/*
880 * gs_unthrottle
881 */
882static void gs_unthrottle(struct tty_struct *tty)
883{
884}
885
886/*
887 * gs_break
888 */
889static void gs_break(struct tty_struct *tty, int break_state)
890{
891}
892
893/*
894 * gs_ioctl
895 */
896static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
897{
898 struct gs_port *port = tty->driver_data;
899
900 if (port == NULL) {
901 pr_err("gs_ioctl: NULL port pointer\n");
902 return -EIO;
903 } 126 }
904 127 return status;
905 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
906 port->port_num, tty, file, cmd, arg);
907
908 /* handle ioctls */
909
910 /* could not handle ioctl */
911 return -ENOIOCTLCMD;
912}
913
914/*
915 * gs_set_termios
916 */
917static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
918{
919} 128}
920 129
921static const struct tty_operations gs_tty_ops = { 130static struct usb_configuration serial_config_driver = {
922 .open = gs_open, 131 /* .label = f(use_acm) */
923 .close = gs_close, 132 .bind = serial_bind_config,
924 .write = gs_write, 133 /* .bConfigurationValue = f(use_acm) */
925 .put_char = gs_put_char, 134 /* .iConfiguration = DYNAMIC */
926 .flush_chars = gs_flush_chars, 135 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
927 .write_room = gs_write_room, 136 .bMaxPower = 1, /* 2 mA, minimal */
928 .ioctl = gs_ioctl,
929 .set_termios = gs_set_termios,
930 .throttle = gs_throttle,
931 .unthrottle = gs_unthrottle,
932 .break_ctl = gs_break,
933 .chars_in_buffer = gs_chars_in_buffer,
934}; 137};
935 138
936/*-------------------------------------------------------------------------*/ 139static int __init gs_bind(struct usb_composite_dev *cdev)
937
938/*
939* gs_send
940*
941* This function finds available write requests, calls
942* gs_send_packet to fill these packets with data, and
943* continues until either there are no more write requests
944* available or no more data to send. This function is
945* run whenever data arrives or write requests are available.
946*/
947static int gs_send(struct gs_dev *dev)
948{
949 int ret,len;
950 unsigned long flags;
951 struct usb_ep *ep;
952 struct usb_request *req;
953
954 if (dev == NULL) {
955 pr_err("gs_send: NULL device pointer\n");
956 return -ENODEV;
957 }
958
959 spin_lock_irqsave(&dev->dev_lock, flags);
960
961 ep = dev->dev_in_ep;
962
963 while(!list_empty(&dev->dev_req_list)) {
964
965 req = list_entry(dev->dev_req_list.next,
966 struct usb_request, list);
967
968 len = gs_send_packet(dev, req->buf, ep->maxpacket);
969
970 if (len > 0) {
971 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
972 "0x%2.2x 0x%2.2x ...\n", len,
973 *((unsigned char *)req->buf),
974 *((unsigned char *)req->buf+1),
975 *((unsigned char *)req->buf+2));
976 list_del(&req->list);
977 req->length = len;
978 spin_unlock_irqrestore(&dev->dev_lock, flags);
979 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
980 pr_err(
981 "gs_send: cannot queue read request, ret=%d\n",
982 ret);
983 spin_lock_irqsave(&dev->dev_lock, flags);
984 break;
985 }
986 spin_lock_irqsave(&dev->dev_lock, flags);
987 } else {
988 break;
989 }
990
991 }
992
993 spin_unlock_irqrestore(&dev->dev_lock, flags);
994
995 return 0;
996}
997
998/*
999 * gs_send_packet
1000 *
1001 * If there is data to send, a packet is built in the given
1002 * buffer and the size is returned. If there is no data to
1003 * send, 0 is returned. If there is any error a negative
1004 * error number is returned.
1005 *
1006 * Called during USB completion routine, on interrupt time.
1007 *
1008 * We assume that disconnect will not happen until all completion
1009 * routines have completed, so we can assume that the dev_port
1010 * array does not change during the lifetime of this function.
1011 */
1012static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1013{
1014 unsigned int len;
1015 struct gs_port *port;
1016
1017 /* TEMPORARY -- only port 0 is supported right now */
1018 port = dev->dev_port[0];
1019
1020 if (port == NULL) {
1021 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1022 return -EIO;
1023 }
1024
1025 spin_lock(&port->port_lock);
1026
1027 len = gs_buf_data_avail(port->port_write_buf);
1028 if (len < size)
1029 size = len;
1030
1031 if (size == 0)
1032 goto exit;
1033
1034 size = gs_buf_get(port->port_write_buf, packet, size);
1035
1036 if (port->port_tty)
1037 wake_up_interruptible(&port->port_tty->write_wait);
1038
1039exit:
1040 spin_unlock(&port->port_lock);
1041 return size;
1042}
1043
1044/*
1045 * gs_recv_packet
1046 *
1047 * Called for each USB packet received. Reads the packet
1048 * header and stuffs the data in the appropriate tty buffer.
1049 * Returns 0 if successful, or a negative error number.
1050 *
1051 * Called during USB completion routine, on interrupt time.
1052 *
1053 * We assume that disconnect will not happen until all completion
1054 * routines have completed, so we can assume that the dev_port
1055 * array does not change during the lifetime of this function.
1056 */
1057static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1058{
1059 unsigned int len;
1060 struct gs_port *port;
1061 int ret;
1062 struct tty_struct *tty;
1063
1064 /* TEMPORARY -- only port 0 is supported right now */
1065 port = dev->dev_port[0];
1066
1067 if (port == NULL) {
1068 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1069 port->port_num);
1070 return -EIO;
1071 }
1072
1073 spin_lock(&port->port_lock);
1074
1075 if (port->port_open_count == 0) {
1076 pr_err("gs_recv_packet: port=%d, port is closed\n",
1077 port->port_num);
1078 ret = -EIO;
1079 goto exit;
1080 }
1081
1082
1083 tty = port->port_tty;
1084
1085 if (tty == NULL) {
1086 pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
1087 port->port_num);
1088 ret = -EIO;
1089 goto exit;
1090 }
1091
1092 if (port->port_tty->magic != TTY_MAGIC) {
1093 pr_err("gs_recv_packet: port=%d, bad tty magic\n",
1094 port->port_num);
1095 ret = -EIO;
1096 goto exit;
1097 }
1098
1099 len = tty_buffer_request_room(tty, size);
1100 if (len > 0) {
1101 tty_insert_flip_string(tty, packet, len);
1102 tty_flip_buffer_push(port->port_tty);
1103 wake_up_interruptible(&port->port_tty->read_wait);
1104 }
1105 ret = 0;
1106exit:
1107 spin_unlock(&port->port_lock);
1108 return ret;
1109}
1110
1111/*
1112* gs_read_complete
1113*/
1114static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1115{ 140{
1116 int ret; 141 int gcnum;
1117 struct gs_dev *dev = ep->driver_data; 142 struct usb_gadget *gadget = cdev->gadget;
1118 143 int status;
1119 if (dev == NULL) {
1120 pr_err("gs_read_complete: NULL device pointer\n");
1121 return;
1122 }
1123 144
1124 switch(req->status) { 145 status = gserial_setup(cdev->gadget, n_ports);
1125 case 0: 146 if (status < 0)
1126 /* normal completion */ 147 return status;
1127 gs_recv_packet(dev, req->buf, req->actual);
1128requeue:
1129 req->length = ep->maxpacket;
1130 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1131 pr_err(
1132 "gs_read_complete: cannot queue read request, ret=%d\n",
1133 ret);
1134 }
1135 break;
1136
1137 case -ESHUTDOWN:
1138 /* disconnect */
1139 gs_debug("gs_read_complete: shutdown\n");
1140 gs_free_req(ep, req);
1141 break;
1142
1143 default:
1144 /* unexpected */
1145 pr_err(
1146 "gs_read_complete: unexpected status error, status=%d\n",
1147 req->status);
1148 goto requeue;
1149 break;
1150 }
1151}
1152
1153/*
1154* gs_write_complete
1155*/
1156static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1157{
1158 struct gs_dev *dev = ep->driver_data;
1159
1160 if (dev == NULL) {
1161 pr_err("gs_write_complete: NULL device pointer\n");
1162 return;
1163 }
1164 148
1165 switch(req->status) { 149 /* Allocate string descriptor numbers ... note that string
1166 case 0: 150 * contents can be overridden by the composite_dev glue.
1167 /* normal completion */ 151 */
1168requeue:
1169 spin_lock(&dev->dev_lock);
1170 list_add(&req->list, &dev->dev_req_list);
1171 spin_unlock(&dev->dev_lock);
1172
1173 gs_send(dev);
1174
1175 break;
1176
1177 case -ESHUTDOWN:
1178 /* disconnect */
1179 gs_debug("gs_write_complete: shutdown\n");
1180 gs_free_req(ep, req);
1181 break;
1182
1183 default:
1184 pr_err(
1185 "gs_write_complete: unexpected status error, status=%d\n",
1186 req->status);
1187 goto requeue;
1188 break;
1189 }
1190}
1191 152
1192/*-------------------------------------------------------------------------*/ 153 /* device description: manufacturer, product */
154 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
155 init_utsname()->sysname, init_utsname()->release,
156 gadget->name);
157 status = usb_string_id(cdev);
158 if (status < 0)
159 goto fail;
160 strings_dev[STRING_MANUFACTURER_IDX].id = status;
1193 161
1194/* Gadget Driver */ 162 device_desc.iManufacturer = status;
1195 163
1196/* 164 status = usb_string_id(cdev);
1197 * gs_unbind 165 if (status < 0)
1198 * 166 goto fail;
1199 * Called on module unload. Frees the control request and device 167 strings_dev[STRING_PRODUCT_IDX].id = status;
1200 * structure.
1201 */
1202static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1203{
1204 struct gs_dev *dev = get_gadget_data(gadget);
1205 168
1206 gs_device = NULL; 169 device_desc.iProduct = status;
1207 170
1208 /* read/write requests already freed, only control request remains */ 171 /* config description */
1209 if (dev != NULL) { 172 status = usb_string_id(cdev);
1210 if (dev->dev_ctrl_req != NULL) { 173 if (status < 0)
1211 gs_free_req(gadget->ep0, dev->dev_ctrl_req); 174 goto fail;
1212 dev->dev_ctrl_req = NULL; 175 strings_dev[STRING_DESCRIPTION_IDX].id = status;
1213 }
1214 gs_reset_config(dev);
1215 gs_free_ports(dev);
1216 kfree(dev);
1217 set_gadget_data(gadget, NULL);
1218 }
1219 176
1220 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME, 177 serial_config_driver.iConfiguration = status;
1221 GS_VERSION_STR);
1222}
1223
1224/*
1225 * gs_bind
1226 *
1227 * Called on module load. Allocates and initializes the device
1228 * structure and a control request.
1229 */
1230static int __init gs_bind(struct usb_gadget *gadget)
1231{
1232 int ret;
1233 struct usb_ep *ep;
1234 struct gs_dev *dev;
1235 int gcnum;
1236
1237 /* Some controllers can't support CDC ACM:
1238 * - sh doesn't support multiple interfaces or configs;
1239 * - sa1100 doesn't have a third interrupt endpoint
1240 */
1241 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1242 use_acm = 0;
1243 178
179 /* set up other descriptors */
1244 gcnum = usb_gadget_controller_number(gadget); 180 gcnum = usb_gadget_controller_number(gadget);
1245 if (gcnum >= 0) 181 if (gcnum >= 0)
1246 gs_device_desc.bcdDevice = 182 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
1247 cpu_to_le16(GS_VERSION_NUM | gcnum);
1248 else { 183 else {
184 /* this is so simple (for now, no altsettings) that it
185 * SHOULD NOT have problems with bulk-capable hardware.
186 * so warn about unrcognized controllers -- don't panic.
187 *
188 * things like configuration and altsetting numbering
189 * can need hardware-specific attention though.
190 */
1249 pr_warning("gs_bind: controller '%s' not recognized\n", 191 pr_warning("gs_bind: controller '%s' not recognized\n",
1250 gadget->name); 192 gadget->name);
1251 /* unrecognized, but safe unless bulk is REALLY quirky */ 193 device_desc.bcdDevice =
1252 gs_device_desc.bcdDevice = 194 __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
1253 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1254 }
1255
1256 dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1257 if (dev == NULL)
1258 return -ENOMEM;
1259
1260 usb_ep_autoconfig_reset(gadget);
1261
1262 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1263 if (!ep)
1264 goto autoconf_fail;
1265 dev->dev_in_ep = ep;
1266 ep->driver_data = dev; /* claim the endpoint */
1267
1268 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1269 if (!ep)
1270 goto autoconf_fail;
1271 dev->dev_out_ep = ep;
1272 ep->driver_data = dev; /* claim the endpoint */
1273
1274 if (use_acm) {
1275 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1276 if (!ep) {
1277 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
1278 goto autoconf_fail;
1279 }
1280 gs_device_desc.idProduct = __constant_cpu_to_le16(
1281 GS_CDC_PRODUCT_ID),
1282 dev->dev_notify_ep = ep;
1283 ep->driver_data = dev; /* claim the endpoint */
1284 }
1285
1286 gs_device_desc.bDeviceClass = use_acm
1287 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1288 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1289
1290 if (gadget_is_dualspeed(gadget)) {
1291 gs_qualifier_desc.bDeviceClass = use_acm
1292 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1293 /* assume ep0 uses the same packet size for both speeds */
1294 gs_qualifier_desc.bMaxPacketSize0 =
1295 gs_device_desc.bMaxPacketSize0;
1296 /* assume endpoints are dual-speed */
1297 gs_highspeed_notify_desc.bEndpointAddress =
1298 gs_fullspeed_notify_desc.bEndpointAddress;
1299 gs_highspeed_in_desc.bEndpointAddress =
1300 gs_fullspeed_in_desc.bEndpointAddress;
1301 gs_highspeed_out_desc.bEndpointAddress =
1302 gs_fullspeed_out_desc.bEndpointAddress;
1303 }
1304
1305 usb_gadget_set_selfpowered(gadget);
1306
1307 if (gadget_is_otg(gadget)) {
1308 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1309 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1310 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1311 } 195 }
1312 196
1313 gs_device = dev; 197 if (gadget_is_otg(cdev->gadget)) {
1314 198 serial_config_driver.descriptors = otg_desc;
1315 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", 199 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1316 init_utsname()->sysname, init_utsname()->release,
1317 gadget->name);
1318
1319 dev->dev_gadget = gadget;
1320 spin_lock_init(&dev->dev_lock);
1321 INIT_LIST_HEAD(&dev->dev_req_list);
1322 set_gadget_data(gadget, dev);
1323
1324 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1325 pr_err("gs_bind: cannot allocate ports\n");
1326 gs_unbind(gadget);
1327 return ret;
1328 } 200 }
1329 201
1330 /* preallocate control response and buffer */ 202 /* register our configuration */
1331 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, 203 status = usb_add_config(cdev, &serial_config_driver);
1332 GFP_KERNEL); 204 if (status < 0)
1333 if (dev->dev_ctrl_req == NULL) { 205 goto fail;
1334 gs_unbind(gadget);
1335 return -ENOMEM;
1336 }
1337 gadget->ep0->driver_data = dev;
1338 206
1339 pr_info("gs_bind: %s %s bound\n", 207 INFO(cdev, "%s\n", GS_VERSION_NAME);
1340 GS_LONG_NAME, GS_VERSION_STR);
1341 208
1342 return 0; 209 return 0;
1343 210
1344autoconf_fail: 211fail:
1345 kfree(dev); 212 gserial_cleanup();
1346 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name); 213 return status;
1347 return -ENODEV;
1348}
1349
1350static int gs_setup_standard(struct usb_gadget *gadget,
1351 const struct usb_ctrlrequest *ctrl)
1352{
1353 int ret = -EOPNOTSUPP;
1354 struct gs_dev *dev = get_gadget_data(gadget);
1355 struct usb_request *req = dev->dev_ctrl_req;
1356 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1357 u16 wValue = le16_to_cpu(ctrl->wValue);
1358 u16 wLength = le16_to_cpu(ctrl->wLength);
1359
1360 switch (ctrl->bRequest) {
1361 case USB_REQ_GET_DESCRIPTOR:
1362 if (ctrl->bRequestType != USB_DIR_IN)
1363 break;
1364
1365 switch (wValue >> 8) {
1366 case USB_DT_DEVICE:
1367 ret = min(wLength,
1368 (u16)sizeof(struct usb_device_descriptor));
1369 memcpy(req->buf, &gs_device_desc, ret);
1370 break;
1371
1372 case USB_DT_DEVICE_QUALIFIER:
1373 if (!gadget_is_dualspeed(gadget))
1374 break;
1375 ret = min(wLength,
1376 (u16)sizeof(struct usb_qualifier_descriptor));
1377 memcpy(req->buf, &gs_qualifier_desc, ret);
1378 break;
1379
1380 case USB_DT_OTHER_SPEED_CONFIG:
1381 if (!gadget_is_dualspeed(gadget))
1382 break;
1383 /* fall through */
1384 case USB_DT_CONFIG:
1385 ret = gs_build_config_buf(req->buf, gadget,
1386 wValue >> 8, wValue & 0xff,
1387 gadget_is_otg(gadget));
1388 if (ret >= 0)
1389 ret = min(wLength, (u16)ret);
1390 break;
1391
1392 case USB_DT_STRING:
1393 /* wIndex == language code. */
1394 ret = usb_gadget_get_string(&gs_string_table,
1395 wValue & 0xff, req->buf);
1396 if (ret >= 0)
1397 ret = min(wLength, (u16)ret);
1398 break;
1399 }
1400 break;
1401
1402 case USB_REQ_SET_CONFIGURATION:
1403 if (ctrl->bRequestType != 0)
1404 break;
1405 spin_lock(&dev->dev_lock);
1406 ret = gs_set_config(dev, wValue);
1407 spin_unlock(&dev->dev_lock);
1408 break;
1409
1410 case USB_REQ_GET_CONFIGURATION:
1411 if (ctrl->bRequestType != USB_DIR_IN)
1412 break;
1413 *(u8 *)req->buf = dev->dev_config;
1414 ret = min(wLength, (u16)1);
1415 break;
1416
1417 case USB_REQ_SET_INTERFACE:
1418 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1419 || !dev->dev_config
1420 || wIndex >= GS_MAX_NUM_INTERFACES)
1421 break;
1422 if (dev->dev_config == GS_BULK_CONFIG_ID
1423 && wIndex != GS_BULK_INTERFACE_ID)
1424 break;
1425 /* no alternate interface settings */
1426 if (wValue != 0)
1427 break;
1428 spin_lock(&dev->dev_lock);
1429 /* PXA hardware partially handles SET_INTERFACE;
1430 * we need to kluge around that interference. */
1431 if (gadget_is_pxa(gadget)) {
1432 ret = gs_set_config(dev, use_acm ?
1433 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1434 goto set_interface_done;
1435 }
1436 if (dev->dev_config != GS_BULK_CONFIG_ID
1437 && wIndex == GS_CONTROL_INTERFACE_ID) {
1438 if (dev->dev_notify_ep) {
1439 usb_ep_disable(dev->dev_notify_ep);
1440 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1441 }
1442 } else {
1443 usb_ep_disable(dev->dev_in_ep);
1444 usb_ep_disable(dev->dev_out_ep);
1445 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1446 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1447 }
1448 ret = 0;
1449set_interface_done:
1450 spin_unlock(&dev->dev_lock);
1451 break;
1452
1453 case USB_REQ_GET_INTERFACE:
1454 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1455 || dev->dev_config == GS_NO_CONFIG_ID)
1456 break;
1457 if (wIndex >= GS_MAX_NUM_INTERFACES
1458 || (dev->dev_config == GS_BULK_CONFIG_ID
1459 && wIndex != GS_BULK_INTERFACE_ID)) {
1460 ret = -EDOM;
1461 break;
1462 }
1463 /* no alternate interface settings */
1464 *(u8 *)req->buf = 0;
1465 ret = min(wLength, (u16)1);
1466 break;
1467
1468 default:
1469 pr_err("gs_setup: unknown standard request, type=%02x, "
1470 "request=%02x, value=%04x, index=%04x, length=%d\n",
1471 ctrl->bRequestType, ctrl->bRequest,
1472 wValue, wIndex, wLength);
1473 break;
1474 }
1475
1476 return ret;
1477} 214}
1478 215
1479static void gs_setup_complete_set_line_coding(struct usb_ep *ep, 216static struct usb_composite_driver gserial_driver = {
1480 struct usb_request *req) 217 .name = "g_serial",
1481{ 218 .dev = &device_desc,
1482 struct gs_dev *dev = ep->driver_data; 219 .strings = dev_strings,
1483 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ 220 .bind = gs_bind,
1484
1485 switch (req->status) {
1486 case 0:
1487 /* normal completion */
1488 if (req->actual != sizeof(port->port_line_coding))
1489 usb_ep_set_halt(ep);
1490 else if (port) {
1491 struct usb_cdc_line_coding *value = req->buf;
1492
1493 /* REVISIT: we currently just remember this data.
1494 * If we change that, (a) validate it first, then
1495 * (b) update whatever hardware needs updating.
1496 */
1497 spin_lock(&port->port_lock);
1498 port->port_line_coding = *value;
1499 spin_unlock(&port->port_lock);
1500 }
1501 break;
1502
1503 case -ESHUTDOWN:
1504 /* disconnect */
1505 gs_free_req(ep, req);
1506 break;
1507
1508 default:
1509 /* unexpected */
1510 break;
1511 }
1512 return;
1513}
1514
1515static int gs_setup_class(struct usb_gadget *gadget,
1516 const struct usb_ctrlrequest *ctrl)
1517{
1518 int ret = -EOPNOTSUPP;
1519 struct gs_dev *dev = get_gadget_data(gadget);
1520 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1521 struct usb_request *req = dev->dev_ctrl_req;
1522 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1523 u16 wValue = le16_to_cpu(ctrl->wValue);
1524 u16 wLength = le16_to_cpu(ctrl->wLength);
1525
1526 switch (ctrl->bRequest) {
1527 case USB_CDC_REQ_SET_LINE_CODING:
1528 if (wLength != sizeof(struct usb_cdc_line_coding))
1529 break;
1530 ret = wLength;
1531 req->complete = gs_setup_complete_set_line_coding;
1532 break;
1533
1534 case USB_CDC_REQ_GET_LINE_CODING:
1535 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
1536 if (port) {
1537 spin_lock(&port->port_lock);
1538 memcpy(req->buf, &port->port_line_coding, ret);
1539 spin_unlock(&port->port_lock);
1540 }
1541 break;
1542
1543 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1544 if (wLength != 0)
1545 break;
1546 ret = 0;
1547 if (port) {
1548 /* REVISIT: we currently just remember this data.
1549 * If we change that, update whatever hardware needs
1550 * updating.
1551 */
1552 spin_lock(&port->port_lock);
1553 port->port_handshake_bits = wValue;
1554 spin_unlock(&port->port_lock);
1555 }
1556 break;
1557
1558 default:
1559 /* NOTE: strictly speaking, we should accept AT-commands
1560 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
1561 * But our call management descriptor says we don't handle
1562 * call management, so we should be able to get by without
1563 * handling those "required" commands (except by stalling).
1564 */
1565 pr_err("gs_setup: unknown class request, "
1566 "type=%02x, request=%02x, value=%04x, "
1567 "index=%04x, length=%d\n",
1568 ctrl->bRequestType, ctrl->bRequest,
1569 wValue, wIndex, wLength);
1570 break;
1571 }
1572
1573 return ret;
1574}
1575
1576/*
1577 * gs_setup_complete
1578 */
1579static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1580{
1581 if (req->status || req->actual != req->length) {
1582 pr_err("gs_setup_complete: status error, status=%d, "
1583 "actual=%d, length=%d\n",
1584 req->status, req->actual, req->length);
1585 }
1586}
1587
1588/*
1589 * gs_setup
1590 *
1591 * Implements all the control endpoint functionality that's not
1592 * handled in hardware or the hardware driver.
1593 *
1594 * Returns the size of the data sent to the host, or a negative
1595 * error number.
1596 */
1597static int gs_setup(struct usb_gadget *gadget,
1598 const struct usb_ctrlrequest *ctrl)
1599{
1600 int ret = -EOPNOTSUPP;
1601 struct gs_dev *dev = get_gadget_data(gadget);
1602 struct usb_request *req = dev->dev_ctrl_req;
1603 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1604 u16 wValue = le16_to_cpu(ctrl->wValue);
1605 u16 wLength = le16_to_cpu(ctrl->wLength);
1606
1607 req->complete = gs_setup_complete;
1608
1609 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1610 case USB_TYPE_STANDARD:
1611 ret = gs_setup_standard(gadget, ctrl);
1612 break;
1613
1614 case USB_TYPE_CLASS:
1615 ret = gs_setup_class(gadget, ctrl);
1616 break;
1617
1618 default:
1619 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1620 "value=%04x, index=%04x, length=%d\n",
1621 ctrl->bRequestType, ctrl->bRequest,
1622 wValue, wIndex, wLength);
1623 break;
1624 }
1625
1626 /* respond with data transfer before status phase? */
1627 if (ret >= 0) {
1628 req->length = ret;
1629 req->zero = ret < wLength
1630 && (ret % gadget->ep0->maxpacket) == 0;
1631 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1632 if (ret < 0) {
1633 pr_err("gs_setup: cannot queue response, ret=%d\n",
1634 ret);
1635 req->status = 0;
1636 gs_setup_complete(gadget->ep0, req);
1637 }
1638 }
1639
1640 /* device either stalls (ret < 0) or reports success */
1641 return ret;
1642}
1643
1644/*
1645 * gs_disconnect
1646 *
1647 * Called when the device is disconnected. Frees the closed
1648 * ports and disconnects open ports. Open ports will be freed
1649 * on close. Then reallocates the ports for the next connection.
1650 */
1651static void gs_disconnect(struct usb_gadget *gadget)
1652{
1653 unsigned long flags;
1654 struct gs_dev *dev = get_gadget_data(gadget);
1655
1656 spin_lock_irqsave(&dev->dev_lock, flags);
1657
1658 gs_reset_config(dev);
1659
1660 /* free closed ports and disconnect open ports */
1661 /* (open ports will be freed when closed) */
1662 gs_free_ports(dev);
1663
1664 /* re-allocate ports for the next connection */
1665 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1666 pr_err("gs_disconnect: cannot re-allocate ports\n");
1667
1668 spin_unlock_irqrestore(&dev->dev_lock, flags);
1669
1670 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1671}
1672
1673static struct usb_gadget_driver gs_gadget_driver = {
1674#ifdef CONFIG_USB_GADGET_DUALSPEED
1675 .speed = USB_SPEED_HIGH,
1676#else
1677 .speed = USB_SPEED_FULL,
1678#endif /* CONFIG_USB_GADGET_DUALSPEED */
1679 .function = GS_LONG_NAME,
1680 .bind = gs_bind,
1681 .unbind = gs_unbind,
1682 .setup = gs_setup,
1683 .disconnect = gs_disconnect,
1684 .driver = {
1685 .name = GS_SHORT_NAME,
1686 .owner = THIS_MODULE,
1687 },
1688}; 221};
1689 222
1690/* 223static int __init init(void)
1691 * gs_set_config
1692 *
1693 * Configures the device by enabling device specific
1694 * optimizations, setting up the endpoints, allocating
1695 * read and write requests and queuing read requests.
1696 *
1697 * The device lock must be held when calling this function.
1698 */
1699static int gs_set_config(struct gs_dev *dev, unsigned config)
1700{ 224{
1701 int i; 225 /* We *could* export two configs; that'd be much cleaner...
1702 int ret = 0; 226 * but neither of these product IDs was defined that way.
1703 struct usb_gadget *gadget = dev->dev_gadget;
1704 struct usb_ep *ep;
1705 struct usb_endpoint_descriptor *out, *in, *notify;
1706 struct usb_request *req;
1707
1708 if (dev == NULL) {
1709 pr_err("gs_set_config: NULL device pointer\n");
1710 return 0;
1711 }
1712
1713 if (config == dev->dev_config)
1714 return 0;
1715
1716 gs_reset_config(dev);
1717
1718 switch (config) {
1719 case GS_NO_CONFIG_ID:
1720 return 0;
1721 case GS_BULK_CONFIG_ID:
1722 if (use_acm)
1723 return -EINVAL;
1724 break;
1725 case GS_ACM_CONFIG_ID:
1726 if (!use_acm)
1727 return -EINVAL;
1728 break;
1729 default:
1730 return -EINVAL;
1731 }
1732
1733 in = choose_ep_desc(gadget,
1734 &gs_highspeed_in_desc,
1735 &gs_fullspeed_in_desc);
1736 out = choose_ep_desc(gadget,
1737 &gs_highspeed_out_desc,
1738 &gs_fullspeed_out_desc);
1739 notify = dev->dev_notify_ep
1740 ? choose_ep_desc(gadget,
1741 &gs_highspeed_notify_desc,
1742 &gs_fullspeed_notify_desc)
1743 : NULL;
1744
1745 ret = usb_ep_enable(dev->dev_in_ep, in);
1746 if (ret == 0) {
1747 dev->dev_in_ep_desc = in;
1748 } else {
1749 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1750 __func__, "IN", dev->dev_in_ep->name, ret);
1751 return ret;
1752 }
1753
1754 ret = usb_ep_enable(dev->dev_out_ep, out);
1755 if (ret == 0) {
1756 dev->dev_out_ep_desc = out;
1757 } else {
1758 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1759 __func__, "OUT", dev->dev_out_ep->name, ret);
1760fail0:
1761 usb_ep_disable(dev->dev_in_ep);
1762 return ret;
1763 }
1764
1765 if (notify) {
1766 ret = usb_ep_enable(dev->dev_notify_ep, notify);
1767 if (ret == 0) {
1768 dev->dev_notify_ep_desc = notify;
1769 } else {
1770 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1771 __func__, "NOTIFY",
1772 dev->dev_notify_ep->name, ret);
1773 usb_ep_disable(dev->dev_out_ep);
1774 goto fail0;
1775 }
1776 }
1777
1778 dev->dev_config = config;
1779
1780 /* allocate and queue read requests */
1781 ep = dev->dev_out_ep;
1782 for (i=0; i<read_q_size && ret == 0; i++) {
1783 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1784 req->complete = gs_read_complete;
1785 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1786 pr_err("gs_set_config: cannot queue read "
1787 "request, ret=%d\n", ret);
1788 }
1789 } else {
1790 pr_err("gs_set_config: cannot allocate "
1791 "read requests\n");
1792 ret = -ENOMEM;
1793 goto exit_reset_config;
1794 }
1795 }
1796
1797 /* allocate write requests, and put on free list */
1798 ep = dev->dev_in_ep;
1799 for (i=0; i<write_q_size; i++) {
1800 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
1801 if (req) {
1802 req->complete = gs_write_complete;
1803 list_add(&req->list, &dev->dev_req_list);
1804 } else {
1805 pr_err("gs_set_config: cannot allocate "
1806 "write requests\n");
1807 ret = -ENOMEM;
1808 goto exit_reset_config;
1809 }
1810 }
1811
1812 /* REVISIT the ACM mode should be able to actually *issue* some
1813 * notifications, for at least serial state change events if
1814 * not also for network connection; say so in bmCapabilities.
1815 */ 227 */
1816
1817 pr_info("gs_set_config: %s configured, %s speed %s config\n",
1818 GS_LONG_NAME,
1819 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1820 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1821
1822 return 0;
1823
1824exit_reset_config:
1825 gs_reset_config(dev);
1826 return ret;
1827}
1828
1829/*
1830 * gs_reset_config
1831 *
1832 * Mark the device as not configured, disable all endpoints,
1833 * which forces completion of pending I/O and frees queued
1834 * requests, and free the remaining write requests on the
1835 * free list.
1836 *
1837 * The device lock must be held when calling this function.
1838 */
1839static void gs_reset_config(struct gs_dev *dev)
1840{
1841 struct usb_request *req;
1842
1843 if (dev == NULL) {
1844 pr_err("gs_reset_config: NULL device pointer\n");
1845 return;
1846 }
1847
1848 if (dev->dev_config == GS_NO_CONFIG_ID)
1849 return;
1850
1851 dev->dev_config = GS_NO_CONFIG_ID;
1852
1853 /* free write requests on the free list */
1854 while(!list_empty(&dev->dev_req_list)) {
1855 req = list_entry(dev->dev_req_list.next,
1856 struct usb_request, list);
1857 list_del(&req->list);
1858 gs_free_req(dev->dev_in_ep, req);
1859 }
1860
1861 /* disable endpoints, forcing completion of pending i/o; */
1862 /* completion handlers free their requests in this case */
1863 if (dev->dev_notify_ep)
1864 usb_ep_disable(dev->dev_notify_ep);
1865 usb_ep_disable(dev->dev_in_ep);
1866 usb_ep_disable(dev->dev_out_ep);
1867}
1868
1869/*
1870 * gs_build_config_buf
1871 *
1872 * Builds the config descriptors in the given buffer and returns the
1873 * length, or a negative error number.
1874 */
1875static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
1876 u8 type, unsigned int index, int is_otg)
1877{
1878 int len;
1879 int high_speed = 0;
1880 const struct usb_config_descriptor *config_desc;
1881 const struct usb_descriptor_header **function;
1882
1883 if (index >= gs_device_desc.bNumConfigurations)
1884 return -EINVAL;
1885
1886 /* other speed switches high and full speed */
1887 if (gadget_is_dualspeed(g)) {
1888 high_speed = (g->speed == USB_SPEED_HIGH);
1889 if (type == USB_DT_OTHER_SPEED_CONFIG)
1890 high_speed = !high_speed;
1891 }
1892
1893 if (use_acm) { 228 if (use_acm) {
1894 config_desc = &gs_acm_config_desc; 229 serial_config_driver.label = "CDC ACM config";
1895 function = high_speed 230 serial_config_driver.bConfigurationValue = 2;
1896 ? gs_acm_highspeed_function 231 device_desc.bDeviceClass = USB_CLASS_COMM;
1897 : gs_acm_fullspeed_function; 232 device_desc.idProduct =
233 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
1898 } else { 234 } else {
1899 config_desc = &gs_bulk_config_desc; 235 serial_config_driver.label = "Generic Serial config";
1900 function = high_speed 236 serial_config_driver.bConfigurationValue = 1;
1901 ? gs_bulk_highspeed_function 237 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
1902 : gs_bulk_fullspeed_function; 238 device_desc.idProduct =
1903 } 239 __constant_cpu_to_le16(GS_PRODUCT_ID);
1904
1905 /* for now, don't advertise srp-only devices */
1906 if (!is_otg)
1907 function++;
1908
1909 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
1910 if (len < 0)
1911 return len;
1912
1913 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
1914
1915 return len;
1916}
1917
1918/*
1919 * gs_alloc_req
1920 *
1921 * Allocate a usb_request and its buffer. Returns a pointer to the
1922 * usb_request or NULL if there is an error.
1923 */
1924static struct usb_request *
1925gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
1926{
1927 struct usb_request *req;
1928
1929 if (ep == NULL)
1930 return NULL;
1931
1932 req = usb_ep_alloc_request(ep, kmalloc_flags);
1933
1934 if (req != NULL) {
1935 req->length = len;
1936 req->buf = kmalloc(len, kmalloc_flags);
1937 if (req->buf == NULL) {
1938 usb_ep_free_request(ep, req);
1939 return NULL;
1940 }
1941 } 240 }
241 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
1942 242
1943 return req; 243 return usb_composite_register(&gserial_driver);
1944} 244}
245module_init(init);
1945 246
1946/* 247static void __exit cleanup(void)
1947 * gs_free_req
1948 *
1949 * Free a usb_request and its buffer.
1950 */
1951static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1952{ 248{
1953 if (ep != NULL && req != NULL) { 249 usb_composite_unregister(&gserial_driver);
1954 kfree(req->buf); 250 gserial_cleanup();
1955 usb_ep_free_request(ep, req);
1956 }
1957}
1958
1959/*
1960 * gs_alloc_ports
1961 *
1962 * Allocate all ports and set the gs_dev struct to point to them.
1963 * Return 0 if successful, or a negative error number.
1964 *
1965 * The device lock is normally held when calling this function.
1966 */
1967static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
1968{
1969 int i;
1970 struct gs_port *port;
1971
1972 if (dev == NULL)
1973 return -EIO;
1974
1975 for (i=0; i<GS_NUM_PORTS; i++) {
1976 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
1977 return -ENOMEM;
1978
1979 port->port_dev = dev;
1980 port->port_num = i;
1981 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
1982 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
1983 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
1984 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
1985 spin_lock_init(&port->port_lock);
1986 init_waitqueue_head(&port->port_write_wait);
1987
1988 dev->dev_port[i] = port;
1989 }
1990
1991 return 0;
1992}
1993
1994/*
1995 * gs_free_ports
1996 *
1997 * Free all closed ports. Open ports are disconnected by
1998 * freeing their write buffers, setting their device pointers
1999 * and the pointers to them in the device to NULL. These
2000 * ports will be freed when closed.
2001 *
2002 * The device lock is normally held when calling this function.
2003 */
2004static void gs_free_ports(struct gs_dev *dev)
2005{
2006 int i;
2007 unsigned long flags;
2008 struct gs_port *port;
2009
2010 if (dev == NULL)
2011 return;
2012
2013 for (i=0; i<GS_NUM_PORTS; i++) {
2014 if ((port=dev->dev_port[i]) != NULL) {
2015 dev->dev_port[i] = NULL;
2016
2017 spin_lock_irqsave(&port->port_lock, flags);
2018
2019 if (port->port_write_buf != NULL) {
2020 gs_buf_free(port->port_write_buf);
2021 port->port_write_buf = NULL;
2022 }
2023
2024 if (port->port_open_count > 0 || port->port_in_use) {
2025 port->port_dev = NULL;
2026 wake_up_interruptible(&port->port_write_wait);
2027 if (port->port_tty) {
2028 tty_hangup(port->port_tty);
2029 }
2030 spin_unlock_irqrestore(&port->port_lock, flags);
2031 } else {
2032 spin_unlock_irqrestore(&port->port_lock, flags);
2033 kfree(port);
2034 }
2035
2036 }
2037 }
2038}
2039
2040/*-------------------------------------------------------------------------*/
2041
2042/* Circular Buffer */
2043
2044/*
2045 * gs_buf_alloc
2046 *
2047 * Allocate a circular buffer and all associated memory.
2048 */
2049static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2050{
2051 struct gs_buf *gb;
2052
2053 if (size == 0)
2054 return NULL;
2055
2056 gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2057 if (gb == NULL)
2058 return NULL;
2059
2060 gb->buf_buf = kmalloc(size, kmalloc_flags);
2061 if (gb->buf_buf == NULL) {
2062 kfree(gb);
2063 return NULL;
2064 }
2065
2066 gb->buf_size = size;
2067 gb->buf_get = gb->buf_put = gb->buf_buf;
2068
2069 return gb;
2070}
2071
2072/*
2073 * gs_buf_free
2074 *
2075 * Free the buffer and all associated memory.
2076 */
2077static void gs_buf_free(struct gs_buf *gb)
2078{
2079 if (gb) {
2080 kfree(gb->buf_buf);
2081 kfree(gb);
2082 }
2083}
2084
2085/*
2086 * gs_buf_clear
2087 *
2088 * Clear out all data in the circular buffer.
2089 */
2090static void gs_buf_clear(struct gs_buf *gb)
2091{
2092 if (gb != NULL)
2093 gb->buf_get = gb->buf_put;
2094 /* equivalent to a get of all data available */
2095}
2096
2097/*
2098 * gs_buf_data_avail
2099 *
2100 * Return the number of bytes of data available in the circular
2101 * buffer.
2102 */
2103static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2104{
2105 if (gb != NULL)
2106 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2107 else
2108 return 0;
2109}
2110
2111/*
2112 * gs_buf_space_avail
2113 *
2114 * Return the number of bytes of space available in the circular
2115 * buffer.
2116 */
2117static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2118{
2119 if (gb != NULL)
2120 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2121 else
2122 return 0;
2123}
2124
2125/*
2126 * gs_buf_put
2127 *
2128 * Copy data data from a user buffer and put it into the circular buffer.
2129 * Restrict to the amount of space available.
2130 *
2131 * Return the number of bytes copied.
2132 */
2133static unsigned int
2134gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2135{
2136 unsigned int len;
2137
2138 if (gb == NULL)
2139 return 0;
2140
2141 len = gs_buf_space_avail(gb);
2142 if (count > len)
2143 count = len;
2144
2145 if (count == 0)
2146 return 0;
2147
2148 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2149 if (count > len) {
2150 memcpy(gb->buf_put, buf, len);
2151 memcpy(gb->buf_buf, buf+len, count - len);
2152 gb->buf_put = gb->buf_buf + count - len;
2153 } else {
2154 memcpy(gb->buf_put, buf, count);
2155 if (count < len)
2156 gb->buf_put += count;
2157 else /* count == len */
2158 gb->buf_put = gb->buf_buf;
2159 }
2160
2161 return count;
2162}
2163
2164/*
2165 * gs_buf_get
2166 *
2167 * Get data from the circular buffer and copy to the given buffer.
2168 * Restrict to the amount of data available.
2169 *
2170 * Return the number of bytes copied.
2171 */
2172static unsigned int
2173gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2174{
2175 unsigned int len;
2176
2177 if (gb == NULL)
2178 return 0;
2179
2180 len = gs_buf_data_avail(gb);
2181 if (count > len)
2182 count = len;
2183
2184 if (count == 0)
2185 return 0;
2186
2187 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2188 if (count > len) {
2189 memcpy(buf, gb->buf_get, len);
2190 memcpy(buf+len, gb->buf_buf, count - len);
2191 gb->buf_get = gb->buf_buf + count - len;
2192 } else {
2193 memcpy(buf, gb->buf_get, count);
2194 if (count < len)
2195 gb->buf_get += count;
2196 else /* count == len */
2197 gb->buf_get = gb->buf_buf;
2198 }
2199
2200 return count;
2201}
2202
2203/*-------------------------------------------------------------------------*/
2204
2205static struct tty_driver *gs_tty_driver;
2206
2207/*
2208 * gs_module_init
2209 *
2210 * Register as a USB gadget driver and a tty driver.
2211 */
2212static int __init gs_module_init(void)
2213{
2214 int i;
2215 int retval;
2216
2217 retval = usb_gadget_register_driver(&gs_gadget_driver);
2218 if (retval) {
2219 pr_err("gs_module_init: cannot register gadget driver, "
2220 "ret=%d\n", retval);
2221 return retval;
2222 }
2223
2224 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
2225 if (!gs_tty_driver)
2226 return -ENOMEM;
2227 gs_tty_driver->owner = THIS_MODULE;
2228 gs_tty_driver->driver_name = GS_SHORT_NAME;
2229 gs_tty_driver->name = "ttygs";
2230 gs_tty_driver->major = GS_MAJOR;
2231 gs_tty_driver->minor_start = GS_MINOR_START;
2232 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2233 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2234 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2235 gs_tty_driver->init_termios = tty_std_termios;
2236 /* must match GS_DEFAULT_DTE_RATE and friends */
2237 gs_tty_driver->init_termios.c_cflag =
2238 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2239 gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE;
2240 gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE;
2241 tty_set_operations(gs_tty_driver, &gs_tty_ops);
2242
2243 for (i = 0; i < GS_NUM_PORTS; i++)
2244 mutex_init(&gs_open_close_lock[i]);
2245
2246 retval = tty_register_driver(gs_tty_driver);
2247 if (retval) {
2248 usb_gadget_unregister_driver(&gs_gadget_driver);
2249 put_tty_driver(gs_tty_driver);
2250 pr_err("gs_module_init: cannot register tty driver, "
2251 "ret=%d\n", retval);
2252 return retval;
2253 }
2254
2255 pr_info("gs_module_init: %s %s loaded\n",
2256 GS_LONG_NAME, GS_VERSION_STR);
2257 return 0;
2258}
2259module_init(gs_module_init);
2260
2261/*
2262 * gs_module_exit
2263 *
2264 * Unregister as a tty driver and a USB gadget driver.
2265 */
2266static void __exit gs_module_exit(void)
2267{
2268 tty_unregister_driver(gs_tty_driver);
2269 put_tty_driver(gs_tty_driver);
2270 usb_gadget_unregister_driver(&gs_gadget_driver);
2271
2272 pr_info("gs_module_exit: %s %s unloaded\n",
2273 GS_LONG_NAME, GS_VERSION_STR);
2274} 251}
2275module_exit(gs_module_exit); 252module_exit(cleanup);
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
new file mode 100644
index 000000000000..5458f43a8668
--- /dev/null
+++ b/drivers/usb/gadget/u_ether.c
@@ -0,0 +1,967 @@
1/*
2 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/* #define VERBOSE_DEBUG */
24
25#include <linux/kernel.h>
26#include <linux/utsname.h>
27#include <linux/device.h>
28#include <linux/ctype.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31
32#include "u_ether.h"
33
34
35/*
36 * This component encapsulates the Ethernet link glue needed to provide
37 * one (!) network link through the USB gadget stack, normally "usb0".
38 *
39 * The control and data models are handled by the function driver which
40 * connects to this code; such as CDC Ethernet, "CDC Subset", or RNDIS.
41 * That includes all descriptor and endpoint management.
42 *
43 * Link level addressing is handled by this component using module
44 * parameters; if no such parameters are provided, random link level
45 * addresses are used. Each end of the link uses one address. The
46 * host end address is exported in various ways, and is often recorded
47 * in configuration databases.
48 *
49 * The driver which assembles each configuration using such a link is
50 * responsible for ensuring that each configuration includes at most one
51 * instance of is network link. (The network layer provides ways for
52 * this single "physical" link to be used by multiple virtual links.)
53 */
54
55#define DRIVER_VERSION "29-May-2008"
56
57struct eth_dev {
58 /* lock is held while accessing port_usb
59 * or updating its backlink port_usb->ioport
60 */
61 spinlock_t lock;
62 struct gether *port_usb;
63
64 struct net_device *net;
65 struct usb_gadget *gadget;
66
67 spinlock_t req_lock; /* guard {rx,tx}_reqs */
68 struct list_head tx_reqs, rx_reqs;
69 atomic_t tx_qlen;
70
71 unsigned header_len;
72 struct sk_buff *(*wrap)(struct sk_buff *skb);
73 int (*unwrap)(struct sk_buff *skb);
74
75 struct work_struct work;
76
77 unsigned long todo;
78#define WORK_RX_MEMORY 0
79
80 bool zlp;
81 u8 host_mac[ETH_ALEN];
82};
83
84/*-------------------------------------------------------------------------*/
85
86#define RX_EXTRA 20 /* bytes guarding against rx overflows */
87
88#define DEFAULT_QLEN 2 /* double buffering by default */
89
90
91#ifdef CONFIG_USB_GADGET_DUALSPEED
92
93static unsigned qmult = 5;
94module_param(qmult, uint, S_IRUGO|S_IWUSR);
95MODULE_PARM_DESC(qmult, "queue length multiplier at high speed");
96
97#else /* full speed (low speed doesn't do bulk) */
98#define qmult 1
99#endif
100
101/* for dual-speed hardware, use deeper queues at highspeed */
102static inline int qlen(struct usb_gadget *gadget)
103{
104 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH)
105 return qmult * DEFAULT_QLEN;
106 else
107 return DEFAULT_QLEN;
108}
109
110/*-------------------------------------------------------------------------*/
111
112/* REVISIT there must be a better way than having two sets
113 * of debug calls ...
114 */
115
116#undef DBG
117#undef VDBG
118#undef ERROR
119#undef WARN
120#undef INFO
121
122#define xprintk(d, level, fmt, args...) \
123 printk(level "%s: " fmt , (d)->net->name , ## args)
124
125#ifdef DEBUG
126#undef DEBUG
127#define DBG(dev, fmt, args...) \
128 xprintk(dev , KERN_DEBUG , fmt , ## args)
129#else
130#define DBG(dev, fmt, args...) \
131 do { } while (0)
132#endif /* DEBUG */
133
134#ifdef VERBOSE_DEBUG
135#define VDBG DBG
136#else
137#define VDBG(dev, fmt, args...) \
138 do { } while (0)
139#endif /* DEBUG */
140
141#define ERROR(dev, fmt, args...) \
142 xprintk(dev , KERN_ERR , fmt , ## args)
143#define WARN(dev, fmt, args...) \
144 xprintk(dev , KERN_WARNING , fmt , ## args)
145#define INFO(dev, fmt, args...) \
146 xprintk(dev , KERN_INFO , fmt , ## args)
147
148/*-------------------------------------------------------------------------*/
149
150/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
151
152static int eth_change_mtu(struct net_device *net, int new_mtu)
153{
154 struct eth_dev *dev = netdev_priv(net);
155 unsigned long flags;
156 int status = 0;
157
158 /* don't change MTU on "live" link (peer won't know) */
159 spin_lock_irqsave(&dev->lock, flags);
160 if (dev->port_usb)
161 status = -EBUSY;
162 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
163 status = -ERANGE;
164 else
165 net->mtu = new_mtu;
166 spin_unlock_irqrestore(&dev->lock, flags);
167
168 return status;
169}
170
171static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
172{
173 struct eth_dev *dev = netdev_priv(net);
174
175 strlcpy(p->driver, "g_ether", sizeof p->driver);
176 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
177 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
178 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
179}
180
181static u32 eth_get_link(struct net_device *net)
182{
183 struct eth_dev *dev = netdev_priv(net);
184 return dev->gadget->speed != USB_SPEED_UNKNOWN;
185}
186
187/* REVISIT can also support:
188 * - WOL (by tracking suspends and issuing remote wakeup)
189 * - msglevel (implies updated messaging)
190 * - ... probably more ethtool ops
191 */
192
193static struct ethtool_ops ops = {
194 .get_drvinfo = eth_get_drvinfo,
195 .get_link = eth_get_link
196};
197
198static void defer_kevent(struct eth_dev *dev, int flag)
199{
200 if (test_and_set_bit(flag, &dev->todo))
201 return;
202 if (!schedule_work(&dev->work))
203 ERROR(dev, "kevent %d may have been dropped\n", flag);
204 else
205 DBG(dev, "kevent %d scheduled\n", flag);
206}
207
208static void rx_complete(struct usb_ep *ep, struct usb_request *req);
209
210static int
211rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
212{
213 struct sk_buff *skb;
214 int retval = -ENOMEM;
215 size_t size = 0;
216 struct usb_ep *out;
217 unsigned long flags;
218
219 spin_lock_irqsave(&dev->lock, flags);
220 if (dev->port_usb)
221 out = dev->port_usb->out_ep;
222 else
223 out = NULL;
224 spin_unlock_irqrestore(&dev->lock, flags);
225
226 if (!out)
227 return -ENOTCONN;
228
229
230 /* Padding up to RX_EXTRA handles minor disagreements with host.
231 * Normally we use the USB "terminate on short read" convention;
232 * so allow up to (N*maxpacket), since that memory is normally
233 * already allocated. Some hardware doesn't deal well with short
234 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
235 * byte off the end (to force hardware errors on overflow).
236 *
237 * RNDIS uses internal framing, and explicitly allows senders to
238 * pad to end-of-packet. That's potentially nice for speed, but
239 * means receivers can't recover lost synch on their own (because
240 * new packets don't only start after a short RX).
241 */
242 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
243 size += dev->port_usb->header_len;
244 size += out->maxpacket - 1;
245 size -= size % out->maxpacket;
246
247 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
248 if (skb == NULL) {
249 DBG(dev, "no rx skb\n");
250 goto enomem;
251 }
252
253 /* Some platforms perform better when IP packets are aligned,
254 * but on at least one, checksumming fails otherwise. Note:
255 * RNDIS headers involve variable numbers of LE32 values.
256 */
257 skb_reserve(skb, NET_IP_ALIGN);
258
259 req->buf = skb->data;
260 req->length = size;
261 req->complete = rx_complete;
262 req->context = skb;
263
264 retval = usb_ep_queue(out, req, gfp_flags);
265 if (retval == -ENOMEM)
266enomem:
267 defer_kevent(dev, WORK_RX_MEMORY);
268 if (retval) {
269 DBG(dev, "rx submit --> %d\n", retval);
270 if (skb)
271 dev_kfree_skb_any(skb);
272 spin_lock_irqsave(&dev->req_lock, flags);
273 list_add(&req->list, &dev->rx_reqs);
274 spin_unlock_irqrestore(&dev->req_lock, flags);
275 }
276 return retval;
277}
278
279static void rx_complete(struct usb_ep *ep, struct usb_request *req)
280{
281 struct sk_buff *skb = req->context;
282 struct eth_dev *dev = ep->driver_data;
283 int status = req->status;
284
285 switch (status) {
286
287 /* normal completion */
288 case 0:
289 skb_put(skb, req->actual);
290 if (dev->unwrap)
291 status = dev->unwrap(skb);
292 if (status < 0
293 || ETH_HLEN > skb->len
294 || skb->len > ETH_FRAME_LEN) {
295 dev->net->stats.rx_errors++;
296 dev->net->stats.rx_length_errors++;
297 DBG(dev, "rx length %d\n", skb->len);
298 break;
299 }
300
301 skb->protocol = eth_type_trans(skb, dev->net);
302 dev->net->stats.rx_packets++;
303 dev->net->stats.rx_bytes += skb->len;
304
305 /* no buffer copies needed, unless hardware can't
306 * use skb buffers.
307 */
308 status = netif_rx(skb);
309 skb = NULL;
310 break;
311
312 /* software-driven interface shutdown */
313 case -ECONNRESET: /* unlink */
314 case -ESHUTDOWN: /* disconnect etc */
315 VDBG(dev, "rx shutdown, code %d\n", status);
316 goto quiesce;
317
318 /* for hardware automagic (such as pxa) */
319 case -ECONNABORTED: /* endpoint reset */
320 DBG(dev, "rx %s reset\n", ep->name);
321 defer_kevent(dev, WORK_RX_MEMORY);
322quiesce:
323 dev_kfree_skb_any(skb);
324 goto clean;
325
326 /* data overrun */
327 case -EOVERFLOW:
328 dev->net->stats.rx_over_errors++;
329 /* FALLTHROUGH */
330
331 default:
332 dev->net->stats.rx_errors++;
333 DBG(dev, "rx status %d\n", status);
334 break;
335 }
336
337 if (skb)
338 dev_kfree_skb_any(skb);
339 if (!netif_running(dev->net)) {
340clean:
341 spin_lock(&dev->req_lock);
342 list_add(&req->list, &dev->rx_reqs);
343 spin_unlock(&dev->req_lock);
344 req = NULL;
345 }
346 if (req)
347 rx_submit(dev, req, GFP_ATOMIC);
348}
349
350static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
351{
352 unsigned i;
353 struct usb_request *req;
354
355 if (!n)
356 return -ENOMEM;
357
358 /* queue/recycle up to N requests */
359 i = n;
360 list_for_each_entry(req, list, list) {
361 if (i-- == 0)
362 goto extra;
363 }
364 while (i--) {
365 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
366 if (!req)
367 return list_empty(list) ? -ENOMEM : 0;
368 list_add(&req->list, list);
369 }
370 return 0;
371
372extra:
373 /* free extras */
374 for (;;) {
375 struct list_head *next;
376
377 next = req->list.next;
378 list_del(&req->list);
379 usb_ep_free_request(ep, req);
380
381 if (next == list)
382 break;
383
384 req = container_of(next, struct usb_request, list);
385 }
386 return 0;
387}
388
389static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
390{
391 int status;
392
393 spin_lock(&dev->req_lock);
394 status = prealloc(&dev->tx_reqs, link->in_ep, n);
395 if (status < 0)
396 goto fail;
397 status = prealloc(&dev->rx_reqs, link->out_ep, n);
398 if (status < 0)
399 goto fail;
400 goto done;
401fail:
402 DBG(dev, "can't alloc requests\n");
403done:
404 spin_unlock(&dev->req_lock);
405 return status;
406}
407
408static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
409{
410 struct usb_request *req;
411 unsigned long flags;
412
413 /* fill unused rxq slots with some skb */
414 spin_lock_irqsave(&dev->req_lock, flags);
415 while (!list_empty(&dev->rx_reqs)) {
416 req = container_of(dev->rx_reqs.next,
417 struct usb_request, list);
418 list_del_init(&req->list);
419 spin_unlock_irqrestore(&dev->req_lock, flags);
420
421 if (rx_submit(dev, req, gfp_flags) < 0) {
422 defer_kevent(dev, WORK_RX_MEMORY);
423 return;
424 }
425
426 spin_lock_irqsave(&dev->req_lock, flags);
427 }
428 spin_unlock_irqrestore(&dev->req_lock, flags);
429}
430
431static void eth_work(struct work_struct *work)
432{
433 struct eth_dev *dev = container_of(work, struct eth_dev, work);
434
435 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
436 if (netif_running(dev->net))
437 rx_fill(dev, GFP_KERNEL);
438 }
439
440 if (dev->todo)
441 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
442}
443
444static void tx_complete(struct usb_ep *ep, struct usb_request *req)
445{
446 struct sk_buff *skb = req->context;
447 struct eth_dev *dev = ep->driver_data;
448
449 switch (req->status) {
450 default:
451 dev->net->stats.tx_errors++;
452 VDBG(dev, "tx err %d\n", req->status);
453 /* FALLTHROUGH */
454 case -ECONNRESET: /* unlink */
455 case -ESHUTDOWN: /* disconnect etc */
456 break;
457 case 0:
458 dev->net->stats.tx_bytes += skb->len;
459 }
460 dev->net->stats.tx_packets++;
461
462 spin_lock(&dev->req_lock);
463 list_add(&req->list, &dev->tx_reqs);
464 spin_unlock(&dev->req_lock);
465 dev_kfree_skb_any(skb);
466
467 atomic_dec(&dev->tx_qlen);
468 if (netif_carrier_ok(dev->net))
469 netif_wake_queue(dev->net);
470}
471
472static inline int is_promisc(u16 cdc_filter)
473{
474 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
475}
476
477static int eth_start_xmit(struct sk_buff *skb, struct net_device *net)
478{
479 struct eth_dev *dev = netdev_priv(net);
480 int length = skb->len;
481 int retval;
482 struct usb_request *req = NULL;
483 unsigned long flags;
484 struct usb_ep *in;
485 u16 cdc_filter;
486
487 spin_lock_irqsave(&dev->lock, flags);
488 if (dev->port_usb) {
489 in = dev->port_usb->in_ep;
490 cdc_filter = dev->port_usb->cdc_filter;
491 } else {
492 in = NULL;
493 cdc_filter = 0;
494 }
495 spin_unlock_irqrestore(&dev->lock, flags);
496
497 if (!in) {
498 dev_kfree_skb_any(skb);
499 return 0;
500 }
501
502 /* apply outgoing CDC or RNDIS filters */
503 if (!is_promisc(cdc_filter)) {
504 u8 *dest = skb->data;
505
506 if (is_multicast_ether_addr(dest)) {
507 u16 type;
508
509 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
510 * SET_ETHERNET_MULTICAST_FILTERS requests
511 */
512 if (is_broadcast_ether_addr(dest))
513 type = USB_CDC_PACKET_TYPE_BROADCAST;
514 else
515 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
516 if (!(cdc_filter & type)) {
517 dev_kfree_skb_any(skb);
518 return 0;
519 }
520 }
521 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
522 }
523
524 spin_lock_irqsave(&dev->req_lock, flags);
525 /*
526 * this freelist can be empty if an interrupt triggered disconnect()
527 * and reconfigured the gadget (shutting down this queue) after the
528 * network stack decided to xmit but before we got the spinlock.
529 */
530 if (list_empty(&dev->tx_reqs)) {
531 spin_unlock_irqrestore(&dev->req_lock, flags);
532 return 1;
533 }
534
535 req = container_of(dev->tx_reqs.next, struct usb_request, list);
536 list_del(&req->list);
537
538 /* temporarily stop TX queue when the freelist empties */
539 if (list_empty(&dev->tx_reqs))
540 netif_stop_queue(net);
541 spin_unlock_irqrestore(&dev->req_lock, flags);
542
543 /* no buffer copies needed, unless the network stack did it
544 * or the hardware can't use skb buffers.
545 * or there's not enough space for extra headers we need
546 */
547 if (dev->wrap) {
548 struct sk_buff *skb_new;
549
550 skb_new = dev->wrap(skb);
551 if (!skb_new)
552 goto drop;
553
554 dev_kfree_skb_any(skb);
555 skb = skb_new;
556 length = skb->len;
557 }
558 req->buf = skb->data;
559 req->context = skb;
560 req->complete = tx_complete;
561
562 /* use zlp framing on tx for strict CDC-Ether conformance,
563 * though any robust network rx path ignores extra padding.
564 * and some hardware doesn't like to write zlps.
565 */
566 req->zero = 1;
567 if (!dev->zlp && (length % in->maxpacket) == 0)
568 length++;
569
570 req->length = length;
571
572 /* throttle highspeed IRQ rate back slightly */
573 if (gadget_is_dualspeed(dev->gadget))
574 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
575 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
576 : 0;
577
578 retval = usb_ep_queue(in, req, GFP_ATOMIC);
579 switch (retval) {
580 default:
581 DBG(dev, "tx queue err %d\n", retval);
582 break;
583 case 0:
584 net->trans_start = jiffies;
585 atomic_inc(&dev->tx_qlen);
586 }
587
588 if (retval) {
589drop:
590 dev->net->stats.tx_dropped++;
591 dev_kfree_skb_any(skb);
592 spin_lock_irqsave(&dev->req_lock, flags);
593 if (list_empty(&dev->tx_reqs))
594 netif_start_queue(net);
595 list_add(&req->list, &dev->tx_reqs);
596 spin_unlock_irqrestore(&dev->req_lock, flags);
597 }
598 return 0;
599}
600
601/*-------------------------------------------------------------------------*/
602
603static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
604{
605 DBG(dev, "%s\n", __func__);
606
607 /* fill the rx queue */
608 rx_fill(dev, gfp_flags);
609
610 /* and open the tx floodgates */
611 atomic_set(&dev->tx_qlen, 0);
612 netif_wake_queue(dev->net);
613}
614
615static int eth_open(struct net_device *net)
616{
617 struct eth_dev *dev = netdev_priv(net);
618 struct gether *link;
619
620 DBG(dev, "%s\n", __func__);
621 if (netif_carrier_ok(dev->net))
622 eth_start(dev, GFP_KERNEL);
623
624 spin_lock_irq(&dev->lock);
625 link = dev->port_usb;
626 if (link && link->open)
627 link->open(link);
628 spin_unlock_irq(&dev->lock);
629
630 return 0;
631}
632
633static int eth_stop(struct net_device *net)
634{
635 struct eth_dev *dev = netdev_priv(net);
636 unsigned long flags;
637
638 VDBG(dev, "%s\n", __func__);
639 netif_stop_queue(net);
640
641 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
642 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
643 dev->net->stats.rx_errors, dev->net->stats.tx_errors
644 );
645
646 /* ensure there are no more active requests */
647 spin_lock_irqsave(&dev->lock, flags);
648 if (dev->port_usb) {
649 struct gether *link = dev->port_usb;
650
651 if (link->close)
652 link->close(link);
653
654 /* NOTE: we have no abort-queue primitive we could use
655 * to cancel all pending I/O. Instead, we disable then
656 * reenable the endpoints ... this idiom may leave toggle
657 * wrong, but that's a self-correcting error.
658 *
659 * REVISIT: we *COULD* just let the transfers complete at
660 * their own pace; the network stack can handle old packets.
661 * For the moment we leave this here, since it works.
662 */
663 usb_ep_disable(link->in_ep);
664 usb_ep_disable(link->out_ep);
665 if (netif_carrier_ok(net)) {
666 DBG(dev, "host still using in/out endpoints\n");
667 usb_ep_enable(link->in_ep, link->in);
668 usb_ep_enable(link->out_ep, link->out);
669 }
670 }
671 spin_unlock_irqrestore(&dev->lock, flags);
672
673 return 0;
674}
675
676/*-------------------------------------------------------------------------*/
677
678/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
679static char *dev_addr;
680module_param(dev_addr, charp, S_IRUGO);
681MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
682
683/* this address is invisible to ifconfig */
684static char *host_addr;
685module_param(host_addr, charp, S_IRUGO);
686MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
687
688
689static u8 __init nibble(unsigned char c)
690{
691 if (isdigit(c))
692 return c - '0';
693 c = toupper(c);
694 if (isxdigit(c))
695 return 10 + c - 'A';
696 return 0;
697}
698
699static int __init get_ether_addr(const char *str, u8 *dev_addr)
700{
701 if (str) {
702 unsigned i;
703
704 for (i = 0; i < 6; i++) {
705 unsigned char num;
706
707 if ((*str == '.') || (*str == ':'))
708 str++;
709 num = nibble(*str++) << 4;
710 num |= (nibble(*str++));
711 dev_addr [i] = num;
712 }
713 if (is_valid_ether_addr(dev_addr))
714 return 0;
715 }
716 random_ether_addr(dev_addr);
717 return 1;
718}
719
720static struct eth_dev *the_dev;
721
722
723/**
724 * gether_setup - initialize one ethernet-over-usb link
725 * @g: gadget to associated with these links
726 * @ethaddr: NULL, or a buffer in which the ethernet address of the
727 * host side of the link is recorded
728 * Context: may sleep
729 *
730 * This sets up the single network link that may be exported by a
731 * gadget driver using this framework. The link layer addresses are
732 * set up using module parameters.
733 *
734 * Returns negative errno, or zero on success
735 */
736int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
737{
738 struct eth_dev *dev;
739 struct net_device *net;
740 int status;
741
742 if (the_dev)
743 return -EBUSY;
744
745 net = alloc_etherdev(sizeof *dev);
746 if (!net)
747 return -ENOMEM;
748
749 dev = netdev_priv(net);
750 spin_lock_init(&dev->lock);
751 spin_lock_init(&dev->req_lock);
752 INIT_WORK(&dev->work, eth_work);
753 INIT_LIST_HEAD(&dev->tx_reqs);
754 INIT_LIST_HEAD(&dev->rx_reqs);
755
756 /* network device setup */
757 dev->net = net;
758 strcpy(net->name, "usb%d");
759
760 if (get_ether_addr(dev_addr, net->dev_addr))
761 dev_warn(&g->dev,
762 "using random %s ethernet address\n", "self");
763 if (get_ether_addr(host_addr, dev->host_mac))
764 dev_warn(&g->dev,
765 "using random %s ethernet address\n", "host");
766
767 if (ethaddr)
768 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
769
770 net->change_mtu = eth_change_mtu;
771 net->hard_start_xmit = eth_start_xmit;
772 net->open = eth_open;
773 net->stop = eth_stop;
774 /* watchdog_timeo, tx_timeout ... */
775 /* set_multicast_list */
776 SET_ETHTOOL_OPS(net, &ops);
777
778 /* two kinds of host-initiated state changes:
779 * - iff DATA transfer is active, carrier is "on"
780 * - tx queueing enabled if open *and* carrier is "on"
781 */
782 netif_stop_queue(net);
783 netif_carrier_off(net);
784
785 dev->gadget = g;
786 SET_NETDEV_DEV(net, &g->dev);
787
788 status = register_netdev(net);
789 if (status < 0) {
790 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
791 free_netdev(net);
792 } else {
793 DECLARE_MAC_BUF(tmp);
794
795 INFO(dev, "MAC %s\n", print_mac(tmp, net->dev_addr));
796 INFO(dev, "HOST MAC %s\n", print_mac(tmp, dev->host_mac));
797
798 the_dev = dev;
799 }
800
801 return status;
802}
803
804/**
805 * gether_cleanup - remove Ethernet-over-USB device
806 * Context: may sleep
807 *
808 * This is called to free all resources allocated by @gether_setup().
809 */
810void gether_cleanup(void)
811{
812 if (!the_dev)
813 return;
814
815 unregister_netdev(the_dev->net);
816 free_netdev(the_dev->net);
817
818 /* assuming we used keventd, it must quiesce too */
819 flush_scheduled_work();
820
821 the_dev = NULL;
822}
823
824
825/**
826 * gether_connect - notify network layer that USB link is active
827 * @link: the USB link, set up with endpoints, descriptors matching
828 * current device speed, and any framing wrapper(s) set up.
829 * Context: irqs blocked
830 *
831 * This is called to activate endpoints and let the network layer know
832 * the connection is active ("carrier detect"). It may cause the I/O
833 * queues to open and start letting network packets flow, but will in
834 * any case activate the endpoints so that they respond properly to the
835 * USB host.
836 *
837 * Verify net_device pointer returned using IS_ERR(). If it doesn't
838 * indicate some error code (negative errno), ep->driver_data values
839 * have been overwritten.
840 */
841struct net_device *gether_connect(struct gether *link)
842{
843 struct eth_dev *dev = the_dev;
844 int result = 0;
845
846 if (!dev)
847 return ERR_PTR(-EINVAL);
848
849 link->in_ep->driver_data = dev;
850 result = usb_ep_enable(link->in_ep, link->in);
851 if (result != 0) {
852 DBG(dev, "enable %s --> %d\n",
853 link->in_ep->name, result);
854 goto fail0;
855 }
856
857 link->out_ep->driver_data = dev;
858 result = usb_ep_enable(link->out_ep, link->out);
859 if (result != 0) {
860 DBG(dev, "enable %s --> %d\n",
861 link->out_ep->name, result);
862 goto fail1;
863 }
864
865 if (result == 0)
866 result = alloc_requests(dev, link, qlen(dev->gadget));
867
868 if (result == 0) {
869 dev->zlp = link->is_zlp_ok;
870 DBG(dev, "qlen %d\n", qlen(dev->gadget));
871
872 dev->header_len = link->header_len;
873 dev->unwrap = link->unwrap;
874 dev->wrap = link->wrap;
875
876 spin_lock(&dev->lock);
877 dev->port_usb = link;
878 link->ioport = dev;
879 spin_unlock(&dev->lock);
880
881 netif_carrier_on(dev->net);
882 if (netif_running(dev->net))
883 eth_start(dev, GFP_ATOMIC);
884
885 /* on error, disable any endpoints */
886 } else {
887 (void) usb_ep_disable(link->out_ep);
888fail1:
889 (void) usb_ep_disable(link->in_ep);
890 }
891fail0:
892 /* caller is responsible for cleanup on error */
893 if (result < 0)
894 return ERR_PTR(result);
895 return dev->net;
896}
897
898/**
899 * gether_disconnect - notify network layer that USB link is inactive
900 * @link: the USB link, on which gether_connect() was called
901 * Context: irqs blocked
902 *
903 * This is called to deactivate endpoints and let the network layer know
904 * the connection went inactive ("no carrier").
905 *
906 * On return, the state is as if gether_connect() had never been called.
907 * The endpoints are inactive, and accordingly without active USB I/O.
908 * Pointers to endpoint descriptors and endpoint private data are nulled.
909 */
910void gether_disconnect(struct gether *link)
911{
912 struct eth_dev *dev = link->ioport;
913 struct usb_request *req;
914
915 WARN_ON(!dev);
916 if (!dev)
917 return;
918
919 DBG(dev, "%s\n", __func__);
920
921 netif_stop_queue(dev->net);
922 netif_carrier_off(dev->net);
923
924 /* disable endpoints, forcing (synchronous) completion
925 * of all pending i/o. then free the request objects
926 * and forget about the endpoints.
927 */
928 usb_ep_disable(link->in_ep);
929 spin_lock(&dev->req_lock);
930 while (!list_empty(&dev->tx_reqs)) {
931 req = container_of(dev->tx_reqs.next,
932 struct usb_request, list);
933 list_del(&req->list);
934
935 spin_unlock(&dev->req_lock);
936 usb_ep_free_request(link->in_ep, req);
937 spin_lock(&dev->req_lock);
938 }
939 spin_unlock(&dev->req_lock);
940 link->in_ep->driver_data = NULL;
941 link->in = NULL;
942
943 usb_ep_disable(link->out_ep);
944 spin_lock(&dev->req_lock);
945 while (!list_empty(&dev->rx_reqs)) {
946 req = container_of(dev->rx_reqs.next,
947 struct usb_request, list);
948 list_del(&req->list);
949
950 spin_unlock(&dev->req_lock);
951 usb_ep_free_request(link->out_ep, req);
952 spin_lock(&dev->req_lock);
953 }
954 spin_unlock(&dev->req_lock);
955 link->out_ep->driver_data = NULL;
956 link->out = NULL;
957
958 /* finish forgetting about this USB link episode */
959 dev->header_len = 0;
960 dev->unwrap = NULL;
961 dev->wrap = NULL;
962
963 spin_lock(&dev->lock);
964 dev->port_usb = NULL;
965 link->ioport = NULL;
966 spin_unlock(&dev->lock);
967}
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
new file mode 100644
index 000000000000..0d1f7ae3b071
--- /dev/null
+++ b/drivers/usb/gadget/u_ether.h
@@ -0,0 +1,127 @@
1/*
2 * u_ether.h -- interface to USB gadget "ethernet link" utilities
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef __U_ETHER_H
24#define __U_ETHER_H
25
26#include <linux/err.h>
27#include <linux/if_ether.h>
28#include <linux/usb/composite.h>
29#include <linux/usb/cdc.h>
30
31#include "gadget_chips.h"
32
33
34/*
35 * This represents the USB side of an "ethernet" link, managed by a USB
36 * function which provides control and (maybe) framing. Two functions
37 * in different configurations could share the same ethernet link/netdev,
38 * using different host interaction models.
39 *
40 * There is a current limitation that only one instance of this link may
41 * be present in any given configuration. When that's a problem, network
42 * layer facilities can be used to package multiple logical links on this
43 * single "physical" one.
44 */
45struct gether {
46 struct usb_function func;
47
48 /* updated by gether_{connect,disconnect} */
49 struct eth_dev *ioport;
50
51 /* endpoints handle full and/or high speeds */
52 struct usb_ep *in_ep;
53 struct usb_ep *out_ep;
54
55 /* descriptors match device speed at gether_connect() time */
56 struct usb_endpoint_descriptor *in;
57 struct usb_endpoint_descriptor *out;
58
59 bool is_zlp_ok;
60
61 u16 cdc_filter;
62
63 /* hooks for added framing, as needed for RNDIS and EEM.
64 * we currently don't support multiple frames per SKB.
65 */
66 u32 header_len;
67 struct sk_buff *(*wrap)(struct sk_buff *skb);
68 int (*unwrap)(struct sk_buff *skb);
69
70 /* called on network open/close */
71 void (*open)(struct gether *);
72 void (*close)(struct gether *);
73};
74
75#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
76 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
77 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
78 |USB_CDC_PACKET_TYPE_DIRECTED)
79
80
81/* netdev setup/teardown as directed by the gadget driver */
82int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN]);
83void gether_cleanup(void);
84
85/* connect/disconnect is handled by individual functions */
86struct net_device *gether_connect(struct gether *);
87void gether_disconnect(struct gether *);
88
89/* Some controllers can't support CDC Ethernet (ECM) ... */
90static inline bool can_support_ecm(struct usb_gadget *gadget)
91{
92 if (!gadget_supports_altsettings(gadget))
93 return false;
94
95 /* SA1100 can do ECM, *without* status endpoint ... but we'll
96 * only use it in non-ECM mode for backwards compatibility
97 * (and since we currently require a status endpoint)
98 */
99 if (gadget_is_sa1100(gadget))
100 return false;
101
102 /* Everything else is *presumably* fine ... but this is a bit
103 * chancy, so be **CERTAIN** there are no hardware issues with
104 * your controller. Add it above if it can't handle CDC.
105 */
106 return true;
107}
108
109/* each configuration may bind one instance of an ethernet link */
110int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
111int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
112
113#ifdef CONFIG_USB_ETH_RNDIS
114
115int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
116
117#else
118
119static inline int
120rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
121{
122 return 0;
123}
124
125#endif
126
127#endif /* __U_ETHER_H */
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
new file mode 100644
index 000000000000..abf9505d3a75
--- /dev/null
+++ b/drivers/usb/gadget/u_serial.c
@@ -0,0 +1,1246 @@
1/*
2 * u_serial.c - utilities for USB gadget "serial port"/TTY support
3 *
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
7 *
8 * This code also borrows from usbserial.c, which is
9 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
10 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
11 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
12 *
13 * This software is distributed under the terms of the GNU General
14 * Public License ("GPL") as published by the Free Software Foundation,
15 * either version 2 of that License or (at your option) any later version.
16 */
17
18/* #define VERBOSE_DEBUG */
19
20#include <linux/kernel.h>
21#include <linux/interrupt.h>
22#include <linux/device.h>
23#include <linux/delay.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26
27#include "u_serial.h"
28
29
30/*
31 * This component encapsulates the TTY layer glue needed to provide basic
32 * "serial port" functionality through the USB gadget stack. Each such
33 * port is exposed through a /dev/ttyGS* node.
34 *
35 * After initialization (gserial_setup), these TTY port devices stay
36 * available until they are removed (gserial_cleanup). Each one may be
37 * connected to a USB function (gserial_connect), or disconnected (with
38 * gserial_disconnect) when the USB host issues a config change event.
39 * Data can only flow when the port is connected to the host.
40 *
41 * A given TTY port can be made available in multiple configurations.
42 * For example, each one might expose a ttyGS0 node which provides a
43 * login application. In one case that might use CDC ACM interface 0,
44 * while another configuration might use interface 3 for that. The
45 * work to handle that (including descriptor management) is not part
46 * of this component.
47 *
48 * Configurations may expose more than one TTY port. For example, if
49 * ttyGS0 provides login service, then ttyGS1 might provide dialer access
50 * for a telephone or fax link. And ttyGS2 might be something that just
51 * needs a simple byte stream interface for some messaging protocol that
52 * is managed in userspace ... OBEX, PTP, and MTP have been mentioned.
53 */
54
55/*
56 * gserial is the lifecycle interface, used by USB functions
57 * gs_port is the I/O nexus, used by the tty driver
58 * tty_struct links to the tty/filesystem framework
59 *
60 * gserial <---> gs_port ... links will be null when the USB link is
61 * inactive; managed by gserial_{connect,disconnect}().
62 * gserial->ioport == usb_ep->driver_data ... gs_port
63 * gs_port->port_usb ... gserial
64 *
65 * gs_port <---> tty_struct ... links will be null when the TTY file
66 * isn't opened; managed by gs_open()/gs_close()
67 * gserial->port_tty ... tty_struct
68 * tty_struct->driver_data ... gserial
69 */
70
71/* RX and TX queues can buffer QUEUE_SIZE packets before they hit the
72 * next layer of buffering. For TX that's a circular buffer; for RX
73 * consider it a NOP. A third layer is provided by the TTY code.
74 */
75#define QUEUE_SIZE 16
76#define WRITE_BUF_SIZE 8192 /* TX only */
77
78/* circular buffer */
79struct gs_buf {
80 unsigned buf_size;
81 char *buf_buf;
82 char *buf_get;
83 char *buf_put;
84};
85
86/*
87 * The port structure holds info for each port, one for each minor number
88 * (and thus for each /dev/ node).
89 */
90struct gs_port {
91 spinlock_t port_lock; /* guard port_* access */
92
93 struct gserial *port_usb;
94 struct tty_struct *port_tty;
95
96 unsigned open_count;
97 bool openclose; /* open/close in progress */
98 u8 port_num;
99
100 wait_queue_head_t close_wait; /* wait for last close */
101
102 struct list_head read_pool;
103 struct tasklet_struct push;
104
105 struct list_head write_pool;
106 struct gs_buf port_write_buf;
107 wait_queue_head_t drain_wait; /* wait while writes drain */
108
109 /* REVISIT this state ... */
110 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
111};
112
113/* increase N_PORTS if you need more */
114#define N_PORTS 4
115static struct portmaster {
116 struct mutex lock; /* protect open/close */
117 struct gs_port *port;
118} ports[N_PORTS];
119static unsigned n_ports;
120
121#define GS_CLOSE_TIMEOUT 15 /* seconds */
122
123
124
125#ifdef VERBOSE_DEBUG
126#define pr_vdebug(fmt, arg...) \
127 pr_debug(fmt, ##arg)
128#else
129#define pr_vdebug(fmt, arg...) \
130 ({ if (0) pr_debug(fmt, ##arg); })
131#endif
132
133/*-------------------------------------------------------------------------*/
134
135/* Circular Buffer */
136
137/*
138 * gs_buf_alloc
139 *
140 * Allocate a circular buffer and all associated memory.
141 */
142static int gs_buf_alloc(struct gs_buf *gb, unsigned size)
143{
144 gb->buf_buf = kmalloc(size, GFP_KERNEL);
145 if (gb->buf_buf == NULL)
146 return -ENOMEM;
147
148 gb->buf_size = size;
149 gb->buf_put = gb->buf_buf;
150 gb->buf_get = gb->buf_buf;
151
152 return 0;
153}
154
155/*
156 * gs_buf_free
157 *
158 * Free the buffer and all associated memory.
159 */
160static void gs_buf_free(struct gs_buf *gb)
161{
162 kfree(gb->buf_buf);
163 gb->buf_buf = NULL;
164}
165
166/*
167 * gs_buf_clear
168 *
169 * Clear out all data in the circular buffer.
170 */
171static void gs_buf_clear(struct gs_buf *gb)
172{
173 gb->buf_get = gb->buf_put;
174 /* equivalent to a get of all data available */
175}
176
177/*
178 * gs_buf_data_avail
179 *
180 * Return the number of bytes of data available in the circular
181 * buffer.
182 */
183static unsigned gs_buf_data_avail(struct gs_buf *gb)
184{
185 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
186}
187
188/*
189 * gs_buf_space_avail
190 *
191 * Return the number of bytes of space available in the circular
192 * buffer.
193 */
194static unsigned gs_buf_space_avail(struct gs_buf *gb)
195{
196 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
197}
198
199/*
200 * gs_buf_put
201 *
202 * Copy data data from a user buffer and put it into the circular buffer.
203 * Restrict to the amount of space available.
204 *
205 * Return the number of bytes copied.
206 */
207static unsigned
208gs_buf_put(struct gs_buf *gb, const char *buf, unsigned count)
209{
210 unsigned len;
211
212 len = gs_buf_space_avail(gb);
213 if (count > len)
214 count = len;
215
216 if (count == 0)
217 return 0;
218
219 len = gb->buf_buf + gb->buf_size - gb->buf_put;
220 if (count > len) {
221 memcpy(gb->buf_put, buf, len);
222 memcpy(gb->buf_buf, buf+len, count - len);
223 gb->buf_put = gb->buf_buf + count - len;
224 } else {
225 memcpy(gb->buf_put, buf, count);
226 if (count < len)
227 gb->buf_put += count;
228 else /* count == len */
229 gb->buf_put = gb->buf_buf;
230 }
231
232 return count;
233}
234
235/*
236 * gs_buf_get
237 *
238 * Get data from the circular buffer and copy to the given buffer.
239 * Restrict to the amount of data available.
240 *
241 * Return the number of bytes copied.
242 */
243static unsigned
244gs_buf_get(struct gs_buf *gb, char *buf, unsigned count)
245{
246 unsigned len;
247
248 len = gs_buf_data_avail(gb);
249 if (count > len)
250 count = len;
251
252 if (count == 0)
253 return 0;
254
255 len = gb->buf_buf + gb->buf_size - gb->buf_get;
256 if (count > len) {
257 memcpy(buf, gb->buf_get, len);
258 memcpy(buf+len, gb->buf_buf, count - len);
259 gb->buf_get = gb->buf_buf + count - len;
260 } else {
261 memcpy(buf, gb->buf_get, count);
262 if (count < len)
263 gb->buf_get += count;
264 else /* count == len */
265 gb->buf_get = gb->buf_buf;
266 }
267
268 return count;
269}
270
271/*-------------------------------------------------------------------------*/
272
273/* I/O glue between TTY (upper) and USB function (lower) driver layers */
274
275/*
276 * gs_alloc_req
277 *
278 * Allocate a usb_request and its buffer. Returns a pointer to the
279 * usb_request or NULL if there is an error.
280 */
281static struct usb_request *
282gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
283{
284 struct usb_request *req;
285
286 req = usb_ep_alloc_request(ep, kmalloc_flags);
287
288 if (req != NULL) {
289 req->length = len;
290 req->buf = kmalloc(len, kmalloc_flags);
291 if (req->buf == NULL) {
292 usb_ep_free_request(ep, req);
293 return NULL;
294 }
295 }
296
297 return req;
298}
299
300/*
301 * gs_free_req
302 *
303 * Free a usb_request and its buffer.
304 */
305static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
306{
307 kfree(req->buf);
308 usb_ep_free_request(ep, req);
309}
310
311/*
312 * gs_send_packet
313 *
314 * If there is data to send, a packet is built in the given
315 * buffer and the size is returned. If there is no data to
316 * send, 0 is returned.
317 *
318 * Called with port_lock held.
319 */
320static unsigned
321gs_send_packet(struct gs_port *port, char *packet, unsigned size)
322{
323 unsigned len;
324
325 len = gs_buf_data_avail(&port->port_write_buf);
326 if (len < size)
327 size = len;
328 if (size != 0)
329 size = gs_buf_get(&port->port_write_buf, packet, size);
330 return size;
331}
332
333/*
334 * gs_start_tx
335 *
336 * This function finds available write requests, calls
337 * gs_send_packet to fill these packets with data, and
338 * continues until either there are no more write requests
339 * available or no more data to send. This function is
340 * run whenever data arrives or write requests are available.
341 *
342 * Context: caller owns port_lock; port_usb is non-null.
343 */
344static int gs_start_tx(struct gs_port *port)
345/*
346__releases(&port->port_lock)
347__acquires(&port->port_lock)
348*/
349{
350 struct list_head *pool = &port->write_pool;
351 struct usb_ep *in = port->port_usb->in;
352 int status = 0;
353 bool do_tty_wake = false;
354
355 while (!list_empty(pool)) {
356 struct usb_request *req;
357 int len;
358
359 req = list_entry(pool->next, struct usb_request, list);
360 len = gs_send_packet(port, req->buf, in->maxpacket);
361 if (len == 0) {
362 wake_up_interruptible(&port->drain_wait);
363 break;
364 }
365 do_tty_wake = true;
366
367 req->length = len;
368 list_del(&req->list);
369
370#ifdef VERBOSE_DEBUG
371 pr_debug("%s: %s, len=%d, 0x%02x 0x%02x 0x%02x ...\n",
372 __func__, in->name, len, *((u8 *)req->buf),
373 *((u8 *)req->buf+1), *((u8 *)req->buf+2));
374#endif
375
376 /* Drop lock while we call out of driver; completions
377 * could be issued while we do so. Disconnection may
378 * happen too; maybe immediately before we queue this!
379 *
380 * NOTE that we may keep sending data for a while after
381 * the TTY closed (dev->ioport->port_tty is NULL).
382 */
383 spin_unlock(&port->port_lock);
384 status = usb_ep_queue(in, req, GFP_ATOMIC);
385 spin_lock(&port->port_lock);
386
387 if (status) {
388 pr_debug("%s: %s %s err %d\n",
389 __func__, "queue", in->name, status);
390 list_add(&req->list, pool);
391 break;
392 }
393
394 /* abort immediately after disconnect */
395 if (!port->port_usb)
396 break;
397 }
398
399 if (do_tty_wake && port->port_tty)
400 tty_wakeup(port->port_tty);
401 return status;
402}
403
404static void gs_rx_push(unsigned long _port)
405{
406 struct gs_port *port = (void *)_port;
407 struct tty_struct *tty = port->port_tty;
408
409 /* With low_latency, tty_flip_buffer_push() doesn't put its
410 * real work through a workqueue, so the ldisc has a better
411 * chance to keep up with peak USB data rates.
412 */
413 if (tty) {
414 tty_flip_buffer_push(tty);
415 wake_up_interruptible(&tty->read_wait);
416 }
417}
418
419/*
420 * gs_recv_packet
421 *
422 * Called for each USB packet received. Reads the packet
423 * header and stuffs the data in the appropriate tty buffer.
424 * Returns 0 if successful, or a negative error number.
425 *
426 * Called during USB completion routine, on interrupt time.
427 * With port_lock.
428 */
429static int gs_recv_packet(struct gs_port *port, char *packet, unsigned size)
430{
431 unsigned len;
432 struct tty_struct *tty;
433
434 /* I/O completions can continue for a while after close(), until the
435 * request queue empties. Just discard any data we receive, until
436 * something reopens this TTY ... as if there were no HW flow control.
437 */
438 tty = port->port_tty;
439 if (tty == NULL) {
440 pr_vdebug("%s: ttyGS%d, after close\n",
441 __func__, port->port_num);
442 return -EIO;
443 }
444
445 len = tty_insert_flip_string(tty, packet, size);
446 if (len > 0)
447 tasklet_schedule(&port->push);
448 if (len < size)
449 pr_debug("%s: ttyGS%d, drop %d bytes\n",
450 __func__, port->port_num, size - len);
451 return 0;
452}
453
454/*
455 * Context: caller owns port_lock, and port_usb is set
456 */
457static unsigned gs_start_rx(struct gs_port *port)
458/*
459__releases(&port->port_lock)
460__acquires(&port->port_lock)
461*/
462{
463 struct list_head *pool = &port->read_pool;
464 struct usb_ep *out = port->port_usb->out;
465 unsigned started = 0;
466
467 while (!list_empty(pool)) {
468 struct usb_request *req;
469 int status;
470 struct tty_struct *tty;
471
472 /* no more rx if closed or throttled */
473 tty = port->port_tty;
474 if (!tty || test_bit(TTY_THROTTLED, &tty->flags))
475 break;
476
477 req = list_entry(pool->next, struct usb_request, list);
478 list_del(&req->list);
479 req->length = out->maxpacket;
480
481 /* drop lock while we call out; the controller driver
482 * may need to call us back (e.g. for disconnect)
483 */
484 spin_unlock(&port->port_lock);
485 status = usb_ep_queue(out, req, GFP_ATOMIC);
486 spin_lock(&port->port_lock);
487
488 if (status) {
489 pr_debug("%s: %s %s err %d\n",
490 __func__, "queue", out->name, status);
491 list_add(&req->list, pool);
492 break;
493 }
494 started++;
495
496 /* abort immediately after disconnect */
497 if (!port->port_usb)
498 break;
499 }
500 return started;
501}
502
503static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
504{
505 int status;
506 struct gs_port *port = ep->driver_data;
507
508 spin_lock(&port->port_lock);
509 list_add(&req->list, &port->read_pool);
510
511 switch (req->status) {
512 case 0:
513 /* normal completion */
514 status = gs_recv_packet(port, req->buf, req->actual);
515 if (status && status != -EIO)
516 pr_debug("%s: %s %s err %d\n",
517 __func__, "recv", ep->name, status);
518 gs_start_rx(port);
519 break;
520
521 case -ESHUTDOWN:
522 /* disconnect */
523 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
524 break;
525
526 default:
527 /* presumably a transient fault */
528 pr_warning("%s: unexpected %s status %d\n",
529 __func__, ep->name, req->status);
530 gs_start_rx(port);
531 break;
532 }
533 spin_unlock(&port->port_lock);
534}
535
536static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
537{
538 struct gs_port *port = ep->driver_data;
539
540 spin_lock(&port->port_lock);
541 list_add(&req->list, &port->write_pool);
542
543 switch (req->status) {
544 default:
545 /* presumably a transient fault */
546 pr_warning("%s: unexpected %s status %d\n",
547 __func__, ep->name, req->status);
548 /* FALL THROUGH */
549 case 0:
550 /* normal completion */
551 gs_start_tx(port);
552 break;
553
554 case -ESHUTDOWN:
555 /* disconnect */
556 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
557 break;
558 }
559
560 spin_unlock(&port->port_lock);
561}
562
563static void gs_free_requests(struct usb_ep *ep, struct list_head *head)
564{
565 struct usb_request *req;
566
567 while (!list_empty(head)) {
568 req = list_entry(head->next, struct usb_request, list);
569 list_del(&req->list);
570 gs_free_req(ep, req);
571 }
572}
573
574static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
575 void (*fn)(struct usb_ep *, struct usb_request *))
576{
577 int i;
578 struct usb_request *req;
579
580 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
581 * do quite that many this time, don't fail ... we just won't
582 * be as speedy as we might otherwise be.
583 */
584 for (i = 0; i < QUEUE_SIZE; i++) {
585 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
586 if (!req)
587 return list_empty(head) ? -ENOMEM : 0;
588 req->complete = fn;
589 list_add_tail(&req->list, head);
590 }
591 return 0;
592}
593
594/**
595 * gs_start_io - start USB I/O streams
596 * @dev: encapsulates endpoints to use
597 * Context: holding port_lock; port_tty and port_usb are non-null
598 *
599 * We only start I/O when something is connected to both sides of
600 * this port. If nothing is listening on the host side, we may
601 * be pointlessly filling up our TX buffers and FIFO.
602 */
603static int gs_start_io(struct gs_port *port)
604{
605 struct list_head *head = &port->read_pool;
606 struct usb_ep *ep = port->port_usb->out;
607 int status;
608 unsigned started;
609
610 /* Allocate RX and TX I/O buffers. We can't easily do this much
611 * earlier (with GFP_KERNEL) because the requests are coupled to
612 * endpoints, as are the packet sizes we'll be using. Different
613 * configurations may use different endpoints with a given port;
614 * and high speed vs full speed changes packet sizes too.
615 */
616 status = gs_alloc_requests(ep, head, gs_read_complete);
617 if (status)
618 return status;
619
620 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
621 gs_write_complete);
622 if (status) {
623 gs_free_requests(ep, head);
624 return status;
625 }
626
627 /* queue read requests */
628 started = gs_start_rx(port);
629
630 /* unblock any pending writes into our circular buffer */
631 if (started) {
632 tty_wakeup(port->port_tty);
633 } else {
634 gs_free_requests(ep, head);
635 gs_free_requests(port->port_usb->in, &port->write_pool);
636 }
637
638 return started ? 0 : status;
639}
640
641/*-------------------------------------------------------------------------*/
642
643/* TTY Driver */
644
645/*
646 * gs_open sets up the link between a gs_port and its associated TTY.
647 * That link is broken *only* by TTY close(), and all driver methods
648 * know that.
649 */
650static int gs_open(struct tty_struct *tty, struct file *file)
651{
652 int port_num = tty->index;
653 struct gs_port *port;
654 int status;
655
656 if (port_num < 0 || port_num >= n_ports)
657 return -ENXIO;
658
659 do {
660 mutex_lock(&ports[port_num].lock);
661 port = ports[port_num].port;
662 if (!port)
663 status = -ENODEV;
664 else {
665 spin_lock_irq(&port->port_lock);
666
667 /* already open? Great. */
668 if (port->open_count) {
669 status = 0;
670 port->open_count++;
671
672 /* currently opening/closing? wait ... */
673 } else if (port->openclose) {
674 status = -EBUSY;
675
676 /* ... else we do the work */
677 } else {
678 status = -EAGAIN;
679 port->openclose = true;
680 }
681 spin_unlock_irq(&port->port_lock);
682 }
683 mutex_unlock(&ports[port_num].lock);
684
685 switch (status) {
686 default:
687 /* fully handled */
688 return status;
689 case -EAGAIN:
690 /* must do the work */
691 break;
692 case -EBUSY:
693 /* wait for EAGAIN task to finish */
694 msleep(1);
695 /* REVISIT could have a waitchannel here, if
696 * concurrent open performance is important
697 */
698 break;
699 }
700 } while (status != -EAGAIN);
701
702 /* Do the "real open" */
703 spin_lock_irq(&port->port_lock);
704
705 /* allocate circular buffer on first open */
706 if (port->port_write_buf.buf_buf == NULL) {
707
708 spin_unlock_irq(&port->port_lock);
709 status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE);
710 spin_lock_irq(&port->port_lock);
711
712 if (status) {
713 pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
714 port->port_num, tty, file);
715 port->openclose = false;
716 goto exit_unlock_port;
717 }
718 }
719
720 /* REVISIT if REMOVED (ports[].port NULL), abort the open
721 * to let rmmod work faster (but this way isn't wrong).
722 */
723
724 /* REVISIT maybe wait for "carrier detect" */
725
726 tty->driver_data = port;
727 port->port_tty = tty;
728
729 port->open_count = 1;
730 port->openclose = false;
731
732 /* low_latency means ldiscs work in tasklet context, without
733 * needing a workqueue schedule ... easier to keep up.
734 */
735 tty->low_latency = 1;
736
737 /* if connected, start the I/O stream */
738 if (port->port_usb) {
739 pr_debug("gs_open: start ttyGS%d\n", port->port_num);
740 gs_start_io(port);
741
742 /* REVISIT for ACM, issue "network connected" event */
743 }
744
745 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
746
747 status = 0;
748
749exit_unlock_port:
750 spin_unlock_irq(&port->port_lock);
751 return status;
752}
753
754static int gs_writes_finished(struct gs_port *p)
755{
756 int cond;
757
758 /* return true on disconnect or empty buffer */
759 spin_lock_irq(&p->port_lock);
760 cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf);
761 spin_unlock_irq(&p->port_lock);
762
763 return cond;
764}
765
766static void gs_close(struct tty_struct *tty, struct file *file)
767{
768 struct gs_port *port = tty->driver_data;
769
770 spin_lock_irq(&port->port_lock);
771
772 if (port->open_count != 1) {
773 if (port->open_count == 0)
774 WARN_ON(1);
775 else
776 --port->open_count;
777 goto exit;
778 }
779
780 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
781
782 /* mark port as closing but in use; we can drop port lock
783 * and sleep if necessary
784 */
785 port->openclose = true;
786 port->open_count = 0;
787
788 if (port->port_usb)
789 /* REVISIT for ACM, issue "network disconnected" event */;
790
791 /* wait for circular write buffer to drain, disconnect, or at
792 * most GS_CLOSE_TIMEOUT seconds; then discard the rest
793 */
794 if (gs_buf_data_avail(&port->port_write_buf) > 0
795 && port->port_usb) {
796 spin_unlock_irq(&port->port_lock);
797 wait_event_interruptible_timeout(port->drain_wait,
798 gs_writes_finished(port),
799 GS_CLOSE_TIMEOUT * HZ);
800 spin_lock_irq(&port->port_lock);
801 }
802
803 /* Iff we're disconnected, there can be no I/O in flight so it's
804 * ok to free the circular buffer; else just scrub it. And don't
805 * let the push tasklet fire again until we're re-opened.
806 */
807 if (port->port_usb == NULL)
808 gs_buf_free(&port->port_write_buf);
809 else
810 gs_buf_clear(&port->port_write_buf);
811
812 tasklet_kill(&port->push);
813
814 tty->driver_data = NULL;
815 port->port_tty = NULL;
816
817 port->openclose = false;
818
819 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
820 port->port_num, tty, file);
821
822 wake_up_interruptible(&port->close_wait);
823exit:
824 spin_unlock_irq(&port->port_lock);
825}
826
827static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
828{
829 struct gs_port *port = tty->driver_data;
830 unsigned long flags;
831 int status;
832
833 pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n",
834 port->port_num, tty, count);
835
836 spin_lock_irqsave(&port->port_lock, flags);
837 if (count)
838 count = gs_buf_put(&port->port_write_buf, buf, count);
839 /* treat count == 0 as flush_chars() */
840 if (port->port_usb)
841 status = gs_start_tx(port);
842 spin_unlock_irqrestore(&port->port_lock, flags);
843
844 return count;
845}
846
847static int gs_put_char(struct tty_struct *tty, unsigned char ch)
848{
849 struct gs_port *port = tty->driver_data;
850 unsigned long flags;
851 int status;
852
853 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
854 port->port_num, tty, ch, __builtin_return_address(0));
855
856 spin_lock_irqsave(&port->port_lock, flags);
857 status = gs_buf_put(&port->port_write_buf, &ch, 1);
858 spin_unlock_irqrestore(&port->port_lock, flags);
859
860 return status;
861}
862
863static void gs_flush_chars(struct tty_struct *tty)
864{
865 struct gs_port *port = tty->driver_data;
866 unsigned long flags;
867
868 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
869
870 spin_lock_irqsave(&port->port_lock, flags);
871 if (port->port_usb)
872 gs_start_tx(port);
873 spin_unlock_irqrestore(&port->port_lock, flags);
874}
875
876static int gs_write_room(struct tty_struct *tty)
877{
878 struct gs_port *port = tty->driver_data;
879 unsigned long flags;
880 int room = 0;
881
882 spin_lock_irqsave(&port->port_lock, flags);
883 if (port->port_usb)
884 room = gs_buf_space_avail(&port->port_write_buf);
885 spin_unlock_irqrestore(&port->port_lock, flags);
886
887 pr_vdebug("gs_write_room: (%d,%p) room=%d\n",
888 port->port_num, tty, room);
889
890 return room;
891}
892
893static int gs_chars_in_buffer(struct tty_struct *tty)
894{
895 struct gs_port *port = tty->driver_data;
896 unsigned long flags;
897 int chars = 0;
898
899 spin_lock_irqsave(&port->port_lock, flags);
900 chars = gs_buf_data_avail(&port->port_write_buf);
901 spin_unlock_irqrestore(&port->port_lock, flags);
902
903 pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
904 port->port_num, tty, chars);
905
906 return chars;
907}
908
909/* undo side effects of setting TTY_THROTTLED */
910static void gs_unthrottle(struct tty_struct *tty)
911{
912 struct gs_port *port = tty->driver_data;
913 unsigned long flags;
914 unsigned started = 0;
915
916 spin_lock_irqsave(&port->port_lock, flags);
917 if (port->port_usb)
918 started = gs_start_rx(port);
919 spin_unlock_irqrestore(&port->port_lock, flags);
920
921 pr_vdebug("gs_unthrottle: ttyGS%d, %d packets\n",
922 port->port_num, started);
923}
924
925static const struct tty_operations gs_tty_ops = {
926 .open = gs_open,
927 .close = gs_close,
928 .write = gs_write,
929 .put_char = gs_put_char,
930 .flush_chars = gs_flush_chars,
931 .write_room = gs_write_room,
932 .chars_in_buffer = gs_chars_in_buffer,
933 .unthrottle = gs_unthrottle,
934};
935
936/*-------------------------------------------------------------------------*/
937
938static struct tty_driver *gs_tty_driver;
939
940static int __init
941gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
942{
943 struct gs_port *port;
944
945 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
946 if (port == NULL)
947 return -ENOMEM;
948
949 spin_lock_init(&port->port_lock);
950 init_waitqueue_head(&port->close_wait);
951 init_waitqueue_head(&port->drain_wait);
952
953 tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
954
955 INIT_LIST_HEAD(&port->read_pool);
956 INIT_LIST_HEAD(&port->write_pool);
957
958 port->port_num = port_num;
959 port->port_line_coding = *coding;
960
961 ports[port_num].port = port;
962
963 return 0;
964}
965
966/**
967 * gserial_setup - initialize TTY driver for one or more ports
968 * @g: gadget to associate with these ports
969 * @count: how many ports to support
970 * Context: may sleep
971 *
972 * The TTY stack needs to know in advance how many devices it should
973 * plan to manage. Use this call to set up the ports you will be
974 * exporting through USB. Later, connect them to functions based
975 * on what configuration is activated by the USB host; and disconnect
976 * them as appropriate.
977 *
978 * An example would be a two-configuration device in which both
979 * configurations expose port 0, but through different functions.
980 * One configuration could even expose port 1 while the other
981 * one doesn't.
982 *
983 * Returns negative errno or zero.
984 */
985int __init gserial_setup(struct usb_gadget *g, unsigned count)
986{
987 unsigned i;
988 struct usb_cdc_line_coding coding;
989 int status;
990
991 if (count == 0 || count > N_PORTS)
992 return -EINVAL;
993
994 gs_tty_driver = alloc_tty_driver(count);
995 if (!gs_tty_driver)
996 return -ENOMEM;
997
998 gs_tty_driver->owner = THIS_MODULE;
999 gs_tty_driver->driver_name = "g_serial";
1000 gs_tty_driver->name = "ttyGS";
1001 /* uses dynamically assigned dev_t values */
1002
1003 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1004 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1005 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1006 gs_tty_driver->init_termios = tty_std_termios;
1007
1008 /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
1009 * MS-Windows. Otherwise, most of these flags shouldn't affect
1010 * anything unless we were to actually hook up to a serial line.
1011 */
1012 gs_tty_driver->init_termios.c_cflag =
1013 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1014 gs_tty_driver->init_termios.c_ispeed = 9600;
1015 gs_tty_driver->init_termios.c_ospeed = 9600;
1016
1017 coding.dwDTERate = __constant_cpu_to_le32(9600);
1018 coding.bCharFormat = 8;
1019 coding.bParityType = USB_CDC_NO_PARITY;
1020 coding.bDataBits = USB_CDC_1_STOP_BITS;
1021
1022 tty_set_operations(gs_tty_driver, &gs_tty_ops);
1023
1024 /* make devices be openable */
1025 for (i = 0; i < count; i++) {
1026 mutex_init(&ports[i].lock);
1027 status = gs_port_alloc(i, &coding);
1028 if (status) {
1029 count = i;
1030 goto fail;
1031 }
1032 }
1033 n_ports = count;
1034
1035 /* export the driver ... */
1036 status = tty_register_driver(gs_tty_driver);
1037 if (status) {
1038 put_tty_driver(gs_tty_driver);
1039 pr_err("%s: cannot register, err %d\n",
1040 __func__, status);
1041 goto fail;
1042 }
1043
1044 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
1045 for (i = 0; i < count; i++) {
1046 struct device *tty_dev;
1047
1048 tty_dev = tty_register_device(gs_tty_driver, i, &g->dev);
1049 if (IS_ERR(tty_dev))
1050 pr_warning("%s: no classdev for port %d, err %ld\n",
1051 __func__, i, PTR_ERR(tty_dev));
1052 }
1053
1054 pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
1055 count, (count == 1) ? "" : "s");
1056
1057 return status;
1058fail:
1059 while (count--)
1060 kfree(ports[count].port);
1061 put_tty_driver(gs_tty_driver);
1062 gs_tty_driver = NULL;
1063 return status;
1064}
1065
1066static int gs_closed(struct gs_port *port)
1067{
1068 int cond;
1069
1070 spin_lock_irq(&port->port_lock);
1071 cond = (port->open_count == 0) && !port->openclose;
1072 spin_unlock_irq(&port->port_lock);
1073 return cond;
1074}
1075
1076/**
1077 * gserial_cleanup - remove TTY-over-USB driver and devices
1078 * Context: may sleep
1079 *
1080 * This is called to free all resources allocated by @gserial_setup().
1081 * Accordingly, it may need to wait until some open /dev/ files have
1082 * closed.
1083 *
1084 * The caller must have issued @gserial_disconnect() for any ports
1085 * that had previously been connected, so that there is never any
1086 * I/O pending when it's called.
1087 */
1088void gserial_cleanup(void)
1089{
1090 unsigned i;
1091 struct gs_port *port;
1092
1093 if (!gs_tty_driver)
1094 return;
1095
1096 /* start sysfs and /dev/ttyGS* node removal */
1097 for (i = 0; i < n_ports; i++)
1098 tty_unregister_device(gs_tty_driver, i);
1099
1100 for (i = 0; i < n_ports; i++) {
1101 /* prevent new opens */
1102 mutex_lock(&ports[i].lock);
1103 port = ports[i].port;
1104 ports[i].port = NULL;
1105 mutex_unlock(&ports[i].lock);
1106
1107 /* wait for old opens to finish */
1108 wait_event(port->close_wait, gs_closed(port));
1109
1110 WARN_ON(port->port_usb != NULL);
1111
1112 kfree(port);
1113 }
1114 n_ports = 0;
1115
1116 tty_unregister_driver(gs_tty_driver);
1117 gs_tty_driver = NULL;
1118
1119 pr_debug("%s: cleaned up ttyGS* support\n", __func__);
1120}
1121
1122/**
1123 * gserial_connect - notify TTY I/O glue that USB link is active
1124 * @gser: the function, set up with endpoints and descriptors
1125 * @port_num: which port is active
1126 * Context: any (usually from irq)
1127 *
1128 * This is called activate endpoints and let the TTY layer know that
1129 * the connection is active ... not unlike "carrier detect". It won't
1130 * necessarily start I/O queues; unless the TTY is held open by any
1131 * task, there would be no point. However, the endpoints will be
1132 * activated so the USB host can perform I/O, subject to basic USB
1133 * hardware flow control.
1134 *
1135 * Caller needs to have set up the endpoints and USB function in @dev
1136 * before calling this, as well as the appropriate (speed-specific)
1137 * endpoint descriptors, and also have set up the TTY driver by calling
1138 * @gserial_setup().
1139 *
1140 * Returns negative errno or zero.
1141 * On success, ep->driver_data will be overwritten.
1142 */
1143int gserial_connect(struct gserial *gser, u8 port_num)
1144{
1145 struct gs_port *port;
1146 unsigned long flags;
1147 int status;
1148
1149 if (!gs_tty_driver || port_num >= n_ports)
1150 return -ENXIO;
1151
1152 /* we "know" gserial_cleanup() hasn't been called */
1153 port = ports[port_num].port;
1154
1155 /* activate the endpoints */
1156 status = usb_ep_enable(gser->in, gser->in_desc);
1157 if (status < 0)
1158 return status;
1159 gser->in->driver_data = port;
1160
1161 status = usb_ep_enable(gser->out, gser->out_desc);
1162 if (status < 0)
1163 goto fail_out;
1164 gser->out->driver_data = port;
1165
1166 /* then tell the tty glue that I/O can work */
1167 spin_lock_irqsave(&port->port_lock, flags);
1168 gser->ioport = port;
1169 port->port_usb = gser;
1170
1171 /* REVISIT unclear how best to handle this state...
1172 * we don't really couple it with the Linux TTY.
1173 */
1174 gser->port_line_coding = port->port_line_coding;
1175
1176 /* REVISIT if waiting on "carrier detect", signal. */
1177
1178 /* REVISIT for ACM, issue "network connection" status notification:
1179 * connected if open_count, else disconnected.
1180 */
1181
1182 /* if it's already open, start I/O */
1183 if (port->open_count) {
1184 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1185 gs_start_io(port);
1186 }
1187
1188 spin_unlock_irqrestore(&port->port_lock, flags);
1189
1190 return status;
1191
1192fail_out:
1193 usb_ep_disable(gser->in);
1194 gser->in->driver_data = NULL;
1195 return status;
1196}
1197
1198/**
1199 * gserial_disconnect - notify TTY I/O glue that USB link is inactive
1200 * @gser: the function, on which gserial_connect() was called
1201 * Context: any (usually from irq)
1202 *
1203 * This is called to deactivate endpoints and let the TTY layer know
1204 * that the connection went inactive ... not unlike "hangup".
1205 *
1206 * On return, the state is as if gserial_connect() had never been called;
1207 * there is no active USB I/O on these endpoints.
1208 */
1209void gserial_disconnect(struct gserial *gser)
1210{
1211 struct gs_port *port = gser->ioport;
1212 unsigned long flags;
1213
1214 if (!port)
1215 return;
1216
1217 /* tell the TTY glue not to do I/O here any more */
1218 spin_lock_irqsave(&port->port_lock, flags);
1219
1220 /* REVISIT as above: how best to track this? */
1221 port->port_line_coding = gser->port_line_coding;
1222
1223 port->port_usb = NULL;
1224 gser->ioport = NULL;
1225 if (port->open_count > 0 || port->openclose) {
1226 wake_up_interruptible(&port->drain_wait);
1227 if (port->port_tty)
1228 tty_hangup(port->port_tty);
1229 }
1230 spin_unlock_irqrestore(&port->port_lock, flags);
1231
1232 /* disable endpoints, aborting down any active I/O */
1233 usb_ep_disable(gser->out);
1234 gser->out->driver_data = NULL;
1235
1236 usb_ep_disable(gser->in);
1237 gser->in->driver_data = NULL;
1238
1239 /* finally, free any unused/unusable I/O buffers */
1240 spin_lock_irqsave(&port->port_lock, flags);
1241 if (port->open_count == 0 && !port->openclose)
1242 gs_buf_free(&port->port_write_buf);
1243 gs_free_requests(gser->out, &port->read_pool);
1244 gs_free_requests(gser->in, &port->write_pool);
1245 spin_unlock_irqrestore(&port->port_lock, flags);
1246}
diff --git a/drivers/usb/gadget/u_serial.h b/drivers/usb/gadget/u_serial.h
new file mode 100644
index 000000000000..7b561138f90e
--- /dev/null
+++ b/drivers/usb/gadget/u_serial.h
@@ -0,0 +1,58 @@
1/*
2 * u_serial.h - interface to USB gadget "serial port"/TTY utilities
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
6 *
7 * This software is distributed under the terms of the GNU General
8 * Public License ("GPL") as published by the Free Software Foundation,
9 * either version 2 of that License or (at your option) any later version.
10 */
11
12#ifndef __U_SERIAL_H
13#define __U_SERIAL_H
14
15#include <linux/usb/composite.h>
16#include <linux/usb/cdc.h>
17
18/*
19 * One non-multiplexed "serial" I/O port ... there can be several of these
20 * on any given USB peripheral device, if it provides enough endpoints.
21 *
22 * The "u_serial" utility component exists to do one thing: manage TTY
23 * style I/O using the USB peripheral endpoints listed here, including
24 * hookups to sysfs and /dev for each logical "tty" device.
25 *
26 * REVISIT need TTY --> USB event flow too, so ACM can report open/close
27 * as carrier detect events. Model after ECM. There's more ACM state too.
28 *
29 * REVISIT someday, allow multiplexing several TTYs over these endpoints.
30 */
31struct gserial {
32 struct usb_function func;
33
34 /* port is managed by gserial_{connect,disconnect} */
35 struct gs_port *ioport;
36
37 struct usb_ep *in;
38 struct usb_ep *out;
39 struct usb_endpoint_descriptor *in_desc;
40 struct usb_endpoint_descriptor *out_desc;
41
42 /* REVISIT avoid this CDC-ACM support harder ... */
43 struct usb_cdc_line_coding port_line_coding; /* 9600-8-N-1 etc */
44};
45
46/* port setup/teardown is handled by gadget driver */
47int gserial_setup(struct usb_gadget *g, unsigned n_ports);
48void gserial_cleanup(void);
49
50/* connect/disconnect is handled by individual functions */
51int gserial_connect(struct gserial *, u8 port_num);
52void gserial_disconnect(struct gserial *);
53
54/* functions are bound to configurations by a config or gadget driver */
55int acm_bind_config(struct usb_configuration *c, u8 port_num);
56int gser_bind_config(struct usb_configuration *c, u8 port_num);
57
58#endif /* __U_SERIAL_H */
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index fce4924dbbe8..aa0bd4f126a1 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -1,8 +1,8 @@
1/* 1/*
2 * zero.c -- Gadget Zero, for USB development 2 * zero.c -- Gadget Zero, for USB development
3 * 3 *
4 * Copyright (C) 2003-2007 David Brownell 4 * Copyright (C) 2003-2008 David Brownell
5 * All rights reserved. 5 * Copyright (C) 2008 by Nokia Corporation
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 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 8 * it under the terms of the GNU General Public License as published by
@@ -30,12 +30,7 @@
30 * 30 *
31 * It supports two similar configurations. One sinks whatever the usb host 31 * It supports two similar configurations. One sinks whatever the usb host
32 * writes, and in return sources zeroes. The other loops whatever the host 32 * writes, and in return sources zeroes. The other loops whatever the host
33 * writes back, so the host can read it. Module options include: 33 * writes back, so the host can read it.
34 *
35 * buflen=N default N=4096, buffer size used
36 * qlen=N default N=32, how many buffers in the loopback queue
37 * loopdefault default false, list loopback config first
38 * autoresume=N default N=0, seconds before triggering remote wakeup
39 * 34 *
40 * Many drivers will only have one configuration, letting them be much 35 * Many drivers will only have one configuration, letting them be much
41 * simpler if they also don't support high speed operation (like this 36 * simpler if they also don't support high speed operation (like this
@@ -47,94 +42,35 @@
47 * work with low capability USB controllers without four bulk endpoints. 42 * work with low capability USB controllers without four bulk endpoints.
48 */ 43 */
49 44
45/*
46 * driver assumes self-powered hardware, and
47 * has no way for users to trigger remote wakeup.
48 */
49
50/* #define VERBOSE_DEBUG */ 50/* #define VERBOSE_DEBUG */
51 51
52#include <linux/kernel.h> 52#include <linux/kernel.h>
53#include <linux/utsname.h> 53#include <linux/utsname.h>
54#include <linux/device.h> 54#include <linux/device.h>
55 55
56#include <linux/usb/ch9.h> 56#include "g_zero.h"
57#include <linux/usb/gadget.h>
58
59#include "gadget_chips.h" 57#include "gadget_chips.h"
60 58
61 59
62/*-------------------------------------------------------------------------*/ 60/*-------------------------------------------------------------------------*/
63 61
64#define DRIVER_VERSION "Earth Day 2008" 62#define DRIVER_VERSION "Cinco de Mayo 2008"
65 63
66static const char shortname[] = "zero";
67static const char longname[] = "Gadget Zero"; 64static const char longname[] = "Gadget Zero";
68 65
69static const char source_sink[] = "source and sink data"; 66unsigned buflen = 4096;
70static const char loopback[] = "loop input to output"; 67module_param(buflen, uint, 0);
71
72/*-------------------------------------------------------------------------*/
73
74/*
75 * driver assumes self-powered hardware, and
76 * has no way for users to trigger remote wakeup.
77 *
78 * this version autoconfigures as much as possible,
79 * which is reasonable for most "bulk-only" drivers.
80 */
81static const char *EP_IN_NAME; /* source */
82static const char *EP_OUT_NAME; /* sink */
83
84/*-------------------------------------------------------------------------*/
85
86/* big enough to hold our biggest descriptor */
87#define USB_BUFSIZ 256
88
89struct zero_dev {
90 spinlock_t lock;
91 struct usb_gadget *gadget;
92 struct usb_request *req; /* for control responses */
93
94 /* when configured, we have one of two configs:
95 * - source data (in to host) and sink it (out from host)
96 * - or loop it back (out from host back in to host)
97 */
98 u8 config;
99 struct usb_ep *in_ep, *out_ep;
100
101 /* autoresume timer */
102 struct timer_list resume;
103};
104
105#define DBG(d, fmt, args...) \
106 dev_dbg(&(d)->gadget->dev , fmt , ## args)
107#define VDBG(d, fmt, args...) \
108 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
109#define ERROR(d, fmt, args...) \
110 dev_err(&(d)->gadget->dev , fmt , ## args)
111#define WARN(d, fmt, args...) \
112 dev_warn(&(d)->gadget->dev , fmt , ## args)
113#define INFO(d, fmt, args...) \
114 dev_info(&(d)->gadget->dev , fmt , ## args)
115
116/*-------------------------------------------------------------------------*/
117
118static unsigned buflen = 4096;
119static unsigned qlen = 32;
120static unsigned pattern = 0;
121
122module_param(buflen, uint, S_IRUGO);
123module_param(qlen, uint, S_IRUGO);
124module_param(pattern, uint, S_IRUGO|S_IWUSR);
125
126/*
127 * if it's nonzero, autoresume says how many seconds to wait
128 * before trying to wake up the host after suspend.
129 */
130static unsigned autoresume = 0;
131module_param(autoresume, uint, 0);
132 68
133/* 69/*
134 * Normally the "loopback" configuration is second (index 1) so 70 * Normally the "loopback" configuration is second (index 1) so
135 * it's not the default. Here's where to change that order, to 71 * it's not the default. Here's where to change that order, to
136 * work better with hosts where config changes are problematic. 72 * work better with hosts where config changes are problematic or
137 * Or controllers (like superh) that only support one config. 73 * controllers (like original superh) that only support one config.
138 */ 74 */
139static int loopdefault = 0; 75static int loopdefault = 0;
140module_param(loopdefault, bool, S_IRUGO|S_IWUSR); 76module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
@@ -156,24 +92,6 @@ module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
156 92
157/*-------------------------------------------------------------------------*/ 93/*-------------------------------------------------------------------------*/
158 94
159/*
160 * DESCRIPTORS ... most are static, but strings and (full)
161 * configuration descriptors are built on demand.
162 */
163
164#define STRING_MANUFACTURER 25
165#define STRING_PRODUCT 42
166#define STRING_SERIAL 101
167#define STRING_SOURCE_SINK 250
168#define STRING_LOOPBACK 251
169
170/*
171 * This device advertises two configurations; these numbers work
172 * on a pxa250 as well as more flexible hardware.
173 */
174#define CONFIG_SOURCE_SINK 3
175#define CONFIG_LOOPBACK 2
176
177static struct usb_device_descriptor device_desc = { 95static struct usb_device_descriptor device_desc = {
178 .bLength = sizeof device_desc, 96 .bLength = sizeof device_desc,
179 .bDescriptorType = USB_DT_DEVICE, 97 .bDescriptorType = USB_DT_DEVICE,
@@ -183,248 +101,64 @@ static struct usb_device_descriptor device_desc = {
183 101
184 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM), 102 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
185 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM), 103 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
186 .iManufacturer = STRING_MANUFACTURER,
187 .iProduct = STRING_PRODUCT,
188 .iSerialNumber = STRING_SERIAL,
189 .bNumConfigurations = 2, 104 .bNumConfigurations = 2,
190}; 105};
191 106
192static struct usb_config_descriptor source_sink_config = { 107#ifdef CONFIG_USB_OTG
193 .bLength = sizeof source_sink_config,
194 .bDescriptorType = USB_DT_CONFIG,
195
196 /* compute wTotalLength on the fly */
197 .bNumInterfaces = 1,
198 .bConfigurationValue = CONFIG_SOURCE_SINK,
199 .iConfiguration = STRING_SOURCE_SINK,
200 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
201 .bMaxPower = 1, /* self-powered */
202};
203
204static struct usb_config_descriptor loopback_config = {
205 .bLength = sizeof loopback_config,
206 .bDescriptorType = USB_DT_CONFIG,
207
208 /* compute wTotalLength on the fly */
209 .bNumInterfaces = 1,
210 .bConfigurationValue = CONFIG_LOOPBACK,
211 .iConfiguration = STRING_LOOPBACK,
212 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
213 .bMaxPower = 1, /* self-powered */
214};
215
216static struct usb_otg_descriptor otg_descriptor = { 108static struct usb_otg_descriptor otg_descriptor = {
217 .bLength = sizeof otg_descriptor, 109 .bLength = sizeof otg_descriptor,
218 .bDescriptorType = USB_DT_OTG, 110 .bDescriptorType = USB_DT_OTG,
219 111
220 .bmAttributes = USB_OTG_SRP, 112 /* REVISIT SRP-only hardware is possible, although
221}; 113 * it would not be called "OTG" ...
222 114 */
223/* one interface in each configuration */ 115 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
224
225static const struct usb_interface_descriptor source_sink_intf = {
226 .bLength = sizeof source_sink_intf,
227 .bDescriptorType = USB_DT_INTERFACE,
228
229 .bNumEndpoints = 2,
230 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
231 .iInterface = STRING_SOURCE_SINK,
232};
233
234static const struct usb_interface_descriptor loopback_intf = {
235 .bLength = sizeof loopback_intf,
236 .bDescriptorType = USB_DT_INTERFACE,
237
238 .bNumEndpoints = 2,
239 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
240 .iInterface = STRING_LOOPBACK,
241};
242
243/* two full speed bulk endpoints; their use is config-dependent */
244
245static struct usb_endpoint_descriptor fs_source_desc = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251};
252
253static struct usb_endpoint_descriptor fs_sink_desc = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259};
260
261static const struct usb_descriptor_header *fs_source_sink_function[] = {
262 (struct usb_descriptor_header *) &otg_descriptor,
263 (struct usb_descriptor_header *) &source_sink_intf,
264 (struct usb_descriptor_header *) &fs_sink_desc,
265 (struct usb_descriptor_header *) &fs_source_desc,
266 NULL,
267};
268
269static const struct usb_descriptor_header *fs_loopback_function[] = {
270 (struct usb_descriptor_header *) &otg_descriptor,
271 (struct usb_descriptor_header *) &loopback_intf,
272 (struct usb_descriptor_header *) &fs_sink_desc,
273 (struct usb_descriptor_header *) &fs_source_desc,
274 NULL,
275};
276
277/*
278 * usb 2.0 devices need to expose both high speed and full speed
279 * descriptors, unless they only run at full speed.
280 *
281 * that means alternate endpoint descriptors (bigger packets)
282 * and a "device qualifier" ... plus more construction options
283 * for the config descriptor.
284 */
285
286static struct usb_endpoint_descriptor hs_source_desc = {
287 .bLength = USB_DT_ENDPOINT_SIZE,
288 .bDescriptorType = USB_DT_ENDPOINT,
289
290 .bmAttributes = USB_ENDPOINT_XFER_BULK,
291 .wMaxPacketSize = __constant_cpu_to_le16(512),
292};
293
294static struct usb_endpoint_descriptor hs_sink_desc = {
295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
297
298 .bmAttributes = USB_ENDPOINT_XFER_BULK,
299 .wMaxPacketSize = __constant_cpu_to_le16(512),
300};
301
302static struct usb_qualifier_descriptor dev_qualifier = {
303 .bLength = sizeof dev_qualifier,
304 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
305
306 .bcdUSB = __constant_cpu_to_le16(0x0200),
307 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
308
309 .bNumConfigurations = 2,
310}; 116};
311 117
312static const struct usb_descriptor_header *hs_source_sink_function[] = { 118const struct usb_descriptor_header *otg_desc[] = {
313 (struct usb_descriptor_header *) &otg_descriptor, 119 (struct usb_descriptor_header *) &otg_descriptor,
314 (struct usb_descriptor_header *) &source_sink_intf,
315 (struct usb_descriptor_header *) &hs_source_desc,
316 (struct usb_descriptor_header *) &hs_sink_desc,
317 NULL, 120 NULL,
318}; 121};
122#endif
319 123
320static const struct usb_descriptor_header *hs_loopback_function[] = { 124/* string IDs are assigned dynamically */
321 (struct usb_descriptor_header *) &otg_descriptor,
322 (struct usb_descriptor_header *) &loopback_intf,
323 (struct usb_descriptor_header *) &hs_source_desc,
324 (struct usb_descriptor_header *) &hs_sink_desc,
325 NULL,
326};
327 125
328/* maxpacket and other transfer characteristics vary by speed. */ 126#define STRING_MANUFACTURER_IDX 0
329static inline struct usb_endpoint_descriptor * 127#define STRING_PRODUCT_IDX 1
330ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 128#define STRING_SERIAL_IDX 2
331 struct usb_endpoint_descriptor *fs)
332{
333 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
334 return hs;
335 return fs;
336}
337 129
338static char manufacturer[50]; 130static char manufacturer[50];
339 131
340/* default serial number takes at least two packets */ 132/* default serial number takes at least two packets */
341static char serial[] = "0123456789.0123456789.0123456789"; 133static char serial[] = "0123456789.0123456789.0123456789";
342 134
343 135static struct usb_string strings_dev[] = {
344/* static strings, in UTF-8 */ 136 [STRING_MANUFACTURER_IDX].s = manufacturer,
345static struct usb_string strings[] = { 137 [STRING_PRODUCT_IDX].s = longname,
346 { STRING_MANUFACTURER, manufacturer, }, 138 [STRING_SERIAL_IDX].s = serial,
347 { STRING_PRODUCT, longname, },
348 { STRING_SERIAL, serial, },
349 { STRING_LOOPBACK, loopback, },
350 { STRING_SOURCE_SINK, source_sink, },
351 { } /* end of list */ 139 { } /* end of list */
352}; 140};
353 141
354static struct usb_gadget_strings stringtab = { 142static struct usb_gadget_strings stringtab_dev = {
355 .language = 0x0409, /* en-us */ 143 .language = 0x0409, /* en-us */
356 .strings = strings, 144 .strings = strings_dev,
357}; 145};
358 146
359/* 147static struct usb_gadget_strings *dev_strings[] = {
360 * config descriptors are also handcrafted. these must agree with code 148 &stringtab_dev,
361 * that sets configurations, and with code managing interfaces and their 149 NULL,
362 * altsettings. other complexity may come from: 150};
363 *
364 * - high speed support, including "other speed config" rules
365 * - multiple configurations
366 * - interfaces with alternate settings
367 * - embedded class or vendor-specific descriptors
368 *
369 * this handles high speed, and has a second config that could as easily
370 * have been an alternate interface setting (on most hardware).
371 *
372 * NOTE: to demonstrate (and test) more USB capabilities, this driver
373 * should include an altsetting to test interrupt transfers, including
374 * high bandwidth modes at high speed. (Maybe work like Intel's test
375 * device?)
376 */
377static int config_buf(struct usb_gadget *gadget,
378 u8 *buf, u8 type, unsigned index)
379{
380 int is_source_sink;
381 int len;
382 const struct usb_descriptor_header **function;
383 int hs = 0;
384
385 /* two configurations will always be index 0 and index 1 */
386 if (index > 1)
387 return -EINVAL;
388 is_source_sink = loopdefault ? (index == 1) : (index == 0);
389
390 if (gadget_is_dualspeed(gadget)) {
391 hs = (gadget->speed == USB_SPEED_HIGH);
392 if (type == USB_DT_OTHER_SPEED_CONFIG)
393 hs = !hs;
394 }
395 if (hs)
396 function = is_source_sink
397 ? hs_source_sink_function
398 : hs_loopback_function;
399 else
400 function = is_source_sink
401 ? fs_source_sink_function
402 : fs_loopback_function;
403
404 /* for now, don't advertise srp-only devices */
405 if (!gadget_is_otg(gadget))
406 function++;
407
408 len = usb_gadget_config_buf(is_source_sink
409 ? &source_sink_config
410 : &loopback_config,
411 buf, USB_BUFSIZ, function);
412 if (len < 0)
413 return len;
414 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
415 return len;
416}
417 151
418/*-------------------------------------------------------------------------*/ 152/*-------------------------------------------------------------------------*/
419 153
420static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) 154struct usb_request *alloc_ep_req(struct usb_ep *ep)
421{ 155{
422 struct usb_request *req; 156 struct usb_request *req;
423 157
424 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 158 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
425 if (req) { 159 if (req) {
426 req->length = length; 160 req->length = buflen;
427 req->buf = kmalloc(length, GFP_ATOMIC); 161 req->buf = kmalloc(buflen, GFP_ATOMIC);
428 if (!req->buf) { 162 if (!req->buf) {
429 usb_ep_free_request(ep, req); 163 usb_ep_free_request(ep, req);
430 req = NULL; 164 req = NULL;
@@ -433,681 +167,73 @@ static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
433 return req; 167 return req;
434} 168}
435 169
436static void free_ep_req(struct usb_ep *ep, struct usb_request *req) 170void free_ep_req(struct usb_ep *ep, struct usb_request *req)
437{ 171{
438 kfree(req->buf); 172 kfree(req->buf);
439 usb_ep_free_request(ep, req); 173 usb_ep_free_request(ep, req);
440} 174}
441 175
442/*-------------------------------------------------------------------------*/ 176static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
443
444/*
445 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals,
446 * this just sinks bulk packets OUT to the peripheral and sources them IN
447 * to the host, optionally with specific data patterns.
448 *
449 * In terms of control messaging, this supports all the standard requests
450 * plus two that support control-OUT tests.
451 *
452 * Note that because this doesn't queue more than one request at a time,
453 * some other function must be used to test queueing logic. The network
454 * link (g_ether) is probably the best option for that.
455 */
456
457/* optionally require specific source/sink data patterns */
458
459static int
460check_read_data(
461 struct zero_dev *dev,
462 struct usb_ep *ep,
463 struct usb_request *req
464)
465{ 177{
466 unsigned i; 178 int value;
467 u8 *buf = req->buf; 179
468 180 if (ep->driver_data) {
469 for (i = 0; i < req->actual; i++, buf++) { 181 value = usb_ep_disable(ep);
470 switch (pattern) { 182 if (value < 0)
471 /* all-zeroes has no synchronization issues */ 183 DBG(cdev, "disable %s --> %d\n",
472 case 0: 184 ep->name, value);
473 if (*buf == 0) 185 ep->driver_data = NULL;
474 continue;
475 break;
476 /* mod63 stays in sync with short-terminated transfers,
477 * or otherwise when host and gadget agree on how large
478 * each usb transfer request should be. resync is done
479 * with set_interface or set_config.
480 */
481 case 1:
482 if (*buf == (u8)(i % 63))
483 continue;
484 break;
485 }
486 ERROR(dev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
487 usb_ep_set_halt(ep);
488 return -EINVAL;
489 } 186 }
490 return 0;
491} 187}
492 188
493static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) 189void disable_endpoints(struct usb_composite_dev *cdev,
190 struct usb_ep *in, struct usb_ep *out)
494{ 191{
495 unsigned i; 192 disable_ep(cdev, in);
496 u8 *buf = req->buf; 193 disable_ep(cdev, out);
497
498 switch (pattern) {
499 case 0:
500 memset(req->buf, 0, req->length);
501 break;
502 case 1:
503 for (i = 0; i < req->length; i++)
504 *buf++ = (u8) (i % 63);
505 break;
506 }
507}
508
509/* if there is only one request in the queue, there'll always be an
510 * irq delay between end of one request and start of the next.
511 * that prevents using hardware dma queues.
512 */
513static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
514{
515 struct zero_dev *dev = ep->driver_data;
516 int status = req->status;
517
518 switch (status) {
519
520 case 0: /* normal completion? */
521 if (ep == dev->out_ep) {
522 check_read_data(dev, ep, req);
523 memset(req->buf, 0x55, req->length);
524 } else
525 reinit_write_data(ep, req);
526 break;
527
528 /* this endpoint is normally active while we're configured */
529 case -ECONNABORTED: /* hardware forced ep reset */
530 case -ECONNRESET: /* request dequeued */
531 case -ESHUTDOWN: /* disconnect from host */
532 VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
533 req->actual, req->length);
534 if (ep == dev->out_ep)
535 check_read_data(dev, ep, req);
536 free_ep_req(ep, req);
537 return;
538
539 case -EOVERFLOW: /* buffer overrun on read means that
540 * we didn't provide a big enough
541 * buffer.
542 */
543 default:
544#if 1
545 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
546 status, req->actual, req->length);
547#endif
548 case -EREMOTEIO: /* short read */
549 break;
550 }
551
552 status = usb_ep_queue(ep, req, GFP_ATOMIC);
553 if (status) {
554 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
555 ep->name, req->length, status);
556 usb_ep_set_halt(ep);
557 /* FIXME recover later ... somehow */
558 }
559}
560
561static struct usb_request *source_sink_start_ep(struct usb_ep *ep)
562{
563 struct usb_request *req;
564 int status;
565
566 req = alloc_ep_req(ep, buflen);
567 if (!req)
568 return NULL;
569
570 memset(req->buf, 0, req->length);
571 req->complete = source_sink_complete;
572
573 if (strcmp(ep->name, EP_IN_NAME) == 0)
574 reinit_write_data(ep, req);
575 else
576 memset(req->buf, 0x55, req->length);
577
578 status = usb_ep_queue(ep, req, GFP_ATOMIC);
579 if (status) {
580 struct zero_dev *dev = ep->driver_data;
581
582 ERROR(dev, "start %s --> %d\n", ep->name, status);
583 free_ep_req(ep, req);
584 req = NULL;
585 }
586
587 return req;
588}
589
590static int set_source_sink_config(struct zero_dev *dev)
591{
592 int result = 0;
593 struct usb_ep *ep;
594 struct usb_gadget *gadget = dev->gadget;
595
596 gadget_for_each_ep(ep, gadget) {
597 const struct usb_endpoint_descriptor *d;
598
599 /* one endpoint writes (sources) zeroes in (to the host) */
600 if (strcmp(ep->name, EP_IN_NAME) == 0) {
601 d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
602 result = usb_ep_enable(ep, d);
603 if (result == 0) {
604 ep->driver_data = dev;
605 if (source_sink_start_ep(ep) != NULL) {
606 dev->in_ep = ep;
607 continue;
608 }
609 usb_ep_disable(ep);
610 result = -EIO;
611 }
612
613 /* one endpoint reads (sinks) anything out (from the host) */
614 } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
615 d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
616 result = usb_ep_enable(ep, d);
617 if (result == 0) {
618 ep->driver_data = dev;
619 if (source_sink_start_ep(ep) != NULL) {
620 dev->out_ep = ep;
621 continue;
622 }
623 usb_ep_disable(ep);
624 result = -EIO;
625 }
626
627 /* ignore any other endpoints */
628 } else
629 continue;
630
631 /* stop on error */
632 ERROR(dev, "can't start %s, result %d\n", ep->name, result);
633 break;
634 }
635 if (result == 0)
636 DBG(dev, "buflen %d\n", buflen);
637
638 /* caller is responsible for cleanup on error */
639 return result;
640}
641
642/*-------------------------------------------------------------------------*/
643
644static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
645{
646 struct zero_dev *dev = ep->driver_data;
647 int status = req->status;
648
649 switch (status) {
650
651 case 0: /* normal completion? */
652 if (ep == dev->out_ep) {
653 /* loop this OUT packet back IN to the host */
654 req->zero = (req->actual < req->length);
655 req->length = req->actual;
656 status = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
657 if (status == 0)
658 return;
659
660 /* "should never get here" */
661 ERROR(dev, "can't loop %s to %s: %d\n",
662 ep->name, dev->in_ep->name,
663 status);
664 }
665
666 /* queue the buffer for some later OUT packet */
667 req->length = buflen;
668 status = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
669 if (status == 0)
670 return;
671
672 /* "should never get here" */
673 /* FALLTHROUGH */
674
675 default:
676 ERROR(dev, "%s loop complete --> %d, %d/%d\n", ep->name,
677 status, req->actual, req->length);
678 /* FALLTHROUGH */
679
680 /* NOTE: since this driver doesn't maintain an explicit record
681 * of requests it submitted (just maintains qlen count), we
682 * rely on the hardware driver to clean up on disconnect or
683 * endpoint disable.
684 */
685 case -ECONNABORTED: /* hardware forced ep reset */
686 case -ECONNRESET: /* request dequeued */
687 case -ESHUTDOWN: /* disconnect from host */
688 free_ep_req(ep, req);
689 return;
690 }
691}
692
693static int set_loopback_config(struct zero_dev *dev)
694{
695 int result = 0;
696 struct usb_ep *ep;
697 struct usb_gadget *gadget = dev->gadget;
698
699 gadget_for_each_ep(ep, gadget) {
700 const struct usb_endpoint_descriptor *d;
701
702 /* one endpoint writes data back IN to the host */
703 if (strcmp(ep->name, EP_IN_NAME) == 0) {
704 d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
705 result = usb_ep_enable(ep, d);
706 if (result == 0) {
707 ep->driver_data = dev;
708 dev->in_ep = ep;
709 continue;
710 }
711
712 /* one endpoint just reads OUT packets */
713 } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
714 d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
715 result = usb_ep_enable(ep, d);
716 if (result == 0) {
717 ep->driver_data = dev;
718 dev->out_ep = ep;
719 continue;
720 }
721
722 /* ignore any other endpoints */
723 } else
724 continue;
725
726 /* stop on error */
727 ERROR(dev, "can't enable %s, result %d\n", ep->name, result);
728 break;
729 }
730
731 /* allocate a bunch of read buffers and queue them all at once.
732 * we buffer at most 'qlen' transfers; fewer if any need more
733 * than 'buflen' bytes each.
734 */
735 if (result == 0) {
736 struct usb_request *req;
737 unsigned i;
738
739 ep = dev->out_ep;
740 for (i = 0; i < qlen && result == 0; i++) {
741 req = alloc_ep_req(ep, buflen);
742 if (req) {
743 req->complete = loopback_complete;
744 result = usb_ep_queue(ep, req, GFP_ATOMIC);
745 if (result)
746 DBG(dev, "%s queue req --> %d\n",
747 ep->name, result);
748 } else
749 result = -ENOMEM;
750 }
751 }
752 if (result == 0)
753 DBG(dev, "qlen %d, buflen %d\n", qlen, buflen);
754
755 /* caller is responsible for cleanup on error */
756 return result;
757}
758
759/*-------------------------------------------------------------------------*/
760
761static void zero_reset_config(struct zero_dev *dev)
762{
763 if (dev->config == 0)
764 return;
765
766 DBG(dev, "reset config\n");
767
768 /* just disable endpoints, forcing completion of pending i/o.
769 * all our completion handlers free their requests in this case.
770 */
771 if (dev->in_ep) {
772 usb_ep_disable(dev->in_ep);
773 dev->in_ep = NULL;
774 }
775 if (dev->out_ep) {
776 usb_ep_disable(dev->out_ep);
777 dev->out_ep = NULL;
778 }
779 dev->config = 0;
780 del_timer(&dev->resume);
781}
782
783/* change our operational config. this code must agree with the code
784 * that returns config descriptors, and altsetting code.
785 *
786 * it's also responsible for power management interactions. some
787 * configurations might not work with our current power sources.
788 *
789 * note that some device controller hardware will constrain what this
790 * code can do, perhaps by disallowing more than one configuration or
791 * by limiting configuration choices (like the pxa2xx).
792 */
793static int zero_set_config(struct zero_dev *dev, unsigned number)
794{
795 int result = 0;
796 struct usb_gadget *gadget = dev->gadget;
797
798 if (number == dev->config)
799 return 0;
800
801 if (gadget_is_sa1100(gadget) && dev->config) {
802 /* tx fifo is full, but we can't clear it...*/
803 ERROR(dev, "can't change configurations\n");
804 return -ESPIPE;
805 }
806 zero_reset_config(dev);
807
808 switch (number) {
809 case CONFIG_SOURCE_SINK:
810 result = set_source_sink_config(dev);
811 break;
812 case CONFIG_LOOPBACK:
813 result = set_loopback_config(dev);
814 break;
815 default:
816 result = -EINVAL;
817 /* FALL THROUGH */
818 case 0:
819 return result;
820 }
821
822 if (!result && (!dev->in_ep || !dev->out_ep))
823 result = -ENODEV;
824 if (result)
825 zero_reset_config(dev);
826 else {
827 char *speed;
828
829 switch (gadget->speed) {
830 case USB_SPEED_LOW: speed = "low"; break;
831 case USB_SPEED_FULL: speed = "full"; break;
832 case USB_SPEED_HIGH: speed = "high"; break;
833 default: speed = "?"; break;
834 }
835
836 dev->config = number;
837 INFO(dev, "%s speed config #%d: %s\n", speed, number,
838 (number == CONFIG_SOURCE_SINK)
839 ? source_sink : loopback);
840 }
841 return result;
842}
843
844/*-------------------------------------------------------------------------*/
845
846static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)
847{
848 if (req->status || req->actual != req->length)
849 DBG((struct zero_dev *) ep->driver_data,
850 "setup complete --> %d, %d/%d\n",
851 req->status, req->actual, req->length);
852}
853
854/*
855 * The setup() callback implements all the ep0 functionality that's
856 * not handled lower down, in hardware or the hardware driver (like
857 * device and endpoint feature flags, and their status). It's all
858 * housekeeping for the gadget function we're implementing. Most of
859 * the work is in config-specific setup.
860 */
861static int
862zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
863{
864 struct zero_dev *dev = get_gadget_data(gadget);
865 struct usb_request *req = dev->req;
866 int value = -EOPNOTSUPP;
867 u16 w_index = le16_to_cpu(ctrl->wIndex);
868 u16 w_value = le16_to_cpu(ctrl->wValue);
869 u16 w_length = le16_to_cpu(ctrl->wLength);
870
871 /* usually this stores reply data in the pre-allocated ep0 buffer,
872 * but config change events will reconfigure hardware.
873 */
874 req->zero = 0;
875 switch (ctrl->bRequest) {
876
877 case USB_REQ_GET_DESCRIPTOR:
878 if (ctrl->bRequestType != USB_DIR_IN)
879 goto unknown;
880 switch (w_value >> 8) {
881
882 case USB_DT_DEVICE:
883 value = min(w_length, (u16) sizeof device_desc);
884 memcpy(req->buf, &device_desc, value);
885 break;
886 case USB_DT_DEVICE_QUALIFIER:
887 if (!gadget_is_dualspeed(gadget))
888 break;
889 value = min(w_length, (u16) sizeof dev_qualifier);
890 memcpy(req->buf, &dev_qualifier, value);
891 break;
892
893 case USB_DT_OTHER_SPEED_CONFIG:
894 if (!gadget_is_dualspeed(gadget))
895 break;
896 // FALLTHROUGH
897 case USB_DT_CONFIG:
898 value = config_buf(gadget, req->buf,
899 w_value >> 8,
900 w_value & 0xff);
901 if (value >= 0)
902 value = min(w_length, (u16) value);
903 break;
904
905 case USB_DT_STRING:
906 /* wIndex == language code.
907 * this driver only handles one language, you can
908 * add string tables for other languages, using
909 * any UTF-8 characters
910 */
911 value = usb_gadget_get_string(&stringtab,
912 w_value & 0xff, req->buf);
913 if (value >= 0)
914 value = min(w_length, (u16) value);
915 break;
916 }
917 break;
918
919 /* currently two configs, two speeds */
920 case USB_REQ_SET_CONFIGURATION:
921 if (ctrl->bRequestType != 0)
922 goto unknown;
923 if (gadget->a_hnp_support)
924 DBG(dev, "HNP available\n");
925 else if (gadget->a_alt_hnp_support)
926 DBG(dev, "HNP needs a different root port\n");
927 else
928 VDBG(dev, "HNP inactive\n");
929 spin_lock(&dev->lock);
930 value = zero_set_config(dev, w_value);
931 spin_unlock(&dev->lock);
932 break;
933 case USB_REQ_GET_CONFIGURATION:
934 if (ctrl->bRequestType != USB_DIR_IN)
935 goto unknown;
936 *(u8 *)req->buf = dev->config;
937 value = min(w_length, (u16) 1);
938 break;
939
940 /* until we add altsetting support, or other interfaces,
941 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
942 * and already killed pending endpoint I/O.
943 */
944 case USB_REQ_SET_INTERFACE:
945 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
946 goto unknown;
947 spin_lock(&dev->lock);
948 if (dev->config && w_index == 0 && w_value == 0) {
949 u8 config = dev->config;
950
951 /* resets interface configuration, forgets about
952 * previous transaction state (queued bufs, etc)
953 * and re-inits endpoint state (toggle etc)
954 * no response queued, just zero status == success.
955 * if we had more than one interface we couldn't
956 * use this "reset the config" shortcut.
957 */
958 zero_reset_config(dev);
959 zero_set_config(dev, config);
960 value = 0;
961 }
962 spin_unlock(&dev->lock);
963 break;
964 case USB_REQ_GET_INTERFACE:
965 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
966 goto unknown;
967 if (!dev->config)
968 break;
969 if (w_index != 0) {
970 value = -EDOM;
971 break;
972 }
973 *(u8 *)req->buf = 0;
974 value = min(w_length, (u16) 1);
975 break;
976
977 /*
978 * These are the same vendor-specific requests supported by
979 * Intel's USB 2.0 compliance test devices. We exceed that
980 * device spec by allowing multiple-packet requests.
981 */
982 case 0x5b: /* control WRITE test -- fill the buffer */
983 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
984 goto unknown;
985 if (w_value || w_index)
986 break;
987 /* just read that many bytes into the buffer */
988 if (w_length > USB_BUFSIZ)
989 break;
990 value = w_length;
991 break;
992 case 0x5c: /* control READ test -- return the buffer */
993 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
994 goto unknown;
995 if (w_value || w_index)
996 break;
997 /* expect those bytes are still in the buffer; send back */
998 if (w_length > USB_BUFSIZ
999 || w_length != req->length)
1000 break;
1001 value = w_length;
1002 break;
1003
1004 default:
1005unknown:
1006 VDBG(dev,
1007 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1008 ctrl->bRequestType, ctrl->bRequest,
1009 w_value, w_index, w_length);
1010 }
1011
1012 /* respond with data transfer before status phase? */
1013 if (value >= 0) {
1014 req->length = value;
1015 req->zero = value < w_length;
1016 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1017 if (value < 0) {
1018 DBG(dev, "ep_queue --> %d\n", value);
1019 req->status = 0;
1020 zero_setup_complete(gadget->ep0, req);
1021 }
1022 }
1023
1024 /* device either stalls (value < 0) or reports success */
1025 return value;
1026}
1027
1028static void zero_disconnect(struct usb_gadget *gadget)
1029{
1030 struct zero_dev *dev = get_gadget_data(gadget);
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&dev->lock, flags);
1034 zero_reset_config(dev);
1035
1036 /* a more significant application might have some non-usb
1037 * activities to quiesce here, saving resources like power
1038 * or pushing the notification up a network stack.
1039 */
1040 spin_unlock_irqrestore(&dev->lock, flags);
1041
1042 /* next we may get setup() calls to enumerate new connections;
1043 * or an unbind() during shutdown (including removing module).
1044 */
1045}
1046
1047static void zero_autoresume(unsigned long _dev)
1048{
1049 struct zero_dev *dev = (struct zero_dev *) _dev;
1050 int status;
1051
1052 /* normally the host would be woken up for something
1053 * more significant than just a timer firing...
1054 */
1055 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1056 status = usb_gadget_wakeup(dev->gadget);
1057 DBG(dev, "wakeup --> %d\n", status);
1058 }
1059} 194}
1060 195
1061/*-------------------------------------------------------------------------*/ 196/*-------------------------------------------------------------------------*/
1062 197
1063static void zero_unbind(struct usb_gadget *gadget) 198static int __init zero_bind(struct usb_composite_dev *cdev)
1064{ 199{
1065 struct zero_dev *dev = get_gadget_data(gadget);
1066
1067 DBG(dev, "unbind\n");
1068
1069 /* we've already been disconnected ... no i/o is active */
1070 if (dev->req) {
1071 dev->req->length = USB_BUFSIZ;
1072 free_ep_req(gadget->ep0, dev->req);
1073 }
1074 del_timer_sync(&dev->resume);
1075 kfree(dev);
1076 set_gadget_data(gadget, NULL);
1077}
1078
1079static int __init zero_bind(struct usb_gadget *gadget)
1080{
1081 struct zero_dev *dev;
1082 struct usb_ep *ep;
1083 int gcnum; 200 int gcnum;
201 struct usb_gadget *gadget = cdev->gadget;
202 int id;
1084 203
1085 /* FIXME this can't yet work right with SH ... it has only 204 /* Allocate string descriptor numbers ... note that string
1086 * one configuration, numbered one. 205 * contents can be overridden by the composite_dev glue.
1087 */ 206 */
1088 if (gadget_is_sh(gadget)) 207 id = usb_string_id(cdev);
1089 return -ENODEV; 208 if (id < 0)
1090 209 return id;
1091 /* Bulk-only drivers like this one SHOULD be able to 210 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1092 * autoconfigure on any sane usb controller driver, 211 device_desc.iManufacturer = id;
1093 * but there may also be important quirks to address. 212
213 id = usb_string_id(cdev);
214 if (id < 0)
215 return id;
216 strings_dev[STRING_PRODUCT_IDX].id = id;
217 device_desc.iProduct = id;
218
219 id = usb_string_id(cdev);
220 if (id < 0)
221 return id;
222 strings_dev[STRING_SERIAL_IDX].id = id;
223 device_desc.iSerialNumber = id;
224
225 /* Register primary, then secondary configuration. Note that
226 * SH3 only allows one config...
1094 */ 227 */
1095 usb_ep_autoconfig_reset(gadget); 228 if (loopdefault) {
1096 ep = usb_ep_autoconfig(gadget, &fs_source_desc); 229 loopback_add(cdev);
1097 if (!ep) { 230 if (!gadget_is_sh(gadget))
1098autoconf_fail: 231 sourcesink_add(cdev);
1099 pr_err("%s: can't autoconfigure on %s\n", 232 } else {
1100 shortname, gadget->name); 233 sourcesink_add(cdev);
1101 return -ENODEV; 234 if (!gadget_is_sh(gadget))
235 loopback_add(cdev);
1102 } 236 }
1103 EP_IN_NAME = ep->name;
1104 ep->driver_data = ep; /* claim */
1105
1106 ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
1107 if (!ep)
1108 goto autoconf_fail;
1109 EP_OUT_NAME = ep->name;
1110 ep->driver_data = ep; /* claim */
1111 237
1112 gcnum = usb_gadget_controller_number(gadget); 238 gcnum = usb_gadget_controller_number(gadget);
1113 if (gcnum >= 0) 239 if (gcnum >= 0)
@@ -1115,144 +241,44 @@ autoconf_fail:
1115 else { 241 else {
1116 /* gadget zero is so simple (for now, no altsettings) that 242 /* gadget zero is so simple (for now, no altsettings) that
1117 * it SHOULD NOT have problems with bulk-capable hardware. 243 * it SHOULD NOT have problems with bulk-capable hardware.
1118 * so warn about unrcognized controllers, don't panic. 244 * so just warn about unrcognized controllers -- don't panic.
1119 * 245 *
1120 * things like configuration and altsetting numbering 246 * things like configuration and altsetting numbering
1121 * can need hardware-specific attention though. 247 * can need hardware-specific attention though.
1122 */ 248 */
1123 pr_warning("%s: controller '%s' not recognized\n", 249 pr_warning("%s: controller '%s' not recognized\n",
1124 shortname, gadget->name); 250 longname, gadget->name);
1125 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); 251 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1126 } 252 }
1127 253
1128 254
1129 /* ok, we made sense of the hardware ... */ 255 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
1130 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1131 if (!dev)
1132 return -ENOMEM;
1133 spin_lock_init(&dev->lock);
1134 dev->gadget = gadget;
1135 set_gadget_data(gadget, dev);
1136
1137 init_timer(&dev->resume);
1138 dev->resume.function = zero_autoresume;
1139 dev->resume.data = (unsigned long) dev;
1140
1141 /* preallocate control response and buffer */
1142 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1143 if (!dev->req)
1144 goto enomem;
1145 dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1146 if (!dev->req->buf)
1147 goto enomem;
1148
1149 dev->req->complete = zero_setup_complete;
1150
1151 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1152
1153 if (gadget_is_dualspeed(gadget)) {
1154 /* assume ep0 uses the same value for both speeds ... */
1155 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1156
1157 /* and that all endpoints are dual-speed */
1158 hs_source_desc.bEndpointAddress =
1159 fs_source_desc.bEndpointAddress;
1160 hs_sink_desc.bEndpointAddress =
1161 fs_sink_desc.bEndpointAddress;
1162 }
1163
1164 if (gadget_is_otg(gadget)) {
1165 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1166 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1167 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1168 }
1169
1170 usb_gadget_set_selfpowered(gadget);
1171
1172 if (autoresume) {
1173 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1174 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1175 }
1176
1177 gadget->ep0->driver_data = dev;
1178
1179 INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
1180 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
1181 EP_OUT_NAME, EP_IN_NAME);
1182 256
1183 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", 257 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
1184 init_utsname()->sysname, init_utsname()->release, 258 init_utsname()->sysname, init_utsname()->release,
1185 gadget->name); 259 gadget->name);
1186 260
1187 return 0; 261 return 0;
1188
1189enomem:
1190 zero_unbind(gadget);
1191 return -ENOMEM;
1192} 262}
1193 263
1194/*-------------------------------------------------------------------------*/ 264static struct usb_composite_driver zero_driver = {
1195 265 .name = "zero",
1196static void zero_suspend(struct usb_gadget *gadget) 266 .dev = &device_desc,
1197{ 267 .strings = dev_strings,
1198 struct zero_dev *dev = get_gadget_data(gadget);
1199
1200 if (gadget->speed == USB_SPEED_UNKNOWN)
1201 return;
1202
1203 if (autoresume) {
1204 mod_timer(&dev->resume, jiffies + (HZ * autoresume));
1205 DBG(dev, "suspend, wakeup in %d seconds\n", autoresume);
1206 } else
1207 DBG(dev, "suspend\n");
1208}
1209
1210static void zero_resume(struct usb_gadget *gadget)
1211{
1212 struct zero_dev *dev = get_gadget_data(gadget);
1213
1214 DBG(dev, "resume\n");
1215 del_timer(&dev->resume);
1216}
1217
1218
1219/*-------------------------------------------------------------------------*/
1220
1221static struct usb_gadget_driver zero_driver = {
1222#ifdef CONFIG_USB_GADGET_DUALSPEED
1223 .speed = USB_SPEED_HIGH,
1224#else
1225 .speed = USB_SPEED_FULL,
1226#endif
1227 .function = (char *) longname,
1228 .bind = zero_bind, 268 .bind = zero_bind,
1229 .unbind = __exit_p(zero_unbind),
1230
1231 .setup = zero_setup,
1232 .disconnect = zero_disconnect,
1233
1234 .suspend = zero_suspend,
1235 .resume = zero_resume,
1236
1237 .driver = {
1238 .name = (char *) shortname,
1239 .owner = THIS_MODULE,
1240 },
1241}; 269};
1242 270
1243MODULE_AUTHOR("David Brownell"); 271MODULE_AUTHOR("David Brownell");
1244MODULE_LICENSE("GPL"); 272MODULE_LICENSE("GPL");
1245 273
1246
1247static int __init init(void) 274static int __init init(void)
1248{ 275{
1249 return usb_gadget_register_driver(&zero_driver); 276 return usb_composite_register(&zero_driver);
1250} 277}
1251module_init(init); 278module_init(init);
1252 279
1253static void __exit cleanup(void) 280static void __exit cleanup(void)
1254{ 281{
1255 usb_gadget_unregister_driver(&zero_driver); 282 usb_composite_unregister(&zero_driver);
1256} 283}
1257module_exit(cleanup); 284module_exit(cleanup);
1258
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
index 08a4335401a9..bf69f4739107 100644
--- a/drivers/usb/host/ehci-au1xxx.c
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -19,263 +19,304 @@
19#define USB_MCFG_RDCOMB (1<<30) 19#define USB_MCFG_RDCOMB (1<<30)
20#define USB_MCFG_SSDEN (1<<23) 20#define USB_MCFG_SSDEN (1<<23)
21#define USB_MCFG_PHYPLLEN (1<<19) 21#define USB_MCFG_PHYPLLEN (1<<19)
22#define USB_MCFG_UCECLKEN (1<<18)
22#define USB_MCFG_EHCCLKEN (1<<17) 23#define USB_MCFG_EHCCLKEN (1<<17)
24#ifdef CONFIG_DMA_COHERENT
23#define USB_MCFG_UCAM (1<<7) 25#define USB_MCFG_UCAM (1<<7)
26#else
27#define USB_MCFG_UCAM (0)
28#endif
24#define USB_MCFG_EBMEN (1<<3) 29#define USB_MCFG_EBMEN (1<<3)
25#define USB_MCFG_EMEMEN (1<<2) 30#define USB_MCFG_EMEMEN (1<<2)
26 31
27#define USBH_ENABLE_CE (USB_MCFG_PHYPLLEN | USB_MCFG_EHCCLKEN) 32#define USBH_ENABLE_CE (USB_MCFG_PHYPLLEN | USB_MCFG_EHCCLKEN)
33#define USBH_ENABLE_INIT (USB_MCFG_PFEN | USB_MCFG_RDCOMB | \
34 USBH_ENABLE_CE | USB_MCFG_SSDEN | \
35 USB_MCFG_UCAM | USB_MCFG_EBMEN | \
36 USB_MCFG_EMEMEN)
28 37
29#ifdef CONFIG_DMA_COHERENT
30#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
31 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
32 | USB_MCFG_SSDEN | USB_MCFG_UCAM \
33 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
34#else
35#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
36 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
37 | USB_MCFG_SSDEN \
38 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
39#endif
40#define USBH_DISABLE (USB_MCFG_EBMEN | USB_MCFG_EMEMEN) 38#define USBH_DISABLE (USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
41 39
42extern int usb_disabled(void); 40extern int usb_disabled(void);
43 41
44/*-------------------------------------------------------------------------*/ 42static void au1xxx_start_ehc(void)
45
46static void au1xxx_start_ehc(struct platform_device *dev)
47{ 43{
48 pr_debug(__FILE__ ": starting Au1xxx EHCI USB Controller\n"); 44 /* enable clock to EHCI block and HS PHY PLL*/
49 45 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_CE, USB_HOST_CONFIG);
50 /* write HW defaults again in case Yamon cleared them */ 46 au_sync();
51 if (au_readl(USB_HOST_CONFIG) == 0) {
52 au_writel(0x00d02000, USB_HOST_CONFIG);
53 au_readl(USB_HOST_CONFIG);
54 udelay(1000);
55 }
56 /* enable host controller */
57 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
58 au_readl(USB_HOST_CONFIG);
59 udelay(1000);
60 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG),
61 USB_HOST_CONFIG);
62 au_readl(USB_HOST_CONFIG);
63 udelay(1000); 47 udelay(1000);
64 48
65 pr_debug(__FILE__ ": Clock to USB host has been enabled\n"); 49 /* enable EHCI mmio */
50 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
51 au_sync();
52 udelay(1000);
66} 53}
67 54
68static void au1xxx_stop_ehc(struct platform_device *dev) 55static void au1xxx_stop_ehc(void)
69{ 56{
70 pr_debug(__FILE__ ": stopping Au1xxx EHCI USB Controller\n"); 57 unsigned long c;
71 58
72 /* Disable mem */ 59 /* Disable mem */
73 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG); 60 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_DISABLE, USB_HOST_CONFIG);
61 au_sync();
74 udelay(1000); 62 udelay(1000);
75 /* Disable clock */ 63
76 au_writel(~USB_MCFG_EHCCLKEN & au_readl(USB_HOST_CONFIG), 64 /* Disable EHC clock. If the HS PHY is unused disable it too. */
77 USB_HOST_CONFIG); 65 c = au_readl(USB_HOST_CONFIG) & ~USB_MCFG_EHCCLKEN;
78 au_readl(USB_HOST_CONFIG); 66 if (!(c & USB_MCFG_UCECLKEN)) /* UDC disabled? */
67 c &= ~USB_MCFG_PHYPLLEN; /* yes: disable HS PHY PLL */
68 au_writel(c, USB_HOST_CONFIG);
69 au_sync();
79} 70}
80 71
81/*-------------------------------------------------------------------------*/ 72static const struct hc_driver ehci_au1xxx_hc_driver = {
73 .description = hcd_name,
74 .product_desc = "Au1xxx EHCI",
75 .hcd_priv_size = sizeof(struct ehci_hcd),
76
77 /*
78 * generic hardware linkage
79 */
80 .irq = ehci_irq,
81 .flags = HCD_MEMORY | HCD_USB2,
82
83 /*
84 * basic lifecycle operations
85 *
86 * FIXME -- ehci_init() doesn't do enough here.
87 * See ehci-ppc-soc for a complete implementation.
88 */
89 .reset = ehci_init,
90 .start = ehci_run,
91 .stop = ehci_stop,
92 .shutdown = ehci_shutdown,
82 93
83/* configure so an HC device and id are always provided */ 94 /*
84/* always called with process context; sleeping is OK */ 95 * managing i/o requests and associated device resources
96 */
97 .urb_enqueue = ehci_urb_enqueue,
98 .urb_dequeue = ehci_urb_dequeue,
99 .endpoint_disable = ehci_endpoint_disable,
85 100
86/** 101 /*
87 * usb_ehci_au1xxx_probe - initialize Au1xxx-based HCDs 102 * scheduling support
88 * Context: !in_interrupt() 103 */
89 * 104 .get_frame_number = ehci_get_frame,
90 * Allocates basic resources for this USB host controller, and 105
91 * then invokes the start() method for the HCD associated with it 106 /*
92 * through the hotplug entry's driver_data. 107 * root hub support
93 * 108 */
94 */ 109 .hub_status_data = ehci_hub_status_data,
95int usb_ehci_au1xxx_probe(const struct hc_driver *driver, 110 .hub_control = ehci_hub_control,
96 struct usb_hcd **hcd_out, struct platform_device *dev) 111 .bus_suspend = ehci_bus_suspend,
112 .bus_resume = ehci_bus_resume,
113 .relinquish_port = ehci_relinquish_port,
114 .port_handed_over = ehci_port_handed_over,
115};
116
117static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
97{ 118{
98 int retval;
99 struct usb_hcd *hcd; 119 struct usb_hcd *hcd;
100 struct ehci_hcd *ehci; 120 struct ehci_hcd *ehci;
121 int ret;
101 122
102#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT) 123 if (usb_disabled())
124 return -ENODEV;
103 125
126#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
104 /* Au1200 AB USB does not support coherent memory */ 127 /* Au1200 AB USB does not support coherent memory */
105 if (!(read_c0_prid() & 0xff)) { 128 if (!(read_c0_prid() & 0xff)) {
106 pr_info("%s: this is chip revision AB!\n", dev->name); 129 printk(KERN_INFO "%s: this is chip revision AB!\n", pdev->name);
107 pr_info("%s: update your board or re-configure the kernel\n", 130 printk(KERN_INFO "%s: update your board or re-configure"
108 dev->name); 131 " the kernel\n", pdev->name);
109 return -ENODEV; 132 return -ENODEV;
110 } 133 }
111#endif 134#endif
112 135
113 au1xxx_start_ehc(dev); 136 if (pdev->resource[1].flags != IORESOURCE_IRQ) {
114
115 if (dev->resource[1].flags != IORESOURCE_IRQ) {
116 pr_debug("resource[1] is not IORESOURCE_IRQ"); 137 pr_debug("resource[1] is not IORESOURCE_IRQ");
117 retval = -ENOMEM; 138 return -ENOMEM;
118 } 139 }
119 hcd = usb_create_hcd(driver, &dev->dev, "Au1xxx"); 140 hcd = usb_create_hcd(&ehci_au1xxx_hc_driver, &pdev->dev, "Au1xxx");
120 if (!hcd) 141 if (!hcd)
121 return -ENOMEM; 142 return -ENOMEM;
122 hcd->rsrc_start = dev->resource[0].start; 143
123 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1; 144 hcd->rsrc_start = pdev->resource[0].start;
145 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
124 146
125 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { 147 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
126 pr_debug("request_mem_region failed"); 148 pr_debug("request_mem_region failed");
127 retval = -EBUSY; 149 ret = -EBUSY;
128 goto err1; 150 goto err1;
129 } 151 }
130 152
131 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); 153 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
132 if (!hcd->regs) { 154 if (!hcd->regs) {
133 pr_debug("ioremap failed"); 155 pr_debug("ioremap failed");
134 retval = -ENOMEM; 156 ret = -ENOMEM;
135 goto err2; 157 goto err2;
136 } 158 }
137 159
160 au1xxx_start_ehc();
161
138 ehci = hcd_to_ehci(hcd); 162 ehci = hcd_to_ehci(hcd);
139 ehci->caps = hcd->regs; 163 ehci->caps = hcd->regs;
140 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase)); 164 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
141 /* cache this readonly data; minimize chip reads */ 165 /* cache this readonly data; minimize chip reads */
142 ehci->hcs_params = readl(&ehci->caps->hcs_params); 166 ehci->hcs_params = readl(&ehci->caps->hcs_params);
143 167
144 /* ehci_hcd_init(hcd_to_ehci(hcd)); */ 168 ret = usb_add_hcd(hcd, pdev->resource[1].start,
145 169 IRQF_DISABLED | IRQF_SHARED);
146 retval = 170 if (ret == 0) {
147 usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED | IRQF_SHARED); 171 platform_set_drvdata(pdev, hcd);
148 if (retval == 0) 172 return ret;
149 return retval; 173 }
150 174
151 au1xxx_stop_ehc(dev); 175 au1xxx_stop_ehc();
152 iounmap(hcd->regs); 176 iounmap(hcd->regs);
153err2: 177err2:
154 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 178 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
155err1: 179err1:
156 usb_put_hcd(hcd); 180 usb_put_hcd(hcd);
157 return retval; 181 return ret;
158} 182}
159 183
160/* may be called without controller electrically present */ 184static int ehci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
161/* may be called with controller, bus, and devices active */
162
163/**
164 * usb_ehci_hcd_au1xxx_remove - shutdown processing for Au1xxx-based HCDs
165 * @dev: USB Host Controller being removed
166 * Context: !in_interrupt()
167 *
168 * Reverses the effect of usb_ehci_hcd_au1xxx_probe(), first invoking
169 * the HCD's stop() method. It is always called from a thread
170 * context, normally "rmmod", "apmd", or something similar.
171 *
172 */
173void usb_ehci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
174{ 185{
186 struct usb_hcd *hcd = platform_get_drvdata(pdev);
187
175 usb_remove_hcd(hcd); 188 usb_remove_hcd(hcd);
176 iounmap(hcd->regs); 189 iounmap(hcd->regs);
177 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 190 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
178 usb_put_hcd(hcd); 191 usb_put_hcd(hcd);
179 au1xxx_stop_ehc(dev); 192 au1xxx_stop_ehc();
193 platform_set_drvdata(pdev, NULL);
194
195 return 0;
180} 196}
181 197
182/*-------------------------------------------------------------------------*/ 198#ifdef CONFIG_PM
199static int ehci_hcd_au1xxx_drv_suspend(struct platform_device *pdev,
200 pm_message_t message)
201{
202 struct usb_hcd *hcd = platform_get_drvdata(pdev);
203 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
204 unsigned long flags;
205 int rc;
183 206
184static const struct hc_driver ehci_au1xxx_hc_driver = { 207 return 0;
185 .description = hcd_name, 208 rc = 0;
186 .product_desc = "Au1xxx EHCI",
187 .hcd_priv_size = sizeof(struct ehci_hcd),
188 209
189 /* 210 if (time_before(jiffies, ehci->next_statechange))
190 * generic hardware linkage 211 msleep(10);
191 */
192 .irq = ehci_irq,
193 .flags = HCD_MEMORY | HCD_USB2,
194 212
195 /* 213 /* Root hub was already suspended. Disable irq emission and
196 * basic lifecycle operations 214 * mark HW unaccessible, bail out if RH has been resumed. Use
215 * the spinlock to properly synchronize with possible pending
216 * RH suspend or resume activity.
197 * 217 *
198 * FIXME -- ehci_init() doesn't do enough here. 218 * This is still racy as hcd->state is manipulated outside of
199 * See ehci-ppc-soc for a complete implementation. 219 * any locks =P But that will be a different fix.
200 */
201 .reset = ehci_init,
202 .start = ehci_run,
203 .stop = ehci_stop,
204 .shutdown = ehci_shutdown,
205
206 /*
207 * managing i/o requests and associated device resources
208 */ 220 */
209 .urb_enqueue = ehci_urb_enqueue, 221 spin_lock_irqsave(&ehci->lock, flags);
210 .urb_dequeue = ehci_urb_dequeue, 222 if (hcd->state != HC_STATE_SUSPENDED) {
211 .endpoint_disable = ehci_endpoint_disable, 223 rc = -EINVAL;
224 goto bail;
225 }
226 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
227 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
212 228
213 /* 229 /* make sure snapshot being resumed re-enumerates everything */
214 * scheduling support 230 if (message.event == PM_EVENT_PRETHAW) {
215 */ 231 ehci_halt(ehci);
216 .get_frame_number = ehci_get_frame, 232 ehci_reset(ehci);
233 }
217 234
218 /* 235 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
219 * root hub support
220 */
221 .hub_status_data = ehci_hub_status_data,
222 .hub_control = ehci_hub_control,
223 .bus_suspend = ehci_bus_suspend,
224 .bus_resume = ehci_bus_resume,
225 .relinquish_port = ehci_relinquish_port,
226 .port_handed_over = ehci_port_handed_over,
227};
228 236
229/*-------------------------------------------------------------------------*/ 237 au1xxx_stop_ehc();
230 238
231static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev) 239bail:
232{ 240 spin_unlock_irqrestore(&ehci->lock, flags);
233 struct usb_hcd *hcd = NULL;
234 int ret;
235 241
236 pr_debug("In ehci_hcd_au1xxx_drv_probe\n"); 242 // could save FLADJ in case of Vaux power loss
243 // ... we'd only use it to handle clock skew
237 244
238 if (usb_disabled()) 245 return rc;
239 return -ENODEV;
240
241 /* FIXME we only want one one probe() not two */
242 ret = usb_ehci_au1xxx_probe(&ehci_au1xxx_hc_driver, &hcd, pdev);
243 return ret;
244} 246}
245 247
246static int ehci_hcd_au1xxx_drv_remove(struct platform_device *pdev) 248
249static int ehci_hcd_au1xxx_drv_resume(struct platform_device *pdev)
247{ 250{
248 struct usb_hcd *hcd = platform_get_drvdata(pdev); 251 struct usb_hcd *hcd = platform_get_drvdata(pdev);
252 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
249 253
250 /* FIXME we only want one one remove() not two */ 254 au1xxx_start_ehc();
251 usb_ehci_au1xxx_remove(hcd, pdev);
252 return 0;
253}
254 255
255 /*TBD*/ 256 // maybe restore FLADJ
256/*static int ehci_hcd_au1xxx_drv_suspend(struct device *dev)
257{
258 struct platform_device *pdev = to_platform_device(dev);
259 struct usb_hcd *hcd = dev_get_drvdata(dev);
260 257
261 return 0; 258 if (time_before(jiffies, ehci->next_statechange))
262} 259 msleep(100);
263static int ehci_hcd_au1xxx_drv_resume(struct device *dev) 260
264{ 261 /* Mark hardware accessible again as we are out of D3 state by now */
265 struct platform_device *pdev = to_platform_device(dev); 262 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
266 struct usb_hcd *hcd = dev_get_drvdata(dev); 263
264 /* If CF is still set, we maintained PCI Vaux power.
265 * Just undo the effect of ehci_pci_suspend().
266 */
267 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
268 int mask = INTR_MASK;
269
270 if (!hcd->self.root_hub->do_remote_wakeup)
271 mask &= ~STS_PCD;
272 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
273 ehci_readl(ehci, &ehci->regs->intr_enable);
274 return 0;
275 }
276
277 ehci_dbg(ehci, "lost power, restarting\n");
278 usb_root_hub_lost_power(hcd->self.root_hub);
279
280 /* Else reset, to cope with power loss or flush-to-storage
281 * style "resume" having let BIOS kick in during reboot.
282 */
283 (void) ehci_halt(ehci);
284 (void) ehci_reset(ehci);
285
286 /* emptying the schedule aborts any urbs */
287 spin_lock_irq(&ehci->lock);
288 if (ehci->reclaim)
289 end_unlink_async(ehci);
290 ehci_work(ehci);
291 spin_unlock_irq(&ehci->lock);
292
293 ehci_writel(ehci, ehci->command, &ehci->regs->command);
294 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
295 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
296
297 /* here we "know" root ports should always stay powered */
298 ehci_port_power(ehci, 1);
299
300 hcd->state = HC_STATE_SUSPENDED;
267 301
268 return 0; 302 return 0;
269} 303}
270*/ 304
271MODULE_ALIAS("platform:au1xxx-ehci"); 305#else
306#define ehci_hcd_au1xxx_drv_suspend NULL
307#define ehci_hcd_au1xxx_drv_resume NULL
308#endif
309
272static struct platform_driver ehci_hcd_au1xxx_driver = { 310static struct platform_driver ehci_hcd_au1xxx_driver = {
273 .probe = ehci_hcd_au1xxx_drv_probe, 311 .probe = ehci_hcd_au1xxx_drv_probe,
274 .remove = ehci_hcd_au1xxx_drv_remove, 312 .remove = ehci_hcd_au1xxx_drv_remove,
275 .shutdown = usb_hcd_platform_shutdown, 313 .shutdown = usb_hcd_platform_shutdown,
276 /*.suspend = ehci_hcd_au1xxx_drv_suspend, */ 314 .suspend = ehci_hcd_au1xxx_drv_suspend,
277 /*.resume = ehci_hcd_au1xxx_drv_resume, */ 315 .resume = ehci_hcd_au1xxx_drv_resume,
278 .driver = { 316 .driver = {
279 .name = "au1xxx-ehci", 317 .name = "au1xxx-ehci",
318 .owner = THIS_MODULE,
280 } 319 }
281}; 320};
321
322MODULE_ALIAS("platform:au1xxx-ehci");
diff --git a/drivers/usb/host/ehci-dbg.c b/drivers/usb/host/ehci-dbg.c
index 0f82fdcaef09..b0f8ed5a7fb9 100644
--- a/drivers/usb/host/ehci-dbg.c
+++ b/drivers/usb/host/ehci-dbg.c
@@ -676,7 +676,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
676 "%s\n" 676 "%s\n"
677 "SUSPENDED (no register access)\n", 677 "SUSPENDED (no register access)\n",
678 hcd->self.controller->bus->name, 678 hcd->self.controller->bus->name,
679 hcd->self.controller->bus_id, 679 dev_name(hcd->self.controller),
680 hcd->product_desc); 680 hcd->product_desc);
681 goto done; 681 goto done;
682 } 682 }
@@ -688,7 +688,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
688 "%s\n" 688 "%s\n"
689 "EHCI %x.%02x, hcd state %d\n", 689 "EHCI %x.%02x, hcd state %d\n",
690 hcd->self.controller->bus->name, 690 hcd->self.controller->bus->name,
691 hcd->self.controller->bus_id, 691 dev_name(hcd->self.controller),
692 hcd->product_desc, 692 hcd->product_desc,
693 i >> 8, i & 0x0ff, hcd->state); 693 i >> 8, i & 0x0ff, hcd->state);
694 size -= temp; 694 size -= temp;
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index 7370d6187c64..01c3da34f678 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -56,7 +56,7 @@ int usb_hcd_fsl_probe(const struct hc_driver *driver,
56 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data; 56 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
57 if (!pdata) { 57 if (!pdata) {
58 dev_err(&pdev->dev, 58 dev_err(&pdev->dev,
59 "No platform data for %s.\n", pdev->dev.bus_id); 59 "No platform data for %s.\n", dev_name(&pdev->dev));
60 return -ENODEV; 60 return -ENODEV;
61 } 61 }
62 62
@@ -69,7 +69,7 @@ int usb_hcd_fsl_probe(const struct hc_driver *driver,
69 (pdata->operating_mode == FSL_USB2_DR_OTG))) { 69 (pdata->operating_mode == FSL_USB2_DR_OTG))) {
70 dev_err(&pdev->dev, 70 dev_err(&pdev->dev,
71 "Non Host Mode configured for %s. Wrong driver linked.\n", 71 "Non Host Mode configured for %s. Wrong driver linked.\n",
72 pdev->dev.bus_id); 72 dev_name(&pdev->dev));
73 return -ENODEV; 73 return -ENODEV;
74 } 74 }
75 75
@@ -77,12 +77,12 @@ int usb_hcd_fsl_probe(const struct hc_driver *driver,
77 if (!res) { 77 if (!res) {
78 dev_err(&pdev->dev, 78 dev_err(&pdev->dev,
79 "Found HC with no IRQ. Check %s setup!\n", 79 "Found HC with no IRQ. Check %s setup!\n",
80 pdev->dev.bus_id); 80 dev_name(&pdev->dev));
81 return -ENODEV; 81 return -ENODEV;
82 } 82 }
83 irq = res->start; 83 irq = res->start;
84 84
85 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id); 85 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
86 if (!hcd) { 86 if (!hcd) {
87 retval = -ENOMEM; 87 retval = -ENOMEM;
88 goto err1; 88 goto err1;
@@ -92,7 +92,7 @@ int usb_hcd_fsl_probe(const struct hc_driver *driver,
92 if (!res) { 92 if (!res) {
93 dev_err(&pdev->dev, 93 dev_err(&pdev->dev,
94 "Found HC with no register addr. Check %s setup!\n", 94 "Found HC with no register addr. Check %s setup!\n",
95 pdev->dev.bus_id); 95 dev_name(&pdev->dev));
96 retval = -ENODEV; 96 retval = -ENODEV;
97 goto err2; 97 goto err2;
98 } 98 }
@@ -132,7 +132,7 @@ int usb_hcd_fsl_probe(const struct hc_driver *driver,
132 err2: 132 err2:
133 usb_put_hcd(hcd); 133 usb_put_hcd(hcd);
134 err1: 134 err1:
135 dev_err(&pdev->dev, "init %s fail, %d\n", pdev->dev.bus_id, retval); 135 dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
136 return retval; 136 return retval;
137} 137}
138 138
@@ -230,8 +230,13 @@ static void mpc83xx_usb_setup(struct usb_hcd *hcd)
230 230
231 /* put controller in host mode. */ 231 /* put controller in host mode. */
232 ehci_writel(ehci, 0x00000003, non_ehci + FSL_SOC_USB_USBMODE); 232 ehci_writel(ehci, 0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
233#ifdef CONFIG_PPC_85xx
234 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x00000008);
235 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000080);
236#else
233 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c); 237 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
234 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040); 238 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
239#endif
235 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001); 240 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
236} 241}
237 242
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 369a8a5ea7bb..d9d53f289caf 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -84,7 +84,7 @@ static const char hcd_name [] = "ehci_hcd";
84#define EHCI_IAA_MSECS 10 /* arbitrary */ 84#define EHCI_IAA_MSECS 10 /* arbitrary */
85#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */ 85#define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
86#define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */ 86#define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
87#define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */ 87#define EHCI_SHRINK_FRAMES 5 /* async qh unlink delay */
88 88
89/* Initial IRQ latency: faster than hw default */ 89/* Initial IRQ latency: faster than hw default */
90static int log2_irq_thresh = 0; // 0 to 6 90static int log2_irq_thresh = 0; // 0 to 6
diff --git a/drivers/usb/host/ehci-ixp4xx.c b/drivers/usb/host/ehci-ixp4xx.c
index 9d042f220097..f9575c409124 100644
--- a/drivers/usb/host/ehci-ixp4xx.c
+++ b/drivers/usb/host/ehci-ixp4xx.c
@@ -77,12 +77,12 @@ static int ixp4xx_ehci_probe(struct platform_device *pdev)
77 if (!res) { 77 if (!res) {
78 dev_err(&pdev->dev, 78 dev_err(&pdev->dev,
79 "Found HC with no IRQ. Check %s setup!\n", 79 "Found HC with no IRQ. Check %s setup!\n",
80 pdev->dev.bus_id); 80 dev_name(&pdev->dev));
81 return -ENODEV; 81 return -ENODEV;
82 } 82 }
83 irq = res->start; 83 irq = res->start;
84 84
85 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id); 85 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
86 if (!hcd) { 86 if (!hcd) {
87 retval = -ENOMEM; 87 retval = -ENOMEM;
88 goto fail_create_hcd; 88 goto fail_create_hcd;
@@ -92,7 +92,7 @@ static int ixp4xx_ehci_probe(struct platform_device *pdev)
92 if (!res) { 92 if (!res) {
93 dev_err(&pdev->dev, 93 dev_err(&pdev->dev,
94 "Found HC with no register addr. Check %s setup!\n", 94 "Found HC with no register addr. Check %s setup!\n",
95 pdev->dev.bus_id); 95 dev_name(&pdev->dev));
96 retval = -ENODEV; 96 retval = -ENODEV;
97 goto fail_request_resource; 97 goto fail_request_resource;
98 } 98 }
@@ -126,7 +126,7 @@ fail_ioremap:
126fail_request_resource: 126fail_request_resource:
127 usb_put_hcd(hcd); 127 usb_put_hcd(hcd);
128fail_create_hcd: 128fail_create_hcd:
129 dev_err(&pdev->dev, "init %s fail, %d\n", pdev->dev.bus_id, retval); 129 dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
130 return retval; 130 return retval;
131} 131}
132 132
diff --git a/drivers/usb/host/ehci-orion.c b/drivers/usb/host/ehci-orion.c
index ab625f0ba1d9..5fbdc14e63b3 100644
--- a/drivers/usb/host/ehci-orion.c
+++ b/drivers/usb/host/ehci-orion.c
@@ -204,7 +204,7 @@ static int __init ehci_orion_drv_probe(struct platform_device *pdev)
204 if (irq <= 0) { 204 if (irq <= 0) {
205 dev_err(&pdev->dev, 205 dev_err(&pdev->dev,
206 "Found HC with no IRQ. Check %s setup!\n", 206 "Found HC with no IRQ. Check %s setup!\n",
207 pdev->dev.bus_id); 207 dev_name(&pdev->dev));
208 err = -ENODEV; 208 err = -ENODEV;
209 goto err1; 209 goto err1;
210 } 210 }
@@ -213,7 +213,7 @@ static int __init ehci_orion_drv_probe(struct platform_device *pdev)
213 if (!res) { 213 if (!res) {
214 dev_err(&pdev->dev, 214 dev_err(&pdev->dev,
215 "Found HC with no register addr. Check %s setup!\n", 215 "Found HC with no register addr. Check %s setup!\n",
216 pdev->dev.bus_id); 216 dev_name(&pdev->dev));
217 err = -ENODEV; 217 err = -ENODEV;
218 goto err1; 218 goto err1;
219 } 219 }
@@ -233,7 +233,7 @@ static int __init ehci_orion_drv_probe(struct platform_device *pdev)
233 } 233 }
234 234
235 hcd = usb_create_hcd(&ehci_orion_hc_driver, 235 hcd = usb_create_hcd(&ehci_orion_hc_driver,
236 &pdev->dev, pdev->dev.bus_id); 236 &pdev->dev, dev_name(&pdev->dev));
237 if (!hcd) { 237 if (!hcd) {
238 err = -ENOMEM; 238 err = -ENOMEM;
239 goto err3; 239 goto err3;
@@ -276,7 +276,7 @@ err2:
276 release_mem_region(res->start, res->end - res->start + 1); 276 release_mem_region(res->start, res->end - res->start + 1);
277err1: 277err1:
278 dev_err(&pdev->dev, "init %s fail, %d\n", 278 dev_err(&pdev->dev, "init %s fail, %d\n",
279 pdev->dev.bus_id, err); 279 dev_name(&pdev->dev), err);
280 280
281 return err; 281 return err;
282} 282}
diff --git a/drivers/usb/host/ehci-ps3.c b/drivers/usb/host/ehci-ps3.c
index 37e6abeb794c..0eba894bcb01 100644
--- a/drivers/usb/host/ehci-ps3.c
+++ b/drivers/usb/host/ehci-ps3.c
@@ -128,7 +128,7 @@ static int ps3_ehci_probe(struct ps3_system_bus_device *dev)
128 128
129 dev->core.dma_mask = &dummy_mask; /* FIXME: for improper usb code */ 129 dev->core.dma_mask = &dummy_mask; /* FIXME: for improper usb code */
130 130
131 hcd = usb_create_hcd(&ps3_ehci_hc_driver, &dev->core, dev->core.bus_id); 131 hcd = usb_create_hcd(&ps3_ehci_hc_driver, &dev->core, dev_name(&dev->core));
132 132
133 if (!hcd) { 133 if (!hcd) {
134 dev_dbg(&dev->core, "%s:%d: usb_create_hcd failed\n", __func__, 134 dev_dbg(&dev->core, "%s:%d: usb_create_hcd failed\n", __func__,
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index b85b54160cda..2622b6596d7c 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -1116,8 +1116,7 @@ static void scan_async (struct ehci_hcd *ehci)
1116 struct ehci_qh *qh; 1116 struct ehci_qh *qh;
1117 enum ehci_timer_action action = TIMER_IO_WATCHDOG; 1117 enum ehci_timer_action action = TIMER_IO_WATCHDOG;
1118 1118
1119 if (!++(ehci->stamp)) 1119 ehci->stamp = ehci_readl(ehci, &ehci->regs->frame_index);
1120 ehci->stamp++;
1121 timer_action_done (ehci, TIMER_ASYNC_SHRINK); 1120 timer_action_done (ehci, TIMER_ASYNC_SHRINK);
1122rescan: 1121rescan:
1123 qh = ehci->async->qh_next.qh; 1122 qh = ehci->async->qh_next.qh;
@@ -1142,18 +1141,20 @@ rescan:
1142 } 1141 }
1143 } 1142 }
1144 1143
1145 /* unlink idle entries, reducing HC PCI usage as well 1144 /* unlink idle entries, reducing DMA usage as well
1146 * as HCD schedule-scanning costs. delay for any qh 1145 * as HCD schedule-scanning costs. delay for any qh
1147 * we just scanned, there's a not-unusual case that it 1146 * we just scanned, there's a not-unusual case that it
1148 * doesn't stay idle for long. 1147 * doesn't stay idle for long.
1149 * (plus, avoids some kind of re-activation race.) 1148 * (plus, avoids some kind of re-activation race.)
1150 */ 1149 */
1151 if (list_empty (&qh->qtd_list)) { 1150 if (list_empty(&qh->qtd_list)
1152 if (qh->stamp == ehci->stamp) 1151 && qh->qh_state == QH_STATE_LINKED) {
1152 if (!ehci->reclaim
1153 && ((ehci->stamp - qh->stamp) & 0x1fff)
1154 >= (EHCI_SHRINK_FRAMES * 8))
1155 start_unlink_async(ehci, qh);
1156 else
1153 action = TIMER_ASYNC_SHRINK; 1157 action = TIMER_ASYNC_SHRINK;
1154 else if (!ehci->reclaim
1155 && qh->qh_state == QH_STATE_LINKED)
1156 start_unlink_async (ehci, qh);
1157 } 1158 }
1158 1159
1159 qh = qh->qh_next.qh; 1160 qh = qh->qh_next.qh;
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 90245fd8bac4..5799298364fb 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -198,7 +198,10 @@ timer_action (struct ehci_hcd *ehci, enum ehci_timer_action action)
198 break; 198 break;
199 // case TIMER_ASYNC_SHRINK: 199 // case TIMER_ASYNC_SHRINK:
200 default: 200 default:
201 t = EHCI_SHRINK_JIFFIES; 201 /* add a jiffie since we synch against the
202 * 8 KHz uframe counter.
203 */
204 t = DIV_ROUND_UP(EHCI_SHRINK_FRAMES * HZ, 1000) + 1;
202 break; 205 break;
203 } 206 }
204 mod_timer(&ehci->watchdog, t + jiffies); 207 mod_timer(&ehci->watchdog, t + jiffies);
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index 20b9a0d07420..31178e10cbbe 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -94,6 +94,10 @@ static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
94 u16 w; 94 u16 w;
95 int quot = len % 4; 95 int quot = len % 4;
96 96
97 /* buffer is already in 'usb data order', which is LE. */
98 /* When reading buffer as u16, we have to take care byte order */
99 /* doesn't get mixed up */
100
97 if ((unsigned long)dp2 & 1) { 101 if ((unsigned long)dp2 & 1) {
98 /* not aligned */ 102 /* not aligned */
99 for (; len > 1; len -= 2) { 103 for (; len > 1; len -= 2) {
@@ -105,8 +109,11 @@ static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
105 isp116x_write_data16(isp116x, (u16) * dp); 109 isp116x_write_data16(isp116x, (u16) * dp);
106 } else { 110 } else {
107 /* aligned */ 111 /* aligned */
108 for (; len > 1; len -= 2) 112 for (; len > 1; len -= 2) {
109 isp116x_raw_write_data16(isp116x, *dp2++); 113 /* Keep byte order ! */
114 isp116x_raw_write_data16(isp116x, cpu_to_le16(*dp2++));
115 }
116
110 if (len) 117 if (len)
111 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2)); 118 isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
112 } 119 }
@@ -124,6 +131,10 @@ static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
124 u16 w; 131 u16 w;
125 int quot = len % 4; 132 int quot = len % 4;
126 133
134 /* buffer is already in 'usb data order', which is LE. */
135 /* When reading buffer as u16, we have to take care byte order */
136 /* doesn't get mixed up */
137
127 if ((unsigned long)dp2 & 1) { 138 if ((unsigned long)dp2 & 1) {
128 /* not aligned */ 139 /* not aligned */
129 for (; len > 1; len -= 2) { 140 for (; len > 1; len -= 2) {
@@ -131,12 +142,16 @@ static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
131 *dp++ = w & 0xff; 142 *dp++ = w & 0xff;
132 *dp++ = (w >> 8) & 0xff; 143 *dp++ = (w >> 8) & 0xff;
133 } 144 }
145
134 if (len) 146 if (len)
135 *dp = 0xff & isp116x_read_data16(isp116x); 147 *dp = 0xff & isp116x_read_data16(isp116x);
136 } else { 148 } else {
137 /* aligned */ 149 /* aligned */
138 for (; len > 1; len -= 2) 150 for (; len > 1; len -= 2) {
139 *dp2++ = isp116x_raw_read_data16(isp116x); 151 /* Keep byte order! */
152 *dp2++ = le16_to_cpu(isp116x_raw_read_data16(isp116x));
153 }
154
140 if (len) 155 if (len)
141 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x); 156 *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
142 } 157 }
@@ -1592,7 +1607,7 @@ static int __devinit isp116x_probe(struct platform_device *pdev)
1592 } 1607 }
1593 1608
1594 /* allocate and initialize hcd */ 1609 /* allocate and initialize hcd */
1595 hcd = usb_create_hcd(&isp116x_hc_driver, &pdev->dev, pdev->dev.bus_id); 1610 hcd = usb_create_hcd(&isp116x_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1596 if (!hcd) { 1611 if (!hcd) {
1597 ret = -ENOMEM; 1612 ret = -ENOMEM;
1598 goto err5; 1613 goto err5;
diff --git a/drivers/usb/host/isp1760-hcd.c b/drivers/usb/host/isp1760-hcd.c
index 65aa5ecf569a..c858f2adb929 100644
--- a/drivers/usb/host/isp1760-hcd.c
+++ b/drivers/usb/host/isp1760-hcd.c
@@ -38,6 +38,7 @@ struct isp1760_hcd {
38 unsigned i_thresh; 38 unsigned i_thresh;
39 unsigned long reset_done; 39 unsigned long reset_done;
40 unsigned long next_statechange; 40 unsigned long next_statechange;
41 unsigned int devflags;
41}; 42};
42 43
43static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd) 44static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
@@ -378,9 +379,31 @@ static int isp1760_hc_setup(struct usb_hcd *hcd)
378{ 379{
379 struct isp1760_hcd *priv = hcd_to_priv(hcd); 380 struct isp1760_hcd *priv = hcd_to_priv(hcd);
380 int result; 381 int result;
381 u32 scratch; 382 u32 scratch, hwmode;
383
384 /* Setup HW Mode Control: This assumes a level active-low interrupt */
385 hwmode = HW_DATA_BUS_32BIT;
386
387 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
388 hwmode &= ~HW_DATA_BUS_32BIT;
389 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
390 hwmode |= HW_ANA_DIGI_OC;
391 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
392 hwmode |= HW_DACK_POL_HIGH;
393 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
394 hwmode |= HW_DREQ_POL_HIGH;
395
396 /*
397 * We have to set this first in case we're in 16-bit mode.
398 * Write it twice to ensure correct upper bits if switching
399 * to 16-bit mode.
400 */
401 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
402 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
382 403
383 isp1760_writel(0xdeadbabe, hcd->regs + HC_SCRATCH_REG); 404 isp1760_writel(0xdeadbabe, hcd->regs + HC_SCRATCH_REG);
405 /* Change bus pattern */
406 scratch = isp1760_readl(hcd->regs + HC_CHIP_ID_REG);
384 scratch = isp1760_readl(hcd->regs + HC_SCRATCH_REG); 407 scratch = isp1760_readl(hcd->regs + HC_SCRATCH_REG);
385 if (scratch != 0xdeadbabe) { 408 if (scratch != 0xdeadbabe) {
386 printk(KERN_ERR "ISP1760: Scratch test failed.\n"); 409 printk(KERN_ERR "ISP1760: Scratch test failed.\n");
@@ -403,17 +426,29 @@ static int isp1760_hc_setup(struct usb_hcd *hcd)
403 426
404 /* Step 11 passed */ 427 /* Step 11 passed */
405 428
406 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_REG); 429 isp1760_info(priv, "bus width: %d, oc: %s\n",
407 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_ENABLE); 430 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
431 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
432 "analog" : "digital");
408 433
409 /* ATL reset */ 434 /* ATL reset */
410 scratch = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL); 435 isp1760_writel(hwmode | ALL_ATX_RESET, hcd->regs + HC_HW_MODE_CTRL);
411 isp1760_writel(scratch | ALL_ATX_RESET, hcd->regs + HC_HW_MODE_CTRL);
412 mdelay(10); 436 mdelay(10);
413 isp1760_writel(scratch, hcd->regs + HC_HW_MODE_CTRL); 437 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
414 438
415 isp1760_writel(PORT1_POWER | PORT1_INIT2, hcd->regs + HC_PORT1_CTRL); 439 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_REG);
416 mdelay(10); 440 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_ENABLE);
441
442 /*
443 * PORT 1 Control register of the ISP1760 is the OTG control
444 * register on ISP1761.
445 */
446 if (!(priv->devflags & ISP1760_FLAG_ISP1761) &&
447 !(priv->devflags & ISP1760_FLAG_PORT1_DIS)) {
448 isp1760_writel(PORT1_POWER | PORT1_INIT2,
449 hcd->regs + HC_PORT1_CTRL);
450 mdelay(10);
451 }
417 452
418 priv->hcs_params = isp1760_readl(hcd->regs + HC_HCSPARAMS); 453 priv->hcs_params = isp1760_readl(hcd->regs + HC_HCSPARAMS);
419 454
@@ -453,8 +488,7 @@ static int isp1760_run(struct usb_hcd *hcd)
453 hcd->state = HC_STATE_RUNNING; 488 hcd->state = HC_STATE_RUNNING;
454 isp1760_enable_interrupts(hcd); 489 isp1760_enable_interrupts(hcd);
455 temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL); 490 temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
456 temp |= FINAL_HW_CONFIG; 491 isp1760_writel(temp | HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
457 isp1760_writel(temp, hcd->regs + HC_HW_MODE_CTRL);
458 492
459 command = isp1760_readl(hcd->regs + HC_USBCMD); 493 command = isp1760_readl(hcd->regs + HC_USBCMD);
460 command &= ~(CMD_LRESET|CMD_RESET); 494 command &= ~(CMD_LRESET|CMD_RESET);
@@ -782,8 +816,8 @@ static void enqueue_one_int_qtd(u32 int_regs, u32 payload,
782 qtd->status |= slot << 16; 816 qtd->status |= slot << 16;
783} 817}
784 818
785void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh, 819static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
786 struct isp1760_qtd *qtd) 820 struct isp1760_qtd *qtd)
787{ 821{
788 struct isp1760_hcd *priv = hcd_to_priv(hcd); 822 struct isp1760_hcd *priv = hcd_to_priv(hcd);
789 u32 skip_map, or_map; 823 u32 skip_map, or_map;
@@ -816,8 +850,8 @@ void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
816 isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG); 850 isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG);
817} 851}
818 852
819void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh, 853static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
820 struct isp1760_qtd *qtd) 854 struct isp1760_qtd *qtd)
821{ 855{
822 struct isp1760_hcd *priv = hcd_to_priv(hcd); 856 struct isp1760_hcd *priv = hcd_to_priv(hcd);
823 u32 skip_map, or_map; 857 u32 skip_map, or_map;
@@ -1592,7 +1626,7 @@ static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1592 struct inter_packet_info *ints; 1626 struct inter_packet_info *ints;
1593 u32 i; 1627 u32 i;
1594 u32 reg_base, or_reg, skip_reg; 1628 u32 reg_base, or_reg, skip_reg;
1595 int flags; 1629 unsigned long flags;
1596 struct ptd ptd; 1630 struct ptd ptd;
1597 1631
1598 switch (usb_pipetype(urb->pipe)) { 1632 switch (usb_pipetype(urb->pipe)) {
@@ -2061,7 +2095,7 @@ static void isp1760_endpoint_disable(struct usb_hcd *usb_hcd,
2061 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd); 2095 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
2062 struct isp1760_qh *qh; 2096 struct isp1760_qh *qh;
2063 struct isp1760_qtd *qtd; 2097 struct isp1760_qtd *qtd;
2064 u32 flags; 2098 unsigned long flags;
2065 2099
2066 spin_lock_irqsave(&priv->lock, flags); 2100 spin_lock_irqsave(&priv->lock, flags);
2067 qh = ep->hcpriv; 2101 qh = ep->hcpriv;
@@ -2112,6 +2146,7 @@ static int isp1760_get_frame(struct usb_hcd *hcd)
2112static void isp1760_stop(struct usb_hcd *hcd) 2146static void isp1760_stop(struct usb_hcd *hcd)
2113{ 2147{
2114 struct isp1760_hcd *priv = hcd_to_priv(hcd); 2148 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2149 u32 temp;
2115 2150
2116 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1, 2151 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2117 NULL, 0); 2152 NULL, 0);
@@ -2120,7 +2155,8 @@ static void isp1760_stop(struct usb_hcd *hcd)
2120 spin_lock_irq(&priv->lock); 2155 spin_lock_irq(&priv->lock);
2121 ehci_reset(priv); 2156 ehci_reset(priv);
2122 /* Disable IRQ */ 2157 /* Disable IRQ */
2123 isp1760_writel(HW_DATA_BUS_32BIT, hcd->regs + HC_HW_MODE_CTRL); 2158 temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
2159 isp1760_writel(temp &= ~HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
2124 spin_unlock_irq(&priv->lock); 2160 spin_unlock_irq(&priv->lock);
2125 2161
2126 isp1760_writel(0, hcd->regs + HC_CONFIGFLAG); 2162 isp1760_writel(0, hcd->regs + HC_CONFIGFLAG);
@@ -2128,10 +2164,11 @@ static void isp1760_stop(struct usb_hcd *hcd)
2128 2164
2129static void isp1760_shutdown(struct usb_hcd *hcd) 2165static void isp1760_shutdown(struct usb_hcd *hcd)
2130{ 2166{
2131 u32 command; 2167 u32 command, temp;
2132 2168
2133 isp1760_stop(hcd); 2169 isp1760_stop(hcd);
2134 isp1760_writel(HW_DATA_BUS_32BIT, hcd->regs + HC_HW_MODE_CTRL); 2170 temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
2171 isp1760_writel(temp &= ~HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
2135 2172
2136 command = isp1760_readl(hcd->regs + HC_USBCMD); 2173 command = isp1760_readl(hcd->regs + HC_USBCMD);
2137 command &= ~CMD_RUN; 2174 command &= ~CMD_RUN;
@@ -2183,7 +2220,8 @@ void deinit_kmem_cache(void)
2183} 2220}
2184 2221
2185struct usb_hcd *isp1760_register(u64 res_start, u64 res_len, int irq, 2222struct usb_hcd *isp1760_register(u64 res_start, u64 res_len, int irq,
2186 u64 irqflags, struct device *dev, const char *busname) 2223 u64 irqflags, struct device *dev, const char *busname,
2224 unsigned int devflags)
2187{ 2225{
2188 struct usb_hcd *hcd; 2226 struct usb_hcd *hcd;
2189 struct isp1760_hcd *priv; 2227 struct isp1760_hcd *priv;
@@ -2195,11 +2233,12 @@ struct usb_hcd *isp1760_register(u64 res_start, u64 res_len, int irq,
2195 /* prevent usb-core allocating DMA pages */ 2233 /* prevent usb-core allocating DMA pages */
2196 dev->dma_mask = NULL; 2234 dev->dma_mask = NULL;
2197 2235
2198 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev->bus_id); 2236 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2199 if (!hcd) 2237 if (!hcd)
2200 return ERR_PTR(-ENOMEM); 2238 return ERR_PTR(-ENOMEM);
2201 2239
2202 priv = hcd_to_priv(hcd); 2240 priv = hcd_to_priv(hcd);
2241 priv->devflags = devflags;
2203 init_memory(priv); 2242 init_memory(priv);
2204 hcd->regs = ioremap(res_start, res_len); 2243 hcd->regs = ioremap(res_start, res_len);
2205 if (!hcd->regs) { 2244 if (!hcd->regs) {
diff --git a/drivers/usb/host/isp1760-hcd.h b/drivers/usb/host/isp1760-hcd.h
index 3d86d0f6b147..6473dd86993c 100644
--- a/drivers/usb/host/isp1760-hcd.h
+++ b/drivers/usb/host/isp1760-hcd.h
@@ -3,7 +3,8 @@
3 3
4/* exports for if */ 4/* exports for if */
5struct usb_hcd *isp1760_register(u64 res_start, u64 res_len, int irq, 5struct usb_hcd *isp1760_register(u64 res_start, u64 res_len, int irq,
6 u64 irqflags, struct device *dev, const char *busname); 6 u64 irqflags, struct device *dev, const char *busname,
7 unsigned int devflags);
7int init_kmem_once(void); 8int init_kmem_once(void);
8void deinit_kmem_cache(void); 9void deinit_kmem_cache(void);
9 10
@@ -31,6 +32,7 @@ void deinit_kmem_cache(void);
31/* Configuration Register */ 32/* Configuration Register */
32#define HC_HW_MODE_CTRL 0x300 33#define HC_HW_MODE_CTRL 0x300
33#define ALL_ATX_RESET (1 << 31) 34#define ALL_ATX_RESET (1 << 31)
35#define HW_ANA_DIGI_OC (1 << 15)
34#define HW_DATA_BUS_32BIT (1 << 8) 36#define HW_DATA_BUS_32BIT (1 << 8)
35#define HW_DACK_POL_HIGH (1 << 6) 37#define HW_DACK_POL_HIGH (1 << 6)
36#define HW_DREQ_POL_HIGH (1 << 5) 38#define HW_DREQ_POL_HIGH (1 << 5)
@@ -56,13 +58,14 @@ void deinit_kmem_cache(void);
56#define PORT1_POWER (3 << 3) 58#define PORT1_POWER (3 << 3)
57#define PORT1_INIT1 (1 << 7) 59#define PORT1_INIT1 (1 << 7)
58#define PORT1_INIT2 (1 << 23) 60#define PORT1_INIT2 (1 << 23)
61#define HW_OTG_CTRL_SET 0x374
62#define HW_OTG_CTRL_CLR 0x376
59 63
60/* Interrupt Register */ 64/* Interrupt Register */
61#define HC_INTERRUPT_REG 0x310 65#define HC_INTERRUPT_REG 0x310
62 66
63#define HC_INTERRUPT_ENABLE 0x314 67#define HC_INTERRUPT_ENABLE 0x314
64#define INTERRUPT_ENABLE_MASK (HC_INTL_INT | HC_ATL_INT | HC_EOT_INT) 68#define INTERRUPT_ENABLE_MASK (HC_INTL_INT | HC_ATL_INT | HC_EOT_INT)
65#define FINAL_HW_CONFIG (HW_GLOBAL_INTR_EN | HW_DATA_BUS_32BIT)
66 69
67#define HC_ISO_INT (1 << 9) 70#define HC_ISO_INT (1 << 9)
68#define HC_ATL_INT (1 << 8) 71#define HC_ATL_INT (1 << 8)
@@ -122,6 +125,19 @@ typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
122#define isp1760_err(priv, fmt, args...) \ 125#define isp1760_err(priv, fmt, args...) \
123 dev_err(priv_to_hcd(priv)->self.controller, fmt, ##args) 126 dev_err(priv_to_hcd(priv)->self.controller, fmt, ##args)
124 127
128/*
129 * Device flags that can vary from board to board. All of these
130 * indicate the most "atypical" case, so that a devflags of 0 is
131 * a sane default configuration.
132 */
133#define ISP1760_FLAG_PORT1_DIS 0x00000001 /* Port 1 disabled */
134#define ISP1760_FLAG_BUS_WIDTH_16 0x00000002 /* 16-bit data bus width */
135#define ISP1760_FLAG_OTG_EN 0x00000004 /* Port 1 supports OTG */
136#define ISP1760_FLAG_ANALOG_OC 0x00000008 /* Analog overcurrent */
137#define ISP1760_FLAG_DACK_POL_HIGH 0x00000010 /* DACK active high */
138#define ISP1760_FLAG_DREQ_POL_HIGH 0x00000020 /* DREQ active high */
139#define ISP1760_FLAG_ISP1761 0x00000040 /* Chip is ISP1761 */
140
125/* chip memory management */ 141/* chip memory management */
126struct memory_chunk { 142struct memory_chunk {
127 unsigned int start; 143 unsigned int start;
diff --git a/drivers/usb/host/isp1760-if.c b/drivers/usb/host/isp1760-if.c
index c9db3fe98726..051ef7b6bdc6 100644
--- a/drivers/usb/host/isp1760-if.c
+++ b/drivers/usb/host/isp1760-if.c
@@ -35,13 +35,15 @@ static int of_isp1760_probe(struct of_device *dev,
35 int virq; 35 int virq;
36 u64 res_len; 36 u64 res_len;
37 int ret; 37 int ret;
38 const unsigned int *prop;
39 unsigned int devflags = 0;
38 40
39 ret = of_address_to_resource(dp, 0, &memory); 41 ret = of_address_to_resource(dp, 0, &memory);
40 if (ret) 42 if (ret)
41 return -ENXIO; 43 return -ENXIO;
42 44
43 res = request_mem_region(memory.start, memory.end - memory.start + 1, 45 res = request_mem_region(memory.start, memory.end - memory.start + 1,
44 dev->dev.bus_id); 46 dev_name(&dev->dev));
45 if (!res) 47 if (!res)
46 return -EBUSY; 48 return -EBUSY;
47 49
@@ -55,8 +57,32 @@ static int of_isp1760_probe(struct of_device *dev,
55 virq = irq_create_of_mapping(oirq.controller, oirq.specifier, 57 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
56 oirq.size); 58 oirq.size);
57 59
60 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
61 devflags |= ISP1760_FLAG_ISP1761;
62
63 if (of_get_property(dp, "port1-disable", NULL) != NULL)
64 devflags |= ISP1760_FLAG_PORT1_DIS;
65
66 /* Some systems wire up only 16 of the 32 data lines */
67 prop = of_get_property(dp, "bus-width", NULL);
68 if (prop && *prop == 16)
69 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
70
71 if (of_get_property(dp, "port1-otg", NULL) != NULL)
72 devflags |= ISP1760_FLAG_OTG_EN;
73
74 if (of_get_property(dp, "analog-oc", NULL) != NULL)
75 devflags |= ISP1760_FLAG_ANALOG_OC;
76
77 if (of_get_property(dp, "dack-polarity", NULL) != NULL)
78 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
79
80 if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
81 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
82
58 hcd = isp1760_register(memory.start, res_len, virq, 83 hcd = isp1760_register(memory.start, res_len, virq,
59 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev->dev.bus_id); 84 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
85 devflags);
60 if (IS_ERR(hcd)) { 86 if (IS_ERR(hcd)) {
61 ret = PTR_ERR(hcd); 87 ret = PTR_ERR(hcd);
62 goto release_reg; 88 goto release_reg;
@@ -87,6 +113,9 @@ static struct of_device_id of_isp1760_match[] = {
87 { 113 {
88 .compatible = "nxp,usb-isp1760", 114 .compatible = "nxp,usb-isp1760",
89 }, 115 },
116 {
117 .compatible = "nxp,usb-isp1761",
118 },
90 { }, 119 { },
91}; 120};
92MODULE_DEVICE_TABLE(of, of_isp1760_match); 121MODULE_DEVICE_TABLE(of, of_isp1760_match);
@@ -116,6 +145,7 @@ static int __devinit isp1761_pci_probe(struct pci_dev *dev,
116 int length; 145 int length;
117 int status = 1; 146 int status = 1;
118 struct usb_hcd *hcd; 147 struct usb_hcd *hcd;
148 unsigned int devflags = 0;
119 149
120 if (usb_disabled()) 150 if (usb_disabled())
121 return -ENODEV; 151 return -ENODEV;
@@ -200,7 +230,8 @@ static int __devinit isp1761_pci_probe(struct pci_dev *dev,
200 230
201 dev->dev.dma_mask = NULL; 231 dev->dev.dma_mask = NULL;
202 hcd = isp1760_register(pci_mem_phy0, length, dev->irq, 232 hcd = isp1760_register(pci_mem_phy0, length, dev->irq,
203 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev->dev.bus_id); 233 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
234 devflags);
204 pci_set_drvdata(dev, hcd); 235 pci_set_drvdata(dev, hcd);
205 if (!hcd) 236 if (!hcd)
206 return 0; 237 return 0;
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
index e534f9de0f05..a5d8e550d897 100644
--- a/drivers/usb/host/ohci-at91.c
+++ b/drivers/usb/host/ohci-at91.c
@@ -91,7 +91,7 @@ static void at91_stop_hc(struct platform_device *pdev)
91 91
92/*-------------------------------------------------------------------------*/ 92/*-------------------------------------------------------------------------*/
93 93
94static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *); 94static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
95 95
96/* configure so an HC device and id are always provided */ 96/* configure so an HC device and id are always provided */
97/* always called with process context; sleeping is OK */ 97/* always called with process context; sleeping is OK */
@@ -184,13 +184,14 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
184 * context, "rmmod" or something similar. 184 * context, "rmmod" or something similar.
185 * 185 *
186 */ 186 */
187static int usb_hcd_at91_remove(struct usb_hcd *hcd, 187static void usb_hcd_at91_remove(struct usb_hcd *hcd,
188 struct platform_device *pdev) 188 struct platform_device *pdev)
189{ 189{
190 usb_remove_hcd(hcd); 190 usb_remove_hcd(hcd);
191 at91_stop_hc(pdev); 191 at91_stop_hc(pdev);
192 iounmap(hcd->regs); 192 iounmap(hcd->regs);
193 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 193 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
194 usb_put_hcd(hcd);
194 195
195 if (cpu_is_at91sam9261()) 196 if (cpu_is_at91sam9261())
196 clk_put(hclk); 197 clk_put(hclk);
@@ -199,7 +200,6 @@ static int usb_hcd_at91_remove(struct usb_hcd *hcd,
199 fclk = iclk = hclk = NULL; 200 fclk = iclk = hclk = NULL;
200 201
201 dev_set_drvdata(&pdev->dev, NULL); 202 dev_set_drvdata(&pdev->dev, NULL);
202 return 0;
203} 203}
204 204
205/*-------------------------------------------------------------------------*/ 205/*-------------------------------------------------------------------------*/
@@ -309,7 +309,8 @@ static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
309 } 309 }
310 310
311 device_init_wakeup(&pdev->dev, 0); 311 device_init_wakeup(&pdev->dev, 0);
312 return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev); 312 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
313 return 0;
313} 314}
314 315
315#ifdef CONFIG_PM 316#ifdef CONFIG_PM
diff --git a/drivers/usb/host/ohci-au1xxx.c b/drivers/usb/host/ohci-au1xxx.c
index 68c17f5ea8ea..c0948008fe3d 100644
--- a/drivers/usb/host/ohci-au1xxx.c
+++ b/drivers/usb/host/ohci-au1xxx.c
@@ -34,7 +34,8 @@
34#ifdef __LITTLE_ENDIAN 34#ifdef __LITTLE_ENDIAN
35#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C) 35#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C)
36#elif __BIG_ENDIAN 36#elif __BIG_ENDIAN
37#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C | USBH_ENABLE_BE) 37#define USBH_ENABLE_INIT (USBH_ENABLE_CE | USBH_ENABLE_E | USBH_ENABLE_C | \
38 USBH_ENABLE_BE)
38#else 39#else
39#error not byte order defined 40#error not byte order defined
40#endif 41#endif
@@ -46,213 +47,87 @@
46#define USB_MCFG_RDCOMB (1<<30) 47#define USB_MCFG_RDCOMB (1<<30)
47#define USB_MCFG_SSDEN (1<<23) 48#define USB_MCFG_SSDEN (1<<23)
48#define USB_MCFG_OHCCLKEN (1<<16) 49#define USB_MCFG_OHCCLKEN (1<<16)
50#ifdef CONFIG_DMA_COHERENT
49#define USB_MCFG_UCAM (1<<7) 51#define USB_MCFG_UCAM (1<<7)
52#else
53#define USB_MCFG_UCAM (0)
54#endif
50#define USB_MCFG_OBMEN (1<<1) 55#define USB_MCFG_OBMEN (1<<1)
51#define USB_MCFG_OMEMEN (1<<0) 56#define USB_MCFG_OMEMEN (1<<0)
52 57
53#define USBH_ENABLE_CE USB_MCFG_OHCCLKEN 58#define USBH_ENABLE_CE USB_MCFG_OHCCLKEN
54#ifdef CONFIG_DMA_COHERENT 59
55#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \ 60#define USBH_ENABLE_INIT (USB_MCFG_PFEN | USB_MCFG_RDCOMB | \
56 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \ 61 USBH_ENABLE_CE | USB_MCFG_SSDEN | \
57 | USB_MCFG_SSDEN | USB_MCFG_UCAM \ 62 USB_MCFG_UCAM | \
58 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN) 63 USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
59#else 64
60#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \
61 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
62 | USB_MCFG_SSDEN \
63 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
64#endif
65#define USBH_DISABLE (USB_MCFG_OBMEN | USB_MCFG_OMEMEN) 65#define USBH_DISABLE (USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
66 66
67#endif /* Au1200 */ 67#endif /* Au1200 */
68 68
69extern int usb_disabled(void); 69extern int usb_disabled(void);
70 70
71/*-------------------------------------------------------------------------*/ 71static void au1xxx_start_ohc(void)
72
73static void au1xxx_start_ohc(struct platform_device *dev)
74{ 72{
75 printk(KERN_DEBUG __FILE__
76 ": starting Au1xxx OHCI USB Controller\n");
77
78 /* enable host controller */ 73 /* enable host controller */
79
80#ifndef CONFIG_SOC_AU1200 74#ifndef CONFIG_SOC_AU1200
81
82 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG); 75 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG);
76 au_sync();
83 udelay(1000); 77 udelay(1000);
84 au_writel(USBH_ENABLE_INIT, USB_HOST_CONFIG);
85 udelay(1000);
86
87#else /* Au1200 */
88 78
89 /* write HW defaults again in case Yamon cleared them */ 79 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
90 if (au_readl(USB_HOST_CONFIG) == 0) { 80 au_sync();
91 au_writel(0x00d02000, USB_HOST_CONFIG);
92 au_readl(USB_HOST_CONFIG);
93 udelay(1000);
94 }
95 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
96 au_readl(USB_HOST_CONFIG);
97 udelay(1000); 81 udelay(1000);
98 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
99 au_readl(USB_HOST_CONFIG);
100 udelay(1000);
101
102#endif /* Au1200 */
103 82
104#ifndef CONFIG_SOC_AU1200
105 /* wait for reset complete (read register twice; see au1500 errata) */ 83 /* wait for reset complete (read register twice; see au1500 errata) */
106 while (au_readl(USB_HOST_CONFIG), 84 while (au_readl(USB_HOST_CONFIG),
107 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD)) 85 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD))
108#endif
109 udelay(1000); 86 udelay(1000);
110 87
111 printk(KERN_DEBUG __FILE__
112 ": Clock to USB host has been enabled \n");
113}
114
115static void au1xxx_stop_ohc(struct platform_device *dev)
116{
117 printk(KERN_DEBUG __FILE__
118 ": stopping Au1xxx OHCI USB Controller\n");
119
120#ifndef CONFIG_SOC_AU1200
121
122 /* Disable clock */
123 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
124
125#else /* Au1200 */ 88#else /* Au1200 */
126 89 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_CE, USB_HOST_CONFIG);
127 /* Disable mem */ 90 au_sync();
128 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
129 udelay(1000); 91 udelay(1000);
130 /* Disable clock */ 92
131 au_writel(~USBH_ENABLE_CE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG); 93 au_writel(au_readl(USB_HOST_CONFIG) | USBH_ENABLE_INIT, USB_HOST_CONFIG);
132 au_readl(USB_HOST_CONFIG); 94 au_sync();
95 udelay(2000);
133#endif /* Au1200 */ 96#endif /* Au1200 */
134} 97}
135 98
136 99static void au1xxx_stop_ohc(void)
137/*-------------------------------------------------------------------------*/
138
139/* configure so an HC device and id are always provided */
140/* always called with process context; sleeping is OK */
141
142
143/**
144 * usb_ohci_au1xxx_probe - initialize Au1xxx-based HCDs
145 * Context: !in_interrupt()
146 *
147 * Allocates basic resources for this USB host controller, and
148 * then invokes the start() method for the HCD associated with it
149 * through the hotplug entry's driver_data.
150 *
151 */
152static int usb_ohci_au1xxx_probe(const struct hc_driver *driver,
153 struct platform_device *dev)
154{ 100{
155 int retval; 101#ifdef CONFIG_SOC_AU1200
156 struct usb_hcd *hcd; 102 /* Disable mem */
157 103 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_DISABLE, USB_HOST_CONFIG);
158#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT) 104 au_sync();
159 /* Au1200 AB USB does not support coherent memory */ 105 udelay(1000);
160 if (!(read_c0_prid() & 0xff)) {
161 pr_info("%s: this is chip revision AB !!\n",
162 dev->name);
163 pr_info("%s: update your board or re-configure the kernel\n",
164 dev->name);
165 return -ENODEV;
166 }
167#endif 106#endif
168 107 /* Disable clock */
169 if (dev->resource[1].flags != IORESOURCE_IRQ) { 108 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
170 pr_debug("resource[1] is not IORESOURCE_IRQ\n"); 109 au_sync();
171 return -ENOMEM;
172 }
173
174 hcd = usb_create_hcd(driver, &dev->dev, "au1xxx");
175 if (!hcd)
176 return -ENOMEM;
177 hcd->rsrc_start = dev->resource[0].start;
178 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
179
180 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
181 pr_debug("request_mem_region failed\n");
182 retval = -EBUSY;
183 goto err1;
184 }
185
186 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
187 if (!hcd->regs) {
188 pr_debug("ioremap failed\n");
189 retval = -ENOMEM;
190 goto err2;
191 }
192
193 au1xxx_start_ohc(dev);
194 ohci_hcd_init(hcd_to_ohci(hcd));
195
196 retval = usb_add_hcd(hcd, dev->resource[1].start, IRQF_DISABLED | IRQF_SHARED);
197 if (retval == 0)
198 return retval;
199
200 au1xxx_stop_ohc(dev);
201 iounmap(hcd->regs);
202 err2:
203 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
204 err1:
205 usb_put_hcd(hcd);
206 return retval;
207}
208
209
210/* may be called without controller electrically present */
211/* may be called with controller, bus, and devices active */
212
213/**
214 * usb_hcd_au1xxx_remove - shutdown processing for Au1xxx-based HCDs
215 * @dev: USB Host Controller being removed
216 * Context: !in_interrupt()
217 *
218 * Reverses the effect of usb_hcd_au1xxx_probe(), first invoking
219 * the HCD's stop() method. It is always called from a thread
220 * context, normally "rmmod", "apmd", or something similar.
221 *
222 */
223static void usb_ohci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
224{
225 usb_remove_hcd(hcd);
226 au1xxx_stop_ohc(dev);
227 iounmap(hcd->regs);
228 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
229 usb_put_hcd(hcd);
230} 110}
231 111
232/*-------------------------------------------------------------------------*/ 112static int __devinit ohci_au1xxx_start(struct usb_hcd *hcd)
233
234static int __devinit
235ohci_au1xxx_start (struct usb_hcd *hcd)
236{ 113{
237 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 114 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
238 int ret; 115 int ret;
239 116
240 ohci_dbg (ohci, "ohci_au1xxx_start, ohci:%p", ohci); 117 ohci_dbg(ohci, "ohci_au1xxx_start, ohci:%p", ohci);
241 118
242 if ((ret = ohci_init (ohci)) < 0) 119 if ((ret = ohci_init(ohci)) < 0)
243 return ret; 120 return ret;
244 121
245 if ((ret = ohci_run (ohci)) < 0) { 122 if ((ret = ohci_run(ohci)) < 0) {
246 err ("can't start %s", hcd->self.bus_name); 123 err ("can't start %s", hcd->self.bus_name);
247 ohci_stop (hcd); 124 ohci_stop(hcd);
248 return ret; 125 return ret;
249 } 126 }
250 127
251 return 0; 128 return 0;
252} 129}
253 130
254/*-------------------------------------------------------------------------*/
255
256static const struct hc_driver ohci_au1xxx_hc_driver = { 131static const struct hc_driver ohci_au1xxx_hc_driver = {
257 .description = hcd_name, 132 .description = hcd_name,
258 .product_desc = "Au1xxx OHCI", 133 .product_desc = "Au1xxx OHCI",
@@ -296,18 +171,66 @@ static const struct hc_driver ohci_au1xxx_hc_driver = {
296 .start_port_reset = ohci_start_port_reset, 171 .start_port_reset = ohci_start_port_reset,
297}; 172};
298 173
299/*-------------------------------------------------------------------------*/
300
301static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev) 174static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
302{ 175{
303 int ret; 176 int ret;
304 177 struct usb_hcd *hcd;
305 pr_debug ("In ohci_hcd_au1xxx_drv_probe");
306 178
307 if (usb_disabled()) 179 if (usb_disabled())
308 return -ENODEV; 180 return -ENODEV;
309 181
310 ret = usb_ohci_au1xxx_probe(&ohci_au1xxx_hc_driver, pdev); 182#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
183 /* Au1200 AB USB does not support coherent memory */
184 if (!(read_c0_prid() & 0xff)) {
185 printk(KERN_INFO "%s: this is chip revision AB !!\n",
186 pdev->name);
187 printk(KERN_INFO "%s: update your board or re-configure "
188 "the kernel\n", pdev->name);
189 return -ENODEV;
190 }
191#endif
192
193 if (pdev->resource[1].flags != IORESOURCE_IRQ) {
194 pr_debug("resource[1] is not IORESOURCE_IRQ\n");
195 return -ENOMEM;
196 }
197
198 hcd = usb_create_hcd(&ohci_au1xxx_hc_driver, &pdev->dev, "au1xxx");
199 if (!hcd)
200 return -ENOMEM;
201
202 hcd->rsrc_start = pdev->resource[0].start;
203 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
204
205 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
206 pr_debug("request_mem_region failed\n");
207 ret = -EBUSY;
208 goto err1;
209 }
210
211 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
212 if (!hcd->regs) {
213 pr_debug("ioremap failed\n");
214 ret = -ENOMEM;
215 goto err2;
216 }
217
218 au1xxx_start_ohc();
219 ohci_hcd_init(hcd_to_ohci(hcd));
220
221 ret = usb_add_hcd(hcd, pdev->resource[1].start,
222 IRQF_DISABLED | IRQF_SHARED);
223 if (ret == 0) {
224 platform_set_drvdata(pdev, hcd);
225 return ret;
226 }
227
228 au1xxx_stop_ohc();
229 iounmap(hcd->regs);
230err2:
231 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
232err1:
233 usb_put_hcd(hcd);
311 return ret; 234 return ret;
312} 235}
313 236
@@ -315,30 +238,78 @@ static int ohci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
315{ 238{
316 struct usb_hcd *hcd = platform_get_drvdata(pdev); 239 struct usb_hcd *hcd = platform_get_drvdata(pdev);
317 240
318 usb_ohci_au1xxx_remove(hcd, pdev); 241 usb_remove_hcd(hcd);
242 au1xxx_stop_ohc();
243 iounmap(hcd->regs);
244 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
245 usb_put_hcd(hcd);
246 platform_set_drvdata(pdev, NULL);
247
319 return 0; 248 return 0;
320} 249}
321 /*TBD*/ 250
322/*static int ohci_hcd_au1xxx_drv_suspend(struct platform_device *dev) 251#ifdef CONFIG_PM
252static int ohci_hcd_au1xxx_drv_suspend(struct platform_device *pdev,
253 pm_message_t message)
323{ 254{
324 struct usb_hcd *hcd = platform_get_drvdata(dev); 255 struct usb_hcd *hcd = platform_get_drvdata(pdev);
256 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
257 unsigned long flags;
258 int rc;
259
260 rc = 0;
261
262 /* Root hub was already suspended. Disable irq emission and
263 * mark HW unaccessible, bail out if RH has been resumed. Use
264 * the spinlock to properly synchronize with possible pending
265 * RH suspend or resume activity.
266 *
267 * This is still racy as hcd->state is manipulated outside of
268 * any locks =P But that will be a different fix.
269 */
270 spin_lock_irqsave(&ohci->lock, flags);
271 if (hcd->state != HC_STATE_SUSPENDED) {
272 rc = -EINVAL;
273 goto bail;
274 }
275 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
276 (void)ohci_readl(ohci, &ohci->regs->intrdisable);
325 277
326 return 0; 278 /* make sure snapshot being resumed re-enumerates everything */
279 if (message.event == PM_EVENT_PRETHAW)
280 ohci_usb_reset(ohci);
281
282 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
283
284 au1xxx_stop_ohc();
285bail:
286 spin_unlock_irqrestore(&ohci->lock, flags);
287
288 return rc;
327} 289}
328static int ohci_hcd_au1xxx_drv_resume(struct platform_device *dev) 290
291static int ohci_hcd_au1xxx_drv_resume(struct platform_device *pdev)
329{ 292{
330 struct usb_hcd *hcd = platform_get_drvdata(dev); 293 struct usb_hcd *hcd = platform_get_drvdata(pdev);
294
295 au1xxx_start_ohc();
296
297 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
298 ohci_finish_controller_resume(hcd);
331 299
332 return 0; 300 return 0;
333} 301}
334*/ 302#else
303#define ohci_hcd_au1xxx_drv_suspend NULL
304#define ohci_hcd_au1xxx_drv_resume NULL
305#endif
335 306
336static struct platform_driver ohci_hcd_au1xxx_driver = { 307static struct platform_driver ohci_hcd_au1xxx_driver = {
337 .probe = ohci_hcd_au1xxx_drv_probe, 308 .probe = ohci_hcd_au1xxx_drv_probe,
338 .remove = ohci_hcd_au1xxx_drv_remove, 309 .remove = ohci_hcd_au1xxx_drv_remove,
339 .shutdown = usb_hcd_platform_shutdown, 310 .shutdown = usb_hcd_platform_shutdown,
340 /*.suspend = ohci_hcd_au1xxx_drv_suspend, */ 311 .suspend = ohci_hcd_au1xxx_drv_suspend,
341 /*.resume = ohci_hcd_au1xxx_drv_resume, */ 312 .resume = ohci_hcd_au1xxx_drv_resume,
342 .driver = { 313 .driver = {
343 .name = "au1xxx-ohci", 314 .name = "au1xxx-ohci",
344 .owner = THIS_MODULE, 315 .owner = THIS_MODULE,
diff --git a/drivers/usb/host/ohci-dbg.c b/drivers/usb/host/ohci-dbg.c
index e06bfaebec54..7cef1d2f7ccc 100644
--- a/drivers/usb/host/ohci-dbg.c
+++ b/drivers/usb/host/ohci-dbg.c
@@ -651,7 +651,7 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
651 "%s\n" 651 "%s\n"
652 "%s version " DRIVER_VERSION "\n", 652 "%s version " DRIVER_VERSION "\n",
653 hcd->self.controller->bus->name, 653 hcd->self.controller->bus->name,
654 hcd->self.controller->bus_id, 654 dev_name(hcd->self.controller),
655 hcd->product_desc, 655 hcd->product_desc,
656 hcd_name); 656 hcd_name);
657 657
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index a8160d65f32b..26bc47941d01 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -974,7 +974,7 @@ MODULE_LICENSE ("GPL");
974#define PCI_DRIVER ohci_pci_driver 974#define PCI_DRIVER ohci_pci_driver
975#endif 975#endif
976 976
977#ifdef CONFIG_SA1111 977#if defined(CONFIG_ARCH_SA1100) && defined(CONFIG_SA1111)
978#include "ohci-sa1111.c" 978#include "ohci-sa1111.c"
979#define SA1111_DRIVER ohci_hcd_sa1111_driver 979#define SA1111_DRIVER ohci_hcd_sa1111_driver
980#endif 980#endif
diff --git a/drivers/usb/host/ohci-omap.c b/drivers/usb/host/ohci-omap.c
index a19a4f80a6e1..6e5e5f81ac90 100644
--- a/drivers/usb/host/ohci-omap.c
+++ b/drivers/usb/host/ohci-omap.c
@@ -329,7 +329,7 @@ static int usb_hcd_omap_probe (const struct hc_driver *driver,
329 } 329 }
330 330
331 331
332 hcd = usb_create_hcd (driver, &pdev->dev, pdev->dev.bus_id); 332 hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
333 if (!hcd) { 333 if (!hcd) {
334 retval = -ENOMEM; 334 retval = -ENOMEM;
335 goto err0; 335 goto err0;
diff --git a/drivers/usb/host/ohci-pnx4008.c b/drivers/usb/host/ohci-pnx4008.c
index 28b458f20cc3..6ad8f2fc57b9 100644
--- a/drivers/usb/host/ohci-pnx4008.c
+++ b/drivers/usb/host/ohci-pnx4008.c
@@ -109,8 +109,6 @@ static struct clk *usb_clk;
109 109
110static int isp1301_probe(struct i2c_adapter *adap); 110static int isp1301_probe(struct i2c_adapter *adap);
111static int isp1301_detach(struct i2c_client *client); 111static int isp1301_detach(struct i2c_client *client);
112static int isp1301_command(struct i2c_client *client, unsigned int cmd,
113 void *arg);
114 112
115static const unsigned short normal_i2c[] = 113static const unsigned short normal_i2c[] =
116 { ISP1301_I2C_ADDR, ISP1301_I2C_ADDR + 1, I2C_CLIENT_END }; 114 { ISP1301_I2C_ADDR, ISP1301_I2C_ADDR + 1, I2C_CLIENT_END };
@@ -123,30 +121,37 @@ static struct i2c_client_address_data addr_data = {
123}; 121};
124 122
125struct i2c_driver isp1301_driver = { 123struct i2c_driver isp1301_driver = {
126 .class = I2C_CLASS_HWMON, 124 .driver = {
125 .name = "isp1301_pnx",
126 },
127 .attach_adapter = isp1301_probe, 127 .attach_adapter = isp1301_probe,
128 .detach_client = isp1301_detach, 128 .detach_client = isp1301_detach,
129 .command = isp1301_command
130}; 129};
131 130
132static int isp1301_attach(struct i2c_adapter *adap, int addr, int kind) 131static int isp1301_attach(struct i2c_adapter *adap, int addr, int kind)
133{ 132{
134 struct i2c_client *c; 133 struct i2c_client *c;
134 int err;
135 135
136 c = kzalloc(sizeof(*c), GFP_KERNEL); 136 c = kzalloc(sizeof(*c), GFP_KERNEL);
137
138 if (!c) 137 if (!c)
139 return -ENOMEM; 138 return -ENOMEM;
140 139
141 strcpy(c->name, "isp1301"); 140 strlcpy(c->name, "isp1301_pnx", I2C_NAME_SIZE);
142 c->flags = 0; 141 c->flags = 0;
143 c->addr = addr; 142 c->addr = addr;
144 c->adapter = adap; 143 c->adapter = adap;
145 c->driver = &isp1301_driver; 144 c->driver = &isp1301_driver;
146 145
146 err = i2c_attach_client(c);
147 if (err) {
148 kfree(c);
149 return err;
150 }
151
147 isp1301_i2c_client = c; 152 isp1301_i2c_client = c;
148 153
149 return i2c_attach_client(c); 154 return 0;
150} 155}
151 156
152static int isp1301_probe(struct i2c_adapter *adap) 157static int isp1301_probe(struct i2c_adapter *adap)
@@ -161,13 +166,6 @@ static int isp1301_detach(struct i2c_client *client)
161 return 0; 166 return 0;
162} 167}
163 168
164/* No commands defined */
165static int isp1301_command(struct i2c_client *client, unsigned int cmd,
166 void *arg)
167{
168 return 0;
169}
170
171static void i2c_write(u8 buf, u8 subaddr) 169static void i2c_write(u8 buf, u8 subaddr)
172{ 170{
173 char tmpbuf[2]; 171 char tmpbuf[2];
@@ -389,7 +387,7 @@ static int __devinit usb_hcd_pnx4008_probe(struct platform_device *pdev)
389 while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) != 387 while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
390 USB_CLOCK_MASK) ; 388 USB_CLOCK_MASK) ;
391 389
392 hcd = usb_create_hcd (driver, &pdev->dev, pdev->dev.bus_id); 390 hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
393 if (!hcd) { 391 if (!hcd) {
394 err("Failed to allocate HC buffer"); 392 err("Failed to allocate HC buffer");
395 ret = -ENOMEM; 393 ret = -ENOMEM;
diff --git a/drivers/usb/host/ohci-ppc-of.c b/drivers/usb/host/ohci-ppc-of.c
index a67252791223..91e6e101a4cc 100644
--- a/drivers/usb/host/ohci-ppc-of.c
+++ b/drivers/usb/host/ohci-ppc-of.c
@@ -14,8 +14,8 @@
14 */ 14 */
15 15
16#include <linux/signal.h> 16#include <linux/signal.h>
17#include <linux/of_platform.h>
17 18
18#include <asm/of_platform.h>
19#include <asm/prom.h> 19#include <asm/prom.h>
20 20
21 21
diff --git a/drivers/usb/host/ohci-ps3.c b/drivers/usb/host/ohci-ps3.c
index c1935ae537f8..55c95647f008 100644
--- a/drivers/usb/host/ohci-ps3.c
+++ b/drivers/usb/host/ohci-ps3.c
@@ -129,7 +129,7 @@ static int ps3_ohci_probe(struct ps3_system_bus_device *dev)
129 129
130 dev->core.dma_mask = &dummy_mask; /* FIXME: for improper usb code */ 130 dev->core.dma_mask = &dummy_mask; /* FIXME: for improper usb code */
131 131
132 hcd = usb_create_hcd(&ps3_ohci_hc_driver, &dev->core, dev->core.bus_id); 132 hcd = usb_create_hcd(&ps3_ohci_hc_driver, &dev->core, dev_name(&dev->core));
133 133
134 if (!hcd) { 134 if (!hcd) {
135 dev_dbg(&dev->core, "%s:%d: usb_create_hcd failed\n", __func__, 135 dev_dbg(&dev->core, "%s:%d: usb_create_hcd failed\n", __func__,
diff --git a/drivers/usb/host/ohci-q.c b/drivers/usb/host/ohci-q.c
index 9b547407c934..6a9b4c557953 100644
--- a/drivers/usb/host/ohci-q.c
+++ b/drivers/usb/host/ohci-q.c
@@ -159,9 +159,6 @@ static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
159{ 159{
160 int branch; 160 int branch;
161 161
162 if (ohci_to_hcd(ohci)->state == HC_STATE_QUIESCING)
163 return -EAGAIN;
164
165 ed->state = ED_OPER; 162 ed->state = ED_OPER;
166 ed->ed_prev = NULL; 163 ed->ed_prev = NULL;
167 ed->ed_next = NULL; 164 ed->ed_next = NULL;
diff --git a/drivers/usb/host/ohci-sm501.c b/drivers/usb/host/ohci-sm501.c
index e610698c6b60..21b164e4abeb 100644
--- a/drivers/usb/host/ohci-sm501.c
+++ b/drivers/usb/host/ohci-sm501.c
@@ -143,7 +143,7 @@ static int ohci_hcd_sm501_drv_probe(struct platform_device *pdev)
143 goto err2; 143 goto err2;
144 } 144 }
145 145
146 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id); 146 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
147 if (!hcd) { 147 if (!hcd) {
148 retval = -ENOMEM; 148 retval = -ENOMEM;
149 goto err2; 149 goto err2;
diff --git a/drivers/usb/host/ohci-ssb.c b/drivers/usb/host/ohci-ssb.c
index 7275186db315..3660c83d80af 100644
--- a/drivers/usb/host/ohci-ssb.c
+++ b/drivers/usb/host/ohci-ssb.c
@@ -113,7 +113,7 @@ static int ssb_ohci_attach(struct ssb_device *dev)
113 ssb_device_enable(dev, flags); 113 ssb_device_enable(dev, flags);
114 114
115 hcd = usb_create_hcd(&ssb_ohci_hc_driver, dev->dev, 115 hcd = usb_create_hcd(&ssb_ohci_hc_driver, dev->dev,
116 dev->dev->bus_id); 116 dev_name(dev->dev));
117 if (!hcd) 117 if (!hcd)
118 goto err_dev_disable; 118 goto err_dev_disable;
119 ohcidev = hcd_to_ssb_ohci(hcd); 119 ohcidev = hcd_to_ssb_ohci(hcd);
diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
index 16667342b3c3..d5f02dddb120 100644
--- a/drivers/usb/host/r8a66597-hcd.c
+++ b/drivers/usb/host/r8a66597-hcd.c
@@ -312,9 +312,9 @@ static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
312static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch) 312static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
313{ 313{
314 u16 pipenum = pipe->info.pipenum; 314 u16 pipenum = pipe->info.pipenum;
315 unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO}; 315 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
316 unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL}; 316 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
317 unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR}; 317 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
318 318
319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */ 319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
320 dma_ch = R8A66597_PIPE_NO_DMA; 320 dma_ch = R8A66597_PIPE_NO_DMA;
@@ -863,6 +863,32 @@ static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
863 dev->dma_map = 0; 863 dev->dma_map = 0;
864} 864}
865 865
866static u16 get_interval(struct urb *urb, __u8 interval)
867{
868 u16 time = 1;
869 int i;
870
871 if (urb->dev->speed == USB_SPEED_HIGH) {
872 if (interval > IITV)
873 time = IITV;
874 else
875 time = interval ? interval - 1 : 0;
876 } else {
877 if (interval > 128) {
878 time = IITV;
879 } else {
880 /* calculate the nearest value for PIPEPERI */
881 for (i = 0; i < 7; i++) {
882 if ((1 << i) < interval &&
883 (1 << (i + 1) > interval))
884 time = 1 << i;
885 }
886 }
887 }
888
889 return time;
890}
891
866static unsigned long get_timer_interval(struct urb *urb, __u8 interval) 892static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
867{ 893{
868 __u8 i; 894 __u8 i;
@@ -901,10 +927,7 @@ static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
901 info.interval = 0; 927 info.interval = 0;
902 info.timer_interval = 0; 928 info.timer_interval = 0;
903 } else { 929 } else {
904 if (ep->bInterval > IITV) 930 info.interval = get_interval(urb, ep->bInterval);
905 info.interval = IITV;
906 else
907 info.interval = ep->bInterval ? ep->bInterval - 1 : 0;
908 info.timer_interval = get_timer_interval(urb, ep->bInterval); 931 info.timer_interval = get_timer_interval(urb, ep->bInterval);
909 } 932 }
910 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 933 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
@@ -2244,6 +2267,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
2244 struct r8a66597 *r8a66597; 2267 struct r8a66597 *r8a66597;
2245 int ret = 0; 2268 int ret = 0;
2246 int i; 2269 int i;
2270 unsigned long irq_trigger;
2247 2271
2248 if (pdev->dev.dma_mask) { 2272 if (pdev->dev.dma_mask) {
2249 ret = -EINVAL; 2273 ret = -EINVAL;
@@ -2302,7 +2326,11 @@ static int __init r8a66597_probe(struct platform_device *pdev)
2302 INIT_LIST_HEAD(&r8a66597->child_device); 2326 INIT_LIST_HEAD(&r8a66597->child_device);
2303 2327
2304 hcd->rsrc_start = res->start; 2328 hcd->rsrc_start = res->start;
2305 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED); 2329 if (irq_sense == INTL)
2330 irq_trigger = IRQF_TRIGGER_LOW;
2331 else
2332 irq_trigger = IRQF_TRIGGER_FALLING;
2333 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2306 if (ret != 0) { 2334 if (ret != 0) {
2307 err("Failed to add hcd"); 2335 err("Failed to add hcd");
2308 goto clean_up; 2336 goto clean_up;
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index 426575247b23..340d72da554a 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -1674,7 +1674,7 @@ sl811h_probe(struct platform_device *dev)
1674 } 1674 }
1675 1675
1676 /* allocate and initialize hcd */ 1676 /* allocate and initialize hcd */
1677 hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev->dev.bus_id); 1677 hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
1678 if (!hcd) { 1678 if (!hcd) {
1679 retval = -ENOMEM; 1679 retval = -ENOMEM;
1680 goto err5; 1680 goto err5;
diff --git a/drivers/usb/host/u132-hcd.c b/drivers/usb/host/u132-hcd.c
index 9b6323f768b2..20ad3c48fcb2 100644
--- a/drivers/usb/host/u132-hcd.c
+++ b/drivers/usb/host/u132-hcd.c
@@ -3124,7 +3124,7 @@ static int __devinit u132_probe(struct platform_device *pdev)
3124 if (pdev->dev.dma_mask) 3124 if (pdev->dev.dma_mask)
3125 return -EINVAL; 3125 return -EINVAL;
3126 3126
3127 hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, pdev->dev.bus_id); 3127 hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, dev_name(&pdev->dev));
3128 if (!hcd) { 3128 if (!hcd) {
3129 printk(KERN_ERR "failed to create the usb hcd struct for U132\n" 3129 printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3130 ); 3130 );
diff --git a/drivers/usb/host/uhci-hub.c b/drivers/usb/host/uhci-hub.c
index 8e4427aebb14..885b585360b9 100644
--- a/drivers/usb/host/uhci-hub.c
+++ b/drivers/usb/host/uhci-hub.c
@@ -12,7 +12,7 @@
12 * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu 12 * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu
13 */ 13 */
14 14
15static __u8 root_hub_hub_des[] = 15static const __u8 root_hub_hub_des[] =
16{ 16{
17 0x09, /* __u8 bLength; */ 17 0x09, /* __u8 bLength; */
18 0x29, /* __u8 bDescriptorType; Hub-descriptor */ 18 0x29, /* __u8 bDescriptorType; Hub-descriptor */
diff --git a/drivers/usb/misc/auerswald.c b/drivers/usb/misc/auerswald.c
index 093938697426..d2f61d5510e7 100644
--- a/drivers/usb/misc/auerswald.c
+++ b/drivers/usb/misc/auerswald.c
@@ -1421,7 +1421,8 @@ ofail: mutex_unlock(&cp->mutex);
1421 1421
1422 1422
1423/* IOCTL functions */ 1423/* IOCTL functions */
1424static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 1424static long auerchar_ioctl(struct file *file, unsigned int cmd,
1425 unsigned long arg)
1425{ 1426{
1426 pauerchar_t ccp = (pauerchar_t) file->private_data; 1427 pauerchar_t ccp = (pauerchar_t) file->private_data;
1427 int ret = 0; 1428 int ret = 0;
@@ -1452,7 +1453,7 @@ static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int
1452 mutex_unlock(&ccp->mutex); 1453 mutex_unlock(&ccp->mutex);
1453 return -ENODEV; 1454 return -ENODEV;
1454 } 1455 }
1455 1456 lock_kernel();
1456 switch (cmd) { 1457 switch (cmd) {
1457 1458
1458 /* return != 0 if Transmitt channel ready to send */ 1459 /* return != 0 if Transmitt channel ready to send */
@@ -1547,9 +1548,10 @@ static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int
1547 1548
1548 default: 1549 default:
1549 dbg ("IOCTL_AU_UNKNOWN"); 1550 dbg ("IOCTL_AU_UNKNOWN");
1550 ret = -ENOIOCTLCMD; 1551 ret = -ENOTTY;
1551 break; 1552 break;
1552 } 1553 }
1554 unlock_kernel();
1553 /* release the mutexes */ 1555 /* release the mutexes */
1554 mutex_unlock(&cp->mutex); 1556 mutex_unlock(&cp->mutex);
1555 mutex_unlock(&ccp->mutex); 1557 mutex_unlock(&ccp->mutex);
@@ -1860,7 +1862,7 @@ static const struct file_operations auerswald_fops =
1860 .llseek = no_llseek, 1862 .llseek = no_llseek,
1861 .read = auerchar_read, 1863 .read = auerchar_read,
1862 .write = auerchar_write, 1864 .write = auerchar_write,
1863 .ioctl = auerchar_ioctl, 1865 .unlocked_ioctl = auerchar_ioctl,
1864 .open = auerchar_open, 1866 .open = auerchar_open,
1865 .release = auerchar_release, 1867 .release = auerchar_release,
1866}; 1868};
diff --git a/drivers/usb/misc/emi62.c b/drivers/usb/misc/emi62.c
index 20886c21e739..5d859ded5bbf 100644
--- a/drivers/usb/misc/emi62.c
+++ b/drivers/usb/misc/emi62.c
@@ -6,8 +6,6 @@
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, as published by 7 * it under the terms of the GNU General Public License, as published by
8 * the Free Software Foundation, version 2. 8 * the Free Software Foundation, version 2.
9 *
10 * $Id: emi62.c,v 1.15 2002/04/23 06:13:59 tapio Exp $
11 */ 9 */
12#include <linux/kernel.h> 10#include <linux/kernel.h>
13#include <linux/errno.h> 11#include <linux/errno.h>
diff --git a/drivers/usb/misc/ftdi-elan.c b/drivers/usb/misc/ftdi-elan.c
index ec88b3bfee46..97c280971532 100644
--- a/drivers/usb/misc/ftdi-elan.c
+++ b/drivers/usb/misc/ftdi-elan.c
@@ -656,29 +656,6 @@ static int ftdi_elan_release(struct inode *inode, struct file *file)
656} 656}
657 657
658 658
659#define FTDI_ELAN_IOC_MAGIC 0xA1
660#define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
661static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
662 unsigned int cmd, unsigned long arg)
663{
664 switch (cmd) {
665 case FTDI_ELAN_IOCDEBUG:{
666 char line[132];
667 int size = strncpy_from_user(line,
668 (const char __user *)arg, sizeof(line));
669 if (size < 0) {
670 return -EINVAL;
671 } else {
672 printk(KERN_ERR "TODO: ioctl %s\n", line);
673 return 0;
674 }
675 }
676 default:
677 return -EFAULT;
678 }
679}
680
681
682/* 659/*
683* 660*
684* blocking bulk reads are used to get data from the device 661* blocking bulk reads are used to get data from the device
@@ -1222,7 +1199,6 @@ error_1:
1222static const struct file_operations ftdi_elan_fops = { 1199static const struct file_operations ftdi_elan_fops = {
1223 .owner = THIS_MODULE, 1200 .owner = THIS_MODULE,
1224 .llseek = no_llseek, 1201 .llseek = no_llseek,
1225 .ioctl = ftdi_elan_ioctl,
1226 .read = ftdi_elan_read, 1202 .read = ftdi_elan_read,
1227 .write = ftdi_elan_write, 1203 .write = ftdi_elan_write,
1228 .open = ftdi_elan_open, 1204 .open = ftdi_elan_open,
diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index 1cb54a28347f..e6ca9979e3ae 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -474,8 +474,8 @@ exit:
474/** 474/**
475 * iowarrior_ioctl 475 * iowarrior_ioctl
476 */ 476 */
477static int iowarrior_ioctl(struct inode *inode, struct file *file, 477static long iowarrior_ioctl(struct file *file, unsigned int cmd,
478 unsigned int cmd, unsigned long arg) 478 unsigned long arg)
479{ 479{
480 struct iowarrior *dev = NULL; 480 struct iowarrior *dev = NULL;
481 __u8 *buffer; 481 __u8 *buffer;
@@ -493,6 +493,7 @@ static int iowarrior_ioctl(struct inode *inode, struct file *file,
493 return -ENOMEM; 493 return -ENOMEM;
494 494
495 /* lock this object */ 495 /* lock this object */
496 lock_kernel();
496 mutex_lock(&dev->mutex); 497 mutex_lock(&dev->mutex);
497 498
498 /* verify that the device wasn't unplugged */ 499 /* verify that the device wasn't unplugged */
@@ -584,6 +585,7 @@ static int iowarrior_ioctl(struct inode *inode, struct file *file,
584error_out: 585error_out:
585 /* unlock the device */ 586 /* unlock the device */
586 mutex_unlock(&dev->mutex); 587 mutex_unlock(&dev->mutex);
588 unlock_kernel();
587 kfree(buffer); 589 kfree(buffer);
588 return retval; 590 return retval;
589} 591}
@@ -719,7 +721,7 @@ static const struct file_operations iowarrior_fops = {
719 .owner = THIS_MODULE, 721 .owner = THIS_MODULE,
720 .write = iowarrior_write, 722 .write = iowarrior_write,
721 .read = iowarrior_read, 723 .read = iowarrior_read,
722 .ioctl = iowarrior_ioctl, 724 .unlocked_ioctl = iowarrior_ioctl,
723 .open = iowarrior_open, 725 .open = iowarrior_open,
724 .release = iowarrior_release, 726 .release = iowarrior_release,
725 .poll = iowarrior_poll, 727 .poll = iowarrior_poll,
diff --git a/drivers/usb/misc/rio500.c b/drivers/usb/misc/rio500.c
index 330c18e390b8..248a12aacef6 100644
--- a/drivers/usb/misc/rio500.c
+++ b/drivers/usb/misc/rio500.c
@@ -104,9 +104,7 @@ static int close_rio(struct inode *inode, struct file *file)
104 return 0; 104 return 0;
105} 105}
106 106
107static int 107static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
108ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
109 unsigned long arg)
110{ 108{
111 struct RioCommand rio_cmd; 109 struct RioCommand rio_cmd;
112 struct rio_usb_data *rio = &rio_instance; 110 struct rio_usb_data *rio = &rio_instance;
@@ -116,6 +114,7 @@ ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
116 int retries; 114 int retries;
117 int retval=0; 115 int retval=0;
118 116
117 lock_kernel();
119 mutex_lock(&(rio->lock)); 118 mutex_lock(&(rio->lock));
120 /* Sanity check to make sure rio is connected, powered, etc */ 119 /* Sanity check to make sure rio is connected, powered, etc */
121 if (rio->present == 0 || rio->rio_dev == NULL) { 120 if (rio->present == 0 || rio->rio_dev == NULL) {
@@ -254,6 +253,7 @@ ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
254 253
255err_out: 254err_out:
256 mutex_unlock(&(rio->lock)); 255 mutex_unlock(&(rio->lock));
256 unlock_kernel();
257 return retval; 257 return retval;
258} 258}
259 259
@@ -433,7 +433,7 @@ file_operations usb_rio_fops = {
433 .owner = THIS_MODULE, 433 .owner = THIS_MODULE,
434 .read = read_rio, 434 .read = read_rio,
435 .write = write_rio, 435 .write = write_rio,
436 .ioctl = ioctl_rio, 436 .unlocked_ioctl = ioctl_rio,
437 .open = open_rio, 437 .open = open_rio,
438 .release = close_rio, 438 .release = close_rio,
439}; 439};
diff --git a/drivers/usb/misc/sisusbvga/sisusb.c b/drivers/usb/misc/sisusbvga/sisusb.c
index 33182f4c2267..fbace41a7cba 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.c
+++ b/drivers/usb/misc/sisusbvga/sisusb.c
@@ -2982,9 +2982,8 @@ sisusb_handle_command(struct sisusb_usb_data *sisusb, struct sisusb_command *y,
2982 return retval; 2982 return retval;
2983} 2983}
2984 2984
2985static int 2985static long
2986sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 2986sisusb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2987 unsigned long arg)
2988{ 2987{
2989 struct sisusb_usb_data *sisusb; 2988 struct sisusb_usb_data *sisusb;
2990 struct sisusb_info x; 2989 struct sisusb_info x;
@@ -2995,6 +2994,7 @@ sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
2995 if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) 2994 if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
2996 return -ENODEV; 2995 return -ENODEV;
2997 2996
2997 lock_kernel();
2998 mutex_lock(&sisusb->lock); 2998 mutex_lock(&sisusb->lock);
2999 2999
3000 /* Sanity check */ 3000 /* Sanity check */
@@ -3053,6 +3053,7 @@ sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
3053 3053
3054err_out: 3054err_out:
3055 mutex_unlock(&sisusb->lock); 3055 mutex_unlock(&sisusb->lock);
3056 unlock_kernel();
3056 return retval; 3057 return retval;
3057} 3058}
3058 3059
@@ -3066,9 +3067,7 @@ sisusb_compat_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
3066 case SISUSB_GET_CONFIG_SIZE: 3067 case SISUSB_GET_CONFIG_SIZE:
3067 case SISUSB_GET_CONFIG: 3068 case SISUSB_GET_CONFIG:
3068 case SISUSB_COMMAND: 3069 case SISUSB_COMMAND:
3069 lock_kernel(); 3070 retval = sisusb_ioctl(f, cmd, arg);
3070 retval = sisusb_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
3071 unlock_kernel();
3072 return retval; 3071 return retval;
3073 3072
3074 default: 3073 default:
@@ -3087,7 +3086,7 @@ static const struct file_operations usb_sisusb_fops = {
3087#ifdef SISUSB_NEW_CONFIG_COMPAT 3086#ifdef SISUSB_NEW_CONFIG_COMPAT
3088 .compat_ioctl = sisusb_compat_ioctl, 3087 .compat_ioctl = sisusb_compat_ioctl,
3089#endif 3088#endif
3090 .ioctl = sisusb_ioctl 3089 .unlocked_ioctl = sisusb_ioctl
3091}; 3090};
3092 3091
3093static struct usb_class_driver usb_sisusb_class = { 3092static struct usb_class_driver usb_sisusb_class = {
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index 7f7021ee4189..2db4228fbb01 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -146,7 +146,7 @@ static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, l
146 return retval; 146 return retval;
147} 147}
148 148
149static int lcd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 149static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
150{ 150{
151 struct usb_lcd *dev; 151 struct usb_lcd *dev;
152 u16 bcdDevice; 152 u16 bcdDevice;
@@ -158,12 +158,14 @@ static int lcd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, u
158 158
159 switch (cmd) { 159 switch (cmd) {
160 case IOCTL_GET_HARD_VERSION: 160 case IOCTL_GET_HARD_VERSION:
161 lock_kernel();
161 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice); 162 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
162 sprintf(buf,"%1d%1d.%1d%1d", 163 sprintf(buf,"%1d%1d.%1d%1d",
163 (bcdDevice & 0xF000)>>12, 164 (bcdDevice & 0xF000)>>12,
164 (bcdDevice & 0xF00)>>8, 165 (bcdDevice & 0xF00)>>8,
165 (bcdDevice & 0xF0)>>4, 166 (bcdDevice & 0xF0)>>4,
166 (bcdDevice & 0xF)); 167 (bcdDevice & 0xF));
168 unlock_kernel();
167 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0) 169 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
168 return -EFAULT; 170 return -EFAULT;
169 break; 171 break;
@@ -272,7 +274,7 @@ static const struct file_operations lcd_fops = {
272 .read = lcd_read, 274 .read = lcd_read,
273 .write = lcd_write, 275 .write = lcd_write,
274 .open = lcd_open, 276 .open = lcd_open,
275 .ioctl = lcd_ioctl, 277 .unlocked_ioctl = lcd_ioctl,
276 .release = lcd_release, 278 .release = lcd_release,
277}; 279};
278 280
diff --git a/drivers/usb/mon/mon_stat.c b/drivers/usb/mon/mon_stat.c
index c7a595cd648a..ac8b0d5ce7f8 100644
--- a/drivers/usb/mon/mon_stat.c
+++ b/drivers/usb/mon/mon_stat.c
@@ -9,6 +9,7 @@
9 9
10#include <linux/kernel.h> 10#include <linux/kernel.h>
11#include <linux/usb.h> 11#include <linux/usb.h>
12#include <linux/fs.h>
12#include <asm/uaccess.h> 13#include <asm/uaccess.h>
13 14
14#include "usb_mon.h" 15#include "usb_mon.h"
@@ -42,19 +43,8 @@ static ssize_t mon_stat_read(struct file *file, char __user *buf,
42 size_t nbytes, loff_t *ppos) 43 size_t nbytes, loff_t *ppos)
43{ 44{
44 struct snap *sp = file->private_data; 45 struct snap *sp = file->private_data;
45 loff_t pos = *ppos;
46 int cnt;
47 46
48 if (pos < 0 || pos >= sp->slen) 47 return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
49 return 0;
50 if (nbytes == 0)
51 return 0;
52 if ((cnt = sp->slen - pos) > nbytes)
53 cnt = nbytes;
54 if (copy_to_user(buf, sp->str + pos, cnt))
55 return -EFAULT;
56 *ppos = pos + cnt;
57 return cnt;
58} 48}
59 49
60static int mon_stat_release(struct inode *inode, struct file *file) 50static int mon_stat_release(struct inode *inode, struct file *file)
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index 9a7681b55266..8878c1767fc8 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -64,14 +64,6 @@ config USB_SERIAL_AIRCABLE
64 To compile this driver as a module, choose M here: the module 64 To compile this driver as a module, choose M here: the module
65 will be called aircable. 65 will be called aircable.
66 66
67config USB_SERIAL_AIRPRIME
68 tristate "USB AirPrime CDMA Wireless Driver"
69 help
70 Say Y here if you want to use a AirPrime CDMA Wireless PC card.
71
72 To compile this driver as a module, choose M here: the
73 module will be called airprime.
74
75config USB_SERIAL_ARK3116 67config USB_SERIAL_ARK3116
76 tristate "USB ARK Micro 3116 USB Serial Driver" 68 tristate "USB ARK Micro 3116 USB Serial Driver"
77 help 69 help
diff --git a/drivers/usb/serial/Makefile b/drivers/usb/serial/Makefile
index 17a762ab6769..6047f818adfe 100644
--- a/drivers/usb/serial/Makefile
+++ b/drivers/usb/serial/Makefile
@@ -12,7 +12,6 @@ usbserial-obj-$(CONFIG_USB_EZUSB) += ezusb.o
12usbserial-objs := usb-serial.o generic.o bus.o $(usbserial-obj-y) 12usbserial-objs := usb-serial.o generic.o bus.o $(usbserial-obj-y)
13 13
14obj-$(CONFIG_USB_SERIAL_AIRCABLE) += aircable.o 14obj-$(CONFIG_USB_SERIAL_AIRCABLE) += aircable.o
15obj-$(CONFIG_USB_SERIAL_AIRPRIME) += airprime.o
16obj-$(CONFIG_USB_SERIAL_ARK3116) += ark3116.o 15obj-$(CONFIG_USB_SERIAL_ARK3116) += ark3116.o
17obj-$(CONFIG_USB_SERIAL_BELKIN) += belkin_sa.o 16obj-$(CONFIG_USB_SERIAL_BELKIN) += belkin_sa.o
18obj-$(CONFIG_USB_SERIAL_CH341) += ch341.o 17obj-$(CONFIG_USB_SERIAL_CH341) += ch341.o
diff --git a/drivers/usb/serial/airprime.c b/drivers/usb/serial/airprime.c
deleted file mode 100644
index 0798c14ce787..000000000000
--- a/drivers/usb/serial/airprime.c
+++ /dev/null
@@ -1,353 +0,0 @@
1/*
2 * AirPrime CDMA Wireless Serial USB driver
3 *
4 * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/tty.h>
14#include <linux/tty_flip.h>
15#include <linux/module.h>
16#include <linux/usb.h>
17#include <linux/usb/serial.h>
18
19static struct usb_device_id id_table [] = {
20 { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
21 { },
22};
23MODULE_DEVICE_TABLE(usb, id_table);
24
25#define URB_TRANSFER_BUFFER_SIZE 4096
26#define NUM_READ_URBS 4
27#define NUM_WRITE_URBS 4
28#define NUM_BULK_EPS 3
29#define MAX_BULK_EPS 6
30
31/* if overridden by the user, then use their value for the size of the
32 * read and write urbs, and the number of endpoints */
33static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
34static int endpoints = NUM_BULK_EPS;
35static int debug;
36struct airprime_private {
37 spinlock_t lock;
38 int outstanding_urbs;
39 int throttled;
40 struct urb *read_urbp[NUM_READ_URBS];
41
42 /* Settings for the port */
43 int rts_state; /* Handshaking pins (outputs) */
44 int dtr_state;
45 int cts_state; /* Handshaking pins (inputs) */
46 int dsr_state;
47 int dcd_state;
48 int ri_state;
49};
50
51static int airprime_send_setup(struct usb_serial_port *port)
52{
53 struct usb_serial *serial = port->serial;
54 struct airprime_private *priv;
55
56 dbg("%s", __func__);
57
58 if (port->number != 0)
59 return 0;
60
61 priv = usb_get_serial_port_data(port);
62
63 if (port->tty) {
64 int val = 0;
65 if (priv->dtr_state)
66 val |= 0x01;
67 if (priv->rts_state)
68 val |= 0x02;
69
70 return usb_control_msg(serial->dev,
71 usb_rcvctrlpipe(serial->dev, 0),
72 0x22, 0x21, val, 0, NULL, 0,
73 USB_CTRL_SET_TIMEOUT);
74 }
75
76 return 0;
77}
78
79static void airprime_read_bulk_callback(struct urb *urb)
80{
81 struct usb_serial_port *port = urb->context;
82 unsigned char *data = urb->transfer_buffer;
83 struct tty_struct *tty;
84 int result;
85 int status = urb->status;
86
87 dbg("%s - port %d", __func__, port->number);
88
89 if (status) {
90 dbg("%s - nonzero read bulk status received: %d",
91 __func__, status);
92 return;
93 }
94 usb_serial_debug_data(debug, &port->dev, __func__,
95 urb->actual_length, data);
96
97 tty = port->tty;
98 if (tty && urb->actual_length) {
99 tty_insert_flip_string(tty, data, urb->actual_length);
100 tty_flip_buffer_push(tty);
101 }
102
103 result = usb_submit_urb(urb, GFP_ATOMIC);
104 if (result)
105 dev_err(&port->dev,
106 "%s - failed resubmitting read urb, error %d\n",
107 __func__, result);
108 return;
109}
110
111static void airprime_write_bulk_callback(struct urb *urb)
112{
113 struct usb_serial_port *port = urb->context;
114 struct airprime_private *priv = usb_get_serial_port_data(port);
115 int status = urb->status;
116 unsigned long flags;
117
118 dbg("%s - port %d", __func__, port->number);
119
120 /* free up the transfer buffer, as usb_free_urb() does not do this */
121 kfree(urb->transfer_buffer);
122
123 if (status)
124 dbg("%s - nonzero write bulk status received: %d",
125 __func__, status);
126 spin_lock_irqsave(&priv->lock, flags);
127 --priv->outstanding_urbs;
128 spin_unlock_irqrestore(&priv->lock, flags);
129
130 usb_serial_port_softint(port);
131}
132
133static int airprime_open(struct usb_serial_port *port, struct file *filp)
134{
135 struct airprime_private *priv = usb_get_serial_port_data(port);
136 struct usb_serial *serial = port->serial;
137 struct urb *urb;
138 char *buffer = NULL;
139 int i;
140 int result = 0;
141
142 dbg("%s - port %d", __func__, port->number);
143
144 /* initialize our private data structure if it isn't already created */
145 if (!priv) {
146 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
147 if (!priv) {
148 result = -ENOMEM;
149 goto out;
150 }
151 spin_lock_init(&priv->lock);
152 usb_set_serial_port_data(port, priv);
153 }
154
155 /* Set some sane defaults */
156 priv->rts_state = 1;
157 priv->dtr_state = 1;
158
159 for (i = 0; i < NUM_READ_URBS; ++i) {
160 buffer = kmalloc(buffer_size, GFP_KERNEL);
161 if (!buffer) {
162 dev_err(&port->dev, "%s - out of memory.\n",
163 __func__);
164 result = -ENOMEM;
165 goto errout;
166 }
167 urb = usb_alloc_urb(0, GFP_KERNEL);
168 if (!urb) {
169 kfree(buffer);
170 dev_err(&port->dev, "%s - no more urbs?\n",
171 __func__);
172 result = -ENOMEM;
173 goto errout;
174 }
175 usb_fill_bulk_urb(urb, serial->dev,
176 usb_rcvbulkpipe(serial->dev,
177 port->bulk_out_endpointAddress),
178 buffer, buffer_size,
179 airprime_read_bulk_callback, port);
180 result = usb_submit_urb(urb, GFP_KERNEL);
181 if (result) {
182 usb_free_urb(urb);
183 kfree(buffer);
184 dev_err(&port->dev,
185 "%s - failed submitting read urb %d for port %d, error %d\n",
186 __func__, i, port->number, result);
187 goto errout;
188 }
189 /* remember this urb so we can kill it when the
190 port is closed */
191 priv->read_urbp[i] = urb;
192 }
193
194 airprime_send_setup(port);
195
196 goto out;
197
198 errout:
199 /* some error happened, cancel any submitted urbs and clean up
200 anything that got allocated successfully */
201
202 while (i-- != 0) {
203 urb = priv->read_urbp[i];
204 buffer = urb->transfer_buffer;
205 usb_kill_urb(urb);
206 usb_free_urb(urb);
207 kfree(buffer);
208 }
209
210 out:
211 return result;
212}
213
214static void airprime_close(struct usb_serial_port *port, struct file *filp)
215{
216 struct airprime_private *priv = usb_get_serial_port_data(port);
217 int i;
218
219 dbg("%s - port %d", __func__, port->number);
220
221 priv->rts_state = 0;
222 priv->dtr_state = 0;
223
224 mutex_lock(&port->serial->disc_mutex);
225 if (!port->serial->disconnected)
226 airprime_send_setup(port);
227 mutex_unlock(&port->serial->disc_mutex);
228
229 for (i = 0; i < NUM_READ_URBS; ++i) {
230 usb_kill_urb(priv->read_urbp[i]);
231 kfree(priv->read_urbp[i]->transfer_buffer);
232 usb_free_urb(priv->read_urbp[i]);
233 }
234
235 /* free up private structure */
236 kfree(priv);
237 usb_set_serial_port_data(port, NULL);
238}
239
240static int airprime_write(struct usb_serial_port *port,
241 const unsigned char *buf, int count)
242{
243 struct airprime_private *priv = usb_get_serial_port_data(port);
244 struct usb_serial *serial = port->serial;
245 struct urb *urb;
246 unsigned char *buffer;
247 unsigned long flags;
248 int status;
249 dbg("%s - port %d", __func__, port->number);
250
251 spin_lock_irqsave(&priv->lock, flags);
252 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
253 spin_unlock_irqrestore(&priv->lock, flags);
254 dbg("%s - write limit hit\n", __func__);
255 return 0;
256 }
257 spin_unlock_irqrestore(&priv->lock, flags);
258 buffer = kmalloc(count, GFP_ATOMIC);
259 if (!buffer) {
260 dev_err(&port->dev, "out of memory\n");
261 return -ENOMEM;
262 }
263 urb = usb_alloc_urb(0, GFP_ATOMIC);
264 if (!urb) {
265 dev_err(&port->dev, "no more free urbs\n");
266 kfree(buffer);
267 return -ENOMEM;
268 }
269 memcpy(buffer, buf, count);
270
271 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
272
273 usb_fill_bulk_urb(urb, serial->dev,
274 usb_sndbulkpipe(serial->dev,
275 port->bulk_out_endpointAddress),
276 buffer, count,
277 airprime_write_bulk_callback, port);
278
279 /* send it down the pipe */
280 status = usb_submit_urb(urb, GFP_ATOMIC);
281 if (status) {
282 dev_err(&port->dev,
283 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
284 __func__, status);
285 count = status;
286 kfree(buffer);
287 } else {
288 spin_lock_irqsave(&priv->lock, flags);
289 ++priv->outstanding_urbs;
290 spin_unlock_irqrestore(&priv->lock, flags);
291 }
292 /* we are done with this urb, so let the host driver
293 * really free it when it is finished with it */
294 usb_free_urb(urb);
295 return count;
296}
297
298static struct usb_driver airprime_driver = {
299 .name = "airprime",
300 .probe = usb_serial_probe,
301 .disconnect = usb_serial_disconnect,
302 .id_table = id_table,
303 .no_dynamic_id = 1,
304};
305
306static struct usb_serial_driver airprime_device = {
307 .driver = {
308 .owner = THIS_MODULE,
309 .name = "airprime",
310 },
311 .usb_driver = &airprime_driver,
312 .id_table = id_table,
313 .open = airprime_open,
314 .close = airprime_close,
315 .write = airprime_write,
316};
317
318static int __init airprime_init(void)
319{
320 int retval;
321
322 airprime_device.num_ports = endpoints;
323 if (endpoints < 0 || endpoints >= MAX_BULK_EPS)
324 airprime_device.num_ports = NUM_BULK_EPS;
325
326 retval = usb_serial_register(&airprime_device);
327 if (retval)
328 return retval;
329 retval = usb_register(&airprime_driver);
330 if (retval)
331 usb_serial_deregister(&airprime_device);
332 return retval;
333}
334
335static void __exit airprime_exit(void)
336{
337 dbg("%s", __func__);
338
339 usb_deregister(&airprime_driver);
340 usb_serial_deregister(&airprime_device);
341}
342
343module_init(airprime_init);
344module_exit(airprime_exit);
345MODULE_LICENSE("GPL");
346
347module_param(debug, bool, S_IRUGO | S_IWUSR);
348MODULE_PARM_DESC(debug, "Debug enabled");
349module_param(buffer_size, int, 0);
350MODULE_PARM_DESC(buffer_size,
351 "Size of the transfer buffers in bytes (default 4096)");
352module_param(endpoints, int, 0);
353MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");
diff --git a/drivers/usb/serial/cp2101.c b/drivers/usb/serial/cp2101.c
index f5b57b196c5a..2bc5576c443a 100644
--- a/drivers/usb/serial/cp2101.c
+++ b/drivers/usb/serial/cp2101.c
@@ -365,8 +365,8 @@ static void cp2101_close (struct usb_serial_port *port, struct file * filp)
365static void cp2101_get_termios (struct usb_serial_port *port) 365static void cp2101_get_termios (struct usb_serial_port *port)
366{ 366{
367 unsigned int cflag, modem_ctl[4]; 367 unsigned int cflag, modem_ctl[4];
368 int baud; 368 unsigned int baud;
369 int bits; 369 unsigned int bits;
370 370
371 dbg("%s - port %d", __func__, port->number); 371 dbg("%s - port %d", __func__, port->number);
372 372
@@ -498,7 +498,7 @@ static void cp2101_set_termios (struct usb_serial_port *port,
498 struct ktermios *old_termios) 498 struct ktermios *old_termios)
499{ 499{
500 unsigned int cflag, old_cflag; 500 unsigned int cflag, old_cflag;
501 int baud=0, bits; 501 unsigned int baud = 0, bits;
502 unsigned int modem_ctl[4]; 502 unsigned int modem_ctl[4];
503 503
504 dbg("%s - port %d", __func__, port->number); 504 dbg("%s - port %d", __func__, port->number);
@@ -654,7 +654,7 @@ static void cp2101_set_termios (struct usb_serial_port *port,
654static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file, 654static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
655 unsigned int set, unsigned int clear) 655 unsigned int set, unsigned int clear)
656{ 656{
657 int control = 0; 657 unsigned int control = 0;
658 658
659 dbg("%s - port %d", __func__, port->number); 659 dbg("%s - port %d", __func__, port->number);
660 660
@@ -683,7 +683,8 @@ static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
683 683
684static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file) 684static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
685{ 685{
686 int control, result; 686 unsigned int control;
687 int result;
687 688
688 dbg("%s - port %d", __func__, port->number); 689 dbg("%s - port %d", __func__, port->number);
689 690
@@ -703,7 +704,7 @@ static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
703 704
704static void cp2101_break_ctl (struct usb_serial_port *port, int break_state) 705static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
705{ 706{
706 int state; 707 unsigned int state;
707 708
708 dbg("%s - port %d", __func__, port->number); 709 dbg("%s - port %d", __func__, port->number);
709 if (break_state == 0) 710 if (break_state == 0)
diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c
index 04a56f300ea6..28bc6fcf44f0 100644
--- a/drivers/usb/serial/digi_acceleport.c
+++ b/drivers/usb/serial/digi_acceleport.c
@@ -229,8 +229,6 @@
229* in case a wake up is lost. 229* in case a wake up is lost.
230* - Following Documentation/DocBook/kernel-locking.pdf no spin locks 230* - Following Documentation/DocBook/kernel-locking.pdf no spin locks
231* are held when calling copy_to/from_user or printk. 231* are held when calling copy_to/from_user or printk.
232*
233* $Id: digi_acceleport.c,v 1.80.1.2 2000/11/02 05:45:08 root Exp $
234*/ 232*/
235 233
236#include <linux/kernel.h> 234#include <linux/kernel.h>
@@ -573,6 +571,7 @@ static struct usb_serial_driver digi_acceleport_4_device = {
573static long cond_wait_interruptible_timeout_irqrestore( 571static long cond_wait_interruptible_timeout_irqrestore(
574 wait_queue_head_t *q, long timeout, 572 wait_queue_head_t *q, long timeout,
575 spinlock_t *lock, unsigned long flags) 573 spinlock_t *lock, unsigned long flags)
574__releases(lock)
576{ 575{
577 DEFINE_WAIT(wait); 576 DEFINE_WAIT(wait);
578 577
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c
index 61daea3f7b2d..a58822a14a87 100644
--- a/drivers/usb/serial/io_ti.c
+++ b/drivers/usb/serial/io_ti.c
@@ -18,8 +18,8 @@
18 * 18 *
19 * Version history: 19 * Version history:
20 * 20 *
21 * July 11, 2002 Removed 4 port device structure since all TI UMP 21 * July 11, 2002 Removed 4 port device structure since all TI UMP
22 * chips have only 2 ports 22 * chips have only 2 ports
23 * David Iacovelli (davidi@ionetworks.com) 23 * David Iacovelli (davidi@ionetworks.com)
24 * 24 *
25 */ 25 */
@@ -38,7 +38,7 @@
38#include <linux/serial.h> 38#include <linux/serial.h>
39#include <linux/ioctl.h> 39#include <linux/ioctl.h>
40#include <linux/firmware.h> 40#include <linux/firmware.h>
41#include <asm/uaccess.h> 41#include <linux/uaccess.h>
42#include <linux/usb.h> 42#include <linux/usb.h>
43#include <linux/usb/serial.h> 43#include <linux/usb/serial.h>
44 44
@@ -57,18 +57,19 @@
57 57
58 58
59struct edgeport_uart_buf_desc { 59struct edgeport_uart_buf_desc {
60 __u32 count; // Number of bytes currently in buffer 60 __u32 count; /* Number of bytes currently in buffer */
61}; 61};
62 62
63/* different hardware types */ 63/* different hardware types */
64#define HARDWARE_TYPE_930 0 64#define HARDWARE_TYPE_930 0
65#define HARDWARE_TYPE_TIUMP 1 65#define HARDWARE_TYPE_TIUMP 1
66 66
67// IOCTL_PRIVATE_TI_GET_MODE Definitions 67/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
68#define TI_MODE_CONFIGURING 0 // Device has not entered start device 68#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
69#define TI_MODE_BOOT 1 // Staying in boot mode 69#define TI_MODE_BOOT 1 /* Staying in boot mode */
70#define TI_MODE_DOWNLOAD 2 // Made it to download mode 70#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
71#define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode 71#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
72 transitioning to download mode */
72 73
73/* read urb state */ 74/* read urb state */
74#define EDGE_READ_URB_RUNNING 0 75#define EDGE_READ_URB_RUNNING 0
@@ -82,10 +83,9 @@ struct edgeport_uart_buf_desc {
82 83
83 84
84/* Product information read from the Edgeport */ 85/* Product information read from the Edgeport */
85struct product_info 86struct product_info {
86{ 87 int TiMode; /* Current TI Mode */
87 int TiMode; // Current TI Mode 88 __u8 hardware_type; /* Type of hardware */
88 __u8 hardware_type; // Type of hardware
89} __attribute__((packed)); 89} __attribute__((packed));
90 90
91/* circular buffer */ 91/* circular buffer */
@@ -116,7 +116,7 @@ struct edgeport_port {
116 happen */ 116 happen */
117 struct edgeport_serial *edge_serial; 117 struct edgeport_serial *edge_serial;
118 struct usb_serial_port *port; 118 struct usb_serial_port *port;
119 __u8 bUartMode; /* Port type, 0: RS232, etc. */ 119 __u8 bUartMode; /* Port type, 0: RS232, etc. */
120 spinlock_t ep_lock; 120 spinlock_t ep_lock;
121 int ep_read_urb_state; 121 int ep_read_urb_state;
122 int ep_write_urb_in_use; 122 int ep_write_urb_in_use;
@@ -125,8 +125,9 @@ struct edgeport_port {
125 125
126struct edgeport_serial { 126struct edgeport_serial {
127 struct product_info product_info; 127 struct product_info product_info;
128 u8 TI_I2C_Type; // Type of I2C in UMP 128 u8 TI_I2C_Type; /* Type of I2C in UMP */
129 u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode 129 u8 TiReadI2C; /* Set to TRUE if we have read the
130 I2c in Boot Mode */
130 struct mutex es_lock; 131 struct mutex es_lock;
131 int num_ports_open; 132 int num_ports_open;
132 struct usb_serial *serial; 133 struct usb_serial *serial;
@@ -214,7 +215,7 @@ static struct usb_device_id id_table_combined [] = {
214 { } 215 { }
215}; 216};
216 217
217MODULE_DEVICE_TABLE (usb, id_table_combined); 218MODULE_DEVICE_TABLE(usb, id_table_combined);
218 219
219static struct usb_driver io_driver = { 220static struct usb_driver io_driver = {
220 .name = "io_ti", 221 .name = "io_ti",
@@ -231,19 +232,19 @@ static unsigned short OperationalBuildNumber;
231 232
232static int debug; 233static int debug;
233 234
234static int TIStayInBootMode = 0;
235static int low_latency = EDGE_LOW_LATENCY; 235static int low_latency = EDGE_LOW_LATENCY;
236static int closing_wait = EDGE_CLOSING_WAIT; 236static int closing_wait = EDGE_CLOSING_WAIT;
237static int ignore_cpu_rev = 0; 237static int ignore_cpu_rev;
238static int default_uart_mode = 0; /* RS232 */ 238static int default_uart_mode; /* RS232 */
239
240 239
241static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length); 240static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241 unsigned char *data, int length);
242 242
243static void stop_read(struct edgeport_port *edge_port); 243static void stop_read(struct edgeport_port *edge_port);
244static int restart_read(struct edgeport_port *edge_port); 244static int restart_read(struct edgeport_port *edge_port);
245 245
246static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios); 246static void edge_set_termios(struct usb_serial_port *port,
247 struct ktermios *old_termios);
247static void edge_send(struct usb_serial_port *port); 248static void edge_send(struct usb_serial_port *port);
248 249
249/* sysfs attributes */ 250/* sysfs attributes */
@@ -262,87 +263,57 @@ static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
262 unsigned int count); 263 unsigned int count);
263 264
264 265
265static int TIReadVendorRequestSync (struct usb_device *dev, 266static int ti_vread_sync(struct usb_device *dev, __u8 request,
266 __u8 request, 267 __u16 value, __u16 index, u8 *data, int size)
267 __u16 value,
268 __u16 index,
269 u8 *data,
270 int size)
271{ 268{
272 int status; 269 int status;
273 270
274 status = usb_control_msg (dev, 271 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
275 usb_rcvctrlpipe(dev, 0), 272 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
276 request, 273 value, index, data, size, 1000);
277 (USB_TYPE_VENDOR |
278 USB_RECIP_DEVICE |
279 USB_DIR_IN),
280 value,
281 index,
282 data,
283 size,
284 1000);
285 if (status < 0) 274 if (status < 0)
286 return status; 275 return status;
287 if (status != size) { 276 if (status != size) {
288 dbg ("%s - wanted to write %d, but only wrote %d", 277 dbg("%s - wanted to write %d, but only wrote %d",
289 __func__, size, status); 278 __func__, size, status);
290 return -ECOMM; 279 return -ECOMM;
291 } 280 }
292 return 0; 281 return 0;
293} 282}
294 283
295static int TISendVendorRequestSync (struct usb_device *dev, 284static int ti_vsend_sync(struct usb_device *dev, __u8 request,
296 __u8 request, 285 __u16 value, __u16 index, u8 *data, int size)
297 __u16 value,
298 __u16 index,
299 u8 *data,
300 int size)
301{ 286{
302 int status; 287 int status;
303 288
304 status = usb_control_msg (dev, 289 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
305 usb_sndctrlpipe(dev, 0), 290 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
306 request, 291 value, index, data, size, 1000);
307 (USB_TYPE_VENDOR |
308 USB_RECIP_DEVICE |
309 USB_DIR_OUT),
310 value,
311 index,
312 data,
313 size,
314 1000);
315 if (status < 0) 292 if (status < 0)
316 return status; 293 return status;
317 if (status != size) { 294 if (status != size) {
318 dbg ("%s - wanted to write %d, but only wrote %d", 295 dbg("%s - wanted to write %d, but only wrote %d",
319 __func__, size, status); 296 __func__, size, status);
320 return -ECOMM; 297 return -ECOMM;
321 } 298 }
322 return 0; 299 return 0;
323} 300}
324 301
325static int TIWriteCommandSync (struct usb_device *dev, __u8 command, 302static int send_cmd(struct usb_device *dev, __u8 command,
326 __u8 moduleid, __u16 value, u8 *data, 303 __u8 moduleid, __u16 value, u8 *data,
327 int size) 304 int size)
328{ 305{
329 return TISendVendorRequestSync (dev, 306 return ti_vsend_sync(dev, command, value, moduleid, data, size);
330 command, // Request
331 value, // wValue
332 moduleid, // wIndex
333 data, // TransferBuffer
334 size); // TransferBufferLength
335
336} 307}
337 308
338/* clear tx/rx buffers and fifo in TI UMP */ 309/* clear tx/rx buffers and fifo in TI UMP */
339static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask) 310static int purge_port(struct usb_serial_port *port, __u16 mask)
340{ 311{
341 int port_number = port->number - port->serial->minor; 312 int port_number = port->number - port->serial->minor;
342 313
343 dbg ("%s - port %d, mask %x", __func__, port_number, mask); 314 dbg("%s - port %d, mask %x", __func__, port_number, mask);
344 315
345 return TIWriteCommandSync (port->serial->dev, 316 return send_cmd(port->serial->dev,
346 UMPC_PURGE_PORT, 317 UMPC_PURGE_PORT,
347 (__u8)(UMPM_UART1_PORT + port_number), 318 (__u8)(UMPM_UART1_PORT + port_number),
348 mask, 319 mask,
@@ -351,92 +322,87 @@ static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
351} 322}
352 323
353/** 324/**
354 * TIReadDownloadMemory - Read edgeport memory from TI chip 325 * read_download_mem - Read edgeport memory from TI chip
355 * @dev: usb device pointer 326 * @dev: usb device pointer
356 * @start_address: Device CPU address at which to read 327 * @start_address: Device CPU address at which to read
357 * @length: Length of above data 328 * @length: Length of above data
358 * @address_type: Can read both XDATA and I2C 329 * @address_type: Can read both XDATA and I2C
359 * @buffer: pointer to input data buffer 330 * @buffer: pointer to input data buffer
360 */ 331 */
361static int TIReadDownloadMemory(struct usb_device *dev, int start_address, 332static int read_download_mem(struct usb_device *dev, int start_address,
362 int length, __u8 address_type, __u8 *buffer) 333 int length, __u8 address_type, __u8 *buffer)
363{ 334{
364 int status = 0; 335 int status = 0;
365 __u8 read_length; 336 __u8 read_length;
366 __be16 be_start_address; 337 __be16 be_start_address;
367 338
368 dbg ("%s - @ %x for %d", __func__, start_address, length); 339 dbg("%s - @ %x for %d", __func__, start_address, length);
369 340
370 /* Read in blocks of 64 bytes 341 /* Read in blocks of 64 bytes
371 * (TI firmware can't handle more than 64 byte reads) 342 * (TI firmware can't handle more than 64 byte reads)
372 */ 343 */
373 while (length) { 344 while (length) {
374 if (length > 64) 345 if (length > 64)
375 read_length= 64; 346 read_length = 64;
376 else 347 else
377 read_length = (__u8)length; 348 read_length = (__u8)length;
378 349
379 if (read_length > 1) { 350 if (read_length > 1) {
380 dbg ("%s - @ %x for %d", __func__, 351 dbg("%s - @ %x for %d", __func__,
381 start_address, read_length); 352 start_address, read_length);
382 } 353 }
383 be_start_address = cpu_to_be16 (start_address); 354 be_start_address = cpu_to_be16(start_address);
384 status = TIReadVendorRequestSync (dev, 355 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
385 UMPC_MEMORY_READ, // Request 356 (__u16)address_type,
386 (__u16)address_type, // wValue (Address type) 357 (__force __u16)be_start_address,
387 (__force __u16)be_start_address, // wIndex (Address to read) 358 buffer, read_length);
388 buffer, // TransferBuffer
389 read_length); // TransferBufferLength
390 359
391 if (status) { 360 if (status) {
392 dbg ("%s - ERROR %x", __func__, status); 361 dbg("%s - ERROR %x", __func__, status);
393 return status; 362 return status;
394 } 363 }
395 364
396 if (read_length > 1) { 365 if (read_length > 1)
397 usb_serial_debug_data(debug, &dev->dev, __func__, 366 usb_serial_debug_data(debug, &dev->dev, __func__,
398 read_length, buffer); 367 read_length, buffer);
399 }
400 368
401 /* Update pointers/length */ 369 /* Update pointers/length */
402 start_address += read_length; 370 start_address += read_length;
403 buffer += read_length; 371 buffer += read_length;
404 length -= read_length; 372 length -= read_length;
405 } 373 }
406 374
407 return status; 375 return status;
408} 376}
409 377
410static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer) 378static int read_ram(struct usb_device *dev, int start_address,
379 int length, __u8 *buffer)
411{ 380{
412 return TIReadDownloadMemory (dev, 381 return read_download_mem(dev, start_address, length,
413 start_address, 382 DTK_ADDR_SPACE_XDATA, buffer);
414 length,
415 DTK_ADDR_SPACE_XDATA,
416 buffer);
417} 383}
418 384
419/* Read edgeport memory to a given block */ 385/* Read edgeport memory to a given block */
420static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer) 386static int read_boot_mem(struct edgeport_serial *serial,
387 int start_address, int length, __u8 *buffer)
421{ 388{
422 int status = 0; 389 int status = 0;
423 int i; 390 int i;
424 391
425 for (i=0; i< length; i++) { 392 for (i = 0; i < length; i++) {
426 status = TIReadVendorRequestSync (serial->serial->dev, 393 status = ti_vread_sync(serial->serial->dev,
427 UMPC_MEMORY_READ, // Request 394 UMPC_MEMORY_READ, serial->TI_I2C_Type,
428 serial->TI_I2C_Type, // wValue (Address type) 395 (__u16)(start_address+i), &buffer[i], 0x01);
429 (__u16)(start_address+i), // wIndex
430 &buffer[i], // TransferBuffer
431 0x01); // TransferBufferLength
432 if (status) { 396 if (status) {
433 dbg ("%s - ERROR %x", __func__, status); 397 dbg("%s - ERROR %x", __func__, status);
434 return status; 398 return status;
435 } 399 }
436 } 400 }
437 401
438 dbg ("%s - start_address = %x, length = %d", __func__, start_address, length); 402 dbg("%s - start_address = %x, length = %d",
439 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer); 403 __func__, start_address, length);
404 usb_serial_debug_data(debug, &serial->serial->dev->dev,
405 __func__, length, buffer);
440 406
441 serial->TiReadI2C = 1; 407 serial->TiReadI2C = 1;
442 408
@@ -444,7 +410,8 @@ static int TIReadBootMemory (struct edgeport_serial *serial, int start_address,
444} 410}
445 411
446/* Write given block to TI EPROM memory */ 412/* Write given block to TI EPROM memory */
447static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) 413static int write_boot_mem(struct edgeport_serial *serial,
414 int start_address, int length, __u8 *buffer)
448{ 415{
449 int status = 0; 416 int status = 0;
450 int i; 417 int i;
@@ -452,57 +419,58 @@ static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address,
452 419
453 /* Must do a read before write */ 420 /* Must do a read before write */
454 if (!serial->TiReadI2C) { 421 if (!serial->TiReadI2C) {
455 status = TIReadBootMemory(serial, 0, 1, &temp); 422 status = read_boot_mem(serial, 0, 1, &temp);
456 if (status) 423 if (status)
457 return status; 424 return status;
458 } 425 }
459 426
460 for (i=0; i < length; ++i) { 427 for (i = 0; i < length; ++i) {
461 status = TISendVendorRequestSync (serial->serial->dev, 428 status = ti_vsend_sync(serial->serial->dev,
462 UMPC_MEMORY_WRITE, // Request 429 UMPC_MEMORY_WRITE, buffer[i],
463 buffer[i], // wValue 430 (__u16)(i + start_address), NULL, 0);
464 (__u16)(i+start_address), // wIndex
465 NULL, // TransferBuffer
466 0); // TransferBufferLength
467 if (status) 431 if (status)
468 return status; 432 return status;
469 } 433 }
470 434
471 dbg ("%s - start_sddr = %x, length = %d", __func__, start_address, length); 435 dbg("%s - start_sddr = %x, length = %d",
472 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer); 436 __func__, start_address, length);
437 usb_serial_debug_data(debug, &serial->serial->dev->dev,
438 __func__, length, buffer);
473 439
474 return status; 440 return status;
475} 441}
476 442
477 443
478/* Write edgeport I2C memory to TI chip */ 444/* Write edgeport I2C memory to TI chip */
479static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer) 445static int write_i2c_mem(struct edgeport_serial *serial,
446 int start_address, int length, __u8 address_type, __u8 *buffer)
480{ 447{
481 int status = 0; 448 int status = 0;
482 int write_length; 449 int write_length;
483 __be16 be_start_address; 450 __be16 be_start_address;
484 451
485 /* We can only send a maximum of 1 aligned byte page at a time */ 452 /* We can only send a maximum of 1 aligned byte page at a time */
486 453
487 /* calulate the number of bytes left in the first page */ 454 /* calulate the number of bytes left in the first page */
488 write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1)); 455 write_length = EPROM_PAGE_SIZE -
456 (start_address & (EPROM_PAGE_SIZE - 1));
489 457
490 if (write_length > length) 458 if (write_length > length)
491 write_length = length; 459 write_length = length;
492 460
493 dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __func__, start_address, write_length); 461 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
494 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer); 462 __func__, start_address, write_length);
463 usb_serial_debug_data(debug, &serial->serial->dev->dev,
464 __func__, write_length, buffer);
495 465
496 /* Write first page */ 466 /* Write first page */
497 be_start_address = cpu_to_be16 (start_address); 467 be_start_address = cpu_to_be16(start_address);
498 status = TISendVendorRequestSync (serial->serial->dev, 468 status = ti_vsend_sync(serial->serial->dev,
499 UMPC_MEMORY_WRITE, // Request 469 UMPC_MEMORY_WRITE, (__u16)address_type,
500 (__u16)address_type, // wValue 470 (__force __u16)be_start_address,
501 (__force __u16)be_start_address, // wIndex 471 buffer, write_length);
502 buffer, // TransferBuffer
503 write_length);
504 if (status) { 472 if (status) {
505 dbg ("%s - ERROR %d", __func__, status); 473 dbg("%s - ERROR %d", __func__, status);
506 return status; 474 return status;
507 } 475 }
508 476
@@ -510,29 +478,31 @@ static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address
510 start_address += write_length; 478 start_address += write_length;
511 buffer += write_length; 479 buffer += write_length;
512 480
513 /* We should be aligned now -- can write max page size bytes at a time */ 481 /* We should be aligned now -- can write
482 max page size bytes at a time */
514 while (length) { 483 while (length) {
515 if (length > EPROM_PAGE_SIZE) 484 if (length > EPROM_PAGE_SIZE)
516 write_length = EPROM_PAGE_SIZE; 485 write_length = EPROM_PAGE_SIZE;
517 else 486 else
518 write_length = length; 487 write_length = length;
519 488
520 dbg ("%s - Page Write Addr = %x, length = %d", __func__, start_address, write_length); 489 dbg("%s - Page Write Addr = %x, length = %d",
521 usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer); 490 __func__, start_address, write_length);
491 usb_serial_debug_data(debug, &serial->serial->dev->dev,
492 __func__, write_length, buffer);
522 493
523 /* Write next page */ 494 /* Write next page */
524 be_start_address = cpu_to_be16 (start_address); 495 be_start_address = cpu_to_be16(start_address);
525 status = TISendVendorRequestSync (serial->serial->dev, 496 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
526 UMPC_MEMORY_WRITE, // Request 497 (__u16)address_type,
527 (__u16)address_type, // wValue 498 (__force __u16)be_start_address,
528 (__force __u16)be_start_address, // wIndex 499 buffer, write_length);
529 buffer, // TransferBuffer
530 write_length); // TransferBufferLength
531 if (status) { 500 if (status) {
532 dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __func__, status); 501 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
502 __func__, status);
533 return status; 503 return status;
534 } 504 }
535 505
536 length -= write_length; 506 length -= write_length;
537 start_address += write_length; 507 start_address += write_length;
538 buffer += write_length; 508 buffer += write_length;
@@ -541,25 +511,25 @@ static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address
541} 511}
542 512
543/* Examine the UMP DMA registers and LSR 513/* Examine the UMP DMA registers and LSR
544 * 514 *
545 * Check the MSBit of the X and Y DMA byte count registers. 515 * Check the MSBit of the X and Y DMA byte count registers.
546 * A zero in this bit indicates that the TX DMA buffers are empty 516 * A zero in this bit indicates that the TX DMA buffers are empty
547 * then check the TX Empty bit in the UART. 517 * then check the TX Empty bit in the UART.
548 */ 518 */
549static int TIIsTxActive (struct edgeport_port *port) 519static int tx_active(struct edgeport_port *port)
550{ 520{
551 int status; 521 int status;
552 struct out_endpoint_desc_block *oedb; 522 struct out_endpoint_desc_block *oedb;
553 __u8 *lsr; 523 __u8 *lsr;
554 int bytes_left = 0; 524 int bytes_left = 0;
555 525
556 oedb = kmalloc (sizeof (* oedb), GFP_KERNEL); 526 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
557 if (!oedb) { 527 if (!oedb) {
558 dev_err (&port->port->dev, "%s - out of memory\n", __func__); 528 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
559 return -ENOMEM; 529 return -ENOMEM;
560 } 530 }
561 531
562 lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte, 532 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
563 as not all platforms can do DMA 533 as not all platforms can do DMA
564 from stack */ 534 from stack */
565 if (!lsr) { 535 if (!lsr) {
@@ -567,43 +537,39 @@ static int TIIsTxActive (struct edgeport_port *port)
567 return -ENOMEM; 537 return -ENOMEM;
568 } 538 }
569 /* Read the DMA Count Registers */ 539 /* Read the DMA Count Registers */
570 status = TIReadRam (port->port->serial->dev, 540 status = read_ram(port->port->serial->dev, port->dma_address,
571 port->dma_address, 541 sizeof(*oedb), (void *)oedb);
572 sizeof( *oedb),
573 (void *)oedb);
574
575 if (status) 542 if (status)
576 goto exit_is_tx_active; 543 goto exit_is_tx_active;
577 544
578 dbg ("%s - XByteCount 0x%X", __func__, oedb->XByteCount); 545 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
579 546
580 /* and the LSR */ 547 /* and the LSR */
581 status = TIReadRam (port->port->serial->dev, 548 status = read_ram(port->port->serial->dev,
582 port->uart_base + UMPMEM_OFFS_UART_LSR, 549 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
583 1,
584 lsr);
585 550
586 if (status) 551 if (status)
587 goto exit_is_tx_active; 552 goto exit_is_tx_active;
588 dbg ("%s - LSR = 0x%X", __func__, *lsr); 553 dbg("%s - LSR = 0x%X", __func__, *lsr);
589 554
590 /* If either buffer has data or we are transmitting then return TRUE */ 555 /* If either buffer has data or we are transmitting then return TRUE */
591 if ((oedb->XByteCount & 0x80 ) != 0 ) 556 if ((oedb->XByteCount & 0x80) != 0)
592 bytes_left += 64; 557 bytes_left += 64;
593 558
594 if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 ) 559 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
595 bytes_left += 1; 560 bytes_left += 1;
596 561
597 /* We return Not Active if we get any kind of error */ 562 /* We return Not Active if we get any kind of error */
598exit_is_tx_active: 563exit_is_tx_active:
599 dbg ("%s - return %d", __func__, bytes_left ); 564 dbg("%s - return %d", __func__, bytes_left);
600 565
601 kfree(lsr); 566 kfree(lsr);
602 kfree(oedb); 567 kfree(oedb);
603 return bytes_left; 568 return bytes_left;
604} 569}
605 570
606static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush) 571static void chase_port(struct edgeport_port *port, unsigned long timeout,
572 int flush)
607{ 573{
608 int baud_rate; 574 int baud_rate;
609 struct tty_struct *tty = port->port->tty; 575 struct tty_struct *tty = port->port->tty;
@@ -611,7 +577,7 @@ static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int f
611 unsigned long flags; 577 unsigned long flags;
612 578
613 if (!timeout) 579 if (!timeout)
614 timeout = (HZ*EDGE_CLOSING_WAIT)/100; 580 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
615 581
616 /* wait for data to drain from the buffer */ 582 /* wait for data to drain from the buffer */
617 spin_lock_irqsave(&port->ep_lock, flags); 583 spin_lock_irqsave(&port->ep_lock, flags);
@@ -621,7 +587,8 @@ static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int f
621 set_current_state(TASK_INTERRUPTIBLE); 587 set_current_state(TASK_INTERRUPTIBLE);
622 if (edge_buf_data_avail(port->ep_out_buf) == 0 588 if (edge_buf_data_avail(port->ep_out_buf) == 0
623 || timeout == 0 || signal_pending(current) 589 || timeout == 0 || signal_pending(current)
624 || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */ 590 || !usb_get_intfdata(port->port->serial->interface))
591 /* disconnect */
625 break; 592 break;
626 spin_unlock_irqrestore(&port->ep_lock, flags); 593 spin_unlock_irqrestore(&port->ep_lock, flags);
627 timeout = schedule_timeout(timeout); 594 timeout = schedule_timeout(timeout);
@@ -636,8 +603,9 @@ static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int f
636 /* wait for data to drain from the device */ 603 /* wait for data to drain from the device */
637 timeout += jiffies; 604 timeout += jiffies;
638 while ((long)(jiffies - timeout) < 0 && !signal_pending(current) 605 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
639 && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */ 606 && usb_get_intfdata(port->port->serial->interface)) {
640 if (!TIIsTxActive(port)) 607 /* not disconnected */
608 if (!tx_active(port))
641 break; 609 break;
642 msleep(10); 610 msleep(10);
643 } 611 }
@@ -647,72 +615,72 @@ static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int f
647 return; 615 return;
648 616
649 /* wait one more character time, based on baud rate */ 617 /* wait one more character time, based on baud rate */
650 /* (TIIsTxActive doesn't seem to wait for the last byte) */ 618 /* (tx_active doesn't seem to wait for the last byte) */
651 if ((baud_rate=port->baud_rate) == 0) 619 baud_rate = port->baud_rate;
620 if (baud_rate == 0)
652 baud_rate = 50; 621 baud_rate = 50;
653 msleep(max(1, DIV_ROUND_UP(10000, baud_rate))); 622 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
654} 623}
655 624
656static int TIChooseConfiguration (struct usb_device *dev) 625static int choose_config(struct usb_device *dev)
657{ 626{
658 // There may be multiple configurations on this device, in which case 627 /*
659 // we would need to read and parse all of them to find out which one 628 * There may be multiple configurations on this device, in which case
660 // we want. However, we just support one config at this point, 629 * we would need to read and parse all of them to find out which one
661 // configuration # 1, which is Config Descriptor 0. 630 * we want. However, we just support one config at this point,
631 * configuration # 1, which is Config Descriptor 0.
632 */
662 633
663 dbg ("%s - Number of Interfaces = %d", __func__, dev->config->desc.bNumInterfaces); 634 dbg("%s - Number of Interfaces = %d",
664 dbg ("%s - MAX Power = %d", __func__, dev->config->desc.bMaxPower*2); 635 __func__, dev->config->desc.bNumInterfaces);
636 dbg("%s - MAX Power = %d",
637 __func__, dev->config->desc.bMaxPower * 2);
665 638
666 if (dev->config->desc.bNumInterfaces != 1) { 639 if (dev->config->desc.bNumInterfaces != 1) {
667 dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__); 640 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
641 __func__);
668 return -ENODEV; 642 return -ENODEV;
669 } 643 }
670 644
671 return 0; 645 return 0;
672} 646}
673 647
674static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) 648static int read_rom(struct edgeport_serial *serial,
649 int start_address, int length, __u8 *buffer)
675{ 650{
676 int status; 651 int status;
677 652
678 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) { 653 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
679 status = TIReadDownloadMemory (serial->serial->dev, 654 status = read_download_mem(serial->serial->dev,
680 start_address, 655 start_address,
681 length, 656 length,
682 serial->TI_I2C_Type, 657 serial->TI_I2C_Type,
683 buffer); 658 buffer);
684 } else { 659 } else {
685 status = TIReadBootMemory (serial, 660 status = read_boot_mem(serial, start_address, length,
686 start_address, 661 buffer);
687 length,
688 buffer);
689 } 662 }
690
691 return status; 663 return status;
692} 664}
693 665
694static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer) 666static int write_rom(struct edgeport_serial *serial, int start_address,
667 int length, __u8 *buffer)
695{ 668{
696 if (serial->product_info.TiMode == TI_MODE_BOOT) 669 if (serial->product_info.TiMode == TI_MODE_BOOT)
697 return TIWriteBootMemory (serial, 670 return write_boot_mem(serial, start_address, length,
698 start_address, 671 buffer);
699 length,
700 buffer);
701 672
702 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) 673 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
703 return TIWriteDownloadI2C (serial, 674 return write_i2c_mem(serial, start_address, length,
704 start_address, 675 serial->TI_I2C_Type, buffer);
705 length,
706 serial->TI_I2C_Type,
707 buffer);
708
709 return -EINVAL; 676 return -EINVAL;
710} 677}
711 678
712 679
713 680
714/* Read a descriptor header from I2C based on type */ 681/* Read a descriptor header from I2C based on type */
715static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc) 682static int get_descriptor_addr(struct edgeport_serial *serial,
683 int desc_type, struct ti_i2c_desc *rom_desc)
716{ 684{
717 int start_address; 685 int start_address;
718 int status; 686 int status;
@@ -720,41 +688,42 @@ static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type
720 /* Search for requested descriptor in I2C */ 688 /* Search for requested descriptor in I2C */
721 start_address = 2; 689 start_address = 2;
722 do { 690 do {
723 status = TIReadRom (serial, 691 status = read_rom(serial,
724 start_address, 692 start_address,
725 sizeof(struct ti_i2c_desc), 693 sizeof(struct ti_i2c_desc),
726 (__u8 *)rom_desc ); 694 (__u8 *)rom_desc);
727 if (status) 695 if (status)
728 return 0; 696 return 0;
729 697
730 if (rom_desc->Type == desc_type) 698 if (rom_desc->Type == desc_type)
731 return start_address; 699 return start_address;
732 700
733 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size; 701 start_address = start_address + sizeof(struct ti_i2c_desc)
702 + rom_desc->Size;
734 703
735 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type); 704 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
736 705
737 return 0; 706 return 0;
738} 707}
739 708
740/* Validate descriptor checksum */ 709/* Validate descriptor checksum */
741static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer) 710static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
742{ 711{
743 __u16 i; 712 __u16 i;
744 __u8 cs = 0; 713 __u8 cs = 0;
745 714
746 for (i=0; i < rom_desc->Size; i++) { 715 for (i = 0; i < rom_desc->Size; i++)
747 cs = (__u8)(cs + buffer[i]); 716 cs = (__u8)(cs + buffer[i]);
748 } 717
749 if (cs != rom_desc->CheckSum) { 718 if (cs != rom_desc->CheckSum) {
750 dbg ("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs); 719 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
751 return -EINVAL; 720 return -EINVAL;
752 } 721 }
753 return 0; 722 return 0;
754} 723}
755 724
756/* Make sure that the I2C image is good */ 725/* Make sure that the I2C image is good */
757static int TiValidateI2cImage (struct edgeport_serial *serial) 726static int check_i2c_image(struct edgeport_serial *serial)
758{ 727{
759 struct device *dev = &serial->serial->dev->dev; 728 struct device *dev = &serial->serial->dev->dev;
760 int status = 0; 729 int status = 0;
@@ -763,120 +732,124 @@ static int TiValidateI2cImage (struct edgeport_serial *serial)
763 __u8 *buffer; 732 __u8 *buffer;
764 __u16 ttype; 733 __u16 ttype;
765 734
766 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); 735 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
767 if (!rom_desc) { 736 if (!rom_desc) {
768 dev_err (dev, "%s - out of memory\n", __func__); 737 dev_err(dev, "%s - out of memory\n", __func__);
769 return -ENOMEM; 738 return -ENOMEM;
770 } 739 }
771 buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL); 740 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
772 if (!buffer) { 741 if (!buffer) {
773 dev_err (dev, "%s - out of memory when allocating buffer\n", __func__); 742 dev_err(dev, "%s - out of memory when allocating buffer\n",
774 kfree (rom_desc); 743 __func__);
744 kfree(rom_desc);
775 return -ENOMEM; 745 return -ENOMEM;
776 } 746 }
777 747
778 // Read the first byte (Signature0) must be 0x52 or 0x10 748 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
779 status = TIReadRom (serial, 0, 1, buffer); 749 status = read_rom(serial, 0, 1, buffer);
780 if (status) 750 if (status)
781 goto ExitTiValidateI2cImage; 751 goto out;
782 752
783 if (*buffer != UMP5152 && *buffer != UMP3410) { 753 if (*buffer != UMP5152 && *buffer != UMP3410) {
784 dev_err (dev, "%s - invalid buffer signature\n", __func__); 754 dev_err(dev, "%s - invalid buffer signature\n", __func__);
785 status = -ENODEV; 755 status = -ENODEV;
786 goto ExitTiValidateI2cImage; 756 goto out;
787 } 757 }
788 758
789 do { 759 do {
790 // Validate the I2C 760 /* Validate the I2C */
791 status = TIReadRom (serial, 761 status = read_rom(serial,
792 start_address, 762 start_address,
793 sizeof(struct ti_i2c_desc), 763 sizeof(struct ti_i2c_desc),
794 (__u8 *)rom_desc); 764 (__u8 *)rom_desc);
795 if (status) 765 if (status)
796 break; 766 break;
797 767
798 if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) { 768 if ((start_address + sizeof(struct ti_i2c_desc) +
769 rom_desc->Size) > TI_MAX_I2C_SIZE) {
799 status = -ENODEV; 770 status = -ENODEV;
800 dbg ("%s - structure too big, erroring out.", __func__); 771 dbg("%s - structure too big, erroring out.", __func__);
801 break; 772 break;
802 } 773 }
803 774
804 dbg ("%s Type = 0x%x", __func__, rom_desc->Type); 775 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
805 776
806 // Skip type 2 record 777 /* Skip type 2 record */
807 ttype = rom_desc->Type & 0x0f; 778 ttype = rom_desc->Type & 0x0f;
808 if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC 779 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
809 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) { 780 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
810 // Read the descriptor data 781 /* Read the descriptor data */
811 status = TIReadRom(serial, 782 status = read_rom(serial, start_address +
812 start_address+sizeof(struct ti_i2c_desc), 783 sizeof(struct ti_i2c_desc),
813 rom_desc->Size, 784 rom_desc->Size, buffer);
814 buffer);
815 if (status) 785 if (status)
816 break; 786 break;
817 787
818 status = ValidChecksum(rom_desc, buffer); 788 status = valid_csum(rom_desc, buffer);
819 if (status) 789 if (status)
820 break; 790 break;
821 } 791 }
822 start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size; 792 start_address = start_address + sizeof(struct ti_i2c_desc) +
793 rom_desc->Size;
823 794
824 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE)); 795 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
796 (start_address < TI_MAX_I2C_SIZE));
825 797
826 if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE)) 798 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
799 (start_address > TI_MAX_I2C_SIZE))
827 status = -ENODEV; 800 status = -ENODEV;
828 801
829ExitTiValidateI2cImage: 802out:
830 kfree (buffer); 803 kfree(buffer);
831 kfree (rom_desc); 804 kfree(rom_desc);
832 return status; 805 return status;
833} 806}
834 807
835static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer) 808static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
836{ 809{
837 int status; 810 int status;
838 int start_address; 811 int start_address;
839 struct ti_i2c_desc *rom_desc; 812 struct ti_i2c_desc *rom_desc;
840 struct edge_ti_manuf_descriptor *desc; 813 struct edge_ti_manuf_descriptor *desc;
841 814
842 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); 815 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
843 if (!rom_desc) { 816 if (!rom_desc) {
844 dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __func__); 817 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
818 __func__);
845 return -ENOMEM; 819 return -ENOMEM;
846 } 820 }
847 start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc); 821 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
822 rom_desc);
848 823
849 if (!start_address) { 824 if (!start_address) {
850 dbg ("%s - Edge Descriptor not found in I2C", __func__); 825 dbg("%s - Edge Descriptor not found in I2C", __func__);
851 status = -ENODEV; 826 status = -ENODEV;
852 goto exit; 827 goto exit;
853 } 828 }
854 829
855 // Read the descriptor data 830 /* Read the descriptor data */
856 status = TIReadRom (serial, 831 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
857 start_address+sizeof(struct ti_i2c_desc), 832 rom_desc->Size, buffer);
858 rom_desc->Size,
859 buffer);
860 if (status) 833 if (status)
861 goto exit; 834 goto exit;
862 835
863 status = ValidChecksum(rom_desc, buffer); 836 status = valid_csum(rom_desc, buffer);
864 837
865 desc = (struct edge_ti_manuf_descriptor *)buffer; 838 desc = (struct edge_ti_manuf_descriptor *)buffer;
866 dbg ( "%s - IonConfig 0x%x", __func__, desc->IonConfig ); 839 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
867 dbg ( "%s - Version %d", __func__, desc->Version ); 840 dbg("%s - Version %d", __func__, desc->Version);
868 dbg ( "%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev ); 841 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
869 dbg ( "%s - NumPorts %d", __func__, desc->NumPorts ); 842 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
870 dbg ( "%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts ); 843 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
871 dbg ( "%s - TotalPorts %d", __func__, desc->TotalPorts ); 844 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
872 845
873exit: 846exit:
874 kfree (rom_desc); 847 kfree(rom_desc);
875 return status; 848 return status;
876} 849}
877 850
878/* Build firmware header used for firmware update */ 851/* Build firmware header used for firmware update */
879static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev) 852static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
880{ 853{
881 __u8 *buffer; 854 __u8 *buffer;
882 int buffer_size; 855 int buffer_size;
@@ -889,24 +862,28 @@ static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
889 const struct firmware *fw; 862 const struct firmware *fw;
890 const char *fw_name = "edgeport/down3.bin"; 863 const char *fw_name = "edgeport/down3.bin";
891 864
892 // In order to update the I2C firmware we must change the type 2 record to type 0xF2. 865 /* In order to update the I2C firmware we must change the type 2 record
893 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver 866 * to type 0xF2. This will force the UMP to come up in Boot Mode.
894 // will download the latest firmware (padded to 15.5k) into the UMP ram. 867 * Then while in boot mode, the driver will download the latest
895 // And finally when the device comes back up in download mode the driver will cause 868 * firmware (padded to 15.5k) into the UMP ram. And finally when the
896 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update 869 * device comes back up in download mode the driver will cause the new
897 // the record type from 0xf2 to 0x02. 870 * firmware to be copied from the UMP Ram to I2C and the firmware will
898 871 * update the record type from 0xf2 to 0x02.
899 // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record) 872 */
900 buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec)); 873
901 874 /* Allocate a 15.5k buffer + 2 bytes for version number
902 buffer = kmalloc (buffer_size, GFP_KERNEL); 875 * (Firmware Record) */
876 buffer_size = (((1024 * 16) - 512 ) +
877 sizeof(struct ti_i2c_firmware_rec));
878
879 buffer = kmalloc(buffer_size, GFP_KERNEL);
903 if (!buffer) { 880 if (!buffer) {
904 dev_err (dev, "%s - out of memory\n", __func__); 881 dev_err(dev, "%s - out of memory\n", __func__);
905 return -ENOMEM; 882 return -ENOMEM;
906 } 883 }
907 884
908 // Set entire image of 0xffs 885 // Set entire image of 0xffs
909 memset (buffer, 0xff, buffer_size); 886 memset(buffer, 0xff, buffer_size);
910 887
911 err = request_firmware(&fw, fw_name, dev); 888 err = request_firmware(&fw, fw_name, dev);
912 if (err) { 889 if (err) {
@@ -921,16 +898,16 @@ static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
921 OperationalMinorVersion = fw->data[1]; 898 OperationalMinorVersion = fw->data[1];
922 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8); 899 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
923 900
924 // Copy version number into firmware record 901 /* Copy version number into firmware record */
925 firmware_rec = (struct ti_i2c_firmware_rec *)buffer; 902 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
926 903
927 firmware_rec->Ver_Major = OperationalMajorVersion; 904 firmware_rec->Ver_Major = OperationalMajorVersion;
928 firmware_rec->Ver_Minor = OperationalMinorVersion; 905 firmware_rec->Ver_Minor = OperationalMinorVersion;
929 906
930 // Pointer to fw_down memory image 907 /* Pointer to fw_down memory image */
931 img_header = (struct ti_i2c_image_header *)&fw->data[4]; 908 img_header = (struct ti_i2c_image_header *)&fw->data[4];
932 909
933 memcpy (buffer + sizeof(struct ti_i2c_firmware_rec), 910 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
934 &fw->data[4 + sizeof(struct ti_i2c_image_header)], 911 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
935 le16_to_cpu(img_header->Length)); 912 le16_to_cpu(img_header->Length));
936 913
@@ -940,12 +917,12 @@ static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
940 cs = (__u8)(cs + buffer[i]); 917 cs = (__u8)(cs + buffer[i]);
941 } 918 }
942 919
943 kfree (buffer); 920 kfree(buffer);
944 921
945 // Build new header 922 /* Build new header */
946 i2c_header = (struct ti_i2c_desc *)header; 923 i2c_header = (struct ti_i2c_desc *)header;
947 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data; 924 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
948 925
949 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK; 926 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
950 i2c_header->Size = (__u16)buffer_size; 927 i2c_header->Size = (__u16)buffer_size;
951 i2c_header->CheckSum = cs; 928 i2c_header->CheckSum = cs;
@@ -956,103 +933,100 @@ static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
956} 933}
957 934
958/* Try to figure out what type of I2c we have */ 935/* Try to figure out what type of I2c we have */
959static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial) 936static int i2c_type_bootmode(struct edgeport_serial *serial)
960{ 937{
961 int status; 938 int status;
962 __u8 data; 939 __u8 data;
963 940
964 // Try to read type 2 941 /* Try to read type 2 */
965 status = TIReadVendorRequestSync (serial->serial->dev, 942 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
966 UMPC_MEMORY_READ, // Request 943 DTK_ADDR_SPACE_I2C_TYPE_II, 0, &data, 0x01);
967 DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
968 0, // wIndex
969 &data, // TransferBuffer
970 0x01); // TransferBufferLength
971 if (status) 944 if (status)
972 dbg ("%s - read 2 status error = %d", __func__, status); 945 dbg("%s - read 2 status error = %d", __func__, status);
973 else 946 else
974 dbg ("%s - read 2 data = 0x%x", __func__, data); 947 dbg("%s - read 2 data = 0x%x", __func__, data);
975 if ((!status) && (data == UMP5152 || data == UMP3410)) { 948 if ((!status) && (data == UMP5152 || data == UMP3410)) {
976 dbg ("%s - ROM_TYPE_II", __func__); 949 dbg("%s - ROM_TYPE_II", __func__);
977 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 950 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
978 return 0; 951 return 0;
979 } 952 }
980 953
981 // Try to read type 3 954 /* Try to read type 3 */
982 status = TIReadVendorRequestSync (serial->serial->dev, 955 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
983 UMPC_MEMORY_READ, // Request 956 DTK_ADDR_SPACE_I2C_TYPE_III, 0, &data, 0x01);
984 DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
985 0, // wIndex
986 &data, // TransferBuffer
987 0x01); // TransferBufferLength
988 if (status) 957 if (status)
989 dbg ("%s - read 3 status error = %d", __func__, status); 958 dbg("%s - read 3 status error = %d", __func__, status);
990 else 959 else
991 dbg ("%s - read 2 data = 0x%x", __func__, data); 960 dbg("%s - read 2 data = 0x%x", __func__, data);
992 if ((!status) && (data == UMP5152 || data == UMP3410)) { 961 if ((!status) && (data == UMP5152 || data == UMP3410)) {
993 dbg ("%s - ROM_TYPE_III", __func__); 962 dbg("%s - ROM_TYPE_III", __func__);
994 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III; 963 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
995 return 0; 964 return 0;
996 } 965 }
997 966
998 dbg ("%s - Unknown", __func__); 967 dbg("%s - Unknown", __func__);
999 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 968 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1000 return -ENODEV; 969 return -ENODEV;
1001} 970}
1002 971
1003static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent) 972static int bulk_xfer(struct usb_serial *serial, void *buffer,
973 int length, int *num_sent)
1004{ 974{
1005 int status; 975 int status;
1006 976
1007 status = usb_bulk_msg (serial->dev, 977 status = usb_bulk_msg(serial->dev,
1008 usb_sndbulkpipe(serial->dev, 978 usb_sndbulkpipe(serial->dev,
1009 serial->port[0]->bulk_out_endpointAddress), 979 serial->port[0]->bulk_out_endpointAddress),
1010 buffer, 980 buffer, length, num_sent, 1000);
1011 length,
1012 num_sent,
1013 1000);
1014 return status; 981 return status;
1015} 982}
1016 983
1017/* Download given firmware image to the device (IN BOOT MODE) */ 984/* Download given firmware image to the device (IN BOOT MODE) */
1018static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length) 985static int download_code(struct edgeport_serial *serial, __u8 *image,
986 int image_length)
1019{ 987{
1020 int status = 0; 988 int status = 0;
1021 int pos; 989 int pos;
1022 int transfer; 990 int transfer;
1023 int done; 991 int done;
1024 992
1025 // Transfer firmware image 993 /* Transfer firmware image */
1026 for (pos = 0; pos < image_length; ) { 994 for (pos = 0; pos < image_length; ) {
1027 // Read the next buffer from file 995 /* Read the next buffer from file */
1028 transfer = image_length - pos; 996 transfer = image_length - pos;
1029 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE) 997 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1030 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE; 998 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1031 999
1032 // Transfer data 1000 /* Transfer data */
1033 status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done); 1001 status = bulk_xfer(serial->serial, &image[pos],
1002 transfer, &done);
1034 if (status) 1003 if (status)
1035 break; 1004 break;
1036 // Advance buffer pointer 1005 /* Advance buffer pointer */
1037 pos += done; 1006 pos += done;
1038 } 1007 }
1039 1008
1040 return status; 1009 return status;
1041} 1010}
1042 1011
1043// FIXME!!! 1012/* FIXME!!! */
1044static int TIConfigureBootDevice (struct usb_device *dev) 1013static int config_boot_dev(struct usb_device *dev)
1045{ 1014{
1046 return 0; 1015 return 0;
1047} 1016}
1048 1017
1018static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1019{
1020 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1021}
1022
1049/** 1023/**
1050 * DownloadTIFirmware - Download run-time operating firmware to the TI5052 1024 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1051 * 1025 *
1052 * This routine downloads the main operating code into the TI5052, using the 1026 * This routine downloads the main operating code into the TI5052, using the
1053 * boot code already burned into E2PROM or ROM. 1027 * boot code already burned into E2PROM or ROM.
1054 */ 1028 */
1055static int TIDownloadFirmware (struct edgeport_serial *serial) 1029static int download_fw(struct edgeport_serial *serial)
1056{ 1030{
1057 struct device *dev = &serial->serial->dev->dev; 1031 struct device *dev = &serial->serial->dev->dev;
1058 int status = 0; 1032 int status = 0;
@@ -1071,22 +1045,25 @@ static int TIDownloadFirmware (struct edgeport_serial *serial)
1071 /* Default to type 2 i2c */ 1045 /* Default to type 2 i2c */
1072 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 1046 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1073 1047
1074 status = TIChooseConfiguration (serial->serial->dev); 1048 status = choose_config(serial->serial->dev);
1075 if (status) 1049 if (status)
1076 return status; 1050 return status;
1077 1051
1078 interface = &serial->serial->interface->cur_altsetting->desc; 1052 interface = &serial->serial->interface->cur_altsetting->desc;
1079 if (!interface) { 1053 if (!interface) {
1080 dev_err (dev, "%s - no interface set, error!\n", __func__); 1054 dev_err(dev, "%s - no interface set, error!\n", __func__);
1081 return -ENODEV; 1055 return -ENODEV;
1082 } 1056 }
1083 1057
1084 // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING 1058 /*
1085 // if we have more than one endpoint we are definitely in download mode 1059 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1060 * if we have more than one endpoint we are definitely in download
1061 * mode
1062 */
1086 if (interface->bNumEndpoints > 1) 1063 if (interface->bNumEndpoints > 1)
1087 serial->product_info.TiMode = TI_MODE_DOWNLOAD; 1064 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1088 else 1065 else
1089 // Otherwise we will remain in configuring mode 1066 /* Otherwise we will remain in configuring mode */
1090 serial->product_info.TiMode = TI_MODE_CONFIGURING; 1067 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1091 1068
1092 /********************************************************************/ 1069 /********************************************************************/
@@ -1097,256 +1074,273 @@ static int TIDownloadFirmware (struct edgeport_serial *serial)
1097 1074
1098 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__); 1075 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
1099 1076
1100 status = TiValidateI2cImage (serial); 1077 status = check_i2c_image(serial);
1101 if (status) { 1078 if (status) {
1102 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__); 1079 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
1103 return status; 1080 return status;
1104 } 1081 }
1105 1082
1106 /* Validate Hardware version number 1083 /* Validate Hardware version number
1107 * Read Manufacturing Descriptor from TI Based Edgeport 1084 * Read Manufacturing Descriptor from TI Based Edgeport
1108 */ 1085 */
1109 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL); 1086 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1110 if (!ti_manuf_desc) { 1087 if (!ti_manuf_desc) {
1111 dev_err (dev, "%s - out of memory.\n", __func__); 1088 dev_err(dev, "%s - out of memory.\n", __func__);
1112 return -ENOMEM; 1089 return -ENOMEM;
1113 } 1090 }
1114 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc); 1091 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1115 if (status) { 1092 if (status) {
1116 kfree (ti_manuf_desc); 1093 kfree(ti_manuf_desc);
1117 return status; 1094 return status;
1118 } 1095 }
1119 1096
1120 // Check version number of ION descriptor 1097 /* Check version number of ION descriptor */
1121 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) { 1098 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1122 dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __func__, 1099 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1123 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev)); 1100 __func__, ti_cpu_rev(ti_manuf_desc));
1124 kfree (ti_manuf_desc); 1101 kfree(ti_manuf_desc);
1125 return -EINVAL; 1102 return -EINVAL;
1126 } 1103 }
1127 1104
1128 rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL); 1105 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1129 if (!rom_desc) { 1106 if (!rom_desc) {
1130 dev_err (dev, "%s - out of memory.\n", __func__); 1107 dev_err(dev, "%s - out of memory.\n", __func__);
1131 kfree (ti_manuf_desc); 1108 kfree(ti_manuf_desc);
1132 return -ENOMEM; 1109 return -ENOMEM;
1133 } 1110 }
1134 1111
1135 // Search for type 2 record (firmware record) 1112 /* Search for type 2 record (firmware record) */
1136 if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) { 1113 start_address = get_descriptor_addr(serial,
1114 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1115 if (start_address != 0) {
1137 struct ti_i2c_firmware_rec *firmware_version; 1116 struct ti_i2c_firmware_rec *firmware_version;
1138 __u8 record; 1117 __u8 record;
1139 1118
1140 dbg ("%s - Found Type FIRMWARE (Type 2) record", __func__); 1119 dbg("%s - Found Type FIRMWARE (Type 2) record",
1120 __func__);
1141 1121
1142 firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL); 1122 firmware_version = kmalloc(sizeof(*firmware_version),
1123 GFP_KERNEL);
1143 if (!firmware_version) { 1124 if (!firmware_version) {
1144 dev_err (dev, "%s - out of memory.\n", __func__); 1125 dev_err(dev, "%s - out of memory.\n", __func__);
1145 kfree (rom_desc); 1126 kfree(rom_desc);
1146 kfree (ti_manuf_desc); 1127 kfree(ti_manuf_desc);
1147 return -ENOMEM; 1128 return -ENOMEM;
1148 } 1129 }
1149 1130
1150 // Validate version number 1131 /* Validate version number
1151 // Read the descriptor data 1132 * Read the descriptor data
1152 status = TIReadRom (serial, 1133 */
1153 start_address+sizeof(struct ti_i2c_desc), 1134 status = read_rom(serial, start_address +
1135 sizeof(struct ti_i2c_desc),
1154 sizeof(struct ti_i2c_firmware_rec), 1136 sizeof(struct ti_i2c_firmware_rec),
1155 (__u8 *)firmware_version); 1137 (__u8 *)firmware_version);
1156 if (status) { 1138 if (status) {
1157 kfree (firmware_version); 1139 kfree(firmware_version);
1158 kfree (rom_desc); 1140 kfree(rom_desc);
1159 kfree (ti_manuf_desc); 1141 kfree(ti_manuf_desc);
1160 return status; 1142 return status;
1161 } 1143 }
1162 1144
1163 // Check version number of download with current version in I2c 1145 /* Check version number of download with current
1164 download_cur_ver = (firmware_version->Ver_Major << 8) + 1146 version in I2c */
1147 download_cur_ver = (firmware_version->Ver_Major << 8) +
1165 (firmware_version->Ver_Minor); 1148 (firmware_version->Ver_Minor);
1166 download_new_ver = (OperationalMajorVersion << 8) + 1149 download_new_ver = (OperationalMajorVersion << 8) +
1167 (OperationalMinorVersion); 1150 (OperationalMinorVersion);
1168 1151
1169 dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d", 1152 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1170 __func__, 1153 __func__,
1171 firmware_version->Ver_Major, 1154 firmware_version->Ver_Major,
1172 firmware_version->Ver_Minor, 1155 firmware_version->Ver_Minor,
1173 OperationalMajorVersion, 1156 OperationalMajorVersion,
1174 OperationalMinorVersion); 1157 OperationalMinorVersion);
1175 1158
1176 // Check if we have an old version in the I2C and update if necessary 1159 /* Check if we have an old version in the I2C and
1160 update if necessary */
1177 if (download_cur_ver != download_new_ver) { 1161 if (download_cur_ver != download_new_ver) {
1178 dbg ("%s - Update I2C Download from %d.%d to %d.%d", 1162 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1179 __func__, 1163 __func__,
1180 firmware_version->Ver_Major, 1164 firmware_version->Ver_Major,
1181 firmware_version->Ver_Minor, 1165 firmware_version->Ver_Minor,
1182 OperationalMajorVersion, 1166 OperationalMajorVersion,
1183 OperationalMinorVersion); 1167 OperationalMinorVersion);
1184 1168
1185 // In order to update the I2C firmware we must change the type 2 record to type 0xF2. 1169 /* In order to update the I2C firmware we must
1186 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver 1170 * change the type 2 record to type 0xF2. This
1187 // will download the latest firmware (padded to 15.5k) into the UMP ram. 1171 * will force the UMP to come up in Boot Mode.
1188 // And finally when the device comes back up in download mode the driver will cause 1172 * Then while in boot mode, the driver will
1189 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update 1173 * download the latest firmware (padded to
1190 // the record type from 0xf2 to 0x02. 1174 * 15.5k) into the UMP ram. Finally when the
1191 1175 * device comes back up in download mode the
1176 * driver will cause the new firmware to be
1177 * copied from the UMP Ram to I2C and the
1178 * firmware will update the record type from
1179 * 0xf2 to 0x02.
1180 */
1192 record = I2C_DESC_TYPE_FIRMWARE_BLANK; 1181 record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1193 1182
1194 // Change the I2C Firmware record type to 0xf2 to trigger an update 1183 /* Change the I2C Firmware record type to
1195 status = TIWriteRom (serial, 1184 0xf2 to trigger an update */
1196 start_address, 1185 status = write_rom(serial, start_address,
1197 sizeof(record), 1186 sizeof(record), &record);
1198 &record);
1199 if (status) { 1187 if (status) {
1200 kfree (firmware_version); 1188 kfree(firmware_version);
1201 kfree (rom_desc); 1189 kfree(rom_desc);
1202 kfree (ti_manuf_desc); 1190 kfree(ti_manuf_desc);
1203 return status; 1191 return status;
1204 } 1192 }
1205 1193
1206 // verify the write -- must do this in order for write to 1194 /* verify the write -- must do this in order
1207 // complete before we do the hardware reset 1195 * for write to complete before we do the
1208 status = TIReadRom (serial, 1196 * hardware reset
1197 */
1198 status = read_rom(serial,
1209 start_address, 1199 start_address,
1210 sizeof(record), 1200 sizeof(record),
1211 &record); 1201 &record);
1212
1213 if (status) { 1202 if (status) {
1214 kfree (firmware_version); 1203 kfree(firmware_version);
1215 kfree (rom_desc); 1204 kfree(rom_desc);
1216 kfree (ti_manuf_desc); 1205 kfree(ti_manuf_desc);
1217 return status; 1206 return status;
1218 } 1207 }
1219 1208
1220 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) { 1209 if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1221 dev_err (dev, "%s - error resetting device\n", __func__); 1210 dev_err(dev,
1222 kfree (firmware_version); 1211 "%s - error resetting device\n",
1223 kfree (rom_desc); 1212 __func__);
1224 kfree (ti_manuf_desc); 1213 kfree(firmware_version);
1214 kfree(rom_desc);
1215 kfree(ti_manuf_desc);
1225 return -ENODEV; 1216 return -ENODEV;
1226 } 1217 }
1227 1218
1228 dbg ("%s - HARDWARE RESET", __func__); 1219 dbg("%s - HARDWARE RESET", __func__);
1229 1220
1230 // Reset UMP -- Back to BOOT MODE 1221 /* Reset UMP -- Back to BOOT MODE */
1231 status = TISendVendorRequestSync (serial->serial->dev, 1222 status = ti_vsend_sync(serial->serial->dev,
1232 UMPC_HARDWARE_RESET, // Request 1223 UMPC_HARDWARE_RESET,
1233 0, // wValue 1224 0, 0, NULL, 0);
1234 0, // wIndex
1235 NULL, // TransferBuffer
1236 0); // TransferBufferLength
1237 1225
1238 dbg ( "%s - HARDWARE RESET return %d", __func__, status); 1226 dbg("%s - HARDWARE RESET return %d",
1227 __func__, status);
1239 1228
1240 /* return an error on purpose. */ 1229 /* return an error on purpose. */
1241 kfree (firmware_version); 1230 kfree(firmware_version);
1242 kfree (rom_desc); 1231 kfree(rom_desc);
1243 kfree (ti_manuf_desc); 1232 kfree(ti_manuf_desc);
1244 return -ENODEV; 1233 return -ENODEV;
1245 } 1234 }
1246 kfree (firmware_version); 1235 kfree(firmware_version);
1247 } 1236 }
1248 // Search for type 0xF2 record (firmware blank record) 1237 /* Search for type 0xF2 record (firmware blank record) */
1249 else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) { 1238 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1250 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec)) 1239#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1240 sizeof(struct ti_i2c_firmware_rec))
1251 __u8 *header; 1241 __u8 *header;
1252 __u8 *vheader; 1242 __u8 *vheader;
1253 1243
1254 header = kmalloc (HEADER_SIZE, GFP_KERNEL); 1244 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1255 if (!header) { 1245 if (!header) {
1256 dev_err (dev, "%s - out of memory.\n", __func__); 1246 dev_err(dev, "%s - out of memory.\n", __func__);
1257 kfree (rom_desc); 1247 kfree(rom_desc);
1258 kfree (ti_manuf_desc); 1248 kfree(ti_manuf_desc);
1259 return -ENOMEM; 1249 return -ENOMEM;
1260 } 1250 }
1261 1251
1262 vheader = kmalloc (HEADER_SIZE, GFP_KERNEL); 1252 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1263 if (!vheader) { 1253 if (!vheader) {
1264 dev_err (dev, "%s - out of memory.\n", __func__); 1254 dev_err(dev, "%s - out of memory.\n", __func__);
1265 kfree (header); 1255 kfree(header);
1266 kfree (rom_desc); 1256 kfree(rom_desc);
1267 kfree (ti_manuf_desc); 1257 kfree(ti_manuf_desc);
1268 return -ENOMEM; 1258 return -ENOMEM;
1269 } 1259 }
1270 1260
1271 dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __func__); 1261 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1272 1262 __func__);
1273 // In order to update the I2C firmware we must change the type 2 record to type 0xF2. 1263
1274 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver 1264 /*
1275 // will download the latest firmware (padded to 15.5k) into the UMP ram. 1265 * In order to update the I2C firmware we must change
1276 // And finally when the device comes back up in download mode the driver will cause 1266 * the type 2 record to type 0xF2. This will force the
1277 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update 1267 * UMP to come up in Boot Mode. Then while in boot
1278 // the record type from 0xf2 to 0x02. 1268 * mode, the driver will download the latest firmware
1279 status = BuildI2CFirmwareHeader(header, dev); 1269 * (padded to 15.5k) into the UMP ram. Finally when the
1270 * device comes back up in download mode the driver
1271 * will cause the new firmware to be copied from the
1272 * UMP Ram to I2C and the firmware will update the
1273 * record type from 0xf2 to 0x02.
1274 */
1275 status = build_i2c_fw_hdr(header, dev);
1280 if (status) { 1276 if (status) {
1281 kfree (vheader); 1277 kfree(vheader);
1282 kfree (header); 1278 kfree(header);
1283 kfree (rom_desc); 1279 kfree(rom_desc);
1284 kfree (ti_manuf_desc); 1280 kfree(ti_manuf_desc);
1285 return status; 1281 return status;
1286 } 1282 }
1287 1283
1288 // Update I2C with type 0xf2 record with correct size and checksum 1284 /* Update I2C with type 0xf2 record with correct
1289 status = TIWriteRom (serial, 1285 size and checksum */
1286 status = write_rom(serial,
1290 start_address, 1287 start_address,
1291 HEADER_SIZE, 1288 HEADER_SIZE,
1292 header); 1289 header);
1293 if (status) { 1290 if (status) {
1294 kfree (vheader); 1291 kfree(vheader);
1295 kfree (header); 1292 kfree(header);
1296 kfree (rom_desc); 1293 kfree(rom_desc);
1297 kfree (ti_manuf_desc); 1294 kfree(ti_manuf_desc);
1298 return status; 1295 return status;
1299 } 1296 }
1300 1297
1301 // verify the write -- must do this in order for write to 1298 /* verify the write -- must do this in order for
1302 // complete before we do the hardware reset 1299 write to complete before we do the hardware reset */
1303 status = TIReadRom (serial, 1300 status = read_rom(serial, start_address,
1304 start_address, 1301 HEADER_SIZE, vheader);
1305 HEADER_SIZE,
1306 vheader);
1307 1302
1308 if (status) { 1303 if (status) {
1309 dbg ("%s - can't read header back", __func__); 1304 dbg("%s - can't read header back", __func__);
1310 kfree (vheader); 1305 kfree(vheader);
1311 kfree (header); 1306 kfree(header);
1312 kfree (rom_desc); 1307 kfree(rom_desc);
1313 kfree (ti_manuf_desc); 1308 kfree(ti_manuf_desc);
1314 return status; 1309 return status;
1315 } 1310 }
1316 if (memcmp(vheader, header, HEADER_SIZE)) { 1311 if (memcmp(vheader, header, HEADER_SIZE)) {
1317 dbg ("%s - write download record failed", __func__); 1312 dbg("%s - write download record failed",
1318 kfree (vheader); 1313 __func__);
1319 kfree (header); 1314 kfree(vheader);
1320 kfree (rom_desc); 1315 kfree(header);
1321 kfree (ti_manuf_desc); 1316 kfree(rom_desc);
1317 kfree(ti_manuf_desc);
1322 return status; 1318 return status;
1323 } 1319 }
1324 1320
1325 kfree (vheader); 1321 kfree(vheader);
1326 kfree (header); 1322 kfree(header);
1327 1323
1328 dbg ("%s - Start firmware update", __func__); 1324 dbg("%s - Start firmware update", __func__);
1329 1325
1330 // Tell firmware to copy download image into I2C 1326 /* Tell firmware to copy download image into I2C */
1331 status = TISendVendorRequestSync (serial->serial->dev, 1327 status = ti_vsend_sync(serial->serial->dev,
1332 UMPC_COPY_DNLD_TO_I2C, // Request 1328 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
1333 0, // wValue
1334 0, // wIndex
1335 NULL, // TransferBuffer
1336 0); // TransferBufferLength
1337 1329
1338 dbg ("%s - Update complete 0x%x", __func__, status); 1330 dbg("%s - Update complete 0x%x", __func__, status);
1339 if (status) { 1331 if (status) {
1340 dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __func__); 1332 dev_err(dev,
1341 kfree (rom_desc); 1333 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1342 kfree (ti_manuf_desc); 1334 __func__);
1335 kfree(rom_desc);
1336 kfree(ti_manuf_desc);
1343 return status; 1337 return status;
1344 } 1338 }
1345 } 1339 }
1346 1340
1347 // The device is running the download code 1341 // The device is running the download code
1348 kfree (rom_desc); 1342 kfree(rom_desc);
1349 kfree (ti_manuf_desc); 1343 kfree(ti_manuf_desc);
1350 return 0; 1344 return 0;
1351 } 1345 }
1352 1346
@@ -1355,32 +1349,26 @@ static int TIDownloadFirmware (struct edgeport_serial *serial)
1355 /********************************************************************/ 1349 /********************************************************************/
1356 dbg("%s - RUNNING IN BOOT MODE", __func__); 1350 dbg("%s - RUNNING IN BOOT MODE", __func__);
1357 1351
1358 // Configure the TI device so we can use the BULK pipes for download 1352 /* Configure the TI device so we can use the BULK pipes for download */
1359 status = TIConfigureBootDevice (serial->serial->dev); 1353 status = config_boot_dev(serial->serial->dev);
1360 if (status) 1354 if (status)
1361 return status; 1355 return status;
1362 1356
1363 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) { 1357 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1364 dbg ("%s - VID = 0x%x", __func__, 1358 != USB_VENDOR_ID_ION) {
1359 dbg("%s - VID = 0x%x", __func__,
1365 le16_to_cpu(serial->serial->dev->descriptor.idVendor)); 1360 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1366 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II; 1361 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1367 goto StayInBootMode; 1362 goto stayinbootmode;
1368 }
1369
1370 // We have an ION device (I2c Must be programmed)
1371 // Determine I2C image type
1372 if (TIGetI2cTypeInBootMode(serial)) {
1373 goto StayInBootMode;
1374 } 1363 }
1375 1364
1376 // Registry variable set? 1365 /* We have an ION device (I2c Must be programmed)
1377 if (TIStayInBootMode) { 1366 Determine I2C image type */
1378 dbg ("%s - TIStayInBootMode", __func__); 1367 if (i2c_type_bootmode(serial))
1379 goto StayInBootMode; 1368 goto stayinbootmode;
1380 }
1381 1369
1382 // Check for ION Vendor ID and that the I2C is valid 1370 /* Check for ION Vendor ID and that the I2C is valid */
1383 if (!TiValidateI2cImage(serial)) { 1371 if (!check_i2c_image(serial)) {
1384 struct ti_i2c_image_header *header; 1372 struct ti_i2c_image_header *header;
1385 int i; 1373 int i;
1386 __u8 cs = 0; 1374 __u8 cs = 0;
@@ -1393,49 +1381,52 @@ static int TIDownloadFirmware (struct edgeport_serial *serial)
1393 /* Validate Hardware version number 1381 /* Validate Hardware version number
1394 * Read Manufacturing Descriptor from TI Based Edgeport 1382 * Read Manufacturing Descriptor from TI Based Edgeport
1395 */ 1383 */
1396 ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL); 1384 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1397 if (!ti_manuf_desc) { 1385 if (!ti_manuf_desc) {
1398 dev_err (dev, "%s - out of memory.\n", __func__); 1386 dev_err(dev, "%s - out of memory.\n", __func__);
1399 return -ENOMEM; 1387 return -ENOMEM;
1400 } 1388 }
1401 status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc); 1389 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1402 if (status) { 1390 if (status) {
1403 kfree (ti_manuf_desc); 1391 kfree(ti_manuf_desc);
1404 goto StayInBootMode; 1392 goto stayinbootmode;
1405 } 1393 }
1406 1394
1407 // Check for version 2 1395 /* Check for version 2 */
1408 if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) { 1396 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1409 dbg ("%s - Wrong CPU Rev %d (Must be 2)", __func__, 1397 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1410 TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev)); 1398 __func__, ti_cpu_rev(ti_manuf_desc));
1411 kfree (ti_manuf_desc); 1399 kfree(ti_manuf_desc);
1412 goto StayInBootMode; 1400 goto stayinbootmode;
1413 } 1401 }
1414 1402
1415 kfree (ti_manuf_desc); 1403 kfree(ti_manuf_desc);
1416 1404
1417 // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
1418 // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
1419 // will download the latest firmware (padded to 15.5k) into the UMP ram.
1420 // And finally when the device comes back up in download mode the driver will cause
1421 // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
1422 // the record type from 0xf2 to 0x02.
1423
1424 /* 1405 /*
1406 * In order to update the I2C firmware we must change the type
1407 * 2 record to type 0xF2. This will force the UMP to come up
1408 * in Boot Mode. Then while in boot mode, the driver will
1409 * download the latest firmware (padded to 15.5k) into the
1410 * UMP ram. Finally when the device comes back up in download
1411 * mode the driver will cause the new firmware to be copied
1412 * from the UMP Ram to I2C and the firmware will update the
1413 * record type from 0xf2 to 0x02.
1414 *
1425 * Do we really have to copy the whole firmware image, 1415 * Do we really have to copy the whole firmware image,
1426 * or could we do this in place! 1416 * or could we do this in place!
1427 */ 1417 */
1428 1418
1429 // Allocate a 15.5k buffer + 3 byte header 1419 /* Allocate a 15.5k buffer + 3 byte header */
1430 buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header)); 1420 buffer_size = (((1024 * 16) - 512) +
1431 buffer = kmalloc (buffer_size, GFP_KERNEL); 1421 sizeof(struct ti_i2c_image_header));
1422 buffer = kmalloc(buffer_size, GFP_KERNEL);
1432 if (!buffer) { 1423 if (!buffer) {
1433 dev_err (dev, "%s - out of memory\n", __func__); 1424 dev_err(dev, "%s - out of memory\n", __func__);
1434 return -ENOMEM; 1425 return -ENOMEM;
1435 } 1426 }
1436 1427
1437 // Initialize the buffer to 0xff (pad the buffer) 1428 /* Initialize the buffer to 0xff (pad the buffer) */
1438 memset (buffer, 0xff, buffer_size); 1429 memset(buffer, 0xff, buffer_size);
1439 1430
1440 err = request_firmware(&fw, fw_name, dev); 1431 err = request_firmware(&fw, fw_name, dev);
1441 if (err) { 1432 if (err) {
@@ -1447,38 +1438,43 @@ static int TIDownloadFirmware (struct edgeport_serial *serial)
1447 memcpy(buffer, &fw->data[4], fw->size - 4); 1438 memcpy(buffer, &fw->data[4], fw->size - 4);
1448 release_firmware(fw); 1439 release_firmware(fw);
1449 1440
1450 for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) { 1441 for (i = sizeof(struct ti_i2c_image_header);
1442 i < buffer_size; i++) {
1451 cs = (__u8)(cs + buffer[i]); 1443 cs = (__u8)(cs + buffer[i]);
1452 } 1444 }
1453 1445
1454 header = (struct ti_i2c_image_header *)buffer; 1446 header = (struct ti_i2c_image_header *)buffer;
1455 1447
1456 // update length and checksum after padding 1448 /* update length and checksum after padding */
1457 header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header))); 1449 header->Length = cpu_to_le16((__u16)(buffer_size -
1450 sizeof(struct ti_i2c_image_header)));
1458 header->CheckSum = cs; 1451 header->CheckSum = cs;
1459 1452
1460 // Download the operational code 1453 /* Download the operational code */
1461 dbg ("%s - Downloading operational code image (TI UMP)", __func__); 1454 dbg("%s - Downloading operational code image (TI UMP)",
1462 status = TIDownloadCodeImage (serial, buffer, buffer_size); 1455 __func__);
1456 status = download_code(serial, buffer, buffer_size);
1463 1457
1464 kfree (buffer); 1458 kfree(buffer);
1465 1459
1466 if (status) { 1460 if (status) {
1467 dbg ("%s - Error downloading operational code image", __func__); 1461 dbg("%s - Error downloading operational code image",
1462 __func__);
1468 return status; 1463 return status;
1469 } 1464 }
1470 1465
1471 // Device will reboot 1466 /* Device will reboot */
1472 serial->product_info.TiMode = TI_MODE_TRANSITIONING; 1467 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1473 1468
1474 dbg ("%s - Download successful -- Device rebooting...", __func__); 1469 dbg("%s - Download successful -- Device rebooting...",
1470 __func__);
1475 1471
1476 /* return an error on purpose */ 1472 /* return an error on purpose */
1477 return -ENODEV; 1473 return -ENODEV;
1478 } 1474 }
1479 1475
1480StayInBootMode: 1476stayinbootmode:
1481 // Eprom is invalid or blank stay in boot mode 1477 /* Eprom is invalid or blank stay in boot mode */
1482 dbg("%s - STAYING IN BOOT MODE", __func__); 1478 dbg("%s - STAYING IN BOOT MODE", __func__);
1483 serial->product_info.TiMode = TI_MODE_BOOT; 1479 serial->product_info.TiMode = TI_MODE_BOOT;
1484 1480
@@ -1486,156 +1482,33 @@ StayInBootMode:
1486} 1482}
1487 1483
1488 1484
1489static int TISetDtr (struct edgeport_port *port) 1485static int ti_do_config(struct edgeport_port *port, int feature, int on)
1490{ 1486{
1491 int port_number = port->port->number - port->port->serial->minor; 1487 int port_number = port->port->number - port->port->serial->minor;
1492 1488 on = !!on; /* 1 or 0 not bitmask */
1493 dbg ("%s", __func__); 1489 return send_cmd(port->port->serial->dev,
1494 port->shadow_mcr |= MCR_DTR; 1490 feature, (__u8)(UMPM_UART1_PORT + port_number),
1495 1491 on, NULL, 0);
1496 return TIWriteCommandSync (port->port->serial->dev,
1497 UMPC_SET_CLR_DTR,
1498 (__u8)(UMPM_UART1_PORT + port_number),
1499 1, /* set */
1500 NULL,
1501 0);
1502} 1492}
1503 1493
1504static int TIClearDtr (struct edgeport_port *port)
1505{
1506 int port_number = port->port->number - port->port->serial->minor;
1507
1508 dbg ("%s", __func__);
1509 port->shadow_mcr &= ~MCR_DTR;
1510 1494
1511 return TIWriteCommandSync (port->port->serial->dev, 1495static int restore_mcr(struct edgeport_port *port, __u8 mcr)
1512 UMPC_SET_CLR_DTR,
1513 (__u8)(UMPM_UART1_PORT + port_number),
1514 0, /* clear */
1515 NULL,
1516 0);
1517}
1518
1519static int TISetRts (struct edgeport_port *port)
1520{
1521 int port_number = port->port->number - port->port->serial->minor;
1522
1523 dbg ("%s", __func__);
1524 port->shadow_mcr |= MCR_RTS;
1525
1526 return TIWriteCommandSync (port->port->serial->dev,
1527 UMPC_SET_CLR_RTS,
1528 (__u8)(UMPM_UART1_PORT + port_number),
1529 1, /* set */
1530 NULL,
1531 0);
1532}
1533
1534static int TIClearRts (struct edgeport_port *port)
1535{
1536 int port_number = port->port->number - port->port->serial->minor;
1537
1538 dbg ("%s", __func__);
1539 port->shadow_mcr &= ~MCR_RTS;
1540
1541 return TIWriteCommandSync (port->port->serial->dev,
1542 UMPC_SET_CLR_RTS,
1543 (__u8)(UMPM_UART1_PORT + port_number),
1544 0, /* clear */
1545 NULL,
1546 0);
1547}
1548
1549static int TISetLoopBack (struct edgeport_port *port)
1550{
1551 int port_number = port->port->number - port->port->serial->minor;
1552
1553 dbg ("%s", __func__);
1554
1555 return TIWriteCommandSync (port->port->serial->dev,
1556 UMPC_SET_CLR_LOOPBACK,
1557 (__u8)(UMPM_UART1_PORT + port_number),
1558 1, /* set */
1559 NULL,
1560 0);
1561}
1562
1563static int TIClearLoopBack (struct edgeport_port *port)
1564{
1565 int port_number = port->port->number - port->port->serial->minor;
1566
1567 dbg ("%s", __func__);
1568
1569 return TIWriteCommandSync (port->port->serial->dev,
1570 UMPC_SET_CLR_LOOPBACK,
1571 (__u8)(UMPM_UART1_PORT + port_number),
1572 0, /* clear */
1573 NULL,
1574 0);
1575}
1576
1577static int TISetBreak (struct edgeport_port *port)
1578{
1579 int port_number = port->port->number - port->port->serial->minor;
1580
1581 dbg ("%s", __func__);
1582
1583 return TIWriteCommandSync (port->port->serial->dev,
1584 UMPC_SET_CLR_BREAK,
1585 (__u8)(UMPM_UART1_PORT + port_number),
1586 1, /* set */
1587 NULL,
1588 0);
1589}
1590
1591static int TIClearBreak (struct edgeport_port *port)
1592{
1593 int port_number = port->port->number - port->port->serial->minor;
1594
1595 dbg ("%s", __func__);
1596
1597 return TIWriteCommandSync (port->port->serial->dev,
1598 UMPC_SET_CLR_BREAK,
1599 (__u8)(UMPM_UART1_PORT + port_number),
1600 0, /* clear */
1601 NULL,
1602 0);
1603}
1604
1605static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
1606{ 1496{
1607 int status = 0; 1497 int status = 0;
1608 1498
1609 dbg ("%s - %x", __func__, mcr); 1499 dbg("%s - %x", __func__, mcr);
1610
1611 if (mcr & MCR_DTR)
1612 status = TISetDtr (port);
1613 else
1614 status = TIClearDtr (port);
1615 1500
1501 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1616 if (status) 1502 if (status)
1617 return status; 1503 return status;
1618 1504 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1619 if (mcr & MCR_RTS)
1620 status = TISetRts (port);
1621 else
1622 status = TIClearRts (port);
1623
1624 if (status) 1505 if (status)
1625 return status; 1506 return status;
1626 1507 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1627 if (mcr & MCR_LOOPBACK)
1628 status = TISetLoopBack (port);
1629 else
1630 status = TIClearLoopBack (port);
1631
1632 return status;
1633} 1508}
1634 1509
1635
1636
1637/* Convert TI LSR to standard UART flags */ 1510/* Convert TI LSR to standard UART flags */
1638static __u8 MapLineStatus (__u8 ti_lsr) 1511static __u8 map_line_status(__u8 ti_lsr)
1639{ 1512{
1640 __u8 lsr = 0; 1513 __u8 lsr = 0;
1641 1514
@@ -1647,22 +1520,23 @@ static __u8 MapLineStatus (__u8 ti_lsr)
1647 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */ 1520 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1648 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */ 1521 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1649 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */ 1522 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1650 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */ 1523 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1651 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */ 1524 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1652 1525
1653#undef MAP_FLAG 1526#undef MAP_FLAG
1654 1527
1655 return lsr; 1528 return lsr;
1656} 1529}
1657 1530
1658static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr) 1531static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
1659{ 1532{
1660 struct async_icount *icount; 1533 struct async_icount *icount;
1661 struct tty_struct *tty; 1534 struct tty_struct *tty;
1662 1535
1663 dbg ("%s - %02x", __func__, msr); 1536 dbg("%s - %02x", __func__, msr);
1664 1537
1665 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) { 1538 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1539 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1666 icount = &edge_port->icount; 1540 icount = &edge_port->icount;
1667 1541
1668 /* update input line counters */ 1542 /* update input line counters */
@@ -1674,7 +1548,7 @@ static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1674 icount->dcd++; 1548 icount->dcd++;
1675 if (msr & EDGEPORT_MSR_DELTA_RI) 1549 if (msr & EDGEPORT_MSR_DELTA_RI)
1676 icount->rng++; 1550 icount->rng++;
1677 wake_up_interruptible (&edge_port->delta_msr_wait); 1551 wake_up_interruptible(&edge_port->delta_msr_wait);
1678 } 1552 }
1679 1553
1680 /* Save the new modem status */ 1554 /* Save the new modem status */
@@ -1694,26 +1568,28 @@ static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
1694 return; 1568 return;
1695} 1569}
1696 1570
1697static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data) 1571static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1572 __u8 lsr, __u8 data)
1698{ 1573{
1699 struct async_icount *icount; 1574 struct async_icount *icount;
1700 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK)); 1575 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1576 LSR_FRM_ERR | LSR_BREAK));
1701 1577
1702 dbg ("%s - %02x", __func__, new_lsr); 1578 dbg("%s - %02x", __func__, new_lsr);
1703 1579
1704 edge_port->shadow_lsr = lsr; 1580 edge_port->shadow_lsr = lsr;
1705 1581
1706 if (new_lsr & LSR_BREAK) { 1582 if (new_lsr & LSR_BREAK)
1707 /* 1583 /*
1708 * Parity and Framing errors only count if they 1584 * Parity and Framing errors only count if they
1709 * occur exclusive of a break being received. 1585 * occur exclusive of a break being received.
1710 */ 1586 */
1711 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK); 1587 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1712 }
1713 1588
1714 /* Place LSR data byte into Rx buffer */ 1589 /* Place LSR data byte into Rx buffer */
1715 if (lsr_data && edge_port->port->tty) 1590 if (lsr_data && edge_port->port->tty)
1716 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1); 1591 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty,
1592 &data, 1);
1717 1593
1718 /* update input line counters */ 1594 /* update input line counters */
1719 icount = &edge_port->icount; 1595 icount = &edge_port->icount;
@@ -1728,7 +1604,7 @@ static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8
1728} 1604}
1729 1605
1730 1606
1731static void edge_interrupt_callback (struct urb *urb) 1607static void edge_interrupt_callback(struct urb *urb)
1732{ 1608{
1733 struct edgeport_serial *edge_serial = urb->context; 1609 struct edgeport_serial *edge_serial = urb->context;
1734 struct usb_serial_port *port; 1610 struct usb_serial_port *port;
@@ -1762,66 +1638,71 @@ static void edge_interrupt_callback (struct urb *urb)
1762 } 1638 }
1763 1639
1764 if (!length) { 1640 if (!length) {
1765 dbg ("%s - no data in urb", __func__); 1641 dbg("%s - no data in urb", __func__);
1766 goto exit; 1642 goto exit;
1767 } 1643 }
1768 1644
1769 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data); 1645 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1770 1646 __func__, length, data);
1647
1771 if (length != 2) { 1648 if (length != 2) {
1772 dbg ("%s - expecting packet of size 2, got %d", __func__, length); 1649 dbg("%s - expecting packet of size 2, got %d",
1650 __func__, length);
1773 goto exit; 1651 goto exit;
1774 } 1652 }
1775 1653
1776 port_number = TIUMP_GET_PORT_FROM_CODE (data[0]); 1654 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1777 function = TIUMP_GET_FUNC_FROM_CODE (data[0]); 1655 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1778 dbg ("%s - port_number %d, function %d, info 0x%x", 1656 dbg("%s - port_number %d, function %d, info 0x%x",
1779 __func__, port_number, function, data[1]); 1657 __func__, port_number, function, data[1]);
1780 port = edge_serial->serial->port[port_number]; 1658 port = edge_serial->serial->port[port_number];
1781 edge_port = usb_get_serial_port_data(port); 1659 edge_port = usb_get_serial_port_data(port);
1782 if (!edge_port) { 1660 if (!edge_port) {
1783 dbg ("%s - edge_port not found", __func__); 1661 dbg("%s - edge_port not found", __func__);
1784 return; 1662 return;
1785 } 1663 }
1786 switch (function) { 1664 switch (function) {
1787 case TIUMP_INTERRUPT_CODE_LSR: 1665 case TIUMP_INTERRUPT_CODE_LSR:
1788 lsr = MapLineStatus(data[1]); 1666 lsr = map_line_status(data[1]);
1789 if (lsr & UMP_UART_LSR_DATA_MASK) { 1667 if (lsr & UMP_UART_LSR_DATA_MASK) {
1790 /* Save the LSR event for bulk read completion routine */ 1668 /* Save the LSR event for bulk read
1791 dbg ("%s - LSR Event Port %u LSR Status = %02x", 1669 completion routine */
1670 dbg("%s - LSR Event Port %u LSR Status = %02x",
1792 __func__, port_number, lsr); 1671 __func__, port_number, lsr);
1793 edge_port->lsr_event = 1; 1672 edge_port->lsr_event = 1;
1794 edge_port->lsr_mask = lsr; 1673 edge_port->lsr_mask = lsr;
1795 } else { 1674 } else {
1796 dbg ("%s - ===== Port %d LSR Status = %02x ======", 1675 dbg("%s - ===== Port %d LSR Status = %02x ======",
1797 __func__, port_number, lsr); 1676 __func__, port_number, lsr);
1798 handle_new_lsr (edge_port, 0, lsr, 0); 1677 handle_new_lsr(edge_port, 0, lsr, 0);
1799 } 1678 }
1800 break; 1679 break;
1801 1680
1802 case TIUMP_INTERRUPT_CODE_MSR: // MSR 1681 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1803 /* Copy MSR from UMP */ 1682 /* Copy MSR from UMP */
1804 msr = data[1]; 1683 msr = data[1];
1805 dbg ("%s - ===== Port %u MSR Status = %02x ======\n", 1684 dbg("%s - ===== Port %u MSR Status = %02x ======\n",
1806 __func__, port_number, msr); 1685 __func__, port_number, msr);
1807 handle_new_msr (edge_port, msr); 1686 handle_new_msr(edge_port, msr);
1808 break; 1687 break;
1809 1688
1810 default: 1689 default:
1811 dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n", 1690 dev_err(&urb->dev->dev,
1812 __func__, data[1]); 1691 "%s - Unknown Interrupt code from UMP %x\n",
1692 __func__, data[1]);
1813 break; 1693 break;
1814 1694
1815 } 1695 }
1816 1696
1817exit: 1697exit:
1818 retval = usb_submit_urb (urb, GFP_ATOMIC); 1698 retval = usb_submit_urb(urb, GFP_ATOMIC);
1819 if (retval) 1699 if (retval)
1820 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n", 1700 dev_err(&urb->dev->dev,
1701 "%s - usb_submit_urb failed with result %d\n",
1821 __func__, retval); 1702 __func__, retval);
1822} 1703}
1823 1704
1824static void edge_bulk_in_callback (struct urb *urb) 1705static void edge_bulk_in_callback(struct urb *urb)
1825{ 1706{
1826 struct edgeport_port *edge_port = urb->context; 1707 struct edgeport_port *edge_port = urb->context;
1827 unsigned char *data = urb->transfer_buffer; 1708 unsigned char *data = urb->transfer_buffer;
@@ -1844,15 +1725,16 @@ static void edge_bulk_in_callback (struct urb *urb)
1844 __func__, status); 1725 __func__, status);
1845 return; 1726 return;
1846 default: 1727 default:
1847 dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n", 1728 dev_err(&urb->dev->dev,
1848 __func__, status); 1729 "%s - nonzero read bulk status received: %d\n",
1730 __func__, status);
1849 } 1731 }
1850 1732
1851 if (status == -EPIPE) 1733 if (status == -EPIPE)
1852 goto exit; 1734 goto exit;
1853 1735
1854 if (status) { 1736 if (status) {
1855 dev_err(&urb->dev->dev,"%s - stopping read!\n", __func__); 1737 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1856 return; 1738 return;
1857 } 1739 }
1858 1740
@@ -1860,9 +1742,9 @@ static void edge_bulk_in_callback (struct urb *urb)
1860 1742
1861 if (edge_port->lsr_event) { 1743 if (edge_port->lsr_event) {
1862 edge_port->lsr_event = 0; 1744 edge_port->lsr_event = 0;
1863 dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======", 1745 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1864 __func__, port_number, edge_port->lsr_mask, *data); 1746 __func__, port_number, edge_port->lsr_mask, *data);
1865 handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data); 1747 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1866 /* Adjust buffer length/pointer */ 1748 /* Adjust buffer length/pointer */
1867 --urb->actual_length; 1749 --urb->actual_length;
1868 ++data; 1750 ++data;
@@ -1870,13 +1752,14 @@ static void edge_bulk_in_callback (struct urb *urb)
1870 1752
1871 tty = edge_port->port->tty; 1753 tty = edge_port->port->tty;
1872 if (tty && urb->actual_length) { 1754 if (tty && urb->actual_length) {
1873 usb_serial_debug_data(debug, &edge_port->port->dev, __func__, urb->actual_length, data); 1755 usb_serial_debug_data(debug, &edge_port->port->dev,
1874 1756 __func__, urb->actual_length, data);
1875 if (edge_port->close_pending) { 1757 if (edge_port->close_pending)
1876 dbg ("%s - close is pending, dropping data on the floor.", __func__); 1758 dbg("%s - close pending, dropping data on the floor",
1877 } else { 1759 __func__);
1878 edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length); 1760 else
1879 } 1761 edge_tty_recv(&edge_port->port->dev, tty, data,
1762 urb->actual_length);
1880 edge_port->icount.rx += urb->actual_length; 1763 edge_port->icount.rx += urb->actual_length;
1881 } 1764 }
1882 1765
@@ -1891,37 +1774,31 @@ exit:
1891 } 1774 }
1892 spin_unlock(&edge_port->ep_lock); 1775 spin_unlock(&edge_port->ep_lock);
1893 if (retval) 1776 if (retval)
1894 dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n", 1777 dev_err(&urb->dev->dev,
1778 "%s - usb_submit_urb failed with result %d\n",
1895 __func__, retval); 1779 __func__, retval);
1896} 1780}
1897 1781
1898static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length) 1782static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1783 unsigned char *data, int length)
1899{ 1784{
1900 int cnt; 1785 int queued;
1901
1902 do {
1903 cnt = tty_buffer_request_room(tty, length);
1904 if (cnt < length) {
1905 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1906 __func__, length - cnt);
1907 if(cnt == 0)
1908 break;
1909 }
1910 tty_insert_flip_string(tty, data, cnt);
1911 data += cnt;
1912 length -= cnt;
1913 } while (length > 0);
1914 1786
1787 tty_buffer_request_room(tty, length);
1788 queued = tty_insert_flip_string(tty, data, length);
1789 if (queued < length)
1790 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1791 __func__, length - queued);
1915 tty_flip_buffer_push(tty); 1792 tty_flip_buffer_push(tty);
1916} 1793}
1917 1794
1918static void edge_bulk_out_callback (struct urb *urb) 1795static void edge_bulk_out_callback(struct urb *urb)
1919{ 1796{
1920 struct usb_serial_port *port = urb->context; 1797 struct usb_serial_port *port = urb->context;
1921 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1798 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1922 int status = urb->status; 1799 int status = urb->status;
1923 1800
1924 dbg ("%s - port %d", __func__, port->number); 1801 dbg("%s - port %d", __func__, port->number);
1925 1802
1926 edge_port->ep_write_urb_in_use = 0; 1803 edge_port->ep_write_urb_in_use = 0;
1927 1804
@@ -1945,7 +1822,7 @@ static void edge_bulk_out_callback (struct urb *urb)
1945 edge_send(port); 1822 edge_send(port);
1946} 1823}
1947 1824
1948static int edge_open (struct usb_serial_port *port, struct file * filp) 1825static int edge_open(struct usb_serial_port *port, struct file *filp)
1949{ 1826{
1950 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1827 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1951 struct edgeport_serial *edge_serial; 1828 struct edgeport_serial *edge_serial;
@@ -1965,118 +1842,119 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
1965 1842
1966 port_number = port->number - port->serial->minor; 1843 port_number = port->number - port->serial->minor;
1967 switch (port_number) { 1844 switch (port_number) {
1968 case 0: 1845 case 0:
1969 edge_port->uart_base = UMPMEM_BASE_UART1; 1846 edge_port->uart_base = UMPMEM_BASE_UART1;
1970 edge_port->dma_address = UMPD_OEDB1_ADDRESS; 1847 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1971 break; 1848 break;
1972 case 1: 1849 case 1:
1973 edge_port->uart_base = UMPMEM_BASE_UART2; 1850 edge_port->uart_base = UMPMEM_BASE_UART2;
1974 edge_port->dma_address = UMPD_OEDB2_ADDRESS; 1851 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1975 break; 1852 break;
1976 default: 1853 default:
1977 dev_err (&port->dev, "Unknown port number!!!\n"); 1854 dev_err(&port->dev, "Unknown port number!!!\n");
1978 return -ENODEV; 1855 return -ENODEV;
1979 } 1856 }
1980 1857
1981 dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x", 1858 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1982 __func__, port_number, edge_port->uart_base, edge_port->dma_address); 1859 __func__, port_number, edge_port->uart_base,
1860 edge_port->dma_address);
1983 1861
1984 dev = port->serial->dev; 1862 dev = port->serial->dev;
1985 1863
1986 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount)); 1864 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1987 init_waitqueue_head (&edge_port->delta_msr_wait); 1865 init_waitqueue_head(&edge_port->delta_msr_wait);
1988 1866
1989 /* turn off loopback */ 1867 /* turn off loopback */
1990 status = TIClearLoopBack (edge_port); 1868 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1991 if (status) { 1869 if (status) {
1992 dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n", 1870 dev_err(&port->dev,
1871 "%s - cannot send clear loopback command, %d\n",
1993 __func__, status); 1872 __func__, status);
1994 return status; 1873 return status;
1995 } 1874 }
1996 1875
1997 /* set up the port settings */ 1876 /* set up the port settings */
1998 edge_set_termios (port, port->tty->termios); 1877 edge_set_termios(port, port->tty->termios);
1999 1878
2000 /* open up the port */ 1879 /* open up the port */
2001 1880
2002 /* milliseconds to timeout for DMA transfer */ 1881 /* milliseconds to timeout for DMA transfer */
2003 transaction_timeout = 2; 1882 transaction_timeout = 2;
2004 1883
2005 edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) ); 1884 edge_port->ump_read_timeout =
1885 max(20, ((transaction_timeout * 3) / 2));
2006 1886
2007 // milliseconds to timeout for DMA transfer 1887 /* milliseconds to timeout for DMA transfer */
2008 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 1888 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
2009 UMP_PIPE_TRANS_TIMEOUT_ENA | 1889 UMP_PIPE_TRANS_TIMEOUT_ENA |
2010 (transaction_timeout << 2)); 1890 (transaction_timeout << 2));
2011 1891
2012 dbg ("%s - Sending UMPC_OPEN_PORT", __func__); 1892 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
2013 1893
2014 /* Tell TI to open and start the port */ 1894 /* Tell TI to open and start the port */
2015 status = TIWriteCommandSync (dev, 1895 status = send_cmd(dev, UMPC_OPEN_PORT,
2016 UMPC_OPEN_PORT, 1896 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
2017 (u8)(UMPM_UART1_PORT + port_number),
2018 open_settings,
2019 NULL,
2020 0);
2021 if (status) { 1897 if (status) {
2022 dev_err(&port->dev,"%s - cannot send open command, %d\n", __func__, status); 1898 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1899 __func__, status);
2023 return status; 1900 return status;
2024 } 1901 }
2025 1902
2026 /* Start the DMA? */ 1903 /* Start the DMA? */
2027 status = TIWriteCommandSync (dev, 1904 status = send_cmd(dev, UMPC_START_PORT,
2028 UMPC_START_PORT, 1905 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
2029 (u8)(UMPM_UART1_PORT + port_number),
2030 0,
2031 NULL,
2032 0);
2033 if (status) { 1906 if (status) {
2034 dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __func__, status); 1907 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1908 __func__, status);
2035 return status; 1909 return status;
2036 } 1910 }
2037 1911
2038 /* Clear TX and RX buffers in UMP */ 1912 /* Clear TX and RX buffers in UMP */
2039 status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN); 1913 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
2040 if (status) { 1914 if (status) {
2041 dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __func__, status); 1915 dev_err(&port->dev,
1916 "%s - cannot send clear buffers command, %d\n",
1917 __func__, status);
2042 return status; 1918 return status;
2043 } 1919 }
2044 1920
2045 /* Read Initial MSR */ 1921 /* Read Initial MSR */
2046 status = TIReadVendorRequestSync (dev, 1922 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
2047 UMPC_READ_MSR, // Request 1923 (__u16)(UMPM_UART1_PORT + port_number),
2048 0, // wValue 1924 &edge_port->shadow_msr, 1);
2049 (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
2050 &edge_port->shadow_msr, // TransferBuffer
2051 1); // TransferBufferLength
2052 if (status) { 1925 if (status) {
2053 dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __func__, status); 1926 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1927 __func__, status);
2054 return status; 1928 return status;
2055 } 1929 }
2056 1930
2057 dbg ("ShadowMSR 0x%X", edge_port->shadow_msr); 1931 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
2058 1932
2059 /* Set Initial MCR */ 1933 /* Set Initial MCR */
2060 edge_port->shadow_mcr = MCR_RTS | MCR_DTR; 1934 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
2061 dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr); 1935 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
2062 1936
2063 edge_serial = edge_port->edge_serial; 1937 edge_serial = edge_port->edge_serial;
2064 if (mutex_lock_interruptible(&edge_serial->es_lock)) 1938 if (mutex_lock_interruptible(&edge_serial->es_lock))
2065 return -ERESTARTSYS; 1939 return -ERESTARTSYS;
2066 if (edge_serial->num_ports_open == 0) { 1940 if (edge_serial->num_ports_open == 0) {
2067 /* we are the first port to be opened, let's post the interrupt urb */ 1941 /* we are the first port to open, post the interrupt urb */
2068 urb = edge_serial->serial->port[0]->interrupt_in_urb; 1942 urb = edge_serial->serial->port[0]->interrupt_in_urb;
2069 if (!urb) { 1943 if (!urb) {
2070 dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __func__); 1944 dev_err(&port->dev,
1945 "%s - no interrupt urb present, exiting\n",
1946 __func__);
2071 status = -EINVAL; 1947 status = -EINVAL;
2072 goto release_es_lock; 1948 goto release_es_lock;
2073 } 1949 }
2074 urb->complete = edge_interrupt_callback; 1950 urb->complete = edge_interrupt_callback;
2075 urb->context = edge_serial; 1951 urb->context = edge_serial;
2076 urb->dev = dev; 1952 urb->dev = dev;
2077 status = usb_submit_urb (urb, GFP_KERNEL); 1953 status = usb_submit_urb(urb, GFP_KERNEL);
2078 if (status) { 1954 if (status) {
2079 dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __func__, status); 1955 dev_err(&port->dev,
1956 "%s - usb_submit_urb failed with value %d\n",
1957 __func__, status);
2080 goto release_es_lock; 1958 goto release_es_lock;
2081 } 1959 }
2082 } 1960 }
@@ -2085,13 +1963,14 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
2085 * reset the data toggle on the bulk endpoints to work around bug in 1963 * reset the data toggle on the bulk endpoints to work around bug in
2086 * host controllers where things get out of sync some times 1964 * host controllers where things get out of sync some times
2087 */ 1965 */
2088 usb_clear_halt (dev, port->write_urb->pipe); 1966 usb_clear_halt(dev, port->write_urb->pipe);
2089 usb_clear_halt (dev, port->read_urb->pipe); 1967 usb_clear_halt(dev, port->read_urb->pipe);
2090 1968
2091 /* start up our bulk read urb */ 1969 /* start up our bulk read urb */
2092 urb = port->read_urb; 1970 urb = port->read_urb;
2093 if (!urb) { 1971 if (!urb) {
2094 dev_err (&port->dev, "%s - no read urb present, exiting\n", __func__); 1972 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1973 __func__);
2095 status = -EINVAL; 1974 status = -EINVAL;
2096 goto unlink_int_urb; 1975 goto unlink_int_urb;
2097 } 1976 }
@@ -2099,9 +1978,11 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
2099 urb->complete = edge_bulk_in_callback; 1978 urb->complete = edge_bulk_in_callback;
2100 urb->context = edge_port; 1979 urb->context = edge_port;
2101 urb->dev = dev; 1980 urb->dev = dev;
2102 status = usb_submit_urb (urb, GFP_KERNEL); 1981 status = usb_submit_urb(urb, GFP_KERNEL);
2103 if (status) { 1982 if (status) {
2104 dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status); 1983 dev_err(&port->dev,
1984 "%s - read bulk usb_submit_urb failed with value %d\n",
1985 __func__, status);
2105 goto unlink_int_urb; 1986 goto unlink_int_urb;
2106 } 1987 }
2107 1988
@@ -2119,7 +2000,7 @@ release_es_lock:
2119 return status; 2000 return status;
2120} 2001}
2121 2002
2122static void edge_close (struct usb_serial_port *port, struct file *filp) 2003static void edge_close(struct usb_serial_port *port, struct file *filp)
2123{ 2004{
2124 struct edgeport_serial *edge_serial; 2005 struct edgeport_serial *edge_serial;
2125 struct edgeport_port *edge_port; 2006 struct edgeport_port *edge_port;
@@ -2127,18 +2008,18 @@ static void edge_close (struct usb_serial_port *port, struct file *filp)
2127 int status; 2008 int status;
2128 2009
2129 dbg("%s - port %d", __func__, port->number); 2010 dbg("%s - port %d", __func__, port->number);
2130 2011
2131 edge_serial = usb_get_serial_data(port->serial); 2012 edge_serial = usb_get_serial_data(port->serial);
2132 edge_port = usb_get_serial_port_data(port); 2013 edge_port = usb_get_serial_port_data(port);
2133 if ((edge_serial == NULL) || (edge_port == NULL)) 2014 if (edge_serial == NULL || edge_port == NULL)
2134 return; 2015 return;
2135 2016
2136 /* The bulkreadcompletion routine will check 2017 /* The bulkreadcompletion routine will check
2137 * this flag and dump add read data */ 2018 * this flag and dump add read data */
2138 edge_port->close_pending = 1; 2019 edge_port->close_pending = 1;
2139 2020
2140 /* chase the port close and flush */ 2021 /* chase the port close and flush */
2141 TIChasePort (edge_port, (HZ*closing_wait)/100, 1); 2022 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
2142 2023
2143 usb_kill_urb(port->read_urb); 2024 usb_kill_urb(port->read_urb);
2144 usb_kill_urb(port->write_urb); 2025 usb_kill_urb(port->write_urb);
@@ -2148,7 +2029,7 @@ static void edge_close (struct usb_serial_port *port, struct file *filp)
2148 * send a close port command to it */ 2029 * send a close port command to it */
2149 dbg("%s - send umpc_close_port", __func__); 2030 dbg("%s - send umpc_close_port", __func__);
2150 port_number = port->number - port->serial->minor; 2031 port_number = port->number - port->serial->minor;
2151 status = TIWriteCommandSync (port->serial->dev, 2032 status = send_cmd(port->serial->dev,
2152 UMPC_CLOSE_PORT, 2033 UMPC_CLOSE_PORT,
2153 (__u8)(UMPM_UART1_PORT + port_number), 2034 (__u8)(UMPM_UART1_PORT + port_number),
2154 0, 2035 0,
@@ -2167,7 +2048,8 @@ static void edge_close (struct usb_serial_port *port, struct file *filp)
2167 dbg("%s - exited", __func__); 2048 dbg("%s - exited", __func__);
2168} 2049}
2169 2050
2170static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count) 2051static int edge_write(struct usb_serial_port *port, const unsigned char *data,
2052 int count)
2171{ 2053{
2172 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2054 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2173 unsigned long flags; 2055 unsigned long flags;
@@ -2223,11 +2105,12 @@ static void edge_send(struct usb_serial_port *port)
2223 2105
2224 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 2106 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2225 2107
2226 usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer); 2108 usb_serial_debug_data(debug, &port->dev, __func__, count,
2109 port->write_urb->transfer_buffer);
2227 2110
2228 /* set up our urb */ 2111 /* set up our urb */
2229 usb_fill_bulk_urb (port->write_urb, port->serial->dev, 2112 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2230 usb_sndbulkpipe (port->serial->dev, 2113 usb_sndbulkpipe(port->serial->dev,
2231 port->bulk_out_endpointAddress), 2114 port->bulk_out_endpointAddress),
2232 port->write_urb->transfer_buffer, count, 2115 port->write_urb->transfer_buffer, count,
2233 edge_bulk_out_callback, 2116 edge_bulk_out_callback,
@@ -2236,22 +2119,21 @@ static void edge_send(struct usb_serial_port *port)
2236 /* send the data out the bulk port */ 2119 /* send the data out the bulk port */
2237 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 2120 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2238 if (result) { 2121 if (result) {
2239 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result); 2122 dev_err(&port->dev,
2123 "%s - failed submitting write urb, error %d\n",
2124 __func__, result);
2240 edge_port->ep_write_urb_in_use = 0; 2125 edge_port->ep_write_urb_in_use = 0;
2241 // TODO: reschedule edge_send 2126 /* TODO: reschedule edge_send */
2242 } else { 2127 } else
2243 edge_port->icount.tx += count; 2128 edge_port->icount.tx += count;
2244 }
2245 2129
2246 /* wakeup any process waiting for writes to complete */ 2130 /* wakeup any process waiting for writes to complete */
2247 /* there is now more room in the buffer for new writes */ 2131 /* there is now more room in the buffer for new writes */
2248 if (tty) { 2132 if (tty)
2249 /* let the tty driver wakeup if it has a special write_wakeup function */
2250 tty_wakeup(tty); 2133 tty_wakeup(tty);
2251 }
2252} 2134}
2253 2135
2254static int edge_write_room (struct usb_serial_port *port) 2136static int edge_write_room(struct usb_serial_port *port)
2255{ 2137{
2256 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2138 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2257 int room = 0; 2139 int room = 0;
@@ -2260,9 +2142,9 @@ static int edge_write_room (struct usb_serial_port *port)
2260 dbg("%s - port %d", __func__, port->number); 2142 dbg("%s - port %d", __func__, port->number);
2261 2143
2262 if (edge_port == NULL) 2144 if (edge_port == NULL)
2263 return -ENODEV; 2145 return 0;
2264 if (edge_port->close_pending == 1) 2146 if (edge_port->close_pending == 1)
2265 return -ENODEV; 2147 return 0;
2266 2148
2267 spin_lock_irqsave(&edge_port->ep_lock, flags); 2149 spin_lock_irqsave(&edge_port->ep_lock, flags);
2268 room = edge_buf_space_avail(edge_port->ep_out_buf); 2150 room = edge_buf_space_avail(edge_port->ep_out_buf);
@@ -2272,7 +2154,7 @@ static int edge_write_room (struct usb_serial_port *port)
2272 return room; 2154 return room;
2273} 2155}
2274 2156
2275static int edge_chars_in_buffer (struct usb_serial_port *port) 2157static int edge_chars_in_buffer(struct usb_serial_port *port)
2276{ 2158{
2277 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2159 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2278 int chars = 0; 2160 int chars = 0;
@@ -2281,22 +2163,22 @@ static int edge_chars_in_buffer (struct usb_serial_port *port)
2281 dbg("%s - port %d", __func__, port->number); 2163 dbg("%s - port %d", __func__, port->number);
2282 2164
2283 if (edge_port == NULL) 2165 if (edge_port == NULL)
2284 return -ENODEV; 2166 return 0;
2285 if (edge_port->close_pending == 1) 2167 if (edge_port->close_pending == 1)
2286 return -ENODEV; 2168 return 0;
2287 2169
2288 spin_lock_irqsave(&edge_port->ep_lock, flags); 2170 spin_lock_irqsave(&edge_port->ep_lock, flags);
2289 chars = edge_buf_data_avail(edge_port->ep_out_buf); 2171 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2290 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 2172 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2291 2173
2292 dbg ("%s - returns %d", __func__, chars); 2174 dbg("%s - returns %d", __func__, chars);
2293 return chars; 2175 return chars;
2294} 2176}
2295 2177
2296static void edge_throttle (struct usb_serial_port *port) 2178static void edge_throttle(struct usb_serial_port *port)
2297{ 2179{
2298 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2180 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2299 struct tty_struct *tty; 2181 struct tty_struct *tty = port->tty;
2300 int status; 2182 int status;
2301 2183
2302 dbg("%s - port %d", __func__, port->number); 2184 dbg("%s - port %d", __func__, port->number);
@@ -2304,19 +2186,14 @@ static void edge_throttle (struct usb_serial_port *port)
2304 if (edge_port == NULL) 2186 if (edge_port == NULL)
2305 return; 2187 return;
2306 2188
2307 tty = port->tty;
2308 if (!tty) {
2309 dbg ("%s - no tty available", __func__);
2310 return;
2311 }
2312
2313 /* if we are implementing XON/XOFF, send the stop character */ 2189 /* if we are implementing XON/XOFF, send the stop character */
2314 if (I_IXOFF(tty)) { 2190 if (I_IXOFF(tty)) {
2315 unsigned char stop_char = STOP_CHAR(tty); 2191 unsigned char stop_char = STOP_CHAR(tty);
2316 status = edge_write (port, &stop_char, 1); 2192 status = edge_write(port, &stop_char, 1);
2317 if (status <= 0) { 2193 if (status <= 0)
2318 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status); 2194 dev_err(&port->dev,
2319 } 2195 "%s - failed to write stop character, %d\n",
2196 __func__, status);
2320 } 2197 }
2321 2198
2322 /* if we are implementing RTS/CTS, stop reads */ 2199 /* if we are implementing RTS/CTS, stop reads */
@@ -2326,10 +2203,10 @@ static void edge_throttle (struct usb_serial_port *port)
2326 2203
2327} 2204}
2328 2205
2329static void edge_unthrottle (struct usb_serial_port *port) 2206static void edge_unthrottle(struct usb_serial_port *port)
2330{ 2207{
2331 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2208 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2332 struct tty_struct *tty; 2209 struct tty_struct *tty = port->tty;
2333 int status; 2210 int status;
2334 2211
2335 dbg("%s - port %d", __func__, port->number); 2212 dbg("%s - port %d", __func__, port->number);
@@ -2337,27 +2214,23 @@ static void edge_unthrottle (struct usb_serial_port *port)
2337 if (edge_port == NULL) 2214 if (edge_port == NULL)
2338 return; 2215 return;
2339 2216
2340 tty = port->tty;
2341 if (!tty) {
2342 dbg ("%s - no tty available", __func__);
2343 return;
2344 }
2345
2346 /* if we are implementing XON/XOFF, send the start character */ 2217 /* if we are implementing XON/XOFF, send the start character */
2347 if (I_IXOFF(tty)) { 2218 if (I_IXOFF(tty)) {
2348 unsigned char start_char = START_CHAR(tty); 2219 unsigned char start_char = START_CHAR(tty);
2349 status = edge_write (port, &start_char, 1); 2220 status = edge_write(port, &start_char, 1);
2350 if (status <= 0) { 2221 if (status <= 0)
2351 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status); 2222 dev_err(&port->dev,
2352 } 2223 "%s - failed to write start character, %d\n",
2224 __func__, status);
2353 } 2225 }
2354
2355 /* if we are implementing RTS/CTS, restart reads */ 2226 /* if we are implementing RTS/CTS, restart reads */
2356 /* are the Edgeport will assert the RTS line */ 2227 /* are the Edgeport will assert the RTS line */
2357 if (C_CRTSCTS(tty)) { 2228 if (C_CRTSCTS(tty)) {
2358 status = restart_read(edge_port); 2229 status = restart_read(edge_port);
2359 if (status) 2230 if (status)
2360 dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status); 2231 dev_err(&port->dev,
2232 "%s - read bulk usb_submit_urb failed: %d\n",
2233 __func__, status);
2361 } 2234 }
2362 2235
2363} 2236}
@@ -2398,22 +2271,26 @@ static int restart_read(struct edgeport_port *edge_port)
2398 return status; 2271 return status;
2399} 2272}
2400 2273
2401static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios) 2274static void change_port_settings(struct edgeport_port *edge_port,
2275 struct ktermios *old_termios)
2402{ 2276{
2403 struct ump_uart_config *config; 2277 struct ump_uart_config *config;
2404 struct tty_struct *tty; 2278 struct tty_struct *tty;
2405 int baud; 2279 int baud;
2406 unsigned cflag; 2280 unsigned cflag;
2407 int status; 2281 int status;
2408 int port_number = edge_port->port->number - edge_port->port->serial->minor; 2282 int port_number = edge_port->port->number -
2283 edge_port->port->serial->minor;
2409 2284
2410 dbg("%s - port %d", __func__, edge_port->port->number); 2285 dbg("%s - port %d", __func__, edge_port->port->number);
2411 2286
2412 tty = edge_port->port->tty; 2287 tty = edge_port->port->tty;
2413 2288
2414 config = kmalloc (sizeof (*config), GFP_KERNEL); 2289 config = kmalloc(sizeof(*config), GFP_KERNEL);
2415 if (!config) { 2290 if (!config) {
2416 dev_err (&edge_port->port->dev, "%s - out of memory\n", __func__); 2291 *tty->termios = *old_termios;
2292 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2293 __func__);
2417 return; 2294 return;
2418 } 2295 }
2419 2296
@@ -2427,22 +2304,22 @@ static void change_port_settings (struct edgeport_port *edge_port, struct ktermi
2427 config->bUartMode = (__u8)(edge_port->bUartMode); 2304 config->bUartMode = (__u8)(edge_port->bUartMode);
2428 2305
2429 switch (cflag & CSIZE) { 2306 switch (cflag & CSIZE) {
2430 case CS5: 2307 case CS5:
2431 config->bDataBits = UMP_UART_CHAR5BITS; 2308 config->bDataBits = UMP_UART_CHAR5BITS;
2432 dbg ("%s - data bits = 5", __func__); 2309 dbg("%s - data bits = 5", __func__);
2433 break; 2310 break;
2434 case CS6: 2311 case CS6:
2435 config->bDataBits = UMP_UART_CHAR6BITS; 2312 config->bDataBits = UMP_UART_CHAR6BITS;
2436 dbg ("%s - data bits = 6", __func__); 2313 dbg("%s - data bits = 6", __func__);
2437 break; 2314 break;
2438 case CS7: 2315 case CS7:
2439 config->bDataBits = UMP_UART_CHAR7BITS; 2316 config->bDataBits = UMP_UART_CHAR7BITS;
2440 dbg ("%s - data bits = 7", __func__); 2317 dbg("%s - data bits = 7", __func__);
2441 break; 2318 break;
2442 default: 2319 default:
2443 case CS8: 2320 case CS8:
2444 config->bDataBits = UMP_UART_CHAR8BITS; 2321 config->bDataBits = UMP_UART_CHAR8BITS;
2445 dbg ("%s - data bits = 8", __func__); 2322 dbg("%s - data bits = 8", __func__);
2446 break; 2323 break;
2447 } 2324 }
2448 2325
@@ -2457,7 +2334,7 @@ static void change_port_settings (struct edgeport_port *edge_port, struct ktermi
2457 dbg("%s - parity = even", __func__); 2334 dbg("%s - parity = even", __func__);
2458 } 2335 }
2459 } else { 2336 } else {
2460 config->bParity = UMP_UART_NOPARITY; 2337 config->bParity = UMP_UART_NOPARITY;
2461 dbg("%s - parity = none", __func__); 2338 dbg("%s - parity = none", __func__);
2462 } 2339 }
2463 2340
@@ -2480,29 +2357,26 @@ static void change_port_settings (struct edgeport_port *edge_port, struct ktermi
2480 restart_read(edge_port); 2357 restart_read(edge_port);
2481 } 2358 }
2482 2359
2483 /* if we are implementing XON/XOFF, set the start and stop character in the device */ 2360 /* if we are implementing XON/XOFF, set the start and stop
2484 if (I_IXOFF(tty) || I_IXON(tty)) { 2361 character in the device */
2485 config->cXon = START_CHAR(tty); 2362 config->cXon = START_CHAR(tty);
2486 config->cXoff = STOP_CHAR(tty); 2363 config->cXoff = STOP_CHAR(tty);
2487 2364
2488 /* if we are implementing INBOUND XON/XOFF */ 2365 /* if we are implementing INBOUND XON/XOFF */
2489 if (I_IXOFF(tty)) { 2366 if (I_IXOFF(tty)) {
2490 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X; 2367 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2491 dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", 2368 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2492 __func__, config->cXon, config->cXoff); 2369 __func__, config->cXon, config->cXoff);
2493 } else { 2370 } else
2494 dbg ("%s - INBOUND XON/XOFF is disabled", __func__); 2371 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2495 }
2496 2372
2497 /* if we are implementing OUTBOUND XON/XOFF */ 2373 /* if we are implementing OUTBOUND XON/XOFF */
2498 if (I_IXON(tty)) { 2374 if (I_IXON(tty)) {
2499 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X; 2375 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2500 dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", 2376 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2501 __func__, config->cXon, config->cXoff); 2377 __func__, config->cXon, config->cXoff);
2502 } else { 2378 } else
2503 dbg ("%s - OUTBOUND XON/XOFF is disabled", __func__); 2379 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2504 }
2505 }
2506 2380
2507 tty->termios->c_cflag &= ~CMSPAR; 2381 tty->termios->c_cflag &= ~CMSPAR;
2508 2382
@@ -2519,62 +2393,52 @@ static void change_port_settings (struct edgeport_port *edge_port, struct ktermi
2519 2393
2520 /* FIXME: Recompute actual baud from divisor here */ 2394 /* FIXME: Recompute actual baud from divisor here */
2521 2395
2522 dbg ("%s - baud rate = %d, wBaudRate = %d", __func__, baud, config->wBaudRate); 2396 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2397 config->wBaudRate);
2523 2398
2524 dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate)); 2399 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2525 dbg ("wFlags: 0x%x", config->wFlags); 2400 dbg("wFlags: 0x%x", config->wFlags);
2526 dbg ("bDataBits: %d", config->bDataBits); 2401 dbg("bDataBits: %d", config->bDataBits);
2527 dbg ("bParity: %d", config->bParity); 2402 dbg("bParity: %d", config->bParity);
2528 dbg ("bStopBits: %d", config->bStopBits); 2403 dbg("bStopBits: %d", config->bStopBits);
2529 dbg ("cXon: %d", config->cXon); 2404 dbg("cXon: %d", config->cXon);
2530 dbg ("cXoff: %d", config->cXoff); 2405 dbg("cXoff: %d", config->cXoff);
2531 dbg ("bUartMode: %d", config->bUartMode); 2406 dbg("bUartMode: %d", config->bUartMode);
2532 2407
2533 /* move the word values into big endian mode */ 2408 /* move the word values into big endian mode */
2534 cpu_to_be16s (&config->wFlags); 2409 cpu_to_be16s(&config->wFlags);
2535 cpu_to_be16s (&config->wBaudRate); 2410 cpu_to_be16s(&config->wBaudRate);
2536 2411
2537 status = TIWriteCommandSync (edge_port->port->serial->dev, 2412 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
2538 UMPC_SET_CONFIG,
2539 (__u8)(UMPM_UART1_PORT + port_number), 2413 (__u8)(UMPM_UART1_PORT + port_number),
2540 0, 2414 0, (__u8 *)config, sizeof(*config));
2541 (__u8 *)config, 2415 if (status)
2542 sizeof(*config)); 2416 dbg("%s - error %d when trying to write config to device",
2543 if (status) {
2544 dbg ("%s - error %d when trying to write config to device",
2545 __func__, status); 2417 __func__, status);
2546 } 2418 kfree(config);
2547
2548 kfree (config);
2549
2550 return; 2419 return;
2551} 2420}
2552 2421
2553static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios) 2422static void edge_set_termios(struct usb_serial_port *port,
2423 struct ktermios *old_termios)
2554{ 2424{
2555 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2425 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2556 struct tty_struct *tty = port->tty; 2426 struct tty_struct *tty = port->tty;
2557 unsigned int cflag;
2558
2559 cflag = tty->termios->c_cflag;
2560 2427
2561 dbg("%s - clfag %08x iflag %08x", __func__, 2428 dbg("%s - clfag %08x iflag %08x", __func__,
2562 tty->termios->c_cflag, tty->termios->c_iflag); 2429 tty->termios->c_cflag, tty->termios->c_iflag);
2563 dbg("%s - old clfag %08x old iflag %08x", __func__, 2430 dbg("%s - old clfag %08x old iflag %08x", __func__,
2564 old_termios->c_cflag, old_termios->c_iflag); 2431 old_termios->c_cflag, old_termios->c_iflag);
2565
2566 dbg("%s - port %d", __func__, port->number); 2432 dbg("%s - port %d", __func__, port->number);
2567 2433
2568 if (edge_port == NULL) 2434 if (edge_port == NULL)
2569 return; 2435 return;
2570
2571 /* change the port settings to the new ones specified */ 2436 /* change the port settings to the new ones specified */
2572 change_port_settings (edge_port, old_termios); 2437 change_port_settings(edge_port, old_termios);
2573
2574 return;
2575} 2438}
2576 2439
2577static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear) 2440static int edge_tiocmset(struct usb_serial_port *port, struct file *file,
2441 unsigned int set, unsigned int clear)
2578{ 2442{
2579 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2443 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2580 unsigned int mcr; 2444 unsigned int mcr;
@@ -2601,8 +2465,7 @@ static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsig
2601 edge_port->shadow_mcr = mcr; 2465 edge_port->shadow_mcr = mcr;
2602 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 2466 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2603 2467
2604 TIRestoreMCR (edge_port, mcr); 2468 restore_mcr(edge_port, mcr);
2605
2606 return 0; 2469 return 0;
2607} 2470}
2608 2471
@@ -2634,7 +2497,8 @@ static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
2634 return result; 2497 return result;
2635} 2498}
2636 2499
2637static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo) 2500static int get_serial_info(struct edgeport_port *edge_port,
2501 struct serial_struct __user *retinfo)
2638{ 2502{
2639 struct serial_struct tmp; 2503 struct serial_struct tmp;
2640 2504
@@ -2652,17 +2516,14 @@ static int get_serial_info (struct edgeport_port *edge_port, struct serial_struc
2652 tmp.baud_base = 9600; 2516 tmp.baud_base = 9600;
2653 tmp.close_delay = 5*HZ; 2517 tmp.close_delay = 5*HZ;
2654 tmp.closing_wait = closing_wait; 2518 tmp.closing_wait = closing_wait;
2655// tmp.custom_divisor = state->custom_divisor;
2656// tmp.hub6 = state->hub6;
2657// tmp.io_type = state->io_type;
2658
2659 2519
2660 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 2520 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2661 return -EFAULT; 2521 return -EFAULT;
2662 return 0; 2522 return 0;
2663} 2523}
2664 2524
2665static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg) 2525static int edge_ioctl(struct usb_serial_port *port, struct file *file,
2526 unsigned int cmd, unsigned long arg)
2666{ 2527{
2667 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2528 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2668 struct async_icount cnow; 2529 struct async_icount cnow;
@@ -2671,81 +2532,63 @@ static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned
2671 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd); 2532 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2672 2533
2673 switch (cmd) { 2534 switch (cmd) {
2674 case TIOCINQ: 2535 case TIOCGSERIAL:
2675 dbg("%s - (%d) TIOCINQ", __func__, port->number); 2536 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2676// return get_number_bytes_avail(edge_port, (unsigned int *) arg); 2537 return get_serial_info(edge_port,
2677 break; 2538 (struct serial_struct __user *) arg);
2678 2539 case TIOCMIWAIT:
2679 case TIOCSERGETLSR: 2540 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2680 dbg("%s - (%d) TIOCSERGETLSR", __func__, port->number); 2541 cprev = edge_port->icount;
2681// return get_lsr_info(edge_port, (unsigned int *) arg); 2542 while (1) {
2682 break; 2543 interruptible_sleep_on(&edge_port->delta_msr_wait);
2683 2544 /* see if a signal did it */
2684 case TIOCGSERIAL: 2545 if (signal_pending(current))
2685 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number); 2546 return -ERESTARTSYS;
2686 return get_serial_info(edge_port, (struct serial_struct __user *) arg); 2547 cnow = edge_port->icount;
2687 break; 2548 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2688 2549 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2689 case TIOCSSERIAL: 2550 return -EIO; /* no change => error */
2690 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number); 2551 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2691 break; 2552 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2692 2553 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2693 case TIOCMIWAIT: 2554 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2694 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number); 2555 return 0;
2695 cprev = edge_port->icount;
2696 while (1) {
2697 interruptible_sleep_on(&edge_port->delta_msr_wait);
2698 /* see if a signal did it */
2699 if (signal_pending(current))
2700 return -ERESTARTSYS;
2701 cnow = edge_port->icount;
2702 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2703 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2704 return -EIO; /* no change => error */
2705 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2706 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2707 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2708 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2709 return 0;
2710 }
2711 cprev = cnow;
2712 } 2556 }
2713 /* not reached */ 2557 cprev = cnow;
2714 break; 2558 }
2715 2559 /* not reached */
2716 case TIOCGICOUNT: 2560 break;
2717 dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, 2561 case TIOCGICOUNT:
2718 port->number, edge_port->icount.rx, edge_port->icount.tx); 2562 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2719 if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount))) 2563 port->number, edge_port->icount.rx, edge_port->icount.tx);
2720 return -EFAULT; 2564 if (copy_to_user((void __user *)arg, &edge_port->icount,
2721 return 0; 2565 sizeof(edge_port->icount)))
2566 return -EFAULT;
2567 return 0;
2722 } 2568 }
2723
2724 return -ENOIOCTLCMD; 2569 return -ENOIOCTLCMD;
2725} 2570}
2726 2571
2727static void edge_break (struct usb_serial_port *port, int break_state) 2572static void edge_break(struct usb_serial_port *port, int on)
2728{ 2573{
2729 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 2574 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2730 int status; 2575 int status;
2576 int bv = 0; /* Off */
2731 2577
2732 dbg ("%s - state = %d", __func__, break_state); 2578 dbg("%s - state = %d", __func__, on);
2733 2579
2734 /* chase the port close */ 2580 /* chase the port close */
2735 TIChasePort (edge_port, 0, 0); 2581 chase_port(edge_port, 0, 0);
2736 2582
2737 if (break_state == -1) { 2583 if (on == -1)
2738 status = TISetBreak (edge_port); 2584 bv = 1; /* On */
2739 } else { 2585 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2740 status = TIClearBreak (edge_port); 2586 if (status)
2741 } 2587 dbg("%s - error %d sending break set/clear command.",
2742 if (status) {
2743 dbg ("%s - error %d sending break set/clear command.",
2744 __func__, status); 2588 __func__, status);
2745 }
2746} 2589}
2747 2590
2748static int edge_startup (struct usb_serial *serial) 2591static int edge_startup(struct usb_serial *serial)
2749{ 2592{
2750 struct edgeport_serial *edge_serial; 2593 struct edgeport_serial *edge_serial;
2751 struct edgeport_port *edge_port; 2594 struct edgeport_port *edge_port;
@@ -2765,9 +2608,9 @@ static int edge_startup (struct usb_serial *serial)
2765 edge_serial->serial = serial; 2608 edge_serial->serial = serial;
2766 usb_set_serial_data(serial, edge_serial); 2609 usb_set_serial_data(serial, edge_serial);
2767 2610
2768 status = TIDownloadFirmware (edge_serial); 2611 status = download_fw(edge_serial);
2769 if (status) { 2612 if (status) {
2770 kfree (edge_serial); 2613 kfree(edge_serial);
2771 return status; 2614 return status;
2772 } 2615 }
2773 2616
@@ -2775,13 +2618,15 @@ static int edge_startup (struct usb_serial *serial)
2775 for (i = 0; i < serial->num_ports; ++i) { 2618 for (i = 0; i < serial->num_ports; ++i) {
2776 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL); 2619 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2777 if (edge_port == NULL) { 2620 if (edge_port == NULL) {
2778 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); 2621 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2622 __func__);
2779 goto cleanup; 2623 goto cleanup;
2780 } 2624 }
2781 spin_lock_init(&edge_port->ep_lock); 2625 spin_lock_init(&edge_port->ep_lock);
2782 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE); 2626 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2783 if (edge_port->ep_out_buf == NULL) { 2627 if (edge_port->ep_out_buf == NULL) {
2784 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); 2628 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2629 __func__);
2785 kfree(edge_port); 2630 kfree(edge_port);
2786 goto cleanup; 2631 goto cleanup;
2787 } 2632 }
@@ -2790,27 +2635,27 @@ static int edge_startup (struct usb_serial *serial)
2790 usb_set_serial_port_data(serial->port[i], edge_port); 2635 usb_set_serial_port_data(serial->port[i], edge_port);
2791 edge_port->bUartMode = default_uart_mode; 2636 edge_port->bUartMode = default_uart_mode;
2792 } 2637 }
2793 2638
2794 return 0; 2639 return 0;
2795 2640
2796cleanup: 2641cleanup:
2797 for (--i; i>=0; --i) { 2642 for (--i; i >= 0; --i) {
2798 edge_port = usb_get_serial_port_data(serial->port[i]); 2643 edge_port = usb_get_serial_port_data(serial->port[i]);
2799 edge_buf_free(edge_port->ep_out_buf); 2644 edge_buf_free(edge_port->ep_out_buf);
2800 kfree(edge_port); 2645 kfree(edge_port);
2801 usb_set_serial_port_data(serial->port[i], NULL); 2646 usb_set_serial_port_data(serial->port[i], NULL);
2802 } 2647 }
2803 kfree (edge_serial); 2648 kfree(edge_serial);
2804 usb_set_serial_data(serial, NULL); 2649 usb_set_serial_data(serial, NULL);
2805 return -ENOMEM; 2650 return -ENOMEM;
2806} 2651}
2807 2652
2808static void edge_shutdown (struct usb_serial *serial) 2653static void edge_shutdown(struct usb_serial *serial)
2809{ 2654{
2810 int i; 2655 int i;
2811 struct edgeport_port *edge_port; 2656 struct edgeport_port *edge_port;
2812 2657
2813 dbg ("%s", __func__); 2658 dbg("%s", __func__);
2814 2659
2815 for (i = 0; i < serial->num_ports; ++i) { 2660 for (i = 0; i < serial->num_ports; ++i) {
2816 edge_port = usb_get_serial_port_data(serial->port[i]); 2661 edge_port = usb_get_serial_port_data(serial->port[i]);
@@ -2852,7 +2697,8 @@ static ssize_t store_uart_mode(struct device *dev,
2852 return count; 2697 return count;
2853} 2698}
2854 2699
2855static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode); 2700static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2701 store_uart_mode);
2856 2702
2857static int edge_create_sysfs_attrs(struct usb_serial_port *port) 2703static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2858{ 2704{
@@ -2922,9 +2768,9 @@ static void edge_buf_free(struct edge_buf *eb)
2922 2768
2923static void edge_buf_clear(struct edge_buf *eb) 2769static void edge_buf_clear(struct edge_buf *eb)
2924{ 2770{
2925 if (eb != NULL) 2771 if (eb != NULL)
2926 eb->buf_get = eb->buf_put; 2772 eb->buf_get = eb->buf_put;
2927 /* equivalent to a get of all data available */ 2773 /* equivalent to a get of all data available */
2928} 2774}
2929 2775
2930 2776
@@ -2937,10 +2783,9 @@ static void edge_buf_clear(struct edge_buf *eb)
2937 2783
2938static unsigned int edge_buf_data_avail(struct edge_buf *eb) 2784static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2939{ 2785{
2940 if (eb != NULL) 2786 if (eb == NULL)
2941 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2942 else
2943 return 0; 2787 return 0;
2788 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
2944} 2789}
2945 2790
2946 2791
@@ -2953,10 +2798,9 @@ static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2953 2798
2954static unsigned int edge_buf_space_avail(struct edge_buf *eb) 2799static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2955{ 2800{
2956 if (eb != NULL) 2801 if (eb == NULL)
2957 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2958 else
2959 return 0; 2802 return 0;
2803 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
2960} 2804}
2961 2805
2962 2806
@@ -3113,7 +2957,7 @@ static int __init edgeport_init(void)
3113 if (retval) 2957 if (retval)
3114 goto failed_2port_device_register; 2958 goto failed_2port_device_register;
3115 retval = usb_register(&io_driver); 2959 retval = usb_register(&io_driver);
3116 if (retval) 2960 if (retval)
3117 goto failed_usb_register; 2961 goto failed_usb_register;
3118 info(DRIVER_DESC " " DRIVER_VERSION); 2962 info(DRIVER_DESC " " DRIVER_VERSION);
3119 return 0; 2963 return 0;
@@ -3125,11 +2969,11 @@ failed_1port_device_register:
3125 return retval; 2969 return retval;
3126} 2970}
3127 2971
3128static void __exit edgeport_exit (void) 2972static void __exit edgeport_exit(void)
3129{ 2973{
3130 usb_deregister (&io_driver); 2974 usb_deregister(&io_driver);
3131 usb_serial_deregister (&edgeport_1port_device); 2975 usb_serial_deregister(&edgeport_1port_device);
3132 usb_serial_deregister (&edgeport_2port_device); 2976 usb_serial_deregister(&edgeport_2port_device);
3133} 2977}
3134 2978
3135module_init(edgeport_init); 2979module_init(edgeport_init);
@@ -3151,8 +2995,8 @@ module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3151MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs"); 2995MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3152 2996
3153module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR); 2997module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
3154MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device"); 2998MODULE_PARM_DESC(ignore_cpu_rev,
2999 "Ignore the cpu revision when connecting to a device");
3155 3000
3156module_param(default_uart_mode, int, S_IRUGO | S_IWUSR); 3001module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3157MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ..."); 3002MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
3158
diff --git a/drivers/usb/serial/ipaq.c b/drivers/usb/serial/ipaq.c
index d9fb3768a2d7..80d9ec5570d6 100644
--- a/drivers/usb/serial/ipaq.c
+++ b/drivers/usb/serial/ipaq.c
@@ -646,12 +646,13 @@ static int ipaq_open(struct usb_serial_port *port, struct file *filp)
646 */ 646 */
647 647
648 kfree(port->bulk_in_buffer); 648 kfree(port->bulk_in_buffer);
649 kfree(port->bulk_out_buffer);
650 port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL); 649 port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
651 if (port->bulk_in_buffer == NULL) { 650 if (port->bulk_in_buffer == NULL) {
652 port->bulk_out_buffer = NULL; /* prevent double free */ 651 port->bulk_out_buffer = NULL; /* prevent double free */
653 goto enomem; 652 goto enomem;
654 } 653 }
654
655 kfree(port->bulk_out_buffer);
655 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL); 656 port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
656 if (port->bulk_out_buffer == NULL) { 657 if (port->bulk_out_buffer == NULL) {
657 kfree(port->bulk_in_buffer); 658 kfree(port->bulk_in_buffer);
diff --git a/drivers/usb/serial/ir-usb.c b/drivers/usb/serial/ir-usb.c
index 004d57385a75..0063c11c8081 100644
--- a/drivers/usb/serial/ir-usb.c
+++ b/drivers/usb/serial/ir-usb.c
@@ -19,7 +19,12 @@
19 * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli 19 * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
20 * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com> 20 * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
21 * 21 *
22 * See Documentation/usb/usb-serial.txt for more information on using this driver 22 * See Documentation/usb/usb-serial.txt for more information on using this
23 * driver
24 *
25 * 2008_Jun_02 Felipe Balbi <me@felipebalbi.com>
26 * Introduced common header to be used also in USB Gadget Framework.
27 * Still needs some other style fixes.
23 * 28 *
24 * 2007_Jun_21 Alan Cox <alan@redhat.com> 29 * 2007_Jun_21 Alan Cox <alan@redhat.com>
25 * Minimal cleanups for some of the driver problens and tty layer abuse. 30 * Minimal cleanups for some of the driver problens and tty layer abuse.
@@ -59,9 +64,10 @@
59#include <linux/tty_flip.h> 64#include <linux/tty_flip.h>
60#include <linux/module.h> 65#include <linux/module.h>
61#include <linux/spinlock.h> 66#include <linux/spinlock.h>
62#include <asm/uaccess.h> 67#include <linux/uaccess.h>
63#include <linux/usb.h> 68#include <linux/usb.h>
64#include <linux/usb/serial.h> 69#include <linux/usb/serial.h>
70#include <linux/usb/irda.h>
65 71
66/* 72/*
67 * Version Information 73 * Version Information
@@ -70,100 +76,75 @@
70#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>" 76#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
71#define DRIVER_DESC "USB IR Dongle driver" 77#define DRIVER_DESC "USB IR Dongle driver"
72 78
73/* USB IrDA class spec information */
74#define USB_CLASS_IRDA 0x02
75#define USB_DT_IRDA 0x21
76#define IU_REQ_GET_CLASS_DESC 0x06
77#define SPEED_2400 0x01
78#define SPEED_9600 0x02
79#define SPEED_19200 0x03
80#define SPEED_38400 0x04
81#define SPEED_57600 0x05
82#define SPEED_115200 0x06
83#define SPEED_576000 0x07
84#define SPEED_1152000 0x08
85#define SPEED_4000000 0x09
86
87struct irda_class_desc {
88 u8 bLength;
89 u8 bDescriptorType;
90 u16 bcdSpecRevision;
91 u8 bmDataSize;
92 u8 bmWindowSize;
93 u8 bmMinTurnaroundTime;
94 u16 wBaudRate;
95 u8 bmAdditionalBOFs;
96 u8 bIrdaRateSniff;
97 u8 bMaxUnicastList;
98} __attribute__ ((packed));
99
100static int debug; 79static int debug;
101 80
102/* if overridden by the user, then use their value for the size of the read and 81/* if overridden by the user, then use their value for the size of the read and
103 * write urbs */ 82 * write urbs */
104static int buffer_size; 83static int buffer_size;
84
105/* if overridden by the user, then use the specified number of XBOFs */ 85/* if overridden by the user, then use the specified number of XBOFs */
106static int xbof = -1; 86static int xbof = -1;
107 87
108static int ir_startup (struct usb_serial *serial); 88static int ir_startup(struct usb_serial *serial);
109static int ir_open (struct usb_serial_port *port, struct file *filep); 89static int ir_open(struct usb_serial_port *port, struct file *filep);
110static void ir_close (struct usb_serial_port *port, struct file *filep); 90static void ir_close(struct usb_serial_port *port, struct file *filep);
111static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count); 91static int ir_write(struct usb_serial_port *port,
112static void ir_write_bulk_callback (struct urb *urb); 92 const unsigned char *buf, int count);
113static void ir_read_bulk_callback (struct urb *urb); 93static void ir_write_bulk_callback(struct urb *urb);
114static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios); 94static void ir_read_bulk_callback(struct urb *urb);
95static void ir_set_termios(struct usb_serial_port *port,
96 struct ktermios *old_termios);
115 97
116/* Not that this lot means you can only have one per system */ 98/* Not that this lot means you can only have one per system */
117static u8 ir_baud = 0; 99static u8 ir_baud;
118static u8 ir_xbof = 0; 100static u8 ir_xbof;
119static u8 ir_add_bof = 0; 101static u8 ir_add_bof;
120 102
121static struct usb_device_id id_table [] = { 103static struct usb_device_id ir_id_table[] = {
122 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */ 104 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
123 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */ 105 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
124 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */ 106 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
125 { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) }, 107 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
126 { } /* Terminating entry */ 108 { } /* Terminating entry */
127}; 109};
128 110
129MODULE_DEVICE_TABLE (usb, id_table); 111MODULE_DEVICE_TABLE(usb, ir_id_table);
130 112
131static struct usb_driver ir_driver = { 113static struct usb_driver ir_driver = {
132 .name = "ir-usb", 114 .name = "ir-usb",
133 .probe = usb_serial_probe, 115 .probe = usb_serial_probe,
134 .disconnect = usb_serial_disconnect, 116 .disconnect = usb_serial_disconnect,
135 .id_table = id_table, 117 .id_table = ir_id_table,
136 .no_dynamic_id = 1, 118 .no_dynamic_id = 1,
137}; 119};
138 120
139
140static struct usb_serial_driver ir_device = { 121static struct usb_serial_driver ir_device = {
141 .driver = { 122 .driver = {
142 .owner = THIS_MODULE, 123 .owner = THIS_MODULE,
143 .name = "ir-usb", 124 .name = "ir-usb",
144 }, 125 },
145 .description = "IR Dongle", 126 .description = "IR Dongle",
146 .usb_driver = &ir_driver, 127 .usb_driver = &ir_driver,
147 .id_table = id_table, 128 .id_table = ir_id_table,
148 .num_ports = 1, 129 .num_ports = 1,
149 .set_termios = ir_set_termios, 130 .set_termios = ir_set_termios,
150 .attach = ir_startup, 131 .attach = ir_startup,
151 .open = ir_open, 132 .open = ir_open,
152 .close = ir_close, 133 .close = ir_close,
153 .write = ir_write, 134 .write = ir_write,
154 .write_bulk_callback = ir_write_bulk_callback, 135 .write_bulk_callback = ir_write_bulk_callback,
155 .read_bulk_callback = ir_read_bulk_callback, 136 .read_bulk_callback = ir_read_bulk_callback,
156}; 137};
157 138
158static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc) 139static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
159{ 140{
160 dbg("bLength=%x", desc->bLength); 141 dbg("bLength=%x", desc->bLength);
161 dbg("bDescriptorType=%x", desc->bDescriptorType); 142 dbg("bDescriptorType=%x", desc->bDescriptorType);
162 dbg("bcdSpecRevision=%x", desc->bcdSpecRevision); 143 dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
163 dbg("bmDataSize=%x", desc->bmDataSize); 144 dbg("bmDataSize=%x", desc->bmDataSize);
164 dbg("bmWindowSize=%x", desc->bmWindowSize); 145 dbg("bmWindowSize=%x", desc->bmWindowSize);
165 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime); 146 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
166 dbg("wBaudRate=%x", desc->wBaudRate); 147 dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
167 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs); 148 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
168 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff); 149 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
169 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList); 150 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
@@ -181,35 +162,37 @@ static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
181 * 162 *
182 * Based on the same function in drivers/net/irda/irda-usb.c 163 * Based on the same function in drivers/net/irda/irda-usb.c
183 */ 164 */
184static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum) 165static struct usb_irda_cs_descriptor *
166irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
185{ 167{
186 struct irda_class_desc *desc; 168 struct usb_irda_cs_descriptor *desc;
187 int ret; 169 int ret;
188 170
189 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL); 171 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
190 if (desc == NULL) 172 if (!desc)
191 return NULL; 173 return NULL;
192 174
193 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0), 175 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
194 IU_REQ_GET_CLASS_DESC, 176 USB_REQ_CS_IRDA_GET_CLASS_DESC,
195 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 177 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
196 0, ifnum, desc, sizeof(*desc), 1000); 178 0, ifnum, desc, sizeof(*desc), 1000);
197 179
198 dbg("%s - ret=%d", __func__, ret); 180 dbg("%s - ret=%d", __func__, ret);
199 if (ret < sizeof(*desc)) { 181 if (ret < sizeof(*desc)) {
200 dbg("%s - class descriptor read %s (%d)", 182 dbg("%s - class descriptor read %s (%d)",
201 __func__, 183 __func__,
202 (ret<0) ? "failed" : "too short", 184 (ret < 0) ? "failed" : "too short",
203 ret); 185 ret);
204 goto error; 186 goto error;
205 } 187 }
206 if (desc->bDescriptorType != USB_DT_IRDA) { 188 if (desc->bDescriptorType != USB_DT_CS_IRDA) {
207 dbg("%s - bad class descriptor type", __func__); 189 dbg("%s - bad class descriptor type", __func__);
208 goto error; 190 goto error;
209 } 191 }
210 192
211 irda_usb_dump_class_desc(desc); 193 irda_usb_dump_class_desc(desc);
212 return desc; 194 return desc;
195
213error: 196error:
214 kfree(desc); 197 kfree(desc);
215 return NULL; 198 return NULL;
@@ -219,64 +202,100 @@ error:
219static u8 ir_xbof_change(u8 xbof) 202static u8 ir_xbof_change(u8 xbof)
220{ 203{
221 u8 result; 204 u8 result;
205
222 /* reference irda-usb.c */ 206 /* reference irda-usb.c */
223 switch(xbof) { 207 switch (xbof) {
224 case 48: result = 0x10; break; 208 case 48:
225 case 28: 209 result = 0x10;
226 case 24: result = 0x20; break; 210 break;
227 default: 211 case 28:
228 case 12: result = 0x30; break; 212 case 24:
229 case 5: 213 result = 0x20;
230 case 6: result = 0x40; break; 214 break;
231 case 3: result = 0x50; break; 215 default:
232 case 2: result = 0x60; break; 216 case 12:
233 case 1: result = 0x70; break; 217 result = 0x30;
234 case 0: result = 0x80; break; 218 break;
219 case 5:
220 case 6:
221 result = 0x40;
222 break;
223 case 3:
224 result = 0x50;
225 break;
226 case 2:
227 result = 0x60;
228 break;
229 case 1:
230 result = 0x70;
231 break;
232 case 0:
233 result = 0x80;
234 break;
235 } 235 }
236
236 return(result); 237 return(result);
237} 238}
238 239
239 240
240static int ir_startup (struct usb_serial *serial) 241static int ir_startup(struct usb_serial *serial)
241{ 242{
242 struct irda_class_desc *irda_desc; 243 struct usb_irda_cs_descriptor *irda_desc;
243 244
244 irda_desc = irda_usb_find_class_desc (serial->dev, 0); 245 irda_desc = irda_usb_find_class_desc(serial->dev, 0);
245 if (irda_desc == NULL) { 246 if (!irda_desc) {
246 dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n"); 247 dev_err(&serial->dev->dev,
248 "IRDA class descriptor not found, device not bound\n");
247 return -ENODEV; 249 return -ENODEV;
248 } 250 }
249 251
250 dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s", 252 dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
251 __func__, 253 __func__,
252 (irda_desc->wBaudRate & 0x0001) ? " 2400" : "", 254 (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
253 (irda_desc->wBaudRate & 0x0002) ? " 9600" : "", 255 (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
254 (irda_desc->wBaudRate & 0x0004) ? " 19200" : "", 256 (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
255 (irda_desc->wBaudRate & 0x0008) ? " 38400" : "", 257 (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
256 (irda_desc->wBaudRate & 0x0010) ? " 57600" : "", 258 (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
257 (irda_desc->wBaudRate & 0x0020) ? " 115200" : "", 259 (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
258 (irda_desc->wBaudRate & 0x0040) ? " 576000" : "", 260 (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
259 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "", 261 (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
260 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : ""); 262 (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
261 263
262 switch( irda_desc->bmAdditionalBOFs ) { 264 switch (irda_desc->bmAdditionalBOFs) {
263 case 0x01: ir_add_bof = 48; break; 265 case USB_IRDA_AB_48:
264 case 0x02: ir_add_bof = 24; break; 266 ir_add_bof = 48;
265 case 0x04: ir_add_bof = 12; break; 267 break;
266 case 0x08: ir_add_bof = 6; break; 268 case USB_IRDA_AB_24:
267 case 0x10: ir_add_bof = 3; break; 269 ir_add_bof = 24;
268 case 0x20: ir_add_bof = 2; break; 270 break;
269 case 0x40: ir_add_bof = 1; break; 271 case USB_IRDA_AB_12:
270 case 0x80: ir_add_bof = 0; break; 272 ir_add_bof = 12;
271 default:; 273 break;
274 case USB_IRDA_AB_6:
275 ir_add_bof = 6;
276 break;
277 case USB_IRDA_AB_3:
278 ir_add_bof = 3;
279 break;
280 case USB_IRDA_AB_2:
281 ir_add_bof = 2;
282 break;
283 case USB_IRDA_AB_1:
284 ir_add_bof = 1;
285 break;
286 case USB_IRDA_AB_0:
287 ir_add_bof = 0;
288 break;
289 default:
290 break;
272 } 291 }
273 292
274 kfree (irda_desc); 293 kfree(irda_desc);
275 294
276 return 0; 295 return 0;
277} 296}
278 297
279static int ir_open (struct usb_serial_port *port, struct file *filp) 298static int ir_open(struct usb_serial_port *port, struct file *filp)
280{ 299{
281 char *buffer; 300 char *buffer;
282 int result = 0; 301 int result = 0;
@@ -285,51 +304,55 @@ static int ir_open (struct usb_serial_port *port, struct file *filp)
285 304
286 if (buffer_size) { 305 if (buffer_size) {
287 /* override the default buffer sizes */ 306 /* override the default buffer sizes */
288 buffer = kmalloc (buffer_size, GFP_KERNEL); 307 buffer = kmalloc(buffer_size, GFP_KERNEL);
289 if (!buffer) { 308 if (!buffer) {
290 dev_err (&port->dev, "%s - out of memory.\n", __func__); 309 dev_err(&port->dev, "%s - out of memory.\n", __func__);
291 return -ENOMEM; 310 return -ENOMEM;
292 } 311 }
293 kfree (port->read_urb->transfer_buffer); 312 kfree(port->read_urb->transfer_buffer);
294 port->read_urb->transfer_buffer = buffer; 313 port->read_urb->transfer_buffer = buffer;
295 port->read_urb->transfer_buffer_length = buffer_size; 314 port->read_urb->transfer_buffer_length = buffer_size;
296 315
297 buffer = kmalloc (buffer_size, GFP_KERNEL); 316 buffer = kmalloc(buffer_size, GFP_KERNEL);
298 if (!buffer) { 317 if (!buffer) {
299 dev_err (&port->dev, "%s - out of memory.\n", __func__); 318 dev_err(&port->dev, "%s - out of memory.\n", __func__);
300 return -ENOMEM; 319 return -ENOMEM;
301 } 320 }
302 kfree (port->write_urb->transfer_buffer); 321 kfree(port->write_urb->transfer_buffer);
303 port->write_urb->transfer_buffer = buffer; 322 port->write_urb->transfer_buffer = buffer;
304 port->write_urb->transfer_buffer_length = buffer_size; 323 port->write_urb->transfer_buffer_length = buffer_size;
305 port->bulk_out_size = buffer_size; 324 port->bulk_out_size = buffer_size;
306 } 325 }
307 326
308 /* Start reading from the device */ 327 /* Start reading from the device */
309 usb_fill_bulk_urb ( 328 usb_fill_bulk_urb(
310 port->read_urb, 329 port->read_urb,
311 port->serial->dev, 330 port->serial->dev,
312 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress), 331 usb_rcvbulkpipe(port->serial->dev,
332 port->bulk_in_endpointAddress),
313 port->read_urb->transfer_buffer, 333 port->read_urb->transfer_buffer,
314 port->read_urb->transfer_buffer_length, 334 port->read_urb->transfer_buffer_length,
315 ir_read_bulk_callback, 335 ir_read_bulk_callback,
316 port); 336 port);
317 result = usb_submit_urb(port->read_urb, GFP_KERNEL); 337 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
318 if (result) 338 if (result)
319 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result); 339 dev_err(&port->dev,
340 "%s - failed submitting read urb, error %d\n",
341 __func__, result);
320 342
321 return result; 343 return result;
322} 344}
323 345
324static void ir_close (struct usb_serial_port *port, struct file * filp) 346static void ir_close(struct usb_serial_port *port, struct file *filp)
325{ 347{
326 dbg("%s - port %d", __func__, port->number); 348 dbg("%s - port %d", __func__, port->number);
327 349
328 /* shutdown our bulk read */ 350 /* shutdown our bulk read */
329 usb_kill_urb(port->read_urb); 351 usb_kill_urb(port->read_urb);
330} 352}
331 353
332static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count) 354static int ir_write(struct usb_serial_port *port,
355 const unsigned char *buf, int count)
333{ 356{
334 unsigned char *transfer_buffer; 357 unsigned char *transfer_buffer;
335 int result; 358 int result;
@@ -338,7 +361,7 @@ static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int
338 dbg("%s - port = %d, count = %d", __func__, port->number, count); 361 dbg("%s - port = %d, count = %d", __func__, port->number, count);
339 362
340 if (!port->tty) { 363 if (!port->tty) {
341 dev_err (&port->dev, "%s - no tty???\n", __func__); 364 dev_err(&port->dev, "%s - no tty???\n", __func__);
342 return 0; 365 return 0;
343 } 366 }
344 367
@@ -359,7 +382,7 @@ static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int
359 382
360 /* 383 /*
361 * The first byte of the packet we send to the device contains an 384 * The first byte of the packet we send to the device contains an
362 * inband header which indicates an additional number of BOFs and 385 * inbound header which indicates an additional number of BOFs and
363 * a baud rate change. 386 * a baud rate change.
364 * 387 *
365 * See section 5.4.2.2 of the USB IrDA spec. 388 * See section 5.4.2.2 of the USB IrDA spec.
@@ -367,9 +390,9 @@ static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int
367 *transfer_buffer = ir_xbof | ir_baud; 390 *transfer_buffer = ir_xbof | ir_baud;
368 ++transfer_buffer; 391 ++transfer_buffer;
369 392
370 memcpy (transfer_buffer, buf, transfer_size); 393 memcpy(transfer_buffer, buf, transfer_size);
371 394
372 usb_fill_bulk_urb ( 395 usb_fill_bulk_urb(
373 port->write_urb, 396 port->write_urb,
374 port->serial->dev, 397 port->serial->dev,
375 usb_sndbulkpipe(port->serial->dev, 398 usb_sndbulkpipe(port->serial->dev,
@@ -381,17 +404,19 @@ static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int
381 404
382 port->write_urb->transfer_flags = URB_ZERO_PACKET; 405 port->write_urb->transfer_flags = URB_ZERO_PACKET;
383 406
384 result = usb_submit_urb (port->write_urb, GFP_ATOMIC); 407 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
385 if (result) { 408 if (result) {
386 port->write_urb_busy = 0; 409 port->write_urb_busy = 0;
387 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result); 410 dev_err(&port->dev,
411 "%s - failed submitting write urb, error %d\n",
412 __func__, result);
388 } else 413 } else
389 result = transfer_size; 414 result = transfer_size;
390 415
391 return result; 416 return result;
392} 417}
393 418
394static void ir_write_bulk_callback (struct urb *urb) 419static void ir_write_bulk_callback(struct urb *urb)
395{ 420{
396 struct usb_serial_port *port = urb->context; 421 struct usb_serial_port *port = urb->context;
397 int status = urb->status; 422 int status = urb->status;
@@ -405,7 +430,7 @@ static void ir_write_bulk_callback (struct urb *urb)
405 return; 430 return;
406 } 431 }
407 432
408 usb_serial_debug_data ( 433 usb_serial_debug_data(
409 debug, 434 debug,
410 &port->dev, 435 &port->dev,
411 __func__, 436 __func__,
@@ -415,7 +440,7 @@ static void ir_write_bulk_callback (struct urb *urb)
415 usb_serial_port_softint(port); 440 usb_serial_port_softint(port);
416} 441}
417 442
418static void ir_read_bulk_callback (struct urb *urb) 443static void ir_read_bulk_callback(struct urb *urb)
419{ 444{
420 struct usb_serial_port *port = urb->context; 445 struct usb_serial_port *port = urb->context;
421 struct tty_struct *tty; 446 struct tty_struct *tty;
@@ -431,68 +456,69 @@ static void ir_read_bulk_callback (struct urb *urb)
431 } 456 }
432 457
433 switch (status) { 458 switch (status) {
434 case 0: /* Successful */ 459 case 0: /* Successful */
435 460
436 /* 461 /*
437 * The first byte of the packet we get from the device 462 * The first byte of the packet we get from the device
438 * contains a busy indicator and baud rate change. 463 * contains a busy indicator and baud rate change.
439 * See section 5.4.1.2 of the USB IrDA spec. 464 * See section 5.4.1.2 of the USB IrDA spec.
440 */ 465 */
441 if ((*data & 0x0f) > 0) 466 if ((*data & 0x0f) > 0)
442 ir_baud = *data & 0x0f; 467 ir_baud = *data & 0x0f;
443 468
444 usb_serial_debug_data ( 469 usb_serial_debug_data(
445 debug, 470 debug,
446 &port->dev, 471 &port->dev,
447 __func__, 472 __func__,
448 urb->actual_length, 473 urb->actual_length,
449 data); 474 data);
450 475
451 tty = port->tty; 476 tty = port->tty;
452 477
453 if (tty_buffer_request_room(tty, urb->actual_length - 1)) { 478 if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
454 tty_insert_flip_string(tty, data+1, urb->actual_length - 1); 479 tty_insert_flip_string(tty, data + 1,
455 tty_flip_buffer_push(tty); 480 urb->actual_length - 1);
456 } 481 tty_flip_buffer_push(tty);
457 482 }
458 /*
459 * No break here.
460 * We want to resubmit the urb so we can read
461 * again.
462 */
463
464 case -EPROTO: /* taking inspiration from pl2303.c */
465
466 /* Continue trying to always read */
467 usb_fill_bulk_urb (
468 port->read_urb,
469 port->serial->dev,
470 usb_rcvbulkpipe(port->serial->dev,
471 port->bulk_in_endpointAddress),
472 port->read_urb->transfer_buffer,
473 port->read_urb->transfer_buffer_length,
474 ir_read_bulk_callback,
475 port);
476
477 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
478 if (result)
479 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
480 __func__, result);
481
482 break ;
483
484 default:
485 dbg("%s - nonzero read bulk status received: %d",
486 __func__,
487 status);
488 break ;
489 483
484 /*
485 * No break here.
486 * We want to resubmit the urb so we can read
487 * again.
488 */
489
490 case -EPROTO: /* taking inspiration from pl2303.c */
491
492 /* Continue trying to always read */
493 usb_fill_bulk_urb(
494 port->read_urb,
495 port->serial->dev,
496 usb_rcvbulkpipe(port->serial->dev,
497 port->bulk_in_endpointAddress),
498 port->read_urb->transfer_buffer,
499 port->read_urb->transfer_buffer_length,
500 ir_read_bulk_callback,
501 port);
502
503 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
504 if (result)
505 dev_err(&port->dev,
506 "%s - failed resubmitting read urb, error %d\n",
507 __func__, result);
508 break;
509
510 default:
511 dbg("%s - nonzero read bulk status received: %d",
512 __func__,
513 status);
514 break;
490 } 515 }
491 516
492 return; 517 return;
493} 518}
494 519
495static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios) 520static void ir_set_termios(struct usb_serial_port *port,
521 struct ktermios *old_termios)
496{ 522{
497 unsigned char *transfer_buffer; 523 unsigned char *transfer_buffer;
498 int result; 524 int result;
@@ -510,19 +536,36 @@ static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_t
510 */ 536 */
511 537
512 switch (baud) { 538 switch (baud) {
513 case 2400: ir_baud = SPEED_2400; break; 539 case 2400:
514 case 9600: ir_baud = SPEED_9600; break; 540 ir_baud = USB_IRDA_BR_2400;
515 case 19200: ir_baud = SPEED_19200; break; 541 break;
516 case 38400: ir_baud = SPEED_38400; break; 542 case 9600:
517 case 57600: ir_baud = SPEED_57600; break; 543 ir_baud = USB_IRDA_BR_9600;
518 case 115200: ir_baud = SPEED_115200; break; 544 break;
519 case 576000: ir_baud = SPEED_576000; break; 545 case 19200:
520 case 1152000: ir_baud = SPEED_1152000; break; 546 ir_baud = USB_IRDA_BR_19200;
521 case 4000000: ir_baud = SPEED_4000000; break; 547 break;
522 break; 548 case 38400:
523 default: 549 ir_baud = USB_IRDA_BR_38400;
524 ir_baud = SPEED_9600; 550 break;
525 baud = 9600; 551 case 57600:
552 ir_baud = USB_IRDA_BR_57600;
553 break;
554 case 115200:
555 ir_baud = USB_IRDA_BR_115200;
556 break;
557 case 576000:
558 ir_baud = USB_IRDA_BR_576000;
559 break;
560 case 1152000:
561 ir_baud = USB_IRDA_BR_1152000;
562 break;
563 case 4000000:
564 ir_baud = USB_IRDA_BR_4000000;
565 break;
566 default:
567 ir_baud = USB_IRDA_BR_9600;
568 baud = 9600;
526 } 569 }
527 570
528 if (xbof == -1) 571 if (xbof == -1)
@@ -538,10 +581,11 @@ static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_t
538 transfer_buffer = port->write_urb->transfer_buffer; 581 transfer_buffer = port->write_urb->transfer_buffer;
539 *transfer_buffer = ir_xbof | ir_baud; 582 *transfer_buffer = ir_xbof | ir_baud;
540 583
541 usb_fill_bulk_urb ( 584 usb_fill_bulk_urb(
542 port->write_urb, 585 port->write_urb,
543 port->serial->dev, 586 port->serial->dev,
544 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress), 587 usb_sndbulkpipe(port->serial->dev,
588 port->bulk_out_endpointAddress),
545 port->write_urb->transfer_buffer, 589 port->write_urb->transfer_buffer,
546 1, 590 1,
547 ir_write_bulk_callback, 591 ir_write_bulk_callback,
@@ -549,38 +593,44 @@ static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_t
549 593
550 port->write_urb->transfer_flags = URB_ZERO_PACKET; 594 port->write_urb->transfer_flags = URB_ZERO_PACKET;
551 595
552 result = usb_submit_urb (port->write_urb, GFP_KERNEL); 596 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
553 if (result) 597 if (result)
554 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result); 598 dev_err(&port->dev,
599 "%s - failed submitting write urb, error %d\n",
600 __func__, result);
555 601
556 /* Only speed changes are supported */ 602 /* Only speed changes are supported */
557 tty_termios_copy_hw(port->tty->termios, old_termios); 603 tty_termios_copy_hw(port->tty->termios, old_termios);
558 tty_encode_baud_rate(port->tty, baud, baud); 604 tty_encode_baud_rate(port->tty, baud, baud);
559} 605}
560 606
561 607static int __init ir_init(void)
562static int __init ir_init (void)
563{ 608{
564 int retval; 609 int retval;
610
565 retval = usb_serial_register(&ir_device); 611 retval = usb_serial_register(&ir_device);
566 if (retval) 612 if (retval)
567 goto failed_usb_serial_register; 613 goto failed_usb_serial_register;
614
568 retval = usb_register(&ir_driver); 615 retval = usb_register(&ir_driver);
569 if (retval) 616 if (retval)
570 goto failed_usb_register; 617 goto failed_usb_register;
618
571 info(DRIVER_DESC " " DRIVER_VERSION); 619 info(DRIVER_DESC " " DRIVER_VERSION);
620
572 return 0; 621 return 0;
622
573failed_usb_register: 623failed_usb_register:
574 usb_serial_deregister(&ir_device); 624 usb_serial_deregister(&ir_device);
625
575failed_usb_serial_register: 626failed_usb_serial_register:
576 return retval; 627 return retval;
577} 628}
578 629
579 630static void __exit ir_exit(void)
580static void __exit ir_exit (void)
581{ 631{
582 usb_deregister (&ir_driver); 632 usb_deregister(&ir_driver);
583 usb_serial_deregister (&ir_device); 633 usb_serial_deregister(&ir_device);
584} 634}
585 635
586 636
diff --git a/drivers/usb/serial/keyspan.h b/drivers/usb/serial/keyspan.h
index b52fb657a244..8bf72639b148 100644
--- a/drivers/usb/serial/keyspan.h
+++ b/drivers/usb/serial/keyspan.h
@@ -138,7 +138,8 @@ static int keyspan_usa67_send_setup (struct usb_serial *serial,
138 138
139/* Product IDs post-renumeration. Note that the 28x and 28xb 139/* Product IDs post-renumeration. Note that the 28x and 28xb
140 have the same id's post-renumeration but behave identically 140 have the same id's post-renumeration but behave identically
141 so it's not an issue. */ 141 so it's not an issue. As such, the 28xb is not listed in any
142 of the device tables. */
142#define keyspan_usa18x_product_id 0x0112 143#define keyspan_usa18x_product_id 0x0112
143#define keyspan_usa19_product_id 0x0107 144#define keyspan_usa19_product_id 0x0107
144#define keyspan_usa19qi_product_id 0x010c 145#define keyspan_usa19qi_product_id 0x010c
@@ -482,7 +483,6 @@ static struct usb_device_id keyspan_ids_combined[] = {
482 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) }, 483 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
483 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) }, 484 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
484 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) }, 485 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
485 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_product_id) },
486 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) }, 486 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
487 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id)}, 487 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id)},
488 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)}, 488 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)},
@@ -532,7 +532,6 @@ static struct usb_device_id keyspan_2port_ids[] = {
532 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) }, 532 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
533 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) }, 533 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
534 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) }, 534 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
535 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_product_id) },
536 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) }, 535 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
537 { } /* Terminating entry */ 536 { } /* Terminating entry */
538}; 537};
diff --git a/drivers/usb/serial/keyspan_pda.c b/drivers/usb/serial/keyspan_pda.c
index 644a1eaaa376..60b3e22bd633 100644
--- a/drivers/usb/serial/keyspan_pda.c
+++ b/drivers/usb/serial/keyspan_pda.c
@@ -408,7 +408,7 @@ static int keyspan_pda_get_modem_info(struct usb_serial *serial,
408 3, /* get pins */ 408 3, /* get pins */
409 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN, 409 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
410 0, 0, &data, 1, 2000); 410 0, 0, &data, 1, 2000);
411 if (rc > 0) 411 if (rc >= 0)
412 *value = data; 412 *value = data;
413 return rc; 413 return rc;
414} 414}
diff --git a/drivers/usb/serial/kl5kusb105.c b/drivers/usb/serial/kl5kusb105.c
index f328948d74e3..79787eda9524 100644
--- a/drivers/usb/serial/kl5kusb105.c
+++ b/drivers/usb/serial/kl5kusb105.c
@@ -236,7 +236,7 @@ static int klsi_105_get_line_state(struct usb_serial_port *port,
236 if (rc < 0) 236 if (rc < 0)
237 err("Reading line status failed (error = %d)", rc); 237 err("Reading line status failed (error = %d)", rc);
238 else { 238 else {
239 status = le16_to_cpu(get_unaligned((__le16 *)status_buf)); 239 status = get_unaligned_le16(status_buf);
240 240
241 info("%s - read status %x %x", __func__, 241 info("%s - read status %x %x", __func__,
242 status_buf[0], status_buf[1]); 242 status_buf[0], status_buf[1]);
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index a73420dd052a..1e936a1cbe0b 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -173,6 +173,7 @@ static int option_send_setup(struct usb_serial_port *port);
173#define DELL_VENDOR_ID 0x413C 173#define DELL_VENDOR_ID 0x413C
174 174
175#define KYOCERA_VENDOR_ID 0x0c88 175#define KYOCERA_VENDOR_ID 0x0c88
176#define KYOCERA_PRODUCT_KPC650 0x17da
176#define KYOCERA_PRODUCT_KPC680 0x180a 177#define KYOCERA_PRODUCT_KPC680 0x180a
177 178
178#define ANYDATA_VENDOR_ID 0x16d5 179#define ANYDATA_VENDOR_ID 0x16d5
@@ -305,6 +306,7 @@ static struct usb_device_id option_ids[] = {
305 { USB_DEVICE(ONDA_VENDOR_ID, ONDA_PRODUCT_ET502HS) }, 306 { USB_DEVICE(ONDA_VENDOR_ID, ONDA_PRODUCT_ET502HS) },
306 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_1) }, 307 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_1) },
307 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_2) }, 308 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_2) },
309 { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_PRODUCT_KPC650) },
308 { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_PRODUCT_KPC680) }, 310 { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_PRODUCT_KPC680) },
309 { USB_DEVICE(QUALCOMM_VENDOR_ID, 0x6000)}, /* ZTE AC8700 */ 311 { USB_DEVICE(QUALCOMM_VENDOR_ID, 0x6000)}, /* ZTE AC8700 */
310 { USB_DEVICE(QUALCOMM_VENDOR_ID, 0x6613)}, /* Onda H600/ZTE MF330 */ 312 { USB_DEVICE(QUALCOMM_VENDOR_ID, 0x6613)}, /* Onda H600/ZTE MF330 */
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 0cb0d77dc429..353798631903 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -283,7 +283,10 @@ static void serial_close(struct tty_struct *tty, struct file * filp)
283 } 283 }
284 284
285 if (port->open_count == 0) { 285 if (port->open_count == 0) {
286 usb_autopm_put_interface(port->serial->interface); 286 mutex_lock(&port->serial->disc_mutex);
287 if (!port->serial->disconnected)
288 usb_autopm_put_interface(port->serial->interface);
289 mutex_unlock(&port->serial->disc_mutex);
287 module_put(port->serial->type->driver.owner); 290 module_put(port->serial->type->driver.owner);
288 } 291 }
289 292
@@ -505,7 +508,7 @@ static void port_release(struct device *dev)
505{ 508{
506 struct usb_serial_port *port = to_usb_serial_port(dev); 509 struct usb_serial_port *port = to_usb_serial_port(dev);
507 510
508 dbg ("%s - %s", __func__, dev->bus_id); 511 dbg ("%s - %s", __func__, dev_name(dev));
509 port_free(port); 512 port_free(port);
510} 513}
511 514
@@ -625,7 +628,7 @@ int usb_serial_probe(struct usb_interface *interface,
625 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS]; 628 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
626 struct usb_serial_driver *type = NULL; 629 struct usb_serial_driver *type = NULL;
627 int retval; 630 int retval;
628 int minor; 631 unsigned int minor;
629 int buffer_size; 632 int buffer_size;
630 int i; 633 int i;
631 int num_interrupt_in = 0; 634 int num_interrupt_in = 0;
@@ -938,8 +941,8 @@ int usb_serial_probe(struct usb_interface *interface,
938 port->dev.bus = &usb_serial_bus_type; 941 port->dev.bus = &usb_serial_bus_type;
939 port->dev.release = &port_release; 942 port->dev.release = &port_release;
940 943
941 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number); 944 dev_set_name(&port->dev, "ttyUSB%d", port->number);
942 dbg ("%s - registering %s", __func__, port->dev.bus_id); 945 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
943 retval = device_register(&port->dev); 946 retval = device_register(&port->dev);
944 if (retval) 947 if (retval)
945 dev_err(&port->dev, "Error registering port device, " 948 dev_err(&port->dev, "Error registering port device, "
diff --git a/drivers/usb/serial/usb_debug.c b/drivers/usb/serial/usb_debug.c
index f9fc926b56d8..9ca4d4db1ddd 100644
--- a/drivers/usb/serial/usb_debug.c
+++ b/drivers/usb/serial/usb_debug.c
@@ -15,6 +15,8 @@
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include <linux/usb/serial.h> 16#include <linux/usb/serial.h>
17 17
18#define USB_DEBUG_MAX_PACKET_SIZE 8
19
18static struct usb_device_id id_table [] = { 20static struct usb_device_id id_table [] = {
19 { USB_DEVICE(0x0525, 0x127a) }, 21 { USB_DEVICE(0x0525, 0x127a) },
20 { }, 22 { },
@@ -29,6 +31,12 @@ static struct usb_driver debug_driver = {
29 .no_dynamic_id = 1, 31 .no_dynamic_id = 1,
30}; 32};
31 33
34int usb_debug_open(struct usb_serial_port *port, struct file *filp)
35{
36 port->bulk_out_size = USB_DEBUG_MAX_PACKET_SIZE;
37 return usb_serial_generic_open(port, filp);
38}
39
32static struct usb_serial_driver debug_device = { 40static struct usb_serial_driver debug_device = {
33 .driver = { 41 .driver = {
34 .owner = THIS_MODULE, 42 .owner = THIS_MODULE,
@@ -36,6 +44,7 @@ static struct usb_serial_driver debug_device = {
36 }, 44 },
37 .id_table = id_table, 45 .id_table = id_table,
38 .num_ports = 1, 46 .num_ports = 1,
47 .open = usb_debug_open,
39}; 48};
40 49
41static int __init debug_init(void) 50static int __init debug_init(void)
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 579e9f52053a..17f1ae232919 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -1,7 +1,5 @@
1/* Driver for Datafab USB Compact Flash reader 1/* Driver for Datafab USB Compact Flash reader
2 * 2 *
3 * $Id: datafab.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
4 *
5 * datafab driver v0.1: 3 * datafab driver v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/debug.c b/drivers/usb/storage/debug.c
index 01e430654a13..a2b5526c9fa0 100644
--- a/drivers/usb/storage/debug.c
+++ b/drivers/usb/storage/debug.c
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Debugging Functions Source Code File 2 * Debugging Functions Source Code File
3 * 3 *
4 * $Id: debug.c,v 1.9 2002/04/22 03:39:43 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
index 77e244a8c376..dbb985d52423 100644
--- a/drivers/usb/storage/debug.h
+++ b/drivers/usb/storage/debug.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Debugging Functions Header File 2 * Debugging Functions Header File
3 * 3 *
4 * $Id: debug.h,v 1.6 2001/01/12 23:51:04 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
diff --git a/drivers/usb/storage/dpcm.c b/drivers/usb/storage/dpcm.c
index 9a410b5a6e5b..939923471af4 100644
--- a/drivers/usb/storage/dpcm.c
+++ b/drivers/usb/storage/dpcm.c
@@ -1,7 +1,5 @@
1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader 1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader
2 * 2 *
3 * $Id: dpcm.c,v 1.4 2001/06/11 02:54:25 mdharm Exp $
4 *
5 * DPCM driver v0.1: 3 * DPCM driver v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/dpcm.h b/drivers/usb/storage/dpcm.h
index 81b464cfcc1e..e7b7b0f120d7 100644
--- a/drivers/usb/storage/dpcm.h
+++ b/drivers/usb/storage/dpcm.h
@@ -1,7 +1,5 @@
1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader 1/* Driver for Microtech DPCM-USB CompactFlash/SmartMedia reader
2 * 2 *
3 * $Id: dpcm.h,v 1.2 2000/08/25 00:13:51 mdharm Exp $
4 *
5 * DPCM driver v0.1: 3 * DPCM driver v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index f5a4e8d6a3b1..7a4d45677227 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -1,7 +1,5 @@
1/* Driver for Freecom USB/IDE adaptor 1/* Driver for Freecom USB/IDE adaptor
2 * 2 *
3 * $Id: freecom.c,v 1.22 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Freecom v0.1: 3 * Freecom v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/freecom.h b/drivers/usb/storage/freecom.h
index 1b012d62d0a8..20d0fe6ba0c8 100644
--- a/drivers/usb/storage/freecom.h
+++ b/drivers/usb/storage/freecom.h
@@ -1,7 +1,5 @@
1/* Driver for Freecom USB/IDE adaptor 1/* Driver for Freecom USB/IDE adaptor
2 * 2 *
3 * $Id: freecom.h,v 1.4 2000/08/29 14:49:15 dlbrown Exp $
4 *
5 * Freecom v0.1: 3 * Freecom v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/initializers.c b/drivers/usb/storage/initializers.c
index 187dd1e01093..4995bb595aef 100644
--- a/drivers/usb/storage/initializers.c
+++ b/drivers/usb/storage/initializers.c
@@ -1,7 +1,5 @@
1/* Special Initializers for certain USB Mass Storage devices 1/* Special Initializers for certain USB Mass Storage devices
2 * 2 *
3 * $Id: initializers.c,v 1.2 2000/09/06 22:35:57 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 4 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * 5 *
diff --git a/drivers/usb/storage/initializers.h b/drivers/usb/storage/initializers.h
index ad3ffd4236c2..529327fbb06b 100644
--- a/drivers/usb/storage/initializers.h
+++ b/drivers/usb/storage/initializers.h
@@ -1,7 +1,5 @@
1/* Header file for Special Initializers for certain USB Mass Storage devices 1/* Header file for Special Initializers for certain USB Mass Storage devices
2 * 2 *
3 * $Id: initializers.h,v 1.1 2000/08/29 23:07:02 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 4 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * 5 *
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 3addcd8f827b..383abf2516a5 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -1,7 +1,5 @@
1/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC 1/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
2 * 2 *
3 * $Id: isd200.c,v 1.16 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance: 3 * Current development and maintenance:
6 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se) 4 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
7 * 5 *
@@ -586,7 +584,7 @@ static void isd200_invoke_transport( struct us_data *us,
586 /* if the command gets aborted by the higher layers, we need to 584 /* if the command gets aborted by the higher layers, we need to
587 * short-circuit all other processing 585 * short-circuit all other processing
588 */ 586 */
589 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 587 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
590 US_DEBUGP("-- command was aborted\n"); 588 US_DEBUGP("-- command was aborted\n");
591 goto Handle_Abort; 589 goto Handle_Abort;
592 } 590 }
@@ -633,7 +631,7 @@ static void isd200_invoke_transport( struct us_data *us,
633 631
634 if (need_auto_sense) { 632 if (need_auto_sense) {
635 result = isd200_read_regs(us); 633 result = isd200_read_regs(us);
636 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 634 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
637 US_DEBUGP("-- auto-sense aborted\n"); 635 US_DEBUGP("-- auto-sense aborted\n");
638 goto Handle_Abort; 636 goto Handle_Abort;
639 } 637 }
@@ -663,7 +661,7 @@ static void isd200_invoke_transport( struct us_data *us,
663 srb->result = DID_ABORT << 16; 661 srb->result = DID_ABORT << 16;
664 662
665 /* permit the reset transfer to take place */ 663 /* permit the reset transfer to take place */
666 clear_bit(US_FLIDX_ABORTING, &us->flags); 664 clear_bit(US_FLIDX_ABORTING, &us->dflags);
667 /* Need reset here */ 665 /* Need reset here */
668} 666}
669 667
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 61097cbb1585..df67f13c9e73 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -1,7 +1,5 @@
1/* Driver for Lexar "Jumpshot" Compact Flash reader 1/* Driver for Lexar "Jumpshot" Compact Flash reader
2 * 2 *
3 * $Id: jumpshot.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
4 *
5 * jumpshot driver v0.1: 3 * jumpshot driver v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/protocol.c b/drivers/usb/storage/protocol.c
index b9b8ede61fb3..3b3357e20ea7 100644
--- a/drivers/usb/storage/protocol.c
+++ b/drivers/usb/storage/protocol.c
@@ -1,7 +1,5 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * 2 *
3 * $Id: protocol.c,v 1.14 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * 5 *
diff --git a/drivers/usb/storage/protocol.h b/drivers/usb/storage/protocol.h
index 8737a36891ca..487056ffb516 100644
--- a/drivers/usb/storage/protocol.h
+++ b/drivers/usb/storage/protocol.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Protocol Functions Header File 2 * Protocol Functions Header File
3 * 3 *
4 * $Id: protocol.h,v 1.4 2001/02/13 07:10:03 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 3fcde9f0fa5f..09779f6a8179 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * SCSI layer glue code 2 * SCSI layer glue code
3 * 3 *
4 * $Id: scsiglue.c,v 1.26 2002/04/22 03:39:43 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
@@ -73,7 +71,6 @@ static const char* host_info(struct Scsi_Host *host)
73static int slave_alloc (struct scsi_device *sdev) 71static int slave_alloc (struct scsi_device *sdev)
74{ 72{
75 struct us_data *us = host_to_us(sdev->host); 73 struct us_data *us = host_to_us(sdev->host);
76 struct usb_host_endpoint *bulk_in_ep;
77 74
78 /* 75 /*
79 * Set the INQUIRY transfer length to 36. We don't use any of 76 * Set the INQUIRY transfer length to 36. We don't use any of
@@ -82,16 +79,22 @@ static int slave_alloc (struct scsi_device *sdev)
82 */ 79 */
83 sdev->inquiry_len = 36; 80 sdev->inquiry_len = 36;
84 81
85 /* Scatter-gather buffers (all but the last) must have a length 82 /* USB has unusual DMA-alignment requirements: Although the
86 * divisible by the bulk maxpacket size. Otherwise a data packet 83 * starting address of each scatter-gather element doesn't matter,
87 * would end up being short, causing a premature end to the data 84 * the length of each element except the last must be divisible
88 * transfer. We'll use the maxpacket value of the bulk-IN pipe 85 * by the Bulk maxpacket value. There's currently no way to
89 * to set the SCSI device queue's DMA alignment mask. 86 * express this by block-layer constraints, so we'll cop out
87 * and simply require addresses to be aligned at 512-byte
88 * boundaries. This is okay since most block I/O involves
89 * hardware sectors that are multiples of 512 bytes in length,
90 * and since host controllers up through USB 2.0 have maxpacket
91 * values no larger than 512.
92 *
93 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
94 * values can be as large as 2048. To make that work properly
95 * will require changes to the block layer.
90 */ 96 */
91 bulk_in_ep = us->pusb_dev->ep_in[usb_pipeendpoint(us->recv_bulk_pipe)]; 97 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
92 blk_queue_update_dma_alignment(sdev->request_queue,
93 le16_to_cpu(bulk_in_ep->desc.wMaxPacketSize) - 1);
94 /* wMaxPacketSize must be a power of 2 */
95 98
96 /* 99 /*
97 * The UFI spec treates the Peripheral Qualifier bits in an 100 * The UFI spec treates the Peripheral Qualifier bits in an
@@ -116,10 +119,10 @@ static int slave_configure(struct scsi_device *sdev)
116 * while others have trouble with more than 64K. At this time we 119 * while others have trouble with more than 64K. At this time we
117 * are limiting both to 32K (64 sectores). 120 * are limiting both to 32K (64 sectores).
118 */ 121 */
119 if (us->flags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) { 122 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
120 unsigned int max_sectors = 64; 123 unsigned int max_sectors = 64;
121 124
122 if (us->flags & US_FL_MAX_SECTORS_MIN) 125 if (us->fflags & US_FL_MAX_SECTORS_MIN)
123 max_sectors = PAGE_CACHE_SIZE >> 9; 126 max_sectors = PAGE_CACHE_SIZE >> 9;
124 if (sdev->request_queue->max_sectors > max_sectors) 127 if (sdev->request_queue->max_sectors > max_sectors)
125 blk_queue_max_sectors(sdev->request_queue, 128 blk_queue_max_sectors(sdev->request_queue,
@@ -148,7 +151,7 @@ static int slave_configure(struct scsi_device *sdev)
148 * majority of devices work fine, but a few still can't 151 * majority of devices work fine, but a few still can't
149 * handle it. The sd driver will simply assume those 152 * handle it. The sd driver will simply assume those
150 * devices are write-enabled. */ 153 * devices are write-enabled. */
151 if (us->flags & US_FL_NO_WP_DETECT) 154 if (us->fflags & US_FL_NO_WP_DETECT)
152 sdev->skip_ms_page_3f = 1; 155 sdev->skip_ms_page_3f = 1;
153 156
154 /* A number of devices have problems with MODE SENSE for 157 /* A number of devices have problems with MODE SENSE for
@@ -158,13 +161,13 @@ static int slave_configure(struct scsi_device *sdev)
158 /* Some disks return the total number of blocks in response 161 /* Some disks return the total number of blocks in response
159 * to READ CAPACITY rather than the highest block number. 162 * to READ CAPACITY rather than the highest block number.
160 * If this device makes that mistake, tell the sd driver. */ 163 * If this device makes that mistake, tell the sd driver. */
161 if (us->flags & US_FL_FIX_CAPACITY) 164 if (us->fflags & US_FL_FIX_CAPACITY)
162 sdev->fix_capacity = 1; 165 sdev->fix_capacity = 1;
163 166
164 /* A few disks have two indistinguishable version, one of 167 /* A few disks have two indistinguishable version, one of
165 * which reports the correct capacity and the other does not. 168 * which reports the correct capacity and the other does not.
166 * The sd driver has to guess which is the case. */ 169 * The sd driver has to guess which is the case. */
167 if (us->flags & US_FL_CAPACITY_HEURISTICS) 170 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
168 sdev->guess_capacity = 1; 171 sdev->guess_capacity = 1;
169 172
170 /* Some devices report a SCSI revision level above 2 but are 173 /* Some devices report a SCSI revision level above 2 but are
@@ -213,7 +216,7 @@ static int slave_configure(struct scsi_device *sdev)
213 216
214 /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM 217 /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
215 * REMOVAL command, so suppress those commands. */ 218 * REMOVAL command, so suppress those commands. */
216 if (us->flags & US_FL_NOT_LOCKABLE) 219 if (us->fflags & US_FL_NOT_LOCKABLE)
217 sdev->lockable = 0; 220 sdev->lockable = 0;
218 221
219 /* this is to satisfy the compiler, tho I don't think the 222 /* this is to satisfy the compiler, tho I don't think the
@@ -238,7 +241,7 @@ static int queuecommand(struct scsi_cmnd *srb,
238 } 241 }
239 242
240 /* fail the command if we are disconnecting */ 243 /* fail the command if we are disconnecting */
241 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 244 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
242 US_DEBUGP("Fail command during disconnect\n"); 245 US_DEBUGP("Fail command during disconnect\n");
243 srb->result = DID_NO_CONNECT << 16; 246 srb->result = DID_NO_CONNECT << 16;
244 done(srb); 247 done(srb);
@@ -248,7 +251,7 @@ static int queuecommand(struct scsi_cmnd *srb,
248 /* enqueue the command and wake up the control thread */ 251 /* enqueue the command and wake up the control thread */
249 srb->scsi_done = done; 252 srb->scsi_done = done;
250 us->srb = srb; 253 us->srb = srb;
251 up(&(us->sema)); 254 complete(&us->cmnd_ready);
252 255
253 return 0; 256 return 0;
254} 257}
@@ -280,9 +283,9 @@ static int command_abort(struct scsi_cmnd *srb)
280 * with the reset). Note that we must retain the host lock while 283 * with the reset). Note that we must retain the host lock while
281 * calling usb_stor_stop_transport(); otherwise it might interfere 284 * calling usb_stor_stop_transport(); otherwise it might interfere
282 * with an auto-reset that begins as soon as we release the lock. */ 285 * with an auto-reset that begins as soon as we release the lock. */
283 set_bit(US_FLIDX_TIMED_OUT, &us->flags); 286 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
284 if (!test_bit(US_FLIDX_RESETTING, &us->flags)) { 287 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
285 set_bit(US_FLIDX_ABORTING, &us->flags); 288 set_bit(US_FLIDX_ABORTING, &us->dflags);
286 usb_stor_stop_transport(us); 289 usb_stor_stop_transport(us);
287 } 290 }
288 scsi_unlock(us_to_host(us)); 291 scsi_unlock(us_to_host(us));
@@ -329,7 +332,7 @@ void usb_stor_report_device_reset(struct us_data *us)
329 struct Scsi_Host *host = us_to_host(us); 332 struct Scsi_Host *host = us_to_host(us);
330 333
331 scsi_report_device_reset(host, 0, 0); 334 scsi_report_device_reset(host, 0, 0);
332 if (us->flags & US_FL_SCM_MULT_TARG) { 335 if (us->fflags & US_FL_SCM_MULT_TARG) {
333 for (i = 1; i < host->max_id; ++i) 336 for (i = 1; i < host->max_id; ++i)
334 scsi_report_device_reset(host, 0, i); 337 scsi_report_device_reset(host, 0, i);
335 } 338 }
@@ -400,7 +403,7 @@ static int proc_info (struct Scsi_Host *host, char *buffer,
400 pos += sprintf(pos, " Quirks:"); 403 pos += sprintf(pos, " Quirks:");
401 404
402#define US_FLAG(name, value) \ 405#define US_FLAG(name, value) \
403 if (us->flags & value) pos += sprintf(pos, " " #name); 406 if (us->fflags & value) pos += sprintf(pos, " " #name);
404US_DO_ALL_FLAGS 407US_DO_ALL_FLAGS
405#undef US_FLAG 408#undef US_FLAG
406 409
diff --git a/drivers/usb/storage/scsiglue.h b/drivers/usb/storage/scsiglue.h
index 737e4fa6045f..ffa1cca93d2c 100644
--- a/drivers/usb/storage/scsiglue.h
+++ b/drivers/usb/storage/scsiglue.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * SCSI Connecting Glue Header File 2 * SCSI Connecting Glue Header File
3 * 3 *
4 * $Id: scsiglue.h,v 1.4 2000/08/25 00:13:51 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index 8972b17da843..c5a54b872c24 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -1,6 +1,5 @@
1/* Driver for SanDisk SDDR-09 SmartMedia reader 1/* Driver for SanDisk SDDR-09 SmartMedia reader
2 * 2 *
3 * $Id: sddr09.c,v 1.24 2002/04/22 03:39:43 mdharm Exp $
4 * (c) 2000, 2001 Robert Baruch (autophile@starband.net) 3 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
5 * (c) 2002 Andries Brouwer (aeb@cwi.nl) 4 * (c) 2002 Andries Brouwer (aeb@cwi.nl)
6 * Developed with the assistance of: 5 * Developed with the assistance of:
diff --git a/drivers/usb/storage/sddr09.h b/drivers/usb/storage/sddr09.h
index c03089a9ec38..e50033ad7b19 100644
--- a/drivers/usb/storage/sddr09.h
+++ b/drivers/usb/storage/sddr09.h
@@ -1,8 +1,6 @@
1/* Driver for SanDisk SDDR-09 SmartMedia reader 1/* Driver for SanDisk SDDR-09 SmartMedia reader
2 * Header File 2 * Header File
3 * 3 *
4 * $Id: sddr09.h,v 1.5 2000/08/25 00:13:51 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 2000 Robert Baruch (autophile@dol.net) 5 * (c) 2000 Robert Baruch (autophile@dol.net)
8 * (c) 2002 Andries Brouwer (aeb@cwi.nl) 6 * (c) 2002 Andries Brouwer (aeb@cwi.nl)
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index 6d14327c921d..0d8df7577899 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -1,7 +1,5 @@
1/* Driver for SanDisk SDDR-55 SmartMedia reader 1/* Driver for SanDisk SDDR-55 SmartMedia reader
2 * 2 *
3 * $Id:$
4 *
5 * SDDR55 driver v0.1: 3 * SDDR55 driver v0.1:
6 * 4 *
7 * First release 5 * First release
diff --git a/drivers/usb/storage/sddr55.h b/drivers/usb/storage/sddr55.h
index d6bd32f6c9f3..a815a0470c84 100644
--- a/drivers/usb/storage/sddr55.h
+++ b/drivers/usb/storage/sddr55.h
@@ -1,8 +1,6 @@
1/* Driver for SanDisk SDDR-55 SmartMedia reader 1/* Driver for SanDisk SDDR-55 SmartMedia reader
2 * Header File 2 * Header File
3 * 3 *
4 * $Id:$
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 2002 Simon Munton 5 * (c) 2002 Simon Munton
8 * 6 *
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 570c1250f6f3..ae6d64810d2a 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1,7 +1,5 @@
1/* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable 1/* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
2 * 2 *
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net) 4 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org> 5 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
diff --git a/drivers/usb/storage/shuttle_usbat.h b/drivers/usb/storage/shuttle_usbat.h
index 3ddf143a1dec..d8bfc43e9044 100644
--- a/drivers/usb/storage/shuttle_usbat.h
+++ b/drivers/usb/storage/shuttle_usbat.h
@@ -1,8 +1,6 @@
1/* Driver for SCM Microsystems USB-ATAPI cable 1/* Driver for SCM Microsystems USB-ATAPI cable
2 * Header File 2 * Header File
3 * 3 *
4 * $Id: shuttle_usbat.h,v 1.5 2000/09/17 14:44:52 groovyjava Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 2000 Robert Baruch (autophile@dol.net) 5 * (c) 2000 Robert Baruch (autophile@dol.net)
8 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org> 6 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 6610d2dd1e7f..fcbbfdb7b2b0 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -1,7 +1,5 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * 2 *
3 * $Id: transport.c,v 1.47 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * 5 *
@@ -75,14 +73,14 @@
75 * by a separate code path.) 73 * by a separate code path.)
76 * 74 *
77 * The abort function (usb_storage_command_abort() in scsiglue.c) first 75 * The abort function (usb_storage_command_abort() in scsiglue.c) first
78 * sets the machine state and the ABORTING bit in us->flags to prevent 76 * sets the machine state and the ABORTING bit in us->dflags to prevent
79 * new URBs from being submitted. It then calls usb_stor_stop_transport() 77 * new URBs from being submitted. It then calls usb_stor_stop_transport()
80 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->flags 78 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
81 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE 79 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
82 * bit is tested to see if the current_sg scatter-gather request needs to be 80 * bit is tested to see if the current_sg scatter-gather request needs to be
83 * stopped. The timeout callback routine does much the same thing. 81 * stopped. The timeout callback routine does much the same thing.
84 * 82 *
85 * When a disconnect occurs, the DISCONNECTING bit in us->flags is set to 83 * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
86 * prevent new URBs from being submitted, and usb_stor_stop_transport() is 84 * prevent new URBs from being submitted, and usb_stor_stop_transport() is
87 * called to stop any ongoing requests. 85 * called to stop any ongoing requests.
88 * 86 *
@@ -127,8 +125,8 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
127 long timeleft; 125 long timeleft;
128 int status; 126 int status;
129 127
130 /* don't submit URBs during abort/disconnect processing */ 128 /* don't submit URBs during abort processing */
131 if (us->flags & ABORTING_OR_DISCONNECTING) 129 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
132 return -EIO; 130 return -EIO;
133 131
134 /* set up data structures for the wakeup system */ 132 /* set up data structures for the wakeup system */
@@ -159,13 +157,13 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
159 157
160 /* since the URB has been submitted successfully, it's now okay 158 /* since the URB has been submitted successfully, it's now okay
161 * to cancel it */ 159 * to cancel it */
162 set_bit(US_FLIDX_URB_ACTIVE, &us->flags); 160 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
163 161
164 /* did an abort/disconnect occur during the submission? */ 162 /* did an abort occur during the submission? */
165 if (us->flags & ABORTING_OR_DISCONNECTING) { 163 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
166 164
167 /* cancel the URB, if it hasn't been cancelled already */ 165 /* cancel the URB, if it hasn't been cancelled already */
168 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) { 166 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
169 US_DEBUGP("-- cancelling URB\n"); 167 US_DEBUGP("-- cancelling URB\n");
170 usb_unlink_urb(us->current_urb); 168 usb_unlink_urb(us->current_urb);
171 } 169 }
@@ -175,7 +173,7 @@ static int usb_stor_msg_common(struct us_data *us, int timeout)
175 timeleft = wait_for_completion_interruptible_timeout( 173 timeleft = wait_for_completion_interruptible_timeout(
176 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT); 174 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
177 175
178 clear_bit(US_FLIDX_URB_ACTIVE, &us->flags); 176 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
179 177
180 if (timeleft <= 0) { 178 if (timeleft <= 0) {
181 US_DEBUGP("%s -- cancelling URB\n", 179 US_DEBUGP("%s -- cancelling URB\n",
@@ -419,8 +417,8 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
419{ 417{
420 int result; 418 int result;
421 419
422 /* don't submit s-g requests during abort/disconnect processing */ 420 /* don't submit s-g requests during abort processing */
423 if (us->flags & ABORTING_OR_DISCONNECTING) 421 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
424 return USB_STOR_XFER_ERROR; 422 return USB_STOR_XFER_ERROR;
425 423
426 /* initialize the scatter-gather request block */ 424 /* initialize the scatter-gather request block */
@@ -435,13 +433,13 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
435 433
436 /* since the block has been initialized successfully, it's now 434 /* since the block has been initialized successfully, it's now
437 * okay to cancel it */ 435 * okay to cancel it */
438 set_bit(US_FLIDX_SG_ACTIVE, &us->flags); 436 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
439 437
440 /* did an abort/disconnect occur during the submission? */ 438 /* did an abort occur during the submission? */
441 if (us->flags & ABORTING_OR_DISCONNECTING) { 439 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
442 440
443 /* cancel the request, if it hasn't been cancelled already */ 441 /* cancel the request, if it hasn't been cancelled already */
444 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) { 442 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
445 US_DEBUGP("-- cancelling sg request\n"); 443 US_DEBUGP("-- cancelling sg request\n");
446 usb_sg_cancel(&us->current_sg); 444 usb_sg_cancel(&us->current_sg);
447 } 445 }
@@ -449,7 +447,7 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
449 447
450 /* wait for the completion of the transfer */ 448 /* wait for the completion of the transfer */
451 usb_sg_wait(&us->current_sg); 449 usb_sg_wait(&us->current_sg);
452 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags); 450 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
453 451
454 result = us->current_sg.status; 452 result = us->current_sg.status;
455 if (act_len) 453 if (act_len)
@@ -530,7 +528,7 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
530 /* if the command gets aborted by the higher layers, we need to 528 /* if the command gets aborted by the higher layers, we need to
531 * short-circuit all other processing 529 * short-circuit all other processing
532 */ 530 */
533 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 531 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
534 US_DEBUGP("-- command was aborted\n"); 532 US_DEBUGP("-- command was aborted\n");
535 srb->result = DID_ABORT << 16; 533 srb->result = DID_ABORT << 16;
536 goto Handle_Errors; 534 goto Handle_Errors;
@@ -616,7 +614,7 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
616 /* let's clean up right away */ 614 /* let's clean up right away */
617 scsi_eh_restore_cmnd(srb, &ses); 615 scsi_eh_restore_cmnd(srb, &ses);
618 616
619 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 617 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
620 US_DEBUGP("-- auto-sense aborted\n"); 618 US_DEBUGP("-- auto-sense aborted\n");
621 srb->result = DID_ABORT << 16; 619 srb->result = DID_ABORT << 16;
622 goto Handle_Errors; 620 goto Handle_Errors;
@@ -629,7 +627,7 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
629 * auto-sense is perfectly valid 627 * auto-sense is perfectly valid
630 */ 628 */
631 srb->result = DID_ERROR << 16; 629 srb->result = DID_ERROR << 16;
632 if (!(us->flags & US_FL_SCM_MULT_TARG)) 630 if (!(us->fflags & US_FL_SCM_MULT_TARG))
633 goto Handle_Errors; 631 goto Handle_Errors;
634 return; 632 return;
635 } 633 }
@@ -679,8 +677,8 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
679 /* Set the RESETTING bit, and clear the ABORTING bit so that 677 /* Set the RESETTING bit, and clear the ABORTING bit so that
680 * the reset may proceed. */ 678 * the reset may proceed. */
681 scsi_lock(us_to_host(us)); 679 scsi_lock(us_to_host(us));
682 set_bit(US_FLIDX_RESETTING, &us->flags); 680 set_bit(US_FLIDX_RESETTING, &us->dflags);
683 clear_bit(US_FLIDX_ABORTING, &us->flags); 681 clear_bit(US_FLIDX_ABORTING, &us->dflags);
684 scsi_unlock(us_to_host(us)); 682 scsi_unlock(us_to_host(us));
685 683
686 /* We must release the device lock because the pre_reset routine 684 /* We must release the device lock because the pre_reset routine
@@ -695,7 +693,7 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
695 scsi_unlock(us_to_host(us)); 693 scsi_unlock(us_to_host(us));
696 us->transport_reset(us); 694 us->transport_reset(us);
697 } 695 }
698 clear_bit(US_FLIDX_RESETTING, &us->flags); 696 clear_bit(US_FLIDX_RESETTING, &us->dflags);
699} 697}
700 698
701/* Stop the current URB transfer */ 699/* Stop the current URB transfer */
@@ -707,13 +705,13 @@ void usb_stor_stop_transport(struct us_data *us)
707 * let's wake it up. The test_and_clear_bit() call 705 * let's wake it up. The test_and_clear_bit() call
708 * guarantees that if a URB has just been submitted, 706 * guarantees that if a URB has just been submitted,
709 * it won't be cancelled more than once. */ 707 * it won't be cancelled more than once. */
710 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) { 708 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
711 US_DEBUGP("-- cancelling URB\n"); 709 US_DEBUGP("-- cancelling URB\n");
712 usb_unlink_urb(us->current_urb); 710 usb_unlink_urb(us->current_urb);
713 } 711 }
714 712
715 /* If we are waiting for a scatter-gather operation, cancel it. */ 713 /* If we are waiting for a scatter-gather operation, cancel it. */
716 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) { 714 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
717 US_DEBUGP("-- cancelling sg request\n"); 715 US_DEBUGP("-- cancelling sg request\n");
718 usb_sg_cancel(&us->current_sg); 716 usb_sg_cancel(&us->current_sg);
719 } 717 }
@@ -914,7 +912,7 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
914 unsigned int cbwlen = US_BULK_CB_WRAP_LEN; 912 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
915 913
916 /* Take care of BULK32 devices; set extra byte to 0 */ 914 /* Take care of BULK32 devices; set extra byte to 0 */
917 if ( unlikely(us->flags & US_FL_BULK32)) { 915 if (unlikely(us->fflags & US_FL_BULK32)) {
918 cbwlen = 32; 916 cbwlen = 32;
919 us->iobuf[31] = 0; 917 us->iobuf[31] = 0;
920 } 918 }
@@ -925,7 +923,7 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
925 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0; 923 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
926 bcb->Tag = ++us->tag; 924 bcb->Tag = ++us->tag;
927 bcb->Lun = srb->device->lun; 925 bcb->Lun = srb->device->lun;
928 if (us->flags & US_FL_SCM_MULT_TARG) 926 if (us->fflags & US_FL_SCM_MULT_TARG)
929 bcb->Lun |= srb->device->id << 4; 927 bcb->Lun |= srb->device->id << 4;
930 bcb->Length = srb->cmd_len; 928 bcb->Length = srb->cmd_len;
931 929
@@ -951,7 +949,7 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
951 /* Some USB-IDE converter chips need a 100us delay between the 949 /* Some USB-IDE converter chips need a 100us delay between the
952 * command phase and the data phase. Some devices need a little 950 * command phase and the data phase. Some devices need a little
953 * more than that, probably because of clock rate inaccuracies. */ 951 * more than that, probably because of clock rate inaccuracies. */
954 if (unlikely(us->flags & US_FL_GO_SLOW)) 952 if (unlikely(us->fflags & US_FL_GO_SLOW))
955 udelay(125); 953 udelay(125);
956 954
957 if (transfer_length) { 955 if (transfer_length) {
@@ -1010,7 +1008,7 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
1010 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n", 1008 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1011 le32_to_cpu(bcs->Signature), bcs->Tag, 1009 le32_to_cpu(bcs->Signature), bcs->Tag,
1012 residue, bcs->Status); 1010 residue, bcs->Status);
1013 if (!(bcs->Tag == us->tag || (us->flags & US_FL_BULK_IGNORE_TAG)) || 1011 if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
1014 bcs->Status > US_BULK_STAT_PHASE) { 1012 bcs->Status > US_BULK_STAT_PHASE) {
1015 US_DEBUGP("Bulk logical error\n"); 1013 US_DEBUGP("Bulk logical error\n");
1016 return USB_STOR_TRANSPORT_ERROR; 1014 return USB_STOR_TRANSPORT_ERROR;
@@ -1035,7 +1033,7 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
1035 /* try to compute the actual residue, based on how much data 1033 /* try to compute the actual residue, based on how much data
1036 * was really transferred and what the device tells us */ 1034 * was really transferred and what the device tells us */
1037 if (residue) { 1035 if (residue) {
1038 if (!(us->flags & US_FL_IGNORE_RESIDUE)) { 1036 if (!(us->fflags & US_FL_IGNORE_RESIDUE)) {
1039 residue = min(residue, transfer_length); 1037 residue = min(residue, transfer_length);
1040 scsi_set_resid(srb, max(scsi_get_resid(srb), 1038 scsi_set_resid(srb, max(scsi_get_resid(srb),
1041 (int) residue)); 1039 (int) residue));
@@ -1090,7 +1088,7 @@ static int usb_stor_reset_common(struct us_data *us,
1090 int result; 1088 int result;
1091 int result2; 1089 int result2;
1092 1090
1093 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 1091 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1094 US_DEBUGP("No reset during disconnect\n"); 1092 US_DEBUGP("No reset during disconnect\n");
1095 return -EIO; 1093 return -EIO;
1096 } 1094 }
@@ -1103,12 +1101,12 @@ static int usb_stor_reset_common(struct us_data *us,
1103 return result; 1101 return result;
1104 } 1102 }
1105 1103
1106 /* Give the device some time to recover from the reset, 1104 /* Give the device some time to recover from the reset,
1107 * but don't delay disconnect processing. */ 1105 * but don't delay disconnect processing. */
1108 wait_event_interruptible_timeout(us->delay_wait, 1106 wait_event_interruptible_timeout(us->delay_wait,
1109 test_bit(US_FLIDX_DISCONNECTING, &us->flags), 1107 test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
1110 HZ*6); 1108 HZ*6);
1111 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 1109 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1112 US_DEBUGP("Reset interrupted by disconnect\n"); 1110 US_DEBUGP("Reset interrupted by disconnect\n");
1113 return -EIO; 1111 return -EIO;
1114 } 1112 }
@@ -1170,13 +1168,12 @@ int usb_stor_port_reset(struct us_data *us)
1170 US_DEBUGP("unable to lock device for reset: %d\n", result); 1168 US_DEBUGP("unable to lock device for reset: %d\n", result);
1171 else { 1169 else {
1172 /* Were we disconnected while waiting for the lock? */ 1170 /* Were we disconnected while waiting for the lock? */
1173 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 1171 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1174 result = -EIO; 1172 result = -EIO;
1175 US_DEBUGP("No reset during disconnect\n"); 1173 US_DEBUGP("No reset during disconnect\n");
1176 } else { 1174 } else {
1177 result = usb_reset_composite_device( 1175 result = usb_reset_device(us->pusb_dev);
1178 us->pusb_dev, us->pusb_intf); 1176 US_DEBUGP("usb_reset_device returns %d\n",
1179 US_DEBUGP("usb_reset_composite_device returns %d\n",
1180 result); 1177 result);
1181 } 1178 }
1182 if (rc_lock) 1179 if (rc_lock)
diff --git a/drivers/usb/storage/transport.h b/drivers/usb/storage/transport.h
index ada7c2f43f84..e70b88182f0e 100644
--- a/drivers/usb/storage/transport.h
+++ b/drivers/usb/storage/transport.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Transport Functions Header File 2 * Transport Functions Header File
3 * 3 *
4 * $Id: transport.h,v 1.18 2002/04/21 02:57:59 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index 39a7c11795c4..7ae69f55aa96 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Unusual Devices File 2 * Unusual Devices File
3 * 3 *
4 * $Id: unusual_devs.h,v 1.32 2002/02/25 02:41:24 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 2000-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 2000-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
@@ -1234,6 +1232,17 @@ UNUSUAL_DEV( 0x0851, 0x1543, 0x0200, 0x0200,
1234 US_SC_DEVICE, US_PR_DEVICE, NULL, 1232 US_SC_DEVICE, US_PR_DEVICE, NULL,
1235 US_FL_NOT_LOCKABLE), 1233 US_FL_NOT_LOCKABLE),
1236 1234
1235/* Andrew Lunn <andrew@lunn.ch>
1236 * PanDigital Digital Picture Frame. Does not like ALLOW_MEDIUM_REMOVAL
1237 * on LUN 4.
1238 * Note: Vend:Prod clash with "Ltd Maxell WS30 Slim Digital Camera"
1239*/
1240UNUSUAL_DEV( 0x0851, 0x1543, 0x0200, 0x0200,
1241 "PanDigital",
1242 "Photo Frame",
1243 US_SC_DEVICE, US_PR_DEVICE, NULL,
1244 US_FL_NOT_LOCKABLE),
1245
1237/* Submitted by Jan De Luyck <lkml@kcore.org> */ 1246/* Submitted by Jan De Luyck <lkml@kcore.org> */
1238UNUSUAL_DEV( 0x08bd, 0x1100, 0x0000, 0x0000, 1247UNUSUAL_DEV( 0x08bd, 0x1100, 0x0000, 0x0000,
1239 "CITIZEN", 1248 "CITIZEN",
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index e268aacb773a..bfea851be985 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -1,7 +1,5 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * 2 *
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by: 3 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 4 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * 5 *
@@ -312,26 +310,27 @@ static int usb_stor_control_thread(void * __us)
312 310
313 for(;;) { 311 for(;;) {
314 US_DEBUGP("*** thread sleeping.\n"); 312 US_DEBUGP("*** thread sleeping.\n");
315 if(down_interruptible(&us->sema)) 313 if (wait_for_completion_interruptible(&us->cmnd_ready))
316 break; 314 break;
317 315
318 US_DEBUGP("*** thread awakened.\n"); 316 US_DEBUGP("*** thread awakened.\n");
319 317
320 /* lock the device pointers */ 318 /* lock the device pointers */
321 mutex_lock(&(us->dev_mutex)); 319 mutex_lock(&(us->dev_mutex));
322 320
323 /* if the device has disconnected, we are free to exit */ 321 /* lock access to the state */
324 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 322 scsi_lock(host);
325 US_DEBUGP("-- exiting\n"); 323
324 /* When we are called with no command pending, we're done */
325 if (us->srb == NULL) {
326 scsi_unlock(host);
326 mutex_unlock(&us->dev_mutex); 327 mutex_unlock(&us->dev_mutex);
328 US_DEBUGP("-- exiting\n");
327 break; 329 break;
328 } 330 }
329 331
330 /* lock access to the state */
331 scsi_lock(host);
332
333 /* has the command timed out *already* ? */ 332 /* has the command timed out *already* ? */
334 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 333 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
335 us->srb->result = DID_ABORT << 16; 334 us->srb->result = DID_ABORT << 16;
336 goto SkipForAbort; 335 goto SkipForAbort;
337 } 336 }
@@ -350,7 +349,7 @@ static int usb_stor_control_thread(void * __us)
350 * the maximum known LUN 349 * the maximum known LUN
351 */ 350 */
352 else if (us->srb->device->id && 351 else if (us->srb->device->id &&
353 !(us->flags & US_FL_SCM_MULT_TARG)) { 352 !(us->fflags & US_FL_SCM_MULT_TARG)) {
354 US_DEBUGP("Bad target number (%d:%d)\n", 353 US_DEBUGP("Bad target number (%d:%d)\n",
355 us->srb->device->id, us->srb->device->lun); 354 us->srb->device->id, us->srb->device->lun);
356 us->srb->result = DID_BAD_TARGET << 16; 355 us->srb->result = DID_BAD_TARGET << 16;
@@ -365,7 +364,7 @@ static int usb_stor_control_thread(void * __us)
365 /* Handle those devices which need us to fake 364 /* Handle those devices which need us to fake
366 * their inquiry data */ 365 * their inquiry data */
367 else if ((us->srb->cmnd[0] == INQUIRY) && 366 else if ((us->srb->cmnd[0] == INQUIRY) &&
368 (us->flags & US_FL_FIX_INQUIRY)) { 367 (us->fflags & US_FL_FIX_INQUIRY)) {
369 unsigned char data_ptr[36] = { 368 unsigned char data_ptr[36] = {
370 0x00, 0x80, 0x02, 0x02, 369 0x00, 0x80, 0x02, 0x02,
371 0x1F, 0x00, 0x00, 0x00}; 370 0x1F, 0x00, 0x00, 0x00};
@@ -384,12 +383,8 @@ static int usb_stor_control_thread(void * __us)
384 /* lock access to the state */ 383 /* lock access to the state */
385 scsi_lock(host); 384 scsi_lock(host);
386 385
387 /* did the command already complete because of a disconnect? */
388 if (!us->srb)
389 ; /* nothing to do */
390
391 /* indicate that the command is done */ 386 /* indicate that the command is done */
392 else if (us->srb->result != DID_ABORT << 16) { 387 if (us->srb->result != DID_ABORT << 16) {
393 US_DEBUGP("scsi cmd done, result=0x%x\n", 388 US_DEBUGP("scsi cmd done, result=0x%x\n",
394 us->srb->result); 389 us->srb->result);
395 us->srb->scsi_done(us->srb); 390 us->srb->scsi_done(us->srb);
@@ -403,12 +398,12 @@ SkipForAbort:
403 * the TIMED_OUT flag, not srb->result == DID_ABORT, because 398 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
404 * the timeout might have occurred after the command had 399 * the timeout might have occurred after the command had
405 * already completed with a different result code. */ 400 * already completed with a different result code. */
406 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) { 401 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
407 complete(&(us->notify)); 402 complete(&(us->notify));
408 403
409 /* Allow USB transfers to resume */ 404 /* Allow USB transfers to resume */
410 clear_bit(US_FLIDX_ABORTING, &us->flags); 405 clear_bit(US_FLIDX_ABORTING, &us->dflags);
411 clear_bit(US_FLIDX_TIMED_OUT, &us->flags); 406 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
412 } 407 }
413 408
414 /* finished working on this command */ 409 /* finished working on this command */
@@ -500,9 +495,9 @@ static int get_device_info(struct us_data *us, const struct usb_device_id *id)
500 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ? 495 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
501 idesc->bInterfaceProtocol : 496 idesc->bInterfaceProtocol :
502 unusual_dev->useTransport; 497 unusual_dev->useTransport;
503 us->flags = USB_US_ORIG_FLAGS(id->driver_info); 498 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
504 499
505 if (us->flags & US_FL_IGNORE_DEVICE) { 500 if (us->fflags & US_FL_IGNORE_DEVICE) {
506 printk(KERN_INFO USB_STORAGE "device ignored\n"); 501 printk(KERN_INFO USB_STORAGE "device ignored\n");
507 return -ENODEV; 502 return -ENODEV;
508 } 503 }
@@ -512,7 +507,7 @@ static int get_device_info(struct us_data *us, const struct usb_device_id *id)
512 * disable it if we're in full-speed 507 * disable it if we're in full-speed
513 */ 508 */
514 if (dev->speed != USB_SPEED_HIGH) 509 if (dev->speed != USB_SPEED_HIGH)
515 us->flags &= ~US_FL_GO_SLOW; 510 us->fflags &= ~US_FL_GO_SLOW;
516 511
517 /* Log a message if a non-generic unusual_dev entry contains an 512 /* Log a message if a non-generic unusual_dev entry contains an
518 * unnecessary subclass or protocol override. This may stimulate 513 * unnecessary subclass or protocol override. This may stimulate
@@ -533,7 +528,7 @@ static int get_device_info(struct us_data *us, const struct usb_device_id *id)
533 if (unusual_dev->useTransport != US_PR_DEVICE && 528 if (unusual_dev->useTransport != US_PR_DEVICE &&
534 us->protocol == idesc->bInterfaceProtocol) 529 us->protocol == idesc->bInterfaceProtocol)
535 msg += 2; 530 msg += 2;
536 if (msg >= 0 && !(us->flags & US_FL_NEED_OVERRIDE)) 531 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
537 printk(KERN_NOTICE USB_STORAGE "This device " 532 printk(KERN_NOTICE USB_STORAGE "This device "
538 "(%04x,%04x,%04x S %02x P %02x)" 533 "(%04x,%04x,%04x S %02x P %02x)"
539 " has %s in unusual_devs.h (kernel" 534 " has %s in unusual_devs.h (kernel"
@@ -663,7 +658,7 @@ static int get_transport(struct us_data *us)
663 US_DEBUGP("Transport: %s\n", us->transport_name); 658 US_DEBUGP("Transport: %s\n", us->transport_name);
664 659
665 /* fix for single-lun devices */ 660 /* fix for single-lun devices */
666 if (us->flags & US_FL_SINGLE_LUN) 661 if (us->fflags & US_FL_SINGLE_LUN)
667 us->max_lun = 0; 662 us->max_lun = 0;
668 return 0; 663 return 0;
669} 664}
@@ -820,12 +815,11 @@ static void usb_stor_release_resources(struct us_data *us)
820 US_DEBUGP("-- %s\n", __func__); 815 US_DEBUGP("-- %s\n", __func__);
821 816
822 /* Tell the control thread to exit. The SCSI host must 817 /* Tell the control thread to exit. The SCSI host must
823 * already have been removed so it won't try to queue 818 * already have been removed and the DISCONNECTING flag set
824 * any more commands. 819 * so that we won't accept any more commands.
825 */ 820 */
826 US_DEBUGP("-- sending exit command to thread\n"); 821 US_DEBUGP("-- sending exit command to thread\n");
827 set_bit(US_FLIDX_DISCONNECTING, &us->flags); 822 complete(&us->cmnd_ready);
828 up(&us->sema);
829 if (us->ctl_thread) 823 if (us->ctl_thread)
830 kthread_stop(us->ctl_thread); 824 kthread_stop(us->ctl_thread);
831 825
@@ -859,39 +853,36 @@ static void dissociate_dev(struct us_data *us)
859 usb_set_intfdata(us->pusb_intf, NULL); 853 usb_set_intfdata(us->pusb_intf, NULL);
860} 854}
861 855
862/* First stage of disconnect processing: stop all commands and remove 856/* First stage of disconnect processing: stop SCSI scanning,
863 * the host */ 857 * remove the host, and stop accepting new commands
858 */
864static void quiesce_and_remove_host(struct us_data *us) 859static void quiesce_and_remove_host(struct us_data *us)
865{ 860{
866 struct Scsi_Host *host = us_to_host(us); 861 struct Scsi_Host *host = us_to_host(us);
867 862
868 /* Prevent new USB transfers, stop the current command, and 863 /* If the device is really gone, cut short reset delays */
869 * interrupt a SCSI-scan or device-reset delay */ 864 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
870 scsi_lock(host); 865 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
871 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
872 scsi_unlock(host);
873 usb_stor_stop_transport(us);
874 wake_up(&us->delay_wait);
875 866
876 /* queuecommand won't accept any new commands and the control 867 /* Prevent SCSI-scanning (if it hasn't started yet)
877 * thread won't execute a previously-queued command. If there 868 * and wait for the SCSI-scanning thread to stop.
878 * is such a command pending, complete it with an error. */ 869 */
879 mutex_lock(&us->dev_mutex); 870 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
880 if (us->srb) { 871 wake_up(&us->delay_wait);
881 us->srb->result = DID_NO_CONNECT << 16; 872 wait_for_completion(&us->scanning_done);
882 scsi_lock(host);
883 us->srb->scsi_done(us->srb);
884 us->srb = NULL;
885 complete(&us->notify); /* in case of an abort */
886 scsi_unlock(host);
887 }
888 mutex_unlock(&us->dev_mutex);
889 873
890 /* Now we own no commands so it's safe to remove the SCSI host */ 874 /* Removing the host will perform an orderly shutdown: caches
875 * synchronized, disks spun down, etc.
876 */
891 scsi_remove_host(host); 877 scsi_remove_host(host);
892 878
893 /* Wait for the SCSI-scanning thread to stop */ 879 /* Prevent any new commands from being accepted and cut short
894 wait_for_completion(&us->scanning_done); 880 * reset delays.
881 */
882 scsi_lock(host);
883 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
884 scsi_unlock(host);
885 wake_up(&us->delay_wait);
895} 886}
896 887
897/* Second stage of disconnect processing: deallocate all resources */ 888/* Second stage of disconnect processing: deallocate all resources */
@@ -919,16 +910,16 @@ static int usb_stor_scan_thread(void * __us)
919 printk(KERN_DEBUG "usb-storage: waiting for device " 910 printk(KERN_DEBUG "usb-storage: waiting for device "
920 "to settle before scanning\n"); 911 "to settle before scanning\n");
921 wait_event_freezable_timeout(us->delay_wait, 912 wait_event_freezable_timeout(us->delay_wait,
922 test_bit(US_FLIDX_DISCONNECTING, &us->flags), 913 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
923 delay_use * HZ); 914 delay_use * HZ);
924 } 915 }
925 916
926 /* If the device is still connected, perform the scanning */ 917 /* If the device is still connected, perform the scanning */
927 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 918 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
928 919
929 /* For bulk-only devices, determine the max LUN value */ 920 /* For bulk-only devices, determine the max LUN value */
930 if (us->protocol == US_PR_BULK && 921 if (us->protocol == US_PR_BULK &&
931 !(us->flags & US_FL_SINGLE_LUN)) { 922 !(us->fflags & US_FL_SINGLE_LUN)) {
932 mutex_lock(&us->dev_mutex); 923 mutex_lock(&us->dev_mutex);
933 us->max_lun = usb_stor_Bulk_max_lun(us); 924 us->max_lun = usb_stor_Bulk_max_lun(us);
934 mutex_unlock(&us->dev_mutex); 925 mutex_unlock(&us->dev_mutex);
@@ -975,7 +966,7 @@ static int storage_probe(struct usb_interface *intf,
975 us = host_to_us(host); 966 us = host_to_us(host);
976 memset(us, 0, sizeof(struct us_data)); 967 memset(us, 0, sizeof(struct us_data));
977 mutex_init(&(us->dev_mutex)); 968 mutex_init(&(us->dev_mutex));
978 init_MUTEX_LOCKED(&(us->sema)); 969 init_completion(&us->cmnd_ready);
979 init_completion(&(us->notify)); 970 init_completion(&(us->notify));
980 init_waitqueue_head(&us->delay_wait); 971 init_waitqueue_head(&us->delay_wait);
981 init_completion(&us->scanning_done); 972 init_completion(&us->scanning_done);
@@ -1023,6 +1014,7 @@ static int storage_probe(struct usb_interface *intf,
1023 if (IS_ERR(th)) { 1014 if (IS_ERR(th)) {
1024 printk(KERN_WARNING USB_STORAGE 1015 printk(KERN_WARNING USB_STORAGE
1025 "Unable to start the device-scanning thread\n"); 1016 "Unable to start the device-scanning thread\n");
1017 complete(&us->scanning_done);
1026 quiesce_and_remove_host(us); 1018 quiesce_and_remove_host(us);
1027 result = PTR_ERR(th); 1019 result = PTR_ERR(th);
1028 goto BadDevice; 1020 goto BadDevice;
@@ -1065,6 +1057,7 @@ static struct usb_driver usb_storage_driver = {
1065 .pre_reset = storage_pre_reset, 1057 .pre_reset = storage_pre_reset,
1066 .post_reset = storage_post_reset, 1058 .post_reset = storage_post_reset,
1067 .id_table = storage_usb_ids, 1059 .id_table = storage_usb_ids,
1060 .soft_unbind = 1,
1068}; 1061};
1069 1062
1070static int __init usb_stor_init(void) 1063static int __init usb_stor_init(void)
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 8d87503e2560..a4ad73bd832d 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -1,8 +1,6 @@
1/* Driver for USB Mass Storage compliant devices 1/* Driver for USB Mass Storage compliant devices
2 * Main Header File 2 * Main Header File
3 * 3 *
4 * $Id: usb.h,v 1.21 2002/04/21 02:57:59 mdharm Exp $
5 *
6 * Current development and maintenance by: 4 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * 6 *
@@ -67,16 +65,14 @@ struct us_unusual_dev {
67}; 65};
68 66
69 67
70/* Dynamic flag definitions: used in set_bit() etc. */ 68/* Dynamic bitflag definitions (us->dflags): used in set_bit() etc. */
71#define US_FLIDX_URB_ACTIVE 18 /* 0x00040000 current_urb is in use */ 69#define US_FLIDX_URB_ACTIVE 0 /* current_urb is in use */
72#define US_FLIDX_SG_ACTIVE 19 /* 0x00080000 current_sg is in use */ 70#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
73#define US_FLIDX_ABORTING 20 /* 0x00100000 abort is in progress */ 71#define US_FLIDX_ABORTING 2 /* abort is in progress */
74#define US_FLIDX_DISCONNECTING 21 /* 0x00200000 disconnect in progress */ 72#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
75#define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \ 73#define US_FLIDX_RESETTING 4 /* device reset in progress */
76 (1UL << US_FLIDX_DISCONNECTING)) 74#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
77#define US_FLIDX_RESETTING 22 /* 0x00400000 device reset in progress */ 75#define US_FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */
78#define US_FLIDX_TIMED_OUT 23 /* 0x00800000 SCSI midlayer timed out */
79
80 76
81#define USB_STOR_STRING_LEN 32 77#define USB_STOR_STRING_LEN 32
82 78
@@ -109,7 +105,8 @@ struct us_data {
109 struct usb_device *pusb_dev; /* this usb_device */ 105 struct usb_device *pusb_dev; /* this usb_device */
110 struct usb_interface *pusb_intf; /* this interface */ 106 struct usb_interface *pusb_intf; /* this interface */
111 struct us_unusual_dev *unusual_dev; /* device-filter entry */ 107 struct us_unusual_dev *unusual_dev; /* device-filter entry */
112 unsigned long flags; /* from filter initially */ 108 unsigned long fflags; /* fixed flags from filter */
109 unsigned long dflags; /* dynamic atomic bitflags */
113 unsigned int send_bulk_pipe; /* cached pipe values */ 110 unsigned int send_bulk_pipe; /* cached pipe values */
114 unsigned int recv_bulk_pipe; 111 unsigned int recv_bulk_pipe;
115 unsigned int send_ctrl_pipe; 112 unsigned int send_ctrl_pipe;
@@ -147,7 +144,7 @@ struct us_data {
147 struct task_struct *ctl_thread; /* the control thread */ 144 struct task_struct *ctl_thread; /* the control thread */
148 145
149 /* mutual exclusion and synchronization structures */ 146 /* mutual exclusion and synchronization structures */
150 struct semaphore sema; /* to sleep thread on */ 147 struct completion cmnd_ready; /* to sleep thread on */
151 struct completion notify; /* thread begin/end */ 148 struct completion notify; /* thread begin/end */
152 wait_queue_head_t delay_wait; /* wait during scan, reset */ 149 wait_queue_head_t delay_wait; /* wait during scan, reset */
153 struct completion scanning_done; /* wait for scan thread */ 150 struct completion scanning_done; /* wait for scan thread */
diff --git a/include/linux/usb.h b/include/linux/usb.h
index c08689ea9b4b..5811c5da69f9 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -160,6 +160,7 @@ struct usb_interface {
160 unsigned is_active:1; /* the interface is not suspended */ 160 unsigned is_active:1; /* the interface is not suspended */
161 unsigned sysfs_files_created:1; /* the sysfs attributes exist */ 161 unsigned sysfs_files_created:1; /* the sysfs attributes exist */
162 unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */ 162 unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */
163 unsigned needs_binding:1; /* needs delayed unbind/rebind */
163 164
164 struct device dev; /* interface specific device info */ 165 struct device dev; /* interface specific device info */
165 struct device *usb_dev; 166 struct device *usb_dev;
@@ -293,7 +294,7 @@ struct usb_devmap {
293struct usb_bus { 294struct usb_bus {
294 struct device *controller; /* host/master side hardware */ 295 struct device *controller; /* host/master side hardware */
295 int busnum; /* Bus number (in order of reg) */ 296 int busnum; /* Bus number (in order of reg) */
296 char *bus_name; /* stable id (PCI slot_name etc) */ 297 const char *bus_name; /* stable id (PCI slot_name etc) */
297 u8 uses_dma; /* Does the host controller use DMA? */ 298 u8 uses_dma; /* Does the host controller use DMA? */
298 u8 otg_port; /* 0, or number of OTG/HNP port */ 299 u8 otg_port; /* 0, or number of OTG/HNP port */
299 unsigned is_b_host:1; /* true during some HNP roleswitches */ 300 unsigned is_b_host:1; /* true during some HNP roleswitches */
@@ -497,8 +498,6 @@ extern int usb_lock_device_for_reset(struct usb_device *udev,
497 498
498/* USB port reset for device reinitialization */ 499/* USB port reset for device reinitialization */
499extern int usb_reset_device(struct usb_device *dev); 500extern int usb_reset_device(struct usb_device *dev);
500extern int usb_reset_composite_device(struct usb_device *dev,
501 struct usb_interface *iface);
502 501
503extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id); 502extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id);
504 503
@@ -958,9 +957,9 @@ struct usbdrv_wrap {
958 * @resume: Called when the device is being resumed by the system. 957 * @resume: Called when the device is being resumed by the system.
959 * @reset_resume: Called when the suspended device has been reset instead 958 * @reset_resume: Called when the suspended device has been reset instead
960 * of being resumed. 959 * of being resumed.
961 * @pre_reset: Called by usb_reset_composite_device() when the device 960 * @pre_reset: Called by usb_reset_device() when the device
962 * is about to be reset. 961 * is about to be reset.
963 * @post_reset: Called by usb_reset_composite_device() after the device 962 * @post_reset: Called by usb_reset_device() after the device
964 * has been reset 963 * has been reset
965 * @id_table: USB drivers use ID table to support hotplugging. 964 * @id_table: USB drivers use ID table to support hotplugging.
966 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set 965 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
@@ -972,6 +971,8 @@ struct usbdrv_wrap {
972 * added to this driver by preventing the sysfs file from being created. 971 * added to this driver by preventing the sysfs file from being created.
973 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend 972 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend
974 * for interfaces bound to this driver. 973 * for interfaces bound to this driver.
974 * @soft_unbind: if set to 1, the USB core will not kill URBs and disable
975 * endpoints before calling the driver's disconnect method.
975 * 976 *
976 * USB interface drivers must provide a name, probe() and disconnect() 977 * USB interface drivers must provide a name, probe() and disconnect()
977 * methods, and an id_table. Other driver fields are optional. 978 * methods, and an id_table. Other driver fields are optional.
@@ -1012,6 +1013,7 @@ struct usb_driver {
1012 struct usbdrv_wrap drvwrap; 1013 struct usbdrv_wrap drvwrap;
1013 unsigned int no_dynamic_id:1; 1014 unsigned int no_dynamic_id:1;
1014 unsigned int supports_autosuspend:1; 1015 unsigned int supports_autosuspend:1;
1016 unsigned int soft_unbind:1;
1015}; 1017};
1016#define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) 1018#define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver)
1017 1019
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
new file mode 100644
index 000000000000..747c3a49cdc9
--- /dev/null
+++ b/include/linux/usb/composite.h
@@ -0,0 +1,338 @@
1/*
2 * composite.h -- framework for usb gadgets which are composite devices
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef __LINUX_USB_COMPOSITE_H
22#define __LINUX_USB_COMPOSITE_H
23
24/*
25 * This framework is an optional layer on top of the USB Gadget interface,
26 * making it easier to build (a) Composite devices, supporting multiple
27 * functions within any single configuration, and (b) Multi-configuration
28 * devices, also supporting multiple functions but without necessarily
29 * having more than one function per configuration.
30 *
31 * Example: a device with a single configuration supporting both network
32 * link and mass storage functions is a composite device. Those functions
33 * might alternatively be packaged in individual configurations, but in
34 * the composite model the host can use both functions at the same time.
35 */
36
37#include <linux/usb/ch9.h>
38#include <linux/usb/gadget.h>
39
40
41struct usb_configuration;
42
43/**
44 * struct usb_function - describes one function of a configuration
45 * @name: For diagnostics, identifies the function.
46 * @strings: tables of strings, keyed by identifiers assigned during bind()
47 * and by language IDs provided in control requests
48 * @descriptors: Table of full (or low) speed descriptors, using interface and
49 * string identifiers assigned during @bind(). If this pointer is null,
50 * the function will not be available at full speed (or at low speed).
51 * @hs_descriptors: Table of high speed descriptors, using interface and
52 * string identifiers assigned during @bind(). If this pointer is null,
53 * the function will not be available at high speed.
54 * @config: assigned when @usb_add_function() is called; this is the
55 * configuration with which this function is associated.
56 * @bind: Before the gadget can register, all of its functions bind() to the
57 * available resources including string and interface identifiers used
58 * in interface or class descriptors; endpoints; I/O buffers; and so on.
59 * @unbind: Reverses @bind; called as a side effect of unregistering the
60 * driver which added this function.
61 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
62 * initialize usb_ep.driver data at this time (when it is used).
63 * Note that setting an interface to its current altsetting resets
64 * interface state, and that all interfaces have a disabled state.
65 * @get_alt: Returns the active altsetting. If this is not provided,
66 * then only altsetting zero is supported.
67 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
68 * include host resetting or reconfiguring the gadget, and disconnection.
69 * @setup: Used for interface-specific control requests.
70 * @suspend: Notifies functions when the host stops sending USB traffic.
71 * @resume: Notifies functions when the host restarts USB traffic.
72 *
73 * A single USB function uses one or more interfaces, and should in most
74 * cases support operation at both full and high speeds. Each function is
75 * associated by @usb_add_function() with a one configuration; that function
76 * causes @bind() to be called so resources can be allocated as part of
77 * setting up a gadget driver. Those resources include endpoints, which
78 * should be allocated using @usb_ep_autoconfig().
79 *
80 * To support dual speed operation, a function driver provides descriptors
81 * for both high and full speed operation. Except in rare cases that don't
82 * involve bulk endpoints, each speed needs different endpoint descriptors.
83 *
84 * Function drivers choose their own strategies for managing instance data.
85 * The simplest strategy just declares it "static', which means the function
86 * can only be activated once. If the function needs to be exposed in more
87 * than one configuration at a given speed, it needs to support multiple
88 * usb_function structures (one for each configuration).
89 *
90 * A more complex strategy might encapsulate a @usb_function structure inside
91 * a driver-specific instance structure to allows multiple activations. An
92 * example of multiple activations might be a CDC ACM function that supports
93 * two or more distinct instances within the same configuration, providing
94 * several independent logical data links to a USB host.
95 */
96struct usb_function {
97 const char *name;
98 struct usb_gadget_strings **strings;
99 struct usb_descriptor_header **descriptors;
100 struct usb_descriptor_header **hs_descriptors;
101
102 struct usb_configuration *config;
103
104 /* REVISIT: bind() functions can be marked __init, which
105 * makes trouble for section mismatch analysis. See if
106 * we can't restructure things to avoid mismatching.
107 * Related: unbind() may kfree() but bind() won't...
108 */
109
110 /* configuration management: bind/unbind */
111 int (*bind)(struct usb_configuration *,
112 struct usb_function *);
113 void (*unbind)(struct usb_configuration *,
114 struct usb_function *);
115
116 /* runtime state management */
117 int (*set_alt)(struct usb_function *,
118 unsigned interface, unsigned alt);
119 int (*get_alt)(struct usb_function *,
120 unsigned interface);
121 void (*disable)(struct usb_function *);
122 int (*setup)(struct usb_function *,
123 const struct usb_ctrlrequest *);
124 void (*suspend)(struct usb_function *);
125 void (*resume)(struct usb_function *);
126
127 /* internals */
128 struct list_head list;
129};
130
131int usb_add_function(struct usb_configuration *, struct usb_function *);
132
133int usb_interface_id(struct usb_configuration *, struct usb_function *);
134
135/**
136 * ep_choose - select descriptor endpoint at current device speed
137 * @g: gadget, connected and running at some speed
138 * @hs: descriptor to use for high speed operation
139 * @fs: descriptor to use for full or low speed operation
140 */
141static inline struct usb_endpoint_descriptor *
142ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
143 struct usb_endpoint_descriptor *fs)
144{
145 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
146 return hs;
147 return fs;
148}
149
150#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
151
152/**
153 * struct usb_configuration - represents one gadget configuration
154 * @label: For diagnostics, describes the configuration.
155 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
156 * and by language IDs provided in control requests.
157 * @descriptors: Table of descriptors preceding all function descriptors.
158 * Examples include OTG and vendor-specific descriptors.
159 * @bind: Called from @usb_add_config() to allocate resources unique to this
160 * configuration and to call @usb_add_function() for each function used.
161 * @unbind: Reverses @bind; called as a side effect of unregistering the
162 * driver which added this configuration.
163 * @setup: Used to delegate control requests that aren't handled by standard
164 * device infrastructure or directed at a specific interface.
165 * @bConfigurationValue: Copied into configuration descriptor.
166 * @iConfiguration: Copied into configuration descriptor.
167 * @bmAttributes: Copied into configuration descriptor.
168 * @bMaxPower: Copied into configuration descriptor.
169 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
170 * the device associated with this configuration.
171 *
172 * Configurations are building blocks for gadget drivers structured around
173 * function drivers. Simple USB gadgets require only one function and one
174 * configuration, and handle dual-speed hardware by always providing the same
175 * functionality. Slightly more complex gadgets may have more than one
176 * single-function configuration at a given speed; or have configurations
177 * that only work at one speed.
178 *
179 * Composite devices are, by definition, ones with configurations which
180 * include more than one function.
181 *
182 * The lifecycle of a usb_configuration includes allocation, initialization
183 * of the fields described above, and calling @usb_add_config() to set up
184 * internal data and bind it to a specific device. The configuration's
185 * @bind() method is then used to initialize all the functions and then
186 * call @usb_add_function() for them.
187 *
188 * Those functions would normally be independant of each other, but that's
189 * not mandatory. CDC WMC devices are an example where functions often
190 * depend on other functions, with some functions subsidiary to others.
191 * Such interdependency may be managed in any way, so long as all of the
192 * descriptors complete by the time the composite driver returns from
193 * its bind() routine.
194 */
195struct usb_configuration {
196 const char *label;
197 struct usb_gadget_strings **strings;
198 const struct usb_descriptor_header **descriptors;
199
200 /* REVISIT: bind() functions can be marked __init, which
201 * makes trouble for section mismatch analysis. See if
202 * we can't restructure things to avoid mismatching...
203 */
204
205 /* configuration management: bind/unbind */
206 int (*bind)(struct usb_configuration *);
207 void (*unbind)(struct usb_configuration *);
208 int (*setup)(struct usb_configuration *,
209 const struct usb_ctrlrequest *);
210
211 /* fields in the config descriptor */
212 u8 bConfigurationValue;
213 u8 iConfiguration;
214 u8 bmAttributes;
215 u8 bMaxPower;
216
217 struct usb_composite_dev *cdev;
218
219 /* internals */
220 struct list_head list;
221 struct list_head functions;
222 u8 next_interface_id;
223 unsigned highspeed:1;
224 unsigned fullspeed:1;
225 struct usb_function *interface[MAX_CONFIG_INTERFACES];
226};
227
228int usb_add_config(struct usb_composite_dev *,
229 struct usb_configuration *);
230
231/**
232 * struct usb_composite_driver - groups configurations into a gadget
233 * @name: For diagnostics, identifies the driver.
234 * @dev: Template descriptor for the device, including default device
235 * identifiers.
236 * @strings: tables of strings, keyed by identifiers assigned during bind()
237 * and language IDs provided in control requests
238 * @bind: (REQUIRED) Used to allocate resources that are shared across the
239 * whole device, such as string IDs, and add its configurations using
240 * @usb_add_config(). This may fail by returning a negative errno
241 * value; it should return zero on successful initialization.
242 * @unbind: Reverses @bind(); called as a side effect of unregistering
243 * this driver.
244 *
245 * Devices default to reporting self powered operation. Devices which rely
246 * on bus powered operation should report this in their @bind() method.
247 *
248 * Before returning from @bind, various fields in the template descriptor
249 * may be overridden. These include the idVendor/idProduct/bcdDevice values
250 * normally to bind the appropriate host side driver, and the three strings
251 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
252 * meaningful device identifiers. (The strings will not be defined unless
253 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
254 * is also reported, as defined by the underlying controller driver.
255 */
256struct usb_composite_driver {
257 const char *name;
258 const struct usb_device_descriptor *dev;
259 struct usb_gadget_strings **strings;
260
261 /* REVISIT: bind() functions can be marked __init, which
262 * makes trouble for section mismatch analysis. See if
263 * we can't restructure things to avoid mismatching...
264 */
265
266 int (*bind)(struct usb_composite_dev *);
267 int (*unbind)(struct usb_composite_dev *);
268};
269
270extern int usb_composite_register(struct usb_composite_driver *);
271extern void usb_composite_unregister(struct usb_composite_driver *);
272
273
274/**
275 * struct usb_composite_device - represents one composite usb gadget
276 * @gadget: read-only, abstracts the gadget's usb peripheral controller
277 * @req: used for control responses; buffer is pre-allocated
278 * @bufsiz: size of buffer pre-allocated in @req
279 * @config: the currently active configuration
280 *
281 * One of these devices is allocated and initialized before the
282 * associated device driver's bind() is called.
283 *
284 * OPEN ISSUE: it appears that some WUSB devices will need to be
285 * built by combining a normal (wired) gadget with a wireless one.
286 * This revision of the gadget framework should probably try to make
287 * sure doing that won't hurt too much.
288 *
289 * One notion for how to handle Wireless USB devices involves:
290 * (a) a second gadget here, discovery mechanism TBD, but likely
291 * needing separate "register/unregister WUSB gadget" calls;
292 * (b) updates to usb_gadget to include flags "is it wireless",
293 * "is it wired", plus (presumably in a wrapper structure)
294 * bandgroup and PHY info;
295 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
296 * wireless-specific parameters like maxburst and maxsequence;
297 * (d) configurations that are specific to wireless links;
298 * (e) function drivers that understand wireless configs and will
299 * support wireless for (additional) function instances;
300 * (f) a function to support association setup (like CBAF), not
301 * necessarily requiring a wireless adapter;
302 * (g) composite device setup that can create one or more wireless
303 * configs, including appropriate association setup support;
304 * (h) more, TBD.
305 */
306struct usb_composite_dev {
307 struct usb_gadget *gadget;
308 struct usb_request *req;
309 unsigned bufsiz;
310
311 struct usb_configuration *config;
312
313 /* internals */
314 struct usb_device_descriptor desc;
315 struct list_head configs;
316 struct usb_composite_driver *driver;
317 u8 next_string_id;
318
319 spinlock_t lock;
320
321 /* REVISIT use and existence of lock ... */
322};
323
324extern int usb_string_id(struct usb_composite_dev *c);
325
326/* messaging utils */
327#define DBG(d, fmt, args...) \
328 dev_dbg(&(d)->gadget->dev , fmt , ## args)
329#define VDBG(d, fmt, args...) \
330 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
331#define ERROR(d, fmt, args...) \
332 dev_err(&(d)->gadget->dev , fmt , ## args)
333#define WARN(d, fmt, args...) \
334 dev_warn(&(d)->gadget->dev , fmt , ## args)
335#define INFO(d, fmt, args...) \
336 dev_info(&(d)->gadget->dev , fmt , ## args)
337
338#endif /* __LINUX_USB_COMPOSITE_H */
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index cf468fbdbf8e..0460a746480c 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -33,7 +33,8 @@ struct usb_ep;
33 * @short_not_ok: When reading data, makes short packets be 33 * @short_not_ok: When reading data, makes short packets be
34 * treated as errors (queue stops advancing till cleanup). 34 * treated as errors (queue stops advancing till cleanup).
35 * @complete: Function called when request completes, so this request and 35 * @complete: Function called when request completes, so this request and
36 * its buffer may be re-used. 36 * its buffer may be re-used. The function will always be called with
37 * interrupts disabled, and it must not sleep.
37 * Reads terminate with a short packet, or when the buffer fills, 38 * Reads terminate with a short packet, or when the buffer fills,
38 * whichever comes first. When writes terminate, some data bytes 39 * whichever comes first. When writes terminate, some data bytes
39 * will usually still be in flight (often in a hardware fifo). 40 * will usually still be in flight (often in a hardware fifo).
@@ -271,7 +272,10 @@ static inline void usb_ep_free_request(struct usb_ep *ep,
271 * (Note that some USB device controllers disallow protocol stall responses 272 * (Note that some USB device controllers disallow protocol stall responses
272 * in some cases.) When control responses are deferred (the response is 273 * in some cases.) When control responses are deferred (the response is
273 * written after the setup callback returns), then usb_ep_set_halt() may be 274 * written after the setup callback returns), then usb_ep_set_halt() may be
274 * used on ep0 to trigger protocol stalls. 275 * used on ep0 to trigger protocol stalls. Depending on the controller,
276 * it may not be possible to trigger a status-stage protocol stall when the
277 * data stage is over, that is, from within the response's completion
278 * routine.
275 * 279 *
276 * For periodic endpoints, like interrupt or isochronous ones, the usb host 280 * For periodic endpoints, like interrupt or isochronous ones, the usb host
277 * arranges to poll once per interval, and the gadget driver usually will 281 * arranges to poll once per interval, and the gadget driver usually will
@@ -858,6 +862,25 @@ int usb_descriptor_fillbuf(void *, unsigned,
858int usb_gadget_config_buf(const struct usb_config_descriptor *config, 862int usb_gadget_config_buf(const struct usb_config_descriptor *config,
859 void *buf, unsigned buflen, const struct usb_descriptor_header **desc); 863 void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
860 864
865/* copy a NULL-terminated vector of descriptors */
866struct usb_descriptor_header **usb_copy_descriptors(
867 struct usb_descriptor_header **);
868
869/* return copy of endpoint descriptor given original descriptor set */
870struct usb_endpoint_descriptor *usb_find_endpoint(
871 struct usb_descriptor_header **src,
872 struct usb_descriptor_header **copy,
873 struct usb_endpoint_descriptor *match);
874
875/**
876 * usb_free_descriptors - free descriptors returned by usb_copy_descriptors()
877 * @v: vector of descriptors
878 */
879static inline void usb_free_descriptors(struct usb_descriptor_header **v)
880{
881 kfree(v);
882}
883
861/*-------------------------------------------------------------------------*/ 884/*-------------------------------------------------------------------------*/
862 885
863/* utility wrapping a simple endpoint selection policy */ 886/* utility wrapping a simple endpoint selection policy */
diff --git a/include/linux/usb/irda.h b/include/linux/usb/irda.h
new file mode 100644
index 000000000000..e345ceaf72d6
--- /dev/null
+++ b/include/linux/usb/irda.h
@@ -0,0 +1,151 @@
1/*
2 * USB IrDA Bridge Device Definition
3 */
4
5#ifndef __LINUX_USB_IRDA_H
6#define __LINUX_USB_IRDA_H
7
8/* This device should use Application-specific class */
9
10#define USB_SUBCLASS_IRDA 0x02
11
12/*-------------------------------------------------------------------------*/
13
14/* Class-Specific requests (bRequest field) */
15
16#define USB_REQ_CS_IRDA_RECEIVING 1
17#define USB_REQ_CS_IRDA_CHECK_MEDIA_BUSY 3
18#define USB_REQ_CS_IRDA_RATE_SNIFF 4
19#define USB_REQ_CS_IRDA_UNICAST_LIST 5
20#define USB_REQ_CS_IRDA_GET_CLASS_DESC 6
21
22/*-------------------------------------------------------------------------*/
23
24/* Class-Specific descriptor */
25
26#define USB_DT_CS_IRDA 0x21
27
28/*-------------------------------------------------------------------------*/
29
30/* Data sizes */
31
32#define USB_IRDA_DS_2048 (1 << 5)
33#define USB_IRDA_DS_1024 (1 << 4)
34#define USB_IRDA_DS_512 (1 << 3)
35#define USB_IRDA_DS_256 (1 << 2)
36#define USB_IRDA_DS_128 (1 << 1)
37#define USB_IRDA_DS_64 (1 << 0)
38
39/* Window sizes */
40
41#define USB_IRDA_WS_7 (1 << 6)
42#define USB_IRDA_WS_6 (1 << 5)
43#define USB_IRDA_WS_5 (1 << 4)
44#define USB_IRDA_WS_4 (1 << 3)
45#define USB_IRDA_WS_3 (1 << 2)
46#define USB_IRDA_WS_2 (1 << 1)
47#define USB_IRDA_WS_1 (1 << 0)
48
49/* Min turnaround times in usecs */
50
51#define USB_IRDA_MTT_0 (1 << 7)
52#define USB_IRDA_MTT_10 (1 << 6)
53#define USB_IRDA_MTT_50 (1 << 5)
54#define USB_IRDA_MTT_100 (1 << 4)
55#define USB_IRDA_MTT_500 (1 << 3)
56#define USB_IRDA_MTT_1000 (1 << 2)
57#define USB_IRDA_MTT_5000 (1 << 1)
58#define USB_IRDA_MTT_10000 (1 << 0)
59
60/* Baud rates */
61
62#define USB_IRDA_BR_4000000 (1 << 8)
63#define USB_IRDA_BR_1152000 (1 << 7)
64#define USB_IRDA_BR_576000 (1 << 6)
65#define USB_IRDA_BR_115200 (1 << 5)
66#define USB_IRDA_BR_57600 (1 << 4)
67#define USB_IRDA_BR_38400 (1 << 3)
68#define USB_IRDA_BR_19200 (1 << 2)
69#define USB_IRDA_BR_9600 (1 << 1)
70#define USB_IRDA_BR_2400 (1 << 0)
71
72/* Additional BOFs */
73
74#define USB_IRDA_AB_0 (1 << 7)
75#define USB_IRDA_AB_1 (1 << 6)
76#define USB_IRDA_AB_2 (1 << 5)
77#define USB_IRDA_AB_3 (1 << 4)
78#define USB_IRDA_AB_6 (1 << 3)
79#define USB_IRDA_AB_12 (1 << 2)
80#define USB_IRDA_AB_24 (1 << 1)
81#define USB_IRDA_AB_48 (1 << 0)
82
83/* IRDA Rate Sniff */
84
85#define USB_IRDA_RATE_SNIFF 1
86
87/*-------------------------------------------------------------------------*/
88
89struct usb_irda_cs_descriptor {
90 __u8 bLength;
91 __u8 bDescriptorType;
92
93 __le16 bcdSpecRevision;
94 __u8 bmDataSize;
95 __u8 bmWindowSize;
96 __u8 bmMinTurnaroundTime;
97 __le16 wBaudRate;
98 __u8 bmAdditionalBOFs;
99 __u8 bIrdaRateSniff;
100 __u8 bMaxUnicastList;
101} __attribute__ ((packed));
102
103/*-------------------------------------------------------------------------*/
104
105/* Data Format */
106
107#define USB_IRDA_STATUS_MEDIA_BUSY (1 << 7)
108
109/* The following is a 4-bit value used for both
110 * inbound and outbound headers:
111 *
112 * 0 - speed ignored
113 * 1 - 2400 bps
114 * 2 - 9600 bps
115 * 3 - 19200 bps
116 * 4 - 38400 bps
117 * 5 - 57600 bps
118 * 6 - 115200 bps
119 * 7 - 576000 bps
120 * 8 - 1.152 Mbps
121 * 9 - 5 mbps
122 * 10..15 - Reserved
123 */
124#define USB_IRDA_STATUS_LINK_SPEED 0x0f
125
126/* The following is a 4-bit value used only for
127 * outbound header:
128 *
129 * 0 - No change (BOF ignored)
130 * 1 - 48 BOFs
131 * 2 - 24 BOFs
132 * 3 - 12 BOFs
133 * 4 - 6 BOFs
134 * 5 - 3 BOFs
135 * 6 - 2 BOFs
136 * 7 - 1 BOFs
137 * 8 - 0 BOFs
138 * 9..15 - Reserved
139 */
140#define USB_IRDA_EXTRA_BOFS 0xf0
141
142struct usb_irda_inbound_header {
143 __u8 bmStatus;
144};
145
146struct usb_irda_outbound_header {
147 __u8 bmChange;
148};
149
150#endif /* __LINUX_USB_IRDA_H */
151
diff --git a/include/linux/usbdevice_fs.h b/include/linux/usbdevice_fs.h
index 3118ede2c67b..0044d9b4cb85 100644
--- a/include/linux/usbdevice_fs.h
+++ b/include/linux/usbdevice_fs.h
@@ -22,8 +22,6 @@
22 * 22 *
23 * History: 23 * History:
24 * 0.1 04.01.2000 Created 24 * 0.1 04.01.2000 Created
25 *
26 * $Id: usbdevice_fs.h,v 1.1 2000/01/06 18:40:41 tom Exp $
27 */ 25 */
28 26
29/*****************************************************************************/ 27/*****************************************************************************/