port.c 16 KB

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