usb4_port.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB4 port device
  4. *
  5. * Copyright (C) 2021, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #include <linux/pm_runtime.h>
  9. #include <linux/component.h>
  10. #include <linux/property.h>
  11. #include "tb.h"
  12. static int connector_bind(struct device *dev, struct device *connector, void *data)
  13. {
  14. int ret;
  15. ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
  16. if (ret)
  17. return ret;
  18. ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
  19. if (ret)
  20. sysfs_remove_link(&dev->kobj, "connector");
  21. return ret;
  22. }
  23. static void connector_unbind(struct device *dev, struct device *connector, void *data)
  24. {
  25. sysfs_remove_link(&connector->kobj, dev_name(dev));
  26. sysfs_remove_link(&dev->kobj, "connector");
  27. }
  28. static const struct component_ops connector_ops = {
  29. .bind = connector_bind,
  30. .unbind = connector_unbind,
  31. };
  32. static ssize_t link_show(struct device *dev, struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  36. struct tb_port *port = usb4->port;
  37. struct tb *tb = port->sw->tb;
  38. const char *link;
  39. if (mutex_lock_interruptible(&tb->lock))
  40. return -ERESTARTSYS;
  41. if (tb_is_upstream_port(port))
  42. link = port->sw->link_usb4 ? "usb4" : "tbt";
  43. else if (tb_port_has_remote(port))
  44. link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
  45. else if (port->xdomain)
  46. link = port->xdomain->link_usb4 ? "usb4" : "tbt";
  47. else
  48. link = "none";
  49. mutex_unlock(&tb->lock);
  50. return sysfs_emit(buf, "%s\n", link);
  51. }
  52. static DEVICE_ATTR_RO(link);
  53. static struct attribute *common_attrs[] = {
  54. &dev_attr_link.attr,
  55. NULL
  56. };
  57. static const struct attribute_group common_group = {
  58. .attrs = common_attrs,
  59. };
  60. static int usb4_port_offline(struct usb4_port *usb4)
  61. {
  62. struct tb_port *port = usb4->port;
  63. int ret;
  64. ret = tb_acpi_power_on_retimers(port);
  65. if (ret)
  66. return ret;
  67. ret = usb4_port_router_offline(port);
  68. if (ret) {
  69. tb_acpi_power_off_retimers(port);
  70. return ret;
  71. }
  72. ret = tb_retimer_scan(port, false);
  73. if (ret) {
  74. usb4_port_router_online(port);
  75. tb_acpi_power_off_retimers(port);
  76. }
  77. return ret;
  78. }
  79. static void usb4_port_online(struct usb4_port *usb4)
  80. {
  81. struct tb_port *port = usb4->port;
  82. usb4_port_router_online(port);
  83. tb_acpi_power_off_retimers(port);
  84. }
  85. /**
  86. * usb4_usb3_port_match() - Matches USB4 port device with USB 3.x port device
  87. * @usb4_port_dev: USB4 port device
  88. * @usb3_port_fwnode: USB 3.x port firmware node
  89. *
  90. * Checks if USB 3.x port @usb3_port_fwnode is tunneled through USB4 port @usb4_port_dev.
  91. * Returns true if match is found, false otherwise.
  92. *
  93. * Function is designed to be used with component framework (component_match_add).
  94. */
  95. bool usb4_usb3_port_match(struct device *usb4_port_dev,
  96. const struct fwnode_handle *usb3_port_fwnode)
  97. {
  98. struct fwnode_handle *nhi_fwnode __free(fwnode_handle) = NULL;
  99. struct usb4_port *usb4;
  100. struct tb_switch *sw;
  101. struct tb_nhi *nhi;
  102. u8 usb4_port_num;
  103. struct tb *tb;
  104. usb4 = tb_to_usb4_port_device(usb4_port_dev);
  105. if (!usb4)
  106. return false;
  107. sw = usb4->port->sw;
  108. tb = sw->tb;
  109. nhi = tb->nhi;
  110. nhi_fwnode = fwnode_find_reference(usb3_port_fwnode, "usb4-host-interface", 0);
  111. if (IS_ERR(nhi_fwnode))
  112. return false;
  113. /* Check if USB3 fwnode references same NHI where USB4 port resides */
  114. if (!device_match_fwnode(&nhi->pdev->dev, nhi_fwnode))
  115. return false;
  116. if (fwnode_property_read_u8(usb3_port_fwnode, "usb4-port-number", &usb4_port_num))
  117. return false;
  118. return usb4_port_index(sw, usb4->port) == usb4_port_num;
  119. }
  120. EXPORT_SYMBOL_GPL(usb4_usb3_port_match);
  121. static ssize_t offline_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  125. return sysfs_emit(buf, "%d\n", usb4->offline);
  126. }
  127. static ssize_t offline_store(struct device *dev,
  128. struct device_attribute *attr, const char *buf, size_t count)
  129. {
  130. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  131. struct tb_port *port = usb4->port;
  132. struct tb *tb = port->sw->tb;
  133. bool val;
  134. int ret;
  135. ret = kstrtobool(buf, &val);
  136. if (ret)
  137. return ret;
  138. pm_runtime_get_sync(&usb4->dev);
  139. if (mutex_lock_interruptible(&tb->lock)) {
  140. ret = -ERESTARTSYS;
  141. goto out_rpm;
  142. }
  143. if (val == usb4->offline)
  144. goto out_unlock;
  145. /* Offline mode works only for ports that are not connected */
  146. if (tb_port_has_remote(port)) {
  147. ret = -EBUSY;
  148. goto out_unlock;
  149. }
  150. if (val) {
  151. ret = usb4_port_offline(usb4);
  152. if (ret)
  153. goto out_unlock;
  154. } else {
  155. usb4_port_online(usb4);
  156. tb_retimer_remove_all(port);
  157. }
  158. usb4->offline = val;
  159. tb_port_dbg(port, "%s offline mode\n", val ? "enter" : "exit");
  160. out_unlock:
  161. mutex_unlock(&tb->lock);
  162. out_rpm:
  163. pm_runtime_mark_last_busy(&usb4->dev);
  164. pm_runtime_put_autosuspend(&usb4->dev);
  165. return ret ? ret : count;
  166. }
  167. static DEVICE_ATTR_RW(offline);
  168. static ssize_t rescan_store(struct device *dev,
  169. struct device_attribute *attr, const char *buf, size_t count)
  170. {
  171. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  172. struct tb_port *port = usb4->port;
  173. struct tb *tb = port->sw->tb;
  174. bool val;
  175. int ret;
  176. ret = kstrtobool(buf, &val);
  177. if (ret)
  178. return ret;
  179. if (!val)
  180. return count;
  181. pm_runtime_get_sync(&usb4->dev);
  182. if (mutex_lock_interruptible(&tb->lock)) {
  183. ret = -ERESTARTSYS;
  184. goto out_rpm;
  185. }
  186. /* Must be in offline mode already */
  187. if (!usb4->offline) {
  188. ret = -EINVAL;
  189. goto out_unlock;
  190. }
  191. tb_retimer_remove_all(port);
  192. ret = tb_retimer_scan(port, true);
  193. out_unlock:
  194. mutex_unlock(&tb->lock);
  195. out_rpm:
  196. pm_runtime_mark_last_busy(&usb4->dev);
  197. pm_runtime_put_autosuspend(&usb4->dev);
  198. return ret ? ret : count;
  199. }
  200. static DEVICE_ATTR_WO(rescan);
  201. static struct attribute *service_attrs[] = {
  202. &dev_attr_offline.attr,
  203. &dev_attr_rescan.attr,
  204. NULL
  205. };
  206. static umode_t service_attr_is_visible(struct kobject *kobj,
  207. struct attribute *attr, int n)
  208. {
  209. struct device *dev = kobj_to_dev(kobj);
  210. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  211. /*
  212. * Always need some platform help to cycle the modes so that
  213. * retimers can be accessed through the sideband.
  214. */
  215. return usb4->can_offline ? attr->mode : 0;
  216. }
  217. static const struct attribute_group service_group = {
  218. .attrs = service_attrs,
  219. .is_visible = service_attr_is_visible,
  220. };
  221. static const struct attribute_group *usb4_port_device_groups[] = {
  222. &common_group,
  223. &service_group,
  224. NULL
  225. };
  226. static void usb4_port_device_release(struct device *dev)
  227. {
  228. struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
  229. kfree(usb4);
  230. }
  231. const struct device_type usb4_port_device_type = {
  232. .name = "usb4_port",
  233. .groups = usb4_port_device_groups,
  234. .release = usb4_port_device_release,
  235. };
  236. /**
  237. * usb4_port_device_add() - Add USB4 port device
  238. * @port: Lane 0 adapter port to add the USB4 port
  239. *
  240. * Creates and registers a USB4 port device for @port.
  241. *
  242. * Return: Pointer to &struct usb4_port or ERR_PTR() in case of an error.
  243. */
  244. struct usb4_port *usb4_port_device_add(struct tb_port *port)
  245. {
  246. struct usb4_port *usb4;
  247. int ret;
  248. usb4 = kzalloc_obj(*usb4);
  249. if (!usb4)
  250. return ERR_PTR(-ENOMEM);
  251. usb4->port = port;
  252. usb4->dev.type = &usb4_port_device_type;
  253. usb4->dev.parent = &port->sw->dev;
  254. dev_set_name(&usb4->dev, "usb4_port%d", port->port);
  255. ret = device_register(&usb4->dev);
  256. if (ret) {
  257. put_device(&usb4->dev);
  258. return ERR_PTR(ret);
  259. }
  260. ret = component_add(&usb4->dev, &connector_ops);
  261. if (ret) {
  262. dev_err(&usb4->dev, "failed to add component\n");
  263. device_unregister(&usb4->dev);
  264. }
  265. if (!tb_is_upstream_port(port))
  266. device_set_wakeup_capable(&usb4->dev, true);
  267. pm_runtime_no_callbacks(&usb4->dev);
  268. pm_runtime_set_active(&usb4->dev);
  269. pm_runtime_enable(&usb4->dev);
  270. pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
  271. pm_runtime_mark_last_busy(&usb4->dev);
  272. pm_runtime_use_autosuspend(&usb4->dev);
  273. return usb4;
  274. }
  275. /**
  276. * usb4_port_device_remove() - Removes USB4 port device
  277. * @usb4: USB4 port device
  278. *
  279. * Unregisters the USB4 port device from the system. The device will be
  280. * released when the last reference is dropped.
  281. */
  282. void usb4_port_device_remove(struct usb4_port *usb4)
  283. {
  284. component_del(&usb4->dev, &connector_ops);
  285. device_unregister(&usb4->dev);
  286. }
  287. /**
  288. * usb4_port_device_resume() - Resumes USB4 port device
  289. * @usb4: USB4 port device
  290. *
  291. * Used to resume USB4 port device after sleep state.
  292. *
  293. * Return: %0 on success, negative errno otherwise.
  294. */
  295. int usb4_port_device_resume(struct usb4_port *usb4)
  296. {
  297. return usb4->offline ? usb4_port_offline(usb4) : 0;
  298. }