[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / pci / hotplug / fakephp.c
blob8e47fa66e25ecaa2e8a9a024d6f6473aa2df0421
1 /*
2 * Fake PCI Hot Plug Controller Driver
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>
8 * Based on ideas and code from:
9 * Vladimir Kondratiev <vladimir.kondratiev@intel.com>
10 * Rolf Eike Beer <eike-kernel@sf-tec.de>
12 * All rights reserved.
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.
18 * Send feedback to <greg@kroah.com>
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.
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.
30 * Use this module at your own risk, you have been warned!
32 * Enabling PCI devices is left as an exercise for the reader...
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"
43 #if !defined(MODULE)
44 #define MY_NAME "fakephp"
45 #else
46 #define MY_NAME THIS_MODULE->name
47 #endif
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)
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
59 #define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
61 struct dummy_slot {
62 struct list_head node;
63 struct hotplug_slot *slot;
64 struct pci_dev *dev;
67 static int debug;
68 static LIST_HEAD(slot_list);
70 static int enable_slot (struct hotplug_slot *slot);
71 static int disable_slot (struct hotplug_slot *slot);
73 static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
74 .owner = THIS_MODULE,
75 .enable_slot = enable_slot,
76 .disable_slot = disable_slot,
79 static void dummy_release(struct hotplug_slot *slot)
81 struct dummy_slot *dslot = slot->private;
83 list_del(&dslot->node);
84 kfree(dslot->slot->info);
85 kfree(dslot->slot);
86 pci_dev_put(dslot->dev);
87 kfree(dslot);
90 static int add_slot(struct pci_dev *dev)
92 struct dummy_slot *dslot;
93 struct hotplug_slot *slot;
94 int retval = -ENOMEM;
96 slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
97 if (!slot)
98 goto error;
99 memset(slot, 0, sizeof(*slot));
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));
106 slot->info->power_status = 1;
107 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
108 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
110 slot->name = &dev->dev.bus_id[0];
111 dbg("slot->name = %s\n", slot->name);
113 dslot = kmalloc(sizeof(struct dummy_slot), GFP_KERNEL);
114 if (!dslot)
115 goto error_info;
117 slot->ops = &dummy_hotplug_slot_ops;
118 slot->release = &dummy_release;
119 slot->private = dslot;
121 retval = pci_hp_register(slot);
122 if (retval) {
123 err("pci_hp_register failed with error %d\n", retval);
124 goto error_dslot;
127 dslot->slot = slot;
128 dslot->dev = pci_dev_get(dev);
129 list_add (&dslot->node, &slot_list);
130 return retval;
132 error_dslot:
133 kfree(dslot);
134 error_info:
135 kfree(slot->info);
136 error_slot:
137 kfree(slot);
138 error:
139 return retval;
142 static int __init pci_scan_buses(void)
144 struct pci_dev *dev = NULL;
145 int retval = 0;
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;
155 return retval;
158 static void remove_slot(struct dummy_slot *dslot)
160 int retval;
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);
169 * Rescan slot.
170 * Tries hard not to re-enable already existing devices
171 * also handles scanning of subfunctions
173 * @param temp Device template. Should be set: bus and devfn.
175 static void pci_rescan_slot(struct pci_dev *temp)
177 struct pci_bus *bus = temp->bus;
178 struct pci_dev *dev;
179 int func;
180 u8 hdr_type;
181 if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
182 temp->hdr_type = hdr_type & 0x7f;
183 if (!pci_find_slot(bus->number, temp->devfn)) {
184 dev = pci_scan_single_device(bus, temp->devfn);
185 if (dev) {
186 dbg("New device on %s function %x:%x\n",
187 bus->name, temp->devfn >> 3,
188 temp->devfn & 7);
189 pci_bus_add_device(dev);
190 add_slot(dev);
193 /* multifunction device? */
194 if (!(hdr_type & 0x80))
195 return;
197 /* continue scanning for other functions */
198 for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) {
199 if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
200 continue;
201 temp->hdr_type = hdr_type & 0x7f;
203 if (!pci_find_slot(bus->number, temp->devfn)) {
204 dev = pci_scan_single_device(bus, temp->devfn);
205 if (dev) {
206 dbg("New device on %s function %x:%x\n",
207 bus->name, temp->devfn >> 3,
208 temp->devfn & 7);
209 pci_bus_add_device(dev);
210 add_slot(dev);
219 * Rescan PCI bus.
220 * call pci_rescan_slot for each possible function of the bus
222 * @param bus
224 static void pci_rescan_bus(const struct pci_bus *bus)
226 unsigned int devfn;
227 struct pci_dev *dev;
228 dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
229 if (!dev)
230 return;
232 memset(dev, 0, sizeof(dev));
233 dev->bus = (struct pci_bus*)bus;
234 dev->sysdata = bus->sysdata;
235 for (devfn = 0; devfn < 0x100; devfn += 8) {
236 dev->devfn = devfn;
237 pci_rescan_slot(dev);
239 kfree(dev);
242 /* recursively scan all buses */
243 static void pci_rescan_buses(const struct list_head *list)
245 const struct list_head *l;
246 list_for_each(l,list) {
247 const struct pci_bus *b = pci_bus_b(l);
248 pci_rescan_bus(b);
249 pci_rescan_buses(&b->children);
253 /* initiate rescan of all pci buses */
254 static inline void pci_rescan(void) {
255 pci_rescan_buses(&pci_root_buses);
259 static int enable_slot(struct hotplug_slot *hotplug_slot)
261 /* mis-use enable_slot for rescanning of the pci bus */
262 pci_rescan();
263 return -ENODEV;
266 /* find the hotplug_slot for the pci_dev */
267 static struct hotplug_slot *get_slot_from_dev(struct pci_dev *dev)
269 struct dummy_slot *dslot;
271 list_for_each_entry(dslot, &slot_list, node) {
272 if (dslot->dev == dev)
273 return dslot->slot;
275 return NULL;
279 static int disable_slot(struct hotplug_slot *slot)
281 struct dummy_slot *dslot;
282 struct hotplug_slot *hslot;
283 struct pci_dev *dev;
284 int func;
286 if (!slot)
287 return -ENODEV;
288 dslot = slot->private;
290 dbg("%s - physical_slot = %s\n", __FUNCTION__, slot->name);
292 /* don't disable bridged devices just yet, we can't handle them easily... */
293 if (dslot->dev->subordinate) {
294 err("Can't remove PCI devices with other PCI devices behind it yet.\n");
295 return -ENODEV;
297 /* search for subfunctions and disable them first */
298 if (!(dslot->dev->devfn & 7)) {
299 for (func = 1; func < 8; func++) {
300 dev = pci_find_slot(dslot->dev->bus->number,
301 dslot->dev->devfn + func);
302 if (dev) {
303 hslot = get_slot_from_dev(dev);
304 if (hslot)
305 disable_slot(hslot);
306 else {
307 err("Hotplug slot not found for subfunction of PCI device\n");
308 return -ENODEV;
310 } else
311 dbg("No device in slot found\n");
315 /* remove the device from the pci core */
316 pci_remove_bus_device(dslot->dev);
318 /* blow away this sysfs entry and other parts. */
319 remove_slot(dslot);
321 return 0;
324 static void cleanup_slots (void)
326 struct list_head *tmp;
327 struct list_head *next;
328 struct dummy_slot *dslot;
330 list_for_each_safe (tmp, next, &slot_list) {
331 dslot = list_entry (tmp, struct dummy_slot, node);
332 remove_slot(dslot);
337 static int __init dummyphp_init(void)
339 info(DRIVER_DESC "\n");
341 return pci_scan_buses();
345 static void __exit dummyphp_exit(void)
347 cleanup_slots();
350 module_init(dummyphp_init);
351 module_exit(dummyphp_exit);
353 MODULE_AUTHOR(DRIVER_AUTHOR);
354 MODULE_DESCRIPTION(DRIVER_DESC);
355 MODULE_LICENSE("GPL");
356 module_param(debug, bool, S_IRUGO | S_IWUSR);
357 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");