pcihp_skeleton.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * PCI Hot Plug Controller Skeleton Driver - 0.3
  3. *
  4. * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2001,2003 IBM Corp.
  6. *
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  17. * NON INFRINGEMENT. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * This driver is to be used as a skeleton driver to show how to interface
  25. * with the pci hotplug core easily.
  26. *
  27. * Send feedback to <greg@kroah.com>
  28. *
  29. */
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/pci.h>
  35. #include <linux/pci_hotplug.h>
  36. #include <linux/init.h>
  37. #define SLOT_NAME_SIZE 10
  38. struct slot {
  39. u8 number;
  40. struct hotplug_slot *hotplug_slot;
  41. struct list_head slot_list;
  42. char name[SLOT_NAME_SIZE];
  43. };
  44. static LIST_HEAD(slot_list);
  45. #define MY_NAME "pcihp_skeleton"
  46. #define dbg(format, arg...) \
  47. do { \
  48. if (debug) \
  49. printk (KERN_DEBUG "%s: " format "\n", \
  50. MY_NAME , ## arg); \
  51. } while (0)
  52. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  53. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  54. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  55. /* local variables */
  56. static int debug;
  57. static int num_slots;
  58. #define DRIVER_VERSION "0.3"
  59. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  60. #define DRIVER_DESC "Hot Plug PCI Controller Skeleton Driver"
  61. MODULE_AUTHOR(DRIVER_AUTHOR);
  62. MODULE_DESCRIPTION(DRIVER_DESC);
  63. MODULE_LICENSE("GPL");
  64. module_param(debug, bool, 0644);
  65. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  66. static int enable_slot (struct hotplug_slot *slot);
  67. static int disable_slot (struct hotplug_slot *slot);
  68. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  69. static int hardware_test (struct hotplug_slot *slot, u32 value);
  70. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  71. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  72. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  73. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  74. static struct hotplug_slot_ops skel_hotplug_slot_ops = {
  75. .owner = THIS_MODULE,
  76. .enable_slot = enable_slot,
  77. .disable_slot = disable_slot,
  78. .set_attention_status = set_attention_status,
  79. .hardware_test = hardware_test,
  80. .get_power_status = get_power_status,
  81. .get_attention_status = get_attention_status,
  82. .get_latch_status = get_latch_status,
  83. .get_adapter_status = get_adapter_status,
  84. };
  85. static int enable_slot(struct hotplug_slot *hotplug_slot)
  86. {
  87. struct slot *slot = hotplug_slot->private;
  88. int retval = 0;
  89. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  90. /*
  91. * Fill in code here to enable the specified slot
  92. */
  93. return retval;
  94. }
  95. static int disable_slot(struct hotplug_slot *hotplug_slot)
  96. {
  97. struct slot *slot = hotplug_slot->private;
  98. int retval = 0;
  99. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  100. /*
  101. * Fill in code here to disable the specified slot
  102. */
  103. return retval;
  104. }
  105. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  106. {
  107. struct slot *slot = hotplug_slot->private;
  108. int retval = 0;
  109. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  110. switch (status) {
  111. case 0:
  112. /*
  113. * Fill in code here to turn light off
  114. */
  115. break;
  116. case 1:
  117. default:
  118. /*
  119. * Fill in code here to turn light on
  120. */
  121. break;
  122. }
  123. return retval;
  124. }
  125. static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value)
  126. {
  127. struct slot *slot = hotplug_slot->private;
  128. int retval = 0;
  129. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  130. switch (value) {
  131. case 0:
  132. /* Specify a test here */
  133. break;
  134. case 1:
  135. /* Specify another test here */
  136. break;
  137. }
  138. return retval;
  139. }
  140. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  141. {
  142. struct slot *slot = hotplug_slot->private;
  143. int retval = 0;
  144. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  145. /*
  146. * Fill in logic to get the current power status of the specific
  147. * slot and store it in the *value location.
  148. */
  149. return retval;
  150. }
  151. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  152. {
  153. struct slot *slot = hotplug_slot->private;
  154. int retval = 0;
  155. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  156. /*
  157. * Fill in logic to get the current attention status of the specific
  158. * slot and store it in the *value location.
  159. */
  160. return retval;
  161. }
  162. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  163. {
  164. struct slot *slot = hotplug_slot->private;
  165. int retval = 0;
  166. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  167. /*
  168. * Fill in logic to get the current latch status of the specific
  169. * slot and store it in the *value location.
  170. */
  171. return retval;
  172. }
  173. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  174. {
  175. struct slot *slot = hotplug_slot->private;
  176. int retval = 0;
  177. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  178. /*
  179. * Fill in logic to get the current adapter status of the specific
  180. * slot and store it in the *value location.
  181. */
  182. return retval;
  183. }
  184. static void release_slot(struct hotplug_slot *hotplug_slot)
  185. {
  186. struct slot *slot = hotplug_slot->private;
  187. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  188. kfree(slot->hotplug_slot->info);
  189. kfree(slot->hotplug_slot);
  190. kfree(slot);
  191. }
  192. static void make_slot_name(struct slot *slot)
  193. {
  194. /*
  195. * Stupid way to make a filename out of the slot name.
  196. * replace this if your hardware provides a better way to name slots.
  197. */
  198. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d", slot->number);
  199. }
  200. /**
  201. * init_slots - initialize 'struct slot' structures for each slot
  202. *
  203. */
  204. static int __init init_slots(void)
  205. {
  206. struct slot *slot;
  207. struct hotplug_slot *hotplug_slot;
  208. struct hotplug_slot_info *info;
  209. int retval = -ENOMEM;
  210. int i;
  211. /*
  212. * Create a structure for each slot, and register that slot
  213. * with the pci_hotplug subsystem.
  214. */
  215. for (i = 0; i < num_slots; ++i) {
  216. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  217. if (!slot)
  218. goto error;
  219. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  220. if (!hotplug_slot)
  221. goto error_slot;
  222. slot->hotplug_slot = hotplug_slot;
  223. info = kzalloc(sizeof(*info), GFP_KERNEL);
  224. if (!info)
  225. goto error_hpslot;
  226. hotplug_slot->info = info;
  227. slot->number = i;
  228. hotplug_slot->name = slot->name;
  229. hotplug_slot->private = slot;
  230. hotplug_slot->release = &release_slot;
  231. make_slot_name(slot);
  232. hotplug_slot->ops = &skel_hotplug_slot_ops;
  233. /*
  234. * Initialize the slot info structure with some known
  235. * good values.
  236. */
  237. get_power_status(hotplug_slot, &info->power_status);
  238. get_attention_status(hotplug_slot, &info->attention_status);
  239. get_latch_status(hotplug_slot, &info->latch_status);
  240. get_adapter_status(hotplug_slot, &info->adapter_status);
  241. dbg("registering slot %d\n", i);
  242. retval = pci_hp_register(slot->hotplug_slot);
  243. if (retval) {
  244. err("pci_hp_register failed with error %d\n", retval);
  245. goto error_info;
  246. }
  247. /* add slot to our internal list */
  248. list_add(&slot->slot_list, &slot_list);
  249. }
  250. return 0;
  251. error_info:
  252. kfree(info);
  253. error_hpslot:
  254. kfree(hotplug_slot);
  255. error_slot:
  256. kfree(slot);
  257. error:
  258. return retval;
  259. }
  260. static void __exit cleanup_slots(void)
  261. {
  262. struct list_head *tmp;
  263. struct list_head *next;
  264. struct slot *slot;
  265. /*
  266. * Unregister all of our slots with the pci_hotplug subsystem.
  267. * Memory will be freed in release_slot() callback after slot's
  268. * lifespan is finished.
  269. */
  270. list_for_each_safe(tmp, next, &slot_list) {
  271. slot = list_entry(tmp, struct slot, slot_list);
  272. list_del(&slot->slot_list);
  273. pci_hp_deregister(slot->hotplug_slot);
  274. }
  275. }
  276. static int __init pcihp_skel_init(void)
  277. {
  278. int retval;
  279. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  280. /*
  281. * Do specific initialization stuff for your driver here
  282. * like initializing your controller hardware (if any) and
  283. * determining the number of slots you have in the system
  284. * right now.
  285. */
  286. num_slots = 5;
  287. return init_slots();
  288. }
  289. static void __exit pcihp_skel_exit(void)
  290. {
  291. /*
  292. * Clean everything up.
  293. */
  294. cleanup_slots();
  295. }
  296. module_init(pcihp_skel_init);
  297. module_exit(pcihp_skel_exit);