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> |
| 33 | #include <asm/scatterlist.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/dma-mapping.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/irq.h> |
| 38 | #include <asm/byteorder.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 | |
| 84 | /* host controllers we manage */ |
| 85 | LIST_HEAD (usb_bus_list); |
| 86 | EXPORT_SYMBOL_GPL (usb_bus_list); |
| 87 | |
| 88 | /* used when allocating bus numbers */ |
| 89 | #define USB_MAXBUS 64 |
| 90 | struct usb_busmap { |
| 91 | unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))]; |
| 92 | }; |
| 93 | static struct usb_busmap busmap; |
| 94 | |
| 95 | /* used when updating list of hcds */ |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 96 | DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | EXPORT_SYMBOL_GPL (usb_bus_list_lock); |
| 98 | |
| 99 | /* used for controlling access to virtual root hubs */ |
| 100 | static DEFINE_SPINLOCK(hcd_root_hub_lock); |
| 101 | |
| 102 | /* used when updating hcd data */ |
| 103 | static DEFINE_SPINLOCK(hcd_data_lock); |
| 104 | |
| 105 | /* wait queue for synchronous unlinks */ |
| 106 | DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); |
| 107 | |
| 108 | /*-------------------------------------------------------------------------*/ |
| 109 | |
| 110 | /* |
| 111 | * Sharable chunks of root hub code. |
| 112 | */ |
| 113 | |
| 114 | /*-------------------------------------------------------------------------*/ |
| 115 | |
| 116 | #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff) |
| 117 | #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff) |
| 118 | |
| 119 | /* usb 2.0 root hub device descriptor */ |
| 120 | static const u8 usb2_rh_dev_descriptor [18] = { |
| 121 | 0x12, /* __u8 bLength; */ |
| 122 | 0x01, /* __u8 bDescriptorType; Device */ |
| 123 | 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ |
| 124 | |
| 125 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 126 | 0x00, /* __u8 bDeviceSubClass; */ |
| 127 | 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/ |
Alan Stern | 16f16d1 | 2005-10-24 15:41:19 -0400 | [diff] [blame] | 128 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | 0x00, 0x00, /* __le16 idVendor; */ |
| 131 | 0x00, 0x00, /* __le16 idProduct; */ |
| 132 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 133 | |
| 134 | 0x03, /* __u8 iManufacturer; */ |
| 135 | 0x02, /* __u8 iProduct; */ |
| 136 | 0x01, /* __u8 iSerialNumber; */ |
| 137 | 0x01 /* __u8 bNumConfigurations; */ |
| 138 | }; |
| 139 | |
| 140 | /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ |
| 141 | |
| 142 | /* usb 1.1 root hub device descriptor */ |
| 143 | static const u8 usb11_rh_dev_descriptor [18] = { |
| 144 | 0x12, /* __u8 bLength; */ |
| 145 | 0x01, /* __u8 bDescriptorType; Device */ |
| 146 | 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ |
| 147 | |
| 148 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 149 | 0x00, /* __u8 bDeviceSubClass; */ |
| 150 | 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ |
Alan Stern | 16f16d1 | 2005-10-24 15:41:19 -0400 | [diff] [blame] | 151 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | 0x00, 0x00, /* __le16 idVendor; */ |
| 154 | 0x00, 0x00, /* __le16 idProduct; */ |
| 155 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 156 | |
| 157 | 0x03, /* __u8 iManufacturer; */ |
| 158 | 0x02, /* __u8 iProduct; */ |
| 159 | 0x01, /* __u8 iSerialNumber; */ |
| 160 | 0x01 /* __u8 bNumConfigurations; */ |
| 161 | }; |
| 162 | |
| 163 | |
| 164 | /*-------------------------------------------------------------------------*/ |
| 165 | |
| 166 | /* Configuration descriptors for our root hubs */ |
| 167 | |
| 168 | static const u8 fs_rh_config_descriptor [] = { |
| 169 | |
| 170 | /* one configuration */ |
| 171 | 0x09, /* __u8 bLength; */ |
| 172 | 0x02, /* __u8 bDescriptorType; Configuration */ |
| 173 | 0x19, 0x00, /* __le16 wTotalLength; */ |
| 174 | 0x01, /* __u8 bNumInterfaces; (1) */ |
| 175 | 0x01, /* __u8 bConfigurationValue; */ |
| 176 | 0x00, /* __u8 iConfiguration; */ |
| 177 | 0xc0, /* __u8 bmAttributes; |
| 178 | Bit 7: must be set, |
| 179 | 6: Self-powered, |
| 180 | 5: Remote wakeup, |
| 181 | 4..0: resvd */ |
| 182 | 0x00, /* __u8 MaxPower; */ |
| 183 | |
| 184 | /* USB 1.1: |
| 185 | * USB 2.0, single TT organization (mandatory): |
| 186 | * one interface, protocol 0 |
| 187 | * |
| 188 | * USB 2.0, multiple TT organization (optional): |
| 189 | * two interfaces, protocols 1 (like single TT) |
| 190 | * and 2 (multiple TT mode) ... config is |
| 191 | * sometimes settable |
| 192 | * NOT IMPLEMENTED |
| 193 | */ |
| 194 | |
| 195 | /* one interface */ |
| 196 | 0x09, /* __u8 if_bLength; */ |
| 197 | 0x04, /* __u8 if_bDescriptorType; Interface */ |
| 198 | 0x00, /* __u8 if_bInterfaceNumber; */ |
| 199 | 0x00, /* __u8 if_bAlternateSetting; */ |
| 200 | 0x01, /* __u8 if_bNumEndpoints; */ |
| 201 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 202 | 0x00, /* __u8 if_bInterfaceSubClass; */ |
| 203 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 204 | 0x00, /* __u8 if_iInterface; */ |
| 205 | |
| 206 | /* one endpoint (status change endpoint) */ |
| 207 | 0x07, /* __u8 ep_bLength; */ |
| 208 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ |
| 209 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 210 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 211 | 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ |
| 212 | 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ |
| 213 | }; |
| 214 | |
| 215 | static const u8 hs_rh_config_descriptor [] = { |
| 216 | |
| 217 | /* one configuration */ |
| 218 | 0x09, /* __u8 bLength; */ |
| 219 | 0x02, /* __u8 bDescriptorType; Configuration */ |
| 220 | 0x19, 0x00, /* __le16 wTotalLength; */ |
| 221 | 0x01, /* __u8 bNumInterfaces; (1) */ |
| 222 | 0x01, /* __u8 bConfigurationValue; */ |
| 223 | 0x00, /* __u8 iConfiguration; */ |
| 224 | 0xc0, /* __u8 bmAttributes; |
| 225 | Bit 7: must be set, |
| 226 | 6: Self-powered, |
| 227 | 5: Remote wakeup, |
| 228 | 4..0: resvd */ |
| 229 | 0x00, /* __u8 MaxPower; */ |
| 230 | |
| 231 | /* USB 1.1: |
| 232 | * USB 2.0, single TT organization (mandatory): |
| 233 | * one interface, protocol 0 |
| 234 | * |
| 235 | * USB 2.0, multiple TT organization (optional): |
| 236 | * two interfaces, protocols 1 (like single TT) |
| 237 | * and 2 (multiple TT mode) ... config is |
| 238 | * sometimes settable |
| 239 | * NOT IMPLEMENTED |
| 240 | */ |
| 241 | |
| 242 | /* one interface */ |
| 243 | 0x09, /* __u8 if_bLength; */ |
| 244 | 0x04, /* __u8 if_bDescriptorType; Interface */ |
| 245 | 0x00, /* __u8 if_bInterfaceNumber; */ |
| 246 | 0x00, /* __u8 if_bAlternateSetting; */ |
| 247 | 0x01, /* __u8 if_bNumEndpoints; */ |
| 248 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 249 | 0x00, /* __u8 if_bInterfaceSubClass; */ |
| 250 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 251 | 0x00, /* __u8 if_iInterface; */ |
| 252 | |
| 253 | /* one endpoint (status change endpoint) */ |
| 254 | 0x07, /* __u8 ep_bLength; */ |
| 255 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ |
| 256 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 257 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
inaky@linux.intel.com | 88fafff | 2006-10-11 20:05:59 -0700 | [diff] [blame] | 258 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
| 259 | * see hub.c:hub_configure() for details. */ |
| 260 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
| 262 | }; |
| 263 | |
| 264 | /*-------------------------------------------------------------------------*/ |
| 265 | |
| 266 | /* |
| 267 | * helper routine for returning string descriptors in UTF-16LE |
| 268 | * input can actually be ISO-8859-1; ASCII is its 7-bit subset |
| 269 | */ |
| 270 | static int ascii2utf (char *s, u8 *utf, int utfmax) |
| 271 | { |
| 272 | int retval; |
| 273 | |
| 274 | for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) { |
| 275 | *utf++ = *s++; |
| 276 | *utf++ = 0; |
| 277 | } |
| 278 | if (utfmax > 0) { |
| 279 | *utf = *s; |
| 280 | ++retval; |
| 281 | } |
| 282 | return retval; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * rh_string - provides manufacturer, product and serial strings for root hub |
| 287 | * @id: the string ID number (1: serial number, 2: product, 3: vendor) |
| 288 | * @hcd: the host controller for this root hub |
| 289 | * @type: string describing our driver |
| 290 | * @data: return packet in UTF-16 LE |
| 291 | * @len: length of the return packet |
| 292 | * |
| 293 | * Produces either a manufacturer, product or serial number string for the |
| 294 | * virtual root hub device. |
| 295 | */ |
| 296 | static int rh_string ( |
| 297 | int id, |
| 298 | struct usb_hcd *hcd, |
| 299 | u8 *data, |
| 300 | int len |
| 301 | ) { |
| 302 | char buf [100]; |
| 303 | |
| 304 | // language ids |
| 305 | if (id == 0) { |
| 306 | buf[0] = 4; buf[1] = 3; /* 4 bytes string data */ |
| 307 | buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */ |
| 308 | len = min (len, 4); |
| 309 | memcpy (data, buf, len); |
| 310 | return len; |
| 311 | |
| 312 | // serial number |
| 313 | } else if (id == 1) { |
| 314 | strlcpy (buf, hcd->self.bus_name, sizeof buf); |
| 315 | |
| 316 | // product description |
| 317 | } else if (id == 2) { |
| 318 | strlcpy (buf, hcd->product_desc, sizeof buf); |
| 319 | |
| 320 | // id 3 == vendor description |
| 321 | } else if (id == 3) { |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 322 | snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, |
| 323 | init_utsname()->release, hcd->driver->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | // unsupported IDs --> "protocol stall" |
| 326 | } else |
| 327 | return -EPIPE; |
| 328 | |
| 329 | switch (len) { /* All cases fall through */ |
| 330 | default: |
| 331 | len = 2 + ascii2utf (buf, data + 2, len - 2); |
| 332 | case 2: |
| 333 | data [1] = 3; /* type == string */ |
| 334 | case 1: |
| 335 | data [0] = 2 * (strlen (buf) + 1); |
| 336 | case 0: |
| 337 | ; /* Compiler wants a statement here */ |
| 338 | } |
| 339 | return len; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* Root hub control transfers execute synchronously */ |
| 344 | static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) |
| 345 | { |
| 346 | struct usb_ctrlrequest *cmd; |
| 347 | u16 typeReq, wValue, wIndex, wLength; |
| 348 | u8 *ubuf = urb->transfer_buffer; |
Mikael Pettersson | 54bee6e | 2006-09-23 17:05:31 -0700 | [diff] [blame] | 349 | u8 tbuf [sizeof (struct usb_hub_descriptor)] |
| 350 | __attribute__((aligned(4))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | const u8 *bufp = tbuf; |
| 352 | int len = 0; |
| 353 | int patch_wakeup = 0; |
| 354 | unsigned long flags; |
| 355 | int status = 0; |
| 356 | int n; |
| 357 | |
| 358 | cmd = (struct usb_ctrlrequest *) urb->setup_packet; |
| 359 | typeReq = (cmd->bRequestType << 8) | cmd->bRequest; |
| 360 | wValue = le16_to_cpu (cmd->wValue); |
| 361 | wIndex = le16_to_cpu (cmd->wIndex); |
| 362 | wLength = le16_to_cpu (cmd->wLength); |
| 363 | |
| 364 | if (wLength > urb->transfer_buffer_length) |
| 365 | goto error; |
| 366 | |
| 367 | urb->actual_length = 0; |
| 368 | switch (typeReq) { |
| 369 | |
| 370 | /* DEVICE REQUESTS */ |
| 371 | |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 372 | /* The root hub's remote wakeup enable bit is implemented using |
| 373 | * driver model wakeup flags. If this system supports wakeup |
| 374 | * through USB, userspace may change the default "allow wakeup" |
| 375 | * policy through sysfs or these calls. |
| 376 | * |
| 377 | * Most root hubs support wakeup from downstream devices, for |
| 378 | * runtime power management (disabling USB clocks and reducing |
| 379 | * VBUS power usage). However, not all of them do so; silicon, |
| 380 | * board, and BIOS bugs here are not uncommon, so these can't |
| 381 | * be treated quite like external hubs. |
| 382 | * |
| 383 | * Likewise, not all root hubs will pass wakeup events upstream, |
| 384 | * to wake up the whole system. So don't assume root hub and |
| 385 | * controller capabilities are identical. |
| 386 | */ |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | case DeviceRequest | USB_REQ_GET_STATUS: |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 389 | tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev) |
| 390 | << USB_DEVICE_REMOTE_WAKEUP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | | (1 << USB_DEVICE_SELF_POWERED); |
| 392 | tbuf [1] = 0; |
| 393 | len = 2; |
| 394 | break; |
| 395 | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: |
| 396 | if (wValue == USB_DEVICE_REMOTE_WAKEUP) |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 397 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | else |
| 399 | goto error; |
| 400 | break; |
| 401 | case DeviceOutRequest | USB_REQ_SET_FEATURE: |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 402 | if (device_can_wakeup(&hcd->self.root_hub->dev) |
| 403 | && wValue == USB_DEVICE_REMOTE_WAKEUP) |
| 404 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | else |
| 406 | goto error; |
| 407 | break; |
| 408 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
| 409 | tbuf [0] = 1; |
| 410 | len = 1; |
| 411 | /* FALLTHROUGH */ |
| 412 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 413 | break; |
| 414 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 415 | switch (wValue & 0xff00) { |
| 416 | case USB_DT_DEVICE << 8: |
| 417 | if (hcd->driver->flags & HCD_USB2) |
| 418 | bufp = usb2_rh_dev_descriptor; |
| 419 | else if (hcd->driver->flags & HCD_USB11) |
| 420 | bufp = usb11_rh_dev_descriptor; |
| 421 | else |
| 422 | goto error; |
| 423 | len = 18; |
| 424 | break; |
| 425 | case USB_DT_CONFIG << 8: |
| 426 | if (hcd->driver->flags & HCD_USB2) { |
| 427 | bufp = hs_rh_config_descriptor; |
| 428 | len = sizeof hs_rh_config_descriptor; |
| 429 | } else { |
| 430 | bufp = fs_rh_config_descriptor; |
| 431 | len = sizeof fs_rh_config_descriptor; |
| 432 | } |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 433 | if (device_can_wakeup(&hcd->self.root_hub->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | patch_wakeup = 1; |
| 435 | break; |
| 436 | case USB_DT_STRING << 8: |
| 437 | n = rh_string (wValue & 0xff, hcd, ubuf, wLength); |
| 438 | if (n < 0) |
| 439 | goto error; |
| 440 | urb->actual_length = n; |
| 441 | break; |
| 442 | default: |
| 443 | goto error; |
| 444 | } |
| 445 | break; |
| 446 | case DeviceRequest | USB_REQ_GET_INTERFACE: |
| 447 | tbuf [0] = 0; |
| 448 | len = 1; |
| 449 | /* FALLTHROUGH */ |
| 450 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: |
| 451 | break; |
| 452 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
| 453 | // wValue == urb->dev->devaddr |
| 454 | dev_dbg (hcd->self.controller, "root hub device address %d\n", |
| 455 | wValue); |
| 456 | break; |
| 457 | |
| 458 | /* INTERFACE REQUESTS (no defined feature/status flags) */ |
| 459 | |
| 460 | /* ENDPOINT REQUESTS */ |
| 461 | |
| 462 | case EndpointRequest | USB_REQ_GET_STATUS: |
| 463 | // ENDPOINT_HALT flag |
| 464 | tbuf [0] = 0; |
| 465 | tbuf [1] = 0; |
| 466 | len = 2; |
| 467 | /* FALLTHROUGH */ |
| 468 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
| 469 | case EndpointOutRequest | USB_REQ_SET_FEATURE: |
| 470 | dev_dbg (hcd->self.controller, "no endpoint features yet\n"); |
| 471 | break; |
| 472 | |
| 473 | /* CLASS REQUESTS (and errors) */ |
| 474 | |
| 475 | default: |
| 476 | /* non-generic request */ |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 477 | switch (typeReq) { |
| 478 | case GetHubStatus: |
| 479 | case GetPortStatus: |
| 480 | len = 4; |
| 481 | break; |
| 482 | case GetHubDescriptor: |
| 483 | len = sizeof (struct usb_hub_descriptor); |
| 484 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 486 | status = hcd->driver->hub_control (hcd, |
| 487 | typeReq, wValue, wIndex, |
| 488 | tbuf, wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | break; |
| 490 | error: |
| 491 | /* "protocol stall" on error */ |
| 492 | status = -EPIPE; |
| 493 | } |
| 494 | |
| 495 | if (status) { |
| 496 | len = 0; |
| 497 | if (status != -EPIPE) { |
| 498 | dev_dbg (hcd->self.controller, |
| 499 | "CTRL: TypeReq=0x%x val=0x%x " |
| 500 | "idx=0x%x len=%d ==> %d\n", |
| 501 | typeReq, wValue, wIndex, |
David Brownell | b13296c | 2005-09-27 10:38:54 -0700 | [diff] [blame] | 502 | wLength, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | if (len) { |
| 506 | if (urb->transfer_buffer_length < len) |
| 507 | len = urb->transfer_buffer_length; |
| 508 | urb->actual_length = len; |
| 509 | // always USB_DIR_IN, toward host |
| 510 | memcpy (ubuf, bufp, len); |
| 511 | |
| 512 | /* report whether RH hardware supports remote wakeup */ |
| 513 | if (patch_wakeup && |
| 514 | len > offsetof (struct usb_config_descriptor, |
| 515 | bmAttributes)) |
| 516 | ((struct usb_config_descriptor *)ubuf)->bmAttributes |
| 517 | |= USB_CONFIG_ATT_WAKEUP; |
| 518 | } |
| 519 | |
| 520 | /* any errors get returned through the urb completion */ |
| 521 | local_irq_save (flags); |
| 522 | spin_lock (&urb->lock); |
| 523 | if (urb->status == -EINPROGRESS) |
| 524 | urb->status = status; |
| 525 | spin_unlock (&urb->lock); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 526 | usb_hcd_giveback_urb (hcd, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | local_irq_restore (flags); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /*-------------------------------------------------------------------------*/ |
| 532 | |
| 533 | /* |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 534 | * Root Hub interrupt transfers are polled using a timer if the |
| 535 | * driver requests it; otherwise the driver is responsible for |
| 536 | * calling usb_hcd_poll_rh_status() when an event occurs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | * |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 538 | * Completions are called in_interrupt(), but they may or may not |
| 539 | * be in_irq(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 541 | void usb_hcd_poll_rh_status(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | struct urb *urb; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 544 | int length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | unsigned long flags; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 546 | char buffer[4]; /* Any root hubs with > 31 ports? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Alan Stern | 1b42ae6 | 2007-03-13 11:10:52 -0400 | [diff] [blame] | 548 | if (unlikely(!hcd->rh_registered)) |
| 549 | return; |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 550 | if (!hcd->uses_new_polling && !hcd->status_urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 553 | length = hcd->driver->hub_status_data(hcd, buffer); |
| 554 | if (length > 0) { |
| 555 | |
| 556 | /* try to complete the status urb */ |
| 557 | local_irq_save (flags); |
| 558 | spin_lock(&hcd_root_hub_lock); |
| 559 | urb = hcd->status_urb; |
| 560 | if (urb) { |
| 561 | spin_lock(&urb->lock); |
| 562 | if (urb->status == -EINPROGRESS) { |
| 563 | hcd->poll_pending = 0; |
| 564 | hcd->status_urb = NULL; |
| 565 | urb->status = 0; |
| 566 | urb->hcpriv = NULL; |
| 567 | urb->actual_length = length; |
| 568 | memcpy(urb->transfer_buffer, buffer, length); |
| 569 | } else /* urb has been unlinked */ |
| 570 | length = 0; |
| 571 | spin_unlock(&urb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } else |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 573 | length = 0; |
| 574 | spin_unlock(&hcd_root_hub_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 576 | /* local irqs are always blocked in completions */ |
| 577 | if (length > 0) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 578 | usb_hcd_giveback_urb (hcd, urb); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 579 | else |
| 580 | hcd->poll_pending = 1; |
| 581 | local_irq_restore (flags); |
| 582 | } |
| 583 | |
| 584 | /* 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] | 585 | * exceed that limit if HZ is 100. The math is more clunky than |
| 586 | * maybe expected, this is to make sure that all timers for USB devices |
| 587 | * fire at the same time to give the CPU a break inbetween */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 588 | if (hcd->uses_new_polling ? hcd->poll_rh : |
| 589 | (length == 0 && hcd->status_urb != NULL)) |
Arjan van de Ven | 01cd081 | 2007-05-22 12:42:56 -0700 | [diff] [blame] | 590 | mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 591 | } |
| 592 | EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); |
| 593 | |
| 594 | /* timer callback */ |
| 595 | static void rh_timer_func (unsigned long _hcd) |
| 596 | { |
| 597 | usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /*-------------------------------------------------------------------------*/ |
| 601 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 602 | static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) |
| 603 | { |
| 604 | int retval; |
| 605 | unsigned long flags; |
| 606 | int len = 1 + (urb->dev->maxchild / 8); |
| 607 | |
| 608 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 609 | if (urb->status != -EINPROGRESS) /* already unlinked */ |
| 610 | retval = urb->status; |
| 611 | else if (hcd->status_urb || urb->transfer_buffer_length < len) { |
| 612 | dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); |
| 613 | retval = -EINVAL; |
| 614 | } else { |
| 615 | hcd->status_urb = urb; |
| 616 | urb->hcpriv = hcd; /* indicate it's queued */ |
| 617 | |
| 618 | if (!hcd->uses_new_polling) |
Arjan van de Ven | 01cd081 | 2007-05-22 12:42:56 -0700 | [diff] [blame] | 619 | mod_timer (&hcd->rh_timer, |
| 620 | (jiffies/(HZ/4) + 1) * (HZ/4)); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 621 | |
| 622 | /* If a status change has already occurred, report it ASAP */ |
| 623 | else if (hcd->poll_pending) |
| 624 | mod_timer (&hcd->rh_timer, jiffies); |
| 625 | retval = 0; |
| 626 | } |
| 627 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 628 | return retval; |
| 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) |
| 632 | { |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 633 | if (usb_pipeint (urb->pipe)) |
| 634 | return rh_queue_status (hcd, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (usb_pipecontrol (urb->pipe)) |
| 636 | return rh_call_control (hcd, urb); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 637 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /*-------------------------------------------------------------------------*/ |
| 641 | |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 642 | /* Unlinks of root-hub control URBs are legal, but they don't do anything |
| 643 | * since these URBs always execute synchronously. |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 644 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb) |
| 646 | { |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 647 | unsigned long flags; |
| 648 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 649 | if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */ |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 650 | ; /* Do nothing */ |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 651 | |
| 652 | } else { /* Status URB */ |
| 653 | if (!hcd->uses_new_polling) |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 654 | del_timer (&hcd->rh_timer); |
| 655 | local_irq_save (flags); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 656 | spin_lock (&hcd_root_hub_lock); |
| 657 | if (urb == hcd->status_urb) { |
| 658 | hcd->status_urb = NULL; |
| 659 | urb->hcpriv = NULL; |
| 660 | } else |
| 661 | urb = NULL; /* wasn't fully queued */ |
| 662 | spin_unlock (&hcd_root_hub_lock); |
| 663 | if (urb) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 664 | usb_hcd_giveback_urb (hcd, urb); |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 665 | local_irq_restore (flags); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | /*-------------------------------------------------------------------------*/ |
| 672 | |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 673 | static struct class *usb_host_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | int usb_host_init(void) |
| 676 | { |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 677 | int retval = 0; |
| 678 | |
| 679 | usb_host_class = class_create(THIS_MODULE, "usb_host"); |
| 680 | if (IS_ERR(usb_host_class)) |
| 681 | retval = PTR_ERR(usb_host_class); |
| 682 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | void usb_host_cleanup(void) |
| 686 | { |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 687 | class_destroy(usb_host_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /** |
| 691 | * usb_bus_init - shared initialization code |
| 692 | * @bus: the bus structure being initialized |
| 693 | * |
| 694 | * This code is used to initialize a usb_bus structure, memory for which is |
| 695 | * separately managed. |
| 696 | */ |
| 697 | static void usb_bus_init (struct usb_bus *bus) |
| 698 | { |
| 699 | memset (&bus->devmap, 0, sizeof(struct usb_devmap)); |
| 700 | |
| 701 | bus->devnum_next = 1; |
| 702 | |
| 703 | bus->root_hub = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | bus->busnum = -1; |
| 705 | bus->bandwidth_allocated = 0; |
| 706 | bus->bandwidth_int_reqs = 0; |
| 707 | bus->bandwidth_isoc_reqs = 0; |
| 708 | |
| 709 | INIT_LIST_HEAD (&bus->bus_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /*-------------------------------------------------------------------------*/ |
| 713 | |
| 714 | /** |
| 715 | * usb_register_bus - registers the USB host controller with the usb core |
| 716 | * @bus: pointer to the bus to register |
| 717 | * Context: !in_interrupt() |
| 718 | * |
| 719 | * Assigns a bus number, and links the controller into usbcore data |
| 720 | * structures so that it can be seen by scanning the bus list. |
| 721 | */ |
| 722 | static int usb_register_bus(struct usb_bus *bus) |
| 723 | { |
| 724 | int busnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 726 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); |
| 728 | if (busnum < USB_MAXBUS) { |
| 729 | set_bit (busnum, busmap.busmap); |
| 730 | bus->busnum = busnum; |
| 731 | } else { |
| 732 | printk (KERN_ERR "%s: too many buses\n", usbcore_name); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 733 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | return -E2BIG; |
| 735 | } |
| 736 | |
Greg Kroah-Hartman | 53f4654 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 737 | bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0), |
| 738 | bus->controller, "usb_host%d", busnum); |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 739 | if (IS_ERR(bus->class_dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | clear_bit(busnum, busmap.busmap); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 741 | mutex_unlock(&usb_bus_list_lock); |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 742 | return PTR_ERR(bus->class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 745 | class_set_devdata(bus->class_dev, bus); |
| 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | /* Add it to the local list of buses */ |
| 748 | list_add (&bus->bus_list, &usb_bus_list); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 749 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 751 | usb_notify_add_bus(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * usb_deregister_bus - deregisters the USB host controller |
| 759 | * @bus: pointer to the bus to deregister |
| 760 | * Context: !in_interrupt() |
| 761 | * |
| 762 | * Recycles the bus number, and unlinks the controller from usbcore data |
| 763 | * structures so that it won't be seen by scanning the bus list. |
| 764 | */ |
| 765 | static void usb_deregister_bus (struct usb_bus *bus) |
| 766 | { |
| 767 | dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); |
| 768 | |
| 769 | /* |
| 770 | * NOTE: make sure that all the devices are removed by the |
| 771 | * controller code, as well as having it call this when cleaning |
| 772 | * itself up |
| 773 | */ |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 774 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | list_del (&bus->bus_list); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 776 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 778 | usb_notify_remove_bus(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | clear_bit (bus->busnum, busmap.busmap); |
| 781 | |
gregkh@suse.de | 8561b10 | 2005-03-15 15:10:13 -0800 | [diff] [blame] | 782 | class_device_unregister(bus->class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /** |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 786 | * 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] | 787 | * @hcd: host controller for this root hub |
| 788 | * |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 789 | * 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] | 790 | * the device properly in the device tree and then calls usb_new_device() |
| 791 | * to register the usb device. It also assigns the root hub's USB address |
| 792 | * (always 1). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | */ |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 794 | static int register_root_hub(struct usb_hcd *hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
| 796 | struct device *parent_dev = hcd->self.controller; |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 797 | struct usb_device *usb_dev = hcd->self.root_hub; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | const int devnum = 1; |
| 799 | int retval; |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | usb_dev->devnum = devnum; |
| 802 | usb_dev->bus->devnum_next = devnum + 1; |
| 803 | memset (&usb_dev->bus->devmap.devicemap, 0, |
| 804 | sizeof usb_dev->bus->devmap.devicemap); |
| 805 | set_bit (devnum, usb_dev->bus->devmap.devicemap); |
| 806 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); |
| 807 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 808 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); |
| 811 | retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); |
| 812 | if (retval != sizeof usb_dev->descriptor) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 813 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
| 815 | usb_dev->dev.bus_id, retval); |
| 816 | return (retval < 0) ? retval : -EMSGSIZE; |
| 817 | } |
| 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | retval = usb_new_device (usb_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | dev_err (parent_dev, "can't register root hub for %s, %d\n", |
| 822 | usb_dev->dev.bus_id, retval); |
| 823 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 824 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | if (retval == 0) { |
| 827 | spin_lock_irq (&hcd_root_hub_lock); |
| 828 | hcd->rh_registered = 1; |
| 829 | spin_unlock_irq (&hcd_root_hub_lock); |
| 830 | |
| 831 | /* Did the HC die before the root hub was registered? */ |
| 832 | if (hcd->state == HC_STATE_HALT) |
| 833 | usb_hc_died (hcd); /* This time clean up */ |
| 834 | } |
| 835 | |
| 836 | return retval; |
| 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 839 | void usb_enable_root_hub_irq (struct usb_bus *bus) |
| 840 | { |
| 841 | struct usb_hcd *hcd; |
| 842 | |
| 843 | hcd = container_of (bus, struct usb_hcd, self); |
Alan Stern | d19ac7d | 2006-09-25 15:41:12 -0400 | [diff] [blame] | 844 | if (hcd->driver->hub_irq_enable && hcd->state != HC_STATE_HALT) |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 845 | hcd->driver->hub_irq_enable (hcd); |
| 846 | } |
| 847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | /*-------------------------------------------------------------------------*/ |
| 850 | |
| 851 | /** |
| 852 | * usb_calc_bus_time - approximate periodic transaction time in nanoseconds |
| 853 | * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} |
| 854 | * @is_input: true iff the transaction sends data to the host |
| 855 | * @isoc: true for isochronous transactions, false for interrupt ones |
| 856 | * @bytecount: how many bytes in the transaction. |
| 857 | * |
| 858 | * Returns approximate bus time in nanoseconds for a periodic transaction. |
| 859 | * See USB 2.0 spec section 5.11.3; only periodic transfers need to be |
| 860 | * scheduled in software, this function is only used for such scheduling. |
| 861 | */ |
| 862 | long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) |
| 863 | { |
| 864 | unsigned long tmp; |
| 865 | |
| 866 | switch (speed) { |
| 867 | case USB_SPEED_LOW: /* INTR only */ |
| 868 | if (is_input) { |
| 869 | tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 870 | return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); |
| 871 | } else { |
| 872 | tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 873 | return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); |
| 874 | } |
| 875 | case USB_SPEED_FULL: /* ISOC or INTR */ |
| 876 | if (isoc) { |
| 877 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 878 | return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp); |
| 879 | } else { |
| 880 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 881 | return (9107L + BW_HOST_DELAY + tmp); |
| 882 | } |
| 883 | case USB_SPEED_HIGH: /* ISOC or INTR */ |
| 884 | // FIXME adjust for input vs output |
| 885 | if (isoc) |
Dan Streetman | 498f78e | 2005-07-29 12:18:28 -0700 | [diff] [blame] | 886 | tmp = HS_NSECS_ISO (bytecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | else |
Dan Streetman | 498f78e | 2005-07-29 12:18:28 -0700 | [diff] [blame] | 888 | tmp = HS_NSECS (bytecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return tmp; |
| 890 | default: |
| 891 | pr_debug ("%s: bogus device speed!\n", usbcore_name); |
| 892 | return -1; |
| 893 | } |
| 894 | } |
| 895 | EXPORT_SYMBOL (usb_calc_bus_time); |
| 896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
| 898 | /*-------------------------------------------------------------------------*/ |
| 899 | |
| 900 | /* |
| 901 | * Generic HC operations. |
| 902 | */ |
| 903 | |
| 904 | /*-------------------------------------------------------------------------*/ |
| 905 | |
| 906 | static void urb_unlink (struct urb *urb) |
| 907 | { |
| 908 | unsigned long flags; |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* clear all state linking urb to this dev (and hcd) */ |
| 911 | |
| 912 | spin_lock_irqsave (&hcd_data_lock, flags); |
| 913 | list_del_init (&urb->urb_list); |
| 914 | spin_unlock_irqrestore (&hcd_data_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | |
| 918 | /* may be called in any context with a valid urb->dev usecount |
| 919 | * caller surrenders "ownership" of urb |
| 920 | * expects usb_submit_urb() to have sanity checked and conditioned all |
| 921 | * inputs in the urb |
| 922 | */ |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 923 | int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
| 925 | int status; |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 926 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | struct usb_host_endpoint *ep; |
| 928 | unsigned long flags; |
| 929 | |
| 930 | if (!hcd) |
| 931 | return -ENODEV; |
| 932 | |
| 933 | usbmon_urb_submit(&hcd->self, urb); |
| 934 | |
| 935 | /* |
| 936 | * Atomically queue the urb, first to our records, then to the HCD. |
| 937 | * Access to urb->status is controlled by urb->lock ... changes on |
| 938 | * i/o completion (normal or fault) or unlinking. |
| 939 | */ |
| 940 | |
| 941 | // FIXME: verify that quiescing hc works right (RH cleans up) |
| 942 | |
| 943 | spin_lock_irqsave (&hcd_data_lock, flags); |
| 944 | ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out) |
| 945 | [usb_pipeendpoint(urb->pipe)]; |
| 946 | if (unlikely (!ep)) |
| 947 | status = -ENOENT; |
| 948 | else if (unlikely (urb->reject)) |
| 949 | status = -EPERM; |
| 950 | else switch (hcd->state) { |
| 951 | case HC_STATE_RUNNING: |
| 952 | case HC_STATE_RESUMING: |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 953 | doit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | list_add_tail (&urb->urb_list, &ep->urb_list); |
| 955 | status = 0; |
| 956 | break; |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 957 | case HC_STATE_SUSPENDED: |
| 958 | /* HC upstream links (register access, wakeup signaling) can work |
| 959 | * even when the downstream links (and DMA etc) are quiesced; let |
| 960 | * usbcore talk to the root hub. |
| 961 | */ |
| 962 | if (hcd->self.controller->power.power_state.event == PM_EVENT_ON |
| 963 | && urb->dev->parent == NULL) |
| 964 | goto doit; |
| 965 | /* FALL THROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | default: |
| 967 | status = -ESHUTDOWN; |
| 968 | break; |
| 969 | } |
| 970 | spin_unlock_irqrestore (&hcd_data_lock, flags); |
| 971 | if (status) { |
| 972 | INIT_LIST_HEAD (&urb->urb_list); |
| 973 | usbmon_urb_submit_error(&hcd->self, urb, status); |
| 974 | return status; |
| 975 | } |
| 976 | |
| 977 | /* increment urb's reference count as part of giving it to the HCD |
| 978 | * (which now controls it). HCD guarantees that it either returns |
| 979 | * an error or calls giveback(), but not both. |
| 980 | */ |
| 981 | urb = usb_get_urb (urb); |
| 982 | atomic_inc (&urb->use_count); |
| 983 | |
| 984 | if (urb->dev == hcd->self.root_hub) { |
| 985 | /* NOTE: requirement on hub callers (usbfs and the hub |
| 986 | * driver, for now) that URBs' urb->transfer_buffer be |
| 987 | * valid and usb_buffer_{sync,unmap}() not be needed, since |
| 988 | * they could clobber root hub response data. |
| 989 | */ |
| 990 | status = rh_urb_enqueue (hcd, urb); |
| 991 | goto done; |
| 992 | } |
| 993 | |
| 994 | /* lower level hcd code should use *_dma exclusively, |
| 995 | * unless it uses pio or talks to another transport. |
| 996 | */ |
Alan Stern | dd990f1 | 2006-08-30 11:29:56 -0400 | [diff] [blame] | 997 | if (hcd->self.uses_dma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | if (usb_pipecontrol (urb->pipe) |
| 999 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) |
| 1000 | urb->setup_dma = dma_map_single ( |
| 1001 | hcd->self.controller, |
| 1002 | urb->setup_packet, |
| 1003 | sizeof (struct usb_ctrlrequest), |
| 1004 | DMA_TO_DEVICE); |
| 1005 | if (urb->transfer_buffer_length != 0 |
| 1006 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) |
| 1007 | urb->transfer_dma = dma_map_single ( |
| 1008 | hcd->self.controller, |
| 1009 | urb->transfer_buffer, |
| 1010 | urb->transfer_buffer_length, |
| 1011 | usb_pipein (urb->pipe) |
| 1012 | ? DMA_FROM_DEVICE |
| 1013 | : DMA_TO_DEVICE); |
| 1014 | } |
| 1015 | |
| 1016 | status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags); |
| 1017 | done: |
| 1018 | if (unlikely (status)) { |
| 1019 | urb_unlink (urb); |
| 1020 | atomic_dec (&urb->use_count); |
| 1021 | if (urb->reject) |
| 1022 | wake_up (&usb_kill_urb_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | usbmon_urb_submit_error(&hcd->self, urb, status); |
Pete Zaitcev | d984abc | 2007-05-11 22:00:29 -0700 | [diff] [blame] | 1024 | usb_put_urb (urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | return status; |
| 1027 | } |
| 1028 | |
| 1029 | /*-------------------------------------------------------------------------*/ |
| 1030 | |
| 1031 | /* called in any context */ |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 1032 | int usb_hcd_get_frame_number (struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1034 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (!HC_IS_RUNNING (hcd->state)) |
| 1037 | return -ESHUTDOWN; |
| 1038 | return hcd->driver->get_frame_number (hcd); |
| 1039 | } |
| 1040 | |
| 1041 | /*-------------------------------------------------------------------------*/ |
| 1042 | |
| 1043 | /* this makes the hcd giveback() the urb more quickly, by kicking it |
| 1044 | * off hardware queues (which may take a while) and returning it as |
| 1045 | * soon as practical. we've already set up the urb's return status, |
| 1046 | * but we can't know if the callback completed already. |
| 1047 | */ |
| 1048 | static int |
| 1049 | unlink1 (struct usb_hcd *hcd, struct urb *urb) |
| 1050 | { |
| 1051 | int value; |
| 1052 | |
| 1053 | if (urb->dev == hcd->self.root_hub) |
| 1054 | value = usb_rh_urb_dequeue (hcd, urb); |
| 1055 | else { |
| 1056 | |
| 1057 | /* The only reason an HCD might fail this call is if |
| 1058 | * it has not yet fully queued the urb to begin with. |
| 1059 | * Such failures should be harmless. */ |
| 1060 | value = hcd->driver->urb_dequeue (hcd, urb); |
| 1061 | } |
| 1062 | |
| 1063 | if (value != 0) |
| 1064 | dev_dbg (hcd->self.controller, "dequeue %p --> %d\n", |
| 1065 | urb, value); |
| 1066 | return value; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * called in any context |
| 1071 | * |
| 1072 | * caller guarantees urb won't be recycled till both unlink() |
| 1073 | * and the urb's completion function return |
| 1074 | */ |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 1075 | int usb_hcd_unlink_urb (struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | { |
| 1077 | struct usb_host_endpoint *ep; |
| 1078 | struct usb_hcd *hcd = NULL; |
| 1079 | struct device *sys = NULL; |
| 1080 | unsigned long flags; |
| 1081 | struct list_head *tmp; |
| 1082 | int retval; |
| 1083 | |
| 1084 | if (!urb) |
| 1085 | return -EINVAL; |
| 1086 | if (!urb->dev || !urb->dev->bus) |
| 1087 | return -ENODEV; |
| 1088 | ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out) |
| 1089 | [usb_pipeendpoint(urb->pipe)]; |
| 1090 | if (!ep) |
| 1091 | return -ENODEV; |
| 1092 | |
| 1093 | /* |
| 1094 | * we contend for urb->status with the hcd core, |
| 1095 | * which changes it while returning the urb. |
| 1096 | * |
| 1097 | * Caller guaranteed that the urb pointer hasn't been freed, and |
| 1098 | * that it was submitted. But as a rule it can't know whether or |
| 1099 | * not it's already been unlinked ... so we respect the reversed |
| 1100 | * lock sequence needed for the usb_hcd_giveback_urb() code paths |
| 1101 | * (urb lock, then hcd_data_lock) in case some other CPU is now |
| 1102 | * unlinking it. |
| 1103 | */ |
| 1104 | spin_lock_irqsave (&urb->lock, flags); |
| 1105 | spin_lock (&hcd_data_lock); |
| 1106 | |
| 1107 | sys = &urb->dev->dev; |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1108 | hcd = bus_to_hcd(urb->dev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | if (hcd == NULL) { |
| 1110 | retval = -ENODEV; |
| 1111 | goto done; |
| 1112 | } |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | /* insist the urb is still queued */ |
| 1115 | list_for_each(tmp, &ep->urb_list) { |
| 1116 | if (tmp == &urb->urb_list) |
| 1117 | break; |
| 1118 | } |
| 1119 | if (tmp != &urb->urb_list) { |
| 1120 | retval = -EIDRM; |
| 1121 | goto done; |
| 1122 | } |
| 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 | */ |
| 1127 | if (urb->status != -EINPROGRESS) { |
| 1128 | retval = -EBUSY; |
| 1129 | goto done; |
| 1130 | } |
| 1131 | |
| 1132 | /* IRQ setup can easily be broken so that USB controllers |
| 1133 | * never get completion IRQs ... maybe even the ones we need to |
| 1134 | * finish unlinking the initial failed usb_set_address() |
| 1135 | * or device descriptor fetch. |
| 1136 | */ |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1137 | if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) |
| 1138 | && hcd->self.root_hub != urb->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | dev_warn (hcd->self.controller, "Unlink after no-IRQ? " |
| 1140 | "Controller is probably using the wrong IRQ." |
| 1141 | "\n"); |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1142 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | urb->status = status; |
| 1146 | |
| 1147 | spin_unlock (&hcd_data_lock); |
| 1148 | spin_unlock_irqrestore (&urb->lock, flags); |
| 1149 | |
| 1150 | retval = unlink1 (hcd, urb); |
| 1151 | if (retval == 0) |
| 1152 | retval = -EINPROGRESS; |
| 1153 | return retval; |
| 1154 | |
| 1155 | done: |
| 1156 | spin_unlock (&hcd_data_lock); |
| 1157 | spin_unlock_irqrestore (&urb->lock, flags); |
| 1158 | if (retval != -EIDRM && sys && sys->driver) |
| 1159 | dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval); |
| 1160 | return retval; |
| 1161 | } |
| 1162 | |
| 1163 | /*-------------------------------------------------------------------------*/ |
| 1164 | |
| 1165 | /* disables the endpoint: cancels any pending urbs, then synchronizes with |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1166 | * the hcd to make sure all endpoint state is gone from hardware, and then |
| 1167 | * waits until the endpoint's queue is completely drained. use for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | * set_configuration, set_interface, driver removal, physical disconnect. |
| 1169 | * |
| 1170 | * example: a qh stored in ep->hcpriv, holding state related to endpoint |
| 1171 | * type, maxpacket size, toggle, halt status, and scheduling. |
| 1172 | */ |
Alan Stern | a6d2bb9 | 2006-08-30 11:27:36 -0400 | [diff] [blame] | 1173 | void usb_hcd_endpoint_disable (struct usb_device *udev, |
| 1174 | struct usb_host_endpoint *ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | { |
| 1176 | struct usb_hcd *hcd; |
| 1177 | struct urb *urb; |
| 1178 | |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1179 | hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | local_irq_disable (); |
| 1181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* ep is already gone from udev->ep_{in,out}[]; no more submits */ |
| 1183 | rescan: |
| 1184 | spin_lock (&hcd_data_lock); |
| 1185 | list_for_each_entry (urb, &ep->urb_list, urb_list) { |
| 1186 | int tmp; |
| 1187 | |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1188 | /* the urb may already have been unlinked */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | if (urb->status != -EINPROGRESS) |
| 1190 | continue; |
| 1191 | usb_get_urb (urb); |
| 1192 | spin_unlock (&hcd_data_lock); |
| 1193 | |
| 1194 | spin_lock (&urb->lock); |
| 1195 | tmp = urb->status; |
| 1196 | if (tmp == -EINPROGRESS) |
| 1197 | urb->status = -ESHUTDOWN; |
| 1198 | spin_unlock (&urb->lock); |
| 1199 | |
| 1200 | /* kick hcd unless it's already returning this */ |
| 1201 | if (tmp == -EINPROGRESS) { |
| 1202 | tmp = urb->pipe; |
| 1203 | unlink1 (hcd, urb); |
| 1204 | dev_dbg (hcd->self.controller, |
| 1205 | "shutdown urb %p pipe %08x ep%d%s%s\n", |
| 1206 | urb, tmp, usb_pipeendpoint (tmp), |
| 1207 | (tmp & USB_DIR_IN) ? "in" : "out", |
| 1208 | ({ char *s; \ |
| 1209 | switch (usb_pipetype (tmp)) { \ |
| 1210 | case PIPE_CONTROL: s = ""; break; \ |
| 1211 | case PIPE_BULK: s = "-bulk"; break; \ |
| 1212 | case PIPE_INTERRUPT: s = "-intr"; break; \ |
| 1213 | default: s = "-iso"; break; \ |
| 1214 | }; s;})); |
| 1215 | } |
| 1216 | usb_put_urb (urb); |
| 1217 | |
| 1218 | /* list contents may have changed */ |
| 1219 | goto rescan; |
| 1220 | } |
| 1221 | spin_unlock (&hcd_data_lock); |
| 1222 | local_irq_enable (); |
| 1223 | |
| 1224 | /* synchronize with the hardware, so old configuration state |
| 1225 | * clears out immediately (and will be freed). |
| 1226 | */ |
| 1227 | might_sleep (); |
| 1228 | if (hcd->driver->endpoint_disable) |
| 1229 | hcd->driver->endpoint_disable (hcd, ep); |
Alan Stern | 455b25f | 2006-08-11 16:01:45 -0400 | [diff] [blame] | 1230 | |
| 1231 | /* Wait until the endpoint queue is completely empty. Most HCDs |
| 1232 | * will have done this already in their endpoint_disable method, |
| 1233 | * but some might not. And there could be root-hub control URBs |
| 1234 | * still pending since they aren't affected by the HCDs' |
| 1235 | * endpoint_disable methods. |
| 1236 | */ |
| 1237 | while (!list_empty (&ep->urb_list)) { |
| 1238 | spin_lock_irq (&hcd_data_lock); |
| 1239 | |
| 1240 | /* The list may have changed while we acquired the spinlock */ |
| 1241 | urb = NULL; |
| 1242 | if (!list_empty (&ep->urb_list)) { |
| 1243 | urb = list_entry (ep->urb_list.prev, struct urb, |
| 1244 | urb_list); |
| 1245 | usb_get_urb (urb); |
| 1246 | } |
| 1247 | spin_unlock_irq (&hcd_data_lock); |
| 1248 | |
| 1249 | if (urb) { |
| 1250 | usb_kill_urb (urb); |
| 1251 | usb_put_urb (urb); |
| 1252 | } |
| 1253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /*-------------------------------------------------------------------------*/ |
| 1257 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1258 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1260 | int hcd_bus_suspend(struct usb_device *rhdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1262 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
| 1263 | int status; |
| 1264 | int old_state = hcd->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1266 | dev_dbg(&rhdev->dev, "bus %s%s\n", |
| 1267 | rhdev->auto_pm ? "auto-" : "", "suspend"); |
| 1268 | if (!hcd->driver->bus_suspend) { |
| 1269 | status = -ENOENT; |
| 1270 | } else { |
| 1271 | hcd->state = HC_STATE_QUIESCING; |
| 1272 | status = hcd->driver->bus_suspend(hcd); |
| 1273 | } |
| 1274 | if (status == 0) { |
| 1275 | usb_set_device_state(rhdev, USB_STATE_SUSPENDED); |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1276 | hcd->state = HC_STATE_SUSPENDED; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1277 | } else { |
| 1278 | hcd->state = old_state; |
| 1279 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1280 | "suspend", status); |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1281 | } |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1282 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1285 | int hcd_bus_resume(struct usb_device *rhdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1287 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
| 1288 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1290 | dev_dbg(&rhdev->dev, "usb %s%s\n", |
| 1291 | rhdev->auto_pm ? "auto-" : "", "resume"); |
Alan Stern | 0c0382e | 2005-10-13 17:08:02 -0400 | [diff] [blame] | 1292 | if (!hcd->driver->bus_resume) |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1293 | return -ENOENT; |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1294 | if (hcd->state == HC_STATE_RUNNING) |
| 1295 | return 0; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1296 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1297 | hcd->state = HC_STATE_RESUMING; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1298 | status = hcd->driver->bus_resume(hcd); |
| 1299 | if (status == 0) { |
| 1300 | /* TRSMRCY = 10 msec */ |
| 1301 | msleep(10); |
| 1302 | usb_set_device_state(rhdev, rhdev->actconfig |
| 1303 | ? USB_STATE_CONFIGURED |
| 1304 | : USB_STATE_ADDRESS); |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1305 | hcd->state = HC_STATE_RUNNING; |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame^] | 1306 | } else { |
| 1307 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1308 | "resume", status); |
| 1309 | usb_hc_died(hcd); |
| 1310 | } |
| 1311 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1314 | /* Workqueue routine for root-hub remote wakeup */ |
| 1315 | static void hcd_resume_work(struct work_struct *work) |
| 1316 | { |
| 1317 | struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); |
| 1318 | struct usb_device *udev = hcd->self.root_hub; |
| 1319 | |
| 1320 | usb_lock_device(udev); |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1321 | usb_mark_last_busy(udev); |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1322 | usb_external_resume_device(udev); |
| 1323 | usb_unlock_device(udev); |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | /** |
| 1327 | * usb_hcd_resume_root_hub - called by HCD to resume its root hub |
| 1328 | * @hcd: host controller for this root hub |
| 1329 | * |
| 1330 | * The USB host controller calls this function when its root hub is |
| 1331 | * suspended (with the remote wakeup feature enabled) and a remote |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1332 | * wakeup request is received. The routine submits a workqueue request |
| 1333 | * to resume the root hub (that is, manage its downstream ports again). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | */ |
| 1335 | void usb_hcd_resume_root_hub (struct usb_hcd *hcd) |
| 1336 | { |
| 1337 | unsigned long flags; |
| 1338 | |
| 1339 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 1340 | if (hcd->rh_registered) |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1341 | queue_work(ksuspend_usb_wq, &hcd->wakeup_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 1343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); |
| 1345 | |
David Brownell | 9293677 | 2005-09-22 22:32:11 -0700 | [diff] [blame] | 1346 | #endif |
| 1347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | /*-------------------------------------------------------------------------*/ |
| 1349 | |
| 1350 | #ifdef CONFIG_USB_OTG |
| 1351 | |
| 1352 | /** |
| 1353 | * usb_bus_start_enum - start immediate enumeration (for OTG) |
| 1354 | * @bus: the bus (must use hcd framework) |
| 1355 | * @port_num: 1-based number of port; usually bus->otg_port |
| 1356 | * Context: in_interrupt() |
| 1357 | * |
| 1358 | * Starts enumeration, with an immediate reset followed later by |
| 1359 | * khubd identifying and possibly configuring the device. |
| 1360 | * This is needed by OTG controller drivers, where it helps meet |
| 1361 | * HNP protocol timing requirements for starting a port reset. |
| 1362 | */ |
| 1363 | int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) |
| 1364 | { |
| 1365 | struct usb_hcd *hcd; |
| 1366 | int status = -EOPNOTSUPP; |
| 1367 | |
| 1368 | /* NOTE: since HNP can't start by grabbing the bus's address0_sem, |
| 1369 | * boards with root hubs hooked up to internal devices (instead of |
| 1370 | * just the OTG port) may need more attention to resetting... |
| 1371 | */ |
| 1372 | hcd = container_of (bus, struct usb_hcd, self); |
| 1373 | if (port_num && hcd->driver->start_port_reset) |
| 1374 | status = hcd->driver->start_port_reset(hcd, port_num); |
| 1375 | |
| 1376 | /* run khubd shortly after (first) root port reset finishes; |
| 1377 | * it may issue others, until at least 50 msecs have passed. |
| 1378 | */ |
| 1379 | if (status == 0) |
| 1380 | mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); |
| 1381 | return status; |
| 1382 | } |
| 1383 | EXPORT_SYMBOL (usb_bus_start_enum); |
| 1384 | |
| 1385 | #endif |
| 1386 | |
| 1387 | /*-------------------------------------------------------------------------*/ |
| 1388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | /** |
| 1390 | * usb_hcd_giveback_urb - return URB from HCD to device driver |
| 1391 | * @hcd: host controller returning the URB |
| 1392 | * @urb: urb being returned to the USB device driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * Context: in_interrupt() |
| 1394 | * |
| 1395 | * This hands the URB from HCD to its USB device driver, using its |
| 1396 | * completion function. The HCD has freed all per-urb resources |
| 1397 | * (and is done using urb->hcpriv). It also released all HCD locks; |
| 1398 | * the device driver won't cause problems if it frees, modifies, |
| 1399 | * or resubmits this URB. |
| 1400 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1401 | void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | { |
| 1403 | int at_root_hub; |
| 1404 | |
| 1405 | at_root_hub = (urb->dev == hcd->self.root_hub); |
| 1406 | urb_unlink (urb); |
| 1407 | |
Alan Stern | dd990f1 | 2006-08-30 11:29:56 -0400 | [diff] [blame] | 1408 | /* lower level hcd code should use *_dma exclusively if the |
| 1409 | * host controller does DMA */ |
| 1410 | if (hcd->self.uses_dma && !at_root_hub) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | if (usb_pipecontrol (urb->pipe) |
| 1412 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) |
| 1413 | dma_unmap_single (hcd->self.controller, urb->setup_dma, |
| 1414 | sizeof (struct usb_ctrlrequest), |
| 1415 | DMA_TO_DEVICE); |
| 1416 | if (urb->transfer_buffer_length != 0 |
| 1417 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) |
| 1418 | dma_unmap_single (hcd->self.controller, |
| 1419 | urb->transfer_dma, |
| 1420 | urb->transfer_buffer_length, |
| 1421 | usb_pipein (urb->pipe) |
| 1422 | ? DMA_FROM_DEVICE |
| 1423 | : DMA_TO_DEVICE); |
| 1424 | } |
| 1425 | |
| 1426 | usbmon_urb_complete (&hcd->self, urb); |
Oliver Neukum | 51a2f07 | 2007-05-25 13:40:56 +0200 | [diff] [blame] | 1427 | usb_unanchor_urb(urb); |
| 1428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | /* pass ownership to the completion handler */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1430 | urb->complete (urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | atomic_dec (&urb->use_count); |
| 1432 | if (unlikely (urb->reject)) |
| 1433 | wake_up (&usb_kill_urb_queue); |
| 1434 | usb_put_urb (urb); |
| 1435 | } |
| 1436 | EXPORT_SYMBOL (usb_hcd_giveback_urb); |
| 1437 | |
| 1438 | /*-------------------------------------------------------------------------*/ |
| 1439 | |
| 1440 | /** |
| 1441 | * usb_hcd_irq - hook IRQs to HCD framework (bus glue) |
| 1442 | * @irq: the IRQ being raised |
| 1443 | * @__hcd: pointer to the HCD whose IRQ is being signaled |
| 1444 | * @r: saved hardware registers |
| 1445 | * |
| 1446 | * If the controller isn't HALTed, calls the driver's irq handler. |
| 1447 | * Checks whether the controller is now dead. |
| 1448 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1449 | irqreturn_t usb_hcd_irq (int irq, void *__hcd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | { |
| 1451 | struct usb_hcd *hcd = __hcd; |
| 1452 | int start = hcd->state; |
| 1453 | |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1454 | if (unlikely(start == HC_STATE_HALT || |
| 1455 | !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | return IRQ_NONE; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1457 | if (hcd->driver->irq (hcd) == IRQ_NONE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | return IRQ_NONE; |
| 1459 | |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1460 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
| 1461 | |
| 1462 | if (unlikely(hcd->state == HC_STATE_HALT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | usb_hc_died (hcd); |
| 1464 | return IRQ_HANDLED; |
| 1465 | } |
| 1466 | |
| 1467 | /*-------------------------------------------------------------------------*/ |
| 1468 | |
| 1469 | /** |
| 1470 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) |
| 1471 | * @hcd: pointer to the HCD representing the controller |
| 1472 | * |
| 1473 | * This is called by bus glue to report a USB host controller that died |
| 1474 | * while operations may still have been pending. It's called automatically |
| 1475 | * by the PCI glue, so only glue for non-PCI busses should need to call it. |
| 1476 | */ |
| 1477 | void usb_hc_died (struct usb_hcd *hcd) |
| 1478 | { |
| 1479 | unsigned long flags; |
| 1480 | |
| 1481 | dev_err (hcd->self.controller, "HC died; cleaning up\n"); |
| 1482 | |
| 1483 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 1484 | if (hcd->rh_registered) { |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 1485 | hcd->poll_rh = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
| 1487 | /* make khubd clean up old urbs and devices */ |
| 1488 | usb_set_device_state (hcd->self.root_hub, |
| 1489 | USB_STATE_NOTATTACHED); |
| 1490 | usb_kick_khubd (hcd->self.root_hub); |
| 1491 | } |
| 1492 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 1493 | } |
| 1494 | EXPORT_SYMBOL_GPL (usb_hc_died); |
| 1495 | |
| 1496 | /*-------------------------------------------------------------------------*/ |
| 1497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | /** |
| 1499 | * usb_create_hcd - create and initialize an HCD structure |
| 1500 | * @driver: HC driver that will use this hcd |
| 1501 | * @dev: device for this HC, stored in hcd->self.controller |
| 1502 | * @bus_name: value to store in hcd->self.bus_name |
| 1503 | * Context: !in_interrupt() |
| 1504 | * |
| 1505 | * Allocate a struct usb_hcd, with extra space at the end for the |
| 1506 | * HC driver's private data. Initialize the generic members of the |
| 1507 | * hcd structure. |
| 1508 | * |
| 1509 | * If memory is unavailable, returns NULL. |
| 1510 | */ |
| 1511 | struct usb_hcd *usb_create_hcd (const struct hc_driver *driver, |
| 1512 | struct device *dev, char *bus_name) |
| 1513 | { |
| 1514 | struct usb_hcd *hcd; |
| 1515 | |
Pekka Enberg | 7b842b6 | 2005-09-06 15:18:34 -0700 | [diff] [blame] | 1516 | hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | if (!hcd) { |
| 1518 | dev_dbg (dev, "hcd alloc failed\n"); |
| 1519 | return NULL; |
| 1520 | } |
| 1521 | dev_set_drvdata(dev, hcd); |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1522 | kref_init(&hcd->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
| 1524 | usb_bus_init(&hcd->self); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | hcd->self.controller = dev; |
| 1526 | hcd->self.bus_name = bus_name; |
Alan Stern | dd990f1 | 2006-08-30 11:29:56 -0400 | [diff] [blame] | 1527 | hcd->self.uses_dma = (dev->dma_mask != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| 1529 | init_timer(&hcd->rh_timer); |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 1530 | hcd->rh_timer.function = rh_timer_func; |
| 1531 | hcd->rh_timer.data = (unsigned long) hcd; |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1532 | #ifdef CONFIG_PM |
| 1533 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
| 1534 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
| 1536 | hcd->driver = driver; |
| 1537 | hcd->product_desc = (driver->product_desc) ? driver->product_desc : |
| 1538 | "USB Host Controller"; |
| 1539 | |
| 1540 | return hcd; |
| 1541 | } |
| 1542 | EXPORT_SYMBOL (usb_create_hcd); |
| 1543 | |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1544 | static void hcd_release (struct kref *kref) |
| 1545 | { |
| 1546 | struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); |
| 1547 | |
| 1548 | kfree(hcd); |
| 1549 | } |
| 1550 | |
| 1551 | struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd) |
| 1552 | { |
| 1553 | if (hcd) |
| 1554 | kref_get (&hcd->kref); |
| 1555 | return hcd; |
| 1556 | } |
| 1557 | EXPORT_SYMBOL (usb_get_hcd); |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | void usb_put_hcd (struct usb_hcd *hcd) |
| 1560 | { |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 1561 | if (hcd) |
| 1562 | kref_put (&hcd->kref, hcd_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | EXPORT_SYMBOL (usb_put_hcd); |
| 1565 | |
| 1566 | /** |
| 1567 | * usb_add_hcd - finish generic HCD structure initialization and register |
| 1568 | * @hcd: the usb_hcd structure to initialize |
| 1569 | * @irqnum: Interrupt line to allocate |
| 1570 | * @irqflags: Interrupt type flags |
| 1571 | * |
| 1572 | * Finish the remaining parts of generic HCD initialization: allocate the |
| 1573 | * buffers of consistent memory, register the bus, request the IRQ line, |
| 1574 | * and call the driver's reset() and start() routines. |
| 1575 | */ |
| 1576 | int usb_add_hcd(struct usb_hcd *hcd, |
| 1577 | unsigned int irqnum, unsigned long irqflags) |
| 1578 | { |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1579 | int retval; |
| 1580 | struct usb_device *rhdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
| 1582 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
| 1583 | |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1584 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 1585 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1586 | /* HC is in reset state, but accessible. Now do the one-time init, |
| 1587 | * bottom up so that hcds can customize the root hubs before khubd |
| 1588 | * starts talking to them. (Note, bus id is assigned early too.) |
| 1589 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | if ((retval = hcd_buffer_create(hcd)) != 0) { |
| 1591 | dev_dbg(hcd->self.controller, "pool alloc failed\n"); |
| 1592 | return retval; |
| 1593 | } |
| 1594 | |
| 1595 | if ((retval = usb_register_bus(&hcd->self)) < 0) |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1596 | goto err_register_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1598 | if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) { |
| 1599 | dev_err(hcd->self.controller, "unable to allocate root hub\n"); |
| 1600 | retval = -ENOMEM; |
| 1601 | goto err_allocate_root_hub; |
| 1602 | } |
| 1603 | rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH : |
| 1604 | USB_SPEED_FULL; |
| 1605 | hcd->self.root_hub = rhdev; |
| 1606 | |
David Brownell | db4cefa | 2006-05-01 22:07:13 -0700 | [diff] [blame] | 1607 | /* wakeup flag init defaults to "everything works" for root hubs, |
| 1608 | * but drivers can override it in reset() if needed, along with |
| 1609 | * recording the overall controller's system wakeup capability. |
| 1610 | */ |
| 1611 | device_init_wakeup(&rhdev->dev, 1); |
| 1612 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1613 | /* "reset" is misnamed; its role is now one-time init. the controller |
| 1614 | * should already have been reset (and boot firmware kicked off etc). |
| 1615 | */ |
| 1616 | if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { |
| 1617 | dev_err(hcd->self.controller, "can't setup\n"); |
| 1618 | goto err_hcd_driver_setup; |
| 1619 | } |
| 1620 | |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 1621 | /* NOTE: root hub and controller capabilities may not be the same */ |
| 1622 | if (device_can_wakeup(hcd->self.controller) |
| 1623 | && device_can_wakeup(&hcd->self.root_hub->dev)) |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1624 | dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1625 | |
| 1626 | /* enable irqs just before we start the controller */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | if (hcd->driver->irq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", |
| 1629 | hcd->driver->description, hcd->self.busnum); |
| 1630 | if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags, |
| 1631 | hcd->irq_descr, hcd)) != 0) { |
| 1632 | dev_err(hcd->self.controller, |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 1633 | "request interrupt %d failed\n", irqnum); |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1634 | goto err_request_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | } |
| 1636 | hcd->irq = irqnum; |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 1637 | dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | (hcd->driver->flags & HCD_MEMORY) ? |
| 1639 | "io mem" : "io base", |
| 1640 | (unsigned long long)hcd->rsrc_start); |
| 1641 | } else { |
| 1642 | hcd->irq = -1; |
| 1643 | if (hcd->rsrc_start) |
| 1644 | dev_info(hcd->self.controller, "%s 0x%08llx\n", |
| 1645 | (hcd->driver->flags & HCD_MEMORY) ? |
| 1646 | "io mem" : "io base", |
| 1647 | (unsigned long long)hcd->rsrc_start); |
| 1648 | } |
| 1649 | |
| 1650 | if ((retval = hcd->driver->start(hcd)) < 0) { |
| 1651 | dev_err(hcd->self.controller, "startup error %d\n", retval); |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1652 | goto err_hcd_driver_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1655 | /* starting here, usbcore will pay attention to this root hub */ |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1656 | rhdev->bus_mA = min(500u, hcd->power_budget); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1657 | if ((retval = register_root_hub(hcd)) != 0) |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1658 | goto err_register_root_hub; |
| 1659 | |
Alan Stern | d5926ae7 | 2005-04-21 15:56:37 -0400 | [diff] [blame] | 1660 | if (hcd->uses_new_polling && hcd->poll_rh) |
| 1661 | usb_hcd_poll_rh_status(hcd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | return retval; |
| 1663 | |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1664 | err_register_root_hub: |
Alan Stern | 8ec8d20 | 2005-04-25 11:25:17 -0400 | [diff] [blame] | 1665 | hcd->driver->stop(hcd); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1666 | err_hcd_driver_start: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | if (hcd->irq >= 0) |
| 1668 | free_irq(irqnum, hcd); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1669 | err_request_irq: |
| 1670 | err_hcd_driver_setup: |
| 1671 | hcd->self.root_hub = NULL; |
| 1672 | usb_put_dev(rhdev); |
| 1673 | err_allocate_root_hub: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | usb_deregister_bus(&hcd->self); |
David Brownell | b1e8f0a | 2006-01-23 15:25:40 -0800 | [diff] [blame] | 1675 | err_register_bus: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | hcd_buffer_destroy(hcd); |
| 1677 | return retval; |
| 1678 | } |
| 1679 | EXPORT_SYMBOL (usb_add_hcd); |
| 1680 | |
| 1681 | /** |
| 1682 | * usb_remove_hcd - shutdown processing for generic HCDs |
| 1683 | * @hcd: the usb_hcd structure to remove |
| 1684 | * Context: !in_interrupt() |
| 1685 | * |
| 1686 | * Disconnects the root hub, then reverses the effects of usb_add_hcd(), |
| 1687 | * invoking the HCD's stop() method. |
| 1688 | */ |
| 1689 | void usb_remove_hcd(struct usb_hcd *hcd) |
| 1690 | { |
| 1691 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
| 1692 | |
| 1693 | if (HC_IS_RUNNING (hcd->state)) |
| 1694 | hcd->state = HC_STATE_QUIESCING; |
| 1695 | |
| 1696 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); |
| 1697 | spin_lock_irq (&hcd_root_hub_lock); |
| 1698 | hcd->rh_registered = 0; |
| 1699 | spin_unlock_irq (&hcd_root_hub_lock); |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 1700 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1701 | #ifdef CONFIG_PM |
Alan Stern | d5d4db7 | 2007-05-29 16:34:52 -0400 | [diff] [blame] | 1702 | cancel_work_sync(&hcd->wakeup_work); |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1703 | #endif |
| 1704 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1705 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | usb_disconnect(&hcd->self.root_hub); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1707 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
| 1709 | hcd->driver->stop(hcd); |
| 1710 | hcd->state = HC_STATE_HALT; |
| 1711 | |
Alan Stern | 1b42ae6 | 2007-03-13 11:10:52 -0400 | [diff] [blame] | 1712 | hcd->poll_rh = 0; |
| 1713 | del_timer_sync(&hcd->rh_timer); |
| 1714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | if (hcd->irq >= 0) |
| 1716 | free_irq(hcd->irq, hcd); |
| 1717 | usb_deregister_bus(&hcd->self); |
| 1718 | hcd_buffer_destroy(hcd); |
| 1719 | } |
| 1720 | EXPORT_SYMBOL (usb_remove_hcd); |
| 1721 | |
Aleksey Gorelov | 64a21d0 | 2006-08-08 17:24:08 -0700 | [diff] [blame] | 1722 | void |
| 1723 | usb_hcd_platform_shutdown(struct platform_device* dev) |
| 1724 | { |
| 1725 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 1726 | |
| 1727 | if (hcd->driver->shutdown) |
| 1728 | hcd->driver->shutdown(hcd); |
| 1729 | } |
| 1730 | EXPORT_SYMBOL (usb_hcd_platform_shutdown); |
| 1731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | /*-------------------------------------------------------------------------*/ |
| 1733 | |
Adrian Bunk | 4749f32 | 2005-06-23 11:36:56 +0200 | [diff] [blame] | 1734 | #if defined(CONFIG_USB_MON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | struct usb_mon_operations *mon_ops; |
| 1737 | |
| 1738 | /* |
| 1739 | * The registration is unlocked. |
| 1740 | * We do it this way because we do not want to lock in hot paths. |
| 1741 | * |
| 1742 | * Notice that the code is minimally error-proof. Because usbmon needs |
| 1743 | * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. |
| 1744 | */ |
| 1745 | |
| 1746 | int usb_mon_register (struct usb_mon_operations *ops) |
| 1747 | { |
| 1748 | |
| 1749 | if (mon_ops) |
| 1750 | return -EBUSY; |
| 1751 | |
| 1752 | mon_ops = ops; |
| 1753 | mb(); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | EXPORT_SYMBOL_GPL (usb_mon_register); |
| 1757 | |
| 1758 | void usb_mon_deregister (void) |
| 1759 | { |
| 1760 | |
| 1761 | if (mon_ops == NULL) { |
| 1762 | printk(KERN_ERR "USB: monitor was not registered\n"); |
| 1763 | return; |
| 1764 | } |
| 1765 | mon_ops = NULL; |
| 1766 | mb(); |
| 1767 | } |
| 1768 | EXPORT_SYMBOL_GPL (usb_mon_deregister); |
| 1769 | |
| 1770 | #endif /* CONFIG_USB_MON */ |