pcihp_skeleton.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 bool 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. .enable_slot = enable_slot,
  76. .disable_slot = disable_slot,
  77. .set_attention_status = set_attention_status,
  78. .hardware_test = hardware_test,
  79. .get_power_status = get_power_status,
  80. .get_attention_status = get_attention_status,
  81. .get_latch_status = get_latch_status,
  82. .get_adapter_status = get_adapter_status,
  83. };
  84. static int enable_slot(struct hotplug_slot *hotplug_slot)
  85. {
  86. struct slot *slot = hotplug_slot->private;
  87. int retval = 0;
  88. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  89. /*
  90. * Fill in code here to enable the specified slot
  91. */
  92. return retval;
  93. }
  94. static int disable_slot(struct hotplug_slot *hotplug_slot)
  95. {
  96. struct slot *slot = hotplug_slot->private;
  97. int retval = 0;
  98. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  99. /*
  100. * Fill in code here to disable the specified slot
  101. */
  102. return retval;
  103. }
  104. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  105. {
  106. struct slot *slot = hotplug_slot->private;
  107. int retval = 0;
  108. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  109. switch (status) {
  110. case 0:
  111. /*
  112. * Fill in code here to turn light off
  113. */
  114. break;
  115. case 1:
  116. default:
  117. /*
  118. * Fill in code here to turn light on
  119. */
  120. break;
  121. }
  122. return retval;
  123. }
  124. static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value)
  125. {
  126. struct slot *slot = hotplug_slot->private;
  127. int retval = 0;
  128. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  129. switch (value) {
  130. case 0:
  131. /* Specify a test here */
  132. break;
  133. case 1:
  134. /* Specify another test here */
  135. break;
  136. }
  137. return retval;
  138. }
  139. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  140. {
  141. struct slot *slot = hotplug_slot->private;
  142. int retval = 0;
  143. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  144. /*
  145. * Fill in logic to get the current power status of the specific
  146. * slot and store it in the *value location.
  147. */
  148. return retval;
  149. }
  150. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  151. {
  152. struct slot *slot = hotplug_slot->private;
  153. int retval = 0;
  154. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  155. /*
  156. * Fill in logic to get the current attention status of the specific
  157. * slot and store it in the *value location.
  158. */
  159. return retval;
  160. }
  161. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  162. {
  163. struct slot *slot = hotplug_slot->private;
  164. int retval = 0;
  165. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  166. /*
  167. * Fill in logic to get the current latch status of the specific
  168. * slot and store it in the *value location.
  169. */
  170. return retval;
  171. }
  172. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  173. {
  174. struct slot *slot = hotplug_slot->private;
  175. int retval = 0;
  176. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  177. /*
  178. * Fill in logic to get the current adapter status of the specific
  179. * slot and store it in the *value location.
  180. */
  181. return retval;
  182. }
  183. static void release_slot(struct hotplug_slot *hotplug_slot)
  184. {
  185. struct slot *slot = hotplug_slot->private;
  186. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  187. kfree(slot->hotplug_slot->info);
  188. kfree(slot->hotplug_slot);
  189. kfree(slot);
  190. }
  191. static void make_slot_name(struct slot *slot)
  192. {
  193. /*
  194. * Stupid way to make a filename out of the slot name.
  195. * replace this if your hardware provides a better way to name slots.
  196. */
  197. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d", slot->number);
  198. }
  199. /**
  200. * init_slots - initialize 'struct slot' structures for each slot
  201. *
  202. */
  203. static int __init init_slots(void)
  204. {
  205. struct slot *slot;
  206. struct hotplug_slot *hotplug_slot;
  207. struct hotplug_slot_info *info;
  208. int retval;
  209. int i;
  210. /*
  211. * Create a structure for each slot, and register that slot
  212. * with the pci_hotplug subsystem.
  213. */
  214. for (i = 0; i < num_slots; ++i) {
  215. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  216. if (!slot) {
  217. retval = -ENOMEM;
  218. goto error;
  219. }
  220. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  221. if (!hotplug_slot) {
  222. retval = -ENOMEM;
  223. goto error_slot;
  224. }
  225. slot->hotplug_slot = hotplug_slot;
  226. info = kzalloc(sizeof(*info), GFP_KERNEL);
  227. if (!info) {
  228. retval = -ENOMEM;
  229. goto error_hpslot;
  230. }
  231. hotplug_slot->info = info;
  232. slot->number = i;
  233. hotplug_slot->name = slot->name;
  234. hotplug_slot->private = slot;
  235. hotplug_slot->release = &release_slot;
  236. make_slot_name(slot);
  237. hotplug_slot->ops = &skel_hotplug_slot_ops;
  238. /*
  239. * Initialize the slot info structure with some known
  240. * good values.
  241. */
  242. get_power_status(hotplug_slot, &info->power_status);
  243. get_attention_status(hotplug_slot, &info->attention_status);
  244. get_latch_status(hotplug_slot, &info->latch_status);
  245. get_adapter_status(hotplug_slot, &info->adapter_status);
  246. dbg("registering slot %d\n", i);
  247. retval = pci_hp_register(slot->hotplug_slot);
  248. if (retval) {
  249. err("pci_hp_register failed with error %d\n", retval);
  250. goto error_info;
  251. }
  252. /* add slot to our internal list */
  253. list_add(&slot->slot_list, &slot_list);
  254. }
  255. return 0;
  256. error_info:
  257. kfree(info);
  258. error_hpslot:
  259. kfree(hotplug_slot);
  260. error_slot:
  261. kfree(slot);
  262. error:
  263. return retval;
  264. }
  265. static void __exit cleanup_slots(void)
  266. {
  267. struct list_head *tmp;
  268. struct list_head *next;
  269. struct slot *slot;
  270. /*
  271. * Unregister all of our slots with the pci_hotplug subsystem.
  272. * Memory will be freed in release_slot() callback after slot's
  273. * lifespan is finished.
  274. */
  275. list_for_each_safe(tmp, next, &slot_list) {
  276. slot = list_entry(tmp, struct slot, slot_list);
  277. list_del(&slot->slot_list);
  278. pci_hp_deregister(slot->hotplug_slot);
  279. }
  280. }
  281. static int __init pcihp_skel_init(void)
  282. {
  283. int retval;
  284. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  285. /*
  286. * Do specific initialization stuff for your driver here
  287. * like initializing your controller hardware (if any) and
  288. * determining the number of slots you have in the system
  289. * right now.
  290. */
  291. num_slots = 5;
  292. return init_slots();
  293. }
  294. static void __exit pcihp_skel_exit(void)
  295. {
  296. /*
  297. * Clean everything up.
  298. */
  299. cleanup_slots();
  300. }
  301. module_init(pcihp_skel_init);
  302. module_exit(pcihp_skel_exit);