aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@woody.linux-foundation.org>2007-04-27 17:19:17 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-04-27 17:19:17 -0400
commit50f732ee63b91eb08a29974b36bd63e1150bb642 (patch)
treefdfc63411a34ffbe26a3b0a997aaeff742a0301b
parentaa5bc2b58e3344da57f26b62e99e13e91c9e0a94 (diff)
parenta7205b30106a2d4ee268132644cdb292da2d9b41 (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6: (78 commits) USB: update MAINAINERS and CREDITS for Freescale USB driver USB: update gadget files for fsl_usb2_udc driver USB: add Freescale high-speed USB SOC device controller driver USB: quirk for broken suspend of IT8152F/G USB: iowarrior.c: timeouts too small in usb_control_msg calls USB: dell device id for option.c USB: Remove Huawei unusual_devs entry USB: CP2101 New Device IDs USB: add picdem device to ldusb usbfs micro optimitation USB: remove ancient/broken CRIS hcd usb ethernet gadget, workaround network stack API glitch USB: add "busnum" attribute for USB devices USB: cxacru: ADSL state management usbatm: Detect usb device shutdown and ignore failed urbs USB: Remove duplicate define of OHCI_QUIRK_ZFMICRO USB: BandRich BandLuxe HSDPA Data Card Driver USB gadget rndis: fix struct rndis_packet_msg_type unaligned bug USB Elan FTDI: check for driver registration status USB: sierra: add more checks on shutdown ...
-rw-r--r--CREDITS8
-rw-r--r--Documentation/ABI/testing/sysfs-bus-usb41
-rw-r--r--Documentation/kernel-parameters.txt2
-rw-r--r--Documentation/usb/usbmon.txt80
-rw-r--r--MAINTAINERS12
-rw-r--r--drivers/block/ub.c11
-rw-r--r--drivers/usb/Makefile1
-rw-r--r--drivers/usb/atm/cxacru.c411
-rw-r--r--drivers/usb/atm/usbatm.c17
-rw-r--r--drivers/usb/class/cdc-acm.c81
-rw-r--r--drivers/usb/class/cdc-acm.h3
-rw-r--r--drivers/usb/core/Kconfig25
-rw-r--r--drivers/usb/core/devio.c96
-rw-r--r--drivers/usb/core/driver.c255
-rw-r--r--drivers/usb/core/hcd.c34
-rw-r--r--drivers/usb/core/hcd.h3
-rw-r--r--drivers/usb/core/hub.c25
-rw-r--r--drivers/usb/core/inode.c2
-rw-r--r--drivers/usb/core/message.c81
-rw-r--r--drivers/usb/core/quirks.c2
-rw-r--r--drivers/usb/core/sysfs.c102
-rw-r--r--drivers/usb/core/usb.c46
-rw-r--r--drivers/usb/core/usb.h26
-rw-r--r--drivers/usb/gadget/Kconfig22
-rw-r--r--drivers/usb/gadget/Makefile1
-rw-r--r--drivers/usb/gadget/ether.c6
-rw-r--r--drivers/usb/gadget/fsl_usb2_udc.c2500
-rw-r--r--drivers/usb/gadget/fsl_usb2_udc.h579
-rw-r--r--drivers/usb/gadget/gadget_chips.h8
-rw-r--r--drivers/usb/gadget/pxa2xx_udc.c98
-rw-r--r--drivers/usb/gadget/rndis.h2
-rw-r--r--drivers/usb/host/Makefile1
-rw-r--r--drivers/usb/host/ehci-fsl.h4
-rw-r--r--drivers/usb/host/ehci-hub.c4
-rw-r--r--drivers/usb/host/hc_crisv10.c4550
-rw-r--r--drivers/usb/host/hc_crisv10.h289
-rw-r--r--drivers/usb/host/ohci-pci.c32
-rw-r--r--drivers/usb/host/uhci-q.c16
-rw-r--r--drivers/usb/input/ati_remote2.c89
-rw-r--r--drivers/usb/input/gtco.c5
-rw-r--r--drivers/usb/misc/adutux.c48
-rw-r--r--drivers/usb/misc/cypress_cy7c63.c4
-rw-r--r--drivers/usb/misc/ftdi-elan.c19
-rw-r--r--drivers/usb/misc/iowarrior.c20
-rw-r--r--drivers/usb/misc/ldusb.c3
-rw-r--r--drivers/usb/misc/usblcd.c7
-rw-r--r--drivers/usb/mon/mon_bin.c14
-rw-r--r--drivers/usb/mon/mon_main.c158
-rw-r--r--drivers/usb/mon/mon_text.c315
-rw-r--r--drivers/usb/mon/usb_mon.h6
-rw-r--r--drivers/usb/net/catc.c27
-rw-r--r--drivers/usb/net/dm9601.c5
-rw-r--r--drivers/usb/net/rndis_host.c112
-rw-r--r--drivers/usb/net/usbnet.c8
-rw-r--r--drivers/usb/net/usbnet.h1
-rw-r--r--drivers/usb/serial/Kconfig6
-rw-r--r--drivers/usb/serial/aircable.c7
-rw-r--r--drivers/usb/serial/ark3116.c3
-rw-r--r--drivers/usb/serial/cp2101.c2
-rw-r--r--drivers/usb/serial/ftdi_sio.c23
-rw-r--r--drivers/usb/serial/ftdi_sio.h1
-rw-r--r--drivers/usb/serial/io_edgeport.c139
-rw-r--r--drivers/usb/serial/io_edgeport.h6
-rw-r--r--drivers/usb/serial/ipaq.c1
-rw-r--r--drivers/usb/serial/kl5kusb105.c28
-rw-r--r--drivers/usb/serial/mct_u232.c12
-rw-r--r--drivers/usb/serial/mos7720.c34
-rw-r--r--drivers/usb/serial/mos7840.c233
-rw-r--r--drivers/usb/serial/omninet.c40
-rw-r--r--drivers/usb/serial/option.c23
-rw-r--r--drivers/usb/serial/sierra.c25
-rw-r--r--drivers/usb/serial/visor.c22
-rw-r--r--drivers/usb/serial/whiteheat.c8
-rw-r--r--drivers/usb/serial/whiteheat.h4
-rw-r--r--drivers/usb/storage/libusual.c3
-rw-r--r--drivers/usb/storage/unusual_devs.h9
-rw-r--r--drivers/usb/usb-skeleton.c41
-rw-r--r--include/linux/usb.h22
-rw-r--r--include/linux/usb/cdc.h11
-rw-r--r--include/linux/usb/ch9.h15
80 files changed, 5355 insertions, 5680 deletions
diff --git a/CREDITS b/CREDITS
index dede114d046e..d7140309e06d 100644
--- a/CREDITS
+++ b/CREDITS
@@ -3301,6 +3301,14 @@ S: 12725 SW Millikan Way, Suite 400
3301S: Beaverton, Oregon 97005 3301S: Beaverton, Oregon 97005
3302S: USA 3302S: USA
3303 3303
3304N: Li Yang
3305E: leoli@freescale.com
3306D: Freescale Highspeed USB device driver
3307D: Freescale QE SoC support and Ethernet driver
3308S: B-1206 Jingmao Guojigongyu
3309S: 16 Baliqiao Nanjie, Beijing 101100
3310S: People's Repulic of China
3311
3304N: Marcelo Tosatti 3312N: Marcelo Tosatti
3305E: marcelo@kvack.org 3313E: marcelo@kvack.org
3306D: v2.4 kernel maintainer 3314D: v2.4 kernel maintainer
diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb
new file mode 100644
index 000000000000..f9937add033d
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-usb
@@ -0,0 +1,41 @@
1What: /sys/bus/usb/devices/.../power/autosuspend
2Date: March 2007
3KernelVersion: 2.6.21
4Contact: Alan Stern <stern@rowland.harvard.edu>
5Description:
6 Each USB device directory will contain a file named
7 power/autosuspend. This file holds the time (in seconds)
8 the device must be idle before it will be autosuspended.
9 0 means the device will be autosuspended as soon as
10 possible. Negative values will prevent the device from
11 being autosuspended at all, and writing a negative value
12 will resume the device if it is already suspended.
13
14 The autosuspend delay for newly-created devices is set to
15 the value of the usbcore.autosuspend module parameter.
16
17What: /sys/bus/usb/devices/.../power/level
18Date: March 2007
19KernelVersion: 2.6.21
20Contact: Alan Stern <stern@rowland.harvard.edu>
21Description:
22 Each USB device directory will contain a file named
23 power/level. This file holds a power-level setting for
24 the device, one of "on", "auto", or "suspend".
25
26 "on" means that the device is not allowed to autosuspend,
27 although normal suspends for system sleep will still
28 be honored. "auto" means the device will autosuspend
29 and autoresume in the usual manner, according to the
30 capabilities of its driver. "suspend" means the device
31 is forced into a suspended state and it will not autoresume
32 in response to I/O requests. However remote-wakeup requests
33 from the device may still be enabled (the remote-wakeup
34 setting is controlled separately by the power/wakeup
35 attribute).
36
37 During normal use, devices should be left in the "auto"
38 level. The other levels are meant for administrative uses.
39 If you want to suspend a device immediately but leave it
40 free to wake up in response to I/O requests, you should
41 write "0" to power/autosuspend.
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
index 12533a958c51..2017942e0966 100644
--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -1792,7 +1792,7 @@ and is between 256 and 4096 characters. It is defined in the file
1792 for newly-detected USB devices (default 2). This 1792 for newly-detected USB devices (default 2). This
1793 is the time required before an idle device will be 1793 is the time required before an idle device will be
1794 autosuspended. Devices for which the delay is set 1794 autosuspended. Devices for which the delay is set
1795 to 0 won't be autosuspended at all. 1795 to a negative value won't be autosuspended at all.
1796 1796
1797 usbhid.mousepoll= 1797 usbhid.mousepoll=
1798 [USBHID] The interval which mice are to be polled at. 1798 [USBHID] The interval which mice are to be polled at.
diff --git a/Documentation/usb/usbmon.txt b/Documentation/usb/usbmon.txt
index 0f6808abd612..53ae866ae37b 100644
--- a/Documentation/usb/usbmon.txt
+++ b/Documentation/usb/usbmon.txt
@@ -16,7 +16,7 @@ situation as with tcpdump.
16 16
17Unlike the packet socket, usbmon has an interface which provides traces 17Unlike the packet socket, usbmon has an interface which provides traces
18in a text format. This is used for two purposes. First, it serves as a 18in a text format. This is used for two purposes. First, it serves as a
19common trace exchange format for tools while most sophisticated formats 19common trace exchange format for tools while more sophisticated formats
20are finalized. Second, humans can read it in case tools are not available. 20are finalized. Second, humans can read it in case tools are not available.
21 21
22To collect a raw text trace, execute following steps. 22To collect a raw text trace, execute following steps.
@@ -34,7 +34,7 @@ if usbmon is built into the kernel.
34Verify that bus sockets are present. 34Verify that bus sockets are present.
35 35
36# ls /sys/kernel/debug/usbmon 36# ls /sys/kernel/debug/usbmon
371s 1t 2s 2t 3s 3t 4s 4t 371s 1t 1u 2s 2t 2u 3s 3t 3u 4s 4t 4u
38# 38#
39 39
402. Find which bus connects to the desired device 402. Find which bus connects to the desired device
@@ -54,7 +54,7 @@ Bus=03 means it's bus 3.
54 54
553. Start 'cat' 553. Start 'cat'
56 56
57# cat /sys/kernel/debug/usbmon/3t > /tmp/1.mon.out 57# cat /sys/kernel/debug/usbmon/3u > /tmp/1.mon.out
58 58
59This process will be reading until killed. Naturally, the output can be 59This process will be reading until killed. Naturally, the output can be
60redirected to a desirable location. This is preferred, because it is going 60redirected to a desirable location. This is preferred, because it is going
@@ -75,46 +75,80 @@ that the file size is not excessive for your favourite editor.
75 75
76* Raw text data format 76* Raw text data format
77 77
78The '1t' type data consists of a stream of events, such as URB submission, 78Two formats are supported currently: the original, or '1t' format, and
79the '1u' format. The '1t' format is deprecated in kernel 2.6.21. The '1u'
80format adds a few fields, such as ISO frame descriptors, interval, etc.
81It produces slightly longer lines, but otherwise is a perfect superset
82of '1t' format.
83
84If it is desired to recognize one from the other in a program, look at the
85"address" word (see below), where '1u' format adds a bus number. If 2 colons
86are present, it's the '1t' format, otherwise '1u'.
87
88Any text format data consists of a stream of events, such as URB submission,
79URB callback, submission error. Every event is a text line, which consists 89URB callback, submission error. Every event is a text line, which consists
80of whitespace separated words. The number or position of words may depend 90of whitespace separated words. The number or position of words may depend
81on the event type, but there is a set of words, common for all types. 91on the event type, but there is a set of words, common for all types.
82 92
83Here is the list of words, from left to right: 93Here is the list of words, from left to right:
94
84- URB Tag. This is used to identify URBs is normally a kernel mode address 95- URB Tag. This is used to identify URBs is normally a kernel mode address
85 of the URB structure in hexadecimal. 96 of the URB structure in hexadecimal.
97
86- Timestamp in microseconds, a decimal number. The timestamp's resolution 98- Timestamp in microseconds, a decimal number. The timestamp's resolution
87 depends on available clock, and so it can be much worse than a microsecond 99 depends on available clock, and so it can be much worse than a microsecond
88 (if the implementation uses jiffies, for example). 100 (if the implementation uses jiffies, for example).
101
89- Event Type. This type refers to the format of the event, not URB type. 102- Event Type. This type refers to the format of the event, not URB type.
90 Available types are: S - submission, C - callback, E - submission error. 103 Available types are: S - submission, C - callback, E - submission error.
91- "Pipe". The pipe concept is deprecated. This is a composite word, used to 104
92 be derived from information in pipes. It consists of three fields, separated 105- "Address" word (formerly a "pipe"). It consists of four fields, separated by
93 by colons: URB type and direction, Device address, Endpoint number. 106 colons: URB type and direction, Bus number, Device address, Endpoint number.
94 Type and direction are encoded with two bytes in the following manner: 107 Type and direction are encoded with two bytes in the following manner:
95 Ci Co Control input and output 108 Ci Co Control input and output
96 Zi Zo Isochronous input and output 109 Zi Zo Isochronous input and output
97 Ii Io Interrupt input and output 110 Ii Io Interrupt input and output
98 Bi Bo Bulk input and output 111 Bi Bo Bulk input and output
99 Device address and Endpoint number are 3-digit and 2-digit (respectively) 112 Bus number, Device address, and Endpoint are decimal numbers, but they may
100 decimal numbers, with leading zeroes. 113 have leading zeros, for the sake of human readers.
101- URB Status. In most cases, this field contains a number, sometimes negative, 114
102 which represents a "status" field of the URB. This field makes no sense for 115- URB Status word. This is either a letter, or several numbers separated
103 submissions, but is present anyway to help scripts with parsing. When an 116 by colons: URB status, interval, start frame, and error count. Unlike the
104 error occurs, the field contains the error code. In case of a submission of 117 "address" word, all fields save the status are optional. Interval is printed
105 a Control packet, this field contains a Setup Tag instead of an error code. 118 only for interrupt and isochronous URBs. Start frame is printed only for
106 It is easy to tell whether the Setup Tag is present because it is never a 119 isochronous URBs. Error count is printed only for isochronous callback
107 number. Thus if scripts find a number in this field, they proceed to read 120 events.
108 Data Length. If they find something else, like a letter, they read the setup 121
109 packet before reading the Data Length. 122 The status field is a decimal number, sometimes negative, which represents
123 a "status" field of the URB. This field makes no sense for submissions, but
124 is present anyway to help scripts with parsing. When an error occurs, the
125 field contains the error code.
126
127 In case of a submission of a Control packet, this field contains a Setup Tag
128 instead of an group of numbers. It is easy to tell whether the Setup Tag is
129 present because it is never a number. Thus if scripts find a set of numbers
130 in this word, they proceed to read Data Length (except for isochronous URBs).
131 If they find something else, like a letter, they read the setup packet before
132 reading the Data Length or isochronous descriptors.
133
110- Setup packet, if present, consists of 5 words: one of each for bmRequestType, 134- Setup packet, if present, consists of 5 words: one of each for bmRequestType,
111 bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0. 135 bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0.
112 These words are safe to decode if Setup Tag was 's'. Otherwise, the setup 136 These words are safe to decode if Setup Tag was 's'. Otherwise, the setup
113 packet was present, but not captured, and the fields contain filler. 137 packet was present, but not captured, and the fields contain filler.
138
139- Number of isochronous frame descriptors and descriptors themselves.
140 If an Isochronous transfer event has a set of descriptors, a total number
141 of them in an URB is printed first, then a word per descriptor, up to a
142 total of 5. The word consists of 3 colon-separated decimal numbers for
143 status, offset, and length respectively. For submissions, initial length
144 is reported. For callbacks, actual length is reported.
145
114- Data Length. For submissions, this is the requested length. For callbacks, 146- Data Length. For submissions, this is the requested length. For callbacks,
115 this is the actual length. 147 this is the actual length.
148
116- Data tag. The usbmon may not always capture data, even if length is nonzero. 149- Data tag. The usbmon may not always capture data, even if length is nonzero.
117 The data words are present only if this tag is '='. 150 The data words are present only if this tag is '='.
151
118- Data words follow, in big endian hexadecimal format. Notice that they are 152- Data words follow, in big endian hexadecimal format. Notice that they are
119 not machine words, but really just a byte stream split into words to make 153 not machine words, but really just a byte stream split into words to make
120 it easier to read. Thus, the last word may contain from one to four bytes. 154 it easier to read. Thus, the last word may contain from one to four bytes.
@@ -153,20 +187,18 @@ class ParsedLine {
153 } 187 }
154} 188}
155 189
156This format may be changed in the future.
157
158Examples: 190Examples:
159 191
160An input control transfer to get a port status. 192An input control transfer to get a port status.
161 193
162d5ea89a0 3575914555 S Ci:001:00 s a3 00 0000 0003 0004 4 < 194d5ea89a0 3575914555 S Ci:1:001:0 s a3 00 0000 0003 0004 4 <
163d5ea89a0 3575914560 C Ci:001:00 0 4 = 01050000 195d5ea89a0 3575914560 C Ci:1:001:0 0 4 = 01050000
164 196
165An output bulk transfer to send a SCSI command 0x5E in a 31-byte Bulk wrapper 197An output bulk transfer to send a SCSI command 0x5E in a 31-byte Bulk wrapper
166to a storage device at address 5: 198to a storage device at address 5:
167 199
168dd65f0e8 4128379752 S Bo:005:02 -115 31 = 55534243 5e000000 00000000 00000600 00000000 00000000 00000000 000000 200dd65f0e8 4128379752 S Bo:1:005:2 -115 31 = 55534243 5e000000 00000000 00000600 00000000 00000000 00000000 000000
169dd65f0e8 4128379808 C Bo:005:02 0 31 > 201dd65f0e8 4128379808 C Bo:1:005:2 0 31 >
170 202
171* Raw binary format and API 203* Raw binary format and API
172 204
diff --git a/MAINTAINERS b/MAINTAINERS
index 415011c9d84e..77bff8ce1a44 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -981,6 +981,11 @@ M: mhw@wittsend.com
981W: http://www.wittsend.com/computone.html 981W: http://www.wittsend.com/computone.html
982S: Maintained 982S: Maintained
983 983
984CONEXANT ACCESSRUNNER USB DRIVER
985P: Simon Arlott
986M: cxacru@fire.lp0.eu
987S: Maintained
988
984COSA/SRP SYNC SERIAL DRIVER 989COSA/SRP SYNC SERIAL DRIVER
985P: Jan "Yenya" Kasprzak 990P: Jan "Yenya" Kasprzak
986M: kas@fi.muni.cz 991M: kas@fi.muni.cz
@@ -1389,6 +1394,13 @@ L: linuxppc-embedded@ozlabs.org
1389L: netdev@vger.kernel.org 1394L: netdev@vger.kernel.org
1390S: Maintained 1395S: Maintained
1391 1396
1397FREESCALE HIGHSPEED USB DEVICE DRIVER
1398P: Li Yang
1399M: leoli@freescale.com
1400L: linux-usb-devel@lists.sourceforge.net
1401L: linuxppc-embedded@ozlabs.org
1402S: Maintained
1403
1392FILE LOCKING (flock() and fcntl()/lockf()) 1404FILE LOCKING (flock() and fcntl()/lockf())
1393P: Matthew Wilcox 1405P: Matthew Wilcox
1394M: matthew@wil.cx 1406M: matthew@wil.cx
diff --git a/drivers/block/ub.c b/drivers/block/ub.c
index 2098eff91e14..746a118a9b52 100644
--- a/drivers/block/ub.c
+++ b/drivers/block/ub.c
@@ -2132,10 +2132,13 @@ static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2132 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 2132 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2133 == USB_ENDPOINT_XFER_BULK) { 2133 == USB_ENDPOINT_XFER_BULK) {
2134 /* BULK in or out? */ 2134 /* BULK in or out? */
2135 if (ep->bEndpointAddress & USB_DIR_IN) 2135 if (ep->bEndpointAddress & USB_DIR_IN) {
2136 ep_in = ep; 2136 if (ep_in == NULL)
2137 else 2137 ep_in = ep;
2138 ep_out = ep; 2138 } else {
2139 if (ep_out == NULL)
2140 ep_out = ep;
2141 }
2139 } 2142 }
2140 } 2143 }
2141 2144
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 8b7ff467d262..c1b0affae290 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -15,7 +15,6 @@ obj-$(CONFIG_USB_OHCI_HCD) += host/
15obj-$(CONFIG_USB_UHCI_HCD) += host/ 15obj-$(CONFIG_USB_UHCI_HCD) += host/
16obj-$(CONFIG_USB_SL811_HCD) += host/ 16obj-$(CONFIG_USB_SL811_HCD) += host/
17obj-$(CONFIG_USB_U132_HCD) += host/ 17obj-$(CONFIG_USB_U132_HCD) += host/
18obj-$(CONFIG_ETRAX_USB_HOST) += host/
19obj-$(CONFIG_USB_OHCI_AT91) += host/ 18obj-$(CONFIG_USB_OHCI_AT91) += host/
20 19
21obj-$(CONFIG_USB_ACM) += class/ 20obj-$(CONFIG_USB_ACM) += class/
diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c
index 3dfa3e40e148..30b7bfbc985a 100644
--- a/drivers/usb/atm/cxacru.c
+++ b/drivers/usb/atm/cxacru.c
@@ -4,6 +4,7 @@
4 * 4 *
5 * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan 5 * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan
6 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru) 6 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
7 * Copyright (C) 2007 Simon Arlott
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify it 9 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free 10 * under the terms of the GNU General Public License as published by the Free
@@ -34,14 +35,14 @@
34#include <linux/errno.h> 35#include <linux/errno.h>
35#include <linux/slab.h> 36#include <linux/slab.h>
36#include <linux/init.h> 37#include <linux/init.h>
37#include <linux/device.h> /* FIXME: linux/firmware.h should include it itself */ 38#include <linux/device.h>
38#include <linux/firmware.h> 39#include <linux/firmware.h>
39#include <linux/mutex.h> 40#include <linux/mutex.h>
40 41
41#include "usbatm.h" 42#include "usbatm.h"
42 43
43#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands" 44#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands, Simon Arlott"
44#define DRIVER_VERSION "0.2" 45#define DRIVER_VERSION "0.3"
45#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver" 46#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver"
46 47
47static const char cxacru_driver_name[] = "cxacru"; 48static const char cxacru_driver_name[] = "cxacru";
@@ -64,7 +65,7 @@ static const char cxacru_driver_name[] = "cxacru";
64#define SDRAM_ENA 0x1 65#define SDRAM_ENA 0x1
65 66
66#define CMD_TIMEOUT 2000 /* msecs */ 67#define CMD_TIMEOUT 2000 /* msecs */
67#define POLL_INTERVAL 5000 /* msecs */ 68#define POLL_INTERVAL 1 /* secs */
68 69
69/* commands for interaction with the modem through the control channel before 70/* commands for interaction with the modem through the control channel before
70 * firmware is loaded */ 71 * firmware is loaded */
@@ -146,6 +147,13 @@ enum cxacru_info_idx {
146 CXINF_MAX = 0x1c, 147 CXINF_MAX = 0x1c,
147}; 148};
148 149
150enum cxacru_poll_state {
151 CXPOLL_STOPPING,
152 CXPOLL_STOPPED,
153 CXPOLL_POLLING,
154 CXPOLL_SHUTDOWN
155};
156
149struct cxacru_modem_type { 157struct cxacru_modem_type {
150 u32 pll_f_clk; 158 u32 pll_f_clk;
151 u32 pll_b_clk; 159 u32 pll_b_clk;
@@ -158,7 +166,12 @@ struct cxacru_data {
158 const struct cxacru_modem_type *modem_type; 166 const struct cxacru_modem_type *modem_type;
159 167
160 int line_status; 168 int line_status;
169 struct mutex adsl_state_serialize;
170 int adsl_status;
161 struct delayed_work poll_work; 171 struct delayed_work poll_work;
172 u32 card_info[CXINF_MAX];
173 struct mutex poll_state_serialize;
174 int poll_state;
162 175
163 /* contol handles */ 176 /* contol handles */
164 struct mutex cm_serialize; 177 struct mutex cm_serialize;
@@ -170,6 +183,275 @@ struct cxacru_data {
170 struct completion snd_done; 183 struct completion snd_done;
171}; 184};
172 185
186static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
187 u8 *wdata, int wsize, u8 *rdata, int rsize);
188static void cxacru_poll_status(struct work_struct *work);
189
190/* Card info exported through sysfs */
191#define CXACRU__ATTR_INIT(_name) \
192static DEVICE_ATTR(_name, S_IRUGO, cxacru_sysfs_show_##_name, NULL)
193
194#define CXACRU_CMD_INIT(_name) \
195static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, \
196 cxacru_sysfs_show_##_name, cxacru_sysfs_store_##_name)
197
198#define CXACRU_ATTR_INIT(_value, _type, _name) \
199static ssize_t cxacru_sysfs_show_##_name(struct device *dev, \
200 struct device_attribute *attr, char *buf) \
201{ \
202 struct usb_interface *intf = to_usb_interface(dev); \
203 struct usbatm_data *usbatm_instance = usb_get_intfdata(intf); \
204 struct cxacru_data *instance = usbatm_instance->driver_data; \
205 return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
206} \
207CXACRU__ATTR_INIT(_name)
208
209#define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
210#define CXACRU_CMD_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
211#define CXACRU__ATTR_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
212
213#define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
214#define CXACRU_CMD_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
215#define CXACRU__ATTR_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
216
217static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
218{
219 return snprintf(buf, PAGE_SIZE, "%u\n", value);
220}
221
222static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
223{
224 return snprintf(buf, PAGE_SIZE, "%d\n", value);
225}
226
227static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
228{
229 if (unlikely(value < 0)) {
230 return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
231 value / 100, -value % 100);
232 } else {
233 return snprintf(buf, PAGE_SIZE, "%d.%02u\n",
234 value / 100, value % 100);
235 }
236}
237
238static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
239{
240 switch (value) {
241 case 0: return snprintf(buf, PAGE_SIZE, "no\n");
242 case 1: return snprintf(buf, PAGE_SIZE, "yes\n");
243 default: return 0;
244 }
245}
246
247static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
248{
249 switch (value) {
250 case 1: return snprintf(buf, PAGE_SIZE, "not connected\n");
251 case 2: return snprintf(buf, PAGE_SIZE, "connected\n");
252 case 3: return snprintf(buf, PAGE_SIZE, "lost\n");
253 default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
254 }
255}
256
257static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
258{
259 switch (value) {
260 case 0: return snprintf(buf, PAGE_SIZE, "down\n");
261 case 1: return snprintf(buf, PAGE_SIZE, "attempting to activate\n");
262 case 2: return snprintf(buf, PAGE_SIZE, "training\n");
263 case 3: return snprintf(buf, PAGE_SIZE, "channel analysis\n");
264 case 4: return snprintf(buf, PAGE_SIZE, "exchange\n");
265 case 5: return snprintf(buf, PAGE_SIZE, "up\n");
266 case 6: return snprintf(buf, PAGE_SIZE, "waiting\n");
267 case 7: return snprintf(buf, PAGE_SIZE, "initialising\n");
268 default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
269 }
270}
271
272static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
273{
274 switch (value) {
275 case 0: return 0;
276 case 1: return snprintf(buf, PAGE_SIZE, "ANSI T1.413\n");
277 case 2: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.1 (G.DMT)\n");
278 case 3: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.2 (G.LITE)\n");
279 default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
280 }
281}
282
283/*
284 * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
285 * this data is already in atm_dev there's no point.
286 *
287 * MAC_ADDRESS_HIGH = 0x????5544
288 * MAC_ADDRESS_LOW = 0x33221100
289 * Where 00-55 are bytes 0-5 of the MAC.
290 */
291static ssize_t cxacru_sysfs_show_mac_address(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
294 struct usb_interface *intf = to_usb_interface(dev);
295 struct usbatm_data *usbatm_instance = usb_get_intfdata(intf);
296 struct atm_dev *atm_dev = usbatm_instance->atm_dev;
297
298 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
299 atm_dev->esi[0], atm_dev->esi[1], atm_dev->esi[2],
300 atm_dev->esi[3], atm_dev->esi[4], atm_dev->esi[5]);
301}
302
303static ssize_t cxacru_sysfs_show_adsl_state(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
306 struct usb_interface *intf = to_usb_interface(dev);
307 struct usbatm_data *usbatm_instance = usb_get_intfdata(intf);
308 struct cxacru_data *instance = usbatm_instance->driver_data;
309 u32 value = instance->card_info[CXINF_LINE_STARTABLE];
310
311 switch (value) {
312 case 0: return snprintf(buf, PAGE_SIZE, "running\n");
313 case 1: return snprintf(buf, PAGE_SIZE, "stopped\n");
314 default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value);
315 }
316}
317
318static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,
319 struct device_attribute *attr, const char *buf, size_t count)
320{
321 struct usb_interface *intf = to_usb_interface(dev);
322 struct usbatm_data *usbatm_instance = usb_get_intfdata(intf);
323 struct cxacru_data *instance = usbatm_instance->driver_data;
324 int ret;
325 int poll = -1;
326 char str_cmd[8];
327 int len = strlen(buf);
328
329 if (!capable(CAP_NET_ADMIN))
330 return -EACCES;
331
332 ret = sscanf(buf, "%7s", str_cmd);
333 if (ret != 1)
334 return -EINVAL;
335 ret = 0;
336
337 if (mutex_lock_interruptible(&instance->adsl_state_serialize))
338 return -ERESTARTSYS;
339
340 if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
341 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
342 if (ret < 0) {
343 atm_err(usbatm_instance, "change adsl state:"
344 " CHIP_ADSL_LINE_STOP returned %d\n", ret);
345
346 ret = -EIO;
347 } else {
348 ret = len;
349 poll = CXPOLL_STOPPED;
350 }
351 }
352
353 /* Line status is only updated every second
354 * and the device appears to only react to
355 * START/STOP every second too. Wait 1.5s to
356 * be sure that restart will have an effect. */
357 if (!strcmp(str_cmd, "restart"))
358 msleep(1500);
359
360 if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
361 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
362 if (ret < 0) {
363 atm_err(usbatm_instance, "change adsl state:"
364 " CHIP_ADSL_LINE_START returned %d\n", ret);
365
366 ret = -EIO;
367 } else {
368 ret = len;
369 poll = CXPOLL_POLLING;
370 }
371 }
372
373 if (!strcmp(str_cmd, "poll")) {
374 ret = len;
375 poll = CXPOLL_POLLING;
376 }
377
378 if (ret == 0) {
379 ret = -EINVAL;
380 poll = -1;
381 }
382
383 if (poll == CXPOLL_POLLING) {
384 mutex_lock(&instance->poll_state_serialize);
385 switch (instance->poll_state) {
386 case CXPOLL_STOPPED:
387 /* start polling */
388 instance->poll_state = CXPOLL_POLLING;
389 break;
390
391 case CXPOLL_STOPPING:
392 /* abort stop request */
393 instance->poll_state = CXPOLL_POLLING;
394 case CXPOLL_POLLING:
395 case CXPOLL_SHUTDOWN:
396 /* don't start polling */
397 poll = -1;
398 }
399 mutex_unlock(&instance->poll_state_serialize);
400 } else if (poll == CXPOLL_STOPPED) {
401 mutex_lock(&instance->poll_state_serialize);
402 /* request stop */
403 if (instance->poll_state == CXPOLL_POLLING)
404 instance->poll_state = CXPOLL_STOPPING;
405 mutex_unlock(&instance->poll_state_serialize);
406 }
407
408 mutex_unlock(&instance->adsl_state_serialize);
409
410 if (poll == CXPOLL_POLLING)
411 cxacru_poll_status(&instance->poll_work.work);
412
413 return ret;
414}
415
416/*
417 * All device attributes are included in CXACRU_ALL_FILES
418 * so that the same list can be used multiple times:
419 * INIT (define the device attributes)
420 * CREATE (create all the device files)
421 * REMOVE (remove all the device files)
422 *
423 * With the last two being defined as needed in the functions
424 * they are used in before calling CXACRU_ALL_FILES()
425 */
426#define CXACRU_ALL_FILES(_action) \
427CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE, u32, downstream_rate); \
428CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE, u32, upstream_rate); \
429CXACRU_ATTR_##_action(CXINF_LINK_STATUS, LINK, link_status); \
430CXACRU_ATTR_##_action(CXINF_LINE_STATUS, LINE, line_status); \
431CXACRU__ATTR_##_action( mac_address); \
432CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN, dB, upstream_snr_margin); \
433CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN, dB, downstream_snr_margin); \
434CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION, dB, upstream_attenuation); \
435CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION, dB, downstream_attenuation); \
436CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER, s8, transmitter_power); \
437CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME, u32, upstream_bits_per_frame); \
438CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32, downstream_bits_per_frame); \
439CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS, u32, startup_attempts); \
440CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS, u32, upstream_crc_errors); \
441CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS, u32, downstream_crc_errors); \
442CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS, u32, upstream_fec_errors); \
443CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS, u32, downstream_fec_errors); \
444CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS, u32, upstream_hec_errors); \
445CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS, u32, downstream_hec_errors); \
446CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE, bool, line_startable); \
447CXACRU_ATTR_##_action(CXINF_MODULATION, MODU, modulation); \
448CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND, u32, adsl_headend); \
449CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT, u32, adsl_headend_environment); \
450CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION, u32, adsl_controller_version); \
451CXACRU_CMD_##_action( adsl_state);
452
453CXACRU_ALL_FILES(INIT);
454
173/* the following three functions are stolen from drivers/usb/core/message.c */ 455/* the following three functions are stolen from drivers/usb/core/message.c */
174static void cxacru_blocking_completion(struct urb *urb) 456static void cxacru_blocking_completion(struct urb *urb)
175{ 457{
@@ -347,8 +629,6 @@ static int cxacru_card_status(struct cxacru_data *instance)
347 return 0; 629 return 0;
348} 630}
349 631
350static void cxacru_poll_status(struct work_struct *work);
351
352static int cxacru_atm_start(struct usbatm_data *usbatm_instance, 632static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
353 struct atm_dev *atm_dev) 633 struct atm_dev *atm_dev)
354{ 634{
@@ -357,6 +637,7 @@ static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
357 struct atm_dev *atm_dev = usbatm_instance->atm_dev; 637 struct atm_dev *atm_dev = usbatm_instance->atm_dev;
358 */ 638 */
359 int ret; 639 int ret;
640 int start_polling = 1;
360 641
361 dbg("cxacru_atm_start"); 642 dbg("cxacru_atm_start");
362 643
@@ -369,14 +650,35 @@ static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
369 } 650 }
370 651
371 /* start ADSL */ 652 /* start ADSL */
653 mutex_lock(&instance->adsl_state_serialize);
372 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0); 654 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
373 if (ret < 0) { 655 if (ret < 0) {
374 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret); 656 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
657 mutex_unlock(&instance->adsl_state_serialize);
375 return ret; 658 return ret;
376 } 659 }
377 660
378 /* Start status polling */ 661 /* Start status polling */
379 cxacru_poll_status(&instance->poll_work.work); 662 mutex_lock(&instance->poll_state_serialize);
663 switch (instance->poll_state) {
664 case CXPOLL_STOPPED:
665 /* start polling */
666 instance->poll_state = CXPOLL_POLLING;
667 break;
668
669 case CXPOLL_STOPPING:
670 /* abort stop request */
671 instance->poll_state = CXPOLL_POLLING;
672 case CXPOLL_POLLING:
673 case CXPOLL_SHUTDOWN:
674 /* don't start polling */
675 start_polling = 0;
676 }
677 mutex_unlock(&instance->poll_state_serialize);
678 mutex_unlock(&instance->adsl_state_serialize);
679
680 if (start_polling)
681 cxacru_poll_status(&instance->poll_work.work);
380 return 0; 682 return 0;
381} 683}
382 684
@@ -387,14 +689,46 @@ static void cxacru_poll_status(struct work_struct *work)
387 u32 buf[CXINF_MAX] = {}; 689 u32 buf[CXINF_MAX] = {};
388 struct usbatm_data *usbatm = instance->usbatm; 690 struct usbatm_data *usbatm = instance->usbatm;
389 struct atm_dev *atm_dev = usbatm->atm_dev; 691 struct atm_dev *atm_dev = usbatm->atm_dev;
692 int keep_polling = 1;
390 int ret; 693 int ret;
391 694
392 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX); 695 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
393 if (ret < 0) { 696 if (ret < 0) {
394 atm_warn(usbatm, "poll status: error %d\n", ret); 697 if (ret != -ESHUTDOWN)
698 atm_warn(usbatm, "poll status: error %d\n", ret);
699
700 mutex_lock(&instance->poll_state_serialize);
701 if (instance->poll_state != CXPOLL_SHUTDOWN) {
702 instance->poll_state = CXPOLL_STOPPED;
703
704 if (ret != -ESHUTDOWN)
705 atm_warn(usbatm, "polling disabled, set adsl_state"
706 " to 'start' or 'poll' to resume\n");
707 }
708 mutex_unlock(&instance->poll_state_serialize);
395 goto reschedule; 709 goto reschedule;
396 } 710 }
397 711
712 memcpy(instance->card_info, buf, sizeof(instance->card_info));
713
714 if (instance->adsl_status != buf[CXINF_LINE_STARTABLE]) {
715 instance->adsl_status = buf[CXINF_LINE_STARTABLE];
716
717 switch (instance->adsl_status) {
718 case 0:
719 atm_printk(KERN_INFO, usbatm, "ADSL state: running\n");
720 break;
721
722 case 1:
723 atm_printk(KERN_INFO, usbatm, "ADSL state: stopped\n");
724 break;
725
726 default:
727 atm_printk(KERN_INFO, usbatm, "Unknown adsl status %02x\n", instance->adsl_status);
728 break;
729 }
730 }
731
398 if (instance->line_status == buf[CXINF_LINE_STATUS]) 732 if (instance->line_status == buf[CXINF_LINE_STATUS])
399 goto reschedule; 733 goto reschedule;
400 734
@@ -449,7 +783,20 @@ static void cxacru_poll_status(struct work_struct *work)
449 break; 783 break;
450 } 784 }
451reschedule: 785reschedule:
452 schedule_delayed_work(&instance->poll_work, msecs_to_jiffies(POLL_INTERVAL)); 786
787 mutex_lock(&instance->poll_state_serialize);
788 if (instance->poll_state == CXPOLL_STOPPING &&
789 instance->adsl_status == 1 && /* stopped */
790 instance->line_status == 0) /* down */
791 instance->poll_state = CXPOLL_STOPPED;
792
793 if (instance->poll_state == CXPOLL_STOPPED)
794 keep_polling = 0;
795 mutex_unlock(&instance->poll_state_serialize);
796
797 if (keep_polling)
798 schedule_delayed_work(&instance->poll_work,
799 round_jiffies_relative(POLL_INTERVAL*HZ));
453} 800}
454 801
455static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw, 802static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
@@ -684,6 +1031,14 @@ static int cxacru_bind(struct usbatm_data *usbatm_instance,
684 1031
685 instance->usbatm = usbatm_instance; 1032 instance->usbatm = usbatm_instance;
686 instance->modem_type = (struct cxacru_modem_type *) id->driver_info; 1033 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1034 memset(instance->card_info, 0, sizeof(instance->card_info));
1035
1036 mutex_init(&instance->poll_state_serialize);
1037 instance->poll_state = CXPOLL_STOPPED;
1038 instance->line_status = -1;
1039 instance->adsl_status = -1;
1040
1041 mutex_init(&instance->adsl_state_serialize);
687 1042
688 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL); 1043 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
689 if (!instance->rcv_buf) { 1044 if (!instance->rcv_buf) {
@@ -710,6 +1065,13 @@ static int cxacru_bind(struct usbatm_data *usbatm_instance,
710 goto fail; 1065 goto fail;
711 } 1066 }
712 1067
1068 #define CXACRU_DEVICE_CREATE_FILE(_name) \
1069 ret = device_create_file(&intf->dev, &dev_attr_##_name); \
1070 if (unlikely(ret)) \
1071 goto fail_sysfs;
1072 CXACRU_ALL_FILES(CREATE);
1073 #undef CXACRU_DEVICE_CREATE_FILE
1074
713 usb_fill_int_urb(instance->rcv_urb, 1075 usb_fill_int_urb(instance->rcv_urb,
714 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD), 1076 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
715 instance->rcv_buf, PAGE_SIZE, 1077 instance->rcv_buf, PAGE_SIZE,
@@ -730,6 +1092,14 @@ static int cxacru_bind(struct usbatm_data *usbatm_instance,
730 1092
731 return 0; 1093 return 0;
732 1094
1095 fail_sysfs:
1096 dbg("cxacru_bind: device_create_file failed (%d)\n", ret);
1097
1098 #define CXACRU_DEVICE_REMOVE_FILE(_name) \
1099 device_remove_file(&intf->dev, &dev_attr_##_name);
1100 CXACRU_ALL_FILES(REMOVE);
1101 #undef CXACRU_DEVICE_REVOVE_FILE
1102
733 fail: 1103 fail:
734 free_page((unsigned long) instance->snd_buf); 1104 free_page((unsigned long) instance->snd_buf);
735 free_page((unsigned long) instance->rcv_buf); 1105 free_page((unsigned long) instance->rcv_buf);
@@ -744,6 +1114,7 @@ static void cxacru_unbind(struct usbatm_data *usbatm_instance,
744 struct usb_interface *intf) 1114 struct usb_interface *intf)
745{ 1115{
746 struct cxacru_data *instance = usbatm_instance->driver_data; 1116 struct cxacru_data *instance = usbatm_instance->driver_data;
1117 int is_polling = 1;
747 1118
748 dbg("cxacru_unbind entered"); 1119 dbg("cxacru_unbind entered");
749 1120
@@ -752,8 +1123,20 @@ static void cxacru_unbind(struct usbatm_data *usbatm_instance,
752 return; 1123 return;
753 } 1124 }
754 1125
755 while (!cancel_delayed_work(&instance->poll_work)) 1126 mutex_lock(&instance->poll_state_serialize);
756 flush_scheduled_work(); 1127 BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
1128
1129 /* ensure that status polling continues unless
1130 * it has already stopped */
1131 if (instance->poll_state == CXPOLL_STOPPED)
1132 is_polling = 0;
1133
1134 /* stop polling from being stopped or started */
1135 instance->poll_state = CXPOLL_SHUTDOWN;
1136 mutex_unlock(&instance->poll_state_serialize);
1137
1138 if (is_polling)
1139 cancel_rearming_delayed_work(&instance->poll_work);
757 1140
758 usb_kill_urb(instance->snd_urb); 1141 usb_kill_urb(instance->snd_urb);
759 usb_kill_urb(instance->rcv_urb); 1142 usb_kill_urb(instance->rcv_urb);
@@ -762,6 +1145,12 @@ static void cxacru_unbind(struct usbatm_data *usbatm_instance,
762 1145
763 free_page((unsigned long) instance->snd_buf); 1146 free_page((unsigned long) instance->snd_buf);
764 free_page((unsigned long) instance->rcv_buf); 1147 free_page((unsigned long) instance->rcv_buf);
1148
1149 #define CXACRU_DEVICE_REMOVE_FILE(_name) \
1150 device_remove_file(&intf->dev, &dev_attr_##_name);
1151 CXACRU_ALL_FILES(REMOVE);
1152 #undef CXACRU_DEVICE_REVOVE_FILE
1153
765 kfree(instance); 1154 kfree(instance);
766 1155
767 usbatm_instance->driver_data = NULL; 1156 usbatm_instance->driver_data = NULL;
diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c
index d3e2c5f90a26..b3f779f5933a 100644
--- a/drivers/usb/atm/usbatm.c
+++ b/drivers/usb/atm/usbatm.c
@@ -274,6 +274,9 @@ static void usbatm_complete(struct urb *urb)
274 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) || 274 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
275 urb->status != -EILSEQ )) 275 urb->status != -EILSEQ ))
276 { 276 {
277 if (urb->status == -ESHUTDOWN)
278 return;
279
277 if (printk_ratelimit()) 280 if (printk_ratelimit())
278 atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n", 281 atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
279 __func__, urb, urb->status); 282 __func__, urb, urb->status);
@@ -968,6 +971,14 @@ static int usbatm_atm_init(struct usbatm_data *instance)
968 /* temp init ATM device, set to 128kbit */ 971 /* temp init ATM device, set to 128kbit */
969 atm_dev->link_rate = 128 * 1000 / 424; 972 atm_dev->link_rate = 128 * 1000 / 424;
970 973
974 ret = sysfs_create_link(&atm_dev->class_dev.kobj,
975 &instance->usb_intf->dev.kobj, "device");
976 if (ret) {
977 atm_err(instance, "%s: sysfs_create_link failed: %d\n",
978 __func__, ret);
979 goto fail_sysfs;
980 }
981
971 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) { 982 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
972 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret); 983 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
973 goto fail; 984 goto fail;
@@ -986,6 +997,8 @@ static int usbatm_atm_init(struct usbatm_data *instance)
986 return 0; 997 return 0;
987 998
988 fail: 999 fail:
1000 sysfs_remove_link(&atm_dev->class_dev.kobj, "device");
1001 fail_sysfs:
989 instance->atm_dev = NULL; 1002 instance->atm_dev = NULL;
990 atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */ 1003 atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
991 return ret; 1004 return ret;
@@ -1318,8 +1331,10 @@ void usbatm_usb_disconnect(struct usb_interface *intf)
1318 kfree(instance->cell_buf); 1331 kfree(instance->cell_buf);
1319 1332
1320 /* ATM finalize */ 1333 /* ATM finalize */
1321 if (instance->atm_dev) 1334 if (instance->atm_dev) {
1335 sysfs_remove_link(&instance->atm_dev->class_dev.kobj, "device");
1322 atm_dev_deregister(instance->atm_dev); 1336 atm_dev_deregister(instance->atm_dev);
1337 }
1323 1338
1324 usbatm_put_instance(instance); /* taken in usbatm_usb_probe */ 1339 usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
1325} 1340}
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 31ae661e586a..14de3b1b6a20 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -212,7 +212,41 @@ static int acm_write_start(struct acm *acm)
212 } 212 }
213 return rc; 213 return rc;
214} 214}
215/*
216 * attributes exported through sysfs
217 */
218static ssize_t show_caps
219(struct device *dev, struct device_attribute *attr, char *buf)
220{
221 struct usb_interface *intf = to_usb_interface(dev);
222 struct acm *acm = usb_get_intfdata(intf);
223
224 return sprintf(buf, "%d", acm->ctrl_caps);
225}
226static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
227
228static ssize_t show_country_codes
229(struct device *dev, struct device_attribute *attr, char *buf)
230{
231 struct usb_interface *intf = to_usb_interface(dev);
232 struct acm *acm = usb_get_intfdata(intf);
233
234 memcpy(buf, acm->country_codes, acm->country_code_size);
235 return acm->country_code_size;
236}
237
238static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
239
240static ssize_t show_country_rel_date
241(struct device *dev, struct device_attribute *attr, char *buf)
242{
243 struct usb_interface *intf = to_usb_interface(dev);
244 struct acm *acm = usb_get_intfdata(intf);
245
246 return sprintf(buf, "%d", acm->country_rel_date);
247}
215 248
249static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
216/* 250/*
217 * Interrupt handlers for various ACM device responses 251 * Interrupt handlers for various ACM device responses
218 */ 252 */
@@ -514,6 +548,7 @@ static void acm_tty_unregister(struct acm *acm)
514 usb_free_urb(acm->writeurb); 548 usb_free_urb(acm->writeurb);
515 for (i = 0; i < nr; i++) 549 for (i = 0; i < nr; i++)
516 usb_free_urb(acm->ru[i].urb); 550 usb_free_urb(acm->ru[i].urb);
551 kfree(acm->country_codes);
517 kfree(acm); 552 kfree(acm);
518} 553}
519 554
@@ -761,6 +796,7 @@ static int acm_probe (struct usb_interface *intf,
761 const struct usb_device_id *id) 796 const struct usb_device_id *id)
762{ 797{
763 struct usb_cdc_union_desc *union_header = NULL; 798 struct usb_cdc_union_desc *union_header = NULL;
799 struct usb_cdc_country_functional_desc *cfd = NULL;
764 char *buffer = intf->altsetting->extra; 800 char *buffer = intf->altsetting->extra;
765 int buflen = intf->altsetting->extralen; 801 int buflen = intf->altsetting->extralen;
766 struct usb_interface *control_interface; 802 struct usb_interface *control_interface;
@@ -824,8 +860,9 @@ static int acm_probe (struct usb_interface *intf,
824 union_header = (struct usb_cdc_union_desc *) 860 union_header = (struct usb_cdc_union_desc *)
825 buffer; 861 buffer;
826 break; 862 break;
827 case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */ 863 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
828 break; /* for now we ignore it */ 864 cfd = (struct usb_cdc_country_functional_desc *)buffer;
865 break;
829 case USB_CDC_HEADER_TYPE: /* maybe check version */ 866 case USB_CDC_HEADER_TYPE: /* maybe check version */
830 break; /* for now we ignore it */ 867 break; /* for now we ignore it */
831 case USB_CDC_ACM_TYPE: 868 case USB_CDC_ACM_TYPE:
@@ -983,6 +1020,34 @@ skip_normal_probe:
983 goto alloc_fail7; 1020 goto alloc_fail7;
984 } 1021 }
985 1022
1023 usb_set_intfdata (intf, acm);
1024
1025 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1026 if (i < 0)
1027 goto alloc_fail8;
1028
1029 if (cfd) { /* export the country data */
1030 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1031 if (!acm->country_codes)
1032 goto skip_countries;
1033 acm->country_code_size = cfd->bLength - 4;
1034 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1035 acm->country_rel_date = cfd->iCountryCodeRelDate;
1036
1037 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1038 if (i < 0) {
1039 kfree(acm->country_codes);
1040 goto skip_countries;
1041 }
1042
1043 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1044 if (i < 0) {
1045 kfree(acm->country_codes);
1046 goto skip_countries;
1047 }
1048 }
1049
1050skip_countries:
986 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), 1051 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
987 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); 1052 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
988 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1053 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
@@ -1006,9 +1071,10 @@ skip_normal_probe:
1006 tty_register_device(acm_tty_driver, minor, &control_interface->dev); 1071 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1007 1072
1008 acm_table[minor] = acm; 1073 acm_table[minor] = acm;
1009 usb_set_intfdata (intf, acm);
1010 return 0;
1011 1074
1075 return 0;
1076alloc_fail8:
1077 usb_free_urb(acm->writeurb);
1012alloc_fail7: 1078alloc_fail7:
1013 for (i = 0; i < num_rx_buf; i++) 1079 for (i = 0; i < num_rx_buf; i++)
1014 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); 1080 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
@@ -1027,7 +1093,7 @@ alloc_fail:
1027 1093
1028static void acm_disconnect(struct usb_interface *intf) 1094static void acm_disconnect(struct usb_interface *intf)
1029{ 1095{
1030 struct acm *acm = usb_get_intfdata (intf); 1096 struct acm *acm = usb_get_intfdata(intf);
1031 struct usb_device *usb_dev = interface_to_usbdev(intf); 1097 struct usb_device *usb_dev = interface_to_usbdev(intf);
1032 int i; 1098 int i;
1033 1099
@@ -1041,6 +1107,11 @@ static void acm_disconnect(struct usb_interface *intf)
1041 mutex_unlock(&open_mutex); 1107 mutex_unlock(&open_mutex);
1042 return; 1108 return;
1043 } 1109 }
1110 if (acm->country_codes){
1111 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1112 device_remove_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1113 }
1114 device_remove_file(&intf->dev, &dev_attr_bmCapabilities);
1044 acm->dev = NULL; 1115 acm->dev = NULL;
1045 usb_set_intfdata(acm->control, NULL); 1116 usb_set_intfdata(acm->control, NULL);
1046 usb_set_intfdata(acm->data, NULL); 1117 usb_set_intfdata(acm->data, NULL);
diff --git a/drivers/usb/class/cdc-acm.h b/drivers/usb/class/cdc-acm.h
index 1bcaea32cfc1..09f7765dbf8d 100644
--- a/drivers/usb/class/cdc-acm.h
+++ b/drivers/usb/class/cdc-acm.h
@@ -91,6 +91,9 @@ struct acm {
91 struct urb *ctrlurb, *writeurb; /* urbs */ 91 struct urb *ctrlurb, *writeurb; /* urbs */
92 u8 *ctrl_buffer; /* buffers of urbs */ 92 u8 *ctrl_buffer; /* buffers of urbs */
93 dma_addr_t ctrl_dma; /* dma handles of buffers */ 93 dma_addr_t ctrl_dma; /* dma handles of buffers */
94 u8 *country_codes; /* country codes from device */
95 unsigned int country_code_size; /* size of this buffer */
96 unsigned int country_rel_date; /* release date of version */
94 struct acm_wb wb[ACM_NW]; 97 struct acm_wb wb[ACM_NW];
95 struct acm_ru ru[ACM_NR]; 98 struct acm_ru ru[ACM_NR];
96 struct acm_rb rb[ACM_NR]; 99 struct acm_rb rb[ACM_NR];
diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index 2fc0f88a3d86..f493fb1eaa27 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -31,7 +31,30 @@ config USB_DEVICEFS
31 For the format of the various /proc/bus/usb/ files, please read 31 For the format of the various /proc/bus/usb/ files, please read
32 <file:Documentation/usb/proc_usb_info.txt>. 32 <file:Documentation/usb/proc_usb_info.txt>.
33 33
34 Most users want to say Y here. 34 Usbfs files can't handle Access Control Lists (ACL), which are the
35 default way to grant access to USB devices for untrusted users of a
36 desktop system. The usbfs functionality is replaced by real
37 device-nodes managed by udev. These nodes live in /dev/bus/usb and
38 are used by libusb.
39
40config USB_DEVICE_CLASS
41 bool "USB device class-devices (DEPRECATED)"
42 depends on USB
43 default n
44 ---help---
45 Userspace access to USB devices is granted by device-nodes exported
46 directly from the usbdev in sysfs. Old versions of the driver
47 core and udev needed additional class devices to export device nodes.
48
49 These additional devices are difficult to handle in userspace, if
50 information about USB interfaces must be available. One device contains
51 the device node, the other device contains the interface data. Both
52 devices are at the same level in sysfs (siblings) and one can't access
53 the other. The device node created directly by the usbdev is the parent
54 device of the interface and therefore easily accessible from the interface
55 event.
56
57 This option provides backward compatibility if needed.
35 58
36config USB_DYNAMIC_MINORS 59config USB_DYNAMIC_MINORS
37 bool "Dynamic USB minor allocation (EXPERIMENTAL)" 60 bool "Dynamic USB minor allocation (EXPERIMENTAL)"
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index fc3545ddb06e..927a181120a9 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -57,7 +57,6 @@
57 57
58#define USB_MAXBUS 64 58#define USB_MAXBUS 64
59#define USB_DEVICE_MAX USB_MAXBUS * 128 59#define USB_DEVICE_MAX USB_MAXBUS * 128
60static struct class *usb_device_class;
61 60
62/* Mutual exclusion for removal, open, and release */ 61/* Mutual exclusion for removal, open, and release */
63DEFINE_MUTEX(usbfs_mutex); 62DEFINE_MUTEX(usbfs_mutex);
@@ -514,22 +513,25 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsig
514 return ret; 513 return ret;
515} 514}
516 515
517static struct usb_device *usbdev_lookup_minor(int minor) 516static int __match_minor(struct device *dev, void *data)
518{ 517{
519 struct device *device; 518 int minor = *((int *)data);
520 struct usb_device *udev = NULL;
521 519
522 down(&usb_device_class->sem); 520 if (dev->devt == MKDEV(USB_DEVICE_MAJOR, minor))
523 list_for_each_entry(device, &usb_device_class->devices, node) { 521 return 1;
524 if (device->devt == MKDEV(USB_DEVICE_MAJOR, minor)) { 522 return 0;
525 udev = device->platform_data; 523}
526 break;
527 }
528 }
529 up(&usb_device_class->sem);
530 524
531 return udev; 525static struct usb_device *usbdev_lookup_by_minor(int minor)
532}; 526{
527 struct device *dev;
528
529 dev = bus_find_device(&usb_bus_type, NULL, &minor, __match_minor);
530 if (!dev)
531 return NULL;
532 put_device(dev);
533 return container_of(dev, struct usb_device, dev);
534}
533 535
534/* 536/*
535 * file operations 537 * file operations
@@ -548,11 +550,14 @@ static int usbdev_open(struct inode *inode, struct file *file)
548 goto out; 550 goto out;
549 551
550 ret = -ENOENT; 552 ret = -ENOENT;
551 /* check if we are called from a real node or usbfs */ 553 /* usbdev device-node */
552 if (imajor(inode) == USB_DEVICE_MAJOR) 554 if (imajor(inode) == USB_DEVICE_MAJOR)
553 dev = usbdev_lookup_minor(iminor(inode)); 555 dev = usbdev_lookup_by_minor(iminor(inode));
556#ifdef CONFIG_USB_DEVICEFS
557 /* procfs file */
554 if (!dev) 558 if (!dev)
555 dev = inode->i_private; 559 dev = inode->i_private;
560#endif
556 if (!dev) 561 if (!dev)
557 goto out; 562 goto out;
558 ret = usb_autoresume_device(dev); 563 ret = usb_autoresume_device(dev);
@@ -575,7 +580,7 @@ static int usbdev_open(struct inode *inode, struct file *file)
575 ps->disccontext = NULL; 580 ps->disccontext = NULL;
576 ps->ifclaimed = 0; 581 ps->ifclaimed = 0;
577 security_task_getsecid(current, &ps->secid); 582 security_task_getsecid(current, &ps->secid);
578 wmb(); 583 smp_wmb();
579 list_add_tail(&ps->list, &dev->filelist); 584 list_add_tail(&ps->list, &dev->filelist);
580 file->private_data = ps; 585 file->private_data = ps;
581 out: 586 out:
@@ -1570,7 +1575,7 @@ static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wai
1570 return mask; 1575 return mask;
1571} 1576}
1572 1577
1573const struct file_operations usbfs_device_file_operations = { 1578const struct file_operations usbdev_file_operations = {
1574 .llseek = usbdev_lseek, 1579 .llseek = usbdev_lseek,
1575 .read = usbdev_read, 1580 .read = usbdev_read,
1576 .poll = usbdev_poll, 1581 .poll = usbdev_poll,
@@ -1579,50 +1584,53 @@ const struct file_operations usbfs_device_file_operations = {
1579 .release = usbdev_release, 1584 .release = usbdev_release,
1580}; 1585};
1581 1586
1582static int usbdev_add(struct usb_device *dev) 1587#ifdef CONFIG_USB_DEVICE_CLASS
1588static struct class *usb_classdev_class;
1589
1590static int usb_classdev_add(struct usb_device *dev)
1583{ 1591{
1584 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1); 1592 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
1585 1593
1586 dev->usbfs_dev = device_create(usb_device_class, &dev->dev, 1594 dev->usb_classdev = device_create(usb_classdev_class, &dev->dev,
1587 MKDEV(USB_DEVICE_MAJOR, minor), 1595 MKDEV(USB_DEVICE_MAJOR, minor),
1588 "usbdev%d.%d", dev->bus->busnum, dev->devnum); 1596 "usbdev%d.%d", dev->bus->busnum, dev->devnum);
1589 if (IS_ERR(dev->usbfs_dev)) 1597 if (IS_ERR(dev->usb_classdev))
1590 return PTR_ERR(dev->usbfs_dev); 1598 return PTR_ERR(dev->usb_classdev);
1591 1599
1592 dev->usbfs_dev->platform_data = dev;
1593 return 0; 1600 return 0;
1594} 1601}
1595 1602
1596static void usbdev_remove(struct usb_device *dev) 1603static void usb_classdev_remove(struct usb_device *dev)
1597{ 1604{
1598 device_unregister(dev->usbfs_dev); 1605 device_unregister(dev->usb_classdev);
1599} 1606}
1600 1607
1601static int usbdev_notify(struct notifier_block *self, unsigned long action, 1608static int usb_classdev_notify(struct notifier_block *self,
1602 void *dev) 1609 unsigned long action, void *dev)
1603{ 1610{
1604 switch (action) { 1611 switch (action) {
1605 case USB_DEVICE_ADD: 1612 case USB_DEVICE_ADD:
1606 if (usbdev_add(dev)) 1613 if (usb_classdev_add(dev))
1607 return NOTIFY_BAD; 1614 return NOTIFY_BAD;
1608 break; 1615 break;
1609 case USB_DEVICE_REMOVE: 1616 case USB_DEVICE_REMOVE:
1610 usbdev_remove(dev); 1617 usb_classdev_remove(dev);
1611 break; 1618 break;
1612 } 1619 }
1613 return NOTIFY_OK; 1620 return NOTIFY_OK;
1614} 1621}
1615 1622
1616static struct notifier_block usbdev_nb = { 1623static struct notifier_block usbdev_nb = {
1617 .notifier_call = usbdev_notify, 1624 .notifier_call = usb_classdev_notify,
1618}; 1625};
1626#endif
1619 1627
1620static struct cdev usb_device_cdev = { 1628static struct cdev usb_device_cdev = {
1621 .kobj = {.name = "usb_device", }, 1629 .kobj = {.name = "usb_device", },
1622 .owner = THIS_MODULE, 1630 .owner = THIS_MODULE,
1623}; 1631};
1624 1632
1625int __init usbdev_init(void) 1633int __init usb_devio_init(void)
1626{ 1634{
1627 int retval; 1635 int retval;
1628 1636
@@ -1632,38 +1640,38 @@ int __init usbdev_init(void)
1632 err("unable to register minors for usb_device"); 1640 err("unable to register minors for usb_device");
1633 goto out; 1641 goto out;
1634 } 1642 }
1635 cdev_init(&usb_device_cdev, &usbfs_device_file_operations); 1643 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1636 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX); 1644 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1637 if (retval) { 1645 if (retval) {
1638 err("unable to get usb_device major %d", USB_DEVICE_MAJOR); 1646 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
1639 goto error_cdev; 1647 goto error_cdev;
1640 } 1648 }
1641 usb_device_class = class_create(THIS_MODULE, "usb_device"); 1649#ifdef CONFIG_USB_DEVICE_CLASS
1642 if (IS_ERR(usb_device_class)) { 1650 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1651 if (IS_ERR(usb_classdev_class)) {
1643 err("unable to register usb_device class"); 1652 err("unable to register usb_device class");
1644 retval = PTR_ERR(usb_device_class); 1653 retval = PTR_ERR(usb_classdev_class);
1645 goto error_class; 1654 cdev_del(&usb_device_cdev);
1655 usb_classdev_class = NULL;
1656 goto out;
1646 } 1657 }
1647 1658
1648 usb_register_notify(&usbdev_nb); 1659 usb_register_notify(&usbdev_nb);
1649 1660#endif
1650out: 1661out:
1651 return retval; 1662 return retval;
1652 1663
1653error_class:
1654 usb_device_class = NULL;
1655 cdev_del(&usb_device_cdev);
1656
1657error_cdev: 1664error_cdev:
1658 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 1665 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1659 goto out; 1666 goto out;
1660} 1667}
1661 1668
1662void usbdev_cleanup(void) 1669void usb_devio_cleanup(void)
1663{ 1670{
1671#ifdef CONFIG_USB_DEVICE_CLASS
1664 usb_unregister_notify(&usbdev_nb); 1672 usb_unregister_notify(&usbdev_nb);
1665 class_destroy(usb_device_class); 1673 class_destroy(usb_classdev_class);
1674#endif
1666 cdev_del(&usb_device_cdev); 1675 cdev_del(&usb_device_cdev);
1667 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 1676 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1668} 1677}
1669
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index e6dd2b9210f6..b9f7f90aef82 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -574,23 +574,10 @@ static int usb_device_match(struct device *dev, struct device_driver *drv)
574} 574}
575 575
576#ifdef CONFIG_HOTPLUG 576#ifdef CONFIG_HOTPLUG
577
578/*
579 * This sends an uevent to userspace, typically helping to load driver
580 * or other modules, configure the device, and more. Drivers can provide
581 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
582 *
583 * We're called either from khubd (the typical case) or from root hub
584 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
585 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
586 * device (and this configuration!) are still present.
587 */
588static int usb_uevent(struct device *dev, char **envp, int num_envp, 577static int usb_uevent(struct device *dev, char **envp, int num_envp,
589 char *buffer, int buffer_size) 578 char *buffer, int buffer_size)
590{ 579{
591 struct usb_interface *intf;
592 struct usb_device *usb_dev; 580 struct usb_device *usb_dev;
593 struct usb_host_interface *alt;
594 int i = 0; 581 int i = 0;
595 int length = 0; 582 int length = 0;
596 583
@@ -600,13 +587,11 @@ static int usb_uevent(struct device *dev, char **envp, int num_envp,
600 /* driver is often null here; dev_dbg() would oops */ 587 /* driver is often null here; dev_dbg() would oops */
601 pr_debug ("usb %s: uevent\n", dev->bus_id); 588 pr_debug ("usb %s: uevent\n", dev->bus_id);
602 589
603 if (is_usb_device(dev)) { 590 if (is_usb_device(dev))
604 usb_dev = to_usb_device(dev); 591 usb_dev = to_usb_device(dev);
605 alt = NULL; 592 else {
606 } else { 593 struct usb_interface *intf = to_usb_interface(dev);
607 intf = to_usb_interface(dev);
608 usb_dev = interface_to_usbdev(intf); 594 usb_dev = interface_to_usbdev(intf);
609 alt = intf->cur_altsetting;
610 } 595 }
611 596
612 if (usb_dev->devnum < 0) { 597 if (usb_dev->devnum < 0) {
@@ -621,9 +606,7 @@ static int usb_uevent(struct device *dev, char **envp, int num_envp,
621#ifdef CONFIG_USB_DEVICEFS 606#ifdef CONFIG_USB_DEVICEFS
622 /* If this is available, userspace programs can directly read 607 /* If this is available, userspace programs can directly read
623 * all the device descriptors we don't tell them about. Or 608 * all the device descriptors we don't tell them about. Or
624 * even act as usermode drivers. 609 * act as usermode drivers.
625 *
626 * FIXME reduce hardwired intelligence here
627 */ 610 */
628 if (add_uevent_var(envp, num_envp, &i, 611 if (add_uevent_var(envp, num_envp, &i,
629 buffer, buffer_size, &length, 612 buffer, buffer_size, &length,
@@ -650,44 +633,29 @@ static int usb_uevent(struct device *dev, char **envp, int num_envp,
650 usb_dev->descriptor.bDeviceProtocol)) 633 usb_dev->descriptor.bDeviceProtocol))
651 return -ENOMEM; 634 return -ENOMEM;
652 635
653 if (!is_usb_device(dev)) { 636 if (add_uevent_var(envp, num_envp, &i,
654
655 if (add_uevent_var(envp, num_envp, &i,
656 buffer, buffer_size, &length, 637 buffer, buffer_size, &length,
657 "INTERFACE=%d/%d/%d", 638 "BUSNUM=%03d",
658 alt->desc.bInterfaceClass, 639 usb_dev->bus->busnum))
659 alt->desc.bInterfaceSubClass, 640 return -ENOMEM;
660 alt->desc.bInterfaceProtocol))
661 return -ENOMEM;
662 641
663 if (add_uevent_var(envp, num_envp, &i, 642 if (add_uevent_var(envp, num_envp, &i,
664 buffer, buffer_size, &length, 643 buffer, buffer_size, &length,
665 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", 644 "DEVNUM=%03d",
666 le16_to_cpu(usb_dev->descriptor.idVendor), 645 usb_dev->devnum))
667 le16_to_cpu(usb_dev->descriptor.idProduct), 646 return -ENOMEM;
668 le16_to_cpu(usb_dev->descriptor.bcdDevice),
669 usb_dev->descriptor.bDeviceClass,
670 usb_dev->descriptor.bDeviceSubClass,
671 usb_dev->descriptor.bDeviceProtocol,
672 alt->desc.bInterfaceClass,
673 alt->desc.bInterfaceSubClass,
674 alt->desc.bInterfaceProtocol))
675 return -ENOMEM;
676 }
677 647
678 envp[i] = NULL; 648 envp[i] = NULL;
679
680 return 0; 649 return 0;
681} 650}
682 651
683#else 652#else
684 653
685static int usb_uevent(struct device *dev, char **envp, 654static int usb_uevent(struct device *dev, char **envp,
686 int num_envp, char *buffer, int buffer_size) 655 int num_envp, char *buffer, int buffer_size)
687{ 656{
688 return -ENODEV; 657 return -ENODEV;
689} 658}
690
691#endif /* CONFIG_HOTPLUG */ 659#endif /* CONFIG_HOTPLUG */
692 660
693/** 661/**
@@ -872,8 +840,10 @@ static int usb_resume_device(struct usb_device *udev)
872 840
873done: 841done:
874 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); 842 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
875 if (status == 0) 843 if (status == 0) {
844 udev->autoresume_disabled = 0;
876 udev->dev.power.power_state.event = PM_EVENT_ON; 845 udev->dev.power.power_state.event = PM_EVENT_ON;
846 }
877 return status; 847 return status;
878} 848}
879 849
@@ -962,6 +932,7 @@ static int autosuspend_check(struct usb_device *udev)
962{ 932{
963 int i; 933 int i;
964 struct usb_interface *intf; 934 struct usb_interface *intf;
935 unsigned long suspend_time;
965 936
966 /* For autosuspend, fail fast if anything is in use or autosuspend 937 /* For autosuspend, fail fast if anything is in use or autosuspend
967 * is disabled. Also fail if any interfaces require remote wakeup 938 * is disabled. Also fail if any interfaces require remote wakeup
@@ -970,9 +941,10 @@ static int autosuspend_check(struct usb_device *udev)
970 udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 941 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
971 if (udev->pm_usage_cnt > 0) 942 if (udev->pm_usage_cnt > 0)
972 return -EBUSY; 943 return -EBUSY;
973 if (!udev->autosuspend_delay) 944 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
974 return -EPERM; 945 return -EPERM;
975 946
947 suspend_time = udev->last_busy + udev->autosuspend_delay;
976 if (udev->actconfig) { 948 if (udev->actconfig) {
977 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 949 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
978 intf = udev->actconfig->interface[i]; 950 intf = udev->actconfig->interface[i];
@@ -988,6 +960,24 @@ static int autosuspend_check(struct usb_device *udev)
988 } 960 }
989 } 961 }
990 } 962 }
963
964 /* If everything is okay but the device hasn't been idle for long
965 * enough, queue a delayed autosuspend request.
966 */
967 if (time_after(suspend_time, jiffies)) {
968 if (!timer_pending(&udev->autosuspend.timer)) {
969
970 /* The value of jiffies may change between the
971 * time_after() comparison above and the subtraction
972 * below. That's okay; the system behaves sanely
973 * when a timer is registered for the present moment
974 * or for the past.
975 */
976 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
977 suspend_time - jiffies);
978 }
979 return -EAGAIN;
980 }
991 return 0; 981 return 0;
992} 982}
993 983
@@ -1033,26 +1023,25 @@ static int autosuspend_check(struct usb_device *udev)
1033 * 1023 *
1034 * This routine can run only in process context. 1024 * This routine can run only in process context.
1035 */ 1025 */
1036int usb_suspend_both(struct usb_device *udev, pm_message_t msg) 1026static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1037{ 1027{
1038 int status = 0; 1028 int status = 0;
1039 int i = 0; 1029 int i = 0;
1040 struct usb_interface *intf; 1030 struct usb_interface *intf;
1041 struct usb_device *parent = udev->parent; 1031 struct usb_device *parent = udev->parent;
1042 1032
1043 cancel_delayed_work(&udev->autosuspend); 1033 if (udev->state == USB_STATE_NOTATTACHED ||
1044 if (udev->state == USB_STATE_NOTATTACHED) 1034 udev->state == USB_STATE_SUSPENDED)
1045 return 0; 1035 goto done;
1046 if (udev->state == USB_STATE_SUSPENDED)
1047 return 0;
1048 1036
1049 udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1037 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1050 1038
1051 if (udev->auto_pm) { 1039 if (udev->auto_pm) {
1052 status = autosuspend_check(udev); 1040 status = autosuspend_check(udev);
1053 if (status < 0) 1041 if (status < 0)
1054 return status; 1042 goto done;
1055 } 1043 }
1044 cancel_delayed_work(&udev->autosuspend);
1056 1045
1057 /* Suspend all the interfaces and then udev itself */ 1046 /* Suspend all the interfaces and then udev itself */
1058 if (udev->actconfig) { 1047 if (udev->actconfig) {
@@ -1077,6 +1066,7 @@ int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1077 } else if (parent) 1066 } else if (parent)
1078 usb_autosuspend_device(parent); 1067 usb_autosuspend_device(parent);
1079 1068
1069 done:
1080 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); 1070 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1081 return status; 1071 return status;
1082} 1072}
@@ -1109,7 +1099,7 @@ int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1109 * 1099 *
1110 * This routine can run only in process context. 1100 * This routine can run only in process context.
1111 */ 1101 */
1112int usb_resume_both(struct usb_device *udev) 1102static int usb_resume_both(struct usb_device *udev)
1113{ 1103{
1114 int status = 0; 1104 int status = 0;
1115 int i; 1105 int i;
@@ -1117,11 +1107,17 @@ int usb_resume_both(struct usb_device *udev)
1117 struct usb_device *parent = udev->parent; 1107 struct usb_device *parent = udev->parent;
1118 1108
1119 cancel_delayed_work(&udev->autosuspend); 1109 cancel_delayed_work(&udev->autosuspend);
1120 if (udev->state == USB_STATE_NOTATTACHED) 1110 if (udev->state == USB_STATE_NOTATTACHED) {
1121 return -ENODEV; 1111 status = -ENODEV;
1112 goto done;
1113 }
1122 1114
1123 /* Propagate the resume up the tree, if necessary */ 1115 /* Propagate the resume up the tree, if necessary */
1124 if (udev->state == USB_STATE_SUSPENDED) { 1116 if (udev->state == USB_STATE_SUSPENDED) {
1117 if (udev->auto_pm && udev->autoresume_disabled) {
1118 status = -EPERM;
1119 goto done;
1120 }
1125 if (parent) { 1121 if (parent) {
1126 status = usb_autoresume_device(parent); 1122 status = usb_autoresume_device(parent);
1127 if (status == 0) { 1123 if (status == 0) {
@@ -1167,6 +1163,7 @@ int usb_resume_both(struct usb_device *udev)
1167 } 1163 }
1168 } 1164 }
1169 1165
1166 done:
1170 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); 1167 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1171 return status; 1168 return status;
1172} 1169}
@@ -1181,20 +1178,34 @@ static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1181 int status = 0; 1178 int status = 0;
1182 1179
1183 usb_pm_lock(udev); 1180 usb_pm_lock(udev);
1181 udev->auto_pm = 1;
1184 udev->pm_usage_cnt += inc_usage_cnt; 1182 udev->pm_usage_cnt += inc_usage_cnt;
1185 WARN_ON(udev->pm_usage_cnt < 0); 1183 WARN_ON(udev->pm_usage_cnt < 0);
1186 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { 1184 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1187 udev->auto_pm = 1; 1185 if (udev->state == USB_STATE_SUSPENDED)
1188 status = usb_resume_both(udev); 1186 status = usb_resume_both(udev);
1189 if (status != 0) 1187 if (status != 0)
1190 udev->pm_usage_cnt -= inc_usage_cnt; 1188 udev->pm_usage_cnt -= inc_usage_cnt;
1191 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) 1189 else if (inc_usage_cnt)
1192 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1190 udev->last_busy = jiffies;
1193 udev->autosuspend_delay); 1191 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1192 if (inc_usage_cnt)
1193 udev->last_busy = jiffies;
1194 status = usb_suspend_both(udev, PMSG_SUSPEND);
1195 }
1194 usb_pm_unlock(udev); 1196 usb_pm_unlock(udev);
1195 return status; 1197 return status;
1196} 1198}
1197 1199
1200/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1201void usb_autosuspend_work(struct work_struct *work)
1202{
1203 struct usb_device *udev =
1204 container_of(work, struct usb_device, autosuspend.work);
1205
1206 usb_autopm_do_device(udev, 0);
1207}
1208
1198/** 1209/**
1199 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1210 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1200 * @udev: the usb_device to autosuspend 1211 * @udev: the usb_device to autosuspend
@@ -1286,15 +1297,20 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1286 if (intf->condition == USB_INTERFACE_UNBOUND) 1297 if (intf->condition == USB_INTERFACE_UNBOUND)
1287 status = -ENODEV; 1298 status = -ENODEV;
1288 else { 1299 else {
1300 udev->auto_pm = 1;
1289 intf->pm_usage_cnt += inc_usage_cnt; 1301 intf->pm_usage_cnt += inc_usage_cnt;
1290 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) { 1302 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1291 udev->auto_pm = 1; 1303 if (udev->state == USB_STATE_SUSPENDED)
1292 status = usb_resume_both(udev); 1304 status = usb_resume_both(udev);
1293 if (status != 0) 1305 if (status != 0)
1294 intf->pm_usage_cnt -= inc_usage_cnt; 1306 intf->pm_usage_cnt -= inc_usage_cnt;
1295 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) 1307 else if (inc_usage_cnt)
1296 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1308 udev->last_busy = jiffies;
1297 udev->autosuspend_delay); 1309 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1310 if (inc_usage_cnt)
1311 udev->last_busy = jiffies;
1312 status = usb_suspend_both(udev, PMSG_SUSPEND);
1313 }
1298 } 1314 }
1299 usb_pm_unlock(udev); 1315 usb_pm_unlock(udev);
1300 return status; 1316 return status;
@@ -1353,11 +1369,14 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1353 * or @intf is unbound. A typical example would be a character-device 1369 * or @intf is unbound. A typical example would be a character-device
1354 * driver when its device file is opened. 1370 * driver when its device file is opened.
1355 * 1371 *
1356 * The routine increments @intf's usage counter. So long as the counter 1372 *
1357 * is greater than 0, autosuspend will not be allowed for @intf or its 1373 * The routine increments @intf's usage counter. (However if the
1358 * usb_device. When the driver is finished using @intf it should call 1374 * autoresume fails then the counter is re-decremented.) So long as the
1359 * usb_autopm_put_interface() to decrement the usage counter and queue 1375 * counter is greater than 0, autosuspend will not be allowed for @intf
1360 * a delayed autosuspend request (if the counter is <= 0). 1376 * or its usb_device. When the driver is finished using @intf it should
1377 * call usb_autopm_put_interface() to decrement the usage counter and
1378 * queue a delayed autosuspend request (if the counter is <= 0).
1379 *
1361 * 1380 *
1362 * Note that @intf->pm_usage_cnt is owned by the interface driver. The 1381 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1363 * core will not change its value other than the increment and decrement 1382 * core will not change its value other than the increment and decrement
@@ -1405,50 +1424,96 @@ int usb_autopm_set_interface(struct usb_interface *intf)
1405} 1424}
1406EXPORT_SYMBOL_GPL(usb_autopm_set_interface); 1425EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1407 1426
1427#else
1428
1429void usb_autosuspend_work(struct work_struct *work)
1430{}
1431
1408#endif /* CONFIG_USB_SUSPEND */ 1432#endif /* CONFIG_USB_SUSPEND */
1409 1433
1410static int usb_suspend(struct device *dev, pm_message_t message) 1434/**
1435 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1436 * @udev: the usb_device to suspend
1437 * @msg: Power Management message describing this state transition
1438 *
1439 * This routine handles external suspend requests: ones not generated
1440 * internally by a USB driver (autosuspend) but rather coming from the user
1441 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1442 * out regardless of @udev's usage counter or those of its interfaces,
1443 * and regardless of whether or not remote wakeup is enabled. Of course,
1444 * interface drivers still have the option of failing the suspend (if
1445 * there are unsuspended children, for example).
1446 *
1447 * The caller must hold @udev's device lock.
1448 */
1449int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1411{ 1450{
1412 int status; 1451 int status;
1413 1452
1414 if (is_usb_device(dev)) { 1453 usb_pm_lock(udev);
1415 struct usb_device *udev = to_usb_device(dev); 1454 udev->auto_pm = 0;
1416 1455 status = usb_suspend_both(udev, msg);
1417 usb_pm_lock(udev); 1456 usb_pm_unlock(udev);
1418 udev->auto_pm = 0;
1419 status = usb_suspend_both(udev, message);
1420 usb_pm_unlock(udev);
1421 } else
1422 status = 0;
1423 return status; 1457 return status;
1424} 1458}
1425 1459
1426static int usb_resume(struct device *dev) 1460/**
1461 * usb_external_resume_device - external resume of a USB device and its interfaces
1462 * @udev: the usb_device to resume
1463 *
1464 * This routine handles external resume requests: ones not generated
1465 * internally by a USB driver (autoresume) but rather coming from the user
1466 * (via sysfs), the PM core (system resume), or the device itself (remote
1467 * wakeup). @udev's usage counter is unaffected.
1468 *
1469 * The caller must hold @udev's device lock.
1470 */
1471int usb_external_resume_device(struct usb_device *udev)
1427{ 1472{
1428 int status; 1473 int status;
1429 1474
1430 if (is_usb_device(dev)) { 1475 usb_pm_lock(udev);
1431 struct usb_device *udev = to_usb_device(dev); 1476 udev->auto_pm = 0;
1432 1477 status = usb_resume_both(udev);
1433 usb_pm_lock(udev); 1478 usb_pm_unlock(udev);
1434 udev->auto_pm = 0;
1435 status = usb_resume_both(udev);
1436 usb_pm_unlock(udev);
1437 1479
1438 /* Rebind drivers that had no suspend method? */ 1480 /* Now that the device is awake, we can start trying to autosuspend
1439 } else 1481 * it again. */
1440 status = 0; 1482 if (status == 0)
1483 usb_try_autosuspend_device(udev);
1441 return status; 1484 return status;
1442} 1485}
1443 1486
1487static int usb_suspend(struct device *dev, pm_message_t message)
1488{
1489 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1490 return 0;
1491 return usb_external_suspend_device(to_usb_device(dev), message);
1492}
1493
1494static int usb_resume(struct device *dev)
1495{
1496 struct usb_device *udev;
1497
1498 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1499 return 0;
1500 udev = to_usb_device(dev);
1501 if (udev->autoresume_disabled)
1502 return -EPERM;
1503 return usb_external_resume_device(udev);
1504}
1505
1506#else
1507
1508#define usb_suspend NULL
1509#define usb_resume NULL
1510
1444#endif /* CONFIG_PM */ 1511#endif /* CONFIG_PM */
1445 1512
1446struct bus_type usb_bus_type = { 1513struct bus_type usb_bus_type = {
1447 .name = "usb", 1514 .name = "usb",
1448 .match = usb_device_match, 1515 .match = usb_device_match,
1449 .uevent = usb_uevent, 1516 .uevent = usb_uevent,
1450#ifdef CONFIG_PM
1451 .suspend = usb_suspend, 1517 .suspend = usb_suspend,
1452 .resume = usb_resume, 1518 .resume = usb_resume,
1453#endif
1454}; 1519};
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index b26c19e8d19f..40cf882293e6 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -37,6 +37,7 @@
37#include <asm/irq.h> 37#include <asm/irq.h>
38#include <asm/byteorder.h> 38#include <asm/byteorder.h>
39#include <linux/platform_device.h> 39#include <linux/platform_device.h>
40#include <linux/workqueue.h>
40 41
41#include <linux/usb.h> 42#include <linux/usb.h>
42 43
@@ -544,6 +545,8 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
544 unsigned long flags; 545 unsigned long flags;
545 char buffer[4]; /* Any root hubs with > 31 ports? */ 546 char buffer[4]; /* Any root hubs with > 31 ports? */
546 547
548 if (unlikely(!hcd->rh_registered))
549 return;
547 if (!hcd->uses_new_polling && !hcd->status_urb) 550 if (!hcd->uses_new_polling && !hcd->status_urb)
548 return; 551 return;
549 552
@@ -1296,14 +1299,26 @@ int hcd_bus_resume (struct usb_bus *bus)
1296 return status; 1299 return status;
1297} 1300}
1298 1301
1302/* Workqueue routine for root-hub remote wakeup */
1303static void hcd_resume_work(struct work_struct *work)
1304{
1305 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
1306 struct usb_device *udev = hcd->self.root_hub;
1307
1308 usb_lock_device(udev);
1309 usb_mark_last_busy(udev);
1310 usb_external_resume_device(udev);
1311 usb_unlock_device(udev);
1312}
1313
1299/** 1314/**
1300 * usb_hcd_resume_root_hub - called by HCD to resume its root hub 1315 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1301 * @hcd: host controller for this root hub 1316 * @hcd: host controller for this root hub
1302 * 1317 *
1303 * The USB host controller calls this function when its root hub is 1318 * The USB host controller calls this function when its root hub is
1304 * suspended (with the remote wakeup feature enabled) and a remote 1319 * suspended (with the remote wakeup feature enabled) and a remote
1305 * wakeup request is received. It queues a request for khubd to 1320 * wakeup request is received. The routine submits a workqueue request
1306 * resume the root hub (that is, manage its downstream ports again). 1321 * to resume the root hub (that is, manage its downstream ports again).
1307 */ 1322 */
1308void usb_hcd_resume_root_hub (struct usb_hcd *hcd) 1323void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1309{ 1324{
@@ -1311,7 +1326,7 @@ void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1311 1326
1312 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1327 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1313 if (hcd->rh_registered) 1328 if (hcd->rh_registered)
1314 usb_resume_root_hub (hcd->self.root_hub); 1329 queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
1315 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1330 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1316} 1331}
1317EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 1332EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
@@ -1500,6 +1515,9 @@ struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1500 init_timer(&hcd->rh_timer); 1515 init_timer(&hcd->rh_timer);
1501 hcd->rh_timer.function = rh_timer_func; 1516 hcd->rh_timer.function = rh_timer_func;
1502 hcd->rh_timer.data = (unsigned long) hcd; 1517 hcd->rh_timer.data = (unsigned long) hcd;
1518#ifdef CONFIG_PM
1519 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
1520#endif
1503 1521
1504 hcd->driver = driver; 1522 hcd->driver = driver;
1505 hcd->product_desc = (driver->product_desc) ? driver->product_desc : 1523 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
@@ -1666,16 +1684,20 @@ void usb_remove_hcd(struct usb_hcd *hcd)
1666 hcd->rh_registered = 0; 1684 hcd->rh_registered = 0;
1667 spin_unlock_irq (&hcd_root_hub_lock); 1685 spin_unlock_irq (&hcd_root_hub_lock);
1668 1686
1687#ifdef CONFIG_PM
1688 flush_workqueue(ksuspend_usb_wq);
1689#endif
1690
1669 mutex_lock(&usb_bus_list_lock); 1691 mutex_lock(&usb_bus_list_lock);
1670 usb_disconnect(&hcd->self.root_hub); 1692 usb_disconnect(&hcd->self.root_hub);
1671 mutex_unlock(&usb_bus_list_lock); 1693 mutex_unlock(&usb_bus_list_lock);
1672 1694
1673 hcd->poll_rh = 0;
1674 del_timer_sync(&hcd->rh_timer);
1675
1676 hcd->driver->stop(hcd); 1695 hcd->driver->stop(hcd);
1677 hcd->state = HC_STATE_HALT; 1696 hcd->state = HC_STATE_HALT;
1678 1697
1698 hcd->poll_rh = 0;
1699 del_timer_sync(&hcd->rh_timer);
1700
1679 if (hcd->irq >= 0) 1701 if (hcd->irq >= 0)
1680 free_irq(hcd->irq, hcd); 1702 free_irq(hcd->irq, hcd);
1681 usb_deregister_bus(&hcd->self); 1703 usb_deregister_bus(&hcd->self);
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 2a269ca20517..ef50fa494e47 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -68,6 +68,9 @@ struct usb_hcd {
68 68
69 struct timer_list rh_timer; /* drives root-hub polling */ 69 struct timer_list rh_timer; /* drives root-hub polling */
70 struct urb *status_urb; /* the current status urb */ 70 struct urb *status_urb; /* the current status urb */
71#ifdef CONFIG_PM
72 struct work_struct wakeup_work; /* for remote wakeup */
73#endif
71 74
72 /* 75 /*
73 * hardware info/state 76 * hardware info/state
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 7a6028599d62..bde29ab2b504 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1367,11 +1367,15 @@ int usb_new_device(struct usb_device *udev)
1367 } 1367 }
1368#endif 1368#endif
1369 1369
1370 /* export the usbdev device-node for libusb */
1371 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
1372 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
1373
1370 /* Register the device. The device driver is responsible 1374 /* Register the device. The device driver is responsible
1371 * for adding the device files to usbfs and sysfs and for 1375 * for adding the device files to sysfs and for configuring
1372 * configuring the device. 1376 * the device.
1373 */ 1377 */
1374 err = device_add (&udev->dev); 1378 err = device_add(&udev->dev);
1375 if (err) { 1379 if (err) {
1376 dev_err(&udev->dev, "can't device_add, error %d\n", err); 1380 dev_err(&udev->dev, "can't device_add, error %d\n", err);
1377 goto fail; 1381 goto fail;
@@ -1855,12 +1859,8 @@ static int remote_wakeup(struct usb_device *udev)
1855 usb_lock_device(udev); 1859 usb_lock_device(udev);
1856 if (udev->state == USB_STATE_SUSPENDED) { 1860 if (udev->state == USB_STATE_SUSPENDED) {
1857 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 1861 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
1858 status = usb_autoresume_device(udev); 1862 usb_mark_last_busy(udev);
1859 1863 status = usb_external_resume_device(udev);
1860 /* Give the interface drivers a chance to do something,
1861 * then autosuspend the device again. */
1862 if (status == 0)
1863 usb_autosuspend_device(udev);
1864 } 1864 }
1865 usb_unlock_device(udev); 1865 usb_unlock_device(udev);
1866 return status; 1866 return status;
@@ -1984,13 +1984,6 @@ static inline int remote_wakeup(struct usb_device *udev)
1984#define hub_resume NULL 1984#define hub_resume NULL
1985#endif 1985#endif
1986 1986
1987void usb_resume_root_hub(struct usb_device *hdev)
1988{
1989 struct usb_hub *hub = hdev_to_hub(hdev);
1990
1991 kick_khubd(hub);
1992}
1993
1994 1987
1995/* USB 2.0 spec, 7.1.7.3 / fig 7-29: 1988/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
1996 * 1989 *
diff --git a/drivers/usb/core/inode.c b/drivers/usb/core/inode.c
index 11dad22da41c..cddfc62c4611 100644
--- a/drivers/usb/core/inode.c
+++ b/drivers/usb/core/inode.c
@@ -662,7 +662,7 @@ static void usbfs_add_device(struct usb_device *dev)
662 sprintf (name, "%03d", dev->devnum); 662 sprintf (name, "%03d", dev->devnum);
663 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG, 663 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
664 dev->bus->usbfs_dentry, dev, 664 dev->bus->usbfs_dentry, dev,
665 &usbfs_device_file_operations, 665 &usbdev_file_operations,
666 devuid, devgid); 666 devuid, devgid);
667 if (dev->usbfs_dentry == NULL) { 667 if (dev->usbfs_dentry == NULL) {
668 err ("error creating usbfs device entry"); 668 err ("error creating usbfs device entry");
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index c359ccb32998..b7434787db5f 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -412,10 +412,24 @@ int usb_sg_init (
412 io->urbs [i]->status = -EINPROGRESS; 412 io->urbs [i]->status = -EINPROGRESS;
413 io->urbs [i]->actual_length = 0; 413 io->urbs [i]->actual_length = 0;
414 414
415 /*
416 * Some systems need to revert to PIO when DMA is temporarily
417 * unavailable. For their sakes, both transfer_buffer and
418 * transfer_dma are set when possible. However this can only
419 * work on systems without HIGHMEM, since DMA buffers located
420 * in high memory are not directly addressable by the CPU for
421 * PIO ... so when HIGHMEM is in use, transfer_buffer is NULL
422 * to prevent stale pointers and to help spot bugs.
423 */
415 if (dma) { 424 if (dma) {
416 /* hc may use _only_ transfer_dma */
417 io->urbs [i]->transfer_dma = sg_dma_address (sg + i); 425 io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
418 len = sg_dma_len (sg + i); 426 len = sg_dma_len (sg + i);
427#ifdef CONFIG_HIGHMEM
428 io->urbs[i]->transfer_buffer = NULL;
429#else
430 io->urbs[i]->transfer_buffer =
431 page_address(sg[i].page) + sg[i].offset;
432#endif
419 } else { 433 } else {
420 /* hc may use _only_ transfer_buffer */ 434 /* hc may use _only_ transfer_buffer */
421 io->urbs [i]->transfer_buffer = 435 io->urbs [i]->transfer_buffer =
@@ -1305,7 +1319,7 @@ int usb_reset_configuration(struct usb_device *dev)
1305 return 0; 1319 return 0;
1306} 1320}
1307 1321
1308static void release_interface(struct device *dev) 1322void usb_release_interface(struct device *dev)
1309{ 1323{
1310 struct usb_interface *intf = to_usb_interface(dev); 1324 struct usb_interface *intf = to_usb_interface(dev);
1311 struct usb_interface_cache *intfc = 1325 struct usb_interface_cache *intfc =
@@ -1315,6 +1329,67 @@ static void release_interface(struct device *dev)
1315 kfree(intf); 1329 kfree(intf);
1316} 1330}
1317 1331
1332#ifdef CONFIG_HOTPLUG
1333static int usb_if_uevent(struct device *dev, char **envp, int num_envp,
1334 char *buffer, int buffer_size)
1335{
1336 struct usb_device *usb_dev;
1337 struct usb_interface *intf;
1338 struct usb_host_interface *alt;
1339 int i = 0;
1340 int length = 0;
1341
1342 if (!dev)
1343 return -ENODEV;
1344
1345 /* driver is often null here; dev_dbg() would oops */
1346 pr_debug ("usb %s: uevent\n", dev->bus_id);
1347
1348 intf = to_usb_interface(dev);
1349 usb_dev = interface_to_usbdev(intf);
1350 alt = intf->cur_altsetting;
1351
1352 if (add_uevent_var(envp, num_envp, &i,
1353 buffer, buffer_size, &length,
1354 "INTERFACE=%d/%d/%d",
1355 alt->desc.bInterfaceClass,
1356 alt->desc.bInterfaceSubClass,
1357 alt->desc.bInterfaceProtocol))
1358 return -ENOMEM;
1359
1360 if (add_uevent_var(envp, num_envp, &i,
1361 buffer, buffer_size, &length,
1362 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1363 le16_to_cpu(usb_dev->descriptor.idVendor),
1364 le16_to_cpu(usb_dev->descriptor.idProduct),
1365 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1366 usb_dev->descriptor.bDeviceClass,
1367 usb_dev->descriptor.bDeviceSubClass,
1368 usb_dev->descriptor.bDeviceProtocol,
1369 alt->desc.bInterfaceClass,
1370 alt->desc.bInterfaceSubClass,
1371 alt->desc.bInterfaceProtocol))
1372 return -ENOMEM;
1373
1374 envp[i] = NULL;
1375 return 0;
1376}
1377
1378#else
1379
1380static int usb_if_uevent(struct device *dev, char **envp,
1381 int num_envp, char *buffer, int buffer_size)
1382{
1383 return -ENODEV;
1384}
1385#endif /* CONFIG_HOTPLUG */
1386
1387struct device_type usb_if_device_type = {
1388 .name = "usb_interface",
1389 .release = usb_release_interface,
1390 .uevent = usb_if_uevent,
1391};
1392
1318/* 1393/*
1319 * usb_set_configuration - Makes a particular device setting be current 1394 * usb_set_configuration - Makes a particular device setting be current
1320 * @dev: the device whose configuration is being updated 1395 * @dev: the device whose configuration is being updated
@@ -1478,8 +1553,8 @@ free_interfaces:
1478 intf->dev.parent = &dev->dev; 1553 intf->dev.parent = &dev->dev;
1479 intf->dev.driver = NULL; 1554 intf->dev.driver = NULL;
1480 intf->dev.bus = &usb_bus_type; 1555 intf->dev.bus = &usb_bus_type;
1556 intf->dev.type = &usb_if_device_type;
1481 intf->dev.dma_mask = dev->dev.dma_mask; 1557 intf->dev.dma_mask = dev->dev.dma_mask;
1482 intf->dev.release = release_interface;
1483 device_initialize (&intf->dev); 1558 device_initialize (&intf->dev);
1484 mark_quiesced(intf); 1559 mark_quiesced(intf);
1485 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d", 1560 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
diff --git a/drivers/usb/core/quirks.c b/drivers/usb/core/quirks.c
index f08ec85a6d64..739f520908aa 100644
--- a/drivers/usb/core/quirks.c
+++ b/drivers/usb/core/quirks.c
@@ -42,7 +42,7 @@ static void usb_autosuspend_quirk(struct usb_device *udev)
42{ 42{
43#ifdef CONFIG_USB_SUSPEND 43#ifdef CONFIG_USB_SUSPEND
44 /* disable autosuspend, but allow the user to re-enable it via sysfs */ 44 /* disable autosuspend, but allow the user to re-enable it via sysfs */
45 udev->autosuspend_delay = 0; 45 udev->autosuspend_disabled = 1;
46#endif 46#endif
47} 47}
48 48
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 311d5df80386..e7c982377488 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -11,6 +11,7 @@
11 11
12 12
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/string.h>
14#include <linux/usb.h> 15#include <linux/usb.h>
15#include "usb.h" 16#include "usb.h"
16 17
@@ -117,6 +118,16 @@ show_speed(struct device *dev, struct device_attribute *attr, char *buf)
117static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL); 118static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
118 119
119static ssize_t 120static ssize_t
121show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
122{
123 struct usb_device *udev;
124
125 udev = to_usb_device(dev);
126 return sprintf(buf, "%d\n", udev->bus->busnum);
127}
128static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
129
130static ssize_t
120show_devnum(struct device *dev, struct device_attribute *attr, char *buf) 131show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
121{ 132{
122 struct usb_device *udev; 133 struct usb_device *udev;
@@ -165,7 +176,7 @@ show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
165{ 176{
166 struct usb_device *udev = to_usb_device(dev); 177 struct usb_device *udev = to_usb_device(dev);
167 178
168 return sprintf(buf, "%u\n", udev->autosuspend_delay / HZ); 179 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
169} 180}
170 181
171static ssize_t 182static ssize_t
@@ -173,39 +184,115 @@ set_autosuspend(struct device *dev, struct device_attribute *attr,
173 const char *buf, size_t count) 184 const char *buf, size_t count)
174{ 185{
175 struct usb_device *udev = to_usb_device(dev); 186 struct usb_device *udev = to_usb_device(dev);
176 unsigned value, old; 187 int value;
177 188
178 if (sscanf(buf, "%u", &value) != 1 || value >= INT_MAX/HZ) 189 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
190 value <= - INT_MAX/HZ)
179 return -EINVAL; 191 return -EINVAL;
180 value *= HZ; 192 value *= HZ;
181 193
182 old = udev->autosuspend_delay;
183 udev->autosuspend_delay = value; 194 udev->autosuspend_delay = value;
184 if (value > 0 && old == 0) 195 if (value >= 0)
185 usb_try_autosuspend_device(udev); 196 usb_try_autosuspend_device(udev);
186 197 else {
198 if (usb_autoresume_device(udev) == 0)
199 usb_autosuspend_device(udev);
200 }
187 return count; 201 return count;
188} 202}
189 203
190static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR, 204static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
191 show_autosuspend, set_autosuspend); 205 show_autosuspend, set_autosuspend);
192 206
207static const char on_string[] = "on";
208static const char auto_string[] = "auto";
209static const char suspend_string[] = "suspend";
210
211static ssize_t
212show_level(struct device *dev, struct device_attribute *attr, char *buf)
213{
214 struct usb_device *udev = to_usb_device(dev);
215 const char *p = auto_string;
216
217 if (udev->state == USB_STATE_SUSPENDED) {
218 if (udev->autoresume_disabled)
219 p = suspend_string;
220 } else {
221 if (udev->autosuspend_disabled)
222 p = on_string;
223 }
224 return sprintf(buf, "%s\n", p);
225}
226
227static ssize_t
228set_level(struct device *dev, struct device_attribute *attr,
229 const char *buf, size_t count)
230{
231 struct usb_device *udev = to_usb_device(dev);
232 int len = count;
233 char *cp;
234 int rc = 0;
235
236 cp = memchr(buf, '\n', count);
237 if (cp)
238 len = cp - buf;
239
240 usb_lock_device(udev);
241
242 /* Setting the flags without calling usb_pm_lock is a subject to
243 * races, but who cares...
244 */
245 if (len == sizeof on_string - 1 &&
246 strncmp(buf, on_string, len) == 0) {
247 udev->autosuspend_disabled = 1;
248 udev->autoresume_disabled = 0;
249 rc = usb_external_resume_device(udev);
250
251 } else if (len == sizeof auto_string - 1 &&
252 strncmp(buf, auto_string, len) == 0) {
253 udev->autosuspend_disabled = 0;
254 udev->autoresume_disabled = 0;
255 rc = usb_external_resume_device(udev);
256
257 } else if (len == sizeof suspend_string - 1 &&
258 strncmp(buf, suspend_string, len) == 0) {
259 udev->autosuspend_disabled = 0;
260 udev->autoresume_disabled = 1;
261 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
262
263 } else
264 rc = -EINVAL;
265
266 usb_unlock_device(udev);
267 return (rc < 0 ? rc : count);
268}
269
270static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
271
193static char power_group[] = "power"; 272static char power_group[] = "power";
194 273
195static int add_power_attributes(struct device *dev) 274static int add_power_attributes(struct device *dev)
196{ 275{
197 int rc = 0; 276 int rc = 0;
198 277
199 if (is_usb_device(dev)) 278 if (is_usb_device(dev)) {
200 rc = sysfs_add_file_to_group(&dev->kobj, 279 rc = sysfs_add_file_to_group(&dev->kobj,
201 &dev_attr_autosuspend.attr, 280 &dev_attr_autosuspend.attr,
202 power_group); 281 power_group);
282 if (rc == 0)
283 rc = sysfs_add_file_to_group(&dev->kobj,
284 &dev_attr_level.attr,
285 power_group);
286 }
203 return rc; 287 return rc;
204} 288}
205 289
206static void remove_power_attributes(struct device *dev) 290static void remove_power_attributes(struct device *dev)
207{ 291{
208 sysfs_remove_file_from_group(&dev->kobj, 292 sysfs_remove_file_from_group(&dev->kobj,
293 &dev_attr_level.attr,
294 power_group);
295 sysfs_remove_file_from_group(&dev->kobj,
209 &dev_attr_autosuspend.attr, 296 &dev_attr_autosuspend.attr,
210 power_group); 297 power_group);
211} 298}
@@ -270,6 +357,7 @@ static struct attribute *dev_attrs[] = {
270 &dev_attr_bNumConfigurations.attr, 357 &dev_attr_bNumConfigurations.attr,
271 &dev_attr_bMaxPacketSize0.attr, 358 &dev_attr_bMaxPacketSize0.attr,
272 &dev_attr_speed.attr, 359 &dev_attr_speed.attr,
360 &dev_attr_busnum.attr,
273 &dev_attr_devnum.attr, 361 &dev_attr_devnum.attr,
274 &dev_attr_version.attr, 362 &dev_attr_version.attr,
275 &dev_attr_maxchild.attr, 363 &dev_attr_maxchild.attr,
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 54b42ce311c1..dfd1b5c87ca3 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -49,12 +49,13 @@ const char *usbcore_name = "usbcore";
49 49
50static int nousb; /* Disable USB when built into kernel image */ 50static int nousb; /* Disable USB when built into kernel image */
51 51
52struct workqueue_struct *ksuspend_usb_wq; /* For autosuspend */ 52/* Workqueue for autosuspend and for remote wakeup of root hubs */
53struct workqueue_struct *ksuspend_usb_wq;
53 54
54#ifdef CONFIG_USB_SUSPEND 55#ifdef CONFIG_USB_SUSPEND
55static int usb_autosuspend_delay = 2; /* Default delay value, 56static int usb_autosuspend_delay = 2; /* Default delay value,
56 * in seconds */ 57 * in seconds */
57module_param_named(autosuspend, usb_autosuspend_delay, uint, 0644); 58module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
58MODULE_PARM_DESC(autosuspend, "default autosuspend delay"); 59MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
59 60
60#else 61#else
@@ -196,6 +197,11 @@ static void usb_release_dev(struct device *dev)
196 kfree(udev); 197 kfree(udev);
197} 198}
198 199
200struct device_type usb_device_type = {
201 .name = "usb_device",
202 .release = usb_release_dev,
203};
204
199#ifdef CONFIG_PM 205#ifdef CONFIG_PM
200 206
201static int ksuspend_usb_init(void) 207static int ksuspend_usb_init(void)
@@ -211,27 +217,6 @@ static void ksuspend_usb_cleanup(void)
211 destroy_workqueue(ksuspend_usb_wq); 217 destroy_workqueue(ksuspend_usb_wq);
212} 218}
213 219
214#ifdef CONFIG_USB_SUSPEND
215
216/* usb_autosuspend_work - callback routine to autosuspend a USB device */
217static void usb_autosuspend_work(struct work_struct *work)
218{
219 struct usb_device *udev =
220 container_of(work, struct usb_device, autosuspend.work);
221
222 usb_pm_lock(udev);
223 udev->auto_pm = 1;
224 usb_suspend_both(udev, PMSG_SUSPEND);
225 usb_pm_unlock(udev);
226}
227
228#else
229
230static void usb_autosuspend_work(struct work_struct *work)
231{}
232
233#endif /* CONFIG_USB_SUSPEND */
234
235#else 220#else
236 221
237#define ksuspend_usb_init() 0 222#define ksuspend_usb_init() 0
@@ -267,13 +252,10 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
267 252
268 device_initialize(&dev->dev); 253 device_initialize(&dev->dev);
269 dev->dev.bus = &usb_bus_type; 254 dev->dev.bus = &usb_bus_type;
255 dev->dev.type = &usb_device_type;
270 dev->dev.dma_mask = bus->controller->dma_mask; 256 dev->dev.dma_mask = bus->controller->dma_mask;
271 dev->dev.release = usb_release_dev;
272 dev->state = USB_STATE_ATTACHED; 257 dev->state = USB_STATE_ATTACHED;
273 258
274 /* This magic assignment distinguishes devices from interfaces */
275 dev->dev.platform_data = &usb_generic_driver;
276
277 INIT_LIST_HEAD(&dev->ep0.urb_list); 259 INIT_LIST_HEAD(&dev->ep0.urb_list);
278 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; 260 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
279 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; 261 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
@@ -902,9 +884,9 @@ static int __init usb_init(void)
902 retval = usb_register(&usbfs_driver); 884 retval = usb_register(&usbfs_driver);
903 if (retval) 885 if (retval)
904 goto driver_register_failed; 886 goto driver_register_failed;
905 retval = usbdev_init(); 887 retval = usb_devio_init();
906 if (retval) 888 if (retval)
907 goto usbdevice_init_failed; 889 goto usb_devio_init_failed;
908 retval = usbfs_init(); 890 retval = usbfs_init();
909 if (retval) 891 if (retval)
910 goto fs_init_failed; 892 goto fs_init_failed;
@@ -919,8 +901,8 @@ static int __init usb_init(void)
919hub_init_failed: 901hub_init_failed:
920 usbfs_cleanup(); 902 usbfs_cleanup();
921fs_init_failed: 903fs_init_failed:
922 usbdev_cleanup(); 904 usb_devio_cleanup();
923usbdevice_init_failed: 905usb_devio_init_failed:
924 usb_deregister(&usbfs_driver); 906 usb_deregister(&usbfs_driver);
925driver_register_failed: 907driver_register_failed:
926 usb_major_cleanup(); 908 usb_major_cleanup();
@@ -947,7 +929,7 @@ static void __exit usb_exit(void)
947 usb_major_cleanup(); 929 usb_major_cleanup();
948 usbfs_cleanup(); 930 usbfs_cleanup();
949 usb_deregister(&usbfs_driver); 931 usb_deregister(&usbfs_driver);
950 usbdev_cleanup(); 932 usb_devio_cleanup();
951 usb_hub_cleanup(); 933 usb_hub_cleanup();
952 usb_host_cleanup(); 934 usb_host_cleanup();
953 bus_unregister(&usb_bus_type); 935 bus_unregister(&usb_bus_type);
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 08b5a04e3755..bf2eb0dae2ec 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -21,7 +21,6 @@ extern char *usb_cache_string(struct usb_device *udev, int index);
21extern int usb_set_configuration(struct usb_device *dev, int configuration); 21extern int usb_set_configuration(struct usb_device *dev, int configuration);
22 22
23extern void usb_kick_khubd(struct usb_device *dev); 23extern void usb_kick_khubd(struct usb_device *dev);
24extern void usb_resume_root_hub(struct usb_device *dev);
25extern int usb_match_device(struct usb_device *dev, 24extern int usb_match_device(struct usb_device *dev,
26 const struct usb_device_id *id); 25 const struct usb_device_id *id);
27 26
@@ -34,10 +33,12 @@ extern void usb_host_cleanup(void);
34 33
35#ifdef CONFIG_PM 34#ifdef CONFIG_PM
36 35
37extern int usb_suspend_both(struct usb_device *udev, pm_message_t msg); 36extern void usb_autosuspend_work(struct work_struct *work);
38extern int usb_resume_both(struct usb_device *udev);
39extern int usb_port_suspend(struct usb_device *dev); 37extern int usb_port_suspend(struct usb_device *dev);
40extern int usb_port_resume(struct usb_device *dev); 38extern int usb_port_resume(struct usb_device *dev);
39extern int usb_external_suspend_device(struct usb_device *udev,
40 pm_message_t msg);
41extern int usb_external_resume_device(struct usb_device *udev);
41 42
42static inline void usb_pm_lock(struct usb_device *udev) 43static inline void usb_pm_lock(struct usb_device *udev)
43{ 44{
@@ -51,11 +52,6 @@ static inline void usb_pm_unlock(struct usb_device *udev)
51 52
52#else 53#else
53 54
54#define usb_suspend_both(udev, msg) 0
55static inline int usb_resume_both(struct usb_device *udev)
56{
57 return 0;
58}
59#define usb_port_suspend(dev) 0 55#define usb_port_suspend(dev) 0
60#define usb_port_resume(dev) 0 56#define usb_port_resume(dev) 0
61static inline void usb_pm_lock(struct usb_device *udev) {} 57static inline void usb_pm_lock(struct usb_device *udev) {}
@@ -82,15 +78,13 @@ static inline int usb_autoresume_device(struct usb_device *udev)
82 78
83extern struct workqueue_struct *ksuspend_usb_wq; 79extern struct workqueue_struct *ksuspend_usb_wq;
84extern struct bus_type usb_bus_type; 80extern struct bus_type usb_bus_type;
81extern struct device_type usb_device_type;
82extern struct device_type usb_if_device_type;
85extern struct usb_device_driver usb_generic_driver; 83extern struct usb_device_driver usb_generic_driver;
86 84
87/* Here's how we tell apart devices and interfaces. Luckily there's
88 * no such thing as a platform USB device, so we can steal the use
89 * of the platform_data field. */
90
91static inline int is_usb_device(const struct device *dev) 85static inline int is_usb_device(const struct device *dev)
92{ 86{
93 return dev->platform_data == &usb_generic_driver; 87 return dev->type == &usb_device_type;
94} 88}
95 89
96/* Do the same for device drivers and interface drivers. */ 90/* Do the same for device drivers and interface drivers. */
@@ -126,11 +120,11 @@ extern const char *usbcore_name;
126extern struct mutex usbfs_mutex; 120extern struct mutex usbfs_mutex;
127extern struct usb_driver usbfs_driver; 121extern struct usb_driver usbfs_driver;
128extern const struct file_operations usbfs_devices_fops; 122extern const struct file_operations usbfs_devices_fops;
129extern const struct file_operations usbfs_device_file_operations; 123extern const struct file_operations usbdev_file_operations;
130extern void usbfs_conn_disc_event(void); 124extern void usbfs_conn_disc_event(void);
131 125
132extern int usbdev_init(void); 126extern int usb_devio_init(void);
133extern void usbdev_cleanup(void); 127extern void usb_devio_cleanup(void);
134 128
135struct dev_state { 129struct dev_state {
136 struct list_head list; /* state list */ 130 struct list_head list; /* state list */
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 4097a86c4b5e..8065f2b53701 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -68,6 +68,27 @@ choice
68 Many controller drivers are platform-specific; these 68 Many controller drivers are platform-specific; these
69 often need board-specific hooks. 69 often need board-specific hooks.
70 70
71config USB_GADGET_FSL_USB2
72 boolean "Freescale Highspeed USB DR Peripheral Controller"
73 depends on MPC834x || PPC_MPC831x
74 select USB_GADGET_DUALSPEED
75 help
76 Some of Freescale PowerPC processors have a High Speed
77 Dual-Role(DR) USB controller, which supports device mode.
78
79 The number of programmable endpoints is different through
80 SOC revisions.
81
82 Say "y" to link the driver statically, or "m" to build a
83 dynamically linked module called "fsl_usb2_udc" and force
84 all gadget drivers to also be dynamically linked.
85
86config USB_FSL_USB2
87 tristate
88 depends on USB_GADGET_FSL_USB2
89 default USB_GADGET
90 select USB_GADGET_SELECTED
91
71config USB_GADGET_NET2280 92config USB_GADGET_NET2280
72 boolean "NetChip 228x" 93 boolean "NetChip 228x"
73 depends on PCI 94 depends on PCI
@@ -370,6 +391,7 @@ config USB_GADGETFS
370 391
371config USB_FILE_STORAGE 392config USB_FILE_STORAGE
372 tristate "File-backed Storage Gadget" 393 tristate "File-backed Storage Gadget"
394 depends on BLOCK
373 help 395 help
374 The File-backed Storage Gadget acts as a USB Mass Storage 396 The File-backed Storage Gadget acts as a USB Mass Storage
375 disk drive. As its storage repository it can use a regular 397 disk drive. As its storage repository it can use a regular
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index e71e086a1cfa..5db19396631c 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_USB_GOKU) += goku_udc.o
8obj-$(CONFIG_USB_OMAP) += omap_udc.o 8obj-$(CONFIG_USB_OMAP) += omap_udc.o
9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o 9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
10obj-$(CONFIG_USB_AT91) += at91_udc.o 10obj-$(CONFIG_USB_AT91) += at91_udc.o
11obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o
11 12
12# 13#
13# USB gadget drivers 14# USB gadget drivers
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 8f9f217e0a68..1dd8b57f4420 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -282,6 +282,9 @@ MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
282#define DEV_CONFIG_CDC 282#define DEV_CONFIG_CDC
283#endif 283#endif
284 284
285#ifdef CONFIG_USB_GADGET_FSL_USB2
286#define DEV_CONFIG_CDC
287#endif
285 288
286/* For CDC-incapable hardware, choose the simple cdc subset. 289/* For CDC-incapable hardware, choose the simple cdc subset.
287 * Anything that talks bulk (without notable bugs) can do this. 290 * Anything that talks bulk (without notable bugs) can do this.
@@ -1735,7 +1738,8 @@ enomem:
1735 defer_kevent (dev, WORK_RX_MEMORY); 1738 defer_kevent (dev, WORK_RX_MEMORY);
1736 if (retval) { 1739 if (retval) {
1737 DEBUG (dev, "rx submit --> %d\n", retval); 1740 DEBUG (dev, "rx submit --> %d\n", retval);
1738 dev_kfree_skb_any (skb); 1741 if (skb)
1742 dev_kfree_skb_any(skb);
1739 spin_lock(&dev->req_lock); 1743 spin_lock(&dev->req_lock);
1740 list_add (&req->list, &dev->rx_reqs); 1744 list_add (&req->list, &dev->rx_reqs);
1741 spin_unlock(&dev->req_lock); 1745 spin_unlock(&dev->req_lock);
diff --git a/drivers/usb/gadget/fsl_usb2_udc.c b/drivers/usb/gadget/fsl_usb2_udc.c
new file mode 100644
index 000000000000..157054ea3978
--- /dev/null
+++ b/drivers/usb/gadget/fsl_usb2_udc.c
@@ -0,0 +1,2500 @@
1/*
2 * Copyright (C) 2004-2007 Freescale Semicondutor, Inc. All rights reserved.
3 *
4 * Author: Li Yang <leoli@freescale.com>
5 * Jiang Bo <tanya.jiang@freescale.com>
6 *
7 * Description:
8 * Freescale high-speed USB SOC DR module device controller driver.
9 * This can be found on MPC8349E/MPC8313E cpus.
10 * The driver is previously named as mpc_udc. Based on bare board
11 * code from Dave Liu and Shlomi Gridish.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18
19#undef VERBOSE
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/ioport.h>
24#include <linux/types.h>
25#include <linux/errno.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/timer.h>
31#include <linux/list.h>
32#include <linux/interrupt.h>
33#include <linux/proc_fs.h>
34#include <linux/mm.h>
35#include <linux/moduleparam.h>
36#include <linux/device.h>
37#include <linux/usb/ch9.h>
38#include <linux/usb_gadget.h>
39#include <linux/usb/otg.h>
40#include <linux/dma-mapping.h>
41#include <linux/platform_device.h>
42#include <linux/fsl_devices.h>
43#include <linux/dmapool.h>
44
45#include <asm/byteorder.h>
46#include <asm/io.h>
47#include <asm/irq.h>
48#include <asm/system.h>
49#include <asm/unaligned.h>
50#include <asm/dma.h>
51#include <asm/cacheflush.h>
52
53#include "fsl_usb2_udc.h"
54
55#define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
56#define DRIVER_AUTHOR "Li Yang/Jiang Bo"
57#define DRIVER_VERSION "Apr 20, 2007"
58
59#define DMA_ADDR_INVALID (~(dma_addr_t)0)
60
61static const char driver_name[] = "fsl-usb2-udc";
62static const char driver_desc[] = DRIVER_DESC;
63
64volatile static struct usb_dr_device *dr_regs = NULL;
65volatile static struct usb_sys_interface *usb_sys_regs = NULL;
66
67/* it is initialized in probe() */
68static struct fsl_udc *udc_controller = NULL;
69
70static const struct usb_endpoint_descriptor
71fsl_ep0_desc = {
72 .bLength = USB_DT_ENDPOINT_SIZE,
73 .bDescriptorType = USB_DT_ENDPOINT,
74 .bEndpointAddress = 0,
75 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
76 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
77};
78
79static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state);
80static int fsl_udc_resume(struct platform_device *pdev);
81static void fsl_ep_fifo_flush(struct usb_ep *_ep);
82
83#ifdef CONFIG_PPC32
84#define fsl_readl(addr) in_le32(addr)
85#define fsl_writel(addr, val32) out_le32(val32, addr)
86#else
87#define fsl_readl(addr) readl(addr)
88#define fsl_writel(addr, val32) writel(addr, val32)
89#endif
90
91/********************************************************************
92 * Internal Used Function
93********************************************************************/
94/*-----------------------------------------------------------------
95 * done() - retire a request; caller blocked irqs
96 * @status : request status to be set, only works when
97 * request is still in progress.
98 *--------------------------------------------------------------*/
99static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
100{
101 struct fsl_udc *udc = NULL;
102 unsigned char stopped = ep->stopped;
103 struct ep_td_struct *curr_td, *next_td;
104 int j;
105
106 udc = (struct fsl_udc *)ep->udc;
107 /* Removed the req from fsl_ep->queue */
108 list_del_init(&req->queue);
109
110 /* req.status should be set as -EINPROGRESS in ep_queue() */
111 if (req->req.status == -EINPROGRESS)
112 req->req.status = status;
113 else
114 status = req->req.status;
115
116 /* Free dtd for the request */
117 next_td = req->head;
118 for (j = 0; j < req->dtd_count; j++) {
119 curr_td = next_td;
120 if (j != req->dtd_count - 1) {
121 next_td = curr_td->next_td_virt;
122 }
123 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
124 }
125
126 if (req->mapped) {
127 dma_unmap_single(ep->udc->gadget.dev.parent,
128 req->req.dma, req->req.length,
129 ep_is_in(ep)
130 ? DMA_TO_DEVICE
131 : DMA_FROM_DEVICE);
132 req->req.dma = DMA_ADDR_INVALID;
133 req->mapped = 0;
134 } else
135 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
136 req->req.dma, req->req.length,
137 ep_is_in(ep)
138 ? DMA_TO_DEVICE
139 : DMA_FROM_DEVICE);
140
141 if (status && (status != -ESHUTDOWN))
142 VDBG("complete %s req %p stat %d len %u/%u",
143 ep->ep.name, &req->req, status,
144 req->req.actual, req->req.length);
145
146 ep->stopped = 1;
147
148 spin_unlock(&ep->udc->lock);
149 /* complete() is from gadget layer,
150 * eg fsg->bulk_in_complete() */
151 if (req->req.complete)
152 req->req.complete(&ep->ep, &req->req);
153
154 spin_lock(&ep->udc->lock);
155 ep->stopped = stopped;
156}
157
158/*-----------------------------------------------------------------
159 * nuke(): delete all requests related to this ep
160 * called with spinlock held
161 *--------------------------------------------------------------*/
162static void nuke(struct fsl_ep *ep, int status)
163{
164 ep->stopped = 1;
165
166 /* Flush fifo */
167 fsl_ep_fifo_flush(&ep->ep);
168
169 /* Whether this eq has request linked */
170 while (!list_empty(&ep->queue)) {
171 struct fsl_req *req = NULL;
172
173 req = list_entry(ep->queue.next, struct fsl_req, queue);
174 done(ep, req, status);
175 }
176}
177
178/*------------------------------------------------------------------
179 Internal Hardware related function
180 ------------------------------------------------------------------*/
181
182static int dr_controller_setup(struct fsl_udc *udc)
183{
184 unsigned int tmp = 0, portctrl = 0, ctrl = 0;
185 unsigned long timeout;
186#define FSL_UDC_RESET_TIMEOUT 1000
187
188 /* before here, make sure dr_regs has been initialized */
189 if (!udc)
190 return -EINVAL;
191
192 /* Stop and reset the usb controller */
193 tmp = fsl_readl(&dr_regs->usbcmd);
194 tmp &= ~USB_CMD_RUN_STOP;
195 fsl_writel(tmp, &dr_regs->usbcmd);
196
197 tmp = fsl_readl(&dr_regs->usbcmd);
198 tmp |= USB_CMD_CTRL_RESET;
199 fsl_writel(tmp, &dr_regs->usbcmd);
200
201 /* Wait for reset to complete */
202 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
203 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
204 if (time_after(jiffies, timeout)) {
205 ERR("udc reset timeout! \n");
206 return -ETIMEDOUT;
207 }
208 cpu_relax();
209 }
210
211 /* Set the controller as device mode */
212 tmp = fsl_readl(&dr_regs->usbmode);
213 tmp |= USB_MODE_CTRL_MODE_DEVICE;
214 /* Disable Setup Lockout */
215 tmp |= USB_MODE_SETUP_LOCK_OFF;
216 fsl_writel(tmp, &dr_regs->usbmode);
217
218 /* Clear the setup status */
219 fsl_writel(0, &dr_regs->usbsts);
220
221 tmp = udc->ep_qh_dma;
222 tmp &= USB_EP_LIST_ADDRESS_MASK;
223 fsl_writel(tmp, &dr_regs->endpointlistaddr);
224
225 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
226 (int)udc->ep_qh, (int)tmp,
227 fsl_readl(&dr_regs->endpointlistaddr));
228
229 /* Config PHY interface */
230 portctrl = fsl_readl(&dr_regs->portsc1);
231 portctrl &= ~PORTSCX_PHY_TYPE_SEL;
232 switch (udc->phy_mode) {
233 case FSL_USB2_PHY_ULPI:
234 portctrl |= PORTSCX_PTS_ULPI;
235 break;
236 case FSL_USB2_PHY_UTMI:
237 case FSL_USB2_PHY_UTMI_WIDE:
238 portctrl |= PORTSCX_PTS_UTMI;
239 break;
240 case FSL_USB2_PHY_SERIAL:
241 portctrl |= PORTSCX_PTS_FSLS;
242 break;
243 default:
244 return -EINVAL;
245 }
246 fsl_writel(portctrl, &dr_regs->portsc1);
247
248 /* Config control enable i/o output, cpu endian register */
249 ctrl = __raw_readl(&usb_sys_regs->control);
250 ctrl |= USB_CTRL_IOENB;
251 __raw_writel(ctrl, &usb_sys_regs->control);
252
253#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
254 /* Turn on cache snooping hardware, since some PowerPC platforms
255 * wholly rely on hardware to deal with cache coherent. */
256
257 /* Setup Snooping for all the 4GB space */
258 tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
259 __raw_writel(tmp, &usb_sys_regs->snoop1);
260 tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
261 __raw_writel(tmp, &usb_sys_regs->snoop2);
262#endif
263
264 return 0;
265}
266
267/* Enable DR irq and set controller to run state */
268static void dr_controller_run(struct fsl_udc *udc)
269{
270 u32 temp;
271
272 /* Enable DR irq reg */
273 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
274 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
275 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
276
277 fsl_writel(temp, &dr_regs->usbintr);
278
279 /* Clear stopped bit */
280 udc->stopped = 0;
281
282 /* Set the controller as device mode */
283 temp = fsl_readl(&dr_regs->usbmode);
284 temp |= USB_MODE_CTRL_MODE_DEVICE;
285 fsl_writel(temp, &dr_regs->usbmode);
286
287 /* Set controller to Run */
288 temp = fsl_readl(&dr_regs->usbcmd);
289 temp |= USB_CMD_RUN_STOP;
290 fsl_writel(temp, &dr_regs->usbcmd);
291
292 return;
293}
294
295static void dr_controller_stop(struct fsl_udc *udc)
296{
297 unsigned int tmp;
298
299 /* disable all INTR */
300 fsl_writel(0, &dr_regs->usbintr);
301
302 /* Set stopped bit for isr */
303 udc->stopped = 1;
304
305 /* disable IO output */
306/* usb_sys_regs->control = 0; */
307
308 /* set controller to Stop */
309 tmp = fsl_readl(&dr_regs->usbcmd);
310 tmp &= ~USB_CMD_RUN_STOP;
311 fsl_writel(tmp, &dr_regs->usbcmd);
312
313 return;
314}
315
316void dr_ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_type)
317{
318 unsigned int tmp_epctrl = 0;
319
320 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
321 if (dir) {
322 if (ep_num)
323 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
324 tmp_epctrl |= EPCTRL_TX_ENABLE;
325 tmp_epctrl |= ((unsigned int)(ep_type)
326 << EPCTRL_TX_EP_TYPE_SHIFT);
327 } else {
328 if (ep_num)
329 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
330 tmp_epctrl |= EPCTRL_RX_ENABLE;
331 tmp_epctrl |= ((unsigned int)(ep_type)
332 << EPCTRL_RX_EP_TYPE_SHIFT);
333 }
334
335 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
336}
337
338static void
339dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
340{
341 u32 tmp_epctrl = 0;
342
343 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
344
345 if (value) {
346 /* set the stall bit */
347 if (dir)
348 tmp_epctrl |= EPCTRL_TX_EP_STALL;
349 else
350 tmp_epctrl |= EPCTRL_RX_EP_STALL;
351 } else {
352 /* clear the stall bit and reset data toggle */
353 if (dir) {
354 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
355 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
356 } else {
357 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
358 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
359 }
360 }
361 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
362}
363
364/* Get stall status of a specific ep
365 Return: 0: not stalled; 1:stalled */
366static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
367{
368 u32 epctrl;
369
370 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
371 if (dir)
372 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
373 else
374 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
375}
376
377/********************************************************************
378 Internal Structure Build up functions
379********************************************************************/
380
381/*------------------------------------------------------------------
382* struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
383 * @zlt: Zero Length Termination Select (1: disable; 0: enable)
384 * @mult: Mult field
385 ------------------------------------------------------------------*/
386static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
387 unsigned char dir, unsigned char ep_type,
388 unsigned int max_pkt_len,
389 unsigned int zlt, unsigned char mult)
390{
391 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
392 unsigned int tmp = 0;
393
394 /* set the Endpoint Capabilites in QH */
395 switch (ep_type) {
396 case USB_ENDPOINT_XFER_CONTROL:
397 /* Interrupt On Setup (IOS). for control ep */
398 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
399 | EP_QUEUE_HEAD_IOS;
400 break;
401 case USB_ENDPOINT_XFER_ISOC:
402 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
403 | (mult << EP_QUEUE_HEAD_MULT_POS);
404 break;
405 case USB_ENDPOINT_XFER_BULK:
406 case USB_ENDPOINT_XFER_INT:
407 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
408 break;
409 default:
410 VDBG("error ep type is %d", ep_type);
411 return;
412 }
413 if (zlt)
414 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
415 p_QH->max_pkt_length = cpu_to_le32(tmp);
416
417 return;
418}
419
420/* Setup qh structure and ep register for ep0. */
421static void ep0_setup(struct fsl_udc *udc)
422{
423 /* the intialization of an ep includes: fields in QH, Regs,
424 * fsl_ep struct */
425 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
426 USB_MAX_CTRL_PAYLOAD, 0, 0);
427 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
428 USB_MAX_CTRL_PAYLOAD, 0, 0);
429 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
430 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
431
432 return;
433
434}
435
436/***********************************************************************
437 Endpoint Management Functions
438***********************************************************************/
439
440/*-------------------------------------------------------------------------
441 * when configurations are set, or when interface settings change
442 * for example the do_set_interface() in gadget layer,
443 * the driver will enable or disable the relevant endpoints
444 * ep0 doesn't use this routine. It is always enabled.
445-------------------------------------------------------------------------*/
446static int fsl_ep_enable(struct usb_ep *_ep,
447 const struct usb_endpoint_descriptor *desc)
448{
449 struct fsl_udc *udc = NULL;
450 struct fsl_ep *ep = NULL;
451 unsigned short max = 0;
452 unsigned char mult = 0, zlt;
453 int retval = -EINVAL;
454 unsigned long flags = 0;
455
456 ep = container_of(_ep, struct fsl_ep, ep);
457
458 /* catch various bogus parameters */
459 if (!_ep || !desc || ep->desc
460 || (desc->bDescriptorType != USB_DT_ENDPOINT))
461 return -EINVAL;
462
463 udc = ep->udc;
464
465 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
466 return -ESHUTDOWN;
467
468 max = le16_to_cpu(desc->wMaxPacketSize);
469
470 /* Disable automatic zlp generation. Driver is reponsible to indicate
471 * explicitly through req->req.zero. This is needed to enable multi-td
472 * request. */
473 zlt = 1;
474
475 /* Assume the max packet size from gadget is always correct */
476 switch (desc->bmAttributes & 0x03) {
477 case USB_ENDPOINT_XFER_CONTROL:
478 case USB_ENDPOINT_XFER_BULK:
479 case USB_ENDPOINT_XFER_INT:
480 /* mult = 0. Execute N Transactions as demonstrated by
481 * the USB variable length packet protocol where N is
482 * computed using the Maximum Packet Length (dQH) and
483 * the Total Bytes field (dTD) */
484 mult = 0;
485 break;
486 case USB_ENDPOINT_XFER_ISOC:
487 /* Calculate transactions needed for high bandwidth iso */
488 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
489 max = max & 0x8ff; /* bit 0~10 */
490 /* 3 transactions at most */
491 if (mult > 3)
492 goto en_done;
493 break;
494 default:
495 goto en_done;
496 }
497
498 spin_lock_irqsave(&udc->lock, flags);
499 ep->ep.maxpacket = max;
500 ep->desc = desc;
501 ep->stopped = 0;
502
503 /* Controller related setup */
504 /* Init EPx Queue Head (Ep Capabilites field in QH
505 * according to max, zlt, mult) */
506 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
507 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
508 ? USB_SEND : USB_RECV),
509 (unsigned char) (desc->bmAttributes
510 & USB_ENDPOINT_XFERTYPE_MASK),
511 max, zlt, mult);
512
513 /* Init endpoint ctrl register */
514 dr_ep_setup((unsigned char) ep_index(ep),
515 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
516 ? USB_SEND : USB_RECV),
517 (unsigned char) (desc->bmAttributes
518 & USB_ENDPOINT_XFERTYPE_MASK));
519
520 spin_unlock_irqrestore(&udc->lock, flags);
521 retval = 0;
522
523 VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
524 ep->desc->bEndpointAddress & 0x0f,
525 (desc->bEndpointAddress & USB_DIR_IN)
526 ? "in" : "out", max);
527en_done:
528 return retval;
529}
530
531/*---------------------------------------------------------------------
532 * @ep : the ep being unconfigured. May not be ep0
533 * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
534*---------------------------------------------------------------------*/
535static int fsl_ep_disable(struct usb_ep *_ep)
536{
537 struct fsl_udc *udc = NULL;
538 struct fsl_ep *ep = NULL;
539 unsigned long flags = 0;
540 u32 epctrl;
541 int ep_num;
542
543 ep = container_of(_ep, struct fsl_ep, ep);
544 if (!_ep || !ep->desc) {
545 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
546 return -EINVAL;
547 }
548
549 /* disable ep on controller */
550 ep_num = ep_index(ep);
551 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
552 if (ep_is_in(ep))
553 epctrl &= ~EPCTRL_TX_ENABLE;
554 else
555 epctrl &= ~EPCTRL_RX_ENABLE;
556 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
557
558 udc = (struct fsl_udc *)ep->udc;
559 spin_lock_irqsave(&udc->lock, flags);
560
561 /* nuke all pending requests (does flush) */
562 nuke(ep, -ESHUTDOWN);
563
564 ep->desc = 0;
565 ep->stopped = 1;
566 spin_unlock_irqrestore(&udc->lock, flags);
567
568 VDBG("disabled %s OK", _ep->name);
569 return 0;
570}
571
572/*---------------------------------------------------------------------
573 * allocate a request object used by this endpoint
574 * the main operation is to insert the req->queue to the eq->queue
575 * Returns the request, or null if one could not be allocated
576*---------------------------------------------------------------------*/
577static struct usb_request *
578fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
579{
580 struct fsl_req *req = NULL;
581
582 req = kzalloc(sizeof *req, gfp_flags);
583 if (!req)
584 return NULL;
585
586 req->req.dma = DMA_ADDR_INVALID;
587 INIT_LIST_HEAD(&req->queue);
588
589 return &req->req;
590}
591
592static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
593{
594 struct fsl_req *req = NULL;
595
596 req = container_of(_req, struct fsl_req, req);
597
598 if (_req)
599 kfree(req);
600}
601
602/*------------------------------------------------------------------
603 * Allocate an I/O buffer
604*---------------------------------------------------------------------*/
605static void *fsl_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
606 dma_addr_t *dma, gfp_t gfp_flags)
607{
608 struct fsl_ep *ep;
609
610 if (!_ep)
611 return NULL;
612
613 ep = container_of(_ep, struct fsl_ep, ep);
614
615 return dma_alloc_coherent(ep->udc->gadget.dev.parent,
616 bytes, dma, gfp_flags);
617}
618
619/*------------------------------------------------------------------
620 * frees an i/o buffer
621*---------------------------------------------------------------------*/
622static void fsl_free_buffer(struct usb_ep *_ep, void *buf,
623 dma_addr_t dma, unsigned bytes)
624{
625 struct fsl_ep *ep;
626
627 if (!_ep)
628 return NULL;
629
630 ep = container_of(_ep, struct fsl_ep, ep);
631
632 dma_free_coherent(ep->udc->gadget.dev.parent, bytes, buf, dma);
633}
634
635/*-------------------------------------------------------------------------*/
636static int fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
637{
638 int i = ep_index(ep) * 2 + ep_is_in(ep);
639 u32 temp, bitmask, tmp_stat;
640 struct ep_queue_head *dQH = &ep->udc->ep_qh[i];
641
642 /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
643 VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
644
645 bitmask = ep_is_in(ep)
646 ? (1 << (ep_index(ep) + 16))
647 : (1 << (ep_index(ep)));
648
649 /* check if the pipe is empty */
650 if (!(list_empty(&ep->queue))) {
651 /* Add td to the end */
652 struct fsl_req *lastreq;
653 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
654 lastreq->tail->next_td_ptr =
655 cpu_to_le32(req->head->td_dma & DTD_ADDR_MASK);
656 /* Read prime bit, if 1 goto done */
657 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
658 goto out;
659
660 do {
661 /* Set ATDTW bit in USBCMD */
662 temp = fsl_readl(&dr_regs->usbcmd);
663 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
664
665 /* Read correct status bit */
666 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
667
668 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
669
670 /* Write ATDTW bit to 0 */
671 temp = fsl_readl(&dr_regs->usbcmd);
672 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
673
674 if (tmp_stat)
675 goto out;
676 }
677
678 /* Write dQH next pointer and terminate bit to 0 */
679 temp = req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
680 dQH->next_dtd_ptr = cpu_to_le32(temp);
681
682 /* Clear active and halt bit */
683 temp = cpu_to_le32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
684 | EP_QUEUE_HEAD_STATUS_HALT));
685 dQH->size_ioc_int_sts &= temp;
686
687 /* Prime endpoint by writing 1 to ENDPTPRIME */
688 temp = ep_is_in(ep)
689 ? (1 << (ep_index(ep) + 16))
690 : (1 << (ep_index(ep)));
691 fsl_writel(temp, &dr_regs->endpointprime);
692out:
693 return 0;
694}
695
696/* Fill in the dTD structure
697 * @req: request that the transfer belongs to
698 * @length: return actually data length of the dTD
699 * @dma: return dma address of the dTD
700 * @is_last: return flag if it is the last dTD of the request
701 * return: pointer to the built dTD */
702static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
703 dma_addr_t *dma, int *is_last)
704{
705 u32 swap_temp;
706 struct ep_td_struct *dtd;
707
708 /* how big will this transfer be? */
709 *length = min(req->req.length - req->req.actual,
710 (unsigned)EP_MAX_LENGTH_TRANSFER);
711
712 dtd = dma_pool_alloc(udc_controller->td_pool, GFP_KERNEL, dma);
713 if (dtd == NULL)
714 return dtd;
715
716 dtd->td_dma = *dma;
717 /* Clear reserved field */
718 swap_temp = cpu_to_le32(dtd->size_ioc_sts);
719 swap_temp &= ~DTD_RESERVED_FIELDS;
720 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
721
722 /* Init all of buffer page pointers */
723 swap_temp = (u32) (req->req.dma + req->req.actual);
724 dtd->buff_ptr0 = cpu_to_le32(swap_temp);
725 dtd->buff_ptr1 = cpu_to_le32(swap_temp + 0x1000);
726 dtd->buff_ptr2 = cpu_to_le32(swap_temp + 0x2000);
727 dtd->buff_ptr3 = cpu_to_le32(swap_temp + 0x3000);
728 dtd->buff_ptr4 = cpu_to_le32(swap_temp + 0x4000);
729
730 req->req.actual += *length;
731
732 /* zlp is needed if req->req.zero is set */
733 if (req->req.zero) {
734 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
735 *is_last = 1;
736 else
737 *is_last = 0;
738 } else if (req->req.length == req->req.actual)
739 *is_last = 1;
740 else
741 *is_last = 0;
742
743 if ((*is_last) == 0)
744 VDBG("multi-dtd request!\n");
745 /* Fill in the transfer size; set active bit */
746 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
747
748 /* Enable interrupt for the last dtd of a request */
749 if (*is_last && !req->req.no_interrupt)
750 swap_temp |= DTD_IOC;
751
752 dtd->size_ioc_sts = cpu_to_le32(swap_temp);
753
754 mb();
755
756 VDBG("length = %d address= 0x%x", *length, (int)*dma);
757
758 return dtd;
759}
760
761/* Generate dtd chain for a request */
762static int fsl_req_to_dtd(struct fsl_req *req)
763{
764 unsigned count;
765 int is_last;
766 int is_first =1;
767 struct ep_td_struct *last_dtd = NULL, *dtd;
768 dma_addr_t dma;
769
770 do {
771 dtd = fsl_build_dtd(req, &count, &dma, &is_last);
772 if (dtd == NULL)
773 return -ENOMEM;
774
775 if (is_first) {
776 is_first = 0;
777 req->head = dtd;
778 } else {
779 last_dtd->next_td_ptr = cpu_to_le32(dma);
780 last_dtd->next_td_virt = dtd;
781 }
782 last_dtd = dtd;
783
784 req->dtd_count++;
785 } while (!is_last);
786
787 dtd->next_td_ptr = cpu_to_le32(DTD_NEXT_TERMINATE);
788
789 req->tail = dtd;
790
791 return 0;
792}
793
794/* queues (submits) an I/O request to an endpoint */
795static int
796fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
797{
798 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
799 struct fsl_req *req = container_of(_req, struct fsl_req, req);
800 struct fsl_udc *udc;
801 unsigned long flags;
802 int is_iso = 0;
803
804 /* catch various bogus parameters */
805 if (!_req || !req->req.complete || !req->req.buf
806 || !list_empty(&req->queue)) {
807 VDBG("%s, bad params\n", __FUNCTION__);
808 return -EINVAL;
809 }
810 if (!_ep || (!ep->desc && ep_index(ep))) {
811 VDBG("%s, bad ep\n", __FUNCTION__);
812 return -EINVAL;
813 }
814 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
815 if (req->req.length > ep->ep.maxpacket)
816 return -EMSGSIZE;
817 is_iso = 1;
818 }
819
820 udc = ep->udc;
821 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
822 return -ESHUTDOWN;
823
824 req->ep = ep;
825
826 /* map virtual address to hardware */
827 if (req->req.dma == DMA_ADDR_INVALID) {
828 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
829 req->req.buf,
830 req->req.length, ep_is_in(ep)
831 ? DMA_TO_DEVICE
832 : DMA_FROM_DEVICE);
833 req->mapped = 1;
834 } else {
835 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
836 req->req.dma, req->req.length,
837 ep_is_in(ep)
838 ? DMA_TO_DEVICE
839 : DMA_FROM_DEVICE);
840 req->mapped = 0;
841 }
842
843 req->req.status = -EINPROGRESS;
844 req->req.actual = 0;
845 req->dtd_count = 0;
846
847 spin_lock_irqsave(&udc->lock, flags);
848
849 /* build dtds and push them to device queue */
850 if (!fsl_req_to_dtd(req)) {
851 fsl_queue_td(ep, req);
852 } else {
853 spin_unlock_irqrestore(&udc->lock, flags);
854 return -ENOMEM;
855 }
856
857 /* Update ep0 state */
858 if ((ep_index(ep) == 0))
859 udc->ep0_state = DATA_STATE_XMIT;
860
861 /* irq handler advances the queue */
862 if (req != NULL)
863 list_add_tail(&req->queue, &ep->queue);
864 spin_unlock_irqrestore(&udc->lock, flags);
865
866 return 0;
867}
868
869/* dequeues (cancels, unlinks) an I/O request from an endpoint */
870static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
871{
872 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
873 struct fsl_req *req;
874 unsigned long flags;
875 int ep_num, stopped, ret = 0;
876 u32 epctrl;
877
878 if (!_ep || !_req)
879 return -EINVAL;
880
881 spin_lock_irqsave(&ep->udc->lock, flags);
882 stopped = ep->stopped;
883
884 /* Stop the ep before we deal with the queue */
885 ep->stopped = 1;
886 ep_num = ep_index(ep);
887 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
888 if (ep_is_in(ep))
889 epctrl &= ~EPCTRL_TX_ENABLE;
890 else
891 epctrl &= ~EPCTRL_RX_ENABLE;
892 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
893
894 /* make sure it's actually queued on this endpoint */
895 list_for_each_entry(req, &ep->queue, queue) {
896 if (&req->req == _req)
897 break;
898 }
899 if (&req->req != _req) {
900 ret = -EINVAL;
901 goto out;
902 }
903
904 /* The request is in progress, or completed but not dequeued */
905 if (ep->queue.next == &req->queue) {
906 _req->status = -ECONNRESET;
907 fsl_ep_fifo_flush(_ep); /* flush current transfer */
908
909 /* The request isn't the last request in this ep queue */
910 if (req->queue.next != &ep->queue) {
911 struct ep_queue_head *qh;
912 struct fsl_req *next_req;
913
914 qh = ep->qh;
915 next_req = list_entry(req->queue.next, struct fsl_req,
916 queue);
917
918 /* Point the QH to the first TD of next request */
919 fsl_writel((u32) next_req->head, &qh->curr_dtd_ptr);
920 }
921
922 /* The request hasn't been processed, patch up the TD chain */
923 } else {
924 struct fsl_req *prev_req;
925
926 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
927 fsl_writel(fsl_readl(&req->tail->next_td_ptr),
928 &prev_req->tail->next_td_ptr);
929
930 }
931
932 done(ep, req, -ECONNRESET);
933
934 /* Enable EP */
935out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
936 if (ep_is_in(ep))
937 epctrl |= EPCTRL_TX_ENABLE;
938 else
939 epctrl |= EPCTRL_RX_ENABLE;
940 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
941 ep->stopped = stopped;
942
943 spin_unlock_irqrestore(&ep->udc->lock, flags);
944 return ret;
945}
946
947/*-------------------------------------------------------------------------*/
948
949/*-----------------------------------------------------------------
950 * modify the endpoint halt feature
951 * @ep: the non-isochronous endpoint being stalled
952 * @value: 1--set halt 0--clear halt
953 * Returns zero, or a negative error code.
954*----------------------------------------------------------------*/
955static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
956{
957 struct fsl_ep *ep = NULL;
958 unsigned long flags = 0;
959 int status = -EOPNOTSUPP; /* operation not supported */
960 unsigned char ep_dir = 0, ep_num = 0;
961 struct fsl_udc *udc = NULL;
962
963 ep = container_of(_ep, struct fsl_ep, ep);
964 udc = ep->udc;
965 if (!_ep || !ep->desc) {
966 status = -EINVAL;
967 goto out;
968 }
969
970 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
971 status = -EOPNOTSUPP;
972 goto out;
973 }
974
975 /* Attempt to halt IN ep will fail if any transfer requests
976 * are still queue */
977 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
978 status = -EAGAIN;
979 goto out;
980 }
981
982 status = 0;
983 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
984 ep_num = (unsigned char)(ep_index(ep));
985 spin_lock_irqsave(&ep->udc->lock, flags);
986 dr_ep_change_stall(ep_num, ep_dir, value);
987 spin_unlock_irqrestore(&ep->udc->lock, flags);
988
989 if (ep_index(ep) == 0) {
990 udc->ep0_state = WAIT_FOR_SETUP;
991 udc->ep0_dir = 0;
992 }
993out:
994 VDBG(" %s %s halt stat %d", ep->ep.name,
995 value ? "set" : "clear", status);
996
997 return status;
998}
999
1000static void fsl_ep_fifo_flush(struct usb_ep *_ep)
1001{
1002 struct fsl_ep *ep;
1003 int ep_num, ep_dir;
1004 u32 bits;
1005 unsigned long timeout;
1006#define FSL_UDC_FLUSH_TIMEOUT 1000
1007
1008 if (!_ep) {
1009 return;
1010 } else {
1011 ep = container_of(_ep, struct fsl_ep, ep);
1012 if (!ep->desc)
1013 return;
1014 }
1015 ep_num = ep_index(ep);
1016 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1017
1018 if (ep_num == 0)
1019 bits = (1 << 16) | 1;
1020 else if (ep_dir == USB_SEND)
1021 bits = 1 << (16 + ep_num);
1022 else
1023 bits = 1 << ep_num;
1024
1025 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
1026 do {
1027 fsl_writel(bits, &dr_regs->endptflush);
1028
1029 /* Wait until flush complete */
1030 while (fsl_readl(&dr_regs->endptflush)) {
1031 if (time_after(jiffies, timeout)) {
1032 ERR("ep flush timeout\n");
1033 return;
1034 }
1035 cpu_relax();
1036 }
1037 /* See if we need to flush again */
1038 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1039}
1040
1041static struct usb_ep_ops fsl_ep_ops = {
1042 .enable = fsl_ep_enable,
1043 .disable = fsl_ep_disable,
1044
1045 .alloc_request = fsl_alloc_request,
1046 .free_request = fsl_free_request,
1047
1048 .alloc_buffer = fsl_alloc_buffer,
1049 .free_buffer = fsl_free_buffer,
1050
1051 .queue = fsl_ep_queue,
1052 .dequeue = fsl_ep_dequeue,
1053
1054 .set_halt = fsl_ep_set_halt,
1055 .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
1056};
1057
1058/*-------------------------------------------------------------------------
1059 Gadget Driver Layer Operations
1060-------------------------------------------------------------------------*/
1061
1062/*----------------------------------------------------------------------
1063 * Get the current frame number (from DR frame_index Reg )
1064 *----------------------------------------------------------------------*/
1065static int fsl_get_frame(struct usb_gadget *gadget)
1066{
1067 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1068}
1069
1070/*-----------------------------------------------------------------------
1071 * Tries to wake up the host connected to this gadget
1072 -----------------------------------------------------------------------*/
1073static int fsl_wakeup(struct usb_gadget *gadget)
1074{
1075 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1076 u32 portsc;
1077
1078 /* Remote wakeup feature not enabled by host */
1079 if (!udc->remote_wakeup)
1080 return -ENOTSUPP;
1081
1082 portsc = fsl_readl(&dr_regs->portsc1);
1083 /* not suspended? */
1084 if (!(portsc & PORTSCX_PORT_SUSPEND))
1085 return 0;
1086 /* trigger force resume */
1087 portsc |= PORTSCX_PORT_FORCE_RESUME;
1088 fsl_writel(portsc, &dr_regs->portsc1);
1089 return 0;
1090}
1091
1092static int can_pullup(struct fsl_udc *udc)
1093{
1094 return udc->driver && udc->softconnect && udc->vbus_active;
1095}
1096
1097/* Notify controller that VBUS is powered, Called by whatever
1098 detects VBUS sessions */
1099static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1100{
1101 struct fsl_udc *udc;
1102 unsigned long flags;
1103
1104 udc = container_of(gadget, struct fsl_udc, gadget);
1105 spin_lock_irqsave(&udc->lock, flags);
1106 VDBG("VBUS %s\n", is_active ? "on" : "off");
1107 udc->vbus_active = (is_active != 0);
1108 if (can_pullup(udc))
1109 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1110 &dr_regs->usbcmd);
1111 else
1112 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1113 &dr_regs->usbcmd);
1114 spin_unlock_irqrestore(&udc->lock, flags);
1115 return 0;
1116}
1117
1118/* constrain controller's VBUS power usage
1119 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1120 * reporting how much power the device may consume. For example, this
1121 * could affect how quickly batteries are recharged.
1122 *
1123 * Returns zero on success, else negative errno.
1124 */
1125static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1126{
1127#ifdef CONFIG_USB_OTG
1128 struct fsl_udc *udc;
1129
1130 udc = container_of(gadget, struct fsl_udc, gadget);
1131
1132 if (udc->transceiver)
1133 return otg_set_power(udc->transceiver, mA);
1134#endif
1135 return -ENOTSUPP;
1136}
1137
1138/* Change Data+ pullup status
1139 * this func is used by usb_gadget_connect/disconnet
1140 */
1141static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1142{
1143 struct fsl_udc *udc;
1144
1145 udc = container_of(gadget, struct fsl_udc, gadget);
1146 udc->softconnect = (is_on != 0);
1147 if (can_pullup(udc))
1148 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1149 &dr_regs->usbcmd);
1150 else
1151 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1152 &dr_regs->usbcmd);
1153
1154 return 0;
1155}
1156
1157/* defined in usb_gadget.h */
1158static struct usb_gadget_ops fsl_gadget_ops = {
1159 .get_frame = fsl_get_frame,
1160 .wakeup = fsl_wakeup,
1161/* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
1162 .vbus_session = fsl_vbus_session,
1163 .vbus_draw = fsl_vbus_draw,
1164 .pullup = fsl_pullup,
1165};
1166
1167/* Set protocol stall on ep0, protocol stall will automatically be cleared
1168 on new transaction */
1169static void ep0stall(struct fsl_udc *udc)
1170{
1171 u32 tmp;
1172
1173 /* must set tx and rx to stall at the same time */
1174 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1175 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1176 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1177 udc->ep0_state = WAIT_FOR_SETUP;
1178 udc->ep0_dir = 0;
1179}
1180
1181/* Prime a status phase for ep0 */
1182static int ep0_prime_status(struct fsl_udc *udc, int direction)
1183{
1184 struct fsl_req *req = udc->status_req;
1185 struct fsl_ep *ep;
1186 int status = 0;
1187
1188 if (direction == EP_DIR_IN)
1189 udc->ep0_dir = USB_DIR_IN;
1190 else
1191 udc->ep0_dir = USB_DIR_OUT;
1192
1193 ep = &udc->eps[0];
1194 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1195
1196 req->ep = ep;
1197 req->req.length = 0;
1198 req->req.status = -EINPROGRESS;
1199 req->req.actual = 0;
1200 req->req.complete = NULL;
1201 req->dtd_count = 0;
1202
1203 if (fsl_req_to_dtd(req) == 0)
1204 status = fsl_queue_td(ep, req);
1205 else
1206 return -ENOMEM;
1207
1208 if (status)
1209 ERR("Can't queue ep0 status request \n");
1210 list_add_tail(&req->queue, &ep->queue);
1211
1212 return status;
1213}
1214
1215static inline int udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1216{
1217 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1218
1219 if (!ep->name)
1220 return 0;
1221
1222 nuke(ep, -ESHUTDOWN);
1223
1224 return 0;
1225}
1226
1227/*
1228 * ch9 Set address
1229 */
1230static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1231{
1232 /* Save the new address to device struct */
1233 udc->device_address = (u8) value;
1234 /* Update usb state */
1235 udc->usb_state = USB_STATE_ADDRESS;
1236 /* Status phase */
1237 if (ep0_prime_status(udc, EP_DIR_IN))
1238 ep0stall(udc);
1239}
1240
1241/*
1242 * ch9 Get status
1243 */
1244static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1245 u16 index, u16 length)
1246{
1247 u16 tmp = 0; /* Status, cpu endian */
1248
1249 struct fsl_req *req;
1250 struct fsl_ep *ep;
1251 int status = 0;
1252
1253 ep = &udc->eps[0];
1254
1255 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1256 /* Get device status */
1257 tmp = 1 << USB_DEVICE_SELF_POWERED;
1258 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1259 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1260 /* Get interface status */
1261 /* We don't have interface information in udc driver */
1262 tmp = 0;
1263 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1264 /* Get endpoint status */
1265 struct fsl_ep *target_ep;
1266
1267 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1268
1269 /* stall if endpoint doesn't exist */
1270 if (!target_ep->desc)
1271 goto stall;
1272 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1273 << USB_ENDPOINT_HALT;
1274 }
1275
1276 udc->ep0_dir = USB_DIR_IN;
1277 /* Borrow the per device status_req */
1278 req = udc->status_req;
1279 /* Fill in the reqest structure */
1280 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1281 req->ep = ep;
1282 req->req.length = 2;
1283 req->req.status = -EINPROGRESS;
1284 req->req.actual = 0;
1285 req->req.complete = NULL;
1286 req->dtd_count = 0;
1287
1288 /* prime the data phase */
1289 if ((fsl_req_to_dtd(req) == 0))
1290 status = fsl_queue_td(ep, req);
1291 else /* no mem */
1292 goto stall;
1293
1294 if (status) {
1295 ERR("Can't respond to getstatus request \n");
1296 goto stall;
1297 }
1298 list_add_tail(&req->queue, &ep->queue);
1299 udc->ep0_state = DATA_STATE_XMIT;
1300 return;
1301stall:
1302 ep0stall(udc);
1303}
1304
1305static void setup_received_irq(struct fsl_udc *udc,
1306 struct usb_ctrlrequest *setup)
1307{
1308 u16 wValue = le16_to_cpu(setup->wValue);
1309 u16 wIndex = le16_to_cpu(setup->wIndex);
1310 u16 wLength = le16_to_cpu(setup->wLength);
1311
1312 udc_reset_ep_queue(udc, 0);
1313
1314 switch (setup->bRequest) {
1315 /* Request that need Data+Status phase from udc */
1316 case USB_REQ_GET_STATUS:
1317 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_STANDARD))
1318 != (USB_DIR_IN | USB_TYPE_STANDARD))
1319 break;
1320 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1321 break;
1322
1323 /* Requests that need Status phase from udc */
1324 case USB_REQ_SET_ADDRESS:
1325 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1326 | USB_RECIP_DEVICE))
1327 break;
1328 ch9setaddress(udc, wValue, wIndex, wLength);
1329 break;
1330
1331 /* Handled by udc, no data, status by udc */
1332 case USB_REQ_CLEAR_FEATURE:
1333 case USB_REQ_SET_FEATURE:
1334 { /* status transaction */
1335 int rc = -EOPNOTSUPP;
1336
1337 if ((setup->bRequestType & USB_RECIP_MASK)
1338 == USB_RECIP_ENDPOINT) {
1339 int pipe = get_pipe_by_windex(wIndex);
1340 struct fsl_ep *ep;
1341
1342 if (wValue != 0 || wLength != 0 || pipe > udc->max_ep)
1343 break;
1344 ep = get_ep_by_pipe(udc, pipe);
1345
1346 spin_unlock(&udc->lock);
1347 rc = fsl_ep_set_halt(&ep->ep,
1348 (setup->bRequest == USB_REQ_SET_FEATURE)
1349 ? 1 : 0);
1350 spin_lock(&udc->lock);
1351
1352 } else if ((setup->bRequestType & USB_RECIP_MASK)
1353 == USB_RECIP_DEVICE) {
1354 /* Note: The driver has not include OTG support yet.
1355 * This will be set when OTG support is added */
1356 if (!udc->gadget.is_otg)
1357 break;
1358 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE)
1359 udc->gadget.b_hnp_enable = 1;
1360 else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
1361 udc->gadget.a_hnp_support = 1;
1362 else if (setup->bRequest ==
1363 USB_DEVICE_A_ALT_HNP_SUPPORT)
1364 udc->gadget.a_alt_hnp_support = 1;
1365 rc = 0;
1366 }
1367 if (rc == 0) {
1368 if (ep0_prime_status(udc, EP_DIR_IN))
1369 ep0stall(udc);
1370 }
1371 break;
1372 }
1373 /* Requests handled by gadget */
1374 default:
1375 if (wLength) {
1376 /* Data phase from gadget, status phase from udc */
1377 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1378 ? USB_DIR_IN : USB_DIR_OUT;
1379 spin_unlock(&udc->lock);
1380 if (udc->driver->setup(&udc->gadget,
1381 &udc->local_setup_buff) < 0)
1382 ep0stall(udc);
1383 spin_lock(&udc->lock);
1384 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1385 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1386
1387 } else {
1388 /* No data phase, IN status from gadget */
1389 udc->ep0_dir = USB_DIR_IN;
1390 spin_unlock(&udc->lock);
1391 if (udc->driver->setup(&udc->gadget,
1392 &udc->local_setup_buff) < 0)
1393 ep0stall(udc);
1394 spin_lock(&udc->lock);
1395 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1396 }
1397 break;
1398 }
1399}
1400
1401/* Process request for Data or Status phase of ep0
1402 * prime status phase if needed */
1403static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1404 struct fsl_req *req)
1405{
1406 if (udc->usb_state == USB_STATE_ADDRESS) {
1407 /* Set the new address */
1408 u32 new_address = (u32) udc->device_address;
1409 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1410 &dr_regs->deviceaddr);
1411 }
1412
1413 done(ep0, req, 0);
1414
1415 switch (udc->ep0_state) {
1416 case DATA_STATE_XMIT:
1417 /* receive status phase */
1418 if (ep0_prime_status(udc, EP_DIR_OUT))
1419 ep0stall(udc);
1420 break;
1421 case DATA_STATE_RECV:
1422 /* send status phase */
1423 if (ep0_prime_status(udc, EP_DIR_IN))
1424 ep0stall(udc);
1425 break;
1426 case WAIT_FOR_OUT_STATUS:
1427 udc->ep0_state = WAIT_FOR_SETUP;
1428 break;
1429 case WAIT_FOR_SETUP:
1430 ERR("Unexpect ep0 packets \n");
1431 break;
1432 default:
1433 ep0stall(udc);
1434 break;
1435 }
1436}
1437
1438/* Tripwire mechanism to ensure a setup packet payload is extracted without
1439 * being corrupted by another incoming setup packet */
1440static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1441{
1442 u32 temp;
1443 struct ep_queue_head *qh;
1444
1445 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1446
1447 /* Clear bit in ENDPTSETUPSTAT */
1448 temp = fsl_readl(&dr_regs->endptsetupstat);
1449 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1450
1451 /* while a hazard exists when setup package arrives */
1452 do {
1453 /* Set Setup Tripwire */
1454 temp = fsl_readl(&dr_regs->usbcmd);
1455 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1456
1457 /* Copy the setup packet to local buffer */
1458 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1459 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1460
1461 /* Clear Setup Tripwire */
1462 temp = fsl_readl(&dr_regs->usbcmd);
1463 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1464}
1465
1466/* process-ep_req(): free the completed Tds for this req */
1467static int process_ep_req(struct fsl_udc *udc, int pipe,
1468 struct fsl_req *curr_req)
1469{
1470 struct ep_td_struct *curr_td;
1471 int td_complete, actual, remaining_length, j, tmp;
1472 int status = 0;
1473 int errors = 0;
1474 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1475 int direction = pipe % 2;
1476
1477 curr_td = curr_req->head;
1478 td_complete = 0;
1479 actual = curr_req->req.length;
1480
1481 for (j = 0; j < curr_req->dtd_count; j++) {
1482 remaining_length = (le32_to_cpu(curr_td->size_ioc_sts)
1483 & DTD_PACKET_SIZE)
1484 >> DTD_LENGTH_BIT_POS;
1485 actual -= remaining_length;
1486
1487 if ((errors = le32_to_cpu(curr_td->size_ioc_sts) &
1488 DTD_ERROR_MASK)) {
1489 if (errors & DTD_STATUS_HALTED) {
1490 ERR("dTD error %08x QH=%d\n", errors, pipe);
1491 /* Clear the errors and Halt condition */
1492 tmp = le32_to_cpu(curr_qh->size_ioc_int_sts);
1493 tmp &= ~errors;
1494 curr_qh->size_ioc_int_sts = cpu_to_le32(tmp);
1495 status = -EPIPE;
1496 /* FIXME: continue with next queued TD? */
1497
1498 break;
1499 }
1500 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1501 VDBG("Transfer overflow");
1502 status = -EPROTO;
1503 break;
1504 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1505 VDBG("ISO error");
1506 status = -EILSEQ;
1507 break;
1508 } else
1509 ERR("Unknown error has occured (0x%x)!\r\n",
1510 errors);
1511
1512 } else if (le32_to_cpu(curr_td->size_ioc_sts)
1513 & DTD_STATUS_ACTIVE) {
1514 VDBG("Request not complete");
1515 status = REQ_UNCOMPLETE;
1516 return status;
1517 } else if (remaining_length) {
1518 if (direction) {
1519 VDBG("Transmit dTD remaining length not zero");
1520 status = -EPROTO;
1521 break;
1522 } else {
1523 td_complete++;
1524 break;
1525 }
1526 } else {
1527 td_complete++;
1528 VDBG("dTD transmitted successful ");
1529 }
1530
1531 if (j != curr_req->dtd_count - 1)
1532 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1533 }
1534
1535 if (status)
1536 return status;
1537
1538 curr_req->req.actual = actual;
1539
1540 return 0;
1541}
1542
1543/* Process a DTD completion interrupt */
1544static void dtd_complete_irq(struct fsl_udc *udc)
1545{
1546 u32 bit_pos;
1547 int i, ep_num, direction, bit_mask, status;
1548 struct fsl_ep *curr_ep;
1549 struct fsl_req *curr_req, *temp_req;
1550
1551 /* Clear the bits in the register */
1552 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1553 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1554
1555 if (!bit_pos)
1556 return;
1557
1558 for (i = 0; i < udc->max_ep * 2; i++) {
1559 ep_num = i >> 1;
1560 direction = i % 2;
1561
1562 bit_mask = 1 << (ep_num + 16 * direction);
1563
1564 if (!(bit_pos & bit_mask))
1565 continue;
1566
1567 curr_ep = get_ep_by_pipe(udc, i);
1568
1569 /* If the ep is configured */
1570 if (curr_ep->name == NULL) {
1571 WARN("Invalid EP?");
1572 continue;
1573 }
1574
1575 /* process the req queue until an uncomplete request */
1576 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1577 queue) {
1578 status = process_ep_req(udc, i, curr_req);
1579
1580 VDBG("status of process_ep_req= %d, ep = %d",
1581 status, ep_num);
1582 if (status == REQ_UNCOMPLETE)
1583 break;
1584 /* write back status to req */
1585 curr_req->req.status = status;
1586
1587 if (ep_num == 0) {
1588 ep0_req_complete(udc, curr_ep, curr_req);
1589 break;
1590 } else
1591 done(curr_ep, curr_req, status);
1592 }
1593 }
1594}
1595
1596/* Process a port change interrupt */
1597static void port_change_irq(struct fsl_udc *udc)
1598{
1599 u32 speed;
1600
1601 if (udc->bus_reset)
1602 udc->bus_reset = 0;
1603
1604 /* Bus resetting is finished */
1605 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET)) {
1606 /* Get the speed */
1607 speed = (fsl_readl(&dr_regs->portsc1)
1608 & PORTSCX_PORT_SPEED_MASK);
1609 switch (speed) {
1610 case PORTSCX_PORT_SPEED_HIGH:
1611 udc->gadget.speed = USB_SPEED_HIGH;
1612 break;
1613 case PORTSCX_PORT_SPEED_FULL:
1614 udc->gadget.speed = USB_SPEED_FULL;
1615 break;
1616 case PORTSCX_PORT_SPEED_LOW:
1617 udc->gadget.speed = USB_SPEED_LOW;
1618 break;
1619 default:
1620 udc->gadget.speed = USB_SPEED_UNKNOWN;
1621 break;
1622 }
1623 }
1624
1625 /* Update USB state */
1626 if (!udc->resume_state)
1627 udc->usb_state = USB_STATE_DEFAULT;
1628}
1629
1630/* Process suspend interrupt */
1631static void suspend_irq(struct fsl_udc *udc)
1632{
1633 udc->resume_state = udc->usb_state;
1634 udc->usb_state = USB_STATE_SUSPENDED;
1635
1636 /* report suspend to the driver, serial.c does not support this */
1637 if (udc->driver->suspend)
1638 udc->driver->suspend(&udc->gadget);
1639}
1640
1641static void bus_resume(struct fsl_udc *udc)
1642{
1643 udc->usb_state = udc->resume_state;
1644 udc->resume_state = 0;
1645
1646 /* report resume to the driver, serial.c does not support this */
1647 if (udc->driver->resume)
1648 udc->driver->resume(&udc->gadget);
1649}
1650
1651/* Clear up all ep queues */
1652static int reset_queues(struct fsl_udc *udc)
1653{
1654 u8 pipe;
1655
1656 for (pipe = 0; pipe < udc->max_pipes; pipe++)
1657 udc_reset_ep_queue(udc, pipe);
1658
1659 /* report disconnect; the driver is already quiesced */
1660 udc->driver->disconnect(&udc->gadget);
1661
1662 return 0;
1663}
1664
1665/* Process reset interrupt */
1666static void reset_irq(struct fsl_udc *udc)
1667{
1668 u32 temp;
1669 unsigned long timeout;
1670
1671 /* Clear the device address */
1672 temp = fsl_readl(&dr_regs->deviceaddr);
1673 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
1674
1675 udc->device_address = 0;
1676
1677 /* Clear usb state */
1678 udc->resume_state = 0;
1679 udc->ep0_dir = 0;
1680 udc->ep0_state = WAIT_FOR_SETUP;
1681 udc->remote_wakeup = 0; /* default to 0 on reset */
1682 udc->gadget.b_hnp_enable = 0;
1683 udc->gadget.a_hnp_support = 0;
1684 udc->gadget.a_alt_hnp_support = 0;
1685
1686 /* Clear all the setup token semaphores */
1687 temp = fsl_readl(&dr_regs->endptsetupstat);
1688 fsl_writel(temp, &dr_regs->endptsetupstat);
1689
1690 /* Clear all the endpoint complete status bits */
1691 temp = fsl_readl(&dr_regs->endptcomplete);
1692 fsl_writel(temp, &dr_regs->endptcomplete);
1693
1694 timeout = jiffies + 100;
1695 while (fsl_readl(&dr_regs->endpointprime)) {
1696 /* Wait until all endptprime bits cleared */
1697 if (time_after(jiffies, timeout)) {
1698 ERR("Timeout for reset\n");
1699 break;
1700 }
1701 cpu_relax();
1702 }
1703
1704 /* Write 1s to the flush register */
1705 fsl_writel(0xffffffff, &dr_regs->endptflush);
1706
1707 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1708 VDBG("Bus reset");
1709 /* Bus is reseting */
1710 udc->bus_reset = 1;
1711 /* Reset all the queues, include XD, dTD, EP queue
1712 * head and TR Queue */
1713 reset_queues(udc);
1714 udc->usb_state = USB_STATE_DEFAULT;
1715 } else {
1716 VDBG("Controller reset");
1717 /* initialize usb hw reg except for regs for EP, not
1718 * touch usbintr reg */
1719 dr_controller_setup(udc);
1720
1721 /* Reset all internal used Queues */
1722 reset_queues(udc);
1723
1724 ep0_setup(udc);
1725
1726 /* Enable DR IRQ reg, Set Run bit, change udc state */
1727 dr_controller_run(udc);
1728 udc->usb_state = USB_STATE_ATTACHED;
1729 }
1730}
1731
1732/*
1733 * USB device controller interrupt handler
1734 */
1735static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1736{
1737 struct fsl_udc *udc = _udc;
1738 u32 irq_src;
1739 irqreturn_t status = IRQ_NONE;
1740 unsigned long flags;
1741
1742 /* Disable ISR for OTG host mode */
1743 if (udc->stopped)
1744 return IRQ_NONE;
1745 spin_lock_irqsave(&udc->lock, flags);
1746 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
1747 /* Clear notification bits */
1748 fsl_writel(irq_src, &dr_regs->usbsts);
1749
1750 /* VDBG("irq_src [0x%8x]", irq_src); */
1751
1752 /* Need to resume? */
1753 if (udc->usb_state == USB_STATE_SUSPENDED)
1754 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
1755 bus_resume(udc);
1756
1757 /* USB Interrupt */
1758 if (irq_src & USB_STS_INT) {
1759 VDBG("Packet int");
1760 /* Setup package, we only support ep0 as control ep */
1761 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
1762 tripwire_handler(udc, 0,
1763 (u8 *) (&udc->local_setup_buff));
1764 setup_received_irq(udc, &udc->local_setup_buff);
1765 status = IRQ_HANDLED;
1766 }
1767
1768 /* completion of dtd */
1769 if (fsl_readl(&dr_regs->endptcomplete)) {
1770 dtd_complete_irq(udc);
1771 status = IRQ_HANDLED;
1772 }
1773 }
1774
1775 /* SOF (for ISO transfer) */
1776 if (irq_src & USB_STS_SOF) {
1777 status = IRQ_HANDLED;
1778 }
1779
1780 /* Port Change */
1781 if (irq_src & USB_STS_PORT_CHANGE) {
1782 port_change_irq(udc);
1783 status = IRQ_HANDLED;
1784 }
1785
1786 /* Reset Received */
1787 if (irq_src & USB_STS_RESET) {
1788 reset_irq(udc);
1789 status = IRQ_HANDLED;
1790 }
1791
1792 /* Sleep Enable (Suspend) */
1793 if (irq_src & USB_STS_SUSPEND) {
1794 suspend_irq(udc);
1795 status = IRQ_HANDLED;
1796 }
1797
1798 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
1799 VDBG("Error IRQ %x ", irq_src);
1800 }
1801
1802 spin_unlock_irqrestore(&udc->lock, flags);
1803 return status;
1804}
1805
1806/*----------------------------------------------------------------*
1807 * Hook to gadget drivers
1808 * Called by initialization code of gadget drivers
1809*----------------------------------------------------------------*/
1810int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1811{
1812 int retval = -ENODEV;
1813 unsigned long flags = 0;
1814
1815 if (!udc_controller)
1816 return -ENODEV;
1817
1818 if (!driver || (driver->speed != USB_SPEED_FULL
1819 && driver->speed != USB_SPEED_HIGH)
1820 || !driver->bind || !driver->disconnect
1821 || !driver->setup)
1822 return -EINVAL;
1823
1824 if (udc_controller->driver)
1825 return -EBUSY;
1826
1827 /* lock is needed but whether should use this lock or another */
1828 spin_lock_irqsave(&udc_controller->lock, flags);
1829
1830 driver->driver.bus = 0;
1831 /* hook up the driver */
1832 udc_controller->driver = driver;
1833 udc_controller->gadget.dev.driver = &driver->driver;
1834 spin_unlock_irqrestore(&udc_controller->lock, flags);
1835
1836 /* bind udc driver to gadget driver */
1837 retval = driver->bind(&udc_controller->gadget);
1838 if (retval) {
1839 VDBG("bind to %s --> %d", driver->driver.name, retval);
1840 udc_controller->gadget.dev.driver = 0;
1841 udc_controller->driver = 0;
1842 goto out;
1843 }
1844
1845 /* Enable DR IRQ reg and Set usbcmd reg Run bit */
1846 dr_controller_run(udc_controller);
1847 udc_controller->usb_state = USB_STATE_ATTACHED;
1848 udc_controller->ep0_state = WAIT_FOR_SETUP;
1849 udc_controller->ep0_dir = 0;
1850 printk(KERN_INFO "%s: bind to driver %s \n",
1851 udc_controller->gadget.name, driver->driver.name);
1852
1853out:
1854 if (retval)
1855 printk("retval %d \n", retval);
1856 return retval;
1857}
1858EXPORT_SYMBOL(usb_gadget_register_driver);
1859
1860/* Disconnect from gadget driver */
1861int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1862{
1863 struct fsl_ep *loop_ep;
1864 unsigned long flags;
1865
1866 if (!udc_controller)
1867 return -ENODEV;
1868
1869 if (!driver || driver != udc_controller->driver || !driver->unbind)
1870 return -EINVAL;
1871
1872#ifdef CONFIG_USB_OTG
1873 if (udc_controller->transceiver)
1874 (void)otg_set_peripheral(udc_controller->transceiver, 0);
1875#endif
1876
1877 /* stop DR, disable intr */
1878 dr_controller_stop(udc_controller);
1879
1880 /* in fact, no needed */
1881 udc_controller->usb_state = USB_STATE_ATTACHED;
1882 udc_controller->ep0_state = WAIT_FOR_SETUP;
1883 udc_controller->ep0_dir = 0;
1884
1885 /* stand operation */
1886 spin_lock_irqsave(&udc_controller->lock, flags);
1887 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
1888 nuke(&udc_controller->eps[0], -ESHUTDOWN);
1889 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
1890 ep.ep_list)
1891 nuke(loop_ep, -ESHUTDOWN);
1892 spin_unlock_irqrestore(&udc_controller->lock, flags);
1893
1894 /* unbind gadget and unhook driver. */
1895 driver->unbind(&udc_controller->gadget);
1896 udc_controller->gadget.dev.driver = 0;
1897 udc_controller->driver = 0;
1898
1899 printk("unregistered gadget driver '%s'\r\n", driver->driver.name);
1900 return 0;
1901}
1902EXPORT_SYMBOL(usb_gadget_unregister_driver);
1903
1904/*-------------------------------------------------------------------------
1905 PROC File System Support
1906-------------------------------------------------------------------------*/
1907#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1908
1909#include <linux/seq_file.h>
1910
1911static const char proc_filename[] = "driver/fsl_usb2_udc";
1912
1913static int fsl_proc_read(char *page, char **start, off_t off, int count,
1914 int *eof, void *_dev)
1915{
1916 char *buf = page;
1917 char *next = buf;
1918 unsigned size = count;
1919 unsigned long flags;
1920 int t, i;
1921 u32 tmp_reg;
1922 struct fsl_ep *ep = NULL;
1923 struct fsl_req *req;
1924
1925 struct fsl_udc *udc = udc_controller;
1926 if (off != 0)
1927 return 0;
1928
1929 spin_lock_irqsave(&udc->lock, flags);
1930
1931 /* ------basic driver infomation ---- */
1932 t = scnprintf(next, size,
1933 DRIVER_DESC "\n"
1934 "%s version: %s\n"
1935 "Gadget driver: %s\n\n",
1936 driver_name, DRIVER_VERSION,
1937 udc->driver ? udc->driver->driver.name : "(none)");
1938 size -= t;
1939 next += t;
1940
1941 /* ------ DR Registers ----- */
1942 tmp_reg = fsl_readl(&dr_regs->usbcmd);
1943 t = scnprintf(next, size,
1944 "USBCMD reg:\n"
1945 "SetupTW: %d\n"
1946 "Run/Stop: %s\n\n",
1947 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
1948 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
1949 size -= t;
1950 next += t;
1951
1952 tmp_reg = fsl_readl(&dr_regs->usbsts);
1953 t = scnprintf(next, size,
1954 "USB Status Reg:\n"
1955 "Dr Suspend: %d" "Reset Received: %d" "System Error: %s"
1956 "USB Error Interrupt: %s\n\n",
1957 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
1958 (tmp_reg & USB_STS_RESET) ? 1 : 0,
1959 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
1960 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
1961 size -= t;
1962 next += t;
1963
1964 tmp_reg = fsl_readl(&dr_regs->usbintr);
1965 t = scnprintf(next, size,
1966 "USB Intrrupt Enable Reg:\n"
1967 "Sleep Enable: %d" "SOF Received Enable: %d"
1968 "Reset Enable: %d\n"
1969 "System Error Enable: %d"
1970 "Port Change Dectected Enable: %d\n"
1971 "USB Error Intr Enable: %d" "USB Intr Enable: %d\n\n",
1972 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
1973 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
1974 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
1975 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
1976 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
1977 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
1978 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
1979 size -= t;
1980 next += t;
1981
1982 tmp_reg = fsl_readl(&dr_regs->frindex);
1983 t = scnprintf(next, size,
1984 "USB Frame Index Reg:" "Frame Number is 0x%x\n\n",
1985 (tmp_reg & USB_FRINDEX_MASKS));
1986 size -= t;
1987 next += t;
1988
1989 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
1990 t = scnprintf(next, size,
1991 "USB Device Address Reg:" "Device Addr is 0x%x\n\n",
1992 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
1993 size -= t;
1994 next += t;
1995
1996 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
1997 t = scnprintf(next, size,
1998 "USB Endpoint List Address Reg:"
1999 "Device Addr is 0x%x\n\n",
2000 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
2001 size -= t;
2002 next += t;
2003
2004 tmp_reg = fsl_readl(&dr_regs->portsc1);
2005 t = scnprintf(next, size,
2006 "USB Port Status&Control Reg:\n"
2007 "Port Transceiver Type : %s" "Port Speed: %s \n"
2008 "PHY Low Power Suspend: %s" "Port Reset: %s"
2009 "Port Suspend Mode: %s \n" "Over-current Change: %s"
2010 "Port Enable/Disable Change: %s\n"
2011 "Port Enabled/Disabled: %s"
2012 "Current Connect Status: %s\n\n", ( {
2013 char *s;
2014 switch (tmp_reg & PORTSCX_PTS_FSLS) {
2015 case PORTSCX_PTS_UTMI:
2016 s = "UTMI"; break;
2017 case PORTSCX_PTS_ULPI:
2018 s = "ULPI "; break;
2019 case PORTSCX_PTS_FSLS:
2020 s = "FS/LS Serial"; break;
2021 default:
2022 s = "None"; break;
2023 }
2024 s;} ), ( {
2025 char *s;
2026 switch (tmp_reg & PORTSCX_PORT_SPEED_UNDEF) {
2027 case PORTSCX_PORT_SPEED_FULL:
2028 s = "Full Speed"; break;
2029 case PORTSCX_PORT_SPEED_LOW:
2030 s = "Low Speed"; break;
2031 case PORTSCX_PORT_SPEED_HIGH:
2032 s = "High Speed"; break;
2033 default:
2034 s = "Undefined"; break;
2035 }
2036 s;
2037 } ),
2038 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
2039 "Normal PHY mode" : "Low power mode",
2040 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
2041 "Not in Reset",
2042 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
2043 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
2044 "No",
2045 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
2046 "Not change",
2047 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
2048 "Not correct",
2049 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
2050 "Attached" : "Not-Att");
2051 size -= t;
2052 next += t;
2053
2054 tmp_reg = fsl_readl(&dr_regs->usbmode);
2055 t = scnprintf(next, size,
2056 "USB Mode Reg:" "Controller Mode is : %s\n\n", ( {
2057 char *s;
2058 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2059 case USB_MODE_CTRL_MODE_IDLE:
2060 s = "Idle"; break;
2061 case USB_MODE_CTRL_MODE_DEVICE:
2062 s = "Device Controller"; break;
2063 case USB_MODE_CTRL_MODE_HOST:
2064 s = "Host Controller"; break;
2065 default:
2066 s = "None"; break;
2067 }
2068 s;
2069 } ));
2070 size -= t;
2071 next += t;
2072
2073 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2074 t = scnprintf(next, size,
2075 "Endpoint Setup Status Reg:" "SETUP on ep 0x%x\n\n",
2076 (tmp_reg & EP_SETUP_STATUS_MASK));
2077 size -= t;
2078 next += t;
2079
2080 for (i = 0; i < udc->max_ep / 2; i++) {
2081 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2082 t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
2083 i, tmp_reg);
2084 size -= t;
2085 next += t;
2086 }
2087 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2088 t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n", tmp_reg);
2089 size -= t;
2090 next += t;
2091
2092 tmp_reg = usb_sys_regs->snoop1;
2093 t = scnprintf(next, size, "\nSnoop1 Reg : = [0x%x]\n\n", tmp_reg);
2094 size -= t;
2095 next += t;
2096
2097 tmp_reg = usb_sys_regs->control;
2098 t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
2099 tmp_reg);
2100 size -= t;
2101 next += t;
2102
2103 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2104 ep = &udc->eps[0];
2105 t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
2106 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2107 size -= t;
2108 next += t;
2109
2110 if (list_empty(&ep->queue)) {
2111 t = scnprintf(next, size, "its req queue is empty\n\n");
2112 size -= t;
2113 next += t;
2114 } else {
2115 list_for_each_entry(req, &ep->queue, queue) {
2116 t = scnprintf(next, size,
2117 "req %p actual 0x%x length 0x%x buf %p\n",
2118 &req->req, req->req.actual,
2119 req->req.length, req->req.buf);
2120 size -= t;
2121 next += t;
2122 }
2123 }
2124 /* other gadget->eplist ep */
2125 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2126 if (ep->desc) {
2127 t = scnprintf(next, size,
2128 "\nFor %s Maxpkt is 0x%x "
2129 "index is 0x%x\n",
2130 ep->ep.name, ep_maxpacket(ep),
2131 ep_index(ep));
2132 size -= t;
2133 next += t;
2134
2135 if (list_empty(&ep->queue)) {
2136 t = scnprintf(next, size,
2137 "its req queue is empty\n\n");
2138 size -= t;
2139 next += t;
2140 } else {
2141 list_for_each_entry(req, &ep->queue, queue) {
2142 t = scnprintf(next, size,
2143 "req %p actual 0x%x length"
2144 "0x%x buf %p\n",
2145 &req->req, req->req.actual,
2146 req->req.length, req->req.buf);
2147 size -= t;
2148 next += t;
2149 } /* end for each_entry of ep req */
2150 } /* end for else */
2151 } /* end for if(ep->queue) */
2152 } /* end (ep->desc) */
2153
2154 spin_unlock_irqrestore(&udc->lock, flags);
2155
2156 *eof = 1;
2157 return count - size;
2158}
2159
2160#define create_proc_file() create_proc_read_entry(proc_filename, \
2161 0, NULL, fsl_proc_read, NULL)
2162
2163#define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2164
2165#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
2166
2167#define create_proc_file() do {} while (0)
2168#define remove_proc_file() do {} while (0)
2169
2170#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
2171
2172/*-------------------------------------------------------------------------*/
2173
2174/* Release udc structures */
2175static void fsl_udc_release(struct device *dev)
2176{
2177 complete(udc_controller->done);
2178 dma_free_coherent(dev, udc_controller->ep_qh_size,
2179 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2180 kfree(udc_controller);
2181}
2182
2183/******************************************************************
2184 Internal structure setup functions
2185*******************************************************************/
2186/*------------------------------------------------------------------
2187 * init resource for globle controller
2188 * Return the udc handle on success or NULL on failure
2189 ------------------------------------------------------------------*/
2190static struct fsl_udc *__init struct_udc_setup(struct platform_device *pdev)
2191{
2192 struct fsl_udc *udc;
2193 struct fsl_usb2_platform_data *pdata;
2194 size_t size;
2195
2196 udc = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
2197 if (udc == NULL) {
2198 ERR("malloc udc failed\n");
2199 return NULL;
2200 }
2201
2202 pdata = pdev->dev.platform_data;
2203 udc->phy_mode = pdata->phy_mode;
2204 /* max_ep_nr is bidirectional ep number, max_ep doubles the number */
2205 udc->max_ep = pdata->max_ep_nr * 2;
2206
2207 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2208 if (!udc->eps) {
2209 ERR("malloc fsl_ep failed\n");
2210 goto cleanup;
2211 }
2212
2213 /* initialized QHs, take care of alignment */
2214 size = udc->max_ep * sizeof(struct ep_queue_head);
2215 if (size < QH_ALIGNMENT)
2216 size = QH_ALIGNMENT;
2217 else if ((size % QH_ALIGNMENT) != 0) {
2218 size += QH_ALIGNMENT + 1;
2219 size &= ~(QH_ALIGNMENT - 1);
2220 }
2221 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2222 &udc->ep_qh_dma, GFP_KERNEL);
2223 if (!udc->ep_qh) {
2224 ERR("malloc QHs for udc failed\n");
2225 kfree(udc->eps);
2226 goto cleanup;
2227 }
2228
2229 udc->ep_qh_size = size;
2230
2231 /* Initialize ep0 status request structure */
2232 /* FIXME: fsl_alloc_request() ignores ep argument */
2233 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2234 struct fsl_req, req);
2235 /* allocate a small amount of memory to get valid address */
2236 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2237 udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
2238
2239 udc->resume_state = USB_STATE_NOTATTACHED;
2240 udc->usb_state = USB_STATE_POWERED;
2241 udc->ep0_dir = 0;
2242 udc->remote_wakeup = 0; /* default to 0 on reset */
2243 spin_lock_init(&udc->lock);
2244
2245 return udc;
2246
2247cleanup:
2248 kfree(udc);
2249 return NULL;
2250}
2251
2252/*----------------------------------------------------------------
2253 * Setup the fsl_ep struct for eps
2254 * Link fsl_ep->ep to gadget->ep_list
2255 * ep0out is not used so do nothing here
2256 * ep0in should be taken care
2257 *--------------------------------------------------------------*/
2258static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2259 char *name, int link)
2260{
2261 struct fsl_ep *ep = &udc->eps[index];
2262
2263 ep->udc = udc;
2264 strcpy(ep->name, name);
2265 ep->ep.name = ep->name;
2266
2267 ep->ep.ops = &fsl_ep_ops;
2268 ep->stopped = 0;
2269
2270 /* for ep0: maxP defined in desc
2271 * for other eps, maxP is set by epautoconfig() called by gadget layer
2272 */
2273 ep->ep.maxpacket = (unsigned short) ~0;
2274
2275 /* the queue lists any req for this ep */
2276 INIT_LIST_HEAD(&ep->queue);
2277
2278 /* gagdet.ep_list used for ep_autoconfig so no ep0 */
2279 if (link)
2280 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2281 ep->gadget = &udc->gadget;
2282 ep->qh = &udc->ep_qh[index];
2283
2284 return 0;
2285}
2286
2287/* Driver probe function
2288 * all intialize operations implemented here except enabling usb_intr reg
2289 */
2290static int __init fsl_udc_probe(struct platform_device *pdev)
2291{
2292 struct resource *res;
2293 int ret = -ENODEV;
2294 unsigned int i;
2295
2296 if (strcmp(pdev->name, driver_name)) {
2297 VDBG("Wrong device\n");
2298 return -ENODEV;
2299 }
2300
2301 /* board setup should have been done in the platform code */
2302
2303 /* Initialize the udc structure including QH member and other member */
2304 udc_controller = struct_udc_setup(pdev);
2305 if (!udc_controller) {
2306 VDBG("udc_controller is NULL \n");
2307 return -ENOMEM;
2308 }
2309
2310 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2311 if (!res)
2312 return -ENXIO;
2313
2314 if (!request_mem_region(res->start, res->end - res->start + 1,
2315 driver_name)) {
2316 ERR("request mem region for %s failed \n", pdev->name);
2317 return -EBUSY;
2318 }
2319
2320 dr_regs = ioremap(res->start, res->end - res->start + 1);
2321 if (!dr_regs) {
2322 ret = -ENOMEM;
2323 goto err1;
2324 }
2325
2326 usb_sys_regs = (struct usb_sys_interface *)
2327 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2328
2329 udc_controller->irq = platform_get_irq(pdev, 0);
2330 if (!udc_controller->irq) {
2331 ret = -ENODEV;
2332 goto err2;
2333 }
2334
2335 ret = request_irq(udc_controller->irq, fsl_udc_irq, SA_SHIRQ,
2336 driver_name, udc_controller);
2337 if (ret != 0) {
2338 ERR("cannot request irq %d err %d \n",
2339 udc_controller->irq, ret);
2340 goto err2;
2341 }
2342
2343 /* initialize usb hw reg except for regs for EP,
2344 * leave usbintr reg untouched */
2345 dr_controller_setup(udc_controller);
2346
2347 /* Setup gadget structure */
2348 udc_controller->gadget.ops = &fsl_gadget_ops;
2349 udc_controller->gadget.is_dualspeed = 1;
2350 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2351 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2352 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2353 udc_controller->gadget.name = driver_name;
2354
2355 /* Setup gadget.dev and register with kernel */
2356 strcpy(udc_controller->gadget.dev.bus_id, "gadget");
2357 udc_controller->gadget.dev.release = fsl_udc_release;
2358 udc_controller->gadget.dev.parent = &pdev->dev;
2359 ret = device_register(&udc_controller->gadget.dev);
2360 if (ret < 0)
2361 goto err3;
2362
2363 /* setup QH and epctrl for ep0 */
2364 ep0_setup(udc_controller);
2365
2366 /* setup udc->eps[] for ep0 */
2367 struct_ep_setup(udc_controller, 0, "ep0", 0);
2368 /* for ep0: the desc defined here;
2369 * for other eps, gadget layer called ep_enable with defined desc
2370 */
2371 udc_controller->eps[0].desc = &fsl_ep0_desc;
2372 udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
2373
2374 /* setup the udc->eps[] for non-control endpoints and link
2375 * to gadget.ep_list */
2376 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
2377 char name[14];
2378
2379 sprintf(name, "ep%dout", i);
2380 struct_ep_setup(udc_controller, i * 2, name, 1);
2381 sprintf(name, "ep%din", i);
2382 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2383 }
2384
2385 /* use dma_pool for TD management */
2386 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
2387 sizeof(struct ep_td_struct),
2388 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2389 if (udc_controller->td_pool == NULL) {
2390 ret = -ENOMEM;
2391 goto err4;
2392 }
2393 create_proc_file();
2394 return 0;
2395
2396err4:
2397 device_unregister(&udc_controller->gadget.dev);
2398err3:
2399 free_irq(udc_controller->irq, udc_controller);
2400err2:
2401 iounmap(dr_regs);
2402err1:
2403 release_mem_region(res->start, res->end - res->start + 1);
2404 return ret;
2405}
2406
2407/* Driver removal function
2408 * Free resources and finish pending transactions
2409 */
2410static int __exit fsl_udc_remove(struct platform_device *pdev)
2411{
2412 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2413
2414 DECLARE_COMPLETION(done);
2415
2416 if (!udc_controller)
2417 return -ENODEV;
2418 udc_controller->done = &done;
2419
2420 /* DR has been stopped in usb_gadget_unregister_driver() */
2421 remove_proc_file();
2422
2423 /* Free allocated memory */
2424 kfree(udc_controller->status_req->req.buf);
2425 kfree(udc_controller->status_req);
2426 kfree(udc_controller->eps);
2427
2428 dma_pool_destroy(udc_controller->td_pool);
2429 free_irq(udc_controller->irq, udc_controller);
2430 iounmap(dr_regs);
2431 release_mem_region(res->start, res->end - res->start + 1);
2432
2433 device_unregister(&udc_controller->gadget.dev);
2434 /* free udc --wait for the release() finished */
2435 wait_for_completion(&done);
2436
2437 return 0;
2438}
2439
2440/*-----------------------------------------------------------------
2441 * Modify Power management attributes
2442 * Used by OTG statemachine to disable gadget temporarily
2443 -----------------------------------------------------------------*/
2444static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
2445{
2446 dr_controller_stop(udc_controller);
2447 return 0;
2448}
2449
2450/*-----------------------------------------------------------------
2451 * Invoked on USB resume. May be called in_interrupt.
2452 * Here we start the DR controller and enable the irq
2453 *-----------------------------------------------------------------*/
2454static int fsl_udc_resume(struct platform_device *pdev)
2455{
2456 /* Enable DR irq reg and set controller Run */
2457 if (udc_controller->stopped) {
2458 dr_controller_setup(udc_controller);
2459 dr_controller_run(udc_controller);
2460 }
2461 udc_controller->usb_state = USB_STATE_ATTACHED;
2462 udc_controller->ep0_state = WAIT_FOR_SETUP;
2463 udc_controller->ep0_dir = 0;
2464 return 0;
2465}
2466
2467/*-------------------------------------------------------------------------
2468 Register entry point for the peripheral controller driver
2469--------------------------------------------------------------------------*/
2470
2471static struct platform_driver udc_driver = {
2472 .remove = __exit_p(fsl_udc_remove),
2473 /* these suspend and resume are not usb suspend and resume */
2474 .suspend = fsl_udc_suspend,
2475 .resume = fsl_udc_resume,
2476 .driver = {
2477 .name = (char *)driver_name,
2478 .owner = THIS_MODULE,
2479 },
2480};
2481
2482static int __init udc_init(void)
2483{
2484 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
2485 return platform_driver_probe(&udc_driver, fsl_udc_probe);
2486}
2487
2488module_init(udc_init);
2489
2490static void __exit udc_exit(void)
2491{
2492 platform_driver_unregister(&udc_driver);
2493 printk("%s unregistered \n", driver_desc);
2494}
2495
2496module_exit(udc_exit);
2497
2498MODULE_DESCRIPTION(DRIVER_DESC);
2499MODULE_AUTHOR(DRIVER_AUTHOR);
2500MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/fsl_usb2_udc.h b/drivers/usb/gadget/fsl_usb2_udc.h
new file mode 100644
index 000000000000..c6291e046507
--- /dev/null
+++ b/drivers/usb/gadget/fsl_usb2_udc.h
@@ -0,0 +1,579 @@
1/*
2 * Freescale USB device/endpoint management registers
3 */
4#ifndef __FSL_USB2_UDC_H
5#define __FSL_USB2_UDC_H
6
7/* ### define USB registers here
8 */
9#define USB_MAX_CTRL_PAYLOAD 64
10#define USB_DR_SYS_OFFSET 0x400
11
12 /* USB DR device mode registers (Little Endian) */
13struct usb_dr_device {
14 /* Capability register */
15 u8 res1[256];
16 u16 caplength; /* Capability Register Length */
17 u16 hciversion; /* Host Controller Interface Version */
18 u32 hcsparams; /* Host Controller Structual Parameters */
19 u32 hccparams; /* Host Controller Capability Parameters */
20 u8 res2[20];
21 u32 dciversion; /* Device Controller Interface Version */
22 u32 dccparams; /* Device Controller Capability Parameters */
23 u8 res3[24];
24 /* Operation register */
25 u32 usbcmd; /* USB Command Register */
26 u32 usbsts; /* USB Status Register */
27 u32 usbintr; /* USB Interrupt Enable Register */
28 u32 frindex; /* Frame Index Register */
29 u8 res4[4];
30 u32 deviceaddr; /* Device Address */
31 u32 endpointlistaddr; /* Endpoint List Address Register */
32 u8 res5[4];
33 u32 burstsize; /* Master Interface Data Burst Size Register */
34 u32 txttfilltuning; /* Transmit FIFO Tuning Controls Register */
35 u8 res6[24];
36 u32 configflag; /* Configure Flag Register */
37 u32 portsc1; /* Port 1 Status and Control Register */
38 u8 res7[28];
39 u32 otgsc; /* On-The-Go Status and Control */
40 u32 usbmode; /* USB Mode Register */
41 u32 endptsetupstat; /* Endpoint Setup Status Register */
42 u32 endpointprime; /* Endpoint Initialization Register */
43 u32 endptflush; /* Endpoint Flush Register */
44 u32 endptstatus; /* Endpoint Status Register */
45 u32 endptcomplete; /* Endpoint Complete Register */
46 u32 endptctrl[6]; /* Endpoint Control Registers */
47};
48
49 /* USB DR host mode registers (Little Endian) */
50struct usb_dr_host {
51 /* Capability register */
52 u8 res1[256];
53 u16 caplength; /* Capability Register Length */
54 u16 hciversion; /* Host Controller Interface Version */
55 u32 hcsparams; /* Host Controller Structual Parameters */
56 u32 hccparams; /* Host Controller Capability Parameters */
57 u8 res2[20];
58 u32 dciversion; /* Device Controller Interface Version */
59 u32 dccparams; /* Device Controller Capability Parameters */
60 u8 res3[24];
61 /* Operation register */
62 u32 usbcmd; /* USB Command Register */
63 u32 usbsts; /* USB Status Register */
64 u32 usbintr; /* USB Interrupt Enable Register */
65 u32 frindex; /* Frame Index Register */
66 u8 res4[4];
67 u32 periodiclistbase; /* Periodic Frame List Base Address Register */
68 u32 asynclistaddr; /* Current Asynchronous List Address Register */
69 u8 res5[4];
70 u32 burstsize; /* Master Interface Data Burst Size Register */
71 u32 txttfilltuning; /* Transmit FIFO Tuning Controls Register */
72 u8 res6[24];
73 u32 configflag; /* Configure Flag Register */
74 u32 portsc1; /* Port 1 Status and Control Register */
75 u8 res7[28];
76 u32 otgsc; /* On-The-Go Status and Control */
77 u32 usbmode; /* USB Mode Register */
78 u32 endptsetupstat; /* Endpoint Setup Status Register */
79 u32 endpointprime; /* Endpoint Initialization Register */
80 u32 endptflush; /* Endpoint Flush Register */
81 u32 endptstatus; /* Endpoint Status Register */
82 u32 endptcomplete; /* Endpoint Complete Register */
83 u32 endptctrl[6]; /* Endpoint Control Registers */
84};
85
86 /* non-EHCI USB system interface registers (Big Endian) */
87struct usb_sys_interface {
88 u32 snoop1;
89 u32 snoop2;
90 u32 age_cnt_thresh; /* Age Count Threshold Register */
91 u32 pri_ctrl; /* Priority Control Register */
92 u32 si_ctrl; /* System Interface Control Register */
93 u8 res[236];
94 u32 control; /* General Purpose Control Register */
95};
96
97/* ep0 transfer state */
98#define WAIT_FOR_SETUP 0
99#define DATA_STATE_XMIT 1
100#define DATA_STATE_NEED_ZLP 2
101#define WAIT_FOR_OUT_STATUS 3
102#define DATA_STATE_RECV 4
103
104/* Frame Index Register Bit Masks */
105#define USB_FRINDEX_MASKS 0x3fff
106/* USB CMD Register Bit Masks */
107#define USB_CMD_RUN_STOP 0x00000001
108#define USB_CMD_CTRL_RESET 0x00000002
109#define USB_CMD_PERIODIC_SCHEDULE_EN 0x00000010
110#define USB_CMD_ASYNC_SCHEDULE_EN 0x00000020
111#define USB_CMD_INT_AA_DOORBELL 0x00000040
112#define USB_CMD_ASP 0x00000300
113#define USB_CMD_ASYNC_SCH_PARK_EN 0x00000800
114#define USB_CMD_SUTW 0x00002000
115#define USB_CMD_ATDTW 0x00004000
116#define USB_CMD_ITC 0x00FF0000
117
118/* bit 15,3,2 are frame list size */
119#define USB_CMD_FRAME_SIZE_1024 0x00000000
120#define USB_CMD_FRAME_SIZE_512 0x00000004
121#define USB_CMD_FRAME_SIZE_256 0x00000008
122#define USB_CMD_FRAME_SIZE_128 0x0000000C
123#define USB_CMD_FRAME_SIZE_64 0x00008000
124#define USB_CMD_FRAME_SIZE_32 0x00008004
125#define USB_CMD_FRAME_SIZE_16 0x00008008
126#define USB_CMD_FRAME_SIZE_8 0x0000800C
127
128/* bit 9-8 are async schedule park mode count */
129#define USB_CMD_ASP_00 0x00000000
130#define USB_CMD_ASP_01 0x00000100
131#define USB_CMD_ASP_10 0x00000200
132#define USB_CMD_ASP_11 0x00000300
133#define USB_CMD_ASP_BIT_POS 8
134
135/* bit 23-16 are interrupt threshold control */
136#define USB_CMD_ITC_NO_THRESHOLD 0x00000000
137#define USB_CMD_ITC_1_MICRO_FRM 0x00010000
138#define USB_CMD_ITC_2_MICRO_FRM 0x00020000
139#define USB_CMD_ITC_4_MICRO_FRM 0x00040000
140#define USB_CMD_ITC_8_MICRO_FRM 0x00080000
141#define USB_CMD_ITC_16_MICRO_FRM 0x00100000
142#define USB_CMD_ITC_32_MICRO_FRM 0x00200000
143#define USB_CMD_ITC_64_MICRO_FRM 0x00400000
144#define USB_CMD_ITC_BIT_POS 16
145
146/* USB STS Register Bit Masks */
147#define USB_STS_INT 0x00000001
148#define USB_STS_ERR 0x00000002
149#define USB_STS_PORT_CHANGE 0x00000004
150#define USB_STS_FRM_LST_ROLL 0x00000008
151#define USB_STS_SYS_ERR 0x00000010
152#define USB_STS_IAA 0x00000020
153#define USB_STS_RESET 0x00000040
154#define USB_STS_SOF 0x00000080
155#define USB_STS_SUSPEND 0x00000100
156#define USB_STS_HC_HALTED 0x00001000
157#define USB_STS_RCL 0x00002000
158#define USB_STS_PERIODIC_SCHEDULE 0x00004000
159#define USB_STS_ASYNC_SCHEDULE 0x00008000
160
161/* USB INTR Register Bit Masks */
162#define USB_INTR_INT_EN 0x00000001
163#define USB_INTR_ERR_INT_EN 0x00000002
164#define USB_INTR_PTC_DETECT_EN 0x00000004
165#define USB_INTR_FRM_LST_ROLL_EN 0x00000008
166#define USB_INTR_SYS_ERR_EN 0x00000010
167#define USB_INTR_ASYN_ADV_EN 0x00000020
168#define USB_INTR_RESET_EN 0x00000040
169#define USB_INTR_SOF_EN 0x00000080
170#define USB_INTR_DEVICE_SUSPEND 0x00000100
171
172/* Device Address bit masks */
173#define USB_DEVICE_ADDRESS_MASK 0xFE000000
174#define USB_DEVICE_ADDRESS_BIT_POS 25
175
176/* endpoint list address bit masks */
177#define USB_EP_LIST_ADDRESS_MASK 0xfffff800
178
179/* PORTSCX Register Bit Masks */
180#define PORTSCX_CURRENT_CONNECT_STATUS 0x00000001
181#define PORTSCX_CONNECT_STATUS_CHANGE 0x00000002
182#define PORTSCX_PORT_ENABLE 0x00000004
183#define PORTSCX_PORT_EN_DIS_CHANGE 0x00000008
184#define PORTSCX_OVER_CURRENT_ACT 0x00000010
185#define PORTSCX_OVER_CURRENT_CHG 0x00000020
186#define PORTSCX_PORT_FORCE_RESUME 0x00000040
187#define PORTSCX_PORT_SUSPEND 0x00000080
188#define PORTSCX_PORT_RESET 0x00000100
189#define PORTSCX_LINE_STATUS_BITS 0x00000C00
190#define PORTSCX_PORT_POWER 0x00001000
191#define PORTSCX_PORT_INDICTOR_CTRL 0x0000C000
192#define PORTSCX_PORT_TEST_CTRL 0x000F0000
193#define PORTSCX_WAKE_ON_CONNECT_EN 0x00100000
194#define PORTSCX_WAKE_ON_CONNECT_DIS 0x00200000
195#define PORTSCX_WAKE_ON_OVER_CURRENT 0x00400000
196#define PORTSCX_PHY_LOW_POWER_SPD 0x00800000
197#define PORTSCX_PORT_FORCE_FULL_SPEED 0x01000000
198#define PORTSCX_PORT_SPEED_MASK 0x0C000000
199#define PORTSCX_PORT_WIDTH 0x10000000
200#define PORTSCX_PHY_TYPE_SEL 0xC0000000
201
202/* bit 11-10 are line status */
203#define PORTSCX_LINE_STATUS_SE0 0x00000000
204#define PORTSCX_LINE_STATUS_JSTATE 0x00000400
205#define PORTSCX_LINE_STATUS_KSTATE 0x00000800
206#define PORTSCX_LINE_STATUS_UNDEF 0x00000C00
207#define PORTSCX_LINE_STATUS_BIT_POS 10
208
209/* bit 15-14 are port indicator control */
210#define PORTSCX_PIC_OFF 0x00000000
211#define PORTSCX_PIC_AMBER 0x00004000
212#define PORTSCX_PIC_GREEN 0x00008000
213#define PORTSCX_PIC_UNDEF 0x0000C000
214#define PORTSCX_PIC_BIT_POS 14
215
216/* bit 19-16 are port test control */
217#define PORTSCX_PTC_DISABLE 0x00000000
218#define PORTSCX_PTC_JSTATE 0x00010000
219#define PORTSCX_PTC_KSTATE 0x00020000
220#define PORTSCX_PTC_SEQNAK 0x00030000
221#define PORTSCX_PTC_PACKET 0x00040000
222#define PORTSCX_PTC_FORCE_EN 0x00050000
223#define PORTSCX_PTC_BIT_POS 16
224
225/* bit 27-26 are port speed */
226#define PORTSCX_PORT_SPEED_FULL 0x00000000
227#define PORTSCX_PORT_SPEED_LOW 0x04000000
228#define PORTSCX_PORT_SPEED_HIGH 0x08000000
229#define PORTSCX_PORT_SPEED_UNDEF 0x0C000000
230#define PORTSCX_SPEED_BIT_POS 26
231
232/* bit 28 is parallel transceiver width for UTMI interface */
233#define PORTSCX_PTW 0x10000000
234#define PORTSCX_PTW_8BIT 0x00000000
235#define PORTSCX_PTW_16BIT 0x10000000
236
237/* bit 31-30 are port transceiver select */
238#define PORTSCX_PTS_UTMI 0x00000000
239#define PORTSCX_PTS_ULPI 0x80000000
240#define PORTSCX_PTS_FSLS 0xC0000000
241#define PORTSCX_PTS_BIT_POS 30
242
243/* otgsc Register Bit Masks */
244#define OTGSC_CTRL_VUSB_DISCHARGE 0x00000001
245#define OTGSC_CTRL_VUSB_CHARGE 0x00000002
246#define OTGSC_CTRL_OTG_TERM 0x00000008
247#define OTGSC_CTRL_DATA_PULSING 0x00000010
248#define OTGSC_STS_USB_ID 0x00000100
249#define OTGSC_STS_A_VBUS_VALID 0x00000200
250#define OTGSC_STS_A_SESSION_VALID 0x00000400
251#define OTGSC_STS_B_SESSION_VALID 0x00000800
252#define OTGSC_STS_B_SESSION_END 0x00001000
253#define OTGSC_STS_1MS_TOGGLE 0x00002000
254#define OTGSC_STS_DATA_PULSING 0x00004000
255#define OTGSC_INTSTS_USB_ID 0x00010000
256#define OTGSC_INTSTS_A_VBUS_VALID 0x00020000
257#define OTGSC_INTSTS_A_SESSION_VALID 0x00040000
258#define OTGSC_INTSTS_B_SESSION_VALID 0x00080000
259#define OTGSC_INTSTS_B_SESSION_END 0x00100000
260#define OTGSC_INTSTS_1MS 0x00200000
261#define OTGSC_INTSTS_DATA_PULSING 0x00400000
262#define OTGSC_INTR_USB_ID 0x01000000
263#define OTGSC_INTR_A_VBUS_VALID 0x02000000
264#define OTGSC_INTR_A_SESSION_VALID 0x04000000
265#define OTGSC_INTR_B_SESSION_VALID 0x08000000
266#define OTGSC_INTR_B_SESSION_END 0x10000000
267#define OTGSC_INTR_1MS_TIMER 0x20000000
268#define OTGSC_INTR_DATA_PULSING 0x40000000
269
270/* USB MODE Register Bit Masks */
271#define USB_MODE_CTRL_MODE_IDLE 0x00000000
272#define USB_MODE_CTRL_MODE_DEVICE 0x00000002
273#define USB_MODE_CTRL_MODE_HOST 0x00000003
274#define USB_MODE_CTRL_MODE_RSV 0x00000001
275#define USB_MODE_SETUP_LOCK_OFF 0x00000008
276#define USB_MODE_STREAM_DISABLE 0x00000010
277/* Endpoint Flush Register */
278#define EPFLUSH_TX_OFFSET 0x00010000
279#define EPFLUSH_RX_OFFSET 0x00000000
280
281/* Endpoint Setup Status bit masks */
282#define EP_SETUP_STATUS_MASK 0x0000003F
283#define EP_SETUP_STATUS_EP0 0x00000001
284
285/* ENDPOINTCTRLx Register Bit Masks */
286#define EPCTRL_TX_ENABLE 0x00800000
287#define EPCTRL_TX_DATA_TOGGLE_RST 0x00400000 /* Not EP0 */
288#define EPCTRL_TX_DATA_TOGGLE_INH 0x00200000 /* Not EP0 */
289#define EPCTRL_TX_TYPE 0x000C0000
290#define EPCTRL_TX_DATA_SOURCE 0x00020000 /* Not EP0 */
291#define EPCTRL_TX_EP_STALL 0x00010000
292#define EPCTRL_RX_ENABLE 0x00000080
293#define EPCTRL_RX_DATA_TOGGLE_RST 0x00000040 /* Not EP0 */
294#define EPCTRL_RX_DATA_TOGGLE_INH 0x00000020 /* Not EP0 */
295#define EPCTRL_RX_TYPE 0x0000000C
296#define EPCTRL_RX_DATA_SINK 0x00000002 /* Not EP0 */
297#define EPCTRL_RX_EP_STALL 0x00000001
298
299/* bit 19-18 and 3-2 are endpoint type */
300#define EPCTRL_EP_TYPE_CONTROL 0
301#define EPCTRL_EP_TYPE_ISO 1
302#define EPCTRL_EP_TYPE_BULK 2
303#define EPCTRL_EP_TYPE_INTERRUPT 3
304#define EPCTRL_TX_EP_TYPE_SHIFT 18
305#define EPCTRL_RX_EP_TYPE_SHIFT 2
306
307/* SNOOPn Register Bit Masks */
308#define SNOOP_ADDRESS_MASK 0xFFFFF000
309#define SNOOP_SIZE_ZERO 0x00 /* snooping disable */
310#define SNOOP_SIZE_4KB 0x0B /* 4KB snoop size */
311#define SNOOP_SIZE_8KB 0x0C
312#define SNOOP_SIZE_16KB 0x0D
313#define SNOOP_SIZE_32KB 0x0E
314#define SNOOP_SIZE_64KB 0x0F
315#define SNOOP_SIZE_128KB 0x10
316#define SNOOP_SIZE_256KB 0x11
317#define SNOOP_SIZE_512KB 0x12
318#define SNOOP_SIZE_1MB 0x13
319#define SNOOP_SIZE_2MB 0x14
320#define SNOOP_SIZE_4MB 0x15
321#define SNOOP_SIZE_8MB 0x16
322#define SNOOP_SIZE_16MB 0x17
323#define SNOOP_SIZE_32MB 0x18
324#define SNOOP_SIZE_64MB 0x19
325#define SNOOP_SIZE_128MB 0x1A
326#define SNOOP_SIZE_256MB 0x1B
327#define SNOOP_SIZE_512MB 0x1C
328#define SNOOP_SIZE_1GB 0x1D
329#define SNOOP_SIZE_2GB 0x1E /* 2GB snoop size */
330
331/* pri_ctrl Register Bit Masks */
332#define PRI_CTRL_PRI_LVL1 0x0000000C
333#define PRI_CTRL_PRI_LVL0 0x00000003
334
335/* si_ctrl Register Bit Masks */
336#define SI_CTRL_ERR_DISABLE 0x00000010
337#define SI_CTRL_IDRC_DISABLE 0x00000008
338#define SI_CTRL_RD_SAFE_EN 0x00000004
339#define SI_CTRL_RD_PREFETCH_DISABLE 0x00000002
340#define SI_CTRL_RD_PREFEFETCH_VAL 0x00000001
341
342/* control Register Bit Masks */
343#define USB_CTRL_IOENB 0x00000004
344#define USB_CTRL_ULPI_INT0EN 0x00000001
345
346/* Endpoint Queue Head data struct
347 * Rem: all the variables of qh are LittleEndian Mode
348 * and NEXT_POINTER_MASK should operate on a LittleEndian, Phy Addr
349 */
350struct ep_queue_head {
351 u32 max_pkt_length; /* Mult(31-30) , Zlt(29) , Max Pkt len
352 and IOS(15) */
353 u32 curr_dtd_ptr; /* Current dTD Pointer(31-5) */
354 u32 next_dtd_ptr; /* Next dTD Pointer(31-5), T(0) */
355 u32 size_ioc_int_sts; /* Total bytes (30-16), IOC (15),
356 MultO(11-10), STS (7-0) */
357 u32 buff_ptr0; /* Buffer pointer Page 0 (31-12) */
358 u32 buff_ptr1; /* Buffer pointer Page 1 (31-12) */
359 u32 buff_ptr2; /* Buffer pointer Page 2 (31-12) */
360 u32 buff_ptr3; /* Buffer pointer Page 3 (31-12) */
361 u32 buff_ptr4; /* Buffer pointer Page 4 (31-12) */
362 u32 res1;
363 u8 setup_buffer[8]; /* Setup data 8 bytes */
364 u32 res2[4];
365};
366
367/* Endpoint Queue Head Bit Masks */
368#define EP_QUEUE_HEAD_MULT_POS 30
369#define EP_QUEUE_HEAD_ZLT_SEL 0x20000000
370#define EP_QUEUE_HEAD_MAX_PKT_LEN_POS 16
371#define EP_QUEUE_HEAD_MAX_PKT_LEN(ep_info) (((ep_info)>>16)&0x07ff)
372#define EP_QUEUE_HEAD_IOS 0x00008000
373#define EP_QUEUE_HEAD_NEXT_TERMINATE 0x00000001
374#define EP_QUEUE_HEAD_IOC 0x00008000
375#define EP_QUEUE_HEAD_MULTO 0x00000C00
376#define EP_QUEUE_HEAD_STATUS_HALT 0x00000040
377#define EP_QUEUE_HEAD_STATUS_ACTIVE 0x00000080
378#define EP_QUEUE_CURRENT_OFFSET_MASK 0x00000FFF
379#define EP_QUEUE_HEAD_NEXT_POINTER_MASK 0xFFFFFFE0
380#define EP_QUEUE_FRINDEX_MASK 0x000007FF
381#define EP_MAX_LENGTH_TRANSFER 0x4000
382
383/* Endpoint Transfer Descriptor data struct */
384/* Rem: all the variables of td are LittleEndian Mode */
385struct ep_td_struct {
386 u32 next_td_ptr; /* Next TD pointer(31-5), T(0) set
387 indicate invalid */
388 u32 size_ioc_sts; /* Total bytes (30-16), IOC (15),
389 MultO(11-10), STS (7-0) */
390 u32 buff_ptr0; /* Buffer pointer Page 0 */
391 u32 buff_ptr1; /* Buffer pointer Page 1 */
392 u32 buff_ptr2; /* Buffer pointer Page 2 */
393 u32 buff_ptr3; /* Buffer pointer Page 3 */
394 u32 buff_ptr4; /* Buffer pointer Page 4 */
395 u32 res;
396 /* 32 bytes */
397 dma_addr_t td_dma; /* dma address for this td */
398 /* virtual address of next td specified in next_td_ptr */
399 struct ep_td_struct *next_td_virt;
400};
401
402/* Endpoint Transfer Descriptor bit Masks */
403#define DTD_NEXT_TERMINATE 0x00000001
404#define DTD_IOC 0x00008000
405#define DTD_STATUS_ACTIVE 0x00000080
406#define DTD_STATUS_HALTED 0x00000040
407#define DTD_STATUS_DATA_BUFF_ERR 0x00000020
408#define DTD_STATUS_TRANSACTION_ERR 0x00000008
409#define DTD_RESERVED_FIELDS 0x80007300
410#define DTD_ADDR_MASK 0xFFFFFFE0
411#define DTD_PACKET_SIZE 0x7FFF0000
412#define DTD_LENGTH_BIT_POS 16
413#define DTD_ERROR_MASK (DTD_STATUS_HALTED | \
414 DTD_STATUS_DATA_BUFF_ERR | \
415 DTD_STATUS_TRANSACTION_ERR)
416/* Alignment requirements; must be a power of two */
417#define DTD_ALIGNMENT 0x20
418#define QH_ALIGNMENT 2048
419
420/* Controller dma boundary */
421#define UDC_DMA_BOUNDARY 0x1000
422
423/* -----------------------------------------------------------------------*/
424/* ##### enum data
425*/
426typedef enum {
427 e_ULPI,
428 e_UTMI_8BIT,
429 e_UTMI_16BIT,
430 e_SERIAL
431} e_PhyInterface;
432
433/*-------------------------------------------------------------------------*/
434
435/* ### driver private data
436 */
437struct fsl_req {
438 struct usb_request req;
439 struct list_head queue;
440 /* ep_queue() func will add
441 a request->queue into a udc_ep->queue 'd tail */
442 struct fsl_ep *ep;
443 unsigned mapped:1;
444
445 struct ep_td_struct *head, *tail; /* For dTD List
446 cpu endian Virtual addr */
447 unsigned int dtd_count;
448};
449
450#define REQ_UNCOMPLETE 1
451
452struct fsl_ep {
453 struct usb_ep ep;
454 struct list_head queue;
455 struct fsl_udc *udc;
456 struct ep_queue_head *qh;
457 const struct usb_endpoint_descriptor *desc;
458 struct usb_gadget *gadget;
459
460 char name[14];
461 unsigned stopped:1;
462};
463
464#define EP_DIR_IN 1
465#define EP_DIR_OUT 0
466
467struct fsl_udc {
468
469 struct usb_gadget gadget;
470 struct usb_gadget_driver *driver;
471 struct fsl_ep *eps;
472 unsigned int max_ep;
473 unsigned int irq;
474
475 struct usb_ctrlrequest local_setup_buff;
476 spinlock_t lock;
477 struct otg_transceiver *transceiver;
478 unsigned softconnect:1;
479 unsigned vbus_active:1;
480 unsigned stopped:1;
481 unsigned remote_wakeup:1;
482
483 struct ep_queue_head *ep_qh; /* Endpoints Queue-Head */
484 struct fsl_req *status_req; /* ep0 status request */
485 struct dma_pool *td_pool; /* dma pool for DTD */
486 enum fsl_usb2_phy_modes phy_mode;
487
488 size_t ep_qh_size; /* size after alignment adjustment*/
489 dma_addr_t ep_qh_dma; /* dma address of QH */
490
491 u32 max_pipes; /* Device max pipes */
492 u32 max_use_endpts; /* Max endpointes to be used */
493 u32 bus_reset; /* Device is bus reseting */
494 u32 resume_state; /* USB state to resume */
495 u32 usb_state; /* USB current state */
496 u32 usb_next_state; /* USB next state */
497 u32 ep0_state; /* Endpoint zero state */
498 u32 ep0_dir; /* Endpoint zero direction: can be
499 USB_DIR_IN or USB_DIR_OUT */
500 u32 usb_sof_count; /* SOF count */
501 u32 errors; /* USB ERRORs count */
502 u8 device_address; /* Device USB address */
503
504 struct completion *done; /* to make sure release() is done */
505};
506
507/*-------------------------------------------------------------------------*/
508
509#ifdef DEBUG
510#define DBG(fmt, args...) printk(KERN_DEBUG "[%s] " fmt "\n", \
511 __FUNCTION__, ## args)
512#else
513#define DBG(fmt, args...) do{}while(0)
514#endif
515
516#if 0
517static void dump_msg(const char *label, const u8 * buf, unsigned int length)
518{
519 unsigned int start, num, i;
520 char line[52], *p;
521
522 if (length >= 512)
523 return;
524 DBG("%s, length %u:\n", label, length);
525 start = 0;
526 while (length > 0) {
527 num = min(length, 16u);
528 p = line;
529 for (i = 0; i < num; ++i) {
530 if (i == 8)
531 *p++ = ' ';
532 sprintf(p, " %02x", buf[i]);
533 p += 3;
534 }
535 *p = 0;
536 printk(KERN_DEBUG "%6x: %s\n", start, line);
537 buf += num;
538 start += num;
539 length -= num;
540 }
541}
542#endif
543
544#ifdef VERBOSE
545#define VDBG DBG
546#else
547#define VDBG(stuff...) do{}while(0)
548#endif
549
550#define ERR(stuff...) printk(KERN_ERR "udc: " stuff)
551#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
552#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
553
554/*-------------------------------------------------------------------------*/
555
556/* ### Add board specific defines here
557 */
558
559/*
560 * ### pipe direction macro from device view
561 */
562#define USB_RECV 0 /* OUT EP */
563#define USB_SEND 1 /* IN EP */
564
565/*
566 * ### internal used help routines.
567 */
568#define ep_index(EP) ((EP)->desc->bEndpointAddress&0xF)
569#define ep_maxpacket(EP) ((EP)->ep.maxpacket)
570#define ep_is_in(EP) ( (ep_index(EP) == 0) ? (EP->udc->ep0_dir == \
571 USB_DIR_IN ):((EP)->desc->bEndpointAddress \
572 & USB_DIR_IN)==USB_DIR_IN)
573#define get_ep_by_pipe(udc, pipe) ((pipe == 1)? &udc->eps[0]: \
574 &udc->eps[pipe])
575#define get_pipe_by_windex(windex) ((windex & USB_ENDPOINT_NUMBER_MASK) \
576 * 2 + ((windex & USB_DIR_IN) ? 1 : 0))
577#define get_pipe_by_ep(EP) (ep_index(EP) * 2 + ep_is_in(EP))
578
579#endif
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index 2e3d6620d216..d041b919e7b8 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -99,6 +99,12 @@
99#define gadget_is_imx(g) 0 99#define gadget_is_imx(g) 0
100#endif 100#endif
101 101
102#ifdef CONFIG_USB_GADGET_FSL_USB2
103#define gadget_is_fsl_usb2(g) !strcmp("fsl-usb2-udc", (g)->name)
104#else
105#define gadget_is_fsl_usb2(g) 0
106#endif
107
102/* Mentor high speed function controller */ 108/* Mentor high speed function controller */
103#ifdef CONFIG_USB_GADGET_MUSBHSFC 109#ifdef CONFIG_USB_GADGET_MUSBHSFC
104#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name) 110#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name)
@@ -177,5 +183,7 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
177 return 0x17; 183 return 0x17;
178 else if (gadget_is_husb2dev(gadget)) 184 else if (gadget_is_husb2dev(gadget))
179 return 0x18; 185 return 0x18;
186 else if (gadget_is_fsl_usb2(gadget))
187 return 0x19;
180 return -ENOENT; 188 return -ENOENT;
181} 189}
diff --git a/drivers/usb/gadget/pxa2xx_udc.c b/drivers/usb/gadget/pxa2xx_udc.c
index f01890dc8751..2c043a1ea156 100644
--- a/drivers/usb/gadget/pxa2xx_udc.c
+++ b/drivers/usb/gadget/pxa2xx_udc.c
@@ -71,7 +71,7 @@
71 * by the host to interact with this device, and allocates endpoints to 71 * by the host to interact with this device, and allocates endpoints to
72 * the different protocol interfaces. The controller driver virtualizes 72 * the different protocol interfaces. The controller driver virtualizes
73 * usb hardware so that the gadget drivers will be more portable. 73 * usb hardware so that the gadget drivers will be more portable.
74 * 74 *
75 * This UDC hardware wants to implement a bit too much USB protocol, so 75 * This UDC hardware wants to implement a bit too much USB protocol, so
76 * it constrains the sorts of USB configuration change events that work. 76 * it constrains the sorts of USB configuration change events that work.
77 * The errata for these chips are misleading; some "fixed" bugs from 77 * The errata for these chips are misleading; some "fixed" bugs from
@@ -141,7 +141,7 @@ MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
141#endif 141#endif
142 142
143/* --------------------------------------------------------------------------- 143/* ---------------------------------------------------------------------------
144 * endpoint related parts of the api to the usb controller hardware, 144 * endpoint related parts of the api to the usb controller hardware,
145 * used by gadget driver; and the inner talker-to-hardware core. 145 * used by gadget driver; and the inner talker-to-hardware core.
146 * --------------------------------------------------------------------------- 146 * ---------------------------------------------------------------------------
147 */ 147 */
@@ -293,7 +293,7 @@ static int pxa2xx_ep_enable (struct usb_ep *_ep,
293 293
294#ifdef USE_DMA 294#ifdef USE_DMA
295 /* for (some) bulk and ISO endpoints, try to get a DMA channel and 295 /* for (some) bulk and ISO endpoints, try to get a DMA channel and
296 * bind it to the endpoint. otherwise use PIO. 296 * bind it to the endpoint. otherwise use PIO.
297 */ 297 */
298 switch (ep->bmAttributes) { 298 switch (ep->bmAttributes) {
299 case USB_ENDPOINT_XFER_ISOC: 299 case USB_ENDPOINT_XFER_ISOC:
@@ -304,7 +304,7 @@ static int pxa2xx_ep_enable (struct usb_ep *_ep,
304 if (!use_dma || !ep->reg_drcmr) 304 if (!use_dma || !ep->reg_drcmr)
305 break; 305 break;
306 ep->dma = pxa_request_dma ((char *)_ep->name, 306 ep->dma = pxa_request_dma ((char *)_ep->name,
307 (le16_to_cpu (desc->wMaxPacketSize) > 64) 307 (le16_to_cpu (desc->wMaxPacketSize) > 64)
308 ? DMA_PRIO_MEDIUM /* some iso */ 308 ? DMA_PRIO_MEDIUM /* some iso */
309 : DMA_PRIO_LOW, 309 : DMA_PRIO_LOW,
310 dma_nodesc_handler, ep); 310 dma_nodesc_handler, ep);
@@ -361,7 +361,7 @@ static int pxa2xx_ep_disable (struct usb_ep *_ep)
361 */ 361 */
362 362
363/* 363/*
364 * pxa2xx_ep_alloc_request - allocate a request data structure 364 * pxa2xx_ep_alloc_request - allocate a request data structure
365 */ 365 */
366static struct usb_request * 366static struct usb_request *
367pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags) 367pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
@@ -378,7 +378,7 @@ pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
378 378
379 379
380/* 380/*
381 * pxa2xx_ep_free_request - deallocate a request data structure 381 * pxa2xx_ep_free_request - deallocate a request data structure
382 */ 382 */
383static void 383static void
384pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req) 384pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
@@ -1031,7 +1031,7 @@ pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
1031 1031
1032 1032
1033/* 1033/*
1034 * nuke - dequeue ALL requests 1034 * nuke - dequeue ALL requests
1035 */ 1035 */
1036static void nuke(struct pxa2xx_ep *ep, int status) 1036static void nuke(struct pxa2xx_ep *ep, int status)
1037{ 1037{
@@ -1136,16 +1136,16 @@ static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
1136 ep->dev->req_pending = 0; 1136 ep->dev->req_pending = 0;
1137 ep->dev->ep0state = EP0_STALL; 1137 ep->dev->ep0state = EP0_STALL;
1138 1138
1139 /* and bulk/intr endpoints like dropping stalls too */ 1139 /* and bulk/intr endpoints like dropping stalls too */
1140 } else { 1140 } else {
1141 unsigned i; 1141 unsigned i;
1142 for (i = 0; i < 1000; i += 20) { 1142 for (i = 0; i < 1000; i += 20) {
1143 if (*ep->reg_udccs & UDCCS_BI_SST) 1143 if (*ep->reg_udccs & UDCCS_BI_SST)
1144 break; 1144 break;
1145 udelay(20); 1145 udelay(20);
1146 } 1146 }
1147 } 1147 }
1148 local_irq_restore(flags); 1148 local_irq_restore(flags);
1149 1149
1150 DBG(DBG_VERBOSE, "%s halt\n", _ep->name); 1150 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
1151 return 0; 1151 return 0;
@@ -1216,7 +1216,7 @@ static struct usb_ep_ops pxa2xx_ep_ops = {
1216 1216
1217 1217
1218/* --------------------------------------------------------------------------- 1218/* ---------------------------------------------------------------------------
1219 * device-scoped parts of the api to the usb controller hardware 1219 * device-scoped parts of the api to the usb controller hardware
1220 * --------------------------------------------------------------------------- 1220 * ---------------------------------------------------------------------------
1221 */ 1221 */
1222 1222
@@ -1239,7 +1239,7 @@ static void udc_enable (struct pxa2xx_udc *);
1239static void udc_disable(struct pxa2xx_udc *); 1239static void udc_disable(struct pxa2xx_udc *);
1240 1240
1241/* We disable the UDC -- and its 48 MHz clock -- whenever it's not 1241/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
1242 * in active use. 1242 * in active use.
1243 */ 1243 */
1244static int pullup(struct pxa2xx_udc *udc, int is_active) 1244static int pullup(struct pxa2xx_udc *udc, int is_active)
1245{ 1245{
@@ -1464,24 +1464,10 @@ done:
1464 1464
1465#endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1465#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1466 1466
1467/* "function" sysfs attribute */
1468static ssize_t
1469show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1470{
1471 struct pxa2xx_udc *dev = dev_get_drvdata (_dev);
1472
1473 if (!dev->driver
1474 || !dev->driver->function
1475 || strlen (dev->driver->function) > PAGE_SIZE)
1476 return 0;
1477 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1478}
1479static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1480
1481/*-------------------------------------------------------------------------*/ 1467/*-------------------------------------------------------------------------*/
1482 1468
1483/* 1469/*
1484 * udc_disable - disable USB device controller 1470 * udc_disable - disable USB device controller
1485 */ 1471 */
1486static void udc_disable(struct pxa2xx_udc *dev) 1472static void udc_disable(struct pxa2xx_udc *dev)
1487{ 1473{
@@ -1507,7 +1493,7 @@ static void udc_disable(struct pxa2xx_udc *dev)
1507 1493
1508 1494
1509/* 1495/*
1510 * udc_reinit - initialize software state 1496 * udc_reinit - initialize software state
1511 */ 1497 */
1512static void udc_reinit(struct pxa2xx_udc *dev) 1498static void udc_reinit(struct pxa2xx_udc *dev)
1513{ 1499{
@@ -1635,18 +1621,20 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1635 dev->gadget.dev.driver = &driver->driver; 1621 dev->gadget.dev.driver = &driver->driver;
1636 dev->pullup = 1; 1622 dev->pullup = 1;
1637 1623
1638 device_add (&dev->gadget.dev); 1624 retval = device_add (&dev->gadget.dev);
1625 if (retval) {
1626fail:
1627 dev->driver = NULL;
1628 dev->gadget.dev.driver = NULL;
1629 return retval;
1630 }
1639 retval = driver->bind(&dev->gadget); 1631 retval = driver->bind(&dev->gadget);
1640 if (retval) { 1632 if (retval) {
1641 DMSG("bind to driver %s --> error %d\n", 1633 DMSG("bind to driver %s --> error %d\n",
1642 driver->driver.name, retval); 1634 driver->driver.name, retval);
1643 device_del (&dev->gadget.dev); 1635 device_del (&dev->gadget.dev);
1644 1636 goto fail;
1645 dev->driver = NULL;
1646 dev->gadget.dev.driver = NULL;
1647 return retval;
1648 } 1637 }
1649 device_create_file(dev->dev, &dev_attr_function);
1650 1638
1651 /* ... then enable host detection and ep0; and we're ready 1639 /* ... then enable host detection and ep0; and we're ready
1652 * for set_configuration as well as eventual disconnect. 1640 * for set_configuration as well as eventual disconnect.
@@ -1704,7 +1692,6 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1704 dev->driver = NULL; 1692 dev->driver = NULL;
1705 1693
1706 device_del (&dev->gadget.dev); 1694 device_del (&dev->gadget.dev);
1707 device_remove_file(dev->dev, &dev_attr_function);
1708 1695
1709 DMSG("unregistered gadget driver '%s'\n", driver->driver.name); 1696 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1710 dump_state(dev); 1697 dump_state(dev);
@@ -2474,12 +2461,12 @@ static struct pxa2xx_udc memory = {
2474#define IXP465_AD 0x00000200 2461#define IXP465_AD 0x00000200
2475 2462
2476/* 2463/*
2477 * probe - binds to the platform device 2464 * probe - binds to the platform device
2478 */ 2465 */
2479static int __init pxa2xx_udc_probe(struct platform_device *pdev) 2466static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2480{ 2467{
2481 struct pxa2xx_udc *dev = &memory; 2468 struct pxa2xx_udc *dev = &memory;
2482 int retval, out_dma = 1, vbus_irq; 2469 int retval, out_dma = 1, vbus_irq, irq;
2483 u32 chiprev; 2470 u32 chiprev;
2484 2471
2485 /* insist on Intel/ARM/XScale */ 2472 /* insist on Intel/ARM/XScale */
@@ -2522,7 +2509,11 @@ static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2522 return -ENODEV; 2509 return -ENODEV;
2523 } 2510 }
2524 2511
2525 pr_debug("%s: IRQ %d%s%s%s\n", driver_name, IRQ_USB, 2512 irq = platform_get_irq(pdev, 0);
2513 if (irq < 0)
2514 return -ENODEV;
2515
2516 pr_debug("%s: IRQ %d%s%s%s\n", driver_name, irq,
2526 dev->has_cfr ? "" : " (!cfr)", 2517 dev->has_cfr ? "" : " (!cfr)",
2527 out_dma ? "" : " (broken dma-out)", 2518 out_dma ? "" : " (broken dma-out)",
2528 SIZE_STR DMASTR 2519 SIZE_STR DMASTR
@@ -2570,11 +2561,11 @@ static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2570 dev->vbus = is_vbus_present(); 2561 dev->vbus = is_vbus_present();
2571 2562
2572 /* irq setup after old hardware state is cleaned up */ 2563 /* irq setup after old hardware state is cleaned up */
2573 retval = request_irq(IRQ_USB, pxa2xx_udc_irq, 2564 retval = request_irq(irq, pxa2xx_udc_irq,
2574 IRQF_DISABLED, driver_name, dev); 2565 IRQF_DISABLED, driver_name, dev);
2575 if (retval != 0) { 2566 if (retval != 0) {
2576 printk(KERN_ERR "%s: can't get irq %i, err %d\n", 2567 printk(KERN_ERR "%s: can't get irq %d, err %d\n",
2577 driver_name, IRQ_USB, retval); 2568 driver_name, irq, retval);
2578 return -EBUSY; 2569 return -EBUSY;
2579 } 2570 }
2580 dev->got_irq = 1; 2571 dev->got_irq = 1;
@@ -2589,7 +2580,7 @@ static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2589 printk(KERN_ERR "%s: can't get irq %i, err %d\n", 2580 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2590 driver_name, LUBBOCK_USB_DISC_IRQ, retval); 2581 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2591lubbock_fail0: 2582lubbock_fail0:
2592 free_irq(IRQ_USB, dev); 2583 free_irq(irq, dev);
2593 return -EBUSY; 2584 return -EBUSY;
2594 } 2585 }
2595 retval = request_irq(LUBBOCK_USB_IRQ, 2586 retval = request_irq(LUBBOCK_USB_IRQ,
@@ -2616,7 +2607,7 @@ lubbock_fail0:
2616 if (retval != 0) { 2607 if (retval != 0) {
2617 printk(KERN_ERR "%s: can't get irq %i, err %d\n", 2608 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2618 driver_name, vbus_irq, retval); 2609 driver_name, vbus_irq, retval);
2619 free_irq(IRQ_USB, dev); 2610 free_irq(irq, dev);
2620 return -EBUSY; 2611 return -EBUSY;
2621 } 2612 }
2622 } 2613 }
@@ -2641,7 +2632,7 @@ static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
2641 remove_proc_files(); 2632 remove_proc_files();
2642 2633
2643 if (dev->got_irq) { 2634 if (dev->got_irq) {
2644 free_irq(IRQ_USB, dev); 2635 free_irq(platform_get_irq(pdev, 0), dev);
2645 dev->got_irq = 0; 2636 dev->got_irq = 0;
2646 } 2637 }
2647#ifdef CONFIG_ARCH_LUBBOCK 2638#ifdef CONFIG_ARCH_LUBBOCK
@@ -2668,7 +2659,7 @@ static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
2668 * 2659 *
2669 * For now, we punt and forcibly disconnect from the USB host when PXA 2660 * For now, we punt and forcibly disconnect from the USB host when PXA
2670 * enters any suspend state. While we're disconnected, we always disable 2661 * enters any suspend state. While we're disconnected, we always disable
2671 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states. 2662 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2672 * Boards without software pullup control shouldn't use those states. 2663 * Boards without software pullup control shouldn't use those states.
2673 * VBUS IRQs should probably be ignored so that the PXA device just acts 2664 * VBUS IRQs should probably be ignored so that the PXA device just acts
2674 * "dead" to USB hosts until system resume. 2665 * "dead" to USB hosts until system resume.
@@ -2701,7 +2692,6 @@ static int pxa2xx_udc_resume(struct platform_device *dev)
2701/*-------------------------------------------------------------------------*/ 2692/*-------------------------------------------------------------------------*/
2702 2693
2703static struct platform_driver udc_driver = { 2694static struct platform_driver udc_driver = {
2704 .probe = pxa2xx_udc_probe,
2705 .shutdown = pxa2xx_udc_shutdown, 2695 .shutdown = pxa2xx_udc_shutdown,
2706 .remove = __exit_p(pxa2xx_udc_remove), 2696 .remove = __exit_p(pxa2xx_udc_remove),
2707 .suspend = pxa2xx_udc_suspend, 2697 .suspend = pxa2xx_udc_suspend,
@@ -2715,7 +2705,7 @@ static struct platform_driver udc_driver = {
2715static int __init udc_init(void) 2705static int __init udc_init(void)
2716{ 2706{
2717 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION); 2707 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2718 return platform_driver_register(&udc_driver); 2708 return platform_driver_probe(&udc_driver, pxa2xx_udc_probe);
2719} 2709}
2720module_init(udc_init); 2710module_init(udc_init);
2721 2711
diff --git a/drivers/usb/gadget/rndis.h b/drivers/usb/gadget/rndis.h
index 4c3c7259f019..397b149f3ca7 100644
--- a/drivers/usb/gadget/rndis.h
+++ b/drivers/usb/gadget/rndis.h
@@ -195,7 +195,7 @@ struct rndis_packet_msg_type
195 __le32 PerPacketInfoLength; 195 __le32 PerPacketInfoLength;
196 __le32 VcHandle; 196 __le32 VcHandle;
197 __le32 Reserved; 197 __le32 Reserved;
198}; 198} __attribute__ ((packed));
199 199
200struct rndis_config_parameter 200struct rndis_config_parameter
201{ 201{
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index a2e58c86849f..2ff396bd180f 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -15,4 +15,3 @@ obj-$(CONFIG_USB_UHCI_HCD) += uhci-hcd.o
15obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd.o 15obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd.o
16obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o 16obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
17obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o 17obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
18obj-$(CONFIG_ETRAX_ARCH_V10) += hc_crisv10.o
diff --git a/drivers/usb/host/ehci-fsl.h b/drivers/usb/host/ehci-fsl.h
index caac0d1967d0..f28736a917e4 100644
--- a/drivers/usb/host/ehci-fsl.h
+++ b/drivers/usb/host/ehci-fsl.h
@@ -31,7 +31,7 @@
31#define FSL_SOC_USB_SNOOP1 0x400 /* NOTE: big-endian */ 31#define FSL_SOC_USB_SNOOP1 0x400 /* NOTE: big-endian */
32#define FSL_SOC_USB_SNOOP2 0x404 /* NOTE: big-endian */ 32#define FSL_SOC_USB_SNOOP2 0x404 /* NOTE: big-endian */
33#define FSL_SOC_USB_AGECNTTHRSH 0x408 /* NOTE: big-endian */ 33#define FSL_SOC_USB_AGECNTTHRSH 0x408 /* NOTE: big-endian */
34#define FSL_SOC_USB_SICTRL 0x40c /* NOTE: big-endian */ 34#define FSL_SOC_USB_PRICTRL 0x40c /* NOTE: big-endian */
35#define FSL_SOC_USB_PRICTRL 0x410 /* NOTE: big-endian */ 35#define FSL_SOC_USB_SICTRL 0x410 /* NOTE: big-endian */
36#define FSL_SOC_USB_CTRL 0x500 /* NOTE: big-endian */ 36#define FSL_SOC_USB_CTRL 0x500 /* NOTE: big-endian */
37#endif /* _EHCI_FSL_H */ 37#endif /* _EHCI_FSL_H */
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 1813b7cac294..f4d301bc83b9 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -136,6 +136,10 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
136 /* restore CMD_RUN, framelist size, and irq threshold */ 136 /* restore CMD_RUN, framelist size, and irq threshold */
137 ehci_writel(ehci, ehci->command, &ehci->regs->command); 137 ehci_writel(ehci, ehci->command, &ehci->regs->command);
138 138
139 /* Some controller/firmware combinations need a delay during which
140 * they set up the port statuses. See Bugzilla #8190. */
141 mdelay(8);
142
139 /* manually resume the ports we suspended during bus_suspend() */ 143 /* manually resume the ports we suspended during bus_suspend() */
140 i = HCS_N_PORTS (ehci->hcs_params); 144 i = HCS_N_PORTS (ehci->hcs_params);
141 while (i--) { 145 while (i--) {
diff --git a/drivers/usb/host/hc_crisv10.c b/drivers/usb/host/hc_crisv10.c
deleted file mode 100644
index 32f7caf24747..000000000000
--- a/drivers/usb/host/hc_crisv10.c
+++ /dev/null
@@ -1,4550 +0,0 @@
1/*
2 * usb-host.c: ETRAX 100LX USB Host Controller Driver (HCD)
3 *
4 * Copyright (c) 2002, 2003 Axis Communications AB.
5 */
6
7#include <linux/kernel.h>
8#include <linux/delay.h>
9#include <linux/ioport.h>
10#include <linux/slab.h>
11#include <linux/errno.h>
12#include <linux/unistd.h>
13#include <linux/interrupt.h>
14#include <linux/init.h>
15#include <linux/list.h>
16#include <linux/spinlock.h>
17
18#include <asm/uaccess.h>
19#include <asm/io.h>
20#include <asm/irq.h>
21#include <asm/dma.h>
22#include <asm/system.h>
23#include <asm/arch/svinto.h>
24
25#include <linux/usb.h>
26/* Ugly include because we don't live with the other host drivers. */
27#include <../drivers/usb/core/hcd.h>
28#include <../drivers/usb/core/usb.h>
29
30#include "hc_crisv10.h"
31
32#define ETRAX_USB_HC_IRQ USB_HC_IRQ_NBR
33#define ETRAX_USB_RX_IRQ USB_DMA_RX_IRQ_NBR
34#define ETRAX_USB_TX_IRQ USB_DMA_TX_IRQ_NBR
35
36static const char *usb_hcd_version = "$Revision: 1.2 $";
37
38#undef KERN_DEBUG
39#define KERN_DEBUG ""
40
41
42#undef USB_DEBUG_RH
43#undef USB_DEBUG_EPID
44#undef USB_DEBUG_SB
45#undef USB_DEBUG_DESC
46#undef USB_DEBUG_URB
47#undef USB_DEBUG_TRACE
48#undef USB_DEBUG_BULK
49#undef USB_DEBUG_CTRL
50#undef USB_DEBUG_INTR
51#undef USB_DEBUG_ISOC
52
53#ifdef USB_DEBUG_RH
54#define dbg_rh(format, arg...) printk(KERN_DEBUG __FILE__ ": (RH) " format "\n" , ## arg)
55#else
56#define dbg_rh(format, arg...) do {} while (0)
57#endif
58
59#ifdef USB_DEBUG_EPID
60#define dbg_epid(format, arg...) printk(KERN_DEBUG __FILE__ ": (EPID) " format "\n" , ## arg)
61#else
62#define dbg_epid(format, arg...) do {} while (0)
63#endif
64
65#ifdef USB_DEBUG_SB
66#define dbg_sb(format, arg...) printk(KERN_DEBUG __FILE__ ": (SB) " format "\n" , ## arg)
67#else
68#define dbg_sb(format, arg...) do {} while (0)
69#endif
70
71#ifdef USB_DEBUG_CTRL
72#define dbg_ctrl(format, arg...) printk(KERN_DEBUG __FILE__ ": (CTRL) " format "\n" , ## arg)
73#else
74#define dbg_ctrl(format, arg...) do {} while (0)
75#endif
76
77#ifdef USB_DEBUG_BULK
78#define dbg_bulk(format, arg...) printk(KERN_DEBUG __FILE__ ": (BULK) " format "\n" , ## arg)
79#else
80#define dbg_bulk(format, arg...) do {} while (0)
81#endif
82
83#ifdef USB_DEBUG_INTR
84#define dbg_intr(format, arg...) printk(KERN_DEBUG __FILE__ ": (INTR) " format "\n" , ## arg)
85#else
86#define dbg_intr(format, arg...) do {} while (0)
87#endif
88
89#ifdef USB_DEBUG_ISOC
90#define dbg_isoc(format, arg...) printk(KERN_DEBUG __FILE__ ": (ISOC) " format "\n" , ## arg)
91#else
92#define dbg_isoc(format, arg...) do {} while (0)
93#endif
94
95#ifdef USB_DEBUG_TRACE
96#define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
97#define DBFEXIT (printk(": Exiting: %s\n", __FUNCTION__))
98#else
99#define DBFENTER do {} while (0)
100#define DBFEXIT do {} while (0)
101#endif
102
103#define usb_pipeslow(pipe) (((pipe) >> 26) & 1)
104
105/*-------------------------------------------------------------------
106 Virtual Root Hub
107 -------------------------------------------------------------------*/
108
109static __u8 root_hub_dev_des[] =
110{
111 0x12, /* __u8 bLength; */
112 0x01, /* __u8 bDescriptorType; Device */
113 0x00, /* __le16 bcdUSB; v1.0 */
114 0x01,
115 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
116 0x00, /* __u8 bDeviceSubClass; */
117 0x00, /* __u8 bDeviceProtocol; */
118 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
119 0x00, /* __le16 idVendor; */
120 0x00,
121 0x00, /* __le16 idProduct; */
122 0x00,
123 0x00, /* __le16 bcdDevice; */
124 0x00,
125 0x00, /* __u8 iManufacturer; */
126 0x02, /* __u8 iProduct; */
127 0x01, /* __u8 iSerialNumber; */
128 0x01 /* __u8 bNumConfigurations; */
129};
130
131/* Configuration descriptor */
132static __u8 root_hub_config_des[] =
133{
134 0x09, /* __u8 bLength; */
135 0x02, /* __u8 bDescriptorType; Configuration */
136 0x19, /* __le16 wTotalLength; */
137 0x00,
138 0x01, /* __u8 bNumInterfaces; */
139 0x01, /* __u8 bConfigurationValue; */
140 0x00, /* __u8 iConfiguration; */
141 0x40, /* __u8 bmAttributes; Bit 7: Bus-powered */
142 0x00, /* __u8 MaxPower; */
143
144 /* interface */
145 0x09, /* __u8 if_bLength; */
146 0x04, /* __u8 if_bDescriptorType; Interface */
147 0x00, /* __u8 if_bInterfaceNumber; */
148 0x00, /* __u8 if_bAlternateSetting; */
149 0x01, /* __u8 if_bNumEndpoints; */
150 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
151 0x00, /* __u8 if_bInterfaceSubClass; */
152 0x00, /* __u8 if_bInterfaceProtocol; */
153 0x00, /* __u8 if_iInterface; */
154
155 /* endpoint */
156 0x07, /* __u8 ep_bLength; */
157 0x05, /* __u8 ep_bDescriptorType; Endpoint */
158 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
159 0x03, /* __u8 ep_bmAttributes; Interrupt */
160 0x08, /* __le16 ep_wMaxPacketSize; 8 Bytes */
161 0x00,
162 0xff /* __u8 ep_bInterval; 255 ms */
163};
164
165static __u8 root_hub_hub_des[] =
166{
167 0x09, /* __u8 bLength; */
168 0x29, /* __u8 bDescriptorType; Hub-descriptor */
169 0x02, /* __u8 bNbrPorts; */
170 0x00, /* __u16 wHubCharacteristics; */
171 0x00,
172 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
173 0x00, /* __u8 bHubContrCurrent; 0 mA */
174 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
175 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
176};
177
178static DEFINE_TIMER(bulk_start_timer, NULL, 0, 0);
179static DEFINE_TIMER(bulk_eot_timer, NULL, 0, 0);
180
181/* We want the start timer to expire before the eot timer, because the former might start
182 traffic, thus making it unnecessary for the latter to time out. */
183#define BULK_START_TIMER_INTERVAL (HZ/10) /* 100 ms */
184#define BULK_EOT_TIMER_INTERVAL (HZ/10+2) /* 120 ms */
185
186#define OK(x) len = (x); dbg_rh("OK(%d): line: %d", x, __LINE__); break
187#define CHECK_ALIGN(x) if (((__u32)(x)) & 0x00000003) \
188{panic("Alignment check (DWORD) failed at %s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__);}
189
190#define SLAB_FLAG (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
191#define KMALLOC_FLAG (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
192
193/* Most helpful debugging aid */
194#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
195
196/* Alternative assert define which stops after a failed assert. */
197/*
198#define assert(expr) \
199{ \
200 if (!(expr)) { \
201 err("assert failed at line %d",__LINE__); \
202 while (1); \
203 } \
204}
205*/
206
207
208/* FIXME: Should RX_BUF_SIZE be a config option, or maybe we should adjust it dynamically?
209 To adjust it dynamically we would have to get an interrupt when we reach the end
210 of the rx descriptor list, or when we get close to the end, and then allocate more
211 descriptors. */
212
213#define NBR_OF_RX_DESC 512
214#define RX_DESC_BUF_SIZE 1024
215#define RX_BUF_SIZE (NBR_OF_RX_DESC * RX_DESC_BUF_SIZE)
216
217/* The number of epids is, among other things, used for pre-allocating
218 ctrl, bulk and isoc EP descriptors (one for each epid).
219 Assumed to be > 1 when initiating the DMA lists. */
220#define NBR_OF_EPIDS 32
221
222/* Support interrupt traffic intervals up to 128 ms. */
223#define MAX_INTR_INTERVAL 128
224
225/* If periodic traffic (intr or isoc) is to be used, then one entry in the EP table
226 must be "invalid". By this we mean that we shouldn't care about epid attentions
227 for this epid, or at least handle them differently from epid attentions for "valid"
228 epids. This define determines which one to use (don't change it). */
229#define INVALID_EPID 31
230/* A special epid for the bulk dummys. */
231#define DUMMY_EPID 30
232
233/* This is just a software cache for the valid entries in R_USB_EPT_DATA. */
234static __u32 epid_usage_bitmask;
235
236/* A bitfield to keep information on in/out traffic is needed to uniquely identify
237 an endpoint on a device, since the most significant bit which indicates traffic
238 direction is lacking in the ep_id field (ETRAX epids can handle both in and
239 out traffic on endpoints that are otherwise identical). The USB framework, however,
240 relies on them to be handled separately. For example, bulk IN and OUT urbs cannot
241 be queued in the same list, since they would block each other. */
242static __u32 epid_out_traffic;
243
244/* DMA IN cache bug. Align the DMA IN buffers to 32 bytes, i.e. a cache line.
245 Since RX_DESC_BUF_SIZE is 1024 is a multiple of 32, all rx buffers will be cache aligned. */
246static volatile unsigned char RxBuf[RX_BUF_SIZE] __attribute__ ((aligned (32)));
247static volatile USB_IN_Desc_t RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned (4)));
248
249/* Pointers into RxDescList. */
250static volatile USB_IN_Desc_t *myNextRxDesc;
251static volatile USB_IN_Desc_t *myLastRxDesc;
252static volatile USB_IN_Desc_t *myPrevRxDesc;
253
254/* EP descriptors must be 32-bit aligned. */
255static volatile USB_EP_Desc_t TxCtrlEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
256static volatile USB_EP_Desc_t TxBulkEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
257/* After each enabled bulk EP (IN or OUT) we put two disabled EP descriptors with the eol flag set,
258 causing the DMA to stop the DMA channel. The first of these two has the intr flag set, which
259 gives us a dma8_sub0_descr interrupt. When we receive this, we advance the DMA one step in the
260 EP list and then restart the bulk channel, thus forcing a switch between bulk EP descriptors
261 in each frame. */
262static volatile USB_EP_Desc_t TxBulkDummyEPList[NBR_OF_EPIDS][2] __attribute__ ((aligned (4)));
263
264static volatile USB_EP_Desc_t TxIsocEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
265static volatile USB_SB_Desc_t TxIsocSB_zout __attribute__ ((aligned (4)));
266
267static volatile USB_EP_Desc_t TxIntrEPList[MAX_INTR_INTERVAL] __attribute__ ((aligned (4)));
268static volatile USB_SB_Desc_t TxIntrSB_zout __attribute__ ((aligned (4)));
269
270/* A zout transfer makes a memory access at the address of its buf pointer, which means that setting
271 this buf pointer to 0 will cause an access to the flash. In addition to this, setting sw_len to 0
272 results in a 16/32 bytes (depending on DMA burst size) transfer. Instead, we set it to 1, and point
273 it to this buffer. */
274static int zout_buffer[4] __attribute__ ((aligned (4)));
275
276/* Cache for allocating new EP and SB descriptors. */
277static struct kmem_cache *usb_desc_cache;
278
279/* Cache for the registers allocated in the top half. */
280static struct kmem_cache *top_half_reg_cache;
281
282/* Cache for the data allocated in the isoc descr top half. */
283static struct kmem_cache *isoc_compl_cache;
284
285static struct usb_bus *etrax_usb_bus;
286
287/* This is a circular (double-linked) list of the active urbs for each epid.
288 The head is never removed, and new urbs are linked onto the list as
289 urb_entry_t elements. Don't reference urb_list directly; use the wrapper
290 functions instead. Note that working with these lists might require spinlock
291 protection. */
292static struct list_head urb_list[NBR_OF_EPIDS];
293
294/* Read about the need and usage of this lock in submit_ctrl_urb. */
295static spinlock_t urb_list_lock;
296
297/* Used when unlinking asynchronously. */
298static struct list_head urb_unlink_list;
299
300/* for returning string descriptors in UTF-16LE */
301static int ascii2utf (char *ascii, __u8 *utf, int utfmax)
302{
303 int retval;
304
305 for (retval = 0; *ascii && utfmax > 1; utfmax -= 2, retval += 2) {
306 *utf++ = *ascii++ & 0x7f;
307 *utf++ = 0;
308 }
309 return retval;
310}
311
312static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
313{
314 char buf [30];
315
316 // assert (len > (2 * (sizeof (buf) + 1)));
317 // assert (strlen (type) <= 8);
318
319 // language ids
320 if (id == 0) {
321 *data++ = 4; *data++ = 3; /* 4 bytes data */
322 *data++ = 0; *data++ = 0; /* some language id */
323 return 4;
324
325 // serial number
326 } else if (id == 1) {
327 sprintf (buf, "%x", serial);
328
329 // product description
330 } else if (id == 2) {
331 sprintf (buf, "USB %s Root Hub", type);
332
333 // id 3 == vendor description
334
335 // unsupported IDs --> "stall"
336 } else
337 return 0;
338
339 data [0] = 2 + ascii2utf (buf, data + 2, len - 2);
340 data [1] = 3;
341 return data [0];
342}
343
344/* Wrappers around the list functions (include/linux/list.h). */
345
346static inline int urb_list_empty(int epid)
347{
348 return list_empty(&urb_list[epid]);
349}
350
351/* Returns first urb for this epid, or NULL if list is empty. */
352static inline struct urb *urb_list_first(int epid)
353{
354 struct urb *first_urb = 0;
355
356 if (!urb_list_empty(epid)) {
357 /* Get the first urb (i.e. head->next). */
358 urb_entry_t *urb_entry = list_entry((&urb_list[epid])->next, urb_entry_t, list);
359 first_urb = urb_entry->urb;
360 }
361 return first_urb;
362}
363
364/* Adds an urb_entry last in the list for this epid. */
365static inline void urb_list_add(struct urb *urb, int epid)
366{
367 urb_entry_t *urb_entry = kmalloc(sizeof(urb_entry_t), KMALLOC_FLAG);
368 assert(urb_entry);
369
370 urb_entry->urb = urb;
371 list_add_tail(&urb_entry->list, &urb_list[epid]);
372}
373
374/* Search through the list for an element that contains this urb. (The list
375 is expected to be short and the one we are about to delete will often be
376 the first in the list.) */
377static inline urb_entry_t *__urb_list_entry(struct urb *urb, int epid)
378{
379 struct list_head *entry;
380 struct list_head *tmp;
381 urb_entry_t *urb_entry;
382
383 list_for_each_safe(entry, tmp, &urb_list[epid]) {
384 urb_entry = list_entry(entry, urb_entry_t, list);
385 assert(urb_entry);
386 assert(urb_entry->urb);
387
388 if (urb_entry->urb == urb) {
389 return urb_entry;
390 }
391 }
392 return 0;
393}
394
395/* Delete an urb from the list. */
396static inline void urb_list_del(struct urb *urb, int epid)
397{
398 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
399 assert(urb_entry);
400
401 /* Delete entry and free. */
402 list_del(&urb_entry->list);
403 kfree(urb_entry);
404}
405
406/* Move an urb to the end of the list. */
407static inline void urb_list_move_last(struct urb *urb, int epid)
408{
409 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
410 assert(urb_entry);
411
412 list_move_tail(&urb_entry->list, &urb_list[epid]);
413}
414
415/* Get the next urb in the list. */
416static inline struct urb *urb_list_next(struct urb *urb, int epid)
417{
418 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
419
420 assert(urb_entry);
421
422 if (urb_entry->list.next != &urb_list[epid]) {
423 struct list_head *elem = urb_entry->list.next;
424 urb_entry = list_entry(elem, urb_entry_t, list);
425 return urb_entry->urb;
426 } else {
427 return NULL;
428 }
429}
430
431
432
433/* For debug purposes only. */
434static inline void urb_list_dump(int epid)
435{
436 struct list_head *entry;
437 struct list_head *tmp;
438 urb_entry_t *urb_entry;
439 int i = 0;
440
441 info("Dumping urb list for epid %d", epid);
442
443 list_for_each_safe(entry, tmp, &urb_list[epid]) {
444 urb_entry = list_entry(entry, urb_entry_t, list);
445 info(" entry %d, urb = 0x%lx", i, (unsigned long)urb_entry->urb);
446 }
447}
448
449static void init_rx_buffers(void);
450static int etrax_rh_unlink_urb(struct urb *urb);
451static void etrax_rh_send_irq(struct urb *urb);
452static void etrax_rh_init_int_timer(struct urb *urb);
453static void etrax_rh_int_timer_do(unsigned long ptr);
454
455static int etrax_usb_setup_epid(struct urb *urb);
456static int etrax_usb_lookup_epid(struct urb *urb);
457static int etrax_usb_allocate_epid(void);
458static void etrax_usb_free_epid(int epid);
459
460static int etrax_remove_from_sb_list(struct urb *urb);
461
462static void* etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size,
463 unsigned mem_flags, dma_addr_t *dma);
464static void etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma);
465
466static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid);
467static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid);
468static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid);
469static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid);
470
471static int etrax_usb_submit_bulk_urb(struct urb *urb);
472static int etrax_usb_submit_ctrl_urb(struct urb *urb);
473static int etrax_usb_submit_intr_urb(struct urb *urb);
474static int etrax_usb_submit_isoc_urb(struct urb *urb);
475
476static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags);
477static int etrax_usb_unlink_urb(struct urb *urb, int status);
478static int etrax_usb_get_frame_number(struct usb_device *usb_dev);
479
480static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc);
481static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc);
482static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc);
483static void etrax_usb_hc_interrupt_bottom_half(void *data);
484
485static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data);
486
487
488/* The following is a list of interrupt handlers for the host controller interrupts we use.
489 They are called from etrax_usb_hc_interrupt_bottom_half. */
490static void etrax_usb_hc_isoc_eof_interrupt(void);
491static void etrax_usb_hc_bulk_eot_interrupt(int timer_induced);
492static void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg);
493static void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg);
494static void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg);
495
496static int etrax_rh_submit_urb (struct urb *urb);
497
498/* Forward declaration needed because they are used in the rx interrupt routine. */
499static void etrax_usb_complete_urb(struct urb *urb, int status);
500static void etrax_usb_complete_bulk_urb(struct urb *urb, int status);
501static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status);
502static void etrax_usb_complete_intr_urb(struct urb *urb, int status);
503static void etrax_usb_complete_isoc_urb(struct urb *urb, int status);
504
505static int etrax_usb_hc_init(void);
506static void etrax_usb_hc_cleanup(void);
507
508static struct usb_operations etrax_usb_device_operations =
509{
510 .get_frame_number = etrax_usb_get_frame_number,
511 .submit_urb = etrax_usb_submit_urb,
512 .unlink_urb = etrax_usb_unlink_urb,
513 .buffer_alloc = etrax_usb_buffer_alloc,
514 .buffer_free = etrax_usb_buffer_free
515};
516
517/* Note that these functions are always available in their "__" variants, for use in
518 error situations. The "__" missing variants are controlled by the USB_DEBUG_DESC/
519 USB_DEBUG_URB macros. */
520static void __dump_urb(struct urb* purb)
521{
522 printk("\nurb :0x%08lx\n", (unsigned long)purb);
523 printk("dev :0x%08lx\n", (unsigned long)purb->dev);
524 printk("pipe :0x%08x\n", purb->pipe);
525 printk("status :%d\n", purb->status);
526 printk("transfer_flags :0x%08x\n", purb->transfer_flags);
527 printk("transfer_buffer :0x%08lx\n", (unsigned long)purb->transfer_buffer);
528 printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
529 printk("actual_length :%d\n", purb->actual_length);
530 printk("setup_packet :0x%08lx\n", (unsigned long)purb->setup_packet);
531 printk("start_frame :%d\n", purb->start_frame);
532 printk("number_of_packets :%d\n", purb->number_of_packets);
533 printk("interval :%d\n", purb->interval);
534 printk("error_count :%d\n", purb->error_count);
535 printk("context :0x%08lx\n", (unsigned long)purb->context);
536 printk("complete :0x%08lx\n\n", (unsigned long)purb->complete);
537}
538
539static void __dump_in_desc(volatile USB_IN_Desc_t *in)
540{
541 printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
542 printk(" sw_len : 0x%04x (%d)\n", in->sw_len, in->sw_len);
543 printk(" command : 0x%04x\n", in->command);
544 printk(" next : 0x%08lx\n", in->next);
545 printk(" buf : 0x%08lx\n", in->buf);
546 printk(" hw_len : 0x%04x (%d)\n", in->hw_len, in->hw_len);
547 printk(" status : 0x%04x\n\n", in->status);
548}
549
550static void __dump_sb_desc(volatile USB_SB_Desc_t *sb)
551{
552 char tt = (sb->command & 0x30) >> 4;
553 char *tt_string;
554
555 switch (tt) {
556 case 0:
557 tt_string = "zout";
558 break;
559 case 1:
560 tt_string = "in";
561 break;
562 case 2:
563 tt_string = "out";
564 break;
565 case 3:
566 tt_string = "setup";
567 break;
568 default:
569 tt_string = "unknown (weird)";
570 }
571
572 printk("\n USB_SB_Desc at 0x%08lx\n", (unsigned long)sb);
573 printk(" command : 0x%04x\n", sb->command);
574 printk(" rem : %d\n", (sb->command & 0x3f00) >> 8);
575 printk(" full : %d\n", (sb->command & 0x40) >> 6);
576 printk(" tt : %d (%s)\n", tt, tt_string);
577 printk(" intr : %d\n", (sb->command & 0x8) >> 3);
578 printk(" eot : %d\n", (sb->command & 0x2) >> 1);
579 printk(" eol : %d\n", sb->command & 0x1);
580 printk(" sw_len : 0x%04x (%d)\n", sb->sw_len, sb->sw_len);
581 printk(" next : 0x%08lx\n", sb->next);
582 printk(" buf : 0x%08lx\n\n", sb->buf);
583}
584
585
586static void __dump_ep_desc(volatile USB_EP_Desc_t *ep)
587{
588 printk("\nUSB_EP_Desc at 0x%08lx\n", (unsigned long)ep);
589 printk(" command : 0x%04x\n", ep->command);
590 printk(" ep_id : %d\n", (ep->command & 0x1f00) >> 8);
591 printk(" enable : %d\n", (ep->command & 0x10) >> 4);
592 printk(" intr : %d\n", (ep->command & 0x8) >> 3);
593 printk(" eof : %d\n", (ep->command & 0x2) >> 1);
594 printk(" eol : %d\n", ep->command & 0x1);
595 printk(" hw_len : 0x%04x (%d)\n", ep->hw_len, ep->hw_len);
596 printk(" next : 0x%08lx\n", ep->next);
597 printk(" sub : 0x%08lx\n\n", ep->sub);
598}
599
600static inline void __dump_ep_list(int pipe_type)
601{
602 volatile USB_EP_Desc_t *ep;
603 volatile USB_EP_Desc_t *first_ep;
604 volatile USB_SB_Desc_t *sb;
605
606 switch (pipe_type)
607 {
608 case PIPE_BULK:
609 first_ep = &TxBulkEPList[0];
610 break;
611 case PIPE_CONTROL:
612 first_ep = &TxCtrlEPList[0];
613 break;
614 case PIPE_INTERRUPT:
615 first_ep = &TxIntrEPList[0];
616 break;
617 case PIPE_ISOCHRONOUS:
618 first_ep = &TxIsocEPList[0];
619 break;
620 default:
621 warn("Cannot dump unknown traffic type");
622 return;
623 }
624 ep = first_ep;
625
626 printk("\n\nDumping EP list...\n\n");
627
628 do {
629 __dump_ep_desc(ep);
630 /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
631 sb = ep->sub ? phys_to_virt(ep->sub) : 0;
632 while (sb) {
633 __dump_sb_desc(sb);
634 sb = sb->next ? phys_to_virt(sb->next) : 0;
635 }
636 ep = (volatile USB_EP_Desc_t *)(phys_to_virt(ep->next));
637
638 } while (ep != first_ep);
639}
640
641static inline void __dump_ept_data(int epid)
642{
643 unsigned long flags;
644 __u32 r_usb_ept_data;
645
646 if (epid < 0 || epid > 31) {
647 printk("Cannot dump ept data for invalid epid %d\n", epid);
648 return;
649 }
650
651 save_flags(flags);
652 cli();
653 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
654 nop();
655 r_usb_ept_data = *R_USB_EPT_DATA;
656 restore_flags(flags);
657
658 printk("\nR_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
659 if (r_usb_ept_data == 0) {
660 /* No need for more detailed printing. */
661 return;
662 }
663 printk(" valid : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
664 printk(" hold : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
665 printk(" error_count_in : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
666 printk(" t_in : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
667 printk(" low_speed : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
668 printk(" port : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
669 printk(" error_code : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
670 printk(" t_out : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
671 printk(" error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
672 printk(" max_len : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
673 printk(" ep : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
674 printk(" dev : %d\n", (r_usb_ept_data & 0x0000003f));
675}
676
677static inline void __dump_ept_data_list(void)
678{
679 int i;
680
681 printk("Dumping the whole R_USB_EPT_DATA list\n");
682
683 for (i = 0; i < 32; i++) {
684 __dump_ept_data(i);
685 }
686}
687#ifdef USB_DEBUG_DESC
688#define dump_in_desc(...) __dump_in_desc(...)
689#define dump_sb_desc(...) __dump_sb_desc(...)
690#define dump_ep_desc(...) __dump_ep_desc(...)
691#else
692#define dump_in_desc(...) do {} while (0)
693#define dump_sb_desc(...) do {} while (0)
694#define dump_ep_desc(...) do {} while (0)
695#endif
696
697#ifdef USB_DEBUG_URB
698#define dump_urb(x) __dump_urb(x)
699#else
700#define dump_urb(x) do {} while (0)
701#endif
702
703static void init_rx_buffers(void)
704{
705 int i;
706
707 DBFENTER;
708
709 for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
710 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
711 RxDescList[i].command = 0;
712 RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
713 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
714 RxDescList[i].hw_len = 0;
715 RxDescList[i].status = 0;
716
717 /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as USB_IN_Desc
718 for the relevant fields.) */
719 prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
720
721 }
722
723 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
724 RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
725 RxDescList[i].next = virt_to_phys(&RxDescList[0]);
726 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
727 RxDescList[i].hw_len = 0;
728 RxDescList[i].status = 0;
729
730 myNextRxDesc = &RxDescList[0];
731 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
732 myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
733
734 *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
735 *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
736
737 DBFEXIT;
738}
739
740static void init_tx_bulk_ep(void)
741{
742 int i;
743
744 DBFENTER;
745
746 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
747 CHECK_ALIGN(&TxBulkEPList[i]);
748 TxBulkEPList[i].hw_len = 0;
749 TxBulkEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
750 TxBulkEPList[i].sub = 0;
751 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[i + 1]);
752
753 /* Initiate two EPs, disabled and with the eol flag set. No need for any
754 preserved epid. */
755
756 /* The first one has the intr flag set so we get an interrupt when the DMA
757 channel is about to become disabled. */
758 CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
759 TxBulkDummyEPList[i][0].hw_len = 0;
760 TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
761 IO_STATE(USB_EP_command, eol, yes) |
762 IO_STATE(USB_EP_command, intr, yes));
763 TxBulkDummyEPList[i][0].sub = 0;
764 TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
765
766 /* The second one. */
767 CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
768 TxBulkDummyEPList[i][1].hw_len = 0;
769 TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
770 IO_STATE(USB_EP_command, eol, yes));
771 TxBulkDummyEPList[i][1].sub = 0;
772 /* The last dummy's next pointer is the same as the current EP's next pointer. */
773 TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
774 }
775
776 /* Configure the last one. */
777 CHECK_ALIGN(&TxBulkEPList[i]);
778 TxBulkEPList[i].hw_len = 0;
779 TxBulkEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
780 IO_FIELD(USB_EP_command, epid, i));
781 TxBulkEPList[i].sub = 0;
782 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[0]);
783
784 /* No need configuring dummy EPs for the last one as it will never be used for
785 bulk traffic (i == INVALD_EPID at this point). */
786
787 /* Set up to start on the last EP so we will enable it when inserting traffic
788 for the first time (imitating the situation where the DMA has stopped
789 because there was no more traffic). */
790 *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
791 /* No point in starting the bulk channel yet.
792 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
793 DBFEXIT;
794}
795
796static void init_tx_ctrl_ep(void)
797{
798 int i;
799
800 DBFENTER;
801
802 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
803 CHECK_ALIGN(&TxCtrlEPList[i]);
804 TxCtrlEPList[i].hw_len = 0;
805 TxCtrlEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
806 TxCtrlEPList[i].sub = 0;
807 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[i + 1]);
808 }
809
810 CHECK_ALIGN(&TxCtrlEPList[i]);
811 TxCtrlEPList[i].hw_len = 0;
812 TxCtrlEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
813 IO_FIELD(USB_EP_command, epid, i));
814
815 TxCtrlEPList[i].sub = 0;
816 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[0]);
817
818 *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[0]);
819 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
820
821 DBFEXIT;
822}
823
824
825static void init_tx_intr_ep(void)
826{
827 int i;
828
829 DBFENTER;
830
831 /* Read comment at zout_buffer declaration for an explanation to this. */
832 TxIntrSB_zout.sw_len = 1;
833 TxIntrSB_zout.next = 0;
834 TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
835 TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
836 IO_STATE(USB_SB_command, tt, zout) |
837 IO_STATE(USB_SB_command, full, yes) |
838 IO_STATE(USB_SB_command, eot, yes) |
839 IO_STATE(USB_SB_command, eol, yes));
840
841 for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
842 CHECK_ALIGN(&TxIntrEPList[i]);
843 TxIntrEPList[i].hw_len = 0;
844 TxIntrEPList[i].command =
845 (IO_STATE(USB_EP_command, eof, yes) |
846 IO_STATE(USB_EP_command, enable, yes) |
847 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
848 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
849 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
850 }
851
852 CHECK_ALIGN(&TxIntrEPList[i]);
853 TxIntrEPList[i].hw_len = 0;
854 TxIntrEPList[i].command =
855 (IO_STATE(USB_EP_command, eof, yes) |
856 IO_STATE(USB_EP_command, eol, yes) |
857 IO_STATE(USB_EP_command, enable, yes) |
858 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
859 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
860 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
861
862 *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
863 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
864 DBFEXIT;
865}
866
867static void init_tx_isoc_ep(void)
868{
869 int i;
870
871 DBFENTER;
872
873 /* Read comment at zout_buffer declaration for an explanation to this. */
874 TxIsocSB_zout.sw_len = 1;
875 TxIsocSB_zout.next = 0;
876 TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
877 TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
878 IO_STATE(USB_SB_command, tt, zout) |
879 IO_STATE(USB_SB_command, full, yes) |
880 IO_STATE(USB_SB_command, eot, yes) |
881 IO_STATE(USB_SB_command, eol, yes));
882
883 /* The last isochronous EP descriptor is a dummy. */
884
885 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
886 CHECK_ALIGN(&TxIsocEPList[i]);
887 TxIsocEPList[i].hw_len = 0;
888 TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
889 TxIsocEPList[i].sub = 0;
890 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
891 }
892
893 CHECK_ALIGN(&TxIsocEPList[i]);
894 TxIsocEPList[i].hw_len = 0;
895
896 /* Must enable the last EP descr to get eof interrupt. */
897 TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
898 IO_STATE(USB_EP_command, eof, yes) |
899 IO_STATE(USB_EP_command, eol, yes) |
900 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
901 TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
902 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
903
904 *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
905 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
906
907 DBFEXIT;
908}
909
910static void etrax_usb_unlink_intr_urb(struct urb *urb)
911{
912 volatile USB_EP_Desc_t *first_ep; /* First EP in the list. */
913 volatile USB_EP_Desc_t *curr_ep; /* Current EP, the iterator. */
914 volatile USB_EP_Desc_t *next_ep; /* The EP after current. */
915 volatile USB_EP_Desc_t *unlink_ep; /* The one we should remove from the list. */
916
917 int epid;
918
919 /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the List". */
920
921 DBFENTER;
922
923 epid = ((etrax_urb_priv_t *)urb->hcpriv)->epid;
924
925 first_ep = &TxIntrEPList[0];
926 curr_ep = first_ep;
927
928
929 /* Note that this loop removes all EP descriptors with this epid. This assumes
930 that all EP descriptors belong to the one and only urb for this epid. */
931
932 do {
933 next_ep = (USB_EP_Desc_t *)phys_to_virt(curr_ep->next);
934
935 if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
936
937 dbg_intr("Found EP to unlink for epid %d", epid);
938
939 /* This is the one we should unlink. */
940 unlink_ep = next_ep;
941
942 /* Actually unlink the EP from the DMA list. */
943 curr_ep->next = unlink_ep->next;
944
945 /* Wait until the DMA is no longer at this descriptor. */
946 while (*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep));
947
948 /* Now we are free to remove it and its SB descriptor.
949 Note that it is assumed here that there is only one sb in the
950 sb list for this ep. */
951 kmem_cache_free(usb_desc_cache, phys_to_virt(unlink_ep->sub));
952 kmem_cache_free(usb_desc_cache, (USB_EP_Desc_t *)unlink_ep);
953 }
954
955 curr_ep = phys_to_virt(curr_ep->next);
956
957 } while (curr_ep != first_ep);
958 urb->hcpriv = NULL;
959}
960
961void etrax_usb_do_intr_recover(int epid)
962{
963 USB_EP_Desc_t *first_ep, *tmp_ep;
964
965 DBFENTER;
966
967 first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
968 tmp_ep = first_ep;
969
970 /* What this does is simply to walk the list of interrupt
971 ep descriptors and enable those that are disabled. */
972
973 do {
974 if (IO_EXTRACT(USB_EP_command, epid, tmp_ep->command) == epid &&
975 !(tmp_ep->command & IO_MASK(USB_EP_command, enable))) {
976 tmp_ep->command |= IO_STATE(USB_EP_command, enable, yes);
977 }
978
979 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
980
981 } while (tmp_ep != first_ep);
982
983
984 DBFEXIT;
985}
986
987static int etrax_rh_unlink_urb (struct urb *urb)
988{
989 etrax_hc_t *hc;
990
991 DBFENTER;
992
993 hc = urb->dev->bus->hcpriv;
994
995 if (hc->rh.urb == urb) {
996 hc->rh.send = 0;
997 del_timer(&hc->rh.rh_int_timer);
998 }
999
1000 DBFEXIT;
1001 return 0;
1002}
1003
1004static void etrax_rh_send_irq(struct urb *urb)
1005{
1006 __u16 data = 0;
1007 etrax_hc_t *hc = urb->dev->bus->hcpriv;
1008 DBFENTER;
1009
1010/*
1011 dbg_rh("R_USB_FM_NUMBER : 0x%08X", *R_USB_FM_NUMBER);
1012 dbg_rh("R_USB_FM_REMAINING: 0x%08X", *R_USB_FM_REMAINING);
1013*/
1014
1015 data |= (hc->rh.wPortChange_1) ? (1 << 1) : 0;
1016 data |= (hc->rh.wPortChange_2) ? (1 << 2) : 0;
1017
1018 *((__u16 *)urb->transfer_buffer) = cpu_to_le16(data);
1019 /* FIXME: Why is actual_length set to 1 when data is 2 bytes?
1020 Since only 1 byte is used, why not declare data as __u8? */
1021 urb->actual_length = 1;
1022 urb->status = 0;
1023
1024 if (hc->rh.send && urb->complete) {
1025 dbg_rh("wPortChange_1: 0x%04X", hc->rh.wPortChange_1);
1026 dbg_rh("wPortChange_2: 0x%04X", hc->rh.wPortChange_2);
1027
1028 urb->complete(urb, NULL);
1029 }
1030
1031 DBFEXIT;
1032}
1033
1034static void etrax_rh_init_int_timer(struct urb *urb)
1035{
1036 etrax_hc_t *hc;
1037
1038 DBFENTER;
1039
1040 hc = urb->dev->bus->hcpriv;
1041 hc->rh.interval = urb->interval;
1042 init_timer(&hc->rh.rh_int_timer);
1043 hc->rh.rh_int_timer.function = etrax_rh_int_timer_do;
1044 hc->rh.rh_int_timer.data = (unsigned long)urb;
1045 /* FIXME: Is the jiffies resolution enough? All intervals < 10 ms will be mapped
1046 to 0, and the rest to the nearest lower 10 ms. */
1047 hc->rh.rh_int_timer.expires = jiffies + ((HZ * hc->rh.interval) / 1000);
1048 add_timer(&hc->rh.rh_int_timer);
1049
1050 DBFEXIT;
1051}
1052
1053static void etrax_rh_int_timer_do(unsigned long ptr)
1054{
1055 struct urb *urb;
1056 etrax_hc_t *hc;
1057
1058 DBFENTER;
1059
1060 urb = (struct urb*)ptr;
1061 hc = urb->dev->bus->hcpriv;
1062
1063 if (hc->rh.send) {
1064 etrax_rh_send_irq(urb);
1065 }
1066
1067 DBFEXIT;
1068}
1069
1070static int etrax_usb_setup_epid(struct urb *urb)
1071{
1072 int epid;
1073 char devnum, endpoint, out_traffic, slow;
1074 int maxlen;
1075 unsigned long flags;
1076
1077 DBFENTER;
1078
1079 epid = etrax_usb_lookup_epid(urb);
1080 if ((epid != -1)){
1081 /* An epid that fits this urb has been found. */
1082 DBFEXIT;
1083 return epid;
1084 }
1085
1086 /* We must find and initiate a new epid for this urb. */
1087 epid = etrax_usb_allocate_epid();
1088
1089 if (epid == -1) {
1090 /* Failed to allocate a new epid. */
1091 DBFEXIT;
1092 return epid;
1093 }
1094
1095 /* We now have a new epid to use. Initiate it. */
1096 set_bit(epid, (void *)&epid_usage_bitmask);
1097
1098 devnum = usb_pipedevice(urb->pipe);
1099 endpoint = usb_pipeendpoint(urb->pipe);
1100 slow = usb_pipeslow(urb->pipe);
1101 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1102 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1103 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1104 out_traffic = 1;
1105 } else {
1106 out_traffic = usb_pipeout(urb->pipe);
1107 }
1108
1109 save_flags(flags);
1110 cli();
1111
1112 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1113 nop();
1114
1115 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1116 *R_USB_EPT_DATA_ISO = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
1117 /* FIXME: Change any to the actual port? */
1118 IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
1119 IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
1120 IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
1121 IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
1122 } else {
1123 *R_USB_EPT_DATA = IO_STATE(R_USB_EPT_DATA, valid, yes) |
1124 IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
1125 /* FIXME: Change any to the actual port? */
1126 IO_STATE(R_USB_EPT_DATA, port, any) |
1127 IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
1128 IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
1129 IO_FIELD(R_USB_EPT_DATA, dev, devnum);
1130 }
1131
1132 restore_flags(flags);
1133
1134 if (out_traffic) {
1135 set_bit(epid, (void *)&epid_out_traffic);
1136 } else {
1137 clear_bit(epid, (void *)&epid_out_traffic);
1138 }
1139
1140 dbg_epid("Setting up epid %d with devnum %d, endpoint %d and max_len %d (%s)",
1141 epid, devnum, endpoint, maxlen, out_traffic ? "OUT" : "IN");
1142
1143 DBFEXIT;
1144 return epid;
1145}
1146
1147static void etrax_usb_free_epid(int epid)
1148{
1149 unsigned long flags;
1150
1151 DBFENTER;
1152
1153 if (!test_bit(epid, (void *)&epid_usage_bitmask)) {
1154 warn("Trying to free unused epid %d", epid);
1155 DBFEXIT;
1156 return;
1157 }
1158
1159 save_flags(flags);
1160 cli();
1161
1162 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1163 nop();
1164 while (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold));
1165 /* This will, among other things, set the valid field to 0. */
1166 *R_USB_EPT_DATA = 0;
1167 restore_flags(flags);
1168
1169 clear_bit(epid, (void *)&epid_usage_bitmask);
1170
1171
1172 dbg_epid("Freed epid %d", epid);
1173
1174 DBFEXIT;
1175}
1176
1177static int etrax_usb_lookup_epid(struct urb *urb)
1178{
1179 int i;
1180 __u32 data;
1181 char devnum, endpoint, slow, out_traffic;
1182 int maxlen;
1183 unsigned long flags;
1184
1185 DBFENTER;
1186
1187 devnum = usb_pipedevice(urb->pipe);
1188 endpoint = usb_pipeendpoint(urb->pipe);
1189 slow = usb_pipeslow(urb->pipe);
1190 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1191 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1192 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1193 out_traffic = 1;
1194 } else {
1195 out_traffic = usb_pipeout(urb->pipe);
1196 }
1197
1198 /* Step through att epids. */
1199 for (i = 0; i < NBR_OF_EPIDS; i++) {
1200 if (test_bit(i, (void *)&epid_usage_bitmask) &&
1201 test_bit(i, (void *)&epid_out_traffic) == out_traffic) {
1202
1203 save_flags(flags);
1204 cli();
1205 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, i);
1206 nop();
1207
1208 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1209 data = *R_USB_EPT_DATA_ISO;
1210 restore_flags(flags);
1211
1212 if ((IO_MASK(R_USB_EPT_DATA_ISO, valid) & data) &&
1213 (IO_EXTRACT(R_USB_EPT_DATA_ISO, dev, data) == devnum) &&
1214 (IO_EXTRACT(R_USB_EPT_DATA_ISO, ep, data) == endpoint) &&
1215 (IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len, data) == maxlen)) {
1216 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1217 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1218 DBFEXIT;
1219 return i;
1220 }
1221 } else {
1222 data = *R_USB_EPT_DATA;
1223 restore_flags(flags);
1224
1225 if ((IO_MASK(R_USB_EPT_DATA, valid) & data) &&
1226 (IO_EXTRACT(R_USB_EPT_DATA, dev, data) == devnum) &&
1227 (IO_EXTRACT(R_USB_EPT_DATA, ep, data) == endpoint) &&
1228 (IO_EXTRACT(R_USB_EPT_DATA, low_speed, data) == slow) &&
1229 (IO_EXTRACT(R_USB_EPT_DATA, max_len, data) == maxlen)) {
1230 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1231 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1232 DBFEXIT;
1233 return i;
1234 }
1235 }
1236 }
1237 }
1238
1239 DBFEXIT;
1240 return -1;
1241}
1242
1243static int etrax_usb_allocate_epid(void)
1244{
1245 int i;
1246
1247 DBFENTER;
1248
1249 for (i = 0; i < NBR_OF_EPIDS; i++) {
1250 if (!test_bit(i, (void *)&epid_usage_bitmask)) {
1251 dbg_epid("Found free epid %d", i);
1252 DBFEXIT;
1253 return i;
1254 }
1255 }
1256
1257 dbg_epid("Found no free epids");
1258 DBFEXIT;
1259 return -1;
1260}
1261
1262static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags)
1263{
1264 etrax_hc_t *hc;
1265 int ret = -EINVAL;
1266
1267 DBFENTER;
1268
1269 if (!urb->dev || !urb->dev->bus) {
1270 return -ENODEV;
1271 }
1272 if (usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)) <= 0) {
1273 info("Submit urb to pipe with maxpacketlen 0, pipe 0x%X\n", urb->pipe);
1274 return -EMSGSIZE;
1275 }
1276
1277 if (urb->timeout) {
1278 /* FIXME. */
1279 warn("urb->timeout specified, ignoring.");
1280 }
1281
1282 hc = (etrax_hc_t*)urb->dev->bus->hcpriv;
1283
1284 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1285 /* This request is for the Virtual Root Hub. */
1286 ret = etrax_rh_submit_urb(urb);
1287
1288 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1289
1290 ret = etrax_usb_submit_bulk_urb(urb);
1291
1292 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1293
1294 ret = etrax_usb_submit_ctrl_urb(urb);
1295
1296 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1297 int bustime;
1298
1299 if (urb->bandwidth == 0) {
1300 bustime = usb_check_bandwidth(urb->dev, urb);
1301 if (bustime < 0) {
1302 ret = bustime;
1303 } else {
1304 ret = etrax_usb_submit_intr_urb(urb);
1305 if (ret == 0)
1306 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1307 }
1308 } else {
1309 /* Bandwidth already set. */
1310 ret = etrax_usb_submit_intr_urb(urb);
1311 }
1312
1313 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1314 int bustime;
1315
1316 if (urb->bandwidth == 0) {
1317 bustime = usb_check_bandwidth(urb->dev, urb);
1318 if (bustime < 0) {
1319 ret = bustime;
1320 } else {
1321 ret = etrax_usb_submit_isoc_urb(urb);
1322 if (ret == 0)
1323 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1324 }
1325 } else {
1326 /* Bandwidth already set. */
1327 ret = etrax_usb_submit_isoc_urb(urb);
1328 }
1329 }
1330
1331 DBFEXIT;
1332
1333 if (ret != 0)
1334 printk("Submit URB error %d\n", ret);
1335
1336 return ret;
1337}
1338
1339static int etrax_usb_unlink_urb(struct urb *urb, int status)
1340{
1341 etrax_hc_t *hc;
1342 etrax_urb_priv_t *urb_priv;
1343 int epid;
1344 unsigned int flags;
1345
1346 DBFENTER;
1347
1348 if (!urb) {
1349 return -EINVAL;
1350 }
1351
1352 /* Disable interrupts here since a descriptor interrupt for the isoc epid
1353 will modify the sb list. This could possibly be done more granular, but
1354 unlink_urb should not be used frequently anyway.
1355 */
1356
1357 save_flags(flags);
1358 cli();
1359
1360 if (!urb->dev || !urb->dev->bus) {
1361 restore_flags(flags);
1362 return -ENODEV;
1363 }
1364 if (!urb->hcpriv) {
1365 /* This happens if a device driver calls unlink on an urb that
1366 was never submitted (lazy driver) or if the urb was completed
1367 while unlink was being called. */
1368 restore_flags(flags);
1369 return 0;
1370 }
1371 if (urb->transfer_flags & URB_ASYNC_UNLINK) {
1372 /* FIXME. */
1373 /* If URB_ASYNC_UNLINK is set:
1374 unlink
1375 move to a separate urb list
1376 call complete at next sof with ECONNRESET
1377
1378 If not:
1379 wait 1 ms
1380 unlink
1381 call complete with ENOENT
1382 */
1383 warn("URB_ASYNC_UNLINK set, ignoring.");
1384 }
1385
1386 /* One might think that urb->status = -EINPROGRESS would be a requirement for unlinking,
1387 but that doesn't work for interrupt and isochronous traffic since they are completed
1388 repeatedly, and urb->status is set then. That may in itself be a bug though. */
1389
1390 hc = urb->dev->bus->hcpriv;
1391 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1392 epid = urb_priv->epid;
1393
1394 /* Set the urb status (synchronous unlink). */
1395 urb->status = -ENOENT;
1396 urb_priv->urb_state = UNLINK;
1397
1398 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1399 int ret;
1400 ret = etrax_rh_unlink_urb(urb);
1401 DBFEXIT;
1402 restore_flags(flags);
1403 return ret;
1404
1405 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1406
1407 dbg_bulk("Unlink of bulk urb (0x%lx)", (unsigned long)urb);
1408
1409 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1410 /* The EP was enabled, disable it and wait. */
1411 TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1412
1413 /* Ah, the luxury of busy-wait. */
1414 while (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[epid]));
1415 }
1416 /* Kicking dummy list out of the party. */
1417 TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
1418
1419 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1420
1421 dbg_ctrl("Unlink of ctrl urb (0x%lx)", (unsigned long)urb);
1422
1423 if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1424 /* The EP was enabled, disable it and wait. */
1425 TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1426
1427 /* Ah, the luxury of busy-wait. */
1428 while (*R_DMA_CH8_SUB1_EP == virt_to_phys(&TxCtrlEPList[epid]));
1429 }
1430
1431 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1432
1433 dbg_intr("Unlink of intr urb (0x%lx)", (unsigned long)urb);
1434
1435 /* Separate function because it's a tad more complicated. */
1436 etrax_usb_unlink_intr_urb(urb);
1437
1438 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1439
1440 dbg_isoc("Unlink of isoc urb (0x%lx)", (unsigned long)urb);
1441
1442 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1443 /* The EP was enabled, disable it and wait. */
1444 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1445
1446 /* Ah, the luxury of busy-wait. */
1447 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
1448 }
1449 }
1450
1451 /* Note that we need to remove the urb from the urb list *before* removing its SB
1452 descriptors. (This means that the isoc eof handler might get a null urb when we
1453 are unlinking the last urb.) */
1454
1455 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1456
1457 urb_list_del(urb, epid);
1458 TxBulkEPList[epid].sub = 0;
1459 etrax_remove_from_sb_list(urb);
1460
1461 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1462
1463 urb_list_del(urb, epid);
1464 TxCtrlEPList[epid].sub = 0;
1465 etrax_remove_from_sb_list(urb);
1466
1467 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1468
1469 urb_list_del(urb, epid);
1470 /* Sanity check (should never happen). */
1471 assert(urb_list_empty(epid));
1472
1473 /* Release allocated bandwidth. */
1474 usb_release_bandwidth(urb->dev, urb, 0);
1475
1476 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1477
1478 if (usb_pipeout(urb->pipe)) {
1479
1480 USB_SB_Desc_t *iter_sb, *prev_sb, *next_sb;
1481
1482 if (__urb_list_entry(urb, epid)) {
1483
1484 urb_list_del(urb, epid);
1485 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1486 prev_sb = 0;
1487 while (iter_sb && (iter_sb != urb_priv->first_sb)) {
1488 prev_sb = iter_sb;
1489 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1490 }
1491
1492 if (iter_sb == 0) {
1493 /* Unlink of the URB currently being transmitted. */
1494 prev_sb = 0;
1495 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1496 }
1497
1498 while (iter_sb && (iter_sb != urb_priv->last_sb)) {
1499 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1500 }
1501 if (iter_sb) {
1502 next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1503 } else {
1504 /* This should only happen if the DMA has completed
1505 processing the SB list for this EP while interrupts
1506 are disabled. */
1507 dbg_isoc("Isoc urb not found, already sent?");
1508 next_sb = 0;
1509 }
1510 if (prev_sb) {
1511 prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
1512 } else {
1513 TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
1514 }
1515
1516 etrax_remove_from_sb_list(urb);
1517 if (urb_list_empty(epid)) {
1518 TxIsocEPList[epid].sub = 0;
1519 dbg_isoc("Last isoc out urb epid %d", epid);
1520 } else if (next_sb || prev_sb) {
1521 dbg_isoc("Re-enable isoc out epid %d", epid);
1522
1523 TxIsocEPList[epid].hw_len = 0;
1524 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1525 } else {
1526 TxIsocEPList[epid].sub = 0;
1527 dbg_isoc("URB list non-empty and no SB list, EP disabled");
1528 }
1529 } else {
1530 dbg_isoc("Urb 0x%p not found, completed already?", urb);
1531 }
1532 } else {
1533
1534 urb_list_del(urb, epid);
1535
1536 /* For in traffic there is only one SB descriptor for each EP even
1537 though there may be several urbs (all urbs point at the same SB). */
1538 if (urb_list_empty(epid)) {
1539 /* No more urbs, remove the SB. */
1540 TxIsocEPList[epid].sub = 0;
1541 etrax_remove_from_sb_list(urb);
1542 } else {
1543 TxIsocEPList[epid].hw_len = 0;
1544 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1545 }
1546 }
1547 /* Release allocated bandwidth. */
1548 usb_release_bandwidth(urb->dev, urb, 1);
1549 }
1550 /* Free the epid if urb list is empty. */
1551 if (urb_list_empty(epid)) {
1552 etrax_usb_free_epid(epid);
1553 }
1554 restore_flags(flags);
1555
1556 /* Must be done before calling completion handler. */
1557 kfree(urb_priv);
1558 urb->hcpriv = 0;
1559
1560 if (urb->complete) {
1561 urb->complete(urb, NULL);
1562 }
1563
1564 DBFEXIT;
1565 return 0;
1566}
1567
1568static int etrax_usb_get_frame_number(struct usb_device *usb_dev)
1569{
1570 DBFENTER;
1571 DBFEXIT;
1572 return (*R_USB_FM_NUMBER & 0x7ff);
1573}
1574
1575static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc)
1576{
1577 DBFENTER;
1578
1579 /* This interrupt handler could be used when unlinking EP descriptors. */
1580
1581 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
1582 USB_EP_Desc_t *ep;
1583
1584 //dbg_bulk("dma8_sub0_descr (BULK) intr.");
1585
1586 /* It should be safe clearing the interrupt here, since we don't expect to get a new
1587 one until we restart the bulk channel. */
1588 *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
1589
1590 /* Wait while the DMA is running (though we don't expect it to be). */
1591 while (*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd));
1592
1593 /* Advance the DMA to the next EP descriptor. */
1594 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
1595
1596 //dbg_bulk("descr intr: DMA is at 0x%lx", (unsigned long)ep);
1597
1598 /* ep->next is already a physical address; no need for a virt_to_phys. */
1599 *R_DMA_CH8_SUB0_EP = ep->next;
1600
1601 /* Start the DMA bulk channel again. */
1602 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1603 }
1604 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
1605 struct urb *urb;
1606 int epid;
1607 etrax_urb_priv_t *urb_priv;
1608 unsigned long int flags;
1609
1610 dbg_ctrl("dma8_sub1_descr (CTRL) intr.");
1611 *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
1612
1613 /* The complete callback gets called so we cli. */
1614 save_flags(flags);
1615 cli();
1616
1617 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1618 if ((TxCtrlEPList[epid].sub == 0) ||
1619 (epid == DUMMY_EPID) ||
1620 (epid == INVALID_EPID)) {
1621 /* Nothing here to see. */
1622 continue;
1623 }
1624
1625 /* Get the first urb (if any). */
1626 urb = urb_list_first(epid);
1627
1628 if (urb) {
1629
1630 /* Sanity check. */
1631 assert(usb_pipetype(urb->pipe) == PIPE_CONTROL);
1632
1633 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1634 assert(urb_priv);
1635
1636 if (urb_priv->urb_state == WAITING_FOR_DESCR_INTR) {
1637 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
1638
1639 etrax_usb_complete_urb(urb, 0);
1640 }
1641 }
1642 }
1643 restore_flags(flags);
1644 }
1645 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
1646 dbg_intr("dma8_sub2_descr (INTR) intr.");
1647 *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
1648 }
1649 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
1650 struct urb *urb;
1651 int epid;
1652 int epid_done;
1653 etrax_urb_priv_t *urb_priv;
1654 USB_SB_Desc_t *sb_desc;
1655
1656 usb_isoc_complete_data_t *comp_data = NULL;
1657
1658 /* One or more isoc out transfers are done. */
1659 dbg_isoc("dma8_sub3_descr (ISOC) intr.");
1660
1661 /* For each isoc out EP search for the first sb_desc with the intr flag
1662 set. This descriptor must be the last packet from an URB. Then
1663 traverse the URB list for the EP until the URB with urb_priv->last_sb
1664 matching the intr-marked sb_desc is found. All URBs before this have
1665 been sent.
1666 */
1667
1668 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1669 /* Skip past epids with no SB lists, epids used for in traffic,
1670 and special (dummy, invalid) epids. */
1671 if ((TxIsocEPList[epid].sub == 0) ||
1672 (test_bit(epid, (void *)&epid_out_traffic) == 0) ||
1673 (epid == DUMMY_EPID) ||
1674 (epid == INVALID_EPID)) {
1675 /* Nothing here to see. */
1676 continue;
1677 }
1678 sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
1679
1680 /* Find the last descriptor of the currently active URB for this ep.
1681 This is the first descriptor in the sub list marked for a descriptor
1682 interrupt. */
1683 while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
1684 sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
1685 }
1686 assert(sb_desc);
1687
1688 dbg_isoc("Check epid %d, sub 0x%p, SB 0x%p",
1689 epid,
1690 phys_to_virt(TxIsocEPList[epid].sub),
1691 sb_desc);
1692
1693 epid_done = 0;
1694
1695 /* Get the first urb (if any). */
1696 urb = urb_list_first(epid);
1697 assert(urb);
1698
1699 while (urb && !epid_done) {
1700
1701 /* Sanity check. */
1702 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
1703
1704 if (!usb_pipeout(urb->pipe)) {
1705 /* descr interrupts are generated only for out pipes. */
1706 epid_done = 1;
1707 continue;
1708 }
1709
1710 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1711 assert(urb_priv);
1712
1713 if (sb_desc != urb_priv->last_sb) {
1714
1715 /* This urb has been sent. */
1716 dbg_isoc("out URB 0x%p sent", urb);
1717
1718 urb_priv->urb_state = TRANSFER_DONE;
1719
1720 } else if ((sb_desc == urb_priv->last_sb) &&
1721 !(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1722
1723 assert((sb_desc->command & IO_MASK(USB_SB_command, eol)) == IO_STATE(USB_SB_command, eol, yes));
1724 assert(sb_desc->next == 0);
1725
1726 dbg_isoc("out URB 0x%p last in list, epid disabled", urb);
1727 TxIsocEPList[epid].sub = 0;
1728 TxIsocEPList[epid].hw_len = 0;
1729 urb_priv->urb_state = TRANSFER_DONE;
1730
1731 epid_done = 1;
1732
1733 } else {
1734 epid_done = 1;
1735 }
1736 if (!epid_done) {
1737 urb = urb_list_next(urb, epid);
1738 }
1739 }
1740
1741 }
1742
1743 *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
1744
1745 comp_data = (usb_isoc_complete_data_t*)kmem_cache_alloc(isoc_compl_cache, GFP_ATOMIC);
1746 assert(comp_data != NULL);
1747
1748 INIT_WORK(&comp_data->usb_bh, etrax_usb_isoc_descr_interrupt_bottom_half, comp_data);
1749 schedule_work(&comp_data->usb_bh);
1750 }
1751
1752 DBFEXIT;
1753 return IRQ_HANDLED;
1754}
1755
1756static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data)
1757{
1758 usb_isoc_complete_data_t *comp_data = (usb_isoc_complete_data_t*)data;
1759
1760 struct urb *urb;
1761 int epid;
1762 int epid_done;
1763 etrax_urb_priv_t *urb_priv;
1764
1765 DBFENTER;
1766
1767 dbg_isoc("dma8_sub3_descr (ISOC) bottom half.");
1768
1769 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1770 unsigned long flags;
1771
1772 save_flags(flags);
1773 cli();
1774
1775 epid_done = 0;
1776
1777 /* The descriptor interrupt handler has marked all transmitted isoch. out
1778 URBs with TRANSFER_DONE. Now we traverse all epids and for all that
1779 have isoch. out traffic traverse its URB list and complete the
1780 transmitted URB.
1781 */
1782
1783 while (!epid_done) {
1784
1785 /* Get the first urb (if any). */
1786 urb = urb_list_first(epid);
1787 if (urb == 0) {
1788 epid_done = 1;
1789 continue;
1790 }
1791
1792 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
1793 epid_done = 1;
1794 continue;
1795 }
1796
1797 if (!usb_pipeout(urb->pipe)) {
1798 /* descr interrupts are generated only for out pipes. */
1799 epid_done = 1;
1800 continue;
1801 }
1802
1803 dbg_isoc("Check epid %d, SB 0x%p", epid, (char*)TxIsocEPList[epid].sub);
1804
1805 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1806 assert(urb_priv);
1807
1808 if (urb_priv->urb_state == TRANSFER_DONE) {
1809 int i;
1810 struct usb_iso_packet_descriptor *packet;
1811
1812 /* This urb has been sent. */
1813 dbg_isoc("Completing isoc out URB 0x%p", urb);
1814
1815 for (i = 0; i < urb->number_of_packets; i++) {
1816 packet = &urb->iso_frame_desc[i];
1817 packet->status = 0;
1818 packet->actual_length = packet->length;
1819 }
1820
1821 etrax_usb_complete_isoc_urb(urb, 0);
1822
1823 if (urb_list_empty(epid)) {
1824 etrax_usb_free_epid(epid);
1825 epid_done = 1;
1826 }
1827 } else {
1828 epid_done = 1;
1829 }
1830 }
1831 restore_flags(flags);
1832
1833 }
1834 kmem_cache_free(isoc_compl_cache, comp_data);
1835
1836 DBFEXIT;
1837}
1838
1839
1840
1841static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc)
1842{
1843 struct urb *urb;
1844 etrax_urb_priv_t *urb_priv;
1845 int epid = 0;
1846 unsigned long flags;
1847
1848 /* Isoc diagnostics. */
1849 static int curr_fm = 0;
1850 static int prev_fm = 0;
1851
1852 DBFENTER;
1853
1854 /* Clear this interrupt. */
1855 *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
1856
1857 /* Note that this while loop assumes that all packets span only
1858 one rx descriptor. */
1859
1860 /* The reason we cli here is that we call the driver's callback functions. */
1861 save_flags(flags);
1862 cli();
1863
1864 while (myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
1865
1866 epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
1867 urb = urb_list_first(epid);
1868
1869 //printk("eop for epid %d, first urb 0x%lx\n", epid, (unsigned long)urb);
1870
1871 if (!urb) {
1872 err("No urb for epid %d in rx interrupt", epid);
1873 __dump_ept_data(epid);
1874 goto skip_out;
1875 }
1876
1877 /* Note that we cannot indescriminately assert(usb_pipein(urb->pipe)) since
1878 ctrl pipes are not. */
1879
1880 if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
1881 __u32 r_usb_ept_data;
1882 int no_error = 0;
1883
1884 assert(test_bit(epid, (void *)&epid_usage_bitmask));
1885
1886 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1887 nop();
1888 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1889 r_usb_ept_data = *R_USB_EPT_DATA_ISO;
1890
1891 if ((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
1892 (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
1893 (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
1894 /* Not an error, just a failure to receive an expected iso
1895 in packet in this frame. This is not documented
1896 in the designers reference.
1897 */
1898 no_error++;
1899 } else {
1900 warn("R_USB_EPT_DATA_ISO for epid %d = 0x%x", epid, r_usb_ept_data);
1901 }
1902 } else {
1903 r_usb_ept_data = *R_USB_EPT_DATA;
1904 warn("R_USB_EPT_DATA for epid %d = 0x%x", epid, r_usb_ept_data);
1905 }
1906
1907 if (!no_error){
1908 warn("error in rx desc->status, epid %d, first urb = 0x%lx",
1909 epid, (unsigned long)urb);
1910 __dump_in_desc(myNextRxDesc);
1911
1912 warn("R_USB_STATUS = 0x%x", *R_USB_STATUS);
1913
1914 /* Check that ept was disabled when error occurred. */
1915 switch (usb_pipetype(urb->pipe)) {
1916 case PIPE_BULK:
1917 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1918 break;
1919 case PIPE_CONTROL:
1920 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1921 break;
1922 case PIPE_INTERRUPT:
1923 assert(!(TxIntrEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1924 break;
1925 case PIPE_ISOCHRONOUS:
1926 assert(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1927 break;
1928 default:
1929 warn("etrax_usb_rx_interrupt: bad pipetype %d in urb 0x%p",
1930 usb_pipetype(urb->pipe),
1931 urb);
1932 }
1933 etrax_usb_complete_urb(urb, -EPROTO);
1934 goto skip_out;
1935 }
1936 }
1937
1938 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1939 assert(urb_priv);
1940
1941 if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
1942 (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
1943 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1944
1945 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
1946 /* We get nodata for empty data transactions, and the rx descriptor's
1947 hw_len field is not valid in that case. No data to copy in other
1948 words. */
1949 } else {
1950 /* Make sure the data fits in the buffer. */
1951 assert(urb_priv->rx_offset + myNextRxDesc->hw_len
1952 <= urb->transfer_buffer_length);
1953
1954 memcpy(urb->transfer_buffer + urb_priv->rx_offset,
1955 phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
1956 urb_priv->rx_offset += myNextRxDesc->hw_len;
1957 }
1958
1959 if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
1960 if ((usb_pipetype(urb->pipe) == PIPE_CONTROL) &&
1961 ((TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)) ==
1962 IO_STATE(USB_EP_command, enable, yes))) {
1963 /* The EP is still enabled, so the OUT packet used to ack
1964 the in data is probably not processed yet. If the EP
1965 sub pointer has not moved beyond urb_priv->last_sb mark
1966 it for a descriptor interrupt and complete the urb in
1967 the descriptor interrupt handler.
1968 */
1969 USB_SB_Desc_t *sub = TxCtrlEPList[urb_priv->epid].sub ? phys_to_virt(TxCtrlEPList[urb_priv->epid].sub) : 0;
1970
1971 while ((sub != NULL) && (sub != urb_priv->last_sb)) {
1972 sub = sub->next ? phys_to_virt(sub->next) : 0;
1973 }
1974 if (sub != NULL) {
1975 /* The urb has not been fully processed. */
1976 urb_priv->urb_state = WAITING_FOR_DESCR_INTR;
1977 } else {
1978 warn("(CTRL) epid enabled and urb (0x%p) processed, ep->sub=0x%p", urb, (char*)TxCtrlEPList[urb_priv->epid].sub);
1979 etrax_usb_complete_urb(urb, 0);
1980 }
1981 } else {
1982 etrax_usb_complete_urb(urb, 0);
1983 }
1984 }
1985
1986 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1987
1988 struct usb_iso_packet_descriptor *packet;
1989
1990 if (urb_priv->urb_state == UNLINK) {
1991 info("Ignoring rx data for urb being unlinked.");
1992 goto skip_out;
1993 } else if (urb_priv->urb_state == NOT_STARTED) {
1994 info("What? Got rx data for urb that isn't started?");
1995 goto skip_out;
1996 }
1997
1998 packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
1999 packet->status = 0;
2000
2001 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
2002 /* We get nodata for empty data transactions, and the rx descriptor's
2003 hw_len field is not valid in that case. We copy 0 bytes however to
2004 stay in synch. */
2005 packet->actual_length = 0;
2006 } else {
2007 packet->actual_length = myNextRxDesc->hw_len;
2008 /* Make sure the data fits in the buffer. */
2009 assert(packet->actual_length <= packet->length);
2010 memcpy(urb->transfer_buffer + packet->offset,
2011 phys_to_virt(myNextRxDesc->buf), packet->actual_length);
2012 }
2013
2014 /* Increment the packet counter. */
2015 urb_priv->isoc_packet_counter++;
2016
2017 /* Note that we don't care about the eot field in the rx descriptor's status.
2018 It will always be set for isoc traffic. */
2019 if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
2020
2021 /* Out-of-synch diagnostics. */
2022 curr_fm = (*R_USB_FM_NUMBER & 0x7ff);
2023 if (((prev_fm + urb_priv->isoc_packet_counter) % (0x7ff + 1)) != curr_fm) {
2024 /* This test is wrong, if there is more than one isoc
2025 in endpoint active it will always calculate wrong
2026 since prev_fm is shared by all endpoints.
2027
2028 FIXME Make this check per URB using urb->start_frame.
2029 */
2030 dbg_isoc("Out of synch? Previous frame = %d, current frame = %d",
2031 prev_fm, curr_fm);
2032
2033 }
2034 prev_fm = curr_fm;
2035
2036 /* Complete the urb with status OK. */
2037 etrax_usb_complete_isoc_urb(urb, 0);
2038 }
2039 }
2040
2041 skip_out:
2042
2043 /* DMA IN cache bug. Flush the DMA IN buffer from the cache. (struct etrax_dma_descr
2044 has the same layout as USB_IN_Desc for the relevant fields.) */
2045 prepare_rx_descriptor((struct etrax_dma_descr*)myNextRxDesc);
2046
2047 myPrevRxDesc = myNextRxDesc;
2048 myPrevRxDesc->command |= IO_MASK(USB_IN_command, eol);
2049 myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
2050 myLastRxDesc = myPrevRxDesc;
2051
2052 myNextRxDesc->status = 0;
2053 myNextRxDesc = phys_to_virt(myNextRxDesc->next);
2054 }
2055
2056 restore_flags(flags);
2057
2058 DBFEXIT;
2059
2060 return IRQ_HANDLED;
2061}
2062
2063
2064/* This function will unlink the SB descriptors associated with this urb. */
2065static int etrax_remove_from_sb_list(struct urb *urb)
2066{
2067 USB_SB_Desc_t *next_sb, *first_sb, *last_sb;
2068 etrax_urb_priv_t *urb_priv;
2069 int i = 0;
2070
2071 DBFENTER;
2072
2073 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2074 assert(urb_priv);
2075
2076 /* Just a sanity check. Since we don't fiddle with the DMA list the EP descriptor
2077 doesn't really need to be disabled, it's just that we expect it to be. */
2078 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2079 assert(!(TxBulkEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2080 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
2081 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2082 }
2083
2084 first_sb = urb_priv->first_sb;
2085 last_sb = urb_priv->last_sb;
2086
2087 assert(first_sb);
2088 assert(last_sb);
2089
2090 while (first_sb != last_sb) {
2091 next_sb = (USB_SB_Desc_t *)phys_to_virt(first_sb->next);
2092 kmem_cache_free(usb_desc_cache, first_sb);
2093 first_sb = next_sb;
2094 i++;
2095 }
2096 kmem_cache_free(usb_desc_cache, last_sb);
2097 i++;
2098 dbg_sb("%d SB descriptors freed", i);
2099 /* Compare i with urb->number_of_packets for Isoc traffic.
2100 Should be same when calling unlink_urb */
2101
2102 DBFEXIT;
2103
2104 return i;
2105}
2106
2107static int etrax_usb_submit_bulk_urb(struct urb *urb)
2108{
2109 int epid;
2110 int empty;
2111 unsigned long flags;
2112 etrax_urb_priv_t *urb_priv;
2113
2114 DBFENTER;
2115
2116 /* Epid allocation, empty check and list add must be protected.
2117 Read about this in etrax_usb_submit_ctrl_urb. */
2118
2119 spin_lock_irqsave(&urb_list_lock, flags);
2120 epid = etrax_usb_setup_epid(urb);
2121 if (epid == -1) {
2122 DBFEXIT;
2123 spin_unlock_irqrestore(&urb_list_lock, flags);
2124 return -ENOMEM;
2125 }
2126 empty = urb_list_empty(epid);
2127 urb_list_add(urb, epid);
2128 spin_unlock_irqrestore(&urb_list_lock, flags);
2129
2130 dbg_bulk("Adding bulk %s urb 0x%lx to %s list, epid %d",
2131 usb_pipein(urb->pipe) ? "IN" : "OUT", (unsigned long)urb, empty ? "empty" : "", epid);
2132
2133 /* Mark the urb as being in progress. */
2134 urb->status = -EINPROGRESS;
2135
2136 /* Setup the hcpriv data. */
2137 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2138 assert(urb_priv != NULL);
2139 /* This sets rx_offset to 0. */
2140 urb_priv->urb_state = NOT_STARTED;
2141 urb->hcpriv = urb_priv;
2142
2143 if (empty) {
2144 etrax_usb_add_to_bulk_sb_list(urb, epid);
2145 }
2146
2147 DBFEXIT;
2148
2149 return 0;
2150}
2151
2152static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid)
2153{
2154 USB_SB_Desc_t *sb_desc;
2155 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2156 unsigned long flags;
2157 char maxlen;
2158
2159 DBFENTER;
2160
2161 dbg_bulk("etrax_usb_add_to_bulk_sb_list, urb 0x%lx", (unsigned long)urb);
2162
2163 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2164
2165 sb_desc = kmem_cache_zalloc(usb_desc_cache, SLAB_FLAG);
2166 assert(sb_desc != NULL);
2167
2168
2169 if (usb_pipeout(urb->pipe)) {
2170
2171 dbg_bulk("Grabbing bulk OUT, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2172
2173 /* This is probably a sanity check of the bulk transaction length
2174 not being larger than 64 kB. */
2175 if (urb->transfer_buffer_length > 0xffff) {
2176 panic("urb->transfer_buffer_length > 0xffff");
2177 }
2178
2179 sb_desc->sw_len = urb->transfer_buffer_length;
2180
2181 /* The rem field is don't care if it's not a full-length transfer, so setting
2182 it shouldn't hurt. Also, rem isn't used for OUT traffic. */
2183 sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
2184 IO_STATE(USB_SB_command, tt, out) |
2185 IO_STATE(USB_SB_command, eot, yes) |
2186 IO_STATE(USB_SB_command, eol, yes));
2187
2188 /* The full field is set to yes, even if we don't actually check that this is
2189 a full-length transfer (i.e., that transfer_buffer_length % maxlen = 0).
2190 Setting full prevents the USB controller from sending an empty packet in
2191 that case. However, if URB_ZERO_PACKET was set we want that. */
2192 if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
2193 sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
2194 }
2195
2196 sb_desc->buf = virt_to_phys(urb->transfer_buffer);
2197 sb_desc->next = 0;
2198
2199 } else if (usb_pipein(urb->pipe)) {
2200
2201 dbg_bulk("Grabbing bulk IN, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2202
2203 sb_desc->sw_len = urb->transfer_buffer_length ?
2204 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2205
2206 /* The rem field is don't care if it's not a full-length transfer, so setting
2207 it shouldn't hurt. */
2208 sb_desc->command =
2209 (IO_FIELD(USB_SB_command, rem,
2210 urb->transfer_buffer_length % maxlen) |
2211 IO_STATE(USB_SB_command, tt, in) |
2212 IO_STATE(USB_SB_command, eot, yes) |
2213 IO_STATE(USB_SB_command, eol, yes));
2214
2215 sb_desc->buf = 0;
2216 sb_desc->next = 0;
2217 }
2218
2219 urb_priv->first_sb = sb_desc;
2220 urb_priv->last_sb = sb_desc;
2221 urb_priv->epid = epid;
2222
2223 urb->hcpriv = urb_priv;
2224
2225 /* Reset toggle bits and reset error count. */
2226 save_flags(flags);
2227 cli();
2228
2229 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2230 nop();
2231
2232 /* FIXME: Is this a special case since the hold field is checked,
2233 or should we check hold in a lot of other cases as well? */
2234 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2235 panic("Hold was set in %s", __FUNCTION__);
2236 }
2237
2238 /* Reset error counters (regardless of which direction this traffic is). */
2239 *R_USB_EPT_DATA &=
2240 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2241 IO_MASK(R_USB_EPT_DATA, error_count_out));
2242
2243 /* Software must preset the toggle bits. */
2244 if (usb_pipeout(urb->pipe)) {
2245 char toggle =
2246 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2247 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
2248 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
2249 } else {
2250 char toggle =
2251 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2252 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
2253 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
2254 }
2255
2256 /* Assert that the EP descriptor is disabled. */
2257 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2258
2259 /* The reason we set the EP's sub pointer directly instead of
2260 walking the SB list and linking it last in the list is that we only
2261 have one active urb at a time (the rest are queued). */
2262
2263 /* Note that we cannot have interrupts running when we have set the SB descriptor
2264 but the EP is not yet enabled. If a bulk eot happens for another EP, we will
2265 find this EP disabled and with a SB != 0, which will make us think that it's done. */
2266 TxBulkEPList[epid].sub = virt_to_phys(sb_desc);
2267 TxBulkEPList[epid].hw_len = 0;
2268 /* Note that we don't have to fill in the ep_id field since this
2269 was done when we allocated the EP descriptors in init_tx_bulk_ep. */
2270
2271 /* Check if the dummy list is already with us (if several urbs were queued). */
2272 if (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0])) {
2273
2274 dbg_bulk("Inviting dummy list to the party for urb 0x%lx, epid %d",
2275 (unsigned long)urb, epid);
2276
2277 /* The last EP in the dummy list already has its next pointer set to
2278 TxBulkEPList[epid].next. */
2279
2280 /* We don't need to check if the DMA is at this EP or not before changing the
2281 next pointer, since we will do it in one 32-bit write (EP descriptors are
2282 32-bit aligned). */
2283 TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
2284 }
2285 /* Enable the EP descr. */
2286 dbg_bulk("Enabling bulk EP for urb 0x%lx, epid %d", (unsigned long)urb, epid);
2287 TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2288
2289 /* Everything is set up, safe to enable interrupts again. */
2290 restore_flags(flags);
2291
2292 /* If the DMA bulk channel isn't running, we need to restart it if it
2293 has stopped at the last EP descriptor (DMA stopped because there was
2294 no more traffic) or if it has stopped at a dummy EP with the intr flag
2295 set (DMA stopped because we were too slow in inserting new traffic). */
2296 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
2297
2298 USB_EP_Desc_t *ep;
2299 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
2300 dbg_bulk("DMA channel not running in add");
2301 dbg_bulk("DMA is at 0x%lx", (unsigned long)ep);
2302
2303 if (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[NBR_OF_EPIDS - 1]) ||
2304 (ep->command & 0x8) >> 3) {
2305 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
2306 /* Update/restart the bulk start timer since we just started the channel. */
2307 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
2308 /* Update/restart the bulk eot timer since we just inserted traffic. */
2309 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
2310 }
2311 }
2312
2313 DBFEXIT;
2314}
2315
2316static void etrax_usb_complete_bulk_urb(struct urb *urb, int status)
2317{
2318 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2319 int epid = urb_priv->epid;
2320 unsigned long flags;
2321
2322 DBFENTER;
2323
2324 if (status)
2325 warn("Completing bulk urb with status %d.", status);
2326
2327 dbg_bulk("Completing bulk urb 0x%lx for epid %d", (unsigned long)urb, epid);
2328
2329 /* Update the urb list. */
2330 urb_list_del(urb, epid);
2331
2332 /* For an IN pipe, we always set the actual length, regardless of whether there was
2333 an error or not (which means the device driver can use the data if it wants to). */
2334 if (usb_pipein(urb->pipe)) {
2335 urb->actual_length = urb_priv->rx_offset;
2336 } else {
2337 /* Set actual_length for OUT urbs also; the USB mass storage driver seems
2338 to want that. We wouldn't know of any partial writes if there was an error. */
2339 if (status == 0) {
2340 urb->actual_length = urb->transfer_buffer_length;
2341 } else {
2342 urb->actual_length = 0;
2343 }
2344 }
2345
2346 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2347 Like, maybe we shouldn't toggle the toggle bits, or maybe we shouldn't insert more traffic. */
2348
2349 save_flags(flags);
2350 cli();
2351
2352 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2353 nop();
2354
2355 /* We need to fiddle with the toggle bits because the hardware doesn't do it for us. */
2356 if (usb_pipeout(urb->pipe)) {
2357 char toggle =
2358 IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
2359 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2360 usb_pipeout(urb->pipe), toggle);
2361 } else {
2362 char toggle =
2363 IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
2364 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2365 usb_pipeout(urb->pipe), toggle);
2366 }
2367 restore_flags(flags);
2368
2369 /* Remember to free the SBs. */
2370 etrax_remove_from_sb_list(urb);
2371 kfree(urb_priv);
2372 urb->hcpriv = 0;
2373
2374 /* If there are any more urb's in the list we'd better start sending */
2375 if (!urb_list_empty(epid)) {
2376
2377 struct urb *new_urb;
2378
2379 /* Get the first urb. */
2380 new_urb = urb_list_first(epid);
2381 assert(new_urb);
2382
2383 dbg_bulk("More bulk for epid %d", epid);
2384
2385 etrax_usb_add_to_bulk_sb_list(new_urb, epid);
2386 }
2387
2388 urb->status = status;
2389
2390 /* We let any non-zero status from the layer above have precedence. */
2391 if (status == 0) {
2392 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2393 is to be treated as an error. */
2394 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2395 if (usb_pipein(urb->pipe) &&
2396 (urb->actual_length !=
2397 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2398 urb->status = -EREMOTEIO;
2399 }
2400 }
2401 }
2402
2403 if (urb->complete) {
2404 urb->complete(urb, NULL);
2405 }
2406
2407 if (urb_list_empty(epid)) {
2408 /* This means that this EP is now free, deconfigure it. */
2409 etrax_usb_free_epid(epid);
2410
2411 /* No more traffic; time to clean up.
2412 Must set sub pointer to 0, since we look at the sub pointer when handling
2413 the bulk eot interrupt. */
2414
2415 dbg_bulk("No bulk for epid %d", epid);
2416
2417 TxBulkEPList[epid].sub = 0;
2418
2419 /* Unlink the dummy list. */
2420
2421 dbg_bulk("Kicking dummy list out of party for urb 0x%lx, epid %d",
2422 (unsigned long)urb, epid);
2423
2424 /* No need to wait for the DMA before changing the next pointer.
2425 The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
2426 the last one (INVALID_EPID) for actual traffic. */
2427 TxBulkEPList[epid].next =
2428 virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2429 }
2430
2431 DBFEXIT;
2432}
2433
2434static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2435{
2436 int epid;
2437 int empty;
2438 unsigned long flags;
2439 etrax_urb_priv_t *urb_priv;
2440
2441 DBFENTER;
2442
2443 /* FIXME: Return -ENXIO if there is already a queued urb for this endpoint? */
2444
2445 /* Epid allocation, empty check and list add must be protected.
2446
2447 Epid allocation because if we find an existing epid for this endpoint an urb might be
2448 completed (emptying the list) before we add the new urb to the list, causing the epid
2449 to be de-allocated. We would then start the transfer with an invalid epid -> epid attn.
2450
2451 Empty check and add because otherwise we might conclude that the list is not empty,
2452 after which it becomes empty before we add the new urb to the list, causing us not to
2453 insert the new traffic into the SB list. */
2454
2455 spin_lock_irqsave(&urb_list_lock, flags);
2456 epid = etrax_usb_setup_epid(urb);
2457 if (epid == -1) {
2458 spin_unlock_irqrestore(&urb_list_lock, flags);
2459 DBFEXIT;
2460 return -ENOMEM;
2461 }
2462 empty = urb_list_empty(epid);
2463 urb_list_add(urb, epid);
2464 spin_unlock_irqrestore(&urb_list_lock, flags);
2465
2466 dbg_ctrl("Adding ctrl urb 0x%lx to %s list, epid %d",
2467 (unsigned long)urb, empty ? "empty" : "", epid);
2468
2469 /* Mark the urb as being in progress. */
2470 urb->status = -EINPROGRESS;
2471
2472 /* Setup the hcpriv data. */
2473 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2474 assert(urb_priv != NULL);
2475 /* This sets rx_offset to 0. */
2476 urb_priv->urb_state = NOT_STARTED;
2477 urb->hcpriv = urb_priv;
2478
2479 if (empty) {
2480 etrax_usb_add_to_ctrl_sb_list(urb, epid);
2481 }
2482
2483 DBFEXIT;
2484
2485 return 0;
2486}
2487
2488static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid)
2489{
2490 USB_SB_Desc_t *sb_desc_setup;
2491 USB_SB_Desc_t *sb_desc_data;
2492 USB_SB_Desc_t *sb_desc_status;
2493
2494 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2495
2496 unsigned long flags;
2497 char maxlen;
2498
2499 DBFENTER;
2500
2501 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2502
2503 sb_desc_setup = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2504 assert(sb_desc_setup != NULL);
2505 sb_desc_status = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2506 assert(sb_desc_status != NULL);
2507
2508 /* Initialize the mandatory setup SB descriptor (used only in control transfers) */
2509 sb_desc_setup->sw_len = 8;
2510 sb_desc_setup->command = (IO_FIELD(USB_SB_command, rem, 0) |
2511 IO_STATE(USB_SB_command, tt, setup) |
2512 IO_STATE(USB_SB_command, full, yes) |
2513 IO_STATE(USB_SB_command, eot, yes));
2514
2515 sb_desc_setup->buf = virt_to_phys(urb->setup_packet);
2516
2517 if (usb_pipeout(urb->pipe)) {
2518 dbg_ctrl("Transfer for epid %d is OUT", epid);
2519
2520 /* If this Control OUT transfer has an optional data stage we add an OUT token
2521 before the mandatory IN (status) token, hence the reordered SB list */
2522
2523 sb_desc_setup->next = virt_to_phys(sb_desc_status);
2524 if (urb->transfer_buffer) {
2525
2526 dbg_ctrl("This OUT transfer has an extra data stage");
2527
2528 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2529 assert(sb_desc_data != NULL);
2530
2531 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2532
2533 sb_desc_data->sw_len = urb->transfer_buffer_length;
2534 sb_desc_data->command = (IO_STATE(USB_SB_command, tt, out) |
2535 IO_STATE(USB_SB_command, full, yes) |
2536 IO_STATE(USB_SB_command, eot, yes));
2537 sb_desc_data->buf = virt_to_phys(urb->transfer_buffer);
2538 sb_desc_data->next = virt_to_phys(sb_desc_status);
2539 }
2540
2541 sb_desc_status->sw_len = 1;
2542 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2543 IO_STATE(USB_SB_command, tt, in) |
2544 IO_STATE(USB_SB_command, eot, yes) |
2545 IO_STATE(USB_SB_command, intr, yes) |
2546 IO_STATE(USB_SB_command, eol, yes));
2547
2548 sb_desc_status->buf = 0;
2549 sb_desc_status->next = 0;
2550
2551 } else if (usb_pipein(urb->pipe)) {
2552
2553 dbg_ctrl("Transfer for epid %d is IN", epid);
2554 dbg_ctrl("transfer_buffer_length = %d", urb->transfer_buffer_length);
2555 dbg_ctrl("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
2556
2557 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2558 assert(sb_desc_data != NULL);
2559
2560 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2561
2562 sb_desc_data->sw_len = urb->transfer_buffer_length ?
2563 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2564 dbg_ctrl("sw_len got %d", sb_desc_data->sw_len);
2565
2566 sb_desc_data->command =
2567 (IO_FIELD(USB_SB_command, rem,
2568 urb->transfer_buffer_length % maxlen) |
2569 IO_STATE(USB_SB_command, tt, in) |
2570 IO_STATE(USB_SB_command, eot, yes));
2571
2572 sb_desc_data->buf = 0;
2573 sb_desc_data->next = virt_to_phys(sb_desc_status);
2574
2575 /* Read comment at zout_buffer declaration for an explanation to this. */
2576 sb_desc_status->sw_len = 1;
2577 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2578 IO_STATE(USB_SB_command, tt, zout) |
2579 IO_STATE(USB_SB_command, full, yes) |
2580 IO_STATE(USB_SB_command, eot, yes) |
2581 IO_STATE(USB_SB_command, intr, yes) |
2582 IO_STATE(USB_SB_command, eol, yes));
2583
2584 sb_desc_status->buf = virt_to_phys(&zout_buffer[0]);
2585 sb_desc_status->next = 0;
2586 }
2587
2588 urb_priv->first_sb = sb_desc_setup;
2589 urb_priv->last_sb = sb_desc_status;
2590 urb_priv->epid = epid;
2591
2592 urb_priv->urb_state = STARTED;
2593
2594 /* Reset toggle bits and reset error count, remember to di and ei */
2595 /* Warning: it is possible that this locking doesn't work with bottom-halves */
2596
2597 save_flags(flags);
2598 cli();
2599
2600 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2601 nop();
2602 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2603 panic("Hold was set in %s", __FUNCTION__);
2604 }
2605
2606
2607 /* FIXME: Compare with etrax_usb_add_to_bulk_sb_list where the toggle bits
2608 are set to a specific value. Why the difference? Read "Transfer and Toggle Bits
2609 in Designer's Reference, p. 8 - 11. */
2610 *R_USB_EPT_DATA &=
2611 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2612 IO_MASK(R_USB_EPT_DATA, error_count_out) |
2613 IO_MASK(R_USB_EPT_DATA, t_in) |
2614 IO_MASK(R_USB_EPT_DATA, t_out));
2615
2616 /* Since we use the rx interrupt to complete ctrl urbs, we can enable interrupts now
2617 (i.e. we don't check the sub pointer on an eot interrupt like we do for bulk traffic). */
2618 restore_flags(flags);
2619
2620 /* Assert that the EP descriptor is disabled. */
2621 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2622
2623 /* Set up and enable the EP descriptor. */
2624 TxCtrlEPList[epid].sub = virt_to_phys(sb_desc_setup);
2625 TxCtrlEPList[epid].hw_len = 0;
2626 TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2627
2628 /* We start the DMA sub channel without checking if it's running or not, because:
2629 1) If it's already running, issuing the start command is a nop.
2630 2) We avoid a test-and-set race condition. */
2631 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
2632
2633 DBFEXIT;
2634}
2635
2636static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status)
2637{
2638 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2639 int epid = urb_priv->epid;
2640
2641 DBFENTER;
2642
2643 if (status)
2644 warn("Completing ctrl urb with status %d.", status);
2645
2646 dbg_ctrl("Completing ctrl epid %d, urb 0x%lx", epid, (unsigned long)urb);
2647
2648 /* Remove this urb from the list. */
2649 urb_list_del(urb, epid);
2650
2651 /* For an IN pipe, we always set the actual length, regardless of whether there was
2652 an error or not (which means the device driver can use the data if it wants to). */
2653 if (usb_pipein(urb->pipe)) {
2654 urb->actual_length = urb_priv->rx_offset;
2655 }
2656
2657 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2658 Like, maybe we shouldn't insert more traffic. */
2659
2660 /* Remember to free the SBs. */
2661 etrax_remove_from_sb_list(urb);
2662 kfree(urb_priv);
2663 urb->hcpriv = 0;
2664
2665 /* If there are any more urbs in the list we'd better start sending. */
2666 if (!urb_list_empty(epid)) {
2667 struct urb *new_urb;
2668
2669 /* Get the first urb. */
2670 new_urb = urb_list_first(epid);
2671 assert(new_urb);
2672
2673 dbg_ctrl("More ctrl for epid %d, first urb = 0x%lx", epid, (unsigned long)new_urb);
2674
2675 etrax_usb_add_to_ctrl_sb_list(new_urb, epid);
2676 }
2677
2678 urb->status = status;
2679
2680 /* We let any non-zero status from the layer above have precedence. */
2681 if (status == 0) {
2682 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2683 is to be treated as an error. */
2684 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2685 if (usb_pipein(urb->pipe) &&
2686 (urb->actual_length !=
2687 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2688 urb->status = -EREMOTEIO;
2689 }
2690 }
2691 }
2692
2693 if (urb->complete) {
2694 urb->complete(urb, NULL);
2695 }
2696
2697 if (urb_list_empty(epid)) {
2698 /* No more traffic. Time to clean up. */
2699 etrax_usb_free_epid(epid);
2700 /* Must set sub pointer to 0. */
2701 dbg_ctrl("No ctrl for epid %d", epid);
2702 TxCtrlEPList[epid].sub = 0;
2703 }
2704
2705 DBFEXIT;
2706}
2707
2708static int etrax_usb_submit_intr_urb(struct urb *urb)
2709{
2710
2711 int epid;
2712
2713 DBFENTER;
2714
2715 if (usb_pipeout(urb->pipe)) {
2716 /* Unsupported transfer type.
2717 We don't support interrupt out traffic. (If we do, we can't support
2718 intervals for neither in or out traffic, but are forced to schedule all
2719 interrupt traffic in one frame.) */
2720 return -EINVAL;
2721 }
2722
2723 epid = etrax_usb_setup_epid(urb);
2724 if (epid == -1) {
2725 DBFEXIT;
2726 return -ENOMEM;
2727 }
2728
2729 if (!urb_list_empty(epid)) {
2730 /* There is already a queued urb for this endpoint. */
2731 etrax_usb_free_epid(epid);
2732 return -ENXIO;
2733 }
2734
2735 urb->status = -EINPROGRESS;
2736
2737 dbg_intr("Add intr urb 0x%lx, to list, epid %d", (unsigned long)urb, epid);
2738
2739 urb_list_add(urb, epid);
2740 etrax_usb_add_to_intr_sb_list(urb, epid);
2741
2742 return 0;
2743
2744 DBFEXIT;
2745}
2746
2747static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2748{
2749
2750 volatile USB_EP_Desc_t *tmp_ep;
2751 volatile USB_EP_Desc_t *first_ep;
2752
2753 char maxlen;
2754 int interval;
2755 int i;
2756
2757 etrax_urb_priv_t *urb_priv;
2758
2759 DBFENTER;
2760
2761 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2762 interval = urb->interval;
2763
2764 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2765 assert(urb_priv != NULL);
2766 urb->hcpriv = urb_priv;
2767
2768 first_ep = &TxIntrEPList[0];
2769
2770 /* Round of the interval to 2^n, it is obvious that this code favours
2771 smaller numbers, but that is actually a good thing */
2772 /* FIXME: The "rounding error" for larger intervals will be quite
2773 large. For in traffic this shouldn't be a problem since it will only
2774 mean that we "poll" more often. */
2775 for (i = 0; interval; i++) {
2776 interval = interval >> 1;
2777 }
2778 interval = 1 << (i - 1);
2779
2780 dbg_intr("Interval rounded to %d", interval);
2781
2782 tmp_ep = first_ep;
2783 i = 0;
2784 do {
2785 if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
2786 if ((i % interval) == 0) {
2787 /* Insert the traffic ep after tmp_ep */
2788 USB_EP_Desc_t *ep_desc;
2789 USB_SB_Desc_t *sb_desc;
2790
2791 dbg_intr("Inserting EP for epid %d", epid);
2792
2793 ep_desc = (USB_EP_Desc_t *)
2794 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2795 sb_desc = (USB_SB_Desc_t *)
2796 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2797 assert(ep_desc != NULL);
2798 CHECK_ALIGN(ep_desc);
2799 assert(sb_desc != NULL);
2800
2801 ep_desc->sub = virt_to_phys(sb_desc);
2802 ep_desc->hw_len = 0;
2803 ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
2804 IO_STATE(USB_EP_command, enable, yes));
2805
2806
2807 /* Round upwards the number of packets of size maxlen
2808 that this SB descriptor should receive. */
2809 sb_desc->sw_len = urb->transfer_buffer_length ?
2810 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2811 sb_desc->next = 0;
2812 sb_desc->buf = 0;
2813 sb_desc->command =
2814 (IO_FIELD(USB_SB_command, rem, urb->transfer_buffer_length % maxlen) |
2815 IO_STATE(USB_SB_command, tt, in) |
2816 IO_STATE(USB_SB_command, eot, yes) |
2817 IO_STATE(USB_SB_command, eol, yes));
2818
2819 ep_desc->next = tmp_ep->next;
2820 tmp_ep->next = virt_to_phys(ep_desc);
2821 }
2822 i++;
2823 }
2824 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
2825 } while (tmp_ep != first_ep);
2826
2827
2828 /* Note that first_sb/last_sb doesn't apply to interrupt traffic. */
2829 urb_priv->epid = epid;
2830
2831 /* We start the DMA sub channel without checking if it's running or not, because:
2832 1) If it's already running, issuing the start command is a nop.
2833 2) We avoid a test-and-set race condition. */
2834 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
2835
2836 DBFEXIT;
2837}
2838
2839
2840
2841static void etrax_usb_complete_intr_urb(struct urb *urb, int status)
2842{
2843 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2844 int epid = urb_priv->epid;
2845
2846 DBFENTER;
2847
2848 if (status)
2849 warn("Completing intr urb with status %d.", status);
2850
2851 dbg_intr("Completing intr epid %d, urb 0x%lx", epid, (unsigned long)urb);
2852
2853 urb->status = status;
2854 urb->actual_length = urb_priv->rx_offset;
2855
2856 dbg_intr("interrupt urb->actual_length = %d", urb->actual_length);
2857
2858 /* We let any non-zero status from the layer above have precedence. */
2859 if (status == 0) {
2860 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2861 is to be treated as an error. */
2862 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2863 if (urb->actual_length !=
2864 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
2865 urb->status = -EREMOTEIO;
2866 }
2867 }
2868 }
2869
2870 /* The driver will resubmit the URB so we need to remove it first */
2871 etrax_usb_unlink_urb(urb, 0);
2872 if (urb->complete) {
2873 urb->complete(urb, NULL);
2874 }
2875
2876 DBFEXIT;
2877}
2878
2879
2880static int etrax_usb_submit_isoc_urb(struct urb *urb)
2881{
2882 int epid;
2883 unsigned long flags;
2884
2885 DBFENTER;
2886
2887 dbg_isoc("Submitting isoc urb = 0x%lx", (unsigned long)urb);
2888
2889 /* Epid allocation, empty check and list add must be protected.
2890 Read about this in etrax_usb_submit_ctrl_urb. */
2891
2892 spin_lock_irqsave(&urb_list_lock, flags);
2893 /* Is there an active epid for this urb ? */
2894 epid = etrax_usb_setup_epid(urb);
2895 if (epid == -1) {
2896 DBFEXIT;
2897 spin_unlock_irqrestore(&urb_list_lock, flags);
2898 return -ENOMEM;
2899 }
2900
2901 /* Ok, now we got valid endpoint, lets insert some traffic */
2902
2903 urb->status = -EINPROGRESS;
2904
2905 /* Find the last urb in the URB_List and add this urb after that one.
2906 Also add the traffic, that is do an etrax_usb_add_to_isoc_sb_list. This
2907 is important to make this in "real time" since isochronous traffic is
2908 time sensitive. */
2909
2910 dbg_isoc("Adding isoc urb to (possibly empty) list");
2911 urb_list_add(urb, epid);
2912 etrax_usb_add_to_isoc_sb_list(urb, epid);
2913 spin_unlock_irqrestore(&urb_list_lock, flags);
2914
2915 DBFEXIT;
2916
2917 return 0;
2918}
2919
2920static void etrax_usb_check_error_isoc_ep(const int epid)
2921{
2922 unsigned long int flags;
2923 int error_code;
2924 __u32 r_usb_ept_data;
2925
2926 /* We can't read R_USB_EPID_ATTN here since it would clear the iso_eof,
2927 bulk_eot and epid_attn interrupts. So we just check the status of
2928 the epid without testing if for it in R_USB_EPID_ATTN. */
2929
2930
2931 save_flags(flags);
2932 cli();
2933 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2934 nop();
2935 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
2936 registers, they are located at the same address and are of the same size.
2937 In other words, this read should be ok for isoc also. */
2938 r_usb_ept_data = *R_USB_EPT_DATA;
2939 restore_flags(flags);
2940
2941 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
2942
2943 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
2944 warn("Hold was set for epid %d.", epid);
2945 return;
2946 }
2947
2948 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, no_error)) {
2949
2950 /* This indicates that the SB list of the ept was completed before
2951 new data was appended to it. This is not an error, but indicates
2952 large system or USB load and could possibly cause trouble for
2953 very timing sensitive USB device drivers so we log it.
2954 */
2955 info("Isoc. epid %d disabled with no error", epid);
2956 return;
2957
2958 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, stall)) {
2959 /* Not really a protocol error, just says that the endpoint gave
2960 a stall response. Note that error_code cannot be stall for isoc. */
2961 panic("Isoc traffic cannot stall");
2962
2963 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, bus_error)) {
2964 /* Two devices responded to a transaction request. Must be resolved
2965 by software. FIXME: Reset ports? */
2966 panic("Bus error for epid %d."
2967 " Two devices responded to transaction request",
2968 epid);
2969
2970 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
2971 /* DMA overrun or underrun. */
2972 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
2973
2974 /* It seems that error_code = buffer_error in
2975 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
2976 are the same error. */
2977 }
2978}
2979
2980
2981static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
2982{
2983
2984 int i = 0;
2985
2986 etrax_urb_priv_t *urb_priv;
2987 USB_SB_Desc_t *prev_sb_desc, *next_sb_desc, *temp_sb_desc;
2988
2989 DBFENTER;
2990
2991 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
2992
2993 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
2994 assert(urb_priv != NULL);
2995
2996 urb->hcpriv = urb_priv;
2997 urb_priv->epid = epid;
2998
2999 if (usb_pipeout(urb->pipe)) {
3000
3001 if (urb->number_of_packets == 0) panic("etrax_usb_add_to_isoc_sb_list 0 packets\n");
3002
3003 dbg_isoc("Transfer for epid %d is OUT", epid);
3004 dbg_isoc("%d packets in URB", urb->number_of_packets);
3005
3006 /* Create one SB descriptor for each packet and link them together. */
3007 for (i = 0; i < urb->number_of_packets; i++) {
3008 if (!urb->iso_frame_desc[i].length)
3009 continue;
3010
3011 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_ATOMIC);
3012 assert(next_sb_desc != NULL);
3013
3014 if (urb->iso_frame_desc[i].length > 0) {
3015
3016 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, out) |
3017 IO_STATE(USB_SB_command, eot, yes));
3018
3019 next_sb_desc->sw_len = urb->iso_frame_desc[i].length;
3020 next_sb_desc->buf = virt_to_phys((char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset);
3021
3022 /* Check if full length transfer. */
3023 if (urb->iso_frame_desc[i].length ==
3024 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
3025 next_sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
3026 }
3027 } else {
3028 dbg_isoc("zero len packet");
3029 next_sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
3030 IO_STATE(USB_SB_command, tt, zout) |
3031 IO_STATE(USB_SB_command, eot, yes) |
3032 IO_STATE(USB_SB_command, full, yes));
3033
3034 next_sb_desc->sw_len = 1;
3035 next_sb_desc->buf = virt_to_phys(&zout_buffer[0]);
3036 }
3037
3038 /* First SB descriptor that belongs to this urb */
3039 if (i == 0)
3040 urb_priv->first_sb = next_sb_desc;
3041 else
3042 prev_sb_desc->next = virt_to_phys(next_sb_desc);
3043
3044 prev_sb_desc = next_sb_desc;
3045 }
3046
3047 next_sb_desc->command |= (IO_STATE(USB_SB_command, intr, yes) |
3048 IO_STATE(USB_SB_command, eol, yes));
3049 next_sb_desc->next = 0;
3050 urb_priv->last_sb = next_sb_desc;
3051
3052 } else if (usb_pipein(urb->pipe)) {
3053
3054 dbg_isoc("Transfer for epid %d is IN", epid);
3055 dbg_isoc("transfer_buffer_length = %d", urb->transfer_buffer_length);
3056 dbg_isoc("rem is calculated to %d", urb->iso_frame_desc[urb->number_of_packets - 1].length);
3057
3058 /* Note that in descriptors for periodic traffic are not consumed. This means that
3059 the USB controller never propagates in the SB list. In other words, if there already
3060 is an SB descriptor in the list for this EP we don't have to do anything. */
3061 if (TxIsocEPList[epid].sub == 0) {
3062 dbg_isoc("Isoc traffic not already running, allocating SB");
3063
3064 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_ATOMIC);
3065 assert(next_sb_desc != NULL);
3066
3067 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, in) |
3068 IO_STATE(USB_SB_command, eot, yes) |
3069 IO_STATE(USB_SB_command, eol, yes));
3070
3071 next_sb_desc->next = 0;
3072 next_sb_desc->sw_len = 1; /* Actual number of packets is not relevant
3073 for periodic in traffic as long as it is more
3074 than zero. Set to 1 always. */
3075 next_sb_desc->buf = 0;
3076
3077 /* The rem field is don't care for isoc traffic, so we don't set it. */
3078
3079 /* Only one SB descriptor that belongs to this urb. */
3080 urb_priv->first_sb = next_sb_desc;
3081 urb_priv->last_sb = next_sb_desc;
3082
3083 } else {
3084
3085 dbg_isoc("Isoc traffic already running, just setting first/last_sb");
3086
3087 /* Each EP for isoc in will have only one SB descriptor, setup when submitting the
3088 already active urb. Note that even though we may have several first_sb/last_sb
3089 pointing at the same SB descriptor, they are freed only once (when the list has
3090 become empty). */
3091 urb_priv->first_sb = phys_to_virt(TxIsocEPList[epid].sub);
3092 urb_priv->last_sb = phys_to_virt(TxIsocEPList[epid].sub);
3093 return;
3094 }
3095
3096 }
3097
3098 /* Find the spot to insert this urb and add it. */
3099 if (TxIsocEPList[epid].sub == 0) {
3100 /* First SB descriptor inserted in this list (in or out). */
3101 dbg_isoc("Inserting SB desc first in list");
3102 TxIsocEPList[epid].hw_len = 0;
3103 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3104
3105 } else {
3106 /* Isochronous traffic is already running, insert new traffic last (only out). */
3107 dbg_isoc("Inserting SB desc last in list");
3108 temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
3109 while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
3110 IO_STATE(USB_SB_command, eol, yes)) {
3111 assert(temp_sb_desc->next);
3112 temp_sb_desc = phys_to_virt(temp_sb_desc->next);
3113 }
3114 dbg_isoc("Appending list on desc 0x%p", temp_sb_desc);
3115
3116 /* Next pointer must be set before eol is removed. */
3117 temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
3118 /* Clear the previous end of list flag since there is a new in the
3119 added SB descriptor list. */
3120 temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
3121
3122 if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
3123 /* 8.8.5 in Designer's Reference says we should check for and correct
3124 any errors in the EP here. That should not be necessary if epid_attn
3125 is handled correctly, so we assume all is ok. */
3126 dbg_isoc("EP disabled");
3127 etrax_usb_check_error_isoc_ep(epid);
3128
3129 /* The SB list was exhausted. */
3130 if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
3131 /* The new sublist did not get processed before the EP was
3132 disabled. Setup the EP again. */
3133 dbg_isoc("Set EP sub to new list");
3134 TxIsocEPList[epid].hw_len = 0;
3135 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3136 }
3137 }
3138 }
3139
3140 if (urb->transfer_flags & URB_ISO_ASAP) {
3141 /* The isoc transfer should be started as soon as possible. The start_frame
3142 field is a return value if URB_ISO_ASAP was set. Comparing R_USB_FM_NUMBER
3143 with a USB Chief trace shows that the first isoc IN token is sent 2 frames
3144 later. I'm not sure how this affects usage of the start_frame field by the
3145 device driver, or how it affects things when USB_ISO_ASAP is not set, so
3146 therefore there's no compensation for the 2 frame "lag" here. */
3147 urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3148 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3149 urb_priv->urb_state = STARTED;
3150 dbg_isoc("URB_ISO_ASAP set, urb->start_frame set to %d", urb->start_frame);
3151 } else {
3152 /* Not started yet. */
3153 urb_priv->urb_state = NOT_STARTED;
3154 dbg_isoc("urb_priv->urb_state set to NOT_STARTED");
3155 }
3156
3157 /* We start the DMA sub channel without checking if it's running or not, because:
3158 1) If it's already running, issuing the start command is a nop.
3159 2) We avoid a test-and-set race condition. */
3160 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3161
3162 DBFEXIT;
3163}
3164
3165static void etrax_usb_complete_isoc_urb(struct urb *urb, int status)
3166{
3167 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3168 int epid = urb_priv->epid;
3169 int auto_resubmit = 0;
3170
3171 DBFENTER;
3172 dbg_isoc("complete urb 0x%p, status %d", urb, status);
3173
3174 if (status)
3175 warn("Completing isoc urb with status %d.", status);
3176
3177 if (usb_pipein(urb->pipe)) {
3178 int i;
3179
3180 /* Make that all isoc packets have status and length set before
3181 completing the urb. */
3182 for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++) {
3183 urb->iso_frame_desc[i].actual_length = 0;
3184 urb->iso_frame_desc[i].status = -EPROTO;
3185 }
3186
3187 urb_list_del(urb, epid);
3188
3189 if (!list_empty(&urb_list[epid])) {
3190 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3191 } else {
3192 unsigned long int flags;
3193 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3194 /* The EP was enabled, disable it and wait. */
3195 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
3196
3197 /* Ah, the luxury of busy-wait. */
3198 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
3199 }
3200
3201 etrax_remove_from_sb_list(urb);
3202 TxIsocEPList[epid].sub = 0;
3203 TxIsocEPList[epid].hw_len = 0;
3204
3205 save_flags(flags);
3206 cli();
3207 etrax_usb_free_epid(epid);
3208 restore_flags(flags);
3209 }
3210
3211 urb->hcpriv = 0;
3212 kfree(urb_priv);
3213
3214 /* Release allocated bandwidth. */
3215 usb_release_bandwidth(urb->dev, urb, 0);
3216 } else if (usb_pipeout(urb->pipe)) {
3217 int freed_descr;
3218
3219 dbg_isoc("Isoc out urb complete 0x%p", urb);
3220
3221 /* Update the urb list. */
3222 urb_list_del(urb, epid);
3223
3224 freed_descr = etrax_remove_from_sb_list(urb);
3225 dbg_isoc("freed %d descriptors of %d packets", freed_descr, urb->number_of_packets);
3226 assert(freed_descr == urb->number_of_packets);
3227 urb->hcpriv = 0;
3228 kfree(urb_priv);
3229
3230 /* Release allocated bandwidth. */
3231 usb_release_bandwidth(urb->dev, urb, 0);
3232 }
3233
3234 urb->status = status;
3235 if (urb->complete) {
3236 urb->complete(urb, NULL);
3237 }
3238
3239 if (auto_resubmit) {
3240 /* Check that urb was not unlinked by the complete callback. */
3241 if (__urb_list_entry(urb, epid)) {
3242 /* Move this one down the list. */
3243 urb_list_move_last(urb, epid);
3244
3245 /* Mark the now first urb as started (may already be). */
3246 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3247
3248 /* Must set this to 0 since this urb is still active after
3249 completion. */
3250 urb_priv->isoc_packet_counter = 0;
3251 } else {
3252 warn("(ISOC) automatic resubmit urb 0x%p removed by complete.", urb);
3253 }
3254 }
3255
3256 DBFEXIT;
3257}
3258
3259static void etrax_usb_complete_urb(struct urb *urb, int status)
3260{
3261 switch (usb_pipetype(urb->pipe)) {
3262 case PIPE_BULK:
3263 etrax_usb_complete_bulk_urb(urb, status);
3264 break;
3265 case PIPE_CONTROL:
3266 etrax_usb_complete_ctrl_urb(urb, status);
3267 break;
3268 case PIPE_INTERRUPT:
3269 etrax_usb_complete_intr_urb(urb, status);
3270 break;
3271 case PIPE_ISOCHRONOUS:
3272 etrax_usb_complete_isoc_urb(urb, status);
3273 break;
3274 default:
3275 err("Unknown pipetype");
3276 }
3277}
3278
3279
3280
3281static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc)
3282{
3283 usb_interrupt_registers_t *reg;
3284 unsigned long flags;
3285 __u32 irq_mask;
3286 __u8 status;
3287 __u32 epid_attn;
3288 __u16 port_status_1;
3289 __u16 port_status_2;
3290 __u32 fm_number;
3291
3292 DBFENTER;
3293
3294 /* Read critical registers into local variables, do kmalloc afterwards. */
3295 save_flags(flags);
3296 cli();
3297
3298 irq_mask = *R_USB_IRQ_MASK_READ;
3299 /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that R_USB_STATUS
3300 must be read before R_USB_EPID_ATTN since reading the latter clears the
3301 ourun and perror fields of R_USB_STATUS. */
3302 status = *R_USB_STATUS;
3303
3304 /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn interrupts. */
3305 epid_attn = *R_USB_EPID_ATTN;
3306
3307 /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
3308 port_status interrupt. */
3309 port_status_1 = *R_USB_RH_PORT_STATUS_1;
3310 port_status_2 = *R_USB_RH_PORT_STATUS_2;
3311
3312 /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
3313 /* Note: the lower 11 bits contain the actual frame number, sent with each sof. */
3314 fm_number = *R_USB_FM_NUMBER;
3315
3316 restore_flags(flags);
3317
3318 reg = (usb_interrupt_registers_t *)kmem_cache_alloc(top_half_reg_cache, GFP_ATOMIC);
3319
3320 assert(reg != NULL);
3321
3322 reg->hc = (etrax_hc_t *)vhc;
3323
3324 /* Now put register values into kmalloc'd area. */
3325 reg->r_usb_irq_mask_read = irq_mask;
3326 reg->r_usb_status = status;
3327 reg->r_usb_epid_attn = epid_attn;
3328 reg->r_usb_rh_port_status_1 = port_status_1;
3329 reg->r_usb_rh_port_status_2 = port_status_2;
3330 reg->r_usb_fm_number = fm_number;
3331
3332 INIT_WORK(&reg->usb_bh, etrax_usb_hc_interrupt_bottom_half, reg);
3333 schedule_work(&reg->usb_bh);
3334
3335 DBFEXIT;
3336
3337 return IRQ_HANDLED;
3338}
3339
3340static void etrax_usb_hc_interrupt_bottom_half(void *data)
3341{
3342 usb_interrupt_registers_t *reg = (usb_interrupt_registers_t *)data;
3343 __u32 irq_mask = reg->r_usb_irq_mask_read;
3344
3345 DBFENTER;
3346
3347 /* Interrupts are handled in order of priority. */
3348 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
3349 etrax_usb_hc_epid_attn_interrupt(reg);
3350 }
3351 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
3352 etrax_usb_hc_port_status_interrupt(reg);
3353 }
3354 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
3355 etrax_usb_hc_ctl_status_interrupt(reg);
3356 }
3357 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
3358 etrax_usb_hc_isoc_eof_interrupt();
3359 }
3360 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
3361 /* Update/restart the bulk start timer since obviously the channel is running. */
3362 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
3363 /* Update/restart the bulk eot timer since we just received an bulk eot interrupt. */
3364 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3365
3366 etrax_usb_hc_bulk_eot_interrupt(0);
3367 }
3368
3369 kmem_cache_free(top_half_reg_cache, reg);
3370
3371 DBFEXIT;
3372}
3373
3374
3375void etrax_usb_hc_isoc_eof_interrupt(void)
3376{
3377 struct urb *urb;
3378 etrax_urb_priv_t *urb_priv;
3379 int epid;
3380 unsigned long flags;
3381
3382 DBFENTER;
3383
3384 /* Do not check the invalid epid (it has a valid sub pointer). */
3385 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
3386
3387 /* Do not check the invalid epid (it has a valid sub pointer). */
3388 if ((epid == DUMMY_EPID) || (epid == INVALID_EPID))
3389 continue;
3390
3391 /* Disable interrupts to block the isoc out descriptor interrupt handler
3392 from being called while the isoc EPID list is being checked.
3393 */
3394 save_flags(flags);
3395 cli();
3396
3397 if (TxIsocEPList[epid].sub == 0) {
3398 /* Nothing here to see. */
3399 restore_flags(flags);
3400 continue;
3401 }
3402
3403 /* Get the first urb (if any). */
3404 urb = urb_list_first(epid);
3405 if (urb == 0) {
3406 warn("Ignoring NULL urb");
3407 restore_flags(flags);
3408 continue;
3409 }
3410 if (usb_pipein(urb->pipe)) {
3411
3412 /* Sanity check. */
3413 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
3414
3415 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3416 assert(urb_priv);
3417
3418 if (urb_priv->urb_state == NOT_STARTED) {
3419
3420 /* If ASAP is not set and urb->start_frame is the current frame,
3421 start the transfer. */
3422 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3423 (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
3424
3425 dbg_isoc("Enabling isoc IN EP descr for epid %d", epid);
3426 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3427
3428 /* This urb is now active. */
3429 urb_priv->urb_state = STARTED;
3430 continue;
3431 }
3432 }
3433 }
3434 restore_flags(flags);
3435 }
3436
3437 DBFEXIT;
3438
3439}
3440
3441void etrax_usb_hc_bulk_eot_interrupt(int timer_induced)
3442{
3443 int epid;
3444
3445 /* The technique is to run one urb at a time, wait for the eot interrupt at which
3446 point the EP descriptor has been disabled. */
3447
3448 DBFENTER;
3449 dbg_bulk("bulk eot%s", timer_induced ? ", called by timer" : "");
3450
3451 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3452
3453 if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
3454 (TxBulkEPList[epid].sub != 0)) {
3455
3456 struct urb *urb;
3457 etrax_urb_priv_t *urb_priv;
3458 unsigned long flags;
3459 __u32 r_usb_ept_data;
3460
3461 /* Found a disabled EP descriptor which has a non-null sub pointer.
3462 Verify that this ctrl EP descriptor got disabled no errors.
3463 FIXME: Necessary to check error_code? */
3464 dbg_bulk("for epid %d?", epid);
3465
3466 /* Get the first urb. */
3467 urb = urb_list_first(epid);
3468
3469 /* FIXME: Could this happen for valid reasons? Why did it disappear? Because of
3470 wrong unlinking? */
3471 if (!urb) {
3472 warn("NULL urb for epid %d", epid);
3473 continue;
3474 }
3475
3476 assert(urb);
3477 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3478 assert(urb_priv);
3479
3480 /* Sanity checks. */
3481 assert(usb_pipetype(urb->pipe) == PIPE_BULK);
3482 if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
3483 err("bulk endpoint got disabled before reaching last sb");
3484 }
3485
3486 /* For bulk IN traffic, there seems to be a race condition between
3487 between the bulk eot and eop interrupts, or rather an uncertainty regarding
3488 the order in which they happen. Normally we expect the eop interrupt from
3489 DMA channel 9 to happen before the eot interrupt.
3490
3491 Therefore, we complete the bulk IN urb in the rx interrupt handler instead. */
3492
3493 if (usb_pipein(urb->pipe)) {
3494 dbg_bulk("in urb, continuing");
3495 continue;
3496 }
3497
3498 save_flags(flags);
3499 cli();
3500 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3501 nop();
3502 r_usb_ept_data = *R_USB_EPT_DATA;
3503 restore_flags(flags);
3504
3505 if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
3506 IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3507 /* This means that the endpoint has no error, is disabled
3508 and had inserted traffic, i.e. transfer successfully completed. */
3509 etrax_usb_complete_bulk_urb(urb, 0);
3510 } else {
3511 /* Shouldn't happen. We expect errors to be caught by epid attention. */
3512 err("Found disabled bulk EP desc, error_code != no_error");
3513 }
3514 }
3515 }
3516
3517 /* Normally, we should find (at least) one disabled EP descriptor with a valid sub pointer.
3518 However, because of the uncertainty in the deliverance of the eop/eot interrupts, we may
3519 not. Also, we might find two disabled EPs when handling an eot interrupt, and then find
3520 none the next time. */
3521
3522 DBFEXIT;
3523
3524}
3525
3526void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg)
3527{
3528 /* This function handles the epid attention interrupt. There are a variety of reasons
3529 for this interrupt to happen (Designer's Reference, p. 8 - 22 for the details):
3530
3531 invalid ep_id - Invalid epid in an EP (EP disabled).
3532 stall - Not strictly an error condition (EP disabled).
3533 3rd error - Three successive transaction errors (EP disabled).
3534 buffer ourun - Buffer overrun or underrun (EP disabled).
3535 past eof1 - Intr or isoc transaction proceeds past EOF1.
3536 near eof - Intr or isoc transaction would not fit inside the frame.
3537 zout transfer - If zout transfer for a bulk endpoint (EP disabled).
3538 setup transfer - If setup transfer for a non-ctrl endpoint (EP disabled). */
3539
3540 int epid;
3541
3542
3543 DBFENTER;
3544
3545 assert(reg != NULL);
3546
3547 /* Note that we loop through all epids. We still want to catch errors for
3548 the invalid one, even though we might handle them differently. */
3549 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3550
3551 if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
3552
3553 struct urb *urb;
3554 __u32 r_usb_ept_data;
3555 unsigned long flags;
3556 int error_code;
3557
3558 save_flags(flags);
3559 cli();
3560 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3561 nop();
3562 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
3563 registers, they are located at the same address and are of the same size.
3564 In other words, this read should be ok for isoc also. */
3565 r_usb_ept_data = *R_USB_EPT_DATA;
3566 restore_flags(flags);
3567
3568 /* First some sanity checks. */
3569 if (epid == INVALID_EPID) {
3570 /* FIXME: What if it became disabled? Could seriously hurt interrupt
3571 traffic. (Use do_intr_recover.) */
3572 warn("Got epid_attn for INVALID_EPID (%d).", epid);
3573 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3574 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3575 continue;
3576 } else if (epid == DUMMY_EPID) {
3577 /* We definitely don't care about these ones. Besides, they are
3578 always disabled, so any possible disabling caused by the
3579 epid attention interrupt is irrelevant. */
3580 warn("Got epid_attn for DUMMY_EPID (%d).", epid);
3581 continue;
3582 }
3583
3584 /* Get the first urb in the urb list for this epid. We blatantly assume
3585 that only the first urb could have caused the epid attention.
3586 (For bulk and ctrl, only one urb is active at any one time. For intr
3587 and isoc we remove them once they are completed.) */
3588 urb = urb_list_first(epid);
3589
3590 if (urb == NULL) {
3591 err("Got epid_attn for epid %i with no urb.", epid);
3592 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3593 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3594 continue;
3595 }
3596
3597 switch (usb_pipetype(urb->pipe)) {
3598 case PIPE_BULK:
3599 warn("Got epid attn for bulk endpoint, epid %d", epid);
3600 break;
3601 case PIPE_CONTROL:
3602 warn("Got epid attn for control endpoint, epid %d", epid);
3603 break;
3604 case PIPE_INTERRUPT:
3605 warn("Got epid attn for interrupt endpoint, epid %d", epid);
3606 break;
3607 case PIPE_ISOCHRONOUS:
3608 warn("Got epid attn for isochronous endpoint, epid %d", epid);
3609 break;
3610 }
3611
3612 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
3613 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
3614 warn("Hold was set for epid %d.", epid);
3615 continue;
3616 }
3617 }
3618
3619 /* Even though error_code occupies bits 22 - 23 in both R_USB_EPT_DATA and
3620 R_USB_EPT_DATA_ISOC, we separate them here so we don't forget in other places. */
3621 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3622 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
3623 } else {
3624 error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data);
3625 }
3626
3627 /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
3628 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3629
3630 /* Isoc traffic doesn't have error_count_in/error_count_out. */
3631 if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
3632 (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, r_usb_ept_data) == 3 ||
3633 IO_EXTRACT(R_USB_EPT_DATA, error_count_out, r_usb_ept_data) == 3)) {
3634 /* 3rd error. */
3635 warn("3rd error for epid %i", epid);
3636 etrax_usb_complete_urb(urb, -EPROTO);
3637
3638 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3639
3640 warn("Perror for epid %d", epid);
3641
3642 if (!(r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
3643 /* invalid ep_id */
3644 panic("Perror because of invalid epid."
3645 " Deconfigured too early?");
3646 } else {
3647 /* past eof1, near eof, zout transfer, setup transfer */
3648
3649 /* Dump the urb and the relevant EP descriptor list. */
3650
3651 __dump_urb(urb);
3652 __dump_ept_data(epid);
3653 __dump_ep_list(usb_pipetype(urb->pipe));
3654
3655 panic("Something wrong with DMA descriptor contents."
3656 " Too much traffic inserted?");
3657 }
3658 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3659 /* buffer ourun */
3660 panic("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3661 }
3662
3663 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, stall)) {
3664 /* Not really a protocol error, just says that the endpoint gave
3665 a stall response. Note that error_code cannot be stall for isoc. */
3666 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3667 panic("Isoc traffic cannot stall");
3668 }
3669
3670 warn("Stall for epid %d", epid);
3671 etrax_usb_complete_urb(urb, -EPIPE);
3672
3673 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, bus_error)) {
3674 /* Two devices responded to a transaction request. Must be resolved
3675 by software. FIXME: Reset ports? */
3676 panic("Bus error for epid %d."
3677 " Two devices responded to transaction request",
3678 epid);
3679
3680 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
3681 /* DMA overrun or underrun. */
3682 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3683
3684 /* It seems that error_code = buffer_error in
3685 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
3686 are the same error. */
3687 etrax_usb_complete_urb(urb, -EPROTO);
3688 }
3689 }
3690 }
3691
3692 DBFEXIT;
3693
3694}
3695
3696void etrax_usb_bulk_start_timer_func(unsigned long dummy)
3697{
3698
3699 /* We might enable an EP descriptor behind the current DMA position when it's about
3700 to decide that there are no more bulk traffic and it should stop the bulk channel.
3701 Therefore we periodically check if the bulk channel is stopped and there is an
3702 enabled bulk EP descriptor, in which case we start the bulk channel. */
3703 dbg_bulk("bulk_start_timer timed out.");
3704
3705 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
3706 int epid;
3707
3708 dbg_bulk("Bulk DMA channel not running.");
3709
3710 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3711 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3712 dbg_bulk("Found enabled EP for epid %d, starting bulk channel.\n",
3713 epid);
3714 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
3715
3716 /* Restart the bulk eot timer since we just started the bulk channel. */
3717 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3718
3719 /* No need to search any further. */
3720 break;
3721 }
3722 }
3723 } else {
3724 dbg_bulk("Bulk DMA channel running.");
3725 }
3726}
3727
3728void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg)
3729{
3730 etrax_hc_t *hc = reg->hc;
3731 __u16 r_usb_rh_port_status_1 = reg->r_usb_rh_port_status_1;
3732 __u16 r_usb_rh_port_status_2 = reg->r_usb_rh_port_status_2;
3733
3734 DBFENTER;
3735
3736 /* The Etrax RH does not include a wPortChange register, so this has to be handled in software
3737 (by saving the old port status value for comparison when the port status interrupt happens).
3738 See section 11.16.2.6.2 in the USB 1.1 spec for details. */
3739
3740 dbg_rh("hc->rh.prev_wPortStatus_1 = 0x%x", hc->rh.prev_wPortStatus_1);
3741 dbg_rh("hc->rh.prev_wPortStatus_2 = 0x%x", hc->rh.prev_wPortStatus_2);
3742 dbg_rh("r_usb_rh_port_status_1 = 0x%x", r_usb_rh_port_status_1);
3743 dbg_rh("r_usb_rh_port_status_2 = 0x%x", r_usb_rh_port_status_2);
3744
3745 /* C_PORT_CONNECTION is set on any transition. */
3746 hc->rh.wPortChange_1 |=
3747 ((r_usb_rh_port_status_1 & (1 << RH_PORT_CONNECTION)) !=
3748 (hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_CONNECTION))) ?
3749 (1 << RH_PORT_CONNECTION) : 0;
3750
3751 hc->rh.wPortChange_2 |=
3752 ((r_usb_rh_port_status_2 & (1 << RH_PORT_CONNECTION)) !=
3753 (hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_CONNECTION))) ?
3754 (1 << RH_PORT_CONNECTION) : 0;
3755
3756 /* C_PORT_ENABLE is _only_ set on a one to zero transition, i.e. when
3757 the port is disabled, not when it's enabled. */
3758 hc->rh.wPortChange_1 |=
3759 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_ENABLE))
3760 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
3761 (1 << RH_PORT_ENABLE) : 0;
3762
3763 hc->rh.wPortChange_2 |=
3764 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_ENABLE))
3765 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
3766 (1 << RH_PORT_ENABLE) : 0;
3767
3768 /* C_PORT_SUSPEND is set to one when the device has transitioned out
3769 of the suspended state, i.e. when suspend goes from one to zero. */
3770 hc->rh.wPortChange_1 |=
3771 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_SUSPEND))
3772 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_SUSPEND))) ?
3773 (1 << RH_PORT_SUSPEND) : 0;
3774
3775 hc->rh.wPortChange_2 |=
3776 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_SUSPEND))
3777 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_SUSPEND))) ?
3778 (1 << RH_PORT_SUSPEND) : 0;
3779
3780
3781 /* C_PORT_RESET is set when reset processing on this port is complete. */
3782 hc->rh.wPortChange_1 |=
3783 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_RESET))
3784 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_RESET))) ?
3785 (1 << RH_PORT_RESET) : 0;
3786
3787 hc->rh.wPortChange_2 |=
3788 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_RESET))
3789 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_RESET))) ?
3790 (1 << RH_PORT_RESET) : 0;
3791
3792 /* Save the new values for next port status change. */
3793 hc->rh.prev_wPortStatus_1 = r_usb_rh_port_status_1;
3794 hc->rh.prev_wPortStatus_2 = r_usb_rh_port_status_2;
3795
3796 dbg_rh("hc->rh.wPortChange_1 set to 0x%x", hc->rh.wPortChange_1);
3797 dbg_rh("hc->rh.wPortChange_2 set to 0x%x", hc->rh.wPortChange_2);
3798
3799 DBFEXIT;
3800
3801}
3802
3803void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg)
3804{
3805 DBFENTER;
3806
3807 /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
3808 list for the corresponding epid? */
3809 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3810 panic("USB controller got ourun.");
3811 }
3812 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3813
3814 /* Before, etrax_usb_do_intr_recover was called on this epid if it was
3815 an interrupt pipe. I don't see how re-enabling all EP descriptors
3816 will help if there was a programming error. */
3817 panic("USB controller got perror.");
3818 }
3819
3820 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
3821 /* We should never operate in device mode. */
3822 panic("USB controller in device mode.");
3823 }
3824
3825 /* These if-statements could probably be nested. */
3826 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, host_mode)) {
3827 info("USB controller in host mode.");
3828 }
3829 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, started)) {
3830 info("USB controller started.");
3831 }
3832 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, running)) {
3833 info("USB controller running.");
3834 }
3835
3836 DBFEXIT;
3837
3838}
3839
3840
3841static int etrax_rh_submit_urb(struct urb *urb)
3842{
3843 struct usb_device *usb_dev = urb->dev;
3844 etrax_hc_t *hc = usb_dev->bus->hcpriv;
3845 unsigned int pipe = urb->pipe;
3846 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
3847 void *data = urb->transfer_buffer;
3848 int leni = urb->transfer_buffer_length;
3849 int len = 0;
3850 int stat = 0;
3851
3852 __u16 bmRType_bReq;
3853 __u16 wValue;
3854 __u16 wIndex;
3855 __u16 wLength;
3856
3857 DBFENTER;
3858
3859 /* FIXME: What is this interrupt urb that is sent to the root hub? */
3860 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
3861 dbg_rh("Root-Hub submit IRQ: every %d ms", urb->interval);
3862 hc->rh.urb = urb;
3863 hc->rh.send = 1;
3864 /* FIXME: We could probably remove this line since it's done
3865 in etrax_rh_init_int_timer. (Don't remove it from
3866 etrax_rh_init_int_timer though.) */
3867 hc->rh.interval = urb->interval;
3868 etrax_rh_init_int_timer(urb);
3869 DBFEXIT;
3870
3871 return 0;
3872 }
3873
3874 bmRType_bReq = cmd->bRequestType | (cmd->bRequest << 8);
3875 wValue = le16_to_cpu(cmd->wValue);
3876 wIndex = le16_to_cpu(cmd->wIndex);
3877 wLength = le16_to_cpu(cmd->wLength);
3878
3879 dbg_rh("bmRType_bReq : 0x%04x (%d)", bmRType_bReq, bmRType_bReq);
3880 dbg_rh("wValue : 0x%04x (%d)", wValue, wValue);
3881 dbg_rh("wIndex : 0x%04x (%d)", wIndex, wIndex);
3882 dbg_rh("wLength : 0x%04x (%d)", wLength, wLength);
3883
3884 switch (bmRType_bReq) {
3885
3886 /* Request Destination:
3887 without flags: Device,
3888 RH_INTERFACE: interface,
3889 RH_ENDPOINT: endpoint,
3890 RH_CLASS means HUB here,
3891 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
3892 */
3893
3894 case RH_GET_STATUS:
3895 *(__u16 *) data = cpu_to_le16 (1);
3896 OK (2);
3897
3898 case RH_GET_STATUS | RH_INTERFACE:
3899 *(__u16 *) data = cpu_to_le16 (0);
3900 OK (2);
3901
3902 case RH_GET_STATUS | RH_ENDPOINT:
3903 *(__u16 *) data = cpu_to_le16 (0);
3904 OK (2);
3905
3906 case RH_GET_STATUS | RH_CLASS:
3907 *(__u32 *) data = cpu_to_le32 (0);
3908 OK (4); /* hub power ** */
3909
3910 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
3911 if (wIndex == 1) {
3912 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_1);
3913 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_1);
3914 } else if (wIndex == 2) {
3915 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_2);
3916 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_2);
3917 } else {
3918 dbg_rh("RH_GET_STATUS whith invalid wIndex!");
3919 OK(0);
3920 }
3921
3922 OK(4);
3923
3924 case RH_CLEAR_FEATURE | RH_ENDPOINT:
3925 switch (wValue) {
3926 case (RH_ENDPOINT_STALL):
3927 OK (0);
3928 }
3929 break;
3930
3931 case RH_CLEAR_FEATURE | RH_CLASS:
3932 switch (wValue) {
3933 case (RH_C_HUB_OVER_CURRENT):
3934 OK (0); /* hub power over current ** */
3935 }
3936 break;
3937
3938 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
3939 switch (wValue) {
3940 case (RH_PORT_ENABLE):
3941 if (wIndex == 1) {
3942
3943 dbg_rh("trying to do disable port 1");
3944
3945 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
3946
3947 while (hc->rh.prev_wPortStatus_1 &
3948 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes));
3949 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
3950 dbg_rh("Port 1 is disabled");
3951
3952 } else if (wIndex == 2) {
3953
3954 dbg_rh("trying to do disable port 2");
3955
3956 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
3957
3958 while (hc->rh.prev_wPortStatus_2 &
3959 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes));
3960 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
3961 dbg_rh("Port 2 is disabled");
3962
3963 } else {
3964 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_ENABLE "
3965 "with invalid wIndex == %d!", wIndex);
3966 }
3967
3968 OK (0);
3969 case (RH_PORT_SUSPEND):
3970 /* Opposite to suspend should be resume, so we'll do a resume. */
3971 /* FIXME: USB 1.1, 11.16.2.2 says:
3972 "Clearing the PORT_SUSPEND feature causes a host-initiated resume
3973 on the specified port. If the port is not in the Suspended state,
3974 the hub should treat this request as a functional no-operation."
3975 Shouldn't we check if the port is in a suspended state before
3976 resuming? */
3977
3978 /* Make sure the controller isn't busy. */
3979 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
3980
3981 if (wIndex == 1) {
3982 *R_USB_COMMAND =
3983 IO_STATE(R_USB_COMMAND, port_sel, port1) |
3984 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3985 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
3986 } else if (wIndex == 2) {
3987 *R_USB_COMMAND =
3988 IO_STATE(R_USB_COMMAND, port_sel, port2) |
3989 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3990 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
3991 } else {
3992 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_SUSPEND "
3993 "with invalid wIndex == %d!", wIndex);
3994 }
3995
3996 OK (0);
3997 case (RH_PORT_POWER):
3998 OK (0); /* port power ** */
3999 case (RH_C_PORT_CONNECTION):
4000 if (wIndex == 1) {
4001 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_CONNECTION);
4002 } else if (wIndex == 2) {
4003 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_CONNECTION);
4004 } else {
4005 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_CONNECTION "
4006 "with invalid wIndex == %d!", wIndex);
4007 }
4008
4009 OK (0);
4010 case (RH_C_PORT_ENABLE):
4011 if (wIndex == 1) {
4012 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_ENABLE);
4013 } else if (wIndex == 2) {
4014 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_ENABLE);
4015 } else {
4016 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_ENABLE "
4017 "with invalid wIndex == %d!", wIndex);
4018 }
4019 OK (0);
4020 case (RH_C_PORT_SUSPEND):
4021/*** WR_RH_PORTSTAT(RH_PS_PSSC); */
4022 OK (0);
4023 case (RH_C_PORT_OVER_CURRENT):
4024 OK (0); /* port power over current ** */
4025 case (RH_C_PORT_RESET):
4026 if (wIndex == 1) {
4027 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_RESET);
4028 } else if (wIndex == 2) {
4029 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_RESET);
4030 } else {
4031 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_RESET "
4032 "with invalid index == %d!", wIndex);
4033 }
4034
4035 OK (0);
4036
4037 }
4038 break;
4039
4040 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
4041 switch (wValue) {
4042 case (RH_PORT_SUSPEND):
4043
4044 /* Make sure the controller isn't busy. */
4045 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4046
4047 if (wIndex == 1) {
4048 *R_USB_COMMAND =
4049 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4050 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4051 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4052 } else if (wIndex == 2) {
4053 *R_USB_COMMAND =
4054 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4055 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4056 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4057 } else {
4058 dbg_rh("RH_SET_FEATURE->RH_PORT_SUSPEND "
4059 "with invalid wIndex == %d!", wIndex);
4060 }
4061
4062 OK (0);
4063 case (RH_PORT_RESET):
4064 if (wIndex == 1) {
4065
4066 port_1_reset:
4067 dbg_rh("Doing reset of port 1");
4068
4069 /* Make sure the controller isn't busy. */
4070 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4071
4072 *R_USB_COMMAND =
4073 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4074 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4075 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4076
4077 /* We must wait at least 10 ms for the device to recover.
4078 15 ms should be enough. */
4079 udelay(15000);
4080
4081 /* Wait for reset bit to go low (should be done by now). */
4082 while (hc->rh.prev_wPortStatus_1 &
4083 IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes));
4084
4085 /* If the port status is
4086 1) connected and enabled then there is a device and everything is fine
4087 2) neither connected nor enabled then there is no device, also fine
4088 3) connected and not enabled then we try again
4089 (Yes, there are other port status combinations besides these.) */
4090
4091 if ((hc->rh.prev_wPortStatus_1 &
4092 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4093 (hc->rh.prev_wPortStatus_1 &
4094 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4095 dbg_rh("Connected device on port 1, but port not enabled?"
4096 " Trying reset again.");
4097 goto port_2_reset;
4098 }
4099
4100 /* Diagnostic printouts. */
4101 if ((hc->rh.prev_wPortStatus_1 &
4102 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, no)) &&
4103 (hc->rh.prev_wPortStatus_1 &
4104 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4105 dbg_rh("No connected device on port 1");
4106 } else if ((hc->rh.prev_wPortStatus_1 &
4107 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4108 (hc->rh.prev_wPortStatus_1 &
4109 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes))) {
4110 dbg_rh("Connected device on port 1, port 1 enabled");
4111 }
4112
4113 } else if (wIndex == 2) {
4114
4115 port_2_reset:
4116 dbg_rh("Doing reset of port 2");
4117
4118 /* Make sure the controller isn't busy. */
4119 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4120
4121 /* Issue the reset command. */
4122 *R_USB_COMMAND =
4123 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4124 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4125 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4126
4127 /* We must wait at least 10 ms for the device to recover.
4128 15 ms should be enough. */
4129 udelay(15000);
4130
4131 /* Wait for reset bit to go low (should be done by now). */
4132 while (hc->rh.prev_wPortStatus_2 &
4133 IO_STATE(R_USB_RH_PORT_STATUS_2, reset, yes));
4134
4135 /* If the port status is
4136 1) connected and enabled then there is a device and everything is fine
4137 2) neither connected nor enabled then there is no device, also fine
4138 3) connected and not enabled then we try again
4139 (Yes, there are other port status combinations besides these.) */
4140
4141 if ((hc->rh.prev_wPortStatus_2 &
4142 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4143 (hc->rh.prev_wPortStatus_2 &
4144 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4145 dbg_rh("Connected device on port 2, but port not enabled?"
4146 " Trying reset again.");
4147 goto port_2_reset;
4148 }
4149
4150 /* Diagnostic printouts. */
4151 if ((hc->rh.prev_wPortStatus_2 &
4152 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, no)) &&
4153 (hc->rh.prev_wPortStatus_2 &
4154 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4155 dbg_rh("No connected device on port 2");
4156 } else if ((hc->rh.prev_wPortStatus_2 &
4157 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4158 (hc->rh.prev_wPortStatus_2 &
4159 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes))) {
4160 dbg_rh("Connected device on port 2, port 2 enabled");
4161 }
4162
4163 } else {
4164 dbg_rh("RH_SET_FEATURE->RH_PORT_RESET with invalid wIndex = %d", wIndex);
4165 }
4166
4167 /* Make sure the controller isn't busy. */
4168 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4169
4170 /* If all enabled ports were disabled the host controller goes down into
4171 started mode, so we need to bring it back into the running state.
4172 (This is safe even if it's already in the running state.) */
4173 *R_USB_COMMAND =
4174 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4175 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4176 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4177
4178 dbg_rh("...Done");
4179 OK(0);
4180
4181 case (RH_PORT_POWER):
4182 OK (0); /* port power ** */
4183 case (RH_PORT_ENABLE):
4184 /* There is no port enable command in the host controller, so if the
4185 port is already enabled, we do nothing. If not, we reset the port
4186 (with an ugly goto). */
4187
4188 if (wIndex == 1) {
4189 if (hc->rh.prev_wPortStatus_1 &
4190 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no)) {
4191 goto port_1_reset;
4192 }
4193 } else if (wIndex == 2) {
4194 if (hc->rh.prev_wPortStatus_2 &
4195 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no)) {
4196 goto port_2_reset;
4197 }
4198 } else {
4199 dbg_rh("RH_SET_FEATURE->RH_GET_STATUS with invalid wIndex = %d", wIndex);
4200 }
4201 OK (0);
4202 }
4203 break;
4204
4205 case RH_SET_ADDRESS:
4206 hc->rh.devnum = wValue;
4207 dbg_rh("RH address set to: %d", hc->rh.devnum);
4208 OK (0);
4209
4210 case RH_GET_DESCRIPTOR:
4211 switch ((wValue & 0xff00) >> 8) {
4212 case (0x01): /* device descriptor */
4213 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_dev_des), wLength));
4214 memcpy (data, root_hub_dev_des, len);
4215 OK (len);
4216 case (0x02): /* configuration descriptor */
4217 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_config_des), wLength));
4218 memcpy (data, root_hub_config_des, len);
4219 OK (len);
4220 case (0x03): /* string descriptors */
4221 len = usb_root_hub_string (wValue & 0xff,
4222 0xff, "ETRAX 100LX",
4223 data, wLength);
4224 if (len > 0) {
4225 OK(min(leni, len));
4226 } else {
4227 stat = -EPIPE;
4228 }
4229
4230 }
4231 break;
4232
4233 case RH_GET_DESCRIPTOR | RH_CLASS:
4234 root_hub_hub_des[2] = hc->rh.numports;
4235 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
4236 memcpy (data, root_hub_hub_des, len);
4237 OK (len);
4238
4239 case RH_GET_CONFIGURATION:
4240 *(__u8 *) data = 0x01;
4241 OK (1);
4242
4243 case RH_SET_CONFIGURATION:
4244 OK (0);
4245
4246 default:
4247 stat = -EPIPE;
4248 }
4249
4250 urb->actual_length = len;
4251 urb->status = stat;
4252 urb->dev = NULL;
4253 if (urb->complete) {
4254 urb->complete(urb, NULL);
4255 }
4256 DBFEXIT;
4257
4258 return 0;
4259}
4260
4261static void
4262etrax_usb_bulk_eot_timer_func(unsigned long dummy)
4263{
4264 /* Because of a race condition in the top half, we might miss a bulk eot.
4265 This timer "simulates" a bulk eot if we don't get one for a while, hopefully
4266 correcting the situation. */
4267 dbg_bulk("bulk_eot_timer timed out.");
4268 etrax_usb_hc_bulk_eot_interrupt(1);
4269}
4270
4271static void*
4272etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size,
4273 unsigned mem_flags, dma_addr_t *dma)
4274{
4275 return kmalloc(size, mem_flags);
4276}
4277
4278static void
4279etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma)
4280{
4281 kfree(addr);
4282}
4283
4284
4285static struct device fake_device;
4286
4287static int __init etrax_usb_hc_init(void)
4288{
4289 static etrax_hc_t *hc;
4290 struct usb_bus *bus;
4291 struct usb_device *usb_rh;
4292 int i;
4293
4294 DBFENTER;
4295
4296 info("ETRAX 100LX USB-HCD %s (c) 2001-2003 Axis Communications AB\n", usb_hcd_version);
4297
4298 hc = kmalloc(sizeof(etrax_hc_t), GFP_KERNEL);
4299 assert(hc != NULL);
4300
4301 /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
4302 /* Note that we specify sizeof(USB_EP_Desc_t) as the size, but also allocate
4303 SB descriptors from this cache. This is ok since sizeof(USB_EP_Desc_t) ==
4304 sizeof(USB_SB_Desc_t). */
4305
4306 usb_desc_cache = kmem_cache_create("usb_desc_cache", sizeof(USB_EP_Desc_t), 0,
4307 SLAB_HWCACHE_ALIGN, 0, 0);
4308 assert(usb_desc_cache != NULL);
4309
4310 top_half_reg_cache = kmem_cache_create("top_half_reg_cache",
4311 sizeof(usb_interrupt_registers_t),
4312 0, SLAB_HWCACHE_ALIGN, 0, 0);
4313 assert(top_half_reg_cache != NULL);
4314
4315 isoc_compl_cache = kmem_cache_create("isoc_compl_cache",
4316 sizeof(usb_isoc_complete_data_t),
4317 0, SLAB_HWCACHE_ALIGN, 0, 0);
4318 assert(isoc_compl_cache != NULL);
4319
4320 etrax_usb_bus = bus = usb_alloc_bus(&etrax_usb_device_operations);
4321 hc->bus = bus;
4322 bus->bus_name="ETRAX 100LX";
4323 bus->hcpriv = hc;
4324
4325 /* Initialize RH to the default address.
4326 And make sure that we have no status change indication */
4327 hc->rh.numports = 2; /* The RH has two ports */
4328 hc->rh.devnum = 1;
4329 hc->rh.wPortChange_1 = 0;
4330 hc->rh.wPortChange_2 = 0;
4331
4332 /* Also initate the previous values to zero */
4333 hc->rh.prev_wPortStatus_1 = 0;
4334 hc->rh.prev_wPortStatus_2 = 0;
4335
4336 /* Initialize the intr-traffic flags */
4337 /* FIXME: This isn't used. (Besides, the error field isn't initialized.) */
4338 hc->intr.sleeping = 0;
4339 hc->intr.wq = NULL;
4340
4341 epid_usage_bitmask = 0;
4342 epid_out_traffic = 0;
4343
4344 /* Mark the invalid epid as being used. */
4345 set_bit(INVALID_EPID, (void *)&epid_usage_bitmask);
4346 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, INVALID_EPID);
4347 nop();
4348 /* The valid bit should still be set ('invalid' is in our world; not the hardware's). */
4349 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, yes) |
4350 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4351
4352 /* Mark the dummy epid as being used. */
4353 set_bit(DUMMY_EPID, (void *)&epid_usage_bitmask);
4354 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, DUMMY_EPID);
4355 nop();
4356 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, no) |
4357 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4358
4359 /* Initialize the urb list by initiating a head for each list. */
4360 for (i = 0; i < NBR_OF_EPIDS; i++) {
4361 INIT_LIST_HEAD(&urb_list[i]);
4362 }
4363 spin_lock_init(&urb_list_lock);
4364
4365 INIT_LIST_HEAD(&urb_unlink_list);
4366
4367
4368 /* Initiate the bulk start timer. */
4369 init_timer(&bulk_start_timer);
4370 bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
4371 bulk_start_timer.function = etrax_usb_bulk_start_timer_func;
4372 add_timer(&bulk_start_timer);
4373
4374
4375 /* Initiate the bulk eot timer. */
4376 init_timer(&bulk_eot_timer);
4377 bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
4378 bulk_eot_timer.function = etrax_usb_bulk_eot_timer_func;
4379 add_timer(&bulk_eot_timer);
4380
4381 /* Set up the data structures for USB traffic. Note that this must be done before
4382 any interrupt that relies on sane DMA list occurrs. */
4383 init_rx_buffers();
4384 init_tx_bulk_ep();
4385 init_tx_ctrl_ep();
4386 init_tx_intr_ep();
4387 init_tx_isoc_ep();
4388
4389 device_initialize(&fake_device);
4390 kobject_set_name(&fake_device.kobj, "etrax_usb");
4391 kobject_add(&fake_device.kobj);
4392 kobject_uevent(&fake_device.kobj, KOBJ_ADD);
4393 hc->bus->controller = &fake_device;
4394 usb_register_bus(hc->bus);
4395
4396 *R_IRQ_MASK2_SET =
4397 /* Note that these interrupts are not used. */
4398 IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
4399 /* Sub channel 1 (ctrl) descr. interrupts are used. */
4400 IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
4401 IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
4402 /* Sub channel 3 (isoc) descr. interrupts are used. */
4403 IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
4404
4405 /* Note that the dma9_descr interrupt is not used. */
4406 *R_IRQ_MASK2_SET =
4407 IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
4408 IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
4409
4410 /* FIXME: Enable iso_eof only when isoc traffic is running. */
4411 *R_USB_IRQ_MASK_SET =
4412 IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) |
4413 IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
4414 IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
4415 IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
4416 IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
4417
4418
4419 if (request_irq(ETRAX_USB_HC_IRQ, etrax_usb_hc_interrupt_top_half, 0,
4420 "ETRAX 100LX built-in USB (HC)", hc)) {
4421 err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
4422 etrax_usb_hc_cleanup();
4423 DBFEXIT;
4424 return -1;
4425 }
4426
4427 if (request_irq(ETRAX_USB_RX_IRQ, etrax_usb_rx_interrupt, 0,
4428 "ETRAX 100LX built-in USB (Rx)", hc)) {
4429 err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
4430 etrax_usb_hc_cleanup();
4431 DBFEXIT;
4432 return -1;
4433 }
4434
4435 if (request_irq(ETRAX_USB_TX_IRQ, etrax_usb_tx_interrupt, 0,
4436 "ETRAX 100LX built-in USB (Tx)", hc)) {
4437 err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
4438 etrax_usb_hc_cleanup();
4439 DBFEXIT;
4440 return -1;
4441 }
4442
4443 /* R_USB_COMMAND:
4444 USB commands in host mode. The fields in this register should all be
4445 written to in one write. Do not read-modify-write one field at a time. A
4446 write to this register will trigger events in the USB controller and an
4447 incomplete command may lead to unpredictable results, and in worst case
4448 even to a deadlock in the controller.
4449 (Note however that the busy field is read-only, so no need to write to it.) */
4450
4451 /* Check the busy bit before writing to R_USB_COMMAND. */
4452
4453 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4454
4455 /* Reset the USB interface. */
4456 *R_USB_COMMAND =
4457 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4458 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4459 IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
4460
4461 /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to 0x2A30 (10800),
4462 to guarantee that control traffic gets 10% of the bandwidth, and periodic transfer may
4463 allocate the rest (90%). This doesn't work though. Read on for a lenghty explanation.
4464
4465 While there is a difference between rev. 2 and rev. 3 of the ETRAX 100LX regarding the NAK
4466 behaviour, it doesn't solve this problem. What happens is that a control transfer will not
4467 be interrupted in its data stage when PSTART happens (the point at which periodic traffic
4468 is started). Thus, if PSTART is set to 10800 and its IN or OUT token is NAKed until just before
4469 PSTART happens, it will continue the IN/OUT transfer as long as it's ACKed. After it's done,
4470 there may be too little time left for an isochronous transfer, causing an epid attention
4471 interrupt due to perror. The work-around for this is to let the control transfers run at the
4472 end of the frame instead of at the beginning, and will be interrupted just fine if it doesn't
4473 fit into the frame. However, since there will *always* be a control transfer at the beginning
4474 of the frame, regardless of what we set PSTART to, that transfer might be a 64-byte transfer
4475 which consumes up to 15% of the frame, leaving only 85% for periodic traffic. The solution to
4476 this would be to 'dummy allocate' 5% of the frame with the usb_claim_bandwidth function to make
4477 sure that the periodic transfers that are inserted will always fit in the frame.
4478
4479 The idea was suggested that a control transfer could be split up into several 8 byte transfers,
4480 so that it would be interrupted by PSTART, but since this can't be done for an IN transfer this
4481 hasn't been implemented.
4482
4483 The value 11960 is chosen to be just after the SOF token, with a couple of bit times extra
4484 for possible bit stuffing. */
4485
4486 *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
4487
4488#ifdef CONFIG_ETRAX_USB_HOST_PORT1
4489 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
4490#endif
4491
4492#ifdef CONFIG_ETRAX_USB_HOST_PORT2
4493 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
4494#endif
4495
4496 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4497
4498 /* Configure the USB interface as a host controller. */
4499 *R_USB_COMMAND =
4500 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4501 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4502 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
4503
4504 /* Note: Do not reset any ports here. Await the port status interrupts, to have a controlled
4505 sequence of resetting the ports. If we reset both ports now, and there are devices
4506 on both ports, we will get a bus error because both devices will answer the set address
4507 request. */
4508
4509 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4510
4511 /* Start processing of USB traffic. */
4512 *R_USB_COMMAND =
4513 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4514 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4515 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4516
4517 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4518
4519 usb_rh = usb_alloc_dev(NULL, hc->bus, 0);
4520 hc->bus->root_hub = usb_rh;
4521 usb_rh->state = USB_STATE_ADDRESS;
4522 usb_rh->speed = USB_SPEED_FULL;
4523 usb_rh->devnum = 1;
4524 hc->bus->devnum_next = 2;
4525 usb_rh->ep0.desc.wMaxPacketSize = __const_cpu_to_le16(64);
4526 usb_get_device_descriptor(usb_rh, USB_DT_DEVICE_SIZE);
4527 usb_new_device(usb_rh);
4528
4529 DBFEXIT;
4530
4531 return 0;
4532}
4533
4534static void etrax_usb_hc_cleanup(void)
4535{
4536 DBFENTER;
4537
4538 free_irq(ETRAX_USB_HC_IRQ, NULL);
4539 free_irq(ETRAX_USB_RX_IRQ, NULL);
4540 free_irq(ETRAX_USB_TX_IRQ, NULL);
4541
4542 usb_deregister_bus(etrax_usb_bus);
4543
4544 /* FIXME: call kmem_cache_destroy here? */
4545
4546 DBFEXIT;
4547}
4548
4549module_init(etrax_usb_hc_init);
4550module_exit(etrax_usb_hc_cleanup);
diff --git a/drivers/usb/host/hc_crisv10.h b/drivers/usb/host/hc_crisv10.h
deleted file mode 100644
index 62f77111d418..000000000000
--- a/drivers/usb/host/hc_crisv10.h
+++ /dev/null
@@ -1,289 +0,0 @@
1#ifndef __LINUX_ETRAX_USB_H
2#define __LINUX_ETRAX_USB_H
3
4#include <linux/types.h>
5#include <linux/list.h>
6
7typedef struct USB_IN_Desc {
8 volatile __u16 sw_len;
9 volatile __u16 command;
10 volatile unsigned long next;
11 volatile unsigned long buf;
12 volatile __u16 hw_len;
13 volatile __u16 status;
14} USB_IN_Desc_t;
15
16typedef struct USB_SB_Desc {
17 volatile __u16 sw_len;
18 volatile __u16 command;
19 volatile unsigned long next;
20 volatile unsigned long buf;
21 __u32 dummy;
22} USB_SB_Desc_t;
23
24typedef struct USB_EP_Desc {
25 volatile __u16 hw_len;
26 volatile __u16 command;
27 volatile unsigned long sub;
28 volatile unsigned long next;
29 __u32 dummy;
30} USB_EP_Desc_t;
31
32struct virt_root_hub {
33 int devnum;
34 void *urb;
35 void *int_addr;
36 int send;
37 int interval;
38 int numports;
39 struct timer_list rh_int_timer;
40 volatile __u16 wPortChange_1;
41 volatile __u16 wPortChange_2;
42 volatile __u16 prev_wPortStatus_1;
43 volatile __u16 prev_wPortStatus_2;
44};
45
46struct etrax_usb_intr_traffic {
47 int sleeping;
48 int error;
49 struct wait_queue *wq;
50};
51
52typedef struct etrax_usb_hc {
53 struct usb_bus *bus;
54 struct virt_root_hub rh;
55 struct etrax_usb_intr_traffic intr;
56} etrax_hc_t;
57
58typedef enum {
59 STARTED,
60 NOT_STARTED,
61 UNLINK,
62 TRANSFER_DONE,
63 WAITING_FOR_DESCR_INTR
64} etrax_usb_urb_state_t;
65
66
67
68typedef struct etrax_usb_urb_priv {
69 /* The first_sb field is used for freeing all SB descriptors belonging
70 to an urb. The corresponding ep descriptor's sub pointer cannot be
71 used for this since the DMA advances the sub pointer as it processes
72 the sb list. */
73 USB_SB_Desc_t *first_sb;
74 /* The last_sb field referes to the last SB descriptor that belongs to
75 this urb. This is important to know so we can free the SB descriptors
76 that ranges between first_sb and last_sb. */
77 USB_SB_Desc_t *last_sb;
78
79 /* The rx_offset field is used in ctrl and bulk traffic to keep track
80 of the offset in the urb's transfer_buffer where incoming data should be
81 copied to. */
82 __u32 rx_offset;
83
84 /* Counter used in isochronous transfers to keep track of the
85 number of packets received/transmitted. */
86 __u32 isoc_packet_counter;
87
88 /* This field is used to pass information about the urb's current state between
89 the various interrupt handlers (thus marked volatile). */
90 volatile etrax_usb_urb_state_t urb_state;
91
92 /* Connection between the submitted urb and ETRAX epid number */
93 __u8 epid;
94
95 /* The rx_data_list field is used for periodic traffic, to hold
96 received data for later processing in the the complete_urb functions,
97 where the data us copied to the urb's transfer_buffer. Basically, we
98 use this intermediate storage because we don't know when it's safe to
99 reuse the transfer_buffer (FIXME?). */
100 struct list_head rx_data_list;
101} etrax_urb_priv_t;
102
103/* This struct is for passing data from the top half to the bottom half. */
104typedef struct usb_interrupt_registers
105{
106 etrax_hc_t *hc;
107 __u32 r_usb_epid_attn;
108 __u8 r_usb_status;
109 __u16 r_usb_rh_port_status_1;
110 __u16 r_usb_rh_port_status_2;
111 __u32 r_usb_irq_mask_read;
112 __u32 r_usb_fm_number;
113 struct work_struct usb_bh;
114} usb_interrupt_registers_t;
115
116/* This struct is for passing data from the isoc top half to the isoc bottom half. */
117typedef struct usb_isoc_complete_data
118{
119 struct urb *urb;
120 struct work_struct usb_bh;
121} usb_isoc_complete_data_t;
122
123/* This struct holds data we get from the rx descriptors for DMA channel 9
124 for periodic traffic (intr and isoc). */
125typedef struct rx_data
126{
127 void *data;
128 int length;
129 struct list_head list;
130} rx_data_t;
131
132typedef struct urb_entry
133{
134 struct urb *urb;
135 struct list_head list;
136} urb_entry_t;
137
138/* ---------------------------------------------------------------------------
139 Virtual Root HUB
140 ------------------------------------------------------------------------- */
141/* destination of request */
142#define RH_INTERFACE 0x01
143#define RH_ENDPOINT 0x02
144#define RH_OTHER 0x03
145
146#define RH_CLASS 0x20
147#define RH_VENDOR 0x40
148
149/* Requests: bRequest << 8 | bmRequestType */
150#define RH_GET_STATUS 0x0080
151#define RH_CLEAR_FEATURE 0x0100
152#define RH_SET_FEATURE 0x0300
153#define RH_SET_ADDRESS 0x0500
154#define RH_GET_DESCRIPTOR 0x0680
155#define RH_SET_DESCRIPTOR 0x0700
156#define RH_GET_CONFIGURATION 0x0880
157#define RH_SET_CONFIGURATION 0x0900
158#define RH_GET_STATE 0x0280
159#define RH_GET_INTERFACE 0x0A80
160#define RH_SET_INTERFACE 0x0B00
161#define RH_SYNC_FRAME 0x0C80
162/* Our Vendor Specific Request */
163#define RH_SET_EP 0x2000
164
165
166/* Hub port features */
167#define RH_PORT_CONNECTION 0x00
168#define RH_PORT_ENABLE 0x01
169#define RH_PORT_SUSPEND 0x02
170#define RH_PORT_OVER_CURRENT 0x03
171#define RH_PORT_RESET 0x04
172#define RH_PORT_POWER 0x08
173#define RH_PORT_LOW_SPEED 0x09
174#define RH_C_PORT_CONNECTION 0x10
175#define RH_C_PORT_ENABLE 0x11
176#define RH_C_PORT_SUSPEND 0x12
177#define RH_C_PORT_OVER_CURRENT 0x13
178#define RH_C_PORT_RESET 0x14
179
180/* Hub features */
181#define RH_C_HUB_LOCAL_POWER 0x00
182#define RH_C_HUB_OVER_CURRENT 0x01
183
184#define RH_DEVICE_REMOTE_WAKEUP 0x00
185#define RH_ENDPOINT_STALL 0x01
186
187/* Our Vendor Specific feature */
188#define RH_REMOVE_EP 0x00
189
190
191#define RH_ACK 0x01
192#define RH_REQ_ERR -1
193#define RH_NACK 0x00
194
195/* Field definitions for */
196
197#define USB_IN_command__eol__BITNR 0 /* command macros */
198#define USB_IN_command__eol__WIDTH 1
199#define USB_IN_command__eol__no 0
200#define USB_IN_command__eol__yes 1
201
202#define USB_IN_command__intr__BITNR 3
203#define USB_IN_command__intr__WIDTH 1
204#define USB_IN_command__intr__no 0
205#define USB_IN_command__intr__yes 1
206
207#define USB_IN_status__eop__BITNR 1 /* status macros. */
208#define USB_IN_status__eop__WIDTH 1
209#define USB_IN_status__eop__no 0
210#define USB_IN_status__eop__yes 1
211
212#define USB_IN_status__eot__BITNR 5
213#define USB_IN_status__eot__WIDTH 1
214#define USB_IN_status__eot__no 0
215#define USB_IN_status__eot__yes 1
216
217#define USB_IN_status__error__BITNR 6
218#define USB_IN_status__error__WIDTH 1
219#define USB_IN_status__error__no 0
220#define USB_IN_status__error__yes 1
221
222#define USB_IN_status__nodata__BITNR 7
223#define USB_IN_status__nodata__WIDTH 1
224#define USB_IN_status__nodata__no 0
225#define USB_IN_status__nodata__yes 1
226
227#define USB_IN_status__epid__BITNR 8
228#define USB_IN_status__epid__WIDTH 5
229
230#define USB_EP_command__eol__BITNR 0
231#define USB_EP_command__eol__WIDTH 1
232#define USB_EP_command__eol__no 0
233#define USB_EP_command__eol__yes 1
234
235#define USB_EP_command__eof__BITNR 1
236#define USB_EP_command__eof__WIDTH 1
237#define USB_EP_command__eof__no 0
238#define USB_EP_command__eof__yes 1
239
240#define USB_EP_command__intr__BITNR 3
241#define USB_EP_command__intr__WIDTH 1
242#define USB_EP_command__intr__no 0
243#define USB_EP_command__intr__yes 1
244
245#define USB_EP_command__enable__BITNR 4
246#define USB_EP_command__enable__WIDTH 1
247#define USB_EP_command__enable__no 0
248#define USB_EP_command__enable__yes 1
249
250#define USB_EP_command__hw_valid__BITNR 5
251#define USB_EP_command__hw_valid__WIDTH 1
252#define USB_EP_command__hw_valid__no 0
253#define USB_EP_command__hw_valid__yes 1
254
255#define USB_EP_command__epid__BITNR 8
256#define USB_EP_command__epid__WIDTH 5
257
258#define USB_SB_command__eol__BITNR 0 /* command macros. */
259#define USB_SB_command__eol__WIDTH 1
260#define USB_SB_command__eol__no 0
261#define USB_SB_command__eol__yes 1
262
263#define USB_SB_command__eot__BITNR 1
264#define USB_SB_command__eot__WIDTH 1
265#define USB_SB_command__eot__no 0
266#define USB_SB_command__eot__yes 1
267
268#define USB_SB_command__intr__BITNR 3
269#define USB_SB_command__intr__WIDTH 1
270#define USB_SB_command__intr__no 0
271#define USB_SB_command__intr__yes 1
272
273#define USB_SB_command__tt__BITNR 4
274#define USB_SB_command__tt__WIDTH 2
275#define USB_SB_command__tt__zout 0
276#define USB_SB_command__tt__in 1
277#define USB_SB_command__tt__out 2
278#define USB_SB_command__tt__setup 3
279
280
281#define USB_SB_command__rem__BITNR 8
282#define USB_SB_command__rem__WIDTH 6
283
284#define USB_SB_command__full__BITNR 6
285#define USB_SB_command__full__WIDTH 1
286#define USB_SB_command__full__no 0
287#define USB_SB_command__full__yes 1
288
289#endif
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index b331ac4d0d62..79705609fd0c 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -20,10 +20,16 @@
20 20
21/*-------------------------------------------------------------------------*/ 21/*-------------------------------------------------------------------------*/
22 22
23static int broken_suspend(struct usb_hcd *hcd)
24{
25 device_init_wakeup(&hcd->self.root_hub->dev, 0);
26 return 0;
27}
28
23/* AMD 756, for most chips (early revs), corrupts register 29/* AMD 756, for most chips (early revs), corrupts register
24 * values on read ... so enable the vendor workaround. 30 * values on read ... so enable the vendor workaround.
25 */ 31 */
26static int __devinit ohci_quirk_amd756(struct usb_hcd *hcd) 32static int ohci_quirk_amd756(struct usb_hcd *hcd)
27{ 33{
28 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 34 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
29 35
@@ -31,16 +37,14 @@ static int __devinit ohci_quirk_amd756(struct usb_hcd *hcd)
31 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n"); 37 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n");
32 38
33 /* also erratum 10 (suspend/resume issues) */ 39 /* also erratum 10 (suspend/resume issues) */
34 device_init_wakeup(&hcd->self.root_hub->dev, 0); 40 return broken_suspend(hcd);
35
36 return 0;
37} 41}
38 42
39/* Apple's OHCI driver has a lot of bizarre workarounds 43/* Apple's OHCI driver has a lot of bizarre workarounds
40 * for this chip. Evidently control and bulk lists 44 * for this chip. Evidently control and bulk lists
41 * can get confused. (B&W G3 models, and ...) 45 * can get confused. (B&W G3 models, and ...)
42 */ 46 */
43static int __devinit ohci_quirk_opti(struct usb_hcd *hcd) 47static int ohci_quirk_opti(struct usb_hcd *hcd)
44{ 48{
45 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 49 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
46 50
@@ -53,7 +57,7 @@ static int __devinit ohci_quirk_opti(struct usb_hcd *hcd)
53 * identify the USB (fn2). This quirk might apply to more or 57 * identify the USB (fn2). This quirk might apply to more or
54 * even all NSC stuff. 58 * even all NSC stuff.
55 */ 59 */
56static int __devinit ohci_quirk_ns(struct usb_hcd *hcd) 60static int ohci_quirk_ns(struct usb_hcd *hcd)
57{ 61{
58 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 62 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
59 struct pci_dev *b; 63 struct pci_dev *b;
@@ -75,7 +79,7 @@ static int __devinit ohci_quirk_ns(struct usb_hcd *hcd)
75 * delays before control or bulk queues get re-activated 79 * delays before control or bulk queues get re-activated
76 * in finish_unlinks() 80 * in finish_unlinks()
77 */ 81 */
78static int __devinit ohci_quirk_zfmicro(struct usb_hcd *hcd) 82static int ohci_quirk_zfmicro(struct usb_hcd *hcd)
79{ 83{
80 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 84 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
81 85
@@ -88,7 +92,7 @@ static int __devinit ohci_quirk_zfmicro(struct usb_hcd *hcd)
88/* Check for Toshiba SCC OHCI which has big endian registers 92/* Check for Toshiba SCC OHCI which has big endian registers
89 * and little endian in memory data structures 93 * and little endian in memory data structures
90 */ 94 */
91static int __devinit ohci_quirk_toshiba_scc(struct usb_hcd *hcd) 95static int ohci_quirk_toshiba_scc(struct usb_hcd *hcd)
92{ 96{
93 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 97 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
94 98
@@ -129,6 +133,18 @@ static const struct pci_device_id ohci_pci_quirks[] = {
129 PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, 0x01b6), 133 PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, 0x01b6),
130 .driver_data = (unsigned long)ohci_quirk_toshiba_scc, 134 .driver_data = (unsigned long)ohci_quirk_toshiba_scc,
131 }, 135 },
136 {
137 /* Toshiba portege 4000 */
138 .vendor = PCI_VENDOR_ID_AL,
139 .device = 0x5237,
140 .subvendor = PCI_VENDOR_ID_TOSHIBA_2,
141 .subdevice = 0x0004,
142 .driver_data = (unsigned long) broken_suspend,
143 },
144 {
145 PCI_DEVICE(PCI_VENDOR_ID_ITE, 0x8152),
146 .driver_data = (unsigned long) broken_suspend,
147 },
132 /* FIXME for some of the early AMD 760 southbridges, OHCI 148 /* FIXME for some of the early AMD 760 southbridges, OHCI
133 * won't work at all. blacklist them. 149 * won't work at all. blacklist them.
134 */ 150 */
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 19a0cc02b9a2..4aed305982ec 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -123,10 +123,14 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
123 123
124static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td) 124static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
125{ 125{
126 if (!list_empty(&td->list)) 126 if (!list_empty(&td->list)) {
127 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td); 127 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
128 if (!list_empty(&td->fl_list)) 128 WARN_ON(1);
129 }
130 if (!list_empty(&td->fl_list)) {
129 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td); 131 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
132 WARN_ON(1);
133 }
130 134
131 dma_pool_free(uhci->td_pool, td, td->dma_handle); 135 dma_pool_free(uhci->td_pool, td, td->dma_handle);
132} 136}
@@ -291,8 +295,10 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
291static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 295static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
292{ 296{
293 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev); 297 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
294 if (!list_empty(&qh->queue)) 298 if (!list_empty(&qh->queue)) {
295 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 299 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
300 WARN_ON(1);
301 }
296 302
297 list_del(&qh->node); 303 list_del(&qh->node);
298 if (qh->udev) { 304 if (qh->udev) {
@@ -740,9 +746,11 @@ static void uhci_free_urb_priv(struct uhci_hcd *uhci,
740{ 746{
741 struct uhci_td *td, *tmp; 747 struct uhci_td *td, *tmp;
742 748
743 if (!list_empty(&urbp->node)) 749 if (!list_empty(&urbp->node)) {
744 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n", 750 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
745 urbp->urb); 751 urbp->urb);
752 WARN_ON(1);
753 }
746 754
747 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { 755 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
748 uhci_remove_td_from_urbp(td); 756 uhci_remove_td_from_urbp(td);
diff --git a/drivers/usb/input/ati_remote2.c b/drivers/usb/input/ati_remote2.c
index 83f1f79db7c7..6459be90599c 100644
--- a/drivers/usb/input/ati_remote2.c
+++ b/drivers/usb/input/ati_remote2.c
@@ -2,6 +2,7 @@
2 * ati_remote2 - ATI/Philips USB RF remote driver 2 * ati_remote2 - ATI/Philips USB RF remote driver
3 * 3 *
4 * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi> 4 * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi>
5 * Copyright (C) 2007 Peter Stokes <linux@dadeos.freeserve.co.uk>
5 * 6 *
6 * 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
7 * it under the terms of the GNU General Public License version 2 8 * it under the terms of the GNU General Public License version 2
@@ -11,13 +12,29 @@
11#include <linux/usb/input.h> 12#include <linux/usb/input.h>
12 13
13#define DRIVER_DESC "ATI/Philips USB RF remote driver" 14#define DRIVER_DESC "ATI/Philips USB RF remote driver"
14#define DRIVER_VERSION "0.1" 15#define DRIVER_VERSION "0.2"
15 16
16MODULE_DESCRIPTION(DRIVER_DESC); 17MODULE_DESCRIPTION(DRIVER_DESC);
17MODULE_VERSION(DRIVER_VERSION); 18MODULE_VERSION(DRIVER_VERSION);
18MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); 19MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
19MODULE_LICENSE("GPL"); 20MODULE_LICENSE("GPL");
20 21
22/*
23 * ATI Remote Wonder II Channel Configuration
24 *
25 * The remote control can by assigned one of sixteen "channels" in order to facilitate
26 * the use of multiple remote controls within range of each other.
27 * A remote's "channel" may be altered by pressing and holding the "PC" button for
28 * approximately 3 seconds, after which the button will slowly flash the count of the
29 * currently configured "channel", using the numeric keypad enter a number between 1 and
30 * 16 and then the "PC" button again, the button will slowly flash the count of the
31 * newly configured "channel".
32 */
33
34static unsigned int channel_mask = 0xFFFF;
35module_param(channel_mask, uint, 0644);
36MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
37
21static unsigned int mode_mask = 0x1F; 38static unsigned int mode_mask = 0x1F;
22module_param(mode_mask, uint, 0644); 39module_param(mode_mask, uint, 0644);
23MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>"); 40MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
@@ -146,15 +163,23 @@ static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
146{ 163{
147 struct input_dev *idev = ar2->idev; 164 struct input_dev *idev = ar2->idev;
148 u8 *data = ar2->buf[0]; 165 u8 *data = ar2->buf[0];
166 int channel, mode;
167
168 channel = data[0] >> 4;
169
170 if (!((1 << channel) & channel_mask))
171 return;
149 172
150 if (data[0] > 4) { 173 mode = data[0] & 0x0F;
174
175 if (mode > 4) {
151 dev_err(&ar2->intf[0]->dev, 176 dev_err(&ar2->intf[0]->dev,
152 "Unknown mode byte (%02x %02x %02x %02x)\n", 177 "Unknown mode byte (%02x %02x %02x %02x)\n",
153 data[3], data[2], data[1], data[0]); 178 data[3], data[2], data[1], data[0]);
154 return; 179 return;
155 } 180 }
156 181
157 if (!((1 << data[0]) & mode_mask)) 182 if (!((1 << mode) & mode_mask))
158 return; 183 return;
159 184
160 input_event(idev, EV_REL, REL_X, (s8) data[1]); 185 input_event(idev, EV_REL, REL_X, (s8) data[1]);
@@ -177,9 +202,16 @@ static void ati_remote2_input_key(struct ati_remote2 *ar2)
177{ 202{
178 struct input_dev *idev = ar2->idev; 203 struct input_dev *idev = ar2->idev;
179 u8 *data = ar2->buf[1]; 204 u8 *data = ar2->buf[1];
180 int hw_code, index; 205 int channel, mode, hw_code, index;
206
207 channel = data[0] >> 4;
208
209 if (!((1 << channel) & channel_mask))
210 return;
181 211
182 if (data[0] > 4) { 212 mode = data[0] & 0x0F;
213
214 if (mode > 4) {
183 dev_err(&ar2->intf[1]->dev, 215 dev_err(&ar2->intf[1]->dev,
184 "Unknown mode byte (%02x %02x %02x %02x)\n", 216 "Unknown mode byte (%02x %02x %02x %02x)\n",
185 data[3], data[2], data[1], data[0]); 217 data[3], data[2], data[1], data[0]);
@@ -199,16 +231,16 @@ static void ati_remote2_input_key(struct ati_remote2 *ar2)
199 * events for the mouse pad so we filter out any subsequent 231 * events for the mouse pad so we filter out any subsequent
200 * events from the same mode key. 232 * events from the same mode key.
201 */ 233 */
202 if (ar2->mode == data[0]) 234 if (ar2->mode == mode)
203 return; 235 return;
204 236
205 if (data[1] == 0) 237 if (data[1] == 0)
206 ar2->mode = data[0]; 238 ar2->mode = mode;
207 239
208 hw_code |= data[0] << 8; 240 hw_code |= mode << 8;
209 } 241 }
210 242
211 if (!((1 << data[0]) & mode_mask)) 243 if (!((1 << mode) & mode_mask))
212 return; 244 return;
213 245
214 index = ati_remote2_lookup(hw_code); 246 index = ati_remote2_lookup(hw_code);
@@ -379,6 +411,41 @@ static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
379 } 411 }
380} 412}
381 413
414static int ati_remote2_setup(struct ati_remote2 *ar2)
415{
416 int r, i, channel;
417
418 /*
419 * Configure receiver to only accept input from remote "channel"
420 * channel == 0 -> Accept input from any remote channel
421 * channel == 1 -> Only accept input from remote channel 1
422 * channel == 2 -> Only accept input from remote channel 2
423 * ...
424 * channel == 16 -> Only accept input from remote channel 16
425 */
426
427 channel = 0;
428 for (i = 0; i < 16; i++) {
429 if ((1 << i) & channel_mask) {
430 if (!(~(1 << i) & 0xFFFF & channel_mask))
431 channel = i + 1;
432 break;
433 }
434 }
435
436 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
437 0x20,
438 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
439 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
440 if (r) {
441 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
442 __FUNCTION__, r);
443 return r;
444 }
445
446 return 0;
447}
448
382static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id) 449static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
383{ 450{
384 struct usb_device *udev = interface_to_usbdev(interface); 451 struct usb_device *udev = interface_to_usbdev(interface);
@@ -409,6 +476,10 @@ static int ati_remote2_probe(struct usb_interface *interface, const struct usb_d
409 if (r) 476 if (r)
410 goto fail2; 477 goto fail2;
411 478
479 r = ati_remote2_setup(ar2);
480 if (r)
481 goto fail2;
482
412 usb_make_path(udev, ar2->phys, sizeof(ar2->phys)); 483 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
413 strlcat(ar2->phys, "/input0", sizeof(ar2->phys)); 484 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
414 485
diff --git a/drivers/usb/input/gtco.c b/drivers/usb/input/gtco.c
index 203cdc1bbba4..ae756e0afc99 100644
--- a/drivers/usb/input/gtco.c
+++ b/drivers/usb/input/gtco.c
@@ -1047,13 +1047,10 @@ static void gtco_disconnect(struct usb_interface *interface)
1047 1047
1048 /* Grab private device ptr */ 1048 /* Grab private device ptr */
1049 struct gtco *device = usb_get_intfdata (interface); 1049 struct gtco *device = usb_get_intfdata (interface);
1050 struct input_dev *inputdev;
1051
1052 inputdev = device->inputdevice;
1053 1050
1054 /* Now reverse all the registration stuff */ 1051 /* Now reverse all the registration stuff */
1055 if (device) { 1052 if (device) {
1056 input_unregister_device(inputdev); 1053 input_unregister_device(device->inputdevice);
1057 usb_kill_urb(device->urbinfo); 1054 usb_kill_urb(device->urbinfo);
1058 usb_free_urb(device->urbinfo); 1055 usb_free_urb(device->urbinfo);
1059 usb_buffer_free(device->usbdev, REPORT_MAX_SIZE, 1056 usb_buffer_free(device->usbdev, REPORT_MAX_SIZE,
diff --git a/drivers/usb/misc/adutux.c b/drivers/usb/misc/adutux.c
index 75bfab95ab3c..77145f9db043 100644
--- a/drivers/usb/misc/adutux.c
+++ b/drivers/usb/misc/adutux.c
@@ -285,23 +285,24 @@ static int adu_open(struct inode *inode, struct file *file)
285 /* save device in the file's private structure */ 285 /* save device in the file's private structure */
286 file->private_data = dev; 286 file->private_data = dev;
287 287
288 /* initialize in direction */ 288 if (dev->open_count == 1) {
289 dev->read_buffer_length = 0; 289 /* initialize in direction */
290 290 dev->read_buffer_length = 0;
291 /* fixup first read by having urb waiting for it */
292 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
293 usb_rcvintpipe(dev->udev,
294 dev->interrupt_in_endpoint->bEndpointAddress),
295 dev->interrupt_in_buffer,
296 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
297 adu_interrupt_in_callback, dev,
298 dev->interrupt_in_endpoint->bInterval);
299 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
300 dev->read_urb_finished = 0;
301 usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
302 /* we ignore failure */
303 /* end of fixup for first read */
304 291
292 /* fixup first read by having urb waiting for it */
293 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
294 usb_rcvintpipe(dev->udev,
295 dev->interrupt_in_endpoint->bEndpointAddress),
296 dev->interrupt_in_buffer,
297 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
298 adu_interrupt_in_callback, dev,
299 dev->interrupt_in_endpoint->bInterval);
300 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
301 dev->read_urb_finished = 0;
302 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
303 if (retval)
304 --dev->open_count;
305 }
305 up(&dev->sem); 306 up(&dev->sem);
306 307
307exit_no_device: 308exit_no_device:
@@ -469,7 +470,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
469 adu_interrupt_in_callback, 470 adu_interrupt_in_callback,
470 dev, 471 dev,
471 dev->interrupt_in_endpoint->bInterval); 472 dev->interrupt_in_endpoint->bInterval);
472 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 473 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
473 if (!retval) { 474 if (!retval) {
474 spin_unlock_irqrestore(&dev->buflock, flags); 475 spin_unlock_irqrestore(&dev->buflock, flags);
475 dbg(2," %s : submitted OK", __FUNCTION__); 476 dbg(2," %s : submitted OK", __FUNCTION__);
@@ -539,7 +540,7 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
539 size_t bytes_written = 0; 540 size_t bytes_written = 0;
540 size_t bytes_to_write; 541 size_t bytes_to_write;
541 size_t buffer_size; 542 size_t buffer_size;
542 int retval = 0; 543 int retval;
543 int timeout = 0; 544 int timeout = 0;
544 545
545 dbg(2," %s : enter, count = %Zd", __FUNCTION__, count); 546 dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
@@ -547,7 +548,9 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
547 dev = file->private_data; 548 dev = file->private_data;
548 549
549 /* lock this object */ 550 /* lock this object */
550 down_interruptible(&dev->sem); 551 retval = down_interruptible(&dev->sem);
552 if (retval)
553 goto exit_nolock;
551 554
552 /* verify that the device wasn't unplugged */ 555 /* verify that the device wasn't unplugged */
553 if (dev->udev == NULL || dev->minor == 0) { 556 if (dev->udev == NULL || dev->minor == 0) {
@@ -575,7 +578,11 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
575 } 578 }
576 up(&dev->sem); 579 up(&dev->sem);
577 timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout); 580 timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
578 down_interruptible(&dev->sem); 581 retval = down_interruptible(&dev->sem);
582 if (retval) {
583 retval = bytes_written ? bytes_written : retval;
584 goto exit_nolock;
585 }
579 if (timeout > 0) { 586 if (timeout > 0) {
580 break; 587 break;
581 } 588 }
@@ -637,6 +644,7 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
637exit: 644exit:
638 /* unlock the device */ 645 /* unlock the device */
639 up(&dev->sem); 646 up(&dev->sem);
647exit_nolock:
640 648
641 dbg(2," %s : leave, return value %d", __FUNCTION__, retval); 649 dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
642 650
diff --git a/drivers/usb/misc/cypress_cy7c63.c b/drivers/usb/misc/cypress_cy7c63.c
index b63b5f34b2aa..d721380b242d 100644
--- a/drivers/usb/misc/cypress_cy7c63.c
+++ b/drivers/usb/misc/cypress_cy7c63.c
@@ -246,11 +246,13 @@ static void cypress_disconnect(struct usb_interface *interface)
246 struct cypress *dev; 246 struct cypress *dev;
247 247
248 dev = usb_get_intfdata(interface); 248 dev = usb_get_intfdata(interface);
249 usb_set_intfdata(interface, NULL);
250 249
251 /* remove device attribute files */ 250 /* remove device attribute files */
252 device_remove_file(&interface->dev, &dev_attr_port0); 251 device_remove_file(&interface->dev, &dev_attr_port0);
253 device_remove_file(&interface->dev, &dev_attr_port1); 252 device_remove_file(&interface->dev, &dev_attr_port1);
253 /* the intfdata can be set to NULL only after the
254 * device files have been removed */
255 usb_set_intfdata(interface, NULL);
254 256
255 usb_put_dev(dev->udev); 257 usb_put_dev(dev->udev);
256 258
diff --git a/drivers/usb/misc/ftdi-elan.c b/drivers/usb/misc/ftdi-elan.c
index bc3327e3dd78..e2172e5cf152 100644
--- a/drivers/usb/misc/ftdi-elan.c
+++ b/drivers/usb/misc/ftdi-elan.c
@@ -2304,7 +2304,6 @@ static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2304#define OHCI_QUIRK_SUPERIO 0x02 2304#define OHCI_QUIRK_SUPERIO 0x02
2305#define OHCI_QUIRK_INITRESET 0x04 2305#define OHCI_QUIRK_INITRESET 0x04
2306#define OHCI_BIG_ENDIAN 0x08 2306#define OHCI_BIG_ENDIAN 0x08
2307#define OHCI_QUIRK_ZFMICRO 0x10
2308#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR 2307#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2309#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \ 2308#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2310 OHCI_INTR_WDH) 2309 OHCI_INTR_WDH)
@@ -2910,24 +2909,28 @@ static int __init ftdi_elan_init(void)
2910 INIT_LIST_HEAD(&ftdi_static_list); 2909 INIT_LIST_HEAD(&ftdi_static_list);
2911 status_queue = create_singlethread_workqueue("ftdi-status-control"); 2910 status_queue = create_singlethread_workqueue("ftdi-status-control");
2912 if (!status_queue) 2911 if (!status_queue)
2913 goto err1; 2912 goto err_status_queue;
2914 command_queue = create_singlethread_workqueue("ftdi-command-engine"); 2913 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2915 if (!command_queue) 2914 if (!command_queue)
2916 goto err2; 2915 goto err_command_queue;
2917 respond_queue = create_singlethread_workqueue("ftdi-respond-engine"); 2916 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2918 if (!respond_queue) 2917 if (!respond_queue)
2919 goto err3; 2918 goto err_respond_queue;
2920 result = usb_register(&ftdi_elan_driver); 2919 result = usb_register(&ftdi_elan_driver);
2921 if (result) 2920 if (result) {
2921 destroy_workqueue(status_queue);
2922 destroy_workqueue(command_queue);
2923 destroy_workqueue(respond_queue);
2922 printk(KERN_ERR "usb_register failed. Error number %d\n", 2924 printk(KERN_ERR "usb_register failed. Error number %d\n",
2923 result); 2925 result);
2926 }
2924 return result; 2927 return result;
2925 2928
2926 err3: 2929 err_respond_queue:
2927 destroy_workqueue(command_queue); 2930 destroy_workqueue(command_queue);
2928 err2: 2931 err_command_queue:
2929 destroy_workqueue(status_queue); 2932 destroy_workqueue(status_queue);
2930 err1: 2933 err_status_queue:
2931 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name); 2934 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2932 return -ENOMEM; 2935 return -ENOMEM;
2933} 2936}
diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index d69665c8de02..fc51207b71b8 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -118,7 +118,7 @@ static int usb_get_report(struct usb_device *dev,
118 USB_DIR_IN | USB_TYPE_CLASS | 118 USB_DIR_IN | USB_TYPE_CLASS |
119 USB_RECIP_INTERFACE, (type << 8) + id, 119 USB_RECIP_INTERFACE, (type << 8) + id,
120 inter->desc.bInterfaceNumber, buf, size, 120 inter->desc.bInterfaceNumber, buf, size,
121 GET_TIMEOUT); 121 GET_TIMEOUT*HZ);
122} 122}
123//#endif 123//#endif
124 124
@@ -133,7 +133,7 @@ static int usb_set_report(struct usb_interface *intf, unsigned char type,
133 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 133 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
134 (type << 8) + id, 134 (type << 8) + id,
135 intf->cur_altsetting->desc.bInterfaceNumber, buf, 135 intf->cur_altsetting->desc.bInterfaceNumber, buf,
136 size, 1); 136 size, HZ);
137} 137}
138 138
139/*---------------------*/ 139/*---------------------*/
@@ -417,14 +417,14 @@ static ssize_t iowarrior_write(struct file *file,
417 if (!int_out_urb) { 417 if (!int_out_urb) {
418 retval = -ENOMEM; 418 retval = -ENOMEM;
419 dbg("%s Unable to allocate urb ", __func__); 419 dbg("%s Unable to allocate urb ", __func__);
420 goto error; 420 goto error_no_urb;
421 } 421 }
422 buf = usb_buffer_alloc(dev->udev, dev->report_size, 422 buf = usb_buffer_alloc(dev->udev, dev->report_size,
423 GFP_KERNEL, &int_out_urb->transfer_dma); 423 GFP_KERNEL, &int_out_urb->transfer_dma);
424 if (!buf) { 424 if (!buf) {
425 retval = -ENOMEM; 425 retval = -ENOMEM;
426 dbg("%s Unable to allocate buffer ", __func__); 426 dbg("%s Unable to allocate buffer ", __func__);
427 goto error; 427 goto error_no_buffer;
428 } 428 }
429 usb_fill_int_urb(int_out_urb, dev->udev, 429 usb_fill_int_urb(int_out_urb, dev->udev,
430 usb_sndintpipe(dev->udev, 430 usb_sndintpipe(dev->udev,
@@ -459,7 +459,9 @@ static ssize_t iowarrior_write(struct file *file,
459error: 459error:
460 usb_buffer_free(dev->udev, dev->report_size, buf, 460 usb_buffer_free(dev->udev, dev->report_size, buf,
461 int_out_urb->transfer_dma); 461 int_out_urb->transfer_dma);
462error_no_buffer:
462 usb_free_urb(int_out_urb); 463 usb_free_urb(int_out_urb);
464error_no_urb:
463 atomic_dec(&dev->write_busy); 465 atomic_dec(&dev->write_busy);
464 wake_up_interruptible(&dev->write_wait); 466 wake_up_interruptible(&dev->write_wait);
465exit: 467exit:
@@ -748,7 +750,6 @@ static int iowarrior_probe(struct usb_interface *interface,
748 struct usb_endpoint_descriptor *endpoint; 750 struct usb_endpoint_descriptor *endpoint;
749 int i; 751 int i;
750 int retval = -ENOMEM; 752 int retval = -ENOMEM;
751 int idele = 0;
752 753
753 /* allocate memory for our device state and intialize it */ 754 /* allocate memory for our device state and intialize it */
754 dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL); 755 dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
@@ -824,11 +825,10 @@ static int iowarrior_probe(struct usb_interface *interface,
824 825
825 /* Set the idle timeout to 0, if this is interface 0 */ 826 /* Set the idle timeout to 0, if this is interface 0 */
826 if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) { 827 if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
827 idele = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 828 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
828 0x0A, 829 0x0A,
829 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, 830 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
830 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 831 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
831 dbg("idele = %d", idele);
832 } 832 }
833 /* allow device read and ioctl */ 833 /* allow device read and ioctl */
834 dev->present = 1; 834 dev->present = 1;
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
index 788a11e6772f..11555bde655b 100644
--- a/drivers/usb/misc/ldusb.c
+++ b/drivers/usb/misc/ldusb.c
@@ -62,6 +62,8 @@
62#define USB_DEVICE_ID_VERNIER_SKIP 0x0003 62#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
63#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 63#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
64 64
65#define USB_VENDOR_ID_MICROCHIP 0x04d8
66#define USB_DEVICE_ID_PICDEM 0x000c
65 67
66#ifdef CONFIG_USB_DYNAMIC_MINORS 68#ifdef CONFIG_USB_DYNAMIC_MINORS
67#define USB_LD_MINOR_BASE 0 69#define USB_LD_MINOR_BASE 0
@@ -89,6 +91,7 @@ static struct usb_device_id ld_usb_table [] = {
89 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) }, 91 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
90 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) }, 92 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
91 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) }, 93 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
94 { USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICDEM) },
92 { } /* Terminating entry */ 95 { } /* Terminating entry */
93}; 96};
94MODULE_DEVICE_TABLE(usb, ld_usb_table); 97MODULE_DEVICE_TABLE(usb, ld_usb_table);
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index ada2ebc464ae..887ef953f3d8 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -47,6 +47,7 @@ struct usb_lcd {
47#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref) 47#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
48 48
49static struct usb_driver lcd_driver; 49static struct usb_driver lcd_driver;
50static DEFINE_MUTEX(usb_lcd_open_mutex);
50 51
51 52
52static void lcd_delete(struct kref *kref) 53static void lcd_delete(struct kref *kref)
@@ -68,6 +69,7 @@ static int lcd_open(struct inode *inode, struct file *file)
68 69
69 subminor = iminor(inode); 70 subminor = iminor(inode);
70 71
72 mutex_lock(&usb_lcd_open_mutex);
71 interface = usb_find_interface(&lcd_driver, subminor); 73 interface = usb_find_interface(&lcd_driver, subminor);
72 if (!interface) { 74 if (!interface) {
73 err ("USBLCD: %s - error, can't find device for minor %d", 75 err ("USBLCD: %s - error, can't find device for minor %d",
@@ -89,6 +91,7 @@ static int lcd_open(struct inode *inode, struct file *file)
89 file->private_data = dev; 91 file->private_data = dev;
90 92
91exit: 93exit:
94 mutex_unlock(&usb_lcd_open_mutex);
92 return retval; 95 return retval;
93} 96}
94 97
@@ -347,7 +350,7 @@ static void lcd_disconnect(struct usb_interface *interface)
347 int minor = interface->minor; 350 int minor = interface->minor;
348 351
349 /* prevent skel_open() from racing skel_disconnect() */ 352 /* prevent skel_open() from racing skel_disconnect() */
350 lock_kernel(); 353 mutex_lock(&usb_lcd_open_mutex);
351 354
352 dev = usb_get_intfdata(interface); 355 dev = usb_get_intfdata(interface);
353 usb_set_intfdata(interface, NULL); 356 usb_set_intfdata(interface, NULL);
@@ -355,7 +358,7 @@ static void lcd_disconnect(struct usb_interface *interface)
355 /* give back our minor */ 358 /* give back our minor */
356 usb_deregister_dev(interface, &lcd_class); 359 usb_deregister_dev(interface, &lcd_class);
357 360
358 unlock_kernel(); 361 mutex_unlock(&usb_lcd_open_mutex);
359 362
360 /* decrement our usage count */ 363 /* decrement our usage count */
361 kref_put(&dev->kref, lcd_delete); 364 kref_put(&dev->kref, lcd_delete);
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index b2bedd974ac3..0af11a66207c 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -356,8 +356,10 @@ static inline char mon_bin_get_setup(unsigned char *setupb,
356 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S') 356 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
357 return '-'; 357 return '-';
358 358
359 if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP) 359 if (urb->dev->bus->uses_dma &&
360 (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
360 return mon_dmapeek(setupb, urb->setup_dma, SETUP_LEN); 361 return mon_dmapeek(setupb, urb->setup_dma, SETUP_LEN);
362 }
361 if (urb->setup_packet == NULL) 363 if (urb->setup_packet == NULL)
362 return 'Z'; 364 return 'Z';
363 365
@@ -369,7 +371,8 @@ static char mon_bin_get_data(const struct mon_reader_bin *rp,
369 unsigned int offset, struct urb *urb, unsigned int length) 371 unsigned int offset, struct urb *urb, unsigned int length)
370{ 372{
371 373
372 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) { 374 if (urb->dev->bus->uses_dma &&
375 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
373 mon_dmapeek_vec(rp, offset, urb->transfer_dma, length); 376 mon_dmapeek_vec(rp, offset, urb->transfer_dma, length);
374 return 0; 377 return 0;
375 } 378 }
@@ -440,7 +443,7 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
440 /* We use the fact that usb_pipein() returns 0x80 */ 443 /* We use the fact that usb_pipein() returns 0x80 */
441 ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe); 444 ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
442 ep->devnum = usb_pipedevice(urb->pipe); 445 ep->devnum = usb_pipedevice(urb->pipe);
443 ep->busnum = rp->r.m_bus->u_bus->busnum; 446 ep->busnum = urb->dev->bus->busnum;
444 ep->id = (unsigned long) urb; 447 ep->id = (unsigned long) urb;
445 ep->ts_sec = ts.tv_sec; 448 ep->ts_sec = ts.tv_sec;
446 ep->ts_usec = ts.tv_usec; 449 ep->ts_usec = ts.tv_usec;
@@ -500,7 +503,7 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
500 /* We use the fact that usb_pipein() returns 0x80 */ 503 /* We use the fact that usb_pipein() returns 0x80 */
501 ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe); 504 ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
502 ep->devnum = usb_pipedevice(urb->pipe); 505 ep->devnum = usb_pipedevice(urb->pipe);
503 ep->busnum = rp->r.m_bus->u_bus->busnum; 506 ep->busnum = urb->dev->bus->busnum;
504 ep->id = (unsigned long) urb; 507 ep->id = (unsigned long) urb;
505 ep->status = error; 508 ep->status = error;
506 509
@@ -515,7 +518,6 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
515static int mon_bin_open(struct inode *inode, struct file *file) 518static int mon_bin_open(struct inode *inode, struct file *file)
516{ 519{
517 struct mon_bus *mbus; 520 struct mon_bus *mbus;
518 struct usb_bus *ubus;
519 struct mon_reader_bin *rp; 521 struct mon_reader_bin *rp;
520 size_t size; 522 size_t size;
521 int rc; 523 int rc;
@@ -525,7 +527,7 @@ static int mon_bin_open(struct inode *inode, struct file *file)
525 mutex_unlock(&mon_lock); 527 mutex_unlock(&mon_lock);
526 return -ENODEV; 528 return -ENODEV;
527 } 529 }
528 if ((ubus = mbus->u_bus) == NULL) { 530 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
529 printk(KERN_ERR TAG ": consistency error on open\n"); 531 printk(KERN_ERR TAG ": consistency error on open\n");
530 mutex_unlock(&mon_lock); 532 mutex_unlock(&mon_lock);
531 return -ENODEV; 533 return -ENODEV;
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c9739e7b35e5..8a1df2c9c73e 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -16,8 +16,6 @@
16#include "usb_mon.h" 16#include "usb_mon.h"
17#include "../core/hcd.h" 17#include "../core/hcd.h"
18 18
19static void mon_submit(struct usb_bus *ubus, struct urb *urb);
20static void mon_complete(struct usb_bus *ubus, struct urb *urb);
21static void mon_stop(struct mon_bus *mbus); 19static void mon_stop(struct mon_bus *mbus);
22static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus); 20static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23static void mon_bus_drop(struct kref *r); 21static void mon_bus_drop(struct kref *r);
@@ -25,6 +23,7 @@ static void mon_bus_init(struct usb_bus *ubus);
25 23
26DEFINE_MUTEX(mon_lock); 24DEFINE_MUTEX(mon_lock);
27 25
26struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
28static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */ 27static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
29 28
30/* 29/*
@@ -35,22 +34,19 @@ static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
35void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r) 34void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
36{ 35{
37 unsigned long flags; 36 unsigned long flags;
38 struct usb_bus *ubus; 37 struct list_head *p;
39 38
40 spin_lock_irqsave(&mbus->lock, flags); 39 spin_lock_irqsave(&mbus->lock, flags);
41 if (mbus->nreaders == 0) { 40 if (mbus->nreaders == 0) {
42 ubus = mbus->u_bus; 41 if (mbus == &mon_bus0) {
43 if (ubus->monitored) { 42 list_for_each (p, &mon_buses) {
44 /* 43 struct mon_bus *m1;
45 * Something is really broken, refuse to go on and 44 m1 = list_entry(p, struct mon_bus, bus_link);
46 * possibly corrupt ops pointers or worse. 45 m1->u_bus->monitored = 1;
47 */ 46 }
48 printk(KERN_ERR TAG ": bus %d is already monitored\n", 47 } else {
49 ubus->busnum); 48 mbus->u_bus->monitored = 1;
50 spin_unlock_irqrestore(&mbus->lock, flags);
51 return;
52 } 49 }
53 ubus->monitored = 1;
54 } 50 }
55 mbus->nreaders++; 51 mbus->nreaders++;
56 list_add_tail(&r->r_link, &mbus->r_list); 52 list_add_tail(&r->r_link, &mbus->r_list);
@@ -80,77 +76,79 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
80 76
81/* 77/*
82 */ 78 */
83static void mon_submit(struct usb_bus *ubus, struct urb *urb) 79static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
84{ 80{
85 struct mon_bus *mbus;
86 unsigned long flags; 81 unsigned long flags;
87 struct list_head *pos; 82 struct list_head *pos;
88 struct mon_reader *r; 83 struct mon_reader *r;
89 84
90 mbus = ubus->mon_bus;
91 if (mbus == NULL)
92 goto out_unlocked;
93
94 spin_lock_irqsave(&mbus->lock, flags); 85 spin_lock_irqsave(&mbus->lock, flags);
95 if (mbus->nreaders == 0)
96 goto out_locked;
97
98 mbus->cnt_events++; 86 mbus->cnt_events++;
99 list_for_each (pos, &mbus->r_list) { 87 list_for_each (pos, &mbus->r_list) {
100 r = list_entry(pos, struct mon_reader, r_link); 88 r = list_entry(pos, struct mon_reader, r_link);
101 r->rnf_submit(r->r_data, urb); 89 r->rnf_submit(r->r_data, urb);
102 } 90 }
103
104 spin_unlock_irqrestore(&mbus->lock, flags); 91 spin_unlock_irqrestore(&mbus->lock, flags);
105 return; 92 return;
93}
106 94
107out_locked: 95static void mon_submit(struct usb_bus *ubus, struct urb *urb)
108 spin_unlock_irqrestore(&mbus->lock, flags); 96{
109out_unlocked: 97 struct mon_bus *mbus;
110 return; 98
99 if ((mbus = ubus->mon_bus) != NULL)
100 mon_bus_submit(mbus, urb);
101 mon_bus_submit(&mon_bus0, urb);
111} 102}
112 103
113/* 104/*
114 */ 105 */
115static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error) 106static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
116{ 107{
117 struct mon_bus *mbus;
118 unsigned long flags; 108 unsigned long flags;
119 struct list_head *pos; 109 struct list_head *pos;
120 struct mon_reader *r; 110 struct mon_reader *r;
121 111
122 mbus = ubus->mon_bus;
123 if (mbus == NULL)
124 goto out_unlocked;
125
126 spin_lock_irqsave(&mbus->lock, flags); 112 spin_lock_irqsave(&mbus->lock, flags);
127 if (mbus->nreaders == 0)
128 goto out_locked;
129
130 mbus->cnt_events++; 113 mbus->cnt_events++;
131 list_for_each (pos, &mbus->r_list) { 114 list_for_each (pos, &mbus->r_list) {
132 r = list_entry(pos, struct mon_reader, r_link); 115 r = list_entry(pos, struct mon_reader, r_link);
133 r->rnf_error(r->r_data, urb, error); 116 r->rnf_error(r->r_data, urb, error);
134 } 117 }
135
136 spin_unlock_irqrestore(&mbus->lock, flags); 118 spin_unlock_irqrestore(&mbus->lock, flags);
137 return; 119 return;
120}
138 121
139out_locked: 122static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
140 spin_unlock_irqrestore(&mbus->lock, flags); 123{
141out_unlocked: 124 struct mon_bus *mbus;
142 return; 125
126 if ((mbus = ubus->mon_bus) != NULL)
127 mon_bus_submit_error(mbus, urb, error);
128 mon_bus_submit_error(&mon_bus0, urb, error);
143} 129}
144 130
145/* 131/*
146 */ 132 */
147static void mon_complete(struct usb_bus *ubus, struct urb *urb) 133static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
148{ 134{
149 struct mon_bus *mbus;
150 unsigned long flags; 135 unsigned long flags;
151 struct list_head *pos; 136 struct list_head *pos;
152 struct mon_reader *r; 137 struct mon_reader *r;
153 138
139 spin_lock_irqsave(&mbus->lock, flags);
140 mbus->cnt_events++;
141 list_for_each (pos, &mbus->r_list) {
142 r = list_entry(pos, struct mon_reader, r_link);
143 r->rnf_complete(r->r_data, urb);
144 }
145 spin_unlock_irqrestore(&mbus->lock, flags);
146}
147
148static void mon_complete(struct usb_bus *ubus, struct urb *urb)
149{
150 struct mon_bus *mbus;
151
154 mbus = ubus->mon_bus; 152 mbus = ubus->mon_bus;
155 if (mbus == NULL) { 153 if (mbus == NULL) {
156 /* 154 /*
@@ -162,13 +160,8 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
162 return; 160 return;
163 } 161 }
164 162
165 spin_lock_irqsave(&mbus->lock, flags); 163 mon_bus_complete(mbus, urb);
166 mbus->cnt_events++; 164 mon_bus_complete(&mon_bus0, urb);
167 list_for_each (pos, &mbus->r_list) {
168 r = list_entry(pos, struct mon_reader, r_link);
169 r->rnf_complete(r->r_data, urb);
170 }
171 spin_unlock_irqrestore(&mbus->lock, flags);
172} 165}
173 166
174/* int (*unlink_urb) (struct urb *urb, int status); */ 167/* int (*unlink_urb) (struct urb *urb, int status); */
@@ -179,14 +172,26 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
179static void mon_stop(struct mon_bus *mbus) 172static void mon_stop(struct mon_bus *mbus)
180{ 173{
181 struct usb_bus *ubus = mbus->u_bus; 174 struct usb_bus *ubus = mbus->u_bus;
175 struct list_head *p;
182 176
183 /* 177 if (mbus == &mon_bus0) {
184 * A stop can be called for a dissolved mon_bus in case of 178 list_for_each (p, &mon_buses) {
185 * a reader staying across an rmmod foo_hcd. 179 mbus = list_entry(p, struct mon_bus, bus_link);
186 */ 180 /*
187 if (ubus != NULL) { 181 * We do not change nreaders here, so rely on mon_lock.
188 ubus->monitored = 0; 182 */
189 mb(); 183 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
184 ubus->monitored = 0;
185 }
186 } else {
187 /*
188 * A stop can be called for a dissolved mon_bus in case of
189 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
190 */
191 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
192 ubus->monitored = 0;
193 mb();
194 }
190 } 195 }
191} 196}
192 197
@@ -199,6 +204,10 @@ static void mon_stop(struct mon_bus *mbus)
199static void mon_bus_add(struct usb_bus *ubus) 204static void mon_bus_add(struct usb_bus *ubus)
200{ 205{
201 mon_bus_init(ubus); 206 mon_bus_init(ubus);
207 mutex_lock(&mon_lock);
208 if (mon_bus0.nreaders != 0)
209 ubus->monitored = 1;
210 mutex_unlock(&mon_lock);
202} 211}
203 212
204/* 213/*
@@ -250,12 +259,7 @@ static struct usb_mon_operations mon_ops_0 = {
250static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus) 259static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
251{ 260{
252 261
253 /*
254 * Never happens, but...
255 */
256 if (ubus->monitored) { 262 if (ubus->monitored) {
257 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
258 ubus->busnum);
259 ubus->monitored = 0; 263 ubus->monitored = 0;
260 mb(); 264 mb();
261 } 265 }
@@ -263,6 +267,8 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
263 ubus->mon_bus = NULL; 267 ubus->mon_bus = NULL;
264 mbus->u_bus = NULL; 268 mbus->u_bus = NULL;
265 mb(); 269 mb();
270
271 /* We want synchronize_irq() here, but that needs an argument. */
266} 272}
267 273
268/* 274/*
@@ -295,9 +301,8 @@ static void mon_bus_init(struct usb_bus *ubus)
295 */ 301 */
296 mbus->u_bus = ubus; 302 mbus->u_bus = ubus;
297 ubus->mon_bus = mbus; 303 ubus->mon_bus = mbus;
298 mbus->uses_dma = ubus->uses_dma;
299 304
300 mbus->text_inited = mon_text_add(mbus, ubus); 305 mbus->text_inited = mon_text_add(mbus, ubus->busnum);
301 // mon_bin_add(...) 306 // mon_bin_add(...)
302 307
303 mutex_lock(&mon_lock); 308 mutex_lock(&mon_lock);
@@ -309,6 +314,18 @@ err_alloc:
309 return; 314 return;
310} 315}
311 316
317static void mon_bus0_init(void)
318{
319 struct mon_bus *mbus = &mon_bus0;
320
321 kref_init(&mbus->ref);
322 spin_lock_init(&mbus->lock);
323 INIT_LIST_HEAD(&mbus->r_list);
324
325 mbus->text_inited = mon_text_add(mbus, 0);
326 // mbus->bin_inited = mon_bin_add(mbus, 0);
327}
328
312/* 329/*
313 * Search a USB bus by number. Notice that USB bus numbers start from one, 330 * Search a USB bus by number. Notice that USB bus numbers start from one,
314 * which we may later use to identify "all" with zero. 331 * which we may later use to identify "all" with zero.
@@ -322,6 +339,9 @@ struct mon_bus *mon_bus_lookup(unsigned int num)
322 struct list_head *p; 339 struct list_head *p;
323 struct mon_bus *mbus; 340 struct mon_bus *mbus;
324 341
342 if (num == 0) {
343 return &mon_bus0;
344 }
325 list_for_each (p, &mon_buses) { 345 list_for_each (p, &mon_buses) {
326 mbus = list_entry(p, struct mon_bus, bus_link); 346 mbus = list_entry(p, struct mon_bus, bus_link);
327 if (mbus->u_bus->busnum == num) { 347 if (mbus->u_bus->busnum == num) {
@@ -341,6 +361,8 @@ static int __init mon_init(void)
341 if ((rc = mon_bin_init()) != 0) 361 if ((rc = mon_bin_init()) != 0)
342 goto err_bin; 362 goto err_bin;
343 363
364 mon_bus0_init();
365
344 if (usb_mon_register(&mon_ops_0) != 0) { 366 if (usb_mon_register(&mon_ops_0) != 0) {
345 printk(KERN_NOTICE TAG ": unable to register with the core\n"); 367 printk(KERN_NOTICE TAG ": unable to register with the core\n");
346 rc = -ENODEV; 368 rc = -ENODEV;
@@ -374,6 +396,7 @@ static void __exit mon_exit(void)
374 usb_mon_deregister(); 396 usb_mon_deregister();
375 397
376 mutex_lock(&mon_lock); 398 mutex_lock(&mon_lock);
399
377 while (!list_empty(&mon_buses)) { 400 while (!list_empty(&mon_buses)) {
378 p = mon_buses.next; 401 p = mon_buses.next;
379 mbus = list_entry(p, struct mon_bus, bus_link); 402 mbus = list_entry(p, struct mon_bus, bus_link);
@@ -397,6 +420,11 @@ static void __exit mon_exit(void)
397 mon_dissolve(mbus, mbus->u_bus); 420 mon_dissolve(mbus, mbus->u_bus);
398 kref_put(&mbus->ref, mon_bus_drop); 421 kref_put(&mbus->ref, mon_bus_drop);
399 } 422 }
423
424 mbus = &mon_bus0;
425 if (mbus->text_inited)
426 mon_text_del(mbus);
427
400 mutex_unlock(&mon_lock); 428 mutex_unlock(&mon_lock);
401 429
402 mon_text_exit(); 430 mon_text_exit();
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 494ee3b9a226..ec0cc51e39ac 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -31,9 +31,21 @@
31 * to a local DoS. But we have to keep to root in order to prevent 31 * to a local DoS. But we have to keep to root in order to prevent
32 * password sniffing from HID devices. 32 * password sniffing from HID devices.
33 */ 33 */
34#define EVENT_MAX (2*PAGE_SIZE / sizeof(struct mon_event_text)) 34#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
35 35
36#define PRINTF_DFL 160 36/*
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
39 */
40#define ISODESC_MAX 5
41
42#define PRINTF_DFL 250 /* with 5 ISOs segs */
43
44struct mon_iso_desc {
45 int status;
46 unsigned int offset;
47 unsigned int length; /* Unsigned here, signed in URB. Historic. */
48};
37 49
38struct mon_event_text { 50struct mon_event_text {
39 struct list_head e_link; 51 struct list_head e_link;
@@ -41,10 +53,16 @@ struct mon_event_text {
41 unsigned int pipe; /* Pipe */ 53 unsigned int pipe; /* Pipe */
42 unsigned long id; /* From pointer, most of the time */ 54 unsigned long id; /* From pointer, most of the time */
43 unsigned int tstamp; 55 unsigned int tstamp;
56 int busnum;
44 int length; /* Depends on type: xfer length or act length */ 57 int length; /* Depends on type: xfer length or act length */
45 int status; 58 int status;
59 int interval;
60 int start_frame;
61 int error_count;
46 char setup_flag; 62 char setup_flag;
47 char data_flag; 63 char data_flag;
64 int numdesc; /* Full number */
65 struct mon_iso_desc isodesc[ISODESC_MAX];
48 unsigned char setup[SETUP_MAX]; 66 unsigned char setup[SETUP_MAX];
49 unsigned char data[DATA_MAX]; 67 unsigned char data[DATA_MAX];
50}; 68};
@@ -68,6 +86,28 @@ static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
68 86
69static void mon_text_ctor(void *, struct kmem_cache *, unsigned long); 87static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
70 88
89struct mon_text_ptr {
90 int cnt, limit;
91 char *pbuf;
92};
93
94static struct mon_event_text *
95 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
96static void mon_text_read_head_t(struct mon_reader_text *rp,
97 struct mon_text_ptr *p, const struct mon_event_text *ep);
98static void mon_text_read_head_u(struct mon_reader_text *rp,
99 struct mon_text_ptr *p, const struct mon_event_text *ep);
100static void mon_text_read_statset(struct mon_reader_text *rp,
101 struct mon_text_ptr *p, const struct mon_event_text *ep);
102static void mon_text_read_intstat(struct mon_reader_text *rp,
103 struct mon_text_ptr *p, const struct mon_event_text *ep);
104static void mon_text_read_isostat(struct mon_reader_text *rp,
105 struct mon_text_ptr *p, const struct mon_event_text *ep);
106static void mon_text_read_isodesc(struct mon_reader_text *rp,
107 struct mon_text_ptr *p, const struct mon_event_text *ep);
108static void mon_text_read_data(struct mon_reader_text *rp,
109 struct mon_text_ptr *p, const struct mon_event_text *ep);
110
71/* 111/*
72 * mon_text_submit 112 * mon_text_submit
73 * mon_text_complete 113 * mon_text_complete
@@ -84,8 +124,10 @@ static inline char mon_text_get_setup(struct mon_event_text *ep,
84 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S') 124 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
85 return '-'; 125 return '-';
86 126
87 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) 127 if (urb->dev->bus->uses_dma &&
128 (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
88 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX); 129 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
130 }
89 if (urb->setup_packet == NULL) 131 if (urb->setup_packet == NULL)
90 return 'Z'; /* '0' would be not as pretty. */ 132 return 'Z'; /* '0' would be not as pretty. */
91 133
@@ -104,10 +146,10 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
104 len = DATA_MAX; 146 len = DATA_MAX;
105 147
106 if (usb_pipein(pipe)) { 148 if (usb_pipein(pipe)) {
107 if (ev_type == 'S') 149 if (ev_type != 'C')
108 return '<'; 150 return '<';
109 } else { 151 } else {
110 if (ev_type == 'C') 152 if (ev_type != 'S')
111 return '>'; 153 return '>';
112 } 154 }
113 155
@@ -120,8 +162,10 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
120 * contain non-NULL garbage in case the upper level promised to 162 * contain non-NULL garbage in case the upper level promised to
121 * set DMA for the HCD. 163 * set DMA for the HCD.
122 */ 164 */
123 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) 165 if (urb->dev->bus->uses_dma &&
166 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
124 return mon_dmapeek(ep->data, urb->transfer_dma, len); 167 return mon_dmapeek(ep->data, urb->transfer_dma, len);
168 }
125 169
126 if (urb->transfer_buffer == NULL) 170 if (urb->transfer_buffer == NULL)
127 return 'Z'; /* '0' would be not as pretty. */ 171 return 'Z'; /* '0' would be not as pretty. */
@@ -146,6 +190,9 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
146{ 190{
147 struct mon_event_text *ep; 191 struct mon_event_text *ep;
148 unsigned int stamp; 192 unsigned int stamp;
193 struct usb_iso_packet_descriptor *fp;
194 struct mon_iso_desc *dp;
195 int i, ndesc;
149 196
150 stamp = mon_get_timestamp(); 197 stamp = mon_get_timestamp();
151 198
@@ -158,12 +205,36 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
158 ep->type = ev_type; 205 ep->type = ev_type;
159 ep->pipe = urb->pipe; 206 ep->pipe = urb->pipe;
160 ep->id = (unsigned long) urb; 207 ep->id = (unsigned long) urb;
208 ep->busnum = urb->dev->bus->busnum;
161 ep->tstamp = stamp; 209 ep->tstamp = stamp;
162 ep->length = (ev_type == 'S') ? 210 ep->length = (ev_type == 'S') ?
163 urb->transfer_buffer_length : urb->actual_length; 211 urb->transfer_buffer_length : urb->actual_length;
164 /* Collecting status makes debugging sense for submits, too */ 212 /* Collecting status makes debugging sense for submits, too */
165 ep->status = urb->status; 213 ep->status = urb->status;
166 214
215 if (usb_pipeint(urb->pipe)) {
216 ep->interval = urb->interval;
217 } else if (usb_pipeisoc(urb->pipe)) {
218 ep->interval = urb->interval;
219 ep->start_frame = urb->start_frame;
220 ep->error_count = urb->error_count;
221 }
222 ep->numdesc = urb->number_of_packets;
223 if (usb_pipeisoc(urb->pipe) && urb->number_of_packets > 0) {
224 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
225 ndesc = ISODESC_MAX;
226 fp = urb->iso_frame_desc;
227 dp = ep->isodesc;
228 for (i = 0; i < ndesc; i++) {
229 dp->status = fp->status;
230 dp->offset = fp->offset;
231 dp->length = (ev_type == 'S') ?
232 fp->length : fp->actual_length;
233 fp++;
234 dp++;
235 }
236 }
237
167 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus); 238 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
168 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type, 239 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
169 rp->r.m_bus); 240 rp->r.m_bus);
@@ -199,6 +270,7 @@ static void mon_text_error(void *data, struct urb *urb, int error)
199 ep->type = 'E'; 270 ep->type = 'E';
200 ep->pipe = urb->pipe; 271 ep->pipe = urb->pipe;
201 ep->id = (unsigned long) urb; 272 ep->id = (unsigned long) urb;
273 ep->busnum = 0;
202 ep->tstamp = 0; 274 ep->tstamp = 0;
203 ep->length = 0; 275 ep->length = 0;
204 ep->status = error; 276 ep->status = error;
@@ -237,13 +309,11 @@ static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
237static int mon_text_open(struct inode *inode, struct file *file) 309static int mon_text_open(struct inode *inode, struct file *file)
238{ 310{
239 struct mon_bus *mbus; 311 struct mon_bus *mbus;
240 struct usb_bus *ubus;
241 struct mon_reader_text *rp; 312 struct mon_reader_text *rp;
242 int rc; 313 int rc;
243 314
244 mutex_lock(&mon_lock); 315 mutex_lock(&mon_lock);
245 mbus = inode->i_private; 316 mbus = inode->i_private;
246 ubus = mbus->u_bus;
247 317
248 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL); 318 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
249 if (rp == NULL) { 319 if (rp == NULL) {
@@ -267,8 +337,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
267 rp->r.rnf_error = mon_text_error; 337 rp->r.rnf_error = mon_text_error;
268 rp->r.rnf_complete = mon_text_complete; 338 rp->r.rnf_complete = mon_text_complete;
269 339
270 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum, 340 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
271 (long)rp);
272 rp->e_slab = kmem_cache_create(rp->slab_name, 341 rp->e_slab = kmem_cache_create(rp->slab_name,
273 sizeof(struct mon_event_text), sizeof(long), 0, 342 sizeof(struct mon_event_text), sizeof(long), 0,
274 mon_text_ctor, NULL); 343 mon_text_ctor, NULL);
@@ -300,17 +369,75 @@ err_alloc:
300 * dd if=/dbg/usbmon/0t bs=10 369 * dd if=/dbg/usbmon/0t bs=10
301 * Also, we do not allow seeks and do not bother advancing the offset. 370 * Also, we do not allow seeks and do not bother advancing the offset.
302 */ 371 */
303static ssize_t mon_text_read(struct file *file, char __user *buf, 372static ssize_t mon_text_read_t(struct file *file, char __user *buf,
304 size_t nbytes, loff_t *ppos) 373 size_t nbytes, loff_t *ppos)
305{ 374{
306 struct mon_reader_text *rp = file->private_data; 375 struct mon_reader_text *rp = file->private_data;
376 struct mon_event_text *ep;
377 struct mon_text_ptr ptr;
378
379 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
380 return PTR_ERR(ep);
381 mutex_lock(&rp->printf_lock);
382 ptr.cnt = 0;
383 ptr.pbuf = rp->printf_buf;
384 ptr.limit = rp->printf_size;
385
386 mon_text_read_head_t(rp, &ptr, ep);
387 mon_text_read_statset(rp, &ptr, ep);
388 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
389 " %d", ep->length);
390 mon_text_read_data(rp, &ptr, ep);
391
392 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
393 ptr.cnt = -EFAULT;
394 mutex_unlock(&rp->printf_lock);
395 kmem_cache_free(rp->e_slab, ep);
396 return ptr.cnt;
397}
398
399static ssize_t mon_text_read_u(struct file *file, char __user *buf,
400 size_t nbytes, loff_t *ppos)
401{
402 struct mon_reader_text *rp = file->private_data;
403 struct mon_event_text *ep;
404 struct mon_text_ptr ptr;
405
406 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
407 return PTR_ERR(ep);
408 mutex_lock(&rp->printf_lock);
409 ptr.cnt = 0;
410 ptr.pbuf = rp->printf_buf;
411 ptr.limit = rp->printf_size;
412
413 mon_text_read_head_u(rp, &ptr, ep);
414 if (ep->type == 'E') {
415 mon_text_read_statset(rp, &ptr, ep);
416 } else if (usb_pipeisoc(ep->pipe)) {
417 mon_text_read_isostat(rp, &ptr, ep);
418 mon_text_read_isodesc(rp, &ptr, ep);
419 } else if (usb_pipeint(ep->pipe)) {
420 mon_text_read_intstat(rp, &ptr, ep);
421 } else {
422 mon_text_read_statset(rp, &ptr, ep);
423 }
424 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
425 " %d", ep->length);
426 mon_text_read_data(rp, &ptr, ep);
427
428 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
429 ptr.cnt = -EFAULT;
430 mutex_unlock(&rp->printf_lock);
431 kmem_cache_free(rp->e_slab, ep);
432 return ptr.cnt;
433}
434
435static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
436 struct file *file)
437{
307 struct mon_bus *mbus = rp->r.m_bus; 438 struct mon_bus *mbus = rp->r.m_bus;
308 DECLARE_WAITQUEUE(waita, current); 439 DECLARE_WAITQUEUE(waita, current);
309 struct mon_event_text *ep; 440 struct mon_event_text *ep;
310 int cnt, limit;
311 char *pbuf;
312 char udir, utype;
313 int data_len, i;
314 441
315 add_wait_queue(&rp->wait, &waita); 442 add_wait_queue(&rp->wait, &waita);
316 set_current_state(TASK_INTERRUPTIBLE); 443 set_current_state(TASK_INTERRUPTIBLE);
@@ -318,7 +445,7 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
318 if (file->f_flags & O_NONBLOCK) { 445 if (file->f_flags & O_NONBLOCK) {
319 set_current_state(TASK_RUNNING); 446 set_current_state(TASK_RUNNING);
320 remove_wait_queue(&rp->wait, &waita); 447 remove_wait_queue(&rp->wait, &waita);
321 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */ 448 return ERR_PTR(-EWOULDBLOCK);
322 } 449 }
323 /* 450 /*
324 * We do not count nwaiters, because ->release is supposed 451 * We do not count nwaiters, because ->release is supposed
@@ -327,17 +454,19 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
327 schedule(); 454 schedule();
328 if (signal_pending(current)) { 455 if (signal_pending(current)) {
329 remove_wait_queue(&rp->wait, &waita); 456 remove_wait_queue(&rp->wait, &waita);
330 return -EINTR; 457 return ERR_PTR(-EINTR);
331 } 458 }
332 set_current_state(TASK_INTERRUPTIBLE); 459 set_current_state(TASK_INTERRUPTIBLE);
333 } 460 }
334 set_current_state(TASK_RUNNING); 461 set_current_state(TASK_RUNNING);
335 remove_wait_queue(&rp->wait, &waita); 462 remove_wait_queue(&rp->wait, &waita);
463 return ep;
464}
336 465
337 mutex_lock(&rp->printf_lock); 466static void mon_text_read_head_t(struct mon_reader_text *rp,
338 cnt = 0; 467 struct mon_text_ptr *p, const struct mon_event_text *ep)
339 pbuf = rp->printf_buf; 468{
340 limit = rp->printf_size; 469 char udir, utype;
341 470
342 udir = usb_pipein(ep->pipe) ? 'i' : 'o'; 471 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
343 switch (usb_pipetype(ep->pipe)) { 472 switch (usb_pipetype(ep->pipe)) {
@@ -346,13 +475,38 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
346 case PIPE_CONTROL: utype = 'C'; break; 475 case PIPE_CONTROL: utype = 'C'; break;
347 default: /* PIPE_BULK */ utype = 'B'; 476 default: /* PIPE_BULK */ utype = 'B';
348 } 477 }
349 cnt += snprintf(pbuf + cnt, limit - cnt, 478 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
350 "%lx %u %c %c%c:%03u:%02u", 479 "%lx %u %c %c%c:%03u:%02u",
351 ep->id, ep->tstamp, ep->type, 480 ep->id, ep->tstamp, ep->type,
352 utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe)); 481 utype, udir,
482 usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
483}
484
485static void mon_text_read_head_u(struct mon_reader_text *rp,
486 struct mon_text_ptr *p, const struct mon_event_text *ep)
487{
488 char udir, utype;
489
490 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
491 switch (usb_pipetype(ep->pipe)) {
492 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
493 case PIPE_INTERRUPT: utype = 'I'; break;
494 case PIPE_CONTROL: utype = 'C'; break;
495 default: /* PIPE_BULK */ utype = 'B';
496 }
497 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
498 "%lx %u %c %c%c:%d:%03u:%u",
499 ep->id, ep->tstamp, ep->type,
500 utype, udir,
501 ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
502}
503
504static void mon_text_read_statset(struct mon_reader_text *rp,
505 struct mon_text_ptr *p, const struct mon_event_text *ep)
506{
353 507
354 if (ep->setup_flag == 0) { /* Setup packet is present and captured */ 508 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
355 cnt += snprintf(pbuf + cnt, limit - cnt, 509 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
356 " s %02x %02x %04x %04x %04x", 510 " s %02x %02x %04x %04x %04x",
357 ep->setup[0], 511 ep->setup[0],
358 ep->setup[1], 512 ep->setup[1],
@@ -360,40 +514,86 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
360 (ep->setup[5] << 8) | ep->setup[4], 514 (ep->setup[5] << 8) | ep->setup[4],
361 (ep->setup[7] << 8) | ep->setup[6]); 515 (ep->setup[7] << 8) | ep->setup[6]);
362 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */ 516 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
363 cnt += snprintf(pbuf + cnt, limit - cnt, 517 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
364 " %c __ __ ____ ____ ____", ep->setup_flag); 518 " %c __ __ ____ ____ ____", ep->setup_flag);
365 } else { /* No setup for this kind of URB */ 519 } else { /* No setup for this kind of URB */
366 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status); 520 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
521 " %d", ep->status);
367 } 522 }
368 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length); 523}
524
525static void mon_text_read_intstat(struct mon_reader_text *rp,
526 struct mon_text_ptr *p, const struct mon_event_text *ep)
527{
528 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
529 " %d:%d", ep->status, ep->interval);
530}
531
532static void mon_text_read_isostat(struct mon_reader_text *rp,
533 struct mon_text_ptr *p, const struct mon_event_text *ep)
534{
535 if (ep->type == 'S') {
536 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
537 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
538 } else {
539 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
540 " %d:%d:%d:%d",
541 ep->status, ep->interval, ep->start_frame, ep->error_count);
542 }
543}
544
545static void mon_text_read_isodesc(struct mon_reader_text *rp,
546 struct mon_text_ptr *p, const struct mon_event_text *ep)
547{
548 int ndesc; /* Display this many */
549 int i;
550 const struct mon_iso_desc *dp;
551
552 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
553 " %d", ep->numdesc);
554 ndesc = ep->numdesc;
555 if (ndesc > ISODESC_MAX)
556 ndesc = ISODESC_MAX;
557 if (ndesc < 0)
558 ndesc = 0;
559 dp = ep->isodesc;
560 for (i = 0; i < ndesc; i++) {
561 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
562 " %d:%u:%u", dp->status, dp->offset, dp->length);
563 dp++;
564 }
565}
566
567static void mon_text_read_data(struct mon_reader_text *rp,
568 struct mon_text_ptr *p, const struct mon_event_text *ep)
569{
570 int data_len, i;
369 571
370 if ((data_len = ep->length) > 0) { 572 if ((data_len = ep->length) > 0) {
371 if (ep->data_flag == 0) { 573 if (ep->data_flag == 0) {
372 cnt += snprintf(pbuf + cnt, limit - cnt, " ="); 574 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
575 " =");
373 if (data_len >= DATA_MAX) 576 if (data_len >= DATA_MAX)
374 data_len = DATA_MAX; 577 data_len = DATA_MAX;
375 for (i = 0; i < data_len; i++) { 578 for (i = 0; i < data_len; i++) {
376 if (i % 4 == 0) { 579 if (i % 4 == 0) {
377 cnt += snprintf(pbuf + cnt, limit - cnt, 580 p->cnt += snprintf(p->pbuf + p->cnt,
581 p->limit - p->cnt,
378 " "); 582 " ");
379 } 583 }
380 cnt += snprintf(pbuf + cnt, limit - cnt, 584 p->cnt += snprintf(p->pbuf + p->cnt,
585 p->limit - p->cnt,
381 "%02x", ep->data[i]); 586 "%02x", ep->data[i]);
382 } 587 }
383 cnt += snprintf(pbuf + cnt, limit - cnt, "\n"); 588 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
589 "\n");
384 } else { 590 } else {
385 cnt += snprintf(pbuf + cnt, limit - cnt, 591 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
386 " %c\n", ep->data_flag); 592 " %c\n", ep->data_flag);
387 } 593 }
388 } else { 594 } else {
389 cnt += snprintf(pbuf + cnt, limit - cnt, "\n"); 595 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
390 } 596 }
391
392 if (copy_to_user(buf, rp->printf_buf, cnt))
393 cnt = -EFAULT;
394 mutex_unlock(&rp->printf_lock);
395 kmem_cache_free(rp->e_slab, ep);
396 return cnt;
397} 597}
398 598
399static int mon_text_release(struct inode *inode, struct file *file) 599static int mon_text_release(struct inode *inode, struct file *file)
@@ -439,34 +639,46 @@ static int mon_text_release(struct inode *inode, struct file *file)
439 return 0; 639 return 0;
440} 640}
441 641
442static const struct file_operations mon_fops_text = { 642static const struct file_operations mon_fops_text_t = {
443 .owner = THIS_MODULE, 643 .owner = THIS_MODULE,
444 .open = mon_text_open, 644 .open = mon_text_open,
445 .llseek = no_llseek, 645 .llseek = no_llseek,
446 .read = mon_text_read, 646 .read = mon_text_read_t,
447 /* .write = mon_text_write, */
448 /* .poll = mon_text_poll, */
449 /* .ioctl = mon_text_ioctl, */
450 .release = mon_text_release, 647 .release = mon_text_release,
451}; 648};
452 649
453int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus) 650static const struct file_operations mon_fops_text_u = {
651 .owner = THIS_MODULE,
652 .open = mon_text_open,
653 .llseek = no_llseek,
654 .read = mon_text_read_u,
655 .release = mon_text_release,
656};
657
658int mon_text_add(struct mon_bus *mbus, int busnum)
454{ 659{
455 struct dentry *d; 660 struct dentry *d;
456 enum { NAMESZ = 10 }; 661 enum { NAMESZ = 10 };
457 char name[NAMESZ]; 662 char name[NAMESZ];
458 int rc; 663 int rc;
459 664
460 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum); 665 rc = snprintf(name, NAMESZ, "%dt", busnum);
461 if (rc <= 0 || rc >= NAMESZ) 666 if (rc <= 0 || rc >= NAMESZ)
462 goto err_print_t; 667 goto err_print_t;
463 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text); 668 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
464 if (d == NULL) 669 if (d == NULL)
465 goto err_create_t; 670 goto err_create_t;
466 mbus->dent_t = d; 671 mbus->dent_t = d;
467 672
468 /* XXX The stats do not belong to here (text API), but oh well... */ 673 rc = snprintf(name, NAMESZ, "%du", busnum);
469 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum); 674 if (rc <= 0 || rc >= NAMESZ)
675 goto err_print_u;
676 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
677 if (d == NULL)
678 goto err_create_u;
679 mbus->dent_u = d;
680
681 rc = snprintf(name, NAMESZ, "%ds", busnum);
470 if (rc <= 0 || rc >= NAMESZ) 682 if (rc <= 0 || rc >= NAMESZ)
471 goto err_print_s; 683 goto err_print_s;
472 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat); 684 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
@@ -478,6 +690,10 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
478 690
479err_create_s: 691err_create_s:
480err_print_s: 692err_print_s:
693 debugfs_remove(mbus->dent_u);
694 mbus->dent_u = NULL;
695err_create_u:
696err_print_u:
481 debugfs_remove(mbus->dent_t); 697 debugfs_remove(mbus->dent_t);
482 mbus->dent_t = NULL; 698 mbus->dent_t = NULL;
483err_create_t: 699err_create_t:
@@ -487,6 +703,7 @@ err_print_t:
487 703
488void mon_text_del(struct mon_bus *mbus) 704void mon_text_del(struct mon_bus *mbus)
489{ 705{
706 debugfs_remove(mbus->dent_u);
490 debugfs_remove(mbus->dent_t); 707 debugfs_remove(mbus->dent_t);
491 debugfs_remove(mbus->dent_s); 708 debugfs_remove(mbus->dent_s);
492} 709}
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index efdfd8993d9e..13d63255283e 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -22,7 +22,7 @@ struct mon_bus {
22 int text_inited; 22 int text_inited;
23 struct dentry *dent_s; /* Debugging file */ 23 struct dentry *dent_s; /* Debugging file */
24 struct dentry *dent_t; /* Text interface file */ 24 struct dentry *dent_t; /* Text interface file */
25 int uses_dma; 25 struct dentry *dent_u; /* Second text interface file */
26 26
27 /* Ref */ 27 /* Ref */
28 int nreaders; /* Under mon_lock AND mbus->lock */ 28 int nreaders; /* Under mon_lock AND mbus->lock */
@@ -52,7 +52,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
52 52
53struct mon_bus *mon_bus_lookup(unsigned int num); 53struct mon_bus *mon_bus_lookup(unsigned int num);
54 54
55int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus); 55int /*bool*/ mon_text_add(struct mon_bus *mbus, int busnum);
56void mon_text_del(struct mon_bus *mbus); 56void mon_text_del(struct mon_bus *mbus);
57// void mon_bin_add(struct mon_bus *); 57// void mon_bin_add(struct mon_bus *);
58 58
@@ -81,4 +81,6 @@ extern struct mutex mon_lock;
81 81
82extern const struct file_operations mon_fops_stat; 82extern const struct file_operations mon_fops_stat;
83 83
84extern struct mon_bus mon_bus0; /* Only for redundant checks */
85
84#endif /* __USB_MON_H */ 86#endif /* __USB_MON_H */
diff --git a/drivers/usb/net/catc.c b/drivers/usb/net/catc.c
index ffec2e01b896..86e90c59d551 100644
--- a/drivers/usb/net/catc.c
+++ b/drivers/usb/net/catc.c
@@ -355,7 +355,7 @@ resubmit:
355 * Transmit routines. 355 * Transmit routines.
356 */ 356 */
357 357
358static void catc_tx_run(struct catc *catc) 358static int catc_tx_run(struct catc *catc)
359{ 359{
360 int status; 360 int status;
361 361
@@ -373,12 +373,14 @@ static void catc_tx_run(struct catc *catc)
373 catc->tx_ptr = 0; 373 catc->tx_ptr = 0;
374 374
375 catc->netdev->trans_start = jiffies; 375 catc->netdev->trans_start = jiffies;
376 return status;
376} 377}
377 378
378static void catc_tx_done(struct urb *urb) 379static void catc_tx_done(struct urb *urb)
379{ 380{
380 struct catc *catc = urb->context; 381 struct catc *catc = urb->context;
381 unsigned long flags; 382 unsigned long flags;
383 int r;
382 384
383 if (urb->status == -ECONNRESET) { 385 if (urb->status == -ECONNRESET) {
384 dbg("Tx Reset."); 386 dbg("Tx Reset.");
@@ -397,10 +399,13 @@ static void catc_tx_done(struct urb *urb)
397 399
398 spin_lock_irqsave(&catc->tx_lock, flags); 400 spin_lock_irqsave(&catc->tx_lock, flags);
399 401
400 if (catc->tx_ptr) 402 if (catc->tx_ptr) {
401 catc_tx_run(catc); 403 r = catc_tx_run(catc);
402 else 404 if (unlikely(r < 0))
405 clear_bit(TX_RUNNING, &catc->flags);
406 } else {
403 clear_bit(TX_RUNNING, &catc->flags); 407 clear_bit(TX_RUNNING, &catc->flags);
408 }
404 409
405 netif_wake_queue(catc->netdev); 410 netif_wake_queue(catc->netdev);
406 411
@@ -411,6 +416,7 @@ static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
411{ 416{
412 struct catc *catc = netdev_priv(netdev); 417 struct catc *catc = netdev_priv(netdev);
413 unsigned long flags; 418 unsigned long flags;
419 int r = 0;
414 char *tx_buf; 420 char *tx_buf;
415 421
416 spin_lock_irqsave(&catc->tx_lock, flags); 422 spin_lock_irqsave(&catc->tx_lock, flags);
@@ -421,8 +427,11 @@ static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
421 skb_copy_from_linear_data(skb, tx_buf + 2, skb->len); 427 skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
422 catc->tx_ptr += skb->len + 2; 428 catc->tx_ptr += skb->len + 2;
423 429
424 if (!test_and_set_bit(TX_RUNNING, &catc->flags)) 430 if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
425 catc_tx_run(catc); 431 r = catc_tx_run(catc);
432 if (r < 0)
433 clear_bit(TX_RUNNING, &catc->flags);
434 }
426 435
427 if ((catc->is_f5u011 && catc->tx_ptr) 436 if ((catc->is_f5u011 && catc->tx_ptr)
428 || (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2)))) 437 || (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
@@ -430,8 +439,10 @@ static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
430 439
431 spin_unlock_irqrestore(&catc->tx_lock, flags); 440 spin_unlock_irqrestore(&catc->tx_lock, flags);
432 441
433 catc->stats.tx_bytes += skb->len; 442 if (r >= 0) {
434 catc->stats.tx_packets++; 443 catc->stats.tx_bytes += skb->len;
444 catc->stats.tx_packets++;
445 }
435 446
436 dev_kfree_skb(skb); 447 dev_kfree_skb(skb);
437 448
diff --git a/drivers/usb/net/dm9601.c b/drivers/usb/net/dm9601.c
index 5130cc7eb314..a67638601477 100644
--- a/drivers/usb/net/dm9601.c
+++ b/drivers/usb/net/dm9601.c
@@ -12,6 +12,7 @@
12 12
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/sched.h> 14#include <linux/sched.h>
15#include <linux/stddef.h>
15#include <linux/init.h> 16#include <linux/init.h>
16#include <linux/netdevice.h> 17#include <linux/netdevice.h>
17#include <linux/etherdevice.h> 18#include <linux/etherdevice.h>
@@ -85,7 +86,7 @@ static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
85 usb_sndctrlpipe(dev->udev, 0), 86 usb_sndctrlpipe(dev->udev, 0),
86 DM_WRITE_REG, 87 DM_WRITE_REG,
87 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE, 88 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
88 value, reg, 0, 0, USB_CTRL_SET_TIMEOUT); 89 value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
89} 90}
90 91
91static void dm_write_async_callback(struct urb *urb) 92static void dm_write_async_callback(struct urb *urb)
@@ -171,7 +172,7 @@ static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
171 172
172 usb_fill_control_urb(urb, dev->udev, 173 usb_fill_control_urb(urb, dev->udev,
173 usb_sndctrlpipe(dev->udev, 0), 174 usb_sndctrlpipe(dev->udev, 0),
174 (void *)req, 0, 0, dm_write_async_callback, req); 175 (void *)req, NULL, 0, dm_write_async_callback, req);
175 176
176 status = usb_submit_urb(urb, GFP_ATOMIC); 177 status = usb_submit_urb(urb, GFP_ATOMIC);
177 if (status < 0) { 178 if (status < 0) {
diff --git a/drivers/usb/net/rndis_host.c b/drivers/usb/net/rndis_host.c
index 1d36772ba6e1..980e4aaa97aa 100644
--- a/drivers/usb/net/rndis_host.c
+++ b/drivers/usb/net/rndis_host.c
@@ -253,6 +253,7 @@ struct rndis_keepalive_c { /* IN (optionally OUT) */
253 * of that mess as possible. 253 * of that mess as possible.
254 */ 254 */
255#define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101) 255#define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
256#define OID_GEN_MAXIMUM_FRAME_SIZE ccpu2(0x00010106)
256#define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e) 257#define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e)
257 258
258/* 259/*
@@ -349,7 +350,7 @@ static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
349 case RNDIS_MSG_INDICATE: { /* fault */ 350 case RNDIS_MSG_INDICATE: { /* fault */
350 // struct rndis_indicate *msg = (void *)buf; 351 // struct rndis_indicate *msg = (void *)buf;
351 dev_info(&info->control->dev, 352 dev_info(&info->control->dev,
352 "rndis fault indication\n"); 353 "rndis fault indication\n");
353 } 354 }
354 break; 355 break;
355 case RNDIS_MSG_KEEPALIVE: { /* ping */ 356 case RNDIS_MSG_KEEPALIVE: { /* ping */
@@ -387,6 +388,71 @@ static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
387 return -ETIMEDOUT; 388 return -ETIMEDOUT;
388} 389}
389 390
391/*
392 * rndis_query:
393 *
394 * Performs a query for @oid along with 0 or more bytes of payload as
395 * specified by @in_len. If @reply_len is not set to -1 then the reply
396 * length is checked against this value, resulting in an error if it
397 * doesn't match.
398 *
399 * NOTE: Adding a payload exactly or greater than the size of the expected
400 * response payload is an evident requirement MSFT added for ActiveSync.
401 *
402 * The only exception is for OIDs that return a variably sized response,
403 * in which case no payload should be added. This undocumented (and
404 * nonsensical!) issue was found by sniffing protocol requests from the
405 * ActiveSync 4.1 Windows driver.
406 */
407static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
408 void *buf, u32 oid, u32 in_len,
409 void **reply, int *reply_len)
410{
411 int retval;
412 union {
413 void *buf;
414 struct rndis_msg_hdr *header;
415 struct rndis_query *get;
416 struct rndis_query_c *get_c;
417 } u;
418 u32 off, len;
419
420 u.buf = buf;
421
422 memset(u.get, 0, sizeof *u.get + in_len);
423 u.get->msg_type = RNDIS_MSG_QUERY;
424 u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
425 u.get->oid = oid;
426 u.get->len = cpu_to_le32(in_len);
427 u.get->offset = ccpu2(20);
428
429 retval = rndis_command(dev, u.header);
430 if (unlikely(retval < 0)) {
431 dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
432 oid, retval);
433 return retval;
434 }
435
436 off = le32_to_cpu(u.get_c->offset);
437 len = le32_to_cpu(u.get_c->len);
438 if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
439 goto response_error;
440
441 if (*reply_len != -1 && len != *reply_len)
442 goto response_error;
443
444 *reply = (unsigned char *) &u.get_c->request_id + off;
445 *reply_len = len;
446
447 return retval;
448
449response_error:
450 dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
451 "invalid response - off %d len %d\n",
452 oid, off, len);
453 return -EDOM;
454}
455
390static int rndis_bind(struct usbnet *dev, struct usb_interface *intf) 456static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
391{ 457{
392 int retval; 458 int retval;
@@ -403,6 +469,8 @@ static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
403 struct rndis_set_c *set_c; 469 struct rndis_set_c *set_c;
404 } u; 470 } u;
405 u32 tmp; 471 u32 tmp;
472 int reply_len;
473 unsigned char *bp;
406 474
407 /* we can't rely on i/o from stack working, or stack allocation */ 475 /* we can't rely on i/o from stack working, or stack allocation */
408 u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL); 476 u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
@@ -421,6 +489,12 @@ static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
421 * TX we'll stick to one Ethernet packet plus RNDIS framing. 489 * TX we'll stick to one Ethernet packet plus RNDIS framing.
422 * For RX we handle drivers that zero-pad to end-of-packet. 490 * For RX we handle drivers that zero-pad to end-of-packet.
423 * Don't let userspace change these settings. 491 * Don't let userspace change these settings.
492 *
493 * NOTE: there still seems to be wierdness here, as if we need
494 * to do some more things to make sure WinCE targets accept this.
495 * They default to jumbograms of 8KB or 16KB, which is absurd
496 * for such low data rates and which is also more than Linux
497 * can usually expect to allocate for SKB data...
424 */ 498 */
425 net->hard_header_len += sizeof (struct rndis_data_hdr); 499 net->hard_header_len += sizeof (struct rndis_data_hdr);
426 dev->hard_mtu = net->mtu + net->hard_header_len; 500 dev->hard_mtu = net->mtu + net->hard_header_len;
@@ -434,7 +508,7 @@ static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
434 if (unlikely(retval < 0)) { 508 if (unlikely(retval < 0)) {
435 /* it might not even be an RNDIS device!! */ 509 /* it might not even be an RNDIS device!! */
436 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval); 510 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
437 goto fail_and_release; 511 goto fail_and_release;
438 } 512 }
439 tmp = le32_to_cpu(u.init_c->max_transfer_size); 513 tmp = le32_to_cpu(u.init_c->max_transfer_size);
440 if (tmp < dev->hard_mtu) { 514 if (tmp < dev->hard_mtu) {
@@ -450,34 +524,15 @@ static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
450 dev->hard_mtu, tmp, dev->rx_urb_size, 524 dev->hard_mtu, tmp, dev->rx_urb_size,
451 1 << le32_to_cpu(u.init_c->packet_alignment)); 525 1 << le32_to_cpu(u.init_c->packet_alignment));
452 526
453 /* Get designated host ethernet address. 527 /* Get designated host ethernet address */
454 * 528 reply_len = ETH_ALEN;
455 * Adding a payload exactly the same size as the expected response 529 retval = rndis_query(dev, intf, u.buf, OID_802_3_PERMANENT_ADDRESS,
456 * payload is an evident requirement MSFT added for ActiveSync. 530 48, (void **) &bp, &reply_len);
457 * This undocumented (and nonsensical) issue was found by sniffing 531 if (unlikely(retval< 0)) {
458 * protocol requests from the ActiveSync 4.1 Windows driver.
459 */
460 memset(u.get, 0, sizeof *u.get + 48);
461 u.get->msg_type = RNDIS_MSG_QUERY;
462 u.get->msg_len = ccpu2(sizeof *u.get + 48);
463 u.get->oid = OID_802_3_PERMANENT_ADDRESS;
464 u.get->len = ccpu2(48);
465 u.get->offset = ccpu2(20);
466
467 retval = rndis_command(dev, u.header);
468 if (unlikely(retval < 0)) {
469 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval); 532 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
470 goto fail_and_release; 533 goto fail_and_release;
471 } 534 }
472 tmp = le32_to_cpu(u.get_c->offset); 535 memcpy(net->dev_addr, bp, ETH_ALEN);
473 if (unlikely((tmp + 8) > (CONTROL_BUFFER_SIZE - ETH_ALEN)
474 || u.get_c->len != ccpu2(ETH_ALEN))) {
475 dev_err(&intf->dev, "rndis ethaddr off %d len %d ?\n",
476 tmp, le32_to_cpu(u.get_c->len));
477 retval = -EDOM;
478 goto fail_and_release;
479 }
480 memcpy(net->dev_addr, tmp + (char *)&u.get_c->request_id, ETH_ALEN);
481 536
482 /* set a nonzero filter to enable data transfers */ 537 /* set a nonzero filter to enable data transfers */
483 memset(u.set, 0, sizeof *u.set); 538 memset(u.set, 0, sizeof *u.set);
@@ -502,6 +557,7 @@ static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
502fail_and_release: 557fail_and_release:
503 usb_set_intfdata(info->data, NULL); 558 usb_set_intfdata(info->data, NULL);
504 usb_driver_release_interface(driver_of(intf), info->data); 559 usb_driver_release_interface(driver_of(intf), info->data);
560 info->data = NULL;
505fail: 561fail:
506 kfree(u.buf); 562 kfree(u.buf);
507 return retval; 563 return retval;
@@ -618,7 +674,7 @@ fill:
618 674
619static const struct driver_info rndis_info = { 675static const struct driver_info rndis_info = {
620 .description = "RNDIS device", 676 .description = "RNDIS device",
621 .flags = FLAG_ETHER | FLAG_FRAMING_RN, 677 .flags = FLAG_ETHER | FLAG_FRAMING_RN | FLAG_NO_SETINT,
622 .bind = rndis_bind, 678 .bind = rndis_bind,
623 .unbind = rndis_unbind, 679 .unbind = rndis_unbind,
624 .status = rndis_status, 680 .status = rndis_status,
diff --git a/drivers/usb/net/usbnet.c b/drivers/usb/net/usbnet.c
index 0c5465a7909b..f9cd42d058b0 100644
--- a/drivers/usb/net/usbnet.c
+++ b/drivers/usb/net/usbnet.c
@@ -734,8 +734,7 @@ void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
734{ 734{
735 struct usbnet *dev = netdev_priv(net); 735 struct usbnet *dev = netdev_priv(net);
736 736
737 /* REVISIT don't always return "usbnet" */ 737 strncpy (info->driver, dev->driver_name, sizeof info->driver);
738 strncpy (info->driver, driver_name, sizeof info->driver);
739 strncpy (info->version, DRIVER_VERSION, sizeof info->version); 738 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
740 strncpy (info->fw_version, dev->driver_info->description, 739 strncpy (info->fw_version, dev->driver_info->description,
741 sizeof info->fw_version); 740 sizeof info->fw_version);
@@ -1115,10 +1114,12 @@ usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1115 struct driver_info *info; 1114 struct driver_info *info;
1116 struct usb_device *xdev; 1115 struct usb_device *xdev;
1117 int status; 1116 int status;
1117 const char *name;
1118 1118
1119 name = udev->dev.driver->name;
1119 info = (struct driver_info *) prod->driver_info; 1120 info = (struct driver_info *) prod->driver_info;
1120 if (!info) { 1121 if (!info) {
1121 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name); 1122 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1122 return -ENODEV; 1123 return -ENODEV;
1123 } 1124 }
1124 xdev = interface_to_usbdev (udev); 1125 xdev = interface_to_usbdev (udev);
@@ -1138,6 +1139,7 @@ usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1138 dev = netdev_priv(net); 1139 dev = netdev_priv(net);
1139 dev->udev = xdev; 1140 dev->udev = xdev;
1140 dev->driver_info = info; 1141 dev->driver_info = info;
1142 dev->driver_name = name;
1141 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 1143 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1142 | NETIF_MSG_PROBE | NETIF_MSG_LINK); 1144 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1143 skb_queue_head_init (&dev->rxq); 1145 skb_queue_head_init (&dev->rxq);
diff --git a/drivers/usb/net/usbnet.h b/drivers/usb/net/usbnet.h
index 07c70abbe0ec..cbb53e065d6c 100644
--- a/drivers/usb/net/usbnet.h
+++ b/drivers/usb/net/usbnet.h
@@ -29,6 +29,7 @@ struct usbnet {
29 /* housekeeping */ 29 /* housekeeping */
30 struct usb_device *udev; 30 struct usb_device *udev;
31 struct driver_info *driver_info; 31 struct driver_info *driver_info;
32 const char *driver_name;
32 wait_queue_head_t *wait; 33 wait_queue_head_t *wait;
33 struct mutex phy_mutex; 34 struct mutex phy_mutex;
34 35
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index 2f4d303ee36f..ba5d1dc03036 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -423,11 +423,11 @@ config USB_SERIAL_MCT_U232
423 module will be called mct_u232. 423 module will be called mct_u232.
424 424
425config USB_SERIAL_MOS7720 425config USB_SERIAL_MOS7720
426 tristate "USB Moschip 7720 Single Port Serial Driver" 426 tristate "USB Moschip 7720 Serial Driver"
427 depends on USB_SERIAL 427 depends on USB_SERIAL
428 ---help--- 428 ---help---
429 Say Y here if you want to use a USB Serial single port adapter from 429 Say Y here if you want to use USB Serial single and double
430 Moschip Semiconductor Tech. 430 port adapters from Moschip Semiconductor Tech.
431 431
432 To compile this driver as a module, choose M here: the 432 To compile this driver as a module, choose M here: the
433 module will be called mos7720. 433 module will be called mos7720.
diff --git a/drivers/usb/serial/aircable.c b/drivers/usb/serial/aircable.c
index 11dad42c3c60..b675735bfbee 100644
--- a/drivers/usb/serial/aircable.c
+++ b/drivers/usb/serial/aircable.c
@@ -209,6 +209,7 @@ static void aircable_send(struct usb_serial_port *port)
209 int count, result; 209 int count, result;
210 struct aircable_private *priv = usb_get_serial_port_data(port); 210 struct aircable_private *priv = usb_get_serial_port_data(port);
211 unsigned char* buf; 211 unsigned char* buf;
212 u16 *dbuf;
212 dbg("%s - port %d", __FUNCTION__, port->number); 213 dbg("%s - port %d", __FUNCTION__, port->number);
213 if (port->write_urb_busy) 214 if (port->write_urb_busy)
214 return; 215 return;
@@ -226,8 +227,8 @@ static void aircable_send(struct usb_serial_port *port)
226 227
227 buf[0] = TX_HEADER_0; 228 buf[0] = TX_HEADER_0;
228 buf[1] = TX_HEADER_1; 229 buf[1] = TX_HEADER_1;
229 buf[2] = (unsigned char)count; 230 dbuf = (u16 *)&buf[2];
230 buf[3] = (unsigned char)(count >> 8); 231 *dbuf = cpu_to_le16((u16)count);
231 serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE); 232 serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
232 233
233 memcpy(port->write_urb->transfer_buffer, buf, 234 memcpy(port->write_urb->transfer_buffer, buf,
@@ -434,7 +435,7 @@ static void aircable_write_bulk_callback(struct urb *urb)
434 __FUNCTION__, urb->status); 435 __FUNCTION__, urb->status);
435 port->write_urb->transfer_buffer_length = 1; 436 port->write_urb->transfer_buffer_length = 1;
436 port->write_urb->dev = port->serial->dev; 437 port->write_urb->dev = port->serial->dev;
437 result = usb_submit_urb(port->write_urb, GFP_KERNEL); 438 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
438 if (result) 439 if (result)
439 dev_err(&urb->dev->dev, 440 dev_err(&urb->dev->dev,
440 "%s - failed resubmitting write urb, error %d\n", 441 "%s - failed resubmitting write urb, error %d\n",
diff --git a/drivers/usb/serial/ark3116.c b/drivers/usb/serial/ark3116.c
index edd685791a6b..ea2175bb2274 100644
--- a/drivers/usb/serial/ark3116.c
+++ b/drivers/usb/serial/ark3116.c
@@ -341,7 +341,7 @@ static int ark3116_open(struct usb_serial_port *port, struct file *filp)
341 341
342 result = usb_serial_generic_open(port, filp); 342 result = usb_serial_generic_open(port, filp);
343 if (result) 343 if (result)
344 return result; 344 goto err_out;
345 345
346 /* open */ 346 /* open */
347 ARK3116_RCV(serial, 111, 0xFE, 0xC0, 0x0000, 0x0003, 0x02, buf); 347 ARK3116_RCV(serial, 111, 0xFE, 0xC0, 0x0000, 0x0003, 0x02, buf);
@@ -372,6 +372,7 @@ static int ark3116_open(struct usb_serial_port *port, struct file *filp)
372 if (port->tty) 372 if (port->tty)
373 ark3116_set_termios(port, &tmp_termios); 373 ark3116_set_termios(port, &tmp_termios);
374 374
375err_out:
375 kfree(buf); 376 kfree(buf);
376 377
377 return result; 378 return result;
diff --git a/drivers/usb/serial/cp2101.c b/drivers/usb/serial/cp2101.c
index d7d0ba986a80..e831cb7f64fd 100644
--- a/drivers/usb/serial/cp2101.c
+++ b/drivers/usb/serial/cp2101.c
@@ -58,9 +58,11 @@ static struct usb_device_id id_table [] = {
58 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */ 58 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
59 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */ 59 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
60 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */ 60 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
61 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
61 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */ 62 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
62 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */ 63 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
63 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */ 64 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
65 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
64 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */ 66 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
65 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */ 67 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
66 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */ 68 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 8ff9d54b21e6..95a1805b064f 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -342,6 +342,7 @@ static struct usb_device_id id_table_combined [] = {
342 { USB_DEVICE(FTDI_VID, FTDI_PERLE_ULTRAPORT_PID) }, 342 { USB_DEVICE(FTDI_VID, FTDI_PERLE_ULTRAPORT_PID) },
343 { USB_DEVICE(FTDI_VID, FTDI_PIEGROUP_PID) }, 343 { USB_DEVICE(FTDI_VID, FTDI_PIEGROUP_PID) },
344 { USB_DEVICE(FTDI_VID, FTDI_TNC_X_PID) }, 344 { USB_DEVICE(FTDI_VID, FTDI_TNC_X_PID) },
345 { USB_DEVICE(FTDI_VID, FTDI_USBX_707_PID) },
345 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2101_PID) }, 346 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2101_PID) },
346 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2102_PID) }, 347 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2102_PID) },
347 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2103_PID) }, 348 { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2103_PID) },
@@ -1433,6 +1434,7 @@ static int ftdi_write (struct usb_serial_port *port,
1433 dbg("%s - write limit hit\n", __FUNCTION__); 1434 dbg("%s - write limit hit\n", __FUNCTION__);
1434 return 0; 1435 return 0;
1435 } 1436 }
1437 priv->tx_outstanding_urbs++;
1436 spin_unlock_irqrestore(&priv->tx_lock, flags); 1438 spin_unlock_irqrestore(&priv->tx_lock, flags);
1437 1439
1438 data_offset = priv->write_offset; 1440 data_offset = priv->write_offset;
@@ -1450,14 +1452,15 @@ static int ftdi_write (struct usb_serial_port *port,
1450 buffer = kmalloc (transfer_size, GFP_ATOMIC); 1452 buffer = kmalloc (transfer_size, GFP_ATOMIC);
1451 if (!buffer) { 1453 if (!buffer) {
1452 err("%s ran out of kernel memory for urb ...", __FUNCTION__); 1454 err("%s ran out of kernel memory for urb ...", __FUNCTION__);
1453 return -ENOMEM; 1455 count = -ENOMEM;
1456 goto error_no_buffer;
1454 } 1457 }
1455 1458
1456 urb = usb_alloc_urb(0, GFP_ATOMIC); 1459 urb = usb_alloc_urb(0, GFP_ATOMIC);
1457 if (!urb) { 1460 if (!urb) {
1458 err("%s - no more free urbs", __FUNCTION__); 1461 err("%s - no more free urbs", __FUNCTION__);
1459 kfree (buffer); 1462 count = -ENOMEM;
1460 return -ENOMEM; 1463 goto error_no_urb;
1461 } 1464 }
1462 1465
1463 /* Copy data */ 1466 /* Copy data */
@@ -1499,10 +1502,9 @@ static int ftdi_write (struct usb_serial_port *port,
1499 if (status) { 1502 if (status) {
1500 err("%s - failed submitting write urb, error %d", __FUNCTION__, status); 1503 err("%s - failed submitting write urb, error %d", __FUNCTION__, status);
1501 count = status; 1504 count = status;
1502 kfree (buffer); 1505 goto error;
1503 } else { 1506 } else {
1504 spin_lock_irqsave(&priv->tx_lock, flags); 1507 spin_lock_irqsave(&priv->tx_lock, flags);
1505 ++priv->tx_outstanding_urbs;
1506 priv->tx_outstanding_bytes += count; 1508 priv->tx_outstanding_bytes += count;
1507 priv->tx_bytes += count; 1509 priv->tx_bytes += count;
1508 spin_unlock_irqrestore(&priv->tx_lock, flags); 1510 spin_unlock_irqrestore(&priv->tx_lock, flags);
@@ -1510,10 +1512,19 @@ static int ftdi_write (struct usb_serial_port *port,
1510 1512
1511 /* we are done with this urb, so let the host driver 1513 /* we are done with this urb, so let the host driver
1512 * really free it when it is finished with it */ 1514 * really free it when it is finished with it */
1513 usb_free_urb (urb); 1515 usb_free_urb(urb);
1514 1516
1515 dbg("%s write returning: %d", __FUNCTION__, count); 1517 dbg("%s write returning: %d", __FUNCTION__, count);
1516 return count; 1518 return count;
1519error:
1520 usb_free_urb(urb);
1521error_no_urb:
1522 kfree (buffer);
1523error_no_buffer:
1524 spin_lock_irqsave(&priv->tx_lock, flags);
1525 priv->tx_outstanding_urbs--;
1526 spin_unlock_irqrestore(&priv->tx_lock, flags);
1527 return count;
1517} /* ftdi_write */ 1528} /* ftdi_write */
1518 1529
1519 1530
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 513cfe1b768b..77ad0a09b384 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -31,6 +31,7 @@
31#define FTDI_RELAIS_PID 0xFA10 /* Relais device from Rudolf Gugler */ 31#define FTDI_RELAIS_PID 0xFA10 /* Relais device from Rudolf Gugler */
32#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */ 32#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */
33#define FTDI_NF_RIC_PID 0x0001 /* Product Id */ 33#define FTDI_NF_RIC_PID 0x0001 /* Product Id */
34#define FTDI_USBX_707_PID 0xF857 /* ADSTech IR Blaster USBX-707 */
34 35
35 36
36/* www.canusb.com Lawicel CANUSB device */ 37/* www.canusb.com Lawicel CANUSB device */
diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c
index 6a26a2e683a6..18f74ac76565 100644
--- a/drivers/usb/serial/io_edgeport.c
+++ b/drivers/usb/serial/io_edgeport.c
@@ -111,7 +111,7 @@ struct edgeport_port {
111 111
112 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */ 112 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
113 struct urb *write_urb; /* write URB for this port */ 113 struct urb *write_urb; /* write URB for this port */
114 char write_in_progress; /* TRUE while a write URB is outstanding */ 114 bool write_in_progress; /* 'true' while a write URB is outstanding */
115 spinlock_t ep_lock; 115 spinlock_t ep_lock;
116 116
117 __u8 shadowLCR; /* last LCR value received */ 117 __u8 shadowLCR; /* last LCR value received */
@@ -123,11 +123,11 @@ struct edgeport_port {
123 __u8 validDataMask; 123 __u8 validDataMask;
124 __u32 baudRate; 124 __u32 baudRate;
125 125
126 char open; 126 bool open;
127 char openPending; 127 bool openPending;
128 char commandPending; 128 bool commandPending;
129 char closePending; 129 bool closePending;
130 char chaseResponsePending; 130 bool chaseResponsePending;
131 131
132 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 132 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
133 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */ 133 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
@@ -156,7 +156,7 @@ struct edgeport_serial {
156 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */ 156 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
157 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */ 157 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
158 struct urb * read_urb; /* our bulk read urb */ 158 struct urb * read_urb; /* our bulk read urb */
159 int read_in_progress; 159 bool read_in_progress;
160 spinlock_t es_lock; 160 spinlock_t es_lock;
161 161
162 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */ 162 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
@@ -212,7 +212,7 @@ static int debug;
212 212
213static int low_latency = 1; /* tty low latency flag, on by default */ 213static int low_latency = 1; /* tty low latency flag, on by default */
214 214
215static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */ 215static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
216 216
217 217
218/* local function prototypes */ 218/* local function prototypes */
@@ -631,14 +631,14 @@ static void edge_interrupt_callback (struct urb *urb)
631 if (edge_serial->rxBytesAvail > 0 && 631 if (edge_serial->rxBytesAvail > 0 &&
632 !edge_serial->read_in_progress) { 632 !edge_serial->read_in_progress) {
633 dbg("%s - posting a read", __FUNCTION__); 633 dbg("%s - posting a read", __FUNCTION__);
634 edge_serial->read_in_progress = TRUE; 634 edge_serial->read_in_progress = true;
635 635
636 /* we have pending bytes on the bulk in pipe, send a request */ 636 /* we have pending bytes on the bulk in pipe, send a request */
637 edge_serial->read_urb->dev = edge_serial->serial->dev; 637 edge_serial->read_urb->dev = edge_serial->serial->dev;
638 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC); 638 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
639 if (result) { 639 if (result) {
640 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result); 640 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
641 edge_serial->read_in_progress = FALSE; 641 edge_serial->read_in_progress = false;
642 } 642 }
643 } 643 }
644 spin_unlock(&edge_serial->es_lock); 644 spin_unlock(&edge_serial->es_lock);
@@ -695,13 +695,13 @@ static void edge_bulk_in_callback (struct urb *urb)
695 695
696 if (urb->status) { 696 if (urb->status) {
697 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); 697 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
698 edge_serial->read_in_progress = FALSE; 698 edge_serial->read_in_progress = false;
699 return; 699 return;
700 } 700 }
701 701
702 if (urb->actual_length == 0) { 702 if (urb->actual_length == 0) {
703 dbg("%s - read bulk callback with no data", __FUNCTION__); 703 dbg("%s - read bulk callback with no data", __FUNCTION__);
704 edge_serial->read_in_progress = FALSE; 704 edge_serial->read_in_progress = false;
705 return; 705 return;
706 } 706 }
707 707
@@ -725,10 +725,10 @@ static void edge_bulk_in_callback (struct urb *urb)
725 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC); 725 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
726 if (status) { 726 if (status) {
727 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status); 727 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
728 edge_serial->read_in_progress = FALSE; 728 edge_serial->read_in_progress = false;
729 } 729 }
730 } else { 730 } else {
731 edge_serial->read_in_progress = FALSE; 731 edge_serial->read_in_progress = false;
732 } 732 }
733 733
734 spin_unlock(&edge_serial->es_lock); 734 spin_unlock(&edge_serial->es_lock);
@@ -759,7 +759,7 @@ static void edge_bulk_out_data_callback (struct urb *urb)
759 } 759 }
760 760
761 // Release the Write URB 761 // Release the Write URB
762 edge_port->write_in_progress = FALSE; 762 edge_port->write_in_progress = false;
763 763
764 // Check if more data needs to be sent 764 // Check if more data needs to be sent
765 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port); 765 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
@@ -779,8 +779,8 @@ static void edge_bulk_out_cmd_callback (struct urb *urb)
779 779
780 dbg("%s", __FUNCTION__); 780 dbg("%s", __FUNCTION__);
781 781
782 CmdUrbs--; 782 atomic_dec(&CmdUrbs);
783 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs); 783 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, atomic_read(&CmdUrbs));
784 784
785 785
786 /* clean up the transfer buffer */ 786 /* clean up the transfer buffer */
@@ -802,7 +802,7 @@ static void edge_bulk_out_cmd_callback (struct urb *urb)
802 tty_wakeup(tty); 802 tty_wakeup(tty);
803 803
804 /* we have completed the command */ 804 /* we have completed the command */
805 edge_port->commandPending = FALSE; 805 edge_port->commandPending = false;
806 wake_up(&edge_port->wait_command); 806 wake_up(&edge_port->wait_command);
807} 807}
808 808
@@ -868,7 +868,7 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
868 port0->bulk_in_buffer, 868 port0->bulk_in_buffer,
869 edge_serial->read_urb->transfer_buffer_length, 869 edge_serial->read_urb->transfer_buffer_length,
870 edge_bulk_in_callback, edge_serial); 870 edge_bulk_in_callback, edge_serial);
871 edge_serial->read_in_progress = FALSE; 871 edge_serial->read_in_progress = false;
872 872
873 /* start interrupt read for this edgeport 873 /* start interrupt read for this edgeport
874 * this interrupt will continue as long as the edgeport is connected */ 874 * this interrupt will continue as long as the edgeport is connected */
@@ -890,26 +890,26 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
890 /* initialize our port settings */ 890 /* initialize our port settings */
891 edge_port->txCredits = 0; /* Can't send any data yet */ 891 edge_port->txCredits = 0; /* Can't send any data yet */
892 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */ 892 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
893 edge_port->chaseResponsePending = FALSE; 893 edge_port->chaseResponsePending = false;
894 894
895 /* send a open port command */ 895 /* send a open port command */
896 edge_port->openPending = TRUE; 896 edge_port->openPending = true;
897 edge_port->open = FALSE; 897 edge_port->open = false;
898 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0); 898 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
899 899
900 if (response < 0) { 900 if (response < 0) {
901 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__); 901 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
902 edge_port->openPending = FALSE; 902 edge_port->openPending = false;
903 return -ENODEV; 903 return -ENODEV;
904 } 904 }
905 905
906 /* now wait for the port to be completely opened */ 906 /* now wait for the port to be completely opened */
907 wait_event_timeout(edge_port->wait_open, (edge_port->openPending != TRUE), OPEN_TIMEOUT); 907 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, OPEN_TIMEOUT);
908 908
909 if (edge_port->open == FALSE) { 909 if (!edge_port->open) {
910 /* open timed out */ 910 /* open timed out */
911 dbg("%s - open timedout", __FUNCTION__); 911 dbg("%s - open timedout", __FUNCTION__);
912 edge_port->openPending = FALSE; 912 edge_port->openPending = false;
913 return -ENODEV; 913 return -ENODEV;
914 } 914 }
915 915
@@ -928,7 +928,7 @@ static int edge_open (struct usb_serial_port *port, struct file * filp)
928 928
929 /* Allocate a URB for the write */ 929 /* Allocate a URB for the write */
930 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL); 930 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
931 edge_port->write_in_progress = FALSE; 931 edge_port->write_in_progress = false;
932 932
933 if (!edge_port->write_urb) { 933 if (!edge_port->write_urb) {
934 dbg("%s - no memory", __FUNCTION__); 934 dbg("%s - no memory", __FUNCTION__);
@@ -966,7 +966,7 @@ static void block_until_chase_response(struct edgeport_port *edge_port)
966 lastCredits = edge_port->txCredits; 966 lastCredits = edge_port->txCredits;
967 967
968 // Did we get our Chase response 968 // Did we get our Chase response
969 if (edge_port->chaseResponsePending == FALSE) { 969 if (!edge_port->chaseResponsePending) {
970 dbg("%s - Got Chase Response", __FUNCTION__); 970 dbg("%s - Got Chase Response", __FUNCTION__);
971 971
972 // did we get all of our credit back? 972 // did we get all of our credit back?
@@ -985,7 +985,7 @@ static void block_until_chase_response(struct edgeport_port *edge_port)
985 // No activity.. count down. 985 // No activity.. count down.
986 loop--; 986 loop--;
987 if (loop == 0) { 987 if (loop == 0) {
988 edge_port->chaseResponsePending = FALSE; 988 edge_port->chaseResponsePending = false;
989 dbg("%s - Chase TIMEOUT", __FUNCTION__); 989 dbg("%s - Chase TIMEOUT", __FUNCTION__);
990 return; 990 return;
991 } 991 }
@@ -1068,13 +1068,13 @@ static void edge_close (struct usb_serial_port *port, struct file * filp)
1068 // block until tx is empty 1068 // block until tx is empty
1069 block_until_tx_empty(edge_port); 1069 block_until_tx_empty(edge_port);
1070 1070
1071 edge_port->closePending = TRUE; 1071 edge_port->closePending = true;
1072 1072
1073 if ((!edge_serial->is_epic) || 1073 if ((!edge_serial->is_epic) ||
1074 ((edge_serial->is_epic) && 1074 ((edge_serial->is_epic) &&
1075 (edge_serial->epic_descriptor.Supports.IOSPChase))) { 1075 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1076 /* flush and chase */ 1076 /* flush and chase */
1077 edge_port->chaseResponsePending = TRUE; 1077 edge_port->chaseResponsePending = true;
1078 1078
1079 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__); 1079 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1080 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0); 1080 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
@@ -1082,7 +1082,7 @@ static void edge_close (struct usb_serial_port *port, struct file * filp)
1082 // block until chase finished 1082 // block until chase finished
1083 block_until_chase_response(edge_port); 1083 block_until_chase_response(edge_port);
1084 } else { 1084 } else {
1085 edge_port->chaseResponsePending = FALSE; 1085 edge_port->chaseResponsePending = false;
1086 } 1086 }
1087 } 1087 }
1088 1088
@@ -1094,10 +1094,10 @@ static void edge_close (struct usb_serial_port *port, struct file * filp)
1094 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0); 1094 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1095 } 1095 }
1096 1096
1097 //port->close = TRUE; 1097 //port->close = true;
1098 edge_port->closePending = FALSE; 1098 edge_port->closePending = false;
1099 edge_port->open = FALSE; 1099 edge_port->open = false;
1100 edge_port->openPending = FALSE; 1100 edge_port->openPending = false;
1101 1101
1102 usb_kill_urb(edge_port->write_urb); 1102 usb_kill_urb(edge_port->write_urb);
1103 1103
@@ -1247,7 +1247,7 @@ static void send_more_port_data(struct edgeport_serial *edge_serial, struct edge
1247 } 1247 }
1248 1248
1249 // lock this write 1249 // lock this write
1250 edge_port->write_in_progress = TRUE; 1250 edge_port->write_in_progress = true;
1251 1251
1252 // get a pointer to the write_urb 1252 // get a pointer to the write_urb
1253 urb = edge_port->write_urb; 1253 urb = edge_port->write_urb;
@@ -1261,7 +1261,7 @@ static void send_more_port_data(struct edgeport_serial *edge_serial, struct edge
1261 buffer = kmalloc (count+2, GFP_ATOMIC); 1261 buffer = kmalloc (count+2, GFP_ATOMIC);
1262 if (buffer == NULL) { 1262 if (buffer == NULL) {
1263 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__); 1263 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1264 edge_port->write_in_progress = FALSE; 1264 edge_port->write_in_progress = false;
1265 goto exit_send; 1265 goto exit_send;
1266 } 1266 }
1267 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count); 1267 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
@@ -1301,7 +1301,7 @@ static void send_more_port_data(struct edgeport_serial *edge_serial, struct edge
1301 if (status) { 1301 if (status) {
1302 /* something went wrong */ 1302 /* something went wrong */
1303 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status); 1303 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
1304 edge_port->write_in_progress = FALSE; 1304 edge_port->write_in_progress = false;
1305 1305
1306 /* revert the credits as something bad happened. */ 1306 /* revert the credits as something bad happened. */
1307 edge_port->txCredits += count; 1307 edge_port->txCredits += count;
@@ -1332,7 +1332,7 @@ static int edge_write_room (struct usb_serial_port *port)
1332 1332
1333 if (edge_port == NULL) 1333 if (edge_port == NULL)
1334 return -ENODEV; 1334 return -ENODEV;
1335 if (edge_port->closePending == TRUE) 1335 if (edge_port->closePending)
1336 return -ENODEV; 1336 return -ENODEV;
1337 1337
1338 dbg("%s - port %d", __FUNCTION__, port->number); 1338 dbg("%s - port %d", __FUNCTION__, port->number);
@@ -1371,7 +1371,7 @@ static int edge_chars_in_buffer (struct usb_serial_port *port)
1371 1371
1372 if (edge_port == NULL) 1372 if (edge_port == NULL)
1373 return -ENODEV; 1373 return -ENODEV;
1374 if (edge_port->closePending == TRUE) 1374 if (edge_port->closePending)
1375 return -ENODEV; 1375 return -ENODEV;
1376 1376
1377 if (!edge_port->open) { 1377 if (!edge_port->open) {
@@ -1762,7 +1762,7 @@ static void edge_break (struct usb_serial_port *port, int break_state)
1762 ((edge_serial->is_epic) && 1762 ((edge_serial->is_epic) &&
1763 (edge_serial->epic_descriptor.Supports.IOSPChase))) { 1763 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1764 /* flush and chase */ 1764 /* flush and chase */
1765 edge_port->chaseResponsePending = TRUE; 1765 edge_port->chaseResponsePending = true;
1766 1766
1767 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__); 1767 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1768 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0); 1768 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
@@ -1770,7 +1770,7 @@ static void edge_break (struct usb_serial_port *port, int break_state)
1770 // block until chase finished 1770 // block until chase finished
1771 block_until_chase_response(edge_port); 1771 block_until_chase_response(edge_port);
1772 } else { 1772 } else {
1773 edge_port->chaseResponsePending = FALSE; 1773 edge_port->chaseResponsePending = false;
1774 } 1774 }
1775 } 1775 }
1776 1776
@@ -1952,13 +1952,13 @@ static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2
1952 // Also, we currently clear flag and close the port regardless of content of above's Byte3. 1952 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1953 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport, 1953 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1954 // like wait longer in block_until_chase_response, but for now we don't. 1954 // like wait longer in block_until_chase_response, but for now we don't.
1955 edge_port->chaseResponsePending = FALSE; 1955 edge_port->chaseResponsePending = false;
1956 wake_up (&edge_port->wait_chase); 1956 wake_up (&edge_port->wait_chase);
1957 return; 1957 return;
1958 1958
1959 case IOSP_EXT_STATUS_RX_CHECK_RSP: 1959 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1960 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 ); 1960 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
1961 //Port->RxCheckRsp = TRUE; 1961 //Port->RxCheckRsp = true;
1962 return; 1962 return;
1963 } 1963 }
1964 } 1964 }
@@ -1974,8 +1974,8 @@ static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2
1974 change_port_settings (edge_port, edge_port->port->tty->termios); 1974 change_port_settings (edge_port, edge_port->port->tty->termios);
1975 1975
1976 /* we have completed the open */ 1976 /* we have completed the open */
1977 edge_port->openPending = FALSE; 1977 edge_port->openPending = false;
1978 edge_port->open = TRUE; 1978 edge_port->open = true;
1979 wake_up(&edge_port->wait_open); 1979 wake_up(&edge_port->wait_open);
1980 return; 1980 return;
1981 } 1981 }
@@ -1983,7 +1983,7 @@ static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2
1983 // If port is closed, silently discard all rcvd status. We can 1983 // If port is closed, silently discard all rcvd status. We can
1984 // have cases where buffered status is received AFTER the close 1984 // have cases where buffered status is received AFTER the close
1985 // port command is sent to the Edgeport. 1985 // port command is sent to the Edgeport.
1986 if ((!edge_port->open ) || (edge_port->closePending)) { 1986 if (!edge_port->open || edge_port->closePending) {
1987 return; 1987 return;
1988 } 1988 }
1989 1989
@@ -1991,14 +1991,14 @@ static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2
1991 // Not currently sent by Edgeport 1991 // Not currently sent by Edgeport
1992 case IOSP_STATUS_LSR: 1992 case IOSP_STATUS_LSR:
1993 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2); 1993 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1994 handle_new_lsr (edge_port, FALSE, byte2, 0); 1994 handle_new_lsr(edge_port, false, byte2, 0);
1995 break; 1995 break;
1996 1996
1997 case IOSP_STATUS_LSR_DATA: 1997 case IOSP_STATUS_LSR_DATA:
1998 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3); 1998 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1999 // byte2 is LSR Register 1999 // byte2 is LSR Register
2000 // byte3 is broken data byte 2000 // byte3 is broken data byte
2001 handle_new_lsr (edge_port, TRUE, byte2, byte3); 2001 handle_new_lsr(edge_port, true, byte2, byte3);
2002 break; 2002 break;
2003 // 2003 //
2004 // case IOSP_EXT_4_STATUS: 2004 // case IOSP_EXT_4_STATUS:
@@ -2317,14 +2317,14 @@ static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer
2317 if (!urb) 2317 if (!urb)
2318 return -ENOMEM; 2318 return -ENOMEM;
2319 2319
2320 CmdUrbs++; 2320 atomic_inc(&CmdUrbs);
2321 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs); 2321 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, atomic_read(&CmdUrbs));
2322 2322
2323 usb_fill_bulk_urb (urb, edge_serial->serial->dev, 2323 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2324 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint), 2324 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2325 buffer, length, edge_bulk_out_cmd_callback, edge_port); 2325 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2326 2326
2327 edge_port->commandPending = TRUE; 2327 edge_port->commandPending = true;
2328 status = usb_submit_urb(urb, GFP_ATOMIC); 2328 status = usb_submit_urb(urb, GFP_ATOMIC);
2329 2329
2330 if (status) { 2330 if (status) {
@@ -2332,16 +2332,16 @@ static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer
2332 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status); 2332 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
2333 usb_kill_urb(urb); 2333 usb_kill_urb(urb);
2334 usb_free_urb(urb); 2334 usb_free_urb(urb);
2335 CmdUrbs--; 2335 atomic_dec(&CmdUrbs);
2336 return status; 2336 return status;
2337 } 2337 }
2338 2338
2339 // wait for command to finish 2339 // wait for command to finish
2340 timeout = COMMAND_TIMEOUT; 2340 timeout = COMMAND_TIMEOUT;
2341#if 0 2341#if 0
2342 wait_event (&edge_port->wait_command, (edge_port->commandPending == FALSE)); 2342 wait_event (&edge_port->wait_command, !edge_port->commandPending);
2343 2343
2344 if (edge_port->commandPending == TRUE) { 2344 if (edge_port->commandPending) {
2345 /* command timed out */ 2345 /* command timed out */
2346 dbg("%s - command timed out", __FUNCTION__); 2346 dbg("%s - command timed out", __FUNCTION__);
2347 status = -EINVAL; 2347 status = -EINVAL;
@@ -2524,8 +2524,8 @@ static void change_port_settings (struct edgeport_port *edge_port, struct ktermi
2524 2524
2525 dbg("%s - port %d", __FUNCTION__, edge_port->port->number); 2525 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2526 2526
2527 if ((!edge_port->open) && 2527 if (!edge_port->open &&
2528 (!edge_port->openPending)) { 2528 !edge_port->openPending) {
2529 dbg("%s - port not opened", __FUNCTION__); 2529 dbg("%s - port not opened", __FUNCTION__);
2530 return; 2530 return;
2531 } 2531 }
@@ -2836,9 +2836,9 @@ static int edge_startup (struct usb_serial *serial)
2836 struct usb_device *dev; 2836 struct usb_device *dev;
2837 int i, j; 2837 int i, j;
2838 int response; 2838 int response;
2839 int interrupt_in_found; 2839 bool interrupt_in_found;
2840 int bulk_in_found; 2840 bool bulk_in_found;
2841 int bulk_out_found; 2841 bool bulk_out_found;
2842 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0, 2842 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2843 EDGE_COMPATIBILITY_MASK1, 2843 EDGE_COMPATIBILITY_MASK1,
2844 EDGE_COMPATIBILITY_MASK2 }; 2844 EDGE_COMPATIBILITY_MASK2 };
@@ -2936,14 +2936,14 @@ static int edge_startup (struct usb_serial *serial)
2936 if (edge_serial->is_epic) { 2936 if (edge_serial->is_epic) {
2937 /* EPIC thing, set up our interrupt polling now and our read urb, so 2937 /* EPIC thing, set up our interrupt polling now and our read urb, so
2938 * that the device knows it really is connected. */ 2938 * that the device knows it really is connected. */
2939 interrupt_in_found = bulk_in_found = bulk_out_found = FALSE; 2939 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2940 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) { 2940 for (i = 0; i < serial->interface->altsetting[0].desc.bNumEndpoints; ++i) {
2941 struct usb_endpoint_descriptor *endpoint; 2941 struct usb_endpoint_descriptor *endpoint;
2942 int buffer_size; 2942 int buffer_size;
2943 2943
2944 endpoint = &serial->interface->altsetting[0].endpoint[i].desc; 2944 endpoint = &serial->interface->altsetting[0].endpoint[i].desc;
2945 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 2945 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2946 if ((!interrupt_in_found) && 2946 if (!interrupt_in_found &&
2947 (usb_endpoint_is_int_in(endpoint))) { 2947 (usb_endpoint_is_int_in(endpoint))) {
2948 /* we found a interrupt in endpoint */ 2948 /* we found a interrupt in endpoint */
2949 dbg("found interrupt in"); 2949 dbg("found interrupt in");
@@ -2972,10 +2972,10 @@ static int edge_startup (struct usb_serial *serial)
2972 edge_serial, 2972 edge_serial,
2973 endpoint->bInterval); 2973 endpoint->bInterval);
2974 2974
2975 interrupt_in_found = TRUE; 2975 interrupt_in_found = true;
2976 } 2976 }
2977 2977
2978 if ((!bulk_in_found) && 2978 if (!bulk_in_found &&
2979 (usb_endpoint_is_bulk_in(endpoint))) { 2979 (usb_endpoint_is_bulk_in(endpoint))) {
2980 /* we found a bulk in endpoint */ 2980 /* we found a bulk in endpoint */
2981 dbg("found bulk in"); 2981 dbg("found bulk in");
@@ -3001,19 +3001,19 @@ static int edge_startup (struct usb_serial *serial)
3001 endpoint->wMaxPacketSize, 3001 endpoint->wMaxPacketSize,
3002 edge_bulk_in_callback, 3002 edge_bulk_in_callback,
3003 edge_serial); 3003 edge_serial);
3004 bulk_in_found = TRUE; 3004 bulk_in_found = true;
3005 } 3005 }
3006 3006
3007 if ((!bulk_out_found) && 3007 if (!bulk_out_found &&
3008 (usb_endpoint_is_bulk_out(endpoint))) { 3008 (usb_endpoint_is_bulk_out(endpoint))) {
3009 /* we found a bulk out endpoint */ 3009 /* we found a bulk out endpoint */
3010 dbg("found bulk out"); 3010 dbg("found bulk out");
3011 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress; 3011 edge_serial->bulk_out_endpoint = endpoint->bEndpointAddress;
3012 bulk_out_found = TRUE; 3012 bulk_out_found = true;
3013 } 3013 }
3014 } 3014 }
3015 3015
3016 if ((!interrupt_in_found) || (!bulk_in_found) || (!bulk_out_found)) { 3016 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3017 err ("Error - the proper endpoints were not found!"); 3017 err ("Error - the proper endpoints were not found!");
3018 return -ENODEV; 3018 return -ENODEV;
3019 } 3019 }
@@ -3083,6 +3083,7 @@ static int __init edgeport_init(void)
3083 retval = usb_register(&io_driver); 3083 retval = usb_register(&io_driver);
3084 if (retval) 3084 if (retval)
3085 goto failed_usb_register; 3085 goto failed_usb_register;
3086 atomic_set(&CmdUrbs, 0);
3086 info(DRIVER_DESC " " DRIVER_VERSION); 3087 info(DRIVER_DESC " " DRIVER_VERSION);
3087 return 0; 3088 return 0;
3088 3089
diff --git a/drivers/usb/serial/io_edgeport.h b/drivers/usb/serial/io_edgeport.h
index 29a913a6daca..cb201c1f67f9 100644
--- a/drivers/usb/serial/io_edgeport.h
+++ b/drivers/usb/serial/io_edgeport.h
@@ -19,12 +19,6 @@
19#define MAX_RS232_PORTS 8 /* Max # of RS-232 ports per device */ 19#define MAX_RS232_PORTS 8 /* Max # of RS-232 ports per device */
20 20
21/* typedefs that the insideout headers need */ 21/* typedefs that the insideout headers need */
22#ifndef TRUE
23 #define TRUE (1)
24#endif
25#ifndef FALSE
26 #define FALSE (0)
27#endif
28#ifndef LOW8 22#ifndef LOW8
29 #define LOW8(a) ((unsigned char)(a & 0xff)) 23 #define LOW8(a) ((unsigned char)(a & 0xff))
30#endif 24#endif
diff --git a/drivers/usb/serial/ipaq.c b/drivers/usb/serial/ipaq.c
index d16e2e1764ad..4df0ec74e0b1 100644
--- a/drivers/usb/serial/ipaq.c
+++ b/drivers/usb/serial/ipaq.c
@@ -255,6 +255,7 @@ static struct usb_device_id ipaq_id_table [] = {
255 { USB_DEVICE(0x04DD, 0x9102) }, /* SHARP WS003SH USB Modem */ 255 { USB_DEVICE(0x04DD, 0x9102) }, /* SHARP WS003SH USB Modem */
256 { USB_DEVICE(0x04DD, 0x9121) }, /* SHARP WS004SH USB Modem */ 256 { USB_DEVICE(0x04DD, 0x9121) }, /* SHARP WS004SH USB Modem */
257 { USB_DEVICE(0x04DD, 0x9123) }, /* SHARP WS007SH USB Modem */ 257 { USB_DEVICE(0x04DD, 0x9123) }, /* SHARP WS007SH USB Modem */
258 { USB_DEVICE(0x04DD, 0x9151) }, /* SHARP S01SH USB Modem */
258 { USB_DEVICE(0x04E8, 0x5F00) }, /* Samsung NEXiO USB Sync */ 259 { USB_DEVICE(0x04E8, 0x5F00) }, /* Samsung NEXiO USB Sync */
259 { USB_DEVICE(0x04E8, 0x5F01) }, /* Samsung NEXiO USB Sync */ 260 { USB_DEVICE(0x04E8, 0x5F01) }, /* Samsung NEXiO USB Sync */
260 { USB_DEVICE(0x04E8, 0x5F02) }, /* Samsung NEXiO USB Sync */ 261 { USB_DEVICE(0x04E8, 0x5F02) }, /* Samsung NEXiO USB Sync */
diff --git a/drivers/usb/serial/kl5kusb105.c b/drivers/usb/serial/kl5kusb105.c
index b2097c45a235..7b085f334ceb 100644
--- a/drivers/usb/serial/kl5kusb105.c
+++ b/drivers/usb/serial/kl5kusb105.c
@@ -238,7 +238,7 @@ static int klsi_105_get_line_state(struct usb_serial_port *port,
238 if (rc < 0) 238 if (rc < 0)
239 err("Reading line status failed (error = %d)", rc); 239 err("Reading line status failed (error = %d)", rc);
240 else { 240 else {
241 status = status_buf[0] + (status_buf[1]<<8); 241 status = le16_to_cpu(*(u16 *)status_buf);
242 242
243 info("%s - read status %x %x", __FUNCTION__, 243 info("%s - read status %x %x", __FUNCTION__,
244 status_buf[0], status_buf[1]); 244 status_buf[0], status_buf[1]);
@@ -257,7 +257,7 @@ static int klsi_105_get_line_state(struct usb_serial_port *port,
257static int klsi_105_startup (struct usb_serial *serial) 257static int klsi_105_startup (struct usb_serial *serial)
258{ 258{
259 struct klsi_105_private *priv; 259 struct klsi_105_private *priv;
260 int i; 260 int i, j;
261 261
262 /* check if we support the product id (see keyspan.c) 262 /* check if we support the product id (see keyspan.c)
263 * FIXME 263 * FIXME
@@ -265,12 +265,12 @@ static int klsi_105_startup (struct usb_serial *serial)
265 265
266 /* allocate the private data structure */ 266 /* allocate the private data structure */
267 for (i=0; i<serial->num_ports; i++) { 267 for (i=0; i<serial->num_ports; i++) {
268 int j;
269 priv = kmalloc(sizeof(struct klsi_105_private), 268 priv = kmalloc(sizeof(struct klsi_105_private),
270 GFP_KERNEL); 269 GFP_KERNEL);
271 if (!priv) { 270 if (!priv) {
272 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__); 271 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__);
273 return -ENOMEM; 272 i--;
273 goto err_cleanup;
274 } 274 }
275 /* set initial values for control structures */ 275 /* set initial values for control structures */
276 priv->cfg.pktlen = 5; 276 priv->cfg.pktlen = 5;
@@ -292,15 +292,14 @@ static int klsi_105_startup (struct usb_serial *serial)
292 priv->write_urb_pool[j] = urb; 292 priv->write_urb_pool[j] = urb;
293 if (urb == NULL) { 293 if (urb == NULL) {
294 err("No more urbs???"); 294 err("No more urbs???");
295 continue; 295 goto err_cleanup;
296 } 296 }
297 297
298 urb->transfer_buffer = NULL;
299 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, 298 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
300 GFP_KERNEL); 299 GFP_KERNEL);
301 if (!urb->transfer_buffer) { 300 if (!urb->transfer_buffer) {
302 err("%s - out of memory for urb buffers.", __FUNCTION__); 301 err("%s - out of memory for urb buffers.", __FUNCTION__);
303 continue; 302 goto err_cleanup;
304 } 303 }
305 } 304 }
306 305
@@ -308,7 +307,20 @@ static int klsi_105_startup (struct usb_serial *serial)
308 init_waitqueue_head(&serial->port[i]->write_wait); 307 init_waitqueue_head(&serial->port[i]->write_wait);
309 } 308 }
310 309
311 return (0); 310 return 0;
311
312err_cleanup:
313 for (; i >= 0; i--) {
314 priv = usb_get_serial_port_data(serial->port[i]);
315 for (j=0; j < NUM_URBS; j++) {
316 if (priv->write_urb_pool[j]) {
317 kfree(priv->write_urb_pool[j]->transfer_buffer);
318 usb_free_urb(priv->write_urb_pool[j]);
319 }
320 }
321 usb_set_serial_port_data(serial->port[i], NULL);
322 }
323 return -ENOMEM;
312} /* klsi_105_startup */ 324} /* klsi_105_startup */
313 325
314 326
diff --git a/drivers/usb/serial/mct_u232.c b/drivers/usb/serial/mct_u232.c
index 4cd839b1407f..3db1adc25f84 100644
--- a/drivers/usb/serial/mct_u232.c
+++ b/drivers/usb/serial/mct_u232.c
@@ -438,17 +438,21 @@ static int mct_u232_open (struct usb_serial_port *port, struct file *filp)
438 if (retval) { 438 if (retval) {
439 err("usb_submit_urb(read bulk) failed pipe 0x%x err %d", 439 err("usb_submit_urb(read bulk) failed pipe 0x%x err %d",
440 port->read_urb->pipe, retval); 440 port->read_urb->pipe, retval);
441 goto exit; 441 goto error;
442 } 442 }
443 443
444 port->interrupt_in_urb->dev = port->serial->dev; 444 port->interrupt_in_urb->dev = port->serial->dev;
445 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 445 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
446 if (retval) 446 if (retval) {
447 usb_kill_urb(port->read_urb);
447 err(" usb_submit_urb(read int) failed pipe 0x%x err %d", 448 err(" usb_submit_urb(read int) failed pipe 0x%x err %d",
448 port->interrupt_in_urb->pipe, retval); 449 port->interrupt_in_urb->pipe, retval);
449 450 goto error;
450exit: 451 }
451 return 0; 452 return 0;
453
454error:
455 return retval;
452} /* mct_u232_open */ 456} /* mct_u232_open */
453 457
454 458
diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
index 19bf403f9db2..b563e2ad8728 100644
--- a/drivers/usb/serial/mos7720.c
+++ b/drivers/usb/serial/mos7720.c
@@ -103,11 +103,9 @@ static void mos7720_interrupt_callback(struct urb *urb)
103{ 103{
104 int result; 104 int result;
105 int length; 105 int length;
106 __u32 *data; 106 __u8 *data;
107 unsigned int status;
108 __u8 sp1; 107 __u8 sp1;
109 __u8 sp2; 108 __u8 sp2;
110 __u8 st;
111 109
112 dbg("%s"," : Entering\n"); 110 dbg("%s"," : Entering\n");
113 111
@@ -141,18 +139,19 @@ static void mos7720_interrupt_callback(struct urb *urb)
141 * Byte 2 IIR Port 2 (port.number is 1) 139 * Byte 2 IIR Port 2 (port.number is 1)
142 * Byte 3 -------------- 140 * Byte 3 --------------
143 * Byte 4 FIFO status for both */ 141 * Byte 4 FIFO status for both */
144 if (length && length > 4) { 142
143 /* the above description is inverted
144 * oneukum 2007-03-14 */
145
146 if (unlikely(length != 4)) {
145 dbg("Wrong data !!!"); 147 dbg("Wrong data !!!");
146 return; 148 return;
147 } 149 }
148 150
149 status = *data; 151 sp1 = data[3];
150 152 sp2 = data[2];
151 sp1 = (status & 0xff000000)>>24;
152 sp2 = (status & 0x00ff0000)>>16;
153 st = status & 0x000000ff;
154 153
155 if ((sp1 & 0x01) || (sp2 & 0x01)) { 154 if ((sp1 | sp2) & 0x01) {
156 /* No Interrupt Pending in both the ports */ 155 /* No Interrupt Pending in both the ports */
157 dbg("No Interrupt !!!"); 156 dbg("No Interrupt !!!");
158 } else { 157 } else {
@@ -333,6 +332,7 @@ static int mos7720_open(struct usb_serial_port *port, struct file * filp)
333 int response; 332 int response;
334 int port_number; 333 int port_number;
335 char data; 334 char data;
335 int allocated_urbs = 0;
336 int j; 336 int j;
337 337
338 serial = port->serial; 338 serial = port->serial;
@@ -353,7 +353,7 @@ static int mos7720_open(struct usb_serial_port *port, struct file * filp)
353 353
354 /* Initialising the write urb pool */ 354 /* Initialising the write urb pool */
355 for (j = 0; j < NUM_URBS; ++j) { 355 for (j = 0; j < NUM_URBS; ++j) {
356 urb = usb_alloc_urb(0,GFP_ATOMIC); 356 urb = usb_alloc_urb(0,GFP_KERNEL);
357 mos7720_port->write_urb_pool[j] = urb; 357 mos7720_port->write_urb_pool[j] = urb;
358 358
359 if (urb == NULL) { 359 if (urb == NULL) {
@@ -365,10 +365,16 @@ static int mos7720_open(struct usb_serial_port *port, struct file * filp)
365 GFP_KERNEL); 365 GFP_KERNEL);
366 if (!urb->transfer_buffer) { 366 if (!urb->transfer_buffer) {
367 err("%s-out of memory for urb buffers.", __FUNCTION__); 367 err("%s-out of memory for urb buffers.", __FUNCTION__);
368 usb_free_urb(mos7720_port->write_urb_pool[j]);
369 mos7720_port->write_urb_pool[j] = NULL;
368 continue; 370 continue;
369 } 371 }
372 allocated_urbs++;
370 } 373 }
371 374
375 if (!allocated_urbs)
376 return -ENOMEM;
377
372 /* Initialize MCS7720 -- Write Init values to corresponding Registers 378 /* Initialize MCS7720 -- Write Init values to corresponding Registers
373 * 379 *
374 * Register Index 380 * Register Index
@@ -526,7 +532,7 @@ static int mos7720_chars_in_buffer(struct usb_serial_port *port)
526 } 532 }
527 533
528 for (i = 0; i < NUM_URBS; ++i) { 534 for (i = 0; i < NUM_URBS; ++i) {
529 if (mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) 535 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
530 chars += URB_TRANSFER_BUFFER_SIZE; 536 chars += URB_TRANSFER_BUFFER_SIZE;
531 } 537 }
532 dbg("%s - returns %d", __FUNCTION__, chars); 538 dbg("%s - returns %d", __FUNCTION__, chars);
@@ -629,7 +635,7 @@ static int mos7720_write_room(struct usb_serial_port *port)
629 } 635 }
630 636
631 for (i = 0; i < NUM_URBS; ++i) { 637 for (i = 0; i < NUM_URBS; ++i) {
632 if (mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) 638 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
633 room += URB_TRANSFER_BUFFER_SIZE; 639 room += URB_TRANSFER_BUFFER_SIZE;
634 } 640 }
635 641
@@ -664,7 +670,7 @@ static int mos7720_write(struct usb_serial_port *port,
664 urb = NULL; 670 urb = NULL;
665 671
666 for (i = 0; i < NUM_URBS; ++i) { 672 for (i = 0; i < NUM_URBS; ++i) {
667 if (mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) { 673 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
668 urb = mos7720_port->write_urb_pool[i]; 674 urb = mos7720_port->write_urb_pool[i];
669 dbg("URB:%d",i); 675 dbg("URB:%d",i);
670 break; 676 break;
diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index c6cca859af45..2366e7b63ece 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -176,9 +176,12 @@ struct moschip_port {
176 int port_num; /*Actual port number in the device(1,2,etc) */ 176 int port_num; /*Actual port number in the device(1,2,etc) */
177 struct urb *write_urb; /* write URB for this port */ 177 struct urb *write_urb; /* write URB for this port */
178 struct urb *read_urb; /* read URB for this port */ 178 struct urb *read_urb; /* read URB for this port */
179 struct urb *int_urb;
179 __u8 shadowLCR; /* last LCR value received */ 180 __u8 shadowLCR; /* last LCR value received */
180 __u8 shadowMCR; /* last MCR value received */ 181 __u8 shadowMCR; /* last MCR value received */
181 char open; 182 char open;
183 char open_ports;
184 char zombie;
182 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 185 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
183 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ 186 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
184 int delta_msr_cond; 187 int delta_msr_cond;
@@ -191,17 +194,17 @@ struct moschip_port {
191 __u8 DcrRegOffset; 194 __u8 DcrRegOffset;
192 //for processing control URBS in interrupt context 195 //for processing control URBS in interrupt context
193 struct urb *control_urb; 196 struct urb *control_urb;
197 struct usb_ctrlrequest *dr;
194 char *ctrl_buf; 198 char *ctrl_buf;
195 int MsrLsr; 199 int MsrLsr;
196 200
201 spinlock_t pool_lock;
197 struct urb *write_urb_pool[NUM_URBS]; 202 struct urb *write_urb_pool[NUM_URBS];
203 char busy[NUM_URBS];
198}; 204};
199 205
200 206
201static int debug; 207static int debug;
202static int mos7840_num_ports; //this says the number of ports in the device
203static int mos7840_num_open_ports;
204
205 208
206/* 209/*
207 * mos7840_set_reg_sync 210 * mos7840_set_reg_sync
@@ -254,7 +257,7 @@ static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
254 struct usb_device *dev = port->serial->dev; 257 struct usb_device *dev = port->serial->dev;
255 val = val & 0x00ff; 258 val = val & 0x00ff;
256 // For the UART control registers, the application number need to be Or'ed 259 // For the UART control registers, the application number need to be Or'ed
257 if (mos7840_num_ports == 4) { 260 if (port->serial->num_ports == 4) {
258 val |= 261 val |=
259 (((__u16) port->number - (__u16) (port->serial->minor)) + 262 (((__u16) port->number - (__u16) (port->serial->minor)) +
260 1) << 8; 263 1) << 8;
@@ -294,7 +297,7 @@ static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
294 297
295 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); 298 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
296 /*Wval is same as application number */ 299 /*Wval is same as application number */
297 if (mos7840_num_ports == 4) { 300 if (port->serial->num_ports == 4) {
298 Wval = 301 Wval =
299 (((__u16) port->number - (__u16) (port->serial->minor)) + 302 (((__u16) port->number - (__u16) (port->serial->minor)) +
300 1) << 8; 303 1) << 8;
@@ -352,7 +355,7 @@ static inline struct moschip_port *mos7840_get_port_private(struct
352 return (struct moschip_port *)usb_get_serial_port_data(port); 355 return (struct moschip_port *)usb_get_serial_port_data(port);
353} 356}
354 357
355static int mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) 358static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
356{ 359{
357 struct moschip_port *mos7840_port; 360 struct moschip_port *mos7840_port;
358 struct async_icount *icount; 361 struct async_icount *icount;
@@ -366,22 +369,24 @@ static int mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
366 /* update input line counters */ 369 /* update input line counters */
367 if (new_msr & MOS_MSR_DELTA_CTS) { 370 if (new_msr & MOS_MSR_DELTA_CTS) {
368 icount->cts++; 371 icount->cts++;
372 smp_wmb();
369 } 373 }
370 if (new_msr & MOS_MSR_DELTA_DSR) { 374 if (new_msr & MOS_MSR_DELTA_DSR) {
371 icount->dsr++; 375 icount->dsr++;
376 smp_wmb();
372 } 377 }
373 if (new_msr & MOS_MSR_DELTA_CD) { 378 if (new_msr & MOS_MSR_DELTA_CD) {
374 icount->dcd++; 379 icount->dcd++;
380 smp_wmb();
375 } 381 }
376 if (new_msr & MOS_MSR_DELTA_RI) { 382 if (new_msr & MOS_MSR_DELTA_RI) {
377 icount->rng++; 383 icount->rng++;
384 smp_wmb();
378 } 385 }
379 } 386 }
380
381 return 0;
382} 387}
383 388
384static int mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) 389static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
385{ 390{
386 struct async_icount *icount; 391 struct async_icount *icount;
387 392
@@ -400,18 +405,20 @@ static int mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
400 icount = &port->icount; 405 icount = &port->icount;
401 if (new_lsr & SERIAL_LSR_BI) { 406 if (new_lsr & SERIAL_LSR_BI) {
402 icount->brk++; 407 icount->brk++;
408 smp_wmb();
403 } 409 }
404 if (new_lsr & SERIAL_LSR_OE) { 410 if (new_lsr & SERIAL_LSR_OE) {
405 icount->overrun++; 411 icount->overrun++;
412 smp_wmb();
406 } 413 }
407 if (new_lsr & SERIAL_LSR_PE) { 414 if (new_lsr & SERIAL_LSR_PE) {
408 icount->parity++; 415 icount->parity++;
416 smp_wmb();
409 } 417 }
410 if (new_lsr & SERIAL_LSR_FE) { 418 if (new_lsr & SERIAL_LSR_FE) {
411 icount->frame++; 419 icount->frame++;
420 smp_wmb();
412 } 421 }
413
414 return 0;
415} 422}
416 423
417/************************************************************************/ 424/************************************************************************/
@@ -426,12 +433,15 @@ static void mos7840_control_callback(struct urb *urb)
426 unsigned char *data; 433 unsigned char *data;
427 struct moschip_port *mos7840_port; 434 struct moschip_port *mos7840_port;
428 __u8 regval = 0x0; 435 __u8 regval = 0x0;
436 int result = 0;
429 437
430 if (!urb) { 438 if (!urb) {
431 dbg("%s", "Invalid Pointer !!!!:\n"); 439 dbg("%s", "Invalid Pointer !!!!:\n");
432 return; 440 return;
433 } 441 }
434 442
443 mos7840_port = (struct moschip_port *)urb->context;
444
435 switch (urb->status) { 445 switch (urb->status) {
436 case 0: 446 case 0:
437 /* success */ 447 /* success */
@@ -449,8 +459,6 @@ static void mos7840_control_callback(struct urb *urb)
449 goto exit; 459 goto exit;
450 } 460 }
451 461
452 mos7840_port = (struct moschip_port *)urb->context;
453
454 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length); 462 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
455 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__, 463 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
456 mos7840_port->MsrLsr, mos7840_port->port_num); 464 mos7840_port->MsrLsr, mos7840_port->port_num);
@@ -462,21 +470,26 @@ static void mos7840_control_callback(struct urb *urb)
462 else if (mos7840_port->MsrLsr == 1) 470 else if (mos7840_port->MsrLsr == 1)
463 mos7840_handle_new_lsr(mos7840_port, regval); 471 mos7840_handle_new_lsr(mos7840_port, regval);
464 472
465 exit: 473exit:
466 return; 474 spin_lock(&mos7840_port->pool_lock);
475 if (!mos7840_port->zombie)
476 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
477 spin_unlock(&mos7840_port->pool_lock);
478 if (result) {
479 dev_err(&urb->dev->dev,
480 "%s - Error %d submitting interrupt urb\n",
481 __FUNCTION__, result);
482 }
467} 483}
468 484
469static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, 485static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
470 __u16 * val) 486 __u16 * val)
471{ 487{
472 struct usb_device *dev = mcs->port->serial->dev; 488 struct usb_device *dev = mcs->port->serial->dev;
473 struct usb_ctrlrequest *dr = NULL; 489 struct usb_ctrlrequest *dr = mcs->dr;
474 unsigned char *buffer = NULL; 490 unsigned char *buffer = mcs->ctrl_buf;
475 int ret = 0; 491 int ret;
476 buffer = (__u8 *) mcs->ctrl_buf;
477 492
478// dr=(struct usb_ctrlrequest *)(buffer);
479 dr = (void *)(buffer + 2);
480 dr->bRequestType = MCS_RD_RTYPE; 493 dr->bRequestType = MCS_RD_RTYPE;
481 dr->bRequest = MCS_RDREQ; 494 dr->bRequest = MCS_RDREQ;
482 dr->wValue = cpu_to_le16(Wval); //0; 495 dr->wValue = cpu_to_le16(Wval); //0;
@@ -506,8 +519,8 @@ static void mos7840_interrupt_callback(struct urb *urb)
506 __u16 Data; 519 __u16 Data;
507 unsigned char *data; 520 unsigned char *data;
508 __u8 sp[5], st; 521 __u8 sp[5], st;
509 int i; 522 int i, rv = 0;
510 __u16 wval; 523 __u16 wval, wreg = 0;
511 524
512 dbg("%s", " : Entering\n"); 525 dbg("%s", " : Entering\n");
513 if (!urb) { 526 if (!urb) {
@@ -569,31 +582,34 @@ static void mos7840_interrupt_callback(struct urb *urb)
569 dbg("Serial Port %d: Receiver status error or ", i); 582 dbg("Serial Port %d: Receiver status error or ", i);
570 dbg("address bit detected in 9-bit mode\n"); 583 dbg("address bit detected in 9-bit mode\n");
571 mos7840_port->MsrLsr = 1; 584 mos7840_port->MsrLsr = 1;
572 mos7840_get_reg(mos7840_port, wval, 585 wreg = LINE_STATUS_REGISTER;
573 LINE_STATUS_REGISTER,
574 &Data);
575 break; 586 break;
576 case SERIAL_IIR_MS: 587 case SERIAL_IIR_MS:
577 dbg("Serial Port %d: Modem status change\n", i); 588 dbg("Serial Port %d: Modem status change\n", i);
578 mos7840_port->MsrLsr = 0; 589 mos7840_port->MsrLsr = 0;
579 mos7840_get_reg(mos7840_port, wval, 590 wreg = MODEM_STATUS_REGISTER;
580 MODEM_STATUS_REGISTER,
581 &Data);
582 break; 591 break;
583 } 592 }
593 spin_lock(&mos7840_port->pool_lock);
594 if (!mos7840_port->zombie) {
595 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
596 } else {
597 spin_unlock(&mos7840_port->pool_lock);
598 return;
599 }
600 spin_unlock(&mos7840_port->pool_lock);
584 } 601 }
585 } 602 }
586 } 603 }
587 exit: 604 if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
605 return;
606exit:
588 result = usb_submit_urb(urb, GFP_ATOMIC); 607 result = usb_submit_urb(urb, GFP_ATOMIC);
589 if (result) { 608 if (result) {
590 dev_err(&urb->dev->dev, 609 dev_err(&urb->dev->dev,
591 "%s - Error %d submitting interrupt urb\n", 610 "%s - Error %d submitting interrupt urb\n",
592 __FUNCTION__, result); 611 __FUNCTION__, result);
593 } 612 }
594
595 return;
596
597} 613}
598 614
599static int mos7840_port_paranoia_check(struct usb_serial_port *port, 615static int mos7840_port_paranoia_check(struct usb_serial_port *port,
@@ -634,7 +650,8 @@ static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
634 if (!port || 650 if (!port ||
635 mos7840_port_paranoia_check(port, function) || 651 mos7840_port_paranoia_check(port, function) ||
636 mos7840_serial_paranoia_check(port->serial, function)) { 652 mos7840_serial_paranoia_check(port->serial, function)) {
637 /* then say that we don't have a valid usb_serial thing, which will * end up genrating -ENODEV return values */ 653 /* then say that we don't have a valid usb_serial thing, which will
654 * end up genrating -ENODEV return values */
638 return NULL; 655 return NULL;
639 } 656 }
640 657
@@ -699,6 +716,7 @@ static void mos7840_bulk_in_callback(struct urb *urb)
699 tty_flip_buffer_push(tty); 716 tty_flip_buffer_push(tty);
700 } 717 }
701 mos7840_port->icount.rx += urb->actual_length; 718 mos7840_port->icount.rx += urb->actual_length;
719 smp_wmb();
702 dbg("mos7840_port->icount.rx is %d:\n", 720 dbg("mos7840_port->icount.rx is %d:\n",
703 mos7840_port->icount.rx); 721 mos7840_port->icount.rx);
704 } 722 }
@@ -708,15 +726,14 @@ static void mos7840_bulk_in_callback(struct urb *urb)
708 return; 726 return;
709 } 727 }
710 728
711 if (mos7840_port->read_urb->status != -EINPROGRESS) {
712 mos7840_port->read_urb->dev = serial->dev;
713 729
714 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 730 mos7840_port->read_urb->dev = serial->dev;
715 731
716 if (status) { 732 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
717 dbg(" usb_submit_urb(read bulk) failed, status = %d", 733
718 status); 734 if (status) {
719 } 735 dbg(" usb_submit_urb(read bulk) failed, status = %d",
736 status);
720 } 737 }
721} 738}
722 739
@@ -730,17 +747,28 @@ static void mos7840_bulk_out_data_callback(struct urb *urb)
730{ 747{
731 struct moschip_port *mos7840_port; 748 struct moschip_port *mos7840_port;
732 struct tty_struct *tty; 749 struct tty_struct *tty;
750 int i;
751
733 if (!urb) { 752 if (!urb) {
734 dbg("%s", "Invalid Pointer !!!!:\n"); 753 dbg("%s", "Invalid Pointer !!!!:\n");
735 return; 754 return;
736 } 755 }
737 756
757 mos7840_port = (struct moschip_port *)urb->context;
758 spin_lock(&mos7840_port->pool_lock);
759 for (i = 0; i < NUM_URBS; i++) {
760 if (urb == mos7840_port->write_urb_pool[i]) {
761 mos7840_port->busy[i] = 0;
762 break;
763 }
764 }
765 spin_unlock(&mos7840_port->pool_lock);
766
738 if (urb->status) { 767 if (urb->status) {
739 dbg("nonzero write bulk status received:%d\n", urb->status); 768 dbg("nonzero write bulk status received:%d\n", urb->status);
740 return; 769 return;
741 } 770 }
742 771
743 mos7840_port = (struct moschip_port *)urb->context;
744 if (!mos7840_port) { 772 if (!mos7840_port) {
745 dbg("%s", "NULL mos7840_port pointer \n"); 773 dbg("%s", "NULL mos7840_port pointer \n");
746 return; 774 return;
@@ -792,13 +820,13 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
792 __u16 Data; 820 __u16 Data;
793 int status; 821 int status;
794 struct moschip_port *mos7840_port; 822 struct moschip_port *mos7840_port;
823 struct moschip_port *port0;
795 824
796 if (mos7840_port_paranoia_check(port, __FUNCTION__)) { 825 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
797 dbg("%s", "Port Paranoia failed \n"); 826 dbg("%s", "Port Paranoia failed \n");
798 return -ENODEV; 827 return -ENODEV;
799 } 828 }
800 829
801 mos7840_num_open_ports++;
802 serial = port->serial; 830 serial = port->serial;
803 831
804 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) { 832 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
@@ -807,16 +835,18 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
807 } 835 }
808 836
809 mos7840_port = mos7840_get_port_private(port); 837 mos7840_port = mos7840_get_port_private(port);
838 port0 = mos7840_get_port_private(serial->port[0]);
810 839
811 if (mos7840_port == NULL) 840 if (mos7840_port == NULL || port0 == NULL)
812 return -ENODEV; 841 return -ENODEV;
813 842
814 usb_clear_halt(serial->dev, port->write_urb->pipe); 843 usb_clear_halt(serial->dev, port->write_urb->pipe);
815 usb_clear_halt(serial->dev, port->read_urb->pipe); 844 usb_clear_halt(serial->dev, port->read_urb->pipe);
845 port0->open_ports++;
816 846
817 /* Initialising the write urb pool */ 847 /* Initialising the write urb pool */
818 for (j = 0; j < NUM_URBS; ++j) { 848 for (j = 0; j < NUM_URBS; ++j) {
819 urb = usb_alloc_urb(0, GFP_ATOMIC); 849 urb = usb_alloc_urb(0, GFP_KERNEL);
820 mos7840_port->write_urb_pool[j] = urb; 850 mos7840_port->write_urb_pool[j] = urb;
821 851
822 if (urb == NULL) { 852 if (urb == NULL) {
@@ -824,10 +854,10 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
824 continue; 854 continue;
825 } 855 }
826 856
827 urb->transfer_buffer = NULL; 857 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
828 urb->transfer_buffer =
829 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
830 if (!urb->transfer_buffer) { 858 if (!urb->transfer_buffer) {
859 usb_free_urb(urb);
860 mos7840_port->write_urb_pool[j] = NULL;
831 err("%s-out of memory for urb buffers.", __FUNCTION__); 861 err("%s-out of memory for urb buffers.", __FUNCTION__);
832 continue; 862 continue;
833 } 863 }
@@ -879,9 +909,7 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
879 } 909 }
880 Data |= 0x08; //Driver done bit 910 Data |= 0x08; //Driver done bit
881 Data |= 0x20; //rx_disable 911 Data |= 0x20; //rx_disable
882 status = 0; 912 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
883 status =
884 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
885 if (status < 0) { 913 if (status < 0) {
886 dbg("writing Controlreg failed\n"); 914 dbg("writing Controlreg failed\n");
887 return -1; 915 return -1;
@@ -893,7 +921,6 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
893 //////////////////////////////////// 921 ////////////////////////////////////
894 922
895 Data = 0x00; 923 Data = 0x00;
896 status = 0;
897 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 924 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
898 if (status < 0) { 925 if (status < 0) {
899 dbg("disableing interrupts failed\n"); 926 dbg("disableing interrupts failed\n");
@@ -901,7 +928,6 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
901 } 928 }
902 // Set FIFO_CONTROL_REGISTER to the default value 929 // Set FIFO_CONTROL_REGISTER to the default value
903 Data = 0x00; 930 Data = 0x00;
904 status = 0;
905 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 931 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
906 if (status < 0) { 932 if (status < 0) {
907 dbg("Writing FIFO_CONTROL_REGISTER failed\n"); 933 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
@@ -909,7 +935,6 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
909 } 935 }
910 936
911 Data = 0xcf; 937 Data = 0xcf;
912 status = 0;
913 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 938 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
914 if (status < 0) { 939 if (status < 0) {
915 dbg("Writing FIFO_CONTROL_REGISTER failed\n"); 940 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
@@ -917,22 +942,18 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
917 } 942 }
918 943
919 Data = 0x03; 944 Data = 0x03;
920 status = 0;
921 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 945 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
922 mos7840_port->shadowLCR = Data; 946 mos7840_port->shadowLCR = Data;
923 947
924 Data = 0x0b; 948 Data = 0x0b;
925 status = 0;
926 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 949 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
927 mos7840_port->shadowMCR = Data; 950 mos7840_port->shadowMCR = Data;
928 951
929 Data = 0x00; 952 Data = 0x00;
930 status = 0;
931 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 953 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
932 mos7840_port->shadowLCR = Data; 954 mos7840_port->shadowLCR = Data;
933 955
934 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80 956 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
935 status = 0;
936 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 957 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
937 958
938 Data = 0x0c; 959 Data = 0x0c;
@@ -999,7 +1020,7 @@ static int mos7840_open(struct usb_serial_port *port, struct file *filp)
999/* Check to see if we've set up our endpoint info yet * 1020/* Check to see if we've set up our endpoint info yet *
1000 * (can't set it up in mos7840_startup as the structures * 1021 * (can't set it up in mos7840_startup as the structures *
1001 * were not set up at that time.) */ 1022 * were not set up at that time.) */
1002 if (mos7840_num_open_ports == 1) { 1023 if (port0->open_ports == 1) {
1003 if (serial->port[0]->interrupt_in_buffer == NULL) { 1024 if (serial->port[0]->interrupt_in_buffer == NULL) {
1004 1025
1005 /* set up interrupt urb */ 1026 /* set up interrupt urb */
@@ -1097,6 +1118,7 @@ static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1097{ 1118{
1098 int i; 1119 int i;
1099 int chars = 0; 1120 int chars = 0;
1121 unsigned long flags;
1100 struct moschip_port *mos7840_port; 1122 struct moschip_port *mos7840_port;
1101 1123
1102 dbg("%s \n", " mos7840_chars_in_buffer:entering ..........."); 1124 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
@@ -1112,13 +1134,15 @@ static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1112 return -1; 1134 return -1;
1113 } 1135 }
1114 1136
1137 spin_lock_irqsave(&mos7840_port->pool_lock,flags);
1115 for (i = 0; i < NUM_URBS; ++i) { 1138 for (i = 0; i < NUM_URBS; ++i) {
1116 if (mos7840_port->write_urb_pool[i]->status == -EINPROGRESS) { 1139 if (mos7840_port->busy[i]) {
1117 chars += URB_TRANSFER_BUFFER_SIZE; 1140 chars += URB_TRANSFER_BUFFER_SIZE;
1118 } 1141 }
1119 } 1142 }
1143 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1120 dbg("%s - returns %d", __FUNCTION__, chars); 1144 dbg("%s - returns %d", __FUNCTION__, chars);
1121 return (chars); 1145 return chars;
1122 1146
1123} 1147}
1124 1148
@@ -1172,6 +1196,7 @@ static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1172{ 1196{
1173 struct usb_serial *serial; 1197 struct usb_serial *serial;
1174 struct moschip_port *mos7840_port; 1198 struct moschip_port *mos7840_port;
1199 struct moschip_port *port0;
1175 int j; 1200 int j;
1176 __u16 Data; 1201 __u16 Data;
1177 1202
@@ -1189,10 +1214,10 @@ static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1189 } 1214 }
1190 1215
1191 mos7840_port = mos7840_get_port_private(port); 1216 mos7840_port = mos7840_get_port_private(port);
1217 port0 = mos7840_get_port_private(serial->port[0]);
1192 1218
1193 if (mos7840_port == NULL) { 1219 if (mos7840_port == NULL || port0 == NULL)
1194 return; 1220 return;
1195 }
1196 1221
1197 for (j = 0; j < NUM_URBS; ++j) 1222 for (j = 0; j < NUM_URBS; ++j)
1198 usb_kill_urb(mos7840_port->write_urb_pool[j]); 1223 usb_kill_urb(mos7840_port->write_urb_pool[j]);
@@ -1234,12 +1259,13 @@ static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1234 } 1259 }
1235// if(mos7840_port->ctrl_buf != NULL) 1260// if(mos7840_port->ctrl_buf != NULL)
1236// kfree(mos7840_port->ctrl_buf); 1261// kfree(mos7840_port->ctrl_buf);
1237 mos7840_num_open_ports--; 1262 port0->open_ports--;
1238 dbg("mos7840_num_open_ports in close%d:in port%d\n", 1263 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1239 mos7840_num_open_ports, port->number); 1264 port0->open_ports, port->number);
1240 if (mos7840_num_open_ports == 0) { 1265 if (port0->open_ports == 0) {
1241 if (serial->port[0]->interrupt_in_urb) { 1266 if (serial->port[0]->interrupt_in_urb) {
1242 dbg("%s", "Shutdown interrupt_in_urb\n"); 1267 dbg("%s", "Shutdown interrupt_in_urb\n");
1268 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1243 } 1269 }
1244 } 1270 }
1245 1271
@@ -1368,6 +1394,7 @@ static int mos7840_write_room(struct usb_serial_port *port)
1368{ 1394{
1369 int i; 1395 int i;
1370 int room = 0; 1396 int room = 0;
1397 unsigned long flags;
1371 struct moschip_port *mos7840_port; 1398 struct moschip_port *mos7840_port;
1372 1399
1373 dbg("%s \n", " mos7840_write_room:entering ..........."); 1400 dbg("%s \n", " mos7840_write_room:entering ...........");
@@ -1384,14 +1411,17 @@ static int mos7840_write_room(struct usb_serial_port *port)
1384 return -1; 1411 return -1;
1385 } 1412 }
1386 1413
1414 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1387 for (i = 0; i < NUM_URBS; ++i) { 1415 for (i = 0; i < NUM_URBS; ++i) {
1388 if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) { 1416 if (!mos7840_port->busy[i]) {
1389 room += URB_TRANSFER_BUFFER_SIZE; 1417 room += URB_TRANSFER_BUFFER_SIZE;
1390 } 1418 }
1391 } 1419 }
1420 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1392 1421
1422 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1393 dbg("%s - returns %d", __FUNCTION__, room); 1423 dbg("%s - returns %d", __FUNCTION__, room);
1394 return (room); 1424 return room;
1395 1425
1396} 1426}
1397 1427
@@ -1410,6 +1440,7 @@ static int mos7840_write(struct usb_serial_port *port,
1410 int i; 1440 int i;
1411 int bytes_sent = 0; 1441 int bytes_sent = 0;
1412 int transfer_size; 1442 int transfer_size;
1443 unsigned long flags;
1413 1444
1414 struct moschip_port *mos7840_port; 1445 struct moschip_port *mos7840_port;
1415 struct usb_serial *serial; 1446 struct usb_serial *serial;
@@ -1476,13 +1507,16 @@ static int mos7840_write(struct usb_serial_port *port,
1476 /* try to find a free urb in the list */ 1507 /* try to find a free urb in the list */
1477 urb = NULL; 1508 urb = NULL;
1478 1509
1510 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1479 for (i = 0; i < NUM_URBS; ++i) { 1511 for (i = 0; i < NUM_URBS; ++i) {
1480 if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) { 1512 if (!mos7840_port->busy[i]) {
1513 mos7840_port->busy[i] = 1;
1481 urb = mos7840_port->write_urb_pool[i]; 1514 urb = mos7840_port->write_urb_pool[i];
1482 dbg("\nURB:%d", i); 1515 dbg("\nURB:%d", i);
1483 break; 1516 break;
1484 } 1517 }
1485 } 1518 }
1519 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1486 1520
1487 if (urb == NULL) { 1521 if (urb == NULL) {
1488 dbg("%s - no more free urbs", __FUNCTION__); 1522 dbg("%s - no more free urbs", __FUNCTION__);
@@ -1518,6 +1552,7 @@ static int mos7840_write(struct usb_serial_port *port,
1518 status = usb_submit_urb(urb, GFP_ATOMIC); 1552 status = usb_submit_urb(urb, GFP_ATOMIC);
1519 1553
1520 if (status) { 1554 if (status) {
1555 mos7840_port->busy[i] = 0;
1521 err("%s - usb_submit_urb(write bulk) failed with status = %d", 1556 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1522 __FUNCTION__, status); 1557 __FUNCTION__, status);
1523 bytes_sent = status; 1558 bytes_sent = status;
@@ -1525,6 +1560,7 @@ static int mos7840_write(struct usb_serial_port *port,
1525 } 1560 }
1526 bytes_sent = transfer_size; 1561 bytes_sent = transfer_size;
1527 mos7840_port->icount.tx += transfer_size; 1562 mos7840_port->icount.tx += transfer_size;
1563 smp_wmb();
1528 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx); 1564 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1529 exit: 1565 exit:
1530 1566
@@ -2490,6 +2526,7 @@ static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2490 if (signal_pending(current)) 2526 if (signal_pending(current))
2491 return -ERESTARTSYS; 2527 return -ERESTARTSYS;
2492 cnow = mos7840_port->icount; 2528 cnow = mos7840_port->icount;
2529 smp_rmb();
2493 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 2530 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2494 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 2531 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2495 return -EIO; /* no change => error */ 2532 return -EIO; /* no change => error */
@@ -2506,6 +2543,7 @@ static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2506 2543
2507 case TIOCGICOUNT: 2544 case TIOCGICOUNT:
2508 cnow = mos7840_port->icount; 2545 cnow = mos7840_port->icount;
2546 smp_rmb();
2509 icount.cts = cnow.cts; 2547 icount.cts = cnow.cts;
2510 icount.dsr = cnow.dsr; 2548 icount.dsr = cnow.dsr;
2511 icount.rng = cnow.rng; 2549 icount.rng = cnow.rng;
@@ -2535,19 +2573,18 @@ static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2535 2573
2536static int mos7840_calc_num_ports(struct usb_serial *serial) 2574static int mos7840_calc_num_ports(struct usb_serial *serial)
2537{ 2575{
2576 int mos7840_num_ports = 0;
2538 2577
2539 dbg("numberofendpoints: %d \n", 2578 dbg("numberofendpoints: %d \n",
2540 (int)serial->interface->cur_altsetting->desc.bNumEndpoints); 2579 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2541 dbg("numberofendpoints: %d \n", 2580 dbg("numberofendpoints: %d \n",
2542 (int)serial->interface->altsetting->desc.bNumEndpoints); 2581 (int)serial->interface->altsetting->desc.bNumEndpoints);
2543 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) { 2582 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2544 mos7840_num_ports = 2; 2583 mos7840_num_ports = serial->num_ports = 2;
2545 serial->type->num_ports = 2;
2546 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) { 2584 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2547 mos7840_num_ports = 4; 2585 serial->num_bulk_in = 4;
2548 serial->type->num_bulk_in = 4; 2586 serial->num_bulk_out = 4;
2549 serial->type->num_bulk_out = 4; 2587 mos7840_num_ports = serial->num_ports = 4;
2550 serial->type->num_ports = 4;
2551 } 2588 }
2552 2589
2553 return mos7840_num_ports; 2590 return mos7840_num_ports;
@@ -2583,7 +2620,9 @@ static int mos7840_startup(struct usb_serial *serial)
2583 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 2620 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2584 if (mos7840_port == NULL) { 2621 if (mos7840_port == NULL) {
2585 err("%s - Out of memory", __FUNCTION__); 2622 err("%s - Out of memory", __FUNCTION__);
2586 return -ENOMEM; 2623 status = -ENOMEM;
2624 i--; /* don't follow NULL pointer cleaning up */
2625 goto error;
2587 } 2626 }
2588 2627
2589 /* Initialize all port interrupt end point to port 0 int endpoint * 2628 /* Initialize all port interrupt end point to port 0 int endpoint *
@@ -2591,6 +2630,7 @@ static int mos7840_startup(struct usb_serial *serial)
2591 2630
2592 mos7840_port->port = serial->port[i]; 2631 mos7840_port->port = serial->port[i];
2593 mos7840_set_port_private(serial->port[i], mos7840_port); 2632 mos7840_set_port_private(serial->port[i], mos7840_port);
2633 spin_lock_init(&mos7840_port->pool_lock);
2594 2634
2595 mos7840_port->port_num = ((serial->port[i]->number - 2635 mos7840_port->port_num = ((serial->port[i]->number -
2596 (serial->port[i]->serial->minor)) + 2636 (serial->port[i]->serial->minor)) +
@@ -2601,22 +2641,22 @@ static int mos7840_startup(struct usb_serial *serial)
2601 mos7840_port->ControlRegOffset = 0x1; 2641 mos7840_port->ControlRegOffset = 0x1;
2602 mos7840_port->DcrRegOffset = 0x4; 2642 mos7840_port->DcrRegOffset = 0x4;
2603 } else if ((mos7840_port->port_num == 2) 2643 } else if ((mos7840_port->port_num == 2)
2604 && (mos7840_num_ports == 4)) { 2644 && (serial->num_ports == 4)) {
2605 mos7840_port->SpRegOffset = 0x8; 2645 mos7840_port->SpRegOffset = 0x8;
2606 mos7840_port->ControlRegOffset = 0x9; 2646 mos7840_port->ControlRegOffset = 0x9;
2607 mos7840_port->DcrRegOffset = 0x16; 2647 mos7840_port->DcrRegOffset = 0x16;
2608 } else if ((mos7840_port->port_num == 2) 2648 } else if ((mos7840_port->port_num == 2)
2609 && (mos7840_num_ports == 2)) { 2649 && (serial->num_ports == 2)) {
2610 mos7840_port->SpRegOffset = 0xa; 2650 mos7840_port->SpRegOffset = 0xa;
2611 mos7840_port->ControlRegOffset = 0xb; 2651 mos7840_port->ControlRegOffset = 0xb;
2612 mos7840_port->DcrRegOffset = 0x19; 2652 mos7840_port->DcrRegOffset = 0x19;
2613 } else if ((mos7840_port->port_num == 3) 2653 } else if ((mos7840_port->port_num == 3)
2614 && (mos7840_num_ports == 4)) { 2654 && (serial->num_ports == 4)) {
2615 mos7840_port->SpRegOffset = 0xa; 2655 mos7840_port->SpRegOffset = 0xa;
2616 mos7840_port->ControlRegOffset = 0xb; 2656 mos7840_port->ControlRegOffset = 0xb;
2617 mos7840_port->DcrRegOffset = 0x19; 2657 mos7840_port->DcrRegOffset = 0x19;
2618 } else if ((mos7840_port->port_num == 4) 2658 } else if ((mos7840_port->port_num == 4)
2619 && (mos7840_num_ports == 4)) { 2659 && (serial->num_ports == 4)) {
2620 mos7840_port->SpRegOffset = 0xc; 2660 mos7840_port->SpRegOffset = 0xc;
2621 mos7840_port->ControlRegOffset = 0xd; 2661 mos7840_port->ControlRegOffset = 0xd;
2622 mos7840_port->DcrRegOffset = 0x1c; 2662 mos7840_port->DcrRegOffset = 0x1c;
@@ -2701,21 +2741,19 @@ static int mos7840_startup(struct usb_serial *serial)
2701 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status); 2741 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2702 2742
2703 Data = 0x20; 2743 Data = 0x20;
2704 status = 0;
2705 status = 2744 status =
2706 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER, 2745 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2707 Data); 2746 Data);
2708 if (status < 0) { 2747 if (status < 0) {
2709 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n", 2748 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2710 status); 2749 status);
2711 break; 2750 goto error;
2712 } else 2751 } else
2713 dbg("CLK_MULTI_REGISTER Writing success status%d\n", 2752 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2714 status); 2753 status);
2715 2754
2716 //write value 0x0 to scratchpad register 2755 //write value 0x0 to scratchpad register
2717 Data = 0x00; 2756 Data = 0x00;
2718 status = 0;
2719 status = 2757 status =
2720 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER, 2758 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2721 Data); 2759 Data);
@@ -2729,7 +2767,7 @@ static int mos7840_startup(struct usb_serial *serial)
2729 2767
2730 //Zero Length flag register 2768 //Zero Length flag register
2731 if ((mos7840_port->port_num != 1) 2769 if ((mos7840_port->port_num != 1)
2732 && (mos7840_num_ports == 2)) { 2770 && (serial->num_ports == 2)) {
2733 2771
2734 Data = 0xff; 2772 Data = 0xff;
2735 status = 0; 2773 status = 0;
@@ -2770,14 +2808,17 @@ static int mos7840_startup(struct usb_serial *serial)
2770 i + 1, status); 2808 i + 1, status);
2771 2809
2772 } 2810 }
2773 mos7840_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC); 2811 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2774 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); 2812 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2775 2813 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2814 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2815 status = -ENOMEM;
2816 goto error;
2817 }
2776 } 2818 }
2777 2819
2778 //Zero Length flag enable 2820 //Zero Length flag enable
2779 Data = 0x0f; 2821 Data = 0x0f;
2780 status = 0;
2781 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 2822 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2782 if (status < 0) { 2823 if (status < 0) {
2783 dbg("Writing ZLP_REG5 failed status-0x%x\n", status); 2824 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
@@ -2789,6 +2830,17 @@ static int mos7840_startup(struct usb_serial *serial)
2789 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2830 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2790 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ); 2831 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2791 return 0; 2832 return 0;
2833error:
2834 for (/* nothing */; i >= 0; i--) {
2835 mos7840_port = mos7840_get_port_private(serial->port[i]);
2836
2837 kfree(mos7840_port->dr);
2838 kfree(mos7840_port->ctrl_buf);
2839 usb_free_urb(mos7840_port->control_urb);
2840 kfree(mos7840_port);
2841 serial->port[i] = NULL;
2842 }
2843 return status;
2792} 2844}
2793 2845
2794/**************************************************************************** 2846/****************************************************************************
@@ -2799,6 +2851,7 @@ static int mos7840_startup(struct usb_serial *serial)
2799static void mos7840_shutdown(struct usb_serial *serial) 2851static void mos7840_shutdown(struct usb_serial *serial)
2800{ 2852{
2801 int i; 2853 int i;
2854 unsigned long flags;
2802 struct moschip_port *mos7840_port; 2855 struct moschip_port *mos7840_port;
2803 dbg("%s \n", " shutdown :entering.........."); 2856 dbg("%s \n", " shutdown :entering..........");
2804 2857
@@ -2814,8 +2867,12 @@ static void mos7840_shutdown(struct usb_serial *serial)
2814 2867
2815 for (i = 0; i < serial->num_ports; ++i) { 2868 for (i = 0; i < serial->num_ports; ++i) {
2816 mos7840_port = mos7840_get_port_private(serial->port[i]); 2869 mos7840_port = mos7840_get_port_private(serial->port[i]);
2817 kfree(mos7840_port->ctrl_buf); 2870 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2871 mos7840_port->zombie = 1;
2872 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2818 usb_kill_urb(mos7840_port->control_urb); 2873 usb_kill_urb(mos7840_port->control_urb);
2874 kfree(mos7840_port->ctrl_buf);
2875 kfree(mos7840_port->dr);
2819 kfree(mos7840_port); 2876 kfree(mos7840_port);
2820 mos7840_set_port_private(serial->port[i], NULL); 2877 mos7840_set_port_private(serial->port[i], NULL);
2821 } 2878 }
diff --git a/drivers/usb/serial/omninet.c b/drivers/usb/serial/omninet.c
index 0216ac12a27d..4adfab988e86 100644
--- a/drivers/usb/serial/omninet.c
+++ b/drivers/usb/serial/omninet.c
@@ -69,6 +69,7 @@ static void omninet_write_bulk_callback (struct urb *urb);
69static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count); 69static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count);
70static int omninet_write_room (struct usb_serial_port *port); 70static int omninet_write_room (struct usb_serial_port *port);
71static void omninet_shutdown (struct usb_serial *serial); 71static void omninet_shutdown (struct usb_serial *serial);
72static int omninet_attach (struct usb_serial *serial);
72 73
73static struct usb_device_id id_table [] = { 74static struct usb_device_id id_table [] = {
74 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) }, 75 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
@@ -99,6 +100,7 @@ static struct usb_serial_driver zyxel_omninet_device = {
99 .num_bulk_in = 1, 100 .num_bulk_in = 1,
100 .num_bulk_out = 2, 101 .num_bulk_out = 2,
101 .num_ports = 1, 102 .num_ports = 1,
103 .attach = omninet_attach,
102 .open = omninet_open, 104 .open = omninet_open,
103 .close = omninet_close, 105 .close = omninet_close,
104 .write = omninet_write, 106 .write = omninet_write,
@@ -145,22 +147,30 @@ struct omninet_data
145 __u8 od_outseq; // Sequence number for bulk_out URBs 147 __u8 od_outseq; // Sequence number for bulk_out URBs
146}; 148};
147 149
150static int omninet_attach (struct usb_serial *serial)
151{
152 struct omninet_data *od;
153 struct usb_serial_port *port = serial->port[0];
154
155 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
156 if( !od ) {
157 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
158 return -ENOMEM;
159 }
160 usb_set_serial_port_data(port, od);
161 return 0;
162}
163
148static int omninet_open (struct usb_serial_port *port, struct file *filp) 164static int omninet_open (struct usb_serial_port *port, struct file *filp)
149{ 165{
150 struct usb_serial *serial = port->serial; 166 struct usb_serial *serial = port->serial;
151 struct usb_serial_port *wport; 167 struct usb_serial_port *wport;
152 struct omninet_data *od; 168 struct omninet_data *od = usb_get_serial_port_data(port);
153 int result = 0; 169 int result = 0;
154 170
155 dbg("%s - port %d", __FUNCTION__, port->number); 171 dbg("%s - port %d", __FUNCTION__, port->number);
156 172
157 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL ); 173 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
158 if( !od ) {
159 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
160 return -ENOMEM;
161 }
162
163 usb_set_serial_port_data(port, od);
164 wport = serial->port[1]; 174 wport = serial->port[1];
165 wport->tty = port->tty; 175 wport->tty = port->tty;
166 176
@@ -170,24 +180,17 @@ static int omninet_open (struct usb_serial_port *port, struct file *filp)
170 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length, 180 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
171 omninet_read_bulk_callback, port); 181 omninet_read_bulk_callback, port);
172 result = usb_submit_urb(port->read_urb, GFP_KERNEL); 182 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
173 if (result) 183 if (result) {
174 err("%s - failed submitting read urb, error %d", __FUNCTION__, result); 184 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
185 }
175 186
176 return result; 187 return result;
177} 188}
178 189
179static void omninet_close (struct usb_serial_port *port, struct file * filp) 190static void omninet_close (struct usb_serial_port *port, struct file * filp)
180{ 191{
181 struct usb_serial *serial = port->serial;
182 struct usb_serial_port *wport;
183
184 dbg("%s - port %d", __FUNCTION__, port->number); 192 dbg("%s - port %d", __FUNCTION__, port->number);
185
186 wport = serial->port[1];
187 usb_kill_urb(wport->write_urb);
188 usb_kill_urb(port->read_urb); 193 usb_kill_urb(port->read_urb);
189
190 kfree(usb_get_serial_port_data(port));
191} 194}
192 195
193 196
@@ -326,7 +329,12 @@ static void omninet_write_bulk_callback (struct urb *urb)
326 329
327static void omninet_shutdown (struct usb_serial *serial) 330static void omninet_shutdown (struct usb_serial *serial)
328{ 331{
332 struct usb_serial_port *wport = serial->port[1];
333 struct usb_serial_port *port = serial->port[0];
329 dbg ("%s", __FUNCTION__); 334 dbg ("%s", __FUNCTION__);
335
336 usb_kill_urb(wport->write_urb);
337 kfree(usb_get_serial_port_data(port));
330} 338}
331 339
332 340
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index e178e6f40319..8c3f55b080b4 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -113,6 +113,12 @@ static int option_send_setup(struct usb_serial_port *port);
113#define ANYDATA_VENDOR_ID 0x16d5 113#define ANYDATA_VENDOR_ID 0x16d5
114#define ANYDATA_PRODUCT_ID 0x6501 114#define ANYDATA_PRODUCT_ID 0x6501
115 115
116#define BANDRICH_VENDOR_ID 0x1A8D
117#define BANDRICH_PRODUCT_C100_1 0x1002
118#define BANDRICH_PRODUCT_C100_2 0x1003
119
120#define DELL_VENDOR_ID 0x413C
121
116static struct usb_device_id option_ids[] = { 122static struct usb_device_id option_ids[] = {
117 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COLT) }, 123 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COLT) },
118 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA) }, 124 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA) },
@@ -165,6 +171,9 @@ static struct usb_device_id option_ids[] = {
165 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2130) }, /* Novatel Merlin ES620 SM Bus */ 171 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2130) }, /* Novatel Merlin ES620 SM Bus */
166 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2410) }, /* Novatel EU740 */ 172 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2410) }, /* Novatel EU740 */
167 { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ID) }, 173 { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ID) },
174 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_1) },
175 { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_2) },
176 { USB_DEVICE(DELL_VENDOR_ID, 0x8118) }, /* Dell Wireless 5510 Mobile Broadband HSDPA ExpressCard */
168 { } /* Terminating entry */ 177 { } /* Terminating entry */
169}; 178};
170MODULE_DEVICE_TABLE(usb, option_ids); 179MODULE_DEVICE_TABLE(usb, option_ids);
@@ -591,12 +600,6 @@ static int option_open(struct usb_serial_port *port, struct file *filp)
591 return (0); 600 return (0);
592} 601}
593 602
594static inline void stop_urb(struct urb *urb)
595{
596 if (urb && urb->status == -EINPROGRESS)
597 usb_kill_urb(urb);
598}
599
600static void option_close(struct usb_serial_port *port, struct file *filp) 603static void option_close(struct usb_serial_port *port, struct file *filp)
601{ 604{
602 int i; 605 int i;
@@ -614,9 +617,9 @@ static void option_close(struct usb_serial_port *port, struct file *filp)
614 617
615 /* Stop reading/writing urbs */ 618 /* Stop reading/writing urbs */
616 for (i = 0; i < N_IN_URB; i++) 619 for (i = 0; i < N_IN_URB; i++)
617 stop_urb(portdata->in_urbs[i]); 620 usb_kill_urb(portdata->in_urbs[i]);
618 for (i = 0; i < N_OUT_URB; i++) 621 for (i = 0; i < N_OUT_URB; i++)
619 stop_urb(portdata->out_urbs[i]); 622 usb_kill_urb(portdata->out_urbs[i]);
620 } 623 }
621 port->tty = NULL; 624 port->tty = NULL;
622} 625}
@@ -747,9 +750,9 @@ static void option_shutdown(struct usb_serial *serial)
747 port = serial->port[i]; 750 port = serial->port[i];
748 portdata = usb_get_serial_port_data(port); 751 portdata = usb_get_serial_port_data(port);
749 for (j = 0; j < N_IN_URB; j++) 752 for (j = 0; j < N_IN_URB; j++)
750 stop_urb(portdata->in_urbs[j]); 753 usb_kill_urb(portdata->in_urbs[j]);
751 for (j = 0; j < N_OUT_URB; j++) 754 for (j = 0; j < N_OUT_URB; j++)
752 stop_urb(portdata->out_urbs[j]); 755 usb_kill_urb(portdata->out_urbs[j]);
753 } 756 }
754 757
755 /* Now free them */ 758 /* Now free them */
diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c
index ecedd833818d..644607de4c11 100644
--- a/drivers/usb/serial/sierra.c
+++ b/drivers/usb/serial/sierra.c
@@ -456,12 +456,6 @@ static int sierra_open(struct usb_serial_port *port, struct file *filp)
456 return (0); 456 return (0);
457} 457}
458 458
459static inline void stop_urb(struct urb *urb)
460{
461 if (urb && urb->status == -EINPROGRESS)
462 usb_kill_urb(urb);
463}
464
465static void sierra_close(struct usb_serial_port *port, struct file *filp) 459static void sierra_close(struct usb_serial_port *port, struct file *filp)
466{ 460{
467 int i; 461 int i;
@@ -479,9 +473,9 @@ static void sierra_close(struct usb_serial_port *port, struct file *filp)
479 473
480 /* Stop reading/writing urbs */ 474 /* Stop reading/writing urbs */
481 for (i = 0; i < N_IN_URB; i++) 475 for (i = 0; i < N_IN_URB; i++)
482 stop_urb(portdata->in_urbs[i]); 476 usb_unlink_urb(portdata->in_urbs[i]);
483 for (i = 0; i < N_OUT_URB; i++) 477 for (i = 0; i < N_OUT_URB; i++)
484 stop_urb(portdata->out_urbs[i]); 478 usb_unlink_urb(portdata->out_urbs[i]);
485 } 479 }
486 port->tty = NULL; 480 port->tty = NULL;
487} 481}
@@ -583,17 +577,26 @@ static void sierra_shutdown(struct usb_serial *serial)
583 /* Stop reading/writing urbs */ 577 /* Stop reading/writing urbs */
584 for (i = 0; i < serial->num_ports; ++i) { 578 for (i = 0; i < serial->num_ports; ++i) {
585 port = serial->port[i]; 579 port = serial->port[i];
580 if (!port)
581 continue;
586 portdata = usb_get_serial_port_data(port); 582 portdata = usb_get_serial_port_data(port);
583 if (!portdata)
584 continue;
585
587 for (j = 0; j < N_IN_URB; j++) 586 for (j = 0; j < N_IN_URB; j++)
588 stop_urb(portdata->in_urbs[j]); 587 usb_unlink_urb(portdata->in_urbs[j]);
589 for (j = 0; j < N_OUT_URB; j++) 588 for (j = 0; j < N_OUT_URB; j++)
590 stop_urb(portdata->out_urbs[j]); 589 usb_unlink_urb(portdata->out_urbs[j]);
591 } 590 }
592 591
593 /* Now free them */ 592 /* Now free them */
594 for (i = 0; i < serial->num_ports; ++i) { 593 for (i = 0; i < serial->num_ports; ++i) {
595 port = serial->port[i]; 594 port = serial->port[i];
595 if (!port)
596 continue;
596 portdata = usb_get_serial_port_data(port); 597 portdata = usb_get_serial_port_data(port);
598 if (!portdata)
599 continue;
597 600
598 for (j = 0; j < N_IN_URB; j++) { 601 for (j = 0; j < N_IN_URB; j++) {
599 if (portdata->in_urbs[j]) { 602 if (portdata->in_urbs[j]) {
@@ -612,6 +615,8 @@ static void sierra_shutdown(struct usb_serial *serial)
612 /* Now free per port private data */ 615 /* Now free per port private data */
613 for (i = 0; i < serial->num_ports; i++) { 616 for (i = 0; i < serial->num_ports; i++) {
614 port = serial->port[i]; 617 port = serial->port[i];
618 if (!port)
619 continue;
615 kfree(usb_get_serial_port_data(port)); 620 kfree(usb_get_serial_port_data(port));
616 } 621 }
617} 622}
diff --git a/drivers/usb/serial/visor.c b/drivers/usb/serial/visor.c
index 2f59ff226e2c..ffbe601cde2a 100644
--- a/drivers/usb/serial/visor.c
+++ b/drivers/usb/serial/visor.c
@@ -384,19 +384,21 @@ static int visor_write (struct usb_serial_port *port, const unsigned char *buf,
384 dbg("%s - write limit hit\n", __FUNCTION__); 384 dbg("%s - write limit hit\n", __FUNCTION__);
385 return 0; 385 return 0;
386 } 386 }
387 priv->outstanding_urbs++;
387 spin_unlock_irqrestore(&priv->lock, flags); 388 spin_unlock_irqrestore(&priv->lock, flags);
388 389
389 buffer = kmalloc (count, GFP_ATOMIC); 390 buffer = kmalloc (count, GFP_ATOMIC);
390 if (!buffer) { 391 if (!buffer) {
391 dev_err(&port->dev, "out of memory\n"); 392 dev_err(&port->dev, "out of memory\n");
392 return -ENOMEM; 393 count = -ENOMEM;
394 goto error_no_buffer;
393 } 395 }
394 396
395 urb = usb_alloc_urb(0, GFP_ATOMIC); 397 urb = usb_alloc_urb(0, GFP_ATOMIC);
396 if (!urb) { 398 if (!urb) {
397 dev_err(&port->dev, "no more free urbs\n"); 399 dev_err(&port->dev, "no more free urbs\n");
398 kfree (buffer); 400 count = -ENOMEM;
399 return -ENOMEM; 401 goto error_no_urb;
400 } 402 }
401 403
402 memcpy (buffer, buf, count); 404 memcpy (buffer, buf, count);
@@ -415,19 +417,27 @@ static int visor_write (struct usb_serial_port *port, const unsigned char *buf,
415 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", 417 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
416 __FUNCTION__, status); 418 __FUNCTION__, status);
417 count = status; 419 count = status;
418 kfree (buffer); 420 goto error;
419 } else { 421 } else {
420 spin_lock_irqsave(&priv->lock, flags); 422 spin_lock_irqsave(&priv->lock, flags);
421 ++priv->outstanding_urbs;
422 priv->bytes_out += count; 423 priv->bytes_out += count;
423 spin_unlock_irqrestore(&priv->lock, flags); 424 spin_unlock_irqrestore(&priv->lock, flags);
424 } 425 }
425 426
426 /* we are done with this urb, so let the host driver 427 /* we are done with this urb, so let the host driver
427 * really free it when it is finished with it */ 428 * really free it when it is finished with it */
428 usb_free_urb (urb); 429 usb_free_urb(urb);
429 430
430 return count; 431 return count;
432error:
433 usb_free_urb(urb);
434error_no_urb:
435 kfree(buffer);
436error_no_buffer:
437 spin_lock_irqsave(&priv->lock, flags);
438 --priv->outstanding_urbs;
439 spin_unlock_irqrestore(&priv->lock, flags);
440 return count;
431} 441}
432 442
433 443
diff --git a/drivers/usb/serial/whiteheat.c b/drivers/usb/serial/whiteheat.c
index bf16e9e1d84e..27c5f8f9a2d5 100644
--- a/drivers/usb/serial/whiteheat.c
+++ b/drivers/usb/serial/whiteheat.c
@@ -1109,7 +1109,7 @@ static int firm_send_command (struct usb_serial_port *port, __u8 command, __u8 *
1109 command_port = port->serial->port[COMMAND_PORT]; 1109 command_port = port->serial->port[COMMAND_PORT];
1110 command_info = usb_get_serial_port_data(command_port); 1110 command_info = usb_get_serial_port_data(command_port);
1111 spin_lock_irqsave(&command_info->lock, flags); 1111 spin_lock_irqsave(&command_info->lock, flags);
1112 command_info->command_finished = FALSE; 1112 command_info->command_finished = false;
1113 1113
1114 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer; 1114 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1115 transfer_buffer[0] = command; 1115 transfer_buffer[0] = command;
@@ -1124,12 +1124,12 @@ static int firm_send_command (struct usb_serial_port *port, __u8 command, __u8 *
1124 spin_unlock_irqrestore(&command_info->lock, flags); 1124 spin_unlock_irqrestore(&command_info->lock, flags);
1125 1125
1126 /* wait for the command to complete */ 1126 /* wait for the command to complete */
1127 wait_event_interruptible_timeout(command_info->wait_command, 1127 wait_event_interruptible_timeout(command_info->wait_command,
1128 (command_info->command_finished != FALSE), COMMAND_TIMEOUT); 1128 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1129 1129
1130 spin_lock_irqsave(&command_info->lock, flags); 1130 spin_lock_irqsave(&command_info->lock, flags);
1131 1131
1132 if (command_info->command_finished == FALSE) { 1132 if (command_info->command_finished == false) {
1133 dbg("%s - command timed out.", __FUNCTION__); 1133 dbg("%s - command timed out.", __FUNCTION__);
1134 retval = -ETIMEDOUT; 1134 retval = -ETIMEDOUT;
1135 goto exit; 1135 goto exit;
diff --git a/drivers/usb/serial/whiteheat.h b/drivers/usb/serial/whiteheat.h
index d714eff58dc0..f16079705664 100644
--- a/drivers/usb/serial/whiteheat.h
+++ b/drivers/usb/serial/whiteheat.h
@@ -20,10 +20,6 @@
20#define __LINUX_USB_SERIAL_WHITEHEAT_H 20#define __LINUX_USB_SERIAL_WHITEHEAT_H
21 21
22 22
23#define FALSE 0
24#define TRUE 1
25
26
27/* WhiteHEAT commands */ 23/* WhiteHEAT commands */
28#define WHITEHEAT_OPEN 1 /* open the port */ 24#define WHITEHEAT_OPEN 1 /* open the port */
29#define WHITEHEAT_CLOSE 2 /* close the port */ 25#define WHITEHEAT_CLOSE 2 /* close the port */
diff --git a/drivers/usb/storage/libusual.c b/drivers/usb/storage/libusual.c
index 599ad10a761b..06d1107dbd47 100644
--- a/drivers/usb/storage/libusual.c
+++ b/drivers/usb/storage/libusual.c
@@ -117,6 +117,7 @@ EXPORT_SYMBOL_GPL(usb_usual_check_type);
117static int usu_probe(struct usb_interface *intf, 117static int usu_probe(struct usb_interface *intf,
118 const struct usb_device_id *id) 118 const struct usb_device_id *id)
119{ 119{
120 int rc;
120 unsigned long type; 121 unsigned long type;
121 struct task_struct* task; 122 struct task_struct* task;
122 unsigned long flags; 123 unsigned long flags;
@@ -135,7 +136,7 @@ static int usu_probe(struct usb_interface *intf,
135 136
136 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type); 137 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
137 if (IS_ERR(task)) { 138 if (IS_ERR(task)) {
138 int rc = PTR_ERR(task); 139 rc = PTR_ERR(task);
139 printk(KERN_WARNING "libusual: " 140 printk(KERN_WARNING "libusual: "
140 "Unable to start the thread for %s: %d\n", 141 "Unable to start the thread for %s: %d\n",
141 bias_names[type], rc); 142 bias_names[type], rc);
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index 4a9d0d5c7282..8b3145ab7757 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -1371,15 +1371,6 @@ UNUSUAL_DEV( 0x1210, 0x0003, 0x0100, 0x0100,
1371 US_SC_DEVICE, US_PR_DEVICE, NULL, 1371 US_SC_DEVICE, US_PR_DEVICE, NULL,
1372 US_FL_IGNORE_RESIDUE ), 1372 US_FL_IGNORE_RESIDUE ),
1373 1373
1374/* This prevents the kernel from detecting the virtual cd-drive with the
1375 * Windows drivers. <johann.wilhelm@student.tugraz.at>
1376*/
1377UNUSUAL_DEV( 0x12d1, 0x1003, 0x0000, 0xffff,
1378 "HUAWEI",
1379 "E220 USB-UMTS Install",
1380 US_SC_DEVICE, US_PR_DEVICE, NULL,
1381 US_FL_IGNORE_DEVICE),
1382
1383/* Reported by Vilius Bilinkevicius <vilisas AT xxx DOT lt) */ 1374/* Reported by Vilius Bilinkevicius <vilisas AT xxx DOT lt) */
1384UNUSUAL_DEV( 0x132b, 0x000b, 0x0001, 0x0001, 1375UNUSUAL_DEV( 0x132b, 0x000b, 0x0001, 0x0001,
1385 "Minolta", 1376 "Minolta",
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index 46929a1b6f24..8432bf171d2e 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -34,18 +34,25 @@ static struct usb_device_id skel_table [] = {
34}; 34};
35MODULE_DEVICE_TABLE(usb, skel_table); 35MODULE_DEVICE_TABLE(usb, skel_table);
36 36
37/* to prevent a race between open and disconnect */
38static DEFINE_MUTEX(skel_open_lock);
39
37 40
38/* Get a minor range for your devices from the usb maintainer */ 41/* Get a minor range for your devices from the usb maintainer */
39#define USB_SKEL_MINOR_BASE 192 42#define USB_SKEL_MINOR_BASE 192
40 43
41/* our private defines. if this grows any larger, use your own .h file */ 44/* our private defines. if this grows any larger, use your own .h file */
42#define MAX_TRANSFER (PAGE_SIZE - 512) 45#define MAX_TRANSFER (PAGE_SIZE - 512)
46/* MAX_TRANSFER is chosen so that the VM is not stressed by
47 allocations > PAGE_SIZE and the number of packets in a page
48 is an integer 512 is the largest possible packet on EHCI */
43#define WRITES_IN_FLIGHT 8 49#define WRITES_IN_FLIGHT 8
50/* arbitrarily chosen */
44 51
45/* Structure to hold all of our device specific stuff */ 52/* Structure to hold all of our device specific stuff */
46struct usb_skel { 53struct usb_skel {
47 struct usb_device *dev; /* the usb device for this device */ 54 struct usb_device *udev; /* the usb device for this device */
48 struct usb_interface *interface; /* the interface for this device */ 55 struct usb_interface *interface; /* the interface for this device */
49 struct semaphore limit_sem; /* limiting the number of writes in progress */ 56 struct semaphore limit_sem; /* limiting the number of writes in progress */
50 unsigned char *bulk_in_buffer; /* the buffer to receive data */ 57 unsigned char *bulk_in_buffer; /* the buffer to receive data */
51 size_t bulk_in_size; /* the size of the receive buffer */ 58 size_t bulk_in_size; /* the size of the receive buffer */
@@ -76,8 +83,10 @@ static int skel_open(struct inode *inode, struct file *file)
76 83
77 subminor = iminor(inode); 84 subminor = iminor(inode);
78 85
86 mutex_lock(&skel_open_lock);
79 interface = usb_find_interface(&skel_driver, subminor); 87 interface = usb_find_interface(&skel_driver, subminor);
80 if (!interface) { 88 if (!interface) {
89 mutex_unlock(&skel_open_lock);
81 err ("%s - error, can't find device for minor %d", 90 err ("%s - error, can't find device for minor %d",
82 __FUNCTION__, subminor); 91 __FUNCTION__, subminor);
83 retval = -ENODEV; 92 retval = -ENODEV;
@@ -86,12 +95,15 @@ static int skel_open(struct inode *inode, struct file *file)
86 95
87 dev = usb_get_intfdata(interface); 96 dev = usb_get_intfdata(interface);
88 if (!dev) { 97 if (!dev) {
98 mutex_unlock(&skel_open_lock);
89 retval = -ENODEV; 99 retval = -ENODEV;
90 goto exit; 100 goto exit;
91 } 101 }
92 102
93 /* increment our usage count for the device */ 103 /* increment our usage count for the device */
94 kref_get(&dev->kref); 104 kref_get(&dev->kref);
105 /* now we can drop the lock */
106 mutex_unlock(&skel_open_lock);
95 107
96 /* prevent the device from being autosuspended */ 108 /* prevent the device from being autosuspended */
97 retval = usb_autopm_get_interface(interface); 109 retval = usb_autopm_get_interface(interface);
@@ -201,12 +213,6 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
201 goto exit; 213 goto exit;
202 } 214 }
203 215
204 mutex_lock(&dev->io_mutex);
205 if (!dev->interface) { /* disconnect() was called */
206 retval = -ENODEV;
207 goto error;
208 }
209
210 /* create a urb, and a buffer for it, and copy the data to the urb */ 216 /* create a urb, and a buffer for it, and copy the data to the urb */
211 urb = usb_alloc_urb(0, GFP_KERNEL); 217 urb = usb_alloc_urb(0, GFP_KERNEL);
212 if (!urb) { 218 if (!urb) {
@@ -225,6 +231,14 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
225 goto error; 231 goto error;
226 } 232 }
227 233
234 /* this lock makes sure we don't submit URBs to gone devices */
235 mutex_lock(&dev->io_mutex);
236 if (!dev->interface) { /* disconnect() was called */
237 mutex_unlock(&dev->io_mutex);
238 retval = -ENODEV;
239 goto error;
240 }
241
228 /* initialize the urb properly */ 242 /* initialize the urb properly */
229 usb_fill_bulk_urb(urb, dev->udev, 243 usb_fill_bulk_urb(urb, dev->udev,
230 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), 244 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
@@ -233,6 +247,7 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
233 247
234 /* send the data out the bulk port */ 248 /* send the data out the bulk port */
235 retval = usb_submit_urb(urb, GFP_KERNEL); 249 retval = usb_submit_urb(urb, GFP_KERNEL);
250 mutex_unlock(&dev->io_mutex);
236 if (retval) { 251 if (retval) {
237 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval); 252 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
238 goto error; 253 goto error;
@@ -241,7 +256,7 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
241 /* release our reference to this urb, the USB core will eventually free it entirely */ 256 /* release our reference to this urb, the USB core will eventually free it entirely */
242 usb_free_urb(urb); 257 usb_free_urb(urb);
243 258
244 mutex_unlock(&dev->io_mutex); 259
245 return writesize; 260 return writesize;
246 261
247error: 262error:
@@ -249,7 +264,6 @@ error:
249 usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); 264 usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
250 usb_free_urb(urb); 265 usb_free_urb(urb);
251 } 266 }
252 mutex_unlock(&dev->io_mutex);
253 up(&dev->limit_sem); 267 up(&dev->limit_sem);
254 268
255exit: 269exit:
@@ -344,6 +358,7 @@ static int skel_probe(struct usb_interface *interface, const struct usb_device_i
344 358
345error: 359error:
346 if (dev) 360 if (dev)
361 /* this frees allocated memory */
347 kref_put(&dev->kref, skel_delete); 362 kref_put(&dev->kref, skel_delete);
348 return retval; 363 return retval;
349} 364}
@@ -354,20 +369,21 @@ static void skel_disconnect(struct usb_interface *interface)
354 int minor = interface->minor; 369 int minor = interface->minor;
355 370
356 /* prevent skel_open() from racing skel_disconnect() */ 371 /* prevent skel_open() from racing skel_disconnect() */
357 lock_kernel(); 372 mutex_lock(&skel_open_lock);
358 373
359 dev = usb_get_intfdata(interface); 374 dev = usb_get_intfdata(interface);
360 usb_set_intfdata(interface, NULL); 375 usb_set_intfdata(interface, NULL);
361 376
362 /* give back our minor */ 377 /* give back our minor */
363 usb_deregister_dev(interface, &skel_class); 378 usb_deregister_dev(interface, &skel_class);
379 mutex_unlock(&skel_open_lock);
364 380
365 /* prevent more I/O from starting */ 381 /* prevent more I/O from starting */
366 mutex_lock(&dev->io_mutex); 382 mutex_lock(&dev->io_mutex);
367 dev->interface = NULL; 383 dev->interface = NULL;
368 mutex_unlock(&dev->io_mutex); 384 mutex_unlock(&dev->io_mutex);
369 385
370 unlock_kernel(); 386
371 387
372 /* decrement our usage count */ 388 /* decrement our usage count */
373 kref_put(&dev->kref, skel_delete); 389 kref_put(&dev->kref, skel_delete);
@@ -380,6 +396,7 @@ static struct usb_driver skel_driver = {
380 .probe = skel_probe, 396 .probe = skel_probe,
381 .disconnect = skel_disconnect, 397 .disconnect = skel_disconnect,
382 .id_table = skel_table, 398 .id_table = skel_table,
399 .supports_autosuspend = 1,
383}; 400};
384 401
385static int __init usb_skel_init(void) 402static int __init usb_skel_init(void)
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 87dc75a6cee1..cfbd2bb8fa2c 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -299,8 +299,9 @@ struct usb_bus {
299 int bandwidth_int_reqs; /* number of Interrupt requests */ 299 int bandwidth_int_reqs; /* number of Interrupt requests */
300 int bandwidth_isoc_reqs; /* number of Isoc. requests */ 300 int bandwidth_isoc_reqs; /* number of Isoc. requests */
301 301
302#ifdef CONFIG_USB_DEVICEFS
302 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */ 303 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */
303 304#endif
304 struct class_device *class_dev; /* class device for this bus */ 305 struct class_device *class_dev; /* class device for this bus */
305 306
306#if defined(CONFIG_USB_MON) 307#if defined(CONFIG_USB_MON)
@@ -373,9 +374,12 @@ struct usb_device {
373 char *serial; /* iSerialNumber string, if present */ 374 char *serial; /* iSerialNumber string, if present */
374 375
375 struct list_head filelist; 376 struct list_head filelist;
376 struct device *usbfs_dev; 377#ifdef CONFIG_USB_DEVICE_CLASS
378 struct device *usb_classdev;
379#endif
380#ifdef CONFIG_USB_DEVICEFS
377 struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */ 381 struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */
378 382#endif
379 /* 383 /*
380 * Child devices - these can be either new devices 384 * Child devices - these can be either new devices
381 * (if this is a hub device), or different instances 385 * (if this is a hub device), or different instances
@@ -394,10 +398,13 @@ struct usb_device {
394 struct delayed_work autosuspend; /* for delayed autosuspends */ 398 struct delayed_work autosuspend; /* for delayed autosuspends */
395 struct mutex pm_mutex; /* protects PM operations */ 399 struct mutex pm_mutex; /* protects PM operations */
396 400
397 unsigned autosuspend_delay; /* in jiffies */ 401 unsigned long last_busy; /* time of last use */
402 int autosuspend_delay; /* in jiffies */
398 403
399 unsigned auto_pm:1; /* autosuspend/resume in progress */ 404 unsigned auto_pm:1; /* autosuspend/resume in progress */
400 unsigned do_remote_wakeup:1; /* remote wakeup should be enabled */ 405 unsigned do_remote_wakeup:1; /* remote wakeup should be enabled */
406 unsigned autosuspend_disabled:1; /* autosuspend and autoresume */
407 unsigned autoresume_disabled:1; /* disabled by the user */
401#endif 408#endif
402}; 409};
403#define to_usb_device(d) container_of(d, struct usb_device, dev) 410#define to_usb_device(d) container_of(d, struct usb_device, dev)
@@ -437,6 +444,11 @@ static inline void usb_autopm_disable(struct usb_interface *intf)
437 usb_autopm_set_interface(intf); 444 usb_autopm_set_interface(intf);
438} 445}
439 446
447static inline void usb_mark_last_busy(struct usb_device *udev)
448{
449 udev->last_busy = jiffies;
450}
451
440#else 452#else
441 453
442static inline int usb_autopm_set_interface(struct usb_interface *intf) 454static inline int usb_autopm_set_interface(struct usb_interface *intf)
@@ -451,6 +463,8 @@ static inline void usb_autopm_enable(struct usb_interface *intf)
451{ } 463{ }
452static inline void usb_autopm_disable(struct usb_interface *intf) 464static inline void usb_autopm_disable(struct usb_interface *intf)
453{ } 465{ }
466static inline void usb_mark_last_busy(struct usb_device *udev)
467{ }
454#endif 468#endif
455 469
456/*-------------------------------------------------------------------------*/ 470/*-------------------------------------------------------------------------*/
diff --git a/include/linux/usb/cdc.h b/include/linux/usb/cdc.h
index 956edf3bbecb..2204ae22c381 100644
--- a/include/linux/usb/cdc.h
+++ b/include/linux/usb/cdc.h
@@ -91,6 +91,17 @@ struct usb_cdc_union_desc {
91 /* ... and there could be other slave interfaces */ 91 /* ... and there could be other slave interfaces */
92} __attribute__ ((packed)); 92} __attribute__ ((packed));
93 93
94/* "Country Selection Functional Descriptor" from CDC spec 5.2.3.9 */
95struct usb_cdc_country_functional_desc {
96 __u8 bLength;
97 __u8 bDescriptorType;
98 __u8 bDescriptorSubType;
99
100 __u8 iCountryCodeRelDate;
101 __le16 wCountyCode0;
102 /* ... and there can be a lot of country codes */
103} __attribute__ ((packed));
104
94/* "Network Channel Terminal Functional Descriptor" from CDC spec 5.2.3.11 */ 105/* "Network Channel Terminal Functional Descriptor" from CDC spec 5.2.3.11 */
95struct usb_cdc_network_terminal_desc { 106struct usb_cdc_network_terminal_desc {
96 __u8 bLength; 107 __u8 bLength;
diff --git a/include/linux/usb/ch9.h b/include/linux/usb/ch9.h
index 1122a6c2c1a3..6169438ec5a2 100644
--- a/include/linux/usb/ch9.h
+++ b/include/linux/usb/ch9.h
@@ -181,12 +181,15 @@ struct usb_ctrlrequest {
181#define USB_DT_WIRE_ADAPTER 0x21 181#define USB_DT_WIRE_ADAPTER 0x21
182#define USB_DT_RPIPE 0x22 182#define USB_DT_RPIPE 0x22
183 183
184/* conventional codes for class-specific descriptors */ 184/* Conventional codes for class-specific descriptors. The convention is
185#define USB_DT_CS_DEVICE 0x21 185 * defined in the USB "Common Class" Spec (3.11). Individual class specs
186#define USB_DT_CS_CONFIG 0x22 186 * are authoritative for their usage, not the "common class" writeup.
187#define USB_DT_CS_STRING 0x23 187 */
188#define USB_DT_CS_INTERFACE 0x24 188#define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE)
189#define USB_DT_CS_ENDPOINT 0x25 189#define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG)
190#define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING)
191#define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE)
192#define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT)
190 193
191/* All standard descriptors have these 2 fields at the beginning */ 194/* All standard descriptors have these 2 fields at the beginning */
192struct usb_descriptor_header { 195struct usb_descriptor_header {