Commit 55c527187c9d78f840b284d596a0b298bc1493af

Authored by Alan Stern
Committed by Greg Kroah-Hartman
1 parent 949bf64311

[PATCH] USB: Consider power budget when choosing configuration

This patch (as609) changes the way we keep track of power budgeting for
USB hubs and devices, and it updates the choose_configuration routine to
take this information into account.  (This is something we should have
been doing all along.)  A new field in struct usb_device holds the amount
of bus current available from the upstream port, and the usb_hub structure
keeps track of the current available for each downstream port.

Two new rules for configuration selection are added:

	Don't select a self-powered configuration when only bus power
	is available.

	Don't select a configuration requiring more bus power than is
	available.

However the first rule is #if-ed out, because I found that the internal
hub in my HP USB keyboard claims that its only configuration is
self-powered.  The rule would prevent the configuration from being chosen,
leaving the hub & keyboard unconfigured.  Since similar descriptor errors
may turn out to be fairly common, it seemed wise not to include a rule
that would break automatic configuration unnecessarily for such devices.

The second rule may also trigger unnecessarily, although this should be
less common.  More likely it will annoy people by sometimes failing to
accept configurations that should never have been chosen in the first
place.

The patch also changes usbcore's reaction when no configuration is
suitable.  Instead of raising an error and rejecting the device, now
the core will simply leave the device unconfigured.  People can always
work around such problems by installing configurations manually through
sysfs.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 5 changed files with 164 additions and 81 deletions Inline Diff

drivers/usb/core/hcd.c
1 /* 1 /*
2 * (C) Copyright Linus Torvalds 1999 2 * (C) Copyright Linus Torvalds 1999
3 * (C) Copyright Johannes Erdfelt 1999-2001 3 * (C) Copyright Johannes Erdfelt 1999-2001
4 * (C) Copyright Andreas Gal 1999 4 * (C) Copyright Andreas Gal 1999
5 * (C) Copyright Gregory P. Smith 1999 5 * (C) Copyright Gregory P. Smith 1999
6 * (C) Copyright Deti Fliegl 1999 6 * (C) Copyright Deti Fliegl 1999
7 * (C) Copyright Randy Dunlap 2000 7 * (C) Copyright Randy Dunlap 2000
8 * (C) Copyright David Brownell 2000-2002 8 * (C) Copyright David Brownell 2000-2002
9 * 9 *
10 * This program is free software; you can redistribute it and/or modify it 10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the 11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your 12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. 13 * option) any later version.
14 * 14 *
15 * This program is distributed in the hope that it will be useful, but 15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details. 18 * for more details.
19 * 19 *
20 * You should have received a copy of the GNU General Public License 20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation, 21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */ 23 */
24 24
25 #include <linux/config.h> 25 #include <linux/config.h>
26 #include <linux/module.h> 26 #include <linux/module.h>
27 #include <linux/version.h> 27 #include <linux/version.h>
28 #include <linux/kernel.h> 28 #include <linux/kernel.h>
29 #include <linux/slab.h> 29 #include <linux/slab.h>
30 #include <linux/completion.h> 30 #include <linux/completion.h>
31 #include <linux/utsname.h> 31 #include <linux/utsname.h>
32 #include <linux/mm.h> 32 #include <linux/mm.h>
33 #include <asm/io.h> 33 #include <asm/io.h>
34 #include <asm/scatterlist.h> 34 #include <asm/scatterlist.h>
35 #include <linux/device.h> 35 #include <linux/device.h>
36 #include <linux/dma-mapping.h> 36 #include <linux/dma-mapping.h>
37 #include <asm/irq.h> 37 #include <asm/irq.h>
38 #include <asm/byteorder.h> 38 #include <asm/byteorder.h>
39 39
40 #include <linux/usb.h> 40 #include <linux/usb.h>
41 41
42 #include "usb.h" 42 #include "usb.h"
43 #include "hcd.h" 43 #include "hcd.h"
44 #include "hub.h" 44 #include "hub.h"
45 45
46 46
47 // #define USB_BANDWIDTH_MESSAGES 47 // #define USB_BANDWIDTH_MESSAGES
48 48
49 /*-------------------------------------------------------------------------*/ 49 /*-------------------------------------------------------------------------*/
50 50
51 /* 51 /*
52 * USB Host Controller Driver framework 52 * USB Host Controller Driver framework
53 * 53 *
54 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing 54 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
55 * HCD-specific behaviors/bugs. 55 * HCD-specific behaviors/bugs.
56 * 56 *
57 * This does error checks, tracks devices and urbs, and delegates to a 57 * This does error checks, tracks devices and urbs, and delegates to a
58 * "hc_driver" only for code (and data) that really needs to know about 58 * "hc_driver" only for code (and data) that really needs to know about
59 * hardware differences. That includes root hub registers, i/o queues, 59 * hardware differences. That includes root hub registers, i/o queues,
60 * and so on ... but as little else as possible. 60 * and so on ... but as little else as possible.
61 * 61 *
62 * Shared code includes most of the "root hub" code (these are emulated, 62 * Shared code includes most of the "root hub" code (these are emulated,
63 * though each HC's hardware works differently) and PCI glue, plus request 63 * though each HC's hardware works differently) and PCI glue, plus request
64 * tracking overhead. The HCD code should only block on spinlocks or on 64 * tracking overhead. The HCD code should only block on spinlocks or on
65 * hardware handshaking; blocking on software events (such as other kernel 65 * hardware handshaking; blocking on software events (such as other kernel
66 * threads releasing resources, or completing actions) is all generic. 66 * threads releasing resources, or completing actions) is all generic.
67 * 67 *
68 * Happens the USB 2.0 spec says this would be invisible inside the "USBD", 68 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
69 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used 69 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
70 * only by the hub driver ... and that neither should be seen or used by 70 * only by the hub driver ... and that neither should be seen or used by
71 * usb client device drivers. 71 * usb client device drivers.
72 * 72 *
73 * Contributors of ideas or unattributed patches include: David Brownell, 73 * Contributors of ideas or unattributed patches include: David Brownell,
74 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... 74 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
75 * 75 *
76 * HISTORY: 76 * HISTORY:
77 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some 77 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
78 * associated cleanup. "usb_hcd" still != "usb_bus". 78 * associated cleanup. "usb_hcd" still != "usb_bus".
79 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel. 79 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
80 */ 80 */
81 81
82 /*-------------------------------------------------------------------------*/ 82 /*-------------------------------------------------------------------------*/
83 83
84 /* host controllers we manage */ 84 /* host controllers we manage */
85 LIST_HEAD (usb_bus_list); 85 LIST_HEAD (usb_bus_list);
86 EXPORT_SYMBOL_GPL (usb_bus_list); 86 EXPORT_SYMBOL_GPL (usb_bus_list);
87 87
88 /* used when allocating bus numbers */ 88 /* used when allocating bus numbers */
89 #define USB_MAXBUS 64 89 #define USB_MAXBUS 64
90 struct usb_busmap { 90 struct usb_busmap {
91 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))]; 91 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
92 }; 92 };
93 static struct usb_busmap busmap; 93 static struct usb_busmap busmap;
94 94
95 /* used when updating list of hcds */ 95 /* used when updating list of hcds */
96 DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */ 96 DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
97 EXPORT_SYMBOL_GPL (usb_bus_list_lock); 97 EXPORT_SYMBOL_GPL (usb_bus_list_lock);
98 98
99 /* used for controlling access to virtual root hubs */ 99 /* used for controlling access to virtual root hubs */
100 static DEFINE_SPINLOCK(hcd_root_hub_lock); 100 static DEFINE_SPINLOCK(hcd_root_hub_lock);
101 101
102 /* used when updating hcd data */ 102 /* used when updating hcd data */
103 static DEFINE_SPINLOCK(hcd_data_lock); 103 static DEFINE_SPINLOCK(hcd_data_lock);
104 104
105 /* wait queue for synchronous unlinks */ 105 /* wait queue for synchronous unlinks */
106 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); 106 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
107 107
108 /*-------------------------------------------------------------------------*/ 108 /*-------------------------------------------------------------------------*/
109 109
110 /* 110 /*
111 * Sharable chunks of root hub code. 111 * Sharable chunks of root hub code.
112 */ 112 */
113 113
114 /*-------------------------------------------------------------------------*/ 114 /*-------------------------------------------------------------------------*/
115 115
116 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff) 116 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
117 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff) 117 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
118 118
119 /* usb 2.0 root hub device descriptor */ 119 /* usb 2.0 root hub device descriptor */
120 static const u8 usb2_rh_dev_descriptor [18] = { 120 static const u8 usb2_rh_dev_descriptor [18] = {
121 0x12, /* __u8 bLength; */ 121 0x12, /* __u8 bLength; */
122 0x01, /* __u8 bDescriptorType; Device */ 122 0x01, /* __u8 bDescriptorType; Device */
123 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ 123 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
124 124
125 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 125 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
126 0x00, /* __u8 bDeviceSubClass; */ 126 0x00, /* __u8 bDeviceSubClass; */
127 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/ 127 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
128 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ 128 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
129 129
130 0x00, 0x00, /* __le16 idVendor; */ 130 0x00, 0x00, /* __le16 idVendor; */
131 0x00, 0x00, /* __le16 idProduct; */ 131 0x00, 0x00, /* __le16 idProduct; */
132 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 132 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
133 133
134 0x03, /* __u8 iManufacturer; */ 134 0x03, /* __u8 iManufacturer; */
135 0x02, /* __u8 iProduct; */ 135 0x02, /* __u8 iProduct; */
136 0x01, /* __u8 iSerialNumber; */ 136 0x01, /* __u8 iSerialNumber; */
137 0x01 /* __u8 bNumConfigurations; */ 137 0x01 /* __u8 bNumConfigurations; */
138 }; 138 };
139 139
140 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ 140 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
141 141
142 /* usb 1.1 root hub device descriptor */ 142 /* usb 1.1 root hub device descriptor */
143 static const u8 usb11_rh_dev_descriptor [18] = { 143 static const u8 usb11_rh_dev_descriptor [18] = {
144 0x12, /* __u8 bLength; */ 144 0x12, /* __u8 bLength; */
145 0x01, /* __u8 bDescriptorType; Device */ 145 0x01, /* __u8 bDescriptorType; Device */
146 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ 146 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
147 147
148 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 148 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
149 0x00, /* __u8 bDeviceSubClass; */ 149 0x00, /* __u8 bDeviceSubClass; */
150 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ 150 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
151 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ 151 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
152 152
153 0x00, 0x00, /* __le16 idVendor; */ 153 0x00, 0x00, /* __le16 idVendor; */
154 0x00, 0x00, /* __le16 idProduct; */ 154 0x00, 0x00, /* __le16 idProduct; */
155 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 155 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
156 156
157 0x03, /* __u8 iManufacturer; */ 157 0x03, /* __u8 iManufacturer; */
158 0x02, /* __u8 iProduct; */ 158 0x02, /* __u8 iProduct; */
159 0x01, /* __u8 iSerialNumber; */ 159 0x01, /* __u8 iSerialNumber; */
160 0x01 /* __u8 bNumConfigurations; */ 160 0x01 /* __u8 bNumConfigurations; */
161 }; 161 };
162 162
163 163
164 /*-------------------------------------------------------------------------*/ 164 /*-------------------------------------------------------------------------*/
165 165
166 /* Configuration descriptors for our root hubs */ 166 /* Configuration descriptors for our root hubs */
167 167
168 static const u8 fs_rh_config_descriptor [] = { 168 static const u8 fs_rh_config_descriptor [] = {
169 169
170 /* one configuration */ 170 /* one configuration */
171 0x09, /* __u8 bLength; */ 171 0x09, /* __u8 bLength; */
172 0x02, /* __u8 bDescriptorType; Configuration */ 172 0x02, /* __u8 bDescriptorType; Configuration */
173 0x19, 0x00, /* __le16 wTotalLength; */ 173 0x19, 0x00, /* __le16 wTotalLength; */
174 0x01, /* __u8 bNumInterfaces; (1) */ 174 0x01, /* __u8 bNumInterfaces; (1) */
175 0x01, /* __u8 bConfigurationValue; */ 175 0x01, /* __u8 bConfigurationValue; */
176 0x00, /* __u8 iConfiguration; */ 176 0x00, /* __u8 iConfiguration; */
177 0xc0, /* __u8 bmAttributes; 177 0xc0, /* __u8 bmAttributes;
178 Bit 7: must be set, 178 Bit 7: must be set,
179 6: Self-powered, 179 6: Self-powered,
180 5: Remote wakeup, 180 5: Remote wakeup,
181 4..0: resvd */ 181 4..0: resvd */
182 0x00, /* __u8 MaxPower; */ 182 0x00, /* __u8 MaxPower; */
183 183
184 /* USB 1.1: 184 /* USB 1.1:
185 * USB 2.0, single TT organization (mandatory): 185 * USB 2.0, single TT organization (mandatory):
186 * one interface, protocol 0 186 * one interface, protocol 0
187 * 187 *
188 * USB 2.0, multiple TT organization (optional): 188 * USB 2.0, multiple TT organization (optional):
189 * two interfaces, protocols 1 (like single TT) 189 * two interfaces, protocols 1 (like single TT)
190 * and 2 (multiple TT mode) ... config is 190 * and 2 (multiple TT mode) ... config is
191 * sometimes settable 191 * sometimes settable
192 * NOT IMPLEMENTED 192 * NOT IMPLEMENTED
193 */ 193 */
194 194
195 /* one interface */ 195 /* one interface */
196 0x09, /* __u8 if_bLength; */ 196 0x09, /* __u8 if_bLength; */
197 0x04, /* __u8 if_bDescriptorType; Interface */ 197 0x04, /* __u8 if_bDescriptorType; Interface */
198 0x00, /* __u8 if_bInterfaceNumber; */ 198 0x00, /* __u8 if_bInterfaceNumber; */
199 0x00, /* __u8 if_bAlternateSetting; */ 199 0x00, /* __u8 if_bAlternateSetting; */
200 0x01, /* __u8 if_bNumEndpoints; */ 200 0x01, /* __u8 if_bNumEndpoints; */
201 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 201 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
202 0x00, /* __u8 if_bInterfaceSubClass; */ 202 0x00, /* __u8 if_bInterfaceSubClass; */
203 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ 203 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
204 0x00, /* __u8 if_iInterface; */ 204 0x00, /* __u8 if_iInterface; */
205 205
206 /* one endpoint (status change endpoint) */ 206 /* one endpoint (status change endpoint) */
207 0x07, /* __u8 ep_bLength; */ 207 0x07, /* __u8 ep_bLength; */
208 0x05, /* __u8 ep_bDescriptorType; Endpoint */ 208 0x05, /* __u8 ep_bDescriptorType; Endpoint */
209 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 209 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
210 0x03, /* __u8 ep_bmAttributes; Interrupt */ 210 0x03, /* __u8 ep_bmAttributes; Interrupt */
211 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ 211 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
212 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ 212 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
213 }; 213 };
214 214
215 static const u8 hs_rh_config_descriptor [] = { 215 static const u8 hs_rh_config_descriptor [] = {
216 216
217 /* one configuration */ 217 /* one configuration */
218 0x09, /* __u8 bLength; */ 218 0x09, /* __u8 bLength; */
219 0x02, /* __u8 bDescriptorType; Configuration */ 219 0x02, /* __u8 bDescriptorType; Configuration */
220 0x19, 0x00, /* __le16 wTotalLength; */ 220 0x19, 0x00, /* __le16 wTotalLength; */
221 0x01, /* __u8 bNumInterfaces; (1) */ 221 0x01, /* __u8 bNumInterfaces; (1) */
222 0x01, /* __u8 bConfigurationValue; */ 222 0x01, /* __u8 bConfigurationValue; */
223 0x00, /* __u8 iConfiguration; */ 223 0x00, /* __u8 iConfiguration; */
224 0xc0, /* __u8 bmAttributes; 224 0xc0, /* __u8 bmAttributes;
225 Bit 7: must be set, 225 Bit 7: must be set,
226 6: Self-powered, 226 6: Self-powered,
227 5: Remote wakeup, 227 5: Remote wakeup,
228 4..0: resvd */ 228 4..0: resvd */
229 0x00, /* __u8 MaxPower; */ 229 0x00, /* __u8 MaxPower; */
230 230
231 /* USB 1.1: 231 /* USB 1.1:
232 * USB 2.0, single TT organization (mandatory): 232 * USB 2.0, single TT organization (mandatory):
233 * one interface, protocol 0 233 * one interface, protocol 0
234 * 234 *
235 * USB 2.0, multiple TT organization (optional): 235 * USB 2.0, multiple TT organization (optional):
236 * two interfaces, protocols 1 (like single TT) 236 * two interfaces, protocols 1 (like single TT)
237 * and 2 (multiple TT mode) ... config is 237 * and 2 (multiple TT mode) ... config is
238 * sometimes settable 238 * sometimes settable
239 * NOT IMPLEMENTED 239 * NOT IMPLEMENTED
240 */ 240 */
241 241
242 /* one interface */ 242 /* one interface */
243 0x09, /* __u8 if_bLength; */ 243 0x09, /* __u8 if_bLength; */
244 0x04, /* __u8 if_bDescriptorType; Interface */ 244 0x04, /* __u8 if_bDescriptorType; Interface */
245 0x00, /* __u8 if_bInterfaceNumber; */ 245 0x00, /* __u8 if_bInterfaceNumber; */
246 0x00, /* __u8 if_bAlternateSetting; */ 246 0x00, /* __u8 if_bAlternateSetting; */
247 0x01, /* __u8 if_bNumEndpoints; */ 247 0x01, /* __u8 if_bNumEndpoints; */
248 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 248 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
249 0x00, /* __u8 if_bInterfaceSubClass; */ 249 0x00, /* __u8 if_bInterfaceSubClass; */
250 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ 250 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
251 0x00, /* __u8 if_iInterface; */ 251 0x00, /* __u8 if_iInterface; */
252 252
253 /* one endpoint (status change endpoint) */ 253 /* one endpoint (status change endpoint) */
254 0x07, /* __u8 ep_bLength; */ 254 0x07, /* __u8 ep_bLength; */
255 0x05, /* __u8 ep_bDescriptorType; Endpoint */ 255 0x05, /* __u8 ep_bDescriptorType; Endpoint */
256 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 256 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
257 0x03, /* __u8 ep_bmAttributes; Interrupt */ 257 0x03, /* __u8 ep_bmAttributes; Interrupt */
258 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ 258 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
259 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ 259 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
260 }; 260 };
261 261
262 /*-------------------------------------------------------------------------*/ 262 /*-------------------------------------------------------------------------*/
263 263
264 /* 264 /*
265 * helper routine for returning string descriptors in UTF-16LE 265 * helper routine for returning string descriptors in UTF-16LE
266 * input can actually be ISO-8859-1; ASCII is its 7-bit subset 266 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
267 */ 267 */
268 static int ascii2utf (char *s, u8 *utf, int utfmax) 268 static int ascii2utf (char *s, u8 *utf, int utfmax)
269 { 269 {
270 int retval; 270 int retval;
271 271
272 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) { 272 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
273 *utf++ = *s++; 273 *utf++ = *s++;
274 *utf++ = 0; 274 *utf++ = 0;
275 } 275 }
276 if (utfmax > 0) { 276 if (utfmax > 0) {
277 *utf = *s; 277 *utf = *s;
278 ++retval; 278 ++retval;
279 } 279 }
280 return retval; 280 return retval;
281 } 281 }
282 282
283 /* 283 /*
284 * rh_string - provides manufacturer, product and serial strings for root hub 284 * rh_string - provides manufacturer, product and serial strings for root hub
285 * @id: the string ID number (1: serial number, 2: product, 3: vendor) 285 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
286 * @hcd: the host controller for this root hub 286 * @hcd: the host controller for this root hub
287 * @type: string describing our driver 287 * @type: string describing our driver
288 * @data: return packet in UTF-16 LE 288 * @data: return packet in UTF-16 LE
289 * @len: length of the return packet 289 * @len: length of the return packet
290 * 290 *
291 * Produces either a manufacturer, product or serial number string for the 291 * Produces either a manufacturer, product or serial number string for the
292 * virtual root hub device. 292 * virtual root hub device.
293 */ 293 */
294 static int rh_string ( 294 static int rh_string (
295 int id, 295 int id,
296 struct usb_hcd *hcd, 296 struct usb_hcd *hcd,
297 u8 *data, 297 u8 *data,
298 int len 298 int len
299 ) { 299 ) {
300 char buf [100]; 300 char buf [100];
301 301
302 // language ids 302 // language ids
303 if (id == 0) { 303 if (id == 0) {
304 buf[0] = 4; buf[1] = 3; /* 4 bytes string data */ 304 buf[0] = 4; buf[1] = 3; /* 4 bytes string data */
305 buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */ 305 buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */
306 len = min (len, 4); 306 len = min (len, 4);
307 memcpy (data, buf, len); 307 memcpy (data, buf, len);
308 return len; 308 return len;
309 309
310 // serial number 310 // serial number
311 } else if (id == 1) { 311 } else if (id == 1) {
312 strlcpy (buf, hcd->self.bus_name, sizeof buf); 312 strlcpy (buf, hcd->self.bus_name, sizeof buf);
313 313
314 // product description 314 // product description
315 } else if (id == 2) { 315 } else if (id == 2) {
316 strlcpy (buf, hcd->product_desc, sizeof buf); 316 strlcpy (buf, hcd->product_desc, sizeof buf);
317 317
318 // id 3 == vendor description 318 // id 3 == vendor description
319 } else if (id == 3) { 319 } else if (id == 3) {
320 snprintf (buf, sizeof buf, "%s %s %s", system_utsname.sysname, 320 snprintf (buf, sizeof buf, "%s %s %s", system_utsname.sysname,
321 system_utsname.release, hcd->driver->description); 321 system_utsname.release, hcd->driver->description);
322 322
323 // unsupported IDs --> "protocol stall" 323 // unsupported IDs --> "protocol stall"
324 } else 324 } else
325 return -EPIPE; 325 return -EPIPE;
326 326
327 switch (len) { /* All cases fall through */ 327 switch (len) { /* All cases fall through */
328 default: 328 default:
329 len = 2 + ascii2utf (buf, data + 2, len - 2); 329 len = 2 + ascii2utf (buf, data + 2, len - 2);
330 case 2: 330 case 2:
331 data [1] = 3; /* type == string */ 331 data [1] = 3; /* type == string */
332 case 1: 332 case 1:
333 data [0] = 2 * (strlen (buf) + 1); 333 data [0] = 2 * (strlen (buf) + 1);
334 case 0: 334 case 0:
335 ; /* Compiler wants a statement here */ 335 ; /* Compiler wants a statement here */
336 } 336 }
337 return len; 337 return len;
338 } 338 }
339 339
340 340
341 /* Root hub control transfers execute synchronously */ 341 /* Root hub control transfers execute synchronously */
342 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) 342 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
343 { 343 {
344 struct usb_ctrlrequest *cmd; 344 struct usb_ctrlrequest *cmd;
345 u16 typeReq, wValue, wIndex, wLength; 345 u16 typeReq, wValue, wIndex, wLength;
346 u8 *ubuf = urb->transfer_buffer; 346 u8 *ubuf = urb->transfer_buffer;
347 u8 tbuf [sizeof (struct usb_hub_descriptor)]; 347 u8 tbuf [sizeof (struct usb_hub_descriptor)];
348 const u8 *bufp = tbuf; 348 const u8 *bufp = tbuf;
349 int len = 0; 349 int len = 0;
350 int patch_wakeup = 0; 350 int patch_wakeup = 0;
351 unsigned long flags; 351 unsigned long flags;
352 int status = 0; 352 int status = 0;
353 int n; 353 int n;
354 354
355 cmd = (struct usb_ctrlrequest *) urb->setup_packet; 355 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
356 typeReq = (cmd->bRequestType << 8) | cmd->bRequest; 356 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
357 wValue = le16_to_cpu (cmd->wValue); 357 wValue = le16_to_cpu (cmd->wValue);
358 wIndex = le16_to_cpu (cmd->wIndex); 358 wIndex = le16_to_cpu (cmd->wIndex);
359 wLength = le16_to_cpu (cmd->wLength); 359 wLength = le16_to_cpu (cmd->wLength);
360 360
361 if (wLength > urb->transfer_buffer_length) 361 if (wLength > urb->transfer_buffer_length)
362 goto error; 362 goto error;
363 363
364 urb->actual_length = 0; 364 urb->actual_length = 0;
365 switch (typeReq) { 365 switch (typeReq) {
366 366
367 /* DEVICE REQUESTS */ 367 /* DEVICE REQUESTS */
368 368
369 case DeviceRequest | USB_REQ_GET_STATUS: 369 case DeviceRequest | USB_REQ_GET_STATUS:
370 tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP) 370 tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
371 | (1 << USB_DEVICE_SELF_POWERED); 371 | (1 << USB_DEVICE_SELF_POWERED);
372 tbuf [1] = 0; 372 tbuf [1] = 0;
373 len = 2; 373 len = 2;
374 break; 374 break;
375 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 375 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
376 if (wValue == USB_DEVICE_REMOTE_WAKEUP) 376 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
377 hcd->remote_wakeup = 0; 377 hcd->remote_wakeup = 0;
378 else 378 else
379 goto error; 379 goto error;
380 break; 380 break;
381 case DeviceOutRequest | USB_REQ_SET_FEATURE: 381 case DeviceOutRequest | USB_REQ_SET_FEATURE:
382 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP) 382 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
383 hcd->remote_wakeup = 1; 383 hcd->remote_wakeup = 1;
384 else 384 else
385 goto error; 385 goto error;
386 break; 386 break;
387 case DeviceRequest | USB_REQ_GET_CONFIGURATION: 387 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
388 tbuf [0] = 1; 388 tbuf [0] = 1;
389 len = 1; 389 len = 1;
390 /* FALLTHROUGH */ 390 /* FALLTHROUGH */
391 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 391 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
392 break; 392 break;
393 case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 393 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
394 switch (wValue & 0xff00) { 394 switch (wValue & 0xff00) {
395 case USB_DT_DEVICE << 8: 395 case USB_DT_DEVICE << 8:
396 if (hcd->driver->flags & HCD_USB2) 396 if (hcd->driver->flags & HCD_USB2)
397 bufp = usb2_rh_dev_descriptor; 397 bufp = usb2_rh_dev_descriptor;
398 else if (hcd->driver->flags & HCD_USB11) 398 else if (hcd->driver->flags & HCD_USB11)
399 bufp = usb11_rh_dev_descriptor; 399 bufp = usb11_rh_dev_descriptor;
400 else 400 else
401 goto error; 401 goto error;
402 len = 18; 402 len = 18;
403 break; 403 break;
404 case USB_DT_CONFIG << 8: 404 case USB_DT_CONFIG << 8:
405 if (hcd->driver->flags & HCD_USB2) { 405 if (hcd->driver->flags & HCD_USB2) {
406 bufp = hs_rh_config_descriptor; 406 bufp = hs_rh_config_descriptor;
407 len = sizeof hs_rh_config_descriptor; 407 len = sizeof hs_rh_config_descriptor;
408 } else { 408 } else {
409 bufp = fs_rh_config_descriptor; 409 bufp = fs_rh_config_descriptor;
410 len = sizeof fs_rh_config_descriptor; 410 len = sizeof fs_rh_config_descriptor;
411 } 411 }
412 if (hcd->can_wakeup) 412 if (hcd->can_wakeup)
413 patch_wakeup = 1; 413 patch_wakeup = 1;
414 break; 414 break;
415 case USB_DT_STRING << 8: 415 case USB_DT_STRING << 8:
416 n = rh_string (wValue & 0xff, hcd, ubuf, wLength); 416 n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
417 if (n < 0) 417 if (n < 0)
418 goto error; 418 goto error;
419 urb->actual_length = n; 419 urb->actual_length = n;
420 break; 420 break;
421 default: 421 default:
422 goto error; 422 goto error;
423 } 423 }
424 break; 424 break;
425 case DeviceRequest | USB_REQ_GET_INTERFACE: 425 case DeviceRequest | USB_REQ_GET_INTERFACE:
426 tbuf [0] = 0; 426 tbuf [0] = 0;
427 len = 1; 427 len = 1;
428 /* FALLTHROUGH */ 428 /* FALLTHROUGH */
429 case DeviceOutRequest | USB_REQ_SET_INTERFACE: 429 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
430 break; 430 break;
431 case DeviceOutRequest | USB_REQ_SET_ADDRESS: 431 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
432 // wValue == urb->dev->devaddr 432 // wValue == urb->dev->devaddr
433 dev_dbg (hcd->self.controller, "root hub device address %d\n", 433 dev_dbg (hcd->self.controller, "root hub device address %d\n",
434 wValue); 434 wValue);
435 break; 435 break;
436 436
437 /* INTERFACE REQUESTS (no defined feature/status flags) */ 437 /* INTERFACE REQUESTS (no defined feature/status flags) */
438 438
439 /* ENDPOINT REQUESTS */ 439 /* ENDPOINT REQUESTS */
440 440
441 case EndpointRequest | USB_REQ_GET_STATUS: 441 case EndpointRequest | USB_REQ_GET_STATUS:
442 // ENDPOINT_HALT flag 442 // ENDPOINT_HALT flag
443 tbuf [0] = 0; 443 tbuf [0] = 0;
444 tbuf [1] = 0; 444 tbuf [1] = 0;
445 len = 2; 445 len = 2;
446 /* FALLTHROUGH */ 446 /* FALLTHROUGH */
447 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: 447 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
448 case EndpointOutRequest | USB_REQ_SET_FEATURE: 448 case EndpointOutRequest | USB_REQ_SET_FEATURE:
449 dev_dbg (hcd->self.controller, "no endpoint features yet\n"); 449 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
450 break; 450 break;
451 451
452 /* CLASS REQUESTS (and errors) */ 452 /* CLASS REQUESTS (and errors) */
453 453
454 default: 454 default:
455 /* non-generic request */ 455 /* non-generic request */
456 switch (typeReq) { 456 switch (typeReq) {
457 case GetHubStatus: 457 case GetHubStatus:
458 case GetPortStatus: 458 case GetPortStatus:
459 len = 4; 459 len = 4;
460 break; 460 break;
461 case GetHubDescriptor: 461 case GetHubDescriptor:
462 len = sizeof (struct usb_hub_descriptor); 462 len = sizeof (struct usb_hub_descriptor);
463 break; 463 break;
464 } 464 }
465 status = hcd->driver->hub_control (hcd, 465 status = hcd->driver->hub_control (hcd,
466 typeReq, wValue, wIndex, 466 typeReq, wValue, wIndex,
467 tbuf, wLength); 467 tbuf, wLength);
468 break; 468 break;
469 error: 469 error:
470 /* "protocol stall" on error */ 470 /* "protocol stall" on error */
471 status = -EPIPE; 471 status = -EPIPE;
472 } 472 }
473 473
474 if (status) { 474 if (status) {
475 len = 0; 475 len = 0;
476 if (status != -EPIPE) { 476 if (status != -EPIPE) {
477 dev_dbg (hcd->self.controller, 477 dev_dbg (hcd->self.controller,
478 "CTRL: TypeReq=0x%x val=0x%x " 478 "CTRL: TypeReq=0x%x val=0x%x "
479 "idx=0x%x len=%d ==> %d\n", 479 "idx=0x%x len=%d ==> %d\n",
480 typeReq, wValue, wIndex, 480 typeReq, wValue, wIndex,
481 wLength, status); 481 wLength, status);
482 } 482 }
483 } 483 }
484 if (len) { 484 if (len) {
485 if (urb->transfer_buffer_length < len) 485 if (urb->transfer_buffer_length < len)
486 len = urb->transfer_buffer_length; 486 len = urb->transfer_buffer_length;
487 urb->actual_length = len; 487 urb->actual_length = len;
488 // always USB_DIR_IN, toward host 488 // always USB_DIR_IN, toward host
489 memcpy (ubuf, bufp, len); 489 memcpy (ubuf, bufp, len);
490 490
491 /* report whether RH hardware supports remote wakeup */ 491 /* report whether RH hardware supports remote wakeup */
492 if (patch_wakeup && 492 if (patch_wakeup &&
493 len > offsetof (struct usb_config_descriptor, 493 len > offsetof (struct usb_config_descriptor,
494 bmAttributes)) 494 bmAttributes))
495 ((struct usb_config_descriptor *)ubuf)->bmAttributes 495 ((struct usb_config_descriptor *)ubuf)->bmAttributes
496 |= USB_CONFIG_ATT_WAKEUP; 496 |= USB_CONFIG_ATT_WAKEUP;
497 } 497 }
498 498
499 /* any errors get returned through the urb completion */ 499 /* any errors get returned through the urb completion */
500 local_irq_save (flags); 500 local_irq_save (flags);
501 spin_lock (&urb->lock); 501 spin_lock (&urb->lock);
502 if (urb->status == -EINPROGRESS) 502 if (urb->status == -EINPROGRESS)
503 urb->status = status; 503 urb->status = status;
504 spin_unlock (&urb->lock); 504 spin_unlock (&urb->lock);
505 usb_hcd_giveback_urb (hcd, urb, NULL); 505 usb_hcd_giveback_urb (hcd, urb, NULL);
506 local_irq_restore (flags); 506 local_irq_restore (flags);
507 return 0; 507 return 0;
508 } 508 }
509 509
510 /*-------------------------------------------------------------------------*/ 510 /*-------------------------------------------------------------------------*/
511 511
512 /* 512 /*
513 * Root Hub interrupt transfers are polled using a timer if the 513 * Root Hub interrupt transfers are polled using a timer if the
514 * driver requests it; otherwise the driver is responsible for 514 * driver requests it; otherwise the driver is responsible for
515 * calling usb_hcd_poll_rh_status() when an event occurs. 515 * calling usb_hcd_poll_rh_status() when an event occurs.
516 * 516 *
517 * Completions are called in_interrupt(), but they may or may not 517 * Completions are called in_interrupt(), but they may or may not
518 * be in_irq(). 518 * be in_irq().
519 */ 519 */
520 void usb_hcd_poll_rh_status(struct usb_hcd *hcd) 520 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
521 { 521 {
522 struct urb *urb; 522 struct urb *urb;
523 int length; 523 int length;
524 unsigned long flags; 524 unsigned long flags;
525 char buffer[4]; /* Any root hubs with > 31 ports? */ 525 char buffer[4]; /* Any root hubs with > 31 ports? */
526 526
527 if (!hcd->uses_new_polling && !hcd->status_urb) 527 if (!hcd->uses_new_polling && !hcd->status_urb)
528 return; 528 return;
529 529
530 length = hcd->driver->hub_status_data(hcd, buffer); 530 length = hcd->driver->hub_status_data(hcd, buffer);
531 if (length > 0) { 531 if (length > 0) {
532 532
533 /* try to complete the status urb */ 533 /* try to complete the status urb */
534 local_irq_save (flags); 534 local_irq_save (flags);
535 spin_lock(&hcd_root_hub_lock); 535 spin_lock(&hcd_root_hub_lock);
536 urb = hcd->status_urb; 536 urb = hcd->status_urb;
537 if (urb) { 537 if (urb) {
538 spin_lock(&urb->lock); 538 spin_lock(&urb->lock);
539 if (urb->status == -EINPROGRESS) { 539 if (urb->status == -EINPROGRESS) {
540 hcd->poll_pending = 0; 540 hcd->poll_pending = 0;
541 hcd->status_urb = NULL; 541 hcd->status_urb = NULL;
542 urb->status = 0; 542 urb->status = 0;
543 urb->hcpriv = NULL; 543 urb->hcpriv = NULL;
544 urb->actual_length = length; 544 urb->actual_length = length;
545 memcpy(urb->transfer_buffer, buffer, length); 545 memcpy(urb->transfer_buffer, buffer, length);
546 } else /* urb has been unlinked */ 546 } else /* urb has been unlinked */
547 length = 0; 547 length = 0;
548 spin_unlock(&urb->lock); 548 spin_unlock(&urb->lock);
549 } else 549 } else
550 length = 0; 550 length = 0;
551 spin_unlock(&hcd_root_hub_lock); 551 spin_unlock(&hcd_root_hub_lock);
552 552
553 /* local irqs are always blocked in completions */ 553 /* local irqs are always blocked in completions */
554 if (length > 0) 554 if (length > 0)
555 usb_hcd_giveback_urb (hcd, urb, NULL); 555 usb_hcd_giveback_urb (hcd, urb, NULL);
556 else 556 else
557 hcd->poll_pending = 1; 557 hcd->poll_pending = 1;
558 local_irq_restore (flags); 558 local_irq_restore (flags);
559 } 559 }
560 560
561 /* The USB 2.0 spec says 256 ms. This is close enough and won't 561 /* The USB 2.0 spec says 256 ms. This is close enough and won't
562 * exceed that limit if HZ is 100. */ 562 * exceed that limit if HZ is 100. */
563 if (hcd->uses_new_polling ? hcd->poll_rh : 563 if (hcd->uses_new_polling ? hcd->poll_rh :
564 (length == 0 && hcd->status_urb != NULL)) 564 (length == 0 && hcd->status_urb != NULL))
565 mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250)); 565 mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
566 } 566 }
567 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); 567 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
568 568
569 /* timer callback */ 569 /* timer callback */
570 static void rh_timer_func (unsigned long _hcd) 570 static void rh_timer_func (unsigned long _hcd)
571 { 571 {
572 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); 572 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
573 } 573 }
574 574
575 /*-------------------------------------------------------------------------*/ 575 /*-------------------------------------------------------------------------*/
576 576
577 static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) 577 static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
578 { 578 {
579 int retval; 579 int retval;
580 unsigned long flags; 580 unsigned long flags;
581 int len = 1 + (urb->dev->maxchild / 8); 581 int len = 1 + (urb->dev->maxchild / 8);
582 582
583 spin_lock_irqsave (&hcd_root_hub_lock, flags); 583 spin_lock_irqsave (&hcd_root_hub_lock, flags);
584 if (urb->status != -EINPROGRESS) /* already unlinked */ 584 if (urb->status != -EINPROGRESS) /* already unlinked */
585 retval = urb->status; 585 retval = urb->status;
586 else if (hcd->status_urb || urb->transfer_buffer_length < len) { 586 else if (hcd->status_urb || urb->transfer_buffer_length < len) {
587 dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); 587 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
588 retval = -EINVAL; 588 retval = -EINVAL;
589 } else { 589 } else {
590 hcd->status_urb = urb; 590 hcd->status_urb = urb;
591 urb->hcpriv = hcd; /* indicate it's queued */ 591 urb->hcpriv = hcd; /* indicate it's queued */
592 592
593 if (!hcd->uses_new_polling) 593 if (!hcd->uses_new_polling)
594 mod_timer (&hcd->rh_timer, jiffies + 594 mod_timer (&hcd->rh_timer, jiffies +
595 msecs_to_jiffies(250)); 595 msecs_to_jiffies(250));
596 596
597 /* If a status change has already occurred, report it ASAP */ 597 /* If a status change has already occurred, report it ASAP */
598 else if (hcd->poll_pending) 598 else if (hcd->poll_pending)
599 mod_timer (&hcd->rh_timer, jiffies); 599 mod_timer (&hcd->rh_timer, jiffies);
600 retval = 0; 600 retval = 0;
601 } 601 }
602 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 602 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
603 return retval; 603 return retval;
604 } 604 }
605 605
606 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) 606 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
607 { 607 {
608 if (usb_pipeint (urb->pipe)) 608 if (usb_pipeint (urb->pipe))
609 return rh_queue_status (hcd, urb); 609 return rh_queue_status (hcd, urb);
610 if (usb_pipecontrol (urb->pipe)) 610 if (usb_pipecontrol (urb->pipe))
611 return rh_call_control (hcd, urb); 611 return rh_call_control (hcd, urb);
612 return -EINVAL; 612 return -EINVAL;
613 } 613 }
614 614
615 /*-------------------------------------------------------------------------*/ 615 /*-------------------------------------------------------------------------*/
616 616
617 /* Asynchronous unlinks of root-hub control URBs are legal, but they 617 /* Asynchronous unlinks of root-hub control URBs are legal, but they
618 * don't do anything. Status URB unlinks must be made in process context 618 * don't do anything. Status URB unlinks must be made in process context
619 * with interrupts enabled. 619 * with interrupts enabled.
620 */ 620 */
621 static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb) 621 static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
622 { 622 {
623 if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */ 623 if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
624 if (in_interrupt()) 624 if (in_interrupt())
625 return 0; /* nothing to do */ 625 return 0; /* nothing to do */
626 626
627 spin_lock_irq(&urb->lock); /* from usb_kill_urb */ 627 spin_lock_irq(&urb->lock); /* from usb_kill_urb */
628 ++urb->reject; 628 ++urb->reject;
629 spin_unlock_irq(&urb->lock); 629 spin_unlock_irq(&urb->lock);
630 630
631 wait_event(usb_kill_urb_queue, 631 wait_event(usb_kill_urb_queue,
632 atomic_read(&urb->use_count) == 0); 632 atomic_read(&urb->use_count) == 0);
633 633
634 spin_lock_irq(&urb->lock); 634 spin_lock_irq(&urb->lock);
635 --urb->reject; 635 --urb->reject;
636 spin_unlock_irq(&urb->lock); 636 spin_unlock_irq(&urb->lock);
637 637
638 } else { /* Status URB */ 638 } else { /* Status URB */
639 if (!hcd->uses_new_polling) 639 if (!hcd->uses_new_polling)
640 del_timer_sync (&hcd->rh_timer); 640 del_timer_sync (&hcd->rh_timer);
641 local_irq_disable (); 641 local_irq_disable ();
642 spin_lock (&hcd_root_hub_lock); 642 spin_lock (&hcd_root_hub_lock);
643 if (urb == hcd->status_urb) { 643 if (urb == hcd->status_urb) {
644 hcd->status_urb = NULL; 644 hcd->status_urb = NULL;
645 urb->hcpriv = NULL; 645 urb->hcpriv = NULL;
646 } else 646 } else
647 urb = NULL; /* wasn't fully queued */ 647 urb = NULL; /* wasn't fully queued */
648 spin_unlock (&hcd_root_hub_lock); 648 spin_unlock (&hcd_root_hub_lock);
649 if (urb) 649 if (urb)
650 usb_hcd_giveback_urb (hcd, urb, NULL); 650 usb_hcd_giveback_urb (hcd, urb, NULL);
651 local_irq_enable (); 651 local_irq_enable ();
652 } 652 }
653 653
654 return 0; 654 return 0;
655 } 655 }
656 656
657 /*-------------------------------------------------------------------------*/ 657 /*-------------------------------------------------------------------------*/
658 658
659 /* exported only within usbcore */ 659 /* exported only within usbcore */
660 struct usb_bus *usb_bus_get(struct usb_bus *bus) 660 struct usb_bus *usb_bus_get(struct usb_bus *bus)
661 { 661 {
662 if (bus) 662 if (bus)
663 kref_get(&bus->kref); 663 kref_get(&bus->kref);
664 return bus; 664 return bus;
665 } 665 }
666 666
667 static void usb_host_release(struct kref *kref) 667 static void usb_host_release(struct kref *kref)
668 { 668 {
669 struct usb_bus *bus = container_of(kref, struct usb_bus, kref); 669 struct usb_bus *bus = container_of(kref, struct usb_bus, kref);
670 670
671 if (bus->release) 671 if (bus->release)
672 bus->release(bus); 672 bus->release(bus);
673 } 673 }
674 674
675 /* exported only within usbcore */ 675 /* exported only within usbcore */
676 void usb_bus_put(struct usb_bus *bus) 676 void usb_bus_put(struct usb_bus *bus)
677 { 677 {
678 if (bus) 678 if (bus)
679 kref_put(&bus->kref, usb_host_release); 679 kref_put(&bus->kref, usb_host_release);
680 } 680 }
681 681
682 /*-------------------------------------------------------------------------*/ 682 /*-------------------------------------------------------------------------*/
683 683
684 static struct class *usb_host_class; 684 static struct class *usb_host_class;
685 685
686 int usb_host_init(void) 686 int usb_host_init(void)
687 { 687 {
688 int retval = 0; 688 int retval = 0;
689 689
690 usb_host_class = class_create(THIS_MODULE, "usb_host"); 690 usb_host_class = class_create(THIS_MODULE, "usb_host");
691 if (IS_ERR(usb_host_class)) 691 if (IS_ERR(usb_host_class))
692 retval = PTR_ERR(usb_host_class); 692 retval = PTR_ERR(usb_host_class);
693 return retval; 693 return retval;
694 } 694 }
695 695
696 void usb_host_cleanup(void) 696 void usb_host_cleanup(void)
697 { 697 {
698 class_destroy(usb_host_class); 698 class_destroy(usb_host_class);
699 } 699 }
700 700
701 /** 701 /**
702 * usb_bus_init - shared initialization code 702 * usb_bus_init - shared initialization code
703 * @bus: the bus structure being initialized 703 * @bus: the bus structure being initialized
704 * 704 *
705 * This code is used to initialize a usb_bus structure, memory for which is 705 * This code is used to initialize a usb_bus structure, memory for which is
706 * separately managed. 706 * separately managed.
707 */ 707 */
708 static void usb_bus_init (struct usb_bus *bus) 708 static void usb_bus_init (struct usb_bus *bus)
709 { 709 {
710 memset (&bus->devmap, 0, sizeof(struct usb_devmap)); 710 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
711 711
712 bus->devnum_next = 1; 712 bus->devnum_next = 1;
713 713
714 bus->root_hub = NULL; 714 bus->root_hub = NULL;
715 bus->hcpriv = NULL; 715 bus->hcpriv = NULL;
716 bus->busnum = -1; 716 bus->busnum = -1;
717 bus->bandwidth_allocated = 0; 717 bus->bandwidth_allocated = 0;
718 bus->bandwidth_int_reqs = 0; 718 bus->bandwidth_int_reqs = 0;
719 bus->bandwidth_isoc_reqs = 0; 719 bus->bandwidth_isoc_reqs = 0;
720 720
721 INIT_LIST_HEAD (&bus->bus_list); 721 INIT_LIST_HEAD (&bus->bus_list);
722 722
723 kref_init(&bus->kref); 723 kref_init(&bus->kref);
724 } 724 }
725 725
726 /** 726 /**
727 * usb_alloc_bus - creates a new USB host controller structure 727 * usb_alloc_bus - creates a new USB host controller structure
728 * @op: pointer to a struct usb_operations that this bus structure should use 728 * @op: pointer to a struct usb_operations that this bus structure should use
729 * Context: !in_interrupt() 729 * Context: !in_interrupt()
730 * 730 *
731 * Creates a USB host controller bus structure with the specified 731 * Creates a USB host controller bus structure with the specified
732 * usb_operations and initializes all the necessary internal objects. 732 * usb_operations and initializes all the necessary internal objects.
733 * 733 *
734 * If no memory is available, NULL is returned. 734 * If no memory is available, NULL is returned.
735 * 735 *
736 * The caller should call usb_put_bus() when it is finished with the structure. 736 * The caller should call usb_put_bus() when it is finished with the structure.
737 */ 737 */
738 struct usb_bus *usb_alloc_bus (struct usb_operations *op) 738 struct usb_bus *usb_alloc_bus (struct usb_operations *op)
739 { 739 {
740 struct usb_bus *bus; 740 struct usb_bus *bus;
741 741
742 bus = kzalloc (sizeof *bus, GFP_KERNEL); 742 bus = kzalloc (sizeof *bus, GFP_KERNEL);
743 if (!bus) 743 if (!bus)
744 return NULL; 744 return NULL;
745 usb_bus_init (bus); 745 usb_bus_init (bus);
746 bus->op = op; 746 bus->op = op;
747 return bus; 747 return bus;
748 } 748 }
749 749
750 /*-------------------------------------------------------------------------*/ 750 /*-------------------------------------------------------------------------*/
751 751
752 /** 752 /**
753 * usb_register_bus - registers the USB host controller with the usb core 753 * usb_register_bus - registers the USB host controller with the usb core
754 * @bus: pointer to the bus to register 754 * @bus: pointer to the bus to register
755 * Context: !in_interrupt() 755 * Context: !in_interrupt()
756 * 756 *
757 * Assigns a bus number, and links the controller into usbcore data 757 * Assigns a bus number, and links the controller into usbcore data
758 * structures so that it can be seen by scanning the bus list. 758 * structures so that it can be seen by scanning the bus list.
759 */ 759 */
760 static int usb_register_bus(struct usb_bus *bus) 760 static int usb_register_bus(struct usb_bus *bus)
761 { 761 {
762 int busnum; 762 int busnum;
763 763
764 down (&usb_bus_list_lock); 764 down (&usb_bus_list_lock);
765 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); 765 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
766 if (busnum < USB_MAXBUS) { 766 if (busnum < USB_MAXBUS) {
767 set_bit (busnum, busmap.busmap); 767 set_bit (busnum, busmap.busmap);
768 bus->busnum = busnum; 768 bus->busnum = busnum;
769 } else { 769 } else {
770 printk (KERN_ERR "%s: too many buses\n", usbcore_name); 770 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
771 up(&usb_bus_list_lock); 771 up(&usb_bus_list_lock);
772 return -E2BIG; 772 return -E2BIG;
773 } 773 }
774 774
775 bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0), 775 bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
776 bus->controller, "usb_host%d", busnum); 776 bus->controller, "usb_host%d", busnum);
777 if (IS_ERR(bus->class_dev)) { 777 if (IS_ERR(bus->class_dev)) {
778 clear_bit(busnum, busmap.busmap); 778 clear_bit(busnum, busmap.busmap);
779 up(&usb_bus_list_lock); 779 up(&usb_bus_list_lock);
780 return PTR_ERR(bus->class_dev); 780 return PTR_ERR(bus->class_dev);
781 } 781 }
782 782
783 class_set_devdata(bus->class_dev, bus); 783 class_set_devdata(bus->class_dev, bus);
784 784
785 /* Add it to the local list of buses */ 785 /* Add it to the local list of buses */
786 list_add (&bus->bus_list, &usb_bus_list); 786 list_add (&bus->bus_list, &usb_bus_list);
787 up (&usb_bus_list_lock); 787 up (&usb_bus_list_lock);
788 788
789 usb_notify_add_bus(bus); 789 usb_notify_add_bus(bus);
790 790
791 dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum); 791 dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
792 return 0; 792 return 0;
793 } 793 }
794 794
795 /** 795 /**
796 * usb_deregister_bus - deregisters the USB host controller 796 * usb_deregister_bus - deregisters the USB host controller
797 * @bus: pointer to the bus to deregister 797 * @bus: pointer to the bus to deregister
798 * Context: !in_interrupt() 798 * Context: !in_interrupt()
799 * 799 *
800 * Recycles the bus number, and unlinks the controller from usbcore data 800 * Recycles the bus number, and unlinks the controller from usbcore data
801 * structures so that it won't be seen by scanning the bus list. 801 * structures so that it won't be seen by scanning the bus list.
802 */ 802 */
803 static void usb_deregister_bus (struct usb_bus *bus) 803 static void usb_deregister_bus (struct usb_bus *bus)
804 { 804 {
805 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); 805 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
806 806
807 /* 807 /*
808 * NOTE: make sure that all the devices are removed by the 808 * NOTE: make sure that all the devices are removed by the
809 * controller code, as well as having it call this when cleaning 809 * controller code, as well as having it call this when cleaning
810 * itself up 810 * itself up
811 */ 811 */
812 down (&usb_bus_list_lock); 812 down (&usb_bus_list_lock);
813 list_del (&bus->bus_list); 813 list_del (&bus->bus_list);
814 up (&usb_bus_list_lock); 814 up (&usb_bus_list_lock);
815 815
816 usb_notify_remove_bus(bus); 816 usb_notify_remove_bus(bus);
817 817
818 clear_bit (bus->busnum, busmap.busmap); 818 clear_bit (bus->busnum, busmap.busmap);
819 819
820 class_device_unregister(bus->class_dev); 820 class_device_unregister(bus->class_dev);
821 } 821 }
822 822
823 /** 823 /**
824 * register_root_hub - called by usb_add_hcd() to register a root hub 824 * register_root_hub - called by usb_add_hcd() to register a root hub
825 * @usb_dev: the usb root hub device to be registered. 825 * @usb_dev: the usb root hub device to be registered.
826 * @hcd: host controller for this root hub 826 * @hcd: host controller for this root hub
827 * 827 *
828 * This function registers the root hub with the USB subsystem. It sets up 828 * This function registers the root hub with the USB subsystem. It sets up
829 * the device properly in the device tree and stores the root_hub pointer 829 * the device properly in the device tree and stores the root_hub pointer
830 * in the bus structure, then calls usb_new_device() to register the usb 830 * in the bus structure, then calls usb_new_device() to register the usb
831 * device. It also assigns the root hub's USB address (always 1). 831 * device. It also assigns the root hub's USB address (always 1).
832 */ 832 */
833 static int register_root_hub (struct usb_device *usb_dev, 833 static int register_root_hub (struct usb_device *usb_dev,
834 struct usb_hcd *hcd) 834 struct usb_hcd *hcd)
835 { 835 {
836 struct device *parent_dev = hcd->self.controller; 836 struct device *parent_dev = hcd->self.controller;
837 const int devnum = 1; 837 const int devnum = 1;
838 int retval; 838 int retval;
839 839
840 usb_dev->devnum = devnum; 840 usb_dev->devnum = devnum;
841 usb_dev->bus->devnum_next = devnum + 1; 841 usb_dev->bus->devnum_next = devnum + 1;
842 memset (&usb_dev->bus->devmap.devicemap, 0, 842 memset (&usb_dev->bus->devmap.devicemap, 0,
843 sizeof usb_dev->bus->devmap.devicemap); 843 sizeof usb_dev->bus->devmap.devicemap);
844 set_bit (devnum, usb_dev->bus->devmap.devicemap); 844 set_bit (devnum, usb_dev->bus->devmap.devicemap);
845 usb_set_device_state(usb_dev, USB_STATE_ADDRESS); 845 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
846 846
847 down (&usb_bus_list_lock); 847 down (&usb_bus_list_lock);
848 usb_dev->bus->root_hub = usb_dev; 848 usb_dev->bus->root_hub = usb_dev;
849 849
850 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 850 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
851 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); 851 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
852 if (retval != sizeof usb_dev->descriptor) { 852 if (retval != sizeof usb_dev->descriptor) {
853 usb_dev->bus->root_hub = NULL; 853 usb_dev->bus->root_hub = NULL;
854 up (&usb_bus_list_lock); 854 up (&usb_bus_list_lock);
855 dev_dbg (parent_dev, "can't read %s device descriptor %d\n", 855 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
856 usb_dev->dev.bus_id, retval); 856 usb_dev->dev.bus_id, retval);
857 return (retval < 0) ? retval : -EMSGSIZE; 857 return (retval < 0) ? retval : -EMSGSIZE;
858 } 858 }
859 859
860 retval = usb_new_device (usb_dev); 860 retval = usb_new_device (usb_dev);
861 if (retval) { 861 if (retval) {
862 usb_dev->bus->root_hub = NULL; 862 usb_dev->bus->root_hub = NULL;
863 dev_err (parent_dev, "can't register root hub for %s, %d\n", 863 dev_err (parent_dev, "can't register root hub for %s, %d\n",
864 usb_dev->dev.bus_id, retval); 864 usb_dev->dev.bus_id, retval);
865 } 865 }
866 up (&usb_bus_list_lock); 866 up (&usb_bus_list_lock);
867 867
868 if (retval == 0) { 868 if (retval == 0) {
869 spin_lock_irq (&hcd_root_hub_lock); 869 spin_lock_irq (&hcd_root_hub_lock);
870 hcd->rh_registered = 1; 870 hcd->rh_registered = 1;
871 spin_unlock_irq (&hcd_root_hub_lock); 871 spin_unlock_irq (&hcd_root_hub_lock);
872 872
873 /* Did the HC die before the root hub was registered? */ 873 /* Did the HC die before the root hub was registered? */
874 if (hcd->state == HC_STATE_HALT) 874 if (hcd->state == HC_STATE_HALT)
875 usb_hc_died (hcd); /* This time clean up */ 875 usb_hc_died (hcd); /* This time clean up */
876 } 876 }
877 877
878 return retval; 878 return retval;
879 } 879 }
880 880
881 void usb_enable_root_hub_irq (struct usb_bus *bus) 881 void usb_enable_root_hub_irq (struct usb_bus *bus)
882 { 882 {
883 struct usb_hcd *hcd; 883 struct usb_hcd *hcd;
884 884
885 hcd = container_of (bus, struct usb_hcd, self); 885 hcd = container_of (bus, struct usb_hcd, self);
886 if (hcd->driver->hub_irq_enable && !hcd->poll_rh && 886 if (hcd->driver->hub_irq_enable && !hcd->poll_rh &&
887 hcd->state != HC_STATE_HALT) 887 hcd->state != HC_STATE_HALT)
888 hcd->driver->hub_irq_enable (hcd); 888 hcd->driver->hub_irq_enable (hcd);
889 } 889 }
890 890
891 891
892 /*-------------------------------------------------------------------------*/ 892 /*-------------------------------------------------------------------------*/
893 893
894 /** 894 /**
895 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds 895 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
896 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} 896 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
897 * @is_input: true iff the transaction sends data to the host 897 * @is_input: true iff the transaction sends data to the host
898 * @isoc: true for isochronous transactions, false for interrupt ones 898 * @isoc: true for isochronous transactions, false for interrupt ones
899 * @bytecount: how many bytes in the transaction. 899 * @bytecount: how many bytes in the transaction.
900 * 900 *
901 * Returns approximate bus time in nanoseconds for a periodic transaction. 901 * Returns approximate bus time in nanoseconds for a periodic transaction.
902 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be 902 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
903 * scheduled in software, this function is only used for such scheduling. 903 * scheduled in software, this function is only used for such scheduling.
904 */ 904 */
905 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) 905 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
906 { 906 {
907 unsigned long tmp; 907 unsigned long tmp;
908 908
909 switch (speed) { 909 switch (speed) {
910 case USB_SPEED_LOW: /* INTR only */ 910 case USB_SPEED_LOW: /* INTR only */
911 if (is_input) { 911 if (is_input) {
912 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; 912 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
913 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); 913 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
914 } else { 914 } else {
915 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; 915 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
916 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); 916 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
917 } 917 }
918 case USB_SPEED_FULL: /* ISOC or INTR */ 918 case USB_SPEED_FULL: /* ISOC or INTR */
919 if (isoc) { 919 if (isoc) {
920 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; 920 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
921 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp); 921 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
922 } else { 922 } else {
923 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; 923 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
924 return (9107L + BW_HOST_DELAY + tmp); 924 return (9107L + BW_HOST_DELAY + tmp);
925 } 925 }
926 case USB_SPEED_HIGH: /* ISOC or INTR */ 926 case USB_SPEED_HIGH: /* ISOC or INTR */
927 // FIXME adjust for input vs output 927 // FIXME adjust for input vs output
928 if (isoc) 928 if (isoc)
929 tmp = HS_NSECS_ISO (bytecount); 929 tmp = HS_NSECS_ISO (bytecount);
930 else 930 else
931 tmp = HS_NSECS (bytecount); 931 tmp = HS_NSECS (bytecount);
932 return tmp; 932 return tmp;
933 default: 933 default:
934 pr_debug ("%s: bogus device speed!\n", usbcore_name); 934 pr_debug ("%s: bogus device speed!\n", usbcore_name);
935 return -1; 935 return -1;
936 } 936 }
937 } 937 }
938 EXPORT_SYMBOL (usb_calc_bus_time); 938 EXPORT_SYMBOL (usb_calc_bus_time);
939 939
940 /* 940 /*
941 * usb_check_bandwidth(): 941 * usb_check_bandwidth():
942 * 942 *
943 * old_alloc is from host_controller->bandwidth_allocated in microseconds; 943 * old_alloc is from host_controller->bandwidth_allocated in microseconds;
944 * bustime is from calc_bus_time(), but converted to microseconds. 944 * bustime is from calc_bus_time(), but converted to microseconds.
945 * 945 *
946 * returns <bustime in us> if successful, 946 * returns <bustime in us> if successful,
947 * or -ENOSPC if bandwidth request fails. 947 * or -ENOSPC if bandwidth request fails.
948 * 948 *
949 * FIXME: 949 * FIXME:
950 * This initial implementation does not use Endpoint.bInterval 950 * This initial implementation does not use Endpoint.bInterval
951 * in managing bandwidth allocation. 951 * in managing bandwidth allocation.
952 * It probably needs to be expanded to use Endpoint.bInterval. 952 * It probably needs to be expanded to use Endpoint.bInterval.
953 * This can be done as a later enhancement (correction). 953 * This can be done as a later enhancement (correction).
954 * 954 *
955 * This will also probably require some kind of 955 * This will also probably require some kind of
956 * frame allocation tracking...meaning, for example, 956 * frame allocation tracking...meaning, for example,
957 * that if multiple drivers request interrupts every 10 USB frames, 957 * that if multiple drivers request interrupts every 10 USB frames,
958 * they don't all have to be allocated at 958 * they don't all have to be allocated at
959 * frame numbers N, N+10, N+20, etc. Some of them could be at 959 * frame numbers N, N+10, N+20, etc. Some of them could be at
960 * N+11, N+21, N+31, etc., and others at 960 * N+11, N+21, N+31, etc., and others at
961 * N+12, N+22, N+32, etc. 961 * N+12, N+22, N+32, etc.
962 * 962 *
963 * Similarly for isochronous transfers... 963 * Similarly for isochronous transfers...
964 * 964 *
965 * Individual HCDs can schedule more directly ... this logic 965 * Individual HCDs can schedule more directly ... this logic
966 * is not correct for high speed transfers. 966 * is not correct for high speed transfers.
967 */ 967 */
968 int usb_check_bandwidth (struct usb_device *dev, struct urb *urb) 968 int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
969 { 969 {
970 unsigned int pipe = urb->pipe; 970 unsigned int pipe = urb->pipe;
971 long bustime; 971 long bustime;
972 int is_in = usb_pipein (pipe); 972 int is_in = usb_pipein (pipe);
973 int is_iso = usb_pipeisoc (pipe); 973 int is_iso = usb_pipeisoc (pipe);
974 int old_alloc = dev->bus->bandwidth_allocated; 974 int old_alloc = dev->bus->bandwidth_allocated;
975 int new_alloc; 975 int new_alloc;
976 976
977 977
978 bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso, 978 bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
979 usb_maxpacket (dev, pipe, !is_in))); 979 usb_maxpacket (dev, pipe, !is_in)));
980 if (is_iso) 980 if (is_iso)
981 bustime /= urb->number_of_packets; 981 bustime /= urb->number_of_packets;
982 982
983 new_alloc = old_alloc + (int) bustime; 983 new_alloc = old_alloc + (int) bustime;
984 if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) { 984 if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
985 #ifdef DEBUG 985 #ifdef DEBUG
986 char *mode = 986 char *mode =
987 #ifdef CONFIG_USB_BANDWIDTH 987 #ifdef CONFIG_USB_BANDWIDTH
988 ""; 988 "";
989 #else 989 #else
990 "would have "; 990 "would have ";
991 #endif 991 #endif
992 dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n", 992 dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
993 mode, old_alloc, bustime, new_alloc); 993 mode, old_alloc, bustime, new_alloc);
994 #endif 994 #endif
995 #ifdef CONFIG_USB_BANDWIDTH 995 #ifdef CONFIG_USB_BANDWIDTH
996 bustime = -ENOSPC; /* report error */ 996 bustime = -ENOSPC; /* report error */
997 #endif 997 #endif
998 } 998 }
999 999
1000 return bustime; 1000 return bustime;
1001 } 1001 }
1002 EXPORT_SYMBOL (usb_check_bandwidth); 1002 EXPORT_SYMBOL (usb_check_bandwidth);
1003 1003
1004 1004
1005 /** 1005 /**
1006 * usb_claim_bandwidth - records bandwidth for a periodic transfer 1006 * usb_claim_bandwidth - records bandwidth for a periodic transfer
1007 * @dev: source/target of request 1007 * @dev: source/target of request
1008 * @urb: request (urb->dev == dev) 1008 * @urb: request (urb->dev == dev)
1009 * @bustime: bandwidth consumed, in (average) microseconds per frame 1009 * @bustime: bandwidth consumed, in (average) microseconds per frame
1010 * @isoc: true iff the request is isochronous 1010 * @isoc: true iff the request is isochronous
1011 * 1011 *
1012 * Bus bandwidth reservations are recorded purely for diagnostic purposes. 1012 * Bus bandwidth reservations are recorded purely for diagnostic purposes.
1013 * HCDs are expected not to overcommit periodic bandwidth, and to record such 1013 * HCDs are expected not to overcommit periodic bandwidth, and to record such
1014 * reservations whenever endpoints are added to the periodic schedule. 1014 * reservations whenever endpoints are added to the periodic schedule.
1015 * 1015 *
1016 * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's 1016 * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
1017 * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable 1017 * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
1018 * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how 1018 * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
1019 * large its periodic schedule is. 1019 * large its periodic schedule is.
1020 */ 1020 */
1021 void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc) 1021 void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
1022 { 1022 {
1023 dev->bus->bandwidth_allocated += bustime; 1023 dev->bus->bandwidth_allocated += bustime;
1024 if (isoc) 1024 if (isoc)
1025 dev->bus->bandwidth_isoc_reqs++; 1025 dev->bus->bandwidth_isoc_reqs++;
1026 else 1026 else
1027 dev->bus->bandwidth_int_reqs++; 1027 dev->bus->bandwidth_int_reqs++;
1028 urb->bandwidth = bustime; 1028 urb->bandwidth = bustime;
1029 1029
1030 #ifdef USB_BANDWIDTH_MESSAGES 1030 #ifdef USB_BANDWIDTH_MESSAGES
1031 dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n", 1031 dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
1032 bustime, 1032 bustime,
1033 isoc ? "ISOC" : "INTR", 1033 isoc ? "ISOC" : "INTR",
1034 dev->bus->bandwidth_allocated, 1034 dev->bus->bandwidth_allocated,
1035 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs); 1035 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
1036 #endif 1036 #endif
1037 } 1037 }
1038 EXPORT_SYMBOL (usb_claim_bandwidth); 1038 EXPORT_SYMBOL (usb_claim_bandwidth);
1039 1039
1040 1040
1041 /** 1041 /**
1042 * usb_release_bandwidth - reverses effect of usb_claim_bandwidth() 1042 * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
1043 * @dev: source/target of request 1043 * @dev: source/target of request
1044 * @urb: request (urb->dev == dev) 1044 * @urb: request (urb->dev == dev)
1045 * @isoc: true iff the request is isochronous 1045 * @isoc: true iff the request is isochronous
1046 * 1046 *
1047 * This records that previously allocated bandwidth has been released. 1047 * This records that previously allocated bandwidth has been released.
1048 * Bandwidth is released when endpoints are removed from the host controller's 1048 * Bandwidth is released when endpoints are removed from the host controller's
1049 * periodic schedule. 1049 * periodic schedule.
1050 */ 1050 */
1051 void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc) 1051 void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
1052 { 1052 {
1053 dev->bus->bandwidth_allocated -= urb->bandwidth; 1053 dev->bus->bandwidth_allocated -= urb->bandwidth;
1054 if (isoc) 1054 if (isoc)
1055 dev->bus->bandwidth_isoc_reqs--; 1055 dev->bus->bandwidth_isoc_reqs--;
1056 else 1056 else
1057 dev->bus->bandwidth_int_reqs--; 1057 dev->bus->bandwidth_int_reqs--;
1058 1058
1059 #ifdef USB_BANDWIDTH_MESSAGES 1059 #ifdef USB_BANDWIDTH_MESSAGES
1060 dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n", 1060 dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
1061 urb->bandwidth, 1061 urb->bandwidth,
1062 isoc ? "ISOC" : "INTR", 1062 isoc ? "ISOC" : "INTR",
1063 dev->bus->bandwidth_allocated, 1063 dev->bus->bandwidth_allocated,
1064 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs); 1064 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
1065 #endif 1065 #endif
1066 urb->bandwidth = 0; 1066 urb->bandwidth = 0;
1067 } 1067 }
1068 EXPORT_SYMBOL (usb_release_bandwidth); 1068 EXPORT_SYMBOL (usb_release_bandwidth);
1069 1069
1070 1070
1071 /*-------------------------------------------------------------------------*/ 1071 /*-------------------------------------------------------------------------*/
1072 1072
1073 /* 1073 /*
1074 * Generic HC operations. 1074 * Generic HC operations.
1075 */ 1075 */
1076 1076
1077 /*-------------------------------------------------------------------------*/ 1077 /*-------------------------------------------------------------------------*/
1078 1078
1079 static void urb_unlink (struct urb *urb) 1079 static void urb_unlink (struct urb *urb)
1080 { 1080 {
1081 unsigned long flags; 1081 unsigned long flags;
1082 1082
1083 /* Release any periodic transfer bandwidth */ 1083 /* Release any periodic transfer bandwidth */
1084 if (urb->bandwidth) 1084 if (urb->bandwidth)
1085 usb_release_bandwidth (urb->dev, urb, 1085 usb_release_bandwidth (urb->dev, urb,
1086 usb_pipeisoc (urb->pipe)); 1086 usb_pipeisoc (urb->pipe));
1087 1087
1088 /* clear all state linking urb to this dev (and hcd) */ 1088 /* clear all state linking urb to this dev (and hcd) */
1089 1089
1090 spin_lock_irqsave (&hcd_data_lock, flags); 1090 spin_lock_irqsave (&hcd_data_lock, flags);
1091 list_del_init (&urb->urb_list); 1091 list_del_init (&urb->urb_list);
1092 spin_unlock_irqrestore (&hcd_data_lock, flags); 1092 spin_unlock_irqrestore (&hcd_data_lock, flags);
1093 usb_put_dev (urb->dev); 1093 usb_put_dev (urb->dev);
1094 } 1094 }
1095 1095
1096 1096
1097 /* may be called in any context with a valid urb->dev usecount 1097 /* may be called in any context with a valid urb->dev usecount
1098 * caller surrenders "ownership" of urb 1098 * caller surrenders "ownership" of urb
1099 * expects usb_submit_urb() to have sanity checked and conditioned all 1099 * expects usb_submit_urb() to have sanity checked and conditioned all
1100 * inputs in the urb 1100 * inputs in the urb
1101 */ 1101 */
1102 static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags) 1102 static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1103 { 1103 {
1104 int status; 1104 int status;
1105 struct usb_hcd *hcd = urb->dev->bus->hcpriv; 1105 struct usb_hcd *hcd = urb->dev->bus->hcpriv;
1106 struct usb_host_endpoint *ep; 1106 struct usb_host_endpoint *ep;
1107 unsigned long flags; 1107 unsigned long flags;
1108 1108
1109 if (!hcd) 1109 if (!hcd)
1110 return -ENODEV; 1110 return -ENODEV;
1111 1111
1112 usbmon_urb_submit(&hcd->self, urb); 1112 usbmon_urb_submit(&hcd->self, urb);
1113 1113
1114 /* 1114 /*
1115 * Atomically queue the urb, first to our records, then to the HCD. 1115 * Atomically queue the urb, first to our records, then to the HCD.
1116 * Access to urb->status is controlled by urb->lock ... changes on 1116 * Access to urb->status is controlled by urb->lock ... changes on
1117 * i/o completion (normal or fault) or unlinking. 1117 * i/o completion (normal or fault) or unlinking.
1118 */ 1118 */
1119 1119
1120 // FIXME: verify that quiescing hc works right (RH cleans up) 1120 // FIXME: verify that quiescing hc works right (RH cleans up)
1121 1121
1122 spin_lock_irqsave (&hcd_data_lock, flags); 1122 spin_lock_irqsave (&hcd_data_lock, flags);
1123 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out) 1123 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
1124 [usb_pipeendpoint(urb->pipe)]; 1124 [usb_pipeendpoint(urb->pipe)];
1125 if (unlikely (!ep)) 1125 if (unlikely (!ep))
1126 status = -ENOENT; 1126 status = -ENOENT;
1127 else if (unlikely (urb->reject)) 1127 else if (unlikely (urb->reject))
1128 status = -EPERM; 1128 status = -EPERM;
1129 else switch (hcd->state) { 1129 else switch (hcd->state) {
1130 case HC_STATE_RUNNING: 1130 case HC_STATE_RUNNING:
1131 case HC_STATE_RESUMING: 1131 case HC_STATE_RESUMING:
1132 doit: 1132 doit:
1133 usb_get_dev (urb->dev); 1133 usb_get_dev (urb->dev);
1134 list_add_tail (&urb->urb_list, &ep->urb_list); 1134 list_add_tail (&urb->urb_list, &ep->urb_list);
1135 status = 0; 1135 status = 0;
1136 break; 1136 break;
1137 case HC_STATE_SUSPENDED: 1137 case HC_STATE_SUSPENDED:
1138 /* HC upstream links (register access, wakeup signaling) can work 1138 /* HC upstream links (register access, wakeup signaling) can work
1139 * even when the downstream links (and DMA etc) are quiesced; let 1139 * even when the downstream links (and DMA etc) are quiesced; let
1140 * usbcore talk to the root hub. 1140 * usbcore talk to the root hub.
1141 */ 1141 */
1142 if (hcd->self.controller->power.power_state.event == PM_EVENT_ON 1142 if (hcd->self.controller->power.power_state.event == PM_EVENT_ON
1143 && urb->dev->parent == NULL) 1143 && urb->dev->parent == NULL)
1144 goto doit; 1144 goto doit;
1145 /* FALL THROUGH */ 1145 /* FALL THROUGH */
1146 default: 1146 default:
1147 status = -ESHUTDOWN; 1147 status = -ESHUTDOWN;
1148 break; 1148 break;
1149 } 1149 }
1150 spin_unlock_irqrestore (&hcd_data_lock, flags); 1150 spin_unlock_irqrestore (&hcd_data_lock, flags);
1151 if (status) { 1151 if (status) {
1152 INIT_LIST_HEAD (&urb->urb_list); 1152 INIT_LIST_HEAD (&urb->urb_list);
1153 usbmon_urb_submit_error(&hcd->self, urb, status); 1153 usbmon_urb_submit_error(&hcd->self, urb, status);
1154 return status; 1154 return status;
1155 } 1155 }
1156 1156
1157 /* increment urb's reference count as part of giving it to the HCD 1157 /* increment urb's reference count as part of giving it to the HCD
1158 * (which now controls it). HCD guarantees that it either returns 1158 * (which now controls it). HCD guarantees that it either returns
1159 * an error or calls giveback(), but not both. 1159 * an error or calls giveback(), but not both.
1160 */ 1160 */
1161 urb = usb_get_urb (urb); 1161 urb = usb_get_urb (urb);
1162 atomic_inc (&urb->use_count); 1162 atomic_inc (&urb->use_count);
1163 1163
1164 if (urb->dev == hcd->self.root_hub) { 1164 if (urb->dev == hcd->self.root_hub) {
1165 /* NOTE: requirement on hub callers (usbfs and the hub 1165 /* NOTE: requirement on hub callers (usbfs and the hub
1166 * driver, for now) that URBs' urb->transfer_buffer be 1166 * driver, for now) that URBs' urb->transfer_buffer be
1167 * valid and usb_buffer_{sync,unmap}() not be needed, since 1167 * valid and usb_buffer_{sync,unmap}() not be needed, since
1168 * they could clobber root hub response data. 1168 * they could clobber root hub response data.
1169 */ 1169 */
1170 status = rh_urb_enqueue (hcd, urb); 1170 status = rh_urb_enqueue (hcd, urb);
1171 goto done; 1171 goto done;
1172 } 1172 }
1173 1173
1174 /* lower level hcd code should use *_dma exclusively, 1174 /* lower level hcd code should use *_dma exclusively,
1175 * unless it uses pio or talks to another transport. 1175 * unless it uses pio or talks to another transport.
1176 */ 1176 */
1177 if (hcd->self.controller->dma_mask) { 1177 if (hcd->self.controller->dma_mask) {
1178 if (usb_pipecontrol (urb->pipe) 1178 if (usb_pipecontrol (urb->pipe)
1179 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) 1179 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1180 urb->setup_dma = dma_map_single ( 1180 urb->setup_dma = dma_map_single (
1181 hcd->self.controller, 1181 hcd->self.controller,
1182 urb->setup_packet, 1182 urb->setup_packet,
1183 sizeof (struct usb_ctrlrequest), 1183 sizeof (struct usb_ctrlrequest),
1184 DMA_TO_DEVICE); 1184 DMA_TO_DEVICE);
1185 if (urb->transfer_buffer_length != 0 1185 if (urb->transfer_buffer_length != 0
1186 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) 1186 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1187 urb->transfer_dma = dma_map_single ( 1187 urb->transfer_dma = dma_map_single (
1188 hcd->self.controller, 1188 hcd->self.controller,
1189 urb->transfer_buffer, 1189 urb->transfer_buffer,
1190 urb->transfer_buffer_length, 1190 urb->transfer_buffer_length,
1191 usb_pipein (urb->pipe) 1191 usb_pipein (urb->pipe)
1192 ? DMA_FROM_DEVICE 1192 ? DMA_FROM_DEVICE
1193 : DMA_TO_DEVICE); 1193 : DMA_TO_DEVICE);
1194 } 1194 }
1195 1195
1196 status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags); 1196 status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
1197 done: 1197 done:
1198 if (unlikely (status)) { 1198 if (unlikely (status)) {
1199 urb_unlink (urb); 1199 urb_unlink (urb);
1200 atomic_dec (&urb->use_count); 1200 atomic_dec (&urb->use_count);
1201 if (urb->reject) 1201 if (urb->reject)
1202 wake_up (&usb_kill_urb_queue); 1202 wake_up (&usb_kill_urb_queue);
1203 usb_put_urb (urb); 1203 usb_put_urb (urb);
1204 usbmon_urb_submit_error(&hcd->self, urb, status); 1204 usbmon_urb_submit_error(&hcd->self, urb, status);
1205 } 1205 }
1206 return status; 1206 return status;
1207 } 1207 }
1208 1208
1209 /*-------------------------------------------------------------------------*/ 1209 /*-------------------------------------------------------------------------*/
1210 1210
1211 /* called in any context */ 1211 /* called in any context */
1212 static int hcd_get_frame_number (struct usb_device *udev) 1212 static int hcd_get_frame_number (struct usb_device *udev)
1213 { 1213 {
1214 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv; 1214 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1215 if (!HC_IS_RUNNING (hcd->state)) 1215 if (!HC_IS_RUNNING (hcd->state))
1216 return -ESHUTDOWN; 1216 return -ESHUTDOWN;
1217 return hcd->driver->get_frame_number (hcd); 1217 return hcd->driver->get_frame_number (hcd);
1218 } 1218 }
1219 1219
1220 /*-------------------------------------------------------------------------*/ 1220 /*-------------------------------------------------------------------------*/
1221 1221
1222 /* this makes the hcd giveback() the urb more quickly, by kicking it 1222 /* this makes the hcd giveback() the urb more quickly, by kicking it
1223 * off hardware queues (which may take a while) and returning it as 1223 * off hardware queues (which may take a while) and returning it as
1224 * soon as practical. we've already set up the urb's return status, 1224 * soon as practical. we've already set up the urb's return status,
1225 * but we can't know if the callback completed already. 1225 * but we can't know if the callback completed already.
1226 */ 1226 */
1227 static int 1227 static int
1228 unlink1 (struct usb_hcd *hcd, struct urb *urb) 1228 unlink1 (struct usb_hcd *hcd, struct urb *urb)
1229 { 1229 {
1230 int value; 1230 int value;
1231 1231
1232 if (urb->dev == hcd->self.root_hub) 1232 if (urb->dev == hcd->self.root_hub)
1233 value = usb_rh_urb_dequeue (hcd, urb); 1233 value = usb_rh_urb_dequeue (hcd, urb);
1234 else { 1234 else {
1235 1235
1236 /* The only reason an HCD might fail this call is if 1236 /* The only reason an HCD might fail this call is if
1237 * it has not yet fully queued the urb to begin with. 1237 * it has not yet fully queued the urb to begin with.
1238 * Such failures should be harmless. */ 1238 * Such failures should be harmless. */
1239 value = hcd->driver->urb_dequeue (hcd, urb); 1239 value = hcd->driver->urb_dequeue (hcd, urb);
1240 } 1240 }
1241 1241
1242 if (value != 0) 1242 if (value != 0)
1243 dev_dbg (hcd->self.controller, "dequeue %p --> %d\n", 1243 dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
1244 urb, value); 1244 urb, value);
1245 return value; 1245 return value;
1246 } 1246 }
1247 1247
1248 /* 1248 /*
1249 * called in any context 1249 * called in any context
1250 * 1250 *
1251 * caller guarantees urb won't be recycled till both unlink() 1251 * caller guarantees urb won't be recycled till both unlink()
1252 * and the urb's completion function return 1252 * and the urb's completion function return
1253 */ 1253 */
1254 static int hcd_unlink_urb (struct urb *urb, int status) 1254 static int hcd_unlink_urb (struct urb *urb, int status)
1255 { 1255 {
1256 struct usb_host_endpoint *ep; 1256 struct usb_host_endpoint *ep;
1257 struct usb_hcd *hcd = NULL; 1257 struct usb_hcd *hcd = NULL;
1258 struct device *sys = NULL; 1258 struct device *sys = NULL;
1259 unsigned long flags; 1259 unsigned long flags;
1260 struct list_head *tmp; 1260 struct list_head *tmp;
1261 int retval; 1261 int retval;
1262 1262
1263 if (!urb) 1263 if (!urb)
1264 return -EINVAL; 1264 return -EINVAL;
1265 if (!urb->dev || !urb->dev->bus) 1265 if (!urb->dev || !urb->dev->bus)
1266 return -ENODEV; 1266 return -ENODEV;
1267 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out) 1267 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
1268 [usb_pipeendpoint(urb->pipe)]; 1268 [usb_pipeendpoint(urb->pipe)];
1269 if (!ep) 1269 if (!ep)
1270 return -ENODEV; 1270 return -ENODEV;
1271 1271
1272 /* 1272 /*
1273 * we contend for urb->status with the hcd core, 1273 * we contend for urb->status with the hcd core,
1274 * which changes it while returning the urb. 1274 * which changes it while returning the urb.
1275 * 1275 *
1276 * Caller guaranteed that the urb pointer hasn't been freed, and 1276 * Caller guaranteed that the urb pointer hasn't been freed, and
1277 * that it was submitted. But as a rule it can't know whether or 1277 * that it was submitted. But as a rule it can't know whether or
1278 * not it's already been unlinked ... so we respect the reversed 1278 * not it's already been unlinked ... so we respect the reversed
1279 * lock sequence needed for the usb_hcd_giveback_urb() code paths 1279 * lock sequence needed for the usb_hcd_giveback_urb() code paths
1280 * (urb lock, then hcd_data_lock) in case some other CPU is now 1280 * (urb lock, then hcd_data_lock) in case some other CPU is now
1281 * unlinking it. 1281 * unlinking it.
1282 */ 1282 */
1283 spin_lock_irqsave (&urb->lock, flags); 1283 spin_lock_irqsave (&urb->lock, flags);
1284 spin_lock (&hcd_data_lock); 1284 spin_lock (&hcd_data_lock);
1285 1285
1286 sys = &urb->dev->dev; 1286 sys = &urb->dev->dev;
1287 hcd = urb->dev->bus->hcpriv; 1287 hcd = urb->dev->bus->hcpriv;
1288 if (hcd == NULL) { 1288 if (hcd == NULL) {
1289 retval = -ENODEV; 1289 retval = -ENODEV;
1290 goto done; 1290 goto done;
1291 } 1291 }
1292 1292
1293 /* insist the urb is still queued */ 1293 /* insist the urb is still queued */
1294 list_for_each(tmp, &ep->urb_list) { 1294 list_for_each(tmp, &ep->urb_list) {
1295 if (tmp == &urb->urb_list) 1295 if (tmp == &urb->urb_list)
1296 break; 1296 break;
1297 } 1297 }
1298 if (tmp != &urb->urb_list) { 1298 if (tmp != &urb->urb_list) {
1299 retval = -EIDRM; 1299 retval = -EIDRM;
1300 goto done; 1300 goto done;
1301 } 1301 }
1302 1302
1303 /* Any status except -EINPROGRESS means something already started to 1303 /* Any status except -EINPROGRESS means something already started to
1304 * unlink this URB from the hardware. So there's no more work to do. 1304 * unlink this URB from the hardware. So there's no more work to do.
1305 */ 1305 */
1306 if (urb->status != -EINPROGRESS) { 1306 if (urb->status != -EINPROGRESS) {
1307 retval = -EBUSY; 1307 retval = -EBUSY;
1308 goto done; 1308 goto done;
1309 } 1309 }
1310 1310
1311 /* IRQ setup can easily be broken so that USB controllers 1311 /* IRQ setup can easily be broken so that USB controllers
1312 * never get completion IRQs ... maybe even the ones we need to 1312 * never get completion IRQs ... maybe even the ones we need to
1313 * finish unlinking the initial failed usb_set_address() 1313 * finish unlinking the initial failed usb_set_address()
1314 * or device descriptor fetch. 1314 * or device descriptor fetch.
1315 */ 1315 */
1316 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) 1316 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags)
1317 && hcd->self.root_hub != urb->dev) { 1317 && hcd->self.root_hub != urb->dev) {
1318 dev_warn (hcd->self.controller, "Unlink after no-IRQ? " 1318 dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
1319 "Controller is probably using the wrong IRQ." 1319 "Controller is probably using the wrong IRQ."
1320 "\n"); 1320 "\n");
1321 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); 1321 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1322 } 1322 }
1323 1323
1324 urb->status = status; 1324 urb->status = status;
1325 1325
1326 spin_unlock (&hcd_data_lock); 1326 spin_unlock (&hcd_data_lock);
1327 spin_unlock_irqrestore (&urb->lock, flags); 1327 spin_unlock_irqrestore (&urb->lock, flags);
1328 1328
1329 retval = unlink1 (hcd, urb); 1329 retval = unlink1 (hcd, urb);
1330 if (retval == 0) 1330 if (retval == 0)
1331 retval = -EINPROGRESS; 1331 retval = -EINPROGRESS;
1332 return retval; 1332 return retval;
1333 1333
1334 done: 1334 done:
1335 spin_unlock (&hcd_data_lock); 1335 spin_unlock (&hcd_data_lock);
1336 spin_unlock_irqrestore (&urb->lock, flags); 1336 spin_unlock_irqrestore (&urb->lock, flags);
1337 if (retval != -EIDRM && sys && sys->driver) 1337 if (retval != -EIDRM && sys && sys->driver)
1338 dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval); 1338 dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
1339 return retval; 1339 return retval;
1340 } 1340 }
1341 1341
1342 /*-------------------------------------------------------------------------*/ 1342 /*-------------------------------------------------------------------------*/
1343 1343
1344 /* disables the endpoint: cancels any pending urbs, then synchronizes with 1344 /* disables the endpoint: cancels any pending urbs, then synchronizes with
1345 * the hcd to make sure all endpoint state is gone from hardware. use for 1345 * the hcd to make sure all endpoint state is gone from hardware. use for
1346 * set_configuration, set_interface, driver removal, physical disconnect. 1346 * set_configuration, set_interface, driver removal, physical disconnect.
1347 * 1347 *
1348 * example: a qh stored in ep->hcpriv, holding state related to endpoint 1348 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1349 * type, maxpacket size, toggle, halt status, and scheduling. 1349 * type, maxpacket size, toggle, halt status, and scheduling.
1350 */ 1350 */
1351 static void 1351 static void
1352 hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep) 1352 hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep)
1353 { 1353 {
1354 struct usb_hcd *hcd; 1354 struct usb_hcd *hcd;
1355 struct urb *urb; 1355 struct urb *urb;
1356 1356
1357 hcd = udev->bus->hcpriv; 1357 hcd = udev->bus->hcpriv;
1358 1358
1359 WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT && 1359 WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
1360 udev->state != USB_STATE_NOTATTACHED); 1360 udev->state != USB_STATE_NOTATTACHED);
1361 1361
1362 local_irq_disable (); 1362 local_irq_disable ();
1363 1363
1364 /* FIXME move most of this into message.c as part of its 1364 /* FIXME move most of this into message.c as part of its
1365 * endpoint disable logic 1365 * endpoint disable logic
1366 */ 1366 */
1367 1367
1368 /* ep is already gone from udev->ep_{in,out}[]; no more submits */ 1368 /* ep is already gone from udev->ep_{in,out}[]; no more submits */
1369 rescan: 1369 rescan:
1370 spin_lock (&hcd_data_lock); 1370 spin_lock (&hcd_data_lock);
1371 list_for_each_entry (urb, &ep->urb_list, urb_list) { 1371 list_for_each_entry (urb, &ep->urb_list, urb_list) {
1372 int tmp; 1372 int tmp;
1373 1373
1374 /* another cpu may be in hcd, spinning on hcd_data_lock 1374 /* another cpu may be in hcd, spinning on hcd_data_lock
1375 * to giveback() this urb. the races here should be 1375 * to giveback() this urb. the races here should be
1376 * small, but a full fix needs a new "can't submit" 1376 * small, but a full fix needs a new "can't submit"
1377 * urb state. 1377 * urb state.
1378 * FIXME urb->reject should allow that... 1378 * FIXME urb->reject should allow that...
1379 */ 1379 */
1380 if (urb->status != -EINPROGRESS) 1380 if (urb->status != -EINPROGRESS)
1381 continue; 1381 continue;
1382 usb_get_urb (urb); 1382 usb_get_urb (urb);
1383 spin_unlock (&hcd_data_lock); 1383 spin_unlock (&hcd_data_lock);
1384 1384
1385 spin_lock (&urb->lock); 1385 spin_lock (&urb->lock);
1386 tmp = urb->status; 1386 tmp = urb->status;
1387 if (tmp == -EINPROGRESS) 1387 if (tmp == -EINPROGRESS)
1388 urb->status = -ESHUTDOWN; 1388 urb->status = -ESHUTDOWN;
1389 spin_unlock (&urb->lock); 1389 spin_unlock (&urb->lock);
1390 1390
1391 /* kick hcd unless it's already returning this */ 1391 /* kick hcd unless it's already returning this */
1392 if (tmp == -EINPROGRESS) { 1392 if (tmp == -EINPROGRESS) {
1393 tmp = urb->pipe; 1393 tmp = urb->pipe;
1394 unlink1 (hcd, urb); 1394 unlink1 (hcd, urb);
1395 dev_dbg (hcd->self.controller, 1395 dev_dbg (hcd->self.controller,
1396 "shutdown urb %p pipe %08x ep%d%s%s\n", 1396 "shutdown urb %p pipe %08x ep%d%s%s\n",
1397 urb, tmp, usb_pipeendpoint (tmp), 1397 urb, tmp, usb_pipeendpoint (tmp),
1398 (tmp & USB_DIR_IN) ? "in" : "out", 1398 (tmp & USB_DIR_IN) ? "in" : "out",
1399 ({ char *s; \ 1399 ({ char *s; \
1400 switch (usb_pipetype (tmp)) { \ 1400 switch (usb_pipetype (tmp)) { \
1401 case PIPE_CONTROL: s = ""; break; \ 1401 case PIPE_CONTROL: s = ""; break; \
1402 case PIPE_BULK: s = "-bulk"; break; \ 1402 case PIPE_BULK: s = "-bulk"; break; \
1403 case PIPE_INTERRUPT: s = "-intr"; break; \ 1403 case PIPE_INTERRUPT: s = "-intr"; break; \
1404 default: s = "-iso"; break; \ 1404 default: s = "-iso"; break; \
1405 }; s;})); 1405 }; s;}));
1406 } 1406 }
1407 usb_put_urb (urb); 1407 usb_put_urb (urb);
1408 1408
1409 /* list contents may have changed */ 1409 /* list contents may have changed */
1410 goto rescan; 1410 goto rescan;
1411 } 1411 }
1412 spin_unlock (&hcd_data_lock); 1412 spin_unlock (&hcd_data_lock);
1413 local_irq_enable (); 1413 local_irq_enable ();
1414 1414
1415 /* synchronize with the hardware, so old configuration state 1415 /* synchronize with the hardware, so old configuration state
1416 * clears out immediately (and will be freed). 1416 * clears out immediately (and will be freed).
1417 */ 1417 */
1418 might_sleep (); 1418 might_sleep ();
1419 if (hcd->driver->endpoint_disable) 1419 if (hcd->driver->endpoint_disable)
1420 hcd->driver->endpoint_disable (hcd, ep); 1420 hcd->driver->endpoint_disable (hcd, ep);
1421 } 1421 }
1422 1422
1423 /*-------------------------------------------------------------------------*/ 1423 /*-------------------------------------------------------------------------*/
1424 1424
1425 #ifdef CONFIG_PM 1425 #ifdef CONFIG_PM
1426 1426
1427 int hcd_bus_suspend (struct usb_bus *bus) 1427 int hcd_bus_suspend (struct usb_bus *bus)
1428 { 1428 {
1429 struct usb_hcd *hcd; 1429 struct usb_hcd *hcd;
1430 int status; 1430 int status;
1431 1431
1432 hcd = container_of (bus, struct usb_hcd, self); 1432 hcd = container_of (bus, struct usb_hcd, self);
1433 if (!hcd->driver->bus_suspend) 1433 if (!hcd->driver->bus_suspend)
1434 return -ENOENT; 1434 return -ENOENT;
1435 hcd->state = HC_STATE_QUIESCING; 1435 hcd->state = HC_STATE_QUIESCING;
1436 status = hcd->driver->bus_suspend (hcd); 1436 status = hcd->driver->bus_suspend (hcd);
1437 if (status == 0) 1437 if (status == 0)
1438 hcd->state = HC_STATE_SUSPENDED; 1438 hcd->state = HC_STATE_SUSPENDED;
1439 else 1439 else
1440 dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n", 1440 dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
1441 "suspend", status); 1441 "suspend", status);
1442 return status; 1442 return status;
1443 } 1443 }
1444 1444
1445 int hcd_bus_resume (struct usb_bus *bus) 1445 int hcd_bus_resume (struct usb_bus *bus)
1446 { 1446 {
1447 struct usb_hcd *hcd; 1447 struct usb_hcd *hcd;
1448 int status; 1448 int status;
1449 1449
1450 hcd = container_of (bus, struct usb_hcd, self); 1450 hcd = container_of (bus, struct usb_hcd, self);
1451 if (!hcd->driver->bus_resume) 1451 if (!hcd->driver->bus_resume)
1452 return -ENOENT; 1452 return -ENOENT;
1453 if (hcd->state == HC_STATE_RUNNING) 1453 if (hcd->state == HC_STATE_RUNNING)
1454 return 0; 1454 return 0;
1455 hcd->state = HC_STATE_RESUMING; 1455 hcd->state = HC_STATE_RESUMING;
1456 status = hcd->driver->bus_resume (hcd); 1456 status = hcd->driver->bus_resume (hcd);
1457 if (status == 0) 1457 if (status == 0)
1458 hcd->state = HC_STATE_RUNNING; 1458 hcd->state = HC_STATE_RUNNING;
1459 else { 1459 else {
1460 dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n", 1460 dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
1461 "resume", status); 1461 "resume", status);
1462 usb_hc_died(hcd); 1462 usb_hc_died(hcd);
1463 } 1463 }
1464 return status; 1464 return status;
1465 } 1465 }
1466 1466
1467 /* 1467 /*
1468 * usb_hcd_suspend_root_hub - HCD autosuspends downstream ports 1468 * usb_hcd_suspend_root_hub - HCD autosuspends downstream ports
1469 * @hcd: host controller for this root hub 1469 * @hcd: host controller for this root hub
1470 * 1470 *
1471 * This call arranges that usb_hcd_resume_root_hub() is safe to call later; 1471 * This call arranges that usb_hcd_resume_root_hub() is safe to call later;
1472 * that the HCD's root hub polling is deactivated; and that the root's hub 1472 * that the HCD's root hub polling is deactivated; and that the root's hub
1473 * driver is suspended. HCDs may call this to autosuspend when their root 1473 * driver is suspended. HCDs may call this to autosuspend when their root
1474 * hub's downstream ports are all inactive: unpowered, disconnected, 1474 * hub's downstream ports are all inactive: unpowered, disconnected,
1475 * disabled, or suspended. 1475 * disabled, or suspended.
1476 * 1476 *
1477 * The HCD will autoresume on device connect change detection (using SRP 1477 * The HCD will autoresume on device connect change detection (using SRP
1478 * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling 1478 * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling
1479 * from any ports that are suspended (if that is enabled). In most cases, 1479 * from any ports that are suspended (if that is enabled). In most cases,
1480 * overcurrent signaling (on powered ports) will also start autoresume. 1480 * overcurrent signaling (on powered ports) will also start autoresume.
1481 * 1481 *
1482 * Always called with IRQs blocked. 1482 * Always called with IRQs blocked.
1483 */ 1483 */
1484 void usb_hcd_suspend_root_hub (struct usb_hcd *hcd) 1484 void usb_hcd_suspend_root_hub (struct usb_hcd *hcd)
1485 { 1485 {
1486 struct urb *urb; 1486 struct urb *urb;
1487 1487
1488 spin_lock (&hcd_root_hub_lock); 1488 spin_lock (&hcd_root_hub_lock);
1489 usb_suspend_root_hub (hcd->self.root_hub); 1489 usb_suspend_root_hub (hcd->self.root_hub);
1490 1490
1491 /* force status urb to complete/unlink while suspended */ 1491 /* force status urb to complete/unlink while suspended */
1492 if (hcd->status_urb) { 1492 if (hcd->status_urb) {
1493 urb = hcd->status_urb; 1493 urb = hcd->status_urb;
1494 urb->status = -ECONNRESET; 1494 urb->status = -ECONNRESET;
1495 urb->hcpriv = NULL; 1495 urb->hcpriv = NULL;
1496 urb->actual_length = 0; 1496 urb->actual_length = 0;
1497 1497
1498 del_timer (&hcd->rh_timer); 1498 del_timer (&hcd->rh_timer);
1499 hcd->poll_pending = 0; 1499 hcd->poll_pending = 0;
1500 hcd->status_urb = NULL; 1500 hcd->status_urb = NULL;
1501 } else 1501 } else
1502 urb = NULL; 1502 urb = NULL;
1503 spin_unlock (&hcd_root_hub_lock); 1503 spin_unlock (&hcd_root_hub_lock);
1504 hcd->state = HC_STATE_SUSPENDED; 1504 hcd->state = HC_STATE_SUSPENDED;
1505 1505
1506 if (urb) 1506 if (urb)
1507 usb_hcd_giveback_urb (hcd, urb, NULL); 1507 usb_hcd_giveback_urb (hcd, urb, NULL);
1508 } 1508 }
1509 EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub); 1509 EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub);
1510 1510
1511 /** 1511 /**
1512 * usb_hcd_resume_root_hub - called by HCD to resume its root hub 1512 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1513 * @hcd: host controller for this root hub 1513 * @hcd: host controller for this root hub
1514 * 1514 *
1515 * The USB host controller calls this function when its root hub is 1515 * The USB host controller calls this function when its root hub is
1516 * suspended (with the remote wakeup feature enabled) and a remote 1516 * suspended (with the remote wakeup feature enabled) and a remote
1517 * wakeup request is received. It queues a request for khubd to 1517 * wakeup request is received. It queues a request for khubd to
1518 * resume the root hub (that is, manage its downstream ports again). 1518 * resume the root hub (that is, manage its downstream ports again).
1519 */ 1519 */
1520 void usb_hcd_resume_root_hub (struct usb_hcd *hcd) 1520 void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1521 { 1521 {
1522 unsigned long flags; 1522 unsigned long flags;
1523 1523
1524 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1524 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1525 if (hcd->rh_registered) 1525 if (hcd->rh_registered)
1526 usb_resume_root_hub (hcd->self.root_hub); 1526 usb_resume_root_hub (hcd->self.root_hub);
1527 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1527 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1528 } 1528 }
1529 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 1529 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1530 1530
1531 #endif 1531 #endif
1532 1532
1533 /*-------------------------------------------------------------------------*/ 1533 /*-------------------------------------------------------------------------*/
1534 1534
1535 #ifdef CONFIG_USB_OTG 1535 #ifdef CONFIG_USB_OTG
1536 1536
1537 /** 1537 /**
1538 * usb_bus_start_enum - start immediate enumeration (for OTG) 1538 * usb_bus_start_enum - start immediate enumeration (for OTG)
1539 * @bus: the bus (must use hcd framework) 1539 * @bus: the bus (must use hcd framework)
1540 * @port_num: 1-based number of port; usually bus->otg_port 1540 * @port_num: 1-based number of port; usually bus->otg_port
1541 * Context: in_interrupt() 1541 * Context: in_interrupt()
1542 * 1542 *
1543 * Starts enumeration, with an immediate reset followed later by 1543 * Starts enumeration, with an immediate reset followed later by
1544 * khubd identifying and possibly configuring the device. 1544 * khubd identifying and possibly configuring the device.
1545 * This is needed by OTG controller drivers, where it helps meet 1545 * This is needed by OTG controller drivers, where it helps meet
1546 * HNP protocol timing requirements for starting a port reset. 1546 * HNP protocol timing requirements for starting a port reset.
1547 */ 1547 */
1548 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) 1548 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1549 { 1549 {
1550 struct usb_hcd *hcd; 1550 struct usb_hcd *hcd;
1551 int status = -EOPNOTSUPP; 1551 int status = -EOPNOTSUPP;
1552 1552
1553 /* NOTE: since HNP can't start by grabbing the bus's address0_sem, 1553 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1554 * boards with root hubs hooked up to internal devices (instead of 1554 * boards with root hubs hooked up to internal devices (instead of
1555 * just the OTG port) may need more attention to resetting... 1555 * just the OTG port) may need more attention to resetting...
1556 */ 1556 */
1557 hcd = container_of (bus, struct usb_hcd, self); 1557 hcd = container_of (bus, struct usb_hcd, self);
1558 if (port_num && hcd->driver->start_port_reset) 1558 if (port_num && hcd->driver->start_port_reset)
1559 status = hcd->driver->start_port_reset(hcd, port_num); 1559 status = hcd->driver->start_port_reset(hcd, port_num);
1560 1560
1561 /* run khubd shortly after (first) root port reset finishes; 1561 /* run khubd shortly after (first) root port reset finishes;
1562 * it may issue others, until at least 50 msecs have passed. 1562 * it may issue others, until at least 50 msecs have passed.
1563 */ 1563 */
1564 if (status == 0) 1564 if (status == 0)
1565 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); 1565 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1566 return status; 1566 return status;
1567 } 1567 }
1568 EXPORT_SYMBOL (usb_bus_start_enum); 1568 EXPORT_SYMBOL (usb_bus_start_enum);
1569 1569
1570 #endif 1570 #endif
1571 1571
1572 /*-------------------------------------------------------------------------*/ 1572 /*-------------------------------------------------------------------------*/
1573 1573
1574 /* 1574 /*
1575 * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue) 1575 * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
1576 */ 1576 */
1577 static struct usb_operations usb_hcd_operations = { 1577 static struct usb_operations usb_hcd_operations = {
1578 .get_frame_number = hcd_get_frame_number, 1578 .get_frame_number = hcd_get_frame_number,
1579 .submit_urb = hcd_submit_urb, 1579 .submit_urb = hcd_submit_urb,
1580 .unlink_urb = hcd_unlink_urb, 1580 .unlink_urb = hcd_unlink_urb,
1581 .buffer_alloc = hcd_buffer_alloc, 1581 .buffer_alloc = hcd_buffer_alloc,
1582 .buffer_free = hcd_buffer_free, 1582 .buffer_free = hcd_buffer_free,
1583 .disable = hcd_endpoint_disable, 1583 .disable = hcd_endpoint_disable,
1584 }; 1584 };
1585 1585
1586 /*-------------------------------------------------------------------------*/ 1586 /*-------------------------------------------------------------------------*/
1587 1587
1588 /** 1588 /**
1589 * usb_hcd_giveback_urb - return URB from HCD to device driver 1589 * usb_hcd_giveback_urb - return URB from HCD to device driver
1590 * @hcd: host controller returning the URB 1590 * @hcd: host controller returning the URB
1591 * @urb: urb being returned to the USB device driver. 1591 * @urb: urb being returned to the USB device driver.
1592 * @regs: pt_regs, passed down to the URB completion handler 1592 * @regs: pt_regs, passed down to the URB completion handler
1593 * Context: in_interrupt() 1593 * Context: in_interrupt()
1594 * 1594 *
1595 * This hands the URB from HCD to its USB device driver, using its 1595 * This hands the URB from HCD to its USB device driver, using its
1596 * completion function. The HCD has freed all per-urb resources 1596 * completion function. The HCD has freed all per-urb resources
1597 * (and is done using urb->hcpriv). It also released all HCD locks; 1597 * (and is done using urb->hcpriv). It also released all HCD locks;
1598 * the device driver won't cause problems if it frees, modifies, 1598 * the device driver won't cause problems if it frees, modifies,
1599 * or resubmits this URB. 1599 * or resubmits this URB.
1600 */ 1600 */
1601 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs) 1601 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1602 { 1602 {
1603 int at_root_hub; 1603 int at_root_hub;
1604 1604
1605 at_root_hub = (urb->dev == hcd->self.root_hub); 1605 at_root_hub = (urb->dev == hcd->self.root_hub);
1606 urb_unlink (urb); 1606 urb_unlink (urb);
1607 1607
1608 /* lower level hcd code should use *_dma exclusively */ 1608 /* lower level hcd code should use *_dma exclusively */
1609 if (hcd->self.controller->dma_mask && !at_root_hub) { 1609 if (hcd->self.controller->dma_mask && !at_root_hub) {
1610 if (usb_pipecontrol (urb->pipe) 1610 if (usb_pipecontrol (urb->pipe)
1611 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) 1611 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1612 dma_unmap_single (hcd->self.controller, urb->setup_dma, 1612 dma_unmap_single (hcd->self.controller, urb->setup_dma,
1613 sizeof (struct usb_ctrlrequest), 1613 sizeof (struct usb_ctrlrequest),
1614 DMA_TO_DEVICE); 1614 DMA_TO_DEVICE);
1615 if (urb->transfer_buffer_length != 0 1615 if (urb->transfer_buffer_length != 0
1616 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) 1616 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1617 dma_unmap_single (hcd->self.controller, 1617 dma_unmap_single (hcd->self.controller,
1618 urb->transfer_dma, 1618 urb->transfer_dma,
1619 urb->transfer_buffer_length, 1619 urb->transfer_buffer_length,
1620 usb_pipein (urb->pipe) 1620 usb_pipein (urb->pipe)
1621 ? DMA_FROM_DEVICE 1621 ? DMA_FROM_DEVICE
1622 : DMA_TO_DEVICE); 1622 : DMA_TO_DEVICE);
1623 } 1623 }
1624 1624
1625 usbmon_urb_complete (&hcd->self, urb); 1625 usbmon_urb_complete (&hcd->self, urb);
1626 /* pass ownership to the completion handler */ 1626 /* pass ownership to the completion handler */
1627 urb->complete (urb, regs); 1627 urb->complete (urb, regs);
1628 atomic_dec (&urb->use_count); 1628 atomic_dec (&urb->use_count);
1629 if (unlikely (urb->reject)) 1629 if (unlikely (urb->reject))
1630 wake_up (&usb_kill_urb_queue); 1630 wake_up (&usb_kill_urb_queue);
1631 usb_put_urb (urb); 1631 usb_put_urb (urb);
1632 } 1632 }
1633 EXPORT_SYMBOL (usb_hcd_giveback_urb); 1633 EXPORT_SYMBOL (usb_hcd_giveback_urb);
1634 1634
1635 /*-------------------------------------------------------------------------*/ 1635 /*-------------------------------------------------------------------------*/
1636 1636
1637 /** 1637 /**
1638 * usb_hcd_irq - hook IRQs to HCD framework (bus glue) 1638 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1639 * @irq: the IRQ being raised 1639 * @irq: the IRQ being raised
1640 * @__hcd: pointer to the HCD whose IRQ is being signaled 1640 * @__hcd: pointer to the HCD whose IRQ is being signaled
1641 * @r: saved hardware registers 1641 * @r: saved hardware registers
1642 * 1642 *
1643 * If the controller isn't HALTed, calls the driver's irq handler. 1643 * If the controller isn't HALTed, calls the driver's irq handler.
1644 * Checks whether the controller is now dead. 1644 * Checks whether the controller is now dead.
1645 */ 1645 */
1646 irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r) 1646 irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1647 { 1647 {
1648 struct usb_hcd *hcd = __hcd; 1648 struct usb_hcd *hcd = __hcd;
1649 int start = hcd->state; 1649 int start = hcd->state;
1650 1650
1651 if (unlikely(start == HC_STATE_HALT || 1651 if (unlikely(start == HC_STATE_HALT ||
1652 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 1652 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
1653 return IRQ_NONE; 1653 return IRQ_NONE;
1654 if (hcd->driver->irq (hcd, r) == IRQ_NONE) 1654 if (hcd->driver->irq (hcd, r) == IRQ_NONE)
1655 return IRQ_NONE; 1655 return IRQ_NONE;
1656 1656
1657 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); 1657 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1658 1658
1659 if (unlikely(hcd->state == HC_STATE_HALT)) 1659 if (unlikely(hcd->state == HC_STATE_HALT))
1660 usb_hc_died (hcd); 1660 usb_hc_died (hcd);
1661 return IRQ_HANDLED; 1661 return IRQ_HANDLED;
1662 } 1662 }
1663 1663
1664 /*-------------------------------------------------------------------------*/ 1664 /*-------------------------------------------------------------------------*/
1665 1665
1666 /** 1666 /**
1667 * usb_hc_died - report abnormal shutdown of a host controller (bus glue) 1667 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1668 * @hcd: pointer to the HCD representing the controller 1668 * @hcd: pointer to the HCD representing the controller
1669 * 1669 *
1670 * This is called by bus glue to report a USB host controller that died 1670 * This is called by bus glue to report a USB host controller that died
1671 * while operations may still have been pending. It's called automatically 1671 * while operations may still have been pending. It's called automatically
1672 * by the PCI glue, so only glue for non-PCI busses should need to call it. 1672 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1673 */ 1673 */
1674 void usb_hc_died (struct usb_hcd *hcd) 1674 void usb_hc_died (struct usb_hcd *hcd)
1675 { 1675 {
1676 unsigned long flags; 1676 unsigned long flags;
1677 1677
1678 dev_err (hcd->self.controller, "HC died; cleaning up\n"); 1678 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1679 1679
1680 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1680 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1681 if (hcd->rh_registered) { 1681 if (hcd->rh_registered) {
1682 hcd->poll_rh = 0; 1682 hcd->poll_rh = 0;
1683 1683
1684 /* make khubd clean up old urbs and devices */ 1684 /* make khubd clean up old urbs and devices */
1685 usb_set_device_state (hcd->self.root_hub, 1685 usb_set_device_state (hcd->self.root_hub,
1686 USB_STATE_NOTATTACHED); 1686 USB_STATE_NOTATTACHED);
1687 usb_kick_khubd (hcd->self.root_hub); 1687 usb_kick_khubd (hcd->self.root_hub);
1688 } 1688 }
1689 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1689 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1690 } 1690 }
1691 EXPORT_SYMBOL_GPL (usb_hc_died); 1691 EXPORT_SYMBOL_GPL (usb_hc_died);
1692 1692
1693 /*-------------------------------------------------------------------------*/ 1693 /*-------------------------------------------------------------------------*/
1694 1694
1695 static void hcd_release (struct usb_bus *bus) 1695 static void hcd_release (struct usb_bus *bus)
1696 { 1696 {
1697 struct usb_hcd *hcd; 1697 struct usb_hcd *hcd;
1698 1698
1699 hcd = container_of(bus, struct usb_hcd, self); 1699 hcd = container_of(bus, struct usb_hcd, self);
1700 kfree(hcd); 1700 kfree(hcd);
1701 } 1701 }
1702 1702
1703 /** 1703 /**
1704 * usb_create_hcd - create and initialize an HCD structure 1704 * usb_create_hcd - create and initialize an HCD structure
1705 * @driver: HC driver that will use this hcd 1705 * @driver: HC driver that will use this hcd
1706 * @dev: device for this HC, stored in hcd->self.controller 1706 * @dev: device for this HC, stored in hcd->self.controller
1707 * @bus_name: value to store in hcd->self.bus_name 1707 * @bus_name: value to store in hcd->self.bus_name
1708 * Context: !in_interrupt() 1708 * Context: !in_interrupt()
1709 * 1709 *
1710 * Allocate a struct usb_hcd, with extra space at the end for the 1710 * Allocate a struct usb_hcd, with extra space at the end for the
1711 * HC driver's private data. Initialize the generic members of the 1711 * HC driver's private data. Initialize the generic members of the
1712 * hcd structure. 1712 * hcd structure.
1713 * 1713 *
1714 * If memory is unavailable, returns NULL. 1714 * If memory is unavailable, returns NULL.
1715 */ 1715 */
1716 struct usb_hcd *usb_create_hcd (const struct hc_driver *driver, 1716 struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1717 struct device *dev, char *bus_name) 1717 struct device *dev, char *bus_name)
1718 { 1718 {
1719 struct usb_hcd *hcd; 1719 struct usb_hcd *hcd;
1720 1720
1721 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); 1721 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
1722 if (!hcd) { 1722 if (!hcd) {
1723 dev_dbg (dev, "hcd alloc failed\n"); 1723 dev_dbg (dev, "hcd alloc failed\n");
1724 return NULL; 1724 return NULL;
1725 } 1725 }
1726 dev_set_drvdata(dev, hcd); 1726 dev_set_drvdata(dev, hcd);
1727 1727
1728 usb_bus_init(&hcd->self); 1728 usb_bus_init(&hcd->self);
1729 hcd->self.op = &usb_hcd_operations; 1729 hcd->self.op = &usb_hcd_operations;
1730 hcd->self.hcpriv = hcd; 1730 hcd->self.hcpriv = hcd;
1731 hcd->self.release = &hcd_release; 1731 hcd->self.release = &hcd_release;
1732 hcd->self.controller = dev; 1732 hcd->self.controller = dev;
1733 hcd->self.bus_name = bus_name; 1733 hcd->self.bus_name = bus_name;
1734 1734
1735 init_timer(&hcd->rh_timer); 1735 init_timer(&hcd->rh_timer);
1736 hcd->rh_timer.function = rh_timer_func; 1736 hcd->rh_timer.function = rh_timer_func;
1737 hcd->rh_timer.data = (unsigned long) hcd; 1737 hcd->rh_timer.data = (unsigned long) hcd;
1738 1738
1739 hcd->driver = driver; 1739 hcd->driver = driver;
1740 hcd->product_desc = (driver->product_desc) ? driver->product_desc : 1740 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
1741 "USB Host Controller"; 1741 "USB Host Controller";
1742 1742
1743 return hcd; 1743 return hcd;
1744 } 1744 }
1745 EXPORT_SYMBOL (usb_create_hcd); 1745 EXPORT_SYMBOL (usb_create_hcd);
1746 1746
1747 void usb_put_hcd (struct usb_hcd *hcd) 1747 void usb_put_hcd (struct usb_hcd *hcd)
1748 { 1748 {
1749 dev_set_drvdata(hcd->self.controller, NULL); 1749 dev_set_drvdata(hcd->self.controller, NULL);
1750 usb_bus_put(&hcd->self); 1750 usb_bus_put(&hcd->self);
1751 } 1751 }
1752 EXPORT_SYMBOL (usb_put_hcd); 1752 EXPORT_SYMBOL (usb_put_hcd);
1753 1753
1754 /** 1754 /**
1755 * usb_add_hcd - finish generic HCD structure initialization and register 1755 * usb_add_hcd - finish generic HCD structure initialization and register
1756 * @hcd: the usb_hcd structure to initialize 1756 * @hcd: the usb_hcd structure to initialize
1757 * @irqnum: Interrupt line to allocate 1757 * @irqnum: Interrupt line to allocate
1758 * @irqflags: Interrupt type flags 1758 * @irqflags: Interrupt type flags
1759 * 1759 *
1760 * Finish the remaining parts of generic HCD initialization: allocate the 1760 * Finish the remaining parts of generic HCD initialization: allocate the
1761 * buffers of consistent memory, register the bus, request the IRQ line, 1761 * buffers of consistent memory, register the bus, request the IRQ line,
1762 * and call the driver's reset() and start() routines. 1762 * and call the driver's reset() and start() routines.
1763 */ 1763 */
1764 int usb_add_hcd(struct usb_hcd *hcd, 1764 int usb_add_hcd(struct usb_hcd *hcd,
1765 unsigned int irqnum, unsigned long irqflags) 1765 unsigned int irqnum, unsigned long irqflags)
1766 { 1766 {
1767 int retval; 1767 int retval;
1768 struct usb_device *rhdev; 1768 struct usb_device *rhdev;
1769 1769
1770 dev_info(hcd->self.controller, "%s\n", hcd->product_desc); 1770 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
1771 1771
1772 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1772 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1773 1773
1774 /* till now HC has been in an indeterminate state ... */ 1774 /* till now HC has been in an indeterminate state ... */
1775 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { 1775 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
1776 dev_err(hcd->self.controller, "can't reset\n"); 1776 dev_err(hcd->self.controller, "can't reset\n");
1777 return retval; 1777 return retval;
1778 } 1778 }
1779 1779
1780 if ((retval = hcd_buffer_create(hcd)) != 0) { 1780 if ((retval = hcd_buffer_create(hcd)) != 0) {
1781 dev_dbg(hcd->self.controller, "pool alloc failed\n"); 1781 dev_dbg(hcd->self.controller, "pool alloc failed\n");
1782 return retval; 1782 return retval;
1783 } 1783 }
1784 1784
1785 if ((retval = usb_register_bus(&hcd->self)) < 0) 1785 if ((retval = usb_register_bus(&hcd->self)) < 0)
1786 goto err_register_bus; 1786 goto err_register_bus;
1787 1787
1788 if (hcd->driver->irq) { 1788 if (hcd->driver->irq) {
1789 char buf[8], *bufp = buf; 1789 char buf[8], *bufp = buf;
1790 1790
1791 #ifdef __sparc__ 1791 #ifdef __sparc__
1792 bufp = __irq_itoa(irqnum); 1792 bufp = __irq_itoa(irqnum);
1793 #else 1793 #else
1794 sprintf(buf, "%d", irqnum); 1794 sprintf(buf, "%d", irqnum);
1795 #endif 1795 #endif
1796 1796
1797 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", 1797 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
1798 hcd->driver->description, hcd->self.busnum); 1798 hcd->driver->description, hcd->self.busnum);
1799 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags, 1799 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
1800 hcd->irq_descr, hcd)) != 0) { 1800 hcd->irq_descr, hcd)) != 0) {
1801 dev_err(hcd->self.controller, 1801 dev_err(hcd->self.controller,
1802 "request interrupt %s failed\n", bufp); 1802 "request interrupt %s failed\n", bufp);
1803 goto err_request_irq; 1803 goto err_request_irq;
1804 } 1804 }
1805 hcd->irq = irqnum; 1805 hcd->irq = irqnum;
1806 dev_info(hcd->self.controller, "irq %s, %s 0x%08llx\n", bufp, 1806 dev_info(hcd->self.controller, "irq %s, %s 0x%08llx\n", bufp,
1807 (hcd->driver->flags & HCD_MEMORY) ? 1807 (hcd->driver->flags & HCD_MEMORY) ?
1808 "io mem" : "io base", 1808 "io mem" : "io base",
1809 (unsigned long long)hcd->rsrc_start); 1809 (unsigned long long)hcd->rsrc_start);
1810 } else { 1810 } else {
1811 hcd->irq = -1; 1811 hcd->irq = -1;
1812 if (hcd->rsrc_start) 1812 if (hcd->rsrc_start)
1813 dev_info(hcd->self.controller, "%s 0x%08llx\n", 1813 dev_info(hcd->self.controller, "%s 0x%08llx\n",
1814 (hcd->driver->flags & HCD_MEMORY) ? 1814 (hcd->driver->flags & HCD_MEMORY) ?
1815 "io mem" : "io base", 1815 "io mem" : "io base",
1816 (unsigned long long)hcd->rsrc_start); 1816 (unsigned long long)hcd->rsrc_start);
1817 } 1817 }
1818 1818
1819 /* Allocate the root hub before calling hcd->driver->start(), 1819 /* Allocate the root hub before calling hcd->driver->start(),
1820 * but don't register it until afterward so that the hardware 1820 * but don't register it until afterward so that the hardware
1821 * is running. 1821 * is running.
1822 */ 1822 */
1823 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) { 1823 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1824 dev_err(hcd->self.controller, "unable to allocate root hub\n"); 1824 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1825 retval = -ENOMEM; 1825 retval = -ENOMEM;
1826 goto err_allocate_root_hub; 1826 goto err_allocate_root_hub;
1827 } 1827 }
1828 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1829 USB_SPEED_FULL;
1830 1828
1831 /* Although in principle hcd->driver->start() might need to use rhdev, 1829 /* Although in principle hcd->driver->start() might need to use rhdev,
1832 * none of the current drivers do. 1830 * none of the current drivers do.
1833 */ 1831 */
1834 if ((retval = hcd->driver->start(hcd)) < 0) { 1832 if ((retval = hcd->driver->start(hcd)) < 0) {
1835 dev_err(hcd->self.controller, "startup error %d\n", retval); 1833 dev_err(hcd->self.controller, "startup error %d\n", retval);
1836 goto err_hcd_driver_start; 1834 goto err_hcd_driver_start;
1837 } 1835 }
1838 1836
1839 /* hcd->driver->start() reported can_wakeup, probably with 1837 /* hcd->driver->start() reported can_wakeup, probably with
1840 * assistance from board's boot firmware. 1838 * assistance from board's boot firmware.
1841 * NOTE: normal devices won't enable wakeup by default. 1839 * NOTE: normal devices won't enable wakeup by default.
1842 */ 1840 */
1843 if (hcd->can_wakeup) 1841 if (hcd->can_wakeup)
1844 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); 1842 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
1845 hcd->remote_wakeup = hcd->can_wakeup; 1843 hcd->remote_wakeup = hcd->can_wakeup;
1846 1844
1845 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1846 USB_SPEED_FULL;
1847 rhdev->bus_mA = min(500u, hcd->power_budget);
1847 if ((retval = register_root_hub(rhdev, hcd)) != 0) 1848 if ((retval = register_root_hub(rhdev, hcd)) != 0)
1848 goto err_register_root_hub; 1849 goto err_register_root_hub;
1849 1850
1850 if (hcd->uses_new_polling && hcd->poll_rh) 1851 if (hcd->uses_new_polling && hcd->poll_rh)
1851 usb_hcd_poll_rh_status(hcd); 1852 usb_hcd_poll_rh_status(hcd);
1852 return retval; 1853 return retval;
1853 1854
1854 err_register_root_hub: 1855 err_register_root_hub:
1855 hcd->driver->stop(hcd); 1856 hcd->driver->stop(hcd);
1856 1857
1857 err_hcd_driver_start: 1858 err_hcd_driver_start:
1858 usb_put_dev(rhdev); 1859 usb_put_dev(rhdev);
1859 1860
1860 err_allocate_root_hub: 1861 err_allocate_root_hub:
1861 if (hcd->irq >= 0) 1862 if (hcd->irq >= 0)
1862 free_irq(irqnum, hcd); 1863 free_irq(irqnum, hcd);
1863 1864
1864 err_request_irq: 1865 err_request_irq:
1865 usb_deregister_bus(&hcd->self); 1866 usb_deregister_bus(&hcd->self);
1866 1867
1867 err_register_bus: 1868 err_register_bus:
1868 hcd_buffer_destroy(hcd); 1869 hcd_buffer_destroy(hcd);
1869 return retval; 1870 return retval;
1870 } 1871 }
1871 EXPORT_SYMBOL (usb_add_hcd); 1872 EXPORT_SYMBOL (usb_add_hcd);
1872 1873
1873 /** 1874 /**
1874 * usb_remove_hcd - shutdown processing for generic HCDs 1875 * usb_remove_hcd - shutdown processing for generic HCDs
1875 * @hcd: the usb_hcd structure to remove 1876 * @hcd: the usb_hcd structure to remove
1876 * Context: !in_interrupt() 1877 * Context: !in_interrupt()
1877 * 1878 *
1878 * Disconnects the root hub, then reverses the effects of usb_add_hcd(), 1879 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
1879 * invoking the HCD's stop() method. 1880 * invoking the HCD's stop() method.
1880 */ 1881 */
1881 void usb_remove_hcd(struct usb_hcd *hcd) 1882 void usb_remove_hcd(struct usb_hcd *hcd)
1882 { 1883 {
1883 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); 1884 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
1884 1885
1885 if (HC_IS_RUNNING (hcd->state)) 1886 if (HC_IS_RUNNING (hcd->state))
1886 hcd->state = HC_STATE_QUIESCING; 1887 hcd->state = HC_STATE_QUIESCING;
1887 1888
1888 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); 1889 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
1889 spin_lock_irq (&hcd_root_hub_lock); 1890 spin_lock_irq (&hcd_root_hub_lock);
1890 hcd->rh_registered = 0; 1891 hcd->rh_registered = 0;
1891 spin_unlock_irq (&hcd_root_hub_lock); 1892 spin_unlock_irq (&hcd_root_hub_lock);
1892 1893
1893 down(&usb_bus_list_lock); 1894 down(&usb_bus_list_lock);
1894 usb_disconnect(&hcd->self.root_hub); 1895 usb_disconnect(&hcd->self.root_hub);
1895 up(&usb_bus_list_lock); 1896 up(&usb_bus_list_lock);
1896 1897
1897 hcd->poll_rh = 0; 1898 hcd->poll_rh = 0;
1898 del_timer_sync(&hcd->rh_timer); 1899 del_timer_sync(&hcd->rh_timer);
1899 1900
1900 hcd->driver->stop(hcd); 1901 hcd->driver->stop(hcd);
1901 hcd->state = HC_STATE_HALT; 1902 hcd->state = HC_STATE_HALT;
1902 1903
1903 if (hcd->irq >= 0) 1904 if (hcd->irq >= 0)
1904 free_irq(hcd->irq, hcd); 1905 free_irq(hcd->irq, hcd);
1905 usb_deregister_bus(&hcd->self); 1906 usb_deregister_bus(&hcd->self);
1906 hcd_buffer_destroy(hcd); 1907 hcd_buffer_destroy(hcd);
1907 } 1908 }
1908 EXPORT_SYMBOL (usb_remove_hcd); 1909 EXPORT_SYMBOL (usb_remove_hcd);
1909 1910
1910 /*-------------------------------------------------------------------------*/ 1911 /*-------------------------------------------------------------------------*/
1911 1912
1912 #if defined(CONFIG_USB_MON) 1913 #if defined(CONFIG_USB_MON)
1913 1914
1914 struct usb_mon_operations *mon_ops; 1915 struct usb_mon_operations *mon_ops;
1915 1916
1916 /* 1917 /*
1917 * The registration is unlocked. 1918 * The registration is unlocked.
1918 * We do it this way because we do not want to lock in hot paths. 1919 * We do it this way because we do not want to lock in hot paths.
1919 * 1920 *
1920 * Notice that the code is minimally error-proof. Because usbmon needs 1921 * Notice that the code is minimally error-proof. Because usbmon needs
1921 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. 1922 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
1922 */ 1923 */
1923 1924
1924 int usb_mon_register (struct usb_mon_operations *ops) 1925 int usb_mon_register (struct usb_mon_operations *ops)
1925 { 1926 {
1926 1927
1927 if (mon_ops) 1928 if (mon_ops)
1928 return -EBUSY; 1929 return -EBUSY;
1929 1930
1930 mon_ops = ops; 1931 mon_ops = ops;
1931 mb(); 1932 mb();
1932 return 0; 1933 return 0;
1933 } 1934 }
1934 EXPORT_SYMBOL_GPL (usb_mon_register); 1935 EXPORT_SYMBOL_GPL (usb_mon_register);
1935 1936
1936 void usb_mon_deregister (void) 1937 void usb_mon_deregister (void)
1937 { 1938 {
1938 1939
1939 if (mon_ops == NULL) { 1940 if (mon_ops == NULL) {
1940 printk(KERN_ERR "USB: monitor was not registered\n"); 1941 printk(KERN_ERR "USB: monitor was not registered\n");
1941 return; 1942 return;
1942 } 1943 }
1943 mon_ops = NULL; 1944 mon_ops = NULL;
1944 mb(); 1945 mb();
1945 } 1946 }
1946 EXPORT_SYMBOL_GPL (usb_mon_deregister); 1947 EXPORT_SYMBOL_GPL (usb_mon_deregister);
1947 1948
drivers/usb/core/hub.c
1 /* 1 /*
2 * USB hub driver. 2 * USB hub driver.
3 * 3 *
4 * (C) Copyright 1999 Linus Torvalds 4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt 5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith 6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 * 8 *
9 */ 9 */
10 10
11 #include <linux/config.h> 11 #include <linux/config.h>
12 #include <linux/kernel.h> 12 #include <linux/kernel.h>
13 #include <linux/errno.h> 13 #include <linux/errno.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/moduleparam.h> 15 #include <linux/moduleparam.h>
16 #include <linux/completion.h> 16 #include <linux/completion.h>
17 #include <linux/sched.h> 17 #include <linux/sched.h>
18 #include <linux/list.h> 18 #include <linux/list.h>
19 #include <linux/slab.h> 19 #include <linux/slab.h>
20 #include <linux/smp_lock.h> 20 #include <linux/smp_lock.h>
21 #include <linux/ioctl.h> 21 #include <linux/ioctl.h>
22 #include <linux/usb.h> 22 #include <linux/usb.h>
23 #include <linux/usbdevice_fs.h> 23 #include <linux/usbdevice_fs.h>
24 #include <linux/kthread.h> 24 #include <linux/kthread.h>
25 25
26 #include <asm/semaphore.h> 26 #include <asm/semaphore.h>
27 #include <asm/uaccess.h> 27 #include <asm/uaccess.h>
28 #include <asm/byteorder.h> 28 #include <asm/byteorder.h>
29 29
30 #include "usb.h" 30 #include "usb.h"
31 #include "hcd.h" 31 #include "hcd.h"
32 #include "hub.h" 32 #include "hub.h"
33 33
34 /* Protect struct usb_device->state and ->children members 34 /* Protect struct usb_device->state and ->children members
35 * Note: Both are also protected by ->dev.sem, except that ->state can 35 * Note: Both are also protected by ->dev.sem, except that ->state can
36 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 36 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
37 static DEFINE_SPINLOCK(device_state_lock); 37 static DEFINE_SPINLOCK(device_state_lock);
38 38
39 /* khubd's worklist and its lock */ 39 /* khubd's worklist and its lock */
40 static DEFINE_SPINLOCK(hub_event_lock); 40 static DEFINE_SPINLOCK(hub_event_lock);
41 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ 41 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
42 42
43 /* Wakes up khubd */ 43 /* Wakes up khubd */
44 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); 44 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
45 45
46 static struct task_struct *khubd_task; 46 static struct task_struct *khubd_task;
47 47
48 /* cycle leds on hubs that aren't blinking for attention */ 48 /* cycle leds on hubs that aren't blinking for attention */
49 static int blinkenlights = 0; 49 static int blinkenlights = 0;
50 module_param (blinkenlights, bool, S_IRUGO); 50 module_param (blinkenlights, bool, S_IRUGO);
51 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); 51 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
52 52
53 /* 53 /*
54 * As of 2.6.10 we introduce a new USB device initialization scheme which 54 * As of 2.6.10 we introduce a new USB device initialization scheme which
55 * closely resembles the way Windows works. Hopefully it will be compatible 55 * closely resembles the way Windows works. Hopefully it will be compatible
56 * with a wider range of devices than the old scheme. However some previously 56 * with a wider range of devices than the old scheme. However some previously
57 * working devices may start giving rise to "device not accepting address" 57 * working devices may start giving rise to "device not accepting address"
58 * errors; if that happens the user can try the old scheme by adjusting the 58 * errors; if that happens the user can try the old scheme by adjusting the
59 * following module parameters. 59 * following module parameters.
60 * 60 *
61 * For maximum flexibility there are two boolean parameters to control the 61 * For maximum flexibility there are two boolean parameters to control the
62 * hub driver's behavior. On the first initialization attempt, if the 62 * hub driver's behavior. On the first initialization attempt, if the
63 * "old_scheme_first" parameter is set then the old scheme will be used, 63 * "old_scheme_first" parameter is set then the old scheme will be used,
64 * otherwise the new scheme is used. If that fails and "use_both_schemes" 64 * otherwise the new scheme is used. If that fails and "use_both_schemes"
65 * is set, then the driver will make another attempt, using the other scheme. 65 * is set, then the driver will make another attempt, using the other scheme.
66 */ 66 */
67 static int old_scheme_first = 0; 67 static int old_scheme_first = 0;
68 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 68 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(old_scheme_first, 69 MODULE_PARM_DESC(old_scheme_first,
70 "start with the old device initialization scheme"); 70 "start with the old device initialization scheme");
71 71
72 static int use_both_schemes = 1; 72 static int use_both_schemes = 1;
73 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 73 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(use_both_schemes, 74 MODULE_PARM_DESC(use_both_schemes,
75 "try the other device initialization scheme if the " 75 "try the other device initialization scheme if the "
76 "first one fails"); 76 "first one fails");
77 77
78 78
79 #ifdef DEBUG 79 #ifdef DEBUG
80 static inline char *portspeed (int portstatus) 80 static inline char *portspeed (int portstatus)
81 { 81 {
82 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED)) 82 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
83 return "480 Mb/s"; 83 return "480 Mb/s";
84 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED)) 84 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
85 return "1.5 Mb/s"; 85 return "1.5 Mb/s";
86 else 86 else
87 return "12 Mb/s"; 87 return "12 Mb/s";
88 } 88 }
89 #endif 89 #endif
90 90
91 /* Note that hdev or one of its children must be locked! */ 91 /* Note that hdev or one of its children must be locked! */
92 static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev) 92 static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev)
93 { 93 {
94 return usb_get_intfdata(hdev->actconfig->interface[0]); 94 return usb_get_intfdata(hdev->actconfig->interface[0]);
95 } 95 }
96 96
97 /* USB 2.0 spec Section 11.24.4.5 */ 97 /* USB 2.0 spec Section 11.24.4.5 */
98 static int get_hub_descriptor(struct usb_device *hdev, void *data, int size) 98 static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
99 { 99 {
100 int i, ret; 100 int i, ret;
101 101
102 for (i = 0; i < 3; i++) { 102 for (i = 0; i < 3; i++) {
103 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 103 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
104 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 104 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
105 USB_DT_HUB << 8, 0, data, size, 105 USB_DT_HUB << 8, 0, data, size,
106 USB_CTRL_GET_TIMEOUT); 106 USB_CTRL_GET_TIMEOUT);
107 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 107 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
108 return ret; 108 return ret;
109 } 109 }
110 return -EINVAL; 110 return -EINVAL;
111 } 111 }
112 112
113 /* 113 /*
114 * USB 2.0 spec Section 11.24.2.1 114 * USB 2.0 spec Section 11.24.2.1
115 */ 115 */
116 static int clear_hub_feature(struct usb_device *hdev, int feature) 116 static int clear_hub_feature(struct usb_device *hdev, int feature)
117 { 117 {
118 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 118 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
119 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 119 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
120 } 120 }
121 121
122 /* 122 /*
123 * USB 2.0 spec Section 11.24.2.2 123 * USB 2.0 spec Section 11.24.2.2
124 */ 124 */
125 static int clear_port_feature(struct usb_device *hdev, int port1, int feature) 125 static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
126 { 126 {
127 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 127 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
128 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 128 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
129 NULL, 0, 1000); 129 NULL, 0, 1000);
130 } 130 }
131 131
132 /* 132 /*
133 * USB 2.0 spec Section 11.24.2.13 133 * USB 2.0 spec Section 11.24.2.13
134 */ 134 */
135 static int set_port_feature(struct usb_device *hdev, int port1, int feature) 135 static int set_port_feature(struct usb_device *hdev, int port1, int feature)
136 { 136 {
137 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 137 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
138 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 138 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
139 NULL, 0, 1000); 139 NULL, 0, 1000);
140 } 140 }
141 141
142 /* 142 /*
143 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 143 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
144 * for info about using port indicators 144 * for info about using port indicators
145 */ 145 */
146 static void set_port_led( 146 static void set_port_led(
147 struct usb_hub *hub, 147 struct usb_hub *hub,
148 int port1, 148 int port1,
149 int selector 149 int selector
150 ) 150 )
151 { 151 {
152 int status = set_port_feature(hub->hdev, (selector << 8) | port1, 152 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
153 USB_PORT_FEAT_INDICATOR); 153 USB_PORT_FEAT_INDICATOR);
154 if (status < 0) 154 if (status < 0)
155 dev_dbg (hub->intfdev, 155 dev_dbg (hub->intfdev,
156 "port %d indicator %s status %d\n", 156 "port %d indicator %s status %d\n",
157 port1, 157 port1,
158 ({ char *s; switch (selector) { 158 ({ char *s; switch (selector) {
159 case HUB_LED_AMBER: s = "amber"; break; 159 case HUB_LED_AMBER: s = "amber"; break;
160 case HUB_LED_GREEN: s = "green"; break; 160 case HUB_LED_GREEN: s = "green"; break;
161 case HUB_LED_OFF: s = "off"; break; 161 case HUB_LED_OFF: s = "off"; break;
162 case HUB_LED_AUTO: s = "auto"; break; 162 case HUB_LED_AUTO: s = "auto"; break;
163 default: s = "??"; break; 163 default: s = "??"; break;
164 }; s; }), 164 }; s; }),
165 status); 165 status);
166 } 166 }
167 167
168 #define LED_CYCLE_PERIOD ((2*HZ)/3) 168 #define LED_CYCLE_PERIOD ((2*HZ)/3)
169 169
170 static void led_work (void *__hub) 170 static void led_work (void *__hub)
171 { 171 {
172 struct usb_hub *hub = __hub; 172 struct usb_hub *hub = __hub;
173 struct usb_device *hdev = hub->hdev; 173 struct usb_device *hdev = hub->hdev;
174 unsigned i; 174 unsigned i;
175 unsigned changed = 0; 175 unsigned changed = 0;
176 int cursor = -1; 176 int cursor = -1;
177 177
178 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 178 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
179 return; 179 return;
180 180
181 for (i = 0; i < hub->descriptor->bNbrPorts; i++) { 181 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
182 unsigned selector, mode; 182 unsigned selector, mode;
183 183
184 /* 30%-50% duty cycle */ 184 /* 30%-50% duty cycle */
185 185
186 switch (hub->indicator[i]) { 186 switch (hub->indicator[i]) {
187 /* cycle marker */ 187 /* cycle marker */
188 case INDICATOR_CYCLE: 188 case INDICATOR_CYCLE:
189 cursor = i; 189 cursor = i;
190 selector = HUB_LED_AUTO; 190 selector = HUB_LED_AUTO;
191 mode = INDICATOR_AUTO; 191 mode = INDICATOR_AUTO;
192 break; 192 break;
193 /* blinking green = sw attention */ 193 /* blinking green = sw attention */
194 case INDICATOR_GREEN_BLINK: 194 case INDICATOR_GREEN_BLINK:
195 selector = HUB_LED_GREEN; 195 selector = HUB_LED_GREEN;
196 mode = INDICATOR_GREEN_BLINK_OFF; 196 mode = INDICATOR_GREEN_BLINK_OFF;
197 break; 197 break;
198 case INDICATOR_GREEN_BLINK_OFF: 198 case INDICATOR_GREEN_BLINK_OFF:
199 selector = HUB_LED_OFF; 199 selector = HUB_LED_OFF;
200 mode = INDICATOR_GREEN_BLINK; 200 mode = INDICATOR_GREEN_BLINK;
201 break; 201 break;
202 /* blinking amber = hw attention */ 202 /* blinking amber = hw attention */
203 case INDICATOR_AMBER_BLINK: 203 case INDICATOR_AMBER_BLINK:
204 selector = HUB_LED_AMBER; 204 selector = HUB_LED_AMBER;
205 mode = INDICATOR_AMBER_BLINK_OFF; 205 mode = INDICATOR_AMBER_BLINK_OFF;
206 break; 206 break;
207 case INDICATOR_AMBER_BLINK_OFF: 207 case INDICATOR_AMBER_BLINK_OFF:
208 selector = HUB_LED_OFF; 208 selector = HUB_LED_OFF;
209 mode = INDICATOR_AMBER_BLINK; 209 mode = INDICATOR_AMBER_BLINK;
210 break; 210 break;
211 /* blink green/amber = reserved */ 211 /* blink green/amber = reserved */
212 case INDICATOR_ALT_BLINK: 212 case INDICATOR_ALT_BLINK:
213 selector = HUB_LED_GREEN; 213 selector = HUB_LED_GREEN;
214 mode = INDICATOR_ALT_BLINK_OFF; 214 mode = INDICATOR_ALT_BLINK_OFF;
215 break; 215 break;
216 case INDICATOR_ALT_BLINK_OFF: 216 case INDICATOR_ALT_BLINK_OFF:
217 selector = HUB_LED_AMBER; 217 selector = HUB_LED_AMBER;
218 mode = INDICATOR_ALT_BLINK; 218 mode = INDICATOR_ALT_BLINK;
219 break; 219 break;
220 default: 220 default:
221 continue; 221 continue;
222 } 222 }
223 if (selector != HUB_LED_AUTO) 223 if (selector != HUB_LED_AUTO)
224 changed = 1; 224 changed = 1;
225 set_port_led(hub, i + 1, selector); 225 set_port_led(hub, i + 1, selector);
226 hub->indicator[i] = mode; 226 hub->indicator[i] = mode;
227 } 227 }
228 if (!changed && blinkenlights) { 228 if (!changed && blinkenlights) {
229 cursor++; 229 cursor++;
230 cursor %= hub->descriptor->bNbrPorts; 230 cursor %= hub->descriptor->bNbrPorts;
231 set_port_led(hub, cursor + 1, HUB_LED_GREEN); 231 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
232 hub->indicator[cursor] = INDICATOR_CYCLE; 232 hub->indicator[cursor] = INDICATOR_CYCLE;
233 changed++; 233 changed++;
234 } 234 }
235 if (changed) 235 if (changed)
236 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 236 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
237 } 237 }
238 238
239 /* use a short timeout for hub/port status fetches */ 239 /* use a short timeout for hub/port status fetches */
240 #define USB_STS_TIMEOUT 1000 240 #define USB_STS_TIMEOUT 1000
241 #define USB_STS_RETRIES 5 241 #define USB_STS_RETRIES 5
242 242
243 /* 243 /*
244 * USB 2.0 spec Section 11.24.2.6 244 * USB 2.0 spec Section 11.24.2.6
245 */ 245 */
246 static int get_hub_status(struct usb_device *hdev, 246 static int get_hub_status(struct usb_device *hdev,
247 struct usb_hub_status *data) 247 struct usb_hub_status *data)
248 { 248 {
249 int i, status = -ETIMEDOUT; 249 int i, status = -ETIMEDOUT;
250 250
251 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 251 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
252 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 252 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
253 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 253 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
254 data, sizeof(*data), USB_STS_TIMEOUT); 254 data, sizeof(*data), USB_STS_TIMEOUT);
255 } 255 }
256 return status; 256 return status;
257 } 257 }
258 258
259 /* 259 /*
260 * USB 2.0 spec Section 11.24.2.7 260 * USB 2.0 spec Section 11.24.2.7
261 */ 261 */
262 static int get_port_status(struct usb_device *hdev, int port1, 262 static int get_port_status(struct usb_device *hdev, int port1,
263 struct usb_port_status *data) 263 struct usb_port_status *data)
264 { 264 {
265 int i, status = -ETIMEDOUT; 265 int i, status = -ETIMEDOUT;
266 266
267 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 267 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
268 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 268 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
269 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, 269 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
270 data, sizeof(*data), USB_STS_TIMEOUT); 270 data, sizeof(*data), USB_STS_TIMEOUT);
271 } 271 }
272 return status; 272 return status;
273 } 273 }
274 274
275 static void kick_khubd(struct usb_hub *hub) 275 static void kick_khubd(struct usb_hub *hub)
276 { 276 {
277 unsigned long flags; 277 unsigned long flags;
278 278
279 spin_lock_irqsave(&hub_event_lock, flags); 279 spin_lock_irqsave(&hub_event_lock, flags);
280 if (list_empty(&hub->event_list)) { 280 if (list_empty(&hub->event_list)) {
281 list_add_tail(&hub->event_list, &hub_event_list); 281 list_add_tail(&hub->event_list, &hub_event_list);
282 wake_up(&khubd_wait); 282 wake_up(&khubd_wait);
283 } 283 }
284 spin_unlock_irqrestore(&hub_event_lock, flags); 284 spin_unlock_irqrestore(&hub_event_lock, flags);
285 } 285 }
286 286
287 void usb_kick_khubd(struct usb_device *hdev) 287 void usb_kick_khubd(struct usb_device *hdev)
288 { 288 {
289 kick_khubd(hdev_to_hub(hdev)); 289 kick_khubd(hdev_to_hub(hdev));
290 } 290 }
291 291
292 292
293 /* completion function, fires on port status changes and various faults */ 293 /* completion function, fires on port status changes and various faults */
294 static void hub_irq(struct urb *urb, struct pt_regs *regs) 294 static void hub_irq(struct urb *urb, struct pt_regs *regs)
295 { 295 {
296 struct usb_hub *hub = (struct usb_hub *)urb->context; 296 struct usb_hub *hub = (struct usb_hub *)urb->context;
297 int status; 297 int status;
298 int i; 298 int i;
299 unsigned long bits; 299 unsigned long bits;
300 300
301 switch (urb->status) { 301 switch (urb->status) {
302 case -ENOENT: /* synchronous unlink */ 302 case -ENOENT: /* synchronous unlink */
303 case -ECONNRESET: /* async unlink */ 303 case -ECONNRESET: /* async unlink */
304 case -ESHUTDOWN: /* hardware going away */ 304 case -ESHUTDOWN: /* hardware going away */
305 return; 305 return;
306 306
307 default: /* presumably an error */ 307 default: /* presumably an error */
308 /* Cause a hub reset after 10 consecutive errors */ 308 /* Cause a hub reset after 10 consecutive errors */
309 dev_dbg (hub->intfdev, "transfer --> %d\n", urb->status); 309 dev_dbg (hub->intfdev, "transfer --> %d\n", urb->status);
310 if ((++hub->nerrors < 10) || hub->error) 310 if ((++hub->nerrors < 10) || hub->error)
311 goto resubmit; 311 goto resubmit;
312 hub->error = urb->status; 312 hub->error = urb->status;
313 /* FALL THROUGH */ 313 /* FALL THROUGH */
314 314
315 /* let khubd handle things */ 315 /* let khubd handle things */
316 case 0: /* we got data: port status changed */ 316 case 0: /* we got data: port status changed */
317 bits = 0; 317 bits = 0;
318 for (i = 0; i < urb->actual_length; ++i) 318 for (i = 0; i < urb->actual_length; ++i)
319 bits |= ((unsigned long) ((*hub->buffer)[i])) 319 bits |= ((unsigned long) ((*hub->buffer)[i]))
320 << (i*8); 320 << (i*8);
321 hub->event_bits[0] = bits; 321 hub->event_bits[0] = bits;
322 break; 322 break;
323 } 323 }
324 324
325 hub->nerrors = 0; 325 hub->nerrors = 0;
326 326
327 /* Something happened, let khubd figure it out */ 327 /* Something happened, let khubd figure it out */
328 kick_khubd(hub); 328 kick_khubd(hub);
329 329
330 resubmit: 330 resubmit:
331 if (hub->quiescing) 331 if (hub->quiescing)
332 return; 332 return;
333 333
334 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 334 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
335 && status != -ENODEV && status != -EPERM) 335 && status != -ENODEV && status != -EPERM)
336 dev_err (hub->intfdev, "resubmit --> %d\n", status); 336 dev_err (hub->intfdev, "resubmit --> %d\n", status);
337 } 337 }
338 338
339 /* USB 2.0 spec Section 11.24.2.3 */ 339 /* USB 2.0 spec Section 11.24.2.3 */
340 static inline int 340 static inline int
341 hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) 341 hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
342 { 342 {
343 return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 343 return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
344 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 344 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
345 tt, NULL, 0, 1000); 345 tt, NULL, 0, 1000);
346 } 346 }
347 347
348 /* 348 /*
349 * enumeration blocks khubd for a long time. we use keventd instead, since 349 * enumeration blocks khubd for a long time. we use keventd instead, since
350 * long blocking there is the exception, not the rule. accordingly, HCDs 350 * long blocking there is the exception, not the rule. accordingly, HCDs
351 * talking to TTs must queue control transfers (not just bulk and iso), so 351 * talking to TTs must queue control transfers (not just bulk and iso), so
352 * both can talk to the same hub concurrently. 352 * both can talk to the same hub concurrently.
353 */ 353 */
354 static void hub_tt_kevent (void *arg) 354 static void hub_tt_kevent (void *arg)
355 { 355 {
356 struct usb_hub *hub = arg; 356 struct usb_hub *hub = arg;
357 unsigned long flags; 357 unsigned long flags;
358 358
359 spin_lock_irqsave (&hub->tt.lock, flags); 359 spin_lock_irqsave (&hub->tt.lock, flags);
360 while (!list_empty (&hub->tt.clear_list)) { 360 while (!list_empty (&hub->tt.clear_list)) {
361 struct list_head *temp; 361 struct list_head *temp;
362 struct usb_tt_clear *clear; 362 struct usb_tt_clear *clear;
363 struct usb_device *hdev = hub->hdev; 363 struct usb_device *hdev = hub->hdev;
364 int status; 364 int status;
365 365
366 temp = hub->tt.clear_list.next; 366 temp = hub->tt.clear_list.next;
367 clear = list_entry (temp, struct usb_tt_clear, clear_list); 367 clear = list_entry (temp, struct usb_tt_clear, clear_list);
368 list_del (&clear->clear_list); 368 list_del (&clear->clear_list);
369 369
370 /* drop lock so HCD can concurrently report other TT errors */ 370 /* drop lock so HCD can concurrently report other TT errors */
371 spin_unlock_irqrestore (&hub->tt.lock, flags); 371 spin_unlock_irqrestore (&hub->tt.lock, flags);
372 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 372 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
373 spin_lock_irqsave (&hub->tt.lock, flags); 373 spin_lock_irqsave (&hub->tt.lock, flags);
374 374
375 if (status) 375 if (status)
376 dev_err (&hdev->dev, 376 dev_err (&hdev->dev,
377 "clear tt %d (%04x) error %d\n", 377 "clear tt %d (%04x) error %d\n",
378 clear->tt, clear->devinfo, status); 378 clear->tt, clear->devinfo, status);
379 kfree(clear); 379 kfree(clear);
380 } 380 }
381 spin_unlock_irqrestore (&hub->tt.lock, flags); 381 spin_unlock_irqrestore (&hub->tt.lock, flags);
382 } 382 }
383 383
384 /** 384 /**
385 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub 385 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
386 * @udev: the device whose split transaction failed 386 * @udev: the device whose split transaction failed
387 * @pipe: identifies the endpoint of the failed transaction 387 * @pipe: identifies the endpoint of the failed transaction
388 * 388 *
389 * High speed HCDs use this to tell the hub driver that some split control or 389 * High speed HCDs use this to tell the hub driver that some split control or
390 * bulk transaction failed in a way that requires clearing internal state of 390 * bulk transaction failed in a way that requires clearing internal state of
391 * a transaction translator. This is normally detected (and reported) from 391 * a transaction translator. This is normally detected (and reported) from
392 * interrupt context. 392 * interrupt context.
393 * 393 *
394 * It may not be possible for that hub to handle additional full (or low) 394 * It may not be possible for that hub to handle additional full (or low)
395 * speed transactions until that state is fully cleared out. 395 * speed transactions until that state is fully cleared out.
396 */ 396 */
397 void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe) 397 void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
398 { 398 {
399 struct usb_tt *tt = udev->tt; 399 struct usb_tt *tt = udev->tt;
400 unsigned long flags; 400 unsigned long flags;
401 struct usb_tt_clear *clear; 401 struct usb_tt_clear *clear;
402 402
403 /* we've got to cope with an arbitrary number of pending TT clears, 403 /* we've got to cope with an arbitrary number of pending TT clears,
404 * since each TT has "at least two" buffers that can need it (and 404 * since each TT has "at least two" buffers that can need it (and
405 * there can be many TTs per hub). even if they're uncommon. 405 * there can be many TTs per hub). even if they're uncommon.
406 */ 406 */
407 if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == NULL) { 407 if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == NULL) {
408 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 408 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
409 /* FIXME recover somehow ... RESET_TT? */ 409 /* FIXME recover somehow ... RESET_TT? */
410 return; 410 return;
411 } 411 }
412 412
413 /* info that CLEAR_TT_BUFFER needs */ 413 /* info that CLEAR_TT_BUFFER needs */
414 clear->tt = tt->multi ? udev->ttport : 1; 414 clear->tt = tt->multi ? udev->ttport : 1;
415 clear->devinfo = usb_pipeendpoint (pipe); 415 clear->devinfo = usb_pipeendpoint (pipe);
416 clear->devinfo |= udev->devnum << 4; 416 clear->devinfo |= udev->devnum << 4;
417 clear->devinfo |= usb_pipecontrol (pipe) 417 clear->devinfo |= usb_pipecontrol (pipe)
418 ? (USB_ENDPOINT_XFER_CONTROL << 11) 418 ? (USB_ENDPOINT_XFER_CONTROL << 11)
419 : (USB_ENDPOINT_XFER_BULK << 11); 419 : (USB_ENDPOINT_XFER_BULK << 11);
420 if (usb_pipein (pipe)) 420 if (usb_pipein (pipe))
421 clear->devinfo |= 1 << 15; 421 clear->devinfo |= 1 << 15;
422 422
423 /* tell keventd to clear state for this TT */ 423 /* tell keventd to clear state for this TT */
424 spin_lock_irqsave (&tt->lock, flags); 424 spin_lock_irqsave (&tt->lock, flags);
425 list_add_tail (&clear->clear_list, &tt->clear_list); 425 list_add_tail (&clear->clear_list, &tt->clear_list);
426 schedule_work (&tt->kevent); 426 schedule_work (&tt->kevent);
427 spin_unlock_irqrestore (&tt->lock, flags); 427 spin_unlock_irqrestore (&tt->lock, flags);
428 } 428 }
429 429
430 static void hub_power_on(struct usb_hub *hub) 430 static void hub_power_on(struct usb_hub *hub)
431 { 431 {
432 int port1; 432 int port1;
433 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; 433 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
434 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 434 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
435 435
436 /* if hub supports power switching, enable power on each port */ 436 /* if hub supports power switching, enable power on each port */
437 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) { 437 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) {
438 dev_dbg(hub->intfdev, "enabling power on all ports\n"); 438 dev_dbg(hub->intfdev, "enabling power on all ports\n");
439 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 439 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
440 set_port_feature(hub->hdev, port1, 440 set_port_feature(hub->hdev, port1,
441 USB_PORT_FEAT_POWER); 441 USB_PORT_FEAT_POWER);
442 } 442 }
443 443
444 /* Wait at least 100 msec for power to become stable */ 444 /* Wait at least 100 msec for power to become stable */
445 msleep(max(pgood_delay, (unsigned) 100)); 445 msleep(max(pgood_delay, (unsigned) 100));
446 } 446 }
447 447
448 static inline void __hub_quiesce(struct usb_hub *hub) 448 static inline void __hub_quiesce(struct usb_hub *hub)
449 { 449 {
450 /* (nonblocking) khubd and related activity won't re-trigger */ 450 /* (nonblocking) khubd and related activity won't re-trigger */
451 hub->quiescing = 1; 451 hub->quiescing = 1;
452 hub->activating = 0; 452 hub->activating = 0;
453 hub->resume_root_hub = 0; 453 hub->resume_root_hub = 0;
454 } 454 }
455 455
456 static void hub_quiesce(struct usb_hub *hub) 456 static void hub_quiesce(struct usb_hub *hub)
457 { 457 {
458 /* (blocking) stop khubd and related activity */ 458 /* (blocking) stop khubd and related activity */
459 __hub_quiesce(hub); 459 __hub_quiesce(hub);
460 usb_kill_urb(hub->urb); 460 usb_kill_urb(hub->urb);
461 if (hub->has_indicators) 461 if (hub->has_indicators)
462 cancel_delayed_work(&hub->leds); 462 cancel_delayed_work(&hub->leds);
463 if (hub->has_indicators || hub->tt.hub) 463 if (hub->has_indicators || hub->tt.hub)
464 flush_scheduled_work(); 464 flush_scheduled_work();
465 } 465 }
466 466
467 static void hub_activate(struct usb_hub *hub) 467 static void hub_activate(struct usb_hub *hub)
468 { 468 {
469 int status; 469 int status;
470 470
471 hub->quiescing = 0; 471 hub->quiescing = 0;
472 hub->activating = 1; 472 hub->activating = 1;
473 hub->resume_root_hub = 0; 473 hub->resume_root_hub = 0;
474 status = usb_submit_urb(hub->urb, GFP_NOIO); 474 status = usb_submit_urb(hub->urb, GFP_NOIO);
475 if (status < 0) 475 if (status < 0)
476 dev_err(hub->intfdev, "activate --> %d\n", status); 476 dev_err(hub->intfdev, "activate --> %d\n", status);
477 if (hub->has_indicators && blinkenlights) 477 if (hub->has_indicators && blinkenlights)
478 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 478 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
479 479
480 /* scan all ports ASAP */ 480 /* scan all ports ASAP */
481 kick_khubd(hub); 481 kick_khubd(hub);
482 } 482 }
483 483
484 static int hub_hub_status(struct usb_hub *hub, 484 static int hub_hub_status(struct usb_hub *hub,
485 u16 *status, u16 *change) 485 u16 *status, u16 *change)
486 { 486 {
487 int ret; 487 int ret;
488 488
489 ret = get_hub_status(hub->hdev, &hub->status->hub); 489 ret = get_hub_status(hub->hdev, &hub->status->hub);
490 if (ret < 0) 490 if (ret < 0)
491 dev_err (hub->intfdev, 491 dev_err (hub->intfdev,
492 "%s failed (err = %d)\n", __FUNCTION__, ret); 492 "%s failed (err = %d)\n", __FUNCTION__, ret);
493 else { 493 else {
494 *status = le16_to_cpu(hub->status->hub.wHubStatus); 494 *status = le16_to_cpu(hub->status->hub.wHubStatus);
495 *change = le16_to_cpu(hub->status->hub.wHubChange); 495 *change = le16_to_cpu(hub->status->hub.wHubChange);
496 ret = 0; 496 ret = 0;
497 } 497 }
498 return ret; 498 return ret;
499 } 499 }
500 500
501 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 501 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
502 { 502 {
503 struct usb_device *hdev = hub->hdev; 503 struct usb_device *hdev = hub->hdev;
504 int ret; 504 int ret;
505 505
506 if (hdev->children[port1-1] && set_state) { 506 if (hdev->children[port1-1] && set_state) {
507 usb_set_device_state(hdev->children[port1-1], 507 usb_set_device_state(hdev->children[port1-1],
508 USB_STATE_NOTATTACHED); 508 USB_STATE_NOTATTACHED);
509 } 509 }
510 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); 510 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
511 if (ret) 511 if (ret)
512 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 512 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
513 port1, ret); 513 port1, ret);
514 514
515 return ret; 515 return ret;
516 } 516 }
517 517
518 518
519 /* caller has locked the hub device */ 519 /* caller has locked the hub device */
520 static void hub_pre_reset(struct usb_hub *hub, int disable_ports) 520 static void hub_pre_reset(struct usb_hub *hub, int disable_ports)
521 { 521 {
522 struct usb_device *hdev = hub->hdev; 522 struct usb_device *hdev = hub->hdev;
523 int port1; 523 int port1;
524 524
525 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 525 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
526 if (hdev->children[port1 - 1]) { 526 if (hdev->children[port1 - 1]) {
527 usb_disconnect(&hdev->children[port1 - 1]); 527 usb_disconnect(&hdev->children[port1 - 1]);
528 if (disable_ports) 528 if (disable_ports)
529 hub_port_disable(hub, port1, 0); 529 hub_port_disable(hub, port1, 0);
530 } 530 }
531 } 531 }
532 hub_quiesce(hub); 532 hub_quiesce(hub);
533 } 533 }
534 534
535 /* caller has locked the hub device */ 535 /* caller has locked the hub device */
536 static void hub_post_reset(struct usb_hub *hub) 536 static void hub_post_reset(struct usb_hub *hub)
537 { 537 {
538 hub_activate(hub); 538 hub_activate(hub);
539 hub_power_on(hub); 539 hub_power_on(hub);
540 } 540 }
541 541
542 542
543 static int hub_configure(struct usb_hub *hub, 543 static int hub_configure(struct usb_hub *hub,
544 struct usb_endpoint_descriptor *endpoint) 544 struct usb_endpoint_descriptor *endpoint)
545 { 545 {
546 struct usb_device *hdev = hub->hdev; 546 struct usb_device *hdev = hub->hdev;
547 struct device *hub_dev = hub->intfdev; 547 struct device *hub_dev = hub->intfdev;
548 u16 hubstatus, hubchange; 548 u16 hubstatus, hubchange;
549 u16 wHubCharacteristics; 549 u16 wHubCharacteristics;
550 unsigned int pipe; 550 unsigned int pipe;
551 int maxp, ret; 551 int maxp, ret;
552 char *message; 552 char *message;
553 553
554 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL, 554 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL,
555 &hub->buffer_dma); 555 &hub->buffer_dma);
556 if (!hub->buffer) { 556 if (!hub->buffer) {
557 message = "can't allocate hub irq buffer"; 557 message = "can't allocate hub irq buffer";
558 ret = -ENOMEM; 558 ret = -ENOMEM;
559 goto fail; 559 goto fail;
560 } 560 }
561 561
562 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 562 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
563 if (!hub->status) { 563 if (!hub->status) {
564 message = "can't kmalloc hub status buffer"; 564 message = "can't kmalloc hub status buffer";
565 ret = -ENOMEM; 565 ret = -ENOMEM;
566 goto fail; 566 goto fail;
567 } 567 }
568 568
569 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 569 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
570 if (!hub->descriptor) { 570 if (!hub->descriptor) {
571 message = "can't kmalloc hub descriptor"; 571 message = "can't kmalloc hub descriptor";
572 ret = -ENOMEM; 572 ret = -ENOMEM;
573 goto fail; 573 goto fail;
574 } 574 }
575 575
576 /* Request the entire hub descriptor. 576 /* Request the entire hub descriptor.
577 * hub->descriptor can handle USB_MAXCHILDREN ports, 577 * hub->descriptor can handle USB_MAXCHILDREN ports,
578 * but the hub can/will return fewer bytes here. 578 * but the hub can/will return fewer bytes here.
579 */ 579 */
580 ret = get_hub_descriptor(hdev, hub->descriptor, 580 ret = get_hub_descriptor(hdev, hub->descriptor,
581 sizeof(*hub->descriptor)); 581 sizeof(*hub->descriptor));
582 if (ret < 0) { 582 if (ret < 0) {
583 message = "can't read hub descriptor"; 583 message = "can't read hub descriptor";
584 goto fail; 584 goto fail;
585 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 585 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
586 message = "hub has too many ports!"; 586 message = "hub has too many ports!";
587 ret = -ENODEV; 587 ret = -ENODEV;
588 goto fail; 588 goto fail;
589 } 589 }
590 590
591 hdev->maxchild = hub->descriptor->bNbrPorts; 591 hdev->maxchild = hub->descriptor->bNbrPorts;
592 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 592 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
593 (hdev->maxchild == 1) ? "" : "s"); 593 (hdev->maxchild == 1) ? "" : "s");
594 594
595 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 595 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
596 596
597 if (wHubCharacteristics & HUB_CHAR_COMPOUND) { 597 if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
598 int i; 598 int i;
599 char portstr [USB_MAXCHILDREN + 1]; 599 char portstr [USB_MAXCHILDREN + 1];
600 600
601 for (i = 0; i < hdev->maxchild; i++) 601 for (i = 0; i < hdev->maxchild; i++)
602 portstr[i] = hub->descriptor->DeviceRemovable 602 portstr[i] = hub->descriptor->DeviceRemovable
603 [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 603 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
604 ? 'F' : 'R'; 604 ? 'F' : 'R';
605 portstr[hdev->maxchild] = 0; 605 portstr[hdev->maxchild] = 0;
606 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 606 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
607 } else 607 } else
608 dev_dbg(hub_dev, "standalone hub\n"); 608 dev_dbg(hub_dev, "standalone hub\n");
609 609
610 switch (wHubCharacteristics & HUB_CHAR_LPSM) { 610 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
611 case 0x00: 611 case 0x00:
612 dev_dbg(hub_dev, "ganged power switching\n"); 612 dev_dbg(hub_dev, "ganged power switching\n");
613 break; 613 break;
614 case 0x01: 614 case 0x01:
615 dev_dbg(hub_dev, "individual port power switching\n"); 615 dev_dbg(hub_dev, "individual port power switching\n");
616 break; 616 break;
617 case 0x02: 617 case 0x02:
618 case 0x03: 618 case 0x03:
619 dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 619 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
620 break; 620 break;
621 } 621 }
622 622
623 switch (wHubCharacteristics & HUB_CHAR_OCPM) { 623 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
624 case 0x00: 624 case 0x00:
625 dev_dbg(hub_dev, "global over-current protection\n"); 625 dev_dbg(hub_dev, "global over-current protection\n");
626 break; 626 break;
627 case 0x08: 627 case 0x08:
628 dev_dbg(hub_dev, "individual port over-current protection\n"); 628 dev_dbg(hub_dev, "individual port over-current protection\n");
629 break; 629 break;
630 case 0x10: 630 case 0x10:
631 case 0x18: 631 case 0x18:
632 dev_dbg(hub_dev, "no over-current protection\n"); 632 dev_dbg(hub_dev, "no over-current protection\n");
633 break; 633 break;
634 } 634 }
635 635
636 spin_lock_init (&hub->tt.lock); 636 spin_lock_init (&hub->tt.lock);
637 INIT_LIST_HEAD (&hub->tt.clear_list); 637 INIT_LIST_HEAD (&hub->tt.clear_list);
638 INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub); 638 INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
639 switch (hdev->descriptor.bDeviceProtocol) { 639 switch (hdev->descriptor.bDeviceProtocol) {
640 case 0: 640 case 0:
641 break; 641 break;
642 case 1: 642 case 1:
643 dev_dbg(hub_dev, "Single TT\n"); 643 dev_dbg(hub_dev, "Single TT\n");
644 hub->tt.hub = hdev; 644 hub->tt.hub = hdev;
645 break; 645 break;
646 case 2: 646 case 2:
647 ret = usb_set_interface(hdev, 0, 1); 647 ret = usb_set_interface(hdev, 0, 1);
648 if (ret == 0) { 648 if (ret == 0) {
649 dev_dbg(hub_dev, "TT per port\n"); 649 dev_dbg(hub_dev, "TT per port\n");
650 hub->tt.multi = 1; 650 hub->tt.multi = 1;
651 } else 651 } else
652 dev_err(hub_dev, "Using single TT (err %d)\n", 652 dev_err(hub_dev, "Using single TT (err %d)\n",
653 ret); 653 ret);
654 hub->tt.hub = hdev; 654 hub->tt.hub = hdev;
655 break; 655 break;
656 default: 656 default:
657 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 657 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
658 hdev->descriptor.bDeviceProtocol); 658 hdev->descriptor.bDeviceProtocol);
659 break; 659 break;
660 } 660 }
661 661
662 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 662 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
663 switch (wHubCharacteristics & HUB_CHAR_TTTT) { 663 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
664 case HUB_TTTT_8_BITS: 664 case HUB_TTTT_8_BITS:
665 if (hdev->descriptor.bDeviceProtocol != 0) { 665 if (hdev->descriptor.bDeviceProtocol != 0) {
666 hub->tt.think_time = 666; 666 hub->tt.think_time = 666;
667 dev_dbg(hub_dev, "TT requires at most %d " 667 dev_dbg(hub_dev, "TT requires at most %d "
668 "FS bit times (%d ns)\n", 668 "FS bit times (%d ns)\n",
669 8, hub->tt.think_time); 669 8, hub->tt.think_time);
670 } 670 }
671 break; 671 break;
672 case HUB_TTTT_16_BITS: 672 case HUB_TTTT_16_BITS:
673 hub->tt.think_time = 666 * 2; 673 hub->tt.think_time = 666 * 2;
674 dev_dbg(hub_dev, "TT requires at most %d " 674 dev_dbg(hub_dev, "TT requires at most %d "
675 "FS bit times (%d ns)\n", 675 "FS bit times (%d ns)\n",
676 16, hub->tt.think_time); 676 16, hub->tt.think_time);
677 break; 677 break;
678 case HUB_TTTT_24_BITS: 678 case HUB_TTTT_24_BITS:
679 hub->tt.think_time = 666 * 3; 679 hub->tt.think_time = 666 * 3;
680 dev_dbg(hub_dev, "TT requires at most %d " 680 dev_dbg(hub_dev, "TT requires at most %d "
681 "FS bit times (%d ns)\n", 681 "FS bit times (%d ns)\n",
682 24, hub->tt.think_time); 682 24, hub->tt.think_time);
683 break; 683 break;
684 case HUB_TTTT_32_BITS: 684 case HUB_TTTT_32_BITS:
685 hub->tt.think_time = 666 * 4; 685 hub->tt.think_time = 666 * 4;
686 dev_dbg(hub_dev, "TT requires at most %d " 686 dev_dbg(hub_dev, "TT requires at most %d "
687 "FS bit times (%d ns)\n", 687 "FS bit times (%d ns)\n",
688 32, hub->tt.think_time); 688 32, hub->tt.think_time);
689 break; 689 break;
690 } 690 }
691 691
692 /* probe() zeroes hub->indicator[] */ 692 /* probe() zeroes hub->indicator[] */
693 if (wHubCharacteristics & HUB_CHAR_PORTIND) { 693 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
694 hub->has_indicators = 1; 694 hub->has_indicators = 1;
695 dev_dbg(hub_dev, "Port indicators are supported\n"); 695 dev_dbg(hub_dev, "Port indicators are supported\n");
696 } 696 }
697 697
698 dev_dbg(hub_dev, "power on to power good time: %dms\n", 698 dev_dbg(hub_dev, "power on to power good time: %dms\n",
699 hub->descriptor->bPwrOn2PwrGood * 2); 699 hub->descriptor->bPwrOn2PwrGood * 2);
700 700
701 /* power budgeting mostly matters with bus-powered hubs, 701 /* power budgeting mostly matters with bus-powered hubs,
702 * and battery-powered root hubs (may provide just 8 mA). 702 * and battery-powered root hubs (may provide just 8 mA).
703 */ 703 */
704 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 704 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
705 if (ret < 0) { 705 if (ret < 2) {
706 message = "can't get hub status"; 706 message = "can't get hub status";
707 goto fail; 707 goto fail;
708 } 708 }
709 le16_to_cpus(&hubstatus); 709 le16_to_cpus(&hubstatus);
710 if (hdev == hdev->bus->root_hub) { 710 if (hdev == hdev->bus->root_hub) {
711 struct usb_hcd *hcd = 711 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
712 container_of(hdev->bus, struct usb_hcd, self); 712 hub->mA_per_port = 500;
713 713 else {
714 hub->power_budget = min(500u, hcd->power_budget) / 2; 714 hub->mA_per_port = hdev->bus_mA;
715 hub->limited_power = 1;
716 }
715 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 717 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
716 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 718 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
717 hub->descriptor->bHubContrCurrent); 719 hub->descriptor->bHubContrCurrent);
718 hub->power_budget = (501 - hub->descriptor->bHubContrCurrent) 720 hub->limited_power = 1;
719 / 2; 721 if (hdev->maxchild > 0) {
722 int remaining = hdev->bus_mA -
723 hub->descriptor->bHubContrCurrent;
724
725 if (remaining < hdev->maxchild * 100)
726 dev_warn(hub_dev,
727 "insufficient power available "
728 "to use all downstream ports\n");
729 hub->mA_per_port = 100; /* 7.2.1.1 */
730 }
731 } else { /* Self-powered external hub */
732 /* FIXME: What about battery-powered external hubs that
733 * provide less current per port? */
734 hub->mA_per_port = 500;
720 } 735 }
721 if (hub->power_budget) 736 if (hub->mA_per_port < 500)
722 dev_dbg(hub_dev, "%dmA bus power budget for children\n", 737 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
723 hub->power_budget * 2); 738 hub->mA_per_port);
724 739
725
726 ret = hub_hub_status(hub, &hubstatus, &hubchange); 740 ret = hub_hub_status(hub, &hubstatus, &hubchange);
727 if (ret < 0) { 741 if (ret < 0) {
728 message = "can't get hub status"; 742 message = "can't get hub status";
729 goto fail; 743 goto fail;
730 } 744 }
731 745
732 /* local power status reports aren't always correct */ 746 /* local power status reports aren't always correct */
733 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 747 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
734 dev_dbg(hub_dev, "local power source is %s\n", 748 dev_dbg(hub_dev, "local power source is %s\n",
735 (hubstatus & HUB_STATUS_LOCAL_POWER) 749 (hubstatus & HUB_STATUS_LOCAL_POWER)
736 ? "lost (inactive)" : "good"); 750 ? "lost (inactive)" : "good");
737 751
738 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 752 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
739 dev_dbg(hub_dev, "%sover-current condition exists\n", 753 dev_dbg(hub_dev, "%sover-current condition exists\n",
740 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 754 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
741 755
742 /* set up the interrupt endpoint */ 756 /* set up the interrupt endpoint */
743 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 757 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
744 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 758 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
745 759
746 if (maxp > sizeof(*hub->buffer)) 760 if (maxp > sizeof(*hub->buffer))
747 maxp = sizeof(*hub->buffer); 761 maxp = sizeof(*hub->buffer);
748 762
749 hub->urb = usb_alloc_urb(0, GFP_KERNEL); 763 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
750 if (!hub->urb) { 764 if (!hub->urb) {
751 message = "couldn't allocate interrupt urb"; 765 message = "couldn't allocate interrupt urb";
752 ret = -ENOMEM; 766 ret = -ENOMEM;
753 goto fail; 767 goto fail;
754 } 768 }
755 769
756 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 770 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
757 hub, endpoint->bInterval); 771 hub, endpoint->bInterval);
758 hub->urb->transfer_dma = hub->buffer_dma; 772 hub->urb->transfer_dma = hub->buffer_dma;
759 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 773 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
760 774
761 /* maybe cycle the hub leds */ 775 /* maybe cycle the hub leds */
762 if (hub->has_indicators && blinkenlights) 776 if (hub->has_indicators && blinkenlights)
763 hub->indicator [0] = INDICATOR_CYCLE; 777 hub->indicator [0] = INDICATOR_CYCLE;
764 778
765 hub_power_on(hub); 779 hub_power_on(hub);
766 hub_activate(hub); 780 hub_activate(hub);
767 return 0; 781 return 0;
768 782
769 fail: 783 fail:
770 dev_err (hub_dev, "config failed, %s (err %d)\n", 784 dev_err (hub_dev, "config failed, %s (err %d)\n",
771 message, ret); 785 message, ret);
772 /* hub_disconnect() frees urb and descriptor */ 786 /* hub_disconnect() frees urb and descriptor */
773 return ret; 787 return ret;
774 } 788 }
775 789
776 static unsigned highspeed_hubs; 790 static unsigned highspeed_hubs;
777 791
778 static void hub_disconnect(struct usb_interface *intf) 792 static void hub_disconnect(struct usb_interface *intf)
779 { 793 {
780 struct usb_hub *hub = usb_get_intfdata (intf); 794 struct usb_hub *hub = usb_get_intfdata (intf);
781 struct usb_device *hdev; 795 struct usb_device *hdev;
782 796
783 usb_set_intfdata (intf, NULL); 797 usb_set_intfdata (intf, NULL);
784 hdev = hub->hdev; 798 hdev = hub->hdev;
785 799
786 if (hdev->speed == USB_SPEED_HIGH) 800 if (hdev->speed == USB_SPEED_HIGH)
787 highspeed_hubs--; 801 highspeed_hubs--;
788 802
789 /* Disconnect all children and quiesce the hub */ 803 /* Disconnect all children and quiesce the hub */
790 hub_pre_reset(hub, 1); 804 hub_pre_reset(hub, 1);
791 805
792 usb_free_urb(hub->urb); 806 usb_free_urb(hub->urb);
793 hub->urb = NULL; 807 hub->urb = NULL;
794 808
795 spin_lock_irq(&hub_event_lock); 809 spin_lock_irq(&hub_event_lock);
796 list_del_init(&hub->event_list); 810 list_del_init(&hub->event_list);
797 spin_unlock_irq(&hub_event_lock); 811 spin_unlock_irq(&hub_event_lock);
798 812
799 kfree(hub->descriptor); 813 kfree(hub->descriptor);
800 hub->descriptor = NULL; 814 hub->descriptor = NULL;
801 815
802 kfree(hub->status); 816 kfree(hub->status);
803 hub->status = NULL; 817 hub->status = NULL;
804 818
805 if (hub->buffer) { 819 if (hub->buffer) {
806 usb_buffer_free(hdev, sizeof(*hub->buffer), hub->buffer, 820 usb_buffer_free(hdev, sizeof(*hub->buffer), hub->buffer,
807 hub->buffer_dma); 821 hub->buffer_dma);
808 hub->buffer = NULL; 822 hub->buffer = NULL;
809 } 823 }
810 824
811 kfree(hub); 825 kfree(hub);
812 } 826 }
813 827
814 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 828 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
815 { 829 {
816 struct usb_host_interface *desc; 830 struct usb_host_interface *desc;
817 struct usb_endpoint_descriptor *endpoint; 831 struct usb_endpoint_descriptor *endpoint;
818 struct usb_device *hdev; 832 struct usb_device *hdev;
819 struct usb_hub *hub; 833 struct usb_hub *hub;
820 834
821 desc = intf->cur_altsetting; 835 desc = intf->cur_altsetting;
822 hdev = interface_to_usbdev(intf); 836 hdev = interface_to_usbdev(intf);
823 837
824 /* Some hubs have a subclass of 1, which AFAICT according to the */ 838 /* Some hubs have a subclass of 1, which AFAICT according to the */
825 /* specs is not defined, but it works */ 839 /* specs is not defined, but it works */
826 if ((desc->desc.bInterfaceSubClass != 0) && 840 if ((desc->desc.bInterfaceSubClass != 0) &&
827 (desc->desc.bInterfaceSubClass != 1)) { 841 (desc->desc.bInterfaceSubClass != 1)) {
828 descriptor_error: 842 descriptor_error:
829 dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); 843 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
830 return -EIO; 844 return -EIO;
831 } 845 }
832 846
833 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 847 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
834 if (desc->desc.bNumEndpoints != 1) 848 if (desc->desc.bNumEndpoints != 1)
835 goto descriptor_error; 849 goto descriptor_error;
836 850
837 endpoint = &desc->endpoint[0].desc; 851 endpoint = &desc->endpoint[0].desc;
838 852
839 /* Output endpoint? Curiouser and curiouser.. */ 853 /* Output endpoint? Curiouser and curiouser.. */
840 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) 854 if (!(endpoint->bEndpointAddress & USB_DIR_IN))
841 goto descriptor_error; 855 goto descriptor_error;
842 856
843 /* If it's not an interrupt endpoint, we'd better punt! */ 857 /* If it's not an interrupt endpoint, we'd better punt! */
844 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 858 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
845 != USB_ENDPOINT_XFER_INT) 859 != USB_ENDPOINT_XFER_INT)
846 goto descriptor_error; 860 goto descriptor_error;
847 861
848 /* We found a hub */ 862 /* We found a hub */
849 dev_info (&intf->dev, "USB hub found\n"); 863 dev_info (&intf->dev, "USB hub found\n");
850 864
851 hub = kzalloc(sizeof(*hub), GFP_KERNEL); 865 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
852 if (!hub) { 866 if (!hub) {
853 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 867 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
854 return -ENOMEM; 868 return -ENOMEM;
855 } 869 }
856 870
857 INIT_LIST_HEAD(&hub->event_list); 871 INIT_LIST_HEAD(&hub->event_list);
858 hub->intfdev = &intf->dev; 872 hub->intfdev = &intf->dev;
859 hub->hdev = hdev; 873 hub->hdev = hdev;
860 INIT_WORK(&hub->leds, led_work, hub); 874 INIT_WORK(&hub->leds, led_work, hub);
861 875
862 usb_set_intfdata (intf, hub); 876 usb_set_intfdata (intf, hub);
863 877
864 if (hdev->speed == USB_SPEED_HIGH) 878 if (hdev->speed == USB_SPEED_HIGH)
865 highspeed_hubs++; 879 highspeed_hubs++;
866 880
867 if (hub_configure(hub, endpoint) >= 0) 881 if (hub_configure(hub, endpoint) >= 0)
868 return 0; 882 return 0;
869 883
870 hub_disconnect (intf); 884 hub_disconnect (intf);
871 return -ENODEV; 885 return -ENODEV;
872 } 886 }
873 887
874 static int 888 static int
875 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 889 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
876 { 890 {
877 struct usb_device *hdev = interface_to_usbdev (intf); 891 struct usb_device *hdev = interface_to_usbdev (intf);
878 892
879 /* assert ifno == 0 (part of hub spec) */ 893 /* assert ifno == 0 (part of hub spec) */
880 switch (code) { 894 switch (code) {
881 case USBDEVFS_HUB_PORTINFO: { 895 case USBDEVFS_HUB_PORTINFO: {
882 struct usbdevfs_hub_portinfo *info = user_data; 896 struct usbdevfs_hub_portinfo *info = user_data;
883 int i; 897 int i;
884 898
885 spin_lock_irq(&device_state_lock); 899 spin_lock_irq(&device_state_lock);
886 if (hdev->devnum <= 0) 900 if (hdev->devnum <= 0)
887 info->nports = 0; 901 info->nports = 0;
888 else { 902 else {
889 info->nports = hdev->maxchild; 903 info->nports = hdev->maxchild;
890 for (i = 0; i < info->nports; i++) { 904 for (i = 0; i < info->nports; i++) {
891 if (hdev->children[i] == NULL) 905 if (hdev->children[i] == NULL)
892 info->port[i] = 0; 906 info->port[i] = 0;
893 else 907 else
894 info->port[i] = 908 info->port[i] =
895 hdev->children[i]->devnum; 909 hdev->children[i]->devnum;
896 } 910 }
897 } 911 }
898 spin_unlock_irq(&device_state_lock); 912 spin_unlock_irq(&device_state_lock);
899 913
900 return info->nports + 1; 914 return info->nports + 1;
901 } 915 }
902 916
903 default: 917 default:
904 return -ENOSYS; 918 return -ENOSYS;
905 } 919 }
906 } 920 }
907 921
908 922
909 /* grab device/port lock, returning index of that port (zero based). 923 /* grab device/port lock, returning index of that port (zero based).
910 * protects the upstream link used by this device from concurrent 924 * protects the upstream link used by this device from concurrent
911 * tree operations like suspend, resume, reset, and disconnect, which 925 * tree operations like suspend, resume, reset, and disconnect, which
912 * apply to everything downstream of a given port. 926 * apply to everything downstream of a given port.
913 */ 927 */
914 static int locktree(struct usb_device *udev) 928 static int locktree(struct usb_device *udev)
915 { 929 {
916 int t; 930 int t;
917 struct usb_device *hdev; 931 struct usb_device *hdev;
918 932
919 if (!udev) 933 if (!udev)
920 return -ENODEV; 934 return -ENODEV;
921 935
922 /* root hub is always the first lock in the series */ 936 /* root hub is always the first lock in the series */
923 hdev = udev->parent; 937 hdev = udev->parent;
924 if (!hdev) { 938 if (!hdev) {
925 usb_lock_device(udev); 939 usb_lock_device(udev);
926 return 0; 940 return 0;
927 } 941 }
928 942
929 /* on the path from root to us, lock everything from 943 /* on the path from root to us, lock everything from
930 * top down, dropping parent locks when not needed 944 * top down, dropping parent locks when not needed
931 */ 945 */
932 t = locktree(hdev); 946 t = locktree(hdev);
933 if (t < 0) 947 if (t < 0)
934 return t; 948 return t;
935 for (t = 0; t < hdev->maxchild; t++) { 949 for (t = 0; t < hdev->maxchild; t++) {
936 if (hdev->children[t] == udev) { 950 if (hdev->children[t] == udev) {
937 /* everything is fail-fast once disconnect 951 /* everything is fail-fast once disconnect
938 * processing starts 952 * processing starts
939 */ 953 */
940 if (udev->state == USB_STATE_NOTATTACHED) 954 if (udev->state == USB_STATE_NOTATTACHED)
941 break; 955 break;
942 956
943 /* when everyone grabs locks top->bottom, 957 /* when everyone grabs locks top->bottom,
944 * non-overlapping work may be concurrent 958 * non-overlapping work may be concurrent
945 */ 959 */
946 usb_lock_device(udev); 960 usb_lock_device(udev);
947 usb_unlock_device(hdev); 961 usb_unlock_device(hdev);
948 return t + 1; 962 return t + 1;
949 } 963 }
950 } 964 }
951 usb_unlock_device(hdev); 965 usb_unlock_device(hdev);
952 return -ENODEV; 966 return -ENODEV;
953 } 967 }
954 968
955 static void recursively_mark_NOTATTACHED(struct usb_device *udev) 969 static void recursively_mark_NOTATTACHED(struct usb_device *udev)
956 { 970 {
957 int i; 971 int i;
958 972
959 for (i = 0; i < udev->maxchild; ++i) { 973 for (i = 0; i < udev->maxchild; ++i) {
960 if (udev->children[i]) 974 if (udev->children[i])
961 recursively_mark_NOTATTACHED(udev->children[i]); 975 recursively_mark_NOTATTACHED(udev->children[i]);
962 } 976 }
963 udev->state = USB_STATE_NOTATTACHED; 977 udev->state = USB_STATE_NOTATTACHED;
964 } 978 }
965 979
966 /** 980 /**
967 * usb_set_device_state - change a device's current state (usbcore, hcds) 981 * usb_set_device_state - change a device's current state (usbcore, hcds)
968 * @udev: pointer to device whose state should be changed 982 * @udev: pointer to device whose state should be changed
969 * @new_state: new state value to be stored 983 * @new_state: new state value to be stored
970 * 984 *
971 * udev->state is _not_ fully protected by the device lock. Although 985 * udev->state is _not_ fully protected by the device lock. Although
972 * most transitions are made only while holding the lock, the state can 986 * most transitions are made only while holding the lock, the state can
973 * can change to USB_STATE_NOTATTACHED at almost any time. This 987 * can change to USB_STATE_NOTATTACHED at almost any time. This
974 * is so that devices can be marked as disconnected as soon as possible, 988 * is so that devices can be marked as disconnected as soon as possible,
975 * without having to wait for any semaphores to be released. As a result, 989 * without having to wait for any semaphores to be released. As a result,
976 * all changes to any device's state must be protected by the 990 * all changes to any device's state must be protected by the
977 * device_state_lock spinlock. 991 * device_state_lock spinlock.
978 * 992 *
979 * Once a device has been added to the device tree, all changes to its state 993 * Once a device has been added to the device tree, all changes to its state
980 * should be made using this routine. The state should _not_ be set directly. 994 * should be made using this routine. The state should _not_ be set directly.
981 * 995 *
982 * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 996 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
983 * Otherwise udev->state is set to new_state, and if new_state is 997 * Otherwise udev->state is set to new_state, and if new_state is
984 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 998 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
985 * to USB_STATE_NOTATTACHED. 999 * to USB_STATE_NOTATTACHED.
986 */ 1000 */
987 void usb_set_device_state(struct usb_device *udev, 1001 void usb_set_device_state(struct usb_device *udev,
988 enum usb_device_state new_state) 1002 enum usb_device_state new_state)
989 { 1003 {
990 unsigned long flags; 1004 unsigned long flags;
991 1005
992 spin_lock_irqsave(&device_state_lock, flags); 1006 spin_lock_irqsave(&device_state_lock, flags);
993 if (udev->state == USB_STATE_NOTATTACHED) 1007 if (udev->state == USB_STATE_NOTATTACHED)
994 ; /* do nothing */ 1008 ; /* do nothing */
995 else if (new_state != USB_STATE_NOTATTACHED) { 1009 else if (new_state != USB_STATE_NOTATTACHED) {
996 udev->state = new_state; 1010 udev->state = new_state;
997 if (new_state == USB_STATE_CONFIGURED) 1011 if (new_state == USB_STATE_CONFIGURED)
998 device_init_wakeup(&udev->dev, 1012 device_init_wakeup(&udev->dev,
999 (udev->actconfig->desc.bmAttributes 1013 (udev->actconfig->desc.bmAttributes
1000 & USB_CONFIG_ATT_WAKEUP)); 1014 & USB_CONFIG_ATT_WAKEUP));
1001 else if (new_state != USB_STATE_SUSPENDED) 1015 else if (new_state != USB_STATE_SUSPENDED)
1002 device_init_wakeup(&udev->dev, 0); 1016 device_init_wakeup(&udev->dev, 0);
1003 } else 1017 } else
1004 recursively_mark_NOTATTACHED(udev); 1018 recursively_mark_NOTATTACHED(udev);
1005 spin_unlock_irqrestore(&device_state_lock, flags); 1019 spin_unlock_irqrestore(&device_state_lock, flags);
1006 } 1020 }
1007 EXPORT_SYMBOL(usb_set_device_state); 1021 EXPORT_SYMBOL(usb_set_device_state);
1008 1022
1009 1023
1010 #ifdef CONFIG_PM 1024 #ifdef CONFIG_PM
1011 1025
1012 /** 1026 /**
1013 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 1027 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
1014 * @rhdev: struct usb_device for the root hub 1028 * @rhdev: struct usb_device for the root hub
1015 * 1029 *
1016 * The USB host controller driver calls this function when its root hub 1030 * The USB host controller driver calls this function when its root hub
1017 * is resumed and Vbus power has been interrupted or the controller 1031 * is resumed and Vbus power has been interrupted or the controller
1018 * has been reset. The routine marks all the children of the root hub 1032 * has been reset. The routine marks all the children of the root hub
1019 * as NOTATTACHED and marks logical connect-change events on their ports. 1033 * as NOTATTACHED and marks logical connect-change events on their ports.
1020 */ 1034 */
1021 void usb_root_hub_lost_power(struct usb_device *rhdev) 1035 void usb_root_hub_lost_power(struct usb_device *rhdev)
1022 { 1036 {
1023 struct usb_hub *hub; 1037 struct usb_hub *hub;
1024 int port1; 1038 int port1;
1025 unsigned long flags; 1039 unsigned long flags;
1026 1040
1027 dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); 1041 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
1028 spin_lock_irqsave(&device_state_lock, flags); 1042 spin_lock_irqsave(&device_state_lock, flags);
1029 hub = hdev_to_hub(rhdev); 1043 hub = hdev_to_hub(rhdev);
1030 for (port1 = 1; port1 <= rhdev->maxchild; ++port1) { 1044 for (port1 = 1; port1 <= rhdev->maxchild; ++port1) {
1031 if (rhdev->children[port1 - 1]) { 1045 if (rhdev->children[port1 - 1]) {
1032 recursively_mark_NOTATTACHED( 1046 recursively_mark_NOTATTACHED(
1033 rhdev->children[port1 - 1]); 1047 rhdev->children[port1 - 1]);
1034 set_bit(port1, hub->change_bits); 1048 set_bit(port1, hub->change_bits);
1035 } 1049 }
1036 } 1050 }
1037 spin_unlock_irqrestore(&device_state_lock, flags); 1051 spin_unlock_irqrestore(&device_state_lock, flags);
1038 } 1052 }
1039 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 1053 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
1040 1054
1041 #endif 1055 #endif
1042 1056
1043 static void choose_address(struct usb_device *udev) 1057 static void choose_address(struct usb_device *udev)
1044 { 1058 {
1045 int devnum; 1059 int devnum;
1046 struct usb_bus *bus = udev->bus; 1060 struct usb_bus *bus = udev->bus;
1047 1061
1048 /* If khubd ever becomes multithreaded, this will need a lock */ 1062 /* If khubd ever becomes multithreaded, this will need a lock */
1049 1063
1050 /* Try to allocate the next devnum beginning at bus->devnum_next. */ 1064 /* Try to allocate the next devnum beginning at bus->devnum_next. */
1051 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1065 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1052 bus->devnum_next); 1066 bus->devnum_next);
1053 if (devnum >= 128) 1067 if (devnum >= 128)
1054 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1); 1068 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1);
1055 1069
1056 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); 1070 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1057 1071
1058 if (devnum < 128) { 1072 if (devnum < 128) {
1059 set_bit(devnum, bus->devmap.devicemap); 1073 set_bit(devnum, bus->devmap.devicemap);
1060 udev->devnum = devnum; 1074 udev->devnum = devnum;
1061 } 1075 }
1062 } 1076 }
1063 1077
1064 static void release_address(struct usb_device *udev) 1078 static void release_address(struct usb_device *udev)
1065 { 1079 {
1066 if (udev->devnum > 0) { 1080 if (udev->devnum > 0) {
1067 clear_bit(udev->devnum, udev->bus->devmap.devicemap); 1081 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1068 udev->devnum = -1; 1082 udev->devnum = -1;
1069 } 1083 }
1070 } 1084 }
1071 1085
1072 /** 1086 /**
1073 * usb_disconnect - disconnect a device (usbcore-internal) 1087 * usb_disconnect - disconnect a device (usbcore-internal)
1074 * @pdev: pointer to device being disconnected 1088 * @pdev: pointer to device being disconnected
1075 * Context: !in_interrupt () 1089 * Context: !in_interrupt ()
1076 * 1090 *
1077 * Something got disconnected. Get rid of it and all of its children. 1091 * Something got disconnected. Get rid of it and all of its children.
1078 * 1092 *
1079 * If *pdev is a normal device then the parent hub must already be locked. 1093 * If *pdev is a normal device then the parent hub must already be locked.
1080 * If *pdev is a root hub then this routine will acquire the 1094 * If *pdev is a root hub then this routine will acquire the
1081 * usb_bus_list_lock on behalf of the caller. 1095 * usb_bus_list_lock on behalf of the caller.
1082 * 1096 *
1083 * Only hub drivers (including virtual root hub drivers for host 1097 * Only hub drivers (including virtual root hub drivers for host
1084 * controllers) should ever call this. 1098 * controllers) should ever call this.
1085 * 1099 *
1086 * This call is synchronous, and may not be used in an interrupt context. 1100 * This call is synchronous, and may not be used in an interrupt context.
1087 */ 1101 */
1088 void usb_disconnect(struct usb_device **pdev) 1102 void usb_disconnect(struct usb_device **pdev)
1089 { 1103 {
1090 struct usb_device *udev = *pdev; 1104 struct usb_device *udev = *pdev;
1091 int i; 1105 int i;
1092 1106
1093 if (!udev) { 1107 if (!udev) {
1094 pr_debug ("%s nodev\n", __FUNCTION__); 1108 pr_debug ("%s nodev\n", __FUNCTION__);
1095 return; 1109 return;
1096 } 1110 }
1097 1111
1098 /* mark the device as inactive, so any further urb submissions for 1112 /* mark the device as inactive, so any further urb submissions for
1099 * this device (and any of its children) will fail immediately. 1113 * this device (and any of its children) will fail immediately.
1100 * this quiesces everyting except pending urbs. 1114 * this quiesces everyting except pending urbs.
1101 */ 1115 */
1102 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1116 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1103 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum); 1117 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
1104 1118
1105 usb_lock_device(udev); 1119 usb_lock_device(udev);
1106 1120
1107 /* Free up all the children before we remove this device */ 1121 /* Free up all the children before we remove this device */
1108 for (i = 0; i < USB_MAXCHILDREN; i++) { 1122 for (i = 0; i < USB_MAXCHILDREN; i++) {
1109 if (udev->children[i]) 1123 if (udev->children[i])
1110 usb_disconnect(&udev->children[i]); 1124 usb_disconnect(&udev->children[i]);
1111 } 1125 }
1112 1126
1113 /* deallocate hcd/hardware state ... nuking all pending urbs and 1127 /* deallocate hcd/hardware state ... nuking all pending urbs and
1114 * cleaning up all state associated with the current configuration 1128 * cleaning up all state associated with the current configuration
1115 * so that the hardware is now fully quiesced. 1129 * so that the hardware is now fully quiesced.
1116 */ 1130 */
1117 usb_disable_device(udev, 0); 1131 usb_disable_device(udev, 0);
1118 1132
1119 usb_notify_remove_device(udev); 1133 usb_notify_remove_device(udev);
1120 1134
1121 /* Free the device number, remove the /proc/bus/usb entry and 1135 /* Free the device number, remove the /proc/bus/usb entry and
1122 * the sysfs attributes, and delete the parent's children[] 1136 * the sysfs attributes, and delete the parent's children[]
1123 * (or root_hub) pointer. 1137 * (or root_hub) pointer.
1124 */ 1138 */
1125 dev_dbg (&udev->dev, "unregistering device\n"); 1139 dev_dbg (&udev->dev, "unregistering device\n");
1126 release_address(udev); 1140 release_address(udev);
1127 usb_remove_sysfs_dev_files(udev); 1141 usb_remove_sysfs_dev_files(udev);
1128 1142
1129 /* Avoid races with recursively_mark_NOTATTACHED() */ 1143 /* Avoid races with recursively_mark_NOTATTACHED() */
1130 spin_lock_irq(&device_state_lock); 1144 spin_lock_irq(&device_state_lock);
1131 *pdev = NULL; 1145 *pdev = NULL;
1132 spin_unlock_irq(&device_state_lock); 1146 spin_unlock_irq(&device_state_lock);
1133 1147
1134 usb_unlock_device(udev); 1148 usb_unlock_device(udev);
1135 1149
1136 device_unregister(&udev->dev); 1150 device_unregister(&udev->dev);
1137 } 1151 }
1138 1152
1153 static inline const char *plural(int n)
1154 {
1155 return (n == 1 ? "" : "s");
1156 }
1157
1139 static int choose_configuration(struct usb_device *udev) 1158 static int choose_configuration(struct usb_device *udev)
1140 { 1159 {
1141 int c, i; 1160 int i;
1161 u16 devstatus;
1162 int bus_powered;
1163 int num_configs;
1164 struct usb_host_config *c, *best;
1142 1165
1143 /* NOTE: this should interact with hub power budgeting */ 1166 /* If this fails, assume the device is bus-powered */
1167 devstatus = 0;
1168 usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
1169 le16_to_cpus(&devstatus);
1170 bus_powered = ((devstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0);
1171 dev_dbg(&udev->dev, "device is %s-powered\n",
1172 bus_powered ? "bus" : "self");
1144 1173
1145 c = udev->config[0].desc.bConfigurationValue; 1174 best = NULL;
1146 if (udev->descriptor.bNumConfigurations != 1) { 1175 c = udev->config;
1147 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) { 1176 num_configs = udev->descriptor.bNumConfigurations;
1148 struct usb_interface_descriptor *desc; 1177 for (i = 0; i < num_configs; (i++, c++)) {
1178 struct usb_interface_descriptor *desc =
1179 &c->intf_cache[0]->altsetting->desc;
1149 1180
1150 /* heuristic: Linux is more likely to have class 1181 /*
1151 * drivers, so avoid vendor-specific interfaces. 1182 * HP's USB bus-powered keyboard has only one configuration
1152 */ 1183 * and it claims to be self-powered; other devices may have
1153 desc = &udev->config[i].intf_cache[0] 1184 * similar errors in their descriptors. If the next test
1154 ->altsetting->desc; 1185 * were allowed to execute, such configurations would always
1155 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) 1186 * be rejected and the devices would not work as expected.
1156 continue; 1187 */
1157 /* COMM/2/all is CDC ACM, except 0xff is MSFT RNDIS. 1188 #if 0
1158 * MSFT needs this to be the first config; never use 1189 /* Rule out self-powered configs for a bus-powered device */
1159 * it as the default unless Linux has host-side RNDIS. 1190 if (bus_powered && (c->desc.bmAttributes &
1160 * A second config would ideally be CDC-Ethernet, but 1191 USB_CONFIG_ATT_SELFPOWER))
1161 * may instead be the "vendor specific" CDC subset 1192 continue;
1162 * long used by ARM Linux for sa1100 or pxa255. 1193 #endif
1163 */ 1194
1164 if (desc->bInterfaceClass == USB_CLASS_COMM 1195 /*
1165 && desc->bInterfaceSubClass == 2 1196 * The next test may not be as effective as it should be.
1166 && desc->bInterfaceProtocol == 0xff) { 1197 * Some hubs have errors in their descriptor, claiming
1167 c = udev->config[1].desc.bConfigurationValue; 1198 * to be self-powered when they are really bus-powered.
1168 continue; 1199 * We will overestimate the amount of current such hubs
1169 } 1200 * make available for each port.
1170 c = udev->config[i].desc.bConfigurationValue; 1201 *
1202 * This is a fairly benign sort of failure. It won't
1203 * cause us to reject configurations that we should have
1204 * accepted.
1205 */
1206
1207 /* Rule out configs that draw too much bus current */
1208 if (c->desc.bMaxPower * 2 > udev->bus_mA)
1209 continue;
1210
1211 /* If the first config's first interface is COMM/2/0xff
1212 * (MSFT RNDIS), rule it out unless Linux has host-side
1213 * RNDIS support. */
1214 if (i == 0 && desc->bInterfaceClass == USB_CLASS_COMM
1215 && desc->bInterfaceSubClass == 2
1216 && desc->bInterfaceProtocol == 0xff) {
1217 #ifndef CONFIG_USB_NET_RNDIS
1218 continue;
1219 #else
1220 best = c;
1221 #endif
1222 }
1223
1224 /* From the remaining configs, choose the first one whose
1225 * first interface is for a non-vendor-specific class.
1226 * Reason: Linux is more likely to have a class driver
1227 * than a vendor-specific driver. */
1228 else if (udev->descriptor.bDeviceClass !=
1229 USB_CLASS_VENDOR_SPEC &&
1230 desc->bInterfaceClass !=
1231 USB_CLASS_VENDOR_SPEC) {
1232 best = c;
1171 break; 1233 break;
1172 } 1234 }
1235
1236 /* If all the remaining configs are vendor-specific,
1237 * choose the first one. */
1238 else if (!best)
1239 best = c;
1240 }
1241
1242 if (best) {
1243 i = best->desc.bConfigurationValue;
1173 dev_info(&udev->dev, 1244 dev_info(&udev->dev,
1174 "configuration #%d chosen from %d choices\n", 1245 "configuration #%d chosen from %d choice%s\n",
1175 c, udev->descriptor.bNumConfigurations); 1246 i, num_configs, plural(num_configs));
1247 } else {
1248 i = -1;
1249 dev_warn(&udev->dev,
1250 "no configuration chosen from %d choice%s\n",
1251 num_configs, plural(num_configs));
1176 } 1252 }
1177 return c; 1253 return i;
1178 } 1254 }
1179 1255
1180 #ifdef DEBUG 1256 #ifdef DEBUG
1181 static void show_string(struct usb_device *udev, char *id, char *string) 1257 static void show_string(struct usb_device *udev, char *id, char *string)
1182 { 1258 {
1183 if (!string) 1259 if (!string)
1184 return; 1260 return;
1185 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); 1261 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1186 } 1262 }
1187 1263
1188 #else 1264 #else
1189 static inline void show_string(struct usb_device *udev, char *id, char *string) 1265 static inline void show_string(struct usb_device *udev, char *id, char *string)
1190 {} 1266 {}
1191 #endif 1267 #endif
1192 1268
1193 1269
1194 #ifdef CONFIG_USB_OTG 1270 #ifdef CONFIG_USB_OTG
1195 #include "otg_whitelist.h" 1271 #include "otg_whitelist.h"
1196 #endif 1272 #endif
1197 1273
1198 /** 1274 /**
1199 * usb_new_device - perform initial device setup (usbcore-internal) 1275 * usb_new_device - perform initial device setup (usbcore-internal)
1200 * @udev: newly addressed device (in ADDRESS state) 1276 * @udev: newly addressed device (in ADDRESS state)
1201 * 1277 *
1202 * This is called with devices which have been enumerated, but not yet 1278 * This is called with devices which have been enumerated, but not yet
1203 * configured. The device descriptor is available, but not descriptors 1279 * configured. The device descriptor is available, but not descriptors
1204 * for any device configuration. The caller must have locked either 1280 * for any device configuration. The caller must have locked either
1205 * the parent hub (if udev is a normal device) or else the 1281 * the parent hub (if udev is a normal device) or else the
1206 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to 1282 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
1207 * udev has already been installed, but udev is not yet visible through 1283 * udev has already been installed, but udev is not yet visible through
1208 * sysfs or other filesystem code. 1284 * sysfs or other filesystem code.
1209 * 1285 *
1210 * Returns 0 for success (device is configured and listed, with its 1286 * Returns 0 for success (device is configured and listed, with its
1211 * interfaces, in sysfs); else a negative errno value. 1287 * interfaces, in sysfs); else a negative errno value.
1212 * 1288 *
1213 * This call is synchronous, and may not be used in an interrupt context. 1289 * This call is synchronous, and may not be used in an interrupt context.
1214 * 1290 *
1215 * Only the hub driver or root-hub registrar should ever call this. 1291 * Only the hub driver or root-hub registrar should ever call this.
1216 */ 1292 */
1217 int usb_new_device(struct usb_device *udev) 1293 int usb_new_device(struct usb_device *udev)
1218 { 1294 {
1219 int err; 1295 int err;
1220 int c; 1296 int c;
1221 1297
1222 err = usb_get_configuration(udev); 1298 err = usb_get_configuration(udev);
1223 if (err < 0) { 1299 if (err < 0) {
1224 dev_err(&udev->dev, "can't read configurations, error %d\n", 1300 dev_err(&udev->dev, "can't read configurations, error %d\n",
1225 err); 1301 err);
1226 goto fail; 1302 goto fail;
1227 } 1303 }
1228 1304
1229 /* read the standard strings and cache them if present */ 1305 /* read the standard strings and cache them if present */
1230 udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 1306 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1231 udev->manufacturer = usb_cache_string(udev, 1307 udev->manufacturer = usb_cache_string(udev,
1232 udev->descriptor.iManufacturer); 1308 udev->descriptor.iManufacturer);
1233 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 1309 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1234 1310
1235 /* Tell the world! */ 1311 /* Tell the world! */
1236 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, " 1312 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, "
1237 "SerialNumber=%d\n", 1313 "SerialNumber=%d\n",
1238 udev->descriptor.iManufacturer, 1314 udev->descriptor.iManufacturer,
1239 udev->descriptor.iProduct, 1315 udev->descriptor.iProduct,
1240 udev->descriptor.iSerialNumber); 1316 udev->descriptor.iSerialNumber);
1241 show_string(udev, "Product", udev->product); 1317 show_string(udev, "Product", udev->product);
1242 show_string(udev, "Manufacturer", udev->manufacturer); 1318 show_string(udev, "Manufacturer", udev->manufacturer);
1243 show_string(udev, "SerialNumber", udev->serial); 1319 show_string(udev, "SerialNumber", udev->serial);
1244 1320
1245 #ifdef CONFIG_USB_OTG 1321 #ifdef CONFIG_USB_OTG
1246 /* 1322 /*
1247 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 1323 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1248 * to wake us after we've powered off VBUS; and HNP, switching roles 1324 * to wake us after we've powered off VBUS; and HNP, switching roles
1249 * "host" to "peripheral". The OTG descriptor helps figure this out. 1325 * "host" to "peripheral". The OTG descriptor helps figure this out.
1250 */ 1326 */
1251 if (!udev->bus->is_b_host 1327 if (!udev->bus->is_b_host
1252 && udev->config 1328 && udev->config
1253 && udev->parent == udev->bus->root_hub) { 1329 && udev->parent == udev->bus->root_hub) {
1254 struct usb_otg_descriptor *desc = 0; 1330 struct usb_otg_descriptor *desc = 0;
1255 struct usb_bus *bus = udev->bus; 1331 struct usb_bus *bus = udev->bus;
1256 1332
1257 /* descriptor may appear anywhere in config */ 1333 /* descriptor may appear anywhere in config */
1258 if (__usb_get_extra_descriptor (udev->rawdescriptors[0], 1334 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1259 le16_to_cpu(udev->config[0].desc.wTotalLength), 1335 le16_to_cpu(udev->config[0].desc.wTotalLength),
1260 USB_DT_OTG, (void **) &desc) == 0) { 1336 USB_DT_OTG, (void **) &desc) == 0) {
1261 if (desc->bmAttributes & USB_OTG_HNP) { 1337 if (desc->bmAttributes & USB_OTG_HNP) {
1262 unsigned port1; 1338 unsigned port1;
1263 struct usb_device *root = udev->parent; 1339 struct usb_device *root = udev->parent;
1264 1340
1265 for (port1 = 1; port1 <= root->maxchild; 1341 for (port1 = 1; port1 <= root->maxchild;
1266 port1++) { 1342 port1++) {
1267 if (root->children[port1-1] == udev) 1343 if (root->children[port1-1] == udev)
1268 break; 1344 break;
1269 } 1345 }
1270 1346
1271 dev_info(&udev->dev, 1347 dev_info(&udev->dev,
1272 "Dual-Role OTG device on %sHNP port\n", 1348 "Dual-Role OTG device on %sHNP port\n",
1273 (port1 == bus->otg_port) 1349 (port1 == bus->otg_port)
1274 ? "" : "non-"); 1350 ? "" : "non-");
1275 1351
1276 /* enable HNP before suspend, it's simpler */ 1352 /* enable HNP before suspend, it's simpler */
1277 if (port1 == bus->otg_port) 1353 if (port1 == bus->otg_port)
1278 bus->b_hnp_enable = 1; 1354 bus->b_hnp_enable = 1;
1279 err = usb_control_msg(udev, 1355 err = usb_control_msg(udev,
1280 usb_sndctrlpipe(udev, 0), 1356 usb_sndctrlpipe(udev, 0),
1281 USB_REQ_SET_FEATURE, 0, 1357 USB_REQ_SET_FEATURE, 0,
1282 bus->b_hnp_enable 1358 bus->b_hnp_enable
1283 ? USB_DEVICE_B_HNP_ENABLE 1359 ? USB_DEVICE_B_HNP_ENABLE
1284 : USB_DEVICE_A_ALT_HNP_SUPPORT, 1360 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1285 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 1361 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1286 if (err < 0) { 1362 if (err < 0) {
1287 /* OTG MESSAGE: report errors here, 1363 /* OTG MESSAGE: report errors here,
1288 * customize to match your product. 1364 * customize to match your product.
1289 */ 1365 */
1290 dev_info(&udev->dev, 1366 dev_info(&udev->dev,
1291 "can't set HNP mode; %d\n", 1367 "can't set HNP mode; %d\n",
1292 err); 1368 err);
1293 bus->b_hnp_enable = 0; 1369 bus->b_hnp_enable = 0;
1294 } 1370 }
1295 } 1371 }
1296 } 1372 }
1297 } 1373 }
1298 1374
1299 if (!is_targeted(udev)) { 1375 if (!is_targeted(udev)) {
1300 1376
1301 /* Maybe it can talk to us, though we can't talk to it. 1377 /* Maybe it can talk to us, though we can't talk to it.
1302 * (Includes HNP test device.) 1378 * (Includes HNP test device.)
1303 */ 1379 */
1304 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 1380 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1305 static int __usb_suspend_device(struct usb_device *, 1381 static int __usb_suspend_device(struct usb_device *,
1306 int port1); 1382 int port1);
1307 err = __usb_suspend_device(udev, udev->bus->otg_port); 1383 err = __usb_suspend_device(udev, udev->bus->otg_port);
1308 if (err < 0) 1384 if (err < 0)
1309 dev_dbg(&udev->dev, "HNP fail, %d\n", err); 1385 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1310 } 1386 }
1311 err = -ENODEV; 1387 err = -ENODEV;
1312 goto fail; 1388 goto fail;
1313 } 1389 }
1314 #endif 1390 #endif
1315 1391
1316 /* put device-specific files into sysfs */ 1392 /* put device-specific files into sysfs */
1317 err = device_add (&udev->dev); 1393 err = device_add (&udev->dev);
1318 if (err) { 1394 if (err) {
1319 dev_err(&udev->dev, "can't device_add, error %d\n", err); 1395 dev_err(&udev->dev, "can't device_add, error %d\n", err);
1320 goto fail; 1396 goto fail;
1321 } 1397 }
1322 usb_create_sysfs_dev_files (udev); 1398 usb_create_sysfs_dev_files (udev);
1323 1399
1324 usb_lock_device(udev); 1400 usb_lock_device(udev);
1325 1401
1326 /* choose and set the configuration. that registers the interfaces 1402 /* choose and set the configuration. that registers the interfaces
1327 * with the driver core, and lets usb device drivers bind to them. 1403 * with the driver core, and lets usb device drivers bind to them.
1328 */ 1404 */
1329 c = choose_configuration(udev); 1405 c = choose_configuration(udev);
1330 if (c < 0) 1406 if (c >= 0) {
1331 dev_warn(&udev->dev,
1332 "can't choose an initial configuration\n");
1333 else {
1334 err = usb_set_configuration(udev, c); 1407 err = usb_set_configuration(udev, c);
1335 if (err) { 1408 if (err) {
1336 dev_err(&udev->dev, "can't set config #%d, error %d\n", 1409 dev_err(&udev->dev, "can't set config #%d, error %d\n",
1337 c, err); 1410 c, err);
1338 usb_remove_sysfs_dev_files(udev); 1411 /* This need not be fatal. The user can try to
1339 device_del(&udev->dev); 1412 * set other configurations. */
1340 goto fail;
1341 } 1413 }
1342 } 1414 }
1343 1415
1344 /* USB device state == configured ... usable */ 1416 /* USB device state == configured ... usable */
1345 usb_notify_add_device(udev); 1417 usb_notify_add_device(udev);
1346 1418
1347 usb_unlock_device(udev); 1419 usb_unlock_device(udev);
1348 1420
1349 return 0; 1421 return 0;
1350 1422
1351 fail: 1423 fail:
1352 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1424 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1353 return err; 1425 return err;
1354 } 1426 }
1355 1427
1356 1428
1357 static int hub_port_status(struct usb_hub *hub, int port1, 1429 static int hub_port_status(struct usb_hub *hub, int port1,
1358 u16 *status, u16 *change) 1430 u16 *status, u16 *change)
1359 { 1431 {
1360 int ret; 1432 int ret;
1361 1433
1362 ret = get_port_status(hub->hdev, port1, &hub->status->port); 1434 ret = get_port_status(hub->hdev, port1, &hub->status->port);
1363 if (ret < 0) 1435 if (ret < 0)
1364 dev_err (hub->intfdev, 1436 dev_err (hub->intfdev,
1365 "%s failed (err = %d)\n", __FUNCTION__, ret); 1437 "%s failed (err = %d)\n", __FUNCTION__, ret);
1366 else { 1438 else {
1367 *status = le16_to_cpu(hub->status->port.wPortStatus); 1439 *status = le16_to_cpu(hub->status->port.wPortStatus);
1368 *change = le16_to_cpu(hub->status->port.wPortChange); 1440 *change = le16_to_cpu(hub->status->port.wPortChange);
1369 ret = 0; 1441 ret = 0;
1370 } 1442 }
1371 return ret; 1443 return ret;
1372 } 1444 }
1373 1445
1374 #define PORT_RESET_TRIES 5 1446 #define PORT_RESET_TRIES 5
1375 #define SET_ADDRESS_TRIES 2 1447 #define SET_ADDRESS_TRIES 2
1376 #define GET_DESCRIPTOR_TRIES 2 1448 #define GET_DESCRIPTOR_TRIES 2
1377 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 1449 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
1378 #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first) 1450 #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
1379 1451
1380 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 1452 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
1381 #define HUB_SHORT_RESET_TIME 10 1453 #define HUB_SHORT_RESET_TIME 10
1382 #define HUB_LONG_RESET_TIME 200 1454 #define HUB_LONG_RESET_TIME 200
1383 #define HUB_RESET_TIMEOUT 500 1455 #define HUB_RESET_TIMEOUT 500
1384 1456
1385 static int hub_port_wait_reset(struct usb_hub *hub, int port1, 1457 static int hub_port_wait_reset(struct usb_hub *hub, int port1,
1386 struct usb_device *udev, unsigned int delay) 1458 struct usb_device *udev, unsigned int delay)
1387 { 1459 {
1388 int delay_time, ret; 1460 int delay_time, ret;
1389 u16 portstatus; 1461 u16 portstatus;
1390 u16 portchange; 1462 u16 portchange;
1391 1463
1392 for (delay_time = 0; 1464 for (delay_time = 0;
1393 delay_time < HUB_RESET_TIMEOUT; 1465 delay_time < HUB_RESET_TIMEOUT;
1394 delay_time += delay) { 1466 delay_time += delay) {
1395 /* wait to give the device a chance to reset */ 1467 /* wait to give the device a chance to reset */
1396 msleep(delay); 1468 msleep(delay);
1397 1469
1398 /* read and decode port status */ 1470 /* read and decode port status */
1399 ret = hub_port_status(hub, port1, &portstatus, &portchange); 1471 ret = hub_port_status(hub, port1, &portstatus, &portchange);
1400 if (ret < 0) 1472 if (ret < 0)
1401 return ret; 1473 return ret;
1402 1474
1403 /* Device went away? */ 1475 /* Device went away? */
1404 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 1476 if (!(portstatus & USB_PORT_STAT_CONNECTION))
1405 return -ENOTCONN; 1477 return -ENOTCONN;
1406 1478
1407 /* bomb out completely if something weird happened */ 1479 /* bomb out completely if something weird happened */
1408 if ((portchange & USB_PORT_STAT_C_CONNECTION)) 1480 if ((portchange & USB_PORT_STAT_C_CONNECTION))
1409 return -EINVAL; 1481 return -EINVAL;
1410 1482
1411 /* if we`ve finished resetting, then break out of the loop */ 1483 /* if we`ve finished resetting, then break out of the loop */
1412 if (!(portstatus & USB_PORT_STAT_RESET) && 1484 if (!(portstatus & USB_PORT_STAT_RESET) &&
1413 (portstatus & USB_PORT_STAT_ENABLE)) { 1485 (portstatus & USB_PORT_STAT_ENABLE)) {
1414 if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1486 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1415 udev->speed = USB_SPEED_HIGH; 1487 udev->speed = USB_SPEED_HIGH;
1416 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1488 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1417 udev->speed = USB_SPEED_LOW; 1489 udev->speed = USB_SPEED_LOW;
1418 else 1490 else
1419 udev->speed = USB_SPEED_FULL; 1491 udev->speed = USB_SPEED_FULL;
1420 return 0; 1492 return 0;
1421 } 1493 }
1422 1494
1423 /* switch to the long delay after two short delay failures */ 1495 /* switch to the long delay after two short delay failures */
1424 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 1496 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
1425 delay = HUB_LONG_RESET_TIME; 1497 delay = HUB_LONG_RESET_TIME;
1426 1498
1427 dev_dbg (hub->intfdev, 1499 dev_dbg (hub->intfdev,
1428 "port %d not reset yet, waiting %dms\n", 1500 "port %d not reset yet, waiting %dms\n",
1429 port1, delay); 1501 port1, delay);
1430 } 1502 }
1431 1503
1432 return -EBUSY; 1504 return -EBUSY;
1433 } 1505 }
1434 1506
1435 static int hub_port_reset(struct usb_hub *hub, int port1, 1507 static int hub_port_reset(struct usb_hub *hub, int port1,
1436 struct usb_device *udev, unsigned int delay) 1508 struct usb_device *udev, unsigned int delay)
1437 { 1509 {
1438 int i, status; 1510 int i, status;
1439 1511
1440 /* Reset the port */ 1512 /* Reset the port */
1441 for (i = 0; i < PORT_RESET_TRIES; i++) { 1513 for (i = 0; i < PORT_RESET_TRIES; i++) {
1442 status = set_port_feature(hub->hdev, 1514 status = set_port_feature(hub->hdev,
1443 port1, USB_PORT_FEAT_RESET); 1515 port1, USB_PORT_FEAT_RESET);
1444 if (status) 1516 if (status)
1445 dev_err(hub->intfdev, 1517 dev_err(hub->intfdev,
1446 "cannot reset port %d (err = %d)\n", 1518 "cannot reset port %d (err = %d)\n",
1447 port1, status); 1519 port1, status);
1448 else { 1520 else {
1449 status = hub_port_wait_reset(hub, port1, udev, delay); 1521 status = hub_port_wait_reset(hub, port1, udev, delay);
1450 if (status && status != -ENOTCONN) 1522 if (status && status != -ENOTCONN)
1451 dev_dbg(hub->intfdev, 1523 dev_dbg(hub->intfdev,
1452 "port_wait_reset: err = %d\n", 1524 "port_wait_reset: err = %d\n",
1453 status); 1525 status);
1454 } 1526 }
1455 1527
1456 /* return on disconnect or reset */ 1528 /* return on disconnect or reset */
1457 switch (status) { 1529 switch (status) {
1458 case 0: 1530 case 0:
1459 /* TRSTRCY = 10 ms; plus some extra */ 1531 /* TRSTRCY = 10 ms; plus some extra */
1460 msleep(10 + 40); 1532 msleep(10 + 40);
1461 /* FALL THROUGH */ 1533 /* FALL THROUGH */
1462 case -ENOTCONN: 1534 case -ENOTCONN:
1463 case -ENODEV: 1535 case -ENODEV:
1464 clear_port_feature(hub->hdev, 1536 clear_port_feature(hub->hdev,
1465 port1, USB_PORT_FEAT_C_RESET); 1537 port1, USB_PORT_FEAT_C_RESET);
1466 /* FIXME need disconnect() for NOTATTACHED device */ 1538 /* FIXME need disconnect() for NOTATTACHED device */
1467 usb_set_device_state(udev, status 1539 usb_set_device_state(udev, status
1468 ? USB_STATE_NOTATTACHED 1540 ? USB_STATE_NOTATTACHED
1469 : USB_STATE_DEFAULT); 1541 : USB_STATE_DEFAULT);
1470 return status; 1542 return status;
1471 } 1543 }
1472 1544
1473 dev_dbg (hub->intfdev, 1545 dev_dbg (hub->intfdev,
1474 "port %d not enabled, trying reset again...\n", 1546 "port %d not enabled, trying reset again...\n",
1475 port1); 1547 port1);
1476 delay = HUB_LONG_RESET_TIME; 1548 delay = HUB_LONG_RESET_TIME;
1477 } 1549 }
1478 1550
1479 dev_err (hub->intfdev, 1551 dev_err (hub->intfdev,
1480 "Cannot enable port %i. Maybe the USB cable is bad?\n", 1552 "Cannot enable port %i. Maybe the USB cable is bad?\n",
1481 port1); 1553 port1);
1482 1554
1483 return status; 1555 return status;
1484 } 1556 }
1485 1557
1486 /* 1558 /*
1487 * Disable a port and mark a logical connnect-change event, so that some 1559 * Disable a port and mark a logical connnect-change event, so that some
1488 * time later khubd will disconnect() any existing usb_device on the port 1560 * time later khubd will disconnect() any existing usb_device on the port
1489 * and will re-enumerate if there actually is a device attached. 1561 * and will re-enumerate if there actually is a device attached.
1490 */ 1562 */
1491 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 1563 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
1492 { 1564 {
1493 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 1565 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
1494 hub_port_disable(hub, port1, 1); 1566 hub_port_disable(hub, port1, 1);
1495 1567
1496 /* FIXME let caller ask to power down the port: 1568 /* FIXME let caller ask to power down the port:
1497 * - some devices won't enumerate without a VBUS power cycle 1569 * - some devices won't enumerate without a VBUS power cycle
1498 * - SRP saves power that way 1570 * - SRP saves power that way
1499 * - ... new call, TBD ... 1571 * - ... new call, TBD ...
1500 * That's easy if this hub can switch power per-port, and 1572 * That's easy if this hub can switch power per-port, and
1501 * khubd reactivates the port later (timer, SRP, etc). 1573 * khubd reactivates the port later (timer, SRP, etc).
1502 * Powerdown must be optional, because of reset/DFU. 1574 * Powerdown must be optional, because of reset/DFU.
1503 */ 1575 */
1504 1576
1505 set_bit(port1, hub->change_bits); 1577 set_bit(port1, hub->change_bits);
1506 kick_khubd(hub); 1578 kick_khubd(hub);
1507 } 1579 }
1508 1580
1509 1581
1510 #ifdef CONFIG_USB_SUSPEND 1582 #ifdef CONFIG_USB_SUSPEND
1511 1583
1512 /* 1584 /*
1513 * Selective port suspend reduces power; most suspended devices draw 1585 * Selective port suspend reduces power; most suspended devices draw
1514 * less than 500 uA. It's also used in OTG, along with remote wakeup. 1586 * less than 500 uA. It's also used in OTG, along with remote wakeup.
1515 * All devices below the suspended port are also suspended. 1587 * All devices below the suspended port are also suspended.
1516 * 1588 *
1517 * Devices leave suspend state when the host wakes them up. Some devices 1589 * Devices leave suspend state when the host wakes them up. Some devices
1518 * also support "remote wakeup", where the device can activate the USB 1590 * also support "remote wakeup", where the device can activate the USB
1519 * tree above them to deliver data, such as a keypress or packet. In 1591 * tree above them to deliver data, such as a keypress or packet. In
1520 * some cases, this wakes the USB host. 1592 * some cases, this wakes the USB host.
1521 */ 1593 */
1522 static int hub_port_suspend(struct usb_hub *hub, int port1, 1594 static int hub_port_suspend(struct usb_hub *hub, int port1,
1523 struct usb_device *udev) 1595 struct usb_device *udev)
1524 { 1596 {
1525 int status; 1597 int status;
1526 1598
1527 // dev_dbg(hub->intfdev, "suspend port %d\n", port1); 1599 // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
1528 1600
1529 /* enable remote wakeup when appropriate; this lets the device 1601 /* enable remote wakeup when appropriate; this lets the device
1530 * wake up the upstream hub (including maybe the root hub). 1602 * wake up the upstream hub (including maybe the root hub).
1531 * 1603 *
1532 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 1604 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
1533 * we don't explicitly enable it here. 1605 * we don't explicitly enable it here.
1534 */ 1606 */
1535 if (device_may_wakeup(&udev->dev)) { 1607 if (device_may_wakeup(&udev->dev)) {
1536 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 1608 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1537 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 1609 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
1538 USB_DEVICE_REMOTE_WAKEUP, 0, 1610 USB_DEVICE_REMOTE_WAKEUP, 0,
1539 NULL, 0, 1611 NULL, 0,
1540 USB_CTRL_SET_TIMEOUT); 1612 USB_CTRL_SET_TIMEOUT);
1541 if (status) 1613 if (status)
1542 dev_dbg(&udev->dev, 1614 dev_dbg(&udev->dev,
1543 "won't remote wakeup, status %d\n", 1615 "won't remote wakeup, status %d\n",
1544 status); 1616 status);
1545 } 1617 }
1546 1618
1547 /* see 7.1.7.6 */ 1619 /* see 7.1.7.6 */
1548 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND); 1620 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
1549 if (status) { 1621 if (status) {
1550 dev_dbg(hub->intfdev, 1622 dev_dbg(hub->intfdev,
1551 "can't suspend port %d, status %d\n", 1623 "can't suspend port %d, status %d\n",
1552 port1, status); 1624 port1, status);
1553 /* paranoia: "should not happen" */ 1625 /* paranoia: "should not happen" */
1554 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 1626 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1555 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 1627 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
1556 USB_DEVICE_REMOTE_WAKEUP, 0, 1628 USB_DEVICE_REMOTE_WAKEUP, 0,
1557 NULL, 0, 1629 NULL, 0,
1558 USB_CTRL_SET_TIMEOUT); 1630 USB_CTRL_SET_TIMEOUT);
1559 } else { 1631 } else {
1560 /* device has up to 10 msec to fully suspend */ 1632 /* device has up to 10 msec to fully suspend */
1561 dev_dbg(&udev->dev, "usb suspend\n"); 1633 dev_dbg(&udev->dev, "usb suspend\n");
1562 usb_set_device_state(udev, USB_STATE_SUSPENDED); 1634 usb_set_device_state(udev, USB_STATE_SUSPENDED);
1563 msleep(10); 1635 msleep(10);
1564 } 1636 }
1565 return status; 1637 return status;
1566 } 1638 }
1567 1639
1568 /* 1640 /*
1569 * Devices on USB hub ports have only one "suspend" state, corresponding 1641 * Devices on USB hub ports have only one "suspend" state, corresponding
1570 * to ACPI D2, "may cause the device to lose some context". 1642 * to ACPI D2, "may cause the device to lose some context".
1571 * State transitions include: 1643 * State transitions include:
1572 * 1644 *
1573 * - suspend, resume ... when the VBUS power link stays live 1645 * - suspend, resume ... when the VBUS power link stays live
1574 * - suspend, disconnect ... VBUS lost 1646 * - suspend, disconnect ... VBUS lost
1575 * 1647 *
1576 * Once VBUS drop breaks the circuit, the port it's using has to go through 1648 * Once VBUS drop breaks the circuit, the port it's using has to go through
1577 * normal re-enumeration procedures, starting with enabling VBUS power. 1649 * normal re-enumeration procedures, starting with enabling VBUS power.
1578 * Other than re-initializing the hub (plug/unplug, except for root hubs), 1650 * Other than re-initializing the hub (plug/unplug, except for root hubs),
1579 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 1651 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
1580 * timer, no SRP, no requests through sysfs. 1652 * timer, no SRP, no requests through sysfs.
1581 * 1653 *
1582 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when 1654 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
1583 * the root hub for their bus goes into global suspend ... so we don't 1655 * the root hub for their bus goes into global suspend ... so we don't
1584 * (falsely) update the device power state to say it suspended. 1656 * (falsely) update the device power state to say it suspended.
1585 */ 1657 */
1586 static int __usb_suspend_device (struct usb_device *udev, int port1) 1658 static int __usb_suspend_device (struct usb_device *udev, int port1)
1587 { 1659 {
1588 int status = 0; 1660 int status = 0;
1589 1661
1590 /* caller owns the udev device lock */ 1662 /* caller owns the udev device lock */
1591 if (port1 < 0) 1663 if (port1 < 0)
1592 return port1; 1664 return port1;
1593 1665
1594 if (udev->state == USB_STATE_SUSPENDED 1666 if (udev->state == USB_STATE_SUSPENDED
1595 || udev->state == USB_STATE_NOTATTACHED) { 1667 || udev->state == USB_STATE_NOTATTACHED) {
1596 return 0; 1668 return 0;
1597 } 1669 }
1598 1670
1599 /* all interfaces must already be suspended */ 1671 /* all interfaces must already be suspended */
1600 if (udev->actconfig) { 1672 if (udev->actconfig) {
1601 int i; 1673 int i;
1602 1674
1603 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1675 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1604 struct usb_interface *intf; 1676 struct usb_interface *intf;
1605 1677
1606 intf = udev->actconfig->interface[i]; 1678 intf = udev->actconfig->interface[i];
1607 if (is_active(intf)) { 1679 if (is_active(intf)) {
1608 dev_dbg(&intf->dev, "nyet suspended\n"); 1680 dev_dbg(&intf->dev, "nyet suspended\n");
1609 return -EBUSY; 1681 return -EBUSY;
1610 } 1682 }
1611 } 1683 }
1612 } 1684 }
1613 1685
1614 /* we only change a device's upstream USB link. 1686 /* we only change a device's upstream USB link.
1615 * root hubs have no upstream USB link. 1687 * root hubs have no upstream USB link.
1616 */ 1688 */
1617 if (udev->parent) 1689 if (udev->parent)
1618 status = hub_port_suspend(hdev_to_hub(udev->parent), port1, 1690 status = hub_port_suspend(hdev_to_hub(udev->parent), port1,
1619 udev); 1691 udev);
1620 1692
1621 if (status == 0) 1693 if (status == 0)
1622 udev->dev.power.power_state = PMSG_SUSPEND; 1694 udev->dev.power.power_state = PMSG_SUSPEND;
1623 return status; 1695 return status;
1624 } 1696 }
1625 1697
1626 #endif 1698 #endif
1627 1699
1628 /* 1700 /*
1629 * usb_suspend_device - suspend a usb device 1701 * usb_suspend_device - suspend a usb device
1630 * @udev: device that's no longer in active use 1702 * @udev: device that's no longer in active use
1631 * Context: must be able to sleep; device not locked; pm locks held 1703 * Context: must be able to sleep; device not locked; pm locks held
1632 * 1704 *
1633 * Suspends a USB device that isn't in active use, conserving power. 1705 * Suspends a USB device that isn't in active use, conserving power.
1634 * Devices may wake out of a suspend, if anything important happens, 1706 * Devices may wake out of a suspend, if anything important happens,
1635 * using the remote wakeup mechanism. They may also be taken out of 1707 * using the remote wakeup mechanism. They may also be taken out of
1636 * suspend by the host, using usb_resume_device(). It's also routine 1708 * suspend by the host, using usb_resume_device(). It's also routine
1637 * to disconnect devices while they are suspended. 1709 * to disconnect devices while they are suspended.
1638 * 1710 *
1639 * This only affects the USB hardware for a device; its interfaces 1711 * This only affects the USB hardware for a device; its interfaces
1640 * (and, for hubs, child devices) must already have been suspended. 1712 * (and, for hubs, child devices) must already have been suspended.
1641 * 1713 *
1642 * Suspending OTG devices may trigger HNP, if that's been enabled 1714 * Suspending OTG devices may trigger HNP, if that's been enabled
1643 * between a pair of dual-role devices. That will change roles, such 1715 * between a pair of dual-role devices. That will change roles, such
1644 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 1716 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
1645 * 1717 *
1646 * Returns 0 on success, else negative errno. 1718 * Returns 0 on success, else negative errno.
1647 */ 1719 */
1648 int usb_suspend_device(struct usb_device *udev) 1720 int usb_suspend_device(struct usb_device *udev)
1649 { 1721 {
1650 #ifdef CONFIG_USB_SUSPEND 1722 #ifdef CONFIG_USB_SUSPEND
1651 int port1; 1723 int port1;
1652 1724
1653 if (udev->state == USB_STATE_NOTATTACHED) 1725 if (udev->state == USB_STATE_NOTATTACHED)
1654 return -ENODEV; 1726 return -ENODEV;
1655 if (!udev->parent) 1727 if (!udev->parent)
1656 port1 = 0; 1728 port1 = 0;
1657 else { 1729 else {
1658 for (port1 = udev->parent->maxchild; port1 > 0; --port1) { 1730 for (port1 = udev->parent->maxchild; port1 > 0; --port1) {
1659 if (udev->parent->children[port1-1] == udev) 1731 if (udev->parent->children[port1-1] == udev)
1660 break; 1732 break;
1661 } 1733 }
1662 if (port1 == 0) 1734 if (port1 == 0)
1663 return -ENODEV; 1735 return -ENODEV;
1664 } 1736 }
1665 1737
1666 return __usb_suspend_device(udev, port1); 1738 return __usb_suspend_device(udev, port1);
1667 #else 1739 #else
1668 /* NOTE: udev->state unchanged, it's not lying ... */ 1740 /* NOTE: udev->state unchanged, it's not lying ... */
1669 udev->dev.power.power_state = PMSG_SUSPEND; 1741 udev->dev.power.power_state = PMSG_SUSPEND;
1670 return 0; 1742 return 0;
1671 #endif 1743 #endif
1672 } 1744 }
1673 1745
1674 /* 1746 /*
1675 * If the USB "suspend" state is in use (rather than "global suspend"), 1747 * If the USB "suspend" state is in use (rather than "global suspend"),
1676 * many devices will be individually taken out of suspend state using 1748 * many devices will be individually taken out of suspend state using
1677 * special" resume" signaling. These routines kick in shortly after 1749 * special" resume" signaling. These routines kick in shortly after
1678 * hardware resume signaling is finished, either because of selective 1750 * hardware resume signaling is finished, either because of selective
1679 * resume (by host) or remote wakeup (by device) ... now see what changed 1751 * resume (by host) or remote wakeup (by device) ... now see what changed
1680 * in the tree that's rooted at this device. 1752 * in the tree that's rooted at this device.
1681 */ 1753 */
1682 static int finish_device_resume(struct usb_device *udev) 1754 static int finish_device_resume(struct usb_device *udev)
1683 { 1755 {
1684 int status; 1756 int status;
1685 u16 devstatus; 1757 u16 devstatus;
1686 1758
1687 /* caller owns the udev device lock */ 1759 /* caller owns the udev device lock */
1688 dev_dbg(&udev->dev, "finish resume\n"); 1760 dev_dbg(&udev->dev, "finish resume\n");
1689 1761
1690 /* usb ch9 identifies four variants of SUSPENDED, based on what 1762 /* usb ch9 identifies four variants of SUSPENDED, based on what
1691 * state the device resumes to. Linux currently won't see the 1763 * state the device resumes to. Linux currently won't see the
1692 * first two on the host side; they'd be inside hub_port_init() 1764 * first two on the host side; they'd be inside hub_port_init()
1693 * during many timeouts, but khubd can't suspend until later. 1765 * during many timeouts, but khubd can't suspend until later.
1694 */ 1766 */
1695 usb_set_device_state(udev, udev->actconfig 1767 usb_set_device_state(udev, udev->actconfig
1696 ? USB_STATE_CONFIGURED 1768 ? USB_STATE_CONFIGURED
1697 : USB_STATE_ADDRESS); 1769 : USB_STATE_ADDRESS);
1698 udev->dev.power.power_state = PMSG_ON; 1770 udev->dev.power.power_state = PMSG_ON;
1699 1771
1700 /* 10.5.4.5 says be sure devices in the tree are still there. 1772 /* 10.5.4.5 says be sure devices in the tree are still there.
1701 * For now let's assume the device didn't go crazy on resume, 1773 * For now let's assume the device didn't go crazy on resume,
1702 * and device drivers will know about any resume quirks. 1774 * and device drivers will know about any resume quirks.
1703 */ 1775 */
1704 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 1776 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
1705 if (status < 0) 1777 if (status < 2)
1706 dev_dbg(&udev->dev, 1778 dev_dbg(&udev->dev,
1707 "gone after usb resume? status %d\n", 1779 "gone after usb resume? status %d\n",
1708 status); 1780 status);
1709 else if (udev->actconfig) { 1781 else if (udev->actconfig) {
1710 unsigned i; 1782 unsigned i;
1711 int (*resume)(struct device *); 1783 int (*resume)(struct device *);
1712 1784
1713 le16_to_cpus(&devstatus); 1785 le16_to_cpus(&devstatus);
1714 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP) 1786 if ((devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
1715 && udev->parent) { 1787 && udev->parent) {
1716 status = usb_control_msg(udev, 1788 status = usb_control_msg(udev,
1717 usb_sndctrlpipe(udev, 0), 1789 usb_sndctrlpipe(udev, 0),
1718 USB_REQ_CLEAR_FEATURE, 1790 USB_REQ_CLEAR_FEATURE,
1719 USB_RECIP_DEVICE, 1791 USB_RECIP_DEVICE,
1720 USB_DEVICE_REMOTE_WAKEUP, 0, 1792 USB_DEVICE_REMOTE_WAKEUP, 0,
1721 NULL, 0, 1793 NULL, 0,
1722 USB_CTRL_SET_TIMEOUT); 1794 USB_CTRL_SET_TIMEOUT);
1723 if (status) { 1795 if (status) {
1724 dev_dbg(&udev->dev, "disable remote " 1796 dev_dbg(&udev->dev, "disable remote "
1725 "wakeup, status %d\n", status); 1797 "wakeup, status %d\n", status);
1726 status = 0; 1798 status = 0;
1727 } 1799 }
1728 } 1800 }
1729 1801
1730 /* resume interface drivers; if this is a hub, it 1802 /* resume interface drivers; if this is a hub, it
1731 * may have a child resume event to deal with soon 1803 * may have a child resume event to deal with soon
1732 */ 1804 */
1733 resume = udev->dev.bus->resume; 1805 resume = udev->dev.bus->resume;
1734 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1806 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1735 struct device *dev = 1807 struct device *dev =
1736 &udev->actconfig->interface[i]->dev; 1808 &udev->actconfig->interface[i]->dev;
1737 1809
1738 down(&dev->sem); 1810 down(&dev->sem);
1739 (void) resume(dev); 1811 (void) resume(dev);
1740 up(&dev->sem); 1812 up(&dev->sem);
1741 } 1813 }
1742 status = 0; 1814 status = 0;
1743 1815
1744 } else if (udev->devnum <= 0) { 1816 } else if (udev->devnum <= 0) {
1745 dev_dbg(&udev->dev, "bogus resume!\n"); 1817 dev_dbg(&udev->dev, "bogus resume!\n");
1746 status = -EINVAL; 1818 status = -EINVAL;
1747 } 1819 }
1748 return status; 1820 return status;
1749 } 1821 }
1750 1822
1751 #ifdef CONFIG_USB_SUSPEND 1823 #ifdef CONFIG_USB_SUSPEND
1752 1824
1753 static int 1825 static int
1754 hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev) 1826 hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
1755 { 1827 {
1756 int status; 1828 int status;
1757 1829
1758 // dev_dbg(hub->intfdev, "resume port %d\n", port1); 1830 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
1759 1831
1760 /* see 7.1.7.7; affects power usage, but not budgeting */ 1832 /* see 7.1.7.7; affects power usage, but not budgeting */
1761 status = clear_port_feature(hub->hdev, 1833 status = clear_port_feature(hub->hdev,
1762 port1, USB_PORT_FEAT_SUSPEND); 1834 port1, USB_PORT_FEAT_SUSPEND);
1763 if (status) { 1835 if (status) {
1764 dev_dbg(hub->intfdev, 1836 dev_dbg(hub->intfdev,
1765 "can't resume port %d, status %d\n", 1837 "can't resume port %d, status %d\n",
1766 port1, status); 1838 port1, status);
1767 } else { 1839 } else {
1768 u16 devstatus; 1840 u16 devstatus;
1769 u16 portchange; 1841 u16 portchange;
1770 1842
1771 /* drive resume for at least 20 msec */ 1843 /* drive resume for at least 20 msec */
1772 if (udev) 1844 if (udev)
1773 dev_dbg(&udev->dev, "RESUME\n"); 1845 dev_dbg(&udev->dev, "RESUME\n");
1774 msleep(25); 1846 msleep(25);
1775 1847
1776 #define LIVE_FLAGS ( USB_PORT_STAT_POWER \ 1848 #define LIVE_FLAGS ( USB_PORT_STAT_POWER \
1777 | USB_PORT_STAT_ENABLE \ 1849 | USB_PORT_STAT_ENABLE \
1778 | USB_PORT_STAT_CONNECTION) 1850 | USB_PORT_STAT_CONNECTION)
1779 1851
1780 /* Virtual root hubs can trigger on GET_PORT_STATUS to 1852 /* Virtual root hubs can trigger on GET_PORT_STATUS to
1781 * stop resume signaling. Then finish the resume 1853 * stop resume signaling. Then finish the resume
1782 * sequence. 1854 * sequence.
1783 */ 1855 */
1784 devstatus = portchange = 0; 1856 devstatus = portchange = 0;
1785 status = hub_port_status(hub, port1, 1857 status = hub_port_status(hub, port1,
1786 &devstatus, &portchange); 1858 &devstatus, &portchange);
1787 if (status < 0 1859 if (status < 0
1788 || (devstatus & LIVE_FLAGS) != LIVE_FLAGS 1860 || (devstatus & LIVE_FLAGS) != LIVE_FLAGS
1789 || (devstatus & USB_PORT_STAT_SUSPEND) != 0 1861 || (devstatus & USB_PORT_STAT_SUSPEND) != 0
1790 ) { 1862 ) {
1791 dev_dbg(hub->intfdev, 1863 dev_dbg(hub->intfdev,
1792 "port %d status %04x.%04x after resume, %d\n", 1864 "port %d status %04x.%04x after resume, %d\n",
1793 port1, portchange, devstatus, status); 1865 port1, portchange, devstatus, status);
1794 } else { 1866 } else {
1795 /* TRSMRCY = 10 msec */ 1867 /* TRSMRCY = 10 msec */
1796 msleep(10); 1868 msleep(10);
1797 if (udev) 1869 if (udev)
1798 status = finish_device_resume(udev); 1870 status = finish_device_resume(udev);
1799 } 1871 }
1800 } 1872 }
1801 if (status < 0) 1873 if (status < 0)
1802 hub_port_logical_disconnect(hub, port1); 1874 hub_port_logical_disconnect(hub, port1);
1803 1875
1804 return status; 1876 return status;
1805 } 1877 }
1806 1878
1807 #endif 1879 #endif
1808 1880
1809 /* 1881 /*
1810 * usb_resume_device - re-activate a suspended usb device 1882 * usb_resume_device - re-activate a suspended usb device
1811 * @udev: device to re-activate 1883 * @udev: device to re-activate
1812 * Context: must be able to sleep; device not locked; pm locks held 1884 * Context: must be able to sleep; device not locked; pm locks held
1813 * 1885 *
1814 * This will re-activate the suspended device, increasing power usage 1886 * This will re-activate the suspended device, increasing power usage
1815 * while letting drivers communicate again with its endpoints. 1887 * while letting drivers communicate again with its endpoints.
1816 * USB resume explicitly guarantees that the power session between 1888 * USB resume explicitly guarantees that the power session between
1817 * the host and the device is the same as it was when the device 1889 * the host and the device is the same as it was when the device
1818 * suspended. 1890 * suspended.
1819 * 1891 *
1820 * Returns 0 on success, else negative errno. 1892 * Returns 0 on success, else negative errno.
1821 */ 1893 */
1822 int usb_resume_device(struct usb_device *udev) 1894 int usb_resume_device(struct usb_device *udev)
1823 { 1895 {
1824 int port1, status; 1896 int port1, status;
1825 1897
1826 if (udev->state == USB_STATE_NOTATTACHED) 1898 if (udev->state == USB_STATE_NOTATTACHED)
1827 return -ENODEV; 1899 return -ENODEV;
1828 if (!udev->parent) 1900 if (!udev->parent)
1829 port1 = 0; 1901 port1 = 0;
1830 else { 1902 else {
1831 for (port1 = udev->parent->maxchild; port1 > 0; --port1) { 1903 for (port1 = udev->parent->maxchild; port1 > 0; --port1) {
1832 if (udev->parent->children[port1-1] == udev) 1904 if (udev->parent->children[port1-1] == udev)
1833 break; 1905 break;
1834 } 1906 }
1835 if (port1 == 0) 1907 if (port1 == 0)
1836 return -ENODEV; 1908 return -ENODEV;
1837 } 1909 }
1838 1910
1839 #ifdef CONFIG_USB_SUSPEND 1911 #ifdef CONFIG_USB_SUSPEND
1840 /* selective resume of one downstream hub-to-device port */ 1912 /* selective resume of one downstream hub-to-device port */
1841 if (udev->parent) { 1913 if (udev->parent) {
1842 if (udev->state == USB_STATE_SUSPENDED) { 1914 if (udev->state == USB_STATE_SUSPENDED) {
1843 // NOTE swsusp may bork us, device state being wrong... 1915 // NOTE swsusp may bork us, device state being wrong...
1844 // NOTE this fails if parent is also suspended... 1916 // NOTE this fails if parent is also suspended...
1845 status = hub_port_resume(hdev_to_hub(udev->parent), 1917 status = hub_port_resume(hdev_to_hub(udev->parent),
1846 port1, udev); 1918 port1, udev);
1847 } else 1919 } else
1848 status = 0; 1920 status = 0;
1849 } else 1921 } else
1850 #endif 1922 #endif
1851 status = finish_device_resume(udev); 1923 status = finish_device_resume(udev);
1852 if (status < 0) 1924 if (status < 0)
1853 dev_dbg(&udev->dev, "can't resume, status %d\n", 1925 dev_dbg(&udev->dev, "can't resume, status %d\n",
1854 status); 1926 status);
1855 1927
1856 /* rebind drivers that had no suspend() */ 1928 /* rebind drivers that had no suspend() */
1857 if (status == 0) { 1929 if (status == 0) {
1858 usb_unlock_device(udev); 1930 usb_unlock_device(udev);
1859 bus_rescan_devices(&usb_bus_type); 1931 bus_rescan_devices(&usb_bus_type);
1860 usb_lock_device(udev); 1932 usb_lock_device(udev);
1861 } 1933 }
1862 return status; 1934 return status;
1863 } 1935 }
1864 1936
1865 static int remote_wakeup(struct usb_device *udev) 1937 static int remote_wakeup(struct usb_device *udev)
1866 { 1938 {
1867 int status = 0; 1939 int status = 0;
1868 1940
1869 #ifdef CONFIG_USB_SUSPEND 1941 #ifdef CONFIG_USB_SUSPEND
1870 1942
1871 /* don't repeat RESUME sequence if this device 1943 /* don't repeat RESUME sequence if this device
1872 * was already woken up by some other task 1944 * was already woken up by some other task
1873 */ 1945 */
1874 usb_lock_device(udev); 1946 usb_lock_device(udev);
1875 if (udev->state == USB_STATE_SUSPENDED) { 1947 if (udev->state == USB_STATE_SUSPENDED) {
1876 dev_dbg(&udev->dev, "RESUME (wakeup)\n"); 1948 dev_dbg(&udev->dev, "RESUME (wakeup)\n");
1877 /* TRSMRCY = 10 msec */ 1949 /* TRSMRCY = 10 msec */
1878 msleep(10); 1950 msleep(10);
1879 status = finish_device_resume(udev); 1951 status = finish_device_resume(udev);
1880 } 1952 }
1881 usb_unlock_device(udev); 1953 usb_unlock_device(udev);
1882 #endif 1954 #endif
1883 return status; 1955 return status;
1884 } 1956 }
1885 1957
1886 static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 1958 static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1887 { 1959 {
1888 struct usb_hub *hub = usb_get_intfdata (intf); 1960 struct usb_hub *hub = usb_get_intfdata (intf);
1889 struct usb_device *hdev = hub->hdev; 1961 struct usb_device *hdev = hub->hdev;
1890 unsigned port1; 1962 unsigned port1;
1891 1963
1892 /* fail if children aren't already suspended */ 1964 /* fail if children aren't already suspended */
1893 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 1965 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1894 struct usb_device *udev; 1966 struct usb_device *udev;
1895 1967
1896 udev = hdev->children [port1-1]; 1968 udev = hdev->children [port1-1];
1897 if (udev && (udev->dev.power.power_state.event 1969 if (udev && (udev->dev.power.power_state.event
1898 == PM_EVENT_ON 1970 == PM_EVENT_ON
1899 #ifdef CONFIG_USB_SUSPEND 1971 #ifdef CONFIG_USB_SUSPEND
1900 || udev->state != USB_STATE_SUSPENDED 1972 || udev->state != USB_STATE_SUSPENDED
1901 #endif 1973 #endif
1902 )) { 1974 )) {
1903 dev_dbg(&intf->dev, "port %d nyet suspended\n", port1); 1975 dev_dbg(&intf->dev, "port %d nyet suspended\n", port1);
1904 return -EBUSY; 1976 return -EBUSY;
1905 } 1977 }
1906 } 1978 }
1907 1979
1908 /* "global suspend" of the downstream HC-to-USB interface */ 1980 /* "global suspend" of the downstream HC-to-USB interface */
1909 if (!hdev->parent) { 1981 if (!hdev->parent) {
1910 struct usb_bus *bus = hdev->bus; 1982 struct usb_bus *bus = hdev->bus;
1911 if (bus) { 1983 if (bus) {
1912 int status = hcd_bus_suspend (bus); 1984 int status = hcd_bus_suspend (bus);
1913 1985
1914 if (status != 0) { 1986 if (status != 0) {
1915 dev_dbg(&hdev->dev, "'global' suspend %d\n", 1987 dev_dbg(&hdev->dev, "'global' suspend %d\n",
1916 status); 1988 status);
1917 return status; 1989 return status;
1918 } 1990 }
1919 } else 1991 } else
1920 return -EOPNOTSUPP; 1992 return -EOPNOTSUPP;
1921 } 1993 }
1922 1994
1923 /* stop khubd and related activity */ 1995 /* stop khubd and related activity */
1924 hub_quiesce(hub); 1996 hub_quiesce(hub);
1925 return 0; 1997 return 0;
1926 } 1998 }
1927 1999
1928 static int hub_resume(struct usb_interface *intf) 2000 static int hub_resume(struct usb_interface *intf)
1929 { 2001 {
1930 struct usb_device *hdev = interface_to_usbdev(intf); 2002 struct usb_device *hdev = interface_to_usbdev(intf);
1931 struct usb_hub *hub = usb_get_intfdata (intf); 2003 struct usb_hub *hub = usb_get_intfdata (intf);
1932 int status; 2004 int status;
1933 2005
1934 /* "global resume" of the downstream HC-to-USB interface */ 2006 /* "global resume" of the downstream HC-to-USB interface */
1935 if (!hdev->parent) { 2007 if (!hdev->parent) {
1936 struct usb_bus *bus = hdev->bus; 2008 struct usb_bus *bus = hdev->bus;
1937 if (bus) { 2009 if (bus) {
1938 status = hcd_bus_resume (bus); 2010 status = hcd_bus_resume (bus);
1939 if (status) { 2011 if (status) {
1940 dev_dbg(&intf->dev, "'global' resume %d\n", 2012 dev_dbg(&intf->dev, "'global' resume %d\n",
1941 status); 2013 status);
1942 return status; 2014 return status;
1943 } 2015 }
1944 } else 2016 } else
1945 return -EOPNOTSUPP; 2017 return -EOPNOTSUPP;
1946 if (status == 0) { 2018 if (status == 0) {
1947 /* TRSMRCY = 10 msec */ 2019 /* TRSMRCY = 10 msec */
1948 msleep(10); 2020 msleep(10);
1949 } 2021 }
1950 } 2022 }
1951 2023
1952 hub_activate(hub); 2024 hub_activate(hub);
1953 2025
1954 /* REVISIT: this recursion probably shouldn't exist. Remove 2026 /* REVISIT: this recursion probably shouldn't exist. Remove
1955 * this code sometime, after retesting with different root and 2027 * this code sometime, after retesting with different root and
1956 * external hubs. 2028 * external hubs.
1957 */ 2029 */
1958 #ifdef CONFIG_USB_SUSPEND 2030 #ifdef CONFIG_USB_SUSPEND
1959 { 2031 {
1960 unsigned port1; 2032 unsigned port1;
1961 2033
1962 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 2034 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1963 struct usb_device *udev; 2035 struct usb_device *udev;
1964 u16 portstat, portchange; 2036 u16 portstat, portchange;
1965 2037
1966 udev = hdev->children [port1-1]; 2038 udev = hdev->children [port1-1];
1967 status = hub_port_status(hub, port1, &portstat, &portchange); 2039 status = hub_port_status(hub, port1, &portstat, &portchange);
1968 if (status == 0) { 2040 if (status == 0) {
1969 if (portchange & USB_PORT_STAT_C_SUSPEND) { 2041 if (portchange & USB_PORT_STAT_C_SUSPEND) {
1970 clear_port_feature(hdev, port1, 2042 clear_port_feature(hdev, port1,
1971 USB_PORT_FEAT_C_SUSPEND); 2043 USB_PORT_FEAT_C_SUSPEND);
1972 portchange &= ~USB_PORT_STAT_C_SUSPEND; 2044 portchange &= ~USB_PORT_STAT_C_SUSPEND;
1973 } 2045 }
1974 2046
1975 /* let khubd handle disconnects etc */ 2047 /* let khubd handle disconnects etc */
1976 if (portchange) 2048 if (portchange)
1977 continue; 2049 continue;
1978 } 2050 }
1979 2051
1980 if (!udev || status < 0) 2052 if (!udev || status < 0)
1981 continue; 2053 continue;
1982 usb_lock_device(udev); 2054 usb_lock_device(udev);
1983 if (portstat & USB_PORT_STAT_SUSPEND) 2055 if (portstat & USB_PORT_STAT_SUSPEND)
1984 status = hub_port_resume(hub, port1, udev); 2056 status = hub_port_resume(hub, port1, udev);
1985 else { 2057 else {
1986 status = finish_device_resume(udev); 2058 status = finish_device_resume(udev);
1987 if (status < 0) { 2059 if (status < 0) {
1988 dev_dbg(&intf->dev, "resume port %d --> %d\n", 2060 dev_dbg(&intf->dev, "resume port %d --> %d\n",
1989 port1, status); 2061 port1, status);
1990 hub_port_logical_disconnect(hub, port1); 2062 hub_port_logical_disconnect(hub, port1);
1991 } 2063 }
1992 } 2064 }
1993 usb_unlock_device(udev); 2065 usb_unlock_device(udev);
1994 } 2066 }
1995 } 2067 }
1996 #endif 2068 #endif
1997 return 0; 2069 return 0;
1998 } 2070 }
1999 2071
2000 void usb_suspend_root_hub(struct usb_device *hdev) 2072 void usb_suspend_root_hub(struct usb_device *hdev)
2001 { 2073 {
2002 struct usb_hub *hub = hdev_to_hub(hdev); 2074 struct usb_hub *hub = hdev_to_hub(hdev);
2003 2075
2004 /* This also makes any led blinker stop retriggering. We're called 2076 /* This also makes any led blinker stop retriggering. We're called
2005 * from irq, so the blinker might still be scheduled. Caller promises 2077 * from irq, so the blinker might still be scheduled. Caller promises
2006 * that the root hub status URB will be canceled. 2078 * that the root hub status URB will be canceled.
2007 */ 2079 */
2008 __hub_quiesce(hub); 2080 __hub_quiesce(hub);
2009 mark_quiesced(to_usb_interface(hub->intfdev)); 2081 mark_quiesced(to_usb_interface(hub->intfdev));
2010 } 2082 }
2011 2083
2012 void usb_resume_root_hub(struct usb_device *hdev) 2084 void usb_resume_root_hub(struct usb_device *hdev)
2013 { 2085 {
2014 struct usb_hub *hub = hdev_to_hub(hdev); 2086 struct usb_hub *hub = hdev_to_hub(hdev);
2015 2087
2016 hub->resume_root_hub = 1; 2088 hub->resume_root_hub = 1;
2017 kick_khubd(hub); 2089 kick_khubd(hub);
2018 } 2090 }
2019 2091
2020 2092
2021 /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 2093 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2022 * 2094 *
2023 * Between connect detection and reset signaling there must be a delay 2095 * Between connect detection and reset signaling there must be a delay
2024 * of 100ms at least for debounce and power-settling. The corresponding 2096 * of 100ms at least for debounce and power-settling. The corresponding
2025 * timer shall restart whenever the downstream port detects a disconnect. 2097 * timer shall restart whenever the downstream port detects a disconnect.
2026 * 2098 *
2027 * Apparently there are some bluetooth and irda-dongles and a number of 2099 * Apparently there are some bluetooth and irda-dongles and a number of
2028 * low-speed devices for which this debounce period may last over a second. 2100 * low-speed devices for which this debounce period may last over a second.
2029 * Not covered by the spec - but easy to deal with. 2101 * Not covered by the spec - but easy to deal with.
2030 * 2102 *
2031 * This implementation uses a 1500ms total debounce timeout; if the 2103 * This implementation uses a 1500ms total debounce timeout; if the
2032 * connection isn't stable by then it returns -ETIMEDOUT. It checks 2104 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2033 * every 25ms for transient disconnects. When the port status has been 2105 * every 25ms for transient disconnects. When the port status has been
2034 * unchanged for 100ms it returns the port status. 2106 * unchanged for 100ms it returns the port status.
2035 */ 2107 */
2036 2108
2037 #define HUB_DEBOUNCE_TIMEOUT 1500 2109 #define HUB_DEBOUNCE_TIMEOUT 1500
2038 #define HUB_DEBOUNCE_STEP 25 2110 #define HUB_DEBOUNCE_STEP 25
2039 #define HUB_DEBOUNCE_STABLE 100 2111 #define HUB_DEBOUNCE_STABLE 100
2040 2112
2041 static int hub_port_debounce(struct usb_hub *hub, int port1) 2113 static int hub_port_debounce(struct usb_hub *hub, int port1)
2042 { 2114 {
2043 int ret; 2115 int ret;
2044 int total_time, stable_time = 0; 2116 int total_time, stable_time = 0;
2045 u16 portchange, portstatus; 2117 u16 portchange, portstatus;
2046 unsigned connection = 0xffff; 2118 unsigned connection = 0xffff;
2047 2119
2048 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 2120 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2049 ret = hub_port_status(hub, port1, &portstatus, &portchange); 2121 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2050 if (ret < 0) 2122 if (ret < 0)
2051 return ret; 2123 return ret;
2052 2124
2053 if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 2125 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2054 (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 2126 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2055 stable_time += HUB_DEBOUNCE_STEP; 2127 stable_time += HUB_DEBOUNCE_STEP;
2056 if (stable_time >= HUB_DEBOUNCE_STABLE) 2128 if (stable_time >= HUB_DEBOUNCE_STABLE)
2057 break; 2129 break;
2058 } else { 2130 } else {
2059 stable_time = 0; 2131 stable_time = 0;
2060 connection = portstatus & USB_PORT_STAT_CONNECTION; 2132 connection = portstatus & USB_PORT_STAT_CONNECTION;
2061 } 2133 }
2062 2134
2063 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2135 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2064 clear_port_feature(hub->hdev, port1, 2136 clear_port_feature(hub->hdev, port1,
2065 USB_PORT_FEAT_C_CONNECTION); 2137 USB_PORT_FEAT_C_CONNECTION);
2066 } 2138 }
2067 2139
2068 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 2140 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2069 break; 2141 break;
2070 msleep(HUB_DEBOUNCE_STEP); 2142 msleep(HUB_DEBOUNCE_STEP);
2071 } 2143 }
2072 2144
2073 dev_dbg (hub->intfdev, 2145 dev_dbg (hub->intfdev,
2074 "debounce: port %d: total %dms stable %dms status 0x%x\n", 2146 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2075 port1, total_time, stable_time, portstatus); 2147 port1, total_time, stable_time, portstatus);
2076 2148
2077 if (stable_time < HUB_DEBOUNCE_STABLE) 2149 if (stable_time < HUB_DEBOUNCE_STABLE)
2078 return -ETIMEDOUT; 2150 return -ETIMEDOUT;
2079 return portstatus; 2151 return portstatus;
2080 } 2152 }
2081 2153
2082 static void ep0_reinit(struct usb_device *udev) 2154 static void ep0_reinit(struct usb_device *udev)
2083 { 2155 {
2084 usb_disable_endpoint(udev, 0 + USB_DIR_IN); 2156 usb_disable_endpoint(udev, 0 + USB_DIR_IN);
2085 usb_disable_endpoint(udev, 0 + USB_DIR_OUT); 2157 usb_disable_endpoint(udev, 0 + USB_DIR_OUT);
2086 udev->ep_in[0] = udev->ep_out[0] = &udev->ep0; 2158 udev->ep_in[0] = udev->ep_out[0] = &udev->ep0;
2087 } 2159 }
2088 2160
2089 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 2161 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
2090 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 2162 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
2091 2163
2092 static int hub_set_address(struct usb_device *udev) 2164 static int hub_set_address(struct usb_device *udev)
2093 { 2165 {
2094 int retval; 2166 int retval;
2095 2167
2096 if (udev->devnum == 0) 2168 if (udev->devnum == 0)
2097 return -EINVAL; 2169 return -EINVAL;
2098 if (udev->state == USB_STATE_ADDRESS) 2170 if (udev->state == USB_STATE_ADDRESS)
2099 return 0; 2171 return 0;
2100 if (udev->state != USB_STATE_DEFAULT) 2172 if (udev->state != USB_STATE_DEFAULT)
2101 return -EINVAL; 2173 return -EINVAL;
2102 retval = usb_control_msg(udev, usb_sndaddr0pipe(), 2174 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
2103 USB_REQ_SET_ADDRESS, 0, udev->devnum, 0, 2175 USB_REQ_SET_ADDRESS, 0, udev->devnum, 0,
2104 NULL, 0, USB_CTRL_SET_TIMEOUT); 2176 NULL, 0, USB_CTRL_SET_TIMEOUT);
2105 if (retval == 0) { 2177 if (retval == 0) {
2106 usb_set_device_state(udev, USB_STATE_ADDRESS); 2178 usb_set_device_state(udev, USB_STATE_ADDRESS);
2107 ep0_reinit(udev); 2179 ep0_reinit(udev);
2108 } 2180 }
2109 return retval; 2181 return retval;
2110 } 2182 }
2111 2183
2112 /* Reset device, (re)assign address, get device descriptor. 2184 /* Reset device, (re)assign address, get device descriptor.
2113 * Device connection must be stable, no more debouncing needed. 2185 * Device connection must be stable, no more debouncing needed.
2114 * Returns device in USB_STATE_ADDRESS, except on error. 2186 * Returns device in USB_STATE_ADDRESS, except on error.
2115 * 2187 *
2116 * If this is called for an already-existing device (as part of 2188 * If this is called for an already-existing device (as part of
2117 * usb_reset_device), the caller must own the device lock. For a 2189 * usb_reset_device), the caller must own the device lock. For a
2118 * newly detected device that is not accessible through any global 2190 * newly detected device that is not accessible through any global
2119 * pointers, it's not necessary to lock the device. 2191 * pointers, it's not necessary to lock the device.
2120 */ 2192 */
2121 static int 2193 static int
2122 hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 2194 hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2123 int retry_counter) 2195 int retry_counter)
2124 { 2196 {
2125 static DECLARE_MUTEX(usb_address0_sem); 2197 static DECLARE_MUTEX(usb_address0_sem);
2126 2198
2127 struct usb_device *hdev = hub->hdev; 2199 struct usb_device *hdev = hub->hdev;
2128 int i, j, retval; 2200 int i, j, retval;
2129 unsigned delay = HUB_SHORT_RESET_TIME; 2201 unsigned delay = HUB_SHORT_RESET_TIME;
2130 enum usb_device_speed oldspeed = udev->speed; 2202 enum usb_device_speed oldspeed = udev->speed;
2131 2203
2132 /* root hub ports have a slightly longer reset period 2204 /* root hub ports have a slightly longer reset period
2133 * (from USB 2.0 spec, section 7.1.7.5) 2205 * (from USB 2.0 spec, section 7.1.7.5)
2134 */ 2206 */
2135 if (!hdev->parent) { 2207 if (!hdev->parent) {
2136 delay = HUB_ROOT_RESET_TIME; 2208 delay = HUB_ROOT_RESET_TIME;
2137 if (port1 == hdev->bus->otg_port) 2209 if (port1 == hdev->bus->otg_port)
2138 hdev->bus->b_hnp_enable = 0; 2210 hdev->bus->b_hnp_enable = 0;
2139 } 2211 }
2140 2212
2141 /* Some low speed devices have problems with the quick delay, so */ 2213 /* Some low speed devices have problems with the quick delay, so */
2142 /* be a bit pessimistic with those devices. RHbug #23670 */ 2214 /* be a bit pessimistic with those devices. RHbug #23670 */
2143 if (oldspeed == USB_SPEED_LOW) 2215 if (oldspeed == USB_SPEED_LOW)
2144 delay = HUB_LONG_RESET_TIME; 2216 delay = HUB_LONG_RESET_TIME;
2145 2217
2146 down(&usb_address0_sem); 2218 down(&usb_address0_sem);
2147 2219
2148 /* Reset the device; full speed may morph to high speed */ 2220 /* Reset the device; full speed may morph to high speed */
2149 retval = hub_port_reset(hub, port1, udev, delay); 2221 retval = hub_port_reset(hub, port1, udev, delay);
2150 if (retval < 0) /* error or disconnect */ 2222 if (retval < 0) /* error or disconnect */
2151 goto fail; 2223 goto fail;
2152 /* success, speed is known */ 2224 /* success, speed is known */
2153 retval = -ENODEV; 2225 retval = -ENODEV;
2154 2226
2155 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 2227 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
2156 dev_dbg(&udev->dev, "device reset changed speed!\n"); 2228 dev_dbg(&udev->dev, "device reset changed speed!\n");
2157 goto fail; 2229 goto fail;
2158 } 2230 }
2159 oldspeed = udev->speed; 2231 oldspeed = udev->speed;
2160 2232
2161 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 2233 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
2162 * it's fixed size except for full speed devices. 2234 * it's fixed size except for full speed devices.
2163 */ 2235 */
2164 switch (udev->speed) { 2236 switch (udev->speed) {
2165 case USB_SPEED_HIGH: /* fixed at 64 */ 2237 case USB_SPEED_HIGH: /* fixed at 64 */
2166 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 2238 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2167 break; 2239 break;
2168 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 2240 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
2169 /* to determine the ep0 maxpacket size, try to read 2241 /* to determine the ep0 maxpacket size, try to read
2170 * the device descriptor to get bMaxPacketSize0 and 2242 * the device descriptor to get bMaxPacketSize0 and
2171 * then correct our initial guess. 2243 * then correct our initial guess.
2172 */ 2244 */
2173 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 2245 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2174 break; 2246 break;
2175 case USB_SPEED_LOW: /* fixed at 8 */ 2247 case USB_SPEED_LOW: /* fixed at 8 */
2176 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8); 2248 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8);
2177 break; 2249 break;
2178 default: 2250 default:
2179 goto fail; 2251 goto fail;
2180 } 2252 }
2181 2253
2182 dev_info (&udev->dev, 2254 dev_info (&udev->dev,
2183 "%s %s speed USB device using %s and address %d\n", 2255 "%s %s speed USB device using %s and address %d\n",
2184 (udev->config) ? "reset" : "new", 2256 (udev->config) ? "reset" : "new",
2185 ({ char *speed; switch (udev->speed) { 2257 ({ char *speed; switch (udev->speed) {
2186 case USB_SPEED_LOW: speed = "low"; break; 2258 case USB_SPEED_LOW: speed = "low"; break;
2187 case USB_SPEED_FULL: speed = "full"; break; 2259 case USB_SPEED_FULL: speed = "full"; break;
2188 case USB_SPEED_HIGH: speed = "high"; break; 2260 case USB_SPEED_HIGH: speed = "high"; break;
2189 default: speed = "?"; break; 2261 default: speed = "?"; break;
2190 }; speed;}), 2262 }; speed;}),
2191 udev->bus->controller->driver->name, 2263 udev->bus->controller->driver->name,
2192 udev->devnum); 2264 udev->devnum);
2193 2265
2194 /* Set up TT records, if needed */ 2266 /* Set up TT records, if needed */
2195 if (hdev->tt) { 2267 if (hdev->tt) {
2196 udev->tt = hdev->tt; 2268 udev->tt = hdev->tt;
2197 udev->ttport = hdev->ttport; 2269 udev->ttport = hdev->ttport;
2198 } else if (udev->speed != USB_SPEED_HIGH 2270 } else if (udev->speed != USB_SPEED_HIGH
2199 && hdev->speed == USB_SPEED_HIGH) { 2271 && hdev->speed == USB_SPEED_HIGH) {
2200 udev->tt = &hub->tt; 2272 udev->tt = &hub->tt;
2201 udev->ttport = port1; 2273 udev->ttport = port1;
2202 } 2274 }
2203 2275
2204 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 2276 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
2205 * Because device hardware and firmware is sometimes buggy in 2277 * Because device hardware and firmware is sometimes buggy in
2206 * this area, and this is how Linux has done it for ages. 2278 * this area, and this is how Linux has done it for ages.
2207 * Change it cautiously. 2279 * Change it cautiously.
2208 * 2280 *
2209 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 2281 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
2210 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 2282 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
2211 * so it may help with some non-standards-compliant devices. 2283 * so it may help with some non-standards-compliant devices.
2212 * Otherwise we start with SET_ADDRESS and then try to read the 2284 * Otherwise we start with SET_ADDRESS and then try to read the
2213 * first 8 bytes of the device descriptor to get the ep0 maxpacket 2285 * first 8 bytes of the device descriptor to get the ep0 maxpacket
2214 * value. 2286 * value.
2215 */ 2287 */
2216 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 2288 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2217 if (USE_NEW_SCHEME(retry_counter)) { 2289 if (USE_NEW_SCHEME(retry_counter)) {
2218 struct usb_device_descriptor *buf; 2290 struct usb_device_descriptor *buf;
2219 int r = 0; 2291 int r = 0;
2220 2292
2221 #define GET_DESCRIPTOR_BUFSIZE 64 2293 #define GET_DESCRIPTOR_BUFSIZE 64
2222 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 2294 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
2223 if (!buf) { 2295 if (!buf) {
2224 retval = -ENOMEM; 2296 retval = -ENOMEM;
2225 continue; 2297 continue;
2226 } 2298 }
2227 2299
2228 /* Use a short timeout the first time through, 2300 /* Use a short timeout the first time through,
2229 * so that recalcitrant full-speed devices with 2301 * so that recalcitrant full-speed devices with
2230 * 8- or 16-byte ep0-maxpackets won't slow things 2302 * 8- or 16-byte ep0-maxpackets won't slow things
2231 * down tremendously by NAKing the unexpectedly 2303 * down tremendously by NAKing the unexpectedly
2232 * early status stage. Also, retry on all errors; 2304 * early status stage. Also, retry on all errors;
2233 * some devices are flakey. 2305 * some devices are flakey.
2234 */ 2306 */
2235 for (j = 0; j < 3; ++j) { 2307 for (j = 0; j < 3; ++j) {
2236 buf->bMaxPacketSize0 = 0; 2308 buf->bMaxPacketSize0 = 0;
2237 r = usb_control_msg(udev, usb_rcvaddr0pipe(), 2309 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
2238 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 2310 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
2239 USB_DT_DEVICE << 8, 0, 2311 USB_DT_DEVICE << 8, 0,
2240 buf, GET_DESCRIPTOR_BUFSIZE, 2312 buf, GET_DESCRIPTOR_BUFSIZE,
2241 (i ? USB_CTRL_GET_TIMEOUT : 1000)); 2313 (i ? USB_CTRL_GET_TIMEOUT : 1000));
2242 switch (buf->bMaxPacketSize0) { 2314 switch (buf->bMaxPacketSize0) {
2243 case 8: case 16: case 32: case 64: 2315 case 8: case 16: case 32: case 64:
2244 if (buf->bDescriptorType == 2316 if (buf->bDescriptorType ==
2245 USB_DT_DEVICE) { 2317 USB_DT_DEVICE) {
2246 r = 0; 2318 r = 0;
2247 break; 2319 break;
2248 } 2320 }
2249 /* FALL THROUGH */ 2321 /* FALL THROUGH */
2250 default: 2322 default:
2251 if (r == 0) 2323 if (r == 0)
2252 r = -EPROTO; 2324 r = -EPROTO;
2253 break; 2325 break;
2254 } 2326 }
2255 if (r == 0) 2327 if (r == 0)
2256 break; 2328 break;
2257 } 2329 }
2258 udev->descriptor.bMaxPacketSize0 = 2330 udev->descriptor.bMaxPacketSize0 =
2259 buf->bMaxPacketSize0; 2331 buf->bMaxPacketSize0;
2260 kfree(buf); 2332 kfree(buf);
2261 2333
2262 retval = hub_port_reset(hub, port1, udev, delay); 2334 retval = hub_port_reset(hub, port1, udev, delay);
2263 if (retval < 0) /* error or disconnect */ 2335 if (retval < 0) /* error or disconnect */
2264 goto fail; 2336 goto fail;
2265 if (oldspeed != udev->speed) { 2337 if (oldspeed != udev->speed) {
2266 dev_dbg(&udev->dev, 2338 dev_dbg(&udev->dev,
2267 "device reset changed speed!\n"); 2339 "device reset changed speed!\n");
2268 retval = -ENODEV; 2340 retval = -ENODEV;
2269 goto fail; 2341 goto fail;
2270 } 2342 }
2271 if (r) { 2343 if (r) {
2272 dev_err(&udev->dev, "device descriptor " 2344 dev_err(&udev->dev, "device descriptor "
2273 "read/%s, error %d\n", 2345 "read/%s, error %d\n",
2274 "64", r); 2346 "64", r);
2275 retval = -EMSGSIZE; 2347 retval = -EMSGSIZE;
2276 continue; 2348 continue;
2277 } 2349 }
2278 #undef GET_DESCRIPTOR_BUFSIZE 2350 #undef GET_DESCRIPTOR_BUFSIZE
2279 } 2351 }
2280 2352
2281 for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 2353 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
2282 retval = hub_set_address(udev); 2354 retval = hub_set_address(udev);
2283 if (retval >= 0) 2355 if (retval >= 0)
2284 break; 2356 break;
2285 msleep(200); 2357 msleep(200);
2286 } 2358 }
2287 if (retval < 0) { 2359 if (retval < 0) {
2288 dev_err(&udev->dev, 2360 dev_err(&udev->dev,
2289 "device not accepting address %d, error %d\n", 2361 "device not accepting address %d, error %d\n",
2290 udev->devnum, retval); 2362 udev->devnum, retval);
2291 goto fail; 2363 goto fail;
2292 } 2364 }
2293 2365
2294 /* cope with hardware quirkiness: 2366 /* cope with hardware quirkiness:
2295 * - let SET_ADDRESS settle, some device hardware wants it 2367 * - let SET_ADDRESS settle, some device hardware wants it
2296 * - read ep0 maxpacket even for high and low speed, 2368 * - read ep0 maxpacket even for high and low speed,
2297 */ 2369 */
2298 msleep(10); 2370 msleep(10);
2299 if (USE_NEW_SCHEME(retry_counter)) 2371 if (USE_NEW_SCHEME(retry_counter))
2300 break; 2372 break;
2301 2373
2302 retval = usb_get_device_descriptor(udev, 8); 2374 retval = usb_get_device_descriptor(udev, 8);
2303 if (retval < 8) { 2375 if (retval < 8) {
2304 dev_err(&udev->dev, "device descriptor " 2376 dev_err(&udev->dev, "device descriptor "
2305 "read/%s, error %d\n", 2377 "read/%s, error %d\n",
2306 "8", retval); 2378 "8", retval);
2307 if (retval >= 0) 2379 if (retval >= 0)
2308 retval = -EMSGSIZE; 2380 retval = -EMSGSIZE;
2309 } else { 2381 } else {
2310 retval = 0; 2382 retval = 0;
2311 break; 2383 break;
2312 } 2384 }
2313 } 2385 }
2314 if (retval) 2386 if (retval)
2315 goto fail; 2387 goto fail;
2316 2388
2317 i = udev->descriptor.bMaxPacketSize0; 2389 i = udev->descriptor.bMaxPacketSize0;
2318 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) { 2390 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
2319 if (udev->speed != USB_SPEED_FULL || 2391 if (udev->speed != USB_SPEED_FULL ||
2320 !(i == 8 || i == 16 || i == 32 || i == 64)) { 2392 !(i == 8 || i == 16 || i == 32 || i == 64)) {
2321 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i); 2393 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
2322 retval = -EMSGSIZE; 2394 retval = -EMSGSIZE;
2323 goto fail; 2395 goto fail;
2324 } 2396 }
2325 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 2397 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
2326 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 2398 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2327 ep0_reinit(udev); 2399 ep0_reinit(udev);
2328 } 2400 }
2329 2401
2330 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 2402 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
2331 if (retval < (signed)sizeof(udev->descriptor)) { 2403 if (retval < (signed)sizeof(udev->descriptor)) {
2332 dev_err(&udev->dev, "device descriptor read/%s, error %d\n", 2404 dev_err(&udev->dev, "device descriptor read/%s, error %d\n",
2333 "all", retval); 2405 "all", retval);
2334 if (retval >= 0) 2406 if (retval >= 0)
2335 retval = -ENOMSG; 2407 retval = -ENOMSG;
2336 goto fail; 2408 goto fail;
2337 } 2409 }
2338 2410
2339 retval = 0; 2411 retval = 0;
2340 2412
2341 fail: 2413 fail:
2342 if (retval) 2414 if (retval)
2343 hub_port_disable(hub, port1, 0); 2415 hub_port_disable(hub, port1, 0);
2344 up(&usb_address0_sem); 2416 up(&usb_address0_sem);
2345 return retval; 2417 return retval;
2346 } 2418 }
2347 2419
2348 static void 2420 static void
2349 check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) 2421 check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
2350 { 2422 {
2351 struct usb_qualifier_descriptor *qual; 2423 struct usb_qualifier_descriptor *qual;
2352 int status; 2424 int status;
2353 2425
2354 qual = kmalloc (sizeof *qual, SLAB_KERNEL); 2426 qual = kmalloc (sizeof *qual, SLAB_KERNEL);
2355 if (qual == NULL) 2427 if (qual == NULL)
2356 return; 2428 return;
2357 2429
2358 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, 2430 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
2359 qual, sizeof *qual); 2431 qual, sizeof *qual);
2360 if (status == sizeof *qual) { 2432 if (status == sizeof *qual) {
2361 dev_info(&udev->dev, "not running at top speed; " 2433 dev_info(&udev->dev, "not running at top speed; "
2362 "connect to a high speed hub\n"); 2434 "connect to a high speed hub\n");
2363 /* hub LEDs are probably harder to miss than syslog */ 2435 /* hub LEDs are probably harder to miss than syslog */
2364 if (hub->has_indicators) { 2436 if (hub->has_indicators) {
2365 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 2437 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2366 schedule_work (&hub->leds); 2438 schedule_work (&hub->leds);
2367 } 2439 }
2368 } 2440 }
2369 kfree(qual); 2441 kfree(qual);
2370 } 2442 }
2371 2443
2372 static unsigned 2444 static unsigned
2373 hub_power_remaining (struct usb_hub *hub) 2445 hub_power_remaining (struct usb_hub *hub)
2374 { 2446 {
2375 struct usb_device *hdev = hub->hdev; 2447 struct usb_device *hdev = hub->hdev;
2376 int remaining; 2448 int remaining;
2377 unsigned i; 2449 int port1;
2378 2450
2379 remaining = hub->power_budget; 2451 if (!hub->limited_power)
2380 if (!remaining) /* self-powered */
2381 return 0; 2452 return 0;
2382 2453
2383 for (i = 0; i < hdev->maxchild; i++) { 2454 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
2384 struct usb_device *udev = hdev->children[i]; 2455 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
2385 int delta, ceiling; 2456 struct usb_device *udev = hdev->children[port1 - 1];
2457 int delta;
2386 2458
2387 if (!udev) 2459 if (!udev)
2388 continue; 2460 continue;
2389 2461
2390 /* 100mA per-port ceiling, or 8mA for OTG ports */ 2462 /* Unconfigured devices may not use more than 100mA,
2391 if (i != (udev->bus->otg_port - 1) || hdev->parent) 2463 * or 8mA for OTG ports */
2392 ceiling = 50;
2393 else
2394 ceiling = 4;
2395
2396 if (udev->actconfig) 2464 if (udev->actconfig)
2397 delta = udev->actconfig->desc.bMaxPower; 2465 delta = udev->actconfig->desc.bMaxPower * 2;
2466 else if (port1 != udev->bus->otg_port || hdev->parent)
2467 delta = 100;
2398 else 2468 else
2399 delta = ceiling; 2469 delta = 8;
2400 // dev_dbg(&udev->dev, "budgeted %dmA\n", 2 * delta); 2470 if (delta > hub->mA_per_port)
2401 if (delta > ceiling) 2471 dev_warn(&udev->dev, "%dmA is over %umA budget "
2402 dev_warn(&udev->dev, "%dmA over %dmA budget!\n", 2472 "for port %d!\n",
2403 2 * (delta - ceiling), 2 * ceiling); 2473 delta, hub->mA_per_port, port1);
2404 remaining -= delta; 2474 remaining -= delta;
2405 } 2475 }
2406 if (remaining < 0) { 2476 if (remaining < 0) {
2407 dev_warn(hub->intfdev, 2477 dev_warn(hub->intfdev, "%dmA over power budget!\n",
2408 "%dmA over power budget!\n", 2478 - remaining);
2409 -2 * remaining);
2410 remaining = 0; 2479 remaining = 0;
2411 } 2480 }
2412 return remaining; 2481 return remaining;
2413 } 2482 }
2414 2483
2415 /* Handle physical or logical connection change events. 2484 /* Handle physical or logical connection change events.
2416 * This routine is called when: 2485 * This routine is called when:
2417 * a port connection-change occurs; 2486 * a port connection-change occurs;
2418 * a port enable-change occurs (often caused by EMI); 2487 * a port enable-change occurs (often caused by EMI);
2419 * usb_reset_device() encounters changed descriptors (as from 2488 * usb_reset_device() encounters changed descriptors (as from
2420 * a firmware download) 2489 * a firmware download)
2421 * caller already locked the hub 2490 * caller already locked the hub
2422 */ 2491 */
2423 static void hub_port_connect_change(struct usb_hub *hub, int port1, 2492 static void hub_port_connect_change(struct usb_hub *hub, int port1,
2424 u16 portstatus, u16 portchange) 2493 u16 portstatus, u16 portchange)
2425 { 2494 {
2426 struct usb_device *hdev = hub->hdev; 2495 struct usb_device *hdev = hub->hdev;
2427 struct device *hub_dev = hub->intfdev; 2496 struct device *hub_dev = hub->intfdev;
2428 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2497 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2429 int status, i; 2498 int status, i;
2430 2499
2431 dev_dbg (hub_dev, 2500 dev_dbg (hub_dev,
2432 "port %d, status %04x, change %04x, %s\n", 2501 "port %d, status %04x, change %04x, %s\n",
2433 port1, portstatus, portchange, portspeed (portstatus)); 2502 port1, portstatus, portchange, portspeed (portstatus));
2434 2503
2435 if (hub->has_indicators) { 2504 if (hub->has_indicators) {
2436 set_port_led(hub, port1, HUB_LED_AUTO); 2505 set_port_led(hub, port1, HUB_LED_AUTO);
2437 hub->indicator[port1-1] = INDICATOR_AUTO; 2506 hub->indicator[port1-1] = INDICATOR_AUTO;
2438 } 2507 }
2439 2508
2440 /* Disconnect any existing devices under this port */ 2509 /* Disconnect any existing devices under this port */
2441 if (hdev->children[port1-1]) 2510 if (hdev->children[port1-1])
2442 usb_disconnect(&hdev->children[port1-1]); 2511 usb_disconnect(&hdev->children[port1-1]);
2443 clear_bit(port1, hub->change_bits); 2512 clear_bit(port1, hub->change_bits);
2444 2513
2445 #ifdef CONFIG_USB_OTG 2514 #ifdef CONFIG_USB_OTG
2446 /* during HNP, don't repeat the debounce */ 2515 /* during HNP, don't repeat the debounce */
2447 if (hdev->bus->is_b_host) 2516 if (hdev->bus->is_b_host)
2448 portchange &= ~USB_PORT_STAT_C_CONNECTION; 2517 portchange &= ~USB_PORT_STAT_C_CONNECTION;
2449 #endif 2518 #endif
2450 2519
2451 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2520 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2452 status = hub_port_debounce(hub, port1); 2521 status = hub_port_debounce(hub, port1);
2453 if (status < 0) { 2522 if (status < 0) {
2454 dev_err (hub_dev, 2523 dev_err (hub_dev,
2455 "connect-debounce failed, port %d disabled\n", 2524 "connect-debounce failed, port %d disabled\n",
2456 port1); 2525 port1);
2457 goto done; 2526 goto done;
2458 } 2527 }
2459 portstatus = status; 2528 portstatus = status;
2460 } 2529 }
2461 2530
2462 /* Return now if nothing is connected */ 2531 /* Return now if nothing is connected */
2463 if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 2532 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2464 2533
2465 /* maybe switch power back on (e.g. root hub was reset) */ 2534 /* maybe switch power back on (e.g. root hub was reset) */
2466 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 2535 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
2467 && !(portstatus & (1 << USB_PORT_FEAT_POWER))) 2536 && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
2468 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 2537 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
2469 2538
2470 if (portstatus & USB_PORT_STAT_ENABLE) 2539 if (portstatus & USB_PORT_STAT_ENABLE)
2471 goto done; 2540 goto done;
2472 return; 2541 return;
2473 } 2542 }
2474 2543
2475 #ifdef CONFIG_USB_SUSPEND 2544 #ifdef CONFIG_USB_SUSPEND
2476 /* If something is connected, but the port is suspended, wake it up. */ 2545 /* If something is connected, but the port is suspended, wake it up. */
2477 if (portstatus & USB_PORT_STAT_SUSPEND) { 2546 if (portstatus & USB_PORT_STAT_SUSPEND) {
2478 status = hub_port_resume(hub, port1, NULL); 2547 status = hub_port_resume(hub, port1, NULL);
2479 if (status < 0) { 2548 if (status < 0) {
2480 dev_dbg(hub_dev, 2549 dev_dbg(hub_dev,
2481 "can't clear suspend on port %d; %d\n", 2550 "can't clear suspend on port %d; %d\n",
2482 port1, status); 2551 port1, status);
2483 goto done; 2552 goto done;
2484 } 2553 }
2485 } 2554 }
2486 #endif 2555 #endif
2487 2556
2488 for (i = 0; i < SET_CONFIG_TRIES; i++) { 2557 for (i = 0; i < SET_CONFIG_TRIES; i++) {
2489 struct usb_device *udev; 2558 struct usb_device *udev;
2490 2559
2491 /* reallocate for each attempt, since references 2560 /* reallocate for each attempt, since references
2492 * to the previous one can escape in various ways 2561 * to the previous one can escape in various ways
2493 */ 2562 */
2494 udev = usb_alloc_dev(hdev, hdev->bus, port1); 2563 udev = usb_alloc_dev(hdev, hdev->bus, port1);
2495 if (!udev) { 2564 if (!udev) {
2496 dev_err (hub_dev, 2565 dev_err (hub_dev,
2497 "couldn't allocate port %d usb_device\n", 2566 "couldn't allocate port %d usb_device\n",
2498 port1); 2567 port1);
2499 goto done; 2568 goto done;
2500 } 2569 }
2501 2570
2502 usb_set_device_state(udev, USB_STATE_POWERED); 2571 usb_set_device_state(udev, USB_STATE_POWERED);
2503 udev->speed = USB_SPEED_UNKNOWN; 2572 udev->speed = USB_SPEED_UNKNOWN;
2504 2573 udev->bus_mA = hub->mA_per_port;
2574
2505 /* set the address */ 2575 /* set the address */
2506 choose_address(udev); 2576 choose_address(udev);
2507 if (udev->devnum <= 0) { 2577 if (udev->devnum <= 0) {
2508 status = -ENOTCONN; /* Don't retry */ 2578 status = -ENOTCONN; /* Don't retry */
2509 goto loop; 2579 goto loop;
2510 } 2580 }
2511 2581
2512 /* reset and get descriptor */ 2582 /* reset and get descriptor */
2513 status = hub_port_init(hub, udev, port1, i); 2583 status = hub_port_init(hub, udev, port1, i);
2514 if (status < 0) 2584 if (status < 0)
2515 goto loop; 2585 goto loop;
2516 2586
2517 /* consecutive bus-powered hubs aren't reliable; they can 2587 /* consecutive bus-powered hubs aren't reliable; they can
2518 * violate the voltage drop budget. if the new child has 2588 * violate the voltage drop budget. if the new child has
2519 * a "powered" LED, users should notice we didn't enable it 2589 * a "powered" LED, users should notice we didn't enable it
2520 * (without reading syslog), even without per-port LEDs 2590 * (without reading syslog), even without per-port LEDs
2521 * on the parent. 2591 * on the parent.
2522 */ 2592 */
2523 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 2593 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
2524 && hub->power_budget) { 2594 && udev->bus_mA <= 100) {
2525 u16 devstat; 2595 u16 devstat;
2526 2596
2527 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 2597 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
2528 &devstat); 2598 &devstat);
2529 if (status < 0) { 2599 if (status < 2) {
2530 dev_dbg(&udev->dev, "get status %d ?\n", status); 2600 dev_dbg(&udev->dev, "get status %d ?\n", status);
2531 goto loop_disable; 2601 goto loop_disable;
2532 } 2602 }
2533 cpu_to_le16s(&devstat); 2603 le16_to_cpus(&devstat);
2534 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 2604 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
2535 dev_err(&udev->dev, 2605 dev_err(&udev->dev,
2536 "can't connect bus-powered hub " 2606 "can't connect bus-powered hub "
2537 "to this port\n"); 2607 "to this port\n");
2538 if (hub->has_indicators) { 2608 if (hub->has_indicators) {
2539 hub->indicator[port1-1] = 2609 hub->indicator[port1-1] =
2540 INDICATOR_AMBER_BLINK; 2610 INDICATOR_AMBER_BLINK;
2541 schedule_work (&hub->leds); 2611 schedule_work (&hub->leds);
2542 } 2612 }
2543 status = -ENOTCONN; /* Don't retry */ 2613 status = -ENOTCONN; /* Don't retry */
2544 goto loop_disable; 2614 goto loop_disable;
2545 } 2615 }
2546 } 2616 }
2547 2617
2548 /* check for devices running slower than they could */ 2618 /* check for devices running slower than they could */
2549 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 2619 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
2550 && udev->speed == USB_SPEED_FULL 2620 && udev->speed == USB_SPEED_FULL
2551 && highspeed_hubs != 0) 2621 && highspeed_hubs != 0)
2552 check_highspeed (hub, udev, port1); 2622 check_highspeed (hub, udev, port1);
2553 2623
2554 /* Store the parent's children[] pointer. At this point 2624 /* Store the parent's children[] pointer. At this point
2555 * udev becomes globally accessible, although presumably 2625 * udev becomes globally accessible, although presumably
2556 * no one will look at it until hdev is unlocked. 2626 * no one will look at it until hdev is unlocked.
2557 */ 2627 */
2558 status = 0; 2628 status = 0;
2559 2629
2560 /* We mustn't add new devices if the parent hub has 2630 /* We mustn't add new devices if the parent hub has
2561 * been disconnected; we would race with the 2631 * been disconnected; we would race with the
2562 * recursively_mark_NOTATTACHED() routine. 2632 * recursively_mark_NOTATTACHED() routine.
2563 */ 2633 */
2564 spin_lock_irq(&device_state_lock); 2634 spin_lock_irq(&device_state_lock);
2565 if (hdev->state == USB_STATE_NOTATTACHED) 2635 if (hdev->state == USB_STATE_NOTATTACHED)
2566 status = -ENOTCONN; 2636 status = -ENOTCONN;
2567 else 2637 else
2568 hdev->children[port1-1] = udev; 2638 hdev->children[port1-1] = udev;
2569 spin_unlock_irq(&device_state_lock); 2639 spin_unlock_irq(&device_state_lock);
2570 2640
2571 /* Run it through the hoops (find a driver, etc) */ 2641 /* Run it through the hoops (find a driver, etc) */
2572 if (!status) { 2642 if (!status) {
2573 status = usb_new_device(udev); 2643 status = usb_new_device(udev);
2574 if (status) { 2644 if (status) {
2575 spin_lock_irq(&device_state_lock); 2645 spin_lock_irq(&device_state_lock);
2576 hdev->children[port1-1] = NULL; 2646 hdev->children[port1-1] = NULL;
2577 spin_unlock_irq(&device_state_lock); 2647 spin_unlock_irq(&device_state_lock);
2578 } 2648 }
2579 } 2649 }
2580 2650
2581 if (status) 2651 if (status)
2582 goto loop_disable; 2652 goto loop_disable;
2583 2653
2584 status = hub_power_remaining(hub); 2654 status = hub_power_remaining(hub);
2585 if (status) 2655 if (status)
2586 dev_dbg(hub_dev, 2656 dev_dbg(hub_dev, "%dmA power budget left\n", status);
2587 "%dmA power budget left\n",
2588 2 * status);
2589 2657
2590 return; 2658 return;
2591 2659
2592 loop_disable: 2660 loop_disable:
2593 hub_port_disable(hub, port1, 1); 2661 hub_port_disable(hub, port1, 1);
2594 loop: 2662 loop:
2595 ep0_reinit(udev); 2663 ep0_reinit(udev);
2596 release_address(udev); 2664 release_address(udev);
2597 usb_put_dev(udev); 2665 usb_put_dev(udev);
2598 if (status == -ENOTCONN) 2666 if (status == -ENOTCONN)
2599 break; 2667 break;
2600 } 2668 }
2601 2669
2602 done: 2670 done:
2603 hub_port_disable(hub, port1, 1); 2671 hub_port_disable(hub, port1, 1);
2604 } 2672 }
2605 2673
2606 static void hub_events(void) 2674 static void hub_events(void)
2607 { 2675 {
2608 struct list_head *tmp; 2676 struct list_head *tmp;
2609 struct usb_device *hdev; 2677 struct usb_device *hdev;
2610 struct usb_interface *intf; 2678 struct usb_interface *intf;
2611 struct usb_hub *hub; 2679 struct usb_hub *hub;
2612 struct device *hub_dev; 2680 struct device *hub_dev;
2613 u16 hubstatus; 2681 u16 hubstatus;
2614 u16 hubchange; 2682 u16 hubchange;
2615 u16 portstatus; 2683 u16 portstatus;
2616 u16 portchange; 2684 u16 portchange;
2617 int i, ret; 2685 int i, ret;
2618 int connect_change; 2686 int connect_change;
2619 2687
2620 /* 2688 /*
2621 * We restart the list every time to avoid a deadlock with 2689 * We restart the list every time to avoid a deadlock with
2622 * deleting hubs downstream from this one. This should be 2690 * deleting hubs downstream from this one. This should be
2623 * safe since we delete the hub from the event list. 2691 * safe since we delete the hub from the event list.
2624 * Not the most efficient, but avoids deadlocks. 2692 * Not the most efficient, but avoids deadlocks.
2625 */ 2693 */
2626 while (1) { 2694 while (1) {
2627 2695
2628 /* Grab the first entry at the beginning of the list */ 2696 /* Grab the first entry at the beginning of the list */
2629 spin_lock_irq(&hub_event_lock); 2697 spin_lock_irq(&hub_event_lock);
2630 if (list_empty(&hub_event_list)) { 2698 if (list_empty(&hub_event_list)) {
2631 spin_unlock_irq(&hub_event_lock); 2699 spin_unlock_irq(&hub_event_lock);
2632 break; 2700 break;
2633 } 2701 }
2634 2702
2635 tmp = hub_event_list.next; 2703 tmp = hub_event_list.next;
2636 list_del_init(tmp); 2704 list_del_init(tmp);
2637 2705
2638 hub = list_entry(tmp, struct usb_hub, event_list); 2706 hub = list_entry(tmp, struct usb_hub, event_list);
2639 hdev = hub->hdev; 2707 hdev = hub->hdev;
2640 intf = to_usb_interface(hub->intfdev); 2708 intf = to_usb_interface(hub->intfdev);
2641 hub_dev = &intf->dev; 2709 hub_dev = &intf->dev;
2642 2710
2643 i = hub->resume_root_hub; 2711 i = hub->resume_root_hub;
2644 2712
2645 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x%s\n", 2713 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x%s\n",
2646 hdev->state, hub->descriptor 2714 hdev->state, hub->descriptor
2647 ? hub->descriptor->bNbrPorts 2715 ? hub->descriptor->bNbrPorts
2648 : 0, 2716 : 0,
2649 /* NOTE: expects max 15 ports... */ 2717 /* NOTE: expects max 15 ports... */
2650 (u16) hub->change_bits[0], 2718 (u16) hub->change_bits[0],
2651 (u16) hub->event_bits[0], 2719 (u16) hub->event_bits[0],
2652 i ? ", resume root" : ""); 2720 i ? ", resume root" : "");
2653 2721
2654 usb_get_intf(intf); 2722 usb_get_intf(intf);
2655 spin_unlock_irq(&hub_event_lock); 2723 spin_unlock_irq(&hub_event_lock);
2656 2724
2657 /* Is this is a root hub wanting to reactivate the downstream 2725 /* Is this is a root hub wanting to reactivate the downstream
2658 * ports? If so, be sure the interface resumes even if its 2726 * ports? If so, be sure the interface resumes even if its
2659 * stub "device" node was never suspended. 2727 * stub "device" node was never suspended.
2660 */ 2728 */
2661 if (i) { 2729 if (i) {
2662 dpm_runtime_resume(&hdev->dev); 2730 dpm_runtime_resume(&hdev->dev);
2663 dpm_runtime_resume(&intf->dev); 2731 dpm_runtime_resume(&intf->dev);
2664 } 2732 }
2665 2733
2666 /* Lock the device, then check to see if we were 2734 /* Lock the device, then check to see if we were
2667 * disconnected while waiting for the lock to succeed. */ 2735 * disconnected while waiting for the lock to succeed. */
2668 if (locktree(hdev) < 0) { 2736 if (locktree(hdev) < 0) {
2669 usb_put_intf(intf); 2737 usb_put_intf(intf);
2670 continue; 2738 continue;
2671 } 2739 }
2672 if (hub != usb_get_intfdata(intf)) 2740 if (hub != usb_get_intfdata(intf))
2673 goto loop; 2741 goto loop;
2674 2742
2675 /* If the hub has died, clean up after it */ 2743 /* If the hub has died, clean up after it */
2676 if (hdev->state == USB_STATE_NOTATTACHED) { 2744 if (hdev->state == USB_STATE_NOTATTACHED) {
2677 hub_pre_reset(hub, 0); 2745 hub_pre_reset(hub, 0);
2678 goto loop; 2746 goto loop;
2679 } 2747 }
2680 2748
2681 /* If this is an inactive or suspended hub, do nothing */ 2749 /* If this is an inactive or suspended hub, do nothing */
2682 if (hub->quiescing) 2750 if (hub->quiescing)
2683 goto loop; 2751 goto loop;
2684 2752
2685 if (hub->error) { 2753 if (hub->error) {
2686 dev_dbg (hub_dev, "resetting for error %d\n", 2754 dev_dbg (hub_dev, "resetting for error %d\n",
2687 hub->error); 2755 hub->error);
2688 2756
2689 ret = usb_reset_device(hdev); 2757 ret = usb_reset_device(hdev);
2690 if (ret) { 2758 if (ret) {
2691 dev_dbg (hub_dev, 2759 dev_dbg (hub_dev,
2692 "error resetting hub: %d\n", ret); 2760 "error resetting hub: %d\n", ret);
2693 goto loop; 2761 goto loop;
2694 } 2762 }
2695 2763
2696 hub->nerrors = 0; 2764 hub->nerrors = 0;
2697 hub->error = 0; 2765 hub->error = 0;
2698 } 2766 }
2699 2767
2700 /* deal with port status changes */ 2768 /* deal with port status changes */
2701 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { 2769 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
2702 if (test_bit(i, hub->busy_bits)) 2770 if (test_bit(i, hub->busy_bits))
2703 continue; 2771 continue;
2704 connect_change = test_bit(i, hub->change_bits); 2772 connect_change = test_bit(i, hub->change_bits);
2705 if (!test_and_clear_bit(i, hub->event_bits) && 2773 if (!test_and_clear_bit(i, hub->event_bits) &&
2706 !connect_change && !hub->activating) 2774 !connect_change && !hub->activating)
2707 continue; 2775 continue;
2708 2776
2709 ret = hub_port_status(hub, i, 2777 ret = hub_port_status(hub, i,
2710 &portstatus, &portchange); 2778 &portstatus, &portchange);
2711 if (ret < 0) 2779 if (ret < 0)
2712 continue; 2780 continue;
2713 2781
2714 if (hub->activating && !hdev->children[i-1] && 2782 if (hub->activating && !hdev->children[i-1] &&
2715 (portstatus & 2783 (portstatus &
2716 USB_PORT_STAT_CONNECTION)) 2784 USB_PORT_STAT_CONNECTION))
2717 connect_change = 1; 2785 connect_change = 1;
2718 2786
2719 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2787 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2720 clear_port_feature(hdev, i, 2788 clear_port_feature(hdev, i,
2721 USB_PORT_FEAT_C_CONNECTION); 2789 USB_PORT_FEAT_C_CONNECTION);
2722 connect_change = 1; 2790 connect_change = 1;
2723 } 2791 }
2724 2792
2725 if (portchange & USB_PORT_STAT_C_ENABLE) { 2793 if (portchange & USB_PORT_STAT_C_ENABLE) {
2726 if (!connect_change) 2794 if (!connect_change)
2727 dev_dbg (hub_dev, 2795 dev_dbg (hub_dev,
2728 "port %d enable change, " 2796 "port %d enable change, "
2729 "status %08x\n", 2797 "status %08x\n",
2730 i, portstatus); 2798 i, portstatus);
2731 clear_port_feature(hdev, i, 2799 clear_port_feature(hdev, i,
2732 USB_PORT_FEAT_C_ENABLE); 2800 USB_PORT_FEAT_C_ENABLE);
2733 2801
2734 /* 2802 /*
2735 * EM interference sometimes causes badly 2803 * EM interference sometimes causes badly
2736 * shielded USB devices to be shutdown by 2804 * shielded USB devices to be shutdown by
2737 * the hub, this hack enables them again. 2805 * the hub, this hack enables them again.
2738 * Works at least with mouse driver. 2806 * Works at least with mouse driver.
2739 */ 2807 */
2740 if (!(portstatus & USB_PORT_STAT_ENABLE) 2808 if (!(portstatus & USB_PORT_STAT_ENABLE)
2741 && !connect_change 2809 && !connect_change
2742 && hdev->children[i-1]) { 2810 && hdev->children[i-1]) {
2743 dev_err (hub_dev, 2811 dev_err (hub_dev,
2744 "port %i " 2812 "port %i "
2745 "disabled by hub (EMI?), " 2813 "disabled by hub (EMI?), "
2746 "re-enabling...\n", 2814 "re-enabling...\n",
2747 i); 2815 i);
2748 connect_change = 1; 2816 connect_change = 1;
2749 } 2817 }
2750 } 2818 }
2751 2819
2752 if (portchange & USB_PORT_STAT_C_SUSPEND) { 2820 if (portchange & USB_PORT_STAT_C_SUSPEND) {
2753 clear_port_feature(hdev, i, 2821 clear_port_feature(hdev, i,
2754 USB_PORT_FEAT_C_SUSPEND); 2822 USB_PORT_FEAT_C_SUSPEND);
2755 if (hdev->children[i-1]) { 2823 if (hdev->children[i-1]) {
2756 ret = remote_wakeup(hdev-> 2824 ret = remote_wakeup(hdev->
2757 children[i-1]); 2825 children[i-1]);
2758 if (ret < 0) 2826 if (ret < 0)
2759 connect_change = 1; 2827 connect_change = 1;
2760 } else { 2828 } else {
2761 ret = -ENODEV; 2829 ret = -ENODEV;
2762 hub_port_disable(hub, i, 1); 2830 hub_port_disable(hub, i, 1);
2763 } 2831 }
2764 dev_dbg (hub_dev, 2832 dev_dbg (hub_dev,
2765 "resume on port %d, status %d\n", 2833 "resume on port %d, status %d\n",
2766 i, ret); 2834 i, ret);
2767 } 2835 }
2768 2836
2769 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 2837 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
2770 dev_err (hub_dev, 2838 dev_err (hub_dev,
2771 "over-current change on port %d\n", 2839 "over-current change on port %d\n",
2772 i); 2840 i);
2773 clear_port_feature(hdev, i, 2841 clear_port_feature(hdev, i,
2774 USB_PORT_FEAT_C_OVER_CURRENT); 2842 USB_PORT_FEAT_C_OVER_CURRENT);
2775 hub_power_on(hub); 2843 hub_power_on(hub);
2776 } 2844 }
2777 2845
2778 if (portchange & USB_PORT_STAT_C_RESET) { 2846 if (portchange & USB_PORT_STAT_C_RESET) {
2779 dev_dbg (hub_dev, 2847 dev_dbg (hub_dev,
2780 "reset change on port %d\n", 2848 "reset change on port %d\n",
2781 i); 2849 i);
2782 clear_port_feature(hdev, i, 2850 clear_port_feature(hdev, i,
2783 USB_PORT_FEAT_C_RESET); 2851 USB_PORT_FEAT_C_RESET);
2784 } 2852 }
2785 2853
2786 if (connect_change) 2854 if (connect_change)
2787 hub_port_connect_change(hub, i, 2855 hub_port_connect_change(hub, i,
2788 portstatus, portchange); 2856 portstatus, portchange);
2789 } /* end for i */ 2857 } /* end for i */
2790 2858
2791 /* deal with hub status changes */ 2859 /* deal with hub status changes */
2792 if (test_and_clear_bit(0, hub->event_bits) == 0) 2860 if (test_and_clear_bit(0, hub->event_bits) == 0)
2793 ; /* do nothing */ 2861 ; /* do nothing */
2794 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 2862 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
2795 dev_err (hub_dev, "get_hub_status failed\n"); 2863 dev_err (hub_dev, "get_hub_status failed\n");
2796 else { 2864 else {
2797 if (hubchange & HUB_CHANGE_LOCAL_POWER) { 2865 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
2798 dev_dbg (hub_dev, "power change\n"); 2866 dev_dbg (hub_dev, "power change\n");
2799 clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 2867 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
2868 if (hubstatus & HUB_STATUS_LOCAL_POWER)
2869 /* FIXME: Is this always true? */
2870 hub->limited_power = 0;
2871 else
2872 hub->limited_power = 1;
2800 } 2873 }
2801 if (hubchange & HUB_CHANGE_OVERCURRENT) { 2874 if (hubchange & HUB_CHANGE_OVERCURRENT) {
2802 dev_dbg (hub_dev, "overcurrent change\n"); 2875 dev_dbg (hub_dev, "overcurrent change\n");
2803 msleep(500); /* Cool down */ 2876 msleep(500); /* Cool down */
2804 clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 2877 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
2805 hub_power_on(hub); 2878 hub_power_on(hub);
2806 } 2879 }
2807 } 2880 }
2808 2881
2809 hub->activating = 0; 2882 hub->activating = 0;
2810 2883
2811 /* If this is a root hub, tell the HCD it's okay to 2884 /* If this is a root hub, tell the HCD it's okay to
2812 * re-enable port-change interrupts now. */ 2885 * re-enable port-change interrupts now. */
2813 if (!hdev->parent) 2886 if (!hdev->parent)
2814 usb_enable_root_hub_irq(hdev->bus); 2887 usb_enable_root_hub_irq(hdev->bus);
2815 2888
2816 loop: 2889 loop:
2817 usb_unlock_device(hdev); 2890 usb_unlock_device(hdev);
2818 usb_put_intf(intf); 2891 usb_put_intf(intf);
2819 2892
2820 } /* end while (1) */ 2893 } /* end while (1) */
2821 } 2894 }
2822 2895
2823 static int hub_thread(void *__unused) 2896 static int hub_thread(void *__unused)
2824 { 2897 {
2825 do { 2898 do {
2826 hub_events(); 2899 hub_events();
2827 wait_event_interruptible(khubd_wait, 2900 wait_event_interruptible(khubd_wait,
2828 !list_empty(&hub_event_list) || 2901 !list_empty(&hub_event_list) ||
2829 kthread_should_stop()); 2902 kthread_should_stop());
2830 try_to_freeze(); 2903 try_to_freeze();
2831 } while (!kthread_should_stop() || !list_empty(&hub_event_list)); 2904 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
2832 2905
2833 pr_debug("%s: khubd exiting\n", usbcore_name); 2906 pr_debug("%s: khubd exiting\n", usbcore_name);
2834 return 0; 2907 return 0;
2835 } 2908 }
2836 2909
2837 static struct usb_device_id hub_id_table [] = { 2910 static struct usb_device_id hub_id_table [] = {
2838 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 2911 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
2839 .bDeviceClass = USB_CLASS_HUB}, 2912 .bDeviceClass = USB_CLASS_HUB},
2840 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 2913 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
2841 .bInterfaceClass = USB_CLASS_HUB}, 2914 .bInterfaceClass = USB_CLASS_HUB},
2842 { } /* Terminating entry */ 2915 { } /* Terminating entry */
2843 }; 2916 };
2844 2917
2845 MODULE_DEVICE_TABLE (usb, hub_id_table); 2918 MODULE_DEVICE_TABLE (usb, hub_id_table);
2846 2919
2847 static struct usb_driver hub_driver = { 2920 static struct usb_driver hub_driver = {
2848 .name = "hub", 2921 .name = "hub",
2849 .probe = hub_probe, 2922 .probe = hub_probe,
2850 .disconnect = hub_disconnect, 2923 .disconnect = hub_disconnect,
2851 .suspend = hub_suspend, 2924 .suspend = hub_suspend,
2852 .resume = hub_resume, 2925 .resume = hub_resume,
2853 .ioctl = hub_ioctl, 2926 .ioctl = hub_ioctl,
2854 .id_table = hub_id_table, 2927 .id_table = hub_id_table,
2855 }; 2928 };
2856 2929
2857 int usb_hub_init(void) 2930 int usb_hub_init(void)
2858 { 2931 {
2859 if (usb_register(&hub_driver) < 0) { 2932 if (usb_register(&hub_driver) < 0) {
2860 printk(KERN_ERR "%s: can't register hub driver\n", 2933 printk(KERN_ERR "%s: can't register hub driver\n",
2861 usbcore_name); 2934 usbcore_name);
2862 return -1; 2935 return -1;
2863 } 2936 }
2864 2937
2865 khubd_task = kthread_run(hub_thread, NULL, "khubd"); 2938 khubd_task = kthread_run(hub_thread, NULL, "khubd");
2866 if (!IS_ERR(khubd_task)) 2939 if (!IS_ERR(khubd_task))
2867 return 0; 2940 return 0;
2868 2941
2869 /* Fall through if kernel_thread failed */ 2942 /* Fall through if kernel_thread failed */
2870 usb_deregister(&hub_driver); 2943 usb_deregister(&hub_driver);
2871 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); 2944 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
2872 2945
2873 return -1; 2946 return -1;
2874 } 2947 }
2875 2948
2876 void usb_hub_cleanup(void) 2949 void usb_hub_cleanup(void)
2877 { 2950 {
2878 kthread_stop(khubd_task); 2951 kthread_stop(khubd_task);
2879 2952
2880 /* 2953 /*
2881 * Hub resources are freed for us by usb_deregister. It calls 2954 * Hub resources are freed for us by usb_deregister. It calls
2882 * usb_driver_purge on every device which in turn calls that 2955 * usb_driver_purge on every device which in turn calls that
2883 * devices disconnect function if it is using this driver. 2956 * devices disconnect function if it is using this driver.
2884 * The hub_disconnect function takes care of releasing the 2957 * The hub_disconnect function takes care of releasing the
2885 * individual hub resources. -greg 2958 * individual hub resources. -greg
2886 */ 2959 */
2887 usb_deregister(&hub_driver); 2960 usb_deregister(&hub_driver);
2888 } /* usb_hub_cleanup() */ 2961 } /* usb_hub_cleanup() */
2889 2962
2890 static int config_descriptors_changed(struct usb_device *udev) 2963 static int config_descriptors_changed(struct usb_device *udev)
2891 { 2964 {
2892 unsigned index; 2965 unsigned index;
2893 unsigned len = 0; 2966 unsigned len = 0;
2894 struct usb_config_descriptor *buf; 2967 struct usb_config_descriptor *buf;
2895 2968
2896 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 2969 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
2897 if (len < le16_to_cpu(udev->config[index].desc.wTotalLength)) 2970 if (len < le16_to_cpu(udev->config[index].desc.wTotalLength))
2898 len = le16_to_cpu(udev->config[index].desc.wTotalLength); 2971 len = le16_to_cpu(udev->config[index].desc.wTotalLength);
2899 } 2972 }
2900 buf = kmalloc (len, SLAB_KERNEL); 2973 buf = kmalloc (len, SLAB_KERNEL);
2901 if (buf == NULL) { 2974 if (buf == NULL) {
2902 dev_err(&udev->dev, "no mem to re-read configs after reset\n"); 2975 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
2903 /* assume the worst */ 2976 /* assume the worst */
2904 return 1; 2977 return 1;
2905 } 2978 }
2906 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 2979 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
2907 int length; 2980 int length;
2908 int old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 2981 int old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
2909 2982
2910 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 2983 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
2911 old_length); 2984 old_length);
2912 if (length < old_length) { 2985 if (length < old_length) {
2913 dev_dbg(&udev->dev, "config index %d, error %d\n", 2986 dev_dbg(&udev->dev, "config index %d, error %d\n",
2914 index, length); 2987 index, length);
2915 break; 2988 break;
2916 } 2989 }
2917 if (memcmp (buf, udev->rawdescriptors[index], old_length) 2990 if (memcmp (buf, udev->rawdescriptors[index], old_length)
2918 != 0) { 2991 != 0) {
2919 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 2992 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
2920 index, buf->bConfigurationValue); 2993 index, buf->bConfigurationValue);
2921 break; 2994 break;
2922 } 2995 }
2923 } 2996 }
2924 kfree(buf); 2997 kfree(buf);
2925 return index != udev->descriptor.bNumConfigurations; 2998 return index != udev->descriptor.bNumConfigurations;
2926 } 2999 }
2927 3000
2928 /** 3001 /**
2929 * usb_reset_device - perform a USB port reset to reinitialize a device 3002 * usb_reset_device - perform a USB port reset to reinitialize a device
2930 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 3003 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
2931 * 3004 *
2932 * WARNING - don't reset any device unless drivers for all of its 3005 * WARNING - don't reset any device unless drivers for all of its
2933 * interfaces are expecting that reset! Maybe some driver->reset() 3006 * interfaces are expecting that reset! Maybe some driver->reset()
2934 * method should eventually help ensure sufficient cooperation. 3007 * method should eventually help ensure sufficient cooperation.
2935 * 3008 *
2936 * Do a port reset, reassign the device's address, and establish its 3009 * Do a port reset, reassign the device's address, and establish its
2937 * former operating configuration. If the reset fails, or the device's 3010 * former operating configuration. If the reset fails, or the device's
2938 * descriptors change from their values before the reset, or the original 3011 * descriptors change from their values before the reset, or the original
2939 * configuration and altsettings cannot be restored, a flag will be set 3012 * configuration and altsettings cannot be restored, a flag will be set
2940 * telling khubd to pretend the device has been disconnected and then 3013 * telling khubd to pretend the device has been disconnected and then
2941 * re-connected. All drivers will be unbound, and the device will be 3014 * re-connected. All drivers will be unbound, and the device will be
2942 * re-enumerated and probed all over again. 3015 * re-enumerated and probed all over again.
2943 * 3016 *
2944 * Returns 0 if the reset succeeded, -ENODEV if the device has been 3017 * Returns 0 if the reset succeeded, -ENODEV if the device has been
2945 * flagged for logical disconnection, or some other negative error code 3018 * flagged for logical disconnection, or some other negative error code
2946 * if the reset wasn't even attempted. 3019 * if the reset wasn't even attempted.
2947 * 3020 *
2948 * The caller must own the device lock. For example, it's safe to use 3021 * The caller must own the device lock. For example, it's safe to use
2949 * this from a driver probe() routine after downloading new firmware. 3022 * this from a driver probe() routine after downloading new firmware.
2950 * For calls that might not occur during probe(), drivers should lock 3023 * For calls that might not occur during probe(), drivers should lock
2951 * the device using usb_lock_device_for_reset(). 3024 * the device using usb_lock_device_for_reset().
2952 */ 3025 */
2953 int usb_reset_device(struct usb_device *udev) 3026 int usb_reset_device(struct usb_device *udev)
2954 { 3027 {
2955 struct usb_device *parent_hdev = udev->parent; 3028 struct usb_device *parent_hdev = udev->parent;
2956 struct usb_hub *parent_hub; 3029 struct usb_hub *parent_hub;
2957 struct usb_device_descriptor descriptor = udev->descriptor; 3030 struct usb_device_descriptor descriptor = udev->descriptor;
2958 struct usb_hub *hub = NULL; 3031 struct usb_hub *hub = NULL;
2959 int i, ret = 0, port1 = -1; 3032 int i, ret = 0, port1 = -1;
2960 3033
2961 if (udev->state == USB_STATE_NOTATTACHED || 3034 if (udev->state == USB_STATE_NOTATTACHED ||
2962 udev->state == USB_STATE_SUSPENDED) { 3035 udev->state == USB_STATE_SUSPENDED) {
2963 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 3036 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
2964 udev->state); 3037 udev->state);
2965 return -EINVAL; 3038 return -EINVAL;
2966 } 3039 }
2967 3040
2968 if (!parent_hdev) { 3041 if (!parent_hdev) {
2969 /* this requires hcd-specific logic; see OHCI hc_restart() */ 3042 /* this requires hcd-specific logic; see OHCI hc_restart() */
2970 dev_dbg(&udev->dev, "%s for root hub!\n", __FUNCTION__); 3043 dev_dbg(&udev->dev, "%s for root hub!\n", __FUNCTION__);
2971 return -EISDIR; 3044 return -EISDIR;
2972 } 3045 }
2973 3046
2974 for (i = 0; i < parent_hdev->maxchild; i++) 3047 for (i = 0; i < parent_hdev->maxchild; i++)
2975 if (parent_hdev->children[i] == udev) { 3048 if (parent_hdev->children[i] == udev) {
2976 port1 = i + 1; 3049 port1 = i + 1;
2977 break; 3050 break;
2978 } 3051 }
2979 3052
2980 if (port1 < 0) { 3053 if (port1 < 0) {
2981 /* If this ever happens, it's very bad */ 3054 /* If this ever happens, it's very bad */
2982 dev_err(&udev->dev, "Can't locate device's port!\n"); 3055 dev_err(&udev->dev, "Can't locate device's port!\n");
2983 return -ENOENT; 3056 return -ENOENT;
2984 } 3057 }
2985 parent_hub = hdev_to_hub(parent_hdev); 3058 parent_hub = hdev_to_hub(parent_hdev);
2986 3059
2987 /* If we're resetting an active hub, take some special actions */ 3060 /* If we're resetting an active hub, take some special actions */
2988 if (udev->actconfig && 3061 if (udev->actconfig &&
2989 udev->actconfig->interface[0]->dev.driver == 3062 udev->actconfig->interface[0]->dev.driver ==
2990 &hub_driver.driver && 3063 &hub_driver.driver &&
2991 (hub = hdev_to_hub(udev)) != NULL) { 3064 (hub = hdev_to_hub(udev)) != NULL) {
2992 hub_pre_reset(hub, 0); 3065 hub_pre_reset(hub, 0);
2993 } 3066 }
2994 3067
2995 set_bit(port1, parent_hub->busy_bits); 3068 set_bit(port1, parent_hub->busy_bits);
2996 for (i = 0; i < SET_CONFIG_TRIES; ++i) { 3069 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
2997 3070
2998 /* ep0 maxpacket size may change; let the HCD know about it. 3071 /* ep0 maxpacket size may change; let the HCD know about it.
2999 * Other endpoints will be handled by re-enumeration. */ 3072 * Other endpoints will be handled by re-enumeration. */
3000 ep0_reinit(udev); 3073 ep0_reinit(udev);
3001 ret = hub_port_init(parent_hub, udev, port1, i); 3074 ret = hub_port_init(parent_hub, udev, port1, i);
3002 if (ret >= 0) 3075 if (ret >= 0)
3003 break; 3076 break;
3004 } 3077 }
3005 clear_bit(port1, parent_hub->busy_bits); 3078 clear_bit(port1, parent_hub->busy_bits);
3006 if (ret < 0) 3079 if (ret < 0)
3007 goto re_enumerate; 3080 goto re_enumerate;
3008 3081
3009 /* Device might have changed firmware (DFU or similar) */ 3082 /* Device might have changed firmware (DFU or similar) */
3010 if (memcmp(&udev->descriptor, &descriptor, sizeof descriptor) 3083 if (memcmp(&udev->descriptor, &descriptor, sizeof descriptor)
3011 || config_descriptors_changed (udev)) { 3084 || config_descriptors_changed (udev)) {
3012 dev_info(&udev->dev, "device firmware changed\n"); 3085 dev_info(&udev->dev, "device firmware changed\n");
3013 udev->descriptor = descriptor; /* for disconnect() calls */ 3086 udev->descriptor = descriptor; /* for disconnect() calls */
3014 goto re_enumerate; 3087 goto re_enumerate;
3015 } 3088 }
3016 3089
3017 if (!udev->actconfig) 3090 if (!udev->actconfig)
3018 goto done; 3091 goto done;
3019 3092
3020 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3093 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3021 USB_REQ_SET_CONFIGURATION, 0, 3094 USB_REQ_SET_CONFIGURATION, 0,
3022 udev->actconfig->desc.bConfigurationValue, 0, 3095 udev->actconfig->desc.bConfigurationValue, 0,
3023 NULL, 0, USB_CTRL_SET_TIMEOUT); 3096 NULL, 0, USB_CTRL_SET_TIMEOUT);
3024 if (ret < 0) { 3097 if (ret < 0) {
3025 dev_err(&udev->dev, 3098 dev_err(&udev->dev,
3026 "can't restore configuration #%d (error=%d)\n", 3099 "can't restore configuration #%d (error=%d)\n",
3027 udev->actconfig->desc.bConfigurationValue, ret); 3100 udev->actconfig->desc.bConfigurationValue, ret);
3028 goto re_enumerate; 3101 goto re_enumerate;
3029 } 3102 }
3030 usb_set_device_state(udev, USB_STATE_CONFIGURED); 3103 usb_set_device_state(udev, USB_STATE_CONFIGURED);
3031 3104
3032 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 3105 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3033 struct usb_interface *intf = udev->actconfig->interface[i]; 3106 struct usb_interface *intf = udev->actconfig->interface[i];
3034 struct usb_interface_descriptor *desc; 3107 struct usb_interface_descriptor *desc;
3035 3108
3036 /* set_interface resets host side toggle even 3109 /* set_interface resets host side toggle even
3037 * for altsetting zero. the interface may have no driver. 3110 * for altsetting zero. the interface may have no driver.
3038 */ 3111 */
3039 desc = &intf->cur_altsetting->desc; 3112 desc = &intf->cur_altsetting->desc;
3040 ret = usb_set_interface(udev, desc->bInterfaceNumber, 3113 ret = usb_set_interface(udev, desc->bInterfaceNumber,
3041 desc->bAlternateSetting); 3114 desc->bAlternateSetting);
3042 if (ret < 0) { 3115 if (ret < 0) {
3043 dev_err(&udev->dev, "failed to restore interface %d " 3116 dev_err(&udev->dev, "failed to restore interface %d "
3044 "altsetting %d (error=%d)\n", 3117 "altsetting %d (error=%d)\n",
3045 desc->bInterfaceNumber, 3118 desc->bInterfaceNumber,
3046 desc->bAlternateSetting, 3119 desc->bAlternateSetting,
3047 ret); 3120 ret);
3048 goto re_enumerate; 3121 goto re_enumerate;
drivers/usb/core/hub.h
1 #ifndef __LINUX_HUB_H 1 #ifndef __LINUX_HUB_H
2 #define __LINUX_HUB_H 2 #define __LINUX_HUB_H
3 3
4 /* 4 /*
5 * Hub protocol and driver data structures. 5 * Hub protocol and driver data structures.
6 * 6 *
7 * Some of these are known to the "virtual root hub" code 7 * Some of these are known to the "virtual root hub" code
8 * in host controller drivers. 8 * in host controller drivers.
9 */ 9 */
10 10
11 #include <linux/list.h> 11 #include <linux/list.h>
12 #include <linux/workqueue.h> 12 #include <linux/workqueue.h>
13 #include <linux/compiler.h> /* likely()/unlikely() */ 13 #include <linux/compiler.h> /* likely()/unlikely() */
14 14
15 /* 15 /*
16 * Hub request types 16 * Hub request types
17 */ 17 */
18 18
19 #define USB_RT_HUB (USB_TYPE_CLASS | USB_RECIP_DEVICE) 19 #define USB_RT_HUB (USB_TYPE_CLASS | USB_RECIP_DEVICE)
20 #define USB_RT_PORT (USB_TYPE_CLASS | USB_RECIP_OTHER) 20 #define USB_RT_PORT (USB_TYPE_CLASS | USB_RECIP_OTHER)
21 21
22 /* 22 /*
23 * Hub class requests 23 * Hub class requests
24 * See USB 2.0 spec Table 11-16 24 * See USB 2.0 spec Table 11-16
25 */ 25 */
26 #define HUB_CLEAR_TT_BUFFER 8 26 #define HUB_CLEAR_TT_BUFFER 8
27 #define HUB_RESET_TT 9 27 #define HUB_RESET_TT 9
28 #define HUB_GET_TT_STATE 10 28 #define HUB_GET_TT_STATE 10
29 #define HUB_STOP_TT 11 29 #define HUB_STOP_TT 11
30 30
31 /* 31 /*
32 * Hub Class feature numbers 32 * Hub Class feature numbers
33 * See USB 2.0 spec Table 11-17 33 * See USB 2.0 spec Table 11-17
34 */ 34 */
35 #define C_HUB_LOCAL_POWER 0 35 #define C_HUB_LOCAL_POWER 0
36 #define C_HUB_OVER_CURRENT 1 36 #define C_HUB_OVER_CURRENT 1
37 37
38 /* 38 /*
39 * Port feature numbers 39 * Port feature numbers
40 * See USB 2.0 spec Table 11-17 40 * See USB 2.0 spec Table 11-17
41 */ 41 */
42 #define USB_PORT_FEAT_CONNECTION 0 42 #define USB_PORT_FEAT_CONNECTION 0
43 #define USB_PORT_FEAT_ENABLE 1 43 #define USB_PORT_FEAT_ENABLE 1
44 #define USB_PORT_FEAT_SUSPEND 2 44 #define USB_PORT_FEAT_SUSPEND 2
45 #define USB_PORT_FEAT_OVER_CURRENT 3 45 #define USB_PORT_FEAT_OVER_CURRENT 3
46 #define USB_PORT_FEAT_RESET 4 46 #define USB_PORT_FEAT_RESET 4
47 #define USB_PORT_FEAT_POWER 8 47 #define USB_PORT_FEAT_POWER 8
48 #define USB_PORT_FEAT_LOWSPEED 9 48 #define USB_PORT_FEAT_LOWSPEED 9
49 #define USB_PORT_FEAT_HIGHSPEED 10 49 #define USB_PORT_FEAT_HIGHSPEED 10
50 #define USB_PORT_FEAT_C_CONNECTION 16 50 #define USB_PORT_FEAT_C_CONNECTION 16
51 #define USB_PORT_FEAT_C_ENABLE 17 51 #define USB_PORT_FEAT_C_ENABLE 17
52 #define USB_PORT_FEAT_C_SUSPEND 18 52 #define USB_PORT_FEAT_C_SUSPEND 18
53 #define USB_PORT_FEAT_C_OVER_CURRENT 19 53 #define USB_PORT_FEAT_C_OVER_CURRENT 19
54 #define USB_PORT_FEAT_C_RESET 20 54 #define USB_PORT_FEAT_C_RESET 20
55 #define USB_PORT_FEAT_TEST 21 55 #define USB_PORT_FEAT_TEST 21
56 #define USB_PORT_FEAT_INDICATOR 22 56 #define USB_PORT_FEAT_INDICATOR 22
57 57
58 /* 58 /*
59 * Hub Status and Hub Change results 59 * Hub Status and Hub Change results
60 * See USB 2.0 spec Table 11-19 and Table 11-20 60 * See USB 2.0 spec Table 11-19 and Table 11-20
61 */ 61 */
62 struct usb_port_status { 62 struct usb_port_status {
63 __le16 wPortStatus; 63 __le16 wPortStatus;
64 __le16 wPortChange; 64 __le16 wPortChange;
65 } __attribute__ ((packed)); 65 } __attribute__ ((packed));
66 66
67 /* 67 /*
68 * wPortStatus bit field 68 * wPortStatus bit field
69 * See USB 2.0 spec Table 11-21 69 * See USB 2.0 spec Table 11-21
70 */ 70 */
71 #define USB_PORT_STAT_CONNECTION 0x0001 71 #define USB_PORT_STAT_CONNECTION 0x0001
72 #define USB_PORT_STAT_ENABLE 0x0002 72 #define USB_PORT_STAT_ENABLE 0x0002
73 #define USB_PORT_STAT_SUSPEND 0x0004 73 #define USB_PORT_STAT_SUSPEND 0x0004
74 #define USB_PORT_STAT_OVERCURRENT 0x0008 74 #define USB_PORT_STAT_OVERCURRENT 0x0008
75 #define USB_PORT_STAT_RESET 0x0010 75 #define USB_PORT_STAT_RESET 0x0010
76 /* bits 5 to 7 are reserved */ 76 /* bits 5 to 7 are reserved */
77 #define USB_PORT_STAT_POWER 0x0100 77 #define USB_PORT_STAT_POWER 0x0100
78 #define USB_PORT_STAT_LOW_SPEED 0x0200 78 #define USB_PORT_STAT_LOW_SPEED 0x0200
79 #define USB_PORT_STAT_HIGH_SPEED 0x0400 79 #define USB_PORT_STAT_HIGH_SPEED 0x0400
80 #define USB_PORT_STAT_TEST 0x0800 80 #define USB_PORT_STAT_TEST 0x0800
81 #define USB_PORT_STAT_INDICATOR 0x1000 81 #define USB_PORT_STAT_INDICATOR 0x1000
82 /* bits 13 to 15 are reserved */ 82 /* bits 13 to 15 are reserved */
83 83
84 /* 84 /*
85 * wPortChange bit field 85 * wPortChange bit field
86 * See USB 2.0 spec Table 11-22 86 * See USB 2.0 spec Table 11-22
87 * Bits 0 to 4 shown, bits 5 to 15 are reserved 87 * Bits 0 to 4 shown, bits 5 to 15 are reserved
88 */ 88 */
89 #define USB_PORT_STAT_C_CONNECTION 0x0001 89 #define USB_PORT_STAT_C_CONNECTION 0x0001
90 #define USB_PORT_STAT_C_ENABLE 0x0002 90 #define USB_PORT_STAT_C_ENABLE 0x0002
91 #define USB_PORT_STAT_C_SUSPEND 0x0004 91 #define USB_PORT_STAT_C_SUSPEND 0x0004
92 #define USB_PORT_STAT_C_OVERCURRENT 0x0008 92 #define USB_PORT_STAT_C_OVERCURRENT 0x0008
93 #define USB_PORT_STAT_C_RESET 0x0010 93 #define USB_PORT_STAT_C_RESET 0x0010
94 94
95 /* 95 /*
96 * wHubCharacteristics (masks) 96 * wHubCharacteristics (masks)
97 * See USB 2.0 spec Table 11-13, offset 3 97 * See USB 2.0 spec Table 11-13, offset 3
98 */ 98 */
99 #define HUB_CHAR_LPSM 0x0003 /* D1 .. D0 */ 99 #define HUB_CHAR_LPSM 0x0003 /* D1 .. D0 */
100 #define HUB_CHAR_COMPOUND 0x0004 /* D2 */ 100 #define HUB_CHAR_COMPOUND 0x0004 /* D2 */
101 #define HUB_CHAR_OCPM 0x0018 /* D4 .. D3 */ 101 #define HUB_CHAR_OCPM 0x0018 /* D4 .. D3 */
102 #define HUB_CHAR_TTTT 0x0060 /* D6 .. D5 */ 102 #define HUB_CHAR_TTTT 0x0060 /* D6 .. D5 */
103 #define HUB_CHAR_PORTIND 0x0080 /* D7 */ 103 #define HUB_CHAR_PORTIND 0x0080 /* D7 */
104 104
105 struct usb_hub_status { 105 struct usb_hub_status {
106 __le16 wHubStatus; 106 __le16 wHubStatus;
107 __le16 wHubChange; 107 __le16 wHubChange;
108 } __attribute__ ((packed)); 108 } __attribute__ ((packed));
109 109
110 /* 110 /*
111 * Hub Status & Hub Change bit masks 111 * Hub Status & Hub Change bit masks
112 * See USB 2.0 spec Table 11-19 and Table 11-20 112 * See USB 2.0 spec Table 11-19 and Table 11-20
113 * Bits 0 and 1 for wHubStatus and wHubChange 113 * Bits 0 and 1 for wHubStatus and wHubChange
114 * Bits 2 to 15 are reserved for both 114 * Bits 2 to 15 are reserved for both
115 */ 115 */
116 #define HUB_STATUS_LOCAL_POWER 0x0001 116 #define HUB_STATUS_LOCAL_POWER 0x0001
117 #define HUB_STATUS_OVERCURRENT 0x0002 117 #define HUB_STATUS_OVERCURRENT 0x0002
118 #define HUB_CHANGE_LOCAL_POWER 0x0001 118 #define HUB_CHANGE_LOCAL_POWER 0x0001
119 #define HUB_CHANGE_OVERCURRENT 0x0002 119 #define HUB_CHANGE_OVERCURRENT 0x0002
120 120
121 121
122 /* 122 /*
123 * Hub descriptor 123 * Hub descriptor
124 * See USB 2.0 spec Table 11-13 124 * See USB 2.0 spec Table 11-13
125 */ 125 */
126 126
127 #define USB_DT_HUB (USB_TYPE_CLASS | 0x09) 127 #define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
128 #define USB_DT_HUB_NONVAR_SIZE 7 128 #define USB_DT_HUB_NONVAR_SIZE 7
129 129
130 struct usb_hub_descriptor { 130 struct usb_hub_descriptor {
131 __u8 bDescLength; 131 __u8 bDescLength;
132 __u8 bDescriptorType; 132 __u8 bDescriptorType;
133 __u8 bNbrPorts; 133 __u8 bNbrPorts;
134 __le16 wHubCharacteristics; 134 __le16 wHubCharacteristics;
135 __u8 bPwrOn2PwrGood; 135 __u8 bPwrOn2PwrGood;
136 __u8 bHubContrCurrent; 136 __u8 bHubContrCurrent;
137 /* add 1 bit for hub status change; round to bytes */ 137 /* add 1 bit for hub status change; round to bytes */
138 __u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8]; 138 __u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8];
139 __u8 PortPwrCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8]; 139 __u8 PortPwrCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8];
140 } __attribute__ ((packed)); 140 } __attribute__ ((packed));
141 141
142 142
143 /* port indicator status selectors, tables 11-7 and 11-25 */ 143 /* port indicator status selectors, tables 11-7 and 11-25 */
144 #define HUB_LED_AUTO 0 144 #define HUB_LED_AUTO 0
145 #define HUB_LED_AMBER 1 145 #define HUB_LED_AMBER 1
146 #define HUB_LED_GREEN 2 146 #define HUB_LED_GREEN 2
147 #define HUB_LED_OFF 3 147 #define HUB_LED_OFF 3
148 148
149 enum hub_led_mode { 149 enum hub_led_mode {
150 INDICATOR_AUTO = 0, 150 INDICATOR_AUTO = 0,
151 INDICATOR_CYCLE, 151 INDICATOR_CYCLE,
152 /* software blinks for attention: software, hardware, reserved */ 152 /* software blinks for attention: software, hardware, reserved */
153 INDICATOR_GREEN_BLINK, INDICATOR_GREEN_BLINK_OFF, 153 INDICATOR_GREEN_BLINK, INDICATOR_GREEN_BLINK_OFF,
154 INDICATOR_AMBER_BLINK, INDICATOR_AMBER_BLINK_OFF, 154 INDICATOR_AMBER_BLINK, INDICATOR_AMBER_BLINK_OFF,
155 INDICATOR_ALT_BLINK, INDICATOR_ALT_BLINK_OFF 155 INDICATOR_ALT_BLINK, INDICATOR_ALT_BLINK_OFF
156 } __attribute__ ((packed)); 156 } __attribute__ ((packed));
157 157
158 struct usb_device; 158 struct usb_device;
159 159
160 /* Transaction Translator Think Times, in bits */ 160 /* Transaction Translator Think Times, in bits */
161 #define HUB_TTTT_8_BITS 0x00 161 #define HUB_TTTT_8_BITS 0x00
162 #define HUB_TTTT_16_BITS 0x20 162 #define HUB_TTTT_16_BITS 0x20
163 #define HUB_TTTT_24_BITS 0x40 163 #define HUB_TTTT_24_BITS 0x40
164 #define HUB_TTTT_32_BITS 0x60 164 #define HUB_TTTT_32_BITS 0x60
165 165
166 /* 166 /*
167 * As of USB 2.0, full/low speed devices are segregated into trees. 167 * As of USB 2.0, full/low speed devices are segregated into trees.
168 * One type grows from USB 1.1 host controllers (OHCI, UHCI etc). 168 * One type grows from USB 1.1 host controllers (OHCI, UHCI etc).
169 * The other type grows from high speed hubs when they connect to 169 * The other type grows from high speed hubs when they connect to
170 * full/low speed devices using "Transaction Translators" (TTs). 170 * full/low speed devices using "Transaction Translators" (TTs).
171 * 171 *
172 * TTs should only be known to the hub driver, and high speed bus 172 * TTs should only be known to the hub driver, and high speed bus
173 * drivers (only EHCI for now). They affect periodic scheduling and 173 * drivers (only EHCI for now). They affect periodic scheduling and
174 * sometimes control/bulk error recovery. 174 * sometimes control/bulk error recovery.
175 */ 175 */
176 struct usb_tt { 176 struct usb_tt {
177 struct usb_device *hub; /* upstream highspeed hub */ 177 struct usb_device *hub; /* upstream highspeed hub */
178 int multi; /* true means one TT per port */ 178 int multi; /* true means one TT per port */
179 unsigned think_time; /* think time in ns */ 179 unsigned think_time; /* think time in ns */
180 180
181 /* for control/bulk error recovery (CLEAR_TT_BUFFER) */ 181 /* for control/bulk error recovery (CLEAR_TT_BUFFER) */
182 spinlock_t lock; 182 spinlock_t lock;
183 struct list_head clear_list; /* of usb_tt_clear */ 183 struct list_head clear_list; /* of usb_tt_clear */
184 struct work_struct kevent; 184 struct work_struct kevent;
185 }; 185 };
186 186
187 struct usb_tt_clear { 187 struct usb_tt_clear {
188 struct list_head clear_list; 188 struct list_head clear_list;
189 unsigned tt; 189 unsigned tt;
190 u16 devinfo; 190 u16 devinfo;
191 }; 191 };
192 192
193 extern void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe); 193 extern void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe);
194 194
195 struct usb_hub { 195 struct usb_hub {
196 struct device *intfdev; /* the "interface" device */ 196 struct device *intfdev; /* the "interface" device */
197 struct usb_device *hdev; 197 struct usb_device *hdev;
198 struct urb *urb; /* for interrupt polling pipe */ 198 struct urb *urb; /* for interrupt polling pipe */
199 199
200 /* buffer for urb ... with extra space in case of babble */ 200 /* buffer for urb ... with extra space in case of babble */
201 char (*buffer)[8]; 201 char (*buffer)[8];
202 dma_addr_t buffer_dma; /* DMA address for buffer */ 202 dma_addr_t buffer_dma; /* DMA address for buffer */
203 union { 203 union {
204 struct usb_hub_status hub; 204 struct usb_hub_status hub;
205 struct usb_port_status port; 205 struct usb_port_status port;
206 } *status; /* buffer for status reports */ 206 } *status; /* buffer for status reports */
207 207
208 int error; /* last reported error */ 208 int error; /* last reported error */
209 int nerrors; /* track consecutive errors */ 209 int nerrors; /* track consecutive errors */
210 210
211 struct list_head event_list; /* hubs w/data or errs ready */ 211 struct list_head event_list; /* hubs w/data or errs ready */
212 unsigned long event_bits[1]; /* status change bitmask */ 212 unsigned long event_bits[1]; /* status change bitmask */
213 unsigned long change_bits[1]; /* ports with logical connect 213 unsigned long change_bits[1]; /* ports with logical connect
214 status change */ 214 status change */
215 unsigned long busy_bits[1]; /* ports being reset */ 215 unsigned long busy_bits[1]; /* ports being reset */
216 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 216 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
217 #error event_bits[] is too short! 217 #error event_bits[] is too short!
218 #endif 218 #endif
219 219
220 struct usb_hub_descriptor *descriptor; /* class descriptor */ 220 struct usb_hub_descriptor *descriptor; /* class descriptor */
221 struct usb_tt tt; /* Transaction Translator */ 221 struct usb_tt tt; /* Transaction Translator */
222 222
223 u8 power_budget; /* in 2mA units; or zero */ 223 unsigned mA_per_port; /* current for each child */
224 224
225 unsigned limited_power:1;
225 unsigned quiescing:1; 226 unsigned quiescing:1;
226 unsigned activating:1; 227 unsigned activating:1;
227 unsigned resume_root_hub:1; 228 unsigned resume_root_hub:1;
228 229
229 unsigned has_indicators:1; 230 unsigned has_indicators:1;
230 enum hub_led_mode indicator[USB_MAXCHILDREN]; 231 enum hub_led_mode indicator[USB_MAXCHILDREN];
231 struct work_struct leds; 232 struct work_struct leds;
232 }; 233 };
233 234
234 #endif /* __LINUX_HUB_H */ 235 #endif /* __LINUX_HUB_H */
235 236
drivers/usb/core/message.c
1 /* 1 /*
2 * message.c - synchronous message handling 2 * message.c - synchronous message handling
3 */ 3 */
4 4
5 #include <linux/config.h> 5 #include <linux/config.h>
6 #include <linux/pci.h> /* for scatterlist macros */ 6 #include <linux/pci.h> /* for scatterlist macros */
7 #include <linux/usb.h> 7 #include <linux/usb.h>
8 #include <linux/module.h> 8 #include <linux/module.h>
9 #include <linux/slab.h> 9 #include <linux/slab.h>
10 #include <linux/init.h> 10 #include <linux/init.h>
11 #include <linux/mm.h> 11 #include <linux/mm.h>
12 #include <linux/timer.h> 12 #include <linux/timer.h>
13 #include <linux/ctype.h> 13 #include <linux/ctype.h>
14 #include <linux/device.h> 14 #include <linux/device.h>
15 #include <asm/byteorder.h> 15 #include <asm/byteorder.h>
16 16
17 #include "hcd.h" /* for usbcore internals */ 17 #include "hcd.h" /* for usbcore internals */
18 #include "usb.h" 18 #include "usb.h"
19 19
20 static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs) 20 static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs)
21 { 21 {
22 complete((struct completion *)urb->context); 22 complete((struct completion *)urb->context);
23 } 23 }
24 24
25 25
26 static void timeout_kill(unsigned long data) 26 static void timeout_kill(unsigned long data)
27 { 27 {
28 struct urb *urb = (struct urb *) data; 28 struct urb *urb = (struct urb *) data;
29 29
30 usb_unlink_urb(urb); 30 usb_unlink_urb(urb);
31 } 31 }
32 32
33 // Starts urb and waits for completion or timeout 33 // Starts urb and waits for completion or timeout
34 // note that this call is NOT interruptible, while 34 // note that this call is NOT interruptible, while
35 // many device driver i/o requests should be interruptible 35 // many device driver i/o requests should be interruptible
36 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length) 36 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
37 { 37 {
38 struct completion done; 38 struct completion done;
39 struct timer_list timer; 39 struct timer_list timer;
40 int status; 40 int status;
41 41
42 init_completion(&done); 42 init_completion(&done);
43 urb->context = &done; 43 urb->context = &done;
44 urb->actual_length = 0; 44 urb->actual_length = 0;
45 status = usb_submit_urb(urb, GFP_NOIO); 45 status = usb_submit_urb(urb, GFP_NOIO);
46 46
47 if (status == 0) { 47 if (status == 0) {
48 if (timeout > 0) { 48 if (timeout > 0) {
49 init_timer(&timer); 49 init_timer(&timer);
50 timer.expires = jiffies + msecs_to_jiffies(timeout); 50 timer.expires = jiffies + msecs_to_jiffies(timeout);
51 timer.data = (unsigned long)urb; 51 timer.data = (unsigned long)urb;
52 timer.function = timeout_kill; 52 timer.function = timeout_kill;
53 /* grr. timeout _should_ include submit delays. */ 53 /* grr. timeout _should_ include submit delays. */
54 add_timer(&timer); 54 add_timer(&timer);
55 } 55 }
56 wait_for_completion(&done); 56 wait_for_completion(&done);
57 status = urb->status; 57 status = urb->status;
58 /* note: HCDs return ETIMEDOUT for other reasons too */ 58 /* note: HCDs return ETIMEDOUT for other reasons too */
59 if (status == -ECONNRESET) { 59 if (status == -ECONNRESET) {
60 dev_dbg(&urb->dev->dev, 60 dev_dbg(&urb->dev->dev,
61 "%s timed out on ep%d%s len=%d/%d\n", 61 "%s timed out on ep%d%s len=%d/%d\n",
62 current->comm, 62 current->comm,
63 usb_pipeendpoint(urb->pipe), 63 usb_pipeendpoint(urb->pipe),
64 usb_pipein(urb->pipe) ? "in" : "out", 64 usb_pipein(urb->pipe) ? "in" : "out",
65 urb->actual_length, 65 urb->actual_length,
66 urb->transfer_buffer_length 66 urb->transfer_buffer_length
67 ); 67 );
68 if (urb->actual_length > 0) 68 if (urb->actual_length > 0)
69 status = 0; 69 status = 0;
70 else 70 else
71 status = -ETIMEDOUT; 71 status = -ETIMEDOUT;
72 } 72 }
73 if (timeout > 0) 73 if (timeout > 0)
74 del_timer_sync(&timer); 74 del_timer_sync(&timer);
75 } 75 }
76 76
77 if (actual_length) 77 if (actual_length)
78 *actual_length = urb->actual_length; 78 *actual_length = urb->actual_length;
79 usb_free_urb(urb); 79 usb_free_urb(urb);
80 return status; 80 return status;
81 } 81 }
82 82
83 /*-------------------------------------------------------------------*/ 83 /*-------------------------------------------------------------------*/
84 // returns status (negative) or length (positive) 84 // returns status (negative) or length (positive)
85 static int usb_internal_control_msg(struct usb_device *usb_dev, 85 static int usb_internal_control_msg(struct usb_device *usb_dev,
86 unsigned int pipe, 86 unsigned int pipe,
87 struct usb_ctrlrequest *cmd, 87 struct usb_ctrlrequest *cmd,
88 void *data, int len, int timeout) 88 void *data, int len, int timeout)
89 { 89 {
90 struct urb *urb; 90 struct urb *urb;
91 int retv; 91 int retv;
92 int length; 92 int length;
93 93
94 urb = usb_alloc_urb(0, GFP_NOIO); 94 urb = usb_alloc_urb(0, GFP_NOIO);
95 if (!urb) 95 if (!urb)
96 return -ENOMEM; 96 return -ENOMEM;
97 97
98 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data, 98 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
99 len, usb_api_blocking_completion, NULL); 99 len, usb_api_blocking_completion, NULL);
100 100
101 retv = usb_start_wait_urb(urb, timeout, &length); 101 retv = usb_start_wait_urb(urb, timeout, &length);
102 if (retv < 0) 102 if (retv < 0)
103 return retv; 103 return retv;
104 else 104 else
105 return length; 105 return length;
106 } 106 }
107 107
108 /** 108 /**
109 * usb_control_msg - Builds a control urb, sends it off and waits for completion 109 * usb_control_msg - Builds a control urb, sends it off and waits for completion
110 * @dev: pointer to the usb device to send the message to 110 * @dev: pointer to the usb device to send the message to
111 * @pipe: endpoint "pipe" to send the message to 111 * @pipe: endpoint "pipe" to send the message to
112 * @request: USB message request value 112 * @request: USB message request value
113 * @requesttype: USB message request type value 113 * @requesttype: USB message request type value
114 * @value: USB message value 114 * @value: USB message value
115 * @index: USB message index value 115 * @index: USB message index value
116 * @data: pointer to the data to send 116 * @data: pointer to the data to send
117 * @size: length in bytes of the data to send 117 * @size: length in bytes of the data to send
118 * @timeout: time in msecs to wait for the message to complete before 118 * @timeout: time in msecs to wait for the message to complete before
119 * timing out (if 0 the wait is forever) 119 * timing out (if 0 the wait is forever)
120 * Context: !in_interrupt () 120 * Context: !in_interrupt ()
121 * 121 *
122 * This function sends a simple control message to a specified endpoint 122 * This function sends a simple control message to a specified endpoint
123 * and waits for the message to complete, or timeout. 123 * and waits for the message to complete, or timeout.
124 * 124 *
125 * If successful, it returns the number of bytes transferred, otherwise a negative error number. 125 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
126 * 126 *
127 * Don't use this function from within an interrupt context, like a 127 * Don't use this function from within an interrupt context, like a
128 * bottom half handler. If you need an asynchronous message, or need to send 128 * bottom half handler. If you need an asynchronous message, or need to send
129 * a message from within interrupt context, use usb_submit_urb() 129 * a message from within interrupt context, use usb_submit_urb()
130 * If a thread in your driver uses this call, make sure your disconnect() 130 * If a thread in your driver uses this call, make sure your disconnect()
131 * method can wait for it to complete. Since you don't have a handle on 131 * method can wait for it to complete. Since you don't have a handle on
132 * the URB used, you can't cancel the request. 132 * the URB used, you can't cancel the request.
133 */ 133 */
134 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype, 134 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
135 __u16 value, __u16 index, void *data, __u16 size, int timeout) 135 __u16 value, __u16 index, void *data, __u16 size, int timeout)
136 { 136 {
137 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); 137 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
138 int ret; 138 int ret;
139 139
140 if (!dr) 140 if (!dr)
141 return -ENOMEM; 141 return -ENOMEM;
142 142
143 dr->bRequestType= requesttype; 143 dr->bRequestType= requesttype;
144 dr->bRequest = request; 144 dr->bRequest = request;
145 dr->wValue = cpu_to_le16p(&value); 145 dr->wValue = cpu_to_le16p(&value);
146 dr->wIndex = cpu_to_le16p(&index); 146 dr->wIndex = cpu_to_le16p(&index);
147 dr->wLength = cpu_to_le16p(&size); 147 dr->wLength = cpu_to_le16p(&size);
148 148
149 //dbg("usb_control_msg"); 149 //dbg("usb_control_msg");
150 150
151 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout); 151 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
152 152
153 kfree(dr); 153 kfree(dr);
154 154
155 return ret; 155 return ret;
156 } 156 }
157 157
158 158
159 /** 159 /**
160 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion 160 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
161 * @usb_dev: pointer to the usb device to send the message to 161 * @usb_dev: pointer to the usb device to send the message to
162 * @pipe: endpoint "pipe" to send the message to 162 * @pipe: endpoint "pipe" to send the message to
163 * @data: pointer to the data to send 163 * @data: pointer to the data to send
164 * @len: length in bytes of the data to send 164 * @len: length in bytes of the data to send
165 * @actual_length: pointer to a location to put the actual length transferred in bytes 165 * @actual_length: pointer to a location to put the actual length transferred in bytes
166 * @timeout: time in msecs to wait for the message to complete before 166 * @timeout: time in msecs to wait for the message to complete before
167 * timing out (if 0 the wait is forever) 167 * timing out (if 0 the wait is forever)
168 * Context: !in_interrupt () 168 * Context: !in_interrupt ()
169 * 169 *
170 * This function sends a simple bulk message to a specified endpoint 170 * This function sends a simple bulk message to a specified endpoint
171 * and waits for the message to complete, or timeout. 171 * and waits for the message to complete, or timeout.
172 * 172 *
173 * If successful, it returns 0, otherwise a negative error number. 173 * If successful, it returns 0, otherwise a negative error number.
174 * The number of actual bytes transferred will be stored in the 174 * The number of actual bytes transferred will be stored in the
175 * actual_length paramater. 175 * actual_length paramater.
176 * 176 *
177 * Don't use this function from within an interrupt context, like a 177 * Don't use this function from within an interrupt context, like a
178 * bottom half handler. If you need an asynchronous message, or need to 178 * bottom half handler. If you need an asynchronous message, or need to
179 * send a message from within interrupt context, use usb_submit_urb() 179 * send a message from within interrupt context, use usb_submit_urb()
180 * If a thread in your driver uses this call, make sure your disconnect() 180 * If a thread in your driver uses this call, make sure your disconnect()
181 * method can wait for it to complete. Since you don't have a handle on 181 * method can wait for it to complete. Since you don't have a handle on
182 * the URB used, you can't cancel the request. 182 * the URB used, you can't cancel the request.
183 * 183 *
184 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT 184 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
185 * ioctl, users are forced to abuse this routine by using it to submit 185 * ioctl, users are forced to abuse this routine by using it to submit
186 * URBs for interrupt endpoints. We will take the liberty of creating 186 * URBs for interrupt endpoints. We will take the liberty of creating
187 * an interrupt URB (with the default interval) if the target is an 187 * an interrupt URB (with the default interval) if the target is an
188 * interrupt endpoint. 188 * interrupt endpoint.
189 */ 189 */
190 int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 190 int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
191 void *data, int len, int *actual_length, int timeout) 191 void *data, int len, int *actual_length, int timeout)
192 { 192 {
193 struct urb *urb; 193 struct urb *urb;
194 struct usb_host_endpoint *ep; 194 struct usb_host_endpoint *ep;
195 195
196 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out) 196 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
197 [usb_pipeendpoint(pipe)]; 197 [usb_pipeendpoint(pipe)];
198 if (!ep || len < 0) 198 if (!ep || len < 0)
199 return -EINVAL; 199 return -EINVAL;
200 200
201 urb = usb_alloc_urb(0, GFP_KERNEL); 201 urb = usb_alloc_urb(0, GFP_KERNEL);
202 if (!urb) 202 if (!urb)
203 return -ENOMEM; 203 return -ENOMEM;
204 204
205 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 205 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
206 USB_ENDPOINT_XFER_INT) { 206 USB_ENDPOINT_XFER_INT) {
207 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30); 207 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
208 usb_fill_int_urb(urb, usb_dev, pipe, data, len, 208 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
209 usb_api_blocking_completion, NULL, 209 usb_api_blocking_completion, NULL,
210 ep->desc.bInterval); 210 ep->desc.bInterval);
211 } else 211 } else
212 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, 212 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
213 usb_api_blocking_completion, NULL); 213 usb_api_blocking_completion, NULL);
214 214
215 return usb_start_wait_urb(urb, timeout, actual_length); 215 return usb_start_wait_urb(urb, timeout, actual_length);
216 } 216 }
217 217
218 /*-------------------------------------------------------------------*/ 218 /*-------------------------------------------------------------------*/
219 219
220 static void sg_clean (struct usb_sg_request *io) 220 static void sg_clean (struct usb_sg_request *io)
221 { 221 {
222 if (io->urbs) { 222 if (io->urbs) {
223 while (io->entries--) 223 while (io->entries--)
224 usb_free_urb (io->urbs [io->entries]); 224 usb_free_urb (io->urbs [io->entries]);
225 kfree (io->urbs); 225 kfree (io->urbs);
226 io->urbs = NULL; 226 io->urbs = NULL;
227 } 227 }
228 if (io->dev->dev.dma_mask != NULL) 228 if (io->dev->dev.dma_mask != NULL)
229 usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents); 229 usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
230 io->dev = NULL; 230 io->dev = NULL;
231 } 231 }
232 232
233 static void sg_complete (struct urb *urb, struct pt_regs *regs) 233 static void sg_complete (struct urb *urb, struct pt_regs *regs)
234 { 234 {
235 struct usb_sg_request *io = (struct usb_sg_request *) urb->context; 235 struct usb_sg_request *io = (struct usb_sg_request *) urb->context;
236 236
237 spin_lock (&io->lock); 237 spin_lock (&io->lock);
238 238
239 /* In 2.5 we require hcds' endpoint queues not to progress after fault 239 /* In 2.5 we require hcds' endpoint queues not to progress after fault
240 * reports, until the completion callback (this!) returns. That lets 240 * reports, until the completion callback (this!) returns. That lets
241 * device driver code (like this routine) unlink queued urbs first, 241 * device driver code (like this routine) unlink queued urbs first,
242 * if it needs to, since the HC won't work on them at all. So it's 242 * if it needs to, since the HC won't work on them at all. So it's
243 * not possible for page N+1 to overwrite page N, and so on. 243 * not possible for page N+1 to overwrite page N, and so on.
244 * 244 *
245 * That's only for "hard" faults; "soft" faults (unlinks) sometimes 245 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
246 * complete before the HCD can get requests away from hardware, 246 * complete before the HCD can get requests away from hardware,
247 * though never during cleanup after a hard fault. 247 * though never during cleanup after a hard fault.
248 */ 248 */
249 if (io->status 249 if (io->status
250 && (io->status != -ECONNRESET 250 && (io->status != -ECONNRESET
251 || urb->status != -ECONNRESET) 251 || urb->status != -ECONNRESET)
252 && urb->actual_length) { 252 && urb->actual_length) {
253 dev_err (io->dev->bus->controller, 253 dev_err (io->dev->bus->controller,
254 "dev %s ep%d%s scatterlist error %d/%d\n", 254 "dev %s ep%d%s scatterlist error %d/%d\n",
255 io->dev->devpath, 255 io->dev->devpath,
256 usb_pipeendpoint (urb->pipe), 256 usb_pipeendpoint (urb->pipe),
257 usb_pipein (urb->pipe) ? "in" : "out", 257 usb_pipein (urb->pipe) ? "in" : "out",
258 urb->status, io->status); 258 urb->status, io->status);
259 // BUG (); 259 // BUG ();
260 } 260 }
261 261
262 if (io->status == 0 && urb->status && urb->status != -ECONNRESET) { 262 if (io->status == 0 && urb->status && urb->status != -ECONNRESET) {
263 int i, found, status; 263 int i, found, status;
264 264
265 io->status = urb->status; 265 io->status = urb->status;
266 266
267 /* the previous urbs, and this one, completed already. 267 /* the previous urbs, and this one, completed already.
268 * unlink pending urbs so they won't rx/tx bad data. 268 * unlink pending urbs so they won't rx/tx bad data.
269 * careful: unlink can sometimes be synchronous... 269 * careful: unlink can sometimes be synchronous...
270 */ 270 */
271 spin_unlock (&io->lock); 271 spin_unlock (&io->lock);
272 for (i = 0, found = 0; i < io->entries; i++) { 272 for (i = 0, found = 0; i < io->entries; i++) {
273 if (!io->urbs [i] || !io->urbs [i]->dev) 273 if (!io->urbs [i] || !io->urbs [i]->dev)
274 continue; 274 continue;
275 if (found) { 275 if (found) {
276 status = usb_unlink_urb (io->urbs [i]); 276 status = usb_unlink_urb (io->urbs [i]);
277 if (status != -EINPROGRESS 277 if (status != -EINPROGRESS
278 && status != -ENODEV 278 && status != -ENODEV
279 && status != -EBUSY) 279 && status != -EBUSY)
280 dev_err (&io->dev->dev, 280 dev_err (&io->dev->dev,
281 "%s, unlink --> %d\n", 281 "%s, unlink --> %d\n",
282 __FUNCTION__, status); 282 __FUNCTION__, status);
283 } else if (urb == io->urbs [i]) 283 } else if (urb == io->urbs [i])
284 found = 1; 284 found = 1;
285 } 285 }
286 spin_lock (&io->lock); 286 spin_lock (&io->lock);
287 } 287 }
288 urb->dev = NULL; 288 urb->dev = NULL;
289 289
290 /* on the last completion, signal usb_sg_wait() */ 290 /* on the last completion, signal usb_sg_wait() */
291 io->bytes += urb->actual_length; 291 io->bytes += urb->actual_length;
292 io->count--; 292 io->count--;
293 if (!io->count) 293 if (!io->count)
294 complete (&io->complete); 294 complete (&io->complete);
295 295
296 spin_unlock (&io->lock); 296 spin_unlock (&io->lock);
297 } 297 }
298 298
299 299
300 /** 300 /**
301 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request 301 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
302 * @io: request block being initialized. until usb_sg_wait() returns, 302 * @io: request block being initialized. until usb_sg_wait() returns,
303 * treat this as a pointer to an opaque block of memory, 303 * treat this as a pointer to an opaque block of memory,
304 * @dev: the usb device that will send or receive the data 304 * @dev: the usb device that will send or receive the data
305 * @pipe: endpoint "pipe" used to transfer the data 305 * @pipe: endpoint "pipe" used to transfer the data
306 * @period: polling rate for interrupt endpoints, in frames or 306 * @period: polling rate for interrupt endpoints, in frames or
307 * (for high speed endpoints) microframes; ignored for bulk 307 * (for high speed endpoints) microframes; ignored for bulk
308 * @sg: scatterlist entries 308 * @sg: scatterlist entries
309 * @nents: how many entries in the scatterlist 309 * @nents: how many entries in the scatterlist
310 * @length: how many bytes to send from the scatterlist, or zero to 310 * @length: how many bytes to send from the scatterlist, or zero to
311 * send every byte identified in the list. 311 * send every byte identified in the list.
312 * @mem_flags: SLAB_* flags affecting memory allocations in this call 312 * @mem_flags: SLAB_* flags affecting memory allocations in this call
313 * 313 *
314 * Returns zero for success, else a negative errno value. This initializes a 314 * Returns zero for success, else a negative errno value. This initializes a
315 * scatter/gather request, allocating resources such as I/O mappings and urb 315 * scatter/gather request, allocating resources such as I/O mappings and urb
316 * memory (except maybe memory used by USB controller drivers). 316 * memory (except maybe memory used by USB controller drivers).
317 * 317 *
318 * The request must be issued using usb_sg_wait(), which waits for the I/O to 318 * The request must be issued using usb_sg_wait(), which waits for the I/O to
319 * complete (or to be canceled) and then cleans up all resources allocated by 319 * complete (or to be canceled) and then cleans up all resources allocated by
320 * usb_sg_init(). 320 * usb_sg_init().
321 * 321 *
322 * The request may be canceled with usb_sg_cancel(), either before or after 322 * The request may be canceled with usb_sg_cancel(), either before or after
323 * usb_sg_wait() is called. 323 * usb_sg_wait() is called.
324 */ 324 */
325 int usb_sg_init ( 325 int usb_sg_init (
326 struct usb_sg_request *io, 326 struct usb_sg_request *io,
327 struct usb_device *dev, 327 struct usb_device *dev,
328 unsigned pipe, 328 unsigned pipe,
329 unsigned period, 329 unsigned period,
330 struct scatterlist *sg, 330 struct scatterlist *sg,
331 int nents, 331 int nents,
332 size_t length, 332 size_t length,
333 gfp_t mem_flags 333 gfp_t mem_flags
334 ) 334 )
335 { 335 {
336 int i; 336 int i;
337 int urb_flags; 337 int urb_flags;
338 int dma; 338 int dma;
339 339
340 if (!io || !dev || !sg 340 if (!io || !dev || !sg
341 || usb_pipecontrol (pipe) 341 || usb_pipecontrol (pipe)
342 || usb_pipeisoc (pipe) 342 || usb_pipeisoc (pipe)
343 || nents <= 0) 343 || nents <= 0)
344 return -EINVAL; 344 return -EINVAL;
345 345
346 spin_lock_init (&io->lock); 346 spin_lock_init (&io->lock);
347 io->dev = dev; 347 io->dev = dev;
348 io->pipe = pipe; 348 io->pipe = pipe;
349 io->sg = sg; 349 io->sg = sg;
350 io->nents = nents; 350 io->nents = nents;
351 351
352 /* not all host controllers use DMA (like the mainstream pci ones); 352 /* not all host controllers use DMA (like the mainstream pci ones);
353 * they can use PIO (sl811) or be software over another transport. 353 * they can use PIO (sl811) or be software over another transport.
354 */ 354 */
355 dma = (dev->dev.dma_mask != NULL); 355 dma = (dev->dev.dma_mask != NULL);
356 if (dma) 356 if (dma)
357 io->entries = usb_buffer_map_sg (dev, pipe, sg, nents); 357 io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
358 else 358 else
359 io->entries = nents; 359 io->entries = nents;
360 360
361 /* initialize all the urbs we'll use */ 361 /* initialize all the urbs we'll use */
362 if (io->entries <= 0) 362 if (io->entries <= 0)
363 return io->entries; 363 return io->entries;
364 364
365 io->count = io->entries; 365 io->count = io->entries;
366 io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags); 366 io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
367 if (!io->urbs) 367 if (!io->urbs)
368 goto nomem; 368 goto nomem;
369 369
370 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT; 370 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
371 if (usb_pipein (pipe)) 371 if (usb_pipein (pipe))
372 urb_flags |= URB_SHORT_NOT_OK; 372 urb_flags |= URB_SHORT_NOT_OK;
373 373
374 for (i = 0; i < io->entries; i++) { 374 for (i = 0; i < io->entries; i++) {
375 unsigned len; 375 unsigned len;
376 376
377 io->urbs [i] = usb_alloc_urb (0, mem_flags); 377 io->urbs [i] = usb_alloc_urb (0, mem_flags);
378 if (!io->urbs [i]) { 378 if (!io->urbs [i]) {
379 io->entries = i; 379 io->entries = i;
380 goto nomem; 380 goto nomem;
381 } 381 }
382 382
383 io->urbs [i]->dev = NULL; 383 io->urbs [i]->dev = NULL;
384 io->urbs [i]->pipe = pipe; 384 io->urbs [i]->pipe = pipe;
385 io->urbs [i]->interval = period; 385 io->urbs [i]->interval = period;
386 io->urbs [i]->transfer_flags = urb_flags; 386 io->urbs [i]->transfer_flags = urb_flags;
387 387
388 io->urbs [i]->complete = sg_complete; 388 io->urbs [i]->complete = sg_complete;
389 io->urbs [i]->context = io; 389 io->urbs [i]->context = io;
390 io->urbs [i]->status = -EINPROGRESS; 390 io->urbs [i]->status = -EINPROGRESS;
391 io->urbs [i]->actual_length = 0; 391 io->urbs [i]->actual_length = 0;
392 392
393 if (dma) { 393 if (dma) {
394 /* hc may use _only_ transfer_dma */ 394 /* hc may use _only_ transfer_dma */
395 io->urbs [i]->transfer_dma = sg_dma_address (sg + i); 395 io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
396 len = sg_dma_len (sg + i); 396 len = sg_dma_len (sg + i);
397 } else { 397 } else {
398 /* hc may use _only_ transfer_buffer */ 398 /* hc may use _only_ transfer_buffer */
399 io->urbs [i]->transfer_buffer = 399 io->urbs [i]->transfer_buffer =
400 page_address (sg [i].page) + sg [i].offset; 400 page_address (sg [i].page) + sg [i].offset;
401 len = sg [i].length; 401 len = sg [i].length;
402 } 402 }
403 403
404 if (length) { 404 if (length) {
405 len = min_t (unsigned, len, length); 405 len = min_t (unsigned, len, length);
406 length -= len; 406 length -= len;
407 if (length == 0) 407 if (length == 0)
408 io->entries = i + 1; 408 io->entries = i + 1;
409 } 409 }
410 io->urbs [i]->transfer_buffer_length = len; 410 io->urbs [i]->transfer_buffer_length = len;
411 } 411 }
412 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT; 412 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
413 413
414 /* transaction state */ 414 /* transaction state */
415 io->status = 0; 415 io->status = 0;
416 io->bytes = 0; 416 io->bytes = 0;
417 init_completion (&io->complete); 417 init_completion (&io->complete);
418 return 0; 418 return 0;
419 419
420 nomem: 420 nomem:
421 sg_clean (io); 421 sg_clean (io);
422 return -ENOMEM; 422 return -ENOMEM;
423 } 423 }
424 424
425 425
426 /** 426 /**
427 * usb_sg_wait - synchronously execute scatter/gather request 427 * usb_sg_wait - synchronously execute scatter/gather request
428 * @io: request block handle, as initialized with usb_sg_init(). 428 * @io: request block handle, as initialized with usb_sg_init().
429 * some fields become accessible when this call returns. 429 * some fields become accessible when this call returns.
430 * Context: !in_interrupt () 430 * Context: !in_interrupt ()
431 * 431 *
432 * This function blocks until the specified I/O operation completes. It 432 * This function blocks until the specified I/O operation completes. It
433 * leverages the grouping of the related I/O requests to get good transfer 433 * leverages the grouping of the related I/O requests to get good transfer
434 * rates, by queueing the requests. At higher speeds, such queuing can 434 * rates, by queueing the requests. At higher speeds, such queuing can
435 * significantly improve USB throughput. 435 * significantly improve USB throughput.
436 * 436 *
437 * There are three kinds of completion for this function. 437 * There are three kinds of completion for this function.
438 * (1) success, where io->status is zero. The number of io->bytes 438 * (1) success, where io->status is zero. The number of io->bytes
439 * transferred is as requested. 439 * transferred is as requested.
440 * (2) error, where io->status is a negative errno value. The number 440 * (2) error, where io->status is a negative errno value. The number
441 * of io->bytes transferred before the error is usually less 441 * of io->bytes transferred before the error is usually less
442 * than requested, and can be nonzero. 442 * than requested, and can be nonzero.
443 * (3) cancellation, a type of error with status -ECONNRESET that 443 * (3) cancellation, a type of error with status -ECONNRESET that
444 * is initiated by usb_sg_cancel(). 444 * is initiated by usb_sg_cancel().
445 * 445 *
446 * When this function returns, all memory allocated through usb_sg_init() or 446 * When this function returns, all memory allocated through usb_sg_init() or
447 * this call will have been freed. The request block parameter may still be 447 * this call will have been freed. The request block parameter may still be
448 * passed to usb_sg_cancel(), or it may be freed. It could also be 448 * passed to usb_sg_cancel(), or it may be freed. It could also be
449 * reinitialized and then reused. 449 * reinitialized and then reused.
450 * 450 *
451 * Data Transfer Rates: 451 * Data Transfer Rates:
452 * 452 *
453 * Bulk transfers are valid for full or high speed endpoints. 453 * Bulk transfers are valid for full or high speed endpoints.
454 * The best full speed data rate is 19 packets of 64 bytes each 454 * The best full speed data rate is 19 packets of 64 bytes each
455 * per frame, or 1216 bytes per millisecond. 455 * per frame, or 1216 bytes per millisecond.
456 * The best high speed data rate is 13 packets of 512 bytes each 456 * The best high speed data rate is 13 packets of 512 bytes each
457 * per microframe, or 52 KBytes per millisecond. 457 * per microframe, or 52 KBytes per millisecond.
458 * 458 *
459 * The reason to use interrupt transfers through this API would most likely 459 * The reason to use interrupt transfers through this API would most likely
460 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond 460 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
461 * could be transferred. That capability is less useful for low or full 461 * could be transferred. That capability is less useful for low or full
462 * speed interrupt endpoints, which allow at most one packet per millisecond, 462 * speed interrupt endpoints, which allow at most one packet per millisecond,
463 * of at most 8 or 64 bytes (respectively). 463 * of at most 8 or 64 bytes (respectively).
464 */ 464 */
465 void usb_sg_wait (struct usb_sg_request *io) 465 void usb_sg_wait (struct usb_sg_request *io)
466 { 466 {
467 int i, entries = io->entries; 467 int i, entries = io->entries;
468 468
469 /* queue the urbs. */ 469 /* queue the urbs. */
470 spin_lock_irq (&io->lock); 470 spin_lock_irq (&io->lock);
471 for (i = 0; i < entries && !io->status; i++) { 471 for (i = 0; i < entries && !io->status; i++) {
472 int retval; 472 int retval;
473 473
474 io->urbs [i]->dev = io->dev; 474 io->urbs [i]->dev = io->dev;
475 retval = usb_submit_urb (io->urbs [i], SLAB_ATOMIC); 475 retval = usb_submit_urb (io->urbs [i], SLAB_ATOMIC);
476 476
477 /* after we submit, let completions or cancelations fire; 477 /* after we submit, let completions or cancelations fire;
478 * we handshake using io->status. 478 * we handshake using io->status.
479 */ 479 */
480 spin_unlock_irq (&io->lock); 480 spin_unlock_irq (&io->lock);
481 switch (retval) { 481 switch (retval) {
482 /* maybe we retrying will recover */ 482 /* maybe we retrying will recover */
483 case -ENXIO: // hc didn't queue this one 483 case -ENXIO: // hc didn't queue this one
484 case -EAGAIN: 484 case -EAGAIN:
485 case -ENOMEM: 485 case -ENOMEM:
486 io->urbs[i]->dev = NULL; 486 io->urbs[i]->dev = NULL;
487 retval = 0; 487 retval = 0;
488 i--; 488 i--;
489 yield (); 489 yield ();
490 break; 490 break;
491 491
492 /* no error? continue immediately. 492 /* no error? continue immediately.
493 * 493 *
494 * NOTE: to work better with UHCI (4K I/O buffer may 494 * NOTE: to work better with UHCI (4K I/O buffer may
495 * need 3K of TDs) it may be good to limit how many 495 * need 3K of TDs) it may be good to limit how many
496 * URBs are queued at once; N milliseconds? 496 * URBs are queued at once; N milliseconds?
497 */ 497 */
498 case 0: 498 case 0:
499 cpu_relax (); 499 cpu_relax ();
500 break; 500 break;
501 501
502 /* fail any uncompleted urbs */ 502 /* fail any uncompleted urbs */
503 default: 503 default:
504 io->urbs [i]->dev = NULL; 504 io->urbs [i]->dev = NULL;
505 io->urbs [i]->status = retval; 505 io->urbs [i]->status = retval;
506 dev_dbg (&io->dev->dev, "%s, submit --> %d\n", 506 dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
507 __FUNCTION__, retval); 507 __FUNCTION__, retval);
508 usb_sg_cancel (io); 508 usb_sg_cancel (io);
509 } 509 }
510 spin_lock_irq (&io->lock); 510 spin_lock_irq (&io->lock);
511 if (retval && (io->status == 0 || io->status == -ECONNRESET)) 511 if (retval && (io->status == 0 || io->status == -ECONNRESET))
512 io->status = retval; 512 io->status = retval;
513 } 513 }
514 io->count -= entries - i; 514 io->count -= entries - i;
515 if (io->count == 0) 515 if (io->count == 0)
516 complete (&io->complete); 516 complete (&io->complete);
517 spin_unlock_irq (&io->lock); 517 spin_unlock_irq (&io->lock);
518 518
519 /* OK, yes, this could be packaged as non-blocking. 519 /* OK, yes, this could be packaged as non-blocking.
520 * So could the submit loop above ... but it's easier to 520 * So could the submit loop above ... but it's easier to
521 * solve neither problem than to solve both! 521 * solve neither problem than to solve both!
522 */ 522 */
523 wait_for_completion (&io->complete); 523 wait_for_completion (&io->complete);
524 524
525 sg_clean (io); 525 sg_clean (io);
526 } 526 }
527 527
528 /** 528 /**
529 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait() 529 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
530 * @io: request block, initialized with usb_sg_init() 530 * @io: request block, initialized with usb_sg_init()
531 * 531 *
532 * This stops a request after it has been started by usb_sg_wait(). 532 * This stops a request after it has been started by usb_sg_wait().
533 * It can also prevents one initialized by usb_sg_init() from starting, 533 * It can also prevents one initialized by usb_sg_init() from starting,
534 * so that call just frees resources allocated to the request. 534 * so that call just frees resources allocated to the request.
535 */ 535 */
536 void usb_sg_cancel (struct usb_sg_request *io) 536 void usb_sg_cancel (struct usb_sg_request *io)
537 { 537 {
538 unsigned long flags; 538 unsigned long flags;
539 539
540 spin_lock_irqsave (&io->lock, flags); 540 spin_lock_irqsave (&io->lock, flags);
541 541
542 /* shut everything down, if it didn't already */ 542 /* shut everything down, if it didn't already */
543 if (!io->status) { 543 if (!io->status) {
544 int i; 544 int i;
545 545
546 io->status = -ECONNRESET; 546 io->status = -ECONNRESET;
547 spin_unlock (&io->lock); 547 spin_unlock (&io->lock);
548 for (i = 0; i < io->entries; i++) { 548 for (i = 0; i < io->entries; i++) {
549 int retval; 549 int retval;
550 550
551 if (!io->urbs [i]->dev) 551 if (!io->urbs [i]->dev)
552 continue; 552 continue;
553 retval = usb_unlink_urb (io->urbs [i]); 553 retval = usb_unlink_urb (io->urbs [i]);
554 if (retval != -EINPROGRESS && retval != -EBUSY) 554 if (retval != -EINPROGRESS && retval != -EBUSY)
555 dev_warn (&io->dev->dev, "%s, unlink --> %d\n", 555 dev_warn (&io->dev->dev, "%s, unlink --> %d\n",
556 __FUNCTION__, retval); 556 __FUNCTION__, retval);
557 } 557 }
558 spin_lock (&io->lock); 558 spin_lock (&io->lock);
559 } 559 }
560 spin_unlock_irqrestore (&io->lock, flags); 560 spin_unlock_irqrestore (&io->lock, flags);
561 } 561 }
562 562
563 /*-------------------------------------------------------------------*/ 563 /*-------------------------------------------------------------------*/
564 564
565 /** 565 /**
566 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request 566 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
567 * @dev: the device whose descriptor is being retrieved 567 * @dev: the device whose descriptor is being retrieved
568 * @type: the descriptor type (USB_DT_*) 568 * @type: the descriptor type (USB_DT_*)
569 * @index: the number of the descriptor 569 * @index: the number of the descriptor
570 * @buf: where to put the descriptor 570 * @buf: where to put the descriptor
571 * @size: how big is "buf"? 571 * @size: how big is "buf"?
572 * Context: !in_interrupt () 572 * Context: !in_interrupt ()
573 * 573 *
574 * Gets a USB descriptor. Convenience functions exist to simplify 574 * Gets a USB descriptor. Convenience functions exist to simplify
575 * getting some types of descriptors. Use 575 * getting some types of descriptors. Use
576 * usb_get_string() or usb_string() for USB_DT_STRING. 576 * usb_get_string() or usb_string() for USB_DT_STRING.
577 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG) 577 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
578 * are part of the device structure. 578 * are part of the device structure.
579 * In addition to a number of USB-standard descriptors, some 579 * In addition to a number of USB-standard descriptors, some
580 * devices also use class-specific or vendor-specific descriptors. 580 * devices also use class-specific or vendor-specific descriptors.
581 * 581 *
582 * This call is synchronous, and may not be used in an interrupt context. 582 * This call is synchronous, and may not be used in an interrupt context.
583 * 583 *
584 * Returns the number of bytes received on success, or else the status code 584 * Returns the number of bytes received on success, or else the status code
585 * returned by the underlying usb_control_msg() call. 585 * returned by the underlying usb_control_msg() call.
586 */ 586 */
587 int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size) 587 int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
588 { 588 {
589 int i; 589 int i;
590 int result; 590 int result;
591 591
592 memset(buf,0,size); // Make sure we parse really received data 592 memset(buf,0,size); // Make sure we parse really received data
593 593
594 for (i = 0; i < 3; ++i) { 594 for (i = 0; i < 3; ++i) {
595 /* retry on length 0 or stall; some devices are flakey */ 595 /* retry on length 0 or stall; some devices are flakey */
596 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 596 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
597 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 597 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
598 (type << 8) + index, 0, buf, size, 598 (type << 8) + index, 0, buf, size,
599 USB_CTRL_GET_TIMEOUT); 599 USB_CTRL_GET_TIMEOUT);
600 if (result == 0 || result == -EPIPE) 600 if (result == 0 || result == -EPIPE)
601 continue; 601 continue;
602 if (result > 1 && ((u8 *)buf)[1] != type) { 602 if (result > 1 && ((u8 *)buf)[1] != type) {
603 result = -EPROTO; 603 result = -EPROTO;
604 continue; 604 continue;
605 } 605 }
606 break; 606 break;
607 } 607 }
608 return result; 608 return result;
609 } 609 }
610 610
611 /** 611 /**
612 * usb_get_string - gets a string descriptor 612 * usb_get_string - gets a string descriptor
613 * @dev: the device whose string descriptor is being retrieved 613 * @dev: the device whose string descriptor is being retrieved
614 * @langid: code for language chosen (from string descriptor zero) 614 * @langid: code for language chosen (from string descriptor zero)
615 * @index: the number of the descriptor 615 * @index: the number of the descriptor
616 * @buf: where to put the string 616 * @buf: where to put the string
617 * @size: how big is "buf"? 617 * @size: how big is "buf"?
618 * Context: !in_interrupt () 618 * Context: !in_interrupt ()
619 * 619 *
620 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character, 620 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
621 * in little-endian byte order). 621 * in little-endian byte order).
622 * The usb_string() function will often be a convenient way to turn 622 * The usb_string() function will often be a convenient way to turn
623 * these strings into kernel-printable form. 623 * these strings into kernel-printable form.
624 * 624 *
625 * Strings may be referenced in device, configuration, interface, or other 625 * Strings may be referenced in device, configuration, interface, or other
626 * descriptors, and could also be used in vendor-specific ways. 626 * descriptors, and could also be used in vendor-specific ways.
627 * 627 *
628 * This call is synchronous, and may not be used in an interrupt context. 628 * This call is synchronous, and may not be used in an interrupt context.
629 * 629 *
630 * Returns the number of bytes received on success, or else the status code 630 * Returns the number of bytes received on success, or else the status code
631 * returned by the underlying usb_control_msg() call. 631 * returned by the underlying usb_control_msg() call.
632 */ 632 */
633 int usb_get_string(struct usb_device *dev, unsigned short langid, 633 int usb_get_string(struct usb_device *dev, unsigned short langid,
634 unsigned char index, void *buf, int size) 634 unsigned char index, void *buf, int size)
635 { 635 {
636 int i; 636 int i;
637 int result; 637 int result;
638 638
639 for (i = 0; i < 3; ++i) { 639 for (i = 0; i < 3; ++i) {
640 /* retry on length 0 or stall; some devices are flakey */ 640 /* retry on length 0 or stall; some devices are flakey */
641 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 641 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
642 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 642 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
643 (USB_DT_STRING << 8) + index, langid, buf, size, 643 (USB_DT_STRING << 8) + index, langid, buf, size,
644 USB_CTRL_GET_TIMEOUT); 644 USB_CTRL_GET_TIMEOUT);
645 if (!(result == 0 || result == -EPIPE)) 645 if (!(result == 0 || result == -EPIPE))
646 break; 646 break;
647 } 647 }
648 return result; 648 return result;
649 } 649 }
650 650
651 static void usb_try_string_workarounds(unsigned char *buf, int *length) 651 static void usb_try_string_workarounds(unsigned char *buf, int *length)
652 { 652 {
653 int newlength, oldlength = *length; 653 int newlength, oldlength = *length;
654 654
655 for (newlength = 2; newlength + 1 < oldlength; newlength += 2) 655 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
656 if (!isprint(buf[newlength]) || buf[newlength + 1]) 656 if (!isprint(buf[newlength]) || buf[newlength + 1])
657 break; 657 break;
658 658
659 if (newlength > 2) { 659 if (newlength > 2) {
660 buf[0] = newlength; 660 buf[0] = newlength;
661 *length = newlength; 661 *length = newlength;
662 } 662 }
663 } 663 }
664 664
665 static int usb_string_sub(struct usb_device *dev, unsigned int langid, 665 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
666 unsigned int index, unsigned char *buf) 666 unsigned int index, unsigned char *buf)
667 { 667 {
668 int rc; 668 int rc;
669 669
670 /* Try to read the string descriptor by asking for the maximum 670 /* Try to read the string descriptor by asking for the maximum
671 * possible number of bytes */ 671 * possible number of bytes */
672 rc = usb_get_string(dev, langid, index, buf, 255); 672 rc = usb_get_string(dev, langid, index, buf, 255);
673 673
674 /* If that failed try to read the descriptor length, then 674 /* If that failed try to read the descriptor length, then
675 * ask for just that many bytes */ 675 * ask for just that many bytes */
676 if (rc < 2) { 676 if (rc < 2) {
677 rc = usb_get_string(dev, langid, index, buf, 2); 677 rc = usb_get_string(dev, langid, index, buf, 2);
678 if (rc == 2) 678 if (rc == 2)
679 rc = usb_get_string(dev, langid, index, buf, buf[0]); 679 rc = usb_get_string(dev, langid, index, buf, buf[0]);
680 } 680 }
681 681
682 if (rc >= 2) { 682 if (rc >= 2) {
683 if (!buf[0] && !buf[1]) 683 if (!buf[0] && !buf[1])
684 usb_try_string_workarounds(buf, &rc); 684 usb_try_string_workarounds(buf, &rc);
685 685
686 /* There might be extra junk at the end of the descriptor */ 686 /* There might be extra junk at the end of the descriptor */
687 if (buf[0] < rc) 687 if (buf[0] < rc)
688 rc = buf[0]; 688 rc = buf[0];
689 689
690 rc = rc - (rc & 1); /* force a multiple of two */ 690 rc = rc - (rc & 1); /* force a multiple of two */
691 } 691 }
692 692
693 if (rc < 2) 693 if (rc < 2)
694 rc = (rc < 0 ? rc : -EINVAL); 694 rc = (rc < 0 ? rc : -EINVAL);
695 695
696 return rc; 696 return rc;
697 } 697 }
698 698
699 /** 699 /**
700 * usb_string - returns ISO 8859-1 version of a string descriptor 700 * usb_string - returns ISO 8859-1 version of a string descriptor
701 * @dev: the device whose string descriptor is being retrieved 701 * @dev: the device whose string descriptor is being retrieved
702 * @index: the number of the descriptor 702 * @index: the number of the descriptor
703 * @buf: where to put the string 703 * @buf: where to put the string
704 * @size: how big is "buf"? 704 * @size: how big is "buf"?
705 * Context: !in_interrupt () 705 * Context: !in_interrupt ()
706 * 706 *
707 * This converts the UTF-16LE encoded strings returned by devices, from 707 * This converts the UTF-16LE encoded strings returned by devices, from
708 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones 708 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
709 * that are more usable in most kernel contexts. Note that all characters 709 * that are more usable in most kernel contexts. Note that all characters
710 * in the chosen descriptor that can't be encoded using ISO-8859-1 710 * in the chosen descriptor that can't be encoded using ISO-8859-1
711 * are converted to the question mark ("?") character, and this function 711 * are converted to the question mark ("?") character, and this function
712 * chooses strings in the first language supported by the device. 712 * chooses strings in the first language supported by the device.
713 * 713 *
714 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit 714 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
715 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode, 715 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
716 * and is appropriate for use many uses of English and several other 716 * and is appropriate for use many uses of English and several other
717 * Western European languages. (But it doesn't include the "Euro" symbol.) 717 * Western European languages. (But it doesn't include the "Euro" symbol.)
718 * 718 *
719 * This call is synchronous, and may not be used in an interrupt context. 719 * This call is synchronous, and may not be used in an interrupt context.
720 * 720 *
721 * Returns length of the string (>= 0) or usb_control_msg status (< 0). 721 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
722 */ 722 */
723 int usb_string(struct usb_device *dev, int index, char *buf, size_t size) 723 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
724 { 724 {
725 unsigned char *tbuf; 725 unsigned char *tbuf;
726 int err; 726 int err;
727 unsigned int u, idx; 727 unsigned int u, idx;
728 728
729 if (dev->state == USB_STATE_SUSPENDED) 729 if (dev->state == USB_STATE_SUSPENDED)
730 return -EHOSTUNREACH; 730 return -EHOSTUNREACH;
731 if (size <= 0 || !buf || !index) 731 if (size <= 0 || !buf || !index)
732 return -EINVAL; 732 return -EINVAL;
733 buf[0] = 0; 733 buf[0] = 0;
734 tbuf = kmalloc(256, GFP_KERNEL); 734 tbuf = kmalloc(256, GFP_KERNEL);
735 if (!tbuf) 735 if (!tbuf)
736 return -ENOMEM; 736 return -ENOMEM;
737 737
738 /* get langid for strings if it's not yet known */ 738 /* get langid for strings if it's not yet known */
739 if (!dev->have_langid) { 739 if (!dev->have_langid) {
740 err = usb_string_sub(dev, 0, 0, tbuf); 740 err = usb_string_sub(dev, 0, 0, tbuf);
741 if (err < 0) { 741 if (err < 0) {
742 dev_err (&dev->dev, 742 dev_err (&dev->dev,
743 "string descriptor 0 read error: %d\n", 743 "string descriptor 0 read error: %d\n",
744 err); 744 err);
745 goto errout; 745 goto errout;
746 } else if (err < 4) { 746 } else if (err < 4) {
747 dev_err (&dev->dev, "string descriptor 0 too short\n"); 747 dev_err (&dev->dev, "string descriptor 0 too short\n");
748 err = -EINVAL; 748 err = -EINVAL;
749 goto errout; 749 goto errout;
750 } else { 750 } else {
751 dev->have_langid = -1; 751 dev->have_langid = -1;
752 dev->string_langid = tbuf[2] | (tbuf[3]<< 8); 752 dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
753 /* always use the first langid listed */ 753 /* always use the first langid listed */
754 dev_dbg (&dev->dev, "default language 0x%04x\n", 754 dev_dbg (&dev->dev, "default language 0x%04x\n",
755 dev->string_langid); 755 dev->string_langid);
756 } 756 }
757 } 757 }
758 758
759 err = usb_string_sub(dev, dev->string_langid, index, tbuf); 759 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
760 if (err < 0) 760 if (err < 0)
761 goto errout; 761 goto errout;
762 762
763 size--; /* leave room for trailing NULL char in output buffer */ 763 size--; /* leave room for trailing NULL char in output buffer */
764 for (idx = 0, u = 2; u < err; u += 2) { 764 for (idx = 0, u = 2; u < err; u += 2) {
765 if (idx >= size) 765 if (idx >= size)
766 break; 766 break;
767 if (tbuf[u+1]) /* high byte */ 767 if (tbuf[u+1]) /* high byte */
768 buf[idx++] = '?'; /* non ISO-8859-1 character */ 768 buf[idx++] = '?'; /* non ISO-8859-1 character */
769 else 769 else
770 buf[idx++] = tbuf[u]; 770 buf[idx++] = tbuf[u];
771 } 771 }
772 buf[idx] = 0; 772 buf[idx] = 0;
773 err = idx; 773 err = idx;
774 774
775 if (tbuf[1] != USB_DT_STRING) 775 if (tbuf[1] != USB_DT_STRING)
776 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf); 776 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);
777 777
778 errout: 778 errout:
779 kfree(tbuf); 779 kfree(tbuf);
780 return err; 780 return err;
781 } 781 }
782 782
783 /** 783 /**
784 * usb_cache_string - read a string descriptor and cache it for later use 784 * usb_cache_string - read a string descriptor and cache it for later use
785 * @udev: the device whose string descriptor is being read 785 * @udev: the device whose string descriptor is being read
786 * @index: the descriptor index 786 * @index: the descriptor index
787 * 787 *
788 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string, 788 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
789 * or NULL if the index is 0 or the string could not be read. 789 * or NULL if the index is 0 or the string could not be read.
790 */ 790 */
791 char *usb_cache_string(struct usb_device *udev, int index) 791 char *usb_cache_string(struct usb_device *udev, int index)
792 { 792 {
793 char *buf; 793 char *buf;
794 char *smallbuf = NULL; 794 char *smallbuf = NULL;
795 int len; 795 int len;
796 796
797 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) { 797 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {
798 if ((len = usb_string(udev, index, buf, 256)) > 0) { 798 if ((len = usb_string(udev, index, buf, 256)) > 0) {
799 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL) 799 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)
800 return buf; 800 return buf;
801 memcpy(smallbuf, buf, len); 801 memcpy(smallbuf, buf, len);
802 } 802 }
803 kfree(buf); 803 kfree(buf);
804 } 804 }
805 return smallbuf; 805 return smallbuf;
806 } 806 }
807 807
808 /* 808 /*
809 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore) 809 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
810 * @dev: the device whose device descriptor is being updated 810 * @dev: the device whose device descriptor is being updated
811 * @size: how much of the descriptor to read 811 * @size: how much of the descriptor to read
812 * Context: !in_interrupt () 812 * Context: !in_interrupt ()
813 * 813 *
814 * Updates the copy of the device descriptor stored in the device structure, 814 * Updates the copy of the device descriptor stored in the device structure,
815 * which dedicates space for this purpose. Note that several fields are 815 * which dedicates space for this purpose. Note that several fields are
816 * converted to the host CPU's byte order: the USB version (bcdUSB), and 816 * converted to the host CPU's byte order: the USB version (bcdUSB), and
817 * vendors product and version fields (idVendor, idProduct, and bcdDevice). 817 * vendors product and version fields (idVendor, idProduct, and bcdDevice).
818 * That lets device drivers compare against non-byteswapped constants. 818 * That lets device drivers compare against non-byteswapped constants.
819 * 819 *
820 * Not exported, only for use by the core. If drivers really want to read 820 * Not exported, only for use by the core. If drivers really want to read
821 * the device descriptor directly, they can call usb_get_descriptor() with 821 * the device descriptor directly, they can call usb_get_descriptor() with
822 * type = USB_DT_DEVICE and index = 0. 822 * type = USB_DT_DEVICE and index = 0.
823 * 823 *
824 * This call is synchronous, and may not be used in an interrupt context. 824 * This call is synchronous, and may not be used in an interrupt context.
825 * 825 *
826 * Returns the number of bytes received on success, or else the status code 826 * Returns the number of bytes received on success, or else the status code
827 * returned by the underlying usb_control_msg() call. 827 * returned by the underlying usb_control_msg() call.
828 */ 828 */
829 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size) 829 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
830 { 830 {
831 struct usb_device_descriptor *desc; 831 struct usb_device_descriptor *desc;
832 int ret; 832 int ret;
833 833
834 if (size > sizeof(*desc)) 834 if (size > sizeof(*desc))
835 return -EINVAL; 835 return -EINVAL;
836 desc = kmalloc(sizeof(*desc), GFP_NOIO); 836 desc = kmalloc(sizeof(*desc), GFP_NOIO);
837 if (!desc) 837 if (!desc)
838 return -ENOMEM; 838 return -ENOMEM;
839 839
840 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size); 840 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
841 if (ret >= 0) 841 if (ret >= 0)
842 memcpy(&dev->descriptor, desc, size); 842 memcpy(&dev->descriptor, desc, size);
843 kfree(desc); 843 kfree(desc);
844 return ret; 844 return ret;
845 } 845 }
846 846
847 /** 847 /**
848 * usb_get_status - issues a GET_STATUS call 848 * usb_get_status - issues a GET_STATUS call
849 * @dev: the device whose status is being checked 849 * @dev: the device whose status is being checked
850 * @type: USB_RECIP_*; for device, interface, or endpoint 850 * @type: USB_RECIP_*; for device, interface, or endpoint
851 * @target: zero (for device), else interface or endpoint number 851 * @target: zero (for device), else interface or endpoint number
852 * @data: pointer to two bytes of bitmap data 852 * @data: pointer to two bytes of bitmap data
853 * Context: !in_interrupt () 853 * Context: !in_interrupt ()
854 * 854 *
855 * Returns device, interface, or endpoint status. Normally only of 855 * Returns device, interface, or endpoint status. Normally only of
856 * interest to see if the device is self powered, or has enabled the 856 * interest to see if the device is self powered, or has enabled the
857 * remote wakeup facility; or whether a bulk or interrupt endpoint 857 * remote wakeup facility; or whether a bulk or interrupt endpoint
858 * is halted ("stalled"). 858 * is halted ("stalled").
859 * 859 *
860 * Bits in these status bitmaps are set using the SET_FEATURE request, 860 * Bits in these status bitmaps are set using the SET_FEATURE request,
861 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt() 861 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
862 * function should be used to clear halt ("stall") status. 862 * function should be used to clear halt ("stall") status.
863 * 863 *
864 * This call is synchronous, and may not be used in an interrupt context. 864 * This call is synchronous, and may not be used in an interrupt context.
865 * 865 *
866 * Returns the number of bytes received on success, or else the status code 866 * Returns the number of bytes received on success, or else the status code
867 * returned by the underlying usb_control_msg() call. 867 * returned by the underlying usb_control_msg() call.
868 */ 868 */
869 int usb_get_status(struct usb_device *dev, int type, int target, void *data) 869 int usb_get_status(struct usb_device *dev, int type, int target, void *data)
870 { 870 {
871 int ret; 871 int ret;
872 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL); 872 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
873 873
874 if (!status) 874 if (!status)
875 return -ENOMEM; 875 return -ENOMEM;
876 876
877 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 877 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
878 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status, 878 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
879 sizeof(*status), USB_CTRL_GET_TIMEOUT); 879 sizeof(*status), USB_CTRL_GET_TIMEOUT);
880 880
881 *(u16 *)data = *status; 881 *(u16 *)data = *status;
882 kfree(status); 882 kfree(status);
883 return ret; 883 return ret;
884 } 884 }
885 885
886 /** 886 /**
887 * usb_clear_halt - tells device to clear endpoint halt/stall condition 887 * usb_clear_halt - tells device to clear endpoint halt/stall condition
888 * @dev: device whose endpoint is halted 888 * @dev: device whose endpoint is halted
889 * @pipe: endpoint "pipe" being cleared 889 * @pipe: endpoint "pipe" being cleared
890 * Context: !in_interrupt () 890 * Context: !in_interrupt ()
891 * 891 *
892 * This is used to clear halt conditions for bulk and interrupt endpoints, 892 * This is used to clear halt conditions for bulk and interrupt endpoints,
893 * as reported by URB completion status. Endpoints that are halted are 893 * as reported by URB completion status. Endpoints that are halted are
894 * sometimes referred to as being "stalled". Such endpoints are unable 894 * sometimes referred to as being "stalled". Such endpoints are unable
895 * to transmit or receive data until the halt status is cleared. Any URBs 895 * to transmit or receive data until the halt status is cleared. Any URBs
896 * queued for such an endpoint should normally be unlinked by the driver 896 * queued for such an endpoint should normally be unlinked by the driver
897 * before clearing the halt condition, as described in sections 5.7.5 897 * before clearing the halt condition, as described in sections 5.7.5
898 * and 5.8.5 of the USB 2.0 spec. 898 * and 5.8.5 of the USB 2.0 spec.
899 * 899 *
900 * Note that control and isochronous endpoints don't halt, although control 900 * Note that control and isochronous endpoints don't halt, although control
901 * endpoints report "protocol stall" (for unsupported requests) using the 901 * endpoints report "protocol stall" (for unsupported requests) using the
902 * same status code used to report a true stall. 902 * same status code used to report a true stall.
903 * 903 *
904 * This call is synchronous, and may not be used in an interrupt context. 904 * This call is synchronous, and may not be used in an interrupt context.
905 * 905 *
906 * Returns zero on success, or else the status code returned by the 906 * Returns zero on success, or else the status code returned by the
907 * underlying usb_control_msg() call. 907 * underlying usb_control_msg() call.
908 */ 908 */
909 int usb_clear_halt(struct usb_device *dev, int pipe) 909 int usb_clear_halt(struct usb_device *dev, int pipe)
910 { 910 {
911 int result; 911 int result;
912 int endp = usb_pipeendpoint(pipe); 912 int endp = usb_pipeendpoint(pipe);
913 913
914 if (usb_pipein (pipe)) 914 if (usb_pipein (pipe))
915 endp |= USB_DIR_IN; 915 endp |= USB_DIR_IN;
916 916
917 /* we don't care if it wasn't halted first. in fact some devices 917 /* we don't care if it wasn't halted first. in fact some devices
918 * (like some ibmcam model 1 units) seem to expect hosts to make 918 * (like some ibmcam model 1 units) seem to expect hosts to make
919 * this request for iso endpoints, which can't halt! 919 * this request for iso endpoints, which can't halt!
920 */ 920 */
921 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 921 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
922 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 922 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
923 USB_ENDPOINT_HALT, endp, NULL, 0, 923 USB_ENDPOINT_HALT, endp, NULL, 0,
924 USB_CTRL_SET_TIMEOUT); 924 USB_CTRL_SET_TIMEOUT);
925 925
926 /* don't un-halt or force to DATA0 except on success */ 926 /* don't un-halt or force to DATA0 except on success */
927 if (result < 0) 927 if (result < 0)
928 return result; 928 return result;
929 929
930 /* NOTE: seems like Microsoft and Apple don't bother verifying 930 /* NOTE: seems like Microsoft and Apple don't bother verifying
931 * the clear "took", so some devices could lock up if you check... 931 * the clear "took", so some devices could lock up if you check...
932 * such as the Hagiwara FlashGate DUAL. So we won't bother. 932 * such as the Hagiwara FlashGate DUAL. So we won't bother.
933 * 933 *
934 * NOTE: make sure the logic here doesn't diverge much from 934 * NOTE: make sure the logic here doesn't diverge much from
935 * the copy in usb-storage, for as long as we need two copies. 935 * the copy in usb-storage, for as long as we need two copies.
936 */ 936 */
937 937
938 /* toggle was reset by the clear */ 938 /* toggle was reset by the clear */
939 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); 939 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
940 940
941 return 0; 941 return 0;
942 } 942 }
943 943
944 /** 944 /**
945 * usb_disable_endpoint -- Disable an endpoint by address 945 * usb_disable_endpoint -- Disable an endpoint by address
946 * @dev: the device whose endpoint is being disabled 946 * @dev: the device whose endpoint is being disabled
947 * @epaddr: the endpoint's address. Endpoint number for output, 947 * @epaddr: the endpoint's address. Endpoint number for output,
948 * endpoint number + USB_DIR_IN for input 948 * endpoint number + USB_DIR_IN for input
949 * 949 *
950 * Deallocates hcd/hardware state for this endpoint ... and nukes all 950 * Deallocates hcd/hardware state for this endpoint ... and nukes all
951 * pending urbs. 951 * pending urbs.
952 * 952 *
953 * If the HCD hasn't registered a disable() function, this sets the 953 * If the HCD hasn't registered a disable() function, this sets the
954 * endpoint's maxpacket size to 0 to prevent further submissions. 954 * endpoint's maxpacket size to 0 to prevent further submissions.
955 */ 955 */
956 void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr) 956 void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
957 { 957 {
958 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; 958 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
959 struct usb_host_endpoint *ep; 959 struct usb_host_endpoint *ep;
960 960
961 if (!dev) 961 if (!dev)
962 return; 962 return;
963 963
964 if (usb_endpoint_out(epaddr)) { 964 if (usb_endpoint_out(epaddr)) {
965 ep = dev->ep_out[epnum]; 965 ep = dev->ep_out[epnum];
966 dev->ep_out[epnum] = NULL; 966 dev->ep_out[epnum] = NULL;
967 } else { 967 } else {
968 ep = dev->ep_in[epnum]; 968 ep = dev->ep_in[epnum];
969 dev->ep_in[epnum] = NULL; 969 dev->ep_in[epnum] = NULL;
970 } 970 }
971 if (ep && dev->bus && dev->bus->op && dev->bus->op->disable) 971 if (ep && dev->bus && dev->bus->op && dev->bus->op->disable)
972 dev->bus->op->disable(dev, ep); 972 dev->bus->op->disable(dev, ep);
973 } 973 }
974 974
975 /** 975 /**
976 * usb_disable_interface -- Disable all endpoints for an interface 976 * usb_disable_interface -- Disable all endpoints for an interface
977 * @dev: the device whose interface is being disabled 977 * @dev: the device whose interface is being disabled
978 * @intf: pointer to the interface descriptor 978 * @intf: pointer to the interface descriptor
979 * 979 *
980 * Disables all the endpoints for the interface's current altsetting. 980 * Disables all the endpoints for the interface's current altsetting.
981 */ 981 */
982 void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf) 982 void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
983 { 983 {
984 struct usb_host_interface *alt = intf->cur_altsetting; 984 struct usb_host_interface *alt = intf->cur_altsetting;
985 int i; 985 int i;
986 986
987 for (i = 0; i < alt->desc.bNumEndpoints; ++i) { 987 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
988 usb_disable_endpoint(dev, 988 usb_disable_endpoint(dev,
989 alt->endpoint[i].desc.bEndpointAddress); 989 alt->endpoint[i].desc.bEndpointAddress);
990 } 990 }
991 } 991 }
992 992
993 /* 993 /*
994 * usb_disable_device - Disable all the endpoints for a USB device 994 * usb_disable_device - Disable all the endpoints for a USB device
995 * @dev: the device whose endpoints are being disabled 995 * @dev: the device whose endpoints are being disabled
996 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it. 996 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
997 * 997 *
998 * Disables all the device's endpoints, potentially including endpoint 0. 998 * Disables all the device's endpoints, potentially including endpoint 0.
999 * Deallocates hcd/hardware state for the endpoints (nuking all or most 999 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1000 * pending urbs) and usbcore state for the interfaces, so that usbcore 1000 * pending urbs) and usbcore state for the interfaces, so that usbcore
1001 * must usb_set_configuration() before any interfaces could be used. 1001 * must usb_set_configuration() before any interfaces could be used.
1002 */ 1002 */
1003 void usb_disable_device(struct usb_device *dev, int skip_ep0) 1003 void usb_disable_device(struct usb_device *dev, int skip_ep0)
1004 { 1004 {
1005 int i; 1005 int i;
1006 1006
1007 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__, 1007 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
1008 skip_ep0 ? "non-ep0" : "all"); 1008 skip_ep0 ? "non-ep0" : "all");
1009 for (i = skip_ep0; i < 16; ++i) { 1009 for (i = skip_ep0; i < 16; ++i) {
1010 usb_disable_endpoint(dev, i); 1010 usb_disable_endpoint(dev, i);
1011 usb_disable_endpoint(dev, i + USB_DIR_IN); 1011 usb_disable_endpoint(dev, i + USB_DIR_IN);
1012 } 1012 }
1013 dev->toggle[0] = dev->toggle[1] = 0; 1013 dev->toggle[0] = dev->toggle[1] = 0;
1014 1014
1015 /* getting rid of interfaces will disconnect 1015 /* getting rid of interfaces will disconnect
1016 * any drivers bound to them (a key side effect) 1016 * any drivers bound to them (a key side effect)
1017 */ 1017 */
1018 if (dev->actconfig) { 1018 if (dev->actconfig) {
1019 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 1019 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1020 struct usb_interface *interface; 1020 struct usb_interface *interface;
1021 1021
1022 /* remove this interface if it has been registered */ 1022 /* remove this interface if it has been registered */
1023 interface = dev->actconfig->interface[i]; 1023 interface = dev->actconfig->interface[i];
1024 if (!device_is_registered(&interface->dev)) 1024 if (!device_is_registered(&interface->dev))
1025 continue; 1025 continue;
1026 dev_dbg (&dev->dev, "unregistering interface %s\n", 1026 dev_dbg (&dev->dev, "unregistering interface %s\n",
1027 interface->dev.bus_id); 1027 interface->dev.bus_id);
1028 usb_remove_sysfs_intf_files(interface); 1028 usb_remove_sysfs_intf_files(interface);
1029 device_del (&interface->dev); 1029 device_del (&interface->dev);
1030 } 1030 }
1031 1031
1032 /* Now that the interfaces are unbound, nobody should 1032 /* Now that the interfaces are unbound, nobody should
1033 * try to access them. 1033 * try to access them.
1034 */ 1034 */
1035 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 1035 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1036 put_device (&dev->actconfig->interface[i]->dev); 1036 put_device (&dev->actconfig->interface[i]->dev);
1037 dev->actconfig->interface[i] = NULL; 1037 dev->actconfig->interface[i] = NULL;
1038 } 1038 }
1039 dev->actconfig = NULL; 1039 dev->actconfig = NULL;
1040 if (dev->state == USB_STATE_CONFIGURED) 1040 if (dev->state == USB_STATE_CONFIGURED)
1041 usb_set_device_state(dev, USB_STATE_ADDRESS); 1041 usb_set_device_state(dev, USB_STATE_ADDRESS);
1042 } 1042 }
1043 } 1043 }
1044 1044
1045 1045
1046 /* 1046 /*
1047 * usb_enable_endpoint - Enable an endpoint for USB communications 1047 * usb_enable_endpoint - Enable an endpoint for USB communications
1048 * @dev: the device whose interface is being enabled 1048 * @dev: the device whose interface is being enabled
1049 * @ep: the endpoint 1049 * @ep: the endpoint
1050 * 1050 *
1051 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers. 1051 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1052 * For control endpoints, both the input and output sides are handled. 1052 * For control endpoints, both the input and output sides are handled.
1053 */ 1053 */
1054 static void 1054 static void
1055 usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep) 1055 usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1056 { 1056 {
1057 unsigned int epaddr = ep->desc.bEndpointAddress; 1057 unsigned int epaddr = ep->desc.bEndpointAddress;
1058 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; 1058 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1059 int is_control; 1059 int is_control;
1060 1060
1061 is_control = ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 1061 is_control = ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1062 == USB_ENDPOINT_XFER_CONTROL); 1062 == USB_ENDPOINT_XFER_CONTROL);
1063 if (usb_endpoint_out(epaddr) || is_control) { 1063 if (usb_endpoint_out(epaddr) || is_control) {
1064 usb_settoggle(dev, epnum, 1, 0); 1064 usb_settoggle(dev, epnum, 1, 0);
1065 dev->ep_out[epnum] = ep; 1065 dev->ep_out[epnum] = ep;
1066 } 1066 }
1067 if (!usb_endpoint_out(epaddr) || is_control) { 1067 if (!usb_endpoint_out(epaddr) || is_control) {
1068 usb_settoggle(dev, epnum, 0, 0); 1068 usb_settoggle(dev, epnum, 0, 0);
1069 dev->ep_in[epnum] = ep; 1069 dev->ep_in[epnum] = ep;
1070 } 1070 }
1071 } 1071 }
1072 1072
1073 /* 1073 /*
1074 * usb_enable_interface - Enable all the endpoints for an interface 1074 * usb_enable_interface - Enable all the endpoints for an interface
1075 * @dev: the device whose interface is being enabled 1075 * @dev: the device whose interface is being enabled
1076 * @intf: pointer to the interface descriptor 1076 * @intf: pointer to the interface descriptor
1077 * 1077 *
1078 * Enables all the endpoints for the interface's current altsetting. 1078 * Enables all the endpoints for the interface's current altsetting.
1079 */ 1079 */
1080 static void usb_enable_interface(struct usb_device *dev, 1080 static void usb_enable_interface(struct usb_device *dev,
1081 struct usb_interface *intf) 1081 struct usb_interface *intf)
1082 { 1082 {
1083 struct usb_host_interface *alt = intf->cur_altsetting; 1083 struct usb_host_interface *alt = intf->cur_altsetting;
1084 int i; 1084 int i;
1085 1085
1086 for (i = 0; i < alt->desc.bNumEndpoints; ++i) 1086 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1087 usb_enable_endpoint(dev, &alt->endpoint[i]); 1087 usb_enable_endpoint(dev, &alt->endpoint[i]);
1088 } 1088 }
1089 1089
1090 /** 1090 /**
1091 * usb_set_interface - Makes a particular alternate setting be current 1091 * usb_set_interface - Makes a particular alternate setting be current
1092 * @dev: the device whose interface is being updated 1092 * @dev: the device whose interface is being updated
1093 * @interface: the interface being updated 1093 * @interface: the interface being updated
1094 * @alternate: the setting being chosen. 1094 * @alternate: the setting being chosen.
1095 * Context: !in_interrupt () 1095 * Context: !in_interrupt ()
1096 * 1096 *
1097 * This is used to enable data transfers on interfaces that may not 1097 * This is used to enable data transfers on interfaces that may not
1098 * be enabled by default. Not all devices support such configurability. 1098 * be enabled by default. Not all devices support such configurability.
1099 * Only the driver bound to an interface may change its setting. 1099 * Only the driver bound to an interface may change its setting.
1100 * 1100 *
1101 * Within any given configuration, each interface may have several 1101 * Within any given configuration, each interface may have several
1102 * alternative settings. These are often used to control levels of 1102 * alternative settings. These are often used to control levels of
1103 * bandwidth consumption. For example, the default setting for a high 1103 * bandwidth consumption. For example, the default setting for a high
1104 * speed interrupt endpoint may not send more than 64 bytes per microframe, 1104 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1105 * while interrupt transfers of up to 3KBytes per microframe are legal. 1105 * while interrupt transfers of up to 3KBytes per microframe are legal.
1106 * Also, isochronous endpoints may never be part of an 1106 * Also, isochronous endpoints may never be part of an
1107 * interface's default setting. To access such bandwidth, alternate 1107 * interface's default setting. To access such bandwidth, alternate
1108 * interface settings must be made current. 1108 * interface settings must be made current.
1109 * 1109 *
1110 * Note that in the Linux USB subsystem, bandwidth associated with 1110 * Note that in the Linux USB subsystem, bandwidth associated with
1111 * an endpoint in a given alternate setting is not reserved until an URB 1111 * an endpoint in a given alternate setting is not reserved until an URB
1112 * is submitted that needs that bandwidth. Some other operating systems 1112 * is submitted that needs that bandwidth. Some other operating systems
1113 * allocate bandwidth early, when a configuration is chosen. 1113 * allocate bandwidth early, when a configuration is chosen.
1114 * 1114 *
1115 * This call is synchronous, and may not be used in an interrupt context. 1115 * This call is synchronous, and may not be used in an interrupt context.
1116 * Also, drivers must not change altsettings while urbs are scheduled for 1116 * Also, drivers must not change altsettings while urbs are scheduled for
1117 * endpoints in that interface; all such urbs must first be completed 1117 * endpoints in that interface; all such urbs must first be completed
1118 * (perhaps forced by unlinking). 1118 * (perhaps forced by unlinking).
1119 * 1119 *
1120 * Returns zero on success, or else the status code returned by the 1120 * Returns zero on success, or else the status code returned by the
1121 * underlying usb_control_msg() call. 1121 * underlying usb_control_msg() call.
1122 */ 1122 */
1123 int usb_set_interface(struct usb_device *dev, int interface, int alternate) 1123 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1124 { 1124 {
1125 struct usb_interface *iface; 1125 struct usb_interface *iface;
1126 struct usb_host_interface *alt; 1126 struct usb_host_interface *alt;
1127 int ret; 1127 int ret;
1128 int manual = 0; 1128 int manual = 0;
1129 1129
1130 if (dev->state == USB_STATE_SUSPENDED) 1130 if (dev->state == USB_STATE_SUSPENDED)
1131 return -EHOSTUNREACH; 1131 return -EHOSTUNREACH;
1132 1132
1133 iface = usb_ifnum_to_if(dev, interface); 1133 iface = usb_ifnum_to_if(dev, interface);
1134 if (!iface) { 1134 if (!iface) {
1135 dev_dbg(&dev->dev, "selecting invalid interface %d\n", 1135 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1136 interface); 1136 interface);
1137 return -EINVAL; 1137 return -EINVAL;
1138 } 1138 }
1139 1139
1140 alt = usb_altnum_to_altsetting(iface, alternate); 1140 alt = usb_altnum_to_altsetting(iface, alternate);
1141 if (!alt) { 1141 if (!alt) {
1142 warn("selecting invalid altsetting %d", alternate); 1142 warn("selecting invalid altsetting %d", alternate);
1143 return -EINVAL; 1143 return -EINVAL;
1144 } 1144 }
1145 1145
1146 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1146 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1147 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, 1147 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1148 alternate, interface, NULL, 0, 5000); 1148 alternate, interface, NULL, 0, 5000);
1149 1149
1150 /* 9.4.10 says devices don't need this and are free to STALL the 1150 /* 9.4.10 says devices don't need this and are free to STALL the
1151 * request if the interface only has one alternate setting. 1151 * request if the interface only has one alternate setting.
1152 */ 1152 */
1153 if (ret == -EPIPE && iface->num_altsetting == 1) { 1153 if (ret == -EPIPE && iface->num_altsetting == 1) {
1154 dev_dbg(&dev->dev, 1154 dev_dbg(&dev->dev,
1155 "manual set_interface for iface %d, alt %d\n", 1155 "manual set_interface for iface %d, alt %d\n",
1156 interface, alternate); 1156 interface, alternate);
1157 manual = 1; 1157 manual = 1;
1158 } else if (ret < 0) 1158 } else if (ret < 0)
1159 return ret; 1159 return ret;
1160 1160
1161 /* FIXME drivers shouldn't need to replicate/bugfix the logic here 1161 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1162 * when they implement async or easily-killable versions of this or 1162 * when they implement async or easily-killable versions of this or
1163 * other "should-be-internal" functions (like clear_halt). 1163 * other "should-be-internal" functions (like clear_halt).
1164 * should hcd+usbcore postprocess control requests? 1164 * should hcd+usbcore postprocess control requests?
1165 */ 1165 */
1166 1166
1167 /* prevent submissions using previous endpoint settings */ 1167 /* prevent submissions using previous endpoint settings */
1168 if (device_is_registered(&iface->dev)) 1168 if (device_is_registered(&iface->dev))
1169 usb_remove_sysfs_intf_files(iface); 1169 usb_remove_sysfs_intf_files(iface);
1170 usb_disable_interface(dev, iface); 1170 usb_disable_interface(dev, iface);
1171 1171
1172 iface->cur_altsetting = alt; 1172 iface->cur_altsetting = alt;
1173 1173
1174 /* If the interface only has one altsetting and the device didn't 1174 /* If the interface only has one altsetting and the device didn't
1175 * accept the request, we attempt to carry out the equivalent action 1175 * accept the request, we attempt to carry out the equivalent action
1176 * by manually clearing the HALT feature for each endpoint in the 1176 * by manually clearing the HALT feature for each endpoint in the
1177 * new altsetting. 1177 * new altsetting.
1178 */ 1178 */
1179 if (manual) { 1179 if (manual) {
1180 int i; 1180 int i;
1181 1181
1182 for (i = 0; i < alt->desc.bNumEndpoints; i++) { 1182 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1183 unsigned int epaddr = 1183 unsigned int epaddr =
1184 alt->endpoint[i].desc.bEndpointAddress; 1184 alt->endpoint[i].desc.bEndpointAddress;
1185 unsigned int pipe = 1185 unsigned int pipe =
1186 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr) 1186 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
1187 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN); 1187 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
1188 1188
1189 usb_clear_halt(dev, pipe); 1189 usb_clear_halt(dev, pipe);
1190 } 1190 }
1191 } 1191 }
1192 1192
1193 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting 1193 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1194 * 1194 *
1195 * Note: 1195 * Note:
1196 * Despite EP0 is always present in all interfaces/AS, the list of 1196 * Despite EP0 is always present in all interfaces/AS, the list of
1197 * endpoints from the descriptor does not contain EP0. Due to its 1197 * endpoints from the descriptor does not contain EP0. Due to its
1198 * omnipresence one might expect EP0 being considered "affected" by 1198 * omnipresence one might expect EP0 being considered "affected" by
1199 * any SetInterface request and hence assume toggles need to be reset. 1199 * any SetInterface request and hence assume toggles need to be reset.
1200 * However, EP0 toggles are re-synced for every individual transfer 1200 * However, EP0 toggles are re-synced for every individual transfer
1201 * during the SETUP stage - hence EP0 toggles are "don't care" here. 1201 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1202 * (Likewise, EP0 never "halts" on well designed devices.) 1202 * (Likewise, EP0 never "halts" on well designed devices.)
1203 */ 1203 */
1204 usb_enable_interface(dev, iface); 1204 usb_enable_interface(dev, iface);
1205 if (device_is_registered(&iface->dev)) 1205 if (device_is_registered(&iface->dev))
1206 usb_create_sysfs_intf_files(iface); 1206 usb_create_sysfs_intf_files(iface);
1207 1207
1208 return 0; 1208 return 0;
1209 } 1209 }
1210 1210
1211 /** 1211 /**
1212 * usb_reset_configuration - lightweight device reset 1212 * usb_reset_configuration - lightweight device reset
1213 * @dev: the device whose configuration is being reset 1213 * @dev: the device whose configuration is being reset
1214 * 1214 *
1215 * This issues a standard SET_CONFIGURATION request to the device using 1215 * This issues a standard SET_CONFIGURATION request to the device using
1216 * the current configuration. The effect is to reset most USB-related 1216 * the current configuration. The effect is to reset most USB-related
1217 * state in the device, including interface altsettings (reset to zero), 1217 * state in the device, including interface altsettings (reset to zero),
1218 * endpoint halts (cleared), and data toggle (only for bulk and interrupt 1218 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1219 * endpoints). Other usbcore state is unchanged, including bindings of 1219 * endpoints). Other usbcore state is unchanged, including bindings of
1220 * usb device drivers to interfaces. 1220 * usb device drivers to interfaces.
1221 * 1221 *
1222 * Because this affects multiple interfaces, avoid using this with composite 1222 * Because this affects multiple interfaces, avoid using this with composite
1223 * (multi-interface) devices. Instead, the driver for each interface may 1223 * (multi-interface) devices. Instead, the driver for each interface may
1224 * use usb_set_interface() on the interfaces it claims. Be careful though; 1224 * use usb_set_interface() on the interfaces it claims. Be careful though;
1225 * some devices don't support the SET_INTERFACE request, and others won't 1225 * some devices don't support the SET_INTERFACE request, and others won't
1226 * reset all the interface state (notably data toggles). Resetting the whole 1226 * reset all the interface state (notably data toggles). Resetting the whole
1227 * configuration would affect other drivers' interfaces. 1227 * configuration would affect other drivers' interfaces.
1228 * 1228 *
1229 * The caller must own the device lock. 1229 * The caller must own the device lock.
1230 * 1230 *
1231 * Returns zero on success, else a negative error code. 1231 * Returns zero on success, else a negative error code.
1232 */ 1232 */
1233 int usb_reset_configuration(struct usb_device *dev) 1233 int usb_reset_configuration(struct usb_device *dev)
1234 { 1234 {
1235 int i, retval; 1235 int i, retval;
1236 struct usb_host_config *config; 1236 struct usb_host_config *config;
1237 1237
1238 if (dev->state == USB_STATE_SUSPENDED) 1238 if (dev->state == USB_STATE_SUSPENDED)
1239 return -EHOSTUNREACH; 1239 return -EHOSTUNREACH;
1240 1240
1241 /* caller must have locked the device and must own 1241 /* caller must have locked the device and must own
1242 * the usb bus readlock (so driver bindings are stable); 1242 * the usb bus readlock (so driver bindings are stable);
1243 * calls during probe() are fine 1243 * calls during probe() are fine
1244 */ 1244 */
1245 1245
1246 for (i = 1; i < 16; ++i) { 1246 for (i = 1; i < 16; ++i) {
1247 usb_disable_endpoint(dev, i); 1247 usb_disable_endpoint(dev, i);
1248 usb_disable_endpoint(dev, i + USB_DIR_IN); 1248 usb_disable_endpoint(dev, i + USB_DIR_IN);
1249 } 1249 }
1250 1250
1251 config = dev->actconfig; 1251 config = dev->actconfig;
1252 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1252 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1253 USB_REQ_SET_CONFIGURATION, 0, 1253 USB_REQ_SET_CONFIGURATION, 0,
1254 config->desc.bConfigurationValue, 0, 1254 config->desc.bConfigurationValue, 0,
1255 NULL, 0, USB_CTRL_SET_TIMEOUT); 1255 NULL, 0, USB_CTRL_SET_TIMEOUT);
1256 if (retval < 0) 1256 if (retval < 0)
1257 return retval; 1257 return retval;
1258 1258
1259 dev->toggle[0] = dev->toggle[1] = 0; 1259 dev->toggle[0] = dev->toggle[1] = 0;
1260 1260
1261 /* re-init hc/hcd interface/endpoint state */ 1261 /* re-init hc/hcd interface/endpoint state */
1262 for (i = 0; i < config->desc.bNumInterfaces; i++) { 1262 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1263 struct usb_interface *intf = config->interface[i]; 1263 struct usb_interface *intf = config->interface[i];
1264 struct usb_host_interface *alt; 1264 struct usb_host_interface *alt;
1265 1265
1266 if (device_is_registered(&intf->dev)) 1266 if (device_is_registered(&intf->dev))
1267 usb_remove_sysfs_intf_files(intf); 1267 usb_remove_sysfs_intf_files(intf);
1268 alt = usb_altnum_to_altsetting(intf, 0); 1268 alt = usb_altnum_to_altsetting(intf, 0);
1269 1269
1270 /* No altsetting 0? We'll assume the first altsetting. 1270 /* No altsetting 0? We'll assume the first altsetting.
1271 * We could use a GetInterface call, but if a device is 1271 * We could use a GetInterface call, but if a device is
1272 * so non-compliant that it doesn't have altsetting 0 1272 * so non-compliant that it doesn't have altsetting 0
1273 * then I wouldn't trust its reply anyway. 1273 * then I wouldn't trust its reply anyway.
1274 */ 1274 */
1275 if (!alt) 1275 if (!alt)
1276 alt = &intf->altsetting[0]; 1276 alt = &intf->altsetting[0];
1277 1277
1278 intf->cur_altsetting = alt; 1278 intf->cur_altsetting = alt;
1279 usb_enable_interface(dev, intf); 1279 usb_enable_interface(dev, intf);
1280 if (device_is_registered(&intf->dev)) 1280 if (device_is_registered(&intf->dev))
1281 usb_create_sysfs_intf_files(intf); 1281 usb_create_sysfs_intf_files(intf);
1282 } 1282 }
1283 return 0; 1283 return 0;
1284 } 1284 }
1285 1285
1286 static void release_interface(struct device *dev) 1286 static void release_interface(struct device *dev)
1287 { 1287 {
1288 struct usb_interface *intf = to_usb_interface(dev); 1288 struct usb_interface *intf = to_usb_interface(dev);
1289 struct usb_interface_cache *intfc = 1289 struct usb_interface_cache *intfc =
1290 altsetting_to_usb_interface_cache(intf->altsetting); 1290 altsetting_to_usb_interface_cache(intf->altsetting);
1291 1291
1292 kref_put(&intfc->ref, usb_release_interface_cache); 1292 kref_put(&intfc->ref, usb_release_interface_cache);
1293 kfree(intf); 1293 kfree(intf);
1294 } 1294 }
1295 1295
1296 /* 1296 /*
1297 * usb_set_configuration - Makes a particular device setting be current 1297 * usb_set_configuration - Makes a particular device setting be current
1298 * @dev: the device whose configuration is being updated 1298 * @dev: the device whose configuration is being updated
1299 * @configuration: the configuration being chosen. 1299 * @configuration: the configuration being chosen.
1300 * Context: !in_interrupt(), caller owns the device lock 1300 * Context: !in_interrupt(), caller owns the device lock
1301 * 1301 *
1302 * This is used to enable non-default device modes. Not all devices 1302 * This is used to enable non-default device modes. Not all devices
1303 * use this kind of configurability; many devices only have one 1303 * use this kind of configurability; many devices only have one
1304 * configuration. 1304 * configuration.
1305 * 1305 *
1306 * USB device configurations may affect Linux interoperability, 1306 * USB device configurations may affect Linux interoperability,
1307 * power consumption and the functionality available. For example, 1307 * power consumption and the functionality available. For example,
1308 * the default configuration is limited to using 100mA of bus power, 1308 * the default configuration is limited to using 100mA of bus power,
1309 * so that when certain device functionality requires more power, 1309 * so that when certain device functionality requires more power,
1310 * and the device is bus powered, that functionality should be in some 1310 * and the device is bus powered, that functionality should be in some
1311 * non-default device configuration. Other device modes may also be 1311 * non-default device configuration. Other device modes may also be
1312 * reflected as configuration options, such as whether two ISDN 1312 * reflected as configuration options, such as whether two ISDN
1313 * channels are available independently; and choosing between open 1313 * channels are available independently; and choosing between open
1314 * standard device protocols (like CDC) or proprietary ones. 1314 * standard device protocols (like CDC) or proprietary ones.
1315 * 1315 *
1316 * Note that USB has an additional level of device configurability, 1316 * Note that USB has an additional level of device configurability,
1317 * associated with interfaces. That configurability is accessed using 1317 * associated with interfaces. That configurability is accessed using
1318 * usb_set_interface(). 1318 * usb_set_interface().
1319 * 1319 *
1320 * This call is synchronous. The calling context must be able to sleep, 1320 * This call is synchronous. The calling context must be able to sleep,
1321 * must own the device lock, and must not hold the driver model's USB 1321 * must own the device lock, and must not hold the driver model's USB
1322 * bus rwsem; usb device driver probe() methods cannot use this routine. 1322 * bus rwsem; usb device driver probe() methods cannot use this routine.
1323 * 1323 *
1324 * Returns zero on success, or else the status code returned by the 1324 * Returns zero on success, or else the status code returned by the
1325 * underlying call that failed. On successful completion, each interface 1325 * underlying call that failed. On successful completion, each interface
1326 * in the original device configuration has been destroyed, and each one 1326 * in the original device configuration has been destroyed, and each one
1327 * in the new configuration has been probed by all relevant usb device 1327 * in the new configuration has been probed by all relevant usb device
1328 * drivers currently known to the kernel. 1328 * drivers currently known to the kernel.
1329 */ 1329 */
1330 int usb_set_configuration(struct usb_device *dev, int configuration) 1330 int usb_set_configuration(struct usb_device *dev, int configuration)
1331 { 1331 {
1332 int i, ret; 1332 int i, ret;
1333 struct usb_host_config *cp = NULL; 1333 struct usb_host_config *cp = NULL;
1334 struct usb_interface **new_interfaces = NULL; 1334 struct usb_interface **new_interfaces = NULL;
1335 int n, nintf; 1335 int n, nintf;
1336 1336
1337 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { 1337 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1338 if (dev->config[i].desc.bConfigurationValue == configuration) { 1338 if (dev->config[i].desc.bConfigurationValue == configuration) {
1339 cp = &dev->config[i]; 1339 cp = &dev->config[i];
1340 break; 1340 break;
1341 } 1341 }
1342 } 1342 }
1343 if ((!cp && configuration != 0)) 1343 if ((!cp && configuration != 0))
1344 return -EINVAL; 1344 return -EINVAL;
1345 1345
1346 /* The USB spec says configuration 0 means unconfigured. 1346 /* The USB spec says configuration 0 means unconfigured.
1347 * But if a device includes a configuration numbered 0, 1347 * But if a device includes a configuration numbered 0,
1348 * we will accept it as a correctly configured state. 1348 * we will accept it as a correctly configured state.
1349 */ 1349 */
1350 if (cp && configuration == 0) 1350 if (cp && configuration == 0)
1351 dev_warn(&dev->dev, "config 0 descriptor??\n"); 1351 dev_warn(&dev->dev, "config 0 descriptor??\n");
1352 1352
1353 if (dev->state == USB_STATE_SUSPENDED) 1353 if (dev->state == USB_STATE_SUSPENDED)
1354 return -EHOSTUNREACH; 1354 return -EHOSTUNREACH;
1355 1355
1356 /* Allocate memory for new interfaces before doing anything else, 1356 /* Allocate memory for new interfaces before doing anything else,
1357 * so that if we run out then nothing will have changed. */ 1357 * so that if we run out then nothing will have changed. */
1358 n = nintf = 0; 1358 n = nintf = 0;
1359 if (cp) { 1359 if (cp) {
1360 nintf = cp->desc.bNumInterfaces; 1360 nintf = cp->desc.bNumInterfaces;
1361 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces), 1361 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1362 GFP_KERNEL); 1362 GFP_KERNEL);
1363 if (!new_interfaces) { 1363 if (!new_interfaces) {
1364 dev_err(&dev->dev, "Out of memory"); 1364 dev_err(&dev->dev, "Out of memory");
1365 return -ENOMEM; 1365 return -ENOMEM;
1366 } 1366 }
1367 1367
1368 for (; n < nintf; ++n) { 1368 for (; n < nintf; ++n) {
1369 new_interfaces[n] = kzalloc( 1369 new_interfaces[n] = kzalloc(
1370 sizeof(struct usb_interface), 1370 sizeof(struct usb_interface),
1371 GFP_KERNEL); 1371 GFP_KERNEL);
1372 if (!new_interfaces[n]) { 1372 if (!new_interfaces[n]) {
1373 dev_err(&dev->dev, "Out of memory"); 1373 dev_err(&dev->dev, "Out of memory");
1374 ret = -ENOMEM; 1374 ret = -ENOMEM;
1375 free_interfaces: 1375 free_interfaces:
1376 while (--n >= 0) 1376 while (--n >= 0)
1377 kfree(new_interfaces[n]); 1377 kfree(new_interfaces[n]);
1378 kfree(new_interfaces); 1378 kfree(new_interfaces);
1379 return ret; 1379 return ret;
1380 } 1380 }
1381 } 1381 }
1382 } 1382 }
1383 1383
1384 /* if it's already configured, clear out old state first. 1384 /* if it's already configured, clear out old state first.
1385 * getting rid of old interfaces means unbinding their drivers. 1385 * getting rid of old interfaces means unbinding their drivers.
1386 */ 1386 */
1387 if (dev->state != USB_STATE_ADDRESS) 1387 if (dev->state != USB_STATE_ADDRESS)
1388 usb_disable_device (dev, 1); // Skip ep0 1388 usb_disable_device (dev, 1); // Skip ep0
1389 1389
1390 n = dev->bus_mA - cp->desc.bMaxPower * 2;
1391 if (n < 0)
1392 dev_warn(&dev->dev, "new config #%d exceeds power "
1393 "limit by %dmA\n",
1394 configuration, -n);
1395
1390 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1396 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1391 USB_REQ_SET_CONFIGURATION, 0, configuration, 0, 1397 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1392 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) 1398 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0)
1393 goto free_interfaces; 1399 goto free_interfaces;
1394 1400
1395 dev->actconfig = cp; 1401 dev->actconfig = cp;
1396 if (!cp) 1402 if (!cp)
1397 usb_set_device_state(dev, USB_STATE_ADDRESS); 1403 usb_set_device_state(dev, USB_STATE_ADDRESS);
1398 else { 1404 else {
1399 usb_set_device_state(dev, USB_STATE_CONFIGURED); 1405 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1400 1406
1401 /* Initialize the new interface structures and the 1407 /* Initialize the new interface structures and the
1402 * hc/hcd/usbcore interface/endpoint state. 1408 * hc/hcd/usbcore interface/endpoint state.
1403 */ 1409 */
1404 for (i = 0; i < nintf; ++i) { 1410 for (i = 0; i < nintf; ++i) {
1405 struct usb_interface_cache *intfc; 1411 struct usb_interface_cache *intfc;
1406 struct usb_interface *intf; 1412 struct usb_interface *intf;
1407 struct usb_host_interface *alt; 1413 struct usb_host_interface *alt;
1408 1414
1409 cp->interface[i] = intf = new_interfaces[i]; 1415 cp->interface[i] = intf = new_interfaces[i];
1410 intfc = cp->intf_cache[i]; 1416 intfc = cp->intf_cache[i];
1411 intf->altsetting = intfc->altsetting; 1417 intf->altsetting = intfc->altsetting;
1412 intf->num_altsetting = intfc->num_altsetting; 1418 intf->num_altsetting = intfc->num_altsetting;
1413 kref_get(&intfc->ref); 1419 kref_get(&intfc->ref);
1414 1420
1415 alt = usb_altnum_to_altsetting(intf, 0); 1421 alt = usb_altnum_to_altsetting(intf, 0);
1416 1422
1417 /* No altsetting 0? We'll assume the first altsetting. 1423 /* No altsetting 0? We'll assume the first altsetting.
1418 * We could use a GetInterface call, but if a device is 1424 * We could use a GetInterface call, but if a device is
1419 * so non-compliant that it doesn't have altsetting 0 1425 * so non-compliant that it doesn't have altsetting 0
1420 * then I wouldn't trust its reply anyway. 1426 * then I wouldn't trust its reply anyway.
1421 */ 1427 */
1422 if (!alt) 1428 if (!alt)
1423 alt = &intf->altsetting[0]; 1429 alt = &intf->altsetting[0];
1424 1430
1425 intf->cur_altsetting = alt; 1431 intf->cur_altsetting = alt;
1426 usb_enable_interface(dev, intf); 1432 usb_enable_interface(dev, intf);
1427 intf->dev.parent = &dev->dev; 1433 intf->dev.parent = &dev->dev;
1428 intf->dev.driver = NULL; 1434 intf->dev.driver = NULL;
1429 intf->dev.bus = &usb_bus_type; 1435 intf->dev.bus = &usb_bus_type;
1430 intf->dev.dma_mask = dev->dev.dma_mask; 1436 intf->dev.dma_mask = dev->dev.dma_mask;
1431 intf->dev.release = release_interface; 1437 intf->dev.release = release_interface;
1432 device_initialize (&intf->dev); 1438 device_initialize (&intf->dev);
1433 mark_quiesced(intf); 1439 mark_quiesced(intf);
1434 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d", 1440 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1435 dev->bus->busnum, dev->devpath, 1441 dev->bus->busnum, dev->devpath,
1436 configuration, 1442 configuration,
1437 alt->desc.bInterfaceNumber); 1443 alt->desc.bInterfaceNumber);
1438 } 1444 }
1439 kfree(new_interfaces); 1445 kfree(new_interfaces);
1440 1446
1441 if (cp->string == NULL) 1447 if (cp->string == NULL)
1442 cp->string = usb_cache_string(dev, 1448 cp->string = usb_cache_string(dev,
1443 cp->desc.iConfiguration); 1449 cp->desc.iConfiguration);
1444 1450
1445 /* Now that all the interfaces are set up, register them 1451 /* Now that all the interfaces are set up, register them
1446 * to trigger binding of drivers to interfaces. probe() 1452 * to trigger binding of drivers to interfaces. probe()
1447 * routines may install different altsettings and may 1453 * routines may install different altsettings and may
1448 * claim() any interfaces not yet bound. Many class drivers 1454 * claim() any interfaces not yet bound. Many class drivers
1449 * need that: CDC, audio, video, etc. 1455 * need that: CDC, audio, video, etc.
1450 */ 1456 */
1451 for (i = 0; i < nintf; ++i) { 1457 for (i = 0; i < nintf; ++i) {
1452 struct usb_interface *intf = cp->interface[i]; 1458 struct usb_interface *intf = cp->interface[i];
1453 1459
1454 dev_dbg (&dev->dev, 1460 dev_dbg (&dev->dev,
1455 "adding %s (config #%d, interface %d)\n", 1461 "adding %s (config #%d, interface %d)\n",
1456 intf->dev.bus_id, configuration, 1462 intf->dev.bus_id, configuration,
1457 intf->cur_altsetting->desc.bInterfaceNumber); 1463 intf->cur_altsetting->desc.bInterfaceNumber);
1458 ret = device_add (&intf->dev); 1464 ret = device_add (&intf->dev);
1459 if (ret != 0) { 1465 if (ret != 0) {
1460 dev_err(&dev->dev, 1466 dev_err(&dev->dev,
1461 "device_add(%s) --> %d\n", 1467 "device_add(%s) --> %d\n",
1462 intf->dev.bus_id, 1468 intf->dev.bus_id,
1463 ret); 1469 ret);
1464 continue; 1470 continue;
1465 } 1471 }
1466 usb_create_sysfs_intf_files (intf); 1472 usb_create_sysfs_intf_files (intf);
1467 } 1473 }
1468 } 1474 }
1469 1475
1470 return 0; 1476 return 0;
1471 } 1477 }
1472 1478
1473 // synchronous request completion model 1479 // synchronous request completion model
1474 EXPORT_SYMBOL(usb_control_msg); 1480 EXPORT_SYMBOL(usb_control_msg);
1475 EXPORT_SYMBOL(usb_bulk_msg); 1481 EXPORT_SYMBOL(usb_bulk_msg);
1476 1482
1477 EXPORT_SYMBOL(usb_sg_init); 1483 EXPORT_SYMBOL(usb_sg_init);
1478 EXPORT_SYMBOL(usb_sg_cancel); 1484 EXPORT_SYMBOL(usb_sg_cancel);
1479 EXPORT_SYMBOL(usb_sg_wait); 1485 EXPORT_SYMBOL(usb_sg_wait);
1480 1486
1481 // synchronous control message convenience routines 1487 // synchronous control message convenience routines
1482 EXPORT_SYMBOL(usb_get_descriptor); 1488 EXPORT_SYMBOL(usb_get_descriptor);
1483 EXPORT_SYMBOL(usb_get_status); 1489 EXPORT_SYMBOL(usb_get_status);
1484 EXPORT_SYMBOL(usb_get_string); 1490 EXPORT_SYMBOL(usb_get_string);
1485 EXPORT_SYMBOL(usb_string); 1491 EXPORT_SYMBOL(usb_string);
1486 1492
1487 // synchronous calls that also maintain usbcore state 1493 // synchronous calls that also maintain usbcore state
1488 EXPORT_SYMBOL(usb_clear_halt); 1494 EXPORT_SYMBOL(usb_clear_halt);
1489 EXPORT_SYMBOL(usb_reset_configuration); 1495 EXPORT_SYMBOL(usb_reset_configuration);
1490 EXPORT_SYMBOL(usb_set_interface); 1496 EXPORT_SYMBOL(usb_set_interface);
1491 1497
1492 1498
1 #ifndef __LINUX_USB_H 1 #ifndef __LINUX_USB_H
2 #define __LINUX_USB_H 2 #define __LINUX_USB_H
3 3
4 #include <linux/mod_devicetable.h> 4 #include <linux/mod_devicetable.h>
5 #include <linux/usb_ch9.h> 5 #include <linux/usb_ch9.h>
6 6
7 #define USB_MAJOR 180 7 #define USB_MAJOR 180
8 #define USB_DEVICE_MAJOR 189 8 #define USB_DEVICE_MAJOR 189
9 9
10 10
11 #ifdef __KERNEL__ 11 #ifdef __KERNEL__
12 12
13 #include <linux/config.h> 13 #include <linux/config.h>
14 #include <linux/errno.h> /* for -ENODEV */ 14 #include <linux/errno.h> /* for -ENODEV */
15 #include <linux/delay.h> /* for mdelay() */ 15 #include <linux/delay.h> /* for mdelay() */
16 #include <linux/interrupt.h> /* for in_interrupt() */ 16 #include <linux/interrupt.h> /* for in_interrupt() */
17 #include <linux/list.h> /* for struct list_head */ 17 #include <linux/list.h> /* for struct list_head */
18 #include <linux/kref.h> /* for struct kref */ 18 #include <linux/kref.h> /* for struct kref */
19 #include <linux/device.h> /* for struct device */ 19 #include <linux/device.h> /* for struct device */
20 #include <linux/fs.h> /* for struct file_operations */ 20 #include <linux/fs.h> /* for struct file_operations */
21 #include <linux/completion.h> /* for struct completion */ 21 #include <linux/completion.h> /* for struct completion */
22 #include <linux/sched.h> /* for current && schedule_timeout */ 22 #include <linux/sched.h> /* for current && schedule_timeout */
23 23
24 struct usb_device; 24 struct usb_device;
25 struct usb_driver; 25 struct usb_driver;
26 26
27 /*-------------------------------------------------------------------------*/ 27 /*-------------------------------------------------------------------------*/
28 28
29 /* 29 /*
30 * Host-side wrappers for standard USB descriptors ... these are parsed 30 * Host-side wrappers for standard USB descriptors ... these are parsed
31 * from the data provided by devices. Parsing turns them from a flat 31 * from the data provided by devices. Parsing turns them from a flat
32 * sequence of descriptors into a hierarchy: 32 * sequence of descriptors into a hierarchy:
33 * 33 *
34 * - devices have one (usually) or more configs; 34 * - devices have one (usually) or more configs;
35 * - configs have one (often) or more interfaces; 35 * - configs have one (often) or more interfaces;
36 * - interfaces have one (usually) or more settings; 36 * - interfaces have one (usually) or more settings;
37 * - each interface setting has zero or (usually) more endpoints. 37 * - each interface setting has zero or (usually) more endpoints.
38 * 38 *
39 * And there might be other descriptors mixed in with those. 39 * And there might be other descriptors mixed in with those.
40 * 40 *
41 * Devices may also have class-specific or vendor-specific descriptors. 41 * Devices may also have class-specific or vendor-specific descriptors.
42 */ 42 */
43 43
44 /** 44 /**
45 * struct usb_host_endpoint - host-side endpoint descriptor and queue 45 * struct usb_host_endpoint - host-side endpoint descriptor and queue
46 * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder 46 * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder
47 * @urb_list: urbs queued to this endpoint; maintained by usbcore 47 * @urb_list: urbs queued to this endpoint; maintained by usbcore
48 * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH) 48 * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH)
49 * with one or more transfer descriptors (TDs) per urb 49 * with one or more transfer descriptors (TDs) per urb
50 * @kobj: kobject for sysfs info 50 * @kobj: kobject for sysfs info
51 * @extra: descriptors following this endpoint in the configuration 51 * @extra: descriptors following this endpoint in the configuration
52 * @extralen: how many bytes of "extra" are valid 52 * @extralen: how many bytes of "extra" are valid
53 * 53 *
54 * USB requests are always queued to a given endpoint, identified by a 54 * USB requests are always queued to a given endpoint, identified by a
55 * descriptor within an active interface in a given USB configuration. 55 * descriptor within an active interface in a given USB configuration.
56 */ 56 */
57 struct usb_host_endpoint { 57 struct usb_host_endpoint {
58 struct usb_endpoint_descriptor desc; 58 struct usb_endpoint_descriptor desc;
59 struct list_head urb_list; 59 struct list_head urb_list;
60 void *hcpriv; 60 void *hcpriv;
61 struct kobject *kobj; /* For sysfs info */ 61 struct kobject *kobj; /* For sysfs info */
62 62
63 unsigned char *extra; /* Extra descriptors */ 63 unsigned char *extra; /* Extra descriptors */
64 int extralen; 64 int extralen;
65 }; 65 };
66 66
67 /* host-side wrapper for one interface setting's parsed descriptors */ 67 /* host-side wrapper for one interface setting's parsed descriptors */
68 struct usb_host_interface { 68 struct usb_host_interface {
69 struct usb_interface_descriptor desc; 69 struct usb_interface_descriptor desc;
70 70
71 /* array of desc.bNumEndpoint endpoints associated with this 71 /* array of desc.bNumEndpoint endpoints associated with this
72 * interface setting. these will be in no particular order. 72 * interface setting. these will be in no particular order.
73 */ 73 */
74 struct usb_host_endpoint *endpoint; 74 struct usb_host_endpoint *endpoint;
75 75
76 char *string; /* iInterface string, if present */ 76 char *string; /* iInterface string, if present */
77 unsigned char *extra; /* Extra descriptors */ 77 unsigned char *extra; /* Extra descriptors */
78 int extralen; 78 int extralen;
79 }; 79 };
80 80
81 enum usb_interface_condition { 81 enum usb_interface_condition {
82 USB_INTERFACE_UNBOUND = 0, 82 USB_INTERFACE_UNBOUND = 0,
83 USB_INTERFACE_BINDING, 83 USB_INTERFACE_BINDING,
84 USB_INTERFACE_BOUND, 84 USB_INTERFACE_BOUND,
85 USB_INTERFACE_UNBINDING, 85 USB_INTERFACE_UNBINDING,
86 }; 86 };
87 87
88 /** 88 /**
89 * struct usb_interface - what usb device drivers talk to 89 * struct usb_interface - what usb device drivers talk to
90 * @altsetting: array of interface structures, one for each alternate 90 * @altsetting: array of interface structures, one for each alternate
91 * setting that may be selected. Each one includes a set of 91 * setting that may be selected. Each one includes a set of
92 * endpoint configurations. They will be in no particular order. 92 * endpoint configurations. They will be in no particular order.
93 * @num_altsetting: number of altsettings defined. 93 * @num_altsetting: number of altsettings defined.
94 * @cur_altsetting: the current altsetting. 94 * @cur_altsetting: the current altsetting.
95 * @driver: the USB driver that is bound to this interface. 95 * @driver: the USB driver that is bound to this interface.
96 * @minor: the minor number assigned to this interface, if this 96 * @minor: the minor number assigned to this interface, if this
97 * interface is bound to a driver that uses the USB major number. 97 * interface is bound to a driver that uses the USB major number.
98 * If this interface does not use the USB major, this field should 98 * If this interface does not use the USB major, this field should
99 * be unused. The driver should set this value in the probe() 99 * be unused. The driver should set this value in the probe()
100 * function of the driver, after it has been assigned a minor 100 * function of the driver, after it has been assigned a minor
101 * number from the USB core by calling usb_register_dev(). 101 * number from the USB core by calling usb_register_dev().
102 * @condition: binding state of the interface: not bound, binding 102 * @condition: binding state of the interface: not bound, binding
103 * (in probe()), bound to a driver, or unbinding (in disconnect()) 103 * (in probe()), bound to a driver, or unbinding (in disconnect())
104 * @dev: driver model's view of this device 104 * @dev: driver model's view of this device
105 * @class_dev: driver model's class view of this device. 105 * @class_dev: driver model's class view of this device.
106 * 106 *
107 * USB device drivers attach to interfaces on a physical device. Each 107 * USB device drivers attach to interfaces on a physical device. Each
108 * interface encapsulates a single high level function, such as feeding 108 * interface encapsulates a single high level function, such as feeding
109 * an audio stream to a speaker or reporting a change in a volume control. 109 * an audio stream to a speaker or reporting a change in a volume control.
110 * Many USB devices only have one interface. The protocol used to talk to 110 * Many USB devices only have one interface. The protocol used to talk to
111 * an interface's endpoints can be defined in a usb "class" specification, 111 * an interface's endpoints can be defined in a usb "class" specification,
112 * or by a product's vendor. The (default) control endpoint is part of 112 * or by a product's vendor. The (default) control endpoint is part of
113 * every interface, but is never listed among the interface's descriptors. 113 * every interface, but is never listed among the interface's descriptors.
114 * 114 *
115 * The driver that is bound to the interface can use standard driver model 115 * The driver that is bound to the interface can use standard driver model
116 * calls such as dev_get_drvdata() on the dev member of this structure. 116 * calls such as dev_get_drvdata() on the dev member of this structure.
117 * 117 *
118 * Each interface may have alternate settings. The initial configuration 118 * Each interface may have alternate settings. The initial configuration
119 * of a device sets altsetting 0, but the device driver can change 119 * of a device sets altsetting 0, but the device driver can change
120 * that setting using usb_set_interface(). Alternate settings are often 120 * that setting using usb_set_interface(). Alternate settings are often
121 * used to control the the use of periodic endpoints, such as by having 121 * used to control the the use of periodic endpoints, such as by having
122 * different endpoints use different amounts of reserved USB bandwidth. 122 * different endpoints use different amounts of reserved USB bandwidth.
123 * All standards-conformant USB devices that use isochronous endpoints 123 * All standards-conformant USB devices that use isochronous endpoints
124 * will use them in non-default settings. 124 * will use them in non-default settings.
125 * 125 *
126 * The USB specification says that alternate setting numbers must run from 126 * The USB specification says that alternate setting numbers must run from
127 * 0 to one less than the total number of alternate settings. But some 127 * 0 to one less than the total number of alternate settings. But some
128 * devices manage to mess this up, and the structures aren't necessarily 128 * devices manage to mess this up, and the structures aren't necessarily
129 * stored in numerical order anyhow. Use usb_altnum_to_altsetting() to 129 * stored in numerical order anyhow. Use usb_altnum_to_altsetting() to
130 * look up an alternate setting in the altsetting array based on its number. 130 * look up an alternate setting in the altsetting array based on its number.
131 */ 131 */
132 struct usb_interface { 132 struct usb_interface {
133 /* array of alternate settings for this interface, 133 /* array of alternate settings for this interface,
134 * stored in no particular order */ 134 * stored in no particular order */
135 struct usb_host_interface *altsetting; 135 struct usb_host_interface *altsetting;
136 136
137 struct usb_host_interface *cur_altsetting; /* the currently 137 struct usb_host_interface *cur_altsetting; /* the currently
138 * active alternate setting */ 138 * active alternate setting */
139 unsigned num_altsetting; /* number of alternate settings */ 139 unsigned num_altsetting; /* number of alternate settings */
140 140
141 int minor; /* minor number this interface is 141 int minor; /* minor number this interface is
142 * bound to */ 142 * bound to */
143 enum usb_interface_condition condition; /* state of binding */ 143 enum usb_interface_condition condition; /* state of binding */
144 struct device dev; /* interface specific device info */ 144 struct device dev; /* interface specific device info */
145 struct class_device *class_dev; 145 struct class_device *class_dev;
146 }; 146 };
147 #define to_usb_interface(d) container_of(d, struct usb_interface, dev) 147 #define to_usb_interface(d) container_of(d, struct usb_interface, dev)
148 #define interface_to_usbdev(intf) \ 148 #define interface_to_usbdev(intf) \
149 container_of(intf->dev.parent, struct usb_device, dev) 149 container_of(intf->dev.parent, struct usb_device, dev)
150 150
151 static inline void *usb_get_intfdata (struct usb_interface *intf) 151 static inline void *usb_get_intfdata (struct usb_interface *intf)
152 { 152 {
153 return dev_get_drvdata (&intf->dev); 153 return dev_get_drvdata (&intf->dev);
154 } 154 }
155 155
156 static inline void usb_set_intfdata (struct usb_interface *intf, void *data) 156 static inline void usb_set_intfdata (struct usb_interface *intf, void *data)
157 { 157 {
158 dev_set_drvdata(&intf->dev, data); 158 dev_set_drvdata(&intf->dev, data);
159 } 159 }
160 160
161 struct usb_interface *usb_get_intf(struct usb_interface *intf); 161 struct usb_interface *usb_get_intf(struct usb_interface *intf);
162 void usb_put_intf(struct usb_interface *intf); 162 void usb_put_intf(struct usb_interface *intf);
163 163
164 /* this maximum is arbitrary */ 164 /* this maximum is arbitrary */
165 #define USB_MAXINTERFACES 32 165 #define USB_MAXINTERFACES 32
166 166
167 /** 167 /**
168 * struct usb_interface_cache - long-term representation of a device interface 168 * struct usb_interface_cache - long-term representation of a device interface
169 * @num_altsetting: number of altsettings defined. 169 * @num_altsetting: number of altsettings defined.
170 * @ref: reference counter. 170 * @ref: reference counter.
171 * @altsetting: variable-length array of interface structures, one for 171 * @altsetting: variable-length array of interface structures, one for
172 * each alternate setting that may be selected. Each one includes a 172 * each alternate setting that may be selected. Each one includes a
173 * set of endpoint configurations. They will be in no particular order. 173 * set of endpoint configurations. They will be in no particular order.
174 * 174 *
175 * These structures persist for the lifetime of a usb_device, unlike 175 * These structures persist for the lifetime of a usb_device, unlike
176 * struct usb_interface (which persists only as long as its configuration 176 * struct usb_interface (which persists only as long as its configuration
177 * is installed). The altsetting arrays can be accessed through these 177 * is installed). The altsetting arrays can be accessed through these
178 * structures at any time, permitting comparison of configurations and 178 * structures at any time, permitting comparison of configurations and
179 * providing support for the /proc/bus/usb/devices pseudo-file. 179 * providing support for the /proc/bus/usb/devices pseudo-file.
180 */ 180 */
181 struct usb_interface_cache { 181 struct usb_interface_cache {
182 unsigned num_altsetting; /* number of alternate settings */ 182 unsigned num_altsetting; /* number of alternate settings */
183 struct kref ref; /* reference counter */ 183 struct kref ref; /* reference counter */
184 184
185 /* variable-length array of alternate settings for this interface, 185 /* variable-length array of alternate settings for this interface,
186 * stored in no particular order */ 186 * stored in no particular order */
187 struct usb_host_interface altsetting[0]; 187 struct usb_host_interface altsetting[0];
188 }; 188 };
189 #define ref_to_usb_interface_cache(r) \ 189 #define ref_to_usb_interface_cache(r) \
190 container_of(r, struct usb_interface_cache, ref) 190 container_of(r, struct usb_interface_cache, ref)
191 #define altsetting_to_usb_interface_cache(a) \ 191 #define altsetting_to_usb_interface_cache(a) \
192 container_of(a, struct usb_interface_cache, altsetting[0]) 192 container_of(a, struct usb_interface_cache, altsetting[0])
193 193
194 /** 194 /**
195 * struct usb_host_config - representation of a device's configuration 195 * struct usb_host_config - representation of a device's configuration
196 * @desc: the device's configuration descriptor. 196 * @desc: the device's configuration descriptor.
197 * @string: pointer to the cached version of the iConfiguration string, if 197 * @string: pointer to the cached version of the iConfiguration string, if
198 * present for this configuration. 198 * present for this configuration.
199 * @interface: array of pointers to usb_interface structures, one for each 199 * @interface: array of pointers to usb_interface structures, one for each
200 * interface in the configuration. The number of interfaces is stored 200 * interface in the configuration. The number of interfaces is stored
201 * in desc.bNumInterfaces. These pointers are valid only while the 201 * in desc.bNumInterfaces. These pointers are valid only while the
202 * the configuration is active. 202 * the configuration is active.
203 * @intf_cache: array of pointers to usb_interface_cache structures, one 203 * @intf_cache: array of pointers to usb_interface_cache structures, one
204 * for each interface in the configuration. These structures exist 204 * for each interface in the configuration. These structures exist
205 * for the entire life of the device. 205 * for the entire life of the device.
206 * @extra: pointer to buffer containing all extra descriptors associated 206 * @extra: pointer to buffer containing all extra descriptors associated
207 * with this configuration (those preceding the first interface 207 * with this configuration (those preceding the first interface
208 * descriptor). 208 * descriptor).
209 * @extralen: length of the extra descriptors buffer. 209 * @extralen: length of the extra descriptors buffer.
210 * 210 *
211 * USB devices may have multiple configurations, but only one can be active 211 * USB devices may have multiple configurations, but only one can be active
212 * at any time. Each encapsulates a different operational environment; 212 * at any time. Each encapsulates a different operational environment;
213 * for example, a dual-speed device would have separate configurations for 213 * for example, a dual-speed device would have separate configurations for
214 * full-speed and high-speed operation. The number of configurations 214 * full-speed and high-speed operation. The number of configurations
215 * available is stored in the device descriptor as bNumConfigurations. 215 * available is stored in the device descriptor as bNumConfigurations.
216 * 216 *
217 * A configuration can contain multiple interfaces. Each corresponds to 217 * A configuration can contain multiple interfaces. Each corresponds to
218 * a different function of the USB device, and all are available whenever 218 * a different function of the USB device, and all are available whenever
219 * the configuration is active. The USB standard says that interfaces 219 * the configuration is active. The USB standard says that interfaces
220 * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot 220 * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot
221 * of devices get this wrong. In addition, the interface array is not 221 * of devices get this wrong. In addition, the interface array is not
222 * guaranteed to be sorted in numerical order. Use usb_ifnum_to_if() to 222 * guaranteed to be sorted in numerical order. Use usb_ifnum_to_if() to
223 * look up an interface entry based on its number. 223 * look up an interface entry based on its number.
224 * 224 *
225 * Device drivers should not attempt to activate configurations. The choice 225 * Device drivers should not attempt to activate configurations. The choice
226 * of which configuration to install is a policy decision based on such 226 * of which configuration to install is a policy decision based on such
227 * considerations as available power, functionality provided, and the user's 227 * considerations as available power, functionality provided, and the user's
228 * desires (expressed through hotplug scripts). However, drivers can call 228 * desires (expressed through hotplug scripts). However, drivers can call
229 * usb_reset_configuration() to reinitialize the current configuration and 229 * usb_reset_configuration() to reinitialize the current configuration and
230 * all its interfaces. 230 * all its interfaces.
231 */ 231 */
232 struct usb_host_config { 232 struct usb_host_config {
233 struct usb_config_descriptor desc; 233 struct usb_config_descriptor desc;
234 234
235 char *string; /* iConfiguration string, if present */ 235 char *string; /* iConfiguration string, if present */
236 /* the interfaces associated with this configuration, 236 /* the interfaces associated with this configuration,
237 * stored in no particular order */ 237 * stored in no particular order */
238 struct usb_interface *interface[USB_MAXINTERFACES]; 238 struct usb_interface *interface[USB_MAXINTERFACES];
239 239
240 /* Interface information available even when this is not the 240 /* Interface information available even when this is not the
241 * active configuration */ 241 * active configuration */
242 struct usb_interface_cache *intf_cache[USB_MAXINTERFACES]; 242 struct usb_interface_cache *intf_cache[USB_MAXINTERFACES];
243 243
244 unsigned char *extra; /* Extra descriptors */ 244 unsigned char *extra; /* Extra descriptors */
245 int extralen; 245 int extralen;
246 }; 246 };
247 247
248 int __usb_get_extra_descriptor(char *buffer, unsigned size, 248 int __usb_get_extra_descriptor(char *buffer, unsigned size,
249 unsigned char type, void **ptr); 249 unsigned char type, void **ptr);
250 #define usb_get_extra_descriptor(ifpoint,type,ptr)\ 250 #define usb_get_extra_descriptor(ifpoint,type,ptr)\
251 __usb_get_extra_descriptor((ifpoint)->extra,(ifpoint)->extralen,\ 251 __usb_get_extra_descriptor((ifpoint)->extra,(ifpoint)->extralen,\
252 type,(void**)ptr) 252 type,(void**)ptr)
253 253
254 /* ----------------------------------------------------------------------- */ 254 /* ----------------------------------------------------------------------- */
255 255
256 struct usb_operations; 256 struct usb_operations;
257 257
258 /* USB device number allocation bitmap */ 258 /* USB device number allocation bitmap */
259 struct usb_devmap { 259 struct usb_devmap {
260 unsigned long devicemap[128 / (8*sizeof(unsigned long))]; 260 unsigned long devicemap[128 / (8*sizeof(unsigned long))];
261 }; 261 };
262 262
263 /* 263 /*
264 * Allocated per bus (tree of devices) we have: 264 * Allocated per bus (tree of devices) we have:
265 */ 265 */
266 struct usb_bus { 266 struct usb_bus {
267 struct device *controller; /* host/master side hardware */ 267 struct device *controller; /* host/master side hardware */
268 int busnum; /* Bus number (in order of reg) */ 268 int busnum; /* Bus number (in order of reg) */
269 char *bus_name; /* stable id (PCI slot_name etc) */ 269 char *bus_name; /* stable id (PCI slot_name etc) */
270 u8 otg_port; /* 0, or number of OTG/HNP port */ 270 u8 otg_port; /* 0, or number of OTG/HNP port */
271 unsigned is_b_host:1; /* true during some HNP roleswitches */ 271 unsigned is_b_host:1; /* true during some HNP roleswitches */
272 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */ 272 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
273 273
274 int devnum_next; /* Next open device number in 274 int devnum_next; /* Next open device number in
275 * round-robin allocation */ 275 * round-robin allocation */
276 276
277 struct usb_devmap devmap; /* device address allocation map */ 277 struct usb_devmap devmap; /* device address allocation map */
278 struct usb_operations *op; /* Operations (specific to the HC) */ 278 struct usb_operations *op; /* Operations (specific to the HC) */
279 struct usb_device *root_hub; /* Root hub */ 279 struct usb_device *root_hub; /* Root hub */
280 struct list_head bus_list; /* list of busses */ 280 struct list_head bus_list; /* list of busses */
281 void *hcpriv; /* Host Controller private data */ 281 void *hcpriv; /* Host Controller private data */
282 282
283 int bandwidth_allocated; /* on this bus: how much of the time 283 int bandwidth_allocated; /* on this bus: how much of the time
284 * reserved for periodic (intr/iso) 284 * reserved for periodic (intr/iso)
285 * requests is used, on average? 285 * requests is used, on average?
286 * Units: microseconds/frame. 286 * Units: microseconds/frame.
287 * Limits: Full/low speed reserve 90%, 287 * Limits: Full/low speed reserve 90%,
288 * while high speed reserves 80%. 288 * while high speed reserves 80%.
289 */ 289 */
290 int bandwidth_int_reqs; /* number of Interrupt requests */ 290 int bandwidth_int_reqs; /* number of Interrupt requests */
291 int bandwidth_isoc_reqs; /* number of Isoc. requests */ 291 int bandwidth_isoc_reqs; /* number of Isoc. requests */
292 292
293 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */ 293 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */
294 294
295 struct class_device *class_dev; /* class device for this bus */ 295 struct class_device *class_dev; /* class device for this bus */
296 struct kref kref; /* reference counting for this bus */ 296 struct kref kref; /* reference counting for this bus */
297 void (*release)(struct usb_bus *bus); 297 void (*release)(struct usb_bus *bus);
298 298
299 #if defined(CONFIG_USB_MON) 299 #if defined(CONFIG_USB_MON)
300 struct mon_bus *mon_bus; /* non-null when associated */ 300 struct mon_bus *mon_bus; /* non-null when associated */
301 int monitored; /* non-zero when monitored */ 301 int monitored; /* non-zero when monitored */
302 #endif 302 #endif
303 }; 303 };
304 304
305 /* ----------------------------------------------------------------------- */ 305 /* ----------------------------------------------------------------------- */
306 306
307 /* This is arbitrary. 307 /* This is arbitrary.
308 * From USB 2.0 spec Table 11-13, offset 7, a hub can 308 * From USB 2.0 spec Table 11-13, offset 7, a hub can
309 * have up to 255 ports. The most yet reported is 10. 309 * have up to 255 ports. The most yet reported is 10.
310 */ 310 */
311 #define USB_MAXCHILDREN (16) 311 #define USB_MAXCHILDREN (16)
312 312
313 struct usb_tt; 313 struct usb_tt;
314 314
315 /* 315 /*
316 * struct usb_device - kernel's representation of a USB device 316 * struct usb_device - kernel's representation of a USB device
317 * 317 *
318 * FIXME: Write the kerneldoc! 318 * FIXME: Write the kerneldoc!
319 * 319 *
320 * Usbcore drivers should not set usbdev->state directly. Instead use 320 * Usbcore drivers should not set usbdev->state directly. Instead use
321 * usb_set_device_state(). 321 * usb_set_device_state().
322 */ 322 */
323 struct usb_device { 323 struct usb_device {
324 int devnum; /* Address on USB bus */ 324 int devnum; /* Address on USB bus */
325 char devpath [16]; /* Use in messages: /port/port/... */ 325 char devpath [16]; /* Use in messages: /port/port/... */
326 enum usb_device_state state; /* configured, not attached, etc */ 326 enum usb_device_state state; /* configured, not attached, etc */
327 enum usb_device_speed speed; /* high/full/low (or error) */ 327 enum usb_device_speed speed; /* high/full/low (or error) */
328 328
329 struct usb_tt *tt; /* low/full speed dev, highspeed hub */ 329 struct usb_tt *tt; /* low/full speed dev, highspeed hub */
330 int ttport; /* device port on that tt hub */ 330 int ttport; /* device port on that tt hub */
331 331
332 unsigned int toggle[2]; /* one bit for each endpoint 332 unsigned int toggle[2]; /* one bit for each endpoint
333 * ([0] = IN, [1] = OUT) */ 333 * ([0] = IN, [1] = OUT) */
334 334
335 struct usb_device *parent; /* our hub, unless we're the root */ 335 struct usb_device *parent; /* our hub, unless we're the root */
336 struct usb_bus *bus; /* Bus we're part of */ 336 struct usb_bus *bus; /* Bus we're part of */
337 struct usb_host_endpoint ep0; 337 struct usb_host_endpoint ep0;
338 338
339 struct device dev; /* Generic device interface */ 339 struct device dev; /* Generic device interface */
340 340
341 struct usb_device_descriptor descriptor;/* Descriptor */ 341 struct usb_device_descriptor descriptor;/* Descriptor */
342 struct usb_host_config *config; /* All of the configs */ 342 struct usb_host_config *config; /* All of the configs */
343 343
344 struct usb_host_config *actconfig;/* the active configuration */ 344 struct usb_host_config *actconfig;/* the active configuration */
345 struct usb_host_endpoint *ep_in[16]; 345 struct usb_host_endpoint *ep_in[16];
346 struct usb_host_endpoint *ep_out[16]; 346 struct usb_host_endpoint *ep_out[16];
347 347
348 char **rawdescriptors; /* Raw descriptors for each config */ 348 char **rawdescriptors; /* Raw descriptors for each config */
349 349
350 unsigned short bus_mA; /* Current available from the bus */
351
350 int have_langid; /* whether string_langid is valid */ 352 int have_langid; /* whether string_langid is valid */
351 int string_langid; /* language ID for strings */ 353 int string_langid; /* language ID for strings */
352 354
353 /* static strings from the device */ 355 /* static strings from the device */
354 char *product; /* iProduct string, if present */ 356 char *product; /* iProduct string, if present */
355 char *manufacturer; /* iManufacturer string, if present */ 357 char *manufacturer; /* iManufacturer string, if present */
356 char *serial; /* iSerialNumber string, if present */ 358 char *serial; /* iSerialNumber string, if present */
357 359
358 struct list_head filelist; 360 struct list_head filelist;
359 struct class_device *class_dev; 361 struct class_device *class_dev;
360 struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */ 362 struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */
361 363
362 /* 364 /*
363 * Child devices - these can be either new devices 365 * Child devices - these can be either new devices
364 * (if this is a hub device), or different instances 366 * (if this is a hub device), or different instances
365 * of this same device. 367 * of this same device.
366 * 368 *
367 * Each instance needs its own set of data structures. 369 * Each instance needs its own set of data structures.
368 */ 370 */
369 371
370 int maxchild; /* Number of ports if hub */ 372 int maxchild; /* Number of ports if hub */
371 struct usb_device *children[USB_MAXCHILDREN]; 373 struct usb_device *children[USB_MAXCHILDREN];
372 }; 374 };
373 #define to_usb_device(d) container_of(d, struct usb_device, dev) 375 #define to_usb_device(d) container_of(d, struct usb_device, dev)
374 376
375 extern struct usb_device *usb_get_dev(struct usb_device *dev); 377 extern struct usb_device *usb_get_dev(struct usb_device *dev);
376 extern void usb_put_dev(struct usb_device *dev); 378 extern void usb_put_dev(struct usb_device *dev);
377 379
378 /* USB device locking */ 380 /* USB device locking */
379 #define usb_lock_device(udev) down(&(udev)->dev.sem) 381 #define usb_lock_device(udev) down(&(udev)->dev.sem)
380 #define usb_unlock_device(udev) up(&(udev)->dev.sem) 382 #define usb_unlock_device(udev) up(&(udev)->dev.sem)
381 #define usb_trylock_device(udev) down_trylock(&(udev)->dev.sem) 383 #define usb_trylock_device(udev) down_trylock(&(udev)->dev.sem)
382 extern int usb_lock_device_for_reset(struct usb_device *udev, 384 extern int usb_lock_device_for_reset(struct usb_device *udev,
383 struct usb_interface *iface); 385 struct usb_interface *iface);
384 386
385 /* USB port reset for device reinitialization */ 387 /* USB port reset for device reinitialization */
386 extern int usb_reset_device(struct usb_device *dev); 388 extern int usb_reset_device(struct usb_device *dev);
387 389
388 extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id); 390 extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id);
389 391
390 /*-------------------------------------------------------------------------*/ 392 /*-------------------------------------------------------------------------*/
391 393
392 /* for drivers using iso endpoints */ 394 /* for drivers using iso endpoints */
393 extern int usb_get_current_frame_number (struct usb_device *usb_dev); 395 extern int usb_get_current_frame_number (struct usb_device *usb_dev);
394 396
395 /* used these for multi-interface device registration */ 397 /* used these for multi-interface device registration */
396 extern int usb_driver_claim_interface(struct usb_driver *driver, 398 extern int usb_driver_claim_interface(struct usb_driver *driver,
397 struct usb_interface *iface, void* priv); 399 struct usb_interface *iface, void* priv);
398 400
399 /** 401 /**
400 * usb_interface_claimed - returns true iff an interface is claimed 402 * usb_interface_claimed - returns true iff an interface is claimed
401 * @iface: the interface being checked 403 * @iface: the interface being checked
402 * 404 *
403 * Returns true (nonzero) iff the interface is claimed, else false (zero). 405 * Returns true (nonzero) iff the interface is claimed, else false (zero).
404 * Callers must own the driver model's usb bus readlock. So driver 406 * Callers must own the driver model's usb bus readlock. So driver
405 * probe() entries don't need extra locking, but other call contexts 407 * probe() entries don't need extra locking, but other call contexts
406 * may need to explicitly claim that lock. 408 * may need to explicitly claim that lock.
407 * 409 *
408 */ 410 */
409 static inline int usb_interface_claimed(struct usb_interface *iface) { 411 static inline int usb_interface_claimed(struct usb_interface *iface) {
410 return (iface->dev.driver != NULL); 412 return (iface->dev.driver != NULL);
411 } 413 }
412 414
413 extern void usb_driver_release_interface(struct usb_driver *driver, 415 extern void usb_driver_release_interface(struct usb_driver *driver,
414 struct usb_interface *iface); 416 struct usb_interface *iface);
415 const struct usb_device_id *usb_match_id(struct usb_interface *interface, 417 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
416 const struct usb_device_id *id); 418 const struct usb_device_id *id);
417 419
418 extern struct usb_interface *usb_find_interface(struct usb_driver *drv, 420 extern struct usb_interface *usb_find_interface(struct usb_driver *drv,
419 int minor); 421 int minor);
420 extern struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, 422 extern struct usb_interface *usb_ifnum_to_if(struct usb_device *dev,
421 unsigned ifnum); 423 unsigned ifnum);
422 extern struct usb_host_interface *usb_altnum_to_altsetting( 424 extern struct usb_host_interface *usb_altnum_to_altsetting(
423 struct usb_interface *intf, unsigned int altnum); 425 struct usb_interface *intf, unsigned int altnum);
424 426
425 427
426 /** 428 /**
427 * usb_make_path - returns stable device path in the usb tree 429 * usb_make_path - returns stable device path in the usb tree
428 * @dev: the device whose path is being constructed 430 * @dev: the device whose path is being constructed
429 * @buf: where to put the string 431 * @buf: where to put the string
430 * @size: how big is "buf"? 432 * @size: how big is "buf"?
431 * 433 *
432 * Returns length of the string (> 0) or negative if size was too small. 434 * Returns length of the string (> 0) or negative if size was too small.
433 * 435 *
434 * This identifier is intended to be "stable", reflecting physical paths in 436 * This identifier is intended to be "stable", reflecting physical paths in
435 * hardware such as physical bus addresses for host controllers or ports on 437 * hardware such as physical bus addresses for host controllers or ports on
436 * USB hubs. That makes it stay the same until systems are physically 438 * USB hubs. That makes it stay the same until systems are physically
437 * reconfigured, by re-cabling a tree of USB devices or by moving USB host 439 * reconfigured, by re-cabling a tree of USB devices or by moving USB host
438 * controllers. Adding and removing devices, including virtual root hubs 440 * controllers. Adding and removing devices, including virtual root hubs
439 * in host controller driver modules, does not change these path identifers; 441 * in host controller driver modules, does not change these path identifers;
440 * neither does rebooting or re-enumerating. These are more useful identifiers 442 * neither does rebooting or re-enumerating. These are more useful identifiers
441 * than changeable ("unstable") ones like bus numbers or device addresses. 443 * than changeable ("unstable") ones like bus numbers or device addresses.
442 * 444 *
443 * With a partial exception for devices connected to USB 2.0 root hubs, these 445 * With a partial exception for devices connected to USB 2.0 root hubs, these
444 * identifiers are also predictable. So long as the device tree isn't changed, 446 * identifiers are also predictable. So long as the device tree isn't changed,
445 * plugging any USB device into a given hub port always gives it the same path. 447 * plugging any USB device into a given hub port always gives it the same path.
446 * Because of the use of "companion" controllers, devices connected to ports on 448 * Because of the use of "companion" controllers, devices connected to ports on
447 * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are 449 * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are
448 * high speed, and a different one if they are full or low speed. 450 * high speed, and a different one if they are full or low speed.
449 */ 451 */
450 static inline int usb_make_path (struct usb_device *dev, char *buf, 452 static inline int usb_make_path (struct usb_device *dev, char *buf,
451 size_t size) 453 size_t size)
452 { 454 {
453 int actual; 455 int actual;
454 actual = snprintf (buf, size, "usb-%s-%s", dev->bus->bus_name, 456 actual = snprintf (buf, size, "usb-%s-%s", dev->bus->bus_name,
455 dev->devpath); 457 dev->devpath);
456 return (actual >= (int)size) ? -1 : actual; 458 return (actual >= (int)size) ? -1 : actual;
457 } 459 }
458 460
459 /*-------------------------------------------------------------------------*/ 461 /*-------------------------------------------------------------------------*/
460 462
461 #define USB_DEVICE_ID_MATCH_DEVICE \ 463 #define USB_DEVICE_ID_MATCH_DEVICE \
462 (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT) 464 (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
463 #define USB_DEVICE_ID_MATCH_DEV_RANGE \ 465 #define USB_DEVICE_ID_MATCH_DEV_RANGE \
464 (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI) 466 (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
465 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \ 467 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
466 (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE) 468 (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
467 #define USB_DEVICE_ID_MATCH_DEV_INFO \ 469 #define USB_DEVICE_ID_MATCH_DEV_INFO \
468 (USB_DEVICE_ID_MATCH_DEV_CLASS | \ 470 (USB_DEVICE_ID_MATCH_DEV_CLASS | \
469 USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \ 471 USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \
470 USB_DEVICE_ID_MATCH_DEV_PROTOCOL) 472 USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
471 #define USB_DEVICE_ID_MATCH_INT_INFO \ 473 #define USB_DEVICE_ID_MATCH_INT_INFO \
472 (USB_DEVICE_ID_MATCH_INT_CLASS | \ 474 (USB_DEVICE_ID_MATCH_INT_CLASS | \
473 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 475 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \
474 USB_DEVICE_ID_MATCH_INT_PROTOCOL) 476 USB_DEVICE_ID_MATCH_INT_PROTOCOL)
475 477
476 /** 478 /**
477 * USB_DEVICE - macro used to describe a specific usb device 479 * USB_DEVICE - macro used to describe a specific usb device
478 * @vend: the 16 bit USB Vendor ID 480 * @vend: the 16 bit USB Vendor ID
479 * @prod: the 16 bit USB Product ID 481 * @prod: the 16 bit USB Product ID
480 * 482 *
481 * This macro is used to create a struct usb_device_id that matches a 483 * This macro is used to create a struct usb_device_id that matches a
482 * specific device. 484 * specific device.
483 */ 485 */
484 #define USB_DEVICE(vend,prod) \ 486 #define USB_DEVICE(vend,prod) \
485 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \ 487 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
486 .idProduct = (prod) 488 .idProduct = (prod)
487 /** 489 /**
488 * USB_DEVICE_VER - macro used to describe a specific usb device with a 490 * USB_DEVICE_VER - macro used to describe a specific usb device with a
489 * version range 491 * version range
490 * @vend: the 16 bit USB Vendor ID 492 * @vend: the 16 bit USB Vendor ID
491 * @prod: the 16 bit USB Product ID 493 * @prod: the 16 bit USB Product ID
492 * @lo: the bcdDevice_lo value 494 * @lo: the bcdDevice_lo value
493 * @hi: the bcdDevice_hi value 495 * @hi: the bcdDevice_hi value
494 * 496 *
495 * This macro is used to create a struct usb_device_id that matches a 497 * This macro is used to create a struct usb_device_id that matches a
496 * specific device, with a version range. 498 * specific device, with a version range.
497 */ 499 */
498 #define USB_DEVICE_VER(vend,prod,lo,hi) \ 500 #define USB_DEVICE_VER(vend,prod,lo,hi) \
499 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \ 501 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \
500 .idVendor = (vend), .idProduct = (prod), \ 502 .idVendor = (vend), .idProduct = (prod), \
501 .bcdDevice_lo = (lo), .bcdDevice_hi = (hi) 503 .bcdDevice_lo = (lo), .bcdDevice_hi = (hi)
502 504
503 /** 505 /**
504 * USB_DEVICE_INFO - macro used to describe a class of usb devices 506 * USB_DEVICE_INFO - macro used to describe a class of usb devices
505 * @cl: bDeviceClass value 507 * @cl: bDeviceClass value
506 * @sc: bDeviceSubClass value 508 * @sc: bDeviceSubClass value
507 * @pr: bDeviceProtocol value 509 * @pr: bDeviceProtocol value
508 * 510 *
509 * This macro is used to create a struct usb_device_id that matches a 511 * This macro is used to create a struct usb_device_id that matches a
510 * specific class of devices. 512 * specific class of devices.
511 */ 513 */
512 #define USB_DEVICE_INFO(cl,sc,pr) \ 514 #define USB_DEVICE_INFO(cl,sc,pr) \
513 .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, .bDeviceClass = (cl), \ 515 .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, .bDeviceClass = (cl), \
514 .bDeviceSubClass = (sc), .bDeviceProtocol = (pr) 516 .bDeviceSubClass = (sc), .bDeviceProtocol = (pr)
515 517
516 /** 518 /**
517 * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces 519 * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces
518 * @cl: bInterfaceClass value 520 * @cl: bInterfaceClass value
519 * @sc: bInterfaceSubClass value 521 * @sc: bInterfaceSubClass value
520 * @pr: bInterfaceProtocol value 522 * @pr: bInterfaceProtocol value
521 * 523 *
522 * This macro is used to create a struct usb_device_id that matches a 524 * This macro is used to create a struct usb_device_id that matches a
523 * specific class of interfaces. 525 * specific class of interfaces.
524 */ 526 */
525 #define USB_INTERFACE_INFO(cl,sc,pr) \ 527 #define USB_INTERFACE_INFO(cl,sc,pr) \
526 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, .bInterfaceClass = (cl), \ 528 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, .bInterfaceClass = (cl), \
527 .bInterfaceSubClass = (sc), .bInterfaceProtocol = (pr) 529 .bInterfaceSubClass = (sc), .bInterfaceProtocol = (pr)
528 530
529 /* ----------------------------------------------------------------------- */ 531 /* ----------------------------------------------------------------------- */
530 532
531 struct usb_dynids { 533 struct usb_dynids {
532 spinlock_t lock; 534 spinlock_t lock;
533 struct list_head list; 535 struct list_head list;
534 }; 536 };
535 537
536 /** 538 /**
537 * struct usb_driver - identifies USB driver to usbcore 539 * struct usb_driver - identifies USB driver to usbcore
538 * @name: The driver name should be unique among USB drivers, 540 * @name: The driver name should be unique among USB drivers,
539 * and should normally be the same as the module name. 541 * and should normally be the same as the module name.
540 * @probe: Called to see if the driver is willing to manage a particular 542 * @probe: Called to see if the driver is willing to manage a particular
541 * interface on a device. If it is, probe returns zero and uses 543 * interface on a device. If it is, probe returns zero and uses
542 * dev_set_drvdata() to associate driver-specific data with the 544 * dev_set_drvdata() to associate driver-specific data with the
543 * interface. It may also use usb_set_interface() to specify the 545 * interface. It may also use usb_set_interface() to specify the
544 * appropriate altsetting. If unwilling to manage the interface, 546 * appropriate altsetting. If unwilling to manage the interface,
545 * return a negative errno value. 547 * return a negative errno value.
546 * @disconnect: Called when the interface is no longer accessible, usually 548 * @disconnect: Called when the interface is no longer accessible, usually
547 * because its device has been (or is being) disconnected or the 549 * because its device has been (or is being) disconnected or the
548 * driver module is being unloaded. 550 * driver module is being unloaded.
549 * @ioctl: Used for drivers that want to talk to userspace through 551 * @ioctl: Used for drivers that want to talk to userspace through
550 * the "usbfs" filesystem. This lets devices provide ways to 552 * the "usbfs" filesystem. This lets devices provide ways to
551 * expose information to user space regardless of where they 553 * expose information to user space regardless of where they
552 * do (or don't) show up otherwise in the filesystem. 554 * do (or don't) show up otherwise in the filesystem.
553 * @suspend: Called when the device is going to be suspended by the system. 555 * @suspend: Called when the device is going to be suspended by the system.
554 * @resume: Called when the device is being resumed by the system. 556 * @resume: Called when the device is being resumed by the system.
555 * @id_table: USB drivers use ID table to support hotplugging. 557 * @id_table: USB drivers use ID table to support hotplugging.
556 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set 558 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
557 * or your driver's probe function will never get called. 559 * or your driver's probe function will never get called.
558 * @dynids: used internally to hold the list of dynamically added device 560 * @dynids: used internally to hold the list of dynamically added device
559 * ids for this driver. 561 * ids for this driver.
560 * @driver: the driver model core driver structure. 562 * @driver: the driver model core driver structure.
561 * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be 563 * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be
562 * added to this driver by preventing the sysfs file from being created. 564 * added to this driver by preventing the sysfs file from being created.
563 * 565 *
564 * USB drivers must provide a name, probe() and disconnect() methods, 566 * USB drivers must provide a name, probe() and disconnect() methods,
565 * and an id_table. Other driver fields are optional. 567 * and an id_table. Other driver fields are optional.
566 * 568 *
567 * The id_table is used in hotplugging. It holds a set of descriptors, 569 * The id_table is used in hotplugging. It holds a set of descriptors,
568 * and specialized data may be associated with each entry. That table 570 * and specialized data may be associated with each entry. That table
569 * is used by both user and kernel mode hotplugging support. 571 * is used by both user and kernel mode hotplugging support.
570 * 572 *
571 * The probe() and disconnect() methods are called in a context where 573 * The probe() and disconnect() methods are called in a context where
572 * they can sleep, but they should avoid abusing the privilege. Most 574 * they can sleep, but they should avoid abusing the privilege. Most
573 * work to connect to a device should be done when the device is opened, 575 * work to connect to a device should be done when the device is opened,
574 * and undone at the last close. The disconnect code needs to address 576 * and undone at the last close. The disconnect code needs to address
575 * concurrency issues with respect to open() and close() methods, as 577 * concurrency issues with respect to open() and close() methods, as
576 * well as forcing all pending I/O requests to complete (by unlinking 578 * well as forcing all pending I/O requests to complete (by unlinking
577 * them as necessary, and blocking until the unlinks complete). 579 * them as necessary, and blocking until the unlinks complete).
578 */ 580 */
579 struct usb_driver { 581 struct usb_driver {
580 const char *name; 582 const char *name;
581 583
582 int (*probe) (struct usb_interface *intf, 584 int (*probe) (struct usb_interface *intf,
583 const struct usb_device_id *id); 585 const struct usb_device_id *id);
584 586
585 void (*disconnect) (struct usb_interface *intf); 587 void (*disconnect) (struct usb_interface *intf);
586 588
587 int (*ioctl) (struct usb_interface *intf, unsigned int code, 589 int (*ioctl) (struct usb_interface *intf, unsigned int code,
588 void *buf); 590 void *buf);
589 591
590 int (*suspend) (struct usb_interface *intf, pm_message_t message); 592 int (*suspend) (struct usb_interface *intf, pm_message_t message);
591 int (*resume) (struct usb_interface *intf); 593 int (*resume) (struct usb_interface *intf);
592 594
593 const struct usb_device_id *id_table; 595 const struct usb_device_id *id_table;
594 596
595 struct usb_dynids dynids; 597 struct usb_dynids dynids;
596 struct device_driver driver; 598 struct device_driver driver;
597 unsigned int no_dynamic_id:1; 599 unsigned int no_dynamic_id:1;
598 }; 600 };
599 #define to_usb_driver(d) container_of(d, struct usb_driver, driver) 601 #define to_usb_driver(d) container_of(d, struct usb_driver, driver)
600 602
601 extern struct bus_type usb_bus_type; 603 extern struct bus_type usb_bus_type;
602 604
603 /** 605 /**
604 * struct usb_class_driver - identifies a USB driver that wants to use the USB major number 606 * struct usb_class_driver - identifies a USB driver that wants to use the USB major number
605 * @name: the usb class device name for this driver. Will show up in sysfs. 607 * @name: the usb class device name for this driver. Will show up in sysfs.
606 * @fops: pointer to the struct file_operations of this driver. 608 * @fops: pointer to the struct file_operations of this driver.
607 * @minor_base: the start of the minor range for this driver. 609 * @minor_base: the start of the minor range for this driver.
608 * 610 *
609 * This structure is used for the usb_register_dev() and 611 * This structure is used for the usb_register_dev() and
610 * usb_unregister_dev() functions, to consolidate a number of the 612 * usb_unregister_dev() functions, to consolidate a number of the
611 * parameters used for them. 613 * parameters used for them.
612 */ 614 */
613 struct usb_class_driver { 615 struct usb_class_driver {
614 char *name; 616 char *name;
615 struct file_operations *fops; 617 struct file_operations *fops;
616 int minor_base; 618 int minor_base;
617 }; 619 };
618 620
619 /* 621 /*
620 * use these in module_init()/module_exit() 622 * use these in module_init()/module_exit()
621 * and don't forget MODULE_DEVICE_TABLE(usb, ...) 623 * and don't forget MODULE_DEVICE_TABLE(usb, ...)
622 */ 624 */
623 int usb_register_driver(struct usb_driver *, struct module *); 625 int usb_register_driver(struct usb_driver *, struct module *);
624 static inline int usb_register(struct usb_driver *driver) 626 static inline int usb_register(struct usb_driver *driver)
625 { 627 {
626 return usb_register_driver(driver, THIS_MODULE); 628 return usb_register_driver(driver, THIS_MODULE);
627 } 629 }
628 extern void usb_deregister(struct usb_driver *); 630 extern void usb_deregister(struct usb_driver *);
629 631
630 extern int usb_register_dev(struct usb_interface *intf, 632 extern int usb_register_dev(struct usb_interface *intf,
631 struct usb_class_driver *class_driver); 633 struct usb_class_driver *class_driver);
632 extern void usb_deregister_dev(struct usb_interface *intf, 634 extern void usb_deregister_dev(struct usb_interface *intf,
633 struct usb_class_driver *class_driver); 635 struct usb_class_driver *class_driver);
634 636
635 extern int usb_disabled(void); 637 extern int usb_disabled(void);
636 638
637 /* ----------------------------------------------------------------------- */ 639 /* ----------------------------------------------------------------------- */
638 640
639 /* 641 /*
640 * URB support, for asynchronous request completions 642 * URB support, for asynchronous request completions
641 */ 643 */
642 644
643 /* 645 /*
644 * urb->transfer_flags: 646 * urb->transfer_flags:
645 */ 647 */
646 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */ 648 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
647 #define URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame 649 #define URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame
648 * ignored */ 650 * ignored */
649 #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */ 651 #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
650 #define URB_NO_SETUP_DMA_MAP 0x0008 /* urb->setup_dma valid on submit */ 652 #define URB_NO_SETUP_DMA_MAP 0x0008 /* urb->setup_dma valid on submit */
651 #define URB_NO_FSBR 0x0020 /* UHCI-specific */ 653 #define URB_NO_FSBR 0x0020 /* UHCI-specific */
652 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */ 654 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
653 #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt 655 #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt
654 * needed */ 656 * needed */
655 657
656 struct usb_iso_packet_descriptor { 658 struct usb_iso_packet_descriptor {
657 unsigned int offset; 659 unsigned int offset;
658 unsigned int length; /* expected length */ 660 unsigned int length; /* expected length */
659 unsigned int actual_length; 661 unsigned int actual_length;
660 unsigned int status; 662 unsigned int status;
661 }; 663 };
662 664
663 struct urb; 665 struct urb;
664 struct pt_regs; 666 struct pt_regs;
665 667
666 typedef void (*usb_complete_t)(struct urb *, struct pt_regs *); 668 typedef void (*usb_complete_t)(struct urb *, struct pt_regs *);
667 669
668 /** 670 /**
669 * struct urb - USB Request Block 671 * struct urb - USB Request Block
670 * @urb_list: For use by current owner of the URB. 672 * @urb_list: For use by current owner of the URB.
671 * @pipe: Holds endpoint number, direction, type, and more. 673 * @pipe: Holds endpoint number, direction, type, and more.
672 * Create these values with the eight macros available; 674 * Create these values with the eight macros available;
673 * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl" 675 * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl"
674 * (control), "bulk", "int" (interrupt), or "iso" (isochronous). 676 * (control), "bulk", "int" (interrupt), or "iso" (isochronous).
675 * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint 677 * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint
676 * numbers range from zero to fifteen. Note that "in" endpoint two 678 * numbers range from zero to fifteen. Note that "in" endpoint two
677 * is a different endpoint (and pipe) from "out" endpoint two. 679 * is a different endpoint (and pipe) from "out" endpoint two.
678 * The current configuration controls the existence, type, and 680 * The current configuration controls the existence, type, and
679 * maximum packet size of any given endpoint. 681 * maximum packet size of any given endpoint.
680 * @dev: Identifies the USB device to perform the request. 682 * @dev: Identifies the USB device to perform the request.
681 * @status: This is read in non-iso completion functions to get the 683 * @status: This is read in non-iso completion functions to get the
682 * status of the particular request. ISO requests only use it 684 * status of the particular request. ISO requests only use it
683 * to tell whether the URB was unlinked; detailed status for 685 * to tell whether the URB was unlinked; detailed status for
684 * each frame is in the fields of the iso_frame-desc. 686 * each frame is in the fields of the iso_frame-desc.
685 * @transfer_flags: A variety of flags may be used to affect how URB 687 * @transfer_flags: A variety of flags may be used to affect how URB
686 * submission, unlinking, or operation are handled. Different 688 * submission, unlinking, or operation are handled. Different
687 * kinds of URB can use different flags. 689 * kinds of URB can use different flags.
688 * @transfer_buffer: This identifies the buffer to (or from) which 690 * @transfer_buffer: This identifies the buffer to (or from) which
689 * the I/O request will be performed (unless URB_NO_TRANSFER_DMA_MAP 691 * the I/O request will be performed (unless URB_NO_TRANSFER_DMA_MAP
690 * is set). This buffer must be suitable for DMA; allocate it with 692 * is set). This buffer must be suitable for DMA; allocate it with
691 * kmalloc() or equivalent. For transfers to "in" endpoints, contents 693 * kmalloc() or equivalent. For transfers to "in" endpoints, contents
692 * of this buffer will be modified. This buffer is used for the data 694 * of this buffer will be modified. This buffer is used for the data
693 * stage of control transfers. 695 * stage of control transfers.
694 * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP, 696 * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP,
695 * the device driver is saying that it provided this DMA address, 697 * the device driver is saying that it provided this DMA address,
696 * which the host controller driver should use in preference to the 698 * which the host controller driver should use in preference to the
697 * transfer_buffer. 699 * transfer_buffer.
698 * @transfer_buffer_length: How big is transfer_buffer. The transfer may 700 * @transfer_buffer_length: How big is transfer_buffer. The transfer may
699 * be broken up into chunks according to the current maximum packet 701 * be broken up into chunks according to the current maximum packet
700 * size for the endpoint, which is a function of the configuration 702 * size for the endpoint, which is a function of the configuration
701 * and is encoded in the pipe. When the length is zero, neither 703 * and is encoded in the pipe. When the length is zero, neither
702 * transfer_buffer nor transfer_dma is used. 704 * transfer_buffer nor transfer_dma is used.
703 * @actual_length: This is read in non-iso completion functions, and 705 * @actual_length: This is read in non-iso completion functions, and
704 * it tells how many bytes (out of transfer_buffer_length) were 706 * it tells how many bytes (out of transfer_buffer_length) were
705 * transferred. It will normally be the same as requested, unless 707 * transferred. It will normally be the same as requested, unless
706 * either an error was reported or a short read was performed. 708 * either an error was reported or a short read was performed.
707 * The URB_SHORT_NOT_OK transfer flag may be used to make such 709 * The URB_SHORT_NOT_OK transfer flag may be used to make such
708 * short reads be reported as errors. 710 * short reads be reported as errors.
709 * @setup_packet: Only used for control transfers, this points to eight bytes 711 * @setup_packet: Only used for control transfers, this points to eight bytes
710 * of setup data. Control transfers always start by sending this data 712 * of setup data. Control transfers always start by sending this data
711 * to the device. Then transfer_buffer is read or written, if needed. 713 * to the device. Then transfer_buffer is read or written, if needed.
712 * @setup_dma: For control transfers with URB_NO_SETUP_DMA_MAP set, the 714 * @setup_dma: For control transfers with URB_NO_SETUP_DMA_MAP set, the
713 * device driver has provided this DMA address for the setup packet. 715 * device driver has provided this DMA address for the setup packet.
714 * The host controller driver should use this in preference to 716 * The host controller driver should use this in preference to
715 * setup_packet. 717 * setup_packet.
716 * @start_frame: Returns the initial frame for isochronous transfers. 718 * @start_frame: Returns the initial frame for isochronous transfers.
717 * @number_of_packets: Lists the number of ISO transfer buffers. 719 * @number_of_packets: Lists the number of ISO transfer buffers.
718 * @interval: Specifies the polling interval for interrupt or isochronous 720 * @interval: Specifies the polling interval for interrupt or isochronous
719 * transfers. The units are frames (milliseconds) for for full and low 721 * transfers. The units are frames (milliseconds) for for full and low
720 * speed devices, and microframes (1/8 millisecond) for highspeed ones. 722 * speed devices, and microframes (1/8 millisecond) for highspeed ones.
721 * @error_count: Returns the number of ISO transfers that reported errors. 723 * @error_count: Returns the number of ISO transfers that reported errors.
722 * @context: For use in completion functions. This normally points to 724 * @context: For use in completion functions. This normally points to
723 * request-specific driver context. 725 * request-specific driver context.
724 * @complete: Completion handler. This URB is passed as the parameter to the 726 * @complete: Completion handler. This URB is passed as the parameter to the
725 * completion function. The completion function may then do what 727 * completion function. The completion function may then do what
726 * it likes with the URB, including resubmitting or freeing it. 728 * it likes with the URB, including resubmitting or freeing it.
727 * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to 729 * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to
728 * collect the transfer status for each buffer. 730 * collect the transfer status for each buffer.
729 * 731 *
730 * This structure identifies USB transfer requests. URBs must be allocated by 732 * This structure identifies USB transfer requests. URBs must be allocated by
731 * calling usb_alloc_urb() and freed with a call to usb_free_urb(). 733 * calling usb_alloc_urb() and freed with a call to usb_free_urb().
732 * Initialization may be done using various usb_fill_*_urb() functions. URBs 734 * Initialization may be done using various usb_fill_*_urb() functions. URBs
733 * are submitted using usb_submit_urb(), and pending requests may be canceled 735 * are submitted using usb_submit_urb(), and pending requests may be canceled
734 * using usb_unlink_urb() or usb_kill_urb(). 736 * using usb_unlink_urb() or usb_kill_urb().
735 * 737 *
736 * Data Transfer Buffers: 738 * Data Transfer Buffers:
737 * 739 *
738 * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise 740 * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise
739 * taken from the general page pool. That is provided by transfer_buffer 741 * taken from the general page pool. That is provided by transfer_buffer
740 * (control requests also use setup_packet), and host controller drivers 742 * (control requests also use setup_packet), and host controller drivers
741 * perform a dma mapping (and unmapping) for each buffer transferred. Those 743 * perform a dma mapping (and unmapping) for each buffer transferred. Those
742 * mapping operations can be expensive on some platforms (perhaps using a dma 744 * mapping operations can be expensive on some platforms (perhaps using a dma
743 * bounce buffer or talking to an IOMMU), 745 * bounce buffer or talking to an IOMMU),
744 * although they're cheap on commodity x86 and ppc hardware. 746 * although they're cheap on commodity x86 and ppc hardware.
745 * 747 *
746 * Alternatively, drivers may pass the URB_NO_xxx_DMA_MAP transfer flags, 748 * Alternatively, drivers may pass the URB_NO_xxx_DMA_MAP transfer flags,
747 * which tell the host controller driver that no such mapping is needed since 749 * which tell the host controller driver that no such mapping is needed since
748 * the device driver is DMA-aware. For example, a device driver might 750 * the device driver is DMA-aware. For example, a device driver might
749 * allocate a DMA buffer with usb_buffer_alloc() or call usb_buffer_map(). 751 * allocate a DMA buffer with usb_buffer_alloc() or call usb_buffer_map().
750 * When these transfer flags are provided, host controller drivers will 752 * When these transfer flags are provided, host controller drivers will
751 * attempt to use the dma addresses found in the transfer_dma and/or 753 * attempt to use the dma addresses found in the transfer_dma and/or
752 * setup_dma fields rather than determining a dma address themselves. (Note 754 * setup_dma fields rather than determining a dma address themselves. (Note
753 * that transfer_buffer and setup_packet must still be set because not all 755 * that transfer_buffer and setup_packet must still be set because not all
754 * host controllers use DMA, nor do virtual root hubs). 756 * host controllers use DMA, nor do virtual root hubs).
755 * 757 *
756 * Initialization: 758 * Initialization:
757 * 759 *
758 * All URBs submitted must initialize the dev, pipe, transfer_flags (may be 760 * All URBs submitted must initialize the dev, pipe, transfer_flags (may be
759 * zero), and complete fields. All URBs must also initialize 761 * zero), and complete fields. All URBs must also initialize
760 * transfer_buffer and transfer_buffer_length. They may provide the 762 * transfer_buffer and transfer_buffer_length. They may provide the
761 * URB_SHORT_NOT_OK transfer flag, indicating that short reads are 763 * URB_SHORT_NOT_OK transfer flag, indicating that short reads are
762 * to be treated as errors; that flag is invalid for write requests. 764 * to be treated as errors; that flag is invalid for write requests.
763 * 765 *
764 * Bulk URBs may 766 * Bulk URBs may
765 * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers 767 * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers
766 * should always terminate with a short packet, even if it means adding an 768 * should always terminate with a short packet, even if it means adding an
767 * extra zero length packet. 769 * extra zero length packet.
768 * 770 *
769 * Control URBs must provide a setup_packet. The setup_packet and 771 * Control URBs must provide a setup_packet. The setup_packet and
770 * transfer_buffer may each be mapped for DMA or not, independently of 772 * transfer_buffer may each be mapped for DMA or not, independently of
771 * the other. The transfer_flags bits URB_NO_TRANSFER_DMA_MAP and 773 * the other. The transfer_flags bits URB_NO_TRANSFER_DMA_MAP and
772 * URB_NO_SETUP_DMA_MAP indicate which buffers have already been mapped. 774 * URB_NO_SETUP_DMA_MAP indicate which buffers have already been mapped.
773 * URB_NO_SETUP_DMA_MAP is ignored for non-control URBs. 775 * URB_NO_SETUP_DMA_MAP is ignored for non-control URBs.
774 * 776 *
775 * Interrupt URBs must provide an interval, saying how often (in milliseconds 777 * Interrupt URBs must provide an interval, saying how often (in milliseconds
776 * or, for highspeed devices, 125 microsecond units) 778 * or, for highspeed devices, 125 microsecond units)
777 * to poll for transfers. After the URB has been submitted, the interval 779 * to poll for transfers. After the URB has been submitted, the interval
778 * field reflects how the transfer was actually scheduled. 780 * field reflects how the transfer was actually scheduled.
779 * The polling interval may be more frequent than requested. 781 * The polling interval may be more frequent than requested.
780 * For example, some controllers have a maximum interval of 32 milliseconds, 782 * For example, some controllers have a maximum interval of 32 milliseconds,
781 * while others support intervals of up to 1024 milliseconds. 783 * while others support intervals of up to 1024 milliseconds.
782 * Isochronous URBs also have transfer intervals. (Note that for isochronous 784 * Isochronous URBs also have transfer intervals. (Note that for isochronous
783 * endpoints, as well as high speed interrupt endpoints, the encoding of 785 * endpoints, as well as high speed interrupt endpoints, the encoding of
784 * the transfer interval in the endpoint descriptor is logarithmic. 786 * the transfer interval in the endpoint descriptor is logarithmic.
785 * Device drivers must convert that value to linear units themselves.) 787 * Device drivers must convert that value to linear units themselves.)
786 * 788 *
787 * Isochronous URBs normally use the URB_ISO_ASAP transfer flag, telling 789 * Isochronous URBs normally use the URB_ISO_ASAP transfer flag, telling
788 * the host controller to schedule the transfer as soon as bandwidth 790 * the host controller to schedule the transfer as soon as bandwidth
789 * utilization allows, and then set start_frame to reflect the actual frame 791 * utilization allows, and then set start_frame to reflect the actual frame
790 * selected during submission. Otherwise drivers must specify the start_frame 792 * selected during submission. Otherwise drivers must specify the start_frame
791 * and handle the case where the transfer can't begin then. However, drivers 793 * and handle the case where the transfer can't begin then. However, drivers
792 * won't know how bandwidth is currently allocated, and while they can 794 * won't know how bandwidth is currently allocated, and while they can
793 * find the current frame using usb_get_current_frame_number () they can't 795 * find the current frame using usb_get_current_frame_number () they can't
794 * know the range for that frame number. (Ranges for frame counter values 796 * know the range for that frame number. (Ranges for frame counter values
795 * are HC-specific, and can go from 256 to 65536 frames from "now".) 797 * are HC-specific, and can go from 256 to 65536 frames from "now".)
796 * 798 *
797 * Isochronous URBs have a different data transfer model, in part because 799 * Isochronous URBs have a different data transfer model, in part because
798 * the quality of service is only "best effort". Callers provide specially 800 * the quality of service is only "best effort". Callers provide specially
799 * allocated URBs, with number_of_packets worth of iso_frame_desc structures 801 * allocated URBs, with number_of_packets worth of iso_frame_desc structures
800 * at the end. Each such packet is an individual ISO transfer. Isochronous 802 * at the end. Each such packet is an individual ISO transfer. Isochronous
801 * URBs are normally queued, submitted by drivers to arrange that 803 * URBs are normally queued, submitted by drivers to arrange that
802 * transfers are at least double buffered, and then explicitly resubmitted 804 * transfers are at least double buffered, and then explicitly resubmitted
803 * in completion handlers, so 805 * in completion handlers, so
804 * that data (such as audio or video) streams at as constant a rate as the 806 * that data (such as audio or video) streams at as constant a rate as the
805 * host controller scheduler can support. 807 * host controller scheduler can support.
806 * 808 *
807 * Completion Callbacks: 809 * Completion Callbacks:
808 * 810 *
809 * The completion callback is made in_interrupt(), and one of the first 811 * The completion callback is made in_interrupt(), and one of the first
810 * things that a completion handler should do is check the status field. 812 * things that a completion handler should do is check the status field.
811 * The status field is provided for all URBs. It is used to report 813 * The status field is provided for all URBs. It is used to report
812 * unlinked URBs, and status for all non-ISO transfers. It should not 814 * unlinked URBs, and status for all non-ISO transfers. It should not
813 * be examined before the URB is returned to the completion handler. 815 * be examined before the URB is returned to the completion handler.
814 * 816 *
815 * The context field is normally used to link URBs back to the relevant 817 * The context field is normally used to link URBs back to the relevant
816 * driver or request state. 818 * driver or request state.
817 * 819 *
818 * When the completion callback is invoked for non-isochronous URBs, the 820 * When the completion callback is invoked for non-isochronous URBs, the
819 * actual_length field tells how many bytes were transferred. This field 821 * actual_length field tells how many bytes were transferred. This field
820 * is updated even when the URB terminated with an error or was unlinked. 822 * is updated even when the URB terminated with an error or was unlinked.
821 * 823 *
822 * ISO transfer status is reported in the status and actual_length fields 824 * ISO transfer status is reported in the status and actual_length fields
823 * of the iso_frame_desc array, and the number of errors is reported in 825 * of the iso_frame_desc array, and the number of errors is reported in
824 * error_count. Completion callbacks for ISO transfers will normally 826 * error_count. Completion callbacks for ISO transfers will normally
825 * (re)submit URBs to ensure a constant transfer rate. 827 * (re)submit URBs to ensure a constant transfer rate.
826 * 828 *
827 * Note that even fields marked "public" should not be touched by the driver 829 * Note that even fields marked "public" should not be touched by the driver
828 * when the urb is owned by the hcd, that is, since the call to 830 * when the urb is owned by the hcd, that is, since the call to
829 * usb_submit_urb() till the entry into the completion routine. 831 * usb_submit_urb() till the entry into the completion routine.
830 */ 832 */
831 struct urb 833 struct urb
832 { 834 {
833 /* private: usb core and host controller only fields in the urb */ 835 /* private: usb core and host controller only fields in the urb */
834 struct kref kref; /* reference count of the URB */ 836 struct kref kref; /* reference count of the URB */
835 spinlock_t lock; /* lock for the URB */ 837 spinlock_t lock; /* lock for the URB */
836 void *hcpriv; /* private data for host controller */ 838 void *hcpriv; /* private data for host controller */
837 int bandwidth; /* bandwidth for INT/ISO request */ 839 int bandwidth; /* bandwidth for INT/ISO request */
838 atomic_t use_count; /* concurrent submissions counter */ 840 atomic_t use_count; /* concurrent submissions counter */
839 u8 reject; /* submissions will fail */ 841 u8 reject; /* submissions will fail */
840 842
841 /* public: documented fields in the urb that can be used by drivers */ 843 /* public: documented fields in the urb that can be used by drivers */
842 struct list_head urb_list; /* list head for use by the urb's 844 struct list_head urb_list; /* list head for use by the urb's
843 * current owner */ 845 * current owner */
844 struct usb_device *dev; /* (in) pointer to associated device */ 846 struct usb_device *dev; /* (in) pointer to associated device */
845 unsigned int pipe; /* (in) pipe information */ 847 unsigned int pipe; /* (in) pipe information */
846 int status; /* (return) non-ISO status */ 848 int status; /* (return) non-ISO status */
847 unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/ 849 unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
848 void *transfer_buffer; /* (in) associated data buffer */ 850 void *transfer_buffer; /* (in) associated data buffer */
849 dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */ 851 dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
850 int transfer_buffer_length; /* (in) data buffer length */ 852 int transfer_buffer_length; /* (in) data buffer length */
851 int actual_length; /* (return) actual transfer length */ 853 int actual_length; /* (return) actual transfer length */
852 unsigned char *setup_packet; /* (in) setup packet (control only) */ 854 unsigned char *setup_packet; /* (in) setup packet (control only) */
853 dma_addr_t setup_dma; /* (in) dma addr for setup_packet */ 855 dma_addr_t setup_dma; /* (in) dma addr for setup_packet */
854 int start_frame; /* (modify) start frame (ISO) */ 856 int start_frame; /* (modify) start frame (ISO) */
855 int number_of_packets; /* (in) number of ISO packets */ 857 int number_of_packets; /* (in) number of ISO packets */
856 int interval; /* (modify) transfer interval 858 int interval; /* (modify) transfer interval
857 * (INT/ISO) */ 859 * (INT/ISO) */
858 int error_count; /* (return) number of ISO errors */ 860 int error_count; /* (return) number of ISO errors */
859 void *context; /* (in) context for completion */ 861 void *context; /* (in) context for completion */
860 usb_complete_t complete; /* (in) completion routine */ 862 usb_complete_t complete; /* (in) completion routine */
861 struct usb_iso_packet_descriptor iso_frame_desc[0]; 863 struct usb_iso_packet_descriptor iso_frame_desc[0];
862 /* (in) ISO ONLY */ 864 /* (in) ISO ONLY */
863 }; 865 };
864 866
865 /* ----------------------------------------------------------------------- */ 867 /* ----------------------------------------------------------------------- */
866 868
867 /** 869 /**
868 * usb_fill_control_urb - initializes a control urb 870 * usb_fill_control_urb - initializes a control urb
869 * @urb: pointer to the urb to initialize. 871 * @urb: pointer to the urb to initialize.
870 * @dev: pointer to the struct usb_device for this urb. 872 * @dev: pointer to the struct usb_device for this urb.
871 * @pipe: the endpoint pipe 873 * @pipe: the endpoint pipe
872 * @setup_packet: pointer to the setup_packet buffer 874 * @setup_packet: pointer to the setup_packet buffer
873 * @transfer_buffer: pointer to the transfer buffer 875 * @transfer_buffer: pointer to the transfer buffer
874 * @buffer_length: length of the transfer buffer 876 * @buffer_length: length of the transfer buffer
875 * @complete: pointer to the usb_complete_t function 877 * @complete: pointer to the usb_complete_t function
876 * @context: what to set the urb context to. 878 * @context: what to set the urb context to.
877 * 879 *
878 * Initializes a control urb with the proper information needed to submit 880 * Initializes a control urb with the proper information needed to submit
879 * it to a device. 881 * it to a device.
880 */ 882 */
881 static inline void usb_fill_control_urb (struct urb *urb, 883 static inline void usb_fill_control_urb (struct urb *urb,
882 struct usb_device *dev, 884 struct usb_device *dev,
883 unsigned int pipe, 885 unsigned int pipe,
884 unsigned char *setup_packet, 886 unsigned char *setup_packet,
885 void *transfer_buffer, 887 void *transfer_buffer,
886 int buffer_length, 888 int buffer_length,
887 usb_complete_t complete, 889 usb_complete_t complete,
888 void *context) 890 void *context)
889 { 891 {
890 spin_lock_init(&urb->lock); 892 spin_lock_init(&urb->lock);
891 urb->dev = dev; 893 urb->dev = dev;
892 urb->pipe = pipe; 894 urb->pipe = pipe;
893 urb->setup_packet = setup_packet; 895 urb->setup_packet = setup_packet;
894 urb->transfer_buffer = transfer_buffer; 896 urb->transfer_buffer = transfer_buffer;
895 urb->transfer_buffer_length = buffer_length; 897 urb->transfer_buffer_length = buffer_length;
896 urb->complete = complete; 898 urb->complete = complete;
897 urb->context = context; 899 urb->context = context;
898 } 900 }
899 901
900 /** 902 /**
901 * usb_fill_bulk_urb - macro to help initialize a bulk urb 903 * usb_fill_bulk_urb - macro to help initialize a bulk urb
902 * @urb: pointer to the urb to initialize. 904 * @urb: pointer to the urb to initialize.
903 * @dev: pointer to the struct usb_device for this urb. 905 * @dev: pointer to the struct usb_device for this urb.
904 * @pipe: the endpoint pipe 906 * @pipe: the endpoint pipe
905 * @transfer_buffer: pointer to the transfer buffer 907 * @transfer_buffer: pointer to the transfer buffer
906 * @buffer_length: length of the transfer buffer 908 * @buffer_length: length of the transfer buffer
907 * @complete: pointer to the usb_complete_t function 909 * @complete: pointer to the usb_complete_t function
908 * @context: what to set the urb context to. 910 * @context: what to set the urb context to.
909 * 911 *
910 * Initializes a bulk urb with the proper information needed to submit it 912 * Initializes a bulk urb with the proper information needed to submit it
911 * to a device. 913 * to a device.
912 */ 914 */
913 static inline void usb_fill_bulk_urb (struct urb *urb, 915 static inline void usb_fill_bulk_urb (struct urb *urb,
914 struct usb_device *dev, 916 struct usb_device *dev,
915 unsigned int pipe, 917 unsigned int pipe,
916 void *transfer_buffer, 918 void *transfer_buffer,
917 int buffer_length, 919 int buffer_length,
918 usb_complete_t complete, 920 usb_complete_t complete,
919 void *context) 921 void *context)
920 { 922 {
921 spin_lock_init(&urb->lock); 923 spin_lock_init(&urb->lock);
922 urb->dev = dev; 924 urb->dev = dev;
923 urb->pipe = pipe; 925 urb->pipe = pipe;
924 urb->transfer_buffer = transfer_buffer; 926 urb->transfer_buffer = transfer_buffer;
925 urb->transfer_buffer_length = buffer_length; 927 urb->transfer_buffer_length = buffer_length;
926 urb->complete = complete; 928 urb->complete = complete;
927 urb->context = context; 929 urb->context = context;
928 } 930 }
929 931
930 /** 932 /**
931 * usb_fill_int_urb - macro to help initialize a interrupt urb 933 * usb_fill_int_urb - macro to help initialize a interrupt urb
932 * @urb: pointer to the urb to initialize. 934 * @urb: pointer to the urb to initialize.
933 * @dev: pointer to the struct usb_device for this urb. 935 * @dev: pointer to the struct usb_device for this urb.
934 * @pipe: the endpoint pipe 936 * @pipe: the endpoint pipe
935 * @transfer_buffer: pointer to the transfer buffer 937 * @transfer_buffer: pointer to the transfer buffer
936 * @buffer_length: length of the transfer buffer 938 * @buffer_length: length of the transfer buffer
937 * @complete: pointer to the usb_complete_t function 939 * @complete: pointer to the usb_complete_t function
938 * @context: what to set the urb context to. 940 * @context: what to set the urb context to.
939 * @interval: what to set the urb interval to, encoded like 941 * @interval: what to set the urb interval to, encoded like
940 * the endpoint descriptor's bInterval value. 942 * the endpoint descriptor's bInterval value.
941 * 943 *
942 * Initializes a interrupt urb with the proper information needed to submit 944 * Initializes a interrupt urb with the proper information needed to submit
943 * it to a device. 945 * it to a device.
944 * Note that high speed interrupt endpoints use a logarithmic encoding of 946 * Note that high speed interrupt endpoints use a logarithmic encoding of
945 * the endpoint interval, and express polling intervals in microframes 947 * the endpoint interval, and express polling intervals in microframes
946 * (eight per millisecond) rather than in frames (one per millisecond). 948 * (eight per millisecond) rather than in frames (one per millisecond).
947 */ 949 */
948 static inline void usb_fill_int_urb (struct urb *urb, 950 static inline void usb_fill_int_urb (struct urb *urb,
949 struct usb_device *dev, 951 struct usb_device *dev,
950 unsigned int pipe, 952 unsigned int pipe,
951 void *transfer_buffer, 953 void *transfer_buffer,
952 int buffer_length, 954 int buffer_length,
953 usb_complete_t complete, 955 usb_complete_t complete,
954 void *context, 956 void *context,
955 int interval) 957 int interval)
956 { 958 {
957 spin_lock_init(&urb->lock); 959 spin_lock_init(&urb->lock);
958 urb->dev = dev; 960 urb->dev = dev;
959 urb->pipe = pipe; 961 urb->pipe = pipe;
960 urb->transfer_buffer = transfer_buffer; 962 urb->transfer_buffer = transfer_buffer;
961 urb->transfer_buffer_length = buffer_length; 963 urb->transfer_buffer_length = buffer_length;
962 urb->complete = complete; 964 urb->complete = complete;
963 urb->context = context; 965 urb->context = context;
964 if (dev->speed == USB_SPEED_HIGH) 966 if (dev->speed == USB_SPEED_HIGH)
965 urb->interval = 1 << (interval - 1); 967 urb->interval = 1 << (interval - 1);
966 else 968 else
967 urb->interval = interval; 969 urb->interval = interval;
968 urb->start_frame = -1; 970 urb->start_frame = -1;
969 } 971 }
970 972
971 extern void usb_init_urb(struct urb *urb); 973 extern void usb_init_urb(struct urb *urb);
972 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags); 974 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags);
973 extern void usb_free_urb(struct urb *urb); 975 extern void usb_free_urb(struct urb *urb);
974 #define usb_put_urb usb_free_urb 976 #define usb_put_urb usb_free_urb
975 extern struct urb *usb_get_urb(struct urb *urb); 977 extern struct urb *usb_get_urb(struct urb *urb);
976 extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags); 978 extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags);
977 extern int usb_unlink_urb(struct urb *urb); 979 extern int usb_unlink_urb(struct urb *urb);
978 extern void usb_kill_urb(struct urb *urb); 980 extern void usb_kill_urb(struct urb *urb);
979 981
980 #define HAVE_USB_BUFFERS 982 #define HAVE_USB_BUFFERS
981 void *usb_buffer_alloc (struct usb_device *dev, size_t size, 983 void *usb_buffer_alloc (struct usb_device *dev, size_t size,
982 gfp_t mem_flags, dma_addr_t *dma); 984 gfp_t mem_flags, dma_addr_t *dma);
983 void usb_buffer_free (struct usb_device *dev, size_t size, 985 void usb_buffer_free (struct usb_device *dev, size_t size,
984 void *addr, dma_addr_t dma); 986 void *addr, dma_addr_t dma);
985 987
986 #if 0 988 #if 0
987 struct urb *usb_buffer_map (struct urb *urb); 989 struct urb *usb_buffer_map (struct urb *urb);
988 void usb_buffer_dmasync (struct urb *urb); 990 void usb_buffer_dmasync (struct urb *urb);
989 void usb_buffer_unmap (struct urb *urb); 991 void usb_buffer_unmap (struct urb *urb);
990 #endif 992 #endif
991 993
992 struct scatterlist; 994 struct scatterlist;
993 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, 995 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
994 struct scatterlist *sg, int nents); 996 struct scatterlist *sg, int nents);
995 #if 0 997 #if 0
996 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, 998 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
997 struct scatterlist *sg, int n_hw_ents); 999 struct scatterlist *sg, int n_hw_ents);
998 #endif 1000 #endif
999 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, 1001 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1000 struct scatterlist *sg, int n_hw_ents); 1002 struct scatterlist *sg, int n_hw_ents);
1001 1003
1002 /*-------------------------------------------------------------------* 1004 /*-------------------------------------------------------------------*
1003 * SYNCHRONOUS CALL SUPPORT * 1005 * SYNCHRONOUS CALL SUPPORT *
1004 *-------------------------------------------------------------------*/ 1006 *-------------------------------------------------------------------*/
1005 1007
1006 extern int usb_control_msg(struct usb_device *dev, unsigned int pipe, 1008 extern int usb_control_msg(struct usb_device *dev, unsigned int pipe,
1007 __u8 request, __u8 requesttype, __u16 value, __u16 index, 1009 __u8 request, __u8 requesttype, __u16 value, __u16 index,
1008 void *data, __u16 size, int timeout); 1010 void *data, __u16 size, int timeout);
1009 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 1011 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
1010 void *data, int len, int *actual_length, 1012 void *data, int len, int *actual_length,
1011 int timeout); 1013 int timeout);
1012 1014
1013 /* wrappers around usb_control_msg() for the most common standard requests */ 1015 /* wrappers around usb_control_msg() for the most common standard requests */
1014 extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype, 1016 extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype,
1015 unsigned char descindex, void *buf, int size); 1017 unsigned char descindex, void *buf, int size);
1016 extern int usb_get_status(struct usb_device *dev, 1018 extern int usb_get_status(struct usb_device *dev,
1017 int type, int target, void *data); 1019 int type, int target, void *data);
1018 extern int usb_get_string(struct usb_device *dev, 1020 extern int usb_get_string(struct usb_device *dev,
1019 unsigned short langid, unsigned char index, void *buf, int size); 1021 unsigned short langid, unsigned char index, void *buf, int size);
1020 extern int usb_string(struct usb_device *dev, int index, 1022 extern int usb_string(struct usb_device *dev, int index,
1021 char *buf, size_t size); 1023 char *buf, size_t size);
1022 1024
1023 /* wrappers that also update important state inside usbcore */ 1025 /* wrappers that also update important state inside usbcore */
1024 extern int usb_clear_halt(struct usb_device *dev, int pipe); 1026 extern int usb_clear_halt(struct usb_device *dev, int pipe);
1025 extern int usb_reset_configuration(struct usb_device *dev); 1027 extern int usb_reset_configuration(struct usb_device *dev);
1026 extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate); 1028 extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate);
1027 1029
1028 /* 1030 /*
1029 * timeouts, in milliseconds, used for sending/receiving control messages 1031 * timeouts, in milliseconds, used for sending/receiving control messages
1030 * they typically complete within a few frames (msec) after they're issued 1032 * they typically complete within a few frames (msec) after they're issued
1031 * USB identifies 5 second timeouts, maybe more in a few cases, and a few 1033 * USB identifies 5 second timeouts, maybe more in a few cases, and a few
1032 * slow devices (like some MGE Ellipse UPSes) actually push that limit. 1034 * slow devices (like some MGE Ellipse UPSes) actually push that limit.
1033 */ 1035 */
1034 #define USB_CTRL_GET_TIMEOUT 5000 1036 #define USB_CTRL_GET_TIMEOUT 5000
1035 #define USB_CTRL_SET_TIMEOUT 5000 1037 #define USB_CTRL_SET_TIMEOUT 5000
1036 1038
1037 1039
1038 /** 1040 /**
1039 * struct usb_sg_request - support for scatter/gather I/O 1041 * struct usb_sg_request - support for scatter/gather I/O
1040 * @status: zero indicates success, else negative errno 1042 * @status: zero indicates success, else negative errno
1041 * @bytes: counts bytes transferred. 1043 * @bytes: counts bytes transferred.
1042 * 1044 *
1043 * These requests are initialized using usb_sg_init(), and then are used 1045 * These requests are initialized using usb_sg_init(), and then are used
1044 * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most 1046 * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most
1045 * members of the request object aren't for driver access. 1047 * members of the request object aren't for driver access.
1046 * 1048 *
1047 * The status and bytecount values are valid only after usb_sg_wait() 1049 * The status and bytecount values are valid only after usb_sg_wait()
1048 * returns. If the status is zero, then the bytecount matches the total 1050 * returns. If the status is zero, then the bytecount matches the total
1049 * from the request. 1051 * from the request.
1050 * 1052 *
1051 * After an error completion, drivers may need to clear a halt condition 1053 * After an error completion, drivers may need to clear a halt condition
1052 * on the endpoint. 1054 * on the endpoint.
1053 */ 1055 */
1054 struct usb_sg_request { 1056 struct usb_sg_request {
1055 int status; 1057 int status;
1056 size_t bytes; 1058 size_t bytes;
1057 1059
1058 /* 1060 /*
1059 * members below are private: to usbcore, 1061 * members below are private: to usbcore,
1060 * and are not provided for driver access! 1062 * and are not provided for driver access!
1061 */ 1063 */
1062 spinlock_t lock; 1064 spinlock_t lock;
1063 1065
1064 struct usb_device *dev; 1066 struct usb_device *dev;
1065 int pipe; 1067 int pipe;
1066 struct scatterlist *sg; 1068 struct scatterlist *sg;
1067 int nents; 1069 int nents;
1068 1070
1069 int entries; 1071 int entries;
1070 struct urb **urbs; 1072 struct urb **urbs;
1071 1073
1072 int count; 1074 int count;
1073 struct completion complete; 1075 struct completion complete;
1074 }; 1076 };
1075 1077
1076 int usb_sg_init ( 1078 int usb_sg_init (
1077 struct usb_sg_request *io, 1079 struct usb_sg_request *io,
1078 struct usb_device *dev, 1080 struct usb_device *dev,
1079 unsigned pipe, 1081 unsigned pipe,
1080 unsigned period, 1082 unsigned period,
1081 struct scatterlist *sg, 1083 struct scatterlist *sg,
1082 int nents, 1084 int nents,
1083 size_t length, 1085 size_t length,
1084 gfp_t mem_flags 1086 gfp_t mem_flags
1085 ); 1087 );
1086 void usb_sg_cancel (struct usb_sg_request *io); 1088 void usb_sg_cancel (struct usb_sg_request *io);
1087 void usb_sg_wait (struct usb_sg_request *io); 1089 void usb_sg_wait (struct usb_sg_request *io);
1088 1090
1089 1091
1090 /* ----------------------------------------------------------------------- */ 1092 /* ----------------------------------------------------------------------- */
1091 1093
1092 /* 1094 /*
1093 * For various legacy reasons, Linux has a small cookie that's paired with 1095 * For various legacy reasons, Linux has a small cookie that's paired with
1094 * a struct usb_device to identify an endpoint queue. Queue characteristics 1096 * a struct usb_device to identify an endpoint queue. Queue characteristics
1095 * are defined by the endpoint's descriptor. This cookie is called a "pipe", 1097 * are defined by the endpoint's descriptor. This cookie is called a "pipe",
1096 * an unsigned int encoded as: 1098 * an unsigned int encoded as:
1097 * 1099 *
1098 * - direction: bit 7 (0 = Host-to-Device [Out], 1100 * - direction: bit 7 (0 = Host-to-Device [Out],
1099 * 1 = Device-to-Host [In] ... 1101 * 1 = Device-to-Host [In] ...
1100 * like endpoint bEndpointAddress) 1102 * like endpoint bEndpointAddress)
1101 * - device address: bits 8-14 ... bit positions known to uhci-hcd 1103 * - device address: bits 8-14 ... bit positions known to uhci-hcd
1102 * - endpoint: bits 15-18 ... bit positions known to uhci-hcd 1104 * - endpoint: bits 15-18 ... bit positions known to uhci-hcd
1103 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 1105 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
1104 * 10 = control, 11 = bulk) 1106 * 10 = control, 11 = bulk)
1105 * 1107 *
1106 * Given the device address and endpoint descriptor, pipes are redundant. 1108 * Given the device address and endpoint descriptor, pipes are redundant.
1107 */ 1109 */
1108 1110
1109 /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */ 1111 /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */
1110 /* (yet ... they're the values used by usbfs) */ 1112 /* (yet ... they're the values used by usbfs) */
1111 #define PIPE_ISOCHRONOUS 0 1113 #define PIPE_ISOCHRONOUS 0
1112 #define PIPE_INTERRUPT 1 1114 #define PIPE_INTERRUPT 1
1113 #define PIPE_CONTROL 2 1115 #define PIPE_CONTROL 2
1114 #define PIPE_BULK 3 1116 #define PIPE_BULK 3
1115 1117
1116 #define usb_pipein(pipe) ((pipe) & USB_DIR_IN) 1118 #define usb_pipein(pipe) ((pipe) & USB_DIR_IN)
1117 #define usb_pipeout(pipe) (!usb_pipein(pipe)) 1119 #define usb_pipeout(pipe) (!usb_pipein(pipe))
1118 1120
1119 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f) 1121 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
1120 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf) 1122 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
1121 1123
1122 #define usb_pipetype(pipe) (((pipe) >> 30) & 3) 1124 #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
1123 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 1125 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
1124 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 1126 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
1125 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 1127 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
1126 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 1128 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
1127 1129
1128 /* The D0/D1 toggle bits ... USE WITH CAUTION (they're almost hcd-internal) */ 1130 /* The D0/D1 toggle bits ... USE WITH CAUTION (they're almost hcd-internal) */
1129 #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> (ep)) & 1) 1131 #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> (ep)) & 1)
1130 #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << (ep))) 1132 #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << (ep)))
1131 #define usb_settoggle(dev, ep, out, bit) \ 1133 #define usb_settoggle(dev, ep, out, bit) \
1132 ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ep))) | \ 1134 ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ep))) | \
1133 ((bit) << (ep))) 1135 ((bit) << (ep)))
1134 1136
1135 1137
1136 static inline unsigned int __create_pipe(struct usb_device *dev, 1138 static inline unsigned int __create_pipe(struct usb_device *dev,
1137 unsigned int endpoint) 1139 unsigned int endpoint)
1138 { 1140 {
1139 return (dev->devnum << 8) | (endpoint << 15); 1141 return (dev->devnum << 8) | (endpoint << 15);
1140 } 1142 }
1141 1143
1142 /* Create various pipes... */ 1144 /* Create various pipes... */
1143 #define usb_sndctrlpipe(dev,endpoint) \ 1145 #define usb_sndctrlpipe(dev,endpoint) \
1144 ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint)) 1146 ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint))
1145 #define usb_rcvctrlpipe(dev,endpoint) \ 1147 #define usb_rcvctrlpipe(dev,endpoint) \
1146 ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN) 1148 ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
1147 #define usb_sndisocpipe(dev,endpoint) \ 1149 #define usb_sndisocpipe(dev,endpoint) \
1148 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint)) 1150 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint))
1149 #define usb_rcvisocpipe(dev,endpoint) \ 1151 #define usb_rcvisocpipe(dev,endpoint) \
1150 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN) 1152 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
1151 #define usb_sndbulkpipe(dev,endpoint) \ 1153 #define usb_sndbulkpipe(dev,endpoint) \
1152 ((PIPE_BULK << 30) | __create_pipe(dev,endpoint)) 1154 ((PIPE_BULK << 30) | __create_pipe(dev,endpoint))
1153 #define usb_rcvbulkpipe(dev,endpoint) \ 1155 #define usb_rcvbulkpipe(dev,endpoint) \
1154 ((PIPE_BULK << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN) 1156 ((PIPE_BULK << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
1155 #define usb_sndintpipe(dev,endpoint) \ 1157 #define usb_sndintpipe(dev,endpoint) \
1156 ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint)) 1158 ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint))
1157 #define usb_rcvintpipe(dev,endpoint) \ 1159 #define usb_rcvintpipe(dev,endpoint) \
1158 ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN) 1160 ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
1159 1161
1160 /*-------------------------------------------------------------------------*/ 1162 /*-------------------------------------------------------------------------*/
1161 1163
1162 static inline __u16 1164 static inline __u16
1163 usb_maxpacket(struct usb_device *udev, int pipe, int is_out) 1165 usb_maxpacket(struct usb_device *udev, int pipe, int is_out)
1164 { 1166 {
1165 struct usb_host_endpoint *ep; 1167 struct usb_host_endpoint *ep;
1166 unsigned epnum = usb_pipeendpoint(pipe); 1168 unsigned epnum = usb_pipeendpoint(pipe);
1167 1169
1168 if (is_out) { 1170 if (is_out) {
1169 WARN_ON(usb_pipein(pipe)); 1171 WARN_ON(usb_pipein(pipe));
1170 ep = udev->ep_out[epnum]; 1172 ep = udev->ep_out[epnum];
1171 } else { 1173 } else {
1172 WARN_ON(usb_pipeout(pipe)); 1174 WARN_ON(usb_pipeout(pipe));
1173 ep = udev->ep_in[epnum]; 1175 ep = udev->ep_in[epnum];
1174 } 1176 }
1175 if (!ep) 1177 if (!ep)
1176 return 0; 1178 return 0;
1177 1179
1178 /* NOTE: only 0x07ff bits are for packet size... */ 1180 /* NOTE: only 0x07ff bits are for packet size... */
1179 return le16_to_cpu(ep->desc.wMaxPacketSize); 1181 return le16_to_cpu(ep->desc.wMaxPacketSize);
1180 } 1182 }
1181 1183
1182 /* ----------------------------------------------------------------------- */ 1184 /* ----------------------------------------------------------------------- */
1183 1185
1184 /* Events from the usb core */ 1186 /* Events from the usb core */
1185 #define USB_DEVICE_ADD 0x0001 1187 #define USB_DEVICE_ADD 0x0001
1186 #define USB_DEVICE_REMOVE 0x0002 1188 #define USB_DEVICE_REMOVE 0x0002
1187 #define USB_BUS_ADD 0x0003 1189 #define USB_BUS_ADD 0x0003
1188 #define USB_BUS_REMOVE 0x0004 1190 #define USB_BUS_REMOVE 0x0004
1189 extern void usb_register_notify(struct notifier_block *nb); 1191 extern void usb_register_notify(struct notifier_block *nb);
1190 extern void usb_unregister_notify(struct notifier_block *nb); 1192 extern void usb_unregister_notify(struct notifier_block *nb);
1191 1193
1192 #ifdef DEBUG 1194 #ifdef DEBUG
1193 #define dbg(format, arg...) printk(KERN_DEBUG "%s: " format "\n" , \ 1195 #define dbg(format, arg...) printk(KERN_DEBUG "%s: " format "\n" , \
1194 __FILE__ , ## arg) 1196 __FILE__ , ## arg)
1195 #else 1197 #else
1196 #define dbg(format, arg...) do {} while (0) 1198 #define dbg(format, arg...) do {} while (0)
1197 #endif 1199 #endif
1198 1200
1199 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n" , \ 1201 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n" , \
1200 __FILE__ , ## arg) 1202 __FILE__ , ## arg)
1201 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n" , \ 1203 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n" , \
1202 __FILE__ , ## arg) 1204 __FILE__ , ## arg)
1203 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n" , \ 1205 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n" , \
1204 __FILE__ , ## arg) 1206 __FILE__ , ## arg)
1205 1207
1206 1208
1207 #endif /* __KERNEL__ */ 1209 #endif /* __KERNEL__ */
1208 1210
1209 #endif 1211 #endif
1210 1212