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