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