Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Satoru Takeuchi | 753388c | 2006-09-06 16:47:28 +0900 | [diff] [blame] | 2 | * PCI Hot Plug Controller Skeleton Driver - 0.3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Satoru Takeuchi | 753388c | 2006-09-06 16:47:28 +0900 | [diff] [blame] | 24 | * This driver is to be used as a skeleton driver to show how to interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * with the pci hotplug core easily. |
| 26 | * |
| 27 | * Send feedback to <greg@kroah.com> |
| 28 | * |
| 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 7a54f25 | 2006-10-13 20:05:19 -0700 | [diff] [blame] | 36 | #include <linux/pci_hotplug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 39 | #define SLOT_NAME_SIZE 10 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct slot { |
| 41 | u8 number; |
| 42 | struct hotplug_slot *hotplug_slot; |
| 43 | struct list_head slot_list; |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 44 | char name[SLOT_NAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static LIST_HEAD(slot_list); |
| 48 | |
| 49 | #define MY_NAME "pcihp_skeleton" |
| 50 | |
| 51 | #define dbg(format, arg...) \ |
| 52 | do { \ |
| 53 | if (debug) \ |
| 54 | printk (KERN_DEBUG "%s: " format "\n", \ |
| 55 | MY_NAME , ## arg); \ |
| 56 | } while (0) |
| 57 | #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg) |
| 58 | #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg) |
| 59 | #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* local variables */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 62 | static bool debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static int num_slots; |
| 64 | |
| 65 | #define DRIVER_VERSION "0.3" |
| 66 | #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>" |
| 67 | #define DRIVER_DESC "Hot Plug PCI Controller Skeleton Driver" |
| 68 | |
| 69 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 70 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 71 | MODULE_LICENSE("GPL"); |
| 72 | module_param(debug, bool, 0644); |
| 73 | MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); |
| 74 | |
| 75 | static int enable_slot (struct hotplug_slot *slot); |
| 76 | static int disable_slot (struct hotplug_slot *slot); |
| 77 | static int set_attention_status (struct hotplug_slot *slot, u8 value); |
| 78 | static int hardware_test (struct hotplug_slot *slot, u32 value); |
| 79 | static int get_power_status (struct hotplug_slot *slot, u8 *value); |
| 80 | static int get_attention_status (struct hotplug_slot *slot, u8 *value); |
| 81 | static int get_latch_status (struct hotplug_slot *slot, u8 *value); |
| 82 | static int get_adapter_status (struct hotplug_slot *slot, u8 *value); |
| 83 | |
| 84 | static struct hotplug_slot_ops skel_hotplug_slot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | .enable_slot = enable_slot, |
| 86 | .disable_slot = disable_slot, |
| 87 | .set_attention_status = set_attention_status, |
| 88 | .hardware_test = hardware_test, |
| 89 | .get_power_status = get_power_status, |
| 90 | .get_attention_status = get_attention_status, |
| 91 | .get_latch_status = get_latch_status, |
| 92 | .get_adapter_status = get_adapter_status, |
| 93 | }; |
| 94 | |
| 95 | static int enable_slot(struct hotplug_slot *hotplug_slot) |
| 96 | { |
| 97 | struct slot *slot = hotplug_slot->private; |
| 98 | int retval = 0; |
| 99 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 100 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * Fill in code here to enable the specified slot |
| 104 | */ |
| 105 | |
| 106 | return retval; |
| 107 | } |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static int disable_slot(struct hotplug_slot *hotplug_slot) |
| 110 | { |
| 111 | struct slot *slot = hotplug_slot->private; |
| 112 | int retval = 0; |
| 113 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 114 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Fill in code here to disable the specified slot |
| 118 | */ |
| 119 | |
| 120 | return retval; |
| 121 | } |
| 122 | |
| 123 | static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status) |
| 124 | { |
| 125 | struct slot *slot = hotplug_slot->private; |
| 126 | int retval = 0; |
| 127 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 128 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | switch (status) { |
| 131 | case 0: |
| 132 | /* |
| 133 | * Fill in code here to turn light off |
| 134 | */ |
| 135 | break; |
| 136 | |
| 137 | case 1: |
| 138 | default: |
| 139 | /* |
| 140 | * Fill in code here to turn light on |
| 141 | */ |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | return retval; |
| 146 | } |
| 147 | |
| 148 | static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value) |
| 149 | { |
| 150 | struct slot *slot = hotplug_slot->private; |
| 151 | int retval = 0; |
| 152 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 153 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | switch (value) { |
| 156 | case 0: |
| 157 | /* Specify a test here */ |
| 158 | break; |
| 159 | case 1: |
| 160 | /* Specify another test here */ |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return retval; |
| 165 | } |
| 166 | |
| 167 | static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value) |
| 168 | { |
| 169 | struct slot *slot = hotplug_slot->private; |
| 170 | int retval = 0; |
| 171 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 172 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Fill in logic to get the current power status of the specific |
| 176 | * slot and store it in the *value location. |
| 177 | */ |
| 178 | |
| 179 | return retval; |
| 180 | } |
| 181 | |
| 182 | static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value) |
| 183 | { |
| 184 | struct slot *slot = hotplug_slot->private; |
| 185 | int retval = 0; |
| 186 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 187 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Fill in logic to get the current attention status of the specific |
| 191 | * slot and store it in the *value location. |
| 192 | */ |
| 193 | |
| 194 | return retval; |
| 195 | } |
| 196 | |
| 197 | static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value) |
| 198 | { |
| 199 | struct slot *slot = hotplug_slot->private; |
| 200 | int retval = 0; |
| 201 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 202 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Fill in logic to get the current latch status of the specific |
| 206 | * slot and store it in the *value location. |
| 207 | */ |
| 208 | |
| 209 | return retval; |
| 210 | } |
| 211 | |
| 212 | static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value) |
| 213 | { |
| 214 | struct slot *slot = hotplug_slot->private; |
| 215 | int retval = 0; |
| 216 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 217 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Fill in logic to get the current adapter status of the specific |
| 221 | * slot and store it in the *value location. |
| 222 | */ |
| 223 | |
| 224 | return retval; |
| 225 | } |
| 226 | |
| 227 | static void release_slot(struct hotplug_slot *hotplug_slot) |
| 228 | { |
| 229 | struct slot *slot = hotplug_slot->private; |
| 230 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 231 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | kfree(slot->hotplug_slot->info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | kfree(slot->hotplug_slot); |
| 234 | kfree(slot); |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | static void make_slot_name(struct slot *slot) |
| 238 | { |
| 239 | /* |
| 240 | * Stupid way to make a filename out of the slot name. |
| 241 | * replace this if your hardware provides a better way to name slots. |
| 242 | */ |
| 243 | snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d", slot->number); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * init_slots - initialize 'struct slot' structures for each slot |
| 248 | * |
| 249 | */ |
| 250 | static int __init init_slots(void) |
| 251 | { |
| 252 | struct slot *slot; |
| 253 | struct hotplug_slot *hotplug_slot; |
| 254 | struct hotplug_slot_info *info; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 255 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int i; |
| 257 | |
| 258 | /* |
| 259 | * Create a structure for each slot, and register that slot |
| 260 | * with the pci_hotplug subsystem. |
| 261 | */ |
| 262 | for (i = 0; i < num_slots; ++i) { |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 263 | slot = kzalloc(sizeof(*slot), GFP_KERNEL); |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 264 | if (!slot) { |
| 265 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | goto error; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 269 | hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL); |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 270 | if (!hotplug_slot) { |
| 271 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | goto error_slot; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | slot->hotplug_slot = hotplug_slot; |
| 275 | |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 276 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 277 | if (!info) { |
| 278 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | goto error_hpslot; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame^] | 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | hotplug_slot->info = info; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | slot->number = i; |
| 284 | |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 285 | hotplug_slot->name = slot->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | hotplug_slot->private = slot; |
| 287 | hotplug_slot->release = &release_slot; |
| 288 | make_slot_name(slot); |
| 289 | hotplug_slot->ops = &skel_hotplug_slot_ops; |
| 290 | |
| 291 | /* |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 292 | * Initialize the slot info structure with some known |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * good values. |
| 294 | */ |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 295 | get_power_status(hotplug_slot, &info->power_status); |
| 296 | get_attention_status(hotplug_slot, &info->attention_status); |
| 297 | get_latch_status(hotplug_slot, &info->latch_status); |
| 298 | get_adapter_status(hotplug_slot, &info->adapter_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | dbg("registering slot %d\n", i); |
| 301 | retval = pci_hp_register(slot->hotplug_slot); |
| 302 | if (retval) { |
| 303 | err("pci_hp_register failed with error %d\n", retval); |
Kenji Kaneshige | 287588b | 2006-01-26 10:01:35 +0900 | [diff] [blame] | 304 | goto error_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* add slot to our internal list */ |
| 308 | list_add(&slot->slot_list, &slot_list); |
| 309 | } |
| 310 | |
| 311 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | error_info: |
| 313 | kfree(info); |
| 314 | error_hpslot: |
| 315 | kfree(hotplug_slot); |
| 316 | error_slot: |
| 317 | kfree(slot); |
| 318 | error: |
| 319 | return retval; |
| 320 | } |
| 321 | |
| 322 | static void __exit cleanup_slots(void) |
| 323 | { |
| 324 | struct list_head *tmp; |
| 325 | struct list_head *next; |
| 326 | struct slot *slot; |
| 327 | |
| 328 | /* |
| 329 | * Unregister all of our slots with the pci_hotplug subsystem. |
| 330 | * Memory will be freed in release_slot() callback after slot's |
| 331 | * lifespan is finished. |
| 332 | */ |
| 333 | list_for_each_safe(tmp, next, &slot_list) { |
| 334 | slot = list_entry(tmp, struct slot, slot_list); |
| 335 | list_del(&slot->slot_list); |
| 336 | pci_hp_deregister(slot->hotplug_slot); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | static int __init pcihp_skel_init(void) |
| 341 | { |
| 342 | int retval; |
| 343 | |
| 344 | info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); |
| 345 | /* |
| 346 | * Do specific initialization stuff for your driver here |
Satoru Takeuchi | 753388c | 2006-09-06 16:47:28 +0900 | [diff] [blame] | 347 | * like initializing your controller hardware (if any) and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * determining the number of slots you have in the system |
| 349 | * right now. |
| 350 | */ |
| 351 | num_slots = 5; |
| 352 | |
| 353 | return init_slots(); |
| 354 | } |
| 355 | |
| 356 | static void __exit pcihp_skel_exit(void) |
| 357 | { |
| 358 | /* |
| 359 | * Clean everything up. |
| 360 | */ |
| 361 | cleanup_slots(); |
| 362 | } |
| 363 | |
| 364 | module_init(pcihp_skel_init); |
| 365 | module_exit(pcihp_skel_exit); |