port.c 15 KB

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