core.c 10.0 KB

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