isa.c 3.8 KB

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