1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Hot Plug Controller Skeleton Driver - 0.3
5 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001,2003 IBM Corp.
10 * This driver is to be used as a skeleton driver to show how to interface
11 * with the pci hotplug core easily.
13 * Send feedback to <greg@kroah.com>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include <linux/pci_hotplug.h>
23 #include <linux/init.h>
25 #define SLOT_NAME_SIZE 10
28 struct hotplug_slot
*hotplug_slot
;
29 struct list_head slot_list
;
30 char name
[SLOT_NAME_SIZE
];
33 static LIST_HEAD(slot_list
);
35 #define MY_NAME "pcihp_skeleton"
37 #define dbg(format, arg...) \
40 printk(KERN_DEBUG "%s: " format "\n", \
43 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
44 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
45 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
51 #define DRIVER_VERSION "0.3"
52 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
53 #define DRIVER_DESC "Hot Plug PCI Controller Skeleton Driver"
55 MODULE_AUTHOR(DRIVER_AUTHOR
);
56 MODULE_DESCRIPTION(DRIVER_DESC
);
57 MODULE_LICENSE("GPL");
58 module_param(debug
, bool, 0644);
59 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
61 static int enable_slot(struct hotplug_slot
*slot
);
62 static int disable_slot(struct hotplug_slot
*slot
);
63 static int set_attention_status(struct hotplug_slot
*slot
, u8 value
);
64 static int hardware_test(struct hotplug_slot
*slot
, u32 value
);
65 static int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
66 static int get_attention_status(struct hotplug_slot
*slot
, u8
*value
);
67 static int get_latch_status(struct hotplug_slot
*slot
, u8
*value
);
68 static int get_adapter_status(struct hotplug_slot
*slot
, u8
*value
);
70 static struct hotplug_slot_ops skel_hotplug_slot_ops
= {
71 .enable_slot
= enable_slot
,
72 .disable_slot
= disable_slot
,
73 .set_attention_status
= set_attention_status
,
74 .hardware_test
= hardware_test
,
75 .get_power_status
= get_power_status
,
76 .get_attention_status
= get_attention_status
,
77 .get_latch_status
= get_latch_status
,
78 .get_adapter_status
= get_adapter_status
,
81 static int enable_slot(struct hotplug_slot
*hotplug_slot
)
83 struct slot
*slot
= hotplug_slot
->private;
86 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
89 * Fill in code here to enable the specified slot
95 static int disable_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 disable the specified slot
109 static int set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
111 struct slot
*slot
= hotplug_slot
->private;
114 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
119 * Fill in code here to turn light off
126 * Fill in code here to turn light on
134 static int hardware_test(struct hotplug_slot
*hotplug_slot
, u32 value
)
136 struct slot
*slot
= hotplug_slot
->private;
139 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
143 /* Specify a test here */
146 /* Specify another test here */
153 static int get_power_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
155 struct slot
*slot
= hotplug_slot
->private;
158 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
161 * Fill in logic to get the current power status of the specific
162 * slot and store it in the *value location.
168 static int get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
170 struct slot
*slot
= hotplug_slot
->private;
173 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
176 * Fill in logic to get the current attention status of the specific
177 * slot and store it in the *value location.
183 static int get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
185 struct slot
*slot
= hotplug_slot
->private;
188 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
191 * Fill in logic to get the current latch status of the specific
192 * slot and store it in the *value location.
198 static int get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
200 struct slot
*slot
= hotplug_slot
->private;
203 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
206 * Fill in logic to get the current adapter status of the specific
207 * slot and store it in the *value location.
213 static void release_slot(struct hotplug_slot
*hotplug_slot
)
215 struct slot
*slot
= hotplug_slot
->private;
217 dbg("%s - physical_slot = %s\n", __func__
, hotplug_slot
->name
);
218 kfree(slot
->hotplug_slot
->info
);
219 kfree(slot
->hotplug_slot
);
223 static void make_slot_name(struct slot
*slot
)
226 * Stupid way to make a filename out of the slot name.
227 * replace this if your hardware provides a better way to name slots.
229 snprintf(slot
->hotplug_slot
->name
, SLOT_NAME_SIZE
, "%d", slot
->number
);
233 * init_slots - initialize 'struct slot' structures for each slot
236 static int __init
init_slots(void)
239 struct hotplug_slot
*hotplug_slot
;
240 struct hotplug_slot_info
*info
;
245 * Create a structure for each slot, and register that slot
246 * with the pci_hotplug subsystem.
248 for (i
= 0; i
< num_slots
; ++i
) {
249 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
255 hotplug_slot
= kzalloc(sizeof(*hotplug_slot
), GFP_KERNEL
);
260 slot
->hotplug_slot
= hotplug_slot
;
262 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
267 hotplug_slot
->info
= info
;
271 hotplug_slot
->name
= slot
->name
;
272 hotplug_slot
->private = slot
;
273 hotplug_slot
->release
= &release_slot
;
274 make_slot_name(slot
);
275 hotplug_slot
->ops
= &skel_hotplug_slot_ops
;
278 * Initialize the slot info structure with some known
281 get_power_status(hotplug_slot
, &info
->power_status
);
282 get_attention_status(hotplug_slot
, &info
->attention_status
);
283 get_latch_status(hotplug_slot
, &info
->latch_status
);
284 get_adapter_status(hotplug_slot
, &info
->adapter_status
);
286 dbg("registering slot %d\n", i
);
287 retval
= pci_hp_register(slot
->hotplug_slot
);
289 err("pci_hp_register failed with error %d\n", retval
);
293 /* add slot to our internal list */
294 list_add(&slot
->slot_list
, &slot_list
);
308 static void __exit
cleanup_slots(void)
310 struct slot
*slot
, *next
;
313 * Unregister all of our slots with the pci_hotplug subsystem.
314 * Memory will be freed in release_slot() callback after slot's
315 * lifespan is finished.
317 list_for_each_entry_safe(slot
, next
, &slot_list
, slot_list
) {
318 list_del(&slot
->slot_list
);
319 pci_hp_deregister(slot
->hotplug_slot
);
323 static int __init
pcihp_skel_init(void)
327 info(DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
329 * Do specific initialization stuff for your driver here
330 * like initializing your controller hardware (if any) and
331 * determining the number of slots you have in the system
339 static void __exit
pcihp_skel_exit(void)
342 * Clean everything up.
347 module_init(pcihp_skel_init
);
348 module_exit(pcihp_skel_exit
);