remove.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_stop_dev(struct pci_dev *dev)
  16. {
  17. if (dev->is_added) {
  18. pci_proc_detach_device(dev);
  19. pci_remove_sysfs_dev_files(dev);
  20. device_unregister(&dev->dev);
  21. dev->is_added = 0;
  22. }
  23. }
  24. static void pci_destroy_dev(struct pci_dev *dev)
  25. {
  26. pci_stop_dev(dev);
  27. /* Remove the device from the device lists, and prevent any further
  28. * list accesses from this device */
  29. down_write(&pci_bus_sem);
  30. list_del(&dev->bus_list);
  31. dev->bus_list.next = dev->bus_list.prev = NULL;
  32. up_write(&pci_bus_sem);
  33. pci_free_resources(dev);
  34. pci_dev_put(dev);
  35. }
  36. /**
  37. * pci_remove_device_safe - remove an unused hotplug device
  38. * @dev: the device to remove
  39. *
  40. * Delete the device structure from the device lists and
  41. * notify userspace (/sbin/hotplug), but only if the device
  42. * in question is not being used by a driver.
  43. * Returns 0 on success.
  44. */
  45. #if 0
  46. int pci_remove_device_safe(struct pci_dev *dev)
  47. {
  48. if (pci_dev_driver(dev))
  49. return -EBUSY;
  50. pci_destroy_dev(dev);
  51. return 0;
  52. }
  53. #endif /* 0 */
  54. void pci_remove_bus(struct pci_bus *pci_bus)
  55. {
  56. pci_proc_detach_bus(pci_bus);
  57. down_write(&pci_bus_sem);
  58. list_del(&pci_bus->node);
  59. up_write(&pci_bus_sem);
  60. pci_remove_legacy_files(pci_bus);
  61. device_remove_file(&pci_bus->dev, &dev_attr_cpuaffinity);
  62. device_unregister(&pci_bus->dev);
  63. }
  64. EXPORT_SYMBOL(pci_remove_bus);
  65. /**
  66. * pci_remove_bus_device - remove a PCI device and any children
  67. * @dev: the device to remove
  68. *
  69. * Remove a PCI device from the device lists, informing the drivers
  70. * that the device has been removed. We also remove any subordinate
  71. * buses and children in a depth-first manner.
  72. *
  73. * For each device we remove, delete the device structure from the
  74. * device lists, remove the /proc entry, and notify userspace
  75. * (/sbin/hotplug).
  76. */
  77. void pci_remove_bus_device(struct pci_dev *dev)
  78. {
  79. if (dev->subordinate) {
  80. struct pci_bus *b = dev->subordinate;
  81. pci_remove_behind_bridge(dev);
  82. pci_remove_bus(b);
  83. dev->subordinate = NULL;
  84. }
  85. pci_destroy_dev(dev);
  86. }
  87. /**
  88. * pci_remove_behind_bridge - remove all devices behind a PCI bridge
  89. * @dev: PCI bridge device
  90. *
  91. * Remove all devices on the bus, except for the parent bridge.
  92. * This also removes any child buses, and any devices they may
  93. * contain in a depth-first manner.
  94. */
  95. void pci_remove_behind_bridge(struct pci_dev *dev)
  96. {
  97. struct list_head *l, *n;
  98. if (dev->subordinate) {
  99. list_for_each_safe(l, n, &dev->subordinate->devices) {
  100. struct pci_dev *dev = pci_dev_b(l);
  101. pci_remove_bus_device(dev);
  102. }
  103. }
  104. }
  105. static void pci_stop_bus_devices(struct pci_bus *bus)
  106. {
  107. struct list_head *l, *n;
  108. list_for_each_safe(l, n, &bus->devices) {
  109. struct pci_dev *dev = pci_dev_b(l);
  110. pci_stop_bus_device(dev);
  111. }
  112. }
  113. /**
  114. * pci_stop_bus_device - stop a PCI device and any children
  115. * @dev: the device to stop
  116. *
  117. * Stop a PCI device (detach the driver, remove from the global list
  118. * and so on). This also stop any subordinate buses and children in a
  119. * depth-first manner.
  120. */
  121. void pci_stop_bus_device(struct pci_dev *dev)
  122. {
  123. if (dev->subordinate)
  124. pci_stop_bus_devices(dev->subordinate);
  125. pci_stop_dev(dev);
  126. }
  127. EXPORT_SYMBOL(pci_remove_bus_device);
  128. EXPORT_SYMBOL(pci_remove_behind_bridge);
  129. EXPORT_SYMBOL_GPL(pci_stop_bus_device);