usb-acpi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <acpi/acpi_bus.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 pld;
  82. int ret = 0;
  83. /*
  84. * Accoding 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. kfree(upc);
  112. return ret;
  113. }
  114. static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
  115. {
  116. struct usb_device *udev;
  117. acpi_handle *parent_handle;
  118. int port_num;
  119. /*
  120. * In the ACPI DSDT table, only usb root hub and usb ports are
  121. * acpi device nodes. The hierarchy like following.
  122. * Device (EHC1)
  123. * Device (HUBN)
  124. * Device (PR01)
  125. * Device (PR11)
  126. * Device (PR12)
  127. * Device (PR13)
  128. * ...
  129. * So all binding process is divided into two parts. binding
  130. * root hub and usb ports.
  131. */
  132. if (is_usb_device(dev)) {
  133. udev = to_usb_device(dev);
  134. if (udev->parent) {
  135. enum usb_port_connect_type type;
  136. /*
  137. * According usb port's connect type to set usb device's
  138. * removability.
  139. */
  140. type = usb_get_hub_port_connect_type(udev->parent,
  141. udev->portnum);
  142. switch (type) {
  143. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  144. udev->removable = USB_DEVICE_REMOVABLE;
  145. break;
  146. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  147. udev->removable = USB_DEVICE_FIXED;
  148. break;
  149. default:
  150. udev->removable = USB_DEVICE_REMOVABLE_UNKNOWN;
  151. break;
  152. }
  153. return -ENODEV;
  154. }
  155. /* root hub's parent is the usb hcd. */
  156. parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
  157. *handle = acpi_get_child(parent_handle, udev->portnum);
  158. if (!*handle)
  159. return -ENODEV;
  160. return 0;
  161. } else if (is_usb_port(dev)) {
  162. sscanf(dev_name(dev), "port%d", &port_num);
  163. /* Get the struct usb_device point of port's hub */
  164. udev = to_usb_device(dev->parent->parent);
  165. /*
  166. * The root hub ports' parent is the root hub. The non-root-hub
  167. * ports' parent is the parent hub port which the hub is
  168. * connected to.
  169. */
  170. if (!udev->parent) {
  171. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
  172. port_num);
  173. if (!*handle)
  174. return -ENODEV;
  175. } else {
  176. parent_handle =
  177. usb_get_hub_port_acpi_handle(udev->parent,
  178. udev->portnum);
  179. if (!parent_handle)
  180. return -ENODEV;
  181. *handle = acpi_get_child(parent_handle, port_num);
  182. if (!*handle)
  183. return -ENODEV;
  184. }
  185. usb_acpi_check_port_connect_type(udev, *handle, port_num);
  186. } else
  187. return -ENODEV;
  188. return 0;
  189. }
  190. static struct acpi_bus_type usb_acpi_bus = {
  191. .bus = &usb_bus_type,
  192. .find_bridge = usb_acpi_find_device,
  193. .find_device = usb_acpi_find_device,
  194. };
  195. int usb_acpi_register(void)
  196. {
  197. return register_acpi_bus_type(&usb_acpi_bus);
  198. }
  199. void usb_acpi_unregister(void)
  200. {
  201. unregister_acpi_bus_type(&usb_acpi_bus);
  202. }