pcihp_skeleton.c 8.8 KB

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