2 * PCI Hot Plug Controller Skeleton Driver - 0.3
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 show how to interface
25 * with the pci hotplug core easily.
27 * Send feedback to <greg@kroah.com>
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/pci_hotplug.h>
37 #include <linux/init.h>
39 #define SLOT_NAME_SIZE 10
42 struct hotplug_slot
*hotplug_slot
;
43 struct list_head slot_list
;
44 char name
[SLOT_NAME_SIZE
];
47 static LIST_HEAD(slot_list
);
49 #define MY_NAME "pcihp_skeleton"
51 #define dbg(format, arg...) \
54 printk (KERN_DEBUG "%s: " format "\n", \
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)
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"
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");
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
);
84 static struct hotplug_slot_ops skel_hotplug_slot_ops
= {
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
,
95 static int enable_slot(struct hotplug_slot
*hotplug_slot
)
97 struct slot
*slot
= hotplug_slot
->private;
100 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
103 * Fill in code here to enable the specified slot
109 static int disable_slot(struct hotplug_slot
*hotplug_slot
)
111 struct slot
*slot
= hotplug_slot
->private;
114 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
117 * Fill in code here to disable the specified slot
123 static int set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
125 struct slot
*slot
= hotplug_slot
->private;
128 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
133 * Fill in code here to turn light off
140 * Fill in code here to turn light on
148 static int hardware_test(struct hotplug_slot
*hotplug_slot
, u32 value
)
150 struct slot
*slot
= hotplug_slot
->private;
153 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
157 /* Specify a test here */
160 /* Specify another test here */
167 static int get_power_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
169 struct slot
*slot
= hotplug_slot
->private;
172 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
175 * Fill in logic to get the current power status of the specific
176 * slot and store it in the *value location.
182 static int get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
184 struct slot
*slot
= hotplug_slot
->private;
187 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
190 * Fill in logic to get the current attention status of the specific
191 * slot and store it in the *value location.
197 static int get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
199 struct slot
*slot
= hotplug_slot
->private;
202 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
205 * Fill in logic to get the current latch status of the specific
206 * slot and store it in the *value location.
212 static int get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
214 struct slot
*slot
= hotplug_slot
->private;
217 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
220 * Fill in logic to get the current adapter status of the specific
221 * slot and store it in the *value location.
227 static void release_slot(struct hotplug_slot
*hotplug_slot
)
229 struct slot
*slot
= hotplug_slot
->private;
231 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
232 kfree(slot
->hotplug_slot
->info
);
233 kfree(slot
->hotplug_slot
);
237 static void make_slot_name(struct slot
*slot
)
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.
243 snprintf(slot
->hotplug_slot
->name
, SLOT_NAME_SIZE
, "%d", slot
->number
);
247 * init_slots - initialize 'struct slot' structures for each slot
250 static int __init
init_slots(void)
253 struct hotplug_slot
*hotplug_slot
;
254 struct hotplug_slot_info
*info
;
255 int retval
= -ENOMEM
;
259 * Create a structure for each slot, and register that slot
260 * with the pci_hotplug subsystem.
262 for (i
= 0; i
< num_slots
; ++i
) {
263 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
267 hotplug_slot
= kzalloc(sizeof(*hotplug_slot
), GFP_KERNEL
);
270 slot
->hotplug_slot
= hotplug_slot
;
272 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
275 hotplug_slot
->info
= info
;
279 hotplug_slot
->name
= slot
->name
;
280 hotplug_slot
->private = slot
;
281 hotplug_slot
->release
= &release_slot
;
282 make_slot_name(slot
);
283 hotplug_slot
->ops
= &skel_hotplug_slot_ops
;
286 * Initialize the slot info structure with some known
289 get_power_status(hotplug_slot
, &info
->power_status
);
290 get_attention_status(hotplug_slot
, &info
->attention_status
);
291 get_latch_status(hotplug_slot
, &info
->latch_status
);
292 get_adapter_status(hotplug_slot
, &info
->adapter_status
);
294 dbg("registering slot %d\n", i
);
295 retval
= pci_hp_register(slot
->hotplug_slot
);
297 err("pci_hp_register failed with error %d\n", retval
);
301 /* add slot to our internal list */
302 list_add(&slot
->slot_list
, &slot_list
);
316 static void __exit
cleanup_slots(void)
318 struct list_head
*tmp
;
319 struct list_head
*next
;
323 * Unregister all of our slots with the pci_hotplug subsystem.
324 * Memory will be freed in release_slot() callback after slot's
325 * lifespan is finished.
327 list_for_each_safe(tmp
, next
, &slot_list
) {
328 slot
= list_entry(tmp
, struct slot
, slot_list
);
329 list_del(&slot
->slot_list
);
330 pci_hp_deregister(slot
->hotplug_slot
);
334 static int __init
pcihp_skel_init(void)
338 info(DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
340 * Do specific initialization stuff for your driver here
341 * like initializing your controller hardware (if any) and
342 * determining the number of slots you have in the system
350 static void __exit
pcihp_skel_exit(void)
353 * Clean everything up.
358 module_init(pcihp_skel_init
);
359 module_exit(pcihp_skel_exit
);