acpiphp_core.c 10 KB

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