pcihp_skeleton.c 8.0 KB

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