container.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define PREFIX "ACPI: "
  38. #define _COMPONENT ACPI_CONTAINER_COMPONENT
  39. ACPI_MODULE_NAME("container");
  40. static const struct acpi_device_id container_device_ids[] = {
  41. {"ACPI0004", 0},
  42. {"PNP0A05", 0},
  43. {"PNP0A06", 0},
  44. {"", 0},
  45. };
  46. static int container_device_attach(struct acpi_device *device,
  47. const struct acpi_device_id *not_used)
  48. {
  49. /*
  50. * FIXME: This is necessary, so that acpi_eject_store() doesn't return
  51. * -ENODEV for containers.
  52. */
  53. return 1;
  54. }
  55. static struct acpi_scan_handler container_device_handler = {
  56. .ids = container_device_ids,
  57. .attach = container_device_attach,
  58. };
  59. static int is_device_present(acpi_handle handle)
  60. {
  61. acpi_handle temp;
  62. acpi_status status;
  63. unsigned long long sta;
  64. status = acpi_get_handle(handle, "_STA", &temp);
  65. if (ACPI_FAILURE(status))
  66. return 1; /* _STA not found, assume device present */
  67. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  68. if (ACPI_FAILURE(status))
  69. return 0; /* Firmware error */
  70. return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
  71. }
  72. static void container_notify_cb(acpi_handle handle, u32 type, void *context)
  73. {
  74. struct acpi_device *device = NULL;
  75. int result;
  76. int present;
  77. acpi_status status;
  78. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  79. acpi_scan_lock_acquire();
  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. goto out;
  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. goto out;
  118. }
  119. break;
  120. default:
  121. /* non-hotplug event; possibly handled by other handler */
  122. goto out;
  123. }
  124. /* Inform firmware that the hotplug operation has completed */
  125. (void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  126. out:
  127. acpi_scan_lock_release();
  128. }
  129. static bool is_container(acpi_handle handle)
  130. {
  131. struct acpi_device_info *info;
  132. bool ret = false;
  133. if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
  134. return false;
  135. if (info->valid & ACPI_VALID_HID) {
  136. const struct acpi_device_id *id;
  137. for (id = container_device_ids; id->id[0]; id++) {
  138. ret = !strcmp((char *)id->id, info->hardware_id.string);
  139. if (ret)
  140. break;
  141. }
  142. }
  143. kfree(info);
  144. return ret;
  145. }
  146. static acpi_status acpi_container_register_notify_handler(acpi_handle handle,
  147. u32 lvl, void *ctxt,
  148. void **retv)
  149. {
  150. if (is_container(handle))
  151. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  152. container_notify_cb, NULL);
  153. return AE_OK;
  154. }
  155. void __init acpi_container_init(void)
  156. {
  157. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
  158. acpi_container_register_notify_handler, NULL,
  159. NULL, NULL);
  160. acpi_scan_add_handler(&container_device_handler);
  161. }