port.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 const struct attribute_group *port_dev_group[];
  22. static ssize_t connect_type_show(struct device *dev,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct usb_port *port_dev = to_usb_port(dev);
  26. char *result;
  27. switch (port_dev->connect_type) {
  28. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  29. result = "hotplug";
  30. break;
  31. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  32. result = "hardwired";
  33. break;
  34. case USB_PORT_NOT_USED:
  35. result = "not used";
  36. break;
  37. default:
  38. result = "unknown";
  39. break;
  40. }
  41. return sprintf(buf, "%s\n", result);
  42. }
  43. static DEVICE_ATTR_RO(connect_type);
  44. static struct attribute *port_dev_attrs[] = {
  45. &dev_attr_connect_type.attr,
  46. NULL,
  47. };
  48. static struct attribute_group port_dev_attr_grp = {
  49. .attrs = port_dev_attrs,
  50. };
  51. static const struct attribute_group *port_dev_group[] = {
  52. &port_dev_attr_grp,
  53. NULL,
  54. };
  55. static void usb_port_device_release(struct device *dev)
  56. {
  57. struct usb_port *port_dev = to_usb_port(dev);
  58. kfree(port_dev);
  59. }
  60. #ifdef CONFIG_PM_RUNTIME
  61. static int usb_port_runtime_resume(struct device *dev)
  62. {
  63. struct usb_port *port_dev = to_usb_port(dev);
  64. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  65. struct usb_interface *intf = to_usb_interface(dev->parent);
  66. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  67. int port1 = port_dev->portnum;
  68. int retval;
  69. if (!hub)
  70. return -EINVAL;
  71. if (hub->in_reset) {
  72. set_bit(port1, hub->power_bits);
  73. return 0;
  74. }
  75. usb_autopm_get_interface(intf);
  76. set_bit(port1, hub->busy_bits);
  77. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  78. if (port_dev->child && !retval) {
  79. /*
  80. * Attempt to wait for usb hub port to be reconnected in order
  81. * to make the resume procedure successful. The device may have
  82. * disconnected while the port was powered off, so ignore the
  83. * return status.
  84. */
  85. retval = hub_port_debounce_be_connected(hub, port1);
  86. if (retval < 0)
  87. dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
  88. retval);
  89. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  90. retval = 0;
  91. }
  92. clear_bit(port1, hub->busy_bits);
  93. usb_autopm_put_interface(intf);
  94. return retval;
  95. }
  96. static int usb_port_runtime_suspend(struct device *dev)
  97. {
  98. struct usb_port *port_dev = to_usb_port(dev);
  99. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  100. struct usb_interface *intf = to_usb_interface(dev->parent);
  101. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  102. int port1 = port_dev->portnum;
  103. int retval;
  104. if (!hub)
  105. return -EINVAL;
  106. if (hub->in_reset)
  107. return -EBUSY;
  108. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  109. == PM_QOS_FLAGS_ALL)
  110. return -EAGAIN;
  111. usb_autopm_get_interface(intf);
  112. set_bit(port1, hub->busy_bits);
  113. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  114. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  115. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  116. clear_bit(port1, hub->busy_bits);
  117. usb_autopm_put_interface(intf);
  118. return retval;
  119. }
  120. #endif
  121. static const struct dev_pm_ops usb_port_pm_ops = {
  122. #ifdef CONFIG_PM_RUNTIME
  123. .runtime_suspend = usb_port_runtime_suspend,
  124. .runtime_resume = usb_port_runtime_resume,
  125. #endif
  126. };
  127. struct device_type usb_port_device_type = {
  128. .name = "usb_port",
  129. .release = usb_port_device_release,
  130. .pm = &usb_port_pm_ops,
  131. };
  132. static struct device_driver usb_port_driver = {
  133. .name = "usb",
  134. .owner = THIS_MODULE,
  135. };
  136. static int link_peers(struct usb_port *left, struct usb_port *right)
  137. {
  138. int rc;
  139. if (left->peer == right && right->peer == left)
  140. return 0;
  141. if (left->peer || right->peer) {
  142. struct usb_port *lpeer = left->peer;
  143. struct usb_port *rpeer = right->peer;
  144. WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
  145. dev_name(&left->dev), dev_name(&right->dev),
  146. dev_name(&left->dev), lpeer,
  147. dev_name(&right->dev), rpeer);
  148. return -EBUSY;
  149. }
  150. rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
  151. if (rc)
  152. return rc;
  153. rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
  154. if (rc) {
  155. sysfs_remove_link(&left->dev.kobj, "peer");
  156. return rc;
  157. }
  158. left->peer = right;
  159. right->peer = left;
  160. return 0;
  161. }
  162. static void link_peers_report(struct usb_port *left, struct usb_port *right)
  163. {
  164. int rc;
  165. rc = link_peers(left, right);
  166. if (rc == 0) {
  167. dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
  168. } else {
  169. dev_warn(&left->dev, "failed to peer to %s (%d)\n",
  170. dev_name(&right->dev), rc);
  171. pr_warn_once("usb: port power management may be unreliable\n");
  172. }
  173. }
  174. static void unlink_peers(struct usb_port *left, struct usb_port *right)
  175. {
  176. WARN(right->peer != left || left->peer != right,
  177. "%s and %s are not peers?\n",
  178. dev_name(&left->dev), dev_name(&right->dev));
  179. sysfs_remove_link(&left->dev.kobj, "peer");
  180. right->peer = NULL;
  181. sysfs_remove_link(&right->dev.kobj, "peer");
  182. left->peer = NULL;
  183. }
  184. /*
  185. * For each usb hub device in the system check to see if it is in the
  186. * peer domain of the given port_dev, and if it is check to see if it
  187. * has a port that matches the given port by location
  188. */
  189. static int match_location(struct usb_device *peer_hdev, void *p)
  190. {
  191. int port1;
  192. struct usb_hcd *hcd, *peer_hcd;
  193. struct usb_port *port_dev = p, *peer;
  194. struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
  195. struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
  196. if (!peer_hub)
  197. return 0;
  198. hcd = bus_to_hcd(hdev->bus);
  199. peer_hcd = bus_to_hcd(peer_hdev->bus);
  200. /* peer_hcd is provisional until we verify it against the known peer */
  201. if (peer_hcd != hcd->shared_hcd)
  202. return 0;
  203. for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
  204. peer = peer_hub->ports[port1 - 1];
  205. if (peer && peer->location == port_dev->location) {
  206. link_peers_report(port_dev, peer);
  207. return 1; /* done */
  208. }
  209. }
  210. return 0;
  211. }
  212. /*
  213. * Find the peer port either via explicit platform firmware "location"
  214. * data, the peer hcd for root hubs, or the upstream peer relationship
  215. * for all other hubs.
  216. */
  217. static void find_and_link_peer(struct usb_hub *hub, int port1)
  218. {
  219. struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
  220. struct usb_device *hdev = hub->hdev;
  221. struct usb_device *peer_hdev;
  222. struct usb_hub *peer_hub;
  223. /*
  224. * If location data is available then we can only peer this port
  225. * by a location match, not the default peer (lest we create a
  226. * situation where we need to go back and undo a default peering
  227. * when the port is later peered by location data)
  228. */
  229. if (port_dev->location) {
  230. /* we link the peer in match_location() if found */
  231. usb_for_each_dev(port_dev, match_location);
  232. return;
  233. } else if (!hdev->parent) {
  234. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  235. struct usb_hcd *peer_hcd = hcd->shared_hcd;
  236. if (!peer_hcd)
  237. return;
  238. peer_hdev = peer_hcd->self.root_hub;
  239. } else {
  240. struct usb_port *upstream;
  241. struct usb_device *parent = hdev->parent;
  242. struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
  243. if (!parent_hub)
  244. return;
  245. upstream = parent_hub->ports[hdev->portnum - 1];
  246. if (!upstream || !upstream->peer)
  247. return;
  248. peer_hdev = upstream->peer->child;
  249. }
  250. peer_hub = usb_hub_to_struct_hub(peer_hdev);
  251. if (!peer_hub || port1 > peer_hdev->maxchild)
  252. return;
  253. /*
  254. * we found a valid default peer, last check is to make sure it
  255. * does not have location data
  256. */
  257. peer = peer_hub->ports[port1 - 1];
  258. if (peer && peer->location == 0)
  259. link_peers_report(port_dev, peer);
  260. }
  261. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  262. {
  263. struct usb_port *port_dev;
  264. int retval;
  265. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  266. if (!port_dev) {
  267. retval = -ENOMEM;
  268. goto exit;
  269. }
  270. hub->ports[port1 - 1] = port_dev;
  271. port_dev->portnum = port1;
  272. set_bit(port1, hub->power_bits);
  273. port_dev->dev.parent = hub->intfdev;
  274. port_dev->dev.groups = port_dev_group;
  275. port_dev->dev.type = &usb_port_device_type;
  276. port_dev->dev.driver = &usb_port_driver;
  277. dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
  278. port1);
  279. retval = device_register(&port_dev->dev);
  280. if (retval)
  281. goto error_register;
  282. find_and_link_peer(hub, port1);
  283. pm_runtime_set_active(&port_dev->dev);
  284. /*
  285. * Do not enable port runtime pm if the hub does not support
  286. * power switching. Also, userspace must have final say of
  287. * whether a port is permitted to power-off. Do not enable
  288. * runtime pm if we fail to expose pm_qos_no_power_off.
  289. */
  290. if (hub_is_port_power_switchable(hub)
  291. && dev_pm_qos_expose_flags(&port_dev->dev,
  292. PM_QOS_FLAG_NO_POWER_OFF) == 0)
  293. pm_runtime_enable(&port_dev->dev);
  294. device_enable_async_suspend(&port_dev->dev);
  295. return 0;
  296. error_register:
  297. put_device(&port_dev->dev);
  298. exit:
  299. return retval;
  300. }
  301. void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
  302. {
  303. struct usb_port *port_dev = hub->ports[port1 - 1];
  304. struct usb_port *peer;
  305. peer = port_dev->peer;
  306. if (peer)
  307. unlink_peers(port_dev, peer);
  308. device_unregister(&port_dev->dev);
  309. }