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