usb-acpi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB-ACPI glue code
  4. *
  5. * Copyright 2012 Red Hat <mjg@redhat.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/usb.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/acpi.h>
  13. #include <linux/pci.h>
  14. #include <linux/usb/hcd.h>
  15. #include "hub.h"
  16. /**
  17. * usb_acpi_power_manageable - check whether usb port has
  18. * acpi power resource.
  19. * @hdev: USB device belonging to the usb hub
  20. * @index: port index based zero
  21. *
  22. * Return true if the port has acpi power resource and false if no.
  23. */
  24. bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
  25. {
  26. acpi_handle port_handle;
  27. int port1 = index + 1;
  28. port_handle = usb_get_hub_port_acpi_handle(hdev,
  29. port1);
  30. if (port_handle)
  31. return acpi_bus_power_manageable(port_handle);
  32. else
  33. return false;
  34. }
  35. EXPORT_SYMBOL_GPL(usb_acpi_power_manageable);
  36. #define UUID_USB_CONTROLLER_DSM "ce2ee385-00e6-48cb-9f05-2edb927c4899"
  37. #define USB_DSM_DISABLE_U1_U2_FOR_PORT 5
  38. /**
  39. * usb_acpi_port_lpm_incapable - check if lpm should be disabled for a port.
  40. * @hdev: USB device belonging to the usb hub
  41. * @index: zero based port index
  42. *
  43. * Some USB3 ports may not support USB3 link power management U1/U2 states
  44. * due to different retimer setup. ACPI provides _DSM method which returns 0x01
  45. * if U1 and U2 states should be disabled. Evaluate _DSM with:
  46. * Arg0: UUID = ce2ee385-00e6-48cb-9f05-2edb927c4899
  47. * Arg1: Revision ID = 0
  48. * Arg2: Function Index = 5
  49. * Arg3: (empty)
  50. *
  51. * Return 1 if USB3 port is LPM incapable, negative on error, otherwise 0
  52. */
  53. int usb_acpi_port_lpm_incapable(struct usb_device *hdev, int index)
  54. {
  55. union acpi_object *obj;
  56. acpi_handle port_handle;
  57. int port1 = index + 1;
  58. guid_t guid;
  59. int ret;
  60. ret = guid_parse(UUID_USB_CONTROLLER_DSM, &guid);
  61. if (ret)
  62. return ret;
  63. port_handle = usb_get_hub_port_acpi_handle(hdev, port1);
  64. if (!port_handle) {
  65. dev_dbg(&hdev->dev, "port-%d no acpi handle\n", port1);
  66. return -ENODEV;
  67. }
  68. if (!acpi_check_dsm(port_handle, &guid, 0,
  69. BIT(USB_DSM_DISABLE_U1_U2_FOR_PORT))) {
  70. dev_dbg(&hdev->dev, "port-%d no _DSM function %d\n",
  71. port1, USB_DSM_DISABLE_U1_U2_FOR_PORT);
  72. return -ENODEV;
  73. }
  74. obj = acpi_evaluate_dsm_typed(port_handle, &guid, 0,
  75. USB_DSM_DISABLE_U1_U2_FOR_PORT, NULL,
  76. ACPI_TYPE_INTEGER);
  77. if (!obj) {
  78. dev_dbg(&hdev->dev, "evaluate port-%d _DSM failed\n", port1);
  79. return -EINVAL;
  80. }
  81. if (obj->integer.value == 0x01)
  82. ret = 1;
  83. ACPI_FREE(obj);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL_GPL(usb_acpi_port_lpm_incapable);
  87. /**
  88. * usb_acpi_set_power_state - control usb port's power via acpi power
  89. * resource
  90. * @hdev: USB device belonging to the usb hub
  91. * @index: port index based zero
  92. * @enable: power state expected to be set
  93. *
  94. * Notice to use usb_acpi_power_manageable() to check whether the usb port
  95. * has acpi power resource before invoking this function.
  96. *
  97. * Returns 0 on success, else negative errno.
  98. */
  99. int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
  100. {
  101. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  102. struct usb_port *port_dev;
  103. acpi_handle port_handle;
  104. unsigned char state;
  105. int port1 = index + 1;
  106. int error = -EINVAL;
  107. if (!hub)
  108. return -ENODEV;
  109. port_dev = hub->ports[port1 - 1];
  110. port_handle = (acpi_handle) usb_get_hub_port_acpi_handle(hdev, port1);
  111. if (!port_handle)
  112. return error;
  113. if (enable)
  114. state = ACPI_STATE_D0;
  115. else
  116. state = ACPI_STATE_D3_COLD;
  117. error = acpi_bus_set_power(port_handle, state);
  118. if (!error)
  119. dev_dbg(&port_dev->dev, "acpi: power was set to %d\n", enable);
  120. else
  121. dev_dbg(&port_dev->dev, "acpi: power failed to be set\n");
  122. return error;
  123. }
  124. EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
  125. /**
  126. * usb_acpi_add_usb4_devlink - add device link to USB4 Host Interface for tunneled USB3 devices
  127. *
  128. * @udev: Tunneled USB3 device connected to a roothub.
  129. *
  130. * Adds a device link between a tunneled USB3 device and the USB4 Host Interface
  131. * device to ensure correct runtime PM suspend and resume order. This function
  132. * should only be called for tunneled USB3 devices.
  133. * The USB4 Host Interface this tunneled device depends on is found from the roothub
  134. * port ACPI device specific data _DSD entry.
  135. *
  136. * Return: negative error code on failure, 0 otherwise
  137. */
  138. static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
  139. {
  140. struct device_link *link;
  141. struct usb_port *port_dev;
  142. struct usb_hub *hub;
  143. if (!udev->parent || udev->parent->parent)
  144. return 0;
  145. hub = usb_hub_to_struct_hub(udev->parent);
  146. if (!hub)
  147. return 0;
  148. port_dev = hub->ports[udev->portnum - 1];
  149. struct fwnode_handle *nhi_fwnode __free(fwnode_handle) =
  150. fwnode_find_reference(dev_fwnode(&port_dev->dev), "usb4-host-interface", 0);
  151. if (IS_ERR(nhi_fwnode) || !nhi_fwnode->dev)
  152. return 0;
  153. link = device_link_add(&port_dev->child->dev, nhi_fwnode->dev,
  154. DL_FLAG_STATELESS |
  155. DL_FLAG_RPM_ACTIVE |
  156. DL_FLAG_PM_RUNTIME);
  157. if (!link) {
  158. dev_err(&port_dev->dev, "Failed to created device link from %s to %s\n",
  159. dev_name(&port_dev->child->dev), dev_name(nhi_fwnode->dev));
  160. return -EINVAL;
  161. }
  162. dev_dbg(&port_dev->dev, "Created device link from %s to %s\n",
  163. dev_name(&port_dev->child->dev), dev_name(nhi_fwnode->dev));
  164. udev->usb4_link = link;
  165. return 0;
  166. }
  167. /*
  168. * Private to usb-acpi, all the core needs to know is that
  169. * port_dev->location is non-zero when it has been set by the firmware.
  170. */
  171. #define USB_ACPI_LOCATION_VALID (1 << 31)
  172. static void
  173. usb_acpi_get_connect_type(struct usb_port *port_dev, acpi_handle *handle)
  174. {
  175. enum usb_port_connect_type connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
  176. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  177. union acpi_object *upc = NULL;
  178. struct acpi_pld_info *pld = NULL;
  179. acpi_status status;
  180. /*
  181. * According to 9.14 in ACPI Spec 6.2. _PLD indicates whether usb port
  182. * is user visible and _UPC indicates whether it is connectable. If
  183. * the port was visible and connectable, it could be freely connected
  184. * and disconnected with USB devices. If no visible and connectable,
  185. * a usb device is directly hard-wired to the port. If no visible and
  186. * no connectable, the port would be not used.
  187. */
  188. status = acpi_get_physical_device_location(handle, &pld);
  189. if (ACPI_SUCCESS(status) && pld)
  190. port_dev->location = USB_ACPI_LOCATION_VALID |
  191. pld->group_token << 8 | pld->group_position;
  192. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  193. if (ACPI_FAILURE(status))
  194. goto out;
  195. upc = buffer.pointer;
  196. if (!upc || (upc->type != ACPI_TYPE_PACKAGE) || upc->package.count != 4)
  197. goto out;
  198. /* UPC states port is connectable */
  199. if (upc->package.elements[0].integer.value)
  200. if (!pld)
  201. ; /* keep connect_type as unknown */
  202. else if (pld->user_visible)
  203. connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
  204. else
  205. connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
  206. else
  207. connect_type = USB_PORT_NOT_USED;
  208. out:
  209. port_dev->connect_type = connect_type;
  210. kfree(upc);
  211. ACPI_FREE(pld);
  212. }
  213. static struct acpi_device *
  214. usb_acpi_get_companion_for_port(struct usb_port *port_dev)
  215. {
  216. struct usb_device *udev;
  217. struct acpi_device *adev;
  218. acpi_handle *parent_handle;
  219. int port1;
  220. /* Get the struct usb_device point of port's hub */
  221. udev = to_usb_device(port_dev->dev.parent->parent);
  222. /*
  223. * The root hub ports' parent is the root hub. The non-root-hub
  224. * ports' parent is the parent hub port which the hub is
  225. * connected to.
  226. */
  227. if (!udev->parent) {
  228. adev = ACPI_COMPANION(&udev->dev);
  229. port1 = usb_hcd_find_raw_port_number(bus_to_hcd(udev->bus),
  230. port_dev->portnum);
  231. } else {
  232. parent_handle = usb_get_hub_port_acpi_handle(udev->parent,
  233. udev->portnum);
  234. if (!parent_handle)
  235. return NULL;
  236. adev = acpi_fetch_acpi_dev(parent_handle);
  237. port1 = port_dev->portnum;
  238. }
  239. return acpi_find_child_by_adr(adev, port1);
  240. }
  241. static struct acpi_device *
  242. usb_acpi_find_companion_for_port(struct usb_port *port_dev)
  243. {
  244. struct acpi_device *adev;
  245. adev = usb_acpi_get_companion_for_port(port_dev);
  246. if (!adev)
  247. return NULL;
  248. usb_acpi_get_connect_type(port_dev, adev->handle);
  249. return adev;
  250. }
  251. static struct acpi_device *
  252. usb_acpi_find_companion_for_device(struct usb_device *udev)
  253. {
  254. struct acpi_device *adev;
  255. struct usb_port *port_dev;
  256. struct usb_hub *hub;
  257. if (!udev->parent) {
  258. /*
  259. * root hub is only child (_ADR=0) under its parent, the HC.
  260. * sysdev pointer is the HC as seen from firmware.
  261. */
  262. adev = ACPI_COMPANION(udev->bus->sysdev);
  263. return acpi_find_child_device(adev, 0, false);
  264. }
  265. hub = usb_hub_to_struct_hub(udev->parent);
  266. if (!hub)
  267. return NULL;
  268. /* Tunneled USB3 devices depend on USB4 Host Interface, set device link to it */
  269. if (udev->speed >= USB_SPEED_SUPER &&
  270. udev->tunnel_mode != USB_LINK_NATIVE)
  271. usb_acpi_add_usb4_devlink(udev);
  272. /*
  273. * This is an embedded USB device connected to a port and such
  274. * devices share port's ACPI companion.
  275. */
  276. port_dev = hub->ports[udev->portnum - 1];
  277. return usb_acpi_get_companion_for_port(port_dev);
  278. }
  279. static struct acpi_device *usb_acpi_find_companion(struct device *dev)
  280. {
  281. /*
  282. * The USB hierarchy like following:
  283. *
  284. * Device (EHC1)
  285. * Device (HUBN)
  286. * Device (PR01)
  287. * Device (PR11)
  288. * Device (PR12)
  289. * Device (FN12)
  290. * Device (FN13)
  291. * Device (PR13)
  292. * ...
  293. * where HUBN is root hub, and PRNN are USB ports and devices
  294. * connected to them, and FNNN are individualk functions for
  295. * connected composite USB devices. PRNN and FNNN may contain
  296. * _CRS and other methods describing sideband resources for
  297. * the connected device.
  298. *
  299. * On the kernel side both root hub and embedded USB devices are
  300. * represented as instances of usb_device structure, and ports
  301. * are represented as usb_port structures, so the whole process
  302. * is split into 2 parts: finding companions for devices and
  303. * finding companions for ports.
  304. *
  305. * Note that we do not handle individual functions of composite
  306. * devices yet, for that we would need to assign companions to
  307. * devices corresponding to USB interfaces.
  308. */
  309. if (is_usb_device(dev))
  310. return usb_acpi_find_companion_for_device(to_usb_device(dev));
  311. else if (is_usb_port(dev))
  312. return usb_acpi_find_companion_for_port(to_usb_port(dev));
  313. return NULL;
  314. }
  315. static bool usb_acpi_bus_match(struct device *dev)
  316. {
  317. return is_usb_device(dev) || is_usb_port(dev);
  318. }
  319. static struct acpi_bus_type usb_acpi_bus = {
  320. .name = "USB",
  321. .match = usb_acpi_bus_match,
  322. .find_companion = usb_acpi_find_companion,
  323. };
  324. int usb_acpi_register(void)
  325. {
  326. return register_acpi_bus_type(&usb_acpi_bus);
  327. }
  328. void usb_acpi_unregister(void)
  329. {
  330. unregister_acpi_bus_type(&usb_acpi_bus);
  331. }