acpiphp_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <gregkh@us.ibm.com>,
  31. * <t-kochi@bq.jp.nec.com>
  32. *
  33. */
  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/slab.h>
  40. #include <linux/smp.h>
  41. #include <linux/smp_lock.h>
  42. #include "pci_hotplug.h"
  43. #include "acpiphp.h"
  44. #define MY_NAME "acpiphp"
  45. static int 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_address (struct hotplug_slot *slot, u32 *value);
  67. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  68. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  69. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  70. .owner = THIS_MODULE,
  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. .get_address = get_address,
  79. };
  80. /**
  81. * acpiphp_register_attention - set attention LED callback
  82. * @info: must be completely filled with LED callbacks
  83. *
  84. * Description: this is used to register a hardware specific ACPI
  85. * driver that manipulates the attention LED. All the fields in
  86. * info must be set.
  87. **/
  88. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  89. {
  90. int retval = -EINVAL;
  91. if (info && info->owner && info->set_attn &&
  92. info->get_attn && !attention_info) {
  93. retval = 0;
  94. attention_info = info;
  95. }
  96. return retval;
  97. }
  98. /**
  99. * acpiphp_unregister_attention - unset attention LED callback
  100. * @info: must match the pointer used to register
  101. *
  102. * Description: this is used to un-register a hardware specific acpi
  103. * driver that manipulates the attention LED. The pointer to the
  104. * info struct must be the same as the one used to set it.
  105. **/
  106. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  107. {
  108. int retval = -EINVAL;
  109. if (info && attention_info == info) {
  110. attention_info = NULL;
  111. retval = 0;
  112. }
  113. return retval;
  114. }
  115. /**
  116. * enable_slot - power on and enable a slot
  117. * @hotplug_slot: slot to enable
  118. *
  119. * Actual tasks are done in acpiphp_enable_slot()
  120. *
  121. */
  122. static int enable_slot(struct hotplug_slot *hotplug_slot)
  123. {
  124. struct slot *slot = hotplug_slot->private;
  125. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  126. /* enable the specified slot */
  127. return acpiphp_enable_slot(slot->acpi_slot);
  128. }
  129. /**
  130. * disable_slot - disable and power off a slot
  131. * @hotplug_slot: slot to disable
  132. *
  133. * Actual tasks are done in acpiphp_disable_slot()
  134. *
  135. */
  136. static int disable_slot(struct hotplug_slot *hotplug_slot)
  137. {
  138. struct slot *slot = hotplug_slot->private;
  139. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  140. /* disable the specified slot */
  141. return acpiphp_disable_slot(slot->acpi_slot);
  142. }
  143. /**
  144. * set_attention_status - set attention LED
  145. * @hotplug_slot: slot to set attention LED on
  146. * @status: value to set attention LED to (0 or 1)
  147. *
  148. * attention status LED, so we use a callback that
  149. * was registered with us. This allows hardware specific
  150. * ACPI implementations to blink the light for us.
  151. **/
  152. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  153. {
  154. int retval = -ENODEV;
  155. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  156. if (attention_info && try_module_get(attention_info->owner)) {
  157. retval = attention_info->set_attn(hotplug_slot, status);
  158. module_put(attention_info->owner);
  159. } else
  160. attention_info = NULL;
  161. return retval;
  162. }
  163. /**
  164. * get_power_status - get power status of a slot
  165. * @hotplug_slot: slot to get status
  166. * @value: pointer to store status
  167. *
  168. * Some platforms may not implement _STA method properly.
  169. * In that case, the value returned may not be reliable.
  170. *
  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", __FUNCTION__, hotplug_slot->name);
  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", __FUNCTION__, hotplug_slot->name);
  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. */
  209. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  210. {
  211. struct slot *slot = hotplug_slot->private;
  212. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  213. *value = acpiphp_get_latch_status(slot->acpi_slot);
  214. return 0;
  215. }
  216. /**
  217. * get_adapter_status - get adapter status of a slot
  218. * @hotplug_slot: slot to get status
  219. * @value: pointer to store status
  220. *
  221. * ACPI doesn't provide any formal means to access adapter status.
  222. * Instead, we fake adapter status from _STA
  223. *
  224. */
  225. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  226. {
  227. struct slot *slot = hotplug_slot->private;
  228. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  229. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  230. return 0;
  231. }
  232. /**
  233. * get_address - get pci address of a slot
  234. * @hotplug_slot: slot to get status
  235. * @value: pointer to struct pci_busdev (seg, bus, dev)
  236. */
  237. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  238. {
  239. struct slot *slot = hotplug_slot->private;
  240. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  241. *value = acpiphp_get_address(slot->acpi_slot);
  242. return 0;
  243. }
  244. static int __init init_acpi(void)
  245. {
  246. int retval;
  247. /* initialize internal data structure etc. */
  248. retval = acpiphp_glue_init();
  249. /* read initial number of slots */
  250. if (!retval) {
  251. num_slots = acpiphp_get_num_slots();
  252. if (num_slots == 0)
  253. retval = -ENODEV;
  254. }
  255. return retval;
  256. }
  257. /**
  258. * make_slot_name - make a slot name that appears in pcihpfs
  259. * @slot: slot to name
  260. *
  261. */
  262. static void make_slot_name(struct slot *slot)
  263. {
  264. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%u",
  265. slot->acpi_slot->sun);
  266. }
  267. /**
  268. * release_slot - free up the memory used by a slot
  269. * @hotplug_slot: slot to free
  270. */
  271. static void release_slot(struct hotplug_slot *hotplug_slot)
  272. {
  273. struct slot *slot = hotplug_slot->private;
  274. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  275. kfree(slot->hotplug_slot->info);
  276. kfree(slot->hotplug_slot->name);
  277. kfree(slot->hotplug_slot);
  278. kfree(slot);
  279. }
  280. /* callback routine to initialize 'struct slot' for each slot */
  281. int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  282. {
  283. struct slot *slot;
  284. struct hotplug_slot *hotplug_slot;
  285. struct hotplug_slot_info *hotplug_slot_info;
  286. int retval = -ENOMEM;
  287. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  288. if (!slot)
  289. goto error;
  290. slot->hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  291. if (!slot->hotplug_slot)
  292. goto error_slot;
  293. slot->hotplug_slot->info = kzalloc(sizeof(*hotplug_slot_info),
  294. GFP_KERNEL);
  295. if (!slot->hotplug_slot->info)
  296. goto error_hpslot;
  297. slot->hotplug_slot->name = kzalloc(SLOT_NAME_SIZE, GFP_KERNEL);
  298. if (!slot->hotplug_slot->name)
  299. goto error_info;
  300. slot->hotplug_slot->private = slot;
  301. slot->hotplug_slot->release = &release_slot;
  302. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  303. slot->acpi_slot = acpiphp_slot;
  304. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  305. slot->hotplug_slot->info->attention_status = 0;
  306. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  307. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  308. slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
  309. slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
  310. acpiphp_slot->slot = slot;
  311. make_slot_name(slot);
  312. retval = pci_hp_register(slot->hotplug_slot);
  313. if (retval) {
  314. err("pci_hp_register failed with error %d\n", retval);
  315. goto error_name;
  316. }
  317. info("Slot [%s] registered\n", slot->hotplug_slot->name);
  318. return 0;
  319. error_name:
  320. kfree(slot->hotplug_slot->name);
  321. error_info:
  322. kfree(slot->hotplug_slot->info);
  323. error_hpslot:
  324. kfree(slot->hotplug_slot);
  325. error_slot:
  326. kfree(slot);
  327. error:
  328. return retval;
  329. }
  330. void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
  331. {
  332. struct slot *slot = acpiphp_slot->slot;
  333. int retval = 0;
  334. info ("Slot [%s] unregistered\n", slot->hotplug_slot->name);
  335. retval = pci_hp_deregister(slot->hotplug_slot);
  336. if (retval)
  337. err("pci_hp_deregister failed with error %d\n", retval);
  338. }
  339. static int __init acpiphp_init(void)
  340. {
  341. int retval;
  342. int docking_station;
  343. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  344. acpiphp_debug = debug;
  345. docking_station = find_dock_station();
  346. /* read all the ACPI info from the system */
  347. retval = init_acpi();
  348. /* if we have found a docking station, we should
  349. * go ahead and load even if init_acpi has found
  350. * no slots. This handles the case when the _DCK
  351. * method not defined under the actual dock bridge
  352. */
  353. if (docking_station)
  354. return 0;
  355. else
  356. return retval;
  357. }
  358. static void __exit acpiphp_exit(void)
  359. {
  360. /* deallocate internal data structures etc. */
  361. acpiphp_glue_exit();
  362. remove_dock_station();
  363. }
  364. module_init(acpiphp_init);
  365. module_exit(acpiphp_exit);