kbuild: document howto build external modules using several directories
[linux-2.6/verdex.git] / drivers / dio / dio-driver.c
blobffe6f44ac76fcb080e976584c7499f1ace73e13d
1 /*
2 * DIO Driver Services
4 * Copyright (C) 2004 Jochen Friedrich
6 * Loosely based on drivers/pci/pci-driver.c and drivers/zorro/zorro-driver.c
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive
10 * for more details.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/dio.h>
18 /**
19 * dio_match_device - Tell if a DIO device structure has a matching
20 * DIO device id structure
21 * @ids: array of DIO device id structures to search in
22 * @dev: the DIO device structure to match against
24 * Used by a driver to check whether a DIO device present in the
25 * system is in its list of supported devices. Returns the matching
26 * dio_device_id structure or %NULL if there is no match.
29 const struct dio_device_id *
30 dio_match_device(const struct dio_device_id *ids,
31 const struct dio_dev *d)
33 while (ids->id) {
34 if (ids->id == DIO_WILDCARD)
35 return ids;
36 if (DIO_NEEDSSECID(ids->id & 0xff)) {
37 if (ids->id == d->id)
38 return ids;
39 } else {
40 if ((ids->id & 0xff) == (d->id & 0xff))
41 return ids;
43 ids++;
45 return NULL;
48 static int dio_device_probe(struct device *dev)
50 int error = 0;
51 struct dio_driver *drv = to_dio_driver(dev->driver);
52 struct dio_dev *d = to_dio_dev(dev);
54 if (!d->driver && drv->probe) {
55 const struct dio_device_id *id;
57 id = dio_match_device(drv->id_table, d);
58 if (id)
59 error = drv->probe(d, id);
60 if (error >= 0) {
61 d->driver = drv;
62 error = 0;
65 return error;
69 /**
70 * dio_register_driver - register a new DIO driver
71 * @drv: the driver structure to register
73 * Adds the driver structure to the list of registered drivers
74 * Returns the number of DIO devices which were claimed by the driver
75 * during registration. The driver remains registered even if the
76 * return value is zero.
79 int dio_register_driver(struct dio_driver *drv)
81 int count = 0;
83 /* initialize common driver fields */
84 drv->driver.name = drv->name;
85 drv->driver.bus = &dio_bus_type;
86 drv->driver.probe = dio_device_probe;
88 /* register with core */
89 count = driver_register(&drv->driver);
90 return count ? count : 1;
94 /**
95 * dio_unregister_driver - unregister a DIO driver
96 * @drv: the driver structure to unregister
98 * Deletes the driver structure from the list of registered DIO drivers,
99 * gives it a chance to clean up by calling its remove() function for
100 * each device it was responsible for, and marks those devices as
101 * driverless.
104 void dio_unregister_driver(struct dio_driver *drv)
106 driver_unregister(&drv->driver);
111 * dio_bus_match - Tell if a DIO device structure has a matching DIO
112 * device id structure
113 * @ids: array of DIO device id structures to search in
114 * @dev: the DIO device structure to match against
116 * Used by a driver to check whether a DIO device present in the
117 * system is in its list of supported devices. Returns the matching
118 * dio_device_id structure or %NULL if there is no match.
121 static int dio_bus_match(struct device *dev, struct device_driver *drv)
123 struct dio_dev *d = to_dio_dev(dev);
124 struct dio_driver *dio_drv = to_dio_driver(drv);
125 const struct dio_device_id *ids = dio_drv->id_table;
127 if (!ids)
128 return 0;
130 while (ids->id) {
131 if (ids->id == DIO_WILDCARD)
132 return 1;
133 if (DIO_NEEDSSECID(ids->id & 0xff)) {
134 if (ids->id == d->id)
135 return 1;
136 } else {
137 if ((ids->id & 0xff) == (d->id & 0xff))
138 return 1;
140 ids++;
142 return 0;
146 struct bus_type dio_bus_type = {
147 .name = "dio",
148 .match = dio_bus_match
152 static int __init dio_driver_init(void)
154 return bus_register(&dio_bus_type);
157 postcore_initcall(dio_driver_init);
159 EXPORT_SYMBOL(dio_match_device);
160 EXPORT_SYMBOL(dio_register_driver);
161 EXPORT_SYMBOL(dio_unregister_driver);
162 EXPORT_SYMBOL(dio_dev_driver);
163 EXPORT_SYMBOL(dio_bus_type);