pcihp_skeleton.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. struct slot {
  39. u8 number;
  40. struct hotplug_slot *hotplug_slot;
  41. struct list_head slot_list;
  42. };
  43. static LIST_HEAD(slot_list);
  44. #define MY_NAME "pcihp_skeleton"
  45. #define dbg(format, arg...) \
  46. do { \
  47. if (debug) \
  48. printk (KERN_DEBUG "%s: " format "\n", \
  49. MY_NAME , ## arg); \
  50. } while (0)
  51. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  52. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  53. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  54. /* local variables */
  55. static int debug;
  56. static int num_slots;
  57. #define DRIVER_VERSION "0.3"
  58. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  59. #define DRIVER_DESC "Hot Plug PCI Controller Skeleton Driver"
  60. MODULE_AUTHOR(DRIVER_AUTHOR);
  61. MODULE_DESCRIPTION(DRIVER_DESC);
  62. MODULE_LICENSE("GPL");
  63. module_param(debug, bool, 0644);
  64. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  65. static int enable_slot (struct hotplug_slot *slot);
  66. static int disable_slot (struct hotplug_slot *slot);
  67. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  68. static int hardware_test (struct hotplug_slot *slot, u32 value);
  69. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  70. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  71. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  72. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  73. static struct hotplug_slot_ops skel_hotplug_slot_ops = {
  74. .owner = THIS_MODULE,
  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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, hotplug_slot->name);
  187. kfree(slot->hotplug_slot->info);
  188. kfree(slot->hotplug_slot->name);
  189. kfree(slot->hotplug_slot);
  190. kfree(slot);
  191. }
  192. #define SLOT_NAME_SIZE 10
  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. char *name;
  211. int retval = -ENOMEM;
  212. int i;
  213. /*
  214. * Create a structure for each slot, and register that slot
  215. * with the pci_hotplug subsystem.
  216. */
  217. for (i = 0; i < num_slots; ++i) {
  218. slot = kmalloc(sizeof(struct slot), GFP_KERNEL);
  219. if (!slot)
  220. goto error;
  221. memset(slot, 0, sizeof(struct slot));
  222. hotplug_slot = kmalloc(sizeof(struct hotplug_slot),
  223. GFP_KERNEL);
  224. if (!hotplug_slot)
  225. goto error_slot;
  226. memset(hotplug_slot, 0, sizeof (struct hotplug_slot));
  227. slot->hotplug_slot = hotplug_slot;
  228. info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
  229. if (!info)
  230. goto error_hpslot;
  231. memset(info, 0, sizeof (struct hotplug_slot_info));
  232. hotplug_slot->info = info;
  233. name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
  234. if (!name)
  235. goto error_info;
  236. hotplug_slot->name = name;
  237. slot->number = i;
  238. hotplug_slot->private = slot;
  239. hotplug_slot->release = &release_slot;
  240. make_slot_name(slot);
  241. hotplug_slot->ops = &skel_hotplug_slot_ops;
  242. /*
  243. * Initialize the slot info structure with some known
  244. * good values.
  245. */
  246. info->power_status = get_power_status(slot);
  247. info->attention_status = get_attention_status(slot);
  248. info->latch_status = get_latch_status(slot);
  249. info->adapter_status = get_adapter_status(slot);
  250. dbg("registering slot %d\n", i);
  251. retval = pci_hp_register(slot->hotplug_slot);
  252. if (retval) {
  253. err("pci_hp_register failed with error %d\n", retval);
  254. goto error_name;
  255. }
  256. /* add slot to our internal list */
  257. list_add(&slot->slot_list, &slot_list);
  258. }
  259. return 0;
  260. error_name:
  261. kfree(name);
  262. error_info:
  263. kfree(info);
  264. error_hpslot:
  265. kfree(hotplug_slot);
  266. error_slot:
  267. kfree(slot);
  268. error:
  269. return retval;
  270. }
  271. static void __exit cleanup_slots(void)
  272. {
  273. struct list_head *tmp;
  274. struct list_head *next;
  275. struct slot *slot;
  276. /*
  277. * Unregister all of our slots with the pci_hotplug subsystem.
  278. * Memory will be freed in release_slot() callback after slot's
  279. * lifespan is finished.
  280. */
  281. list_for_each_safe(tmp, next, &slot_list) {
  282. slot = list_entry(tmp, struct slot, slot_list);
  283. list_del(&slot->slot_list);
  284. pci_hp_deregister(slot->hotplug_slot);
  285. }
  286. }
  287. static int __init pcihp_skel_init(void)
  288. {
  289. int retval;
  290. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  291. /*
  292. * Do specific initialization stuff for your driver here
  293. * Like initializing your controller hardware (if any) and
  294. * determining the number of slots you have in the system
  295. * right now.
  296. */
  297. num_slots = 5;
  298. return init_slots();
  299. }
  300. static void __exit pcihp_skel_exit(void)
  301. {
  302. /*
  303. * Clean everything up.
  304. */
  305. cleanup_slots();
  306. }
  307. module_init(pcihp_skel_init);
  308. module_exit(pcihp_skel_exit);