port.c 14 KB

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