container.c 6.9 KB

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