usb-acpi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_info *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. ACPI_FREE(pld);
  112. kfree(upc);
  113. return ret;
  114. }
  115. static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
  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 -ENODEV;
  155. }
  156. /* root hub's parent is the usb hcd. */
  157. parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
  158. *handle = acpi_get_child(parent_handle, udev->portnum);
  159. if (!*handle)
  160. return -ENODEV;
  161. return 0;
  162. } else if (is_usb_port(dev)) {
  163. sscanf(dev_name(dev), "port%d", &port_num);
  164. /* Get the struct usb_device point of port's hub */
  165. udev = to_usb_device(dev->parent->parent);
  166. /*
  167. * The root hub ports' parent is the root hub. The non-root-hub
  168. * ports' parent is the parent hub port which the hub is
  169. * connected to.
  170. */
  171. if (!udev->parent) {
  172. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
  173. port_num);
  174. if (!*handle)
  175. return -ENODEV;
  176. } else {
  177. parent_handle =
  178. usb_get_hub_port_acpi_handle(udev->parent,
  179. udev->portnum);
  180. if (!parent_handle)
  181. return -ENODEV;
  182. *handle = acpi_get_child(parent_handle, port_num);
  183. if (!*handle)
  184. return -ENODEV;
  185. }
  186. usb_acpi_check_port_connect_type(udev, *handle, port_num);
  187. } else
  188. return -ENODEV;
  189. return 0;
  190. }
  191. static struct acpi_bus_type usb_acpi_bus = {
  192. .bus = &usb_bus_type,
  193. .find_bridge = usb_acpi_find_device,
  194. .find_device = usb_acpi_find_device,
  195. };
  196. int usb_acpi_register(void)
  197. {
  198. return register_acpi_bus_type(&usb_acpi_bus);
  199. }
  200. void usb_acpi_unregister(void)
  201. {
  202. unregister_acpi_bus_type(&usb_acpi_bus);
  203. }