core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 = 0;
  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(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 = dev->data;
  77. acpi_handle handle = acpi_dev->handle;
  78. struct acpi_buffer buffer;
  79. int ret;
  80. pnp_dbg(&dev->dev, "set resources\n");
  81. ret = pnpacpi_build_resource_template(dev, &buffer);
  82. if (ret)
  83. return ret;
  84. ret = pnpacpi_encode_resources(dev, &buffer);
  85. if (ret) {
  86. kfree(buffer.pointer);
  87. return ret;
  88. }
  89. if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
  90. ret = -EINVAL;
  91. else if (acpi_bus_power_manageable(handle))
  92. ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
  93. kfree(buffer.pointer);
  94. return ret;
  95. }
  96. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  97. {
  98. struct acpi_device *acpi_dev = dev->data;
  99. acpi_handle handle = acpi_dev->handle;
  100. int ret;
  101. dev_dbg(&dev->dev, "disable resources\n");
  102. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  103. ret = 0;
  104. if (acpi_bus_power_manageable(handle))
  105. acpi_bus_set_power(handle, ACPI_STATE_D3);
  106. /* continue even if acpi_bus_set_power() fails */
  107. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
  108. ret = -ENODEV;
  109. return ret;
  110. }
  111. #ifdef CONFIG_ACPI_SLEEP
  112. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  113. {
  114. struct acpi_device *acpi_dev = dev->data;
  115. acpi_handle handle = acpi_dev->handle;
  116. int power_state;
  117. power_state = acpi_pm_device_sleep_state(&dev->dev, NULL);
  118. if (power_state < 0)
  119. power_state = (state.event == PM_EVENT_ON) ?
  120. ACPI_STATE_D0 : ACPI_STATE_D3;
  121. return acpi_bus_set_power(handle, power_state);
  122. }
  123. static int pnpacpi_resume(struct pnp_dev *dev)
  124. {
  125. struct acpi_device *acpi_dev = dev->data;
  126. acpi_handle handle = acpi_dev->handle;
  127. return acpi_bus_set_power(handle, ACPI_STATE_D0);
  128. }
  129. #endif
  130. struct pnp_protocol pnpacpi_protocol = {
  131. .name = "Plug and Play ACPI",
  132. .get = pnpacpi_get_resources,
  133. .set = pnpacpi_set_resources,
  134. .disable = pnpacpi_disable_resources,
  135. #ifdef CONFIG_ACPI_SLEEP
  136. .suspend = pnpacpi_suspend,
  137. .resume = pnpacpi_resume,
  138. #endif
  139. };
  140. EXPORT_SYMBOL(pnpacpi_protocol);
  141. static int __init pnpacpi_add_device(struct acpi_device *device)
  142. {
  143. acpi_handle temp = NULL;
  144. acpi_status status;
  145. struct pnp_dev *dev;
  146. struct acpi_hardware_id *id;
  147. /*
  148. * If a PnPacpi device is not present , the device
  149. * driver should not be loaded.
  150. */
  151. status = acpi_get_handle(device->handle, "_CRS", &temp);
  152. if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
  153. is_exclusive_device(device) || (!device->status.present))
  154. return 0;
  155. dev = pnp_alloc_dev(&pnpacpi_protocol, num, acpi_device_hid(device));
  156. if (!dev)
  157. return -ENOMEM;
  158. dev->data = device;
  159. /* .enabled means the device can decode the resources */
  160. dev->active = device->status.enabled;
  161. status = acpi_get_handle(device->handle, "_SRS", &temp);
  162. if (ACPI_SUCCESS(status))
  163. dev->capabilities |= PNP_CONFIGURABLE;
  164. dev->capabilities |= PNP_READ;
  165. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  166. dev->capabilities |= PNP_WRITE;
  167. if (device->flags.removable)
  168. dev->capabilities |= PNP_REMOVABLE;
  169. status = acpi_get_handle(device->handle, "_DIS", &temp);
  170. if (ACPI_SUCCESS(status))
  171. dev->capabilities |= PNP_DISABLE;
  172. if (strlen(acpi_device_name(device)))
  173. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  174. else
  175. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  176. if (dev->active)
  177. pnpacpi_parse_allocated_resource(dev);
  178. if (dev->capabilities & PNP_CONFIGURABLE)
  179. pnpacpi_parse_resource_option_data(dev);
  180. list_for_each_entry(id, &device->pnp.ids, list) {
  181. if (!strcmp(id->id, acpi_device_hid(device)))
  182. continue;
  183. if (!ispnpidacpi(id->id))
  184. continue;
  185. pnp_add_id(dev, id->id);
  186. }
  187. /* clear out the damaged flags */
  188. if (!dev->active)
  189. pnp_init_resources(dev);
  190. pnp_add_device(dev);
  191. num++;
  192. return AE_OK;
  193. }
  194. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  195. u32 lvl, void *context,
  196. void **rv)
  197. {
  198. struct acpi_device *device;
  199. if (!acpi_bus_get_device(handle, &device))
  200. pnpacpi_add_device(device);
  201. else
  202. return AE_CTRL_DEPTH;
  203. return AE_OK;
  204. }
  205. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  206. {
  207. struct acpi_device *acpi = to_acpi_device(dev);
  208. struct pnp_dev *pnp = _pnp;
  209. /* true means it matched */
  210. return !acpi_get_physical_device(acpi->handle)
  211. && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
  212. }
  213. static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
  214. {
  215. struct device *adev;
  216. struct acpi_device *acpi;
  217. adev = bus_find_device(&acpi_bus_type, NULL,
  218. to_pnp_dev(dev), acpi_pnp_match);
  219. if (!adev)
  220. return -ENODEV;
  221. acpi = to_acpi_device(adev);
  222. *handle = acpi->handle;
  223. put_device(adev);
  224. return 0;
  225. }
  226. /* complete initialization of a PNPACPI device includes having
  227. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  228. */
  229. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  230. .bus = &pnp_bus_type,
  231. .find_device = acpi_pnp_find_device,
  232. };
  233. int pnpacpi_disabled __initdata;
  234. static int __init pnpacpi_init(void)
  235. {
  236. if (acpi_disabled || pnpacpi_disabled) {
  237. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  238. return 0;
  239. }
  240. printk(KERN_INFO "pnp: PnP ACPI init\n");
  241. pnp_register_protocol(&pnpacpi_protocol);
  242. register_acpi_bus_type(&acpi_pnp_bus);
  243. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  244. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  245. unregister_acpi_bus_type(&acpi_pnp_bus);
  246. pnp_platform_devices = 1;
  247. return 0;
  248. }
  249. fs_initcall(pnpacpi_init);
  250. static int __init pnpacpi_setup(char *str)
  251. {
  252. if (str == NULL)
  253. return 1;
  254. if (!strncmp(str, "off", 3))
  255. pnpacpi_disabled = 1;
  256. return 1;
  257. }
  258. __setup("pnpacpi=", pnpacpi_setup);