core.c 9.7 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 "../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 (!('A' <= (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 = ACPI_HANDLE(&dev->dev);
  82. if (!handle || 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. if (WARN_ON_ONCE(acpi_dev != dev->data))
  87. dev->data = acpi_dev;
  88. ret = pnpacpi_build_resource_template(dev, &buffer);
  89. if (ret)
  90. return ret;
  91. ret = pnpacpi_encode_resources(dev, &buffer);
  92. if (ret) {
  93. kfree(buffer.pointer);
  94. return ret;
  95. }
  96. if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
  97. ret = -EINVAL;
  98. else if (acpi_bus_power_manageable(handle))
  99. ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
  100. kfree(buffer.pointer);
  101. return ret;
  102. }
  103. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  104. {
  105. struct acpi_device *acpi_dev;
  106. acpi_handle handle;
  107. int ret;
  108. dev_dbg(&dev->dev, "disable resources\n");
  109. handle = ACPI_HANDLE(&dev->dev);
  110. if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
  111. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  112. return 0;
  113. }
  114. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  115. ret = 0;
  116. if (acpi_bus_power_manageable(handle))
  117. acpi_bus_set_power(handle, ACPI_STATE_D3_COLD);
  118. /* continue even if acpi_bus_set_power() fails */
  119. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
  120. ret = -ENODEV;
  121. return ret;
  122. }
  123. #ifdef CONFIG_ACPI_SLEEP
  124. static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
  125. {
  126. struct acpi_device *acpi_dev;
  127. acpi_handle handle;
  128. handle = ACPI_HANDLE(&dev->dev);
  129. if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
  130. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  131. return false;
  132. }
  133. return acpi_bus_can_wakeup(handle);
  134. }
  135. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  136. {
  137. struct acpi_device *acpi_dev;
  138. acpi_handle handle;
  139. int error = 0;
  140. handle = ACPI_HANDLE(&dev->dev);
  141. if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
  142. dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
  143. return 0;
  144. }
  145. if (device_can_wakeup(&dev->dev)) {
  146. error = acpi_pm_device_sleep_wake(&dev->dev,
  147. device_may_wakeup(&dev->dev));
  148. if (error)
  149. return error;
  150. }
  151. if (acpi_bus_power_manageable(handle)) {
  152. int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
  153. ACPI_STATE_D3_COLD);
  154. if (power_state < 0)
  155. power_state = (state.event == PM_EVENT_ON) ?
  156. ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
  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 = ACPI_HANDLE(&dev->dev);
  171. int error = 0;
  172. if (!handle || 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. struct pnp_dev *dev;
  207. char *pnpid;
  208. struct acpi_hardware_id *id;
  209. int error;
  210. /* Skip devices that are already bound */
  211. if (device->physical_node_count)
  212. return 0;
  213. /*
  214. * If a PnPacpi device is not present , the device
  215. * driver should not be loaded.
  216. */
  217. if (!acpi_has_method(device->handle, "_CRS"))
  218. return 0;
  219. pnpid = pnpacpi_get_id(device);
  220. if (!pnpid)
  221. return 0;
  222. if (is_exclusive_device(device) || !device->status.present)
  223. return 0;
  224. dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
  225. if (!dev)
  226. return -ENOMEM;
  227. dev->data = device;
  228. /* .enabled means the device can decode the resources */
  229. dev->active = device->status.enabled;
  230. if (acpi_has_method(device->handle, "_SRS"))
  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. if (acpi_has_method(device->handle, "_DIS"))
  238. dev->capabilities |= PNP_DISABLE;
  239. if (strlen(acpi_device_name(device)))
  240. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  241. else
  242. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  243. if (dev->active)
  244. pnpacpi_parse_allocated_resource(dev);
  245. if (dev->capabilities & PNP_CONFIGURABLE)
  246. pnpacpi_parse_resource_option_data(dev);
  247. list_for_each_entry(id, &device->pnp.ids, list) {
  248. if (!strcmp(id->id, pnpid))
  249. continue;
  250. if (!ispnpidacpi(id->id))
  251. continue;
  252. pnp_add_id(dev, id->id);
  253. }
  254. /* clear out the damaged flags */
  255. if (!dev->active)
  256. pnp_init_resources(dev);
  257. error = pnp_add_device(dev);
  258. if (error) {
  259. put_device(&dev->dev);
  260. return error;
  261. }
  262. num++;
  263. return 0;
  264. }
  265. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  266. u32 lvl, void *context,
  267. void **rv)
  268. {
  269. struct acpi_device *device;
  270. if (!acpi_bus_get_device(handle, &device))
  271. pnpacpi_add_device(device);
  272. else
  273. return AE_CTRL_DEPTH;
  274. return AE_OK;
  275. }
  276. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  277. {
  278. struct acpi_device *acpi = to_acpi_device(dev);
  279. struct pnp_dev *pnp = _pnp;
  280. /* true means it matched */
  281. return !acpi->physical_node_count
  282. && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
  283. }
  284. static struct acpi_device * __init acpi_pnp_find_companion(struct device *dev)
  285. {
  286. dev = bus_find_device(&acpi_bus_type, NULL, to_pnp_dev(dev),
  287. acpi_pnp_match);
  288. if (!dev)
  289. return NULL;
  290. put_device(dev);
  291. return to_acpi_device(dev);
  292. }
  293. /* complete initialization of a PNPACPI device includes having
  294. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  295. */
  296. static bool acpi_pnp_bus_match(struct device *dev)
  297. {
  298. return dev->bus == &pnp_bus_type;
  299. }
  300. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  301. .name = "PNP",
  302. .match = acpi_pnp_bus_match,
  303. .find_companion = acpi_pnp_find_companion,
  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);