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