core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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;
  77. acpi_handle handle;
  78. struct acpi_buffer buffer;
  79. int ret;
  80. pnp_dbg(&dev->dev, "set resources\n");
  81. handle = DEVICE_ACPI_HANDLE(&dev->dev);
  82. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  83. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  84. return -ENODEV;
  85. }
  86. ret = pnpacpi_build_resource_template(dev, &buffer);
  87. if (ret)
  88. return ret;
  89. ret = pnpacpi_encode_resources(dev, &buffer);
  90. if (ret) {
  91. kfree(buffer.pointer);
  92. return ret;
  93. }
  94. if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
  95. ret = -EINVAL;
  96. else if (acpi_bus_power_manageable(handle))
  97. ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
  98. kfree(buffer.pointer);
  99. return ret;
  100. }
  101. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  102. {
  103. struct acpi_device *acpi_dev;
  104. acpi_handle handle;
  105. int ret;
  106. dev_dbg(&dev->dev, "disable resources\n");
  107. handle = DEVICE_ACPI_HANDLE(&dev->dev);
  108. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  109. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  110. return 0;
  111. }
  112. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  113. ret = 0;
  114. if (acpi_bus_power_manageable(handle))
  115. acpi_bus_set_power(handle, ACPI_STATE_D3);
  116. /* continue even if acpi_bus_set_power() fails */
  117. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
  118. ret = -ENODEV;
  119. return ret;
  120. }
  121. #ifdef CONFIG_ACPI_SLEEP
  122. static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
  123. {
  124. struct acpi_device *acpi_dev;
  125. acpi_handle handle;
  126. handle = DEVICE_ACPI_HANDLE(&dev->dev);
  127. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  128. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  129. return false;
  130. }
  131. return acpi_bus_can_wakeup(handle);
  132. }
  133. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  134. {
  135. struct acpi_device *acpi_dev;
  136. acpi_handle handle;
  137. int error = 0;
  138. handle = DEVICE_ACPI_HANDLE(&dev->dev);
  139. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  140. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  141. return 0;
  142. }
  143. if (device_can_wakeup(&dev->dev)) {
  144. error = acpi_pm_device_sleep_wake(&dev->dev,
  145. device_may_wakeup(&dev->dev));
  146. if (error)
  147. return error;
  148. }
  149. if (acpi_bus_power_manageable(handle)) {
  150. int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL);
  151. if (power_state < 0)
  152. power_state = (state.event == PM_EVENT_ON) ?
  153. ACPI_STATE_D0 : ACPI_STATE_D3;
  154. /*
  155. * acpi_bus_set_power() often fails (keyboard port can't be
  156. * powered-down?), and in any case, our return value is ignored
  157. * by pnp_bus_suspend(). Hence we don't revert the wakeup
  158. * setting if the set_power fails.
  159. */
  160. error = acpi_bus_set_power(handle, power_state);
  161. }
  162. return error;
  163. }
  164. static int pnpacpi_resume(struct pnp_dev *dev)
  165. {
  166. struct acpi_device *acpi_dev;
  167. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  168. int error = 0;
  169. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
  170. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  171. return -ENODEV;
  172. }
  173. if (device_may_wakeup(&dev->dev))
  174. acpi_pm_device_sleep_wake(&dev->dev, false);
  175. if (acpi_bus_power_manageable(handle))
  176. error = acpi_bus_set_power(handle, ACPI_STATE_D0);
  177. return error;
  178. }
  179. #endif
  180. struct pnp_protocol pnpacpi_protocol = {
  181. .name = "Plug and Play ACPI",
  182. .get = pnpacpi_get_resources,
  183. .set = pnpacpi_set_resources,
  184. .disable = pnpacpi_disable_resources,
  185. #ifdef CONFIG_ACPI_SLEEP
  186. .can_wakeup = pnpacpi_can_wakeup,
  187. .suspend = pnpacpi_suspend,
  188. .resume = pnpacpi_resume,
  189. #endif
  190. };
  191. EXPORT_SYMBOL(pnpacpi_protocol);
  192. static char *__init pnpacpi_get_id(struct acpi_device *device)
  193. {
  194. struct acpi_hardware_id *id;
  195. list_for_each_entry(id, &device->pnp.ids, list) {
  196. if (ispnpidacpi(id->id))
  197. return id->id;
  198. }
  199. return NULL;
  200. }
  201. static int __init pnpacpi_add_device(struct acpi_device *device)
  202. {
  203. acpi_handle temp = NULL;
  204. acpi_status status;
  205. struct pnp_dev *dev;
  206. char *pnpid;
  207. struct acpi_hardware_id *id;
  208. /*
  209. * If a PnPacpi device is not present , the device
  210. * driver should not be loaded.
  211. */
  212. status = acpi_get_handle(device->handle, "_CRS", &temp);
  213. if (ACPI_FAILURE(status))
  214. return 0;
  215. pnpid = pnpacpi_get_id(device);
  216. if (!pnpid)
  217. return 0;
  218. if (is_exclusive_device(device) || !device->status.present)
  219. return 0;
  220. dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
  221. if (!dev)
  222. return -ENOMEM;
  223. dev->data = device;
  224. /* .enabled means the device can decode the resources */
  225. dev->active = device->status.enabled;
  226. status = acpi_get_handle(device->handle, "_SRS", &temp);
  227. if (ACPI_SUCCESS(status))
  228. dev->capabilities |= PNP_CONFIGURABLE;
  229. dev->capabilities |= PNP_READ;
  230. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  231. dev->capabilities |= PNP_WRITE;
  232. if (device->flags.removable)
  233. dev->capabilities |= PNP_REMOVABLE;
  234. status = acpi_get_handle(device->handle, "_DIS", &temp);
  235. if (ACPI_SUCCESS(status))
  236. dev->capabilities |= PNP_DISABLE;
  237. if (strlen(acpi_device_name(device)))
  238. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  239. else
  240. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  241. if (dev->active)
  242. pnpacpi_parse_allocated_resource(dev);
  243. if (dev->capabilities & PNP_CONFIGURABLE)
  244. pnpacpi_parse_resource_option_data(dev);
  245. list_for_each_entry(id, &device->pnp.ids, list) {
  246. if (!strcmp(id->id, pnpid))
  247. continue;
  248. if (!ispnpidacpi(id->id))
  249. continue;
  250. pnp_add_id(dev, id->id);
  251. }
  252. /* clear out the damaged flags */
  253. if (!dev->active)
  254. pnp_init_resources(dev);
  255. pnp_add_device(dev);
  256. num++;
  257. return AE_OK;
  258. }
  259. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  260. u32 lvl, void *context,
  261. void **rv)
  262. {
  263. struct acpi_device *device;
  264. if (!acpi_bus_get_device(handle, &device))
  265. pnpacpi_add_device(device);
  266. else
  267. return AE_CTRL_DEPTH;
  268. return AE_OK;
  269. }
  270. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  271. {
  272. struct acpi_device *acpi = to_acpi_device(dev);
  273. struct pnp_dev *pnp = _pnp;
  274. /* true means it matched */
  275. return !acpi_get_physical_device(acpi->handle)
  276. && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
  277. }
  278. static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
  279. {
  280. struct device *adev;
  281. struct acpi_device *acpi;
  282. adev = bus_find_device(&acpi_bus_type, NULL,
  283. to_pnp_dev(dev), acpi_pnp_match);
  284. if (!adev)
  285. return -ENODEV;
  286. acpi = to_acpi_device(adev);
  287. *handle = acpi->handle;
  288. put_device(adev);
  289. return 0;
  290. }
  291. /* complete initialization of a PNPACPI device includes having
  292. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  293. */
  294. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  295. .bus = &pnp_bus_type,
  296. .find_device = acpi_pnp_find_device,
  297. };
  298. int pnpacpi_disabled __initdata;
  299. static int __init pnpacpi_init(void)
  300. {
  301. if (acpi_disabled || pnpacpi_disabled) {
  302. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  303. return 0;
  304. }
  305. printk(KERN_INFO "pnp: PnP ACPI init\n");
  306. pnp_register_protocol(&pnpacpi_protocol);
  307. register_acpi_bus_type(&acpi_pnp_bus);
  308. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  309. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  310. unregister_acpi_bus_type(&acpi_pnp_bus);
  311. pnp_platform_devices = 1;
  312. return 0;
  313. }
  314. fs_initcall(pnpacpi_init);
  315. static int __init pnpacpi_setup(char *str)
  316. {
  317. if (str == NULL)
  318. return 1;
  319. if (!strncmp(str, "off", 3))
  320. pnpacpi_disabled = 1;
  321. return 1;
  322. }
  323. __setup("pnpacpi=", pnpacpi_setup);