vserver 1.9.3
[linux-2.6.git] / drivers / pci / remove.c
1 #include <linux/pci.h>
2 #include <linux/module.h>
3 #include "pci.h"
4
5 #undef DEBUG
6
7 #ifdef DEBUG
8 #define DBG(x...) printk(x)
9 #else
10 #define DBG(x...)
11 #endif
12
13 static void pci_free_resources(struct pci_dev *dev)
14 {
15         int i;
16
17         msi_remove_pci_irq_vectors(dev);
18
19         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
20                 struct resource *res = dev->resource + i;
21                 if (res->parent)
22                         release_resource(res);
23         }
24 }
25
26 static void pci_destroy_dev(struct pci_dev *dev)
27 {
28         pci_proc_detach_device(dev);
29         device_unregister(&dev->dev);
30
31         /* Remove the device from the device lists, and prevent any further
32          * list accesses from this device */
33         spin_lock(&pci_bus_lock);
34         list_del(&dev->bus_list);
35         list_del(&dev->global_list);
36         dev->bus_list.next = dev->bus_list.prev = NULL;
37         dev->global_list.next = dev->global_list.prev = NULL;
38         spin_unlock(&pci_bus_lock);
39
40         pci_free_resources(dev);
41         pci_dev_put(dev);
42 }
43
44 /**
45  * pci_remove_device_safe - remove an unused hotplug device
46  * @dev: the device to remove
47  *
48  * Delete the device structure from the device lists and 
49  * notify userspace (/sbin/hotplug), but only if the device
50  * in question is not being used by a driver.
51  * Returns 0 on success.
52  */
53 int pci_remove_device_safe(struct pci_dev *dev)
54 {
55         if (pci_dev_driver(dev))
56                 return -EBUSY;
57         pci_destroy_dev(dev);
58         return 0;
59 }
60 EXPORT_SYMBOL(pci_remove_device_safe);
61
62 void pci_remove_bus(struct pci_bus *b)
63 {
64         pci_proc_detach_bus(b);
65
66         spin_lock(&pci_bus_lock);
67         list_del(&b->node);
68         spin_unlock(&pci_bus_lock);
69
70         class_device_unregister(&b->class_dev);
71 }
72 EXPORT_SYMBOL(pci_remove_bus);
73
74 /**
75  * pci_remove_bus_device - remove a PCI device and any children
76  * @dev: the device to remove
77  *
78  * Remove a PCI device from the device lists, informing the drivers
79  * that the device has been removed.  We also remove any subordinate
80  * buses and children in a depth-first manner.
81  *
82  * For each device we remove, delete the device structure from the
83  * device lists, remove the /proc entry, and notify userspace
84  * (/sbin/hotplug).
85  */
86 void pci_remove_bus_device(struct pci_dev *dev)
87 {
88         if (dev->subordinate) {
89                 struct pci_bus *b = dev->subordinate;
90
91                 pci_remove_behind_bridge(dev);
92                 pci_remove_bus(b);
93                 dev->subordinate = NULL;
94         }
95
96         pci_destroy_dev(dev);
97 }
98
99 /**
100  * pci_remove_behind_bridge - remove all devices behind a PCI bridge
101  * @dev: PCI bridge device
102  *
103  * Remove all devices on the bus, except for the parent bridge.
104  * This also removes any child buses, and any devices they may
105  * contain in a depth-first manner.
106  */
107 void pci_remove_behind_bridge(struct pci_dev *dev)
108 {
109         struct list_head *l, *n;
110
111         if (dev->subordinate) {
112                 list_for_each_safe(l, n, &dev->subordinate->devices) {
113                         struct pci_dev *dev = pci_dev_b(l);
114
115                         pci_remove_bus_device(dev);
116                 }
117         }
118 }
119
120 EXPORT_SYMBOL(pci_remove_bus_device);
121 EXPORT_SYMBOL(pci_remove_behind_bridge);