core.c 8.5 KB

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