usb-acpi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static int usb_acpi_check_port_connect_type(struct usb_device *hdev,
  21. acpi_handle handle, int port1)
  22. {
  23. acpi_status status;
  24. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  25. union acpi_object *upc;
  26. struct acpi_pld pld;
  27. int ret = 0;
  28. /*
  29. * Accoding to ACPI Spec 9.13. PLD indicates whether usb port is
  30. * user visible and _UPC indicates whether it is connectable. If
  31. * the port was visible and connectable, it could be freely connected
  32. * and disconnected with USB devices. If no visible and connectable,
  33. * a usb device is directly hard-wired to the port. If no visible and
  34. * no connectable, the port would be not used.
  35. */
  36. status = acpi_get_physical_device_location(handle, &pld);
  37. if (ACPI_FAILURE(status))
  38. return -ENODEV;
  39. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  40. upc = buffer.pointer;
  41. if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
  42. || upc->package.count != 4) {
  43. ret = -EINVAL;
  44. goto out;
  45. }
  46. if (upc->package.elements[0].integer.value)
  47. if (pld.user_visible)
  48. usb_set_hub_port_connect_type(hdev, port1,
  49. USB_PORT_CONNECT_TYPE_HOT_PLUG);
  50. else
  51. usb_set_hub_port_connect_type(hdev, port1,
  52. USB_PORT_CONNECT_TYPE_HARD_WIRED);
  53. else if (!pld.user_visible)
  54. usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED);
  55. out:
  56. kfree(upc);
  57. return ret;
  58. }
  59. static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
  60. {
  61. struct usb_device *udev;
  62. acpi_handle *parent_handle;
  63. int port_num;
  64. /*
  65. * In the ACPI DSDT table, only usb root hub and usb ports are
  66. * acpi device nodes. The hierarchy like following.
  67. * Device (EHC1)
  68. * Device (HUBN)
  69. * Device (PR01)
  70. * Device (PR11)
  71. * Device (PR12)
  72. * Device (PR13)
  73. * ...
  74. * So all binding process is divided into two parts. binding
  75. * root hub and usb ports.
  76. */
  77. if (is_usb_device(dev)) {
  78. udev = to_usb_device(dev);
  79. if (udev->parent) {
  80. enum usb_port_connect_type type;
  81. /*
  82. * According usb port's connect type to set usb device's
  83. * removability.
  84. */
  85. type = usb_get_hub_port_connect_type(udev->parent,
  86. udev->portnum);
  87. switch (type) {
  88. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  89. udev->removable = USB_DEVICE_REMOVABLE;
  90. break;
  91. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  92. udev->removable = USB_DEVICE_FIXED;
  93. break;
  94. default:
  95. udev->removable = USB_DEVICE_REMOVABLE_UNKNOWN;
  96. break;
  97. }
  98. return -ENODEV;
  99. }
  100. /* root hub's parent is the usb hcd. */
  101. parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
  102. *handle = acpi_get_child(parent_handle, udev->portnum);
  103. if (!*handle)
  104. return -ENODEV;
  105. return 0;
  106. } else if (is_usb_port(dev)) {
  107. sscanf(dev_name(dev), "port%d", &port_num);
  108. /* Get the struct usb_device point of port's hub */
  109. udev = to_usb_device(dev->parent->parent);
  110. /*
  111. * The root hub ports' parent is the root hub. The non-root-hub
  112. * ports' parent is the parent hub port which the hub is
  113. * connected to.
  114. */
  115. if (!udev->parent) {
  116. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
  117. port_num);
  118. if (!*handle)
  119. return -ENODEV;
  120. } else {
  121. parent_handle =
  122. usb_get_hub_port_acpi_handle(udev->parent,
  123. udev->portnum);
  124. if (!parent_handle)
  125. return -ENODEV;
  126. *handle = acpi_get_child(parent_handle, port_num);
  127. if (!*handle)
  128. return -ENODEV;
  129. }
  130. usb_acpi_check_port_connect_type(udev, *handle, port_num);
  131. } else
  132. return -ENODEV;
  133. return 0;
  134. }
  135. static struct acpi_bus_type usb_acpi_bus = {
  136. .bus = &usb_bus_type,
  137. .find_bridge = usb_acpi_find_device,
  138. .find_device = usb_acpi_find_device,
  139. };
  140. int usb_acpi_register(void)
  141. {
  142. return register_acpi_bus_type(&usb_acpi_bus);
  143. }
  144. void usb_acpi_unregister(void)
  145. {
  146. unregister_acpi_bus_type(&usb_acpi_bus);
  147. }