usb-acpi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_upc(struct usb_device *udev, acpi_handle handle)
  21. {
  22. acpi_status status;
  23. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  24. union acpi_object *upc;
  25. int ret = 0;
  26. status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
  27. if (ACPI_FAILURE(status))
  28. return -ENODEV;
  29. upc = buffer.pointer;
  30. if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
  31. || upc->package.count != 4) {
  32. ret = -EINVAL;
  33. goto out;
  34. }
  35. if (upc->package.elements[0].integer.value)
  36. udev->removable = USB_DEVICE_REMOVABLE;
  37. else
  38. udev->removable = USB_DEVICE_FIXED;
  39. out:
  40. kfree(upc);
  41. return ret;
  42. }
  43. static int usb_acpi_check_pld(struct usb_device *udev, acpi_handle handle)
  44. {
  45. acpi_status status;
  46. struct acpi_pld pld;
  47. status = acpi_get_physical_device_location(handle, &pld);
  48. if (ACPI_FAILURE(status))
  49. return -ENODEV;
  50. if (pld.user_visible)
  51. udev->removable = USB_DEVICE_REMOVABLE;
  52. else
  53. udev->removable = USB_DEVICE_FIXED;
  54. return 0;
  55. }
  56. static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
  57. {
  58. struct usb_device *udev;
  59. struct device *parent;
  60. acpi_handle *parent_handle;
  61. if (!is_usb_device(dev))
  62. return -ENODEV;
  63. udev = to_usb_device(dev);
  64. parent = dev->parent;
  65. parent_handle = DEVICE_ACPI_HANDLE(parent);
  66. if (!parent_handle)
  67. return -ENODEV;
  68. *handle = acpi_get_child(parent_handle, udev->portnum);
  69. if (!*handle)
  70. return -ENODEV;
  71. /*
  72. * PLD will tell us whether a port is removable to the user or
  73. * not. If we don't get an answer from PLD (it's not present
  74. * or it's malformed) then try to infer it from UPC. If a
  75. * device isn't connectable then it's probably not removable.
  76. */
  77. if (usb_acpi_check_pld(udev, *handle) != 0)
  78. usb_acpi_check_upc(udev, *handle);
  79. return 0;
  80. }
  81. static struct acpi_bus_type usb_acpi_bus = {
  82. .bus = &usb_bus_type,
  83. .find_bridge = NULL,
  84. .find_device = usb_acpi_find_device,
  85. };
  86. int usb_acpi_register(void)
  87. {
  88. return register_acpi_bus_type(&usb_acpi_bus);
  89. }
  90. void usb_acpi_unregister(void)
  91. {
  92. unregister_acpi_bus_type(&usb_acpi_bus);
  93. }