core.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/acpi.h>
  22. #include <linux/pnp.h>
  23. #include <linux/slab.h>
  24. #include <linux/mod_devicetable.h>
  25. #include <acpi/acpi_bus.h>
  26. #include "../base.h"
  27. #include "pnpacpi.h"
  28. static int num;
  29. /* We need only to blacklist devices that have already an acpi driver that
  30. * can't use pnp layer. We don't need to blacklist device that are directly
  31. * used by the kernel (PCI root, ...), as it is harmless and there were
  32. * already present in pnpbios. But there is an exception for devices that
  33. * have irqs (PIC, Timer) because we call acpi_register_gsi.
  34. * Finally, only devices that have a CRS method need to be in this list.
  35. */
  36. static struct acpi_device_id excluded_id_list[] __initdata = {
  37. {"PNP0C09", 0}, /* EC */
  38. {"PNP0C0F", 0}, /* Link device */
  39. {"PNP0000", 0}, /* PIC */
  40. {"PNP0100", 0}, /* Timer */
  41. {"", 0},
  42. };
  43. static inline int __init is_exclusive_device(struct acpi_device *dev)
  44. {
  45. return (!acpi_match_device_ids(dev, excluded_id_list));
  46. }
  47. /*
  48. * Compatible Device IDs
  49. */
  50. #define TEST_HEX(c) \
  51. if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
  52. return 0
  53. #define TEST_ALPHA(c) \
  54. if (!('@' <= (c) || (c) <= 'Z')) \
  55. return 0
  56. static int __init ispnpidacpi(const char *id)
  57. {
  58. TEST_ALPHA(id[0]);
  59. TEST_ALPHA(id[1]);
  60. TEST_ALPHA(id[2]);
  61. TEST_HEX(id[3]);
  62. TEST_HEX(id[4]);
  63. TEST_HEX(id[5]);
  64. TEST_HEX(id[6]);
  65. if (id[7] != '\0')
  66. return 0;
  67. return 1;
  68. }
  69. static int pnpacpi_get_resources(struct pnp_dev *dev)
  70. {
  71. pnp_dbg(&dev->dev, "get resources\n");
  72. return pnpacpi_parse_allocated_resource(dev);
  73. }
  74. static int pnpacpi_set_resources(struct pnp_dev *dev)
  75. {
  76. struct acpi_device *acpi_dev = dev->data;
  77. acpi_handle handle = acpi_dev->handle;
  78. struct acpi_buffer buffer;
  79. int ret;
  80. pnp_dbg(&dev->dev, "set resources\n");
  81. ret = pnpacpi_build_resource_template(dev, &buffer);
  82. if (ret)
  83. return ret;
  84. ret = pnpacpi_encode_resources(dev, &buffer);
  85. if (ret) {
  86. kfree(buffer.pointer);
  87. return ret;
  88. }
  89. if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
  90. ret = -EINVAL;
  91. else if (acpi_bus_power_manageable(handle))
  92. ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
  93. kfree(buffer.pointer);
  94. return ret;
  95. }
  96. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  97. {
  98. struct acpi_device *acpi_dev = dev->data;
  99. acpi_handle handle = acpi_dev->handle;
  100. int ret;
  101. dev_dbg(&dev->dev, "disable resources\n");
  102. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  103. ret = 0;
  104. if (acpi_bus_power_manageable(handle))
  105. acpi_bus_set_power(handle, ACPI_STATE_D3);
  106. /* continue even if acpi_bus_set_power() fails */
  107. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
  108. ret = -ENODEV;
  109. return ret;
  110. }
  111. #ifdef CONFIG_ACPI_SLEEP
  112. static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
  113. {
  114. struct acpi_device *acpi_dev = dev->data;
  115. acpi_handle handle = acpi_dev->handle;
  116. return acpi_bus_can_wakeup(handle);
  117. }
  118. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  119. {
  120. struct acpi_device *acpi_dev = dev->data;
  121. acpi_handle handle = acpi_dev->handle;
  122. int power_state;
  123. if (device_can_wakeup(&dev->dev)) {
  124. int rc = acpi_pm_device_sleep_wake(&dev->dev,
  125. device_may_wakeup(&dev->dev));
  126. if (rc)
  127. return rc;
  128. }
  129. power_state = acpi_pm_device_sleep_state(&dev->dev, NULL);
  130. if (power_state < 0)
  131. power_state = (state.event == PM_EVENT_ON) ?
  132. ACPI_STATE_D0 : ACPI_STATE_D3;
  133. /* acpi_bus_set_power() often fails (keyboard port can't be
  134. * powered-down?), and in any case, our return value is ignored
  135. * by pnp_bus_suspend(). Hence we don't revert the wakeup
  136. * setting if the set_power fails.
  137. */
  138. return acpi_bus_set_power(handle, power_state);
  139. }
  140. static int pnpacpi_resume(struct pnp_dev *dev)
  141. {
  142. struct acpi_device *acpi_dev = dev->data;
  143. acpi_handle handle = acpi_dev->handle;
  144. if (device_may_wakeup(&dev->dev))
  145. acpi_pm_device_sleep_wake(&dev->dev, false);
  146. return acpi_bus_set_power(handle, ACPI_STATE_D0);
  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 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. acpi_handle temp = NULL;
  173. acpi_status status;
  174. struct pnp_dev *dev;
  175. char *pnpid;
  176. struct acpi_hardware_id *id;
  177. /*
  178. * If a PnPacpi device is not present , the device
  179. * driver should not be loaded.
  180. */
  181. status = acpi_get_handle(device->handle, "_CRS", &temp);
  182. if (ACPI_FAILURE(status))
  183. return 0;
  184. pnpid = pnpacpi_get_id(device);
  185. if (!pnpid)
  186. return 0;
  187. if (is_exclusive_device(device) || !device->status.present)
  188. return 0;
  189. dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
  190. if (!dev)
  191. return -ENOMEM;
  192. dev->data = device;
  193. /* .enabled means the device can decode the resources */
  194. dev->active = device->status.enabled;
  195. status = acpi_get_handle(device->handle, "_SRS", &temp);
  196. if (ACPI_SUCCESS(status))
  197. dev->capabilities |= PNP_CONFIGURABLE;
  198. dev->capabilities |= PNP_READ;
  199. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  200. dev->capabilities |= PNP_WRITE;
  201. if (device->flags.removable)
  202. dev->capabilities |= PNP_REMOVABLE;
  203. status = acpi_get_handle(device->handle, "_DIS", &temp);
  204. if (ACPI_SUCCESS(status))
  205. dev->capabilities |= PNP_DISABLE;
  206. if (strlen(acpi_device_name(device)))
  207. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  208. else
  209. strncpy(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. pnp_add_device(dev);
  225. num++;
  226. return AE_OK;
  227. }
  228. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  229. u32 lvl, void *context,
  230. void **rv)
  231. {
  232. struct acpi_device *device;
  233. if (!acpi_bus_get_device(handle, &device))
  234. pnpacpi_add_device(device);
  235. else
  236. return AE_CTRL_DEPTH;
  237. return AE_OK;
  238. }
  239. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  240. {
  241. struct acpi_device *acpi = to_acpi_device(dev);
  242. struct pnp_dev *pnp = _pnp;
  243. /* true means it matched */
  244. return !acpi_get_physical_device(acpi->handle)
  245. && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
  246. }
  247. static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
  248. {
  249. struct device *adev;
  250. struct acpi_device *acpi;
  251. adev = bus_find_device(&acpi_bus_type, NULL,
  252. to_pnp_dev(dev), acpi_pnp_match);
  253. if (!adev)
  254. return -ENODEV;
  255. acpi = to_acpi_device(adev);
  256. *handle = acpi->handle;
  257. put_device(adev);
  258. return 0;
  259. }
  260. /* complete initialization of a PNPACPI device includes having
  261. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  262. */
  263. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  264. .bus = &pnp_bus_type,
  265. .find_device = acpi_pnp_find_device,
  266. };
  267. int pnpacpi_disabled __initdata;
  268. static int __init pnpacpi_init(void)
  269. {
  270. if (acpi_disabled || pnpacpi_disabled) {
  271. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  272. return 0;
  273. }
  274. printk(KERN_INFO "pnp: PnP ACPI init\n");
  275. pnp_register_protocol(&pnpacpi_protocol);
  276. register_acpi_bus_type(&acpi_pnp_bus);
  277. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  278. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  279. unregister_acpi_bus_type(&acpi_pnp_bus);
  280. pnp_platform_devices = 1;
  281. return 0;
  282. }
  283. fs_initcall(pnpacpi_init);
  284. static int __init pnpacpi_setup(char *str)
  285. {
  286. if (str == NULL)
  287. return 1;
  288. if (!strncmp(str, "off", 3))
  289. pnpacpi_disabled = 1;
  290. return 1;
  291. }
  292. __setup("pnpacpi=", pnpacpi_setup);