port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 struct attribute *port_dev_attrs[] = {
  46. &dev_attr_connect_type.attr,
  47. NULL,
  48. };
  49. static struct attribute_group port_dev_attr_grp = {
  50. .attrs = port_dev_attrs,
  51. };
  52. static const struct attribute_group *port_dev_group[] = {
  53. &port_dev_attr_grp,
  54. NULL,
  55. };
  56. static void usb_port_device_release(struct device *dev)
  57. {
  58. struct usb_port *port_dev = to_usb_port(dev);
  59. kfree(port_dev->req);
  60. kfree(port_dev);
  61. }
  62. #ifdef CONFIG_PM_RUNTIME
  63. static int usb_port_runtime_resume(struct device *dev)
  64. {
  65. struct usb_port *port_dev = to_usb_port(dev);
  66. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  67. struct usb_interface *intf = to_usb_interface(dev->parent);
  68. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  69. struct usb_device *udev = port_dev->child;
  70. struct usb_port *peer = port_dev->peer;
  71. int port1 = port_dev->portnum;
  72. int retval;
  73. if (!hub)
  74. return -EINVAL;
  75. if (hub->in_reset) {
  76. set_bit(port1, hub->power_bits);
  77. return 0;
  78. }
  79. /*
  80. * Power on our usb3 peer before this usb2 port to prevent a usb3
  81. * device from degrading to its usb2 connection
  82. */
  83. if (!port_dev->is_superspeed && peer)
  84. pm_runtime_get_sync(&peer->dev);
  85. usb_autopm_get_interface(intf);
  86. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  87. msleep(hub_power_on_good_delay(hub));
  88. if (udev && !retval) {
  89. /*
  90. * Attempt to wait for usb hub port to be reconnected in order
  91. * to make the resume procedure successful. The device may have
  92. * disconnected while the port was powered off, so ignore the
  93. * return status.
  94. */
  95. retval = hub_port_debounce_be_connected(hub, port1);
  96. if (retval < 0)
  97. dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
  98. retval);
  99. retval = 0;
  100. /* Force the child awake to revalidate after the power loss. */
  101. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  102. pm_runtime_get_noresume(&port_dev->dev);
  103. pm_request_resume(&udev->dev);
  104. }
  105. }
  106. usb_autopm_put_interface(intf);
  107. return retval;
  108. }
  109. static int usb_port_runtime_suspend(struct device *dev)
  110. {
  111. struct usb_port *port_dev = to_usb_port(dev);
  112. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  113. struct usb_interface *intf = to_usb_interface(dev->parent);
  114. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  115. struct usb_port *peer = port_dev->peer;
  116. int port1 = port_dev->portnum;
  117. int retval;
  118. if (!hub)
  119. return -EINVAL;
  120. if (hub->in_reset)
  121. return -EBUSY;
  122. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  123. == PM_QOS_FLAGS_ALL)
  124. return -EAGAIN;
  125. if (usb_port_block_power_off)
  126. return -EBUSY;
  127. usb_autopm_get_interface(intf);
  128. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  129. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  130. if (!port_dev->is_superspeed)
  131. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  132. usb_autopm_put_interface(intf);
  133. /*
  134. * Our peer usb3 port may now be able to suspend, so
  135. * asynchronously queue a suspend request to observe that this
  136. * usb2 port is now off.
  137. */
  138. if (!port_dev->is_superspeed && peer)
  139. pm_runtime_put(&peer->dev);
  140. return retval;
  141. }
  142. #endif
  143. static const struct dev_pm_ops usb_port_pm_ops = {
  144. #ifdef CONFIG_PM_RUNTIME
  145. .runtime_suspend = usb_port_runtime_suspend,
  146. .runtime_resume = usb_port_runtime_resume,
  147. #endif
  148. };
  149. struct device_type usb_port_device_type = {
  150. .name = "usb_port",
  151. .release = usb_port_device_release,
  152. .pm = &usb_port_pm_ops,
  153. };
  154. static struct device_driver usb_port_driver = {
  155. .name = "usb",
  156. .owner = THIS_MODULE,
  157. };
  158. static int link_peers(struct usb_port *left, struct usb_port *right)
  159. {
  160. struct usb_port *ss_port, *hs_port;
  161. int rc;
  162. if (left->peer == right && right->peer == left)
  163. return 0;
  164. if (left->peer || right->peer) {
  165. struct usb_port *lpeer = left->peer;
  166. struct usb_port *rpeer = right->peer;
  167. char *method;
  168. if (left->location && left->location == right->location)
  169. method = "location";
  170. else
  171. method = "default";
  172. pr_warn("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
  173. dev_name(&left->dev), dev_name(&right->dev), method,
  174. dev_name(&left->dev),
  175. lpeer ? dev_name(&lpeer->dev) : "none",
  176. dev_name(&right->dev),
  177. rpeer ? dev_name(&rpeer->dev) : "none");
  178. return -EBUSY;
  179. }
  180. rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
  181. if (rc)
  182. return rc;
  183. rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
  184. if (rc) {
  185. sysfs_remove_link(&left->dev.kobj, "peer");
  186. return rc;
  187. }
  188. /*
  189. * We need to wake the HiSpeed port to make sure we don't race
  190. * setting ->peer with usb_port_runtime_suspend(). Otherwise we
  191. * may miss a suspend event for the SuperSpeed port.
  192. */
  193. if (left->is_superspeed) {
  194. ss_port = left;
  195. WARN_ON(right->is_superspeed);
  196. hs_port = right;
  197. } else {
  198. ss_port = right;
  199. WARN_ON(!right->is_superspeed);
  200. hs_port = left;
  201. }
  202. pm_runtime_get_sync(&hs_port->dev);
  203. left->peer = right;
  204. right->peer = left;
  205. /*
  206. * The SuperSpeed reference is dropped when the HiSpeed port in
  207. * this relationship suspends, i.e. when it is safe to allow a
  208. * SuperSpeed connection to drop since there is no risk of a
  209. * device degrading to its powered-off HiSpeed connection.
  210. *
  211. * Also, drop the HiSpeed ref taken above.
  212. */
  213. pm_runtime_get_sync(&ss_port->dev);
  214. pm_runtime_put(&hs_port->dev);
  215. return 0;
  216. }
  217. static void link_peers_report(struct usb_port *left, struct usb_port *right)
  218. {
  219. int rc;
  220. rc = link_peers(left, right);
  221. if (rc == 0) {
  222. dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
  223. } else {
  224. dev_warn(&left->dev, "failed to peer to %s (%d)\n",
  225. dev_name(&right->dev), rc);
  226. pr_warn_once("usb: port power management may be unreliable\n");
  227. usb_port_block_power_off = 1;
  228. }
  229. }
  230. static void unlink_peers(struct usb_port *left, struct usb_port *right)
  231. {
  232. struct usb_port *ss_port, *hs_port;
  233. WARN(right->peer != left || left->peer != right,
  234. "%s and %s are not peers?\n",
  235. dev_name(&left->dev), dev_name(&right->dev));
  236. /*
  237. * We wake the HiSpeed port to make sure we don't race its
  238. * usb_port_runtime_resume() event which takes a SuperSpeed ref
  239. * when ->peer is !NULL.
  240. */
  241. if (left->is_superspeed) {
  242. ss_port = left;
  243. hs_port = right;
  244. } else {
  245. ss_port = right;
  246. hs_port = left;
  247. }
  248. pm_runtime_get_sync(&hs_port->dev);
  249. sysfs_remove_link(&left->dev.kobj, "peer");
  250. right->peer = NULL;
  251. sysfs_remove_link(&right->dev.kobj, "peer");
  252. left->peer = NULL;
  253. /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
  254. pm_runtime_put(&ss_port->dev);
  255. /* Drop the ref taken above */
  256. pm_runtime_put(&hs_port->dev);
  257. }
  258. /*
  259. * For each usb hub device in the system check to see if it is in the
  260. * peer domain of the given port_dev, and if it is check to see if it
  261. * has a port that matches the given port by location
  262. */
  263. static int match_location(struct usb_device *peer_hdev, void *p)
  264. {
  265. int port1;
  266. struct usb_hcd *hcd, *peer_hcd;
  267. struct usb_port *port_dev = p, *peer;
  268. struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
  269. struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
  270. if (!peer_hub)
  271. return 0;
  272. hcd = bus_to_hcd(hdev->bus);
  273. peer_hcd = bus_to_hcd(peer_hdev->bus);
  274. /* peer_hcd is provisional until we verify it against the known peer */
  275. if (peer_hcd != hcd->shared_hcd)
  276. return 0;
  277. for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
  278. peer = peer_hub->ports[port1 - 1];
  279. if (peer && peer->location == port_dev->location) {
  280. link_peers_report(port_dev, peer);
  281. return 1; /* done */
  282. }
  283. }
  284. return 0;
  285. }
  286. /*
  287. * Find the peer port either via explicit platform firmware "location"
  288. * data, the peer hcd for root hubs, or the upstream peer relationship
  289. * for all other hubs.
  290. */
  291. static void find_and_link_peer(struct usb_hub *hub, int port1)
  292. {
  293. struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
  294. struct usb_device *hdev = hub->hdev;
  295. struct usb_device *peer_hdev;
  296. struct usb_hub *peer_hub;
  297. /*
  298. * If location data is available then we can only peer this port
  299. * by a location match, not the default peer (lest we create a
  300. * situation where we need to go back and undo a default peering
  301. * when the port is later peered by location data)
  302. */
  303. if (port_dev->location) {
  304. /* we link the peer in match_location() if found */
  305. usb_for_each_dev(port_dev, match_location);
  306. return;
  307. } else if (!hdev->parent) {
  308. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  309. struct usb_hcd *peer_hcd = hcd->shared_hcd;
  310. if (!peer_hcd)
  311. return;
  312. peer_hdev = peer_hcd->self.root_hub;
  313. } else {
  314. struct usb_port *upstream;
  315. struct usb_device *parent = hdev->parent;
  316. struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
  317. if (!parent_hub)
  318. return;
  319. upstream = parent_hub->ports[hdev->portnum - 1];
  320. if (!upstream || !upstream->peer)
  321. return;
  322. peer_hdev = upstream->peer->child;
  323. }
  324. peer_hub = usb_hub_to_struct_hub(peer_hdev);
  325. if (!peer_hub || port1 > peer_hdev->maxchild)
  326. return;
  327. /*
  328. * we found a valid default peer, last check is to make sure it
  329. * does not have location data
  330. */
  331. peer = peer_hub->ports[port1 - 1];
  332. if (peer && peer->location == 0)
  333. link_peers_report(port_dev, peer);
  334. }
  335. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  336. {
  337. struct usb_port *port_dev;
  338. int retval;
  339. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  340. if (!port_dev)
  341. return -ENOMEM;
  342. port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
  343. if (!port_dev->req) {
  344. kfree(port_dev);
  345. return -ENOMEM;
  346. }
  347. hub->ports[port1 - 1] = port_dev;
  348. port_dev->portnum = port1;
  349. set_bit(port1, hub->power_bits);
  350. port_dev->dev.parent = hub->intfdev;
  351. port_dev->dev.groups = port_dev_group;
  352. port_dev->dev.type = &usb_port_device_type;
  353. port_dev->dev.driver = &usb_port_driver;
  354. if (hub_is_superspeed(hub->hdev))
  355. port_dev->is_superspeed = 1;
  356. dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
  357. port1);
  358. mutex_init(&port_dev->status_lock);
  359. retval = device_register(&port_dev->dev);
  360. if (retval) {
  361. put_device(&port_dev->dev);
  362. return retval;
  363. }
  364. /* Set default policy of port-poweroff disabled. */
  365. retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
  366. DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
  367. if (retval < 0) {
  368. device_unregister(&port_dev->dev);
  369. return retval;
  370. }
  371. find_and_link_peer(hub, port1);
  372. /*
  373. * Enable runtime pm and hold a refernce that hub_configure()
  374. * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
  375. * and the hub has been fully registered (hdev->maxchild set).
  376. */
  377. pm_runtime_set_active(&port_dev->dev);
  378. pm_runtime_get_noresume(&port_dev->dev);
  379. pm_runtime_enable(&port_dev->dev);
  380. device_enable_async_suspend(&port_dev->dev);
  381. /*
  382. * Keep hidden the ability to enable port-poweroff if the hub
  383. * does not support power switching.
  384. */
  385. if (!hub_is_port_power_switchable(hub))
  386. return 0;
  387. /* Attempt to let userspace take over the policy. */
  388. retval = dev_pm_qos_expose_flags(&port_dev->dev,
  389. PM_QOS_FLAG_NO_POWER_OFF);
  390. if (retval < 0) {
  391. dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
  392. return 0;
  393. }
  394. /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
  395. retval = dev_pm_qos_remove_request(port_dev->req);
  396. if (retval >= 0) {
  397. kfree(port_dev->req);
  398. port_dev->req = NULL;
  399. }
  400. return 0;
  401. }
  402. void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
  403. {
  404. struct usb_port *port_dev = hub->ports[port1 - 1];
  405. struct usb_port *peer;
  406. peer = port_dev->peer;
  407. if (peer)
  408. unlink_peers(port_dev, peer);
  409. device_unregister(&port_dev->dev);
  410. }