PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/pci/hotplug/acpiphp_core.c

https://bitbucket.org/danhamilt1/linux
C | 363 lines | 184 code | 66 blank | 113 comment | 18 complexity | a9552559738dddeaae97f84eb5c0fe2d MD5 | raw file
  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. int retval;
  137. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  138. /* disable the specified slot */
  139. retval = acpiphp_disable_slot(slot->acpi_slot);
  140. if (!retval)
  141. retval = acpiphp_eject_slot(slot->acpi_slot);
  142. return retval;
  143. }
  144. /**
  145. * set_attention_status - set attention LED
  146. * @hotplug_slot: slot to set attention LED on
  147. * @status: value to set attention LED to (0 or 1)
  148. *
  149. * attention status LED, so we use a callback that
  150. * was registered with us. This allows hardware specific
  151. * ACPI implementations to blink the light for us.
  152. */
  153. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  154. {
  155. int retval = -ENODEV;
  156. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  157. if (attention_info && try_module_get(attention_info->owner)) {
  158. retval = attention_info->set_attn(hotplug_slot, status);
  159. module_put(attention_info->owner);
  160. } else
  161. attention_info = NULL;
  162. return retval;
  163. }
  164. /**
  165. * get_power_status - get power status of a slot
  166. * @hotplug_slot: slot to get status
  167. * @value: pointer to store status
  168. *
  169. * Some platforms may not implement _STA method properly.
  170. * In that case, the value returned may not be reliable.
  171. */
  172. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  173. {
  174. struct slot *slot = hotplug_slot->private;
  175. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  176. *value = acpiphp_get_power_status(slot->acpi_slot);
  177. return 0;
  178. }
  179. /**
  180. * get_attention_status - get attention LED status
  181. * @hotplug_slot: slot to get status from
  182. * @value: returns with value of attention LED
  183. *
  184. * ACPI doesn't have known method to determine the state
  185. * of the attention status LED, so we use a callback that
  186. * was registered with us. This allows hardware specific
  187. * ACPI implementations to determine its state.
  188. */
  189. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  190. {
  191. int retval = -EINVAL;
  192. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  193. if (attention_info && try_module_get(attention_info->owner)) {
  194. retval = attention_info->get_attn(hotplug_slot, value);
  195. module_put(attention_info->owner);
  196. } else
  197. attention_info = NULL;
  198. return retval;
  199. }
  200. /**
  201. * get_latch_status - get latch status of a slot
  202. * @hotplug_slot: slot to get status
  203. * @value: pointer to store status
  204. *
  205. * ACPI doesn't provide any formal means to access latch status.
  206. * Instead, we fake latch status from _STA.
  207. */
  208. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  209. {
  210. struct slot *slot = hotplug_slot->private;
  211. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  212. *value = acpiphp_get_latch_status(slot->acpi_slot);
  213. return 0;
  214. }
  215. /**
  216. * get_adapter_status - get adapter status of a slot
  217. * @hotplug_slot: slot to get status
  218. * @value: pointer to store status
  219. *
  220. * ACPI doesn't provide any formal means to access adapter status.
  221. * Instead, we fake adapter status from _STA.
  222. */
  223. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  224. {
  225. struct slot *slot = hotplug_slot->private;
  226. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  227. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  228. return 0;
  229. }
  230. /**
  231. * release_slot - free up the memory used by a slot
  232. * @hotplug_slot: slot to free
  233. */
  234. static void release_slot(struct hotplug_slot *hotplug_slot)
  235. {
  236. struct slot *slot = hotplug_slot->private;
  237. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  238. kfree(slot->hotplug_slot);
  239. kfree(slot);
  240. }
  241. /* callback routine to initialize 'struct slot' for each slot */
  242. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  243. {
  244. struct slot *slot;
  245. int retval = -ENOMEM;
  246. char name[SLOT_NAME_SIZE];
  247. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  248. if (!slot)
  249. goto error;
  250. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  251. if (!slot->hotplug_slot)
  252. goto error_slot;
  253. slot->hotplug_slot->info = &slot->info;
  254. slot->hotplug_slot->private = slot;
  255. slot->hotplug_slot->release = &release_slot;
  256. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  257. slot->acpi_slot = acpiphp_slot;
  258. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  259. slot->hotplug_slot->info->attention_status = 0;
  260. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  261. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  262. acpiphp_slot->slot = slot;
  263. snprintf(name, SLOT_NAME_SIZE, "%llu", slot->acpi_slot->sun);
  264. retval = pci_hp_register(slot->hotplug_slot,
  265. acpiphp_slot->bridge->pci_bus,
  266. acpiphp_slot->device,
  267. name);
  268. if (retval == -EBUSY)
  269. goto error_hpslot;
  270. if (retval) {
  271. err("pci_hp_register failed with error %d\n", retval);
  272. goto error_hpslot;
  273. }
  274. info("Slot [%s] registered\n", slot_name(slot));
  275. return 0;
  276. error_hpslot:
  277. kfree(slot->hotplug_slot);
  278. error_slot:
  279. kfree(slot);
  280. error:
  281. return retval;
  282. }
  283. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  284. {
  285. struct slot *slot = acpiphp_slot->slot;
  286. int retval = 0;
  287. info("Slot [%s] unregistered\n", slot_name(slot));
  288. retval = pci_hp_deregister(slot->hotplug_slot);
  289. if (retval)
  290. err("pci_hp_deregister failed with error %d\n", retval);
  291. }
  292. void __init acpiphp_init(void)
  293. {
  294. info(DRIVER_DESC " version: " DRIVER_VERSION "%s\n",
  295. acpiphp_disabled ? ", disabled by user; please report a bug"
  296. : "");
  297. }