usb-acpi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /**
  37. * usb_acpi_set_power_state - control usb port's power via acpi power
  38. * resource
  39. * @hdev: USB device belonging to the usb hub
  40. * @index: port index based zero
  41. * @enable: power state expected to be set
  42. *
  43. * Notice to use usb_acpi_power_manageable() to check whether the usb port
  44. * has acpi power resource before invoking this function.
  45. *
  46. * Returns 0 on success, else negative errno.
  47. */
  48. int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
  49. {
  50. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  51. struct usb_port *port_dev;
  52. acpi_handle port_handle;
  53. unsigned char state;
  54. int port1 = index + 1;
  55. int error = -EINVAL;
  56. if (!hub)
  57. return -ENODEV;
  58. port_dev = hub->ports[port1 - 1];
  59. port_handle = (acpi_handle) usb_get_hub_port_acpi_handle(hdev, port1);
  60. if (!port_handle)
  61. return error;
  62. if (enable)
  63. state = ACPI_STATE_D0;
  64. else
  65. state = ACPI_STATE_D3_COLD;
  66. error = acpi_bus_set_power(port_handle, state);
  67. if (!error)
  68. dev_dbg(&port_dev->dev, "acpi: power was set to %d\n", enable);
  69. else
  70. dev_dbg(&port_dev->dev, "acpi: power failed to be set\n");
  71. return error;
  72. }
  73. EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
  74. static enum usb_port_connect_type usb_acpi_get_connect_type(acpi_handle handle,
  75. struct acpi_pld_info *pld)
  76. {
  77. enum usb_port_connect_type connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
  78. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  79. union acpi_object *upc = NULL;
  80. acpi_status status;
  81. /*
  82. * According to 9.14 in ACPI Spec 6.2. _PLD indicates whether usb port
  83. * is user visible and _UPC indicates whether it is connectable. If
  84. * the port was visible and connectable, it could be freely connected
  85. * and disconnected with USB devices. If no visible and connectable,
  86. * a usb device is directly hard-wired to the port. If no visible and
  87. * no connectable, the port would be not used.
  88. */
  89. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  90. if (ACPI_FAILURE(status))
  91. goto out;
  92. upc = buffer.pointer;
  93. if (!upc || (upc->type != ACPI_TYPE_PACKAGE) || upc->package.count != 4)
  94. goto out;
  95. if (upc->package.elements[0].integer.value)
  96. if (pld->user_visible)
  97. connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
  98. else
  99. connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
  100. else if (!pld->user_visible)
  101. connect_type = USB_PORT_NOT_USED;
  102. out:
  103. kfree(upc);
  104. return connect_type;
  105. }
  106. /*
  107. * Private to usb-acpi, all the core needs to know is that
  108. * port_dev->location is non-zero when it has been set by the firmware.
  109. */
  110. #define USB_ACPI_LOCATION_VALID (1 << 31)
  111. static struct acpi_device *usb_acpi_find_port(struct acpi_device *parent,
  112. int raw)
  113. {
  114. struct acpi_device *adev;
  115. if (!parent)
  116. return NULL;
  117. list_for_each_entry(adev, &parent->children, node) {
  118. if (acpi_device_adr(adev) == raw)
  119. return adev;
  120. }
  121. return acpi_find_child_device(parent, raw, false);
  122. }
  123. static struct acpi_device *
  124. usb_acpi_get_companion_for_port(struct usb_port *port_dev)
  125. {
  126. struct usb_device *udev;
  127. struct acpi_device *adev;
  128. acpi_handle *parent_handle;
  129. int port1;
  130. /* Get the struct usb_device point of port's hub */
  131. udev = to_usb_device(port_dev->dev.parent->parent);
  132. /*
  133. * The root hub ports' parent is the root hub. The non-root-hub
  134. * ports' parent is the parent hub port which the hub is
  135. * connected to.
  136. */
  137. if (!udev->parent) {
  138. adev = ACPI_COMPANION(&udev->dev);
  139. port1 = usb_hcd_find_raw_port_number(bus_to_hcd(udev->bus),
  140. port_dev->portnum);
  141. } else {
  142. parent_handle = usb_get_hub_port_acpi_handle(udev->parent,
  143. udev->portnum);
  144. if (!parent_handle)
  145. return NULL;
  146. acpi_bus_get_device(parent_handle, &adev);
  147. port1 = port_dev->portnum;
  148. }
  149. return usb_acpi_find_port(adev, port1);
  150. }
  151. static struct acpi_device *
  152. usb_acpi_find_companion_for_port(struct usb_port *port_dev)
  153. {
  154. struct acpi_device *adev;
  155. struct acpi_pld_info *pld;
  156. acpi_handle *handle;
  157. acpi_status status;
  158. adev = usb_acpi_get_companion_for_port(port_dev);
  159. if (!adev)
  160. return NULL;
  161. handle = adev->handle;
  162. status = acpi_get_physical_device_location(handle, &pld);
  163. if (ACPI_SUCCESS(status) && pld) {
  164. port_dev->location = USB_ACPI_LOCATION_VALID
  165. | pld->group_token << 8 | pld->group_position;
  166. port_dev->connect_type = usb_acpi_get_connect_type(handle, pld);
  167. ACPI_FREE(pld);
  168. }
  169. return adev;
  170. }
  171. static struct acpi_device *
  172. usb_acpi_find_companion_for_device(struct usb_device *udev)
  173. {
  174. struct acpi_device *adev;
  175. struct usb_port *port_dev;
  176. struct usb_hub *hub;
  177. if (!udev->parent) {
  178. /* root hub is only child (_ADR=0) under its parent, the HC */
  179. adev = ACPI_COMPANION(udev->dev.parent);
  180. return acpi_find_child_device(adev, 0, false);
  181. }
  182. hub = usb_hub_to_struct_hub(udev->parent);
  183. if (!hub)
  184. return NULL;
  185. /*
  186. * This is an embedded USB device connected to a port and such
  187. * devices share port's ACPI companion.
  188. */
  189. port_dev = hub->ports[udev->portnum - 1];
  190. return usb_acpi_get_companion_for_port(port_dev);
  191. }
  192. static struct acpi_device *usb_acpi_find_companion(struct device *dev)
  193. {
  194. /*
  195. * The USB hierarchy like following:
  196. *
  197. * Device (EHC1)
  198. * Device (HUBN)
  199. * Device (PR01)
  200. * Device (PR11)
  201. * Device (PR12)
  202. * Device (FN12)
  203. * Device (FN13)
  204. * Device (PR13)
  205. * ...
  206. * where HUBN is root hub, and PRNN are USB ports and devices
  207. * connected to them, and FNNN are individualk functions for
  208. * connected composite USB devices. PRNN and FNNN may contain
  209. * _CRS and other methods describing sideband resources for
  210. * the connected device.
  211. *
  212. * On the kernel side both root hub and embedded USB devices are
  213. * represented as instances of usb_device structure, and ports
  214. * are represented as usb_port structures, so the whole process
  215. * is split into 2 parts: finding companions for devices and
  216. * finding companions for ports.
  217. *
  218. * Note that we do not handle individual functions of composite
  219. * devices yet, for that we would need to assign companions to
  220. * devices corresponding to USB interfaces.
  221. */
  222. if (is_usb_device(dev))
  223. return usb_acpi_find_companion_for_device(to_usb_device(dev));
  224. else if (is_usb_port(dev))
  225. return usb_acpi_find_companion_for_port(to_usb_port(dev));
  226. return NULL;
  227. }
  228. static bool usb_acpi_bus_match(struct device *dev)
  229. {
  230. return is_usb_device(dev) || is_usb_port(dev);
  231. }
  232. static struct acpi_bus_type usb_acpi_bus = {
  233. .name = "USB",
  234. .match = usb_acpi_bus_match,
  235. .find_companion = usb_acpi_find_companion,
  236. };
  237. int usb_acpi_register(void)
  238. {
  239. return register_acpi_bus_type(&usb_acpi_bus);
  240. }
  241. void usb_acpi_unregister(void)
  242. {
  243. unregister_acpi_bus_type(&usb_acpi_bus);
  244. }