core.c 9.7 KB

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