remove.c 2.9 KB

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