blob: e4c48e329367d93617635e65a671ffb2816a47f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DIO Driver Services
3 *
4 * Copyright (C) 2004 Jochen Friedrich
5 *
6 * Loosely based on drivers/pci/pci-driver.c and drivers/zorro/zorro-driver.c
7 *
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.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/dio.h>
16
17
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
23 *
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.
27 */
28
29const struct dio_device_id *
30dio_match_device(const struct dio_device_id *ids,
31 const struct dio_dev *d)
32{
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;
42 }
43 ids++;
44 }
45 return NULL;
46}
47
48static int dio_device_probe(struct device *dev)
49{
50 int error = 0;
51 struct dio_driver *drv = to_dio_driver(dev->driver);
52 struct dio_dev *d = to_dio_dev(dev);
53
54 if (!d->driver && drv->probe) {
55 const struct dio_device_id *id;
56
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;
63 }
64 }
65 return error;
66}
67
68
69 /**
70 * dio_register_driver - register a new DIO driver
71 * @drv: the driver structure to register
72 *
73 * Adds the driver structure to the list of registered drivers
Bjorn Helgaase51c01b2006-03-25 03:07:17 -080074 * Returns zero or a negative error value.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76
77int dio_register_driver(struct dio_driver *drv)
78{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* initialize common driver fields */
80 drv->driver.name = drv->name;
81 drv->driver.bus = &dio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* register with core */
Bjorn Helgaase51c01b2006-03-25 03:07:17 -080084 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87
88 /**
89 * dio_unregister_driver - unregister a DIO driver
90 * @drv: the driver structure to unregister
91 *
92 * Deletes the driver structure from the list of registered DIO drivers,
93 * gives it a chance to clean up by calling its remove() function for
94 * each device it was responsible for, and marks those devices as
95 * driverless.
96 */
97
98void dio_unregister_driver(struct dio_driver *drv)
99{
100 driver_unregister(&drv->driver);
101}
102
103
104 /**
105 * dio_bus_match - Tell if a DIO device structure has a matching DIO
106 * device id structure
107 * @ids: array of DIO device id structures to search in
108 * @dev: the DIO device structure to match against
109 *
110 * Used by a driver to check whether a DIO device present in the
111 * system is in its list of supported devices. Returns the matching
112 * dio_device_id structure or %NULL if there is no match.
113 */
114
115static int dio_bus_match(struct device *dev, struct device_driver *drv)
116{
117 struct dio_dev *d = to_dio_dev(dev);
118 struct dio_driver *dio_drv = to_dio_driver(drv);
119 const struct dio_device_id *ids = dio_drv->id_table;
120
121 if (!ids)
122 return 0;
123
124 while (ids->id) {
125 if (ids->id == DIO_WILDCARD)
126 return 1;
127 if (DIO_NEEDSSECID(ids->id & 0xff)) {
128 if (ids->id == d->id)
129 return 1;
130 } else {
131 if ((ids->id & 0xff) == (d->id & 0xff))
132 return 1;
133 }
134 ids++;
135 }
136 return 0;
137}
138
139
140struct bus_type dio_bus_type = {
141 .name = "dio",
Russell King5b34bf82006-01-05 14:37:18 +0000142 .match = dio_bus_match,
143 .probe = dio_device_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146
147static int __init dio_driver_init(void)
148{
149 return bus_register(&dio_bus_type);
150}
151
152postcore_initcall(dio_driver_init);
153
154EXPORT_SYMBOL(dio_match_device);
155EXPORT_SYMBOL(dio_register_driver);
156EXPORT_SYMBOL(dio_unregister_driver);
157EXPORT_SYMBOL(dio_dev_driver);
158EXPORT_SYMBOL(dio_bus_type);