isa.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ISA bus.
  3. */
  4. #include <linux/device.h>
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/isa.h>
  10. static struct device isa_bus = {
  11. .bus_id = "isa"
  12. };
  13. struct isa_dev {
  14. struct device dev;
  15. struct device *next;
  16. unsigned int id;
  17. };
  18. #define to_isa_dev(x) container_of((x), struct isa_dev, dev)
  19. static int isa_bus_match(struct device *dev, struct device_driver *driver)
  20. {
  21. struct isa_driver *isa_driver = to_isa_driver(driver);
  22. if (dev->platform_data == isa_driver) {
  23. if (!isa_driver->match ||
  24. isa_driver->match(dev, to_isa_dev(dev)->id))
  25. return 1;
  26. dev->platform_data = NULL;
  27. }
  28. return 0;
  29. }
  30. static int isa_bus_probe(struct device *dev)
  31. {
  32. struct isa_driver *isa_driver = dev->platform_data;
  33. if (isa_driver->probe)
  34. return isa_driver->probe(dev, to_isa_dev(dev)->id);
  35. return 0;
  36. }
  37. static int isa_bus_remove(struct device *dev)
  38. {
  39. struct isa_driver *isa_driver = dev->platform_data;
  40. if (isa_driver->remove)
  41. return isa_driver->remove(dev, to_isa_dev(dev)->id);
  42. return 0;
  43. }
  44. static void isa_bus_shutdown(struct device *dev)
  45. {
  46. struct isa_driver *isa_driver = dev->platform_data;
  47. if (isa_driver->shutdown)
  48. isa_driver->shutdown(dev, to_isa_dev(dev)->id);
  49. }
  50. static int isa_bus_suspend(struct device *dev, pm_message_t state)
  51. {
  52. struct isa_driver *isa_driver = dev->platform_data;
  53. if (isa_driver->suspend)
  54. return isa_driver->suspend(dev, to_isa_dev(dev)->id, state);
  55. return 0;
  56. }
  57. static int isa_bus_resume(struct device *dev)
  58. {
  59. struct isa_driver *isa_driver = dev->platform_data;
  60. if (isa_driver->resume)
  61. return isa_driver->resume(dev, to_isa_dev(dev)->id);
  62. return 0;
  63. }
  64. static struct bus_type isa_bus_type = {
  65. .name = "isa",
  66. .match = isa_bus_match,
  67. .probe = isa_bus_probe,
  68. .remove = isa_bus_remove,
  69. .shutdown = isa_bus_shutdown,
  70. .suspend = isa_bus_suspend,
  71. .resume = isa_bus_resume
  72. };
  73. static void isa_dev_release(struct device *dev)
  74. {
  75. kfree(to_isa_dev(dev));
  76. }
  77. void isa_unregister_driver(struct isa_driver *isa_driver)
  78. {
  79. struct device *dev = isa_driver->devices;
  80. while (dev) {
  81. struct device *tmp = to_isa_dev(dev)->next;
  82. device_unregister(dev);
  83. dev = tmp;
  84. }
  85. driver_unregister(&isa_driver->driver);
  86. }
  87. EXPORT_SYMBOL_GPL(isa_unregister_driver);
  88. int isa_register_driver(struct isa_driver *isa_driver, unsigned int ndev)
  89. {
  90. int error;
  91. unsigned int id;
  92. isa_driver->driver.bus = &isa_bus_type;
  93. isa_driver->devices = NULL;
  94. error = driver_register(&isa_driver->driver);
  95. if (error)
  96. return error;
  97. for (id = 0; id < ndev; id++) {
  98. struct isa_dev *isa_dev;
  99. isa_dev = kzalloc(sizeof *isa_dev, GFP_KERNEL);
  100. if (!isa_dev) {
  101. error = -ENOMEM;
  102. break;
  103. }
  104. isa_dev->dev.parent = &isa_bus;
  105. isa_dev->dev.bus = &isa_bus_type;
  106. snprintf(isa_dev->dev.bus_id, BUS_ID_SIZE, "%s.%u",
  107. isa_driver->driver.name, id);
  108. isa_dev->dev.platform_data = isa_driver;
  109. isa_dev->dev.release = isa_dev_release;
  110. isa_dev->id = id;
  111. error = device_register(&isa_dev->dev);
  112. if (error) {
  113. put_device(&isa_dev->dev);
  114. break;
  115. }
  116. if (isa_dev->dev.platform_data) {
  117. isa_dev->next = isa_driver->devices;
  118. isa_driver->devices = &isa_dev->dev;
  119. } else
  120. device_unregister(&isa_dev->dev);
  121. }
  122. if (!error && !isa_driver->devices)
  123. error = -ENODEV;
  124. if (error)
  125. isa_unregister_driver(isa_driver);
  126. return error;
  127. }
  128. EXPORT_SYMBOL_GPL(isa_register_driver);
  129. static int __init isa_bus_init(void)
  130. {
  131. int error;
  132. error = bus_register(&isa_bus_type);
  133. if (!error) {
  134. error = device_register(&isa_bus);
  135. if (error)
  136. bus_unregister(&isa_bus_type);
  137. }
  138. return error;
  139. }
  140. device_initcall(isa_bus_init);