port.c 15 KB

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