usb-acpi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "usb.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. acpi_handle port_handle;
  55. unsigned char state;
  56. int port1 = index + 1;
  57. int error = -EINVAL;
  58. port_handle = (acpi_handle)usb_get_hub_port_acpi_handle(hdev,
  59. 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(&hdev->dev, "The power of hub port %d was set to %d\n",
  69. port1, enable);
  70. else
  71. dev_dbg(&hdev->dev, "The power of hub port failed to be set\n");
  72. return error;
  73. }
  74. EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
  75. static int usb_acpi_check_port_connect_type(struct usb_device *hdev,
  76. acpi_handle handle, int port1)
  77. {
  78. acpi_status status;
  79. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  80. union acpi_object *upc;
  81. struct acpi_pld_info *pld;
  82. int ret = 0;
  83. /*
  84. * According to ACPI Spec 9.13. PLD indicates whether usb port is
  85. * user visible and _UPC indicates whether it is connectable. If
  86. * the port was visible and connectable, it could be freely connected
  87. * and disconnected with USB devices. If no visible and connectable,
  88. * a usb device is directly hard-wired to the port. If no visible and
  89. * no connectable, the port would be not used.
  90. */
  91. status = acpi_get_physical_device_location(handle, &pld);
  92. if (ACPI_FAILURE(status))
  93. return -ENODEV;
  94. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  95. upc = buffer.pointer;
  96. if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
  97. || upc->package.count != 4) {
  98. ret = -EINVAL;
  99. goto out;
  100. }
  101. if (upc->package.elements[0].integer.value)
  102. if (pld->user_visible)
  103. usb_set_hub_port_connect_type(hdev, port1,
  104. USB_PORT_CONNECT_TYPE_HOT_PLUG);
  105. else
  106. usb_set_hub_port_connect_type(hdev, port1,
  107. USB_PORT_CONNECT_TYPE_HARD_WIRED);
  108. else if (!pld->user_visible)
  109. usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED);
  110. out:
  111. ACPI_FREE(pld);
  112. kfree(upc);
  113. return ret;
  114. }
  115. static struct acpi_device *usb_acpi_find_companion(struct device *dev)
  116. {
  117. struct usb_device *udev;
  118. acpi_handle *parent_handle;
  119. int port_num;
  120. /*
  121. * In the ACPI DSDT table, only usb root hub and usb ports are
  122. * acpi device nodes. The hierarchy like following.
  123. * Device (EHC1)
  124. * Device (HUBN)
  125. * Device (PR01)
  126. * Device (PR11)
  127. * Device (PR12)
  128. * Device (PR13)
  129. * ...
  130. * So all binding process is divided into two parts. binding
  131. * root hub and usb ports.
  132. */
  133. if (is_usb_device(dev)) {
  134. udev = to_usb_device(dev);
  135. if (udev->parent) {
  136. enum usb_port_connect_type type;
  137. /*
  138. * According usb port's connect type to set usb device's
  139. * removability.
  140. */
  141. type = usb_get_hub_port_connect_type(udev->parent,
  142. udev->portnum);
  143. switch (type) {
  144. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  145. udev->removable = USB_DEVICE_REMOVABLE;
  146. break;
  147. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  148. udev->removable = USB_DEVICE_FIXED;
  149. break;
  150. default:
  151. udev->removable = USB_DEVICE_REMOVABLE_UNKNOWN;
  152. break;
  153. }
  154. return NULL;
  155. }
  156. /* root hub's parent is the usb hcd. */
  157. return acpi_find_child_device(ACPI_COMPANION(dev->parent),
  158. udev->portnum, false);
  159. } else if (is_usb_port(dev)) {
  160. struct acpi_device *adev = NULL;
  161. sscanf(dev_name(dev), "port%d", &port_num);
  162. /* Get the struct usb_device point of port's hub */
  163. udev = to_usb_device(dev->parent->parent);
  164. /*
  165. * The root hub ports' parent is the root hub. The non-root-hub
  166. * ports' parent is the parent hub port which the hub is
  167. * connected to.
  168. */
  169. if (!udev->parent) {
  170. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  171. int raw_port_num;
  172. raw_port_num = usb_hcd_find_raw_port_number(hcd,
  173. port_num);
  174. adev = acpi_find_child_device(ACPI_COMPANION(&udev->dev),
  175. raw_port_num, false);
  176. if (!adev)
  177. return NULL;
  178. } else {
  179. parent_handle =
  180. usb_get_hub_port_acpi_handle(udev->parent,
  181. udev->portnum);
  182. if (!parent_handle)
  183. return NULL;
  184. acpi_bus_get_device(parent_handle, &adev);
  185. adev = acpi_find_child_device(adev, port_num, false);
  186. if (!adev)
  187. return NULL;
  188. }
  189. usb_acpi_check_port_connect_type(udev, adev->handle, port_num);
  190. return adev;
  191. }
  192. return NULL;
  193. }
  194. static bool usb_acpi_bus_match(struct device *dev)
  195. {
  196. return is_usb_device(dev) || is_usb_port(dev);
  197. }
  198. static struct acpi_bus_type usb_acpi_bus = {
  199. .name = "USB",
  200. .match = usb_acpi_bus_match,
  201. .find_companion = usb_acpi_find_companion,
  202. };
  203. int usb_acpi_register(void)
  204. {
  205. return register_acpi_bus_type(&usb_acpi_bus);
  206. }
  207. void usb_acpi_unregister(void)
  208. {
  209. unregister_acpi_bus_type(&usb_acpi_bus);
  210. }