acpiphp_core.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 (matthew.wilcox@hp.com)
  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@hp.com>"
  37. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver"
  38. MODULE_AUTHOR(DRIVER_AUTHOR);
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. MODULE_PARM_DESC(disable, "disable acpiphp driver");
  42. module_param_named(disable, acpiphp_disabled, bool, 0444);
  43. static int enable_slot(struct hotplug_slot *slot);
  44. static int disable_slot(struct hotplug_slot *slot);
  45. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  46. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  47. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  48. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  49. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  50. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  51. .enable_slot = enable_slot,
  52. .disable_slot = disable_slot,
  53. .set_attention_status = set_attention_status,
  54. .get_power_status = get_power_status,
  55. .get_attention_status = get_attention_status,
  56. .get_latch_status = get_latch_status,
  57. .get_adapter_status = get_adapter_status,
  58. };
  59. /**
  60. * acpiphp_register_attention - set attention LED callback
  61. * @info: must be completely filled with LED callbacks
  62. *
  63. * Description: This is used to register a hardware specific ACPI
  64. * driver that manipulates the attention LED. All the fields in
  65. * info must be set.
  66. */
  67. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  68. {
  69. int retval = -EINVAL;
  70. if (info && info->owner && info->set_attn &&
  71. info->get_attn && !attention_info) {
  72. retval = 0;
  73. attention_info = info;
  74. }
  75. return retval;
  76. }
  77. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  78. /**
  79. * acpiphp_unregister_attention - unset attention LED callback
  80. * @info: must match the pointer used to register
  81. *
  82. * Description: This is used to un-register a hardware specific acpi
  83. * driver that manipulates the attention LED. The pointer to the
  84. * info struct must be the same as the one used to set it.
  85. */
  86. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  87. {
  88. int retval = -EINVAL;
  89. if (info && attention_info == info) {
  90. attention_info = NULL;
  91. retval = 0;
  92. }
  93. return retval;
  94. }
  95. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  96. /**
  97. * enable_slot - power on and enable a slot
  98. * @hotplug_slot: slot to enable
  99. *
  100. * Actual tasks are done in acpiphp_enable_slot()
  101. */
  102. static int enable_slot(struct hotplug_slot *hotplug_slot)
  103. {
  104. struct slot *slot = hotplug_slot->private;
  105. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  106. /* enable the specified slot */
  107. return acpiphp_enable_slot(slot->acpi_slot);
  108. }
  109. /**
  110. * disable_slot - disable and power off a slot
  111. * @hotplug_slot: slot to disable
  112. *
  113. * Actual tasks are done in acpiphp_disable_slot()
  114. */
  115. static int disable_slot(struct hotplug_slot *hotplug_slot)
  116. {
  117. struct slot *slot = hotplug_slot->private;
  118. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  119. /* disable the specified slot */
  120. return acpiphp_disable_slot(slot->acpi_slot);
  121. }
  122. /**
  123. * set_attention_status - set attention LED
  124. * @hotplug_slot: slot to set attention LED on
  125. * @status: value to set attention LED to (0 or 1)
  126. *
  127. * attention status LED, so we use a callback that
  128. * was registered with us. This allows hardware specific
  129. * ACPI implementations to blink the light for us.
  130. */
  131. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  132. {
  133. int retval = -ENODEV;
  134. pr_debug("%s - physical_slot = %s\n", __func__,
  135. hotplug_slot_name(hotplug_slot));
  136. if (attention_info && try_module_get(attention_info->owner)) {
  137. retval = attention_info->set_attn(hotplug_slot, status);
  138. module_put(attention_info->owner);
  139. } else
  140. attention_info = NULL;
  141. return retval;
  142. }
  143. /**
  144. * get_power_status - get power status of a slot
  145. * @hotplug_slot: slot to get status
  146. * @value: pointer to store status
  147. *
  148. * Some platforms may not implement _STA method properly.
  149. * In that case, the value returned may not be reliable.
  150. */
  151. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  152. {
  153. struct slot *slot = hotplug_slot->private;
  154. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  155. *value = acpiphp_get_power_status(slot->acpi_slot);
  156. return 0;
  157. }
  158. /**
  159. * get_attention_status - get attention LED status
  160. * @hotplug_slot: slot to get status from
  161. * @value: returns with value of attention LED
  162. *
  163. * ACPI doesn't have known method to determine the state
  164. * of the attention status LED, so we use a callback that
  165. * was registered with us. This allows hardware specific
  166. * ACPI implementations to determine its state.
  167. */
  168. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  169. {
  170. int retval = -EINVAL;
  171. pr_debug("%s - physical_slot = %s\n", __func__,
  172. hotplug_slot_name(hotplug_slot));
  173. if (attention_info && try_module_get(attention_info->owner)) {
  174. retval = attention_info->get_attn(hotplug_slot, value);
  175. module_put(attention_info->owner);
  176. } else
  177. attention_info = NULL;
  178. return retval;
  179. }
  180. /**
  181. * get_latch_status - get latch status of a slot
  182. * @hotplug_slot: slot to get status
  183. * @value: pointer to store status
  184. *
  185. * ACPI doesn't provide any formal means to access latch status.
  186. * Instead, we fake latch status from _STA.
  187. */
  188. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  189. {
  190. struct slot *slot = hotplug_slot->private;
  191. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  192. *value = acpiphp_get_latch_status(slot->acpi_slot);
  193. return 0;
  194. }
  195. /**
  196. * get_adapter_status - get adapter status of a slot
  197. * @hotplug_slot: slot to get status
  198. * @value: pointer to store status
  199. *
  200. * ACPI doesn't provide any formal means to access adapter status.
  201. * Instead, we fake adapter status from _STA.
  202. */
  203. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  204. {
  205. struct slot *slot = hotplug_slot->private;
  206. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  207. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  208. return 0;
  209. }
  210. /* callback routine to initialize 'struct slot' for each slot */
  211. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot,
  212. unsigned int sun)
  213. {
  214. struct slot *slot;
  215. int retval = -ENOMEM;
  216. char name[SLOT_NAME_SIZE];
  217. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  218. if (!slot)
  219. goto error;
  220. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  221. if (!slot->hotplug_slot)
  222. goto error_slot;
  223. slot->hotplug_slot->info = &slot->info;
  224. slot->hotplug_slot->private = slot;
  225. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  226. slot->acpi_slot = acpiphp_slot;
  227. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  228. slot->hotplug_slot->info->attention_status = 0;
  229. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  230. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  231. acpiphp_slot->slot = slot;
  232. slot->sun = sun;
  233. snprintf(name, SLOT_NAME_SIZE, "%u", sun);
  234. retval = pci_hp_register(slot->hotplug_slot, acpiphp_slot->bus,
  235. acpiphp_slot->device, name);
  236. if (retval == -EBUSY)
  237. goto error_hpslot;
  238. if (retval) {
  239. pr_err("pci_hp_register failed with error %d\n", retval);
  240. goto error_hpslot;
  241. }
  242. pr_info("Slot [%s] registered\n", slot_name(slot));
  243. return 0;
  244. error_hpslot:
  245. kfree(slot->hotplug_slot);
  246. error_slot:
  247. kfree(slot);
  248. error:
  249. return retval;
  250. }
  251. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  252. {
  253. struct slot *slot = acpiphp_slot->slot;
  254. pr_info("Slot [%s] unregistered\n", slot_name(slot));
  255. pci_hp_deregister(slot->hotplug_slot);
  256. kfree(slot->hotplug_slot);
  257. kfree(slot);
  258. }
  259. void __init acpiphp_init(void)
  260. {
  261. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "%s\n",
  262. acpiphp_disabled ? ", disabled by user; please report a bug"
  263. : "");
  264. }