isa.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ISA Drivers
  2. -----------
  3. The following text is adapted from the commit message of the initial
  4. commit of the ISA bus driver authored by Rene Herman.
  5. During the recent "isa drivers using platform devices" discussion it was
  6. pointed out that (ALSA) ISA drivers ran into the problem of not having
  7. the option to fail driver load (device registration rather) upon not
  8. finding their hardware due to a probe() error not being passed up
  9. through the driver model. In the course of that, I suggested a separate
  10. ISA bus might be best; Russell King agreed and suggested this bus could
  11. use the .match() method for the actual device discovery.
  12. The attached does this. For this old non (generically) discoverable ISA
  13. hardware only the driver itself can do discovery so as a difference with
  14. the platform_bus, this isa_bus also distributes match() up to the
  15. driver.
  16. As another difference: these devices only exist in the driver model due
  17. to the driver creating them because it might want to drive them, meaning
  18. that all device creation has been made internal as well.
  19. The usage model this provides is nice, and has been acked from the ALSA
  20. side by Takashi Iwai and Jaroslav Kysela. The ALSA driver module_init's
  21. now (for oldisa-only drivers) become:
  22. static int __init alsa_card_foo_init(void)
  23. {
  24. return isa_register_driver(&snd_foo_isa_driver, SNDRV_CARDS);
  25. }
  26. static void __exit alsa_card_foo_exit(void)
  27. {
  28. isa_unregister_driver(&snd_foo_isa_driver);
  29. }
  30. Quite like the other bus models therefore. This removes a lot of
  31. duplicated init code from the ALSA ISA drivers.
  32. The passed in isa_driver struct is the regular driver struct embedding a
  33. struct device_driver, the normal probe/remove/shutdown/suspend/resume
  34. callbacks, and as indicated that .match callback.
  35. The "SNDRV_CARDS" you see being passed in is a "unsigned int ndev"
  36. parameter, indicating how many devices to create and call our methods
  37. with.
  38. The platform_driver callbacks are called with a platform_device param;
  39. the isa_driver callbacks are being called with a "struct device *dev,
  40. unsigned int id" pair directly -- with the device creation completely
  41. internal to the bus it's much cleaner to not leak isa_dev's by passing
  42. them in at all. The id is the only thing we ever want other then the
  43. struct device * anyways, and it makes for nicer code in the callbacks as
  44. well.
  45. With this additional .match() callback ISA drivers have all options. If
  46. ALSA would want to keep the old non-load behaviour, it could stick all
  47. of the old .probe in .match, which would only keep them registered after
  48. everything was found to be present and accounted for. If it wanted the
  49. behaviour of always loading as it inadvertently did for a bit after the
  50. changeover to platform devices, it could just not provide a .match() and
  51. do everything in .probe() as before.
  52. If it, as Takashi Iwai already suggested earlier as a way of following
  53. the model from saner buses more closely, wants to load when a later bind
  54. could conceivably succeed, it could use .match() for the prerequisites
  55. (such as checking the user wants the card enabled and that port/irq/dma
  56. values have been passed in) and .probe() for everything else. This is
  57. the nicest model.
  58. To the code...
  59. This exports only two functions; isa_{,un}register_driver().
  60. isa_register_driver() register's the struct device_driver, and then
  61. loops over the passed in ndev creating devices and registering them.
  62. This causes the bus match method to be called for them, which is:
  63. int isa_bus_match(struct device *dev, struct device_driver *driver)
  64. {
  65. struct isa_driver *isa_driver = to_isa_driver(driver);
  66. if (dev->platform_data == isa_driver) {
  67. if (!isa_driver->match ||
  68. isa_driver->match(dev, to_isa_dev(dev)->id))
  69. return 1;
  70. dev->platform_data = NULL;
  71. }
  72. return 0;
  73. }
  74. The first thing this does is check if this device is in fact one of this
  75. driver's devices by seeing if the device's platform_data pointer is set
  76. to this driver. Platform devices compare strings, but we don't need to
  77. do that with everything being internal, so isa_register_driver() abuses
  78. dev->platform_data as a isa_driver pointer which we can then check here.
  79. I believe platform_data is available for this, but if rather not, moving
  80. the isa_driver pointer to the private struct isa_dev is ofcourse fine as
  81. well.
  82. Then, if the the driver did not provide a .match, it matches. If it did,
  83. the driver match() method is called to determine a match.
  84. If it did _not_ match, dev->platform_data is reset to indicate this to
  85. isa_register_driver which can then unregister the device again.
  86. If during all this, there's any error, or no devices matched at all
  87. everything is backed out again and the error, or -ENODEV, is returned.
  88. isa_unregister_driver() just unregisters the matched devices and the
  89. driver itself.
  90. module_isa_driver is a helper macro for ISA drivers which do not do
  91. anything special in module init/exit. This eliminates a lot of
  92. boilerplate code. Each module may only use this macro once, and calling
  93. it replaces module_init and module_exit.
  94. max_num_isa_dev is a macro to determine the maximum possible number of
  95. ISA devices which may be registered in the I/O port address space given
  96. the address extent of the ISA devices.