core.c 6.9 KB

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