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