usb-acpi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <acpi/acpi_bus.h>
  20. #include "usb.h"
  21. /**
  22. * usb_acpi_power_manageable - check whether usb port has
  23. * acpi power resource.
  24. * @hdev: USB device belonging to the usb hub
  25. * @index: port index based zero
  26. *
  27. * Return true if the port has acpi power resource and false if no.
  28. */
  29. bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
  30. {
  31. acpi_handle port_handle;
  32. int port1 = index + 1;
  33. port_handle = usb_get_hub_port_acpi_handle(hdev,
  34. port1);
  35. if (port_handle)
  36. return acpi_bus_power_manageable(port_handle);
  37. else
  38. return false;
  39. }
  40. EXPORT_SYMBOL_GPL(usb_acpi_power_manageable);
  41. /**
  42. * usb_acpi_set_power_state - control usb port's power via acpi power
  43. * resource
  44. * @hdev: USB device belonging to the usb hub
  45. * @index: port index based zero
  46. * @enable: power state expected to be set
  47. *
  48. * Notice to use usb_acpi_power_manageable() to check whether the usb port
  49. * has acpi power resource before invoking this function.
  50. *
  51. * Returns 0 on success, else negative errno.
  52. */
  53. int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
  54. {
  55. acpi_handle port_handle;
  56. unsigned char state;
  57. int port1 = index + 1;
  58. int error = -EINVAL;
  59. port_handle = (acpi_handle)usb_get_hub_port_acpi_handle(hdev,
  60. port1);
  61. if (!port_handle)
  62. return error;
  63. if (enable)
  64. state = ACPI_STATE_D0;
  65. else
  66. state = ACPI_STATE_D3_COLD;
  67. error = acpi_bus_set_power(port_handle, state);
  68. if (!error)
  69. dev_dbg(&hdev->dev, "The power of hub port %d was set to %d\n",
  70. port1, enable);
  71. else
  72. dev_dbg(&hdev->dev, "The power of hub port failed to be set\n");
  73. return error;
  74. }
  75. EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
  76. static int usb_acpi_check_port_connect_type(struct usb_device *hdev,
  77. acpi_handle handle, int port1)
  78. {
  79. acpi_status status;
  80. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  81. union acpi_object *upc;
  82. struct acpi_pld_info *pld;
  83. int ret = 0;
  84. /*
  85. * Accoding to ACPI Spec 9.13. PLD indicates whether usb port is
  86. * user visible and _UPC indicates whether it is connectable. If
  87. * the port was visible and connectable, it could be freely connected
  88. * and disconnected with USB devices. If no visible and connectable,
  89. * a usb device is directly hard-wired to the port. If no visible and
  90. * no connectable, the port would be not used.
  91. */
  92. status = acpi_get_physical_device_location(handle, &pld);
  93. if (ACPI_FAILURE(status))
  94. return -ENODEV;
  95. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  96. upc = buffer.pointer;
  97. if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
  98. || upc->package.count != 4) {
  99. ret = -EINVAL;
  100. goto out;
  101. }
  102. if (upc->package.elements[0].integer.value)
  103. if (pld->user_visible)
  104. usb_set_hub_port_connect_type(hdev, port1,
  105. USB_PORT_CONNECT_TYPE_HOT_PLUG);
  106. else
  107. usb_set_hub_port_connect_type(hdev, port1,
  108. USB_PORT_CONNECT_TYPE_HARD_WIRED);
  109. else if (!pld->user_visible)
  110. usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED);
  111. out:
  112. ACPI_FREE(pld);
  113. kfree(upc);
  114. return ret;
  115. }
  116. static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
  117. {
  118. struct usb_device *udev;
  119. acpi_handle *parent_handle;
  120. int port_num;
  121. /*
  122. * In the ACPI DSDT table, only usb root hub and usb ports are
  123. * acpi device nodes. The hierarchy like following.
  124. * Device (EHC1)
  125. * Device (HUBN)
  126. * Device (PR01)
  127. * Device (PR11)
  128. * Device (PR12)
  129. * Device (PR13)
  130. * ...
  131. * So all binding process is divided into two parts. binding
  132. * root hub and usb ports.
  133. */
  134. if (is_usb_device(dev)) {
  135. udev = to_usb_device(dev);
  136. if (udev->parent) {
  137. enum usb_port_connect_type type;
  138. /*
  139. * According usb port's connect type to set usb device's
  140. * removability.
  141. */
  142. type = usb_get_hub_port_connect_type(udev->parent,
  143. udev->portnum);
  144. switch (type) {
  145. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  146. udev->removable = USB_DEVICE_REMOVABLE;
  147. break;
  148. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  149. udev->removable = USB_DEVICE_FIXED;
  150. break;
  151. default:
  152. udev->removable = USB_DEVICE_REMOVABLE_UNKNOWN;
  153. break;
  154. }
  155. return -ENODEV;
  156. }
  157. /* root hub's parent is the usb hcd. */
  158. parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
  159. *handle = acpi_get_child(parent_handle, udev->portnum);
  160. if (!*handle)
  161. return -ENODEV;
  162. return 0;
  163. } else if (is_usb_port(dev)) {
  164. sscanf(dev_name(dev), "port%d", &port_num);
  165. /* Get the struct usb_device point of port's hub */
  166. udev = to_usb_device(dev->parent->parent);
  167. /*
  168. * The root hub ports' parent is the root hub. The non-root-hub
  169. * ports' parent is the parent hub port which the hub is
  170. * connected to.
  171. */
  172. if (!udev->parent) {
  173. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  174. int raw_port_num;
  175. raw_port_num = usb_hcd_find_raw_port_number(hcd,
  176. port_num);
  177. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
  178. raw_port_num);
  179. if (!*handle)
  180. return -ENODEV;
  181. } else {
  182. parent_handle =
  183. usb_get_hub_port_acpi_handle(udev->parent,
  184. udev->portnum);
  185. if (!parent_handle)
  186. return -ENODEV;
  187. *handle = acpi_get_child(parent_handle, port_num);
  188. if (!*handle)
  189. return -ENODEV;
  190. }
  191. usb_acpi_check_port_connect_type(udev, *handle, port_num);
  192. } else
  193. return -ENODEV;
  194. return 0;
  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_device = usb_acpi_find_device,
  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. }