core.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <acpi/actypes.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 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 void __init pnpidacpi_to_pnpid(char *id, char *str)
  70. {
  71. str[0] = id[0];
  72. str[1] = id[1];
  73. str[2] = id[2];
  74. str[3] = tolower(id[3]);
  75. str[4] = tolower(id[4]);
  76. str[5] = tolower(id[5]);
  77. str[6] = tolower(id[6]);
  78. str[7] = '\0';
  79. }
  80. static int pnpacpi_get_resources(struct pnp_dev *dev,
  81. struct pnp_resource_table *res)
  82. {
  83. acpi_status status;
  84. status = pnpacpi_parse_allocated_resource((acpi_handle) dev->data,
  85. &dev->res);
  86. return ACPI_FAILURE(status) ? -ENODEV : 0;
  87. }
  88. static int pnpacpi_set_resources(struct pnp_dev *dev,
  89. struct pnp_resource_table *res)
  90. {
  91. acpi_handle handle = dev->data;
  92. struct acpi_buffer buffer;
  93. int ret = 0;
  94. acpi_status status;
  95. ret = pnpacpi_build_resource_template(handle, &buffer);
  96. if (ret)
  97. return ret;
  98. ret = pnpacpi_encode_resources(res, &buffer);
  99. if (ret) {
  100. kfree(buffer.pointer);
  101. return ret;
  102. }
  103. status = acpi_set_current_resources(handle, &buffer);
  104. if (ACPI_FAILURE(status))
  105. ret = -EINVAL;
  106. kfree(buffer.pointer);
  107. return ret;
  108. }
  109. static int pnpacpi_disable_resources(struct pnp_dev *dev)
  110. {
  111. acpi_status status;
  112. /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
  113. status = acpi_evaluate_object((acpi_handle) dev->data,
  114. "_DIS", NULL, NULL);
  115. return ACPI_FAILURE(status) ? -ENODEV : 0;
  116. }
  117. #ifdef CONFIG_ACPI_SLEEP
  118. static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
  119. {
  120. int power_state;
  121. power_state = acpi_pm_device_sleep_state(&dev->dev,
  122. device_may_wakeup(&dev->dev),
  123. NULL);
  124. if (power_state < 0)
  125. power_state = (state.event == PM_EVENT_ON) ?
  126. ACPI_STATE_D0 : ACPI_STATE_D3;
  127. return acpi_bus_set_power((acpi_handle) dev->data, power_state);
  128. }
  129. static int pnpacpi_resume(struct pnp_dev *dev)
  130. {
  131. return acpi_bus_set_power((acpi_handle) dev->data, ACPI_STATE_D0);
  132. }
  133. #endif
  134. static struct pnp_protocol pnpacpi_protocol = {
  135. .name = "Plug and Play ACPI",
  136. .get = pnpacpi_get_resources,
  137. .set = pnpacpi_set_resources,
  138. .disable = pnpacpi_disable_resources,
  139. #ifdef CONFIG_ACPI_SLEEP
  140. .suspend = pnpacpi_suspend,
  141. .resume = pnpacpi_resume,
  142. #endif
  143. };
  144. static int __init pnpacpi_add_device(struct acpi_device *device)
  145. {
  146. acpi_handle temp = NULL;
  147. acpi_status status;
  148. struct pnp_id *dev_id;
  149. struct pnp_dev *dev;
  150. status = acpi_get_handle(device->handle, "_CRS", &temp);
  151. if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
  152. is_exclusive_device(device))
  153. return 0;
  154. dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
  155. if (!dev) {
  156. pnp_err("Out of memory");
  157. return -ENOMEM;
  158. }
  159. dev->data = device->handle;
  160. /* .enabled means the device can decode the resources */
  161. dev->active = device->status.enabled;
  162. status = acpi_get_handle(device->handle, "_SRS", &temp);
  163. if (ACPI_SUCCESS(status))
  164. dev->capabilities |= PNP_CONFIGURABLE;
  165. dev->capabilities |= PNP_READ;
  166. if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
  167. dev->capabilities |= PNP_WRITE;
  168. if (device->flags.removable)
  169. dev->capabilities |= PNP_REMOVABLE;
  170. status = acpi_get_handle(device->handle, "_DIS", &temp);
  171. if (ACPI_SUCCESS(status))
  172. dev->capabilities |= PNP_DISABLE;
  173. dev->protocol = &pnpacpi_protocol;
  174. if (strlen(acpi_device_name(device)))
  175. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  176. else
  177. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  178. dev->number = num;
  179. /* set the initial values for the PnP device */
  180. dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
  181. if (!dev_id)
  182. goto err;
  183. pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
  184. pnp_add_id(dev_id, dev);
  185. if (dev->active) {
  186. /* parse allocated resource */
  187. status = pnpacpi_parse_allocated_resource(device->handle,
  188. &dev->res);
  189. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  190. pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s",
  191. acpi_device_hid(device));
  192. }
  193. }
  194. if (dev->capabilities & PNP_CONFIGURABLE) {
  195. status = pnpacpi_parse_resource_option_data(device->handle,
  196. dev);
  197. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  198. pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s",
  199. acpi_device_hid(device));
  200. }
  201. }
  202. /* parse compatible ids */
  203. if (device->flags.compatible_ids) {
  204. struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
  205. int i;
  206. for (i = 0; i < cid_list->count; i++) {
  207. if (!ispnpidacpi(cid_list->id[i].value))
  208. continue;
  209. dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
  210. if (!dev_id)
  211. continue;
  212. pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
  213. pnp_add_id(dev_id, dev);
  214. }
  215. }
  216. /* clear out the damaged flags */
  217. if (!dev->active)
  218. pnp_init_resource_table(&dev->res);
  219. pnp_add_device(dev);
  220. num++;
  221. return AE_OK;
  222. err:
  223. kfree(dev);
  224. return -EINVAL;
  225. }
  226. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  227. u32 lvl, void *context,
  228. void **rv)
  229. {
  230. struct acpi_device *device;
  231. if (!acpi_bus_get_device(handle, &device))
  232. pnpacpi_add_device(device);
  233. else
  234. return AE_CTRL_DEPTH;
  235. return AE_OK;
  236. }
  237. static int __init acpi_pnp_match(struct device *dev, void *_pnp)
  238. {
  239. struct acpi_device *acpi = to_acpi_device(dev);
  240. struct pnp_dev *pnp = _pnp;
  241. /* true means it matched */
  242. return acpi->flags.hardware_id
  243. && !acpi_get_physical_device(acpi->handle)
  244. && compare_pnp_id(pnp->id, acpi->pnp.hardware_id);
  245. }
  246. static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
  247. {
  248. struct device *adev;
  249. struct acpi_device *acpi;
  250. adev = bus_find_device(&acpi_bus_type, NULL,
  251. to_pnp_dev(dev), acpi_pnp_match);
  252. if (!adev)
  253. return -ENODEV;
  254. acpi = to_acpi_device(adev);
  255. *handle = acpi->handle;
  256. put_device(adev);
  257. return 0;
  258. }
  259. /* complete initialization of a PNPACPI device includes having
  260. * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
  261. */
  262. static struct acpi_bus_type __initdata acpi_pnp_bus = {
  263. .bus = &pnp_bus_type,
  264. .find_device = acpi_pnp_find_device,
  265. };
  266. int pnpacpi_disabled __initdata;
  267. static int __init pnpacpi_init(void)
  268. {
  269. if (acpi_disabled || pnpacpi_disabled) {
  270. pnp_info("PnP ACPI: disabled");
  271. return 0;
  272. }
  273. pnp_info("PnP ACPI init");
  274. pnp_register_protocol(&pnpacpi_protocol);
  275. register_acpi_bus_type(&acpi_pnp_bus);
  276. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  277. pnp_info("PnP ACPI: found %d devices", num);
  278. unregister_acpi_bus_type(&acpi_pnp_bus);
  279. pnp_platform_devices = 1;
  280. return 0;
  281. }
  282. subsys_initcall(pnpacpi_init);
  283. static int __init pnpacpi_setup(char *str)
  284. {
  285. if (str == NULL)
  286. return 1;
  287. if (!strncmp(str, "off", 3))
  288. pnpacpi_disabled = 1;
  289. return 1;
  290. }
  291. __setup("pnpacpi=", pnpacpi_setup);