port.c 15 KB

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