isa.c 3.8 KB

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