Back to home page

LXR

 
 

    


0001 ISA Drivers
0002 -----------
0003 
0004 The following text is adapted from the commit message of the initial
0005 commit of the ISA bus driver authored by Rene Herman.
0006 
0007 During the recent "isa drivers using platform devices" discussion it was
0008 pointed out that (ALSA) ISA drivers ran into the problem of not having
0009 the option to fail driver load (device registration rather) upon not
0010 finding their hardware due to a probe() error not being passed up
0011 through the driver model. In the course of that, I suggested a separate
0012 ISA bus might be best; Russell King agreed and suggested this bus could
0013 use the .match() method for the actual device discovery.
0014 
0015 The attached does this. For this old non (generically) discoverable ISA
0016 hardware only the driver itself can do discovery so as a difference with
0017 the platform_bus, this isa_bus also distributes match() up to the
0018 driver.
0019 
0020 As another difference: these devices only exist in the driver model due
0021 to the driver creating them because it might want to drive them, meaning
0022 that all device creation has been made internal as well.
0023 
0024 The usage model this provides is nice, and has been acked from the ALSA
0025 side by Takashi Iwai and Jaroslav Kysela. The ALSA driver module_init's
0026 now (for oldisa-only drivers) become:
0027 
0028 static int __init alsa_card_foo_init(void)
0029 {
0030         return isa_register_driver(&snd_foo_isa_driver, SNDRV_CARDS);
0031 }
0032 
0033 static void __exit alsa_card_foo_exit(void)
0034 {
0035         isa_unregister_driver(&snd_foo_isa_driver);
0036 }
0037 
0038 Quite like the other bus models therefore. This removes a lot of
0039 duplicated init code from the ALSA ISA drivers.
0040 
0041 The passed in isa_driver struct is the regular driver struct embedding a
0042 struct device_driver, the normal probe/remove/shutdown/suspend/resume
0043 callbacks, and as indicated that .match callback.
0044 
0045 The "SNDRV_CARDS" you see being passed in is a "unsigned int ndev"
0046 parameter, indicating how many devices to create and call our methods
0047 with.
0048 
0049 The platform_driver callbacks are called with a platform_device param;
0050 the isa_driver callbacks are being called with a "struct device *dev,
0051 unsigned int id" pair directly -- with the device creation completely
0052 internal to the bus it's much cleaner to not leak isa_dev's by passing
0053 them in at all. The id is the only thing we ever want other then the
0054 struct device * anyways, and it makes for nicer code in the callbacks as
0055 well.
0056 
0057 With this additional .match() callback ISA drivers have all options. If
0058 ALSA would want to keep the old non-load behaviour, it could stick all
0059 of the old .probe in .match, which would only keep them registered after
0060 everything was found to be present and accounted for. If it wanted the
0061 behaviour of always loading as it inadvertently did for a bit after the
0062 changeover to platform devices, it could just not provide a .match() and
0063 do everything in .probe() as before.
0064 
0065 If it, as Takashi Iwai already suggested earlier as a way of following
0066 the model from saner buses more closely, wants to load when a later bind
0067 could conceivably succeed, it could use .match() for the prerequisites
0068 (such as checking the user wants the card enabled and that port/irq/dma
0069 values have been passed in) and .probe() for everything else. This is
0070 the nicest model.
0071 
0072 To the code...
0073 
0074 This exports only two functions; isa_{,un}register_driver().
0075 
0076 isa_register_driver() register's the struct device_driver, and then
0077 loops over the passed in ndev creating devices and registering them.
0078 This causes the bus match method to be called for them, which is:
0079 
0080 int isa_bus_match(struct device *dev, struct device_driver *driver)
0081 {
0082           struct isa_driver *isa_driver = to_isa_driver(driver);
0083 
0084           if (dev->platform_data == isa_driver) {
0085                   if (!isa_driver->match ||
0086                           isa_driver->match(dev, to_isa_dev(dev)->id))
0087                           return 1;
0088                   dev->platform_data = NULL;
0089           }
0090           return 0;
0091 }
0092 
0093 The first thing this does is check if this device is in fact one of this
0094 driver's devices by seeing if the device's platform_data pointer is set
0095 to this driver. Platform devices compare strings, but we don't need to
0096 do that with everything being internal, so isa_register_driver() abuses
0097 dev->platform_data as a isa_driver pointer which we can then check here.
0098 I believe platform_data is available for this, but if rather not, moving
0099 the isa_driver pointer to the private struct isa_dev is ofcourse fine as
0100 well.
0101 
0102 Then, if the the driver did not provide a .match, it matches. If it did,
0103 the driver match() method is called to determine a match.
0104 
0105 If it did _not_ match, dev->platform_data is reset to indicate this to
0106 isa_register_driver which can then unregister the device again.
0107 
0108 If during all this, there's any error, or no devices matched at all
0109 everything is backed out again and the error, or -ENODEV, is returned.
0110 
0111 isa_unregister_driver() just unregisters the matched devices and the
0112 driver itself.
0113 
0114 module_isa_driver is a helper macro for ISA drivers which do not do
0115 anything special in module init/exit. This eliminates a lot of
0116 boilerplate code. Each module may only use this macro once, and calling
0117 it replaces module_init and module_exit.
0118 
0119 max_num_isa_dev is a macro to determine the maximum possible number of
0120 ISA devices which may be registered in the I/O port address space given
0121 the address extent of the ISA devices.