Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB hub driver. |
| 3 | * |
| 4 | * (C) Copyright 1999 Linus Torvalds |
| 5 | * (C) Copyright 1999 Johannes Erdfelt |
| 6 | * (C) Copyright 1999 Gregory P. Smith |
| 7 | * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) |
| 8 | * |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/ioctl.h> |
| 20 | #include <linux/usb.h> |
| 21 | #include <linux/usbdevice_fs.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 22 | #include <linux/usb/hcd.h> |
Phil Dibowitz | 93362a8 | 2010-07-22 00:05:01 +0200 | [diff] [blame] | 23 | #include <linux/usb/quirks.h> |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 26 | #include <linux/freezer.h> |
Theodore Ts'o | b04b315 | 2012-07-04 11:22:20 -0400 | [diff] [blame^] | 27 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/byteorder.h> |
| 31 | |
| 32 | #include "usb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 34 | /* if we are in debug mode, always announce new devices */ |
| 35 | #ifdef DEBUG |
| 36 | #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES |
| 37 | #define CONFIG_USB_ANNOUNCE_NEW_DEVICES |
| 38 | #endif |
| 39 | #endif |
| 40 | |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 41 | struct usb_hub { |
| 42 | struct device *intfdev; /* the "interface" device */ |
| 43 | struct usb_device *hdev; |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 44 | struct kref kref; |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 45 | struct urb *urb; /* for interrupt polling pipe */ |
| 46 | |
| 47 | /* buffer for urb ... with extra space in case of babble */ |
| 48 | char (*buffer)[8]; |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 49 | union { |
| 50 | struct usb_hub_status hub; |
| 51 | struct usb_port_status port; |
| 52 | } *status; /* buffer for status reports */ |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 53 | struct mutex status_mutex; /* for the status buffer */ |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 54 | |
| 55 | int error; /* last reported error */ |
| 56 | int nerrors; /* track consecutive errors */ |
| 57 | |
| 58 | struct list_head event_list; /* hubs w/data or errs ready */ |
| 59 | unsigned long event_bits[1]; /* status change bitmask */ |
| 60 | unsigned long change_bits[1]; /* ports with logical connect |
| 61 | status change */ |
| 62 | unsigned long busy_bits[1]; /* ports being reset or |
| 63 | resumed */ |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 64 | unsigned long removed_bits[1]; /* ports with a "removed" |
| 65 | device present */ |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 66 | unsigned long wakeup_bits[1]; /* ports that have signaled |
| 67 | remote wakeup */ |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 68 | #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ |
| 69 | #error event_bits[] is too short! |
| 70 | #endif |
| 71 | |
| 72 | struct usb_hub_descriptor *descriptor; /* class descriptor */ |
| 73 | struct usb_tt tt; /* Transaction Translator */ |
| 74 | |
| 75 | unsigned mA_per_port; /* current for each child */ |
| 76 | |
| 77 | unsigned limited_power:1; |
| 78 | unsigned quiescing:1; |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 79 | unsigned disconnected:1; |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 80 | |
| 81 | unsigned has_indicators:1; |
| 82 | u8 indicator[USB_MAXCHILDREN]; |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 83 | struct delayed_work leds; |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 84 | struct delayed_work init_work; |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 85 | void **port_owners; |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 88 | static inline int hub_is_superspeed(struct usb_device *hdev) |
| 89 | { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 90 | return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS); |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 91 | } |
Alan Stern | 1bb5f66 | 2006-11-06 11:56:13 -0500 | [diff] [blame] | 92 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 93 | /* Protect struct usb_device->state and ->children members |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 94 | * Note: Both are also protected by ->dev.sem, except that ->state can |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ |
| 96 | static DEFINE_SPINLOCK(device_state_lock); |
| 97 | |
| 98 | /* khubd's worklist and its lock */ |
| 99 | static DEFINE_SPINLOCK(hub_event_lock); |
| 100 | static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ |
| 101 | |
| 102 | /* Wakes up khubd */ |
| 103 | static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); |
| 104 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 105 | static struct task_struct *khubd_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* cycle leds on hubs that aren't blinking for attention */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 108 | static bool blinkenlights = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | module_param (blinkenlights, bool, S_IRUGO); |
| 110 | MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); |
| 111 | |
| 112 | /* |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 113 | * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about |
| 114 | * 10 seconds to send reply for the initial 64-byte descriptor request. |
| 115 | */ |
| 116 | /* define initial 64-byte descriptor request timeout in milliseconds */ |
| 117 | static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; |
| 118 | module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 119 | MODULE_PARM_DESC(initial_descriptor_timeout, |
| 120 | "initial 64-byte descriptor request timeout in milliseconds " |
| 121 | "(default 5000 - 5.0 seconds)"); |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 122 | |
| 123 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * As of 2.6.10 we introduce a new USB device initialization scheme which |
| 125 | * closely resembles the way Windows works. Hopefully it will be compatible |
| 126 | * with a wider range of devices than the old scheme. However some previously |
| 127 | * working devices may start giving rise to "device not accepting address" |
| 128 | * errors; if that happens the user can try the old scheme by adjusting the |
| 129 | * following module parameters. |
| 130 | * |
| 131 | * For maximum flexibility there are two boolean parameters to control the |
| 132 | * hub driver's behavior. On the first initialization attempt, if the |
| 133 | * "old_scheme_first" parameter is set then the old scheme will be used, |
| 134 | * otherwise the new scheme is used. If that fails and "use_both_schemes" |
| 135 | * is set, then the driver will make another attempt, using the other scheme. |
| 136 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 137 | static bool old_scheme_first = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); |
| 139 | MODULE_PARM_DESC(old_scheme_first, |
| 140 | "start with the old device initialization scheme"); |
| 141 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 142 | static bool use_both_schemes = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); |
| 144 | MODULE_PARM_DESC(use_both_schemes, |
| 145 | "try the other device initialization scheme if the " |
| 146 | "first one fails"); |
| 147 | |
Alan Stern | 32fe019 | 2007-10-10 16:27:07 -0400 | [diff] [blame] | 148 | /* Mutual exclusion for EHCI CF initialization. This interferes with |
| 149 | * port reset on some companion controllers. |
| 150 | */ |
| 151 | DECLARE_RWSEM(ehci_cf_port_reset_rwsem); |
| 152 | EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); |
| 153 | |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 154 | #define HUB_DEBOUNCE_TIMEOUT 1500 |
| 155 | #define HUB_DEBOUNCE_STEP 25 |
| 156 | #define HUB_DEBOUNCE_STABLE 100 |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 159 | static int usb_reset_and_verify_device(struct usb_device *udev); |
| 160 | |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 161 | static inline char *portspeed(struct usb_hub *hub, int portstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 163 | if (hub_is_superspeed(hub->hdev)) |
| 164 | return "5.0 Gb/s"; |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 165 | if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return "480 Mb/s"; |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 167 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return "1.5 Mb/s"; |
| 169 | else |
| 170 | return "12 Mb/s"; |
| 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* Note that hdev or one of its children must be locked! */ |
Alan Stern | 25118084 | 2009-07-22 14:41:18 -0400 | [diff] [blame] | 174 | static struct usb_hub *hdev_to_hub(struct usb_device *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 176 | if (!hdev || !hdev->actconfig) |
Alan Stern | 25118084 | 2009-07-22 14:41:18 -0400 | [diff] [blame] | 177 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return usb_get_intfdata(hdev->actconfig->interface[0]); |
| 179 | } |
| 180 | |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 181 | static int usb_device_supports_lpm(struct usb_device *udev) |
| 182 | { |
| 183 | /* USB 2.1 (and greater) devices indicate LPM support through |
| 184 | * their USB 2.0 Extended Capabilities BOS descriptor. |
| 185 | */ |
| 186 | if (udev->speed == USB_SPEED_HIGH) { |
| 187 | if (udev->bos->ext_cap && |
| 188 | (USB_LPM_SUPPORT & |
| 189 | le32_to_cpu(udev->bos->ext_cap->bmAttributes))) |
| 190 | return 1; |
| 191 | return 0; |
| 192 | } |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 193 | |
| 194 | /* All USB 3.0 must support LPM, but we need their max exit latency |
| 195 | * information from the SuperSpeed Extended Capabilities BOS descriptor. |
| 196 | */ |
| 197 | if (!udev->bos->ss_cap) { |
| 198 | dev_warn(&udev->dev, "No LPM exit latency info found. " |
| 199 | "Power management will be impacted.\n"); |
| 200 | return 0; |
| 201 | } |
| 202 | if (udev->parent->lpm_capable) |
| 203 | return 1; |
| 204 | |
| 205 | dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " |
| 206 | "Power management will be impacted.\n"); |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 210 | /* |
| 211 | * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from |
| 212 | * either U1 or U2. |
| 213 | */ |
| 214 | static void usb_set_lpm_mel(struct usb_device *udev, |
| 215 | struct usb3_lpm_parameters *udev_lpm_params, |
| 216 | unsigned int udev_exit_latency, |
| 217 | struct usb_hub *hub, |
| 218 | struct usb3_lpm_parameters *hub_lpm_params, |
| 219 | unsigned int hub_exit_latency) |
| 220 | { |
| 221 | unsigned int total_mel; |
| 222 | unsigned int device_mel; |
| 223 | unsigned int hub_mel; |
| 224 | |
| 225 | /* |
| 226 | * Calculate the time it takes to transition all links from the roothub |
| 227 | * to the parent hub into U0. The parent hub must then decode the |
| 228 | * packet (hub header decode latency) to figure out which port it was |
| 229 | * bound for. |
| 230 | * |
| 231 | * The Hub Header decode latency is expressed in 0.1us intervals (0x1 |
| 232 | * means 0.1us). Multiply that by 100 to get nanoseconds. |
| 233 | */ |
| 234 | total_mel = hub_lpm_params->mel + |
| 235 | (hub->descriptor->u.ss.bHubHdrDecLat * 100); |
| 236 | |
| 237 | /* |
| 238 | * How long will it take to transition the downstream hub's port into |
| 239 | * U0? The greater of either the hub exit latency or the device exit |
| 240 | * latency. |
| 241 | * |
| 242 | * The BOS U1/U2 exit latencies are expressed in 1us intervals. |
| 243 | * Multiply that by 1000 to get nanoseconds. |
| 244 | */ |
| 245 | device_mel = udev_exit_latency * 1000; |
| 246 | hub_mel = hub_exit_latency * 1000; |
| 247 | if (device_mel > hub_mel) |
| 248 | total_mel += device_mel; |
| 249 | else |
| 250 | total_mel += hub_mel; |
| 251 | |
| 252 | udev_lpm_params->mel = total_mel; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate |
| 257 | * a transition from either U1 or U2. |
| 258 | */ |
| 259 | static void usb_set_lpm_pel(struct usb_device *udev, |
| 260 | struct usb3_lpm_parameters *udev_lpm_params, |
| 261 | unsigned int udev_exit_latency, |
| 262 | struct usb_hub *hub, |
| 263 | struct usb3_lpm_parameters *hub_lpm_params, |
| 264 | unsigned int hub_exit_latency, |
| 265 | unsigned int port_to_port_exit_latency) |
| 266 | { |
| 267 | unsigned int first_link_pel; |
| 268 | unsigned int hub_pel; |
| 269 | |
| 270 | /* |
| 271 | * First, the device sends an LFPS to transition the link between the |
| 272 | * device and the parent hub into U0. The exit latency is the bigger of |
| 273 | * the device exit latency or the hub exit latency. |
| 274 | */ |
| 275 | if (udev_exit_latency > hub_exit_latency) |
| 276 | first_link_pel = udev_exit_latency * 1000; |
| 277 | else |
| 278 | first_link_pel = hub_exit_latency * 1000; |
| 279 | |
| 280 | /* |
| 281 | * When the hub starts to receive the LFPS, there is a slight delay for |
| 282 | * it to figure out that one of the ports is sending an LFPS. Then it |
| 283 | * will forward the LFPS to its upstream link. The exit latency is the |
| 284 | * delay, plus the PEL that we calculated for this hub. |
| 285 | */ |
| 286 | hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; |
| 287 | |
| 288 | /* |
| 289 | * According to figure C-7 in the USB 3.0 spec, the PEL for this device |
| 290 | * is the greater of the two exit latencies. |
| 291 | */ |
| 292 | if (first_link_pel > hub_pel) |
| 293 | udev_lpm_params->pel = first_link_pel; |
| 294 | else |
| 295 | udev_lpm_params->pel = hub_pel; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Set the System Exit Latency (SEL) to indicate the total worst-case time from |
| 300 | * when a device initiates a transition to U0, until when it will receive the |
| 301 | * first packet from the host controller. |
| 302 | * |
| 303 | * Section C.1.5.1 describes the four components to this: |
| 304 | * - t1: device PEL |
| 305 | * - t2: time for the ERDY to make it from the device to the host. |
| 306 | * - t3: a host-specific delay to process the ERDY. |
| 307 | * - t4: time for the packet to make it from the host to the device. |
| 308 | * |
| 309 | * t3 is specific to both the xHCI host and the platform the host is integrated |
| 310 | * into. The Intel HW folks have said it's negligible, FIXME if a different |
| 311 | * vendor says otherwise. |
| 312 | */ |
| 313 | static void usb_set_lpm_sel(struct usb_device *udev, |
| 314 | struct usb3_lpm_parameters *udev_lpm_params) |
| 315 | { |
| 316 | struct usb_device *parent; |
| 317 | unsigned int num_hubs; |
| 318 | unsigned int total_sel; |
| 319 | |
| 320 | /* t1 = device PEL */ |
| 321 | total_sel = udev_lpm_params->pel; |
| 322 | /* How many external hubs are in between the device & the root port. */ |
| 323 | for (parent = udev->parent, num_hubs = 0; parent->parent; |
| 324 | parent = parent->parent) |
| 325 | num_hubs++; |
| 326 | /* t2 = 2.1us + 250ns * (num_hubs - 1) */ |
| 327 | if (num_hubs > 0) |
| 328 | total_sel += 2100 + 250 * (num_hubs - 1); |
| 329 | |
| 330 | /* t4 = 250ns * num_hubs */ |
| 331 | total_sel += 250 * num_hubs; |
| 332 | |
| 333 | udev_lpm_params->sel = total_sel; |
| 334 | } |
| 335 | |
| 336 | static void usb_set_lpm_parameters(struct usb_device *udev) |
| 337 | { |
| 338 | struct usb_hub *hub; |
| 339 | unsigned int port_to_port_delay; |
| 340 | unsigned int udev_u1_del; |
| 341 | unsigned int udev_u2_del; |
| 342 | unsigned int hub_u1_del; |
| 343 | unsigned int hub_u2_del; |
| 344 | |
| 345 | if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER) |
| 346 | return; |
| 347 | |
| 348 | hub = hdev_to_hub(udev->parent); |
| 349 | /* It doesn't take time to transition the roothub into U0, since it |
| 350 | * doesn't have an upstream link. |
| 351 | */ |
| 352 | if (!hub) |
| 353 | return; |
| 354 | |
| 355 | udev_u1_del = udev->bos->ss_cap->bU1devExitLat; |
| 356 | udev_u2_del = udev->bos->ss_cap->bU2DevExitLat; |
| 357 | hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; |
| 358 | hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat; |
| 359 | |
| 360 | usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, |
| 361 | hub, &udev->parent->u1_params, hub_u1_del); |
| 362 | |
| 363 | usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, |
| 364 | hub, &udev->parent->u2_params, hub_u2_del); |
| 365 | |
| 366 | /* |
| 367 | * Appendix C, section C.2.2.2, says that there is a slight delay from |
| 368 | * when the parent hub notices the downstream port is trying to |
| 369 | * transition to U0 to when the hub initiates a U0 transition on its |
| 370 | * upstream port. The section says the delays are tPort2PortU1EL and |
| 371 | * tPort2PortU2EL, but it doesn't define what they are. |
| 372 | * |
| 373 | * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking |
| 374 | * about the same delays. Use the maximum delay calculations from those |
| 375 | * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For |
| 376 | * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I |
| 377 | * assume the device exit latencies they are talking about are the hub |
| 378 | * exit latencies. |
| 379 | * |
| 380 | * What do we do if the U2 exit latency is less than the U1 exit |
| 381 | * latency? It's possible, although not likely... |
| 382 | */ |
| 383 | port_to_port_delay = 1; |
| 384 | |
| 385 | usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, |
| 386 | hub, &udev->parent->u1_params, hub_u1_del, |
| 387 | port_to_port_delay); |
| 388 | |
| 389 | if (hub_u2_del > hub_u1_del) |
| 390 | port_to_port_delay = 1 + hub_u2_del - hub_u1_del; |
| 391 | else |
| 392 | port_to_port_delay = 1 + hub_u1_del; |
| 393 | |
| 394 | usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, |
| 395 | hub, &udev->parent->u2_params, hub_u2_del, |
| 396 | port_to_port_delay); |
| 397 | |
| 398 | /* Now that we've got PEL, calculate SEL. */ |
| 399 | usb_set_lpm_sel(udev, &udev->u1_params); |
| 400 | usb_set_lpm_sel(udev, &udev->u2_params); |
| 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /* USB 2.0 spec Section 11.24.4.5 */ |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 404 | static int get_hub_descriptor(struct usb_device *hdev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 406 | int i, ret, size; |
| 407 | unsigned dtype; |
| 408 | |
| 409 | if (hub_is_superspeed(hdev)) { |
| 410 | dtype = USB_DT_SS_HUB; |
| 411 | size = USB_DT_SS_HUB_SIZE; |
| 412 | } else { |
| 413 | dtype = USB_DT_HUB; |
| 414 | size = sizeof(struct usb_hub_descriptor); |
| 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | for (i = 0; i < 3; i++) { |
| 418 | ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 419 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 420 | dtype << 8, 0, data, size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | USB_CTRL_GET_TIMEOUT); |
| 422 | if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) |
| 423 | return ret; |
| 424 | } |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * USB 2.0 spec Section 11.24.2.1 |
| 430 | */ |
| 431 | static int clear_hub_feature(struct usb_device *hdev, int feature) |
| 432 | { |
| 433 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 434 | USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * USB 2.0 spec Section 11.24.2.2 |
| 439 | */ |
| 440 | static int clear_port_feature(struct usb_device *hdev, int port1, int feature) |
| 441 | { |
| 442 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 443 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, |
| 444 | NULL, 0, 1000); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * USB 2.0 spec Section 11.24.2.13 |
| 449 | */ |
| 450 | static int set_port_feature(struct usb_device *hdev, int port1, int feature) |
| 451 | { |
| 452 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 453 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, |
| 454 | NULL, 0, 1000); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 |
| 459 | * for info about using port indicators |
| 460 | */ |
| 461 | static void set_port_led( |
| 462 | struct usb_hub *hub, |
| 463 | int port1, |
| 464 | int selector |
| 465 | ) |
| 466 | { |
| 467 | int status = set_port_feature(hub->hdev, (selector << 8) | port1, |
| 468 | USB_PORT_FEAT_INDICATOR); |
| 469 | if (status < 0) |
| 470 | dev_dbg (hub->intfdev, |
| 471 | "port %d indicator %s status %d\n", |
| 472 | port1, |
| 473 | ({ char *s; switch (selector) { |
| 474 | case HUB_LED_AMBER: s = "amber"; break; |
| 475 | case HUB_LED_GREEN: s = "green"; break; |
| 476 | case HUB_LED_OFF: s = "off"; break; |
| 477 | case HUB_LED_AUTO: s = "auto"; break; |
| 478 | default: s = "??"; break; |
| 479 | }; s; }), |
| 480 | status); |
| 481 | } |
| 482 | |
| 483 | #define LED_CYCLE_PERIOD ((2*HZ)/3) |
| 484 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 485 | static void led_work (struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 487 | struct usb_hub *hub = |
| 488 | container_of(work, struct usb_hub, leds.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | struct usb_device *hdev = hub->hdev; |
| 490 | unsigned i; |
| 491 | unsigned changed = 0; |
| 492 | int cursor = -1; |
| 493 | |
| 494 | if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) |
| 495 | return; |
| 496 | |
| 497 | for (i = 0; i < hub->descriptor->bNbrPorts; i++) { |
| 498 | unsigned selector, mode; |
| 499 | |
| 500 | /* 30%-50% duty cycle */ |
| 501 | |
| 502 | switch (hub->indicator[i]) { |
| 503 | /* cycle marker */ |
| 504 | case INDICATOR_CYCLE: |
| 505 | cursor = i; |
| 506 | selector = HUB_LED_AUTO; |
| 507 | mode = INDICATOR_AUTO; |
| 508 | break; |
| 509 | /* blinking green = sw attention */ |
| 510 | case INDICATOR_GREEN_BLINK: |
| 511 | selector = HUB_LED_GREEN; |
| 512 | mode = INDICATOR_GREEN_BLINK_OFF; |
| 513 | break; |
| 514 | case INDICATOR_GREEN_BLINK_OFF: |
| 515 | selector = HUB_LED_OFF; |
| 516 | mode = INDICATOR_GREEN_BLINK; |
| 517 | break; |
| 518 | /* blinking amber = hw attention */ |
| 519 | case INDICATOR_AMBER_BLINK: |
| 520 | selector = HUB_LED_AMBER; |
| 521 | mode = INDICATOR_AMBER_BLINK_OFF; |
| 522 | break; |
| 523 | case INDICATOR_AMBER_BLINK_OFF: |
| 524 | selector = HUB_LED_OFF; |
| 525 | mode = INDICATOR_AMBER_BLINK; |
| 526 | break; |
| 527 | /* blink green/amber = reserved */ |
| 528 | case INDICATOR_ALT_BLINK: |
| 529 | selector = HUB_LED_GREEN; |
| 530 | mode = INDICATOR_ALT_BLINK_OFF; |
| 531 | break; |
| 532 | case INDICATOR_ALT_BLINK_OFF: |
| 533 | selector = HUB_LED_AMBER; |
| 534 | mode = INDICATOR_ALT_BLINK; |
| 535 | break; |
| 536 | default: |
| 537 | continue; |
| 538 | } |
| 539 | if (selector != HUB_LED_AUTO) |
| 540 | changed = 1; |
| 541 | set_port_led(hub, i + 1, selector); |
| 542 | hub->indicator[i] = mode; |
| 543 | } |
| 544 | if (!changed && blinkenlights) { |
| 545 | cursor++; |
| 546 | cursor %= hub->descriptor->bNbrPorts; |
| 547 | set_port_led(hub, cursor + 1, HUB_LED_GREEN); |
| 548 | hub->indicator[cursor] = INDICATOR_CYCLE; |
| 549 | changed++; |
| 550 | } |
| 551 | if (changed) |
| 552 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); |
| 553 | } |
| 554 | |
| 555 | /* use a short timeout for hub/port status fetches */ |
| 556 | #define USB_STS_TIMEOUT 1000 |
| 557 | #define USB_STS_RETRIES 5 |
| 558 | |
| 559 | /* |
| 560 | * USB 2.0 spec Section 11.24.2.6 |
| 561 | */ |
| 562 | static int get_hub_status(struct usb_device *hdev, |
| 563 | struct usb_hub_status *data) |
| 564 | { |
| 565 | int i, status = -ETIMEDOUT; |
| 566 | |
Libor Pechacek | 3824c1d | 2011-05-20 14:53:25 +0200 | [diff] [blame] | 567 | for (i = 0; i < USB_STS_RETRIES && |
| 568 | (status == -ETIMEDOUT || status == -EPIPE); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 570 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, |
| 571 | data, sizeof(*data), USB_STS_TIMEOUT); |
| 572 | } |
| 573 | return status; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * USB 2.0 spec Section 11.24.2.7 |
| 578 | */ |
| 579 | static int get_port_status(struct usb_device *hdev, int port1, |
| 580 | struct usb_port_status *data) |
| 581 | { |
| 582 | int i, status = -ETIMEDOUT; |
| 583 | |
Libor Pechacek | 3824c1d | 2011-05-20 14:53:25 +0200 | [diff] [blame] | 584 | for (i = 0; i < USB_STS_RETRIES && |
| 585 | (status == -ETIMEDOUT || status == -EPIPE); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 587 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, |
| 588 | data, sizeof(*data), USB_STS_TIMEOUT); |
| 589 | } |
| 590 | return status; |
| 591 | } |
| 592 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 593 | static int hub_port_status(struct usb_hub *hub, int port1, |
| 594 | u16 *status, u16 *change) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | mutex_lock(&hub->status_mutex); |
| 599 | ret = get_port_status(hub->hdev, port1, &hub->status->port); |
| 600 | if (ret < 4) { |
| 601 | dev_err(hub->intfdev, |
| 602 | "%s failed (err = %d)\n", __func__, ret); |
| 603 | if (ret >= 0) |
| 604 | ret = -EIO; |
| 605 | } else { |
| 606 | *status = le16_to_cpu(hub->status->port.wPortStatus); |
| 607 | *change = le16_to_cpu(hub->status->port.wPortChange); |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 608 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 609 | ret = 0; |
| 610 | } |
| 611 | mutex_unlock(&hub->status_mutex); |
| 612 | return ret; |
| 613 | } |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | static void kick_khubd(struct usb_hub *hub) |
| 616 | { |
| 617 | unsigned long flags; |
| 618 | |
| 619 | spin_lock_irqsave(&hub_event_lock, flags); |
Roel Kluin | 2e2c5ee | 2007-10-26 23:54:35 +0200 | [diff] [blame] | 620 | if (!hub->disconnected && list_empty(&hub->event_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | list_add_tail(&hub->event_list, &hub_event_list); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 622 | |
| 623 | /* Suppress autosuspend until khubd runs */ |
| 624 | usb_autopm_get_interface_no_resume( |
| 625 | to_usb_interface(hub->intfdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | wake_up(&khubd_wait); |
| 627 | } |
| 628 | spin_unlock_irqrestore(&hub_event_lock, flags); |
| 629 | } |
| 630 | |
| 631 | void usb_kick_khubd(struct usb_device *hdev) |
| 632 | { |
Alan Stern | 25118084 | 2009-07-22 14:41:18 -0400 | [diff] [blame] | 633 | struct usb_hub *hub = hdev_to_hub(hdev); |
| 634 | |
| 635 | if (hub) |
| 636 | kick_khubd(hub); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 639 | /* |
| 640 | * Let the USB core know that a USB 3.0 device has sent a Function Wake Device |
| 641 | * Notification, which indicates it had initiated remote wakeup. |
| 642 | * |
| 643 | * USB 3.0 hubs do not report the port link state change from U3 to U0 when the |
| 644 | * device initiates resume, so the USB core will not receive notice of the |
| 645 | * resume through the normal hub interrupt URB. |
| 646 | */ |
| 647 | void usb_wakeup_notification(struct usb_device *hdev, |
| 648 | unsigned int portnum) |
| 649 | { |
| 650 | struct usb_hub *hub; |
| 651 | |
| 652 | if (!hdev) |
| 653 | return; |
| 654 | |
| 655 | hub = hdev_to_hub(hdev); |
| 656 | if (hub) { |
| 657 | set_bit(portnum, hub->wakeup_bits); |
| 658 | kick_khubd(hub); |
| 659 | } |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(usb_wakeup_notification); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | /* completion function, fires on port status changes and various faults */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 664 | static void hub_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 666 | struct usb_hub *hub = urb->context; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 667 | int status = urb->status; |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 668 | unsigned i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | unsigned long bits; |
| 670 | |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 671 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | case -ENOENT: /* synchronous unlink */ |
| 673 | case -ECONNRESET: /* async unlink */ |
| 674 | case -ESHUTDOWN: /* hardware going away */ |
| 675 | return; |
| 676 | |
| 677 | default: /* presumably an error */ |
| 678 | /* Cause a hub reset after 10 consecutive errors */ |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 679 | dev_dbg (hub->intfdev, "transfer --> %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | if ((++hub->nerrors < 10) || hub->error) |
| 681 | goto resubmit; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 682 | hub->error = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /* FALL THROUGH */ |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | /* let khubd handle things */ |
| 686 | case 0: /* we got data: port status changed */ |
| 687 | bits = 0; |
| 688 | for (i = 0; i < urb->actual_length; ++i) |
| 689 | bits |= ((unsigned long) ((*hub->buffer)[i])) |
| 690 | << (i*8); |
| 691 | hub->event_bits[0] = bits; |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | hub->nerrors = 0; |
| 696 | |
| 697 | /* Something happened, let khubd figure it out */ |
| 698 | kick_khubd(hub); |
| 699 | |
| 700 | resubmit: |
| 701 | if (hub->quiescing) |
| 702 | return; |
| 703 | |
| 704 | if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 |
| 705 | && status != -ENODEV && status != -EPERM) |
| 706 | dev_err (hub->intfdev, "resubmit --> %d\n", status); |
| 707 | } |
| 708 | |
| 709 | /* USB 2.0 spec Section 11.24.2.3 */ |
| 710 | static inline int |
| 711 | hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) |
| 712 | { |
Alan Stern | c2f6595 | 2009-11-18 11:37:15 -0500 | [diff] [blame] | 713 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, |
| 715 | tt, NULL, 0, 1000); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * enumeration blocks khubd for a long time. we use keventd instead, since |
| 720 | * long blocking there is the exception, not the rule. accordingly, HCDs |
| 721 | * talking to TTs must queue control transfers (not just bulk and iso), so |
| 722 | * both can talk to the same hub concurrently. |
| 723 | */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 724 | static void hub_tt_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 726 | struct usb_hub *hub = |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 727 | container_of(work, struct usb_hub, tt.clear_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | unsigned long flags; |
Mark Lord | 55e5fdf | 2007-05-14 19:48:02 -0400 | [diff] [blame] | 729 | int limit = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | spin_lock_irqsave (&hub->tt.lock, flags); |
Mark Lord | 55e5fdf | 2007-05-14 19:48:02 -0400 | [diff] [blame] | 732 | while (--limit && !list_empty (&hub->tt.clear_list)) { |
H Hartley Sweeten | d0f830d | 2009-04-22 16:03:05 -0400 | [diff] [blame] | 733 | struct list_head *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | struct usb_tt_clear *clear; |
| 735 | struct usb_device *hdev = hub->hdev; |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 736 | const struct hc_driver *drv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | int status; |
| 738 | |
H Hartley Sweeten | d0f830d | 2009-04-22 16:03:05 -0400 | [diff] [blame] | 739 | next = hub->tt.clear_list.next; |
| 740 | clear = list_entry (next, struct usb_tt_clear, clear_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | list_del (&clear->clear_list); |
| 742 | |
| 743 | /* drop lock so HCD can concurrently report other TT errors */ |
| 744 | spin_unlock_irqrestore (&hub->tt.lock, flags); |
| 745 | status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | if (status) |
| 747 | dev_err (&hdev->dev, |
| 748 | "clear tt %d (%04x) error %d\n", |
| 749 | clear->tt, clear->devinfo, status); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 750 | |
| 751 | /* Tell the HCD, even if the operation failed */ |
| 752 | drv = clear->hcd->driver; |
| 753 | if (drv->clear_tt_buffer_complete) |
| 754 | (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); |
| 755 | |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 756 | kfree(clear); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 757 | spin_lock_irqsave(&hub->tt.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | spin_unlock_irqrestore (&hub->tt.lock, flags); |
| 760 | } |
| 761 | |
| 762 | /** |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 763 | * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub |
| 764 | * @urb: an URB associated with the failed or incomplete split transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | * |
| 766 | * High speed HCDs use this to tell the hub driver that some split control or |
| 767 | * bulk transaction failed in a way that requires clearing internal state of |
| 768 | * a transaction translator. This is normally detected (and reported) from |
| 769 | * interrupt context. |
| 770 | * |
| 771 | * It may not be possible for that hub to handle additional full (or low) |
| 772 | * speed transactions until that state is fully cleared out. |
| 773 | */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 774 | int usb_hub_clear_tt_buffer(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 776 | struct usb_device *udev = urb->dev; |
| 777 | int pipe = urb->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | struct usb_tt *tt = udev->tt; |
| 779 | unsigned long flags; |
| 780 | struct usb_tt_clear *clear; |
| 781 | |
| 782 | /* we've got to cope with an arbitrary number of pending TT clears, |
| 783 | * since each TT has "at least two" buffers that can need it (and |
| 784 | * there can be many TTs per hub). even if they're uncommon. |
| 785 | */ |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 786 | if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); |
| 788 | /* FIXME recover somehow ... RESET_TT? */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 789 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* info that CLEAR_TT_BUFFER needs */ |
| 793 | clear->tt = tt->multi ? udev->ttport : 1; |
| 794 | clear->devinfo = usb_pipeendpoint (pipe); |
| 795 | clear->devinfo |= udev->devnum << 4; |
| 796 | clear->devinfo |= usb_pipecontrol (pipe) |
| 797 | ? (USB_ENDPOINT_XFER_CONTROL << 11) |
| 798 | : (USB_ENDPOINT_XFER_BULK << 11); |
| 799 | if (usb_pipein (pipe)) |
| 800 | clear->devinfo |= 1 << 15; |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 801 | |
| 802 | /* info for completion callback */ |
| 803 | clear->hcd = bus_to_hcd(udev->bus); |
| 804 | clear->ep = urb->ep; |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | /* tell keventd to clear state for this TT */ |
| 807 | spin_lock_irqsave (&tt->lock, flags); |
| 808 | list_add_tail (&clear->clear_list, &tt->clear_list); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 809 | schedule_work(&tt->clear_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | spin_unlock_irqrestore (&tt->lock, flags); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 813 | EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 815 | /* If do_delay is false, return the number of milliseconds the caller |
| 816 | * needs to delay. |
| 817 | */ |
| 818 | static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
| 820 | int port1; |
David Brownell | b789696 | 2005-08-31 10:41:44 -0700 | [diff] [blame] | 821 | unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 822 | unsigned delay; |
Alan Stern | 4489a57 | 2006-04-27 15:54:22 -0400 | [diff] [blame] | 823 | u16 wHubCharacteristics = |
| 824 | le16_to_cpu(hub->descriptor->wHubCharacteristics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Alan Stern | 4489a57 | 2006-04-27 15:54:22 -0400 | [diff] [blame] | 826 | /* Enable power on each port. Some hubs have reserved values |
| 827 | * of LPSM (> 2) in their descriptors, even though they are |
| 828 | * USB 2.0 hubs. Some hubs do not implement port-power switching |
| 829 | * but only emulate it. In all cases, the ports won't work |
| 830 | * unless we send these messages to the hub. |
| 831 | */ |
| 832 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | dev_dbg(hub->intfdev, "enabling power on all ports\n"); |
Alan Stern | 4489a57 | 2006-04-27 15:54:22 -0400 | [diff] [blame] | 834 | else |
| 835 | dev_dbg(hub->intfdev, "trying to enable port power on " |
| 836 | "non-switchable hub\n"); |
| 837 | for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) |
| 838 | set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
David Brownell | b789696 | 2005-08-31 10:41:44 -0700 | [diff] [blame] | 840 | /* Wait at least 100 msec for power to become stable */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 841 | delay = max(pgood_delay, (unsigned) 100); |
| 842 | if (do_delay) |
| 843 | msleep(delay); |
| 844 | return delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | static int hub_hub_status(struct usb_hub *hub, |
| 848 | u16 *status, u16 *change) |
| 849 | { |
| 850 | int ret; |
| 851 | |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 852 | mutex_lock(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | ret = get_hub_status(hub->hdev, &hub->status->hub); |
| 854 | if (ret < 0) |
| 855 | dev_err (hub->intfdev, |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 856 | "%s failed (err = %d)\n", __func__, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | else { |
| 858 | *status = le16_to_cpu(hub->status->hub.wHubStatus); |
| 859 | *change = le16_to_cpu(hub->status->hub.wHubChange); |
| 860 | ret = 0; |
| 861 | } |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 862 | mutex_unlock(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return ret; |
| 864 | } |
| 865 | |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 866 | static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) |
| 867 | { |
| 868 | struct usb_device *hdev = hub->hdev; |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 869 | int ret = 0; |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 870 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 871 | if (hdev->children[port1-1] && set_state) |
| 872 | usb_set_device_state(hdev->children[port1-1], |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 873 | USB_STATE_NOTATTACHED); |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 874 | if (!hub->error && !hub_is_superspeed(hub->hdev)) |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 875 | ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 876 | if (ret) |
| 877 | dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 878 | port1, ret); |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 879 | return ret; |
| 880 | } |
| 881 | |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 882 | /* |
Justin P. Mattock | 6d42fcd | 2011-02-26 20:33:56 -0800 | [diff] [blame] | 883 | * Disable a port and mark a logical connect-change event, so that some |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 884 | * time later khubd will disconnect() any existing usb_device on the port |
| 885 | * and will re-enumerate if there actually is a device attached. |
| 886 | */ |
| 887 | static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) |
| 888 | { |
| 889 | dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); |
| 890 | hub_port_disable(hub, port1, 1); |
| 891 | |
| 892 | /* FIXME let caller ask to power down the port: |
| 893 | * - some devices won't enumerate without a VBUS power cycle |
| 894 | * - SRP saves power that way |
| 895 | * - ... new call, TBD ... |
| 896 | * That's easy if this hub can switch power per-port, and |
| 897 | * khubd reactivates the port later (timer, SRP, etc). |
| 898 | * Powerdown must be optional, because of reset/DFU. |
| 899 | */ |
| 900 | |
| 901 | set_bit(port1, hub->change_bits); |
| 902 | kick_khubd(hub); |
| 903 | } |
| 904 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 905 | /** |
| 906 | * usb_remove_device - disable a device's port on its parent hub |
| 907 | * @udev: device to be disabled and removed |
| 908 | * Context: @udev locked, must be able to sleep. |
| 909 | * |
| 910 | * After @udev's port has been disabled, khubd is notified and it will |
| 911 | * see that the device has been disconnected. When the device is |
| 912 | * physically unplugged and something is plugged in, the events will |
| 913 | * be received and processed normally. |
| 914 | */ |
| 915 | int usb_remove_device(struct usb_device *udev) |
| 916 | { |
| 917 | struct usb_hub *hub; |
| 918 | struct usb_interface *intf; |
| 919 | |
| 920 | if (!udev->parent) /* Can't remove a root hub */ |
| 921 | return -EINVAL; |
| 922 | hub = hdev_to_hub(udev->parent); |
| 923 | intf = to_usb_interface(hub->intfdev); |
| 924 | |
| 925 | usb_autopm_get_interface(intf); |
| 926 | set_bit(udev->portnum, hub->removed_bits); |
| 927 | hub_port_logical_disconnect(hub, udev->portnum); |
| 928 | usb_autopm_put_interface(intf); |
| 929 | return 0; |
| 930 | } |
| 931 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 932 | enum hub_activation_type { |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 933 | HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 934 | HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 935 | }; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 936 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 937 | static void hub_init_func2(struct work_struct *ws); |
| 938 | static void hub_init_func3(struct work_struct *ws); |
| 939 | |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 940 | static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 941 | { |
| 942 | struct usb_device *hdev = hub->hdev; |
Sarah Sharp | 653a39d | 2010-12-23 11:12:42 -0800 | [diff] [blame] | 943 | struct usb_hcd *hcd; |
| 944 | int ret; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 945 | int port1; |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 946 | int status; |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 947 | bool need_debounce_delay = false; |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 948 | unsigned delay; |
| 949 | |
| 950 | /* Continue a partial initialization */ |
| 951 | if (type == HUB_INIT2) |
| 952 | goto init2; |
| 953 | if (type == HUB_INIT3) |
| 954 | goto init3; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 955 | |
Elric Fu | a45aa3b | 2012-02-18 13:32:27 +0800 | [diff] [blame] | 956 | /* The superspeed hub except for root hub has to use Hub Depth |
| 957 | * value as an offset into the route string to locate the bits |
| 958 | * it uses to determine the downstream port number. So hub driver |
| 959 | * should send a set hub depth request to superspeed hub after |
| 960 | * the superspeed hub is set configuration in initialization or |
| 961 | * reset procedure. |
| 962 | * |
| 963 | * After a resume, port power should still be on. |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 964 | * For any other type of activation, turn it on. |
| 965 | */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 966 | if (type != HUB_RESUME) { |
Elric Fu | a45aa3b | 2012-02-18 13:32:27 +0800 | [diff] [blame] | 967 | if (hdev->parent && hub_is_superspeed(hdev)) { |
| 968 | ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 969 | HUB_SET_DEPTH, USB_RT_HUB, |
| 970 | hdev->level - 1, 0, NULL, 0, |
| 971 | USB_CTRL_SET_TIMEOUT); |
| 972 | if (ret < 0) |
| 973 | dev_err(hub->intfdev, |
| 974 | "set hub depth failed\n"); |
| 975 | } |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 976 | |
| 977 | /* Speed up system boot by using a delayed_work for the |
| 978 | * hub's initial power-up delays. This is pretty awkward |
| 979 | * and the implementation looks like a home-brewed sort of |
| 980 | * setjmp/longjmp, but it saves at least 100 ms for each |
| 981 | * root hub (assuming usbcore is compiled into the kernel |
| 982 | * rather than as a module). It adds up. |
| 983 | * |
| 984 | * This can't be done for HUB_RESUME or HUB_RESET_RESUME |
| 985 | * because for those activation types the ports have to be |
| 986 | * operational when we return. In theory this could be done |
| 987 | * for HUB_POST_RESET, but it's easier not to. |
| 988 | */ |
| 989 | if (type == HUB_INIT) { |
| 990 | delay = hub_power_on(hub, false); |
| 991 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); |
| 992 | schedule_delayed_work(&hub->init_work, |
| 993 | msecs_to_jiffies(delay)); |
Alan Stern | 61fbeba | 2008-10-27 12:07:44 -0400 | [diff] [blame] | 994 | |
| 995 | /* Suppress autosuspend until init is done */ |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 996 | usb_autopm_get_interface_no_resume( |
| 997 | to_usb_interface(hub->intfdev)); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 998 | return; /* Continues at init2: below */ |
Sarah Sharp | 653a39d | 2010-12-23 11:12:42 -0800 | [diff] [blame] | 999 | } else if (type == HUB_RESET_RESUME) { |
| 1000 | /* The internal host controller state for the hub device |
| 1001 | * may be gone after a host power loss on system resume. |
| 1002 | * Update the device's info so the HW knows it's a hub. |
| 1003 | */ |
| 1004 | hcd = bus_to_hcd(hdev->bus); |
| 1005 | if (hcd->driver->update_hub_device) { |
| 1006 | ret = hcd->driver->update_hub_device(hcd, hdev, |
| 1007 | &hub->tt, GFP_NOIO); |
| 1008 | if (ret < 0) { |
| 1009 | dev_err(hub->intfdev, "Host not " |
| 1010 | "accepting hub info " |
| 1011 | "update.\n"); |
| 1012 | dev_err(hub->intfdev, "LS/FS devices " |
| 1013 | "and hubs may not work " |
| 1014 | "under this hub\n."); |
| 1015 | } |
| 1016 | } |
| 1017 | hub_power_on(hub, true); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1018 | } else { |
| 1019 | hub_power_on(hub, true); |
| 1020 | } |
| 1021 | } |
| 1022 | init2: |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1023 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1024 | /* Check each port and set hub->change_bits to let khubd know |
| 1025 | * which ports need attention. |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1026 | */ |
| 1027 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1028 | struct usb_device *udev = hdev->children[port1-1]; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1029 | u16 portstatus, portchange; |
| 1030 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1031 | portstatus = portchange = 0; |
| 1032 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
| 1033 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) |
| 1034 | dev_dbg(hub->intfdev, |
| 1035 | "port %d: status %04x change %04x\n", |
| 1036 | port1, portstatus, portchange); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1037 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1038 | /* After anything other than HUB_RESUME (i.e., initialization |
| 1039 | * or any sort of reset), every port should be disabled. |
| 1040 | * Unconnected ports should likewise be disabled (paranoia), |
| 1041 | * and so should ports for which we have no usb_device. |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1042 | */ |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1043 | if ((portstatus & USB_PORT_STAT_ENABLE) && ( |
| 1044 | type != HUB_RESUME || |
| 1045 | !(portstatus & USB_PORT_STAT_CONNECTION) || |
| 1046 | !udev || |
| 1047 | udev->state == USB_STATE_NOTATTACHED)) { |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1048 | /* |
| 1049 | * USB3 protocol ports will automatically transition |
| 1050 | * to Enabled state when detect an USB3.0 device attach. |
| 1051 | * Do not disable USB3 protocol ports. |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1052 | */ |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 1053 | if (!hub_is_superspeed(hdev)) { |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1054 | clear_port_feature(hdev, port1, |
| 1055 | USB_PORT_FEAT_ENABLE); |
| 1056 | portstatus &= ~USB_PORT_STAT_ENABLE; |
Sarah Sharp | 85f0ff4 | 2010-10-14 07:22:54 -0700 | [diff] [blame] | 1057 | } else { |
| 1058 | /* Pretend that power was lost for USB3 devs */ |
| 1059 | portstatus &= ~USB_PORT_STAT_ENABLE; |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1060 | } |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1063 | /* Clear status-change flags; we'll debounce later */ |
| 1064 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
| 1065 | need_debounce_delay = true; |
| 1066 | clear_port_feature(hub->hdev, port1, |
| 1067 | USB_PORT_FEAT_C_CONNECTION); |
| 1068 | } |
| 1069 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
| 1070 | need_debounce_delay = true; |
| 1071 | clear_port_feature(hub->hdev, port1, |
| 1072 | USB_PORT_FEAT_C_ENABLE); |
| 1073 | } |
Don Zickus | 79c3dd8 | 2011-11-03 09:07:18 -0400 | [diff] [blame] | 1074 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
| 1075 | hub_is_superspeed(hub->hdev)) { |
| 1076 | need_debounce_delay = true; |
| 1077 | clear_port_feature(hub->hdev, port1, |
| 1078 | USB_PORT_FEAT_C_BH_PORT_RESET); |
| 1079 | } |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 1080 | /* We can forget about a "removed" device when there's a |
| 1081 | * physical disconnect or the connect status changes. |
| 1082 | */ |
| 1083 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 1084 | (portchange & USB_PORT_STAT_C_CONNECTION)) |
| 1085 | clear_bit(port1, hub->removed_bits); |
| 1086 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1087 | if (!udev || udev->state == USB_STATE_NOTATTACHED) { |
| 1088 | /* Tell khubd to disconnect the device or |
| 1089 | * check for a new connection |
| 1090 | */ |
| 1091 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) |
| 1092 | set_bit(port1, hub->change_bits); |
| 1093 | |
| 1094 | } else if (portstatus & USB_PORT_STAT_ENABLE) { |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1095 | bool port_resumed = (portstatus & |
| 1096 | USB_PORT_STAT_LINK_STATE) == |
| 1097 | USB_SS_PORT_LS_U0; |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1098 | /* The power session apparently survived the resume. |
| 1099 | * If there was an overcurrent or suspend change |
| 1100 | * (i.e., remote wakeup request), have khubd |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1101 | * take care of it. Look at the port link state |
| 1102 | * for USB 3.0 hubs, since they don't have a suspend |
| 1103 | * change bit, and they don't set the port link change |
| 1104 | * bit on device-initiated resume. |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1105 | */ |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1106 | if (portchange || (hub_is_superspeed(hub->hdev) && |
| 1107 | port_resumed)) |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1108 | set_bit(port1, hub->change_bits); |
| 1109 | |
| 1110 | } else if (udev->persist_enabled) { |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1111 | #ifdef CONFIG_PM |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1112 | udev->reset_resume = 1; |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1113 | #endif |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 1114 | set_bit(port1, hub->change_bits); |
| 1115 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1116 | } else { |
| 1117 | /* The power session is gone; tell khubd */ |
| 1118 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
| 1119 | set_bit(port1, hub->change_bits); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1120 | } |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1121 | } |
| 1122 | |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1123 | /* If no port-status-change flags were set, we don't need any |
| 1124 | * debouncing. If flags were set we can try to debounce the |
| 1125 | * ports all at once right now, instead of letting khubd do them |
| 1126 | * one at a time later on. |
| 1127 | * |
| 1128 | * If any port-status changes do occur during this delay, khubd |
| 1129 | * will see them later and handle them normally. |
| 1130 | */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1131 | if (need_debounce_delay) { |
| 1132 | delay = HUB_DEBOUNCE_STABLE; |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1133 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1134 | /* Don't do a long sleep inside a workqueue routine */ |
| 1135 | if (type == HUB_INIT2) { |
| 1136 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); |
| 1137 | schedule_delayed_work(&hub->init_work, |
| 1138 | msecs_to_jiffies(delay)); |
| 1139 | return; /* Continues at init3: below */ |
| 1140 | } else { |
| 1141 | msleep(delay); |
| 1142 | } |
| 1143 | } |
| 1144 | init3: |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1145 | hub->quiescing = 0; |
| 1146 | |
| 1147 | status = usb_submit_urb(hub->urb, GFP_NOIO); |
| 1148 | if (status < 0) |
| 1149 | dev_err(hub->intfdev, "activate --> %d\n", status); |
| 1150 | if (hub->has_indicators && blinkenlights) |
| 1151 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); |
| 1152 | |
| 1153 | /* Scan all ports that need attention */ |
| 1154 | kick_khubd(hub); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1155 | |
| 1156 | /* Allow autosuspend if it was suppressed */ |
| 1157 | if (type <= HUB_INIT3) |
| 1158 | usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1161 | /* Implement the continuations for the delays above */ |
| 1162 | static void hub_init_func2(struct work_struct *ws) |
| 1163 | { |
| 1164 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); |
| 1165 | |
| 1166 | hub_activate(hub, HUB_INIT2); |
| 1167 | } |
| 1168 | |
| 1169 | static void hub_init_func3(struct work_struct *ws) |
| 1170 | { |
| 1171 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); |
| 1172 | |
| 1173 | hub_activate(hub, HUB_INIT3); |
| 1174 | } |
| 1175 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1176 | enum hub_quiescing_type { |
| 1177 | HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND |
| 1178 | }; |
| 1179 | |
| 1180 | static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) |
| 1181 | { |
| 1182 | struct usb_device *hdev = hub->hdev; |
| 1183 | int i; |
| 1184 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1185 | cancel_delayed_work_sync(&hub->init_work); |
| 1186 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1187 | /* khubd and related activity won't re-trigger */ |
| 1188 | hub->quiescing = 1; |
| 1189 | |
| 1190 | if (type != HUB_SUSPEND) { |
| 1191 | /* Disconnect all the children */ |
| 1192 | for (i = 0; i < hdev->maxchild; ++i) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1193 | if (hdev->children[i]) |
| 1194 | usb_disconnect(&hdev->children[i]); |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* Stop khubd and related activity */ |
| 1199 | usb_kill_urb(hub->urb); |
| 1200 | if (hub->has_indicators) |
| 1201 | cancel_delayed_work_sync(&hub->leds); |
| 1202 | if (hub->tt.hub) |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 1203 | cancel_work_sync(&hub->tt.clear_work); |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1204 | } |
| 1205 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 1206 | /* caller has locked the hub device */ |
| 1207 | static int hub_pre_reset(struct usb_interface *intf) |
| 1208 | { |
| 1209 | struct usb_hub *hub = usb_get_intfdata(intf); |
| 1210 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1211 | hub_quiesce(hub, HUB_PRE_RESET); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1212 | return 0; |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /* caller has locked the hub device */ |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1216 | static int hub_post_reset(struct usb_interface *intf) |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1217 | { |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1218 | struct usb_hub *hub = usb_get_intfdata(intf); |
| 1219 | |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1220 | hub_activate(hub, HUB_POST_RESET); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1221 | return 0; |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1222 | } |
| 1223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | static int hub_configure(struct usb_hub *hub, |
| 1225 | struct usb_endpoint_descriptor *endpoint) |
| 1226 | { |
Sarah Sharp | b356b7c | 2009-09-04 10:53:24 -0700 | [diff] [blame] | 1227 | struct usb_hcd *hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | struct usb_device *hdev = hub->hdev; |
| 1229 | struct device *hub_dev = hub->intfdev; |
| 1230 | u16 hubstatus, hubchange; |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1231 | u16 wHubCharacteristics; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | unsigned int pipe; |
| 1233 | int maxp, ret; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1234 | char *message = "out of memory"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Alan Stern | d697cdd | 2009-10-27 15:18:46 -0400 | [diff] [blame] | 1236 | hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | if (!hub->buffer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | ret = -ENOMEM; |
| 1239 | goto fail; |
| 1240 | } |
| 1241 | |
| 1242 | hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); |
| 1243 | if (!hub->status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | ret = -ENOMEM; |
| 1245 | goto fail; |
| 1246 | } |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 1247 | mutex_init(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); |
| 1250 | if (!hub->descriptor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | ret = -ENOMEM; |
| 1252 | goto fail; |
| 1253 | } |
| 1254 | |
| 1255 | /* Request the entire hub descriptor. |
| 1256 | * hub->descriptor can handle USB_MAXCHILDREN ports, |
| 1257 | * but the hub can/will return fewer bytes here. |
| 1258 | */ |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1259 | ret = get_hub_descriptor(hdev, hub->descriptor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | if (ret < 0) { |
| 1261 | message = "can't read hub descriptor"; |
| 1262 | goto fail; |
| 1263 | } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { |
| 1264 | message = "hub has too many ports!"; |
| 1265 | ret = -ENODEV; |
| 1266 | goto fail; |
| 1267 | } |
| 1268 | |
| 1269 | hdev->maxchild = hub->descriptor->bNbrPorts; |
| 1270 | dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, |
| 1271 | (hdev->maxchild == 1) ? "" : "s"); |
| 1272 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1273 | hdev->children = kzalloc(hdev->maxchild * |
| 1274 | sizeof(struct usb_device *), GFP_KERNEL); |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1275 | hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL); |
| 1276 | if (!hdev->children || !hub->port_owners) { |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1277 | ret = -ENOMEM; |
| 1278 | goto fail; |
| 1279 | } |
| 1280 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1281 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1283 | /* FIXME for USB 3.0, skip for now */ |
| 1284 | if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && |
| 1285 | !(hub_is_superspeed(hdev))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | int i; |
| 1287 | char portstr [USB_MAXCHILDREN + 1]; |
| 1288 | |
| 1289 | for (i = 0; i < hdev->maxchild; i++) |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1290 | portstr[i] = hub->descriptor->u.hs.DeviceRemovable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | [((i + 1) / 8)] & (1 << ((i + 1) % 8)) |
| 1292 | ? 'F' : 'R'; |
| 1293 | portstr[hdev->maxchild] = 0; |
| 1294 | dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); |
| 1295 | } else |
| 1296 | dev_dbg(hub_dev, "standalone hub\n"); |
| 1297 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1298 | switch (wHubCharacteristics & HUB_CHAR_LPSM) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1299 | case HUB_CHAR_COMMON_LPSM: |
| 1300 | dev_dbg(hub_dev, "ganged power switching\n"); |
| 1301 | break; |
| 1302 | case HUB_CHAR_INDV_PORT_LPSM: |
| 1303 | dev_dbg(hub_dev, "individual port power switching\n"); |
| 1304 | break; |
| 1305 | case HUB_CHAR_NO_LPSM: |
| 1306 | case HUB_CHAR_LPSM: |
| 1307 | dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); |
| 1308 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1311 | switch (wHubCharacteristics & HUB_CHAR_OCPM) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1312 | case HUB_CHAR_COMMON_OCPM: |
| 1313 | dev_dbg(hub_dev, "global over-current protection\n"); |
| 1314 | break; |
| 1315 | case HUB_CHAR_INDV_PORT_OCPM: |
| 1316 | dev_dbg(hub_dev, "individual port over-current protection\n"); |
| 1317 | break; |
| 1318 | case HUB_CHAR_NO_OCPM: |
| 1319 | case HUB_CHAR_OCPM: |
| 1320 | dev_dbg(hub_dev, "no over-current protection\n"); |
| 1321 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | spin_lock_init (&hub->tt.lock); |
| 1325 | INIT_LIST_HEAD (&hub->tt.clear_list); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 1326 | INIT_WORK(&hub->tt.clear_work, hub_tt_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | switch (hdev->descriptor.bDeviceProtocol) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1328 | case USB_HUB_PR_FS: |
| 1329 | break; |
| 1330 | case USB_HUB_PR_HS_SINGLE_TT: |
| 1331 | dev_dbg(hub_dev, "Single TT\n"); |
| 1332 | hub->tt.hub = hdev; |
| 1333 | break; |
| 1334 | case USB_HUB_PR_HS_MULTI_TT: |
| 1335 | ret = usb_set_interface(hdev, 0, 1); |
| 1336 | if (ret == 0) { |
| 1337 | dev_dbg(hub_dev, "TT per port\n"); |
| 1338 | hub->tt.multi = 1; |
| 1339 | } else |
| 1340 | dev_err(hub_dev, "Using single TT (err %d)\n", |
| 1341 | ret); |
| 1342 | hub->tt.hub = hdev; |
| 1343 | break; |
| 1344 | case USB_HUB_PR_SS: |
| 1345 | /* USB 3.0 hubs don't have a TT */ |
| 1346 | break; |
| 1347 | default: |
| 1348 | dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", |
| 1349 | hdev->descriptor.bDeviceProtocol); |
| 1350 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1353 | /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1354 | switch (wHubCharacteristics & HUB_CHAR_TTTT) { |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1355 | case HUB_TTTT_8_BITS: |
| 1356 | if (hdev->descriptor.bDeviceProtocol != 0) { |
| 1357 | hub->tt.think_time = 666; |
| 1358 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1359 | "FS bit times (%d ns)\n", |
| 1360 | 8, hub->tt.think_time); |
| 1361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | break; |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1363 | case HUB_TTTT_16_BITS: |
| 1364 | hub->tt.think_time = 666 * 2; |
| 1365 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1366 | "FS bit times (%d ns)\n", |
| 1367 | 16, hub->tt.think_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | break; |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1369 | case HUB_TTTT_24_BITS: |
| 1370 | hub->tt.think_time = 666 * 3; |
| 1371 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1372 | "FS bit times (%d ns)\n", |
| 1373 | 24, hub->tt.think_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | break; |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1375 | case HUB_TTTT_32_BITS: |
| 1376 | hub->tt.think_time = 666 * 4; |
| 1377 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1378 | "FS bit times (%d ns)\n", |
| 1379 | 32, hub->tt.think_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | /* probe() zeroes hub->indicator[] */ |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1384 | if (wHubCharacteristics & HUB_CHAR_PORTIND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | hub->has_indicators = 1; |
| 1386 | dev_dbg(hub_dev, "Port indicators are supported\n"); |
| 1387 | } |
| 1388 | |
| 1389 | dev_dbg(hub_dev, "power on to power good time: %dms\n", |
| 1390 | hub->descriptor->bPwrOn2PwrGood * 2); |
| 1391 | |
| 1392 | /* power budgeting mostly matters with bus-powered hubs, |
| 1393 | * and battery-powered root hubs (may provide just 8 mA). |
| 1394 | */ |
| 1395 | ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1396 | if (ret < 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | message = "can't get hub status"; |
| 1398 | goto fail; |
| 1399 | } |
Alan Stern | 7d35b92 | 2005-04-25 11:18:32 -0400 | [diff] [blame] | 1400 | le16_to_cpus(&hubstatus); |
| 1401 | if (hdev == hdev->bus->root_hub) { |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1402 | if (hdev->bus_mA == 0 || hdev->bus_mA >= 500) |
| 1403 | hub->mA_per_port = 500; |
| 1404 | else { |
| 1405 | hub->mA_per_port = hdev->bus_mA; |
| 1406 | hub->limited_power = 1; |
| 1407 | } |
Alan Stern | 7d35b92 | 2005-04-25 11:18:32 -0400 | [diff] [blame] | 1408 | } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", |
| 1410 | hub->descriptor->bHubContrCurrent); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1411 | hub->limited_power = 1; |
| 1412 | if (hdev->maxchild > 0) { |
| 1413 | int remaining = hdev->bus_mA - |
| 1414 | hub->descriptor->bHubContrCurrent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1416 | if (remaining < hdev->maxchild * 100) |
| 1417 | dev_warn(hub_dev, |
| 1418 | "insufficient power available " |
| 1419 | "to use all downstream ports\n"); |
| 1420 | hub->mA_per_port = 100; /* 7.2.1.1 */ |
| 1421 | } |
| 1422 | } else { /* Self-powered external hub */ |
| 1423 | /* FIXME: What about battery-powered external hubs that |
| 1424 | * provide less current per port? */ |
| 1425 | hub->mA_per_port = 500; |
| 1426 | } |
| 1427 | if (hub->mA_per_port < 500) |
| 1428 | dev_dbg(hub_dev, "%umA bus power budget for each child\n", |
| 1429 | hub->mA_per_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
Sarah Sharp | b356b7c | 2009-09-04 10:53:24 -0700 | [diff] [blame] | 1431 | /* Update the HCD's internal representation of this hub before khubd |
| 1432 | * starts getting port status changes for devices under the hub. |
| 1433 | */ |
| 1434 | hcd = bus_to_hcd(hdev->bus); |
| 1435 | if (hcd->driver->update_hub_device) { |
| 1436 | ret = hcd->driver->update_hub_device(hcd, hdev, |
| 1437 | &hub->tt, GFP_KERNEL); |
| 1438 | if (ret < 0) { |
| 1439 | message = "can't update HCD hub info"; |
| 1440 | goto fail; |
| 1441 | } |
| 1442 | } |
| 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | ret = hub_hub_status(hub, &hubstatus, &hubchange); |
| 1445 | if (ret < 0) { |
| 1446 | message = "can't get hub status"; |
| 1447 | goto fail; |
| 1448 | } |
| 1449 | |
| 1450 | /* local power status reports aren't always correct */ |
| 1451 | if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) |
| 1452 | dev_dbg(hub_dev, "local power source is %s\n", |
| 1453 | (hubstatus & HUB_STATUS_LOCAL_POWER) |
| 1454 | ? "lost (inactive)" : "good"); |
| 1455 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1456 | if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | dev_dbg(hub_dev, "%sover-current condition exists\n", |
| 1458 | (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); |
| 1459 | |
inaky@linux.intel.com | 88fafff | 2006-10-11 20:05:59 -0700 | [diff] [blame] | 1460 | /* set up the interrupt endpoint |
| 1461 | * We use the EP's maxpacket size instead of (PORTS+1+7)/8 |
| 1462 | * bytes as USB2.0[11.12.3] says because some hubs are known |
| 1463 | * to send more data (and thus cause overflow). For root hubs, |
| 1464 | * maxpktsize is defined in hcd.c's fake endpoint descriptors |
| 1465 | * to be big enough for at least USB_MAXCHILDREN ports. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); |
| 1467 | maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); |
| 1468 | |
| 1469 | if (maxp > sizeof(*hub->buffer)) |
| 1470 | maxp = sizeof(*hub->buffer); |
| 1471 | |
| 1472 | hub->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1473 | if (!hub->urb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | ret = -ENOMEM; |
| 1475 | goto fail; |
| 1476 | } |
| 1477 | |
| 1478 | usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, |
| 1479 | hub, endpoint->bInterval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
| 1481 | /* maybe cycle the hub leds */ |
| 1482 | if (hub->has_indicators && blinkenlights) |
| 1483 | hub->indicator [0] = INDICATOR_CYCLE; |
| 1484 | |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1485 | hub_activate(hub, HUB_INIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return 0; |
| 1487 | |
| 1488 | fail: |
| 1489 | dev_err (hub_dev, "config failed, %s (err %d)\n", |
| 1490 | message, ret); |
| 1491 | /* hub_disconnect() frees urb and descriptor */ |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1495 | static void hub_release(struct kref *kref) |
| 1496 | { |
| 1497 | struct usb_hub *hub = container_of(kref, struct usb_hub, kref); |
| 1498 | |
| 1499 | usb_put_intf(to_usb_interface(hub->intfdev)); |
| 1500 | kfree(hub); |
| 1501 | } |
| 1502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | static unsigned highspeed_hubs; |
| 1504 | |
| 1505 | static void hub_disconnect(struct usb_interface *intf) |
| 1506 | { |
Huajun Li | 8816230 | 2012-03-12 21:00:19 +0800 | [diff] [blame] | 1507 | struct usb_hub *hub = usb_get_intfdata(intf); |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1508 | struct usb_device *hdev = interface_to_usbdev(intf); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1509 | |
| 1510 | /* Take the hub off the event list and don't let it be added again */ |
| 1511 | spin_lock_irq(&hub_event_lock); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1512 | if (!list_empty(&hub->event_list)) { |
| 1513 | list_del_init(&hub->event_list); |
| 1514 | usb_autopm_put_interface_no_suspend(intf); |
| 1515 | } |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1516 | hub->disconnected = 1; |
| 1517 | spin_unlock_irq(&hub_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1519 | /* Disconnect all children and quiesce the hub */ |
| 1520 | hub->error = 0; |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1521 | hub_quiesce(hub, HUB_DISCONNECT); |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1522 | |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 1523 | usb_set_intfdata (intf, NULL); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1524 | hub->hdev->maxchild = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1526 | if (hub->hdev->speed == USB_SPEED_HIGH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | highspeed_hubs--; |
| 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | usb_free_urb(hub->urb); |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1530 | kfree(hdev->children); |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1531 | kfree(hub->port_owners); |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1532 | kfree(hub->descriptor); |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1533 | kfree(hub->status); |
Alan Stern | d697cdd | 2009-10-27 15:18:46 -0400 | [diff] [blame] | 1534 | kfree(hub->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1536 | kref_put(&hub->kref, hub_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 1540 | { |
| 1541 | struct usb_host_interface *desc; |
| 1542 | struct usb_endpoint_descriptor *endpoint; |
| 1543 | struct usb_device *hdev; |
| 1544 | struct usb_hub *hub; |
| 1545 | |
| 1546 | desc = intf->cur_altsetting; |
| 1547 | hdev = interface_to_usbdev(intf); |
| 1548 | |
Sarah Sharp | 2839f5b | 2012-01-06 16:27:25 -0800 | [diff] [blame] | 1549 | /* Hubs have proper suspend/resume support. */ |
| 1550 | usb_enable_autosuspend(hdev); |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1551 | |
Felipe Balbi | 38f3ad5 | 2008-06-12 10:49:47 +0300 | [diff] [blame] | 1552 | if (hdev->level == MAX_TOPO_LEVEL) { |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 1553 | dev_err(&intf->dev, |
| 1554 | "Unsupported bus topology: hub nested too deep\n"); |
Felipe Balbi | 38f3ad5 | 2008-06-12 10:49:47 +0300 | [diff] [blame] | 1555 | return -E2BIG; |
| 1556 | } |
| 1557 | |
David Brownell | 89ccbdc | 2006-04-02 10:18:09 -0800 | [diff] [blame] | 1558 | #ifdef CONFIG_USB_OTG_BLACKLIST_HUB |
| 1559 | if (hdev->parent) { |
| 1560 | dev_warn(&intf->dev, "ignoring external hub\n"); |
| 1561 | return -ENODEV; |
| 1562 | } |
| 1563 | #endif |
| 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
| 1566 | /* specs is not defined, but it works */ |
| 1567 | if ((desc->desc.bInterfaceSubClass != 0) && |
| 1568 | (desc->desc.bInterfaceSubClass != 1)) { |
| 1569 | descriptor_error: |
| 1570 | dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); |
| 1571 | return -EIO; |
| 1572 | } |
| 1573 | |
| 1574 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
| 1575 | if (desc->desc.bNumEndpoints != 1) |
| 1576 | goto descriptor_error; |
| 1577 | |
| 1578 | endpoint = &desc->endpoint[0].desc; |
| 1579 | |
Luiz Fernando N. Capitulino | fbf81c2 | 2006-09-27 11:58:53 -0700 | [diff] [blame] | 1580 | /* If it's not an interrupt in endpoint, we'd better punt! */ |
| 1581 | if (!usb_endpoint_is_int_in(endpoint)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | goto descriptor_error; |
| 1583 | |
| 1584 | /* We found a hub */ |
| 1585 | dev_info (&intf->dev, "USB hub found\n"); |
| 1586 | |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 1587 | hub = kzalloc(sizeof(*hub), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | if (!hub) { |
| 1589 | dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); |
| 1590 | return -ENOMEM; |
| 1591 | } |
| 1592 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1593 | kref_init(&hub->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | INIT_LIST_HEAD(&hub->event_list); |
| 1595 | hub->intfdev = &intf->dev; |
| 1596 | hub->hdev = hdev; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1597 | INIT_DELAYED_WORK(&hub->leds, led_work); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1598 | INIT_DELAYED_WORK(&hub->init_work, NULL); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1599 | usb_get_intf(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
| 1601 | usb_set_intfdata (intf, hub); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 1602 | intf->needs_remote_wakeup = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | |
| 1604 | if (hdev->speed == USB_SPEED_HIGH) |
| 1605 | highspeed_hubs++; |
| 1606 | |
| 1607 | if (hub_configure(hub, endpoint) >= 0) |
| 1608 | return 0; |
| 1609 | |
| 1610 | hub_disconnect (intf); |
| 1611 | return -ENODEV; |
| 1612 | } |
| 1613 | |
| 1614 | static int |
| 1615 | hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) |
| 1616 | { |
| 1617 | struct usb_device *hdev = interface_to_usbdev (intf); |
| 1618 | |
| 1619 | /* assert ifno == 0 (part of hub spec) */ |
| 1620 | switch (code) { |
| 1621 | case USBDEVFS_HUB_PORTINFO: { |
| 1622 | struct usbdevfs_hub_portinfo *info = user_data; |
| 1623 | int i; |
| 1624 | |
| 1625 | spin_lock_irq(&device_state_lock); |
| 1626 | if (hdev->devnum <= 0) |
| 1627 | info->nports = 0; |
| 1628 | else { |
| 1629 | info->nports = hdev->maxchild; |
| 1630 | for (i = 0; i < info->nports; i++) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1631 | if (hdev->children[i] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | info->port[i] = 0; |
| 1633 | else |
| 1634 | info->port[i] = |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1635 | hdev->children[i]->devnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | spin_unlock_irq(&device_state_lock); |
| 1639 | |
| 1640 | return info->nports + 1; |
| 1641 | } |
| 1642 | |
| 1643 | default: |
| 1644 | return -ENOSYS; |
| 1645 | } |
| 1646 | } |
| 1647 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1648 | /* |
| 1649 | * Allow user programs to claim ports on a hub. When a device is attached |
| 1650 | * to one of these "claimed" ports, the program will "own" the device. |
| 1651 | */ |
| 1652 | static int find_port_owner(struct usb_device *hdev, unsigned port1, |
| 1653 | void ***ppowner) |
| 1654 | { |
| 1655 | if (hdev->state == USB_STATE_NOTATTACHED) |
| 1656 | return -ENODEV; |
| 1657 | if (port1 == 0 || port1 > hdev->maxchild) |
| 1658 | return -EINVAL; |
| 1659 | |
| 1660 | /* This assumes that devices not managed by the hub driver |
| 1661 | * will always have maxchild equal to 0. |
| 1662 | */ |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1663 | *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* In the following three functions, the caller must hold hdev's lock */ |
| 1668 | int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner) |
| 1669 | { |
| 1670 | int rc; |
| 1671 | void **powner; |
| 1672 | |
| 1673 | rc = find_port_owner(hdev, port1, &powner); |
| 1674 | if (rc) |
| 1675 | return rc; |
| 1676 | if (*powner) |
| 1677 | return -EBUSY; |
| 1678 | *powner = owner; |
| 1679 | return rc; |
| 1680 | } |
| 1681 | |
| 1682 | int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner) |
| 1683 | { |
| 1684 | int rc; |
| 1685 | void **powner; |
| 1686 | |
| 1687 | rc = find_port_owner(hdev, port1, &powner); |
| 1688 | if (rc) |
| 1689 | return rc; |
| 1690 | if (*powner != owner) |
| 1691 | return -ENOENT; |
| 1692 | *powner = NULL; |
| 1693 | return rc; |
| 1694 | } |
| 1695 | |
| 1696 | void usb_hub_release_all_ports(struct usb_device *hdev, void *owner) |
| 1697 | { |
| 1698 | int n; |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1699 | void **powner; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1700 | |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1701 | n = find_port_owner(hdev, 1, &powner); |
| 1702 | if (n == 0) { |
| 1703 | for (; n < hdev->maxchild; (++n, ++powner)) { |
| 1704 | if (*powner == owner) |
| 1705 | *powner = NULL; |
| 1706 | } |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /* The caller must hold udev's lock */ |
| 1711 | bool usb_device_is_owned(struct usb_device *udev) |
| 1712 | { |
| 1713 | struct usb_hub *hub; |
| 1714 | |
| 1715 | if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) |
| 1716 | return false; |
| 1717 | hub = hdev_to_hub(udev->parent); |
Greg Kroah-Hartman | 304f0b2 | 2012-05-14 09:22:58 -0700 | [diff] [blame] | 1718 | return !!hub->port_owners[udev->portnum - 1]; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1719 | } |
| 1720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | static void recursively_mark_NOTATTACHED(struct usb_device *udev) |
| 1723 | { |
| 1724 | int i; |
| 1725 | |
| 1726 | for (i = 0; i < udev->maxchild; ++i) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1727 | if (udev->children[i]) |
| 1728 | recursively_mark_NOTATTACHED(udev->children[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | } |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1730 | if (udev->state == USB_STATE_SUSPENDED) |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 1731 | udev->active_duration -= jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | udev->state = USB_STATE_NOTATTACHED; |
| 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * usb_set_device_state - change a device's current state (usbcore, hcds) |
| 1737 | * @udev: pointer to device whose state should be changed |
| 1738 | * @new_state: new state value to be stored |
| 1739 | * |
| 1740 | * udev->state is _not_ fully protected by the device lock. Although |
| 1741 | * most transitions are made only while holding the lock, the state can |
| 1742 | * can change to USB_STATE_NOTATTACHED at almost any time. This |
| 1743 | * is so that devices can be marked as disconnected as soon as possible, |
| 1744 | * without having to wait for any semaphores to be released. As a result, |
| 1745 | * all changes to any device's state must be protected by the |
| 1746 | * device_state_lock spinlock. |
| 1747 | * |
| 1748 | * Once a device has been added to the device tree, all changes to its state |
| 1749 | * should be made using this routine. The state should _not_ be set directly. |
| 1750 | * |
| 1751 | * If udev->state is already USB_STATE_NOTATTACHED then no change is made. |
| 1752 | * Otherwise udev->state is set to new_state, and if new_state is |
| 1753 | * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set |
| 1754 | * to USB_STATE_NOTATTACHED. |
| 1755 | */ |
| 1756 | void usb_set_device_state(struct usb_device *udev, |
| 1757 | enum usb_device_state new_state) |
| 1758 | { |
| 1759 | unsigned long flags; |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1760 | int wakeup = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
| 1762 | spin_lock_irqsave(&device_state_lock, flags); |
| 1763 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1764 | ; /* do nothing */ |
David Brownell | b94dc6b | 2005-09-12 19:39:39 -0700 | [diff] [blame] | 1765 | else if (new_state != USB_STATE_NOTATTACHED) { |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 1766 | |
| 1767 | /* root hub wakeup capabilities are managed out-of-band |
| 1768 | * and may involve silicon errata ... ignore them here. |
| 1769 | */ |
| 1770 | if (udev->parent) { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1771 | if (udev->state == USB_STATE_SUSPENDED |
| 1772 | || new_state == USB_STATE_SUSPENDED) |
| 1773 | ; /* No change to wakeup settings */ |
| 1774 | else if (new_state == USB_STATE_CONFIGURED) |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1775 | wakeup = udev->actconfig->desc.bmAttributes |
| 1776 | & USB_CONFIG_ATT_WAKEUP; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1777 | else |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1778 | wakeup = 0; |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 1779 | } |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 1780 | if (udev->state == USB_STATE_SUSPENDED && |
| 1781 | new_state != USB_STATE_SUSPENDED) |
| 1782 | udev->active_duration -= jiffies; |
| 1783 | else if (new_state == USB_STATE_SUSPENDED && |
| 1784 | udev->state != USB_STATE_SUSPENDED) |
| 1785 | udev->active_duration += jiffies; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1786 | udev->state = new_state; |
David Brownell | b94dc6b | 2005-09-12 19:39:39 -0700 | [diff] [blame] | 1787 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | recursively_mark_NOTATTACHED(udev); |
| 1789 | spin_unlock_irqrestore(&device_state_lock, flags); |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1790 | if (wakeup >= 0) |
| 1791 | device_set_wakeup_capable(&udev->dev, wakeup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | } |
David Vrabel | 6da9c99 | 2009-02-18 14:43:47 +0000 | [diff] [blame] | 1793 | EXPORT_SYMBOL_GPL(usb_set_device_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 1795 | /* |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1796 | * Choose a device number. |
| 1797 | * |
| 1798 | * Device numbers are used as filenames in usbfs. On USB-1.1 and |
| 1799 | * USB-2.0 buses they are also used as device addresses, however on |
| 1800 | * USB-3.0 buses the address is assigned by the controller hardware |
| 1801 | * and it usually is not the same as the device number. |
| 1802 | * |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 1803 | * WUSB devices are simple: they have no hubs behind, so the mapping |
| 1804 | * device <-> virtual port number becomes 1:1. Why? to simplify the |
| 1805 | * life of the device connection logic in |
| 1806 | * drivers/usb/wusbcore/devconnect.c. When we do the initial secret |
| 1807 | * handshake we need to assign a temporary address in the unauthorized |
| 1808 | * space. For simplicity we use the first virtual port number found to |
| 1809 | * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] |
| 1810 | * and that becomes it's address [X < 128] or its unauthorized address |
| 1811 | * [X | 0x80]. |
| 1812 | * |
| 1813 | * We add 1 as an offset to the one-based USB-stack port number |
| 1814 | * (zero-based wusb virtual port index) for two reasons: (a) dev addr |
| 1815 | * 0 is reserved by USB for default address; (b) Linux's USB stack |
| 1816 | * uses always #1 for the root hub of the controller. So USB stack's |
| 1817 | * port #1, which is wusb virtual-port #0 has address #2. |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 1818 | * |
| 1819 | * Devices connected under xHCI are not as simple. The host controller |
| 1820 | * supports virtualization, so the hardware assigns device addresses and |
| 1821 | * the HCD must setup data structures before issuing a set address |
| 1822 | * command to the hardware. |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 1823 | */ |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1824 | static void choose_devnum(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | { |
| 1826 | int devnum; |
| 1827 | struct usb_bus *bus = udev->bus; |
| 1828 | |
| 1829 | /* If khubd ever becomes multithreaded, this will need a lock */ |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 1830 | if (udev->wusb) { |
| 1831 | devnum = udev->portnum + 1; |
| 1832 | BUG_ON(test_bit(devnum, bus->devmap.devicemap)); |
| 1833 | } else { |
| 1834 | /* Try to allocate the next devnum beginning at |
| 1835 | * bus->devnum_next. */ |
| 1836 | devnum = find_next_zero_bit(bus->devmap.devicemap, 128, |
| 1837 | bus->devnum_next); |
| 1838 | if (devnum >= 128) |
| 1839 | devnum = find_next_zero_bit(bus->devmap.devicemap, |
| 1840 | 128, 1); |
| 1841 | bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); |
| 1842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | if (devnum < 128) { |
| 1844 | set_bit(devnum, bus->devmap.devicemap); |
| 1845 | udev->devnum = devnum; |
| 1846 | } |
| 1847 | } |
| 1848 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1849 | static void release_devnum(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | { |
| 1851 | if (udev->devnum > 0) { |
| 1852 | clear_bit(udev->devnum, udev->bus->devmap.devicemap); |
| 1853 | udev->devnum = -1; |
| 1854 | } |
| 1855 | } |
| 1856 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1857 | static void update_devnum(struct usb_device *udev, int devnum) |
David Vrabel | 4953d14 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 1858 | { |
| 1859 | /* The address for a WUSB device is managed by wusbcore. */ |
| 1860 | if (!udev->wusb) |
| 1861 | udev->devnum = devnum; |
| 1862 | } |
| 1863 | |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 1864 | static void hub_free_dev(struct usb_device *udev) |
| 1865 | { |
| 1866 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1867 | |
| 1868 | /* Root hubs aren't real devices, so don't free HCD resources */ |
| 1869 | if (hcd->driver->free_dev && udev->parent) |
| 1870 | hcd->driver->free_dev(hcd, udev); |
| 1871 | } |
| 1872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | /** |
| 1874 | * usb_disconnect - disconnect a device (usbcore-internal) |
| 1875 | * @pdev: pointer to device being disconnected |
| 1876 | * Context: !in_interrupt () |
| 1877 | * |
| 1878 | * Something got disconnected. Get rid of it and all of its children. |
| 1879 | * |
| 1880 | * If *pdev is a normal device then the parent hub must already be locked. |
| 1881 | * If *pdev is a root hub then this routine will acquire the |
| 1882 | * usb_bus_list_lock on behalf of the caller. |
| 1883 | * |
| 1884 | * Only hub drivers (including virtual root hub drivers for host |
| 1885 | * controllers) should ever call this. |
| 1886 | * |
| 1887 | * This call is synchronous, and may not be used in an interrupt context. |
| 1888 | */ |
| 1889 | void usb_disconnect(struct usb_device **pdev) |
| 1890 | { |
| 1891 | struct usb_device *udev = *pdev; |
| 1892 | int i; |
| 1893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | /* mark the device as inactive, so any further urb submissions for |
| 1895 | * this device (and any of its children) will fail immediately. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1896 | * this quiesces everything except pending urbs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | */ |
| 1898 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1899 | dev_info(&udev->dev, "USB disconnect, device number %d\n", |
| 1900 | udev->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 1902 | usb_lock_device(udev); |
| 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | /* Free up all the children before we remove this device */ |
Huajun Li | 8816230 | 2012-03-12 21:00:19 +0800 | [diff] [blame] | 1905 | for (i = 0; i < udev->maxchild; i++) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1906 | if (udev->children[i]) |
| 1907 | usb_disconnect(&udev->children[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /* deallocate hcd/hardware state ... nuking all pending urbs and |
| 1911 | * cleaning up all state associated with the current configuration |
| 1912 | * so that the hardware is now fully quiesced. |
| 1913 | */ |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1914 | dev_dbg (&udev->dev, "unregistering device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | usb_disable_device(udev, 0); |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 1916 | usb_hcd_synchronize_unlinks(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 1918 | usb_remove_ep_devs(&udev->ep0); |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1919 | usb_unlock_device(udev); |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1920 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1921 | /* Unregister the device. The device driver is responsible |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 1922 | * for de-configuring the device and invoking the remove-device |
| 1923 | * notifier chain (used by usbfs and possibly others). |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1924 | */ |
| 1925 | device_del(&udev->dev); |
| 1926 | |
| 1927 | /* Free the device number and delete the parent's children[] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | * (or root_hub) pointer. |
| 1929 | */ |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 1930 | release_devnum(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
| 1932 | /* Avoid races with recursively_mark_NOTATTACHED() */ |
| 1933 | spin_lock_irq(&device_state_lock); |
| 1934 | *pdev = NULL; |
| 1935 | spin_unlock_irq(&device_state_lock); |
| 1936 | |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 1937 | hub_free_dev(udev); |
| 1938 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1939 | put_device(&udev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1942 | #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | static void show_string(struct usb_device *udev, char *id, char *string) |
| 1944 | { |
| 1945 | if (!string) |
| 1946 | return; |
| 1947 | dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); |
| 1948 | } |
| 1949 | |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1950 | static void announce_device(struct usb_device *udev) |
| 1951 | { |
| 1952 | dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", |
| 1953 | le16_to_cpu(udev->descriptor.idVendor), |
| 1954 | le16_to_cpu(udev->descriptor.idProduct)); |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 1955 | dev_info(&udev->dev, |
| 1956 | "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1957 | udev->descriptor.iManufacturer, |
| 1958 | udev->descriptor.iProduct, |
| 1959 | udev->descriptor.iSerialNumber); |
| 1960 | show_string(udev, "Product", udev->product); |
| 1961 | show_string(udev, "Manufacturer", udev->manufacturer); |
| 1962 | show_string(udev, "SerialNumber", udev->serial); |
| 1963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | #else |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 1965 | static inline void announce_device(struct usb_device *udev) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | #endif |
| 1967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | #ifdef CONFIG_USB_OTG |
| 1969 | #include "otg_whitelist.h" |
| 1970 | #endif |
| 1971 | |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 1972 | /** |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 1973 | * usb_enumerate_device_otg - FIXME (usbcore-internal) |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 1974 | * @udev: newly addressed device (in ADDRESS state) |
| 1975 | * |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 1976 | * Finish enumeration for On-The-Go devices |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 1977 | */ |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 1978 | static int usb_enumerate_device_otg(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 1980 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
| 1982 | #ifdef CONFIG_USB_OTG |
| 1983 | /* |
| 1984 | * OTG-aware devices on OTG-capable root hubs may be able to use SRP, |
| 1985 | * to wake us after we've powered off VBUS; and HNP, switching roles |
| 1986 | * "host" to "peripheral". The OTG descriptor helps figure this out. |
| 1987 | */ |
| 1988 | if (!udev->bus->is_b_host |
| 1989 | && udev->config |
| 1990 | && udev->parent == udev->bus->root_hub) { |
Felipe Balbi | 2eb5052 | 2009-12-04 15:47:43 +0200 | [diff] [blame] | 1991 | struct usb_otg_descriptor *desc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | struct usb_bus *bus = udev->bus; |
| 1993 | |
| 1994 | /* descriptor may appear anywhere in config */ |
| 1995 | if (__usb_get_extra_descriptor (udev->rawdescriptors[0], |
| 1996 | le16_to_cpu(udev->config[0].desc.wTotalLength), |
| 1997 | USB_DT_OTG, (void **) &desc) == 0) { |
| 1998 | if (desc->bmAttributes & USB_OTG_HNP) { |
Alan Stern | 12c3da3 | 2005-11-23 12:09:52 -0500 | [diff] [blame] | 1999 | unsigned port1 = udev->portnum; |
David Brownell | fc849b8 | 2006-09-18 16:53:26 -0700 | [diff] [blame] | 2000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | dev_info(&udev->dev, |
| 2002 | "Dual-Role OTG device on %sHNP port\n", |
| 2003 | (port1 == bus->otg_port) |
| 2004 | ? "" : "non-"); |
| 2005 | |
| 2006 | /* enable HNP before suspend, it's simpler */ |
| 2007 | if (port1 == bus->otg_port) |
| 2008 | bus->b_hnp_enable = 1; |
| 2009 | err = usb_control_msg(udev, |
| 2010 | usb_sndctrlpipe(udev, 0), |
| 2011 | USB_REQ_SET_FEATURE, 0, |
| 2012 | bus->b_hnp_enable |
| 2013 | ? USB_DEVICE_B_HNP_ENABLE |
| 2014 | : USB_DEVICE_A_ALT_HNP_SUPPORT, |
| 2015 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 2016 | if (err < 0) { |
| 2017 | /* OTG MESSAGE: report errors here, |
| 2018 | * customize to match your product. |
| 2019 | */ |
| 2020 | dev_info(&udev->dev, |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 2021 | "can't set HNP mode: %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | err); |
| 2023 | bus->b_hnp_enable = 0; |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | if (!is_targeted(udev)) { |
| 2030 | |
| 2031 | /* Maybe it can talk to us, though we can't talk to it. |
| 2032 | * (Includes HNP test device.) |
| 2033 | */ |
| 2034 | if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { |
David Brownell | 634a84f | 2009-01-15 13:51:28 -0800 | [diff] [blame] | 2035 | err = usb_port_suspend(udev, PMSG_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | if (err < 0) |
| 2037 | dev_dbg(&udev->dev, "HNP fail, %d\n", err); |
| 2038 | } |
Vikram Pandita | ffcdc18 | 2007-05-25 21:31:07 -0700 | [diff] [blame] | 2039 | err = -ENOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | goto fail; |
| 2041 | } |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2042 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | #endif |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2044 | return err; |
| 2045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2047 | |
| 2048 | /** |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2049 | * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2050 | * @udev: newly addressed device (in ADDRESS state) |
| 2051 | * |
| 2052 | * This is only called by usb_new_device() and usb_authorize_device() |
| 2053 | * and FIXME -- all comments that apply to them apply here wrt to |
| 2054 | * environment. |
| 2055 | * |
| 2056 | * If the device is WUSB and not authorized, we don't attempt to read |
| 2057 | * the string descriptors, as they will be errored out by the device |
| 2058 | * until it has been authorized. |
| 2059 | */ |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2060 | static int usb_enumerate_device(struct usb_device *udev) |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2061 | { |
| 2062 | int err; |
| 2063 | |
| 2064 | if (udev->config == NULL) { |
| 2065 | err = usb_get_configuration(udev); |
| 2066 | if (err < 0) { |
| 2067 | dev_err(&udev->dev, "can't read configurations, error %d\n", |
| 2068 | err); |
| 2069 | goto fail; |
| 2070 | } |
| 2071 | } |
| 2072 | if (udev->wusb == 1 && udev->authorized == 0) { |
| 2073 | udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
| 2074 | udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
| 2075 | udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
| 2076 | } |
| 2077 | else { |
| 2078 | /* read the standard strings and cache them if present */ |
| 2079 | udev->product = usb_cache_string(udev, udev->descriptor.iProduct); |
| 2080 | udev->manufacturer = usb_cache_string(udev, |
| 2081 | udev->descriptor.iManufacturer); |
| 2082 | udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); |
| 2083 | } |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2084 | err = usb_enumerate_device_otg(udev); |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2085 | fail: |
| 2086 | return err; |
| 2087 | } |
| 2088 | |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2089 | static void set_usb_port_removable(struct usb_device *udev) |
| 2090 | { |
| 2091 | struct usb_device *hdev = udev->parent; |
| 2092 | struct usb_hub *hub; |
| 2093 | u8 port = udev->portnum; |
| 2094 | u16 wHubCharacteristics; |
| 2095 | bool removable = true; |
| 2096 | |
| 2097 | if (!hdev) |
| 2098 | return; |
| 2099 | |
| 2100 | hub = hdev_to_hub(udev->parent); |
| 2101 | |
| 2102 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
| 2103 | |
| 2104 | if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) |
| 2105 | return; |
| 2106 | |
| 2107 | if (hub_is_superspeed(hdev)) { |
| 2108 | if (hub->descriptor->u.ss.DeviceRemovable & (1 << port)) |
| 2109 | removable = false; |
| 2110 | } else { |
| 2111 | if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) |
| 2112 | removable = false; |
| 2113 | } |
| 2114 | |
| 2115 | if (removable) |
| 2116 | udev->removable = USB_DEVICE_REMOVABLE; |
| 2117 | else |
| 2118 | udev->removable = USB_DEVICE_FIXED; |
| 2119 | } |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2120 | |
| 2121 | /** |
| 2122 | * usb_new_device - perform initial device setup (usbcore-internal) |
| 2123 | * @udev: newly addressed device (in ADDRESS state) |
| 2124 | * |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2125 | * This is called with devices which have been detected but not fully |
| 2126 | * enumerated. The device descriptor is available, but not descriptors |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2127 | * for any device configuration. The caller must have locked either |
| 2128 | * the parent hub (if udev is a normal device) or else the |
| 2129 | * usb_bus_list_lock (if udev is a root hub). The parent's pointer to |
| 2130 | * udev has already been installed, but udev is not yet visible through |
| 2131 | * sysfs or other filesystem code. |
| 2132 | * |
| 2133 | * It will return if the device is configured properly or not. Zero if |
| 2134 | * the interface was registered with the driver core; else a negative |
| 2135 | * errno value. |
| 2136 | * |
| 2137 | * This call is synchronous, and may not be used in an interrupt context. |
| 2138 | * |
| 2139 | * Only the hub driver or root-hub registrar should ever call this. |
| 2140 | */ |
| 2141 | int usb_new_device(struct usb_device *udev) |
| 2142 | { |
| 2143 | int err; |
| 2144 | |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2145 | if (udev->parent) { |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2146 | /* Initialize non-root-hub device wakeup to disabled; |
| 2147 | * device (un)configuration controls wakeup capable |
| 2148 | * sysfs power/wakeup controls wakeup enabled/disabled |
| 2149 | */ |
| 2150 | device_init_wakeup(&udev->dev, 0); |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2151 | } |
| 2152 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2153 | /* Tell the runtime-PM framework the device is active */ |
| 2154 | pm_runtime_set_active(&udev->dev); |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2155 | pm_runtime_get_noresume(&udev->dev); |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 2156 | pm_runtime_use_autosuspend(&udev->dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2157 | pm_runtime_enable(&udev->dev); |
| 2158 | |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2159 | /* By default, forbid autosuspend for all devices. It will be |
| 2160 | * allowed for hubs during binding. |
| 2161 | */ |
| 2162 | usb_disable_autosuspend(udev); |
| 2163 | |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2164 | err = usb_enumerate_device(udev); /* Read descriptors */ |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2165 | if (err < 0) |
| 2166 | goto fail; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 2167 | dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", |
| 2168 | udev->devnum, udev->bus->busnum, |
| 2169 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2170 | /* export the usbdev device-node for libusb */ |
| 2171 | udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, |
| 2172 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); |
| 2173 | |
Alan Stern | 6cd1320 | 2008-11-13 15:08:30 -0500 | [diff] [blame] | 2174 | /* Tell the world! */ |
| 2175 | announce_device(udev); |
Alan Stern | 195af2c | 2007-07-16 15:28:19 -0400 | [diff] [blame] | 2176 | |
Theodore Ts'o | b04b315 | 2012-07-04 11:22:20 -0400 | [diff] [blame^] | 2177 | if (udev->serial) |
| 2178 | add_device_randomness(udev->serial, strlen(udev->serial)); |
| 2179 | if (udev->product) |
| 2180 | add_device_randomness(udev->product, strlen(udev->product)); |
| 2181 | if (udev->manufacturer) |
| 2182 | add_device_randomness(udev->manufacturer, |
| 2183 | strlen(udev->manufacturer)); |
| 2184 | |
Rafael J. Wysocki | 927bc91 | 2010-02-08 19:18:16 +0100 | [diff] [blame] | 2185 | device_enable_async_suspend(&udev->dev); |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2186 | |
| 2187 | /* |
| 2188 | * check whether the hub marks this port as non-removable. Do it |
| 2189 | * now so that platform-specific data can override it in |
| 2190 | * device_add() |
| 2191 | */ |
| 2192 | if (udev->parent) |
| 2193 | set_usb_port_removable(udev); |
| 2194 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2195 | /* Register the device. The device driver is responsible |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2196 | * for configuring the device and invoking the add-device |
| 2197 | * notifier chain (used by usbfs and possibly others). |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2198 | */ |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2199 | err = device_add(&udev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | if (err) { |
| 2201 | dev_err(&udev->dev, "can't device_add, error %d\n", err); |
| 2202 | goto fail; |
| 2203 | } |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 2204 | |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2205 | (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2206 | usb_mark_last_busy(udev); |
| 2207 | pm_runtime_put_sync_autosuspend(&udev->dev); |
Greg Kroah-Hartman | c066475 | 2006-08-11 01:55:12 -0700 | [diff] [blame] | 2208 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
| 2210 | fail: |
| 2211 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2212 | pm_runtime_disable(&udev->dev); |
| 2213 | pm_runtime_set_suspended(&udev->dev); |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2214 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2217 | |
| 2218 | /** |
Randy Dunlap | fd39c86 | 2007-10-15 17:30:02 -0700 | [diff] [blame] | 2219 | * usb_deauthorize_device - deauthorize a device (usbcore-internal) |
| 2220 | * @usb_dev: USB device |
| 2221 | * |
| 2222 | * Move the USB device to a very basic state where interfaces are disabled |
| 2223 | * and the device is in fact unconfigured and unusable. |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2224 | * |
| 2225 | * We share a lock (that we have) with device_del(), so we need to |
| 2226 | * defer its call. |
| 2227 | */ |
| 2228 | int usb_deauthorize_device(struct usb_device *usb_dev) |
| 2229 | { |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2230 | usb_lock_device(usb_dev); |
| 2231 | if (usb_dev->authorized == 0) |
| 2232 | goto out_unauthorized; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2233 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2234 | usb_dev->authorized = 0; |
| 2235 | usb_set_configuration(usb_dev, -1); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2236 | |
| 2237 | kfree(usb_dev->product); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2238 | usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2239 | kfree(usb_dev->manufacturer); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2240 | usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2241 | kfree(usb_dev->serial); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2242 | usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2243 | |
| 2244 | usb_destroy_configuration(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2245 | usb_dev->descriptor.bNumConfigurations = 0; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2246 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2247 | out_unauthorized: |
| 2248 | usb_unlock_device(usb_dev); |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
| 2252 | |
| 2253 | int usb_authorize_device(struct usb_device *usb_dev) |
| 2254 | { |
| 2255 | int result = 0, c; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2256 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2257 | usb_lock_device(usb_dev); |
| 2258 | if (usb_dev->authorized == 1) |
| 2259 | goto out_authorized; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2260 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2261 | result = usb_autoresume_device(usb_dev); |
| 2262 | if (result < 0) { |
| 2263 | dev_err(&usb_dev->dev, |
| 2264 | "can't autoresume for authorization: %d\n", result); |
| 2265 | goto error_autoresume; |
| 2266 | } |
| 2267 | result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); |
| 2268 | if (result < 0) { |
| 2269 | dev_err(&usb_dev->dev, "can't re-read device descriptor for " |
| 2270 | "authorization: %d\n", result); |
| 2271 | goto error_device_descriptor; |
| 2272 | } |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2273 | |
| 2274 | kfree(usb_dev->product); |
| 2275 | usb_dev->product = NULL; |
| 2276 | kfree(usb_dev->manufacturer); |
| 2277 | usb_dev->manufacturer = NULL; |
| 2278 | kfree(usb_dev->serial); |
| 2279 | usb_dev->serial = NULL; |
| 2280 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2281 | usb_dev->authorized = 1; |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2282 | result = usb_enumerate_device(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2283 | if (result < 0) |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2284 | goto error_enumerate; |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2285 | /* Choose and set the configuration. This registers the interfaces |
| 2286 | * with the driver core and lets interface drivers bind to them. |
| 2287 | */ |
Greg Kroah-Hartman | b5ea060 | 2007-08-02 22:44:27 -0600 | [diff] [blame] | 2288 | c = usb_choose_configuration(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2289 | if (c >= 0) { |
| 2290 | result = usb_set_configuration(usb_dev, c); |
| 2291 | if (result) { |
| 2292 | dev_err(&usb_dev->dev, |
| 2293 | "can't set config #%d, error %d\n", c, result); |
| 2294 | /* This need not be fatal. The user can try to |
| 2295 | * set other configurations. */ |
| 2296 | } |
| 2297 | } |
| 2298 | dev_info(&usb_dev->dev, "authorized to connect\n"); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2299 | |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2300 | error_enumerate: |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2301 | error_device_descriptor: |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2302 | usb_autosuspend_device(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2303 | error_autoresume: |
| 2304 | out_authorized: |
| 2305 | usb_unlock_device(usb_dev); // complements locktree |
| 2306 | return result; |
| 2307 | } |
| 2308 | |
| 2309 | |
Inaky Perez-Gonzalez | 0165de0 | 2006-08-25 19:35:29 -0700 | [diff] [blame] | 2310 | /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ |
| 2311 | static unsigned hub_is_wusb(struct usb_hub *hub) |
| 2312 | { |
| 2313 | struct usb_hcd *hcd; |
| 2314 | if (hub->hdev->parent != NULL) /* not a root hub? */ |
| 2315 | return 0; |
| 2316 | hcd = container_of(hub->hdev->bus, struct usb_hcd, self); |
| 2317 | return hcd->wireless; |
| 2318 | } |
| 2319 | |
| 2320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | #define PORT_RESET_TRIES 5 |
| 2322 | #define SET_ADDRESS_TRIES 2 |
| 2323 | #define GET_DESCRIPTOR_TRIES 2 |
| 2324 | #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 2325 | #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | |
| 2327 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ |
| 2328 | #define HUB_SHORT_RESET_TIME 10 |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2329 | #define HUB_BH_RESET_TIME 50 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | #define HUB_LONG_RESET_TIME 200 |
| 2331 | #define HUB_RESET_TIMEOUT 500 |
| 2332 | |
Sarah Sharp | 10d674a | 2011-09-14 14:24:52 -0700 | [diff] [blame] | 2333 | static int hub_port_reset(struct usb_hub *hub, int port1, |
| 2334 | struct usb_device *udev, unsigned int delay, bool warm); |
| 2335 | |
| 2336 | /* Is a USB 3.0 port in the Inactive state? */ |
| 2337 | static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus) |
| 2338 | { |
| 2339 | return hub_is_superspeed(hub->hdev) && |
| 2340 | (portstatus & USB_PORT_STAT_LINK_STATE) == |
| 2341 | USB_SS_PORT_LS_SS_INACTIVE; |
| 2342 | } |
| 2343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | static int hub_port_wait_reset(struct usb_hub *hub, int port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2345 | struct usb_device *udev, unsigned int delay, bool warm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | { |
| 2347 | int delay_time, ret; |
| 2348 | u16 portstatus; |
| 2349 | u16 portchange; |
| 2350 | |
| 2351 | for (delay_time = 0; |
| 2352 | delay_time < HUB_RESET_TIMEOUT; |
| 2353 | delay_time += delay) { |
| 2354 | /* wait to give the device a chance to reset */ |
| 2355 | msleep(delay); |
| 2356 | |
| 2357 | /* read and decode port status */ |
| 2358 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 2359 | if (ret < 0) |
| 2360 | return ret; |
| 2361 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2362 | /* |
| 2363 | * Some buggy devices require a warm reset to be issued even |
| 2364 | * when the port appears not to be connected. |
| 2365 | */ |
| 2366 | if (!warm) { |
Sarah Sharp | 10d674a | 2011-09-14 14:24:52 -0700 | [diff] [blame] | 2367 | /* |
| 2368 | * Some buggy devices can cause an NEC host controller |
| 2369 | * to transition to the "Error" state after a hot port |
| 2370 | * reset. This will show up as the port state in |
| 2371 | * "Inactive", and the port may also report a |
| 2372 | * disconnect. Forcing a warm port reset seems to make |
| 2373 | * the device work. |
| 2374 | * |
| 2375 | * See https://bugzilla.kernel.org/show_bug.cgi?id=41752 |
| 2376 | */ |
| 2377 | if (hub_port_inactive(hub, portstatus)) { |
| 2378 | int ret; |
| 2379 | |
| 2380 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) |
| 2381 | clear_port_feature(hub->hdev, port1, |
| 2382 | USB_PORT_FEAT_C_CONNECTION); |
| 2383 | if (portchange & USB_PORT_STAT_C_LINK_STATE) |
| 2384 | clear_port_feature(hub->hdev, port1, |
| 2385 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 2386 | if (portchange & USB_PORT_STAT_C_RESET) |
| 2387 | clear_port_feature(hub->hdev, port1, |
| 2388 | USB_PORT_FEAT_C_RESET); |
| 2389 | dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", |
| 2390 | port1); |
| 2391 | ret = hub_port_reset(hub, port1, |
| 2392 | udev, HUB_BH_RESET_TIME, |
| 2393 | true); |
| 2394 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) |
| 2395 | clear_port_feature(hub->hdev, port1, |
| 2396 | USB_PORT_FEAT_C_CONNECTION); |
| 2397 | return ret; |
| 2398 | } |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2399 | /* Device went away? */ |
| 2400 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) |
| 2401 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2403 | /* bomb out completely if the connection bounced */ |
| 2404 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) |
| 2405 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2407 | /* if we`ve finished resetting, then break out of |
| 2408 | * the loop |
| 2409 | */ |
| 2410 | if (!(portstatus & USB_PORT_STAT_RESET) && |
| 2411 | (portstatus & USB_PORT_STAT_ENABLE)) { |
| 2412 | if (hub_is_wusb(hub)) |
| 2413 | udev->speed = USB_SPEED_WIRELESS; |
| 2414 | else if (hub_is_superspeed(hub->hdev)) |
| 2415 | udev->speed = USB_SPEED_SUPER; |
| 2416 | else if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
| 2417 | udev->speed = USB_SPEED_HIGH; |
| 2418 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
| 2419 | udev->speed = USB_SPEED_LOW; |
| 2420 | else |
| 2421 | udev->speed = USB_SPEED_FULL; |
| 2422 | return 0; |
| 2423 | } |
| 2424 | } else { |
| 2425 | if (portchange & USB_PORT_STAT_C_BH_RESET) |
| 2426 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | /* switch to the long delay after two short delay failures */ |
| 2430 | if (delay_time >= 2 * HUB_SHORT_RESET_TIME) |
| 2431 | delay = HUB_LONG_RESET_TIME; |
| 2432 | |
| 2433 | dev_dbg (hub->intfdev, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2434 | "port %d not %sreset yet, waiting %dms\n", |
| 2435 | port1, warm ? "warm " : "", delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | return -EBUSY; |
| 2439 | } |
| 2440 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2441 | static void hub_port_finish_reset(struct usb_hub *hub, int port1, |
| 2442 | struct usb_device *udev, int *status, bool warm) |
| 2443 | { |
| 2444 | switch (*status) { |
| 2445 | case 0: |
| 2446 | if (!warm) { |
| 2447 | struct usb_hcd *hcd; |
| 2448 | /* TRSTRCY = 10 ms; plus some extra */ |
| 2449 | msleep(10 + 40); |
| 2450 | update_devnum(udev, 0); |
| 2451 | hcd = bus_to_hcd(udev->bus); |
| 2452 | if (hcd->driver->reset_device) { |
| 2453 | *status = hcd->driver->reset_device(hcd, udev); |
| 2454 | if (*status < 0) { |
| 2455 | dev_err(&udev->dev, "Cannot reset " |
| 2456 | "HCD device state\n"); |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | /* FALL THROUGH */ |
| 2462 | case -ENOTCONN: |
| 2463 | case -ENODEV: |
| 2464 | clear_port_feature(hub->hdev, |
| 2465 | port1, USB_PORT_FEAT_C_RESET); |
| 2466 | /* FIXME need disconnect() for NOTATTACHED device */ |
| 2467 | if (warm) { |
| 2468 | clear_port_feature(hub->hdev, port1, |
| 2469 | USB_PORT_FEAT_C_BH_PORT_RESET); |
| 2470 | clear_port_feature(hub->hdev, port1, |
| 2471 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 2472 | } else { |
| 2473 | usb_set_device_state(udev, *status |
| 2474 | ? USB_STATE_NOTATTACHED |
| 2475 | : USB_STATE_DEFAULT); |
| 2476 | } |
| 2477 | break; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | static int hub_port_reset(struct usb_hub *hub, int port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2483 | struct usb_device *udev, unsigned int delay, bool warm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | { |
| 2485 | int i, status; |
| 2486 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2487 | if (!warm) { |
| 2488 | /* Block EHCI CF initialization during the port reset. |
| 2489 | * Some companion controllers don't like it when they mix. |
| 2490 | */ |
| 2491 | down_read(&ehci_cf_port_reset_rwsem); |
| 2492 | } else { |
| 2493 | if (!hub_is_superspeed(hub->hdev)) { |
| 2494 | dev_err(hub->intfdev, "only USB3 hub support " |
| 2495 | "warm reset\n"); |
| 2496 | return -EINVAL; |
| 2497 | } |
| 2498 | } |
Alan Stern | 32fe019 | 2007-10-10 16:27:07 -0400 | [diff] [blame] | 2499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | /* Reset the port */ |
| 2501 | for (i = 0; i < PORT_RESET_TRIES; i++) { |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2502 | status = set_port_feature(hub->hdev, port1, (warm ? |
| 2503 | USB_PORT_FEAT_BH_PORT_RESET : |
| 2504 | USB_PORT_FEAT_RESET)); |
| 2505 | if (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | dev_err(hub->intfdev, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2507 | "cannot %sreset port %d (err = %d)\n", |
| 2508 | warm ? "warm " : "", port1, status); |
| 2509 | } else { |
| 2510 | status = hub_port_wait_reset(hub, port1, udev, delay, |
| 2511 | warm); |
David Brownell | dd16525 | 2005-08-31 10:45:25 -0700 | [diff] [blame] | 2512 | if (status && status != -ENOTCONN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | dev_dbg(hub->intfdev, |
| 2514 | "port_wait_reset: err = %d\n", |
| 2515 | status); |
| 2516 | } |
| 2517 | |
| 2518 | /* return on disconnect or reset */ |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2519 | if (status == 0 || status == -ENOTCONN || status == -ENODEV) { |
| 2520 | hub_port_finish_reset(hub, port1, udev, &status, warm); |
Alan Stern | 32fe019 | 2007-10-10 16:27:07 -0400 | [diff] [blame] | 2521 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | dev_dbg (hub->intfdev, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2525 | "port %d not enabled, trying %sreset again...\n", |
| 2526 | port1, warm ? "warm " : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | delay = HUB_LONG_RESET_TIME; |
| 2528 | } |
| 2529 | |
| 2530 | dev_err (hub->intfdev, |
| 2531 | "Cannot enable port %i. Maybe the USB cable is bad?\n", |
| 2532 | port1); |
| 2533 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2534 | done: |
| 2535 | if (!warm) |
| 2536 | up_read(&ehci_cf_port_reset_rwsem); |
| 2537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | return status; |
| 2539 | } |
| 2540 | |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2541 | /* Check if a port is power on */ |
| 2542 | static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) |
| 2543 | { |
| 2544 | int ret = 0; |
| 2545 | |
| 2546 | if (hub_is_superspeed(hub->hdev)) { |
| 2547 | if (portstatus & USB_SS_PORT_STAT_POWER) |
| 2548 | ret = 1; |
| 2549 | } else { |
| 2550 | if (portstatus & USB_PORT_STAT_POWER) |
| 2551 | ret = 1; |
| 2552 | } |
| 2553 | |
| 2554 | return ret; |
| 2555 | } |
| 2556 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2557 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2559 | /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ |
| 2560 | static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) |
| 2561 | { |
| 2562 | int ret = 0; |
| 2563 | |
| 2564 | if (hub_is_superspeed(hub->hdev)) { |
| 2565 | if ((portstatus & USB_PORT_STAT_LINK_STATE) |
| 2566 | == USB_SS_PORT_LS_U3) |
| 2567 | ret = 1; |
| 2568 | } else { |
| 2569 | if (portstatus & USB_PORT_STAT_SUSPEND) |
| 2570 | ret = 1; |
| 2571 | } |
| 2572 | |
| 2573 | return ret; |
| 2574 | } |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2575 | |
| 2576 | /* Determine whether the device on a port is ready for a normal resume, |
| 2577 | * is ready for a reset-resume, or should be disconnected. |
| 2578 | */ |
| 2579 | static int check_port_resume_type(struct usb_device *udev, |
| 2580 | struct usb_hub *hub, int port1, |
| 2581 | int status, unsigned portchange, unsigned portstatus) |
| 2582 | { |
| 2583 | /* Is the device still present? */ |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2584 | if (status || port_is_suspended(hub, portstatus) || |
| 2585 | !port_is_power_on(hub, portstatus) || |
| 2586 | !(portstatus & USB_PORT_STAT_CONNECTION)) { |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2587 | if (status >= 0) |
| 2588 | status = -ENODEV; |
| 2589 | } |
| 2590 | |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 2591 | /* Can't do a normal resume if the port isn't enabled, |
| 2592 | * so try a reset-resume instead. |
| 2593 | */ |
| 2594 | else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { |
| 2595 | if (udev->persist_enabled) |
| 2596 | udev->reset_resume = 1; |
| 2597 | else |
| 2598 | status = -ENODEV; |
| 2599 | } |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2600 | |
| 2601 | if (status) { |
| 2602 | dev_dbg(hub->intfdev, |
| 2603 | "port %d status %04x.%04x after resume, %d\n", |
| 2604 | port1, portchange, portstatus, status); |
| 2605 | } else if (udev->reset_resume) { |
| 2606 | |
| 2607 | /* Late port handoff can set status-change bits */ |
| 2608 | if (portchange & USB_PORT_STAT_C_CONNECTION) |
| 2609 | clear_port_feature(hub->hdev, port1, |
| 2610 | USB_PORT_FEAT_C_CONNECTION); |
| 2611 | if (portchange & USB_PORT_STAT_C_ENABLE) |
| 2612 | clear_port_feature(hub->hdev, port1, |
| 2613 | USB_PORT_FEAT_C_ENABLE); |
| 2614 | } |
| 2615 | |
| 2616 | return status; |
| 2617 | } |
| 2618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | #ifdef CONFIG_USB_SUSPEND |
| 2620 | |
| 2621 | /* |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 2622 | * usb_port_suspend - suspend a usb device's upstream port |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2623 | * @udev: device that's no longer in active use, not a root hub |
David Brownell | 5edbfb7 | 2005-09-22 22:45:26 -0700 | [diff] [blame] | 2624 | * Context: must be able to sleep; device not locked; pm locks held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | * |
| 2626 | * Suspends a USB device that isn't in active use, conserving power. |
| 2627 | * Devices may wake out of a suspend, if anything important happens, |
| 2628 | * using the remote wakeup mechanism. They may also be taken out of |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 2629 | * suspend by the host, using usb_port_resume(). It's also routine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | * to disconnect devices while they are suspended. |
| 2631 | * |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 2632 | * This only affects the USB hardware for a device; its interfaces |
| 2633 | * (and, for hubs, child devices) must already have been suspended. |
| 2634 | * |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2635 | * Selective port suspend reduces power; most suspended devices draw |
| 2636 | * less than 500 uA. It's also used in OTG, along with remote wakeup. |
| 2637 | * All devices below the suspended port are also suspended. |
| 2638 | * |
| 2639 | * Devices leave suspend state when the host wakes them up. Some devices |
| 2640 | * also support "remote wakeup", where the device can activate the USB |
| 2641 | * tree above them to deliver data, such as a keypress or packet. In |
| 2642 | * some cases, this wakes the USB host. |
| 2643 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | * Suspending OTG devices may trigger HNP, if that's been enabled |
| 2645 | * between a pair of dual-role devices. That will change roles, such |
| 2646 | * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. |
| 2647 | * |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 2648 | * Devices on USB hub ports have only one "suspend" state, corresponding |
| 2649 | * to ACPI D2, "may cause the device to lose some context". |
| 2650 | * State transitions include: |
| 2651 | * |
| 2652 | * - suspend, resume ... when the VBUS power link stays live |
| 2653 | * - suspend, disconnect ... VBUS lost |
| 2654 | * |
| 2655 | * Once VBUS drop breaks the circuit, the port it's using has to go through |
| 2656 | * normal re-enumeration procedures, starting with enabling VBUS power. |
| 2657 | * Other than re-initializing the hub (plug/unplug, except for root hubs), |
| 2658 | * Linux (2.6) currently has NO mechanisms to initiate that: no khubd |
| 2659 | * timer, no SRP, no requests through sysfs. |
| 2660 | * |
| 2661 | * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when |
| 2662 | * the root hub for their bus goes into global suspend ... so we don't |
| 2663 | * (falsely) update the device power state to say it suspended. |
| 2664 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | * Returns 0 on success, else negative errno. |
| 2666 | */ |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 2667 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | { |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2669 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
| 2670 | int port1 = udev->portnum; |
| 2671 | int status; |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 2672 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2673 | /* enable remote wakeup when appropriate; this lets the device |
| 2674 | * wake up the upstream hub (including maybe the root hub). |
| 2675 | * |
| 2676 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when |
| 2677 | * we don't explicitly enable it here. |
| 2678 | */ |
| 2679 | if (udev->do_remote_wakeup) { |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 2680 | if (!hub_is_superspeed(hub->hdev)) { |
| 2681 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2682 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, |
| 2683 | USB_DEVICE_REMOTE_WAKEUP, 0, |
| 2684 | NULL, 0, |
| 2685 | USB_CTRL_SET_TIMEOUT); |
| 2686 | } else { |
| 2687 | /* Assume there's only one function on the USB 3.0 |
| 2688 | * device and enable remote wake for the first |
| 2689 | * interface. FIXME if the interface association |
| 2690 | * descriptor shows there's more than one function. |
| 2691 | */ |
| 2692 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2693 | USB_REQ_SET_FEATURE, |
| 2694 | USB_RECIP_INTERFACE, |
| 2695 | USB_INTRF_FUNC_SUSPEND, |
Sarah Sharp | 3b9b6ac | 2012-01-06 15:48:30 -0800 | [diff] [blame] | 2696 | USB_INTRF_FUNC_SUSPEND_RW | |
| 2697 | USB_INTRF_FUNC_SUSPEND_LP, |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 2698 | NULL, 0, |
| 2699 | USB_CTRL_SET_TIMEOUT); |
| 2700 | } |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 2701 | if (status) { |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2702 | dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", |
| 2703 | status); |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 2704 | /* bail if autosuspend is requested */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 2705 | if (PMSG_IS_AUTO(msg)) |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 2706 | return status; |
| 2707 | } |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2708 | } |
| 2709 | |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 2710 | /* disable USB2 hardware LPM */ |
| 2711 | if (udev->usb2_hw_lpm_enabled == 1) |
| 2712 | usb_set_usb2_hardware_lpm(udev, 0); |
| 2713 | |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 2714 | if (usb_unlocked_disable_lpm(udev)) { |
| 2715 | dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.", |
| 2716 | __func__); |
| 2717 | return -ENOMEM; |
| 2718 | } |
| 2719 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2720 | /* see 7.1.7.6 */ |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 2721 | if (hub_is_superspeed(hub->hdev)) |
| 2722 | status = set_port_feature(hub->hdev, |
| 2723 | port1 | (USB_SS_PORT_LS_U3 << 3), |
| 2724 | USB_PORT_FEAT_LINK_STATE); |
Andiry Xu | a8f08d8 | 2011-03-31 14:56:50 +0800 | [diff] [blame] | 2725 | else |
| 2726 | status = set_port_feature(hub->hdev, port1, |
| 2727 | USB_PORT_FEAT_SUSPEND); |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2728 | if (status) { |
| 2729 | dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", |
| 2730 | port1, status); |
| 2731 | /* paranoia: "should not happen" */ |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 2732 | if (udev->do_remote_wakeup) |
| 2733 | (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2734 | USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, |
| 2735 | USB_DEVICE_REMOTE_WAKEUP, 0, |
| 2736 | NULL, 0, |
| 2737 | USB_CTRL_SET_TIMEOUT); |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 2738 | |
Andiry Xu | c3e751e | 2012-05-05 00:50:10 +0800 | [diff] [blame] | 2739 | /* Try to enable USB2 hardware LPM again */ |
| 2740 | if (udev->usb2_hw_lpm_capable == 1) |
| 2741 | usb_set_usb2_hardware_lpm(udev, 1); |
| 2742 | |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 2743 | /* Try to enable USB3 LPM again */ |
| 2744 | usb_unlocked_enable_lpm(udev); |
| 2745 | |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 2746 | /* System sleep transitions should never fail */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 2747 | if (!PMSG_IS_AUTO(msg)) |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 2748 | status = 0; |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2749 | } else { |
| 2750 | /* device has up to 10 msec to fully suspend */ |
Alan Stern | 30b1a7a | 2011-09-27 21:54:22 +0200 | [diff] [blame] | 2751 | dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", |
| 2752 | (PMSG_IS_AUTO(msg) ? "auto-" : ""), |
| 2753 | udev->do_remote_wakeup); |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2754 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
| 2755 | msleep(10); |
| 2756 | } |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2757 | usb_mark_last_busy(hub->hdev); |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 2758 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | } |
David Brownell | f3f3253 | 2005-09-22 22:37:29 -0700 | [diff] [blame] | 2760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | /* |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 2762 | * If the USB "suspend" state is in use (rather than "global suspend"), |
| 2763 | * many devices will be individually taken out of suspend state using |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2764 | * special "resume" signaling. This routine kicks in shortly after |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | * hardware resume signaling is finished, either because of selective |
| 2766 | * resume (by host) or remote wakeup (by device) ... now see what changed |
| 2767 | * in the tree that's rooted at this device. |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2768 | * |
| 2769 | * If @udev->reset_resume is set then the device is reset before the |
| 2770 | * status check is done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | */ |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 2772 | static int finish_port_resume(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | { |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2774 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | u16 devstatus; |
| 2776 | |
| 2777 | /* caller owns the udev device lock */ |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 2778 | dev_dbg(&udev->dev, "%s\n", |
| 2779 | udev->reset_resume ? "finish reset-resume" : "finish resume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | |
| 2781 | /* usb ch9 identifies four variants of SUSPENDED, based on what |
| 2782 | * state the device resumes to. Linux currently won't see the |
| 2783 | * first two on the host side; they'd be inside hub_port_init() |
| 2784 | * during many timeouts, but khubd can't suspend until later. |
| 2785 | */ |
| 2786 | usb_set_device_state(udev, udev->actconfig |
| 2787 | ? USB_STATE_CONFIGURED |
| 2788 | : USB_STATE_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2790 | /* 10.5.4.5 says not to reset a suspended port if the attached |
| 2791 | * device is enabled for remote wakeup. Hence the reset |
| 2792 | * operation is carried out here, after the port has been |
| 2793 | * resumed. |
| 2794 | */ |
| 2795 | if (udev->reset_resume) |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 2796 | retry_reset_resume: |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 2797 | status = usb_reset_and_verify_device(udev); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | /* 10.5.4.5 says be sure devices in the tree are still there. |
| 2800 | * For now let's assume the device didn't go crazy on resume, |
| 2801 | * and device drivers will know about any resume quirks. |
| 2802 | */ |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2803 | if (status == 0) { |
Alan Stern | 46dede4 | 2007-08-14 10:56:10 -0400 | [diff] [blame] | 2804 | devstatus = 0; |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2805 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); |
| 2806 | if (status >= 0) |
Alan Stern | 46dede4 | 2007-08-14 10:56:10 -0400 | [diff] [blame] | 2807 | status = (status > 0 ? 0 : -ENODEV); |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 2808 | |
| 2809 | /* If a normal resume failed, try doing a reset-resume */ |
| 2810 | if (status && !udev->reset_resume && udev->persist_enabled) { |
| 2811 | dev_dbg(&udev->dev, "retry with reset-resume\n"); |
| 2812 | udev->reset_resume = 1; |
| 2813 | goto retry_reset_resume; |
| 2814 | } |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2815 | } |
Alan Stern | b40b7a9 | 2006-06-19 15:12:38 -0400 | [diff] [blame] | 2816 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2817 | if (status) { |
| 2818 | dev_dbg(&udev->dev, "gone after usb resume? status %d\n", |
| 2819 | status); |
| 2820 | } else if (udev->actconfig) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | le16_to_cpus(&devstatus); |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 2822 | if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | status = usb_control_msg(udev, |
| 2824 | usb_sndctrlpipe(udev, 0), |
| 2825 | USB_REQ_CLEAR_FEATURE, |
| 2826 | USB_RECIP_DEVICE, |
| 2827 | USB_DEVICE_REMOTE_WAKEUP, 0, |
| 2828 | NULL, 0, |
| 2829 | USB_CTRL_SET_TIMEOUT); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 2830 | if (status) |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 2831 | dev_dbg(&udev->dev, |
| 2832 | "disable remote wakeup, status %d\n", |
| 2833 | status); |
Alan Stern | 4bf0ba8 | 2005-11-21 11:58:07 -0500 | [diff] [blame] | 2834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | } |
| 2837 | return status; |
| 2838 | } |
| 2839 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2840 | /* |
| 2841 | * usb_port_resume - re-activate a suspended usb device's upstream port |
| 2842 | * @udev: device to re-activate, not a root hub |
| 2843 | * Context: must be able to sleep; device not locked; pm locks held |
| 2844 | * |
| 2845 | * This will re-activate the suspended device, increasing power usage |
| 2846 | * while letting drivers communicate again with its endpoints. |
| 2847 | * USB resume explicitly guarantees that the power session between |
| 2848 | * the host and the device is the same as it was when the device |
| 2849 | * suspended. |
| 2850 | * |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 2851 | * If @udev->reset_resume is set then this routine won't check that the |
| 2852 | * port is still enabled. Furthermore, finish_port_resume() above will |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2853 | * reset @udev. The end result is that a broken power session can be |
| 2854 | * recovered and @udev will appear to persist across a loss of VBUS power. |
| 2855 | * |
| 2856 | * For example, if a host controller doesn't maintain VBUS suspend current |
| 2857 | * during a system sleep or is reset when the system wakes up, all the USB |
| 2858 | * power sessions below it will be broken. This is especially troublesome |
| 2859 | * for mass-storage devices containing mounted filesystems, since the |
| 2860 | * device will appear to have disconnected and all the memory mappings |
| 2861 | * to it will be lost. Using the USB_PERSIST facility, the device can be |
| 2862 | * made to appear as if it had not disconnected. |
| 2863 | * |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 2864 | * This facility can be dangerous. Although usb_reset_and_verify_device() makes |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 2865 | * every effort to insure that the same device is present after the |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2866 | * reset as before, it cannot provide a 100% guarantee. Furthermore it's |
| 2867 | * quite possible for a device to remain unaltered but its media to be |
| 2868 | * changed. If the user replaces a flash memory card while the system is |
| 2869 | * asleep, he will have only himself to blame when the filesystem on the |
| 2870 | * new card is corrupted and the system crashes. |
| 2871 | * |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2872 | * Returns 0 on success, else negative errno. |
| 2873 | */ |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 2874 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | { |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2876 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
| 2877 | int port1 = udev->portnum; |
| 2878 | int status; |
| 2879 | u16 portchange, portstatus; |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 2880 | |
| 2881 | /* Skip the initial Clear-Suspend step for a remote wakeup */ |
| 2882 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2883 | if (status == 0 && !port_is_suspended(hub, portstatus)) |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 2884 | goto SuspendCleared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | |
| 2886 | // dev_dbg(hub->intfdev, "resume port %d\n", port1); |
| 2887 | |
Alan Stern | d5cbad4 | 2006-08-11 16:52:39 -0400 | [diff] [blame] | 2888 | set_bit(port1, hub->busy_bits); |
| 2889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | /* see 7.1.7.7; affects power usage, but not budgeting */ |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 2891 | if (hub_is_superspeed(hub->hdev)) |
| 2892 | status = set_port_feature(hub->hdev, |
| 2893 | port1 | (USB_SS_PORT_LS_U0 << 3), |
| 2894 | USB_PORT_FEAT_LINK_STATE); |
| 2895 | else |
| 2896 | status = clear_port_feature(hub->hdev, |
| 2897 | port1, USB_PORT_FEAT_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | if (status) { |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 2899 | dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", |
| 2900 | port1, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | /* drive resume for at least 20 msec */ |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 2903 | dev_dbg(&udev->dev, "usb %sresume\n", |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 2904 | (PMSG_IS_AUTO(msg) ? "auto-" : "")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | msleep(25); |
| 2906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | /* Virtual root hubs can trigger on GET_PORT_STATUS to |
| 2908 | * stop resume signaling. Then finish the resume |
| 2909 | * sequence. |
| 2910 | */ |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 2911 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2912 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2913 | /* TRSMRCY = 10 msec */ |
| 2914 | msleep(10); |
| 2915 | } |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2916 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2917 | SuspendCleared: |
| 2918 | if (status == 0) { |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 2919 | if (hub_is_superspeed(hub->hdev)) { |
| 2920 | if (portchange & USB_PORT_STAT_C_LINK_STATE) |
| 2921 | clear_port_feature(hub->hdev, port1, |
| 2922 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 2923 | } else { |
| 2924 | if (portchange & USB_PORT_STAT_C_SUSPEND) |
| 2925 | clear_port_feature(hub->hdev, port1, |
| 2926 | USB_PORT_FEAT_C_SUSPEND); |
| 2927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | |
Alan Stern | d5cbad4 | 2006-08-11 16:52:39 -0400 | [diff] [blame] | 2930 | clear_bit(port1, hub->busy_bits); |
Alan Stern | d5cbad4 | 2006-08-11 16:52:39 -0400 | [diff] [blame] | 2931 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2932 | status = check_port_resume_type(udev, |
| 2933 | hub, port1, status, portchange, portstatus); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2934 | if (status == 0) |
| 2935 | status = finish_port_resume(udev); |
| 2936 | if (status < 0) { |
| 2937 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); |
| 2938 | hub_port_logical_disconnect(hub, port1); |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 2939 | } else { |
| 2940 | /* Try to enable USB2 hardware LPM */ |
| 2941 | if (udev->usb2_hw_lpm_capable == 1) |
| 2942 | usb_set_usb2_hardware_lpm(udev, 1); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 2943 | |
| 2944 | /* Try to enable USB3 LPM */ |
| 2945 | usb_unlocked_enable_lpm(udev); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2946 | } |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | return status; |
| 2949 | } |
| 2950 | |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 2951 | /* caller has locked udev */ |
Alan Stern | 0534d46 | 2010-01-08 12:56:30 -0500 | [diff] [blame] | 2952 | int usb_remote_wakeup(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | { |
| 2954 | int status = 0; |
| 2955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | if (udev->state == USB_STATE_SUSPENDED) { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 2957 | dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2958 | status = usb_autoresume_device(udev); |
| 2959 | if (status == 0) { |
| 2960 | /* Let the drivers do their thing, then... */ |
| 2961 | usb_autosuspend_device(udev); |
| 2962 | } |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 2963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | return status; |
| 2965 | } |
| 2966 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2967 | #else /* CONFIG_USB_SUSPEND */ |
| 2968 | |
| 2969 | /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */ |
| 2970 | |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 2971 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2972 | { |
| 2973 | return 0; |
| 2974 | } |
| 2975 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2976 | /* However we may need to do a reset-resume */ |
| 2977 | |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 2978 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2979 | { |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2980 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
| 2981 | int port1 = udev->portnum; |
| 2982 | int status; |
| 2983 | u16 portchange, portstatus; |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2984 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2985 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
| 2986 | status = check_port_resume_type(udev, |
| 2987 | hub, port1, status, portchange, portstatus); |
| 2988 | |
| 2989 | if (status) { |
| 2990 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); |
| 2991 | hub_port_logical_disconnect(hub, port1); |
| 2992 | } else if (udev->reset_resume) { |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2993 | dev_dbg(&udev->dev, "reset-resume\n"); |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 2994 | status = usb_reset_and_verify_device(udev); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 2995 | } |
| 2996 | return status; |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2997 | } |
| 2998 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2999 | #endif |
| 3000 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 3001 | static int hub_suspend(struct usb_interface *intf, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | { |
| 3003 | struct usb_hub *hub = usb_get_intfdata (intf); |
| 3004 | struct usb_device *hdev = hub->hdev; |
| 3005 | unsigned port1; |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 3006 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3008 | /* Warn if children aren't already suspended */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
| 3010 | struct usb_device *udev; |
| 3011 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 3012 | udev = hdev->children [port1-1]; |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 3013 | if (udev && udev->can_submit) { |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3014 | dev_warn(&intf->dev, "port %d nyet suspended\n", port1); |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 3015 | if (PMSG_IS_AUTO(msg)) |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3016 | return -EBUSY; |
David Brownell | c9f89fa | 2005-09-13 19:57:04 -0700 | [diff] [blame] | 3017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | } |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 3019 | if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { |
| 3020 | /* Enable hub to send remote wakeup for all ports. */ |
| 3021 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
| 3022 | status = set_port_feature(hdev, |
| 3023 | port1 | |
| 3024 | USB_PORT_FEAT_REMOTE_WAKE_CONNECT | |
| 3025 | USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | |
| 3026 | USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, |
| 3027 | USB_PORT_FEAT_REMOTE_WAKE_MASK); |
| 3028 | } |
| 3029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 3031 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 3032 | |
David Brownell | c9f89fa | 2005-09-13 19:57:04 -0700 | [diff] [blame] | 3033 | /* stop khubd and related activity */ |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 3034 | hub_quiesce(hub, HUB_SUSPEND); |
Alan Stern | b6f6436 | 2007-05-04 11:51:54 -0400 | [diff] [blame] | 3035 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | static int hub_resume(struct usb_interface *intf) |
| 3039 | { |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3040 | struct usb_hub *hub = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3042 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 3043 | hub_activate(hub, HUB_RESUME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | return 0; |
| 3045 | } |
| 3046 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 3047 | static int hub_reset_resume(struct usb_interface *intf) |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3048 | { |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 3049 | struct usb_hub *hub = usb_get_intfdata(intf); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3050 | |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3051 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 3052 | hub_activate(hub, HUB_RESET_RESUME); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3053 | return 0; |
| 3054 | } |
| 3055 | |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3056 | /** |
| 3057 | * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power |
| 3058 | * @rhdev: struct usb_device for the root hub |
| 3059 | * |
| 3060 | * The USB host controller driver calls this function when its root hub |
| 3061 | * is resumed and Vbus power has been interrupted or the controller |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 3062 | * has been reset. The routine marks @rhdev as having lost power. |
| 3063 | * When the hub driver is resumed it will take notice and carry out |
| 3064 | * power-session recovery for all the "USB-PERSIST"-enabled child devices; |
| 3065 | * the others will be disconnected. |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3066 | */ |
| 3067 | void usb_root_hub_lost_power(struct usb_device *rhdev) |
| 3068 | { |
| 3069 | dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); |
| 3070 | rhdev->reset_resume = 1; |
| 3071 | } |
| 3072 | EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); |
| 3073 | |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3074 | static const char * const usb3_lpm_names[] = { |
| 3075 | "U0", |
| 3076 | "U1", |
| 3077 | "U2", |
| 3078 | "U3", |
| 3079 | }; |
| 3080 | |
| 3081 | /* |
| 3082 | * Send a Set SEL control transfer to the device, prior to enabling |
| 3083 | * device-initiated U1 or U2. This lets the device know the exit latencies from |
| 3084 | * the time the device initiates a U1 or U2 exit, to the time it will receive a |
| 3085 | * packet from the host. |
| 3086 | * |
| 3087 | * This function will fail if the SEL or PEL values for udev are greater than |
| 3088 | * the maximum allowed values for the link state to be enabled. |
| 3089 | */ |
| 3090 | static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) |
| 3091 | { |
| 3092 | struct usb_set_sel_req *sel_values; |
| 3093 | unsigned long long u1_sel; |
| 3094 | unsigned long long u1_pel; |
| 3095 | unsigned long long u2_sel; |
| 3096 | unsigned long long u2_pel; |
| 3097 | int ret; |
| 3098 | |
| 3099 | /* Convert SEL and PEL stored in ns to us */ |
| 3100 | u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); |
| 3101 | u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); |
| 3102 | u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); |
| 3103 | u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); |
| 3104 | |
| 3105 | /* |
| 3106 | * Make sure that the calculated SEL and PEL values for the link |
| 3107 | * state we're enabling aren't bigger than the max SEL/PEL |
| 3108 | * value that will fit in the SET SEL control transfer. |
| 3109 | * Otherwise the device would get an incorrect idea of the exit |
| 3110 | * latency for the link state, and could start a device-initiated |
| 3111 | * U1/U2 when the exit latencies are too high. |
| 3112 | */ |
| 3113 | if ((state == USB3_LPM_U1 && |
| 3114 | (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || |
| 3115 | u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || |
| 3116 | (state == USB3_LPM_U2 && |
| 3117 | (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || |
| 3118 | u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { |
| 3119 | dev_dbg(&udev->dev, "Device-initiated %s disabled due " |
| 3120 | "to long SEL %llu ms or PEL %llu ms\n", |
| 3121 | usb3_lpm_names[state], u1_sel, u1_pel); |
| 3122 | return -EINVAL; |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * If we're enabling device-initiated LPM for one link state, |
| 3127 | * but the other link state has a too high SEL or PEL value, |
| 3128 | * just set those values to the max in the Set SEL request. |
| 3129 | */ |
| 3130 | if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) |
| 3131 | u1_sel = USB3_LPM_MAX_U1_SEL_PEL; |
| 3132 | |
| 3133 | if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) |
| 3134 | u1_pel = USB3_LPM_MAX_U1_SEL_PEL; |
| 3135 | |
| 3136 | if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) |
| 3137 | u2_sel = USB3_LPM_MAX_U2_SEL_PEL; |
| 3138 | |
| 3139 | if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) |
| 3140 | u2_pel = USB3_LPM_MAX_U2_SEL_PEL; |
| 3141 | |
| 3142 | /* |
| 3143 | * usb_enable_lpm() can be called as part of a failed device reset, |
| 3144 | * which may be initiated by an error path of a mass storage driver. |
| 3145 | * Therefore, use GFP_NOIO. |
| 3146 | */ |
| 3147 | sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); |
| 3148 | if (!sel_values) |
| 3149 | return -ENOMEM; |
| 3150 | |
| 3151 | sel_values->u1_sel = u1_sel; |
| 3152 | sel_values->u1_pel = u1_pel; |
| 3153 | sel_values->u2_sel = cpu_to_le16(u2_sel); |
| 3154 | sel_values->u2_pel = cpu_to_le16(u2_pel); |
| 3155 | |
| 3156 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3157 | USB_REQ_SET_SEL, |
| 3158 | USB_RECIP_DEVICE, |
| 3159 | 0, 0, |
| 3160 | sel_values, sizeof *(sel_values), |
| 3161 | USB_CTRL_SET_TIMEOUT); |
| 3162 | kfree(sel_values); |
| 3163 | return ret; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * Enable or disable device-initiated U1 or U2 transitions. |
| 3168 | */ |
| 3169 | static int usb_set_device_initiated_lpm(struct usb_device *udev, |
| 3170 | enum usb3_link_state state, bool enable) |
| 3171 | { |
| 3172 | int ret; |
| 3173 | int feature; |
| 3174 | |
| 3175 | switch (state) { |
| 3176 | case USB3_LPM_U1: |
| 3177 | feature = USB_DEVICE_U1_ENABLE; |
| 3178 | break; |
| 3179 | case USB3_LPM_U2: |
| 3180 | feature = USB_DEVICE_U2_ENABLE; |
| 3181 | break; |
| 3182 | default: |
| 3183 | dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", |
| 3184 | __func__, enable ? "enable" : "disable"); |
| 3185 | return -EINVAL; |
| 3186 | } |
| 3187 | |
| 3188 | if (udev->state != USB_STATE_CONFIGURED) { |
| 3189 | dev_dbg(&udev->dev, "%s: Can't %s %s state " |
| 3190 | "for unconfigured device.\n", |
| 3191 | __func__, enable ? "enable" : "disable", |
| 3192 | usb3_lpm_names[state]); |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | if (enable) { |
| 3197 | /* |
| 3198 | * First, let the device know about the exit latencies |
| 3199 | * associated with the link state we're about to enable. |
| 3200 | */ |
| 3201 | ret = usb_req_set_sel(udev, state); |
| 3202 | if (ret < 0) { |
| 3203 | dev_warn(&udev->dev, "Set SEL for device-initiated " |
| 3204 | "%s failed.\n", usb3_lpm_names[state]); |
| 3205 | return -EBUSY; |
| 3206 | } |
| 3207 | /* |
| 3208 | * Now send the control transfer to enable device-initiated LPM |
| 3209 | * for either U1 or U2. |
| 3210 | */ |
| 3211 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3212 | USB_REQ_SET_FEATURE, |
| 3213 | USB_RECIP_DEVICE, |
| 3214 | feature, |
| 3215 | 0, NULL, 0, |
| 3216 | USB_CTRL_SET_TIMEOUT); |
| 3217 | } else { |
| 3218 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3219 | USB_REQ_CLEAR_FEATURE, |
| 3220 | USB_RECIP_DEVICE, |
| 3221 | feature, |
| 3222 | 0, NULL, 0, |
| 3223 | USB_CTRL_SET_TIMEOUT); |
| 3224 | } |
| 3225 | if (ret < 0) { |
| 3226 | dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", |
| 3227 | enable ? "Enable" : "Disable", |
| 3228 | usb3_lpm_names[state]); |
| 3229 | return -EBUSY; |
| 3230 | } |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
| 3234 | static int usb_set_lpm_timeout(struct usb_device *udev, |
| 3235 | enum usb3_link_state state, int timeout) |
| 3236 | { |
| 3237 | int ret; |
| 3238 | int feature; |
| 3239 | |
| 3240 | switch (state) { |
| 3241 | case USB3_LPM_U1: |
| 3242 | feature = USB_PORT_FEAT_U1_TIMEOUT; |
| 3243 | break; |
| 3244 | case USB3_LPM_U2: |
| 3245 | feature = USB_PORT_FEAT_U2_TIMEOUT; |
| 3246 | break; |
| 3247 | default: |
| 3248 | dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", |
| 3249 | __func__); |
| 3250 | return -EINVAL; |
| 3251 | } |
| 3252 | |
| 3253 | if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && |
| 3254 | timeout != USB3_LPM_DEVICE_INITIATED) { |
| 3255 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " |
| 3256 | "which is a reserved value.\n", |
| 3257 | usb3_lpm_names[state], timeout); |
| 3258 | return -EINVAL; |
| 3259 | } |
| 3260 | |
| 3261 | ret = set_port_feature(udev->parent, |
| 3262 | USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, |
| 3263 | feature); |
| 3264 | if (ret < 0) { |
| 3265 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," |
| 3266 | "error code %i\n", usb3_lpm_names[state], |
| 3267 | timeout, ret); |
| 3268 | return -EBUSY; |
| 3269 | } |
| 3270 | if (state == USB3_LPM_U1) |
| 3271 | udev->u1_params.timeout = timeout; |
| 3272 | else |
| 3273 | udev->u2_params.timeout = timeout; |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | /* |
| 3278 | * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated |
| 3279 | * U1/U2 entry. |
| 3280 | * |
| 3281 | * We will attempt to enable U1 or U2, but there are no guarantees that the |
| 3282 | * control transfers to set the hub timeout or enable device-initiated U1/U2 |
| 3283 | * will be successful. |
| 3284 | * |
| 3285 | * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI |
| 3286 | * driver know about it. If that call fails, it should be harmless, and just |
| 3287 | * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. |
| 3288 | */ |
| 3289 | static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, |
| 3290 | enum usb3_link_state state) |
| 3291 | { |
| 3292 | int timeout; |
| 3293 | |
| 3294 | /* We allow the host controller to set the U1/U2 timeout internally |
| 3295 | * first, so that it can change its schedule to account for the |
| 3296 | * additional latency to send data to a device in a lower power |
| 3297 | * link state. |
| 3298 | */ |
| 3299 | timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); |
| 3300 | |
| 3301 | /* xHCI host controller doesn't want to enable this LPM state. */ |
| 3302 | if (timeout == 0) |
| 3303 | return; |
| 3304 | |
| 3305 | if (timeout < 0) { |
| 3306 | dev_warn(&udev->dev, "Could not enable %s link state, " |
| 3307 | "xHCI error %i.\n", usb3_lpm_names[state], |
| 3308 | timeout); |
| 3309 | return; |
| 3310 | } |
| 3311 | |
| 3312 | if (usb_set_lpm_timeout(udev, state, timeout)) |
| 3313 | /* If we can't set the parent hub U1/U2 timeout, |
| 3314 | * device-initiated LPM won't be allowed either, so let the xHCI |
| 3315 | * host know that this link state won't be enabled. |
| 3316 | */ |
| 3317 | hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); |
| 3318 | |
| 3319 | /* Only a configured device will accept the Set Feature U1/U2_ENABLE */ |
| 3320 | else if (udev->actconfig) |
| 3321 | usb_set_device_initiated_lpm(udev, state, true); |
| 3322 | |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated |
| 3327 | * U1/U2 entry. |
| 3328 | * |
| 3329 | * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. |
| 3330 | * If zero is returned, the parent will not allow the link to go into U1/U2. |
| 3331 | * |
| 3332 | * If zero is returned, device-initiated U1/U2 entry may still be enabled, but |
| 3333 | * it won't have an effect on the bus link state because the parent hub will |
| 3334 | * still disallow device-initiated U1/U2 entry. |
| 3335 | * |
| 3336 | * If zero is returned, the xHCI host controller may still think U1/U2 entry is |
| 3337 | * possible. The result will be slightly more bus bandwidth will be taken up |
| 3338 | * (to account for U1/U2 exit latency), but it should be harmless. |
| 3339 | */ |
| 3340 | static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, |
| 3341 | enum usb3_link_state state) |
| 3342 | { |
| 3343 | int feature; |
| 3344 | |
| 3345 | switch (state) { |
| 3346 | case USB3_LPM_U1: |
| 3347 | feature = USB_PORT_FEAT_U1_TIMEOUT; |
| 3348 | break; |
| 3349 | case USB3_LPM_U2: |
| 3350 | feature = USB_PORT_FEAT_U2_TIMEOUT; |
| 3351 | break; |
| 3352 | default: |
| 3353 | dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", |
| 3354 | __func__); |
| 3355 | return -EINVAL; |
| 3356 | } |
| 3357 | |
| 3358 | if (usb_set_lpm_timeout(udev, state, 0)) |
| 3359 | return -EBUSY; |
| 3360 | |
| 3361 | usb_set_device_initiated_lpm(udev, state, false); |
| 3362 | |
| 3363 | if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) |
| 3364 | dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " |
| 3365 | "bus schedule bandwidth may be impacted.\n", |
| 3366 | usb3_lpm_names[state]); |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
| 3370 | /* |
| 3371 | * Disable hub-initiated and device-initiated U1 and U2 entry. |
| 3372 | * Caller must own the bandwidth_mutex. |
| 3373 | * |
| 3374 | * This will call usb_enable_lpm() on failure, which will decrement |
| 3375 | * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. |
| 3376 | */ |
| 3377 | int usb_disable_lpm(struct usb_device *udev) |
| 3378 | { |
| 3379 | struct usb_hcd *hcd; |
| 3380 | |
| 3381 | if (!udev || !udev->parent || |
| 3382 | udev->speed != USB_SPEED_SUPER || |
| 3383 | !udev->lpm_capable) |
| 3384 | return 0; |
| 3385 | |
| 3386 | hcd = bus_to_hcd(udev->bus); |
| 3387 | if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) |
| 3388 | return 0; |
| 3389 | |
| 3390 | udev->lpm_disable_count++; |
Dan Carpenter | 55558c3 | 2012-05-22 20:54:34 +0300 | [diff] [blame] | 3391 | if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3392 | return 0; |
| 3393 | |
| 3394 | /* If LPM is enabled, attempt to disable it. */ |
| 3395 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) |
| 3396 | goto enable_lpm; |
| 3397 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) |
| 3398 | goto enable_lpm; |
| 3399 | |
| 3400 | return 0; |
| 3401 | |
| 3402 | enable_lpm: |
| 3403 | usb_enable_lpm(udev); |
| 3404 | return -EBUSY; |
| 3405 | } |
| 3406 | EXPORT_SYMBOL_GPL(usb_disable_lpm); |
| 3407 | |
| 3408 | /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ |
| 3409 | int usb_unlocked_disable_lpm(struct usb_device *udev) |
| 3410 | { |
| 3411 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 3412 | int ret; |
| 3413 | |
| 3414 | if (!hcd) |
| 3415 | return -EINVAL; |
| 3416 | |
| 3417 | mutex_lock(hcd->bandwidth_mutex); |
| 3418 | ret = usb_disable_lpm(udev); |
| 3419 | mutex_unlock(hcd->bandwidth_mutex); |
| 3420 | |
| 3421 | return ret; |
| 3422 | } |
| 3423 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); |
| 3424 | |
| 3425 | /* |
| 3426 | * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The |
| 3427 | * xHCI host policy may prevent U1 or U2 from being enabled. |
| 3428 | * |
| 3429 | * Other callers may have disabled link PM, so U1 and U2 entry will be disabled |
| 3430 | * until the lpm_disable_count drops to zero. Caller must own the |
| 3431 | * bandwidth_mutex. |
| 3432 | */ |
| 3433 | void usb_enable_lpm(struct usb_device *udev) |
| 3434 | { |
| 3435 | struct usb_hcd *hcd; |
| 3436 | |
| 3437 | if (!udev || !udev->parent || |
| 3438 | udev->speed != USB_SPEED_SUPER || |
| 3439 | !udev->lpm_capable) |
| 3440 | return; |
| 3441 | |
| 3442 | udev->lpm_disable_count--; |
| 3443 | hcd = bus_to_hcd(udev->bus); |
| 3444 | /* Double check that we can both enable and disable LPM. |
| 3445 | * Device must be configured to accept set feature U1/U2 timeout. |
| 3446 | */ |
| 3447 | if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || |
| 3448 | !hcd->driver->disable_usb3_lpm_timeout) |
| 3449 | return; |
| 3450 | |
| 3451 | if (udev->lpm_disable_count > 0) |
| 3452 | return; |
| 3453 | |
| 3454 | usb_enable_link_state(hcd, udev, USB3_LPM_U1); |
| 3455 | usb_enable_link_state(hcd, udev, USB3_LPM_U2); |
| 3456 | } |
| 3457 | EXPORT_SYMBOL_GPL(usb_enable_lpm); |
| 3458 | |
| 3459 | /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ |
| 3460 | void usb_unlocked_enable_lpm(struct usb_device *udev) |
| 3461 | { |
| 3462 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 3463 | |
| 3464 | if (!hcd) |
| 3465 | return; |
| 3466 | |
| 3467 | mutex_lock(hcd->bandwidth_mutex); |
| 3468 | usb_enable_lpm(udev); |
| 3469 | mutex_unlock(hcd->bandwidth_mutex); |
| 3470 | } |
| 3471 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); |
| 3472 | |
| 3473 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 3474 | #else /* CONFIG_PM */ |
| 3475 | |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3476 | #define hub_suspend NULL |
| 3477 | #define hub_resume NULL |
| 3478 | #define hub_reset_resume NULL |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3479 | |
| 3480 | int usb_disable_lpm(struct usb_device *udev) |
| 3481 | { |
| 3482 | return 0; |
| 3483 | } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 3484 | EXPORT_SYMBOL_GPL(usb_disable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3485 | |
| 3486 | void usb_enable_lpm(struct usb_device *udev) { } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 3487 | EXPORT_SYMBOL_GPL(usb_enable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3488 | |
| 3489 | int usb_unlocked_disable_lpm(struct usb_device *udev) |
| 3490 | { |
| 3491 | return 0; |
| 3492 | } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 3493 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3494 | |
| 3495 | void usb_unlocked_enable_lpm(struct usb_device *udev) { } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 3496 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 3497 | #endif |
| 3498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | |
| 3500 | /* USB 2.0 spec, 7.1.7.3 / fig 7-29: |
| 3501 | * |
| 3502 | * Between connect detection and reset signaling there must be a delay |
| 3503 | * of 100ms at least for debounce and power-settling. The corresponding |
| 3504 | * timer shall restart whenever the downstream port detects a disconnect. |
| 3505 | * |
| 3506 | * Apparently there are some bluetooth and irda-dongles and a number of |
| 3507 | * low-speed devices for which this debounce period may last over a second. |
| 3508 | * Not covered by the spec - but easy to deal with. |
| 3509 | * |
| 3510 | * This implementation uses a 1500ms total debounce timeout; if the |
| 3511 | * connection isn't stable by then it returns -ETIMEDOUT. It checks |
| 3512 | * every 25ms for transient disconnects. When the port status has been |
| 3513 | * unchanged for 100ms it returns the port status. |
| 3514 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | static int hub_port_debounce(struct usb_hub *hub, int port1) |
| 3516 | { |
| 3517 | int ret; |
| 3518 | int total_time, stable_time = 0; |
| 3519 | u16 portchange, portstatus; |
| 3520 | unsigned connection = 0xffff; |
| 3521 | |
| 3522 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { |
| 3523 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 3524 | if (ret < 0) |
| 3525 | return ret; |
| 3526 | |
| 3527 | if (!(portchange & USB_PORT_STAT_C_CONNECTION) && |
| 3528 | (portstatus & USB_PORT_STAT_CONNECTION) == connection) { |
| 3529 | stable_time += HUB_DEBOUNCE_STEP; |
| 3530 | if (stable_time >= HUB_DEBOUNCE_STABLE) |
| 3531 | break; |
| 3532 | } else { |
| 3533 | stable_time = 0; |
| 3534 | connection = portstatus & USB_PORT_STAT_CONNECTION; |
| 3535 | } |
| 3536 | |
| 3537 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
| 3538 | clear_port_feature(hub->hdev, port1, |
| 3539 | USB_PORT_FEAT_C_CONNECTION); |
| 3540 | } |
| 3541 | |
| 3542 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) |
| 3543 | break; |
| 3544 | msleep(HUB_DEBOUNCE_STEP); |
| 3545 | } |
| 3546 | |
| 3547 | dev_dbg (hub->intfdev, |
| 3548 | "debounce: port %d: total %dms stable %dms status 0x%x\n", |
| 3549 | port1, total_time, stable_time, portstatus); |
| 3550 | |
| 3551 | if (stable_time < HUB_DEBOUNCE_STABLE) |
| 3552 | return -ETIMEDOUT; |
| 3553 | return portstatus; |
| 3554 | } |
| 3555 | |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3556 | void usb_ep0_reinit(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | { |
Alan Stern | ddeac4e | 2009-01-15 17:03:33 -0500 | [diff] [blame] | 3558 | usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); |
| 3559 | usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); |
Alan Stern | 2caf7fc | 2008-12-31 11:31:33 -0500 | [diff] [blame] | 3560 | usb_enable_endpoint(udev, &udev->ep0, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | } |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3562 | EXPORT_SYMBOL_GPL(usb_ep0_reinit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | |
| 3564 | #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) |
| 3565 | #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) |
| 3566 | |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 3567 | static int hub_set_address(struct usb_device *udev, int devnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | { |
| 3569 | int retval; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3570 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3572 | /* |
| 3573 | * The host controller will choose the device address, |
| 3574 | * instead of the core having chosen it earlier |
| 3575 | */ |
| 3576 | if (!hcd->driver->address_device && devnum <= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | return -EINVAL; |
| 3578 | if (udev->state == USB_STATE_ADDRESS) |
| 3579 | return 0; |
| 3580 | if (udev->state != USB_STATE_DEFAULT) |
| 3581 | return -EINVAL; |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 3582 | if (hcd->driver->address_device) |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3583 | retval = hcd->driver->address_device(hcd, udev); |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 3584 | else |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3585 | retval = usb_control_msg(udev, usb_sndaddr0pipe(), |
| 3586 | USB_REQ_SET_ADDRESS, 0, devnum, 0, |
| 3587 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | if (retval == 0) { |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 3589 | update_devnum(udev, devnum); |
David Vrabel | 4953d14 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3590 | /* Device now using proper address. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | usb_set_device_state(udev, USB_STATE_ADDRESS); |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3592 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | } |
| 3594 | return retval; |
| 3595 | } |
| 3596 | |
| 3597 | /* Reset device, (re)assign address, get device descriptor. |
| 3598 | * Device connection must be stable, no more debouncing needed. |
| 3599 | * Returns device in USB_STATE_ADDRESS, except on error. |
| 3600 | * |
| 3601 | * If this is called for an already-existing device (as part of |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 3602 | * usb_reset_and_verify_device), the caller must own the device lock. For a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | * newly detected device that is not accessible through any global |
| 3604 | * pointers, it's not necessary to lock the device. |
| 3605 | */ |
| 3606 | static int |
| 3607 | hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, |
| 3608 | int retry_counter) |
| 3609 | { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 3610 | static DEFINE_MUTEX(usb_address0_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | |
| 3612 | struct usb_device *hdev = hub->hdev; |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 3613 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | int i, j, retval; |
| 3615 | unsigned delay = HUB_SHORT_RESET_TIME; |
| 3616 | enum usb_device_speed oldspeed = udev->speed; |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3617 | const char *speed; |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 3618 | int devnum = udev->devnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
| 3620 | /* root hub ports have a slightly longer reset period |
| 3621 | * (from USB 2.0 spec, section 7.1.7.5) |
| 3622 | */ |
| 3623 | if (!hdev->parent) { |
| 3624 | delay = HUB_ROOT_RESET_TIME; |
| 3625 | if (port1 == hdev->bus->otg_port) |
| 3626 | hdev->bus->b_hnp_enable = 0; |
| 3627 | } |
| 3628 | |
| 3629 | /* Some low speed devices have problems with the quick delay, so */ |
| 3630 | /* be a bit pessimistic with those devices. RHbug #23670 */ |
| 3631 | if (oldspeed == USB_SPEED_LOW) |
| 3632 | delay = HUB_LONG_RESET_TIME; |
| 3633 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 3634 | mutex_lock(&usb_address0_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | |
Luben Tuikov | 07194ab | 2011-02-11 11:33:10 -0800 | [diff] [blame] | 3636 | /* Reset the device; full speed may morph to high speed */ |
| 3637 | /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 3638 | retval = hub_port_reset(hub, port1, udev, delay, false); |
Luben Tuikov | 07194ab | 2011-02-11 11:33:10 -0800 | [diff] [blame] | 3639 | if (retval < 0) /* error or disconnect */ |
| 3640 | goto fail; |
| 3641 | /* success, speed is known */ |
| 3642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | retval = -ENODEV; |
| 3644 | |
| 3645 | if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { |
| 3646 | dev_dbg(&udev->dev, "device reset changed speed!\n"); |
| 3647 | goto fail; |
| 3648 | } |
| 3649 | oldspeed = udev->speed; |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 3650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... |
| 3652 | * it's fixed size except for full speed devices. |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 3653 | * For Wireless USB devices, ep0 max packet is always 512 (tho |
| 3654 | * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | */ |
| 3656 | switch (udev->speed) { |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 3657 | case USB_SPEED_SUPER: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 3658 | case USB_SPEED_WIRELESS: /* fixed at 512 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 3659 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 3660 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | case USB_SPEED_HIGH: /* fixed at 64 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 3662 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | break; |
| 3664 | case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ |
| 3665 | /* to determine the ep0 maxpacket size, try to read |
| 3666 | * the device descriptor to get bMaxPacketSize0 and |
| 3667 | * then correct our initial guess. |
| 3668 | */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 3669 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | break; |
| 3671 | case USB_SPEED_LOW: /* fixed at 8 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 3672 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | break; |
| 3674 | default: |
| 3675 | goto fail; |
| 3676 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3677 | |
| 3678 | if (udev->speed == USB_SPEED_WIRELESS) |
| 3679 | speed = "variable speed Wireless"; |
| 3680 | else |
| 3681 | speed = usb_speed_string(udev->speed); |
| 3682 | |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3683 | if (udev->speed != USB_SPEED_SUPER) |
| 3684 | dev_info(&udev->dev, |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3685 | "%s %s USB device number %d using %s\n", |
| 3686 | (udev->config) ? "reset" : "new", speed, |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 3687 | devnum, udev->bus->controller->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | |
| 3689 | /* Set up TT records, if needed */ |
| 3690 | if (hdev->tt) { |
| 3691 | udev->tt = hdev->tt; |
| 3692 | udev->ttport = hdev->ttport; |
| 3693 | } else if (udev->speed != USB_SPEED_HIGH |
| 3694 | && hdev->speed == USB_SPEED_HIGH) { |
Alan Stern | d199c96 | 2011-01-31 10:56:37 -0500 | [diff] [blame] | 3695 | if (!hub->tt.hub) { |
| 3696 | dev_err(&udev->dev, "parent hub has no TT\n"); |
| 3697 | retval = -EINVAL; |
| 3698 | goto fail; |
| 3699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | udev->tt = &hub->tt; |
| 3701 | udev->ttport = port1; |
| 3702 | } |
| 3703 | |
| 3704 | /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? |
| 3705 | * Because device hardware and firmware is sometimes buggy in |
| 3706 | * this area, and this is how Linux has done it for ages. |
| 3707 | * Change it cautiously. |
| 3708 | * |
| 3709 | * NOTE: If USE_NEW_SCHEME() is true we will start by issuing |
| 3710 | * a 64-byte GET_DESCRIPTOR request. This is what Windows does, |
| 3711 | * so it may help with some non-standards-compliant devices. |
| 3712 | * Otherwise we start with SET_ADDRESS and then try to read the |
| 3713 | * first 8 bytes of the device descriptor to get the ep0 maxpacket |
| 3714 | * value. |
| 3715 | */ |
| 3716 | for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3717 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | struct usb_device_descriptor *buf; |
| 3719 | int r = 0; |
| 3720 | |
| 3721 | #define GET_DESCRIPTOR_BUFSIZE 64 |
| 3722 | buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); |
| 3723 | if (!buf) { |
| 3724 | retval = -ENOMEM; |
| 3725 | continue; |
| 3726 | } |
| 3727 | |
Alan Stern | b89ee19 | 2007-05-11 10:19:04 -0400 | [diff] [blame] | 3728 | /* Retry on all errors; some devices are flakey. |
| 3729 | * 255 is for WUSB devices, we actually need to use |
| 3730 | * 512 (WUSB1.0[4.8.1]). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | */ |
| 3732 | for (j = 0; j < 3; ++j) { |
| 3733 | buf->bMaxPacketSize0 = 0; |
| 3734 | r = usb_control_msg(udev, usb_rcvaddr0pipe(), |
| 3735 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 3736 | USB_DT_DEVICE << 8, 0, |
| 3737 | buf, GET_DESCRIPTOR_BUFSIZE, |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 3738 | initial_descriptor_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | switch (buf->bMaxPacketSize0) { |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 3740 | case 8: case 16: case 32: case 64: case 255: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | if (buf->bDescriptorType == |
| 3742 | USB_DT_DEVICE) { |
| 3743 | r = 0; |
| 3744 | break; |
| 3745 | } |
| 3746 | /* FALL THROUGH */ |
| 3747 | default: |
| 3748 | if (r == 0) |
| 3749 | r = -EPROTO; |
| 3750 | break; |
| 3751 | } |
| 3752 | if (r == 0) |
| 3753 | break; |
| 3754 | } |
| 3755 | udev->descriptor.bMaxPacketSize0 = |
| 3756 | buf->bMaxPacketSize0; |
| 3757 | kfree(buf); |
| 3758 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 3759 | retval = hub_port_reset(hub, port1, udev, delay, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | if (retval < 0) /* error or disconnect */ |
| 3761 | goto fail; |
| 3762 | if (oldspeed != udev->speed) { |
| 3763 | dev_dbg(&udev->dev, |
| 3764 | "device reset changed speed!\n"); |
| 3765 | retval = -ENODEV; |
| 3766 | goto fail; |
| 3767 | } |
| 3768 | if (r) { |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 3769 | dev_err(&udev->dev, |
| 3770 | "device descriptor read/64, error %d\n", |
| 3771 | r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | retval = -EMSGSIZE; |
| 3773 | continue; |
| 3774 | } |
| 3775 | #undef GET_DESCRIPTOR_BUFSIZE |
| 3776 | } |
| 3777 | |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3778 | /* |
| 3779 | * If device is WUSB, we already assigned an |
| 3780 | * unauthorized address in the Connect Ack sequence; |
| 3781 | * authorization will assign the final address. |
| 3782 | */ |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3783 | if (udev->wusb == 0) { |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3784 | for (j = 0; j < SET_ADDRESS_TRIES; ++j) { |
| 3785 | retval = hub_set_address(udev, devnum); |
| 3786 | if (retval >= 0) |
| 3787 | break; |
| 3788 | msleep(200); |
| 3789 | } |
| 3790 | if (retval < 0) { |
| 3791 | dev_err(&udev->dev, |
| 3792 | "device not accepting address %d, error %d\n", |
| 3793 | devnum, retval); |
| 3794 | goto fail; |
| 3795 | } |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3796 | if (udev->speed == USB_SPEED_SUPER) { |
| 3797 | devnum = udev->devnum; |
| 3798 | dev_info(&udev->dev, |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 3799 | "%s SuperSpeed USB device number %d using %s\n", |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3800 | (udev->config) ? "reset" : "new", |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 3801 | devnum, udev->bus->controller->driver->name); |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3802 | } |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3803 | |
| 3804 | /* cope with hardware quirkiness: |
| 3805 | * - let SET_ADDRESS settle, some device hardware wants it |
| 3806 | * - read ep0 maxpacket even for high and low speed, |
| 3807 | */ |
| 3808 | msleep(10); |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 3809 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | break; |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
| 3813 | retval = usb_get_device_descriptor(udev, 8); |
| 3814 | if (retval < 8) { |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 3815 | dev_err(&udev->dev, |
| 3816 | "device descriptor read/8, error %d\n", |
| 3817 | retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | if (retval >= 0) |
| 3819 | retval = -EMSGSIZE; |
| 3820 | } else { |
| 3821 | retval = 0; |
| 3822 | break; |
| 3823 | } |
| 3824 | } |
| 3825 | if (retval) |
| 3826 | goto fail; |
| 3827 | |
Elric Fu | d8aec3d | 2012-03-26 21:16:02 +0800 | [diff] [blame] | 3828 | /* |
| 3829 | * Some superspeed devices have finished the link training process |
| 3830 | * and attached to a superspeed hub port, but the device descriptor |
| 3831 | * got from those devices show they aren't superspeed devices. Warm |
| 3832 | * reset the port attached by the devices can fix them. |
| 3833 | */ |
| 3834 | if ((udev->speed == USB_SPEED_SUPER) && |
| 3835 | (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { |
| 3836 | dev_err(&udev->dev, "got a wrong device descriptor, " |
| 3837 | "warm reset device\n"); |
| 3838 | hub_port_reset(hub, port1, udev, |
| 3839 | HUB_BH_RESET_TIME, true); |
| 3840 | retval = -EINVAL; |
| 3841 | goto fail; |
| 3842 | } |
| 3843 | |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 3844 | if (udev->descriptor.bMaxPacketSize0 == 0xff || |
| 3845 | udev->speed == USB_SPEED_SUPER) |
| 3846 | i = 512; |
| 3847 | else |
| 3848 | i = udev->descriptor.bMaxPacketSize0; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3849 | if (usb_endpoint_maxp(&udev->ep0.desc) != i) { |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 3850 | if (udev->speed == USB_SPEED_LOW || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | !(i == 8 || i == 16 || i == 32 || i == 64)) { |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 3852 | dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | retval = -EMSGSIZE; |
| 3854 | goto fail; |
| 3855 | } |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 3856 | if (udev->speed == USB_SPEED_FULL) |
| 3857 | dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); |
| 3858 | else |
| 3859 | dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 3861 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); |
| 3865 | if (retval < (signed)sizeof(udev->descriptor)) { |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 3866 | dev_err(&udev->dev, "device descriptor read/all, error %d\n", |
| 3867 | retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | if (retval >= 0) |
| 3869 | retval = -ENOMSG; |
| 3870 | goto fail; |
| 3871 | } |
| 3872 | |
Andiry Xu | 1ff4df5 | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 3873 | if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { |
| 3874 | retval = usb_get_bos_descriptor(udev); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 3875 | if (!retval) { |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 3876 | udev->lpm_capable = usb_device_supports_lpm(udev); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 3877 | usb_set_lpm_parameters(udev); |
| 3878 | } |
Andiry Xu | 1ff4df5 | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 3879 | } |
Andiry Xu | 3148bf0 | 2011-09-23 14:19:47 -0700 | [diff] [blame] | 3880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | retval = 0; |
Alek Du | 48f2497 | 2010-06-04 15:47:55 +0800 | [diff] [blame] | 3882 | /* notify HCD that we have a device connected and addressed */ |
| 3883 | if (hcd->driver->update_device) |
| 3884 | hcd->driver->update_device(hcd, udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | fail: |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 3886 | if (retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | hub_port_disable(hub, port1, 0); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 3888 | update_devnum(udev, devnum); /* for disconnect processing */ |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 3889 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 3890 | mutex_unlock(&usb_address0_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | return retval; |
| 3892 | } |
| 3893 | |
| 3894 | static void |
| 3895 | check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) |
| 3896 | { |
| 3897 | struct usb_qualifier_descriptor *qual; |
| 3898 | int status; |
| 3899 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 3900 | qual = kmalloc (sizeof *qual, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | if (qual == NULL) |
| 3902 | return; |
| 3903 | |
| 3904 | status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, |
| 3905 | qual, sizeof *qual); |
| 3906 | if (status == sizeof *qual) { |
| 3907 | dev_info(&udev->dev, "not running at top speed; " |
| 3908 | "connect to a high speed hub\n"); |
| 3909 | /* hub LEDs are probably harder to miss than syslog */ |
| 3910 | if (hub->has_indicators) { |
| 3911 | hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3912 | schedule_delayed_work (&hub->leds, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | } |
| 3914 | } |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 3915 | kfree(qual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | static unsigned |
| 3919 | hub_power_remaining (struct usb_hub *hub) |
| 3920 | { |
| 3921 | struct usb_device *hdev = hub->hdev; |
| 3922 | int remaining; |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3923 | int port1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3925 | if (!hub->limited_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | return 0; |
| 3927 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3928 | remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; |
| 3929 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 3930 | struct usb_device *udev = hdev->children[port1 - 1]; |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3931 | int delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | |
| 3933 | if (!udev) |
| 3934 | continue; |
| 3935 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3936 | /* Unconfigured devices may not use more than 100mA, |
| 3937 | * or 8mA for OTG ports */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | if (udev->actconfig) |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3939 | delta = udev->actconfig->desc.bMaxPower * 2; |
| 3940 | else if (port1 != udev->bus->otg_port || hdev->parent) |
| 3941 | delta = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | else |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3943 | delta = 8; |
| 3944 | if (delta > hub->mA_per_port) |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 3945 | dev_warn(&udev->dev, |
| 3946 | "%dmA is over %umA budget for port %d!\n", |
| 3947 | delta, hub->mA_per_port, port1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | remaining -= delta; |
| 3949 | } |
| 3950 | if (remaining < 0) { |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 3951 | dev_warn(hub->intfdev, "%dmA over power budget!\n", |
| 3952 | - remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | remaining = 0; |
| 3954 | } |
| 3955 | return remaining; |
| 3956 | } |
| 3957 | |
| 3958 | /* Handle physical or logical connection change events. |
| 3959 | * This routine is called when: |
| 3960 | * a port connection-change occurs; |
| 3961 | * a port enable-change occurs (often caused by EMI); |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 3962 | * usb_reset_and_verify_device() encounters changed descriptors (as from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | * a firmware download) |
| 3964 | * caller already locked the hub |
| 3965 | */ |
| 3966 | static void hub_port_connect_change(struct usb_hub *hub, int port1, |
| 3967 | u16 portstatus, u16 portchange) |
| 3968 | { |
| 3969 | struct usb_device *hdev = hub->hdev; |
| 3970 | struct device *hub_dev = hub->intfdev; |
Balaji Rao | 90da096 | 2007-11-22 01:58:14 +0530 | [diff] [blame] | 3971 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 3972 | unsigned wHubCharacteristics = |
| 3973 | le16_to_cpu(hub->descriptor->wHubCharacteristics); |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 3974 | struct usb_device *udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | int status, i; |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 3976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | dev_dbg (hub_dev, |
| 3978 | "port %d, status %04x, change %04x, %s\n", |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 3979 | port1, portstatus, portchange, portspeed(hub, portstatus)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | |
| 3981 | if (hub->has_indicators) { |
| 3982 | set_port_led(hub, port1, HUB_LED_AUTO); |
| 3983 | hub->indicator[port1-1] = INDICATOR_AUTO; |
| 3984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | |
| 3986 | #ifdef CONFIG_USB_OTG |
| 3987 | /* during HNP, don't repeat the debounce */ |
| 3988 | if (hdev->bus->is_b_host) |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 3989 | portchange &= ~(USB_PORT_STAT_C_CONNECTION | |
| 3990 | USB_PORT_STAT_C_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | #endif |
| 3992 | |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 3993 | /* Try to resuscitate an existing device */ |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 3994 | udev = hdev->children[port1-1]; |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 3995 | if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && |
| 3996 | udev->state != USB_STATE_NOTATTACHED) { |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 3997 | usb_lock_device(udev); |
| 3998 | if (portstatus & USB_PORT_STAT_ENABLE) { |
| 3999 | status = 0; /* Nothing to do */ |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4000 | |
| 4001 | #ifdef CONFIG_USB_SUSPEND |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4002 | } else if (udev->state == USB_STATE_SUSPENDED && |
| 4003 | udev->persist_enabled) { |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4004 | /* For a suspended device, treat this as a |
| 4005 | * remote wakeup event. |
| 4006 | */ |
Alan Stern | 0534d46 | 2010-01-08 12:56:30 -0500 | [diff] [blame] | 4007 | status = usb_remote_wakeup(udev); |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4008 | #endif |
| 4009 | |
| 4010 | } else { |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4011 | status = -ENODEV; /* Don't resuscitate */ |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4012 | } |
| 4013 | usb_unlock_device(udev); |
| 4014 | |
| 4015 | if (status == 0) { |
| 4016 | clear_bit(port1, hub->change_bits); |
| 4017 | return; |
| 4018 | } |
| 4019 | } |
| 4020 | |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 4021 | /* Disconnect any existing devices under this port */ |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4022 | if (udev) |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4023 | usb_disconnect(&hdev->children[port1-1]); |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 4024 | clear_bit(port1, hub->change_bits); |
| 4025 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 4026 | /* We can forget about a "removed" device when there's a physical |
| 4027 | * disconnect or the connect status changes. |
| 4028 | */ |
| 4029 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 4030 | (portchange & USB_PORT_STAT_C_CONNECTION)) |
| 4031 | clear_bit(port1, hub->removed_bits); |
| 4032 | |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4033 | if (portchange & (USB_PORT_STAT_C_CONNECTION | |
| 4034 | USB_PORT_STAT_C_ENABLE)) { |
| 4035 | status = hub_port_debounce(hub, port1); |
| 4036 | if (status < 0) { |
| 4037 | if (printk_ratelimit()) |
| 4038 | dev_err(hub_dev, "connect-debounce failed, " |
| 4039 | "port %d disabled\n", port1); |
| 4040 | portstatus &= ~USB_PORT_STAT_CONNECTION; |
| 4041 | } else { |
| 4042 | portstatus = status; |
| 4043 | } |
| 4044 | } |
| 4045 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 4046 | /* Return now if debouncing failed or nothing is connected or |
| 4047 | * the device was "removed". |
| 4048 | */ |
| 4049 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 4050 | test_bit(port1, hub->removed_bits)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | |
| 4052 | /* maybe switch power back on (e.g. root hub was reset) */ |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4053 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 4054 | && !port_is_power_on(hub, portstatus)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | if (portstatus & USB_PORT_STAT_ENABLE) |
| 4058 | goto done; |
| 4059 | return; |
| 4060 | } |
| 4061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | for (i = 0; i < SET_CONFIG_TRIES; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | |
| 4064 | /* reallocate for each attempt, since references |
| 4065 | * to the previous one can escape in various ways |
| 4066 | */ |
| 4067 | udev = usb_alloc_dev(hdev, hdev->bus, port1); |
| 4068 | if (!udev) { |
| 4069 | dev_err (hub_dev, |
| 4070 | "couldn't allocate port %d usb_device\n", |
| 4071 | port1); |
| 4072 | goto done; |
| 4073 | } |
| 4074 | |
| 4075 | usb_set_device_state(udev, USB_STATE_POWERED); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4076 | udev->bus_mA = hub->mA_per_port; |
Alan Stern | b6956ff | 2006-08-30 15:46:48 -0400 | [diff] [blame] | 4077 | udev->level = hdev->level + 1; |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4078 | udev->wusb = hub_is_wusb(hub); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4079 | |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 4080 | /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ |
| 4081 | if (hub_is_superspeed(hub->hdev)) |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 4082 | udev->speed = USB_SPEED_SUPER; |
| 4083 | else |
| 4084 | udev->speed = USB_SPEED_UNKNOWN; |
| 4085 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4086 | choose_devnum(udev); |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 4087 | if (udev->devnum <= 0) { |
| 4088 | status = -ENOTCONN; /* Don't retry */ |
| 4089 | goto loop; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 4092 | /* reset (non-USB 3.0 devices) and get descriptor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | status = hub_port_init(hub, udev, port1, i); |
| 4094 | if (status < 0) |
| 4095 | goto loop; |
| 4096 | |
Phil Dibowitz | 93362a8 | 2010-07-22 00:05:01 +0200 | [diff] [blame] | 4097 | usb_detect_quirks(udev); |
| 4098 | if (udev->quirks & USB_QUIRK_DELAY_INIT) |
| 4099 | msleep(1000); |
| 4100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | /* consecutive bus-powered hubs aren't reliable; they can |
| 4102 | * violate the voltage drop budget. if the new child has |
| 4103 | * a "powered" LED, users should notice we didn't enable it |
| 4104 | * (without reading syslog), even without per-port LEDs |
| 4105 | * on the parent. |
| 4106 | */ |
| 4107 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4108 | && udev->bus_mA <= 100) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | u16 devstat; |
| 4110 | |
| 4111 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, |
| 4112 | &devstat); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4113 | if (status < 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | dev_dbg(&udev->dev, "get status %d ?\n", status); |
| 4115 | goto loop_disable; |
| 4116 | } |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4117 | le16_to_cpus(&devstat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
| 4119 | dev_err(&udev->dev, |
| 4120 | "can't connect bus-powered hub " |
| 4121 | "to this port\n"); |
| 4122 | if (hub->has_indicators) { |
| 4123 | hub->indicator[port1-1] = |
| 4124 | INDICATOR_AMBER_BLINK; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 4125 | schedule_delayed_work (&hub->leds, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | } |
| 4127 | status = -ENOTCONN; /* Don't retry */ |
| 4128 | goto loop_disable; |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | /* check for devices running slower than they could */ |
| 4133 | if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 |
| 4134 | && udev->speed == USB_SPEED_FULL |
| 4135 | && highspeed_hubs != 0) |
| 4136 | check_highspeed (hub, udev, port1); |
| 4137 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4138 | /* Store the parent's children[] pointer. At this point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | * udev becomes globally accessible, although presumably |
| 4140 | * no one will look at it until hdev is unlocked. |
| 4141 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | status = 0; |
| 4143 | |
| 4144 | /* We mustn't add new devices if the parent hub has |
| 4145 | * been disconnected; we would race with the |
| 4146 | * recursively_mark_NOTATTACHED() routine. |
| 4147 | */ |
| 4148 | spin_lock_irq(&device_state_lock); |
| 4149 | if (hdev->state == USB_STATE_NOTATTACHED) |
| 4150 | status = -ENOTCONN; |
| 4151 | else |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4152 | hdev->children[port1-1] = udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | spin_unlock_irq(&device_state_lock); |
| 4154 | |
| 4155 | /* Run it through the hoops (find a driver, etc) */ |
| 4156 | if (!status) { |
| 4157 | status = usb_new_device(udev); |
| 4158 | if (status) { |
| 4159 | spin_lock_irq(&device_state_lock); |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4160 | hdev->children[port1-1] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | spin_unlock_irq(&device_state_lock); |
| 4162 | } |
| 4163 | } |
| 4164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | if (status) |
| 4166 | goto loop_disable; |
| 4167 | |
| 4168 | status = hub_power_remaining(hub); |
| 4169 | if (status) |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4170 | dev_dbg(hub_dev, "%dmA power budget left\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | |
| 4172 | return; |
| 4173 | |
| 4174 | loop_disable: |
| 4175 | hub_port_disable(hub, port1, 1); |
| 4176 | loop: |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4177 | usb_ep0_reinit(udev); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4178 | release_devnum(udev); |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 4179 | hub_free_dev(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | usb_put_dev(udev); |
Vikram Pandita | ffcdc18 | 2007-05-25 21:31:07 -0700 | [diff] [blame] | 4181 | if ((status == -ENOTCONN) || (status == -ENOTSUPP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | break; |
| 4183 | } |
Alan Stern | 3a31155 | 2008-05-20 16:58:29 -0400 | [diff] [blame] | 4184 | if (hub->hdev->parent || |
| 4185 | !hcd->driver->port_handed_over || |
| 4186 | !(hcd->driver->port_handed_over)(hcd, port1)) |
| 4187 | dev_err(hub_dev, "unable to enumerate USB device on port %d\n", |
| 4188 | port1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | |
| 4190 | done: |
| 4191 | hub_port_disable(hub, port1, 1); |
Balaji Rao | 90da096 | 2007-11-22 01:58:14 +0530 | [diff] [blame] | 4192 | if (hcd->driver->relinquish_port && !hub->hdev->parent) |
| 4193 | hcd->driver->relinquish_port(hcd, port1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4196 | /* Returns 1 if there was a remote wakeup and a connect status change. */ |
| 4197 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 4198 | u16 portstatus, u16 portchange) |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4199 | { |
| 4200 | struct usb_device *hdev; |
| 4201 | struct usb_device *udev; |
| 4202 | int connect_change = 0; |
| 4203 | int ret; |
| 4204 | |
| 4205 | hdev = hub->hdev; |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4206 | udev = hdev->children[port-1]; |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 4207 | if (!hub_is_superspeed(hdev)) { |
| 4208 | if (!(portchange & USB_PORT_STAT_C_SUSPEND)) |
| 4209 | return 0; |
| 4210 | clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); |
| 4211 | } else { |
| 4212 | if (!udev || udev->state != USB_STATE_SUSPENDED || |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 4213 | (portstatus & USB_PORT_STAT_LINK_STATE) != |
| 4214 | USB_SS_PORT_LS_U0) |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 4215 | return 0; |
| 4216 | } |
| 4217 | |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4218 | if (udev) { |
| 4219 | /* TRSMRCY = 10 msec */ |
| 4220 | msleep(10); |
| 4221 | |
| 4222 | usb_lock_device(udev); |
| 4223 | ret = usb_remote_wakeup(udev); |
| 4224 | usb_unlock_device(udev); |
| 4225 | if (ret < 0) |
| 4226 | connect_change = 1; |
| 4227 | } else { |
| 4228 | ret = -ENODEV; |
| 4229 | hub_port_disable(hub, port, 1); |
| 4230 | } |
| 4231 | dev_dbg(hub->intfdev, "resume on port %d, status %d\n", |
| 4232 | port, ret); |
| 4233 | return connect_change; |
| 4234 | } |
| 4235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | static void hub_events(void) |
| 4237 | { |
| 4238 | struct list_head *tmp; |
| 4239 | struct usb_device *hdev; |
| 4240 | struct usb_interface *intf; |
| 4241 | struct usb_hub *hub; |
| 4242 | struct device *hub_dev; |
| 4243 | u16 hubstatus; |
| 4244 | u16 hubchange; |
| 4245 | u16 portstatus; |
| 4246 | u16 portchange; |
| 4247 | int i, ret; |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 4248 | int connect_change, wakeup_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | |
| 4250 | /* |
| 4251 | * We restart the list every time to avoid a deadlock with |
| 4252 | * deleting hubs downstream from this one. This should be |
| 4253 | * safe since we delete the hub from the event list. |
| 4254 | * Not the most efficient, but avoids deadlocks. |
| 4255 | */ |
| 4256 | while (1) { |
| 4257 | |
| 4258 | /* Grab the first entry at the beginning of the list */ |
| 4259 | spin_lock_irq(&hub_event_lock); |
| 4260 | if (list_empty(&hub_event_list)) { |
| 4261 | spin_unlock_irq(&hub_event_lock); |
| 4262 | break; |
| 4263 | } |
| 4264 | |
| 4265 | tmp = hub_event_list.next; |
| 4266 | list_del_init(tmp); |
| 4267 | |
| 4268 | hub = list_entry(tmp, struct usb_hub, event_list); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 4269 | kref_get(&hub->kref); |
| 4270 | spin_unlock_irq(&hub_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 4272 | hdev = hub->hdev; |
| 4273 | hub_dev = hub->intfdev; |
| 4274 | intf = to_usb_interface(hub_dev); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4275 | dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | hdev->state, hub->descriptor |
| 4277 | ? hub->descriptor->bNbrPorts |
| 4278 | : 0, |
| 4279 | /* NOTE: expects max 15 ports... */ |
| 4280 | (u16) hub->change_bits[0], |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4281 | (u16) hub->event_bits[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | /* Lock the device, then check to see if we were |
| 4284 | * disconnected while waiting for the lock to succeed. */ |
Alan Stern | 06b84e8 | 2007-05-04 11:54:50 -0400 | [diff] [blame] | 4285 | usb_lock_device(hdev); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 4286 | if (unlikely(hub->disconnected)) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 4287 | goto loop_disconnected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | |
| 4289 | /* If the hub has died, clean up after it */ |
| 4290 | if (hdev->state == USB_STATE_NOTATTACHED) { |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 4291 | hub->error = -ENODEV; |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 4292 | hub_quiesce(hub, HUB_DISCONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | goto loop; |
| 4294 | } |
| 4295 | |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4296 | /* Autoresume */ |
| 4297 | ret = usb_autopm_get_interface(intf); |
| 4298 | if (ret) { |
| 4299 | dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | goto loop; |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | /* If this is an inactive hub, do nothing */ |
| 4304 | if (hub->quiescing) |
| 4305 | goto loop_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
| 4307 | if (hub->error) { |
| 4308 | dev_dbg (hub_dev, "resetting for error %d\n", |
| 4309 | hub->error); |
| 4310 | |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4311 | ret = usb_reset_device(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | if (ret) { |
| 4313 | dev_dbg (hub_dev, |
| 4314 | "error resetting hub: %d\n", ret); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4315 | goto loop_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | hub->nerrors = 0; |
| 4319 | hub->error = 0; |
| 4320 | } |
| 4321 | |
| 4322 | /* deal with port status changes */ |
| 4323 | for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { |
| 4324 | if (test_bit(i, hub->busy_bits)) |
| 4325 | continue; |
| 4326 | connect_change = test_bit(i, hub->change_bits); |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 4327 | wakeup_change = test_and_clear_bit(i, hub->wakeup_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | if (!test_and_clear_bit(i, hub->event_bits) && |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 4329 | !connect_change && !wakeup_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | continue; |
| 4331 | |
| 4332 | ret = hub_port_status(hub, i, |
| 4333 | &portstatus, &portchange); |
| 4334 | if (ret < 0) |
| 4335 | continue; |
| 4336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
| 4338 | clear_port_feature(hdev, i, |
| 4339 | USB_PORT_FEAT_C_CONNECTION); |
| 4340 | connect_change = 1; |
| 4341 | } |
| 4342 | |
| 4343 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
| 4344 | if (!connect_change) |
| 4345 | dev_dbg (hub_dev, |
| 4346 | "port %d enable change, " |
| 4347 | "status %08x\n", |
| 4348 | i, portstatus); |
| 4349 | clear_port_feature(hdev, i, |
| 4350 | USB_PORT_FEAT_C_ENABLE); |
| 4351 | |
| 4352 | /* |
| 4353 | * EM interference sometimes causes badly |
| 4354 | * shielded USB devices to be shutdown by |
| 4355 | * the hub, this hack enables them again. |
| 4356 | * Works at least with mouse driver. |
| 4357 | */ |
| 4358 | if (!(portstatus & USB_PORT_STAT_ENABLE) |
| 4359 | && !connect_change |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4360 | && hdev->children[i-1]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | dev_err (hub_dev, |
| 4362 | "port %i " |
| 4363 | "disabled by hub (EMI?), " |
| 4364 | "re-enabling...\n", |
| 4365 | i); |
| 4366 | connect_change = 1; |
| 4367 | } |
| 4368 | } |
| 4369 | |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 4370 | if (hub_handle_remote_wakeup(hub, i, |
| 4371 | portstatus, portchange)) |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4372 | connect_change = 1; |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4375 | u16 status = 0; |
| 4376 | u16 unused; |
| 4377 | |
| 4378 | dev_dbg(hub_dev, "over-current change on port " |
| 4379 | "%d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | clear_port_feature(hdev, i, |
| 4381 | USB_PORT_FEAT_C_OVER_CURRENT); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4382 | msleep(100); /* Cool down */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 4383 | hub_power_on(hub, true); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4384 | hub_port_status(hub, i, &status, &unused); |
| 4385 | if (status & USB_PORT_STAT_OVERCURRENT) |
| 4386 | dev_err(hub_dev, "over-current " |
| 4387 | "condition on port %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if (portchange & USB_PORT_STAT_C_RESET) { |
| 4391 | dev_dbg (hub_dev, |
| 4392 | "reset change on port %d\n", |
| 4393 | i); |
| 4394 | clear_port_feature(hdev, i, |
| 4395 | USB_PORT_FEAT_C_RESET); |
| 4396 | } |
Sarah Sharp | c706157 | 2010-12-06 15:08:20 -0800 | [diff] [blame] | 4397 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
| 4398 | hub_is_superspeed(hub->hdev)) { |
| 4399 | dev_dbg(hub_dev, |
| 4400 | "warm reset change on port %d\n", |
| 4401 | i); |
| 4402 | clear_port_feature(hdev, i, |
| 4403 | USB_PORT_FEAT_C_BH_PORT_RESET); |
| 4404 | } |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 4405 | if (portchange & USB_PORT_STAT_C_LINK_STATE) { |
| 4406 | clear_port_feature(hub->hdev, i, |
| 4407 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 4408 | } |
| 4409 | if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { |
| 4410 | dev_warn(hub_dev, |
| 4411 | "config error on port %d\n", |
| 4412 | i); |
| 4413 | clear_port_feature(hub->hdev, i, |
| 4414 | USB_PORT_FEAT_C_PORT_CONFIG_ERROR); |
| 4415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | |
Andiry Xu | 5e467f6 | 2011-04-27 18:07:54 +0800 | [diff] [blame] | 4417 | /* Warm reset a USB3 protocol port if it's in |
| 4418 | * SS.Inactive state. |
| 4419 | */ |
| 4420 | if (hub_is_superspeed(hub->hdev) && |
| 4421 | (portstatus & USB_PORT_STAT_LINK_STATE) |
| 4422 | == USB_SS_PORT_LS_SS_INACTIVE) { |
| 4423 | dev_dbg(hub_dev, "warm reset port %d\n", i); |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 4424 | hub_port_reset(hub, i, NULL, |
| 4425 | HUB_BH_RESET_TIME, true); |
Andiry Xu | 5e467f6 | 2011-04-27 18:07:54 +0800 | [diff] [blame] | 4426 | } |
| 4427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | if (connect_change) |
| 4429 | hub_port_connect_change(hub, i, |
| 4430 | portstatus, portchange); |
| 4431 | } /* end for i */ |
| 4432 | |
| 4433 | /* deal with hub status changes */ |
| 4434 | if (test_and_clear_bit(0, hub->event_bits) == 0) |
| 4435 | ; /* do nothing */ |
| 4436 | else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) |
| 4437 | dev_err (hub_dev, "get_hub_status failed\n"); |
| 4438 | else { |
| 4439 | if (hubchange & HUB_CHANGE_LOCAL_POWER) { |
| 4440 | dev_dbg (hub_dev, "power change\n"); |
| 4441 | clear_hub_feature(hdev, C_HUB_LOCAL_POWER); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4442 | if (hubstatus & HUB_STATUS_LOCAL_POWER) |
| 4443 | /* FIXME: Is this always true? */ |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4444 | hub->limited_power = 1; |
jidong xiao | 403fae7 | 2007-09-14 00:08:51 +0800 | [diff] [blame] | 4445 | else |
| 4446 | hub->limited_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | } |
| 4448 | if (hubchange & HUB_CHANGE_OVERCURRENT) { |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4449 | u16 status = 0; |
| 4450 | u16 unused; |
| 4451 | |
| 4452 | dev_dbg(hub_dev, "over-current change\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | clear_hub_feature(hdev, C_HUB_OVER_CURRENT); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4454 | msleep(500); /* Cool down */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 4455 | hub_power_on(hub, true); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 4456 | hub_hub_status(hub, &status, &unused); |
| 4457 | if (status & HUB_STATUS_OVERCURRENT) |
| 4458 | dev_err(hub_dev, "over-current " |
| 4459 | "condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 4463 | loop_autopm: |
| 4464 | /* Balance the usb_autopm_get_interface() above */ |
| 4465 | usb_autopm_put_interface_no_suspend(intf); |
| 4466 | loop: |
| 4467 | /* Balance the usb_autopm_get_interface_no_resume() in |
| 4468 | * kick_khubd() and allow autosuspend. |
| 4469 | */ |
| 4470 | usb_autopm_put_interface(intf); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 4471 | loop_disconnected: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | usb_unlock_device(hdev); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 4473 | kref_put(&hub->kref, hub_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | |
| 4475 | } /* end while (1) */ |
| 4476 | } |
| 4477 | |
| 4478 | static int hub_thread(void *__unused) |
| 4479 | { |
Alan Stern | 3bb1af5 | 2008-03-03 15:15:36 -0500 | [diff] [blame] | 4480 | /* khubd needs to be freezable to avoid intefering with USB-PERSIST |
| 4481 | * port handover. Otherwise it might see that a full-speed device |
| 4482 | * was gone before the EHCI controller had handed its port over to |
| 4483 | * the companion full-speed controller. |
| 4484 | */ |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 4485 | set_freezable(); |
Alan Stern | 3bb1af5 | 2008-03-03 15:15:36 -0500 | [diff] [blame] | 4486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | do { |
| 4488 | hub_events(); |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 4489 | wait_event_freezable(khubd_wait, |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 4490 | !list_empty(&hub_event_list) || |
| 4491 | kthread_should_stop()); |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 4492 | } while (!kthread_should_stop() || !list_empty(&hub_event_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 4494 | pr_debug("%s: khubd exiting\n", usbcore_name); |
| 4495 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Németh Márton | 1e927d9 | 2010-01-10 15:34:53 +0100 | [diff] [blame] | 4498 | static const struct usb_device_id hub_id_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, |
| 4500 | .bDeviceClass = USB_CLASS_HUB}, |
| 4501 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, |
| 4502 | .bInterfaceClass = USB_CLASS_HUB}, |
| 4503 | { } /* Terminating entry */ |
| 4504 | }; |
| 4505 | |
| 4506 | MODULE_DEVICE_TABLE (usb, hub_id_table); |
| 4507 | |
| 4508 | static struct usb_driver hub_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | .name = "hub", |
| 4510 | .probe = hub_probe, |
| 4511 | .disconnect = hub_disconnect, |
| 4512 | .suspend = hub_suspend, |
| 4513 | .resume = hub_resume, |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 4514 | .reset_resume = hub_reset_resume, |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 4515 | .pre_reset = hub_pre_reset, |
| 4516 | .post_reset = hub_post_reset, |
Andi Kleen | c532b29 | 2010-06-01 23:04:41 +0200 | [diff] [blame] | 4517 | .unlocked_ioctl = hub_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | .id_table = hub_id_table, |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 4519 | .supports_autosuspend = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | }; |
| 4521 | |
| 4522 | int usb_hub_init(void) |
| 4523 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | if (usb_register(&hub_driver) < 0) { |
| 4525 | printk(KERN_ERR "%s: can't register hub driver\n", |
| 4526 | usbcore_name); |
| 4527 | return -1; |
| 4528 | } |
| 4529 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 4530 | khubd_task = kthread_run(hub_thread, NULL, "khubd"); |
| 4531 | if (!IS_ERR(khubd_task)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | |
| 4534 | /* Fall through if kernel_thread failed */ |
| 4535 | usb_deregister(&hub_driver); |
| 4536 | printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); |
| 4537 | |
| 4538 | return -1; |
| 4539 | } |
| 4540 | |
| 4541 | void usb_hub_cleanup(void) |
| 4542 | { |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 4543 | kthread_stop(khubd_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
| 4545 | /* |
| 4546 | * Hub resources are freed for us by usb_deregister. It calls |
| 4547 | * usb_driver_purge on every device which in turn calls that |
| 4548 | * devices disconnect function if it is using this driver. |
| 4549 | * The hub_disconnect function takes care of releasing the |
| 4550 | * individual hub resources. -greg |
| 4551 | */ |
| 4552 | usb_deregister(&hub_driver); |
| 4553 | } /* usb_hub_cleanup() */ |
| 4554 | |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4555 | static int descriptors_changed(struct usb_device *udev, |
| 4556 | struct usb_device_descriptor *old_device_descriptor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4558 | int changed = 0; |
| 4559 | unsigned index; |
| 4560 | unsigned serial_len = 0; |
| 4561 | unsigned len; |
| 4562 | unsigned old_length; |
| 4563 | int length; |
| 4564 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4566 | if (memcmp(&udev->descriptor, old_device_descriptor, |
| 4567 | sizeof(*old_device_descriptor)) != 0) |
| 4568 | return 1; |
| 4569 | |
| 4570 | /* Since the idVendor, idProduct, and bcdDevice values in the |
| 4571 | * device descriptor haven't changed, we will assume the |
| 4572 | * Manufacturer and Product strings haven't changed either. |
| 4573 | * But the SerialNumber string could be different (e.g., a |
| 4574 | * different flash card of the same brand). |
| 4575 | */ |
| 4576 | if (udev->serial) |
| 4577 | serial_len = strlen(udev->serial) + 1; |
| 4578 | |
| 4579 | len = serial_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4581 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
| 4582 | len = max(len, old_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | } |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4584 | |
Oliver Neukum | 0cc1a51 | 2008-01-10 10:31:48 +0100 | [diff] [blame] | 4585 | buf = kmalloc(len, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | if (buf == NULL) { |
| 4587 | dev_err(&udev->dev, "no mem to re-read configs after reset\n"); |
| 4588 | /* assume the worst */ |
| 4589 | return 1; |
| 4590 | } |
| 4591 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4592 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
| 4594 | old_length); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4595 | if (length != old_length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | dev_dbg(&udev->dev, "config index %d, error %d\n", |
| 4597 | index, length); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4598 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | break; |
| 4600 | } |
| 4601 | if (memcmp (buf, udev->rawdescriptors[index], old_length) |
| 4602 | != 0) { |
| 4603 | dev_dbg(&udev->dev, "config index %d changed (#%d)\n", |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4604 | index, |
| 4605 | ((struct usb_config_descriptor *) buf)-> |
| 4606 | bConfigurationValue); |
| 4607 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | break; |
| 4609 | } |
| 4610 | } |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4611 | |
| 4612 | if (!changed && serial_len) { |
| 4613 | length = usb_string(udev, udev->descriptor.iSerialNumber, |
| 4614 | buf, serial_len); |
| 4615 | if (length + 1 != serial_len) { |
| 4616 | dev_dbg(&udev->dev, "serial string error %d\n", |
| 4617 | length); |
| 4618 | changed = 1; |
| 4619 | } else if (memcmp(buf, udev->serial, length) != 0) { |
| 4620 | dev_dbg(&udev->dev, "serial string changed\n"); |
| 4621 | changed = 1; |
| 4622 | } |
| 4623 | } |
| 4624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | kfree(buf); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4626 | return changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | /** |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4630 | * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
| 4632 | * |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4633 | * WARNING - don't use this routine to reset a composite device |
| 4634 | * (one with multiple interfaces owned by separate drivers)! |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4635 | * Use usb_reset_device() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | * |
| 4637 | * Do a port reset, reassign the device's address, and establish its |
| 4638 | * former operating configuration. If the reset fails, or the device's |
| 4639 | * descriptors change from their values before the reset, or the original |
| 4640 | * configuration and altsettings cannot be restored, a flag will be set |
| 4641 | * telling khubd to pretend the device has been disconnected and then |
| 4642 | * re-connected. All drivers will be unbound, and the device will be |
| 4643 | * re-enumerated and probed all over again. |
| 4644 | * |
| 4645 | * Returns 0 if the reset succeeded, -ENODEV if the device has been |
| 4646 | * flagged for logical disconnection, or some other negative error code |
| 4647 | * if the reset wasn't even attempted. |
| 4648 | * |
| 4649 | * The caller must own the device lock. For example, it's safe to use |
| 4650 | * this from a driver probe() routine after downloading new firmware. |
| 4651 | * For calls that might not occur during probe(), drivers should lock |
| 4652 | * the device using usb_lock_device_for_reset(). |
Alan Stern | 6bc6cff | 2007-05-04 11:53:03 -0400 | [diff] [blame] | 4653 | * |
| 4654 | * Locking exception: This routine may also be called from within an |
| 4655 | * autoresume handler. Such usage won't conflict with other tasks |
| 4656 | * holding the device lock because these tasks should always call |
| 4657 | * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | */ |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4659 | static int usb_reset_and_verify_device(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | { |
| 4661 | struct usb_device *parent_hdev = udev->parent; |
| 4662 | struct usb_hub *parent_hub; |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4663 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | struct usb_device_descriptor descriptor = udev->descriptor; |
Alan Stern | 12c3da3 | 2005-11-23 12:09:52 -0500 | [diff] [blame] | 4665 | int i, ret = 0; |
| 4666 | int port1 = udev->portnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | |
| 4668 | if (udev->state == USB_STATE_NOTATTACHED || |
| 4669 | udev->state == USB_STATE_SUSPENDED) { |
| 4670 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", |
| 4671 | udev->state); |
| 4672 | return -EINVAL; |
| 4673 | } |
| 4674 | |
| 4675 | if (!parent_hdev) { |
Wolfram Sang | 4bec991 | 2010-08-29 18:17:14 +0200 | [diff] [blame] | 4676 | /* this requires hcd-specific logic; see ohci_restart() */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 4677 | dev_dbg(&udev->dev, "%s for root hub!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | return -EISDIR; |
| 4679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | parent_hub = hdev_to_hub(parent_hdev); |
| 4681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | set_bit(port1, parent_hub->busy_bits); |
| 4683 | for (i = 0; i < SET_CONFIG_TRIES; ++i) { |
| 4684 | |
| 4685 | /* ep0 maxpacket size may change; let the HCD know about it. |
| 4686 | * Other endpoints will be handled by re-enumeration. */ |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4687 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | ret = hub_port_init(parent_hub, udev, port1, i); |
Alan Stern | dd4dd19 | 2007-05-04 11:55:54 -0400 | [diff] [blame] | 4689 | if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | break; |
| 4691 | } |
| 4692 | clear_bit(port1, parent_hub->busy_bits); |
Alan Stern | d5cbad4 | 2006-08-11 16:52:39 -0400 | [diff] [blame] | 4693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | if (ret < 0) |
| 4695 | goto re_enumerate; |
| 4696 | |
| 4697 | /* Device might have changed firmware (DFU or similar) */ |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 4698 | if (descriptors_changed(udev, &descriptor)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | dev_info(&udev->dev, "device firmware changed\n"); |
| 4700 | udev->descriptor = descriptor; /* for disconnect() calls */ |
| 4701 | goto re_enumerate; |
| 4702 | } |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 4703 | |
| 4704 | /* Restore the device's previous configuration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | if (!udev->actconfig) |
| 4706 | goto done; |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4707 | |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 4708 | mutex_lock(hcd->bandwidth_mutex); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 4709 | /* Disable LPM while we reset the device and reinstall the alt settings. |
| 4710 | * Device-initiated LPM settings, and system exit latency settings are |
| 4711 | * cleared when the device is reset, so we have to set them up again. |
| 4712 | */ |
| 4713 | ret = usb_disable_lpm(udev); |
| 4714 | if (ret) { |
| 4715 | dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); |
| 4716 | mutex_unlock(hcd->bandwidth_mutex); |
| 4717 | goto done; |
| 4718 | } |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4719 | ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); |
| 4720 | if (ret < 0) { |
| 4721 | dev_warn(&udev->dev, |
| 4722 | "Busted HC? Not enough HCD resources for " |
| 4723 | "old configuration.\n"); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 4724 | usb_enable_lpm(udev); |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 4725 | mutex_unlock(hcd->bandwidth_mutex); |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4726 | goto re_enumerate; |
| 4727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 4729 | USB_REQ_SET_CONFIGURATION, 0, |
| 4730 | udev->actconfig->desc.bConfigurationValue, 0, |
| 4731 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 4732 | if (ret < 0) { |
| 4733 | dev_err(&udev->dev, |
| 4734 | "can't restore configuration #%d (error=%d)\n", |
| 4735 | udev->actconfig->desc.bConfigurationValue, ret); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 4736 | usb_enable_lpm(udev); |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 4737 | mutex_unlock(hcd->bandwidth_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | goto re_enumerate; |
| 4739 | } |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 4740 | mutex_unlock(hcd->bandwidth_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | usb_set_device_state(udev, USB_STATE_CONFIGURED); |
| 4742 | |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 4743 | /* Put interfaces back into the same altsettings as before. |
| 4744 | * Don't bother to send the Set-Interface request for interfaces |
| 4745 | * that were already in altsetting 0; besides being unnecessary, |
| 4746 | * many devices can't handle it. Instead just reset the host-side |
| 4747 | * endpoint state. |
| 4748 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4750 | struct usb_host_config *config = udev->actconfig; |
| 4751 | struct usb_interface *intf = config->interface[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | struct usb_interface_descriptor *desc; |
| 4753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | desc = &intf->cur_altsetting->desc; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 4755 | if (desc->bAlternateSetting == 0) { |
| 4756 | usb_disable_interface(udev, intf, true); |
| 4757 | usb_enable_interface(udev, intf, true); |
| 4758 | ret = 0; |
| 4759 | } else { |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 4760 | /* Let the bandwidth allocation function know that this |
| 4761 | * device has been reset, and it will have to use |
| 4762 | * alternate setting 0 as the current alternate setting. |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 4763 | */ |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 4764 | intf->resetting_device = 1; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 4765 | ret = usb_set_interface(udev, desc->bInterfaceNumber, |
| 4766 | desc->bAlternateSetting); |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 4767 | intf->resetting_device = 0; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 4768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | if (ret < 0) { |
| 4770 | dev_err(&udev->dev, "failed to restore interface %d " |
| 4771 | "altsetting %d (error=%d)\n", |
| 4772 | desc->bInterfaceNumber, |
| 4773 | desc->bAlternateSetting, |
| 4774 | ret); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 4775 | usb_unlocked_enable_lpm(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | goto re_enumerate; |
| 4777 | } |
| 4778 | } |
| 4779 | |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 4780 | /* Now that the alt settings are re-installed, enable LPM. */ |
| 4781 | usb_unlocked_enable_lpm(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | return 0; |
| 4784 | |
| 4785 | re_enumerate: |
| 4786 | hub_port_logical_disconnect(parent_hub, port1); |
| 4787 | return -ENODEV; |
| 4788 | } |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4789 | |
| 4790 | /** |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4791 | * usb_reset_device - warn interface drivers and perform a USB port reset |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4792 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4793 | * |
| 4794 | * Warns all drivers bound to registered interfaces (using their pre_reset |
| 4795 | * method), performs the port reset, and then lets the drivers know that |
| 4796 | * the reset is over (using their post_reset method). |
| 4797 | * |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4798 | * Return value is the same as for usb_reset_and_verify_device(). |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4799 | * |
| 4800 | * The caller must own the device lock. For example, it's safe to use |
| 4801 | * this from a driver probe() routine after downloading new firmware. |
| 4802 | * For calls that might not occur during probe(), drivers should lock |
| 4803 | * the device using usb_lock_device_for_reset(). |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4804 | * |
| 4805 | * If an interface is currently being probed or disconnected, we assume |
| 4806 | * its driver knows how to handle resets. For all other interfaces, |
| 4807 | * if the driver doesn't have pre_reset and post_reset methods then |
| 4808 | * we attempt to unbind it and rebind afterward. |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4809 | */ |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4810 | int usb_reset_device(struct usb_device *udev) |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4811 | { |
| 4812 | int ret; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 4813 | int i; |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4814 | struct usb_host_config *config = udev->actconfig; |
| 4815 | |
| 4816 | if (udev->state == USB_STATE_NOTATTACHED || |
| 4817 | udev->state == USB_STATE_SUSPENDED) { |
| 4818 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", |
| 4819 | udev->state); |
| 4820 | return -EINVAL; |
| 4821 | } |
| 4822 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 4823 | /* Prevent autosuspend during the reset */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 4824 | usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 4825 | |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4826 | if (config) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4827 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 4828 | struct usb_interface *cintf = config->interface[i]; |
| 4829 | struct usb_driver *drv; |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4830 | int unbind = 0; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 4831 | |
| 4832 | if (cintf->dev.driver) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4833 | drv = to_usb_driver(cintf->dev.driver); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4834 | if (drv->pre_reset && drv->post_reset) |
| 4835 | unbind = (drv->pre_reset)(cintf); |
| 4836 | else if (cintf->condition == |
| 4837 | USB_INTERFACE_BOUND) |
| 4838 | unbind = 1; |
| 4839 | if (unbind) |
| 4840 | usb_forced_unbind_intf(cintf); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | } |
| 4844 | |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4845 | ret = usb_reset_and_verify_device(udev); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4846 | |
| 4847 | if (config) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4848 | for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 4849 | struct usb_interface *cintf = config->interface[i]; |
| 4850 | struct usb_driver *drv; |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4851 | int rebind = cintf->needs_binding; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 4852 | |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4853 | if (!rebind && cintf->dev.driver) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4854 | drv = to_usb_driver(cintf->dev.driver); |
| 4855 | if (drv->post_reset) |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4856 | rebind = (drv->post_reset)(cintf); |
| 4857 | else if (cintf->condition == |
| 4858 | USB_INTERFACE_BOUND) |
| 4859 | rebind = 1; |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4860 | } |
Alan Stern | 6c64094 | 2008-10-21 15:40:03 -0400 | [diff] [blame] | 4861 | if (ret == 0 && rebind) |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 4862 | usb_rebind_intf(cintf); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4863 | } |
| 4864 | } |
| 4865 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 4866 | usb_autosuspend_device(udev); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 4867 | return ret; |
| 4868 | } |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 4869 | EXPORT_SYMBOL_GPL(usb_reset_device); |
Inaky Perez-Gonzalez | dc023dc | 2008-11-13 10:31:35 -0800 | [diff] [blame] | 4870 | |
| 4871 | |
| 4872 | /** |
| 4873 | * usb_queue_reset_device - Reset a USB device from an atomic context |
| 4874 | * @iface: USB interface belonging to the device to reset |
| 4875 | * |
| 4876 | * This function can be used to reset a USB device from an atomic |
| 4877 | * context, where usb_reset_device() won't work (as it blocks). |
| 4878 | * |
| 4879 | * Doing a reset via this method is functionally equivalent to calling |
| 4880 | * usb_reset_device(), except for the fact that it is delayed to a |
| 4881 | * workqueue. This means that any drivers bound to other interfaces |
| 4882 | * might be unbound, as well as users from usbfs in user space. |
| 4883 | * |
| 4884 | * Corner cases: |
| 4885 | * |
| 4886 | * - Scheduling two resets at the same time from two different drivers |
| 4887 | * attached to two different interfaces of the same device is |
| 4888 | * possible; depending on how the driver attached to each interface |
| 4889 | * handles ->pre_reset(), the second reset might happen or not. |
| 4890 | * |
| 4891 | * - If a driver is unbound and it had a pending reset, the reset will |
| 4892 | * be cancelled. |
| 4893 | * |
| 4894 | * - This function can be called during .probe() or .disconnect() |
| 4895 | * times. On return from .disconnect(), any pending resets will be |
| 4896 | * cancelled. |
| 4897 | * |
| 4898 | * There is no no need to lock/unlock the @reset_ws as schedule_work() |
| 4899 | * does its own. |
| 4900 | * |
| 4901 | * NOTE: We don't do any reference count tracking because it is not |
| 4902 | * needed. The lifecycle of the work_struct is tied to the |
| 4903 | * usb_interface. Before destroying the interface we cancel the |
| 4904 | * work_struct, so the fact that work_struct is queued and or |
| 4905 | * running means the interface (and thus, the device) exist and |
| 4906 | * are referenced. |
| 4907 | */ |
| 4908 | void usb_queue_reset_device(struct usb_interface *iface) |
| 4909 | { |
| 4910 | schedule_work(&iface->reset_ws); |
| 4911 | } |
| 4912 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); |