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