container.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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
  62. is_device_present(acpi_handle handle)
  63. {
  64. acpi_handle temp;
  65. acpi_status status;
  66. unsigned long sta;
  67. ACPI_FUNCTION_TRACE("is_device_present");
  68. status = acpi_get_handle(handle, "_STA", &temp);
  69. if (ACPI_FAILURE(status))
  70. return_VALUE(1); /* _STA not found, assmue device present */
  71. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  72. if (ACPI_FAILURE(status))
  73. return_VALUE(0); /* Firmware error */
  74. return_VALUE((sta & ACPI_STA_PRESENT) == ACPI_STA_PRESENT);
  75. }
  76. /*******************************************************************/
  77. static int
  78. acpi_container_add(struct acpi_device *device)
  79. {
  80. struct acpi_container *container;
  81. ACPI_FUNCTION_TRACE("acpi_container_add");
  82. if (!device) {
  83. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "device is NULL\n"));
  84. return_VALUE(-EINVAL);
  85. }
  86. container = kmalloc(sizeof(struct acpi_container), GFP_KERNEL);
  87. if(!container)
  88. return_VALUE(-ENOMEM);
  89. memset(container, 0, sizeof(struct acpi_container));
  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. acpi_driver_data(device) = container;
  94. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device <%s> bid <%s>\n", \
  95. acpi_device_name(device), acpi_device_bid(device)));
  96. return_VALUE(0);
  97. }
  98. static int
  99. acpi_container_remove(struct acpi_device *device, int type)
  100. {
  101. acpi_status status = AE_OK;
  102. struct acpi_container *pc = NULL;
  103. pc = (struct acpi_container*) acpi_driver_data(device);
  104. if (pc)
  105. kfree(pc);
  106. return status;
  107. }
  108. static int
  109. container_device_add(struct acpi_device **device, acpi_handle handle)
  110. {
  111. acpi_handle phandle;
  112. struct acpi_device *pdev;
  113. int result;
  114. ACPI_FUNCTION_TRACE("container_device_add");
  115. if (acpi_get_parent(handle, &phandle)) {
  116. return_VALUE(-ENODEV);
  117. }
  118. if (acpi_bus_get_device(phandle, &pdev)) {
  119. return_VALUE(-ENODEV);
  120. }
  121. if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_DEVICE)) {
  122. return_VALUE(-ENODEV);
  123. }
  124. result = acpi_bus_start(*device);
  125. return_VALUE(result);
  126. }
  127. static void
  128. container_notify_cb(acpi_handle handle, u32 type, void *context)
  129. {
  130. struct acpi_device *device = NULL;
  131. int result;
  132. int present;
  133. acpi_status status;
  134. ACPI_FUNCTION_TRACE("container_notify_cb");
  135. present = is_device_present(handle);
  136. switch (type) {
  137. case ACPI_NOTIFY_BUS_CHECK:
  138. /* Fall through */
  139. case ACPI_NOTIFY_DEVICE_CHECK:
  140. printk("Container driver received %s event\n",
  141. (type == ACPI_NOTIFY_BUS_CHECK)?
  142. "ACPI_NOTIFY_BUS_CHECK":"ACPI_NOTIFY_DEVICE_CHECK");
  143. status = acpi_bus_get_device(handle, &device);
  144. if (present) {
  145. if (ACPI_FAILURE(status) || !device) {
  146. result = container_device_add(&device, handle);
  147. if (!result)
  148. kobject_hotplug(&device->kobj,
  149. KOBJ_ONLINE);
  150. else
  151. printk("Failed to add container\n");
  152. }
  153. } else {
  154. if (ACPI_SUCCESS(status)) {
  155. /* device exist and this is a remove request */
  156. kobject_hotplug(&device->kobj, KOBJ_OFFLINE);
  157. }
  158. }
  159. break;
  160. case ACPI_NOTIFY_EJECT_REQUEST:
  161. if (!acpi_bus_get_device(handle, &device) && device) {
  162. kobject_hotplug(&device->kobj, KOBJ_OFFLINE);
  163. }
  164. break;
  165. default:
  166. break;
  167. }
  168. return_VOID;
  169. }
  170. static acpi_status
  171. container_walk_namespace_cb(acpi_handle handle,
  172. u32 lvl,
  173. void *context,
  174. void **rv)
  175. {
  176. char *hid = NULL;
  177. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  178. struct acpi_device_info *info;
  179. acpi_status status;
  180. int *action = context;
  181. ACPI_FUNCTION_TRACE("container_walk_namespace_cb");
  182. status = acpi_get_object_info(handle, &buffer);
  183. if (ACPI_FAILURE(status) || !buffer.pointer) {
  184. return_ACPI_STATUS(AE_OK);
  185. }
  186. info = buffer.pointer;
  187. if (info->valid & ACPI_VALID_HID)
  188. hid = info->hardware_id.value;
  189. if (hid == NULL) {
  190. goto end;
  191. }
  192. if (strcmp(hid, "ACPI0004") && strcmp(hid, "PNP0A05") &&
  193. strcmp(hid, "PNP0A06")) {
  194. goto end;
  195. }
  196. switch(*action) {
  197. case INSTALL_NOTIFY_HANDLER:
  198. acpi_install_notify_handler(handle,
  199. ACPI_SYSTEM_NOTIFY,
  200. container_notify_cb,
  201. NULL);
  202. break;
  203. case UNINSTALL_NOTIFY_HANDLER:
  204. acpi_remove_notify_handler(handle,
  205. ACPI_SYSTEM_NOTIFY,
  206. container_notify_cb);
  207. break;
  208. default:
  209. break;
  210. }
  211. end:
  212. acpi_os_free(buffer.pointer);
  213. return_ACPI_STATUS(AE_OK);
  214. }
  215. static int __init
  216. acpi_container_init(void)
  217. {
  218. int result = 0;
  219. int action = INSTALL_NOTIFY_HANDLER;
  220. result = acpi_bus_register_driver(&acpi_container_driver);
  221. if (result < 0) {
  222. return(result);
  223. }
  224. /* register notify handler to every container device */
  225. acpi_walk_namespace(ACPI_TYPE_DEVICE,
  226. ACPI_ROOT_OBJECT,
  227. ACPI_UINT32_MAX,
  228. container_walk_namespace_cb,
  229. &action, NULL);
  230. return(0);
  231. }
  232. static void __exit
  233. acpi_container_exit(void)
  234. {
  235. int action = UNINSTALL_NOTIFY_HANDLER;
  236. ACPI_FUNCTION_TRACE("acpi_container_exit");
  237. acpi_walk_namespace(ACPI_TYPE_DEVICE,
  238. ACPI_ROOT_OBJECT,
  239. ACPI_UINT32_MAX,
  240. container_walk_namespace_cb,
  241. &action, NULL);
  242. acpi_bus_unregister_driver(&acpi_container_driver);
  243. return_VOID;
  244. }
  245. module_init(acpi_container_init);
  246. module_exit(acpi_container_exit);