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