core.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * pnpacpi -- PnP ACPI driver
  3. *
  4. * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
  5. * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/export.h>
  22. #include <linux/acpi.h>
  23. #include <linux/pnp.h>
  24. #include <linux/slab.h>
  25. #include <linux/mod_devicetable.h>
  26. #include "../base.h"
  27. #include "pnpacpi.h"
  28. static int num;
  29. /*
  30. * Compatible Device IDs
  31. */
  32. #define TEST_HEX(c) \
  33. if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
  34. return 0
  35. #define TEST_ALPHA(c) \
  36. if (!('A' <= (c) && (c) <= 'Z')) \
  37. return 0
  38. static int __init ispnpidacpi(const char *id)
  39. {
  40. TEST_ALPHA(id[0]);
  41. TEST_ALPHA(id[1]);
  42. TEST_ALPHA(id[2]);
  43. TEST_HEX(id[3]);
  44. TEST_HEX(id[4]);
  45. TEST_HEX(id[5]);
  46. TEST_HEX(id[6]);
  47. if (id[7] != '\0')
  48. return 0;
  49. return 1;
  50. }
  51. static int pnpacpi_get_resources(struct pnp_dev *dev)
  52. {
  53. pnp_dbg(&dev->dev, "get resources\n");
  54. return pnpacpi_parse_allocated_resource(dev);
  55. }
  56. static int pnpacpi_set_resources(struct pnp_dev *dev)
  57. {
  58. struct acpi_device *acpi_dev;
  59. acpi_handle handle;
  60. int ret = 0;
  61. pnp_dbg(&dev->dev, "set resources\n");
  62. acpi_dev = ACPI_COMPANION(&dev->dev);
  63. if (!acpi_dev) {
  64. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  65. return -ENODEV;
  66. }
  67. if (WARN_ON_ONCE(acpi_dev != dev->data))
  68. dev->data = acpi_dev;
  69. handle = acpi_dev->handle;
  70. if (acpi_has_method(handle, METHOD_NAME__SRS)) {
  71. struct acpi_buffer buffer;
  72. ret = pnpacpi_build_resource_template(dev, &buffer);
  73. if (ret)
  74. return ret;
  75. ret = pnpacpi_encode_resources(dev, &buffer);
  76. if (!ret) {
  77. acpi_status status;
  78. status = acpi_set_current_resources(handle, &buffer);
  79. if (ACPI_FAILURE(status))
  80. ret = -EIO;
  81. }
  82. kfree(buffer.pointer);
  83. }
  84. if (!ret && acpi_device_power_manageable(acpi_dev))
  85. ret = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
  86. return ret;
  87. }
  88. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  89. {
  90. struct acpi_device *acpi_dev;
  91. acpi_status status;
  92. dev_dbg(&dev->dev, "disable resources\n");
  93. acpi_dev = ACPI_COMPANION(&dev->dev);
  94. if (!acpi_dev) {
  95. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  96. return 0;
  97. }
  98. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  99. if (acpi_device_power_manageable(acpi_dev))
  100. acpi_device_set_power(acpi_dev, ACPI_STATE_D3_COLD);
  101. /* continue even if acpi_device_set_power() fails */
  102. status = acpi_evaluate_object(acpi_dev->handle, "_DIS", NULL, NULL);
  103. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  104. return -ENODEV;
  105. return 0;
  106. }
  107. #ifdef CONFIG_ACPI_SLEEP
  108. static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
  109. {
  110. struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
  111. if (!acpi_dev) {
  112. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  113. return false;
  114. }
  115. return acpi_bus_can_wakeup(acpi_dev->handle);
  116. }
  117. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  118. {
  119. struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
  120. int error = 0;
  121. if (!acpi_dev) {
  122. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  123. return 0;
  124. }
  125. if (device_can_wakeup(&dev->dev)) {
  126. error = acpi_pm_device_sleep_wake(&dev->dev,
  127. device_may_wakeup(&dev->dev));
  128. if (error)
  129. return error;
  130. }
  131. if (acpi_device_power_manageable(acpi_dev)) {
  132. int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
  133. ACPI_STATE_D3_COLD);
  134. if (power_state < 0)
  135. power_state = (state.event == PM_EVENT_ON) ?
  136. ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
  137. /*
  138. * acpi_device_set_power() can fail (keyboard port can't be
  139. * powered-down?), and in any case, our return value is ignored
  140. * by pnp_bus_suspend(). Hence we don't revert the wakeup
  141. * setting if the set_power fails.
  142. */
  143. error = acpi_device_set_power(acpi_dev, power_state);
  144. }
  145. return error;
  146. }
  147. static int pnpacpi_resume(struct pnp_dev *dev)
  148. {
  149. struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
  150. int error = 0;
  151. if (!acpi_dev) {
  152. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  153. return -ENODEV;
  154. }
  155. if (device_may_wakeup(&dev->dev))
  156. acpi_pm_device_sleep_wake(&dev->dev, false);
  157. if (acpi_device_power_manageable(acpi_dev))
  158. error = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
  159. return error;
  160. }
  161. #endif
  162. struct pnp_protocol pnpacpi_protocol = {
  163. .name = "Plug and Play ACPI",
  164. .get = pnpacpi_get_resources,
  165. .set = pnpacpi_set_resources,
  166. .disable = pnpacpi_disable_resources,
  167. #ifdef CONFIG_ACPI_SLEEP
  168. .can_wakeup = pnpacpi_can_wakeup,
  169. .suspend = pnpacpi_suspend,
  170. .resume = pnpacpi_resume,
  171. #endif
  172. };
  173. EXPORT_SYMBOL(pnpacpi_protocol);
  174. static const char *__init pnpacpi_get_id(struct acpi_device *device)
  175. {
  176. struct acpi_hardware_id *id;
  177. list_for_each_entry(id, &device->pnp.ids, list) {
  178. if (ispnpidacpi(id->id))
  179. return id->id;
  180. }
  181. return NULL;
  182. }
  183. static int __init pnpacpi_add_device(struct acpi_device *device)
  184. {
  185. struct pnp_dev *dev;
  186. const char *pnpid;
  187. struct acpi_hardware_id *id;
  188. int error;
  189. /* Skip devices that are already bound */
  190. if (device->physical_node_count)
  191. return 0;
  192. /*
  193. * If a PnPacpi device is not present , the device
  194. * driver should not be loaded.
  195. */
  196. if (!acpi_has_method(device->handle, "_CRS"))
  197. return 0;
  198. pnpid = pnpacpi_get_id(device);
  199. if (!pnpid)
  200. return 0;
  201. if (!device->status.present)
  202. return 0;
  203. dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
  204. if (!dev)
  205. return -ENOMEM;
  206. ACPI_COMPANION_SET(&dev->dev, device);
  207. dev->data = device;
  208. /* .enabled means the device can decode the resources */
  209. dev->active = device->status.enabled;
  210. if (acpi_has_method(device->handle, "_SRS"))
  211. dev->capabilities |= PNP_CONFIGURABLE;
  212. dev->capabilities |= PNP_READ;
  213. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  214. dev->capabilities |= PNP_WRITE;
  215. if (device->flags.removable)
  216. dev->capabilities |= PNP_REMOVABLE;
  217. if (acpi_has_method(device->handle, "_DIS"))
  218. dev->capabilities |= PNP_DISABLE;
  219. if (strlen(acpi_device_name(device)))
  220. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  221. else
  222. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  223. if (dev->active)
  224. pnpacpi_parse_allocated_resource(dev);
  225. if (dev->capabilities & PNP_CONFIGURABLE)
  226. pnpacpi_parse_resource_option_data(dev);
  227. list_for_each_entry(id, &device->pnp.ids, list) {
  228. if (!strcmp(id->id, pnpid))
  229. continue;
  230. if (!ispnpidacpi(id->id))
  231. continue;
  232. pnp_add_id(dev, id->id);
  233. }
  234. /* clear out the damaged flags */
  235. if (!dev->active)
  236. pnp_init_resources(dev);
  237. error = pnp_add_device(dev);
  238. if (error) {
  239. put_device(&dev->dev);
  240. return error;
  241. }
  242. num++;
  243. return 0;
  244. }
  245. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  246. u32 lvl, void *context,
  247. void **rv)
  248. {
  249. struct acpi_device *device;
  250. if (acpi_bus_get_device(handle, &device))
  251. return AE_CTRL_DEPTH;
  252. if (acpi_is_pnp_device(device))
  253. pnpacpi_add_device(device);
  254. return AE_OK;
  255. }
  256. int pnpacpi_disabled __initdata;
  257. static int __init pnpacpi_init(void)
  258. {
  259. if (acpi_disabled || pnpacpi_disabled) {
  260. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  261. return 0;
  262. }
  263. printk(KERN_INFO "pnp: PnP ACPI init\n");
  264. pnp_register_protocol(&pnpacpi_protocol);
  265. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  266. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  267. pnp_platform_devices = 1;
  268. return 0;
  269. }
  270. fs_initcall(pnpacpi_init);
  271. static int __init pnpacpi_setup(char *str)
  272. {
  273. if (str == NULL)
  274. return 1;
  275. if (!strncmp(str, "off", 3))
  276. pnpacpi_disabled = 1;
  277. return 1;
  278. }
  279. __setup("pnpacpi=", pnpacpi_setup);