blob: e3dd6cf9e89f19c7e868ca3fd2ec460cf630e0a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Satoru Takeuchi753388c2006-09-06 16:47:28 +09002 * PCI Hot Plug Controller Skeleton Driver - 0.3
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Takeuchi753388c2006-09-06 16:47:28 +090024 * This driver is to be used as a skeleton driver to show how to interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * with the pci hotplug core easily.
26 *
27 * Send feedback to <greg@kroah.com>
28 *
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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-Hartman7a54f252006-10-13 20:05:19 -070036#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kenji Kaneshige287588b2006-01-26 10:01:35 +090039#define SLOT_NAME_SIZE 10
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct slot {
41 u8 number;
42 struct hotplug_slot *hotplug_slot;
43 struct list_head slot_list;
Kenji Kaneshige287588b2006-01-26 10:01:35 +090044 char name[SLOT_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47static 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 Torvalds1da177e2005-04-16 15:20:36 -070061/* local variables */
62static int debug;
63static 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
69MODULE_AUTHOR(DRIVER_AUTHOR);
70MODULE_DESCRIPTION(DRIVER_DESC);
71MODULE_LICENSE("GPL");
72module_param(debug, bool, 0644);
73MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
74
75static int enable_slot (struct hotplug_slot *slot);
76static int disable_slot (struct hotplug_slot *slot);
77static int set_attention_status (struct hotplug_slot *slot, u8 value);
78static int hardware_test (struct hotplug_slot *slot, u32 value);
79static int get_power_status (struct hotplug_slot *slot, u8 *value);
80static int get_attention_status (struct hotplug_slot *slot, u8 *value);
81static int get_latch_status (struct hotplug_slot *slot, u8 *value);
82static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
83
84static struct hotplug_slot_ops skel_hotplug_slot_ops = {
85 .owner = THIS_MODULE,
86 .enable_slot = enable_slot,
87 .disable_slot = disable_slot,
88 .set_attention_status = set_attention_status,
89 .hardware_test = hardware_test,
90 .get_power_status = get_power_status,
91 .get_attention_status = get_attention_status,
92 .get_latch_status = get_latch_status,
93 .get_adapter_status = get_adapter_status,
94};
95
96static int enable_slot(struct hotplug_slot *hotplug_slot)
97{
98 struct slot *slot = hotplug_slot->private;
99 int retval = 0;
100
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800101 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /*
104 * Fill in code here to enable the specified slot
105 */
106
107 return retval;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int disable_slot(struct hotplug_slot *hotplug_slot)
111{
112 struct slot *slot = hotplug_slot->private;
113 int retval = 0;
114
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800115 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /*
118 * Fill in code here to disable the specified slot
119 */
120
121 return retval;
122}
123
124static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
125{
126 struct slot *slot = hotplug_slot->private;
127 int retval = 0;
128
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800129 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 switch (status) {
132 case 0:
133 /*
134 * Fill in code here to turn light off
135 */
136 break;
137
138 case 1:
139 default:
140 /*
141 * Fill in code here to turn light on
142 */
143 break;
144 }
145
146 return retval;
147}
148
149static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value)
150{
151 struct slot *slot = hotplug_slot->private;
152 int retval = 0;
153
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800154 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 switch (value) {
157 case 0:
158 /* Specify a test here */
159 break;
160 case 1:
161 /* Specify another test here */
162 break;
163 }
164
165 return retval;
166}
167
168static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
169{
170 struct slot *slot = hotplug_slot->private;
171 int retval = 0;
172
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800173 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
176 * Fill in logic to get the current power status of the specific
177 * slot and store it in the *value location.
178 */
179
180 return retval;
181}
182
183static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
184{
185 struct slot *slot = hotplug_slot->private;
186 int retval = 0;
187
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800188 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
191 * Fill in logic to get the current attention status of the specific
192 * slot and store it in the *value location.
193 */
194
195 return retval;
196}
197
198static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
199{
200 struct slot *slot = hotplug_slot->private;
201 int retval = 0;
202
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800203 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
206 * Fill in logic to get the current latch status of the specific
207 * slot and store it in the *value location.
208 */
209
210 return retval;
211}
212
213static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
214{
215 struct slot *slot = hotplug_slot->private;
216 int retval = 0;
217
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800218 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /*
221 * Fill in logic to get the current adapter status of the specific
222 * slot and store it in the *value location.
223 */
224
225 return retval;
226}
227
228static void release_slot(struct hotplug_slot *hotplug_slot)
229{
230 struct slot *slot = hotplug_slot->private;
231
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800232 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kfree(slot->hotplug_slot->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 kfree(slot->hotplug_slot);
235 kfree(slot);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void make_slot_name(struct slot *slot)
239{
240 /*
241 * Stupid way to make a filename out of the slot name.
242 * replace this if your hardware provides a better way to name slots.
243 */
244 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d", slot->number);
245}
246
247/**
248 * init_slots - initialize 'struct slot' structures for each slot
249 *
250 */
251static int __init init_slots(void)
252{
253 struct slot *slot;
254 struct hotplug_slot *hotplug_slot;
255 struct hotplug_slot_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int retval = -ENOMEM;
257 int i;
258
259 /*
260 * Create a structure for each slot, and register that slot
261 * with the pci_hotplug subsystem.
262 */
263 for (i = 0; i < num_slots; ++i) {
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900264 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!slot)
266 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900268 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!hotplug_slot)
270 goto error_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 slot->hotplug_slot = hotplug_slot;
272
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900273 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!info)
275 goto error_hpslot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 hotplug_slot->info = info;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 slot->number = i;
279
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900280 hotplug_slot->name = slot->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 hotplug_slot->private = slot;
282 hotplug_slot->release = &release_slot;
283 make_slot_name(slot);
284 hotplug_slot->ops = &skel_hotplug_slot_ops;
285
286 /*
Steven Coleeaae4b32005-05-03 18:38:30 -0600287 * Initialize the slot info structure with some known
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * good values.
289 */
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900290 get_power_status(hotplug_slot, &info->power_status);
291 get_attention_status(hotplug_slot, &info->attention_status);
292 get_latch_status(hotplug_slot, &info->latch_status);
293 get_adapter_status(hotplug_slot, &info->adapter_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 dbg("registering slot %d\n", i);
296 retval = pci_hp_register(slot->hotplug_slot);
297 if (retval) {
298 err("pci_hp_register failed with error %d\n", retval);
Kenji Kaneshige287588b2006-01-26 10:01:35 +0900299 goto error_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 /* add slot to our internal list */
303 list_add(&slot->slot_list, &slot_list);
304 }
305
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307error_info:
308 kfree(info);
309error_hpslot:
310 kfree(hotplug_slot);
311error_slot:
312 kfree(slot);
313error:
314 return retval;
315}
316
317static void __exit cleanup_slots(void)
318{
319 struct list_head *tmp;
320 struct list_head *next;
321 struct slot *slot;
322
323 /*
324 * Unregister all of our slots with the pci_hotplug subsystem.
325 * Memory will be freed in release_slot() callback after slot's
326 * lifespan is finished.
327 */
328 list_for_each_safe(tmp, next, &slot_list) {
329 slot = list_entry(tmp, struct slot, slot_list);
330 list_del(&slot->slot_list);
331 pci_hp_deregister(slot->hotplug_slot);
332 }
333}
334
335static int __init pcihp_skel_init(void)
336{
337 int retval;
338
339 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
340 /*
341 * Do specific initialization stuff for your driver here
Satoru Takeuchi753388c2006-09-06 16:47:28 +0900342 * like initializing your controller hardware (if any) and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * determining the number of slots you have in the system
344 * right now.
345 */
346 num_slots = 5;
347
348 return init_slots();
349}
350
351static void __exit pcihp_skel_exit(void)
352{
353 /*
354 * Clean everything up.
355 */
356 cleanup_slots();
357}
358
359module_init(pcihp_skel_init);
360module_exit(pcihp_skel_exit);