container.c 7.4 KB

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