ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pci / hotplug / fakephp.c
1 /*
2  * Fake PCI Hot Plug Controller Driver
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2003 IBM Corp.
6  * Copyright (C) 2003 Rolf Eike Beer <eike-kernel@sf-tec.de>
7  *
8  * Based on ideas and code from:
9  *      Vladimir Kondratiev <vladimir.kondratiev@intel.com>
10  *      Rolf Eike Beer <eike-kernel@sf-tec.de>
11  *
12  * All rights reserved.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, version 2 of the License.
17  *
18  * Send feedback to <greg@kroah.com>
19  */
20
21 /*
22  *
23  * This driver will "emulate" removing PCI devices from the system.  If
24  * the "power" file is written to with "0" then the specified PCI device
25  * will be completely removed from the kernel.
26  *
27  * WARNING, this does NOT turn off the power to the PCI device.  This is
28  * a "logical" removal, not a physical or electrical removal.
29  *
30  * Use this module at your own risk, you have been warned!
31  *
32  * Enabling PCI devices is left as an exercise for the reader...
33  *
34  */
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include "pci_hotplug.h"
41 #include "../pci.h"
42
43 #if !defined(CONFIG_HOTPLUG_PCI_FAKE_MODULE)
44         #define MY_NAME "fakephp"
45 #else
46         #define MY_NAME THIS_MODULE->name
47 #endif
48
49 #define dbg(format, arg...)                                     \
50         do {                                                    \
51                 if (debug)                                      \
52                         printk(KERN_DEBUG "%s: " format,        \
53                                 MY_NAME , ## arg);              \
54         } while (0)
55 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME , ## arg)
56 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME , ## arg)
57
58 #define DRIVER_AUTHOR   "Greg Kroah-Hartman <greg@kroah.com>"
59 #define DRIVER_DESC     "Fake PCI Hot Plug Controller Driver"
60
61 struct dummy_slot {
62         struct list_head node;
63         struct hotplug_slot *slot;
64         struct pci_dev *dev;
65 };
66
67 static int debug;
68 static LIST_HEAD(slot_list);
69
70 static int enable_slot (struct hotplug_slot *slot);
71 static int disable_slot (struct hotplug_slot *slot);
72
73 static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
74         .owner                  = THIS_MODULE,
75         .enable_slot            = enable_slot,
76         .disable_slot           = disable_slot,
77 };
78
79 static void dummy_release(struct hotplug_slot *slot)
80 {
81         struct dummy_slot *dslot = slot->private;
82
83         list_del(&dslot->node);
84         kfree(dslot->slot->info);
85         kfree(dslot->slot);
86         pci_dev_put(dslot->dev);
87         kfree(dslot);
88 }
89
90 static int add_slot(struct pci_dev *dev)
91 {
92         struct dummy_slot *dslot;
93         struct hotplug_slot *slot;
94         int retval = -ENOMEM;
95
96         slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
97         if (!slot)
98                 goto error;
99         memset(slot, 0, sizeof(*slot));
100
101         slot->info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
102         if (!slot->info)
103                 goto error_slot;
104         memset(slot->info, 0, sizeof(struct hotplug_slot_info));
105
106         slot->info->power_status = 1;
107         slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
108         slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
109
110         slot->name = &dev->dev.bus_id[0];
111         dbg("slot->name = %s\n", slot->name);
112
113         dslot = kmalloc(sizeof(struct dummy_slot), GFP_KERNEL);
114         if (!dslot)
115                 goto error_info;
116
117         slot->ops = &dummy_hotplug_slot_ops;
118         slot->release = &dummy_release;
119         slot->private = dslot;
120
121         retval = pci_hp_register(slot);
122         if (retval) {
123                 err("pci_hp_register failed with error %d\n", retval);
124                 goto error_dslot;
125         }
126
127         dslot->slot = slot;
128         dslot->dev = pci_dev_get(dev);
129         list_add (&dslot->node, &slot_list);
130         return retval;
131
132 error_dslot:
133         kfree(dslot);
134 error_info:
135         kfree(slot->info);
136 error_slot:
137         kfree(slot);
138 error:
139         return retval;
140 }
141
142 static int __init pci_scan_buses(void)
143 {
144         struct pci_dev *dev = NULL;
145         int retval = 0;
146
147         while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
148                 retval = add_slot(dev);
149                 if (retval) {
150                         pci_dev_put(dev);
151                         break;
152                 }
153         }
154
155         return retval;
156 }
157
158 static void remove_slot(struct dummy_slot *dslot)
159 {
160         int retval;
161
162         dbg("removing slot %s\n", dslot->slot->name);
163         retval = pci_hp_deregister(dslot->slot);
164         if (retval)
165                 err("Problem unregistering a slot %s\n", dslot->slot->name);
166 }
167
168 static int enable_slot(struct hotplug_slot *hotplug_slot)
169 {
170         return -ENODEV;
171 }
172
173 static int disable_slot(struct hotplug_slot *slot)
174 {
175         struct dummy_slot *dslot;
176
177         if (!slot)
178                 return -ENODEV;
179         dslot = slot->private;
180
181         dbg("%s - physical_slot = %s\n", __FUNCTION__, slot->name);
182
183         /* don't disable bridged devices just yet, we can't handle them easily... */
184         if (dslot->dev->subordinate) {
185                 err("Can't remove PCI devices with other PCI devices behind it yet.\n");
186                 return -ENODEV;
187         }
188
189         /* remove the device from the pci core */
190         pci_remove_bus_device(dslot->dev);
191
192         /* blow away this sysfs entry and other parts. */
193         remove_slot(dslot);
194
195         return 0;
196 }
197
198 static void cleanup_slots (void)
199 {
200         struct list_head *tmp;
201         struct list_head *next;
202         struct dummy_slot *dslot;
203
204         list_for_each_safe (tmp, next, &slot_list) {
205                 dslot = list_entry (tmp, struct dummy_slot, node);
206                 remove_slot(dslot);
207         }
208         
209 }
210
211 static int __init dummyphp_init(void)
212 {
213         info(DRIVER_DESC "\n");
214
215         return pci_scan_buses();
216 }
217
218
219 static void __exit dummyphp_exit(void)
220 {
221         cleanup_slots();
222 }
223
224 module_init(dummyphp_init);
225 module_exit(dummyphp_exit);
226
227 MODULE_AUTHOR(DRIVER_AUTHOR);
228 MODULE_DESCRIPTION(DRIVER_DESC);
229 MODULE_LICENSE("GPL");
230 MODULE_PARM(debug, "i");
231 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
232