core.c 8.2 KB

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