PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/core/hub.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 4051 lines | 2699 code | 486 blank | 866 comment | 636 complexity | 391473d5e0cab8c688aa1a8fac051e4a MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/completion.h>
  15. #include <linux/sched.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/usb.h>
  20. #include <linux/usbdevice_fs.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/usb/quirks.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/freezer.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/byteorder.h>
  28. #include "usb.h"
  29. /* if we are in debug mode, always announce new devices */
  30. #ifdef DEBUG
  31. #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  32. #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
  33. #endif
  34. #endif
  35. struct usb_hub {
  36. struct device *intfdev; /* the "interface" device */
  37. struct usb_device *hdev;
  38. struct kref kref;
  39. struct urb *urb; /* for interrupt polling pipe */
  40. /* buffer for urb ... with extra space in case of babble */
  41. char (*buffer)[8];
  42. union {
  43. struct usb_hub_status hub;
  44. struct usb_port_status port;
  45. } *status; /* buffer for status reports */
  46. struct mutex status_mutex; /* for the status buffer */
  47. int error; /* last reported error */
  48. int nerrors; /* track consecutive errors */
  49. struct list_head event_list; /* hubs w/data or errs ready */
  50. unsigned long event_bits[1]; /* status change bitmask */
  51. unsigned long change_bits[1]; /* ports with logical connect
  52. status change */
  53. unsigned long busy_bits[1]; /* ports being reset or
  54. resumed */
  55. unsigned long removed_bits[1]; /* ports with a "removed"
  56. device present */
  57. #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
  58. #error event_bits[] is too short!
  59. #endif
  60. struct usb_hub_descriptor *descriptor; /* class descriptor */
  61. struct usb_tt tt; /* Transaction Translator */
  62. unsigned mA_per_port; /* current for each child */
  63. unsigned limited_power:1;
  64. unsigned quiescing:1;
  65. unsigned disconnected:1;
  66. unsigned has_indicators:1;
  67. u8 indicator[USB_MAXCHILDREN];
  68. struct delayed_work leds;
  69. struct delayed_work init_work;
  70. void **port_owners;
  71. };
  72. static inline int hub_is_superspeed(struct usb_device *hdev)
  73. {
  74. return (hdev->descriptor.bDeviceProtocol == 3);
  75. }
  76. /* Protect struct usb_device->state and ->children members
  77. * Note: Both are also protected by ->dev.sem, except that ->state can
  78. * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
  79. static DEFINE_SPINLOCK(device_state_lock);
  80. /* khubd's worklist and its lock */
  81. static DEFINE_SPINLOCK(hub_event_lock);
  82. static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
  83. /* Wakes up khubd */
  84. static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
  85. static struct task_struct *khubd_task;
  86. /* cycle leds on hubs that aren't blinking for attention */
  87. static int blinkenlights = 0;
  88. module_param (blinkenlights, bool, S_IRUGO);
  89. MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
  90. /*
  91. * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
  92. * 10 seconds to send reply for the initial 64-byte descriptor request.
  93. */
  94. /* define initial 64-byte descriptor request timeout in milliseconds */
  95. static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
  96. module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
  97. MODULE_PARM_DESC(initial_descriptor_timeout,
  98. "initial 64-byte descriptor request timeout in milliseconds "
  99. "(default 5000 - 5.0 seconds)");
  100. /*
  101. * As of 2.6.10 we introduce a new USB device initialization scheme which
  102. * closely resembles the way Windows works. Hopefully it will be compatible
  103. * with a wider range of devices than the old scheme. However some previously
  104. * working devices may start giving rise to "device not accepting address"
  105. * errors; if that happens the user can try the old scheme by adjusting the
  106. * following module parameters.
  107. *
  108. * For maximum flexibility there are two boolean parameters to control the
  109. * hub driver's behavior. On the first initialization attempt, if the
  110. * "old_scheme_first" parameter is set then the old scheme will be used,
  111. * otherwise the new scheme is used. If that fails and "use_both_schemes"
  112. * is set, then the driver will make another attempt, using the other scheme.
  113. */
  114. static int old_scheme_first = 0;
  115. module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
  116. MODULE_PARM_DESC(old_scheme_first,
  117. "start with the old device initialization scheme");
  118. static int use_both_schemes = 1;
  119. module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
  120. MODULE_PARM_DESC(use_both_schemes,
  121. "try the other device initialization scheme if the "
  122. "first one fails");
  123. /* Mutual exclusion for EHCI CF initialization. This interferes with
  124. * port reset on some companion controllers.
  125. */
  126. DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
  127. EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
  128. #define HUB_DEBOUNCE_TIMEOUT 1500
  129. #define HUB_DEBOUNCE_STEP 25
  130. #define HUB_DEBOUNCE_STABLE 100
  131. static int usb_reset_and_verify_device(struct usb_device *udev);
  132. static inline char *portspeed(struct usb_hub *hub, int portstatus)
  133. {
  134. if (hub_is_superspeed(hub->hdev))
  135. return "5.0 Gb/s";
  136. if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  137. return "480 Mb/s";
  138. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  139. return "1.5 Mb/s";
  140. else
  141. return "12 Mb/s";
  142. }
  143. /* Note that hdev or one of its children must be locked! */
  144. static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
  145. {
  146. if (!hdev || !hdev->actconfig)
  147. return NULL;
  148. return usb_get_intfdata(hdev->actconfig->interface[0]);
  149. }
  150. /* USB 2.0 spec Section 11.24.4.5 */
  151. static int get_hub_descriptor(struct usb_device *hdev, void *data)
  152. {
  153. int i, ret, size;
  154. unsigned dtype;
  155. if (hub_is_superspeed(hdev)) {
  156. dtype = USB_DT_SS_HUB;
  157. size = USB_DT_SS_HUB_SIZE;
  158. } else {
  159. dtype = USB_DT_HUB;
  160. size = sizeof(struct usb_hub_descriptor);
  161. }
  162. for (i = 0; i < 3; i++) {
  163. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  164. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  165. dtype << 8, 0, data, size,
  166. USB_CTRL_GET_TIMEOUT);
  167. if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
  168. return ret;
  169. }
  170. return -EINVAL;
  171. }
  172. /*
  173. * USB 2.0 spec Section 11.24.2.1
  174. */
  175. static int clear_hub_feature(struct usb_device *hdev, int feature)
  176. {
  177. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  178. USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
  179. }
  180. /*
  181. * USB 2.0 spec Section 11.24.2.2
  182. */
  183. static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
  184. {
  185. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  186. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  187. NULL, 0, 1000);
  188. }
  189. /*
  190. * USB 2.0 spec Section 11.24.2.13
  191. */
  192. static int set_port_feature(struct usb_device *hdev, int port1, int feature)
  193. {
  194. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  195. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  196. NULL, 0, 1000);
  197. }
  198. /*
  199. * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
  200. * for info about using port indicators
  201. */
  202. static void set_port_led(
  203. struct usb_hub *hub,
  204. int port1,
  205. int selector
  206. )
  207. {
  208. int status = set_port_feature(hub->hdev, (selector << 8) | port1,
  209. USB_PORT_FEAT_INDICATOR);
  210. if (status < 0)
  211. dev_dbg (hub->intfdev,
  212. "port %d indicator %s status %d\n",
  213. port1,
  214. ({ char *s; switch (selector) {
  215. case HUB_LED_AMBER: s = "amber"; break;
  216. case HUB_LED_GREEN: s = "green"; break;
  217. case HUB_LED_OFF: s = "off"; break;
  218. case HUB_LED_AUTO: s = "auto"; break;
  219. default: s = "??"; break;
  220. }; s; }),
  221. status);
  222. }
  223. #define LED_CYCLE_PERIOD ((2*HZ)/3)
  224. static void led_work (struct work_struct *work)
  225. {
  226. struct usb_hub *hub =
  227. container_of(work, struct usb_hub, leds.work);
  228. struct usb_device *hdev = hub->hdev;
  229. unsigned i;
  230. unsigned changed = 0;
  231. int cursor = -1;
  232. if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
  233. return;
  234. for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
  235. unsigned selector, mode;
  236. /* 30%-50% duty cycle */
  237. switch (hub->indicator[i]) {
  238. /* cycle marker */
  239. case INDICATOR_CYCLE:
  240. cursor = i;
  241. selector = HUB_LED_AUTO;
  242. mode = INDICATOR_AUTO;
  243. break;
  244. /* blinking green = sw attention */
  245. case INDICATOR_GREEN_BLINK:
  246. selector = HUB_LED_GREEN;
  247. mode = INDICATOR_GREEN_BLINK_OFF;
  248. break;
  249. case INDICATOR_GREEN_BLINK_OFF:
  250. selector = HUB_LED_OFF;
  251. mode = INDICATOR_GREEN_BLINK;
  252. break;
  253. /* blinking amber = hw attention */
  254. case INDICATOR_AMBER_BLINK:
  255. selector = HUB_LED_AMBER;
  256. mode = INDICATOR_AMBER_BLINK_OFF;
  257. break;
  258. case INDICATOR_AMBER_BLINK_OFF:
  259. selector = HUB_LED_OFF;
  260. mode = INDICATOR_AMBER_BLINK;
  261. break;
  262. /* blink green/amber = reserved */
  263. case INDICATOR_ALT_BLINK:
  264. selector = HUB_LED_GREEN;
  265. mode = INDICATOR_ALT_BLINK_OFF;
  266. break;
  267. case INDICATOR_ALT_BLINK_OFF:
  268. selector = HUB_LED_AMBER;
  269. mode = INDICATOR_ALT_BLINK;
  270. break;
  271. default:
  272. continue;
  273. }
  274. if (selector != HUB_LED_AUTO)
  275. changed = 1;
  276. set_port_led(hub, i + 1, selector);
  277. hub->indicator[i] = mode;
  278. }
  279. if (!changed && blinkenlights) {
  280. cursor++;
  281. cursor %= hub->descriptor->bNbrPorts;
  282. set_port_led(hub, cursor + 1, HUB_LED_GREEN);
  283. hub->indicator[cursor] = INDICATOR_CYCLE;
  284. changed++;
  285. }
  286. if (changed)
  287. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  288. }
  289. /* use a short timeout for hub/port status fetches */
  290. #define USB_STS_TIMEOUT 1000
  291. #define USB_STS_RETRIES 5
  292. /*
  293. * USB 2.0 spec Section 11.24.2.6
  294. */
  295. static int get_hub_status(struct usb_device *hdev,
  296. struct usb_hub_status *data)
  297. {
  298. int i, status = -ETIMEDOUT;
  299. for (i = 0; i < USB_STS_RETRIES &&
  300. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  301. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  302. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  303. data, sizeof(*data), USB_STS_TIMEOUT);
  304. }
  305. return status;
  306. }
  307. /*
  308. * USB 2.0 spec Section 11.24.2.7
  309. */
  310. static int get_port_status(struct usb_device *hdev, int port1,
  311. struct usb_port_status *data)
  312. {
  313. int i, status = -ETIMEDOUT;
  314. for (i = 0; i < USB_STS_RETRIES &&
  315. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  316. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  317. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
  318. data, sizeof(*data), USB_STS_TIMEOUT);
  319. }
  320. return status;
  321. }
  322. static int hub_port_status(struct usb_hub *hub, int port1,
  323. u16 *status, u16 *change)
  324. {
  325. int ret;
  326. mutex_lock(&hub->status_mutex);
  327. ret = get_port_status(hub->hdev, port1, &hub->status->port);
  328. if (ret < 4) {
  329. dev_err(hub->intfdev,
  330. "%s failed (err = %d)\n", __func__, ret);
  331. if (ret >= 0)
  332. ret = -EIO;
  333. } else {
  334. *status = le16_to_cpu(hub->status->port.wPortStatus);
  335. *change = le16_to_cpu(hub->status->port.wPortChange);
  336. ret = 0;
  337. }
  338. mutex_unlock(&hub->status_mutex);
  339. return ret;
  340. }
  341. static void kick_khubd(struct usb_hub *hub)
  342. {
  343. unsigned long flags;
  344. spin_lock_irqsave(&hub_event_lock, flags);
  345. if (!hub->disconnected && list_empty(&hub->event_list)) {
  346. list_add_tail(&hub->event_list, &hub_event_list);
  347. /* Suppress autosuspend until khubd runs */
  348. usb_autopm_get_interface_no_resume(
  349. to_usb_interface(hub->intfdev));
  350. wake_up(&khubd_wait);
  351. }
  352. spin_unlock_irqrestore(&hub_event_lock, flags);
  353. }
  354. void usb_kick_khubd(struct usb_device *hdev)
  355. {
  356. struct usb_hub *hub = hdev_to_hub(hdev);
  357. if (hub)
  358. kick_khubd(hub);
  359. }
  360. /* completion function, fires on port status changes and various faults */
  361. static void hub_irq(struct urb *urb)
  362. {
  363. struct usb_hub *hub = urb->context;
  364. int status = urb->status;
  365. unsigned i;
  366. unsigned long bits;
  367. switch (status) {
  368. case -ENOENT: /* synchronous unlink */
  369. case -ECONNRESET: /* async unlink */
  370. case -ESHUTDOWN: /* hardware going away */
  371. return;
  372. default: /* presumably an error */
  373. /* Cause a hub reset after 10 consecutive errors */
  374. dev_dbg (hub->intfdev, "transfer --> %d\n", status);
  375. if ((++hub->nerrors < 10) || hub->error)
  376. goto resubmit;
  377. hub->error = status;
  378. /* FALL THROUGH */
  379. /* let khubd handle things */
  380. case 0: /* we got data: port status changed */
  381. bits = 0;
  382. for (i = 0; i < urb->actual_length; ++i)
  383. bits |= ((unsigned long) ((*hub->buffer)[i]))
  384. << (i*8);
  385. hub->event_bits[0] = bits;
  386. break;
  387. }
  388. hub->nerrors = 0;
  389. /* Something happened, let khubd figure it out */
  390. kick_khubd(hub);
  391. resubmit:
  392. if (hub->quiescing)
  393. return;
  394. if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
  395. && status != -ENODEV && status != -EPERM)
  396. dev_err (hub->intfdev, "resubmit --> %d\n", status);
  397. }
  398. /* USB 2.0 spec Section 11.24.2.3 */
  399. static inline int
  400. hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
  401. {
  402. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  403. HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
  404. tt, NULL, 0, 1000);
  405. }
  406. /*
  407. * enumeration blocks khubd for a long time. we use keventd instead, since
  408. * long blocking there is the exception, not the rule. accordingly, HCDs
  409. * talking to TTs must queue control transfers (not just bulk and iso), so
  410. * both can talk to the same hub concurrently.
  411. */
  412. static void hub_tt_work(struct work_struct *work)
  413. {
  414. struct usb_hub *hub =
  415. container_of(work, struct usb_hub, tt.clear_work);
  416. unsigned long flags;
  417. int limit = 100;
  418. spin_lock_irqsave (&hub->tt.lock, flags);
  419. while (--limit && !list_empty (&hub->tt.clear_list)) {
  420. struct list_head *next;
  421. struct usb_tt_clear *clear;
  422. struct usb_device *hdev = hub->hdev;
  423. const struct hc_driver *drv;
  424. int status;
  425. next = hub->tt.clear_list.next;
  426. clear = list_entry (next, struct usb_tt_clear, clear_list);
  427. list_del (&clear->clear_list);
  428. /* drop lock so HCD can concurrently report other TT errors */
  429. spin_unlock_irqrestore (&hub->tt.lock, flags);
  430. status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
  431. if (status)
  432. dev_err (&hdev->dev,
  433. "clear tt %d (%04x) error %d\n",
  434. clear->tt, clear->devinfo, status);
  435. /* Tell the HCD, even if the operation failed */
  436. drv = clear->hcd->driver;
  437. if (drv->clear_tt_buffer_complete)
  438. (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
  439. kfree(clear);
  440. spin_lock_irqsave(&hub->tt.lock, flags);
  441. }
  442. spin_unlock_irqrestore (&hub->tt.lock, flags);
  443. }
  444. /**
  445. * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
  446. * @urb: an URB associated with the failed or incomplete split transaction
  447. *
  448. * High speed HCDs use this to tell the hub driver that some split control or
  449. * bulk transaction failed in a way that requires clearing internal state of
  450. * a transaction translator. This is normally detected (and reported) from
  451. * interrupt context.
  452. *
  453. * It may not be possible for that hub to handle additional full (or low)
  454. * speed transactions until that state is fully cleared out.
  455. */
  456. int usb_hub_clear_tt_buffer(struct urb *urb)
  457. {
  458. struct usb_device *udev = urb->dev;
  459. int pipe = urb->pipe;
  460. struct usb_tt *tt = udev->tt;
  461. unsigned long flags;
  462. struct usb_tt_clear *clear;
  463. /* we've got to cope with an arbitrary number of pending TT clears,
  464. * since each TT has "at least two" buffers that can need it (and
  465. * there can be many TTs per hub). even if they're uncommon.
  466. */
  467. if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
  468. dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
  469. /* FIXME recover somehow ... RESET_TT? */
  470. return -ENOMEM;
  471. }
  472. /* info that CLEAR_TT_BUFFER needs */
  473. clear->tt = tt->multi ? udev->ttport : 1;
  474. clear->devinfo = usb_pipeendpoint (pipe);
  475. clear->devinfo |= udev->devnum << 4;
  476. clear->devinfo |= usb_pipecontrol (pipe)
  477. ? (USB_ENDPOINT_XFER_CONTROL << 11)
  478. : (USB_ENDPOINT_XFER_BULK << 11);
  479. if (usb_pipein (pipe))
  480. clear->devinfo |= 1 << 15;
  481. /* info for completion callback */
  482. clear->hcd = bus_to_hcd(udev->bus);
  483. clear->ep = urb->ep;
  484. /* tell keventd to clear state for this TT */
  485. spin_lock_irqsave (&tt->lock, flags);
  486. list_add_tail (&clear->clear_list, &tt->clear_list);
  487. schedule_work(&tt->clear_work);
  488. spin_unlock_irqrestore (&tt->lock, flags);
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
  492. /* If do_delay is false, return the number of milliseconds the caller
  493. * needs to delay.
  494. */
  495. static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
  496. {
  497. int port1;
  498. unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
  499. unsigned delay;
  500. u16 wHubCharacteristics =
  501. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  502. /* Enable power on each port. Some hubs have reserved values
  503. * of LPSM (> 2) in their descriptors, even though they are
  504. * USB 2.0 hubs. Some hubs do not implement port-power switching
  505. * but only emulate it. In all cases, the ports won't work
  506. * unless we send these messages to the hub.
  507. */
  508. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
  509. dev_dbg(hub->intfdev, "enabling power on all ports\n");
  510. else
  511. dev_dbg(hub->intfdev, "trying to enable port power on "
  512. "non-switchable hub\n");
  513. for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
  514. set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
  515. /* Wait at least 100 msec for power to become stable */
  516. delay = max(pgood_delay, (unsigned) 100);
  517. if (do_delay)
  518. msleep(delay);
  519. return delay;
  520. }
  521. static int hub_hub_status(struct usb_hub *hub,
  522. u16 *status, u16 *change)
  523. {
  524. int ret;
  525. mutex_lock(&hub->status_mutex);
  526. ret = get_hub_status(hub->hdev, &hub->status->hub);
  527. if (ret < 0)
  528. dev_err (hub->intfdev,
  529. "%s failed (err = %d)\n", __func__, ret);
  530. else {
  531. *status = le16_to_cpu(hub->status->hub.wHubStatus);
  532. *change = le16_to_cpu(hub->status->hub.wHubChange);
  533. ret = 0;
  534. }
  535. mutex_unlock(&hub->status_mutex);
  536. return ret;
  537. }
  538. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
  539. {
  540. struct usb_device *hdev = hub->hdev;
  541. int ret = 0;
  542. if (hdev->children[port1-1] && set_state)
  543. usb_set_device_state(hdev->children[port1-1],
  544. USB_STATE_NOTATTACHED);
  545. if (!hub->error && !hub_is_superspeed(hub->hdev))
  546. ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
  547. if (ret)
  548. dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
  549. port1, ret);
  550. return ret;
  551. }
  552. /*
  553. * Disable a port and mark a logical connect-change event, so that some
  554. * time later khubd will disconnect() any existing usb_device on the port
  555. * and will re-enumerate if there actually is a device attached.
  556. */
  557. static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
  558. {
  559. dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
  560. hub_port_disable(hub, port1, 1);
  561. /* FIXME let caller ask to power down the port:
  562. * - some devices won't enumerate without a VBUS power cycle
  563. * - SRP saves power that way
  564. * - ... new call, TBD ...
  565. * That's easy if this hub can switch power per-port, and
  566. * khubd reactivates the port later (timer, SRP, etc).
  567. * Powerdown must be optional, because of reset/DFU.
  568. */
  569. set_bit(port1, hub->change_bits);
  570. kick_khubd(hub);
  571. }
  572. /**
  573. * usb_remove_device - disable a device's port on its parent hub
  574. * @udev: device to be disabled and removed
  575. * Context: @udev locked, must be able to sleep.
  576. *
  577. * After @udev's port has been disabled, khubd is notified and it will
  578. * see that the device has been disconnected. When the device is
  579. * physically unplugged and something is plugged in, the events will
  580. * be received and processed normally.
  581. */
  582. int usb_remove_device(struct usb_device *udev)
  583. {
  584. struct usb_hub *hub;
  585. struct usb_interface *intf;
  586. if (!udev->parent) /* Can't remove a root hub */
  587. return -EINVAL;
  588. hub = hdev_to_hub(udev->parent);
  589. intf = to_usb_interface(hub->intfdev);
  590. usb_autopm_get_interface(intf);
  591. set_bit(udev->portnum, hub->removed_bits);
  592. hub_port_logical_disconnect(hub, udev->portnum);
  593. usb_autopm_put_interface(intf);
  594. return 0;
  595. }
  596. enum hub_activation_type {
  597. HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
  598. HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
  599. };
  600. static void hub_init_func2(struct work_struct *ws);
  601. static void hub_init_func3(struct work_struct *ws);
  602. static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
  603. {
  604. struct usb_device *hdev = hub->hdev;
  605. struct usb_hcd *hcd;
  606. int ret;
  607. int port1;
  608. int status;
  609. bool need_debounce_delay = false;
  610. unsigned delay;
  611. /* Continue a partial initialization */
  612. if (type == HUB_INIT2)
  613. goto init2;
  614. if (type == HUB_INIT3)
  615. goto init3;
  616. /* After a resume, port power should still be on.
  617. * For any other type of activation, turn it on.
  618. */
  619. if (type != HUB_RESUME) {
  620. /* Speed up system boot by using a delayed_work for the
  621. * hub's initial power-up delays. This is pretty awkward
  622. * and the implementation looks like a home-brewed sort of
  623. * setjmp/longjmp, but it saves at least 100 ms for each
  624. * root hub (assuming usbcore is compiled into the kernel
  625. * rather than as a module). It adds up.
  626. *
  627. * This can't be done for HUB_RESUME or HUB_RESET_RESUME
  628. * because for those activation types the ports have to be
  629. * operational when we return. In theory this could be done
  630. * for HUB_POST_RESET, but it's easier not to.
  631. */
  632. if (type == HUB_INIT) {
  633. delay = hub_power_on(hub, false);
  634. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
  635. schedule_delayed_work(&hub->init_work,
  636. msecs_to_jiffies(delay));
  637. /* Suppress autosuspend until init is done */
  638. usb_autopm_get_interface_no_resume(
  639. to_usb_interface(hub->intfdev));
  640. return; /* Continues at init2: below */
  641. } else if (type == HUB_RESET_RESUME) {
  642. /* The internal host controller state for the hub device
  643. * may be gone after a host power loss on system resume.
  644. * Update the device's info so the HW knows it's a hub.
  645. */
  646. hcd = bus_to_hcd(hdev->bus);
  647. if (hcd->driver->update_hub_device) {
  648. ret = hcd->driver->update_hub_device(hcd, hdev,
  649. &hub->tt, GFP_NOIO);
  650. if (ret < 0) {
  651. dev_err(hub->intfdev, "Host not "
  652. "accepting hub info "
  653. "update.\n");
  654. dev_err(hub->intfdev, "LS/FS devices "
  655. "and hubs may not work "
  656. "under this hub\n.");
  657. }
  658. }
  659. hub_power_on(hub, true);
  660. } else {
  661. hub_power_on(hub, true);
  662. }
  663. }
  664. init2:
  665. /* Check each port and set hub->change_bits to let khubd know
  666. * which ports need attention.
  667. */
  668. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  669. struct usb_device *udev = hdev->children[port1-1];
  670. u16 portstatus, portchange;
  671. portstatus = portchange = 0;
  672. status = hub_port_status(hub, port1, &portstatus, &portchange);
  673. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  674. dev_dbg(hub->intfdev,
  675. "port %d: status %04x change %04x\n",
  676. port1, portstatus, portchange);
  677. /* After anything other than HUB_RESUME (i.e., initialization
  678. * or any sort of reset), every port should be disabled.
  679. * Unconnected ports should likewise be disabled (paranoia),
  680. * and so should ports for which we have no usb_device.
  681. */
  682. if ((portstatus & USB_PORT_STAT_ENABLE) && (
  683. type != HUB_RESUME ||
  684. !(portstatus & USB_PORT_STAT_CONNECTION) ||
  685. !udev ||
  686. udev->state == USB_STATE_NOTATTACHED)) {
  687. /*
  688. * USB3 protocol ports will automatically transition
  689. * to Enabled state when detect an USB3.0 device attach.
  690. * Do not disable USB3 protocol ports.
  691. */
  692. if (!hub_is_superspeed(hdev)) {
  693. clear_port_feature(hdev, port1,
  694. USB_PORT_FEAT_ENABLE);
  695. portstatus &= ~USB_PORT_STAT_ENABLE;
  696. } else {
  697. /* Pretend that power was lost for USB3 devs */
  698. portstatus &= ~USB_PORT_STAT_ENABLE;
  699. }
  700. }
  701. /* Clear status-change flags; we'll debounce later */
  702. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  703. need_debounce_delay = true;
  704. clear_port_feature(hub->hdev, port1,
  705. USB_PORT_FEAT_C_CONNECTION);
  706. }
  707. if (portchange & USB_PORT_STAT_C_ENABLE) {
  708. need_debounce_delay = true;
  709. clear_port_feature(hub->hdev, port1,
  710. USB_PORT_FEAT_C_ENABLE);
  711. }
  712. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  713. need_debounce_delay = true;
  714. clear_port_feature(hub->hdev, port1,
  715. USB_PORT_FEAT_C_PORT_LINK_STATE);
  716. }
  717. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  718. hub_is_superspeed(hub->hdev)) {
  719. need_debounce_delay = true;
  720. clear_port_feature(hub->hdev, port1,
  721. USB_PORT_FEAT_C_BH_PORT_RESET);
  722. }
  723. /* We can forget about a "removed" device when there's a
  724. * physical disconnect or the connect status changes.
  725. */
  726. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  727. (portchange & USB_PORT_STAT_C_CONNECTION))
  728. clear_bit(port1, hub->removed_bits);
  729. if (!udev || udev->state == USB_STATE_NOTATTACHED) {
  730. /* Tell khubd to disconnect the device or
  731. * check for a new connection
  732. */
  733. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  734. set_bit(port1, hub->change_bits);
  735. } else if (portstatus & USB_PORT_STAT_ENABLE) {
  736. /* The power session apparently survived the resume.
  737. * If there was an overcurrent or suspend change
  738. * (i.e., remote wakeup request), have khubd
  739. * take care of it.
  740. */
  741. if (portchange)
  742. set_bit(port1, hub->change_bits);
  743. } else if (udev->persist_enabled) {
  744. #ifdef CONFIG_PM
  745. udev->reset_resume = 1;
  746. #endif
  747. set_bit(port1, hub->change_bits);
  748. } else {
  749. /* The power session is gone; tell khubd */
  750. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  751. set_bit(port1, hub->change_bits);
  752. }
  753. }
  754. /* If no port-status-change flags were set, we don't need any
  755. * debouncing. If flags were set we can try to debounce the
  756. * ports all at once right now, instead of letting khubd do them
  757. * one at a time later on.
  758. *
  759. * If any port-status changes do occur during this delay, khubd
  760. * will see them later and handle them normally.
  761. */
  762. if (need_debounce_delay) {
  763. delay = HUB_DEBOUNCE_STABLE;
  764. /* Don't do a long sleep inside a workqueue routine */
  765. if (type == HUB_INIT2) {
  766. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
  767. schedule_delayed_work(&hub->init_work,
  768. msecs_to_jiffies(delay));
  769. return; /* Continues at init3: below */
  770. } else {
  771. msleep(delay);
  772. }
  773. }
  774. init3:
  775. hub->quiescing = 0;
  776. status = usb_submit_urb(hub->urb, GFP_NOIO);
  777. if (status < 0)
  778. dev_err(hub->intfdev, "activate --> %d\n", status);
  779. if (hub->has_indicators && blinkenlights)
  780. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  781. /* Scan all ports that need attention */
  782. kick_khubd(hub);
  783. /* Allow autosuspend if it was suppressed */
  784. if (type <= HUB_INIT3)
  785. usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
  786. }
  787. /* Implement the continuations for the delays above */
  788. static void hub_init_func2(struct work_struct *ws)
  789. {
  790. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  791. hub_activate(hub, HUB_INIT2);
  792. }
  793. static void hub_init_func3(struct work_struct *ws)
  794. {
  795. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  796. hub_activate(hub, HUB_INIT3);
  797. }
  798. enum hub_quiescing_type {
  799. HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
  800. };
  801. static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
  802. {
  803. struct usb_device *hdev = hub->hdev;
  804. int i;
  805. cancel_delayed_work_sync(&hub->init_work);
  806. /* khubd and related activity won't re-trigger */
  807. hub->quiescing = 1;
  808. if (type != HUB_SUSPEND) {
  809. /* Disconnect all the children */
  810. for (i = 0; i < hdev->maxchild; ++i) {
  811. if (hdev->children[i])
  812. usb_disconnect(&hdev->children[i]);
  813. }
  814. }
  815. /* Stop khubd and related activity */
  816. usb_kill_urb(hub->urb);
  817. if (hub->has_indicators)
  818. cancel_delayed_work_sync(&hub->leds);
  819. if (hub->tt.hub)
  820. cancel_work_sync(&hub->tt.clear_work);
  821. }
  822. /* caller has locked the hub device */
  823. static int hub_pre_reset(struct usb_interface *intf)
  824. {
  825. struct usb_hub *hub = usb_get_intfdata(intf);
  826. hub_quiesce(hub, HUB_PRE_RESET);
  827. return 0;
  828. }
  829. /* caller has locked the hub device */
  830. static int hub_post_reset(struct usb_interface *intf)
  831. {
  832. struct usb_hub *hub = usb_get_intfdata(intf);
  833. hub_activate(hub, HUB_POST_RESET);
  834. return 0;
  835. }
  836. static int hub_configure(struct usb_hub *hub,
  837. struct usb_endpoint_descriptor *endpoint)
  838. {
  839. struct usb_hcd *hcd;
  840. struct usb_device *hdev = hub->hdev;
  841. struct device *hub_dev = hub->intfdev;
  842. u16 hubstatus, hubchange;
  843. u16 wHubCharacteristics;
  844. unsigned int pipe;
  845. int maxp, ret;
  846. char *message = "out of memory";
  847. hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
  848. if (!hub->buffer) {
  849. ret = -ENOMEM;
  850. goto fail;
  851. }
  852. hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
  853. if (!hub->status) {
  854. ret = -ENOMEM;
  855. goto fail;
  856. }
  857. mutex_init(&hub->status_mutex);
  858. hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
  859. if (!hub->descriptor) {
  860. ret = -ENOMEM;
  861. goto fail;
  862. }
  863. if (hub_is_superspeed(hdev) && (hdev->parent != NULL)) {
  864. ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  865. HUB_SET_DEPTH, USB_RT_HUB,
  866. hdev->level - 1, 0, NULL, 0,
  867. USB_CTRL_SET_TIMEOUT);
  868. if (ret < 0) {
  869. message = "can't set hub depth";
  870. goto fail;
  871. }
  872. }
  873. /* Request the entire hub descriptor.
  874. * hub->descriptor can handle USB_MAXCHILDREN ports,
  875. * but the hub can/will return fewer bytes here.
  876. */
  877. ret = get_hub_descriptor(hdev, hub->descriptor);
  878. if (ret < 0) {
  879. message = "can't read hub descriptor";
  880. goto fail;
  881. } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
  882. message = "hub has too many ports!";
  883. ret = -ENODEV;
  884. goto fail;
  885. }
  886. hdev->maxchild = hub->descriptor->bNbrPorts;
  887. dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
  888. (hdev->maxchild == 1) ? "" : "s");
  889. hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
  890. if (!hub->port_owners) {
  891. ret = -ENOMEM;
  892. goto fail;
  893. }
  894. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  895. /* FIXME for USB 3.0, skip for now */
  896. if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
  897. !(hub_is_superspeed(hdev))) {
  898. int i;
  899. char portstr [USB_MAXCHILDREN + 1];
  900. for (i = 0; i < hdev->maxchild; i++)
  901. portstr[i] = hub->descriptor->u.hs.DeviceRemovable
  902. [((i + 1) / 8)] & (1 << ((i + 1) % 8))
  903. ? 'F' : 'R';
  904. portstr[hdev->maxchild] = 0;
  905. dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
  906. } else
  907. dev_dbg(hub_dev, "standalone hub\n");
  908. switch (wHubCharacteristics & HUB_CHAR_LPSM) {
  909. case 0x00:
  910. dev_dbg(hub_dev, "ganged power switching\n");
  911. break;
  912. case 0x01:
  913. dev_dbg(hub_dev, "individual port power switching\n");
  914. break;
  915. case 0x02:
  916. case 0x03:
  917. dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
  918. break;
  919. }
  920. switch (wHubCharacteristics & HUB_CHAR_OCPM) {
  921. case 0x00:
  922. dev_dbg(hub_dev, "global over-current protection\n");
  923. break;
  924. case 0x08:
  925. dev_dbg(hub_dev, "individual port over-current protection\n");
  926. break;
  927. case 0x10:
  928. case 0x18:
  929. dev_dbg(hub_dev, "no over-current protection\n");
  930. break;
  931. }
  932. spin_lock_init (&hub->tt.lock);
  933. INIT_LIST_HEAD (&hub->tt.clear_list);
  934. INIT_WORK(&hub->tt.clear_work, hub_tt_work);
  935. switch (hdev->descriptor.bDeviceProtocol) {
  936. case 0:
  937. break;
  938. case 1:
  939. dev_dbg(hub_dev, "Single TT\n");
  940. hub->tt.hub = hdev;
  941. break;
  942. case 2:
  943. ret = usb_set_interface(hdev, 0, 1);
  944. if (ret == 0) {
  945. dev_dbg(hub_dev, "TT per port\n");
  946. hub->tt.multi = 1;
  947. } else
  948. dev_err(hub_dev, "Using single TT (err %d)\n",
  949. ret);
  950. hub->tt.hub = hdev;
  951. break;
  952. case 3:
  953. /* USB 3.0 hubs don't have a TT */
  954. break;
  955. default:
  956. dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
  957. hdev->descriptor.bDeviceProtocol);
  958. break;
  959. }
  960. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  961. switch (wHubCharacteristics & HUB_CHAR_TTTT) {
  962. case HUB_TTTT_8_BITS:
  963. if (hdev->descriptor.bDeviceProtocol != 0) {
  964. hub->tt.think_time = 666;
  965. dev_dbg(hub_dev, "TT requires at most %d "
  966. "FS bit times (%d ns)\n",
  967. 8, hub->tt.think_time);
  968. }
  969. break;
  970. case HUB_TTTT_16_BITS:
  971. hub->tt.think_time = 666 * 2;
  972. dev_dbg(hub_dev, "TT requires at most %d "
  973. "FS bit times (%d ns)\n",
  974. 16, hub->tt.think_time);
  975. break;
  976. case HUB_TTTT_24_BITS:
  977. hub->tt.think_time = 666 * 3;
  978. dev_dbg(hub_dev, "TT requires at most %d "
  979. "FS bit times (%d ns)\n",
  980. 24, hub->tt.think_time);
  981. break;
  982. case HUB_TTTT_32_BITS:
  983. hub->tt.think_time = 666 * 4;
  984. dev_dbg(hub_dev, "TT requires at most %d "
  985. "FS bit times (%d ns)\n",
  986. 32, hub->tt.think_time);
  987. break;
  988. }
  989. /* probe() zeroes hub->indicator[] */
  990. if (wHubCharacteristics & HUB_CHAR_PORTIND) {
  991. hub->has_indicators = 1;
  992. dev_dbg(hub_dev, "Port indicators are supported\n");
  993. }
  994. dev_dbg(hub_dev, "power on to power good time: %dms\n",
  995. hub->descriptor->bPwrOn2PwrGood * 2);
  996. /* power budgeting mostly matters with bus-powered hubs,
  997. * and battery-powered root hubs (may provide just 8 mA).
  998. */
  999. ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
  1000. if (ret < 2) {
  1001. message = "can't get hub status";
  1002. goto fail;
  1003. }
  1004. le16_to_cpus(&hubstatus);
  1005. if (hdev == hdev->bus->root_hub) {
  1006. if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
  1007. hub->mA_per_port = 500;
  1008. else {
  1009. hub->mA_per_port = hdev->bus_mA;
  1010. hub->limited_power = 1;
  1011. }
  1012. } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  1013. dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
  1014. hub->descriptor->bHubContrCurrent);
  1015. hub->limited_power = 1;
  1016. if (hdev->maxchild > 0) {
  1017. int remaining = hdev->bus_mA -
  1018. hub->descriptor->bHubContrCurrent;
  1019. if (remaining < hdev->maxchild * 100)
  1020. dev_warn(hub_dev,
  1021. "insufficient power available "
  1022. "to use all downstream ports\n");
  1023. hub->mA_per_port = 100; /* 7.2.1.1 */
  1024. }
  1025. } else { /* Self-powered external hub */
  1026. /* FIXME: What about battery-powered external hubs that
  1027. * provide less current per port? */
  1028. hub->mA_per_port = 500;
  1029. }
  1030. if (hub->mA_per_port < 500)
  1031. dev_dbg(hub_dev, "%umA bus power budget for each child\n",
  1032. hub->mA_per_port);
  1033. /* Update the HCD's internal representation of this hub before khubd
  1034. * starts getting port status changes for devices under the hub.
  1035. */
  1036. hcd = bus_to_hcd(hdev->bus);
  1037. if (hcd->driver->update_hub_device) {
  1038. ret = hcd->driver->update_hub_device(hcd, hdev,
  1039. &hub->tt, GFP_KERNEL);
  1040. if (ret < 0) {
  1041. message = "can't update HCD hub info";
  1042. goto fail;
  1043. }
  1044. }
  1045. ret = hub_hub_status(hub, &hubstatus, &hubchange);
  1046. if (ret < 0) {
  1047. message = "can't get hub status";
  1048. goto fail;
  1049. }
  1050. /* local power status reports aren't always correct */
  1051. if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
  1052. dev_dbg(hub_dev, "local power source is %s\n",
  1053. (hubstatus & HUB_STATUS_LOCAL_POWER)
  1054. ? "lost (inactive)" : "good");
  1055. if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
  1056. dev_dbg(hub_dev, "%sover-current condition exists\n",
  1057. (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
  1058. /* set up the interrupt endpoint
  1059. * We use the EP's maxpacket size instead of (PORTS+1+7)/8
  1060. * bytes as USB2.0[11.12.3] says because some hubs are known
  1061. * to send more data (and thus cause overflow). For root hubs,
  1062. * maxpktsize is defined in hcd.c's fake endpoint descriptors
  1063. * to be big enough for at least USB_MAXCHILDREN ports. */
  1064. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  1065. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  1066. if (maxp > sizeof(*hub->buffer))
  1067. maxp = sizeof(*hub->buffer);
  1068. hub->urb = usb_alloc_urb(0, GFP_KERNEL);
  1069. if (!hub->urb) {
  1070. ret = -ENOMEM;
  1071. goto fail;
  1072. }
  1073. usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
  1074. hub, endpoint->bInterval);
  1075. /* maybe cycle the hub leds */
  1076. if (hub->has_indicators && blinkenlights)
  1077. hub->indicator [0] = INDICATOR_CYCLE;
  1078. hub_activate(hub, HUB_INIT);
  1079. return 0;
  1080. fail:
  1081. dev_err (hub_dev, "config failed, %s (err %d)\n",
  1082. message, ret);
  1083. /* hub_disconnect() frees urb and descriptor */
  1084. return ret;
  1085. }
  1086. static void hub_release(struct kref *kref)
  1087. {
  1088. struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
  1089. usb_put_intf(to_usb_interface(hub->intfdev));
  1090. kfree(hub);
  1091. }
  1092. static unsigned highspeed_hubs;
  1093. static void hub_disconnect(struct usb_interface *intf)
  1094. {
  1095. struct usb_hub *hub = usb_get_intfdata (intf);
  1096. /* Take the hub off the event list and don't let it be added again */
  1097. spin_lock_irq(&hub_event_lock);
  1098. if (!list_empty(&hub->event_list)) {
  1099. list_del_init(&hub->event_list);
  1100. usb_autopm_put_interface_no_suspend(intf);
  1101. }
  1102. hub->disconnected = 1;
  1103. spin_unlock_irq(&hub_event_lock);
  1104. /* Disconnect all children and quiesce the hub */
  1105. hub->error = 0;
  1106. hub_quiesce(hub, HUB_DISCONNECT);
  1107. usb_set_intfdata (intf, NULL);
  1108. hub->hdev->maxchild = 0;
  1109. if (hub->hdev->speed == USB_SPEED_HIGH)
  1110. highspeed_hubs--;
  1111. usb_free_urb(hub->urb);
  1112. kfree(hub->port_owners);
  1113. kfree(hub->descriptor);
  1114. kfree(hub->status);
  1115. kfree(hub->buffer);
  1116. kref_put(&hub->kref, hub_release);
  1117. }
  1118. static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1119. {
  1120. struct usb_host_interface *desc;
  1121. struct usb_endpoint_descriptor *endpoint;
  1122. struct usb_device *hdev;
  1123. struct usb_hub *hub;
  1124. desc = intf->cur_altsetting;
  1125. hdev = interface_to_usbdev(intf);
  1126. /* Hubs have proper suspend/resume support. USB 3.0 device suspend is
  1127. * different from USB 2.0/1.1 device suspend, and unfortunately we
  1128. * don't support it yet. So leave autosuspend disabled for USB 3.0
  1129. * external hubs for now. Enable autosuspend for USB 3.0 roothubs,
  1130. * since that isn't a "real" hub.
  1131. */
  1132. if (!hub_is_superspeed(hdev) || !hdev->parent)
  1133. usb_enable_autosuspend(hdev);
  1134. if (hdev->level == MAX_TOPO_LEVEL) {
  1135. dev_err(&intf->dev,
  1136. "Unsupported bus topology: hub nested too deep\n");
  1137. return -E2BIG;
  1138. }
  1139. #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
  1140. if (hdev->parent) {
  1141. dev_warn(&intf->dev, "ignoring external hub\n");
  1142. return -ENODEV;
  1143. }
  1144. #endif
  1145. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1146. /* specs is not defined, but it works */
  1147. if ((desc->desc.bInterfaceSubClass != 0) &&
  1148. (desc->desc.bInterfaceSubClass != 1)) {
  1149. descriptor_error:
  1150. dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
  1151. return -EIO;
  1152. }
  1153. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1154. if (desc->desc.bNumEndpoints != 1)
  1155. goto descriptor_error;
  1156. endpoint = &desc->endpoint[0].desc;
  1157. /* If it's not an interrupt in endpoint, we'd better punt! */
  1158. if (!usb_endpoint_is_int_in(endpoint))
  1159. goto descriptor_error;
  1160. /* We found a hub */
  1161. dev_info (&intf->dev, "USB hub found\n");
  1162. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  1163. if (!hub) {
  1164. dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
  1165. return -ENOMEM;
  1166. }
  1167. kref_init(&hub->kref);
  1168. INIT_LIST_HEAD(&hub->event_list);
  1169. hub->intfdev = &intf->dev;
  1170. hub->hdev = hdev;
  1171. INIT_DELAYED_WORK(&hub->leds, led_work);
  1172. INIT_DELAYED_WORK(&hub->init_work, NULL);
  1173. usb_get_intf(intf);
  1174. usb_set_intfdata (intf, hub);
  1175. intf->needs_remote_wakeup = 1;
  1176. if (hdev->speed == USB_SPEED_HIGH)
  1177. highspeed_hubs++;
  1178. if (hub_configure(hub, endpoint) >= 0)
  1179. return 0;
  1180. hub_disconnect (intf);
  1181. return -ENODEV;
  1182. }
  1183. /* No BKL needed */
  1184. static int
  1185. hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
  1186. {
  1187. struct usb_device *hdev = interface_to_usbdev (intf);
  1188. /* assert ifno == 0 (part of hub spec) */
  1189. switch (code) {
  1190. case USBDEVFS_HUB_PORTINFO: {
  1191. struct usbdevfs_hub_portinfo *info = user_data;
  1192. int i;
  1193. spin_lock_irq(&device_state_lock);
  1194. if (hdev->devnum <= 0)
  1195. info->nports = 0;
  1196. else {
  1197. info->nports = hdev->maxchild;
  1198. for (i = 0; i < info->nports; i++) {
  1199. if (hdev->children[i] == NULL)
  1200. info->port[i] = 0;
  1201. else
  1202. info->port[i] =
  1203. hdev->children[i]->devnum;
  1204. }
  1205. }
  1206. spin_unlock_irq(&device_state_lock);
  1207. return info->nports + 1;
  1208. }
  1209. default:
  1210. return -ENOSYS;
  1211. }
  1212. }
  1213. /*
  1214. * Allow user programs to claim ports on a hub. When a device is attached
  1215. * to one of these "claimed" ports, the program will "own" the device.
  1216. */
  1217. static int find_port_owner(struct usb_device *hdev, unsigned port1,
  1218. void ***ppowner)
  1219. {
  1220. if (hdev->state == USB_STATE_NOTATTACHED)
  1221. return -ENODEV;
  1222. if (port1 == 0 || port1 > hdev->maxchild)
  1223. return -EINVAL;
  1224. /* This assumes that devices not managed by the hub driver
  1225. * will always have maxchild equal to 0.
  1226. */
  1227. *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
  1228. return 0;
  1229. }
  1230. /* In the following three functions, the caller must hold hdev's lock */
  1231. int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
  1232. {
  1233. int rc;
  1234. void **powner;
  1235. rc = find_port_owner(hdev, port1, &powner);
  1236. if (rc)
  1237. return rc;
  1238. if (*powner)
  1239. return -EBUSY;
  1240. *powner = owner;
  1241. return rc;
  1242. }
  1243. int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
  1244. {
  1245. int rc;
  1246. void **powner;
  1247. rc = find_port_owner(hdev, port1, &powner);
  1248. if (rc)
  1249. return rc;
  1250. if (*powner != owner)
  1251. return -ENOENT;
  1252. *powner = NULL;
  1253. return rc;
  1254. }
  1255. void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
  1256. {
  1257. int n;
  1258. void **powner;
  1259. n = find_port_owner(hdev, 1, &powner);
  1260. if (n == 0) {
  1261. for (; n < hdev->maxchild; (++n, ++powner)) {
  1262. if (*powner == owner)
  1263. *powner = NULL;
  1264. }
  1265. }
  1266. }
  1267. /* The caller must hold udev's lock */
  1268. bool usb_device_is_owned(struct usb_device *udev)
  1269. {
  1270. struct usb_hub *hub;
  1271. if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
  1272. return false;
  1273. hub = hdev_to_hub(udev->parent);
  1274. return !!hub->port_owners[udev->portnum - 1];
  1275. }
  1276. static void recursively_mark_NOTATTACHED(struct usb_device *udev)
  1277. {
  1278. int i;
  1279. for (i = 0; i < udev->maxchild; ++i) {
  1280. if (udev->children[i])
  1281. recursively_mark_NOTATTACHED(udev->children[i]);
  1282. }
  1283. if (udev->state == USB_STATE_SUSPENDED)
  1284. udev->active_duration -= jiffies;
  1285. udev->state = USB_STATE_NOTATTACHED;
  1286. }
  1287. /**
  1288. * usb_set_device_state - change a device's current state (usbcore, hcds)
  1289. * @udev: pointer to device whose state should be changed
  1290. * @new_state: new state value to be stored
  1291. *
  1292. * udev->state is _not_ fully protected by the device lock. Although
  1293. * most transitions are made only while holding the lock, the state can
  1294. * can change to USB_STATE_NOTATTACHED at almost any time. This
  1295. * is so that devices can be marked as disconnected as soon as possible,
  1296. * without having to wait for any semaphores to be released. As a result,
  1297. * all changes to any device's state must be protected by the
  1298. * device_state_lock spinlock.
  1299. *
  1300. * Once a device has been added to the device tree, all changes to its state
  1301. * should be made using this routine. The state should _not_ be set directly.
  1302. *
  1303. * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
  1304. * Otherwise udev->state is set to new_state, and if new_state is
  1305. * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
  1306. * to USB_STATE_NOTATTACHED.
  1307. */
  1308. void usb_set_device_state(struct usb_device *udev,
  1309. enum usb_device_state new_state)
  1310. {
  1311. unsigned long flags;
  1312. int wakeup = -1;
  1313. spin_lock_irqsave(&device_state_lock, flags);
  1314. if (udev->state == USB_STATE_NOTATTACHED)
  1315. ; /* do nothing */
  1316. else if (new_state != USB_STATE_NOTATTACHED) {
  1317. /* root hub wakeup capabilities are managed out-of-band
  1318. * and may involve silicon errata ... ignore them here.
  1319. */
  1320. if (udev->parent) {
  1321. if (udev->state == USB_STATE_SUSPENDED
  1322. || new_state == USB_STATE_SUSPENDED)
  1323. ; /* No change to wakeup settings */
  1324. else if (new_state == USB_STATE_CONFIGURED)
  1325. wakeup = udev->actconfig->desc.bmAttributes
  1326. & USB_CONFIG_ATT_WAKEUP;
  1327. else
  1328. wakeup = 0;
  1329. }
  1330. if (udev->state == USB_STATE_SUSPENDED &&
  1331. new_state != USB_STATE_SUSPENDED)
  1332. udev->active_duration -= jiffies;
  1333. else if (new_state == USB_STATE_SUSPENDED &&
  1334. udev->state != USB_STATE_SUSPENDED)
  1335. udev->active_duration += jiffies;
  1336. udev->state = new_state;
  1337. } else
  1338. recursively_mark_NOTATTACHED(udev);
  1339. spin_unlock_irqrestore(&device_state_lock, flags);
  1340. if (wakeup >= 0)
  1341. device_set_wakeup_capable(&udev->dev, wakeup);
  1342. }
  1343. EXPORT_SYMBOL_GPL(usb_set_device_state);
  1344. /*
  1345. * Choose a device number.
  1346. *
  1347. * Device numbers are used as filenames in usbfs. On USB-1.1 and
  1348. * USB-2.0 buses they are also used as device addresses, however on
  1349. * USB-3.0 buses the address is assigned by the controller hardware
  1350. * and it usually is not the same as the device number.
  1351. *
  1352. * WUSB devices are simple: they have no hubs behind, so the mapping
  1353. * device <-> virtual port number becomes 1:1. Why? to simplify the
  1354. * life of the device connection logic in
  1355. * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
  1356. * handshake we need to assign a temporary address in the unauthorized
  1357. * space. For simplicity we use the first virtual port number found to
  1358. * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
  1359. * and that becomes it's address [X < 128] or its unauthorized address
  1360. * [X | 0x80].
  1361. *
  1362. * We add 1 as an offset to the one-based USB-stack port number
  1363. * (zero-based wusb virtual port index) for two reasons: (a) dev addr
  1364. * 0 is reserved by USB for default address; (b) Linux's USB stack
  1365. * uses always #1 for the root hub of the controller. So USB stack's
  1366. * port #1, which is wusb virtual-port #0 has address #2.
  1367. *
  1368. * Devices connected under xHCI are not as simple. The host controller
  1369. * supports virtualization, so the hardware assigns device addresses and
  1370. * the HCD must setup data structures before issuing a set address
  1371. * command to the hardware.
  1372. */
  1373. static void choose_devnum(struct usb_device *udev)
  1374. {
  1375. int devnum;
  1376. struct usb_bus *bus = udev->bus;
  1377. /* If khubd ever becomes multithreaded, this will need a lock */
  1378. if (udev->wusb) {
  1379. devnum = udev->portnum + 1;
  1380. BUG_ON(test_bit(devnum, bus->devmap.devicemap));
  1381. } else {
  1382. /* Try to allocate the next devnum beginning at
  1383. * bus->devnum_next. */
  1384. devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
  1385. bus->devnum_next);
  1386. if (devnum >= 128)
  1387. devnum = find_next_zero_bit(bus->devmap.devicemap,
  1388. 128, 1);
  1389. bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
  1390. }
  1391. if (devnum < 128) {
  1392. set_bit(devnum, bus->devmap.devicemap);
  1393. udev->devnum = devnum;
  1394. }
  1395. }
  1396. static void release_devnum(struct usb_device *udev)
  1397. {
  1398. if (udev->devnum > 0) {
  1399. clear_bit(udev->devnum, udev->bus->devmap.devicemap);
  1400. udev->devnum = -1;
  1401. }
  1402. }
  1403. static void update_devnum(struct usb_device *udev, int devnum)
  1404. {
  1405. /* The address for a WUSB device is managed by wusbcore. */
  1406. if (!udev->wusb)
  1407. udev->devnum = devnum;
  1408. }
  1409. static void hub_free_dev(struct usb_device *udev)
  1410. {
  1411. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1412. /* Root hubs aren't real devices, so don't free HCD resources */
  1413. if (hcd->driver->free_dev && udev->parent)
  1414. hcd->driver->free_dev(hcd, udev);
  1415. }
  1416. /**
  1417. * usb_disconnect - disconnect a device (usbcore-internal)
  1418. * @pdev: pointer to device being disconnected
  1419. * Context: !in_interrupt ()
  1420. *
  1421. * Something got disconnected. Get rid of it and all of its children.
  1422. *
  1423. * If *pdev is a normal device then the parent hub must already be locked.
  1424. * If *pdev is a root hub then this routine will acquire the
  1425. * usb_bus_list_lock on behalf of the caller.
  1426. *
  1427. * Only hub drivers (including virtual root hub drivers for host
  1428. * controllers) should ever call this.
  1429. *
  1430. * This call is synchronous, and may not be used in an interrupt context.
  1431. */
  1432. void usb_disconnect(struct usb_device **pdev)
  1433. {
  1434. struct usb_device *udev = *pdev;
  1435. int i;
  1436. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1437. if (!udev) {
  1438. pr_debug ("%s nodev\n", __func__);
  1439. return;
  1440. }
  1441. /* mark the device as inactive, so any further urb submissions for
  1442. * this device (and any of its children) will fail immediately.
  1443. * this quiesces everything except pending urbs.
  1444. */
  1445. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1446. dev_info(&udev->dev, "USB disconnect, device number %d\n",
  1447. udev->devnum);
  1448. usb_lock_device(udev);
  1449. /* Free up all the children before we remove this device */
  1450. for (i = 0; i < USB_MAXCHILDREN; i++) {
  1451. if (udev->children[i])
  1452. usb_disconnect(&udev->children[i]);
  1453. }
  1454. /* deallocate hcd/hardware state ... nuking all pending urbs and
  1455. * cleaning up all state associated with the current configuration
  1456. * so that the hardware is now fully quiesced.
  1457. */
  1458. dev_dbg (&udev->dev, "unregistering device\n");
  1459. mutex_lock(hcd->bandwidth_mutex);
  1460. usb_disable_device(udev, 0);
  1461. mutex_unlock(hcd->bandwidth_mutex);
  1462. usb_hcd_synchronize_unlinks(udev);
  1463. usb_remove_ep_devs(&udev->ep0);
  1464. usb_unlock_device(udev);
  1465. /* Unregister the device. The device driver is responsible
  1466. * for de-configuring the device and invoking the remove-device
  1467. * notifier chain (used by usbfs and possibly others).
  1468. */
  1469. device_del(&udev->dev);
  1470. /* Free the device number and delete the parent's children[]
  1471. * (or root_hub) pointer.
  1472. */
  1473. release_devnum(udev);
  1474. /* Avoid races with recursively_mark_NOTATTACHED() */
  1475. spin_lock_irq(&device_state_lock);
  1476. *pdev = NULL;
  1477. spin_unlock_irq(&device_state_lock);
  1478. hub_free_dev(udev);
  1479. put_device(&udev->dev);
  1480. }
  1481. #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  1482. static void show_string(struct usb_device *udev, char *id, char *string)
  1483. {
  1484. if (!string)
  1485. return;
  1486. dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
  1487. }
  1488. static void announce_device(struct usb_device *udev)
  1489. {
  1490. dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
  1491. le16_to_cpu(udev->descriptor.idVendor),
  1492. le16_to_cpu(udev->descriptor.idProduct));
  1493. dev_info(&udev->dev,
  1494. "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  1495. udev->descriptor.iManufacturer,
  1496. udev->descriptor.iProduct,
  1497. udev->descriptor.iSerialNumber);
  1498. show_string(udev, "Product", udev->product);
  1499. show_string(udev, "Manufacturer", udev->manufacturer);
  1500. show_string(udev, "SerialNumber", udev->serial);
  1501. }
  1502. #else
  1503. static inline void announce_device(struct usb_device *udev) { }
  1504. #endif
  1505. #ifdef CONFIG_USB_OTG
  1506. #include "otg_whitelist.h"
  1507. #endif
  1508. /**
  1509. * usb_enumerate_device_otg - FIXME (usbcore-internal)
  1510. * @udev: newly addressed device (in ADDRESS state)
  1511. *
  1512. * Finish enumeration for On-The-Go devices
  1513. */
  1514. static int usb_enumerate_device_otg(struct usb_device *udev)
  1515. {
  1516. int err = 0;
  1517. #ifdef CONFIG_USB_OTG
  1518. /*
  1519. * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
  1520. * to wake us after we've powered off VBUS; and HNP, switching roles
  1521. * "host" to "peripheral". The OTG descriptor helps figure this out.
  1522. */
  1523. if (!udev->bus->is_b_host
  1524. && udev->config
  1525. && udev->parent == udev->bus->root_hub) {
  1526. struct usb_otg_descriptor *desc = NULL;
  1527. struct usb_bus *bus = udev->bus;
  1528. /* descriptor may appear anywhere in config */
  1529. if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
  1530. le16_to_cpu(udev->config[0].desc.wTotalLength),
  1531. USB_DT_OTG, (void **) &desc) == 0) {
  1532. if (desc->bmAttributes & USB_OTG_HNP) {
  1533. unsigned port1 = udev->portnum;
  1534. dev_info(&udev->dev,
  1535. "Dual-Role OTG device on %sHNP port\n",
  1536. (port1 == bus->otg_port)
  1537. ? "" : "non-");
  1538. /* enable HNP before suspend, it's simpler */
  1539. if (port1 == bus->otg_port)
  1540. bus->b_hnp_enable = 1;
  1541. err = usb_control_msg(udev,
  1542. usb_sndctrlpipe(udev, 0),
  1543. USB_REQ_SET_FEATURE, 0,
  1544. bus->b_hnp_enable
  1545. ? USB_DEVICE_B_HNP_ENABLE
  1546. : USB_DEVICE_A_ALT_HNP_SUPPORT,
  1547. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1548. if (err < 0) {
  1549. /* OTG MESSAGE: report errors here,
  1550. * customize to match your product.
  1551. */
  1552. dev_info(&udev->dev,
  1553. "can't set HNP mode: %d\n",
  1554. err);
  1555. bus->b_hnp_enable = 0;
  1556. }
  1557. }
  1558. }
  1559. }
  1560. if (!is_targeted(udev)) {
  1561. /* Maybe it can talk to us, though we can't talk to it.
  1562. * (Includes HNP test device.)
  1563. */
  1564. if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
  1565. err = usb_port_suspend(udev, PMSG_SUSPEND);
  1566. if (err < 0)
  1567. dev_dbg(&udev->dev, "HNP fail, %d\n", err);
  1568. }
  1569. err = -ENOTSUPP;
  1570. goto fail;
  1571. }
  1572. fail:
  1573. #endif
  1574. return err;
  1575. }
  1576. /**
  1577. * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
  1578. * @udev: newly addressed device (in ADDRESS state)
  1579. *
  1580. * This is only called by usb_new_device() and usb_authorize_device()
  1581. * and FIXME -- all comments that apply to them apply here wrt to
  1582. * environment.
  1583. *
  1584. * If the device is WUSB and not authorized, we don't attempt to read
  1585. * the string descriptors, as they will be errored out by the device
  1586. * until it has been authorized.
  1587. */
  1588. static int usb_enumerate_device(struct usb_device *udev)
  1589. {
  1590. int err;
  1591. if (udev->config == NULL) {
  1592. err = usb_get_configuration(udev);
  1593. if (err < 0) {
  1594. dev_err(&udev->dev, "can't read configurations, error %d\n",
  1595. err);
  1596. goto fail;
  1597. }
  1598. }
  1599. if (udev->wusb == 1 && udev->authorized == 0) {
  1600. udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1601. udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1602. udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1603. }
  1604. else {
  1605. /* read the standard strings and cache them if present */
  1606. udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
  1607. udev->manufacturer = usb_cache_string(udev,
  1608. udev->descriptor.iManufacturer);
  1609. udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
  1610. }
  1611. err = usb_enumerate_device_otg(udev);
  1612. fail:
  1613. return err;
  1614. }
  1615. /**
  1616. * usb_new_device - perform initial device setup (usbcore-internal)
  1617. * @udev: newly addressed device (in ADDRESS state)
  1618. *
  1619. * This is called with devices which have been detected but not fully
  1620. * enumerated. The device descriptor is available, but not descriptors
  1621. * for any device configuration. The caller must have locked either
  1622. * the parent hub (if udev is a normal device) or else the
  1623. * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
  1624. * udev has already been installed, but udev is not yet visible through
  1625. * sysfs or other filesystem code.
  1626. *
  1627. * It will return if the device is configured properly or not. Zero if
  1628. * the interface was registered with the driver core; else a negative
  1629. * errno value.
  1630. *
  1631. * This call is synchronous, and may not be used in an interrupt context.
  1632. *
  1633. * Only the hub driver or root-hub registrar should ever call this.
  1634. */
  1635. int usb_new_device(struct usb_device *udev)
  1636. {
  1637. int err;
  1638. if (udev->parent) {
  1639. /* Initialize non-root-hub device wakeup to disabled;
  1640. * device (un)configuration controls wakeup capable
  1641. * sysfs power/wakeup controls wakeup enabled/disabled
  1642. */
  1643. device_init_wakeup(&udev->dev, 0);
  1644. }
  1645. /* Tell the runtime-PM framework the device is active */
  1646. pm_runtime_set_active(&udev->dev);
  1647. pm_runtime_get_noresume(&udev->dev);
  1648. pm_runtime_use_autosuspend(&udev->dev);
  1649. pm_runtime_enable(&udev->dev);
  1650. /* By default, forbid autosuspend for all devices. It will be
  1651. * allowed for hubs during binding.
  1652. */
  1653. usb_disable_autosuspend(udev);
  1654. err = usb_enumerate_device(udev); /* Read descriptors */
  1655. if (err < 0)
  1656. goto fail;
  1657. dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
  1658. udev->devnum, udev->bus->busnum,
  1659. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1660. /* export the usbdev device-node for libusb */
  1661. udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
  1662. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1663. /* Tell the world! */
  1664. announce_device(udev);
  1665. device_enable_async_suspend(&udev->dev);
  1666. /* Register the device. The device driver is responsible
  1667. * for configuring the device and invoking the add-device
  1668. * notifier chain (used by usbfs and possibly others).
  1669. */
  1670. err = device_add(&udev->dev);
  1671. if (err) {
  1672. dev_err(&udev->dev, "can't device_add, error %d\n", err);
  1673. goto fail;
  1674. }
  1675. (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
  1676. usb_mark_last_busy(udev);
  1677. pm_runtime_put_sync_autosuspend(&udev->dev);
  1678. return err;
  1679. fail:
  1680. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1681. pm_runtime_disable(&udev->dev);
  1682. pm_runtime_set_suspended(&udev->dev);
  1683. return err;
  1684. }
  1685. /**
  1686. * usb_deauthorize_device - deauthorize a device (usbcore-internal)
  1687. * @usb_dev: USB device
  1688. *
  1689. * Move the USB device to a very basic state where interfaces are disabled
  1690. * and the device is in fact unconfigured and unusable.
  1691. *
  1692. * We share a lock (that we have) with device_del(), so we need to
  1693. * defer its call.
  1694. */
  1695. int usb_deauthorize_device(struct usb_device *usb_dev)
  1696. {
  1697. usb_lock_device(usb_dev);
  1698. if (usb_dev->authorized == 0)
  1699. goto out_unauthorized;
  1700. usb_dev->authorized = 0;
  1701. usb_set_configuration(usb_dev, -1);
  1702. kfree(usb_dev->product);
  1703. usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1704. kfree(usb_dev->manufacturer);
  1705. usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1706. kfree(usb_dev->serial);
  1707. usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1708. usb_destroy_configuration(usb_dev);
  1709. usb_dev->descriptor.bNumConfigurations = 0;
  1710. out_unauthorized:
  1711. usb_unlock_device(usb_dev);
  1712. return 0;
  1713. }
  1714. int usb_authorize_device(struct usb_device *usb_dev)
  1715. {
  1716. int result = 0, c;
  1717. usb_lock_device(usb_dev);
  1718. if (usb_dev->authorized == 1)
  1719. goto out_authorized;
  1720. result = usb_autoresume_device(usb_dev);
  1721. if (result < 0) {
  1722. dev_err(&usb_dev->dev,
  1723. "can't autoresume for authorization: %d\n", result);
  1724. goto error_autoresume;
  1725. }
  1726. result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
  1727. if (result < 0) {
  1728. dev_err(&usb_dev->dev, "can't re-read device descriptor for "
  1729. "authorization: %d\n", result);
  1730. goto error_device_descriptor;
  1731. }
  1732. kfree(usb_dev->product);
  1733. usb_dev->product = NULL;
  1734. kfree(usb_dev->manufacturer);
  1735. usb_dev->manufacturer = NULL;
  1736. kfree(usb_dev->serial);
  1737. usb_dev->serial = NULL;
  1738. usb_dev->authorized = 1;
  1739. result = usb_enumerate_device(usb_dev);
  1740. if (result < 0)
  1741. goto error_enumerate;
  1742. /* Choose and set the configuration. This registers the interfaces
  1743. * with the driver core and lets interface drivers bind to them.
  1744. */
  1745. c = usb_choose_configuration(usb_dev);
  1746. if (c >= 0) {
  1747. result = usb_set_configuration(usb_dev, c);
  1748. if (result) {
  1749. dev_err(&usb_dev->dev,
  1750. "can't set config #%d, error %d\n", c, result);
  1751. /* This need not be fatal. The user can try to
  1752. * set other configurations. */
  1753. }
  1754. }
  1755. dev_info(&usb_dev->dev, "authorized to connect\n");
  1756. error_enumerate:
  1757. error_device_descriptor:
  1758. usb_autosuspend_device(usb_dev);
  1759. error_autoresume:
  1760. out_authorized:
  1761. usb_unlock_device(usb_dev); // complements locktree
  1762. return result;
  1763. }
  1764. /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
  1765. static unsigned hub_is_wusb(struct usb_hub *hub)
  1766. {
  1767. struct usb_hcd *hcd;
  1768. if (hub->hdev->parent != NULL) /* not a root hub? */
  1769. return 0;
  1770. hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
  1771. return hcd->wireless;
  1772. }
  1773. #define PORT_RESET_TRIES 5
  1774. #define SET_ADDRESS_TRIES 2
  1775. #define GET_DESCRIPTOR_TRIES 2
  1776. #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
  1777. #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
  1778. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  1779. #define HUB_SHORT_RESET_TIME 10
  1780. #define HUB_LONG_RESET_TIME 200
  1781. #define HUB_RESET_TIMEOUT 500
  1782. static int hub_port_wait_reset(struct usb_hub *hub, int port1,
  1783. struct usb_device *udev, unsigned int delay)
  1784. {
  1785. int delay_time, ret;
  1786. u16 portstatus;
  1787. u16 portchange;
  1788. for (delay_time = 0;
  1789. delay_time < HUB_RESET_TIMEOUT;
  1790. delay_time += delay) {
  1791. /* wait to give the device a chance to reset */
  1792. msleep(delay);
  1793. /* read and decode port status */
  1794. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  1795. if (ret < 0)
  1796. return ret;
  1797. /* Device went away? */
  1798. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  1799. return -ENOTCONN;
  1800. /* bomb out completely if the connection bounced */
  1801. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1802. return -ENOTCONN;
  1803. /* if we`ve finished resetting, then break out of the loop */
  1804. if (!(portstatus & USB_PORT_STAT_RESET) &&
  1805. (portstatus & USB_PORT_STAT_ENABLE)) {
  1806. if (hub_is_wusb(hub))
  1807. udev->speed = USB_SPEED_WIRELESS;
  1808. else if (hub_is_superspeed(hub->hdev))
  1809. udev->speed = USB_SPEED_SUPER;
  1810. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  1811. udev->speed = USB_SPEED_HIGH;
  1812. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  1813. udev->speed = USB_SPEED_LOW;
  1814. else
  1815. udev->speed = USB_SPEED_FULL;
  1816. return 0;
  1817. }
  1818. /* switch to the long delay after two short delay failures */
  1819. if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
  1820. delay = HUB_LONG_RESET_TIME;
  1821. dev_dbg (hub->intfdev,
  1822. "port %d not reset yet, waiting %dms\n",
  1823. port1, delay);
  1824. }
  1825. return -EBUSY;
  1826. }
  1827. static int hub_port_reset(struct usb_hub *hub, int port1,
  1828. struct usb_device *udev, unsigned int delay)
  1829. {
  1830. int i, status;
  1831. struct usb_hcd *hcd;
  1832. hcd = bus_to_hcd(udev->bus);
  1833. /* Block EHCI CF initialization during the port reset.
  1834. * Some companion controllers don't like it when they mix.
  1835. */
  1836. down_read(&ehci_cf_port_reset_rwsem);
  1837. /* Reset the port */
  1838. for (i = 0; i < PORT_RESET_TRIES; i++) {
  1839. status = set_port_feature(hub->hdev,
  1840. port1, USB_PORT_FEAT_RESET);
  1841. if (status)
  1842. dev_err(hub->intfdev,
  1843. "cannot reset port %d (err = %d)\n",
  1844. port1, status);
  1845. else {
  1846. status = hub_port_wait_reset(hub, port1, udev, delay);
  1847. if (status && status != -ENOTCONN)
  1848. dev_dbg(hub->intfdev,
  1849. "port_wait_reset: err = %d\n",
  1850. status);
  1851. }
  1852. /* return on disconnect or reset */
  1853. switch (status) {
  1854. case 0:
  1855. /* TRSTRCY = 10 ms; plus some extra */
  1856. msleep(10 + 40);
  1857. update_devnum(udev, 0);
  1858. if (hcd->driver->reset_device) {
  1859. status = hcd->driver->reset_device(hcd, udev);
  1860. if (status < 0) {
  1861. dev_err(&udev->dev, "Cannot reset "
  1862. "HCD device state\n");
  1863. break;
  1864. }
  1865. }
  1866. /* FALL THROUGH */
  1867. case -ENOTCONN:
  1868. case -ENODEV:
  1869. clear_port_feature(hub->hdev,
  1870. port1, USB_PORT_FEAT_C_RESET);
  1871. /* FIXME need disconnect() for NOTATTACHED device */
  1872. usb_set_device_state(udev, status
  1873. ? USB_STATE_NOTATTACHED
  1874. : USB_STATE_DEFAULT);
  1875. goto done;
  1876. }
  1877. dev_dbg (hub->intfdev,
  1878. "port %d not enabled, trying reset again...\n",
  1879. port1);
  1880. delay = HUB_LONG_RESET_TIME;
  1881. }
  1882. dev_err (hub->intfdev,
  1883. "Cannot enable port %i. Maybe the USB cable is bad?\n",
  1884. port1);
  1885. done:
  1886. up_read(&ehci_cf_port_reset_rwsem);
  1887. return status;
  1888. }
  1889. /* Warm reset a USB3 protocol port */
  1890. static int hub_port_warm_reset(struct usb_hub *hub, int port)
  1891. {
  1892. int ret;
  1893. u16 portstatus, portchange;
  1894. if (!hub_is_superspeed(hub->hdev)) {
  1895. dev_err(hub->intfdev, "only USB3 hub support warm reset\n");
  1896. return -EINVAL;
  1897. }
  1898. /* Warm reset the port */
  1899. ret = set_port_feature(hub->hdev,
  1900. port, USB_PORT_FEAT_BH_PORT_RESET);
  1901. if (ret) {
  1902. dev_err(hub->intfdev, "cannot warm reset port %d\n", port);
  1903. return ret;
  1904. }
  1905. msleep(20);
  1906. ret = hub_port_status(hub, port, &portstatus, &portchange);
  1907. if (portchange & USB_PORT_STAT_C_RESET)
  1908. clear_port_feature(hub->hdev, port, USB_PORT_FEAT_C_RESET);
  1909. if (portchange & USB_PORT_STAT_C_BH_RESET)
  1910. clear_port_feature(hub->hdev, port,
  1911. USB_PORT_FEAT_C_BH_PORT_RESET);
  1912. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  1913. clear_port_feature(hub->hdev, port,
  1914. USB_PORT_FEAT_C_PORT_LINK_STATE);
  1915. return ret;
  1916. }
  1917. /* Check if a port is power on */
  1918. static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
  1919. {
  1920. int ret = 0;
  1921. if (hub_is_superspeed(hub->hdev)) {
  1922. if (portstatus & USB_SS_PORT_STAT_POWER)
  1923. ret = 1;
  1924. } else {
  1925. if (portstatus & USB_PORT_STAT_POWER)
  1926. ret = 1;
  1927. }
  1928. return ret;
  1929. }
  1930. #ifdef CONFIG_PM
  1931. /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
  1932. static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
  1933. {
  1934. int ret = 0;
  1935. if (hub_is_superspeed(hub->hdev)) {
  1936. if ((portstatus & USB_PORT_STAT_LINK_STATE)
  1937. == USB_SS_PORT_LS_U3)
  1938. ret = 1;
  1939. } else {
  1940. if (portstatus & USB_PORT_STAT_SUSPEND)
  1941. ret = 1;
  1942. }
  1943. return ret;
  1944. }
  1945. /* Determine whether the device on a port is ready for a normal resume,
  1946. * is ready for a reset-resume, or should be disconnected.
  1947. */
  1948. static int check_port_resume_type(struct usb_device *udev,
  1949. struct usb_hub *hub, int port1,
  1950. int status, unsigned portchange, unsigned portstatus)
  1951. {
  1952. /* Is the device still present? */
  1953. if (status || port_is_suspended(hub, portstatus) ||
  1954. !port_is_power_on(hub, portstatus) ||
  1955. !(portstatus & USB_PORT_STAT_CONNECTION)) {
  1956. if (status >= 0)
  1957. status = -ENODEV;
  1958. }
  1959. /* Can't do a normal resume if the port isn't enabled,
  1960. * so try a reset-resume instead.
  1961. */
  1962. else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
  1963. if (udev->persist_enabled)
  1964. udev->reset_resume = 1;
  1965. else
  1966. status = -ENODEV;
  1967. }
  1968. if (status) {
  1969. dev_dbg(hub->intfdev,
  1970. "port %d status %04x.%04x after resume, %d\n",
  1971. port1, portchange, portstatus, status);
  1972. } else if (udev->reset_resume) {
  1973. /* Late port handoff can set status-change bits */
  1974. if (portchange & USB_PORT_STAT_C_CONNECTION)
  1975. clear_port_feature(hub->hdev, port1,
  1976. USB_PORT_FEAT_C_CONNECTION);
  1977. if (portchange & USB_PORT_STAT_C_ENABLE)
  1978. clear_port_feature(hub->hdev, port1,
  1979. USB_PORT_FEAT_C_ENABLE);
  1980. }
  1981. return status;
  1982. }
  1983. #ifdef CONFIG_USB_SUSPEND
  1984. /*
  1985. * usb_port_suspend - suspend a usb device's upstream port
  1986. * @udev: device that's no longer in active use, not a root hub
  1987. * Context: must be able to sleep; device not locked; pm locks held
  1988. *
  1989. * Suspends a USB device that isn't in active use, conserving power.
  1990. * Devices may wake out of a suspend, if anything important happens,
  1991. * using the remote wakeup mechanism. They may also be taken out of
  1992. * suspend by the host, using usb_port_resume(). It's also routine
  1993. * to disconnect devices while they are suspended.
  1994. *
  1995. * This only affects the USB hardware for a device; its interfaces
  1996. * (and, for hubs, child devices) must already have been suspended.
  1997. *
  1998. * Selective port suspend reduces power; most suspended devices draw
  1999. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  2000. * All devices below the suspended port are also suspended.
  2001. *
  2002. * Devices leave suspend state when the host wakes them up. Some devices
  2003. * also support "remote wakeup", where the device can activate the USB
  2004. * tree above them to deliver data, such as a keypress or packet. In
  2005. * some cases, this wakes the USB host.
  2006. *
  2007. * Suspending OTG devices may trigger HNP, if that's been enabled
  2008. * between a pair of dual-role devices. That will change roles, such
  2009. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  2010. *
  2011. * Devices on USB hub ports have only one "suspend" state, corresponding
  2012. * to ACPI D2, "may cause the device to lose some context".
  2013. * State transitions include:
  2014. *
  2015. * - suspend, resume ... when the VBUS power link stays live
  2016. * - suspend, disconnect ... VBUS lost
  2017. *
  2018. * Once VBUS drop breaks the circuit, the port it's using has to go through
  2019. * normal re-enumeration procedures, starting with enabling VBUS power.
  2020. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  2021. * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
  2022. * timer, no SRP, no requests through sysfs.
  2023. *
  2024. * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
  2025. * the root hub for their bus goes into global suspend ... so we don't
  2026. * (falsely) update the device power state to say it suspended.
  2027. *
  2028. * Returns 0 on success, else negative errno.
  2029. */
  2030. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2031. {
  2032. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2033. int port1 = udev->portnum;
  2034. int status;
  2035. // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
  2036. /* enable remote wakeup when appropriate; this lets the device
  2037. * wake up the upstream hub (including maybe the root hub).
  2038. *
  2039. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  2040. * we don't explicitly enable it here.
  2041. */
  2042. if (udev->do_remote_wakeup) {
  2043. status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2044. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2045. USB_DEVICE_REMOTE_WAKEUP, 0,
  2046. NULL, 0,
  2047. USB_CTRL_SET_TIMEOUT);
  2048. if (status) {
  2049. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  2050. status);
  2051. /* bail if autosuspend is requested */
  2052. if (msg.event & PM_EVENT_AUTO)
  2053. return status;
  2054. }
  2055. }
  2056. /* see 7.1.7.6 */
  2057. if (hub_is_superspeed(hub->hdev))
  2058. status = set_port_feature(hub->hdev,
  2059. port1 | (USB_SS_PORT_LS_U3 << 3),
  2060. USB_PORT_FEAT_LINK_STATE);
  2061. else
  2062. status = set_port_feature(hub->hdev, port1,
  2063. USB_PORT_FEAT_SUSPEND);
  2064. if (status) {
  2065. dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
  2066. port1, status);
  2067. /* paranoia: "should not happen" */
  2068. if (udev->do_remote_wakeup)
  2069. (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2070. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2071. USB_DEVICE_REMOTE_WAKEUP, 0,
  2072. NULL, 0,
  2073. USB_CTRL_SET_TIMEOUT);
  2074. /* System sleep transitions should never fail */
  2075. if (!(msg.event & PM_EVENT_AUTO))
  2076. status = 0;
  2077. } else {
  2078. /* device has up to 10 msec to fully suspend */
  2079. dev_dbg(&udev->dev, "usb %ssuspend\n",
  2080. (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
  2081. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  2082. msleep(10);
  2083. }
  2084. usb_mark_last_busy(hub->hdev);
  2085. return status;
  2086. }
  2087. /*
  2088. * If the USB "suspend" state is in use (rather than "global suspend"),
  2089. * many devices will be individually taken out of suspend state using
  2090. * special "resume" signaling. This routine kicks in shortly after
  2091. * hardware resume signaling is finished, either because of selective
  2092. * resume (by host) or remote wakeup (by device) ... now see what changed
  2093. * in the tree that's rooted at this device.
  2094. *
  2095. * If @udev->reset_resume is set then the device is reset before the
  2096. * status check is done.
  2097. */
  2098. static int finish_port_resume(struct usb_device *udev)
  2099. {
  2100. int status = 0;
  2101. u16 devstatus;
  2102. /* caller owns the udev device lock */
  2103. dev_dbg(&udev->dev, "%s\n",
  2104. udev->reset_resume ? "finish reset-resume" : "finish resume");
  2105. /* usb ch9 identifies four variants of SUSPENDED, based on what
  2106. * state the device resumes to. Linux currently won't see the
  2107. * first two on the host side; they'd be inside hub_port_init()
  2108. * during many timeouts, but khubd can't suspend until later.
  2109. */
  2110. usb_set_device_state(udev, udev->actconfig
  2111. ? USB_STATE_CONFIGURED
  2112. : USB_STATE_ADDRESS);
  2113. /* 10.5.4.5 says not to reset a suspended port if the attached
  2114. * device is enabled for remote wakeup. Hence the reset
  2115. * operation is carried out here, after the port has been
  2116. * resumed.
  2117. */
  2118. if (udev->reset_resume)
  2119. retry_reset_resume:
  2120. status = usb_reset_and_verify_device(udev);
  2121. /* 10.5.4.5 says be sure devices in the tree are still there.
  2122. * For now let's assume the device didn't go crazy on resume,
  2123. * and device drivers will know about any resume quirks.
  2124. */
  2125. if (status == 0) {
  2126. devstatus = 0;
  2127. status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  2128. if (status >= 0)
  2129. status = (status > 0 ? 0 : -ENODEV);
  2130. /* If a normal resume failed, try doing a reset-resume */
  2131. if (status && !udev->reset_resume && udev->persist_enabled) {
  2132. dev_dbg(&udev->dev, "retry with reset-resume\n");
  2133. udev->reset_resume = 1;
  2134. goto retry_reset_resume;
  2135. }
  2136. }
  2137. if (status) {
  2138. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  2139. status);
  2140. } else if (udev->actconfig) {
  2141. le16_to_cpus(&devstatus);
  2142. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
  2143. status = usb_control_msg(udev,
  2144. usb_sndctrlpipe(udev, 0),
  2145. USB_REQ_CLEAR_FEATURE,
  2146. USB_RECIP_DEVICE,
  2147. USB_DEVICE_REMOTE_WAKEUP, 0,
  2148. NULL, 0,
  2149. USB_CTRL_SET_TIMEOUT);
  2150. if (status)
  2151. dev_dbg(&udev->dev,
  2152. "disable remote wakeup, status %d\n",
  2153. status);
  2154. }
  2155. status = 0;
  2156. }
  2157. return status;
  2158. }
  2159. /*
  2160. * usb_port_resume - re-activate a suspended usb device's upstream port
  2161. * @udev: device to re-activate, not a root hub
  2162. * Context: must be able to sleep; device not locked; pm locks held
  2163. *
  2164. * This will re-activate the suspended device, increasing power usage
  2165. * while letting drivers communicate again with its endpoints.
  2166. * USB resume explicitly guarantees that the power session between
  2167. * the host and the device is the same as it was when the device
  2168. * suspended.
  2169. *
  2170. * If @udev->reset_resume is set then this routine won't check that the
  2171. * port is still enabled. Furthermore, finish_port_resume() above will
  2172. * reset @udev. The end result is that a broken power session can be
  2173. * recovered and @udev will appear to persist across a loss of VBUS power.
  2174. *
  2175. * For example, if a host controller doesn't maintain VBUS suspend current
  2176. * during a system sleep or is reset when the system wakes up, all the USB
  2177. * power sessions below it will be broken. This is especially troublesome
  2178. * for mass-storage devices containing mounted filesystems, since the
  2179. * device will appear to have disconnected and all the memory mappings
  2180. * to it will be lost. Using the USB_PERSIST facility, the device can be
  2181. * made to appear as if it had not disconnected.
  2182. *
  2183. * This facility can be dangerous. Although usb_reset_and_verify_device() makes
  2184. * every effort to insure that the same device is present after the
  2185. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  2186. * quite possible for a device to remain unaltered but its media to be
  2187. * changed. If the user replaces a flash memory card while the system is
  2188. * asleep, he will have only himself to blame when the filesystem on the
  2189. * new card is corrupted and the system crashes.
  2190. *
  2191. * Returns 0 on success, else negative errno.
  2192. */
  2193. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2194. {
  2195. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2196. int port1 = udev->portnum;
  2197. int status;
  2198. u16 portchange, portstatus;
  2199. /* Skip the initial Clear-Suspend step for a remote wakeup */
  2200. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2201. if (status == 0 && !port_is_suspended(hub, portstatus))
  2202. goto SuspendCleared;
  2203. // dev_dbg(hub->intfdev, "resume port %d\n", port1);
  2204. set_bit(port1, hub->busy_bits);
  2205. /* see 7.1.7.7; affects power usage, but not budgeting */
  2206. if (hub_is_superspeed(hub->hdev))
  2207. status = set_port_feature(hub->hdev,
  2208. port1 | (USB_SS_PORT_LS_U0 << 3),
  2209. USB_PORT_FEAT_LINK_STATE);
  2210. else
  2211. status = clear_port_feature(hub->hdev,
  2212. port1, USB_PORT_FEAT_SUSPEND);
  2213. if (status) {
  2214. dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
  2215. port1, status);
  2216. } else {
  2217. /* drive resume for at least 20 msec */
  2218. dev_dbg(&udev->dev, "usb %sresume\n",
  2219. (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
  2220. msleep(25);
  2221. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  2222. * stop resume signaling. Then finish the resume
  2223. * sequence.
  2224. */
  2225. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2226. /* TRSMRCY = 10 msec */
  2227. msleep(10);
  2228. }
  2229. SuspendCleared:
  2230. if (status == 0) {
  2231. if (hub_is_superspeed(hub->hdev)) {
  2232. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  2233. clear_port_feature(hub->hdev, port1,
  2234. USB_PORT_FEAT_C_PORT_LINK_STATE);
  2235. } else {
  2236. if (portchange & USB_PORT_STAT_C_SUSPEND)
  2237. clear_port_feature(hub->hdev, port1,
  2238. USB_PORT_FEAT_C_SUSPEND);
  2239. }
  2240. }
  2241. clear_bit(port1, hub->busy_bits);
  2242. status = check_port_resume_type(udev,
  2243. hub, port1, status, portchange, portstatus);
  2244. if (status == 0)
  2245. status = finish_port_resume(udev);
  2246. if (status < 0) {
  2247. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2248. hub_port_logical_disconnect(hub, port1);
  2249. }
  2250. return status;
  2251. }
  2252. /* caller has locked udev */
  2253. int usb_remote_wakeup(struct usb_device *udev)
  2254. {
  2255. int status = 0;
  2256. if (udev->state == USB_STATE_SUSPENDED) {
  2257. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  2258. status = usb_autoresume_device(udev);
  2259. if (status == 0) {
  2260. /* Let the drivers do their thing, then... */
  2261. usb_autosuspend_device(udev);
  2262. }
  2263. }
  2264. return status;
  2265. }
  2266. #else /* CONFIG_USB_SUSPEND */
  2267. /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
  2268. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2269. {
  2270. return 0;
  2271. }
  2272. /* However we may need to do a reset-resume */
  2273. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2274. {
  2275. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2276. int port1 = udev->portnum;
  2277. int status;
  2278. u16 portchange, portstatus;
  2279. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2280. status = check_port_resume_type(udev,
  2281. hub, port1, status, portchange, portstatus);
  2282. if (status) {
  2283. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2284. hub_port_logical_disconnect(hub, port1);
  2285. } else if (udev->reset_resume) {
  2286. dev_dbg(&udev->dev, "reset-resume\n");
  2287. status = usb_reset_and_verify_device(udev);
  2288. }
  2289. return status;
  2290. }
  2291. #endif
  2292. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  2293. {
  2294. struct usb_hub *hub = usb_get_intfdata (intf);
  2295. struct usb_device *hdev = hub->hdev;
  2296. unsigned port1;
  2297. /* Warn if children aren't already suspended */
  2298. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  2299. struct usb_device *udev;
  2300. udev = hdev->children [port1-1];
  2301. if (udev && udev->can_submit) {
  2302. dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
  2303. if (msg.event & PM_EVENT_AUTO)
  2304. return -EBUSY;
  2305. }
  2306. }
  2307. dev_dbg(&intf->dev, "%s\n", __func__);
  2308. /* stop khubd and related activity */
  2309. hub_quiesce(hub, HUB_SUSPEND);
  2310. return 0;
  2311. }
  2312. static int hub_resume(struct usb_interface *intf)
  2313. {
  2314. struct usb_hub *hub = usb_get_intfdata(intf);
  2315. dev_dbg(&intf->dev, "%s\n", __func__);
  2316. hub_activate(hub, HUB_RESUME);
  2317. return 0;
  2318. }
  2319. static int hub_reset_resume(struct usb_interface *intf)
  2320. {
  2321. struct usb_hub *hub = usb_get_intfdata(intf);
  2322. dev_dbg(&intf->dev, "%s\n", __func__);
  2323. hub_activate(hub, HUB_RESET_RESUME);
  2324. return 0;
  2325. }
  2326. /**
  2327. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  2328. * @rhdev: struct usb_device for the root hub
  2329. *
  2330. * The USB host controller driver calls this function when its root hub
  2331. * is resumed and Vbus power has been interrupted or the controller
  2332. * has been reset. The routine marks @rhdev as having lost power.
  2333. * When the hub driver is resumed it will take notice and carry out
  2334. * power-session recovery for all the "USB-PERSIST"-enabled child devices;
  2335. * the others will be disconnected.
  2336. */
  2337. void usb_root_hub_lost_power(struct usb_device *rhdev)
  2338. {
  2339. dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
  2340. rhdev->reset_resume = 1;
  2341. }
  2342. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  2343. #else /* CONFIG_PM */
  2344. #define hub_suspend NULL
  2345. #define hub_resume NULL
  2346. #define hub_reset_resume NULL
  2347. #endif
  2348. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  2349. *
  2350. * Between connect detection and reset signaling there must be a delay
  2351. * of 100ms at least for debounce and power-settling. The corresponding
  2352. * timer shall restart whenever the downstream port detects a disconnect.
  2353. *
  2354. * Apparently there are some bluetooth and irda-dongles and a number of
  2355. * low-speed devices for which this debounce period may last over a second.
  2356. * Not covered by the spec - but easy to deal with.
  2357. *
  2358. * This implementation uses a 1500ms total debounce timeout; if the
  2359. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  2360. * every 25ms for transient disconnects. When the port status has been
  2361. * unchanged for 100ms it returns the port status.
  2362. */
  2363. static int hub_port_debounce(struct usb_hub *hub, int port1)
  2364. {
  2365. int ret;
  2366. int total_time, stable_time = 0;
  2367. u16 portchange, portstatus;
  2368. unsigned connection = 0xffff;
  2369. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  2370. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  2371. if (ret < 0)
  2372. return ret;
  2373. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  2374. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  2375. stable_time += HUB_DEBOUNCE_STEP;
  2376. if (stable_time >= HUB_DEBOUNCE_STABLE)
  2377. break;
  2378. } else {
  2379. stable_time = 0;
  2380. connection = portstatus & USB_PORT_STAT_CONNECTION;
  2381. }
  2382. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2383. clear_port_feature(hub->hdev, port1,
  2384. USB_PORT_FEAT_C_CONNECTION);
  2385. }
  2386. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  2387. break;
  2388. msleep(HUB_DEBOUNCE_STEP);
  2389. }
  2390. dev_dbg (hub->intfdev,
  2391. "debounce: port %d: total %dms stable %dms status 0x%x\n",
  2392. port1, total_time, stable_time, portstatus);
  2393. if (stable_time < HUB_DEBOUNCE_STABLE)
  2394. return -ETIMEDOUT;
  2395. return portstatus;
  2396. }
  2397. void usb_ep0_reinit(struct usb_device *udev)
  2398. {
  2399. usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
  2400. usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
  2401. usb_enable_endpoint(udev, &udev->ep0, true);
  2402. }
  2403. EXPORT_SYMBOL_GPL(usb_ep0_reinit);
  2404. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  2405. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  2406. static int hub_set_address(struct usb_device *udev, int devnum)
  2407. {
  2408. int retval;
  2409. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2410. /*
  2411. * The host controller will choose the device address,
  2412. * instead of the core having chosen it earlier
  2413. */
  2414. if (!hcd->driver->address_device && devnum <= 1)
  2415. return -EINVAL;
  2416. if (udev->state == USB_STATE_ADDRESS)
  2417. return 0;
  2418. if (udev->state != USB_STATE_DEFAULT)
  2419. return -EINVAL;
  2420. if (hcd->driver->address_device)
  2421. retval = hcd->driver->address_device(hcd, udev);
  2422. else
  2423. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  2424. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  2425. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2426. if (retval == 0) {
  2427. update_devnum(udev, devnum);
  2428. /* Device now using proper address. */
  2429. usb_set_device_state(udev, USB_STATE_ADDRESS);
  2430. usb_ep0_reinit(udev);
  2431. }
  2432. return retval;
  2433. }
  2434. /* Reset device, (re)assign address, get device descriptor.
  2435. * Device connection must be stable, no more debouncing needed.
  2436. * Returns device in USB_STATE_ADDRESS, except on error.
  2437. *
  2438. * If this is called for an already-existing device (as part of
  2439. * usb_reset_and_verify_device), the caller must own the device lock. For a
  2440. * newly detected device that is not accessible through any global
  2441. * pointers, it's not necessary to lock the device.
  2442. */
  2443. static int
  2444. hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
  2445. int retry_counter)
  2446. {
  2447. static DEFINE_MUTEX(usb_address0_mutex);
  2448. struct usb_device *hdev = hub->hdev;
  2449. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2450. int i, j, retval;
  2451. unsigned delay = HUB_SHORT_RESET_TIME;
  2452. enum usb_device_speed oldspeed = udev->speed;
  2453. char *speed, *type;
  2454. int devnum = udev->devnum;
  2455. /* root hub ports have a slightly longer reset period
  2456. * (from USB 2.0 spec, section 7.1.7.5)
  2457. */
  2458. if (!hdev->parent) {
  2459. delay = HUB_ROOT_RESET_TIME;
  2460. if (port1 == hdev->bus->otg_port)
  2461. hdev->bus->b_hnp_enable = 0;
  2462. }
  2463. /* Some low speed devices have problems with the quick delay, so */
  2464. /* be a bit pessimistic with those devices. RHbug #23670 */
  2465. if (oldspeed == USB_SPEED_LOW)
  2466. delay = HUB_LONG_RESET_TIME;
  2467. mutex_lock(&usb_address0_mutex);
  2468. /* Reset the device; full speed may morph to high speed */
  2469. /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
  2470. retval = hub_port_reset(hub, port1, udev, delay);
  2471. if (retval < 0) /* error or disconnect */
  2472. goto fail;
  2473. /* success, speed is known */
  2474. retval = -ENODEV;
  2475. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
  2476. dev_dbg(&udev->dev, "device reset changed speed!\n");
  2477. goto fail;
  2478. }
  2479. oldspeed = udev->speed;
  2480. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  2481. * it's fixed size except for full speed devices.
  2482. * For Wireless USB devices, ep0 max packet is always 512 (tho
  2483. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  2484. */
  2485. switch (udev->speed) {
  2486. case USB_SPEED_SUPER:
  2487. case USB_SPEED_WIRELESS: /* fixed at 512 */
  2488. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  2489. break;
  2490. case USB_SPEED_HIGH: /* fixed at 64 */
  2491. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2492. break;
  2493. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  2494. /* to determine the ep0 maxpacket size, try to read
  2495. * the device descriptor to get bMaxPacketSize0 and
  2496. * then correct our initial guess.
  2497. */
  2498. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2499. break;
  2500. case USB_SPEED_LOW: /* fixed at 8 */
  2501. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  2502. break;
  2503. default:
  2504. goto fail;
  2505. }
  2506. type = "";
  2507. switch (udev->speed) {
  2508. case USB_SPEED_LOW: speed = "low"; break;
  2509. case USB_SPEED_FULL: speed = "full"; break;
  2510. case USB_SPEED_HIGH: speed = "high"; break;
  2511. case USB_SPEED_SUPER:
  2512. speed = "super";
  2513. break;
  2514. case USB_SPEED_WIRELESS:
  2515. speed = "variable";
  2516. type = "Wireless ";
  2517. break;
  2518. default: speed = "?"; break;
  2519. }
  2520. if (udev->speed != USB_SPEED_SUPER)
  2521. dev_info(&udev->dev,
  2522. "%s %s speed %sUSB device number %d using %s\n",
  2523. (udev->config) ? "reset" : "new", speed, type,
  2524. devnum, udev->bus->controller->driver->name);
  2525. /* Set up TT records, if needed */
  2526. if (hdev->tt) {
  2527. udev->tt = hdev->tt;
  2528. udev->ttport = hdev->ttport;
  2529. } else if (udev->speed != USB_SPEED_HIGH
  2530. && hdev->speed == USB_SPEED_HIGH) {
  2531. if (!hub->tt.hub) {
  2532. dev_err(&udev->dev, "parent hub has no TT\n");
  2533. retval = -EINVAL;
  2534. goto fail;
  2535. }
  2536. udev->tt = &hub->tt;
  2537. udev->ttport = port1;
  2538. }
  2539. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  2540. * Because device hardware and firmware is sometimes buggy in
  2541. * this area, and this is how Linux has done it for ages.
  2542. * Change it cautiously.
  2543. *
  2544. * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
  2545. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  2546. * so it may help with some non-standards-compliant devices.
  2547. * Otherwise we start with SET_ADDRESS and then try to read the
  2548. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  2549. * value.
  2550. */
  2551. for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
  2552. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
  2553. struct usb_device_descriptor *buf;
  2554. int r = 0;
  2555. #define GET_DESCRIPTOR_BUFSIZE 64
  2556. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  2557. if (!buf) {
  2558. retval = -ENOMEM;
  2559. continue;
  2560. }
  2561. /* Retry on all errors; some devices are flakey.
  2562. * 255 is for WUSB devices, we actually need to use
  2563. * 512 (WUSB1.0[4.8.1]).
  2564. */
  2565. for (j = 0; j < 3; ++j) {
  2566. buf->bMaxPacketSize0 = 0;
  2567. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  2568. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  2569. USB_DT_DEVICE << 8, 0,
  2570. buf, GET_DESCRIPTOR_BUFSIZE,
  2571. initial_descriptor_timeout);
  2572. switch (buf->bMaxPacketSize0) {
  2573. case 8: case 16: case 32: case 64: case 255:
  2574. if (buf->bDescriptorType ==
  2575. USB_DT_DEVICE) {
  2576. r = 0;
  2577. break;
  2578. }
  2579. /* FALL THROUGH */
  2580. default:
  2581. if (r == 0)
  2582. r = -EPROTO;
  2583. break;
  2584. }
  2585. if (r == 0)
  2586. break;
  2587. }
  2588. udev->descriptor.bMaxPacketSize0 =
  2589. buf->bMaxPacketSize0;
  2590. kfree(buf);
  2591. retval = hub_port_reset(hub, port1, udev, delay);
  2592. if (retval < 0) /* error or disconnect */
  2593. goto fail;
  2594. if (oldspeed != udev->speed) {
  2595. dev_dbg(&udev->dev,
  2596. "device reset changed speed!\n");
  2597. retval = -ENODEV;
  2598. goto fail;
  2599. }
  2600. if (r) {
  2601. dev_err(&udev->dev,
  2602. "device descriptor read/64, error %d\n",
  2603. r);
  2604. retval = -EMSGSIZE;
  2605. continue;
  2606. }
  2607. #undef GET_DESCRIPTOR_BUFSIZE
  2608. }
  2609. /*
  2610. * If device is WUSB, we already assigned an
  2611. * unauthorized address in the Connect Ack sequence;
  2612. * authorization will assign the final address.
  2613. */
  2614. if (udev->wusb == 0) {
  2615. for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
  2616. retval = hub_set_address(udev, devnum);
  2617. if (retval >= 0)
  2618. break;
  2619. msleep(200);
  2620. }
  2621. if (retval < 0) {
  2622. dev_err(&udev->dev,
  2623. "device not accepting address %d, error %d\n",
  2624. devnum, retval);
  2625. goto fail;
  2626. }
  2627. if (udev->speed == USB_SPEED_SUPER) {
  2628. devnum = udev->devnum;
  2629. dev_info(&udev->dev,
  2630. "%s SuperSpeed USB device number %d using %s\n",
  2631. (udev->config) ? "reset" : "new",
  2632. devnum, udev->bus->controller->driver->name);
  2633. }
  2634. /* cope with hardware quirkiness:
  2635. * - let SET_ADDRESS settle, some device hardware wants it
  2636. * - read ep0 maxpacket even for high and low speed,
  2637. */
  2638. msleep(10);
  2639. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
  2640. break;
  2641. }
  2642. retval = usb_get_device_descriptor(udev, 8);
  2643. if (retval < 8) {
  2644. dev_err(&udev->dev,
  2645. "device descriptor read/8, error %d\n",
  2646. retval);
  2647. if (retval >= 0)
  2648. retval = -EMSGSIZE;
  2649. } else {
  2650. retval = 0;
  2651. break;
  2652. }
  2653. }
  2654. if (retval)
  2655. goto fail;
  2656. if (udev->descriptor.bMaxPacketSize0 == 0xff ||
  2657. udev->speed == USB_SPEED_SUPER)
  2658. i = 512;
  2659. else
  2660. i = udev->descriptor.bMaxPacketSize0;
  2661. if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
  2662. if (udev->speed == USB_SPEED_LOW ||
  2663. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  2664. dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
  2665. retval = -EMSGSIZE;
  2666. goto fail;
  2667. }
  2668. if (udev->speed == USB_SPEED_FULL)
  2669. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  2670. else
  2671. dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
  2672. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  2673. usb_ep0_reinit(udev);
  2674. }
  2675. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  2676. if (retval < (signed)sizeof(udev->descriptor)) {
  2677. dev_err(&udev->dev, "device descriptor read/all, error %d\n",
  2678. retval);
  2679. if (retval >= 0)
  2680. retval = -ENOMSG;
  2681. goto fail;
  2682. }
  2683. retval = 0;
  2684. /* notify HCD that we have a device connected and addressed */
  2685. if (hcd->driver->update_device)
  2686. hcd->driver->update_device(hcd, udev);
  2687. fail:
  2688. if (retval) {
  2689. hub_port_disable(hub, port1, 0);
  2690. update_devnum(udev, devnum); /* for disconnect processing */
  2691. }
  2692. mutex_unlock(&usb_address0_mutex);
  2693. return retval;
  2694. }
  2695. static void
  2696. check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
  2697. {
  2698. struct usb_qualifier_descriptor *qual;
  2699. int status;
  2700. qual = kmalloc (sizeof *qual, GFP_KERNEL);
  2701. if (qual == NULL)
  2702. return;
  2703. status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
  2704. qual, sizeof *qual);
  2705. if (status == sizeof *qual) {
  2706. dev_info(&udev->dev, "not running at top speed; "
  2707. "connect to a high speed hub\n");
  2708. /* hub LEDs are probably harder to miss than syslog */
  2709. if (hub->has_indicators) {
  2710. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  2711. schedule_delayed_work (&hub->leds, 0);
  2712. }
  2713. }
  2714. kfree(qual);
  2715. }
  2716. static unsigned
  2717. hub_power_remaining (struct usb_hub *hub)
  2718. {
  2719. struct usb_device *hdev = hub->hdev;
  2720. int remaining;
  2721. int port1;
  2722. if (!hub->limited_power)
  2723. return 0;
  2724. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  2725. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  2726. struct usb_device *udev = hdev->children[port1 - 1];
  2727. int delta;
  2728. if (!udev)
  2729. continue;
  2730. /* Unconfigured devices may not use more than 100mA,
  2731. * or 8mA for OTG ports */
  2732. if (udev->actconfig)
  2733. delta = udev->actconfig->desc.bMaxPower * 2;
  2734. else if (port1 != udev->bus->otg_port || hdev->parent)
  2735. delta = 100;
  2736. else
  2737. delta = 8;
  2738. if (delta > hub->mA_per_port)
  2739. dev_warn(&udev->dev,
  2740. "%dmA is over %umA budget for port %d!\n",
  2741. delta, hub->mA_per_port, port1);
  2742. remaining -= delta;
  2743. }
  2744. if (remaining < 0) {
  2745. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  2746. - remaining);
  2747. remaining = 0;
  2748. }
  2749. return remaining;
  2750. }
  2751. /* Handle physical or logical connection change events.
  2752. * This routine is called when:
  2753. * a port connection-change occurs;
  2754. * a port enable-change occurs (often caused by EMI);
  2755. * usb_reset_and_verify_device() encounters changed descriptors (as from
  2756. * a firmware download)
  2757. * caller already locked the hub
  2758. */
  2759. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  2760. u16 portstatus, u16 portchange)
  2761. {
  2762. struct usb_device *hdev = hub->hdev;
  2763. struct device *hub_dev = hub->intfdev;
  2764. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2765. unsigned wHubCharacteristics =
  2766. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  2767. struct usb_device *udev;
  2768. int status, i;
  2769. dev_dbg (hub_dev,
  2770. "port %d, status %04x, change %04x, %s\n",
  2771. port1, portstatus, portchange, portspeed(hub, portstatus));
  2772. if (hub->has_indicators) {
  2773. set_port_led(hub, port1, HUB_LED_AUTO);
  2774. hub->indicator[port1-1] = INDICATOR_AUTO;
  2775. }
  2776. #ifdef CONFIG_USB_OTG
  2777. /* during HNP, don't repeat the debounce */
  2778. if (hdev->bus->is_b_host)
  2779. portchange &= ~(USB_PORT_STAT_C_CONNECTION |
  2780. USB_PORT_STAT_C_ENABLE);
  2781. #endif
  2782. /* Try to resuscitate an existing device */
  2783. udev = hdev->children[port1-1];
  2784. if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
  2785. udev->state != USB_STATE_NOTATTACHED) {
  2786. usb_lock_device(udev);
  2787. if (portstatus & USB_PORT_STAT_ENABLE) {
  2788. status = 0; /* Nothing to do */
  2789. #ifdef CONFIG_USB_SUSPEND
  2790. } else if (udev->state == USB_STATE_SUSPENDED &&
  2791. udev->persist_enabled) {
  2792. /* For a suspended device, treat this as a
  2793. * remote wakeup event.
  2794. */
  2795. status = usb_remote_wakeup(udev);
  2796. #endif
  2797. } else {
  2798. status = -ENODEV; /* Don't resuscitate */
  2799. }
  2800. usb_unlock_device(udev);
  2801. if (status == 0) {
  2802. clear_bit(port1, hub->change_bits);
  2803. return;
  2804. }
  2805. }
  2806. /* Disconnect any existing devices under this port */
  2807. if (udev)
  2808. usb_disconnect(&hdev->children[port1-1]);
  2809. clear_bit(port1, hub->change_bits);
  2810. /* We can forget about a "removed" device when there's a physical
  2811. * disconnect or the connect status changes.
  2812. */
  2813. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2814. (portchange & USB_PORT_STAT_C_CONNECTION))
  2815. clear_bit(port1, hub->removed_bits);
  2816. if (portchange & (USB_PORT_STAT_C_CONNECTION |
  2817. USB_PORT_STAT_C_ENABLE)) {
  2818. status = hub_port_debounce(hub, port1);
  2819. if (status < 0) {
  2820. if (printk_ratelimit())
  2821. dev_err(hub_dev, "connect-debounce failed, "
  2822. "port %d disabled\n", port1);
  2823. portstatus &= ~USB_PORT_STAT_CONNECTION;
  2824. } else {
  2825. portstatus = status;
  2826. }
  2827. }
  2828. /* Return now if debouncing failed or nothing is connected or
  2829. * the device was "removed".
  2830. */
  2831. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2832. test_bit(port1, hub->removed_bits)) {
  2833. /* maybe switch power back on (e.g. root hub was reset) */
  2834. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
  2835. && !port_is_power_on(hub, portstatus))
  2836. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  2837. if (portstatus & USB_PORT_STAT_ENABLE)
  2838. goto done;
  2839. return;
  2840. }
  2841. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  2842. /* reallocate for each attempt, since references
  2843. * to the previous one can escape in various ways
  2844. */
  2845. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  2846. if (!udev) {
  2847. dev_err (hub_dev,
  2848. "couldn't allocate port %d usb_device\n",
  2849. port1);
  2850. goto done;
  2851. }
  2852. usb_set_device_state(udev, USB_STATE_POWERED);
  2853. udev->bus_mA = hub->mA_per_port;
  2854. udev->level = hdev->level + 1;
  2855. udev->wusb = hub_is_wusb(hub);
  2856. /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
  2857. if (hub_is_superspeed(hub->hdev))
  2858. udev->speed = USB_SPEED_SUPER;
  2859. else
  2860. udev->speed = USB_SPEED_UNKNOWN;
  2861. choose_devnum(udev);
  2862. if (udev->devnum <= 0) {
  2863. status = -ENOTCONN; /* Don't retry */
  2864. goto loop;
  2865. }
  2866. /* reset (non-USB 3.0 devices) and get descriptor */
  2867. status = hub_port_init(hub, udev, port1, i);
  2868. if (status < 0)
  2869. goto loop;
  2870. usb_detect_quirks(udev);
  2871. if (udev->quirks & USB_QUIRK_DELAY_INIT)
  2872. msleep(1000);
  2873. /* consecutive bus-powered hubs aren't reliable; they can
  2874. * violate the voltage drop budget. if the new child has
  2875. * a "powered" LED, users should notice we didn't enable it
  2876. * (without reading syslog), even without per-port LEDs
  2877. * on the parent.
  2878. */
  2879. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  2880. && udev->bus_mA <= 100) {
  2881. u16 devstat;
  2882. status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
  2883. &devstat);
  2884. if (status < 2) {
  2885. dev_dbg(&udev->dev, "get status %d ?\n", status);
  2886. goto loop_disable;
  2887. }
  2888. le16_to_cpus(&devstat);
  2889. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  2890. dev_err(&udev->dev,
  2891. "can't connect bus-powered hub "
  2892. "to this port\n");
  2893. if (hub->has_indicators) {
  2894. hub->indicator[port1-1] =
  2895. INDICATOR_AMBER_BLINK;
  2896. schedule_delayed_work (&hub->leds, 0);
  2897. }
  2898. status = -ENOTCONN; /* Don't retry */
  2899. goto loop_disable;
  2900. }
  2901. }
  2902. /* check for devices running slower than they could */
  2903. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  2904. && udev->speed == USB_SPEED_FULL
  2905. && highspeed_hubs != 0)
  2906. check_highspeed (hub, udev, port1);
  2907. /* Store the parent's children[] pointer. At this point
  2908. * udev becomes globally accessible, although presumably
  2909. * no one will look at it until hdev is unlocked.
  2910. */
  2911. status = 0;
  2912. /* We mustn't add new devices if the parent hub has
  2913. * been disconnected; we would race with the
  2914. * recursively_mark_NOTATTACHED() routine.
  2915. */
  2916. spin_lock_irq(&device_state_lock);
  2917. if (hdev->state == USB_STATE_NOTATTACHED)
  2918. status = -ENOTCONN;
  2919. else
  2920. hdev->children[port1-1] = udev;
  2921. spin_unlock_irq(&device_state_lock);
  2922. /* Run it through the hoops (find a driver, etc) */
  2923. if (!status) {
  2924. status = usb_new_device(udev);
  2925. if (status) {
  2926. spin_lock_irq(&device_state_lock);
  2927. hdev->children[port1-1] = NULL;
  2928. spin_unlock_irq(&device_state_lock);
  2929. }
  2930. }
  2931. if (status)
  2932. goto loop_disable;
  2933. status = hub_power_remaining(hub);
  2934. if (status)
  2935. dev_dbg(hub_dev, "%dmA power budget left\n", status);
  2936. return;
  2937. loop_disable:
  2938. hub_port_disable(hub, port1, 1);
  2939. loop:
  2940. usb_ep0_reinit(udev);
  2941. release_devnum(udev);
  2942. hub_free_dev(udev);
  2943. usb_put_dev(udev);
  2944. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  2945. break;
  2946. }
  2947. if (hub->hdev->parent ||
  2948. !hcd->driver->port_handed_over ||
  2949. !(hcd->driver->port_handed_over)(hcd, port1))
  2950. dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
  2951. port1);
  2952. done:
  2953. hub_port_disable(hub, port1, 1);
  2954. if (hcd->driver->relinquish_port && !hub->hdev->parent)
  2955. hcd->driver->relinquish_port(hcd, port1);
  2956. }
  2957. static void hub_events(void)
  2958. {
  2959. struct list_head *tmp;
  2960. struct usb_device *hdev;
  2961. struct usb_interface *intf;
  2962. struct usb_hub *hub;
  2963. struct device *hub_dev;
  2964. u16 hubstatus;
  2965. u16 hubchange;
  2966. u16 portstatus;
  2967. u16 portchange;
  2968. int i, ret;
  2969. int connect_change;
  2970. /*
  2971. * We restart the list every time to avoid a deadlock with
  2972. * deleting hubs downstream from this one. This should be
  2973. * safe since we delete the hub from the event list.
  2974. * Not the most efficient, but avoids deadlocks.
  2975. */
  2976. while (1) {
  2977. /* Grab the first entry at the beginning of the list */
  2978. spin_lock_irq(&hub_event_lock);
  2979. if (list_empty(&hub_event_list)) {
  2980. spin_unlock_irq(&hub_event_lock);
  2981. break;
  2982. }
  2983. tmp = hub_event_list.next;
  2984. list_del_init(tmp);
  2985. hub = list_entry(tmp, struct usb_hub, event_list);
  2986. kref_get(&hub->kref);
  2987. spin_unlock_irq(&hub_event_lock);
  2988. hdev = hub->hdev;
  2989. hub_dev = hub->intfdev;
  2990. intf = to_usb_interface(hub_dev);
  2991. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  2992. hdev->state, hub->descriptor
  2993. ? hub->descriptor->bNbrPorts
  2994. : 0,
  2995. /* NOTE: expects max 15 ports... */
  2996. (u16) hub->change_bits[0],
  2997. (u16) hub->event_bits[0]);
  2998. /* Lock the device, then check to see if we were
  2999. * disconnected while waiting for the lock to succeed. */
  3000. usb_lock_device(hdev);
  3001. if (unlikely(hub->disconnected))
  3002. goto loop_disconnected;
  3003. /* If the hub has died, clean up after it */
  3004. if (hdev->state == USB_STATE_NOTATTACHED) {
  3005. hub->error = -ENODEV;
  3006. hub_quiesce(hub, HUB_DISCONNECT);
  3007. goto loop;
  3008. }
  3009. /* Autoresume */
  3010. ret = usb_autopm_get_interface(intf);
  3011. if (ret) {
  3012. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  3013. goto loop;
  3014. }
  3015. /* If this is an inactive hub, do nothing */
  3016. if (hub->quiescing)
  3017. goto loop_autopm;
  3018. if (hub->error) {
  3019. dev_dbg (hub_dev, "resetting for error %d\n",
  3020. hub->error);
  3021. ret = usb_reset_device(hdev);
  3022. if (ret) {
  3023. dev_dbg (hub_dev,
  3024. "error resetting hub: %d\n", ret);
  3025. goto loop_autopm;
  3026. }
  3027. hub->nerrors = 0;
  3028. hub->error = 0;
  3029. }
  3030. /* deal with port status changes */
  3031. for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
  3032. if (test_bit(i, hub->busy_bits))
  3033. continue;
  3034. connect_change = test_bit(i, hub->change_bits);
  3035. if (!test_and_clear_bit(i, hub->event_bits) &&
  3036. !connect_change)
  3037. continue;
  3038. ret = hub_port_status(hub, i,
  3039. &portstatus, &portchange);
  3040. if (ret < 0)
  3041. continue;
  3042. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  3043. clear_port_feature(hdev, i,
  3044. USB_PORT_FEAT_C_CONNECTION);
  3045. connect_change = 1;
  3046. }
  3047. if (portchange & USB_PORT_STAT_C_ENABLE) {
  3048. if (!connect_change)
  3049. dev_dbg (hub_dev,
  3050. "port %d enable change, "
  3051. "status %08x\n",
  3052. i, portstatus);
  3053. clear_port_feature(hdev, i,
  3054. USB_PORT_FEAT_C_ENABLE);
  3055. /*
  3056. * EM interference sometimes causes badly
  3057. * shielded USB devices to be shutdown by
  3058. * the hub, this hack enables them again.
  3059. * Works at least with mouse driver.
  3060. */
  3061. if (!(portstatus & USB_PORT_STAT_ENABLE)
  3062. && !connect_change
  3063. && hdev->children[i-1]) {
  3064. dev_err (hub_dev,
  3065. "port %i "
  3066. "disabled by hub (EMI?), "
  3067. "re-enabling...\n",
  3068. i);
  3069. connect_change = 1;
  3070. }
  3071. }
  3072. if (portchange & USB_PORT_STAT_C_SUSPEND) {
  3073. struct usb_device *udev;
  3074. clear_port_feature(hdev, i,
  3075. USB_PORT_FEAT_C_SUSPEND);
  3076. udev = hdev->children[i-1];
  3077. if (udev) {
  3078. /* TRSMRCY = 10 msec */
  3079. msleep(10);
  3080. usb_lock_device(udev);
  3081. ret = usb_remote_wakeup(hdev->
  3082. children[i-1]);
  3083. usb_unlock_device(udev);
  3084. if (ret < 0)
  3085. connect_change = 1;
  3086. } else {
  3087. ret = -ENODEV;
  3088. hub_port_disable(hub, i, 1);
  3089. }
  3090. dev_dbg (hub_dev,
  3091. "resume on port %d, status %d\n",
  3092. i, ret);
  3093. }
  3094. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  3095. u16 status = 0;
  3096. u16 unused;
  3097. dev_dbg(hub_dev, "over-current change on port "
  3098. "%d\n", i);
  3099. clear_port_feature(hdev, i,
  3100. USB_PORT_FEAT_C_OVER_CURRENT);
  3101. msleep(100); /* Cool down */
  3102. hub_power_on(hub, true);
  3103. hub_port_status(hub, i, &status, &unused);
  3104. if (status & USB_PORT_STAT_OVERCURRENT)
  3105. dev_err(hub_dev, "over-current "
  3106. "condition on port %d\n", i);
  3107. }
  3108. if (portchange & USB_PORT_STAT_C_RESET) {
  3109. dev_dbg (hub_dev,
  3110. "reset change on port %d\n",
  3111. i);
  3112. clear_port_feature(hdev, i,
  3113. USB_PORT_FEAT_C_RESET);
  3114. }
  3115. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  3116. hub_is_superspeed(hub->hdev)) {
  3117. dev_dbg(hub_dev,
  3118. "warm reset change on port %d\n",
  3119. i);
  3120. clear_port_feature(hdev, i,
  3121. USB_PORT_FEAT_C_BH_PORT_RESET);
  3122. }
  3123. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  3124. clear_port_feature(hub->hdev, i,
  3125. USB_PORT_FEAT_C_PORT_LINK_STATE);
  3126. }
  3127. if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
  3128. dev_warn(hub_dev,
  3129. "config error on port %d\n",
  3130. i);
  3131. clear_port_feature(hub->hdev, i,
  3132. USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
  3133. }
  3134. /* Warm reset a USB3 protocol port if it's in
  3135. * SS.Inactive state.
  3136. */
  3137. if (hub_is_superspeed(hub->hdev) &&
  3138. (portstatus & USB_PORT_STAT_LINK_STATE)
  3139. == USB_SS_PORT_LS_SS_INACTIVE) {
  3140. dev_dbg(hub_dev, "warm reset port %d\n", i);
  3141. hub_port_warm_reset(hub, i);
  3142. }
  3143. if (connect_change)
  3144. hub_port_connect_change(hub, i,
  3145. portstatus, portchange);
  3146. } /* end for i */
  3147. /* deal with hub status changes */
  3148. if (test_and_clear_bit(0, hub->event_bits) == 0)
  3149. ; /* do nothing */
  3150. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  3151. dev_err (hub_dev, "get_hub_status failed\n");
  3152. else {
  3153. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  3154. dev_dbg (hub_dev, "power change\n");
  3155. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  3156. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  3157. /* FIXME: Is this always true? */
  3158. hub->limited_power = 1;
  3159. else
  3160. hub->limited_power = 0;
  3161. }
  3162. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  3163. u16 status = 0;
  3164. u16 unused;
  3165. dev_dbg(hub_dev, "over-current change\n");
  3166. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  3167. msleep(500); /* Cool down */
  3168. hub_power_on(hub, true);
  3169. hub_hub_status(hub, &status, &unused);
  3170. if (status & HUB_STATUS_OVERCURRENT)
  3171. dev_err(hub_dev, "over-current "
  3172. "condition\n");
  3173. }
  3174. }
  3175. loop_autopm:
  3176. /* Balance the usb_autopm_get_interface() above */
  3177. usb_autopm_put_interface_no_suspend(intf);
  3178. loop:
  3179. /* Balance the usb_autopm_get_interface_no_resume() in
  3180. * kick_khubd() and allow autosuspend.
  3181. */
  3182. usb_autopm_put_interface(intf);
  3183. loop_disconnected:
  3184. usb_unlock_device(hdev);
  3185. kref_put(&hub->kref, hub_release);
  3186. } /* end while (1) */
  3187. }
  3188. static int hub_thread(void *__unused)
  3189. {
  3190. /* khubd needs to be freezable to avoid intefering with USB-PERSIST
  3191. * port handover. Otherwise it might see that a full-speed device
  3192. * was gone before the EHCI controller had handed its port over to
  3193. * the companion full-speed controller.
  3194. */
  3195. set_freezable();
  3196. do {
  3197. hub_events();
  3198. wait_event_freezable(khubd_wait,
  3199. !list_empty(&hub_event_list) ||
  3200. kthread_should_stop());
  3201. } while (!kthread_should_stop() || !list_empty(&hub_event_list));
  3202. pr_debug("%s: khubd exiting\n", usbcore_name);
  3203. return 0;
  3204. }
  3205. static const struct usb_device_id hub_id_table[] = {
  3206. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  3207. .bDeviceClass = USB_CLASS_HUB},
  3208. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  3209. .bInterfaceClass = USB_CLASS_HUB},
  3210. { } /* Terminating entry */
  3211. };
  3212. MODULE_DEVICE_TABLE (usb, hub_id_table);
  3213. static struct usb_driver hub_driver = {
  3214. .name = "hub",
  3215. .probe = hub_probe,
  3216. .disconnect = hub_disconnect,
  3217. .suspend = hub_suspend,
  3218. .resume = hub_resume,
  3219. .reset_resume = hub_reset_resume,
  3220. .pre_reset = hub_pre_reset,
  3221. .post_reset = hub_post_reset,
  3222. .unlocked_ioctl = hub_ioctl,
  3223. .id_table = hub_id_table,
  3224. .supports_autosuspend = 1,
  3225. };
  3226. int usb_hub_init(void)
  3227. {
  3228. if (usb_register(&hub_driver) < 0) {
  3229. printk(KERN_ERR "%s: can't register hub driver\n",
  3230. usbcore_name);
  3231. return -1;
  3232. }
  3233. khubd_task = kthread_run(hub_thread, NULL, "khubd");
  3234. if (!IS_ERR(khubd_task))
  3235. return 0;
  3236. /* Fall through if kernel_thread failed */
  3237. usb_deregister(&hub_driver);
  3238. printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
  3239. return -1;
  3240. }
  3241. void usb_hub_cleanup(void)
  3242. {
  3243. kthread_stop(khubd_task);
  3244. /*
  3245. * Hub resources are freed for us by usb_deregister. It calls
  3246. * usb_driver_purge on every device which in turn calls that
  3247. * devices disconnect function if it is using this driver.
  3248. * The hub_disconnect function takes care of releasing the
  3249. * individual hub resources. -greg
  3250. */
  3251. usb_deregister(&hub_driver);
  3252. } /* usb_hub_cleanup() */
  3253. static int descriptors_changed(struct usb_device *udev,
  3254. struct usb_device_descriptor *old_device_descriptor)
  3255. {
  3256. int changed = 0;
  3257. unsigned index;
  3258. unsigned serial_len = 0;
  3259. unsigned len;
  3260. unsigned old_length;
  3261. int length;
  3262. char *buf;
  3263. if (memcmp(&udev->descriptor, old_device_descriptor,
  3264. sizeof(*old_device_descriptor)) != 0)
  3265. return 1;
  3266. /* Since the idVendor, idProduct, and bcdDevice values in the
  3267. * device descriptor haven't changed, we will assume the
  3268. * Manufacturer and Product strings haven't changed either.
  3269. * But the SerialNumber string could be different (e.g., a
  3270. * different flash card of the same brand).
  3271. */
  3272. if (udev->serial)
  3273. serial_len = strlen(udev->serial) + 1;
  3274. len = serial_len;
  3275. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3276. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3277. len = max(len, old_length);
  3278. }
  3279. buf = kmalloc(len, GFP_NOIO);
  3280. if (buf == NULL) {
  3281. dev_err(&udev->dev, "no mem to re-read configs after reset\n");
  3282. /* assume the worst */
  3283. return 1;
  3284. }
  3285. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3286. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3287. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  3288. old_length);
  3289. if (length != old_length) {
  3290. dev_dbg(&udev->dev, "config index %d, error %d\n",
  3291. index, length);
  3292. changed = 1;
  3293. break;
  3294. }
  3295. if (memcmp (buf, udev->rawdescriptors[index], old_length)
  3296. != 0) {
  3297. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  3298. index,
  3299. ((struct usb_config_descriptor *) buf)->
  3300. bConfigurationValue);
  3301. changed = 1;
  3302. break;
  3303. }
  3304. }
  3305. if (!changed && serial_len) {
  3306. length = usb_string(udev, udev->descriptor.iSerialNumber,
  3307. buf, serial_len);
  3308. if (length + 1 != serial_len) {
  3309. dev_dbg(&udev->dev, "serial string error %d\n",
  3310. length);
  3311. changed = 1;
  3312. } else if (memcmp(buf, udev->serial, length) != 0) {
  3313. dev_dbg(&udev->dev, "serial string changed\n");
  3314. changed = 1;
  3315. }
  3316. }
  3317. kfree(buf);
  3318. return changed;
  3319. }
  3320. /**
  3321. * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
  3322. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3323. *
  3324. * WARNING - don't use this routine to reset a composite device
  3325. * (one with multiple interfaces owned by separate drivers)!
  3326. * Use usb_reset_device() instead.
  3327. *
  3328. * Do a port reset, reassign the device's address, and establish its
  3329. * former operating configuration. If the reset fails, or the device's
  3330. * descriptors change from their values before the reset, or the original
  3331. * configuration and altsettings cannot be restored, a flag will be set
  3332. * telling khubd to pretend the device has been disconnected and then
  3333. * re-connected. All drivers will be unbound, and the device will be
  3334. * re-enumerated and probed all over again.
  3335. *
  3336. * Returns 0 if the reset succeeded, -ENODEV if the device has been
  3337. * flagged for logical disconnection, or some other negative error code
  3338. * if the reset wasn't even attempted.
  3339. *
  3340. * The caller must own the device lock. For example, it's safe to use
  3341. * this from a driver probe() routine after downloading new firmware.
  3342. * For calls that might not occur during probe(), drivers should lock
  3343. * the device using usb_lock_device_for_reset().
  3344. *
  3345. * Locking exception: This routine may also be called from within an
  3346. * autoresume handler. Such usage won't conflict with other tasks
  3347. * holding the device lock because these tasks should always call
  3348. * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
  3349. */
  3350. static int usb_reset_and_verify_device(struct usb_device *udev)
  3351. {
  3352. struct usb_device *parent_hdev = udev->parent;
  3353. struct usb_hub *parent_hub;
  3354. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3355. struct usb_device_descriptor descriptor = udev->descriptor;
  3356. int i, ret = 0;
  3357. int port1 = udev->portnum;
  3358. if (udev->state == USB_STATE_NOTATTACHED ||
  3359. udev->state == USB_STATE_SUSPENDED) {
  3360. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3361. udev->state);
  3362. return -EINVAL;
  3363. }
  3364. if (!parent_hdev) {
  3365. /* this requires hcd-specific logic; see ohci_restart() */
  3366. dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
  3367. return -EISDIR;
  3368. }
  3369. parent_hub = hdev_to_hub(parent_hdev);
  3370. set_bit(port1, parent_hub->busy_bits);
  3371. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  3372. /* ep0 maxpacket size may change; let the HCD know about it.
  3373. * Other endpoints will be handled by re-enumeration. */
  3374. usb_ep0_reinit(udev);
  3375. ret = hub_port_init(parent_hub, udev, port1, i);
  3376. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  3377. break;
  3378. }
  3379. clear_bit(port1, parent_hub->busy_bits);
  3380. if (ret < 0)
  3381. goto re_enumerate;
  3382. /* Device might have changed firmware (DFU or similar) */
  3383. if (descriptors_changed(udev, &descriptor)) {
  3384. dev_info(&udev->dev, "device firmware changed\n");
  3385. udev->descriptor = descriptor; /* for disconnect() calls */
  3386. goto re_enumerate;
  3387. }
  3388. /* Restore the device's previous configuration */
  3389. if (!udev->actconfig)
  3390. goto done;
  3391. mutex_lock(hcd->bandwidth_mutex);
  3392. ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
  3393. if (ret < 0) {
  3394. dev_warn(&udev->dev,
  3395. "Busted HC? Not enough HCD resources for "
  3396. "old configuration.\n");
  3397. mutex_unlock(hcd->bandwidth_mutex);
  3398. goto re_enumerate;
  3399. }
  3400. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3401. USB_REQ_SET_CONFIGURATION, 0,
  3402. udev->actconfig->desc.bConfigurationValue, 0,
  3403. NULL, 0, USB_CTRL_SET_TIMEOUT);
  3404. if (ret < 0) {
  3405. dev_err(&udev->dev,
  3406. "can't restore configuration #%d (error=%d)\n",
  3407. udev->actconfig->desc.bConfigurationValue, ret);
  3408. mutex_unlock(hcd->bandwidth_mutex);
  3409. goto re_enumerate;
  3410. }
  3411. mutex_unlock(hcd->bandwidth_mutex);
  3412. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  3413. /* Put interfaces back into the same altsettings as before.
  3414. * Don't bother to send the Set-Interface request for interfaces
  3415. * that were already in altsetting 0; besides being unnecessary,
  3416. * many devices can't handle it. Instead just reset the host-side
  3417. * endpoint state.
  3418. */
  3419. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  3420. struct usb_host_config *config = udev->actconfig;
  3421. struct usb_interface *intf = config->interface[i];
  3422. struct usb_interface_descriptor *desc;
  3423. desc = &intf->cur_altsetting->desc;
  3424. if (desc->bAlternateSetting == 0) {
  3425. usb_disable_interface(udev, intf, true);
  3426. usb_enable_interface(udev, intf, true);
  3427. ret = 0;
  3428. } else {
  3429. /* Let the bandwidth allocation function know that this
  3430. * device has been reset, and it will have to use
  3431. * alternate setting 0 as the current alternate setting.
  3432. */
  3433. intf->resetting_device = 1;
  3434. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  3435. desc->bAlternateSetting);
  3436. intf->resetting_device = 0;
  3437. }
  3438. if (ret < 0) {
  3439. dev_err(&udev->dev, "failed to restore interface %d "
  3440. "altsetting %d (error=%d)\n",
  3441. desc->bInterfaceNumber,
  3442. desc->bAlternateSetting,
  3443. ret);
  3444. goto re_enumerate;
  3445. }
  3446. }
  3447. done:
  3448. return 0;
  3449. re_enumerate:
  3450. hub_port_logical_disconnect(parent_hub, port1);
  3451. return -ENODEV;
  3452. }
  3453. /**
  3454. * usb_reset_device - warn interface drivers and perform a USB port reset
  3455. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3456. *
  3457. * Warns all drivers bound to registered interfaces (using their pre_reset
  3458. * method), performs the port reset, and then lets the drivers know that
  3459. * the reset is over (using their post_reset method).
  3460. *
  3461. * Return value is the same as for usb_reset_and_verify_device().
  3462. *
  3463. * The caller must own the device lock. For example, it's safe to use
  3464. * this from a driver probe() routine after downloading new firmware.
  3465. * For calls that might not occur during probe(), drivers should lock
  3466. * the device using usb_lock_device_for_reset().
  3467. *
  3468. * If an interface is currently being probed or disconnected, we assume
  3469. * its driver knows how to handle resets. For all other interfaces,
  3470. * if the driver doesn't have pre_reset and post_reset methods then
  3471. * we attempt to unbind it and rebind afterward.
  3472. */
  3473. int usb_reset_device(struct usb_device *udev)
  3474. {
  3475. int ret;
  3476. int i;
  3477. struct usb_host_config *config = udev->actconfig;
  3478. if (udev->state == USB_STATE_NOTATTACHED ||
  3479. udev->state == USB_STATE_SUSPENDED) {
  3480. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3481. udev->state);
  3482. return -EINVAL;
  3483. }
  3484. /* Prevent autosuspend during the reset */
  3485. usb_autoresume_device(udev);
  3486. if (config) {
  3487. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  3488. struct usb_interface *cintf = config->interface[i];
  3489. struct usb_driver *drv;
  3490. int unbind = 0;
  3491. if (cintf->dev.driver) {
  3492. drv = to_usb_driver(cintf->dev.driver);
  3493. if (drv->pre_reset && drv->post_reset)
  3494. unbind = (drv->pre_reset)(cintf);
  3495. else if (cintf->condition ==
  3496. USB_INTERFACE_BOUND)
  3497. unbind = 1;
  3498. if (unbind)
  3499. usb_forced_unbind_intf(cintf);
  3500. }
  3501. }
  3502. }
  3503. ret = usb_reset_and_verify_device(udev);
  3504. if (config) {
  3505. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  3506. struct usb_interface *cintf = config->interface[i];
  3507. struct usb_driver *drv;
  3508. int rebind = cintf->needs_binding;
  3509. if (!rebind && cintf->dev.driver) {
  3510. drv = to_usb_driver(cintf->dev.driver);
  3511. if (drv->post_reset)
  3512. rebind = (drv->post_reset)(cintf);
  3513. else if (cintf->condition ==
  3514. USB_INTERFACE_BOUND)
  3515. rebind = 1;
  3516. }
  3517. if (ret == 0 && rebind)
  3518. usb_rebind_intf(cintf);
  3519. }
  3520. }
  3521. usb_autosuspend_device(udev);
  3522. return ret;
  3523. }
  3524. EXPORT_SYMBOL_GPL(usb_reset_device);
  3525. /**
  3526. * usb_queue_reset_device - Reset a USB device from an atomic context
  3527. * @iface: USB interface belonging to the device to reset
  3528. *
  3529. * This function can be used to reset a USB device from an atomic
  3530. * context, where usb_reset_device() won't work (as it blocks).
  3531. *
  3532. * Doing a reset via this method is functionally equivalent to calling
  3533. * usb_reset_device(), except for the fact that it is delayed to a
  3534. * workqueue. This means that any drivers bound to other interfaces
  3535. * might be unbound, as well as users from usbfs in user space.
  3536. *
  3537. * Corner cases:
  3538. *
  3539. * - Scheduling two resets at the same time from two different drivers
  3540. * attached to two different interfaces of the same device is
  3541. * possible; depending on how the driver attached to each interface
  3542. * handles ->pre_reset(), the second reset might happen or not.
  3543. *
  3544. * - If a driver is unbound and it had a pending reset, the reset will
  3545. * be cancelled.
  3546. *
  3547. * - This function can be called during .probe() or .disconnect()
  3548. * times. On return from .disconnect(), any pending resets will be
  3549. * cancelled.
  3550. *
  3551. * There is no no need to lock/unlock the @reset_ws as schedule_work()
  3552. * does its own.
  3553. *
  3554. * NOTE: We don't do any reference count tracking because it is not
  3555. * needed. The lifecycle of the work_struct is tied to the
  3556. * usb_interface. Before destroying the interface we cancel the
  3557. * work_struct, so the fact that work_struct is queued and or
  3558. * running means the interface (and thus, the device) exist and
  3559. * are referenced.
  3560. */
  3561. void usb_queue_reset_device(struct usb_interface *iface)
  3562. {
  3563. schedule_work(&iface->reset_ws);
  3564. }
  3565. EXPORT_SYMBOL_GPL(usb_queue_reset_device);