acpiphp_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. static int debug;
  44. int acpiphp_debug;
  45. /* local variables */
  46. static int num_slots;
  47. static struct acpiphp_attention_info *attention_info;
  48. #define DRIVER_VERSION "0.5"
  49. #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>, Matthew Wilcox <willy@hp.com>"
  50. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver"
  51. MODULE_AUTHOR(DRIVER_AUTHOR);
  52. MODULE_DESCRIPTION(DRIVER_DESC);
  53. MODULE_LICENSE("GPL");
  54. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  55. module_param(debug, bool, 0644);
  56. /* export the attention callback registration methods */
  57. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  58. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  59. static int enable_slot (struct hotplug_slot *slot);
  60. static int disable_slot (struct hotplug_slot *slot);
  61. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  62. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_address (struct hotplug_slot *slot, u32 *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. .owner = THIS_MODULE,
  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. .get_address = get_address,
  77. };
  78. /**
  79. * acpiphp_register_attention - set attention LED callback
  80. * @info: must be completely filled with LED callbacks
  81. *
  82. * Description: this is used to register a hardware specific ACPI
  83. * driver that manipulates the attention LED. All the fields in
  84. * info must be set.
  85. **/
  86. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  87. {
  88. int retval = -EINVAL;
  89. if (info && info->owner && info->set_attn &&
  90. info->get_attn && !attention_info) {
  91. retval = 0;
  92. attention_info = info;
  93. }
  94. return retval;
  95. }
  96. /**
  97. * acpiphp_unregister_attention - unset attention LED callback
  98. * @info: must match the pointer used to register
  99. *
  100. * Description: this is used to un-register a hardware specific acpi
  101. * driver that manipulates the attention LED. The pointer to the
  102. * info struct must be the same as the one used to set it.
  103. **/
  104. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  105. {
  106. int retval = -EINVAL;
  107. if (info && attention_info == info) {
  108. attention_info = NULL;
  109. retval = 0;
  110. }
  111. return retval;
  112. }
  113. /**
  114. * enable_slot - power on and enable a slot
  115. * @hotplug_slot: slot to enable
  116. *
  117. * Actual tasks are done in acpiphp_enable_slot()
  118. *
  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", __FUNCTION__, hotplug_slot->name);
  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. */
  134. static int disable_slot(struct hotplug_slot *hotplug_slot)
  135. {
  136. struct slot *slot = hotplug_slot->private;
  137. int retval;
  138. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  139. /* disable the specified slot */
  140. retval = acpiphp_disable_slot(slot->acpi_slot);
  141. if (!retval)
  142. retval = acpiphp_eject_slot(slot->acpi_slot);
  143. return retval;
  144. }
  145. /**
  146. * set_attention_status - set attention LED
  147. * @hotplug_slot: slot to set attention LED on
  148. * @status: value to set attention LED to (0 or 1)
  149. *
  150. * attention status LED, so we use a callback that
  151. * was registered with us. This allows hardware specific
  152. * ACPI implementations to blink the light for us.
  153. **/
  154. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  155. {
  156. int retval = -ENODEV;
  157. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  158. if (attention_info && try_module_get(attention_info->owner)) {
  159. retval = attention_info->set_attn(hotplug_slot, status);
  160. module_put(attention_info->owner);
  161. } else
  162. attention_info = NULL;
  163. return retval;
  164. }
  165. /**
  166. * get_power_status - get power status of a slot
  167. * @hotplug_slot: slot to get status
  168. * @value: pointer to store status
  169. *
  170. * Some platforms may not implement _STA method properly.
  171. * In that case, the value returned may not be reliable.
  172. *
  173. */
  174. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  175. {
  176. struct slot *slot = hotplug_slot->private;
  177. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  178. *value = acpiphp_get_power_status(slot->acpi_slot);
  179. return 0;
  180. }
  181. /**
  182. * get_attention_status - get attention LED status
  183. * @hotplug_slot: slot to get status from
  184. * @value: returns with value of attention LED
  185. *
  186. * ACPI doesn't have known method to determine the state
  187. * of the attention status LED, so we use a callback that
  188. * was registered with us. This allows hardware specific
  189. * ACPI implementations to determine its state
  190. **/
  191. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  192. {
  193. int retval = -EINVAL;
  194. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  195. if (attention_info && try_module_get(attention_info->owner)) {
  196. retval = attention_info->get_attn(hotplug_slot, value);
  197. module_put(attention_info->owner);
  198. } else
  199. attention_info = NULL;
  200. return retval;
  201. }
  202. /**
  203. * get_latch_status - get latch status of a slot
  204. * @hotplug_slot: slot to get status
  205. * @value: pointer to store status
  206. *
  207. * ACPI doesn't provide any formal means to access latch status.
  208. * Instead, we fake latch status from _STA
  209. *
  210. */
  211. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  212. {
  213. struct slot *slot = hotplug_slot->private;
  214. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  215. *value = acpiphp_get_latch_status(slot->acpi_slot);
  216. return 0;
  217. }
  218. /**
  219. * get_adapter_status - get adapter status of a slot
  220. * @hotplug_slot: slot to get status
  221. * @value: pointer to store status
  222. *
  223. * ACPI doesn't provide any formal means to access adapter status.
  224. * Instead, we fake adapter status from _STA
  225. *
  226. */
  227. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  228. {
  229. struct slot *slot = hotplug_slot->private;
  230. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  231. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  232. return 0;
  233. }
  234. /**
  235. * get_address - get pci address of a slot
  236. * @hotplug_slot: slot to get status
  237. * @value: pointer to struct pci_busdev (seg, bus, dev)
  238. */
  239. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  240. {
  241. struct slot *slot = hotplug_slot->private;
  242. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  243. *value = acpiphp_get_address(slot->acpi_slot);
  244. return 0;
  245. }
  246. static int __init init_acpi(void)
  247. {
  248. int retval;
  249. /* initialize internal data structure etc. */
  250. retval = acpiphp_glue_init();
  251. /* read initial number of slots */
  252. if (!retval) {
  253. num_slots = acpiphp_get_num_slots();
  254. if (num_slots == 0) {
  255. acpiphp_glue_exit();
  256. retval = -ENODEV;
  257. }
  258. }
  259. return retval;
  260. }
  261. /**
  262. * release_slot - free up the memory used by a slot
  263. * @hotplug_slot: slot to free
  264. */
  265. static void release_slot(struct hotplug_slot *hotplug_slot)
  266. {
  267. struct slot *slot = hotplug_slot->private;
  268. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  269. kfree(slot->hotplug_slot);
  270. kfree(slot);
  271. }
  272. /* callback routine to initialize 'struct slot' for each slot */
  273. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  274. {
  275. struct slot *slot;
  276. int retval = -ENOMEM;
  277. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  278. if (!slot)
  279. goto error;
  280. slot->hotplug_slot = kzalloc(sizeof(*slot->hotplug_slot), GFP_KERNEL);
  281. if (!slot->hotplug_slot)
  282. goto error_slot;
  283. slot->hotplug_slot->info = &slot->info;
  284. slot->hotplug_slot->name = slot->name;
  285. slot->hotplug_slot->private = slot;
  286. slot->hotplug_slot->release = &release_slot;
  287. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  288. slot->acpi_slot = acpiphp_slot;
  289. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  290. slot->hotplug_slot->info->attention_status = 0;
  291. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  292. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  293. slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
  294. slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
  295. acpiphp_slot->slot = slot;
  296. snprintf(slot->name, sizeof(slot->name), "%u", slot->acpi_slot->sun);
  297. retval = pci_hp_register(slot->hotplug_slot);
  298. if (retval) {
  299. err("pci_hp_register failed with error %d\n", retval);
  300. goto error_hpslot;
  301. }
  302. info("Slot [%s] registered\n", slot->hotplug_slot->name);
  303. return 0;
  304. error_hpslot:
  305. kfree(slot->hotplug_slot);
  306. error_slot:
  307. kfree(slot);
  308. error:
  309. return retval;
  310. }
  311. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  312. {
  313. struct slot *slot = acpiphp_slot->slot;
  314. int retval = 0;
  315. info ("Slot [%s] unregistered\n", slot->hotplug_slot->name);
  316. retval = pci_hp_deregister(slot->hotplug_slot);
  317. if (retval)
  318. err("pci_hp_deregister failed with error %d\n", retval);
  319. }
  320. static int __init acpiphp_init(void)
  321. {
  322. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  323. acpiphp_debug = debug;
  324. /* read all the ACPI info from the system */
  325. return init_acpi();
  326. }
  327. static void __exit acpiphp_exit(void)
  328. {
  329. /* deallocate internal data structures etc. */
  330. acpiphp_glue_exit();
  331. }
  332. module_init(acpiphp_init);
  333. module_exit(acpiphp_exit);