port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * usb port device code
  3. *
  4. * Copyright (C) 2012 Intel Corp
  5. *
  6. * Author: Lan Tianyu <tianyu.lan@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/pm_qos.h>
  20. #include "hub.h"
  21. static int usb_port_block_power_off;
  22. static const struct attribute_group *port_dev_group[];
  23. static ssize_t connect_type_show(struct device *dev,
  24. struct device_attribute *attr, char *buf)
  25. {
  26. struct usb_port *port_dev = to_usb_port(dev);
  27. char *result;
  28. switch (port_dev->connect_type) {
  29. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  30. result = "hotplug";
  31. break;
  32. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  33. result = "hardwired";
  34. break;
  35. case USB_PORT_NOT_USED:
  36. result = "not used";
  37. break;
  38. default:
  39. result = "unknown";
  40. break;
  41. }
  42. return sprintf(buf, "%s\n", result);
  43. }
  44. static DEVICE_ATTR_RO(connect_type);
  45. static struct attribute *port_dev_attrs[] = {
  46. &dev_attr_connect_type.attr,
  47. NULL,
  48. };
  49. static struct attribute_group port_dev_attr_grp = {
  50. .attrs = port_dev_attrs,
  51. };
  52. static const struct attribute_group *port_dev_group[] = {
  53. &port_dev_attr_grp,
  54. NULL,
  55. };
  56. static void usb_port_device_release(struct device *dev)
  57. {
  58. struct usb_port *port_dev = to_usb_port(dev);
  59. kfree(port_dev->req);
  60. kfree(port_dev);
  61. }
  62. #ifdef CONFIG_PM
  63. static int usb_port_runtime_resume(struct device *dev)
  64. {
  65. struct usb_port *port_dev = to_usb_port(dev);
  66. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  67. struct usb_interface *intf = to_usb_interface(dev->parent);
  68. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  69. struct usb_device *udev = port_dev->child;
  70. struct usb_port *peer = port_dev->peer;
  71. int port1 = port_dev->portnum;
  72. int retval;
  73. if (!hub)
  74. return -EINVAL;
  75. if (hub->in_reset) {
  76. set_bit(port1, hub->power_bits);
  77. return 0;
  78. }
  79. /*
  80. * Power on our usb3 peer before this usb2 port to prevent a usb3
  81. * device from degrading to its usb2 connection
  82. */
  83. if (!port_dev->is_superspeed && peer)
  84. pm_runtime_get_sync(&peer->dev);
  85. usb_autopm_get_interface(intf);
  86. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  87. msleep(hub_power_on_good_delay(hub));
  88. if (udev && !retval) {
  89. /*
  90. * Our preference is to simply wait for the port to reconnect,
  91. * as that is the lowest latency method to restart the port.
  92. * However, there are cases where toggling port power results in
  93. * the host port and the device port getting out of sync causing
  94. * a link training live lock. Upon timeout, flag the port as
  95. * needing warm reset recovery (to be performed later by
  96. * usb_port_resume() as requested via usb_wakeup_notification())
  97. */
  98. if (hub_port_debounce_be_connected(hub, port1) < 0) {
  99. dev_dbg(&port_dev->dev, "reconnect timeout\n");
  100. if (hub_is_superspeed(hdev))
  101. set_bit(port1, hub->warm_reset_bits);
  102. }
  103. /* Force the child awake to revalidate after the power loss. */
  104. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  105. pm_runtime_get_noresume(&port_dev->dev);
  106. pm_request_resume(&udev->dev);
  107. }
  108. }
  109. usb_autopm_put_interface(intf);
  110. return retval;
  111. }
  112. static int usb_port_runtime_suspend(struct device *dev)
  113. {
  114. struct usb_port *port_dev = to_usb_port(dev);
  115. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  116. struct usb_interface *intf = to_usb_interface(dev->parent);
  117. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  118. struct usb_port *peer = port_dev->peer;
  119. int port1 = port_dev->portnum;
  120. int retval;
  121. if (!hub)
  122. return -EINVAL;
  123. if (hub->in_reset)
  124. return -EBUSY;
  125. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  126. == PM_QOS_FLAGS_ALL)
  127. return -EAGAIN;
  128. if (usb_port_block_power_off)
  129. return -EBUSY;
  130. usb_autopm_get_interface(intf);
  131. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  132. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  133. if (!port_dev->is_superspeed)
  134. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  135. usb_autopm_put_interface(intf);
  136. /*
  137. * Our peer usb3 port may now be able to suspend, so
  138. * asynchronously queue a suspend request to observe that this
  139. * usb2 port is now off.
  140. */
  141. if (!port_dev->is_superspeed && peer)
  142. pm_runtime_put(&peer->dev);
  143. return retval;
  144. }
  145. #endif
  146. static const struct dev_pm_ops usb_port_pm_ops = {
  147. #ifdef CONFIG_PM
  148. .runtime_suspend = usb_port_runtime_suspend,
  149. .runtime_resume = usb_port_runtime_resume,
  150. #endif
  151. };
  152. struct device_type usb_port_device_type = {
  153. .name = "usb_port",
  154. .release = usb_port_device_release,
  155. .pm = &usb_port_pm_ops,
  156. };
  157. static struct device_driver usb_port_driver = {
  158. .name = "usb",
  159. .owner = THIS_MODULE,
  160. };
  161. static int link_peers(struct usb_port *left, struct usb_port *right)
  162. {
  163. struct usb_port *ss_port, *hs_port;
  164. int rc;
  165. if (left->peer == right && right->peer == left)
  166. return 0;
  167. if (left->peer || right->peer) {
  168. struct usb_port *lpeer = left->peer;
  169. struct usb_port *rpeer = right->peer;
  170. char *method;
  171. if (left->location && left->location == right->location)
  172. method = "location";
  173. else
  174. method = "default";
  175. pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
  176. dev_name(&left->dev), dev_name(&right->dev), method,
  177. dev_name(&left->dev),
  178. lpeer ? dev_name(&lpeer->dev) : "none",
  179. dev_name(&right->dev),
  180. rpeer ? dev_name(&rpeer->dev) : "none");
  181. return -EBUSY;
  182. }
  183. rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
  184. if (rc)
  185. return rc;
  186. rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
  187. if (rc) {
  188. sysfs_remove_link(&left->dev.kobj, "peer");
  189. return rc;
  190. }
  191. /*
  192. * We need to wake the HiSpeed port to make sure we don't race
  193. * setting ->peer with usb_port_runtime_suspend(). Otherwise we
  194. * may miss a suspend event for the SuperSpeed port.
  195. */
  196. if (left->is_superspeed) {
  197. ss_port = left;
  198. WARN_ON(right->is_superspeed);
  199. hs_port = right;
  200. } else {
  201. ss_port = right;
  202. WARN_ON(!right->is_superspeed);
  203. hs_port = left;
  204. }
  205. pm_runtime_get_sync(&hs_port->dev);
  206. left->peer = right;
  207. right->peer = left;
  208. /*
  209. * The SuperSpeed reference is dropped when the HiSpeed port in
  210. * this relationship suspends, i.e. when it is safe to allow a
  211. * SuperSpeed connection to drop since there is no risk of a
  212. * device degrading to its powered-off HiSpeed connection.
  213. *
  214. * Also, drop the HiSpeed ref taken above.
  215. */
  216. pm_runtime_get_sync(&ss_port->dev);
  217. pm_runtime_put(&hs_port->dev);
  218. return 0;
  219. }
  220. static void link_peers_report(struct usb_port *left, struct usb_port *right)
  221. {
  222. int rc;
  223. rc = link_peers(left, right);
  224. if (rc == 0) {
  225. dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
  226. } else {
  227. dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
  228. dev_name(&right->dev), rc);
  229. pr_warn_once("usb: port power management may be unreliable\n");
  230. usb_port_block_power_off = 1;
  231. }
  232. }
  233. static void unlink_peers(struct usb_port *left, struct usb_port *right)
  234. {
  235. struct usb_port *ss_port, *hs_port;
  236. WARN(right->peer != left || left->peer != right,
  237. "%s and %s are not peers?\n",
  238. dev_name(&left->dev), dev_name(&right->dev));
  239. /*
  240. * We wake the HiSpeed port to make sure we don't race its
  241. * usb_port_runtime_resume() event which takes a SuperSpeed ref
  242. * when ->peer is !NULL.
  243. */
  244. if (left->is_superspeed) {
  245. ss_port = left;
  246. hs_port = right;
  247. } else {
  248. ss_port = right;
  249. hs_port = left;
  250. }
  251. pm_runtime_get_sync(&hs_port->dev);
  252. sysfs_remove_link(&left->dev.kobj, "peer");
  253. right->peer = NULL;
  254. sysfs_remove_link(&right->dev.kobj, "peer");
  255. left->peer = NULL;
  256. /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
  257. pm_runtime_put(&ss_port->dev);
  258. /* Drop the ref taken above */
  259. pm_runtime_put(&hs_port->dev);
  260. }
  261. /*
  262. * For each usb hub device in the system check to see if it is in the
  263. * peer domain of the given port_dev, and if it is check to see if it
  264. * has a port that matches the given port by location
  265. */
  266. static int match_location(struct usb_device *peer_hdev, void *p)
  267. {
  268. int port1;
  269. struct usb_hcd *hcd, *peer_hcd;
  270. struct usb_port *port_dev = p, *peer;
  271. struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
  272. struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
  273. if (!peer_hub)
  274. return 0;
  275. hcd = bus_to_hcd(hdev->bus);
  276. peer_hcd = bus_to_hcd(peer_hdev->bus);
  277. /* peer_hcd is provisional until we verify it against the known peer */
  278. if (peer_hcd != hcd->shared_hcd)
  279. return 0;
  280. for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
  281. peer = peer_hub->ports[port1 - 1];
  282. if (peer && peer->location == port_dev->location) {
  283. link_peers_report(port_dev, peer);
  284. return 1; /* done */
  285. }
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Find the peer port either via explicit platform firmware "location"
  291. * data, the peer hcd for root hubs, or the upstream peer relationship
  292. * for all other hubs.
  293. */
  294. static void find_and_link_peer(struct usb_hub *hub, int port1)
  295. {
  296. struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
  297. struct usb_device *hdev = hub->hdev;
  298. struct usb_device *peer_hdev;
  299. struct usb_hub *peer_hub;
  300. /*
  301. * If location data is available then we can only peer this port
  302. * by a location match, not the default peer (lest we create a
  303. * situation where we need to go back and undo a default peering
  304. * when the port is later peered by location data)
  305. */
  306. if (port_dev->location) {
  307. /* we link the peer in match_location() if found */
  308. usb_for_each_dev(port_dev, match_location);
  309. return;
  310. } else if (!hdev->parent) {
  311. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  312. struct usb_hcd *peer_hcd = hcd->shared_hcd;
  313. if (!peer_hcd)
  314. return;
  315. peer_hdev = peer_hcd->self.root_hub;
  316. } else {
  317. struct usb_port *upstream;
  318. struct usb_device *parent = hdev->parent;
  319. struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
  320. if (!parent_hub)
  321. return;
  322. upstream = parent_hub->ports[hdev->portnum - 1];
  323. if (!upstream || !upstream->peer)
  324. return;
  325. peer_hdev = upstream->peer->child;
  326. }
  327. peer_hub = usb_hub_to_struct_hub(peer_hdev);
  328. if (!peer_hub || port1 > peer_hdev->maxchild)
  329. return;
  330. /*
  331. * we found a valid default peer, last check is to make sure it
  332. * does not have location data
  333. */
  334. peer = peer_hub->ports[port1 - 1];
  335. if (peer && peer->location == 0)
  336. link_peers_report(port_dev, peer);
  337. }
  338. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  339. {
  340. struct usb_port *port_dev;
  341. int retval;
  342. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  343. if (!port_dev)
  344. return -ENOMEM;
  345. port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
  346. if (!port_dev->req) {
  347. kfree(port_dev);
  348. return -ENOMEM;
  349. }
  350. hub->ports[port1 - 1] = port_dev;
  351. port_dev->portnum = port1;
  352. set_bit(port1, hub->power_bits);
  353. port_dev->dev.parent = hub->intfdev;
  354. port_dev->dev.groups = port_dev_group;
  355. port_dev->dev.type = &usb_port_device_type;
  356. port_dev->dev.driver = &usb_port_driver;
  357. if (hub_is_superspeed(hub->hdev))
  358. port_dev->is_superspeed = 1;
  359. dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
  360. port1);
  361. mutex_init(&port_dev->status_lock);
  362. retval = device_register(&port_dev->dev);
  363. if (retval) {
  364. put_device(&port_dev->dev);
  365. return retval;
  366. }
  367. /* Set default policy of port-poweroff disabled. */
  368. retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
  369. DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
  370. if (retval < 0) {
  371. device_unregister(&port_dev->dev);
  372. return retval;
  373. }
  374. find_and_link_peer(hub, port1);
  375. /*
  376. * Enable runtime pm and hold a refernce that hub_configure()
  377. * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
  378. * and the hub has been fully registered (hdev->maxchild set).
  379. */
  380. pm_runtime_set_active(&port_dev->dev);
  381. pm_runtime_get_noresume(&port_dev->dev);
  382. pm_runtime_enable(&port_dev->dev);
  383. device_enable_async_suspend(&port_dev->dev);
  384. /*
  385. * Keep hidden the ability to enable port-poweroff if the hub
  386. * does not support power switching.
  387. */
  388. if (!hub_is_port_power_switchable(hub))
  389. return 0;
  390. /* Attempt to let userspace take over the policy. */
  391. retval = dev_pm_qos_expose_flags(&port_dev->dev,
  392. PM_QOS_FLAG_NO_POWER_OFF);
  393. if (retval < 0) {
  394. dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
  395. return 0;
  396. }
  397. /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
  398. retval = dev_pm_qos_remove_request(port_dev->req);
  399. if (retval >= 0) {
  400. kfree(port_dev->req);
  401. port_dev->req = NULL;
  402. }
  403. return 0;
  404. }
  405. void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
  406. {
  407. struct usb_port *port_dev = hub->ports[port1 - 1];
  408. struct usb_port *peer;
  409. peer = port_dev->peer;
  410. if (peer)
  411. unlink_peers(port_dev, peer);
  412. device_unregister(&port_dev->dev);
  413. }