2 * PCI Hot Plug Controller Skeleton Driver - 0.2
4 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001,2003 IBM Corp.
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.
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
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.
24 * This driver is to be used as a skeleton driver to be show how to interface
25 * with the pci hotplug core easily.
27 * Send feedback to <greg@kroah.com>
31 #include <linux/config.h>
32 #include <linux/module.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"
40 #define SLOT_MAGIC 0x67267322
44 struct hotplug_slot *hotplug_slot;
45 struct list_head slot_list;
48 static LIST_HEAD(slot_list);
50 #if !defined(CONFIG_HOTPLUG_PCI_SKELETON_MODULE)
51 #define MY_NAME "pcihp_skeleton"
53 #define MY_NAME THIS_MODULE->name
56 #define dbg(format, arg...) \
59 printk (KERN_DEBUG "%s: " format "\n", \
62 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
63 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
64 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
72 #define DRIVER_VERSION "0.2"
73 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
74 #define DRIVER_DESC "Hot Plug PCI Controller Skeleton Driver"
76 MODULE_AUTHOR(DRIVER_AUTHOR);
77 MODULE_DESCRIPTION(DRIVER_DESC);
78 MODULE_LICENSE("GPL");
79 MODULE_PARM(debug, "i");
80 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
82 static int enable_slot (struct hotplug_slot *slot);
83 static int disable_slot (struct hotplug_slot *slot);
84 static int set_attention_status (struct hotplug_slot *slot, u8 value);
85 static int hardware_test (struct hotplug_slot *slot, u32 value);
86 static int get_power_status (struct hotplug_slot *slot, u8 *value);
87 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
88 static int get_latch_status (struct hotplug_slot *slot, u8 *value);
89 static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
91 static struct hotplug_slot_ops skel_hotplug_slot_ops = {
93 .enable_slot = enable_slot,
94 .disable_slot = disable_slot,
95 .set_attention_status = set_attention_status,
96 .hardware_test = hardware_test,
97 .get_power_status = get_power_status,
98 .get_attention_status = get_attention_status,
99 .get_latch_status = get_latch_status,
100 .get_adapter_status = get_adapter_status,
104 /* Inline functions to check the sanity of a pointer that is passed to us */
105 static inline int slot_paranoia_check (struct slot *slot, const char *function)
108 dbg("%s - slot == NULL", function);
111 if (slot->magic != SLOT_MAGIC) {
112 dbg("%s - bad magic number for slot", function);
115 if (!slot->hotplug_slot) {
116 dbg("%s - slot->hotplug_slot == NULL!", function);
122 static inline struct slot *get_slot (struct hotplug_slot *hotplug_slot, const char *function)
127 dbg("%s - hotplug_slot == NULL\n", function);
131 slot = (struct slot *)hotplug_slot->private;
132 if (slot_paranoia_check (slot, function))
138 static int enable_slot (struct hotplug_slot *hotplug_slot)
140 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
146 dbg ("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
149 * Fill in code here to enable the specified slot
156 static int disable_slot (struct hotplug_slot *hotplug_slot)
158 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
164 dbg ("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
167 * Fill in code here to disable the specified slot
173 static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
175 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
181 dbg ("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
186 * Fill in code here to turn light off
193 * Fill in code here to turn light on
201 static int hardware_test (struct hotplug_slot *hotplug_slot, u32 value)
203 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
209 dbg ("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
211 err ("No hardware tests are defined for this driver");
214 /* Or you can specify a test if you want to */
219 static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
221 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
227 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
230 * Fill in logic to get the current power status of the specific
231 * slot and store it in the *value location.
237 static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
239 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
245 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
248 * Fill in logic to get the current attention status of the specific
249 * slot and store it in the *value location.
255 static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
257 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
263 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
266 * Fill in logic to get the current latch status of the specific
267 * slot and store it in the *value location.
273 static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
275 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
281 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
284 * Fill in logic to get the current adapter status of the specific
285 * slot and store it in the *value location.
291 static void release_slots(struct hotplug_slot *hotplug_slot)
293 struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
299 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
300 kfree(slot->hotplug_slot->info);
301 kfree(slot->hotplug_slot->name);
302 kfree(slot->hotplug_slot);
306 #define SLOT_NAME_SIZE 10
307 static void make_slot_name (struct slot *slot)
310 * Stupid way to make a filename out of the slot name.
311 * replace this if your hardware provides a better way to name slots.
313 snprintf (slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d", slot->number);
316 static int init_slots (void)
319 struct hotplug_slot *hotplug_slot;
320 struct hotplug_slot_info *info;
326 * Create a structure for each slot, and register that slot
327 * with the pci_hotplug subsystem.
329 for (i = 0; i < num_slots; ++i) {
330 slot = kmalloc (sizeof (struct slot), GFP_KERNEL);
333 memset(slot, 0, sizeof(struct slot));
335 hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
340 memset(hotplug_slot, 0, sizeof (struct hotplug_slot));
341 slot->hotplug_slot = hotplug_slot;
343 info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
345 kfree (hotplug_slot);
349 memset(info, 0, sizeof (struct hotplug_slot_info));
350 hotplug_slot->info = info;
352 name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
355 kfree (hotplug_slot);
359 hotplug_slot->name = name;
361 slot->magic = SLOT_MAGIC;
364 hotplug_slot->private = slot;
365 hotplug_slot->release = &release_slot;
366 make_slot_name (slot);
367 hotplug_slot->ops = &skel_hotplug_slot_ops;
370 * Initilize the slot info structure with some known
373 info->power_status = get_power_status(slot);
374 info->attention_status = get_attention_status(slot);
375 info->latch_status = get_latch_status(slot);
376 info->adapter_status = get_adapter_status(slot);
378 dbg ("registering slot %d\n", i);
379 retval = pci_hp_register (slot->hotplug_slot);
381 err ("pci_hp_register failed with error %d\n", retval);
384 kfree (hotplug_slot);
389 /* add slot to our internal list */
390 list_add (&slot->slot_list, &slot_list);
396 static void cleanup_slots(void)
398 struct list_head *tmp;
399 struct list_head *next;
403 * Unregister all of our slots with the pci_hotplug subsystem.
404 * Memory will be freed in release_slot() callback after slot's
405 * lifespan is finished.
407 list_for_each_safe (tmp, next, &slot_list) {
408 slot = list_entry (tmp, struct slot, slot_list);
409 list_del (&slot->slot_list);
410 pci_hp_deregister (slot->hotplug_slot);
414 static int __init pcihp_skel_init(void)
419 * Do specific initialization stuff for your driver here
420 * Like initilizing your controller hardware (if any) and
421 * determining the number of slots you have in the system
426 retval = init_slots();
430 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
434 static void __exit pcihp_skel_exit(void)
437 * Clean everything up.
442 module_init(pcihp_skel_init);
443 module_exit(pcihp_skel_exit);