core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. 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. if (!ispnpidacpi(acpi_device_hid(device)) ||
  122. is_exclusive_device(device))
  123. return 0;
  124. pnp_dbg("ACPI device : hid %s", acpi_device_hid(device));
  125. dev = kcalloc(1, sizeof(struct pnp_dev), GFP_KERNEL);
  126. if (!dev) {
  127. pnp_err("Out of memory");
  128. return -ENOMEM;
  129. }
  130. dev->data = device->handle;
  131. /* .enabled means if the device can decode the resources */
  132. dev->active = device->status.enabled;
  133. status = acpi_get_handle(device->handle, "_SRS", &temp);
  134. if (ACPI_SUCCESS(status))
  135. dev->capabilities |= PNP_CONFIGURABLE;
  136. dev->capabilities |= PNP_READ;
  137. if (device->flags.dynamic_status)
  138. dev->capabilities |= PNP_WRITE;
  139. if (device->flags.removable)
  140. dev->capabilities |= PNP_REMOVABLE;
  141. status = acpi_get_handle(device->handle, "_DIS", &temp);
  142. if (ACPI_SUCCESS(status))
  143. dev->capabilities |= PNP_DISABLE;
  144. dev->protocol = &pnpacpi_protocol;
  145. if (strlen(acpi_device_name(device)))
  146. strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
  147. else
  148. strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
  149. dev->number = num;
  150. /* set the initial values for the PnP device */
  151. dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
  152. if (!dev_id)
  153. goto err;
  154. pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
  155. pnp_add_id(dev_id, dev);
  156. if(dev->active) {
  157. /* parse allocated resource */
  158. status = pnpacpi_parse_allocated_resource(device->handle, &dev->res);
  159. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  160. pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id->id);
  161. goto err1;
  162. }
  163. }
  164. if(dev->capabilities & PNP_CONFIGURABLE) {
  165. status = pnpacpi_parse_resource_option_data(device->handle,
  166. dev);
  167. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  168. pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id->id);
  169. goto err1;
  170. }
  171. }
  172. /* parse compatible ids */
  173. if (device->flags.compatible_ids) {
  174. struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
  175. int i;
  176. for (i = 0; i < cid_list->count; i++) {
  177. if (!ispnpidacpi(cid_list->id[i].value))
  178. continue;
  179. dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
  180. if (!dev_id)
  181. continue;
  182. pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
  183. pnp_add_id(dev_id, dev);
  184. }
  185. }
  186. /* clear out the damaged flags */
  187. if (!dev->active)
  188. pnp_init_resource_table(&dev->res);
  189. pnp_add_device(dev);
  190. num ++;
  191. return AE_OK;
  192. err1:
  193. kfree(dev_id);
  194. err:
  195. kfree(dev);
  196. return -EINVAL;
  197. }
  198. static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
  199. u32 lvl, void *context, void **rv)
  200. {
  201. struct acpi_device *device;
  202. if (!acpi_bus_get_device(handle, &device))
  203. pnpacpi_add_device(device);
  204. else
  205. return AE_CTRL_DEPTH;
  206. return AE_OK;
  207. }
  208. int pnpacpi_disabled __initdata;
  209. int __init pnpacpi_init(void)
  210. {
  211. if (acpi_disabled || pnpacpi_disabled) {
  212. pnp_info("PnP ACPI: disabled");
  213. return 0;
  214. }
  215. pnp_info("PnP ACPI init");
  216. pnp_register_protocol(&pnpacpi_protocol);
  217. acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
  218. pnp_info("PnP ACPI: found %d devices", num);
  219. return 0;
  220. }
  221. subsys_initcall(pnpacpi_init);
  222. static int __init pnpacpi_setup(char *str)
  223. {
  224. if (str == NULL)
  225. return 1;
  226. if (!strncmp(str, "off", 3))
  227. pnpacpi_disabled = 1;
  228. return 1;
  229. }
  230. __setup("pnpacpi=", pnpacpi_setup);
  231. EXPORT_SYMBOL(pnpacpi_protocol);