usb-acpi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * USB-ACPI glue code
  3. *
  4. * Copyright 2012 Red Hat <mjg@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/usb.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/acpi.h>
  17. #include <linux/pci.h>
  18. #include <linux/usb/hcd.h>
  19. #include "hub.h"
  20. /**
  21. * usb_acpi_power_manageable - check whether usb port has
  22. * acpi power resource.
  23. * @hdev: USB device belonging to the usb hub
  24. * @index: port index based zero
  25. *
  26. * Return true if the port has acpi power resource and false if no.
  27. */
  28. bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
  29. {
  30. acpi_handle port_handle;
  31. int port1 = index + 1;
  32. port_handle = usb_get_hub_port_acpi_handle(hdev,
  33. port1);
  34. if (port_handle)
  35. return acpi_bus_power_manageable(port_handle);
  36. else
  37. return false;
  38. }
  39. EXPORT_SYMBOL_GPL(usb_acpi_power_manageable);
  40. /**
  41. * usb_acpi_set_power_state - control usb port's power via acpi power
  42. * resource
  43. * @hdev: USB device belonging to the usb hub
  44. * @index: port index based zero
  45. * @enable: power state expected to be set
  46. *
  47. * Notice to use usb_acpi_power_manageable() to check whether the usb port
  48. * has acpi power resource before invoking this function.
  49. *
  50. * Returns 0 on success, else negative errno.
  51. */
  52. int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
  53. {
  54. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  55. struct usb_port *port_dev;
  56. acpi_handle port_handle;
  57. unsigned char state;
  58. int port1 = index + 1;
  59. int error = -EINVAL;
  60. if (!hub)
  61. return -ENODEV;
  62. port_dev = hub->ports[port1 - 1];
  63. port_handle = (acpi_handle) usb_get_hub_port_acpi_handle(hdev, port1);
  64. if (!port_handle)
  65. return error;
  66. if (enable)
  67. state = ACPI_STATE_D0;
  68. else
  69. state = ACPI_STATE_D3_COLD;
  70. error = acpi_bus_set_power(port_handle, state);
  71. if (!error)
  72. dev_dbg(&port_dev->dev, "acpi: power was set to %d\n", enable);
  73. else
  74. dev_dbg(&port_dev->dev, "acpi: power failed to be set\n");
  75. return error;
  76. }
  77. EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
  78. static enum usb_port_connect_type usb_acpi_get_connect_type(acpi_handle handle,
  79. struct acpi_pld_info *pld)
  80. {
  81. enum usb_port_connect_type connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
  82. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  83. union acpi_object *upc;
  84. acpi_status status;
  85. /*
  86. * According to ACPI Spec 9.13. PLD indicates whether usb port is
  87. * user visible and _UPC indicates whether it is connectable. If
  88. * the port was visible and connectable, it could be freely connected
  89. * and disconnected with USB devices. If no visible and connectable,
  90. * a usb device is directly hard-wired to the port. If no visible and
  91. * no connectable, the port would be not used.
  92. */
  93. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  94. upc = buffer.pointer;
  95. if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
  96. || upc->package.count != 4) {
  97. goto out;
  98. }
  99. if (upc->package.elements[0].integer.value)
  100. if (pld->user_visible)
  101. connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
  102. else
  103. connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
  104. else if (!pld->user_visible)
  105. connect_type = USB_PORT_NOT_USED;
  106. out:
  107. kfree(upc);
  108. return connect_type;
  109. }
  110. /*
  111. * Private to usb-acpi, all the core needs to know is that
  112. * port_dev->location is non-zero when it has been set by the firmware.
  113. */
  114. #define USB_ACPI_LOCATION_VALID (1 << 31)
  115. static struct acpi_device *usb_acpi_find_port(struct acpi_device *parent,
  116. int raw)
  117. {
  118. struct acpi_device *adev;
  119. if (!parent)
  120. return NULL;
  121. list_for_each_entry(adev, &parent->children, node) {
  122. if (acpi_device_adr(adev) == raw)
  123. return adev;
  124. }
  125. return acpi_find_child_device(parent, raw, false);
  126. }
  127. static struct acpi_device *usb_acpi_find_companion(struct device *dev)
  128. {
  129. struct usb_device *udev;
  130. struct acpi_device *adev;
  131. acpi_handle *parent_handle;
  132. /*
  133. * In the ACPI DSDT table, only usb root hub and usb ports are
  134. * acpi device nodes. The hierarchy like following.
  135. * Device (EHC1)
  136. * Device (HUBN)
  137. * Device (PR01)
  138. * Device (PR11)
  139. * Device (PR12)
  140. * Device (PR13)
  141. * ...
  142. * So all binding process is divided into two parts. binding
  143. * root hub and usb ports.
  144. */
  145. if (is_usb_device(dev)) {
  146. udev = to_usb_device(dev);
  147. if (udev->parent)
  148. return NULL;
  149. /* root hub is only child (_ADR=0) under its parent, the HC */
  150. adev = ACPI_COMPANION(dev->parent);
  151. return acpi_find_child_device(adev, 0, false);
  152. } else if (is_usb_port(dev)) {
  153. struct usb_port *port_dev = to_usb_port(dev);
  154. int port1 = port_dev->portnum;
  155. struct acpi_pld_info *pld;
  156. acpi_handle *handle;
  157. acpi_status status;
  158. /* Get the struct usb_device point of port's hub */
  159. udev = to_usb_device(dev->parent->parent);
  160. /*
  161. * The root hub ports' parent is the root hub. The non-root-hub
  162. * ports' parent is the parent hub port which the hub is
  163. * connected to.
  164. */
  165. if (!udev->parent) {
  166. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  167. int raw;
  168. raw = usb_hcd_find_raw_port_number(hcd, port1);
  169. adev = usb_acpi_find_port(ACPI_COMPANION(&udev->dev),
  170. raw);
  171. if (!adev)
  172. return NULL;
  173. } else {
  174. parent_handle =
  175. usb_get_hub_port_acpi_handle(udev->parent,
  176. udev->portnum);
  177. if (!parent_handle)
  178. return NULL;
  179. acpi_bus_get_device(parent_handle, &adev);
  180. adev = usb_acpi_find_port(adev, port1);
  181. if (!adev)
  182. return NULL;
  183. }
  184. handle = adev->handle;
  185. status = acpi_get_physical_device_location(handle, &pld);
  186. if (ACPI_FAILURE(status) || !pld)
  187. return adev;
  188. port_dev->location = USB_ACPI_LOCATION_VALID
  189. | pld->group_token << 8 | pld->group_position;
  190. port_dev->connect_type = usb_acpi_get_connect_type(handle, pld);
  191. ACPI_FREE(pld);
  192. return adev;
  193. }
  194. return NULL;
  195. }
  196. static bool usb_acpi_bus_match(struct device *dev)
  197. {
  198. return is_usb_device(dev) || is_usb_port(dev);
  199. }
  200. static struct acpi_bus_type usb_acpi_bus = {
  201. .name = "USB",
  202. .match = usb_acpi_bus_match,
  203. .find_companion = usb_acpi_find_companion,
  204. };
  205. int usb_acpi_register(void)
  206. {
  207. return register_acpi_bus_type(&usb_acpi_bus);
  208. }
  209. void usb_acpi_unregister(void)
  210. {
  211. unregister_acpi_bus_type(&usb_acpi_bus);
  212. }