acpiphp_core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * ACPI PCI Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2002 Hiroshi Aono ([email protected])
  8. * Copyright (C) 2002,2003 Takayoshi Kochi ([email protected])
  9. * Copyright (C) 2002,2003 NEC Corporation
  10. * Copyright (C) 2003-2005 Matthew Wilcox ([email protected])
  11. * Copyright (C) 2003-2005 Hewlett Packard
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  23. * NON INFRINGEMENT. See the GNU General Public License for more
  24. * details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * Send feedback to <[email protected]>
  31. *
  32. */
  33. #define pr_fmt(fmt) "acpiphp: " fmt
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/kernel.h>
  38. #include <linux/pci.h>
  39. #include <linux/pci-acpi.h>
  40. #include <linux/pci_hotplug.h>
  41. #include <linux/slab.h>
  42. #include <linux/smp.h>
  43. #include "acpiphp.h"
  44. /* name size which is used for entries in pcihpfs */
  45. #define SLOT_NAME_SIZE 21 /* {_SUN} */
  46. bool acpiphp_disabled;
  47. /* local variables */
  48. static struct acpiphp_attention_info *attention_info;
  49. #define DRIVER_VERSION "0.5"
  50. #define DRIVER_AUTHOR "Greg Kroah-Hartman <[email protected]>, Takayoshi Kochi <[email protected]>, Matthew Wilcox <[email protected]>"
  51. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver"
  52. MODULE_AUTHOR(DRIVER_AUTHOR);
  53. MODULE_DESCRIPTION(DRIVER_DESC);
  54. MODULE_LICENSE("GPL");
  55. MODULE_PARM_DESC(disable, "disable acpiphp driver");
  56. module_param_named(disable, acpiphp_disabled, bool, 0444);
  57. static int enable_slot(struct hotplug_slot *slot);
  58. static int disable_slot(struct hotplug_slot *slot);
  59. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  60. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  61. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  62. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  63. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  64. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  65. .enable_slot = enable_slot,
  66. .disable_slot = disable_slot,
  67. .set_attention_status = set_attention_status,
  68. .get_power_status = get_power_status,
  69. .get_attention_status = get_attention_status,
  70. .get_latch_status = get_latch_status,
  71. .get_adapter_status = get_adapter_status,
  72. };
  73. /**
  74. * acpiphp_register_attention - set attention LED callback
  75. * @info: must be completely filled with LED callbacks
  76. *
  77. * Description: This is used to register a hardware specific ACPI
  78. * driver that manipulates the attention LED. All the fields in
  79. * info must be set.
  80. */
  81. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  82. {
  83. int retval = -EINVAL;
  84. if (info && info->owner && info->set_attn &&
  85. info->get_attn && !attention_info) {
  86. retval = 0;
  87. attention_info = info;
  88. }
  89. return retval;
  90. }
  91. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  92. /**
  93. * acpiphp_unregister_attention - unset attention LED callback
  94. * @info: must match the pointer used to register
  95. *
  96. * Description: This is used to un-register a hardware specific acpi
  97. * driver that manipulates the attention LED. The pointer to the
  98. * info struct must be the same as the one used to set it.
  99. */
  100. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  101. {
  102. int retval = -EINVAL;
  103. if (info && attention_info == info) {
  104. attention_info = NULL;
  105. retval = 0;
  106. }
  107. return retval;
  108. }
  109. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  110. /**
  111. * enable_slot - power on and enable a slot
  112. * @hotplug_slot: slot to enable
  113. *
  114. * Actual tasks are done in acpiphp_enable_slot()
  115. */
  116. static int enable_slot(struct hotplug_slot *hotplug_slot)
  117. {
  118. struct slot *slot = hotplug_slot->private;
  119. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  120. /* enable the specified slot */
  121. return acpiphp_enable_slot(slot->acpi_slot);
  122. }
  123. /**
  124. * disable_slot - disable and power off a slot
  125. * @hotplug_slot: slot to disable
  126. *
  127. * Actual tasks are done in acpiphp_disable_slot()
  128. */
  129. static int disable_slot(struct hotplug_slot *hotplug_slot)
  130. {
  131. struct slot *slot = hotplug_slot->private;
  132. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  133. /* disable the specified slot */
  134. return acpiphp_disable_slot(slot->acpi_slot);
  135. }
  136. /**
  137. * set_attention_status - set attention LED
  138. * @hotplug_slot: slot to set attention LED on
  139. * @status: value to set attention LED to (0 or 1)
  140. *
  141. * attention status LED, so we use a callback that
  142. * was registered with us. This allows hardware specific
  143. * ACPI implementations to blink the light for us.
  144. */
  145. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  146. {
  147. int retval = -ENODEV;
  148. pr_debug("%s - physical_slot = %s\n", __func__,
  149. hotplug_slot_name(hotplug_slot));
  150. if (attention_info && try_module_get(attention_info->owner)) {
  151. retval = attention_info->set_attn(hotplug_slot, status);
  152. module_put(attention_info->owner);
  153. } else
  154. attention_info = NULL;
  155. return retval;
  156. }
  157. /**
  158. * get_power_status - get power status of a slot
  159. * @hotplug_slot: slot to get status
  160. * @value: pointer to store status
  161. *
  162. * Some platforms may not implement _STA method properly.
  163. * In that case, the value returned may not be reliable.
  164. */
  165. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  166. {
  167. struct slot *slot = hotplug_slot->private;
  168. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  169. *value = acpiphp_get_power_status(slot->acpi_slot);
  170. return 0;
  171. }
  172. /**
  173. * get_attention_status - get attention LED status
  174. * @hotplug_slot: slot to get status from
  175. * @value: returns with value of attention LED
  176. *
  177. * ACPI doesn't have known method to determine the state
  178. * of the attention status LED, so we use a callback that
  179. * was registered with us. This allows hardware specific
  180. * ACPI implementations to determine its state.
  181. */
  182. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  183. {
  184. int retval = -EINVAL;
  185. pr_debug("%s - physical_slot = %s\n", __func__,
  186. hotplug_slot_name(hotplug_slot));
  187. if (attention_info && try_module_get(attention_info->owner)) {
  188. retval = attention_info->get_attn(hotplug_slot, value);
  189. module_put(attention_info->owner);
  190. } else
  191. attention_info = NULL;
  192. return retval;
  193. }
  194. /**
  195. * get_latch_status - get latch status of a slot
  196. * @hotplug_slot: slot to get status
  197. * @value: pointer to store status
  198. *
  199. * ACPI doesn't provide any formal means to access latch status.
  200. * Instead, we fake latch status from _STA.
  201. */
  202. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  203. {
  204. struct slot *slot = hotplug_slot->private;
  205. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  206. *value = acpiphp_get_latch_status(slot->acpi_slot);
  207. return 0;
  208. }
  209. /**
  210. * get_adapter_status - get adapter status of a slot
  211. * @hotplug_slot: slot to get status
  212. * @value: pointer to store status
  213. *
  214. * ACPI doesn't provide any formal means to access adapter status.
  215. * Instead, we fake adapter status from _STA.
  216. */
  217. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  218. {
  219. struct slot *slot = hotplug_slot->private;
  220. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  221. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  222. return 0;
  223. }
  224. /**
  225. * release_slot - free up the memory used by a slot
  226. * @hotplug_slot: slot to free
  227. */
  228. static void release_slot(struct hotplug_slot *hotplug_slot)
  229. {
  230. struct slot *slot = hotplug_slot->private;
  231. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  232. kfree(slot->hotplug_slot);
  233. kfree(slot);
  234. }
  235. /* callback routine to initialize 'struct slot' for each slot */
  236. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot,
  237. unsigned int sun)
  238. {
  239. struct slot *slot;
  240. int retval = -ENOMEM;
  241. char name[SLOT_NAME_SIZE];
  242. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  243. if (!slot)
  244. goto error;
  245. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  246. if (!slot->hotplug_slot)
  247. goto error_slot;
  248. slot->hotplug_slot->info = &slot->info;
  249. slot->hotplug_slot->private = slot;
  250. slot->hotplug_slot->release = &release_slot;
  251. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  252. slot->acpi_slot = acpiphp_slot;
  253. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  254. slot->hotplug_slot->info->attention_status = 0;
  255. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  256. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  257. acpiphp_slot->slot = slot;
  258. slot->sun = sun;
  259. snprintf(name, SLOT_NAME_SIZE, "%u", sun);
  260. retval = pci_hp_register(slot->hotplug_slot, acpiphp_slot->bus,
  261. acpiphp_slot->device, name);
  262. if (retval == -EBUSY)
  263. goto error_hpslot;
  264. if (retval) {
  265. pr_err("pci_hp_register failed with error %d\n", retval);
  266. goto error_hpslot;
  267. }
  268. pr_info("Slot [%s] registered\n", slot_name(slot));
  269. return 0;
  270. error_hpslot:
  271. kfree(slot->hotplug_slot);
  272. error_slot:
  273. kfree(slot);
  274. error:
  275. return retval;
  276. }
  277. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  278. {
  279. struct slot *slot = acpiphp_slot->slot;
  280. int retval = 0;
  281. pr_info("Slot [%s] unregistered\n", slot_name(slot));
  282. retval = pci_hp_deregister(slot->hotplug_slot);
  283. if (retval)
  284. pr_err("pci_hp_deregister failed with error %d\n", retval);
  285. }
  286. void __init acpiphp_init(void)
  287. {
  288. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "%s\n",
  289. acpiphp_disabled ? ", disabled by user; please report a bug"
  290. : "");
  291. }