core.c 7.4 KB

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