remove.c 3.0 KB

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