port.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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/kstrtox.h>
  10. #include <linux/slab.h>
  11. #include <linux/pm_qos.h>
  12. #include <linux/component.h>
  13. #include <linux/usb/of.h>
  14. #include "hub.h"
  15. static int usb_port_block_power_off;
  16. static const struct attribute_group *port_dev_group[];
  17. static ssize_t early_stop_show(struct device *dev,
  18. struct device_attribute *attr, char *buf)
  19. {
  20. struct usb_port *port_dev = to_usb_port(dev);
  21. return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no");
  22. }
  23. static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
  24. const char *buf, size_t count)
  25. {
  26. struct usb_port *port_dev = to_usb_port(dev);
  27. bool value;
  28. if (kstrtobool(buf, &value))
  29. return -EINVAL;
  30. if (value)
  31. port_dev->early_stop = 1;
  32. else
  33. port_dev->early_stop = 0;
  34. return count;
  35. }
  36. static DEVICE_ATTR_RW(early_stop);
  37. static ssize_t disable_show(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct usb_port *port_dev = to_usb_port(dev);
  41. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  42. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  43. struct usb_interface *intf = to_usb_interface(dev->parent);
  44. int port1 = port_dev->portnum;
  45. u16 portstatus, unused;
  46. bool disabled;
  47. int rc;
  48. struct kernfs_node *kn;
  49. if (!hub)
  50. return -ENODEV;
  51. hub_get(hub);
  52. rc = usb_autopm_get_interface(intf);
  53. if (rc < 0)
  54. goto out_hub_get;
  55. /*
  56. * Prevent deadlock if another process is concurrently
  57. * trying to unregister hdev.
  58. */
  59. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  60. if (!kn) {
  61. rc = -ENODEV;
  62. goto out_autopm;
  63. }
  64. usb_lock_device(hdev);
  65. if (hub->disconnected) {
  66. rc = -ENODEV;
  67. goto out_hdev_lock;
  68. }
  69. usb_hub_port_status(hub, port1, &portstatus, &unused);
  70. disabled = !usb_port_is_power_on(hub, portstatus);
  71. out_hdev_lock:
  72. usb_unlock_device(hdev);
  73. sysfs_unbreak_active_protection(kn);
  74. out_autopm:
  75. usb_autopm_put_interface(intf);
  76. out_hub_get:
  77. hub_put(hub);
  78. if (rc)
  79. return rc;
  80. return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
  81. }
  82. static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
  83. const char *buf, size_t count)
  84. {
  85. struct usb_port *port_dev = to_usb_port(dev);
  86. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  87. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  88. struct usb_interface *intf = to_usb_interface(dev->parent);
  89. int port1 = port_dev->portnum;
  90. bool disabled;
  91. int rc;
  92. struct kernfs_node *kn;
  93. if (!hub)
  94. return -ENODEV;
  95. rc = kstrtobool(buf, &disabled);
  96. if (rc)
  97. return rc;
  98. hub_get(hub);
  99. rc = usb_autopm_get_interface(intf);
  100. if (rc < 0)
  101. goto out_hub_get;
  102. /*
  103. * Prevent deadlock if another process is concurrently
  104. * trying to unregister hdev.
  105. */
  106. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  107. if (!kn) {
  108. rc = -ENODEV;
  109. goto out_autopm;
  110. }
  111. usb_lock_device(hdev);
  112. if (hub->disconnected) {
  113. rc = -ENODEV;
  114. goto out_hdev_lock;
  115. }
  116. if (disabled && port_dev->child)
  117. usb_disconnect(&port_dev->child);
  118. rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
  119. if (disabled) {
  120. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  121. if (!port_dev->is_superspeed)
  122. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  123. }
  124. if (!rc)
  125. rc = count;
  126. out_hdev_lock:
  127. usb_unlock_device(hdev);
  128. sysfs_unbreak_active_protection(kn);
  129. out_autopm:
  130. usb_autopm_put_interface(intf);
  131. out_hub_get:
  132. hub_put(hub);
  133. return rc;
  134. }
  135. static DEVICE_ATTR_RW(disable);
  136. static ssize_t location_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct usb_port *port_dev = to_usb_port(dev);
  140. return sprintf(buf, "0x%08x\n", port_dev->location);
  141. }
  142. static DEVICE_ATTR_RO(location);
  143. static ssize_t connect_type_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct usb_port *port_dev = to_usb_port(dev);
  147. char *result;
  148. switch (port_dev->connect_type) {
  149. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  150. result = "hotplug";
  151. break;
  152. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  153. result = "hardwired";
  154. break;
  155. case USB_PORT_NOT_USED:
  156. result = "not used";
  157. break;
  158. default:
  159. result = "unknown";
  160. break;
  161. }
  162. return sprintf(buf, "%s\n", result);
  163. }
  164. static DEVICE_ATTR_RO(connect_type);
  165. static ssize_t state_show(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct usb_port *port_dev = to_usb_port(dev);
  169. enum usb_device_state state = READ_ONCE(port_dev->state);
  170. return sysfs_emit(buf, "%s\n", usb_state_string(state));
  171. }
  172. static DEVICE_ATTR_RO(state);
  173. static ssize_t over_current_count_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. struct usb_port *port_dev = to_usb_port(dev);
  177. return sprintf(buf, "%u\n", port_dev->over_current_count);
  178. }
  179. static DEVICE_ATTR_RO(over_current_count);
  180. static ssize_t quirks_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. struct usb_port *port_dev = to_usb_port(dev);
  184. return sprintf(buf, "%08x\n", port_dev->quirks);
  185. }
  186. static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
  187. const char *buf, size_t count)
  188. {
  189. struct usb_port *port_dev = to_usb_port(dev);
  190. u32 value;
  191. if (kstrtou32(buf, 16, &value))
  192. return -EINVAL;
  193. port_dev->quirks = value;
  194. return count;
  195. }
  196. static DEVICE_ATTR_RW(quirks);
  197. static ssize_t usb3_lpm_permit_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct usb_port *port_dev = to_usb_port(dev);
  201. const char *p;
  202. if (port_dev->usb3_lpm_u1_permit) {
  203. if (port_dev->usb3_lpm_u2_permit)
  204. p = "u1_u2";
  205. else
  206. p = "u1";
  207. } else {
  208. if (port_dev->usb3_lpm_u2_permit)
  209. p = "u2";
  210. else
  211. p = "0";
  212. }
  213. return sprintf(buf, "%s\n", p);
  214. }
  215. static ssize_t usb3_lpm_permit_store(struct device *dev,
  216. struct device_attribute *attr,
  217. const char *buf, size_t count)
  218. {
  219. struct usb_port *port_dev = to_usb_port(dev);
  220. struct usb_device *udev = port_dev->child;
  221. struct usb_hcd *hcd;
  222. if (!strncmp(buf, "u1_u2", 5)) {
  223. port_dev->usb3_lpm_u1_permit = 1;
  224. port_dev->usb3_lpm_u2_permit = 1;
  225. } else if (!strncmp(buf, "u1", 2)) {
  226. port_dev->usb3_lpm_u1_permit = 1;
  227. port_dev->usb3_lpm_u2_permit = 0;
  228. } else if (!strncmp(buf, "u2", 2)) {
  229. port_dev->usb3_lpm_u1_permit = 0;
  230. port_dev->usb3_lpm_u2_permit = 1;
  231. } else if (!strncmp(buf, "0", 1)) {
  232. port_dev->usb3_lpm_u1_permit = 0;
  233. port_dev->usb3_lpm_u2_permit = 0;
  234. } else
  235. return -EINVAL;
  236. /* If device is connected to the port, disable or enable lpm
  237. * to make new u1 u2 setting take effect immediately.
  238. */
  239. if (udev) {
  240. hcd = bus_to_hcd(udev->bus);
  241. if (!hcd)
  242. return -EINVAL;
  243. usb_lock_device(udev);
  244. mutex_lock(hcd->bandwidth_mutex);
  245. if (!usb_disable_lpm(udev))
  246. usb_enable_lpm(udev);
  247. mutex_unlock(hcd->bandwidth_mutex);
  248. usb_unlock_device(udev);
  249. }
  250. return count;
  251. }
  252. static DEVICE_ATTR_RW(usb3_lpm_permit);
  253. static struct attribute *port_dev_attrs[] = {
  254. &dev_attr_connect_type.attr,
  255. &dev_attr_state.attr,
  256. &dev_attr_location.attr,
  257. &dev_attr_quirks.attr,
  258. &dev_attr_over_current_count.attr,
  259. &dev_attr_disable.attr,
  260. &dev_attr_early_stop.attr,
  261. NULL,
  262. };
  263. static const struct attribute_group port_dev_attr_grp = {
  264. .attrs = port_dev_attrs,
  265. };
  266. static const struct attribute_group *port_dev_group[] = {
  267. &port_dev_attr_grp,
  268. NULL,
  269. };
  270. static struct attribute *port_dev_usb3_attrs[] = {
  271. &dev_attr_usb3_lpm_permit.attr,
  272. NULL,
  273. };
  274. static const struct attribute_group port_dev_usb3_attr_grp = {
  275. .attrs = port_dev_usb3_attrs,
  276. };
  277. static const struct attribute_group *port_dev_usb3_group[] = {
  278. &port_dev_attr_grp,
  279. &port_dev_usb3_attr_grp,
  280. NULL,
  281. };
  282. static void usb_port_device_release(struct device *dev)
  283. {
  284. struct usb_port *port_dev = to_usb_port(dev);
  285. kfree(port_dev->req);
  286. kfree(port_dev);
  287. }
  288. #ifdef CONFIG_PM
  289. static int usb_port_runtime_resume(struct device *dev)
  290. {
  291. struct usb_port *port_dev = to_usb_port(dev);
  292. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  293. struct usb_interface *intf = to_usb_interface(dev->parent);
  294. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  295. struct usb_device *udev = port_dev->child;
  296. struct usb_port *peer = port_dev->peer;
  297. int port1 = port_dev->portnum;
  298. int retval;
  299. if (!hub)
  300. return -EINVAL;
  301. if (hub->in_reset) {
  302. set_bit(port1, hub->power_bits);
  303. return 0;
  304. }
  305. /*
  306. * Power on our usb3 peer before this usb2 port to prevent a usb3
  307. * device from degrading to its usb2 connection
  308. */
  309. if (!port_dev->is_superspeed && peer)
  310. pm_runtime_get_sync(&peer->dev);
  311. retval = usb_autopm_get_interface(intf);
  312. if (retval < 0)
  313. return retval;
  314. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  315. msleep(hub_power_on_good_delay(hub));
  316. if (udev && !retval) {
  317. /*
  318. * Our preference is to simply wait for the port to reconnect,
  319. * as that is the lowest latency method to restart the port.
  320. * However, there are cases where toggling port power results in
  321. * the host port and the device port getting out of sync causing
  322. * a link training live lock. Upon timeout, flag the port as
  323. * needing warm reset recovery (to be performed later by
  324. * usb_port_resume() as requested via usb_wakeup_notification())
  325. */
  326. if (hub_port_debounce_be_connected(hub, port1) < 0) {
  327. dev_dbg(&port_dev->dev, "reconnect timeout\n");
  328. if (hub_is_superspeed(hdev))
  329. set_bit(port1, hub->warm_reset_bits);
  330. }
  331. /* Force the child awake to revalidate after the power loss. */
  332. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  333. pm_runtime_get_noresume(&port_dev->dev);
  334. pm_request_resume(&udev->dev);
  335. }
  336. }
  337. usb_autopm_put_interface(intf);
  338. return retval;
  339. }
  340. static int usb_port_runtime_suspend(struct device *dev)
  341. {
  342. struct usb_port *port_dev = to_usb_port(dev);
  343. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  344. struct usb_interface *intf = to_usb_interface(dev->parent);
  345. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  346. struct usb_port *peer = port_dev->peer;
  347. int port1 = port_dev->portnum;
  348. int retval;
  349. if (!hub)
  350. return -EINVAL;
  351. if (hub->in_reset)
  352. return -EBUSY;
  353. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  354. == PM_QOS_FLAGS_ALL)
  355. return -EAGAIN;
  356. if (usb_port_block_power_off)
  357. return -EBUSY;
  358. retval = usb_autopm_get_interface(intf);
  359. if (retval < 0)
  360. return retval;
  361. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  362. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  363. if (!port_dev->is_superspeed)
  364. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  365. usb_autopm_put_interface(intf);
  366. /*
  367. * Our peer usb3 port may now be able to suspend, so
  368. * asynchronously queue a suspend request to observe that this
  369. * usb2 port is now off.
  370. */
  371. if (!port_dev->is_superspeed && peer)
  372. pm_runtime_put(&peer->dev);
  373. return retval;
  374. }
  375. #endif
  376. static void usb_port_shutdown(struct device *dev)
  377. {
  378. struct usb_port *port_dev = to_usb_port(dev);
  379. struct usb_device *udev = port_dev->child;
  380. if (udev && !udev->port_is_suspended) {
  381. usb_disable_usb2_hardware_lpm(udev);
  382. usb_unlocked_disable_lpm(udev);
  383. }
  384. }
  385. static const struct dev_pm_ops usb_port_pm_ops = {
  386. #ifdef CONFIG_PM
  387. .runtime_suspend = usb_port_runtime_suspend,
  388. .runtime_resume = usb_port_runtime_resume,
  389. #endif
  390. };
  391. const struct device_type usb_port_device_type = {
  392. .name = "usb_port",
  393. .release = usb_port_device_release,
  394. .pm = &usb_port_pm_ops,
  395. };
  396. static struct device_driver usb_port_driver = {
  397. .name = "usb",
  398. .owner = THIS_MODULE,
  399. .shutdown = usb_port_shutdown,
  400. };
  401. static int link_peers(struct usb_port *left, struct usb_port *right)
  402. {
  403. struct usb_port *ss_port, *hs_port;
  404. int rc;
  405. if (left->peer == right && right->peer == left)
  406. return 0;
  407. if (left->peer || right->peer) {
  408. struct usb_port *lpeer = left->peer;
  409. struct usb_port *rpeer = right->peer;
  410. char *method;
  411. if (left->location && left->location == right->location)
  412. method = "location";
  413. else
  414. method = "default";
  415. pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
  416. dev_name(&left->dev), dev_name(&right->dev), method,
  417. dev_name(&left->dev),
  418. lpeer ? dev_name(&lpeer->dev) : "none",
  419. dev_name(&right->dev),
  420. rpeer ? dev_name(&rpeer->dev) : "none");
  421. return -EBUSY;
  422. }
  423. rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
  424. if (rc)
  425. return rc;
  426. rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
  427. if (rc) {
  428. sysfs_remove_link(&left->dev.kobj, "peer");
  429. return rc;
  430. }
  431. /*
  432. * We need to wake the HiSpeed port to make sure we don't race
  433. * setting ->peer with usb_port_runtime_suspend(). Otherwise we
  434. * may miss a suspend event for the SuperSpeed port.
  435. */
  436. if (left->is_superspeed) {
  437. ss_port = left;
  438. WARN_ON(right->is_superspeed);
  439. hs_port = right;
  440. } else {
  441. ss_port = right;
  442. WARN_ON(!right->is_superspeed);
  443. hs_port = left;
  444. }
  445. pm_runtime_get_sync(&hs_port->dev);
  446. left->peer = right;
  447. right->peer = left;
  448. /*
  449. * The SuperSpeed reference is dropped when the HiSpeed port in
  450. * this relationship suspends, i.e. when it is safe to allow a
  451. * SuperSpeed connection to drop since there is no risk of a
  452. * device degrading to its powered-off HiSpeed connection.
  453. *
  454. * Also, drop the HiSpeed ref taken above.
  455. */
  456. pm_runtime_get_sync(&ss_port->dev);
  457. pm_runtime_put(&hs_port->dev);
  458. return 0;
  459. }
  460. static void link_peers_report(struct usb_port *left, struct usb_port *right)
  461. {
  462. int rc;
  463. rc = link_peers(left, right);
  464. if (rc == 0) {
  465. dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
  466. } else {
  467. dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
  468. dev_name(&right->dev), rc);
  469. pr_warn_once("usb: port power management may be unreliable\n");
  470. usb_port_block_power_off = 1;
  471. }
  472. }
  473. static void unlink_peers(struct usb_port *left, struct usb_port *right)
  474. {
  475. struct usb_port *ss_port, *hs_port;
  476. WARN(right->peer != left || left->peer != right,
  477. "%s and %s are not peers?\n",
  478. dev_name(&left->dev), dev_name(&right->dev));
  479. /*
  480. * We wake the HiSpeed port to make sure we don't race its
  481. * usb_port_runtime_resume() event which takes a SuperSpeed ref
  482. * when ->peer is !NULL.
  483. */
  484. if (left->is_superspeed) {
  485. ss_port = left;
  486. hs_port = right;
  487. } else {
  488. ss_port = right;
  489. hs_port = left;
  490. }
  491. pm_runtime_get_sync(&hs_port->dev);
  492. sysfs_remove_link(&left->dev.kobj, "peer");
  493. right->peer = NULL;
  494. sysfs_remove_link(&right->dev.kobj, "peer");
  495. left->peer = NULL;
  496. /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
  497. pm_runtime_put(&ss_port->dev);
  498. /* Drop the ref taken above */
  499. pm_runtime_put(&hs_port->dev);
  500. }
  501. /*
  502. * For each usb hub device in the system check to see if it is in the
  503. * peer domain of the given port_dev, and if it is check to see if it
  504. * has a port that matches the given port by location
  505. */
  506. static int match_location(struct usb_device *peer_hdev, void *p)
  507. {
  508. int port1;
  509. struct usb_hcd *hcd, *peer_hcd;
  510. struct usb_port *port_dev = p, *peer;
  511. struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
  512. struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
  513. if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
  514. return 0;
  515. hcd = bus_to_hcd(hdev->bus);
  516. peer_hcd = bus_to_hcd(peer_hdev->bus);
  517. /* peer_hcd is provisional until we verify it against the known peer */
  518. if (peer_hcd != hcd->shared_hcd)
  519. return 0;
  520. for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
  521. peer = peer_hub->ports[port1 - 1];
  522. if (peer && peer->connect_type != USB_PORT_NOT_USED &&
  523. peer->location == port_dev->location) {
  524. link_peers_report(port_dev, peer);
  525. return 1; /* done */
  526. }
  527. }
  528. return 0;
  529. }
  530. /*
  531. * Find the peer port either via explicit platform firmware "location"
  532. * data, the peer hcd for root hubs, or the upstream peer relationship
  533. * for all other hubs.
  534. */
  535. static void find_and_link_peer(struct usb_hub *hub, int port1)
  536. {
  537. struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
  538. struct usb_device *hdev = hub->hdev;
  539. struct usb_device *peer_hdev;
  540. struct usb_hub *peer_hub;
  541. /*
  542. * If location data is available then we can only peer this port
  543. * by a location match, not the default peer (lest we create a
  544. * situation where we need to go back and undo a default peering
  545. * when the port is later peered by location data)
  546. */
  547. if (port_dev->location) {
  548. /* we link the peer in match_location() if found */
  549. usb_for_each_dev(port_dev, match_location);
  550. return;
  551. } else if (!hdev->parent) {
  552. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  553. struct usb_hcd *peer_hcd = hcd->shared_hcd;
  554. if (!peer_hcd)
  555. return;
  556. peer_hdev = peer_hcd->self.root_hub;
  557. } else {
  558. struct usb_port *upstream;
  559. struct usb_device *parent = hdev->parent;
  560. struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
  561. if (!parent_hub)
  562. return;
  563. upstream = parent_hub->ports[hdev->portnum - 1];
  564. if (!upstream || !upstream->peer)
  565. return;
  566. peer_hdev = upstream->peer->child;
  567. }
  568. peer_hub = usb_hub_to_struct_hub(peer_hdev);
  569. if (!peer_hub || port1 > peer_hdev->maxchild)
  570. return;
  571. /*
  572. * we found a valid default peer, last check is to make sure it
  573. * does not have location data
  574. */
  575. peer = peer_hub->ports[port1 - 1];
  576. if (peer && peer->location == 0)
  577. link_peers_report(port_dev, peer);
  578. }
  579. static int connector_bind(struct device *dev, struct device *connector, void *data)
  580. {
  581. struct usb_port *port_dev = to_usb_port(dev);
  582. int ret;
  583. ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
  584. if (ret)
  585. return ret;
  586. ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
  587. if (ret) {
  588. sysfs_remove_link(&dev->kobj, "connector");
  589. return ret;
  590. }
  591. port_dev->connector = data;
  592. /*
  593. * If there is already USB device connected to the port, letting the
  594. * Type-C connector know about it immediately.
  595. */
  596. if (port_dev->child)
  597. typec_attach(port_dev->connector, &port_dev->child->dev);
  598. return 0;
  599. }
  600. static void connector_unbind(struct device *dev, struct device *connector, void *data)
  601. {
  602. struct usb_port *port_dev = to_usb_port(dev);
  603. sysfs_remove_link(&connector->kobj, dev_name(dev));
  604. sysfs_remove_link(&dev->kobj, "connector");
  605. port_dev->connector = NULL;
  606. }
  607. static const struct component_ops connector_ops = {
  608. .bind = connector_bind,
  609. .unbind = connector_unbind,
  610. };
  611. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  612. {
  613. struct usb_port *port_dev;
  614. struct usb_device *hdev = hub->hdev;
  615. int retval;
  616. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  617. if (!port_dev)
  618. return -ENOMEM;
  619. port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
  620. if (!port_dev->req) {
  621. kfree(port_dev);
  622. return -ENOMEM;
  623. }
  624. port_dev->connect_type = usb_of_get_connect_type(hdev, port1);
  625. hub->ports[port1 - 1] = port_dev;
  626. port_dev->portnum = port1;
  627. set_bit(port1, hub->power_bits);
  628. port_dev->dev.parent = hub->intfdev;
  629. if (hub_is_superspeed(hdev)) {
  630. port_dev->is_superspeed = 1;
  631. port_dev->usb3_lpm_u1_permit = 1;
  632. port_dev->usb3_lpm_u2_permit = 1;
  633. port_dev->dev.groups = port_dev_usb3_group;
  634. } else
  635. port_dev->dev.groups = port_dev_group;
  636. port_dev->dev.type = &usb_port_device_type;
  637. port_dev->dev.driver = &usb_port_driver;
  638. dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
  639. port1);
  640. mutex_init(&port_dev->status_lock);
  641. retval = device_register(&port_dev->dev);
  642. if (retval) {
  643. put_device(&port_dev->dev);
  644. return retval;
  645. }
  646. port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
  647. if (!port_dev->state_kn) {
  648. dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
  649. retval = -ENODEV;
  650. goto err_unregister;
  651. }
  652. /* Set default policy of port-poweroff disabled. */
  653. retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
  654. DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
  655. if (retval < 0) {
  656. goto err_put_kn;
  657. }
  658. retval = component_add(&port_dev->dev, &connector_ops);
  659. if (retval) {
  660. dev_warn(&port_dev->dev, "failed to add component\n");
  661. goto err_put_kn;
  662. }
  663. find_and_link_peer(hub, port1);
  664. /*
  665. * Enable runtime pm and hold a refernce that hub_configure()
  666. * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
  667. * and the hub has been fully registered (hdev->maxchild set).
  668. */
  669. pm_runtime_set_active(&port_dev->dev);
  670. pm_runtime_get_noresume(&port_dev->dev);
  671. pm_runtime_enable(&port_dev->dev);
  672. device_enable_async_suspend(&port_dev->dev);
  673. /*
  674. * Keep hidden the ability to enable port-poweroff if the hub
  675. * does not support power switching.
  676. */
  677. if (!hub_is_port_power_switchable(hub))
  678. return 0;
  679. /* Attempt to let userspace take over the policy. */
  680. retval = dev_pm_qos_expose_flags(&port_dev->dev,
  681. PM_QOS_FLAG_NO_POWER_OFF);
  682. if (retval < 0) {
  683. dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
  684. return 0;
  685. }
  686. /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
  687. retval = dev_pm_qos_remove_request(port_dev->req);
  688. if (retval >= 0) {
  689. kfree(port_dev->req);
  690. port_dev->req = NULL;
  691. }
  692. return 0;
  693. err_put_kn:
  694. sysfs_put(port_dev->state_kn);
  695. err_unregister:
  696. device_unregister(&port_dev->dev);
  697. return retval;
  698. }
  699. void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
  700. {
  701. struct usb_port *port_dev = hub->ports[port1 - 1];
  702. struct usb_port *peer;
  703. peer = port_dev->peer;
  704. if (peer)
  705. unlink_peers(port_dev, peer);
  706. component_del(&port_dev->dev, &connector_ops);
  707. sysfs_put(port_dev->state_kn);
  708. device_unregister(&port_dev->dev);
  709. }