aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorRene Herman <rene.herman@keyaccess.nl>2006-06-06 17:54:02 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2006-06-21 15:40:49 -0400
commita5117ba7da37deb09df5eb802dace229b3fb1e9f (patch)
treeeec1e160cbc11a4a1dae107ed27d92c991f5fcf6 /drivers
parent3e95637a48820ff8bedb33e6439def96ccff1de5 (diff)
[PATCH] Driver model: add ISA bus
During the recent "isa drivers using platform devices" discussion it was pointed out that (ALSA) ISA drivers ran into the problem of not having the option to fail driver load (device registration rather) upon not finding their hardware due to a probe() error not being passed up through the driver model. In the course of that, I suggested a seperate ISA bus might be best; Russell King agreed and suggested this bus could use the .match() method for the actual device discovery. The attached does this. For this old non (generically) discoverable ISA hardware only the driver itself can do discovery so as a difference with the platform_bus, this isa_bus also distributes match() up to the driver. As another difference: these devices only exist in the driver model due to the driver creating them because it might want to drive them, meaning that all device creation has been made internal as well. The usage model this provides is nice, and has been acked from the ALSA side by Takashi Iwai and Jaroslav Kysela. The ALSA driver module_init's now (for oldisa-only drivers) become: static int __init alsa_card_foo_init(void) { return isa_register_driver(&snd_foo_isa_driver, SNDRV_CARDS); } static void __exit alsa_card_foo_exit(void) { isa_unregister_driver(&snd_foo_isa_driver); } Quite like the other bus models therefore. This removes a lot of duplicated init code from the ALSA ISA drivers. The passed in isa_driver struct is the regular driver struct embedding a struct device_driver, the normal probe/remove/shutdown/suspend/resume callbacks, and as indicated that .match callback. The "SNDRV_CARDS" you see being passed in is a "unsigned int ndev" parameter, indicating how many devices to create and call our methods with. The platform_driver callbacks are called with a platform_device param; the isa_driver callbacks are being called with a "struct device *dev, unsigned int id" pair directly -- with the device creation completely internal to the bus it's much cleaner to not leak isa_dev's by passing them in at all. The id is the only thing we ever want other then the struct device * anyways, and it makes for nicer code in the callbacks as well. With this additional .match() callback ISA drivers have all options. If ALSA would want to keep the old non-load behaviour, it could stick all of the old .probe in .match, which would only keep them registered after everything was found to be present and accounted for. If it wanted the behaviour of always loading as it inadvertently did for a bit after the changeover to platform devices, it could just not provide a .match() and do everything in .probe() as before. If it, as Takashi Iwai already suggested earlier as a way of following the model from saner buses more closely, wants to load when a later bind could conceivably succeed, it could use .match() for the prerequisites (such as checking the user wants the card enabled and that port/irq/dma values have been passed in) and .probe() for everything else. This is the nicest model. To the code... This exports only two functions; isa_{,un}register_driver(). isa_register_driver() register's the struct device_driver, and then loops over the passed in ndev creating devices and registering them. This causes the bus match method to be called for them, which is: int isa_bus_match(struct device *dev, struct device_driver *driver) { struct isa_driver *isa_driver = to_isa_driver(driver); if (dev->platform_data == isa_driver) { if (!isa_driver->match || isa_driver->match(dev, to_isa_dev(dev)->id)) return 1; dev->platform_data = NULL; } return 0; } The first thing this does is check if this device is in fact one of this driver's devices by seeing if the device's platform_data pointer is set to this driver. Platform devices compare strings, but we don't need to do that with everything being internal, so isa_register_driver() abuses dev->platform_data as a isa_driver pointer which we can then check here. I believe platform_data is available for this, but if rather not, moving the isa_driver pointer to the private struct isa_dev is ofcourse fine as well. Then, if the the driver did not provide a .match, it matches. If it did, the driver match() method is called to determine a match. If it did _not_ match, dev->platform_data is reset to indicate this to isa_register_driver which can then unregister the device again. If during all this, there's any error, or no devices matched at all everything is backed out again and the error, or -ENODEV, is returned. isa_unregister_driver() just unregisters the matched devices and the driver itself. More global points/questions... - I'm introducing include/linux/isa.h. It was available but is ofcourse a somewhat generic name. Moving more isa stuff over to it in time is ofcourse fine, so can I have it please? :) - I'm using device_initcall() and added the isa.o (dependent on CONFIG_ISA) after the base driver model things in the Makefile. Will this do, or I really need to stick it in drivers/base/init.c, inside #ifdef CONFIG_ISA? It's working fine. Lastly -- I also looked, a bit, into integrating with PnP. "Old ISA" could be another pnp_protocol, but this does not seem to be a good match, largely due to the same reason platform_devices weren't -- the devices do not have a life of their own outside the driver, meaning the pnp_protocol {get,set}_resources callbacks would need to callback into driver -- which again means you first need to _have_ that driver. Even if there's clean way around that, you only end up inventing fake but valid-form PnP IDs and generally catering to the PnP layer without any practical advantages over this very simple isa_bus. The thing I also suggested earlier about the user echoing values into /sys to set up the hardware from userspace first is... well, cute, but a horrible idea from a user standpoint. Comments ofcourse appreciated. Hope it's okay. As said, the usage model is nice at least. Signed-off-by: Rene Herman <rene.herman@keyaccess.nl>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/base/Makefile1
-rw-r--r--drivers/base/isa.c180
2 files changed, 181 insertions, 0 deletions
diff --git a/drivers/base/Makefile b/drivers/base/Makefile
index 659cde6c2fb..b539e5e75b5 100644
--- a/drivers/base/Makefile
+++ b/drivers/base/Makefile
@@ -5,6 +5,7 @@ obj-y := core.o sys.o bus.o dd.o \
5 cpu.o firmware.o init.o map.o dmapool.o \ 5 cpu.o firmware.o init.o map.o dmapool.o \
6 attribute_container.o transport_class.o 6 attribute_container.o transport_class.o
7obj-y += power/ 7obj-y += power/
8obj-$(CONFIG_ISA) += isa.o
8obj-$(CONFIG_FW_LOADER) += firmware_class.o 9obj-$(CONFIG_FW_LOADER) += firmware_class.o
9obj-$(CONFIG_NUMA) += node.o 10obj-$(CONFIG_NUMA) += node.o
10obj-$(CONFIG_MEMORY_HOTPLUG) += memory.o 11obj-$(CONFIG_MEMORY_HOTPLUG) += memory.o
diff --git a/drivers/base/isa.c b/drivers/base/isa.c
new file mode 100644
index 00000000000..d2222397a40
--- /dev/null
+++ b/drivers/base/isa.c
@@ -0,0 +1,180 @@
1/*
2 * ISA bus.
3 */
4
5#include <linux/device.h>
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/isa.h>
11
12static struct device isa_bus = {
13 .bus_id = "isa"
14};
15
16struct isa_dev {
17 struct device dev;
18 struct device *next;
19 unsigned int id;
20};
21
22#define to_isa_dev(x) container_of((x), struct isa_dev, dev)
23
24static int isa_bus_match(struct device *dev, struct device_driver *driver)
25{
26 struct isa_driver *isa_driver = to_isa_driver(driver);
27
28 if (dev->platform_data == isa_driver) {
29 if (!isa_driver->match ||
30 isa_driver->match(dev, to_isa_dev(dev)->id))
31 return 1;
32 dev->platform_data = NULL;
33 }
34 return 0;
35}
36
37static int isa_bus_probe(struct device *dev)
38{
39 struct isa_driver *isa_driver = dev->platform_data;
40
41 if (isa_driver->probe)
42 return isa_driver->probe(dev, to_isa_dev(dev)->id);
43
44 return 0;
45}
46
47static int isa_bus_remove(struct device *dev)
48{
49 struct isa_driver *isa_driver = dev->platform_data;
50
51 if (isa_driver->remove)
52 return isa_driver->remove(dev, to_isa_dev(dev)->id);
53
54 return 0;
55}
56
57static void isa_bus_shutdown(struct device *dev)
58{
59 struct isa_driver *isa_driver = dev->platform_data;
60
61 if (isa_driver->shutdown)
62 isa_driver->shutdown(dev, to_isa_dev(dev)->id);
63}
64
65static int isa_bus_suspend(struct device *dev, pm_message_t state)
66{
67 struct isa_driver *isa_driver = dev->platform_data;
68
69 if (isa_driver->suspend)
70 return isa_driver->suspend(dev, to_isa_dev(dev)->id, state);
71
72 return 0;
73}
74
75static int isa_bus_resume(struct device *dev)
76{
77 struct isa_driver *isa_driver = dev->platform_data;
78
79 if (isa_driver->resume)
80 return isa_driver->resume(dev, to_isa_dev(dev)->id);
81
82 return 0;
83}
84
85static struct bus_type isa_bus_type = {
86 .name = "isa",
87 .match = isa_bus_match,
88 .probe = isa_bus_probe,
89 .remove = isa_bus_remove,
90 .shutdown = isa_bus_shutdown,
91 .suspend = isa_bus_suspend,
92 .resume = isa_bus_resume
93};
94
95static void isa_dev_release(struct device *dev)
96{
97 kfree(to_isa_dev(dev));
98}
99
100void isa_unregister_driver(struct isa_driver *isa_driver)
101{
102 struct device *dev = isa_driver->devices;
103
104 while (dev) {
105 struct device *tmp = to_isa_dev(dev)->next;
106 device_unregister(dev);
107 dev = tmp;
108 }
109 driver_unregister(&isa_driver->driver);
110}
111EXPORT_SYMBOL_GPL(isa_unregister_driver);
112
113int isa_register_driver(struct isa_driver *isa_driver, unsigned int ndev)
114{
115 int error;
116 unsigned int id;
117
118 isa_driver->driver.bus = &isa_bus_type;
119 isa_driver->devices = NULL;
120
121 error = driver_register(&isa_driver->driver);
122 if (error)
123 return error;
124
125 for (id = 0; id < ndev; id++) {
126 struct isa_dev *isa_dev;
127
128 isa_dev = kzalloc(sizeof *isa_dev, GFP_KERNEL);
129 if (!isa_dev) {
130 error = -ENOMEM;
131 break;
132 }
133
134 isa_dev->dev.parent = &isa_bus;
135 isa_dev->dev.bus = &isa_bus_type;
136
137 snprintf(isa_dev->dev.bus_id, BUS_ID_SIZE, "%s.%u",
138 isa_driver->driver.name, id);
139
140 isa_dev->dev.platform_data = isa_driver;
141 isa_dev->dev.release = isa_dev_release;
142 isa_dev->id = id;
143
144 error = device_register(&isa_dev->dev);
145 if (error) {
146 put_device(&isa_dev->dev);
147 break;
148 }
149
150 if (isa_dev->dev.platform_data) {
151 isa_dev->next = isa_driver->devices;
152 isa_driver->devices = &isa_dev->dev;
153 } else
154 device_unregister(&isa_dev->dev);
155 }
156
157 if (!error && !isa_driver->devices)
158 error = -ENODEV;
159
160 if (error)
161 isa_unregister_driver(isa_driver);
162
163 return error;
164}
165EXPORT_SYMBOL_GPL(isa_register_driver);
166
167static int __init isa_bus_init(void)
168{
169 int error;
170
171 error = bus_register(&isa_bus_type);
172 if (!error) {
173 error = device_register(&isa_bus);
174 if (error)
175 bus_unregister(&isa_bus_type);
176 }
177 return error;
178}
179
180device_initcall(isa_bus_init);