port.c 15 KB

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