container.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/types.h>
  33. #include <linux/acpi.h>
  34. #include <acpi/acpi_bus.h>
  35. #include <acpi/acpi_drivers.h>
  36. #include <acpi/container.h>
  37. #define PREFIX "ACPI: "
  38. #define ACPI_CONTAINER_DEVICE_NAME "ACPI container device"
  39. #define ACPI_CONTAINER_CLASS "container"
  40. #define INSTALL_NOTIFY_HANDLER 1
  41. #define UNINSTALL_NOTIFY_HANDLER 2
  42. #define _COMPONENT ACPI_CONTAINER_COMPONENT
  43. ACPI_MODULE_NAME("container");
  44. MODULE_AUTHOR("Anil S Keshavamurthy");
  45. MODULE_DESCRIPTION("ACPI container driver");
  46. MODULE_LICENSE("GPL");
  47. static int acpi_container_add(struct acpi_device *device);
  48. static int acpi_container_remove(struct acpi_device *device, int type);
  49. static const struct acpi_device_id container_device_ids[] = {
  50. {"ACPI0004", 0},
  51. {"PNP0A05", 0},
  52. {"PNP0A06", 0},
  53. {"", 0},
  54. };
  55. MODULE_DEVICE_TABLE(acpi, container_device_ids);
  56. static struct acpi_driver acpi_container_driver = {
  57. .name = "container",
  58. .class = ACPI_CONTAINER_CLASS,
  59. .ids = container_device_ids,
  60. .ops = {
  61. .add = acpi_container_add,
  62. .remove = acpi_container_remove,
  63. },
  64. };
  65. /*******************************************************************/
  66. static int is_device_present(acpi_handle handle)
  67. {
  68. acpi_handle temp;
  69. acpi_status status;
  70. unsigned long long sta;
  71. status = acpi_get_handle(handle, "_STA", &temp);
  72. if (ACPI_FAILURE(status))
  73. return 1; /* _STA not found, assume device present */
  74. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  75. if (ACPI_FAILURE(status))
  76. return 0; /* Firmware error */
  77. return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
  78. }
  79. /*******************************************************************/
  80. static int acpi_container_add(struct acpi_device *device)
  81. {
  82. struct acpi_container *container;
  83. if (!device) {
  84. printk(KERN_ERR PREFIX "device is NULL\n");
  85. return -EINVAL;
  86. }
  87. container = kzalloc(sizeof(struct acpi_container), GFP_KERNEL);
  88. if (!container)
  89. return -ENOMEM;
  90. container->handle = device->handle;
  91. strcpy(acpi_device_name(device), ACPI_CONTAINER_DEVICE_NAME);
  92. strcpy(acpi_device_class(device), ACPI_CONTAINER_CLASS);
  93. device->driver_data = container;
  94. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device <%s> bid <%s>\n",
  95. acpi_device_name(device), acpi_device_bid(device)));
  96. return 0;
  97. }
  98. static int acpi_container_remove(struct acpi_device *device, int type)
  99. {
  100. acpi_status status = AE_OK;
  101. struct acpi_container *pc = NULL;
  102. pc = acpi_driver_data(device);
  103. kfree(pc);
  104. return status;
  105. }
  106. static int container_device_add(struct acpi_device **device, acpi_handle handle)
  107. {
  108. acpi_handle phandle;
  109. struct acpi_device *pdev;
  110. int result;
  111. if (acpi_get_parent(handle, &phandle)) {
  112. return -ENODEV;
  113. }
  114. if (acpi_bus_get_device(phandle, &pdev)) {
  115. return -ENODEV;
  116. }
  117. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_DEVICE)) {
  118. return -ENODEV;
  119. }
  120. result = acpi_bus_start(*device);
  121. return result;
  122. }
  123. static void container_notify_cb(acpi_handle handle, u32 type, void *context)
  124. {
  125. struct acpi_device *device = NULL;
  126. int result;
  127. int present;
  128. acpi_status status;
  129. present = is_device_present(handle);
  130. switch (type) {
  131. case ACPI_NOTIFY_BUS_CHECK:
  132. /* Fall through */
  133. case ACPI_NOTIFY_DEVICE_CHECK:
  134. printk(KERN_WARNING "Container driver received %s event\n",
  135. (type == ACPI_NOTIFY_BUS_CHECK) ?
  136. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK");
  137. status = acpi_bus_get_device(handle, &device);
  138. if (present) {
  139. if (ACPI_FAILURE(status) || !device) {
  140. result = container_device_add(&device, handle);
  141. if (!result)
  142. kobject_uevent(&device->dev.kobj,
  143. KOBJ_ONLINE);
  144. else
  145. printk(KERN_WARNING
  146. "Failed to add container\n");
  147. }
  148. } else {
  149. if (ACPI_SUCCESS(status)) {
  150. /* device exist and this is a remove request */
  151. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  152. }
  153. }
  154. break;
  155. case ACPI_NOTIFY_EJECT_REQUEST:
  156. if (!acpi_bus_get_device(handle, &device) && device) {
  157. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. return;
  164. }
  165. static acpi_status
  166. container_walk_namespace_cb(acpi_handle handle,
  167. u32 lvl, void *context, void **rv)
  168. {
  169. char *hid = NULL;
  170. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  171. struct acpi_device_info *info;
  172. acpi_status status;
  173. int *action = context;
  174. status = acpi_get_object_info(handle, &buffer);
  175. if (ACPI_FAILURE(status) || !buffer.pointer) {
  176. return AE_OK;
  177. }
  178. info = buffer.pointer;
  179. if (info->valid & ACPI_VALID_HID)
  180. hid = info->hardware_id.value;
  181. if (hid == NULL) {
  182. goto end;
  183. }
  184. if (strcmp(hid, "ACPI0004") && strcmp(hid, "PNP0A05") &&
  185. strcmp(hid, "PNP0A06")) {
  186. goto end;
  187. }
  188. switch (*action) {
  189. case INSTALL_NOTIFY_HANDLER:
  190. acpi_install_notify_handler(handle,
  191. ACPI_SYSTEM_NOTIFY,
  192. container_notify_cb, NULL);
  193. break;
  194. case UNINSTALL_NOTIFY_HANDLER:
  195. acpi_remove_notify_handler(handle,
  196. ACPI_SYSTEM_NOTIFY,
  197. container_notify_cb);
  198. break;
  199. default:
  200. break;
  201. }
  202. end:
  203. kfree(buffer.pointer);
  204. return AE_OK;
  205. }
  206. static int __init acpi_container_init(void)
  207. {
  208. int result = 0;
  209. int action = INSTALL_NOTIFY_HANDLER;
  210. result = acpi_bus_register_driver(&acpi_container_driver);
  211. if (result < 0) {
  212. return (result);
  213. }
  214. /* register notify handler to every container device */
  215. acpi_walk_namespace(ACPI_TYPE_DEVICE,
  216. ACPI_ROOT_OBJECT,
  217. ACPI_UINT32_MAX,
  218. container_walk_namespace_cb, &action, NULL);
  219. return (0);
  220. }
  221. static void __exit acpi_container_exit(void)
  222. {
  223. int action = UNINSTALL_NOTIFY_HANDLER;
  224. acpi_walk_namespace(ACPI_TYPE_DEVICE,
  225. ACPI_ROOT_OBJECT,
  226. ACPI_UINT32_MAX,
  227. container_walk_namespace_cb, &action, NULL);
  228. acpi_bus_unregister_driver(&acpi_container_driver);
  229. return;
  230. }
  231. module_init(acpi_container_init);
  232. module_exit(acpi_container_exit);