acpiphp_core.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ACPI PCI Hot Plug Controller Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  9. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  10. * Copyright (C) 2002,2003 NEC Corporation
  11. * Copyright (C) 2003-2005 Matthew Wilcox (willy@infradead.org)
  12. * Copyright (C) 2003-2005 Hewlett Packard
  13. *
  14. * All rights reserved.
  15. *
  16. * Send feedback to <kristen.c.accardi@intel.com>
  17. *
  18. */
  19. #define pr_fmt(fmt) "acpiphp: " fmt
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci-acpi.h>
  26. #include <linux/pci_hotplug.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp.h>
  29. #include "acpiphp.h"
  30. /* name size which is used for entries in pcihpfs */
  31. #define SLOT_NAME_SIZE 21 /* {_SUN} */
  32. bool acpiphp_disabled;
  33. /* local variables */
  34. static struct acpiphp_attention_info *attention_info;
  35. #define DRIVER_VERSION "0.5"
  36. #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>, Matthew Wilcox <willy@infradead.org>"
  37. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver"
  38. MODULE_AUTHOR(DRIVER_AUTHOR);
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_PARM_DESC(disable, "disable acpiphp driver");
  41. module_param_named(disable, acpiphp_disabled, bool, 0444);
  42. static int enable_slot(struct hotplug_slot *slot);
  43. static int disable_slot(struct hotplug_slot *slot);
  44. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  45. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  46. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  47. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  48. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  49. static const struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  50. .enable_slot = enable_slot,
  51. .disable_slot = disable_slot,
  52. .set_attention_status = set_attention_status,
  53. .get_power_status = get_power_status,
  54. .get_attention_status = get_attention_status,
  55. .get_latch_status = get_latch_status,
  56. .get_adapter_status = get_adapter_status,
  57. };
  58. /**
  59. * acpiphp_register_attention - set attention LED callback
  60. * @info: must be completely filled with LED callbacks
  61. *
  62. * Description: This is used to register a hardware specific ACPI
  63. * driver that manipulates the attention LED. All the fields in
  64. * info must be set.
  65. */
  66. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  67. {
  68. int retval = -EINVAL;
  69. if (info && info->set_attn && info->get_attn && !attention_info) {
  70. retval = 0;
  71. attention_info = info;
  72. }
  73. return retval;
  74. }
  75. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  76. /**
  77. * acpiphp_unregister_attention - unset attention LED callback
  78. * @info: must match the pointer used to register
  79. *
  80. * Description: This is used to un-register a hardware specific acpi
  81. * driver that manipulates the attention LED. The pointer to the
  82. * info struct must be the same as the one used to set it.
  83. */
  84. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  85. {
  86. int retval = -EINVAL;
  87. if (info && attention_info == info) {
  88. attention_info = NULL;
  89. retval = 0;
  90. }
  91. return retval;
  92. }
  93. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  94. /**
  95. * enable_slot - power on and enable a slot
  96. * @hotplug_slot: slot to enable
  97. *
  98. * Actual tasks are done in acpiphp_enable_slot()
  99. */
  100. static int enable_slot(struct hotplug_slot *hotplug_slot)
  101. {
  102. struct slot *slot = to_slot(hotplug_slot);
  103. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  104. /* enable the specified slot */
  105. return acpiphp_enable_slot(slot->acpi_slot);
  106. }
  107. /**
  108. * disable_slot - disable and power off a slot
  109. * @hotplug_slot: slot to disable
  110. *
  111. * Actual tasks are done in acpiphp_disable_slot()
  112. */
  113. static int disable_slot(struct hotplug_slot *hotplug_slot)
  114. {
  115. struct slot *slot = to_slot(hotplug_slot);
  116. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  117. /* disable the specified slot */
  118. return acpiphp_disable_slot(slot->acpi_slot);
  119. }
  120. /**
  121. * set_attention_status - set attention LED
  122. * @hotplug_slot: slot to set attention LED on
  123. * @status: value to set attention LED to (0 or 1)
  124. *
  125. * attention status LED, so we use a callback that
  126. * was registered with us. This allows hardware specific
  127. * ACPI implementations to blink the light for us.
  128. */
  129. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  130. {
  131. int retval = -ENODEV;
  132. pr_debug("%s - physical_slot = %s\n", __func__,
  133. hotplug_slot_name(hotplug_slot));
  134. if (attention_info && try_module_get(attention_info->owner)) {
  135. retval = attention_info->set_attn(hotplug_slot, status);
  136. module_put(attention_info->owner);
  137. } else
  138. attention_info = NULL;
  139. return retval;
  140. }
  141. /**
  142. * get_power_status - get power status of a slot
  143. * @hotplug_slot: slot to get status
  144. * @value: pointer to store status
  145. *
  146. * Some platforms may not implement _STA method properly.
  147. * In that case, the value returned may not be reliable.
  148. */
  149. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  150. {
  151. struct slot *slot = to_slot(hotplug_slot);
  152. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  153. *value = acpiphp_get_power_status(slot->acpi_slot);
  154. return 0;
  155. }
  156. /**
  157. * get_attention_status - get attention LED status
  158. * @hotplug_slot: slot to get status from
  159. * @value: returns with value of attention LED
  160. *
  161. * ACPI doesn't have known method to determine the state
  162. * of the attention status LED, so we use a callback that
  163. * was registered with us. This allows hardware specific
  164. * ACPI implementations to determine its state.
  165. */
  166. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  167. {
  168. int retval = -EINVAL;
  169. pr_debug("%s - physical_slot = %s\n", __func__,
  170. hotplug_slot_name(hotplug_slot));
  171. if (attention_info && try_module_get(attention_info->owner)) {
  172. retval = attention_info->get_attn(hotplug_slot, value);
  173. module_put(attention_info->owner);
  174. } else
  175. attention_info = NULL;
  176. return retval;
  177. }
  178. /**
  179. * get_latch_status - get latch status of a slot
  180. * @hotplug_slot: slot to get status
  181. * @value: pointer to store status
  182. *
  183. * ACPI doesn't provide any formal means to access latch status.
  184. * Instead, we fake latch status from _STA.
  185. */
  186. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  187. {
  188. struct slot *slot = to_slot(hotplug_slot);
  189. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  190. *value = acpiphp_get_latch_status(slot->acpi_slot);
  191. return 0;
  192. }
  193. /**
  194. * get_adapter_status - get adapter status of a slot
  195. * @hotplug_slot: slot to get status
  196. * @value: pointer to store status
  197. *
  198. * ACPI doesn't provide any formal means to access adapter status.
  199. * Instead, we fake adapter status from _STA.
  200. */
  201. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  202. {
  203. struct slot *slot = to_slot(hotplug_slot);
  204. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  205. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  206. return 0;
  207. }
  208. /* callback routine to initialize 'struct slot' for each slot */
  209. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot,
  210. unsigned int sun)
  211. {
  212. struct slot *slot;
  213. int retval = -ENOMEM;
  214. char name[SLOT_NAME_SIZE];
  215. slot = kzalloc_obj(*slot);
  216. if (!slot)
  217. goto error;
  218. slot->hotplug_slot.ops = &acpi_hotplug_slot_ops;
  219. slot->acpi_slot = acpiphp_slot;
  220. acpiphp_slot->slot = slot;
  221. slot->sun = sun;
  222. snprintf(name, SLOT_NAME_SIZE, "%u", sun);
  223. retval = pci_hp_register(&slot->hotplug_slot, acpiphp_slot->bus,
  224. acpiphp_slot->device, name);
  225. if (retval == -EBUSY)
  226. goto error_slot;
  227. if (retval) {
  228. pr_err("pci_hp_register failed with error %d\n", retval);
  229. goto error_slot;
  230. }
  231. pr_info("Slot [%s] registered\n", slot_name(slot));
  232. return 0;
  233. error_slot:
  234. kfree(slot);
  235. error:
  236. return retval;
  237. }
  238. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  239. {
  240. struct slot *slot = acpiphp_slot->slot;
  241. pr_info("Slot [%s] unregistered\n", slot_name(slot));
  242. pci_hp_deregister(&slot->hotplug_slot);
  243. kfree(slot);
  244. }
  245. void __init acpiphp_init(void)
  246. {
  247. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "%s\n",
  248. acpiphp_disabled ? ", disabled by user; please report a bug"
  249. : "");
  250. }