/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

Large files are truncated click here to view the full file

  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->