core.c 6.6 KB

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