core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_FAILURE(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_FAILURE(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_FAILURE(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_FAILURE(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. if (power_state < 0)
  155. power_state = (state.event == PM_EVENT_ON) ?
  156. ACPI_STATE_D0 : ACPI_STATE_D3;
  157. /*
  158. * acpi_bus_set_power() often fails (keyboard port can't be
  159. * powered-down?), and in any case, our return value is ignored
  160. * by pnp_bus_suspend(). Hence we don't revert the wakeup
  161. * setting if the set_power fails.
  162. */
  163. error = acpi_bus_set_power(handle, power_state);
  164. }
  165. return error;
  166. }
  167. static int pnpacpi_resume(struct pnp_dev *dev)
  168. {
  169. struct acpi_device *acpi_dev;
  170. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  171. int error = 0;
  172. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  173. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  174. return -ENODEV;
  175. }
  176. if (device_may_wakeup(&dev->dev))
  177. acpi_pm_device_sleep_wake(&dev->dev, false);
  178. if (acpi_bus_power_manageable(handle))
  179. error = acpi_bus_set_power(handle, ACPI_STATE_D0);
  180. return error;
  181. }
  182. #endif
  183. struct pnp_protocol pnpacpi_protocol = {
  184. .name = "Plug and Play ACPI",
  185. .get = pnpacpi_get_resources,
  186. .set = pnpacpi_set_resources,
  187. .disable = pnpacpi_disable_resources,
  188. #ifdef CONFIG_ACPI_SLEEP
  189. .can_wakeup = pnpacpi_can_wakeup,
  190. .suspend = pnpacpi_suspend,
  191. .resume = pnpacpi_resume,
  192. #endif
  193. };
  194. EXPORT_SYMBOL(pnpacpi_protocol);
  195. static char *__init pnpacpi_get_id(struct acpi_device *device)
  196. {
  197. struct acpi_hardware_id *id;
  198. list_for_each_entry(id, &device->pnp.ids, list) {
  199. if (ispnpidacpi(id->id))
  200. return id->id;
  201. }
  202. return NULL;
  203. }
  204. static int __init pnpacpi_add_device(struct acpi_device *device)
  205. {
  206. acpi_handle temp = NULL;
  207. acpi_status status;
  208. struct pnp_dev *dev;
  209. char *pnpid;
  210. struct acpi_hardware_id *id;
  211. /*
  212. * If a PnPacpi device is not present , the device
  213. * driver should not be loaded.
  214. */
  215. status = acpi_get_handle(device->handle, "_CRS", &temp);
  216. if (ACPI_FAILURE(status))
  217. return 0;
  218. pnpid = pnpacpi_get_id(device);
  219. if (!pnpid)
  220. return 0;
  221. if (is_exclusive_device(device) || !device->status.present)
  222. return 0;
  223. dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
  224. if (!dev)
  225. return -ENOMEM;
  226. dev->data = device;
  227. /* .enabled means the device can decode the resources */
  228. dev->active = device->status.enabled;
  229. status = acpi_get_handle(device->handle, "_SRS", &temp);
  230. if (ACPI_SUCCESS(status))
  231. dev->capabilities |= PNP_CONFIGURABLE;
  232. dev->capabilities |= PNP_READ;
  233. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  234. dev->capabilities |= PNP_WRITE;
  235. if (device->flags.removable)
  236. dev->capabilities |= PNP_REMOVABLE;
  237. status = acpi_get_handle(device->handle, "_DIS", &temp);
  238. if (ACPI_SUCCESS(status))
  239. dev->capabilities |= PNP_DISABLE;
  240. if (strlen(acpi_device_name(device)))
  241. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  242. else
  243. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  244. if (dev->active)
  245. pnpacpi_parse_allocated_resource(dev);
  246. if (dev->capabilities & PNP_CONFIGURABLE)
  247. pnpacpi_parse_resource_option_data(dev);
  248. list_for_each_entry(id, &device->pnp.ids, list) {
  249. if (!strcmp(id->id, pnpid))
  250. continue;
  251. if (!ispnpidacpi(id->id))
  252. continue;
  253. pnp_add_id(dev, id->id);
  254. }
  255. /* clear out the damaged flags */
  256. if (!dev->active)
  257. pnp_init_resources(dev);
  258. pnp_add_device(dev);
  259. num++;
  260. return AE_OK;
  261. }
  262. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  263. u32 lvl, void *context,
  264. void **rv)
  265. {
  266. struct acpi_device *device;
  267. if (!acpi_bus_get_device(handle, &device))
  268. pnpacpi_add_device(device);
  269. else
  270. return AE_CTRL_DEPTH;
  271. return AE_OK;
  272. }
  273. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  274. {
  275. struct acpi_device *acpi = to_acpi_device(dev);
  276. struct pnp_dev *pnp = _pnp;
  277. struct device *physical_device;
  278. physical_device = acpi_get_physical_device(acpi->handle);
  279. if (physical_device)
  280. put_device(physical_device);
  281. /* true means it matched */
  282. return !physical_device
  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 struct acpi_bus_type __initdata acpi_pnp_bus = {
  302. .bus = &pnp_bus_type,
  303. .find_device = acpi_pnp_find_device,
  304. };
  305. int pnpacpi_disabled __initdata;
  306. static int __init pnpacpi_init(void)
  307. {
  308. if (acpi_disabled || pnpacpi_disabled) {
  309. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  310. return 0;
  311. }
  312. printk(KERN_INFO "pnp: PnP ACPI init\n");
  313. pnp_register_protocol(&pnpacpi_protocol);
  314. register_acpi_bus_type(&acpi_pnp_bus);
  315. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  316. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  317. unregister_acpi_bus_type(&acpi_pnp_bus);
  318. pnp_platform_devices = 1;
  319. return 0;
  320. }
  321. fs_initcall(pnpacpi_init);
  322. static int __init pnpacpi_setup(char *str)
  323. {
  324. if (str == NULL)
  325. return 1;
  326. if (!strncmp(str, "off", 3))
  327. pnpacpi_disabled = 1;
  328. return 1;
  329. }
  330. __setup("pnpacpi=", pnpacpi_setup);