core.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/mod_devicetable.h>
  24. #include <acpi/acpi_bus.h>
  25. #include "../base.h"
  26. #include "pnpacpi.h"
  27. static int num = 0;
  28. /* We need only to blacklist devices that have already an acpi driver that
  29. * can't use pnp layer. We don't need to blacklist device that are directly
  30. * used by the kernel (PCI root, ...), as it is harmless and there were
  31. * already present in pnpbios. But there is an exception for devices that
  32. * have irqs (PIC, Timer) because we call acpi_register_gsi.
  33. * Finally, only devices that have a CRS method need to be in this list.
  34. */
  35. static struct acpi_device_id excluded_id_list[] __initdata = {
  36. {"PNP0C09", 0}, /* EC */
  37. {"PNP0C0F", 0}, /* Link device */
  38. {"PNP0000", 0}, /* PIC */
  39. {"PNP0100", 0}, /* Timer */
  40. {"", 0},
  41. };
  42. static inline int __init is_exclusive_device(struct acpi_device *dev)
  43. {
  44. return (!acpi_match_device_ids(dev, excluded_id_list));
  45. }
  46. /*
  47. * Compatible Device IDs
  48. */
  49. #define TEST_HEX(c) \
  50. if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
  51. return 0
  52. #define TEST_ALPHA(c) \
  53. if (!('@' <= (c) || (c) <= 'Z')) \
  54. return 0
  55. static int __init ispnpidacpi(char *id)
  56. {
  57. TEST_ALPHA(id[0]);
  58. TEST_ALPHA(id[1]);
  59. TEST_ALPHA(id[2]);
  60. TEST_HEX(id[3]);
  61. TEST_HEX(id[4]);
  62. TEST_HEX(id[5]);
  63. TEST_HEX(id[6]);
  64. if (id[7] != '\0')
  65. return 0;
  66. return 1;
  67. }
  68. static int pnpacpi_get_resources(struct pnp_dev *dev)
  69. {
  70. pnp_dbg(&dev->dev, "get resources\n");
  71. return pnpacpi_parse_allocated_resource(dev);
  72. }
  73. static int pnpacpi_set_resources(struct pnp_dev *dev)
  74. {
  75. acpi_handle handle = dev->data;
  76. struct acpi_buffer buffer;
  77. int ret;
  78. acpi_status status;
  79. pnp_dbg(&dev->dev, "set resources\n");
  80. ret = pnpacpi_build_resource_template(dev, &buffer);
  81. if (ret)
  82. return ret;
  83. ret = pnpacpi_encode_resources(dev, &buffer);
  84. if (ret) {
  85. kfree(buffer.pointer);
  86. return ret;
  87. }
  88. status = acpi_set_current_resources(handle, &buffer);
  89. if (ACPI_FAILURE(status))
  90. ret = -EINVAL;
  91. kfree(buffer.pointer);
  92. return ret;
  93. }
  94. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  95. {
  96. acpi_status status;
  97. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  98. status = acpi_evaluate_object((acpi_handle) dev->data,
  99. "_DIS", NULL, NULL);
  100. return ACPI_FAILURE(status) ? -ENODEV : 0;
  101. }
  102. #ifdef CONFIG_ACPI_SLEEP
  103. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  104. {
  105. int power_state;
  106. power_state = acpi_pm_device_sleep_state(&dev->dev, NULL);
  107. if (power_state < 0)
  108. power_state = (state.event == PM_EVENT_ON) ?
  109. ACPI_STATE_D0 : ACPI_STATE_D3;
  110. return acpi_bus_set_power((acpi_handle) dev->data, power_state);
  111. }
  112. static int pnpacpi_resume(struct pnp_dev *dev)
  113. {
  114. return acpi_bus_set_power((acpi_handle) dev->data, ACPI_STATE_D0);
  115. }
  116. #endif
  117. static struct pnp_protocol pnpacpi_protocol = {
  118. .name = "Plug and Play ACPI",
  119. .get = pnpacpi_get_resources,
  120. .set = pnpacpi_set_resources,
  121. .disable = pnpacpi_disable_resources,
  122. #ifdef CONFIG_ACPI_SLEEP
  123. .suspend = pnpacpi_suspend,
  124. .resume = pnpacpi_resume,
  125. #endif
  126. };
  127. static int __init pnpacpi_add_device(struct acpi_device *device)
  128. {
  129. acpi_handle temp = NULL;
  130. acpi_status status;
  131. struct pnp_dev *dev;
  132. /*
  133. * If a PnPacpi device is not present , the device
  134. * driver should not be loaded.
  135. */
  136. status = acpi_get_handle(device->handle, "_CRS", &temp);
  137. if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
  138. is_exclusive_device(device) || (!device->status.present))
  139. return 0;
  140. dev = pnp_alloc_dev(&pnpacpi_protocol, num, acpi_device_hid(device));
  141. if (!dev)
  142. return -ENOMEM;
  143. dev->data = device->handle;
  144. /* .enabled means the device can decode the resources */
  145. dev->active = device->status.enabled;
  146. status = acpi_get_handle(device->handle, "_SRS", &temp);
  147. if (ACPI_SUCCESS(status))
  148. dev->capabilities |= PNP_CONFIGURABLE;
  149. dev->capabilities |= PNP_READ;
  150. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  151. dev->capabilities |= PNP_WRITE;
  152. if (device->flags.removable)
  153. dev->capabilities |= PNP_REMOVABLE;
  154. status = acpi_get_handle(device->handle, "_DIS", &temp);
  155. if (ACPI_SUCCESS(status))
  156. dev->capabilities |= PNP_DISABLE;
  157. if (strlen(acpi_device_name(device)))
  158. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  159. else
  160. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  161. if (dev->active)
  162. pnpacpi_parse_allocated_resource(dev);
  163. if (dev->capabilities & PNP_CONFIGURABLE)
  164. pnpacpi_parse_resource_option_data(dev);
  165. if (device->flags.compatible_ids) {
  166. struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
  167. int i;
  168. for (i = 0; i < cid_list->count; i++) {
  169. if (!ispnpidacpi(cid_list->id[i].value))
  170. continue;
  171. pnp_add_id(dev, cid_list->id[i].value);
  172. }
  173. }
  174. /* clear out the damaged flags */
  175. if (!dev->active)
  176. pnp_init_resources(dev);
  177. pnp_add_device(dev);
  178. num++;
  179. return AE_OK;
  180. }
  181. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  182. u32 lvl, void *context,
  183. void **rv)
  184. {
  185. struct acpi_device *device;
  186. if (!acpi_bus_get_device(handle, &device))
  187. pnpacpi_add_device(device);
  188. else
  189. return AE_CTRL_DEPTH;
  190. return AE_OK;
  191. }
  192. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  193. {
  194. struct acpi_device *acpi = to_acpi_device(dev);
  195. struct pnp_dev *pnp = _pnp;
  196. /* true means it matched */
  197. return acpi->flags.hardware_id
  198. && !acpi_get_physical_device(acpi->handle)
  199. && compare_pnp_id(pnp->id, acpi->pnp.hardware_id);
  200. }
  201. static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
  202. {
  203. struct device *adev;
  204. struct acpi_device *acpi;
  205. adev = bus_find_device(&acpi_bus_type, NULL,
  206. to_pnp_dev(dev), acpi_pnp_match);
  207. if (!adev)
  208. return -ENODEV;
  209. acpi = to_acpi_device(adev);
  210. *handle = acpi->handle;
  211. put_device(adev);
  212. return 0;
  213. }
  214. /* complete initialization of a PNPACPI device includes having
  215. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  216. */
  217. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  218. .bus = &pnp_bus_type,
  219. .find_device = acpi_pnp_find_device,
  220. };
  221. int pnpacpi_disabled __initdata;
  222. static int __init pnpacpi_init(void)
  223. {
  224. if (acpi_disabled || pnpacpi_disabled) {
  225. printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
  226. return 0;
  227. }
  228. printk(KERN_INFO "pnp: PnP ACPI init\n");
  229. pnp_register_protocol(&pnpacpi_protocol);
  230. register_acpi_bus_type(&acpi_pnp_bus);
  231. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  232. printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
  233. unregister_acpi_bus_type(&acpi_pnp_bus);
  234. pnp_platform_devices = 1;
  235. return 0;
  236. }
  237. fs_initcall(pnpacpi_init);
  238. static int __init pnpacpi_setup(char *str)
  239. {
  240. if (str == NULL)
  241. return 1;
  242. if (!strncmp(str, "off", 3))
  243. pnpacpi_disabled = 1;
  244. return 1;
  245. }
  246. __setup("pnpacpi=", pnpacpi_setup);