container.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * acpi_container.c - ACPI Generic Container Driver
  3. * ($Revision: )
  4. *
  5. * Copyright (C) 2004 Anil S Keshavamurthy (anil.s.keshavamurthy@intel.com)
  6. * Copyright (C) 2004 Keiichiro Tokunaga (tokunaga.keiich@jp.fujitsu.com)
  7. * Copyright (C) 2004 Motoyuki Ito (motoyuki@soft.fujitsu.com)
  8. * Copyright (C) 2004 Intel Corp.
  9. * Copyright (C) 2004 FUJITSU LIMITED
  10. *
  11. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  26. *
  27. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/types.h>
  34. #include <linux/acpi.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #include <acpi/container.h>
  38. #define PREFIX "ACPI: "
  39. #define _COMPONENT ACPI_CONTAINER_COMPONENT
  40. ACPI_MODULE_NAME("container");
  41. static const struct acpi_device_id container_device_ids[] = {
  42. {"ACPI0004", 0},
  43. {"PNP0A05", 0},
  44. {"PNP0A06", 0},
  45. {"", 0},
  46. };
  47. static int container_device_attach(struct acpi_device *device,
  48. const struct acpi_device_id *not_used)
  49. {
  50. /*
  51. * FIXME: This is necessary, so that acpi_eject_store() doesn't return
  52. * -ENODEV for containers.
  53. */
  54. return 1;
  55. }
  56. static struct acpi_scan_handler container_device_handler = {
  57. .ids = container_device_ids,
  58. .attach = container_device_attach,
  59. };
  60. static int is_device_present(acpi_handle handle)
  61. {
  62. acpi_handle temp;
  63. acpi_status status;
  64. unsigned long long sta;
  65. status = acpi_get_handle(handle, "_STA", &temp);
  66. if (ACPI_FAILURE(status))
  67. return 1; /* _STA not found, assume device present */
  68. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  69. if (ACPI_FAILURE(status))
  70. return 0; /* Firmware error */
  71. return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
  72. }
  73. static void container_notify_cb(acpi_handle handle, u32 type, void *context)
  74. {
  75. struct acpi_device *device = NULL;
  76. int result;
  77. int present;
  78. acpi_status status;
  79. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  80. switch (type) {
  81. case ACPI_NOTIFY_BUS_CHECK:
  82. /* Fall through */
  83. case ACPI_NOTIFY_DEVICE_CHECK:
  84. pr_debug("Container driver received %s event\n",
  85. (type == ACPI_NOTIFY_BUS_CHECK) ?
  86. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK");
  87. present = is_device_present(handle);
  88. status = acpi_bus_get_device(handle, &device);
  89. if (!present) {
  90. if (ACPI_SUCCESS(status)) {
  91. /* device exist and this is a remove request */
  92. device->flags.eject_pending = 1;
  93. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  94. return;
  95. }
  96. break;
  97. }
  98. if (!ACPI_FAILURE(status) || device)
  99. break;
  100. result = acpi_bus_scan(handle);
  101. if (result) {
  102. acpi_handle_warn(handle, "Failed to add container\n");
  103. break;
  104. }
  105. result = acpi_bus_get_device(handle, &device);
  106. if (result) {
  107. acpi_handle_warn(handle, "Missing device object\n");
  108. break;
  109. }
  110. kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
  111. ost_code = ACPI_OST_SC_SUCCESS;
  112. break;
  113. case ACPI_NOTIFY_EJECT_REQUEST:
  114. if (!acpi_bus_get_device(handle, &device) && device) {
  115. device->flags.eject_pending = 1;
  116. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  117. return;
  118. }
  119. break;
  120. default:
  121. /* non-hotplug event; possibly handled by other handler */
  122. return;
  123. }
  124. /* Inform firmware that the hotplug operation has completed */
  125. (void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  126. return;
  127. }
  128. static bool is_container(acpi_handle handle)
  129. {
  130. struct acpi_device_info *info;
  131. bool ret = false;
  132. if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
  133. return false;
  134. if (info->valid & ACPI_VALID_HID) {
  135. const struct acpi_device_id *id;
  136. for (id = container_device_ids; id->id[0]; id++) {
  137. ret = !strcmp((char *)id->id, info->hardware_id.string);
  138. if (ret)
  139. break;
  140. }
  141. }
  142. kfree(info);
  143. return ret;
  144. }
  145. static acpi_status acpi_container_register_notify_handler(acpi_handle handle,
  146. u32 lvl, void *ctxt,
  147. void **retv)
  148. {
  149. if (is_container(handle))
  150. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  151. container_notify_cb, NULL);
  152. return AE_OK;
  153. }
  154. void __init acpi_container_init(void)
  155. {
  156. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
  157. acpi_container_register_notify_handler, NULL,
  158. NULL, NULL);
  159. acpi_scan_add_handler(&container_device_handler);
  160. }