port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. usb_autopm_get_interface(intf);
  171. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  172. msleep(hub_power_on_good_delay(hub));
  173. if (udev && !retval) {
  174. /*
  175. * Our preference is to simply wait for the port to reconnect,
  176. * as that is the lowest latency method to restart the port.
  177. * However, there are cases where toggling port power results in
  178. * the host port and the device port getting out of sync causing
  179. * a link training live lock. Upon timeout, flag the port as
  180. * needing warm reset recovery (to be performed later by
  181. * usb_port_resume() as requested via usb_wakeup_notification())
  182. */
  183. if (hub_port_debounce_be_connected(hub, port1) < 0) {
  184. dev_dbg(&port_dev->dev, "reconnect timeout\n");
  185. if (hub_is_superspeed(hdev))
  186. set_bit(port1, hub->warm_reset_bits);
  187. }
  188. /* Force the child awake to revalidate after the power loss. */
  189. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  190. pm_runtime_get_noresume(&port_dev->dev);
  191. pm_request_resume(&udev->dev);
  192. }
  193. }
  194. usb_autopm_put_interface(intf);
  195. return retval;
  196. }
  197. static int usb_port_runtime_suspend(struct device *dev)
  198. {
  199. struct usb_port *port_dev = to_usb_port(dev);
  200. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  201. struct usb_interface *intf = to_usb_interface(dev->parent);
  202. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  203. struct usb_port *peer = port_dev->peer;
  204. int port1 = port_dev->portnum;
  205. int retval;
  206. if (!hub)
  207. return -EINVAL;
  208. if (hub->in_reset)
  209. return -EBUSY;
  210. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  211. == PM_QOS_FLAGS_ALL)
  212. return -EAGAIN;
  213. if (usb_port_block_power_off)
  214. return -EBUSY;
  215. usb_autopm_get_interface(intf);
  216. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  217. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  218. if (!port_dev->is_superspeed)
  219. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  220. usb_autopm_put_interface(intf);
  221. /*
  222. * Our peer usb3 port may now be able to suspend, so
  223. * asynchronously queue a suspend request to observe that this
  224. * usb2 port is now off.
  225. */
  226. if (!port_dev->is_superspeed && peer)
  227. pm_runtime_put(&peer->dev);
  228. return retval;
  229. }
  230. #endif
  231. static const struct dev_pm_ops usb_port_pm_ops = {
  232. #ifdef CONFIG_PM
  233. .runtime_suspend = usb_port_runtime_suspend,
  234. .runtime_resume = usb_port_runtime_resume,
  235. #endif
  236. };
  237. struct device_type usb_port_device_type = {
  238. .name = "usb_port",
  239. .release = usb_port_device_release,
  240. .pm = &usb_port_pm_ops,
  241. };
  242. static struct device_driver usb_port_driver = {
  243. .name = "usb",
  244. .owner = THIS_MODULE,
  245. };
  246. static int link_peers(struct usb_port *left, struct usb_port *right)
  247. {
  248. struct usb_port *ss_port, *hs_port;
  249. int rc;
  250. if (left->peer == right && right->peer == left)
  251. return 0;
  252. if (left->peer || right->peer) {
  253. struct usb_port *lpeer = left->peer;
  254. struct usb_port *rpeer = right->peer;
  255. char *method;
  256. if (left->location && left->location == right->location)
  257. method = "location";
  258. else
  259. method = "default";
  260. pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
  261. dev_name(&left->dev), dev_name(&right->dev), method,
  262. dev_name(&left->dev),
  263. lpeer ? dev_name(&lpeer->dev) : "none",
  264. dev_name(&right->dev),
  265. rpeer ? dev_name(&rpeer->dev) : "none");
  266. return -EBUSY;
  267. }
  268. rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
  269. if (rc)
  270. return rc;
  271. rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
  272. if (rc) {
  273. sysfs_remove_link(&left->dev.kobj, "peer");
  274. return rc;
  275. }
  276. /*
  277. * We need to wake the HiSpeed port to make sure we don't race
  278. * setting ->peer with usb_port_runtime_suspend(). Otherwise we
  279. * may miss a suspend event for the SuperSpeed port.
  280. */
  281. if (left->is_superspeed) {
  282. ss_port = left;
  283. WARN_ON(right->is_superspeed);
  284. hs_port = right;
  285. } else {
  286. ss_port = right;
  287. WARN_ON(!right->is_superspeed);
  288. hs_port = left;
  289. }
  290. pm_runtime_get_sync(&hs_port->dev);
  291. left->peer = right;
  292. right->peer = left;
  293. /*
  294. * The SuperSpeed reference is dropped when the HiSpeed port in
  295. * this relationship suspends, i.e. when it is safe to allow a
  296. * SuperSpeed connection to drop since there is no risk of a
  297. * device degrading to its powered-off HiSpeed connection.
  298. *
  299. * Also, drop the HiSpeed ref taken above.
  300. */
  301. pm_runtime_get_sync(&ss_port->dev);
  302. pm_runtime_put(&hs_port->dev);
  303. return 0;
  304. }
  305. static void link_peers_report(struct usb_port *left, struct usb_port *right)
  306. {
  307. int rc;
  308. rc = link_peers(left, right);
  309. if (rc == 0) {
  310. dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
  311. } else {
  312. dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
  313. dev_name(&right->dev), rc);
  314. pr_warn_once("usb: port power management may be unreliable\n");
  315. usb_port_block_power_off = 1;
  316. }
  317. }
  318. static void unlink_peers(struct usb_port *left, struct usb_port *right)
  319. {
  320. struct usb_port *ss_port, *hs_port;
  321. WARN(right->peer != left || left->peer != right,
  322. "%s and %s are not peers?\n",
  323. dev_name(&left->dev), dev_name(&right->dev));
  324. /*
  325. * We wake the HiSpeed port to make sure we don't race its
  326. * usb_port_runtime_resume() event which takes a SuperSpeed ref
  327. * when ->peer is !NULL.
  328. */
  329. if (left->is_superspeed) {
  330. ss_port = left;
  331. hs_port = right;
  332. } else {
  333. ss_port = right;
  334. hs_port = left;
  335. }
  336. pm_runtime_get_sync(&hs_port->dev);
  337. sysfs_remove_link(&left->dev.kobj, "peer");
  338. right->peer = NULL;
  339. sysfs_remove_link(&right->dev.kobj, "peer");
  340. left->peer = NULL;
  341. /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
  342. pm_runtime_put(&ss_port->dev);
  343. /* Drop the ref taken above */
  344. pm_runtime_put(&hs_port->dev);
  345. }
  346. /*
  347. * For each usb hub device in the system check to see if it is in the
  348. * peer domain of the given port_dev, and if it is check to see if it
  349. * has a port that matches the given port by location
  350. */
  351. static int match_location(struct usb_device *peer_hdev, void *p)
  352. {
  353. int port1;
  354. struct usb_hcd *hcd, *peer_hcd;
  355. struct usb_port *port_dev = p, *peer;
  356. struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
  357. struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
  358. if (!peer_hub)
  359. return 0;
  360. hcd = bus_to_hcd(hdev->bus);
  361. peer_hcd = bus_to_hcd(peer_hdev->bus);
  362. /* peer_hcd is provisional until we verify it against the known peer */
  363. if (peer_hcd != hcd->shared_hcd)
  364. return 0;
  365. for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
  366. peer = peer_hub->ports[port1 - 1];
  367. if (peer && peer->location == port_dev->location) {
  368. link_peers_report(port_dev, peer);
  369. return 1; /* done */
  370. }
  371. }
  372. return 0;
  373. }
  374. /*
  375. * Find the peer port either via explicit platform firmware "location"
  376. * data, the peer hcd for root hubs, or the upstream peer relationship
  377. * for all other hubs.
  378. */
  379. static void find_and_link_peer(struct usb_hub *hub, int port1)
  380. {
  381. struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
  382. struct usb_device *hdev = hub->hdev;
  383. struct usb_device *peer_hdev;
  384. struct usb_hub *peer_hub;
  385. /*
  386. * If location data is available then we can only peer this port
  387. * by a location match, not the default peer (lest we create a
  388. * situation where we need to go back and undo a default peering
  389. * when the port is later peered by location data)
  390. */
  391. if (port_dev->location) {
  392. /* we link the peer in match_location() if found */
  393. usb_for_each_dev(port_dev, match_location);
  394. return;
  395. } else if (!hdev->parent) {
  396. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  397. struct usb_hcd *peer_hcd = hcd->shared_hcd;
  398. if (!peer_hcd)
  399. return;
  400. peer_hdev = peer_hcd->self.root_hub;
  401. } else {
  402. struct usb_port *upstream;
  403. struct usb_device *parent = hdev->parent;
  404. struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
  405. if (!parent_hub)
  406. return;
  407. upstream = parent_hub->ports[hdev->portnum - 1];
  408. if (!upstream || !upstream->peer)
  409. return;
  410. peer_hdev = upstream->peer->child;
  411. }
  412. peer_hub = usb_hub_to_struct_hub(peer_hdev);
  413. if (!peer_hub || port1 > peer_hdev->maxchild)
  414. return;
  415. /*
  416. * we found a valid default peer, last check is to make sure it
  417. * does not have location data
  418. */
  419. peer = peer_hub->ports[port1 - 1];
  420. if (peer && peer->location == 0)
  421. link_peers_report(port_dev, peer);
  422. }
  423. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  424. {
  425. struct usb_port *port_dev;
  426. struct usb_device *hdev = hub->hdev;
  427. int retval;
  428. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  429. if (!port_dev)
  430. return -ENOMEM;
  431. port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
  432. if (!port_dev->req) {
  433. kfree(port_dev);
  434. return -ENOMEM;
  435. }
  436. hub->ports[port1 - 1] = port_dev;
  437. port_dev->portnum = port1;
  438. set_bit(port1, hub->power_bits);
  439. port_dev->dev.parent = hub->intfdev;
  440. if (hub_is_superspeed(hdev)) {
  441. port_dev->usb3_lpm_u1_permit = 1;
  442. port_dev->usb3_lpm_u2_permit = 1;
  443. port_dev->dev.groups = port_dev_usb3_group;
  444. } else
  445. port_dev->dev.groups = port_dev_group;
  446. port_dev->dev.type = &usb_port_device_type;
  447. port_dev->dev.driver = &usb_port_driver;
  448. if (hub_is_superspeed(hub->hdev))
  449. port_dev->is_superspeed = 1;
  450. dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
  451. port1);
  452. mutex_init(&port_dev->status_lock);
  453. retval = device_register(&port_dev->dev);
  454. if (retval) {
  455. put_device(&port_dev->dev);
  456. return retval;
  457. }
  458. /* Set default policy of port-poweroff disabled. */
  459. retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
  460. DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
  461. if (retval < 0) {
  462. device_unregister(&port_dev->dev);
  463. return retval;
  464. }
  465. find_and_link_peer(hub, port1);
  466. /*
  467. * Enable runtime pm and hold a refernce that hub_configure()
  468. * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
  469. * and the hub has been fully registered (hdev->maxchild set).
  470. */
  471. pm_runtime_set_active(&port_dev->dev);
  472. pm_runtime_get_noresume(&port_dev->dev);
  473. pm_runtime_enable(&port_dev->dev);
  474. device_enable_async_suspend(&port_dev->dev);
  475. /*
  476. * Keep hidden the ability to enable port-poweroff if the hub
  477. * does not support power switching.
  478. */
  479. if (!hub_is_port_power_switchable(hub))
  480. return 0;
  481. /* Attempt to let userspace take over the policy. */
  482. retval = dev_pm_qos_expose_flags(&port_dev->dev,
  483. PM_QOS_FLAG_NO_POWER_OFF);
  484. if (retval < 0) {
  485. dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
  486. return 0;
  487. }
  488. /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
  489. retval = dev_pm_qos_remove_request(port_dev->req);
  490. if (retval >= 0) {
  491. kfree(port_dev->req);
  492. port_dev->req = NULL;
  493. }
  494. return 0;
  495. }
  496. void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
  497. {
  498. struct usb_port *port_dev = hub->ports[port1 - 1];
  499. struct usb_port *peer;
  500. peer = port_dev->peer;
  501. if (peer)
  502. unlink_peers(port_dev, peer);
  503. device_unregister(&port_dev->dev);
  504. }