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