Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright Linus Torvalds 1999 |
| 3 | * (C) Copyright Johannes Erdfelt 1999-2001 |
| 4 | * (C) Copyright Andreas Gal 1999 |
| 5 | * (C) Copyright Gregory P. Smith 1999 |
| 6 | * (C) Copyright Deti Fliegl 1999 |
| 7 | * (C) Copyright Randy Dunlap 2000 |
| 8 | * (C) Copyright David Brownell 2000-2002 |
| 9 | * |
| 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 |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 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 |
| 17 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | * for more details. |
| 19 | * |
| 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, |
| 22 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/version.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/completion.h> |
| 30 | #include <linux/utsname.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/device.h> |
| 34 | #include <linux/dma-mapping.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/irq.h> |
| 37 | #include <asm/byteorder.h> |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
Aleksey Gorelov | 64a21d0 | 2006-08-08 17:24:08 -0700 | [diff] [blame] | 39 | #include <linux/platform_device.h> |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 40 | #include <linux/workqueue.h> |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <linux/usb.h> |
| 44 | |
| 45 | #include "usb.h" |
| 46 | #include "hcd.h" |
| 47 | #include "hub.h" |
| 48 | |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /*-------------------------------------------------------------------------*/ |
| 51 | |
| 52 | /* |
| 53 | * USB Host Controller Driver framework |
| 54 | * |
| 55 | * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing |
| 56 | * HCD-specific behaviors/bugs. |
| 57 | * |
| 58 | * This does error checks, tracks devices and urbs, and delegates to a |
| 59 | * "hc_driver" only for code (and data) that really needs to know about |
| 60 | * hardware differences. That includes root hub registers, i/o queues, |
| 61 | * and so on ... but as little else as possible. |
| 62 | * |
| 63 | * Shared code includes most of the "root hub" code (these are emulated, |
| 64 | * though each HC's hardware works differently) and PCI glue, plus request |
| 65 | * tracking overhead. The HCD code should only block on spinlocks or on |
| 66 | * hardware handshaking; blocking on software events (such as other kernel |
| 67 | * threads releasing resources, or completing actions) is all generic. |
| 68 | * |
| 69 | * Happens the USB 2.0 spec says this would be invisible inside the "USBD", |
| 70 | * and includes mostly a "HCDI" (HCD Interface) along with some APIs used |
| 71 | * only by the hub driver ... and that neither should be seen or used by |
| 72 | * usb client device drivers. |
| 73 | * |
| 74 | * Contributors of ideas or unattributed patches include: David Brownell, |
| 75 | * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... |
| 76 | * |
| 77 | * HISTORY: |
| 78 | * 2002-02-21 Pull in most of the usb_bus support from usb.c; some |
| 79 | * associated cleanup. "usb_hcd" still != "usb_bus". |
| 80 | * 2001-12-12 Initial patch version for Linux 2.5.1 kernel. |
| 81 | */ |
| 82 | |
| 83 | /*-------------------------------------------------------------------------*/ |
| 84 | |
Alan Stern | 9beeee6 | 2008-10-02 11:48:13 -0400 | [diff] [blame] | 85 | /* Keep track of which host controller drivers are loaded */ |
| 86 | unsigned long usb_hcds_loaded; |
| 87 | EXPORT_SYMBOL_GPL(usb_hcds_loaded); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* host controllers we manage */ |
| 90 | LIST_HEAD (usb_bus_list); |
| 91 | EXPORT_SYMBOL_GPL (usb_bus_list); |
| 92 | |
| 93 | /* used when allocating bus numbers */ |
| 94 | #define USB_MAXBUS 64 |
| 95 | struct usb_busmap { |
| 96 | unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))]; |
| 97 | }; |
| 98 | static struct usb_busmap busmap; |
| 99 | |
| 100 | /* used when updating list of hcds */ |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 101 | DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | EXPORT_SYMBOL_GPL (usb_bus_list_lock); |
| 103 | |
| 104 | /* used for controlling access to virtual root hubs */ |
| 105 | static DEFINE_SPINLOCK(hcd_root_hub_lock); |
| 106 | |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 107 | /* used when updating an endpoint's URB list */ |
| 108 | static DEFINE_SPINLOCK(hcd_urb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 110 | /* used to protect against unlinking URBs after the device is gone */ |
| 111 | static DEFINE_SPINLOCK(hcd_urb_unlink_lock); |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* wait queue for synchronous unlinks */ |
| 114 | DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); |
| 115 | |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 116 | static inline int is_root_hub(struct usb_device *udev) |
| 117 | { |
| 118 | return (udev->parent == NULL); |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /*-------------------------------------------------------------------------*/ |
| 122 | |
| 123 | /* |
| 124 | * Sharable chunks of root hub code. |
| 125 | */ |
| 126 | |
| 127 | /*-------------------------------------------------------------------------*/ |
| 128 | |
| 129 | #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff) |
| 130 | #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff) |
| 131 | |
Sarah Sharp | d2e9b4d | 2009-04-27 19:55:01 -0700 | [diff] [blame] | 132 | /* usb 3.0 root hub device descriptor */ |
| 133 | static const u8 usb3_rh_dev_descriptor[18] = { |
| 134 | 0x12, /* __u8 bLength; */ |
| 135 | 0x01, /* __u8 bDescriptorType; Device */ |
| 136 | 0x00, 0x03, /* __le16 bcdUSB; v3.0 */ |
| 137 | |
| 138 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 139 | 0x00, /* __u8 bDeviceSubClass; */ |
| 140 | 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */ |
| 141 | 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ |
| 142 | |
| 143 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ |
| 144 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ |
| 145 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 146 | |
| 147 | 0x03, /* __u8 iManufacturer; */ |
| 148 | 0x02, /* __u8 iProduct; */ |
| 149 | 0x01, /* __u8 iSerialNumber; */ |
| 150 | 0x01 /* __u8 bNumConfigurations; */ |
| 151 | }; |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* usb 2.0 root hub device descriptor */ |
| 154 | static const u8 usb2_rh_dev_descriptor [18] = { |
| 155 | 0x12, /* __u8 bLength; */ |
| 156 | 0x01, /* __u8 bDescriptorType; Device */ |
| 157 | 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ |
| 158 | |
| 159 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 160 | 0x00, /* __u8 bDeviceSubClass; */ |
Alan Stern | 7329e21 | 2008-04-03 18:02:56 -0400 | [diff] [blame] | 161 | 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ |
Alan Stern | 16f16d1 | 2005-10-24 15:41:19 -0400 | [diff] [blame] | 162 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Greg Kroah-Hartman | 667d691 | 2008-01-28 09:50:12 -0800 | [diff] [blame] | 164 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ |
| 165 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 167 | |
| 168 | 0x03, /* __u8 iManufacturer; */ |
| 169 | 0x02, /* __u8 iProduct; */ |
| 170 | 0x01, /* __u8 iSerialNumber; */ |
| 171 | 0x01 /* __u8 bNumConfigurations; */ |
| 172 | }; |
| 173 | |
| 174 | /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ |
| 175 | |
| 176 | /* usb 1.1 root hub device descriptor */ |
| 177 | static const u8 usb11_rh_dev_descriptor [18] = { |
| 178 | 0x12, /* __u8 bLength; */ |
| 179 | 0x01, /* __u8 bDescriptorType; Device */ |
| 180 | 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ |
| 181 | |
| 182 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 183 | 0x00, /* __u8 bDeviceSubClass; */ |
| 184 | 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ |
Alan Stern | 16f16d1 | 2005-10-24 15:41:19 -0400 | [diff] [blame] | 185 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Greg Kroah-Hartman | 667d691 | 2008-01-28 09:50:12 -0800 | [diff] [blame] | 187 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ |
| 188 | 0x01, 0x00, /* __le16 idProduct; device 0x0001 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 190 | |
| 191 | 0x03, /* __u8 iManufacturer; */ |
| 192 | 0x02, /* __u8 iProduct; */ |
| 193 | 0x01, /* __u8 iSerialNumber; */ |
| 194 | 0x01 /* __u8 bNumConfigurations; */ |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | /*-------------------------------------------------------------------------*/ |
| 199 | |
| 200 | /* Configuration descriptors for our root hubs */ |
| 201 | |
| 202 | static const u8 fs_rh_config_descriptor [] = { |
| 203 | |
| 204 | /* one configuration */ |
| 205 | 0x09, /* __u8 bLength; */ |
| 206 | 0x02, /* __u8 bDescriptorType; Configuration */ |
| 207 | 0x19, 0x00, /* __le16 wTotalLength; */ |
| 208 | 0x01, /* __u8 bNumInterfaces; (1) */ |
| 209 | 0x01, /* __u8 bConfigurationValue; */ |
| 210 | 0x00, /* __u8 iConfiguration; */ |
| 211 | 0xc0, /* __u8 bmAttributes; |
| 212 | Bit 7: must be set, |
| 213 | 6: Self-powered, |
| 214 | 5: Remote wakeup, |
| 215 | 4..0: resvd */ |
| 216 | 0x00, /* __u8 MaxPower; */ |
| 217 | |
| 218 | /* USB 1.1: |
| 219 | * USB 2.0, single TT organization (mandatory): |
| 220 | * one interface, protocol 0 |
| 221 | * |
| 222 | * USB 2.0, multiple TT organization (optional): |
| 223 | * two interfaces, protocols 1 (like single TT) |
| 224 | * and 2 (multiple TT mode) ... config is |
| 225 | * sometimes settable |
| 226 | * NOT IMPLEMENTED |
| 227 | */ |
| 228 | |
| 229 | /* one interface */ |
| 230 | 0x09, /* __u8 if_bLength; */ |
| 231 | 0x04, /* __u8 if_bDescriptorType; Interface */ |
| 232 | 0x00, /* __u8 if_bInterfaceNumber; */ |
| 233 | 0x00, /* __u8 if_bAlternateSetting; */ |
| 234 | 0x01, /* __u8 if_bNumEndpoints; */ |
| 235 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 236 | 0x00, /* __u8 if_bInterfaceSubClass; */ |
| 237 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 238 | 0x00, /* __u8 if_iInterface; */ |
| 239 | |
| 240 | /* one endpoint (status change endpoint) */ |
| 241 | 0x07, /* __u8 ep_bLength; */ |
| 242 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ |
| 243 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 244 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 245 | 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ |
| 246 | 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ |
| 247 | }; |
| 248 | |
| 249 | static const u8 hs_rh_config_descriptor [] = { |
| 250 | |
| 251 | /* one configuration */ |
| 252 | 0x09, /* __u8 bLength; */ |
| 253 | 0x02, /* __u8 bDescriptorType; Configuration */ |
| 254 | 0x19, 0x00, /* __le16 wTotalLength; */ |
| 255 | 0x01, /* __u8 bNumInterfaces; (1) */ |
| 256 | 0x01, /* __u8 bConfigurationValue; */ |
| 257 | 0x00, /* __u8 iConfiguration; */ |
| 258 | 0xc0, /* __u8 bmAttributes; |
| 259 | Bit 7: must be set, |
| 260 | 6: Self-powered, |
| 261 | 5: Remote wakeup, |
| 262 | 4..0: resvd */ |
| 263 | 0x00, /* __u8 MaxPower; */ |
| 264 | |
| 265 | /* USB 1.1: |
| 266 | * USB 2.0, single TT organization (mandatory): |
| 267 | * one interface, protocol 0 |
| 268 | * |
| 269 | * USB 2.0, multiple TT organization (optional): |
| 270 | * two interfaces, protocols 1 (like single TT) |
| 271 | * and 2 (multiple TT mode) ... config is |
| 272 | * sometimes settable |
| 273 | * NOT IMPLEMENTED |
| 274 | */ |
| 275 | |
| 276 | /* one interface */ |
| 277 | 0x09, /* __u8 if_bLength; */ |
| 278 | 0x04, /* __u8 if_bDescriptorType; Interface */ |
| 279 | 0x00, /* __u8 if_bInterfaceNumber; */ |
| 280 | 0x00, /* __u8 if_bAlternateSetting; */ |
| 281 | 0x01, /* __u8 if_bNumEndpoints; */ |
| 282 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 283 | 0x00, /* __u8 if_bInterfaceSubClass; */ |
| 284 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 285 | 0x00, /* __u8 if_iInterface; */ |
| 286 | |
| 287 | /* one endpoint (status change endpoint) */ |
| 288 | 0x07, /* __u8 ep_bLength; */ |
| 289 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ |
| 290 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 291 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
inaky@linux.intel.com | 88fafff | 2006-10-11 20:05:59 -0700 | [diff] [blame] | 292 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
| 293 | * see hub.c:hub_configure() for details. */ |
| 294 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
| 296 | }; |
| 297 | |
Sarah Sharp | d2e9b4d | 2009-04-27 19:55:01 -0700 | [diff] [blame] | 298 | static const u8 ss_rh_config_descriptor[] = { |
| 299 | /* one configuration */ |
| 300 | 0x09, /* __u8 bLength; */ |
| 301 | 0x02, /* __u8 bDescriptorType; Configuration */ |
| 302 | 0x19, 0x00, /* __le16 wTotalLength; FIXME */ |
| 303 | 0x01, /* __u8 bNumInterfaces; (1) */ |
| 304 | 0x01, /* __u8 bConfigurationValue; */ |
| 305 | 0x00, /* __u8 iConfiguration; */ |
| 306 | 0xc0, /* __u8 bmAttributes; |
| 307 | Bit 7: must be set, |
| 308 | 6: Self-powered, |
| 309 | 5: Remote wakeup, |
| 310 | 4..0: resvd */ |
| 311 | 0x00, /* __u8 MaxPower; */ |
| 312 | |
| 313 | /* one interface */ |
| 314 | 0x09, /* __u8 if_bLength; */ |
| 315 | 0x04, /* __u8 if_bDescriptorType; Interface */ |
| 316 | 0x00, /* __u8 if_bInterfaceNumber; */ |
| 317 | 0x00, /* __u8 if_bAlternateSetting; */ |
| 318 | 0x01, /* __u8 if_bNumEndpoints; */ |
| 319 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 320 | 0x00, /* __u8 if_bInterfaceSubClass; */ |
| 321 | 0x00, /* __u8 if_bInterfaceProtocol; */ |
| 322 | 0x00, /* __u8 if_iInterface; */ |
| 323 | |
| 324 | /* one endpoint (status change endpoint) */ |
| 325 | 0x07, /* __u8 ep_bLength; */ |
| 326 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ |
| 327 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 328 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 329 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
| 330 | * see hub.c:hub_configure() for details. */ |
| 331 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, |
| 332 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
| 333 | /* |
| 334 | * All 3.0 hubs should have an endpoint companion descriptor, |
| 335 | * but we're ignoring that for now. FIXME? |
| 336 | */ |
| 337 | }; |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /*-------------------------------------------------------------------------*/ |
| 340 | |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 341 | /** |
| 342 | * ascii2desc() - Helper routine for producing UTF-16LE string descriptors |
| 343 | * @s: Null-terminated ASCII (actually ISO-8859-1) string |
| 344 | * @buf: Buffer for USB string descriptor (header + UTF-16LE) |
| 345 | * @len: Length (in bytes; may be odd) of descriptor buffer. |
| 346 | * |
| 347 | * The return value is the number of bytes filled in: 2 + 2*strlen(s) or |
| 348 | * buflen, whichever is less. |
| 349 | * |
| 350 | * USB String descriptors can contain at most 126 characters; input |
| 351 | * strings longer than that are truncated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | */ |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 353 | static unsigned |
| 354 | ascii2desc(char const *s, u8 *buf, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 356 | unsigned n, t = 2 + 2*strlen(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 358 | if (t > 254) |
| 359 | t = 254; /* Longest possible UTF string descriptor */ |
| 360 | if (len > t) |
| 361 | len = t; |
| 362 | |
| 363 | t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */ |
| 364 | |
| 365 | n = len; |
| 366 | while (n--) { |
| 367 | *buf++ = t; |
| 368 | if (!n--) |
| 369 | break; |
| 370 | *buf++ = t >> 8; |
| 371 | t = (unsigned char)*s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 373 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 376 | /** |
| 377 | * rh_string() - provides string descriptors for root hub |
| 378 | * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | * @hcd: the host controller for this root hub |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 380 | * @data: buffer for output packet |
| 381 | * @len: length of the provided buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * |
| 383 | * Produces either a manufacturer, product or serial number string for the |
| 384 | * virtual root hub device. |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 385 | * Returns the number of bytes filled in: the length of the descriptor or |
| 386 | * of the provided buffer, whichever is less. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | */ |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 388 | static unsigned |
| 389 | rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 390 | { |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 391 | char buf[100]; |
| 392 | char const *s; |
| 393 | static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | // language ids |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 396 | switch (id) { |
| 397 | case 0: |
| 398 | /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */ |
| 399 | /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */ |
| 400 | if (len > 4) |
| 401 | len = 4; |
| 402 | memcpy(data, langids, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return len; |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 404 | case 1: |
| 405 | /* Serial number */ |
| 406 | s = hcd->self.bus_name; |
| 407 | break; |
| 408 | case 2: |
| 409 | /* Product name */ |
| 410 | s = hcd->product_desc; |
| 411 | break; |
| 412 | case 3: |
| 413 | /* Manufacturer */ |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 414 | snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, |
| 415 | init_utsname()->release, hcd->driver->description); |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 416 | s = buf; |
| 417 | break; |
| 418 | default: |
| 419 | /* Can't happen; caller guarantees it */ |
| 420 | return 0; |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
George Spelvin | 392ca68 | 2009-08-24 22:06:41 -0400 | [diff] [blame] | 423 | return ascii2desc(s, data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | /* Root hub control transfers execute synchronously */ |
| 428 | static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) |
| 429 | { |
| 430 | struct usb_ctrlrequest *cmd; |
| 431 | u16 typeReq, wValue, wIndex, wLength; |
| 432 | u8 *ubuf = urb->transfer_buffer; |
Mikael Pettersson | 54bee6e | 2006-09-23 17:05:31 -0700 | [diff] [blame] | 433 | u8 tbuf [sizeof (struct usb_hub_descriptor)] |
| 434 | __attribute__((aligned(4))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | const u8 *bufp = tbuf; |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 436 | unsigned len = 0; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 437 | int status; |
Alan Stern | 7329e21 | 2008-04-03 18:02:56 -0400 | [diff] [blame] | 438 | u8 patch_wakeup = 0; |
| 439 | u8 patch_protocol = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 441 | might_sleep(); |
| 442 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 443 | spin_lock_irq(&hcd_root_hub_lock); |
| 444 | status = usb_hcd_link_urb_to_ep(hcd, urb); |
| 445 | spin_unlock_irq(&hcd_root_hub_lock); |
| 446 | if (status) |
| 447 | return status; |
Alan Stern | b0d9efb | 2007-08-21 15:39:21 -0400 | [diff] [blame] | 448 | urb->hcpriv = hcd; /* Indicate it's queued */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | cmd = (struct usb_ctrlrequest *) urb->setup_packet; |
| 451 | typeReq = (cmd->bRequestType << 8) | cmd->bRequest; |
| 452 | wValue = le16_to_cpu (cmd->wValue); |
| 453 | wIndex = le16_to_cpu (cmd->wIndex); |
| 454 | wLength = le16_to_cpu (cmd->wLength); |
| 455 | |
| 456 | if (wLength > urb->transfer_buffer_length) |
| 457 | goto error; |
| 458 | |
| 459 | urb->actual_length = 0; |
| 460 | switch (typeReq) { |
| 461 | |
| 462 | /* DEVICE REQUESTS */ |
| 463 | |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 464 | /* The root hub's remote wakeup enable bit is implemented using |
| 465 | * driver model wakeup flags. If this system supports wakeup |
| 466 | * through USB, userspace may change the default "allow wakeup" |
| 467 | * policy through sysfs or these calls. |
| 468 | * |
| 469 | * Most root hubs support wakeup from downstream devices, for |
| 470 | * runtime power management (disabling USB clocks and reducing |
| 471 | * VBUS power usage). However, not all of them do so; silicon, |
| 472 | * board, and BIOS bugs here are not uncommon, so these can't |
| 473 | * be treated quite like external hubs. |
| 474 | * |
| 475 | * Likewise, not all root hubs will pass wakeup events upstream, |
| 476 | * to wake up the whole system. So don't assume root hub and |
| 477 | * controller capabilities are identical. |
| 478 | */ |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | case DeviceRequest | USB_REQ_GET_STATUS: |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 481 | tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev) |
| 482 | << USB_DEVICE_REMOTE_WAKEUP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | | (1 << USB_DEVICE_SELF_POWERED); |
| 484 | tbuf [1] = 0; |
| 485 | len = 2; |
| 486 | break; |
| 487 | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: |
| 488 | if (wValue == USB_DEVICE_REMOTE_WAKEUP) |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 489 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | else |
| 491 | goto error; |
| 492 | break; |
| 493 | case DeviceOutRequest | USB_REQ_SET_FEATURE: |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 494 | if (device_can_wakeup(&hcd->self.root_hub->dev) |
| 495 | && wValue == USB_DEVICE_REMOTE_WAKEUP) |
| 496 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | else |
| 498 | goto error; |
| 499 | break; |
| 500 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
| 501 | tbuf [0] = 1; |
| 502 | len = 1; |
| 503 | /* FALLTHROUGH */ |
| 504 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 505 | break; |
| 506 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 507 | switch (wValue & 0xff00) { |
| 508 | case USB_DT_DEVICE << 8: |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 509 | switch (hcd->driver->flags & HCD_MASK) { |
| 510 | case HCD_USB3: |
Sarah Sharp | d2e9b4d | 2009-04-27 19:55:01 -0700 | [diff] [blame] | 511 | bufp = usb3_rh_dev_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 512 | break; |
| 513 | case HCD_USB2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | bufp = usb2_rh_dev_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 515 | break; |
| 516 | case HCD_USB11: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | bufp = usb11_rh_dev_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 518 | break; |
| 519 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | goto error; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | len = 18; |
Alan Stern | 7329e21 | 2008-04-03 18:02:56 -0400 | [diff] [blame] | 523 | if (hcd->has_tt) |
| 524 | patch_protocol = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | break; |
| 526 | case USB_DT_CONFIG << 8: |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 527 | switch (hcd->driver->flags & HCD_MASK) { |
| 528 | case HCD_USB3: |
Sarah Sharp | d2e9b4d | 2009-04-27 19:55:01 -0700 | [diff] [blame] | 529 | bufp = ss_rh_config_descriptor; |
| 530 | len = sizeof ss_rh_config_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 531 | break; |
| 532 | case HCD_USB2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | bufp = hs_rh_config_descriptor; |
| 534 | len = sizeof hs_rh_config_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 535 | break; |
| 536 | case HCD_USB11: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | bufp = fs_rh_config_descriptor; |
| 538 | len = sizeof fs_rh_config_descriptor; |
Viral Mehta | 7dd19e6 | 2009-05-05 15:54:23 +0530 | [diff] [blame] | 539 | break; |
| 540 | default: |
| 541 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 543 | if (device_can_wakeup(&hcd->self.root_hub->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | patch_wakeup = 1; |
| 545 | break; |
| 546 | case USB_DT_STRING << 8: |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 547 | if ((wValue & 0xff) < 4) |
| 548 | urb->actual_length = rh_string(wValue & 0xff, |
| 549 | hcd, ubuf, wLength); |
| 550 | else /* unsupported IDs --> "protocol stall" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | break; |
| 553 | default: |
| 554 | goto error; |
| 555 | } |
| 556 | break; |
| 557 | case DeviceRequest | USB_REQ_GET_INTERFACE: |
| 558 | tbuf [0] = 0; |
| 559 | len = 1; |
| 560 | /* FALLTHROUGH */ |
| 561 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: |
| 562 | break; |
| 563 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
| 564 | // wValue == urb->dev->devaddr |
| 565 | dev_dbg (hcd->self.controller, "root hub device address %d\n", |
| 566 | wValue); |
| 567 | break; |
| 568 | |
| 569 | /* INTERFACE REQUESTS (no defined feature/status flags) */ |
| 570 | |
| 571 | /* ENDPOINT REQUESTS */ |
| 572 | |
| 573 | case EndpointRequest | USB_REQ_GET_STATUS: |
| 574 | // ENDPOINT_HALT flag |
| 575 | tbuf [0] = 0; |
| 576 | tbuf [1] = 0; |
| 577 | len = 2; |
| 578 | /* FALLTHROUGH */ |
| 579 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
| 580 | case EndpointOutRequest | USB_REQ_SET_FEATURE: |
| 581 | dev_dbg (hcd->self.controller, "no endpoint features yet\n"); |
| 582 | break; |
| 583 | |
| 584 | /* CLASS REQUESTS (and errors) */ |
| 585 | |
| 586 | default: |
| 587 | /* non-generic request */ |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 588 | switch (typeReq) { |
| 589 | case GetHubStatus: |
| 590 | case GetPortStatus: |
| 591 | len = 4; |
| 592 | break; |
| 593 | case GetHubDescriptor: |
| 594 | len = sizeof (struct usb_hub_descriptor); |
| 595 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 597 | status = hcd->driver->hub_control (hcd, |
| 598 | typeReq, wValue, wIndex, |
| 599 | tbuf, wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | break; |
| 601 | error: |
| 602 | /* "protocol stall" on error */ |
| 603 | status = -EPIPE; |
| 604 | } |
| 605 | |
| 606 | if (status) { |
| 607 | len = 0; |
| 608 | if (status != -EPIPE) { |
| 609 | dev_dbg (hcd->self.controller, |
| 610 | "CTRL: TypeReq=0x%x val=0x%x " |
| 611 | "idx=0x%x len=%d ==> %d\n", |
| 612 | typeReq, wValue, wIndex, |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 613 | wLength, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | if (len) { |
| 617 | if (urb->transfer_buffer_length < len) |
| 618 | len = urb->transfer_buffer_length; |
| 619 | urb->actual_length = len; |
| 620 | // always USB_DIR_IN, toward host |
| 621 | memcpy (ubuf, bufp, len); |
| 622 | |
| 623 | /* report whether RH hardware supports remote wakeup */ |
| 624 | if (patch_wakeup && |
| 625 | len > offsetof (struct usb_config_descriptor, |
| 626 | bmAttributes)) |
| 627 | ((struct usb_config_descriptor *)ubuf)->bmAttributes |
| 628 | |= USB_CONFIG_ATT_WAKEUP; |
Alan Stern | 7329e21 | 2008-04-03 18:02:56 -0400 | [diff] [blame] | 629 | |
| 630 | /* report whether RH hardware has an integrated TT */ |
| 631 | if (patch_protocol && |
| 632 | len > offsetof(struct usb_device_descriptor, |
| 633 | bDeviceProtocol)) |
| 634 | ((struct usb_device_descriptor *) ubuf)-> |
| 635 | bDeviceProtocol = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* any errors get returned through the urb completion */ |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 639 | spin_lock_irq(&hcd_root_hub_lock); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 640 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 641 | |
| 642 | /* This peculiar use of spinlocks echoes what real HC drivers do. |
| 643 | * Avoiding calls to local_irq_disable/enable makes the code |
| 644 | * RT-friendly. |
| 645 | */ |
| 646 | spin_unlock(&hcd_root_hub_lock); |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 647 | usb_hcd_giveback_urb(hcd, urb, status); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 648 | spin_lock(&hcd_root_hub_lock); |
| 649 | |
| 650 | spin_unlock_irq(&hcd_root_hub_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /*-------------------------------------------------------------------------*/ |
| 655 | |
| 656 | /* |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 657 | * Root Hub interrupt transfers are polled using a timer if the |
| 658 | * driver requests it; otherwise the driver is responsible for |
| 659 | * calling usb_hcd_poll_rh_status() when an event occurs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 661 | * Completions are called in_interrupt(), but they may or may not |
| 662 | * be in_irq(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 664 | void usb_hcd_poll_rh_status(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
| 666 | struct urb *urb; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 667 | int length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | unsigned long flags; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 669 | char buffer[6]; /* Any root hubs with > 31 ports? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Alan Stern | 1b42ae6 | 2007-03-13 11:10:52 -0400 | [diff] [blame] | 671 | if (unlikely(!hcd->rh_registered)) |
| 672 | return; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 673 | if (!hcd->uses_new_polling && !hcd->status_urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 676 | length = hcd->driver->hub_status_data(hcd, buffer); |
| 677 | if (length > 0) { |
| 678 | |
| 679 | /* try to complete the status urb */ |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 680 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 681 | urb = hcd->status_urb; |
| 682 | if (urb) { |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 683 | hcd->poll_pending = 0; |
| 684 | hcd->status_urb = NULL; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 685 | urb->actual_length = length; |
| 686 | memcpy(urb->transfer_buffer, buffer, length); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 687 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 688 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 689 | spin_unlock(&hcd_root_hub_lock); |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 690 | usb_hcd_giveback_urb(hcd, urb, 0); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 691 | spin_lock(&hcd_root_hub_lock); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 692 | } else { |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 693 | length = 0; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 694 | hcd->poll_pending = 1; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 695 | } |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 696 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* The USB 2.0 spec says 256 ms. This is close enough and won't |
Arjan van de Ven | 01cd081 | 2007-05-22 12:42:56 -0700 | [diff] [blame] | 700 | * exceed that limit if HZ is 100. The math is more clunky than |
| 701 | * maybe expected, this is to make sure that all timers for USB devices |
| 702 | * fire at the same time to give the CPU a break inbetween */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 703 | if (hcd->uses_new_polling ? hcd->poll_rh : |
| 704 | (length == 0 && hcd->status_urb != NULL)) |
Arjan van de Ven | 01cd081 | 2007-05-22 12:42:56 -0700 | [diff] [blame] | 705 | mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 706 | } |
| 707 | EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); |
| 708 | |
| 709 | /* timer callback */ |
| 710 | static void rh_timer_func (unsigned long _hcd) |
| 711 | { |
| 712 | usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /*-------------------------------------------------------------------------*/ |
| 716 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 717 | static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) |
| 718 | { |
| 719 | int retval; |
| 720 | unsigned long flags; |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 721 | unsigned len = 1 + (urb->dev->maxchild / 8); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 722 | |
| 723 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 724 | if (hcd->status_urb || urb->transfer_buffer_length < len) { |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 725 | dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); |
| 726 | retval = -EINVAL; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 727 | goto done; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 728 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 729 | |
| 730 | retval = usb_hcd_link_urb_to_ep(hcd, urb); |
| 731 | if (retval) |
| 732 | goto done; |
| 733 | |
| 734 | hcd->status_urb = urb; |
| 735 | urb->hcpriv = hcd; /* indicate it's queued */ |
| 736 | if (!hcd->uses_new_polling) |
| 737 | mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
| 738 | |
| 739 | /* If a status change has already occurred, report it ASAP */ |
| 740 | else if (hcd->poll_pending) |
| 741 | mod_timer(&hcd->rh_timer, jiffies); |
| 742 | retval = 0; |
| 743 | done: |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 744 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 745 | return retval; |
| 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) |
| 749 | { |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 750 | if (usb_endpoint_xfer_int(&urb->ep->desc)) |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 751 | return rh_queue_status (hcd, urb); |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 752 | if (usb_endpoint_xfer_control(&urb->ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return rh_call_control (hcd, urb); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 754 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /*-------------------------------------------------------------------------*/ |
| 758 | |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 759 | /* Unlinks of root-hub control URBs are legal, but they don't do anything |
| 760 | * since these URBs always execute synchronously. |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 761 | */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 762 | static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 764 | unsigned long flags; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 765 | int rc; |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 766 | |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 767 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 768 | rc = usb_hcd_check_unlink_urb(hcd, urb, status); |
| 769 | if (rc) |
| 770 | goto done; |
| 771 | |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 772 | if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */ |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 773 | ; /* Do nothing */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 774 | |
| 775 | } else { /* Status URB */ |
| 776 | if (!hcd->uses_new_polling) |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 777 | del_timer (&hcd->rh_timer); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 778 | if (urb == hcd->status_urb) { |
| 779 | hcd->status_urb = NULL; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 780 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 782 | spin_unlock(&hcd_root_hub_lock); |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 783 | usb_hcd_giveback_urb(hcd, urb, status); |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 784 | spin_lock(&hcd_root_hub_lock); |
| 785 | } |
| 786 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 787 | done: |
Alan Stern | 9439eb9 | 2007-08-02 15:05:45 -0400 | [diff] [blame] | 788 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 789 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Inaky Perez-Gonzalez | 5234ce1 | 2007-07-31 20:33:58 -0700 | [diff] [blame] | 792 | |
| 793 | |
| 794 | /* |
| 795 | * Show & store the current value of authorized_default |
| 796 | */ |
| 797 | static ssize_t usb_host_authorized_default_show(struct device *dev, |
| 798 | struct device_attribute *attr, |
| 799 | char *buf) |
| 800 | { |
| 801 | struct usb_device *rh_usb_dev = to_usb_device(dev); |
| 802 | struct usb_bus *usb_bus = rh_usb_dev->bus; |
| 803 | struct usb_hcd *usb_hcd; |
| 804 | |
| 805 | if (usb_bus == NULL) /* FIXME: not sure if this case is possible */ |
| 806 | return -ENODEV; |
| 807 | usb_hcd = bus_to_hcd(usb_bus); |
| 808 | return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default); |
| 809 | } |
| 810 | |
| 811 | static ssize_t usb_host_authorized_default_store(struct device *dev, |
| 812 | struct device_attribute *attr, |
| 813 | const char *buf, size_t size) |
| 814 | { |
| 815 | ssize_t result; |
| 816 | unsigned val; |
| 817 | struct usb_device *rh_usb_dev = to_usb_device(dev); |
| 818 | struct usb_bus *usb_bus = rh_usb_dev->bus; |
| 819 | struct usb_hcd *usb_hcd; |
| 820 | |
| 821 | if (usb_bus == NULL) /* FIXME: not sure if this case is possible */ |
| 822 | return -ENODEV; |
| 823 | usb_hcd = bus_to_hcd(usb_bus); |
| 824 | result = sscanf(buf, "%u\n", &val); |
| 825 | if (result == 1) { |
| 826 | usb_hcd->authorized_default = val? 1 : 0; |
| 827 | result = size; |
| 828 | } |
| 829 | else |
| 830 | result = -EINVAL; |
| 831 | return result; |
| 832 | } |
| 833 | |
| 834 | static DEVICE_ATTR(authorized_default, 0644, |
| 835 | usb_host_authorized_default_show, |
| 836 | usb_host_authorized_default_store); |
| 837 | |
| 838 | |
| 839 | /* Group all the USB bus attributes */ |
| 840 | static struct attribute *usb_bus_attrs[] = { |
| 841 | &dev_attr_authorized_default.attr, |
| 842 | NULL, |
| 843 | }; |
| 844 | |
| 845 | static struct attribute_group usb_bus_attr_group = { |
| 846 | .name = NULL, /* we want them in the same directory */ |
| 847 | .attrs = usb_bus_attrs, |
| 848 | }; |
| 849 | |
| 850 | |
| 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | /*-------------------------------------------------------------------------*/ |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /** |
| 855 | * usb_bus_init - shared initialization code |
| 856 | * @bus: the bus structure being initialized |
| 857 | * |
| 858 | * This code is used to initialize a usb_bus structure, memory for which is |
| 859 | * separately managed. |
| 860 | */ |
| 861 | static void usb_bus_init (struct usb_bus *bus) |
| 862 | { |
| 863 | memset (&bus->devmap, 0, sizeof(struct usb_devmap)); |
| 864 | |
| 865 | bus->devnum_next = 1; |
| 866 | |
| 867 | bus->root_hub = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | bus->busnum = -1; |
| 869 | bus->bandwidth_allocated = 0; |
| 870 | bus->bandwidth_int_reqs = 0; |
| 871 | bus->bandwidth_isoc_reqs = 0; |
| 872 | |
| 873 | INIT_LIST_HEAD (&bus->bus_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | /*-------------------------------------------------------------------------*/ |
| 877 | |
| 878 | /** |
| 879 | * usb_register_bus - registers the USB host controller with the usb core |
| 880 | * @bus: pointer to the bus to register |
| 881 | * Context: !in_interrupt() |
| 882 | * |
| 883 | * Assigns a bus number, and links the controller into usbcore data |
| 884 | * structures so that it can be seen by scanning the bus list. |
| 885 | */ |
| 886 | static int usb_register_bus(struct usb_bus *bus) |
| 887 | { |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 888 | int result = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | int busnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 891 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 893 | if (busnum >= USB_MAXBUS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | printk (KERN_ERR "%s: too many buses\n", usbcore_name); |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 895 | goto error_find_busnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 897 | set_bit (busnum, busmap.busmap); |
| 898 | bus->busnum = busnum; |
Tony Jones | 5a3201b | 2007-09-11 14:07:31 -0700 | [diff] [blame] | 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | /* Add it to the local list of buses */ |
| 901 | list_add (&bus->bus_list, &usb_bus_list); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 902 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 904 | usb_notify_add_bus(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 906 | dev_info (bus->controller, "new USB bus registered, assigned bus " |
| 907 | "number %d\n", bus->busnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return 0; |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 909 | |
Inaky Perez-Gonzalez | eb579f5 | 2007-07-31 20:33:59 -0700 | [diff] [blame] | 910 | error_find_busnum: |
| 911 | mutex_unlock(&usb_bus_list_lock); |
| 912 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
| 916 | * usb_deregister_bus - deregisters the USB host controller |
| 917 | * @bus: pointer to the bus to deregister |
| 918 | * Context: !in_interrupt() |
| 919 | * |
| 920 | * Recycles the bus number, and unlinks the controller from usbcore data |
| 921 | * structures so that it won't be seen by scanning the bus list. |
| 922 | */ |
| 923 | static void usb_deregister_bus (struct usb_bus *bus) |
| 924 | { |
| 925 | dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); |
| 926 | |
| 927 | /* |
| 928 | * NOTE: make sure that all the devices are removed by the |
| 929 | * controller code, as well as having it call this when cleaning |
| 930 | * itself up |
| 931 | */ |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 932 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | list_del (&bus->bus_list); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 934 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 936 | usb_notify_remove_bus(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | clear_bit (bus->busnum, busmap.busmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 942 | * register_root_hub - called by usb_add_hcd() to register a root hub |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | * @hcd: host controller for this root hub |
| 944 | * |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 945 | * This function registers the root hub with the USB subsystem. It sets up |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 946 | * the device properly in the device tree and then calls usb_new_device() |
| 947 | * to register the usb device. It also assigns the root hub's USB address |
| 948 | * (always 1). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | */ |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 950 | static int register_root_hub(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
| 952 | struct device *parent_dev = hcd->self.controller; |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 953 | struct usb_device *usb_dev = hcd->self.root_hub; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | const int devnum = 1; |
| 955 | int retval; |
| 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | usb_dev->devnum = devnum; |
| 958 | usb_dev->bus->devnum_next = devnum + 1; |
| 959 | memset (&usb_dev->bus->devmap.devicemap, 0, |
| 960 | sizeof usb_dev->bus->devmap.devicemap); |
| 961 | set_bit (devnum, usb_dev->bus->devmap.devicemap); |
| 962 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); |
| 963 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 964 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 966 | usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); |
| 968 | if (retval != sizeof usb_dev->descriptor) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 969 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
Kay Sievers | 7071a3c | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 971 | dev_name(&usb_dev->dev), retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | return (retval < 0) ? retval : -EMSGSIZE; |
| 973 | } |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | retval = usb_new_device (usb_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | if (retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | dev_err (parent_dev, "can't register root hub for %s, %d\n", |
Kay Sievers | 7071a3c | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 978 | dev_name(&usb_dev->dev), retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 980 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | if (retval == 0) { |
| 983 | spin_lock_irq (&hcd_root_hub_lock); |
| 984 | hcd->rh_registered = 1; |
| 985 | spin_unlock_irq (&hcd_root_hub_lock); |
| 986 | |
| 987 | /* Did the HC die before the root hub was registered? */ |
| 988 | if (hcd->state == HC_STATE_HALT) |
| 989 | usb_hc_died (hcd); /* This time clean up */ |
| 990 | } |
| 991 | |
| 992 | return retval; |
| 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | |
| 996 | /*-------------------------------------------------------------------------*/ |
| 997 | |
| 998 | /** |
| 999 | * usb_calc_bus_time - approximate periodic transaction time in nanoseconds |
| 1000 | * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} |
| 1001 | * @is_input: true iff the transaction sends data to the host |
| 1002 | * @isoc: true for isochronous transactions, false for interrupt ones |
| 1003 | * @bytecount: how many bytes in the transaction. |
| 1004 | * |
| 1005 | * Returns approximate bus time in nanoseconds for a periodic transaction. |
| 1006 | * See USB 2.0 spec section 5.11.3; only periodic transfers need to be |
| 1007 | * scheduled in software, this function is only used for such scheduling. |
| 1008 | */ |
| 1009 | long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) |
| 1010 | { |
| 1011 | unsigned long tmp; |
| 1012 | |
| 1013 | switch (speed) { |
| 1014 | case USB_SPEED_LOW: /* INTR only */ |
| 1015 | if (is_input) { |
| 1016 | tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1017 | return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); |
| 1018 | } else { |
| 1019 | tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1020 | return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); |
| 1021 | } |
| 1022 | case USB_SPEED_FULL: /* ISOC or INTR */ |
| 1023 | if (isoc) { |
| 1024 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1025 | return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp); |
| 1026 | } else { |
| 1027 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1028 | return (9107L + BW_HOST_DELAY + tmp); |
| 1029 | } |
| 1030 | case USB_SPEED_HIGH: /* ISOC or INTR */ |
| 1031 | // FIXME adjust for input vs output |
| 1032 | if (isoc) |
Dan Streetman | 498f78e | 2005-07-29 12:18:28 -0700 | [diff] [blame] | 1033 | tmp = HS_NSECS_ISO (bytecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | else |
Dan Streetman | 498f78e | 2005-07-29 12:18:28 -0700 | [diff] [blame] | 1035 | tmp = HS_NSECS (bytecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | return tmp; |
| 1037 | default: |
| 1038 | pr_debug ("%s: bogus device speed!\n", usbcore_name); |
| 1039 | return -1; |
| 1040 | } |
| 1041 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1042 | EXPORT_SYMBOL_GPL(usb_calc_bus_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | /*-------------------------------------------------------------------------*/ |
| 1046 | |
| 1047 | /* |
| 1048 | * Generic HC operations. |
| 1049 | */ |
| 1050 | |
| 1051 | /*-------------------------------------------------------------------------*/ |
| 1052 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1053 | /** |
| 1054 | * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue |
| 1055 | * @hcd: host controller to which @urb was submitted |
| 1056 | * @urb: URB being submitted |
| 1057 | * |
| 1058 | * Host controller drivers should call this routine in their enqueue() |
| 1059 | * method. The HCD's private spinlock must be held and interrupts must |
| 1060 | * be disabled. The actions carried out here are required for URB |
| 1061 | * submission, as well as for endpoint shutdown and for usb_kill_urb. |
| 1062 | * |
| 1063 | * Returns 0 for no error, otherwise a negative error code (in which case |
| 1064 | * the enqueue() method must fail). If no error occurs but enqueue() fails |
| 1065 | * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing |
| 1066 | * the private spinlock and returning. |
| 1067 | */ |
| 1068 | int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb) |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1069 | { |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1070 | int rc = 0; |
| 1071 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1072 | spin_lock(&hcd_urb_list_lock); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1073 | |
| 1074 | /* Check that the URB isn't being killed */ |
Ming Lei | 49367d8 | 2008-12-12 21:38:45 +0800 | [diff] [blame] | 1075 | if (unlikely(atomic_read(&urb->reject))) { |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1076 | rc = -EPERM; |
| 1077 | goto done; |
| 1078 | } |
| 1079 | |
| 1080 | if (unlikely(!urb->ep->enabled)) { |
| 1081 | rc = -ENOENT; |
| 1082 | goto done; |
| 1083 | } |
| 1084 | |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 1085 | if (unlikely(!urb->dev->can_submit)) { |
| 1086 | rc = -EHOSTUNREACH; |
| 1087 | goto done; |
| 1088 | } |
| 1089 | |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1090 | /* |
| 1091 | * Check the host controller's state and add the URB to the |
| 1092 | * endpoint's queue. |
| 1093 | */ |
| 1094 | switch (hcd->state) { |
| 1095 | case HC_STATE_RUNNING: |
| 1096 | case HC_STATE_RESUMING: |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1097 | urb->unlinked = 0; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1098 | list_add_tail(&urb->urb_list, &urb->ep->urb_list); |
| 1099 | break; |
| 1100 | default: |
| 1101 | rc = -ESHUTDOWN; |
| 1102 | goto done; |
| 1103 | } |
| 1104 | done: |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1105 | spin_unlock(&hcd_urb_list_lock); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1106 | return rc; |
| 1107 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1108 | EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1109 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1110 | /** |
| 1111 | * usb_hcd_check_unlink_urb - check whether an URB may be unlinked |
| 1112 | * @hcd: host controller to which @urb was submitted |
| 1113 | * @urb: URB being checked for unlinkability |
| 1114 | * @status: error code to store in @urb if the unlink succeeds |
| 1115 | * |
| 1116 | * Host controller drivers should call this routine in their dequeue() |
| 1117 | * method. The HCD's private spinlock must be held and interrupts must |
| 1118 | * be disabled. The actions carried out here are required for making |
| 1119 | * sure than an unlink is valid. |
| 1120 | * |
| 1121 | * Returns 0 for no error, otherwise a negative error code (in which case |
| 1122 | * the dequeue() method must fail). The possible error codes are: |
| 1123 | * |
| 1124 | * -EIDRM: @urb was not submitted or has already completed. |
| 1125 | * The completion function may not have been called yet. |
| 1126 | * |
| 1127 | * -EBUSY: @urb has already been unlinked. |
| 1128 | */ |
| 1129 | int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb, |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1130 | int status) |
| 1131 | { |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1132 | struct list_head *tmp; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1133 | |
| 1134 | /* insist the urb is still queued */ |
| 1135 | list_for_each(tmp, &urb->ep->urb_list) { |
| 1136 | if (tmp == &urb->urb_list) |
| 1137 | break; |
| 1138 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1139 | if (tmp != &urb->urb_list) |
| 1140 | return -EIDRM; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1141 | |
| 1142 | /* Any status except -EINPROGRESS means something already started to |
| 1143 | * unlink this URB from the hardware. So there's no more work to do. |
| 1144 | */ |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1145 | if (urb->unlinked) |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1146 | return -EBUSY; |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1147 | urb->unlinked = status; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1148 | |
| 1149 | /* IRQ setup can easily be broken so that USB controllers |
| 1150 | * never get completion IRQs ... maybe even the ones we need to |
| 1151 | * finish unlinking the initial failed usb_set_address() |
| 1152 | * or device descriptor fetch. |
| 1153 | */ |
| 1154 | if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) && |
| 1155 | !is_root_hub(urb->dev)) { |
| 1156 | dev_warn(hcd->self.controller, "Unlink after no-IRQ? " |
| 1157 | "Controller is probably using the wrong IRQ.\n"); |
| 1158 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
| 1159 | } |
| 1160 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1161 | return 0; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1162 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1163 | EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1164 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1165 | /** |
| 1166 | * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue |
| 1167 | * @hcd: host controller to which @urb was submitted |
| 1168 | * @urb: URB being unlinked |
| 1169 | * |
| 1170 | * Host controller drivers should call this routine before calling |
| 1171 | * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and |
| 1172 | * interrupts must be disabled. The actions carried out here are required |
| 1173 | * for URB completion. |
| 1174 | */ |
| 1175 | void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | /* clear all state linking urb to this dev (and hcd) */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1178 | spin_lock(&hcd_urb_list_lock); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1179 | list_del_init(&urb->urb_list); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1180 | spin_unlock(&hcd_urb_list_lock); |
Pete Zaitcev | 9f6a93f | 2007-06-08 13:37:49 -0700 | [diff] [blame] | 1181 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1182 | EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1184 | /* |
| 1185 | * Some usb host controllers can only perform dma using a small SRAM area. |
| 1186 | * The usb core itself is however optimized for host controllers that can dma |
| 1187 | * using regular system memory - like pci devices doing bus mastering. |
| 1188 | * |
| 1189 | * To support host controllers with limited dma capabilites we provide dma |
| 1190 | * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag. |
| 1191 | * For this to work properly the host controller code must first use the |
| 1192 | * function dma_declare_coherent_memory() to point out which memory area |
| 1193 | * that should be used for dma allocations. |
| 1194 | * |
| 1195 | * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for |
| 1196 | * dma using dma_alloc_coherent() which in turn allocates from the memory |
| 1197 | * area pointed out with dma_declare_coherent_memory(). |
| 1198 | * |
| 1199 | * So, to summarize... |
| 1200 | * |
| 1201 | * - We need "local" memory, canonical example being |
| 1202 | * a small SRAM on a discrete controller being the |
| 1203 | * only memory that the controller can read ... |
| 1204 | * (a) "normal" kernel memory is no good, and |
| 1205 | * (b) there's not enough to share |
| 1206 | * |
| 1207 | * - The only *portable* hook for such stuff in the |
| 1208 | * DMA framework is dma_declare_coherent_memory() |
| 1209 | * |
| 1210 | * - So we use that, even though the primary requirement |
| 1211 | * is that the memory be "local" (hence addressible |
| 1212 | * by that device), not "coherent". |
| 1213 | * |
| 1214 | */ |
| 1215 | |
| 1216 | static int hcd_alloc_coherent(struct usb_bus *bus, |
| 1217 | gfp_t mem_flags, dma_addr_t *dma_handle, |
| 1218 | void **vaddr_handle, size_t size, |
| 1219 | enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | { |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1221 | unsigned char *vaddr; |
| 1222 | |
| 1223 | vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr), |
| 1224 | mem_flags, dma_handle); |
| 1225 | if (!vaddr) |
| 1226 | return -ENOMEM; |
| 1227 | |
| 1228 | /* |
| 1229 | * Store the virtual address of the buffer at the end |
| 1230 | * of the allocated dma buffer. The size of the buffer |
| 1231 | * may be uneven so use unaligned functions instead |
| 1232 | * of just rounding up. It makes sense to optimize for |
| 1233 | * memory footprint over access speed since the amount |
| 1234 | * of memory available for dma may be limited. |
| 1235 | */ |
| 1236 | put_unaligned((unsigned long)*vaddr_handle, |
| 1237 | (unsigned long *)(vaddr + size)); |
| 1238 | |
| 1239 | if (dir == DMA_TO_DEVICE) |
| 1240 | memcpy(vaddr, *vaddr_handle, size); |
| 1241 | |
| 1242 | *vaddr_handle = vaddr; |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, |
| 1247 | void **vaddr_handle, size_t size, |
| 1248 | enum dma_data_direction dir) |
| 1249 | { |
| 1250 | unsigned char *vaddr = *vaddr_handle; |
| 1251 | |
| 1252 | vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size)); |
| 1253 | |
| 1254 | if (dir == DMA_FROM_DEVICE) |
| 1255 | memcpy(vaddr, *vaddr_handle, size); |
| 1256 | |
| 1257 | hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle); |
| 1258 | |
| 1259 | *vaddr_handle = vaddr; |
| 1260 | *dma_handle = 0; |
| 1261 | } |
| 1262 | |
| 1263 | static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
| 1264 | gfp_t mem_flags) |
| 1265 | { |
| 1266 | enum dma_data_direction dir; |
| 1267 | int ret = 0; |
| 1268 | |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1269 | /* Map the URB's buffers for DMA access. |
| 1270 | * Lower level HCD code should use *_dma exclusively, |
Sarah Sharp | e04748e | 2009-04-27 19:59:01 -0700 | [diff] [blame] | 1271 | * unless it uses pio or talks to another transport, |
| 1272 | * or uses the provided scatter gather list for bulk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | */ |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1274 | if (is_root_hub(urb->dev)) |
| 1275 | return 0; |
| 1276 | |
| 1277 | if (usb_endpoint_xfer_control(&urb->ep->desc) |
| 1278 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) { |
Larry Finger | 85e034f | 2009-11-05 10:37:03 -0600 | [diff] [blame] | 1279 | if (hcd->self.uses_dma) { |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1280 | urb->setup_dma = dma_map_single( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | hcd->self.controller, |
| 1282 | urb->setup_packet, |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1283 | sizeof(struct usb_ctrlrequest), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | DMA_TO_DEVICE); |
Larry Finger | 85e034f | 2009-11-05 10:37:03 -0600 | [diff] [blame] | 1285 | if (dma_mapping_error(hcd->self.controller, |
| 1286 | urb->setup_dma)) |
| 1287 | return -EAGAIN; |
| 1288 | } else if (hcd->driver->flags & HCD_LOCAL_MEM) |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1289 | ret = hcd_alloc_coherent( |
| 1290 | urb->dev->bus, mem_flags, |
| 1291 | &urb->setup_dma, |
| 1292 | (void **)&urb->setup_packet, |
| 1293 | sizeof(struct usb_ctrlrequest), |
| 1294 | DMA_TO_DEVICE); |
| 1295 | } |
| 1296 | |
| 1297 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1298 | if (ret == 0 && urb->transfer_buffer_length != 0 |
| 1299 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { |
Larry Finger | 85e034f | 2009-11-05 10:37:03 -0600 | [diff] [blame] | 1300 | if (hcd->self.uses_dma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | urb->transfer_dma = dma_map_single ( |
| 1302 | hcd->self.controller, |
| 1303 | urb->transfer_buffer, |
| 1304 | urb->transfer_buffer_length, |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1305 | dir); |
Larry Finger | 85e034f | 2009-11-05 10:37:03 -0600 | [diff] [blame] | 1306 | if (dma_mapping_error(hcd->self.controller, |
| 1307 | urb->transfer_dma)) |
| 1308 | return -EAGAIN; |
| 1309 | } else if (hcd->driver->flags & HCD_LOCAL_MEM) { |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1310 | ret = hcd_alloc_coherent( |
| 1311 | urb->dev->bus, mem_flags, |
| 1312 | &urb->transfer_dma, |
| 1313 | &urb->transfer_buffer, |
| 1314 | urb->transfer_buffer_length, |
| 1315 | dir); |
| 1316 | |
| 1317 | if (ret && usb_endpoint_xfer_control(&urb->ep->desc) |
| 1318 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) |
| 1319 | hcd_free_coherent(urb->dev->bus, |
| 1320 | &urb->setup_dma, |
| 1321 | (void **)&urb->setup_packet, |
| 1322 | sizeof(struct usb_ctrlrequest), |
| 1323 | DMA_TO_DEVICE); |
| 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1326 | return ret; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1329 | static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 1330 | { |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1331 | enum dma_data_direction dir; |
| 1332 | |
| 1333 | if (is_root_hub(urb->dev)) |
| 1334 | return; |
| 1335 | |
| 1336 | if (usb_endpoint_xfer_control(&urb->ep->desc) |
| 1337 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) { |
| 1338 | if (hcd->self.uses_dma) |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1339 | dma_unmap_single(hcd->self.controller, urb->setup_dma, |
| 1340 | sizeof(struct usb_ctrlrequest), |
| 1341 | DMA_TO_DEVICE); |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1342 | else if (hcd->driver->flags & HCD_LOCAL_MEM) |
| 1343 | hcd_free_coherent(urb->dev->bus, &urb->setup_dma, |
| 1344 | (void **)&urb->setup_packet, |
| 1345 | sizeof(struct usb_ctrlrequest), |
| 1346 | DMA_TO_DEVICE); |
| 1347 | } |
| 1348 | |
| 1349 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1350 | if (urb->transfer_buffer_length != 0 |
| 1351 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { |
| 1352 | if (hcd->self.uses_dma) |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1353 | dma_unmap_single(hcd->self.controller, |
| 1354 | urb->transfer_dma, |
| 1355 | urb->transfer_buffer_length, |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1356 | dir); |
| 1357 | else if (hcd->driver->flags & HCD_LOCAL_MEM) |
| 1358 | hcd_free_coherent(urb->dev->bus, &urb->transfer_dma, |
| 1359 | &urb->transfer_buffer, |
| 1360 | urb->transfer_buffer_length, |
| 1361 | dir); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /*-------------------------------------------------------------------------*/ |
| 1366 | |
| 1367 | /* may be called in any context with a valid urb->dev usecount |
| 1368 | * caller surrenders "ownership" of urb |
| 1369 | * expects usb_submit_urb() to have sanity checked and conditioned all |
| 1370 | * inputs in the urb |
| 1371 | */ |
| 1372 | int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) |
| 1373 | { |
| 1374 | int status; |
| 1375 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
| 1376 | |
| 1377 | /* increment urb's reference count as part of giving it to the HCD |
| 1378 | * (which will control it). HCD guarantees that it either returns |
| 1379 | * an error or calls giveback(), but not both. |
| 1380 | */ |
| 1381 | usb_get_urb(urb); |
| 1382 | atomic_inc(&urb->use_count); |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 1383 | atomic_inc(&urb->dev->urbnum); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1384 | usbmon_urb_submit(&hcd->self, urb); |
| 1385 | |
| 1386 | /* NOTE requirements on root-hub callers (usbfs and the hub |
| 1387 | * driver, for now): URBs' urb->transfer_buffer must be |
| 1388 | * valid and usb_buffer_{sync,unmap}() not be needed, since |
| 1389 | * they could clobber root hub response data. Also, control |
| 1390 | * URBs must be submitted in process context with interrupts |
| 1391 | * enabled. |
| 1392 | */ |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1393 | status = map_urb_for_dma(hcd, urb, mem_flags); |
| 1394 | if (unlikely(status)) { |
| 1395 | usbmon_urb_submit_error(&hcd->self, urb, status); |
| 1396 | goto error; |
| 1397 | } |
| 1398 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1399 | if (is_root_hub(urb->dev)) |
| 1400 | status = rh_urb_enqueue(hcd, urb); |
| 1401 | else |
| 1402 | status = hcd->driver->urb_enqueue(hcd, urb, mem_flags); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1403 | |
| 1404 | if (unlikely(status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | usbmon_urb_submit_error(&hcd->self, urb, status); |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1406 | unmap_urb_for_dma(hcd, urb); |
Magnus Damm | b347667 | 2008-01-23 15:58:35 +0900 | [diff] [blame] | 1407 | error: |
Alan Stern | b0d9efb | 2007-08-21 15:39:21 -0400 | [diff] [blame] | 1408 | urb->hcpriv = NULL; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1409 | INIT_LIST_HEAD(&urb->urb_list); |
| 1410 | atomic_dec(&urb->use_count); |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 1411 | atomic_dec(&urb->dev->urbnum); |
Ming Lei | 49367d8 | 2008-12-12 21:38:45 +0800 | [diff] [blame] | 1412 | if (atomic_read(&urb->reject)) |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1413 | wake_up(&usb_kill_urb_queue); |
| 1414 | usb_put_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | return status; |
| 1417 | } |
| 1418 | |
| 1419 | /*-------------------------------------------------------------------------*/ |
| 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | /* this makes the hcd giveback() the urb more quickly, by kicking it |
| 1422 | * off hardware queues (which may take a while) and returning it as |
| 1423 | * soon as practical. we've already set up the urb's return status, |
| 1424 | * but we can't know if the callback completed already. |
| 1425 | */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1426 | static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | { |
| 1428 | int value; |
| 1429 | |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 1430 | if (is_root_hub(urb->dev)) |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1431 | value = usb_rh_urb_dequeue(hcd, urb, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | else { |
| 1433 | |
| 1434 | /* The only reason an HCD might fail this call is if |
| 1435 | * it has not yet fully queued the urb to begin with. |
| 1436 | * Such failures should be harmless. */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1437 | value = hcd->driver->urb_dequeue(hcd, urb, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | return value; |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * called in any context |
| 1444 | * |
| 1445 | * caller guarantees urb won't be recycled till both unlink() |
| 1446 | * and the urb's completion function return |
| 1447 | */ |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 1448 | int usb_hcd_unlink_urb (struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1450 | struct usb_hcd *hcd; |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 1451 | int retval = -EIDRM; |
| 1452 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 1454 | /* Prevent the device and bus from going away while |
| 1455 | * the unlink is carried out. If they are already gone |
| 1456 | * then urb->use_count must be 0, since disconnected |
| 1457 | * devices can't have any active URBs. |
| 1458 | */ |
| 1459 | spin_lock_irqsave(&hcd_urb_unlink_lock, flags); |
| 1460 | if (atomic_read(&urb->use_count) > 0) { |
| 1461 | retval = 0; |
| 1462 | usb_get_dev(urb->dev); |
| 1463 | } |
| 1464 | spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags); |
| 1465 | if (retval == 0) { |
| 1466 | hcd = bus_to_hcd(urb->dev->bus); |
| 1467 | retval = unlink1(hcd, urb, status); |
| 1468 | usb_put_dev(urb->dev); |
| 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | if (retval == 0) |
| 1472 | retval = -EINPROGRESS; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1473 | else if (retval != -EIDRM && retval != -EBUSY) |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1474 | dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n", |
| 1475 | urb, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | return retval; |
| 1477 | } |
| 1478 | |
| 1479 | /*-------------------------------------------------------------------------*/ |
| 1480 | |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1481 | /** |
| 1482 | * usb_hcd_giveback_urb - return URB from HCD to device driver |
| 1483 | * @hcd: host controller returning the URB |
| 1484 | * @urb: urb being returned to the USB device driver. |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1485 | * @status: completion status code for the URB. |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1486 | * Context: in_interrupt() |
| 1487 | * |
| 1488 | * This hands the URB from HCD to its USB device driver, using its |
| 1489 | * completion function. The HCD has freed all per-urb resources |
| 1490 | * (and is done using urb->hcpriv). It also released all HCD locks; |
| 1491 | * the device driver won't cause problems if it frees, modifies, |
| 1492 | * or resubmits this URB. |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1493 | * |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1494 | * If @urb was unlinked, the value of @status will be overridden by |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1495 | * @urb->unlinked. Erroneous short transfers are detected in case |
| 1496 | * the HCD hasn't checked for them. |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1497 | */ |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1498 | void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1499 | { |
Alan Stern | b0d9efb | 2007-08-21 15:39:21 -0400 | [diff] [blame] | 1500 | urb->hcpriv = NULL; |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1501 | if (unlikely(urb->unlinked)) |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1502 | status = urb->unlinked; |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1503 | else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && |
Alan Stern | b0d9efb | 2007-08-21 15:39:21 -0400 | [diff] [blame] | 1504 | urb->actual_length < urb->transfer_buffer_length && |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1505 | !status)) |
| 1506 | status = -EREMOTEIO; |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1507 | |
Alan Stern | 1f5a3d0 | 2007-08-22 13:06:53 -0400 | [diff] [blame] | 1508 | unmap_urb_for_dma(hcd, urb); |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1509 | usbmon_urb_complete(&hcd->self, urb, status); |
Alan Stern | 1f5a3d0 | 2007-08-22 13:06:53 -0400 | [diff] [blame] | 1510 | usb_unanchor_urb(urb); |
| 1511 | |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1512 | /* pass ownership to the completion handler */ |
Alan Stern | 4a00027 | 2007-08-24 15:42:24 -0400 | [diff] [blame] | 1513 | urb->status = status; |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1514 | urb->complete (urb); |
| 1515 | atomic_dec (&urb->use_count); |
Ming Lei | 49367d8 | 2008-12-12 21:38:45 +0800 | [diff] [blame] | 1516 | if (unlikely(atomic_read(&urb->reject))) |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1517 | wake_up (&usb_kill_urb_queue); |
| 1518 | usb_put_urb (urb); |
| 1519 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1520 | EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb); |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1521 | |
| 1522 | /*-------------------------------------------------------------------------*/ |
| 1523 | |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1524 | /* Cancel all URBs pending on this endpoint and wait for the endpoint's |
| 1525 | * queue to drain completely. The caller must first insure that no more |
| 1526 | * URBs can be submitted for this endpoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | */ |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1528 | void usb_hcd_flush_endpoint(struct usb_device *udev, |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 1529 | struct usb_host_endpoint *ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
| 1531 | struct usb_hcd *hcd; |
| 1532 | struct urb *urb; |
| 1533 | |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1534 | if (!ep) |
| 1535 | return; |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1536 | might_sleep(); |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1537 | hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1539 | /* No more submits can occur */ |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1540 | spin_lock_irq(&hcd_urb_list_lock); |
Alan Stern | ddc1fd6 | 2007-11-21 15:13:10 -0800 | [diff] [blame] | 1541 | rescan: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | list_for_each_entry (urb, &ep->urb_list, urb_list) { |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 1543 | int is_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Alan Stern | eb23105 | 2007-08-21 15:40:36 -0400 | [diff] [blame] | 1545 | if (urb->unlinked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | continue; |
| 1547 | usb_get_urb (urb); |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 1548 | is_in = usb_urb_dir_in(urb); |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 1549 | spin_unlock(&hcd_urb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1551 | /* kick hcd */ |
| 1552 | unlink1(hcd, urb, -ESHUTDOWN); |
| 1553 | dev_dbg (hcd->self.controller, |
| 1554 | "shutdown urb %p ep%d%s%s\n", |
| 1555 | urb, usb_endpoint_num(&ep->desc), |
| 1556 | is_in ? "in" : "out", |
| 1557 | ({ char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1559 | switch (usb_endpoint_type(&ep->desc)) { |
| 1560 | case USB_ENDPOINT_XFER_CONTROL: |
| 1561 | s = ""; break; |
| 1562 | case USB_ENDPOINT_XFER_BULK: |
| 1563 | s = "-bulk"; break; |
| 1564 | case USB_ENDPOINT_XFER_INT: |
| 1565 | s = "-intr"; break; |
| 1566 | default: |
| 1567 | s = "-iso"; break; |
| 1568 | }; |
| 1569 | s; |
| 1570 | })); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | usb_put_urb (urb); |
| 1572 | |
| 1573 | /* list contents may have changed */ |
Alan Stern | ddc1fd6 | 2007-11-21 15:13:10 -0800 | [diff] [blame] | 1574 | spin_lock(&hcd_urb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | goto rescan; |
| 1576 | } |
Alan Stern | 9a9bf40 | 2007-08-02 15:06:54 -0400 | [diff] [blame] | 1577 | spin_unlock_irq(&hcd_urb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1579 | /* Wait until the endpoint queue is completely empty */ |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1580 | while (!list_empty (&ep->urb_list)) { |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 1581 | spin_lock_irq(&hcd_urb_list_lock); |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1582 | |
| 1583 | /* The list may have changed while we acquired the spinlock */ |
| 1584 | urb = NULL; |
| 1585 | if (!list_empty (&ep->urb_list)) { |
| 1586 | urb = list_entry (ep->urb_list.prev, struct urb, |
| 1587 | urb_list); |
| 1588 | usb_get_urb (urb); |
| 1589 | } |
Alan Stern | 809a58b | 2007-07-18 12:14:24 -0400 | [diff] [blame] | 1590 | spin_unlock_irq(&hcd_urb_list_lock); |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1591 | |
| 1592 | if (urb) { |
| 1593 | usb_kill_urb (urb); |
| 1594 | usb_put_urb (urb); |
| 1595 | } |
| 1596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1599 | /** |
Randy Dunlap | 70445ae | 2009-12-13 10:17:16 -0800 | [diff] [blame] | 1600 | * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds |
| 1601 | * the bus bandwidth |
| 1602 | * @udev: target &usb_device |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1603 | * @new_config: new configuration to install |
| 1604 | * @cur_alt: the current alternate interface setting |
| 1605 | * @new_alt: alternate interface setting that is being installed |
| 1606 | * |
| 1607 | * To change configurations, pass in the new configuration in new_config, |
| 1608 | * and pass NULL for cur_alt and new_alt. |
| 1609 | * |
| 1610 | * To reset a device's configuration (put the device in the ADDRESSED state), |
| 1611 | * pass in NULL for new_config, cur_alt, and new_alt. |
| 1612 | * |
| 1613 | * To change alternate interface settings, pass in NULL for new_config, |
| 1614 | * pass in the current alternate interface setting in cur_alt, |
| 1615 | * and pass in the new alternate interface setting in new_alt. |
| 1616 | * |
| 1617 | * Returns an error if the requested bandwidth change exceeds the |
| 1618 | * bus bandwidth or host controller internal resources. |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1619 | */ |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1620 | int usb_hcd_alloc_bandwidth(struct usb_device *udev, |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1621 | struct usb_host_config *new_config, |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1622 | struct usb_host_interface *cur_alt, |
| 1623 | struct usb_host_interface *new_alt) |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1624 | { |
| 1625 | int num_intfs, i, j; |
H Hartley Sweeten | 576a362 | 2009-11-17 14:53:41 -0800 | [diff] [blame] | 1626 | struct usb_host_interface *alt = NULL; |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1627 | int ret = 0; |
| 1628 | struct usb_hcd *hcd; |
| 1629 | struct usb_host_endpoint *ep; |
| 1630 | |
| 1631 | hcd = bus_to_hcd(udev->bus); |
| 1632 | if (!hcd->driver->check_bandwidth) |
| 1633 | return 0; |
| 1634 | |
| 1635 | /* Configuration is being removed - set configuration 0 */ |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1636 | if (!new_config && !cur_alt) { |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1637 | for (i = 1; i < 16; ++i) { |
| 1638 | ep = udev->ep_out[i]; |
| 1639 | if (ep) |
| 1640 | hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1641 | ep = udev->ep_in[i]; |
| 1642 | if (ep) |
| 1643 | hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1644 | } |
| 1645 | hcd->driver->check_bandwidth(hcd, udev); |
| 1646 | return 0; |
| 1647 | } |
| 1648 | /* Check if the HCD says there's enough bandwidth. Enable all endpoints |
| 1649 | * each interface's alt setting 0 and ask the HCD to check the bandwidth |
| 1650 | * of the bus. There will always be bandwidth for endpoint 0, so it's |
| 1651 | * ok to exclude it. |
| 1652 | */ |
| 1653 | if (new_config) { |
| 1654 | num_intfs = new_config->desc.bNumInterfaces; |
| 1655 | /* Remove endpoints (except endpoint 0, which is always on the |
| 1656 | * schedule) from the old config from the schedule |
| 1657 | */ |
| 1658 | for (i = 1; i < 16; ++i) { |
| 1659 | ep = udev->ep_out[i]; |
| 1660 | if (ep) { |
| 1661 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1662 | if (ret < 0) |
| 1663 | goto reset; |
| 1664 | } |
| 1665 | ep = udev->ep_in[i]; |
| 1666 | if (ep) { |
| 1667 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1668 | if (ret < 0) |
| 1669 | goto reset; |
| 1670 | } |
| 1671 | } |
| 1672 | for (i = 0; i < num_intfs; ++i) { |
Sarah Sharp | 91017f9 | 2009-12-03 09:44:34 -0800 | [diff] [blame] | 1673 | /* Set up endpoints for alternate interface setting 0 */ |
| 1674 | alt = usb_find_alt_setting(new_config, i, 0); |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1675 | if (!alt) |
| 1676 | /* No alt setting 0? Pick the first setting. */ |
| 1677 | alt = &new_config->intf_cache[i]->altsetting[0]; |
| 1678 | |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1679 | for (j = 0; j < alt->desc.bNumEndpoints; j++) { |
| 1680 | ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); |
| 1681 | if (ret < 0) |
| 1682 | goto reset; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1686 | if (cur_alt && new_alt) { |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame^] | 1687 | struct usb_interface *iface = usb_ifnum_to_if(udev, |
| 1688 | cur_alt->desc.bInterfaceNumber); |
| 1689 | |
| 1690 | if (iface->resetting_device) { |
| 1691 | /* |
| 1692 | * The USB core just reset the device, so the xHCI host |
| 1693 | * and the device will think alt setting 0 is installed. |
| 1694 | * However, the USB core will pass in the alternate |
| 1695 | * setting installed before the reset as cur_alt. Dig |
| 1696 | * out the alternate setting 0 structure, or the first |
| 1697 | * alternate setting if a broken device doesn't have alt |
| 1698 | * setting 0. |
| 1699 | */ |
| 1700 | cur_alt = usb_altnum_to_altsetting(iface, 0); |
| 1701 | if (!cur_alt) |
| 1702 | cur_alt = &iface->altsetting[0]; |
| 1703 | } |
| 1704 | |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 1705 | /* Drop all the endpoints in the current alt setting */ |
| 1706 | for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) { |
| 1707 | ret = hcd->driver->drop_endpoint(hcd, udev, |
| 1708 | &cur_alt->endpoint[i]); |
| 1709 | if (ret < 0) |
| 1710 | goto reset; |
| 1711 | } |
| 1712 | /* Add all the endpoints in the new alt setting */ |
| 1713 | for (i = 0; i < new_alt->desc.bNumEndpoints; i++) { |
| 1714 | ret = hcd->driver->add_endpoint(hcd, udev, |
| 1715 | &new_alt->endpoint[i]); |
| 1716 | if (ret < 0) |
| 1717 | goto reset; |
| 1718 | } |
| 1719 | } |
Sarah Sharp | 79abb1a | 2009-04-27 19:58:26 -0700 | [diff] [blame] | 1720 | ret = hcd->driver->check_bandwidth(hcd, udev); |
| 1721 | reset: |
| 1722 | if (ret < 0) |
| 1723 | hcd->driver->reset_bandwidth(hcd, udev); |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1727 | /* Disables the endpoint: synchronizes with the hcd to make sure all |
| 1728 | * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must |
| 1729 | * have been called previously. Use for set_configuration, set_interface, |
| 1730 | * driver removal, physical disconnect. |
| 1731 | * |
| 1732 | * example: a qh stored in ep->hcpriv, holding state related to endpoint |
| 1733 | * type, maxpacket size, toggle, halt status, and scheduling. |
| 1734 | */ |
| 1735 | void usb_hcd_disable_endpoint(struct usb_device *udev, |
| 1736 | struct usb_host_endpoint *ep) |
| 1737 | { |
| 1738 | struct usb_hcd *hcd; |
| 1739 | |
| 1740 | might_sleep(); |
| 1741 | hcd = bus_to_hcd(udev->bus); |
| 1742 | if (hcd->driver->endpoint_disable) |
| 1743 | hcd->driver->endpoint_disable(hcd, ep); |
| 1744 | } |
| 1745 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1746 | /** |
| 1747 | * usb_hcd_reset_endpoint - reset host endpoint state |
| 1748 | * @udev: USB device. |
| 1749 | * @ep: the endpoint to reset. |
| 1750 | * |
| 1751 | * Resets any host endpoint state such as the toggle bit, sequence |
| 1752 | * number and current window. |
| 1753 | */ |
| 1754 | void usb_hcd_reset_endpoint(struct usb_device *udev, |
| 1755 | struct usb_host_endpoint *ep) |
| 1756 | { |
| 1757 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1758 | |
| 1759 | if (hcd->driver->endpoint_reset) |
| 1760 | hcd->driver->endpoint_reset(hcd, ep); |
| 1761 | else { |
| 1762 | int epnum = usb_endpoint_num(&ep->desc); |
| 1763 | int is_out = usb_endpoint_dir_out(&ep->desc); |
| 1764 | int is_control = usb_endpoint_xfer_control(&ep->desc); |
| 1765 | |
| 1766 | usb_settoggle(udev, epnum, is_out, 0); |
| 1767 | if (is_control) |
| 1768 | usb_settoggle(udev, epnum, !is_out, 0); |
| 1769 | } |
| 1770 | } |
| 1771 | |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 1772 | /* Protect against drivers that try to unlink URBs after the device |
| 1773 | * is gone, by waiting until all unlinks for @udev are finished. |
| 1774 | * Since we don't currently track URBs by device, simply wait until |
| 1775 | * nothing is running in the locked region of usb_hcd_unlink_urb(). |
| 1776 | */ |
| 1777 | void usb_hcd_synchronize_unlinks(struct usb_device *udev) |
| 1778 | { |
| 1779 | spin_lock_irq(&hcd_urb_unlink_lock); |
| 1780 | spin_unlock_irq(&hcd_urb_unlink_lock); |
| 1781 | } |
| 1782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /*-------------------------------------------------------------------------*/ |
| 1784 | |
Alan Stern | 32aca56 | 2007-07-18 12:08:02 -0400 | [diff] [blame] | 1785 | /* called in any context */ |
| 1786 | int usb_hcd_get_frame_number (struct usb_device *udev) |
| 1787 | { |
| 1788 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1789 | |
| 1790 | if (!HC_IS_RUNNING (hcd->state)) |
| 1791 | return -ESHUTDOWN; |
| 1792 | return hcd->driver->get_frame_number (hcd); |
| 1793 | } |
| 1794 | |
| 1795 | /*-------------------------------------------------------------------------*/ |
| 1796 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1797 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1799 | int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | { |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1801 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
| 1802 | int status; |
| 1803 | int old_state = hcd->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1805 | dev_dbg(&rhdev->dev, "bus %s%s\n", |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1806 | (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend"); |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1807 | if (!hcd->driver->bus_suspend) { |
| 1808 | status = -ENOENT; |
| 1809 | } else { |
| 1810 | hcd->state = HC_STATE_QUIESCING; |
| 1811 | status = hcd->driver->bus_suspend(hcd); |
| 1812 | } |
| 1813 | if (status == 0) { |
| 1814 | usb_set_device_state(rhdev, USB_STATE_SUSPENDED); |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1815 | hcd->state = HC_STATE_SUSPENDED; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1816 | } else { |
| 1817 | hcd->state = old_state; |
| 1818 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1819 | "suspend", status); |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1820 | } |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1821 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1824 | int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | { |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1826 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
| 1827 | int status; |
Alan Stern | cfa59da | 2007-06-21 16:25:35 -0400 | [diff] [blame] | 1828 | int old_state = hcd->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1830 | dev_dbg(&rhdev->dev, "usb %s%s\n", |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1831 | (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume"); |
Alan Stern | 0c0382e | 2005-10-13 17:08:02 -0400 | [diff] [blame] | 1832 | if (!hcd->driver->bus_resume) |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1833 | return -ENOENT; |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1834 | if (hcd->state == HC_STATE_RUNNING) |
| 1835 | return 0; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1836 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1837 | hcd->state = HC_STATE_RESUMING; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1838 | status = hcd->driver->bus_resume(hcd); |
| 1839 | if (status == 0) { |
| 1840 | /* TRSMRCY = 10 msec */ |
| 1841 | msleep(10); |
| 1842 | usb_set_device_state(rhdev, rhdev->actconfig |
| 1843 | ? USB_STATE_CONFIGURED |
| 1844 | : USB_STATE_ADDRESS); |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1845 | hcd->state = HC_STATE_RUNNING; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1846 | } else { |
Alan Stern | cfa59da | 2007-06-21 16:25:35 -0400 | [diff] [blame] | 1847 | hcd->state = old_state; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 1848 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1849 | "resume", status); |
Alan Stern | cfa59da | 2007-06-21 16:25:35 -0400 | [diff] [blame] | 1850 | if (status != -ESHUTDOWN) |
| 1851 | usb_hc_died(hcd); |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1852 | } |
| 1853 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1856 | /* Workqueue routine for root-hub remote wakeup */ |
| 1857 | static void hcd_resume_work(struct work_struct *work) |
| 1858 | { |
| 1859 | struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); |
| 1860 | struct usb_device *udev = hcd->self.root_hub; |
| 1861 | |
| 1862 | usb_lock_device(udev); |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1863 | usb_mark_last_busy(udev); |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1864 | usb_external_resume_device(udev, PMSG_REMOTE_RESUME); |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1865 | usb_unlock_device(udev); |
| 1866 | } |
| 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | /** |
| 1869 | * usb_hcd_resume_root_hub - called by HCD to resume its root hub |
| 1870 | * @hcd: host controller for this root hub |
| 1871 | * |
| 1872 | * The USB host controller calls this function when its root hub is |
| 1873 | * suspended (with the remote wakeup feature enabled) and a remote |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1874 | * wakeup request is received. The routine submits a workqueue request |
| 1875 | * to resume the root hub (that is, manage its downstream ports again). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | */ |
| 1877 | void usb_hcd_resume_root_hub (struct usb_hcd *hcd) |
| 1878 | { |
| 1879 | unsigned long flags; |
| 1880 | |
| 1881 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 1882 | if (hcd->rh_registered) |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1883 | queue_work(ksuspend_usb_wq, &hcd->wakeup_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 1885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); |
| 1887 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1888 | #endif |
| 1889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | /*-------------------------------------------------------------------------*/ |
| 1891 | |
| 1892 | #ifdef CONFIG_USB_OTG |
| 1893 | |
| 1894 | /** |
| 1895 | * usb_bus_start_enum - start immediate enumeration (for OTG) |
| 1896 | * @bus: the bus (must use hcd framework) |
| 1897 | * @port_num: 1-based number of port; usually bus->otg_port |
| 1898 | * Context: in_interrupt() |
| 1899 | * |
| 1900 | * Starts enumeration, with an immediate reset followed later by |
| 1901 | * khubd identifying and possibly configuring the device. |
| 1902 | * This is needed by OTG controller drivers, where it helps meet |
| 1903 | * HNP protocol timing requirements for starting a port reset. |
| 1904 | */ |
| 1905 | int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) |
| 1906 | { |
| 1907 | struct usb_hcd *hcd; |
| 1908 | int status = -EOPNOTSUPP; |
| 1909 | |
| 1910 | /* NOTE: since HNP can't start by grabbing the bus's address0_sem, |
| 1911 | * boards with root hubs hooked up to internal devices (instead of |
| 1912 | * just the OTG port) may need more attention to resetting... |
| 1913 | */ |
| 1914 | hcd = container_of (bus, struct usb_hcd, self); |
| 1915 | if (port_num && hcd->driver->start_port_reset) |
| 1916 | status = hcd->driver->start_port_reset(hcd, port_num); |
| 1917 | |
| 1918 | /* run khubd shortly after (first) root port reset finishes; |
| 1919 | * it may issue others, until at least 50 msecs have passed. |
| 1920 | */ |
| 1921 | if (status == 0) |
| 1922 | mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); |
| 1923 | return status; |
| 1924 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1925 | EXPORT_SYMBOL_GPL(usb_bus_start_enum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
| 1927 | #endif |
| 1928 | |
| 1929 | /*-------------------------------------------------------------------------*/ |
| 1930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | * usb_hcd_irq - hook IRQs to HCD framework (bus glue) |
| 1933 | * @irq: the IRQ being raised |
| 1934 | * @__hcd: pointer to the HCD whose IRQ is being signaled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | * |
| 1936 | * If the controller isn't HALTed, calls the driver's irq handler. |
| 1937 | * Checks whether the controller is now dead. |
| 1938 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1939 | irqreturn_t usb_hcd_irq (int irq, void *__hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | { |
| 1941 | struct usb_hcd *hcd = __hcd; |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 1942 | unsigned long flags; |
| 1943 | irqreturn_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 1945 | /* IRQF_DISABLED doesn't work correctly with shared IRQs |
| 1946 | * when the first handler doesn't use it. So let's just |
| 1947 | * assume it's never used. |
| 1948 | */ |
| 1949 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 1951 | if (unlikely(hcd->state == HC_STATE_HALT || |
| 1952 | !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) { |
| 1953 | rc = IRQ_NONE; |
| 1954 | } else if (hcd->driver->irq(hcd) == IRQ_NONE) { |
| 1955 | rc = IRQ_NONE; |
| 1956 | } else { |
| 1957 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1958 | |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 1959 | if (unlikely(hcd->state == HC_STATE_HALT)) |
| 1960 | usb_hc_died(hcd); |
| 1961 | rc = IRQ_HANDLED; |
| 1962 | } |
| 1963 | |
| 1964 | local_irq_restore(flags); |
| 1965 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /*-------------------------------------------------------------------------*/ |
| 1969 | |
| 1970 | /** |
| 1971 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) |
| 1972 | * @hcd: pointer to the HCD representing the controller |
| 1973 | * |
| 1974 | * This is called by bus glue to report a USB host controller that died |
| 1975 | * while operations may still have been pending. It's called automatically |
| 1976 | * by the PCI glue, so only glue for non-PCI busses should need to call it. |
| 1977 | */ |
| 1978 | void usb_hc_died (struct usb_hcd *hcd) |
| 1979 | { |
| 1980 | unsigned long flags; |
| 1981 | |
| 1982 | dev_err (hcd->self.controller, "HC died; cleaning up\n"); |
| 1983 | |
| 1984 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 1985 | if (hcd->rh_registered) { |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 1986 | hcd->poll_rh = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | |
| 1988 | /* make khubd clean up old urbs and devices */ |
| 1989 | usb_set_device_state (hcd->self.root_hub, |
| 1990 | USB_STATE_NOTATTACHED); |
| 1991 | usb_kick_khubd (hcd->self.root_hub); |
| 1992 | } |
| 1993 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 1994 | } |
| 1995 | EXPORT_SYMBOL_GPL (usb_hc_died); |
| 1996 | |
| 1997 | /*-------------------------------------------------------------------------*/ |
| 1998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | /** |
| 2000 | * usb_create_hcd - create and initialize an HCD structure |
| 2001 | * @driver: HC driver that will use this hcd |
| 2002 | * @dev: device for this HC, stored in hcd->self.controller |
| 2003 | * @bus_name: value to store in hcd->self.bus_name |
| 2004 | * Context: !in_interrupt() |
| 2005 | * |
| 2006 | * Allocate a struct usb_hcd, with extra space at the end for the |
| 2007 | * HC driver's private data. Initialize the generic members of the |
| 2008 | * hcd structure. |
| 2009 | * |
| 2010 | * If memory is unavailable, returns NULL. |
| 2011 | */ |
| 2012 | struct usb_hcd *usb_create_hcd (const struct hc_driver *driver, |
Greg Kroah-Hartman | 1b26da1 | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 2013 | struct device *dev, const char *bus_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | { |
| 2015 | struct usb_hcd *hcd; |
| 2016 | |
Pekka Enberg | 7b842b6 | 2005-09-06 15:18:34 -0700 | [diff] [blame] | 2017 | hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | if (!hcd) { |
| 2019 | dev_dbg (dev, "hcd alloc failed\n"); |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | dev_set_drvdata(dev, hcd); |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 2023 | kref_init(&hcd->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
| 2025 | usb_bus_init(&hcd->self); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | hcd->self.controller = dev; |
| 2027 | hcd->self.bus_name = bus_name; |
Alan Stern | dd990f1 | 2006-08-30 11:29:56 -0400 | [diff] [blame] | 2028 | hcd->self.uses_dma = (dev->dma_mask != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
| 2030 | init_timer(&hcd->rh_timer); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 2031 | hcd->rh_timer.function = rh_timer_func; |
| 2032 | hcd->rh_timer.data = (unsigned long) hcd; |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 2033 | #ifdef CONFIG_PM |
| 2034 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
| 2035 | #endif |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 2036 | mutex_init(&hcd->bandwidth_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
| 2038 | hcd->driver = driver; |
| 2039 | hcd->product_desc = (driver->product_desc) ? driver->product_desc : |
| 2040 | "USB Host Controller"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | return hcd; |
| 2042 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2043 | EXPORT_SYMBOL_GPL(usb_create_hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 2045 | static void hcd_release (struct kref *kref) |
| 2046 | { |
| 2047 | struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); |
| 2048 | |
| 2049 | kfree(hcd); |
| 2050 | } |
| 2051 | |
| 2052 | struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd) |
| 2053 | { |
| 2054 | if (hcd) |
| 2055 | kref_get (&hcd->kref); |
| 2056 | return hcd; |
| 2057 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2058 | EXPORT_SYMBOL_GPL(usb_get_hcd); |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | void usb_put_hcd (struct usb_hcd *hcd) |
| 2061 | { |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 2062 | if (hcd) |
| 2063 | kref_put (&hcd->kref, hcd_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2065 | EXPORT_SYMBOL_GPL(usb_put_hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
| 2067 | /** |
| 2068 | * usb_add_hcd - finish generic HCD structure initialization and register |
| 2069 | * @hcd: the usb_hcd structure to initialize |
| 2070 | * @irqnum: Interrupt line to allocate |
| 2071 | * @irqflags: Interrupt type flags |
| 2072 | * |
| 2073 | * Finish the remaining parts of generic HCD initialization: allocate the |
| 2074 | * buffers of consistent memory, register the bus, request the IRQ line, |
| 2075 | * and call the driver's reset() and start() routines. |
| 2076 | */ |
| 2077 | int usb_add_hcd(struct usb_hcd *hcd, |
| 2078 | unsigned int irqnum, unsigned long irqflags) |
| 2079 | { |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2080 | int retval; |
| 2081 | struct usb_device *rhdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | |
| 2083 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
| 2084 | |
Inaky Perez-Gonzalez | 5234ce1 | 2007-07-31 20:33:58 -0700 | [diff] [blame] | 2085 | hcd->authorized_default = hcd->wireless? 0 : 1; |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 2086 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 2087 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2088 | /* HC is in reset state, but accessible. Now do the one-time init, |
| 2089 | * bottom up so that hcds can customize the root hubs before khubd |
| 2090 | * starts talking to them. (Note, bus id is assigned early too.) |
| 2091 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | if ((retval = hcd_buffer_create(hcd)) != 0) { |
| 2093 | dev_dbg(hcd->self.controller, "pool alloc failed\n"); |
| 2094 | return retval; |
| 2095 | } |
| 2096 | |
| 2097 | if ((retval = usb_register_bus(&hcd->self)) < 0) |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2098 | goto err_register_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2100 | if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) { |
| 2101 | dev_err(hcd->self.controller, "unable to allocate root hub\n"); |
| 2102 | retval = -ENOMEM; |
| 2103 | goto err_allocate_root_hub; |
| 2104 | } |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 2105 | |
| 2106 | switch (hcd->driver->flags & HCD_MASK) { |
| 2107 | case HCD_USB11: |
| 2108 | rhdev->speed = USB_SPEED_FULL; |
| 2109 | break; |
| 2110 | case HCD_USB2: |
| 2111 | rhdev->speed = USB_SPEED_HIGH; |
| 2112 | break; |
| 2113 | case HCD_USB3: |
| 2114 | rhdev->speed = USB_SPEED_SUPER; |
| 2115 | break; |
| 2116 | default: |
| 2117 | goto err_allocate_root_hub; |
| 2118 | } |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2119 | hcd->self.root_hub = rhdev; |
| 2120 | |
David Brownell | db4cefa | 2006-05-01 22:07:13 -0700 | [diff] [blame] | 2121 | /* wakeup flag init defaults to "everything works" for root hubs, |
| 2122 | * but drivers can override it in reset() if needed, along with |
| 2123 | * recording the overall controller's system wakeup capability. |
| 2124 | */ |
| 2125 | device_init_wakeup(&rhdev->dev, 1); |
| 2126 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2127 | /* "reset" is misnamed; its role is now one-time init. the controller |
| 2128 | * should already have been reset (and boot firmware kicked off etc). |
| 2129 | */ |
| 2130 | if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { |
| 2131 | dev_err(hcd->self.controller, "can't setup\n"); |
| 2132 | goto err_hcd_driver_setup; |
| 2133 | } |
| 2134 | |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 2135 | /* NOTE: root hub and controller capabilities may not be the same */ |
| 2136 | if (device_can_wakeup(hcd->self.controller) |
| 2137 | && device_can_wakeup(&hcd->self.root_hub->dev)) |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2138 | dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2139 | |
| 2140 | /* enable irqs just before we start the controller */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | if (hcd->driver->irq) { |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 2142 | |
| 2143 | /* IRQF_DISABLED doesn't work as advertised when used together |
| 2144 | * with IRQF_SHARED. As usb_hcd_irq() will always disable |
| 2145 | * interrupts we can remove it here. |
| 2146 | */ |
Geoff Levand | 83a7982 | 2008-08-22 14:13:00 -0700 | [diff] [blame] | 2147 | if (irqflags & IRQF_SHARED) |
| 2148 | irqflags &= ~IRQF_DISABLED; |
Stefan Becker | de85422 | 2008-07-01 19:19:22 +0300 | [diff] [blame] | 2149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", |
| 2151 | hcd->driver->description, hcd->self.busnum); |
| 2152 | if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags, |
| 2153 | hcd->irq_descr, hcd)) != 0) { |
| 2154 | dev_err(hcd->self.controller, |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 2155 | "request interrupt %d failed\n", irqnum); |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2156 | goto err_request_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
| 2158 | hcd->irq = irqnum; |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 2159 | dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | (hcd->driver->flags & HCD_MEMORY) ? |
| 2161 | "io mem" : "io base", |
| 2162 | (unsigned long long)hcd->rsrc_start); |
| 2163 | } else { |
| 2164 | hcd->irq = -1; |
| 2165 | if (hcd->rsrc_start) |
| 2166 | dev_info(hcd->self.controller, "%s 0x%08llx\n", |
| 2167 | (hcd->driver->flags & HCD_MEMORY) ? |
| 2168 | "io mem" : "io base", |
| 2169 | (unsigned long long)hcd->rsrc_start); |
| 2170 | } |
| 2171 | |
| 2172 | if ((retval = hcd->driver->start(hcd)) < 0) { |
| 2173 | dev_err(hcd->self.controller, "startup error %d\n", retval); |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2174 | goto err_hcd_driver_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2177 | /* starting here, usbcore will pay attention to this root hub */ |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 2178 | rhdev->bus_mA = min(500u, hcd->power_budget); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2179 | if ((retval = register_root_hub(hcd)) != 0) |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2180 | goto err_register_root_hub; |
| 2181 | |
Inaky Perez-Gonzalez | 5234ce1 | 2007-07-31 20:33:58 -0700 | [diff] [blame] | 2182 | retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
| 2183 | if (retval < 0) { |
| 2184 | printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n", |
| 2185 | retval); |
| 2186 | goto error_create_attr_group; |
| 2187 | } |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 2188 | if (hcd->uses_new_polling && hcd->poll_rh) |
| 2189 | usb_hcd_poll_rh_status(hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | return retval; |
| 2191 | |
Inaky Perez-Gonzalez | 5234ce1 | 2007-07-31 20:33:58 -0700 | [diff] [blame] | 2192 | error_create_attr_group: |
| 2193 | mutex_lock(&usb_bus_list_lock); |
| 2194 | usb_disconnect(&hcd->self.root_hub); |
| 2195 | mutex_unlock(&usb_bus_list_lock); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2196 | err_register_root_hub: |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 2197 | hcd->driver->stop(hcd); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2198 | err_hcd_driver_start: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | if (hcd->irq >= 0) |
| 2200 | free_irq(irqnum, hcd); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2201 | err_request_irq: |
| 2202 | err_hcd_driver_setup: |
| 2203 | hcd->self.root_hub = NULL; |
| 2204 | usb_put_dev(rhdev); |
| 2205 | err_allocate_root_hub: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | usb_deregister_bus(&hcd->self); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 2207 | err_register_bus: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | hcd_buffer_destroy(hcd); |
| 2209 | return retval; |
| 2210 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2211 | EXPORT_SYMBOL_GPL(usb_add_hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | |
| 2213 | /** |
| 2214 | * usb_remove_hcd - shutdown processing for generic HCDs |
| 2215 | * @hcd: the usb_hcd structure to remove |
| 2216 | * Context: !in_interrupt() |
| 2217 | * |
| 2218 | * Disconnects the root hub, then reverses the effects of usb_add_hcd(), |
| 2219 | * invoking the HCD's stop() method. |
| 2220 | */ |
| 2221 | void usb_remove_hcd(struct usb_hcd *hcd) |
| 2222 | { |
| 2223 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
| 2224 | |
| 2225 | if (HC_IS_RUNNING (hcd->state)) |
| 2226 | hcd->state = HC_STATE_QUIESCING; |
| 2227 | |
| 2228 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); |
| 2229 | spin_lock_irq (&hcd_root_hub_lock); |
| 2230 | hcd->rh_registered = 0; |
| 2231 | spin_unlock_irq (&hcd_root_hub_lock); |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 2232 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 2233 | #ifdef CONFIG_PM |
Alan Stern | d5d4db7 | 2007-05-29 16:34:52 -0400 | [diff] [blame] | 2234 | cancel_work_sync(&hcd->wakeup_work); |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 2235 | #endif |
| 2236 | |
Inaky Perez-Gonzalez | 5234ce1 | 2007-07-31 20:33:58 -0700 | [diff] [blame] | 2237 | sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 2238 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | usb_disconnect(&hcd->self.root_hub); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 2240 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
| 2242 | hcd->driver->stop(hcd); |
| 2243 | hcd->state = HC_STATE_HALT; |
| 2244 | |
Alan Stern | 1b42ae6 | 2007-03-13 11:10:52 -0400 | [diff] [blame] | 2245 | hcd->poll_rh = 0; |
| 2246 | del_timer_sync(&hcd->rh_timer); |
| 2247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | if (hcd->irq >= 0) |
| 2249 | free_irq(hcd->irq, hcd); |
| 2250 | usb_deregister_bus(&hcd->self); |
| 2251 | hcd_buffer_destroy(hcd); |
| 2252 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2253 | EXPORT_SYMBOL_GPL(usb_remove_hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | |
Aleksey Gorelov | 64a21d0 | 2006-08-08 17:24:08 -0700 | [diff] [blame] | 2255 | void |
| 2256 | usb_hcd_platform_shutdown(struct platform_device* dev) |
| 2257 | { |
| 2258 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 2259 | |
| 2260 | if (hcd->driver->shutdown) |
| 2261 | hcd->driver->shutdown(hcd); |
| 2262 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 2263 | EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
Aleksey Gorelov | 64a21d0 | 2006-08-08 17:24:08 -0700 | [diff] [blame] | 2264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | /*-------------------------------------------------------------------------*/ |
| 2266 | |
Pete Zaitcev | f150fa1 | 2008-11-13 21:31:21 -0700 | [diff] [blame] | 2267 | #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
| 2269 | struct usb_mon_operations *mon_ops; |
| 2270 | |
| 2271 | /* |
| 2272 | * The registration is unlocked. |
| 2273 | * We do it this way because we do not want to lock in hot paths. |
| 2274 | * |
| 2275 | * Notice that the code is minimally error-proof. Because usbmon needs |
| 2276 | * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. |
| 2277 | */ |
| 2278 | |
| 2279 | int usb_mon_register (struct usb_mon_operations *ops) |
| 2280 | { |
| 2281 | |
| 2282 | if (mon_ops) |
| 2283 | return -EBUSY; |
| 2284 | |
| 2285 | mon_ops = ops; |
| 2286 | mb(); |
| 2287 | return 0; |
| 2288 | } |
| 2289 | EXPORT_SYMBOL_GPL (usb_mon_register); |
| 2290 | |
| 2291 | void usb_mon_deregister (void) |
| 2292 | { |
| 2293 | |
| 2294 | if (mon_ops == NULL) { |
| 2295 | printk(KERN_ERR "USB: monitor was not registered\n"); |
| 2296 | return; |
| 2297 | } |
| 2298 | mon_ops = NULL; |
| 2299 | mb(); |
| 2300 | } |
| 2301 | EXPORT_SYMBOL_GPL (usb_mon_deregister); |
| 2302 | |
Pete Zaitcev | f150fa1 | 2008-11-13 21:31:21 -0700 | [diff] [blame] | 2303 | #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */ |