acpiphp_core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * ACPI PCI Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  8. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  9. * Copyright (C) 2002,2003 NEC Corporation
  10. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  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 <kristen.c.accardi@intel.com>
  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 <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>, Matthew Wilcox <willy@hp.com>"
  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. /* export the attention callback registration methods */
  58. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  59. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  60. static int enable_slot (struct hotplug_slot *slot);
  61. static int disable_slot (struct hotplug_slot *slot);
  62. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  63. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  67. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  68. .enable_slot = enable_slot,
  69. .disable_slot = disable_slot,
  70. .set_attention_status = set_attention_status,
  71. .get_power_status = get_power_status,
  72. .get_attention_status = get_attention_status,
  73. .get_latch_status = get_latch_status,
  74. .get_adapter_status = get_adapter_status,
  75. };
  76. /**
  77. * acpiphp_register_attention - set attention LED callback
  78. * @info: must be completely filled with LED callbacks
  79. *
  80. * Description: This is used to register a hardware specific ACPI
  81. * driver that manipulates the attention LED. All the fields in
  82. * info must be set.
  83. */
  84. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  85. {
  86. int retval = -EINVAL;
  87. if (info && info->owner && info->set_attn &&
  88. info->get_attn && !attention_info) {
  89. retval = 0;
  90. attention_info = info;
  91. }
  92. return retval;
  93. }
  94. /**
  95. * acpiphp_unregister_attention - unset attention LED callback
  96. * @info: must match the pointer used to register
  97. *
  98. * Description: This is used to un-register a hardware specific acpi
  99. * driver that manipulates the attention LED. The pointer to the
  100. * info struct must be the same as the one used to set it.
  101. */
  102. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  103. {
  104. int retval = -EINVAL;
  105. if (info && attention_info == info) {
  106. attention_info = NULL;
  107. retval = 0;
  108. }
  109. return retval;
  110. }
  111. /**
  112. * enable_slot - power on and enable a slot
  113. * @hotplug_slot: slot to enable
  114. *
  115. * Actual tasks are done in acpiphp_enable_slot()
  116. */
  117. static int enable_slot(struct hotplug_slot *hotplug_slot)
  118. {
  119. struct slot *slot = hotplug_slot->private;
  120. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  121. /* enable the specified slot */
  122. return acpiphp_enable_slot(slot->acpi_slot);
  123. }
  124. /**
  125. * disable_slot - disable and power off a slot
  126. * @hotplug_slot: slot to disable
  127. *
  128. * Actual tasks are done in acpiphp_disable_slot()
  129. */
  130. static int disable_slot(struct hotplug_slot *hotplug_slot)
  131. {
  132. struct slot *slot = hotplug_slot->private;
  133. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  134. /* disable the specified slot */
  135. return acpiphp_disable_and_eject_slot(slot->acpi_slot);
  136. }
  137. /**
  138. * set_attention_status - set attention LED
  139. * @hotplug_slot: slot to set attention LED on
  140. * @status: value to set attention LED to (0 or 1)
  141. *
  142. * attention status LED, so we use a callback that
  143. * was registered with us. This allows hardware specific
  144. * ACPI implementations to blink the light for us.
  145. */
  146. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  147. {
  148. int retval = -ENODEV;
  149. pr_debug("%s - physical_slot = %s\n", __func__,
  150. hotplug_slot_name(hotplug_slot));
  151. if (attention_info && try_module_get(attention_info->owner)) {
  152. retval = attention_info->set_attn(hotplug_slot, status);
  153. module_put(attention_info->owner);
  154. } else
  155. attention_info = NULL;
  156. return retval;
  157. }
  158. /**
  159. * get_power_status - get power status of a slot
  160. * @hotplug_slot: slot to get status
  161. * @value: pointer to store status
  162. *
  163. * Some platforms may not implement _STA method properly.
  164. * In that case, the value returned may not be reliable.
  165. */
  166. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  167. {
  168. struct slot *slot = hotplug_slot->private;
  169. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  170. *value = acpiphp_get_power_status(slot->acpi_slot);
  171. return 0;
  172. }
  173. /**
  174. * get_attention_status - get attention LED status
  175. * @hotplug_slot: slot to get status from
  176. * @value: returns with value of attention LED
  177. *
  178. * ACPI doesn't have known method to determine the state
  179. * of the attention status LED, so we use a callback that
  180. * was registered with us. This allows hardware specific
  181. * ACPI implementations to determine its state.
  182. */
  183. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  184. {
  185. int retval = -EINVAL;
  186. pr_debug("%s - physical_slot = %s\n", __func__,
  187. hotplug_slot_name(hotplug_slot));
  188. if (attention_info && try_module_get(attention_info->owner)) {
  189. retval = attention_info->get_attn(hotplug_slot, value);
  190. module_put(attention_info->owner);
  191. } else
  192. attention_info = NULL;
  193. return retval;
  194. }
  195. /**
  196. * get_latch_status - get latch 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 latch status.
  201. * Instead, we fake latch status from _STA.
  202. */
  203. static int get_latch_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_latch_status(slot->acpi_slot);
  208. return 0;
  209. }
  210. /**
  211. * get_adapter_status - get adapter status of a slot
  212. * @hotplug_slot: slot to get status
  213. * @value: pointer to store status
  214. *
  215. * ACPI doesn't provide any formal means to access adapter status.
  216. * Instead, we fake adapter status from _STA.
  217. */
  218. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  219. {
  220. struct slot *slot = hotplug_slot->private;
  221. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  222. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  223. return 0;
  224. }
  225. /**
  226. * release_slot - free up the memory used by a slot
  227. * @hotplug_slot: slot to free
  228. */
  229. static void release_slot(struct hotplug_slot *hotplug_slot)
  230. {
  231. struct slot *slot = hotplug_slot->private;
  232. pr_debug("%s - physical_slot = %s\n", __func__, slot_name(slot));
  233. kfree(slot->hotplug_slot);
  234. kfree(slot);
  235. }
  236. /* callback routine to initialize 'struct slot' for each slot */
  237. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot,
  238. unsigned int sun)
  239. {
  240. struct slot *slot;
  241. int retval = -ENOMEM;
  242. char name[SLOT_NAME_SIZE];
  243. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  244. if (!slot)
  245. goto error;
  246. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  247. if (!slot->hotplug_slot)
  248. goto error_slot;
  249. slot->hotplug_slot->info = &slot->info;
  250. slot->hotplug_slot->private = slot;
  251. slot->hotplug_slot->release = &release_slot;
  252. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  253. slot->acpi_slot = acpiphp_slot;
  254. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  255. slot->hotplug_slot->info->attention_status = 0;
  256. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  257. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  258. acpiphp_slot->slot = slot;
  259. slot->sun = sun;
  260. snprintf(name, SLOT_NAME_SIZE, "%u", sun);
  261. retval = pci_hp_register(slot->hotplug_slot, acpiphp_slot->bus,
  262. acpiphp_slot->device, name);
  263. if (retval == -EBUSY)
  264. goto error_hpslot;
  265. if (retval) {
  266. pr_err("pci_hp_register failed with error %d\n", retval);
  267. goto error_hpslot;
  268. }
  269. pr_info("Slot [%s] registered\n", slot_name(slot));
  270. return 0;
  271. error_hpslot:
  272. kfree(slot->hotplug_slot);
  273. error_slot:
  274. kfree(slot);
  275. error:
  276. return retval;
  277. }
  278. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  279. {
  280. struct slot *slot = acpiphp_slot->slot;
  281. int retval = 0;
  282. pr_info("Slot [%s] unregistered\n", slot_name(slot));
  283. retval = pci_hp_deregister(slot->hotplug_slot);
  284. if (retval)
  285. pr_err("pci_hp_deregister failed with error %d\n", retval);
  286. }
  287. void __init acpiphp_init(void)
  288. {
  289. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "%s\n",
  290. acpiphp_disabled ? ", disabled by user; please report a bug"
  291. : "");
  292. }