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