core.c 6.6 KB

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