acpiphp_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_hotplug.h>
  39. #include <linux/slab.h>
  40. #include <linux/smp.h>
  41. #include "acpiphp.h"
  42. #define MY_NAME "acpiphp"
  43. /* name size which is used for entries in pcihpfs */
  44. #define SLOT_NAME_SIZE 21 /* {_SUN} */
  45. static bool debug;
  46. int acpiphp_debug;
  47. /* local variables */
  48. static int num_slots;
  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_param(debug, bool, 0644);
  58. /* export the attention callback registration methods */
  59. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  60. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  61. static int enable_slot (struct hotplug_slot *slot);
  62. static int disable_slot (struct hotplug_slot *slot);
  63. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  64. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  67. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  68. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  69. .enable_slot = enable_slot,
  70. .disable_slot = disable_slot,
  71. .set_attention_status = set_attention_status,
  72. .get_power_status = get_power_status,
  73. .get_attention_status = get_attention_status,
  74. .get_latch_status = get_latch_status,
  75. .get_adapter_status = get_adapter_status,
  76. };
  77. /**
  78. * acpiphp_register_attention - set attention LED callback
  79. * @info: must be completely filled with LED callbacks
  80. *
  81. * Description: This is used to register a hardware specific ACPI
  82. * driver that manipulates the attention LED. All the fields in
  83. * info must be set.
  84. */
  85. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  86. {
  87. int retval = -EINVAL;
  88. if (info && info->owner && info->set_attn &&
  89. info->get_attn && !attention_info) {
  90. retval = 0;
  91. attention_info = info;
  92. }
  93. return retval;
  94. }
  95. /**
  96. * acpiphp_unregister_attention - unset attention LED callback
  97. * @info: must match the pointer used to register
  98. *
  99. * Description: This is used to un-register a hardware specific acpi
  100. * driver that manipulates the attention LED. The pointer to the
  101. * info struct must be the same as the one used to set it.
  102. */
  103. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  104. {
  105. int retval = -EINVAL;
  106. if (info && attention_info == info) {
  107. attention_info = NULL;
  108. retval = 0;
  109. }
  110. return retval;
  111. }
  112. /**
  113. * enable_slot - power on and enable a slot
  114. * @hotplug_slot: slot to enable
  115. *
  116. * Actual tasks are done in acpiphp_enable_slot()
  117. */
  118. static int enable_slot(struct hotplug_slot *hotplug_slot)
  119. {
  120. struct slot *slot = hotplug_slot->private;
  121. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  122. /* enable the specified slot */
  123. return acpiphp_enable_slot(slot->acpi_slot);
  124. }
  125. /**
  126. * disable_slot - disable and power off a slot
  127. * @hotplug_slot: slot to disable
  128. *
  129. * Actual tasks are done in acpiphp_disable_slot()
  130. */
  131. static int disable_slot(struct hotplug_slot *hotplug_slot)
  132. {
  133. struct slot *slot = hotplug_slot->private;
  134. int retval;
  135. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  136. /* disable the specified slot */
  137. retval = acpiphp_disable_slot(slot->acpi_slot);
  138. if (!retval)
  139. retval = acpiphp_eject_slot(slot->acpi_slot);
  140. return retval;
  141. }
  142. /**
  143. * set_attention_status - set attention LED
  144. * @hotplug_slot: slot to set attention LED on
  145. * @status: value to set attention LED to (0 or 1)
  146. *
  147. * attention status LED, so we use a callback that
  148. * was registered with us. This allows hardware specific
  149. * ACPI implementations to blink the light for us.
  150. */
  151. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  152. {
  153. int retval = -ENODEV;
  154. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  155. if (attention_info && try_module_get(attention_info->owner)) {
  156. retval = attention_info->set_attn(hotplug_slot, status);
  157. module_put(attention_info->owner);
  158. } else
  159. attention_info = NULL;
  160. return retval;
  161. }
  162. /**
  163. * get_power_status - get power status of a slot
  164. * @hotplug_slot: slot to get status
  165. * @value: pointer to store status
  166. *
  167. * Some platforms may not implement _STA method properly.
  168. * In that case, the value returned may not be reliable.
  169. */
  170. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  171. {
  172. struct slot *slot = hotplug_slot->private;
  173. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  174. *value = acpiphp_get_power_status(slot->acpi_slot);
  175. return 0;
  176. }
  177. /**
  178. * get_attention_status - get attention LED status
  179. * @hotplug_slot: slot to get status from
  180. * @value: returns with value of attention LED
  181. *
  182. * ACPI doesn't have known method to determine the state
  183. * of the attention status LED, so we use a callback that
  184. * was registered with us. This allows hardware specific
  185. * ACPI implementations to determine its state.
  186. */
  187. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  188. {
  189. int retval = -EINVAL;
  190. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  191. if (attention_info && try_module_get(attention_info->owner)) {
  192. retval = attention_info->get_attn(hotplug_slot, value);
  193. module_put(attention_info->owner);
  194. } else
  195. attention_info = NULL;
  196. return retval;
  197. }
  198. /**
  199. * get_latch_status - get latch status of a slot
  200. * @hotplug_slot: slot to get status
  201. * @value: pointer to store status
  202. *
  203. * ACPI doesn't provide any formal means to access latch status.
  204. * Instead, we fake latch status from _STA.
  205. */
  206. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  207. {
  208. struct slot *slot = hotplug_slot->private;
  209. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  210. *value = acpiphp_get_latch_status(slot->acpi_slot);
  211. return 0;
  212. }
  213. /**
  214. * get_adapter_status - get adapter status of a slot
  215. * @hotplug_slot: slot to get status
  216. * @value: pointer to store status
  217. *
  218. * ACPI doesn't provide any formal means to access adapter status.
  219. * Instead, we fake adapter status from _STA.
  220. */
  221. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  222. {
  223. struct slot *slot = hotplug_slot->private;
  224. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  225. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  226. return 0;
  227. }
  228. static int __init init_acpi(void)
  229. {
  230. int retval;
  231. /* initialize internal data structure etc. */
  232. retval = acpiphp_glue_init();
  233. /* read initial number of slots */
  234. if (!retval) {
  235. num_slots = acpiphp_get_num_slots();
  236. if (num_slots == 0) {
  237. acpiphp_glue_exit();
  238. retval = -ENODEV;
  239. }
  240. }
  241. return retval;
  242. }
  243. /**
  244. * release_slot - free up the memory used by a slot
  245. * @hotplug_slot: slot to free
  246. */
  247. static void release_slot(struct hotplug_slot *hotplug_slot)
  248. {
  249. struct slot *slot = hotplug_slot->private;
  250. dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
  251. kfree(slot->hotplug_slot);
  252. kfree(slot);
  253. }
  254. /* callback routine to initialize 'struct slot' for each slot */
  255. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  256. {
  257. struct slot *slot;
  258. int retval = -ENOMEM;
  259. char name[SLOT_NAME_SIZE];
  260. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  261. if (!slot)
  262. goto error;
  263. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  264. if (!slot->hotplug_slot)
  265. goto error_slot;
  266. slot->hotplug_slot->info = &slot->info;
  267. slot->hotplug_slot->private = slot;
  268. slot->hotplug_slot->release = &release_slot;
  269. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  270. slot->acpi_slot = acpiphp_slot;
  271. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  272. slot->hotplug_slot->info->attention_status = 0;
  273. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  274. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  275. acpiphp_slot->slot = slot;
  276. snprintf(name, SLOT_NAME_SIZE, "%llu", slot->acpi_slot->sun);
  277. retval = pci_hp_register(slot->hotplug_slot,
  278. acpiphp_slot->bridge->pci_bus,
  279. acpiphp_slot->device,
  280. name);
  281. if (retval == -EBUSY)
  282. goto error_hpslot;
  283. if (retval) {
  284. err("pci_hp_register failed with error %d\n", retval);
  285. goto error_hpslot;
  286. }
  287. info("Slot [%s] registered\n", slot_name(slot));
  288. return 0;
  289. error_hpslot:
  290. kfree(slot->hotplug_slot);
  291. error_slot:
  292. kfree(slot);
  293. error:
  294. return retval;
  295. }
  296. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  297. {
  298. struct slot *slot = acpiphp_slot->slot;
  299. int retval = 0;
  300. info("Slot [%s] unregistered\n", slot_name(slot));
  301. retval = pci_hp_deregister(slot->hotplug_slot);
  302. if (retval)
  303. err("pci_hp_deregister failed with error %d\n", retval);
  304. }
  305. static int __init acpiphp_init(void)
  306. {
  307. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  308. if (acpi_pci_disabled)
  309. return 0;
  310. acpiphp_debug = debug;
  311. /* read all the ACPI info from the system */
  312. return init_acpi();
  313. }
  314. static void __exit acpiphp_exit(void)
  315. {
  316. if (acpi_pci_disabled)
  317. return;
  318. /* deallocate internal data structures etc. */
  319. acpiphp_glue_exit();
  320. }
  321. module_init(acpiphp_init);
  322. module_exit(acpiphp_exit);