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