container.c 7.3 KB

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